blob: 2d17f3b9bb16568c19275864b325e700c4ff5e88 [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"
Martin Wetterwald53a759c2017-04-13 10:08:44 +020036#define SMSC_DRIVER_VERSION "1.0.6"
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{
Nisar Sayed9c082732017-09-11 17:43:11 +0000529 u32 flow = 0, afc_cfg;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000530
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;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000540
Nisar Sayed9c082732017-09-11 17:43:11 +0000541 if (cap & FLOW_CTRL_TX) {
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000542 afc_cfg |= 0xF;
Nisar Sayed9c082732017-09-11 17:43:11 +0000543 flow |= 0xFFFF0000;
544 } else {
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000545 afc_cfg &= ~0xF;
Nisar Sayed9c082732017-09-11 17:43:11 +0000546 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000547
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 afc_cfg |= 0xF;
554 }
555
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000556 ret = smsc95xx_write_reg(dev, FLOW, flow);
Steve Glendinningb052e072012-11-30 05:55:52 +0000557 if (ret < 0)
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000558 return ret;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000559
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000560 return smsc95xx_write_reg(dev, AFC_CFG, afc_cfg);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000561}
562
563static int smsc95xx_link_reset(struct usbnet *dev)
564{
565 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
566 struct mii_if_info *mii = &dev->mii;
David Decotigny8ae6daca2011-04-27 18:32:38 +0000567 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000568 unsigned long flags;
569 u16 lcladv, rmtadv;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000570 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000571
572 /* clear interrupt status */
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000573 ret = smsc95xx_mdio_read(dev->net, mii->phy_id, PHY_INT_SRC);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000574 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000575 return ret;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000576
577 ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000578 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000579 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000580
581 mii_check_media(mii, 1, 1);
582 mii_ethtool_gset(&dev->mii, &ecmd);
583 lcladv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
584 rmtadv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_LPA);
585
David Decotigny8ae6daca2011-04-27 18:32:38 +0000586 netif_dbg(dev, link, dev->net,
587 "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
588 ethtool_cmd_speed(&ecmd), ecmd.duplex, lcladv, rmtadv);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000589
590 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
591 if (ecmd.duplex != DUPLEX_FULL) {
592 pdata->mac_cr &= ~MAC_CR_FDPX_;
593 pdata->mac_cr |= MAC_CR_RCVOWN_;
594 } else {
595 pdata->mac_cr &= ~MAC_CR_RCVOWN_;
596 pdata->mac_cr |= MAC_CR_FDPX_;
597 }
598 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
599
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000600 ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000601 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000602 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000603
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000604 ret = smsc95xx_phy_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
Steve Glendinningb052e072012-11-30 05:55:52 +0000605 if (ret < 0)
606 netdev_warn(dev->net, "Error updating PHY flow control\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000607
Steve Glendinningb052e072012-11-30 05:55:52 +0000608 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000609}
610
611static void smsc95xx_status(struct usbnet *dev, struct urb *urb)
612{
613 u32 intdata;
614
615 if (urb->actual_length != 4) {
Joe Perches60b86752010-02-17 10:30:23 +0000616 netdev_warn(dev->net, "unexpected urb length %d\n",
617 urb->actual_length);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000618 return;
619 }
620
621 memcpy(&intdata, urb->transfer_buffer, 4);
Steve Glendinning1d74a6b2008-10-09 14:34:47 -0700622 le32_to_cpus(&intdata);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000623
Joe Perchesa475f602010-02-17 10:30:24 +0000624 netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000625
626 if (intdata & INT_ENP_PHY_INT_)
627 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
628 else
Joe Perches60b86752010-02-17 10:30:23 +0000629 netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
630 intdata);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000631}
632
Christoph Fritzd69d1692016-05-26 04:06:47 +0200633static void set_carrier(struct usbnet *dev, bool link)
634{
635 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
636
637 if (pdata->link_ok == link)
638 return;
639
640 pdata->link_ok = link;
641
642 if (link)
643 usbnet_link_change(dev, 1, 0);
644 else
645 usbnet_link_change(dev, 0, 0);
646}
647
648static void check_carrier(struct work_struct *work)
649{
650 struct smsc95xx_priv *pdata = container_of(work, struct smsc95xx_priv,
651 carrier_check.work);
652 struct usbnet *dev = pdata->dev;
653 int ret;
654
655 if (pdata->suspend_flags != 0)
656 return;
657
658 ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMSR);
659 if (ret < 0) {
660 netdev_warn(dev->net, "Failed to read MII_BMSR\n");
661 return;
662 }
663 if (ret & BMSR_LSTATUS)
664 set_carrier(dev, 1);
665 else
666 set_carrier(dev, 0);
667
668 schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
669}
670
Steve Glendinningf7b29272008-11-20 04:19:21 -0800671/* Enable or disable Tx & Rx checksum offload engines */
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000672static int smsc95xx_set_features(struct net_device *netdev,
673 netdev_features_t features)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000674{
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700675 struct usbnet *dev = netdev_priv(netdev);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000676 u32 read_buf;
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700677 int ret;
678
679 ret = smsc95xx_read_reg(dev, COE_CR, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000680 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000681 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000682
Nisar Sayedfe0cd8c2017-05-19 14:00:25 +0000683 if (features & NETIF_F_IP_CSUM)
Steve Glendinningf7b29272008-11-20 04:19:21 -0800684 read_buf |= Tx_COE_EN_;
685 else
686 read_buf &= ~Tx_COE_EN_;
687
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700688 if (features & NETIF_F_RXCSUM)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000689 read_buf |= Rx_COE_EN_;
690 else
691 read_buf &= ~Rx_COE_EN_;
692
693 ret = smsc95xx_write_reg(dev, COE_CR, read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000694 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000695 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000696
Joe Perchesa475f602010-02-17 10:30:24 +0000697 netif_dbg(dev, hw, dev->net, "COE_CR = 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000698 return 0;
699}
700
701static int smsc95xx_ethtool_get_eeprom_len(struct net_device *net)
702{
703 return MAX_EEPROM_SIZE;
704}
705
706static int smsc95xx_ethtool_get_eeprom(struct net_device *netdev,
707 struct ethtool_eeprom *ee, u8 *data)
708{
709 struct usbnet *dev = netdev_priv(netdev);
710
711 ee->magic = LAN95XX_EEPROM_MAGIC;
712
713 return smsc95xx_read_eeprom(dev, ee->offset, ee->len, data);
714}
715
716static int smsc95xx_ethtool_set_eeprom(struct net_device *netdev,
717 struct ethtool_eeprom *ee, u8 *data)
718{
719 struct usbnet *dev = netdev_priv(netdev);
720
721 if (ee->magic != LAN95XX_EEPROM_MAGIC) {
Joe Perches60b86752010-02-17 10:30:23 +0000722 netdev_warn(dev->net, "EEPROM: magic value mismatch, magic = 0x%x\n",
723 ee->magic);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000724 return -EINVAL;
725 }
726
727 return smsc95xx_write_eeprom(dev, ee->offset, ee->len, data);
728}
729
Emeric Vigier9fa32e92012-07-09 17:44:45 -0400730static int smsc95xx_ethtool_getregslen(struct net_device *netdev)
731{
732 /* all smsc95xx registers */
Steve Glendinning96245312012-12-10 01:03:07 +0000733 return COE_CR - ID_REV + sizeof(u32);
Emeric Vigier9fa32e92012-07-09 17:44:45 -0400734}
735
736static void
737smsc95xx_ethtool_getregs(struct net_device *netdev, struct ethtool_regs *regs,
738 void *buf)
739{
740 struct usbnet *dev = netdev_priv(netdev);
Dan Carpenterd3484462012-07-10 20:32:51 +0000741 unsigned int i, j;
742 int retval;
Emeric Vigier9fa32e92012-07-09 17:44:45 -0400743 u32 *data = buf;
744
745 retval = smsc95xx_read_reg(dev, ID_REV, &regs->version);
746 if (retval < 0) {
747 netdev_warn(netdev, "REGS: cannot read ID_REV\n");
748 return;
749 }
750
751 for (i = ID_REV, j = 0; i <= COE_CR; i += (sizeof(u32)), j++) {
752 retval = smsc95xx_read_reg(dev, i, &data[j]);
753 if (retval < 0) {
754 netdev_warn(netdev, "REGS: cannot read reg[%x]\n", i);
755 return;
756 }
757 }
758}
759
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000760static void smsc95xx_ethtool_get_wol(struct net_device *net,
761 struct ethtool_wolinfo *wolinfo)
762{
763 struct usbnet *dev = netdev_priv(net);
764 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
765
766 wolinfo->supported = SUPPORTED_WAKE;
767 wolinfo->wolopts = pdata->wolopts;
768}
769
770static int smsc95xx_ethtool_set_wol(struct net_device *net,
771 struct ethtool_wolinfo *wolinfo)
772{
773 struct usbnet *dev = netdev_priv(net);
774 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
Steve Glendinning3b146922012-11-30 05:55:50 +0000775 int ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000776
Florian Fainellic530c472018-09-28 16:18:56 -0700777 if (wolinfo->wolopts & ~SUPPORTED_WAKE)
778 return -EINVAL;
779
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000780 pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
Steve Glendinning3b146922012-11-30 05:55:50 +0000781
782 ret = device_set_wakeup_enable(&dev->udev->dev, pdata->wolopts);
Steve Glendinningb052e072012-11-30 05:55:52 +0000783 if (ret < 0)
784 netdev_warn(dev->net, "device_set_wakeup_enable error %d\n", ret);
Steve Glendinning3b146922012-11-30 05:55:50 +0000785
Steve Glendinningb052e072012-11-30 05:55:52 +0000786 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000787}
788
Woojung Huh13722bb2016-09-02 20:34:22 +0000789static int get_mdix_status(struct net_device *net)
790{
791 struct usbnet *dev = netdev_priv(net);
792 u32 val;
793 int buf;
794
795 buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, SPECIAL_CTRL_STS);
796 if (buf & SPECIAL_CTRL_STS_OVRRD_AMDIX_) {
797 if (buf & SPECIAL_CTRL_STS_AMDIX_ENABLE_)
798 return ETH_TP_MDI_AUTO;
799 else if (buf & SPECIAL_CTRL_STS_AMDIX_STATE_)
800 return ETH_TP_MDI_X;
801 } else {
802 buf = smsc95xx_read_reg(dev, STRAP_STATUS, &val);
803 if (val & STRAP_STATUS_AMDIX_EN_)
804 return ETH_TP_MDI_AUTO;
805 }
806
807 return ETH_TP_MDI;
808}
809
810static void set_mdix_status(struct net_device *net, __u8 mdix_ctrl)
811{
812 struct usbnet *dev = netdev_priv(net);
813 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
814 int buf;
815
816 if ((pdata->chip_id == ID_REV_CHIP_ID_9500A_) ||
817 (pdata->chip_id == ID_REV_CHIP_ID_9530_) ||
818 (pdata->chip_id == ID_REV_CHIP_ID_89530_) ||
819 (pdata->chip_id == ID_REV_CHIP_ID_9730_)) {
820 /* Extend Manual AutoMDIX timer for 9500A/9500Ai */
821 buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
822 PHY_EDPD_CONFIG);
823 buf |= PHY_EDPD_CONFIG_EXT_CROSSOVER_;
824 smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
825 PHY_EDPD_CONFIG, buf);
826 }
827
828 if (mdix_ctrl == ETH_TP_MDI) {
829 buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
830 SPECIAL_CTRL_STS);
831 buf |= SPECIAL_CTRL_STS_OVRRD_AMDIX_;
832 buf &= ~(SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
833 SPECIAL_CTRL_STS_AMDIX_STATE_);
834 smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
835 SPECIAL_CTRL_STS, buf);
836 } else if (mdix_ctrl == ETH_TP_MDI_X) {
837 buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
838 SPECIAL_CTRL_STS);
839 buf |= SPECIAL_CTRL_STS_OVRRD_AMDIX_;
840 buf &= ~(SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
841 SPECIAL_CTRL_STS_AMDIX_STATE_);
842 buf |= SPECIAL_CTRL_STS_AMDIX_STATE_;
843 smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
844 SPECIAL_CTRL_STS, buf);
845 } else if (mdix_ctrl == ETH_TP_MDI_AUTO) {
846 buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
847 SPECIAL_CTRL_STS);
848 buf &= ~SPECIAL_CTRL_STS_OVRRD_AMDIX_;
849 buf &= ~(SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
850 SPECIAL_CTRL_STS_AMDIX_STATE_);
851 buf |= SPECIAL_CTRL_STS_AMDIX_ENABLE_;
852 smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
853 SPECIAL_CTRL_STS, buf);
854 }
855 pdata->mdix_ctrl = mdix_ctrl;
856}
857
Philippe Reyneseaf9a322017-03-16 23:18:48 +0100858static int smsc95xx_get_link_ksettings(struct net_device *net,
859 struct ethtool_link_ksettings *cmd)
Woojung Huh13722bb2016-09-02 20:34:22 +0000860{
861 struct usbnet *dev = netdev_priv(net);
862 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
863 int retval;
864
Philippe Reyneseaf9a322017-03-16 23:18:48 +0100865 retval = usbnet_get_link_ksettings(net, cmd);
Woojung Huh13722bb2016-09-02 20:34:22 +0000866
Philippe Reyneseaf9a322017-03-16 23:18:48 +0100867 cmd->base.eth_tp_mdix = pdata->mdix_ctrl;
868 cmd->base.eth_tp_mdix_ctrl = pdata->mdix_ctrl;
Woojung Huh13722bb2016-09-02 20:34:22 +0000869
870 return retval;
871}
872
Philippe Reyneseaf9a322017-03-16 23:18:48 +0100873static int smsc95xx_set_link_ksettings(struct net_device *net,
874 const struct ethtool_link_ksettings *cmd)
Woojung Huh13722bb2016-09-02 20:34:22 +0000875{
876 struct usbnet *dev = netdev_priv(net);
877 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
878 int retval;
879
Philippe Reyneseaf9a322017-03-16 23:18:48 +0100880 if (pdata->mdix_ctrl != cmd->base.eth_tp_mdix_ctrl)
881 set_mdix_status(net, cmd->base.eth_tp_mdix_ctrl);
Woojung Huh13722bb2016-09-02 20:34:22 +0000882
Philippe Reyneseaf9a322017-03-16 23:18:48 +0100883 retval = usbnet_set_link_ksettings(net, cmd);
Woojung Huh13722bb2016-09-02 20:34:22 +0000884
885 return retval;
886}
887
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700888static const struct ethtool_ops smsc95xx_ethtool_ops = {
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000889 .get_link = usbnet_get_link,
890 .nway_reset = usbnet_nway_reset,
891 .get_drvinfo = usbnet_get_drvinfo,
892 .get_msglevel = usbnet_get_msglevel,
893 .set_msglevel = usbnet_set_msglevel,
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000894 .get_eeprom_len = smsc95xx_ethtool_get_eeprom_len,
895 .get_eeprom = smsc95xx_ethtool_get_eeprom,
896 .set_eeprom = smsc95xx_ethtool_set_eeprom,
Emeric Vigier9fa32e92012-07-09 17:44:45 -0400897 .get_regs_len = smsc95xx_ethtool_getregslen,
898 .get_regs = smsc95xx_ethtool_getregs,
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000899 .get_wol = smsc95xx_ethtool_get_wol,
900 .set_wol = smsc95xx_ethtool_set_wol,
Philippe Reyneseaf9a322017-03-16 23:18:48 +0100901 .get_link_ksettings = smsc95xx_get_link_ksettings,
902 .set_link_ksettings = smsc95xx_set_link_ksettings,
Petr Kulhavya8f5cb92017-07-13 19:40:57 +0200903 .get_ts_info = ethtool_op_get_ts_info,
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000904};
905
906static int smsc95xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
907{
908 struct usbnet *dev = netdev_priv(netdev);
909
910 if (!netif_running(netdev))
911 return -EINVAL;
912
913 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
914}
915
916static void smsc95xx_init_mac_address(struct usbnet *dev)
917{
Arnd Bergmannc4895652016-04-29 09:05:59 +0200918 const u8 *mac_addr;
919
920 /* maybe the boot loader passed the MAC address in devicetree */
921 mac_addr = of_get_mac_address(dev->udev->dev.of_node);
922 if (mac_addr) {
923 memcpy(dev->net->dev_addr, mac_addr, ETH_ALEN);
924 return;
925 }
926
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000927 /* try reading mac address from EEPROM */
928 if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
929 dev->net->dev_addr) == 0) {
930 if (is_valid_ether_addr(dev->net->dev_addr)) {
931 /* eeprom values are valid so use them */
Joe Perchesa475f602010-02-17 10:30:24 +0000932 netif_dbg(dev, ifup, dev->net, "MAC address read from EEPROM\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000933 return;
934 }
935 }
936
Arnd Bergmannc4895652016-04-29 09:05:59 +0200937 /* no useful static MAC address found. generate a random one */
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000938 eth_hw_addr_random(dev->net);
Joe Perchesc7e12ea2012-07-12 19:33:07 +0000939 netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000940}
941
942static int smsc95xx_set_mac_address(struct usbnet *dev)
943{
944 u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
945 dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
946 u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
947 int ret;
948
949 ret = smsc95xx_write_reg(dev, ADDRL, addr_lo);
Steve Glendinningb052e072012-11-30 05:55:52 +0000950 if (ret < 0)
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000951 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000952
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000953 return smsc95xx_write_reg(dev, ADDRH, addr_hi);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000954}
955
956/* starts the TX path */
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000957static int smsc95xx_start_tx_path(struct usbnet *dev)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000958{
959 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
960 unsigned long flags;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000961 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000962
963 /* Enable Tx at MAC */
964 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
965 pdata->mac_cr |= MAC_CR_TXEN_;
966 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
967
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000968 ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000969 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000970 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000971
972 /* Enable Tx at SCSRs */
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000973 return smsc95xx_write_reg(dev, TX_CFG, TX_CFG_ON_);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000974}
975
976/* Starts the Receive path */
Ming Leiec321152012-11-06 04:53:07 +0000977static int smsc95xx_start_rx_path(struct usbnet *dev, int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000978{
979 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
980 unsigned long flags;
981
982 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
983 pdata->mac_cr |= MAC_CR_RXEN_;
984 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
985
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000986 return __smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr, in_pm);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000987}
988
989static int smsc95xx_phy_initialize(struct usbnet *dev)
990{
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000991 int bmcr, ret, timeout = 0;
Steve Glendinningdb443c42010-03-16 09:03:06 +0000992
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000993 /* Initialize MII structure */
994 dev->mii.dev = dev->net;
995 dev->mii.mdio_read = smsc95xx_mdio_read;
996 dev->mii.mdio_write = smsc95xx_mdio_write;
997 dev->mii.phy_id_mask = 0x1f;
998 dev->mii.reg_num_mask = 0x1f;
999 dev->mii.phy_id = SMSC95XX_INTERNAL_PHY_ID;
1000
Steve Glendinningdb443c42010-03-16 09:03:06 +00001001 /* reset phy and wait for reset to complete */
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001002 smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
Steve Glendinningdb443c42010-03-16 09:03:06 +00001003
1004 do {
1005 msleep(10);
1006 bmcr = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
1007 timeout++;
Rabin Vincentd9460922011-04-30 08:29:27 +00001008 } while ((bmcr & BMCR_RESET) && (timeout < 100));
Steve Glendinningdb443c42010-03-16 09:03:06 +00001009
1010 if (timeout >= 100) {
1011 netdev_warn(dev->net, "timeout on PHY Reset");
1012 return -EIO;
1013 }
1014
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001015 smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
1016 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
1017 ADVERTISE_PAUSE_ASYM);
1018
1019 /* read to clear */
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001020 ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
Steve Glendinningb052e072012-11-30 05:55:52 +00001021 if (ret < 0) {
1022 netdev_warn(dev->net, "Failed to read PHY_INT_SRC during init\n");
1023 return ret;
1024 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001025
1026 smsc95xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
1027 PHY_INT_MASK_DEFAULT_);
1028 mii_nway_restart(&dev->mii);
1029
Joe Perchesa475f602010-02-17 10:30:24 +00001030 netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001031 return 0;
1032}
1033
1034static int smsc95xx_reset(struct usbnet *dev)
1035{
1036 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1037 u32 read_buf, write_buf, burst_cap;
1038 int ret = 0, timeout;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001039
Joe Perchesa475f602010-02-17 10:30:24 +00001040 netif_dbg(dev, ifup, dev->net, "entering smsc95xx_reset\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001041
Steve Glendinning44367612012-09-28 00:07:08 +00001042 ret = smsc95xx_write_reg(dev, HW_CFG, HW_CFG_LRST_);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001043 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001044 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001045
1046 timeout = 0;
1047 do {
Steve Glendinningcf2acec2012-09-28 00:07:07 +00001048 msleep(10);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001049 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001050 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001051 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001052 timeout++;
1053 } while ((read_buf & HW_CFG_LRST_) && (timeout < 100));
1054
1055 if (timeout >= 100) {
Joe Perches60b86752010-02-17 10:30:23 +00001056 netdev_warn(dev->net, "timeout waiting for completion of Lite Reset\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001057 return ret;
1058 }
1059
Steve Glendinning44367612012-09-28 00:07:08 +00001060 ret = smsc95xx_write_reg(dev, PM_CTRL, PM_CTL_PHY_RST_);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001061 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001062 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001063
1064 timeout = 0;
1065 do {
Steve Glendinningcf2acec2012-09-28 00:07:07 +00001066 msleep(10);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001067 ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001068 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001069 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001070 timeout++;
1071 } while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));
1072
1073 if (timeout >= 100) {
Joe Perches60b86752010-02-17 10:30:23 +00001074 netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001075 return ret;
1076 }
1077
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001078 ret = smsc95xx_set_mac_address(dev);
1079 if (ret < 0)
1080 return ret;
1081
Joe Perches1e1d7412012-11-24 01:27:49 +00001082 netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n",
1083 dev->net->dev_addr);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001084
1085 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001086 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001087 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001088
Joe Perches1e1d7412012-11-24 01:27:49 +00001089 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n",
1090 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001091
1092 read_buf |= HW_CFG_BIR_;
1093
1094 ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001095 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001096 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001097
1098 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001099 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001100 return ret;
Steve Glendinningb052e072012-11-30 05:55:52 +00001101
Joe Perchesa475f602010-02-17 10:30:24 +00001102 netif_dbg(dev, ifup, dev->net,
1103 "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
1104 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001105
1106 if (!turbo_mode) {
1107 burst_cap = 0;
1108 dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
1109 } else if (dev->udev->speed == USB_SPEED_HIGH) {
1110 burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
1111 dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
1112 } else {
1113 burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
1114 dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
1115 }
1116
Joe Perches1e1d7412012-11-24 01:27:49 +00001117 netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n",
1118 (ulong)dev->rx_urb_size);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001119
1120 ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001121 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001122 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001123
1124 ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001125 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001126 return ret;
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001127
Joe Perchesa475f602010-02-17 10:30:24 +00001128 netif_dbg(dev, ifup, dev->net,
1129 "Read Value from BURST_CAP after writing: 0x%08x\n",
1130 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001131
Steve Glendinning44367612012-09-28 00:07:08 +00001132 ret = smsc95xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001133 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001134 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001135
1136 ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001137 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001138 return ret;
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001139
Joe Perchesa475f602010-02-17 10:30:24 +00001140 netif_dbg(dev, ifup, dev->net,
1141 "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
1142 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001143
1144 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001145 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001146 return ret;
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001147
Joe Perches1e1d7412012-11-24 01:27:49 +00001148 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG: 0x%08x\n",
1149 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001150
1151 if (turbo_mode)
1152 read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);
1153
1154 read_buf &= ~HW_CFG_RXDOFF_;
1155
1156 /* set Rx data offset=2, Make IP header aligns on word boundary. */
1157 read_buf |= NET_IP_ALIGN << 9;
1158
1159 ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001160 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001161 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001162
1163 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001164 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001165 return ret;
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001166
Joe Perchesa475f602010-02-17 10:30:24 +00001167 netif_dbg(dev, ifup, dev->net,
1168 "Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001169
Steve Glendinning44367612012-09-28 00:07:08 +00001170 ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001171 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001172 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001173
1174 ret = smsc95xx_read_reg(dev, ID_REV, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001175 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001176 return ret;
Joe Perchesa475f602010-02-17 10:30:24 +00001177 netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001178
Steve Glendinningf2935012009-05-01 05:46:51 +00001179 /* Configure GPIO pins as LED outputs */
1180 write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED |
1181 LED_GPIO_CFG_FDX_LED;
1182 ret = smsc95xx_write_reg(dev, LED_GPIO_CFG, write_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001183 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001184 return ret;
Steve Glendinningf2935012009-05-01 05:46:51 +00001185
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001186 /* Init Tx */
Steve Glendinning44367612012-09-28 00:07:08 +00001187 ret = smsc95xx_write_reg(dev, FLOW, 0);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001188 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001189 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001190
Steve Glendinning44367612012-09-28 00:07:08 +00001191 ret = smsc95xx_write_reg(dev, AFC_CFG, AFC_CFG_DEFAULT);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001192 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001193 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001194
1195 /* Don't need mac_cr_lock during initialisation */
1196 ret = smsc95xx_read_reg(dev, MAC_CR, &pdata->mac_cr);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001197 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001198 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001199
1200 /* Init Rx */
1201 /* Set Vlan */
Steve Glendinning44367612012-09-28 00:07:08 +00001202 ret = smsc95xx_write_reg(dev, VLAN1, (u32)ETH_P_8021Q);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001203 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001204 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001205
Steve Glendinningf7b29272008-11-20 04:19:21 -08001206 /* Enable or disable checksum offload engines */
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001207 ret = smsc95xx_set_features(dev->net, dev->net->features);
Steve Glendinningb052e072012-11-30 05:55:52 +00001208 if (ret < 0) {
1209 netdev_warn(dev->net, "Failed to set checksum offload features\n");
1210 return ret;
1211 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001212
1213 smsc95xx_set_multicast(dev->net);
1214
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001215 ret = smsc95xx_phy_initialize(dev);
Steve Glendinningb052e072012-11-30 05:55:52 +00001216 if (ret < 0) {
1217 netdev_warn(dev->net, "Failed to init PHY\n");
1218 return ret;
1219 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001220
1221 ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001222 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001223 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001224
1225 /* enable PHY interrupts */
1226 read_buf |= INT_EP_CTL_PHY_INT_;
1227
1228 ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001229 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001230 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001231
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001232 ret = smsc95xx_start_tx_path(dev);
Steve Glendinningb052e072012-11-30 05:55:52 +00001233 if (ret < 0) {
1234 netdev_warn(dev->net, "Failed to start TX path\n");
1235 return ret;
1236 }
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001237
Ming Leiec321152012-11-06 04:53:07 +00001238 ret = smsc95xx_start_rx_path(dev, 0);
Steve Glendinningb052e072012-11-30 05:55:52 +00001239 if (ret < 0) {
1240 netdev_warn(dev->net, "Failed to start RX path\n");
1241 return ret;
1242 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001243
Joe Perchesa475f602010-02-17 10:30:24 +00001244 netif_dbg(dev, ifup, dev->net, "smsc95xx_reset, return 0\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001245 return 0;
1246}
1247
Stephen Hemminger63e77b32009-03-20 19:35:58 +00001248static const struct net_device_ops smsc95xx_netdev_ops = {
1249 .ndo_open = usbnet_open,
1250 .ndo_stop = usbnet_stop,
1251 .ndo_start_xmit = usbnet_start_xmit,
1252 .ndo_tx_timeout = usbnet_tx_timeout,
1253 .ndo_change_mtu = usbnet_change_mtu,
Greg Ungererc8b5d122017-04-03 15:50:03 +10001254 .ndo_get_stats64 = usbnet_get_stats64,
Stephen Hemminger63e77b32009-03-20 19:35:58 +00001255 .ndo_set_mac_address = eth_mac_addr,
1256 .ndo_validate_addr = eth_validate_addr,
1257 .ndo_do_ioctl = smsc95xx_ioctl,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001258 .ndo_set_rx_mode = smsc95xx_set_multicast,
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001259 .ndo_set_features = smsc95xx_set_features,
Stephen Hemminger63e77b32009-03-20 19:35:58 +00001260};
1261
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001262static int smsc95xx_bind(struct usbnet *dev, struct usb_interface *intf)
1263{
1264 struct smsc95xx_priv *pdata = NULL;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001265 u32 val;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001266 int ret;
1267
1268 printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
1269
1270 ret = usbnet_get_endpoints(dev, intf);
Steve Glendinningb052e072012-11-30 05:55:52 +00001271 if (ret < 0) {
1272 netdev_warn(dev->net, "usbnet_get_endpoints failed: %d\n", ret);
1273 return ret;
1274 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001275
1276 dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc95xx_priv),
Joe Perches38673c82013-02-03 17:28:11 +00001277 GFP_KERNEL);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001278
1279 pdata = (struct smsc95xx_priv *)(dev->data[0]);
Joe Perches38673c82013-02-03 17:28:11 +00001280 if (!pdata)
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001281 return -ENOMEM;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001282
1283 spin_lock_init(&pdata->mac_cr_lock);
1284
Nisar Sayedfe0cd8c2017-05-19 14:00:25 +00001285 /* LAN95xx devices do not alter the computed checksum of 0 to 0xffff.
1286 * RFC 2460, ipv6 UDP calculated checksum yields a result of zero must
1287 * be changed to 0xffff. RFC 768, ipv4 UDP computed checksum is zero,
1288 * it is transmitted as all ones. The zero transmitted checksum means
1289 * transmitter generated no checksum. Hence, enable csum offload only
1290 * for ipv4 packets.
1291 */
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001292 if (DEFAULT_TX_CSUM_ENABLE)
Nisar Sayedfe0cd8c2017-05-19 14:00:25 +00001293 dev->net->features |= NETIF_F_IP_CSUM;
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001294 if (DEFAULT_RX_CSUM_ENABLE)
1295 dev->net->features |= NETIF_F_RXCSUM;
1296
Nisar Sayedfe0cd8c2017-05-19 14:00:25 +00001297 dev->net->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001298
Bernard Blackhamf4e8ab72010-10-18 13:16:39 +00001299 smsc95xx_init_mac_address(dev);
1300
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001301 /* Init all registers */
1302 ret = smsc95xx_reset(dev);
1303
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001304 /* detect device revision as different features may be available */
1305 ret = smsc95xx_read_reg(dev, ID_REV, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001306 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001307 return ret;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001308 val >>= 16;
Woojung Huh13722bb2016-09-02 20:34:22 +00001309 pdata->chip_id = val;
1310 pdata->mdix_ctrl = get_mdix_status(dev->net);
Steve Glendinning9ebca502012-11-22 08:05:23 +00001311
1312 if ((val == ID_REV_CHIP_ID_9500A_) || (val == ID_REV_CHIP_ID_9530_) ||
1313 (val == ID_REV_CHIP_ID_89530_) || (val == ID_REV_CHIP_ID_9730_))
1314 pdata->features = (FEATURE_8_WAKEUP_FILTERS |
1315 FEATURE_PHY_NLP_CROSSOVER |
Ming Leieb970ff2013-02-22 03:05:05 +00001316 FEATURE_REMOTE_WAKEUP);
Steve Glendinning9ebca502012-11-22 08:05:23 +00001317 else if (val == ID_REV_CHIP_ID_9512_)
1318 pdata->features = FEATURE_8_WAKEUP_FILTERS;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001319
Stephen Hemminger63e77b32009-03-20 19:35:58 +00001320 dev->net->netdev_ops = &smsc95xx_netdev_ops;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001321 dev->net->ethtool_ops = &smsc95xx_ethtool_ops;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001322 dev->net->flags |= IFF_MULTICAST;
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001323 dev->net->hard_header_len += SMSC95XX_TX_OVERHEAD_CSUM;
Stephane Fillod9bbf5662012-04-20 09:39:23 +00001324 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
Christoph Fritzd69d1692016-05-26 04:06:47 +02001325
1326 pdata->dev = dev;
1327 INIT_DELAYED_WORK(&pdata->carrier_check, check_carrier);
1328 schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
1329
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001330 return 0;
1331}
1332
1333static void smsc95xx_unbind(struct usbnet *dev, struct usb_interface *intf)
1334{
1335 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
Christoph Fritzd69d1692016-05-26 04:06:47 +02001336
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001337 if (pdata) {
Christoph Fritzd69d1692016-05-26 04:06:47 +02001338 cancel_delayed_work(&pdata->carrier_check);
Joe Perchesa475f602010-02-17 10:30:24 +00001339 netif_dbg(dev, ifdown, dev->net, "free pdata\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001340 kfree(pdata);
1341 pdata = NULL;
1342 dev->data[0] = 0;
1343 }
1344}
1345
Steve Glendinning068bb1a2012-11-30 05:55:51 +00001346static u32 smsc_crc(const u8 *buffer, size_t len, int filter)
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001347{
Steve Glendinning068bb1a2012-11-30 05:55:51 +00001348 u32 crc = bitrev16(crc16(0xFFFF, buffer, len));
1349 return crc << ((filter % 2) * 16);
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001350}
1351
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001352static int smsc95xx_enable_phy_wakeup_interrupts(struct usbnet *dev, u16 mask)
1353{
1354 struct mii_if_info *mii = &dev->mii;
1355 int ret;
1356
Joe Perches1e1d7412012-11-24 01:27:49 +00001357 netdev_dbg(dev->net, "enabling PHY wakeup interrupts\n");
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001358
1359 /* read to clear */
1360 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_SRC);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001361 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001362 return ret;
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001363
1364 /* enable interrupt source */
1365 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_MASK);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001366 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001367 return ret;
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001368
1369 ret |= mask;
1370
1371 smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_INT_MASK, ret);
1372
1373 return 0;
1374}
1375
1376static int smsc95xx_link_ok_nopm(struct usbnet *dev)
1377{
1378 struct mii_if_info *mii = &dev->mii;
1379 int ret;
1380
1381 /* first, a dummy read, needed to latch some MII phys */
1382 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001383 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001384 return ret;
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001385
1386 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001387 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001388 return ret;
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001389
1390 return !!(ret & BMSR_LSTATUS);
1391}
1392
Steve Glendinning319b95b2012-11-22 08:05:25 +00001393static int smsc95xx_enter_suspend0(struct usbnet *dev)
1394{
1395 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1396 u32 val;
1397 int ret;
1398
1399 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001400 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001401 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001402
1403 val &= (~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_));
1404 val |= PM_CTL_SUS_MODE_0;
1405
1406 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001407 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001408 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001409
1410 /* clear wol status */
1411 val &= ~PM_CTL_WUPS_;
1412 val |= PM_CTL_WUPS_WOL_;
1413
1414 /* enable energy detection */
1415 if (pdata->wolopts & WAKE_PHY)
1416 val |= PM_CTL_WUPS_ED_;
1417
1418 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001419 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001420 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001421
1422 /* read back PM_CTRL */
1423 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Ming Lei76437212013-02-22 03:05:03 +00001424 if (ret < 0)
1425 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001426
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001427 pdata->suspend_flags |= SUSPEND_SUSPEND0;
1428
Ming Lei76437212013-02-22 03:05:03 +00001429 return 0;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001430}
1431
1432static int smsc95xx_enter_suspend1(struct usbnet *dev)
1433{
1434 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1435 struct mii_if_info *mii = &dev->mii;
1436 u32 val;
1437 int ret;
1438
1439 /* reconfigure link pulse detection timing for
1440 * compatibility with non-standard link partners
1441 */
1442 if (pdata->features & FEATURE_PHY_NLP_CROSSOVER)
1443 smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_EDPD_CONFIG,
1444 PHY_EDPD_CONFIG_DEFAULT);
1445
1446 /* enable energy detect power-down mode */
1447 ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001448 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001449 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001450
1451 ret |= MODE_CTRL_STS_EDPWRDOWN_;
1452
1453 smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS, ret);
1454
1455 /* enter SUSPEND1 mode */
1456 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001457 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001458 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001459
1460 val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1461 val |= PM_CTL_SUS_MODE_1;
1462
1463 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001464 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001465 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001466
1467 /* clear wol status, enable energy detection */
1468 val &= ~PM_CTL_WUPS_;
1469 val |= (PM_CTL_WUPS_ED_ | PM_CTL_ED_EN_);
1470
1471 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Ming Lei76437212013-02-22 03:05:03 +00001472 if (ret < 0)
1473 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001474
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001475 pdata->suspend_flags |= SUSPEND_SUSPEND1;
1476
Ming Lei76437212013-02-22 03:05:03 +00001477 return 0;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001478}
1479
1480static int smsc95xx_enter_suspend2(struct usbnet *dev)
1481{
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001482 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
Steve Glendinning319b95b2012-11-22 08:05:25 +00001483 u32 val;
1484 int ret;
1485
1486 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001487 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001488 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001489
1490 val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1491 val |= PM_CTL_SUS_MODE_2;
1492
1493 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Ming Lei76437212013-02-22 03:05:03 +00001494 if (ret < 0)
1495 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001496
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001497 pdata->suspend_flags |= SUSPEND_SUSPEND2;
1498
Ming Lei76437212013-02-22 03:05:03 +00001499 return 0;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001500}
1501
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001502static int smsc95xx_enter_suspend3(struct usbnet *dev)
1503{
1504 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1505 u32 val;
1506 int ret;
1507
1508 ret = smsc95xx_read_reg_nopm(dev, RX_FIFO_INF, &val);
1509 if (ret < 0)
1510 return ret;
1511
Martin Wetterwald53a759c2017-04-13 10:08:44 +02001512 if (val & RX_FIFO_INF_USED_) {
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001513 netdev_info(dev->net, "rx fifo not empty in autosuspend\n");
1514 return -EBUSY;
1515 }
1516
1517 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1518 if (ret < 0)
1519 return ret;
1520
1521 val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1522 val |= PM_CTL_SUS_MODE_3 | PM_CTL_RES_CLR_WKP_STS;
1523
1524 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1525 if (ret < 0)
1526 return ret;
1527
1528 /* clear wol status */
1529 val &= ~PM_CTL_WUPS_;
1530 val |= PM_CTL_WUPS_WOL_;
1531
1532 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1533 if (ret < 0)
1534 return ret;
1535
1536 pdata->suspend_flags |= SUSPEND_SUSPEND3;
1537
1538 return 0;
1539}
1540
1541static int smsc95xx_autosuspend(struct usbnet *dev, u32 link_up)
1542{
1543 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1544 int ret;
1545
1546 if (!netif_running(dev->net)) {
1547 /* interface is ifconfig down so fully power down hw */
1548 netdev_dbg(dev->net, "autosuspend entering SUSPEND2\n");
1549 return smsc95xx_enter_suspend2(dev);
1550 }
1551
1552 if (!link_up) {
1553 /* link is down so enter EDPD mode, but only if device can
1554 * reliably resume from it. This check should be redundant
Ming Leieb970ff2013-02-22 03:05:05 +00001555 * as current FEATURE_REMOTE_WAKEUP parts also support
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001556 * FEATURE_PHY_NLP_CROSSOVER but it's included for clarity */
1557 if (!(pdata->features & FEATURE_PHY_NLP_CROSSOVER)) {
1558 netdev_warn(dev->net, "EDPD not supported\n");
1559 return -EBUSY;
1560 }
1561
1562 netdev_dbg(dev->net, "autosuspend entering SUSPEND1\n");
1563
1564 /* enable PHY wakeup events for if cable is attached */
1565 ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1566 PHY_INT_MASK_ANEG_COMP_);
1567 if (ret < 0) {
1568 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1569 return ret;
1570 }
1571
1572 netdev_info(dev->net, "entering SUSPEND1 mode\n");
1573 return smsc95xx_enter_suspend1(dev);
1574 }
1575
1576 /* enable PHY wakeup events so we remote wakeup if cable is pulled */
1577 ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1578 PHY_INT_MASK_LINK_DOWN_);
1579 if (ret < 0) {
1580 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1581 return ret;
1582 }
1583
1584 netdev_dbg(dev->net, "autosuspend entering SUSPEND3\n");
1585 return smsc95xx_enter_suspend3(dev);
1586}
1587
Steve Glendinningb5a04472012-09-28 00:07:11 +00001588static int smsc95xx_suspend(struct usb_interface *intf, pm_message_t message)
1589{
1590 struct usbnet *dev = usb_get_intfdata(intf);
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001591 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001592 u32 val, link_up;
Steve Glendinningb5a04472012-09-28 00:07:11 +00001593 int ret;
Steve Glendinningb5a04472012-09-28 00:07:11 +00001594
Steve Glendinningb5a04472012-09-28 00:07:11 +00001595 ret = usbnet_suspend(intf, message);
Steve Glendinningb052e072012-11-30 05:55:52 +00001596 if (ret < 0) {
1597 netdev_warn(dev->net, "usbnet_suspend error\n");
1598 return ret;
1599 }
Steve Glendinningb5a04472012-09-28 00:07:11 +00001600
Frieder Schrempf7b900ea2018-10-31 22:52:19 +01001601 cancel_delayed_work_sync(&pdata->carrier_check);
1602
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001603 if (pdata->suspend_flags) {
1604 netdev_warn(dev->net, "error during last resume\n");
1605 pdata->suspend_flags = 0;
1606 }
1607
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001608 /* determine if link is up using only _nopm functions */
1609 link_up = smsc95xx_link_ok_nopm(dev);
1610
Ming Lei42e21c02013-02-22 03:05:04 +00001611 if (message.event == PM_EVENT_AUTO_SUSPEND &&
Ming Leieb970ff2013-02-22 03:05:05 +00001612 (pdata->features & FEATURE_REMOTE_WAKEUP)) {
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001613 ret = smsc95xx_autosuspend(dev, link_up);
1614 goto done;
1615 }
1616
1617 /* if we get this far we're not autosuspending */
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001618 /* if no wol options set, or if link is down and we're not waking on
1619 * PHY activity, enter lowest power SUSPEND2 mode
1620 */
1621 if (!(pdata->wolopts & SUPPORTED_WAKE) ||
1622 !(link_up || (pdata->wolopts & WAKE_PHY))) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001623 netdev_info(dev->net, "entering SUSPEND2 mode\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001624
1625 /* disable energy detect (link up) & wake up events */
Ming Leiec321152012-11-06 04:53:07 +00001626 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001627 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001628 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001629
1630 val &= ~(WUCSR_MPEN_ | WUCSR_WAKE_EN_);
1631
Ming Leiec321152012-11-06 04:53:07 +00001632 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001633 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001634 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001635
Ming Leiec321152012-11-06 04:53:07 +00001636 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001637 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001638 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001639
1640 val &= ~(PM_CTL_ED_EN_ | PM_CTL_WOL_EN_);
1641
Ming Leiec321152012-11-06 04:53:07 +00001642 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001643 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001644 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001645
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001646 ret = smsc95xx_enter_suspend2(dev);
1647 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001648 }
1649
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001650 if (pdata->wolopts & WAKE_PHY) {
1651 ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1652 (PHY_INT_MASK_ANEG_COMP_ | PHY_INT_MASK_LINK_DOWN_));
Steve Glendinningb052e072012-11-30 05:55:52 +00001653 if (ret < 0) {
1654 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1655 goto done;
1656 }
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001657
1658 /* if link is down then configure EDPD and enter SUSPEND1,
1659 * otherwise enter SUSPEND0 below
1660 */
1661 if (!link_up) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001662 netdev_info(dev->net, "entering SUSPEND1 mode\n");
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001663 ret = smsc95xx_enter_suspend1(dev);
1664 goto done;
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001665 }
1666 }
1667
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001668 if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
Kees Cook6396bb22018-06-12 14:03:40 -07001669 u32 *filter_mask = kcalloc(32, sizeof(u32), GFP_KERNEL);
Ming Lei06a221b2012-11-06 04:53:06 +00001670 u32 command[2];
1671 u32 offset[2];
1672 u32 crc[4];
Steve Glendinning9ebca502012-11-22 08:05:23 +00001673 int wuff_filter_count =
1674 (pdata->features & FEATURE_8_WAKEUP_FILTERS) ?
1675 LAN9500A_WUFF_NUM : LAN9500_WUFF_NUM;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001676 int i, filter = 0;
1677
Steve Glendinningeed9a722012-11-30 05:55:48 +00001678 if (!filter_mask) {
1679 netdev_warn(dev->net, "Unable to allocate filter_mask\n");
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001680 ret = -ENOMEM;
1681 goto done;
Steve Glendinningeed9a722012-11-30 05:55:48 +00001682 }
1683
Ming Lei06a221b2012-11-06 04:53:06 +00001684 memset(command, 0, sizeof(command));
1685 memset(offset, 0, sizeof(offset));
1686 memset(crc, 0, sizeof(crc));
1687
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001688 if (pdata->wolopts & WAKE_BCAST) {
1689 const u8 bcast[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Joe Perches1e1d7412012-11-24 01:27:49 +00001690 netdev_info(dev->net, "enabling broadcast detection\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001691 filter_mask[filter * 4] = 0x003F;
1692 filter_mask[filter * 4 + 1] = 0x00;
1693 filter_mask[filter * 4 + 2] = 0x00;
1694 filter_mask[filter * 4 + 3] = 0x00;
1695 command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1696 offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1697 crc[filter/2] |= smsc_crc(bcast, 6, filter);
1698 filter++;
1699 }
1700
1701 if (pdata->wolopts & WAKE_MCAST) {
1702 const u8 mcast[] = {0x01, 0x00, 0x5E};
Joe Perches1e1d7412012-11-24 01:27:49 +00001703 netdev_info(dev->net, "enabling multicast detection\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001704 filter_mask[filter * 4] = 0x0007;
1705 filter_mask[filter * 4 + 1] = 0x00;
1706 filter_mask[filter * 4 + 2] = 0x00;
1707 filter_mask[filter * 4 + 3] = 0x00;
1708 command[filter/4] |= 0x09UL << ((filter % 4) * 8);
1709 offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1710 crc[filter/2] |= smsc_crc(mcast, 3, filter);
1711 filter++;
1712 }
1713
1714 if (pdata->wolopts & WAKE_ARP) {
1715 const u8 arp[] = {0x08, 0x06};
Joe Perches1e1d7412012-11-24 01:27:49 +00001716 netdev_info(dev->net, "enabling ARP detection\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001717 filter_mask[filter * 4] = 0x0003;
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] |= 0x05UL << ((filter % 4) * 8);
1722 offset[filter/4] |= 0x0C << ((filter % 4) * 8);
1723 crc[filter/2] |= smsc_crc(arp, 2, filter);
1724 filter++;
1725 }
1726
1727 if (pdata->wolopts & WAKE_UCAST) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001728 netdev_info(dev->net, "enabling unicast detection\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001729 filter_mask[filter * 4] = 0x003F;
1730 filter_mask[filter * 4 + 1] = 0x00;
1731 filter_mask[filter * 4 + 2] = 0x00;
1732 filter_mask[filter * 4 + 3] = 0x00;
1733 command[filter/4] |= 0x01UL << ((filter % 4) * 8);
1734 offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1735 crc[filter/2] |= smsc_crc(dev->net->dev_addr, ETH_ALEN, filter);
1736 filter++;
1737 }
1738
Steve Glendinning9ebca502012-11-22 08:05:23 +00001739 for (i = 0; i < (wuff_filter_count * 4); i++) {
Ming Leiec321152012-11-06 04:53:07 +00001740 ret = smsc95xx_write_reg_nopm(dev, WUFF, filter_mask[i]);
Steve Glendinningb052e072012-11-30 05:55:52 +00001741 if (ret < 0) {
Ming Lei06a221b2012-11-06 04:53:06 +00001742 kfree(filter_mask);
Steve Glendinningb052e072012-11-30 05:55:52 +00001743 goto done;
1744 }
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001745 }
Ming Lei06a221b2012-11-06 04:53:06 +00001746 kfree(filter_mask);
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001747
Steve Glendinning9ebca502012-11-22 08:05:23 +00001748 for (i = 0; i < (wuff_filter_count / 4); i++) {
Ming Leiec321152012-11-06 04:53:07 +00001749 ret = smsc95xx_write_reg_nopm(dev, WUFF, command[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
Steve Glendinning9ebca502012-11-22 08:05:23 +00001754 for (i = 0; i < (wuff_filter_count / 4); i++) {
Ming Leiec321152012-11-06 04:53:07 +00001755 ret = smsc95xx_write_reg_nopm(dev, WUFF, offset[i]);
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
Steve Glendinning9ebca502012-11-22 08:05:23 +00001760 for (i = 0; i < (wuff_filter_count / 2); i++) {
Ming Leiec321152012-11-06 04:53:07 +00001761 ret = smsc95xx_write_reg_nopm(dev, WUFF, crc[i]);
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
1766 /* clear any pending pattern match packet status */
Ming Leiec321152012-11-06 04:53:07 +00001767 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001768 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001769 goto done;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001770
1771 val |= WUCSR_WUFR_;
1772
Ming Leiec321152012-11-06 04:53:07 +00001773 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001774 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001775 goto done;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001776 }
1777
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001778 if (pdata->wolopts & WAKE_MAGIC) {
1779 /* clear any pending magic packet status */
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
1784 val |= WUCSR_MPR_;
1785
Ming Leiec321152012-11-06 04:53:07 +00001786 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001787 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001788 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001789 }
1790
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001791 /* enable/disable wakeup sources */
Ming Leiec321152012-11-06 04:53:07 +00001792 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001793 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001794 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001795
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001796 if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001797 netdev_info(dev->net, "enabling pattern match wakeup\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001798 val |= WUCSR_WAKE_EN_;
1799 } else {
Joe Perches1e1d7412012-11-24 01:27:49 +00001800 netdev_info(dev->net, "disabling pattern match wakeup\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001801 val &= ~WUCSR_WAKE_EN_;
1802 }
1803
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001804 if (pdata->wolopts & WAKE_MAGIC) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001805 netdev_info(dev->net, "enabling magic packet wakeup\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001806 val |= WUCSR_MPEN_;
1807 } else {
Joe Perches1e1d7412012-11-24 01:27:49 +00001808 netdev_info(dev->net, "disabling magic packet wakeup\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001809 val &= ~WUCSR_MPEN_;
1810 }
1811
Ming Leiec321152012-11-06 04:53:07 +00001812 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001813 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001814 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001815
1816 /* enable wol wakeup source */
Ming Leiec321152012-11-06 04:53:07 +00001817 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001818 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001819 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001820
1821 val |= PM_CTL_WOL_EN_;
1822
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001823 /* phy energy detect wakeup source */
1824 if (pdata->wolopts & WAKE_PHY)
1825 val |= PM_CTL_ED_EN_;
1826
Ming Leiec321152012-11-06 04:53:07 +00001827 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001828 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001829 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001830
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001831 /* enable receiver to enable frame reception */
Ming Leiec321152012-11-06 04:53:07 +00001832 smsc95xx_start_rx_path(dev, 1);
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001833
1834 /* some wol options are enabled, so enter SUSPEND0 */
Joe Perches1e1d7412012-11-24 01:27:49 +00001835 netdev_info(dev->net, "entering SUSPEND0 mode\n");
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001836 ret = smsc95xx_enter_suspend0(dev);
1837
1838done:
Ming Lei0d41be52013-03-15 12:08:58 +08001839 /*
1840 * TODO: resume() might need to handle the suspend failure
1841 * in system sleep
1842 */
1843 if (ret && PMSG_IS_AUTO(message))
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001844 usbnet_resume(intf);
Frieder Schrempf7b900ea2018-10-31 22:52:19 +01001845
1846 if (ret)
1847 schedule_delayed_work(&pdata->carrier_check,
1848 CARRIER_CHECK_DELAY);
1849
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001850 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001851}
1852
1853static int smsc95xx_resume(struct usb_interface *intf)
1854{
1855 struct usbnet *dev = usb_get_intfdata(intf);
Sudip Mukherjee8bca81d2014-11-11 14:10:47 +05301856 struct smsc95xx_priv *pdata;
1857 u8 suspend_flags;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001858 int ret;
1859 u32 val;
1860
1861 BUG_ON(!dev);
Sudip Mukherjee8bca81d2014-11-11 14:10:47 +05301862 pdata = (struct smsc95xx_priv *)(dev->data[0]);
1863 suspend_flags = pdata->suspend_flags;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001864
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001865 netdev_dbg(dev->net, "resume suspend_flags=0x%02x\n", suspend_flags);
1866
1867 /* do this first to ensure it's cleared even in error case */
1868 pdata->suspend_flags = 0;
Christoph Fritzd69d1692016-05-26 04:06:47 +02001869 schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001870
1871 if (suspend_flags & SUSPEND_ALLMODES) {
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001872 /* clear wake-up sources */
Ming Leiec321152012-11-06 04:53:07 +00001873 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001874 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001875 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001876
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001877 val &= ~(WUCSR_WAKE_EN_ | WUCSR_MPEN_);
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001878
Ming Leiec321152012-11-06 04:53:07 +00001879 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001880 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001881 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001882
1883 /* clear wake-up status */
Ming Leiec321152012-11-06 04:53:07 +00001884 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001885 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001886 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001887
1888 val &= ~PM_CTL_WOL_EN_;
1889 val |= PM_CTL_WUPS_;
1890
Ming Leiec321152012-11-06 04:53:07 +00001891 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001892 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001893 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001894 }
1895
Steve Glendinningaf3d7c12012-11-22 08:05:22 +00001896 ret = usbnet_resume(intf);
Steve Glendinningb052e072012-11-30 05:55:52 +00001897 if (ret < 0)
1898 netdev_warn(dev->net, "usbnet_resume error\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001899
Steve Glendinningb052e072012-11-30 05:55:52 +00001900 return ret;
Steve Glendinningb5a04472012-09-28 00:07:11 +00001901}
1902
Joonyoung Shimb4df4802014-07-10 11:49:42 +09001903static int smsc95xx_reset_resume(struct usb_interface *intf)
1904{
1905 struct usbnet *dev = usb_get_intfdata(intf);
1906 int ret;
1907
1908 ret = smsc95xx_reset(dev);
1909 if (ret < 0)
1910 return ret;
1911
1912 return smsc95xx_resume(intf);
1913}
1914
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001915static void smsc95xx_rx_csum_offload(struct sk_buff *skb)
1916{
1917 skb->csum = *(u16 *)(skb_tail_pointer(skb) - 2);
1918 skb->ip_summed = CHECKSUM_COMPLETE;
1919 skb_trim(skb, skb->len - 2);
1920}
1921
1922static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
1923{
Emil Goodeeb855692014-02-13 17:50:19 +01001924 /* This check is no longer done by usbnet */
1925 if (skb->len < dev->net->hard_header_len)
1926 return 0;
1927
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001928 while (skb->len > 0) {
1929 u32 header, align_count;
1930 struct sk_buff *ax_skb;
1931 unsigned char *packet;
1932 u16 size;
1933
1934 memcpy(&header, skb->data, sizeof(header));
1935 le32_to_cpus(&header);
1936 skb_pull(skb, 4 + NET_IP_ALIGN);
1937 packet = skb->data;
1938
1939 /* get the packet length */
1940 size = (u16)((header & RX_STS_FL_) >> 16);
1941 align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4;
1942
1943 if (unlikely(header & RX_STS_ES_)) {
Joe Perchesa475f602010-02-17 10:30:24 +00001944 netif_dbg(dev, rx_err, dev->net,
1945 "Error header=0x%08x\n", header);
Herbert Xu80667ac2009-06-29 16:53:00 +00001946 dev->net->stats.rx_errors++;
1947 dev->net->stats.rx_dropped++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001948
1949 if (header & RX_STS_CRC_) {
Herbert Xu80667ac2009-06-29 16:53:00 +00001950 dev->net->stats.rx_crc_errors++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001951 } else {
1952 if (header & (RX_STS_TL_ | RX_STS_RF_))
Herbert Xu80667ac2009-06-29 16:53:00 +00001953 dev->net->stats.rx_frame_errors++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001954
1955 if ((header & RX_STS_LE_) &&
1956 (!(header & RX_STS_FT_)))
Herbert Xu80667ac2009-06-29 16:53:00 +00001957 dev->net->stats.rx_length_errors++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001958 }
1959 } else {
1960 /* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
1961 if (unlikely(size > (ETH_FRAME_LEN + 12))) {
Joe Perchesa475f602010-02-17 10:30:24 +00001962 netif_dbg(dev, rx_err, dev->net,
1963 "size err header=0x%08x\n", header);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001964 return 0;
1965 }
1966
1967 /* last frame in this batch */
1968 if (skb->len == size) {
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001969 if (dev->net->features & NETIF_F_RXCSUM)
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001970 smsc95xx_rx_csum_offload(skb);
Peter Korsgaarddf18acc2009-05-13 10:10:41 +00001971 skb_trim(skb, skb->len - 4); /* remove fcs */
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001972 skb->truesize = size + sizeof(struct sk_buff);
1973
1974 return 1;
1975 }
1976
1977 ax_skb = skb_clone(skb, GFP_ATOMIC);
1978 if (unlikely(!ax_skb)) {
Joe Perches60b86752010-02-17 10:30:23 +00001979 netdev_warn(dev->net, "Error allocating skb\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001980 return 0;
1981 }
1982
1983 ax_skb->len = size;
1984 ax_skb->data = packet;
1985 skb_set_tail_pointer(ax_skb, size);
1986
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001987 if (dev->net->features & NETIF_F_RXCSUM)
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001988 smsc95xx_rx_csum_offload(ax_skb);
Peter Korsgaarddf18acc2009-05-13 10:10:41 +00001989 skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001990 ax_skb->truesize = size + sizeof(struct sk_buff);
1991
1992 usbnet_skb_return(dev, ax_skb);
1993 }
1994
1995 skb_pull(skb, size);
1996
1997 /* padding bytes before the next frame starts */
1998 if (skb->len)
1999 skb_pull(skb, align_count);
2000 }
2001
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002002 return 1;
2003}
2004
Steve Glendinningf7b29272008-11-20 04:19:21 -08002005static u32 smsc95xx_calc_csum_preamble(struct sk_buff *skb)
2006{
Michał Mirosław55508d62010-12-14 15:24:08 +00002007 u16 low_16 = (u16)skb_checksum_start_offset(skb);
2008 u16 high_16 = low_16 + skb->csum_offset;
Steve Glendinningf7b29272008-11-20 04:19:21 -08002009 return (high_16 << 16) | low_16;
2010}
2011
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002012static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,
2013 struct sk_buff *skb, gfp_t flags)
2014{
Michał Mirosław78e47fe2011-04-01 20:56:23 -07002015 bool csum = skb->ip_summed == CHECKSUM_PARTIAL;
Steve Glendinningf7b29272008-11-20 04:19:21 -08002016 int overhead = csum ? SMSC95XX_TX_OVERHEAD_CSUM : SMSC95XX_TX_OVERHEAD;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002017 u32 tx_cmd_a, tx_cmd_b;
2018
Steve Glendinningf7b29272008-11-20 04:19:21 -08002019 /* We do not advertise SG, so skbs should be already linearized */
2020 BUG_ON(skb_shinfo(skb)->nr_frags);
2021
James Hughese9156cd2017-04-19 11:13:40 +01002022 /* Make writable and expand header space by overhead if required */
2023 if (skb_cow_head(skb, overhead)) {
2024 /* Must deallocate here as returning NULL to indicate error
2025 * means the skb won't be deallocated in the caller.
2026 */
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002027 dev_kfree_skb_any(skb);
James Hughese9156cd2017-04-19 11:13:40 +01002028 return NULL;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002029 }
2030
Steve Glendinningf7b29272008-11-20 04:19:21 -08002031 if (csum) {
Steve Glendinning11bc3082010-03-18 22:18:41 -07002032 if (skb->len <= 45) {
2033 /* workaround - hardware tx checksum does not work
2034 * properly with extremely small packets */
Michał Mirosław55508d62010-12-14 15:24:08 +00002035 long csstart = skb_checksum_start_offset(skb);
Steve Glendinning11bc3082010-03-18 22:18:41 -07002036 __wsum calc = csum_partial(skb->data + csstart,
2037 skb->len - csstart, 0);
2038 *((__sum16 *)(skb->data + csstart
2039 + skb->csum_offset)) = csum_fold(calc);
2040
2041 csum = false;
2042 } else {
2043 u32 csum_preamble = smsc95xx_calc_csum_preamble(skb);
2044 skb_push(skb, 4);
Steve Glendinning00acda62012-11-02 00:44:20 +00002045 cpu_to_le32s(&csum_preamble);
Steve Glendinning11bc3082010-03-18 22:18:41 -07002046 memcpy(skb->data, &csum_preamble, 4);
2047 }
Steve Glendinningf7b29272008-11-20 04:19:21 -08002048 }
2049
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002050 skb_push(skb, 4);
2051 tx_cmd_b = (u32)(skb->len - 4);
Steve Glendinningf7b29272008-11-20 04:19:21 -08002052 if (csum)
2053 tx_cmd_b |= TX_CMD_B_CSUM_ENABLE;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002054 cpu_to_le32s(&tx_cmd_b);
2055 memcpy(skb->data, &tx_cmd_b, 4);
2056
2057 skb_push(skb, 4);
2058 tx_cmd_a = (u32)(skb->len - 8) | TX_CMD_A_FIRST_SEG_ |
2059 TX_CMD_A_LAST_SEG_;
2060 cpu_to_le32s(&tx_cmd_a);
2061 memcpy(skb->data, &tx_cmd_a, 4);
2062
2063 return skb;
2064}
2065
Steve Glendinningb2d4b152013-01-03 03:00:16 +00002066static int smsc95xx_manage_power(struct usbnet *dev, int on)
2067{
2068 struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
2069
2070 dev->intf->needs_remote_wakeup = on;
2071
Ming Leieb970ff2013-02-22 03:05:05 +00002072 if (pdata->features & FEATURE_REMOTE_WAKEUP)
Steve Glendinningb2d4b152013-01-03 03:00:16 +00002073 return 0;
2074
Ming Leieb970ff2013-02-22 03:05:05 +00002075 /* this chip revision isn't capable of remote wakeup */
2076 netdev_info(dev->net, "hardware isn't capable of remote wakeup\n");
Steve Glendinningb2d4b152013-01-03 03:00:16 +00002077
2078 if (on)
2079 usb_autopm_get_interface_no_resume(dev->intf);
2080 else
2081 usb_autopm_put_interface(dev->intf);
2082
2083 return 0;
2084}
2085
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002086static const struct driver_info smsc95xx_info = {
2087 .description = "smsc95xx USB 2.0 Ethernet",
2088 .bind = smsc95xx_bind,
2089 .unbind = smsc95xx_unbind,
2090 .link_reset = smsc95xx_link_reset,
2091 .reset = smsc95xx_reset,
2092 .rx_fixup = smsc95xx_rx_fixup,
2093 .tx_fixup = smsc95xx_tx_fixup,
2094 .status = smsc95xx_status,
Steve Glendinningb2d4b152013-01-03 03:00:16 +00002095 .manage_power = smsc95xx_manage_power,
Paolo Pisati07d69d42012-04-23 04:05:20 +00002096 .flags = FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002097};
2098
2099static const struct usb_device_id products[] = {
2100 {
2101 /* SMSC9500 USB Ethernet Device */
2102 USB_DEVICE(0x0424, 0x9500),
2103 .driver_info = (unsigned long) &smsc95xx_info,
2104 },
Steve Glendinning726474b2009-05-01 06:07:22 +00002105 {
Steve Glendinning6f41d122009-09-22 05:13:02 +00002106 /* SMSC9505 USB Ethernet Device */
2107 USB_DEVICE(0x0424, 0x9505),
2108 .driver_info = (unsigned long) &smsc95xx_info,
2109 },
2110 {
2111 /* SMSC9500A USB Ethernet Device */
2112 USB_DEVICE(0x0424, 0x9E00),
2113 .driver_info = (unsigned long) &smsc95xx_info,
2114 },
2115 {
2116 /* SMSC9505A USB Ethernet Device */
2117 USB_DEVICE(0x0424, 0x9E01),
2118 .driver_info = (unsigned long) &smsc95xx_info,
2119 },
2120 {
Steve Glendinning726474b2009-05-01 06:07:22 +00002121 /* SMSC9512/9514 USB Hub & Ethernet Device */
2122 USB_DEVICE(0x0424, 0xec00),
2123 .driver_info = (unsigned long) &smsc95xx_info,
2124 },
Steve Glendinning6f41d122009-09-22 05:13:02 +00002125 {
2126 /* SMSC9500 USB Ethernet Device (SAL10) */
2127 USB_DEVICE(0x0424, 0x9900),
2128 .driver_info = (unsigned long) &smsc95xx_info,
2129 },
2130 {
2131 /* SMSC9505 USB Ethernet Device (SAL10) */
2132 USB_DEVICE(0x0424, 0x9901),
2133 .driver_info = (unsigned long) &smsc95xx_info,
2134 },
2135 {
2136 /* SMSC9500A USB Ethernet Device (SAL10) */
2137 USB_DEVICE(0x0424, 0x9902),
2138 .driver_info = (unsigned long) &smsc95xx_info,
2139 },
2140 {
2141 /* SMSC9505A USB Ethernet Device (SAL10) */
2142 USB_DEVICE(0x0424, 0x9903),
2143 .driver_info = (unsigned long) &smsc95xx_info,
2144 },
2145 {
2146 /* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */
2147 USB_DEVICE(0x0424, 0x9904),
2148 .driver_info = (unsigned long) &smsc95xx_info,
2149 },
2150 {
2151 /* SMSC9500A USB Ethernet Device (HAL) */
2152 USB_DEVICE(0x0424, 0x9905),
2153 .driver_info = (unsigned long) &smsc95xx_info,
2154 },
2155 {
2156 /* SMSC9505A USB Ethernet Device (HAL) */
2157 USB_DEVICE(0x0424, 0x9906),
2158 .driver_info = (unsigned long) &smsc95xx_info,
2159 },
2160 {
2161 /* SMSC9500 USB Ethernet Device (Alternate ID) */
2162 USB_DEVICE(0x0424, 0x9907),
2163 .driver_info = (unsigned long) &smsc95xx_info,
2164 },
2165 {
2166 /* SMSC9500A USB Ethernet Device (Alternate ID) */
2167 USB_DEVICE(0x0424, 0x9908),
2168 .driver_info = (unsigned long) &smsc95xx_info,
2169 },
2170 {
2171 /* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */
2172 USB_DEVICE(0x0424, 0x9909),
2173 .driver_info = (unsigned long) &smsc95xx_info,
2174 },
Steve Glendinning88edaa42011-04-10 18:59:27 -07002175 {
2176 /* SMSC LAN9530 USB Ethernet Device */
2177 USB_DEVICE(0x0424, 0x9530),
2178 .driver_info = (unsigned long) &smsc95xx_info,
2179 },
2180 {
2181 /* SMSC LAN9730 USB Ethernet Device */
2182 USB_DEVICE(0x0424, 0x9730),
2183 .driver_info = (unsigned long) &smsc95xx_info,
2184 },
2185 {
2186 /* SMSC LAN89530 USB Ethernet Device */
2187 USB_DEVICE(0x0424, 0x9E08),
2188 .driver_info = (unsigned long) &smsc95xx_info,
2189 },
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002190 { }, /* END */
2191};
2192MODULE_DEVICE_TABLE(usb, products);
2193
2194static struct usb_driver smsc95xx_driver = {
2195 .name = "smsc95xx",
2196 .id_table = products,
2197 .probe = usbnet_probe,
Steve Glendinningb5a04472012-09-28 00:07:11 +00002198 .suspend = smsc95xx_suspend,
Steve Glendinninge0e474a2012-09-28 00:07:12 +00002199 .resume = smsc95xx_resume,
Joonyoung Shimb4df4802014-07-10 11:49:42 +09002200 .reset_resume = smsc95xx_reset_resume,
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002201 .disconnect = usbnet_disconnect,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07002202 .disable_hub_initiated_lpm = 1,
Steve Glendinningb2d4b152013-01-03 03:00:16 +00002203 .supports_autosuspend = 1,
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002204};
2205
Greg Kroah-Hartmand632eb12011-11-18 09:44:20 -08002206module_usb_driver(smsc95xx_driver);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002207
2208MODULE_AUTHOR("Nancy Lin");
Steve Glendinning90b24cf2012-04-16 12:13:29 +01002209MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002210MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices");
2211MODULE_LICENSE("GPL");