blob: 1ada51eb71f7df3409ce77992d2a6d99ddd0b826 [file] [log] [blame]
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001 /***************************************************************************
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, 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>
31#include "smsc95xx.h"
32
33#define SMSC_CHIPNAME "smsc95xx"
Steve Glendinningf7b29272008-11-20 04:19:21 -080034#define SMSC_DRIVER_VERSION "1.0.4"
Steve Glendinning2f7ca802008-10-02 05:27:57 +000035#define HS_USB_PKT_SIZE (512)
36#define FS_USB_PKT_SIZE (64)
37#define DEFAULT_HS_BURST_CAP_SIZE (16 * 1024 + 5 * HS_USB_PKT_SIZE)
38#define DEFAULT_FS_BURST_CAP_SIZE (6 * 1024 + 33 * FS_USB_PKT_SIZE)
39#define DEFAULT_BULK_IN_DELAY (0x00002000)
40#define MAX_SINGLE_PACKET_SIZE (2048)
41#define LAN95XX_EEPROM_MAGIC (0x9500)
42#define EEPROM_MAC_OFFSET (0x01)
Steve Glendinningf7b29272008-11-20 04:19:21 -080043#define DEFAULT_TX_CSUM_ENABLE (true)
Steve Glendinning2f7ca802008-10-02 05:27:57 +000044#define DEFAULT_RX_CSUM_ENABLE (true)
45#define SMSC95XX_INTERNAL_PHY_ID (1)
46#define SMSC95XX_TX_OVERHEAD (8)
Steve Glendinningf7b29272008-11-20 04:19:21 -080047#define SMSC95XX_TX_OVERHEAD_CSUM (12)
Steve Glendinning2f7ca802008-10-02 05:27:57 +000048
49struct smsc95xx_priv {
50 u32 mac_cr;
51 spinlock_t mac_cr_lock;
Steve Glendinningf7b29272008-11-20 04:19:21 -080052 bool use_tx_csum;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000053 bool use_rx_csum;
54};
55
56struct usb_context {
57 struct usb_ctrlrequest req;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000058 struct usbnet *dev;
59};
60
Hannes Eder0227abc2009-02-14 11:47:47 +000061static int turbo_mode = true;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000062module_param(turbo_mode, bool, 0644);
63MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
64
65static int smsc95xx_read_reg(struct usbnet *dev, u32 index, u32 *data)
66{
67 u32 *buf = kmalloc(4, GFP_KERNEL);
68 int ret;
69
70 BUG_ON(!dev);
71
72 if (!buf)
73 return -ENOMEM;
74
75 ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
76 USB_VENDOR_REQUEST_READ_REGISTER,
77 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
78 00, index, buf, 4, USB_CTRL_GET_TIMEOUT);
79
80 if (unlikely(ret < 0))
Joe Perches60b86752010-02-17 10:30:23 +000081 netdev_warn(dev->net, "Failed to read register index 0x%08x\n", index);
Steve Glendinning2f7ca802008-10-02 05:27:57 +000082
83 le32_to_cpus(buf);
84 *data = *buf;
85 kfree(buf);
86
87 return ret;
88}
89
90static int smsc95xx_write_reg(struct usbnet *dev, u32 index, u32 data)
91{
92 u32 *buf = kmalloc(4, GFP_KERNEL);
93 int ret;
94
95 BUG_ON(!dev);
96
97 if (!buf)
98 return -ENOMEM;
99
100 *buf = data;
101 cpu_to_le32s(buf);
102
103 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
104 USB_VENDOR_REQUEST_WRITE_REGISTER,
105 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
106 00, index, buf, 4, USB_CTRL_SET_TIMEOUT);
107
108 if (unlikely(ret < 0))
Joe Perches60b86752010-02-17 10:30:23 +0000109 netdev_warn(dev->net, "Failed to write register index 0x%08x\n", index);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000110
111 kfree(buf);
112
113 return ret;
114}
115
116/* Loop until the read is completed with timeout
117 * called with phy_mutex held */
118static int smsc95xx_phy_wait_not_busy(struct usbnet *dev)
119{
120 unsigned long start_time = jiffies;
121 u32 val;
122
123 do {
124 smsc95xx_read_reg(dev, MII_ADDR, &val);
125 if (!(val & MII_BUSY_))
126 return 0;
127 } while (!time_after(jiffies, start_time + HZ));
128
129 return -EIO;
130}
131
132static int smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
133{
134 struct usbnet *dev = netdev_priv(netdev);
135 u32 val, addr;
136
137 mutex_lock(&dev->phy_mutex);
138
139 /* confirm MII not busy */
140 if (smsc95xx_phy_wait_not_busy(dev)) {
Joe Perches60b86752010-02-17 10:30:23 +0000141 netdev_warn(dev->net, "MII is busy in smsc95xx_mdio_read\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000142 mutex_unlock(&dev->phy_mutex);
143 return -EIO;
144 }
145
146 /* set the address, index & direction (read from PHY) */
147 phy_id &= dev->mii.phy_id_mask;
148 idx &= dev->mii.reg_num_mask;
149 addr = (phy_id << 11) | (idx << 6) | MII_READ_;
150 smsc95xx_write_reg(dev, MII_ADDR, addr);
151
152 if (smsc95xx_phy_wait_not_busy(dev)) {
Joe Perches60b86752010-02-17 10:30:23 +0000153 netdev_warn(dev->net, "Timed out reading MII reg %02X\n", idx);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000154 mutex_unlock(&dev->phy_mutex);
155 return -EIO;
156 }
157
158 smsc95xx_read_reg(dev, MII_DATA, &val);
159
160 mutex_unlock(&dev->phy_mutex);
161
162 return (u16)(val & 0xFFFF);
163}
164
165static void smsc95xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
166 int regval)
167{
168 struct usbnet *dev = netdev_priv(netdev);
169 u32 val, addr;
170
171 mutex_lock(&dev->phy_mutex);
172
173 /* confirm MII not busy */
174 if (smsc95xx_phy_wait_not_busy(dev)) {
Joe Perches60b86752010-02-17 10:30:23 +0000175 netdev_warn(dev->net, "MII is busy in smsc95xx_mdio_write\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000176 mutex_unlock(&dev->phy_mutex);
177 return;
178 }
179
180 val = regval;
181 smsc95xx_write_reg(dev, MII_DATA, val);
182
183 /* set the address, index & direction (write to PHY) */
184 phy_id &= dev->mii.phy_id_mask;
185 idx &= dev->mii.reg_num_mask;
186 addr = (phy_id << 11) | (idx << 6) | MII_WRITE_;
187 smsc95xx_write_reg(dev, MII_ADDR, addr);
188
189 if (smsc95xx_phy_wait_not_busy(dev))
Joe Perches60b86752010-02-17 10:30:23 +0000190 netdev_warn(dev->net, "Timed out writing MII reg %02X\n", idx);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000191
192 mutex_unlock(&dev->phy_mutex);
193}
194
195static int smsc95xx_wait_eeprom(struct usbnet *dev)
196{
197 unsigned long start_time = jiffies;
198 u32 val;
199
200 do {
201 smsc95xx_read_reg(dev, E2P_CMD, &val);
202 if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_))
203 break;
204 udelay(40);
205 } while (!time_after(jiffies, start_time + HZ));
206
207 if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) {
Joe Perches60b86752010-02-17 10:30:23 +0000208 netdev_warn(dev->net, "EEPROM read operation timeout\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000209 return -EIO;
210 }
211
212 return 0;
213}
214
215static int smsc95xx_eeprom_confirm_not_busy(struct usbnet *dev)
216{
217 unsigned long start_time = jiffies;
218 u32 val;
219
220 do {
221 smsc95xx_read_reg(dev, E2P_CMD, &val);
222
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000223 if (!(val & E2P_CMD_BUSY_))
224 return 0;
225
226 udelay(40);
227 } while (!time_after(jiffies, start_time + HZ));
228
Joe Perches60b86752010-02-17 10:30:23 +0000229 netdev_warn(dev->net, "EEPROM is busy\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000230 return -EIO;
231}
232
233static int smsc95xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
234 u8 *data)
235{
236 u32 val;
237 int i, ret;
238
239 BUG_ON(!dev);
240 BUG_ON(!data);
241
242 ret = smsc95xx_eeprom_confirm_not_busy(dev);
243 if (ret)
244 return ret;
245
246 for (i = 0; i < length; i++) {
247 val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_);
248 smsc95xx_write_reg(dev, E2P_CMD, val);
249
250 ret = smsc95xx_wait_eeprom(dev);
251 if (ret < 0)
252 return ret;
253
254 smsc95xx_read_reg(dev, E2P_DATA, &val);
255
256 data[i] = val & 0xFF;
257 offset++;
258 }
259
260 return 0;
261}
262
263static int smsc95xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
264 u8 *data)
265{
266 u32 val;
267 int i, ret;
268
269 BUG_ON(!dev);
270 BUG_ON(!data);
271
272 ret = smsc95xx_eeprom_confirm_not_busy(dev);
273 if (ret)
274 return ret;
275
276 /* Issue write/erase enable command */
277 val = E2P_CMD_BUSY_ | E2P_CMD_EWEN_;
278 smsc95xx_write_reg(dev, E2P_CMD, val);
279
280 ret = smsc95xx_wait_eeprom(dev);
281 if (ret < 0)
282 return ret;
283
284 for (i = 0; i < length; i++) {
285
286 /* Fill data register */
287 val = data[i];
288 smsc95xx_write_reg(dev, E2P_DATA, val);
289
290 /* Send "write" command */
291 val = E2P_CMD_BUSY_ | E2P_CMD_WRITE_ | (offset & E2P_CMD_ADDR_);
292 smsc95xx_write_reg(dev, E2P_CMD, val);
293
294 ret = smsc95xx_wait_eeprom(dev);
295 if (ret < 0)
296 return ret;
297
298 offset++;
299 }
300
301 return 0;
302}
303
Steve Glendinning150a7fc2009-01-25 17:54:46 -0800304static void smsc95xx_async_cmd_callback(struct urb *urb)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000305{
306 struct usb_context *usb_context = urb->context;
307 struct usbnet *dev = usb_context->dev;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800308 int status = urb->status;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000309
Oliver Neukumc94cb312008-12-18 23:00:59 -0800310 if (status < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000311 netdev_warn(dev->net, "async callback failed with %d\n", status);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000312
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000313 kfree(usb_context);
314 usb_free_urb(urb);
315}
316
Steve Glendinning1d74a6b2008-10-09 14:34:47 -0700317static int smsc95xx_write_reg_async(struct usbnet *dev, u16 index, u32 *data)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000318{
319 struct usb_context *usb_context;
320 int status;
321 struct urb *urb;
Steve Glendinning1d74a6b2008-10-09 14:34:47 -0700322 const u16 size = 4;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000323
324 urb = usb_alloc_urb(0, GFP_ATOMIC);
325 if (!urb) {
Joe Perches60b86752010-02-17 10:30:23 +0000326 netdev_warn(dev->net, "Error allocating URB\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000327 return -ENOMEM;
328 }
329
330 usb_context = kmalloc(sizeof(struct usb_context), GFP_ATOMIC);
331 if (usb_context == NULL) {
Joe Perches60b86752010-02-17 10:30:23 +0000332 netdev_warn(dev->net, "Error allocating control msg\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000333 usb_free_urb(urb);
334 return -ENOMEM;
335 }
336
337 usb_context->req.bRequestType =
338 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
339 usb_context->req.bRequest = USB_VENDOR_REQUEST_WRITE_REGISTER;
340 usb_context->req.wValue = 00;
Steve Glendinning1d74a6b2008-10-09 14:34:47 -0700341 usb_context->req.wIndex = cpu_to_le16(index);
342 usb_context->req.wLength = cpu_to_le16(size);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000343
344 usb_fill_control_urb(urb, dev->udev, usb_sndctrlpipe(dev->udev, 0),
345 (void *)&usb_context->req, data, size,
Steve Glendinning150a7fc2009-01-25 17:54:46 -0800346 smsc95xx_async_cmd_callback,
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000347 (void *)usb_context);
348
349 status = usb_submit_urb(urb, GFP_ATOMIC);
350 if (status < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000351 netdev_warn(dev->net, "Error submitting control msg, sts=%d\n",
352 status);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000353 kfree(usb_context);
354 usb_free_urb(urb);
355 }
356
357 return status;
358}
359
360/* returns hash bit number for given MAC address
361 * example:
362 * 01 00 5E 00 00 01 -> returns bit number 31 */
363static unsigned int smsc95xx_hash(char addr[ETH_ALEN])
364{
365 return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
366}
367
368static void smsc95xx_set_multicast(struct net_device *netdev)
369{
370 struct usbnet *dev = netdev_priv(netdev);
371 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
372 u32 hash_hi = 0;
373 u32 hash_lo = 0;
374 unsigned long flags;
375
376 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
377
378 if (dev->net->flags & IFF_PROMISC) {
Joe Perchesa475f602010-02-17 10:30:24 +0000379 netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000380 pdata->mac_cr |= MAC_CR_PRMS_;
381 pdata->mac_cr &= ~(MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
382 } else if (dev->net->flags & IFF_ALLMULTI) {
Joe Perchesa475f602010-02-17 10:30:24 +0000383 netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000384 pdata->mac_cr |= MAC_CR_MCPAS_;
385 pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_HPFILT_);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000386 } else if (!netdev_mc_empty(dev->net)) {
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000387 struct dev_mc_list *mc_list = dev->net->mc_list;
388 int count = 0;
389
390 pdata->mac_cr |= MAC_CR_HPFILT_;
391 pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);
392
393 while (mc_list) {
394 count++;
395 if (mc_list->dmi_addrlen == ETH_ALEN) {
396 u32 bitnum = smsc95xx_hash(mc_list->dmi_addr);
397 u32 mask = 0x01 << (bitnum & 0x1F);
398 if (bitnum & 0x20)
399 hash_hi |= mask;
400 else
401 hash_lo |= mask;
402 } else {
Joe Perches60b86752010-02-17 10:30:23 +0000403 netdev_warn(dev->net, "dmi_addrlen != 6\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000404 }
405 mc_list = mc_list->next;
406 }
407
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000408 if (count != ((u32) netdev_mc_count(dev->net)))
Joe Perches60b86752010-02-17 10:30:23 +0000409 netdev_warn(dev->net, "mc_count != dev->mc_count\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000410
Joe Perchesa475f602010-02-17 10:30:24 +0000411 netif_dbg(dev, drv, dev->net, "HASHH=0x%08X, HASHL=0x%08X\n",
Joe Perches60b86752010-02-17 10:30:23 +0000412 hash_hi, hash_lo);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000413 } else {
Joe Perchesa475f602010-02-17 10:30:24 +0000414 netif_dbg(dev, drv, dev->net, "receive own packets only\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000415 pdata->mac_cr &=
416 ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
417 }
418
419 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
420
421 /* Initiate async writes, as we can't wait for completion here */
422 smsc95xx_write_reg_async(dev, HASHH, &hash_hi);
423 smsc95xx_write_reg_async(dev, HASHL, &hash_lo);
424 smsc95xx_write_reg_async(dev, MAC_CR, &pdata->mac_cr);
425}
426
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000427static void smsc95xx_phy_update_flowcontrol(struct usbnet *dev, u8 duplex,
428 u16 lcladv, u16 rmtadv)
429{
430 u32 flow, afc_cfg = 0;
431
432 int ret = smsc95xx_read_reg(dev, AFC_CFG, &afc_cfg);
433 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000434 netdev_warn(dev->net, "error reading AFC_CFG\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000435 return;
436 }
437
438 if (duplex == DUPLEX_FULL) {
Steve Glendinningbc02ff92008-12-16 02:00:48 -0800439 u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000440
441 if (cap & FLOW_CTRL_RX)
442 flow = 0xFFFF0002;
443 else
444 flow = 0;
445
446 if (cap & FLOW_CTRL_TX)
447 afc_cfg |= 0xF;
448 else
449 afc_cfg &= ~0xF;
450
Joe Perchesa475f602010-02-17 10:30:24 +0000451 netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
Joe Perches60b86752010-02-17 10:30:23 +0000452 cap & FLOW_CTRL_RX ? "enabled" : "disabled",
453 cap & FLOW_CTRL_TX ? "enabled" : "disabled");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000454 } else {
Joe Perchesa475f602010-02-17 10:30:24 +0000455 netif_dbg(dev, link, dev->net, "half duplex\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000456 flow = 0;
457 afc_cfg |= 0xF;
458 }
459
460 smsc95xx_write_reg(dev, FLOW, flow);
461 smsc95xx_write_reg(dev, AFC_CFG, afc_cfg);
462}
463
464static int smsc95xx_link_reset(struct usbnet *dev)
465{
466 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
467 struct mii_if_info *mii = &dev->mii;
468 struct ethtool_cmd ecmd;
469 unsigned long flags;
470 u16 lcladv, rmtadv;
471 u32 intdata;
472
473 /* clear interrupt status */
474 smsc95xx_mdio_read(dev->net, mii->phy_id, PHY_INT_SRC);
475 intdata = 0xFFFFFFFF;
476 smsc95xx_write_reg(dev, INT_STS, intdata);
477
478 mii_check_media(mii, 1, 1);
479 mii_ethtool_gset(&dev->mii, &ecmd);
480 lcladv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
481 rmtadv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_LPA);
482
Joe Perchesa475f602010-02-17 10:30:24 +0000483 netif_dbg(dev, link, dev->net, "speed: %d duplex: %d lcladv: %04x rmtadv: %04x\n",
484 ecmd.speed, ecmd.duplex, lcladv, rmtadv);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000485
486 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
487 if (ecmd.duplex != DUPLEX_FULL) {
488 pdata->mac_cr &= ~MAC_CR_FDPX_;
489 pdata->mac_cr |= MAC_CR_RCVOWN_;
490 } else {
491 pdata->mac_cr &= ~MAC_CR_RCVOWN_;
492 pdata->mac_cr |= MAC_CR_FDPX_;
493 }
494 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
495
496 smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
497
498 smsc95xx_phy_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
499
500 return 0;
501}
502
503static void smsc95xx_status(struct usbnet *dev, struct urb *urb)
504{
505 u32 intdata;
506
507 if (urb->actual_length != 4) {
Joe Perches60b86752010-02-17 10:30:23 +0000508 netdev_warn(dev->net, "unexpected urb length %d\n",
509 urb->actual_length);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000510 return;
511 }
512
513 memcpy(&intdata, urb->transfer_buffer, 4);
Steve Glendinning1d74a6b2008-10-09 14:34:47 -0700514 le32_to_cpus(&intdata);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000515
Joe Perchesa475f602010-02-17 10:30:24 +0000516 netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000517
518 if (intdata & INT_ENP_PHY_INT_)
519 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
520 else
Joe Perches60b86752010-02-17 10:30:23 +0000521 netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
522 intdata);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000523}
524
Steve Glendinningf7b29272008-11-20 04:19:21 -0800525/* Enable or disable Tx & Rx checksum offload engines */
526static int smsc95xx_set_csums(struct usbnet *dev)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000527{
Steve Glendinningf7b29272008-11-20 04:19:21 -0800528 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000529 u32 read_buf;
530 int ret = smsc95xx_read_reg(dev, COE_CR, &read_buf);
531 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000532 netdev_warn(dev->net, "Failed to read COE_CR: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000533 return ret;
534 }
535
Steve Glendinningf7b29272008-11-20 04:19:21 -0800536 if (pdata->use_tx_csum)
537 read_buf |= Tx_COE_EN_;
538 else
539 read_buf &= ~Tx_COE_EN_;
540
541 if (pdata->use_rx_csum)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000542 read_buf |= Rx_COE_EN_;
543 else
544 read_buf &= ~Rx_COE_EN_;
545
546 ret = smsc95xx_write_reg(dev, COE_CR, read_buf);
547 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000548 netdev_warn(dev->net, "Failed to write COE_CR: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000549 return ret;
550 }
551
Joe Perchesa475f602010-02-17 10:30:24 +0000552 netif_dbg(dev, hw, dev->net, "COE_CR = 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000553 return 0;
554}
555
556static int smsc95xx_ethtool_get_eeprom_len(struct net_device *net)
557{
558 return MAX_EEPROM_SIZE;
559}
560
561static int smsc95xx_ethtool_get_eeprom(struct net_device *netdev,
562 struct ethtool_eeprom *ee, u8 *data)
563{
564 struct usbnet *dev = netdev_priv(netdev);
565
566 ee->magic = LAN95XX_EEPROM_MAGIC;
567
568 return smsc95xx_read_eeprom(dev, ee->offset, ee->len, data);
569}
570
571static int smsc95xx_ethtool_set_eeprom(struct net_device *netdev,
572 struct ethtool_eeprom *ee, u8 *data)
573{
574 struct usbnet *dev = netdev_priv(netdev);
575
576 if (ee->magic != LAN95XX_EEPROM_MAGIC) {
Joe Perches60b86752010-02-17 10:30:23 +0000577 netdev_warn(dev->net, "EEPROM: magic value mismatch, magic = 0x%x\n",
578 ee->magic);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000579 return -EINVAL;
580 }
581
582 return smsc95xx_write_eeprom(dev, ee->offset, ee->len, data);
583}
584
585static u32 smsc95xx_ethtool_get_rx_csum(struct net_device *netdev)
586{
587 struct usbnet *dev = netdev_priv(netdev);
588 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
589
590 return pdata->use_rx_csum;
591}
592
593static int smsc95xx_ethtool_set_rx_csum(struct net_device *netdev, u32 val)
594{
595 struct usbnet *dev = netdev_priv(netdev);
596 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
597
598 pdata->use_rx_csum = !!val;
599
Steve Glendinningf7b29272008-11-20 04:19:21 -0800600 return smsc95xx_set_csums(dev);
601}
602
603static u32 smsc95xx_ethtool_get_tx_csum(struct net_device *netdev)
604{
605 struct usbnet *dev = netdev_priv(netdev);
606 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
607
608 return pdata->use_tx_csum;
609}
610
611static int smsc95xx_ethtool_set_tx_csum(struct net_device *netdev, u32 val)
612{
613 struct usbnet *dev = netdev_priv(netdev);
614 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
615
616 pdata->use_tx_csum = !!val;
617
618 ethtool_op_set_tx_hw_csum(netdev, pdata->use_tx_csum);
619 return smsc95xx_set_csums(dev);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000620}
621
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700622static const struct ethtool_ops smsc95xx_ethtool_ops = {
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000623 .get_link = usbnet_get_link,
624 .nway_reset = usbnet_nway_reset,
625 .get_drvinfo = usbnet_get_drvinfo,
626 .get_msglevel = usbnet_get_msglevel,
627 .set_msglevel = usbnet_set_msglevel,
628 .get_settings = usbnet_get_settings,
629 .set_settings = usbnet_set_settings,
630 .get_eeprom_len = smsc95xx_ethtool_get_eeprom_len,
631 .get_eeprom = smsc95xx_ethtool_get_eeprom,
632 .set_eeprom = smsc95xx_ethtool_set_eeprom,
Steve Glendinningf7b29272008-11-20 04:19:21 -0800633 .get_tx_csum = smsc95xx_ethtool_get_tx_csum,
634 .set_tx_csum = smsc95xx_ethtool_set_tx_csum,
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000635 .get_rx_csum = smsc95xx_ethtool_get_rx_csum,
636 .set_rx_csum = smsc95xx_ethtool_set_rx_csum,
637};
638
639static int smsc95xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
640{
641 struct usbnet *dev = netdev_priv(netdev);
642
643 if (!netif_running(netdev))
644 return -EINVAL;
645
646 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
647}
648
649static void smsc95xx_init_mac_address(struct usbnet *dev)
650{
651 /* try reading mac address from EEPROM */
652 if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
653 dev->net->dev_addr) == 0) {
654 if (is_valid_ether_addr(dev->net->dev_addr)) {
655 /* eeprom values are valid so use them */
Joe Perchesa475f602010-02-17 10:30:24 +0000656 netif_dbg(dev, ifup, dev->net, "MAC address read from EEPROM\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000657 return;
658 }
659 }
660
661 /* no eeprom, or eeprom values are invalid. generate random MAC */
662 random_ether_addr(dev->net->dev_addr);
Joe Perchesa475f602010-02-17 10:30:24 +0000663 netif_dbg(dev, ifup, dev->net, "MAC address set to random_ether_addr\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000664}
665
666static int smsc95xx_set_mac_address(struct usbnet *dev)
667{
668 u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
669 dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
670 u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
671 int ret;
672
673 ret = smsc95xx_write_reg(dev, ADDRL, addr_lo);
674 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000675 netdev_warn(dev->net, "Failed to write ADDRL: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000676 return ret;
677 }
678
679 ret = smsc95xx_write_reg(dev, ADDRH, addr_hi);
680 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000681 netdev_warn(dev->net, "Failed to write ADDRH: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000682 return ret;
683 }
684
685 return 0;
686}
687
688/* starts the TX path */
689static void smsc95xx_start_tx_path(struct usbnet *dev)
690{
691 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
692 unsigned long flags;
693 u32 reg_val;
694
695 /* Enable Tx at MAC */
696 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
697 pdata->mac_cr |= MAC_CR_TXEN_;
698 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
699
700 smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
701
702 /* Enable Tx at SCSRs */
703 reg_val = TX_CFG_ON_;
704 smsc95xx_write_reg(dev, TX_CFG, reg_val);
705}
706
707/* Starts the Receive path */
708static void smsc95xx_start_rx_path(struct usbnet *dev)
709{
710 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
711 unsigned long flags;
712
713 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
714 pdata->mac_cr |= MAC_CR_RXEN_;
715 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
716
717 smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
718}
719
720static int smsc95xx_phy_initialize(struct usbnet *dev)
721{
722 /* Initialize MII structure */
723 dev->mii.dev = dev->net;
724 dev->mii.mdio_read = smsc95xx_mdio_read;
725 dev->mii.mdio_write = smsc95xx_mdio_write;
726 dev->mii.phy_id_mask = 0x1f;
727 dev->mii.reg_num_mask = 0x1f;
728 dev->mii.phy_id = SMSC95XX_INTERNAL_PHY_ID;
729
730 smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
731 smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
732 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
733 ADVERTISE_PAUSE_ASYM);
734
735 /* read to clear */
736 smsc95xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
737
738 smsc95xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
739 PHY_INT_MASK_DEFAULT_);
740 mii_nway_restart(&dev->mii);
741
Joe Perchesa475f602010-02-17 10:30:24 +0000742 netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000743 return 0;
744}
745
746static int smsc95xx_reset(struct usbnet *dev)
747{
748 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
Steve Glendinningf7b29272008-11-20 04:19:21 -0800749 struct net_device *netdev = dev->net;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000750 u32 read_buf, write_buf, burst_cap;
751 int ret = 0, timeout;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000752
Joe Perchesa475f602010-02-17 10:30:24 +0000753 netif_dbg(dev, ifup, dev->net, "entering smsc95xx_reset\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000754
755 write_buf = HW_CFG_LRST_;
756 ret = smsc95xx_write_reg(dev, HW_CFG, write_buf);
757 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000758 netdev_warn(dev->net, "Failed to write HW_CFG_LRST_ bit in HW_CFG register, ret = %d\n",
759 ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000760 return ret;
761 }
762
763 timeout = 0;
764 do {
765 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
766 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000767 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000768 return ret;
769 }
770 msleep(10);
771 timeout++;
772 } while ((read_buf & HW_CFG_LRST_) && (timeout < 100));
773
774 if (timeout >= 100) {
Joe Perches60b86752010-02-17 10:30:23 +0000775 netdev_warn(dev->net, "timeout waiting for completion of Lite Reset\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000776 return ret;
777 }
778
779 write_buf = PM_CTL_PHY_RST_;
780 ret = smsc95xx_write_reg(dev, PM_CTRL, write_buf);
781 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000782 netdev_warn(dev->net, "Failed to write PM_CTRL: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000783 return ret;
784 }
785
786 timeout = 0;
787 do {
788 ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf);
789 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000790 netdev_warn(dev->net, "Failed to read PM_CTRL: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000791 return ret;
792 }
793 msleep(10);
794 timeout++;
795 } while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));
796
797 if (timeout >= 100) {
Joe Perches60b86752010-02-17 10:30:23 +0000798 netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000799 return ret;
800 }
801
802 smsc95xx_init_mac_address(dev);
803
804 ret = smsc95xx_set_mac_address(dev);
805 if (ret < 0)
806 return ret;
807
Joe Perchesa475f602010-02-17 10:30:24 +0000808 netif_dbg(dev, ifup, dev->net,
809 "MAC Address: %pM\n", dev->net->dev_addr);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000810
811 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
812 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000813 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000814 return ret;
815 }
816
Joe Perchesa475f602010-02-17 10:30:24 +0000817 netif_dbg(dev, ifup, dev->net,
818 "Read Value from HW_CFG : 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000819
820 read_buf |= HW_CFG_BIR_;
821
822 ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
823 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000824 netdev_warn(dev->net, "Failed to write HW_CFG_BIR_ bit in HW_CFG register, ret = %d\n",
825 ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000826 return ret;
827 }
828
829 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
830 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000831 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000832 return ret;
833 }
Joe Perchesa475f602010-02-17 10:30:24 +0000834 netif_dbg(dev, ifup, dev->net,
835 "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
836 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000837
838 if (!turbo_mode) {
839 burst_cap = 0;
840 dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
841 } else if (dev->udev->speed == USB_SPEED_HIGH) {
842 burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
843 dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
844 } else {
845 burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
846 dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
847 }
848
Joe Perchesa475f602010-02-17 10:30:24 +0000849 netif_dbg(dev, ifup, dev->net,
850 "rx_urb_size=%ld\n", (ulong)dev->rx_urb_size);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000851
852 ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
853 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000854 netdev_warn(dev->net, "Failed to write BURST_CAP: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000855 return ret;
856 }
857
858 ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf);
859 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000860 netdev_warn(dev->net, "Failed to read BURST_CAP: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000861 return ret;
862 }
Joe Perchesa475f602010-02-17 10:30:24 +0000863 netif_dbg(dev, ifup, dev->net,
864 "Read Value from BURST_CAP after writing: 0x%08x\n",
865 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000866
867 read_buf = DEFAULT_BULK_IN_DELAY;
868 ret = smsc95xx_write_reg(dev, BULK_IN_DLY, read_buf);
869 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000870 netdev_warn(dev->net, "ret = %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000871 return ret;
872 }
873
874 ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf);
875 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000876 netdev_warn(dev->net, "Failed to read BULK_IN_DLY: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000877 return ret;
878 }
Joe Perchesa475f602010-02-17 10:30:24 +0000879 netif_dbg(dev, ifup, dev->net,
880 "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
881 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000882
883 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
884 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000885 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000886 return ret;
887 }
Joe Perchesa475f602010-02-17 10:30:24 +0000888 netif_dbg(dev, ifup, dev->net,
889 "Read Value from HW_CFG: 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000890
891 if (turbo_mode)
892 read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);
893
894 read_buf &= ~HW_CFG_RXDOFF_;
895
896 /* set Rx data offset=2, Make IP header aligns on word boundary. */
897 read_buf |= NET_IP_ALIGN << 9;
898
899 ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
900 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000901 netdev_warn(dev->net, "Failed to write HW_CFG register, ret=%d\n",
902 ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000903 return ret;
904 }
905
906 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
907 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000908 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000909 return ret;
910 }
Joe Perchesa475f602010-02-17 10:30:24 +0000911 netif_dbg(dev, ifup, dev->net,
912 "Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000913
914 write_buf = 0xFFFFFFFF;
915 ret = smsc95xx_write_reg(dev, INT_STS, write_buf);
916 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000917 netdev_warn(dev->net, "Failed to write INT_STS register, ret=%d\n",
918 ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000919 return ret;
920 }
921
922 ret = smsc95xx_read_reg(dev, ID_REV, &read_buf);
923 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000924 netdev_warn(dev->net, "Failed to read ID_REV: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000925 return ret;
926 }
Joe Perchesa475f602010-02-17 10:30:24 +0000927 netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000928
Steve Glendinningf2935012009-05-01 05:46:51 +0000929 /* Configure GPIO pins as LED outputs */
930 write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED |
931 LED_GPIO_CFG_FDX_LED;
932 ret = smsc95xx_write_reg(dev, LED_GPIO_CFG, write_buf);
933 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000934 netdev_warn(dev->net, "Failed to write LED_GPIO_CFG register, ret=%d\n",
935 ret);
Steve Glendinningf2935012009-05-01 05:46:51 +0000936 return ret;
937 }
938
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000939 /* Init Tx */
940 write_buf = 0;
941 ret = smsc95xx_write_reg(dev, FLOW, write_buf);
942 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000943 netdev_warn(dev->net, "Failed to write FLOW: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000944 return ret;
945 }
946
947 read_buf = AFC_CFG_DEFAULT;
948 ret = smsc95xx_write_reg(dev, AFC_CFG, read_buf);
949 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000950 netdev_warn(dev->net, "Failed to write AFC_CFG: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000951 return ret;
952 }
953
954 /* Don't need mac_cr_lock during initialisation */
955 ret = smsc95xx_read_reg(dev, MAC_CR, &pdata->mac_cr);
956 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000957 netdev_warn(dev->net, "Failed to read MAC_CR: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000958 return ret;
959 }
960
961 /* Init Rx */
962 /* Set Vlan */
963 write_buf = (u32)ETH_P_8021Q;
964 ret = smsc95xx_write_reg(dev, VLAN1, write_buf);
965 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000966 netdev_warn(dev->net, "Failed to write VAN1: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000967 return ret;
968 }
969
Steve Glendinningf7b29272008-11-20 04:19:21 -0800970 /* Enable or disable checksum offload engines */
971 ethtool_op_set_tx_hw_csum(netdev, pdata->use_tx_csum);
972 ret = smsc95xx_set_csums(dev);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000973 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000974 netdev_warn(dev->net, "Failed to set csum offload: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000975 return ret;
976 }
977
978 smsc95xx_set_multicast(dev->net);
979
980 if (smsc95xx_phy_initialize(dev) < 0)
981 return -EIO;
982
983 ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
984 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000985 netdev_warn(dev->net, "Failed to read INT_EP_CTL: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000986 return ret;
987 }
988
989 /* enable PHY interrupts */
990 read_buf |= INT_EP_CTL_PHY_INT_;
991
992 ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf);
993 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000994 netdev_warn(dev->net, "Failed to write INT_EP_CTL: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000995 return ret;
996 }
997
998 smsc95xx_start_tx_path(dev);
999 smsc95xx_start_rx_path(dev);
1000
Joe Perchesa475f602010-02-17 10:30:24 +00001001 netif_dbg(dev, ifup, dev->net, "smsc95xx_reset, return 0\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001002 return 0;
1003}
1004
Stephen Hemminger63e77b32009-03-20 19:35:58 +00001005static const struct net_device_ops smsc95xx_netdev_ops = {
1006 .ndo_open = usbnet_open,
1007 .ndo_stop = usbnet_stop,
1008 .ndo_start_xmit = usbnet_start_xmit,
1009 .ndo_tx_timeout = usbnet_tx_timeout,
1010 .ndo_change_mtu = usbnet_change_mtu,
1011 .ndo_set_mac_address = eth_mac_addr,
1012 .ndo_validate_addr = eth_validate_addr,
1013 .ndo_do_ioctl = smsc95xx_ioctl,
1014 .ndo_set_multicast_list = smsc95xx_set_multicast,
1015};
1016
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001017static int smsc95xx_bind(struct usbnet *dev, struct usb_interface *intf)
1018{
1019 struct smsc95xx_priv *pdata = NULL;
1020 int ret;
1021
1022 printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
1023
1024 ret = usbnet_get_endpoints(dev, intf);
1025 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +00001026 netdev_warn(dev->net, "usbnet_get_endpoints failed: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001027 return ret;
1028 }
1029
1030 dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc95xx_priv),
1031 GFP_KERNEL);
1032
1033 pdata = (struct smsc95xx_priv *)(dev->data[0]);
1034 if (!pdata) {
Joe Perches60b86752010-02-17 10:30:23 +00001035 netdev_warn(dev->net, "Unable to allocate struct smsc95xx_priv\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001036 return -ENOMEM;
1037 }
1038
1039 spin_lock_init(&pdata->mac_cr_lock);
1040
Steve Glendinningf7b29272008-11-20 04:19:21 -08001041 pdata->use_tx_csum = DEFAULT_TX_CSUM_ENABLE;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001042 pdata->use_rx_csum = DEFAULT_RX_CSUM_ENABLE;
1043
1044 /* Init all registers */
1045 ret = smsc95xx_reset(dev);
1046
Stephen Hemminger63e77b32009-03-20 19:35:58 +00001047 dev->net->netdev_ops = &smsc95xx_netdev_ops;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001048 dev->net->ethtool_ops = &smsc95xx_ethtool_ops;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001049 dev->net->flags |= IFF_MULTICAST;
1050 dev->net->hard_header_len += SMSC95XX_TX_OVERHEAD;
1051 return 0;
1052}
1053
1054static void smsc95xx_unbind(struct usbnet *dev, struct usb_interface *intf)
1055{
1056 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1057 if (pdata) {
Joe Perchesa475f602010-02-17 10:30:24 +00001058 netif_dbg(dev, ifdown, dev->net, "free pdata\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001059 kfree(pdata);
1060 pdata = NULL;
1061 dev->data[0] = 0;
1062 }
1063}
1064
1065static void smsc95xx_rx_csum_offload(struct sk_buff *skb)
1066{
1067 skb->csum = *(u16 *)(skb_tail_pointer(skb) - 2);
1068 skb->ip_summed = CHECKSUM_COMPLETE;
1069 skb_trim(skb, skb->len - 2);
1070}
1071
1072static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
1073{
1074 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1075
1076 while (skb->len > 0) {
1077 u32 header, align_count;
1078 struct sk_buff *ax_skb;
1079 unsigned char *packet;
1080 u16 size;
1081
1082 memcpy(&header, skb->data, sizeof(header));
1083 le32_to_cpus(&header);
1084 skb_pull(skb, 4 + NET_IP_ALIGN);
1085 packet = skb->data;
1086
1087 /* get the packet length */
1088 size = (u16)((header & RX_STS_FL_) >> 16);
1089 align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4;
1090
1091 if (unlikely(header & RX_STS_ES_)) {
Joe Perchesa475f602010-02-17 10:30:24 +00001092 netif_dbg(dev, rx_err, dev->net,
1093 "Error header=0x%08x\n", header);
Herbert Xu80667ac2009-06-29 16:53:00 +00001094 dev->net->stats.rx_errors++;
1095 dev->net->stats.rx_dropped++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001096
1097 if (header & RX_STS_CRC_) {
Herbert Xu80667ac2009-06-29 16:53:00 +00001098 dev->net->stats.rx_crc_errors++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001099 } else {
1100 if (header & (RX_STS_TL_ | RX_STS_RF_))
Herbert Xu80667ac2009-06-29 16:53:00 +00001101 dev->net->stats.rx_frame_errors++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001102
1103 if ((header & RX_STS_LE_) &&
1104 (!(header & RX_STS_FT_)))
Herbert Xu80667ac2009-06-29 16:53:00 +00001105 dev->net->stats.rx_length_errors++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001106 }
1107 } else {
1108 /* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
1109 if (unlikely(size > (ETH_FRAME_LEN + 12))) {
Joe Perchesa475f602010-02-17 10:30:24 +00001110 netif_dbg(dev, rx_err, dev->net,
1111 "size err header=0x%08x\n", header);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001112 return 0;
1113 }
1114
1115 /* last frame in this batch */
1116 if (skb->len == size) {
1117 if (pdata->use_rx_csum)
1118 smsc95xx_rx_csum_offload(skb);
Peter Korsgaarddf18acc2009-05-13 10:10:41 +00001119 skb_trim(skb, skb->len - 4); /* remove fcs */
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001120 skb->truesize = size + sizeof(struct sk_buff);
1121
1122 return 1;
1123 }
1124
1125 ax_skb = skb_clone(skb, GFP_ATOMIC);
1126 if (unlikely(!ax_skb)) {
Joe Perches60b86752010-02-17 10:30:23 +00001127 netdev_warn(dev->net, "Error allocating skb\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001128 return 0;
1129 }
1130
1131 ax_skb->len = size;
1132 ax_skb->data = packet;
1133 skb_set_tail_pointer(ax_skb, size);
1134
1135 if (pdata->use_rx_csum)
1136 smsc95xx_rx_csum_offload(ax_skb);
Peter Korsgaarddf18acc2009-05-13 10:10:41 +00001137 skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001138 ax_skb->truesize = size + sizeof(struct sk_buff);
1139
1140 usbnet_skb_return(dev, ax_skb);
1141 }
1142
1143 skb_pull(skb, size);
1144
1145 /* padding bytes before the next frame starts */
1146 if (skb->len)
1147 skb_pull(skb, align_count);
1148 }
1149
1150 if (unlikely(skb->len < 0)) {
Joe Perches60b86752010-02-17 10:30:23 +00001151 netdev_warn(dev->net, "invalid rx length<0 %d\n", skb->len);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001152 return 0;
1153 }
1154
1155 return 1;
1156}
1157
Steve Glendinningf7b29272008-11-20 04:19:21 -08001158static u32 smsc95xx_calc_csum_preamble(struct sk_buff *skb)
1159{
1160 int len = skb->data - skb->head;
1161 u16 high_16 = (u16)(skb->csum_offset + skb->csum_start - len);
1162 u16 low_16 = (u16)(skb->csum_start - len);
1163 return (high_16 << 16) | low_16;
1164}
1165
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001166static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,
1167 struct sk_buff *skb, gfp_t flags)
1168{
Steve Glendinningf7b29272008-11-20 04:19:21 -08001169 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1170 bool csum = pdata->use_tx_csum && (skb->ip_summed == CHECKSUM_PARTIAL);
1171 int overhead = csum ? SMSC95XX_TX_OVERHEAD_CSUM : SMSC95XX_TX_OVERHEAD;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001172 u32 tx_cmd_a, tx_cmd_b;
1173
Steve Glendinningf7b29272008-11-20 04:19:21 -08001174 /* We do not advertise SG, so skbs should be already linearized */
1175 BUG_ON(skb_shinfo(skb)->nr_frags);
1176
1177 if (skb_headroom(skb) < overhead) {
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001178 struct sk_buff *skb2 = skb_copy_expand(skb,
Steve Glendinningf7b29272008-11-20 04:19:21 -08001179 overhead, 0, flags);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001180 dev_kfree_skb_any(skb);
1181 skb = skb2;
1182 if (!skb)
1183 return NULL;
1184 }
1185
Steve Glendinningf7b29272008-11-20 04:19:21 -08001186 if (csum) {
1187 u32 csum_preamble = smsc95xx_calc_csum_preamble(skb);
1188 skb_push(skb, 4);
1189 memcpy(skb->data, &csum_preamble, 4);
1190 }
1191
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001192 skb_push(skb, 4);
1193 tx_cmd_b = (u32)(skb->len - 4);
Steve Glendinningf7b29272008-11-20 04:19:21 -08001194 if (csum)
1195 tx_cmd_b |= TX_CMD_B_CSUM_ENABLE;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001196 cpu_to_le32s(&tx_cmd_b);
1197 memcpy(skb->data, &tx_cmd_b, 4);
1198
1199 skb_push(skb, 4);
1200 tx_cmd_a = (u32)(skb->len - 8) | TX_CMD_A_FIRST_SEG_ |
1201 TX_CMD_A_LAST_SEG_;
1202 cpu_to_le32s(&tx_cmd_a);
1203 memcpy(skb->data, &tx_cmd_a, 4);
1204
1205 return skb;
1206}
1207
1208static const struct driver_info smsc95xx_info = {
1209 .description = "smsc95xx USB 2.0 Ethernet",
1210 .bind = smsc95xx_bind,
1211 .unbind = smsc95xx_unbind,
1212 .link_reset = smsc95xx_link_reset,
1213 .reset = smsc95xx_reset,
1214 .rx_fixup = smsc95xx_rx_fixup,
1215 .tx_fixup = smsc95xx_tx_fixup,
1216 .status = smsc95xx_status,
Steve Glendinningec475622009-09-22 04:00:27 +00001217 .flags = FLAG_ETHER | FLAG_SEND_ZLP,
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001218};
1219
1220static const struct usb_device_id products[] = {
1221 {
1222 /* SMSC9500 USB Ethernet Device */
1223 USB_DEVICE(0x0424, 0x9500),
1224 .driver_info = (unsigned long) &smsc95xx_info,
1225 },
Steve Glendinning726474b2009-05-01 06:07:22 +00001226 {
Steve Glendinning6f41d122009-09-22 05:13:02 +00001227 /* SMSC9505 USB Ethernet Device */
1228 USB_DEVICE(0x0424, 0x9505),
1229 .driver_info = (unsigned long) &smsc95xx_info,
1230 },
1231 {
1232 /* SMSC9500A USB Ethernet Device */
1233 USB_DEVICE(0x0424, 0x9E00),
1234 .driver_info = (unsigned long) &smsc95xx_info,
1235 },
1236 {
1237 /* SMSC9505A USB Ethernet Device */
1238 USB_DEVICE(0x0424, 0x9E01),
1239 .driver_info = (unsigned long) &smsc95xx_info,
1240 },
1241 {
Steve Glendinning726474b2009-05-01 06:07:22 +00001242 /* SMSC9512/9514 USB Hub & Ethernet Device */
1243 USB_DEVICE(0x0424, 0xec00),
1244 .driver_info = (unsigned long) &smsc95xx_info,
1245 },
Steve Glendinning6f41d122009-09-22 05:13:02 +00001246 {
1247 /* SMSC9500 USB Ethernet Device (SAL10) */
1248 USB_DEVICE(0x0424, 0x9900),
1249 .driver_info = (unsigned long) &smsc95xx_info,
1250 },
1251 {
1252 /* SMSC9505 USB Ethernet Device (SAL10) */
1253 USB_DEVICE(0x0424, 0x9901),
1254 .driver_info = (unsigned long) &smsc95xx_info,
1255 },
1256 {
1257 /* SMSC9500A USB Ethernet Device (SAL10) */
1258 USB_DEVICE(0x0424, 0x9902),
1259 .driver_info = (unsigned long) &smsc95xx_info,
1260 },
1261 {
1262 /* SMSC9505A USB Ethernet Device (SAL10) */
1263 USB_DEVICE(0x0424, 0x9903),
1264 .driver_info = (unsigned long) &smsc95xx_info,
1265 },
1266 {
1267 /* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */
1268 USB_DEVICE(0x0424, 0x9904),
1269 .driver_info = (unsigned long) &smsc95xx_info,
1270 },
1271 {
1272 /* SMSC9500A USB Ethernet Device (HAL) */
1273 USB_DEVICE(0x0424, 0x9905),
1274 .driver_info = (unsigned long) &smsc95xx_info,
1275 },
1276 {
1277 /* SMSC9505A USB Ethernet Device (HAL) */
1278 USB_DEVICE(0x0424, 0x9906),
1279 .driver_info = (unsigned long) &smsc95xx_info,
1280 },
1281 {
1282 /* SMSC9500 USB Ethernet Device (Alternate ID) */
1283 USB_DEVICE(0x0424, 0x9907),
1284 .driver_info = (unsigned long) &smsc95xx_info,
1285 },
1286 {
1287 /* SMSC9500A USB Ethernet Device (Alternate ID) */
1288 USB_DEVICE(0x0424, 0x9908),
1289 .driver_info = (unsigned long) &smsc95xx_info,
1290 },
1291 {
1292 /* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */
1293 USB_DEVICE(0x0424, 0x9909),
1294 .driver_info = (unsigned long) &smsc95xx_info,
1295 },
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001296 { }, /* END */
1297};
1298MODULE_DEVICE_TABLE(usb, products);
1299
1300static struct usb_driver smsc95xx_driver = {
1301 .name = "smsc95xx",
1302 .id_table = products,
1303 .probe = usbnet_probe,
1304 .suspend = usbnet_suspend,
1305 .resume = usbnet_resume,
1306 .disconnect = usbnet_disconnect,
1307};
1308
1309static int __init smsc95xx_init(void)
1310{
1311 return usb_register(&smsc95xx_driver);
1312}
1313module_init(smsc95xx_init);
1314
1315static void __exit smsc95xx_exit(void)
1316{
1317 usb_deregister(&smsc95xx_driver);
1318}
1319module_exit(smsc95xx_exit);
1320
1321MODULE_AUTHOR("Nancy Lin");
1322MODULE_AUTHOR("Steve Glendinning <steve.glendinning@smsc.com>");
1323MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices");
1324MODULE_LICENSE("GPL");