if_mue.c revision 1.14 1 /* $NetBSD: if_mue.c,v 1.14 2018/09/16 01:29:28 rin Exp $ */
2 /* $OpenBSD: if_mue.c,v 1.3 2018/08/04 16:42:46 jsg Exp $ */
3
4 /*
5 * Copyright (c) 2018 Kevin Lo <kevlo (at) openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 /* Driver for Microchip LAN7500/LAN7800 chipsets. */
21
22 #include <sys/cdefs.h>
23 __KERNEL_RCSID(0, "$NetBSD: if_mue.c,v 1.14 2018/09/16 01:29:28 rin Exp $");
24
25 #ifdef _KERNEL_OPT
26 #include "opt_usb.h"
27 #include "opt_inet.h"
28 #endif
29
30 #include <sys/param.h>
31 #include <sys/cprng.h>
32 #include <sys/bus.h>
33 #include <sys/systm.h>
34 #include <sys/sockio.h>
35 #include <sys/mbuf.h>
36 #include <sys/mutex.h>
37 #include <sys/kernel.h>
38 #include <sys/proc.h>
39 #include <sys/socket.h>
40
41 #include <sys/device.h>
42
43 #include <sys/rndsource.h>
44
45 #include <net/if.h>
46 #include <net/if_dl.h>
47 #include <net/if_media.h>
48 #include <net/if_ether.h>
49
50 #include <net/bpf.h>
51
52 #include <netinet/if_inarp.h>
53 #include <netinet/in.h>
54 #include <netinet/ip.h> /* XXX for struct ip */
55 #include <netinet/ip6.h> /* XXX for struct ip6_hdr */
56
57 #include <dev/mii/mii.h>
58 #include <dev/mii/miivar.h>
59
60 #include <dev/usb/usb.h>
61 #include <dev/usb/usbdi.h>
62 #include <dev/usb/usbdi_util.h>
63 #include <dev/usb/usbdivar.h>
64 #include <dev/usb/usbdevs.h>
65
66 #include <dev/usb/if_muereg.h>
67 #include <dev/usb/if_muevar.h>
68
69 #define MUE_PRINTF(sc, fmt, args...) \
70 device_printf((sc)->mue_dev, "%s: " fmt, __func__, ##args);
71
72 #ifdef USB_DEBUG
73 int muedebug = 0;
74 #define DPRINTF(sc, fmt, args...) \
75 do { \
76 if (muedebug) \
77 MUE_PRINTF(sc, fmt, ##args); \
78 } while (0 /* CONSTCOND */)
79 #else
80 #define DPRINTF(sc, fmt, args...) /* nothing */
81 #endif
82
83 /*
84 * Various supported device vendors/products.
85 */
86 struct mue_type {
87 struct usb_devno mue_dev;
88 uint16_t mue_flags;
89 #define LAN7500 0x0001 /* LAN7500 */
90 };
91
92 const struct mue_type mue_devs[] = {
93 { { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN7500 }, LAN7500 },
94 { { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN7505 }, LAN7500 },
95 { { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN7800 }, 0 },
96 { { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN7801 }, 0 },
97 { { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN7850 }, 0 }
98 };
99
100 #define MUE_LOOKUP(uaa) ((const struct mue_type *)usb_lookup(mue_devs, \
101 uaa->uaa_vendor, uaa->uaa_product))
102
103 #define MUE_ENADDR_LO(enaddr) \
104 ((enaddr[3] << 24) | (enaddr[2] << 16) | (enaddr[1] << 8) | enaddr[0])
105 #define MUE_ENADDR_HI(enaddr) \
106 ((enaddr[5] << 8) | enaddr[4])
107
108 static int mue_match(device_t, cfdata_t, void *);
109 static void mue_attach(device_t, device_t, void *);
110 static int mue_detach(device_t, int);
111 static int mue_activate(device_t, enum devact);
112
113 static uint32_t mue_csr_read(struct mue_softc *, uint32_t);
114 static int mue_csr_write(struct mue_softc *, uint32_t, uint32_t);
115 static int mue_wait_for_bits(struct mue_softc *sc, uint32_t, uint32_t,
116 uint32_t, uint32_t);
117
118 static void mue_lock_mii(struct mue_softc *);
119 static void mue_unlock_mii(struct mue_softc *);
120
121 static int mue_miibus_readreg(device_t, int, int);
122 static void mue_miibus_writereg(device_t, int, int, int);
123 static void mue_miibus_statchg(struct ifnet *);
124 static int mue_ifmedia_upd(struct ifnet *);
125 static void mue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
126
127 static uint8_t mue_eeprom_getbyte(struct mue_softc *, int, uint8_t *);
128 static int mue_read_eeprom(struct mue_softc *, uint8_t *, int, int);
129 static bool mue_eeprom_present(struct mue_softc *sc);
130
131 static int mue_read_otp_raw(struct mue_softc *, uint8_t *, int, int);
132 static int mue_read_otp(struct mue_softc *, uint8_t *, int, int);
133
134 static void mue_dataport_write(struct mue_softc *, uint32_t, uint32_t,
135 uint32_t, uint32_t *);
136
137 static void mue_init_ltm(struct mue_softc *);
138
139 static int mue_chip_init(struct mue_softc *);
140
141 static void mue_set_macaddr(struct mue_softc *);
142 static int mue_get_macaddr(struct mue_softc *, prop_dictionary_t);
143
144 static int mue_rx_list_init(struct mue_softc *);
145 static int mue_tx_list_init(struct mue_softc *);
146 static int mue_open_pipes(struct mue_softc *);
147 static void mue_start_rx(struct mue_softc *);
148
149 static int mue_encap(struct mue_softc *, struct mbuf *, int);
150 static void mue_tx_offload(struct mue_softc *, struct mbuf *);
151
152 static void mue_setmulti(struct mue_softc *);
153 static void mue_sethwcsum(struct mue_softc *);
154
155 static void mue_rxeof(struct usbd_xfer *, void *, usbd_status);
156 static void mue_txeof(struct usbd_xfer *, void *, usbd_status);
157
158 static int mue_init(struct ifnet *);
159 static int mue_ioctl(struct ifnet *, u_long, void *);
160 static void mue_watchdog(struct ifnet *);
161 static void mue_reset(struct mue_softc *);
162 static void mue_start(struct ifnet *);
163 static void mue_stop(struct ifnet *, int);
164 static void mue_tick(void *);
165 static void mue_tick_task(void *);
166
167 static struct mbuf *mue_newbuf(void);
168
169 #define MUE_SETBIT(sc, reg, x) \
170 mue_csr_write(sc, reg, mue_csr_read(sc, reg) | (x))
171
172 #define MUE_CLRBIT(sc, reg, x) \
173 mue_csr_write(sc, reg, mue_csr_read(sc, reg) & ~(x))
174
175 #define MUE_WAIT_SET(sc, reg, set, fail) \
176 mue_wait_for_bits(sc, reg, set, ~0, fail)
177
178 #define MUE_WAIT_CLR(sc, reg, clear, fail) \
179 mue_wait_for_bits(sc, reg, 0, clear, fail)
180
181 #define ETHER_IS_VALID(addr) \
182 (!ETHER_IS_MULTICAST(addr) && !ETHER_IS_ZERO(addr))
183
184 #define ETHER_IS_ZERO(addr) \
185 (!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]))
186
187 #define ETHER_ALIGN 2
188
189 CFATTACH_DECL_NEW(mue, sizeof(struct mue_softc), mue_match, mue_attach,
190 mue_detach, mue_activate);
191
192 static uint32_t
193 mue_csr_read(struct mue_softc *sc, uint32_t reg)
194 {
195 usb_device_request_t req;
196 usbd_status err;
197 uDWord val;
198
199 if (sc->mue_dying)
200 return 0;
201
202 USETDW(val, 0);
203 req.bmRequestType = UT_READ_VENDOR_DEVICE;
204 req.bRequest = MUE_UR_READREG;
205 USETW(req.wValue, 0);
206 USETW(req.wIndex, reg);
207 USETW(req.wLength, 4);
208
209 err = usbd_do_request(sc->mue_udev, &req, &val);
210 if (err) {
211 MUE_PRINTF(sc, "reg = 0x%x: %s\n", reg, usbd_errstr(err));
212 return 0;
213 }
214
215 return UGETDW(val);
216 }
217
218 static int
219 mue_csr_write(struct mue_softc *sc, uint32_t reg, uint32_t aval)
220 {
221 usb_device_request_t req;
222 usbd_status err;
223 uDWord val;
224
225 if (sc->mue_dying)
226 return 0;
227
228 USETDW(val, aval);
229 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
230 req.bRequest = MUE_UR_WRITEREG;
231 USETW(req.wValue, 0);
232 USETW(req.wIndex, reg);
233 USETW(req.wLength, 4);
234
235 err = usbd_do_request(sc->mue_udev, &req, &val);
236 if (err) {
237 MUE_PRINTF(sc, "reg = 0x%x: %s\n", reg, usbd_errstr(err));
238 return -1;
239 }
240
241 return 0;
242 }
243
244 static int
245 mue_wait_for_bits(struct mue_softc *sc, uint32_t reg,
246 uint32_t set, uint32_t clear, uint32_t fail)
247 {
248 uint32_t val;
249 int ntries;
250
251 for (ntries = 0; ntries < 1000; ntries++) {
252 val = mue_csr_read(sc, reg);
253 if ((val & set) || !(val & clear))
254 return 0;
255 if (val & fail)
256 return 1;
257 usbd_delay_ms(sc->mue_udev, 1);
258 }
259
260 return 1;
261 }
262
263 /*
264 * Get exclusive access to the MII registers.
265 */
266 static void
267 mue_lock_mii(struct mue_softc *sc)
268 {
269 sc->mue_refcnt++;
270 mutex_enter(&sc->mue_mii_lock);
271 }
272
273 static void
274 mue_unlock_mii(struct mue_softc *sc)
275 {
276 mutex_exit(&sc->mue_mii_lock);
277 if (--sc->mue_refcnt < 0)
278 usb_detach_wakeupold(sc->mue_dev);
279 }
280
281 static int
282 mue_miibus_readreg(device_t dev, int phy, int reg)
283 {
284 struct mue_softc *sc = device_private(dev);
285 uint32_t val;
286
287 if (sc->mue_dying) {
288 DPRINTF(sc, "dying\n");
289 return 0;
290 }
291
292 if (sc->mue_phyno != phy)
293 return 0;
294
295 mue_lock_mii(sc);
296 if (MUE_WAIT_CLR(sc, MUE_MII_ACCESS, MUE_MII_ACCESS_BUSY, 0)) {
297 mue_unlock_mii(sc);
298 MUE_PRINTF(sc, "not ready\n");
299 return -1;
300 }
301
302 mue_csr_write(sc, MUE_MII_ACCESS, MUE_MII_ACCESS_READ |
303 MUE_MII_ACCESS_BUSY | MUE_MII_ACCESS_REGADDR(reg) |
304 MUE_MII_ACCESS_PHYADDR(phy));
305
306 if (MUE_WAIT_CLR(sc, MUE_MII_ACCESS, MUE_MII_ACCESS_BUSY, 0)) {
307 mue_unlock_mii(sc);
308 MUE_PRINTF(sc, "timed out\n");
309 return -1;
310 }
311
312 val = mue_csr_read(sc, MUE_MII_DATA);
313 mue_unlock_mii(sc);
314 return val & 0xffff;
315 }
316
317 static void
318 mue_miibus_writereg(device_t dev, int phy, int reg, int data)
319 {
320 struct mue_softc *sc = device_private(dev);
321
322 if (sc->mue_dying) {
323 DPRINTF(sc, "dying\n");
324 return;
325 }
326
327 if (sc->mue_phyno != phy) {
328 DPRINTF(sc, "sc->mue_phyno (%d) != phy (%d)\n",
329 sc->mue_phyno, phy);
330 return;
331 }
332
333 mue_lock_mii(sc);
334 if (MUE_WAIT_CLR(sc, MUE_MII_ACCESS, MUE_MII_ACCESS_BUSY, 0)) {
335 mue_unlock_mii(sc);
336 MUE_PRINTF(sc, "not ready\n");
337 return;
338 }
339
340 mue_csr_write(sc, MUE_MII_DATA, data);
341 mue_csr_write(sc, MUE_MII_ACCESS, MUE_MII_ACCESS_WRITE |
342 MUE_MII_ACCESS_BUSY | MUE_MII_ACCESS_REGADDR(reg) |
343 MUE_MII_ACCESS_PHYADDR(phy));
344
345 if (MUE_WAIT_CLR(sc, MUE_MII_ACCESS, MUE_MII_ACCESS_BUSY, 0))
346 MUE_PRINTF(sc, "timed out\n");
347
348 mue_unlock_mii(sc);
349 }
350
351 static void
352 mue_miibus_statchg(struct ifnet *ifp)
353 {
354 struct mue_softc *sc = ifp->if_softc;
355 struct mii_data *mii = GET_MII(sc);
356 uint32_t flow, threshold;
357
358 if (mii == NULL || ifp == NULL || (ifp->if_flags & IFF_RUNNING) == 0) {
359 DPRINTF(sc, "not ready\n");
360 return;
361 }
362
363 sc->mue_link = 0;
364 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
365 (IFM_ACTIVE | IFM_AVALID)) {
366 switch (IFM_SUBTYPE(mii->mii_media_active)) {
367 case IFM_10_T:
368 case IFM_100_TX:
369 case IFM_1000_T:
370 sc->mue_link++;
371 break;
372 default:
373 break;
374 }
375 }
376
377 /* Lost link, do nothing. */
378 if (sc->mue_link == 0) {
379 DPRINTF(sc, "mii_media_status = 0x%x\n", mii->mii_media_status);
380 return;
381 }
382
383 if (!(sc->mue_flags & LAN7500)) {
384 if (sc->mue_udev->ud_speed == USB_SPEED_SUPER) {
385 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
386 /* Disable U2 and enable U1. */
387 MUE_CLRBIT(sc, MUE_USB_CFG1,
388 MUE_USB_CFG1_DEV_U2_INIT_EN);
389 MUE_SETBIT(sc, MUE_USB_CFG1,
390 MUE_USB_CFG1_DEV_U1_INIT_EN);
391 } else {
392 /* Enable U1 and U2. */
393 MUE_SETBIT(sc, MUE_USB_CFG1,
394 MUE_USB_CFG1_DEV_U1_INIT_EN |
395 MUE_USB_CFG1_DEV_U2_INIT_EN);
396 }
397 }
398 }
399
400 flow = 0;
401 /* XXX Linux does not check IFM_FDX flag for 7800. */
402 if (IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) {
403 if (IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE)
404 flow |= MUE_FLOW_TX_FCEN | MUE_FLOW_PAUSE_TIME;
405 if (IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE)
406 flow |= MUE_FLOW_RX_FCEN;
407 }
408
409 /* XXX Magic numbers taken from Linux driver. */
410 if (sc->mue_flags & LAN7500)
411 threshold = 0x820;
412 else
413 switch (sc->mue_udev->ud_speed) {
414 case USB_SPEED_SUPER:
415 threshold = 0x817;
416 break;
417 case USB_SPEED_HIGH:
418 threshold = 0x211;
419 break;
420 default:
421 threshold = 0;
422 break;
423 }
424
425 /* Threshold value should be set before enabling flow. */
426 mue_csr_write(sc, (sc->mue_flags & LAN7500) ?
427 MUE_7500_FCT_FLOW : MUE_7800_FCT_FLOW, threshold);
428 mue_csr_write(sc, MUE_FLOW, flow);
429
430 DPRINTF(sc, "done\n");
431 }
432
433 /*
434 * Set media options.
435 */
436 static int
437 mue_ifmedia_upd(struct ifnet *ifp)
438 {
439 struct mue_softc *sc = ifp->if_softc;
440 struct mii_data *mii = GET_MII(sc);
441
442 sc->mue_link = 0; /* XXX */
443
444 if (mii->mii_instance) {
445 struct mii_softc *miisc;
446 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
447 mii_phy_reset(miisc);
448 }
449 return mii_mediachg(mii);
450 }
451
452 /*
453 * Report current media status.
454 */
455 static void
456 mue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
457 {
458 struct mue_softc *sc = ifp->if_softc;
459 struct mii_data *mii = GET_MII(sc);
460
461 mii_pollstat(mii);
462 ifmr->ifm_active = mii->mii_media_active;
463 ifmr->ifm_status = mii->mii_media_status;
464 }
465
466 static uint8_t
467 mue_eeprom_getbyte(struct mue_softc *sc, int off, uint8_t *dest)
468 {
469 uint32_t val;
470
471 if (MUE_WAIT_CLR(sc, MUE_E2P_CMD, MUE_E2P_CMD_BUSY, 0)) {
472 MUE_PRINTF(sc, "not ready\n");
473 return ETIMEDOUT;
474 }
475
476 mue_csr_write(sc, MUE_E2P_CMD, MUE_E2P_CMD_READ | MUE_E2P_CMD_BUSY |
477 (off & MUE_E2P_CMD_ADDR_MASK));
478
479 if (MUE_WAIT_CLR(sc, MUE_E2P_CMD, MUE_E2P_CMD_BUSY,
480 MUE_E2P_CMD_TIMEOUT)) {
481 MUE_PRINTF(sc, "timed out\n");
482 return ETIMEDOUT;
483 }
484
485 val = mue_csr_read(sc, MUE_E2P_DATA);
486 *dest = val & 0xff;
487
488 return 0;
489 }
490
491 static int
492 mue_read_eeprom(struct mue_softc *sc, uint8_t *dest, int off, int cnt)
493 {
494 uint32_t val = 0; /* XXX gcc */
495 uint8_t byte;
496 int i, err;
497
498 /*
499 * EEPROM pins are muxed with the LED function on LAN7800 device.
500 */
501 if (sc->mue_product == USB_PRODUCT_SMSC_LAN7800) {
502 val = mue_csr_read(sc, MUE_HW_CFG);
503 mue_csr_write(sc, MUE_HW_CFG,
504 val & ~(MUE_HW_CFG_LED0_EN | MUE_HW_CFG_LED1_EN));
505 }
506
507 for (i = 0; i < cnt; i++) {
508 err = mue_eeprom_getbyte(sc, off + i, &byte);
509 if (err)
510 break;
511 *(dest + i) = byte;
512 }
513
514 if (sc->mue_product == USB_PRODUCT_SMSC_LAN7800)
515 mue_csr_write(sc, MUE_HW_CFG, val);
516
517 return err ? 1 : 0;
518 }
519
520 static bool
521 mue_eeprom_present(struct mue_softc *sc)
522 {
523 uint32_t val;
524 uint8_t sig;
525 int ret;
526
527 if (sc->mue_flags & LAN7500) {
528 val = mue_csr_read(sc, MUE_E2P_CMD);
529 return val & MUE_E2P_CMD_LOADED;
530 } else {
531 ret = mue_read_eeprom(sc, &sig, MUE_E2P_IND_OFFSET, 1);
532 return (ret == 0) && (sig == MUE_E2P_IND);
533 }
534 }
535
536 static int
537 mue_read_otp_raw(struct mue_softc *sc, uint8_t *dest, int off, int cnt)
538 {
539 uint32_t val;
540 int i, err;
541
542 val = mue_csr_read(sc, MUE_OTP_PWR_DN);
543
544 /* Checking if bit is set. */
545 if (val & MUE_OTP_PWR_DN_PWRDN_N) {
546 /* Clear it, then wait for it to be cleared. */
547 mue_csr_write(sc, MUE_OTP_PWR_DN, 0);
548 err = MUE_WAIT_CLR(sc, MUE_OTP_PWR_DN, MUE_OTP_PWR_DN_PWRDN_N,
549 0);
550 if (err) {
551 MUE_PRINTF(sc, "not ready\n");
552 return 1;
553 }
554 }
555
556 /* Start reading the bytes, one at a time. */
557 for (i = 0; i < cnt; i++) {
558 mue_csr_write(sc, MUE_OTP_ADDR1,
559 ((off + i) >> 8) & MUE_OTP_ADDR1_MASK);
560 mue_csr_write(sc, MUE_OTP_ADDR2,
561 ((off + i) & MUE_OTP_ADDR2_MASK));
562 mue_csr_write(sc, MUE_OTP_FUNC_CMD, MUE_OTP_FUNC_CMD_READ);
563 mue_csr_write(sc, MUE_OTP_CMD_GO, MUE_OTP_CMD_GO_GO);
564
565 err = MUE_WAIT_CLR(sc, MUE_OTP_STATUS, MUE_OTP_STATUS_BUSY, 0);
566 if (err) {
567 MUE_PRINTF(sc, "timed out\n");
568 return 1;
569 }
570 val = mue_csr_read(sc, MUE_OTP_RD_DATA);
571 *(dest + i) = (uint8_t)(val & 0xff);
572 }
573
574 return 0;
575 }
576
577 static int
578 mue_read_otp(struct mue_softc *sc, uint8_t *dest, int off, int cnt)
579 {
580 uint8_t sig;
581 int err;
582
583 if (sc->mue_flags & LAN7500)
584 return 1;
585
586 err = mue_read_otp_raw(sc, &sig, MUE_OTP_IND_OFFSET, 1);
587 if (err)
588 return 1;
589 switch (sig) {
590 case MUE_OTP_IND_1:
591 break;
592 case MUE_OTP_IND_2:
593 off += 0x100;
594 break;
595 default:
596 DPRINTF(sc, "OTP not found\n");
597 return 1;
598 }
599 err = mue_read_otp_raw(sc, dest, off, cnt);
600 return err;
601 }
602
603 static void
604 mue_dataport_write(struct mue_softc *sc, uint32_t sel, uint32_t addr,
605 uint32_t cnt, uint32_t *data)
606 {
607 uint32_t i;
608
609 if (MUE_WAIT_SET(sc, MUE_DP_SEL, MUE_DP_SEL_DPRDY, 0)) {
610 MUE_PRINTF(sc, "not ready\n");
611 return;
612 }
613
614 mue_csr_write(sc, MUE_DP_SEL,
615 (mue_csr_read(sc, MUE_DP_SEL) & ~MUE_DP_SEL_RSEL_MASK) | sel);
616
617 for (i = 0; i < cnt; i++) {
618 mue_csr_write(sc, MUE_DP_ADDR, addr + i);
619 mue_csr_write(sc, MUE_DP_DATA, data[i]);
620 mue_csr_write(sc, MUE_DP_CMD, MUE_DP_CMD_WRITE);
621 if (MUE_WAIT_SET(sc, MUE_DP_SEL, MUE_DP_SEL_DPRDY, 0)) {
622 MUE_PRINTF(sc, "timed out\n");
623 return;
624 }
625 }
626 }
627
628 static void
629 mue_init_ltm(struct mue_softc *sc)
630 {
631 uint32_t idx[MUE_NUM_LTM_INDEX] = { 0, 0, 0, 0, 0, 0 };
632 uint8_t temp[2];
633 size_t i;
634
635 if (mue_csr_read(sc, MUE_USB_CFG1) & MUE_USB_CFG1_LTM_ENABLE) {
636 if (mue_eeprom_present(sc) &&
637 (mue_read_eeprom(sc, temp, MUE_E2P_LTM_OFFSET, 2) == 0)) {
638 if (temp[0] != sizeof(idx)) {
639 DPRINTF(sc, "EEPROM: unexpected size\n");
640 goto done;
641 }
642 if (mue_read_eeprom(sc, (uint8_t *)idx, temp[1] << 1,
643 sizeof(idx))) {
644 DPRINTF(sc, "EEPROM: failed to read\n");
645 goto done;
646 }
647 DPRINTF(sc, "success\n");
648 } else if (mue_read_otp(sc, temp, MUE_E2P_LTM_OFFSET, 2) == 0) {
649 if (temp[0] != sizeof(idx)) {
650 DPRINTF(sc, "OTP: unexpected size\n");
651 goto done;
652 }
653 if (mue_read_otp(sc, (uint8_t *)idx, temp[1] << 1,
654 sizeof(idx))) {
655 DPRINTF(sc, "OTP: failed to read\n");
656 goto done;
657 }
658 DPRINTF(sc, "success\n");
659 } else {
660 DPRINTF(sc, "nothing to do\n");
661 }
662 } else {
663 DPRINTF(sc, "nothing to do\n");
664 }
665 done:
666 for (i = 0; i < __arraycount(idx); i++)
667 mue_csr_write(sc, MUE_LTM_INDEX(i), idx[i]);
668 }
669
670 static int
671 mue_chip_init(struct mue_softc *sc)
672 {
673 uint32_t val;
674
675 if ((sc->mue_flags & LAN7500) &&
676 MUE_WAIT_SET(sc, MUE_PMT_CTL, MUE_PMT_CTL_READY, 0)) {
677 MUE_PRINTF(sc, "not ready\n");
678 return ETIMEDOUT;
679 }
680
681 MUE_SETBIT(sc, MUE_HW_CFG, MUE_HW_CFG_LRST);
682 if (MUE_WAIT_CLR(sc, MUE_HW_CFG, MUE_HW_CFG_LRST, 0)) {
683 MUE_PRINTF(sc, "timed out\n");
684 return ETIMEDOUT;
685 }
686
687 /* Respond to the IN token with a NAK. */
688 if (sc->mue_flags & LAN7500)
689 MUE_SETBIT(sc, MUE_HW_CFG, MUE_HW_CFG_BIR);
690 else
691 MUE_SETBIT(sc, MUE_USB_CFG0, MUE_USB_CFG0_BIR);
692
693 if (sc->mue_flags & LAN7500) {
694 if (sc->mue_udev->ud_speed == USB_SPEED_HIGH)
695 val = MUE_7500_HS_RX_BUFSIZE /
696 MUE_HS_USB_PKT_SIZE;
697 else
698 val = MUE_7500_FS_RX_BUFSIZE /
699 MUE_FS_USB_PKT_SIZE;
700 mue_csr_write(sc, MUE_7500_BURST_CAP, val);
701 mue_csr_write(sc, MUE_7500_BULKIN_DELAY,
702 MUE_7500_DEFAULT_BULKIN_DELAY);
703
704 MUE_SETBIT(sc, MUE_HW_CFG, MUE_HW_CFG_BCE | MUE_HW_CFG_MEF);
705
706 /* Set FIFO sizes. */
707 val = (MUE_7500_MAX_RX_FIFO_SIZE - 512) / 512;
708 mue_csr_write(sc, MUE_7500_FCT_RX_FIFO_END, val);
709 val = (MUE_7500_MAX_TX_FIFO_SIZE - 512) / 512;
710 mue_csr_write(sc, MUE_7500_FCT_TX_FIFO_END, val);
711 } else {
712 /* Init LTM. */
713 mue_init_ltm(sc);
714
715 val = MUE_7800_RX_BUFSIZE;
716 switch (sc->mue_udev->ud_speed) {
717 case USB_SPEED_SUPER:
718 val /= MUE_SS_USB_PKT_SIZE;
719 break;
720 case USB_SPEED_HIGH:
721 val /= MUE_HS_USB_PKT_SIZE;
722 break;
723 default:
724 val /= MUE_FS_USB_PKT_SIZE;
725 break;
726 }
727 mue_csr_write(sc, MUE_7800_BURST_CAP, val);
728 mue_csr_write(sc, MUE_7800_BULKIN_DELAY,
729 MUE_7800_DEFAULT_BULKIN_DELAY);
730
731 MUE_SETBIT(sc, MUE_HW_CFG, MUE_HW_CFG_MEF);
732 MUE_SETBIT(sc, MUE_USB_CFG0, MUE_USB_CFG0_BCE);
733
734 /*
735 * Set FCL's RX and TX FIFO sizes: according to data sheet this
736 * is already the default value. But we initialize it to the
737 * same value anyways, as that's what the Linux driver does.
738 */
739 val = (MUE_7800_MAX_RX_FIFO_SIZE - 512) / 512;
740 mue_csr_write(sc, MUE_7800_FCT_RX_FIFO_END, val);
741 val = (MUE_7800_MAX_TX_FIFO_SIZE - 512) / 512;
742 mue_csr_write(sc, MUE_7800_FCT_TX_FIFO_END, val);
743 }
744
745 /* Enabling interrupts. */
746 mue_csr_write(sc, MUE_INT_STATUS, ~0);
747
748 mue_csr_write(sc, (sc->mue_flags & LAN7500) ?
749 MUE_7500_FCT_FLOW : MUE_7800_FCT_FLOW, 0);
750 mue_csr_write(sc, MUE_FLOW, 0);
751
752 /* Reset PHY. */
753 MUE_SETBIT(sc, MUE_PMT_CTL, MUE_PMT_CTL_PHY_RST);
754 if (MUE_WAIT_CLR(sc, MUE_PMT_CTL, MUE_PMT_CTL_PHY_RST, 0)) {
755 MUE_PRINTF(sc, "PHY not ready\n");
756 return ETIMEDOUT;
757 }
758
759 /* LAN7801 only has RGMII mode. */
760 if (sc->mue_product == USB_PRODUCT_SMSC_LAN7801)
761 MUE_CLRBIT(sc, MUE_MAC_CR, MUE_MAC_CR_GMII_EN);
762
763 if ((sc->mue_flags & LAN7500) ||
764 (sc->mue_product == USB_PRODUCT_SMSC_LAN7800 &&
765 !mue_eeprom_present(sc))) {
766 /* Allow MAC to detect speed and duplex from PHY. */
767 MUE_SETBIT(sc, MUE_MAC_CR, MUE_MAC_CR_AUTO_SPEED |
768 MUE_MAC_CR_AUTO_DUPLEX);
769 }
770
771 MUE_SETBIT(sc, MUE_MAC_TX, MUE_MAC_TX_TXEN);
772 MUE_SETBIT(sc, (sc->mue_flags & LAN7500) ?
773 MUE_7500_FCT_TX_CTL : MUE_7800_FCT_TX_CTL, MUE_FCT_TX_CTL_EN);
774
775 /* Set the maximum frame size. */
776 MUE_CLRBIT(sc, MUE_MAC_RX, MUE_MAC_RX_RXEN);
777 val = mue_csr_read(sc, MUE_MAC_RX);
778 val &= ~MUE_MAC_RX_MAX_SIZE_MASK;
779 val |= MUE_MAC_RX_MAX_LEN(ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN);
780 mue_csr_write(sc, MUE_MAC_RX, val);
781 MUE_SETBIT(sc, MUE_MAC_RX, MUE_MAC_RX_RXEN);
782
783 MUE_SETBIT(sc, (sc->mue_flags & LAN7500) ?
784 MUE_7500_FCT_RX_CTL : MUE_7800_FCT_RX_CTL, MUE_FCT_RX_CTL_EN);
785
786 /* Set default GPIO/LED settings only if no EEPROM is detected. */
787 if ((sc->mue_flags & LAN7500) && !mue_eeprom_present(sc)) {
788 MUE_CLRBIT(sc, MUE_LED_CFG, MUE_LED_CFG_LED10_FUN_SEL);
789 MUE_SETBIT(sc, MUE_LED_CFG,
790 MUE_LED_CFG_LEDGPIO_EN | MUE_LED_CFG_LED2_FUN_SEL);
791 }
792
793 /* XXX We assume two LEDs at least when EEPROM is missing. */
794 if (sc->mue_product == USB_PRODUCT_SMSC_LAN7800 &&
795 !mue_eeprom_present(sc))
796 MUE_SETBIT(sc, MUE_HW_CFG,
797 MUE_HW_CFG_LED0_EN | MUE_HW_CFG_LED1_EN);
798
799 return 0;
800 }
801
802 static void
803 mue_set_macaddr(struct mue_softc *sc)
804 {
805 struct ifnet *ifp = GET_IFP(sc);
806 const uint8_t *enaddr = CLLADDR(ifp->if_sadl);
807 uint32_t lo, hi;
808
809 lo = MUE_ENADDR_LO(enaddr);
810 hi = MUE_ENADDR_HI(enaddr);
811
812 mue_csr_write(sc, MUE_RX_ADDRL, lo);
813 mue_csr_write(sc, MUE_RX_ADDRH, hi);
814 }
815
816 static int
817 mue_get_macaddr(struct mue_softc *sc, prop_dictionary_t dict)
818 {
819 prop_data_t eaprop;
820 uint32_t low, high;
821
822 if (!(sc->mue_flags & LAN7500)) {
823 low = mue_csr_read(sc, MUE_RX_ADDRL);
824 high = mue_csr_read(sc, MUE_RX_ADDRH);
825 sc->mue_enaddr[5] = (uint8_t)((high >> 8) & 0xff);
826 sc->mue_enaddr[4] = (uint8_t)((high) & 0xff);
827 sc->mue_enaddr[3] = (uint8_t)((low >> 24) & 0xff);
828 sc->mue_enaddr[2] = (uint8_t)((low >> 16) & 0xff);
829 sc->mue_enaddr[1] = (uint8_t)((low >> 8) & 0xff);
830 sc->mue_enaddr[0] = (uint8_t)((low) & 0xff);
831 if (ETHER_IS_VALID(sc->mue_enaddr))
832 return 0;
833 else {
834 DPRINTF(sc, "registers: %s\n",
835 ether_sprintf(sc->mue_enaddr));
836 }
837 }
838
839 if (mue_eeprom_present(sc) && !mue_read_eeprom(sc, sc->mue_enaddr,
840 MUE_E2P_MAC_OFFSET, ETHER_ADDR_LEN)) {
841 if (ETHER_IS_VALID(sc->mue_enaddr))
842 return 0;
843 else {
844 DPRINTF(sc, "EEPROM: %s\n",
845 ether_sprintf(sc->mue_enaddr));
846 }
847 }
848
849 if (mue_read_otp(sc, sc->mue_enaddr, MUE_OTP_MAC_OFFSET,
850 ETHER_ADDR_LEN) == 0) {
851 if (ETHER_IS_VALID(sc->mue_enaddr))
852 return 0;
853 else {
854 DPRINTF(sc, "OTP: %s\n",
855 ether_sprintf(sc->mue_enaddr));
856 }
857 }
858
859 /*
860 * Other MD methods. This should be tried only if other methods fail.
861 * Otherwise, MAC address for internal device can be assinged to
862 * external devices on Raspberry Pi, for example.
863 */
864 eaprop = prop_dictionary_get(dict, "mac-address");
865 if (eaprop != NULL) {
866 KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA);
867 KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN);
868 memcpy(sc->mue_enaddr, prop_data_data_nocopy(eaprop),
869 ETHER_ADDR_LEN);
870 if (ETHER_IS_VALID(sc->mue_enaddr))
871 return 0;
872 else {
873 DPRINTF(sc, "prop_dictionary_get: %s\n",
874 ether_sprintf(sc->mue_enaddr));
875 }
876 }
877
878 return 1;
879 }
880
881
882 /*
883 * Probe for a Microchip chip. */
884 static int
885 mue_match(device_t parent, cfdata_t match, void *aux)
886 {
887 struct usb_attach_arg *uaa = aux;
888
889 return (MUE_LOOKUP(uaa) != NULL) ? UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
890 }
891
892 static void
893 mue_attach(device_t parent, device_t self, void *aux)
894 {
895 struct mue_softc *sc = device_private(self);
896 prop_dictionary_t dict = device_properties(self);
897 struct usb_attach_arg *uaa = aux;
898 struct usbd_device *dev = uaa->uaa_device;
899 usb_interface_descriptor_t *id;
900 usb_endpoint_descriptor_t *ed;
901 char *devinfop;
902 struct mii_data *mii;
903 struct ifnet *ifp;
904 usbd_status err;
905 uint8_t i;
906 int s;
907
908 aprint_naive("\n");
909 aprint_normal("\n");
910
911 sc->mue_dev = self;
912 sc->mue_udev = dev;
913
914 devinfop = usbd_devinfo_alloc(sc->mue_udev, 0);
915 aprint_normal_dev(self, "%s\n", devinfop);
916 usbd_devinfo_free(devinfop);
917
918 #define MUE_CONFIG_NO 1
919 err = usbd_set_config_no(dev, MUE_CONFIG_NO, 1);
920 if (err) {
921 aprint_error_dev(self, "failed to set configuration: %s\n",
922 usbd_errstr(err));
923 return;
924 }
925
926 mutex_init(&sc->mue_mii_lock, MUTEX_DEFAULT, IPL_NONE);
927 usb_init_task(&sc->mue_tick_task, mue_tick_task, sc, 0);
928 usb_init_task(&sc->mue_stop_task, (void (*)(void *))mue_stop, sc, 0);
929
930 #define MUE_IFACE_IDX 0
931 err = usbd_device2interface_handle(dev, MUE_IFACE_IDX, &sc->mue_iface);
932 if (err) {
933 aprint_error_dev(self, "failed to get interface handle: %s\n",
934 usbd_errstr(err));
935 return;
936 }
937
938 sc->mue_product = uaa->uaa_product;
939 sc->mue_flags = MUE_LOOKUP(uaa)->mue_flags;
940
941 /* Decide on what our bufsize will be. */
942 if (sc->mue_flags & LAN7500)
943 sc->mue_rxbufsz = (sc->mue_udev->ud_speed == USB_SPEED_HIGH) ?
944 MUE_7500_HS_RX_BUFSIZE : MUE_7500_FS_RX_BUFSIZE;
945 else
946 sc->mue_rxbufsz = MUE_7800_RX_BUFSIZE;
947 sc->mue_txbufsz = MUE_TX_BUFSIZE;
948
949 /* Find endpoints. */
950 id = usbd_get_interface_descriptor(sc->mue_iface);
951 for (i = 0; i < id->bNumEndpoints; i++) {
952 ed = usbd_interface2endpoint_descriptor(sc->mue_iface, i);
953 if (ed == NULL) {
954 aprint_error_dev(self, "failed to get ep %hhd\n", i);
955 return;
956 }
957 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
958 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
959 sc->mue_ed[MUE_ENDPT_RX] = ed->bEndpointAddress;
960 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
961 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
962 sc->mue_ed[MUE_ENDPT_TX] = ed->bEndpointAddress;
963 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
964 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
965 sc->mue_ed[MUE_ENDPT_INTR] = ed->bEndpointAddress;
966 }
967 }
968 KASSERT(sc->mue_ed[MUE_ENDPT_RX] != 0);
969 KASSERT(sc->mue_ed[MUE_ENDPT_TX] != 0);
970 KASSERT(sc->mue_ed[MUE_ENDPT_INTR] != 0);
971
972 s = splnet();
973
974 sc->mue_phyno = 1;
975
976 if (mue_chip_init(sc)) {
977 aprint_error_dev(self, "failed to initialize chip\n");
978 splx(s);
979 return;
980 }
981
982 /* A Microchip chip was detected. Inform the world. */
983 if (sc->mue_flags & LAN7500)
984 aprint_normal_dev(self, "LAN7500\n");
985 else
986 aprint_normal_dev(self, "LAN7800\n");
987
988 if (mue_get_macaddr(sc, dict)) {
989 aprint_error_dev(self, "Ethernet address assigned randomly\n");
990 cprng_fast(sc->mue_enaddr, ETHER_ADDR_LEN);
991 sc->mue_enaddr[0] &= ~0x01; /* unicast */
992 sc->mue_enaddr[0] |= 0x02; /* locally administered */
993 }
994
995 aprint_normal_dev(self, "Ethernet address %s\n",
996 ether_sprintf(sc->mue_enaddr));
997
998 /* Initialize interface info.*/
999 ifp = GET_IFP(sc);
1000 ifp->if_softc = sc;
1001 strlcpy(ifp->if_xname, device_xname(sc->mue_dev), IFNAMSIZ);
1002 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1003 ifp->if_init = mue_init;
1004 ifp->if_ioctl = mue_ioctl;
1005 ifp->if_start = mue_start;
1006 ifp->if_stop = mue_stop;
1007 ifp->if_watchdog = mue_watchdog;
1008
1009 IFQ_SET_READY(&ifp->if_snd);
1010
1011 ifp->if_capabilities = IFCAP_TSOv4 | IFCAP_TSOv6;
1012
1013 sc->mue_ec.ec_capabilities = ETHERCAP_VLAN_MTU;
1014
1015 /* Initialize MII/media info. */
1016 mii = GET_MII(sc);
1017 mii->mii_ifp = ifp;
1018 mii->mii_readreg = mue_miibus_readreg;
1019 mii->mii_writereg = mue_miibus_writereg;
1020 mii->mii_statchg = mue_miibus_statchg;
1021 mii->mii_flags = MIIF_AUTOTSLEEP;
1022
1023 sc->mue_ec.ec_mii = mii;
1024 ifmedia_init(&mii->mii_media, 0, mue_ifmedia_upd, mue_ifmedia_sts);
1025 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
1026
1027 if (LIST_FIRST(&mii->mii_phys) == NULL) {
1028 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
1029 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
1030 } else
1031 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
1032
1033 /* Attach the interface. */
1034 if_attach(ifp);
1035 ether_ifattach(ifp, sc->mue_enaddr);
1036
1037 rnd_attach_source(&sc->mue_rnd_source, device_xname(sc->mue_dev),
1038 RND_TYPE_NET, RND_FLAG_DEFAULT);
1039
1040 callout_init(&sc->mue_stat_ch, 0);
1041
1042 splx(s);
1043
1044 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->mue_udev, sc->mue_dev);
1045 }
1046
1047 static int
1048 mue_detach(device_t self, int flags)
1049 {
1050 struct mue_softc *sc = device_private(self);
1051 struct ifnet *ifp = GET_IFP(sc);
1052 size_t i;
1053 int s;
1054
1055 sc->mue_dying = true;
1056
1057 callout_halt(&sc->mue_stat_ch, NULL);
1058
1059 for (i = 0; i < __arraycount(sc->mue_ep); i++)
1060 if (sc->mue_ep[i] != NULL)
1061 usbd_abort_pipe(sc->mue_ep[i]);
1062
1063 /*
1064 * Remove any pending tasks. They cannot be executing because they run
1065 * in the same thread as detach.
1066 */
1067 usb_rem_task_wait(sc->mue_udev, &sc->mue_tick_task, USB_TASKQ_DRIVER,
1068 NULL);
1069 usb_rem_task_wait(sc->mue_udev, &sc->mue_stop_task, USB_TASKQ_DRIVER,
1070 NULL);
1071
1072 s = splusb();
1073
1074 if (ifp->if_flags & IFF_RUNNING)
1075 mue_stop(ifp, 1);
1076
1077 rnd_detach_source(&sc->mue_rnd_source);
1078 mii_detach(&sc->mue_mii, MII_PHY_ANY, MII_OFFSET_ANY);
1079 ifmedia_delete_instance(&sc->mue_mii.mii_media, IFM_INST_ANY);
1080 if (ifp->if_softc != NULL) {
1081 ether_ifdetach(ifp);
1082 if_detach(ifp);
1083 }
1084
1085 if (--sc->mue_refcnt >= 0) {
1086 /* Wait for processes to go away. */
1087 usb_detach_waitold(sc->mue_dev);
1088 }
1089 splx(s);
1090
1091 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->mue_udev, sc->mue_dev);
1092
1093 mutex_destroy(&sc->mue_mii_lock);
1094
1095 return 0;
1096 }
1097
1098 static int
1099 mue_activate(device_t self, enum devact act)
1100 {
1101 struct mue_softc *sc = device_private(self);
1102 struct ifnet *ifp = GET_IFP(sc);
1103
1104 switch (act) {
1105 case DVACT_DEACTIVATE:
1106 if_deactivate(ifp);
1107 sc->mue_dying = true;
1108 return 0;
1109 default:
1110 return EOPNOTSUPP;
1111 }
1112 return 0;
1113 }
1114
1115 static int
1116 mue_rx_list_init(struct mue_softc *sc)
1117 {
1118 struct mue_cdata *cd;
1119 struct mue_chain *c;
1120 size_t i;
1121 int err;
1122
1123 cd = &sc->mue_cdata;
1124 for (i = 0; i < __arraycount(cd->mue_rx_chain); i++) {
1125 c = &cd->mue_rx_chain[i];
1126 c->mue_sc = sc;
1127 c->mue_idx = i;
1128 if (c->mue_xfer == NULL) {
1129 err = usbd_create_xfer(sc->mue_ep[MUE_ENDPT_RX],
1130 sc->mue_rxbufsz, 0, 0, &c->mue_xfer);
1131 if (err)
1132 return err;
1133 c->mue_buf = usbd_get_buffer(c->mue_xfer);
1134 }
1135 }
1136
1137 return 0;
1138 }
1139
1140 static int
1141 mue_tx_list_init(struct mue_softc *sc)
1142 {
1143 struct mue_cdata *cd;
1144 struct mue_chain *c;
1145 size_t i;
1146 int err;
1147
1148 cd = &sc->mue_cdata;
1149 for (i = 0; i < __arraycount(cd->mue_tx_chain); i++) {
1150 c = &cd->mue_tx_chain[i];
1151 c->mue_sc = sc;
1152 c->mue_idx = i;
1153 if (c->mue_xfer == NULL) {
1154 err = usbd_create_xfer(sc->mue_ep[MUE_ENDPT_TX],
1155 sc->mue_txbufsz, USBD_FORCE_SHORT_XFER, 0,
1156 &c->mue_xfer);
1157 if (err)
1158 return err;
1159 c->mue_buf = usbd_get_buffer(c->mue_xfer);
1160 }
1161 }
1162
1163 return 0;
1164 }
1165
1166 static int
1167 mue_open_pipes(struct mue_softc *sc)
1168 {
1169 usbd_status err;
1170
1171 /* Open RX and TX pipes. */
1172 err = usbd_open_pipe(sc->mue_iface, sc->mue_ed[MUE_ENDPT_RX],
1173 USBD_EXCLUSIVE_USE, &sc->mue_ep[MUE_ENDPT_RX]);
1174 if (err) {
1175 MUE_PRINTF(sc, "rx pipe: %s\n", usbd_errstr(err));
1176 return EIO;
1177 }
1178 err = usbd_open_pipe(sc->mue_iface, sc->mue_ed[MUE_ENDPT_TX],
1179 USBD_EXCLUSIVE_USE, &sc->mue_ep[MUE_ENDPT_TX]);
1180 if (err) {
1181 MUE_PRINTF(sc, "tx pipe: %s\n", usbd_errstr(err));
1182 return EIO;
1183 }
1184 return 0;
1185 }
1186
1187 static void
1188 mue_start_rx(struct mue_softc *sc)
1189 {
1190 struct mue_chain *c;
1191 size_t i;
1192
1193 /* Start up the receive pipe. */
1194 for (i = 0; i < __arraycount(sc->mue_cdata.mue_rx_chain); i++) {
1195 c = &sc->mue_cdata.mue_rx_chain[i];
1196 usbd_setup_xfer(c->mue_xfer, c, c->mue_buf, sc->mue_rxbufsz,
1197 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, mue_rxeof);
1198 usbd_transfer(c->mue_xfer);
1199 }
1200 }
1201
1202 static int
1203 mue_encap(struct mue_softc *sc, struct mbuf *m, int idx)
1204 {
1205 struct ifnet *ifp = GET_IFP(sc);
1206 struct mue_chain *c;
1207 usbd_status err;
1208 struct mue_txbuf_hdr hdr;
1209 uint32_t tx_cmd_a, tx_cmd_b;
1210 int len;
1211 bool tso;
1212
1213 tso = m->m_pkthdr.csum_flags & (M_CSUM_TSOv4 | M_CSUM_TSOv6);
1214
1215 len = m->m_pkthdr.len;
1216 if (__predict_false((!tso && len > MUE_MAX_TX_LEN) ||
1217 ( tso && len > MUE_MAX_TSO_LEN))) {
1218 MUE_PRINTF(sc, "packet length %d\n too long", len);
1219 return EINVAL;
1220 }
1221
1222 c = &sc->mue_cdata.mue_tx_chain[idx];
1223
1224 KASSERT((len & ~MUE_TX_CMD_A_LEN_MASK) == 0);
1225 tx_cmd_a = len | MUE_TX_CMD_A_FCS;
1226
1227 if (tso) {
1228 tx_cmd_a |= MUE_TX_CMD_A_LSO;
1229 if (__predict_true(m->m_pkthdr.segsz > MUE_TX_MSS_MIN))
1230 tx_cmd_b = m->m_pkthdr.segsz;
1231 else
1232 tx_cmd_b = MUE_TX_MSS_MIN;
1233 tx_cmd_b <<= MUE_TX_CMD_B_MSS_SHIFT;
1234 KASSERT((tx_cmd_b & ~MUE_TX_CMD_B_MSS_MASK) == 0);
1235 mue_tx_offload(sc, m);
1236 } else
1237 tx_cmd_b = 0;
1238
1239 hdr.tx_cmd_a = htole32(tx_cmd_a);
1240 hdr.tx_cmd_b = htole32(tx_cmd_b);
1241
1242 memcpy(c->mue_buf, &hdr, sizeof(hdr));
1243 m_copydata(m, 0, len, c->mue_buf + sizeof(hdr));
1244
1245 usbd_setup_xfer(c->mue_xfer, c, c->mue_buf, len + sizeof(hdr),
1246 USBD_FORCE_SHORT_XFER, 10000, mue_txeof);
1247
1248 /* Transmit */
1249 err = usbd_transfer(c->mue_xfer);
1250 if (__predict_false(err != USBD_IN_PROGRESS)) {
1251 MUE_PRINTF(sc, "%s\n", usbd_errstr(err));
1252 mue_stop(ifp, 0);
1253 return EIO;
1254 }
1255
1256 sc->mue_cdata.mue_tx_cnt++;
1257
1258 return 0;
1259 }
1260
1261 static void
1262 mue_tx_offload(struct mue_softc *sc, struct mbuf *m)
1263 {
1264 struct ether_header *eh;
1265 struct ip *ip;
1266 struct ip6_hdr *ip6;
1267 int offset;
1268
1269 eh = mtod(m, struct ether_header *);
1270 switch (htons(eh->ether_type)) {
1271 case ETHERTYPE_IP:
1272 case ETHERTYPE_IPV6:
1273 offset = ETHER_HDR_LEN;
1274 break;
1275 case ETHERTYPE_VLAN:
1276 /* XXX not yet supported */
1277 offset = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
1278 break;
1279 default:
1280 /* XXX */
1281 panic("%s: unsupported ethertype\n", __func__);
1282 /* NOTREACHED */
1283 }
1284
1285 /* Packet length should be cleared. */
1286 if (m->m_pkthdr.csum_flags & M_CSUM_TSOv4) {
1287 ip = (void *)(mtod(m, char *) + offset);
1288 ip->ip_len = 0;
1289 } else {
1290 ip6 = (void *)(mtod(m, char *) + offset);
1291 ip6->ip6_plen = 0;
1292 }
1293 }
1294
1295 static void
1296 mue_setmulti(struct mue_softc *sc)
1297 {
1298 struct ifnet *ifp = GET_IFP(sc);
1299 const uint8_t *enaddr = CLLADDR(ifp->if_sadl);
1300 struct ether_multi *enm;
1301 struct ether_multistep step;
1302 uint32_t pfiltbl[MUE_NUM_ADDR_FILTX][2];
1303 uint32_t hashtbl[MUE_DP_SEL_VHF_HASH_LEN];
1304 uint32_t reg, rxfilt, h, hireg, loreg;
1305 size_t i;
1306
1307 if (sc->mue_dying)
1308 return;
1309
1310 /* Clear perfect filter and hash tables. */
1311 memset(pfiltbl, 0, sizeof(pfiltbl));
1312 memset(hashtbl, 0, sizeof(hashtbl));
1313
1314 reg = (sc->mue_flags & LAN7500) ? MUE_7500_RFE_CTL : MUE_7800_RFE_CTL;
1315 rxfilt = mue_csr_read(sc, reg);
1316 rxfilt &= ~(MUE_RFE_CTL_PERFECT | MUE_RFE_CTL_MULTICAST_HASH |
1317 MUE_RFE_CTL_UNICAST | MUE_RFE_CTL_MULTICAST);
1318
1319 /* Always accept broadcast frames. */
1320 rxfilt |= MUE_RFE_CTL_BROADCAST;
1321
1322 if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
1323 allmulti: rxfilt |= MUE_RFE_CTL_MULTICAST;
1324 if (ifp->if_flags & IFF_PROMISC) {
1325 rxfilt |= MUE_RFE_CTL_UNICAST;
1326 DPRINTF(sc, "promisc\n");
1327 } else {
1328 DPRINTF(sc, "allmulti\n");
1329 }
1330 } else {
1331 /* Now program new ones. */
1332 pfiltbl[0][0] = MUE_ENADDR_HI(enaddr) | MUE_ADDR_FILTX_VALID;
1333 pfiltbl[0][1] = MUE_ENADDR_LO(enaddr);
1334 i = 1;
1335 ETHER_FIRST_MULTI(step, &sc->mue_ec, enm);
1336 while (enm != NULL) {
1337 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
1338 ETHER_ADDR_LEN)) {
1339 memset(pfiltbl, 0, sizeof(pfiltbl));
1340 memset(hashtbl, 0, sizeof(hashtbl));
1341 rxfilt &= ~MUE_RFE_CTL_MULTICAST_HASH;
1342 goto allmulti;
1343 }
1344 if (i < MUE_NUM_ADDR_FILTX) {
1345 /* Use perfect address table if possible. */
1346 pfiltbl[i][0] = MUE_ENADDR_HI(enm->enm_addrlo) |
1347 MUE_ADDR_FILTX_VALID;
1348 pfiltbl[i][1] = MUE_ENADDR_LO(enm->enm_addrlo);
1349 } else {
1350 /* Otherwise, use hash table. */
1351 rxfilt |= MUE_RFE_CTL_MULTICAST_HASH;
1352 h = (ether_crc32_be(enm->enm_addrlo,
1353 ETHER_ADDR_LEN) >> 23) & 0x1ff;
1354 hashtbl[h / 32] |= 1 << (h % 32);
1355 }
1356 i++;
1357 ETHER_NEXT_MULTI(step, enm);
1358 }
1359 rxfilt |= MUE_RFE_CTL_PERFECT;
1360 if (rxfilt & MUE_RFE_CTL_MULTICAST_HASH) {
1361 DPRINTF(sc, "perfect filter and hash tables\n");
1362 } else {
1363 DPRINTF(sc, "perfect filter\n");
1364 }
1365 }
1366
1367 for (i = 0; i < MUE_NUM_ADDR_FILTX; i++) {
1368 hireg = (sc->mue_flags & LAN7500) ?
1369 MUE_7500_ADDR_FILTX(i) : MUE_7800_ADDR_FILTX(i);
1370 loreg = hireg + 4;
1371 mue_csr_write(sc, hireg, 0);
1372 mue_csr_write(sc, loreg, pfiltbl[i][1]);
1373 mue_csr_write(sc, hireg, pfiltbl[i][0]);
1374 }
1375
1376 mue_dataport_write(sc, MUE_DP_SEL_VHF, MUE_DP_SEL_VHF_VLAN_LEN,
1377 MUE_DP_SEL_VHF_HASH_LEN, hashtbl);
1378
1379 mue_csr_write(sc, reg, rxfilt);
1380 }
1381
1382 static void
1383 mue_sethwcsum(struct mue_softc *sc)
1384 {
1385 struct ifnet *ifp = GET_IFP(sc);
1386 uint32_t reg, val;
1387
1388 reg = (sc->mue_flags & LAN7500) ? MUE_7500_RFE_CTL : MUE_7800_RFE_CTL;
1389 val = mue_csr_read(sc, reg);
1390
1391 if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Rx|IFCAP_CSUM_UDPv4_Rx)) {
1392 DPRINTF(sc, "enabled\n");;
1393 val |= MUE_RFE_CTL_IGMP_COE | MUE_RFE_CTL_ICMP_COE;
1394 val |= MUE_RFE_CTL_TCPUDP_COE | MUE_RFE_CTL_IP_COE;
1395 } else {
1396 DPRINTF(sc, "disabled\n");;
1397 val &=
1398 ~(MUE_RFE_CTL_IGMP_COE | MUE_RFE_CTL_ICMP_COE);
1399 val &=
1400 ~(MUE_RFE_CTL_TCPUDP_COE | MUE_RFE_CTL_IP_COE);
1401 }
1402
1403 val &= ~MUE_RFE_CTL_VLAN_FILTER;
1404
1405 mue_csr_write(sc, reg, val);
1406 }
1407
1408
1409 static void
1410 mue_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1411 {
1412 struct mue_chain *c = (struct mue_chain *)priv;
1413 struct mue_softc *sc = c->mue_sc;
1414 struct ifnet *ifp = GET_IFP(sc);
1415 struct mbuf *m;
1416 struct mue_rxbuf_hdr *hdrp;
1417 uint32_t rx_cmd_a, total_len;
1418 uint16_t pktlen;
1419 int s;
1420 char *buf = c->mue_buf;
1421
1422 if (__predict_false(sc->mue_dying)) {
1423 DPRINTF(sc, "dying\n");
1424 return;
1425 }
1426
1427 if (__predict_false(!(ifp->if_flags & IFF_RUNNING))) {
1428 DPRINTF(sc, "not running\n");
1429 return;
1430 }
1431
1432 if (__predict_false(status != USBD_NORMAL_COMPLETION)) {
1433 DPRINTF(sc, "%s\n", usbd_errstr(status));
1434 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1435 return;
1436 if (usbd_ratecheck(&sc->mue_rx_notice))
1437 MUE_PRINTF(sc, "%s\n", usbd_errstr(status));
1438 if (status == USBD_STALLED)
1439 usbd_clear_endpoint_stall_async(
1440 sc->mue_ep[MUE_ENDPT_RX]);
1441 goto done;
1442 }
1443
1444 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1445
1446 KASSERTMSG(total_len <= sc->mue_rxbufsz, "%d <= %u",
1447 total_len, sc->mue_rxbufsz);
1448
1449 do {
1450 if (__predict_false(total_len < sizeof(*hdrp))) {
1451 MUE_PRINTF(sc, "packet length %u too short\n",
1452 total_len);
1453 ifp->if_ierrors++;
1454 goto done;
1455 }
1456
1457 hdrp = (struct mue_rxbuf_hdr *)buf;
1458 rx_cmd_a = le32toh(hdrp->rx_cmd_a);
1459
1460 if (__predict_false(rx_cmd_a & MUE_RX_CMD_A_RED)) {
1461 MUE_PRINTF(sc, "rx_cmd_a: 0x%x\n", rx_cmd_a);
1462 ifp->if_ierrors++;
1463 goto done;
1464 }
1465
1466 /* XXX not yet */
1467 KASSERT((rx_cmd_a & MUE_RX_CMD_A_ICSM) == 0);
1468
1469 pktlen = (uint16_t)(rx_cmd_a & MUE_RX_CMD_A_LEN_MASK);
1470 if (sc->mue_flags & LAN7500)
1471 pktlen -= 2;
1472
1473 if (__predict_false(pktlen < ETHER_HDR_LEN + ETHER_CRC_LEN ||
1474 pktlen > MCLBYTES - ETHER_ALIGN ||
1475 pktlen + sizeof(*hdrp) > total_len)) {
1476 MUE_PRINTF(sc, "invalid packet length %d\n", pktlen);
1477 ifp->if_ierrors++;
1478 goto done;
1479 }
1480
1481 m = mue_newbuf();
1482 if (__predict_false(m == NULL)) {
1483 MUE_PRINTF(sc, "failed to allocate mbuf\n");
1484 ifp->if_ierrors++;
1485 goto done;
1486 }
1487
1488 m_set_rcvif(m, ifp);
1489 m->m_pkthdr.len = m->m_len = pktlen;
1490 m->m_flags |= M_HASFCS;
1491 memcpy(mtod(m, char *), buf + sizeof(*hdrp), pktlen);
1492
1493 /* Attention: sizeof(hdr) = 10 */
1494 pktlen = roundup(pktlen + sizeof(*hdrp), 4);
1495 if (pktlen > total_len)
1496 pktlen = total_len;
1497 total_len -= pktlen;
1498 buf += pktlen;
1499
1500 s = splnet();
1501 if_percpuq_enqueue(ifp->if_percpuq, m);
1502 splx(s);
1503 } while (total_len > 0);
1504
1505 done:
1506 /* Setup new transfer. */
1507 usbd_setup_xfer(xfer, c, c->mue_buf, sc->mue_rxbufsz,
1508 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, mue_rxeof);
1509 usbd_transfer(xfer);
1510 }
1511
1512 static void
1513 mue_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1514 {
1515 struct mue_chain *c = priv;
1516 struct mue_softc *sc = c->mue_sc;
1517 struct ifnet *ifp = GET_IFP(sc);
1518 int s;
1519
1520 if (__predict_false(sc->mue_dying))
1521 return;
1522
1523 s = splnet();
1524
1525 if (__predict_false(status != USBD_NORMAL_COMPLETION)) {
1526 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1527 splx(s);
1528 return;
1529 }
1530 ifp->if_oerrors++;
1531 MUE_PRINTF(sc, "%s\n", usbd_errstr(status));
1532 if (status == USBD_STALLED)
1533 usbd_clear_endpoint_stall_async(
1534 sc->mue_ep[MUE_ENDPT_TX]);
1535 splx(s);
1536 return;
1537 }
1538
1539 ifp->if_timer = 0;
1540 ifp->if_flags &= ~IFF_OACTIVE;
1541
1542 if (!IFQ_IS_EMPTY(&ifp->if_snd))
1543 mue_start(ifp);
1544
1545 ifp->if_opackets++;
1546 splx(s);
1547 }
1548
1549 static int
1550 mue_init(struct ifnet *ifp)
1551 {
1552 struct mue_softc *sc = ifp->if_softc;
1553 int s;
1554
1555 if (sc->mue_dying) {
1556 DPRINTF(sc, "dying\n");
1557 return EIO;
1558 }
1559
1560 s = splnet();
1561
1562 /* Cancel pending I/O and free all TX/RX buffers. */
1563 if (ifp->if_flags & IFF_RUNNING)
1564 mue_stop(ifp, 1);
1565
1566 mue_reset(sc);
1567
1568 /* Set MAC address. */
1569 mue_set_macaddr(sc);
1570
1571 /* Load the multicast filter. */
1572 mue_setmulti(sc);
1573
1574 /* TCP/UDP checksum offload engines. */
1575 mue_sethwcsum(sc);
1576
1577 if (mue_open_pipes(sc)) {
1578 splx(s);
1579 return EIO;
1580 }
1581
1582 /* Init RX ring. */
1583 if (mue_rx_list_init(sc)) {
1584 MUE_PRINTF(sc, "failed to init rx list\n");
1585 splx(s);
1586 return ENOBUFS;
1587 }
1588
1589 /* Init TX ring. */
1590 if (mue_tx_list_init(sc)) {
1591 MUE_PRINTF(sc, "failed to init tx list\n");
1592 splx(s);
1593 return ENOBUFS;
1594 }
1595
1596 mue_start_rx(sc);
1597
1598 ifp->if_flags |= IFF_RUNNING;
1599 ifp->if_flags &= ~IFF_OACTIVE;
1600
1601 splx(s);
1602
1603 callout_reset(&sc->mue_stat_ch, hz, mue_tick, sc);
1604
1605 return 0;
1606 }
1607
1608 static int
1609 mue_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1610 {
1611 struct mue_softc *sc = ifp->if_softc;
1612 struct ifreq /*const*/ *ifr = data;
1613 int s, error = 0;
1614
1615 s = splnet();
1616
1617 switch(cmd) {
1618 case SIOCSIFFLAGS:
1619 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1620 break;
1621
1622 switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
1623 case IFF_RUNNING:
1624 mue_stop(ifp, 1);
1625 break;
1626 case IFF_UP:
1627 mue_init(ifp);
1628 break;
1629 case IFF_UP | IFF_RUNNING:
1630 if ((ifp->if_flags ^ sc->mue_if_flags) == IFF_PROMISC)
1631 mue_setmulti(sc);
1632 else
1633 mue_init(ifp);
1634 break;
1635 }
1636 sc->mue_if_flags = ifp->if_flags;
1637 break;
1638 case SIOCGIFMEDIA:
1639 case SIOCSIFMEDIA:
1640 error = ifmedia_ioctl(ifp, ifr, &sc->mue_mii.mii_media, cmd);
1641 break;
1642 default:
1643 if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
1644 break;
1645 error = 0;
1646 if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI)
1647 mue_setmulti(sc);
1648 break;
1649 }
1650 splx(s);
1651
1652 return error;
1653 }
1654
1655 static void
1656 mue_watchdog(struct ifnet *ifp)
1657 {
1658 struct mue_softc *sc = ifp->if_softc;
1659 struct mue_chain *c;
1660 usbd_status stat;
1661 int s;
1662
1663 ifp->if_oerrors++;
1664 MUE_PRINTF(sc, "timed out\n");
1665
1666 s = splusb();
1667 c = &sc->mue_cdata.mue_tx_chain[0];
1668 usbd_get_xfer_status(c->mue_xfer, NULL, NULL, NULL, &stat);
1669 mue_txeof(c->mue_xfer, c, stat);
1670
1671 if (!IFQ_IS_EMPTY(&ifp->if_snd))
1672 mue_start(ifp);
1673 splx(s);
1674 }
1675
1676 static void
1677 mue_reset(struct mue_softc *sc)
1678 {
1679 if (sc->mue_dying)
1680 return;
1681
1682 /* Wait a little while for the chip to get its brains in order. */
1683 usbd_delay_ms(sc->mue_udev, 1);
1684
1685 // mue_chip_init(sc); /* XXX */
1686 }
1687
1688 static void
1689 mue_start(struct ifnet *ifp)
1690 {
1691 struct mue_softc *sc = ifp->if_softc;
1692 struct mbuf *m;
1693
1694 if (__predict_false(!sc->mue_link)) {
1695 DPRINTF(sc, "no link\n");
1696 return;
1697 }
1698
1699 if (__predict_false((ifp->if_flags & (IFF_OACTIVE|IFF_RUNNING))
1700 != IFF_RUNNING)) {
1701 DPRINTF(sc, "not ready\n");
1702 return;
1703 }
1704
1705 IFQ_POLL(&ifp->if_snd, m);
1706 if (m == NULL)
1707 return;
1708
1709 if (__predict_false(mue_encap(sc, m, 0))) {
1710 ifp->if_oerrors++;
1711 return;
1712 }
1713 IFQ_DEQUEUE(&ifp->if_snd, m);
1714
1715 bpf_mtap(ifp, m, BPF_D_OUT);
1716 m_freem(m);
1717
1718 ifp->if_flags |= IFF_OACTIVE;
1719
1720 /* Set a timeout in case the chip goes out to lunch. */
1721 ifp->if_timer = 5;
1722 }
1723
1724 static void
1725 mue_stop(struct ifnet *ifp, int disable __unused)
1726 {
1727 struct mue_softc *sc = ifp->if_softc;
1728 usbd_status err;
1729 size_t i;
1730
1731 mue_reset(sc);
1732
1733 ifp->if_timer = 0;
1734 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1735
1736 callout_stop(&sc->mue_stat_ch);
1737
1738 /* Stop transfers. */
1739 for (i = 0; i < __arraycount(sc->mue_ep); i++)
1740 if (sc->mue_ep[i] != NULL) {
1741 err = usbd_abort_pipe(sc->mue_ep[i]);
1742 if (err)
1743 MUE_PRINTF(sc, "abort pipe %zu: %s\n",
1744 i, usbd_errstr(err));
1745 }
1746
1747 /* Free RX resources. */
1748 for (i = 0; i < __arraycount(sc->mue_cdata.mue_rx_chain); i++)
1749 if (sc->mue_cdata.mue_rx_chain[i].mue_xfer != NULL) {
1750 usbd_destroy_xfer(
1751 sc->mue_cdata.mue_rx_chain[i].mue_xfer);
1752 sc->mue_cdata.mue_rx_chain[i].mue_xfer = NULL;
1753 }
1754
1755 /* Free TX resources. */
1756 for (i = 0; i < __arraycount(sc->mue_cdata.mue_tx_chain); i++)
1757 if (sc->mue_cdata.mue_tx_chain[i].mue_xfer != NULL) {
1758 usbd_destroy_xfer(
1759 sc->mue_cdata.mue_tx_chain[i].mue_xfer);
1760 sc->mue_cdata.mue_tx_chain[i].mue_xfer = NULL;
1761 }
1762
1763 /* Close pipes */
1764 for (i = 0; i < __arraycount(sc->mue_ep); i++)
1765 if (sc->mue_ep[i] != NULL) {
1766 err = usbd_close_pipe(sc->mue_ep[i]);
1767 if (err)
1768 MUE_PRINTF(sc, "close pipe %zu: %s\n",
1769 i, usbd_errstr(err));
1770 sc->mue_ep[i] = NULL;
1771 }
1772
1773 sc->mue_link = 0; /* XXX */
1774
1775 DPRINTF(sc, "done\n");
1776 }
1777
1778 static void
1779 mue_tick(void *xsc)
1780 {
1781 struct mue_softc *sc = xsc;
1782
1783 if (sc == NULL)
1784 return;
1785
1786 if (sc->mue_dying)
1787 return;
1788
1789 /* Perform periodic stuff in process context. */
1790 usb_add_task(sc->mue_udev, &sc->mue_tick_task, USB_TASKQ_DRIVER);
1791 }
1792
1793 static void
1794 mue_tick_task(void *xsc)
1795 {
1796 struct mue_softc *sc = xsc;
1797 struct ifnet *ifp = GET_IFP(sc);
1798 struct mii_data *mii = GET_MII(sc);
1799 int s;
1800
1801 if (sc == NULL)
1802 return;
1803
1804 if (sc->mue_dying)
1805 return;
1806
1807 s = splnet();
1808 mii_tick(mii);
1809 if (sc->mue_link == 0)
1810 mue_miibus_statchg(ifp);
1811 callout_reset(&sc->mue_stat_ch, hz, mue_tick, sc);
1812 splx(s);
1813 }
1814
1815 static struct mbuf *
1816 mue_newbuf(void)
1817 {
1818 struct mbuf *m;
1819
1820 MGETHDR(m, M_DONTWAIT, MT_DATA);
1821 if (__predict_false(m == NULL))
1822 return NULL;
1823
1824 MCLGET(m, M_DONTWAIT);
1825 if (__predict_false(!(m->m_flags & M_EXT))) {
1826 m_freem(m);
1827 return NULL;
1828 }
1829
1830 m_adj(m, ETHER_ALIGN);
1831
1832 return m;
1833 }
1834