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