blob: dc989a8b5afbd62b07cd65a2fa5edde67d3cc5fb [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
Jeff Kirsher9cb00072013-12-06 06:28:46 -080016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Steve Glendinning2f7ca802008-10-02 05:27:57 +000017 *
18 *****************************************************************************/
19
20#include <linux/module.h>
21#include <linux/kmod.h>
Steve Glendinning2f7ca802008-10-02 05:27:57 +000022#include <linux/netdevice.h>
23#include <linux/etherdevice.h>
24#include <linux/ethtool.h>
25#include <linux/mii.h>
26#include <linux/usb.h>
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +000027#include <linux/bitrev.h>
28#include <linux/crc16.h>
Steve Glendinning2f7ca802008-10-02 05:27:57 +000029#include <linux/crc32.h>
30#include <linux/usb/usbnet.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Arnd Bergmannc4895652016-04-29 09:05:59 +020032#include <linux/of_net.h>
Steve Glendinning2f7ca802008-10-02 05:27:57 +000033#include "smsc95xx.h"
34
35#define SMSC_CHIPNAME "smsc95xx"
Steve Glendinningf7b29272008-11-20 04:19:21 -080036#define SMSC_DRIVER_VERSION "1.0.4"
Steve Glendinning2f7ca802008-10-02 05:27:57 +000037#define HS_USB_PKT_SIZE (512)
38#define FS_USB_PKT_SIZE (64)
39#define DEFAULT_HS_BURST_CAP_SIZE (16 * 1024 + 5 * HS_USB_PKT_SIZE)
40#define DEFAULT_FS_BURST_CAP_SIZE (6 * 1024 + 33 * FS_USB_PKT_SIZE)
41#define DEFAULT_BULK_IN_DELAY (0x00002000)
42#define MAX_SINGLE_PACKET_SIZE (2048)
43#define LAN95XX_EEPROM_MAGIC (0x9500)
44#define EEPROM_MAC_OFFSET (0x01)
Steve Glendinningf7b29272008-11-20 04:19:21 -080045#define DEFAULT_TX_CSUM_ENABLE (true)
Steve Glendinning2f7ca802008-10-02 05:27:57 +000046#define DEFAULT_RX_CSUM_ENABLE (true)
47#define SMSC95XX_INTERNAL_PHY_ID (1)
48#define SMSC95XX_TX_OVERHEAD (8)
Steve Glendinningf7b29272008-11-20 04:19:21 -080049#define SMSC95XX_TX_OVERHEAD_CSUM (12)
Steve Glendinninge5e3af82012-11-22 08:05:24 +000050#define SUPPORTED_WAKE (WAKE_PHY | WAKE_UCAST | WAKE_BCAST | \
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +000051 WAKE_MCAST | WAKE_ARP | WAKE_MAGIC)
Steve Glendinning2f7ca802008-10-02 05:27:57 +000052
Steve Glendinning9ebca502012-11-22 08:05:23 +000053#define FEATURE_8_WAKEUP_FILTERS (0x01)
54#define FEATURE_PHY_NLP_CROSSOVER (0x02)
Ming Leieb970ff2013-02-22 03:05:05 +000055#define FEATURE_REMOTE_WAKEUP (0x04)
Steve Glendinning9ebca502012-11-22 08:05:23 +000056
Steve Glendinningb2d4b152013-01-03 03:00:16 +000057#define SUSPEND_SUSPEND0 (0x01)
58#define SUSPEND_SUSPEND1 (0x02)
59#define SUSPEND_SUSPEND2 (0x04)
60#define SUSPEND_SUSPEND3 (0x08)
61#define SUSPEND_ALLMODES (SUSPEND_SUSPEND0 | SUSPEND_SUSPEND1 | \
62 SUSPEND_SUSPEND2 | SUSPEND_SUSPEND3)
63
Christoph Fritzd69d1692016-05-26 04:06:47 +020064#define CARRIER_CHECK_DELAY (2 * HZ)
65
Steve Glendinning2f7ca802008-10-02 05:27:57 +000066struct smsc95xx_priv {
67 u32 mac_cr;
Marc Zyngier3c0f3c62011-03-18 03:53:58 +000068 u32 hash_hi;
69 u32 hash_lo;
Steve Glendinninge0e474a2012-09-28 00:07:12 +000070 u32 wolopts;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000071 spinlock_t mac_cr_lock;
Steve Glendinning9ebca502012-11-22 08:05:23 +000072 u8 features;
Steve Glendinningb2d4b152013-01-03 03:00:16 +000073 u8 suspend_flags;
Christoph Fritzd69d1692016-05-26 04:06:47 +020074 bool link_ok;
75 struct delayed_work carrier_check;
76 struct usbnet *dev;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000077};
78
Rusty Russelleb939922011-12-19 14:08:01 +000079static bool turbo_mode = true;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000080module_param(turbo_mode, bool, 0644);
81MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
82
Ming Leiec321152012-11-06 04:53:07 +000083static int __must_check __smsc95xx_read_reg(struct usbnet *dev, u32 index,
84 u32 *data, int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +000085{
Ming Lei72108fd2012-10-24 19:47:04 +000086 u32 buf;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000087 int ret;
Ming Leiec321152012-11-06 04:53:07 +000088 int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
Steve Glendinning2f7ca802008-10-02 05:27:57 +000089
90 BUG_ON(!dev);
91
Ming Leiec321152012-11-06 04:53:07 +000092 if (!in_pm)
93 fn = usbnet_read_cmd;
94 else
95 fn = usbnet_read_cmd_nopm;
96
97 ret = fn(dev, USB_VENDOR_REQUEST_READ_REGISTER, USB_DIR_IN
98 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
99 0, index, &buf, 4);
Dan Carpenter5a36b682016-05-04 09:22:01 +0300100 if (unlikely(ret < 0)) {
Joe Perches1e1d7412012-11-24 01:27:49 +0000101 netdev_warn(dev->net, "Failed to read reg index 0x%08x: %d\n",
102 index, ret);
Dan Carpenter5a36b682016-05-04 09:22:01 +0300103 return ret;
104 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000105
Ming Lei72108fd2012-10-24 19:47:04 +0000106 le32_to_cpus(&buf);
107 *data = buf;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000108
109 return ret;
110}
111
Ming Leiec321152012-11-06 04:53:07 +0000112static int __must_check __smsc95xx_write_reg(struct usbnet *dev, u32 index,
113 u32 data, int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000114{
Ming Lei72108fd2012-10-24 19:47:04 +0000115 u32 buf;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000116 int ret;
Ming Leiec321152012-11-06 04:53:07 +0000117 int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000118
119 BUG_ON(!dev);
120
Ming Leiec321152012-11-06 04:53:07 +0000121 if (!in_pm)
122 fn = usbnet_write_cmd;
123 else
124 fn = usbnet_write_cmd_nopm;
125
Ming Lei72108fd2012-10-24 19:47:04 +0000126 buf = data;
127 cpu_to_le32s(&buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000128
Ming Leiec321152012-11-06 04:53:07 +0000129 ret = fn(dev, USB_VENDOR_REQUEST_WRITE_REGISTER, USB_DIR_OUT
130 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
131 0, index, &buf, 4);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000132 if (unlikely(ret < 0))
Joe Perches1e1d7412012-11-24 01:27:49 +0000133 netdev_warn(dev->net, "Failed to write reg index 0x%08x: %d\n",
134 index, ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000135
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000136 return ret;
137}
138
Ming Leiec321152012-11-06 04:53:07 +0000139static int __must_check smsc95xx_read_reg_nopm(struct usbnet *dev, u32 index,
140 u32 *data)
141{
142 return __smsc95xx_read_reg(dev, index, data, 1);
143}
144
145static int __must_check smsc95xx_write_reg_nopm(struct usbnet *dev, u32 index,
146 u32 data)
147{
148 return __smsc95xx_write_reg(dev, index, data, 1);
149}
150
151static int __must_check smsc95xx_read_reg(struct usbnet *dev, u32 index,
152 u32 *data)
153{
154 return __smsc95xx_read_reg(dev, index, data, 0);
155}
156
157static int __must_check smsc95xx_write_reg(struct usbnet *dev, u32 index,
158 u32 data)
159{
160 return __smsc95xx_write_reg(dev, index, data, 0);
161}
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000162
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000163/* Loop until the read is completed with timeout
164 * called with phy_mutex held */
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000165static int __must_check __smsc95xx_phy_wait_not_busy(struct usbnet *dev,
166 int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000167{
168 unsigned long start_time = jiffies;
169 u32 val;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000170 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000171
172 do {
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000173 ret = __smsc95xx_read_reg(dev, MII_ADDR, &val, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000174 if (ret < 0) {
175 netdev_warn(dev->net, "Error reading MII_ACCESS\n");
176 return ret;
177 }
178
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000179 if (!(val & MII_BUSY_))
180 return 0;
181 } while (!time_after(jiffies, start_time + HZ));
182
183 return -EIO;
184}
185
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000186static int __smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx,
187 int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000188{
189 struct usbnet *dev = netdev_priv(netdev);
190 u32 val, addr;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000191 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000192
193 mutex_lock(&dev->phy_mutex);
194
195 /* confirm MII not busy */
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000196 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000197 if (ret < 0) {
198 netdev_warn(dev->net, "MII is busy in smsc95xx_mdio_read\n");
199 goto done;
200 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000201
202 /* set the address, index & direction (read from PHY) */
203 phy_id &= dev->mii.phy_id_mask;
204 idx &= dev->mii.reg_num_mask;
Steve Glendinning80928802012-11-06 00:08:53 +0000205 addr = (phy_id << 11) | (idx << 6) | MII_READ_ | MII_BUSY_;
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000206 ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000207 if (ret < 0) {
208 netdev_warn(dev->net, "Error writing MII_ADDR\n");
209 goto done;
210 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000211
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000212 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000213 if (ret < 0) {
214 netdev_warn(dev->net, "Timed out reading MII reg %02X\n", idx);
215 goto done;
216 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000217
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000218 ret = __smsc95xx_read_reg(dev, MII_DATA, &val, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000219 if (ret < 0) {
220 netdev_warn(dev->net, "Error reading MII_DATA\n");
221 goto done;
222 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000223
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000224 ret = (u16)(val & 0xFFFF);
225
226done:
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000227 mutex_unlock(&dev->phy_mutex);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000228 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000229}
230
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000231static void __smsc95xx_mdio_write(struct net_device *netdev, int phy_id,
232 int idx, int regval, int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000233{
234 struct usbnet *dev = netdev_priv(netdev);
235 u32 val, addr;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000236 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000237
238 mutex_lock(&dev->phy_mutex);
239
240 /* confirm MII not busy */
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000241 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000242 if (ret < 0) {
243 netdev_warn(dev->net, "MII is busy in smsc95xx_mdio_write\n");
244 goto done;
245 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000246
247 val = regval;
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000248 ret = __smsc95xx_write_reg(dev, MII_DATA, val, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000249 if (ret < 0) {
250 netdev_warn(dev->net, "Error writing MII_DATA\n");
251 goto done;
252 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000253
254 /* set the address, index & direction (write to PHY) */
255 phy_id &= dev->mii.phy_id_mask;
256 idx &= dev->mii.reg_num_mask;
Steve Glendinning80928802012-11-06 00:08:53 +0000257 addr = (phy_id << 11) | (idx << 6) | MII_WRITE_ | MII_BUSY_;
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000258 ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000259 if (ret < 0) {
260 netdev_warn(dev->net, "Error writing MII_ADDR\n");
261 goto done;
262 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000263
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000264 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000265 if (ret < 0) {
266 netdev_warn(dev->net, "Timed out writing MII reg %02X\n", idx);
267 goto done;
268 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000269
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000270done:
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000271 mutex_unlock(&dev->phy_mutex);
272}
273
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000274static int smsc95xx_mdio_read_nopm(struct net_device *netdev, int phy_id,
275 int idx)
276{
277 return __smsc95xx_mdio_read(netdev, phy_id, idx, 1);
278}
279
280static void smsc95xx_mdio_write_nopm(struct net_device *netdev, int phy_id,
281 int idx, int regval)
282{
283 __smsc95xx_mdio_write(netdev, phy_id, idx, regval, 1);
284}
285
286static int smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
287{
288 return __smsc95xx_mdio_read(netdev, phy_id, idx, 0);
289}
290
291static void smsc95xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
292 int regval)
293{
294 __smsc95xx_mdio_write(netdev, phy_id, idx, regval, 0);
295}
296
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000297static int __must_check smsc95xx_wait_eeprom(struct usbnet *dev)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000298{
299 unsigned long start_time = jiffies;
300 u32 val;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000301 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000302
303 do {
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000304 ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
Steve Glendinningb052e072012-11-30 05:55:52 +0000305 if (ret < 0) {
306 netdev_warn(dev->net, "Error reading E2P_CMD\n");
307 return ret;
308 }
309
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000310 if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_))
311 break;
312 udelay(40);
313 } while (!time_after(jiffies, start_time + HZ));
314
315 if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) {
Joe Perches60b86752010-02-17 10:30:23 +0000316 netdev_warn(dev->net, "EEPROM read operation timeout\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000317 return -EIO;
318 }
319
320 return 0;
321}
322
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000323static int __must_check smsc95xx_eeprom_confirm_not_busy(struct usbnet *dev)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000324{
325 unsigned long start_time = jiffies;
326 u32 val;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000327 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000328
329 do {
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000330 ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
Steve Glendinningb052e072012-11-30 05:55:52 +0000331 if (ret < 0) {
332 netdev_warn(dev->net, "Error reading E2P_CMD\n");
333 return ret;
334 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000335
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000336 if (!(val & E2P_CMD_BUSY_))
337 return 0;
338
339 udelay(40);
340 } while (!time_after(jiffies, start_time + HZ));
341
Joe Perches60b86752010-02-17 10:30:23 +0000342 netdev_warn(dev->net, "EEPROM is busy\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000343 return -EIO;
344}
345
346static int smsc95xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
347 u8 *data)
348{
349 u32 val;
350 int i, ret;
351
352 BUG_ON(!dev);
353 BUG_ON(!data);
354
355 ret = smsc95xx_eeprom_confirm_not_busy(dev);
356 if (ret)
357 return ret;
358
359 for (i = 0; i < length; i++) {
360 val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000361 ret = smsc95xx_write_reg(dev, E2P_CMD, val);
Steve Glendinningb052e072012-11-30 05:55:52 +0000362 if (ret < 0) {
363 netdev_warn(dev->net, "Error writing E2P_CMD\n");
364 return ret;
365 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000366
367 ret = smsc95xx_wait_eeprom(dev);
368 if (ret < 0)
369 return ret;
370
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000371 ret = smsc95xx_read_reg(dev, E2P_DATA, &val);
Steve Glendinningb052e072012-11-30 05:55:52 +0000372 if (ret < 0) {
373 netdev_warn(dev->net, "Error reading E2P_DATA\n");
374 return ret;
375 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000376
377 data[i] = val & 0xFF;
378 offset++;
379 }
380
381 return 0;
382}
383
384static int smsc95xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
385 u8 *data)
386{
387 u32 val;
388 int i, ret;
389
390 BUG_ON(!dev);
391 BUG_ON(!data);
392
393 ret = smsc95xx_eeprom_confirm_not_busy(dev);
394 if (ret)
395 return ret;
396
397 /* Issue write/erase enable command */
398 val = E2P_CMD_BUSY_ | E2P_CMD_EWEN_;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000399 ret = smsc95xx_write_reg(dev, E2P_CMD, val);
Steve Glendinningb052e072012-11-30 05:55:52 +0000400 if (ret < 0) {
401 netdev_warn(dev->net, "Error writing E2P_DATA\n");
402 return ret;
403 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000404
405 ret = smsc95xx_wait_eeprom(dev);
406 if (ret < 0)
407 return ret;
408
409 for (i = 0; i < length; i++) {
410
411 /* Fill data register */
412 val = data[i];
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000413 ret = smsc95xx_write_reg(dev, E2P_DATA, val);
Steve Glendinningb052e072012-11-30 05:55:52 +0000414 if (ret < 0) {
415 netdev_warn(dev->net, "Error writing E2P_DATA\n");
416 return ret;
417 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000418
419 /* Send "write" command */
420 val = E2P_CMD_BUSY_ | E2P_CMD_WRITE_ | (offset & E2P_CMD_ADDR_);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000421 ret = smsc95xx_write_reg(dev, E2P_CMD, val);
Steve Glendinningb052e072012-11-30 05:55:52 +0000422 if (ret < 0) {
423 netdev_warn(dev->net, "Error writing E2P_CMD\n");
424 return ret;
425 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000426
427 ret = smsc95xx_wait_eeprom(dev);
428 if (ret < 0)
429 return ret;
430
431 offset++;
432 }
433
434 return 0;
435}
436
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000437static int __must_check smsc95xx_write_reg_async(struct usbnet *dev, u16 index,
Steve Glendinning7b9e7582012-12-10 01:03:08 +0000438 u32 data)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000439{
Steve Glendinning1d74a6b2008-10-09 14:34:47 -0700440 const u16 size = 4;
Steve Glendinning7b9e7582012-12-10 01:03:08 +0000441 u32 buf;
Ming Lei72108fd2012-10-24 19:47:04 +0000442 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000443
Steve Glendinning7b9e7582012-12-10 01:03:08 +0000444 buf = data;
445 cpu_to_le32s(&buf);
446
Ming Lei72108fd2012-10-24 19:47:04 +0000447 ret = usbnet_write_cmd_async(dev, USB_VENDOR_REQUEST_WRITE_REGISTER,
448 USB_DIR_OUT | USB_TYPE_VENDOR |
449 USB_RECIP_DEVICE,
Steve Glendinning7b9e7582012-12-10 01:03:08 +0000450 0, index, &buf, size);
Ming Lei72108fd2012-10-24 19:47:04 +0000451 if (ret < 0)
452 netdev_warn(dev->net, "Error write async cmd, sts=%d\n",
453 ret);
454 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000455}
456
457/* returns hash bit number for given MAC address
458 * example:
459 * 01 00 5E 00 00 01 -> returns bit number 31 */
460static unsigned int smsc95xx_hash(char addr[ETH_ALEN])
461{
462 return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
463}
464
465static void smsc95xx_set_multicast(struct net_device *netdev)
466{
467 struct usbnet *dev = netdev_priv(netdev);
468 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000469 unsigned long flags;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000470 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000471
Marc Zyngier3c0f3c62011-03-18 03:53:58 +0000472 pdata->hash_hi = 0;
473 pdata->hash_lo = 0;
474
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000475 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
476
477 if (dev->net->flags & IFF_PROMISC) {
Joe Perchesa475f602010-02-17 10:30:24 +0000478 netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000479 pdata->mac_cr |= MAC_CR_PRMS_;
480 pdata->mac_cr &= ~(MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
481 } else if (dev->net->flags & IFF_ALLMULTI) {
Joe Perchesa475f602010-02-17 10:30:24 +0000482 netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000483 pdata->mac_cr |= MAC_CR_MCPAS_;
484 pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_HPFILT_);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000485 } else if (!netdev_mc_empty(dev->net)) {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000486 struct netdev_hw_addr *ha;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000487
488 pdata->mac_cr |= MAC_CR_HPFILT_;
489 pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);
490
Jiri Pirko22bedad32010-04-01 21:22:57 +0000491 netdev_for_each_mc_addr(ha, netdev) {
492 u32 bitnum = smsc95xx_hash(ha->addr);
Jiri Pirkoa92635d2010-02-18 04:02:26 +0000493 u32 mask = 0x01 << (bitnum & 0x1F);
494 if (bitnum & 0x20)
Marc Zyngier3c0f3c62011-03-18 03:53:58 +0000495 pdata->hash_hi |= mask;
Jiri Pirkoa92635d2010-02-18 04:02:26 +0000496 else
Marc Zyngier3c0f3c62011-03-18 03:53:58 +0000497 pdata->hash_lo |= mask;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000498 }
499
Joe Perchesa475f602010-02-17 10:30:24 +0000500 netif_dbg(dev, drv, dev->net, "HASHH=0x%08X, HASHL=0x%08X\n",
Marc Zyngier3c0f3c62011-03-18 03:53:58 +0000501 pdata->hash_hi, pdata->hash_lo);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000502 } else {
Joe Perchesa475f602010-02-17 10:30:24 +0000503 netif_dbg(dev, drv, dev->net, "receive own packets only\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000504 pdata->mac_cr &=
505 ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
506 }
507
508 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
509
510 /* Initiate async writes, as we can't wait for completion here */
Steve Glendinning7b9e7582012-12-10 01:03:08 +0000511 ret = smsc95xx_write_reg_async(dev, HASHH, pdata->hash_hi);
Steve Glendinningb052e072012-11-30 05:55:52 +0000512 if (ret < 0)
513 netdev_warn(dev->net, "failed to initiate async write to HASHH\n");
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000514
Steve Glendinning7b9e7582012-12-10 01:03:08 +0000515 ret = smsc95xx_write_reg_async(dev, HASHL, pdata->hash_lo);
Steve Glendinningb052e072012-11-30 05:55:52 +0000516 if (ret < 0)
517 netdev_warn(dev->net, "failed to initiate async write to HASHL\n");
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000518
Steve Glendinning7b9e7582012-12-10 01:03:08 +0000519 ret = smsc95xx_write_reg_async(dev, MAC_CR, pdata->mac_cr);
Steve Glendinningb052e072012-11-30 05:55:52 +0000520 if (ret < 0)
521 netdev_warn(dev->net, "failed to initiate async write to MAC_CR\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000522}
523
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000524static int smsc95xx_phy_update_flowcontrol(struct usbnet *dev, u8 duplex,
525 u16 lcladv, u16 rmtadv)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000526{
527 u32 flow, afc_cfg = 0;
528
529 int ret = smsc95xx_read_reg(dev, AFC_CFG, &afc_cfg);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000530 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000531 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000532
533 if (duplex == DUPLEX_FULL) {
Steve Glendinningbc02ff92008-12-16 02:00:48 -0800534 u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000535
536 if (cap & FLOW_CTRL_RX)
537 flow = 0xFFFF0002;
538 else
539 flow = 0;
540
541 if (cap & FLOW_CTRL_TX)
542 afc_cfg |= 0xF;
543 else
544 afc_cfg &= ~0xF;
545
Joe Perchesa475f602010-02-17 10:30:24 +0000546 netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
Joe Perches60b86752010-02-17 10:30:23 +0000547 cap & FLOW_CTRL_RX ? "enabled" : "disabled",
548 cap & FLOW_CTRL_TX ? "enabled" : "disabled");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000549 } else {
Joe Perchesa475f602010-02-17 10:30:24 +0000550 netif_dbg(dev, link, dev->net, "half duplex\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000551 flow = 0;
552 afc_cfg |= 0xF;
553 }
554
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000555 ret = smsc95xx_write_reg(dev, FLOW, flow);
Steve Glendinningb052e072012-11-30 05:55:52 +0000556 if (ret < 0)
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000557 return ret;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000558
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000559 return smsc95xx_write_reg(dev, AFC_CFG, afc_cfg);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000560}
561
562static int smsc95xx_link_reset(struct usbnet *dev)
563{
564 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
565 struct mii_if_info *mii = &dev->mii;
David Decotigny8ae6dac2011-04-27 18:32:38 +0000566 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000567 unsigned long flags;
568 u16 lcladv, rmtadv;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000569 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000570
571 /* clear interrupt status */
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000572 ret = smsc95xx_mdio_read(dev->net, mii->phy_id, PHY_INT_SRC);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000573 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000574 return ret;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000575
576 ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000577 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000578 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000579
580 mii_check_media(mii, 1, 1);
581 mii_ethtool_gset(&dev->mii, &ecmd);
582 lcladv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
583 rmtadv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_LPA);
584
David Decotigny8ae6dac2011-04-27 18:32:38 +0000585 netif_dbg(dev, link, dev->net,
586 "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
587 ethtool_cmd_speed(&ecmd), ecmd.duplex, lcladv, rmtadv);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000588
589 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
590 if (ecmd.duplex != DUPLEX_FULL) {
591 pdata->mac_cr &= ~MAC_CR_FDPX_;
592 pdata->mac_cr |= MAC_CR_RCVOWN_;
593 } else {
594 pdata->mac_cr &= ~MAC_CR_RCVOWN_;
595 pdata->mac_cr |= MAC_CR_FDPX_;
596 }
597 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
598
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000599 ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000600 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000601 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000602
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000603 ret = smsc95xx_phy_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
Steve Glendinningb052e072012-11-30 05:55:52 +0000604 if (ret < 0)
605 netdev_warn(dev->net, "Error updating PHY flow control\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000606
Steve Glendinningb052e072012-11-30 05:55:52 +0000607 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000608}
609
610static void smsc95xx_status(struct usbnet *dev, struct urb *urb)
611{
612 u32 intdata;
613
614 if (urb->actual_length != 4) {
Joe Perches60b86752010-02-17 10:30:23 +0000615 netdev_warn(dev->net, "unexpected urb length %d\n",
616 urb->actual_length);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000617 return;
618 }
619
620 memcpy(&intdata, urb->transfer_buffer, 4);
Steve Glendinning1d74a6b2008-10-09 14:34:47 -0700621 le32_to_cpus(&intdata);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000622
Joe Perchesa475f602010-02-17 10:30:24 +0000623 netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000624
625 if (intdata & INT_ENP_PHY_INT_)
626 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
627 else
Joe Perches60b86752010-02-17 10:30:23 +0000628 netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
629 intdata);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000630}
631
Christoph Fritzd69d1692016-05-26 04:06:47 +0200632static void set_carrier(struct usbnet *dev, bool link)
633{
634 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
635
636 if (pdata->link_ok == link)
637 return;
638
639 pdata->link_ok = link;
640
641 if (link)
642 usbnet_link_change(dev, 1, 0);
643 else
644 usbnet_link_change(dev, 0, 0);
645}
646
647static void check_carrier(struct work_struct *work)
648{
649 struct smsc95xx_priv *pdata = container_of(work, struct smsc95xx_priv,
650 carrier_check.work);
651 struct usbnet *dev = pdata->dev;
652 int ret;
653
654 if (pdata->suspend_flags != 0)
655 return;
656
657 ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMSR);
658 if (ret < 0) {
659 netdev_warn(dev->net, "Failed to read MII_BMSR\n");
660 return;
661 }
662 if (ret & BMSR_LSTATUS)
663 set_carrier(dev, 1);
664 else
665 set_carrier(dev, 0);
666
667 schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
668}
669
Steve Glendinningf7b29272008-11-20 04:19:21 -0800670/* Enable or disable Tx & Rx checksum offload engines */
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000671static int smsc95xx_set_features(struct net_device *netdev,
672 netdev_features_t features)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000673{
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700674 struct usbnet *dev = netdev_priv(netdev);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000675 u32 read_buf;
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700676 int ret;
677
678 ret = smsc95xx_read_reg(dev, COE_CR, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000679 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000680 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000681
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700682 if (features & NETIF_F_HW_CSUM)
Steve Glendinningf7b29272008-11-20 04:19:21 -0800683 read_buf |= Tx_COE_EN_;
684 else
685 read_buf &= ~Tx_COE_EN_;
686
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700687 if (features & NETIF_F_RXCSUM)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000688 read_buf |= Rx_COE_EN_;
689 else
690 read_buf &= ~Rx_COE_EN_;
691
692 ret = smsc95xx_write_reg(dev, COE_CR, read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000693 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000694 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000695
Joe Perchesa475f602010-02-17 10:30:24 +0000696 netif_dbg(dev, hw, dev->net, "COE_CR = 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000697 return 0;
698}
699
700static int smsc95xx_ethtool_get_eeprom_len(struct net_device *net)
701{
702 return MAX_EEPROM_SIZE;
703}
704
705static int smsc95xx_ethtool_get_eeprom(struct net_device *netdev,
706 struct ethtool_eeprom *ee, u8 *data)
707{
708 struct usbnet *dev = netdev_priv(netdev);
709
710 ee->magic = LAN95XX_EEPROM_MAGIC;
711
712 return smsc95xx_read_eeprom(dev, ee->offset, ee->len, data);
713}
714
715static int smsc95xx_ethtool_set_eeprom(struct net_device *netdev,
716 struct ethtool_eeprom *ee, u8 *data)
717{
718 struct usbnet *dev = netdev_priv(netdev);
719
720 if (ee->magic != LAN95XX_EEPROM_MAGIC) {
Joe Perches60b86752010-02-17 10:30:23 +0000721 netdev_warn(dev->net, "EEPROM: magic value mismatch, magic = 0x%x\n",
722 ee->magic);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000723 return -EINVAL;
724 }
725
726 return smsc95xx_write_eeprom(dev, ee->offset, ee->len, data);
727}
728
Emeric Vigier9fa32e92012-07-09 17:44:45 -0400729static int smsc95xx_ethtool_getregslen(struct net_device *netdev)
730{
731 /* all smsc95xx registers */
Steve Glendinning96245312012-12-10 01:03:07 +0000732 return COE_CR - ID_REV + sizeof(u32);
Emeric Vigier9fa32e92012-07-09 17:44:45 -0400733}
734
735static void
736smsc95xx_ethtool_getregs(struct net_device *netdev, struct ethtool_regs *regs,
737 void *buf)
738{
739 struct usbnet *dev = netdev_priv(netdev);
Dan Carpenterd3484462012-07-10 20:32:51 +0000740 unsigned int i, j;
741 int retval;
Emeric Vigier9fa32e92012-07-09 17:44:45 -0400742 u32 *data = buf;
743
744 retval = smsc95xx_read_reg(dev, ID_REV, &regs->version);
745 if (retval < 0) {
746 netdev_warn(netdev, "REGS: cannot read ID_REV\n");
747 return;
748 }
749
750 for (i = ID_REV, j = 0; i <= COE_CR; i += (sizeof(u32)), j++) {
751 retval = smsc95xx_read_reg(dev, i, &data[j]);
752 if (retval < 0) {
753 netdev_warn(netdev, "REGS: cannot read reg[%x]\n", i);
754 return;
755 }
756 }
757}
758
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000759static void smsc95xx_ethtool_get_wol(struct net_device *net,
760 struct ethtool_wolinfo *wolinfo)
761{
762 struct usbnet *dev = netdev_priv(net);
763 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
764
765 wolinfo->supported = SUPPORTED_WAKE;
766 wolinfo->wolopts = pdata->wolopts;
767}
768
769static int smsc95xx_ethtool_set_wol(struct net_device *net,
770 struct ethtool_wolinfo *wolinfo)
771{
772 struct usbnet *dev = netdev_priv(net);
773 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
Steve Glendinning3b146922012-11-30 05:55:50 +0000774 int ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000775
776 pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
Steve Glendinning3b146922012-11-30 05:55:50 +0000777
778 ret = device_set_wakeup_enable(&dev->udev->dev, pdata->wolopts);
Steve Glendinningb052e072012-11-30 05:55:52 +0000779 if (ret < 0)
780 netdev_warn(dev->net, "device_set_wakeup_enable error %d\n", ret);
Steve Glendinning3b146922012-11-30 05:55:50 +0000781
Steve Glendinningb052e072012-11-30 05:55:52 +0000782 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000783}
784
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700785static const struct ethtool_ops smsc95xx_ethtool_ops = {
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000786 .get_link = usbnet_get_link,
787 .nway_reset = usbnet_nway_reset,
788 .get_drvinfo = usbnet_get_drvinfo,
789 .get_msglevel = usbnet_get_msglevel,
790 .set_msglevel = usbnet_set_msglevel,
791 .get_settings = usbnet_get_settings,
792 .set_settings = usbnet_set_settings,
793 .get_eeprom_len = smsc95xx_ethtool_get_eeprom_len,
794 .get_eeprom = smsc95xx_ethtool_get_eeprom,
795 .set_eeprom = smsc95xx_ethtool_set_eeprom,
Emeric Vigier9fa32e92012-07-09 17:44:45 -0400796 .get_regs_len = smsc95xx_ethtool_getregslen,
797 .get_regs = smsc95xx_ethtool_getregs,
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000798 .get_wol = smsc95xx_ethtool_get_wol,
799 .set_wol = smsc95xx_ethtool_set_wol,
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000800};
801
802static int smsc95xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
803{
804 struct usbnet *dev = netdev_priv(netdev);
805
806 if (!netif_running(netdev))
807 return -EINVAL;
808
809 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
810}
811
812static void smsc95xx_init_mac_address(struct usbnet *dev)
813{
Arnd Bergmannc4895652016-04-29 09:05:59 +0200814 const u8 *mac_addr;
815
816 /* maybe the boot loader passed the MAC address in devicetree */
817 mac_addr = of_get_mac_address(dev->udev->dev.of_node);
818 if (mac_addr) {
819 memcpy(dev->net->dev_addr, mac_addr, ETH_ALEN);
820 return;
821 }
822
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000823 /* try reading mac address from EEPROM */
824 if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
825 dev->net->dev_addr) == 0) {
826 if (is_valid_ether_addr(dev->net->dev_addr)) {
827 /* eeprom values are valid so use them */
Joe Perchesa475f602010-02-17 10:30:24 +0000828 netif_dbg(dev, ifup, dev->net, "MAC address read from EEPROM\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000829 return;
830 }
831 }
832
Arnd Bergmannc4895652016-04-29 09:05:59 +0200833 /* no useful static MAC address found. generate a random one */
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000834 eth_hw_addr_random(dev->net);
Joe Perchesc7e12ea2012-07-12 19:33:07 +0000835 netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000836}
837
838static int smsc95xx_set_mac_address(struct usbnet *dev)
839{
840 u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
841 dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
842 u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
843 int ret;
844
845 ret = smsc95xx_write_reg(dev, ADDRL, addr_lo);
Steve Glendinningb052e072012-11-30 05:55:52 +0000846 if (ret < 0)
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000847 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000848
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000849 return smsc95xx_write_reg(dev, ADDRH, addr_hi);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000850}
851
852/* starts the TX path */
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000853static int smsc95xx_start_tx_path(struct usbnet *dev)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000854{
855 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
856 unsigned long flags;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000857 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000858
859 /* Enable Tx at MAC */
860 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
861 pdata->mac_cr |= MAC_CR_TXEN_;
862 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
863
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000864 ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000865 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000866 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000867
868 /* Enable Tx at SCSRs */
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000869 return smsc95xx_write_reg(dev, TX_CFG, TX_CFG_ON_);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000870}
871
872/* Starts the Receive path */
Ming Leiec321152012-11-06 04:53:07 +0000873static int smsc95xx_start_rx_path(struct usbnet *dev, int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000874{
875 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
876 unsigned long flags;
877
878 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
879 pdata->mac_cr |= MAC_CR_RXEN_;
880 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
881
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000882 return __smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr, in_pm);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000883}
884
885static int smsc95xx_phy_initialize(struct usbnet *dev)
886{
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000887 int bmcr, ret, timeout = 0;
Steve Glendinningdb443c42010-03-16 09:03:06 +0000888
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000889 /* Initialize MII structure */
890 dev->mii.dev = dev->net;
891 dev->mii.mdio_read = smsc95xx_mdio_read;
892 dev->mii.mdio_write = smsc95xx_mdio_write;
893 dev->mii.phy_id_mask = 0x1f;
894 dev->mii.reg_num_mask = 0x1f;
895 dev->mii.phy_id = SMSC95XX_INTERNAL_PHY_ID;
896
Steve Glendinningdb443c42010-03-16 09:03:06 +0000897 /* reset phy and wait for reset to complete */
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000898 smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
Steve Glendinningdb443c42010-03-16 09:03:06 +0000899
900 do {
901 msleep(10);
902 bmcr = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
903 timeout++;
Rabin Vincentd9460922011-04-30 08:29:27 +0000904 } while ((bmcr & BMCR_RESET) && (timeout < 100));
Steve Glendinningdb443c42010-03-16 09:03:06 +0000905
906 if (timeout >= 100) {
907 netdev_warn(dev->net, "timeout on PHY Reset");
908 return -EIO;
909 }
910
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000911 smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
912 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
913 ADVERTISE_PAUSE_ASYM);
914
915 /* read to clear */
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000916 ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
Steve Glendinningb052e072012-11-30 05:55:52 +0000917 if (ret < 0) {
918 netdev_warn(dev->net, "Failed to read PHY_INT_SRC during init\n");
919 return ret;
920 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000921
922 smsc95xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
923 PHY_INT_MASK_DEFAULT_);
924 mii_nway_restart(&dev->mii);
925
Joe Perchesa475f602010-02-17 10:30:24 +0000926 netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000927 return 0;
928}
929
930static int smsc95xx_reset(struct usbnet *dev)
931{
932 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
933 u32 read_buf, write_buf, burst_cap;
934 int ret = 0, timeout;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000935
Joe Perchesa475f602010-02-17 10:30:24 +0000936 netif_dbg(dev, ifup, dev->net, "entering smsc95xx_reset\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000937
Steve Glendinning44367612012-09-28 00:07:08 +0000938 ret = smsc95xx_write_reg(dev, HW_CFG, HW_CFG_LRST_);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000939 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000940 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000941
942 timeout = 0;
943 do {
Steve Glendinningcf2acec2012-09-28 00:07:07 +0000944 msleep(10);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000945 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000946 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000947 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000948 timeout++;
949 } while ((read_buf & HW_CFG_LRST_) && (timeout < 100));
950
951 if (timeout >= 100) {
Joe Perches60b86752010-02-17 10:30:23 +0000952 netdev_warn(dev->net, "timeout waiting for completion of Lite Reset\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000953 return ret;
954 }
955
Steve Glendinning44367612012-09-28 00:07:08 +0000956 ret = smsc95xx_write_reg(dev, PM_CTRL, PM_CTL_PHY_RST_);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000957 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000958 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000959
960 timeout = 0;
961 do {
Steve Glendinningcf2acec2012-09-28 00:07:07 +0000962 msleep(10);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000963 ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000964 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000965 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000966 timeout++;
967 } while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));
968
969 if (timeout >= 100) {
Joe Perches60b86752010-02-17 10:30:23 +0000970 netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000971 return ret;
972 }
973
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000974 ret = smsc95xx_set_mac_address(dev);
975 if (ret < 0)
976 return ret;
977
Joe Perches1e1d7412012-11-24 01:27:49 +0000978 netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n",
979 dev->net->dev_addr);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000980
981 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000982 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000983 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000984
Joe Perches1e1d7412012-11-24 01:27:49 +0000985 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n",
986 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000987
988 read_buf |= HW_CFG_BIR_;
989
990 ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000991 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000992 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000993
994 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000995 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000996 return ret;
Steve Glendinningb052e072012-11-30 05:55:52 +0000997
Joe Perchesa475f602010-02-17 10:30:24 +0000998 netif_dbg(dev, ifup, dev->net,
999 "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
1000 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001001
1002 if (!turbo_mode) {
1003 burst_cap = 0;
1004 dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
1005 } else if (dev->udev->speed == USB_SPEED_HIGH) {
1006 burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
1007 dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
1008 } else {
1009 burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
1010 dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
1011 }
1012
Joe Perches1e1d7412012-11-24 01:27:49 +00001013 netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n",
1014 (ulong)dev->rx_urb_size);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001015
1016 ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001017 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001018 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001019
1020 ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001021 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001022 return ret;
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001023
Joe Perchesa475f602010-02-17 10:30:24 +00001024 netif_dbg(dev, ifup, dev->net,
1025 "Read Value from BURST_CAP after writing: 0x%08x\n",
1026 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001027
Steve Glendinning44367612012-09-28 00:07:08 +00001028 ret = smsc95xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001029 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001030 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001031
1032 ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001033 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001034 return ret;
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001035
Joe Perchesa475f602010-02-17 10:30:24 +00001036 netif_dbg(dev, ifup, dev->net,
1037 "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
1038 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001039
1040 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001041 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001042 return ret;
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001043
Joe Perches1e1d7412012-11-24 01:27:49 +00001044 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG: 0x%08x\n",
1045 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001046
1047 if (turbo_mode)
1048 read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);
1049
1050 read_buf &= ~HW_CFG_RXDOFF_;
1051
1052 /* set Rx data offset=2, Make IP header aligns on word boundary. */
1053 read_buf |= NET_IP_ALIGN << 9;
1054
1055 ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001056 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001057 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001058
1059 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001060 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001061 return ret;
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001062
Joe Perchesa475f602010-02-17 10:30:24 +00001063 netif_dbg(dev, ifup, dev->net,
1064 "Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001065
Steve Glendinning44367612012-09-28 00:07:08 +00001066 ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001067 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001068 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001069
1070 ret = smsc95xx_read_reg(dev, ID_REV, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001071 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001072 return ret;
Joe Perchesa475f602010-02-17 10:30:24 +00001073 netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001074
Steve Glendinningf2935012009-05-01 05:46:51 +00001075 /* Configure GPIO pins as LED outputs */
1076 write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED |
1077 LED_GPIO_CFG_FDX_LED;
1078 ret = smsc95xx_write_reg(dev, LED_GPIO_CFG, write_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001079 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001080 return ret;
Steve Glendinningf2935012009-05-01 05:46:51 +00001081
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001082 /* Init Tx */
Steve Glendinning44367612012-09-28 00:07:08 +00001083 ret = smsc95xx_write_reg(dev, FLOW, 0);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001084 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001085 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001086
Steve Glendinning44367612012-09-28 00:07:08 +00001087 ret = smsc95xx_write_reg(dev, AFC_CFG, AFC_CFG_DEFAULT);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001088 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001089 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001090
1091 /* Don't need mac_cr_lock during initialisation */
1092 ret = smsc95xx_read_reg(dev, MAC_CR, &pdata->mac_cr);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001093 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001094 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001095
1096 /* Init Rx */
1097 /* Set Vlan */
Steve Glendinning44367612012-09-28 00:07:08 +00001098 ret = smsc95xx_write_reg(dev, VLAN1, (u32)ETH_P_8021Q);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001099 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001100 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001101
Steve Glendinningf7b29272008-11-20 04:19:21 -08001102 /* Enable or disable checksum offload engines */
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001103 ret = smsc95xx_set_features(dev->net, dev->net->features);
Steve Glendinningb052e072012-11-30 05:55:52 +00001104 if (ret < 0) {
1105 netdev_warn(dev->net, "Failed to set checksum offload features\n");
1106 return ret;
1107 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001108
1109 smsc95xx_set_multicast(dev->net);
1110
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001111 ret = smsc95xx_phy_initialize(dev);
Steve Glendinningb052e072012-11-30 05:55:52 +00001112 if (ret < 0) {
1113 netdev_warn(dev->net, "Failed to init PHY\n");
1114 return ret;
1115 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001116
1117 ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001118 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001119 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001120
1121 /* enable PHY interrupts */
1122 read_buf |= INT_EP_CTL_PHY_INT_;
1123
1124 ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001125 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001126 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001127
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001128 ret = smsc95xx_start_tx_path(dev);
Steve Glendinningb052e072012-11-30 05:55:52 +00001129 if (ret < 0) {
1130 netdev_warn(dev->net, "Failed to start TX path\n");
1131 return ret;
1132 }
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001133
Ming Leiec321152012-11-06 04:53:07 +00001134 ret = smsc95xx_start_rx_path(dev, 0);
Steve Glendinningb052e072012-11-30 05:55:52 +00001135 if (ret < 0) {
1136 netdev_warn(dev->net, "Failed to start RX path\n");
1137 return ret;
1138 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001139
Joe Perchesa475f602010-02-17 10:30:24 +00001140 netif_dbg(dev, ifup, dev->net, "smsc95xx_reset, return 0\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001141 return 0;
1142}
1143
Stephen Hemminger63e77b32009-03-20 19:35:58 +00001144static const struct net_device_ops smsc95xx_netdev_ops = {
1145 .ndo_open = usbnet_open,
1146 .ndo_stop = usbnet_stop,
1147 .ndo_start_xmit = usbnet_start_xmit,
1148 .ndo_tx_timeout = usbnet_tx_timeout,
1149 .ndo_change_mtu = usbnet_change_mtu,
1150 .ndo_set_mac_address = eth_mac_addr,
1151 .ndo_validate_addr = eth_validate_addr,
1152 .ndo_do_ioctl = smsc95xx_ioctl,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001153 .ndo_set_rx_mode = smsc95xx_set_multicast,
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001154 .ndo_set_features = smsc95xx_set_features,
Stephen Hemminger63e77b32009-03-20 19:35:58 +00001155};
1156
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001157static int smsc95xx_bind(struct usbnet *dev, struct usb_interface *intf)
1158{
1159 struct smsc95xx_priv *pdata = NULL;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001160 u32 val;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001161 int ret;
1162
1163 printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
1164
1165 ret = usbnet_get_endpoints(dev, intf);
Steve Glendinningb052e072012-11-30 05:55:52 +00001166 if (ret < 0) {
1167 netdev_warn(dev->net, "usbnet_get_endpoints failed: %d\n", ret);
1168 return ret;
1169 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001170
1171 dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc95xx_priv),
Joe Perches38673c82013-02-03 17:28:11 +00001172 GFP_KERNEL);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001173
1174 pdata = (struct smsc95xx_priv *)(dev->data[0]);
Joe Perches38673c82013-02-03 17:28:11 +00001175 if (!pdata)
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001176 return -ENOMEM;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001177
1178 spin_lock_init(&pdata->mac_cr_lock);
1179
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001180 if (DEFAULT_TX_CSUM_ENABLE)
1181 dev->net->features |= NETIF_F_HW_CSUM;
1182 if (DEFAULT_RX_CSUM_ENABLE)
1183 dev->net->features |= NETIF_F_RXCSUM;
1184
1185 dev->net->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001186
Bernard Blackhamf4e8ab72010-10-18 13:16:39 +00001187 smsc95xx_init_mac_address(dev);
1188
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001189 /* Init all registers */
1190 ret = smsc95xx_reset(dev);
1191
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001192 /* detect device revision as different features may be available */
1193 ret = smsc95xx_read_reg(dev, ID_REV, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001194 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001195 return ret;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001196 val >>= 16;
Steve Glendinning9ebca502012-11-22 08:05:23 +00001197
1198 if ((val == ID_REV_CHIP_ID_9500A_) || (val == ID_REV_CHIP_ID_9530_) ||
1199 (val == ID_REV_CHIP_ID_89530_) || (val == ID_REV_CHIP_ID_9730_))
1200 pdata->features = (FEATURE_8_WAKEUP_FILTERS |
1201 FEATURE_PHY_NLP_CROSSOVER |
Ming Leieb970ff2013-02-22 03:05:05 +00001202 FEATURE_REMOTE_WAKEUP);
Steve Glendinning9ebca502012-11-22 08:05:23 +00001203 else if (val == ID_REV_CHIP_ID_9512_)
1204 pdata->features = FEATURE_8_WAKEUP_FILTERS;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001205
Stephen Hemminger63e77b32009-03-20 19:35:58 +00001206 dev->net->netdev_ops = &smsc95xx_netdev_ops;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001207 dev->net->ethtool_ops = &smsc95xx_ethtool_ops;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001208 dev->net->flags |= IFF_MULTICAST;
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001209 dev->net->hard_header_len += SMSC95XX_TX_OVERHEAD_CSUM;
Stephane Fillod9bbf5662012-04-20 09:39:23 +00001210 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
Christoph Fritzd69d1692016-05-26 04:06:47 +02001211
1212 pdata->dev = dev;
1213 INIT_DELAYED_WORK(&pdata->carrier_check, check_carrier);
1214 schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
1215
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001216 return 0;
1217}
1218
1219static void smsc95xx_unbind(struct usbnet *dev, struct usb_interface *intf)
1220{
1221 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
Christoph Fritzd69d1692016-05-26 04:06:47 +02001222
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001223 if (pdata) {
Christoph Fritzd69d1692016-05-26 04:06:47 +02001224 cancel_delayed_work(&pdata->carrier_check);
Joe Perchesa475f602010-02-17 10:30:24 +00001225 netif_dbg(dev, ifdown, dev->net, "free pdata\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001226 kfree(pdata);
1227 pdata = NULL;
1228 dev->data[0] = 0;
1229 }
1230}
1231
Steve Glendinning068bb1a2012-11-30 05:55:51 +00001232static u32 smsc_crc(const u8 *buffer, size_t len, int filter)
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001233{
Steve Glendinning068bb1a2012-11-30 05:55:51 +00001234 u32 crc = bitrev16(crc16(0xFFFF, buffer, len));
1235 return crc << ((filter % 2) * 16);
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001236}
1237
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001238static int smsc95xx_enable_phy_wakeup_interrupts(struct usbnet *dev, u16 mask)
1239{
1240 struct mii_if_info *mii = &dev->mii;
1241 int ret;
1242
Joe Perches1e1d7412012-11-24 01:27:49 +00001243 netdev_dbg(dev->net, "enabling PHY wakeup interrupts\n");
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001244
1245 /* read to clear */
1246 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_SRC);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001247 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001248 return ret;
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001249
1250 /* enable interrupt source */
1251 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_MASK);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001252 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001253 return ret;
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001254
1255 ret |= mask;
1256
1257 smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_INT_MASK, ret);
1258
1259 return 0;
1260}
1261
1262static int smsc95xx_link_ok_nopm(struct usbnet *dev)
1263{
1264 struct mii_if_info *mii = &dev->mii;
1265 int ret;
1266
1267 /* first, a dummy read, needed to latch some MII phys */
1268 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001269 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001270 return ret;
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001271
1272 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001273 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001274 return ret;
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001275
1276 return !!(ret & BMSR_LSTATUS);
1277}
1278
Steve Glendinning319b95b2012-11-22 08:05:25 +00001279static int smsc95xx_enter_suspend0(struct usbnet *dev)
1280{
1281 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1282 u32 val;
1283 int ret;
1284
1285 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001286 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001287 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001288
1289 val &= (~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_));
1290 val |= PM_CTL_SUS_MODE_0;
1291
1292 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001293 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001294 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001295
1296 /* clear wol status */
1297 val &= ~PM_CTL_WUPS_;
1298 val |= PM_CTL_WUPS_WOL_;
1299
1300 /* enable energy detection */
1301 if (pdata->wolopts & WAKE_PHY)
1302 val |= PM_CTL_WUPS_ED_;
1303
1304 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001305 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001306 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001307
1308 /* read back PM_CTRL */
1309 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Ming Lei76437212013-02-22 03:05:03 +00001310 if (ret < 0)
1311 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001312
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001313 pdata->suspend_flags |= SUSPEND_SUSPEND0;
1314
Ming Lei76437212013-02-22 03:05:03 +00001315 return 0;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001316}
1317
1318static int smsc95xx_enter_suspend1(struct usbnet *dev)
1319{
1320 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1321 struct mii_if_info *mii = &dev->mii;
1322 u32 val;
1323 int ret;
1324
1325 /* reconfigure link pulse detection timing for
1326 * compatibility with non-standard link partners
1327 */
1328 if (pdata->features & FEATURE_PHY_NLP_CROSSOVER)
1329 smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_EDPD_CONFIG,
1330 PHY_EDPD_CONFIG_DEFAULT);
1331
1332 /* enable energy detect power-down mode */
1333 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001334 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001335 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001336
1337 ret |= MODE_CTRL_STS_EDPWRDOWN_;
1338
1339 smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS, ret);
1340
1341 /* enter SUSPEND1 mode */
1342 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001343 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001344 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001345
1346 val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1347 val |= PM_CTL_SUS_MODE_1;
1348
1349 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001350 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001351 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001352
1353 /* clear wol status, enable energy detection */
1354 val &= ~PM_CTL_WUPS_;
1355 val |= (PM_CTL_WUPS_ED_ | PM_CTL_ED_EN_);
1356
1357 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Ming Lei76437212013-02-22 03:05:03 +00001358 if (ret < 0)
1359 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001360
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001361 pdata->suspend_flags |= SUSPEND_SUSPEND1;
1362
Ming Lei76437212013-02-22 03:05:03 +00001363 return 0;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001364}
1365
1366static int smsc95xx_enter_suspend2(struct usbnet *dev)
1367{
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001368 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
Steve Glendinning319b95b2012-11-22 08:05:25 +00001369 u32 val;
1370 int ret;
1371
1372 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001373 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001374 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001375
1376 val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1377 val |= PM_CTL_SUS_MODE_2;
1378
1379 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Ming Lei76437212013-02-22 03:05:03 +00001380 if (ret < 0)
1381 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001382
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001383 pdata->suspend_flags |= SUSPEND_SUSPEND2;
1384
Ming Lei76437212013-02-22 03:05:03 +00001385 return 0;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001386}
1387
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001388static int smsc95xx_enter_suspend3(struct usbnet *dev)
1389{
1390 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1391 u32 val;
1392 int ret;
1393
1394 ret = smsc95xx_read_reg_nopm(dev, RX_FIFO_INF, &val);
1395 if (ret < 0)
1396 return ret;
1397
1398 if (val & 0xFFFF) {
1399 netdev_info(dev->net, "rx fifo not empty in autosuspend\n");
1400 return -EBUSY;
1401 }
1402
1403 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1404 if (ret < 0)
1405 return ret;
1406
1407 val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1408 val |= PM_CTL_SUS_MODE_3 | PM_CTL_RES_CLR_WKP_STS;
1409
1410 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1411 if (ret < 0)
1412 return ret;
1413
1414 /* clear wol status */
1415 val &= ~PM_CTL_WUPS_;
1416 val |= PM_CTL_WUPS_WOL_;
1417
1418 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1419 if (ret < 0)
1420 return ret;
1421
1422 pdata->suspend_flags |= SUSPEND_SUSPEND3;
1423
1424 return 0;
1425}
1426
1427static int smsc95xx_autosuspend(struct usbnet *dev, u32 link_up)
1428{
1429 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1430 int ret;
1431
1432 if (!netif_running(dev->net)) {
1433 /* interface is ifconfig down so fully power down hw */
1434 netdev_dbg(dev->net, "autosuspend entering SUSPEND2\n");
1435 return smsc95xx_enter_suspend2(dev);
1436 }
1437
1438 if (!link_up) {
1439 /* link is down so enter EDPD mode, but only if device can
1440 * reliably resume from it. This check should be redundant
Ming Leieb970ff2013-02-22 03:05:05 +00001441 * as current FEATURE_REMOTE_WAKEUP parts also support
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001442 * FEATURE_PHY_NLP_CROSSOVER but it's included for clarity */
1443 if (!(pdata->features & FEATURE_PHY_NLP_CROSSOVER)) {
1444 netdev_warn(dev->net, "EDPD not supported\n");
1445 return -EBUSY;
1446 }
1447
1448 netdev_dbg(dev->net, "autosuspend entering SUSPEND1\n");
1449
1450 /* enable PHY wakeup events for if cable is attached */
1451 ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1452 PHY_INT_MASK_ANEG_COMP_);
1453 if (ret < 0) {
1454 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1455 return ret;
1456 }
1457
1458 netdev_info(dev->net, "entering SUSPEND1 mode\n");
1459 return smsc95xx_enter_suspend1(dev);
1460 }
1461
1462 /* enable PHY wakeup events so we remote wakeup if cable is pulled */
1463 ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1464 PHY_INT_MASK_LINK_DOWN_);
1465 if (ret < 0) {
1466 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1467 return ret;
1468 }
1469
1470 netdev_dbg(dev->net, "autosuspend entering SUSPEND3\n");
1471 return smsc95xx_enter_suspend3(dev);
1472}
1473
Steve Glendinningb5a04472012-09-28 00:07:11 +00001474static int smsc95xx_suspend(struct usb_interface *intf, pm_message_t message)
1475{
1476 struct usbnet *dev = usb_get_intfdata(intf);
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001477 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001478 u32 val, link_up;
Steve Glendinningb5a04472012-09-28 00:07:11 +00001479 int ret;
Steve Glendinningb5a04472012-09-28 00:07:11 +00001480
Steve Glendinningb5a04472012-09-28 00:07:11 +00001481 ret = usbnet_suspend(intf, message);
Steve Glendinningb052e072012-11-30 05:55:52 +00001482 if (ret < 0) {
1483 netdev_warn(dev->net, "usbnet_suspend error\n");
1484 return ret;
1485 }
Steve Glendinningb5a04472012-09-28 00:07:11 +00001486
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001487 if (pdata->suspend_flags) {
1488 netdev_warn(dev->net, "error during last resume\n");
1489 pdata->suspend_flags = 0;
1490 }
1491
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001492 /* determine if link is up using only _nopm functions */
1493 link_up = smsc95xx_link_ok_nopm(dev);
1494
Ming Lei42e21c02013-02-22 03:05:04 +00001495 if (message.event == PM_EVENT_AUTO_SUSPEND &&
Ming Leieb970ff2013-02-22 03:05:05 +00001496 (pdata->features & FEATURE_REMOTE_WAKEUP)) {
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001497 ret = smsc95xx_autosuspend(dev, link_up);
1498 goto done;
1499 }
1500
1501 /* if we get this far we're not autosuspending */
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001502 /* if no wol options set, or if link is down and we're not waking on
1503 * PHY activity, enter lowest power SUSPEND2 mode
1504 */
1505 if (!(pdata->wolopts & SUPPORTED_WAKE) ||
1506 !(link_up || (pdata->wolopts & WAKE_PHY))) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001507 netdev_info(dev->net, "entering SUSPEND2 mode\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001508
1509 /* disable energy detect (link up) & wake up events */
Ming Leiec321152012-11-06 04:53:07 +00001510 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001511 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001512 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001513
1514 val &= ~(WUCSR_MPEN_ | WUCSR_WAKE_EN_);
1515
Ming Leiec321152012-11-06 04:53:07 +00001516 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001517 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001518 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001519
Ming Leiec321152012-11-06 04:53:07 +00001520 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001521 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001522 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001523
1524 val &= ~(PM_CTL_ED_EN_ | PM_CTL_WOL_EN_);
1525
Ming Leiec321152012-11-06 04:53:07 +00001526 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001527 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001528 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001529
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001530 ret = smsc95xx_enter_suspend2(dev);
1531 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001532 }
1533
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001534 if (pdata->wolopts & WAKE_PHY) {
1535 ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1536 (PHY_INT_MASK_ANEG_COMP_ | PHY_INT_MASK_LINK_DOWN_));
Steve Glendinningb052e072012-11-30 05:55:52 +00001537 if (ret < 0) {
1538 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1539 goto done;
1540 }
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001541
1542 /* if link is down then configure EDPD and enter SUSPEND1,
1543 * otherwise enter SUSPEND0 below
1544 */
1545 if (!link_up) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001546 netdev_info(dev->net, "entering SUSPEND1 mode\n");
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001547 ret = smsc95xx_enter_suspend1(dev);
1548 goto done;
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001549 }
1550 }
1551
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001552 if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
Steve Glendinningeed9a722012-11-30 05:55:48 +00001553 u32 *filter_mask = kzalloc(sizeof(u32) * 32, GFP_KERNEL);
Ming Lei06a221b2012-11-06 04:53:06 +00001554 u32 command[2];
1555 u32 offset[2];
1556 u32 crc[4];
Steve Glendinning9ebca502012-11-22 08:05:23 +00001557 int wuff_filter_count =
1558 (pdata->features & FEATURE_8_WAKEUP_FILTERS) ?
1559 LAN9500A_WUFF_NUM : LAN9500_WUFF_NUM;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001560 int i, filter = 0;
1561
Steve Glendinningeed9a722012-11-30 05:55:48 +00001562 if (!filter_mask) {
1563 netdev_warn(dev->net, "Unable to allocate filter_mask\n");
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001564 ret = -ENOMEM;
1565 goto done;
Steve Glendinningeed9a722012-11-30 05:55:48 +00001566 }
1567
Ming Lei06a221b2012-11-06 04:53:06 +00001568 memset(command, 0, sizeof(command));
1569 memset(offset, 0, sizeof(offset));
1570 memset(crc, 0, sizeof(crc));
1571
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001572 if (pdata->wolopts & WAKE_BCAST) {
1573 const u8 bcast[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Joe Perches1e1d7412012-11-24 01:27:49 +00001574 netdev_info(dev->net, "enabling broadcast detection\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001575 filter_mask[filter * 4] = 0x003F;
1576 filter_mask[filter * 4 + 1] = 0x00;
1577 filter_mask[filter * 4 + 2] = 0x00;
1578 filter_mask[filter * 4 + 3] = 0x00;
1579 command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1580 offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1581 crc[filter/2] |= smsc_crc(bcast, 6, filter);
1582 filter++;
1583 }
1584
1585 if (pdata->wolopts & WAKE_MCAST) {
1586 const u8 mcast[] = {0x01, 0x00, 0x5E};
Joe Perches1e1d7412012-11-24 01:27:49 +00001587 netdev_info(dev->net, "enabling multicast detection\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001588 filter_mask[filter * 4] = 0x0007;
1589 filter_mask[filter * 4 + 1] = 0x00;
1590 filter_mask[filter * 4 + 2] = 0x00;
1591 filter_mask[filter * 4 + 3] = 0x00;
1592 command[filter/4] |= 0x09UL << ((filter % 4) * 8);
1593 offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1594 crc[filter/2] |= smsc_crc(mcast, 3, filter);
1595 filter++;
1596 }
1597
1598 if (pdata->wolopts & WAKE_ARP) {
1599 const u8 arp[] = {0x08, 0x06};
Joe Perches1e1d7412012-11-24 01:27:49 +00001600 netdev_info(dev->net, "enabling ARP detection\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001601 filter_mask[filter * 4] = 0x0003;
1602 filter_mask[filter * 4 + 1] = 0x00;
1603 filter_mask[filter * 4 + 2] = 0x00;
1604 filter_mask[filter * 4 + 3] = 0x00;
1605 command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1606 offset[filter/4] |= 0x0C << ((filter % 4) * 8);
1607 crc[filter/2] |= smsc_crc(arp, 2, filter);
1608 filter++;
1609 }
1610
1611 if (pdata->wolopts & WAKE_UCAST) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001612 netdev_info(dev->net, "enabling unicast detection\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001613 filter_mask[filter * 4] = 0x003F;
1614 filter_mask[filter * 4 + 1] = 0x00;
1615 filter_mask[filter * 4 + 2] = 0x00;
1616 filter_mask[filter * 4 + 3] = 0x00;
1617 command[filter/4] |= 0x01UL << ((filter % 4) * 8);
1618 offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1619 crc[filter/2] |= smsc_crc(dev->net->dev_addr, ETH_ALEN, filter);
1620 filter++;
1621 }
1622
Steve Glendinning9ebca502012-11-22 08:05:23 +00001623 for (i = 0; i < (wuff_filter_count * 4); i++) {
Ming Leiec321152012-11-06 04:53:07 +00001624 ret = smsc95xx_write_reg_nopm(dev, WUFF, filter_mask[i]);
Steve Glendinningb052e072012-11-30 05:55:52 +00001625 if (ret < 0) {
Ming Lei06a221b2012-11-06 04:53:06 +00001626 kfree(filter_mask);
Steve Glendinningb052e072012-11-30 05:55:52 +00001627 goto done;
1628 }
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001629 }
Ming Lei06a221b2012-11-06 04:53:06 +00001630 kfree(filter_mask);
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001631
Steve Glendinning9ebca502012-11-22 08:05:23 +00001632 for (i = 0; i < (wuff_filter_count / 4); i++) {
Ming Leiec321152012-11-06 04:53:07 +00001633 ret = smsc95xx_write_reg_nopm(dev, WUFF, command[i]);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001634 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001635 goto done;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001636 }
1637
Steve Glendinning9ebca502012-11-22 08:05:23 +00001638 for (i = 0; i < (wuff_filter_count / 4); i++) {
Ming Leiec321152012-11-06 04:53:07 +00001639 ret = smsc95xx_write_reg_nopm(dev, WUFF, offset[i]);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001640 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001641 goto done;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001642 }
1643
Steve Glendinning9ebca502012-11-22 08:05:23 +00001644 for (i = 0; i < (wuff_filter_count / 2); i++) {
Ming Leiec321152012-11-06 04:53:07 +00001645 ret = smsc95xx_write_reg_nopm(dev, WUFF, crc[i]);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001646 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001647 goto done;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001648 }
1649
1650 /* clear any pending pattern match packet status */
Ming Leiec321152012-11-06 04:53:07 +00001651 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001652 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001653 goto done;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001654
1655 val |= WUCSR_WUFR_;
1656
Ming Leiec321152012-11-06 04:53:07 +00001657 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001658 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001659 goto done;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001660 }
1661
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001662 if (pdata->wolopts & WAKE_MAGIC) {
1663 /* clear any pending magic packet status */
Ming Leiec321152012-11-06 04:53:07 +00001664 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001665 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001666 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001667
1668 val |= WUCSR_MPR_;
1669
Ming Leiec321152012-11-06 04:53:07 +00001670 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001671 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001672 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001673 }
1674
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001675 /* enable/disable wakeup sources */
Ming Leiec321152012-11-06 04:53:07 +00001676 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001677 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001678 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001679
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001680 if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001681 netdev_info(dev->net, "enabling pattern match wakeup\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001682 val |= WUCSR_WAKE_EN_;
1683 } else {
Joe Perches1e1d7412012-11-24 01:27:49 +00001684 netdev_info(dev->net, "disabling pattern match wakeup\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001685 val &= ~WUCSR_WAKE_EN_;
1686 }
1687
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001688 if (pdata->wolopts & WAKE_MAGIC) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001689 netdev_info(dev->net, "enabling magic packet wakeup\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001690 val |= WUCSR_MPEN_;
1691 } else {
Joe Perches1e1d7412012-11-24 01:27:49 +00001692 netdev_info(dev->net, "disabling magic packet wakeup\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001693 val &= ~WUCSR_MPEN_;
1694 }
1695
Ming Leiec321152012-11-06 04:53:07 +00001696 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001697 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001698 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001699
1700 /* enable wol wakeup source */
Ming Leiec321152012-11-06 04:53:07 +00001701 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001702 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001703 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001704
1705 val |= PM_CTL_WOL_EN_;
1706
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001707 /* phy energy detect wakeup source */
1708 if (pdata->wolopts & WAKE_PHY)
1709 val |= PM_CTL_ED_EN_;
1710
Ming Leiec321152012-11-06 04:53:07 +00001711 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001712 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001713 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001714
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001715 /* enable receiver to enable frame reception */
Ming Leiec321152012-11-06 04:53:07 +00001716 smsc95xx_start_rx_path(dev, 1);
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001717
1718 /* some wol options are enabled, so enter SUSPEND0 */
Joe Perches1e1d7412012-11-24 01:27:49 +00001719 netdev_info(dev->net, "entering SUSPEND0 mode\n");
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001720 ret = smsc95xx_enter_suspend0(dev);
1721
1722done:
Ming Lei0d41be52013-03-15 12:08:58 +08001723 /*
1724 * TODO: resume() might need to handle the suspend failure
1725 * in system sleep
1726 */
1727 if (ret && PMSG_IS_AUTO(message))
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001728 usbnet_resume(intf);
1729 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001730}
1731
1732static int smsc95xx_resume(struct usb_interface *intf)
1733{
1734 struct usbnet *dev = usb_get_intfdata(intf);
Sudip Mukherjee8bca81d2014-11-11 14:10:47 +05301735 struct smsc95xx_priv *pdata;
1736 u8 suspend_flags;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001737 int ret;
1738 u32 val;
1739
1740 BUG_ON(!dev);
Sudip Mukherjee8bca81d2014-11-11 14:10:47 +05301741 pdata = (struct smsc95xx_priv *)(dev->data[0]);
1742 suspend_flags = pdata->suspend_flags;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001743
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001744 netdev_dbg(dev->net, "resume suspend_flags=0x%02x\n", suspend_flags);
1745
1746 /* do this first to ensure it's cleared even in error case */
1747 pdata->suspend_flags = 0;
Christoph Fritzd69d1692016-05-26 04:06:47 +02001748 schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001749
1750 if (suspend_flags & SUSPEND_ALLMODES) {
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001751 /* clear wake-up sources */
Ming Leiec321152012-11-06 04:53:07 +00001752 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001753 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001754 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001755
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001756 val &= ~(WUCSR_WAKE_EN_ | WUCSR_MPEN_);
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001757
Ming Leiec321152012-11-06 04:53:07 +00001758 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001759 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001760 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001761
1762 /* clear wake-up status */
Ming Leiec321152012-11-06 04:53:07 +00001763 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001764 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001765 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001766
1767 val &= ~PM_CTL_WOL_EN_;
1768 val |= PM_CTL_WUPS_;
1769
Ming Leiec321152012-11-06 04:53:07 +00001770 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001771 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001772 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001773 }
1774
Steve Glendinningaf3d7c12012-11-22 08:05:22 +00001775 ret = usbnet_resume(intf);
Steve Glendinningb052e072012-11-30 05:55:52 +00001776 if (ret < 0)
1777 netdev_warn(dev->net, "usbnet_resume error\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001778
Steve Glendinningb052e072012-11-30 05:55:52 +00001779 return ret;
Steve Glendinningb5a04472012-09-28 00:07:11 +00001780}
1781
Joonyoung Shimb4df4802014-07-10 11:49:42 +09001782static int smsc95xx_reset_resume(struct usb_interface *intf)
1783{
1784 struct usbnet *dev = usb_get_intfdata(intf);
1785 int ret;
1786
1787 ret = smsc95xx_reset(dev);
1788 if (ret < 0)
1789 return ret;
1790
1791 return smsc95xx_resume(intf);
1792}
1793
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001794static void smsc95xx_rx_csum_offload(struct sk_buff *skb)
1795{
1796 skb->csum = *(u16 *)(skb_tail_pointer(skb) - 2);
1797 skb->ip_summed = CHECKSUM_COMPLETE;
1798 skb_trim(skb, skb->len - 2);
1799}
1800
1801static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
1802{
Emil Goodeeb855692014-02-13 17:50:19 +01001803 /* This check is no longer done by usbnet */
1804 if (skb->len < dev->net->hard_header_len)
1805 return 0;
1806
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001807 while (skb->len > 0) {
1808 u32 header, align_count;
1809 struct sk_buff *ax_skb;
1810 unsigned char *packet;
1811 u16 size;
1812
1813 memcpy(&header, skb->data, sizeof(header));
1814 le32_to_cpus(&header);
1815 skb_pull(skb, 4 + NET_IP_ALIGN);
1816 packet = skb->data;
1817
1818 /* get the packet length */
1819 size = (u16)((header & RX_STS_FL_) >> 16);
1820 align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4;
1821
1822 if (unlikely(header & RX_STS_ES_)) {
Joe Perchesa475f602010-02-17 10:30:24 +00001823 netif_dbg(dev, rx_err, dev->net,
1824 "Error header=0x%08x\n", header);
Herbert Xu80667ac2009-06-29 16:53:00 +00001825 dev->net->stats.rx_errors++;
1826 dev->net->stats.rx_dropped++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001827
1828 if (header & RX_STS_CRC_) {
Herbert Xu80667ac2009-06-29 16:53:00 +00001829 dev->net->stats.rx_crc_errors++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001830 } else {
1831 if (header & (RX_STS_TL_ | RX_STS_RF_))
Herbert Xu80667ac2009-06-29 16:53:00 +00001832 dev->net->stats.rx_frame_errors++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001833
1834 if ((header & RX_STS_LE_) &&
1835 (!(header & RX_STS_FT_)))
Herbert Xu80667ac2009-06-29 16:53:00 +00001836 dev->net->stats.rx_length_errors++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001837 }
1838 } else {
1839 /* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
1840 if (unlikely(size > (ETH_FRAME_LEN + 12))) {
Joe Perchesa475f602010-02-17 10:30:24 +00001841 netif_dbg(dev, rx_err, dev->net,
1842 "size err header=0x%08x\n", header);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001843 return 0;
1844 }
1845
1846 /* last frame in this batch */
1847 if (skb->len == size) {
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001848 if (dev->net->features & NETIF_F_RXCSUM)
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001849 smsc95xx_rx_csum_offload(skb);
Peter Korsgaarddf18acc2009-05-13 10:10:41 +00001850 skb_trim(skb, skb->len - 4); /* remove fcs */
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001851 skb->truesize = size + sizeof(struct sk_buff);
1852
1853 return 1;
1854 }
1855
1856 ax_skb = skb_clone(skb, GFP_ATOMIC);
1857 if (unlikely(!ax_skb)) {
Joe Perches60b86752010-02-17 10:30:23 +00001858 netdev_warn(dev->net, "Error allocating skb\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001859 return 0;
1860 }
1861
1862 ax_skb->len = size;
1863 ax_skb->data = packet;
1864 skb_set_tail_pointer(ax_skb, size);
1865
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001866 if (dev->net->features & NETIF_F_RXCSUM)
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001867 smsc95xx_rx_csum_offload(ax_skb);
Peter Korsgaarddf18acc2009-05-13 10:10:41 +00001868 skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001869 ax_skb->truesize = size + sizeof(struct sk_buff);
1870
1871 usbnet_skb_return(dev, ax_skb);
1872 }
1873
1874 skb_pull(skb, size);
1875
1876 /* padding bytes before the next frame starts */
1877 if (skb->len)
1878 skb_pull(skb, align_count);
1879 }
1880
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001881 return 1;
1882}
1883
Steve Glendinningf7b29272008-11-20 04:19:21 -08001884static u32 smsc95xx_calc_csum_preamble(struct sk_buff *skb)
1885{
Michał Mirosław55508d62010-12-14 15:24:08 +00001886 u16 low_16 = (u16)skb_checksum_start_offset(skb);
1887 u16 high_16 = low_16 + skb->csum_offset;
Steve Glendinningf7b29272008-11-20 04:19:21 -08001888 return (high_16 << 16) | low_16;
1889}
1890
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001891static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,
1892 struct sk_buff *skb, gfp_t flags)
1893{
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001894 bool csum = skb->ip_summed == CHECKSUM_PARTIAL;
Steve Glendinningf7b29272008-11-20 04:19:21 -08001895 int overhead = csum ? SMSC95XX_TX_OVERHEAD_CSUM : SMSC95XX_TX_OVERHEAD;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001896 u32 tx_cmd_a, tx_cmd_b;
1897
Steve Glendinningf7b29272008-11-20 04:19:21 -08001898 /* We do not advertise SG, so skbs should be already linearized */
1899 BUG_ON(skb_shinfo(skb)->nr_frags);
1900
1901 if (skb_headroom(skb) < overhead) {
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001902 struct sk_buff *skb2 = skb_copy_expand(skb,
Steve Glendinningf7b29272008-11-20 04:19:21 -08001903 overhead, 0, flags);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001904 dev_kfree_skb_any(skb);
1905 skb = skb2;
1906 if (!skb)
1907 return NULL;
1908 }
1909
Steve Glendinningf7b29272008-11-20 04:19:21 -08001910 if (csum) {
Steve Glendinning11bc3082010-03-18 22:18:41 -07001911 if (skb->len <= 45) {
1912 /* workaround - hardware tx checksum does not work
1913 * properly with extremely small packets */
Michał Mirosław55508d62010-12-14 15:24:08 +00001914 long csstart = skb_checksum_start_offset(skb);
Steve Glendinning11bc3082010-03-18 22:18:41 -07001915 __wsum calc = csum_partial(skb->data + csstart,
1916 skb->len - csstart, 0);
1917 *((__sum16 *)(skb->data + csstart
1918 + skb->csum_offset)) = csum_fold(calc);
1919
1920 csum = false;
1921 } else {
1922 u32 csum_preamble = smsc95xx_calc_csum_preamble(skb);
1923 skb_push(skb, 4);
Steve Glendinning00acda62012-11-02 00:44:20 +00001924 cpu_to_le32s(&csum_preamble);
Steve Glendinning11bc3082010-03-18 22:18:41 -07001925 memcpy(skb->data, &csum_preamble, 4);
1926 }
Steve Glendinningf7b29272008-11-20 04:19:21 -08001927 }
1928
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001929 skb_push(skb, 4);
1930 tx_cmd_b = (u32)(skb->len - 4);
Steve Glendinningf7b29272008-11-20 04:19:21 -08001931 if (csum)
1932 tx_cmd_b |= TX_CMD_B_CSUM_ENABLE;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001933 cpu_to_le32s(&tx_cmd_b);
1934 memcpy(skb->data, &tx_cmd_b, 4);
1935
1936 skb_push(skb, 4);
1937 tx_cmd_a = (u32)(skb->len - 8) | TX_CMD_A_FIRST_SEG_ |
1938 TX_CMD_A_LAST_SEG_;
1939 cpu_to_le32s(&tx_cmd_a);
1940 memcpy(skb->data, &tx_cmd_a, 4);
1941
1942 return skb;
1943}
1944
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001945static int smsc95xx_manage_power(struct usbnet *dev, int on)
1946{
1947 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1948
1949 dev->intf->needs_remote_wakeup = on;
1950
Ming Leieb970ff2013-02-22 03:05:05 +00001951 if (pdata->features & FEATURE_REMOTE_WAKEUP)
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001952 return 0;
1953
Ming Leieb970ff2013-02-22 03:05:05 +00001954 /* this chip revision isn't capable of remote wakeup */
1955 netdev_info(dev->net, "hardware isn't capable of remote wakeup\n");
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001956
1957 if (on)
1958 usb_autopm_get_interface_no_resume(dev->intf);
1959 else
1960 usb_autopm_put_interface(dev->intf);
1961
1962 return 0;
1963}
1964
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001965static const struct driver_info smsc95xx_info = {
1966 .description = "smsc95xx USB 2.0 Ethernet",
1967 .bind = smsc95xx_bind,
1968 .unbind = smsc95xx_unbind,
1969 .link_reset = smsc95xx_link_reset,
1970 .reset = smsc95xx_reset,
1971 .rx_fixup = smsc95xx_rx_fixup,
1972 .tx_fixup = smsc95xx_tx_fixup,
1973 .status = smsc95xx_status,
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001974 .manage_power = smsc95xx_manage_power,
Paolo Pisati07d69d42012-04-23 04:05:20 +00001975 .flags = FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001976};
1977
1978static const struct usb_device_id products[] = {
1979 {
1980 /* SMSC9500 USB Ethernet Device */
1981 USB_DEVICE(0x0424, 0x9500),
1982 .driver_info = (unsigned long) &smsc95xx_info,
1983 },
Steve Glendinning726474b2009-05-01 06:07:22 +00001984 {
Steve Glendinning6f41d122009-09-22 05:13:02 +00001985 /* SMSC9505 USB Ethernet Device */
1986 USB_DEVICE(0x0424, 0x9505),
1987 .driver_info = (unsigned long) &smsc95xx_info,
1988 },
1989 {
1990 /* SMSC9500A USB Ethernet Device */
1991 USB_DEVICE(0x0424, 0x9E00),
1992 .driver_info = (unsigned long) &smsc95xx_info,
1993 },
1994 {
1995 /* SMSC9505A USB Ethernet Device */
1996 USB_DEVICE(0x0424, 0x9E01),
1997 .driver_info = (unsigned long) &smsc95xx_info,
1998 },
1999 {
Steve Glendinning726474b2009-05-01 06:07:22 +00002000 /* SMSC9512/9514 USB Hub & Ethernet Device */
2001 USB_DEVICE(0x0424, 0xec00),
2002 .driver_info = (unsigned long) &smsc95xx_info,
2003 },
Steve Glendinning6f41d122009-09-22 05:13:02 +00002004 {
2005 /* SMSC9500 USB Ethernet Device (SAL10) */
2006 USB_DEVICE(0x0424, 0x9900),
2007 .driver_info = (unsigned long) &smsc95xx_info,
2008 },
2009 {
2010 /* SMSC9505 USB Ethernet Device (SAL10) */
2011 USB_DEVICE(0x0424, 0x9901),
2012 .driver_info = (unsigned long) &smsc95xx_info,
2013 },
2014 {
2015 /* SMSC9500A USB Ethernet Device (SAL10) */
2016 USB_DEVICE(0x0424, 0x9902),
2017 .driver_info = (unsigned long) &smsc95xx_info,
2018 },
2019 {
2020 /* SMSC9505A USB Ethernet Device (SAL10) */
2021 USB_DEVICE(0x0424, 0x9903),
2022 .driver_info = (unsigned long) &smsc95xx_info,
2023 },
2024 {
2025 /* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */
2026 USB_DEVICE(0x0424, 0x9904),
2027 .driver_info = (unsigned long) &smsc95xx_info,
2028 },
2029 {
2030 /* SMSC9500A USB Ethernet Device (HAL) */
2031 USB_DEVICE(0x0424, 0x9905),
2032 .driver_info = (unsigned long) &smsc95xx_info,
2033 },
2034 {
2035 /* SMSC9505A USB Ethernet Device (HAL) */
2036 USB_DEVICE(0x0424, 0x9906),
2037 .driver_info = (unsigned long) &smsc95xx_info,
2038 },
2039 {
2040 /* SMSC9500 USB Ethernet Device (Alternate ID) */
2041 USB_DEVICE(0x0424, 0x9907),
2042 .driver_info = (unsigned long) &smsc95xx_info,
2043 },
2044 {
2045 /* SMSC9500A USB Ethernet Device (Alternate ID) */
2046 USB_DEVICE(0x0424, 0x9908),
2047 .driver_info = (unsigned long) &smsc95xx_info,
2048 },
2049 {
2050 /* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */
2051 USB_DEVICE(0x0424, 0x9909),
2052 .driver_info = (unsigned long) &smsc95xx_info,
2053 },
Steve Glendinning88edaa42011-04-10 18:59:27 -07002054 {
2055 /* SMSC LAN9530 USB Ethernet Device */
2056 USB_DEVICE(0x0424, 0x9530),
2057 .driver_info = (unsigned long) &smsc95xx_info,
2058 },
2059 {
2060 /* SMSC LAN9730 USB Ethernet Device */
2061 USB_DEVICE(0x0424, 0x9730),
2062 .driver_info = (unsigned long) &smsc95xx_info,
2063 },
2064 {
2065 /* SMSC LAN89530 USB Ethernet Device */
2066 USB_DEVICE(0x0424, 0x9E08),
2067 .driver_info = (unsigned long) &smsc95xx_info,
2068 },
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002069 { }, /* END */
2070};
2071MODULE_DEVICE_TABLE(usb, products);
2072
2073static struct usb_driver smsc95xx_driver = {
2074 .name = "smsc95xx",
2075 .id_table = products,
2076 .probe = usbnet_probe,
Steve Glendinningb5a04472012-09-28 00:07:11 +00002077 .suspend = smsc95xx_suspend,
Steve Glendinninge0e474a2012-09-28 00:07:12 +00002078 .resume = smsc95xx_resume,
Joonyoung Shimb4df4802014-07-10 11:49:42 +09002079 .reset_resume = smsc95xx_reset_resume,
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002080 .disconnect = usbnet_disconnect,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07002081 .disable_hub_initiated_lpm = 1,
Steve Glendinningb2d4b152013-01-03 03:00:16 +00002082 .supports_autosuspend = 1,
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002083};
2084
Greg Kroah-Hartmand632eb12011-11-18 09:44:20 -08002085module_usb_driver(smsc95xx_driver);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002086
2087MODULE_AUTHOR("Nancy Lin");
Steve Glendinning90b24cf2012-04-16 12:13:29 +01002088MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002089MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices");
2090MODULE_LICENSE("GPL");