xref: /linux/drivers/net/usb/aqc111.c (revision c3a1ddd5e116540912441606a2cd4b944be1622a)
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 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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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, &reg16);
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, &reg16);
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, &reg16);
460 
461 	return 0;
462 }
463 
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 	ret = aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_NODE_ID,
475 			       ETH_ALEN, ETH_ALEN, net->dev_addr);
476 	if (ret < 0)
477 		return ret;
478 
479 	return 0;
480 }
481 
482 static int aqc111_vlan_rx_kill_vid(struct net_device *net,
483 				   __be16 proto, u16 vid)
484 {
485 	struct usbnet *dev = netdev_priv(net);
486 	u8 vlan_ctrl = 0;
487 	u16 reg16 = 0;
488 	u8 reg8 = 0;
489 
490 	aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL, 1, 1, &reg8);
491 	vlan_ctrl = reg8;
492 
493 	/* Address */
494 	reg8 = (vid / 16);
495 	aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_ADDRESS, 1, 1, &reg8);
496 	/* Data */
497 	reg8 = vlan_ctrl | SFR_VLAN_CONTROL_RD;
498 	aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL, 1, 1, &reg8);
499 	aqc111_read16_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_DATA0, 2, &reg16);
500 	reg16 &= ~(1 << (vid % 16));
501 	aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_DATA0, 2, &reg16);
502 	reg8 = vlan_ctrl | SFR_VLAN_CONTROL_WE;
503 	aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL, 1, 1, &reg8);
504 
505 	return 0;
506 }
507 
508 static int aqc111_vlan_rx_add_vid(struct net_device *net, __be16 proto, u16 vid)
509 {
510 	struct usbnet *dev = netdev_priv(net);
511 	u8 vlan_ctrl = 0;
512 	u16 reg16 = 0;
513 	u8 reg8 = 0;
514 
515 	aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL, 1, 1, &reg8);
516 	vlan_ctrl = reg8;
517 
518 	/* Address */
519 	reg8 = (vid / 16);
520 	aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_ADDRESS, 1, 1, &reg8);
521 	/* Data */
522 	reg8 = vlan_ctrl | SFR_VLAN_CONTROL_RD;
523 	aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL, 1, 1, &reg8);
524 	aqc111_read16_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_DATA0, 2, &reg16);
525 	reg16 |= (1 << (vid % 16));
526 	aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_DATA0, 2, &reg16);
527 	reg8 = vlan_ctrl | SFR_VLAN_CONTROL_WE;
528 	aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL, 1, 1, &reg8);
529 
530 	return 0;
531 }
532 
533 static void aqc111_set_rx_mode(struct net_device *net)
534 {
535 	struct usbnet *dev = netdev_priv(net);
536 	struct aqc111_data *aqc111_data = dev->driver_priv;
537 	int mc_count = 0;
538 
539 	mc_count = netdev_mc_count(net);
540 
541 	aqc111_data->rxctl &= ~(SFR_RX_CTL_PRO | SFR_RX_CTL_AMALL |
542 				SFR_RX_CTL_AM);
543 
544 	if (net->flags & IFF_PROMISC) {
545 		aqc111_data->rxctl |= SFR_RX_CTL_PRO;
546 	} else if ((net->flags & IFF_ALLMULTI) || mc_count > AQ_MAX_MCAST) {
547 		aqc111_data->rxctl |= SFR_RX_CTL_AMALL;
548 	} else if (!netdev_mc_empty(net)) {
549 		u8 m_filter[AQ_MCAST_FILTER_SIZE] = { 0 };
550 		struct netdev_hw_addr *ha = NULL;
551 		u32 crc_bits = 0;
552 
553 		netdev_for_each_mc_addr(ha, net) {
554 			crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
555 			m_filter[crc_bits >> 3] |= BIT(crc_bits & 7);
556 		}
557 
558 		aqc111_write_cmd_async(dev, AQ_ACCESS_MAC,
559 				       SFR_MULTI_FILTER_ARRY,
560 				       AQ_MCAST_FILTER_SIZE,
561 				       AQ_MCAST_FILTER_SIZE, m_filter);
562 
563 		aqc111_data->rxctl |= SFR_RX_CTL_AM;
564 	}
565 
566 	aqc111_write16_cmd_async(dev, AQ_ACCESS_MAC, SFR_RX_CTL,
567 				 2, &aqc111_data->rxctl);
568 }
569 
570 static int aqc111_set_features(struct net_device *net,
571 			       netdev_features_t features)
572 {
573 	struct usbnet *dev = netdev_priv(net);
574 	struct aqc111_data *aqc111_data = dev->driver_priv;
575 	netdev_features_t changed = net->features ^ features;
576 	u16 reg16 = 0;
577 	u8 reg8 = 0;
578 
579 	if (changed & NETIF_F_IP_CSUM) {
580 		aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_TXCOE_CTL, 1, 1, &reg8);
581 		reg8 ^= SFR_TXCOE_TCP | SFR_TXCOE_UDP;
582 		aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_TXCOE_CTL,
583 				 1, 1, &reg8);
584 	}
585 
586 	if (changed & NETIF_F_IPV6_CSUM) {
587 		aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_TXCOE_CTL, 1, 1, &reg8);
588 		reg8 ^= SFR_TXCOE_TCPV6 | SFR_TXCOE_UDPV6;
589 		aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_TXCOE_CTL,
590 				 1, 1, &reg8);
591 	}
592 
593 	if (changed & NETIF_F_RXCSUM) {
594 		aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_RXCOE_CTL, 1, 1, &reg8);
595 		if (features & NETIF_F_RXCSUM) {
596 			aqc111_data->rx_checksum = 1;
597 			reg8 &= ~(SFR_RXCOE_IP | SFR_RXCOE_TCP | SFR_RXCOE_UDP |
598 				  SFR_RXCOE_TCPV6 | SFR_RXCOE_UDPV6);
599 		} else {
600 			aqc111_data->rx_checksum = 0;
601 			reg8 |= SFR_RXCOE_IP | SFR_RXCOE_TCP | SFR_RXCOE_UDP |
602 				SFR_RXCOE_TCPV6 | SFR_RXCOE_UDPV6;
603 		}
604 
605 		aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_RXCOE_CTL,
606 				 1, 1, &reg8);
607 	}
608 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
609 		if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
610 			u16 i = 0;
611 
612 			for (i = 0; i < 256; i++) {
613 				/* Address */
614 				reg8 = i;
615 				aqc111_write_cmd(dev, AQ_ACCESS_MAC,
616 						 SFR_VLAN_ID_ADDRESS,
617 						 1, 1, &reg8);
618 				/* Data */
619 				aqc111_write16_cmd(dev, AQ_ACCESS_MAC,
620 						   SFR_VLAN_ID_DATA0,
621 						   2, &reg16);
622 				reg8 = SFR_VLAN_CONTROL_WE;
623 				aqc111_write_cmd(dev, AQ_ACCESS_MAC,
624 						 SFR_VLAN_ID_CONTROL,
625 						 1, 1, &reg8);
626 			}
627 			aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL,
628 					1, 1, &reg8);
629 			reg8 |= SFR_VLAN_CONTROL_VFE;
630 			aqc111_write_cmd(dev, AQ_ACCESS_MAC,
631 					 SFR_VLAN_ID_CONTROL, 1, 1, &reg8);
632 		} else {
633 			aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL,
634 					1, 1, &reg8);
635 			reg8 &= ~SFR_VLAN_CONTROL_VFE;
636 			aqc111_write_cmd(dev, AQ_ACCESS_MAC,
637 					 SFR_VLAN_ID_CONTROL, 1, 1, &reg8);
638 		}
639 	}
640 
641 	return 0;
642 }
643 
644 static const struct net_device_ops aqc111_netdev_ops = {
645 	.ndo_open		= usbnet_open,
646 	.ndo_stop		= usbnet_stop,
647 	.ndo_start_xmit		= usbnet_start_xmit,
648 	.ndo_tx_timeout		= usbnet_tx_timeout,
649 	.ndo_get_stats64	= dev_get_tstats64,
650 	.ndo_change_mtu		= aqc111_change_mtu,
651 	.ndo_set_mac_address	= aqc111_set_mac_addr,
652 	.ndo_validate_addr	= eth_validate_addr,
653 	.ndo_vlan_rx_add_vid	= aqc111_vlan_rx_add_vid,
654 	.ndo_vlan_rx_kill_vid	= aqc111_vlan_rx_kill_vid,
655 	.ndo_set_rx_mode	= aqc111_set_rx_mode,
656 	.ndo_set_features	= aqc111_set_features,
657 };
658 
659 static int aqc111_read_perm_mac(struct usbnet *dev)
660 {
661 	u8 buf[ETH_ALEN];
662 	int ret;
663 
664 	ret = aqc111_read_cmd(dev, AQ_FLASH_PARAMETERS, 0, 0, ETH_ALEN, buf);
665 	if (ret < 0)
666 		goto out;
667 
668 	ether_addr_copy(dev->net->perm_addr, buf);
669 
670 	return 0;
671 out:
672 	return ret;
673 }
674 
675 static void aqc111_read_fw_version(struct usbnet *dev,
676 				   struct aqc111_data *aqc111_data)
677 {
678 	aqc111_read_cmd(dev, AQ_ACCESS_MAC, AQ_FW_VER_MAJOR,
679 			1, 1, &aqc111_data->fw_ver.major);
680 	aqc111_read_cmd(dev, AQ_ACCESS_MAC, AQ_FW_VER_MINOR,
681 			1, 1, &aqc111_data->fw_ver.minor);
682 	aqc111_read_cmd(dev, AQ_ACCESS_MAC, AQ_FW_VER_REV,
683 			1, 1, &aqc111_data->fw_ver.rev);
684 
685 	if (aqc111_data->fw_ver.major & 0x80)
686 		aqc111_data->fw_ver.major &= ~0x80;
687 }
688 
689 static int aqc111_bind(struct usbnet *dev, struct usb_interface *intf)
690 {
691 	struct usb_device *udev = interface_to_usbdev(intf);
692 	enum usb_device_speed usb_speed = udev->speed;
693 	struct aqc111_data *aqc111_data;
694 	int ret;
695 
696 	/* Check if vendor configuration */
697 	if (udev->actconfig->desc.bConfigurationValue != 1) {
698 		usb_driver_set_configuration(udev, 1);
699 		return -ENODEV;
700 	}
701 
702 	usb_reset_configuration(dev->udev);
703 
704 	ret = usbnet_get_endpoints(dev, intf);
705 	if (ret < 0) {
706 		netdev_dbg(dev->net, "usbnet_get_endpoints failed");
707 		return ret;
708 	}
709 
710 	aqc111_data = kzalloc_obj(*aqc111_data);
711 	if (!aqc111_data)
712 		return -ENOMEM;
713 
714 	/* store aqc111_data pointer in device data field */
715 	dev->driver_priv = aqc111_data;
716 
717 	/* Init the MAC address */
718 	ret = aqc111_read_perm_mac(dev);
719 	if (ret)
720 		goto out;
721 
722 	eth_hw_addr_set(dev->net, dev->net->perm_addr);
723 
724 	/* Set Rx urb size */
725 	dev->rx_urb_size = URB_SIZE;
726 
727 	/* Set TX needed headroom & tailroom */
728 	dev->net->needed_headroom += sizeof(u64);
729 	dev->net->needed_tailroom += sizeof(u64);
730 
731 	dev->net->max_mtu = 16334;
732 
733 	dev->net->netdev_ops = &aqc111_netdev_ops;
734 	dev->net->ethtool_ops = &aqc111_ethtool_ops;
735 
736 	if (usb_device_no_sg_constraint(dev->udev))
737 		dev->can_dma_sg = 1;
738 
739 	dev->net->hw_features |= AQ_SUPPORT_HW_FEATURE;
740 	dev->net->features |= AQ_SUPPORT_FEATURE;
741 	dev->net->vlan_features |= AQ_SUPPORT_VLAN_FEATURE;
742 
743 	netif_set_tso_max_size(dev->net, 65535);
744 
745 	aqc111_read_fw_version(dev, aqc111_data);
746 	aqc111_data->autoneg = AUTONEG_ENABLE;
747 	aqc111_data->advertised_speed = (usb_speed == USB_SPEED_SUPER) ?
748 					 SPEED_5000 : SPEED_1000;
749 
750 	return 0;
751 
752 out:
753 	kfree(aqc111_data);
754 	return ret;
755 }
756 
757 static void aqc111_unbind(struct usbnet *dev, struct usb_interface *intf)
758 {
759 	struct aqc111_data *aqc111_data = dev->driver_priv;
760 	u16 reg16;
761 
762 	/* Force bz */
763 	reg16 = SFR_PHYPWR_RSTCTL_BZ;
764 	aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_PHYPWR_RSTCTL,
765 				2, &reg16);
766 	reg16 = 0;
767 	aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_PHYPWR_RSTCTL,
768 				2, &reg16);
769 
770 	/* Power down ethernet PHY */
771 	aqc111_data->phy_cfg &= ~AQ_ADV_MASK;
772 	aqc111_data->phy_cfg |= AQ_LOW_POWER;
773 	aqc111_data->phy_cfg &= ~AQ_PHY_POWER_EN;
774 	aqc111_write32_cmd_nopm(dev, AQ_PHY_OPS, 0, 0,
775 				&aqc111_data->phy_cfg);
776 
777 	kfree(aqc111_data);
778 }
779 
780 static void aqc111_status(struct usbnet *dev, struct urb *urb)
781 {
782 	struct aqc111_data *aqc111_data = dev->driver_priv;
783 	u64 *event_data = NULL;
784 	int link = 0;
785 
786 	if (urb->actual_length < sizeof(*event_data))
787 		return;
788 
789 	event_data = urb->transfer_buffer;
790 	le64_to_cpus(event_data);
791 
792 	if (*event_data & AQ_LS_MASK)
793 		link = 1;
794 	else
795 		link = 0;
796 
797 	aqc111_data->link_speed = (*event_data & AQ_SPEED_MASK) >>
798 				  AQ_SPEED_SHIFT;
799 	aqc111_data->link = link;
800 
801 	if (netif_carrier_ok(dev->net) != link)
802 		usbnet_defer_kevent(dev, EVENT_LINK_RESET);
803 }
804 
805 static void aqc111_configure_rx(struct usbnet *dev,
806 				struct aqc111_data *aqc111_data)
807 {
808 	enum usb_device_speed usb_speed = dev->udev->speed;
809 	u16 link_speed = 0, usb_host = 0;
810 	u8 buf[5] = { 0 };
811 	u8 queue_num = 0;
812 	u16 reg16 = 0;
813 	u8 reg8 = 0;
814 
815 	buf[0] = 0x00;
816 	buf[1] = 0xF8;
817 	buf[2] = 0x07;
818 	switch (aqc111_data->link_speed) {
819 	case AQ_INT_SPEED_5G:
820 		link_speed = 5000;
821 		reg8 = 0x05;
822 		reg16 = 0x001F;
823 		break;
824 	case AQ_INT_SPEED_2_5G:
825 		link_speed = 2500;
826 		reg16 = 0x003F;
827 		break;
828 	case AQ_INT_SPEED_1G:
829 		link_speed = 1000;
830 		reg16 = 0x009F;
831 		break;
832 	case AQ_INT_SPEED_100M:
833 		link_speed = 100;
834 		queue_num = 1;
835 		reg16 = 0x063F;
836 		buf[1] = 0xFB;
837 		buf[2] = 0x4;
838 		break;
839 	}
840 
841 	aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_INTER_PACKET_GAP_0,
842 			 1, 1, &reg8);
843 
844 	aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_TX_PAUSE_RESEND_T, 3, 3, buf);
845 
846 	switch (usb_speed) {
847 	case USB_SPEED_SUPER:
848 		usb_host = 3;
849 		break;
850 	case USB_SPEED_HIGH:
851 		usb_host = 2;
852 		break;
853 	case USB_SPEED_FULL:
854 	case USB_SPEED_LOW:
855 		usb_host = 1;
856 		queue_num = 0;
857 		break;
858 	default:
859 		usb_host = 0;
860 		break;
861 	}
862 
863 	if (dev->net->mtu > 12500 && dev->net->mtu <= 16334)
864 		queue_num = 2; /* For Jumbo packet 16KB */
865 
866 	memcpy(buf, &AQC111_BULKIN_SIZE[queue_num], 5);
867 	/* RX bulk configuration */
868 	aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_RX_BULKIN_QCTRL, 5, 5, buf);
869 
870 	/* Set high low water level */
871 	if (dev->net->mtu <= 4500)
872 		reg16 = 0x0810;
873 	else if (dev->net->mtu <= 9500)
874 		reg16 = 0x1020;
875 	else if (dev->net->mtu <= 12500)
876 		reg16 = 0x1420;
877 	else if (dev->net->mtu <= 16334)
878 		reg16 = 0x1A20;
879 
880 	aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_PAUSE_WATERLVL_LOW,
881 			   2, &reg16);
882 	netdev_info(dev->net, "Link Speed %d, USB %d", link_speed, usb_host);
883 }
884 
885 static void aqc111_configure_csum_offload(struct usbnet *dev)
886 {
887 	u8 reg8 = 0;
888 
889 	if (dev->net->features & NETIF_F_RXCSUM) {
890 		reg8 |= SFR_RXCOE_IP | SFR_RXCOE_TCP | SFR_RXCOE_UDP |
891 			SFR_RXCOE_TCPV6 | SFR_RXCOE_UDPV6;
892 	}
893 	aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_RXCOE_CTL, 1, 1, &reg8);
894 
895 	reg8 = 0;
896 	if (dev->net->features & NETIF_F_IP_CSUM)
897 		reg8 |= SFR_TXCOE_IP | SFR_TXCOE_TCP | SFR_TXCOE_UDP;
898 
899 	if (dev->net->features & NETIF_F_IPV6_CSUM)
900 		reg8 |= SFR_TXCOE_TCPV6 | SFR_TXCOE_UDPV6;
901 
902 	aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_TXCOE_CTL, 1, 1, &reg8);
903 }
904 
905 static int aqc111_link_reset(struct usbnet *dev)
906 {
907 	struct aqc111_data *aqc111_data = dev->driver_priv;
908 	u16 reg16 = 0;
909 	u8 reg8 = 0;
910 
911 	if (aqc111_data->link == 1) { /* Link up */
912 		aqc111_configure_rx(dev, aqc111_data);
913 
914 		/* Vlan Tag Filter */
915 		reg8 = SFR_VLAN_CONTROL_VSO;
916 		if (dev->net->features & NETIF_F_HW_VLAN_CTAG_FILTER)
917 			reg8 |= SFR_VLAN_CONTROL_VFE;
918 
919 		aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL,
920 				 1, 1, &reg8);
921 
922 		reg8 = 0x0;
923 		aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BMRX_DMA_CONTROL,
924 				 1, 1, &reg8);
925 
926 		aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BMTX_DMA_CONTROL,
927 				 1, 1, &reg8);
928 
929 		aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_ARC_CTRL, 1, 1, &reg8);
930 
931 		reg16 = SFR_RX_CTL_IPE | SFR_RX_CTL_AB;
932 		aqc111_data->rxctl = reg16;
933 		aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_RX_CTL, 2, &reg16);
934 
935 		reg8 = SFR_RX_PATH_READY;
936 		aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_ETH_MAC_PATH,
937 				 1, 1, &reg8);
938 
939 		reg8 = SFR_BULK_OUT_EFF_EN;
940 		aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BULK_OUT_CTRL,
941 				 1, 1, &reg8);
942 
943 		reg16 = 0;
944 		aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
945 				   2, &reg16);
946 
947 		reg16 = SFR_MEDIUM_XGMIIMODE | SFR_MEDIUM_FULL_DUPLEX;
948 		aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
949 				   2, &reg16);
950 
951 		aqc111_configure_csum_offload(dev);
952 
953 		aqc111_set_rx_mode(dev->net);
954 
955 		aqc111_read16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
956 				  2, &reg16);
957 
958 		if (dev->net->mtu > 1500)
959 			reg16 |= SFR_MEDIUM_JUMBO_EN;
960 
961 		reg16 |= SFR_MEDIUM_RECEIVE_EN | SFR_MEDIUM_RXFLOW_CTRLEN |
962 			 SFR_MEDIUM_TXFLOW_CTRLEN;
963 		aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
964 				   2, &reg16);
965 
966 		aqc111_data->rxctl |= SFR_RX_CTL_START;
967 		aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_RX_CTL,
968 				   2, &aqc111_data->rxctl);
969 
970 		netif_carrier_on(dev->net);
971 	} else {
972 		aqc111_read16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
973 				  2, &reg16);
974 		reg16 &= ~SFR_MEDIUM_RECEIVE_EN;
975 		aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
976 				   2, &reg16);
977 
978 		aqc111_data->rxctl &= ~SFR_RX_CTL_START;
979 		aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_RX_CTL,
980 				   2, &aqc111_data->rxctl);
981 
982 		reg8 = SFR_BULK_OUT_FLUSH_EN | SFR_BULK_OUT_EFF_EN;
983 		aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BULK_OUT_CTRL,
984 				 1, 1, &reg8);
985 		reg8 = SFR_BULK_OUT_EFF_EN;
986 		aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BULK_OUT_CTRL,
987 				 1, 1, &reg8);
988 
989 		netif_carrier_off(dev->net);
990 	}
991 	return 0;
992 }
993 
994 static int aqc111_reset(struct usbnet *dev)
995 {
996 	struct aqc111_data *aqc111_data = dev->driver_priv;
997 	u8 reg8 = 0;
998 
999 	dev->rx_urb_size = URB_SIZE;
1000 
1001 	if (usb_device_no_sg_constraint(dev->udev))
1002 		dev->can_dma_sg = 1;
1003 
1004 	dev->net->hw_features |= AQ_SUPPORT_HW_FEATURE;
1005 	dev->net->features |= AQ_SUPPORT_FEATURE;
1006 	dev->net->vlan_features |= AQ_SUPPORT_VLAN_FEATURE;
1007 
1008 	/* Power up ethernet PHY */
1009 	aqc111_data->phy_cfg = AQ_PHY_POWER_EN;
1010 	aqc111_write32_cmd(dev, AQ_PHY_OPS, 0, 0,
1011 			   &aqc111_data->phy_cfg);
1012 
1013 	/* Set the MAC address */
1014 	aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_NODE_ID, ETH_ALEN,
1015 			 ETH_ALEN, dev->net->dev_addr);
1016 
1017 	reg8 = 0xFF;
1018 	aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BM_INT_MASK, 1, 1, &reg8);
1019 
1020 	reg8 = 0x0;
1021 	aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_SWP_CTRL, 1, 1, &reg8);
1022 
1023 	aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_MONITOR_MODE, 1, 1, &reg8);
1024 	reg8 &= ~(SFR_MONITOR_MODE_EPHYRW | SFR_MONITOR_MODE_RWLC |
1025 		  SFR_MONITOR_MODE_RWMP | SFR_MONITOR_MODE_RWWF |
1026 		  SFR_MONITOR_MODE_RW_FLAG);
1027 	aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_MONITOR_MODE, 1, 1, &reg8);
1028 
1029 	netif_carrier_off(dev->net);
1030 
1031 	/* Phy advertise */
1032 	aqc111_set_phy_speed(dev, aqc111_data->autoneg,
1033 			     aqc111_data->advertised_speed);
1034 
1035 	return 0;
1036 }
1037 
1038 static int aqc111_stop(struct usbnet *dev)
1039 {
1040 	struct aqc111_data *aqc111_data = dev->driver_priv;
1041 	u16 reg16 = 0;
1042 
1043 	aqc111_read16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
1044 			  2, &reg16);
1045 	reg16 &= ~SFR_MEDIUM_RECEIVE_EN;
1046 	aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
1047 			   2, &reg16);
1048 	reg16 = 0;
1049 	aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_RX_CTL, 2, &reg16);
1050 
1051 	/* Put PHY to low power*/
1052 	aqc111_data->phy_cfg |= AQ_LOW_POWER;
1053 	aqc111_write32_cmd(dev, AQ_PHY_OPS, 0, 0,
1054 			   &aqc111_data->phy_cfg);
1055 
1056 	netif_carrier_off(dev->net);
1057 
1058 	return 0;
1059 }
1060 
1061 static void aqc111_rx_checksum(struct sk_buff *skb, u64 pkt_desc)
1062 {
1063 	u32 pkt_type = 0;
1064 
1065 	skb->ip_summed = CHECKSUM_NONE;
1066 	/* checksum error bit is set */
1067 	if (pkt_desc & AQ_RX_PD_L4_ERR || pkt_desc & AQ_RX_PD_L3_ERR)
1068 		return;
1069 
1070 	pkt_type = pkt_desc & AQ_RX_PD_L4_TYPE_MASK;
1071 	/* It must be a TCP or UDP packet with a valid checksum */
1072 	if (pkt_type == AQ_RX_PD_L4_TCP || pkt_type == AQ_RX_PD_L4_UDP)
1073 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1074 }
1075 
1076 static int aqc111_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
1077 {
1078 	struct aqc111_data *aqc111_data = dev->driver_priv;
1079 	struct sk_buff *new_skb = NULL;
1080 	u32 pkt_total_offset = 0;
1081 	u64 *pkt_desc_ptr = NULL;
1082 	u32 start_of_descs = 0;
1083 	u32 desc_offset = 0; /*RX Header Offset*/
1084 	u16 pkt_count = 0;
1085 	u64 desc_hdr = 0;
1086 	u16 vlan_tag = 0;
1087 	u32 skb_len;
1088 
1089 	if (!skb)
1090 		goto err;
1091 
1092 	skb_len = skb->len;
1093 	if (skb_len < sizeof(desc_hdr))
1094 		goto err;
1095 
1096 	/* RX Descriptor Header */
1097 	skb_trim(skb, skb_len - sizeof(desc_hdr));
1098 	desc_hdr = le64_to_cpup((u64 *)skb_tail_pointer(skb));
1099 
1100 	/* Check these packets */
1101 	desc_offset = (desc_hdr & AQ_RX_DH_DESC_OFFSET_MASK) >>
1102 		      AQ_RX_DH_DESC_OFFSET_SHIFT;
1103 	pkt_count = desc_hdr & AQ_RX_DH_PKT_CNT_MASK;
1104 	start_of_descs = skb_len - ((pkt_count + 1) *  sizeof(desc_hdr));
1105 
1106 	/* self check descs position */
1107 	if (start_of_descs != desc_offset)
1108 		goto err;
1109 
1110 	/* self check desc_offset from header and make sure that the
1111 	 * bounds of the metadata array are inside the SKB
1112 	 */
1113 	if (pkt_count * 2 + desc_offset >= skb_len)
1114 		goto err;
1115 
1116 	/* Packets must not overlap the metadata array */
1117 	skb_trim(skb, desc_offset);
1118 
1119 	if (pkt_count == 0)
1120 		goto err;
1121 
1122 	/* Get the first RX packet descriptor */
1123 	pkt_desc_ptr = (u64 *)(skb->data + desc_offset);
1124 
1125 	while (pkt_count--) {
1126 		u64 pkt_desc = le64_to_cpup(pkt_desc_ptr);
1127 		u32 pkt_len_with_padd = 0;
1128 		u32 pkt_len = 0;
1129 
1130 		pkt_len = (u32)((pkt_desc & AQ_RX_PD_LEN_MASK) >>
1131 			  AQ_RX_PD_LEN_SHIFT);
1132 		pkt_len_with_padd = ((pkt_len + 7) & 0x7FFF8);
1133 
1134 		pkt_total_offset += pkt_len_with_padd;
1135 		if (pkt_total_offset > desc_offset ||
1136 		    (pkt_count == 0 && pkt_total_offset != desc_offset)) {
1137 			goto err;
1138 		}
1139 
1140 		if (pkt_desc & AQ_RX_PD_DROP ||
1141 		    !(pkt_desc & AQ_RX_PD_RX_OK) ||
1142 		    pkt_len > (dev->hard_mtu + AQ_RX_HW_PAD)) {
1143 			skb_pull(skb, pkt_len_with_padd);
1144 			/* Next RX Packet Descriptor */
1145 			pkt_desc_ptr++;
1146 			continue;
1147 		}
1148 
1149 		new_skb = netdev_alloc_skb_ip_align(dev->net, pkt_len);
1150 
1151 		if (!new_skb)
1152 			goto err;
1153 
1154 		skb_put(new_skb, pkt_len);
1155 		memcpy(new_skb->data, skb->data, pkt_len);
1156 		skb_pull(new_skb, AQ_RX_HW_PAD);
1157 
1158 		if (aqc111_data->rx_checksum)
1159 			aqc111_rx_checksum(new_skb, pkt_desc);
1160 
1161 		if (pkt_desc & AQ_RX_PD_VLAN) {
1162 			vlan_tag = pkt_desc >> AQ_RX_PD_VLAN_SHIFT;
1163 			__vlan_hwaccel_put_tag(new_skb, htons(ETH_P_8021Q),
1164 					       vlan_tag & VLAN_VID_MASK);
1165 		}
1166 
1167 		usbnet_skb_return(dev, new_skb);
1168 		if (pkt_count == 0)
1169 			break;
1170 
1171 		skb_pull(skb, pkt_len_with_padd);
1172 
1173 		/* Next RX Packet Header */
1174 		pkt_desc_ptr++;
1175 
1176 		new_skb = NULL;
1177 	}
1178 
1179 	return 1;
1180 
1181 err:
1182 	return 0;
1183 }
1184 
1185 static struct sk_buff *aqc111_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
1186 				       gfp_t flags)
1187 {
1188 	int frame_size = dev->maxpacket;
1189 	struct sk_buff *new_skb = NULL;
1190 	u64 *tx_desc_ptr = NULL;
1191 	int padding_size = 0;
1192 	int headroom = 0;
1193 	int tailroom = 0;
1194 	u64 tx_desc = 0;
1195 	u16 tci = 0;
1196 
1197 	/*Length of actual data*/
1198 	tx_desc |= skb->len & AQ_TX_DESC_LEN_MASK;
1199 
1200 	/* TSO MSS */
1201 	tx_desc |= ((u64)(skb_shinfo(skb)->gso_size & AQ_TX_DESC_MSS_MASK)) <<
1202 		   AQ_TX_DESC_MSS_SHIFT;
1203 
1204 	headroom = (skb->len + sizeof(tx_desc)) % 8;
1205 	if (headroom != 0)
1206 		padding_size = 8 - headroom;
1207 
1208 	if (((skb->len + sizeof(tx_desc) + padding_size) % frame_size) == 0) {
1209 		padding_size += 8;
1210 		tx_desc |= AQ_TX_DESC_DROP_PADD;
1211 	}
1212 
1213 	/* Vlan Tag */
1214 	if (vlan_get_tag(skb, &tci) >= 0) {
1215 		tx_desc |= AQ_TX_DESC_VLAN;
1216 		tx_desc |= ((u64)tci & AQ_TX_DESC_VLAN_MASK) <<
1217 			   AQ_TX_DESC_VLAN_SHIFT;
1218 	}
1219 
1220 	if (!dev->can_dma_sg && (dev->net->features & NETIF_F_SG) &&
1221 	    skb_linearize(skb))
1222 		return NULL;
1223 
1224 	headroom = skb_headroom(skb);
1225 	tailroom = skb_tailroom(skb);
1226 
1227 	if (!(headroom >= sizeof(tx_desc) && tailroom >= padding_size)) {
1228 		new_skb = skb_copy_expand(skb, sizeof(tx_desc),
1229 					  padding_size, flags);
1230 		dev_kfree_skb_any(skb);
1231 		skb = new_skb;
1232 		if (!skb)
1233 			return NULL;
1234 	}
1235 	if (padding_size != 0)
1236 		skb_put_zero(skb, padding_size);
1237 	/* Copy TX header */
1238 	tx_desc_ptr = skb_push(skb, sizeof(tx_desc));
1239 	*tx_desc_ptr = cpu_to_le64(tx_desc);
1240 
1241 	usbnet_set_skb_tx_stats(skb, 1, 0);
1242 
1243 	return skb;
1244 }
1245 
1246 static const struct driver_info aqc111_info = {
1247 	.description	= "Aquantia AQtion USB to 5GbE Controller",
1248 	.bind		= aqc111_bind,
1249 	.unbind		= aqc111_unbind,
1250 	.status		= aqc111_status,
1251 	.link_reset	= aqc111_link_reset,
1252 	.reset		= aqc111_reset,
1253 	.stop		= aqc111_stop,
1254 	.flags		= FLAG_ETHER | FLAG_FRAMING_AX |
1255 			  FLAG_AVOID_UNLINK_URBS | FLAG_MULTI_PACKET,
1256 	.rx_fixup	= aqc111_rx_fixup,
1257 	.tx_fixup	= aqc111_tx_fixup,
1258 };
1259 
1260 #define ASIX111_DESC \
1261 "ASIX USB 3.1 Gen1 to 5G Multi-Gigabit Ethernet Adapter"
1262 
1263 static const struct driver_info asix111_info = {
1264 	.description	= ASIX111_DESC,
1265 	.bind		= aqc111_bind,
1266 	.unbind		= aqc111_unbind,
1267 	.status		= aqc111_status,
1268 	.link_reset	= aqc111_link_reset,
1269 	.reset		= aqc111_reset,
1270 	.stop		= aqc111_stop,
1271 	.flags		= FLAG_ETHER | FLAG_FRAMING_AX |
1272 			  FLAG_AVOID_UNLINK_URBS | FLAG_MULTI_PACKET,
1273 	.rx_fixup	= aqc111_rx_fixup,
1274 	.tx_fixup	= aqc111_tx_fixup,
1275 };
1276 
1277 #undef ASIX111_DESC
1278 
1279 #define ASIX112_DESC \
1280 "ASIX USB 3.1 Gen1 to 2.5G Multi-Gigabit Ethernet Adapter"
1281 
1282 static const struct driver_info asix112_info = {
1283 	.description	= ASIX112_DESC,
1284 	.bind		= aqc111_bind,
1285 	.unbind		= aqc111_unbind,
1286 	.status		= aqc111_status,
1287 	.link_reset	= aqc111_link_reset,
1288 	.reset		= aqc111_reset,
1289 	.stop		= aqc111_stop,
1290 	.flags		= FLAG_ETHER | FLAG_FRAMING_AX |
1291 			  FLAG_AVOID_UNLINK_URBS | FLAG_MULTI_PACKET,
1292 	.rx_fixup	= aqc111_rx_fixup,
1293 	.tx_fixup	= aqc111_tx_fixup,
1294 };
1295 
1296 #undef ASIX112_DESC
1297 
1298 static const struct driver_info trendnet_info = {
1299 	.description	= "USB-C 3.1 to 5GBASE-T Ethernet Adapter",
1300 	.bind		= aqc111_bind,
1301 	.unbind		= aqc111_unbind,
1302 	.status		= aqc111_status,
1303 	.link_reset	= aqc111_link_reset,
1304 	.reset		= aqc111_reset,
1305 	.stop		= aqc111_stop,
1306 	.flags		= FLAG_ETHER | FLAG_FRAMING_AX |
1307 			  FLAG_AVOID_UNLINK_URBS | FLAG_MULTI_PACKET,
1308 	.rx_fixup	= aqc111_rx_fixup,
1309 	.tx_fixup	= aqc111_tx_fixup,
1310 };
1311 
1312 static const struct driver_info qnap_info = {
1313 	.description	= "QNAP QNA-UC5G1T USB to 5GbE Adapter",
1314 	.bind		= aqc111_bind,
1315 	.unbind		= aqc111_unbind,
1316 	.status		= aqc111_status,
1317 	.link_reset	= aqc111_link_reset,
1318 	.reset		= aqc111_reset,
1319 	.stop		= aqc111_stop,
1320 	.flags		= FLAG_ETHER | FLAG_FRAMING_AX |
1321 			  FLAG_AVOID_UNLINK_URBS | FLAG_MULTI_PACKET,
1322 	.rx_fixup	= aqc111_rx_fixup,
1323 	.tx_fixup	= aqc111_tx_fixup,
1324 };
1325 
1326 static int aqc111_suspend(struct usb_interface *intf, pm_message_t message)
1327 {
1328 	struct usbnet *dev = usb_get_intfdata(intf);
1329 	struct aqc111_data *aqc111_data = dev->driver_priv;
1330 	u16 temp_rx_ctrl = 0x00;
1331 	u16 reg16;
1332 	u8 reg8;
1333 
1334 	usbnet_suspend(intf, message);
1335 
1336 	aqc111_read16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_CTL, 2, &reg16);
1337 	temp_rx_ctrl = reg16;
1338 	/* Stop RX operations*/
1339 	reg16 &= ~SFR_RX_CTL_START;
1340 	aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_CTL, 2, &reg16);
1341 	/* Force bz */
1342 	aqc111_read16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_PHYPWR_RSTCTL,
1343 			       2, &reg16);
1344 	reg16 |= SFR_PHYPWR_RSTCTL_BZ;
1345 	aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_PHYPWR_RSTCTL,
1346 				2, &reg16);
1347 
1348 	reg8 = SFR_BULK_OUT_EFF_EN;
1349 	aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_BULK_OUT_CTRL,
1350 			      1, 1, &reg8);
1351 
1352 	temp_rx_ctrl &= ~(SFR_RX_CTL_START | SFR_RX_CTL_RF_WAK |
1353 			  SFR_RX_CTL_AP | SFR_RX_CTL_AM);
1354 	aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_CTL,
1355 				2, &temp_rx_ctrl);
1356 
1357 	reg8 = 0x00;
1358 	aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_ETH_MAC_PATH,
1359 			      1, 1, &reg8);
1360 
1361 	if (aqc111_data->wol_flags) {
1362 		struct aqc111_wol_cfg wol_cfg;
1363 
1364 		memset(&wol_cfg, 0, sizeof(struct aqc111_wol_cfg));
1365 
1366 		aqc111_data->phy_cfg |= AQ_WOL;
1367 		ether_addr_copy(wol_cfg.hw_addr, dev->net->dev_addr);
1368 		wol_cfg.flags = aqc111_data->wol_flags;
1369 
1370 		temp_rx_ctrl |= (SFR_RX_CTL_AB | SFR_RX_CTL_START);
1371 		aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_CTL,
1372 					2, &temp_rx_ctrl);
1373 		reg8 = 0x00;
1374 		aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_BM_INT_MASK,
1375 				      1, 1, &reg8);
1376 		reg8 = SFR_BMRX_DMA_EN;
1377 		aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_BMRX_DMA_CONTROL,
1378 				      1, 1, &reg8);
1379 		reg8 = SFR_RX_PATH_READY;
1380 		aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_ETH_MAC_PATH,
1381 				      1, 1, &reg8);
1382 		reg8 = 0x07;
1383 		aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_BULKIN_QCTRL,
1384 				      1, 1, &reg8);
1385 		reg8 = 0x00;
1386 		aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC,
1387 				      SFR_RX_BULKIN_QTIMR_LOW, 1, 1, &reg8);
1388 		aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC,
1389 				      SFR_RX_BULKIN_QTIMR_HIGH, 1, 1, &reg8);
1390 		reg8 = 0xFF;
1391 		aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_BULKIN_QSIZE,
1392 				      1, 1, &reg8);
1393 		aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_BULKIN_QIFG,
1394 				      1, 1, &reg8);
1395 
1396 		aqc111_read16_cmd_nopm(dev, AQ_ACCESS_MAC,
1397 				       SFR_MEDIUM_STATUS_MODE, 2, &reg16);
1398 		reg16 |= SFR_MEDIUM_RECEIVE_EN;
1399 		aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC,
1400 					SFR_MEDIUM_STATUS_MODE, 2, &reg16);
1401 
1402 		aqc111_write_cmd_nopm(dev, AQ_WOL_CFG, 0, 0,
1403 				      WOL_CFG_SIZE, &wol_cfg);
1404 		aqc111_write32_cmd_nopm(dev, AQ_PHY_OPS, 0, 0,
1405 					&aqc111_data->phy_cfg);
1406 	} else {
1407 		aqc111_data->phy_cfg |= AQ_LOW_POWER;
1408 		aqc111_write32_cmd_nopm(dev, AQ_PHY_OPS, 0, 0,
1409 					&aqc111_data->phy_cfg);
1410 
1411 		/* Disable RX path */
1412 		aqc111_read16_cmd_nopm(dev, AQ_ACCESS_MAC,
1413 				       SFR_MEDIUM_STATUS_MODE, 2, &reg16);
1414 		reg16 &= ~SFR_MEDIUM_RECEIVE_EN;
1415 		aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC,
1416 					SFR_MEDIUM_STATUS_MODE, 2, &reg16);
1417 	}
1418 
1419 	return 0;
1420 }
1421 
1422 static int aqc111_resume(struct usb_interface *intf)
1423 {
1424 	struct usbnet *dev = usb_get_intfdata(intf);
1425 	struct aqc111_data *aqc111_data = dev->driver_priv;
1426 	u16 reg16;
1427 	u8 reg8;
1428 
1429 	netif_carrier_off(dev->net);
1430 
1431 	/* Power up ethernet PHY */
1432 	aqc111_data->phy_cfg |= AQ_PHY_POWER_EN;
1433 	aqc111_data->phy_cfg &= ~AQ_LOW_POWER;
1434 	aqc111_data->phy_cfg &= ~AQ_WOL;
1435 
1436 	reg8 = 0xFF;
1437 	aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_BM_INT_MASK,
1438 			      1, 1, &reg8);
1439 	/* Configure RX control register => start operation */
1440 	reg16 = aqc111_data->rxctl;
1441 	reg16 &= ~SFR_RX_CTL_START;
1442 	aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_CTL, 2, &reg16);
1443 
1444 	reg16 |= SFR_RX_CTL_START;
1445 	aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_CTL, 2, &reg16);
1446 
1447 	aqc111_set_phy_speed(dev, aqc111_data->autoneg,
1448 			     aqc111_data->advertised_speed);
1449 
1450 	aqc111_read16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
1451 			       2, &reg16);
1452 	reg16 |= SFR_MEDIUM_RECEIVE_EN;
1453 	aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
1454 				2, &reg16);
1455 	reg8 = SFR_RX_PATH_READY;
1456 	aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_ETH_MAC_PATH,
1457 			      1, 1, &reg8);
1458 	reg8 = 0x0;
1459 	aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BMRX_DMA_CONTROL, 1, 1, &reg8);
1460 
1461 	return usbnet_resume(intf);
1462 }
1463 
1464 #define AQC111_USB_ETH_DEV(vid, pid, table) \
1465 	USB_DEVICE_INTERFACE_CLASS((vid), (pid), USB_CLASS_VENDOR_SPEC), \
1466 	.driver_info = (unsigned long)&(table) \
1467 }, \
1468 { \
1469 	USB_DEVICE_AND_INTERFACE_INFO((vid), (pid), \
1470 				      USB_CLASS_COMM, \
1471 				      USB_CDC_SUBCLASS_ETHERNET, \
1472 				      USB_CDC_PROTO_NONE), \
1473 	.driver_info = (unsigned long)&(table),
1474 
1475 static const struct usb_device_id products[] = {
1476 	{AQC111_USB_ETH_DEV(0x2eca, 0xc101, aqc111_info)},
1477 	{AQC111_USB_ETH_DEV(0x0b95, 0x2790, asix111_info)},
1478 	{AQC111_USB_ETH_DEV(0x0b95, 0x2791, asix112_info)},
1479 	{AQC111_USB_ETH_DEV(0x20f4, 0xe05a, trendnet_info)},
1480 	{AQC111_USB_ETH_DEV(0x1c04, 0x0015, qnap_info)},
1481 	{ },/* END */
1482 };
1483 MODULE_DEVICE_TABLE(usb, products);
1484 
1485 static struct usb_driver aq_driver = {
1486 	.name		= "aqc111",
1487 	.id_table	= products,
1488 	.probe		= usbnet_probe,
1489 	.suspend	= aqc111_suspend,
1490 	.resume		= aqc111_resume,
1491 	.disconnect	= usbnet_disconnect,
1492 };
1493 
1494 module_usb_driver(aq_driver);
1495 
1496 MODULE_DESCRIPTION("Aquantia AQtion USB to 5/2.5GbE Controllers");
1497 MODULE_LICENSE("GPL");
1498