xref: /openbmc/linux/drivers/net/usb/asix_common.c (revision cef69974)
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 }
435 
436 int asix_write_gpio(struct usbnet *dev, u16 value, int sleep, int in_pm)
437 {
438 	int ret;
439 
440 	netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
441 	ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL, in_pm);
442 	if (ret < 0)
443 		netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
444 			   value, ret);
445 
446 	if (sleep)
447 		msleep(sleep);
448 
449 	return ret;
450 }
451 
452 /*
453  * AX88772 & AX88178 have a 16-bit RX_CTL value
454  */
455 void asix_set_multicast(struct net_device *net)
456 {
457 	struct usbnet *dev = netdev_priv(net);
458 	struct asix_data *data = (struct asix_data *)&dev->data;
459 	u16 rx_ctl = AX_DEFAULT_RX_CTL;
460 
461 	if (net->flags & IFF_PROMISC) {
462 		rx_ctl |= AX_RX_CTL_PRO;
463 	} else if (net->flags & IFF_ALLMULTI ||
464 		   netdev_mc_count(net) > AX_MAX_MCAST) {
465 		rx_ctl |= AX_RX_CTL_AMALL;
466 	} else if (netdev_mc_empty(net)) {
467 		/* just broadcast and directed */
468 	} else {
469 		/* We use the 20 byte dev->data
470 		 * for our 8 byte filter buffer
471 		 * to avoid allocating memory that
472 		 * is tricky to free later */
473 		struct netdev_hw_addr *ha;
474 		u32 crc_bits;
475 
476 		memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
477 
478 		/* Build the multicast hash filter. */
479 		netdev_for_each_mc_addr(ha, net) {
480 			crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
481 			data->multi_filter[crc_bits >> 3] |=
482 			    1 << (crc_bits & 7);
483 		}
484 
485 		asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
486 				   AX_MCAST_FILTER_SIZE, data->multi_filter);
487 
488 		rx_ctl |= AX_RX_CTL_AM;
489 	}
490 
491 	asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
492 }
493 
494 static int __asix_mdio_read(struct net_device *netdev, int phy_id, int loc,
495 			    bool in_pm)
496 {
497 	struct usbnet *dev = netdev_priv(netdev);
498 	__le16 res;
499 	int ret;
500 
501 	mutex_lock(&dev->phy_mutex);
502 
503 	ret = asix_check_host_enable(dev, in_pm);
504 	if (ret == -ENODEV || ret == -ETIMEDOUT) {
505 		mutex_unlock(&dev->phy_mutex);
506 		return ret;
507 	}
508 
509 	ret = asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2,
510 			    &res, in_pm);
511 	if (ret < 0)
512 		goto out;
513 
514 	ret = asix_set_hw_mii(dev, in_pm);
515 out:
516 	mutex_unlock(&dev->phy_mutex);
517 
518 	netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
519 			phy_id, loc, le16_to_cpu(res));
520 
521 	return ret < 0 ? ret : le16_to_cpu(res);
522 }
523 
524 int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
525 {
526 	return __asix_mdio_read(netdev, phy_id, loc, false);
527 }
528 
529 static int __asix_mdio_write(struct net_device *netdev, int phy_id, int loc,
530 			     int val, bool in_pm)
531 {
532 	struct usbnet *dev = netdev_priv(netdev);
533 	__le16 res = cpu_to_le16(val);
534 	int ret;
535 
536 	netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
537 			phy_id, loc, val);
538 
539 	mutex_lock(&dev->phy_mutex);
540 
541 	ret = asix_check_host_enable(dev, in_pm);
542 	if (ret == -ENODEV)
543 		goto out;
544 
545 	ret = asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2,
546 			     &res, in_pm);
547 	if (ret < 0)
548 		goto out;
549 
550 	ret = asix_set_hw_mii(dev, in_pm);
551 out:
552 	mutex_unlock(&dev->phy_mutex);
553 
554 	return ret < 0 ? ret : 0;
555 }
556 
557 void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
558 {
559 	__asix_mdio_write(netdev, phy_id, loc, val, false);
560 }
561 
562 /* MDIO read and write wrappers for phylib */
563 int asix_mdio_bus_read(struct mii_bus *bus, int phy_id, int regnum)
564 {
565 	struct usbnet *priv = bus->priv;
566 
567 	return __asix_mdio_read(priv->net, phy_id, regnum, false);
568 }
569 
570 int asix_mdio_bus_write(struct mii_bus *bus, int phy_id, int regnum, u16 val)
571 {
572 	struct usbnet *priv = bus->priv;
573 
574 	return __asix_mdio_write(priv->net, phy_id, regnum, val, false);
575 }
576 
577 int asix_mdio_read_nopm(struct net_device *netdev, int phy_id, int loc)
578 {
579 	return __asix_mdio_read(netdev, phy_id, loc, true);
580 }
581 
582 void
583 asix_mdio_write_nopm(struct net_device *netdev, int phy_id, int loc, int val)
584 {
585 	__asix_mdio_write(netdev, phy_id, loc, val, true);
586 }
587 
588 void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
589 {
590 	struct usbnet *dev = netdev_priv(net);
591 	u8 opt;
592 
593 	if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE,
594 			  0, 0, 1, &opt, 0) < 0) {
595 		wolinfo->supported = 0;
596 		wolinfo->wolopts = 0;
597 		return;
598 	}
599 	wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
600 	wolinfo->wolopts = 0;
601 	if (opt & AX_MONITOR_LINK)
602 		wolinfo->wolopts |= WAKE_PHY;
603 	if (opt & AX_MONITOR_MAGIC)
604 		wolinfo->wolopts |= WAKE_MAGIC;
605 }
606 
607 int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
608 {
609 	struct usbnet *dev = netdev_priv(net);
610 	u8 opt = 0;
611 
612 	if (wolinfo->wolopts & ~(WAKE_PHY | WAKE_MAGIC))
613 		return -EINVAL;
614 
615 	if (wolinfo->wolopts & WAKE_PHY)
616 		opt |= AX_MONITOR_LINK;
617 	if (wolinfo->wolopts & WAKE_MAGIC)
618 		opt |= AX_MONITOR_MAGIC;
619 
620 	if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
621 			      opt, 0, 0, NULL, 0) < 0)
622 		return -EINVAL;
623 
624 	return 0;
625 }
626 
627 int asix_get_eeprom_len(struct net_device *net)
628 {
629 	return AX_EEPROM_LEN;
630 }
631 
632 int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
633 		    u8 *data)
634 {
635 	struct usbnet *dev = netdev_priv(net);
636 	u16 *eeprom_buff;
637 	int first_word, last_word;
638 	int i;
639 
640 	if (eeprom->len == 0)
641 		return -EINVAL;
642 
643 	eeprom->magic = AX_EEPROM_MAGIC;
644 
645 	first_word = eeprom->offset >> 1;
646 	last_word = (eeprom->offset + eeprom->len - 1) >> 1;
647 
648 	eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
649 				    GFP_KERNEL);
650 	if (!eeprom_buff)
651 		return -ENOMEM;
652 
653 	/* ax8817x returns 2 bytes from eeprom on read */
654 	for (i = first_word; i <= last_word; i++) {
655 		if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2,
656 				  &eeprom_buff[i - first_word], 0) < 0) {
657 			kfree(eeprom_buff);
658 			return -EIO;
659 		}
660 	}
661 
662 	memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
663 	kfree(eeprom_buff);
664 	return 0;
665 }
666 
667 int asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
668 		    u8 *data)
669 {
670 	struct usbnet *dev = netdev_priv(net);
671 	u16 *eeprom_buff;
672 	int first_word, last_word;
673 	int i;
674 	int ret;
675 
676 	netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n",
677 		   eeprom->len, eeprom->offset, eeprom->magic);
678 
679 	if (eeprom->len == 0)
680 		return -EINVAL;
681 
682 	if (eeprom->magic != AX_EEPROM_MAGIC)
683 		return -EINVAL;
684 
685 	first_word = eeprom->offset >> 1;
686 	last_word = (eeprom->offset + eeprom->len - 1) >> 1;
687 
688 	eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
689 				    GFP_KERNEL);
690 	if (!eeprom_buff)
691 		return -ENOMEM;
692 
693 	/* align data to 16 bit boundaries, read the missing data from
694 	   the EEPROM */
695 	if (eeprom->offset & 1) {
696 		ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, first_word, 0, 2,
697 				    &eeprom_buff[0], 0);
698 		if (ret < 0) {
699 			netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word);
700 			goto free;
701 		}
702 	}
703 
704 	if ((eeprom->offset + eeprom->len) & 1) {
705 		ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, last_word, 0, 2,
706 				    &eeprom_buff[last_word - first_word], 0);
707 		if (ret < 0) {
708 			netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word);
709 			goto free;
710 		}
711 	}
712 
713 	memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len);
714 
715 	/* write data to EEPROM */
716 	ret = asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0x0000, 0, 0, NULL, 0);
717 	if (ret < 0) {
718 		netdev_err(net, "Failed to enable EEPROM write\n");
719 		goto free;
720 	}
721 	msleep(20);
722 
723 	for (i = first_word; i <= last_word; i++) {
724 		netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n",
725 			   i, eeprom_buff[i - first_word]);
726 		ret = asix_write_cmd(dev, AX_CMD_WRITE_EEPROM, i,
727 				     eeprom_buff[i - first_word], 0, NULL, 0);
728 		if (ret < 0) {
729 			netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n",
730 				   i);
731 			goto free;
732 		}
733 		msleep(20);
734 	}
735 
736 	ret = asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0x0000, 0, 0, NULL, 0);
737 	if (ret < 0) {
738 		netdev_err(net, "Failed to disable EEPROM write\n");
739 		goto free;
740 	}
741 
742 	ret = 0;
743 free:
744 	kfree(eeprom_buff);
745 	return ret;
746 }
747 
748 void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
749 {
750 	/* Inherit standard device info */
751 	usbnet_get_drvinfo(net, info);
752 	strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
753 	strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
754 }
755 
756 int asix_set_mac_address(struct net_device *net, void *p)
757 {
758 	struct usbnet *dev = netdev_priv(net);
759 	struct asix_data *data = (struct asix_data *)&dev->data;
760 	struct sockaddr *addr = p;
761 
762 	if (netif_running(net))
763 		return -EBUSY;
764 	if (!is_valid_ether_addr(addr->sa_data))
765 		return -EADDRNOTAVAIL;
766 
767 	eth_hw_addr_set(net, addr->sa_data);
768 
769 	/* We use the 20 byte dev->data
770 	 * for our 6 byte mac buffer
771 	 * to avoid allocating memory that
772 	 * is tricky to free later */
773 	memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
774 	asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,
775 							data->mac_addr);
776 
777 	return 0;
778 }
779