Linux-libre 3.16.85-gnu
[librecmc/linux-libre.git] / drivers / net / usb / ax88179_178a.c
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
27 #define AX88179_PHY_ID                          0x03
28 #define AX_EEPROM_LEN                           0x100
29 #define AX88179_EEPROM_MAGIC                    0x17900b95
30 #define AX_MCAST_FLTSIZE                        8
31 #define AX_MAX_MCAST                            64
32 #define AX_INT_PPLS_LINK                        ((u32)BIT(16))
33 #define AX_RXHDR_L4_TYPE_MASK                   0x1c
34 #define AX_RXHDR_L4_TYPE_UDP                    4
35 #define AX_RXHDR_L4_TYPE_TCP                    16
36 #define AX_RXHDR_L3CSUM_ERR                     2
37 #define AX_RXHDR_L4CSUM_ERR                     1
38 #define AX_RXHDR_CRC_ERR                        ((u32)BIT(29))
39 #define AX_RXHDR_DROP_ERR                       ((u32)BIT(31))
40 #define AX_ACCESS_MAC                           0x01
41 #define AX_ACCESS_PHY                           0x02
42 #define AX_ACCESS_EEPROM                        0x04
43 #define AX_ACCESS_EFUS                          0x05
44 #define AX_PAUSE_WATERLVL_HIGH                  0x54
45 #define AX_PAUSE_WATERLVL_LOW                   0x55
46
47 #define PHYSICAL_LINK_STATUS                    0x02
48         #define AX_USB_SS               0x04
49         #define AX_USB_HS               0x02
50
51 #define GENERAL_STATUS                          0x03
52 /* Check AX88179 version. UA1:Bit2 = 0,  UA2:Bit2 = 1 */
53         #define AX_SECLD                0x04
54
55 #define AX_SROM_ADDR                            0x07
56 #define AX_SROM_CMD                             0x0a
57         #define EEP_RD                  0x04
58         #define EEP_BUSY                0x10
59
60 #define AX_SROM_DATA_LOW                        0x08
61 #define AX_SROM_DATA_HIGH                       0x09
62
63 #define AX_RX_CTL                               0x0b
64         #define AX_RX_CTL_DROPCRCERR    0x0100
65         #define AX_RX_CTL_IPE           0x0200
66         #define AX_RX_CTL_START         0x0080
67         #define AX_RX_CTL_AP            0x0020
68         #define AX_RX_CTL_AM            0x0010
69         #define AX_RX_CTL_AB            0x0008
70         #define AX_RX_CTL_AMALL         0x0002
71         #define AX_RX_CTL_PRO           0x0001
72         #define AX_RX_CTL_STOP          0x0000
73
74 #define AX_NODE_ID                              0x10
75 #define AX_MULFLTARY                            0x16
76
77 #define AX_MEDIUM_STATUS_MODE                   0x22
78         #define AX_MEDIUM_GIGAMODE      0x01
79         #define AX_MEDIUM_FULL_DUPLEX   0x02
80         #define AX_MEDIUM_EN_125MHZ     0x08
81         #define AX_MEDIUM_RXFLOW_CTRLEN 0x10
82         #define AX_MEDIUM_TXFLOW_CTRLEN 0x20
83         #define AX_MEDIUM_RECEIVE_EN    0x100
84         #define AX_MEDIUM_PS            0x200
85         #define AX_MEDIUM_JUMBO_EN      0x8040
86
87 #define AX_MONITOR_MOD                          0x24
88         #define AX_MONITOR_MODE_RWLC    0x02
89         #define AX_MONITOR_MODE_RWMP    0x04
90         #define AX_MONITOR_MODE_PMEPOL  0x20
91         #define AX_MONITOR_MODE_PMETYPE 0x40
92
93 #define AX_GPIO_CTRL                            0x25
94         #define AX_GPIO_CTRL_GPIO3EN    0x80
95         #define AX_GPIO_CTRL_GPIO2EN    0x40
96         #define AX_GPIO_CTRL_GPIO1EN    0x20
97
98 #define AX_PHYPWR_RSTCTL                        0x26
99         #define AX_PHYPWR_RSTCTL_BZ     0x0010
100         #define AX_PHYPWR_RSTCTL_IPRL   0x0020
101         #define AX_PHYPWR_RSTCTL_AT     0x1000
102
103 #define AX_RX_BULKIN_QCTRL                      0x2e
104 #define AX_CLK_SELECT                           0x33
105         #define AX_CLK_SELECT_BCS       0x01
106         #define AX_CLK_SELECT_ACS       0x02
107         #define AX_CLK_SELECT_ULR       0x08
108
109 #define AX_RXCOE_CTL                            0x34
110         #define AX_RXCOE_IP             0x01
111         #define AX_RXCOE_TCP            0x02
112         #define AX_RXCOE_UDP            0x04
113         #define AX_RXCOE_TCPV6          0x20
114         #define AX_RXCOE_UDPV6          0x40
115
116 #define AX_TXCOE_CTL                            0x35
117         #define AX_TXCOE_IP             0x01
118         #define AX_TXCOE_TCP            0x02
119         #define AX_TXCOE_UDP            0x04
120         #define AX_TXCOE_TCPV6          0x20
121         #define AX_TXCOE_UDPV6          0x40
122
123 #define AX_LEDCTRL                              0x73
124
125 #define GMII_PHY_PHYSR                          0x11
126         #define GMII_PHY_PHYSR_SMASK    0xc000
127         #define GMII_PHY_PHYSR_GIGA     0x8000
128         #define GMII_PHY_PHYSR_100      0x4000
129         #define GMII_PHY_PHYSR_FULL     0x2000
130         #define GMII_PHY_PHYSR_LINK     0x400
131
132 #define GMII_LED_ACT                            0x1a
133         #define GMII_LED_ACTIVE_MASK    0xff8f
134         #define GMII_LED0_ACTIVE        BIT(4)
135         #define GMII_LED1_ACTIVE        BIT(5)
136         #define GMII_LED2_ACTIVE        BIT(6)
137
138 #define GMII_LED_LINK                           0x1c
139         #define GMII_LED_LINK_MASK      0xf888
140         #define GMII_LED0_LINK_10       BIT(0)
141         #define GMII_LED0_LINK_100      BIT(1)
142         #define GMII_LED0_LINK_1000     BIT(2)
143         #define GMII_LED1_LINK_10       BIT(4)
144         #define GMII_LED1_LINK_100      BIT(5)
145         #define GMII_LED1_LINK_1000     BIT(6)
146         #define GMII_LED2_LINK_10       BIT(8)
147         #define GMII_LED2_LINK_100      BIT(9)
148         #define GMII_LED2_LINK_1000     BIT(10)
149         #define LED0_ACTIVE             BIT(0)
150         #define LED0_LINK_10            BIT(1)
151         #define LED0_LINK_100           BIT(2)
152         #define LED0_LINK_1000          BIT(3)
153         #define LED0_FD                 BIT(4)
154         #define LED0_USB3_MASK          0x001f
155         #define LED1_ACTIVE             BIT(5)
156         #define LED1_LINK_10            BIT(6)
157         #define LED1_LINK_100           BIT(7)
158         #define LED1_LINK_1000          BIT(8)
159         #define LED1_FD                 BIT(9)
160         #define LED1_USB3_MASK          0x03e0
161         #define LED2_ACTIVE             BIT(10)
162         #define LED2_LINK_1000          BIT(13)
163         #define LED2_LINK_100           BIT(12)
164         #define LED2_LINK_10            BIT(11)
165         #define LED2_FD                 BIT(14)
166         #define LED_VALID               BIT(15)
167         #define LED2_USB3_MASK          0x7c00
168
169 #define GMII_PHYPAGE                            0x1e
170 #define GMII_PHY_PAGE_SELECT                    0x1f
171         #define GMII_PHY_PGSEL_EXT      0x0007
172         #define GMII_PHY_PGSEL_PAGE0    0x0000
173
174 struct ax88179_data {
175         u16 rxctl;
176         u16 reserved;
177 };
178
179 struct ax88179_int_data {
180         __le32 intdata1;
181         __le32 intdata2;
182 };
183
184 static const struct {
185         unsigned char ctrl, timer_l, timer_h, size, ifg;
186 } AX88179_BULKIN_SIZE[] =       {
187         {7, 0x4f, 0,    0x12, 0xff},
188         {7, 0x20, 3,    0x16, 0xff},
189         {7, 0xae, 7,    0x18, 0xff},
190         {7, 0xcc, 0x4c, 0x18, 8},
191 };
192
193 static int __ax88179_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
194                               u16 size, void *data, int in_pm)
195 {
196         int ret;
197         int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
198
199         BUG_ON(!dev);
200
201         if (!in_pm)
202                 fn = usbnet_read_cmd;
203         else
204                 fn = usbnet_read_cmd_nopm;
205
206         ret = fn(dev, cmd, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
207                  value, index, data, size);
208
209         if (unlikely(ret < 0))
210                 netdev_warn(dev->net, "Failed to read reg index 0x%04x: %d\n",
211                             index, ret);
212
213         return ret;
214 }
215
216 static int __ax88179_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
217                                u16 size, void *data, int in_pm)
218 {
219         int ret;
220         int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
221
222         BUG_ON(!dev);
223
224         if (!in_pm)
225                 fn = usbnet_write_cmd;
226         else
227                 fn = usbnet_write_cmd_nopm;
228
229         ret = fn(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
230                  value, index, data, size);
231
232         if (unlikely(ret < 0))
233                 netdev_warn(dev->net, "Failed to write reg index 0x%04x: %d\n",
234                             index, ret);
235
236         return ret;
237 }
238
239 static void ax88179_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value,
240                                     u16 index, u16 size, void *data)
241 {
242         u16 buf;
243
244         if (2 == size) {
245                 buf = *((u16 *)data);
246                 cpu_to_le16s(&buf);
247                 usbnet_write_cmd_async(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR |
248                                        USB_RECIP_DEVICE, value, index, &buf,
249                                        size);
250         } else {
251                 usbnet_write_cmd_async(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR |
252                                        USB_RECIP_DEVICE, value, index, data,
253                                        size);
254         }
255 }
256
257 static int ax88179_read_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value,
258                                  u16 index, u16 size, void *data)
259 {
260         int ret;
261
262         if (2 == size) {
263                 u16 buf;
264                 ret = __ax88179_read_cmd(dev, cmd, value, index, size, &buf, 1);
265                 le16_to_cpus(&buf);
266                 *((u16 *)data) = buf;
267         } else if (4 == size) {
268                 u32 buf;
269                 ret = __ax88179_read_cmd(dev, cmd, value, index, size, &buf, 1);
270                 le32_to_cpus(&buf);
271                 *((u32 *)data) = buf;
272         } else {
273                 ret = __ax88179_read_cmd(dev, cmd, value, index, size, data, 1);
274         }
275
276         return ret;
277 }
278
279 static int ax88179_write_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value,
280                                   u16 index, u16 size, void *data)
281 {
282         int ret;
283
284         if (2 == size) {
285                 u16 buf;
286                 buf = *((u16 *)data);
287                 cpu_to_le16s(&buf);
288                 ret = __ax88179_write_cmd(dev, cmd, value, index,
289                                           size, &buf, 1);
290         } else {
291                 ret = __ax88179_write_cmd(dev, cmd, value, index,
292                                           size, data, 1);
293         }
294
295         return ret;
296 }
297
298 static int ax88179_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
299                             u16 size, void *data)
300 {
301         int ret;
302
303         if (2 == size) {
304                 u16 buf;
305                 ret = __ax88179_read_cmd(dev, cmd, value, index, size, &buf, 0);
306                 le16_to_cpus(&buf);
307                 *((u16 *)data) = buf;
308         } else if (4 == size) {
309                 u32 buf;
310                 ret = __ax88179_read_cmd(dev, cmd, value, index, size, &buf, 0);
311                 le32_to_cpus(&buf);
312                 *((u32 *)data) = buf;
313         } else {
314                 ret = __ax88179_read_cmd(dev, cmd, value, index, size, data, 0);
315         }
316
317         return ret;
318 }
319
320 static int ax88179_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
321                              u16 size, void *data)
322 {
323         int ret;
324
325         if (2 == size) {
326                 u16 buf;
327                 buf = *((u16 *)data);
328                 cpu_to_le16s(&buf);
329                 ret = __ax88179_write_cmd(dev, cmd, value, index,
330                                           size, &buf, 0);
331         } else {
332                 ret = __ax88179_write_cmd(dev, cmd, value, index,
333                                           size, data, 0);
334         }
335
336         return ret;
337 }
338
339 static void ax88179_status(struct usbnet *dev, struct urb *urb)
340 {
341         struct ax88179_int_data *event;
342         u32 link;
343
344         if (urb->actual_length < 8)
345                 return;
346
347         event = urb->transfer_buffer;
348         le32_to_cpus((void *)&event->intdata1);
349
350         link = (((__force u32)event->intdata1) & AX_INT_PPLS_LINK) >> 16;
351
352         if (netif_carrier_ok(dev->net) != link) {
353                 usbnet_link_change(dev, link, 1);
354                 netdev_info(dev->net, "ax88179 - Link status is: %d\n", link);
355         }
356 }
357
358 static int ax88179_mdio_read(struct net_device *netdev, int phy_id, int loc)
359 {
360         struct usbnet *dev = netdev_priv(netdev);
361         u16 res;
362
363         ax88179_read_cmd(dev, AX_ACCESS_PHY, phy_id, (__u16)loc, 2, &res);
364         return res;
365 }
366
367 static void ax88179_mdio_write(struct net_device *netdev, int phy_id, int loc,
368                                int val)
369 {
370         struct usbnet *dev = netdev_priv(netdev);
371         u16 res = (u16) val;
372
373         ax88179_write_cmd(dev, AX_ACCESS_PHY, phy_id, (__u16)loc, 2, &res);
374 }
375
376 static int ax88179_suspend(struct usb_interface *intf, pm_message_t message)
377 {
378         struct usbnet *dev = usb_get_intfdata(intf);
379         u16 tmp16;
380         u8 tmp8;
381
382         usbnet_suspend(intf, message);
383
384         /* Disable RX path */
385         ax88179_read_cmd_nopm(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
386                               2, 2, &tmp16);
387         tmp16 &= ~AX_MEDIUM_RECEIVE_EN;
388         ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
389                                2, 2, &tmp16);
390
391         /* Force bulk-in zero length */
392         ax88179_read_cmd_nopm(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL,
393                               2, 2, &tmp16);
394
395         tmp16 |= AX_PHYPWR_RSTCTL_BZ | AX_PHYPWR_RSTCTL_IPRL;
396         ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL,
397                                2, 2, &tmp16);
398
399         /* change clock */
400         tmp8 = 0;
401         ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, &tmp8);
402
403         /* Configure RX control register => stop operation */
404         tmp16 = AX_RX_CTL_STOP;
405         ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, &tmp16);
406
407         return 0;
408 }
409
410 /* This function is used to enable the autodetach function. */
411 /* This function is determined by offset 0x43 of EEPROM */
412 static int ax88179_auto_detach(struct usbnet *dev, int in_pm)
413 {
414         u16 tmp16;
415         u8 tmp8;
416         int (*fnr)(struct usbnet *, u8, u16, u16, u16, void *);
417         int (*fnw)(struct usbnet *, u8, u16, u16, u16, void *);
418
419         if (!in_pm) {
420                 fnr = ax88179_read_cmd;
421                 fnw = ax88179_write_cmd;
422         } else {
423                 fnr = ax88179_read_cmd_nopm;
424                 fnw = ax88179_write_cmd_nopm;
425         }
426
427         if (fnr(dev, AX_ACCESS_EEPROM, 0x43, 1, 2, &tmp16) < 0)
428                 return 0;
429
430         if ((tmp16 == 0xFFFF) || (!(tmp16 & 0x0100)))
431                 return 0;
432
433         /* Enable Auto Detach bit */
434         tmp8 = 0;
435         fnr(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, &tmp8);
436         tmp8 |= AX_CLK_SELECT_ULR;
437         fnw(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, &tmp8);
438
439         fnr(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, &tmp16);
440         tmp16 |= AX_PHYPWR_RSTCTL_AT;
441         fnw(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, &tmp16);
442
443         return 0;
444 }
445
446 static int ax88179_resume(struct usb_interface *intf)
447 {
448         struct usbnet *dev = usb_get_intfdata(intf);
449         u16 tmp16;
450         u8 tmp8;
451
452         usbnet_link_change(dev, 0, 0);
453
454         /* Power up ethernet PHY */
455         tmp16 = 0;
456         ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL,
457                                2, 2, &tmp16);
458         udelay(1000);
459
460         tmp16 = AX_PHYPWR_RSTCTL_IPRL;
461         ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL,
462                                2, 2, &tmp16);
463         msleep(200);
464
465         /* Ethernet PHY Auto Detach*/
466         ax88179_auto_detach(dev, 1);
467
468         /* Enable clock */
469         ax88179_read_cmd_nopm(dev, AX_ACCESS_MAC,  AX_CLK_SELECT, 1, 1, &tmp8);
470         tmp8 |= AX_CLK_SELECT_ACS | AX_CLK_SELECT_BCS;
471         ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, &tmp8);
472         msleep(100);
473
474         /* Configure RX control register => start operation */
475         tmp16 = AX_RX_CTL_DROPCRCERR | AX_RX_CTL_IPE | AX_RX_CTL_START |
476                 AX_RX_CTL_AP | AX_RX_CTL_AMALL | AX_RX_CTL_AB;
477         ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, &tmp16);
478
479         return usbnet_resume(intf);
480 }
481
482 static void
483 ax88179_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
484 {
485         struct usbnet *dev = netdev_priv(net);
486         u8 opt;
487
488         if (ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_MONITOR_MOD,
489                              1, 1, &opt) < 0) {
490                 wolinfo->supported = 0;
491                 wolinfo->wolopts = 0;
492                 return;
493         }
494
495         wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
496         wolinfo->wolopts = 0;
497         if (opt & AX_MONITOR_MODE_RWLC)
498                 wolinfo->wolopts |= WAKE_PHY;
499         if (opt & AX_MONITOR_MODE_RWMP)
500                 wolinfo->wolopts |= WAKE_MAGIC;
501 }
502
503 static int
504 ax88179_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
505 {
506         struct usbnet *dev = netdev_priv(net);
507         u8 opt = 0;
508
509         if (wolinfo->wolopts & ~(WAKE_PHY | WAKE_MAGIC))
510                 return -EINVAL;
511
512         if (wolinfo->wolopts & WAKE_PHY)
513                 opt |= AX_MONITOR_MODE_RWLC;
514         if (wolinfo->wolopts & WAKE_MAGIC)
515                 opt |= AX_MONITOR_MODE_RWMP;
516
517         if (ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MONITOR_MOD,
518                               1, 1, &opt) < 0)
519                 return -EINVAL;
520
521         return 0;
522 }
523
524 static int ax88179_get_eeprom_len(struct net_device *net)
525 {
526         return AX_EEPROM_LEN;
527 }
528
529 static int
530 ax88179_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
531                    u8 *data)
532 {
533         struct usbnet *dev = netdev_priv(net);
534         u16 *eeprom_buff;
535         int first_word, last_word;
536         int i, ret;
537
538         if (eeprom->len == 0)
539                 return -EINVAL;
540
541         eeprom->magic = AX88179_EEPROM_MAGIC;
542
543         first_word = eeprom->offset >> 1;
544         last_word = (eeprom->offset + eeprom->len - 1) >> 1;
545         eeprom_buff = kmalloc(sizeof(u16) * (last_word - first_word + 1),
546                               GFP_KERNEL);
547         if (!eeprom_buff)
548                 return -ENOMEM;
549
550         /* ax88179/178A returns 2 bytes from eeprom on read */
551         for (i = first_word; i <= last_word; i++) {
552                 ret = __ax88179_read_cmd(dev, AX_ACCESS_EEPROM, i, 1, 2,
553                                          &eeprom_buff[i - first_word],
554                                          0);
555                 if (ret < 0) {
556                         kfree(eeprom_buff);
557                         return -EIO;
558                 }
559         }
560
561         memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
562         kfree(eeprom_buff);
563         return 0;
564 }
565
566 static int ax88179_get_settings(struct net_device *net, struct ethtool_cmd *cmd)
567 {
568         struct usbnet *dev = netdev_priv(net);
569         return mii_ethtool_gset(&dev->mii, cmd);
570 }
571
572 static int ax88179_set_settings(struct net_device *net, struct ethtool_cmd *cmd)
573 {
574         struct usbnet *dev = netdev_priv(net);
575         return mii_ethtool_sset(&dev->mii, cmd);
576 }
577
578
579 static int ax88179_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
580 {
581         struct usbnet *dev = netdev_priv(net);
582         return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
583 }
584
585 static const struct ethtool_ops ax88179_ethtool_ops = {
586         .get_link               = ethtool_op_get_link,
587         .get_msglevel           = usbnet_get_msglevel,
588         .set_msglevel           = usbnet_set_msglevel,
589         .get_wol                = ax88179_get_wol,
590         .set_wol                = ax88179_set_wol,
591         .get_eeprom_len         = ax88179_get_eeprom_len,
592         .get_eeprom             = ax88179_get_eeprom,
593         .get_settings           = ax88179_get_settings,
594         .set_settings           = ax88179_set_settings,
595         .nway_reset             = usbnet_nway_reset,
596 };
597
598 static void ax88179_set_multicast(struct net_device *net)
599 {
600         struct usbnet *dev = netdev_priv(net);
601         struct ax88179_data *data = (struct ax88179_data *)dev->data;
602         u8 *m_filter = ((u8 *)dev->data) + 12;
603
604         data->rxctl = (AX_RX_CTL_START | AX_RX_CTL_AB | AX_RX_CTL_IPE);
605
606         if (net->flags & IFF_PROMISC) {
607                 data->rxctl |= AX_RX_CTL_PRO;
608         } else if (net->flags & IFF_ALLMULTI ||
609                    netdev_mc_count(net) > AX_MAX_MCAST) {
610                 data->rxctl |= AX_RX_CTL_AMALL;
611         } else if (netdev_mc_empty(net)) {
612                 /* just broadcast and directed */
613         } else {
614                 /* We use the 20 byte dev->data for our 8 byte filter buffer
615                  * to avoid allocating memory that is tricky to free later
616                  */
617                 u32 crc_bits;
618                 struct netdev_hw_addr *ha;
619
620                 memset(m_filter, 0, AX_MCAST_FLTSIZE);
621
622                 netdev_for_each_mc_addr(ha, net) {
623                         crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
624                         *(m_filter + (crc_bits >> 3)) |= (1 << (crc_bits & 7));
625                 }
626
627                 ax88179_write_cmd_async(dev, AX_ACCESS_MAC, AX_MULFLTARY,
628                                         AX_MCAST_FLTSIZE, AX_MCAST_FLTSIZE,
629                                         m_filter);
630
631                 data->rxctl |= AX_RX_CTL_AM;
632         }
633
634         ax88179_write_cmd_async(dev, AX_ACCESS_MAC, AX_RX_CTL,
635                                 2, 2, &data->rxctl);
636 }
637
638 static int
639 ax88179_set_features(struct net_device *net, netdev_features_t features)
640 {
641         u8 tmp;
642         struct usbnet *dev = netdev_priv(net);
643         netdev_features_t changed = net->features ^ features;
644
645         if (changed & NETIF_F_IP_CSUM) {
646                 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, &tmp);
647                 tmp ^= AX_TXCOE_TCP | AX_TXCOE_UDP;
648                 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, &tmp);
649         }
650
651         if (changed & NETIF_F_IPV6_CSUM) {
652                 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, &tmp);
653                 tmp ^= AX_TXCOE_TCPV6 | AX_TXCOE_UDPV6;
654                 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, &tmp);
655         }
656
657         if (changed & NETIF_F_RXCSUM) {
658                 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_RXCOE_CTL, 1, 1, &tmp);
659                 tmp ^= AX_RXCOE_IP | AX_RXCOE_TCP | AX_RXCOE_UDP |
660                        AX_RXCOE_TCPV6 | AX_RXCOE_UDPV6;
661                 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RXCOE_CTL, 1, 1, &tmp);
662         }
663
664         return 0;
665 }
666
667 static int ax88179_change_mtu(struct net_device *net, int new_mtu)
668 {
669         struct usbnet *dev = netdev_priv(net);
670         u16 tmp16;
671
672         if (new_mtu <= 0 || new_mtu > 4088)
673                 return -EINVAL;
674
675         net->mtu = new_mtu;
676         dev->hard_mtu = net->mtu + net->hard_header_len;
677
678         if (net->mtu > 1500) {
679                 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
680                                  2, 2, &tmp16);
681                 tmp16 |= AX_MEDIUM_JUMBO_EN;
682                 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
683                                   2, 2, &tmp16);
684         } else {
685                 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
686                                  2, 2, &tmp16);
687                 tmp16 &= ~AX_MEDIUM_JUMBO_EN;
688                 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
689                                   2, 2, &tmp16);
690         }
691
692         /* max qlen depend on hard_mtu and rx_urb_size */
693         usbnet_update_max_qlen(dev);
694
695         return 0;
696 }
697
698 static int ax88179_set_mac_addr(struct net_device *net, void *p)
699 {
700         struct usbnet *dev = netdev_priv(net);
701         struct sockaddr *addr = p;
702         int ret;
703
704         if (netif_running(net))
705                 return -EBUSY;
706         if (!is_valid_ether_addr(addr->sa_data))
707                 return -EADDRNOTAVAIL;
708
709         memcpy(net->dev_addr, addr->sa_data, ETH_ALEN);
710
711         /* Set the MAC address */
712         ret = ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_NODE_ID, ETH_ALEN,
713                                  ETH_ALEN, net->dev_addr);
714         if (ret < 0)
715                 return ret;
716
717         return 0;
718 }
719
720 static const struct net_device_ops ax88179_netdev_ops = {
721         .ndo_open               = usbnet_open,
722         .ndo_stop               = usbnet_stop,
723         .ndo_start_xmit         = usbnet_start_xmit,
724         .ndo_tx_timeout         = usbnet_tx_timeout,
725         .ndo_change_mtu         = ax88179_change_mtu,
726         .ndo_set_mac_address    = ax88179_set_mac_addr,
727         .ndo_validate_addr      = eth_validate_addr,
728         .ndo_do_ioctl           = ax88179_ioctl,
729         .ndo_set_rx_mode        = ax88179_set_multicast,
730         .ndo_set_features       = ax88179_set_features,
731 };
732
733 static int ax88179_check_eeprom(struct usbnet *dev)
734 {
735         u8 i, buf, eeprom[20];
736         u16 csum, delay = HZ / 10;
737         unsigned long jtimeout;
738
739         /* Read EEPROM content */
740         for (i = 0; i < 6; i++) {
741                 buf = i;
742                 if (ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_SROM_ADDR,
743                                       1, 1, &buf) < 0)
744                         return -EINVAL;
745
746                 buf = EEP_RD;
747                 if (ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_SROM_CMD,
748                                       1, 1, &buf) < 0)
749                         return -EINVAL;
750
751                 jtimeout = jiffies + delay;
752                 do {
753                         ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_SROM_CMD,
754                                          1, 1, &buf);
755
756                         if (time_after(jiffies, jtimeout))
757                                 return -EINVAL;
758
759                 } while (buf & EEP_BUSY);
760
761                 __ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_SROM_DATA_LOW,
762                                    2, 2, &eeprom[i * 2], 0);
763
764                 if ((i == 0) && (eeprom[0] == 0xFF))
765                         return -EINVAL;
766         }
767
768         csum = eeprom[6] + eeprom[7] + eeprom[8] + eeprom[9];
769         csum = (csum >> 8) + (csum & 0xff);
770         if ((csum + eeprom[10]) != 0xff)
771                 return -EINVAL;
772
773         return 0;
774 }
775
776 static int ax88179_check_efuse(struct usbnet *dev, u16 *ledmode)
777 {
778         u8      i;
779         u8      efuse[64];
780         u16     csum = 0;
781
782         if (ax88179_read_cmd(dev, AX_ACCESS_EFUS, 0, 64, 64, efuse) < 0)
783                 return -EINVAL;
784
785         if (*efuse == 0xFF)
786                 return -EINVAL;
787
788         for (i = 0; i < 64; i++)
789                 csum = csum + efuse[i];
790
791         while (csum > 255)
792                 csum = (csum & 0x00FF) + ((csum >> 8) & 0x00FF);
793
794         if (csum != 0xFF)
795                 return -EINVAL;
796
797         *ledmode = (efuse[51] << 8) | efuse[52];
798
799         return 0;
800 }
801
802 static int ax88179_convert_old_led(struct usbnet *dev, u16 *ledvalue)
803 {
804         u16 led;
805
806         /* Loaded the old eFuse LED Mode */
807         if (ax88179_read_cmd(dev, AX_ACCESS_EEPROM, 0x3C, 1, 2, &led) < 0)
808                 return -EINVAL;
809
810         led >>= 8;
811         switch (led) {
812         case 0xFF:
813                 led = LED0_ACTIVE | LED1_LINK_10 | LED1_LINK_100 |
814                       LED1_LINK_1000 | LED2_ACTIVE | LED2_LINK_10 |
815                       LED2_LINK_100 | LED2_LINK_1000 | LED_VALID;
816                 break;
817         case 0xFE:
818                 led = LED0_ACTIVE | LED1_LINK_1000 | LED2_LINK_100 | LED_VALID;
819                 break;
820         case 0xFD:
821                 led = LED0_ACTIVE | LED1_LINK_1000 | LED2_LINK_100 |
822                       LED2_LINK_10 | LED_VALID;
823                 break;
824         case 0xFC:
825                 led = LED0_ACTIVE | LED1_ACTIVE | LED1_LINK_1000 | LED2_ACTIVE |
826                       LED2_LINK_100 | LED2_LINK_10 | LED_VALID;
827                 break;
828         default:
829                 led = LED0_ACTIVE | LED1_LINK_10 | LED1_LINK_100 |
830                       LED1_LINK_1000 | LED2_ACTIVE | LED2_LINK_10 |
831                       LED2_LINK_100 | LED2_LINK_1000 | LED_VALID;
832                 break;
833         }
834
835         *ledvalue = led;
836
837         return 0;
838 }
839
840 static int ax88179_led_setting(struct usbnet *dev)
841 {
842         u8 ledfd, value = 0;
843         u16 tmp, ledact, ledlink, ledvalue = 0, delay = HZ / 10;
844         unsigned long jtimeout;
845
846         /* Check AX88179 version. UA1 or UA2*/
847         ax88179_read_cmd(dev, AX_ACCESS_MAC, GENERAL_STATUS, 1, 1, &value);
848
849         if (!(value & AX_SECLD)) {      /* UA1 */
850                 value = AX_GPIO_CTRL_GPIO3EN | AX_GPIO_CTRL_GPIO2EN |
851                         AX_GPIO_CTRL_GPIO1EN;
852                 if (ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_GPIO_CTRL,
853                                       1, 1, &value) < 0)
854                         return -EINVAL;
855         }
856
857         /* Check EEPROM */
858         if (!ax88179_check_eeprom(dev)) {
859                 value = 0x42;
860                 if (ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_SROM_ADDR,
861                                       1, 1, &value) < 0)
862                         return -EINVAL;
863
864                 value = EEP_RD;
865                 if (ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_SROM_CMD,
866                                       1, 1, &value) < 0)
867                         return -EINVAL;
868
869                 jtimeout = jiffies + delay;
870                 do {
871                         ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_SROM_CMD,
872                                          1, 1, &value);
873
874                         if (time_after(jiffies, jtimeout))
875                                 return -EINVAL;
876
877                 } while (value & EEP_BUSY);
878
879                 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_SROM_DATA_HIGH,
880                                  1, 1, &value);
881                 ledvalue = (value << 8);
882
883                 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_SROM_DATA_LOW,
884                                  1, 1, &value);
885                 ledvalue |= value;
886
887                 /* load internal ROM for defaule setting */
888                 if ((ledvalue == 0xFFFF) || ((ledvalue & LED_VALID) == 0))
889                         ax88179_convert_old_led(dev, &ledvalue);
890
891         } else if (!ax88179_check_efuse(dev, &ledvalue)) {
892                 if ((ledvalue == 0xFFFF) || ((ledvalue & LED_VALID) == 0))
893                         ax88179_convert_old_led(dev, &ledvalue);
894         } else {
895                 ax88179_convert_old_led(dev, &ledvalue);
896         }
897
898         tmp = GMII_PHY_PGSEL_EXT;
899         ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
900                           GMII_PHY_PAGE_SELECT, 2, &tmp);
901
902         tmp = 0x2c;
903         ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
904                           GMII_PHYPAGE, 2, &tmp);
905
906         ax88179_read_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
907                          GMII_LED_ACT, 2, &ledact);
908
909         ax88179_read_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
910                          GMII_LED_LINK, 2, &ledlink);
911
912         ledact &= GMII_LED_ACTIVE_MASK;
913         ledlink &= GMII_LED_LINK_MASK;
914
915         if (ledvalue & LED0_ACTIVE)
916                 ledact |= GMII_LED0_ACTIVE;
917
918         if (ledvalue & LED1_ACTIVE)
919                 ledact |= GMII_LED1_ACTIVE;
920
921         if (ledvalue & LED2_ACTIVE)
922                 ledact |= GMII_LED2_ACTIVE;
923
924         if (ledvalue & LED0_LINK_10)
925                 ledlink |= GMII_LED0_LINK_10;
926
927         if (ledvalue & LED1_LINK_10)
928                 ledlink |= GMII_LED1_LINK_10;
929
930         if (ledvalue & LED2_LINK_10)
931                 ledlink |= GMII_LED2_LINK_10;
932
933         if (ledvalue & LED0_LINK_100)
934                 ledlink |= GMII_LED0_LINK_100;
935
936         if (ledvalue & LED1_LINK_100)
937                 ledlink |= GMII_LED1_LINK_100;
938
939         if (ledvalue & LED2_LINK_100)
940                 ledlink |= GMII_LED2_LINK_100;
941
942         if (ledvalue & LED0_LINK_1000)
943                 ledlink |= GMII_LED0_LINK_1000;
944
945         if (ledvalue & LED1_LINK_1000)
946                 ledlink |= GMII_LED1_LINK_1000;
947
948         if (ledvalue & LED2_LINK_1000)
949                 ledlink |= GMII_LED2_LINK_1000;
950
951         tmp = ledact;
952         ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
953                           GMII_LED_ACT, 2, &tmp);
954
955         tmp = ledlink;
956         ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
957                           GMII_LED_LINK, 2, &tmp);
958
959         tmp = GMII_PHY_PGSEL_PAGE0;
960         ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
961                           GMII_PHY_PAGE_SELECT, 2, &tmp);
962
963         /* LED full duplex setting */
964         ledfd = 0;
965         if (ledvalue & LED0_FD)
966                 ledfd |= 0x01;
967         else if ((ledvalue & LED0_USB3_MASK) == 0)
968                 ledfd |= 0x02;
969
970         if (ledvalue & LED1_FD)
971                 ledfd |= 0x04;
972         else if ((ledvalue & LED1_USB3_MASK) == 0)
973                 ledfd |= 0x08;
974
975         if (ledvalue & LED2_FD)
976                 ledfd |= 0x10;
977         else if ((ledvalue & LED2_USB3_MASK) == 0)
978                 ledfd |= 0x20;
979
980         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_LEDCTRL, 1, 1, &ledfd);
981
982         return 0;
983 }
984
985 static int ax88179_bind(struct usbnet *dev, struct usb_interface *intf)
986 {
987         u8 buf[5];
988         u16 *tmp16;
989         u8 *tmp;
990         struct ax88179_data *ax179_data = (struct ax88179_data *)dev->data;
991
992         usbnet_get_endpoints(dev, intf);
993
994         tmp16 = (u16 *)buf;
995         tmp = (u8 *)buf;
996
997         memset(ax179_data, 0, sizeof(*ax179_data));
998
999         /* Power up ethernet PHY */
1000         *tmp16 = 0;
1001         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, tmp16);
1002         *tmp16 = AX_PHYPWR_RSTCTL_IPRL;
1003         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, tmp16);
1004         msleep(200);
1005
1006         *tmp = AX_CLK_SELECT_ACS | AX_CLK_SELECT_BCS;
1007         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, tmp);
1008         msleep(100);
1009
1010         ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_NODE_ID, ETH_ALEN,
1011                          ETH_ALEN, dev->net->dev_addr);
1012         memcpy(dev->net->perm_addr, dev->net->dev_addr, ETH_ALEN);
1013
1014         /* RX bulk configuration */
1015         memcpy(tmp, &AX88179_BULKIN_SIZE[0], 5);
1016         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_BULKIN_QCTRL, 5, 5, tmp);
1017
1018         dev->rx_urb_size = 1024 * 20;
1019
1020         *tmp = 0x34;
1021         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PAUSE_WATERLVL_LOW, 1, 1, tmp);
1022
1023         *tmp = 0x52;
1024         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PAUSE_WATERLVL_HIGH,
1025                           1, 1, tmp);
1026
1027         dev->net->netdev_ops = &ax88179_netdev_ops;
1028         dev->net->ethtool_ops = &ax88179_ethtool_ops;
1029         dev->net->needed_headroom = 8;
1030
1031         /* Initialize MII structure */
1032         dev->mii.dev = dev->net;
1033         dev->mii.mdio_read = ax88179_mdio_read;
1034         dev->mii.mdio_write = ax88179_mdio_write;
1035         dev->mii.phy_id_mask = 0xff;
1036         dev->mii.reg_num_mask = 0xff;
1037         dev->mii.phy_id = 0x03;
1038         dev->mii.supports_gmii = 1;
1039
1040         dev->net->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1041                               NETIF_F_RXCSUM;
1042
1043         dev->net->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1044                                  NETIF_F_RXCSUM;
1045
1046         /* Enable checksum offload */
1047         *tmp = AX_RXCOE_IP | AX_RXCOE_TCP | AX_RXCOE_UDP |
1048                AX_RXCOE_TCPV6 | AX_RXCOE_UDPV6;
1049         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RXCOE_CTL, 1, 1, tmp);
1050
1051         *tmp = AX_TXCOE_IP | AX_TXCOE_TCP | AX_TXCOE_UDP |
1052                AX_TXCOE_TCPV6 | AX_TXCOE_UDPV6;
1053         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, tmp);
1054
1055         /* Configure RX control register => start operation */
1056         *tmp16 = AX_RX_CTL_DROPCRCERR | AX_RX_CTL_IPE | AX_RX_CTL_START |
1057                  AX_RX_CTL_AP | AX_RX_CTL_AMALL | AX_RX_CTL_AB;
1058         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, tmp16);
1059
1060         *tmp = AX_MONITOR_MODE_PMETYPE | AX_MONITOR_MODE_PMEPOL |
1061                AX_MONITOR_MODE_RWMP;
1062         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MONITOR_MOD, 1, 1, tmp);
1063
1064         /* Configure default medium type => giga */
1065         *tmp16 = AX_MEDIUM_RECEIVE_EN | AX_MEDIUM_TXFLOW_CTRLEN |
1066                  AX_MEDIUM_RXFLOW_CTRLEN | AX_MEDIUM_FULL_DUPLEX |
1067                  AX_MEDIUM_GIGAMODE;
1068         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
1069                           2, 2, tmp16);
1070
1071         ax88179_led_setting(dev);
1072
1073         /* Restart autoneg */
1074         mii_nway_restart(&dev->mii);
1075
1076         usbnet_link_change(dev, 0, 0);
1077
1078         return 0;
1079 }
1080
1081 static void ax88179_unbind(struct usbnet *dev, struct usb_interface *intf)
1082 {
1083         u16 tmp16;
1084
1085         /* Configure RX control register => stop operation */
1086         tmp16 = AX_RX_CTL_STOP;
1087         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, &tmp16);
1088
1089         tmp16 = 0;
1090         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, &tmp16);
1091
1092         /* Power down ethernet PHY */
1093         tmp16 = 0;
1094         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, &tmp16);
1095 }
1096
1097 static void
1098 ax88179_rx_checksum(struct sk_buff *skb, u32 *pkt_hdr)
1099 {
1100         skb->ip_summed = CHECKSUM_NONE;
1101
1102         /* checksum error bit is set */
1103         if ((*pkt_hdr & AX_RXHDR_L3CSUM_ERR) ||
1104             (*pkt_hdr & AX_RXHDR_L4CSUM_ERR))
1105                 return;
1106
1107         /* It must be a TCP or UDP packet with a valid checksum */
1108         if (((*pkt_hdr & AX_RXHDR_L4_TYPE_MASK) == AX_RXHDR_L4_TYPE_TCP) ||
1109             ((*pkt_hdr & AX_RXHDR_L4_TYPE_MASK) == AX_RXHDR_L4_TYPE_UDP))
1110                 skb->ip_summed = CHECKSUM_UNNECESSARY;
1111 }
1112
1113 static int ax88179_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
1114 {
1115         struct sk_buff *ax_skb;
1116         int pkt_cnt;
1117         u32 rx_hdr;
1118         u16 hdr_off;
1119         u32 *pkt_hdr;
1120
1121         /* This check is no longer done by usbnet */
1122         if (skb->len < dev->net->hard_header_len)
1123                 return 0;
1124
1125         skb_trim(skb, skb->len - 4);
1126         memcpy(&rx_hdr, skb_tail_pointer(skb), 4);
1127         le32_to_cpus(&rx_hdr);
1128
1129         pkt_cnt = (u16)rx_hdr;
1130         hdr_off = (u16)(rx_hdr >> 16);
1131         pkt_hdr = (u32 *)(skb->data + hdr_off);
1132
1133         while (pkt_cnt--) {
1134                 u16 pkt_len;
1135
1136                 le32_to_cpus(pkt_hdr);
1137                 pkt_len = (*pkt_hdr >> 16) & 0x1fff;
1138
1139                 /* Check CRC or runt packet */
1140                 if ((*pkt_hdr & AX_RXHDR_CRC_ERR) ||
1141                     (*pkt_hdr & AX_RXHDR_DROP_ERR)) {
1142                         skb_pull(skb, (pkt_len + 7) & 0xFFF8);
1143                         pkt_hdr++;
1144                         continue;
1145                 }
1146
1147                 if (pkt_cnt == 0) {
1148                         /* Skip IP alignment psudo header */
1149                         skb_pull(skb, 2);
1150                         skb->len = pkt_len;
1151                         skb_set_tail_pointer(skb, pkt_len);
1152                         skb->truesize = pkt_len + sizeof(struct sk_buff);
1153                         ax88179_rx_checksum(skb, pkt_hdr);
1154                         return 1;
1155                 }
1156
1157                 ax_skb = skb_clone(skb, GFP_ATOMIC);
1158                 if (ax_skb) {
1159                         ax_skb->len = pkt_len;
1160                         ax_skb->data = skb->data + 2;
1161                         skb_set_tail_pointer(ax_skb, pkt_len);
1162                         ax_skb->truesize = pkt_len + sizeof(struct sk_buff);
1163                         ax88179_rx_checksum(ax_skb, pkt_hdr);
1164                         usbnet_skb_return(dev, ax_skb);
1165                 } else {
1166                         return 0;
1167                 }
1168
1169                 skb_pull(skb, (pkt_len + 7) & 0xFFF8);
1170                 pkt_hdr++;
1171         }
1172         return 1;
1173 }
1174
1175 static struct sk_buff *
1176 ax88179_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
1177 {
1178         u32 tx_hdr1, tx_hdr2;
1179         int frame_size = dev->maxpacket;
1180         int mss = skb_shinfo(skb)->gso_size;
1181         int headroom;
1182
1183         tx_hdr1 = skb->len;
1184         tx_hdr2 = mss;
1185         if (((skb->len + 8) % frame_size) == 0)
1186                 tx_hdr2 |= 0x80008000;  /* Enable padding */
1187
1188         headroom = skb_headroom(skb) - 8;
1189
1190         if ((skb_header_cloned(skb) || headroom < 0) &&
1191             pskb_expand_head(skb, headroom < 0 ? 8 : 0, 0, GFP_ATOMIC)) {
1192                 dev_kfree_skb_any(skb);
1193                 return NULL;
1194         }
1195
1196         skb_push(skb, 4);
1197         cpu_to_le32s(&tx_hdr2);
1198         skb_copy_to_linear_data(skb, &tx_hdr2, 4);
1199
1200         skb_push(skb, 4);
1201         cpu_to_le32s(&tx_hdr1);
1202         skb_copy_to_linear_data(skb, &tx_hdr1, 4);
1203
1204         return skb;
1205 }
1206
1207 static int ax88179_link_reset(struct usbnet *dev)
1208 {
1209         struct ax88179_data *ax179_data = (struct ax88179_data *)dev->data;
1210         u8 tmp[5], link_sts;
1211         u16 mode, tmp16, delay = HZ / 10;
1212         u32 tmp32 = 0x40000000;
1213         unsigned long jtimeout;
1214
1215         jtimeout = jiffies + delay;
1216         while (tmp32 & 0x40000000) {
1217                 mode = 0;
1218                 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, &mode);
1219                 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2,
1220                                   &ax179_data->rxctl);
1221
1222                 /*link up, check the usb device control TX FIFO full or empty*/
1223                 ax88179_read_cmd(dev, 0x81, 0x8c, 0, 4, &tmp32);
1224
1225                 if (time_after(jiffies, jtimeout))
1226                         return 0;
1227         }
1228
1229         mode = AX_MEDIUM_RECEIVE_EN | AX_MEDIUM_TXFLOW_CTRLEN |
1230                AX_MEDIUM_RXFLOW_CTRLEN;
1231
1232         ax88179_read_cmd(dev, AX_ACCESS_MAC, PHYSICAL_LINK_STATUS,
1233                          1, 1, &link_sts);
1234
1235         ax88179_read_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1236                          GMII_PHY_PHYSR, 2, &tmp16);
1237
1238         if (!(tmp16 & GMII_PHY_PHYSR_LINK)) {
1239                 return 0;
1240         } else if (GMII_PHY_PHYSR_GIGA == (tmp16 & GMII_PHY_PHYSR_SMASK)) {
1241                 mode |= AX_MEDIUM_GIGAMODE | AX_MEDIUM_EN_125MHZ;
1242                 if (dev->net->mtu > 1500)
1243                         mode |= AX_MEDIUM_JUMBO_EN;
1244
1245                 if (link_sts & AX_USB_SS)
1246                         memcpy(tmp, &AX88179_BULKIN_SIZE[0], 5);
1247                 else if (link_sts & AX_USB_HS)
1248                         memcpy(tmp, &AX88179_BULKIN_SIZE[1], 5);
1249                 else
1250                         memcpy(tmp, &AX88179_BULKIN_SIZE[3], 5);
1251         } else if (GMII_PHY_PHYSR_100 == (tmp16 & GMII_PHY_PHYSR_SMASK)) {
1252                 mode |= AX_MEDIUM_PS;
1253
1254                 if (link_sts & (AX_USB_SS | AX_USB_HS))
1255                         memcpy(tmp, &AX88179_BULKIN_SIZE[2], 5);
1256                 else
1257                         memcpy(tmp, &AX88179_BULKIN_SIZE[3], 5);
1258         } else {
1259                 memcpy(tmp, &AX88179_BULKIN_SIZE[3], 5);
1260         }
1261
1262         /* RX bulk configuration */
1263         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_BULKIN_QCTRL, 5, 5, tmp);
1264
1265         dev->rx_urb_size = (1024 * (tmp[3] + 2));
1266
1267         if (tmp16 & GMII_PHY_PHYSR_FULL)
1268                 mode |= AX_MEDIUM_FULL_DUPLEX;
1269         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
1270                           2, 2, &mode);
1271
1272         netif_carrier_on(dev->net);
1273
1274         return 0;
1275 }
1276
1277 static int ax88179_reset(struct usbnet *dev)
1278 {
1279         u8 buf[5];
1280         u16 *tmp16;
1281         u8 *tmp;
1282
1283         tmp16 = (u16 *)buf;
1284         tmp = (u8 *)buf;
1285
1286         /* Power up ethernet PHY */
1287         *tmp16 = 0;
1288         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, tmp16);
1289
1290         *tmp16 = AX_PHYPWR_RSTCTL_IPRL;
1291         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, tmp16);
1292         msleep(200);
1293
1294         *tmp = AX_CLK_SELECT_ACS | AX_CLK_SELECT_BCS;
1295         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, tmp);
1296         msleep(100);
1297
1298         /* Ethernet PHY Auto Detach*/
1299         ax88179_auto_detach(dev, 0);
1300
1301         ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_NODE_ID, ETH_ALEN, ETH_ALEN,
1302                          dev->net->dev_addr);
1303         memcpy(dev->net->perm_addr, dev->net->dev_addr, ETH_ALEN);
1304
1305         /* RX bulk configuration */
1306         memcpy(tmp, &AX88179_BULKIN_SIZE[0], 5);
1307         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_BULKIN_QCTRL, 5, 5, tmp);
1308
1309         dev->rx_urb_size = 1024 * 20;
1310
1311         *tmp = 0x34;
1312         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PAUSE_WATERLVL_LOW, 1, 1, tmp);
1313
1314         *tmp = 0x52;
1315         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PAUSE_WATERLVL_HIGH,
1316                           1, 1, tmp);
1317
1318         dev->net->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1319                               NETIF_F_RXCSUM;
1320
1321         dev->net->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1322                                  NETIF_F_RXCSUM;
1323
1324         /* Enable checksum offload */
1325         *tmp = AX_RXCOE_IP | AX_RXCOE_TCP | AX_RXCOE_UDP |
1326                AX_RXCOE_TCPV6 | AX_RXCOE_UDPV6;
1327         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RXCOE_CTL, 1, 1, tmp);
1328
1329         *tmp = AX_TXCOE_IP | AX_TXCOE_TCP | AX_TXCOE_UDP |
1330                AX_TXCOE_TCPV6 | AX_TXCOE_UDPV6;
1331         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, tmp);
1332
1333         /* Configure RX control register => start operation */
1334         *tmp16 = AX_RX_CTL_DROPCRCERR | AX_RX_CTL_IPE | AX_RX_CTL_START |
1335                  AX_RX_CTL_AP | AX_RX_CTL_AMALL | AX_RX_CTL_AB;
1336         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, tmp16);
1337
1338         *tmp = AX_MONITOR_MODE_PMETYPE | AX_MONITOR_MODE_PMEPOL |
1339                AX_MONITOR_MODE_RWMP;
1340         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MONITOR_MOD, 1, 1, tmp);
1341
1342         /* Configure default medium type => giga */
1343         *tmp16 = AX_MEDIUM_RECEIVE_EN | AX_MEDIUM_TXFLOW_CTRLEN |
1344                  AX_MEDIUM_RXFLOW_CTRLEN | AX_MEDIUM_FULL_DUPLEX |
1345                  AX_MEDIUM_GIGAMODE;
1346         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
1347                           2, 2, tmp16);
1348
1349         ax88179_led_setting(dev);
1350
1351         /* Restart autoneg */
1352         mii_nway_restart(&dev->mii);
1353
1354         usbnet_link_change(dev, 0, 0);
1355
1356         return 0;
1357 }
1358
1359 static int ax88179_stop(struct usbnet *dev)
1360 {
1361         u16 tmp16;
1362
1363         ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
1364                          2, 2, &tmp16);
1365         tmp16 &= ~AX_MEDIUM_RECEIVE_EN;
1366         ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
1367                           2, 2, &tmp16);
1368
1369         return 0;
1370 }
1371
1372 static const struct driver_info ax88179_info = {
1373         .description = "ASIX AX88179 USB 3.0 Gigabit Ethernet",
1374         .bind = ax88179_bind,
1375         .unbind = ax88179_unbind,
1376         .status = ax88179_status,
1377         .link_reset = ax88179_link_reset,
1378         .reset = ax88179_reset,
1379         .stop = ax88179_stop,
1380         .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1381         .rx_fixup = ax88179_rx_fixup,
1382         .tx_fixup = ax88179_tx_fixup,
1383 };
1384
1385 static const struct driver_info ax88178a_info = {
1386         .description = "ASIX AX88178A USB 2.0 Gigabit Ethernet",
1387         .bind = ax88179_bind,
1388         .unbind = ax88179_unbind,
1389         .status = ax88179_status,
1390         .link_reset = ax88179_link_reset,
1391         .reset = ax88179_reset,
1392         .stop = ax88179_stop,
1393         .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1394         .rx_fixup = ax88179_rx_fixup,
1395         .tx_fixup = ax88179_tx_fixup,
1396 };
1397
1398 static const struct driver_info dlink_dub1312_info = {
1399         .description = "D-Link DUB-1312 USB 3.0 to Gigabit Ethernet Adapter",
1400         .bind = ax88179_bind,
1401         .unbind = ax88179_unbind,
1402         .status = ax88179_status,
1403         .link_reset = ax88179_link_reset,
1404         .reset = ax88179_reset,
1405         .stop = ax88179_stop,
1406         .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1407         .rx_fixup = ax88179_rx_fixup,
1408         .tx_fixup = ax88179_tx_fixup,
1409 };
1410
1411 static const struct driver_info sitecom_info = {
1412         .description = "Sitecom USB 3.0 to Gigabit Adapter",
1413         .bind = ax88179_bind,
1414         .unbind = ax88179_unbind,
1415         .status = ax88179_status,
1416         .link_reset = ax88179_link_reset,
1417         .reset = ax88179_reset,
1418         .stop = ax88179_stop,
1419         .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1420         .rx_fixup = ax88179_rx_fixup,
1421         .tx_fixup = ax88179_tx_fixup,
1422 };
1423
1424 static const struct driver_info samsung_info = {
1425         .description = "Samsung USB Ethernet Adapter",
1426         .bind = ax88179_bind,
1427         .unbind = ax88179_unbind,
1428         .status = ax88179_status,
1429         .link_reset = ax88179_link_reset,
1430         .reset = ax88179_reset,
1431         .stop = ax88179_stop,
1432         .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1433         .rx_fixup = ax88179_rx_fixup,
1434         .tx_fixup = ax88179_tx_fixup,
1435 };
1436
1437 static const struct driver_info lenovo_info = {
1438         .description = "Lenovo OneLinkDock Gigabit LAN",
1439         .bind = ax88179_bind,
1440         .unbind = ax88179_unbind,
1441         .status = ax88179_status,
1442         .link_reset = ax88179_link_reset,
1443         .reset = ax88179_reset,
1444         .stop = ax88179_stop,
1445         .flags = FLAG_ETHER | FLAG_FRAMING_AX,
1446         .rx_fixup = ax88179_rx_fixup,
1447         .tx_fixup = ax88179_tx_fixup,
1448 };
1449
1450 static const struct usb_device_id products[] = {
1451 {
1452         /* ASIX AX88179 10/100/1000 */
1453         USB_DEVICE(0x0b95, 0x1790),
1454         .driver_info = (unsigned long)&ax88179_info,
1455 }, {
1456         /* ASIX AX88178A 10/100/1000 */
1457         USB_DEVICE(0x0b95, 0x178a),
1458         .driver_info = (unsigned long)&ax88178a_info,
1459 }, {
1460         /* D-Link DUB-1312 USB 3.0 to Gigabit Ethernet Adapter */
1461         USB_DEVICE(0x2001, 0x4a00),
1462         .driver_info = (unsigned long)&dlink_dub1312_info,
1463 }, {
1464         /* Sitecom USB 3.0 to Gigabit Adapter */
1465         USB_DEVICE(0x0df6, 0x0072),
1466         .driver_info = (unsigned long)&sitecom_info,
1467 }, {
1468         /* Samsung USB Ethernet Adapter */
1469         USB_DEVICE(0x04e8, 0xa100),
1470         .driver_info = (unsigned long)&samsung_info,
1471 }, {
1472         /* Lenovo OneLinkDock Gigabit LAN */
1473         USB_DEVICE(0x17ef, 0x304b),
1474         .driver_info = (unsigned long)&lenovo_info,
1475 },
1476         { },
1477 };
1478 MODULE_DEVICE_TABLE(usb, products);
1479
1480 static struct usb_driver ax88179_178a_driver = {
1481         .name =         "ax88179_178a",
1482         .id_table =     products,
1483         .probe =        usbnet_probe,
1484         .suspend =      ax88179_suspend,
1485         .resume =       ax88179_resume,
1486         .reset_resume = ax88179_resume,
1487         .disconnect =   usbnet_disconnect,
1488         .supports_autosuspend = 1,
1489         .disable_hub_initiated_lpm = 1,
1490 };
1491
1492 module_usb_driver(ax88179_178a_driver);
1493
1494 MODULE_DESCRIPTION("ASIX AX88179/178A based USB 3.0/2.0 Gigabit Ethernet Devices");
1495 MODULE_LICENSE("GPL");