1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * ASIX AX8817X based USB 2.0 Ethernet Devices
4 * Copyright (C) 2003-2006 David Hollis <dhollis@davehollis.com>
5 * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
6 * Copyright (C) 2006 James Painter <jamie.painter@iname.com>
7 * Copyright (c) 2002-2003 TiVo Inc.
8 */
9
10 #include "asix.h"
11
12 #define AX_HOST_EN_RETRIES 30
13
asix_read_cmd(struct usbnet * dev,u8 cmd,u16 value,u16 index,u16 size,void * data,int in_pm)14 int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
15 u16 size, void *data, int in_pm)
16 {
17 int ret;
18 int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
19
20 BUG_ON(!dev);
21
22 if (!in_pm)
23 fn = usbnet_read_cmd;
24 else
25 fn = usbnet_read_cmd_nopm;
26
27 ret = fn(dev, cmd, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
28 value, index, data, size);
29
30 if (unlikely(ret < 0))
31 netdev_warn(dev->net, "Failed to read reg index 0x%04x: %d\n",
32 index, ret);
33
34 return ret;
35 }
36
asix_write_cmd(struct usbnet * dev,u8 cmd,u16 value,u16 index,u16 size,void * data,int in_pm)37 int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
38 u16 size, void *data, int in_pm)
39 {
40 int ret;
41 int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
42
43 BUG_ON(!dev);
44
45 if (!in_pm)
46 fn = usbnet_write_cmd;
47 else
48 fn = usbnet_write_cmd_nopm;
49
50 ret = fn(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
51 value, index, data, size);
52
53 if (unlikely(ret < 0))
54 netdev_warn(dev->net, "Failed to write reg index 0x%04x: %d\n",
55 index, ret);
56
57 return ret;
58 }
59
asix_write_cmd_async(struct usbnet * dev,u8 cmd,u16 value,u16 index,u16 size,void * data)60 void asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
61 u16 size, void *data)
62 {
63 usbnet_write_cmd_async(dev, cmd,
64 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
65 value, index, data, size);
66 }
67
asix_check_host_enable(struct usbnet * dev,int in_pm)68 static int asix_check_host_enable(struct usbnet *dev, int in_pm)
69 {
70 int i, ret;
71 u8 smsr;
72
73 for (i = 0; i < AX_HOST_EN_RETRIES; ++i) {
74 ret = asix_set_sw_mii(dev, in_pm);
75 if (ret == -ENODEV || ret == -ETIMEDOUT)
76 break;
77 usleep_range(1000, 1100);
78 ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG,
79 0, 0, 1, &smsr, in_pm);
80 if (ret == -ENODEV)
81 break;
82 else if (ret < sizeof(smsr))
83 continue;
84 else if (smsr & AX_HOST_EN)
85 break;
86 }
87
88 return i >= AX_HOST_EN_RETRIES ? -ETIMEDOUT : ret;
89 }
90
reset_asix_rx_fixup_info(struct asix_rx_fixup_info * rx)91 static void reset_asix_rx_fixup_info(struct asix_rx_fixup_info *rx)
92 {
93 /* Reset the variables that have a lifetime outside of
94 * asix_rx_fixup_internal() so that future processing starts from a
95 * known set of initial conditions.
96 */
97
98 if (rx->ax_skb) {
99 /* Discard any incomplete Ethernet frame in the netdev buffer */
100 kfree_skb(rx->ax_skb);
101 rx->ax_skb = NULL;
102 }
103
104 /* Assume the Data header 32-bit word is at the start of the current
105 * or next URB socket buffer so reset all the state variables.
106 */
107 rx->remaining = 0;
108 rx->split_head = false;
109 rx->header = 0;
110 }
111
asix_rx_fixup_internal(struct usbnet * dev,struct sk_buff * skb,struct asix_rx_fixup_info * rx)112 int asix_rx_fixup_internal(struct usbnet *dev, struct sk_buff *skb,
113 struct asix_rx_fixup_info *rx)
114 {
115 int offset = 0;
116 u16 size;
117
118 /* When an Ethernet frame spans multiple URB socket buffers,
119 * do a sanity test for the Data header synchronisation.
120 * Attempt to detect the situation of the previous socket buffer having
121 * been truncated or a socket buffer was missing. These situations
122 * cause a discontinuity in the data stream and therefore need to avoid
123 * appending bad data to the end of the current netdev socket buffer.
124 * Also avoid unnecessarily discarding a good current netdev socket
125 * buffer.
126 */
127 if (rx->remaining && (rx->remaining + sizeof(u32) <= skb->len)) {
128 offset = ((rx->remaining + 1) & 0xfffe);
129 rx->header = get_unaligned_le32(skb->data + offset);
130 offset = 0;
131
132 size = (u16)(rx->header & 0x7ff);
133 if (size != ((~rx->header >> 16) & 0x7ff)) {
134 netdev_err(dev->net, "asix_rx_fixup() Data Header synchronisation was lost, remaining %d\n",
135 rx->remaining);
136 reset_asix_rx_fixup_info(rx);
137 }
138 }
139
140 while (offset + sizeof(u16) <= skb->len) {
141 u16 copy_length;
142
143 if (!rx->remaining) {
144 if (skb->len - offset == sizeof(u16)) {
145 rx->header = get_unaligned_le16(
146 skb->data + offset);
147 rx->split_head = true;
148 offset += sizeof(u16);
149 break;
150 }
151
152 if (rx->split_head == true) {
153 rx->header |= (get_unaligned_le16(
154 skb->data + offset) << 16);
155 rx->split_head = false;
156 offset += sizeof(u16);
157 } else {
158 rx->header = get_unaligned_le32(skb->data +
159 offset);
160 offset += sizeof(u32);
161 }
162
163 /* take frame length from Data header 32-bit word */
164 size = (u16)(rx->header & 0x7ff);
165 if (size != ((~rx->header >> 16) & 0x7ff)) {
166 netdev_err(dev->net, "asix_rx_fixup() Bad Header Length 0x%x, offset %d\n",
167 rx->header, offset);
168 reset_asix_rx_fixup_info(rx);
169 return 0;
170 }
171 if (size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) {
172 netdev_dbg(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
173 size);
174 reset_asix_rx_fixup_info(rx);
175 return 0;
176 }
177
178 /* Sometimes may fail to get a netdev socket buffer but
179 * continue to process the URB socket buffer so that
180 * synchronisation of the Ethernet frame Data header
181 * word is maintained.
182 */
183 rx->ax_skb = netdev_alloc_skb_ip_align(dev->net, size);
184
185 rx->remaining = size;
186 }
187
188 if (rx->remaining > skb->len - offset) {
189 copy_length = skb->len - offset;
190 rx->remaining -= copy_length;
191 } else {
192 copy_length = rx->remaining;
193 rx->remaining = 0;
194 }
195
196 if (rx->ax_skb) {
197 skb_put_data(rx->ax_skb, skb->data + offset,
198 copy_length);
199 if (!rx->remaining) {
200 usbnet_skb_return(dev, rx->ax_skb);
201 rx->ax_skb = NULL;
202 }
203 }
204
205 offset += (copy_length + 1) & 0xfffe;
206 }
207
208 if (skb->len != offset) {
209 netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d, %d\n",
210 skb->len, offset);
211 reset_asix_rx_fixup_info(rx);
212 return 0;
213 }
214
215 return 1;
216 }
217
asix_rx_fixup_common(struct usbnet * dev,struct sk_buff * skb)218 int asix_rx_fixup_common(struct usbnet *dev, struct sk_buff *skb)
219 {
220 struct asix_common_private *dp = dev->driver_priv;
221 struct asix_rx_fixup_info *rx = &dp->rx_fixup_info;
222
223 return asix_rx_fixup_internal(dev, skb, rx);
224 }
225
asix_rx_fixup_common_free(struct asix_common_private * dp)226 void asix_rx_fixup_common_free(struct asix_common_private *dp)
227 {
228 struct asix_rx_fixup_info *rx;
229
230 if (!dp)
231 return;
232
233 rx = &dp->rx_fixup_info;
234
235 if (rx->ax_skb) {
236 kfree_skb(rx->ax_skb);
237 rx->ax_skb = NULL;
238 }
239 }
240
asix_tx_fixup(struct usbnet * dev,struct sk_buff * skb,gfp_t flags)241 struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
242 gfp_t flags)
243 {
244 int padlen;
245 int headroom = skb_headroom(skb);
246 int tailroom = skb_tailroom(skb);
247 u32 packet_len;
248 u32 padbytes = 0xffff0000;
249 void *ptr;
250
251 padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4;
252
253 /* We need to push 4 bytes in front of frame (packet_len)
254 * and maybe add 4 bytes after the end (if padlen is 4)
255 *
256 * Avoid skb_copy_expand() expensive call, using following rules :
257 * - We are allowed to push 4 bytes in headroom if skb_header_cloned()
258 * is false (and if we have 4 bytes of headroom)
259 * - We are allowed to put 4 bytes at tail if skb_cloned()
260 * is false (and if we have 4 bytes of tailroom)
261 *
262 * TCP packets for example are cloned, but __skb_header_release()
263 * was called in tcp stack, allowing us to use headroom for our needs.
264 */
265 if (!skb_header_cloned(skb) &&
266 !(padlen && skb_cloned(skb)) &&
267 headroom + tailroom >= 4 + padlen) {
268 /* following should not happen, but better be safe */
269 if (headroom < 4 ||
270 tailroom < padlen) {
271 skb->data = memmove(skb->head + 4, skb->data, skb->len);
272 skb_set_tail_pointer(skb, skb->len);
273 }
274 } else {
275 struct sk_buff *skb2;
276
277 skb2 = skb_copy_expand(skb, 4, padlen, flags);
278 dev_kfree_skb_any(skb);
279 skb = skb2;
280 if (!skb)
281 return NULL;
282 }
283
284 packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len;
285 ptr = skb_push(skb, 4);
286 put_unaligned_le32(packet_len, ptr);
287
288 if (padlen) {
289 put_unaligned_le32(padbytes, skb_tail_pointer(skb));
290 skb_put(skb, sizeof(padbytes));
291 }
292
293 usbnet_set_skb_tx_stats(skb, 1, 0);
294 return skb;
295 }
296
asix_set_sw_mii(struct usbnet * dev,int in_pm)297 int asix_set_sw_mii(struct usbnet *dev, int in_pm)
298 {
299 int ret;
300 ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL, in_pm);
301
302 if (ret < 0)
303 netdev_err(dev->net, "Failed to enable software MII access\n");
304 return ret;
305 }
306
asix_set_hw_mii(struct usbnet * dev,int in_pm)307 int asix_set_hw_mii(struct usbnet *dev, int in_pm)
308 {
309 int ret;
310 ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL, in_pm);
311 if (ret < 0)
312 netdev_err(dev->net, "Failed to enable hardware MII access\n");
313 return ret;
314 }
315
asix_read_phy_addr(struct usbnet * dev,bool internal)316 int asix_read_phy_addr(struct usbnet *dev, bool internal)
317 {
318 int ret, offset;
319 u8 buf[2];
320
321 ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf, 0);
322 if (ret < 0)
323 goto error;
324
325 if (ret < 2) {
326 ret = -EIO;
327 goto error;
328 }
329
330 offset = (internal ? 1 : 0);
331 ret = buf[offset];
332
333 netdev_dbg(dev->net, "%s PHY address 0x%x\n",
334 internal ? "internal" : "external", ret);
335
336 return ret;
337
338 error:
339 netdev_err(dev->net, "Error reading PHY_ID register: %02x\n", ret);
340
341 return ret;
342 }
343
asix_sw_reset(struct usbnet * dev,u8 flags,int in_pm)344 int asix_sw_reset(struct usbnet *dev, u8 flags, int in_pm)
345 {
346 int ret;
347
348 ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL, in_pm);
349 if (ret < 0)
350 netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
351
352 return ret;
353 }
354
asix_read_rx_ctl(struct usbnet * dev,int in_pm)355 u16 asix_read_rx_ctl(struct usbnet *dev, int in_pm)
356 {
357 __le16 v;
358 int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v, in_pm);
359
360 if (ret < 0) {
361 netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
362 goto out;
363 }
364 ret = le16_to_cpu(v);
365 out:
366 return ret;
367 }
368
asix_write_rx_ctl(struct usbnet * dev,u16 mode,int in_pm)369 int asix_write_rx_ctl(struct usbnet *dev, u16 mode, int in_pm)
370 {
371 int ret;
372
373 netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
374 ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL, in_pm);
375 if (ret < 0)
376 netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
377 mode, ret);
378
379 return ret;
380 }
381
asix_read_medium_status(struct usbnet * dev,int in_pm)382 u16 asix_read_medium_status(struct usbnet *dev, int in_pm)
383 {
384 __le16 v;
385 int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS,
386 0, 0, 2, &v, in_pm);
387
388 if (ret < 0) {
389 netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
390 ret);
391 return ret; /* TODO: callers not checking for error ret */
392 }
393
394 return le16_to_cpu(v);
395
396 }
397
asix_write_medium_mode(struct usbnet * dev,u16 mode,int in_pm)398 int asix_write_medium_mode(struct usbnet *dev, u16 mode, int in_pm)
399 {
400 int ret;
401
402 netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
403 ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE,
404 mode, 0, 0, NULL, in_pm);
405 if (ret < 0)
406 netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
407 mode, ret);
408
409 return ret;
410 }
411
412 /* set MAC link settings according to information from phylib */
asix_adjust_link(struct net_device * netdev)413 void asix_adjust_link(struct net_device *netdev)
414 {
415 struct phy_device *phydev = netdev->phydev;
416 struct usbnet *dev = netdev_priv(netdev);
417 u16 mode = 0;
418
419 if (phydev->link) {
420 mode = AX88772_MEDIUM_DEFAULT;
421
422 if (phydev->duplex == DUPLEX_HALF)
423 mode &= ~AX_MEDIUM_FD;
424
425 if (phydev->speed != SPEED_100)
426 mode &= ~AX_MEDIUM_PS;
427 }
428
429 asix_write_medium_mode(dev, mode, 0);
430 phy_print_status(phydev);
431 }
432
asix_write_gpio(struct usbnet * dev,u16 value,int sleep,int in_pm)433 int asix_write_gpio(struct usbnet *dev, u16 value, int sleep, int in_pm)
434 {
435 int ret;
436
437 netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
438 ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL, in_pm);
439 if (ret < 0)
440 netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
441 value, ret);
442
443 if (sleep)
444 msleep(sleep);
445
446 return ret;
447 }
448
449 /*
450 * AX88772 & AX88178 have a 16-bit RX_CTL value
451 */
asix_set_multicast(struct net_device * net)452 void asix_set_multicast(struct net_device *net)
453 {
454 struct usbnet *dev = netdev_priv(net);
455 struct asix_data *data = (struct asix_data *)&dev->data;
456 u16 rx_ctl = AX_DEFAULT_RX_CTL;
457
458 if (net->flags & IFF_PROMISC) {
459 rx_ctl |= AX_RX_CTL_PRO;
460 } else if (net->flags & IFF_ALLMULTI ||
461 netdev_mc_count(net) > AX_MAX_MCAST) {
462 rx_ctl |= AX_RX_CTL_AMALL;
463 } else if (netdev_mc_empty(net)) {
464 /* just broadcast and directed */
465 } else {
466 /* We use the 20 byte dev->data
467 * for our 8 byte filter buffer
468 * to avoid allocating memory that
469 * is tricky to free later */
470 struct netdev_hw_addr *ha;
471 u32 crc_bits;
472
473 memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
474
475 /* Build the multicast hash filter. */
476 netdev_for_each_mc_addr(ha, net) {
477 crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
478 data->multi_filter[crc_bits >> 3] |=
479 1 << (crc_bits & 7);
480 }
481
482 asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
483 AX_MCAST_FILTER_SIZE, data->multi_filter);
484
485 rx_ctl |= AX_RX_CTL_AM;
486 }
487
488 asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
489 }
490
asix_mdio_read(struct net_device * netdev,int phy_id,int loc)491 int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
492 {
493 struct usbnet *dev = netdev_priv(netdev);
494 __le16 res;
495 int ret;
496
497 mutex_lock(&dev->phy_mutex);
498
499 ret = asix_check_host_enable(dev, 0);
500 if (ret == -ENODEV || ret == -ETIMEDOUT) {
501 mutex_unlock(&dev->phy_mutex);
502 return ret;
503 }
504
505 ret = asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2,
506 &res, 0);
507 if (ret < 0)
508 goto out;
509
510 ret = asix_set_hw_mii(dev, 0);
511 out:
512 mutex_unlock(&dev->phy_mutex);
513
514 netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
515 phy_id, loc, le16_to_cpu(res));
516
517 return ret < 0 ? ret : le16_to_cpu(res);
518 }
519
__asix_mdio_write(struct net_device * netdev,int phy_id,int loc,int val)520 static int __asix_mdio_write(struct net_device *netdev, int phy_id, int loc,
521 int val)
522 {
523 struct usbnet *dev = netdev_priv(netdev);
524 __le16 res = cpu_to_le16(val);
525 int ret;
526
527 netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
528 phy_id, loc, val);
529
530 mutex_lock(&dev->phy_mutex);
531
532 ret = asix_check_host_enable(dev, 0);
533 if (ret == -ENODEV)
534 goto out;
535
536 ret = asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2,
537 &res, 0);
538 if (ret < 0)
539 goto out;
540
541 ret = asix_set_hw_mii(dev, 0);
542 out:
543 mutex_unlock(&dev->phy_mutex);
544
545 return ret < 0 ? ret : 0;
546 }
547
asix_mdio_write(struct net_device * netdev,int phy_id,int loc,int val)548 void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
549 {
550 __asix_mdio_write(netdev, phy_id, loc, val);
551 }
552
553 /* MDIO read and write wrappers for phylib */
asix_mdio_bus_read(struct mii_bus * bus,int phy_id,int regnum)554 int asix_mdio_bus_read(struct mii_bus *bus, int phy_id, int regnum)
555 {
556 struct usbnet *priv = bus->priv;
557
558 return asix_mdio_read(priv->net, phy_id, regnum);
559 }
560
asix_mdio_bus_write(struct mii_bus * bus,int phy_id,int regnum,u16 val)561 int asix_mdio_bus_write(struct mii_bus *bus, int phy_id, int regnum, u16 val)
562 {
563 struct usbnet *priv = bus->priv;
564
565 return __asix_mdio_write(priv->net, phy_id, regnum, val);
566 }
567
asix_mdio_read_nopm(struct net_device * netdev,int phy_id,int loc)568 int asix_mdio_read_nopm(struct net_device *netdev, int phy_id, int loc)
569 {
570 struct usbnet *dev = netdev_priv(netdev);
571 __le16 res;
572 int ret;
573
574 mutex_lock(&dev->phy_mutex);
575
576 ret = asix_check_host_enable(dev, 1);
577 if (ret == -ENODEV || ret == -ETIMEDOUT) {
578 mutex_unlock(&dev->phy_mutex);
579 return ret;
580 }
581
582 asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
583 (__u16)loc, 2, &res, 1);
584 asix_set_hw_mii(dev, 1);
585 mutex_unlock(&dev->phy_mutex);
586
587 netdev_dbg(dev->net, "asix_mdio_read_nopm() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
588 phy_id, loc, le16_to_cpu(res));
589
590 return le16_to_cpu(res);
591 }
592
593 void
asix_mdio_write_nopm(struct net_device * netdev,int phy_id,int loc,int val)594 asix_mdio_write_nopm(struct net_device *netdev, int phy_id, int loc, int val)
595 {
596 struct usbnet *dev = netdev_priv(netdev);
597 __le16 res = cpu_to_le16(val);
598 int ret;
599
600 netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
601 phy_id, loc, val);
602
603 mutex_lock(&dev->phy_mutex);
604
605 ret = asix_check_host_enable(dev, 1);
606 if (ret == -ENODEV) {
607 mutex_unlock(&dev->phy_mutex);
608 return;
609 }
610
611 asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id,
612 (__u16)loc, 2, &res, 1);
613 asix_set_hw_mii(dev, 1);
614 mutex_unlock(&dev->phy_mutex);
615 }
616
asix_get_wol(struct net_device * net,struct ethtool_wolinfo * wolinfo)617 void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
618 {
619 struct usbnet *dev = netdev_priv(net);
620 u8 opt;
621
622 if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE,
623 0, 0, 1, &opt, 0) < 0) {
624 wolinfo->supported = 0;
625 wolinfo->wolopts = 0;
626 return;
627 }
628 wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
629 wolinfo->wolopts = 0;
630 if (opt & AX_MONITOR_LINK)
631 wolinfo->wolopts |= WAKE_PHY;
632 if (opt & AX_MONITOR_MAGIC)
633 wolinfo->wolopts |= WAKE_MAGIC;
634 }
635
asix_set_wol(struct net_device * net,struct ethtool_wolinfo * wolinfo)636 int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
637 {
638 struct usbnet *dev = netdev_priv(net);
639 u8 opt = 0;
640
641 if (wolinfo->wolopts & ~(WAKE_PHY | WAKE_MAGIC))
642 return -EINVAL;
643
644 if (wolinfo->wolopts & WAKE_PHY)
645 opt |= AX_MONITOR_LINK;
646 if (wolinfo->wolopts & WAKE_MAGIC)
647 opt |= AX_MONITOR_MAGIC;
648
649 if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
650 opt, 0, 0, NULL, 0) < 0)
651 return -EINVAL;
652
653 return 0;
654 }
655
asix_get_eeprom_len(struct net_device * net)656 int asix_get_eeprom_len(struct net_device *net)
657 {
658 return AX_EEPROM_LEN;
659 }
660
asix_get_eeprom(struct net_device * net,struct ethtool_eeprom * eeprom,u8 * data)661 int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
662 u8 *data)
663 {
664 struct usbnet *dev = netdev_priv(net);
665 u16 *eeprom_buff;
666 int first_word, last_word;
667 int i;
668
669 if (eeprom->len == 0)
670 return -EINVAL;
671
672 eeprom->magic = AX_EEPROM_MAGIC;
673
674 first_word = eeprom->offset >> 1;
675 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
676
677 eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
678 GFP_KERNEL);
679 if (!eeprom_buff)
680 return -ENOMEM;
681
682 /* ax8817x returns 2 bytes from eeprom on read */
683 for (i = first_word; i <= last_word; i++) {
684 if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2,
685 &eeprom_buff[i - first_word], 0) < 0) {
686 kfree(eeprom_buff);
687 return -EIO;
688 }
689 }
690
691 memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
692 kfree(eeprom_buff);
693 return 0;
694 }
695
asix_set_eeprom(struct net_device * net,struct ethtool_eeprom * eeprom,u8 * data)696 int asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
697 u8 *data)
698 {
699 struct usbnet *dev = netdev_priv(net);
700 u16 *eeprom_buff;
701 int first_word, last_word;
702 int i;
703 int ret;
704
705 netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n",
706 eeprom->len, eeprom->offset, eeprom->magic);
707
708 if (eeprom->len == 0)
709 return -EINVAL;
710
711 if (eeprom->magic != AX_EEPROM_MAGIC)
712 return -EINVAL;
713
714 first_word = eeprom->offset >> 1;
715 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
716
717 eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
718 GFP_KERNEL);
719 if (!eeprom_buff)
720 return -ENOMEM;
721
722 /* align data to 16 bit boundaries, read the missing data from
723 the EEPROM */
724 if (eeprom->offset & 1) {
725 ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, first_word, 0, 2,
726 &eeprom_buff[0], 0);
727 if (ret < 0) {
728 netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word);
729 goto free;
730 }
731 }
732
733 if ((eeprom->offset + eeprom->len) & 1) {
734 ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, last_word, 0, 2,
735 &eeprom_buff[last_word - first_word], 0);
736 if (ret < 0) {
737 netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word);
738 goto free;
739 }
740 }
741
742 memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len);
743
744 /* write data to EEPROM */
745 ret = asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0x0000, 0, 0, NULL, 0);
746 if (ret < 0) {
747 netdev_err(net, "Failed to enable EEPROM write\n");
748 goto free;
749 }
750 msleep(20);
751
752 for (i = first_word; i <= last_word; i++) {
753 netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n",
754 i, eeprom_buff[i - first_word]);
755 ret = asix_write_cmd(dev, AX_CMD_WRITE_EEPROM, i,
756 eeprom_buff[i - first_word], 0, NULL, 0);
757 if (ret < 0) {
758 netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n",
759 i);
760 goto free;
761 }
762 msleep(20);
763 }
764
765 ret = asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0x0000, 0, 0, NULL, 0);
766 if (ret < 0) {
767 netdev_err(net, "Failed to disable EEPROM write\n");
768 goto free;
769 }
770
771 ret = 0;
772 free:
773 kfree(eeprom_buff);
774 return ret;
775 }
776
asix_get_drvinfo(struct net_device * net,struct ethtool_drvinfo * info)777 void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
778 {
779 /* Inherit standard device info */
780 usbnet_get_drvinfo(net, info);
781 strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
782 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
783 }
784
asix_set_mac_address(struct net_device * net,void * p)785 int asix_set_mac_address(struct net_device *net, void *p)
786 {
787 struct usbnet *dev = netdev_priv(net);
788 struct asix_data *data = (struct asix_data *)&dev->data;
789 struct sockaddr *addr = p;
790
791 if (netif_running(net))
792 return -EBUSY;
793 if (!is_valid_ether_addr(addr->sa_data))
794 return -EADDRNOTAVAIL;
795
796 eth_hw_addr_set(net, addr->sa_data);
797
798 /* We use the 20 byte dev->data
799 * for our 6 byte mac buffer
800 * to avoid allocating memory that
801 * is tricky to free later */
802 memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
803 asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
804 data->mac_addr);
805
806 return 0;
807 }
808