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