blob: 26b1bd8e845b437b7fdff7e22d8c4803fc020268 [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002 /***************************************************************************
3 *
4 * Copyright (C) 2007-2008 SMSC
5 *
Steve Glendinning2f7ca802008-10-02 05:27:57 +00006 *****************************************************************************/
7
8#include <linux/module.h>
9#include <linux/kmod.h>
Steve Glendinning2f7ca802008-10-02 05:27:57 +000010#include <linux/netdevice.h>
11#include <linux/etherdevice.h>
12#include <linux/ethtool.h>
13#include <linux/mii.h>
14#include <linux/usb.h>
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +000015#include <linux/bitrev.h>
16#include <linux/crc16.h>
Steve Glendinning2f7ca802008-10-02 05:27:57 +000017#include <linux/crc32.h>
18#include <linux/usb/usbnet.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Arnd Bergmannc4895652016-04-29 09:05:59 +020020#include <linux/of_net.h>
Andre Edich05b35e72020-08-26 13:17:17 +020021#include <linux/mdio.h>
22#include <linux/phy.h>
Steve Glendinning2f7ca802008-10-02 05:27:57 +000023#include "smsc95xx.h"
24
25#define SMSC_CHIPNAME "smsc95xx"
Andre Edich05b35e72020-08-26 13:17:17 +020026#define SMSC_DRIVER_VERSION "2.0.0"
Steve Glendinning2f7ca802008-10-02 05:27:57 +000027#define HS_USB_PKT_SIZE (512)
28#define FS_USB_PKT_SIZE (64)
29#define DEFAULT_HS_BURST_CAP_SIZE (16 * 1024 + 5 * HS_USB_PKT_SIZE)
30#define DEFAULT_FS_BURST_CAP_SIZE (6 * 1024 + 33 * FS_USB_PKT_SIZE)
31#define DEFAULT_BULK_IN_DELAY (0x00002000)
32#define MAX_SINGLE_PACKET_SIZE (2048)
33#define LAN95XX_EEPROM_MAGIC (0x9500)
34#define EEPROM_MAC_OFFSET (0x01)
Steve Glendinningf7b29272008-11-20 04:19:21 -080035#define DEFAULT_TX_CSUM_ENABLE (true)
Steve Glendinning2f7ca802008-10-02 05:27:57 +000036#define DEFAULT_RX_CSUM_ENABLE (true)
37#define SMSC95XX_INTERNAL_PHY_ID (1)
38#define SMSC95XX_TX_OVERHEAD (8)
Steve Glendinningf7b29272008-11-20 04:19:21 -080039#define SMSC95XX_TX_OVERHEAD_CSUM (12)
Steve Glendinninge5e3af82012-11-22 08:05:24 +000040#define SUPPORTED_WAKE (WAKE_PHY | WAKE_UCAST | WAKE_BCAST | \
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +000041 WAKE_MCAST | WAKE_ARP | WAKE_MAGIC)
Steve Glendinning2f7ca802008-10-02 05:27:57 +000042
Steve Glendinning9ebca502012-11-22 08:05:23 +000043#define FEATURE_8_WAKEUP_FILTERS (0x01)
44#define FEATURE_PHY_NLP_CROSSOVER (0x02)
Ming Leieb970ff2013-02-22 03:05:05 +000045#define FEATURE_REMOTE_WAKEUP (0x04)
Steve Glendinning9ebca502012-11-22 08:05:23 +000046
Steve Glendinningb2d4b152013-01-03 03:00:16 +000047#define SUSPEND_SUSPEND0 (0x01)
48#define SUSPEND_SUSPEND1 (0x02)
49#define SUSPEND_SUSPEND2 (0x04)
50#define SUSPEND_SUSPEND3 (0x08)
51#define SUSPEND_ALLMODES (SUSPEND_SUSPEND0 | SUSPEND_SUSPEND1 | \
52 SUSPEND_SUSPEND2 | SUSPEND_SUSPEND3)
53
Steve Glendinning2f7ca802008-10-02 05:27:57 +000054struct smsc95xx_priv {
55 u32 mac_cr;
Marc Zyngier3c0f3c62011-03-18 03:53:58 +000056 u32 hash_hi;
57 u32 hash_lo;
Steve Glendinninge0e474a2012-09-28 00:07:12 +000058 u32 wolopts;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000059 spinlock_t mac_cr_lock;
Steve Glendinning9ebca502012-11-22 08:05:23 +000060 u8 features;
Steve Glendinningb2d4b152013-01-03 03:00:16 +000061 u8 suspend_flags;
Andre Edich05b35e72020-08-26 13:17:17 +020062 struct mii_bus *mdiobus;
63 struct phy_device *phydev;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000064};
65
Rusty Russelleb939922011-12-19 14:08:01 +000066static bool turbo_mode = true;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000067module_param(turbo_mode, bool, 0644);
68MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
69
Ming Leiec321152012-11-06 04:53:07 +000070static int __must_check __smsc95xx_read_reg(struct usbnet *dev, u32 index,
71 u32 *data, int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +000072{
Ming Lei72108fd2012-10-24 19:47:04 +000073 u32 buf;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000074 int ret;
Ming Leiec321152012-11-06 04:53:07 +000075 int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
Steve Glendinning2f7ca802008-10-02 05:27:57 +000076
77 BUG_ON(!dev);
78
Ming Leiec321152012-11-06 04:53:07 +000079 if (!in_pm)
80 fn = usbnet_read_cmd;
81 else
82 fn = usbnet_read_cmd_nopm;
83
84 ret = fn(dev, USB_VENDOR_REQUEST_READ_REGISTER, USB_DIR_IN
85 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
86 0, index, &buf, 4);
Dan Carpenter5a36b682016-05-04 09:22:01 +030087 if (unlikely(ret < 0)) {
Joe Perches1e1d7412012-11-24 01:27:49 +000088 netdev_warn(dev->net, "Failed to read reg index 0x%08x: %d\n",
89 index, ret);
Dan Carpenter5a36b682016-05-04 09:22:01 +030090 return ret;
91 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +000092
Ming Lei72108fd2012-10-24 19:47:04 +000093 le32_to_cpus(&buf);
94 *data = buf;
Steve Glendinning2f7ca802008-10-02 05:27:57 +000095
96 return ret;
97}
98
Ming Leiec321152012-11-06 04:53:07 +000099static int __must_check __smsc95xx_write_reg(struct usbnet *dev, u32 index,
100 u32 data, int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000101{
Ming Lei72108fd2012-10-24 19:47:04 +0000102 u32 buf;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000103 int ret;
Ming Leiec321152012-11-06 04:53:07 +0000104 int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000105
106 BUG_ON(!dev);
107
Ming Leiec321152012-11-06 04:53:07 +0000108 if (!in_pm)
109 fn = usbnet_write_cmd;
110 else
111 fn = usbnet_write_cmd_nopm;
112
Ming Lei72108fd2012-10-24 19:47:04 +0000113 buf = data;
114 cpu_to_le32s(&buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000115
Ming Leiec321152012-11-06 04:53:07 +0000116 ret = fn(dev, USB_VENDOR_REQUEST_WRITE_REGISTER, USB_DIR_OUT
117 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
118 0, index, &buf, 4);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000119 if (unlikely(ret < 0))
Joe Perches1e1d7412012-11-24 01:27:49 +0000120 netdev_warn(dev->net, "Failed to write reg index 0x%08x: %d\n",
121 index, ret);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000122
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000123 return ret;
124}
125
Ming Leiec321152012-11-06 04:53:07 +0000126static int __must_check smsc95xx_read_reg_nopm(struct usbnet *dev, u32 index,
127 u32 *data)
128{
129 return __smsc95xx_read_reg(dev, index, data, 1);
130}
131
132static int __must_check smsc95xx_write_reg_nopm(struct usbnet *dev, u32 index,
133 u32 data)
134{
135 return __smsc95xx_write_reg(dev, index, data, 1);
136}
137
138static int __must_check smsc95xx_read_reg(struct usbnet *dev, u32 index,
139 u32 *data)
140{
141 return __smsc95xx_read_reg(dev, index, data, 0);
142}
143
144static int __must_check smsc95xx_write_reg(struct usbnet *dev, u32 index,
145 u32 data)
146{
147 return __smsc95xx_write_reg(dev, index, data, 0);
148}
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000149
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000150/* Loop until the read is completed with timeout
151 * called with phy_mutex held */
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000152static int __must_check __smsc95xx_phy_wait_not_busy(struct usbnet *dev,
153 int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000154{
155 unsigned long start_time = jiffies;
156 u32 val;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000157 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000158
159 do {
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000160 ret = __smsc95xx_read_reg(dev, MII_ADDR, &val, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000161 if (ret < 0) {
162 netdev_warn(dev->net, "Error reading MII_ACCESS\n");
163 return ret;
164 }
165
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000166 if (!(val & MII_BUSY_))
167 return 0;
168 } while (!time_after(jiffies, start_time + HZ));
169
170 return -EIO;
171}
172
Andre Edich05b35e72020-08-26 13:17:17 +0200173static u32 mii_address_cmd(int phy_id, int idx, u16 op)
174{
175 return (phy_id & 0x1f) << 11 | (idx & 0x1f) << 6 | op;
176}
177
178static int __smsc95xx_mdio_read(struct usbnet *dev, int phy_id, int idx,
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000179 int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000180{
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000181 u32 val, addr;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000182 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000183
184 mutex_lock(&dev->phy_mutex);
185
186 /* confirm MII not busy */
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000187 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000188 if (ret < 0) {
Andre Edich05b35e72020-08-26 13:17:17 +0200189 netdev_warn(dev->net, "%s: MII is busy\n", __func__);
Steve Glendinningb052e072012-11-30 05:55:52 +0000190 goto done;
191 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000192
193 /* set the address, index & direction (read from PHY) */
Andre Edich05b35e72020-08-26 13:17:17 +0200194 addr = mii_address_cmd(phy_id, idx, MII_READ_ | MII_BUSY_);
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000195 ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000196 if (ret < 0) {
197 netdev_warn(dev->net, "Error writing MII_ADDR\n");
198 goto done;
199 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000200
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000201 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000202 if (ret < 0) {
203 netdev_warn(dev->net, "Timed out reading MII reg %02X\n", idx);
204 goto done;
205 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000206
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000207 ret = __smsc95xx_read_reg(dev, MII_DATA, &val, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000208 if (ret < 0) {
209 netdev_warn(dev->net, "Error reading MII_DATA\n");
210 goto done;
211 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000212
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000213 ret = (u16)(val & 0xFFFF);
214
215done:
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000216 mutex_unlock(&dev->phy_mutex);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000217 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000218}
219
Andre Edich05b35e72020-08-26 13:17:17 +0200220static void __smsc95xx_mdio_write(struct usbnet *dev, int phy_id,
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000221 int idx, int regval, int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000222{
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000223 u32 val, addr;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000224 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000225
226 mutex_lock(&dev->phy_mutex);
227
228 /* confirm MII not busy */
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000229 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000230 if (ret < 0) {
Andre Edich05b35e72020-08-26 13:17:17 +0200231 netdev_warn(dev->net, "%s: MII is busy\n", __func__);
Steve Glendinningb052e072012-11-30 05:55:52 +0000232 goto done;
233 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000234
235 val = regval;
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000236 ret = __smsc95xx_write_reg(dev, MII_DATA, val, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000237 if (ret < 0) {
238 netdev_warn(dev->net, "Error writing MII_DATA\n");
239 goto done;
240 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000241
242 /* set the address, index & direction (write to PHY) */
Andre Edich05b35e72020-08-26 13:17:17 +0200243 addr = mii_address_cmd(phy_id, idx, MII_WRITE_ | MII_BUSY_);
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000244 ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000245 if (ret < 0) {
246 netdev_warn(dev->net, "Error writing MII_ADDR\n");
247 goto done;
248 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000249
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000250 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
Steve Glendinningb052e072012-11-30 05:55:52 +0000251 if (ret < 0) {
252 netdev_warn(dev->net, "Timed out writing MII reg %02X\n", idx);
253 goto done;
254 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000255
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000256done:
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000257 mutex_unlock(&dev->phy_mutex);
258}
259
Andre Edich368be1c2020-08-26 13:17:15 +0200260static int smsc95xx_mdio_read_nopm(struct usbnet *dev, int idx)
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000261{
Andre Edich05b35e72020-08-26 13:17:17 +0200262 struct smsc95xx_priv *pdata = dev->driver_priv;
Andre Edich368be1c2020-08-26 13:17:15 +0200263
Andre Edich05b35e72020-08-26 13:17:17 +0200264 return __smsc95xx_mdio_read(dev, pdata->phydev->mdio.addr, idx, 1);
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000265}
266
Andre Edich368be1c2020-08-26 13:17:15 +0200267static void smsc95xx_mdio_write_nopm(struct usbnet *dev, int idx, int regval)
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000268{
Andre Edich05b35e72020-08-26 13:17:17 +0200269 struct smsc95xx_priv *pdata = dev->driver_priv;
Andre Edich368be1c2020-08-26 13:17:15 +0200270
Andre Edich05b35e72020-08-26 13:17:17 +0200271 __smsc95xx_mdio_write(dev, pdata->phydev->mdio.addr, idx, regval, 1);
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000272}
273
Andre Edich05b35e72020-08-26 13:17:17 +0200274static int smsc95xx_mdiobus_read(struct mii_bus *bus, int phy_id, int idx)
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000275{
Andre Edich05b35e72020-08-26 13:17:17 +0200276 struct usbnet *dev = bus->priv;
277
278 return __smsc95xx_mdio_read(dev, phy_id, idx, 0);
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000279}
280
Andre Edich05b35e72020-08-26 13:17:17 +0200281static int smsc95xx_mdiobus_write(struct mii_bus *bus, int phy_id, int idx,
282 u16 regval)
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000283{
Andre Edich05b35e72020-08-26 13:17:17 +0200284 struct usbnet *dev = bus->priv;
285
286 __smsc95xx_mdio_write(dev, phy_id, idx, regval, 0);
287 return 0;
Steve Glendinninge5e3af82012-11-22 08:05:24 +0000288}
289
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000290static int __must_check smsc95xx_wait_eeprom(struct usbnet *dev)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000291{
292 unsigned long start_time = jiffies;
293 u32 val;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000294 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000295
296 do {
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000297 ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
Steve Glendinningb052e072012-11-30 05:55:52 +0000298 if (ret < 0) {
299 netdev_warn(dev->net, "Error reading E2P_CMD\n");
300 return ret;
301 }
302
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000303 if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_))
304 break;
305 udelay(40);
306 } while (!time_after(jiffies, start_time + HZ));
307
308 if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) {
Joe Perches60b86752010-02-17 10:30:23 +0000309 netdev_warn(dev->net, "EEPROM read operation timeout\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000310 return -EIO;
311 }
312
313 return 0;
314}
315
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000316static int __must_check smsc95xx_eeprom_confirm_not_busy(struct usbnet *dev)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000317{
318 unsigned long start_time = jiffies;
319 u32 val;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000320 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000321
322 do {
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000323 ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
Steve Glendinningb052e072012-11-30 05:55:52 +0000324 if (ret < 0) {
325 netdev_warn(dev->net, "Error reading E2P_CMD\n");
326 return ret;
327 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000328
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000329 if (!(val & E2P_CMD_BUSY_))
330 return 0;
331
332 udelay(40);
333 } while (!time_after(jiffies, start_time + HZ));
334
Joe Perches60b86752010-02-17 10:30:23 +0000335 netdev_warn(dev->net, "EEPROM is busy\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000336 return -EIO;
337}
338
339static int smsc95xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
340 u8 *data)
341{
342 u32 val;
343 int i, ret;
344
345 BUG_ON(!dev);
346 BUG_ON(!data);
347
348 ret = smsc95xx_eeprom_confirm_not_busy(dev);
349 if (ret)
350 return ret;
351
352 for (i = 0; i < length; i++) {
353 val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000354 ret = smsc95xx_write_reg(dev, E2P_CMD, val);
Steve Glendinningb052e072012-11-30 05:55:52 +0000355 if (ret < 0) {
356 netdev_warn(dev->net, "Error writing E2P_CMD\n");
357 return ret;
358 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000359
360 ret = smsc95xx_wait_eeprom(dev);
361 if (ret < 0)
362 return ret;
363
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000364 ret = smsc95xx_read_reg(dev, E2P_DATA, &val);
Steve Glendinningb052e072012-11-30 05:55:52 +0000365 if (ret < 0) {
366 netdev_warn(dev->net, "Error reading E2P_DATA\n");
367 return ret;
368 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000369
370 data[i] = val & 0xFF;
371 offset++;
372 }
373
374 return 0;
375}
376
377static int smsc95xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
378 u8 *data)
379{
380 u32 val;
381 int i, ret;
382
383 BUG_ON(!dev);
384 BUG_ON(!data);
385
386 ret = smsc95xx_eeprom_confirm_not_busy(dev);
387 if (ret)
388 return ret;
389
390 /* Issue write/erase enable command */
391 val = E2P_CMD_BUSY_ | E2P_CMD_EWEN_;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000392 ret = smsc95xx_write_reg(dev, E2P_CMD, val);
Steve Glendinningb052e072012-11-30 05:55:52 +0000393 if (ret < 0) {
394 netdev_warn(dev->net, "Error writing E2P_DATA\n");
395 return ret;
396 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000397
398 ret = smsc95xx_wait_eeprom(dev);
399 if (ret < 0)
400 return ret;
401
402 for (i = 0; i < length; i++) {
403
404 /* Fill data register */
405 val = data[i];
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000406 ret = smsc95xx_write_reg(dev, E2P_DATA, val);
Steve Glendinningb052e072012-11-30 05:55:52 +0000407 if (ret < 0) {
408 netdev_warn(dev->net, "Error writing E2P_DATA\n");
409 return ret;
410 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000411
412 /* Send "write" command */
413 val = E2P_CMD_BUSY_ | E2P_CMD_WRITE_ | (offset & E2P_CMD_ADDR_);
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000414 ret = smsc95xx_write_reg(dev, E2P_CMD, val);
Steve Glendinningb052e072012-11-30 05:55:52 +0000415 if (ret < 0) {
416 netdev_warn(dev->net, "Error writing E2P_CMD\n");
417 return ret;
418 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000419
420 ret = smsc95xx_wait_eeprom(dev);
421 if (ret < 0)
422 return ret;
423
424 offset++;
425 }
426
427 return 0;
428}
429
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000430static int __must_check smsc95xx_write_reg_async(struct usbnet *dev, u16 index,
Steve Glendinning7b9e7582012-12-10 01:03:08 +0000431 u32 data)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000432{
Steve Glendinning1d74a6b2008-10-09 14:34:47 -0700433 const u16 size = 4;
Steve Glendinning7b9e7582012-12-10 01:03:08 +0000434 u32 buf;
Ming Lei72108fd2012-10-24 19:47:04 +0000435 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000436
Steve Glendinning7b9e7582012-12-10 01:03:08 +0000437 buf = data;
438 cpu_to_le32s(&buf);
439
Ming Lei72108fd2012-10-24 19:47:04 +0000440 ret = usbnet_write_cmd_async(dev, USB_VENDOR_REQUEST_WRITE_REGISTER,
441 USB_DIR_OUT | USB_TYPE_VENDOR |
442 USB_RECIP_DEVICE,
Steve Glendinning7b9e7582012-12-10 01:03:08 +0000443 0, index, &buf, size);
Ming Lei72108fd2012-10-24 19:47:04 +0000444 if (ret < 0)
445 netdev_warn(dev->net, "Error write async cmd, sts=%d\n",
446 ret);
447 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000448}
449
450/* returns hash bit number for given MAC address
451 * example:
452 * 01 00 5E 00 00 01 -> returns bit number 31 */
453static unsigned int smsc95xx_hash(char addr[ETH_ALEN])
454{
455 return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
456}
457
458static void smsc95xx_set_multicast(struct net_device *netdev)
459{
460 struct usbnet *dev = netdev_priv(netdev);
Andre Edichad90a732020-08-26 13:17:16 +0200461 struct smsc95xx_priv *pdata = dev->driver_priv;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000462 unsigned long flags;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000463 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000464
Marc Zyngier3c0f3c62011-03-18 03:53:58 +0000465 pdata->hash_hi = 0;
466 pdata->hash_lo = 0;
467
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000468 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
469
470 if (dev->net->flags & IFF_PROMISC) {
Joe Perchesa475f602010-02-17 10:30:24 +0000471 netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000472 pdata->mac_cr |= MAC_CR_PRMS_;
473 pdata->mac_cr &= ~(MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
474 } else if (dev->net->flags & IFF_ALLMULTI) {
Joe Perchesa475f602010-02-17 10:30:24 +0000475 netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000476 pdata->mac_cr |= MAC_CR_MCPAS_;
477 pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_HPFILT_);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000478 } else if (!netdev_mc_empty(dev->net)) {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000479 struct netdev_hw_addr *ha;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000480
481 pdata->mac_cr |= MAC_CR_HPFILT_;
482 pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);
483
Jiri Pirko22bedad32010-04-01 21:22:57 +0000484 netdev_for_each_mc_addr(ha, netdev) {
485 u32 bitnum = smsc95xx_hash(ha->addr);
Jiri Pirkoa92635d2010-02-18 04:02:26 +0000486 u32 mask = 0x01 << (bitnum & 0x1F);
487 if (bitnum & 0x20)
Marc Zyngier3c0f3c62011-03-18 03:53:58 +0000488 pdata->hash_hi |= mask;
Jiri Pirkoa92635d2010-02-18 04:02:26 +0000489 else
Marc Zyngier3c0f3c62011-03-18 03:53:58 +0000490 pdata->hash_lo |= mask;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000491 }
492
Joe Perchesa475f602010-02-17 10:30:24 +0000493 netif_dbg(dev, drv, dev->net, "HASHH=0x%08X, HASHL=0x%08X\n",
Marc Zyngier3c0f3c62011-03-18 03:53:58 +0000494 pdata->hash_hi, pdata->hash_lo);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000495 } else {
Joe Perchesa475f602010-02-17 10:30:24 +0000496 netif_dbg(dev, drv, dev->net, "receive own packets only\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000497 pdata->mac_cr &=
498 ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
499 }
500
501 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
502
503 /* Initiate async writes, as we can't wait for completion here */
Steve Glendinning7b9e7582012-12-10 01:03:08 +0000504 ret = smsc95xx_write_reg_async(dev, HASHH, pdata->hash_hi);
Steve Glendinningb052e072012-11-30 05:55:52 +0000505 if (ret < 0)
506 netdev_warn(dev->net, "failed to initiate async write to HASHH\n");
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000507
Steve Glendinning7b9e7582012-12-10 01:03:08 +0000508 ret = smsc95xx_write_reg_async(dev, HASHL, pdata->hash_lo);
Steve Glendinningb052e072012-11-30 05:55:52 +0000509 if (ret < 0)
510 netdev_warn(dev->net, "failed to initiate async write to HASHL\n");
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000511
Steve Glendinning7b9e7582012-12-10 01:03:08 +0000512 ret = smsc95xx_write_reg_async(dev, MAC_CR, pdata->mac_cr);
Steve Glendinningb052e072012-11-30 05:55:52 +0000513 if (ret < 0)
514 netdev_warn(dev->net, "failed to initiate async write to MAC_CR\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000515}
516
Andre Edich05b35e72020-08-26 13:17:17 +0200517static int smsc95xx_phy_update_flowcontrol(struct usbnet *dev)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000518{
Nisar Sayed9c082732017-09-11 17:43:11 +0000519 u32 flow = 0, afc_cfg;
Andre Edich05b35e72020-08-26 13:17:17 +0200520 struct smsc95xx_priv *pdata = dev->driver_priv;
521 bool tx_pause, rx_pause;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000522
523 int ret = smsc95xx_read_reg(dev, AFC_CFG, &afc_cfg);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000524 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000525 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000526
Andre Edich05b35e72020-08-26 13:17:17 +0200527 if (pdata->phydev->duplex == DUPLEX_FULL) {
528 phy_get_pause(pdata->phydev, &tx_pause, &rx_pause);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000529
Andre Edich05b35e72020-08-26 13:17:17 +0200530 if (rx_pause)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000531 flow = 0xFFFF0002;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000532
Andre Edich05b35e72020-08-26 13:17:17 +0200533 if (tx_pause) {
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000534 afc_cfg |= 0xF;
Nisar Sayed9c082732017-09-11 17:43:11 +0000535 flow |= 0xFFFF0000;
536 } else {
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000537 afc_cfg &= ~0xF;
Nisar Sayed9c082732017-09-11 17:43:11 +0000538 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000539
Joe Perchesa475f602010-02-17 10:30:24 +0000540 netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
Andre Edich05b35e72020-08-26 13:17:17 +0200541 rx_pause ? "enabled" : "disabled",
542 tx_pause ? "enabled" : "disabled");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000543 } else {
Joe Perchesa475f602010-02-17 10:30:24 +0000544 netif_dbg(dev, link, dev->net, "half duplex\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000545 afc_cfg |= 0xF;
546 }
547
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000548 ret = smsc95xx_write_reg(dev, FLOW, flow);
Steve Glendinningb052e072012-11-30 05:55:52 +0000549 if (ret < 0)
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000550 return ret;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000551
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000552 return smsc95xx_write_reg(dev, AFC_CFG, afc_cfg);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000553}
554
555static int smsc95xx_link_reset(struct usbnet *dev)
556{
Andre Edichad90a732020-08-26 13:17:16 +0200557 struct smsc95xx_priv *pdata = dev->driver_priv;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000558 unsigned long flags;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000559 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000560
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000561 ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000562 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000563 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000564
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000565 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
Andre Edich05b35e72020-08-26 13:17:17 +0200566 if (pdata->phydev->duplex != DUPLEX_FULL) {
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000567 pdata->mac_cr &= ~MAC_CR_FDPX_;
568 pdata->mac_cr |= MAC_CR_RCVOWN_;
569 } else {
570 pdata->mac_cr &= ~MAC_CR_RCVOWN_;
571 pdata->mac_cr |= MAC_CR_FDPX_;
572 }
573 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
574
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000575 ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000576 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000577 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000578
Andre Edich05b35e72020-08-26 13:17:17 +0200579 ret = smsc95xx_phy_update_flowcontrol(dev);
Steve Glendinningb052e072012-11-30 05:55:52 +0000580 if (ret < 0)
581 netdev_warn(dev->net, "Error updating PHY flow control\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000582
Steve Glendinningb052e072012-11-30 05:55:52 +0000583 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000584}
585
586static void smsc95xx_status(struct usbnet *dev, struct urb *urb)
587{
588 u32 intdata;
589
590 if (urb->actual_length != 4) {
Joe Perches60b86752010-02-17 10:30:23 +0000591 netdev_warn(dev->net, "unexpected urb length %d\n",
592 urb->actual_length);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000593 return;
594 }
595
Ben Dooks6809d212018-11-14 11:50:21 +0000596 intdata = get_unaligned_le32(urb->transfer_buffer);
Joe Perchesa475f602010-02-17 10:30:24 +0000597 netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000598
599 if (intdata & INT_ENP_PHY_INT_)
600 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
601 else
Joe Perches60b86752010-02-17 10:30:23 +0000602 netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
603 intdata);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000604}
605
Steve Glendinningf7b29272008-11-20 04:19:21 -0800606/* Enable or disable Tx & Rx checksum offload engines */
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000607static int smsc95xx_set_features(struct net_device *netdev,
608 netdev_features_t features)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000609{
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700610 struct usbnet *dev = netdev_priv(netdev);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000611 u32 read_buf;
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700612 int ret;
613
614 ret = smsc95xx_read_reg(dev, COE_CR, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000615 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000616 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000617
Nisar Sayedfe0cd8c2017-05-19 14:00:25 +0000618 if (features & NETIF_F_IP_CSUM)
Steve Glendinningf7b29272008-11-20 04:19:21 -0800619 read_buf |= Tx_COE_EN_;
620 else
621 read_buf &= ~Tx_COE_EN_;
622
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700623 if (features & NETIF_F_RXCSUM)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000624 read_buf |= Rx_COE_EN_;
625 else
626 read_buf &= ~Rx_COE_EN_;
627
628 ret = smsc95xx_write_reg(dev, COE_CR, read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000629 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000630 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000631
Joe Perchesa475f602010-02-17 10:30:24 +0000632 netif_dbg(dev, hw, dev->net, "COE_CR = 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000633 return 0;
634}
635
636static int smsc95xx_ethtool_get_eeprom_len(struct net_device *net)
637{
638 return MAX_EEPROM_SIZE;
639}
640
641static int smsc95xx_ethtool_get_eeprom(struct net_device *netdev,
642 struct ethtool_eeprom *ee, u8 *data)
643{
644 struct usbnet *dev = netdev_priv(netdev);
645
646 ee->magic = LAN95XX_EEPROM_MAGIC;
647
648 return smsc95xx_read_eeprom(dev, ee->offset, ee->len, data);
649}
650
651static int smsc95xx_ethtool_set_eeprom(struct net_device *netdev,
652 struct ethtool_eeprom *ee, u8 *data)
653{
654 struct usbnet *dev = netdev_priv(netdev);
655
656 if (ee->magic != LAN95XX_EEPROM_MAGIC) {
Joe Perches60b86752010-02-17 10:30:23 +0000657 netdev_warn(dev->net, "EEPROM: magic value mismatch, magic = 0x%x\n",
658 ee->magic);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000659 return -EINVAL;
660 }
661
662 return smsc95xx_write_eeprom(dev, ee->offset, ee->len, data);
663}
664
Emeric Vigier9fa32e92012-07-09 17:44:45 -0400665static int smsc95xx_ethtool_getregslen(struct net_device *netdev)
666{
667 /* all smsc95xx registers */
Steve Glendinning96245312012-12-10 01:03:07 +0000668 return COE_CR - ID_REV + sizeof(u32);
Emeric Vigier9fa32e92012-07-09 17:44:45 -0400669}
670
671static void
672smsc95xx_ethtool_getregs(struct net_device *netdev, struct ethtool_regs *regs,
673 void *buf)
674{
675 struct usbnet *dev = netdev_priv(netdev);
Dan Carpenterd3484462012-07-10 20:32:51 +0000676 unsigned int i, j;
677 int retval;
Emeric Vigier9fa32e92012-07-09 17:44:45 -0400678 u32 *data = buf;
679
680 retval = smsc95xx_read_reg(dev, ID_REV, &regs->version);
681 if (retval < 0) {
682 netdev_warn(netdev, "REGS: cannot read ID_REV\n");
683 return;
684 }
685
686 for (i = ID_REV, j = 0; i <= COE_CR; i += (sizeof(u32)), j++) {
687 retval = smsc95xx_read_reg(dev, i, &data[j]);
688 if (retval < 0) {
689 netdev_warn(netdev, "REGS: cannot read reg[%x]\n", i);
690 return;
691 }
692 }
693}
694
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000695static void smsc95xx_ethtool_get_wol(struct net_device *net,
696 struct ethtool_wolinfo *wolinfo)
697{
698 struct usbnet *dev = netdev_priv(net);
Andre Edichad90a732020-08-26 13:17:16 +0200699 struct smsc95xx_priv *pdata = dev->driver_priv;
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000700
701 wolinfo->supported = SUPPORTED_WAKE;
702 wolinfo->wolopts = pdata->wolopts;
703}
704
705static int smsc95xx_ethtool_set_wol(struct net_device *net,
706 struct ethtool_wolinfo *wolinfo)
707{
708 struct usbnet *dev = netdev_priv(net);
Andre Edichad90a732020-08-26 13:17:16 +0200709 struct smsc95xx_priv *pdata = dev->driver_priv;
Steve Glendinning3b146922012-11-30 05:55:50 +0000710 int ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000711
Florian Fainellic530c472018-09-28 16:18:56 -0700712 if (wolinfo->wolopts & ~SUPPORTED_WAKE)
713 return -EINVAL;
714
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000715 pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
Steve Glendinning3b146922012-11-30 05:55:50 +0000716
717 ret = device_set_wakeup_enable(&dev->udev->dev, pdata->wolopts);
Steve Glendinningb052e072012-11-30 05:55:52 +0000718 if (ret < 0)
719 netdev_warn(dev->net, "device_set_wakeup_enable error %d\n", ret);
Steve Glendinning3b146922012-11-30 05:55:50 +0000720
Steve Glendinningb052e072012-11-30 05:55:52 +0000721 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000722}
723
Andre Edich05b35e72020-08-26 13:17:17 +0200724static u32 smsc95xx_get_link(struct net_device *net)
Woojung Huh13722bb2016-09-02 20:34:22 +0000725{
Andre Edich05b35e72020-08-26 13:17:17 +0200726 phy_read_status(net->phydev);
727 return net->phydev->link;
Woojung Huh13722bb2016-09-02 20:34:22 +0000728}
729
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700730static const struct ethtool_ops smsc95xx_ethtool_ops = {
Andre Edich05b35e72020-08-26 13:17:17 +0200731 .get_link = smsc95xx_get_link,
732 .nway_reset = phy_ethtool_nway_reset,
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000733 .get_drvinfo = usbnet_get_drvinfo,
734 .get_msglevel = usbnet_get_msglevel,
735 .set_msglevel = usbnet_set_msglevel,
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000736 .get_eeprom_len = smsc95xx_ethtool_get_eeprom_len,
737 .get_eeprom = smsc95xx_ethtool_get_eeprom,
738 .set_eeprom = smsc95xx_ethtool_set_eeprom,
Emeric Vigier9fa32e92012-07-09 17:44:45 -0400739 .get_regs_len = smsc95xx_ethtool_getregslen,
740 .get_regs = smsc95xx_ethtool_getregs,
Steve Glendinninge0e474a2012-09-28 00:07:12 +0000741 .get_wol = smsc95xx_ethtool_get_wol,
742 .set_wol = smsc95xx_ethtool_set_wol,
Andre Edich05b35e72020-08-26 13:17:17 +0200743 .get_link_ksettings = phy_ethtool_get_link_ksettings,
744 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Petr Kulhavya8f5cb92017-07-13 19:40:57 +0200745 .get_ts_info = ethtool_op_get_ts_info,
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000746};
747
748static int smsc95xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
749{
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000750 if (!netif_running(netdev))
751 return -EINVAL;
752
Andre Edich05b35e72020-08-26 13:17:17 +0200753 return phy_mii_ioctl(netdev->phydev, rq, cmd);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000754}
755
756static void smsc95xx_init_mac_address(struct usbnet *dev)
757{
Arnd Bergmannc4895652016-04-29 09:05:59 +0200758 /* maybe the boot loader passed the MAC address in devicetree */
Łukasz Stelmach4f359b62020-09-30 16:25:25 +0200759 if (!eth_platform_get_mac_address(&dev->udev->dev,
760 dev->net->dev_addr)) {
761 if (is_valid_ether_addr(dev->net->dev_addr)) {
762 /* device tree values are valid so use them */
763 netif_dbg(dev, ifup, dev->net, "MAC address read from the device tree\n");
764 return;
765 }
Arnd Bergmannc4895652016-04-29 09:05:59 +0200766 }
767
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000768 /* try reading mac address from EEPROM */
769 if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
770 dev->net->dev_addr) == 0) {
771 if (is_valid_ether_addr(dev->net->dev_addr)) {
772 /* eeprom values are valid so use them */
Joe Perchesa475f602010-02-17 10:30:24 +0000773 netif_dbg(dev, ifup, dev->net, "MAC address read from EEPROM\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000774 return;
775 }
776 }
777
Arnd Bergmannc4895652016-04-29 09:05:59 +0200778 /* no useful static MAC address found. generate a random one */
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000779 eth_hw_addr_random(dev->net);
Joe Perchesc7e12ea2012-07-12 19:33:07 +0000780 netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000781}
782
783static int smsc95xx_set_mac_address(struct usbnet *dev)
784{
785 u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
786 dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
787 u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
788 int ret;
789
790 ret = smsc95xx_write_reg(dev, ADDRL, addr_lo);
Steve Glendinningb052e072012-11-30 05:55:52 +0000791 if (ret < 0)
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000792 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000793
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000794 return smsc95xx_write_reg(dev, ADDRH, addr_hi);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000795}
796
797/* starts the TX path */
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000798static int smsc95xx_start_tx_path(struct usbnet *dev)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000799{
Andre Edichad90a732020-08-26 13:17:16 +0200800 struct smsc95xx_priv *pdata = dev->driver_priv;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000801 unsigned long flags;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000802 int ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000803
804 /* Enable Tx at MAC */
805 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
806 pdata->mac_cr |= MAC_CR_TXEN_;
807 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
808
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000809 ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000810 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000811 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000812
813 /* Enable Tx at SCSRs */
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000814 return smsc95xx_write_reg(dev, TX_CFG, TX_CFG_ON_);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000815}
816
817/* Starts the Receive path */
Ming Leiec321152012-11-06 04:53:07 +0000818static int smsc95xx_start_rx_path(struct usbnet *dev, int in_pm)
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000819{
Andre Edichad90a732020-08-26 13:17:16 +0200820 struct smsc95xx_priv *pdata = dev->driver_priv;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000821 unsigned long flags;
822
823 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
824 pdata->mac_cr |= MAC_CR_RXEN_;
825 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
826
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000827 return __smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr, in_pm);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000828}
829
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000830static int smsc95xx_reset(struct usbnet *dev)
831{
Andre Edichad90a732020-08-26 13:17:16 +0200832 struct smsc95xx_priv *pdata = dev->driver_priv;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000833 u32 read_buf, write_buf, burst_cap;
834 int ret = 0, timeout;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000835
Joe Perchesa475f602010-02-17 10:30:24 +0000836 netif_dbg(dev, ifup, dev->net, "entering smsc95xx_reset\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000837
Steve Glendinning44367612012-09-28 00:07:08 +0000838 ret = smsc95xx_write_reg(dev, HW_CFG, HW_CFG_LRST_);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000839 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000840 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000841
842 timeout = 0;
843 do {
Steve Glendinningcf2acec2012-09-28 00:07:07 +0000844 msleep(10);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000845 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000846 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000847 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000848 timeout++;
849 } while ((read_buf & HW_CFG_LRST_) && (timeout < 100));
850
851 if (timeout >= 100) {
Joe Perches60b86752010-02-17 10:30:23 +0000852 netdev_warn(dev->net, "timeout waiting for completion of Lite Reset\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000853 return ret;
854 }
855
Steve Glendinning44367612012-09-28 00:07:08 +0000856 ret = smsc95xx_write_reg(dev, PM_CTRL, PM_CTL_PHY_RST_);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000857 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000858 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000859
860 timeout = 0;
861 do {
Steve Glendinningcf2acec2012-09-28 00:07:07 +0000862 msleep(10);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000863 ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000864 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000865 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000866 timeout++;
867 } while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));
868
869 if (timeout >= 100) {
Joe Perches60b86752010-02-17 10:30:23 +0000870 netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000871 return ret;
872 }
873
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000874 ret = smsc95xx_set_mac_address(dev);
875 if (ret < 0)
876 return ret;
877
Joe Perches1e1d7412012-11-24 01:27:49 +0000878 netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n",
879 dev->net->dev_addr);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000880
881 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000882 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000883 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000884
Joe Perches1e1d7412012-11-24 01:27:49 +0000885 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n",
886 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000887
888 read_buf |= HW_CFG_BIR_;
889
890 ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000891 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000892 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000893
894 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000895 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000896 return ret;
Steve Glendinningb052e072012-11-30 05:55:52 +0000897
Joe Perchesa475f602010-02-17 10:30:24 +0000898 netif_dbg(dev, ifup, dev->net,
899 "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
900 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000901
902 if (!turbo_mode) {
903 burst_cap = 0;
904 dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
905 } else if (dev->udev->speed == USB_SPEED_HIGH) {
906 burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
907 dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
908 } else {
909 burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
910 dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
911 }
912
Joe Perches1e1d7412012-11-24 01:27:49 +0000913 netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n",
914 (ulong)dev->rx_urb_size);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000915
916 ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000917 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000918 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000919
920 ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000921 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000922 return ret;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000923
Joe Perchesa475f602010-02-17 10:30:24 +0000924 netif_dbg(dev, ifup, dev->net,
925 "Read Value from BURST_CAP after writing: 0x%08x\n",
926 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000927
Steve Glendinning44367612012-09-28 00:07:08 +0000928 ret = smsc95xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000929 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000930 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000931
932 ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000933 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000934 return ret;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000935
Joe Perchesa475f602010-02-17 10:30:24 +0000936 netif_dbg(dev, ifup, dev->net,
937 "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
938 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000939
940 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000941 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000942 return ret;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000943
Joe Perches1e1d7412012-11-24 01:27:49 +0000944 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG: 0x%08x\n",
945 read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000946
947 if (turbo_mode)
948 read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);
949
950 read_buf &= ~HW_CFG_RXDOFF_;
951
952 /* set Rx data offset=2, Make IP header aligns on word boundary. */
953 read_buf |= NET_IP_ALIGN << 9;
954
955 ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000956 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000957 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000958
959 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000960 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000961 return ret;
Steve Glendinning769ea6d2012-09-28 00:07:09 +0000962
Joe Perchesa475f602010-02-17 10:30:24 +0000963 netif_dbg(dev, ifup, dev->net,
964 "Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000965
Steve Glendinning44367612012-09-28 00:07:08 +0000966 ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000967 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000968 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000969
970 ret = smsc95xx_read_reg(dev, ID_REV, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000971 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000972 return ret;
Joe Perchesa475f602010-02-17 10:30:24 +0000973 netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", read_buf);
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000974
Steve Glendinningf2935012009-05-01 05:46:51 +0000975 /* Configure GPIO pins as LED outputs */
976 write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED |
977 LED_GPIO_CFG_FDX_LED;
978 ret = smsc95xx_write_reg(dev, LED_GPIO_CFG, write_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000979 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000980 return ret;
Steve Glendinningf2935012009-05-01 05:46:51 +0000981
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000982 /* Init Tx */
Steve Glendinning44367612012-09-28 00:07:08 +0000983 ret = smsc95xx_write_reg(dev, FLOW, 0);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000984 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000985 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000986
Steve Glendinning44367612012-09-28 00:07:08 +0000987 ret = smsc95xx_write_reg(dev, AFC_CFG, AFC_CFG_DEFAULT);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000988 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000989 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000990
991 /* Don't need mac_cr_lock during initialisation */
992 ret = smsc95xx_read_reg(dev, MAC_CR, &pdata->mac_cr);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000993 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +0000994 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +0000995
996 /* Init Rx */
997 /* Set Vlan */
Steve Glendinning44367612012-09-28 00:07:08 +0000998 ret = smsc95xx_write_reg(dev, VLAN1, (u32)ETH_P_8021Q);
Steve Glendinninge360a8b2013-01-03 03:00:15 +0000999 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001000 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001001
Steve Glendinningf7b29272008-11-20 04:19:21 -08001002 /* Enable or disable checksum offload engines */
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001003 ret = smsc95xx_set_features(dev->net, dev->net->features);
Steve Glendinningb052e072012-11-30 05:55:52 +00001004 if (ret < 0) {
1005 netdev_warn(dev->net, "Failed to set checksum offload features\n");
1006 return ret;
1007 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001008
1009 smsc95xx_set_multicast(dev->net);
1010
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001011 ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001012 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001013 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001014
1015 /* enable PHY interrupts */
1016 read_buf |= INT_EP_CTL_PHY_INT_;
1017
1018 ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001019 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001020 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001021
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001022 ret = smsc95xx_start_tx_path(dev);
Steve Glendinningb052e072012-11-30 05:55:52 +00001023 if (ret < 0) {
1024 netdev_warn(dev->net, "Failed to start TX path\n");
1025 return ret;
1026 }
Steve Glendinning769ea6d2012-09-28 00:07:09 +00001027
Ming Leiec321152012-11-06 04:53:07 +00001028 ret = smsc95xx_start_rx_path(dev, 0);
Steve Glendinningb052e072012-11-30 05:55:52 +00001029 if (ret < 0) {
1030 netdev_warn(dev->net, "Failed to start RX path\n");
1031 return ret;
1032 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001033
Joe Perchesa475f602010-02-17 10:30:24 +00001034 netif_dbg(dev, ifup, dev->net, "smsc95xx_reset, return 0\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001035 return 0;
1036}
1037
Stephen Hemminger63e77b32009-03-20 19:35:58 +00001038static const struct net_device_ops smsc95xx_netdev_ops = {
1039 .ndo_open = usbnet_open,
1040 .ndo_stop = usbnet_stop,
1041 .ndo_start_xmit = usbnet_start_xmit,
1042 .ndo_tx_timeout = usbnet_tx_timeout,
1043 .ndo_change_mtu = usbnet_change_mtu,
Heiner Kallweit323955a2020-11-10 20:51:03 +01001044 .ndo_get_stats64 = dev_get_tstats64,
Stephen Hemminger63e77b32009-03-20 19:35:58 +00001045 .ndo_set_mac_address = eth_mac_addr,
1046 .ndo_validate_addr = eth_validate_addr,
Arnd Bergmanna7605372021-07-27 15:45:13 +02001047 .ndo_eth_ioctl = smsc95xx_ioctl,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001048 .ndo_set_rx_mode = smsc95xx_set_multicast,
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001049 .ndo_set_features = smsc95xx_set_features,
Stephen Hemminger63e77b32009-03-20 19:35:58 +00001050};
1051
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001052static int smsc95xx_bind(struct usbnet *dev, struct usb_interface *intf)
1053{
Andre Edichad90a732020-08-26 13:17:16 +02001054 struct smsc95xx_priv *pdata;
Andre Edich05b35e72020-08-26 13:17:17 +02001055 bool is_internal_phy;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001056 u32 val;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001057 int ret;
1058
1059 printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
1060
1061 ret = usbnet_get_endpoints(dev, intf);
Steve Glendinningb052e072012-11-30 05:55:52 +00001062 if (ret < 0) {
1063 netdev_warn(dev->net, "usbnet_get_endpoints failed: %d\n", ret);
1064 return ret;
1065 }
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001066
Andre Edichad90a732020-08-26 13:17:16 +02001067 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
Joe Perches38673c82013-02-03 17:28:11 +00001068 if (!pdata)
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001069 return -ENOMEM;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001070
Andre Edichad90a732020-08-26 13:17:16 +02001071 dev->driver_priv = pdata;
1072
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001073 spin_lock_init(&pdata->mac_cr_lock);
1074
Nisar Sayedfe0cd8c2017-05-19 14:00:25 +00001075 /* LAN95xx devices do not alter the computed checksum of 0 to 0xffff.
1076 * RFC 2460, ipv6 UDP calculated checksum yields a result of zero must
1077 * be changed to 0xffff. RFC 768, ipv4 UDP computed checksum is zero,
1078 * it is transmitted as all ones. The zero transmitted checksum means
1079 * transmitter generated no checksum. Hence, enable csum offload only
1080 * for ipv4 packets.
1081 */
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001082 if (DEFAULT_TX_CSUM_ENABLE)
Nisar Sayedfe0cd8c2017-05-19 14:00:25 +00001083 dev->net->features |= NETIF_F_IP_CSUM;
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001084 if (DEFAULT_RX_CSUM_ENABLE)
1085 dev->net->features |= NETIF_F_RXCSUM;
1086
Nisar Sayedfe0cd8c2017-05-19 14:00:25 +00001087 dev->net->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
Ben Dooks810eeb12018-11-14 11:50:19 +00001088 set_bit(EVENT_NO_IP_ALIGN, &dev->flags);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001089
Bernard Blackhamf4e8ab72010-10-18 13:16:39 +00001090 smsc95xx_init_mac_address(dev);
1091
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001092 /* Init all registers */
1093 ret = smsc95xx_reset(dev);
Andre Edich7c8b1e82020-07-06 10:39:34 +02001094 if (ret)
1095 goto free_pdata;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001096
Andre Edich05b35e72020-08-26 13:17:17 +02001097 pdata->mdiobus = mdiobus_alloc();
1098 if (!pdata->mdiobus) {
1099 ret = -ENOMEM;
1100 goto free_pdata;
1101 }
1102
1103 ret = smsc95xx_read_reg(dev, HW_CFG, &val);
1104 if (ret < 0)
1105 goto free_mdio;
1106
1107 is_internal_phy = !(val & HW_CFG_PSEL_);
1108 if (is_internal_phy)
1109 pdata->mdiobus->phy_mask = ~(1u << SMSC95XX_INTERNAL_PHY_ID);
1110
1111 pdata->mdiobus->priv = dev;
1112 pdata->mdiobus->read = smsc95xx_mdiobus_read;
1113 pdata->mdiobus->write = smsc95xx_mdiobus_write;
1114 pdata->mdiobus->name = "smsc95xx-mdiobus";
1115 pdata->mdiobus->parent = &dev->udev->dev;
1116
1117 snprintf(pdata->mdiobus->id, ARRAY_SIZE(pdata->mdiobus->id),
1118 "usb-%03d:%03d", dev->udev->bus->busnum, dev->udev->devnum);
1119
1120 ret = mdiobus_register(pdata->mdiobus);
1121 if (ret) {
1122 netdev_err(dev->net, "Could not register MDIO bus\n");
1123 goto free_mdio;
1124 }
1125
1126 pdata->phydev = phy_find_first(pdata->mdiobus);
1127 if (!pdata->phydev) {
1128 netdev_err(dev->net, "no PHY found\n");
1129 ret = -ENODEV;
1130 goto unregister_mdio;
1131 }
1132
1133 pdata->phydev->is_internal = is_internal_phy;
1134
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001135 /* detect device revision as different features may be available */
1136 ret = smsc95xx_read_reg(dev, ID_REV, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001137 if (ret < 0)
Andre Edich05b35e72020-08-26 13:17:17 +02001138 goto unregister_mdio;
Andre Edich3ed58f92020-07-06 10:39:35 +02001139
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001140 val >>= 16;
Steve Glendinning9ebca502012-11-22 08:05:23 +00001141 if ((val == ID_REV_CHIP_ID_9500A_) || (val == ID_REV_CHIP_ID_9530_) ||
1142 (val == ID_REV_CHIP_ID_89530_) || (val == ID_REV_CHIP_ID_9730_))
1143 pdata->features = (FEATURE_8_WAKEUP_FILTERS |
1144 FEATURE_PHY_NLP_CROSSOVER |
Ming Leieb970ff2013-02-22 03:05:05 +00001145 FEATURE_REMOTE_WAKEUP);
Steve Glendinning9ebca502012-11-22 08:05:23 +00001146 else if (val == ID_REV_CHIP_ID_9512_)
1147 pdata->features = FEATURE_8_WAKEUP_FILTERS;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001148
Stephen Hemminger63e77b32009-03-20 19:35:58 +00001149 dev->net->netdev_ops = &smsc95xx_netdev_ops;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001150 dev->net->ethtool_ops = &smsc95xx_ethtool_ops;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001151 dev->net->flags |= IFF_MULTICAST;
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001152 dev->net->hard_header_len += SMSC95XX_TX_OVERHEAD_CSUM;
Stefan Wahren85b18b02018-11-08 20:38:26 +01001153 dev->net->min_mtu = ETH_MIN_MTU;
1154 dev->net->max_mtu = ETH_DATA_LEN;
Stephane Fillod9bbf5662012-04-20 09:39:23 +00001155 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001156 return 0;
Andre Edich7c8b1e82020-07-06 10:39:34 +02001157
Andre Edich05b35e72020-08-26 13:17:17 +02001158unregister_mdio:
1159 mdiobus_unregister(pdata->mdiobus);
1160
1161free_mdio:
1162 mdiobus_free(pdata->mdiobus);
1163
Andre Edich7c8b1e82020-07-06 10:39:34 +02001164free_pdata:
1165 kfree(pdata);
1166 return ret;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001167}
1168
1169static void smsc95xx_unbind(struct usbnet *dev, struct usb_interface *intf)
1170{
Andre Edichad90a732020-08-26 13:17:16 +02001171 struct smsc95xx_priv *pdata = dev->driver_priv;
Christoph Fritzd69d1692016-05-26 04:06:47 +02001172
Andre Edich05b35e72020-08-26 13:17:17 +02001173 mdiobus_unregister(pdata->mdiobus);
1174 mdiobus_free(pdata->mdiobus);
Andre Edichad90a732020-08-26 13:17:16 +02001175 netif_dbg(dev, ifdown, dev->net, "free pdata\n");
1176 kfree(pdata);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001177}
1178
Andre Edich05b35e72020-08-26 13:17:17 +02001179static void smsc95xx_handle_link_change(struct net_device *net)
1180{
Aaro Koskinen5ab8a442021-09-24 01:00:16 +03001181 struct usbnet *dev = netdev_priv(net);
1182
Andre Edich05b35e72020-08-26 13:17:17 +02001183 phy_print_status(net->phydev);
Aaro Koskinen5ab8a442021-09-24 01:00:16 +03001184 usbnet_defer_kevent(dev, EVENT_LINK_CHANGE);
Andre Edich05b35e72020-08-26 13:17:17 +02001185}
1186
1187static int smsc95xx_start_phy(struct usbnet *dev)
1188{
1189 struct smsc95xx_priv *pdata = dev->driver_priv;
1190 struct net_device *net = dev->net;
1191 int ret;
1192
1193 ret = smsc95xx_reset(dev);
1194 if (ret < 0)
1195 return ret;
1196
1197 ret = phy_connect_direct(net, pdata->phydev,
1198 &smsc95xx_handle_link_change,
1199 PHY_INTERFACE_MODE_MII);
1200 if (ret) {
1201 netdev_err(net, "can't attach PHY to %s\n", pdata->mdiobus->id);
1202 return ret;
1203 }
1204
1205 phy_attached_info(net->phydev);
1206 phy_start(net->phydev);
1207 return 0;
1208}
1209
1210static int smsc95xx_disconnect_phy(struct usbnet *dev)
1211{
1212 phy_stop(dev->net->phydev);
1213 phy_disconnect(dev->net->phydev);
1214 return 0;
1215}
1216
Steve Glendinning068bb1a2012-11-30 05:55:51 +00001217static u32 smsc_crc(const u8 *buffer, size_t len, int filter)
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001218{
Steve Glendinning068bb1a2012-11-30 05:55:51 +00001219 u32 crc = bitrev16(crc16(0xFFFF, buffer, len));
1220 return crc << ((filter % 2) * 16);
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001221}
1222
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001223static int smsc95xx_enable_phy_wakeup_interrupts(struct usbnet *dev, u16 mask)
1224{
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001225 int ret;
1226
Joe Perches1e1d7412012-11-24 01:27:49 +00001227 netdev_dbg(dev->net, "enabling PHY wakeup interrupts\n");
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001228
1229 /* read to clear */
Andre Edich368be1c2020-08-26 13:17:15 +02001230 ret = smsc95xx_mdio_read_nopm(dev, PHY_INT_SRC);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001231 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001232 return ret;
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001233
1234 /* enable interrupt source */
Andre Edich368be1c2020-08-26 13:17:15 +02001235 ret = smsc95xx_mdio_read_nopm(dev, PHY_INT_MASK);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001236 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001237 return ret;
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001238
1239 ret |= mask;
1240
Andre Edich368be1c2020-08-26 13:17:15 +02001241 smsc95xx_mdio_write_nopm(dev, PHY_INT_MASK, ret);
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001242
1243 return 0;
1244}
1245
1246static int smsc95xx_link_ok_nopm(struct usbnet *dev)
1247{
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001248 int ret;
1249
1250 /* first, a dummy read, needed to latch some MII phys */
Andre Edich368be1c2020-08-26 13:17:15 +02001251 ret = smsc95xx_mdio_read_nopm(dev, MII_BMSR);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001252 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001253 return ret;
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001254
Andre Edich368be1c2020-08-26 13:17:15 +02001255 ret = smsc95xx_mdio_read_nopm(dev, MII_BMSR);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001256 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001257 return ret;
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001258
1259 return !!(ret & BMSR_LSTATUS);
1260}
1261
Steve Glendinning319b95b2012-11-22 08:05:25 +00001262static int smsc95xx_enter_suspend0(struct usbnet *dev)
1263{
Andre Edichad90a732020-08-26 13:17:16 +02001264 struct smsc95xx_priv *pdata = dev->driver_priv;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001265 u32 val;
1266 int ret;
1267
1268 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001269 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001270 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001271
1272 val &= (~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_));
1273 val |= PM_CTL_SUS_MODE_0;
1274
1275 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001276 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001277 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001278
1279 /* clear wol status */
1280 val &= ~PM_CTL_WUPS_;
1281 val |= PM_CTL_WUPS_WOL_;
1282
1283 /* enable energy detection */
1284 if (pdata->wolopts & WAKE_PHY)
1285 val |= PM_CTL_WUPS_ED_;
1286
1287 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001288 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001289 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001290
1291 /* read back PM_CTRL */
1292 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Ming Lei76437212013-02-22 03:05:03 +00001293 if (ret < 0)
1294 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001295
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001296 pdata->suspend_flags |= SUSPEND_SUSPEND0;
1297
Ming Lei76437212013-02-22 03:05:03 +00001298 return 0;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001299}
1300
1301static int smsc95xx_enter_suspend1(struct usbnet *dev)
1302{
Andre Edichad90a732020-08-26 13:17:16 +02001303 struct smsc95xx_priv *pdata = dev->driver_priv;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001304 u32 val;
1305 int ret;
1306
1307 /* reconfigure link pulse detection timing for
1308 * compatibility with non-standard link partners
1309 */
1310 if (pdata->features & FEATURE_PHY_NLP_CROSSOVER)
Andre Edich368be1c2020-08-26 13:17:15 +02001311 smsc95xx_mdio_write_nopm(dev, PHY_EDPD_CONFIG,
1312 PHY_EDPD_CONFIG_DEFAULT);
Steve Glendinning319b95b2012-11-22 08:05:25 +00001313
1314 /* enable energy detect power-down mode */
Andre Edich368be1c2020-08-26 13:17:15 +02001315 ret = smsc95xx_mdio_read_nopm(dev, PHY_MODE_CTRL_STS);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001316 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001317 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001318
1319 ret |= MODE_CTRL_STS_EDPWRDOWN_;
1320
Andre Edich368be1c2020-08-26 13:17:15 +02001321 smsc95xx_mdio_write_nopm(dev, PHY_MODE_CTRL_STS, ret);
Steve Glendinning319b95b2012-11-22 08:05:25 +00001322
1323 /* enter SUSPEND1 mode */
1324 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001325 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001326 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001327
1328 val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1329 val |= PM_CTL_SUS_MODE_1;
1330
1331 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001332 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001333 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001334
1335 /* clear wol status, enable energy detection */
1336 val &= ~PM_CTL_WUPS_;
1337 val |= (PM_CTL_WUPS_ED_ | PM_CTL_ED_EN_);
1338
1339 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Ming Lei76437212013-02-22 03:05:03 +00001340 if (ret < 0)
1341 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001342
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001343 pdata->suspend_flags |= SUSPEND_SUSPEND1;
1344
Ming Lei76437212013-02-22 03:05:03 +00001345 return 0;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001346}
1347
1348static int smsc95xx_enter_suspend2(struct usbnet *dev)
1349{
Andre Edichad90a732020-08-26 13:17:16 +02001350 struct smsc95xx_priv *pdata = dev->driver_priv;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001351 u32 val;
1352 int ret;
1353
1354 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001355 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001356 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001357
1358 val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1359 val |= PM_CTL_SUS_MODE_2;
1360
1361 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Ming Lei76437212013-02-22 03:05:03 +00001362 if (ret < 0)
1363 return ret;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001364
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001365 pdata->suspend_flags |= SUSPEND_SUSPEND2;
1366
Ming Lei76437212013-02-22 03:05:03 +00001367 return 0;
Steve Glendinning319b95b2012-11-22 08:05:25 +00001368}
1369
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001370static int smsc95xx_enter_suspend3(struct usbnet *dev)
1371{
Andre Edichad90a732020-08-26 13:17:16 +02001372 struct smsc95xx_priv *pdata = dev->driver_priv;
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001373 u32 val;
1374 int ret;
1375
1376 ret = smsc95xx_read_reg_nopm(dev, RX_FIFO_INF, &val);
1377 if (ret < 0)
1378 return ret;
1379
Martin Wetterwald53a759c2017-04-13 10:08:44 +02001380 if (val & RX_FIFO_INF_USED_) {
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001381 netdev_info(dev->net, "rx fifo not empty in autosuspend\n");
1382 return -EBUSY;
1383 }
1384
1385 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1386 if (ret < 0)
1387 return ret;
1388
1389 val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1390 val |= PM_CTL_SUS_MODE_3 | PM_CTL_RES_CLR_WKP_STS;
1391
1392 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1393 if (ret < 0)
1394 return ret;
1395
1396 /* clear wol status */
1397 val &= ~PM_CTL_WUPS_;
1398 val |= PM_CTL_WUPS_WOL_;
1399
1400 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1401 if (ret < 0)
1402 return ret;
1403
1404 pdata->suspend_flags |= SUSPEND_SUSPEND3;
1405
1406 return 0;
1407}
1408
1409static int smsc95xx_autosuspend(struct usbnet *dev, u32 link_up)
1410{
Andre Edichad90a732020-08-26 13:17:16 +02001411 struct smsc95xx_priv *pdata = dev->driver_priv;
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001412 int ret;
1413
1414 if (!netif_running(dev->net)) {
1415 /* interface is ifconfig down so fully power down hw */
1416 netdev_dbg(dev->net, "autosuspend entering SUSPEND2\n");
1417 return smsc95xx_enter_suspend2(dev);
1418 }
1419
1420 if (!link_up) {
1421 /* link is down so enter EDPD mode, but only if device can
1422 * reliably resume from it. This check should be redundant
Ming Leieb970ff2013-02-22 03:05:05 +00001423 * as current FEATURE_REMOTE_WAKEUP parts also support
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001424 * FEATURE_PHY_NLP_CROSSOVER but it's included for clarity */
1425 if (!(pdata->features & FEATURE_PHY_NLP_CROSSOVER)) {
1426 netdev_warn(dev->net, "EDPD not supported\n");
1427 return -EBUSY;
1428 }
1429
1430 netdev_dbg(dev->net, "autosuspend entering SUSPEND1\n");
1431
1432 /* enable PHY wakeup events for if cable is attached */
1433 ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1434 PHY_INT_MASK_ANEG_COMP_);
1435 if (ret < 0) {
1436 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1437 return ret;
1438 }
1439
1440 netdev_info(dev->net, "entering SUSPEND1 mode\n");
1441 return smsc95xx_enter_suspend1(dev);
1442 }
1443
1444 /* enable PHY wakeup events so we remote wakeup if cable is pulled */
1445 ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1446 PHY_INT_MASK_LINK_DOWN_);
1447 if (ret < 0) {
1448 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1449 return ret;
1450 }
1451
1452 netdev_dbg(dev->net, "autosuspend entering SUSPEND3\n");
1453 return smsc95xx_enter_suspend3(dev);
1454}
1455
Steve Glendinningb5a04472012-09-28 00:07:11 +00001456static int smsc95xx_suspend(struct usb_interface *intf, pm_message_t message)
1457{
1458 struct usbnet *dev = usb_get_intfdata(intf);
Andre Edichad90a732020-08-26 13:17:16 +02001459 struct smsc95xx_priv *pdata = dev->driver_priv;
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001460 u32 val, link_up;
Steve Glendinningb5a04472012-09-28 00:07:11 +00001461 int ret;
Steve Glendinningb5a04472012-09-28 00:07:11 +00001462
Steve Glendinningb5a04472012-09-28 00:07:11 +00001463 ret = usbnet_suspend(intf, message);
Steve Glendinningb052e072012-11-30 05:55:52 +00001464 if (ret < 0) {
1465 netdev_warn(dev->net, "usbnet_suspend error\n");
1466 return ret;
1467 }
Steve Glendinningb5a04472012-09-28 00:07:11 +00001468
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001469 if (pdata->suspend_flags) {
1470 netdev_warn(dev->net, "error during last resume\n");
1471 pdata->suspend_flags = 0;
1472 }
1473
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001474 /* determine if link is up using only _nopm functions */
1475 link_up = smsc95xx_link_ok_nopm(dev);
1476
Ming Lei42e21c02013-02-22 03:05:04 +00001477 if (message.event == PM_EVENT_AUTO_SUSPEND &&
Ming Leieb970ff2013-02-22 03:05:05 +00001478 (pdata->features & FEATURE_REMOTE_WAKEUP)) {
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001479 ret = smsc95xx_autosuspend(dev, link_up);
1480 goto done;
1481 }
1482
1483 /* if we get this far we're not autosuspending */
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001484 /* if no wol options set, or if link is down and we're not waking on
1485 * PHY activity, enter lowest power SUSPEND2 mode
1486 */
1487 if (!(pdata->wolopts & SUPPORTED_WAKE) ||
1488 !(link_up || (pdata->wolopts & WAKE_PHY))) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001489 netdev_info(dev->net, "entering SUSPEND2 mode\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001490
1491 /* disable energy detect (link up) & wake up events */
Ming Leiec321152012-11-06 04:53:07 +00001492 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001493 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001494 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001495
1496 val &= ~(WUCSR_MPEN_ | WUCSR_WAKE_EN_);
1497
Ming Leiec321152012-11-06 04:53:07 +00001498 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001499 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001500 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001501
Ming Leiec321152012-11-06 04:53:07 +00001502 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001503 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001504 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001505
1506 val &= ~(PM_CTL_ED_EN_ | PM_CTL_WOL_EN_);
1507
Ming Leiec321152012-11-06 04:53:07 +00001508 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001509 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001510 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001511
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001512 ret = smsc95xx_enter_suspend2(dev);
1513 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001514 }
1515
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001516 if (pdata->wolopts & WAKE_PHY) {
1517 ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1518 (PHY_INT_MASK_ANEG_COMP_ | PHY_INT_MASK_LINK_DOWN_));
Steve Glendinningb052e072012-11-30 05:55:52 +00001519 if (ret < 0) {
1520 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1521 goto done;
1522 }
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001523
1524 /* if link is down then configure EDPD and enter SUSPEND1,
1525 * otherwise enter SUSPEND0 below
1526 */
1527 if (!link_up) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001528 netdev_info(dev->net, "entering SUSPEND1 mode\n");
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001529 ret = smsc95xx_enter_suspend1(dev);
1530 goto done;
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001531 }
1532 }
1533
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001534 if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
Kees Cook6396bb22018-06-12 14:03:40 -07001535 u32 *filter_mask = kcalloc(32, sizeof(u32), GFP_KERNEL);
Ming Lei06a221b2012-11-06 04:53:06 +00001536 u32 command[2];
1537 u32 offset[2];
1538 u32 crc[4];
Steve Glendinning9ebca502012-11-22 08:05:23 +00001539 int wuff_filter_count =
1540 (pdata->features & FEATURE_8_WAKEUP_FILTERS) ?
1541 LAN9500A_WUFF_NUM : LAN9500_WUFF_NUM;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001542 int i, filter = 0;
1543
Steve Glendinningeed9a722012-11-30 05:55:48 +00001544 if (!filter_mask) {
1545 netdev_warn(dev->net, "Unable to allocate filter_mask\n");
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001546 ret = -ENOMEM;
1547 goto done;
Steve Glendinningeed9a722012-11-30 05:55:48 +00001548 }
1549
Ming Lei06a221b2012-11-06 04:53:06 +00001550 memset(command, 0, sizeof(command));
1551 memset(offset, 0, sizeof(offset));
1552 memset(crc, 0, sizeof(crc));
1553
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001554 if (pdata->wolopts & WAKE_BCAST) {
1555 const u8 bcast[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Joe Perches1e1d7412012-11-24 01:27:49 +00001556 netdev_info(dev->net, "enabling broadcast detection\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001557 filter_mask[filter * 4] = 0x003F;
1558 filter_mask[filter * 4 + 1] = 0x00;
1559 filter_mask[filter * 4 + 2] = 0x00;
1560 filter_mask[filter * 4 + 3] = 0x00;
1561 command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1562 offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1563 crc[filter/2] |= smsc_crc(bcast, 6, filter);
1564 filter++;
1565 }
1566
1567 if (pdata->wolopts & WAKE_MCAST) {
1568 const u8 mcast[] = {0x01, 0x00, 0x5E};
Joe Perches1e1d7412012-11-24 01:27:49 +00001569 netdev_info(dev->net, "enabling multicast detection\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001570 filter_mask[filter * 4] = 0x0007;
1571 filter_mask[filter * 4 + 1] = 0x00;
1572 filter_mask[filter * 4 + 2] = 0x00;
1573 filter_mask[filter * 4 + 3] = 0x00;
1574 command[filter/4] |= 0x09UL << ((filter % 4) * 8);
1575 offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1576 crc[filter/2] |= smsc_crc(mcast, 3, filter);
1577 filter++;
1578 }
1579
1580 if (pdata->wolopts & WAKE_ARP) {
1581 const u8 arp[] = {0x08, 0x06};
Joe Perches1e1d7412012-11-24 01:27:49 +00001582 netdev_info(dev->net, "enabling ARP detection\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001583 filter_mask[filter * 4] = 0x0003;
1584 filter_mask[filter * 4 + 1] = 0x00;
1585 filter_mask[filter * 4 + 2] = 0x00;
1586 filter_mask[filter * 4 + 3] = 0x00;
1587 command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1588 offset[filter/4] |= 0x0C << ((filter % 4) * 8);
1589 crc[filter/2] |= smsc_crc(arp, 2, filter);
1590 filter++;
1591 }
1592
1593 if (pdata->wolopts & WAKE_UCAST) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001594 netdev_info(dev->net, "enabling unicast detection\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001595 filter_mask[filter * 4] = 0x003F;
1596 filter_mask[filter * 4 + 1] = 0x00;
1597 filter_mask[filter * 4 + 2] = 0x00;
1598 filter_mask[filter * 4 + 3] = 0x00;
1599 command[filter/4] |= 0x01UL << ((filter % 4) * 8);
1600 offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1601 crc[filter/2] |= smsc_crc(dev->net->dev_addr, ETH_ALEN, filter);
1602 filter++;
1603 }
1604
Steve Glendinning9ebca502012-11-22 08:05:23 +00001605 for (i = 0; i < (wuff_filter_count * 4); i++) {
Ming Leiec321152012-11-06 04:53:07 +00001606 ret = smsc95xx_write_reg_nopm(dev, WUFF, filter_mask[i]);
Steve Glendinningb052e072012-11-30 05:55:52 +00001607 if (ret < 0) {
Ming Lei06a221b2012-11-06 04:53:06 +00001608 kfree(filter_mask);
Steve Glendinningb052e072012-11-30 05:55:52 +00001609 goto done;
1610 }
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001611 }
Ming Lei06a221b2012-11-06 04:53:06 +00001612 kfree(filter_mask);
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001613
Steve Glendinning9ebca502012-11-22 08:05:23 +00001614 for (i = 0; i < (wuff_filter_count / 4); i++) {
Ming Leiec321152012-11-06 04:53:07 +00001615 ret = smsc95xx_write_reg_nopm(dev, WUFF, command[i]);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001616 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001617 goto done;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001618 }
1619
Steve Glendinning9ebca502012-11-22 08:05:23 +00001620 for (i = 0; i < (wuff_filter_count / 4); i++) {
Ming Leiec321152012-11-06 04:53:07 +00001621 ret = smsc95xx_write_reg_nopm(dev, WUFF, offset[i]);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001622 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001623 goto done;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001624 }
1625
Steve Glendinning9ebca502012-11-22 08:05:23 +00001626 for (i = 0; i < (wuff_filter_count / 2); i++) {
Ming Leiec321152012-11-06 04:53:07 +00001627 ret = smsc95xx_write_reg_nopm(dev, WUFF, crc[i]);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001628 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001629 goto done;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001630 }
1631
1632 /* clear any pending pattern match packet status */
Ming Leiec321152012-11-06 04:53:07 +00001633 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001634 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001635 goto done;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001636
1637 val |= WUCSR_WUFR_;
1638
Ming Leiec321152012-11-06 04:53:07 +00001639 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001640 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001641 goto done;
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001642 }
1643
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001644 if (pdata->wolopts & WAKE_MAGIC) {
1645 /* clear any pending magic packet status */
Ming Leiec321152012-11-06 04:53:07 +00001646 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001647 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001648 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001649
1650 val |= WUCSR_MPR_;
1651
Ming Leiec321152012-11-06 04:53:07 +00001652 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001653 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001654 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001655 }
1656
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001657 /* enable/disable wakeup sources */
Ming Leiec321152012-11-06 04:53:07 +00001658 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001659 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001660 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001661
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001662 if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001663 netdev_info(dev->net, "enabling pattern match wakeup\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001664 val |= WUCSR_WAKE_EN_;
1665 } else {
Joe Perches1e1d7412012-11-24 01:27:49 +00001666 netdev_info(dev->net, "disabling pattern match wakeup\n");
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001667 val &= ~WUCSR_WAKE_EN_;
1668 }
1669
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001670 if (pdata->wolopts & WAKE_MAGIC) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001671 netdev_info(dev->net, "enabling magic packet wakeup\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001672 val |= WUCSR_MPEN_;
1673 } else {
Joe Perches1e1d7412012-11-24 01:27:49 +00001674 netdev_info(dev->net, "disabling magic packet wakeup\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001675 val &= ~WUCSR_MPEN_;
1676 }
1677
Ming Leiec321152012-11-06 04:53:07 +00001678 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001679 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001680 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001681
1682 /* enable wol wakeup source */
Ming Leiec321152012-11-06 04:53:07 +00001683 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001684 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001685 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001686
1687 val |= PM_CTL_WOL_EN_;
1688
Steve Glendinninge5e3af82012-11-22 08:05:24 +00001689 /* phy energy detect wakeup source */
1690 if (pdata->wolopts & WAKE_PHY)
1691 val |= PM_CTL_ED_EN_;
1692
Ming Leiec321152012-11-06 04:53:07 +00001693 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001694 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001695 goto done;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001696
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001697 /* enable receiver to enable frame reception */
Ming Leiec321152012-11-06 04:53:07 +00001698 smsc95xx_start_rx_path(dev, 1);
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001699
1700 /* some wol options are enabled, so enter SUSPEND0 */
Joe Perches1e1d7412012-11-24 01:27:49 +00001701 netdev_info(dev->net, "entering SUSPEND0 mode\n");
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001702 ret = smsc95xx_enter_suspend0(dev);
1703
1704done:
Ming Lei0d41be52013-03-15 12:08:58 +08001705 /*
1706 * TODO: resume() might need to handle the suspend failure
1707 * in system sleep
1708 */
1709 if (ret && PMSG_IS_AUTO(message))
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001710 usbnet_resume(intf);
Frieder Schrempf7b900ea2018-10-31 22:52:19 +01001711
Steve Glendinning3b9f7d82012-11-30 05:55:49 +00001712 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001713}
1714
1715static int smsc95xx_resume(struct usb_interface *intf)
1716{
1717 struct usbnet *dev = usb_get_intfdata(intf);
Sudip Mukherjee8bca81d2014-11-11 14:10:47 +05301718 struct smsc95xx_priv *pdata;
1719 u8 suspend_flags;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001720 int ret;
1721 u32 val;
1722
1723 BUG_ON(!dev);
Andre Edichad90a732020-08-26 13:17:16 +02001724 pdata = dev->driver_priv;
Sudip Mukherjee8bca81d2014-11-11 14:10:47 +05301725 suspend_flags = pdata->suspend_flags;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001726
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001727 netdev_dbg(dev->net, "resume suspend_flags=0x%02x\n", suspend_flags);
1728
1729 /* do this first to ensure it's cleared even in error case */
1730 pdata->suspend_flags = 0;
1731
1732 if (suspend_flags & SUSPEND_ALLMODES) {
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001733 /* clear wake-up sources */
Ming Leiec321152012-11-06 04:53:07 +00001734 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001735 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001736 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001737
Steve Glendinningbbd9f9e2012-10-26 03:43:56 +00001738 val &= ~(WUCSR_WAKE_EN_ | WUCSR_MPEN_);
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001739
Ming Leiec321152012-11-06 04:53:07 +00001740 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001741 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001742 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001743
1744 /* clear wake-up status */
Ming Leiec321152012-11-06 04:53:07 +00001745 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001746 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001747 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001748
1749 val &= ~PM_CTL_WOL_EN_;
1750 val |= PM_CTL_WUPS_;
1751
Ming Leiec321152012-11-06 04:53:07 +00001752 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
Steve Glendinninge360a8b2013-01-03 03:00:15 +00001753 if (ret < 0)
Steve Glendinningb052e072012-11-30 05:55:52 +00001754 return ret;
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001755 }
1756
Steve Glendinningaf3d7c12012-11-22 08:05:22 +00001757 ret = usbnet_resume(intf);
Steve Glendinningb052e072012-11-30 05:55:52 +00001758 if (ret < 0)
1759 netdev_warn(dev->net, "usbnet_resume error\n");
Steve Glendinninge0e474a2012-09-28 00:07:12 +00001760
Andre Edich05b35e72020-08-26 13:17:17 +02001761 phy_init_hw(pdata->phydev);
Steve Glendinningb052e072012-11-30 05:55:52 +00001762 return ret;
Steve Glendinningb5a04472012-09-28 00:07:11 +00001763}
1764
Joonyoung Shimb4df4802014-07-10 11:49:42 +09001765static int smsc95xx_reset_resume(struct usb_interface *intf)
1766{
1767 struct usbnet *dev = usb_get_intfdata(intf);
1768 int ret;
1769
1770 ret = smsc95xx_reset(dev);
1771 if (ret < 0)
1772 return ret;
1773
1774 return smsc95xx_resume(intf);
1775}
1776
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001777static void smsc95xx_rx_csum_offload(struct sk_buff *skb)
1778{
1779 skb->csum = *(u16 *)(skb_tail_pointer(skb) - 2);
1780 skb->ip_summed = CHECKSUM_COMPLETE;
1781 skb_trim(skb, skb->len - 2);
1782}
1783
1784static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
1785{
Emil Goodeeb855692014-02-13 17:50:19 +01001786 /* This check is no longer done by usbnet */
1787 if (skb->len < dev->net->hard_header_len)
1788 return 0;
1789
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001790 while (skb->len > 0) {
1791 u32 header, align_count;
1792 struct sk_buff *ax_skb;
1793 unsigned char *packet;
1794 u16 size;
1795
Ben Dooks6809d212018-11-14 11:50:21 +00001796 header = get_unaligned_le32(skb->data);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001797 skb_pull(skb, 4 + NET_IP_ALIGN);
1798 packet = skb->data;
1799
1800 /* get the packet length */
1801 size = (u16)((header & RX_STS_FL_) >> 16);
1802 align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4;
1803
1804 if (unlikely(header & RX_STS_ES_)) {
Joe Perchesa475f602010-02-17 10:30:24 +00001805 netif_dbg(dev, rx_err, dev->net,
1806 "Error header=0x%08x\n", header);
Herbert Xu80667ac2009-06-29 16:53:00 +00001807 dev->net->stats.rx_errors++;
1808 dev->net->stats.rx_dropped++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001809
1810 if (header & RX_STS_CRC_) {
Herbert Xu80667ac2009-06-29 16:53:00 +00001811 dev->net->stats.rx_crc_errors++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001812 } else {
1813 if (header & (RX_STS_TL_ | RX_STS_RF_))
Herbert Xu80667ac2009-06-29 16:53:00 +00001814 dev->net->stats.rx_frame_errors++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001815
1816 if ((header & RX_STS_LE_) &&
1817 (!(header & RX_STS_FT_)))
Herbert Xu80667ac2009-06-29 16:53:00 +00001818 dev->net->stats.rx_length_errors++;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001819 }
1820 } else {
1821 /* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
1822 if (unlikely(size > (ETH_FRAME_LEN + 12))) {
Joe Perchesa475f602010-02-17 10:30:24 +00001823 netif_dbg(dev, rx_err, dev->net,
1824 "size err header=0x%08x\n", header);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001825 return 0;
1826 }
1827
1828 /* last frame in this batch */
1829 if (skb->len == size) {
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001830 if (dev->net->features & NETIF_F_RXCSUM)
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001831 smsc95xx_rx_csum_offload(skb);
Peter Korsgaarddf18acc2009-05-13 10:10:41 +00001832 skb_trim(skb, skb->len - 4); /* remove fcs */
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001833 skb->truesize = size + sizeof(struct sk_buff);
1834
1835 return 1;
1836 }
1837
1838 ax_skb = skb_clone(skb, GFP_ATOMIC);
1839 if (unlikely(!ax_skb)) {
Joe Perches60b86752010-02-17 10:30:23 +00001840 netdev_warn(dev->net, "Error allocating skb\n");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001841 return 0;
1842 }
1843
1844 ax_skb->len = size;
1845 ax_skb->data = packet;
1846 skb_set_tail_pointer(ax_skb, size);
1847
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001848 if (dev->net->features & NETIF_F_RXCSUM)
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001849 smsc95xx_rx_csum_offload(ax_skb);
Peter Korsgaarddf18acc2009-05-13 10:10:41 +00001850 skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001851 ax_skb->truesize = size + sizeof(struct sk_buff);
1852
1853 usbnet_skb_return(dev, ax_skb);
1854 }
1855
1856 skb_pull(skb, size);
1857
1858 /* padding bytes before the next frame starts */
1859 if (skb->len)
1860 skb_pull(skb, align_count);
1861 }
1862
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001863 return 1;
1864}
1865
Steve Glendinningf7b29272008-11-20 04:19:21 -08001866static u32 smsc95xx_calc_csum_preamble(struct sk_buff *skb)
1867{
Michał Mirosław55508d62010-12-14 15:24:08 +00001868 u16 low_16 = (u16)skb_checksum_start_offset(skb);
1869 u16 high_16 = low_16 + skb->csum_offset;
Steve Glendinningf7b29272008-11-20 04:19:21 -08001870 return (high_16 << 16) | low_16;
1871}
1872
Ben Dooks75938f72018-11-14 11:50:22 +00001873/* The TX CSUM won't work if the checksum lies in the last 4 bytes of the
1874 * transmission. This is fairly unlikely, only seems to trigger with some
1875 * short TCP ACK packets sent.
1876 *
1877 * Note, this calculation should probably check for the alignment of the
1878 * data as well, but a straight check for csum being in the last four bytes
1879 * of the packet should be ok for now.
1880 */
1881static bool smsc95xx_can_tx_checksum(struct sk_buff *skb)
1882{
1883 unsigned int len = skb->len - skb_checksum_start_offset(skb);
1884
1885 if (skb->len <= 45)
1886 return false;
1887 return skb->csum_offset < (len - (4 + 1));
1888}
1889
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001890static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,
1891 struct sk_buff *skb, gfp_t flags)
1892{
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001893 bool csum = skb->ip_summed == CHECKSUM_PARTIAL;
Steve Glendinningf7b29272008-11-20 04:19:21 -08001894 int overhead = csum ? SMSC95XX_TX_OVERHEAD_CSUM : SMSC95XX_TX_OVERHEAD;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001895 u32 tx_cmd_a, tx_cmd_b;
Ben Dooks0c8b2652018-11-14 11:50:20 +00001896 void *ptr;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001897
Steve Glendinningf7b29272008-11-20 04:19:21 -08001898 /* We do not advertise SG, so skbs should be already linearized */
1899 BUG_ON(skb_shinfo(skb)->nr_frags);
1900
James Hughese9156cd2017-04-19 11:13:40 +01001901 /* Make writable and expand header space by overhead if required */
1902 if (skb_cow_head(skb, overhead)) {
1903 /* Must deallocate here as returning NULL to indicate error
1904 * means the skb won't be deallocated in the caller.
1905 */
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001906 dev_kfree_skb_any(skb);
James Hughese9156cd2017-04-19 11:13:40 +01001907 return NULL;
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001908 }
1909
Ben Dooks0c8b2652018-11-14 11:50:20 +00001910 tx_cmd_b = (u32)skb->len;
1911 tx_cmd_a = tx_cmd_b | TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
1912
Steve Glendinningf7b29272008-11-20 04:19:21 -08001913 if (csum) {
Ben Dooks75938f72018-11-14 11:50:22 +00001914 if (!smsc95xx_can_tx_checksum(skb)) {
Steve Glendinning11bc3082010-03-18 22:18:41 -07001915 /* workaround - hardware tx checksum does not work
1916 * properly with extremely small packets */
Michał Mirosław55508d62010-12-14 15:24:08 +00001917 long csstart = skb_checksum_start_offset(skb);
Steve Glendinning11bc3082010-03-18 22:18:41 -07001918 __wsum calc = csum_partial(skb->data + csstart,
1919 skb->len - csstart, 0);
1920 *((__sum16 *)(skb->data + csstart
1921 + skb->csum_offset)) = csum_fold(calc);
1922
1923 csum = false;
1924 } else {
1925 u32 csum_preamble = smsc95xx_calc_csum_preamble(skb);
Ben Dooks0c8b2652018-11-14 11:50:20 +00001926 ptr = skb_push(skb, 4);
1927 put_unaligned_le32(csum_preamble, ptr);
1928
1929 tx_cmd_a += 4;
1930 tx_cmd_b += 4;
1931 tx_cmd_b |= TX_CMD_B_CSUM_ENABLE;
Steve Glendinning11bc3082010-03-18 22:18:41 -07001932 }
Steve Glendinningf7b29272008-11-20 04:19:21 -08001933 }
1934
Ben Dooks0c8b2652018-11-14 11:50:20 +00001935 ptr = skb_push(skb, 8);
1936 put_unaligned_le32(tx_cmd_a, ptr);
1937 put_unaligned_le32(tx_cmd_b, ptr+4);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001938
1939 return skb;
1940}
1941
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001942static int smsc95xx_manage_power(struct usbnet *dev, int on)
1943{
Andre Edichad90a732020-08-26 13:17:16 +02001944 struct smsc95xx_priv *pdata = dev->driver_priv;
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001945
1946 dev->intf->needs_remote_wakeup = on;
1947
Ming Leieb970ff2013-02-22 03:05:05 +00001948 if (pdata->features & FEATURE_REMOTE_WAKEUP)
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001949 return 0;
1950
Ming Leieb970ff2013-02-22 03:05:05 +00001951 /* this chip revision isn't capable of remote wakeup */
1952 netdev_info(dev->net, "hardware isn't capable of remote wakeup\n");
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001953
1954 if (on)
1955 usb_autopm_get_interface_no_resume(dev->intf);
1956 else
1957 usb_autopm_put_interface(dev->intf);
1958
1959 return 0;
1960}
1961
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001962static const struct driver_info smsc95xx_info = {
1963 .description = "smsc95xx USB 2.0 Ethernet",
1964 .bind = smsc95xx_bind,
1965 .unbind = smsc95xx_unbind,
1966 .link_reset = smsc95xx_link_reset,
Andre Edich05b35e72020-08-26 13:17:17 +02001967 .reset = smsc95xx_start_phy,
1968 .stop = smsc95xx_disconnect_phy,
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001969 .rx_fixup = smsc95xx_rx_fixup,
1970 .tx_fixup = smsc95xx_tx_fixup,
1971 .status = smsc95xx_status,
Steve Glendinningb2d4b152013-01-03 03:00:16 +00001972 .manage_power = smsc95xx_manage_power,
Paolo Pisati07d69d42012-04-23 04:05:20 +00001973 .flags = FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
Steve Glendinning2f7ca802008-10-02 05:27:57 +00001974};
1975
1976static const struct usb_device_id products[] = {
1977 {
1978 /* SMSC9500 USB Ethernet Device */
1979 USB_DEVICE(0x0424, 0x9500),
1980 .driver_info = (unsigned long) &smsc95xx_info,
1981 },
Steve Glendinning726474b2009-05-01 06:07:22 +00001982 {
Steve Glendinning6f41d122009-09-22 05:13:02 +00001983 /* SMSC9505 USB Ethernet Device */
1984 USB_DEVICE(0x0424, 0x9505),
1985 .driver_info = (unsigned long) &smsc95xx_info,
1986 },
1987 {
1988 /* SMSC9500A USB Ethernet Device */
1989 USB_DEVICE(0x0424, 0x9E00),
1990 .driver_info = (unsigned long) &smsc95xx_info,
1991 },
1992 {
1993 /* SMSC9505A USB Ethernet Device */
1994 USB_DEVICE(0x0424, 0x9E01),
1995 .driver_info = (unsigned long) &smsc95xx_info,
1996 },
1997 {
Steve Glendinning726474b2009-05-01 06:07:22 +00001998 /* SMSC9512/9514 USB Hub & Ethernet Device */
1999 USB_DEVICE(0x0424, 0xec00),
2000 .driver_info = (unsigned long) &smsc95xx_info,
2001 },
Steve Glendinning6f41d122009-09-22 05:13:02 +00002002 {
2003 /* SMSC9500 USB Ethernet Device (SAL10) */
2004 USB_DEVICE(0x0424, 0x9900),
2005 .driver_info = (unsigned long) &smsc95xx_info,
2006 },
2007 {
2008 /* SMSC9505 USB Ethernet Device (SAL10) */
2009 USB_DEVICE(0x0424, 0x9901),
2010 .driver_info = (unsigned long) &smsc95xx_info,
2011 },
2012 {
2013 /* SMSC9500A USB Ethernet Device (SAL10) */
2014 USB_DEVICE(0x0424, 0x9902),
2015 .driver_info = (unsigned long) &smsc95xx_info,
2016 },
2017 {
2018 /* SMSC9505A USB Ethernet Device (SAL10) */
2019 USB_DEVICE(0x0424, 0x9903),
2020 .driver_info = (unsigned long) &smsc95xx_info,
2021 },
2022 {
2023 /* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */
2024 USB_DEVICE(0x0424, 0x9904),
2025 .driver_info = (unsigned long) &smsc95xx_info,
2026 },
2027 {
2028 /* SMSC9500A USB Ethernet Device (HAL) */
2029 USB_DEVICE(0x0424, 0x9905),
2030 .driver_info = (unsigned long) &smsc95xx_info,
2031 },
2032 {
2033 /* SMSC9505A USB Ethernet Device (HAL) */
2034 USB_DEVICE(0x0424, 0x9906),
2035 .driver_info = (unsigned long) &smsc95xx_info,
2036 },
2037 {
2038 /* SMSC9500 USB Ethernet Device (Alternate ID) */
2039 USB_DEVICE(0x0424, 0x9907),
2040 .driver_info = (unsigned long) &smsc95xx_info,
2041 },
2042 {
2043 /* SMSC9500A USB Ethernet Device (Alternate ID) */
2044 USB_DEVICE(0x0424, 0x9908),
2045 .driver_info = (unsigned long) &smsc95xx_info,
2046 },
2047 {
2048 /* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */
2049 USB_DEVICE(0x0424, 0x9909),
2050 .driver_info = (unsigned long) &smsc95xx_info,
2051 },
Steve Glendinning88edaa42011-04-10 18:59:27 -07002052 {
2053 /* SMSC LAN9530 USB Ethernet Device */
2054 USB_DEVICE(0x0424, 0x9530),
2055 .driver_info = (unsigned long) &smsc95xx_info,
2056 },
2057 {
2058 /* SMSC LAN9730 USB Ethernet Device */
2059 USB_DEVICE(0x0424, 0x9730),
2060 .driver_info = (unsigned long) &smsc95xx_info,
2061 },
2062 {
2063 /* SMSC LAN89530 USB Ethernet Device */
2064 USB_DEVICE(0x0424, 0x9E08),
2065 .driver_info = (unsigned long) &smsc95xx_info,
2066 },
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002067 { }, /* END */
2068};
2069MODULE_DEVICE_TABLE(usb, products);
2070
2071static struct usb_driver smsc95xx_driver = {
2072 .name = "smsc95xx",
2073 .id_table = products,
2074 .probe = usbnet_probe,
Steve Glendinningb5a04472012-09-28 00:07:11 +00002075 .suspend = smsc95xx_suspend,
Steve Glendinninge0e474a2012-09-28 00:07:12 +00002076 .resume = smsc95xx_resume,
Joonyoung Shimb4df4802014-07-10 11:49:42 +09002077 .reset_resume = smsc95xx_reset_resume,
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002078 .disconnect = usbnet_disconnect,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07002079 .disable_hub_initiated_lpm = 1,
Steve Glendinningb2d4b152013-01-03 03:00:16 +00002080 .supports_autosuspend = 1,
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002081};
2082
Greg Kroah-Hartmand632eb12011-11-18 09:44:20 -08002083module_usb_driver(smsc95xx_driver);
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002084
2085MODULE_AUTHOR("Nancy Lin");
Steve Glendinning90b24cf2012-04-16 12:13:29 +01002086MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
Steve Glendinning2f7ca802008-10-02 05:27:57 +00002087MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices");
2088MODULE_LICENSE("GPL");