blob: 727874d9deb6d886fd9d357484c57d3ee899d079 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Steve Glendinning2f7ca802008-10-02 05:27:57 +000032#include "smsc95xx.h"
33
34#define SMSC_CHIPNAME "smsc95xx"
Steve Glendinningf7b29272008-11-20 04:19:21 -080035#define SMSC_DRIVER_VERSION "1.0.4"
Steve Glendinning2f7ca802008-10-02 05:27:57 +000036#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 (2048)
42#define LAN95XX_EEPROM_MAGIC (0x9500)
43#define EEPROM_MAC_OFFSET (0x01)
Steve Glendinningf7b29272008-11-20 04:19:21 -080044#define DEFAULT_TX_CSUM_ENABLE (true)
Steve Glendinning2f7ca802008-10-02 05:27:57 +000045#define DEFAULT_RX_CSUM_ENABLE (true)
46#define SMSC95XX_INTERNAL_PHY_ID (1)
47#define SMSC95XX_TX_OVERHEAD (8)
Steve Glendinningf7b29272008-11-20 04:19:21 -080048#define SMSC95XX_TX_OVERHEAD_CSUM (12)
Steve Glendinning2f7ca802008-10-02 05:27:57 +000049
50struct smsc95xx_priv {
51 u32 mac_cr;
Marc Zyngier3c0f3c62011-03-18 03:53:58 +000052 u32 hash_hi;
53 u32 hash_lo;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000054 spinlock_t mac_cr_lock;
Steve Glendinningf7b29272008-11-20 04:19:21 -080055 bool use_tx_csum;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000056 bool use_rx_csum;
57};
58
59struct usb_context {
60 struct usb_ctrlrequest req;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000061 struct usbnet *dev;
62};
63
Hannes Eder0227abc2009-02-14 11:47:47 +000064static int turbo_mode = true;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000065module_param(turbo_mode, bool, 0644);
66MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
67
68static int smsc95xx_read_reg(struct usbnet *dev, u32 index, u32 *data)
69{
70 u32 *buf = kmalloc(4, GFP_KERNEL);
71 int ret;
72
73 BUG_ON(!dev);
74
75 if (!buf)
76 return -ENOMEM;
77
78 ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
79 USB_VENDOR_REQUEST_READ_REGISTER,
80 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
81 00, index, buf, 4, USB_CTRL_GET_TIMEOUT);
82
83 if (unlikely(ret < 0))
Joe Perches60b86752010-02-17 10:30:23 +000084 netdev_warn(dev->net, "Failed to read register index 0x%08x\n", index);
Steve Glendinning2f7ca802008-10-02 05:27:57 +000085
86 le32_to_cpus(buf);
87 *data = *buf;
88 kfree(buf);
89
90 return ret;
91}
92
93static int smsc95xx_write_reg(struct usbnet *dev, u32 index, u32 data)
94{
95 u32 *buf = kmalloc(4, GFP_KERNEL);
96 int ret;
97
98 BUG_ON(!dev);
99
100 if (!buf)
101 return -ENOMEM;
102
103 *buf = data;
104 cpu_to_le32s(buf);
105
106 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
107 USB_VENDOR_REQUEST_WRITE_REGISTER,
108 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
109 00, index, buf, 4, USB_CTRL_SET_TIMEOUT);
110
111 if (unlikely(ret < 0))
Joe Perches60b86752010-02-17 10:30:23 +0000112 netdev_warn(dev->net, "Failed to write register index 0x%08x\n", index);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000113
114 kfree(buf);
115
116 return ret;
117}
118
119/* Loop until the read is completed with timeout
120 * called with phy_mutex held */
121static int smsc95xx_phy_wait_not_busy(struct usbnet *dev)
122{
123 unsigned long start_time = jiffies;
124 u32 val;
125
126 do {
127 smsc95xx_read_reg(dev, MII_ADDR, &val);
128 if (!(val & MII_BUSY_))
129 return 0;
130 } while (!time_after(jiffies, start_time + HZ));
131
132 return -EIO;
133}
134
135static int smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
136{
137 struct usbnet *dev = netdev_priv(netdev);
138 u32 val, addr;
139
140 mutex_lock(&dev->phy_mutex);
141
142 /* confirm MII not busy */
143 if (smsc95xx_phy_wait_not_busy(dev)) {
Joe Perches60b86752010-02-17 10:30:23 +0000144 netdev_warn(dev->net, "MII is busy in smsc95xx_mdio_read\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000145 mutex_unlock(&dev->phy_mutex);
146 return -EIO;
147 }
148
149 /* set the address, index & direction (read from PHY) */
150 phy_id &= dev->mii.phy_id_mask;
151 idx &= dev->mii.reg_num_mask;
152 addr = (phy_id << 11) | (idx << 6) | MII_READ_;
153 smsc95xx_write_reg(dev, MII_ADDR, addr);
154
155 if (smsc95xx_phy_wait_not_busy(dev)) {
Joe Perches60b86752010-02-17 10:30:23 +0000156 netdev_warn(dev->net, "Timed out reading MII reg %02X\n", idx);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000157 mutex_unlock(&dev->phy_mutex);
158 return -EIO;
159 }
160
161 smsc95xx_read_reg(dev, MII_DATA, &val);
162
163 mutex_unlock(&dev->phy_mutex);
164
165 return (u16)(val & 0xFFFF);
166}
167
168static void smsc95xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
169 int regval)
170{
171 struct usbnet *dev = netdev_priv(netdev);
172 u32 val, addr;
173
174 mutex_lock(&dev->phy_mutex);
175
176 /* confirm MII not busy */
177 if (smsc95xx_phy_wait_not_busy(dev)) {
Joe Perches60b86752010-02-17 10:30:23 +0000178 netdev_warn(dev->net, "MII is busy in smsc95xx_mdio_write\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000179 mutex_unlock(&dev->phy_mutex);
180 return;
181 }
182
183 val = regval;
184 smsc95xx_write_reg(dev, MII_DATA, val);
185
186 /* set the address, index & direction (write to PHY) */
187 phy_id &= dev->mii.phy_id_mask;
188 idx &= dev->mii.reg_num_mask;
189 addr = (phy_id << 11) | (idx << 6) | MII_WRITE_;
190 smsc95xx_write_reg(dev, MII_ADDR, addr);
191
192 if (smsc95xx_phy_wait_not_busy(dev))
Joe Perches60b86752010-02-17 10:30:23 +0000193 netdev_warn(dev->net, "Timed out writing MII reg %02X\n", idx);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000194
195 mutex_unlock(&dev->phy_mutex);
196}
197
198static int smsc95xx_wait_eeprom(struct usbnet *dev)
199{
200 unsigned long start_time = jiffies;
201 u32 val;
202
203 do {
204 smsc95xx_read_reg(dev, E2P_CMD, &val);
205 if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_))
206 break;
207 udelay(40);
208 } while (!time_after(jiffies, start_time + HZ));
209
210 if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) {
Joe Perches60b86752010-02-17 10:30:23 +0000211 netdev_warn(dev->net, "EEPROM read operation timeout\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000212 return -EIO;
213 }
214
215 return 0;
216}
217
218static int smsc95xx_eeprom_confirm_not_busy(struct usbnet *dev)
219{
220 unsigned long start_time = jiffies;
221 u32 val;
222
223 do {
224 smsc95xx_read_reg(dev, E2P_CMD, &val);
225
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000226 if (!(val & E2P_CMD_BUSY_))
227 return 0;
228
229 udelay(40);
230 } while (!time_after(jiffies, start_time + HZ));
231
Joe Perches60b86752010-02-17 10:30:23 +0000232 netdev_warn(dev->net, "EEPROM is busy\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000233 return -EIO;
234}
235
236static int smsc95xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
237 u8 *data)
238{
239 u32 val;
240 int i, ret;
241
242 BUG_ON(!dev);
243 BUG_ON(!data);
244
245 ret = smsc95xx_eeprom_confirm_not_busy(dev);
246 if (ret)
247 return ret;
248
249 for (i = 0; i < length; i++) {
250 val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_);
251 smsc95xx_write_reg(dev, E2P_CMD, val);
252
253 ret = smsc95xx_wait_eeprom(dev);
254 if (ret < 0)
255 return ret;
256
257 smsc95xx_read_reg(dev, E2P_DATA, &val);
258
259 data[i] = val & 0xFF;
260 offset++;
261 }
262
263 return 0;
264}
265
266static int smsc95xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
267 u8 *data)
268{
269 u32 val;
270 int i, ret;
271
272 BUG_ON(!dev);
273 BUG_ON(!data);
274
275 ret = smsc95xx_eeprom_confirm_not_busy(dev);
276 if (ret)
277 return ret;
278
279 /* Issue write/erase enable command */
280 val = E2P_CMD_BUSY_ | E2P_CMD_EWEN_;
281 smsc95xx_write_reg(dev, E2P_CMD, val);
282
283 ret = smsc95xx_wait_eeprom(dev);
284 if (ret < 0)
285 return ret;
286
287 for (i = 0; i < length; i++) {
288
289 /* Fill data register */
290 val = data[i];
291 smsc95xx_write_reg(dev, E2P_DATA, val);
292
293 /* Send "write" command */
294 val = E2P_CMD_BUSY_ | E2P_CMD_WRITE_ | (offset & E2P_CMD_ADDR_);
295 smsc95xx_write_reg(dev, E2P_CMD, val);
296
297 ret = smsc95xx_wait_eeprom(dev);
298 if (ret < 0)
299 return ret;
300
301 offset++;
302 }
303
304 return 0;
305}
306
Steve Glendinning150a7fc2009-01-25 17:54:46 -0800307static void smsc95xx_async_cmd_callback(struct urb *urb)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000308{
309 struct usb_context *usb_context = urb->context;
310 struct usbnet *dev = usb_context->dev;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800311 int status = urb->status;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000312
Oliver Neukumc94cb312008-12-18 23:00:59 -0800313 if (status < 0)
Joe Perches60b86752010-02-17 10:30:23 +0000314 netdev_warn(dev->net, "async callback failed with %d\n", status);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000315
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000316 kfree(usb_context);
317 usb_free_urb(urb);
318}
319
Steve Glendinning1d74a6b2008-10-09 14:34:47 -0700320static int smsc95xx_write_reg_async(struct usbnet *dev, u16 index, u32 *data)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000321{
322 struct usb_context *usb_context;
323 int status;
324 struct urb *urb;
Steve Glendinning1d74a6b2008-10-09 14:34:47 -0700325 const u16 size = 4;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000326
327 urb = usb_alloc_urb(0, GFP_ATOMIC);
328 if (!urb) {
Joe Perches60b86752010-02-17 10:30:23 +0000329 netdev_warn(dev->net, "Error allocating URB\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000330 return -ENOMEM;
331 }
332
333 usb_context = kmalloc(sizeof(struct usb_context), GFP_ATOMIC);
334 if (usb_context == NULL) {
Joe Perches60b86752010-02-17 10:30:23 +0000335 netdev_warn(dev->net, "Error allocating control msg\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000336 usb_free_urb(urb);
337 return -ENOMEM;
338 }
339
340 usb_context->req.bRequestType =
341 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
342 usb_context->req.bRequest = USB_VENDOR_REQUEST_WRITE_REGISTER;
343 usb_context->req.wValue = 00;
Steve Glendinning1d74a6b2008-10-09 14:34:47 -0700344 usb_context->req.wIndex = cpu_to_le16(index);
345 usb_context->req.wLength = cpu_to_le16(size);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000346
347 usb_fill_control_urb(urb, dev->udev, usb_sndctrlpipe(dev->udev, 0),
348 (void *)&usb_context->req, data, size,
Steve Glendinning150a7fc2009-01-25 17:54:46 -0800349 smsc95xx_async_cmd_callback,
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000350 (void *)usb_context);
351
352 status = usb_submit_urb(urb, GFP_ATOMIC);
353 if (status < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000354 netdev_warn(dev->net, "Error submitting control msg, sts=%d\n",
355 status);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000356 kfree(usb_context);
357 usb_free_urb(urb);
358 }
359
360 return status;
361}
362
363/* returns hash bit number for given MAC address
364 * example:
365 * 01 00 5E 00 00 01 -> returns bit number 31 */
366static unsigned int smsc95xx_hash(char addr[ETH_ALEN])
367{
368 return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
369}
370
371static void smsc95xx_set_multicast(struct net_device *netdev)
372{
373 struct usbnet *dev = netdev_priv(netdev);
374 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000375 unsigned long flags;
376
Marc Zyngier3c0f3c62011-03-18 03:53:58 +0000377 pdata->hash_hi = 0;
378 pdata->hash_lo = 0;
379
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000380 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
381
382 if (dev->net->flags & IFF_PROMISC) {
Joe Perchesa475f602010-02-17 10:30:24 +0000383 netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000384 pdata->mac_cr |= MAC_CR_PRMS_;
385 pdata->mac_cr &= ~(MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
386 } else if (dev->net->flags & IFF_ALLMULTI) {
Joe Perchesa475f602010-02-17 10:30:24 +0000387 netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000388 pdata->mac_cr |= MAC_CR_MCPAS_;
389 pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_HPFILT_);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000390 } else if (!netdev_mc_empty(dev->net)) {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000391 struct netdev_hw_addr *ha;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000392
393 pdata->mac_cr |= MAC_CR_HPFILT_;
394 pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);
395
Jiri Pirko22bedad32010-04-01 21:22:57 +0000396 netdev_for_each_mc_addr(ha, netdev) {
397 u32 bitnum = smsc95xx_hash(ha->addr);
Jiri Pirkoa92635d2010-02-18 04:02:26 +0000398 u32 mask = 0x01 << (bitnum & 0x1F);
399 if (bitnum & 0x20)
Marc Zyngier3c0f3c62011-03-18 03:53:58 +0000400 pdata->hash_hi |= mask;
Jiri Pirkoa92635d2010-02-18 04:02:26 +0000401 else
Marc Zyngier3c0f3c62011-03-18 03:53:58 +0000402 pdata->hash_lo |= mask;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000403 }
404
Joe Perchesa475f602010-02-17 10:30:24 +0000405 netif_dbg(dev, drv, dev->net, "HASHH=0x%08X, HASHL=0x%08X\n",
Marc Zyngier3c0f3c62011-03-18 03:53:58 +0000406 pdata->hash_hi, pdata->hash_lo);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000407 } else {
Joe Perchesa475f602010-02-17 10:30:24 +0000408 netif_dbg(dev, drv, dev->net, "receive own packets only\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000409 pdata->mac_cr &=
410 ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
411 }
412
413 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
414
415 /* Initiate async writes, as we can't wait for completion here */
Marc Zyngier3c0f3c62011-03-18 03:53:58 +0000416 smsc95xx_write_reg_async(dev, HASHH, &pdata->hash_hi);
417 smsc95xx_write_reg_async(dev, HASHL, &pdata->hash_lo);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000418 smsc95xx_write_reg_async(dev, MAC_CR, &pdata->mac_cr);
419}
420
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000421static void smsc95xx_phy_update_flowcontrol(struct usbnet *dev, u8 duplex,
422 u16 lcladv, u16 rmtadv)
423{
424 u32 flow, afc_cfg = 0;
425
426 int ret = smsc95xx_read_reg(dev, AFC_CFG, &afc_cfg);
427 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000428 netdev_warn(dev->net, "error reading AFC_CFG\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000429 return;
430 }
431
432 if (duplex == DUPLEX_FULL) {
Steve Glendinningbc02ff92008-12-16 02:00:48 -0800433 u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000434
435 if (cap & FLOW_CTRL_RX)
436 flow = 0xFFFF0002;
437 else
438 flow = 0;
439
440 if (cap & FLOW_CTRL_TX)
441 afc_cfg |= 0xF;
442 else
443 afc_cfg &= ~0xF;
444
Joe Perchesa475f602010-02-17 10:30:24 +0000445 netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
Joe Perches60b86752010-02-17 10:30:23 +0000446 cap & FLOW_CTRL_RX ? "enabled" : "disabled",
447 cap & FLOW_CTRL_TX ? "enabled" : "disabled");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000448 } else {
Joe Perchesa475f602010-02-17 10:30:24 +0000449 netif_dbg(dev, link, dev->net, "half duplex\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000450 flow = 0;
451 afc_cfg |= 0xF;
452 }
453
454 smsc95xx_write_reg(dev, FLOW, flow);
455 smsc95xx_write_reg(dev, AFC_CFG, afc_cfg);
456}
457
458static int smsc95xx_link_reset(struct usbnet *dev)
459{
460 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
461 struct mii_if_info *mii = &dev->mii;
462 struct ethtool_cmd ecmd;
463 unsigned long flags;
464 u16 lcladv, rmtadv;
465 u32 intdata;
466
467 /* clear interrupt status */
468 smsc95xx_mdio_read(dev->net, mii->phy_id, PHY_INT_SRC);
469 intdata = 0xFFFFFFFF;
470 smsc95xx_write_reg(dev, INT_STS, intdata);
471
472 mii_check_media(mii, 1, 1);
473 mii_ethtool_gset(&dev->mii, &ecmd);
474 lcladv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
475 rmtadv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_LPA);
476
Joe Perchesa475f602010-02-17 10:30:24 +0000477 netif_dbg(dev, link, dev->net, "speed: %d duplex: %d lcladv: %04x rmtadv: %04x\n",
478 ecmd.speed, ecmd.duplex, lcladv, rmtadv);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000479
480 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
481 if (ecmd.duplex != DUPLEX_FULL) {
482 pdata->mac_cr &= ~MAC_CR_FDPX_;
483 pdata->mac_cr |= MAC_CR_RCVOWN_;
484 } else {
485 pdata->mac_cr &= ~MAC_CR_RCVOWN_;
486 pdata->mac_cr |= MAC_CR_FDPX_;
487 }
488 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
489
490 smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
491
492 smsc95xx_phy_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
493
494 return 0;
495}
496
497static void smsc95xx_status(struct usbnet *dev, struct urb *urb)
498{
499 u32 intdata;
500
501 if (urb->actual_length != 4) {
Joe Perches60b86752010-02-17 10:30:23 +0000502 netdev_warn(dev->net, "unexpected urb length %d\n",
503 urb->actual_length);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000504 return;
505 }
506
507 memcpy(&intdata, urb->transfer_buffer, 4);
Steve Glendinning1d74a6b2008-10-09 14:34:47 -0700508 le32_to_cpus(&intdata);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000509
Joe Perchesa475f602010-02-17 10:30:24 +0000510 netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000511
512 if (intdata & INT_ENP_PHY_INT_)
513 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
514 else
Joe Perches60b86752010-02-17 10:30:23 +0000515 netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
516 intdata);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000517}
518
Steve Glendinningf7b29272008-11-20 04:19:21 -0800519/* Enable or disable Tx & Rx checksum offload engines */
520static int smsc95xx_set_csums(struct usbnet *dev)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000521{
Steve Glendinningf7b29272008-11-20 04:19:21 -0800522 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000523 u32 read_buf;
524 int ret = smsc95xx_read_reg(dev, COE_CR, &read_buf);
525 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000526 netdev_warn(dev->net, "Failed to read COE_CR: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000527 return ret;
528 }
529
Steve Glendinningf7b29272008-11-20 04:19:21 -0800530 if (pdata->use_tx_csum)
531 read_buf |= Tx_COE_EN_;
532 else
533 read_buf &= ~Tx_COE_EN_;
534
535 if (pdata->use_rx_csum)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000536 read_buf |= Rx_COE_EN_;
537 else
538 read_buf &= ~Rx_COE_EN_;
539
540 ret = smsc95xx_write_reg(dev, COE_CR, read_buf);
541 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000542 netdev_warn(dev->net, "Failed to write COE_CR: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000543 return ret;
544 }
545
Joe Perchesa475f602010-02-17 10:30:24 +0000546 netif_dbg(dev, hw, dev->net, "COE_CR = 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000547 return 0;
548}
549
550static int smsc95xx_ethtool_get_eeprom_len(struct net_device *net)
551{
552 return MAX_EEPROM_SIZE;
553}
554
555static int smsc95xx_ethtool_get_eeprom(struct net_device *netdev,
556 struct ethtool_eeprom *ee, u8 *data)
557{
558 struct usbnet *dev = netdev_priv(netdev);
559
560 ee->magic = LAN95XX_EEPROM_MAGIC;
561
562 return smsc95xx_read_eeprom(dev, ee->offset, ee->len, data);
563}
564
565static int smsc95xx_ethtool_set_eeprom(struct net_device *netdev,
566 struct ethtool_eeprom *ee, u8 *data)
567{
568 struct usbnet *dev = netdev_priv(netdev);
569
570 if (ee->magic != LAN95XX_EEPROM_MAGIC) {
Joe Perches60b86752010-02-17 10:30:23 +0000571 netdev_warn(dev->net, "EEPROM: magic value mismatch, magic = 0x%x\n",
572 ee->magic);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000573 return -EINVAL;
574 }
575
576 return smsc95xx_write_eeprom(dev, ee->offset, ee->len, data);
577}
578
579static u32 smsc95xx_ethtool_get_rx_csum(struct net_device *netdev)
580{
581 struct usbnet *dev = netdev_priv(netdev);
582 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
583
584 return pdata->use_rx_csum;
585}
586
587static int smsc95xx_ethtool_set_rx_csum(struct net_device *netdev, u32 val)
588{
589 struct usbnet *dev = netdev_priv(netdev);
590 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
591
592 pdata->use_rx_csum = !!val;
593
Steve Glendinningf7b29272008-11-20 04:19:21 -0800594 return smsc95xx_set_csums(dev);
595}
596
597static u32 smsc95xx_ethtool_get_tx_csum(struct net_device *netdev)
598{
599 struct usbnet *dev = netdev_priv(netdev);
600 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
601
602 return pdata->use_tx_csum;
603}
604
605static int smsc95xx_ethtool_set_tx_csum(struct net_device *netdev, u32 val)
606{
607 struct usbnet *dev = netdev_priv(netdev);
608 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
609
610 pdata->use_tx_csum = !!val;
611
612 ethtool_op_set_tx_hw_csum(netdev, pdata->use_tx_csum);
613 return smsc95xx_set_csums(dev);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000614}
615
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700616static const struct ethtool_ops smsc95xx_ethtool_ops = {
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000617 .get_link = usbnet_get_link,
618 .nway_reset = usbnet_nway_reset,
619 .get_drvinfo = usbnet_get_drvinfo,
620 .get_msglevel = usbnet_get_msglevel,
621 .set_msglevel = usbnet_set_msglevel,
622 .get_settings = usbnet_get_settings,
623 .set_settings = usbnet_set_settings,
624 .get_eeprom_len = smsc95xx_ethtool_get_eeprom_len,
625 .get_eeprom = smsc95xx_ethtool_get_eeprom,
626 .set_eeprom = smsc95xx_ethtool_set_eeprom,
Steve Glendinningf7b29272008-11-20 04:19:21 -0800627 .get_tx_csum = smsc95xx_ethtool_get_tx_csum,
628 .set_tx_csum = smsc95xx_ethtool_set_tx_csum,
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000629 .get_rx_csum = smsc95xx_ethtool_get_rx_csum,
630 .set_rx_csum = smsc95xx_ethtool_set_rx_csum,
631};
632
633static int smsc95xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
634{
635 struct usbnet *dev = netdev_priv(netdev);
636
637 if (!netif_running(netdev))
638 return -EINVAL;
639
640 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
641}
642
643static void smsc95xx_init_mac_address(struct usbnet *dev)
644{
645 /* try reading mac address from EEPROM */
646 if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
647 dev->net->dev_addr) == 0) {
648 if (is_valid_ether_addr(dev->net->dev_addr)) {
649 /* eeprom values are valid so use them */
Joe Perchesa475f602010-02-17 10:30:24 +0000650 netif_dbg(dev, ifup, dev->net, "MAC address read from EEPROM\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000651 return;
652 }
653 }
654
655 /* no eeprom, or eeprom values are invalid. generate random MAC */
656 random_ether_addr(dev->net->dev_addr);
Joe Perchesa475f602010-02-17 10:30:24 +0000657 netif_dbg(dev, ifup, dev->net, "MAC address set to random_ether_addr\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000658}
659
660static int smsc95xx_set_mac_address(struct usbnet *dev)
661{
662 u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
663 dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
664 u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
665 int ret;
666
667 ret = smsc95xx_write_reg(dev, ADDRL, addr_lo);
668 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000669 netdev_warn(dev->net, "Failed to write ADDRL: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000670 return ret;
671 }
672
673 ret = smsc95xx_write_reg(dev, ADDRH, addr_hi);
674 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000675 netdev_warn(dev->net, "Failed to write ADDRH: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000676 return ret;
677 }
678
679 return 0;
680}
681
682/* starts the TX path */
683static void smsc95xx_start_tx_path(struct usbnet *dev)
684{
685 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
686 unsigned long flags;
687 u32 reg_val;
688
689 /* Enable Tx at MAC */
690 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
691 pdata->mac_cr |= MAC_CR_TXEN_;
692 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
693
694 smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
695
696 /* Enable Tx at SCSRs */
697 reg_val = TX_CFG_ON_;
698 smsc95xx_write_reg(dev, TX_CFG, reg_val);
699}
700
701/* Starts the Receive path */
702static void smsc95xx_start_rx_path(struct usbnet *dev)
703{
704 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
705 unsigned long flags;
706
707 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
708 pdata->mac_cr |= MAC_CR_RXEN_;
709 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
710
711 smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
712}
713
714static int smsc95xx_phy_initialize(struct usbnet *dev)
715{
Steve Glendinningdb443c42010-03-16 09:03:06 +0000716 int bmcr, timeout = 0;
717
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000718 /* Initialize MII structure */
719 dev->mii.dev = dev->net;
720 dev->mii.mdio_read = smsc95xx_mdio_read;
721 dev->mii.mdio_write = smsc95xx_mdio_write;
722 dev->mii.phy_id_mask = 0x1f;
723 dev->mii.reg_num_mask = 0x1f;
724 dev->mii.phy_id = SMSC95XX_INTERNAL_PHY_ID;
725
Steve Glendinningdb443c42010-03-16 09:03:06 +0000726 /* reset phy and wait for reset to complete */
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000727 smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
Steve Glendinningdb443c42010-03-16 09:03:06 +0000728
729 do {
730 msleep(10);
731 bmcr = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
732 timeout++;
733 } while ((bmcr & MII_BMCR) && (timeout < 100));
734
735 if (timeout >= 100) {
736 netdev_warn(dev->net, "timeout on PHY Reset");
737 return -EIO;
738 }
739
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000740 smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
741 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
742 ADVERTISE_PAUSE_ASYM);
743
744 /* read to clear */
745 smsc95xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
746
747 smsc95xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
748 PHY_INT_MASK_DEFAULT_);
749 mii_nway_restart(&dev->mii);
750
Joe Perchesa475f602010-02-17 10:30:24 +0000751 netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000752 return 0;
753}
754
755static int smsc95xx_reset(struct usbnet *dev)
756{
757 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
Steve Glendinningf7b29272008-11-20 04:19:21 -0800758 struct net_device *netdev = dev->net;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000759 u32 read_buf, write_buf, burst_cap;
760 int ret = 0, timeout;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000761
Joe Perchesa475f602010-02-17 10:30:24 +0000762 netif_dbg(dev, ifup, dev->net, "entering smsc95xx_reset\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000763
764 write_buf = HW_CFG_LRST_;
765 ret = smsc95xx_write_reg(dev, HW_CFG, write_buf);
766 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000767 netdev_warn(dev->net, "Failed to write HW_CFG_LRST_ bit in HW_CFG register, ret = %d\n",
768 ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000769 return ret;
770 }
771
772 timeout = 0;
773 do {
774 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
775 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000776 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000777 return ret;
778 }
779 msleep(10);
780 timeout++;
781 } while ((read_buf & HW_CFG_LRST_) && (timeout < 100));
782
783 if (timeout >= 100) {
Joe Perches60b86752010-02-17 10:30:23 +0000784 netdev_warn(dev->net, "timeout waiting for completion of Lite Reset\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000785 return ret;
786 }
787
788 write_buf = PM_CTL_PHY_RST_;
789 ret = smsc95xx_write_reg(dev, PM_CTRL, write_buf);
790 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000791 netdev_warn(dev->net, "Failed to write PM_CTRL: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000792 return ret;
793 }
794
795 timeout = 0;
796 do {
797 ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf);
798 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000799 netdev_warn(dev->net, "Failed to read PM_CTRL: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000800 return ret;
801 }
802 msleep(10);
803 timeout++;
804 } while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));
805
806 if (timeout >= 100) {
Joe Perches60b86752010-02-17 10:30:23 +0000807 netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000808 return ret;
809 }
810
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000811 ret = smsc95xx_set_mac_address(dev);
812 if (ret < 0)
813 return ret;
814
Joe Perchesa475f602010-02-17 10:30:24 +0000815 netif_dbg(dev, ifup, dev->net,
816 "MAC Address: %pM\n", dev->net->dev_addr);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000817
818 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
819 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000820 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000821 return ret;
822 }
823
Joe Perchesa475f602010-02-17 10:30:24 +0000824 netif_dbg(dev, ifup, dev->net,
825 "Read Value from HW_CFG : 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000826
827 read_buf |= HW_CFG_BIR_;
828
829 ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
830 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000831 netdev_warn(dev->net, "Failed to write HW_CFG_BIR_ bit in HW_CFG register, ret = %d\n",
832 ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000833 return ret;
834 }
835
836 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
837 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000838 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000839 return ret;
840 }
Joe Perchesa475f602010-02-17 10:30:24 +0000841 netif_dbg(dev, ifup, dev->net,
842 "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
843 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000844
845 if (!turbo_mode) {
846 burst_cap = 0;
847 dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
848 } else if (dev->udev->speed == USB_SPEED_HIGH) {
849 burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
850 dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
851 } else {
852 burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
853 dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
854 }
855
Joe Perchesa475f602010-02-17 10:30:24 +0000856 netif_dbg(dev, ifup, dev->net,
857 "rx_urb_size=%ld\n", (ulong)dev->rx_urb_size);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000858
859 ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
860 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000861 netdev_warn(dev->net, "Failed to write BURST_CAP: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000862 return ret;
863 }
864
865 ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf);
866 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000867 netdev_warn(dev->net, "Failed to read BURST_CAP: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000868 return ret;
869 }
Joe Perchesa475f602010-02-17 10:30:24 +0000870 netif_dbg(dev, ifup, dev->net,
871 "Read Value from BURST_CAP after writing: 0x%08x\n",
872 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000873
874 read_buf = DEFAULT_BULK_IN_DELAY;
875 ret = smsc95xx_write_reg(dev, BULK_IN_DLY, read_buf);
876 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000877 netdev_warn(dev->net, "ret = %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000878 return ret;
879 }
880
881 ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf);
882 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000883 netdev_warn(dev->net, "Failed to read BULK_IN_DLY: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000884 return ret;
885 }
Joe Perchesa475f602010-02-17 10:30:24 +0000886 netif_dbg(dev, ifup, dev->net,
887 "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
888 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000889
890 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
891 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000892 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000893 return ret;
894 }
Joe Perchesa475f602010-02-17 10:30:24 +0000895 netif_dbg(dev, ifup, dev->net,
896 "Read Value from HW_CFG: 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000897
898 if (turbo_mode)
899 read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);
900
901 read_buf &= ~HW_CFG_RXDOFF_;
902
903 /* set Rx data offset=2, Make IP header aligns on word boundary. */
904 read_buf |= NET_IP_ALIGN << 9;
905
906 ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
907 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000908 netdev_warn(dev->net, "Failed to write HW_CFG register, ret=%d\n",
909 ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000910 return ret;
911 }
912
913 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
914 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000915 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000916 return ret;
917 }
Joe Perchesa475f602010-02-17 10:30:24 +0000918 netif_dbg(dev, ifup, dev->net,
919 "Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000920
921 write_buf = 0xFFFFFFFF;
922 ret = smsc95xx_write_reg(dev, INT_STS, write_buf);
923 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000924 netdev_warn(dev->net, "Failed to write INT_STS register, ret=%d\n",
925 ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000926 return ret;
927 }
928
929 ret = smsc95xx_read_reg(dev, ID_REV, &read_buf);
930 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000931 netdev_warn(dev->net, "Failed to read ID_REV: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000932 return ret;
933 }
Joe Perchesa475f602010-02-17 10:30:24 +0000934 netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000935
Steve Glendinningf2935012009-05-01 05:46:51 +0000936 /* Configure GPIO pins as LED outputs */
937 write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED |
938 LED_GPIO_CFG_FDX_LED;
939 ret = smsc95xx_write_reg(dev, LED_GPIO_CFG, write_buf);
940 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000941 netdev_warn(dev->net, "Failed to write LED_GPIO_CFG register, ret=%d\n",
942 ret);
Steve Glendinningf2935012009-05-01 05:46:51 +0000943 return ret;
944 }
945
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000946 /* Init Tx */
947 write_buf = 0;
948 ret = smsc95xx_write_reg(dev, FLOW, write_buf);
949 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000950 netdev_warn(dev->net, "Failed to write FLOW: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000951 return ret;
952 }
953
954 read_buf = AFC_CFG_DEFAULT;
955 ret = smsc95xx_write_reg(dev, AFC_CFG, read_buf);
956 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000957 netdev_warn(dev->net, "Failed to write AFC_CFG: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000958 return ret;
959 }
960
961 /* Don't need mac_cr_lock during initialisation */
962 ret = smsc95xx_read_reg(dev, MAC_CR, &pdata->mac_cr);
963 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000964 netdev_warn(dev->net, "Failed to read MAC_CR: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000965 return ret;
966 }
967
968 /* Init Rx */
969 /* Set Vlan */
970 write_buf = (u32)ETH_P_8021Q;
971 ret = smsc95xx_write_reg(dev, VLAN1, write_buf);
972 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000973 netdev_warn(dev->net, "Failed to write VAN1: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000974 return ret;
975 }
976
Steve Glendinningf7b29272008-11-20 04:19:21 -0800977 /* Enable or disable checksum offload engines */
978 ethtool_op_set_tx_hw_csum(netdev, pdata->use_tx_csum);
979 ret = smsc95xx_set_csums(dev);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000980 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000981 netdev_warn(dev->net, "Failed to set csum offload: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000982 return ret;
983 }
984
985 smsc95xx_set_multicast(dev->net);
986
987 if (smsc95xx_phy_initialize(dev) < 0)
988 return -EIO;
989
990 ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
991 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +0000992 netdev_warn(dev->net, "Failed to read INT_EP_CTL: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000993 return ret;
994 }
995
996 /* enable PHY interrupts */
997 read_buf |= INT_EP_CTL_PHY_INT_;
998
999 ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf);
1000 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +00001001 netdev_warn(dev->net, "Failed to write INT_EP_CTL: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001002 return ret;
1003 }
1004
1005 smsc95xx_start_tx_path(dev);
1006 smsc95xx_start_rx_path(dev);
1007
Joe Perchesa475f602010-02-17 10:30:24 +00001008 netif_dbg(dev, ifup, dev->net, "smsc95xx_reset, return 0\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001009 return 0;
1010}
1011
Stephen Hemminger63e77b32009-03-20 19:35:58 +00001012static const struct net_device_ops smsc95xx_netdev_ops = {
1013 .ndo_open = usbnet_open,
1014 .ndo_stop = usbnet_stop,
1015 .ndo_start_xmit = usbnet_start_xmit,
1016 .ndo_tx_timeout = usbnet_tx_timeout,
1017 .ndo_change_mtu = usbnet_change_mtu,
1018 .ndo_set_mac_address = eth_mac_addr,
1019 .ndo_validate_addr = eth_validate_addr,
1020 .ndo_do_ioctl = smsc95xx_ioctl,
1021 .ndo_set_multicast_list = smsc95xx_set_multicast,
1022};
1023
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001024static int smsc95xx_bind(struct usbnet *dev, struct usb_interface *intf)
1025{
1026 struct smsc95xx_priv *pdata = NULL;
1027 int ret;
1028
1029 printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
1030
1031 ret = usbnet_get_endpoints(dev, intf);
1032 if (ret < 0) {
Joe Perches60b86752010-02-17 10:30:23 +00001033 netdev_warn(dev->net, "usbnet_get_endpoints failed: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001034 return ret;
1035 }
1036
1037 dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc95xx_priv),
1038 GFP_KERNEL);
1039
1040 pdata = (struct smsc95xx_priv *)(dev->data[0]);
1041 if (!pdata) {
Joe Perches60b86752010-02-17 10:30:23 +00001042 netdev_warn(dev->net, "Unable to allocate struct smsc95xx_priv\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001043 return -ENOMEM;
1044 }
1045
1046 spin_lock_init(&pdata->mac_cr_lock);
1047
Steve Glendinningf7b29272008-11-20 04:19:21 -08001048 pdata->use_tx_csum = DEFAULT_TX_CSUM_ENABLE;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001049 pdata->use_rx_csum = DEFAULT_RX_CSUM_ENABLE;
1050
Bernard Blackhamf4e8ab72010-10-18 13:16:39 +00001051 smsc95xx_init_mac_address(dev);
1052
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001053 /* Init all registers */
1054 ret = smsc95xx_reset(dev);
1055
Stephen Hemminger63e77b32009-03-20 19:35:58 +00001056 dev->net->netdev_ops = &smsc95xx_netdev_ops;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001057 dev->net->ethtool_ops = &smsc95xx_ethtool_ops;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001058 dev->net->flags |= IFF_MULTICAST;
1059 dev->net->hard_header_len += SMSC95XX_TX_OVERHEAD;
1060 return 0;
1061}
1062
1063static void smsc95xx_unbind(struct usbnet *dev, struct usb_interface *intf)
1064{
1065 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1066 if (pdata) {
Joe Perchesa475f602010-02-17 10:30:24 +00001067 netif_dbg(dev, ifdown, dev->net, "free pdata\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001068 kfree(pdata);
1069 pdata = NULL;
1070 dev->data[0] = 0;
1071 }
1072}
1073
1074static void smsc95xx_rx_csum_offload(struct sk_buff *skb)
1075{
1076 skb->csum = *(u16 *)(skb_tail_pointer(skb) - 2);
1077 skb->ip_summed = CHECKSUM_COMPLETE;
1078 skb_trim(skb, skb->len - 2);
1079}
1080
1081static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
1082{
1083 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1084
1085 while (skb->len > 0) {
1086 u32 header, align_count;
1087 struct sk_buff *ax_skb;
1088 unsigned char *packet;
1089 u16 size;
1090
1091 memcpy(&header, skb->data, sizeof(header));
1092 le32_to_cpus(&header);
1093 skb_pull(skb, 4 + NET_IP_ALIGN);
1094 packet = skb->data;
1095
1096 /* get the packet length */
1097 size = (u16)((header & RX_STS_FL_) >> 16);
1098 align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4;
1099
1100 if (unlikely(header & RX_STS_ES_)) {
Joe Perchesa475f602010-02-17 10:30:24 +00001101 netif_dbg(dev, rx_err, dev->net,
1102 "Error header=0x%08x\n", header);
Herbert Xu80667ac2009-06-29 16:53:00 +00001103 dev->net->stats.rx_errors++;
1104 dev->net->stats.rx_dropped++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001105
1106 if (header & RX_STS_CRC_) {
Herbert Xu80667ac2009-06-29 16:53:00 +00001107 dev->net->stats.rx_crc_errors++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001108 } else {
1109 if (header & (RX_STS_TL_ | RX_STS_RF_))
Herbert Xu80667ac2009-06-29 16:53:00 +00001110 dev->net->stats.rx_frame_errors++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001111
1112 if ((header & RX_STS_LE_) &&
1113 (!(header & RX_STS_FT_)))
Herbert Xu80667ac2009-06-29 16:53:00 +00001114 dev->net->stats.rx_length_errors++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001115 }
1116 } else {
1117 /* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
1118 if (unlikely(size > (ETH_FRAME_LEN + 12))) {
Joe Perchesa475f602010-02-17 10:30:24 +00001119 netif_dbg(dev, rx_err, dev->net,
1120 "size err header=0x%08x\n", header);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001121 return 0;
1122 }
1123
1124 /* last frame in this batch */
1125 if (skb->len == size) {
1126 if (pdata->use_rx_csum)
1127 smsc95xx_rx_csum_offload(skb);
Peter Korsgaarddf18acc2009-05-13 10:10:41 +00001128 skb_trim(skb, skb->len - 4); /* remove fcs */
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001129 skb->truesize = size + sizeof(struct sk_buff);
1130
1131 return 1;
1132 }
1133
1134 ax_skb = skb_clone(skb, GFP_ATOMIC);
1135 if (unlikely(!ax_skb)) {
Joe Perches60b86752010-02-17 10:30:23 +00001136 netdev_warn(dev->net, "Error allocating skb\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001137 return 0;
1138 }
1139
1140 ax_skb->len = size;
1141 ax_skb->data = packet;
1142 skb_set_tail_pointer(ax_skb, size);
1143
1144 if (pdata->use_rx_csum)
1145 smsc95xx_rx_csum_offload(ax_skb);
Peter Korsgaarddf18acc2009-05-13 10:10:41 +00001146 skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001147 ax_skb->truesize = size + sizeof(struct sk_buff);
1148
1149 usbnet_skb_return(dev, ax_skb);
1150 }
1151
1152 skb_pull(skb, size);
1153
1154 /* padding bytes before the next frame starts */
1155 if (skb->len)
1156 skb_pull(skb, align_count);
1157 }
1158
1159 if (unlikely(skb->len < 0)) {
Joe Perches60b86752010-02-17 10:30:23 +00001160 netdev_warn(dev->net, "invalid rx length<0 %d\n", skb->len);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001161 return 0;
1162 }
1163
1164 return 1;
1165}
1166
Steve Glendinningf7b29272008-11-20 04:19:21 -08001167static u32 smsc95xx_calc_csum_preamble(struct sk_buff *skb)
1168{
Michał Mirosław55508d62010-12-14 15:24:08 +00001169 u16 low_16 = (u16)skb_checksum_start_offset(skb);
1170 u16 high_16 = low_16 + skb->csum_offset;
Steve Glendinningf7b29272008-11-20 04:19:21 -08001171 return (high_16 << 16) | low_16;
1172}
1173
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001174static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,
1175 struct sk_buff *skb, gfp_t flags)
1176{
Steve Glendinningf7b29272008-11-20 04:19:21 -08001177 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1178 bool csum = pdata->use_tx_csum && (skb->ip_summed == CHECKSUM_PARTIAL);
1179 int overhead = csum ? SMSC95XX_TX_OVERHEAD_CSUM : SMSC95XX_TX_OVERHEAD;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001180 u32 tx_cmd_a, tx_cmd_b;
1181
Steve Glendinningf7b29272008-11-20 04:19:21 -08001182 /* We do not advertise SG, so skbs should be already linearized */
1183 BUG_ON(skb_shinfo(skb)->nr_frags);
1184
1185 if (skb_headroom(skb) < overhead) {
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001186 struct sk_buff *skb2 = skb_copy_expand(skb,
Steve Glendinningf7b29272008-11-20 04:19:21 -08001187 overhead, 0, flags);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001188 dev_kfree_skb_any(skb);
1189 skb = skb2;
1190 if (!skb)
1191 return NULL;
1192 }
1193
Steve Glendinningf7b29272008-11-20 04:19:21 -08001194 if (csum) {
Steve Glendinning11bc3082010-03-18 22:18:41 -07001195 if (skb->len <= 45) {
1196 /* workaround - hardware tx checksum does not work
1197 * properly with extremely small packets */
Michał Mirosław55508d62010-12-14 15:24:08 +00001198 long csstart = skb_checksum_start_offset(skb);
Steve Glendinning11bc3082010-03-18 22:18:41 -07001199 __wsum calc = csum_partial(skb->data + csstart,
1200 skb->len - csstart, 0);
1201 *((__sum16 *)(skb->data + csstart
1202 + skb->csum_offset)) = csum_fold(calc);
1203
1204 csum = false;
1205 } else {
1206 u32 csum_preamble = smsc95xx_calc_csum_preamble(skb);
1207 skb_push(skb, 4);
1208 memcpy(skb->data, &csum_preamble, 4);
1209 }
Steve Glendinningf7b29272008-11-20 04:19:21 -08001210 }
1211
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001212 skb_push(skb, 4);
1213 tx_cmd_b = (u32)(skb->len - 4);
Steve Glendinningf7b29272008-11-20 04:19:21 -08001214 if (csum)
1215 tx_cmd_b |= TX_CMD_B_CSUM_ENABLE;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001216 cpu_to_le32s(&tx_cmd_b);
1217 memcpy(skb->data, &tx_cmd_b, 4);
1218
1219 skb_push(skb, 4);
1220 tx_cmd_a = (u32)(skb->len - 8) | TX_CMD_A_FIRST_SEG_ |
1221 TX_CMD_A_LAST_SEG_;
1222 cpu_to_le32s(&tx_cmd_a);
1223 memcpy(skb->data, &tx_cmd_a, 4);
1224
1225 return skb;
1226}
1227
1228static const struct driver_info smsc95xx_info = {
1229 .description = "smsc95xx USB 2.0 Ethernet",
1230 .bind = smsc95xx_bind,
1231 .unbind = smsc95xx_unbind,
1232 .link_reset = smsc95xx_link_reset,
1233 .reset = smsc95xx_reset,
1234 .rx_fixup = smsc95xx_rx_fixup,
1235 .tx_fixup = smsc95xx_tx_fixup,
1236 .status = smsc95xx_status,
Steve Glendinningec475622009-09-22 04:00:27 +00001237 .flags = FLAG_ETHER | FLAG_SEND_ZLP,
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001238};
1239
1240static const struct usb_device_id products[] = {
1241 {
1242 /* SMSC9500 USB Ethernet Device */
1243 USB_DEVICE(0x0424, 0x9500),
1244 .driver_info = (unsigned long) &smsc95xx_info,
1245 },
Steve Glendinning726474b2009-05-01 06:07:22 +00001246 {
Steve Glendinning6f41d122009-09-22 05:13:02 +00001247 /* SMSC9505 USB Ethernet Device */
1248 USB_DEVICE(0x0424, 0x9505),
1249 .driver_info = (unsigned long) &smsc95xx_info,
1250 },
1251 {
1252 /* SMSC9500A USB Ethernet Device */
1253 USB_DEVICE(0x0424, 0x9E00),
1254 .driver_info = (unsigned long) &smsc95xx_info,
1255 },
1256 {
1257 /* SMSC9505A USB Ethernet Device */
1258 USB_DEVICE(0x0424, 0x9E01),
1259 .driver_info = (unsigned long) &smsc95xx_info,
1260 },
1261 {
Steve Glendinning726474b2009-05-01 06:07:22 +00001262 /* SMSC9512/9514 USB Hub & Ethernet Device */
1263 USB_DEVICE(0x0424, 0xec00),
1264 .driver_info = (unsigned long) &smsc95xx_info,
1265 },
Steve Glendinning6f41d122009-09-22 05:13:02 +00001266 {
1267 /* SMSC9500 USB Ethernet Device (SAL10) */
1268 USB_DEVICE(0x0424, 0x9900),
1269 .driver_info = (unsigned long) &smsc95xx_info,
1270 },
1271 {
1272 /* SMSC9505 USB Ethernet Device (SAL10) */
1273 USB_DEVICE(0x0424, 0x9901),
1274 .driver_info = (unsigned long) &smsc95xx_info,
1275 },
1276 {
1277 /* SMSC9500A USB Ethernet Device (SAL10) */
1278 USB_DEVICE(0x0424, 0x9902),
1279 .driver_info = (unsigned long) &smsc95xx_info,
1280 },
1281 {
1282 /* SMSC9505A USB Ethernet Device (SAL10) */
1283 USB_DEVICE(0x0424, 0x9903),
1284 .driver_info = (unsigned long) &smsc95xx_info,
1285 },
1286 {
1287 /* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */
1288 USB_DEVICE(0x0424, 0x9904),
1289 .driver_info = (unsigned long) &smsc95xx_info,
1290 },
1291 {
1292 /* SMSC9500A USB Ethernet Device (HAL) */
1293 USB_DEVICE(0x0424, 0x9905),
1294 .driver_info = (unsigned long) &smsc95xx_info,
1295 },
1296 {
1297 /* SMSC9505A USB Ethernet Device (HAL) */
1298 USB_DEVICE(0x0424, 0x9906),
1299 .driver_info = (unsigned long) &smsc95xx_info,
1300 },
1301 {
1302 /* SMSC9500 USB Ethernet Device (Alternate ID) */
1303 USB_DEVICE(0x0424, 0x9907),
1304 .driver_info = (unsigned long) &smsc95xx_info,
1305 },
1306 {
1307 /* SMSC9500A USB Ethernet Device (Alternate ID) */
1308 USB_DEVICE(0x0424, 0x9908),
1309 .driver_info = (unsigned long) &smsc95xx_info,
1310 },
1311 {
1312 /* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */
1313 USB_DEVICE(0x0424, 0x9909),
1314 .driver_info = (unsigned long) &smsc95xx_info,
1315 },
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001316 { }, /* END */
1317};
1318MODULE_DEVICE_TABLE(usb, products);
1319
1320static struct usb_driver smsc95xx_driver = {
1321 .name = "smsc95xx",
1322 .id_table = products,
1323 .probe = usbnet_probe,
1324 .suspend = usbnet_suspend,
1325 .resume = usbnet_resume,
1326 .disconnect = usbnet_disconnect,
1327};
1328
1329static int __init smsc95xx_init(void)
1330{
1331 return usb_register(&smsc95xx_driver);
1332}
1333module_init(smsc95xx_init);
1334
1335static void __exit smsc95xx_exit(void)
1336{
1337 usb_deregister(&smsc95xx_driver);
1338}
1339module_exit(smsc95xx_exit);
1340
1341MODULE_AUTHOR("Nancy Lin");
1342MODULE_AUTHOR("Steve Glendinning <steve.glendinning@smsc.com>");
1343MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices");
1344MODULE_LICENSE("GPL");