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