Loading...
1/*
2 * ASIX AX88179/178A USB 3.0/2.0 to Gigabit Ethernet Devices
3 *
4 * Copyright (C) 2011-2013 ASIX
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <linux/module.h>
21#include <linux/etherdevice.h>
22#include <linux/mii.h>
23#include <linux/usb.h>
24#include <linux/crc32.h>
25#include <linux/usb/usbnet.h>
26#include <uapi/linux/mdio.h>
27#include <linux/mdio.h>
28
29#define AX88179_PHY_ID 0x03
30#define AX_EEPROM_LEN 0x100
31#define AX88179_EEPROM_MAGIC 0x17900b95
32#define AX_MCAST_FLTSIZE 8
33#define AX_MAX_MCAST 64
34#define AX_INT_PPLS_LINK ((u32)BIT(16))
35#define AX_RXHDR_L4_TYPE_MASK 0x1c
36#define AX_RXHDR_L4_TYPE_UDP 4
37#define AX_RXHDR_L4_TYPE_TCP 16
38#define AX_RXHDR_L3CSUM_ERR 2
39#define AX_RXHDR_L4CSUM_ERR 1
40#define AX_RXHDR_CRC_ERR ((u32)BIT(29))
41#define AX_RXHDR_DROP_ERR ((u32)BIT(31))
42#define AX_ACCESS_MAC 0x01
43#define AX_ACCESS_PHY 0x02
44#define AX_ACCESS_EEPROM 0x04
45#define AX_ACCESS_EFUS 0x05
46#define AX_PAUSE_WATERLVL_HIGH 0x54
47#define AX_PAUSE_WATERLVL_LOW 0x55
48
49#define PHYSICAL_LINK_STATUS 0x02
50 #define AX_USB_SS 0x04
51 #define AX_USB_HS 0x02
52
53#define GENERAL_STATUS 0x03
54/* Check AX88179 version. UA1:Bit2 = 0, UA2:Bit2 = 1 */
55 #define AX_SECLD 0x04
56
57#define AX_SROM_ADDR 0x07
58#define AX_SROM_CMD 0x0a
59 #define EEP_RD 0x04
60 #define EEP_BUSY 0x10
61
62#define AX_SROM_DATA_LOW 0x08
63#define AX_SROM_DATA_HIGH 0x09
64
65#define AX_RX_CTL 0x0b
66 #define AX_RX_CTL_DROPCRCERR 0x0100
67 #define AX_RX_CTL_IPE 0x0200
68 #define AX_RX_CTL_START 0x0080
69 #define AX_RX_CTL_AP 0x0020
70 #define AX_RX_CTL_AM 0x0010
71 #define AX_RX_CTL_AB 0x0008
72 #define AX_RX_CTL_AMALL 0x0002
73 #define AX_RX_CTL_PRO 0x0001
74 #define AX_RX_CTL_STOP 0x0000
75
76#define AX_NODE_ID 0x10
77#define AX_MULFLTARY 0x16
78
79#define AX_MEDIUM_STATUS_MODE 0x22
80 #define AX_MEDIUM_GIGAMODE 0x01
81 #define AX_MEDIUM_FULL_DUPLEX 0x02
82 #define AX_MEDIUM_EN_125MHZ 0x08
83 #define AX_MEDIUM_RXFLOW_CTRLEN 0x10
84 #define AX_MEDIUM_TXFLOW_CTRLEN 0x20
85 #define AX_MEDIUM_RECEIVE_EN 0x100
86 #define AX_MEDIUM_PS 0x200
87 #define AX_MEDIUM_JUMBO_EN 0x8040
88
89#define AX_MONITOR_MOD 0x24
90 #define AX_MONITOR_MODE_RWLC 0x02
91 #define AX_MONITOR_MODE_RWMP 0x04
92 #define AX_MONITOR_MODE_PMEPOL 0x20
93 #define AX_MONITOR_MODE_PMETYPE 0x40
94
95#define AX_GPIO_CTRL 0x25
96 #define AX_GPIO_CTRL_GPIO3EN 0x80
97 #define AX_GPIO_CTRL_GPIO2EN 0x40
98 #define AX_GPIO_CTRL_GPIO1EN 0x20
99
100#define AX_PHYPWR_RSTCTL 0x26
101 #define AX_PHYPWR_RSTCTL_BZ 0x0010
102 #define AX_PHYPWR_RSTCTL_IPRL 0x0020
103 #define AX_PHYPWR_RSTCTL_AT 0x1000
104
105#define AX_RX_BULKIN_QCTRL 0x2e
106#define AX_CLK_SELECT 0x33
107 #define AX_CLK_SELECT_BCS 0x01
108 #define AX_CLK_SELECT_ACS 0x02
109 #define AX_CLK_SELECT_ULR 0x08
110
111#define AX_RXCOE_CTL 0x34
112 #define AX_RXCOE_IP 0x01
113 #define AX_RXCOE_TCP 0x02
114 #define AX_RXCOE_UDP 0x04
115 #define AX_RXCOE_TCPV6 0x20
116 #define AX_RXCOE_UDPV6 0x40
117
118#define AX_TXCOE_CTL 0x35
119 #define AX_TXCOE_IP 0x01
120 #define AX_TXCOE_TCP 0x02
121 #define AX_TXCOE_UDP 0x04
122 #define AX_TXCOE_TCPV6 0x20
123 #define AX_TXCOE_UDPV6 0x40
124
125#define AX_LEDCTRL 0x73
126
127#define GMII_PHY_PHYSR 0x11
128 #define GMII_PHY_PHYSR_SMASK 0xc000
129 #define GMII_PHY_PHYSR_GIGA 0x8000
130 #define GMII_PHY_PHYSR_100 0x4000
131 #define GMII_PHY_PHYSR_FULL 0x2000
132 #define GMII_PHY_PHYSR_LINK 0x400
133
134#define GMII_LED_ACT 0x1a
135 #define GMII_LED_ACTIVE_MASK 0xff8f
136 #define GMII_LED0_ACTIVE BIT(4)
137 #define GMII_LED1_ACTIVE BIT(5)
138 #define GMII_LED2_ACTIVE BIT(6)
139
140#define GMII_LED_LINK 0x1c
141 #define GMII_LED_LINK_MASK 0xf888
142 #define GMII_LED0_LINK_10 BIT(0)
143 #define GMII_LED0_LINK_100 BIT(1)
144 #define GMII_LED0_LINK_1000 BIT(2)
145 #define GMII_LED1_LINK_10 BIT(4)
146 #define GMII_LED1_LINK_100 BIT(5)
147 #define GMII_LED1_LINK_1000 BIT(6)
148 #define GMII_LED2_LINK_10 BIT(8)
149 #define GMII_LED2_LINK_100 BIT(9)
150 #define GMII_LED2_LINK_1000 BIT(10)
151 #define LED0_ACTIVE BIT(0)
152 #define LED0_LINK_10 BIT(1)
153 #define LED0_LINK_100 BIT(2)
154 #define LED0_LINK_1000 BIT(3)
155 #define LED0_FD BIT(4)
156 #define LED0_USB3_MASK 0x001f
157 #define LED1_ACTIVE BIT(5)
158 #define LED1_LINK_10 BIT(6)
159 #define LED1_LINK_100 BIT(7)
160 #define LED1_LINK_1000 BIT(8)
161 #define LED1_FD BIT(9)
162 #define LED1_USB3_MASK 0x03e0
163 #define LED2_ACTIVE BIT(10)
164 #define LED2_LINK_1000 BIT(13)
165 #define LED2_LINK_100 BIT(12)
166 #define LED2_LINK_10 BIT(11)
167 #define LED2_FD BIT(14)
168 #define LED_VALID BIT(15)
169 #define LED2_USB3_MASK 0x7c00
170
171#define GMII_PHYPAGE 0x1e
172#define GMII_PHY_PAGE_SELECT 0x1f
173 #define GMII_PHY_PGSEL_EXT 0x0007
174 #define GMII_PHY_PGSEL_PAGE0 0x0000
175 #define GMII_PHY_PGSEL_PAGE3 0x0003
176 #define GMII_PHY_PGSEL_PAGE5 0x0005
177
178struct ax88179_data {
179 u8 eee_enabled;
180 u8 eee_active;
181 u16 rxctl;
182 u16 reserved;
183};
184
185struct ax88179_int_data {
186 __le32 intdata1;
187 __le32 intdata2;
188};
189
190static const struct {
191 unsigned char ctrl, timer_l, timer_h, size, ifg;
192} AX88179_BULKIN_SIZE[] = {
193 {7, 0x4f, 0, 0x12, 0xff},
194 {7, 0x20, 3, 0x16, 0xff},
195 {7, 0xae, 7, 0x18, 0xff},
196 {7, 0xcc, 0x4c, 0x18, 8},
197};
198
199static int __ax88179_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
200 u16 size, void *data, int in_pm)
201{
202 int ret;
203 int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
204
205 BUG_ON(!dev);
206
207 if (!in_pm)
208 fn = usbnet_read_cmd;
209 else
210 fn = usbnet_read_cmd_nopm;
211
212 ret = fn(dev, cmd, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
213 value, index, data, size);
214
215 if (unlikely(ret < 0))
216 netdev_warn(dev->net, "Failed to read reg index 0x%04x: %d\n",
217 index, ret);
218
219 return ret;
220}
221
222static int __ax88179_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
223 u16 size, void *data, int in_pm)
224{
225 int ret;
226 int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
227
228 BUG_ON(!dev);
229
230 if (!in_pm)
231 fn = usbnet_write_cmd;
232 else
233 fn = usbnet_write_cmd_nopm;
234
235 ret = fn(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
236 value, index, data, size);
237
238 if (unlikely(ret < 0))
239 netdev_warn(dev->net, "Failed to write reg index 0x%04x: %d\n",
240 index, ret);
241
242 return ret;
243}
244
245static void ax88179_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value,
246 u16 index, u16 size, void *data)
247{
248 u16 buf;
249
250 if (2 == size) {
251 buf = *((u16 *)data);
252 cpu_to_le16s(&buf);
253 usbnet_write_cmd_async(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR |
254 USB_RECIP_DEVICE, value, index, &buf,
255 size);
256 } else {
257 usbnet_write_cmd_async(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR |
258 USB_RECIP_DEVICE, value, index, data,
259 size);
260 }
261}
262
263static int ax88179_read_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value,
264 u16 index, u16 size, void *data)
265{
266 int ret;
267
268 if (2 == size) {
269 u16 buf;
270 ret = __ax88179_read_cmd(dev, cmd, value, index, size, &buf, 1);
271 le16_to_cpus(&buf);
272 *((u16 *)data) = buf;
273 } else if (4 == size) {
274 u32 buf;
275 ret = __ax88179_read_cmd(dev, cmd, value, index, size, &buf, 1);
276 le32_to_cpus(&buf);
277 *((u32 *)data) = buf;
278 } else {
279 ret = __ax88179_read_cmd(dev, cmd, value, index, size, data, 1);
280 }
281
282 return ret;
283}
284
285static int ax88179_write_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value,
286 u16 index, u16 size, void *data)
287{
288 int ret;
289
290 if (2 == size) {
291 u16 buf;
292 buf = *((u16 *)data);
293 cpu_to_le16s(&buf);
294 ret = __ax88179_write_cmd(dev, cmd, value, index,
295 size, &buf, 1);
296 } else {
297 ret = __ax88179_write_cmd(dev, cmd, value, index,
298 size, data, 1);
299 }
300
301 return ret;
302}
303
304static int ax88179_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
305 u16 size, void *data)
306{
307 int ret;
308
309 if (2 == size) {
310 u16 buf;
311 ret = __ax88179_read_cmd(dev, cmd, value, index, size, &buf, 0);
312 le16_to_cpus(&buf);
313 *((u16 *)data) = buf;
314 } else if (4 == size) {
315 u32 buf;
316 ret = __ax88179_read_cmd(dev, cmd, value, index, size, &buf, 0);
317 le32_to_cpus(&buf);
318 *((u32 *)data) = buf;
319 } else {
320 ret = __ax88179_read_cmd(dev, cmd, value, index, size, data, 0);
321 }
322
323 return ret;
324}
325
326static int ax88179_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
327 u16 size, void *data)
328{
329 int ret;
330
331 if (2 == size) {
332 u16 buf;
333 buf = *((u16 *)data);
334 cpu_to_le16s(&buf);
335 ret = __ax88179_write_cmd(dev, cmd, value, index,
336 size, &buf, 0);
337 } else {
338 ret = __ax88179_write_cmd(dev, cmd, value, index,
339 size, data, 0);
340 }
341
342 return ret;
343}
344
345static void ax88179_status(struct usbnet *dev, struct urb *urb)
346{
347 struct ax88179_int_data *event;
348 u32 link;
349
350 if (urb->actual_length < 8)
351 return;
352
353 event = urb->transfer_buffer;
354 le32_to_cpus((void *)&event->intdata1);
355
356 link = (((__force u32)event->intdata1) & AX_INT_PPLS_LINK) >> 16;
357
358 if (netif_carrier_ok(dev->net) != link) {
359 usbnet_link_change(dev, link, 1);
360 netdev_info(dev->net, "ax88179 - Link status is: %d\n", link);
361 }
362}
363
364static int ax88179_mdio_read(struct net_device *netdev, int phy_id, int loc)
365{
366 struct usbnet *dev = netdev_priv(netdev);
367 u16 res;
368
369 ax88179_read_cmd(dev, AX_ACCESS_PHY, phy_id, (__u16)loc, 2, &res);
370 return res;
371}
372
373static void ax88179_mdio_write(struct net_device *netdev, int phy_id, int loc,
374 int val)
375{
376 struct usbnet *dev = netdev_priv(netdev);
377 u16 res = (u16) val;
378
379 ax88179_write_cmd(dev, AX_ACCESS_PHY, phy_id, (__u16)loc, 2, &res);
380}
381
382static inline int ax88179_phy_mmd_indirect(struct usbnet *dev, u16 prtad,
383 u16 devad)
384{
385 u16 tmp16;
386 int ret;
387
388 tmp16 = devad;
389 ret = ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
390 MII_MMD_CTRL, 2, &tmp16);
391
392 tmp16 = prtad;
393 ret = ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
394 MII_MMD_DATA, 2, &tmp16);
395
396 tmp16 = devad | MII_MMD_CTRL_NOINCR;
397 ret = ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
398 MII_MMD_CTRL, 2, &tmp16);
399
400 return ret;
401}
402
403static int
404ax88179_phy_read_mmd_indirect(struct usbnet *dev, u16 prtad, u16 devad)
405{
406 int ret;
407 u16 tmp16;
408
409 ax88179_phy_mmd_indirect(dev, prtad, devad);
410
411 ret = ax88179_read_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
412 MII_MMD_DATA, 2, &tmp16);
413 if (ret < 0)
414 return ret;
415
416 return tmp16;
417}
418
419static int
420ax88179_phy_write_mmd_indirect(struct usbnet *dev, u16 prtad, u16 devad,
421 u16 data)
422{
423 int ret;
424
425 ax88179_phy_mmd_indirect(dev, prtad, devad);
426
427 ret = ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
428 MII_MMD_DATA, 2, &data);
429
430 if (ret < 0)
431 return ret;
432
433 return 0;
434}
435
436static int ax88179_suspend(struct usb_interface *intf, pm_message_t message)
437{
438 struct usbnet *dev = usb_get_intfdata(intf);
439 u16 tmp16;
440 u8 tmp8;
441
442 usbnet_suspend(intf, message);
443
444 /* Disable RX path */
445 ax88179_read_cmd_nopm(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
446 2, 2, &tmp16);
447 tmp16 &= ~AX_MEDIUM_RECEIVE_EN;
448 ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
449 2, 2, &tmp16);
450
451 /* Force bulk-in zero length */
452 ax88179_read_cmd_nopm(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL,
453 2, 2, &tmp16);
454
455 tmp16 |= AX_PHYPWR_RSTCTL_BZ | AX_PHYPWR_RSTCTL_IPRL;
456 ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL,
457 2, 2, &tmp16);
458
459 /* change clock */
460 tmp8 = 0;
461 ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, &tmp8);
462
463 /* Configure RX control register => stop operation */
464 tmp16 = AX_RX_CTL_STOP;
465 ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, &tmp16);
466
467 return 0;
468}
469
470/* This function is used to enable the autodetach function. */
471/* This function is determined by offset 0x43 of EEPROM */
472static int ax88179_auto_detach(struct usbnet *dev, int in_pm)
473{
474 u16 tmp16;
475 u8 tmp8;
476 int (*fnr)(struct usbnet *, u8, u16, u16, u16, void *);
477 int (*fnw)(struct usbnet *, u8, u16, u16, u16, void *);
478
479 if (!in_pm) {
480 fnr = ax88179_read_cmd;
481 fnw = ax88179_write_cmd;
482 } else {
483 fnr = ax88179_read_cmd_nopm;
484 fnw = ax88179_write_cmd_nopm;
485 }
486
487 if (fnr(dev, AX_ACCESS_EEPROM, 0x43, 1, 2, &tmp16) < 0)
488 return 0;
489
490 if ((tmp16 == 0xFFFF) || (!(tmp16 & 0x0100)))
491 return 0;
492
493 /* Enable Auto Detach bit */
494 tmp8 = 0;
495 fnr(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, &tmp8);
496 tmp8 |= AX_CLK_SELECT_ULR;
497 fnw(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, &tmp8);
498
499 fnr(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, &tmp16);
500 tmp16 |= AX_PHYPWR_RSTCTL_AT;
501 fnw(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, &tmp16);
502
503 return 0;
504}
505
506static int ax88179_resume(struct usb_interface *intf)
507{
508 struct usbnet *dev = usb_get_intfdata(intf);
509 u16 tmp16;
510 u8 tmp8;
511
512 usbnet_link_change(dev, 0, 0);
513
514 /* Power up ethernet PHY */
515 tmp16 = 0;
516 ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL,
517 2, 2, &tmp16);
518 udelay(1000);
519
520 tmp16 = AX_PHYPWR_RSTCTL_IPRL;
521 ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL,
522 2, 2, &tmp16);
523 msleep(200);
524
525 /* Ethernet PHY Auto Detach*/
526 ax88179_auto_detach(dev, 1);
527
528 /* Enable clock */
529 ax88179_read_cmd_nopm(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, &tmp8);
530 tmp8 |= AX_CLK_SELECT_ACS | AX_CLK_SELECT_BCS;
531 ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, &tmp8);
532 msleep(100);
533
534 /* Configure RX control register => start operation */
535 tmp16 = AX_RX_CTL_DROPCRCERR | AX_RX_CTL_IPE | AX_RX_CTL_START |
536 AX_RX_CTL_AP | AX_RX_CTL_AMALL | AX_RX_CTL_AB;
537 ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, &tmp16);
538
539 return usbnet_resume(intf);
540}
541
542static void
543ax88179_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
544{
545 struct usbnet *dev = netdev_priv(net);
546 u8 opt;
547
548 if (ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_MONITOR_MOD,
549 1, 1, &opt) < 0) {
550 wolinfo->supported = 0;
551 wolinfo->wolopts = 0;
552 return;
553 }
554
555 wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
556 wolinfo->wolopts = 0;
557 if (opt & AX_MONITOR_MODE_RWLC)
558 wolinfo->wolopts |= WAKE_PHY;
559 if (opt & AX_MONITOR_MODE_RWMP)
560 wolinfo->wolopts |= WAKE_MAGIC;
561}
562
563static int
564ax88179_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
565{
566 struct usbnet *dev = netdev_priv(net);
567 u8 opt = 0;
568
569 if (wolinfo->wolopts & WAKE_PHY)
570 opt |= AX_MONITOR_MODE_RWLC;
571 if (wolinfo->wolopts & WAKE_MAGIC)
572 opt |= AX_MONITOR_MODE_RWMP;
573
574 if (ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MONITOR_MOD,
575 1, 1, &opt) < 0)
576 return -EINVAL;
577
578 return 0;
579}
580
581static int ax88179_get_eeprom_len(struct net_device *net)
582{
583 return AX_EEPROM_LEN;
584}
585
586static int
587ax88179_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
588 u8 *data)
589{
590 struct usbnet *dev = netdev_priv(net);
591 u16 *eeprom_buff;
592 int first_word, last_word;
593 int i, ret;
594
595 if (eeprom->len == 0)
596 return -EINVAL;
597
598 eeprom->magic = AX88179_EEPROM_MAGIC;
599
600 first_word = eeprom->offset >> 1;
601 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
602 eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1),
603 GFP_KERNEL);
604 if (!eeprom_buff)
605 return -ENOMEM;
606
607 /* ax88179/178A returns 2 bytes from eeprom on read */
608 for (i = first_word; i <= last_word; i++) {
609 ret = __ax88179_read_cmd(dev, AX_ACCESS_EEPROM, i, 1, 2,
610 &eeprom_buff[i - first_word],
611 0);
612 if (ret < 0) {
613 kfree(eeprom_buff);
614 return -EIO;
615 }
616 }
617
618 memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
619 kfree(eeprom_buff);
620 return 0;
621}
622
623static int ax88179_get_link_ksettings(struct net_device *net,
624 struct ethtool_link_ksettings *cmd)
625{
626 struct usbnet *dev = netdev_priv(net);
627
628 mii_ethtool_get_link_ksettings(&dev->mii, cmd);
629
630 return 0;
631}
632
633static int ax88179_set_link_ksettings(struct net_device *net,
634 const struct ethtool_link_ksettings *cmd)
635{
636 struct usbnet *dev = netdev_priv(net);
637 return mii_ethtool_set_link_ksettings(&dev->mii, cmd);
638}
639
640static int
641ax88179_ethtool_get_eee(struct usbnet *dev, struct ethtool_eee *data)
642{
643 int val;
644
645 /* Get Supported EEE */
646 val = ax88179_phy_read_mmd_indirect(dev, MDIO_PCS_EEE_ABLE,
647 MDIO_MMD_PCS);
648 if (val < 0)
649 return val;
650 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
651
652 /* Get advertisement EEE */
653 val = ax88179_phy_read_mmd_indirect(dev, MDIO_AN_EEE_ADV,
654 MDIO_MMD_AN);
655 if (val < 0)
656 return val;
657 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
658
659 /* Get LP advertisement EEE */
660 val = ax88179_phy_read_mmd_indirect(dev, MDIO_AN_EEE_LPABLE,
661 MDIO_MMD_AN);
662 if (val < 0)
663 return val;
664 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
665
666 return 0;
667}
668
669static int
670ax88179_ethtool_set_eee(struct usbnet *dev, struct ethtool_eee *data)
671{
672 u16 tmp16 = ethtool_adv_to_mmd_eee_adv_t(data->advertised);
673
674 return ax88179_phy_write_mmd_indirect(dev, MDIO_AN_EEE_ADV,
675 MDIO_MMD_AN, tmp16);
676}
677
678static int ax88179_chk_eee(struct usbnet *dev)
679{
680 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
681 struct ax88179_data *priv = (struct ax88179_data *)dev->data;
682
683 mii_ethtool_gset(&dev->mii, &ecmd);
684
685 if (ecmd.duplex & DUPLEX_FULL) {
686 int eee_lp, eee_cap, eee_adv;
687 u32 lp, cap, adv, supported = 0;
688
689 eee_cap = ax88179_phy_read_mmd_indirect(dev,
690 MDIO_PCS_EEE_ABLE,
691 MDIO_MMD_PCS);
692 if (eee_cap < 0) {
693 priv->eee_active = 0;
694 return false;
695 }
696
697 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
698 if (!cap) {
699 priv->eee_active = 0;
700 return false;
701 }
702
703 eee_lp = ax88179_phy_read_mmd_indirect(dev,
704 MDIO_AN_EEE_LPABLE,
705 MDIO_MMD_AN);
706 if (eee_lp < 0) {
707 priv->eee_active = 0;
708 return false;
709 }
710
711 eee_adv = ax88179_phy_read_mmd_indirect(dev,
712 MDIO_AN_EEE_ADV,
713 MDIO_MMD_AN);
714
715 if (eee_adv < 0) {
716 priv->eee_active = 0;
717 return false;
718 }
719
720 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
721 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
722 supported = (ecmd.speed == SPEED_1000) ?
723 SUPPORTED_1000baseT_Full :
724 SUPPORTED_100baseT_Full;
725
726 if (!(lp & adv & supported)) {
727 priv->eee_active = 0;
728 return false;
729 }
730
731 priv->eee_active = 1;
732 return true;
733 }
734
735 priv->eee_active = 0;
736 return false;
737}
738
739static void ax88179_disable_eee(struct usbnet *dev)
740{
741 u16 tmp16;
742
743 tmp16 = GMII_PHY_PGSEL_PAGE3;
744 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
745 GMII_PHY_PAGE_SELECT, 2, &tmp16);
746
747 tmp16 = 0x3246;
748 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
749 MII_PHYADDR, 2, &tmp16);
750
751 tmp16 = GMII_PHY_PGSEL_PAGE0;
752 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
753 GMII_PHY_PAGE_SELECT, 2, &tmp16);
754}
755
756static void ax88179_enable_eee(struct usbnet *dev)
757{
758 u16 tmp16;
759
760 tmp16 = GMII_PHY_PGSEL_PAGE3;
761 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
762 GMII_PHY_PAGE_SELECT, 2, &tmp16);
763
764 tmp16 = 0x3247;
765 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
766 MII_PHYADDR, 2, &tmp16);
767
768 tmp16 = GMII_PHY_PGSEL_PAGE5;
769 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
770 GMII_PHY_PAGE_SELECT, 2, &tmp16);
771
772 tmp16 = 0x0680;
773 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
774 MII_BMSR, 2, &tmp16);
775
776 tmp16 = GMII_PHY_PGSEL_PAGE0;
777 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
778 GMII_PHY_PAGE_SELECT, 2, &tmp16);
779}
780
781static int ax88179_get_eee(struct net_device *net, struct ethtool_eee *edata)
782{
783 struct usbnet *dev = netdev_priv(net);
784 struct ax88179_data *priv = (struct ax88179_data *)dev->data;
785
786 edata->eee_enabled = priv->eee_enabled;
787 edata->eee_active = priv->eee_active;
788
789 return ax88179_ethtool_get_eee(dev, edata);
790}
791
792static int ax88179_set_eee(struct net_device *net, struct ethtool_eee *edata)
793{
794 struct usbnet *dev = netdev_priv(net);
795 struct ax88179_data *priv = (struct ax88179_data *)dev->data;
796 int ret = -EOPNOTSUPP;
797
798 priv->eee_enabled = edata->eee_enabled;
799 if (!priv->eee_enabled) {
800 ax88179_disable_eee(dev);
801 } else {
802 priv->eee_enabled = ax88179_chk_eee(dev);
803 if (!priv->eee_enabled)
804 return -EOPNOTSUPP;
805
806 ax88179_enable_eee(dev);
807 }
808
809 ret = ax88179_ethtool_set_eee(dev, edata);
810 if (ret)
811 return ret;
812
813 mii_nway_restart(&dev->mii);
814
815 usbnet_link_change(dev, 0, 0);
816
817 return ret;
818}
819
820static int ax88179_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
821{
822 struct usbnet *dev = netdev_priv(net);
823 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
824}
825
826static const struct ethtool_ops ax88179_ethtool_ops = {
827 .get_link = ethtool_op_get_link,
828 .get_msglevel = usbnet_get_msglevel,
829 .set_msglevel = usbnet_set_msglevel,
830 .get_wol = ax88179_get_wol,
831 .set_wol = ax88179_set_wol,
832 .get_eeprom_len = ax88179_get_eeprom_len,
833 .get_eeprom = ax88179_get_eeprom,
834 .get_eee = ax88179_get_eee,
835 .set_eee = ax88179_set_eee,
836 .nway_reset = usbnet_nway_reset,
837 .get_link_ksettings = ax88179_get_link_ksettings,
838 .set_link_ksettings = ax88179_set_link_ksettings,
839};
840
841static void ax88179_set_multicast(struct net_device *net)
842{
843 struct usbnet *dev = netdev_priv(net);
844 struct ax88179_data *data = (struct ax88179_data *)dev->data;
845 u8 *m_filter = ((u8 *)dev->data) + 12;
846
847 data->rxctl = (AX_RX_CTL_START | AX_RX_CTL_AB | AX_RX_CTL_IPE);
848
849 if (net->flags & IFF_PROMISC) {
850 data->rxctl |= AX_RX_CTL_PRO;
851 } else if (net->flags & IFF_ALLMULTI ||
852 netdev_mc_count(net) > AX_MAX_MCAST) {
853 data->rxctl |= AX_RX_CTL_AMALL;
854 } else if (netdev_mc_empty(net)) {
855 /* just broadcast and directed */
856 } else {
857 /* We use the 20 byte dev->data for our 8 byte filter buffer
858 * to avoid allocating memory that is tricky to free later
859 */
860 u32 crc_bits;
861 struct netdev_hw_addr *ha;
862
863 memset(m_filter, 0, AX_MCAST_FLTSIZE);
864
865 netdev_for_each_mc_addr(ha, net) {
866 crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
867 *(m_filter + (crc_bits >> 3)) |= (1 << (crc_bits & 7));
868 }
869
870 ax88179_write_cmd_async(dev, AX_ACCESS_MAC, AX_MULFLTARY,
871 AX_MCAST_FLTSIZE, AX_MCAST_FLTSIZE,
872 m_filter);
873
874 data->rxctl |= AX_RX_CTL_AM;
875 }
876
877 ax88179_write_cmd_async(dev, AX_ACCESS_MAC, AX_RX_CTL,
878 2, 2, &data->rxctl);
879}
880
881static int
882ax88179_set_features(struct net_device *net, netdev_features_t features)
883{
884 u8 tmp;
885 struct usbnet *dev = netdev_priv(net);
886 netdev_features_t changed = net->features ^ features;
887
888 if (changed & NETIF_F_IP_CSUM) {
889 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, &tmp);
890 tmp ^= AX_TXCOE_TCP | AX_TXCOE_UDP;
891 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, &tmp);
892 }
893
894 if (changed & NETIF_F_IPV6_CSUM) {
895 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, &tmp);
896 tmp ^= AX_TXCOE_TCPV6 | AX_TXCOE_UDPV6;
897 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, &tmp);
898 }
899
900 if (changed & NETIF_F_RXCSUM) {
901 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_RXCOE_CTL, 1, 1, &tmp);
902 tmp ^= AX_RXCOE_IP | AX_RXCOE_TCP | AX_RXCOE_UDP |
903 AX_RXCOE_TCPV6 | AX_RXCOE_UDPV6;
904 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RXCOE_CTL, 1, 1, &tmp);
905 }
906
907 return 0;
908}
909
910static int ax88179_change_mtu(struct net_device *net, int new_mtu)
911{
912 struct usbnet *dev = netdev_priv(net);
913 u16 tmp16;
914
915 net->mtu = new_mtu;
916 dev->hard_mtu = net->mtu + net->hard_header_len;
917
918 if (net->mtu > 1500) {
919 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
920 2, 2, &tmp16);
921 tmp16 |= AX_MEDIUM_JUMBO_EN;
922 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
923 2, 2, &tmp16);
924 } else {
925 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
926 2, 2, &tmp16);
927 tmp16 &= ~AX_MEDIUM_JUMBO_EN;
928 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
929 2, 2, &tmp16);
930 }
931
932 /* max qlen depend on hard_mtu and rx_urb_size */
933 usbnet_update_max_qlen(dev);
934
935 return 0;
936}
937
938static int ax88179_set_mac_addr(struct net_device *net, void *p)
939{
940 struct usbnet *dev = netdev_priv(net);
941 struct sockaddr *addr = p;
942 int ret;
943
944 if (netif_running(net))
945 return -EBUSY;
946 if (!is_valid_ether_addr(addr->sa_data))
947 return -EADDRNOTAVAIL;
948
949 memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
950
951 /* Set the MAC address */
952 ret = ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_NODE_ID, ETH_ALEN,
953 ETH_ALEN, net->dev_addr);
954 if (ret < 0)
955 return ret;
956
957 return 0;
958}
959
960static const struct net_device_ops ax88179_netdev_ops = {
961 .ndo_open = usbnet_open,
962 .ndo_stop = usbnet_stop,
963 .ndo_start_xmit = usbnet_start_xmit,
964 .ndo_tx_timeout = usbnet_tx_timeout,
965 .ndo_get_stats64 = usbnet_get_stats64,
966 .ndo_change_mtu = ax88179_change_mtu,
967 .ndo_set_mac_address = ax88179_set_mac_addr,
968 .ndo_validate_addr = eth_validate_addr,
969 .ndo_do_ioctl = ax88179_ioctl,
970 .ndo_set_rx_mode = ax88179_set_multicast,
971 .ndo_set_features = ax88179_set_features,
972};
973
974static int ax88179_check_eeprom(struct usbnet *dev)
975{
976 u8 i, buf, eeprom[20];
977 u16 csum, delay = HZ / 10;
978 unsigned long jtimeout;
979
980 /* Read EEPROM content */
981 for (i = 0; i < 6; i++) {
982 buf = i;
983 if (ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_SROM_ADDR,
984 1, 1, &buf) < 0)
985 return -EINVAL;
986
987 buf = EEP_RD;
988 if (ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_SROM_CMD,
989 1, 1, &buf) < 0)
990 return -EINVAL;
991
992 jtimeout = jiffies + delay;
993 do {
994 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_SROM_CMD,
995 1, 1, &buf);
996
997 if (time_after(jiffies, jtimeout))
998 return -EINVAL;
999
1000 } while (buf & EEP_BUSY);
1001
1002 __ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_SROM_DATA_LOW,
1003 2, 2, &eeprom[i * 2], 0);
1004
1005 if ((i == 0) && (eeprom[0] == 0xFF))
1006 return -EINVAL;
1007 }
1008
1009 csum = eeprom[6] + eeprom[7] + eeprom[8] + eeprom[9];
1010 csum = (csum >> 8) + (csum & 0xff);
1011 if ((csum + eeprom[10]) != 0xff)
1012 return -EINVAL;
1013
1014 return 0;
1015}
1016
1017static int ax88179_check_efuse(struct usbnet *dev, u16 *ledmode)
1018{
1019 u8 i;
1020 u8 efuse[64];
1021 u16 csum = 0;
1022
1023 if (ax88179_read_cmd(dev, AX_ACCESS_EFUS, 0, 64, 64, efuse) < 0)
1024 return -EINVAL;
1025
1026 if (*efuse == 0xFF)
1027 return -EINVAL;
1028
1029 for (i = 0; i < 64; i++)
1030 csum = csum + efuse[i];
1031
1032 while (csum > 255)
1033 csum = (csum & 0x00FF) + ((csum >> 8) & 0x00FF);
1034
1035 if (csum != 0xFF)
1036 return -EINVAL;
1037
1038 *ledmode = (efuse[51] << 8) | efuse[52];
1039
1040 return 0;
1041}
1042
1043static int ax88179_convert_old_led(struct usbnet *dev, u16 *ledvalue)
1044{
1045 u16 led;
1046
1047 /* Loaded the old eFuse LED Mode */
1048 if (ax88179_read_cmd(dev, AX_ACCESS_EEPROM, 0x3C, 1, 2, &led) < 0)
1049 return -EINVAL;
1050
1051 led >>= 8;
1052 switch (led) {
1053 case 0xFF:
1054 led = LED0_ACTIVE | LED1_LINK_10 | LED1_LINK_100 |
1055 LED1_LINK_1000 | LED2_ACTIVE | LED2_LINK_10 |
1056 LED2_LINK_100 | LED2_LINK_1000 | LED_VALID;
1057 break;
1058 case 0xFE:
1059 led = LED0_ACTIVE | LED1_LINK_1000 | LED2_LINK_100 | LED_VALID;
1060 break;
1061 case 0xFD:
1062 led = LED0_ACTIVE | LED1_LINK_1000 | LED2_LINK_100 |
1063 LED2_LINK_10 | LED_VALID;
1064 break;
1065 case 0xFC:
1066 led = LED0_ACTIVE | LED1_ACTIVE | LED1_LINK_1000 | LED2_ACTIVE |
1067 LED2_LINK_100 | LED2_LINK_10 | LED_VALID;
1068 break;
1069 default:
1070 led = LED0_ACTIVE | LED1_LINK_10 | LED1_LINK_100 |
1071 LED1_LINK_1000 | LED2_ACTIVE | LED2_LINK_10 |
1072 LED2_LINK_100 | LED2_LINK_1000 | LED_VALID;
1073 break;
1074 }
1075
1076 *ledvalue = led;
1077
1078 return 0;
1079}
1080
1081static int ax88179_led_setting(struct usbnet *dev)
1082{
1083 u8 ledfd, value = 0;
1084 u16 tmp, ledact, ledlink, ledvalue = 0, delay = HZ / 10;
1085 unsigned long jtimeout;
1086
1087 /* Check AX88179 version. UA1 or UA2*/
1088 ax88179_read_cmd(dev, AX_ACCESS_MAC, GENERAL_STATUS, 1, 1, &value);
1089
1090 if (!(value & AX_SECLD)) { /* UA1 */
1091 value = AX_GPIO_CTRL_GPIO3EN | AX_GPIO_CTRL_GPIO2EN |
1092 AX_GPIO_CTRL_GPIO1EN;
1093 if (ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_GPIO_CTRL,
1094 1, 1, &value) < 0)
1095 return -EINVAL;
1096 }
1097
1098 /* Check EEPROM */
1099 if (!ax88179_check_eeprom(dev)) {
1100 value = 0x42;
1101 if (ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_SROM_ADDR,
1102 1, 1, &value) < 0)
1103 return -EINVAL;
1104
1105 value = EEP_RD;
1106 if (ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_SROM_CMD,
1107 1, 1, &value) < 0)
1108 return -EINVAL;
1109
1110 jtimeout = jiffies + delay;
1111 do {
1112 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_SROM_CMD,
1113 1, 1, &value);
1114
1115 if (time_after(jiffies, jtimeout))
1116 return -EINVAL;
1117
1118 } while (value & EEP_BUSY);
1119
1120 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_SROM_DATA_HIGH,
1121 1, 1, &value);
1122 ledvalue = (value << 8);
1123
1124 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_SROM_DATA_LOW,
1125 1, 1, &value);
1126 ledvalue |= value;
1127
1128 /* load internal ROM for defaule setting */
1129 if ((ledvalue == 0xFFFF) || ((ledvalue & LED_VALID) == 0))
1130 ax88179_convert_old_led(dev, &ledvalue);
1131
1132 } else if (!ax88179_check_efuse(dev, &ledvalue)) {
1133 if ((ledvalue == 0xFFFF) || ((ledvalue & LED_VALID) == 0))
1134 ax88179_convert_old_led(dev, &ledvalue);
1135 } else {
1136 ax88179_convert_old_led(dev, &ledvalue);
1137 }
1138
1139 tmp = GMII_PHY_PGSEL_EXT;
1140 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1141 GMII_PHY_PAGE_SELECT, 2, &tmp);
1142
1143 tmp = 0x2c;
1144 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1145 GMII_PHYPAGE, 2, &tmp);
1146
1147 ax88179_read_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1148 GMII_LED_ACT, 2, &ledact);
1149
1150 ax88179_read_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1151 GMII_LED_LINK, 2, &ledlink);
1152
1153 ledact &= GMII_LED_ACTIVE_MASK;
1154 ledlink &= GMII_LED_LINK_MASK;
1155
1156 if (ledvalue & LED0_ACTIVE)
1157 ledact |= GMII_LED0_ACTIVE;
1158
1159 if (ledvalue & LED1_ACTIVE)
1160 ledact |= GMII_LED1_ACTIVE;
1161
1162 if (ledvalue & LED2_ACTIVE)
1163 ledact |= GMII_LED2_ACTIVE;
1164
1165 if (ledvalue & LED0_LINK_10)
1166 ledlink |= GMII_LED0_LINK_10;
1167
1168 if (ledvalue & LED1_LINK_10)
1169 ledlink |= GMII_LED1_LINK_10;
1170
1171 if (ledvalue & LED2_LINK_10)
1172 ledlink |= GMII_LED2_LINK_10;
1173
1174 if (ledvalue & LED0_LINK_100)
1175 ledlink |= GMII_LED0_LINK_100;
1176
1177 if (ledvalue & LED1_LINK_100)
1178 ledlink |= GMII_LED1_LINK_100;
1179
1180 if (ledvalue & LED2_LINK_100)
1181 ledlink |= GMII_LED2_LINK_100;
1182
1183 if (ledvalue & LED0_LINK_1000)
1184 ledlink |= GMII_LED0_LINK_1000;
1185
1186 if (ledvalue & LED1_LINK_1000)
1187 ledlink |= GMII_LED1_LINK_1000;
1188
1189 if (ledvalue & LED2_LINK_1000)
1190 ledlink |= GMII_LED2_LINK_1000;
1191
1192 tmp = ledact;
1193 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1194 GMII_LED_ACT, 2, &tmp);
1195
1196 tmp = ledlink;
1197 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1198 GMII_LED_LINK, 2, &tmp);
1199
1200 tmp = GMII_PHY_PGSEL_PAGE0;
1201 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1202 GMII_PHY_PAGE_SELECT, 2, &tmp);
1203
1204 /* LED full duplex setting */
1205 ledfd = 0;
1206 if (ledvalue & LED0_FD)
1207 ledfd |= 0x01;
1208 else if ((ledvalue & LED0_USB3_MASK) == 0)
1209 ledfd |= 0x02;
1210
1211 if (ledvalue & LED1_FD)
1212 ledfd |= 0x04;
1213 else if ((ledvalue & LED1_USB3_MASK) == 0)
1214 ledfd |= 0x08;
1215
1216 if (ledvalue & LED2_FD)
1217 ledfd |= 0x10;
1218 else if ((ledvalue & LED2_USB3_MASK) == 0)
1219 ledfd |= 0x20;
1220
1221 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_LEDCTRL, 1, 1, &ledfd);
1222
1223 return 0;
1224}
1225
1226static int ax88179_bind(struct usbnet *dev, struct usb_interface *intf)
1227{
1228 u8 buf[5];
1229 u16 *tmp16;
1230 u8 *tmp;
1231 struct ax88179_data *ax179_data = (struct ax88179_data *)dev->data;
1232 struct ethtool_eee eee_data;
1233
1234 usbnet_get_endpoints(dev, intf);
1235
1236 tmp16 = (u16 *)buf;
1237 tmp = (u8 *)buf;
1238
1239 memset(ax179_data, 0, sizeof(*ax179_data));
1240
1241 /* Power up ethernet PHY */
1242 *tmp16 = 0;
1243 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, tmp16);
1244 *tmp16 = AX_PHYPWR_RSTCTL_IPRL;
1245 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, tmp16);
1246 msleep(200);
1247
1248 *tmp = AX_CLK_SELECT_ACS | AX_CLK_SELECT_BCS;
1249 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, tmp);
1250 msleep(100);
1251
1252 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_NODE_ID, ETH_ALEN,
1253 ETH_ALEN, dev->net->dev_addr);
1254 memcpy(dev->net->perm_addr, dev->net->dev_addr, ETH_ALEN);
1255
1256 /* RX bulk configuration */
1257 memcpy(tmp, &AX88179_BULKIN_SIZE[0], 5);
1258 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_BULKIN_QCTRL, 5, 5, tmp);
1259
1260 dev->rx_urb_size = 1024 * 20;
1261
1262 *tmp = 0x34;
1263 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PAUSE_WATERLVL_LOW, 1, 1, tmp);
1264
1265 *tmp = 0x52;
1266 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PAUSE_WATERLVL_HIGH,
1267 1, 1, tmp);
1268
1269 dev->net->netdev_ops = &ax88179_netdev_ops;
1270 dev->net->ethtool_ops = &ax88179_ethtool_ops;
1271 dev->net->needed_headroom = 8;
1272 dev->net->max_mtu = 4088;
1273
1274 /* Initialize MII structure */
1275 dev->mii.dev = dev->net;
1276 dev->mii.mdio_read = ax88179_mdio_read;
1277 dev->mii.mdio_write = ax88179_mdio_write;
1278 dev->mii.phy_id_mask = 0xff;
1279 dev->mii.reg_num_mask = 0xff;
1280 dev->mii.phy_id = 0x03;
1281 dev->mii.supports_gmii = 1;
1282
1283 dev->net->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1284 NETIF_F_RXCSUM;
1285
1286 dev->net->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1287 NETIF_F_RXCSUM;
1288
1289 /* Enable checksum offload */
1290 *tmp = AX_RXCOE_IP | AX_RXCOE_TCP | AX_RXCOE_UDP |
1291 AX_RXCOE_TCPV6 | AX_RXCOE_UDPV6;
1292 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RXCOE_CTL, 1, 1, tmp);
1293
1294 *tmp = AX_TXCOE_IP | AX_TXCOE_TCP | AX_TXCOE_UDP |
1295 AX_TXCOE_TCPV6 | AX_TXCOE_UDPV6;
1296 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, tmp);
1297
1298 /* Configure RX control register => start operation */
1299 *tmp16 = AX_RX_CTL_DROPCRCERR | AX_RX_CTL_IPE | AX_RX_CTL_START |
1300 AX_RX_CTL_AP | AX_RX_CTL_AMALL | AX_RX_CTL_AB;
1301 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, tmp16);
1302
1303 *tmp = AX_MONITOR_MODE_PMETYPE | AX_MONITOR_MODE_PMEPOL |
1304 AX_MONITOR_MODE_RWMP;
1305 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MONITOR_MOD, 1, 1, tmp);
1306
1307 /* Configure default medium type => giga */
1308 *tmp16 = AX_MEDIUM_RECEIVE_EN | AX_MEDIUM_TXFLOW_CTRLEN |
1309 AX_MEDIUM_RXFLOW_CTRLEN | AX_MEDIUM_FULL_DUPLEX |
1310 AX_MEDIUM_GIGAMODE;
1311 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
1312 2, 2, tmp16);
1313
1314 ax88179_led_setting(dev);
1315
1316 ax179_data->eee_enabled = 0;
1317 ax179_data->eee_active = 0;
1318
1319 ax88179_disable_eee(dev);
1320
1321 ax88179_ethtool_get_eee(dev, &eee_data);
1322 eee_data.advertised = 0;
1323 ax88179_ethtool_set_eee(dev, &eee_data);
1324
1325 /* Restart autoneg */
1326 mii_nway_restart(&dev->mii);
1327
1328 usbnet_link_change(dev, 0, 0);
1329
1330 return 0;
1331}
1332
1333static void ax88179_unbind(struct usbnet *dev, struct usb_interface *intf)
1334{
1335 u16 tmp16;
1336
1337 /* Configure RX control register => stop operation */
1338 tmp16 = AX_RX_CTL_STOP;
1339 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, &tmp16);
1340
1341 tmp16 = 0;
1342 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, &tmp16);
1343
1344 /* Power down ethernet PHY */
1345 tmp16 = 0;
1346 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, &tmp16);
1347}
1348
1349static void
1350ax88179_rx_checksum(struct sk_buff *skb, u32 *pkt_hdr)
1351{
1352 skb->ip_summed = CHECKSUM_NONE;
1353
1354 /* checksum error bit is set */
1355 if ((*pkt_hdr & AX_RXHDR_L3CSUM_ERR) ||
1356 (*pkt_hdr & AX_RXHDR_L4CSUM_ERR))
1357 return;
1358
1359 /* It must be a TCP or UDP packet with a valid checksum */
1360 if (((*pkt_hdr & AX_RXHDR_L4_TYPE_MASK) == AX_RXHDR_L4_TYPE_TCP) ||
1361 ((*pkt_hdr & AX_RXHDR_L4_TYPE_MASK) == AX_RXHDR_L4_TYPE_UDP))
1362 skb->ip_summed = CHECKSUM_UNNECESSARY;
1363}
1364
1365static int ax88179_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
1366{
1367 struct sk_buff *ax_skb;
1368 int pkt_cnt;
1369 u32 rx_hdr;
1370 u16 hdr_off;
1371 u32 *pkt_hdr;
1372
1373 /* This check is no longer done by usbnet */
1374 if (skb->len < dev->net->hard_header_len)
1375 return 0;
1376
1377 skb_trim(skb, skb->len - 4);
1378 memcpy(&rx_hdr, skb_tail_pointer(skb), 4);
1379 le32_to_cpus(&rx_hdr);
1380
1381 pkt_cnt = (u16)rx_hdr;
1382 hdr_off = (u16)(rx_hdr >> 16);
1383 pkt_hdr = (u32 *)(skb->data + hdr_off);
1384
1385 while (pkt_cnt--) {
1386 u16 pkt_len;
1387
1388 le32_to_cpus(pkt_hdr);
1389 pkt_len = (*pkt_hdr >> 16) & 0x1fff;
1390
1391 /* Check CRC or runt packet */
1392 if ((*pkt_hdr & AX_RXHDR_CRC_ERR) ||
1393 (*pkt_hdr & AX_RXHDR_DROP_ERR)) {
1394 skb_pull(skb, (pkt_len + 7) & 0xFFF8);
1395 pkt_hdr++;
1396 continue;
1397 }
1398
1399 if (pkt_cnt == 0) {
1400 /* Skip IP alignment psudo header */
1401 skb_pull(skb, 2);
1402 skb->len = pkt_len;
1403 skb_set_tail_pointer(skb, pkt_len);
1404 skb->truesize = pkt_len + sizeof(struct sk_buff);
1405 ax88179_rx_checksum(skb, pkt_hdr);
1406 return 1;
1407 }
1408
1409 ax_skb = skb_clone(skb, GFP_ATOMIC);
1410 if (ax_skb) {
1411 ax_skb->len = pkt_len;
1412 ax_skb->data = skb->data + 2;
1413 skb_set_tail_pointer(ax_skb, pkt_len);
1414 ax_skb->truesize = pkt_len + sizeof(struct sk_buff);
1415 ax88179_rx_checksum(ax_skb, pkt_hdr);
1416 usbnet_skb_return(dev, ax_skb);
1417 } else {
1418 return 0;
1419 }
1420
1421 skb_pull(skb, (pkt_len + 7) & 0xFFF8);
1422 pkt_hdr++;
1423 }
1424 return 1;
1425}
1426
1427static struct sk_buff *
1428ax88179_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
1429{
1430 u32 tx_hdr1, tx_hdr2;
1431 int frame_size = dev->maxpacket;
1432 int mss = skb_shinfo(skb)->gso_size;
1433 int headroom;
1434
1435 tx_hdr1 = skb->len;
1436 tx_hdr2 = mss;
1437 if (((skb->len + 8) % frame_size) == 0)
1438 tx_hdr2 |= 0x80008000; /* Enable padding */
1439
1440 headroom = skb_headroom(skb) - 8;
1441
1442 if ((skb_header_cloned(skb) || headroom < 0) &&
1443 pskb_expand_head(skb, headroom < 0 ? 8 : 0, 0, GFP_ATOMIC)) {
1444 dev_kfree_skb_any(skb);
1445 return NULL;
1446 }
1447
1448 skb_push(skb, 4);
1449 cpu_to_le32s(&tx_hdr2);
1450 skb_copy_to_linear_data(skb, &tx_hdr2, 4);
1451
1452 skb_push(skb, 4);
1453 cpu_to_le32s(&tx_hdr1);
1454 skb_copy_to_linear_data(skb, &tx_hdr1, 4);
1455
1456 return skb;
1457}
1458
1459static int ax88179_link_reset(struct usbnet *dev)
1460{
1461 struct ax88179_data *ax179_data = (struct ax88179_data *)dev->data;
1462 u8 tmp[5], link_sts;
1463 u16 mode, tmp16, delay = HZ / 10;
1464 u32 tmp32 = 0x40000000;
1465 unsigned long jtimeout;
1466
1467 jtimeout = jiffies + delay;
1468 while (tmp32 & 0x40000000) {
1469 mode = 0;
1470 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, &mode);
1471 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2,
1472 &ax179_data->rxctl);
1473
1474 /*link up, check the usb device control TX FIFO full or empty*/
1475 ax88179_read_cmd(dev, 0x81, 0x8c, 0, 4, &tmp32);
1476
1477 if (time_after(jiffies, jtimeout))
1478 return 0;
1479 }
1480
1481 mode = AX_MEDIUM_RECEIVE_EN | AX_MEDIUM_TXFLOW_CTRLEN |
1482 AX_MEDIUM_RXFLOW_CTRLEN;
1483
1484 ax88179_read_cmd(dev, AX_ACCESS_MAC, PHYSICAL_LINK_STATUS,
1485 1, 1, &link_sts);
1486
1487 ax88179_read_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1488 GMII_PHY_PHYSR, 2, &tmp16);
1489
1490 if (!(tmp16 & GMII_PHY_PHYSR_LINK)) {
1491 return 0;
1492 } else if (GMII_PHY_PHYSR_GIGA == (tmp16 & GMII_PHY_PHYSR_SMASK)) {
1493 mode |= AX_MEDIUM_GIGAMODE | AX_MEDIUM_EN_125MHZ;
1494 if (dev->net->mtu > 1500)
1495 mode |= AX_MEDIUM_JUMBO_EN;
1496
1497 if (link_sts & AX_USB_SS)
1498 memcpy(tmp, &AX88179_BULKIN_SIZE[0], 5);
1499 else if (link_sts & AX_USB_HS)
1500 memcpy(tmp, &AX88179_BULKIN_SIZE[1], 5);
1501 else
1502 memcpy(tmp, &AX88179_BULKIN_SIZE[3], 5);
1503 } else if (GMII_PHY_PHYSR_100 == (tmp16 & GMII_PHY_PHYSR_SMASK)) {
1504 mode |= AX_MEDIUM_PS;
1505
1506 if (link_sts & (AX_USB_SS | AX_USB_HS))
1507 memcpy(tmp, &AX88179_BULKIN_SIZE[2], 5);
1508 else
1509 memcpy(tmp, &AX88179_BULKIN_SIZE[3], 5);
1510 } else {
1511 memcpy(tmp, &AX88179_BULKIN_SIZE[3], 5);
1512 }
1513
1514 /* RX bulk configuration */
1515 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_BULKIN_QCTRL, 5, 5, tmp);
1516
1517 dev->rx_urb_size = (1024 * (tmp[3] + 2));
1518
1519 if (tmp16 & GMII_PHY_PHYSR_FULL)
1520 mode |= AX_MEDIUM_FULL_DUPLEX;
1521 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
1522 2, 2, &mode);
1523
1524 ax179_data->eee_enabled = ax88179_chk_eee(dev);
1525
1526 netif_carrier_on(dev->net);
1527
1528 return 0;
1529}
1530
1531static int ax88179_reset(struct usbnet *dev)
1532{
1533 u8 buf[5];
1534 u16 *tmp16;
1535 u8 *tmp;
1536 struct ax88179_data *ax179_data = (struct ax88179_data *)dev->data;
1537 struct ethtool_eee eee_data;
1538
1539 tmp16 = (u16 *)buf;
1540 tmp = (u8 *)buf;
1541
1542 /* Power up ethernet PHY */
1543 *tmp16 = 0;
1544 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, tmp16);
1545
1546 *tmp16 = AX_PHYPWR_RSTCTL_IPRL;
1547 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, tmp16);
1548 msleep(200);
1549
1550 *tmp = AX_CLK_SELECT_ACS | AX_CLK_SELECT_BCS;
1551 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, tmp);
1552 msleep(100);
1553
1554 /* Ethernet PHY Auto Detach*/
1555 ax88179_auto_detach(dev, 0);
1556
1557 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_NODE_ID, ETH_ALEN, ETH_ALEN,
1558 dev->net->dev_addr);
1559
1560 /* RX bulk configuration */
1561 memcpy(tmp, &AX88179_BULKIN_SIZE[0], 5);
1562 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_BULKIN_QCTRL, 5, 5, tmp);
1563
1564 dev->rx_urb_size = 1024 * 20;
1565
1566 *tmp = 0x34;
1567 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PAUSE_WATERLVL_LOW, 1, 1, tmp);
1568
1569 *tmp = 0x52;
1570 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PAUSE_WATERLVL_HIGH,
1571 1, 1, tmp);
1572
1573 dev->net->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1574 NETIF_F_RXCSUM;
1575
1576 dev->net->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1577 NETIF_F_RXCSUM;
1578
1579 /* Enable checksum offload */
1580 *tmp = AX_RXCOE_IP | AX_RXCOE_TCP | AX_RXCOE_UDP |
1581 AX_RXCOE_TCPV6 | AX_RXCOE_UDPV6;
1582 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RXCOE_CTL, 1, 1, tmp);
1583
1584 *tmp = AX_TXCOE_IP | AX_TXCOE_TCP | AX_TXCOE_UDP |
1585 AX_TXCOE_TCPV6 | AX_TXCOE_UDPV6;
1586 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, tmp);
1587
1588 /* Configure RX control register => start operation */
1589 *tmp16 = AX_RX_CTL_DROPCRCERR | AX_RX_CTL_IPE | AX_RX_CTL_START |
1590 AX_RX_CTL_AP | AX_RX_CTL_AMALL | AX_RX_CTL_AB;
1591 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, tmp16);
1592
1593 *tmp = AX_MONITOR_MODE_PMETYPE | AX_MONITOR_MODE_PMEPOL |
1594 AX_MONITOR_MODE_RWMP;
1595 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MONITOR_MOD, 1, 1, tmp);
1596
1597 /* Configure default medium type => giga */
1598 *tmp16 = AX_MEDIUM_RECEIVE_EN | AX_MEDIUM_TXFLOW_CTRLEN |
1599 AX_MEDIUM_RXFLOW_CTRLEN | AX_MEDIUM_FULL_DUPLEX |
1600 AX_MEDIUM_GIGAMODE;
1601 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
1602 2, 2, tmp16);
1603
1604 ax88179_led_setting(dev);
1605
1606 ax179_data->eee_enabled = 0;
1607 ax179_data->eee_active = 0;
1608
1609 ax88179_disable_eee(dev);
1610
1611 ax88179_ethtool_get_eee(dev, &eee_data);
1612 eee_data.advertised = 0;
1613 ax88179_ethtool_set_eee(dev, &eee_data);
1614
1615 /* Restart autoneg */
1616 mii_nway_restart(&dev->mii);
1617
1618 usbnet_link_change(dev, 0, 0);
1619
1620 return 0;
1621}
1622
1623static int ax88179_stop(struct usbnet *dev)
1624{
1625 u16 tmp16;
1626
1627 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
1628 2, 2, &tmp16);
1629 tmp16 &= ~AX_MEDIUM_RECEIVE_EN;
1630 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
1631 2, 2, &tmp16);
1632
1633 return 0;
1634}
1635
1636static const struct driver_info ax88179_info = {
1637 .description = "ASIX AX88179 USB 3.0 Gigabit Ethernet",
1638 .bind = ax88179_bind,
1639 .unbind = ax88179_unbind,
1640 .status = ax88179_status,
1641 .link_reset = ax88179_link_reset,
1642 .reset = ax88179_reset,
1643 .stop = ax88179_stop,
1644 .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1645 .rx_fixup = ax88179_rx_fixup,
1646 .tx_fixup = ax88179_tx_fixup,
1647};
1648
1649static const struct driver_info ax88178a_info = {
1650 .description = "ASIX AX88178A USB 2.0 Gigabit Ethernet",
1651 .bind = ax88179_bind,
1652 .unbind = ax88179_unbind,
1653 .status = ax88179_status,
1654 .link_reset = ax88179_link_reset,
1655 .reset = ax88179_reset,
1656 .stop = ax88179_stop,
1657 .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1658 .rx_fixup = ax88179_rx_fixup,
1659 .tx_fixup = ax88179_tx_fixup,
1660};
1661
1662static const struct driver_info cypress_GX3_info = {
1663 .description = "Cypress GX3 SuperSpeed to Gigabit Ethernet Controller",
1664 .bind = ax88179_bind,
1665 .unbind = ax88179_unbind,
1666 .status = ax88179_status,
1667 .link_reset = ax88179_link_reset,
1668 .reset = ax88179_reset,
1669 .stop = ax88179_stop,
1670 .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1671 .rx_fixup = ax88179_rx_fixup,
1672 .tx_fixup = ax88179_tx_fixup,
1673};
1674
1675static const struct driver_info dlink_dub1312_info = {
1676 .description = "D-Link DUB-1312 USB 3.0 to Gigabit Ethernet Adapter",
1677 .bind = ax88179_bind,
1678 .unbind = ax88179_unbind,
1679 .status = ax88179_status,
1680 .link_reset = ax88179_link_reset,
1681 .reset = ax88179_reset,
1682 .stop = ax88179_stop,
1683 .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1684 .rx_fixup = ax88179_rx_fixup,
1685 .tx_fixup = ax88179_tx_fixup,
1686};
1687
1688static const struct driver_info sitecom_info = {
1689 .description = "Sitecom USB 3.0 to Gigabit Adapter",
1690 .bind = ax88179_bind,
1691 .unbind = ax88179_unbind,
1692 .status = ax88179_status,
1693 .link_reset = ax88179_link_reset,
1694 .reset = ax88179_reset,
1695 .stop = ax88179_stop,
1696 .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1697 .rx_fixup = ax88179_rx_fixup,
1698 .tx_fixup = ax88179_tx_fixup,
1699};
1700
1701static const struct driver_info samsung_info = {
1702 .description = "Samsung USB Ethernet Adapter",
1703 .bind = ax88179_bind,
1704 .unbind = ax88179_unbind,
1705 .status = ax88179_status,
1706 .link_reset = ax88179_link_reset,
1707 .reset = ax88179_reset,
1708 .stop = ax88179_stop,
1709 .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1710 .rx_fixup = ax88179_rx_fixup,
1711 .tx_fixup = ax88179_tx_fixup,
1712};
1713
1714static const struct driver_info lenovo_info = {
1715 .description = "Lenovo OneLinkDock Gigabit LAN",
1716 .bind = ax88179_bind,
1717 .unbind = ax88179_unbind,
1718 .status = ax88179_status,
1719 .link_reset = ax88179_link_reset,
1720 .reset = ax88179_reset,
1721 .stop = ax88179_stop,
1722 .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1723 .rx_fixup = ax88179_rx_fixup,
1724 .tx_fixup = ax88179_tx_fixup,
1725};
1726
1727static const struct driver_info belkin_info = {
1728 .description = "Belkin USB Ethernet Adapter",
1729 .bind = ax88179_bind,
1730 .unbind = ax88179_unbind,
1731 .status = ax88179_status,
1732 .link_reset = ax88179_link_reset,
1733 .reset = ax88179_reset,
1734 .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1735 .rx_fixup = ax88179_rx_fixup,
1736 .tx_fixup = ax88179_tx_fixup,
1737};
1738
1739static const struct usb_device_id products[] = {
1740{
1741 /* ASIX AX88179 10/100/1000 */
1742 USB_DEVICE(0x0b95, 0x1790),
1743 .driver_info = (unsigned long)&ax88179_info,
1744}, {
1745 /* ASIX AX88178A 10/100/1000 */
1746 USB_DEVICE(0x0b95, 0x178a),
1747 .driver_info = (unsigned long)&ax88178a_info,
1748}, {
1749 /* Cypress GX3 SuperSpeed to Gigabit Ethernet Bridge Controller */
1750 USB_DEVICE(0x04b4, 0x3610),
1751 .driver_info = (unsigned long)&cypress_GX3_info,
1752}, {
1753 /* D-Link DUB-1312 USB 3.0 to Gigabit Ethernet Adapter */
1754 USB_DEVICE(0x2001, 0x4a00),
1755 .driver_info = (unsigned long)&dlink_dub1312_info,
1756}, {
1757 /* Sitecom USB 3.0 to Gigabit Adapter */
1758 USB_DEVICE(0x0df6, 0x0072),
1759 .driver_info = (unsigned long)&sitecom_info,
1760}, {
1761 /* Samsung USB Ethernet Adapter */
1762 USB_DEVICE(0x04e8, 0xa100),
1763 .driver_info = (unsigned long)&samsung_info,
1764}, {
1765 /* Lenovo OneLinkDock Gigabit LAN */
1766 USB_DEVICE(0x17ef, 0x304b),
1767 .driver_info = (unsigned long)&lenovo_info,
1768}, {
1769 /* Belkin B2B128 USB 3.0 Hub + Gigabit Ethernet Adapter */
1770 USB_DEVICE(0x050d, 0x0128),
1771 .driver_info = (unsigned long)&belkin_info,
1772},
1773 { },
1774};
1775MODULE_DEVICE_TABLE(usb, products);
1776
1777static struct usb_driver ax88179_178a_driver = {
1778 .name = "ax88179_178a",
1779 .id_table = products,
1780 .probe = usbnet_probe,
1781 .suspend = ax88179_suspend,
1782 .resume = ax88179_resume,
1783 .reset_resume = ax88179_resume,
1784 .disconnect = usbnet_disconnect,
1785 .supports_autosuspend = 1,
1786 .disable_hub_initiated_lpm = 1,
1787};
1788
1789module_usb_driver(ax88179_178a_driver);
1790
1791MODULE_DESCRIPTION("ASIX AX88179/178A based USB 3.0/2.0 Gigabit Ethernet Devices");
1792MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * ASIX AX88179/178A USB 3.0/2.0 to Gigabit Ethernet Devices
4 *
5 * Copyright (C) 2011-2013 ASIX
6 */
7
8#include <linux/module.h>
9#include <linux/etherdevice.h>
10#include <linux/mii.h>
11#include <linux/usb.h>
12#include <linux/crc32.h>
13#include <linux/usb/usbnet.h>
14#include <uapi/linux/mdio.h>
15#include <linux/mdio.h>
16
17#define AX88179_PHY_ID 0x03
18#define AX_EEPROM_LEN 0x100
19#define AX88179_EEPROM_MAGIC 0x17900b95
20#define AX_MCAST_FLTSIZE 8
21#define AX_MAX_MCAST 64
22#define AX_INT_PPLS_LINK ((u32)BIT(16))
23#define AX_RXHDR_L4_TYPE_MASK 0x1c
24#define AX_RXHDR_L4_TYPE_UDP 4
25#define AX_RXHDR_L4_TYPE_TCP 16
26#define AX_RXHDR_L3CSUM_ERR 2
27#define AX_RXHDR_L4CSUM_ERR 1
28#define AX_RXHDR_CRC_ERR ((u32)BIT(29))
29#define AX_RXHDR_DROP_ERR ((u32)BIT(31))
30#define AX_ACCESS_MAC 0x01
31#define AX_ACCESS_PHY 0x02
32#define AX_ACCESS_EEPROM 0x04
33#define AX_ACCESS_EFUS 0x05
34#define AX_RELOAD_EEPROM_EFUSE 0x06
35#define AX_PAUSE_WATERLVL_HIGH 0x54
36#define AX_PAUSE_WATERLVL_LOW 0x55
37
38#define PHYSICAL_LINK_STATUS 0x02
39 #define AX_USB_SS 0x04
40 #define AX_USB_HS 0x02
41
42#define GENERAL_STATUS 0x03
43/* Check AX88179 version. UA1:Bit2 = 0, UA2:Bit2 = 1 */
44 #define AX_SECLD 0x04
45
46#define AX_SROM_ADDR 0x07
47#define AX_SROM_CMD 0x0a
48 #define EEP_RD 0x04
49 #define EEP_BUSY 0x10
50
51#define AX_SROM_DATA_LOW 0x08
52#define AX_SROM_DATA_HIGH 0x09
53
54#define AX_RX_CTL 0x0b
55 #define AX_RX_CTL_DROPCRCERR 0x0100
56 #define AX_RX_CTL_IPE 0x0200
57 #define AX_RX_CTL_START 0x0080
58 #define AX_RX_CTL_AP 0x0020
59 #define AX_RX_CTL_AM 0x0010
60 #define AX_RX_CTL_AB 0x0008
61 #define AX_RX_CTL_AMALL 0x0002
62 #define AX_RX_CTL_PRO 0x0001
63 #define AX_RX_CTL_STOP 0x0000
64
65#define AX_NODE_ID 0x10
66#define AX_MULFLTARY 0x16
67
68#define AX_MEDIUM_STATUS_MODE 0x22
69 #define AX_MEDIUM_GIGAMODE 0x01
70 #define AX_MEDIUM_FULL_DUPLEX 0x02
71 #define AX_MEDIUM_EN_125MHZ 0x08
72 #define AX_MEDIUM_RXFLOW_CTRLEN 0x10
73 #define AX_MEDIUM_TXFLOW_CTRLEN 0x20
74 #define AX_MEDIUM_RECEIVE_EN 0x100
75 #define AX_MEDIUM_PS 0x200
76 #define AX_MEDIUM_JUMBO_EN 0x8040
77
78#define AX_MONITOR_MOD 0x24
79 #define AX_MONITOR_MODE_RWLC 0x02
80 #define AX_MONITOR_MODE_RWMP 0x04
81 #define AX_MONITOR_MODE_PMEPOL 0x20
82 #define AX_MONITOR_MODE_PMETYPE 0x40
83
84#define AX_GPIO_CTRL 0x25
85 #define AX_GPIO_CTRL_GPIO3EN 0x80
86 #define AX_GPIO_CTRL_GPIO2EN 0x40
87 #define AX_GPIO_CTRL_GPIO1EN 0x20
88
89#define AX_PHYPWR_RSTCTL 0x26
90 #define AX_PHYPWR_RSTCTL_BZ 0x0010
91 #define AX_PHYPWR_RSTCTL_IPRL 0x0020
92 #define AX_PHYPWR_RSTCTL_AT 0x1000
93
94#define AX_RX_BULKIN_QCTRL 0x2e
95#define AX_CLK_SELECT 0x33
96 #define AX_CLK_SELECT_BCS 0x01
97 #define AX_CLK_SELECT_ACS 0x02
98 #define AX_CLK_SELECT_ULR 0x08
99
100#define AX_RXCOE_CTL 0x34
101 #define AX_RXCOE_IP 0x01
102 #define AX_RXCOE_TCP 0x02
103 #define AX_RXCOE_UDP 0x04
104 #define AX_RXCOE_TCPV6 0x20
105 #define AX_RXCOE_UDPV6 0x40
106
107#define AX_TXCOE_CTL 0x35
108 #define AX_TXCOE_IP 0x01
109 #define AX_TXCOE_TCP 0x02
110 #define AX_TXCOE_UDP 0x04
111 #define AX_TXCOE_TCPV6 0x20
112 #define AX_TXCOE_UDPV6 0x40
113
114#define AX_LEDCTRL 0x73
115
116#define GMII_PHY_PHYSR 0x11
117 #define GMII_PHY_PHYSR_SMASK 0xc000
118 #define GMII_PHY_PHYSR_GIGA 0x8000
119 #define GMII_PHY_PHYSR_100 0x4000
120 #define GMII_PHY_PHYSR_FULL 0x2000
121 #define GMII_PHY_PHYSR_LINK 0x400
122
123#define GMII_LED_ACT 0x1a
124 #define GMII_LED_ACTIVE_MASK 0xff8f
125 #define GMII_LED0_ACTIVE BIT(4)
126 #define GMII_LED1_ACTIVE BIT(5)
127 #define GMII_LED2_ACTIVE BIT(6)
128
129#define GMII_LED_LINK 0x1c
130 #define GMII_LED_LINK_MASK 0xf888
131 #define GMII_LED0_LINK_10 BIT(0)
132 #define GMII_LED0_LINK_100 BIT(1)
133 #define GMII_LED0_LINK_1000 BIT(2)
134 #define GMII_LED1_LINK_10 BIT(4)
135 #define GMII_LED1_LINK_100 BIT(5)
136 #define GMII_LED1_LINK_1000 BIT(6)
137 #define GMII_LED2_LINK_10 BIT(8)
138 #define GMII_LED2_LINK_100 BIT(9)
139 #define GMII_LED2_LINK_1000 BIT(10)
140 #define LED0_ACTIVE BIT(0)
141 #define LED0_LINK_10 BIT(1)
142 #define LED0_LINK_100 BIT(2)
143 #define LED0_LINK_1000 BIT(3)
144 #define LED0_FD BIT(4)
145 #define LED0_USB3_MASK 0x001f
146 #define LED1_ACTIVE BIT(5)
147 #define LED1_LINK_10 BIT(6)
148 #define LED1_LINK_100 BIT(7)
149 #define LED1_LINK_1000 BIT(8)
150 #define LED1_FD BIT(9)
151 #define LED1_USB3_MASK 0x03e0
152 #define LED2_ACTIVE BIT(10)
153 #define LED2_LINK_1000 BIT(13)
154 #define LED2_LINK_100 BIT(12)
155 #define LED2_LINK_10 BIT(11)
156 #define LED2_FD BIT(14)
157 #define LED_VALID BIT(15)
158 #define LED2_USB3_MASK 0x7c00
159
160#define GMII_PHYPAGE 0x1e
161#define GMII_PHY_PAGE_SELECT 0x1f
162 #define GMII_PHY_PGSEL_EXT 0x0007
163 #define GMII_PHY_PGSEL_PAGE0 0x0000
164 #define GMII_PHY_PGSEL_PAGE3 0x0003
165 #define GMII_PHY_PGSEL_PAGE5 0x0005
166
167struct ax88179_data {
168 u8 eee_enabled;
169 u8 eee_active;
170 u16 rxctl;
171 u16 reserved;
172};
173
174struct ax88179_int_data {
175 __le32 intdata1;
176 __le32 intdata2;
177};
178
179static const struct {
180 unsigned char ctrl, timer_l, timer_h, size, ifg;
181} AX88179_BULKIN_SIZE[] = {
182 {7, 0x4f, 0, 0x12, 0xff},
183 {7, 0x20, 3, 0x16, 0xff},
184 {7, 0xae, 7, 0x18, 0xff},
185 {7, 0xcc, 0x4c, 0x18, 8},
186};
187
188static int __ax88179_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
189 u16 size, void *data, int in_pm)
190{
191 int ret;
192 int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
193
194 BUG_ON(!dev);
195
196 if (!in_pm)
197 fn = usbnet_read_cmd;
198 else
199 fn = usbnet_read_cmd_nopm;
200
201 ret = fn(dev, cmd, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
202 value, index, data, size);
203
204 if (unlikely(ret < 0))
205 netdev_warn(dev->net, "Failed to read reg index 0x%04x: %d\n",
206 index, ret);
207
208 return ret;
209}
210
211static int __ax88179_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
212 u16 size, void *data, int in_pm)
213{
214 int ret;
215 int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
216
217 BUG_ON(!dev);
218
219 if (!in_pm)
220 fn = usbnet_write_cmd;
221 else
222 fn = usbnet_write_cmd_nopm;
223
224 ret = fn(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
225 value, index, data, size);
226
227 if (unlikely(ret < 0))
228 netdev_warn(dev->net, "Failed to write reg index 0x%04x: %d\n",
229 index, ret);
230
231 return ret;
232}
233
234static void ax88179_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value,
235 u16 index, u16 size, void *data)
236{
237 u16 buf;
238
239 if (2 == size) {
240 buf = *((u16 *)data);
241 cpu_to_le16s(&buf);
242 usbnet_write_cmd_async(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR |
243 USB_RECIP_DEVICE, value, index, &buf,
244 size);
245 } else {
246 usbnet_write_cmd_async(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR |
247 USB_RECIP_DEVICE, value, index, data,
248 size);
249 }
250}
251
252static int ax88179_read_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value,
253 u16 index, u16 size, void *data)
254{
255 int ret;
256
257 if (2 == size) {
258 u16 buf;
259 ret = __ax88179_read_cmd(dev, cmd, value, index, size, &buf, 1);
260 le16_to_cpus(&buf);
261 *((u16 *)data) = buf;
262 } else if (4 == size) {
263 u32 buf;
264 ret = __ax88179_read_cmd(dev, cmd, value, index, size, &buf, 1);
265 le32_to_cpus(&buf);
266 *((u32 *)data) = buf;
267 } else {
268 ret = __ax88179_read_cmd(dev, cmd, value, index, size, data, 1);
269 }
270
271 return ret;
272}
273
274static int ax88179_write_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value,
275 u16 index, u16 size, void *data)
276{
277 int ret;
278
279 if (2 == size) {
280 u16 buf;
281 buf = *((u16 *)data);
282 cpu_to_le16s(&buf);
283 ret = __ax88179_write_cmd(dev, cmd, value, index,
284 size, &buf, 1);
285 } else {
286 ret = __ax88179_write_cmd(dev, cmd, value, index,
287 size, data, 1);
288 }
289
290 return ret;
291}
292
293static int ax88179_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
294 u16 size, void *data)
295{
296 int ret;
297
298 if (2 == size) {
299 u16 buf = 0;
300 ret = __ax88179_read_cmd(dev, cmd, value, index, size, &buf, 0);
301 le16_to_cpus(&buf);
302 *((u16 *)data) = buf;
303 } else if (4 == size) {
304 u32 buf = 0;
305 ret = __ax88179_read_cmd(dev, cmd, value, index, size, &buf, 0);
306 le32_to_cpus(&buf);
307 *((u32 *)data) = buf;
308 } else {
309 ret = __ax88179_read_cmd(dev, cmd, value, index, size, data, 0);
310 }
311
312 return ret;
313}
314
315static int ax88179_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
316 u16 size, void *data)
317{
318 int ret;
319
320 if (2 == size) {
321 u16 buf;
322 buf = *((u16 *)data);
323 cpu_to_le16s(&buf);
324 ret = __ax88179_write_cmd(dev, cmd, value, index,
325 size, &buf, 0);
326 } else {
327 ret = __ax88179_write_cmd(dev, cmd, value, index,
328 size, data, 0);
329 }
330
331 return ret;
332}
333
334static void ax88179_status(struct usbnet *dev, struct urb *urb)
335{
336 struct ax88179_int_data *event;
337 u32 link;
338
339 if (urb->actual_length < 8)
340 return;
341
342 event = urb->transfer_buffer;
343 le32_to_cpus((void *)&event->intdata1);
344
345 link = (((__force u32)event->intdata1) & AX_INT_PPLS_LINK) >> 16;
346
347 if (netif_carrier_ok(dev->net) != link) {
348 usbnet_link_change(dev, link, 1);
349 netdev_info(dev->net, "ax88179 - Link status is: %d\n", link);
350 }
351}
352
353static int ax88179_mdio_read(struct net_device *netdev, int phy_id, int loc)
354{
355 struct usbnet *dev = netdev_priv(netdev);
356 u16 res;
357
358 ax88179_read_cmd(dev, AX_ACCESS_PHY, phy_id, (__u16)loc, 2, &res);
359 return res;
360}
361
362static void ax88179_mdio_write(struct net_device *netdev, int phy_id, int loc,
363 int val)
364{
365 struct usbnet *dev = netdev_priv(netdev);
366 u16 res = (u16) val;
367
368 ax88179_write_cmd(dev, AX_ACCESS_PHY, phy_id, (__u16)loc, 2, &res);
369}
370
371static inline int ax88179_phy_mmd_indirect(struct usbnet *dev, u16 prtad,
372 u16 devad)
373{
374 u16 tmp16;
375 int ret;
376
377 tmp16 = devad;
378 ret = ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
379 MII_MMD_CTRL, 2, &tmp16);
380
381 tmp16 = prtad;
382 ret = ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
383 MII_MMD_DATA, 2, &tmp16);
384
385 tmp16 = devad | MII_MMD_CTRL_NOINCR;
386 ret = ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
387 MII_MMD_CTRL, 2, &tmp16);
388
389 return ret;
390}
391
392static int
393ax88179_phy_read_mmd_indirect(struct usbnet *dev, u16 prtad, u16 devad)
394{
395 int ret;
396 u16 tmp16;
397
398 ax88179_phy_mmd_indirect(dev, prtad, devad);
399
400 ret = ax88179_read_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
401 MII_MMD_DATA, 2, &tmp16);
402 if (ret < 0)
403 return ret;
404
405 return tmp16;
406}
407
408static int
409ax88179_phy_write_mmd_indirect(struct usbnet *dev, u16 prtad, u16 devad,
410 u16 data)
411{
412 int ret;
413
414 ax88179_phy_mmd_indirect(dev, prtad, devad);
415
416 ret = ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
417 MII_MMD_DATA, 2, &data);
418
419 if (ret < 0)
420 return ret;
421
422 return 0;
423}
424
425static int ax88179_suspend(struct usb_interface *intf, pm_message_t message)
426{
427 struct usbnet *dev = usb_get_intfdata(intf);
428 u16 tmp16;
429 u8 tmp8;
430
431 usbnet_suspend(intf, message);
432
433 /* Disable RX path */
434 ax88179_read_cmd_nopm(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
435 2, 2, &tmp16);
436 tmp16 &= ~AX_MEDIUM_RECEIVE_EN;
437 ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
438 2, 2, &tmp16);
439
440 /* Force bulk-in zero length */
441 ax88179_read_cmd_nopm(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL,
442 2, 2, &tmp16);
443
444 tmp16 |= AX_PHYPWR_RSTCTL_BZ | AX_PHYPWR_RSTCTL_IPRL;
445 ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL,
446 2, 2, &tmp16);
447
448 /* change clock */
449 tmp8 = 0;
450 ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, &tmp8);
451
452 /* Configure RX control register => stop operation */
453 tmp16 = AX_RX_CTL_STOP;
454 ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, &tmp16);
455
456 return 0;
457}
458
459/* This function is used to enable the autodetach function. */
460/* This function is determined by offset 0x43 of EEPROM */
461static int ax88179_auto_detach(struct usbnet *dev, int in_pm)
462{
463 u16 tmp16;
464 u8 tmp8;
465 int (*fnr)(struct usbnet *, u8, u16, u16, u16, void *);
466 int (*fnw)(struct usbnet *, u8, u16, u16, u16, void *);
467
468 if (!in_pm) {
469 fnr = ax88179_read_cmd;
470 fnw = ax88179_write_cmd;
471 } else {
472 fnr = ax88179_read_cmd_nopm;
473 fnw = ax88179_write_cmd_nopm;
474 }
475
476 if (fnr(dev, AX_ACCESS_EEPROM, 0x43, 1, 2, &tmp16) < 0)
477 return 0;
478
479 if ((tmp16 == 0xFFFF) || (!(tmp16 & 0x0100)))
480 return 0;
481
482 /* Enable Auto Detach bit */
483 tmp8 = 0;
484 fnr(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, &tmp8);
485 tmp8 |= AX_CLK_SELECT_ULR;
486 fnw(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, &tmp8);
487
488 fnr(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, &tmp16);
489 tmp16 |= AX_PHYPWR_RSTCTL_AT;
490 fnw(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, &tmp16);
491
492 return 0;
493}
494
495static int ax88179_resume(struct usb_interface *intf)
496{
497 struct usbnet *dev = usb_get_intfdata(intf);
498 u16 tmp16;
499 u8 tmp8;
500
501 usbnet_link_change(dev, 0, 0);
502
503 /* Power up ethernet PHY */
504 tmp16 = 0;
505 ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL,
506 2, 2, &tmp16);
507 udelay(1000);
508
509 tmp16 = AX_PHYPWR_RSTCTL_IPRL;
510 ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL,
511 2, 2, &tmp16);
512 msleep(200);
513
514 /* Ethernet PHY Auto Detach*/
515 ax88179_auto_detach(dev, 1);
516
517 /* Enable clock */
518 ax88179_read_cmd_nopm(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, &tmp8);
519 tmp8 |= AX_CLK_SELECT_ACS | AX_CLK_SELECT_BCS;
520 ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, &tmp8);
521 msleep(100);
522
523 /* Configure RX control register => start operation */
524 tmp16 = AX_RX_CTL_DROPCRCERR | AX_RX_CTL_IPE | AX_RX_CTL_START |
525 AX_RX_CTL_AP | AX_RX_CTL_AMALL | AX_RX_CTL_AB;
526 ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, &tmp16);
527
528 return usbnet_resume(intf);
529}
530
531static void
532ax88179_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
533{
534 struct usbnet *dev = netdev_priv(net);
535 u8 opt;
536
537 if (ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_MONITOR_MOD,
538 1, 1, &opt) < 0) {
539 wolinfo->supported = 0;
540 wolinfo->wolopts = 0;
541 return;
542 }
543
544 wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
545 wolinfo->wolopts = 0;
546 if (opt & AX_MONITOR_MODE_RWLC)
547 wolinfo->wolopts |= WAKE_PHY;
548 if (opt & AX_MONITOR_MODE_RWMP)
549 wolinfo->wolopts |= WAKE_MAGIC;
550}
551
552static int
553ax88179_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
554{
555 struct usbnet *dev = netdev_priv(net);
556 u8 opt = 0;
557
558 if (wolinfo->wolopts & ~(WAKE_PHY | WAKE_MAGIC))
559 return -EINVAL;
560
561 if (wolinfo->wolopts & WAKE_PHY)
562 opt |= AX_MONITOR_MODE_RWLC;
563 if (wolinfo->wolopts & WAKE_MAGIC)
564 opt |= AX_MONITOR_MODE_RWMP;
565
566 if (ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MONITOR_MOD,
567 1, 1, &opt) < 0)
568 return -EINVAL;
569
570 return 0;
571}
572
573static int ax88179_get_eeprom_len(struct net_device *net)
574{
575 return AX_EEPROM_LEN;
576}
577
578static int
579ax88179_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
580 u8 *data)
581{
582 struct usbnet *dev = netdev_priv(net);
583 u16 *eeprom_buff;
584 int first_word, last_word;
585 int i, ret;
586
587 if (eeprom->len == 0)
588 return -EINVAL;
589
590 eeprom->magic = AX88179_EEPROM_MAGIC;
591
592 first_word = eeprom->offset >> 1;
593 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
594 eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
595 GFP_KERNEL);
596 if (!eeprom_buff)
597 return -ENOMEM;
598
599 /* ax88179/178A returns 2 bytes from eeprom on read */
600 for (i = first_word; i <= last_word; i++) {
601 ret = __ax88179_read_cmd(dev, AX_ACCESS_EEPROM, i, 1, 2,
602 &eeprom_buff[i - first_word],
603 0);
604 if (ret < 0) {
605 kfree(eeprom_buff);
606 return -EIO;
607 }
608 }
609
610 memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
611 kfree(eeprom_buff);
612 return 0;
613}
614
615static int
616ax88179_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
617 u8 *data)
618{
619 struct usbnet *dev = netdev_priv(net);
620 u16 *eeprom_buff;
621 int first_word;
622 int last_word;
623 int ret;
624 int i;
625
626 netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n",
627 eeprom->len, eeprom->offset, eeprom->magic);
628
629 if (eeprom->len == 0)
630 return -EINVAL;
631
632 if (eeprom->magic != AX88179_EEPROM_MAGIC)
633 return -EINVAL;
634
635 first_word = eeprom->offset >> 1;
636 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
637
638 eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
639 GFP_KERNEL);
640 if (!eeprom_buff)
641 return -ENOMEM;
642
643 /* align data to 16 bit boundaries, read the missing data from
644 the EEPROM */
645 if (eeprom->offset & 1) {
646 ret = ax88179_read_cmd(dev, AX_ACCESS_EEPROM, first_word, 1, 2,
647 &eeprom_buff[0]);
648 if (ret < 0) {
649 netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word);
650 goto free;
651 }
652 }
653
654 if ((eeprom->offset + eeprom->len) & 1) {
655 ret = ax88179_read_cmd(dev, AX_ACCESS_EEPROM, last_word, 1, 2,
656 &eeprom_buff[last_word - first_word]);
657 if (ret < 0) {
658 netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word);
659 goto free;
660 }
661 }
662
663 memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len);
664
665 for (i = first_word; i <= last_word; i++) {
666 netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n",
667 i, eeprom_buff[i - first_word]);
668 ret = ax88179_write_cmd(dev, AX_ACCESS_EEPROM, i, 1, 2,
669 &eeprom_buff[i - first_word]);
670 if (ret < 0) {
671 netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n", i);
672 goto free;
673 }
674 msleep(20);
675 }
676
677 /* reload EEPROM data */
678 ret = ax88179_write_cmd(dev, AX_RELOAD_EEPROM_EFUSE, 0x0000, 0, 0, NULL);
679 if (ret < 0) {
680 netdev_err(net, "Failed to reload EEPROM data\n");
681 goto free;
682 }
683
684 ret = 0;
685free:
686 kfree(eeprom_buff);
687 return ret;
688}
689
690static int ax88179_get_link_ksettings(struct net_device *net,
691 struct ethtool_link_ksettings *cmd)
692{
693 struct usbnet *dev = netdev_priv(net);
694
695 mii_ethtool_get_link_ksettings(&dev->mii, cmd);
696
697 return 0;
698}
699
700static int ax88179_set_link_ksettings(struct net_device *net,
701 const struct ethtool_link_ksettings *cmd)
702{
703 struct usbnet *dev = netdev_priv(net);
704 return mii_ethtool_set_link_ksettings(&dev->mii, cmd);
705}
706
707static int
708ax88179_ethtool_get_eee(struct usbnet *dev, struct ethtool_eee *data)
709{
710 int val;
711
712 /* Get Supported EEE */
713 val = ax88179_phy_read_mmd_indirect(dev, MDIO_PCS_EEE_ABLE,
714 MDIO_MMD_PCS);
715 if (val < 0)
716 return val;
717 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
718
719 /* Get advertisement EEE */
720 val = ax88179_phy_read_mmd_indirect(dev, MDIO_AN_EEE_ADV,
721 MDIO_MMD_AN);
722 if (val < 0)
723 return val;
724 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
725
726 /* Get LP advertisement EEE */
727 val = ax88179_phy_read_mmd_indirect(dev, MDIO_AN_EEE_LPABLE,
728 MDIO_MMD_AN);
729 if (val < 0)
730 return val;
731 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
732
733 return 0;
734}
735
736static int
737ax88179_ethtool_set_eee(struct usbnet *dev, struct ethtool_eee *data)
738{
739 u16 tmp16 = ethtool_adv_to_mmd_eee_adv_t(data->advertised);
740
741 return ax88179_phy_write_mmd_indirect(dev, MDIO_AN_EEE_ADV,
742 MDIO_MMD_AN, tmp16);
743}
744
745static int ax88179_chk_eee(struct usbnet *dev)
746{
747 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
748 struct ax88179_data *priv = (struct ax88179_data *)dev->data;
749
750 mii_ethtool_gset(&dev->mii, &ecmd);
751
752 if (ecmd.duplex & DUPLEX_FULL) {
753 int eee_lp, eee_cap, eee_adv;
754 u32 lp, cap, adv, supported = 0;
755
756 eee_cap = ax88179_phy_read_mmd_indirect(dev,
757 MDIO_PCS_EEE_ABLE,
758 MDIO_MMD_PCS);
759 if (eee_cap < 0) {
760 priv->eee_active = 0;
761 return false;
762 }
763
764 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
765 if (!cap) {
766 priv->eee_active = 0;
767 return false;
768 }
769
770 eee_lp = ax88179_phy_read_mmd_indirect(dev,
771 MDIO_AN_EEE_LPABLE,
772 MDIO_MMD_AN);
773 if (eee_lp < 0) {
774 priv->eee_active = 0;
775 return false;
776 }
777
778 eee_adv = ax88179_phy_read_mmd_indirect(dev,
779 MDIO_AN_EEE_ADV,
780 MDIO_MMD_AN);
781
782 if (eee_adv < 0) {
783 priv->eee_active = 0;
784 return false;
785 }
786
787 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
788 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
789 supported = (ecmd.speed == SPEED_1000) ?
790 SUPPORTED_1000baseT_Full :
791 SUPPORTED_100baseT_Full;
792
793 if (!(lp & adv & supported)) {
794 priv->eee_active = 0;
795 return false;
796 }
797
798 priv->eee_active = 1;
799 return true;
800 }
801
802 priv->eee_active = 0;
803 return false;
804}
805
806static void ax88179_disable_eee(struct usbnet *dev)
807{
808 u16 tmp16;
809
810 tmp16 = GMII_PHY_PGSEL_PAGE3;
811 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
812 GMII_PHY_PAGE_SELECT, 2, &tmp16);
813
814 tmp16 = 0x3246;
815 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
816 MII_PHYADDR, 2, &tmp16);
817
818 tmp16 = GMII_PHY_PGSEL_PAGE0;
819 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
820 GMII_PHY_PAGE_SELECT, 2, &tmp16);
821}
822
823static void ax88179_enable_eee(struct usbnet *dev)
824{
825 u16 tmp16;
826
827 tmp16 = GMII_PHY_PGSEL_PAGE3;
828 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
829 GMII_PHY_PAGE_SELECT, 2, &tmp16);
830
831 tmp16 = 0x3247;
832 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
833 MII_PHYADDR, 2, &tmp16);
834
835 tmp16 = GMII_PHY_PGSEL_PAGE5;
836 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
837 GMII_PHY_PAGE_SELECT, 2, &tmp16);
838
839 tmp16 = 0x0680;
840 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
841 MII_BMSR, 2, &tmp16);
842
843 tmp16 = GMII_PHY_PGSEL_PAGE0;
844 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
845 GMII_PHY_PAGE_SELECT, 2, &tmp16);
846}
847
848static int ax88179_get_eee(struct net_device *net, struct ethtool_eee *edata)
849{
850 struct usbnet *dev = netdev_priv(net);
851 struct ax88179_data *priv = (struct ax88179_data *)dev->data;
852
853 edata->eee_enabled = priv->eee_enabled;
854 edata->eee_active = priv->eee_active;
855
856 return ax88179_ethtool_get_eee(dev, edata);
857}
858
859static int ax88179_set_eee(struct net_device *net, struct ethtool_eee *edata)
860{
861 struct usbnet *dev = netdev_priv(net);
862 struct ax88179_data *priv = (struct ax88179_data *)dev->data;
863 int ret;
864
865 priv->eee_enabled = edata->eee_enabled;
866 if (!priv->eee_enabled) {
867 ax88179_disable_eee(dev);
868 } else {
869 priv->eee_enabled = ax88179_chk_eee(dev);
870 if (!priv->eee_enabled)
871 return -EOPNOTSUPP;
872
873 ax88179_enable_eee(dev);
874 }
875
876 ret = ax88179_ethtool_set_eee(dev, edata);
877 if (ret)
878 return ret;
879
880 mii_nway_restart(&dev->mii);
881
882 usbnet_link_change(dev, 0, 0);
883
884 return ret;
885}
886
887static int ax88179_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
888{
889 struct usbnet *dev = netdev_priv(net);
890 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
891}
892
893static const struct ethtool_ops ax88179_ethtool_ops = {
894 .get_link = ethtool_op_get_link,
895 .get_msglevel = usbnet_get_msglevel,
896 .set_msglevel = usbnet_set_msglevel,
897 .get_wol = ax88179_get_wol,
898 .set_wol = ax88179_set_wol,
899 .get_eeprom_len = ax88179_get_eeprom_len,
900 .get_eeprom = ax88179_get_eeprom,
901 .set_eeprom = ax88179_set_eeprom,
902 .get_eee = ax88179_get_eee,
903 .set_eee = ax88179_set_eee,
904 .nway_reset = usbnet_nway_reset,
905 .get_link_ksettings = ax88179_get_link_ksettings,
906 .set_link_ksettings = ax88179_set_link_ksettings,
907 .get_ts_info = ethtool_op_get_ts_info,
908};
909
910static void ax88179_set_multicast(struct net_device *net)
911{
912 struct usbnet *dev = netdev_priv(net);
913 struct ax88179_data *data = (struct ax88179_data *)dev->data;
914 u8 *m_filter = ((u8 *)dev->data) + 12;
915
916 data->rxctl = (AX_RX_CTL_START | AX_RX_CTL_AB | AX_RX_CTL_IPE);
917
918 if (net->flags & IFF_PROMISC) {
919 data->rxctl |= AX_RX_CTL_PRO;
920 } else if (net->flags & IFF_ALLMULTI ||
921 netdev_mc_count(net) > AX_MAX_MCAST) {
922 data->rxctl |= AX_RX_CTL_AMALL;
923 } else if (netdev_mc_empty(net)) {
924 /* just broadcast and directed */
925 } else {
926 /* We use the 20 byte dev->data for our 8 byte filter buffer
927 * to avoid allocating memory that is tricky to free later
928 */
929 u32 crc_bits;
930 struct netdev_hw_addr *ha;
931
932 memset(m_filter, 0, AX_MCAST_FLTSIZE);
933
934 netdev_for_each_mc_addr(ha, net) {
935 crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
936 *(m_filter + (crc_bits >> 3)) |= (1 << (crc_bits & 7));
937 }
938
939 ax88179_write_cmd_async(dev, AX_ACCESS_MAC, AX_MULFLTARY,
940 AX_MCAST_FLTSIZE, AX_MCAST_FLTSIZE,
941 m_filter);
942
943 data->rxctl |= AX_RX_CTL_AM;
944 }
945
946 ax88179_write_cmd_async(dev, AX_ACCESS_MAC, AX_RX_CTL,
947 2, 2, &data->rxctl);
948}
949
950static int
951ax88179_set_features(struct net_device *net, netdev_features_t features)
952{
953 u8 tmp;
954 struct usbnet *dev = netdev_priv(net);
955 netdev_features_t changed = net->features ^ features;
956
957 if (changed & NETIF_F_IP_CSUM) {
958 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, &tmp);
959 tmp ^= AX_TXCOE_TCP | AX_TXCOE_UDP;
960 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, &tmp);
961 }
962
963 if (changed & NETIF_F_IPV6_CSUM) {
964 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, &tmp);
965 tmp ^= AX_TXCOE_TCPV6 | AX_TXCOE_UDPV6;
966 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, &tmp);
967 }
968
969 if (changed & NETIF_F_RXCSUM) {
970 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_RXCOE_CTL, 1, 1, &tmp);
971 tmp ^= AX_RXCOE_IP | AX_RXCOE_TCP | AX_RXCOE_UDP |
972 AX_RXCOE_TCPV6 | AX_RXCOE_UDPV6;
973 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RXCOE_CTL, 1, 1, &tmp);
974 }
975
976 return 0;
977}
978
979static int ax88179_change_mtu(struct net_device *net, int new_mtu)
980{
981 struct usbnet *dev = netdev_priv(net);
982 u16 tmp16;
983
984 net->mtu = new_mtu;
985 dev->hard_mtu = net->mtu + net->hard_header_len;
986
987 if (net->mtu > 1500) {
988 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
989 2, 2, &tmp16);
990 tmp16 |= AX_MEDIUM_JUMBO_EN;
991 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
992 2, 2, &tmp16);
993 } else {
994 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
995 2, 2, &tmp16);
996 tmp16 &= ~AX_MEDIUM_JUMBO_EN;
997 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
998 2, 2, &tmp16);
999 }
1000
1001 /* max qlen depend on hard_mtu and rx_urb_size */
1002 usbnet_update_max_qlen(dev);
1003
1004 return 0;
1005}
1006
1007static int ax88179_set_mac_addr(struct net_device *net, void *p)
1008{
1009 struct usbnet *dev = netdev_priv(net);
1010 struct sockaddr *addr = p;
1011 int ret;
1012
1013 if (netif_running(net))
1014 return -EBUSY;
1015 if (!is_valid_ether_addr(addr->sa_data))
1016 return -EADDRNOTAVAIL;
1017
1018 memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
1019
1020 /* Set the MAC address */
1021 ret = ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_NODE_ID, ETH_ALEN,
1022 ETH_ALEN, net->dev_addr);
1023 if (ret < 0)
1024 return ret;
1025
1026 return 0;
1027}
1028
1029static const struct net_device_ops ax88179_netdev_ops = {
1030 .ndo_open = usbnet_open,
1031 .ndo_stop = usbnet_stop,
1032 .ndo_start_xmit = usbnet_start_xmit,
1033 .ndo_tx_timeout = usbnet_tx_timeout,
1034 .ndo_get_stats64 = dev_get_tstats64,
1035 .ndo_change_mtu = ax88179_change_mtu,
1036 .ndo_set_mac_address = ax88179_set_mac_addr,
1037 .ndo_validate_addr = eth_validate_addr,
1038 .ndo_do_ioctl = ax88179_ioctl,
1039 .ndo_set_rx_mode = ax88179_set_multicast,
1040 .ndo_set_features = ax88179_set_features,
1041};
1042
1043static int ax88179_check_eeprom(struct usbnet *dev)
1044{
1045 u8 i, buf, eeprom[20];
1046 u16 csum, delay = HZ / 10;
1047 unsigned long jtimeout;
1048
1049 /* Read EEPROM content */
1050 for (i = 0; i < 6; i++) {
1051 buf = i;
1052 if (ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_SROM_ADDR,
1053 1, 1, &buf) < 0)
1054 return -EINVAL;
1055
1056 buf = EEP_RD;
1057 if (ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_SROM_CMD,
1058 1, 1, &buf) < 0)
1059 return -EINVAL;
1060
1061 jtimeout = jiffies + delay;
1062 do {
1063 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_SROM_CMD,
1064 1, 1, &buf);
1065
1066 if (time_after(jiffies, jtimeout))
1067 return -EINVAL;
1068
1069 } while (buf & EEP_BUSY);
1070
1071 __ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_SROM_DATA_LOW,
1072 2, 2, &eeprom[i * 2], 0);
1073
1074 if ((i == 0) && (eeprom[0] == 0xFF))
1075 return -EINVAL;
1076 }
1077
1078 csum = eeprom[6] + eeprom[7] + eeprom[8] + eeprom[9];
1079 csum = (csum >> 8) + (csum & 0xff);
1080 if ((csum + eeprom[10]) != 0xff)
1081 return -EINVAL;
1082
1083 return 0;
1084}
1085
1086static int ax88179_check_efuse(struct usbnet *dev, u16 *ledmode)
1087{
1088 u8 i;
1089 u8 efuse[64];
1090 u16 csum = 0;
1091
1092 if (ax88179_read_cmd(dev, AX_ACCESS_EFUS, 0, 64, 64, efuse) < 0)
1093 return -EINVAL;
1094
1095 if (*efuse == 0xFF)
1096 return -EINVAL;
1097
1098 for (i = 0; i < 64; i++)
1099 csum = csum + efuse[i];
1100
1101 while (csum > 255)
1102 csum = (csum & 0x00FF) + ((csum >> 8) & 0x00FF);
1103
1104 if (csum != 0xFF)
1105 return -EINVAL;
1106
1107 *ledmode = (efuse[51] << 8) | efuse[52];
1108
1109 return 0;
1110}
1111
1112static int ax88179_convert_old_led(struct usbnet *dev, u16 *ledvalue)
1113{
1114 u16 led;
1115
1116 /* Loaded the old eFuse LED Mode */
1117 if (ax88179_read_cmd(dev, AX_ACCESS_EEPROM, 0x3C, 1, 2, &led) < 0)
1118 return -EINVAL;
1119
1120 led >>= 8;
1121 switch (led) {
1122 case 0xFF:
1123 led = LED0_ACTIVE | LED1_LINK_10 | LED1_LINK_100 |
1124 LED1_LINK_1000 | LED2_ACTIVE | LED2_LINK_10 |
1125 LED2_LINK_100 | LED2_LINK_1000 | LED_VALID;
1126 break;
1127 case 0xFE:
1128 led = LED0_ACTIVE | LED1_LINK_1000 | LED2_LINK_100 | LED_VALID;
1129 break;
1130 case 0xFD:
1131 led = LED0_ACTIVE | LED1_LINK_1000 | LED2_LINK_100 |
1132 LED2_LINK_10 | LED_VALID;
1133 break;
1134 case 0xFC:
1135 led = LED0_ACTIVE | LED1_ACTIVE | LED1_LINK_1000 | LED2_ACTIVE |
1136 LED2_LINK_100 | LED2_LINK_10 | LED_VALID;
1137 break;
1138 default:
1139 led = LED0_ACTIVE | LED1_LINK_10 | LED1_LINK_100 |
1140 LED1_LINK_1000 | LED2_ACTIVE | LED2_LINK_10 |
1141 LED2_LINK_100 | LED2_LINK_1000 | LED_VALID;
1142 break;
1143 }
1144
1145 *ledvalue = led;
1146
1147 return 0;
1148}
1149
1150static int ax88179_led_setting(struct usbnet *dev)
1151{
1152 u8 ledfd, value = 0;
1153 u16 tmp, ledact, ledlink, ledvalue = 0, delay = HZ / 10;
1154 unsigned long jtimeout;
1155
1156 /* Check AX88179 version. UA1 or UA2*/
1157 ax88179_read_cmd(dev, AX_ACCESS_MAC, GENERAL_STATUS, 1, 1, &value);
1158
1159 if (!(value & AX_SECLD)) { /* UA1 */
1160 value = AX_GPIO_CTRL_GPIO3EN | AX_GPIO_CTRL_GPIO2EN |
1161 AX_GPIO_CTRL_GPIO1EN;
1162 if (ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_GPIO_CTRL,
1163 1, 1, &value) < 0)
1164 return -EINVAL;
1165 }
1166
1167 /* Check EEPROM */
1168 if (!ax88179_check_eeprom(dev)) {
1169 value = 0x42;
1170 if (ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_SROM_ADDR,
1171 1, 1, &value) < 0)
1172 return -EINVAL;
1173
1174 value = EEP_RD;
1175 if (ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_SROM_CMD,
1176 1, 1, &value) < 0)
1177 return -EINVAL;
1178
1179 jtimeout = jiffies + delay;
1180 do {
1181 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_SROM_CMD,
1182 1, 1, &value);
1183
1184 if (time_after(jiffies, jtimeout))
1185 return -EINVAL;
1186
1187 } while (value & EEP_BUSY);
1188
1189 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_SROM_DATA_HIGH,
1190 1, 1, &value);
1191 ledvalue = (value << 8);
1192
1193 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_SROM_DATA_LOW,
1194 1, 1, &value);
1195 ledvalue |= value;
1196
1197 /* load internal ROM for defaule setting */
1198 if ((ledvalue == 0xFFFF) || ((ledvalue & LED_VALID) == 0))
1199 ax88179_convert_old_led(dev, &ledvalue);
1200
1201 } else if (!ax88179_check_efuse(dev, &ledvalue)) {
1202 if ((ledvalue == 0xFFFF) || ((ledvalue & LED_VALID) == 0))
1203 ax88179_convert_old_led(dev, &ledvalue);
1204 } else {
1205 ax88179_convert_old_led(dev, &ledvalue);
1206 }
1207
1208 tmp = GMII_PHY_PGSEL_EXT;
1209 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1210 GMII_PHY_PAGE_SELECT, 2, &tmp);
1211
1212 tmp = 0x2c;
1213 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1214 GMII_PHYPAGE, 2, &tmp);
1215
1216 ax88179_read_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1217 GMII_LED_ACT, 2, &ledact);
1218
1219 ax88179_read_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1220 GMII_LED_LINK, 2, &ledlink);
1221
1222 ledact &= GMII_LED_ACTIVE_MASK;
1223 ledlink &= GMII_LED_LINK_MASK;
1224
1225 if (ledvalue & LED0_ACTIVE)
1226 ledact |= GMII_LED0_ACTIVE;
1227
1228 if (ledvalue & LED1_ACTIVE)
1229 ledact |= GMII_LED1_ACTIVE;
1230
1231 if (ledvalue & LED2_ACTIVE)
1232 ledact |= GMII_LED2_ACTIVE;
1233
1234 if (ledvalue & LED0_LINK_10)
1235 ledlink |= GMII_LED0_LINK_10;
1236
1237 if (ledvalue & LED1_LINK_10)
1238 ledlink |= GMII_LED1_LINK_10;
1239
1240 if (ledvalue & LED2_LINK_10)
1241 ledlink |= GMII_LED2_LINK_10;
1242
1243 if (ledvalue & LED0_LINK_100)
1244 ledlink |= GMII_LED0_LINK_100;
1245
1246 if (ledvalue & LED1_LINK_100)
1247 ledlink |= GMII_LED1_LINK_100;
1248
1249 if (ledvalue & LED2_LINK_100)
1250 ledlink |= GMII_LED2_LINK_100;
1251
1252 if (ledvalue & LED0_LINK_1000)
1253 ledlink |= GMII_LED0_LINK_1000;
1254
1255 if (ledvalue & LED1_LINK_1000)
1256 ledlink |= GMII_LED1_LINK_1000;
1257
1258 if (ledvalue & LED2_LINK_1000)
1259 ledlink |= GMII_LED2_LINK_1000;
1260
1261 tmp = ledact;
1262 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1263 GMII_LED_ACT, 2, &tmp);
1264
1265 tmp = ledlink;
1266 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1267 GMII_LED_LINK, 2, &tmp);
1268
1269 tmp = GMII_PHY_PGSEL_PAGE0;
1270 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1271 GMII_PHY_PAGE_SELECT, 2, &tmp);
1272
1273 /* LED full duplex setting */
1274 ledfd = 0;
1275 if (ledvalue & LED0_FD)
1276 ledfd |= 0x01;
1277 else if ((ledvalue & LED0_USB3_MASK) == 0)
1278 ledfd |= 0x02;
1279
1280 if (ledvalue & LED1_FD)
1281 ledfd |= 0x04;
1282 else if ((ledvalue & LED1_USB3_MASK) == 0)
1283 ledfd |= 0x08;
1284
1285 if (ledvalue & LED2_FD)
1286 ledfd |= 0x10;
1287 else if ((ledvalue & LED2_USB3_MASK) == 0)
1288 ledfd |= 0x20;
1289
1290 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_LEDCTRL, 1, 1, &ledfd);
1291
1292 return 0;
1293}
1294
1295static void ax88179_get_mac_addr(struct usbnet *dev)
1296{
1297 u8 mac[ETH_ALEN];
1298
1299 memset(mac, 0, sizeof(mac));
1300
1301 /* Maybe the boot loader passed the MAC address via device tree */
1302 if (!eth_platform_get_mac_address(&dev->udev->dev, mac)) {
1303 netif_dbg(dev, ifup, dev->net,
1304 "MAC address read from device tree");
1305 } else {
1306 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_NODE_ID, ETH_ALEN,
1307 ETH_ALEN, mac);
1308 netif_dbg(dev, ifup, dev->net,
1309 "MAC address read from ASIX chip");
1310 }
1311
1312 if (is_valid_ether_addr(mac)) {
1313 memcpy(dev->net->dev_addr, mac, ETH_ALEN);
1314 } else {
1315 netdev_info(dev->net, "invalid MAC address, using random\n");
1316 eth_hw_addr_random(dev->net);
1317 }
1318
1319 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_NODE_ID, ETH_ALEN, ETH_ALEN,
1320 dev->net->dev_addr);
1321}
1322
1323static int ax88179_bind(struct usbnet *dev, struct usb_interface *intf)
1324{
1325 u8 buf[5];
1326 u16 *tmp16;
1327 u8 *tmp;
1328 struct ax88179_data *ax179_data = (struct ax88179_data *)dev->data;
1329 struct ethtool_eee eee_data;
1330
1331 usbnet_get_endpoints(dev, intf);
1332
1333 tmp16 = (u16 *)buf;
1334 tmp = (u8 *)buf;
1335
1336 memset(ax179_data, 0, sizeof(*ax179_data));
1337
1338 /* Power up ethernet PHY */
1339 *tmp16 = 0;
1340 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, tmp16);
1341 *tmp16 = AX_PHYPWR_RSTCTL_IPRL;
1342 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, tmp16);
1343 msleep(200);
1344
1345 *tmp = AX_CLK_SELECT_ACS | AX_CLK_SELECT_BCS;
1346 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, tmp);
1347 msleep(100);
1348
1349 /* Read MAC address from DTB or asix chip */
1350 ax88179_get_mac_addr(dev);
1351 memcpy(dev->net->perm_addr, dev->net->dev_addr, ETH_ALEN);
1352
1353 /* RX bulk configuration */
1354 memcpy(tmp, &AX88179_BULKIN_SIZE[0], 5);
1355 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_BULKIN_QCTRL, 5, 5, tmp);
1356
1357 dev->rx_urb_size = 1024 * 20;
1358
1359 *tmp = 0x34;
1360 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PAUSE_WATERLVL_LOW, 1, 1, tmp);
1361
1362 *tmp = 0x52;
1363 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PAUSE_WATERLVL_HIGH,
1364 1, 1, tmp);
1365
1366 dev->net->netdev_ops = &ax88179_netdev_ops;
1367 dev->net->ethtool_ops = &ax88179_ethtool_ops;
1368 dev->net->needed_headroom = 8;
1369 dev->net->max_mtu = 4088;
1370
1371 /* Initialize MII structure */
1372 dev->mii.dev = dev->net;
1373 dev->mii.mdio_read = ax88179_mdio_read;
1374 dev->mii.mdio_write = ax88179_mdio_write;
1375 dev->mii.phy_id_mask = 0xff;
1376 dev->mii.reg_num_mask = 0xff;
1377 dev->mii.phy_id = 0x03;
1378 dev->mii.supports_gmii = 1;
1379
1380 dev->net->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1381 NETIF_F_RXCSUM;
1382
1383 dev->net->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1384 NETIF_F_RXCSUM;
1385
1386 /* Enable checksum offload */
1387 *tmp = AX_RXCOE_IP | AX_RXCOE_TCP | AX_RXCOE_UDP |
1388 AX_RXCOE_TCPV6 | AX_RXCOE_UDPV6;
1389 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RXCOE_CTL, 1, 1, tmp);
1390
1391 *tmp = AX_TXCOE_IP | AX_TXCOE_TCP | AX_TXCOE_UDP |
1392 AX_TXCOE_TCPV6 | AX_TXCOE_UDPV6;
1393 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, tmp);
1394
1395 /* Configure RX control register => start operation */
1396 *tmp16 = AX_RX_CTL_DROPCRCERR | AX_RX_CTL_IPE | AX_RX_CTL_START |
1397 AX_RX_CTL_AP | AX_RX_CTL_AMALL | AX_RX_CTL_AB;
1398 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, tmp16);
1399
1400 *tmp = AX_MONITOR_MODE_PMETYPE | AX_MONITOR_MODE_PMEPOL |
1401 AX_MONITOR_MODE_RWMP;
1402 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MONITOR_MOD, 1, 1, tmp);
1403
1404 /* Configure default medium type => giga */
1405 *tmp16 = AX_MEDIUM_RECEIVE_EN | AX_MEDIUM_TXFLOW_CTRLEN |
1406 AX_MEDIUM_RXFLOW_CTRLEN | AX_MEDIUM_FULL_DUPLEX |
1407 AX_MEDIUM_GIGAMODE;
1408 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
1409 2, 2, tmp16);
1410
1411 ax88179_led_setting(dev);
1412
1413 ax179_data->eee_enabled = 0;
1414 ax179_data->eee_active = 0;
1415
1416 ax88179_disable_eee(dev);
1417
1418 ax88179_ethtool_get_eee(dev, &eee_data);
1419 eee_data.advertised = 0;
1420 ax88179_ethtool_set_eee(dev, &eee_data);
1421
1422 /* Restart autoneg */
1423 mii_nway_restart(&dev->mii);
1424
1425 usbnet_link_change(dev, 0, 0);
1426
1427 return 0;
1428}
1429
1430static void ax88179_unbind(struct usbnet *dev, struct usb_interface *intf)
1431{
1432 u16 tmp16;
1433
1434 /* Configure RX control register => stop operation */
1435 tmp16 = AX_RX_CTL_STOP;
1436 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, &tmp16);
1437
1438 tmp16 = 0;
1439 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, &tmp16);
1440
1441 /* Power down ethernet PHY */
1442 tmp16 = 0;
1443 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, &tmp16);
1444}
1445
1446static void
1447ax88179_rx_checksum(struct sk_buff *skb, u32 *pkt_hdr)
1448{
1449 skb->ip_summed = CHECKSUM_NONE;
1450
1451 /* checksum error bit is set */
1452 if ((*pkt_hdr & AX_RXHDR_L3CSUM_ERR) ||
1453 (*pkt_hdr & AX_RXHDR_L4CSUM_ERR))
1454 return;
1455
1456 /* It must be a TCP or UDP packet with a valid checksum */
1457 if (((*pkt_hdr & AX_RXHDR_L4_TYPE_MASK) == AX_RXHDR_L4_TYPE_TCP) ||
1458 ((*pkt_hdr & AX_RXHDR_L4_TYPE_MASK) == AX_RXHDR_L4_TYPE_UDP))
1459 skb->ip_summed = CHECKSUM_UNNECESSARY;
1460}
1461
1462static int ax88179_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
1463{
1464 struct sk_buff *ax_skb;
1465 int pkt_cnt;
1466 u32 rx_hdr;
1467 u16 hdr_off;
1468 u32 *pkt_hdr;
1469
1470 /* This check is no longer done by usbnet */
1471 if (skb->len < dev->net->hard_header_len)
1472 return 0;
1473
1474 skb_trim(skb, skb->len - 4);
1475 rx_hdr = get_unaligned_le32(skb_tail_pointer(skb));
1476
1477 pkt_cnt = (u16)rx_hdr;
1478 hdr_off = (u16)(rx_hdr >> 16);
1479 pkt_hdr = (u32 *)(skb->data + hdr_off);
1480
1481 while (pkt_cnt--) {
1482 u16 pkt_len;
1483
1484 le32_to_cpus(pkt_hdr);
1485 pkt_len = (*pkt_hdr >> 16) & 0x1fff;
1486
1487 /* Check CRC or runt packet */
1488 if ((*pkt_hdr & AX_RXHDR_CRC_ERR) ||
1489 (*pkt_hdr & AX_RXHDR_DROP_ERR)) {
1490 skb_pull(skb, (pkt_len + 7) & 0xFFF8);
1491 pkt_hdr++;
1492 continue;
1493 }
1494
1495 if (pkt_cnt == 0) {
1496 skb->len = pkt_len;
1497 /* Skip IP alignment pseudo header */
1498 skb_pull(skb, 2);
1499 skb_set_tail_pointer(skb, skb->len);
1500 skb->truesize = pkt_len + sizeof(struct sk_buff);
1501 ax88179_rx_checksum(skb, pkt_hdr);
1502 return 1;
1503 }
1504
1505 ax_skb = skb_clone(skb, GFP_ATOMIC);
1506 if (ax_skb) {
1507 ax_skb->len = pkt_len;
1508 /* Skip IP alignment pseudo header */
1509 skb_pull(ax_skb, 2);
1510 skb_set_tail_pointer(ax_skb, ax_skb->len);
1511 ax_skb->truesize = pkt_len + sizeof(struct sk_buff);
1512 ax88179_rx_checksum(ax_skb, pkt_hdr);
1513 usbnet_skb_return(dev, ax_skb);
1514 } else {
1515 return 0;
1516 }
1517
1518 skb_pull(skb, (pkt_len + 7) & 0xFFF8);
1519 pkt_hdr++;
1520 }
1521 return 1;
1522}
1523
1524static struct sk_buff *
1525ax88179_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
1526{
1527 u32 tx_hdr1, tx_hdr2;
1528 int frame_size = dev->maxpacket;
1529 int mss = skb_shinfo(skb)->gso_size;
1530 int headroom;
1531 void *ptr;
1532
1533 tx_hdr1 = skb->len;
1534 tx_hdr2 = mss;
1535 if (((skb->len + 8) % frame_size) == 0)
1536 tx_hdr2 |= 0x80008000; /* Enable padding */
1537
1538 headroom = skb_headroom(skb) - 8;
1539
1540 if ((skb_header_cloned(skb) || headroom < 0) &&
1541 pskb_expand_head(skb, headroom < 0 ? 8 : 0, 0, GFP_ATOMIC)) {
1542 dev_kfree_skb_any(skb);
1543 return NULL;
1544 }
1545
1546 ptr = skb_push(skb, 8);
1547 put_unaligned_le32(tx_hdr1, ptr);
1548 put_unaligned_le32(tx_hdr2, ptr + 4);
1549
1550 return skb;
1551}
1552
1553static int ax88179_link_reset(struct usbnet *dev)
1554{
1555 struct ax88179_data *ax179_data = (struct ax88179_data *)dev->data;
1556 u8 tmp[5], link_sts;
1557 u16 mode, tmp16, delay = HZ / 10;
1558 u32 tmp32 = 0x40000000;
1559 unsigned long jtimeout;
1560
1561 jtimeout = jiffies + delay;
1562 while (tmp32 & 0x40000000) {
1563 mode = 0;
1564 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, &mode);
1565 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2,
1566 &ax179_data->rxctl);
1567
1568 /*link up, check the usb device control TX FIFO full or empty*/
1569 ax88179_read_cmd(dev, 0x81, 0x8c, 0, 4, &tmp32);
1570
1571 if (time_after(jiffies, jtimeout))
1572 return 0;
1573 }
1574
1575 mode = AX_MEDIUM_RECEIVE_EN | AX_MEDIUM_TXFLOW_CTRLEN |
1576 AX_MEDIUM_RXFLOW_CTRLEN;
1577
1578 ax88179_read_cmd(dev, AX_ACCESS_MAC, PHYSICAL_LINK_STATUS,
1579 1, 1, &link_sts);
1580
1581 ax88179_read_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1582 GMII_PHY_PHYSR, 2, &tmp16);
1583
1584 if (!(tmp16 & GMII_PHY_PHYSR_LINK)) {
1585 return 0;
1586 } else if (GMII_PHY_PHYSR_GIGA == (tmp16 & GMII_PHY_PHYSR_SMASK)) {
1587 mode |= AX_MEDIUM_GIGAMODE | AX_MEDIUM_EN_125MHZ;
1588 if (dev->net->mtu > 1500)
1589 mode |= AX_MEDIUM_JUMBO_EN;
1590
1591 if (link_sts & AX_USB_SS)
1592 memcpy(tmp, &AX88179_BULKIN_SIZE[0], 5);
1593 else if (link_sts & AX_USB_HS)
1594 memcpy(tmp, &AX88179_BULKIN_SIZE[1], 5);
1595 else
1596 memcpy(tmp, &AX88179_BULKIN_SIZE[3], 5);
1597 } else if (GMII_PHY_PHYSR_100 == (tmp16 & GMII_PHY_PHYSR_SMASK)) {
1598 mode |= AX_MEDIUM_PS;
1599
1600 if (link_sts & (AX_USB_SS | AX_USB_HS))
1601 memcpy(tmp, &AX88179_BULKIN_SIZE[2], 5);
1602 else
1603 memcpy(tmp, &AX88179_BULKIN_SIZE[3], 5);
1604 } else {
1605 memcpy(tmp, &AX88179_BULKIN_SIZE[3], 5);
1606 }
1607
1608 /* RX bulk configuration */
1609 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_BULKIN_QCTRL, 5, 5, tmp);
1610
1611 dev->rx_urb_size = (1024 * (tmp[3] + 2));
1612
1613 if (tmp16 & GMII_PHY_PHYSR_FULL)
1614 mode |= AX_MEDIUM_FULL_DUPLEX;
1615 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
1616 2, 2, &mode);
1617
1618 ax179_data->eee_enabled = ax88179_chk_eee(dev);
1619
1620 netif_carrier_on(dev->net);
1621
1622 return 0;
1623}
1624
1625static int ax88179_reset(struct usbnet *dev)
1626{
1627 u8 buf[5];
1628 u16 *tmp16;
1629 u8 *tmp;
1630 struct ax88179_data *ax179_data = (struct ax88179_data *)dev->data;
1631 struct ethtool_eee eee_data;
1632
1633 tmp16 = (u16 *)buf;
1634 tmp = (u8 *)buf;
1635
1636 /* Power up ethernet PHY */
1637 *tmp16 = 0;
1638 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, tmp16);
1639
1640 *tmp16 = AX_PHYPWR_RSTCTL_IPRL;
1641 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, tmp16);
1642 msleep(200);
1643
1644 *tmp = AX_CLK_SELECT_ACS | AX_CLK_SELECT_BCS;
1645 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, tmp);
1646 msleep(100);
1647
1648 /* Ethernet PHY Auto Detach*/
1649 ax88179_auto_detach(dev, 0);
1650
1651 /* Read MAC address from DTB or asix chip */
1652 ax88179_get_mac_addr(dev);
1653
1654 /* RX bulk configuration */
1655 memcpy(tmp, &AX88179_BULKIN_SIZE[0], 5);
1656 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_BULKIN_QCTRL, 5, 5, tmp);
1657
1658 dev->rx_urb_size = 1024 * 20;
1659
1660 *tmp = 0x34;
1661 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PAUSE_WATERLVL_LOW, 1, 1, tmp);
1662
1663 *tmp = 0x52;
1664 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PAUSE_WATERLVL_HIGH,
1665 1, 1, tmp);
1666
1667 dev->net->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1668 NETIF_F_RXCSUM;
1669
1670 dev->net->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1671 NETIF_F_RXCSUM;
1672
1673 /* Enable checksum offload */
1674 *tmp = AX_RXCOE_IP | AX_RXCOE_TCP | AX_RXCOE_UDP |
1675 AX_RXCOE_TCPV6 | AX_RXCOE_UDPV6;
1676 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RXCOE_CTL, 1, 1, tmp);
1677
1678 *tmp = AX_TXCOE_IP | AX_TXCOE_TCP | AX_TXCOE_UDP |
1679 AX_TXCOE_TCPV6 | AX_TXCOE_UDPV6;
1680 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, tmp);
1681
1682 /* Configure RX control register => start operation */
1683 *tmp16 = AX_RX_CTL_DROPCRCERR | AX_RX_CTL_IPE | AX_RX_CTL_START |
1684 AX_RX_CTL_AP | AX_RX_CTL_AMALL | AX_RX_CTL_AB;
1685 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, tmp16);
1686
1687 *tmp = AX_MONITOR_MODE_PMETYPE | AX_MONITOR_MODE_PMEPOL |
1688 AX_MONITOR_MODE_RWMP;
1689 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MONITOR_MOD, 1, 1, tmp);
1690
1691 /* Configure default medium type => giga */
1692 *tmp16 = AX_MEDIUM_RECEIVE_EN | AX_MEDIUM_TXFLOW_CTRLEN |
1693 AX_MEDIUM_RXFLOW_CTRLEN | AX_MEDIUM_FULL_DUPLEX |
1694 AX_MEDIUM_GIGAMODE;
1695 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
1696 2, 2, tmp16);
1697
1698 ax88179_led_setting(dev);
1699
1700 ax179_data->eee_enabled = 0;
1701 ax179_data->eee_active = 0;
1702
1703 ax88179_disable_eee(dev);
1704
1705 ax88179_ethtool_get_eee(dev, &eee_data);
1706 eee_data.advertised = 0;
1707 ax88179_ethtool_set_eee(dev, &eee_data);
1708
1709 /* Restart autoneg */
1710 mii_nway_restart(&dev->mii);
1711
1712 usbnet_link_change(dev, 0, 0);
1713
1714 return 0;
1715}
1716
1717static int ax88179_stop(struct usbnet *dev)
1718{
1719 u16 tmp16;
1720
1721 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
1722 2, 2, &tmp16);
1723 tmp16 &= ~AX_MEDIUM_RECEIVE_EN;
1724 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
1725 2, 2, &tmp16);
1726
1727 return 0;
1728}
1729
1730static const struct driver_info ax88179_info = {
1731 .description = "ASIX AX88179 USB 3.0 Gigabit Ethernet",
1732 .bind = ax88179_bind,
1733 .unbind = ax88179_unbind,
1734 .status = ax88179_status,
1735 .link_reset = ax88179_link_reset,
1736 .reset = ax88179_reset,
1737 .stop = ax88179_stop,
1738 .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1739 .rx_fixup = ax88179_rx_fixup,
1740 .tx_fixup = ax88179_tx_fixup,
1741};
1742
1743static const struct driver_info ax88178a_info = {
1744 .description = "ASIX AX88178A USB 2.0 Gigabit Ethernet",
1745 .bind = ax88179_bind,
1746 .unbind = ax88179_unbind,
1747 .status = ax88179_status,
1748 .link_reset = ax88179_link_reset,
1749 .reset = ax88179_reset,
1750 .stop = ax88179_stop,
1751 .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1752 .rx_fixup = ax88179_rx_fixup,
1753 .tx_fixup = ax88179_tx_fixup,
1754};
1755
1756static const struct driver_info cypress_GX3_info = {
1757 .description = "Cypress GX3 SuperSpeed to Gigabit Ethernet Controller",
1758 .bind = ax88179_bind,
1759 .unbind = ax88179_unbind,
1760 .status = ax88179_status,
1761 .link_reset = ax88179_link_reset,
1762 .reset = ax88179_reset,
1763 .stop = ax88179_stop,
1764 .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1765 .rx_fixup = ax88179_rx_fixup,
1766 .tx_fixup = ax88179_tx_fixup,
1767};
1768
1769static const struct driver_info dlink_dub1312_info = {
1770 .description = "D-Link DUB-1312 USB 3.0 to Gigabit Ethernet Adapter",
1771 .bind = ax88179_bind,
1772 .unbind = ax88179_unbind,
1773 .status = ax88179_status,
1774 .link_reset = ax88179_link_reset,
1775 .reset = ax88179_reset,
1776 .stop = ax88179_stop,
1777 .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1778 .rx_fixup = ax88179_rx_fixup,
1779 .tx_fixup = ax88179_tx_fixup,
1780};
1781
1782static const struct driver_info sitecom_info = {
1783 .description = "Sitecom USB 3.0 to Gigabit Adapter",
1784 .bind = ax88179_bind,
1785 .unbind = ax88179_unbind,
1786 .status = ax88179_status,
1787 .link_reset = ax88179_link_reset,
1788 .reset = ax88179_reset,
1789 .stop = ax88179_stop,
1790 .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1791 .rx_fixup = ax88179_rx_fixup,
1792 .tx_fixup = ax88179_tx_fixup,
1793};
1794
1795static const struct driver_info samsung_info = {
1796 .description = "Samsung USB Ethernet Adapter",
1797 .bind = ax88179_bind,
1798 .unbind = ax88179_unbind,
1799 .status = ax88179_status,
1800 .link_reset = ax88179_link_reset,
1801 .reset = ax88179_reset,
1802 .stop = ax88179_stop,
1803 .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1804 .rx_fixup = ax88179_rx_fixup,
1805 .tx_fixup = ax88179_tx_fixup,
1806};
1807
1808static const struct driver_info lenovo_info = {
1809 .description = "Lenovo OneLinkDock Gigabit LAN",
1810 .bind = ax88179_bind,
1811 .unbind = ax88179_unbind,
1812 .status = ax88179_status,
1813 .link_reset = ax88179_link_reset,
1814 .reset = ax88179_reset,
1815 .stop = ax88179_stop,
1816 .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1817 .rx_fixup = ax88179_rx_fixup,
1818 .tx_fixup = ax88179_tx_fixup,
1819};
1820
1821static const struct driver_info belkin_info = {
1822 .description = "Belkin USB Ethernet Adapter",
1823 .bind = ax88179_bind,
1824 .unbind = ax88179_unbind,
1825 .status = ax88179_status,
1826 .link_reset = ax88179_link_reset,
1827 .reset = ax88179_reset,
1828 .stop = ax88179_stop,
1829 .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1830 .rx_fixup = ax88179_rx_fixup,
1831 .tx_fixup = ax88179_tx_fixup,
1832};
1833
1834static const struct driver_info toshiba_info = {
1835 .description = "Toshiba USB Ethernet Adapter",
1836 .bind = ax88179_bind,
1837 .unbind = ax88179_unbind,
1838 .status = ax88179_status,
1839 .link_reset = ax88179_link_reset,
1840 .reset = ax88179_reset,
1841 .stop = ax88179_stop,
1842 .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1843 .rx_fixup = ax88179_rx_fixup,
1844 .tx_fixup = ax88179_tx_fixup,
1845};
1846
1847static const struct driver_info mct_info = {
1848 .description = "MCT USB 3.0 Gigabit Ethernet Adapter",
1849 .bind = ax88179_bind,
1850 .unbind = ax88179_unbind,
1851 .status = ax88179_status,
1852 .link_reset = ax88179_link_reset,
1853 .reset = ax88179_reset,
1854 .stop = ax88179_stop,
1855 .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1856 .rx_fixup = ax88179_rx_fixup,
1857 .tx_fixup = ax88179_tx_fixup,
1858};
1859
1860static const struct usb_device_id products[] = {
1861{
1862 /* ASIX AX88179 10/100/1000 */
1863 USB_DEVICE(0x0b95, 0x1790),
1864 .driver_info = (unsigned long)&ax88179_info,
1865}, {
1866 /* ASIX AX88178A 10/100/1000 */
1867 USB_DEVICE(0x0b95, 0x178a),
1868 .driver_info = (unsigned long)&ax88178a_info,
1869}, {
1870 /* Cypress GX3 SuperSpeed to Gigabit Ethernet Bridge Controller */
1871 USB_DEVICE(0x04b4, 0x3610),
1872 .driver_info = (unsigned long)&cypress_GX3_info,
1873}, {
1874 /* D-Link DUB-1312 USB 3.0 to Gigabit Ethernet Adapter */
1875 USB_DEVICE(0x2001, 0x4a00),
1876 .driver_info = (unsigned long)&dlink_dub1312_info,
1877}, {
1878 /* Sitecom USB 3.0 to Gigabit Adapter */
1879 USB_DEVICE(0x0df6, 0x0072),
1880 .driver_info = (unsigned long)&sitecom_info,
1881}, {
1882 /* Samsung USB Ethernet Adapter */
1883 USB_DEVICE(0x04e8, 0xa100),
1884 .driver_info = (unsigned long)&samsung_info,
1885}, {
1886 /* Lenovo OneLinkDock Gigabit LAN */
1887 USB_DEVICE(0x17ef, 0x304b),
1888 .driver_info = (unsigned long)&lenovo_info,
1889}, {
1890 /* Belkin B2B128 USB 3.0 Hub + Gigabit Ethernet Adapter */
1891 USB_DEVICE(0x050d, 0x0128),
1892 .driver_info = (unsigned long)&belkin_info,
1893}, {
1894 /* Toshiba USB 3.0 GBit Ethernet Adapter */
1895 USB_DEVICE(0x0930, 0x0a13),
1896 .driver_info = (unsigned long)&toshiba_info,
1897}, {
1898 /* Magic Control Technology U3-A9003 USB 3.0 Gigabit Ethernet Adapter */
1899 USB_DEVICE(0x0711, 0x0179),
1900 .driver_info = (unsigned long)&mct_info,
1901},
1902 { },
1903};
1904MODULE_DEVICE_TABLE(usb, products);
1905
1906static struct usb_driver ax88179_178a_driver = {
1907 .name = "ax88179_178a",
1908 .id_table = products,
1909 .probe = usbnet_probe,
1910 .suspend = ax88179_suspend,
1911 .resume = ax88179_resume,
1912 .reset_resume = ax88179_resume,
1913 .disconnect = usbnet_disconnect,
1914 .supports_autosuspend = 1,
1915 .disable_hub_initiated_lpm = 1,
1916};
1917
1918module_usb_driver(ax88179_178a_driver);
1919
1920MODULE_DESCRIPTION("ASIX AX88179/178A based USB 3.0/2.0 Gigabit Ethernet Devices");
1921MODULE_LICENSE("GPL");