blob: 064df1af0df3ed3dc757f04b72b71b5bb06b95c4 [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>
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +000029#include <linux/bitrev.h>
30#include <linux/crc16.h>
Steve Glendinning2f7ca802008-10-02 05:27:57 +000031#include <linux/crc32.h>
32#include <linux/usb/usbnet.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Steve Glendinning2f7ca802008-10-02 05:27:57 +000034#include "smsc95xx.h"
35
36#define SMSC_CHIPNAME "smsc95xx"
Steve Glendinningf7b29272008-11-20 04:19:21 -080037#define SMSC_DRIVER_VERSION "1.0.4"
Steve Glendinning2f7ca802008-10-02 05:27:57 +000038#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 (2048)
44#define LAN95XX_EEPROM_MAGIC (0x9500)
45#define EEPROM_MAC_OFFSET (0x01)
Steve Glendinningf7b29272008-11-20 04:19:21 -080046#define DEFAULT_TX_CSUM_ENABLE (true)
Steve Glendinning2f7ca802008-10-02 05:27:57 +000047#define DEFAULT_RX_CSUM_ENABLE (true)
48#define SMSC95XX_INTERNAL_PHY_ID (1)
49#define SMSC95XX_TX_OVERHEAD (8)
Steve Glendinningf7b29272008-11-20 04:19:21 -080050#define SMSC95XX_TX_OVERHEAD_CSUM (12)
Steve Glendinninge5e3af82012-11-22 08:05:24 +000051#define SUPPORTED_WAKE (WAKE_PHY | WAKE_UCAST | WAKE_BCAST | \
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +000052 WAKE_MCAST | WAKE_ARP | WAKE_MAGIC)
Steve Glendinning2f7ca802008-10-02 05:27:57 +000053
Steve Glendinning9ebca502012-11-22 08:05:23 +000054#define FEATURE_8_WAKEUP_FILTERS (0x01)
55#define FEATURE_PHY_NLP_CROSSOVER (0x02)
56#define FEATURE_AUTOSUSPEND (0x04)
57
Steve Glendinning769ea6d2012-09-28 00:07:09 +000058#define check_warn(ret, fmt, args...) \
59 ({ if (ret < 0) netdev_warn(dev->net, fmt, ##args); })
60
61#define check_warn_return(ret, fmt, args...) \
62 ({ if (ret < 0) { netdev_warn(dev->net, fmt, ##args); return ret; } })
63
64#define check_warn_goto_done(ret, fmt, args...) \
65 ({ if (ret < 0) { netdev_warn(dev->net, fmt, ##args); goto done; } })
66
Steve Glendinning2f7ca802008-10-02 05:27:57 +000067struct smsc95xx_priv {
68 u32 mac_cr;
Marc Zyngier3c0f3c62011-03-18 03:53:58 +000069 u32 hash_hi;
70 u32 hash_lo;
Steve Glendinninge0e474a2012-09-28 00:07:12 +000071 u32 wolopts;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000072 spinlock_t mac_cr_lock;
Steve Glendinning9ebca502012-11-22 08:05:23 +000073 u8 features;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000074};
75
Rusty Russelleb939922011-12-19 14:08:01 +000076static bool turbo_mode = true;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000077module_param(turbo_mode, bool, 0644);
78MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
79
Ming Leiec321152012-11-06 04:53:07 +000080static int __must_check __smsc95xx_read_reg(struct usbnet *dev, u32 index,
81 u32 *data, int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +000082{
Ming Lei72108fd2012-10-24 19:47:04 +000083 u32 buf;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000084 int ret;
Ming Leiec321152012-11-06 04:53:07 +000085 int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
Steve Glendinning2f7ca802008-10-02 05:27:57 +000086
87 BUG_ON(!dev);
88
Ming Leiec321152012-11-06 04:53:07 +000089 if (!in_pm)
90 fn = usbnet_read_cmd;
91 else
92 fn = usbnet_read_cmd_nopm;
93
94 ret = fn(dev, USB_VENDOR_REQUEST_READ_REGISTER, USB_DIR_IN
95 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
96 0, index, &buf, 4);
Steve Glendinning2f7ca802008-10-02 05:27:57 +000097 if (unlikely(ret < 0))
Joe Perches1e1d7412012-11-24 01:27:49 +000098 netdev_warn(dev->net, "Failed to read reg index 0x%08x: %d\n",
99 index, ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000100
Ming Lei72108fd2012-10-24 19:47:04 +0000101 le32_to_cpus(&buf);
102 *data = buf;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000103
104 return ret;
105}
106
Ming Leiec321152012-11-06 04:53:07 +0000107static int __must_check __smsc95xx_write_reg(struct usbnet *dev, u32 index,
108 u32 data, int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000109{
Ming Lei72108fd2012-10-24 19:47:04 +0000110 u32 buf;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000111 int ret;
Ming Leiec321152012-11-06 04:53:07 +0000112 int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000113
114 BUG_ON(!dev);
115
Ming Leiec321152012-11-06 04:53:07 +0000116 if (!in_pm)
117 fn = usbnet_write_cmd;
118 else
119 fn = usbnet_write_cmd_nopm;
120
Ming Lei72108fd2012-10-24 19:47:04 +0000121 buf = data;
122 cpu_to_le32s(&buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000123
Ming Leiec321152012-11-06 04:53:07 +0000124 ret = fn(dev, USB_VENDOR_REQUEST_WRITE_REGISTER, USB_DIR_OUT
125 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
126 0, index, &buf, 4);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000127 if (unlikely(ret < 0))
Joe Perches1e1d7412012-11-24 01:27:49 +0000128 netdev_warn(dev->net, "Failed to write reg index 0x%08x: %d\n",
129 index, ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000130
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000131 return ret;
132}
133
Ming Leiec321152012-11-06 04:53:07 +0000134static int __must_check smsc95xx_read_reg_nopm(struct usbnet *dev, u32 index,
135 u32 *data)
136{
137 return __smsc95xx_read_reg(dev, index, data, 1);
138}
139
140static int __must_check smsc95xx_write_reg_nopm(struct usbnet *dev, u32 index,
141 u32 data)
142{
143 return __smsc95xx_write_reg(dev, index, data, 1);
144}
145
146static int __must_check smsc95xx_read_reg(struct usbnet *dev, u32 index,
147 u32 *data)
148{
149 return __smsc95xx_read_reg(dev, index, data, 0);
150}
151
152static int __must_check smsc95xx_write_reg(struct usbnet *dev, u32 index,
153 u32 data)
154{
155 return __smsc95xx_write_reg(dev, index, data, 0);
156}
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000157
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000158/* Loop until the read is completed with timeout
159 * called with phy_mutex held */
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000160static int __must_check __smsc95xx_phy_wait_not_busy(struct usbnet *dev,
161 int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000162{
163 unsigned long start_time = jiffies;
164 u32 val;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000165 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000166
167 do {
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000168 ret = __smsc95xx_read_reg(dev, MII_ADDR, &val, in_pm);
Joe Perches1e1d7412012-11-24 01:27:49 +0000169 check_warn_return(ret, "Error reading MII_ACCESS\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000170 if (!(val & MII_BUSY_))
171 return 0;
172 } while (!time_after(jiffies, start_time + HZ));
173
174 return -EIO;
175}
176
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000177static int __smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx,
178 int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000179{
180 struct usbnet *dev = netdev_priv(netdev);
181 u32 val, addr;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000182 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000183
184 mutex_lock(&dev->phy_mutex);
185
186 /* confirm MII not busy */
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000187 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
Joe Perches1e1d7412012-11-24 01:27:49 +0000188 check_warn_goto_done(ret, "MII is busy in smsc95xx_mdio_read\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000189
190 /* set the address, index & direction (read from PHY) */
191 phy_id &= dev->mii.phy_id_mask;
192 idx &= dev->mii.reg_num_mask;
Steve Glendinning80928802012-11-06 00:08:53 +0000193 addr = (phy_id << 11) | (idx << 6) | MII_READ_ | MII_BUSY_;
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000194 ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
Joe Perches1e1d7412012-11-24 01:27:49 +0000195 check_warn_goto_done(ret, "Error writing MII_ADDR\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000196
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000197 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
Joe Perches1e1d7412012-11-24 01:27:49 +0000198 check_warn_goto_done(ret, "Timed out reading MII reg %02X\n", idx);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000199
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000200 ret = __smsc95xx_read_reg(dev, MII_DATA, &val, in_pm);
Joe Perches1e1d7412012-11-24 01:27:49 +0000201 check_warn_goto_done(ret, "Error reading MII_DATA\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000202
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000203 ret = (u16)(val & 0xFFFF);
204
205done:
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000206 mutex_unlock(&dev->phy_mutex);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000207 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000208}
209
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000210static void __smsc95xx_mdio_write(struct net_device *netdev, int phy_id,
211 int idx, int regval, int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000212{
213 struct usbnet *dev = netdev_priv(netdev);
214 u32 val, addr;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000215 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000216
217 mutex_lock(&dev->phy_mutex);
218
219 /* confirm MII not busy */
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000220 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
Joe Perches1e1d7412012-11-24 01:27:49 +0000221 check_warn_goto_done(ret, "MII is busy in smsc95xx_mdio_write\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000222
223 val = regval;
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000224 ret = __smsc95xx_write_reg(dev, MII_DATA, val, in_pm);
Joe Perches1e1d7412012-11-24 01:27:49 +0000225 check_warn_goto_done(ret, "Error writing MII_DATA\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000226
227 /* set the address, index & direction (write to PHY) */
228 phy_id &= dev->mii.phy_id_mask;
229 idx &= dev->mii.reg_num_mask;
Steve Glendinning80928802012-11-06 00:08:53 +0000230 addr = (phy_id << 11) | (idx << 6) | MII_WRITE_ | MII_BUSY_;
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000231 ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
Joe Perches1e1d7412012-11-24 01:27:49 +0000232 check_warn_goto_done(ret, "Error writing MII_ADDR\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000233
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000234 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
Joe Perches1e1d7412012-11-24 01:27:49 +0000235 check_warn_goto_done(ret, "Timed out writing MII reg %02X\n", idx);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000236
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000237done:
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000238 mutex_unlock(&dev->phy_mutex);
239}
240
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000241static int smsc95xx_mdio_read_nopm(struct net_device *netdev, int phy_id,
242 int idx)
243{
244 return __smsc95xx_mdio_read(netdev, phy_id, idx, 1);
245}
246
247static void smsc95xx_mdio_write_nopm(struct net_device *netdev, int phy_id,
248 int idx, int regval)
249{
250 __smsc95xx_mdio_write(netdev, phy_id, idx, regval, 1);
251}
252
253static int smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
254{
255 return __smsc95xx_mdio_read(netdev, phy_id, idx, 0);
256}
257
258static void smsc95xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
259 int regval)
260{
261 __smsc95xx_mdio_write(netdev, phy_id, idx, regval, 0);
262}
263
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000264static int __must_check smsc95xx_wait_eeprom(struct usbnet *dev)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000265{
266 unsigned long start_time = jiffies;
267 u32 val;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000268 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000269
270 do {
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000271 ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +0000272 check_warn_return(ret, "Error reading E2P_CMD\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000273 if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_))
274 break;
275 udelay(40);
276 } while (!time_after(jiffies, start_time + HZ));
277
278 if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) {
Joe Perches60b86752010-02-17 10:30:23 +0000279 netdev_warn(dev->net, "EEPROM read operation timeout\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000280 return -EIO;
281 }
282
283 return 0;
284}
285
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000286static int __must_check smsc95xx_eeprom_confirm_not_busy(struct usbnet *dev)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000287{
288 unsigned long start_time = jiffies;
289 u32 val;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000290 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000291
292 do {
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000293 ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +0000294 check_warn_return(ret, "Error reading E2P_CMD\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000295
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000296 if (!(val & E2P_CMD_BUSY_))
297 return 0;
298
299 udelay(40);
300 } while (!time_after(jiffies, start_time + HZ));
301
Joe Perches60b86752010-02-17 10:30:23 +0000302 netdev_warn(dev->net, "EEPROM is busy\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000303 return -EIO;
304}
305
306static int smsc95xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
307 u8 *data)
308{
309 u32 val;
310 int i, ret;
311
312 BUG_ON(!dev);
313 BUG_ON(!data);
314
315 ret = smsc95xx_eeprom_confirm_not_busy(dev);
316 if (ret)
317 return ret;
318
319 for (i = 0; i < length; i++) {
320 val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000321 ret = smsc95xx_write_reg(dev, E2P_CMD, val);
Joe Perches1e1d7412012-11-24 01:27:49 +0000322 check_warn_return(ret, "Error writing E2P_CMD\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000323
324 ret = smsc95xx_wait_eeprom(dev);
325 if (ret < 0)
326 return ret;
327
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000328 ret = smsc95xx_read_reg(dev, E2P_DATA, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +0000329 check_warn_return(ret, "Error reading E2P_DATA\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000330
331 data[i] = val & 0xFF;
332 offset++;
333 }
334
335 return 0;
336}
337
338static int smsc95xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
339 u8 *data)
340{
341 u32 val;
342 int i, ret;
343
344 BUG_ON(!dev);
345 BUG_ON(!data);
346
347 ret = smsc95xx_eeprom_confirm_not_busy(dev);
348 if (ret)
349 return ret;
350
351 /* Issue write/erase enable command */
352 val = E2P_CMD_BUSY_ | E2P_CMD_EWEN_;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000353 ret = smsc95xx_write_reg(dev, E2P_CMD, val);
Joe Perches1e1d7412012-11-24 01:27:49 +0000354 check_warn_return(ret, "Error writing E2P_DATA\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000355
356 ret = smsc95xx_wait_eeprom(dev);
357 if (ret < 0)
358 return ret;
359
360 for (i = 0; i < length; i++) {
361
362 /* Fill data register */
363 val = data[i];
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000364 ret = smsc95xx_write_reg(dev, E2P_DATA, val);
Joe Perches1e1d7412012-11-24 01:27:49 +0000365 check_warn_return(ret, "Error writing E2P_DATA\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000366
367 /* Send "write" command */
368 val = E2P_CMD_BUSY_ | E2P_CMD_WRITE_ | (offset & E2P_CMD_ADDR_);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000369 ret = smsc95xx_write_reg(dev, E2P_CMD, val);
Joe Perches1e1d7412012-11-24 01:27:49 +0000370 check_warn_return(ret, "Error writing E2P_CMD\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000371
372 ret = smsc95xx_wait_eeprom(dev);
373 if (ret < 0)
374 return ret;
375
376 offset++;
377 }
378
379 return 0;
380}
381
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000382static int __must_check smsc95xx_write_reg_async(struct usbnet *dev, u16 index,
383 u32 *data)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000384{
Steve Glendinning1d74a6b2008-10-09 14:34:47 -0700385 const u16 size = 4;
Ming Lei72108fd2012-10-24 19:47:04 +0000386 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000387
Ming Lei72108fd2012-10-24 19:47:04 +0000388 ret = usbnet_write_cmd_async(dev, USB_VENDOR_REQUEST_WRITE_REGISTER,
389 USB_DIR_OUT | USB_TYPE_VENDOR |
390 USB_RECIP_DEVICE,
391 0, index, data, size);
392 if (ret < 0)
393 netdev_warn(dev->net, "Error write async cmd, sts=%d\n",
394 ret);
395 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000396}
397
398/* returns hash bit number for given MAC address
399 * example:
400 * 01 00 5E 00 00 01 -> returns bit number 31 */
401static unsigned int smsc95xx_hash(char addr[ETH_ALEN])
402{
403 return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
404}
405
406static void smsc95xx_set_multicast(struct net_device *netdev)
407{
408 struct usbnet *dev = netdev_priv(netdev);
409 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000410 unsigned long flags;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000411 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000412
Marc Zyngier3c0f3c62011-03-18 03:53:58 +0000413 pdata->hash_hi = 0;
414 pdata->hash_lo = 0;
415
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000416 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
417
418 if (dev->net->flags & IFF_PROMISC) {
Joe Perchesa475f602010-02-17 10:30:24 +0000419 netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000420 pdata->mac_cr |= MAC_CR_PRMS_;
421 pdata->mac_cr &= ~(MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
422 } else if (dev->net->flags & IFF_ALLMULTI) {
Joe Perchesa475f602010-02-17 10:30:24 +0000423 netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000424 pdata->mac_cr |= MAC_CR_MCPAS_;
425 pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_HPFILT_);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000426 } else if (!netdev_mc_empty(dev->net)) {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000427 struct netdev_hw_addr *ha;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000428
429 pdata->mac_cr |= MAC_CR_HPFILT_;
430 pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);
431
Jiri Pirko22bedad32010-04-01 21:22:57 +0000432 netdev_for_each_mc_addr(ha, netdev) {
433 u32 bitnum = smsc95xx_hash(ha->addr);
Jiri Pirkoa92635d2010-02-18 04:02:26 +0000434 u32 mask = 0x01 << (bitnum & 0x1F);
435 if (bitnum & 0x20)
Marc Zyngier3c0f3c62011-03-18 03:53:58 +0000436 pdata->hash_hi |= mask;
Jiri Pirkoa92635d2010-02-18 04:02:26 +0000437 else
Marc Zyngier3c0f3c62011-03-18 03:53:58 +0000438 pdata->hash_lo |= mask;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000439 }
440
Joe Perchesa475f602010-02-17 10:30:24 +0000441 netif_dbg(dev, drv, dev->net, "HASHH=0x%08X, HASHL=0x%08X\n",
Marc Zyngier3c0f3c62011-03-18 03:53:58 +0000442 pdata->hash_hi, pdata->hash_lo);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000443 } else {
Joe Perchesa475f602010-02-17 10:30:24 +0000444 netif_dbg(dev, drv, dev->net, "receive own packets only\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000445 pdata->mac_cr &=
446 ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
447 }
448
449 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
450
451 /* Initiate async writes, as we can't wait for completion here */
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000452 ret = smsc95xx_write_reg_async(dev, HASHH, &pdata->hash_hi);
Joe Perches1e1d7412012-11-24 01:27:49 +0000453 check_warn(ret, "failed to initiate async write to HASHH\n");
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000454
455 ret = smsc95xx_write_reg_async(dev, HASHL, &pdata->hash_lo);
Joe Perches1e1d7412012-11-24 01:27:49 +0000456 check_warn(ret, "failed to initiate async write to HASHL\n");
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000457
458 ret = smsc95xx_write_reg_async(dev, MAC_CR, &pdata->mac_cr);
Joe Perches1e1d7412012-11-24 01:27:49 +0000459 check_warn(ret, "failed to initiate async write to MAC_CR\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000460}
461
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000462static int smsc95xx_phy_update_flowcontrol(struct usbnet *dev, u8 duplex,
463 u16 lcladv, u16 rmtadv)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000464{
465 u32 flow, afc_cfg = 0;
466
467 int ret = smsc95xx_read_reg(dev, AFC_CFG, &afc_cfg);
Joe Perches1e1d7412012-11-24 01:27:49 +0000468 check_warn_return(ret, "Error reading AFC_CFG\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000469
470 if (duplex == DUPLEX_FULL) {
Steve Glendinningbc02ff92008-12-16 02:00:48 -0800471 u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000472
473 if (cap & FLOW_CTRL_RX)
474 flow = 0xFFFF0002;
475 else
476 flow = 0;
477
478 if (cap & FLOW_CTRL_TX)
479 afc_cfg |= 0xF;
480 else
481 afc_cfg &= ~0xF;
482
Joe Perchesa475f602010-02-17 10:30:24 +0000483 netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
Joe Perches60b86752010-02-17 10:30:23 +0000484 cap & FLOW_CTRL_RX ? "enabled" : "disabled",
485 cap & FLOW_CTRL_TX ? "enabled" : "disabled");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000486 } else {
Joe Perchesa475f602010-02-17 10:30:24 +0000487 netif_dbg(dev, link, dev->net, "half duplex\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000488 flow = 0;
489 afc_cfg |= 0xF;
490 }
491
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000492 ret = smsc95xx_write_reg(dev, FLOW, flow);
Joe Perches1e1d7412012-11-24 01:27:49 +0000493 check_warn_return(ret, "Error writing FLOW\n");
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000494
495 ret = smsc95xx_write_reg(dev, AFC_CFG, afc_cfg);
Joe Perches1e1d7412012-11-24 01:27:49 +0000496 check_warn_return(ret, "Error writing AFC_CFG\n");
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000497
498 return 0;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000499}
500
501static int smsc95xx_link_reset(struct usbnet *dev)
502{
503 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
504 struct mii_if_info *mii = &dev->mii;
David Decotigny8ae6dac2011-04-27 18:32:38 +0000505 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000506 unsigned long flags;
507 u16 lcladv, rmtadv;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000508 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000509
510 /* clear interrupt status */
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000511 ret = smsc95xx_mdio_read(dev->net, mii->phy_id, PHY_INT_SRC);
Joe Perches1e1d7412012-11-24 01:27:49 +0000512 check_warn_return(ret, "Error reading PHY_INT_SRC\n");
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000513
514 ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
Joe Perches1e1d7412012-11-24 01:27:49 +0000515 check_warn_return(ret, "Error writing INT_STS\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000516
517 mii_check_media(mii, 1, 1);
518 mii_ethtool_gset(&dev->mii, &ecmd);
519 lcladv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
520 rmtadv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_LPA);
521
David Decotigny8ae6dac2011-04-27 18:32:38 +0000522 netif_dbg(dev, link, dev->net,
523 "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
524 ethtool_cmd_speed(&ecmd), ecmd.duplex, lcladv, rmtadv);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000525
526 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
527 if (ecmd.duplex != DUPLEX_FULL) {
528 pdata->mac_cr &= ~MAC_CR_FDPX_;
529 pdata->mac_cr |= MAC_CR_RCVOWN_;
530 } else {
531 pdata->mac_cr &= ~MAC_CR_RCVOWN_;
532 pdata->mac_cr |= MAC_CR_FDPX_;
533 }
534 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
535
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000536 ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
Joe Perches1e1d7412012-11-24 01:27:49 +0000537 check_warn_return(ret, "Error writing MAC_CR\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000538
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000539 ret = smsc95xx_phy_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
Joe Perches1e1d7412012-11-24 01:27:49 +0000540 check_warn_return(ret, "Error updating PHY flow control\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000541
542 return 0;
543}
544
545static void smsc95xx_status(struct usbnet *dev, struct urb *urb)
546{
547 u32 intdata;
548
549 if (urb->actual_length != 4) {
Joe Perches60b86752010-02-17 10:30:23 +0000550 netdev_warn(dev->net, "unexpected urb length %d\n",
551 urb->actual_length);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000552 return;
553 }
554
555 memcpy(&intdata, urb->transfer_buffer, 4);
Steve Glendinning1d74a6b2008-10-09 14:34:47 -0700556 le32_to_cpus(&intdata);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000557
Joe Perchesa475f602010-02-17 10:30:24 +0000558 netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000559
560 if (intdata & INT_ENP_PHY_INT_)
561 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
562 else
Joe Perches60b86752010-02-17 10:30:23 +0000563 netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
564 intdata);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000565}
566
Steve Glendinningf7b29272008-11-20 04:19:21 -0800567/* Enable or disable Tx & Rx checksum offload engines */
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000568static int smsc95xx_set_features(struct net_device *netdev,
569 netdev_features_t features)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000570{
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700571 struct usbnet *dev = netdev_priv(netdev);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000572 u32 read_buf;
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700573 int ret;
574
575 ret = smsc95xx_read_reg(dev, COE_CR, &read_buf);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000576 check_warn_return(ret, "Failed to read COE_CR: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000577
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700578 if (features & NETIF_F_HW_CSUM)
Steve Glendinningf7b29272008-11-20 04:19:21 -0800579 read_buf |= Tx_COE_EN_;
580 else
581 read_buf &= ~Tx_COE_EN_;
582
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700583 if (features & NETIF_F_RXCSUM)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000584 read_buf |= Rx_COE_EN_;
585 else
586 read_buf &= ~Rx_COE_EN_;
587
588 ret = smsc95xx_write_reg(dev, COE_CR, read_buf);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000589 check_warn_return(ret, "Failed to write COE_CR: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000590
Joe Perchesa475f602010-02-17 10:30:24 +0000591 netif_dbg(dev, hw, dev->net, "COE_CR = 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000592 return 0;
593}
594
595static int smsc95xx_ethtool_get_eeprom_len(struct net_device *net)
596{
597 return MAX_EEPROM_SIZE;
598}
599
600static int smsc95xx_ethtool_get_eeprom(struct net_device *netdev,
601 struct ethtool_eeprom *ee, u8 *data)
602{
603 struct usbnet *dev = netdev_priv(netdev);
604
605 ee->magic = LAN95XX_EEPROM_MAGIC;
606
607 return smsc95xx_read_eeprom(dev, ee->offset, ee->len, data);
608}
609
610static int smsc95xx_ethtool_set_eeprom(struct net_device *netdev,
611 struct ethtool_eeprom *ee, u8 *data)
612{
613 struct usbnet *dev = netdev_priv(netdev);
614
615 if (ee->magic != LAN95XX_EEPROM_MAGIC) {
Joe Perches60b86752010-02-17 10:30:23 +0000616 netdev_warn(dev->net, "EEPROM: magic value mismatch, magic = 0x%x\n",
617 ee->magic);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000618 return -EINVAL;
619 }
620
621 return smsc95xx_write_eeprom(dev, ee->offset, ee->len, data);
622}
623
Emeric Vigier9fa32e92012-07-09 17:44:45 -0400624static int smsc95xx_ethtool_getregslen(struct net_device *netdev)
625{
626 /* all smsc95xx registers */
627 return COE_CR - ID_REV + 1;
628}
629
630static void
631smsc95xx_ethtool_getregs(struct net_device *netdev, struct ethtool_regs *regs,
632 void *buf)
633{
634 struct usbnet *dev = netdev_priv(netdev);
Dan Carpenterd3484462012-07-10 20:32:51 +0000635 unsigned int i, j;
636 int retval;
Emeric Vigier9fa32e92012-07-09 17:44:45 -0400637 u32 *data = buf;
638
639 retval = smsc95xx_read_reg(dev, ID_REV, &regs->version);
640 if (retval < 0) {
641 netdev_warn(netdev, "REGS: cannot read ID_REV\n");
642 return;
643 }
644
645 for (i = ID_REV, j = 0; i <= COE_CR; i += (sizeof(u32)), j++) {
646 retval = smsc95xx_read_reg(dev, i, &data[j]);
647 if (retval < 0) {
648 netdev_warn(netdev, "REGS: cannot read reg[%x]\n", i);
649 return;
650 }
651 }
652}
653
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000654static void smsc95xx_ethtool_get_wol(struct net_device *net,
655 struct ethtool_wolinfo *wolinfo)
656{
657 struct usbnet *dev = netdev_priv(net);
658 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
659
660 wolinfo->supported = SUPPORTED_WAKE;
661 wolinfo->wolopts = pdata->wolopts;
662}
663
664static int smsc95xx_ethtool_set_wol(struct net_device *net,
665 struct ethtool_wolinfo *wolinfo)
666{
667 struct usbnet *dev = netdev_priv(net);
668 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
Steve Glendinning3b146922012-11-30 05:55:50 +0000669 int ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000670
671 pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
Steve Glendinning3b146922012-11-30 05:55:50 +0000672
673 ret = device_set_wakeup_enable(&dev->udev->dev, pdata->wolopts);
674 check_warn_return(ret, "device_set_wakeup_enable error %d\n", ret);
675
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000676 return 0;
677}
678
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700679static const struct ethtool_ops smsc95xx_ethtool_ops = {
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000680 .get_link = usbnet_get_link,
681 .nway_reset = usbnet_nway_reset,
682 .get_drvinfo = usbnet_get_drvinfo,
683 .get_msglevel = usbnet_get_msglevel,
684 .set_msglevel = usbnet_set_msglevel,
685 .get_settings = usbnet_get_settings,
686 .set_settings = usbnet_set_settings,
687 .get_eeprom_len = smsc95xx_ethtool_get_eeprom_len,
688 .get_eeprom = smsc95xx_ethtool_get_eeprom,
689 .set_eeprom = smsc95xx_ethtool_set_eeprom,
Emeric Vigier9fa32e92012-07-09 17:44:45 -0400690 .get_regs_len = smsc95xx_ethtool_getregslen,
691 .get_regs = smsc95xx_ethtool_getregs,
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000692 .get_wol = smsc95xx_ethtool_get_wol,
693 .set_wol = smsc95xx_ethtool_set_wol,
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000694};
695
696static int smsc95xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
697{
698 struct usbnet *dev = netdev_priv(netdev);
699
700 if (!netif_running(netdev))
701 return -EINVAL;
702
703 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
704}
705
706static void smsc95xx_init_mac_address(struct usbnet *dev)
707{
708 /* try reading mac address from EEPROM */
709 if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
710 dev->net->dev_addr) == 0) {
711 if (is_valid_ether_addr(dev->net->dev_addr)) {
712 /* eeprom values are valid so use them */
Joe Perchesa475f602010-02-17 10:30:24 +0000713 netif_dbg(dev, ifup, dev->net, "MAC address read from EEPROM\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000714 return;
715 }
716 }
717
718 /* no eeprom, or eeprom values are invalid. generate random MAC */
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000719 eth_hw_addr_random(dev->net);
Joe Perchesc7e12ea2012-07-12 19:33:07 +0000720 netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000721}
722
723static int smsc95xx_set_mac_address(struct usbnet *dev)
724{
725 u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
726 dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
727 u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
728 int ret;
729
730 ret = smsc95xx_write_reg(dev, ADDRL, addr_lo);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000731 check_warn_return(ret, "Failed to write ADDRL: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000732
733 ret = smsc95xx_write_reg(dev, ADDRH, addr_hi);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000734 check_warn_return(ret, "Failed to write ADDRH: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000735
736 return 0;
737}
738
739/* starts the TX path */
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000740static int smsc95xx_start_tx_path(struct usbnet *dev)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000741{
742 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
743 unsigned long flags;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000744 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000745
746 /* Enable Tx at MAC */
747 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
748 pdata->mac_cr |= MAC_CR_TXEN_;
749 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
750
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000751 ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
752 check_warn_return(ret, "Failed to write MAC_CR: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000753
754 /* Enable Tx at SCSRs */
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000755 ret = smsc95xx_write_reg(dev, TX_CFG, TX_CFG_ON_);
756 check_warn_return(ret, "Failed to write TX_CFG: %d\n", ret);
757
758 return 0;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000759}
760
761/* Starts the Receive path */
Ming Leiec321152012-11-06 04:53:07 +0000762static int smsc95xx_start_rx_path(struct usbnet *dev, int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000763{
764 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
765 unsigned long flags;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000766 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000767
768 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
769 pdata->mac_cr |= MAC_CR_RXEN_;
770 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
771
Ming Leiec321152012-11-06 04:53:07 +0000772 ret = __smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr, in_pm);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000773 check_warn_return(ret, "Failed to write MAC_CR: %d\n", ret);
774
775 return 0;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000776}
777
778static int smsc95xx_phy_initialize(struct usbnet *dev)
779{
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000780 int bmcr, ret, timeout = 0;
Steve Glendinningdb443c42010-03-16 09:03:06 +0000781
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000782 /* Initialize MII structure */
783 dev->mii.dev = dev->net;
784 dev->mii.mdio_read = smsc95xx_mdio_read;
785 dev->mii.mdio_write = smsc95xx_mdio_write;
786 dev->mii.phy_id_mask = 0x1f;
787 dev->mii.reg_num_mask = 0x1f;
788 dev->mii.phy_id = SMSC95XX_INTERNAL_PHY_ID;
789
Steve Glendinningdb443c42010-03-16 09:03:06 +0000790 /* reset phy and wait for reset to complete */
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000791 smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
Steve Glendinningdb443c42010-03-16 09:03:06 +0000792
793 do {
794 msleep(10);
795 bmcr = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
796 timeout++;
Rabin Vincentd9460922011-04-30 08:29:27 +0000797 } while ((bmcr & BMCR_RESET) && (timeout < 100));
Steve Glendinningdb443c42010-03-16 09:03:06 +0000798
799 if (timeout >= 100) {
800 netdev_warn(dev->net, "timeout on PHY Reset");
801 return -EIO;
802 }
803
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000804 smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
805 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
806 ADVERTISE_PAUSE_ASYM);
807
808 /* read to clear */
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000809 ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
Joe Perches1e1d7412012-11-24 01:27:49 +0000810 check_warn_return(ret, "Failed to read PHY_INT_SRC during init\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000811
812 smsc95xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
813 PHY_INT_MASK_DEFAULT_);
814 mii_nway_restart(&dev->mii);
815
Joe Perchesa475f602010-02-17 10:30:24 +0000816 netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000817 return 0;
818}
819
820static int smsc95xx_reset(struct usbnet *dev)
821{
822 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
823 u32 read_buf, write_buf, burst_cap;
824 int ret = 0, timeout;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000825
Joe Perchesa475f602010-02-17 10:30:24 +0000826 netif_dbg(dev, ifup, dev->net, "entering smsc95xx_reset\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000827
Steve Glendinning44367612012-09-28 00:07:08 +0000828 ret = smsc95xx_write_reg(dev, HW_CFG, HW_CFG_LRST_);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000829 check_warn_return(ret, "Failed to write HW_CFG_LRST_ bit in HW_CFG\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000830
831 timeout = 0;
832 do {
Steve Glendinningcf2acec2012-09-28 00:07:07 +0000833 msleep(10);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000834 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000835 check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000836 timeout++;
837 } while ((read_buf & HW_CFG_LRST_) && (timeout < 100));
838
839 if (timeout >= 100) {
Joe Perches60b86752010-02-17 10:30:23 +0000840 netdev_warn(dev->net, "timeout waiting for completion of Lite Reset\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000841 return ret;
842 }
843
Steve Glendinning44367612012-09-28 00:07:08 +0000844 ret = smsc95xx_write_reg(dev, PM_CTRL, PM_CTL_PHY_RST_);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000845 check_warn_return(ret, "Failed to write PM_CTRL: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000846
847 timeout = 0;
848 do {
Steve Glendinningcf2acec2012-09-28 00:07:07 +0000849 msleep(10);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000850 ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000851 check_warn_return(ret, "Failed to read PM_CTRL: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000852 timeout++;
853 } while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));
854
855 if (timeout >= 100) {
Joe Perches60b86752010-02-17 10:30:23 +0000856 netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000857 return ret;
858 }
859
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000860 ret = smsc95xx_set_mac_address(dev);
861 if (ret < 0)
862 return ret;
863
Joe Perches1e1d7412012-11-24 01:27:49 +0000864 netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n",
865 dev->net->dev_addr);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000866
867 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000868 check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000869
Joe Perches1e1d7412012-11-24 01:27:49 +0000870 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n",
871 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000872
873 read_buf |= HW_CFG_BIR_;
874
875 ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000876 check_warn_return(ret, "Failed to write HW_CFG_BIR_ bit in HW_CFG\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000877
878 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000879 check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
Joe Perchesa475f602010-02-17 10:30:24 +0000880 netif_dbg(dev, ifup, dev->net,
881 "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
882 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000883
884 if (!turbo_mode) {
885 burst_cap = 0;
886 dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
887 } else if (dev->udev->speed == USB_SPEED_HIGH) {
888 burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
889 dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
890 } else {
891 burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
892 dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
893 }
894
Joe Perches1e1d7412012-11-24 01:27:49 +0000895 netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n",
896 (ulong)dev->rx_urb_size);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000897
898 ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000899 check_warn_return(ret, "Failed to write BURST_CAP: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000900
901 ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000902 check_warn_return(ret, "Failed to read BURST_CAP: %d\n", ret);
903
Joe Perchesa475f602010-02-17 10:30:24 +0000904 netif_dbg(dev, ifup, dev->net,
905 "Read Value from BURST_CAP after writing: 0x%08x\n",
906 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000907
Steve Glendinning44367612012-09-28 00:07:08 +0000908 ret = smsc95xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000909 check_warn_return(ret, "Failed to write BULK_IN_DLY: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000910
911 ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000912 check_warn_return(ret, "Failed to read BULK_IN_DLY: %d\n", ret);
913
Joe Perchesa475f602010-02-17 10:30:24 +0000914 netif_dbg(dev, ifup, dev->net,
915 "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
916 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000917
918 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000919 check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
920
Joe Perches1e1d7412012-11-24 01:27:49 +0000921 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG: 0x%08x\n",
922 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000923
924 if (turbo_mode)
925 read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);
926
927 read_buf &= ~HW_CFG_RXDOFF_;
928
929 /* set Rx data offset=2, Make IP header aligns on word boundary. */
930 read_buf |= NET_IP_ALIGN << 9;
931
932 ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000933 check_warn_return(ret, "Failed to write HW_CFG: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000934
935 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000936 check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
937
Joe Perchesa475f602010-02-17 10:30:24 +0000938 netif_dbg(dev, ifup, dev->net,
939 "Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000940
Steve Glendinning44367612012-09-28 00:07:08 +0000941 ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000942 check_warn_return(ret, "Failed to write INT_STS: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000943
944 ret = smsc95xx_read_reg(dev, ID_REV, &read_buf);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000945 check_warn_return(ret, "Failed to read ID_REV: %d\n", ret);
Joe Perchesa475f602010-02-17 10:30:24 +0000946 netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000947
Steve Glendinningf2935012009-05-01 05:46:51 +0000948 /* Configure GPIO pins as LED outputs */
949 write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED |
950 LED_GPIO_CFG_FDX_LED;
951 ret = smsc95xx_write_reg(dev, LED_GPIO_CFG, write_buf);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000952 check_warn_return(ret, "Failed to write LED_GPIO_CFG: %d\n", ret);
Steve Glendinningf2935012009-05-01 05:46:51 +0000953
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000954 /* Init Tx */
Steve Glendinning44367612012-09-28 00:07:08 +0000955 ret = smsc95xx_write_reg(dev, FLOW, 0);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000956 check_warn_return(ret, "Failed to write FLOW: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000957
Steve Glendinning44367612012-09-28 00:07:08 +0000958 ret = smsc95xx_write_reg(dev, AFC_CFG, AFC_CFG_DEFAULT);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000959 check_warn_return(ret, "Failed to write AFC_CFG: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000960
961 /* Don't need mac_cr_lock during initialisation */
962 ret = smsc95xx_read_reg(dev, MAC_CR, &pdata->mac_cr);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000963 check_warn_return(ret, "Failed to read MAC_CR: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000964
965 /* Init Rx */
966 /* Set Vlan */
Steve Glendinning44367612012-09-28 00:07:08 +0000967 ret = smsc95xx_write_reg(dev, VLAN1, (u32)ETH_P_8021Q);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000968 check_warn_return(ret, "Failed to write VLAN1: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000969
Steve Glendinningf7b29272008-11-20 04:19:21 -0800970 /* Enable or disable checksum offload engines */
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000971 ret = smsc95xx_set_features(dev->net, dev->net->features);
Joe Perches1e1d7412012-11-24 01:27:49 +0000972 check_warn_return(ret, "Failed to set checksum offload features\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000973
974 smsc95xx_set_multicast(dev->net);
975
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000976 ret = smsc95xx_phy_initialize(dev);
Joe Perches1e1d7412012-11-24 01:27:49 +0000977 check_warn_return(ret, "Failed to init PHY\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000978
979 ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000980 check_warn_return(ret, "Failed to read INT_EP_CTL: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000981
982 /* enable PHY interrupts */
983 read_buf |= INT_EP_CTL_PHY_INT_;
984
985 ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000986 check_warn_return(ret, "Failed to write INT_EP_CTL: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000987
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000988 ret = smsc95xx_start_tx_path(dev);
Joe Perches1e1d7412012-11-24 01:27:49 +0000989 check_warn_return(ret, "Failed to start TX path\n");
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000990
Ming Leiec321152012-11-06 04:53:07 +0000991 ret = smsc95xx_start_rx_path(dev, 0);
Joe Perches1e1d7412012-11-24 01:27:49 +0000992 check_warn_return(ret, "Failed to start RX path\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000993
Joe Perchesa475f602010-02-17 10:30:24 +0000994 netif_dbg(dev, ifup, dev->net, "smsc95xx_reset, return 0\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000995 return 0;
996}
997
Stephen Hemminger63e77b32009-03-20 19:35:58 +0000998static const struct net_device_ops smsc95xx_netdev_ops = {
999 .ndo_open = usbnet_open,
1000 .ndo_stop = usbnet_stop,
1001 .ndo_start_xmit = usbnet_start_xmit,
1002 .ndo_tx_timeout = usbnet_tx_timeout,
1003 .ndo_change_mtu = usbnet_change_mtu,
1004 .ndo_set_mac_address = eth_mac_addr,
1005 .ndo_validate_addr = eth_validate_addr,
1006 .ndo_do_ioctl = smsc95xx_ioctl,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001007 .ndo_set_rx_mode = smsc95xx_set_multicast,
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001008 .ndo_set_features = smsc95xx_set_features,
Stephen Hemminger63e77b32009-03-20 19:35:58 +00001009};
1010
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001011static int smsc95xx_bind(struct usbnet *dev, struct usb_interface *intf)
1012{
1013 struct smsc95xx_priv *pdata = NULL;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001014 u32 val;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001015 int ret;
1016
1017 printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
1018
1019 ret = usbnet_get_endpoints(dev, intf);
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001020 check_warn_return(ret, "usbnet_get_endpoints failed: %d\n", ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001021
1022 dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc95xx_priv),
1023 GFP_KERNEL);
1024
1025 pdata = (struct smsc95xx_priv *)(dev->data[0]);
1026 if (!pdata) {
Joe Perches60b86752010-02-17 10:30:23 +00001027 netdev_warn(dev->net, "Unable to allocate struct smsc95xx_priv\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001028 return -ENOMEM;
1029 }
1030
1031 spin_lock_init(&pdata->mac_cr_lock);
1032
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001033 if (DEFAULT_TX_CSUM_ENABLE)
1034 dev->net->features |= NETIF_F_HW_CSUM;
1035 if (DEFAULT_RX_CSUM_ENABLE)
1036 dev->net->features |= NETIF_F_RXCSUM;
1037
1038 dev->net->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001039
Bernard Blackhamf4e8ab72010-10-18 13:16:39 +00001040 smsc95xx_init_mac_address(dev);
1041
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001042 /* Init all registers */
1043 ret = smsc95xx_reset(dev);
1044
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001045 /* detect device revision as different features may be available */
1046 ret = smsc95xx_read_reg(dev, ID_REV, &val);
1047 check_warn_return(ret, "Failed to read ID_REV: %d\n", ret);
1048 val >>= 16;
Steve Glendinning9ebca502012-11-22 08:05:23 +00001049
1050 if ((val == ID_REV_CHIP_ID_9500A_) || (val == ID_REV_CHIP_ID_9530_) ||
1051 (val == ID_REV_CHIP_ID_89530_) || (val == ID_REV_CHIP_ID_9730_))
1052 pdata->features = (FEATURE_8_WAKEUP_FILTERS |
1053 FEATURE_PHY_NLP_CROSSOVER |
1054 FEATURE_AUTOSUSPEND);
1055 else if (val == ID_REV_CHIP_ID_9512_)
1056 pdata->features = FEATURE_8_WAKEUP_FILTERS;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001057
Stephen Hemminger63e77b32009-03-20 19:35:58 +00001058 dev->net->netdev_ops = &smsc95xx_netdev_ops;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001059 dev->net->ethtool_ops = &smsc95xx_ethtool_ops;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001060 dev->net->flags |= IFF_MULTICAST;
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001061 dev->net->hard_header_len += SMSC95XX_TX_OVERHEAD_CSUM;
Stephane Fillod9bbf5662012-04-20 09:39:23 +00001062 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001063 return 0;
1064}
1065
1066static void smsc95xx_unbind(struct usbnet *dev, struct usb_interface *intf)
1067{
1068 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1069 if (pdata) {
Joe Perchesa475f602010-02-17 10:30:24 +00001070 netif_dbg(dev, ifdown, dev->net, "free pdata\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001071 kfree(pdata);
1072 pdata = NULL;
1073 dev->data[0] = 0;
1074 }
1075}
1076
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001077static u16 smsc_crc(const u8 *buffer, size_t len, int filter)
1078{
1079 return bitrev16(crc16(0xFFFF, buffer, len)) << ((filter % 2) * 16);
1080}
1081
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001082static int smsc95xx_enable_phy_wakeup_interrupts(struct usbnet *dev, u16 mask)
1083{
1084 struct mii_if_info *mii = &dev->mii;
1085 int ret;
1086
Joe Perches1e1d7412012-11-24 01:27:49 +00001087 netdev_dbg(dev->net, "enabling PHY wakeup interrupts\n");
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001088
1089 /* read to clear */
1090 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_SRC);
Joe Perches1e1d7412012-11-24 01:27:49 +00001091 check_warn_return(ret, "Error reading PHY_INT_SRC\n");
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001092
1093 /* enable interrupt source */
1094 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_MASK);
Joe Perches1e1d7412012-11-24 01:27:49 +00001095 check_warn_return(ret, "Error reading PHY_INT_MASK\n");
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001096
1097 ret |= mask;
1098
1099 smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_INT_MASK, ret);
1100
1101 return 0;
1102}
1103
1104static int smsc95xx_link_ok_nopm(struct usbnet *dev)
1105{
1106 struct mii_if_info *mii = &dev->mii;
1107 int ret;
1108
1109 /* first, a dummy read, needed to latch some MII phys */
1110 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
Joe Perches1e1d7412012-11-24 01:27:49 +00001111 check_warn_return(ret, "Error reading MII_BMSR\n");
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001112
1113 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
Joe Perches1e1d7412012-11-24 01:27:49 +00001114 check_warn_return(ret, "Error reading MII_BMSR\n");
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001115
1116 return !!(ret & BMSR_LSTATUS);
1117}
1118
Steve Glendinning319b95b2012-11-22 08:05:25 +00001119static int smsc95xx_enter_suspend0(struct usbnet *dev)
1120{
1121 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1122 u32 val;
1123 int ret;
1124
1125 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001126 check_warn_return(ret, "Error reading PM_CTRL\n");
Steve Glendinning319b95b2012-11-22 08:05:25 +00001127
1128 val &= (~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_));
1129 val |= PM_CTL_SUS_MODE_0;
1130
1131 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001132 check_warn_return(ret, "Error writing PM_CTRL\n");
Steve Glendinning319b95b2012-11-22 08:05:25 +00001133
1134 /* clear wol status */
1135 val &= ~PM_CTL_WUPS_;
1136 val |= PM_CTL_WUPS_WOL_;
1137
1138 /* enable energy detection */
1139 if (pdata->wolopts & WAKE_PHY)
1140 val |= PM_CTL_WUPS_ED_;
1141
1142 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001143 check_warn_return(ret, "Error writing PM_CTRL\n");
Steve Glendinning319b95b2012-11-22 08:05:25 +00001144
1145 /* read back PM_CTRL */
1146 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001147 check_warn_return(ret, "Error reading PM_CTRL\n");
Steve Glendinning319b95b2012-11-22 08:05:25 +00001148
Steve Glendinning319b95b2012-11-22 08:05:25 +00001149 return 0;
1150}
1151
1152static int smsc95xx_enter_suspend1(struct usbnet *dev)
1153{
1154 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1155 struct mii_if_info *mii = &dev->mii;
1156 u32 val;
1157 int ret;
1158
1159 /* reconfigure link pulse detection timing for
1160 * compatibility with non-standard link partners
1161 */
1162 if (pdata->features & FEATURE_PHY_NLP_CROSSOVER)
1163 smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_EDPD_CONFIG,
1164 PHY_EDPD_CONFIG_DEFAULT);
1165
1166 /* enable energy detect power-down mode */
1167 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS);
Joe Perches1e1d7412012-11-24 01:27:49 +00001168 check_warn_return(ret, "Error reading PHY_MODE_CTRL_STS\n");
Steve Glendinning319b95b2012-11-22 08:05:25 +00001169
1170 ret |= MODE_CTRL_STS_EDPWRDOWN_;
1171
1172 smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS, ret);
1173
1174 /* enter SUSPEND1 mode */
1175 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001176 check_warn_return(ret, "Error reading PM_CTRL\n");
Steve Glendinning319b95b2012-11-22 08:05:25 +00001177
1178 val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1179 val |= PM_CTL_SUS_MODE_1;
1180
1181 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001182 check_warn_return(ret, "Error writing PM_CTRL\n");
Steve Glendinning319b95b2012-11-22 08:05:25 +00001183
1184 /* clear wol status, enable energy detection */
1185 val &= ~PM_CTL_WUPS_;
1186 val |= (PM_CTL_WUPS_ED_ | PM_CTL_ED_EN_);
1187
1188 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001189 check_warn_return(ret, "Error writing PM_CTRL\n");
Steve Glendinning319b95b2012-11-22 08:05:25 +00001190
Steve Glendinning319b95b2012-11-22 08:05:25 +00001191 return 0;
1192}
1193
1194static int smsc95xx_enter_suspend2(struct usbnet *dev)
1195{
1196 u32 val;
1197 int ret;
1198
1199 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001200 check_warn_return(ret, "Error reading PM_CTRL\n");
Steve Glendinning319b95b2012-11-22 08:05:25 +00001201
1202 val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1203 val |= PM_CTL_SUS_MODE_2;
1204
1205 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001206 check_warn_return(ret, "Error writing PM_CTRL\n");
Steve Glendinning319b95b2012-11-22 08:05:25 +00001207
1208 return 0;
1209}
1210
Steve Glendinningb5a04472012-09-28 00:07:11 +00001211static int smsc95xx_suspend(struct usb_interface *intf, pm_message_t message)
1212{
1213 struct usbnet *dev = usb_get_intfdata(intf);
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001214 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001215 u32 val, link_up;
Steve Glendinningb5a04472012-09-28 00:07:11 +00001216 int ret;
Steve Glendinningb5a04472012-09-28 00:07:11 +00001217
Steve Glendinningb5a04472012-09-28 00:07:11 +00001218 ret = usbnet_suspend(intf, message);
Joe Perches1e1d7412012-11-24 01:27:49 +00001219 check_warn_return(ret, "usbnet_suspend error\n");
Steve Glendinningb5a04472012-09-28 00:07:11 +00001220
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001221 /* determine if link is up using only _nopm functions */
1222 link_up = smsc95xx_link_ok_nopm(dev);
1223
1224 /* if no wol options set, or if link is down and we're not waking on
1225 * PHY activity, enter lowest power SUSPEND2 mode
1226 */
1227 if (!(pdata->wolopts & SUPPORTED_WAKE) ||
1228 !(link_up || (pdata->wolopts & WAKE_PHY))) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001229 netdev_info(dev->net, "entering SUSPEND2 mode\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001230
1231 /* disable energy detect (link up) & wake up events */
Ming Leiec321152012-11-06 04:53:07 +00001232 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001233 check_warn_goto_done(ret, "Error reading WUCSR\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001234
1235 val &= ~(WUCSR_MPEN_ | WUCSR_WAKE_EN_);
1236
Ming Leiec321152012-11-06 04:53:07 +00001237 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001238 check_warn_goto_done(ret, "Error writing WUCSR\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001239
Ming Leiec321152012-11-06 04:53:07 +00001240 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001241 check_warn_goto_done(ret, "Error reading PM_CTRL\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001242
1243 val &= ~(PM_CTL_ED_EN_ | PM_CTL_WOL_EN_);
1244
Ming Leiec321152012-11-06 04:53:07 +00001245 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001246 check_warn_goto_done(ret, "Error writing PM_CTRL\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001247
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001248 ret = smsc95xx_enter_suspend2(dev);
1249 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001250 }
1251
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001252 if (pdata->wolopts & WAKE_PHY) {
1253 ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1254 (PHY_INT_MASK_ANEG_COMP_ | PHY_INT_MASK_LINK_DOWN_));
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001255 check_warn_goto_done(ret, "error enabling PHY wakeup ints\n");
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001256
1257 /* if link is down then configure EDPD and enter SUSPEND1,
1258 * otherwise enter SUSPEND0 below
1259 */
1260 if (!link_up) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001261 netdev_info(dev->net, "entering SUSPEND1 mode\n");
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001262 ret = smsc95xx_enter_suspend1(dev);
1263 goto done;
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001264 }
1265 }
1266
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001267 if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
Steve Glendinningeed9a722012-11-30 05:55:48 +00001268 u32 *filter_mask = kzalloc(sizeof(u32) * 32, GFP_KERNEL);
Ming Lei06a221b2012-11-06 04:53:06 +00001269 u32 command[2];
1270 u32 offset[2];
1271 u32 crc[4];
Steve Glendinning9ebca502012-11-22 08:05:23 +00001272 int wuff_filter_count =
1273 (pdata->features & FEATURE_8_WAKEUP_FILTERS) ?
1274 LAN9500A_WUFF_NUM : LAN9500_WUFF_NUM;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001275 int i, filter = 0;
1276
Steve Glendinningeed9a722012-11-30 05:55:48 +00001277 if (!filter_mask) {
1278 netdev_warn(dev->net, "Unable to allocate filter_mask\n");
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001279 ret = -ENOMEM;
1280 goto done;
Steve Glendinningeed9a722012-11-30 05:55:48 +00001281 }
1282
Ming Lei06a221b2012-11-06 04:53:06 +00001283 memset(command, 0, sizeof(command));
1284 memset(offset, 0, sizeof(offset));
1285 memset(crc, 0, sizeof(crc));
1286
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001287 if (pdata->wolopts & WAKE_BCAST) {
1288 const u8 bcast[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Joe Perches1e1d7412012-11-24 01:27:49 +00001289 netdev_info(dev->net, "enabling broadcast detection\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001290 filter_mask[filter * 4] = 0x003F;
1291 filter_mask[filter * 4 + 1] = 0x00;
1292 filter_mask[filter * 4 + 2] = 0x00;
1293 filter_mask[filter * 4 + 3] = 0x00;
1294 command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1295 offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1296 crc[filter/2] |= smsc_crc(bcast, 6, filter);
1297 filter++;
1298 }
1299
1300 if (pdata->wolopts & WAKE_MCAST) {
1301 const u8 mcast[] = {0x01, 0x00, 0x5E};
Joe Perches1e1d7412012-11-24 01:27:49 +00001302 netdev_info(dev->net, "enabling multicast detection\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001303 filter_mask[filter * 4] = 0x0007;
1304 filter_mask[filter * 4 + 1] = 0x00;
1305 filter_mask[filter * 4 + 2] = 0x00;
1306 filter_mask[filter * 4 + 3] = 0x00;
1307 command[filter/4] |= 0x09UL << ((filter % 4) * 8);
1308 offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1309 crc[filter/2] |= smsc_crc(mcast, 3, filter);
1310 filter++;
1311 }
1312
1313 if (pdata->wolopts & WAKE_ARP) {
1314 const u8 arp[] = {0x08, 0x06};
Joe Perches1e1d7412012-11-24 01:27:49 +00001315 netdev_info(dev->net, "enabling ARP detection\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001316 filter_mask[filter * 4] = 0x0003;
1317 filter_mask[filter * 4 + 1] = 0x00;
1318 filter_mask[filter * 4 + 2] = 0x00;
1319 filter_mask[filter * 4 + 3] = 0x00;
1320 command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1321 offset[filter/4] |= 0x0C << ((filter % 4) * 8);
1322 crc[filter/2] |= smsc_crc(arp, 2, filter);
1323 filter++;
1324 }
1325
1326 if (pdata->wolopts & WAKE_UCAST) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001327 netdev_info(dev->net, "enabling unicast detection\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001328 filter_mask[filter * 4] = 0x003F;
1329 filter_mask[filter * 4 + 1] = 0x00;
1330 filter_mask[filter * 4 + 2] = 0x00;
1331 filter_mask[filter * 4 + 3] = 0x00;
1332 command[filter/4] |= 0x01UL << ((filter % 4) * 8);
1333 offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1334 crc[filter/2] |= smsc_crc(dev->net->dev_addr, ETH_ALEN, filter);
1335 filter++;
1336 }
1337
Steve Glendinning9ebca502012-11-22 08:05:23 +00001338 for (i = 0; i < (wuff_filter_count * 4); i++) {
Ming Leiec321152012-11-06 04:53:07 +00001339 ret = smsc95xx_write_reg_nopm(dev, WUFF, filter_mask[i]);
Ming Lei06a221b2012-11-06 04:53:06 +00001340 if (ret < 0)
1341 kfree(filter_mask);
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001342 check_warn_goto_done(ret, "Error writing WUFF\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001343 }
Ming Lei06a221b2012-11-06 04:53:06 +00001344 kfree(filter_mask);
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001345
Steve Glendinning9ebca502012-11-22 08:05:23 +00001346 for (i = 0; i < (wuff_filter_count / 4); i++) {
Ming Leiec321152012-11-06 04:53:07 +00001347 ret = smsc95xx_write_reg_nopm(dev, WUFF, command[i]);
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001348 check_warn_goto_done(ret, "Error writing WUFF\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001349 }
1350
Steve Glendinning9ebca502012-11-22 08:05:23 +00001351 for (i = 0; i < (wuff_filter_count / 4); i++) {
Ming Leiec321152012-11-06 04:53:07 +00001352 ret = smsc95xx_write_reg_nopm(dev, WUFF, offset[i]);
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001353 check_warn_goto_done(ret, "Error writing WUFF\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001354 }
1355
Steve Glendinning9ebca502012-11-22 08:05:23 +00001356 for (i = 0; i < (wuff_filter_count / 2); i++) {
Ming Leiec321152012-11-06 04:53:07 +00001357 ret = smsc95xx_write_reg_nopm(dev, WUFF, crc[i]);
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001358 check_warn_goto_done(ret, "Error writing WUFF\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001359 }
1360
1361 /* clear any pending pattern match packet status */
Ming Leiec321152012-11-06 04:53:07 +00001362 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001363 check_warn_goto_done(ret, "Error reading WUCSR\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001364
1365 val |= WUCSR_WUFR_;
1366
Ming Leiec321152012-11-06 04:53:07 +00001367 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001368 check_warn_goto_done(ret, "Error writing WUCSR\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001369 }
1370
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001371 if (pdata->wolopts & WAKE_MAGIC) {
1372 /* clear any pending magic packet status */
Ming Leiec321152012-11-06 04:53:07 +00001373 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001374 check_warn_goto_done(ret, "Error reading WUCSR\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001375
1376 val |= WUCSR_MPR_;
1377
Ming Leiec321152012-11-06 04:53:07 +00001378 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001379 check_warn_goto_done(ret, "Error writing WUCSR\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001380 }
1381
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001382 /* enable/disable wakeup sources */
Ming Leiec321152012-11-06 04:53:07 +00001383 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001384 check_warn_goto_done(ret, "Error reading WUCSR\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001385
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001386 if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001387 netdev_info(dev->net, "enabling pattern match wakeup\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001388 val |= WUCSR_WAKE_EN_;
1389 } else {
Joe Perches1e1d7412012-11-24 01:27:49 +00001390 netdev_info(dev->net, "disabling pattern match wakeup\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001391 val &= ~WUCSR_WAKE_EN_;
1392 }
1393
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001394 if (pdata->wolopts & WAKE_MAGIC) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001395 netdev_info(dev->net, "enabling magic packet wakeup\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001396 val |= WUCSR_MPEN_;
1397 } else {
Joe Perches1e1d7412012-11-24 01:27:49 +00001398 netdev_info(dev->net, "disabling magic packet wakeup\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001399 val &= ~WUCSR_MPEN_;
1400 }
1401
Ming Leiec321152012-11-06 04:53:07 +00001402 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001403 check_warn_goto_done(ret, "Error writing WUCSR\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001404
1405 /* enable wol wakeup source */
Ming Leiec321152012-11-06 04:53:07 +00001406 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001407 check_warn_goto_done(ret, "Error reading PM_CTRL\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001408
1409 val |= PM_CTL_WOL_EN_;
1410
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001411 /* phy energy detect wakeup source */
1412 if (pdata->wolopts & WAKE_PHY)
1413 val |= PM_CTL_ED_EN_;
1414
Ming Leiec321152012-11-06 04:53:07 +00001415 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001416 check_warn_goto_done(ret, "Error writing PM_CTRL\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001417
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001418 /* enable receiver to enable frame reception */
Ming Leiec321152012-11-06 04:53:07 +00001419 smsc95xx_start_rx_path(dev, 1);
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001420
1421 /* some wol options are enabled, so enter SUSPEND0 */
Joe Perches1e1d7412012-11-24 01:27:49 +00001422 netdev_info(dev->net, "entering SUSPEND0 mode\n");
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001423 ret = smsc95xx_enter_suspend0(dev);
1424
1425done:
1426 if (ret)
1427 usbnet_resume(intf);
1428 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001429}
1430
1431static int smsc95xx_resume(struct usb_interface *intf)
1432{
1433 struct usbnet *dev = usb_get_intfdata(intf);
1434 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1435 int ret;
1436 u32 val;
1437
1438 BUG_ON(!dev);
1439
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001440 if (pdata->wolopts) {
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001441 /* clear wake-up sources */
Ming Leiec321152012-11-06 04:53:07 +00001442 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001443 check_warn_return(ret, "Error reading WUCSR\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001444
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001445 val &= ~(WUCSR_WAKE_EN_ | WUCSR_MPEN_);
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001446
Ming Leiec321152012-11-06 04:53:07 +00001447 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001448 check_warn_return(ret, "Error writing WUCSR\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001449
1450 /* clear wake-up status */
Ming Leiec321152012-11-06 04:53:07 +00001451 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001452 check_warn_return(ret, "Error reading PM_CTRL\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001453
1454 val &= ~PM_CTL_WOL_EN_;
1455 val |= PM_CTL_WUPS_;
1456
Ming Leiec321152012-11-06 04:53:07 +00001457 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Joe Perches1e1d7412012-11-24 01:27:49 +00001458 check_warn_return(ret, "Error writing PM_CTRL\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001459 }
1460
Steve Glendinningaf3d7c12012-11-22 08:05:22 +00001461 ret = usbnet_resume(intf);
Joe Perches1e1d7412012-11-24 01:27:49 +00001462 check_warn_return(ret, "usbnet_resume error\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001463
Steve Glendinningb5a04472012-09-28 00:07:11 +00001464 return 0;
1465}
1466
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001467static void smsc95xx_rx_csum_offload(struct sk_buff *skb)
1468{
1469 skb->csum = *(u16 *)(skb_tail_pointer(skb) - 2);
1470 skb->ip_summed = CHECKSUM_COMPLETE;
1471 skb_trim(skb, skb->len - 2);
1472}
1473
1474static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
1475{
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001476 while (skb->len > 0) {
1477 u32 header, align_count;
1478 struct sk_buff *ax_skb;
1479 unsigned char *packet;
1480 u16 size;
1481
1482 memcpy(&header, skb->data, sizeof(header));
1483 le32_to_cpus(&header);
1484 skb_pull(skb, 4 + NET_IP_ALIGN);
1485 packet = skb->data;
1486
1487 /* get the packet length */
1488 size = (u16)((header & RX_STS_FL_) >> 16);
1489 align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4;
1490
1491 if (unlikely(header & RX_STS_ES_)) {
Joe Perchesa475f602010-02-17 10:30:24 +00001492 netif_dbg(dev, rx_err, dev->net,
1493 "Error header=0x%08x\n", header);
Herbert Xu80667ac2009-06-29 16:53:00 +00001494 dev->net->stats.rx_errors++;
1495 dev->net->stats.rx_dropped++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001496
1497 if (header & RX_STS_CRC_) {
Herbert Xu80667ac2009-06-29 16:53:00 +00001498 dev->net->stats.rx_crc_errors++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001499 } else {
1500 if (header & (RX_STS_TL_ | RX_STS_RF_))
Herbert Xu80667ac2009-06-29 16:53:00 +00001501 dev->net->stats.rx_frame_errors++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001502
1503 if ((header & RX_STS_LE_) &&
1504 (!(header & RX_STS_FT_)))
Herbert Xu80667ac2009-06-29 16:53:00 +00001505 dev->net->stats.rx_length_errors++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001506 }
1507 } else {
1508 /* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
1509 if (unlikely(size > (ETH_FRAME_LEN + 12))) {
Joe Perchesa475f602010-02-17 10:30:24 +00001510 netif_dbg(dev, rx_err, dev->net,
1511 "size err header=0x%08x\n", header);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001512 return 0;
1513 }
1514
1515 /* last frame in this batch */
1516 if (skb->len == size) {
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001517 if (dev->net->features & NETIF_F_RXCSUM)
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001518 smsc95xx_rx_csum_offload(skb);
Peter Korsgaarddf18acc2009-05-13 10:10:41 +00001519 skb_trim(skb, skb->len - 4); /* remove fcs */
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001520 skb->truesize = size + sizeof(struct sk_buff);
1521
1522 return 1;
1523 }
1524
1525 ax_skb = skb_clone(skb, GFP_ATOMIC);
1526 if (unlikely(!ax_skb)) {
Joe Perches60b86752010-02-17 10:30:23 +00001527 netdev_warn(dev->net, "Error allocating skb\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001528 return 0;
1529 }
1530
1531 ax_skb->len = size;
1532 ax_skb->data = packet;
1533 skb_set_tail_pointer(ax_skb, size);
1534
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001535 if (dev->net->features & NETIF_F_RXCSUM)
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001536 smsc95xx_rx_csum_offload(ax_skb);
Peter Korsgaarddf18acc2009-05-13 10:10:41 +00001537 skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001538 ax_skb->truesize = size + sizeof(struct sk_buff);
1539
1540 usbnet_skb_return(dev, ax_skb);
1541 }
1542
1543 skb_pull(skb, size);
1544
1545 /* padding bytes before the next frame starts */
1546 if (skb->len)
1547 skb_pull(skb, align_count);
1548 }
1549
1550 if (unlikely(skb->len < 0)) {
Joe Perches60b86752010-02-17 10:30:23 +00001551 netdev_warn(dev->net, "invalid rx length<0 %d\n", skb->len);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001552 return 0;
1553 }
1554
1555 return 1;
1556}
1557
Steve Glendinningf7b29272008-11-20 04:19:21 -08001558static u32 smsc95xx_calc_csum_preamble(struct sk_buff *skb)
1559{
Michał Mirosław55508d62010-12-14 15:24:08 +00001560 u16 low_16 = (u16)skb_checksum_start_offset(skb);
1561 u16 high_16 = low_16 + skb->csum_offset;
Steve Glendinningf7b29272008-11-20 04:19:21 -08001562 return (high_16 << 16) | low_16;
1563}
1564
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001565static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,
1566 struct sk_buff *skb, gfp_t flags)
1567{
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001568 bool csum = skb->ip_summed == CHECKSUM_PARTIAL;
Steve Glendinningf7b29272008-11-20 04:19:21 -08001569 int overhead = csum ? SMSC95XX_TX_OVERHEAD_CSUM : SMSC95XX_TX_OVERHEAD;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001570 u32 tx_cmd_a, tx_cmd_b;
1571
Steve Glendinningf7b29272008-11-20 04:19:21 -08001572 /* We do not advertise SG, so skbs should be already linearized */
1573 BUG_ON(skb_shinfo(skb)->nr_frags);
1574
1575 if (skb_headroom(skb) < overhead) {
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001576 struct sk_buff *skb2 = skb_copy_expand(skb,
Steve Glendinningf7b29272008-11-20 04:19:21 -08001577 overhead, 0, flags);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001578 dev_kfree_skb_any(skb);
1579 skb = skb2;
1580 if (!skb)
1581 return NULL;
1582 }
1583
Steve Glendinningf7b29272008-11-20 04:19:21 -08001584 if (csum) {
Steve Glendinning11bc3082010-03-18 22:18:41 -07001585 if (skb->len <= 45) {
1586 /* workaround - hardware tx checksum does not work
1587 * properly with extremely small packets */
Michał Mirosław55508d62010-12-14 15:24:08 +00001588 long csstart = skb_checksum_start_offset(skb);
Steve Glendinning11bc3082010-03-18 22:18:41 -07001589 __wsum calc = csum_partial(skb->data + csstart,
1590 skb->len - csstart, 0);
1591 *((__sum16 *)(skb->data + csstart
1592 + skb->csum_offset)) = csum_fold(calc);
1593
1594 csum = false;
1595 } else {
1596 u32 csum_preamble = smsc95xx_calc_csum_preamble(skb);
1597 skb_push(skb, 4);
Steve Glendinning00acda62012-11-02 00:44:20 +00001598 cpu_to_le32s(&csum_preamble);
Steve Glendinning11bc3082010-03-18 22:18:41 -07001599 memcpy(skb->data, &csum_preamble, 4);
1600 }
Steve Glendinningf7b29272008-11-20 04:19:21 -08001601 }
1602
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001603 skb_push(skb, 4);
1604 tx_cmd_b = (u32)(skb->len - 4);
Steve Glendinningf7b29272008-11-20 04:19:21 -08001605 if (csum)
1606 tx_cmd_b |= TX_CMD_B_CSUM_ENABLE;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001607 cpu_to_le32s(&tx_cmd_b);
1608 memcpy(skb->data, &tx_cmd_b, 4);
1609
1610 skb_push(skb, 4);
1611 tx_cmd_a = (u32)(skb->len - 8) | TX_CMD_A_FIRST_SEG_ |
1612 TX_CMD_A_LAST_SEG_;
1613 cpu_to_le32s(&tx_cmd_a);
1614 memcpy(skb->data, &tx_cmd_a, 4);
1615
1616 return skb;
1617}
1618
1619static const struct driver_info smsc95xx_info = {
1620 .description = "smsc95xx USB 2.0 Ethernet",
1621 .bind = smsc95xx_bind,
1622 .unbind = smsc95xx_unbind,
1623 .link_reset = smsc95xx_link_reset,
1624 .reset = smsc95xx_reset,
1625 .rx_fixup = smsc95xx_rx_fixup,
1626 .tx_fixup = smsc95xx_tx_fixup,
1627 .status = smsc95xx_status,
Paolo Pisati07d69d42012-04-23 04:05:20 +00001628 .flags = FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001629};
1630
1631static const struct usb_device_id products[] = {
1632 {
1633 /* SMSC9500 USB Ethernet Device */
1634 USB_DEVICE(0x0424, 0x9500),
1635 .driver_info = (unsigned long) &smsc95xx_info,
1636 },
Steve Glendinning726474b2009-05-01 06:07:22 +00001637 {
Steve Glendinning6f41d122009-09-22 05:13:02 +00001638 /* SMSC9505 USB Ethernet Device */
1639 USB_DEVICE(0x0424, 0x9505),
1640 .driver_info = (unsigned long) &smsc95xx_info,
1641 },
1642 {
1643 /* SMSC9500A USB Ethernet Device */
1644 USB_DEVICE(0x0424, 0x9E00),
1645 .driver_info = (unsigned long) &smsc95xx_info,
1646 },
1647 {
1648 /* SMSC9505A USB Ethernet Device */
1649 USB_DEVICE(0x0424, 0x9E01),
1650 .driver_info = (unsigned long) &smsc95xx_info,
1651 },
1652 {
Steve Glendinning726474b2009-05-01 06:07:22 +00001653 /* SMSC9512/9514 USB Hub & Ethernet Device */
1654 USB_DEVICE(0x0424, 0xec00),
1655 .driver_info = (unsigned long) &smsc95xx_info,
1656 },
Steve Glendinning6f41d122009-09-22 05:13:02 +00001657 {
1658 /* SMSC9500 USB Ethernet Device (SAL10) */
1659 USB_DEVICE(0x0424, 0x9900),
1660 .driver_info = (unsigned long) &smsc95xx_info,
1661 },
1662 {
1663 /* SMSC9505 USB Ethernet Device (SAL10) */
1664 USB_DEVICE(0x0424, 0x9901),
1665 .driver_info = (unsigned long) &smsc95xx_info,
1666 },
1667 {
1668 /* SMSC9500A USB Ethernet Device (SAL10) */
1669 USB_DEVICE(0x0424, 0x9902),
1670 .driver_info = (unsigned long) &smsc95xx_info,
1671 },
1672 {
1673 /* SMSC9505A USB Ethernet Device (SAL10) */
1674 USB_DEVICE(0x0424, 0x9903),
1675 .driver_info = (unsigned long) &smsc95xx_info,
1676 },
1677 {
1678 /* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */
1679 USB_DEVICE(0x0424, 0x9904),
1680 .driver_info = (unsigned long) &smsc95xx_info,
1681 },
1682 {
1683 /* SMSC9500A USB Ethernet Device (HAL) */
1684 USB_DEVICE(0x0424, 0x9905),
1685 .driver_info = (unsigned long) &smsc95xx_info,
1686 },
1687 {
1688 /* SMSC9505A USB Ethernet Device (HAL) */
1689 USB_DEVICE(0x0424, 0x9906),
1690 .driver_info = (unsigned long) &smsc95xx_info,
1691 },
1692 {
1693 /* SMSC9500 USB Ethernet Device (Alternate ID) */
1694 USB_DEVICE(0x0424, 0x9907),
1695 .driver_info = (unsigned long) &smsc95xx_info,
1696 },
1697 {
1698 /* SMSC9500A USB Ethernet Device (Alternate ID) */
1699 USB_DEVICE(0x0424, 0x9908),
1700 .driver_info = (unsigned long) &smsc95xx_info,
1701 },
1702 {
1703 /* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */
1704 USB_DEVICE(0x0424, 0x9909),
1705 .driver_info = (unsigned long) &smsc95xx_info,
1706 },
Steve Glendinning88edaa42011-04-10 18:59:27 -07001707 {
1708 /* SMSC LAN9530 USB Ethernet Device */
1709 USB_DEVICE(0x0424, 0x9530),
1710 .driver_info = (unsigned long) &smsc95xx_info,
1711 },
1712 {
1713 /* SMSC LAN9730 USB Ethernet Device */
1714 USB_DEVICE(0x0424, 0x9730),
1715 .driver_info = (unsigned long) &smsc95xx_info,
1716 },
1717 {
1718 /* SMSC LAN89530 USB Ethernet Device */
1719 USB_DEVICE(0x0424, 0x9E08),
1720 .driver_info = (unsigned long) &smsc95xx_info,
1721 },
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001722 { }, /* END */
1723};
1724MODULE_DEVICE_TABLE(usb, products);
1725
1726static struct usb_driver smsc95xx_driver = {
1727 .name = "smsc95xx",
1728 .id_table = products,
1729 .probe = usbnet_probe,
Steve Glendinningb5a04472012-09-28 00:07:11 +00001730 .suspend = smsc95xx_suspend,
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001731 .resume = smsc95xx_resume,
1732 .reset_resume = smsc95xx_resume,
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001733 .disconnect = usbnet_disconnect,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001734 .disable_hub_initiated_lpm = 1,
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001735};
1736
Greg Kroah-Hartmand632eb12011-11-18 09:44:20 -08001737module_usb_driver(smsc95xx_driver);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001738
1739MODULE_AUTHOR("Nancy Lin");
Steve Glendinning90b24cf2012-04-16 12:13:29 +01001740MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001741MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices");
1742MODULE_LICENSE("GPL");