blob: 759e577008b0246d14cae507328863891c1ca4cc [file] [log] [blame]
Steve Glendinningd0cad872010-03-16 08:46:46 +00001 /***************************************************************************
2 *
3 * Copyright (C) 2007-2010 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 *****************************************************************************/
20
21#include <linux/module.h>
22#include <linux/kmod.h>
23#include <linux/init.h>
24#include <linux/netdevice.h>
25#include <linux/etherdevice.h>
26#include <linux/ethtool.h>
27#include <linux/mii.h>
28#include <linux/usb.h>
29#include <linux/crc32.h>
30#include <linux/usb/usbnet.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Steve Glendinningd0cad872010-03-16 08:46:46 +000032#include "smsc75xx.h"
33
34#define SMSC_CHIPNAME "smsc75xx"
35#define SMSC_DRIVER_VERSION "1.0.0"
36#define HS_USB_PKT_SIZE (512)
37#define FS_USB_PKT_SIZE (64)
38#define DEFAULT_HS_BURST_CAP_SIZE (16 * 1024 + 5 * HS_USB_PKT_SIZE)
39#define DEFAULT_FS_BURST_CAP_SIZE (6 * 1024 + 33 * FS_USB_PKT_SIZE)
40#define DEFAULT_BULK_IN_DELAY (0x00002000)
41#define MAX_SINGLE_PACKET_SIZE (9000)
42#define LAN75XX_EEPROM_MAGIC (0x7500)
43#define EEPROM_MAC_OFFSET (0x01)
44#define DEFAULT_TX_CSUM_ENABLE (true)
45#define DEFAULT_RX_CSUM_ENABLE (true)
46#define DEFAULT_TSO_ENABLE (true)
47#define SMSC75XX_INTERNAL_PHY_ID (1)
48#define SMSC75XX_TX_OVERHEAD (8)
49#define MAX_RX_FIFO_SIZE (20 * 1024)
50#define MAX_TX_FIFO_SIZE (12 * 1024)
51#define USB_VENDOR_ID_SMSC (0x0424)
52#define USB_PRODUCT_ID_LAN7500 (0x7500)
53#define USB_PRODUCT_ID_LAN7505 (0x7505)
Nico Erfurthea1649d2011-11-08 07:30:40 +000054#define RXW_PADDING 2
Steve Glendinningd0cad872010-03-16 08:46:46 +000055
56#define check_warn(ret, fmt, args...) \
57 ({ if (ret < 0) netdev_warn(dev->net, fmt, ##args); })
58
59#define check_warn_return(ret, fmt, args...) \
60 ({ if (ret < 0) { netdev_warn(dev->net, fmt, ##args); return ret; } })
61
62#define check_warn_goto_done(ret, fmt, args...) \
63 ({ if (ret < 0) { netdev_warn(dev->net, fmt, ##args); goto done; } })
64
65struct smsc75xx_priv {
66 struct usbnet *dev;
67 u32 rfe_ctl;
68 u32 multicast_hash_table[DP_SEL_VHF_HASH_LEN];
Steve Glendinningd0cad872010-03-16 08:46:46 +000069 struct mutex dataport_mutex;
70 spinlock_t rfe_ctl_lock;
71 struct work_struct set_multicast;
72};
73
74struct usb_context {
75 struct usb_ctrlrequest req;
76 struct usbnet *dev;
77};
78
Rusty Russelleb939922011-12-19 14:08:01 +000079static bool turbo_mode = true;
Steve Glendinningd0cad872010-03-16 08:46:46 +000080module_param(turbo_mode, bool, 0644);
81MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
82
83static int __must_check smsc75xx_read_reg(struct usbnet *dev, u32 index,
84 u32 *data)
85{
86 u32 *buf = kmalloc(4, GFP_KERNEL);
87 int ret;
88
89 BUG_ON(!dev);
90
91 if (!buf)
92 return -ENOMEM;
93
94 ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
95 USB_VENDOR_REQUEST_READ_REGISTER,
96 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
97 00, index, buf, 4, USB_CTRL_GET_TIMEOUT);
98
99 if (unlikely(ret < 0))
100 netdev_warn(dev->net,
Steve Glendinning4f49add2012-04-30 07:56:52 +0000101 "Failed to read reg index 0x%08x: %d", index, ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000102
103 le32_to_cpus(buf);
104 *data = *buf;
105 kfree(buf);
106
107 return ret;
108}
109
110static int __must_check smsc75xx_write_reg(struct usbnet *dev, u32 index,
111 u32 data)
112{
113 u32 *buf = kmalloc(4, GFP_KERNEL);
114 int ret;
115
116 BUG_ON(!dev);
117
118 if (!buf)
119 return -ENOMEM;
120
121 *buf = data;
122 cpu_to_le32s(buf);
123
124 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
125 USB_VENDOR_REQUEST_WRITE_REGISTER,
126 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
127 00, index, buf, 4, USB_CTRL_SET_TIMEOUT);
128
129 if (unlikely(ret < 0))
130 netdev_warn(dev->net,
Steve Glendinning4f49add2012-04-30 07:56:52 +0000131 "Failed to write reg index 0x%08x: %d", index, ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000132
133 kfree(buf);
134
135 return ret;
136}
137
138/* Loop until the read is completed with timeout
139 * called with phy_mutex held */
140static int smsc75xx_phy_wait_not_busy(struct usbnet *dev)
141{
142 unsigned long start_time = jiffies;
143 u32 val;
144 int ret;
145
146 do {
147 ret = smsc75xx_read_reg(dev, MII_ACCESS, &val);
148 check_warn_return(ret, "Error reading MII_ACCESS");
149
150 if (!(val & MII_ACCESS_BUSY))
151 return 0;
152 } while (!time_after(jiffies, start_time + HZ));
153
154 return -EIO;
155}
156
157static int smsc75xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
158{
159 struct usbnet *dev = netdev_priv(netdev);
160 u32 val, addr;
161 int ret;
162
163 mutex_lock(&dev->phy_mutex);
164
165 /* confirm MII not busy */
166 ret = smsc75xx_phy_wait_not_busy(dev);
167 check_warn_goto_done(ret, "MII is busy in smsc75xx_mdio_read");
168
169 /* set the address, index & direction (read from PHY) */
170 phy_id &= dev->mii.phy_id_mask;
171 idx &= dev->mii.reg_num_mask;
172 addr = ((phy_id << MII_ACCESS_PHY_ADDR_SHIFT) & MII_ACCESS_PHY_ADDR)
173 | ((idx << MII_ACCESS_REG_ADDR_SHIFT) & MII_ACCESS_REG_ADDR)
Steve Glendinningcb8722d2012-04-30 07:56:51 +0000174 | MII_ACCESS_READ | MII_ACCESS_BUSY;
Steve Glendinningd0cad872010-03-16 08:46:46 +0000175 ret = smsc75xx_write_reg(dev, MII_ACCESS, addr);
176 check_warn_goto_done(ret, "Error writing MII_ACCESS");
177
178 ret = smsc75xx_phy_wait_not_busy(dev);
179 check_warn_goto_done(ret, "Timed out reading MII reg %02X", idx);
180
181 ret = smsc75xx_read_reg(dev, MII_DATA, &val);
182 check_warn_goto_done(ret, "Error reading MII_DATA");
183
184 ret = (u16)(val & 0xFFFF);
185
186done:
187 mutex_unlock(&dev->phy_mutex);
188 return ret;
189}
190
191static void smsc75xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
192 int regval)
193{
194 struct usbnet *dev = netdev_priv(netdev);
195 u32 val, addr;
196 int ret;
197
198 mutex_lock(&dev->phy_mutex);
199
200 /* confirm MII not busy */
201 ret = smsc75xx_phy_wait_not_busy(dev);
202 check_warn_goto_done(ret, "MII is busy in smsc75xx_mdio_write");
203
204 val = regval;
205 ret = smsc75xx_write_reg(dev, MII_DATA, val);
206 check_warn_goto_done(ret, "Error writing MII_DATA");
207
208 /* set the address, index & direction (write to PHY) */
209 phy_id &= dev->mii.phy_id_mask;
210 idx &= dev->mii.reg_num_mask;
211 addr = ((phy_id << MII_ACCESS_PHY_ADDR_SHIFT) & MII_ACCESS_PHY_ADDR)
212 | ((idx << MII_ACCESS_REG_ADDR_SHIFT) & MII_ACCESS_REG_ADDR)
Steve Glendinningcb8722d2012-04-30 07:56:51 +0000213 | MII_ACCESS_WRITE | MII_ACCESS_BUSY;
Steve Glendinningd0cad872010-03-16 08:46:46 +0000214 ret = smsc75xx_write_reg(dev, MII_ACCESS, addr);
215 check_warn_goto_done(ret, "Error writing MII_ACCESS");
216
217 ret = smsc75xx_phy_wait_not_busy(dev);
218 check_warn_goto_done(ret, "Timed out writing MII reg %02X", idx);
219
220done:
221 mutex_unlock(&dev->phy_mutex);
222}
223
224static int smsc75xx_wait_eeprom(struct usbnet *dev)
225{
226 unsigned long start_time = jiffies;
227 u32 val;
228 int ret;
229
230 do {
231 ret = smsc75xx_read_reg(dev, E2P_CMD, &val);
232 check_warn_return(ret, "Error reading E2P_CMD");
233
234 if (!(val & E2P_CMD_BUSY) || (val & E2P_CMD_TIMEOUT))
235 break;
236 udelay(40);
237 } while (!time_after(jiffies, start_time + HZ));
238
239 if (val & (E2P_CMD_TIMEOUT | E2P_CMD_BUSY)) {
240 netdev_warn(dev->net, "EEPROM read operation timeout");
241 return -EIO;
242 }
243
244 return 0;
245}
246
247static int smsc75xx_eeprom_confirm_not_busy(struct usbnet *dev)
248{
249 unsigned long start_time = jiffies;
250 u32 val;
251 int ret;
252
253 do {
254 ret = smsc75xx_read_reg(dev, E2P_CMD, &val);
255 check_warn_return(ret, "Error reading E2P_CMD");
256
257 if (!(val & E2P_CMD_BUSY))
258 return 0;
259
260 udelay(40);
261 } while (!time_after(jiffies, start_time + HZ));
262
263 netdev_warn(dev->net, "EEPROM is busy");
264 return -EIO;
265}
266
267static int smsc75xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
268 u8 *data)
269{
270 u32 val;
271 int i, ret;
272
273 BUG_ON(!dev);
274 BUG_ON(!data);
275
276 ret = smsc75xx_eeprom_confirm_not_busy(dev);
277 if (ret)
278 return ret;
279
280 for (i = 0; i < length; i++) {
281 val = E2P_CMD_BUSY | E2P_CMD_READ | (offset & E2P_CMD_ADDR);
282 ret = smsc75xx_write_reg(dev, E2P_CMD, val);
283 check_warn_return(ret, "Error writing E2P_CMD");
284
285 ret = smsc75xx_wait_eeprom(dev);
286 if (ret < 0)
287 return ret;
288
289 ret = smsc75xx_read_reg(dev, E2P_DATA, &val);
290 check_warn_return(ret, "Error reading E2P_DATA");
291
292 data[i] = val & 0xFF;
293 offset++;
294 }
295
296 return 0;
297}
298
299static int smsc75xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
300 u8 *data)
301{
302 u32 val;
303 int i, ret;
304
305 BUG_ON(!dev);
306 BUG_ON(!data);
307
308 ret = smsc75xx_eeprom_confirm_not_busy(dev);
309 if (ret)
310 return ret;
311
312 /* Issue write/erase enable command */
313 val = E2P_CMD_BUSY | E2P_CMD_EWEN;
314 ret = smsc75xx_write_reg(dev, E2P_CMD, val);
315 check_warn_return(ret, "Error writing E2P_CMD");
316
317 ret = smsc75xx_wait_eeprom(dev);
318 if (ret < 0)
319 return ret;
320
321 for (i = 0; i < length; i++) {
322
323 /* Fill data register */
324 val = data[i];
325 ret = smsc75xx_write_reg(dev, E2P_DATA, val);
326 check_warn_return(ret, "Error writing E2P_DATA");
327
328 /* Send "write" command */
329 val = E2P_CMD_BUSY | E2P_CMD_WRITE | (offset & E2P_CMD_ADDR);
330 ret = smsc75xx_write_reg(dev, E2P_CMD, val);
331 check_warn_return(ret, "Error writing E2P_CMD");
332
333 ret = smsc75xx_wait_eeprom(dev);
334 if (ret < 0)
335 return ret;
336
337 offset++;
338 }
339
340 return 0;
341}
342
343static int smsc75xx_dataport_wait_not_busy(struct usbnet *dev)
344{
345 int i, ret;
346
347 for (i = 0; i < 100; i++) {
348 u32 dp_sel;
349 ret = smsc75xx_read_reg(dev, DP_SEL, &dp_sel);
350 check_warn_return(ret, "Error reading DP_SEL");
351
352 if (dp_sel & DP_SEL_DPRDY)
353 return 0;
354
355 udelay(40);
356 }
357
358 netdev_warn(dev->net, "smsc75xx_dataport_wait_not_busy timed out");
359
360 return -EIO;
361}
362
363static int smsc75xx_dataport_write(struct usbnet *dev, u32 ram_select, u32 addr,
364 u32 length, u32 *buf)
365{
366 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
367 u32 dp_sel;
368 int i, ret;
369
370 mutex_lock(&pdata->dataport_mutex);
371
372 ret = smsc75xx_dataport_wait_not_busy(dev);
373 check_warn_goto_done(ret, "smsc75xx_dataport_write busy on entry");
374
375 ret = smsc75xx_read_reg(dev, DP_SEL, &dp_sel);
376 check_warn_goto_done(ret, "Error reading DP_SEL");
377
378 dp_sel &= ~DP_SEL_RSEL;
379 dp_sel |= ram_select;
380 ret = smsc75xx_write_reg(dev, DP_SEL, dp_sel);
381 check_warn_goto_done(ret, "Error writing DP_SEL");
382
383 for (i = 0; i < length; i++) {
384 ret = smsc75xx_write_reg(dev, DP_ADDR, addr + i);
385 check_warn_goto_done(ret, "Error writing DP_ADDR");
386
387 ret = smsc75xx_write_reg(dev, DP_DATA, buf[i]);
388 check_warn_goto_done(ret, "Error writing DP_DATA");
389
390 ret = smsc75xx_write_reg(dev, DP_CMD, DP_CMD_WRITE);
391 check_warn_goto_done(ret, "Error writing DP_CMD");
392
393 ret = smsc75xx_dataport_wait_not_busy(dev);
394 check_warn_goto_done(ret, "smsc75xx_dataport_write timeout");
395 }
396
397done:
398 mutex_unlock(&pdata->dataport_mutex);
399 return ret;
400}
401
402/* returns hash bit number for given MAC address */
403static u32 smsc75xx_hash(char addr[ETH_ALEN])
404{
405 return (ether_crc(ETH_ALEN, addr) >> 23) & 0x1ff;
406}
407
408static void smsc75xx_deferred_multicast_write(struct work_struct *param)
409{
410 struct smsc75xx_priv *pdata =
411 container_of(param, struct smsc75xx_priv, set_multicast);
412 struct usbnet *dev = pdata->dev;
413 int ret;
414
415 netif_dbg(dev, drv, dev->net, "deferred multicast write 0x%08x",
416 pdata->rfe_ctl);
417
418 smsc75xx_dataport_write(dev, DP_SEL_VHF, DP_SEL_VHF_VLAN_LEN,
419 DP_SEL_VHF_HASH_LEN, pdata->multicast_hash_table);
420
421 ret = smsc75xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
422 check_warn(ret, "Error writing RFE_CRL");
423}
424
425static void smsc75xx_set_multicast(struct net_device *netdev)
426{
427 struct usbnet *dev = netdev_priv(netdev);
428 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
429 unsigned long flags;
430 int i;
431
432 spin_lock_irqsave(&pdata->rfe_ctl_lock, flags);
433
434 pdata->rfe_ctl &=
435 ~(RFE_CTL_AU | RFE_CTL_AM | RFE_CTL_DPF | RFE_CTL_MHF);
436 pdata->rfe_ctl |= RFE_CTL_AB;
437
438 for (i = 0; i < DP_SEL_VHF_HASH_LEN; i++)
439 pdata->multicast_hash_table[i] = 0;
440
441 if (dev->net->flags & IFF_PROMISC) {
442 netif_dbg(dev, drv, dev->net, "promiscuous mode enabled");
443 pdata->rfe_ctl |= RFE_CTL_AM | RFE_CTL_AU;
444 } else if (dev->net->flags & IFF_ALLMULTI) {
445 netif_dbg(dev, drv, dev->net, "receive all multicast enabled");
446 pdata->rfe_ctl |= RFE_CTL_AM | RFE_CTL_DPF;
447 } else if (!netdev_mc_empty(dev->net)) {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000448 struct netdev_hw_addr *ha;
Steve Glendinningd0cad872010-03-16 08:46:46 +0000449
450 netif_dbg(dev, drv, dev->net, "receive multicast hash filter");
451
452 pdata->rfe_ctl |= RFE_CTL_MHF | RFE_CTL_DPF;
453
Jiri Pirko22bedad32010-04-01 21:22:57 +0000454 netdev_for_each_mc_addr(ha, netdev) {
455 u32 bitnum = smsc75xx_hash(ha->addr);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000456 pdata->multicast_hash_table[bitnum / 32] |=
457 (1 << (bitnum % 32));
458 }
459 } else {
460 netif_dbg(dev, drv, dev->net, "receive own packets only");
461 pdata->rfe_ctl |= RFE_CTL_DPF;
462 }
463
464 spin_unlock_irqrestore(&pdata->rfe_ctl_lock, flags);
465
466 /* defer register writes to a sleepable context */
467 schedule_work(&pdata->set_multicast);
468}
469
470static int smsc75xx_update_flowcontrol(struct usbnet *dev, u8 duplex,
471 u16 lcladv, u16 rmtadv)
472{
473 u32 flow = 0, fct_flow = 0;
474 int ret;
475
476 if (duplex == DUPLEX_FULL) {
477 u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
478
479 if (cap & FLOW_CTRL_TX) {
480 flow = (FLOW_TX_FCEN | 0xFFFF);
481 /* set fct_flow thresholds to 20% and 80% */
482 fct_flow = (8 << 8) | 32;
483 }
484
485 if (cap & FLOW_CTRL_RX)
486 flow |= FLOW_RX_FCEN;
487
488 netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s",
489 (cap & FLOW_CTRL_RX ? "enabled" : "disabled"),
490 (cap & FLOW_CTRL_TX ? "enabled" : "disabled"));
491 } else {
492 netif_dbg(dev, link, dev->net, "half duplex");
493 }
494
495 ret = smsc75xx_write_reg(dev, FLOW, flow);
496 check_warn_return(ret, "Error writing FLOW");
497
498 ret = smsc75xx_write_reg(dev, FCT_FLOW, fct_flow);
499 check_warn_return(ret, "Error writing FCT_FLOW");
500
501 return 0;
502}
503
504static int smsc75xx_link_reset(struct usbnet *dev)
505{
506 struct mii_if_info *mii = &dev->mii;
David Decotigny8ae6dac2011-04-27 18:32:38 +0000507 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
Steve Glendinningd0cad872010-03-16 08:46:46 +0000508 u16 lcladv, rmtadv;
509 int ret;
510
Steve Glendinning4f94a922012-05-04 00:57:12 +0000511 /* write to clear phy interrupt status */
Steve Glendinning77496222012-05-04 00:57:11 +0000512 smsc75xx_mdio_write(dev->net, mii->phy_id, PHY_INT_SRC,
513 PHY_INT_SRC_CLEAR_ALL);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000514
515 ret = smsc75xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL);
516 check_warn_return(ret, "Error writing INT_STS");
517
518 mii_check_media(mii, 1, 1);
519 mii_ethtool_gset(&dev->mii, &ecmd);
520 lcladv = smsc75xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
521 rmtadv = smsc75xx_mdio_read(dev->net, mii->phy_id, MII_LPA);
522
David Decotigny8ae6dac2011-04-27 18:32:38 +0000523 netif_dbg(dev, link, dev->net, "speed: %u duplex: %d lcladv: %04x"
524 " rmtadv: %04x", ethtool_cmd_speed(&ecmd),
525 ecmd.duplex, lcladv, rmtadv);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000526
527 return smsc75xx_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
528}
529
530static void smsc75xx_status(struct usbnet *dev, struct urb *urb)
531{
532 u32 intdata;
533
534 if (urb->actual_length != 4) {
535 netdev_warn(dev->net,
536 "unexpected urb length %d", urb->actual_length);
537 return;
538 }
539
540 memcpy(&intdata, urb->transfer_buffer, 4);
541 le32_to_cpus(&intdata);
542
543 netif_dbg(dev, link, dev->net, "intdata: 0x%08X", intdata);
544
545 if (intdata & INT_ENP_PHY_INT)
546 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
547 else
548 netdev_warn(dev->net,
549 "unexpected interrupt, intdata=0x%08X", intdata);
550}
551
Steve Glendinningd0cad872010-03-16 08:46:46 +0000552static int smsc75xx_ethtool_get_eeprom_len(struct net_device *net)
553{
554 return MAX_EEPROM_SIZE;
555}
556
557static int smsc75xx_ethtool_get_eeprom(struct net_device *netdev,
558 struct ethtool_eeprom *ee, u8 *data)
559{
560 struct usbnet *dev = netdev_priv(netdev);
561
562 ee->magic = LAN75XX_EEPROM_MAGIC;
563
564 return smsc75xx_read_eeprom(dev, ee->offset, ee->len, data);
565}
566
567static int smsc75xx_ethtool_set_eeprom(struct net_device *netdev,
568 struct ethtool_eeprom *ee, u8 *data)
569{
570 struct usbnet *dev = netdev_priv(netdev);
571
572 if (ee->magic != LAN75XX_EEPROM_MAGIC) {
573 netdev_warn(dev->net,
574 "EEPROM: magic value mismatch: 0x%x", ee->magic);
575 return -EINVAL;
576 }
577
578 return smsc75xx_write_eeprom(dev, ee->offset, ee->len, data);
579}
580
Steve Glendinningd0cad872010-03-16 08:46:46 +0000581static const struct ethtool_ops smsc75xx_ethtool_ops = {
582 .get_link = usbnet_get_link,
583 .nway_reset = usbnet_nway_reset,
584 .get_drvinfo = usbnet_get_drvinfo,
585 .get_msglevel = usbnet_get_msglevel,
586 .set_msglevel = usbnet_set_msglevel,
587 .get_settings = usbnet_get_settings,
588 .set_settings = usbnet_set_settings,
589 .get_eeprom_len = smsc75xx_ethtool_get_eeprom_len,
590 .get_eeprom = smsc75xx_ethtool_get_eeprom,
591 .set_eeprom = smsc75xx_ethtool_set_eeprom,
Steve Glendinningd0cad872010-03-16 08:46:46 +0000592};
593
594static int smsc75xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
595{
596 struct usbnet *dev = netdev_priv(netdev);
597
598 if (!netif_running(netdev))
599 return -EINVAL;
600
601 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
602}
603
604static void smsc75xx_init_mac_address(struct usbnet *dev)
605{
606 /* try reading mac address from EEPROM */
607 if (smsc75xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
608 dev->net->dev_addr) == 0) {
609 if (is_valid_ether_addr(dev->net->dev_addr)) {
610 /* eeprom values are valid so use them */
611 netif_dbg(dev, ifup, dev->net,
612 "MAC address read from EEPROM");
613 return;
614 }
615 }
616
617 /* no eeprom, or eeprom values are invalid. generate random MAC */
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000618 eth_hw_addr_random(dev->net);
Joe Perchesc7e12ea2012-07-12 19:33:07 +0000619 netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000620}
621
622static int smsc75xx_set_mac_address(struct usbnet *dev)
623{
624 u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
625 dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
626 u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
627
628 int ret = smsc75xx_write_reg(dev, RX_ADDRH, addr_hi);
629 check_warn_return(ret, "Failed to write RX_ADDRH: %d", ret);
630
631 ret = smsc75xx_write_reg(dev, RX_ADDRL, addr_lo);
632 check_warn_return(ret, "Failed to write RX_ADDRL: %d", ret);
633
634 addr_hi |= ADDR_FILTX_FB_VALID;
635 ret = smsc75xx_write_reg(dev, ADDR_FILTX, addr_hi);
636 check_warn_return(ret, "Failed to write ADDR_FILTX: %d", ret);
637
638 ret = smsc75xx_write_reg(dev, ADDR_FILTX + 4, addr_lo);
639 check_warn_return(ret, "Failed to write ADDR_FILTX+4: %d", ret);
640
641 return 0;
642}
643
644static int smsc75xx_phy_initialize(struct usbnet *dev)
645{
Steve Glendinningb1405042012-04-30 07:56:54 +0000646 int bmcr, ret, timeout = 0;
Steve Glendinningd0cad872010-03-16 08:46:46 +0000647
648 /* Initialize MII structure */
649 dev->mii.dev = dev->net;
650 dev->mii.mdio_read = smsc75xx_mdio_read;
651 dev->mii.mdio_write = smsc75xx_mdio_write;
652 dev->mii.phy_id_mask = 0x1f;
653 dev->mii.reg_num_mask = 0x1f;
Steve Glendinningc0b92e42012-04-30 07:56:55 +0000654 dev->mii.supports_gmii = 1;
Steve Glendinningd0cad872010-03-16 08:46:46 +0000655 dev->mii.phy_id = SMSC75XX_INTERNAL_PHY_ID;
656
657 /* reset phy and wait for reset to complete */
658 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
659
660 do {
661 msleep(10);
662 bmcr = smsc75xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
663 check_warn_return(bmcr, "Error reading MII_BMCR");
664 timeout++;
Steve Glendinning8a1d59d2012-04-30 07:56:53 +0000665 } while ((bmcr & BMCR_RESET) && (timeout < 100));
Steve Glendinningd0cad872010-03-16 08:46:46 +0000666
667 if (timeout >= 100) {
668 netdev_warn(dev->net, "timeout on PHY Reset");
669 return -EIO;
670 }
671
672 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
673 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
674 ADVERTISE_PAUSE_ASYM);
Steve Glendinningc0b92e42012-04-30 07:56:55 +0000675 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, MII_CTRL1000,
676 ADVERTISE_1000FULL);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000677
Steve Glendinningb1405042012-04-30 07:56:54 +0000678 /* read and write to clear phy interrupt status */
679 ret = smsc75xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
680 check_warn_return(ret, "Error reading PHY_INT_SRC");
681 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_SRC, 0xffff);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000682
683 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
684 PHY_INT_MASK_DEFAULT);
685 mii_nway_restart(&dev->mii);
686
687 netif_dbg(dev, ifup, dev->net, "phy initialised successfully");
688 return 0;
689}
690
691static int smsc75xx_set_rx_max_frame_length(struct usbnet *dev, int size)
692{
693 int ret = 0;
694 u32 buf;
695 bool rxenabled;
696
697 ret = smsc75xx_read_reg(dev, MAC_RX, &buf);
698 check_warn_return(ret, "Failed to read MAC_RX: %d", ret);
699
700 rxenabled = ((buf & MAC_RX_RXEN) != 0);
701
702 if (rxenabled) {
703 buf &= ~MAC_RX_RXEN;
704 ret = smsc75xx_write_reg(dev, MAC_RX, buf);
705 check_warn_return(ret, "Failed to write MAC_RX: %d", ret);
706 }
707
708 /* add 4 to size for FCS */
709 buf &= ~MAC_RX_MAX_SIZE;
710 buf |= (((size + 4) << MAC_RX_MAX_SIZE_SHIFT) & MAC_RX_MAX_SIZE);
711
712 ret = smsc75xx_write_reg(dev, MAC_RX, buf);
713 check_warn_return(ret, "Failed to write MAC_RX: %d", ret);
714
715 if (rxenabled) {
716 buf |= MAC_RX_RXEN;
717 ret = smsc75xx_write_reg(dev, MAC_RX, buf);
718 check_warn_return(ret, "Failed to write MAC_RX: %d", ret);
719 }
720
721 return 0;
722}
723
724static int smsc75xx_change_mtu(struct net_device *netdev, int new_mtu)
725{
726 struct usbnet *dev = netdev_priv(netdev);
727
728 int ret = smsc75xx_set_rx_max_frame_length(dev, new_mtu);
729 check_warn_return(ret, "Failed to set mac rx frame length");
730
731 return usbnet_change_mtu(netdev, new_mtu);
732}
733
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700734/* Enable or disable Rx checksum offload engine */
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000735static int smsc75xx_set_features(struct net_device *netdev,
736 netdev_features_t features)
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700737{
738 struct usbnet *dev = netdev_priv(netdev);
739 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
740 unsigned long flags;
741 int ret;
742
743 spin_lock_irqsave(&pdata->rfe_ctl_lock, flags);
744
745 if (features & NETIF_F_RXCSUM)
746 pdata->rfe_ctl |= RFE_CTL_TCPUDP_CKM | RFE_CTL_IP_CKM;
747 else
748 pdata->rfe_ctl &= ~(RFE_CTL_TCPUDP_CKM | RFE_CTL_IP_CKM);
749
750 spin_unlock_irqrestore(&pdata->rfe_ctl_lock, flags);
751 /* it's racing here! */
752
753 ret = smsc75xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
754 check_warn_return(ret, "Error writing RFE_CTL");
755
756 return 0;
757}
758
Steve Glendinning8762cec2012-09-28 00:57:51 +0000759static int smsc75xx_wait_ready(struct usbnet *dev)
760{
761 int timeout = 0;
762
763 do {
764 u32 buf;
765 int ret = smsc75xx_read_reg(dev, PMT_CTL, &buf);
766 check_warn_return(ret, "Failed to read PMT_CTL: %d", ret);
767
768 if (buf & PMT_CTL_DEV_RDY)
769 return 0;
770
771 msleep(10);
772 timeout++;
773 } while (timeout < 100);
774
775 netdev_warn(dev->net, "timeout waiting for device ready");
776 return -EIO;
777}
778
Steve Glendinningd0cad872010-03-16 08:46:46 +0000779static int smsc75xx_reset(struct usbnet *dev)
780{
781 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
782 u32 buf;
783 int ret = 0, timeout;
784
785 netif_dbg(dev, ifup, dev->net, "entering smsc75xx_reset");
786
Steve Glendinning8762cec2012-09-28 00:57:51 +0000787 ret = smsc75xx_wait_ready(dev);
788 check_warn_return(ret, "device not ready in smsc75xx_reset");
789
Steve Glendinningd0cad872010-03-16 08:46:46 +0000790 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
791 check_warn_return(ret, "Failed to read HW_CFG: %d", ret);
792
793 buf |= HW_CFG_LRST;
794
795 ret = smsc75xx_write_reg(dev, HW_CFG, buf);
796 check_warn_return(ret, "Failed to write HW_CFG: %d", ret);
797
798 timeout = 0;
799 do {
800 msleep(10);
801 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
802 check_warn_return(ret, "Failed to read HW_CFG: %d", ret);
803 timeout++;
804 } while ((buf & HW_CFG_LRST) && (timeout < 100));
805
806 if (timeout >= 100) {
807 netdev_warn(dev->net, "timeout on completion of Lite Reset");
808 return -EIO;
809 }
810
811 netif_dbg(dev, ifup, dev->net, "Lite reset complete, resetting PHY");
812
813 ret = smsc75xx_read_reg(dev, PMT_CTL, &buf);
814 check_warn_return(ret, "Failed to read PMT_CTL: %d", ret);
815
816 buf |= PMT_CTL_PHY_RST;
817
818 ret = smsc75xx_write_reg(dev, PMT_CTL, buf);
819 check_warn_return(ret, "Failed to write PMT_CTL: %d", ret);
820
821 timeout = 0;
822 do {
823 msleep(10);
824 ret = smsc75xx_read_reg(dev, PMT_CTL, &buf);
825 check_warn_return(ret, "Failed to read PMT_CTL: %d", ret);
826 timeout++;
827 } while ((buf & PMT_CTL_PHY_RST) && (timeout < 100));
828
829 if (timeout >= 100) {
830 netdev_warn(dev->net, "timeout waiting for PHY Reset");
831 return -EIO;
832 }
833
834 netif_dbg(dev, ifup, dev->net, "PHY reset complete");
835
836 smsc75xx_init_mac_address(dev);
837
838 ret = smsc75xx_set_mac_address(dev);
839 check_warn_return(ret, "Failed to set mac address");
840
841 netif_dbg(dev, ifup, dev->net, "MAC Address: %pM", dev->net->dev_addr);
842
843 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
844 check_warn_return(ret, "Failed to read HW_CFG: %d", ret);
845
846 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x", buf);
847
848 buf |= HW_CFG_BIR;
849
850 ret = smsc75xx_write_reg(dev, HW_CFG, buf);
851 check_warn_return(ret, "Failed to write HW_CFG: %d", ret);
852
853 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
854 check_warn_return(ret, "Failed to read HW_CFG: %d", ret);
855
856 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG after "
857 "writing HW_CFG_BIR: 0x%08x", buf);
858
859 if (!turbo_mode) {
860 buf = 0;
861 dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
862 } else if (dev->udev->speed == USB_SPEED_HIGH) {
863 buf = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
864 dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
865 } else {
866 buf = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
867 dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
868 }
869
870 netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld",
871 (ulong)dev->rx_urb_size);
872
873 ret = smsc75xx_write_reg(dev, BURST_CAP, buf);
874 check_warn_return(ret, "Failed to write BURST_CAP: %d", ret);
875
876 ret = smsc75xx_read_reg(dev, BURST_CAP, &buf);
877 check_warn_return(ret, "Failed to read BURST_CAP: %d", ret);
878
879 netif_dbg(dev, ifup, dev->net,
880 "Read Value from BURST_CAP after writing: 0x%08x", buf);
881
882 ret = smsc75xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
883 check_warn_return(ret, "Failed to write BULK_IN_DLY: %d", ret);
884
885 ret = smsc75xx_read_reg(dev, BULK_IN_DLY, &buf);
886 check_warn_return(ret, "Failed to read BULK_IN_DLY: %d", ret);
887
888 netif_dbg(dev, ifup, dev->net,
889 "Read Value from BULK_IN_DLY after writing: 0x%08x", buf);
890
891 if (turbo_mode) {
892 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
893 check_warn_return(ret, "Failed to read HW_CFG: %d", ret);
894
895 netif_dbg(dev, ifup, dev->net, "HW_CFG: 0x%08x", buf);
896
897 buf |= (HW_CFG_MEF | HW_CFG_BCE);
898
899 ret = smsc75xx_write_reg(dev, HW_CFG, buf);
900 check_warn_return(ret, "Failed to write HW_CFG: %d", ret);
901
902 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
903 check_warn_return(ret, "Failed to read HW_CFG: %d", ret);
904
905 netif_dbg(dev, ifup, dev->net, "HW_CFG: 0x%08x", buf);
906 }
907
908 /* set FIFO sizes */
909 buf = (MAX_RX_FIFO_SIZE - 512) / 512;
910 ret = smsc75xx_write_reg(dev, FCT_RX_FIFO_END, buf);
911 check_warn_return(ret, "Failed to write FCT_RX_FIFO_END: %d", ret);
912
913 netif_dbg(dev, ifup, dev->net, "FCT_RX_FIFO_END set to 0x%08x", buf);
914
915 buf = (MAX_TX_FIFO_SIZE - 512) / 512;
916 ret = smsc75xx_write_reg(dev, FCT_TX_FIFO_END, buf);
917 check_warn_return(ret, "Failed to write FCT_TX_FIFO_END: %d", ret);
918
919 netif_dbg(dev, ifup, dev->net, "FCT_TX_FIFO_END set to 0x%08x", buf);
920
921 ret = smsc75xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL);
922 check_warn_return(ret, "Failed to write INT_STS: %d", ret);
923
924 ret = smsc75xx_read_reg(dev, ID_REV, &buf);
925 check_warn_return(ret, "Failed to read ID_REV: %d", ret);
926
927 netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x", buf);
928
Steve Glendinning97138a12012-05-04 00:57:13 +0000929 ret = smsc75xx_read_reg(dev, E2P_CMD, &buf);
930 check_warn_return(ret, "Failed to read E2P_CMD: %d", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000931
Steve Glendinning97138a12012-05-04 00:57:13 +0000932 /* only set default GPIO/LED settings if no EEPROM is detected */
933 if (!(buf & E2P_CMD_LOADED)) {
934 ret = smsc75xx_read_reg(dev, LED_GPIO_CFG, &buf);
935 check_warn_return(ret, "Failed to read LED_GPIO_CFG: %d", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000936
Steve Glendinning97138a12012-05-04 00:57:13 +0000937 buf &= ~(LED_GPIO_CFG_LED2_FUN_SEL | LED_GPIO_CFG_LED10_FUN_SEL);
938 buf |= LED_GPIO_CFG_LEDGPIO_EN | LED_GPIO_CFG_LED2_FUN_SEL;
939
940 ret = smsc75xx_write_reg(dev, LED_GPIO_CFG, buf);
941 check_warn_return(ret, "Failed to write LED_GPIO_CFG: %d", ret);
942 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000943
944 ret = smsc75xx_write_reg(dev, FLOW, 0);
945 check_warn_return(ret, "Failed to write FLOW: %d", ret);
946
947 ret = smsc75xx_write_reg(dev, FCT_FLOW, 0);
948 check_warn_return(ret, "Failed to write FCT_FLOW: %d", ret);
949
950 /* Don't need rfe_ctl_lock during initialisation */
951 ret = smsc75xx_read_reg(dev, RFE_CTL, &pdata->rfe_ctl);
952 check_warn_return(ret, "Failed to read RFE_CTL: %d", ret);
953
954 pdata->rfe_ctl |= RFE_CTL_AB | RFE_CTL_DPF;
955
956 ret = smsc75xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
957 check_warn_return(ret, "Failed to write RFE_CTL: %d", ret);
958
959 ret = smsc75xx_read_reg(dev, RFE_CTL, &pdata->rfe_ctl);
960 check_warn_return(ret, "Failed to read RFE_CTL: %d", ret);
961
962 netif_dbg(dev, ifup, dev->net, "RFE_CTL set to 0x%08x", pdata->rfe_ctl);
963
964 /* Enable or disable checksum offload engines */
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700965 smsc75xx_set_features(dev->net, dev->net->features);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000966
967 smsc75xx_set_multicast(dev->net);
968
969 ret = smsc75xx_phy_initialize(dev);
970 check_warn_return(ret, "Failed to initialize PHY: %d", ret);
971
972 ret = smsc75xx_read_reg(dev, INT_EP_CTL, &buf);
973 check_warn_return(ret, "Failed to read INT_EP_CTL: %d", ret);
974
975 /* enable PHY interrupts */
976 buf |= INT_ENP_PHY_INT;
977
978 ret = smsc75xx_write_reg(dev, INT_EP_CTL, buf);
979 check_warn_return(ret, "Failed to write INT_EP_CTL: %d", ret);
980
Steve Glendinning2f3a0812012-04-30 07:56:56 +0000981 /* allow mac to detect speed and duplex from phy */
982 ret = smsc75xx_read_reg(dev, MAC_CR, &buf);
983 check_warn_return(ret, "Failed to read MAC_CR: %d", ret);
984
985 buf |= (MAC_CR_ADD | MAC_CR_ASD);
986 ret = smsc75xx_write_reg(dev, MAC_CR, buf);
987 check_warn_return(ret, "Failed to write MAC_CR: %d", ret);
988
Steve Glendinningd0cad872010-03-16 08:46:46 +0000989 ret = smsc75xx_read_reg(dev, MAC_TX, &buf);
990 check_warn_return(ret, "Failed to read MAC_TX: %d", ret);
991
992 buf |= MAC_TX_TXEN;
993
994 ret = smsc75xx_write_reg(dev, MAC_TX, buf);
995 check_warn_return(ret, "Failed to write MAC_TX: %d", ret);
996
997 netif_dbg(dev, ifup, dev->net, "MAC_TX set to 0x%08x", buf);
998
999 ret = smsc75xx_read_reg(dev, FCT_TX_CTL, &buf);
1000 check_warn_return(ret, "Failed to read FCT_TX_CTL: %d", ret);
1001
1002 buf |= FCT_TX_CTL_EN;
1003
1004 ret = smsc75xx_write_reg(dev, FCT_TX_CTL, buf);
1005 check_warn_return(ret, "Failed to write FCT_TX_CTL: %d", ret);
1006
1007 netif_dbg(dev, ifup, dev->net, "FCT_TX_CTL set to 0x%08x", buf);
1008
1009 ret = smsc75xx_set_rx_max_frame_length(dev, 1514);
1010 check_warn_return(ret, "Failed to set max rx frame length");
1011
1012 ret = smsc75xx_read_reg(dev, MAC_RX, &buf);
1013 check_warn_return(ret, "Failed to read MAC_RX: %d", ret);
1014
1015 buf |= MAC_RX_RXEN;
1016
1017 ret = smsc75xx_write_reg(dev, MAC_RX, buf);
1018 check_warn_return(ret, "Failed to write MAC_RX: %d", ret);
1019
1020 netif_dbg(dev, ifup, dev->net, "MAC_RX set to 0x%08x", buf);
1021
1022 ret = smsc75xx_read_reg(dev, FCT_RX_CTL, &buf);
1023 check_warn_return(ret, "Failed to read FCT_RX_CTL: %d", ret);
1024
1025 buf |= FCT_RX_CTL_EN;
1026
1027 ret = smsc75xx_write_reg(dev, FCT_RX_CTL, buf);
1028 check_warn_return(ret, "Failed to write FCT_RX_CTL: %d", ret);
1029
1030 netif_dbg(dev, ifup, dev->net, "FCT_RX_CTL set to 0x%08x", buf);
1031
1032 netif_dbg(dev, ifup, dev->net, "smsc75xx_reset, return 0");
1033 return 0;
1034}
1035
1036static const struct net_device_ops smsc75xx_netdev_ops = {
1037 .ndo_open = usbnet_open,
1038 .ndo_stop = usbnet_stop,
1039 .ndo_start_xmit = usbnet_start_xmit,
1040 .ndo_tx_timeout = usbnet_tx_timeout,
1041 .ndo_change_mtu = smsc75xx_change_mtu,
1042 .ndo_set_mac_address = eth_mac_addr,
1043 .ndo_validate_addr = eth_validate_addr,
1044 .ndo_do_ioctl = smsc75xx_ioctl,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001045 .ndo_set_rx_mode = smsc75xx_set_multicast,
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001046 .ndo_set_features = smsc75xx_set_features,
Steve Glendinningd0cad872010-03-16 08:46:46 +00001047};
1048
1049static int smsc75xx_bind(struct usbnet *dev, struct usb_interface *intf)
1050{
1051 struct smsc75xx_priv *pdata = NULL;
1052 int ret;
1053
1054 printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
1055
1056 ret = usbnet_get_endpoints(dev, intf);
1057 check_warn_return(ret, "usbnet_get_endpoints failed: %d", ret);
1058
1059 dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc75xx_priv),
1060 GFP_KERNEL);
1061
1062 pdata = (struct smsc75xx_priv *)(dev->data[0]);
1063 if (!pdata) {
1064 netdev_warn(dev->net, "Unable to allocate smsc75xx_priv");
1065 return -ENOMEM;
1066 }
1067
1068 pdata->dev = dev;
1069
1070 spin_lock_init(&pdata->rfe_ctl_lock);
1071 mutex_init(&pdata->dataport_mutex);
1072
1073 INIT_WORK(&pdata->set_multicast, smsc75xx_deferred_multicast_write);
1074
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001075 if (DEFAULT_TX_CSUM_ENABLE) {
1076 dev->net->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1077 if (DEFAULT_TSO_ENABLE)
1078 dev->net->features |= NETIF_F_SG |
1079 NETIF_F_TSO | NETIF_F_TSO6;
1080 }
1081 if (DEFAULT_RX_CSUM_ENABLE)
1082 dev->net->features |= NETIF_F_RXCSUM;
Steve Glendinningd0cad872010-03-16 08:46:46 +00001083
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001084 dev->net->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1085 NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_RXCSUM;
Steve Glendinningd0cad872010-03-16 08:46:46 +00001086
1087 /* Init all registers */
1088 ret = smsc75xx_reset(dev);
1089
1090 dev->net->netdev_ops = &smsc75xx_netdev_ops;
1091 dev->net->ethtool_ops = &smsc75xx_ethtool_ops;
1092 dev->net->flags |= IFF_MULTICAST;
1093 dev->net->hard_header_len += SMSC75XX_TX_OVERHEAD;
Stephane Filloda99ff7d2012-04-15 11:38:29 +00001094 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
Steve Glendinningd0cad872010-03-16 08:46:46 +00001095 return 0;
1096}
1097
1098static void smsc75xx_unbind(struct usbnet *dev, struct usb_interface *intf)
1099{
1100 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1101 if (pdata) {
1102 netif_dbg(dev, ifdown, dev->net, "free pdata");
1103 kfree(pdata);
1104 pdata = NULL;
1105 dev->data[0] = 0;
1106 }
1107}
1108
Steve Glendinning16c79a02012-09-28 00:57:52 +00001109static int smsc75xx_suspend(struct usb_interface *intf, pm_message_t message)
1110{
1111 struct usbnet *dev = usb_get_intfdata(intf);
1112 int ret;
1113 u32 val;
1114
1115 if (WARN_ON_ONCE(!dev))
1116 return -EINVAL;
1117
1118 ret = usbnet_suspend(intf, message);
1119 check_warn_return(ret, "usbnet_suspend error");
1120
1121 netdev_info(dev->net, "entering SUSPEND2 mode");
1122
1123 ret = smsc75xx_read_reg(dev, PMT_CTL, &val);
1124 check_warn_return(ret, "Error reading PMT_CTL");
1125
1126 val &= ~(PMT_CTL_SUS_MODE | PMT_CTL_WUPS | PMT_CTL_PHY_RST);
1127 val |= PMT_CTL_SUS_MODE_2;
1128
1129 ret = smsc75xx_write_reg(dev, PMT_CTL, val);
1130 check_warn_return(ret, "Error writing PMT_CTL");
1131
1132 return 0;
1133}
1134
1135static int smsc75xx_resume(struct usb_interface *intf)
1136{
1137 struct usbnet *dev = usb_get_intfdata(intf);
1138 int ret;
1139 u32 val;
1140
1141 if (WARN_ON_ONCE(!dev))
1142 return -EINVAL;
1143
1144 netdev_info(dev->net, "resuming from SUSPEND2");
1145
1146 ret = smsc75xx_read_reg(dev, PMT_CTL, &val);
1147 check_warn_return(ret, "Error reading PMT_CTL");
1148
1149 val |= PMT_CTL_PHY_PWRUP;
1150
1151 ret = smsc75xx_write_reg(dev, PMT_CTL, val);
1152 check_warn_return(ret, "Error writing PMT_CTL");
1153
1154 ret = smsc75xx_wait_ready(dev);
1155 check_warn_return(ret, "device not ready in smsc75xx_resume");
1156
1157 return usbnet_resume(intf);
1158}
1159
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001160static void smsc75xx_rx_csum_offload(struct usbnet *dev, struct sk_buff *skb,
1161 u32 rx_cmd_a, u32 rx_cmd_b)
Steve Glendinningd0cad872010-03-16 08:46:46 +00001162{
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001163 if (!(dev->net->features & NETIF_F_RXCSUM) ||
1164 unlikely(rx_cmd_a & RX_CMD_A_LCSM)) {
Steve Glendinningd0cad872010-03-16 08:46:46 +00001165 skb->ip_summed = CHECKSUM_NONE;
1166 } else {
1167 skb->csum = ntohs((u16)(rx_cmd_b >> RX_CMD_B_CSUM_SHIFT));
1168 skb->ip_summed = CHECKSUM_COMPLETE;
1169 }
1170}
1171
1172static int smsc75xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
1173{
Steve Glendinningd0cad872010-03-16 08:46:46 +00001174 while (skb->len > 0) {
1175 u32 rx_cmd_a, rx_cmd_b, align_count, size;
1176 struct sk_buff *ax_skb;
1177 unsigned char *packet;
1178
1179 memcpy(&rx_cmd_a, skb->data, sizeof(rx_cmd_a));
1180 le32_to_cpus(&rx_cmd_a);
1181 skb_pull(skb, 4);
1182
1183 memcpy(&rx_cmd_b, skb->data, sizeof(rx_cmd_b));
1184 le32_to_cpus(&rx_cmd_b);
Nico Erfurthea1649d2011-11-08 07:30:40 +00001185 skb_pull(skb, 4 + RXW_PADDING);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001186
1187 packet = skb->data;
1188
1189 /* get the packet length */
Nico Erfurthea1649d2011-11-08 07:30:40 +00001190 size = (rx_cmd_a & RX_CMD_A_LEN) - RXW_PADDING;
1191 align_count = (4 - ((size + RXW_PADDING) % 4)) % 4;
Steve Glendinningd0cad872010-03-16 08:46:46 +00001192
1193 if (unlikely(rx_cmd_a & RX_CMD_A_RED)) {
1194 netif_dbg(dev, rx_err, dev->net,
1195 "Error rx_cmd_a=0x%08x", rx_cmd_a);
1196 dev->net->stats.rx_errors++;
1197 dev->net->stats.rx_dropped++;
1198
1199 if (rx_cmd_a & RX_CMD_A_FCS)
1200 dev->net->stats.rx_crc_errors++;
1201 else if (rx_cmd_a & (RX_CMD_A_LONG | RX_CMD_A_RUNT))
1202 dev->net->stats.rx_frame_errors++;
1203 } else {
1204 /* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
1205 if (unlikely(size > (ETH_FRAME_LEN + 12))) {
1206 netif_dbg(dev, rx_err, dev->net,
1207 "size err rx_cmd_a=0x%08x", rx_cmd_a);
1208 return 0;
1209 }
1210
1211 /* last frame in this batch */
1212 if (skb->len == size) {
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001213 smsc75xx_rx_csum_offload(dev, skb, rx_cmd_a,
1214 rx_cmd_b);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001215
1216 skb_trim(skb, skb->len - 4); /* remove fcs */
1217 skb->truesize = size + sizeof(struct sk_buff);
1218
1219 return 1;
1220 }
1221
1222 ax_skb = skb_clone(skb, GFP_ATOMIC);
1223 if (unlikely(!ax_skb)) {
1224 netdev_warn(dev->net, "Error allocating skb");
1225 return 0;
1226 }
1227
1228 ax_skb->len = size;
1229 ax_skb->data = packet;
1230 skb_set_tail_pointer(ax_skb, size);
1231
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001232 smsc75xx_rx_csum_offload(dev, ax_skb, rx_cmd_a,
1233 rx_cmd_b);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001234
1235 skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
1236 ax_skb->truesize = size + sizeof(struct sk_buff);
1237
1238 usbnet_skb_return(dev, ax_skb);
1239 }
1240
1241 skb_pull(skb, size);
1242
1243 /* padding bytes before the next frame starts */
1244 if (skb->len)
1245 skb_pull(skb, align_count);
1246 }
1247
1248 if (unlikely(skb->len < 0)) {
1249 netdev_warn(dev->net, "invalid rx length<0 %d", skb->len);
1250 return 0;
1251 }
1252
1253 return 1;
1254}
1255
1256static struct sk_buff *smsc75xx_tx_fixup(struct usbnet *dev,
1257 struct sk_buff *skb, gfp_t flags)
1258{
1259 u32 tx_cmd_a, tx_cmd_b;
1260
1261 skb_linearize(skb);
1262
1263 if (skb_headroom(skb) < SMSC75XX_TX_OVERHEAD) {
1264 struct sk_buff *skb2 =
1265 skb_copy_expand(skb, SMSC75XX_TX_OVERHEAD, 0, flags);
1266 dev_kfree_skb_any(skb);
1267 skb = skb2;
1268 if (!skb)
1269 return NULL;
1270 }
1271
1272 tx_cmd_a = (u32)(skb->len & TX_CMD_A_LEN) | TX_CMD_A_FCS;
1273
1274 if (skb->ip_summed == CHECKSUM_PARTIAL)
1275 tx_cmd_a |= TX_CMD_A_IPE | TX_CMD_A_TPE;
1276
1277 if (skb_is_gso(skb)) {
1278 u16 mss = max(skb_shinfo(skb)->gso_size, TX_MSS_MIN);
1279 tx_cmd_b = (mss << TX_CMD_B_MSS_SHIFT) & TX_CMD_B_MSS;
1280
1281 tx_cmd_a |= TX_CMD_A_LSO;
1282 } else {
1283 tx_cmd_b = 0;
1284 }
1285
1286 skb_push(skb, 4);
1287 cpu_to_le32s(&tx_cmd_b);
1288 memcpy(skb->data, &tx_cmd_b, 4);
1289
1290 skb_push(skb, 4);
1291 cpu_to_le32s(&tx_cmd_a);
1292 memcpy(skb->data, &tx_cmd_a, 4);
1293
1294 return skb;
1295}
1296
1297static const struct driver_info smsc75xx_info = {
1298 .description = "smsc75xx USB 2.0 Gigabit Ethernet",
1299 .bind = smsc75xx_bind,
1300 .unbind = smsc75xx_unbind,
1301 .link_reset = smsc75xx_link_reset,
1302 .reset = smsc75xx_reset,
1303 .rx_fixup = smsc75xx_rx_fixup,
1304 .tx_fixup = smsc75xx_tx_fixup,
1305 .status = smsc75xx_status,
Steve Glendinning7bdd3052012-04-30 07:56:50 +00001306 .flags = FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
Steve Glendinningd0cad872010-03-16 08:46:46 +00001307};
1308
1309static const struct usb_device_id products[] = {
1310 {
1311 /* SMSC7500 USB Gigabit Ethernet Device */
1312 USB_DEVICE(USB_VENDOR_ID_SMSC, USB_PRODUCT_ID_LAN7500),
1313 .driver_info = (unsigned long) &smsc75xx_info,
1314 },
1315 {
1316 /* SMSC7500 USB Gigabit Ethernet Device */
1317 USB_DEVICE(USB_VENDOR_ID_SMSC, USB_PRODUCT_ID_LAN7505),
1318 .driver_info = (unsigned long) &smsc75xx_info,
1319 },
1320 { }, /* END */
1321};
1322MODULE_DEVICE_TABLE(usb, products);
1323
1324static struct usb_driver smsc75xx_driver = {
1325 .name = SMSC_CHIPNAME,
1326 .id_table = products,
1327 .probe = usbnet_probe,
Steve Glendinning16c79a02012-09-28 00:57:52 +00001328 .suspend = smsc75xx_suspend,
1329 .resume = smsc75xx_resume,
1330 .reset_resume = smsc75xx_resume,
Steve Glendinningd0cad872010-03-16 08:46:46 +00001331 .disconnect = usbnet_disconnect,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001332 .disable_hub_initiated_lpm = 1,
Steve Glendinningd0cad872010-03-16 08:46:46 +00001333};
1334
Greg Kroah-Hartmand632eb12011-11-18 09:44:20 -08001335module_usb_driver(smsc75xx_driver);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001336
1337MODULE_AUTHOR("Nancy Lin");
Steve Glendinning90b24cf2012-04-16 12:13:29 +01001338MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
Steve Glendinningd0cad872010-03-16 08:46:46 +00001339MODULE_DESCRIPTION("SMSC75XX USB 2.0 Gigabit Ethernet Devices");
1340MODULE_LICENSE("GPL");