1 /*
2 * Copyright (c) 2021-2021 Huawei Device Co., Ltd.
3 *
4 * HDF is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8
9 #include "eth_chip_driver.h"
10 #include "eth_device.h"
11 #include "osal.h"
12
13 #define HDF_LOG_TAG eth_core
14 struct EthConfig *g_ethConfig = NULL;
15
HdfEthGetDriverFactory(const char * driverName)16 static struct HdfEthChipDriverFactory *HdfEthGetDriverFactory(const char *driverName)
17 {
18 struct HdfEthChipDriverManager *initMgr = NULL;
19 if (driverName == NULL) {
20 HDF_LOGE("%s: driverName is ", __func__);
21 return NULL;
22 }
23 initMgr = HdfEthGetChipDriverMgr();
24 if (initMgr == NULL) {
25 HDF_LOGE("%s: initMgr is NULL", __func__);
26 return NULL;
27 }
28 return initMgr->GetEthChipDriverByName(driverName);
29 }
30
DeinitEth(struct EthDevice * ethDevice)31 static int32_t DeinitEth(struct EthDevice *ethDevice)
32 {
33 HDF_LOGD("%s enter", __func__);
34 struct HdfEthChipDriverFactory *ethChipDriverFact = NULL;
35
36 if (ethDevice == NULL) {
37 HDF_LOGE("%s the input ethDevice is NULL!", __func__);
38 return HDF_ERR_INVALID_PARAM;
39 }
40 ethChipDriverFact = HdfEthGetDriverFactory(ethDevice->name);
41 if (ethChipDriverFact == NULL) {
42 HDF_LOGE("%s: get ethChipDriverFact failed! driverName = %s", __func__, ethDevice->name);
43 return HDF_FAILURE;
44 }
45
46 if (ethChipDriverFact->DeinitEthDriver != NULL) {
47 return ethChipDriverFact->DeinitEthDriver(ethDevice);
48 }
49 return HDF_SUCCESS;
50 }
51
52 #if (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
SetEthNetworkAddr(struct NetDevice * netDev)53 static int32_t SetEthNetworkAddr(struct NetDevice *netDev)
54 {
55 IpV4Addr ip, netmask, gw;
56
57 ip.addr = 0x0A01a8c0UL; /* 192, 168, 1. 10 */
58 netmask.addr = 0x00ffffffUL; /* 255, 255, 255, 0 */
59 gw.addr = 0x0101a8c0UL; /* 192, 168, 12, 1 */
60
61 if (NetIfSetAddr(netDev, &ip, &netmask, &gw) != HDF_SUCCESS) {
62 HDF_LOGE("%s fail: NetIfSetAddr error!", __func__);
63 return HDF_FAILURE;
64 }
65 return HDF_SUCCESS;
66 }
67 #endif
68
InitMacInterface(struct EthDevice * ethDevice,struct HdfEthMacChipDriver * macChipDriver)69 static int32_t InitMacInterface(struct EthDevice *ethDevice, struct HdfEthMacChipDriver *macChipDriver)
70 {
71 int32_t ret;
72
73 if (macChipDriver->ethMacOps == NULL) {
74 HDF_LOGE("%s:ethMacOps is NULL.", __func__);
75 return HDF_FAILURE;
76 }
77 if (macChipDriver->ethMacOps->MacInit == NULL) {
78 HDF_LOGE("%s:MacInit is not implement.", __func__);
79 return HDF_FAILURE;
80 }
81 macChipDriver->ethMacOps->MacInit();
82
83 if (macChipDriver->ethMacOps->PortReset == NULL) {
84 HDF_LOGE("%s:PortReset is not implement.", __func__);
85 return HDF_FAILURE;
86 }
87 ret = macChipDriver->ethMacOps->PortReset(ethDevice);
88 if (ret != HDF_SUCCESS) {
89 HDF_LOGE("%s:PortReset failed! ret = %d.", __func__, ret);
90 return HDF_FAILURE;
91 }
92
93 if (macChipDriver->ethMacOps->PortInit == NULL) {
94 HDF_LOGE("%s:PortInit is not implement.", __func__);
95 return HDF_FAILURE;
96 }
97 ret = macChipDriver->ethMacOps->PortInit(ethDevice);
98 if (ret != HDF_SUCCESS) {
99 HDF_LOGE("%s:PortInit failed! ret = %d.", __func__, ret);
100 return HDF_FAILURE;
101 }
102 return HDF_SUCCESS;
103 }
104
InitEth(struct EthDevice * ethDevice,const uint8_t isSetDefault,struct HdfEthChipDriverFactory * ethChipDriverFact)105 static int32_t InitEth(struct EthDevice *ethDevice, const uint8_t isSetDefault,
106 struct HdfEthChipDriverFactory *ethChipDriverFact)
107 {
108 int32_t ret;
109 struct HdfEthNetDeviceData *data = NULL;
110 struct HdfEthMacChipDriver *macChipDriver = NULL;
111 unsigned char enaddr[MAC_ADDR_SIZE] = {0};
112
113 macChipDriver = ethChipDriverFact->BuildMacDriver();
114 if (macChipDriver == NULL) {
115 HDF_LOGE("%s:mac chip driver build fail!", __func__);
116 return HDF_FAILURE;
117 }
118 data = GetEthNetDeviceData(ethDevice->netdev);
119 if (data == NULL) {
120 HDF_LOGE("%s: data is NULL!", __func__);
121 ethChipDriverFact->ReleaseMacDriver(macChipDriver);
122 return HDF_FAILURE;
123 }
124 data->macChipDriver = macChipDriver;
125
126 ret = ethChipDriverFact->InitEthDriver(ethDevice);
127 if (ret != HDF_SUCCESS) {
128 HDF_LOGE("%s: failed to init eth phy", __func__);
129 DeinitEth(ethDevice);
130 ReleaseEthDevice(ethDevice);
131 return ret;
132 }
133
134 ret = InitMacInterface(ethDevice, macChipDriver);
135 if (ret != HDF_SUCCESS) {
136 HDF_LOGE("%s: InitMacInterface error!", __func__);
137 DeinitEth(ethDevice);
138 ReleaseEthDevice(ethDevice);
139 return HDF_FAILURE;
140 }
141 OsalMSleep(DELAY_TIME_LONG);
142
143 ethChipDriverFact->GetMacAddr(enaddr, MAC_ADDR_SIZE);
144 NetIfSetMacAddr(ethDevice->netdev, enaddr, MAC_ADDR_SIZE);
145
146 #if (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
147 ret = SetEthNetworkAddr(ethDevice->netdev);
148 if (ret != HDF_SUCCESS) {
149 HDF_LOGE("%s set eth network addr fail", __func__);
150 DeinitEth(ethDevice);
151 ReleaseEthDevice(ethDevice);
152 return ret;
153 }
154 #endif
155 NetIfSetStatus(ethDevice->netdev, NETIF_UP);
156 return HDF_SUCCESS;
157 }
158
HdfEthDriverInit(struct HdfDeviceObject * deviceObject)159 static int32_t HdfEthDriverInit(struct HdfDeviceObject *deviceObject)
160 {
161 int32_t ret;
162 uint8_t i;
163
164 if (deviceObject == NULL) {
165 HDF_LOGE("%s deviceObject is NULL", __func__);
166 return HDF_ERR_INVALID_PARAM;
167 }
168 g_ethConfig = GetEthConfig(deviceObject->property);
169 if (g_ethConfig == NULL) {
170 HDF_LOGE("%s failed to get g_ethConfig!", __func__);
171 return HDF_FAILURE;
172 }
173 for (i = 0; i < g_ethConfig->deviceListSize; i++) {
174 struct EthDevice *ethDevice = CreateEthDevice(&g_ethConfig->deviceInst[i]);
175 if (ethDevice == NULL) {
176 return HDF_FAILURE;
177 }
178 ethDevice->config = &g_ethConfig->deviceInst[i];
179
180 struct HdfEthChipDriverFactory *ethChipDriverFact = HdfEthGetDriverFactory(ethDevice->name);
181 if (ethChipDriverFact == NULL) {
182 HDF_LOGE("%s: get ethChipDriverFact failed! driverName = %s", __func__, ethDevice->name);
183 ReleaseEthDevice(ethDevice);
184 return HDF_FAILURE;
185 }
186 ret = InitEth(ethDevice, g_ethConfig->deviceInst[i].isSetDefault, ethChipDriverFact);
187 if (ret != HDF_SUCCESS) {
188 HDF_LOGE("%s failed to init eth driver, ret: %d", __func__, ret);
189 return ret;
190 }
191 }
192 HDF_LOGE("%s hdf eth driver framework init success", __func__);
193 return ret;
194 }
195
HdfEthDriverBind(struct HdfDeviceObject * deviceObject)196 static int32_t HdfEthDriverBind(struct HdfDeviceObject *deviceObject)
197 {
198 (void)deviceObject;
199 return HDF_SUCCESS;
200 }
201
HdfEthDriverRelease(struct HdfDeviceObject * deviceObject)202 static void HdfEthDriverRelease(struct HdfDeviceObject *deviceObject)
203 {
204 (void)deviceObject;
205 }
206
207 struct HdfDriverEntry g_ethEntry = {
208 .moduleVersion = 1,
209 .Bind = HdfEthDriverBind,
210 .Init = HdfEthDriverInit,
211 .Release = HdfEthDriverRelease,
212 .moduleName = "HDF_ETHERNET"
213 };
214
215 HDF_INIT(g_ethEntry);