blob: c26aeba1838e2ceb6bffa1da4ce47d83a3b9136e [file] [log] [blame]
Steve Glendinningd0cad872010-03-16 08:46:46 +00001 /***************************************************************************
2 *
3 * Copyright (C) 2007-2010 SMSC
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 *****************************************************************************/
20
21#include <linux/module.h>
22#include <linux/kmod.h>
23#include <linux/init.h>
24#include <linux/netdevice.h>
25#include <linux/etherdevice.h>
26#include <linux/ethtool.h>
27#include <linux/mii.h>
28#include <linux/usb.h>
Steve Glendinning899a3912012-10-30 07:46:32 +000029#include <linux/bitrev.h>
30#include <linux/crc16.h>
Steve Glendinningd0cad872010-03-16 08:46:46 +000031#include <linux/crc32.h>
32#include <linux/usb/usbnet.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Steve Glendinningd0cad872010-03-16 08:46:46 +000034#include "smsc75xx.h"
35
36#define SMSC_CHIPNAME "smsc75xx"
37#define SMSC_DRIVER_VERSION "1.0.0"
38#define HS_USB_PKT_SIZE (512)
39#define FS_USB_PKT_SIZE (64)
40#define DEFAULT_HS_BURST_CAP_SIZE (16 * 1024 + 5 * HS_USB_PKT_SIZE)
41#define DEFAULT_FS_BURST_CAP_SIZE (6 * 1024 + 33 * FS_USB_PKT_SIZE)
42#define DEFAULT_BULK_IN_DELAY (0x00002000)
43#define MAX_SINGLE_PACKET_SIZE (9000)
44#define LAN75XX_EEPROM_MAGIC (0x7500)
45#define EEPROM_MAC_OFFSET (0x01)
46#define DEFAULT_TX_CSUM_ENABLE (true)
47#define DEFAULT_RX_CSUM_ENABLE (true)
48#define DEFAULT_TSO_ENABLE (true)
49#define SMSC75XX_INTERNAL_PHY_ID (1)
50#define SMSC75XX_TX_OVERHEAD (8)
51#define MAX_RX_FIFO_SIZE (20 * 1024)
52#define MAX_TX_FIFO_SIZE (12 * 1024)
53#define USB_VENDOR_ID_SMSC (0x0424)
54#define USB_PRODUCT_ID_LAN7500 (0x7500)
55#define USB_PRODUCT_ID_LAN7505 (0x7505)
Nico Erfurthea1649d2011-11-08 07:30:40 +000056#define RXW_PADDING 2
Steve Glendinning899a3912012-10-30 07:46:32 +000057#define SUPPORTED_WAKE (WAKE_UCAST | WAKE_BCAST | \
58 WAKE_MCAST | WAKE_ARP | WAKE_MAGIC)
Steve Glendinningd0cad872010-03-16 08:46:46 +000059
60#define check_warn(ret, fmt, args...) \
61 ({ if (ret < 0) netdev_warn(dev->net, fmt, ##args); })
62
63#define check_warn_return(ret, fmt, args...) \
64 ({ if (ret < 0) { netdev_warn(dev->net, fmt, ##args); return ret; } })
65
66#define check_warn_goto_done(ret, fmt, args...) \
67 ({ if (ret < 0) { netdev_warn(dev->net, fmt, ##args); goto done; } })
68
69struct smsc75xx_priv {
70 struct usbnet *dev;
71 u32 rfe_ctl;
Steve Glendinning6c636502012-09-28 00:57:53 +000072 u32 wolopts;
Steve Glendinningd0cad872010-03-16 08:46:46 +000073 u32 multicast_hash_table[DP_SEL_VHF_HASH_LEN];
Steve Glendinningd0cad872010-03-16 08:46:46 +000074 struct mutex dataport_mutex;
75 spinlock_t rfe_ctl_lock;
76 struct work_struct set_multicast;
77};
78
79struct usb_context {
80 struct usb_ctrlrequest req;
81 struct usbnet *dev;
82};
83
Rusty Russelleb939922011-12-19 14:08:01 +000084static bool turbo_mode = true;
Steve Glendinningd0cad872010-03-16 08:46:46 +000085module_param(turbo_mode, bool, 0644);
86MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
87
Ming Lei47bbea42012-11-06 04:53:05 +000088static int __must_check __smsc75xx_read_reg(struct usbnet *dev, u32 index,
89 u32 *data, int in_pm)
Steve Glendinningd0cad872010-03-16 08:46:46 +000090{
Ming Lei2b2e41e2012-10-24 19:47:03 +000091 u32 buf;
Steve Glendinningd0cad872010-03-16 08:46:46 +000092 int ret;
Ming Lei47bbea42012-11-06 04:53:05 +000093 int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
Steve Glendinningd0cad872010-03-16 08:46:46 +000094
95 BUG_ON(!dev);
96
Ming Lei47bbea42012-11-06 04:53:05 +000097 if (!in_pm)
98 fn = usbnet_read_cmd;
99 else
100 fn = usbnet_read_cmd_nopm;
101
102 ret = fn(dev, USB_VENDOR_REQUEST_READ_REGISTER, USB_DIR_IN
103 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
104 0, index, &buf, 4);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000105 if (unlikely(ret < 0))
Joe Perches1e1d7412012-11-24 01:27:49 +0000106 netdev_warn(dev->net, "Failed to read reg index 0x%08x: %d\n",
107 index, ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000108
Ming Lei2b2e41e2012-10-24 19:47:03 +0000109 le32_to_cpus(&buf);
110 *data = buf;
Steve Glendinningd0cad872010-03-16 08:46:46 +0000111
112 return ret;
113}
114
Ming Lei47bbea42012-11-06 04:53:05 +0000115static int __must_check __smsc75xx_write_reg(struct usbnet *dev, u32 index,
116 u32 data, int in_pm)
Steve Glendinningd0cad872010-03-16 08:46:46 +0000117{
Ming Lei2b2e41e2012-10-24 19:47:03 +0000118 u32 buf;
Steve Glendinningd0cad872010-03-16 08:46:46 +0000119 int ret;
Ming Lei47bbea42012-11-06 04:53:05 +0000120 int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000121
122 BUG_ON(!dev);
123
Ming Lei47bbea42012-11-06 04:53:05 +0000124 if (!in_pm)
125 fn = usbnet_write_cmd;
126 else
127 fn = usbnet_write_cmd_nopm;
128
Ming Lei2b2e41e2012-10-24 19:47:03 +0000129 buf = data;
130 cpu_to_le32s(&buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000131
Ming Lei47bbea42012-11-06 04:53:05 +0000132 ret = fn(dev, USB_VENDOR_REQUEST_WRITE_REGISTER, USB_DIR_OUT
133 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
134 0, index, &buf, 4);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000135 if (unlikely(ret < 0))
Joe Perches1e1d7412012-11-24 01:27:49 +0000136 netdev_warn(dev->net, "Failed to write reg index 0x%08x: %d\n",
137 index, ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000138
Steve Glendinningd0cad872010-03-16 08:46:46 +0000139 return ret;
140}
141
Ming Lei47bbea42012-11-06 04:53:05 +0000142static int __must_check smsc75xx_read_reg_nopm(struct usbnet *dev, u32 index,
143 u32 *data)
144{
145 return __smsc75xx_read_reg(dev, index, data, 1);
146}
147
148static int __must_check smsc75xx_write_reg_nopm(struct usbnet *dev, u32 index,
149 u32 data)
150{
151 return __smsc75xx_write_reg(dev, index, data, 1);
152}
153
154static int __must_check smsc75xx_read_reg(struct usbnet *dev, u32 index,
155 u32 *data)
156{
157 return __smsc75xx_read_reg(dev, index, data, 0);
158}
159
160static int __must_check smsc75xx_write_reg(struct usbnet *dev, u32 index,
161 u32 data)
162{
163 return __smsc75xx_write_reg(dev, index, data, 0);
164}
165
Steve Glendinning6c636502012-09-28 00:57:53 +0000166static int smsc75xx_set_feature(struct usbnet *dev, u32 feature)
167{
168 if (WARN_ON_ONCE(!dev))
169 return -EINVAL;
170
Ming Lei47bbea42012-11-06 04:53:05 +0000171 return usbnet_write_cmd_nopm(dev, USB_REQ_SET_FEATURE,
172 USB_DIR_OUT | USB_RECIP_DEVICE,
173 feature, 0, NULL, 0);
Steve Glendinning6c636502012-09-28 00:57:53 +0000174}
175
176static int smsc75xx_clear_feature(struct usbnet *dev, u32 feature)
177{
178 if (WARN_ON_ONCE(!dev))
179 return -EINVAL;
180
Ming Lei47bbea42012-11-06 04:53:05 +0000181 return usbnet_write_cmd_nopm(dev, USB_REQ_CLEAR_FEATURE,
182 USB_DIR_OUT | USB_RECIP_DEVICE,
183 feature, 0, NULL, 0);
Steve Glendinning6c636502012-09-28 00:57:53 +0000184}
185
Steve Glendinningd0cad872010-03-16 08:46:46 +0000186/* Loop until the read is completed with timeout
187 * called with phy_mutex held */
188static int smsc75xx_phy_wait_not_busy(struct usbnet *dev)
189{
190 unsigned long start_time = jiffies;
191 u32 val;
192 int ret;
193
194 do {
195 ret = smsc75xx_read_reg(dev, MII_ACCESS, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +0000196 check_warn_return(ret, "Error reading MII_ACCESS\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000197
198 if (!(val & MII_ACCESS_BUSY))
199 return 0;
200 } while (!time_after(jiffies, start_time + HZ));
201
202 return -EIO;
203}
204
205static int smsc75xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
206{
207 struct usbnet *dev = netdev_priv(netdev);
208 u32 val, addr;
209 int ret;
210
211 mutex_lock(&dev->phy_mutex);
212
213 /* confirm MII not busy */
214 ret = smsc75xx_phy_wait_not_busy(dev);
Joe Perches1e1d7412012-11-24 01:27:49 +0000215 check_warn_goto_done(ret, "MII is busy in smsc75xx_mdio_read\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000216
217 /* set the address, index & direction (read from PHY) */
218 phy_id &= dev->mii.phy_id_mask;
219 idx &= dev->mii.reg_num_mask;
220 addr = ((phy_id << MII_ACCESS_PHY_ADDR_SHIFT) & MII_ACCESS_PHY_ADDR)
221 | ((idx << MII_ACCESS_REG_ADDR_SHIFT) & MII_ACCESS_REG_ADDR)
Steve Glendinningcb8722d2012-04-30 07:56:51 +0000222 | MII_ACCESS_READ | MII_ACCESS_BUSY;
Steve Glendinningd0cad872010-03-16 08:46:46 +0000223 ret = smsc75xx_write_reg(dev, MII_ACCESS, addr);
Joe Perches1e1d7412012-11-24 01:27:49 +0000224 check_warn_goto_done(ret, "Error writing MII_ACCESS\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000225
226 ret = smsc75xx_phy_wait_not_busy(dev);
Joe Perches1e1d7412012-11-24 01:27:49 +0000227 check_warn_goto_done(ret, "Timed out reading MII reg %02X\n", idx);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000228
229 ret = smsc75xx_read_reg(dev, MII_DATA, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +0000230 check_warn_goto_done(ret, "Error reading MII_DATA\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000231
232 ret = (u16)(val & 0xFFFF);
233
234done:
235 mutex_unlock(&dev->phy_mutex);
236 return ret;
237}
238
239static void smsc75xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
240 int regval)
241{
242 struct usbnet *dev = netdev_priv(netdev);
243 u32 val, addr;
244 int ret;
245
246 mutex_lock(&dev->phy_mutex);
247
248 /* confirm MII not busy */
249 ret = smsc75xx_phy_wait_not_busy(dev);
Joe Perches1e1d7412012-11-24 01:27:49 +0000250 check_warn_goto_done(ret, "MII is busy in smsc75xx_mdio_write\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000251
252 val = regval;
253 ret = smsc75xx_write_reg(dev, MII_DATA, val);
Joe Perches1e1d7412012-11-24 01:27:49 +0000254 check_warn_goto_done(ret, "Error writing MII_DATA\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000255
256 /* set the address, index & direction (write to PHY) */
257 phy_id &= dev->mii.phy_id_mask;
258 idx &= dev->mii.reg_num_mask;
259 addr = ((phy_id << MII_ACCESS_PHY_ADDR_SHIFT) & MII_ACCESS_PHY_ADDR)
260 | ((idx << MII_ACCESS_REG_ADDR_SHIFT) & MII_ACCESS_REG_ADDR)
Steve Glendinningcb8722d2012-04-30 07:56:51 +0000261 | MII_ACCESS_WRITE | MII_ACCESS_BUSY;
Steve Glendinningd0cad872010-03-16 08:46:46 +0000262 ret = smsc75xx_write_reg(dev, MII_ACCESS, addr);
Joe Perches1e1d7412012-11-24 01:27:49 +0000263 check_warn_goto_done(ret, "Error writing MII_ACCESS\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000264
265 ret = smsc75xx_phy_wait_not_busy(dev);
Joe Perches1e1d7412012-11-24 01:27:49 +0000266 check_warn_goto_done(ret, "Timed out writing MII reg %02X\n", idx);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000267
268done:
269 mutex_unlock(&dev->phy_mutex);
270}
271
272static int smsc75xx_wait_eeprom(struct usbnet *dev)
273{
274 unsigned long start_time = jiffies;
275 u32 val;
276 int ret;
277
278 do {
279 ret = smsc75xx_read_reg(dev, E2P_CMD, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +0000280 check_warn_return(ret, "Error reading E2P_CMD\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000281
282 if (!(val & E2P_CMD_BUSY) || (val & E2P_CMD_TIMEOUT))
283 break;
284 udelay(40);
285 } while (!time_after(jiffies, start_time + HZ));
286
287 if (val & (E2P_CMD_TIMEOUT | E2P_CMD_BUSY)) {
Joe Perches1e1d7412012-11-24 01:27:49 +0000288 netdev_warn(dev->net, "EEPROM read operation timeout\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000289 return -EIO;
290 }
291
292 return 0;
293}
294
295static int smsc75xx_eeprom_confirm_not_busy(struct usbnet *dev)
296{
297 unsigned long start_time = jiffies;
298 u32 val;
299 int ret;
300
301 do {
302 ret = smsc75xx_read_reg(dev, E2P_CMD, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +0000303 check_warn_return(ret, "Error reading E2P_CMD\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000304
305 if (!(val & E2P_CMD_BUSY))
306 return 0;
307
308 udelay(40);
309 } while (!time_after(jiffies, start_time + HZ));
310
Joe Perches1e1d7412012-11-24 01:27:49 +0000311 netdev_warn(dev->net, "EEPROM is busy\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000312 return -EIO;
313}
314
315static int smsc75xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
316 u8 *data)
317{
318 u32 val;
319 int i, ret;
320
321 BUG_ON(!dev);
322 BUG_ON(!data);
323
324 ret = smsc75xx_eeprom_confirm_not_busy(dev);
325 if (ret)
326 return ret;
327
328 for (i = 0; i < length; i++) {
329 val = E2P_CMD_BUSY | E2P_CMD_READ | (offset & E2P_CMD_ADDR);
330 ret = smsc75xx_write_reg(dev, E2P_CMD, val);
Joe Perches1e1d7412012-11-24 01:27:49 +0000331 check_warn_return(ret, "Error writing E2P_CMD\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000332
333 ret = smsc75xx_wait_eeprom(dev);
334 if (ret < 0)
335 return ret;
336
337 ret = smsc75xx_read_reg(dev, E2P_DATA, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +0000338 check_warn_return(ret, "Error reading E2P_DATA\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000339
340 data[i] = val & 0xFF;
341 offset++;
342 }
343
344 return 0;
345}
346
347static int smsc75xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
348 u8 *data)
349{
350 u32 val;
351 int i, ret;
352
353 BUG_ON(!dev);
354 BUG_ON(!data);
355
356 ret = smsc75xx_eeprom_confirm_not_busy(dev);
357 if (ret)
358 return ret;
359
360 /* Issue write/erase enable command */
361 val = E2P_CMD_BUSY | E2P_CMD_EWEN;
362 ret = smsc75xx_write_reg(dev, E2P_CMD, val);
Joe Perches1e1d7412012-11-24 01:27:49 +0000363 check_warn_return(ret, "Error writing E2P_CMD\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000364
365 ret = smsc75xx_wait_eeprom(dev);
366 if (ret < 0)
367 return ret;
368
369 for (i = 0; i < length; i++) {
370
371 /* Fill data register */
372 val = data[i];
373 ret = smsc75xx_write_reg(dev, E2P_DATA, val);
Joe Perches1e1d7412012-11-24 01:27:49 +0000374 check_warn_return(ret, "Error writing E2P_DATA\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000375
376 /* Send "write" command */
377 val = E2P_CMD_BUSY | E2P_CMD_WRITE | (offset & E2P_CMD_ADDR);
378 ret = smsc75xx_write_reg(dev, E2P_CMD, val);
Joe Perches1e1d7412012-11-24 01:27:49 +0000379 check_warn_return(ret, "Error writing E2P_CMD\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000380
381 ret = smsc75xx_wait_eeprom(dev);
382 if (ret < 0)
383 return ret;
384
385 offset++;
386 }
387
388 return 0;
389}
390
391static int smsc75xx_dataport_wait_not_busy(struct usbnet *dev)
392{
393 int i, ret;
394
395 for (i = 0; i < 100; i++) {
396 u32 dp_sel;
397 ret = smsc75xx_read_reg(dev, DP_SEL, &dp_sel);
Joe Perches1e1d7412012-11-24 01:27:49 +0000398 check_warn_return(ret, "Error reading DP_SEL\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000399
400 if (dp_sel & DP_SEL_DPRDY)
401 return 0;
402
403 udelay(40);
404 }
405
Joe Perches1e1d7412012-11-24 01:27:49 +0000406 netdev_warn(dev->net, "smsc75xx_dataport_wait_not_busy timed out\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000407
408 return -EIO;
409}
410
411static int smsc75xx_dataport_write(struct usbnet *dev, u32 ram_select, u32 addr,
412 u32 length, u32 *buf)
413{
414 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
415 u32 dp_sel;
416 int i, ret;
417
418 mutex_lock(&pdata->dataport_mutex);
419
420 ret = smsc75xx_dataport_wait_not_busy(dev);
Joe Perches1e1d7412012-11-24 01:27:49 +0000421 check_warn_goto_done(ret, "smsc75xx_dataport_write busy on entry\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000422
423 ret = smsc75xx_read_reg(dev, DP_SEL, &dp_sel);
Joe Perches1e1d7412012-11-24 01:27:49 +0000424 check_warn_goto_done(ret, "Error reading DP_SEL\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000425
426 dp_sel &= ~DP_SEL_RSEL;
427 dp_sel |= ram_select;
428 ret = smsc75xx_write_reg(dev, DP_SEL, dp_sel);
Joe Perches1e1d7412012-11-24 01:27:49 +0000429 check_warn_goto_done(ret, "Error writing DP_SEL\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000430
431 for (i = 0; i < length; i++) {
432 ret = smsc75xx_write_reg(dev, DP_ADDR, addr + i);
Joe Perches1e1d7412012-11-24 01:27:49 +0000433 check_warn_goto_done(ret, "Error writing DP_ADDR\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000434
435 ret = smsc75xx_write_reg(dev, DP_DATA, buf[i]);
Joe Perches1e1d7412012-11-24 01:27:49 +0000436 check_warn_goto_done(ret, "Error writing DP_DATA\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000437
438 ret = smsc75xx_write_reg(dev, DP_CMD, DP_CMD_WRITE);
Joe Perches1e1d7412012-11-24 01:27:49 +0000439 check_warn_goto_done(ret, "Error writing DP_CMD\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000440
441 ret = smsc75xx_dataport_wait_not_busy(dev);
Joe Perches1e1d7412012-11-24 01:27:49 +0000442 check_warn_goto_done(ret, "smsc75xx_dataport_write timeout\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000443 }
444
445done:
446 mutex_unlock(&pdata->dataport_mutex);
447 return ret;
448}
449
450/* returns hash bit number for given MAC address */
451static u32 smsc75xx_hash(char addr[ETH_ALEN])
452{
453 return (ether_crc(ETH_ALEN, addr) >> 23) & 0x1ff;
454}
455
456static void smsc75xx_deferred_multicast_write(struct work_struct *param)
457{
458 struct smsc75xx_priv *pdata =
459 container_of(param, struct smsc75xx_priv, set_multicast);
460 struct usbnet *dev = pdata->dev;
461 int ret;
462
Joe Perches1e1d7412012-11-24 01:27:49 +0000463 netif_dbg(dev, drv, dev->net, "deferred multicast write 0x%08x\n",
464 pdata->rfe_ctl);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000465
466 smsc75xx_dataport_write(dev, DP_SEL_VHF, DP_SEL_VHF_VLAN_LEN,
467 DP_SEL_VHF_HASH_LEN, pdata->multicast_hash_table);
468
469 ret = smsc75xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
Joe Perches1e1d7412012-11-24 01:27:49 +0000470 check_warn(ret, "Error writing RFE_CRL\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000471}
472
473static void smsc75xx_set_multicast(struct net_device *netdev)
474{
475 struct usbnet *dev = netdev_priv(netdev);
476 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
477 unsigned long flags;
478 int i;
479
480 spin_lock_irqsave(&pdata->rfe_ctl_lock, flags);
481
482 pdata->rfe_ctl &=
483 ~(RFE_CTL_AU | RFE_CTL_AM | RFE_CTL_DPF | RFE_CTL_MHF);
484 pdata->rfe_ctl |= RFE_CTL_AB;
485
486 for (i = 0; i < DP_SEL_VHF_HASH_LEN; i++)
487 pdata->multicast_hash_table[i] = 0;
488
489 if (dev->net->flags & IFF_PROMISC) {
Joe Perches1e1d7412012-11-24 01:27:49 +0000490 netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000491 pdata->rfe_ctl |= RFE_CTL_AM | RFE_CTL_AU;
492 } else if (dev->net->flags & IFF_ALLMULTI) {
Joe Perches1e1d7412012-11-24 01:27:49 +0000493 netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000494 pdata->rfe_ctl |= RFE_CTL_AM | RFE_CTL_DPF;
495 } else if (!netdev_mc_empty(dev->net)) {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000496 struct netdev_hw_addr *ha;
Steve Glendinningd0cad872010-03-16 08:46:46 +0000497
Joe Perches1e1d7412012-11-24 01:27:49 +0000498 netif_dbg(dev, drv, dev->net, "receive multicast hash filter\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000499
500 pdata->rfe_ctl |= RFE_CTL_MHF | RFE_CTL_DPF;
501
Jiri Pirko22bedad32010-04-01 21:22:57 +0000502 netdev_for_each_mc_addr(ha, netdev) {
503 u32 bitnum = smsc75xx_hash(ha->addr);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000504 pdata->multicast_hash_table[bitnum / 32] |=
505 (1 << (bitnum % 32));
506 }
507 } else {
Joe Perches1e1d7412012-11-24 01:27:49 +0000508 netif_dbg(dev, drv, dev->net, "receive own packets only\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000509 pdata->rfe_ctl |= RFE_CTL_DPF;
510 }
511
512 spin_unlock_irqrestore(&pdata->rfe_ctl_lock, flags);
513
514 /* defer register writes to a sleepable context */
515 schedule_work(&pdata->set_multicast);
516}
517
518static int smsc75xx_update_flowcontrol(struct usbnet *dev, u8 duplex,
519 u16 lcladv, u16 rmtadv)
520{
521 u32 flow = 0, fct_flow = 0;
522 int ret;
523
524 if (duplex == DUPLEX_FULL) {
525 u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
526
527 if (cap & FLOW_CTRL_TX) {
528 flow = (FLOW_TX_FCEN | 0xFFFF);
529 /* set fct_flow thresholds to 20% and 80% */
530 fct_flow = (8 << 8) | 32;
531 }
532
533 if (cap & FLOW_CTRL_RX)
534 flow |= FLOW_RX_FCEN;
535
Joe Perches1e1d7412012-11-24 01:27:49 +0000536 netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
537 (cap & FLOW_CTRL_RX ? "enabled" : "disabled"),
538 (cap & FLOW_CTRL_TX ? "enabled" : "disabled"));
Steve Glendinningd0cad872010-03-16 08:46:46 +0000539 } else {
Joe Perches1e1d7412012-11-24 01:27:49 +0000540 netif_dbg(dev, link, dev->net, "half duplex\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000541 }
542
543 ret = smsc75xx_write_reg(dev, FLOW, flow);
Joe Perches1e1d7412012-11-24 01:27:49 +0000544 check_warn_return(ret, "Error writing FLOW\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000545
546 ret = smsc75xx_write_reg(dev, FCT_FLOW, fct_flow);
Joe Perches1e1d7412012-11-24 01:27:49 +0000547 check_warn_return(ret, "Error writing FCT_FLOW\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000548
549 return 0;
550}
551
552static int smsc75xx_link_reset(struct usbnet *dev)
553{
554 struct mii_if_info *mii = &dev->mii;
David Decotigny8ae6dac2011-04-27 18:32:38 +0000555 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
Steve Glendinningd0cad872010-03-16 08:46:46 +0000556 u16 lcladv, rmtadv;
557 int ret;
558
Steve Glendinning4f94a922012-05-04 00:57:12 +0000559 /* write to clear phy interrupt status */
Steve Glendinning77496222012-05-04 00:57:11 +0000560 smsc75xx_mdio_write(dev->net, mii->phy_id, PHY_INT_SRC,
561 PHY_INT_SRC_CLEAR_ALL);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000562
563 ret = smsc75xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL);
Joe Perches1e1d7412012-11-24 01:27:49 +0000564 check_warn_return(ret, "Error writing INT_STS\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000565
566 mii_check_media(mii, 1, 1);
567 mii_ethtool_gset(&dev->mii, &ecmd);
568 lcladv = smsc75xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
569 rmtadv = smsc75xx_mdio_read(dev->net, mii->phy_id, MII_LPA);
570
Joe Perches1e1d7412012-11-24 01:27:49 +0000571 netif_dbg(dev, link, dev->net, "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
572 ethtool_cmd_speed(&ecmd), ecmd.duplex, lcladv, rmtadv);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000573
574 return smsc75xx_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
575}
576
577static void smsc75xx_status(struct usbnet *dev, struct urb *urb)
578{
579 u32 intdata;
580
581 if (urb->actual_length != 4) {
Joe Perches1e1d7412012-11-24 01:27:49 +0000582 netdev_warn(dev->net, "unexpected urb length %d\n",
583 urb->actual_length);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000584 return;
585 }
586
587 memcpy(&intdata, urb->transfer_buffer, 4);
588 le32_to_cpus(&intdata);
589
Joe Perches1e1d7412012-11-24 01:27:49 +0000590 netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000591
592 if (intdata & INT_ENP_PHY_INT)
593 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
594 else
Joe Perches1e1d7412012-11-24 01:27:49 +0000595 netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
596 intdata);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000597}
598
Steve Glendinningd0cad872010-03-16 08:46:46 +0000599static int smsc75xx_ethtool_get_eeprom_len(struct net_device *net)
600{
601 return MAX_EEPROM_SIZE;
602}
603
604static int smsc75xx_ethtool_get_eeprom(struct net_device *netdev,
605 struct ethtool_eeprom *ee, u8 *data)
606{
607 struct usbnet *dev = netdev_priv(netdev);
608
609 ee->magic = LAN75XX_EEPROM_MAGIC;
610
611 return smsc75xx_read_eeprom(dev, ee->offset, ee->len, data);
612}
613
614static int smsc75xx_ethtool_set_eeprom(struct net_device *netdev,
615 struct ethtool_eeprom *ee, u8 *data)
616{
617 struct usbnet *dev = netdev_priv(netdev);
618
619 if (ee->magic != LAN75XX_EEPROM_MAGIC) {
Joe Perches1e1d7412012-11-24 01:27:49 +0000620 netdev_warn(dev->net, "EEPROM: magic value mismatch: 0x%x\n",
621 ee->magic);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000622 return -EINVAL;
623 }
624
625 return smsc75xx_write_eeprom(dev, ee->offset, ee->len, data);
626}
627
Steve Glendinning6c636502012-09-28 00:57:53 +0000628static void smsc75xx_ethtool_get_wol(struct net_device *net,
629 struct ethtool_wolinfo *wolinfo)
630{
631 struct usbnet *dev = netdev_priv(net);
632 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
633
634 wolinfo->supported = SUPPORTED_WAKE;
635 wolinfo->wolopts = pdata->wolopts;
636}
637
638static int smsc75xx_ethtool_set_wol(struct net_device *net,
639 struct ethtool_wolinfo *wolinfo)
640{
641 struct usbnet *dev = netdev_priv(net);
642 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
643
644 pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
645 return 0;
646}
647
Steve Glendinningd0cad872010-03-16 08:46:46 +0000648static const struct ethtool_ops smsc75xx_ethtool_ops = {
649 .get_link = usbnet_get_link,
650 .nway_reset = usbnet_nway_reset,
651 .get_drvinfo = usbnet_get_drvinfo,
652 .get_msglevel = usbnet_get_msglevel,
653 .set_msglevel = usbnet_set_msglevel,
654 .get_settings = usbnet_get_settings,
655 .set_settings = usbnet_set_settings,
656 .get_eeprom_len = smsc75xx_ethtool_get_eeprom_len,
657 .get_eeprom = smsc75xx_ethtool_get_eeprom,
658 .set_eeprom = smsc75xx_ethtool_set_eeprom,
Steve Glendinning6c636502012-09-28 00:57:53 +0000659 .get_wol = smsc75xx_ethtool_get_wol,
660 .set_wol = smsc75xx_ethtool_set_wol,
Steve Glendinningd0cad872010-03-16 08:46:46 +0000661};
662
663static int smsc75xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
664{
665 struct usbnet *dev = netdev_priv(netdev);
666
667 if (!netif_running(netdev))
668 return -EINVAL;
669
670 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
671}
672
673static void smsc75xx_init_mac_address(struct usbnet *dev)
674{
675 /* try reading mac address from EEPROM */
676 if (smsc75xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
677 dev->net->dev_addr) == 0) {
678 if (is_valid_ether_addr(dev->net->dev_addr)) {
679 /* eeprom values are valid so use them */
680 netif_dbg(dev, ifup, dev->net,
Joe Perches1e1d7412012-11-24 01:27:49 +0000681 "MAC address read from EEPROM\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000682 return;
683 }
684 }
685
686 /* no eeprom, or eeprom values are invalid. generate random MAC */
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000687 eth_hw_addr_random(dev->net);
Joe Perches1e1d7412012-11-24 01:27:49 +0000688 netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000689}
690
691static int smsc75xx_set_mac_address(struct usbnet *dev)
692{
693 u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
694 dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
695 u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
696
697 int ret = smsc75xx_write_reg(dev, RX_ADDRH, addr_hi);
Joe Perches1e1d7412012-11-24 01:27:49 +0000698 check_warn_return(ret, "Failed to write RX_ADDRH: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000699
700 ret = smsc75xx_write_reg(dev, RX_ADDRL, addr_lo);
Joe Perches1e1d7412012-11-24 01:27:49 +0000701 check_warn_return(ret, "Failed to write RX_ADDRL: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000702
703 addr_hi |= ADDR_FILTX_FB_VALID;
704 ret = smsc75xx_write_reg(dev, ADDR_FILTX, addr_hi);
Joe Perches1e1d7412012-11-24 01:27:49 +0000705 check_warn_return(ret, "Failed to write ADDR_FILTX: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000706
707 ret = smsc75xx_write_reg(dev, ADDR_FILTX + 4, addr_lo);
Joe Perches1e1d7412012-11-24 01:27:49 +0000708 check_warn_return(ret, "Failed to write ADDR_FILTX+4: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000709
710 return 0;
711}
712
713static int smsc75xx_phy_initialize(struct usbnet *dev)
714{
Steve Glendinningb1405042012-04-30 07:56:54 +0000715 int bmcr, ret, timeout = 0;
Steve Glendinningd0cad872010-03-16 08:46:46 +0000716
717 /* Initialize MII structure */
718 dev->mii.dev = dev->net;
719 dev->mii.mdio_read = smsc75xx_mdio_read;
720 dev->mii.mdio_write = smsc75xx_mdio_write;
721 dev->mii.phy_id_mask = 0x1f;
722 dev->mii.reg_num_mask = 0x1f;
Steve Glendinningc0b92e42012-04-30 07:56:55 +0000723 dev->mii.supports_gmii = 1;
Steve Glendinningd0cad872010-03-16 08:46:46 +0000724 dev->mii.phy_id = SMSC75XX_INTERNAL_PHY_ID;
725
726 /* reset phy and wait for reset to complete */
727 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
728
729 do {
730 msleep(10);
731 bmcr = smsc75xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
Joe Perches1e1d7412012-11-24 01:27:49 +0000732 check_warn_return(bmcr, "Error reading MII_BMCR\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000733 timeout++;
Steve Glendinning8a1d59d2012-04-30 07:56:53 +0000734 } while ((bmcr & BMCR_RESET) && (timeout < 100));
Steve Glendinningd0cad872010-03-16 08:46:46 +0000735
736 if (timeout >= 100) {
Joe Perches1e1d7412012-11-24 01:27:49 +0000737 netdev_warn(dev->net, "timeout on PHY Reset\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000738 return -EIO;
739 }
740
741 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
742 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
743 ADVERTISE_PAUSE_ASYM);
Steve Glendinningc0b92e42012-04-30 07:56:55 +0000744 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, MII_CTRL1000,
745 ADVERTISE_1000FULL);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000746
Steve Glendinningb1405042012-04-30 07:56:54 +0000747 /* read and write to clear phy interrupt status */
748 ret = smsc75xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
Joe Perches1e1d7412012-11-24 01:27:49 +0000749 check_warn_return(ret, "Error reading PHY_INT_SRC\n");
Steve Glendinningb1405042012-04-30 07:56:54 +0000750 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_SRC, 0xffff);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000751
752 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
753 PHY_INT_MASK_DEFAULT);
754 mii_nway_restart(&dev->mii);
755
Joe Perches1e1d7412012-11-24 01:27:49 +0000756 netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000757 return 0;
758}
759
760static int smsc75xx_set_rx_max_frame_length(struct usbnet *dev, int size)
761{
762 int ret = 0;
763 u32 buf;
764 bool rxenabled;
765
766 ret = smsc75xx_read_reg(dev, MAC_RX, &buf);
Joe Perches1e1d7412012-11-24 01:27:49 +0000767 check_warn_return(ret, "Failed to read MAC_RX: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000768
769 rxenabled = ((buf & MAC_RX_RXEN) != 0);
770
771 if (rxenabled) {
772 buf &= ~MAC_RX_RXEN;
773 ret = smsc75xx_write_reg(dev, MAC_RX, buf);
Joe Perches1e1d7412012-11-24 01:27:49 +0000774 check_warn_return(ret, "Failed to write MAC_RX: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000775 }
776
777 /* add 4 to size for FCS */
778 buf &= ~MAC_RX_MAX_SIZE;
779 buf |= (((size + 4) << MAC_RX_MAX_SIZE_SHIFT) & MAC_RX_MAX_SIZE);
780
781 ret = smsc75xx_write_reg(dev, MAC_RX, buf);
Joe Perches1e1d7412012-11-24 01:27:49 +0000782 check_warn_return(ret, "Failed to write MAC_RX: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000783
784 if (rxenabled) {
785 buf |= MAC_RX_RXEN;
786 ret = smsc75xx_write_reg(dev, MAC_RX, buf);
Joe Perches1e1d7412012-11-24 01:27:49 +0000787 check_warn_return(ret, "Failed to write MAC_RX: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000788 }
789
790 return 0;
791}
792
793static int smsc75xx_change_mtu(struct net_device *netdev, int new_mtu)
794{
795 struct usbnet *dev = netdev_priv(netdev);
796
797 int ret = smsc75xx_set_rx_max_frame_length(dev, new_mtu);
Joe Perches1e1d7412012-11-24 01:27:49 +0000798 check_warn_return(ret, "Failed to set mac rx frame length\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000799
800 return usbnet_change_mtu(netdev, new_mtu);
801}
802
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700803/* Enable or disable Rx checksum offload engine */
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000804static int smsc75xx_set_features(struct net_device *netdev,
805 netdev_features_t features)
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700806{
807 struct usbnet *dev = netdev_priv(netdev);
808 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
809 unsigned long flags;
810 int ret;
811
812 spin_lock_irqsave(&pdata->rfe_ctl_lock, flags);
813
814 if (features & NETIF_F_RXCSUM)
815 pdata->rfe_ctl |= RFE_CTL_TCPUDP_CKM | RFE_CTL_IP_CKM;
816 else
817 pdata->rfe_ctl &= ~(RFE_CTL_TCPUDP_CKM | RFE_CTL_IP_CKM);
818
819 spin_unlock_irqrestore(&pdata->rfe_ctl_lock, flags);
820 /* it's racing here! */
821
822 ret = smsc75xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
Joe Perches1e1d7412012-11-24 01:27:49 +0000823 check_warn_return(ret, "Error writing RFE_CTL\n");
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700824
825 return 0;
826}
827
Ming Lei47bbea42012-11-06 04:53:05 +0000828static int smsc75xx_wait_ready(struct usbnet *dev, int in_pm)
Steve Glendinning8762cec2012-09-28 00:57:51 +0000829{
830 int timeout = 0;
831
832 do {
833 u32 buf;
Ming Lei47bbea42012-11-06 04:53:05 +0000834 int ret;
835
836 ret = __smsc75xx_read_reg(dev, PMT_CTL, &buf, in_pm);
837
Joe Perches1e1d7412012-11-24 01:27:49 +0000838 check_warn_return(ret, "Failed to read PMT_CTL: %d\n", ret);
Steve Glendinning8762cec2012-09-28 00:57:51 +0000839
840 if (buf & PMT_CTL_DEV_RDY)
841 return 0;
842
843 msleep(10);
844 timeout++;
845 } while (timeout < 100);
846
Joe Perches1e1d7412012-11-24 01:27:49 +0000847 netdev_warn(dev->net, "timeout waiting for device ready\n");
Steve Glendinning8762cec2012-09-28 00:57:51 +0000848 return -EIO;
849}
850
Steve Glendinningd0cad872010-03-16 08:46:46 +0000851static int smsc75xx_reset(struct usbnet *dev)
852{
853 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
854 u32 buf;
855 int ret = 0, timeout;
856
Joe Perches1e1d7412012-11-24 01:27:49 +0000857 netif_dbg(dev, ifup, dev->net, "entering smsc75xx_reset\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000858
Ming Lei47bbea42012-11-06 04:53:05 +0000859 ret = smsc75xx_wait_ready(dev, 0);
Joe Perches1e1d7412012-11-24 01:27:49 +0000860 check_warn_return(ret, "device not ready in smsc75xx_reset\n");
Steve Glendinning8762cec2012-09-28 00:57:51 +0000861
Steve Glendinningd0cad872010-03-16 08:46:46 +0000862 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
Joe Perches1e1d7412012-11-24 01:27:49 +0000863 check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000864
865 buf |= HW_CFG_LRST;
866
867 ret = smsc75xx_write_reg(dev, HW_CFG, buf);
Joe Perches1e1d7412012-11-24 01:27:49 +0000868 check_warn_return(ret, "Failed to write HW_CFG: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000869
870 timeout = 0;
871 do {
872 msleep(10);
873 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
Joe Perches1e1d7412012-11-24 01:27:49 +0000874 check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000875 timeout++;
876 } while ((buf & HW_CFG_LRST) && (timeout < 100));
877
878 if (timeout >= 100) {
Joe Perches1e1d7412012-11-24 01:27:49 +0000879 netdev_warn(dev->net, "timeout on completion of Lite Reset\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000880 return -EIO;
881 }
882
Joe Perches1e1d7412012-11-24 01:27:49 +0000883 netif_dbg(dev, ifup, dev->net, "Lite reset complete, resetting PHY\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000884
885 ret = smsc75xx_read_reg(dev, PMT_CTL, &buf);
Joe Perches1e1d7412012-11-24 01:27:49 +0000886 check_warn_return(ret, "Failed to read PMT_CTL: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000887
888 buf |= PMT_CTL_PHY_RST;
889
890 ret = smsc75xx_write_reg(dev, PMT_CTL, buf);
Joe Perches1e1d7412012-11-24 01:27:49 +0000891 check_warn_return(ret, "Failed to write PMT_CTL: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000892
893 timeout = 0;
894 do {
895 msleep(10);
896 ret = smsc75xx_read_reg(dev, PMT_CTL, &buf);
Joe Perches1e1d7412012-11-24 01:27:49 +0000897 check_warn_return(ret, "Failed to read PMT_CTL: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000898 timeout++;
899 } while ((buf & PMT_CTL_PHY_RST) && (timeout < 100));
900
901 if (timeout >= 100) {
Joe Perches1e1d7412012-11-24 01:27:49 +0000902 netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000903 return -EIO;
904 }
905
Joe Perches1e1d7412012-11-24 01:27:49 +0000906 netif_dbg(dev, ifup, dev->net, "PHY reset complete\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000907
908 smsc75xx_init_mac_address(dev);
909
910 ret = smsc75xx_set_mac_address(dev);
Joe Perches1e1d7412012-11-24 01:27:49 +0000911 check_warn_return(ret, "Failed to set mac address\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000912
Joe Perches1e1d7412012-11-24 01:27:49 +0000913 netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n",
914 dev->net->dev_addr);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000915
916 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
Joe Perches1e1d7412012-11-24 01:27:49 +0000917 check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000918
Joe Perches1e1d7412012-11-24 01:27:49 +0000919 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n",
920 buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000921
922 buf |= HW_CFG_BIR;
923
924 ret = smsc75xx_write_reg(dev, HW_CFG, buf);
Joe Perches1e1d7412012-11-24 01:27:49 +0000925 check_warn_return(ret, "Failed to write HW_CFG: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000926
927 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
Joe Perches1e1d7412012-11-24 01:27:49 +0000928 check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000929
Joe Perches1e1d7412012-11-24 01:27:49 +0000930 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG after writing HW_CFG_BIR: 0x%08x\n",
931 buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000932
933 if (!turbo_mode) {
934 buf = 0;
935 dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
936 } else if (dev->udev->speed == USB_SPEED_HIGH) {
937 buf = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
938 dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
939 } else {
940 buf = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
941 dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
942 }
943
Joe Perches1e1d7412012-11-24 01:27:49 +0000944 netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n",
945 (ulong)dev->rx_urb_size);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000946
947 ret = smsc75xx_write_reg(dev, BURST_CAP, buf);
Joe Perches1e1d7412012-11-24 01:27:49 +0000948 check_warn_return(ret, "Failed to write BURST_CAP: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000949
950 ret = smsc75xx_read_reg(dev, BURST_CAP, &buf);
Joe Perches1e1d7412012-11-24 01:27:49 +0000951 check_warn_return(ret, "Failed to read BURST_CAP: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000952
953 netif_dbg(dev, ifup, dev->net,
Joe Perches1e1d7412012-11-24 01:27:49 +0000954 "Read Value from BURST_CAP after writing: 0x%08x\n", buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000955
956 ret = smsc75xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
Joe Perches1e1d7412012-11-24 01:27:49 +0000957 check_warn_return(ret, "Failed to write BULK_IN_DLY: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000958
959 ret = smsc75xx_read_reg(dev, BULK_IN_DLY, &buf);
Joe Perches1e1d7412012-11-24 01:27:49 +0000960 check_warn_return(ret, "Failed to read BULK_IN_DLY: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000961
962 netif_dbg(dev, ifup, dev->net,
Joe Perches1e1d7412012-11-24 01:27:49 +0000963 "Read Value from BULK_IN_DLY after writing: 0x%08x\n", buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000964
965 if (turbo_mode) {
966 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
Joe Perches1e1d7412012-11-24 01:27:49 +0000967 check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000968
Joe Perches1e1d7412012-11-24 01:27:49 +0000969 netif_dbg(dev, ifup, dev->net, "HW_CFG: 0x%08x\n", buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000970
971 buf |= (HW_CFG_MEF | HW_CFG_BCE);
972
973 ret = smsc75xx_write_reg(dev, HW_CFG, buf);
Joe Perches1e1d7412012-11-24 01:27:49 +0000974 check_warn_return(ret, "Failed to write HW_CFG: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000975
976 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
Joe Perches1e1d7412012-11-24 01:27:49 +0000977 check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000978
Joe Perches1e1d7412012-11-24 01:27:49 +0000979 netif_dbg(dev, ifup, dev->net, "HW_CFG: 0x%08x\n", buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000980 }
981
982 /* set FIFO sizes */
983 buf = (MAX_RX_FIFO_SIZE - 512) / 512;
984 ret = smsc75xx_write_reg(dev, FCT_RX_FIFO_END, buf);
Joe Perches1e1d7412012-11-24 01:27:49 +0000985 check_warn_return(ret, "Failed to write FCT_RX_FIFO_END: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000986
Joe Perches1e1d7412012-11-24 01:27:49 +0000987 netif_dbg(dev, ifup, dev->net, "FCT_RX_FIFO_END set to 0x%08x\n", buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000988
989 buf = (MAX_TX_FIFO_SIZE - 512) / 512;
990 ret = smsc75xx_write_reg(dev, FCT_TX_FIFO_END, buf);
Joe Perches1e1d7412012-11-24 01:27:49 +0000991 check_warn_return(ret, "Failed to write FCT_TX_FIFO_END: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000992
Joe Perches1e1d7412012-11-24 01:27:49 +0000993 netif_dbg(dev, ifup, dev->net, "FCT_TX_FIFO_END set to 0x%08x\n", buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000994
995 ret = smsc75xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL);
Joe Perches1e1d7412012-11-24 01:27:49 +0000996 check_warn_return(ret, "Failed to write INT_STS: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000997
998 ret = smsc75xx_read_reg(dev, ID_REV, &buf);
Joe Perches1e1d7412012-11-24 01:27:49 +0000999 check_warn_return(ret, "Failed to read ID_REV: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001000
Joe Perches1e1d7412012-11-24 01:27:49 +00001001 netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001002
Steve Glendinning97138a12012-05-04 00:57:13 +00001003 ret = smsc75xx_read_reg(dev, E2P_CMD, &buf);
Joe Perches1e1d7412012-11-24 01:27:49 +00001004 check_warn_return(ret, "Failed to read E2P_CMD: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001005
Steve Glendinning97138a12012-05-04 00:57:13 +00001006 /* only set default GPIO/LED settings if no EEPROM is detected */
1007 if (!(buf & E2P_CMD_LOADED)) {
1008 ret = smsc75xx_read_reg(dev, LED_GPIO_CFG, &buf);
Joe Perches1e1d7412012-11-24 01:27:49 +00001009 check_warn_return(ret, "Failed to read LED_GPIO_CFG: %d\n",
1010 ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001011
Steve Glendinning97138a12012-05-04 00:57:13 +00001012 buf &= ~(LED_GPIO_CFG_LED2_FUN_SEL | LED_GPIO_CFG_LED10_FUN_SEL);
1013 buf |= LED_GPIO_CFG_LEDGPIO_EN | LED_GPIO_CFG_LED2_FUN_SEL;
1014
1015 ret = smsc75xx_write_reg(dev, LED_GPIO_CFG, buf);
Joe Perches1e1d7412012-11-24 01:27:49 +00001016 check_warn_return(ret, "Failed to write LED_GPIO_CFG: %d\n",
1017 ret);
Steve Glendinning97138a12012-05-04 00:57:13 +00001018 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001019
1020 ret = smsc75xx_write_reg(dev, FLOW, 0);
Joe Perches1e1d7412012-11-24 01:27:49 +00001021 check_warn_return(ret, "Failed to write FLOW: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001022
1023 ret = smsc75xx_write_reg(dev, FCT_FLOW, 0);
Joe Perches1e1d7412012-11-24 01:27:49 +00001024 check_warn_return(ret, "Failed to write FCT_FLOW: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001025
1026 /* Don't need rfe_ctl_lock during initialisation */
1027 ret = smsc75xx_read_reg(dev, RFE_CTL, &pdata->rfe_ctl);
Joe Perches1e1d7412012-11-24 01:27:49 +00001028 check_warn_return(ret, "Failed to read RFE_CTL: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001029
1030 pdata->rfe_ctl |= RFE_CTL_AB | RFE_CTL_DPF;
1031
1032 ret = smsc75xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
Joe Perches1e1d7412012-11-24 01:27:49 +00001033 check_warn_return(ret, "Failed to write RFE_CTL: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001034
1035 ret = smsc75xx_read_reg(dev, RFE_CTL, &pdata->rfe_ctl);
Joe Perches1e1d7412012-11-24 01:27:49 +00001036 check_warn_return(ret, "Failed to read RFE_CTL: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001037
Joe Perches1e1d7412012-11-24 01:27:49 +00001038 netif_dbg(dev, ifup, dev->net, "RFE_CTL set to 0x%08x\n",
1039 pdata->rfe_ctl);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001040
1041 /* Enable or disable checksum offload engines */
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001042 smsc75xx_set_features(dev->net, dev->net->features);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001043
1044 smsc75xx_set_multicast(dev->net);
1045
1046 ret = smsc75xx_phy_initialize(dev);
Joe Perches1e1d7412012-11-24 01:27:49 +00001047 check_warn_return(ret, "Failed to initialize PHY: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001048
1049 ret = smsc75xx_read_reg(dev, INT_EP_CTL, &buf);
Joe Perches1e1d7412012-11-24 01:27:49 +00001050 check_warn_return(ret, "Failed to read INT_EP_CTL: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001051
1052 /* enable PHY interrupts */
1053 buf |= INT_ENP_PHY_INT;
1054
1055 ret = smsc75xx_write_reg(dev, INT_EP_CTL, buf);
Joe Perches1e1d7412012-11-24 01:27:49 +00001056 check_warn_return(ret, "Failed to write INT_EP_CTL: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001057
Steve Glendinning2f3a0812012-04-30 07:56:56 +00001058 /* allow mac to detect speed and duplex from phy */
1059 ret = smsc75xx_read_reg(dev, MAC_CR, &buf);
Joe Perches1e1d7412012-11-24 01:27:49 +00001060 check_warn_return(ret, "Failed to read MAC_CR: %d\n", ret);
Steve Glendinning2f3a0812012-04-30 07:56:56 +00001061
1062 buf |= (MAC_CR_ADD | MAC_CR_ASD);
1063 ret = smsc75xx_write_reg(dev, MAC_CR, buf);
Joe Perches1e1d7412012-11-24 01:27:49 +00001064 check_warn_return(ret, "Failed to write MAC_CR: %d\n", ret);
Steve Glendinning2f3a0812012-04-30 07:56:56 +00001065
Steve Glendinningd0cad872010-03-16 08:46:46 +00001066 ret = smsc75xx_read_reg(dev, MAC_TX, &buf);
Joe Perches1e1d7412012-11-24 01:27:49 +00001067 check_warn_return(ret, "Failed to read MAC_TX: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001068
1069 buf |= MAC_TX_TXEN;
1070
1071 ret = smsc75xx_write_reg(dev, MAC_TX, buf);
Joe Perches1e1d7412012-11-24 01:27:49 +00001072 check_warn_return(ret, "Failed to write MAC_TX: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001073
Joe Perches1e1d7412012-11-24 01:27:49 +00001074 netif_dbg(dev, ifup, dev->net, "MAC_TX set to 0x%08x\n", buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001075
1076 ret = smsc75xx_read_reg(dev, FCT_TX_CTL, &buf);
Joe Perches1e1d7412012-11-24 01:27:49 +00001077 check_warn_return(ret, "Failed to read FCT_TX_CTL: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001078
1079 buf |= FCT_TX_CTL_EN;
1080
1081 ret = smsc75xx_write_reg(dev, FCT_TX_CTL, buf);
Joe Perches1e1d7412012-11-24 01:27:49 +00001082 check_warn_return(ret, "Failed to write FCT_TX_CTL: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001083
Joe Perches1e1d7412012-11-24 01:27:49 +00001084 netif_dbg(dev, ifup, dev->net, "FCT_TX_CTL set to 0x%08x\n", buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001085
1086 ret = smsc75xx_set_rx_max_frame_length(dev, 1514);
Joe Perches1e1d7412012-11-24 01:27:49 +00001087 check_warn_return(ret, "Failed to set max rx frame length\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +00001088
1089 ret = smsc75xx_read_reg(dev, MAC_RX, &buf);
Joe Perches1e1d7412012-11-24 01:27:49 +00001090 check_warn_return(ret, "Failed to read MAC_RX: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001091
1092 buf |= MAC_RX_RXEN;
1093
1094 ret = smsc75xx_write_reg(dev, MAC_RX, buf);
Joe Perches1e1d7412012-11-24 01:27:49 +00001095 check_warn_return(ret, "Failed to write MAC_RX: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001096
Joe Perches1e1d7412012-11-24 01:27:49 +00001097 netif_dbg(dev, ifup, dev->net, "MAC_RX set to 0x%08x\n", buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001098
1099 ret = smsc75xx_read_reg(dev, FCT_RX_CTL, &buf);
Joe Perches1e1d7412012-11-24 01:27:49 +00001100 check_warn_return(ret, "Failed to read FCT_RX_CTL: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001101
1102 buf |= FCT_RX_CTL_EN;
1103
1104 ret = smsc75xx_write_reg(dev, FCT_RX_CTL, buf);
Joe Perches1e1d7412012-11-24 01:27:49 +00001105 check_warn_return(ret, "Failed to write FCT_RX_CTL: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001106
Joe Perches1e1d7412012-11-24 01:27:49 +00001107 netif_dbg(dev, ifup, dev->net, "FCT_RX_CTL set to 0x%08x\n", buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001108
Joe Perches1e1d7412012-11-24 01:27:49 +00001109 netif_dbg(dev, ifup, dev->net, "smsc75xx_reset, return 0\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +00001110 return 0;
1111}
1112
1113static const struct net_device_ops smsc75xx_netdev_ops = {
1114 .ndo_open = usbnet_open,
1115 .ndo_stop = usbnet_stop,
1116 .ndo_start_xmit = usbnet_start_xmit,
1117 .ndo_tx_timeout = usbnet_tx_timeout,
1118 .ndo_change_mtu = smsc75xx_change_mtu,
1119 .ndo_set_mac_address = eth_mac_addr,
1120 .ndo_validate_addr = eth_validate_addr,
1121 .ndo_do_ioctl = smsc75xx_ioctl,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001122 .ndo_set_rx_mode = smsc75xx_set_multicast,
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001123 .ndo_set_features = smsc75xx_set_features,
Steve Glendinningd0cad872010-03-16 08:46:46 +00001124};
1125
1126static int smsc75xx_bind(struct usbnet *dev, struct usb_interface *intf)
1127{
1128 struct smsc75xx_priv *pdata = NULL;
1129 int ret;
1130
1131 printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
1132
1133 ret = usbnet_get_endpoints(dev, intf);
Joe Perches1e1d7412012-11-24 01:27:49 +00001134 check_warn_return(ret, "usbnet_get_endpoints failed: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001135
1136 dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc75xx_priv),
1137 GFP_KERNEL);
1138
1139 pdata = (struct smsc75xx_priv *)(dev->data[0]);
1140 if (!pdata) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001141 netdev_warn(dev->net, "Unable to allocate smsc75xx_priv\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +00001142 return -ENOMEM;
1143 }
1144
1145 pdata->dev = dev;
1146
1147 spin_lock_init(&pdata->rfe_ctl_lock);
1148 mutex_init(&pdata->dataport_mutex);
1149
1150 INIT_WORK(&pdata->set_multicast, smsc75xx_deferred_multicast_write);
1151
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001152 if (DEFAULT_TX_CSUM_ENABLE) {
1153 dev->net->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1154 if (DEFAULT_TSO_ENABLE)
1155 dev->net->features |= NETIF_F_SG |
1156 NETIF_F_TSO | NETIF_F_TSO6;
1157 }
1158 if (DEFAULT_RX_CSUM_ENABLE)
1159 dev->net->features |= NETIF_F_RXCSUM;
Steve Glendinningd0cad872010-03-16 08:46:46 +00001160
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001161 dev->net->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1162 NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_RXCSUM;
Steve Glendinningd0cad872010-03-16 08:46:46 +00001163
1164 /* Init all registers */
1165 ret = smsc75xx_reset(dev);
Steve Glendinning33763b72012-11-28 05:59:45 +00001166 check_warn_return(ret, "smsc75xx_reset error %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001167
1168 dev->net->netdev_ops = &smsc75xx_netdev_ops;
1169 dev->net->ethtool_ops = &smsc75xx_ethtool_ops;
1170 dev->net->flags |= IFF_MULTICAST;
1171 dev->net->hard_header_len += SMSC75XX_TX_OVERHEAD;
Stephane Filloda99ff7d2012-04-15 11:38:29 +00001172 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
Steve Glendinningd0cad872010-03-16 08:46:46 +00001173 return 0;
1174}
1175
1176static void smsc75xx_unbind(struct usbnet *dev, struct usb_interface *intf)
1177{
1178 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1179 if (pdata) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001180 netif_dbg(dev, ifdown, dev->net, "free pdata\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +00001181 kfree(pdata);
1182 pdata = NULL;
1183 dev->data[0] = 0;
1184 }
1185}
1186
Steve Glendinning899a3912012-10-30 07:46:32 +00001187static u16 smsc_crc(const u8 *buffer, size_t len)
1188{
1189 return bitrev16(crc16(0xFFFF, buffer, len));
1190}
1191
1192static int smsc75xx_write_wuff(struct usbnet *dev, int filter, u32 wuf_cfg,
1193 u32 wuf_mask1)
1194{
1195 int cfg_base = WUF_CFGX + filter * 4;
1196 int mask_base = WUF_MASKX + filter * 16;
1197 int ret;
1198
1199 ret = smsc75xx_write_reg(dev, cfg_base, wuf_cfg);
Joe Perches1e1d7412012-11-24 01:27:49 +00001200 check_warn_return(ret, "Error writing WUF_CFGX\n");
Steve Glendinning899a3912012-10-30 07:46:32 +00001201
1202 ret = smsc75xx_write_reg(dev, mask_base, wuf_mask1);
Joe Perches1e1d7412012-11-24 01:27:49 +00001203 check_warn_return(ret, "Error writing WUF_MASKX\n");
Steve Glendinning899a3912012-10-30 07:46:32 +00001204
1205 ret = smsc75xx_write_reg(dev, mask_base + 4, 0);
Joe Perches1e1d7412012-11-24 01:27:49 +00001206 check_warn_return(ret, "Error writing WUF_MASKX\n");
Steve Glendinning899a3912012-10-30 07:46:32 +00001207
1208 ret = smsc75xx_write_reg(dev, mask_base + 8, 0);
Joe Perches1e1d7412012-11-24 01:27:49 +00001209 check_warn_return(ret, "Error writing WUF_MASKX\n");
Steve Glendinning899a3912012-10-30 07:46:32 +00001210
1211 ret = smsc75xx_write_reg(dev, mask_base + 12, 0);
Joe Perches1e1d7412012-11-24 01:27:49 +00001212 check_warn_return(ret, "Error writing WUF_MASKX\n");
Steve Glendinning899a3912012-10-30 07:46:32 +00001213
1214 return 0;
1215}
1216
Steve Glendinning9deb2752012-11-28 05:59:46 +00001217static int smsc75xx_enter_suspend0(struct usbnet *dev)
1218{
1219 u32 val;
1220 int ret;
1221
1222 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1223 check_warn_return(ret, "Error reading PMT_CTL\n");
1224
1225 val &= (~(PMT_CTL_SUS_MODE | PMT_CTL_PHY_RST));
1226 val |= PMT_CTL_SUS_MODE_0 | PMT_CTL_WOL_EN | PMT_CTL_WUPS;
1227
1228 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1229 check_warn_return(ret, "Error writing PMT_CTL\n");
1230
1231 smsc75xx_set_feature(dev, USB_DEVICE_REMOTE_WAKEUP);
1232
1233 return 0;
1234}
1235
1236static int smsc75xx_enter_suspend2(struct usbnet *dev)
1237{
1238 u32 val;
1239 int ret;
1240
1241 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1242 check_warn_return(ret, "Error reading PMT_CTL\n");
1243
1244 val &= ~(PMT_CTL_SUS_MODE | PMT_CTL_WUPS | PMT_CTL_PHY_RST);
1245 val |= PMT_CTL_SUS_MODE_2;
1246
1247 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1248 check_warn_return(ret, "Error writing PMT_CTL\n");
1249
1250 return 0;
1251}
1252
Steve Glendinning16c79a02012-09-28 00:57:52 +00001253static int smsc75xx_suspend(struct usb_interface *intf, pm_message_t message)
1254{
1255 struct usbnet *dev = usb_get_intfdata(intf);
Steve Glendinning6c636502012-09-28 00:57:53 +00001256 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
Steve Glendinning16c79a02012-09-28 00:57:52 +00001257 int ret;
1258 u32 val;
1259
Steve Glendinning16c79a02012-09-28 00:57:52 +00001260 ret = usbnet_suspend(intf, message);
Joe Perches1e1d7412012-11-24 01:27:49 +00001261 check_warn_return(ret, "usbnet_suspend error\n");
Steve Glendinning16c79a02012-09-28 00:57:52 +00001262
Steve Glendinning6c636502012-09-28 00:57:53 +00001263 /* if no wol options set, enter lowest power SUSPEND2 mode */
1264 if (!(pdata->wolopts & SUPPORTED_WAKE)) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001265 netdev_info(dev->net, "entering SUSPEND2 mode\n");
Steve Glendinning6c636502012-09-28 00:57:53 +00001266
1267 /* disable energy detect (link up) & wake up events */
Ming Lei47bbea42012-11-06 04:53:05 +00001268 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001269 check_warn_return(ret, "Error reading WUCSR\n");
Steve Glendinning6c636502012-09-28 00:57:53 +00001270
1271 val &= ~(WUCSR_MPEN | WUCSR_WUEN);
1272
Ming Lei47bbea42012-11-06 04:53:05 +00001273 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001274 check_warn_return(ret, "Error writing WUCSR\n");
Steve Glendinning6c636502012-09-28 00:57:53 +00001275
Ming Lei47bbea42012-11-06 04:53:05 +00001276 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001277 check_warn_return(ret, "Error reading PMT_CTL\n");
Steve Glendinning6c636502012-09-28 00:57:53 +00001278
1279 val &= ~(PMT_CTL_ED_EN | PMT_CTL_WOL_EN);
1280
Ming Lei47bbea42012-11-06 04:53:05 +00001281 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001282 check_warn_return(ret, "Error writing PMT_CTL\n");
Steve Glendinning6c636502012-09-28 00:57:53 +00001283
Steve Glendinning9deb2752012-11-28 05:59:46 +00001284 return smsc75xx_enter_suspend2(dev);
Steve Glendinning6c636502012-09-28 00:57:53 +00001285 }
1286
Steve Glendinning899a3912012-10-30 07:46:32 +00001287 if (pdata->wolopts & (WAKE_MCAST | WAKE_ARP)) {
1288 int i, filter = 0;
1289
1290 /* disable all filters */
1291 for (i = 0; i < WUF_NUM; i++) {
Ming Lei47bbea42012-11-06 04:53:05 +00001292 ret = smsc75xx_write_reg_nopm(dev, WUF_CFGX + i * 4, 0);
Joe Perches1e1d7412012-11-24 01:27:49 +00001293 check_warn_return(ret, "Error writing WUF_CFGX\n");
Steve Glendinning899a3912012-10-30 07:46:32 +00001294 }
1295
1296 if (pdata->wolopts & WAKE_MCAST) {
1297 const u8 mcast[] = {0x01, 0x00, 0x5E};
Joe Perches1e1d7412012-11-24 01:27:49 +00001298 netdev_info(dev->net, "enabling multicast detection\n");
Steve Glendinning899a3912012-10-30 07:46:32 +00001299
1300 val = WUF_CFGX_EN | WUF_CFGX_ATYPE_MULTICAST
1301 | smsc_crc(mcast, 3);
1302 ret = smsc75xx_write_wuff(dev, filter++, val, 0x0007);
Joe Perches1e1d7412012-11-24 01:27:49 +00001303 check_warn_return(ret, "Error writing wakeup filter\n");
Steve Glendinning899a3912012-10-30 07:46:32 +00001304 }
1305
1306 if (pdata->wolopts & WAKE_ARP) {
1307 const u8 arp[] = {0x08, 0x06};
Joe Perches1e1d7412012-11-24 01:27:49 +00001308 netdev_info(dev->net, "enabling ARP detection\n");
Steve Glendinning899a3912012-10-30 07:46:32 +00001309
1310 val = WUF_CFGX_EN | WUF_CFGX_ATYPE_ALL | (0x0C << 16)
1311 | smsc_crc(arp, 2);
1312 ret = smsc75xx_write_wuff(dev, filter++, val, 0x0003);
Joe Perches1e1d7412012-11-24 01:27:49 +00001313 check_warn_return(ret, "Error writing wakeup filter\n");
Steve Glendinning899a3912012-10-30 07:46:32 +00001314 }
1315
1316 /* clear any pending pattern match packet status */
Ming Lei47bbea42012-11-06 04:53:05 +00001317 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001318 check_warn_return(ret, "Error reading WUCSR\n");
Steve Glendinning6c636502012-09-28 00:57:53 +00001319
Steve Glendinning899a3912012-10-30 07:46:32 +00001320 val |= WUCSR_WUFR;
1321
Ming Lei47bbea42012-11-06 04:53:05 +00001322 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001323 check_warn_return(ret, "Error writing WUCSR\n");
Steve Glendinning899a3912012-10-30 07:46:32 +00001324
Joe Perches1e1d7412012-11-24 01:27:49 +00001325 netdev_info(dev->net, "enabling packet match detection\n");
Ming Lei47bbea42012-11-06 04:53:05 +00001326 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001327 check_warn_return(ret, "Error reading WUCSR\n");
Steve Glendinning899a3912012-10-30 07:46:32 +00001328
1329 val |= WUCSR_WUEN;
1330
Ming Lei47bbea42012-11-06 04:53:05 +00001331 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001332 check_warn_return(ret, "Error writing WUCSR\n");
Steve Glendinning899a3912012-10-30 07:46:32 +00001333 } else {
Joe Perches1e1d7412012-11-24 01:27:49 +00001334 netdev_info(dev->net, "disabling packet match detection\n");
Ming Lei47bbea42012-11-06 04:53:05 +00001335 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001336 check_warn_return(ret, "Error reading WUCSR\n");
Steve Glendinning899a3912012-10-30 07:46:32 +00001337
1338 val &= ~WUCSR_WUEN;
Steve Glendinning6c636502012-09-28 00:57:53 +00001339
Ming Lei47bbea42012-11-06 04:53:05 +00001340 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001341 check_warn_return(ret, "Error writing WUCSR\n");
Steve Glendinning6c636502012-09-28 00:57:53 +00001342 }
1343
Steve Glendinning899a3912012-10-30 07:46:32 +00001344 /* disable magic, bcast & unicast wakeup sources */
Ming Lei47bbea42012-11-06 04:53:05 +00001345 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001346 check_warn_return(ret, "Error reading WUCSR\n");
Steve Glendinning6c636502012-09-28 00:57:53 +00001347
Steve Glendinning899a3912012-10-30 07:46:32 +00001348 val &= ~(WUCSR_MPEN | WUCSR_BCST_EN | WUCSR_PFDA_EN);
Steve Glendinning6c636502012-09-28 00:57:53 +00001349
Ming Lei47bbea42012-11-06 04:53:05 +00001350 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001351 check_warn_return(ret, "Error writing WUCSR\n");
Steve Glendinning6c636502012-09-28 00:57:53 +00001352
Steve Glendinning899a3912012-10-30 07:46:32 +00001353 if (pdata->wolopts & WAKE_MAGIC) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001354 netdev_info(dev->net, "enabling magic packet wakeup\n");
Ming Lei47bbea42012-11-06 04:53:05 +00001355 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001356 check_warn_return(ret, "Error reading WUCSR\n");
Steve Glendinning6c636502012-09-28 00:57:53 +00001357
Steve Glendinning899a3912012-10-30 07:46:32 +00001358 /* clear any pending magic packet status */
1359 val |= WUCSR_MPR | WUCSR_MPEN;
Steve Glendinning6c636502012-09-28 00:57:53 +00001360
Ming Lei47bbea42012-11-06 04:53:05 +00001361 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001362 check_warn_return(ret, "Error writing WUCSR\n");
Steve Glendinning899a3912012-10-30 07:46:32 +00001363 }
Steve Glendinning6c636502012-09-28 00:57:53 +00001364
Steve Glendinning899a3912012-10-30 07:46:32 +00001365 if (pdata->wolopts & WAKE_BCAST) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001366 netdev_info(dev->net, "enabling broadcast detection\n");
Ming Lei47bbea42012-11-06 04:53:05 +00001367 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001368 check_warn_return(ret, "Error reading WUCSR\n");
Steve Glendinning899a3912012-10-30 07:46:32 +00001369
1370 val |= WUCSR_BCAST_FR | WUCSR_BCST_EN;
1371
Ming Lei47bbea42012-11-06 04:53:05 +00001372 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001373 check_warn_return(ret, "Error writing WUCSR\n");
Steve Glendinning899a3912012-10-30 07:46:32 +00001374 }
1375
1376 if (pdata->wolopts & WAKE_UCAST) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001377 netdev_info(dev->net, "enabling unicast detection\n");
Ming Lei47bbea42012-11-06 04:53:05 +00001378 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001379 check_warn_return(ret, "Error reading WUCSR\n");
Steve Glendinning899a3912012-10-30 07:46:32 +00001380
1381 val |= WUCSR_WUFR | WUCSR_PFDA_EN;
1382
Ming Lei47bbea42012-11-06 04:53:05 +00001383 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001384 check_warn_return(ret, "Error writing WUCSR\n");
Steve Glendinning899a3912012-10-30 07:46:32 +00001385 }
1386
1387 /* enable receiver to enable frame reception */
Ming Lei47bbea42012-11-06 04:53:05 +00001388 ret = smsc75xx_read_reg_nopm(dev, MAC_RX, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001389 check_warn_return(ret, "Failed to read MAC_RX: %d\n", ret);
Steve Glendinning6c636502012-09-28 00:57:53 +00001390
1391 val |= MAC_RX_RXEN;
1392
Ming Lei47bbea42012-11-06 04:53:05 +00001393 ret = smsc75xx_write_reg_nopm(dev, MAC_RX, val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001394 check_warn_return(ret, "Failed to write MAC_RX: %d\n", ret);
Steve Glendinning6c636502012-09-28 00:57:53 +00001395
1396 /* some wol options are enabled, so enter SUSPEND0 */
Joe Perches1e1d7412012-11-24 01:27:49 +00001397 netdev_info(dev->net, "entering SUSPEND0 mode\n");
Steve Glendinning9deb2752012-11-28 05:59:46 +00001398 return smsc75xx_enter_suspend0(dev);
Steve Glendinning16c79a02012-09-28 00:57:52 +00001399}
1400
1401static int smsc75xx_resume(struct usb_interface *intf)
1402{
1403 struct usbnet *dev = usb_get_intfdata(intf);
Steve Glendinning6c636502012-09-28 00:57:53 +00001404 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
Steve Glendinning16c79a02012-09-28 00:57:52 +00001405 int ret;
1406 u32 val;
1407
Steve Glendinning899a3912012-10-30 07:46:32 +00001408 if (pdata->wolopts) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001409 netdev_info(dev->net, "resuming from SUSPEND0\n");
Steve Glendinning16c79a02012-09-28 00:57:52 +00001410
Steve Glendinning6c636502012-09-28 00:57:53 +00001411 smsc75xx_clear_feature(dev, USB_DEVICE_REMOTE_WAKEUP);
Steve Glendinning16c79a02012-09-28 00:57:52 +00001412
Steve Glendinning899a3912012-10-30 07:46:32 +00001413 /* Disable wakeup sources */
Ming Lei47bbea42012-11-06 04:53:05 +00001414 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001415 check_warn_return(ret, "Error reading WUCSR\n");
Steve Glendinning16c79a02012-09-28 00:57:52 +00001416
Steve Glendinning899a3912012-10-30 07:46:32 +00001417 val &= ~(WUCSR_WUEN | WUCSR_MPEN | WUCSR_PFDA_EN
1418 | WUCSR_BCST_EN);
Steve Glendinning16c79a02012-09-28 00:57:52 +00001419
Ming Lei47bbea42012-11-06 04:53:05 +00001420 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001421 check_warn_return(ret, "Error writing WUCSR\n");
Steve Glendinning6c636502012-09-28 00:57:53 +00001422
1423 /* clear wake-up status */
Ming Lei47bbea42012-11-06 04:53:05 +00001424 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001425 check_warn_return(ret, "Error reading PMT_CTL\n");
Steve Glendinning6c636502012-09-28 00:57:53 +00001426
1427 val &= ~PMT_CTL_WOL_EN;
1428 val |= PMT_CTL_WUPS;
1429
Ming Lei47bbea42012-11-06 04:53:05 +00001430 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001431 check_warn_return(ret, "Error writing PMT_CTL\n");
Steve Glendinning6c636502012-09-28 00:57:53 +00001432 } else {
Joe Perches1e1d7412012-11-24 01:27:49 +00001433 netdev_info(dev->net, "resuming from SUSPEND2\n");
Steve Glendinning6c636502012-09-28 00:57:53 +00001434
Ming Lei47bbea42012-11-06 04:53:05 +00001435 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001436 check_warn_return(ret, "Error reading PMT_CTL\n");
Steve Glendinning6c636502012-09-28 00:57:53 +00001437
1438 val |= PMT_CTL_PHY_PWRUP;
1439
Ming Lei47bbea42012-11-06 04:53:05 +00001440 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001441 check_warn_return(ret, "Error writing PMT_CTL\n");
Steve Glendinning6c636502012-09-28 00:57:53 +00001442 }
Steve Glendinning16c79a02012-09-28 00:57:52 +00001443
Ming Lei47bbea42012-11-06 04:53:05 +00001444 ret = smsc75xx_wait_ready(dev, 1);
Joe Perches1e1d7412012-11-24 01:27:49 +00001445 check_warn_return(ret, "device not ready in smsc75xx_resume\n");
Steve Glendinning16c79a02012-09-28 00:57:52 +00001446
1447 return usbnet_resume(intf);
1448}
1449
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001450static void smsc75xx_rx_csum_offload(struct usbnet *dev, struct sk_buff *skb,
1451 u32 rx_cmd_a, u32 rx_cmd_b)
Steve Glendinningd0cad872010-03-16 08:46:46 +00001452{
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001453 if (!(dev->net->features & NETIF_F_RXCSUM) ||
1454 unlikely(rx_cmd_a & RX_CMD_A_LCSM)) {
Steve Glendinningd0cad872010-03-16 08:46:46 +00001455 skb->ip_summed = CHECKSUM_NONE;
1456 } else {
1457 skb->csum = ntohs((u16)(rx_cmd_b >> RX_CMD_B_CSUM_SHIFT));
1458 skb->ip_summed = CHECKSUM_COMPLETE;
1459 }
1460}
1461
1462static int smsc75xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
1463{
Steve Glendinningd0cad872010-03-16 08:46:46 +00001464 while (skb->len > 0) {
1465 u32 rx_cmd_a, rx_cmd_b, align_count, size;
1466 struct sk_buff *ax_skb;
1467 unsigned char *packet;
1468
1469 memcpy(&rx_cmd_a, skb->data, sizeof(rx_cmd_a));
1470 le32_to_cpus(&rx_cmd_a);
1471 skb_pull(skb, 4);
1472
1473 memcpy(&rx_cmd_b, skb->data, sizeof(rx_cmd_b));
1474 le32_to_cpus(&rx_cmd_b);
Nico Erfurthea1649d2011-11-08 07:30:40 +00001475 skb_pull(skb, 4 + RXW_PADDING);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001476
1477 packet = skb->data;
1478
1479 /* get the packet length */
Nico Erfurthea1649d2011-11-08 07:30:40 +00001480 size = (rx_cmd_a & RX_CMD_A_LEN) - RXW_PADDING;
1481 align_count = (4 - ((size + RXW_PADDING) % 4)) % 4;
Steve Glendinningd0cad872010-03-16 08:46:46 +00001482
1483 if (unlikely(rx_cmd_a & RX_CMD_A_RED)) {
1484 netif_dbg(dev, rx_err, dev->net,
Joe Perches1e1d7412012-11-24 01:27:49 +00001485 "Error rx_cmd_a=0x%08x\n", rx_cmd_a);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001486 dev->net->stats.rx_errors++;
1487 dev->net->stats.rx_dropped++;
1488
1489 if (rx_cmd_a & RX_CMD_A_FCS)
1490 dev->net->stats.rx_crc_errors++;
1491 else if (rx_cmd_a & (RX_CMD_A_LONG | RX_CMD_A_RUNT))
1492 dev->net->stats.rx_frame_errors++;
1493 } else {
1494 /* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
1495 if (unlikely(size > (ETH_FRAME_LEN + 12))) {
1496 netif_dbg(dev, rx_err, dev->net,
Joe Perches1e1d7412012-11-24 01:27:49 +00001497 "size err rx_cmd_a=0x%08x\n",
1498 rx_cmd_a);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001499 return 0;
1500 }
1501
1502 /* last frame in this batch */
1503 if (skb->len == size) {
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001504 smsc75xx_rx_csum_offload(dev, skb, rx_cmd_a,
1505 rx_cmd_b);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001506
1507 skb_trim(skb, skb->len - 4); /* remove fcs */
1508 skb->truesize = size + sizeof(struct sk_buff);
1509
1510 return 1;
1511 }
1512
1513 ax_skb = skb_clone(skb, GFP_ATOMIC);
1514 if (unlikely(!ax_skb)) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001515 netdev_warn(dev->net, "Error allocating skb\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +00001516 return 0;
1517 }
1518
1519 ax_skb->len = size;
1520 ax_skb->data = packet;
1521 skb_set_tail_pointer(ax_skb, size);
1522
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001523 smsc75xx_rx_csum_offload(dev, ax_skb, rx_cmd_a,
1524 rx_cmd_b);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001525
1526 skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
1527 ax_skb->truesize = size + sizeof(struct sk_buff);
1528
1529 usbnet_skb_return(dev, ax_skb);
1530 }
1531
1532 skb_pull(skb, size);
1533
1534 /* padding bytes before the next frame starts */
1535 if (skb->len)
1536 skb_pull(skb, align_count);
1537 }
1538
1539 if (unlikely(skb->len < 0)) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001540 netdev_warn(dev->net, "invalid rx length<0 %d\n", skb->len);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001541 return 0;
1542 }
1543
1544 return 1;
1545}
1546
1547static struct sk_buff *smsc75xx_tx_fixup(struct usbnet *dev,
1548 struct sk_buff *skb, gfp_t flags)
1549{
1550 u32 tx_cmd_a, tx_cmd_b;
1551
1552 skb_linearize(skb);
1553
1554 if (skb_headroom(skb) < SMSC75XX_TX_OVERHEAD) {
1555 struct sk_buff *skb2 =
1556 skb_copy_expand(skb, SMSC75XX_TX_OVERHEAD, 0, flags);
1557 dev_kfree_skb_any(skb);
1558 skb = skb2;
1559 if (!skb)
1560 return NULL;
1561 }
1562
1563 tx_cmd_a = (u32)(skb->len & TX_CMD_A_LEN) | TX_CMD_A_FCS;
1564
1565 if (skb->ip_summed == CHECKSUM_PARTIAL)
1566 tx_cmd_a |= TX_CMD_A_IPE | TX_CMD_A_TPE;
1567
1568 if (skb_is_gso(skb)) {
1569 u16 mss = max(skb_shinfo(skb)->gso_size, TX_MSS_MIN);
1570 tx_cmd_b = (mss << TX_CMD_B_MSS_SHIFT) & TX_CMD_B_MSS;
1571
1572 tx_cmd_a |= TX_CMD_A_LSO;
1573 } else {
1574 tx_cmd_b = 0;
1575 }
1576
1577 skb_push(skb, 4);
1578 cpu_to_le32s(&tx_cmd_b);
1579 memcpy(skb->data, &tx_cmd_b, 4);
1580
1581 skb_push(skb, 4);
1582 cpu_to_le32s(&tx_cmd_a);
1583 memcpy(skb->data, &tx_cmd_a, 4);
1584
1585 return skb;
1586}
1587
1588static const struct driver_info smsc75xx_info = {
1589 .description = "smsc75xx USB 2.0 Gigabit Ethernet",
1590 .bind = smsc75xx_bind,
1591 .unbind = smsc75xx_unbind,
1592 .link_reset = smsc75xx_link_reset,
1593 .reset = smsc75xx_reset,
1594 .rx_fixup = smsc75xx_rx_fixup,
1595 .tx_fixup = smsc75xx_tx_fixup,
1596 .status = smsc75xx_status,
Steve Glendinning7bdd3052012-04-30 07:56:50 +00001597 .flags = FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
Steve Glendinningd0cad872010-03-16 08:46:46 +00001598};
1599
1600static const struct usb_device_id products[] = {
1601 {
1602 /* SMSC7500 USB Gigabit Ethernet Device */
1603 USB_DEVICE(USB_VENDOR_ID_SMSC, USB_PRODUCT_ID_LAN7500),
1604 .driver_info = (unsigned long) &smsc75xx_info,
1605 },
1606 {
1607 /* SMSC7500 USB Gigabit Ethernet Device */
1608 USB_DEVICE(USB_VENDOR_ID_SMSC, USB_PRODUCT_ID_LAN7505),
1609 .driver_info = (unsigned long) &smsc75xx_info,
1610 },
1611 { }, /* END */
1612};
1613MODULE_DEVICE_TABLE(usb, products);
1614
1615static struct usb_driver smsc75xx_driver = {
1616 .name = SMSC_CHIPNAME,
1617 .id_table = products,
1618 .probe = usbnet_probe,
Steve Glendinning16c79a02012-09-28 00:57:52 +00001619 .suspend = smsc75xx_suspend,
1620 .resume = smsc75xx_resume,
1621 .reset_resume = smsc75xx_resume,
Steve Glendinningd0cad872010-03-16 08:46:46 +00001622 .disconnect = usbnet_disconnect,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001623 .disable_hub_initiated_lpm = 1,
Steve Glendinningd0cad872010-03-16 08:46:46 +00001624};
1625
Greg Kroah-Hartmand632eb12011-11-18 09:44:20 -08001626module_usb_driver(smsc75xx_driver);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001627
1628MODULE_AUTHOR("Nancy Lin");
Steve Glendinning90b24cf2012-04-16 12:13:29 +01001629MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
Steve Glendinningd0cad872010-03-16 08:46:46 +00001630MODULE_DESCRIPTION("SMSC75XX USB 2.0 Gigabit Ethernet Devices");
1631MODULE_LICENSE("GPL");