1 /***************************************************************************
2 *
3 * Copyright (C) 2007-2008 SMSC
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 *
18 *****************************************************************************/
19
20 #include <linux/module.h>
21 #include <linux/kmod.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/ethtool.h>
25 #include <linux/mii.h>
26 #include <linux/usb.h>
27 #include <linux/bitrev.h>
28 #include <linux/crc16.h>
29 #include <linux/crc32.h>
30 #include <linux/usb/usbnet.h>
31 #include <linux/slab.h>
32 #include <linux/of_net.h>
33 #include "smsc95xx.h"
34
35 #define SMSC_CHIPNAME "smsc95xx"
36 #define SMSC_DRIVER_VERSION "1.0.6"
37 #define HS_USB_PKT_SIZE (512)
38 #define FS_USB_PKT_SIZE (64)
39 #define DEFAULT_HS_BURST_CAP_SIZE (16 * 1024 + 5 * HS_USB_PKT_SIZE)
40 #define DEFAULT_FS_BURST_CAP_SIZE (6 * 1024 + 33 * FS_USB_PKT_SIZE)
41 #define DEFAULT_BULK_IN_DELAY (0x00002000)
42 #define MAX_SINGLE_PACKET_SIZE (2048)
43 #define LAN95XX_EEPROM_MAGIC (0x9500)
44 #define EEPROM_MAC_OFFSET (0x01)
45 #define DEFAULT_TX_CSUM_ENABLE (true)
46 #define DEFAULT_RX_CSUM_ENABLE (true)
47 #define SMSC95XX_INTERNAL_PHY_ID (1)
48 #define SMSC95XX_TX_OVERHEAD (8)
49 #define SMSC95XX_TX_OVERHEAD_CSUM (12)
50 #define SUPPORTED_WAKE (WAKE_PHY | WAKE_UCAST | WAKE_BCAST | \
51 WAKE_MCAST | WAKE_ARP | WAKE_MAGIC)
52
53 #define FEATURE_8_WAKEUP_FILTERS (0x01)
54 #define FEATURE_PHY_NLP_CROSSOVER (0x02)
55 #define FEATURE_REMOTE_WAKEUP (0x04)
56
57 #define SUSPEND_SUSPEND0 (0x01)
58 #define SUSPEND_SUSPEND1 (0x02)
59 #define SUSPEND_SUSPEND2 (0x04)
60 #define SUSPEND_SUSPEND3 (0x08)
61 #define SUSPEND_ALLMODES (SUSPEND_SUSPEND0 | SUSPEND_SUSPEND1 | \
62 SUSPEND_SUSPEND2 | SUSPEND_SUSPEND3)
63
64 #define CARRIER_CHECK_DELAY (2 * HZ)
65
66 struct smsc95xx_priv {
67 u32 chip_id;
68 u32 mac_cr;
69 u32 hash_hi;
70 u32 hash_lo;
71 u32 wolopts;
72 spinlock_t mac_cr_lock;
73 u8 features;
74 u8 suspend_flags;
75 u8 mdix_ctrl;
76 bool link_ok;
77 struct delayed_work carrier_check;
78 struct usbnet *dev;
79 };
80
81 static bool turbo_mode = true;
82 module_param(turbo_mode, bool, 0644);
83 MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
84
__smsc95xx_read_reg(struct usbnet * dev,u32 index,u32 * data,int in_pm)85 static int __must_check __smsc95xx_read_reg(struct usbnet *dev, u32 index,
86 u32 *data, int in_pm)
87 {
88 u32 buf;
89 int ret;
90 int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
91
92 BUG_ON(!dev);
93
94 if (!in_pm)
95 fn = usbnet_read_cmd;
96 else
97 fn = usbnet_read_cmd_nopm;
98
99 ret = fn(dev, USB_VENDOR_REQUEST_READ_REGISTER, USB_DIR_IN
100 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
101 0, index, &buf, 4);
102 if (unlikely(ret < 0)) {
103 netdev_warn(dev->net, "Failed to read reg index 0x%08x: %d\n",
104 index, ret);
105 return ret;
106 }
107
108 le32_to_cpus(&buf);
109 *data = buf;
110
111 return ret;
112 }
113
__smsc95xx_write_reg(struct usbnet * dev,u32 index,u32 data,int in_pm)114 static int __must_check __smsc95xx_write_reg(struct usbnet *dev, u32 index,
115 u32 data, int in_pm)
116 {
117 u32 buf;
118 int ret;
119 int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
120
121 BUG_ON(!dev);
122
123 if (!in_pm)
124 fn = usbnet_write_cmd;
125 else
126 fn = usbnet_write_cmd_nopm;
127
128 buf = data;
129 cpu_to_le32s(&buf);
130
131 ret = fn(dev, USB_VENDOR_REQUEST_WRITE_REGISTER, USB_DIR_OUT
132 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
133 0, index, &buf, 4);
134 if (unlikely(ret < 0))
135 netdev_warn(dev->net, "Failed to write reg index 0x%08x: %d\n",
136 index, ret);
137
138 return ret;
139 }
140
smsc95xx_read_reg_nopm(struct usbnet * dev,u32 index,u32 * data)141 static int __must_check smsc95xx_read_reg_nopm(struct usbnet *dev, u32 index,
142 u32 *data)
143 {
144 return __smsc95xx_read_reg(dev, index, data, 1);
145 }
146
smsc95xx_write_reg_nopm(struct usbnet * dev,u32 index,u32 data)147 static int __must_check smsc95xx_write_reg_nopm(struct usbnet *dev, u32 index,
148 u32 data)
149 {
150 return __smsc95xx_write_reg(dev, index, data, 1);
151 }
152
smsc95xx_read_reg(struct usbnet * dev,u32 index,u32 * data)153 static int __must_check smsc95xx_read_reg(struct usbnet *dev, u32 index,
154 u32 *data)
155 {
156 return __smsc95xx_read_reg(dev, index, data, 0);
157 }
158
smsc95xx_write_reg(struct usbnet * dev,u32 index,u32 data)159 static int __must_check smsc95xx_write_reg(struct usbnet *dev, u32 index,
160 u32 data)
161 {
162 return __smsc95xx_write_reg(dev, index, data, 0);
163 }
164
165 /* Loop until the read is completed with timeout
166 * called with phy_mutex held */
__smsc95xx_phy_wait_not_busy(struct usbnet * dev,int in_pm)167 static int __must_check __smsc95xx_phy_wait_not_busy(struct usbnet *dev,
168 int in_pm)
169 {
170 unsigned long start_time = jiffies;
171 u32 val;
172 int ret;
173
174 do {
175 ret = __smsc95xx_read_reg(dev, MII_ADDR, &val, in_pm);
176 if (ret < 0) {
177 netdev_warn(dev->net, "Error reading MII_ACCESS\n");
178 return ret;
179 }
180
181 if (!(val & MII_BUSY_))
182 return 0;
183 } while (!time_after(jiffies, start_time + HZ));
184
185 return -EIO;
186 }
187
__smsc95xx_mdio_read(struct net_device * netdev,int phy_id,int idx,int in_pm)188 static int __smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx,
189 int in_pm)
190 {
191 struct usbnet *dev = netdev_priv(netdev);
192 u32 val, addr;
193 int ret;
194
195 mutex_lock(&dev->phy_mutex);
196
197 /* confirm MII not busy */
198 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
199 if (ret < 0) {
200 netdev_warn(dev->net, "MII is busy in smsc95xx_mdio_read\n");
201 goto done;
202 }
203
204 /* set the address, index & direction (read from PHY) */
205 phy_id &= dev->mii.phy_id_mask;
206 idx &= dev->mii.reg_num_mask;
207 addr = (phy_id << 11) | (idx << 6) | MII_READ_ | MII_BUSY_;
208 ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
209 if (ret < 0) {
210 netdev_warn(dev->net, "Error writing MII_ADDR\n");
211 goto done;
212 }
213
214 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
215 if (ret < 0) {
216 netdev_warn(dev->net, "Timed out reading MII reg %02X\n", idx);
217 goto done;
218 }
219
220 ret = __smsc95xx_read_reg(dev, MII_DATA, &val, in_pm);
221 if (ret < 0) {
222 netdev_warn(dev->net, "Error reading MII_DATA\n");
223 goto done;
224 }
225
226 ret = (u16)(val & 0xFFFF);
227
228 done:
229 mutex_unlock(&dev->phy_mutex);
230 return ret;
231 }
232
__smsc95xx_mdio_write(struct net_device * netdev,int phy_id,int idx,int regval,int in_pm)233 static void __smsc95xx_mdio_write(struct net_device *netdev, int phy_id,
234 int idx, int regval, int in_pm)
235 {
236 struct usbnet *dev = netdev_priv(netdev);
237 u32 val, addr;
238 int ret;
239
240 mutex_lock(&dev->phy_mutex);
241
242 /* confirm MII not busy */
243 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
244 if (ret < 0) {
245 netdev_warn(dev->net, "MII is busy in smsc95xx_mdio_write\n");
246 goto done;
247 }
248
249 val = regval;
250 ret = __smsc95xx_write_reg(dev, MII_DATA, val, in_pm);
251 if (ret < 0) {
252 netdev_warn(dev->net, "Error writing MII_DATA\n");
253 goto done;
254 }
255
256 /* set the address, index & direction (write to PHY) */
257 phy_id &= dev->mii.phy_id_mask;
258 idx &= dev->mii.reg_num_mask;
259 addr = (phy_id << 11) | (idx << 6) | MII_WRITE_ | MII_BUSY_;
260 ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
261 if (ret < 0) {
262 netdev_warn(dev->net, "Error writing MII_ADDR\n");
263 goto done;
264 }
265
266 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
267 if (ret < 0) {
268 netdev_warn(dev->net, "Timed out writing MII reg %02X\n", idx);
269 goto done;
270 }
271
272 done:
273 mutex_unlock(&dev->phy_mutex);
274 }
275
smsc95xx_mdio_read_nopm(struct net_device * netdev,int phy_id,int idx)276 static int smsc95xx_mdio_read_nopm(struct net_device *netdev, int phy_id,
277 int idx)
278 {
279 return __smsc95xx_mdio_read(netdev, phy_id, idx, 1);
280 }
281
smsc95xx_mdio_write_nopm(struct net_device * netdev,int phy_id,int idx,int regval)282 static void smsc95xx_mdio_write_nopm(struct net_device *netdev, int phy_id,
283 int idx, int regval)
284 {
285 __smsc95xx_mdio_write(netdev, phy_id, idx, regval, 1);
286 }
287
smsc95xx_mdio_read(struct net_device * netdev,int phy_id,int idx)288 static int smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
289 {
290 return __smsc95xx_mdio_read(netdev, phy_id, idx, 0);
291 }
292
smsc95xx_mdio_write(struct net_device * netdev,int phy_id,int idx,int regval)293 static void smsc95xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
294 int regval)
295 {
296 __smsc95xx_mdio_write(netdev, phy_id, idx, regval, 0);
297 }
298
smsc95xx_wait_eeprom(struct usbnet * dev)299 static int __must_check smsc95xx_wait_eeprom(struct usbnet *dev)
300 {
301 unsigned long start_time = jiffies;
302 u32 val;
303 int ret;
304
305 do {
306 ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
307 if (ret < 0) {
308 netdev_warn(dev->net, "Error reading E2P_CMD\n");
309 return ret;
310 }
311
312 if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_))
313 break;
314 udelay(40);
315 } while (!time_after(jiffies, start_time + HZ));
316
317 if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) {
318 netdev_warn(dev->net, "EEPROM read operation timeout\n");
319 return -EIO;
320 }
321
322 return 0;
323 }
324
smsc95xx_eeprom_confirm_not_busy(struct usbnet * dev)325 static int __must_check smsc95xx_eeprom_confirm_not_busy(struct usbnet *dev)
326 {
327 unsigned long start_time = jiffies;
328 u32 val;
329 int ret;
330
331 do {
332 ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
333 if (ret < 0) {
334 netdev_warn(dev->net, "Error reading E2P_CMD\n");
335 return ret;
336 }
337
338 if (!(val & E2P_CMD_BUSY_))
339 return 0;
340
341 udelay(40);
342 } while (!time_after(jiffies, start_time + HZ));
343
344 netdev_warn(dev->net, "EEPROM is busy\n");
345 return -EIO;
346 }
347
smsc95xx_read_eeprom(struct usbnet * dev,u32 offset,u32 length,u8 * data)348 static int smsc95xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
349 u8 *data)
350 {
351 u32 val;
352 int i, ret;
353
354 BUG_ON(!dev);
355 BUG_ON(!data);
356
357 ret = smsc95xx_eeprom_confirm_not_busy(dev);
358 if (ret)
359 return ret;
360
361 for (i = 0; i < length; i++) {
362 val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_);
363 ret = smsc95xx_write_reg(dev, E2P_CMD, val);
364 if (ret < 0) {
365 netdev_warn(dev->net, "Error writing E2P_CMD\n");
366 return ret;
367 }
368
369 ret = smsc95xx_wait_eeprom(dev);
370 if (ret < 0)
371 return ret;
372
373 ret = smsc95xx_read_reg(dev, E2P_DATA, &val);
374 if (ret < 0) {
375 netdev_warn(dev->net, "Error reading E2P_DATA\n");
376 return ret;
377 }
378
379 data[i] = val & 0xFF;
380 offset++;
381 }
382
383 return 0;
384 }
385
smsc95xx_write_eeprom(struct usbnet * dev,u32 offset,u32 length,u8 * data)386 static int smsc95xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
387 u8 *data)
388 {
389 u32 val;
390 int i, ret;
391
392 BUG_ON(!dev);
393 BUG_ON(!data);
394
395 ret = smsc95xx_eeprom_confirm_not_busy(dev);
396 if (ret)
397 return ret;
398
399 /* Issue write/erase enable command */
400 val = E2P_CMD_BUSY_ | E2P_CMD_EWEN_;
401 ret = smsc95xx_write_reg(dev, E2P_CMD, val);
402 if (ret < 0) {
403 netdev_warn(dev->net, "Error writing E2P_DATA\n");
404 return ret;
405 }
406
407 ret = smsc95xx_wait_eeprom(dev);
408 if (ret < 0)
409 return ret;
410
411 for (i = 0; i < length; i++) {
412
413 /* Fill data register */
414 val = data[i];
415 ret = smsc95xx_write_reg(dev, E2P_DATA, val);
416 if (ret < 0) {
417 netdev_warn(dev->net, "Error writing E2P_DATA\n");
418 return ret;
419 }
420
421 /* Send "write" command */
422 val = E2P_CMD_BUSY_ | E2P_CMD_WRITE_ | (offset & E2P_CMD_ADDR_);
423 ret = smsc95xx_write_reg(dev, E2P_CMD, val);
424 if (ret < 0) {
425 netdev_warn(dev->net, "Error writing E2P_CMD\n");
426 return ret;
427 }
428
429 ret = smsc95xx_wait_eeprom(dev);
430 if (ret < 0)
431 return ret;
432
433 offset++;
434 }
435
436 return 0;
437 }
438
smsc95xx_write_reg_async(struct usbnet * dev,u16 index,u32 data)439 static int __must_check smsc95xx_write_reg_async(struct usbnet *dev, u16 index,
440 u32 data)
441 {
442 const u16 size = 4;
443 u32 buf;
444 int ret;
445
446 buf = data;
447 cpu_to_le32s(&buf);
448
449 ret = usbnet_write_cmd_async(dev, USB_VENDOR_REQUEST_WRITE_REGISTER,
450 USB_DIR_OUT | USB_TYPE_VENDOR |
451 USB_RECIP_DEVICE,
452 0, index, &buf, size);
453 if (ret < 0)
454 netdev_warn(dev->net, "Error write async cmd, sts=%d\n",
455 ret);
456 return ret;
457 }
458
459 /* returns hash bit number for given MAC address
460 * example:
461 * 01 00 5E 00 00 01 -> returns bit number 31 */
smsc95xx_hash(char addr[ETH_ALEN])462 static unsigned int smsc95xx_hash(char addr[ETH_ALEN])
463 {
464 return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
465 }
466
smsc95xx_set_multicast(struct net_device * netdev)467 static void smsc95xx_set_multicast(struct net_device *netdev)
468 {
469 struct usbnet *dev = netdev_priv(netdev);
470 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
471 unsigned long flags;
472 int ret;
473
474 pdata->hash_hi = 0;
475 pdata->hash_lo = 0;
476
477 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
478
479 if (dev->net->flags & IFF_PROMISC) {
480 netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
481 pdata->mac_cr |= MAC_CR_PRMS_;
482 pdata->mac_cr &= ~(MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
483 } else if (dev->net->flags & IFF_ALLMULTI) {
484 netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
485 pdata->mac_cr |= MAC_CR_MCPAS_;
486 pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_HPFILT_);
487 } else if (!netdev_mc_empty(dev->net)) {
488 struct netdev_hw_addr *ha;
489
490 pdata->mac_cr |= MAC_CR_HPFILT_;
491 pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);
492
493 netdev_for_each_mc_addr(ha, netdev) {
494 u32 bitnum = smsc95xx_hash(ha->addr);
495 u32 mask = 0x01 << (bitnum & 0x1F);
496 if (bitnum & 0x20)
497 pdata->hash_hi |= mask;
498 else
499 pdata->hash_lo |= mask;
500 }
501
502 netif_dbg(dev, drv, dev->net, "HASHH=0x%08X, HASHL=0x%08X\n",
503 pdata->hash_hi, pdata->hash_lo);
504 } else {
505 netif_dbg(dev, drv, dev->net, "receive own packets only\n");
506 pdata->mac_cr &=
507 ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
508 }
509
510 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
511
512 /* Initiate async writes, as we can't wait for completion here */
513 ret = smsc95xx_write_reg_async(dev, HASHH, pdata->hash_hi);
514 if (ret < 0)
515 netdev_warn(dev->net, "failed to initiate async write to HASHH\n");
516
517 ret = smsc95xx_write_reg_async(dev, HASHL, pdata->hash_lo);
518 if (ret < 0)
519 netdev_warn(dev->net, "failed to initiate async write to HASHL\n");
520
521 ret = smsc95xx_write_reg_async(dev, MAC_CR, pdata->mac_cr);
522 if (ret < 0)
523 netdev_warn(dev->net, "failed to initiate async write to MAC_CR\n");
524 }
525
smsc95xx_phy_update_flowcontrol(struct usbnet * dev,u8 duplex,u16 lcladv,u16 rmtadv)526 static int smsc95xx_phy_update_flowcontrol(struct usbnet *dev, u8 duplex,
527 u16 lcladv, u16 rmtadv)
528 {
529 u32 flow = 0, afc_cfg;
530
531 int ret = smsc95xx_read_reg(dev, AFC_CFG, &afc_cfg);
532 if (ret < 0)
533 return ret;
534
535 if (duplex == DUPLEX_FULL) {
536 u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
537
538 if (cap & FLOW_CTRL_RX)
539 flow = 0xFFFF0002;
540
541 if (cap & FLOW_CTRL_TX) {
542 afc_cfg |= 0xF;
543 flow |= 0xFFFF0000;
544 } else {
545 afc_cfg &= ~0xF;
546 }
547
548 netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
549 cap & FLOW_CTRL_RX ? "enabled" : "disabled",
550 cap & FLOW_CTRL_TX ? "enabled" : "disabled");
551 } else {
552 netif_dbg(dev, link, dev->net, "half duplex\n");
553 afc_cfg |= 0xF;
554 }
555
556 ret = smsc95xx_write_reg(dev, FLOW, flow);
557 if (ret < 0)
558 return ret;
559
560 return smsc95xx_write_reg(dev, AFC_CFG, afc_cfg);
561 }
562
smsc95xx_link_reset(struct usbnet * dev)563 static int smsc95xx_link_reset(struct usbnet *dev)
564 {
565 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
566 struct mii_if_info *mii = &dev->mii;
567 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
568 unsigned long flags;
569 u16 lcladv, rmtadv;
570 int ret;
571
572 /* clear interrupt status */
573 ret = smsc95xx_mdio_read(dev->net, mii->phy_id, PHY_INT_SRC);
574 if (ret < 0)
575 return ret;
576
577 ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
578 if (ret < 0)
579 return ret;
580
581 mii_check_media(mii, 1, 1);
582 mii_ethtool_gset(&dev->mii, &ecmd);
583 lcladv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
584 rmtadv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_LPA);
585
586 netif_dbg(dev, link, dev->net,
587 "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
588 ethtool_cmd_speed(&ecmd), ecmd.duplex, lcladv, rmtadv);
589
590 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
591 if (ecmd.duplex != DUPLEX_FULL) {
592 pdata->mac_cr &= ~MAC_CR_FDPX_;
593 pdata->mac_cr |= MAC_CR_RCVOWN_;
594 } else {
595 pdata->mac_cr &= ~MAC_CR_RCVOWN_;
596 pdata->mac_cr |= MAC_CR_FDPX_;
597 }
598 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
599
600 ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
601 if (ret < 0)
602 return ret;
603
604 ret = smsc95xx_phy_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
605 if (ret < 0)
606 netdev_warn(dev->net, "Error updating PHY flow control\n");
607
608 return ret;
609 }
610
smsc95xx_status(struct usbnet * dev,struct urb * urb)611 static void smsc95xx_status(struct usbnet *dev, struct urb *urb)
612 {
613 u32 intdata;
614
615 if (urb->actual_length != 4) {
616 netdev_warn(dev->net, "unexpected urb length %d\n",
617 urb->actual_length);
618 return;
619 }
620
621 memcpy(&intdata, urb->transfer_buffer, 4);
622 le32_to_cpus(&intdata);
623
624 netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
625
626 if (intdata & INT_ENP_PHY_INT_)
627 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
628 else
629 netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
630 intdata);
631 }
632
set_carrier(struct usbnet * dev,bool link)633 static void set_carrier(struct usbnet *dev, bool link)
634 {
635 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
636
637 if (pdata->link_ok == link)
638 return;
639
640 pdata->link_ok = link;
641
642 if (link)
643 usbnet_link_change(dev, 1, 0);
644 else
645 usbnet_link_change(dev, 0, 0);
646 }
647
check_carrier(struct work_struct * work)648 static void check_carrier(struct work_struct *work)
649 {
650 struct smsc95xx_priv *pdata = container_of(work, struct smsc95xx_priv,
651 carrier_check.work);
652 struct usbnet *dev = pdata->dev;
653 int ret;
654
655 if (pdata->suspend_flags != 0)
656 return;
657
658 ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMSR);
659 if (ret < 0) {
660 netdev_warn(dev->net, "Failed to read MII_BMSR\n");
661 return;
662 }
663 if (ret & BMSR_LSTATUS)
664 set_carrier(dev, 1);
665 else
666 set_carrier(dev, 0);
667
668 schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
669 }
670
671 /* Enable or disable Tx & Rx checksum offload engines */
smsc95xx_set_features(struct net_device * netdev,netdev_features_t features)672 static int smsc95xx_set_features(struct net_device *netdev,
673 netdev_features_t features)
674 {
675 struct usbnet *dev = netdev_priv(netdev);
676 u32 read_buf;
677 int ret;
678
679 ret = smsc95xx_read_reg(dev, COE_CR, &read_buf);
680 if (ret < 0)
681 return ret;
682
683 if (features & NETIF_F_IP_CSUM)
684 read_buf |= Tx_COE_EN_;
685 else
686 read_buf &= ~Tx_COE_EN_;
687
688 if (features & NETIF_F_RXCSUM)
689 read_buf |= Rx_COE_EN_;
690 else
691 read_buf &= ~Rx_COE_EN_;
692
693 ret = smsc95xx_write_reg(dev, COE_CR, read_buf);
694 if (ret < 0)
695 return ret;
696
697 netif_dbg(dev, hw, dev->net, "COE_CR = 0x%08x\n", read_buf);
698 return 0;
699 }
700
smsc95xx_ethtool_get_eeprom_len(struct net_device * net)701 static int smsc95xx_ethtool_get_eeprom_len(struct net_device *net)
702 {
703 return MAX_EEPROM_SIZE;
704 }
705
smsc95xx_ethtool_get_eeprom(struct net_device * netdev,struct ethtool_eeprom * ee,u8 * data)706 static int smsc95xx_ethtool_get_eeprom(struct net_device *netdev,
707 struct ethtool_eeprom *ee, u8 *data)
708 {
709 struct usbnet *dev = netdev_priv(netdev);
710
711 ee->magic = LAN95XX_EEPROM_MAGIC;
712
713 return smsc95xx_read_eeprom(dev, ee->offset, ee->len, data);
714 }
715
smsc95xx_ethtool_set_eeprom(struct net_device * netdev,struct ethtool_eeprom * ee,u8 * data)716 static int smsc95xx_ethtool_set_eeprom(struct net_device *netdev,
717 struct ethtool_eeprom *ee, u8 *data)
718 {
719 struct usbnet *dev = netdev_priv(netdev);
720
721 if (ee->magic != LAN95XX_EEPROM_MAGIC) {
722 netdev_warn(dev->net, "EEPROM: magic value mismatch, magic = 0x%x\n",
723 ee->magic);
724 return -EINVAL;
725 }
726
727 return smsc95xx_write_eeprom(dev, ee->offset, ee->len, data);
728 }
729
smsc95xx_ethtool_getregslen(struct net_device * netdev)730 static int smsc95xx_ethtool_getregslen(struct net_device *netdev)
731 {
732 /* all smsc95xx registers */
733 return COE_CR - ID_REV + sizeof(u32);
734 }
735
736 static void
smsc95xx_ethtool_getregs(struct net_device * netdev,struct ethtool_regs * regs,void * buf)737 smsc95xx_ethtool_getregs(struct net_device *netdev, struct ethtool_regs *regs,
738 void *buf)
739 {
740 struct usbnet *dev = netdev_priv(netdev);
741 unsigned int i, j;
742 int retval;
743 u32 *data = buf;
744
745 retval = smsc95xx_read_reg(dev, ID_REV, ®s->version);
746 if (retval < 0) {
747 netdev_warn(netdev, "REGS: cannot read ID_REV\n");
748 return;
749 }
750
751 for (i = ID_REV, j = 0; i <= COE_CR; i += (sizeof(u32)), j++) {
752 retval = smsc95xx_read_reg(dev, i, &data[j]);
753 if (retval < 0) {
754 netdev_warn(netdev, "REGS: cannot read reg[%x]\n", i);
755 return;
756 }
757 }
758 }
759
smsc95xx_ethtool_get_wol(struct net_device * net,struct ethtool_wolinfo * wolinfo)760 static void smsc95xx_ethtool_get_wol(struct net_device *net,
761 struct ethtool_wolinfo *wolinfo)
762 {
763 struct usbnet *dev = netdev_priv(net);
764 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
765
766 wolinfo->supported = SUPPORTED_WAKE;
767 wolinfo->wolopts = pdata->wolopts;
768 }
769
smsc95xx_ethtool_set_wol(struct net_device * net,struct ethtool_wolinfo * wolinfo)770 static int smsc95xx_ethtool_set_wol(struct net_device *net,
771 struct ethtool_wolinfo *wolinfo)
772 {
773 struct usbnet *dev = netdev_priv(net);
774 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
775 int ret;
776
777 if (wolinfo->wolopts & ~SUPPORTED_WAKE)
778 return -EINVAL;
779
780 pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
781
782 ret = device_set_wakeup_enable(&dev->udev->dev, pdata->wolopts);
783 if (ret < 0)
784 netdev_warn(dev->net, "device_set_wakeup_enable error %d\n", ret);
785
786 return ret;
787 }
788
get_mdix_status(struct net_device * net)789 static int get_mdix_status(struct net_device *net)
790 {
791 struct usbnet *dev = netdev_priv(net);
792 u32 val;
793 int buf;
794
795 buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, SPECIAL_CTRL_STS);
796 if (buf & SPECIAL_CTRL_STS_OVRRD_AMDIX_) {
797 if (buf & SPECIAL_CTRL_STS_AMDIX_ENABLE_)
798 return ETH_TP_MDI_AUTO;
799 else if (buf & SPECIAL_CTRL_STS_AMDIX_STATE_)
800 return ETH_TP_MDI_X;
801 } else {
802 buf = smsc95xx_read_reg(dev, STRAP_STATUS, &val);
803 if (val & STRAP_STATUS_AMDIX_EN_)
804 return ETH_TP_MDI_AUTO;
805 }
806
807 return ETH_TP_MDI;
808 }
809
set_mdix_status(struct net_device * net,__u8 mdix_ctrl)810 static void set_mdix_status(struct net_device *net, __u8 mdix_ctrl)
811 {
812 struct usbnet *dev = netdev_priv(net);
813 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
814 int buf;
815
816 if ((pdata->chip_id == ID_REV_CHIP_ID_9500A_) ||
817 (pdata->chip_id == ID_REV_CHIP_ID_9530_) ||
818 (pdata->chip_id == ID_REV_CHIP_ID_89530_) ||
819 (pdata->chip_id == ID_REV_CHIP_ID_9730_)) {
820 /* Extend Manual AutoMDIX timer for 9500A/9500Ai */
821 buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
822 PHY_EDPD_CONFIG);
823 buf |= PHY_EDPD_CONFIG_EXT_CROSSOVER_;
824 smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
825 PHY_EDPD_CONFIG, buf);
826 }
827
828 if (mdix_ctrl == ETH_TP_MDI) {
829 buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
830 SPECIAL_CTRL_STS);
831 buf |= SPECIAL_CTRL_STS_OVRRD_AMDIX_;
832 buf &= ~(SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
833 SPECIAL_CTRL_STS_AMDIX_STATE_);
834 smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
835 SPECIAL_CTRL_STS, buf);
836 } else if (mdix_ctrl == ETH_TP_MDI_X) {
837 buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
838 SPECIAL_CTRL_STS);
839 buf |= SPECIAL_CTRL_STS_OVRRD_AMDIX_;
840 buf &= ~(SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
841 SPECIAL_CTRL_STS_AMDIX_STATE_);
842 buf |= SPECIAL_CTRL_STS_AMDIX_STATE_;
843 smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
844 SPECIAL_CTRL_STS, buf);
845 } else if (mdix_ctrl == ETH_TP_MDI_AUTO) {
846 buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
847 SPECIAL_CTRL_STS);
848 buf &= ~SPECIAL_CTRL_STS_OVRRD_AMDIX_;
849 buf &= ~(SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
850 SPECIAL_CTRL_STS_AMDIX_STATE_);
851 buf |= SPECIAL_CTRL_STS_AMDIX_ENABLE_;
852 smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
853 SPECIAL_CTRL_STS, buf);
854 }
855 pdata->mdix_ctrl = mdix_ctrl;
856 }
857
smsc95xx_get_link_ksettings(struct net_device * net,struct ethtool_link_ksettings * cmd)858 static int smsc95xx_get_link_ksettings(struct net_device *net,
859 struct ethtool_link_ksettings *cmd)
860 {
861 struct usbnet *dev = netdev_priv(net);
862 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
863 int retval;
864
865 retval = usbnet_get_link_ksettings(net, cmd);
866
867 cmd->base.eth_tp_mdix = pdata->mdix_ctrl;
868 cmd->base.eth_tp_mdix_ctrl = pdata->mdix_ctrl;
869
870 return retval;
871 }
872
smsc95xx_set_link_ksettings(struct net_device * net,const struct ethtool_link_ksettings * cmd)873 static int smsc95xx_set_link_ksettings(struct net_device *net,
874 const struct ethtool_link_ksettings *cmd)
875 {
876 struct usbnet *dev = netdev_priv(net);
877 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
878 int retval;
879
880 if (pdata->mdix_ctrl != cmd->base.eth_tp_mdix_ctrl)
881 set_mdix_status(net, cmd->base.eth_tp_mdix_ctrl);
882
883 retval = usbnet_set_link_ksettings(net, cmd);
884
885 return retval;
886 }
887
888 static const struct ethtool_ops smsc95xx_ethtool_ops = {
889 .get_link = usbnet_get_link,
890 .nway_reset = usbnet_nway_reset,
891 .get_drvinfo = usbnet_get_drvinfo,
892 .get_msglevel = usbnet_get_msglevel,
893 .set_msglevel = usbnet_set_msglevel,
894 .get_eeprom_len = smsc95xx_ethtool_get_eeprom_len,
895 .get_eeprom = smsc95xx_ethtool_get_eeprom,
896 .set_eeprom = smsc95xx_ethtool_set_eeprom,
897 .get_regs_len = smsc95xx_ethtool_getregslen,
898 .get_regs = smsc95xx_ethtool_getregs,
899 .get_wol = smsc95xx_ethtool_get_wol,
900 .set_wol = smsc95xx_ethtool_set_wol,
901 .get_link_ksettings = smsc95xx_get_link_ksettings,
902 .set_link_ksettings = smsc95xx_set_link_ksettings,
903 .get_ts_info = ethtool_op_get_ts_info,
904 };
905
smsc95xx_ioctl(struct net_device * netdev,struct ifreq * rq,int cmd)906 static int smsc95xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
907 {
908 struct usbnet *dev = netdev_priv(netdev);
909
910 if (!netif_running(netdev))
911 return -EINVAL;
912
913 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
914 }
915
smsc95xx_init_mac_address(struct usbnet * dev)916 static void smsc95xx_init_mac_address(struct usbnet *dev)
917 {
918 const u8 *mac_addr;
919
920 /* maybe the boot loader passed the MAC address in devicetree */
921 mac_addr = of_get_mac_address(dev->udev->dev.of_node);
922 if (mac_addr) {
923 memcpy(dev->net->dev_addr, mac_addr, ETH_ALEN);
924 return;
925 }
926
927 /* try reading mac address from EEPROM */
928 if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
929 dev->net->dev_addr) == 0) {
930 if (is_valid_ether_addr(dev->net->dev_addr)) {
931 /* eeprom values are valid so use them */
932 netif_dbg(dev, ifup, dev->net, "MAC address read from EEPROM\n");
933 return;
934 }
935 }
936
937 /* no useful static MAC address found. generate a random one */
938 eth_hw_addr_random(dev->net);
939 netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
940 }
941
smsc95xx_set_mac_address(struct usbnet * dev)942 static int smsc95xx_set_mac_address(struct usbnet *dev)
943 {
944 u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
945 dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
946 u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
947 int ret;
948
949 ret = smsc95xx_write_reg(dev, ADDRL, addr_lo);
950 if (ret < 0)
951 return ret;
952
953 return smsc95xx_write_reg(dev, ADDRH, addr_hi);
954 }
955
956 /* starts the TX path */
smsc95xx_start_tx_path(struct usbnet * dev)957 static int smsc95xx_start_tx_path(struct usbnet *dev)
958 {
959 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
960 unsigned long flags;
961 int ret;
962
963 /* Enable Tx at MAC */
964 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
965 pdata->mac_cr |= MAC_CR_TXEN_;
966 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
967
968 ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
969 if (ret < 0)
970 return ret;
971
972 /* Enable Tx at SCSRs */
973 return smsc95xx_write_reg(dev, TX_CFG, TX_CFG_ON_);
974 }
975
976 /* Starts the Receive path */
smsc95xx_start_rx_path(struct usbnet * dev,int in_pm)977 static int smsc95xx_start_rx_path(struct usbnet *dev, int in_pm)
978 {
979 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
980 unsigned long flags;
981
982 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
983 pdata->mac_cr |= MAC_CR_RXEN_;
984 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
985
986 return __smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr, in_pm);
987 }
988
smsc95xx_phy_initialize(struct usbnet * dev)989 static int smsc95xx_phy_initialize(struct usbnet *dev)
990 {
991 int bmcr, ret, timeout = 0;
992
993 /* Initialize MII structure */
994 dev->mii.dev = dev->net;
995 dev->mii.mdio_read = smsc95xx_mdio_read;
996 dev->mii.mdio_write = smsc95xx_mdio_write;
997 dev->mii.phy_id_mask = 0x1f;
998 dev->mii.reg_num_mask = 0x1f;
999 dev->mii.phy_id = SMSC95XX_INTERNAL_PHY_ID;
1000
1001 /* reset phy and wait for reset to complete */
1002 smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
1003
1004 do {
1005 msleep(10);
1006 bmcr = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
1007 timeout++;
1008 } while ((bmcr & BMCR_RESET) && (timeout < 100));
1009
1010 if (timeout >= 100) {
1011 netdev_warn(dev->net, "timeout on PHY Reset");
1012 return -EIO;
1013 }
1014
1015 smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
1016 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
1017 ADVERTISE_PAUSE_ASYM);
1018
1019 /* read to clear */
1020 ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
1021 if (ret < 0) {
1022 netdev_warn(dev->net, "Failed to read PHY_INT_SRC during init\n");
1023 return ret;
1024 }
1025
1026 smsc95xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
1027 PHY_INT_MASK_DEFAULT_);
1028 mii_nway_restart(&dev->mii);
1029
1030 netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
1031 return 0;
1032 }
1033
smsc95xx_reset(struct usbnet * dev)1034 static int smsc95xx_reset(struct usbnet *dev)
1035 {
1036 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1037 u32 read_buf, write_buf, burst_cap;
1038 int ret = 0, timeout;
1039
1040 netif_dbg(dev, ifup, dev->net, "entering smsc95xx_reset\n");
1041
1042 ret = smsc95xx_write_reg(dev, HW_CFG, HW_CFG_LRST_);
1043 if (ret < 0)
1044 return ret;
1045
1046 timeout = 0;
1047 do {
1048 msleep(10);
1049 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1050 if (ret < 0)
1051 return ret;
1052 timeout++;
1053 } while ((read_buf & HW_CFG_LRST_) && (timeout < 100));
1054
1055 if (timeout >= 100) {
1056 netdev_warn(dev->net, "timeout waiting for completion of Lite Reset\n");
1057 return ret;
1058 }
1059
1060 ret = smsc95xx_write_reg(dev, PM_CTRL, PM_CTL_PHY_RST_);
1061 if (ret < 0)
1062 return ret;
1063
1064 timeout = 0;
1065 do {
1066 msleep(10);
1067 ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf);
1068 if (ret < 0)
1069 return ret;
1070 timeout++;
1071 } while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));
1072
1073 if (timeout >= 100) {
1074 netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
1075 return ret;
1076 }
1077
1078 ret = smsc95xx_set_mac_address(dev);
1079 if (ret < 0)
1080 return ret;
1081
1082 netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n",
1083 dev->net->dev_addr);
1084
1085 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1086 if (ret < 0)
1087 return ret;
1088
1089 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n",
1090 read_buf);
1091
1092 read_buf |= HW_CFG_BIR_;
1093
1094 ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
1095 if (ret < 0)
1096 return ret;
1097
1098 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1099 if (ret < 0)
1100 return ret;
1101
1102 netif_dbg(dev, ifup, dev->net,
1103 "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
1104 read_buf);
1105
1106 if (!turbo_mode) {
1107 burst_cap = 0;
1108 dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
1109 } else if (dev->udev->speed == USB_SPEED_HIGH) {
1110 burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
1111 dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
1112 } else {
1113 burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
1114 dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
1115 }
1116
1117 netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n",
1118 (ulong)dev->rx_urb_size);
1119
1120 ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
1121 if (ret < 0)
1122 return ret;
1123
1124 ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf);
1125 if (ret < 0)
1126 return ret;
1127
1128 netif_dbg(dev, ifup, dev->net,
1129 "Read Value from BURST_CAP after writing: 0x%08x\n",
1130 read_buf);
1131
1132 ret = smsc95xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
1133 if (ret < 0)
1134 return ret;
1135
1136 ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf);
1137 if (ret < 0)
1138 return ret;
1139
1140 netif_dbg(dev, ifup, dev->net,
1141 "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
1142 read_buf);
1143
1144 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1145 if (ret < 0)
1146 return ret;
1147
1148 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG: 0x%08x\n",
1149 read_buf);
1150
1151 if (turbo_mode)
1152 read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);
1153
1154 read_buf &= ~HW_CFG_RXDOFF_;
1155
1156 /* set Rx data offset=2, Make IP header aligns on word boundary. */
1157 read_buf |= NET_IP_ALIGN << 9;
1158
1159 ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
1160 if (ret < 0)
1161 return ret;
1162
1163 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1164 if (ret < 0)
1165 return ret;
1166
1167 netif_dbg(dev, ifup, dev->net,
1168 "Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
1169
1170 ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
1171 if (ret < 0)
1172 return ret;
1173
1174 ret = smsc95xx_read_reg(dev, ID_REV, &read_buf);
1175 if (ret < 0)
1176 return ret;
1177 netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", read_buf);
1178
1179 /* Configure GPIO pins as LED outputs */
1180 write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED |
1181 LED_GPIO_CFG_FDX_LED;
1182 ret = smsc95xx_write_reg(dev, LED_GPIO_CFG, write_buf);
1183 if (ret < 0)
1184 return ret;
1185
1186 /* Init Tx */
1187 ret = smsc95xx_write_reg(dev, FLOW, 0);
1188 if (ret < 0)
1189 return ret;
1190
1191 ret = smsc95xx_write_reg(dev, AFC_CFG, AFC_CFG_DEFAULT);
1192 if (ret < 0)
1193 return ret;
1194
1195 /* Don't need mac_cr_lock during initialisation */
1196 ret = smsc95xx_read_reg(dev, MAC_CR, &pdata->mac_cr);
1197 if (ret < 0)
1198 return ret;
1199
1200 /* Init Rx */
1201 /* Set Vlan */
1202 ret = smsc95xx_write_reg(dev, VLAN1, (u32)ETH_P_8021Q);
1203 if (ret < 0)
1204 return ret;
1205
1206 /* Enable or disable checksum offload engines */
1207 ret = smsc95xx_set_features(dev->net, dev->net->features);
1208 if (ret < 0) {
1209 netdev_warn(dev->net, "Failed to set checksum offload features\n");
1210 return ret;
1211 }
1212
1213 smsc95xx_set_multicast(dev->net);
1214
1215 ret = smsc95xx_phy_initialize(dev);
1216 if (ret < 0) {
1217 netdev_warn(dev->net, "Failed to init PHY\n");
1218 return ret;
1219 }
1220
1221 ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
1222 if (ret < 0)
1223 return ret;
1224
1225 /* enable PHY interrupts */
1226 read_buf |= INT_EP_CTL_PHY_INT_;
1227
1228 ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf);
1229 if (ret < 0)
1230 return ret;
1231
1232 ret = smsc95xx_start_tx_path(dev);
1233 if (ret < 0) {
1234 netdev_warn(dev->net, "Failed to start TX path\n");
1235 return ret;
1236 }
1237
1238 ret = smsc95xx_start_rx_path(dev, 0);
1239 if (ret < 0) {
1240 netdev_warn(dev->net, "Failed to start RX path\n");
1241 return ret;
1242 }
1243
1244 netif_dbg(dev, ifup, dev->net, "smsc95xx_reset, return 0\n");
1245 return 0;
1246 }
1247
1248 static const struct net_device_ops smsc95xx_netdev_ops = {
1249 .ndo_open = usbnet_open,
1250 .ndo_stop = usbnet_stop,
1251 .ndo_start_xmit = usbnet_start_xmit,
1252 .ndo_tx_timeout = usbnet_tx_timeout,
1253 .ndo_change_mtu = usbnet_change_mtu,
1254 .ndo_get_stats64 = usbnet_get_stats64,
1255 .ndo_set_mac_address = eth_mac_addr,
1256 .ndo_validate_addr = eth_validate_addr,
1257 .ndo_do_ioctl = smsc95xx_ioctl,
1258 .ndo_set_rx_mode = smsc95xx_set_multicast,
1259 .ndo_set_features = smsc95xx_set_features,
1260 };
1261
smsc95xx_bind(struct usbnet * dev,struct usb_interface * intf)1262 static int smsc95xx_bind(struct usbnet *dev, struct usb_interface *intf)
1263 {
1264 struct smsc95xx_priv *pdata = NULL;
1265 u32 val;
1266 int ret;
1267
1268 printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
1269
1270 ret = usbnet_get_endpoints(dev, intf);
1271 if (ret < 0) {
1272 netdev_warn(dev->net, "usbnet_get_endpoints failed: %d\n", ret);
1273 return ret;
1274 }
1275
1276 dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc95xx_priv),
1277 GFP_KERNEL);
1278
1279 pdata = (struct smsc95xx_priv *)(dev->data[0]);
1280 if (!pdata)
1281 return -ENOMEM;
1282
1283 spin_lock_init(&pdata->mac_cr_lock);
1284
1285 /* LAN95xx devices do not alter the computed checksum of 0 to 0xffff.
1286 * RFC 2460, ipv6 UDP calculated checksum yields a result of zero must
1287 * be changed to 0xffff. RFC 768, ipv4 UDP computed checksum is zero,
1288 * it is transmitted as all ones. The zero transmitted checksum means
1289 * transmitter generated no checksum. Hence, enable csum offload only
1290 * for ipv4 packets.
1291 */
1292 if (DEFAULT_TX_CSUM_ENABLE)
1293 dev->net->features |= NETIF_F_IP_CSUM;
1294 if (DEFAULT_RX_CSUM_ENABLE)
1295 dev->net->features |= NETIF_F_RXCSUM;
1296
1297 dev->net->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1298 set_bit(EVENT_NO_IP_ALIGN, &dev->flags);
1299
1300 smsc95xx_init_mac_address(dev);
1301
1302 /* Init all registers */
1303 ret = smsc95xx_reset(dev);
1304
1305 /* detect device revision as different features may be available */
1306 ret = smsc95xx_read_reg(dev, ID_REV, &val);
1307 if (ret < 0)
1308 return ret;
1309 val >>= 16;
1310 pdata->chip_id = val;
1311 pdata->mdix_ctrl = get_mdix_status(dev->net);
1312
1313 if ((val == ID_REV_CHIP_ID_9500A_) || (val == ID_REV_CHIP_ID_9530_) ||
1314 (val == ID_REV_CHIP_ID_89530_) || (val == ID_REV_CHIP_ID_9730_))
1315 pdata->features = (FEATURE_8_WAKEUP_FILTERS |
1316 FEATURE_PHY_NLP_CROSSOVER |
1317 FEATURE_REMOTE_WAKEUP);
1318 else if (val == ID_REV_CHIP_ID_9512_)
1319 pdata->features = FEATURE_8_WAKEUP_FILTERS;
1320
1321 dev->net->netdev_ops = &smsc95xx_netdev_ops;
1322 dev->net->ethtool_ops = &smsc95xx_ethtool_ops;
1323 dev->net->flags |= IFF_MULTICAST;
1324 dev->net->hard_header_len += SMSC95XX_TX_OVERHEAD_CSUM;
1325 dev->net->min_mtu = ETH_MIN_MTU;
1326 dev->net->max_mtu = ETH_DATA_LEN;
1327 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
1328
1329 pdata->dev = dev;
1330 INIT_DELAYED_WORK(&pdata->carrier_check, check_carrier);
1331 schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
1332
1333 return 0;
1334 }
1335
smsc95xx_unbind(struct usbnet * dev,struct usb_interface * intf)1336 static void smsc95xx_unbind(struct usbnet *dev, struct usb_interface *intf)
1337 {
1338 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1339
1340 if (pdata) {
1341 cancel_delayed_work(&pdata->carrier_check);
1342 netif_dbg(dev, ifdown, dev->net, "free pdata\n");
1343 kfree(pdata);
1344 pdata = NULL;
1345 dev->data[0] = 0;
1346 }
1347 }
1348
smsc_crc(const u8 * buffer,size_t len,int filter)1349 static u32 smsc_crc(const u8 *buffer, size_t len, int filter)
1350 {
1351 u32 crc = bitrev16(crc16(0xFFFF, buffer, len));
1352 return crc << ((filter % 2) * 16);
1353 }
1354
smsc95xx_enable_phy_wakeup_interrupts(struct usbnet * dev,u16 mask)1355 static int smsc95xx_enable_phy_wakeup_interrupts(struct usbnet *dev, u16 mask)
1356 {
1357 struct mii_if_info *mii = &dev->mii;
1358 int ret;
1359
1360 netdev_dbg(dev->net, "enabling PHY wakeup interrupts\n");
1361
1362 /* read to clear */
1363 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_SRC);
1364 if (ret < 0)
1365 return ret;
1366
1367 /* enable interrupt source */
1368 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_MASK);
1369 if (ret < 0)
1370 return ret;
1371
1372 ret |= mask;
1373
1374 smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_INT_MASK, ret);
1375
1376 return 0;
1377 }
1378
smsc95xx_link_ok_nopm(struct usbnet * dev)1379 static int smsc95xx_link_ok_nopm(struct usbnet *dev)
1380 {
1381 struct mii_if_info *mii = &dev->mii;
1382 int ret;
1383
1384 /* first, a dummy read, needed to latch some MII phys */
1385 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
1386 if (ret < 0)
1387 return ret;
1388
1389 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
1390 if (ret < 0)
1391 return ret;
1392
1393 return !!(ret & BMSR_LSTATUS);
1394 }
1395
smsc95xx_enter_suspend0(struct usbnet * dev)1396 static int smsc95xx_enter_suspend0(struct usbnet *dev)
1397 {
1398 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1399 u32 val;
1400 int ret;
1401
1402 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1403 if (ret < 0)
1404 return ret;
1405
1406 val &= (~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_));
1407 val |= PM_CTL_SUS_MODE_0;
1408
1409 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1410 if (ret < 0)
1411 return ret;
1412
1413 /* clear wol status */
1414 val &= ~PM_CTL_WUPS_;
1415 val |= PM_CTL_WUPS_WOL_;
1416
1417 /* enable energy detection */
1418 if (pdata->wolopts & WAKE_PHY)
1419 val |= PM_CTL_WUPS_ED_;
1420
1421 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1422 if (ret < 0)
1423 return ret;
1424
1425 /* read back PM_CTRL */
1426 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1427 if (ret < 0)
1428 return ret;
1429
1430 pdata->suspend_flags |= SUSPEND_SUSPEND0;
1431
1432 return 0;
1433 }
1434
smsc95xx_enter_suspend1(struct usbnet * dev)1435 static int smsc95xx_enter_suspend1(struct usbnet *dev)
1436 {
1437 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1438 struct mii_if_info *mii = &dev->mii;
1439 u32 val;
1440 int ret;
1441
1442 /* reconfigure link pulse detection timing for
1443 * compatibility with non-standard link partners
1444 */
1445 if (pdata->features & FEATURE_PHY_NLP_CROSSOVER)
1446 smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_EDPD_CONFIG,
1447 PHY_EDPD_CONFIG_DEFAULT);
1448
1449 /* enable energy detect power-down mode */
1450 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS);
1451 if (ret < 0)
1452 return ret;
1453
1454 ret |= MODE_CTRL_STS_EDPWRDOWN_;
1455
1456 smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS, ret);
1457
1458 /* enter SUSPEND1 mode */
1459 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1460 if (ret < 0)
1461 return ret;
1462
1463 val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1464 val |= PM_CTL_SUS_MODE_1;
1465
1466 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1467 if (ret < 0)
1468 return ret;
1469
1470 /* clear wol status, enable energy detection */
1471 val &= ~PM_CTL_WUPS_;
1472 val |= (PM_CTL_WUPS_ED_ | PM_CTL_ED_EN_);
1473
1474 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1475 if (ret < 0)
1476 return ret;
1477
1478 pdata->suspend_flags |= SUSPEND_SUSPEND1;
1479
1480 return 0;
1481 }
1482
smsc95xx_enter_suspend2(struct usbnet * dev)1483 static int smsc95xx_enter_suspend2(struct usbnet *dev)
1484 {
1485 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1486 u32 val;
1487 int ret;
1488
1489 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1490 if (ret < 0)
1491 return ret;
1492
1493 val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1494 val |= PM_CTL_SUS_MODE_2;
1495
1496 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1497 if (ret < 0)
1498 return ret;
1499
1500 pdata->suspend_flags |= SUSPEND_SUSPEND2;
1501
1502 return 0;
1503 }
1504
smsc95xx_enter_suspend3(struct usbnet * dev)1505 static int smsc95xx_enter_suspend3(struct usbnet *dev)
1506 {
1507 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1508 u32 val;
1509 int ret;
1510
1511 ret = smsc95xx_read_reg_nopm(dev, RX_FIFO_INF, &val);
1512 if (ret < 0)
1513 return ret;
1514
1515 if (val & RX_FIFO_INF_USED_) {
1516 netdev_info(dev->net, "rx fifo not empty in autosuspend\n");
1517 return -EBUSY;
1518 }
1519
1520 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1521 if (ret < 0)
1522 return ret;
1523
1524 val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1525 val |= PM_CTL_SUS_MODE_3 | PM_CTL_RES_CLR_WKP_STS;
1526
1527 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1528 if (ret < 0)
1529 return ret;
1530
1531 /* clear wol status */
1532 val &= ~PM_CTL_WUPS_;
1533 val |= PM_CTL_WUPS_WOL_;
1534
1535 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1536 if (ret < 0)
1537 return ret;
1538
1539 pdata->suspend_flags |= SUSPEND_SUSPEND3;
1540
1541 return 0;
1542 }
1543
smsc95xx_autosuspend(struct usbnet * dev,u32 link_up)1544 static int smsc95xx_autosuspend(struct usbnet *dev, u32 link_up)
1545 {
1546 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1547 int ret;
1548
1549 if (!netif_running(dev->net)) {
1550 /* interface is ifconfig down so fully power down hw */
1551 netdev_dbg(dev->net, "autosuspend entering SUSPEND2\n");
1552 return smsc95xx_enter_suspend2(dev);
1553 }
1554
1555 if (!link_up) {
1556 /* link is down so enter EDPD mode, but only if device can
1557 * reliably resume from it. This check should be redundant
1558 * as current FEATURE_REMOTE_WAKEUP parts also support
1559 * FEATURE_PHY_NLP_CROSSOVER but it's included for clarity */
1560 if (!(pdata->features & FEATURE_PHY_NLP_CROSSOVER)) {
1561 netdev_warn(dev->net, "EDPD not supported\n");
1562 return -EBUSY;
1563 }
1564
1565 netdev_dbg(dev->net, "autosuspend entering SUSPEND1\n");
1566
1567 /* enable PHY wakeup events for if cable is attached */
1568 ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1569 PHY_INT_MASK_ANEG_COMP_);
1570 if (ret < 0) {
1571 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1572 return ret;
1573 }
1574
1575 netdev_info(dev->net, "entering SUSPEND1 mode\n");
1576 return smsc95xx_enter_suspend1(dev);
1577 }
1578
1579 /* enable PHY wakeup events so we remote wakeup if cable is pulled */
1580 ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1581 PHY_INT_MASK_LINK_DOWN_);
1582 if (ret < 0) {
1583 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1584 return ret;
1585 }
1586
1587 netdev_dbg(dev->net, "autosuspend entering SUSPEND3\n");
1588 return smsc95xx_enter_suspend3(dev);
1589 }
1590
smsc95xx_suspend(struct usb_interface * intf,pm_message_t message)1591 static int smsc95xx_suspend(struct usb_interface *intf, pm_message_t message)
1592 {
1593 struct usbnet *dev = usb_get_intfdata(intf);
1594 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1595 u32 val, link_up;
1596 int ret;
1597
1598 ret = usbnet_suspend(intf, message);
1599 if (ret < 0) {
1600 netdev_warn(dev->net, "usbnet_suspend error\n");
1601 return ret;
1602 }
1603
1604 cancel_delayed_work_sync(&pdata->carrier_check);
1605
1606 if (pdata->suspend_flags) {
1607 netdev_warn(dev->net, "error during last resume\n");
1608 pdata->suspend_flags = 0;
1609 }
1610
1611 /* determine if link is up using only _nopm functions */
1612 link_up = smsc95xx_link_ok_nopm(dev);
1613
1614 if (message.event == PM_EVENT_AUTO_SUSPEND &&
1615 (pdata->features & FEATURE_REMOTE_WAKEUP)) {
1616 ret = smsc95xx_autosuspend(dev, link_up);
1617 goto done;
1618 }
1619
1620 /* if we get this far we're not autosuspending */
1621 /* if no wol options set, or if link is down and we're not waking on
1622 * PHY activity, enter lowest power SUSPEND2 mode
1623 */
1624 if (!(pdata->wolopts & SUPPORTED_WAKE) ||
1625 !(link_up || (pdata->wolopts & WAKE_PHY))) {
1626 netdev_info(dev->net, "entering SUSPEND2 mode\n");
1627
1628 /* disable energy detect (link up) & wake up events */
1629 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1630 if (ret < 0)
1631 goto done;
1632
1633 val &= ~(WUCSR_MPEN_ | WUCSR_WAKE_EN_);
1634
1635 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1636 if (ret < 0)
1637 goto done;
1638
1639 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1640 if (ret < 0)
1641 goto done;
1642
1643 val &= ~(PM_CTL_ED_EN_ | PM_CTL_WOL_EN_);
1644
1645 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1646 if (ret < 0)
1647 goto done;
1648
1649 ret = smsc95xx_enter_suspend2(dev);
1650 goto done;
1651 }
1652
1653 if (pdata->wolopts & WAKE_PHY) {
1654 ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1655 (PHY_INT_MASK_ANEG_COMP_ | PHY_INT_MASK_LINK_DOWN_));
1656 if (ret < 0) {
1657 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1658 goto done;
1659 }
1660
1661 /* if link is down then configure EDPD and enter SUSPEND1,
1662 * otherwise enter SUSPEND0 below
1663 */
1664 if (!link_up) {
1665 netdev_info(dev->net, "entering SUSPEND1 mode\n");
1666 ret = smsc95xx_enter_suspend1(dev);
1667 goto done;
1668 }
1669 }
1670
1671 if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
1672 u32 *filter_mask = kzalloc(sizeof(u32) * 32, GFP_KERNEL);
1673 u32 command[2];
1674 u32 offset[2];
1675 u32 crc[4];
1676 int wuff_filter_count =
1677 (pdata->features & FEATURE_8_WAKEUP_FILTERS) ?
1678 LAN9500A_WUFF_NUM : LAN9500_WUFF_NUM;
1679 int i, filter = 0;
1680
1681 if (!filter_mask) {
1682 netdev_warn(dev->net, "Unable to allocate filter_mask\n");
1683 ret = -ENOMEM;
1684 goto done;
1685 }
1686
1687 memset(command, 0, sizeof(command));
1688 memset(offset, 0, sizeof(offset));
1689 memset(crc, 0, sizeof(crc));
1690
1691 if (pdata->wolopts & WAKE_BCAST) {
1692 const u8 bcast[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1693 netdev_info(dev->net, "enabling broadcast detection\n");
1694 filter_mask[filter * 4] = 0x003F;
1695 filter_mask[filter * 4 + 1] = 0x00;
1696 filter_mask[filter * 4 + 2] = 0x00;
1697 filter_mask[filter * 4 + 3] = 0x00;
1698 command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1699 offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1700 crc[filter/2] |= smsc_crc(bcast, 6, filter);
1701 filter++;
1702 }
1703
1704 if (pdata->wolopts & WAKE_MCAST) {
1705 const u8 mcast[] = {0x01, 0x00, 0x5E};
1706 netdev_info(dev->net, "enabling multicast detection\n");
1707 filter_mask[filter * 4] = 0x0007;
1708 filter_mask[filter * 4 + 1] = 0x00;
1709 filter_mask[filter * 4 + 2] = 0x00;
1710 filter_mask[filter * 4 + 3] = 0x00;
1711 command[filter/4] |= 0x09UL << ((filter % 4) * 8);
1712 offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1713 crc[filter/2] |= smsc_crc(mcast, 3, filter);
1714 filter++;
1715 }
1716
1717 if (pdata->wolopts & WAKE_ARP) {
1718 const u8 arp[] = {0x08, 0x06};
1719 netdev_info(dev->net, "enabling ARP detection\n");
1720 filter_mask[filter * 4] = 0x0003;
1721 filter_mask[filter * 4 + 1] = 0x00;
1722 filter_mask[filter * 4 + 2] = 0x00;
1723 filter_mask[filter * 4 + 3] = 0x00;
1724 command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1725 offset[filter/4] |= 0x0C << ((filter % 4) * 8);
1726 crc[filter/2] |= smsc_crc(arp, 2, filter);
1727 filter++;
1728 }
1729
1730 if (pdata->wolopts & WAKE_UCAST) {
1731 netdev_info(dev->net, "enabling unicast detection\n");
1732 filter_mask[filter * 4] = 0x003F;
1733 filter_mask[filter * 4 + 1] = 0x00;
1734 filter_mask[filter * 4 + 2] = 0x00;
1735 filter_mask[filter * 4 + 3] = 0x00;
1736 command[filter/4] |= 0x01UL << ((filter % 4) * 8);
1737 offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1738 crc[filter/2] |= smsc_crc(dev->net->dev_addr, ETH_ALEN, filter);
1739 filter++;
1740 }
1741
1742 for (i = 0; i < (wuff_filter_count * 4); i++) {
1743 ret = smsc95xx_write_reg_nopm(dev, WUFF, filter_mask[i]);
1744 if (ret < 0) {
1745 kfree(filter_mask);
1746 goto done;
1747 }
1748 }
1749 kfree(filter_mask);
1750
1751 for (i = 0; i < (wuff_filter_count / 4); i++) {
1752 ret = smsc95xx_write_reg_nopm(dev, WUFF, command[i]);
1753 if (ret < 0)
1754 goto done;
1755 }
1756
1757 for (i = 0; i < (wuff_filter_count / 4); i++) {
1758 ret = smsc95xx_write_reg_nopm(dev, WUFF, offset[i]);
1759 if (ret < 0)
1760 goto done;
1761 }
1762
1763 for (i = 0; i < (wuff_filter_count / 2); i++) {
1764 ret = smsc95xx_write_reg_nopm(dev, WUFF, crc[i]);
1765 if (ret < 0)
1766 goto done;
1767 }
1768
1769 /* clear any pending pattern match packet status */
1770 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1771 if (ret < 0)
1772 goto done;
1773
1774 val |= WUCSR_WUFR_;
1775
1776 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1777 if (ret < 0)
1778 goto done;
1779 }
1780
1781 if (pdata->wolopts & WAKE_MAGIC) {
1782 /* clear any pending magic packet status */
1783 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1784 if (ret < 0)
1785 goto done;
1786
1787 val |= WUCSR_MPR_;
1788
1789 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1790 if (ret < 0)
1791 goto done;
1792 }
1793
1794 /* enable/disable wakeup sources */
1795 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1796 if (ret < 0)
1797 goto done;
1798
1799 if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
1800 netdev_info(dev->net, "enabling pattern match wakeup\n");
1801 val |= WUCSR_WAKE_EN_;
1802 } else {
1803 netdev_info(dev->net, "disabling pattern match wakeup\n");
1804 val &= ~WUCSR_WAKE_EN_;
1805 }
1806
1807 if (pdata->wolopts & WAKE_MAGIC) {
1808 netdev_info(dev->net, "enabling magic packet wakeup\n");
1809 val |= WUCSR_MPEN_;
1810 } else {
1811 netdev_info(dev->net, "disabling magic packet wakeup\n");
1812 val &= ~WUCSR_MPEN_;
1813 }
1814
1815 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1816 if (ret < 0)
1817 goto done;
1818
1819 /* enable wol wakeup source */
1820 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1821 if (ret < 0)
1822 goto done;
1823
1824 val |= PM_CTL_WOL_EN_;
1825
1826 /* phy energy detect wakeup source */
1827 if (pdata->wolopts & WAKE_PHY)
1828 val |= PM_CTL_ED_EN_;
1829
1830 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1831 if (ret < 0)
1832 goto done;
1833
1834 /* enable receiver to enable frame reception */
1835 smsc95xx_start_rx_path(dev, 1);
1836
1837 /* some wol options are enabled, so enter SUSPEND0 */
1838 netdev_info(dev->net, "entering SUSPEND0 mode\n");
1839 ret = smsc95xx_enter_suspend0(dev);
1840
1841 done:
1842 /*
1843 * TODO: resume() might need to handle the suspend failure
1844 * in system sleep
1845 */
1846 if (ret && PMSG_IS_AUTO(message))
1847 usbnet_resume(intf);
1848
1849 if (ret)
1850 schedule_delayed_work(&pdata->carrier_check,
1851 CARRIER_CHECK_DELAY);
1852
1853 return ret;
1854 }
1855
smsc95xx_resume(struct usb_interface * intf)1856 static int smsc95xx_resume(struct usb_interface *intf)
1857 {
1858 struct usbnet *dev = usb_get_intfdata(intf);
1859 struct smsc95xx_priv *pdata;
1860 u8 suspend_flags;
1861 int ret;
1862 u32 val;
1863
1864 BUG_ON(!dev);
1865 pdata = (struct smsc95xx_priv *)(dev->data[0]);
1866 suspend_flags = pdata->suspend_flags;
1867
1868 netdev_dbg(dev->net, "resume suspend_flags=0x%02x\n", suspend_flags);
1869
1870 /* do this first to ensure it's cleared even in error case */
1871 pdata->suspend_flags = 0;
1872 schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
1873
1874 if (suspend_flags & SUSPEND_ALLMODES) {
1875 /* clear wake-up sources */
1876 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1877 if (ret < 0)
1878 return ret;
1879
1880 val &= ~(WUCSR_WAKE_EN_ | WUCSR_MPEN_);
1881
1882 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1883 if (ret < 0)
1884 return ret;
1885
1886 /* clear wake-up status */
1887 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1888 if (ret < 0)
1889 return ret;
1890
1891 val &= ~PM_CTL_WOL_EN_;
1892 val |= PM_CTL_WUPS_;
1893
1894 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1895 if (ret < 0)
1896 return ret;
1897 }
1898
1899 ret = usbnet_resume(intf);
1900 if (ret < 0)
1901 netdev_warn(dev->net, "usbnet_resume error\n");
1902
1903 return ret;
1904 }
1905
smsc95xx_reset_resume(struct usb_interface * intf)1906 static int smsc95xx_reset_resume(struct usb_interface *intf)
1907 {
1908 struct usbnet *dev = usb_get_intfdata(intf);
1909 int ret;
1910
1911 ret = smsc95xx_reset(dev);
1912 if (ret < 0)
1913 return ret;
1914
1915 return smsc95xx_resume(intf);
1916 }
1917
smsc95xx_rx_csum_offload(struct sk_buff * skb)1918 static void smsc95xx_rx_csum_offload(struct sk_buff *skb)
1919 {
1920 skb->csum = *(u16 *)(skb_tail_pointer(skb) - 2);
1921 skb->ip_summed = CHECKSUM_COMPLETE;
1922 skb_trim(skb, skb->len - 2);
1923 }
1924
smsc95xx_rx_fixup(struct usbnet * dev,struct sk_buff * skb)1925 static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
1926 {
1927 /* This check is no longer done by usbnet */
1928 if (skb->len < dev->net->hard_header_len)
1929 return 0;
1930
1931 while (skb->len > 0) {
1932 u32 header, align_count;
1933 struct sk_buff *ax_skb;
1934 unsigned char *packet;
1935 u16 size;
1936
1937 memcpy(&header, skb->data, sizeof(header));
1938 le32_to_cpus(&header);
1939 skb_pull(skb, 4 + NET_IP_ALIGN);
1940 packet = skb->data;
1941
1942 /* get the packet length */
1943 size = (u16)((header & RX_STS_FL_) >> 16);
1944 align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4;
1945
1946 if (unlikely(header & RX_STS_ES_)) {
1947 netif_dbg(dev, rx_err, dev->net,
1948 "Error header=0x%08x\n", header);
1949 dev->net->stats.rx_errors++;
1950 dev->net->stats.rx_dropped++;
1951
1952 if (header & RX_STS_CRC_) {
1953 dev->net->stats.rx_crc_errors++;
1954 } else {
1955 if (header & (RX_STS_TL_ | RX_STS_RF_))
1956 dev->net->stats.rx_frame_errors++;
1957
1958 if ((header & RX_STS_LE_) &&
1959 (!(header & RX_STS_FT_)))
1960 dev->net->stats.rx_length_errors++;
1961 }
1962 } else {
1963 /* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
1964 if (unlikely(size > (ETH_FRAME_LEN + 12))) {
1965 netif_dbg(dev, rx_err, dev->net,
1966 "size err header=0x%08x\n", header);
1967 return 0;
1968 }
1969
1970 /* last frame in this batch */
1971 if (skb->len == size) {
1972 if (dev->net->features & NETIF_F_RXCSUM)
1973 smsc95xx_rx_csum_offload(skb);
1974 skb_trim(skb, skb->len - 4); /* remove fcs */
1975 skb->truesize = size + sizeof(struct sk_buff);
1976
1977 return 1;
1978 }
1979
1980 ax_skb = skb_clone(skb, GFP_ATOMIC);
1981 if (unlikely(!ax_skb)) {
1982 netdev_warn(dev->net, "Error allocating skb\n");
1983 return 0;
1984 }
1985
1986 ax_skb->len = size;
1987 ax_skb->data = packet;
1988 skb_set_tail_pointer(ax_skb, size);
1989
1990 if (dev->net->features & NETIF_F_RXCSUM)
1991 smsc95xx_rx_csum_offload(ax_skb);
1992 skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
1993 ax_skb->truesize = size + sizeof(struct sk_buff);
1994
1995 usbnet_skb_return(dev, ax_skb);
1996 }
1997
1998 skb_pull(skb, size);
1999
2000 /* padding bytes before the next frame starts */
2001 if (skb->len)
2002 skb_pull(skb, align_count);
2003 }
2004
2005 return 1;
2006 }
2007
smsc95xx_calc_csum_preamble(struct sk_buff * skb)2008 static u32 smsc95xx_calc_csum_preamble(struct sk_buff *skb)
2009 {
2010 u16 low_16 = (u16)skb_checksum_start_offset(skb);
2011 u16 high_16 = low_16 + skb->csum_offset;
2012 return (high_16 << 16) | low_16;
2013 }
2014
smsc95xx_tx_fixup(struct usbnet * dev,struct sk_buff * skb,gfp_t flags)2015 static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,
2016 struct sk_buff *skb, gfp_t flags)
2017 {
2018 bool csum = skb->ip_summed == CHECKSUM_PARTIAL;
2019 int overhead = csum ? SMSC95XX_TX_OVERHEAD_CSUM : SMSC95XX_TX_OVERHEAD;
2020 u32 tx_cmd_a, tx_cmd_b;
2021
2022 /* We do not advertise SG, so skbs should be already linearized */
2023 BUG_ON(skb_shinfo(skb)->nr_frags);
2024
2025 /* Make writable and expand header space by overhead if required */
2026 if (skb_cow_head(skb, overhead)) {
2027 /* Must deallocate here as returning NULL to indicate error
2028 * means the skb won't be deallocated in the caller.
2029 */
2030 dev_kfree_skb_any(skb);
2031 return NULL;
2032 }
2033
2034 if (csum) {
2035 if (skb->len <= 45) {
2036 /* workaround - hardware tx checksum does not work
2037 * properly with extremely small packets */
2038 long csstart = skb_checksum_start_offset(skb);
2039 __wsum calc = csum_partial(skb->data + csstart,
2040 skb->len - csstart, 0);
2041 *((__sum16 *)(skb->data + csstart
2042 + skb->csum_offset)) = csum_fold(calc);
2043
2044 csum = false;
2045 } else {
2046 u32 csum_preamble = smsc95xx_calc_csum_preamble(skb);
2047 skb_push(skb, 4);
2048 cpu_to_le32s(&csum_preamble);
2049 memcpy(skb->data, &csum_preamble, 4);
2050 }
2051 }
2052
2053 skb_push(skb, 4);
2054 tx_cmd_b = (u32)(skb->len - 4);
2055 if (csum)
2056 tx_cmd_b |= TX_CMD_B_CSUM_ENABLE;
2057 cpu_to_le32s(&tx_cmd_b);
2058 memcpy(skb->data, &tx_cmd_b, 4);
2059
2060 skb_push(skb, 4);
2061 tx_cmd_a = (u32)(skb->len - 8) | TX_CMD_A_FIRST_SEG_ |
2062 TX_CMD_A_LAST_SEG_;
2063 cpu_to_le32s(&tx_cmd_a);
2064 memcpy(skb->data, &tx_cmd_a, 4);
2065
2066 return skb;
2067 }
2068
smsc95xx_manage_power(struct usbnet * dev,int on)2069 static int smsc95xx_manage_power(struct usbnet *dev, int on)
2070 {
2071 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
2072
2073 dev->intf->needs_remote_wakeup = on;
2074
2075 if (pdata->features & FEATURE_REMOTE_WAKEUP)
2076 return 0;
2077
2078 /* this chip revision isn't capable of remote wakeup */
2079 netdev_info(dev->net, "hardware isn't capable of remote wakeup\n");
2080
2081 if (on)
2082 usb_autopm_get_interface_no_resume(dev->intf);
2083 else
2084 usb_autopm_put_interface(dev->intf);
2085
2086 return 0;
2087 }
2088
2089 static const struct driver_info smsc95xx_info = {
2090 .description = "smsc95xx USB 2.0 Ethernet",
2091 .bind = smsc95xx_bind,
2092 .unbind = smsc95xx_unbind,
2093 .link_reset = smsc95xx_link_reset,
2094 .reset = smsc95xx_reset,
2095 .rx_fixup = smsc95xx_rx_fixup,
2096 .tx_fixup = smsc95xx_tx_fixup,
2097 .status = smsc95xx_status,
2098 .manage_power = smsc95xx_manage_power,
2099 .flags = FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
2100 };
2101
2102 static const struct usb_device_id products[] = {
2103 {
2104 /* SMSC9500 USB Ethernet Device */
2105 USB_DEVICE(0x0424, 0x9500),
2106 .driver_info = (unsigned long) &smsc95xx_info,
2107 },
2108 {
2109 /* SMSC9505 USB Ethernet Device */
2110 USB_DEVICE(0x0424, 0x9505),
2111 .driver_info = (unsigned long) &smsc95xx_info,
2112 },
2113 {
2114 /* SMSC9500A USB Ethernet Device */
2115 USB_DEVICE(0x0424, 0x9E00),
2116 .driver_info = (unsigned long) &smsc95xx_info,
2117 },
2118 {
2119 /* SMSC9505A USB Ethernet Device */
2120 USB_DEVICE(0x0424, 0x9E01),
2121 .driver_info = (unsigned long) &smsc95xx_info,
2122 },
2123 {
2124 /* SMSC9512/9514 USB Hub & Ethernet Device */
2125 USB_DEVICE(0x0424, 0xec00),
2126 .driver_info = (unsigned long) &smsc95xx_info,
2127 },
2128 {
2129 /* SMSC9500 USB Ethernet Device (SAL10) */
2130 USB_DEVICE(0x0424, 0x9900),
2131 .driver_info = (unsigned long) &smsc95xx_info,
2132 },
2133 {
2134 /* SMSC9505 USB Ethernet Device (SAL10) */
2135 USB_DEVICE(0x0424, 0x9901),
2136 .driver_info = (unsigned long) &smsc95xx_info,
2137 },
2138 {
2139 /* SMSC9500A USB Ethernet Device (SAL10) */
2140 USB_DEVICE(0x0424, 0x9902),
2141 .driver_info = (unsigned long) &smsc95xx_info,
2142 },
2143 {
2144 /* SMSC9505A USB Ethernet Device (SAL10) */
2145 USB_DEVICE(0x0424, 0x9903),
2146 .driver_info = (unsigned long) &smsc95xx_info,
2147 },
2148 {
2149 /* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */
2150 USB_DEVICE(0x0424, 0x9904),
2151 .driver_info = (unsigned long) &smsc95xx_info,
2152 },
2153 {
2154 /* SMSC9500A USB Ethernet Device (HAL) */
2155 USB_DEVICE(0x0424, 0x9905),
2156 .driver_info = (unsigned long) &smsc95xx_info,
2157 },
2158 {
2159 /* SMSC9505A USB Ethernet Device (HAL) */
2160 USB_DEVICE(0x0424, 0x9906),
2161 .driver_info = (unsigned long) &smsc95xx_info,
2162 },
2163 {
2164 /* SMSC9500 USB Ethernet Device (Alternate ID) */
2165 USB_DEVICE(0x0424, 0x9907),
2166 .driver_info = (unsigned long) &smsc95xx_info,
2167 },
2168 {
2169 /* SMSC9500A USB Ethernet Device (Alternate ID) */
2170 USB_DEVICE(0x0424, 0x9908),
2171 .driver_info = (unsigned long) &smsc95xx_info,
2172 },
2173 {
2174 /* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */
2175 USB_DEVICE(0x0424, 0x9909),
2176 .driver_info = (unsigned long) &smsc95xx_info,
2177 },
2178 {
2179 /* SMSC LAN9530 USB Ethernet Device */
2180 USB_DEVICE(0x0424, 0x9530),
2181 .driver_info = (unsigned long) &smsc95xx_info,
2182 },
2183 {
2184 /* SMSC LAN9730 USB Ethernet Device */
2185 USB_DEVICE(0x0424, 0x9730),
2186 .driver_info = (unsigned long) &smsc95xx_info,
2187 },
2188 {
2189 /* SMSC LAN89530 USB Ethernet Device */
2190 USB_DEVICE(0x0424, 0x9E08),
2191 .driver_info = (unsigned long) &smsc95xx_info,
2192 },
2193 { }, /* END */
2194 };
2195 MODULE_DEVICE_TABLE(usb, products);
2196
2197 static struct usb_driver smsc95xx_driver = {
2198 .name = "smsc95xx",
2199 .id_table = products,
2200 .probe = usbnet_probe,
2201 .suspend = smsc95xx_suspend,
2202 .resume = smsc95xx_resume,
2203 .reset_resume = smsc95xx_reset_resume,
2204 .disconnect = usbnet_disconnect,
2205 .disable_hub_initiated_lpm = 1,
2206 .supports_autosuspend = 1,
2207 };
2208
2209 module_usb_driver(smsc95xx_driver);
2210
2211 MODULE_AUTHOR("Nancy Lin");
2212 MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
2213 MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices");
2214 MODULE_LICENSE("GPL");
2215