1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Aquantia Corp. Aquantia AQtion USB to 5GbE Controller
3 * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
4 * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
5 * Copyright (C) 2002-2003 TiVo Inc.
6 * Copyright (C) 2017-2018 ASIX
7 * Copyright (C) 2018 Aquantia Corp.
8 */
9
10 #include <linux/module.h>
11 #include <linux/netdevice.h>
12 #include <linux/ethtool.h>
13 #include <linux/mii.h>
14 #include <linux/usb.h>
15 #include <linux/crc32.h>
16 #include <linux/if_vlan.h>
17 #include <linux/usb/cdc.h>
18 #include <linux/usb/usbnet.h>
19 #include <linux/linkmode.h>
20
21 #include "aqc111.h"
22
aqc111_read_cmd_nopm(struct usbnet * dev,u8 cmd,u16 value,u16 index,u16 size,void * data)23 static int aqc111_read_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value,
24 u16 index, u16 size, void *data)
25 {
26 int ret;
27
28 ret = usbnet_read_cmd_nopm(dev, cmd, USB_DIR_IN | USB_TYPE_VENDOR |
29 USB_RECIP_DEVICE, value, index, data, size);
30
31 if (unlikely(ret < size)) {
32 netdev_warn(dev->net,
33 "Failed to read(0x%x) reg index 0x%04x: %d\n",
34 cmd, index, ret);
35
36 ret = ret < 0 ? ret : -ENODATA;
37 }
38
39 return ret;
40 }
41
aqc111_read_cmd(struct usbnet * dev,u8 cmd,u16 value,u16 index,u16 size,void * data)42 static int aqc111_read_cmd(struct usbnet *dev, u8 cmd, u16 value,
43 u16 index, u16 size, void *data)
44 {
45 int ret;
46
47 ret = usbnet_read_cmd(dev, cmd, USB_DIR_IN | USB_TYPE_VENDOR |
48 USB_RECIP_DEVICE, value, index, data, size);
49
50 if (unlikely(ret < size)) {
51 netdev_warn(dev->net,
52 "Failed to read(0x%x) reg index 0x%04x: %d\n",
53 cmd, index, ret);
54
55 ret = ret < 0 ? ret : -ENODATA;
56 }
57
58 return ret;
59 }
60
aqc111_read16_cmd_nopm(struct usbnet * dev,u8 cmd,u16 value,u16 index,u16 * data)61 static int aqc111_read16_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value,
62 u16 index, u16 *data)
63 {
64 int ret = 0;
65
66 ret = aqc111_read_cmd_nopm(dev, cmd, value, index, sizeof(*data), data);
67 le16_to_cpus(data);
68
69 return ret;
70 }
71
aqc111_read16_cmd(struct usbnet * dev,u8 cmd,u16 value,u16 index,u16 * data)72 static int aqc111_read16_cmd(struct usbnet *dev, u8 cmd, u16 value,
73 u16 index, u16 *data)
74 {
75 int ret = 0;
76
77 ret = aqc111_read_cmd(dev, cmd, value, index, sizeof(*data), data);
78 le16_to_cpus(data);
79
80 return ret;
81 }
82
__aqc111_write_cmd(struct usbnet * dev,u8 cmd,u8 reqtype,u16 value,u16 index,u16 size,const void * data)83 static int __aqc111_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
84 u16 value, u16 index, u16 size, const void *data)
85 {
86 int err = -ENOMEM;
87 void *buf = NULL;
88
89 netdev_dbg(dev->net,
90 "%s cmd=%#x reqtype=%#x value=%#x index=%#x size=%d\n",
91 __func__, cmd, reqtype, value, index, size);
92
93 if (data) {
94 buf = kmemdup(data, size, GFP_KERNEL);
95 if (!buf)
96 goto out;
97 }
98
99 err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
100 cmd, reqtype, value, index, buf, size,
101 (cmd == AQ_PHY_POWER) ? AQ_USB_PHY_SET_TIMEOUT :
102 AQ_USB_SET_TIMEOUT);
103
104 if (unlikely(err < 0))
105 netdev_warn(dev->net,
106 "Failed to write(0x%x) reg index 0x%04x: %d\n",
107 cmd, index, err);
108 kfree(buf);
109
110 out:
111 return err;
112 }
113
aqc111_write_cmd_nopm(struct usbnet * dev,u8 cmd,u16 value,u16 index,u16 size,void * data)114 static int aqc111_write_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value,
115 u16 index, u16 size, void *data)
116 {
117 int ret;
118
119 ret = __aqc111_write_cmd(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR |
120 USB_RECIP_DEVICE, value, index, size, data);
121
122 return ret;
123 }
124
aqc111_write_cmd(struct usbnet * dev,u8 cmd,u16 value,u16 index,u16 size,const void * data)125 static int aqc111_write_cmd(struct usbnet *dev, u8 cmd, u16 value,
126 u16 index, u16 size, const void *data)
127 {
128 int ret;
129
130 if (usb_autopm_get_interface(dev->intf) < 0)
131 return -ENODEV;
132
133 ret = __aqc111_write_cmd(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR |
134 USB_RECIP_DEVICE, value, index, size, data);
135
136 usb_autopm_put_interface(dev->intf);
137
138 return ret;
139 }
140
aqc111_write16_cmd_nopm(struct usbnet * dev,u8 cmd,u16 value,u16 index,u16 * data)141 static int aqc111_write16_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value,
142 u16 index, u16 *data)
143 {
144 u16 tmp = *data;
145
146 cpu_to_le16s(&tmp);
147
148 return aqc111_write_cmd_nopm(dev, cmd, value, index, sizeof(tmp), &tmp);
149 }
150
aqc111_write16_cmd(struct usbnet * dev,u8 cmd,u16 value,u16 index,u16 * data)151 static int aqc111_write16_cmd(struct usbnet *dev, u8 cmd, u16 value,
152 u16 index, u16 *data)
153 {
154 u16 tmp = *data;
155
156 cpu_to_le16s(&tmp);
157
158 return aqc111_write_cmd(dev, cmd, value, index, sizeof(tmp), &tmp);
159 }
160
aqc111_write32_cmd_nopm(struct usbnet * dev,u8 cmd,u16 value,u16 index,u32 * data)161 static int aqc111_write32_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value,
162 u16 index, u32 *data)
163 {
164 u32 tmp = *data;
165
166 cpu_to_le32s(&tmp);
167
168 return aqc111_write_cmd_nopm(dev, cmd, value, index, sizeof(tmp), &tmp);
169 }
170
aqc111_write32_cmd(struct usbnet * dev,u8 cmd,u16 value,u16 index,u32 * data)171 static int aqc111_write32_cmd(struct usbnet *dev, u8 cmd, u16 value,
172 u16 index, u32 *data)
173 {
174 u32 tmp = *data;
175
176 cpu_to_le32s(&tmp);
177
178 return aqc111_write_cmd(dev, cmd, value, index, sizeof(tmp), &tmp);
179 }
180
aqc111_write_cmd_async(struct usbnet * dev,u8 cmd,u16 value,u16 index,u16 size,void * data)181 static int aqc111_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value,
182 u16 index, u16 size, void *data)
183 {
184 return usbnet_write_cmd_async(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR |
185 USB_RECIP_DEVICE, value, index, data,
186 size);
187 }
188
aqc111_write16_cmd_async(struct usbnet * dev,u8 cmd,u16 value,u16 index,u16 * data)189 static int aqc111_write16_cmd_async(struct usbnet *dev, u8 cmd, u16 value,
190 u16 index, u16 *data)
191 {
192 u16 tmp = *data;
193
194 cpu_to_le16s(&tmp);
195
196 return aqc111_write_cmd_async(dev, cmd, value, index,
197 sizeof(tmp), &tmp);
198 }
199
aqc111_get_drvinfo(struct net_device * net,struct ethtool_drvinfo * info)200 static void aqc111_get_drvinfo(struct net_device *net,
201 struct ethtool_drvinfo *info)
202 {
203 struct usbnet *dev = netdev_priv(net);
204 struct aqc111_data *aqc111_data = dev->driver_priv;
205
206 /* Inherit standard device info */
207 usbnet_get_drvinfo(net, info);
208 snprintf(info->fw_version, sizeof(info->fw_version), "%u.%u.%u",
209 aqc111_data->fw_ver.major,
210 aqc111_data->fw_ver.minor,
211 aqc111_data->fw_ver.rev);
212 }
213
aqc111_get_wol(struct net_device * net,struct ethtool_wolinfo * wolinfo)214 static void aqc111_get_wol(struct net_device *net,
215 struct ethtool_wolinfo *wolinfo)
216 {
217 struct usbnet *dev = netdev_priv(net);
218 struct aqc111_data *aqc111_data = dev->driver_priv;
219
220 wolinfo->supported = WAKE_MAGIC;
221 wolinfo->wolopts = 0;
222
223 if (aqc111_data->wol_flags & AQ_WOL_FLAG_MP)
224 wolinfo->wolopts |= WAKE_MAGIC;
225 }
226
aqc111_set_wol(struct net_device * net,struct ethtool_wolinfo * wolinfo)227 static int aqc111_set_wol(struct net_device *net,
228 struct ethtool_wolinfo *wolinfo)
229 {
230 struct usbnet *dev = netdev_priv(net);
231 struct aqc111_data *aqc111_data = dev->driver_priv;
232
233 if (wolinfo->wolopts & ~WAKE_MAGIC)
234 return -EINVAL;
235
236 aqc111_data->wol_flags = 0;
237 if (wolinfo->wolopts & WAKE_MAGIC)
238 aqc111_data->wol_flags |= AQ_WOL_FLAG_MP;
239
240 return 0;
241 }
242
aqc111_speed_to_link_mode(u32 speed,struct ethtool_link_ksettings * elk)243 static void aqc111_speed_to_link_mode(u32 speed,
244 struct ethtool_link_ksettings *elk)
245 {
246 switch (speed) {
247 case SPEED_5000:
248 ethtool_link_ksettings_add_link_mode(elk, advertising,
249 5000baseT_Full);
250 break;
251 case SPEED_2500:
252 ethtool_link_ksettings_add_link_mode(elk, advertising,
253 2500baseT_Full);
254 break;
255 case SPEED_1000:
256 ethtool_link_ksettings_add_link_mode(elk, advertising,
257 1000baseT_Full);
258 break;
259 case SPEED_100:
260 ethtool_link_ksettings_add_link_mode(elk, advertising,
261 100baseT_Full);
262 break;
263 }
264 }
265
aqc111_get_link_ksettings(struct net_device * net,struct ethtool_link_ksettings * elk)266 static int aqc111_get_link_ksettings(struct net_device *net,
267 struct ethtool_link_ksettings *elk)
268 {
269 struct usbnet *dev = netdev_priv(net);
270 struct aqc111_data *aqc111_data = dev->driver_priv;
271 enum usb_device_speed usb_speed = dev->udev->speed;
272 u32 speed = SPEED_UNKNOWN;
273
274 ethtool_link_ksettings_zero_link_mode(elk, supported);
275 ethtool_link_ksettings_add_link_mode(elk, supported,
276 100baseT_Full);
277 ethtool_link_ksettings_add_link_mode(elk, supported,
278 1000baseT_Full);
279 if (usb_speed == USB_SPEED_SUPER) {
280 ethtool_link_ksettings_add_link_mode(elk, supported,
281 2500baseT_Full);
282 ethtool_link_ksettings_add_link_mode(elk, supported,
283 5000baseT_Full);
284 }
285 ethtool_link_ksettings_add_link_mode(elk, supported, TP);
286 ethtool_link_ksettings_add_link_mode(elk, supported, Autoneg);
287
288 elk->base.port = PORT_TP;
289 elk->base.transceiver = XCVR_INTERNAL;
290
291 elk->base.mdio_support = 0x00; /*Not supported*/
292
293 if (aqc111_data->autoneg)
294 linkmode_copy(elk->link_modes.advertising,
295 elk->link_modes.supported);
296 else
297 aqc111_speed_to_link_mode(aqc111_data->advertised_speed, elk);
298
299 elk->base.autoneg = aqc111_data->autoneg;
300
301 switch (aqc111_data->link_speed) {
302 case AQ_INT_SPEED_5G:
303 speed = SPEED_5000;
304 break;
305 case AQ_INT_SPEED_2_5G:
306 speed = SPEED_2500;
307 break;
308 case AQ_INT_SPEED_1G:
309 speed = SPEED_1000;
310 break;
311 case AQ_INT_SPEED_100M:
312 speed = SPEED_100;
313 break;
314 }
315 elk->base.duplex = DUPLEX_FULL;
316 elk->base.speed = speed;
317
318 return 0;
319 }
320
aqc111_set_phy_speed(struct usbnet * dev,u8 autoneg,u16 speed)321 static void aqc111_set_phy_speed(struct usbnet *dev, u8 autoneg, u16 speed)
322 {
323 struct aqc111_data *aqc111_data = dev->driver_priv;
324
325 aqc111_data->phy_cfg &= ~AQ_ADV_MASK;
326 aqc111_data->phy_cfg |= AQ_PAUSE;
327 aqc111_data->phy_cfg |= AQ_ASYM_PAUSE;
328 aqc111_data->phy_cfg |= AQ_DOWNSHIFT;
329 aqc111_data->phy_cfg &= ~AQ_DSH_RETRIES_MASK;
330 aqc111_data->phy_cfg |= (3 << AQ_DSH_RETRIES_SHIFT) &
331 AQ_DSH_RETRIES_MASK;
332
333 if (autoneg == AUTONEG_ENABLE) {
334 switch (speed) {
335 case SPEED_5000:
336 aqc111_data->phy_cfg |= AQ_ADV_5G;
337 fallthrough;
338 case SPEED_2500:
339 aqc111_data->phy_cfg |= AQ_ADV_2G5;
340 fallthrough;
341 case SPEED_1000:
342 aqc111_data->phy_cfg |= AQ_ADV_1G;
343 fallthrough;
344 case SPEED_100:
345 aqc111_data->phy_cfg |= AQ_ADV_100M;
346 /* fall-through */
347 }
348 } else {
349 switch (speed) {
350 case SPEED_5000:
351 aqc111_data->phy_cfg |= AQ_ADV_5G;
352 break;
353 case SPEED_2500:
354 aqc111_data->phy_cfg |= AQ_ADV_2G5;
355 break;
356 case SPEED_1000:
357 aqc111_data->phy_cfg |= AQ_ADV_1G;
358 break;
359 case SPEED_100:
360 aqc111_data->phy_cfg |= AQ_ADV_100M;
361 break;
362 }
363 }
364
365 aqc111_write32_cmd(dev, AQ_PHY_OPS, 0, 0, &aqc111_data->phy_cfg);
366 }
367
aqc111_set_link_ksettings(struct net_device * net,const struct ethtool_link_ksettings * elk)368 static int aqc111_set_link_ksettings(struct net_device *net,
369 const struct ethtool_link_ksettings *elk)
370 {
371 struct usbnet *dev = netdev_priv(net);
372 struct aqc111_data *aqc111_data = dev->driver_priv;
373 enum usb_device_speed usb_speed = dev->udev->speed;
374 u8 autoneg = elk->base.autoneg;
375 u32 speed = elk->base.speed;
376
377 if (autoneg == AUTONEG_ENABLE) {
378 if (aqc111_data->autoneg != AUTONEG_ENABLE) {
379 aqc111_data->autoneg = AUTONEG_ENABLE;
380 aqc111_data->advertised_speed =
381 (usb_speed == USB_SPEED_SUPER) ?
382 SPEED_5000 : SPEED_1000;
383 aqc111_set_phy_speed(dev, aqc111_data->autoneg,
384 aqc111_data->advertised_speed);
385 }
386 } else {
387 if (speed != SPEED_100 &&
388 speed != SPEED_1000 &&
389 speed != SPEED_2500 &&
390 speed != SPEED_5000 &&
391 speed != SPEED_UNKNOWN)
392 return -EINVAL;
393
394 if (elk->base.duplex != DUPLEX_FULL)
395 return -EINVAL;
396
397 if (usb_speed != USB_SPEED_SUPER && speed > SPEED_1000)
398 return -EINVAL;
399
400 aqc111_data->autoneg = AUTONEG_DISABLE;
401 if (speed != SPEED_UNKNOWN)
402 aqc111_data->advertised_speed = speed;
403
404 aqc111_set_phy_speed(dev, aqc111_data->autoneg,
405 aqc111_data->advertised_speed);
406 }
407
408 return 0;
409 }
410
411 static const struct ethtool_ops aqc111_ethtool_ops = {
412 .get_drvinfo = aqc111_get_drvinfo,
413 .get_wol = aqc111_get_wol,
414 .set_wol = aqc111_set_wol,
415 .get_msglevel = usbnet_get_msglevel,
416 .set_msglevel = usbnet_set_msglevel,
417 .get_link = ethtool_op_get_link,
418 .get_link_ksettings = aqc111_get_link_ksettings,
419 .set_link_ksettings = aqc111_set_link_ksettings
420 };
421
aqc111_change_mtu(struct net_device * net,int new_mtu)422 static int aqc111_change_mtu(struct net_device *net, int new_mtu)
423 {
424 struct usbnet *dev = netdev_priv(net);
425 u16 reg16 = 0;
426 u8 buf[5];
427
428 WRITE_ONCE(net->mtu, new_mtu);
429 dev->hard_mtu = net->mtu + net->hard_header_len;
430
431 aqc111_read16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
432 2, ®16);
433 if (net->mtu > 1500)
434 reg16 |= SFR_MEDIUM_JUMBO_EN;
435 else
436 reg16 &= ~SFR_MEDIUM_JUMBO_EN;
437
438 aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
439 2, ®16);
440
441 if (dev->net->mtu > 12500) {
442 memcpy(buf, &AQC111_BULKIN_SIZE[2], 5);
443 /* RX bulk configuration */
444 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_RX_BULKIN_QCTRL,
445 5, 5, buf);
446 }
447
448 /* Set high low water level */
449 if (dev->net->mtu <= 4500)
450 reg16 = 0x0810;
451 else if (dev->net->mtu <= 9500)
452 reg16 = 0x1020;
453 else if (dev->net->mtu <= 12500)
454 reg16 = 0x1420;
455 else
456 reg16 = 0x1A20;
457
458 aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_PAUSE_WATERLVL_LOW,
459 2, ®16);
460
461 return 0;
462 }
463
aqc111_set_mac_addr(struct net_device * net,void * p)464 static int aqc111_set_mac_addr(struct net_device *net, void *p)
465 {
466 struct usbnet *dev = netdev_priv(net);
467 int ret = 0;
468
469 ret = eth_mac_addr(net, p);
470 if (ret < 0)
471 return ret;
472
473 /* Set the MAC address */
474 return aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_NODE_ID, ETH_ALEN,
475 ETH_ALEN, net->dev_addr);
476 }
477
aqc111_vlan_rx_kill_vid(struct net_device * net,__be16 proto,u16 vid)478 static int aqc111_vlan_rx_kill_vid(struct net_device *net,
479 __be16 proto, u16 vid)
480 {
481 struct usbnet *dev = netdev_priv(net);
482 u8 vlan_ctrl = 0;
483 u16 reg16 = 0;
484 u8 reg8 = 0;
485
486 aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL, 1, 1, ®8);
487 vlan_ctrl = reg8;
488
489 /* Address */
490 reg8 = (vid / 16);
491 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_ADDRESS, 1, 1, ®8);
492 /* Data */
493 reg8 = vlan_ctrl | SFR_VLAN_CONTROL_RD;
494 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL, 1, 1, ®8);
495 aqc111_read16_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_DATA0, 2, ®16);
496 reg16 &= ~(1 << (vid % 16));
497 aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_DATA0, 2, ®16);
498 reg8 = vlan_ctrl | SFR_VLAN_CONTROL_WE;
499 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL, 1, 1, ®8);
500
501 return 0;
502 }
503
aqc111_vlan_rx_add_vid(struct net_device * net,__be16 proto,u16 vid)504 static int aqc111_vlan_rx_add_vid(struct net_device *net, __be16 proto, u16 vid)
505 {
506 struct usbnet *dev = netdev_priv(net);
507 u8 vlan_ctrl = 0;
508 u16 reg16 = 0;
509 u8 reg8 = 0;
510
511 aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL, 1, 1, ®8);
512 vlan_ctrl = reg8;
513
514 /* Address */
515 reg8 = (vid / 16);
516 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_ADDRESS, 1, 1, ®8);
517 /* Data */
518 reg8 = vlan_ctrl | SFR_VLAN_CONTROL_RD;
519 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL, 1, 1, ®8);
520 aqc111_read16_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_DATA0, 2, ®16);
521 reg16 |= (1 << (vid % 16));
522 aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_DATA0, 2, ®16);
523 reg8 = vlan_ctrl | SFR_VLAN_CONTROL_WE;
524 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL, 1, 1, ®8);
525
526 return 0;
527 }
528
aqc111_set_rx_mode(struct net_device * net)529 static void aqc111_set_rx_mode(struct net_device *net)
530 {
531 struct usbnet *dev = netdev_priv(net);
532 struct aqc111_data *aqc111_data = dev->driver_priv;
533 int mc_count = 0;
534
535 mc_count = netdev_mc_count(net);
536
537 aqc111_data->rxctl &= ~(SFR_RX_CTL_PRO | SFR_RX_CTL_AMALL |
538 SFR_RX_CTL_AM);
539
540 if (net->flags & IFF_PROMISC) {
541 aqc111_data->rxctl |= SFR_RX_CTL_PRO;
542 } else if ((net->flags & IFF_ALLMULTI) || mc_count > AQ_MAX_MCAST) {
543 aqc111_data->rxctl |= SFR_RX_CTL_AMALL;
544 } else if (!netdev_mc_empty(net)) {
545 u8 m_filter[AQ_MCAST_FILTER_SIZE] = { 0 };
546 struct netdev_hw_addr *ha = NULL;
547 u32 crc_bits = 0;
548
549 netdev_for_each_mc_addr(ha, net) {
550 crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
551 m_filter[crc_bits >> 3] |= BIT(crc_bits & 7);
552 }
553
554 aqc111_write_cmd_async(dev, AQ_ACCESS_MAC,
555 SFR_MULTI_FILTER_ARRY,
556 AQ_MCAST_FILTER_SIZE,
557 AQ_MCAST_FILTER_SIZE, m_filter);
558
559 aqc111_data->rxctl |= SFR_RX_CTL_AM;
560 }
561
562 aqc111_write16_cmd_async(dev, AQ_ACCESS_MAC, SFR_RX_CTL,
563 2, &aqc111_data->rxctl);
564 }
565
aqc111_set_features(struct net_device * net,netdev_features_t features)566 static int aqc111_set_features(struct net_device *net,
567 netdev_features_t features)
568 {
569 struct usbnet *dev = netdev_priv(net);
570 struct aqc111_data *aqc111_data = dev->driver_priv;
571 netdev_features_t changed = net->features ^ features;
572 u16 reg16 = 0;
573 u8 reg8 = 0;
574
575 if (changed & NETIF_F_IP_CSUM) {
576 aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_TXCOE_CTL, 1, 1, ®8);
577 reg8 ^= SFR_TXCOE_TCP | SFR_TXCOE_UDP;
578 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_TXCOE_CTL,
579 1, 1, ®8);
580 }
581
582 if (changed & NETIF_F_IPV6_CSUM) {
583 aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_TXCOE_CTL, 1, 1, ®8);
584 reg8 ^= SFR_TXCOE_TCPV6 | SFR_TXCOE_UDPV6;
585 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_TXCOE_CTL,
586 1, 1, ®8);
587 }
588
589 if (changed & NETIF_F_RXCSUM) {
590 aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_RXCOE_CTL, 1, 1, ®8);
591 if (features & NETIF_F_RXCSUM) {
592 aqc111_data->rx_checksum = 1;
593 reg8 &= ~(SFR_RXCOE_IP | SFR_RXCOE_TCP | SFR_RXCOE_UDP |
594 SFR_RXCOE_TCPV6 | SFR_RXCOE_UDPV6);
595 } else {
596 aqc111_data->rx_checksum = 0;
597 reg8 |= SFR_RXCOE_IP | SFR_RXCOE_TCP | SFR_RXCOE_UDP |
598 SFR_RXCOE_TCPV6 | SFR_RXCOE_UDPV6;
599 }
600
601 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_RXCOE_CTL,
602 1, 1, ®8);
603 }
604 if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
605 if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
606 u16 i = 0;
607
608 for (i = 0; i < 256; i++) {
609 /* Address */
610 reg8 = i;
611 aqc111_write_cmd(dev, AQ_ACCESS_MAC,
612 SFR_VLAN_ID_ADDRESS,
613 1, 1, ®8);
614 /* Data */
615 aqc111_write16_cmd(dev, AQ_ACCESS_MAC,
616 SFR_VLAN_ID_DATA0,
617 2, ®16);
618 reg8 = SFR_VLAN_CONTROL_WE;
619 aqc111_write_cmd(dev, AQ_ACCESS_MAC,
620 SFR_VLAN_ID_CONTROL,
621 1, 1, ®8);
622 }
623 aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL,
624 1, 1, ®8);
625 reg8 |= SFR_VLAN_CONTROL_VFE;
626 aqc111_write_cmd(dev, AQ_ACCESS_MAC,
627 SFR_VLAN_ID_CONTROL, 1, 1, ®8);
628 } else {
629 aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL,
630 1, 1, ®8);
631 reg8 &= ~SFR_VLAN_CONTROL_VFE;
632 aqc111_write_cmd(dev, AQ_ACCESS_MAC,
633 SFR_VLAN_ID_CONTROL, 1, 1, ®8);
634 }
635 }
636
637 return 0;
638 }
639
640 static const struct net_device_ops aqc111_netdev_ops = {
641 .ndo_open = usbnet_open,
642 .ndo_stop = usbnet_stop,
643 .ndo_start_xmit = usbnet_start_xmit,
644 .ndo_tx_timeout = usbnet_tx_timeout,
645 .ndo_get_stats64 = dev_get_tstats64,
646 .ndo_change_mtu = aqc111_change_mtu,
647 .ndo_set_mac_address = aqc111_set_mac_addr,
648 .ndo_validate_addr = eth_validate_addr,
649 .ndo_vlan_rx_add_vid = aqc111_vlan_rx_add_vid,
650 .ndo_vlan_rx_kill_vid = aqc111_vlan_rx_kill_vid,
651 .ndo_set_rx_mode = aqc111_set_rx_mode,
652 .ndo_set_features = aqc111_set_features,
653 };
654
aqc111_read_perm_mac(struct usbnet * dev)655 static int aqc111_read_perm_mac(struct usbnet *dev)
656 {
657 u8 buf[ETH_ALEN];
658 int ret;
659
660 ret = aqc111_read_cmd(dev, AQ_FLASH_PARAMETERS, 0, 0, ETH_ALEN, buf);
661 if (ret < 0)
662 goto out;
663
664 ether_addr_copy(dev->net->perm_addr, buf);
665
666 return 0;
667 out:
668 return ret;
669 }
670
aqc111_read_fw_version(struct usbnet * dev,struct aqc111_data * aqc111_data)671 static void aqc111_read_fw_version(struct usbnet *dev,
672 struct aqc111_data *aqc111_data)
673 {
674 aqc111_read_cmd(dev, AQ_ACCESS_MAC, AQ_FW_VER_MAJOR,
675 1, 1, &aqc111_data->fw_ver.major);
676 aqc111_read_cmd(dev, AQ_ACCESS_MAC, AQ_FW_VER_MINOR,
677 1, 1, &aqc111_data->fw_ver.minor);
678 aqc111_read_cmd(dev, AQ_ACCESS_MAC, AQ_FW_VER_REV,
679 1, 1, &aqc111_data->fw_ver.rev);
680
681 if (aqc111_data->fw_ver.major & 0x80)
682 aqc111_data->fw_ver.major &= ~0x80;
683 }
684
aqc111_bind(struct usbnet * dev,struct usb_interface * intf)685 static int aqc111_bind(struct usbnet *dev, struct usb_interface *intf)
686 {
687 struct usb_device *udev = interface_to_usbdev(intf);
688 enum usb_device_speed usb_speed = udev->speed;
689 struct aqc111_data *aqc111_data;
690 int ret;
691
692 /* Check if vendor configuration */
693 if (udev->actconfig->desc.bConfigurationValue != 1) {
694 usb_driver_set_configuration(udev, 1);
695 return -ENODEV;
696 }
697
698 usb_reset_configuration(dev->udev);
699
700 ret = usbnet_get_endpoints(dev, intf);
701 if (ret < 0) {
702 netdev_dbg(dev->net, "usbnet_get_endpoints failed");
703 return ret;
704 }
705
706 aqc111_data = kzalloc_obj(*aqc111_data);
707 if (!aqc111_data)
708 return -ENOMEM;
709
710 /* store aqc111_data pointer in device data field */
711 dev->driver_priv = aqc111_data;
712
713 /* Init the MAC address */
714 ret = aqc111_read_perm_mac(dev);
715 if (ret)
716 goto out;
717
718 eth_hw_addr_set(dev->net, dev->net->perm_addr);
719
720 /* Set Rx urb size */
721 dev->rx_urb_size = URB_SIZE;
722
723 /* Set TX needed headroom & tailroom */
724 dev->net->needed_headroom += sizeof(u64);
725 dev->net->needed_tailroom += sizeof(u64);
726
727 dev->net->max_mtu = 16334;
728
729 dev->net->netdev_ops = &aqc111_netdev_ops;
730 dev->net->ethtool_ops = &aqc111_ethtool_ops;
731
732 if (usb_device_no_sg_constraint(dev->udev))
733 dev->can_dma_sg = 1;
734
735 dev->net->hw_features |= AQ_SUPPORT_HW_FEATURE;
736 dev->net->features |= AQ_SUPPORT_FEATURE;
737 dev->net->vlan_features |= AQ_SUPPORT_VLAN_FEATURE;
738
739 netif_set_tso_max_size(dev->net, 65535);
740
741 aqc111_read_fw_version(dev, aqc111_data);
742 aqc111_data->autoneg = AUTONEG_ENABLE;
743 aqc111_data->advertised_speed = (usb_speed == USB_SPEED_SUPER) ?
744 SPEED_5000 : SPEED_1000;
745
746 return 0;
747
748 out:
749 kfree(aqc111_data);
750 return ret;
751 }
752
aqc111_unbind(struct usbnet * dev,struct usb_interface * intf)753 static void aqc111_unbind(struct usbnet *dev, struct usb_interface *intf)
754 {
755 struct aqc111_data *aqc111_data = dev->driver_priv;
756 u16 reg16;
757
758 /* Force bz */
759 reg16 = SFR_PHYPWR_RSTCTL_BZ;
760 aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_PHYPWR_RSTCTL,
761 2, ®16);
762 reg16 = 0;
763 aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_PHYPWR_RSTCTL,
764 2, ®16);
765
766 /* Power down ethernet PHY */
767 aqc111_data->phy_cfg &= ~AQ_ADV_MASK;
768 aqc111_data->phy_cfg |= AQ_LOW_POWER;
769 aqc111_data->phy_cfg &= ~AQ_PHY_POWER_EN;
770 aqc111_write32_cmd_nopm(dev, AQ_PHY_OPS, 0, 0,
771 &aqc111_data->phy_cfg);
772
773 kfree(aqc111_data);
774 }
775
aqc111_status(struct usbnet * dev,struct urb * urb)776 static void aqc111_status(struct usbnet *dev, struct urb *urb)
777 {
778 struct aqc111_data *aqc111_data = dev->driver_priv;
779 u64 *event_data = NULL;
780 int link = 0;
781
782 if (urb->actual_length < sizeof(*event_data))
783 return;
784
785 event_data = urb->transfer_buffer;
786 le64_to_cpus(event_data);
787
788 if (*event_data & AQ_LS_MASK)
789 link = 1;
790 else
791 link = 0;
792
793 aqc111_data->link_speed = (*event_data & AQ_SPEED_MASK) >>
794 AQ_SPEED_SHIFT;
795 aqc111_data->link = link;
796
797 if (netif_carrier_ok(dev->net) != link)
798 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
799 }
800
aqc111_configure_rx(struct usbnet * dev,struct aqc111_data * aqc111_data)801 static void aqc111_configure_rx(struct usbnet *dev,
802 struct aqc111_data *aqc111_data)
803 {
804 enum usb_device_speed usb_speed = dev->udev->speed;
805 u16 link_speed = 0, usb_host = 0;
806 u8 buf[5] = { 0 };
807 u8 queue_num = 0;
808 u16 reg16 = 0;
809 u8 reg8 = 0;
810
811 buf[0] = 0x00;
812 buf[1] = 0xF8;
813 buf[2] = 0x07;
814 switch (aqc111_data->link_speed) {
815 case AQ_INT_SPEED_5G:
816 link_speed = 5000;
817 reg8 = 0x05;
818 reg16 = 0x001F;
819 break;
820 case AQ_INT_SPEED_2_5G:
821 link_speed = 2500;
822 reg16 = 0x003F;
823 break;
824 case AQ_INT_SPEED_1G:
825 link_speed = 1000;
826 reg16 = 0x009F;
827 break;
828 case AQ_INT_SPEED_100M:
829 link_speed = 100;
830 queue_num = 1;
831 reg16 = 0x063F;
832 buf[1] = 0xFB;
833 buf[2] = 0x4;
834 break;
835 }
836
837 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_INTER_PACKET_GAP_0,
838 1, 1, ®8);
839
840 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_TX_PAUSE_RESEND_T, 3, 3, buf);
841
842 switch (usb_speed) {
843 case USB_SPEED_SUPER:
844 usb_host = 3;
845 break;
846 case USB_SPEED_HIGH:
847 usb_host = 2;
848 break;
849 case USB_SPEED_FULL:
850 case USB_SPEED_LOW:
851 usb_host = 1;
852 queue_num = 0;
853 break;
854 default:
855 usb_host = 0;
856 break;
857 }
858
859 if (dev->net->mtu > 12500 && dev->net->mtu <= 16334)
860 queue_num = 2; /* For Jumbo packet 16KB */
861
862 memcpy(buf, &AQC111_BULKIN_SIZE[queue_num], 5);
863 /* RX bulk configuration */
864 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_RX_BULKIN_QCTRL, 5, 5, buf);
865
866 /* Set high low water level */
867 if (dev->net->mtu <= 4500)
868 reg16 = 0x0810;
869 else if (dev->net->mtu <= 9500)
870 reg16 = 0x1020;
871 else if (dev->net->mtu <= 12500)
872 reg16 = 0x1420;
873 else if (dev->net->mtu <= 16334)
874 reg16 = 0x1A20;
875
876 aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_PAUSE_WATERLVL_LOW,
877 2, ®16);
878 netdev_info(dev->net, "Link Speed %d, USB %d", link_speed, usb_host);
879 }
880
aqc111_configure_csum_offload(struct usbnet * dev)881 static void aqc111_configure_csum_offload(struct usbnet *dev)
882 {
883 u8 reg8 = 0;
884
885 if (dev->net->features & NETIF_F_RXCSUM) {
886 reg8 |= SFR_RXCOE_IP | SFR_RXCOE_TCP | SFR_RXCOE_UDP |
887 SFR_RXCOE_TCPV6 | SFR_RXCOE_UDPV6;
888 }
889 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_RXCOE_CTL, 1, 1, ®8);
890
891 reg8 = 0;
892 if (dev->net->features & NETIF_F_IP_CSUM)
893 reg8 |= SFR_TXCOE_IP | SFR_TXCOE_TCP | SFR_TXCOE_UDP;
894
895 if (dev->net->features & NETIF_F_IPV6_CSUM)
896 reg8 |= SFR_TXCOE_TCPV6 | SFR_TXCOE_UDPV6;
897
898 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_TXCOE_CTL, 1, 1, ®8);
899 }
900
aqc111_link_reset(struct usbnet * dev)901 static int aqc111_link_reset(struct usbnet *dev)
902 {
903 struct aqc111_data *aqc111_data = dev->driver_priv;
904 u16 reg16 = 0;
905 u8 reg8 = 0;
906
907 if (aqc111_data->link == 1) { /* Link up */
908 aqc111_configure_rx(dev, aqc111_data);
909
910 /* Vlan Tag Filter */
911 reg8 = SFR_VLAN_CONTROL_VSO;
912 if (dev->net->features & NETIF_F_HW_VLAN_CTAG_FILTER)
913 reg8 |= SFR_VLAN_CONTROL_VFE;
914
915 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL,
916 1, 1, ®8);
917
918 reg8 = 0x0;
919 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BMRX_DMA_CONTROL,
920 1, 1, ®8);
921
922 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BMTX_DMA_CONTROL,
923 1, 1, ®8);
924
925 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_ARC_CTRL, 1, 1, ®8);
926
927 reg16 = SFR_RX_CTL_IPE | SFR_RX_CTL_AB;
928 aqc111_data->rxctl = reg16;
929 aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_RX_CTL, 2, ®16);
930
931 reg8 = SFR_RX_PATH_READY;
932 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_ETH_MAC_PATH,
933 1, 1, ®8);
934
935 reg8 = SFR_BULK_OUT_EFF_EN;
936 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BULK_OUT_CTRL,
937 1, 1, ®8);
938
939 reg16 = 0;
940 aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
941 2, ®16);
942
943 reg16 = SFR_MEDIUM_XGMIIMODE | SFR_MEDIUM_FULL_DUPLEX;
944 aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
945 2, ®16);
946
947 aqc111_configure_csum_offload(dev);
948
949 aqc111_set_rx_mode(dev->net);
950
951 aqc111_read16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
952 2, ®16);
953
954 if (dev->net->mtu > 1500)
955 reg16 |= SFR_MEDIUM_JUMBO_EN;
956
957 reg16 |= SFR_MEDIUM_RECEIVE_EN | SFR_MEDIUM_RXFLOW_CTRLEN |
958 SFR_MEDIUM_TXFLOW_CTRLEN;
959 aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
960 2, ®16);
961
962 aqc111_data->rxctl |= SFR_RX_CTL_START;
963 aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_RX_CTL,
964 2, &aqc111_data->rxctl);
965
966 netif_carrier_on(dev->net);
967 } else {
968 aqc111_read16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
969 2, ®16);
970 reg16 &= ~SFR_MEDIUM_RECEIVE_EN;
971 aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
972 2, ®16);
973
974 aqc111_data->rxctl &= ~SFR_RX_CTL_START;
975 aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_RX_CTL,
976 2, &aqc111_data->rxctl);
977
978 reg8 = SFR_BULK_OUT_FLUSH_EN | SFR_BULK_OUT_EFF_EN;
979 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BULK_OUT_CTRL,
980 1, 1, ®8);
981 reg8 = SFR_BULK_OUT_EFF_EN;
982 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BULK_OUT_CTRL,
983 1, 1, ®8);
984
985 netif_carrier_off(dev->net);
986 }
987 return 0;
988 }
989
aqc111_reset(struct usbnet * dev)990 static int aqc111_reset(struct usbnet *dev)
991 {
992 struct aqc111_data *aqc111_data = dev->driver_priv;
993 u8 reg8 = 0;
994
995 dev->rx_urb_size = URB_SIZE;
996
997 if (usb_device_no_sg_constraint(dev->udev))
998 dev->can_dma_sg = 1;
999
1000 dev->net->hw_features |= AQ_SUPPORT_HW_FEATURE;
1001 dev->net->features |= AQ_SUPPORT_FEATURE;
1002 dev->net->vlan_features |= AQ_SUPPORT_VLAN_FEATURE;
1003
1004 /* Power up ethernet PHY */
1005 aqc111_data->phy_cfg = AQ_PHY_POWER_EN;
1006 aqc111_write32_cmd(dev, AQ_PHY_OPS, 0, 0,
1007 &aqc111_data->phy_cfg);
1008
1009 /* Set the MAC address */
1010 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_NODE_ID, ETH_ALEN,
1011 ETH_ALEN, dev->net->dev_addr);
1012
1013 reg8 = 0xFF;
1014 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BM_INT_MASK, 1, 1, ®8);
1015
1016 reg8 = 0x0;
1017 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_SWP_CTRL, 1, 1, ®8);
1018
1019 aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_MONITOR_MODE, 1, 1, ®8);
1020 reg8 &= ~(SFR_MONITOR_MODE_EPHYRW | SFR_MONITOR_MODE_RWLC |
1021 SFR_MONITOR_MODE_RWMP | SFR_MONITOR_MODE_RWWF |
1022 SFR_MONITOR_MODE_RW_FLAG);
1023 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_MONITOR_MODE, 1, 1, ®8);
1024
1025 netif_carrier_off(dev->net);
1026
1027 /* Phy advertise */
1028 aqc111_set_phy_speed(dev, aqc111_data->autoneg,
1029 aqc111_data->advertised_speed);
1030
1031 return 0;
1032 }
1033
aqc111_stop(struct usbnet * dev)1034 static int aqc111_stop(struct usbnet *dev)
1035 {
1036 struct aqc111_data *aqc111_data = dev->driver_priv;
1037 u16 reg16 = 0;
1038
1039 aqc111_read16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
1040 2, ®16);
1041 reg16 &= ~SFR_MEDIUM_RECEIVE_EN;
1042 aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
1043 2, ®16);
1044 reg16 = 0;
1045 aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_RX_CTL, 2, ®16);
1046
1047 /* Put PHY to low power*/
1048 aqc111_data->phy_cfg |= AQ_LOW_POWER;
1049 aqc111_write32_cmd(dev, AQ_PHY_OPS, 0, 0,
1050 &aqc111_data->phy_cfg);
1051
1052 netif_carrier_off(dev->net);
1053
1054 return 0;
1055 }
1056
aqc111_rx_checksum(struct sk_buff * skb,u64 pkt_desc)1057 static void aqc111_rx_checksum(struct sk_buff *skb, u64 pkt_desc)
1058 {
1059 u32 pkt_type = 0;
1060
1061 skb->ip_summed = CHECKSUM_NONE;
1062 /* checksum error bit is set */
1063 if (pkt_desc & AQ_RX_PD_L4_ERR || pkt_desc & AQ_RX_PD_L3_ERR)
1064 return;
1065
1066 pkt_type = pkt_desc & AQ_RX_PD_L4_TYPE_MASK;
1067 /* It must be a TCP or UDP packet with a valid checksum */
1068 if (pkt_type == AQ_RX_PD_L4_TCP || pkt_type == AQ_RX_PD_L4_UDP)
1069 skb->ip_summed = CHECKSUM_UNNECESSARY;
1070 }
1071
aqc111_rx_fixup(struct usbnet * dev,struct sk_buff * skb)1072 static int aqc111_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
1073 {
1074 struct aqc111_data *aqc111_data = dev->driver_priv;
1075 struct sk_buff *new_skb = NULL;
1076 u32 pkt_total_offset = 0;
1077 u64 *pkt_desc_ptr = NULL;
1078 u32 start_of_descs = 0;
1079 u32 desc_offset = 0; /*RX Header Offset*/
1080 u16 pkt_count = 0;
1081 u64 desc_hdr = 0;
1082 u16 vlan_tag = 0;
1083 u32 skb_len;
1084
1085 if (!skb)
1086 goto err;
1087
1088 skb_len = skb->len;
1089 if (skb_len < sizeof(desc_hdr))
1090 goto err;
1091
1092 /* RX Descriptor Header */
1093 skb_trim(skb, skb_len - sizeof(desc_hdr));
1094 desc_hdr = le64_to_cpup((u64 *)skb_tail_pointer(skb));
1095
1096 /* Check these packets */
1097 desc_offset = (desc_hdr & AQ_RX_DH_DESC_OFFSET_MASK) >>
1098 AQ_RX_DH_DESC_OFFSET_SHIFT;
1099 pkt_count = desc_hdr & AQ_RX_DH_PKT_CNT_MASK;
1100 start_of_descs = skb_len - ((pkt_count + 1) * sizeof(desc_hdr));
1101
1102 /* self check descs position */
1103 if (start_of_descs != desc_offset)
1104 goto err;
1105
1106 /* self check desc_offset from header and make sure that the
1107 * bounds of the metadata array are inside the SKB
1108 */
1109 if (pkt_count * 2 + desc_offset >= skb_len)
1110 goto err;
1111
1112 /* Packets must not overlap the metadata array */
1113 skb_trim(skb, desc_offset);
1114
1115 if (pkt_count == 0)
1116 goto err;
1117
1118 /* Get the first RX packet descriptor */
1119 pkt_desc_ptr = (u64 *)(skb->data + desc_offset);
1120
1121 while (pkt_count--) {
1122 u64 pkt_desc = le64_to_cpup(pkt_desc_ptr);
1123 u32 pkt_len_with_padd = 0;
1124 u32 pkt_len = 0;
1125
1126 pkt_len = (u32)((pkt_desc & AQ_RX_PD_LEN_MASK) >>
1127 AQ_RX_PD_LEN_SHIFT);
1128 pkt_len_with_padd = ((pkt_len + 7) & 0x7FFF8);
1129
1130 pkt_total_offset += pkt_len_with_padd;
1131 if (pkt_total_offset > desc_offset ||
1132 (pkt_count == 0 && pkt_total_offset != desc_offset)) {
1133 goto err;
1134 }
1135
1136 if (pkt_desc & AQ_RX_PD_DROP ||
1137 !(pkt_desc & AQ_RX_PD_RX_OK) ||
1138 pkt_len > (dev->hard_mtu + AQ_RX_HW_PAD)) {
1139 skb_pull(skb, pkt_len_with_padd);
1140 /* Next RX Packet Descriptor */
1141 pkt_desc_ptr++;
1142 continue;
1143 }
1144
1145 new_skb = netdev_alloc_skb_ip_align(dev->net, pkt_len);
1146
1147 if (!new_skb)
1148 goto err;
1149
1150 skb_put(new_skb, pkt_len);
1151 memcpy(new_skb->data, skb->data, pkt_len);
1152 skb_pull(new_skb, AQ_RX_HW_PAD);
1153
1154 if (aqc111_data->rx_checksum)
1155 aqc111_rx_checksum(new_skb, pkt_desc);
1156
1157 if (pkt_desc & AQ_RX_PD_VLAN) {
1158 vlan_tag = pkt_desc >> AQ_RX_PD_VLAN_SHIFT;
1159 __vlan_hwaccel_put_tag(new_skb, htons(ETH_P_8021Q),
1160 vlan_tag & VLAN_VID_MASK);
1161 }
1162
1163 usbnet_skb_return(dev, new_skb);
1164 if (pkt_count == 0)
1165 break;
1166
1167 skb_pull(skb, pkt_len_with_padd);
1168
1169 /* Next RX Packet Header */
1170 pkt_desc_ptr++;
1171
1172 new_skb = NULL;
1173 }
1174
1175 return 1;
1176
1177 err:
1178 return 0;
1179 }
1180
aqc111_tx_fixup(struct usbnet * dev,struct sk_buff * skb,gfp_t flags)1181 static struct sk_buff *aqc111_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
1182 gfp_t flags)
1183 {
1184 int frame_size = dev->maxpacket;
1185 struct sk_buff *new_skb = NULL;
1186 u64 *tx_desc_ptr = NULL;
1187 int padding_size = 0;
1188 int headroom = 0;
1189 int tailroom = 0;
1190 u64 tx_desc = 0;
1191 u16 tci = 0;
1192
1193 /*Length of actual data*/
1194 tx_desc |= skb->len & AQ_TX_DESC_LEN_MASK;
1195
1196 /* TSO MSS */
1197 tx_desc |= ((u64)(skb_shinfo(skb)->gso_size & AQ_TX_DESC_MSS_MASK)) <<
1198 AQ_TX_DESC_MSS_SHIFT;
1199
1200 headroom = (skb->len + sizeof(tx_desc)) % 8;
1201 if (headroom != 0)
1202 padding_size = 8 - headroom;
1203
1204 if (((skb->len + sizeof(tx_desc) + padding_size) % frame_size) == 0) {
1205 padding_size += 8;
1206 tx_desc |= AQ_TX_DESC_DROP_PADD;
1207 }
1208
1209 /* Vlan Tag */
1210 if (vlan_get_tag(skb, &tci) >= 0) {
1211 tx_desc |= AQ_TX_DESC_VLAN;
1212 tx_desc |= ((u64)tci & AQ_TX_DESC_VLAN_MASK) <<
1213 AQ_TX_DESC_VLAN_SHIFT;
1214 }
1215
1216 if (!dev->can_dma_sg && (dev->net->features & NETIF_F_SG) &&
1217 skb_linearize(skb))
1218 return NULL;
1219
1220 headroom = skb_headroom(skb);
1221 tailroom = skb_tailroom(skb);
1222
1223 if (!(headroom >= sizeof(tx_desc) && tailroom >= padding_size)) {
1224 new_skb = skb_copy_expand(skb, sizeof(tx_desc),
1225 padding_size, flags);
1226 dev_kfree_skb_any(skb);
1227 skb = new_skb;
1228 if (!skb)
1229 return NULL;
1230 }
1231 if (padding_size != 0)
1232 skb_put_zero(skb, padding_size);
1233 /* Copy TX header */
1234 tx_desc_ptr = skb_push(skb, sizeof(tx_desc));
1235 *tx_desc_ptr = cpu_to_le64(tx_desc);
1236
1237 usbnet_set_skb_tx_stats(skb, 1, 0);
1238
1239 return skb;
1240 }
1241
1242 static const struct driver_info aqc111_info = {
1243 .description = "Aquantia AQtion USB to 5GbE Controller",
1244 .bind = aqc111_bind,
1245 .unbind = aqc111_unbind,
1246 .status = aqc111_status,
1247 .link_reset = aqc111_link_reset,
1248 .reset = aqc111_reset,
1249 .stop = aqc111_stop,
1250 .flags = FLAG_ETHER | FLAG_FRAMING_AX |
1251 FLAG_AVOID_UNLINK_URBS | FLAG_MULTI_PACKET,
1252 .rx_fixup = aqc111_rx_fixup,
1253 .tx_fixup = aqc111_tx_fixup,
1254 };
1255
1256 #define ASIX111_DESC \
1257 "ASIX USB 3.1 Gen1 to 5G Multi-Gigabit Ethernet Adapter"
1258
1259 static const struct driver_info asix111_info = {
1260 .description = ASIX111_DESC,
1261 .bind = aqc111_bind,
1262 .unbind = aqc111_unbind,
1263 .status = aqc111_status,
1264 .link_reset = aqc111_link_reset,
1265 .reset = aqc111_reset,
1266 .stop = aqc111_stop,
1267 .flags = FLAG_ETHER | FLAG_FRAMING_AX |
1268 FLAG_AVOID_UNLINK_URBS | FLAG_MULTI_PACKET,
1269 .rx_fixup = aqc111_rx_fixup,
1270 .tx_fixup = aqc111_tx_fixup,
1271 };
1272
1273 #undef ASIX111_DESC
1274
1275 #define ASIX112_DESC \
1276 "ASIX USB 3.1 Gen1 to 2.5G Multi-Gigabit Ethernet Adapter"
1277
1278 static const struct driver_info asix112_info = {
1279 .description = ASIX112_DESC,
1280 .bind = aqc111_bind,
1281 .unbind = aqc111_unbind,
1282 .status = aqc111_status,
1283 .link_reset = aqc111_link_reset,
1284 .reset = aqc111_reset,
1285 .stop = aqc111_stop,
1286 .flags = FLAG_ETHER | FLAG_FRAMING_AX |
1287 FLAG_AVOID_UNLINK_URBS | FLAG_MULTI_PACKET,
1288 .rx_fixup = aqc111_rx_fixup,
1289 .tx_fixup = aqc111_tx_fixup,
1290 };
1291
1292 #undef ASIX112_DESC
1293
1294 static const struct driver_info trendnet_info = {
1295 .description = "USB-C 3.1 to 5GBASE-T Ethernet Adapter",
1296 .bind = aqc111_bind,
1297 .unbind = aqc111_unbind,
1298 .status = aqc111_status,
1299 .link_reset = aqc111_link_reset,
1300 .reset = aqc111_reset,
1301 .stop = aqc111_stop,
1302 .flags = FLAG_ETHER | FLAG_FRAMING_AX |
1303 FLAG_AVOID_UNLINK_URBS | FLAG_MULTI_PACKET,
1304 .rx_fixup = aqc111_rx_fixup,
1305 .tx_fixup = aqc111_tx_fixup,
1306 };
1307
1308 static const struct driver_info qnap_info = {
1309 .description = "QNAP QNA-UC5G1T USB to 5GbE Adapter",
1310 .bind = aqc111_bind,
1311 .unbind = aqc111_unbind,
1312 .status = aqc111_status,
1313 .link_reset = aqc111_link_reset,
1314 .reset = aqc111_reset,
1315 .stop = aqc111_stop,
1316 .flags = FLAG_ETHER | FLAG_FRAMING_AX |
1317 FLAG_AVOID_UNLINK_URBS | FLAG_MULTI_PACKET,
1318 .rx_fixup = aqc111_rx_fixup,
1319 .tx_fixup = aqc111_tx_fixup,
1320 };
1321
aqc111_suspend(struct usb_interface * intf,pm_message_t message)1322 static int aqc111_suspend(struct usb_interface *intf, pm_message_t message)
1323 {
1324 struct usbnet *dev = usb_get_intfdata(intf);
1325 struct aqc111_data *aqc111_data = dev->driver_priv;
1326 u16 temp_rx_ctrl = 0x00;
1327 u16 reg16;
1328 u8 reg8;
1329
1330 usbnet_suspend(intf, message);
1331
1332 aqc111_read16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_CTL, 2, ®16);
1333 temp_rx_ctrl = reg16;
1334 /* Stop RX operations*/
1335 reg16 &= ~SFR_RX_CTL_START;
1336 aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_CTL, 2, ®16);
1337 /* Force bz */
1338 aqc111_read16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_PHYPWR_RSTCTL,
1339 2, ®16);
1340 reg16 |= SFR_PHYPWR_RSTCTL_BZ;
1341 aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_PHYPWR_RSTCTL,
1342 2, ®16);
1343
1344 reg8 = SFR_BULK_OUT_EFF_EN;
1345 aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_BULK_OUT_CTRL,
1346 1, 1, ®8);
1347
1348 temp_rx_ctrl &= ~(SFR_RX_CTL_START | SFR_RX_CTL_RF_WAK |
1349 SFR_RX_CTL_AP | SFR_RX_CTL_AM);
1350 aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_CTL,
1351 2, &temp_rx_ctrl);
1352
1353 reg8 = 0x00;
1354 aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_ETH_MAC_PATH,
1355 1, 1, ®8);
1356
1357 if (aqc111_data->wol_flags) {
1358 struct aqc111_wol_cfg wol_cfg;
1359
1360 memset(&wol_cfg, 0, sizeof(struct aqc111_wol_cfg));
1361
1362 aqc111_data->phy_cfg |= AQ_WOL;
1363 ether_addr_copy(wol_cfg.hw_addr, dev->net->dev_addr);
1364 wol_cfg.flags = aqc111_data->wol_flags;
1365
1366 temp_rx_ctrl |= (SFR_RX_CTL_AB | SFR_RX_CTL_START);
1367 aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_CTL,
1368 2, &temp_rx_ctrl);
1369 reg8 = 0x00;
1370 aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_BM_INT_MASK,
1371 1, 1, ®8);
1372 reg8 = SFR_BMRX_DMA_EN;
1373 aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_BMRX_DMA_CONTROL,
1374 1, 1, ®8);
1375 reg8 = SFR_RX_PATH_READY;
1376 aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_ETH_MAC_PATH,
1377 1, 1, ®8);
1378 reg8 = 0x07;
1379 aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_BULKIN_QCTRL,
1380 1, 1, ®8);
1381 reg8 = 0x00;
1382 aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC,
1383 SFR_RX_BULKIN_QTIMR_LOW, 1, 1, ®8);
1384 aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC,
1385 SFR_RX_BULKIN_QTIMR_HIGH, 1, 1, ®8);
1386 reg8 = 0xFF;
1387 aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_BULKIN_QSIZE,
1388 1, 1, ®8);
1389 aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_BULKIN_QIFG,
1390 1, 1, ®8);
1391
1392 aqc111_read16_cmd_nopm(dev, AQ_ACCESS_MAC,
1393 SFR_MEDIUM_STATUS_MODE, 2, ®16);
1394 reg16 |= SFR_MEDIUM_RECEIVE_EN;
1395 aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC,
1396 SFR_MEDIUM_STATUS_MODE, 2, ®16);
1397
1398 aqc111_write_cmd(dev, AQ_WOL_CFG, 0, 0,
1399 WOL_CFG_SIZE, &wol_cfg);
1400 aqc111_write32_cmd(dev, AQ_PHY_OPS, 0, 0,
1401 &aqc111_data->phy_cfg);
1402 } else {
1403 aqc111_data->phy_cfg |= AQ_LOW_POWER;
1404 aqc111_write32_cmd(dev, AQ_PHY_OPS, 0, 0,
1405 &aqc111_data->phy_cfg);
1406
1407 /* Disable RX path */
1408 aqc111_read16_cmd_nopm(dev, AQ_ACCESS_MAC,
1409 SFR_MEDIUM_STATUS_MODE, 2, ®16);
1410 reg16 &= ~SFR_MEDIUM_RECEIVE_EN;
1411 aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC,
1412 SFR_MEDIUM_STATUS_MODE, 2, ®16);
1413 }
1414
1415 return 0;
1416 }
1417
aqc111_resume(struct usb_interface * intf)1418 static int aqc111_resume(struct usb_interface *intf)
1419 {
1420 struct usbnet *dev = usb_get_intfdata(intf);
1421 struct aqc111_data *aqc111_data = dev->driver_priv;
1422 u16 reg16;
1423 u8 reg8;
1424
1425 netif_carrier_off(dev->net);
1426
1427 /* Power up ethernet PHY */
1428 aqc111_data->phy_cfg |= AQ_PHY_POWER_EN;
1429 aqc111_data->phy_cfg &= ~AQ_LOW_POWER;
1430 aqc111_data->phy_cfg &= ~AQ_WOL;
1431
1432 reg8 = 0xFF;
1433 aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_BM_INT_MASK,
1434 1, 1, ®8);
1435 /* Configure RX control register => start operation */
1436 reg16 = aqc111_data->rxctl;
1437 reg16 &= ~SFR_RX_CTL_START;
1438 aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_CTL, 2, ®16);
1439
1440 reg16 |= SFR_RX_CTL_START;
1441 aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_CTL, 2, ®16);
1442
1443 aqc111_set_phy_speed(dev, aqc111_data->autoneg,
1444 aqc111_data->advertised_speed);
1445
1446 aqc111_read16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
1447 2, ®16);
1448 reg16 |= SFR_MEDIUM_RECEIVE_EN;
1449 aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
1450 2, ®16);
1451 reg8 = SFR_RX_PATH_READY;
1452 aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_ETH_MAC_PATH,
1453 1, 1, ®8);
1454 reg8 = 0x0;
1455 aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BMRX_DMA_CONTROL, 1, 1, ®8);
1456
1457 return usbnet_resume(intf);
1458 }
1459
1460 #define AQC111_USB_ETH_DEV(vid, pid, table) \
1461 USB_DEVICE_INTERFACE_CLASS((vid), (pid), USB_CLASS_VENDOR_SPEC), \
1462 .driver_info = (unsigned long)&(table) \
1463 }, \
1464 { \
1465 USB_DEVICE_AND_INTERFACE_INFO((vid), (pid), \
1466 USB_CLASS_COMM, \
1467 USB_CDC_SUBCLASS_ETHERNET, \
1468 USB_CDC_PROTO_NONE), \
1469 .driver_info = (unsigned long)&(table),
1470
1471 static const struct usb_device_id products[] = {
1472 {AQC111_USB_ETH_DEV(0x2eca, 0xc101, aqc111_info)},
1473 {AQC111_USB_ETH_DEV(0x0b95, 0x2790, asix111_info)},
1474 {AQC111_USB_ETH_DEV(0x0b95, 0x2791, asix112_info)},
1475 {AQC111_USB_ETH_DEV(0x20f4, 0xe05a, trendnet_info)},
1476 {AQC111_USB_ETH_DEV(0x1c04, 0x0015, qnap_info)},
1477 { },/* END */
1478 };
1479 MODULE_DEVICE_TABLE(usb, products);
1480
1481 static struct usb_driver aq_driver = {
1482 .name = "aqc111",
1483 .id_table = products,
1484 .probe = usbnet_probe,
1485 .suspend = aqc111_suspend,
1486 .resume = aqc111_resume,
1487 .disconnect = usbnet_disconnect,
1488 };
1489
1490 module_usb_driver(aq_driver);
1491
1492 MODULE_DESCRIPTION("Aquantia AQtion USB to 5/2.5GbE Controllers");
1493 MODULE_LICENSE("GPL");
1494