blob: c2f67cecdf5bfcd4d7b07566c8aaaf7ec14269e2 [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"
Woojung Huh13722bb2016-09-02 20:34:22 +000036#define SMSC_DRIVER_VERSION "1.0.5"
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 {
Woojung Huh13722bb2016-09-02 20:34:22 +000067 u32 chip_id;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000068 u32 mac_cr;
Marc Zyngier3c0f3c62011-03-18 03:53:58 +000069 u32 hash_hi;
70 u32 hash_lo;
Steve Glendinninge0e474a2012-09-28 00:07:12 +000071 u32 wolopts;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000072 spinlock_t mac_cr_lock;
Steve Glendinning9ebca502012-11-22 08:05:23 +000073 u8 features;
Steve Glendinningb2d4b152013-01-03 03:00:16 +000074 u8 suspend_flags;
Woojung Huh13722bb2016-09-02 20:34:22 +000075 u8 mdix_ctrl;
Christoph Fritzd69d1692016-05-26 04:06:47 +020076 bool link_ok;
77 struct delayed_work carrier_check;
78 struct usbnet *dev;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000079};
80
Rusty Russelleb939922011-12-19 14:08:01 +000081static bool turbo_mode = true;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000082module_param(turbo_mode, bool, 0644);
83MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
84
Ming Leiec321152012-11-06 04:53:07 +000085static int __must_check __smsc95xx_read_reg(struct usbnet *dev, u32 index,
86 u32 *data, int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +000087{
Ming Lei72108fd2012-10-24 19:47:04 +000088 u32 buf;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000089 int ret;
Ming Leiec321152012-11-06 04:53:07 +000090 int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
Steve Glendinning2f7ca802008-10-02 05:27:57 +000091
92 BUG_ON(!dev);
93
Ming Leiec321152012-11-06 04:53:07 +000094 if (!in_pm)
95 fn = usbnet_read_cmd;
96 else
97 fn = usbnet_read_cmd_nopm;
98
99 ret = fn(dev, USB_VENDOR_REQUEST_READ_REGISTER, USB_DIR_IN
100 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
101 0, index, &buf, 4);
Dan Carpenter5a36b682016-05-04 09:22:01 +0300102 if (unlikely(ret < 0)) {
Joe Perches1e1d7412012-11-24 01:27:49 +0000103 netdev_warn(dev->net, "Failed to read reg index 0x%08x: %d\n",
104 index, ret);
Dan Carpenter5a36b682016-05-04 09:22:01 +0300105 return ret;
106 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000107
Ming Lei72108fd2012-10-24 19:47:04 +0000108 le32_to_cpus(&buf);
109 *data = buf;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000110
111 return ret;
112}
113
Ming Leiec321152012-11-06 04:53:07 +0000114static int __must_check __smsc95xx_write_reg(struct usbnet *dev, u32 index,
115 u32 data, int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000116{
Ming Lei72108fd2012-10-24 19:47:04 +0000117 u32 buf;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000118 int ret;
Ming Leiec321152012-11-06 04:53:07 +0000119 int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000120
121 BUG_ON(!dev);
122
Ming Leiec321152012-11-06 04:53:07 +0000123 if (!in_pm)
124 fn = usbnet_write_cmd;
125 else
126 fn = usbnet_write_cmd_nopm;
127
Ming Lei72108fd2012-10-24 19:47:04 +0000128 buf = data;
129 cpu_to_le32s(&buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000130
Ming Leiec321152012-11-06 04:53:07 +0000131 ret = fn(dev, USB_VENDOR_REQUEST_WRITE_REGISTER, USB_DIR_OUT
132 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
133 0, index, &buf, 4);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000134 if (unlikely(ret < 0))
Joe Perches1e1d7412012-11-24 01:27:49 +0000135 netdev_warn(dev->net, "Failed to write reg index 0x%08x: %d\n",
136 index, ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000137
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000138 return ret;
139}
140
Ming Leiec321152012-11-06 04:53:07 +0000141static int __must_check smsc95xx_read_reg_nopm(struct usbnet *dev, u32 index,
142 u32 *data)
143{
144 return __smsc95xx_read_reg(dev, index, data, 1);
145}
146
147static int __must_check smsc95xx_write_reg_nopm(struct usbnet *dev, u32 index,
148 u32 data)
149{
150 return __smsc95xx_write_reg(dev, index, data, 1);
151}
152
153static int __must_check smsc95xx_read_reg(struct usbnet *dev, u32 index,
154 u32 *data)
155{
156 return __smsc95xx_read_reg(dev, index, data, 0);
157}
158
159static int __must_check smsc95xx_write_reg(struct usbnet *dev, u32 index,
160 u32 data)
161{
162 return __smsc95xx_write_reg(dev, index, data, 0);
163}
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000164
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000165/* Loop until the read is completed with timeout
166 * called with phy_mutex held */
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000167static int __must_check __smsc95xx_phy_wait_not_busy(struct usbnet *dev,
168 int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000169{
170 unsigned long start_time = jiffies;
171 u32 val;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000172 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000173
174 do {
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000175 ret = __smsc95xx_read_reg(dev, MII_ADDR, &val, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000176 if (ret < 0) {
177 netdev_warn(dev->net, "Error reading MII_ACCESS\n");
178 return ret;
179 }
180
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000181 if (!(val & MII_BUSY_))
182 return 0;
183 } while (!time_after(jiffies, start_time + HZ));
184
185 return -EIO;
186}
187
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000188static int __smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx,
189 int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000190{
191 struct usbnet *dev = netdev_priv(netdev);
192 u32 val, addr;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000193 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000194
195 mutex_lock(&dev->phy_mutex);
196
197 /* confirm MII not busy */
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000198 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000199 if (ret < 0) {
200 netdev_warn(dev->net, "MII is busy in smsc95xx_mdio_read\n");
201 goto done;
202 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000203
204 /* set the address, index & direction (read from PHY) */
205 phy_id &= dev->mii.phy_id_mask;
206 idx &= dev->mii.reg_num_mask;
Steve Glendinning80928802012-11-06 00:08:53 +0000207 addr = (phy_id << 11) | (idx << 6) | MII_READ_ | MII_BUSY_;
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000208 ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000209 if (ret < 0) {
210 netdev_warn(dev->net, "Error writing MII_ADDR\n");
211 goto done;
212 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000213
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000214 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000215 if (ret < 0) {
216 netdev_warn(dev->net, "Timed out reading MII reg %02X\n", idx);
217 goto done;
218 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000219
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000220 ret = __smsc95xx_read_reg(dev, MII_DATA, &val, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000221 if (ret < 0) {
222 netdev_warn(dev->net, "Error reading MII_DATA\n");
223 goto done;
224 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000225
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000226 ret = (u16)(val & 0xFFFF);
227
228done:
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000229 mutex_unlock(&dev->phy_mutex);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000230 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000231}
232
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000233static void __smsc95xx_mdio_write(struct net_device *netdev, int phy_id,
234 int idx, int regval, int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000235{
236 struct usbnet *dev = netdev_priv(netdev);
237 u32 val, addr;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000238 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000239
240 mutex_lock(&dev->phy_mutex);
241
242 /* confirm MII not busy */
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000243 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000244 if (ret < 0) {
245 netdev_warn(dev->net, "MII is busy in smsc95xx_mdio_write\n");
246 goto done;
247 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000248
249 val = regval;
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000250 ret = __smsc95xx_write_reg(dev, MII_DATA, val, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000251 if (ret < 0) {
252 netdev_warn(dev->net, "Error writing MII_DATA\n");
253 goto done;
254 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000255
256 /* set the address, index & direction (write to PHY) */
257 phy_id &= dev->mii.phy_id_mask;
258 idx &= dev->mii.reg_num_mask;
Steve Glendinning80928802012-11-06 00:08:53 +0000259 addr = (phy_id << 11) | (idx << 6) | MII_WRITE_ | MII_BUSY_;
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000260 ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000261 if (ret < 0) {
262 netdev_warn(dev->net, "Error writing MII_ADDR\n");
263 goto done;
264 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000265
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000266 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000267 if (ret < 0) {
268 netdev_warn(dev->net, "Timed out writing MII reg %02X\n", idx);
269 goto done;
270 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000271
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000272done:
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000273 mutex_unlock(&dev->phy_mutex);
274}
275
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000276static int smsc95xx_mdio_read_nopm(struct net_device *netdev, int phy_id,
277 int idx)
278{
279 return __smsc95xx_mdio_read(netdev, phy_id, idx, 1);
280}
281
282static void smsc95xx_mdio_write_nopm(struct net_device *netdev, int phy_id,
283 int idx, int regval)
284{
285 __smsc95xx_mdio_write(netdev, phy_id, idx, regval, 1);
286}
287
288static int smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
289{
290 return __smsc95xx_mdio_read(netdev, phy_id, idx, 0);
291}
292
293static void smsc95xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
294 int regval)
295{
296 __smsc95xx_mdio_write(netdev, phy_id, idx, regval, 0);
297}
298
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000299static int __must_check smsc95xx_wait_eeprom(struct usbnet *dev)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000300{
301 unsigned long start_time = jiffies;
302 u32 val;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000303 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000304
305 do {
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000306 ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
Steve Glendinningb052e072012-11-30 05:55:52 +0000307 if (ret < 0) {
308 netdev_warn(dev->net, "Error reading E2P_CMD\n");
309 return ret;
310 }
311
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000312 if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_))
313 break;
314 udelay(40);
315 } while (!time_after(jiffies, start_time + HZ));
316
317 if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) {
Joe Perches60b86752010-02-17 10:30:23 +0000318 netdev_warn(dev->net, "EEPROM read operation timeout\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000319 return -EIO;
320 }
321
322 return 0;
323}
324
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000325static int __must_check smsc95xx_eeprom_confirm_not_busy(struct usbnet *dev)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000326{
327 unsigned long start_time = jiffies;
328 u32 val;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000329 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000330
331 do {
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000332 ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
Steve Glendinningb052e072012-11-30 05:55:52 +0000333 if (ret < 0) {
334 netdev_warn(dev->net, "Error reading E2P_CMD\n");
335 return ret;
336 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000337
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000338 if (!(val & E2P_CMD_BUSY_))
339 return 0;
340
341 udelay(40);
342 } while (!time_after(jiffies, start_time + HZ));
343
Joe Perches60b86752010-02-17 10:30:23 +0000344 netdev_warn(dev->net, "EEPROM is busy\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000345 return -EIO;
346}
347
348static int smsc95xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
349 u8 *data)
350{
351 u32 val;
352 int i, ret;
353
354 BUG_ON(!dev);
355 BUG_ON(!data);
356
357 ret = smsc95xx_eeprom_confirm_not_busy(dev);
358 if (ret)
359 return ret;
360
361 for (i = 0; i < length; i++) {
362 val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000363 ret = smsc95xx_write_reg(dev, E2P_CMD, val);
Steve Glendinningb052e072012-11-30 05:55:52 +0000364 if (ret < 0) {
365 netdev_warn(dev->net, "Error writing E2P_CMD\n");
366 return ret;
367 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000368
369 ret = smsc95xx_wait_eeprom(dev);
370 if (ret < 0)
371 return ret;
372
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000373 ret = smsc95xx_read_reg(dev, E2P_DATA, &val);
Steve Glendinningb052e072012-11-30 05:55:52 +0000374 if (ret < 0) {
375 netdev_warn(dev->net, "Error reading E2P_DATA\n");
376 return ret;
377 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000378
379 data[i] = val & 0xFF;
380 offset++;
381 }
382
383 return 0;
384}
385
386static int smsc95xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
387 u8 *data)
388{
389 u32 val;
390 int i, ret;
391
392 BUG_ON(!dev);
393 BUG_ON(!data);
394
395 ret = smsc95xx_eeprom_confirm_not_busy(dev);
396 if (ret)
397 return ret;
398
399 /* Issue write/erase enable command */
400 val = E2P_CMD_BUSY_ | E2P_CMD_EWEN_;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000401 ret = smsc95xx_write_reg(dev, E2P_CMD, val);
Steve Glendinningb052e072012-11-30 05:55:52 +0000402 if (ret < 0) {
403 netdev_warn(dev->net, "Error writing E2P_DATA\n");
404 return ret;
405 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000406
407 ret = smsc95xx_wait_eeprom(dev);
408 if (ret < 0)
409 return ret;
410
411 for (i = 0; i < length; i++) {
412
413 /* Fill data register */
414 val = data[i];
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000415 ret = smsc95xx_write_reg(dev, E2P_DATA, val);
Steve Glendinningb052e072012-11-30 05:55:52 +0000416 if (ret < 0) {
417 netdev_warn(dev->net, "Error writing E2P_DATA\n");
418 return ret;
419 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000420
421 /* Send "write" command */
422 val = E2P_CMD_BUSY_ | E2P_CMD_WRITE_ | (offset & E2P_CMD_ADDR_);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000423 ret = smsc95xx_write_reg(dev, E2P_CMD, val);
Steve Glendinningb052e072012-11-30 05:55:52 +0000424 if (ret < 0) {
425 netdev_warn(dev->net, "Error writing E2P_CMD\n");
426 return ret;
427 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000428
429 ret = smsc95xx_wait_eeprom(dev);
430 if (ret < 0)
431 return ret;
432
433 offset++;
434 }
435
436 return 0;
437}
438
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000439static int __must_check smsc95xx_write_reg_async(struct usbnet *dev, u16 index,
Steve Glendinning7b9e7582012-12-10 01:03:08 +0000440 u32 data)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000441{
Steve Glendinning1d74a6b2008-10-09 14:34:47 -0700442 const u16 size = 4;
Steve Glendinning7b9e7582012-12-10 01:03:08 +0000443 u32 buf;
Ming Lei72108fd2012-10-24 19:47:04 +0000444 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000445
Steve Glendinning7b9e7582012-12-10 01:03:08 +0000446 buf = data;
447 cpu_to_le32s(&buf);
448
Ming Lei72108fd2012-10-24 19:47:04 +0000449 ret = usbnet_write_cmd_async(dev, USB_VENDOR_REQUEST_WRITE_REGISTER,
450 USB_DIR_OUT | USB_TYPE_VENDOR |
451 USB_RECIP_DEVICE,
Steve Glendinning7b9e7582012-12-10 01:03:08 +0000452 0, index, &buf, size);
Ming Lei72108fd2012-10-24 19:47:04 +0000453 if (ret < 0)
454 netdev_warn(dev->net, "Error write async cmd, sts=%d\n",
455 ret);
456 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000457}
458
459/* returns hash bit number for given MAC address
460 * example:
461 * 01 00 5E 00 00 01 -> returns bit number 31 */
462static unsigned int smsc95xx_hash(char addr[ETH_ALEN])
463{
464 return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
465}
466
467static void smsc95xx_set_multicast(struct net_device *netdev)
468{
469 struct usbnet *dev = netdev_priv(netdev);
470 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000471 unsigned long flags;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000472 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000473
Marc Zyngier3c0f3c62011-03-18 03:53:58 +0000474 pdata->hash_hi = 0;
475 pdata->hash_lo = 0;
476
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000477 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
478
479 if (dev->net->flags & IFF_PROMISC) {
Joe Perchesa475f602010-02-17 10:30:24 +0000480 netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000481 pdata->mac_cr |= MAC_CR_PRMS_;
482 pdata->mac_cr &= ~(MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
483 } else if (dev->net->flags & IFF_ALLMULTI) {
Joe Perchesa475f602010-02-17 10:30:24 +0000484 netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000485 pdata->mac_cr |= MAC_CR_MCPAS_;
486 pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_HPFILT_);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000487 } else if (!netdev_mc_empty(dev->net)) {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000488 struct netdev_hw_addr *ha;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000489
490 pdata->mac_cr |= MAC_CR_HPFILT_;
491 pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);
492
Jiri Pirko22bedad32010-04-01 21:22:57 +0000493 netdev_for_each_mc_addr(ha, netdev) {
494 u32 bitnum = smsc95xx_hash(ha->addr);
Jiri Pirkoa92635d2010-02-18 04:02:26 +0000495 u32 mask = 0x01 << (bitnum & 0x1F);
496 if (bitnum & 0x20)
Marc Zyngier3c0f3c62011-03-18 03:53:58 +0000497 pdata->hash_hi |= mask;
Jiri Pirkoa92635d2010-02-18 04:02:26 +0000498 else
Marc Zyngier3c0f3c62011-03-18 03:53:58 +0000499 pdata->hash_lo |= mask;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000500 }
501
Joe Perchesa475f602010-02-17 10:30:24 +0000502 netif_dbg(dev, drv, dev->net, "HASHH=0x%08X, HASHL=0x%08X\n",
Marc Zyngier3c0f3c62011-03-18 03:53:58 +0000503 pdata->hash_hi, pdata->hash_lo);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000504 } else {
Joe Perchesa475f602010-02-17 10:30:24 +0000505 netif_dbg(dev, drv, dev->net, "receive own packets only\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000506 pdata->mac_cr &=
507 ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
508 }
509
510 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
511
512 /* Initiate async writes, as we can't wait for completion here */
Steve Glendinning7b9e7582012-12-10 01:03:08 +0000513 ret = smsc95xx_write_reg_async(dev, HASHH, pdata->hash_hi);
Steve Glendinningb052e072012-11-30 05:55:52 +0000514 if (ret < 0)
515 netdev_warn(dev->net, "failed to initiate async write to HASHH\n");
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000516
Steve Glendinning7b9e7582012-12-10 01:03:08 +0000517 ret = smsc95xx_write_reg_async(dev, HASHL, pdata->hash_lo);
Steve Glendinningb052e072012-11-30 05:55:52 +0000518 if (ret < 0)
519 netdev_warn(dev->net, "failed to initiate async write to HASHL\n");
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000520
Steve Glendinning7b9e7582012-12-10 01:03:08 +0000521 ret = smsc95xx_write_reg_async(dev, MAC_CR, pdata->mac_cr);
Steve Glendinningb052e072012-11-30 05:55:52 +0000522 if (ret < 0)
523 netdev_warn(dev->net, "failed to initiate async write to MAC_CR\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000524}
525
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000526static int smsc95xx_phy_update_flowcontrol(struct usbnet *dev, u8 duplex,
527 u16 lcladv, u16 rmtadv)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000528{
529 u32 flow, afc_cfg = 0;
530
531 int ret = smsc95xx_read_reg(dev, AFC_CFG, &afc_cfg);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000532 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000533 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000534
535 if (duplex == DUPLEX_FULL) {
Steve Glendinningbc02ff92008-12-16 02:00:48 -0800536 u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000537
538 if (cap & FLOW_CTRL_RX)
539 flow = 0xFFFF0002;
540 else
541 flow = 0;
542
543 if (cap & FLOW_CTRL_TX)
544 afc_cfg |= 0xF;
545 else
546 afc_cfg &= ~0xF;
547
Joe Perchesa475f602010-02-17 10:30:24 +0000548 netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
Joe Perches60b86752010-02-17 10:30:23 +0000549 cap & FLOW_CTRL_RX ? "enabled" : "disabled",
550 cap & FLOW_CTRL_TX ? "enabled" : "disabled");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000551 } else {
Joe Perchesa475f602010-02-17 10:30:24 +0000552 netif_dbg(dev, link, dev->net, "half duplex\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000553 flow = 0;
554 afc_cfg |= 0xF;
555 }
556
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000557 ret = smsc95xx_write_reg(dev, FLOW, flow);
Steve Glendinningb052e072012-11-30 05:55:52 +0000558 if (ret < 0)
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000559 return ret;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000560
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000561 return smsc95xx_write_reg(dev, AFC_CFG, afc_cfg);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000562}
563
564static int smsc95xx_link_reset(struct usbnet *dev)
565{
566 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
567 struct mii_if_info *mii = &dev->mii;
David Decotigny8ae6daca2011-04-27 18:32:38 +0000568 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000569 unsigned long flags;
570 u16 lcladv, rmtadv;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000571 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000572
573 /* clear interrupt status */
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000574 ret = smsc95xx_mdio_read(dev->net, mii->phy_id, PHY_INT_SRC);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000575 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000576 return ret;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000577
578 ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000579 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000580 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000581
582 mii_check_media(mii, 1, 1);
583 mii_ethtool_gset(&dev->mii, &ecmd);
584 lcladv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
585 rmtadv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_LPA);
586
David Decotigny8ae6daca2011-04-27 18:32:38 +0000587 netif_dbg(dev, link, dev->net,
588 "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
589 ethtool_cmd_speed(&ecmd), ecmd.duplex, lcladv, rmtadv);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000590
591 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
592 if (ecmd.duplex != DUPLEX_FULL) {
593 pdata->mac_cr &= ~MAC_CR_FDPX_;
594 pdata->mac_cr |= MAC_CR_RCVOWN_;
595 } else {
596 pdata->mac_cr &= ~MAC_CR_RCVOWN_;
597 pdata->mac_cr |= MAC_CR_FDPX_;
598 }
599 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
600
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000601 ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000602 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000603 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000604
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000605 ret = smsc95xx_phy_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
Steve Glendinningb052e072012-11-30 05:55:52 +0000606 if (ret < 0)
607 netdev_warn(dev->net, "Error updating PHY flow control\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000608
Steve Glendinningb052e072012-11-30 05:55:52 +0000609 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000610}
611
612static void smsc95xx_status(struct usbnet *dev, struct urb *urb)
613{
614 u32 intdata;
615
616 if (urb->actual_length != 4) {
Joe Perches60b86752010-02-17 10:30:23 +0000617 netdev_warn(dev->net, "unexpected urb length %d\n",
618 urb->actual_length);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000619 return;
620 }
621
622 memcpy(&intdata, urb->transfer_buffer, 4);
Steve Glendinning1d74a6b2008-10-09 14:34:47 -0700623 le32_to_cpus(&intdata);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000624
Joe Perchesa475f602010-02-17 10:30:24 +0000625 netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000626
627 if (intdata & INT_ENP_PHY_INT_)
628 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
629 else
Joe Perches60b86752010-02-17 10:30:23 +0000630 netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
631 intdata);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000632}
633
Christoph Fritzd69d1692016-05-26 04:06:47 +0200634static void set_carrier(struct usbnet *dev, bool link)
635{
636 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
637
638 if (pdata->link_ok == link)
639 return;
640
641 pdata->link_ok = link;
642
643 if (link)
644 usbnet_link_change(dev, 1, 0);
645 else
646 usbnet_link_change(dev, 0, 0);
647}
648
649static void check_carrier(struct work_struct *work)
650{
651 struct smsc95xx_priv *pdata = container_of(work, struct smsc95xx_priv,
652 carrier_check.work);
653 struct usbnet *dev = pdata->dev;
654 int ret;
655
656 if (pdata->suspend_flags != 0)
657 return;
658
659 ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMSR);
660 if (ret < 0) {
661 netdev_warn(dev->net, "Failed to read MII_BMSR\n");
662 return;
663 }
664 if (ret & BMSR_LSTATUS)
665 set_carrier(dev, 1);
666 else
667 set_carrier(dev, 0);
668
669 schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
670}
671
Steve Glendinningf7b29272008-11-20 04:19:21 -0800672/* Enable or disable Tx & Rx checksum offload engines */
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000673static int smsc95xx_set_features(struct net_device *netdev,
674 netdev_features_t features)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000675{
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700676 struct usbnet *dev = netdev_priv(netdev);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000677 u32 read_buf;
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700678 int ret;
679
680 ret = smsc95xx_read_reg(dev, COE_CR, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000681 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000682 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000683
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700684 if (features & NETIF_F_HW_CSUM)
Steve Glendinningf7b29272008-11-20 04:19:21 -0800685 read_buf |= Tx_COE_EN_;
686 else
687 read_buf &= ~Tx_COE_EN_;
688
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700689 if (features & NETIF_F_RXCSUM)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000690 read_buf |= Rx_COE_EN_;
691 else
692 read_buf &= ~Rx_COE_EN_;
693
694 ret = smsc95xx_write_reg(dev, COE_CR, read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000695 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000696 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000697
Joe Perchesa475f602010-02-17 10:30:24 +0000698 netif_dbg(dev, hw, dev->net, "COE_CR = 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000699 return 0;
700}
701
702static int smsc95xx_ethtool_get_eeprom_len(struct net_device *net)
703{
704 return MAX_EEPROM_SIZE;
705}
706
707static int smsc95xx_ethtool_get_eeprom(struct net_device *netdev,
708 struct ethtool_eeprom *ee, u8 *data)
709{
710 struct usbnet *dev = netdev_priv(netdev);
711
712 ee->magic = LAN95XX_EEPROM_MAGIC;
713
714 return smsc95xx_read_eeprom(dev, ee->offset, ee->len, data);
715}
716
717static int smsc95xx_ethtool_set_eeprom(struct net_device *netdev,
718 struct ethtool_eeprom *ee, u8 *data)
719{
720 struct usbnet *dev = netdev_priv(netdev);
721
722 if (ee->magic != LAN95XX_EEPROM_MAGIC) {
Joe Perches60b86752010-02-17 10:30:23 +0000723 netdev_warn(dev->net, "EEPROM: magic value mismatch, magic = 0x%x\n",
724 ee->magic);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000725 return -EINVAL;
726 }
727
728 return smsc95xx_write_eeprom(dev, ee->offset, ee->len, data);
729}
730
Emeric Vigier9fa32e92012-07-09 17:44:45 -0400731static int smsc95xx_ethtool_getregslen(struct net_device *netdev)
732{
733 /* all smsc95xx registers */
Steve Glendinning96245312012-12-10 01:03:07 +0000734 return COE_CR - ID_REV + sizeof(u32);
Emeric Vigier9fa32e92012-07-09 17:44:45 -0400735}
736
737static void
738smsc95xx_ethtool_getregs(struct net_device *netdev, struct ethtool_regs *regs,
739 void *buf)
740{
741 struct usbnet *dev = netdev_priv(netdev);
Dan Carpenterd3484462012-07-10 20:32:51 +0000742 unsigned int i, j;
743 int retval;
Emeric Vigier9fa32e92012-07-09 17:44:45 -0400744 u32 *data = buf;
745
746 retval = smsc95xx_read_reg(dev, ID_REV, &regs->version);
747 if (retval < 0) {
748 netdev_warn(netdev, "REGS: cannot read ID_REV\n");
749 return;
750 }
751
752 for (i = ID_REV, j = 0; i <= COE_CR; i += (sizeof(u32)), j++) {
753 retval = smsc95xx_read_reg(dev, i, &data[j]);
754 if (retval < 0) {
755 netdev_warn(netdev, "REGS: cannot read reg[%x]\n", i);
756 return;
757 }
758 }
759}
760
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000761static void smsc95xx_ethtool_get_wol(struct net_device *net,
762 struct ethtool_wolinfo *wolinfo)
763{
764 struct usbnet *dev = netdev_priv(net);
765 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
766
767 wolinfo->supported = SUPPORTED_WAKE;
768 wolinfo->wolopts = pdata->wolopts;
769}
770
771static int smsc95xx_ethtool_set_wol(struct net_device *net,
772 struct ethtool_wolinfo *wolinfo)
773{
774 struct usbnet *dev = netdev_priv(net);
775 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
Steve Glendinning3b146922012-11-30 05:55:50 +0000776 int ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000777
778 pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
Steve Glendinning3b146922012-11-30 05:55:50 +0000779
780 ret = device_set_wakeup_enable(&dev->udev->dev, pdata->wolopts);
Steve Glendinningb052e072012-11-30 05:55:52 +0000781 if (ret < 0)
782 netdev_warn(dev->net, "device_set_wakeup_enable error %d\n", ret);
Steve Glendinning3b146922012-11-30 05:55:50 +0000783
Steve Glendinningb052e072012-11-30 05:55:52 +0000784 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000785}
786
Woojung Huh13722bb2016-09-02 20:34:22 +0000787static int get_mdix_status(struct net_device *net)
788{
789 struct usbnet *dev = netdev_priv(net);
790 u32 val;
791 int buf;
792
793 buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, SPECIAL_CTRL_STS);
794 if (buf & SPECIAL_CTRL_STS_OVRRD_AMDIX_) {
795 if (buf & SPECIAL_CTRL_STS_AMDIX_ENABLE_)
796 return ETH_TP_MDI_AUTO;
797 else if (buf & SPECIAL_CTRL_STS_AMDIX_STATE_)
798 return ETH_TP_MDI_X;
799 } else {
800 buf = smsc95xx_read_reg(dev, STRAP_STATUS, &val);
801 if (val & STRAP_STATUS_AMDIX_EN_)
802 return ETH_TP_MDI_AUTO;
803 }
804
805 return ETH_TP_MDI;
806}
807
808static void set_mdix_status(struct net_device *net, __u8 mdix_ctrl)
809{
810 struct usbnet *dev = netdev_priv(net);
811 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
812 int buf;
813
814 if ((pdata->chip_id == ID_REV_CHIP_ID_9500A_) ||
815 (pdata->chip_id == ID_REV_CHIP_ID_9530_) ||
816 (pdata->chip_id == ID_REV_CHIP_ID_89530_) ||
817 (pdata->chip_id == ID_REV_CHIP_ID_9730_)) {
818 /* Extend Manual AutoMDIX timer for 9500A/9500Ai */
819 buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
820 PHY_EDPD_CONFIG);
821 buf |= PHY_EDPD_CONFIG_EXT_CROSSOVER_;
822 smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
823 PHY_EDPD_CONFIG, buf);
824 }
825
826 if (mdix_ctrl == ETH_TP_MDI) {
827 buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
828 SPECIAL_CTRL_STS);
829 buf |= SPECIAL_CTRL_STS_OVRRD_AMDIX_;
830 buf &= ~(SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
831 SPECIAL_CTRL_STS_AMDIX_STATE_);
832 smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
833 SPECIAL_CTRL_STS, buf);
834 } else if (mdix_ctrl == ETH_TP_MDI_X) {
835 buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
836 SPECIAL_CTRL_STS);
837 buf |= SPECIAL_CTRL_STS_OVRRD_AMDIX_;
838 buf &= ~(SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
839 SPECIAL_CTRL_STS_AMDIX_STATE_);
840 buf |= SPECIAL_CTRL_STS_AMDIX_STATE_;
841 smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
842 SPECIAL_CTRL_STS, buf);
843 } else if (mdix_ctrl == ETH_TP_MDI_AUTO) {
844 buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
845 SPECIAL_CTRL_STS);
846 buf &= ~SPECIAL_CTRL_STS_OVRRD_AMDIX_;
847 buf &= ~(SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
848 SPECIAL_CTRL_STS_AMDIX_STATE_);
849 buf |= SPECIAL_CTRL_STS_AMDIX_ENABLE_;
850 smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
851 SPECIAL_CTRL_STS, buf);
852 }
853 pdata->mdix_ctrl = mdix_ctrl;
854}
855
Philippe Reyneseaf9a322017-03-16 23:18:48 +0100856static int smsc95xx_get_link_ksettings(struct net_device *net,
857 struct ethtool_link_ksettings *cmd)
Woojung Huh13722bb2016-09-02 20:34:22 +0000858{
859 struct usbnet *dev = netdev_priv(net);
860 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
861 int retval;
862
Philippe Reyneseaf9a322017-03-16 23:18:48 +0100863 retval = usbnet_get_link_ksettings(net, cmd);
Woojung Huh13722bb2016-09-02 20:34:22 +0000864
Philippe Reyneseaf9a322017-03-16 23:18:48 +0100865 cmd->base.eth_tp_mdix = pdata->mdix_ctrl;
866 cmd->base.eth_tp_mdix_ctrl = pdata->mdix_ctrl;
Woojung Huh13722bb2016-09-02 20:34:22 +0000867
868 return retval;
869}
870
Philippe Reyneseaf9a322017-03-16 23:18:48 +0100871static int smsc95xx_set_link_ksettings(struct net_device *net,
872 const struct ethtool_link_ksettings *cmd)
Woojung Huh13722bb2016-09-02 20:34:22 +0000873{
874 struct usbnet *dev = netdev_priv(net);
875 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
876 int retval;
877
Philippe Reyneseaf9a322017-03-16 23:18:48 +0100878 if (pdata->mdix_ctrl != cmd->base.eth_tp_mdix_ctrl)
879 set_mdix_status(net, cmd->base.eth_tp_mdix_ctrl);
Woojung Huh13722bb2016-09-02 20:34:22 +0000880
Philippe Reyneseaf9a322017-03-16 23:18:48 +0100881 retval = usbnet_set_link_ksettings(net, cmd);
Woojung Huh13722bb2016-09-02 20:34:22 +0000882
883 return retval;
884}
885
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700886static const struct ethtool_ops smsc95xx_ethtool_ops = {
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000887 .get_link = usbnet_get_link,
888 .nway_reset = usbnet_nway_reset,
889 .get_drvinfo = usbnet_get_drvinfo,
890 .get_msglevel = usbnet_get_msglevel,
891 .set_msglevel = usbnet_set_msglevel,
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000892 .get_eeprom_len = smsc95xx_ethtool_get_eeprom_len,
893 .get_eeprom = smsc95xx_ethtool_get_eeprom,
894 .set_eeprom = smsc95xx_ethtool_set_eeprom,
Emeric Vigier9fa32e92012-07-09 17:44:45 -0400895 .get_regs_len = smsc95xx_ethtool_getregslen,
896 .get_regs = smsc95xx_ethtool_getregs,
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000897 .get_wol = smsc95xx_ethtool_get_wol,
898 .set_wol = smsc95xx_ethtool_set_wol,
Philippe Reyneseaf9a322017-03-16 23:18:48 +0100899 .get_link_ksettings = smsc95xx_get_link_ksettings,
900 .set_link_ksettings = smsc95xx_set_link_ksettings,
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000901};
902
903static int smsc95xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
904{
905 struct usbnet *dev = netdev_priv(netdev);
906
907 if (!netif_running(netdev))
908 return -EINVAL;
909
910 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
911}
912
913static void smsc95xx_init_mac_address(struct usbnet *dev)
914{
Arnd Bergmannc4895652016-04-29 09:05:59 +0200915 const u8 *mac_addr;
916
917 /* maybe the boot loader passed the MAC address in devicetree */
918 mac_addr = of_get_mac_address(dev->udev->dev.of_node);
919 if (mac_addr) {
920 memcpy(dev->net->dev_addr, mac_addr, ETH_ALEN);
921 return;
922 }
923
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000924 /* try reading mac address from EEPROM */
925 if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
926 dev->net->dev_addr) == 0) {
927 if (is_valid_ether_addr(dev->net->dev_addr)) {
928 /* eeprom values are valid so use them */
Joe Perchesa475f602010-02-17 10:30:24 +0000929 netif_dbg(dev, ifup, dev->net, "MAC address read from EEPROM\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000930 return;
931 }
932 }
933
Arnd Bergmannc4895652016-04-29 09:05:59 +0200934 /* no useful static MAC address found. generate a random one */
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000935 eth_hw_addr_random(dev->net);
Joe Perchesc7e12ea2012-07-12 19:33:07 +0000936 netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000937}
938
939static int smsc95xx_set_mac_address(struct usbnet *dev)
940{
941 u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
942 dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
943 u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
944 int ret;
945
946 ret = smsc95xx_write_reg(dev, ADDRL, addr_lo);
Steve Glendinningb052e072012-11-30 05:55:52 +0000947 if (ret < 0)
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000948 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000949
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000950 return smsc95xx_write_reg(dev, ADDRH, addr_hi);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000951}
952
953/* starts the TX path */
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000954static int smsc95xx_start_tx_path(struct usbnet *dev)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000955{
956 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
957 unsigned long flags;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000958 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000959
960 /* Enable Tx at MAC */
961 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
962 pdata->mac_cr |= MAC_CR_TXEN_;
963 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
964
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000965 ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000966 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000967 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000968
969 /* Enable Tx at SCSRs */
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000970 return smsc95xx_write_reg(dev, TX_CFG, TX_CFG_ON_);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000971}
972
973/* Starts the Receive path */
Ming Leiec321152012-11-06 04:53:07 +0000974static int smsc95xx_start_rx_path(struct usbnet *dev, int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000975{
976 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
977 unsigned long flags;
978
979 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
980 pdata->mac_cr |= MAC_CR_RXEN_;
981 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
982
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000983 return __smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr, in_pm);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000984}
985
986static int smsc95xx_phy_initialize(struct usbnet *dev)
987{
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000988 int bmcr, ret, timeout = 0;
Steve Glendinningdb443c42010-03-16 09:03:06 +0000989
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000990 /* Initialize MII structure */
991 dev->mii.dev = dev->net;
992 dev->mii.mdio_read = smsc95xx_mdio_read;
993 dev->mii.mdio_write = smsc95xx_mdio_write;
994 dev->mii.phy_id_mask = 0x1f;
995 dev->mii.reg_num_mask = 0x1f;
996 dev->mii.phy_id = SMSC95XX_INTERNAL_PHY_ID;
997
Steve Glendinningdb443c42010-03-16 09:03:06 +0000998 /* reset phy and wait for reset to complete */
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000999 smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
Steve Glendinningdb443c42010-03-16 09:03:06 +00001000
1001 do {
1002 msleep(10);
1003 bmcr = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
1004 timeout++;
Rabin Vincentd9460922011-04-30 08:29:27 +00001005 } while ((bmcr & BMCR_RESET) && (timeout < 100));
Steve Glendinningdb443c42010-03-16 09:03:06 +00001006
1007 if (timeout >= 100) {
1008 netdev_warn(dev->net, "timeout on PHY Reset");
1009 return -EIO;
1010 }
1011
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001012 smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
1013 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
1014 ADVERTISE_PAUSE_ASYM);
1015
1016 /* read to clear */
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001017 ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
Steve Glendinningb052e072012-11-30 05:55:52 +00001018 if (ret < 0) {
1019 netdev_warn(dev->net, "Failed to read PHY_INT_SRC during init\n");
1020 return ret;
1021 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001022
1023 smsc95xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
1024 PHY_INT_MASK_DEFAULT_);
1025 mii_nway_restart(&dev->mii);
1026
Joe Perchesa475f602010-02-17 10:30:24 +00001027 netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001028 return 0;
1029}
1030
1031static int smsc95xx_reset(struct usbnet *dev)
1032{
1033 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1034 u32 read_buf, write_buf, burst_cap;
1035 int ret = 0, timeout;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001036
Joe Perchesa475f602010-02-17 10:30:24 +00001037 netif_dbg(dev, ifup, dev->net, "entering smsc95xx_reset\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001038
Steve Glendinning44367612012-09-28 00:07:08 +00001039 ret = smsc95xx_write_reg(dev, HW_CFG, HW_CFG_LRST_);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001040 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001041 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001042
1043 timeout = 0;
1044 do {
Steve Glendinningcf2acec2012-09-28 00:07:07 +00001045 msleep(10);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001046 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001047 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001048 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001049 timeout++;
1050 } while ((read_buf & HW_CFG_LRST_) && (timeout < 100));
1051
1052 if (timeout >= 100) {
Joe Perches60b86752010-02-17 10:30:23 +00001053 netdev_warn(dev->net, "timeout waiting for completion of Lite Reset\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001054 return ret;
1055 }
1056
Steve Glendinning44367612012-09-28 00:07:08 +00001057 ret = smsc95xx_write_reg(dev, PM_CTRL, PM_CTL_PHY_RST_);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001058 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001059 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001060
1061 timeout = 0;
1062 do {
Steve Glendinningcf2acec2012-09-28 00:07:07 +00001063 msleep(10);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001064 ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001065 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001066 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001067 timeout++;
1068 } while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));
1069
1070 if (timeout >= 100) {
Joe Perches60b86752010-02-17 10:30:23 +00001071 netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001072 return ret;
1073 }
1074
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001075 ret = smsc95xx_set_mac_address(dev);
1076 if (ret < 0)
1077 return ret;
1078
Joe Perches1e1d7412012-11-24 01:27:49 +00001079 netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n",
1080 dev->net->dev_addr);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001081
1082 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001083 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001084 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001085
Joe Perches1e1d7412012-11-24 01:27:49 +00001086 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n",
1087 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001088
1089 read_buf |= HW_CFG_BIR_;
1090
1091 ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001092 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001093 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001094
1095 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001096 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001097 return ret;
Steve Glendinningb052e072012-11-30 05:55:52 +00001098
Joe Perchesa475f602010-02-17 10:30:24 +00001099 netif_dbg(dev, ifup, dev->net,
1100 "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
1101 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001102
1103 if (!turbo_mode) {
1104 burst_cap = 0;
1105 dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
1106 } else if (dev->udev->speed == USB_SPEED_HIGH) {
1107 burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
1108 dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
1109 } else {
1110 burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
1111 dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
1112 }
1113
Joe Perches1e1d7412012-11-24 01:27:49 +00001114 netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n",
1115 (ulong)dev->rx_urb_size);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001116
1117 ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
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 ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001122 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001123 return ret;
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001124
Joe Perchesa475f602010-02-17 10:30:24 +00001125 netif_dbg(dev, ifup, dev->net,
1126 "Read Value from BURST_CAP after writing: 0x%08x\n",
1127 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001128
Steve Glendinning44367612012-09-28 00:07:08 +00001129 ret = smsc95xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001130 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001131 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001132
1133 ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001134 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001135 return ret;
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001136
Joe Perchesa475f602010-02-17 10:30:24 +00001137 netif_dbg(dev, ifup, dev->net,
1138 "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
1139 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001140
1141 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001142 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001143 return ret;
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001144
Joe Perches1e1d7412012-11-24 01:27:49 +00001145 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG: 0x%08x\n",
1146 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001147
1148 if (turbo_mode)
1149 read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);
1150
1151 read_buf &= ~HW_CFG_RXDOFF_;
1152
1153 /* set Rx data offset=2, Make IP header aligns on word boundary. */
1154 read_buf |= NET_IP_ALIGN << 9;
1155
1156 ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001157 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001158 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001159
1160 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001161 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001162 return ret;
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001163
Joe Perchesa475f602010-02-17 10:30:24 +00001164 netif_dbg(dev, ifup, dev->net,
1165 "Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001166
Steve Glendinning44367612012-09-28 00:07:08 +00001167 ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001168 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001169 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001170
1171 ret = smsc95xx_read_reg(dev, ID_REV, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001172 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001173 return ret;
Joe Perchesa475f602010-02-17 10:30:24 +00001174 netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001175
Steve Glendinningf2935012009-05-01 05:46:51 +00001176 /* Configure GPIO pins as LED outputs */
1177 write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED |
1178 LED_GPIO_CFG_FDX_LED;
1179 ret = smsc95xx_write_reg(dev, LED_GPIO_CFG, write_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001180 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001181 return ret;
Steve Glendinningf2935012009-05-01 05:46:51 +00001182
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001183 /* Init Tx */
Steve Glendinning44367612012-09-28 00:07:08 +00001184 ret = smsc95xx_write_reg(dev, FLOW, 0);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001185 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001186 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001187
Steve Glendinning44367612012-09-28 00:07:08 +00001188 ret = smsc95xx_write_reg(dev, AFC_CFG, AFC_CFG_DEFAULT);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001189 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001190 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001191
1192 /* Don't need mac_cr_lock during initialisation */
1193 ret = smsc95xx_read_reg(dev, MAC_CR, &pdata->mac_cr);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001194 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001195 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001196
1197 /* Init Rx */
1198 /* Set Vlan */
Steve Glendinning44367612012-09-28 00:07:08 +00001199 ret = smsc95xx_write_reg(dev, VLAN1, (u32)ETH_P_8021Q);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001200 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001201 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001202
Steve Glendinningf7b29272008-11-20 04:19:21 -08001203 /* Enable or disable checksum offload engines */
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001204 ret = smsc95xx_set_features(dev->net, dev->net->features);
Steve Glendinningb052e072012-11-30 05:55:52 +00001205 if (ret < 0) {
1206 netdev_warn(dev->net, "Failed to set checksum offload features\n");
1207 return ret;
1208 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001209
1210 smsc95xx_set_multicast(dev->net);
1211
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001212 ret = smsc95xx_phy_initialize(dev);
Steve Glendinningb052e072012-11-30 05:55:52 +00001213 if (ret < 0) {
1214 netdev_warn(dev->net, "Failed to init PHY\n");
1215 return ret;
1216 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001217
1218 ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001219 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001220 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001221
1222 /* enable PHY interrupts */
1223 read_buf |= INT_EP_CTL_PHY_INT_;
1224
1225 ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001226 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001227 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001228
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001229 ret = smsc95xx_start_tx_path(dev);
Steve Glendinningb052e072012-11-30 05:55:52 +00001230 if (ret < 0) {
1231 netdev_warn(dev->net, "Failed to start TX path\n");
1232 return ret;
1233 }
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001234
Ming Leiec321152012-11-06 04:53:07 +00001235 ret = smsc95xx_start_rx_path(dev, 0);
Steve Glendinningb052e072012-11-30 05:55:52 +00001236 if (ret < 0) {
1237 netdev_warn(dev->net, "Failed to start RX path\n");
1238 return ret;
1239 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001240
Joe Perchesa475f602010-02-17 10:30:24 +00001241 netif_dbg(dev, ifup, dev->net, "smsc95xx_reset, return 0\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001242 return 0;
1243}
1244
Stephen Hemminger63e77b32009-03-20 19:35:58 +00001245static const struct net_device_ops smsc95xx_netdev_ops = {
1246 .ndo_open = usbnet_open,
1247 .ndo_stop = usbnet_stop,
1248 .ndo_start_xmit = usbnet_start_xmit,
1249 .ndo_tx_timeout = usbnet_tx_timeout,
1250 .ndo_change_mtu = usbnet_change_mtu,
Greg Ungererc8b5d122017-04-03 15:50:03 +10001251 .ndo_get_stats64 = usbnet_get_stats64,
Stephen Hemminger63e77b32009-03-20 19:35:58 +00001252 .ndo_set_mac_address = eth_mac_addr,
1253 .ndo_validate_addr = eth_validate_addr,
1254 .ndo_do_ioctl = smsc95xx_ioctl,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001255 .ndo_set_rx_mode = smsc95xx_set_multicast,
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001256 .ndo_set_features = smsc95xx_set_features,
Stephen Hemminger63e77b32009-03-20 19:35:58 +00001257};
1258
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001259static int smsc95xx_bind(struct usbnet *dev, struct usb_interface *intf)
1260{
1261 struct smsc95xx_priv *pdata = NULL;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001262 u32 val;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001263 int ret;
1264
1265 printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
1266
1267 ret = usbnet_get_endpoints(dev, intf);
Steve Glendinningb052e072012-11-30 05:55:52 +00001268 if (ret < 0) {
1269 netdev_warn(dev->net, "usbnet_get_endpoints failed: %d\n", ret);
1270 return ret;
1271 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001272
1273 dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc95xx_priv),
Joe Perches38673c82013-02-03 17:28:11 +00001274 GFP_KERNEL);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001275
1276 pdata = (struct smsc95xx_priv *)(dev->data[0]);
Joe Perches38673c82013-02-03 17:28:11 +00001277 if (!pdata)
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001278 return -ENOMEM;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001279
1280 spin_lock_init(&pdata->mac_cr_lock);
1281
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001282 if (DEFAULT_TX_CSUM_ENABLE)
1283 dev->net->features |= NETIF_F_HW_CSUM;
1284 if (DEFAULT_RX_CSUM_ENABLE)
1285 dev->net->features |= NETIF_F_RXCSUM;
1286
1287 dev->net->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001288
Bernard Blackhamf4e8ab72010-10-18 13:16:39 +00001289 smsc95xx_init_mac_address(dev);
1290
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001291 /* Init all registers */
1292 ret = smsc95xx_reset(dev);
1293
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001294 /* detect device revision as different features may be available */
1295 ret = smsc95xx_read_reg(dev, ID_REV, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001296 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001297 return ret;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001298 val >>= 16;
Woojung Huh13722bb2016-09-02 20:34:22 +00001299 pdata->chip_id = val;
1300 pdata->mdix_ctrl = get_mdix_status(dev->net);
Steve Glendinning9ebca502012-11-22 08:05:23 +00001301
1302 if ((val == ID_REV_CHIP_ID_9500A_) || (val == ID_REV_CHIP_ID_9530_) ||
1303 (val == ID_REV_CHIP_ID_89530_) || (val == ID_REV_CHIP_ID_9730_))
1304 pdata->features = (FEATURE_8_WAKEUP_FILTERS |
1305 FEATURE_PHY_NLP_CROSSOVER |
Ming Leieb970ff2013-02-22 03:05:05 +00001306 FEATURE_REMOTE_WAKEUP);
Steve Glendinning9ebca502012-11-22 08:05:23 +00001307 else if (val == ID_REV_CHIP_ID_9512_)
1308 pdata->features = FEATURE_8_WAKEUP_FILTERS;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001309
Stephen Hemminger63e77b32009-03-20 19:35:58 +00001310 dev->net->netdev_ops = &smsc95xx_netdev_ops;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001311 dev->net->ethtool_ops = &smsc95xx_ethtool_ops;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001312 dev->net->flags |= IFF_MULTICAST;
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001313 dev->net->hard_header_len += SMSC95XX_TX_OVERHEAD_CSUM;
Stephane Fillod9bbf5662012-04-20 09:39:23 +00001314 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
Christoph Fritzd69d1692016-05-26 04:06:47 +02001315
1316 pdata->dev = dev;
1317 INIT_DELAYED_WORK(&pdata->carrier_check, check_carrier);
1318 schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
1319
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001320 return 0;
1321}
1322
1323static void smsc95xx_unbind(struct usbnet *dev, struct usb_interface *intf)
1324{
1325 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
Christoph Fritzd69d1692016-05-26 04:06:47 +02001326
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001327 if (pdata) {
Christoph Fritzd69d1692016-05-26 04:06:47 +02001328 cancel_delayed_work(&pdata->carrier_check);
Joe Perchesa475f602010-02-17 10:30:24 +00001329 netif_dbg(dev, ifdown, dev->net, "free pdata\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001330 kfree(pdata);
1331 pdata = NULL;
1332 dev->data[0] = 0;
1333 }
1334}
1335
Steve Glendinning068bb1a2012-11-30 05:55:51 +00001336static u32 smsc_crc(const u8 *buffer, size_t len, int filter)
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001337{
Steve Glendinning068bb1a2012-11-30 05:55:51 +00001338 u32 crc = bitrev16(crc16(0xFFFF, buffer, len));
1339 return crc << ((filter % 2) * 16);
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001340}
1341
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001342static int smsc95xx_enable_phy_wakeup_interrupts(struct usbnet *dev, u16 mask)
1343{
1344 struct mii_if_info *mii = &dev->mii;
1345 int ret;
1346
Joe Perches1e1d7412012-11-24 01:27:49 +00001347 netdev_dbg(dev->net, "enabling PHY wakeup interrupts\n");
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001348
1349 /* read to clear */
1350 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_SRC);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001351 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001352 return ret;
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001353
1354 /* enable interrupt source */
1355 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_MASK);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001356 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001357 return ret;
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001358
1359 ret |= mask;
1360
1361 smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_INT_MASK, ret);
1362
1363 return 0;
1364}
1365
1366static int smsc95xx_link_ok_nopm(struct usbnet *dev)
1367{
1368 struct mii_if_info *mii = &dev->mii;
1369 int ret;
1370
1371 /* first, a dummy read, needed to latch some MII phys */
1372 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001373 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001374 return ret;
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001375
1376 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001377 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001378 return ret;
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001379
1380 return !!(ret & BMSR_LSTATUS);
1381}
1382
Steve Glendinning319b95b2012-11-22 08:05:25 +00001383static int smsc95xx_enter_suspend0(struct usbnet *dev)
1384{
1385 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1386 u32 val;
1387 int ret;
1388
1389 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001390 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001391 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001392
1393 val &= (~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_));
1394 val |= PM_CTL_SUS_MODE_0;
1395
1396 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001397 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001398 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001399
1400 /* clear wol status */
1401 val &= ~PM_CTL_WUPS_;
1402 val |= PM_CTL_WUPS_WOL_;
1403
1404 /* enable energy detection */
1405 if (pdata->wolopts & WAKE_PHY)
1406 val |= PM_CTL_WUPS_ED_;
1407
1408 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001409 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001410 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001411
1412 /* read back PM_CTRL */
1413 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Ming Lei76437212013-02-22 03:05:03 +00001414 if (ret < 0)
1415 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001416
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001417 pdata->suspend_flags |= SUSPEND_SUSPEND0;
1418
Ming Lei76437212013-02-22 03:05:03 +00001419 return 0;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001420}
1421
1422static int smsc95xx_enter_suspend1(struct usbnet *dev)
1423{
1424 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1425 struct mii_if_info *mii = &dev->mii;
1426 u32 val;
1427 int ret;
1428
1429 /* reconfigure link pulse detection timing for
1430 * compatibility with non-standard link partners
1431 */
1432 if (pdata->features & FEATURE_PHY_NLP_CROSSOVER)
1433 smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_EDPD_CONFIG,
1434 PHY_EDPD_CONFIG_DEFAULT);
1435
1436 /* enable energy detect power-down mode */
1437 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001438 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001439 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001440
1441 ret |= MODE_CTRL_STS_EDPWRDOWN_;
1442
1443 smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS, ret);
1444
1445 /* enter SUSPEND1 mode */
1446 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001447 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001448 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001449
1450 val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1451 val |= PM_CTL_SUS_MODE_1;
1452
1453 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001454 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001455 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001456
1457 /* clear wol status, enable energy detection */
1458 val &= ~PM_CTL_WUPS_;
1459 val |= (PM_CTL_WUPS_ED_ | PM_CTL_ED_EN_);
1460
1461 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Ming Lei76437212013-02-22 03:05:03 +00001462 if (ret < 0)
1463 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001464
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001465 pdata->suspend_flags |= SUSPEND_SUSPEND1;
1466
Ming Lei76437212013-02-22 03:05:03 +00001467 return 0;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001468}
1469
1470static int smsc95xx_enter_suspend2(struct usbnet *dev)
1471{
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001472 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
Steve Glendinning319b95b2012-11-22 08:05:25 +00001473 u32 val;
1474 int ret;
1475
1476 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001477 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001478 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001479
1480 val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1481 val |= PM_CTL_SUS_MODE_2;
1482
1483 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Ming Lei76437212013-02-22 03:05:03 +00001484 if (ret < 0)
1485 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001486
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001487 pdata->suspend_flags |= SUSPEND_SUSPEND2;
1488
Ming Lei76437212013-02-22 03:05:03 +00001489 return 0;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001490}
1491
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001492static int smsc95xx_enter_suspend3(struct usbnet *dev)
1493{
1494 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1495 u32 val;
1496 int ret;
1497
1498 ret = smsc95xx_read_reg_nopm(dev, RX_FIFO_INF, &val);
1499 if (ret < 0)
1500 return ret;
1501
1502 if (val & 0xFFFF) {
1503 netdev_info(dev->net, "rx fifo not empty in autosuspend\n");
1504 return -EBUSY;
1505 }
1506
1507 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1508 if (ret < 0)
1509 return ret;
1510
1511 val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1512 val |= PM_CTL_SUS_MODE_3 | PM_CTL_RES_CLR_WKP_STS;
1513
1514 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1515 if (ret < 0)
1516 return ret;
1517
1518 /* clear wol status */
1519 val &= ~PM_CTL_WUPS_;
1520 val |= PM_CTL_WUPS_WOL_;
1521
1522 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1523 if (ret < 0)
1524 return ret;
1525
1526 pdata->suspend_flags |= SUSPEND_SUSPEND3;
1527
1528 return 0;
1529}
1530
1531static int smsc95xx_autosuspend(struct usbnet *dev, u32 link_up)
1532{
1533 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1534 int ret;
1535
1536 if (!netif_running(dev->net)) {
1537 /* interface is ifconfig down so fully power down hw */
1538 netdev_dbg(dev->net, "autosuspend entering SUSPEND2\n");
1539 return smsc95xx_enter_suspend2(dev);
1540 }
1541
1542 if (!link_up) {
1543 /* link is down so enter EDPD mode, but only if device can
1544 * reliably resume from it. This check should be redundant
Ming Leieb970ff2013-02-22 03:05:05 +00001545 * as current FEATURE_REMOTE_WAKEUP parts also support
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001546 * FEATURE_PHY_NLP_CROSSOVER but it's included for clarity */
1547 if (!(pdata->features & FEATURE_PHY_NLP_CROSSOVER)) {
1548 netdev_warn(dev->net, "EDPD not supported\n");
1549 return -EBUSY;
1550 }
1551
1552 netdev_dbg(dev->net, "autosuspend entering SUSPEND1\n");
1553
1554 /* enable PHY wakeup events for if cable is attached */
1555 ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1556 PHY_INT_MASK_ANEG_COMP_);
1557 if (ret < 0) {
1558 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1559 return ret;
1560 }
1561
1562 netdev_info(dev->net, "entering SUSPEND1 mode\n");
1563 return smsc95xx_enter_suspend1(dev);
1564 }
1565
1566 /* enable PHY wakeup events so we remote wakeup if cable is pulled */
1567 ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1568 PHY_INT_MASK_LINK_DOWN_);
1569 if (ret < 0) {
1570 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1571 return ret;
1572 }
1573
1574 netdev_dbg(dev->net, "autosuspend entering SUSPEND3\n");
1575 return smsc95xx_enter_suspend3(dev);
1576}
1577
Steve Glendinningb5a04472012-09-28 00:07:11 +00001578static int smsc95xx_suspend(struct usb_interface *intf, pm_message_t message)
1579{
1580 struct usbnet *dev = usb_get_intfdata(intf);
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001581 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001582 u32 val, link_up;
Steve Glendinningb5a04472012-09-28 00:07:11 +00001583 int ret;
Steve Glendinningb5a04472012-09-28 00:07:11 +00001584
Steve Glendinningb5a04472012-09-28 00:07:11 +00001585 ret = usbnet_suspend(intf, message);
Steve Glendinningb052e072012-11-30 05:55:52 +00001586 if (ret < 0) {
1587 netdev_warn(dev->net, "usbnet_suspend error\n");
1588 return ret;
1589 }
Steve Glendinningb5a04472012-09-28 00:07:11 +00001590
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001591 if (pdata->suspend_flags) {
1592 netdev_warn(dev->net, "error during last resume\n");
1593 pdata->suspend_flags = 0;
1594 }
1595
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001596 /* determine if link is up using only _nopm functions */
1597 link_up = smsc95xx_link_ok_nopm(dev);
1598
Ming Lei42e21c02013-02-22 03:05:04 +00001599 if (message.event == PM_EVENT_AUTO_SUSPEND &&
Ming Leieb970ff2013-02-22 03:05:05 +00001600 (pdata->features & FEATURE_REMOTE_WAKEUP)) {
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001601 ret = smsc95xx_autosuspend(dev, link_up);
1602 goto done;
1603 }
1604
1605 /* if we get this far we're not autosuspending */
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001606 /* if no wol options set, or if link is down and we're not waking on
1607 * PHY activity, enter lowest power SUSPEND2 mode
1608 */
1609 if (!(pdata->wolopts & SUPPORTED_WAKE) ||
1610 !(link_up || (pdata->wolopts & WAKE_PHY))) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001611 netdev_info(dev->net, "entering SUSPEND2 mode\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001612
1613 /* disable energy detect (link up) & wake up events */
Ming Leiec321152012-11-06 04:53:07 +00001614 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001615 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001616 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001617
1618 val &= ~(WUCSR_MPEN_ | WUCSR_WAKE_EN_);
1619
Ming Leiec321152012-11-06 04:53:07 +00001620 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001621 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001622 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001623
Ming Leiec321152012-11-06 04:53:07 +00001624 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001625 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001626 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001627
1628 val &= ~(PM_CTL_ED_EN_ | PM_CTL_WOL_EN_);
1629
Ming Leiec321152012-11-06 04:53:07 +00001630 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001631 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001632 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001633
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001634 ret = smsc95xx_enter_suspend2(dev);
1635 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001636 }
1637
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001638 if (pdata->wolopts & WAKE_PHY) {
1639 ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1640 (PHY_INT_MASK_ANEG_COMP_ | PHY_INT_MASK_LINK_DOWN_));
Steve Glendinningb052e072012-11-30 05:55:52 +00001641 if (ret < 0) {
1642 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1643 goto done;
1644 }
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001645
1646 /* if link is down then configure EDPD and enter SUSPEND1,
1647 * otherwise enter SUSPEND0 below
1648 */
1649 if (!link_up) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001650 netdev_info(dev->net, "entering SUSPEND1 mode\n");
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001651 ret = smsc95xx_enter_suspend1(dev);
1652 goto done;
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001653 }
1654 }
1655
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001656 if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
Steve Glendinningeed9a722012-11-30 05:55:48 +00001657 u32 *filter_mask = kzalloc(sizeof(u32) * 32, GFP_KERNEL);
Ming Lei06a221b2012-11-06 04:53:06 +00001658 u32 command[2];
1659 u32 offset[2];
1660 u32 crc[4];
Steve Glendinning9ebca502012-11-22 08:05:23 +00001661 int wuff_filter_count =
1662 (pdata->features & FEATURE_8_WAKEUP_FILTERS) ?
1663 LAN9500A_WUFF_NUM : LAN9500_WUFF_NUM;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001664 int i, filter = 0;
1665
Steve Glendinningeed9a722012-11-30 05:55:48 +00001666 if (!filter_mask) {
1667 netdev_warn(dev->net, "Unable to allocate filter_mask\n");
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001668 ret = -ENOMEM;
1669 goto done;
Steve Glendinningeed9a722012-11-30 05:55:48 +00001670 }
1671
Ming Lei06a221b2012-11-06 04:53:06 +00001672 memset(command, 0, sizeof(command));
1673 memset(offset, 0, sizeof(offset));
1674 memset(crc, 0, sizeof(crc));
1675
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001676 if (pdata->wolopts & WAKE_BCAST) {
1677 const u8 bcast[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Joe Perches1e1d7412012-11-24 01:27:49 +00001678 netdev_info(dev->net, "enabling broadcast detection\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001679 filter_mask[filter * 4] = 0x003F;
1680 filter_mask[filter * 4 + 1] = 0x00;
1681 filter_mask[filter * 4 + 2] = 0x00;
1682 filter_mask[filter * 4 + 3] = 0x00;
1683 command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1684 offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1685 crc[filter/2] |= smsc_crc(bcast, 6, filter);
1686 filter++;
1687 }
1688
1689 if (pdata->wolopts & WAKE_MCAST) {
1690 const u8 mcast[] = {0x01, 0x00, 0x5E};
Joe Perches1e1d7412012-11-24 01:27:49 +00001691 netdev_info(dev->net, "enabling multicast detection\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001692 filter_mask[filter * 4] = 0x0007;
1693 filter_mask[filter * 4 + 1] = 0x00;
1694 filter_mask[filter * 4 + 2] = 0x00;
1695 filter_mask[filter * 4 + 3] = 0x00;
1696 command[filter/4] |= 0x09UL << ((filter % 4) * 8);
1697 offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1698 crc[filter/2] |= smsc_crc(mcast, 3, filter);
1699 filter++;
1700 }
1701
1702 if (pdata->wolopts & WAKE_ARP) {
1703 const u8 arp[] = {0x08, 0x06};
Joe Perches1e1d7412012-11-24 01:27:49 +00001704 netdev_info(dev->net, "enabling ARP detection\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001705 filter_mask[filter * 4] = 0x0003;
1706 filter_mask[filter * 4 + 1] = 0x00;
1707 filter_mask[filter * 4 + 2] = 0x00;
1708 filter_mask[filter * 4 + 3] = 0x00;
1709 command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1710 offset[filter/4] |= 0x0C << ((filter % 4) * 8);
1711 crc[filter/2] |= smsc_crc(arp, 2, filter);
1712 filter++;
1713 }
1714
1715 if (pdata->wolopts & WAKE_UCAST) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001716 netdev_info(dev->net, "enabling unicast detection\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001717 filter_mask[filter * 4] = 0x003F;
1718 filter_mask[filter * 4 + 1] = 0x00;
1719 filter_mask[filter * 4 + 2] = 0x00;
1720 filter_mask[filter * 4 + 3] = 0x00;
1721 command[filter/4] |= 0x01UL << ((filter % 4) * 8);
1722 offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1723 crc[filter/2] |= smsc_crc(dev->net->dev_addr, ETH_ALEN, filter);
1724 filter++;
1725 }
1726
Steve Glendinning9ebca502012-11-22 08:05:23 +00001727 for (i = 0; i < (wuff_filter_count * 4); i++) {
Ming Leiec321152012-11-06 04:53:07 +00001728 ret = smsc95xx_write_reg_nopm(dev, WUFF, filter_mask[i]);
Steve Glendinningb052e072012-11-30 05:55:52 +00001729 if (ret < 0) {
Ming Lei06a221b2012-11-06 04:53:06 +00001730 kfree(filter_mask);
Steve Glendinningb052e072012-11-30 05:55:52 +00001731 goto done;
1732 }
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001733 }
Ming Lei06a221b2012-11-06 04:53:06 +00001734 kfree(filter_mask);
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001735
Steve Glendinning9ebca502012-11-22 08:05:23 +00001736 for (i = 0; i < (wuff_filter_count / 4); i++) {
Ming Leiec321152012-11-06 04:53:07 +00001737 ret = smsc95xx_write_reg_nopm(dev, WUFF, command[i]);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001738 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001739 goto done;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001740 }
1741
Steve Glendinning9ebca502012-11-22 08:05:23 +00001742 for (i = 0; i < (wuff_filter_count / 4); i++) {
Ming Leiec321152012-11-06 04:53:07 +00001743 ret = smsc95xx_write_reg_nopm(dev, WUFF, offset[i]);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001744 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001745 goto done;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001746 }
1747
Steve Glendinning9ebca502012-11-22 08:05:23 +00001748 for (i = 0; i < (wuff_filter_count / 2); i++) {
Ming Leiec321152012-11-06 04:53:07 +00001749 ret = smsc95xx_write_reg_nopm(dev, WUFF, crc[i]);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001750 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001751 goto done;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001752 }
1753
1754 /* clear any pending pattern match packet status */
Ming Leiec321152012-11-06 04:53:07 +00001755 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001756 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001757 goto done;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001758
1759 val |= WUCSR_WUFR_;
1760
Ming Leiec321152012-11-06 04:53:07 +00001761 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001762 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001763 goto done;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001764 }
1765
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001766 if (pdata->wolopts & WAKE_MAGIC) {
1767 /* clear any pending magic packet status */
Ming Leiec321152012-11-06 04:53:07 +00001768 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001769 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001770 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001771
1772 val |= WUCSR_MPR_;
1773
Ming Leiec321152012-11-06 04:53:07 +00001774 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001775 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001776 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001777 }
1778
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001779 /* enable/disable wakeup sources */
Ming Leiec321152012-11-06 04:53:07 +00001780 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001781 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001782 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001783
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001784 if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001785 netdev_info(dev->net, "enabling pattern match wakeup\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001786 val |= WUCSR_WAKE_EN_;
1787 } else {
Joe Perches1e1d7412012-11-24 01:27:49 +00001788 netdev_info(dev->net, "disabling pattern match wakeup\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001789 val &= ~WUCSR_WAKE_EN_;
1790 }
1791
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001792 if (pdata->wolopts & WAKE_MAGIC) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001793 netdev_info(dev->net, "enabling magic packet wakeup\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001794 val |= WUCSR_MPEN_;
1795 } else {
Joe Perches1e1d7412012-11-24 01:27:49 +00001796 netdev_info(dev->net, "disabling magic packet wakeup\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001797 val &= ~WUCSR_MPEN_;
1798 }
1799
Ming Leiec321152012-11-06 04:53:07 +00001800 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001801 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001802 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001803
1804 /* enable wol wakeup source */
Ming Leiec321152012-11-06 04:53:07 +00001805 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001806 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001807 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001808
1809 val |= PM_CTL_WOL_EN_;
1810
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001811 /* phy energy detect wakeup source */
1812 if (pdata->wolopts & WAKE_PHY)
1813 val |= PM_CTL_ED_EN_;
1814
Ming Leiec321152012-11-06 04:53:07 +00001815 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001816 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001817 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001818
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001819 /* enable receiver to enable frame reception */
Ming Leiec321152012-11-06 04:53:07 +00001820 smsc95xx_start_rx_path(dev, 1);
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001821
1822 /* some wol options are enabled, so enter SUSPEND0 */
Joe Perches1e1d7412012-11-24 01:27:49 +00001823 netdev_info(dev->net, "entering SUSPEND0 mode\n");
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001824 ret = smsc95xx_enter_suspend0(dev);
1825
1826done:
Ming Lei0d41be52013-03-15 12:08:58 +08001827 /*
1828 * TODO: resume() might need to handle the suspend failure
1829 * in system sleep
1830 */
1831 if (ret && PMSG_IS_AUTO(message))
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001832 usbnet_resume(intf);
1833 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001834}
1835
1836static int smsc95xx_resume(struct usb_interface *intf)
1837{
1838 struct usbnet *dev = usb_get_intfdata(intf);
Sudip Mukherjee8bca81d2014-11-11 14:10:47 +05301839 struct smsc95xx_priv *pdata;
1840 u8 suspend_flags;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001841 int ret;
1842 u32 val;
1843
1844 BUG_ON(!dev);
Sudip Mukherjee8bca81d2014-11-11 14:10:47 +05301845 pdata = (struct smsc95xx_priv *)(dev->data[0]);
1846 suspend_flags = pdata->suspend_flags;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001847
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001848 netdev_dbg(dev->net, "resume suspend_flags=0x%02x\n", suspend_flags);
1849
1850 /* do this first to ensure it's cleared even in error case */
1851 pdata->suspend_flags = 0;
Christoph Fritzd69d1692016-05-26 04:06:47 +02001852 schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001853
1854 if (suspend_flags & SUSPEND_ALLMODES) {
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001855 /* clear wake-up sources */
Ming Leiec321152012-11-06 04:53:07 +00001856 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001857 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001858 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001859
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001860 val &= ~(WUCSR_WAKE_EN_ | WUCSR_MPEN_);
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001861
Ming Leiec321152012-11-06 04:53:07 +00001862 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001863 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001864 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001865
1866 /* clear wake-up status */
Ming Leiec321152012-11-06 04:53:07 +00001867 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001868 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001869 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001870
1871 val &= ~PM_CTL_WOL_EN_;
1872 val |= PM_CTL_WUPS_;
1873
Ming Leiec321152012-11-06 04:53:07 +00001874 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001875 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001876 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001877 }
1878
Steve Glendinningaf3d7c12012-11-22 08:05:22 +00001879 ret = usbnet_resume(intf);
Steve Glendinningb052e072012-11-30 05:55:52 +00001880 if (ret < 0)
1881 netdev_warn(dev->net, "usbnet_resume error\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001882
Steve Glendinningb052e072012-11-30 05:55:52 +00001883 return ret;
Steve Glendinningb5a04472012-09-28 00:07:11 +00001884}
1885
Joonyoung Shimb4df4802014-07-10 11:49:42 +09001886static int smsc95xx_reset_resume(struct usb_interface *intf)
1887{
1888 struct usbnet *dev = usb_get_intfdata(intf);
1889 int ret;
1890
1891 ret = smsc95xx_reset(dev);
1892 if (ret < 0)
1893 return ret;
1894
1895 return smsc95xx_resume(intf);
1896}
1897
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001898static void smsc95xx_rx_csum_offload(struct sk_buff *skb)
1899{
1900 skb->csum = *(u16 *)(skb_tail_pointer(skb) - 2);
1901 skb->ip_summed = CHECKSUM_COMPLETE;
1902 skb_trim(skb, skb->len - 2);
1903}
1904
1905static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
1906{
Emil Goodeeb855692014-02-13 17:50:19 +01001907 /* This check is no longer done by usbnet */
1908 if (skb->len < dev->net->hard_header_len)
1909 return 0;
1910
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001911 while (skb->len > 0) {
1912 u32 header, align_count;
1913 struct sk_buff *ax_skb;
1914 unsigned char *packet;
1915 u16 size;
1916
1917 memcpy(&header, skb->data, sizeof(header));
1918 le32_to_cpus(&header);
1919 skb_pull(skb, 4 + NET_IP_ALIGN);
1920 packet = skb->data;
1921
1922 /* get the packet length */
1923 size = (u16)((header & RX_STS_FL_) >> 16);
1924 align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4;
1925
1926 if (unlikely(header & RX_STS_ES_)) {
Joe Perchesa475f602010-02-17 10:30:24 +00001927 netif_dbg(dev, rx_err, dev->net,
1928 "Error header=0x%08x\n", header);
Herbert Xu80667ac2009-06-29 16:53:00 +00001929 dev->net->stats.rx_errors++;
1930 dev->net->stats.rx_dropped++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001931
1932 if (header & RX_STS_CRC_) {
Herbert Xu80667ac2009-06-29 16:53:00 +00001933 dev->net->stats.rx_crc_errors++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001934 } else {
1935 if (header & (RX_STS_TL_ | RX_STS_RF_))
Herbert Xu80667ac2009-06-29 16:53:00 +00001936 dev->net->stats.rx_frame_errors++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001937
1938 if ((header & RX_STS_LE_) &&
1939 (!(header & RX_STS_FT_)))
Herbert Xu80667ac2009-06-29 16:53:00 +00001940 dev->net->stats.rx_length_errors++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001941 }
1942 } else {
1943 /* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
1944 if (unlikely(size > (ETH_FRAME_LEN + 12))) {
Joe Perchesa475f602010-02-17 10:30:24 +00001945 netif_dbg(dev, rx_err, dev->net,
1946 "size err header=0x%08x\n", header);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001947 return 0;
1948 }
1949
1950 /* last frame in this batch */
1951 if (skb->len == size) {
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001952 if (dev->net->features & NETIF_F_RXCSUM)
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001953 smsc95xx_rx_csum_offload(skb);
Peter Korsgaarddf18acc2009-05-13 10:10:41 +00001954 skb_trim(skb, skb->len - 4); /* remove fcs */
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001955 skb->truesize = size + sizeof(struct sk_buff);
1956
1957 return 1;
1958 }
1959
1960 ax_skb = skb_clone(skb, GFP_ATOMIC);
1961 if (unlikely(!ax_skb)) {
Joe Perches60b86752010-02-17 10:30:23 +00001962 netdev_warn(dev->net, "Error allocating skb\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001963 return 0;
1964 }
1965
1966 ax_skb->len = size;
1967 ax_skb->data = packet;
1968 skb_set_tail_pointer(ax_skb, size);
1969
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001970 if (dev->net->features & NETIF_F_RXCSUM)
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001971 smsc95xx_rx_csum_offload(ax_skb);
Peter Korsgaarddf18acc2009-05-13 10:10:41 +00001972 skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001973 ax_skb->truesize = size + sizeof(struct sk_buff);
1974
1975 usbnet_skb_return(dev, ax_skb);
1976 }
1977
1978 skb_pull(skb, size);
1979
1980 /* padding bytes before the next frame starts */
1981 if (skb->len)
1982 skb_pull(skb, align_count);
1983 }
1984
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001985 return 1;
1986}
1987
Steve Glendinningf7b29272008-11-20 04:19:21 -08001988static u32 smsc95xx_calc_csum_preamble(struct sk_buff *skb)
1989{
Michał Mirosław55508d62010-12-14 15:24:08 +00001990 u16 low_16 = (u16)skb_checksum_start_offset(skb);
1991 u16 high_16 = low_16 + skb->csum_offset;
Steve Glendinningf7b29272008-11-20 04:19:21 -08001992 return (high_16 << 16) | low_16;
1993}
1994
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001995static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,
1996 struct sk_buff *skb, gfp_t flags)
1997{
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001998 bool csum = skb->ip_summed == CHECKSUM_PARTIAL;
Steve Glendinningf7b29272008-11-20 04:19:21 -08001999 int overhead = csum ? SMSC95XX_TX_OVERHEAD_CSUM : SMSC95XX_TX_OVERHEAD;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002000 u32 tx_cmd_a, tx_cmd_b;
2001
Steve Glendinningf7b29272008-11-20 04:19:21 -08002002 /* We do not advertise SG, so skbs should be already linearized */
2003 BUG_ON(skb_shinfo(skb)->nr_frags);
2004
2005 if (skb_headroom(skb) < overhead) {
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002006 struct sk_buff *skb2 = skb_copy_expand(skb,
Steve Glendinningf7b29272008-11-20 04:19:21 -08002007 overhead, 0, flags);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002008 dev_kfree_skb_any(skb);
2009 skb = skb2;
2010 if (!skb)
2011 return NULL;
2012 }
2013
Steve Glendinningf7b29272008-11-20 04:19:21 -08002014 if (csum) {
Steve Glendinning11bc3082010-03-18 22:18:41 -07002015 if (skb->len <= 45) {
2016 /* workaround - hardware tx checksum does not work
2017 * properly with extremely small packets */
Michał Mirosław55508d62010-12-14 15:24:08 +00002018 long csstart = skb_checksum_start_offset(skb);
Steve Glendinning11bc3082010-03-18 22:18:41 -07002019 __wsum calc = csum_partial(skb->data + csstart,
2020 skb->len - csstart, 0);
2021 *((__sum16 *)(skb->data + csstart
2022 + skb->csum_offset)) = csum_fold(calc);
2023
2024 csum = false;
2025 } else {
2026 u32 csum_preamble = smsc95xx_calc_csum_preamble(skb);
2027 skb_push(skb, 4);
Steve Glendinning00acda62012-11-02 00:44:20 +00002028 cpu_to_le32s(&csum_preamble);
Steve Glendinning11bc3082010-03-18 22:18:41 -07002029 memcpy(skb->data, &csum_preamble, 4);
2030 }
Steve Glendinningf7b29272008-11-20 04:19:21 -08002031 }
2032
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002033 skb_push(skb, 4);
2034 tx_cmd_b = (u32)(skb->len - 4);
Steve Glendinningf7b29272008-11-20 04:19:21 -08002035 if (csum)
2036 tx_cmd_b |= TX_CMD_B_CSUM_ENABLE;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002037 cpu_to_le32s(&tx_cmd_b);
2038 memcpy(skb->data, &tx_cmd_b, 4);
2039
2040 skb_push(skb, 4);
2041 tx_cmd_a = (u32)(skb->len - 8) | TX_CMD_A_FIRST_SEG_ |
2042 TX_CMD_A_LAST_SEG_;
2043 cpu_to_le32s(&tx_cmd_a);
2044 memcpy(skb->data, &tx_cmd_a, 4);
2045
2046 return skb;
2047}
2048
Steve Glendinningb2d4b152013-01-03 03:00:16 +00002049static int smsc95xx_manage_power(struct usbnet *dev, int on)
2050{
2051 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
2052
2053 dev->intf->needs_remote_wakeup = on;
2054
Ming Leieb970ff2013-02-22 03:05:05 +00002055 if (pdata->features & FEATURE_REMOTE_WAKEUP)
Steve Glendinningb2d4b152013-01-03 03:00:16 +00002056 return 0;
2057
Ming Leieb970ff2013-02-22 03:05:05 +00002058 /* this chip revision isn't capable of remote wakeup */
2059 netdev_info(dev->net, "hardware isn't capable of remote wakeup\n");
Steve Glendinningb2d4b152013-01-03 03:00:16 +00002060
2061 if (on)
2062 usb_autopm_get_interface_no_resume(dev->intf);
2063 else
2064 usb_autopm_put_interface(dev->intf);
2065
2066 return 0;
2067}
2068
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002069static const struct driver_info smsc95xx_info = {
2070 .description = "smsc95xx USB 2.0 Ethernet",
2071 .bind = smsc95xx_bind,
2072 .unbind = smsc95xx_unbind,
2073 .link_reset = smsc95xx_link_reset,
2074 .reset = smsc95xx_reset,
2075 .rx_fixup = smsc95xx_rx_fixup,
2076 .tx_fixup = smsc95xx_tx_fixup,
2077 .status = smsc95xx_status,
Steve Glendinningb2d4b152013-01-03 03:00:16 +00002078 .manage_power = smsc95xx_manage_power,
Paolo Pisati07d69d42012-04-23 04:05:20 +00002079 .flags = FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002080};
2081
2082static const struct usb_device_id products[] = {
2083 {
2084 /* SMSC9500 USB Ethernet Device */
2085 USB_DEVICE(0x0424, 0x9500),
2086 .driver_info = (unsigned long) &smsc95xx_info,
2087 },
Steve Glendinning726474b2009-05-01 06:07:22 +00002088 {
Steve Glendinning6f41d122009-09-22 05:13:02 +00002089 /* SMSC9505 USB Ethernet Device */
2090 USB_DEVICE(0x0424, 0x9505),
2091 .driver_info = (unsigned long) &smsc95xx_info,
2092 },
2093 {
2094 /* SMSC9500A USB Ethernet Device */
2095 USB_DEVICE(0x0424, 0x9E00),
2096 .driver_info = (unsigned long) &smsc95xx_info,
2097 },
2098 {
2099 /* SMSC9505A USB Ethernet Device */
2100 USB_DEVICE(0x0424, 0x9E01),
2101 .driver_info = (unsigned long) &smsc95xx_info,
2102 },
2103 {
Steve Glendinning726474b2009-05-01 06:07:22 +00002104 /* SMSC9512/9514 USB Hub & Ethernet Device */
2105 USB_DEVICE(0x0424, 0xec00),
2106 .driver_info = (unsigned long) &smsc95xx_info,
2107 },
Steve Glendinning6f41d122009-09-22 05:13:02 +00002108 {
2109 /* SMSC9500 USB Ethernet Device (SAL10) */
2110 USB_DEVICE(0x0424, 0x9900),
2111 .driver_info = (unsigned long) &smsc95xx_info,
2112 },
2113 {
2114 /* SMSC9505 USB Ethernet Device (SAL10) */
2115 USB_DEVICE(0x0424, 0x9901),
2116 .driver_info = (unsigned long) &smsc95xx_info,
2117 },
2118 {
2119 /* SMSC9500A USB Ethernet Device (SAL10) */
2120 USB_DEVICE(0x0424, 0x9902),
2121 .driver_info = (unsigned long) &smsc95xx_info,
2122 },
2123 {
2124 /* SMSC9505A USB Ethernet Device (SAL10) */
2125 USB_DEVICE(0x0424, 0x9903),
2126 .driver_info = (unsigned long) &smsc95xx_info,
2127 },
2128 {
2129 /* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */
2130 USB_DEVICE(0x0424, 0x9904),
2131 .driver_info = (unsigned long) &smsc95xx_info,
2132 },
2133 {
2134 /* SMSC9500A USB Ethernet Device (HAL) */
2135 USB_DEVICE(0x0424, 0x9905),
2136 .driver_info = (unsigned long) &smsc95xx_info,
2137 },
2138 {
2139 /* SMSC9505A USB Ethernet Device (HAL) */
2140 USB_DEVICE(0x0424, 0x9906),
2141 .driver_info = (unsigned long) &smsc95xx_info,
2142 },
2143 {
2144 /* SMSC9500 USB Ethernet Device (Alternate ID) */
2145 USB_DEVICE(0x0424, 0x9907),
2146 .driver_info = (unsigned long) &smsc95xx_info,
2147 },
2148 {
2149 /* SMSC9500A USB Ethernet Device (Alternate ID) */
2150 USB_DEVICE(0x0424, 0x9908),
2151 .driver_info = (unsigned long) &smsc95xx_info,
2152 },
2153 {
2154 /* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */
2155 USB_DEVICE(0x0424, 0x9909),
2156 .driver_info = (unsigned long) &smsc95xx_info,
2157 },
Steve Glendinning88edaa42011-04-10 18:59:27 -07002158 {
2159 /* SMSC LAN9530 USB Ethernet Device */
2160 USB_DEVICE(0x0424, 0x9530),
2161 .driver_info = (unsigned long) &smsc95xx_info,
2162 },
2163 {
2164 /* SMSC LAN9730 USB Ethernet Device */
2165 USB_DEVICE(0x0424, 0x9730),
2166 .driver_info = (unsigned long) &smsc95xx_info,
2167 },
2168 {
2169 /* SMSC LAN89530 USB Ethernet Device */
2170 USB_DEVICE(0x0424, 0x9E08),
2171 .driver_info = (unsigned long) &smsc95xx_info,
2172 },
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002173 { }, /* END */
2174};
2175MODULE_DEVICE_TABLE(usb, products);
2176
2177static struct usb_driver smsc95xx_driver = {
2178 .name = "smsc95xx",
2179 .id_table = products,
2180 .probe = usbnet_probe,
Steve Glendinningb5a04472012-09-28 00:07:11 +00002181 .suspend = smsc95xx_suspend,
Steve Glendinninge0e474a2012-09-28 00:07:12 +00002182 .resume = smsc95xx_resume,
Joonyoung Shimb4df4802014-07-10 11:49:42 +09002183 .reset_resume = smsc95xx_reset_resume,
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002184 .disconnect = usbnet_disconnect,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07002185 .disable_hub_initiated_lpm = 1,
Steve Glendinningb2d4b152013-01-03 03:00:16 +00002186 .supports_autosuspend = 1,
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002187};
2188
Greg Kroah-Hartmand632eb12011-11-18 09:44:20 -08002189module_usb_driver(smsc95xx_driver);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002190
2191MODULE_AUTHOR("Nancy Lin");
Steve Glendinning90b24cf2012-04-16 12:13:29 +01002192MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002193MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices");
2194MODULE_LICENSE("GPL");