if_url.c revision 1.62 1 /* $NetBSD: if_url.c,v 1.62 2019/03/08 03:05:15 msaitoh Exp $ */
2
3 /*
4 * Copyright (c) 2001, 2002
5 * Shingo WATANABE <nabe (at) nabechan.org>. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the author nor the names of any co-contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 */
32
33 /*
34 * The RTL8150L(Realtek USB to fast ethernet controller) spec can be found at
35 * ftp://ftp.realtek.com.tw/lancard/data_sheet/8150/8150v14.pdf
36 * ftp://152.104.125.40/lancard/data_sheet/8150/8150v14.pdf
37 */
38
39 /*
40 * TODO:
41 * Interrupt Endpoint support
42 * External PHYs
43 * powerhook() support?
44 */
45
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: if_url.c,v 1.62 2019/03/08 03:05:15 msaitoh Exp $");
48
49 #ifdef _KERNEL_OPT
50 #include "opt_inet.h"
51 #include "opt_usb.h"
52 #endif
53
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/rwlock.h>
57 #include <sys/mbuf.h>
58 #include <sys/kernel.h>
59 #include <sys/socket.h>
60
61 #include <sys/device.h>
62 #include <sys/rndsource.h>
63
64 #include <net/if.h>
65 #include <net/if_arp.h>
66 #include <net/if_dl.h>
67 #include <net/if_media.h>
68
69 #include <net/bpf.h>
70
71 #include <net/if_ether.h>
72 #ifdef INET
73 #include <netinet/in.h>
74 #include <netinet/if_inarp.h>
75 #endif
76
77 #include <dev/mii/mii.h>
78 #include <dev/mii/miivar.h>
79 #include <dev/mii/urlphyreg.h>
80
81 #include <dev/usb/usb.h>
82 #include <dev/usb/usbdi.h>
83 #include <dev/usb/usbdi_util.h>
84 #include <dev/usb/usbdevs.h>
85
86 #include <dev/usb/if_urlreg.h>
87
88
89 /* Function declarations */
90 int url_match(device_t, cfdata_t, void *);
91 void url_attach(device_t, device_t, void *);
92 int url_detach(device_t, int);
93 int url_activate(device_t, enum devact);
94 extern struct cfdriver url_cd;
95 CFATTACH_DECL_NEW(url, sizeof(struct url_softc), url_match, url_attach,
96 url_detach, url_activate);
97
98 Static int url_openpipes(struct url_softc *);
99 Static int url_rx_list_init(struct url_softc *);
100 Static int url_tx_list_init(struct url_softc *);
101 Static int url_newbuf(struct url_softc *, struct url_chain *, struct mbuf *);
102 Static void url_start(struct ifnet *);
103 Static int url_send(struct url_softc *, struct mbuf *, int);
104 Static void url_txeof(struct usbd_xfer *, void *, usbd_status);
105 Static void url_rxeof(struct usbd_xfer *, void *, usbd_status);
106 Static void url_tick(void *);
107 Static void url_tick_task(void *);
108 Static int url_ioctl(struct ifnet *, u_long, void *);
109 Static void url_stop_task(struct url_softc *);
110 Static void url_stop(struct ifnet *, int);
111 Static void url_watchdog(struct ifnet *);
112 Static int url_ifmedia_change(struct ifnet *);
113 Static void url_ifmedia_status(struct ifnet *, struct ifmediareq *);
114 Static void url_lock_mii(struct url_softc *);
115 Static void url_unlock_mii(struct url_softc *);
116 Static int url_int_miibus_readreg(device_t, int, int, uint16_t *);
117 Static int url_int_miibus_writereg(device_t, int, int, uint16_t);
118 Static void url_miibus_statchg(struct ifnet *);
119 Static int url_init(struct ifnet *);
120 Static void url_setmulti(struct url_softc *);
121 Static void url_reset(struct url_softc *);
122
123 Static int url_csr_read_1(struct url_softc *, int);
124 Static int url_csr_read_2(struct url_softc *, int);
125 Static int url_csr_write_1(struct url_softc *, int, int);
126 Static int url_csr_write_2(struct url_softc *, int, int);
127 Static int url_csr_write_4(struct url_softc *, int, int);
128 Static int url_mem(struct url_softc *, int, int, void *, int);
129
130 /* Macros */
131 #ifdef URL_DEBUG
132 #define DPRINTF(x) if (urldebug) printf x
133 #define DPRINTFN(n,x) if (urldebug >= (n)) printf x
134 int urldebug = 0;
135 #else
136 #define DPRINTF(x)
137 #define DPRINTFN(n,x)
138 #endif
139
140 #define URL_SETBIT(sc, reg, x) \
141 url_csr_write_1(sc, reg, url_csr_read_1(sc, reg) | (x))
142
143 #define URL_SETBIT2(sc, reg, x) \
144 url_csr_write_2(sc, reg, url_csr_read_2(sc, reg) | (x))
145
146 #define URL_CLRBIT(sc, reg, x) \
147 url_csr_write_1(sc, reg, url_csr_read_1(sc, reg) & ~(x))
148
149 #define URL_CLRBIT2(sc, reg, x) \
150 url_csr_write_2(sc, reg, url_csr_read_2(sc, reg) & ~(x))
151
152 static const struct url_type {
153 struct usb_devno url_dev;
154 uint16_t url_flags;
155 #define URL_EXT_PHY 0x0001
156 } url_devs [] = {
157 /* MELCO LUA-KTX */
158 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX }, 0},
159 /* Realtek RTL8150L Generic (GREEN HOUSE USBKR100) */
160 {{ USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8150L}, 0},
161 /* Longshine LCS-8138TX */
162 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_LCS8138TX}, 0},
163 /* Micronet SP128AR */
164 {{ USB_VENDOR_MICRONET, USB_PRODUCT_MICRONET_SP128AR}, 0},
165 /* OQO model 01 */
166 {{ USB_VENDOR_OQO, USB_PRODUCT_OQO_ETHER01}, 0},
167 };
168 #define url_lookup(v, p) ((const struct url_type *)usb_lookup(url_devs, v, p))
169
170
171 /* Probe */
172 int
173 url_match(device_t parent, cfdata_t match, void *aux)
174 {
175 struct usb_attach_arg *uaa = aux;
176
177 return url_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
178 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
179 }
180 /* Attach */
181 void
182 url_attach(device_t parent, device_t self, void *aux)
183 {
184 struct url_softc *sc = device_private(self);
185 struct usb_attach_arg *uaa = aux;
186 struct usbd_device *dev = uaa->uaa_device;
187 struct usbd_interface *iface;
188 usbd_status err;
189 usb_interface_descriptor_t *id;
190 usb_endpoint_descriptor_t *ed;
191 char *devinfop;
192 struct ifnet *ifp;
193 struct mii_data *mii;
194 u_char eaddr[ETHER_ADDR_LEN];
195 int i, s;
196
197 sc->sc_dev = self;
198
199 aprint_naive("\n");
200 aprint_normal("\n");
201
202 devinfop = usbd_devinfo_alloc(dev, 0);
203 aprint_normal_dev(self, "%s\n", devinfop);
204 usbd_devinfo_free(devinfop);
205
206 /* Move the device into the configured state. */
207 err = usbd_set_config_no(dev, URL_CONFIG_NO, 1);
208 if (err) {
209 aprint_error_dev(self, "failed to set configuration"
210 ", err=%s\n", usbd_errstr(err));
211 goto bad;
212 }
213
214 usb_init_task(&sc->sc_tick_task, url_tick_task, sc, 0);
215 rw_init(&sc->sc_mii_rwlock);
216 usb_init_task(&sc->sc_stop_task, (void (*)(void *))url_stop_task, sc, 0);
217
218 /* get control interface */
219 err = usbd_device2interface_handle(dev, URL_IFACE_INDEX, &iface);
220 if (err) {
221 aprint_error_dev(self, "failed to get interface, err=%s\n",
222 usbd_errstr(err));
223 goto bad;
224 }
225
226 sc->sc_udev = dev;
227 sc->sc_ctl_iface = iface;
228 sc->sc_flags = url_lookup(uaa->uaa_vendor, uaa->uaa_product)->url_flags;
229
230 /* get interface descriptor */
231 id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
232
233 /* find endpoints */
234 sc->sc_bulkin_no = sc->sc_bulkout_no = sc->sc_intrin_no = -1;
235 for (i = 0; i < id->bNumEndpoints; i++) {
236 ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
237 if (ed == NULL) {
238 aprint_error_dev(self,
239 "couldn't get endpoint %d\n", i);
240 goto bad;
241 }
242 if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
243 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
244 sc->sc_bulkin_no = ed->bEndpointAddress; /* RX */
245 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
246 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT)
247 sc->sc_bulkout_no = ed->bEndpointAddress; /* TX */
248 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT &&
249 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
250 sc->sc_intrin_no = ed->bEndpointAddress; /* Status */
251 }
252
253 if (sc->sc_bulkin_no == -1 || sc->sc_bulkout_no == -1 ||
254 sc->sc_intrin_no == -1) {
255 aprint_error_dev(self, "missing endpoint\n");
256 goto bad;
257 }
258
259 s = splnet();
260
261 /* reset the adapter */
262 url_reset(sc);
263
264 /* Get Ethernet Address */
265 err = url_mem(sc, URL_CMD_READMEM, URL_IDR0, (void *)eaddr,
266 ETHER_ADDR_LEN);
267 if (err) {
268 aprint_error_dev(self, "read MAC address failed\n");
269 splx(s);
270 goto bad;
271 }
272
273 /* Print Ethernet Address */
274 aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
275
276 /* initialize interface information */
277 ifp = GET_IFP(sc);
278 ifp->if_softc = sc;
279 ifp->if_mtu = ETHERMTU;
280 strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
281 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
282 ifp->if_start = url_start;
283 ifp->if_ioctl = url_ioctl;
284 ifp->if_watchdog = url_watchdog;
285 ifp->if_init = url_init;
286 ifp->if_stop = url_stop;
287
288 IFQ_SET_READY(&ifp->if_snd);
289
290 /*
291 * Do ifmedia setup.
292 */
293 mii = &sc->sc_mii;
294 mii->mii_ifp = ifp;
295 mii->mii_readreg = url_int_miibus_readreg;
296 mii->mii_writereg = url_int_miibus_writereg;
297 #if 0
298 if (sc->sc_flags & URL_EXT_PHY) {
299 mii->mii_readreg = url_ext_miibus_readreg;
300 mii->mii_writereg = url_ext_miibus_writereg;
301 }
302 #endif
303 mii->mii_statchg = url_miibus_statchg;
304 mii->mii_flags = MIIF_AUTOTSLEEP;
305 sc->sc_ec.ec_mii = mii;
306 ifmedia_init(&mii->mii_media, 0,
307 url_ifmedia_change, url_ifmedia_status);
308 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
309 if (LIST_FIRST(&mii->mii_phys) == NULL) {
310 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
311 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
312 } else
313 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
314
315 /* attach the interface */
316 if_attach(ifp);
317 ether_ifattach(ifp, eaddr);
318
319 rnd_attach_source(&sc->rnd_source, device_xname(self),
320 RND_TYPE_NET, RND_FLAG_DEFAULT);
321
322 callout_init(&sc->sc_stat_ch, 0);
323 sc->sc_attached = 1;
324 splx(s);
325
326 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, sc->sc_dev);
327
328 if (!pmf_device_register(self, NULL, NULL))
329 aprint_error_dev(self, "couldn't establish power handler\n");
330
331 return;
332
333 bad:
334 sc->sc_dying = 1;
335 return;
336 }
337
338 /* detach */
339 int
340 url_detach(device_t self, int flags)
341 {
342 struct url_softc *sc = device_private(self);
343 struct ifnet *ifp = GET_IFP(sc);
344 int s;
345
346 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
347
348 /* Detached before attached finished */
349 if (!sc->sc_attached)
350 return 0;
351
352 pmf_device_deregister(self);
353
354 /*
355 * XXX Halting callout guarantees no more tick tasks. What
356 * guarantees no more stop tasks? What guarantees no more
357 * calls to url_send? Don't we need to wait for if_detach or
358 * something? Should set sc->sc_dying here? Is device
359 * deactivation guaranteed to have already happened?
360 */
361 callout_halt(&sc->sc_stat_ch, NULL);
362
363 /* Remove any pending tasks */
364 usb_rem_task_wait(sc->sc_udev, &sc->sc_tick_task, USB_TASKQ_DRIVER,
365 NULL);
366 usb_rem_task_wait(sc->sc_udev, &sc->sc_stop_task, USB_TASKQ_DRIVER,
367 NULL);
368
369 s = splusb();
370
371 if (--sc->sc_refcnt >= 0) {
372 /* Wait for processes to go away */
373 usb_detach_waitold(sc->sc_dev);
374 }
375
376 if (ifp->if_flags & IFF_RUNNING)
377 url_stop(GET_IFP(sc), 1);
378
379 rnd_detach_source(&sc->rnd_source);
380 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
381 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
382 ether_ifdetach(ifp);
383 if_detach(ifp);
384
385 #ifdef DIAGNOSTIC
386 if (sc->sc_pipe_tx != NULL)
387 aprint_debug_dev(self, "detach has active tx endpoint.\n");
388 if (sc->sc_pipe_rx != NULL)
389 aprint_debug_dev(self, "detach has active rx endpoint.\n");
390 if (sc->sc_pipe_intr != NULL)
391 aprint_debug_dev(self, "detach has active intr endpoint.\n");
392 #endif
393
394 sc->sc_attached = 0;
395
396 splx(s);
397
398 rw_destroy(&sc->sc_mii_rwlock);
399 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
400
401 return 0;
402 }
403
404 /* read/write memory */
405 Static int
406 url_mem(struct url_softc *sc, int cmd, int offset, void *buf, int len)
407 {
408 usb_device_request_t req;
409 usbd_status err;
410
411 if (sc == NULL)
412 return 0;
413
414 DPRINTFN(0x200,
415 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
416
417 if (sc->sc_dying)
418 return 0;
419
420 if (cmd == URL_CMD_READMEM)
421 req.bmRequestType = UT_READ_VENDOR_DEVICE;
422 else
423 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
424 req.bRequest = URL_REQ_MEM;
425 USETW(req.wValue, offset);
426 USETW(req.wIndex, 0x0000);
427 USETW(req.wLength, len);
428
429 sc->sc_refcnt++;
430 err = usbd_do_request(sc->sc_udev, &req, buf);
431 if (--sc->sc_refcnt < 0)
432 usb_detach_wakeupold(sc->sc_dev);
433 if (err) {
434 DPRINTF(("%s: url_mem(): %s failed. off=%04x, err=%d\n",
435 device_xname(sc->sc_dev),
436 cmd == URL_CMD_READMEM ? "read" : "write",
437 offset, err));
438 }
439
440 return err;
441 }
442
443 /* read 1byte from register */
444 Static int
445 url_csr_read_1(struct url_softc *sc, int reg)
446 {
447 uint8_t val = 0;
448
449 DPRINTFN(0x100,
450 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
451
452 if (sc->sc_dying)
453 return 0;
454
455 return url_mem(sc, URL_CMD_READMEM, reg, &val, 1) ? 0 : val;
456 }
457
458 /* read 2bytes from register */
459 Static int
460 url_csr_read_2(struct url_softc *sc, int reg)
461 {
462 uWord val;
463
464 DPRINTFN(0x100,
465 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
466
467 if (sc->sc_dying)
468 return 0;
469
470 USETW(val, 0);
471 return url_mem(sc, URL_CMD_READMEM, reg, &val, 2) ? 0 : UGETW(val);
472 }
473
474 /* write 1byte to register */
475 Static int
476 url_csr_write_1(struct url_softc *sc, int reg, int aval)
477 {
478 uint8_t val = aval;
479
480 DPRINTFN(0x100,
481 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
482
483 if (sc->sc_dying)
484 return 0;
485
486 return url_mem(sc, URL_CMD_WRITEMEM, reg, &val, 1) ? -1 : 0;
487 }
488
489 /* write 2bytes to register */
490 Static int
491 url_csr_write_2(struct url_softc *sc, int reg, int aval)
492 {
493 uWord val;
494
495 DPRINTFN(0x100,
496 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
497
498 USETW(val, aval);
499
500 if (sc->sc_dying)
501 return 0;
502
503 return url_mem(sc, URL_CMD_WRITEMEM, reg, &val, 2) ? -1 : 0;
504 }
505
506 /* write 4bytes to register */
507 Static int
508 url_csr_write_4(struct url_softc *sc, int reg, int aval)
509 {
510 uDWord val;
511
512 DPRINTFN(0x100,
513 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
514
515 USETDW(val, aval);
516
517 if (sc->sc_dying)
518 return 0;
519
520 return url_mem(sc, URL_CMD_WRITEMEM, reg, &val, 4) ? -1 : 0;
521 }
522
523 Static int
524 url_init(struct ifnet *ifp)
525 {
526 struct url_softc *sc = ifp->if_softc;
527 struct mii_data *mii = GET_MII(sc);
528 const u_char *eaddr;
529 int i, rc, s;
530
531 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
532
533 if (sc->sc_dying)
534 return EIO;
535
536 s = splnet();
537
538 /* Cancel pending I/O and free all TX/RX buffers */
539 url_stop(ifp, 1);
540
541 eaddr = CLLADDR(ifp->if_sadl);
542 for (i = 0; i < ETHER_ADDR_LEN; i++)
543 url_csr_write_1(sc, URL_IDR0 + i, eaddr[i]);
544
545 /* Init transmission control register */
546 URL_CLRBIT(sc, URL_TCR,
547 URL_TCR_TXRR1 | URL_TCR_TXRR0 |
548 URL_TCR_IFG1 | URL_TCR_IFG0 |
549 URL_TCR_NOCRC);
550
551 /* Init receive control register */
552 URL_SETBIT2(sc, URL_RCR, URL_RCR_TAIL | URL_RCR_AD);
553 if (ifp->if_flags & IFF_BROADCAST)
554 URL_SETBIT2(sc, URL_RCR, URL_RCR_AB);
555 else
556 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AB);
557
558 /* If we want promiscuous mode, accept all physical frames. */
559 if (ifp->if_flags & IFF_PROMISC)
560 URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP);
561 else
562 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP);
563
564
565 /* Load the multicast filter */
566 url_setmulti(sc);
567
568 /* Enable RX and TX */
569 URL_SETBIT(sc, URL_CR, URL_CR_TE | URL_CR_RE);
570
571 if ((rc = mii_mediachg(mii)) == ENXIO)
572 rc = 0;
573 else if (rc != 0)
574 goto out;
575
576 if (sc->sc_pipe_tx == NULL || sc->sc_pipe_rx == NULL) {
577 if (url_openpipes(sc)) {
578 splx(s);
579 return EIO;
580 }
581 }
582 /* Initialize transmit ring */
583 if (url_tx_list_init(sc)) {
584 printf("%s: tx list init failed\n", device_xname(sc->sc_dev));
585 splx(s);
586 return EIO;
587 }
588
589 /* Initialize receive ring */
590 if (url_rx_list_init(sc)) {
591 printf("%s: rx list init failed\n", device_xname(sc->sc_dev));
592 splx(s);
593 return EIO;
594 }
595 /* Start up the receive pipe. */
596 for (i = 0; i < URL_RX_LIST_CNT; i++) {
597 struct url_chain *c = &sc->sc_cdata.url_rx_chain[i];
598
599 usbd_setup_xfer(c->url_xfer, c, c->url_buf, URL_BUFSZ,
600 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, url_rxeof);
601 (void)usbd_transfer(c->url_xfer);
602 DPRINTF(("%s: %s: start read\n", device_xname(sc->sc_dev),
603 __func__));
604 }
605
606 ifp->if_flags |= IFF_RUNNING;
607 ifp->if_flags &= ~IFF_OACTIVE;
608
609 callout_reset(&sc->sc_stat_ch, hz, url_tick, sc);
610
611 out:
612 splx(s);
613 return rc;
614 }
615
616 Static void
617 url_reset(struct url_softc *sc)
618 {
619 int i;
620
621 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
622
623 if (sc->sc_dying)
624 return;
625
626 URL_SETBIT(sc, URL_CR, URL_CR_SOFT_RST);
627
628 for (i = 0; i < URL_TX_TIMEOUT; i++) {
629 if (!(url_csr_read_1(sc, URL_CR) & URL_CR_SOFT_RST))
630 break;
631 delay(10); /* XXX */
632 }
633
634 delay(10000); /* XXX */
635 }
636
637 int
638 url_activate(device_t self, enum devact act)
639 {
640 struct url_softc *sc = device_private(self);
641
642 DPRINTF(("%s: %s: enter, act=%d\n", device_xname(sc->sc_dev),
643 __func__, act));
644
645 switch (act) {
646 case DVACT_DEACTIVATE:
647 if_deactivate(&sc->sc_ec.ec_if);
648 sc->sc_dying = 1;
649 return 0;
650 default:
651 return EOPNOTSUPP;
652 }
653 }
654
655 #define url_calchash(addr) (ether_crc32_be((addr), ETHER_ADDR_LEN) >> 26)
656
657
658 Static void
659 url_setmulti(struct url_softc *sc)
660 {
661 struct ifnet *ifp;
662 struct ether_multi *enm;
663 struct ether_multistep step;
664 uint32_t hashes[2] = { 0, 0 };
665 int h = 0;
666 int mcnt = 0;
667
668 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
669
670 if (sc->sc_dying)
671 return;
672
673 ifp = GET_IFP(sc);
674
675 if (ifp->if_flags & IFF_PROMISC) {
676 URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP);
677 return;
678 } else if (ifp->if_flags & IFF_ALLMULTI) {
679 allmulti:
680 ifp->if_flags |= IFF_ALLMULTI;
681 URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM);
682 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AAP);
683 return;
684 }
685
686 /* first, zot all the existing hash bits */
687 url_csr_write_4(sc, URL_MAR0, 0);
688 url_csr_write_4(sc, URL_MAR4, 0);
689
690 /* now program new ones */
691 ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
692 while (enm != NULL) {
693 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
694 ETHER_ADDR_LEN) != 0)
695 goto allmulti;
696
697 h = url_calchash(enm->enm_addrlo);
698 if (h < 32)
699 hashes[0] |= (1 << h);
700 else
701 hashes[1] |= (1 << (h -32));
702 mcnt++;
703 ETHER_NEXT_MULTI(step, enm);
704 }
705
706 ifp->if_flags &= ~IFF_ALLMULTI;
707
708 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP);
709
710 if (mcnt){
711 URL_SETBIT2(sc, URL_RCR, URL_RCR_AM);
712 } else {
713 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AM);
714 }
715 url_csr_write_4(sc, URL_MAR0, hashes[0]);
716 url_csr_write_4(sc, URL_MAR4, hashes[1]);
717 }
718
719 Static int
720 url_openpipes(struct url_softc *sc)
721 {
722 usbd_status err;
723 int error = 0;
724
725 if (sc->sc_dying)
726 return EIO;
727
728 sc->sc_refcnt++;
729
730 /* Open RX pipe */
731 err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkin_no,
732 USBD_EXCLUSIVE_USE, &sc->sc_pipe_rx);
733 if (err) {
734 printf("%s: open rx pipe failed: %s\n",
735 device_xname(sc->sc_dev), usbd_errstr(err));
736 error = EIO;
737 goto done;
738 }
739
740 /* Open TX pipe */
741 err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkout_no,
742 USBD_EXCLUSIVE_USE, &sc->sc_pipe_tx);
743 if (err) {
744 printf("%s: open tx pipe failed: %s\n",
745 device_xname(sc->sc_dev), usbd_errstr(err));
746 error = EIO;
747 goto done;
748 }
749
750 #if 0
751 /* XXX: interrupt endpoint is not yet supported */
752 /* Open Interrupt pipe */
753 err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_intrin_no,
754 USBD_EXCLUSIVE_USE, &sc->sc_pipe_intr, sc,
755 &sc->sc_cdata.url_ibuf, URL_INTR_PKGLEN,
756 url_intr, USBD_DEFAULT_INTERVAL);
757 if (err) {
758 printf("%s: open intr pipe failed: %s\n",
759 device_xname(sc->sc_dev), usbd_errstr(err));
760 error = EIO;
761 goto done;
762 }
763 #endif
764
765 done:
766 if (--sc->sc_refcnt < 0)
767 usb_detach_wakeupold(sc->sc_dev);
768
769 return error;
770 }
771
772 Static int
773 url_newbuf(struct url_softc *sc, struct url_chain *c, struct mbuf *m)
774 {
775 struct mbuf *m_new = NULL;
776
777 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
778
779 if (m == NULL) {
780 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
781 if (m_new == NULL) {
782 printf("%s: no memory for rx list "
783 "-- packet dropped!\n", device_xname(sc->sc_dev));
784 return ENOBUFS;
785 }
786 MCLGET(m_new, M_DONTWAIT);
787 if (!(m_new->m_flags & M_EXT)) {
788 printf("%s: no memory for rx list "
789 "-- packet dropped!\n", device_xname(sc->sc_dev));
790 m_freem(m_new);
791 return ENOBUFS;
792 }
793 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
794 } else {
795 m_new = m;
796 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
797 m_new->m_data = m_new->m_ext.ext_buf;
798 }
799
800 m_adj(m_new, ETHER_ALIGN);
801 c->url_mbuf = m_new;
802
803 return 0;
804 }
805
806
807 Static int
808 url_rx_list_init(struct url_softc *sc)
809 {
810 struct url_cdata *cd;
811 struct url_chain *c;
812 int i;
813
814 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
815
816 cd = &sc->sc_cdata;
817 for (i = 0; i < URL_RX_LIST_CNT; i++) {
818 c = &cd->url_rx_chain[i];
819 c->url_sc = sc;
820 c->url_idx = i;
821 if (url_newbuf(sc, c, NULL) == ENOBUFS)
822 return ENOBUFS;
823 if (c->url_xfer == NULL) {
824 int error = usbd_create_xfer(sc->sc_pipe_rx, URL_BUFSZ,
825 0, 0, &c->url_xfer);
826 if (error)
827 return error;
828 c->url_buf = usbd_get_buffer(c->url_xfer);
829 }
830 }
831
832 return 0;
833 }
834
835 Static int
836 url_tx_list_init(struct url_softc *sc)
837 {
838 struct url_cdata *cd;
839 struct url_chain *c;
840 int i;
841
842 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
843
844 cd = &sc->sc_cdata;
845 for (i = 0; i < URL_TX_LIST_CNT; i++) {
846 c = &cd->url_tx_chain[i];
847 c->url_sc = sc;
848 c->url_idx = i;
849 c->url_mbuf = NULL;
850 if (c->url_xfer == NULL) {
851 int error = usbd_create_xfer(sc->sc_pipe_tx, URL_BUFSZ,
852 USBD_FORCE_SHORT_XFER, 0, &c->url_xfer);
853 if (error)
854 return error;
855 c->url_buf = usbd_get_buffer(c->url_xfer);
856 }
857 }
858
859 return 0;
860 }
861
862 Static void
863 url_start(struct ifnet *ifp)
864 {
865 struct url_softc *sc = ifp->if_softc;
866 struct mbuf *m_head = NULL;
867
868 DPRINTF(("%s: %s: enter, link=%d\n", device_xname(sc->sc_dev),
869 __func__, sc->sc_link));
870
871 if (sc->sc_dying)
872 return;
873
874 if (!sc->sc_link)
875 return;
876
877 if (ifp->if_flags & IFF_OACTIVE)
878 return;
879
880 IFQ_POLL(&ifp->if_snd, m_head);
881 if (m_head == NULL)
882 return;
883
884 if (url_send(sc, m_head, 0)) {
885 ifp->if_flags |= IFF_OACTIVE;
886 return;
887 }
888
889 IFQ_DEQUEUE(&ifp->if_snd, m_head);
890
891 bpf_mtap(ifp, m_head, BPF_D_OUT);
892
893 ifp->if_flags |= IFF_OACTIVE;
894
895 /* Set a timeout in case the chip goes out to lunch. */
896 ifp->if_timer = 5;
897 }
898
899 Static int
900 url_send(struct url_softc *sc, struct mbuf *m, int idx)
901 {
902 int total_len;
903 struct url_chain *c;
904 usbd_status err;
905
906 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
907
908 c = &sc->sc_cdata.url_tx_chain[idx];
909
910 /* Copy the mbuf data into a contiguous buffer */
911 m_copydata(m, 0, m->m_pkthdr.len, c->url_buf);
912 c->url_mbuf = m;
913 total_len = m->m_pkthdr.len;
914
915 if (total_len < URL_MIN_FRAME_LEN) {
916 memset(c->url_buf + total_len, 0,
917 URL_MIN_FRAME_LEN - total_len);
918 total_len = URL_MIN_FRAME_LEN;
919 }
920 usbd_setup_xfer(c->url_xfer,c, c->url_buf, total_len,
921 USBD_FORCE_SHORT_XFER, URL_TX_TIMEOUT, url_txeof);
922
923 /* Transmit */
924 sc->sc_refcnt++;
925 err = usbd_transfer(c->url_xfer);
926 if (--sc->sc_refcnt < 0)
927 usb_detach_wakeupold(sc->sc_dev);
928 if (err != USBD_IN_PROGRESS) {
929 printf("%s: url_send error=%s\n", device_xname(sc->sc_dev),
930 usbd_errstr(err));
931 /* Stop the interface */
932 usb_add_task(sc->sc_udev, &sc->sc_stop_task,
933 USB_TASKQ_DRIVER);
934 return EIO;
935 }
936
937 DPRINTF(("%s: %s: send %d bytes\n", device_xname(sc->sc_dev),
938 __func__, total_len));
939
940 sc->sc_cdata.url_tx_cnt++;
941
942 return 0;
943 }
944
945 Static void
946 url_txeof(struct usbd_xfer *xfer, void *priv,
947 usbd_status status)
948 {
949 struct url_chain *c = priv;
950 struct url_softc *sc = c->url_sc;
951 struct ifnet *ifp = GET_IFP(sc);
952 int s;
953
954 if (sc->sc_dying)
955 return;
956
957 s = splnet();
958
959 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
960
961 ifp->if_timer = 0;
962 ifp->if_flags &= ~IFF_OACTIVE;
963
964 if (status != USBD_NORMAL_COMPLETION) {
965 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
966 splx(s);
967 return;
968 }
969 ifp->if_oerrors++;
970 printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev),
971 usbd_errstr(status));
972 if (status == USBD_STALLED) {
973 sc->sc_refcnt++;
974 usbd_clear_endpoint_stall_async(sc->sc_pipe_tx);
975 if (--sc->sc_refcnt < 0)
976 usb_detach_wakeupold(sc->sc_dev);
977 }
978 splx(s);
979 return;
980 }
981
982 ifp->if_opackets++;
983
984 m_freem(c->url_mbuf);
985 c->url_mbuf = NULL;
986
987 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
988 url_start(ifp);
989
990 splx(s);
991 }
992
993 Static void
994 url_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
995 {
996 struct url_chain *c = priv;
997 struct url_softc *sc = c->url_sc;
998 struct ifnet *ifp = GET_IFP(sc);
999 struct mbuf *m;
1000 uint32_t total_len;
1001 url_rxhdr_t rxhdr;
1002 int s;
1003
1004 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
1005
1006 if (sc->sc_dying)
1007 return;
1008
1009 if (status != USBD_NORMAL_COMPLETION) {
1010 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1011 return;
1012 sc->sc_rx_errs++;
1013 if (usbd_ratecheck(&sc->sc_rx_notice)) {
1014 printf("%s: %u usb errors on rx: %s\n",
1015 device_xname(sc->sc_dev), sc->sc_rx_errs,
1016 usbd_errstr(status));
1017 sc->sc_rx_errs = 0;
1018 }
1019 if (status == USBD_STALLED) {
1020 sc->sc_refcnt++;
1021 usbd_clear_endpoint_stall_async(sc->sc_pipe_rx);
1022 if (--sc->sc_refcnt < 0)
1023 usb_detach_wakeupold(sc->sc_dev);
1024 }
1025 goto done;
1026 }
1027
1028 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1029
1030 memcpy(mtod(c->url_mbuf, char *), c->url_buf, total_len);
1031
1032 if (total_len <= ETHER_CRC_LEN) {
1033 ifp->if_ierrors++;
1034 goto done;
1035 }
1036
1037 memcpy(&rxhdr, c->url_buf + total_len - ETHER_CRC_LEN, sizeof(rxhdr));
1038
1039 DPRINTF(("%s: RX Status: %dbytes%s%s%s%s packets\n",
1040 device_xname(sc->sc_dev),
1041 UGETW(rxhdr) & URL_RXHDR_BYTEC_MASK,
1042 UGETW(rxhdr) & URL_RXHDR_VALID_MASK ? ", Valid" : "",
1043 UGETW(rxhdr) & URL_RXHDR_RUNTPKT_MASK ? ", Runt" : "",
1044 UGETW(rxhdr) & URL_RXHDR_PHYPKT_MASK ? ", Physical match" : "",
1045 UGETW(rxhdr) & URL_RXHDR_MCASTPKT_MASK ? ", Multicast" : ""));
1046
1047 if ((UGETW(rxhdr) & URL_RXHDR_VALID_MASK) == 0) {
1048 ifp->if_ierrors++;
1049 goto done;
1050 }
1051
1052 total_len -= ETHER_CRC_LEN;
1053
1054 m = c->url_mbuf;
1055 m->m_pkthdr.len = m->m_len = total_len;
1056 m_set_rcvif(m, ifp);
1057
1058 s = splnet();
1059
1060 if (url_newbuf(sc, c, NULL) == ENOBUFS) {
1061 ifp->if_ierrors++;
1062 goto done1;
1063 }
1064
1065 DPRINTF(("%s: %s: deliver %d\n", device_xname(sc->sc_dev),
1066 __func__, m->m_len));
1067 if_percpuq_enqueue((ifp)->if_percpuq, (m));
1068
1069 done1:
1070 splx(s);
1071
1072 done:
1073 /* Setup new transfer */
1074 usbd_setup_xfer(xfer, c, c->url_buf, URL_BUFSZ, USBD_SHORT_XFER_OK,
1075 USBD_NO_TIMEOUT, url_rxeof);
1076 sc->sc_refcnt++;
1077 usbd_transfer(xfer);
1078 if (--sc->sc_refcnt < 0)
1079 usb_detach_wakeupold(sc->sc_dev);
1080
1081 DPRINTF(("%s: %s: start rx\n", device_xname(sc->sc_dev), __func__));
1082 }
1083
1084 #if 0
1085 Static void url_intr(void)
1086 {
1087 }
1088 #endif
1089
1090 Static int
1091 url_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1092 {
1093 struct url_softc *sc = ifp->if_softc;
1094 int s, error = 0;
1095
1096 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1097
1098 if (sc->sc_dying)
1099 return EIO;
1100
1101 s = splnet();
1102
1103 error = ether_ioctl(ifp, cmd, data);
1104 if (error == ENETRESET) {
1105 if (ifp->if_flags & IFF_RUNNING)
1106 url_setmulti(sc);
1107 error = 0;
1108 }
1109
1110 splx(s);
1111
1112 return error;
1113 }
1114
1115 Static void
1116 url_watchdog(struct ifnet *ifp)
1117 {
1118 struct url_softc *sc = ifp->if_softc;
1119 struct url_chain *c;
1120 usbd_status stat;
1121 int s;
1122
1123 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1124
1125 ifp->if_oerrors++;
1126 printf("%s: watchdog timeout\n", device_xname(sc->sc_dev));
1127
1128 s = splusb();
1129 c = &sc->sc_cdata.url_tx_chain[0];
1130 usbd_get_xfer_status(c->url_xfer, NULL, NULL, NULL, &stat);
1131 url_txeof(c->url_xfer, c, stat);
1132
1133 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1134 url_start(ifp);
1135 splx(s);
1136 }
1137
1138 Static void
1139 url_stop_task(struct url_softc *sc)
1140 {
1141 url_stop(GET_IFP(sc), 1);
1142 }
1143
1144 /* Stop the adapter and free any mbufs allocated to the RX and TX lists. */
1145 Static void
1146 url_stop(struct ifnet *ifp, int disable)
1147 {
1148 struct url_softc *sc = ifp->if_softc;
1149 usbd_status err;
1150 int i;
1151
1152 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1153
1154 ifp->if_timer = 0;
1155
1156 url_reset(sc);
1157
1158 callout_stop(&sc->sc_stat_ch);
1159
1160 /* Stop transfers */
1161 /* RX endpoint */
1162 if (sc->sc_pipe_rx != NULL) {
1163 err = usbd_abort_pipe(sc->sc_pipe_rx);
1164 if (err)
1165 printf("%s: abort rx pipe failed: %s\n",
1166 device_xname(sc->sc_dev), usbd_errstr(err));
1167 }
1168
1169 /* TX endpoint */
1170 if (sc->sc_pipe_tx != NULL) {
1171 err = usbd_abort_pipe(sc->sc_pipe_tx);
1172 if (err)
1173 printf("%s: abort tx pipe failed: %s\n",
1174 device_xname(sc->sc_dev), usbd_errstr(err));
1175 }
1176
1177 #if 0
1178 /* XXX: Interrupt endpoint is not yet supported!! */
1179 /* Interrupt endpoint */
1180 if (sc->sc_pipe_intr != NULL) {
1181 err = usbd_abort_pipe(sc->sc_pipe_intr);
1182 if (err)
1183 printf("%s: abort intr pipe failed: %s\n",
1184 device_xname(sc->sc_dev), usbd_errstr(err));
1185 err = usbd_close_pipe(sc->sc_pipe_intr);
1186 if (err)
1187 printf("%s: close intr pipe failed: %s\n",
1188 device_xname(sc->sc_dev), usbd_errstr(err));
1189 sc->sc_pipe_intr = NULL;
1190 }
1191 #endif
1192
1193 /* Free RX resources. */
1194 for (i = 0; i < URL_RX_LIST_CNT; i++) {
1195 if (sc->sc_cdata.url_rx_chain[i].url_mbuf != NULL) {
1196 m_freem(sc->sc_cdata.url_rx_chain[i].url_mbuf);
1197 sc->sc_cdata.url_rx_chain[i].url_mbuf = NULL;
1198 }
1199 if (sc->sc_cdata.url_rx_chain[i].url_xfer != NULL) {
1200 usbd_destroy_xfer(sc->sc_cdata.url_rx_chain[i].url_xfer);
1201 sc->sc_cdata.url_rx_chain[i].url_xfer = NULL;
1202 }
1203 }
1204
1205 /* Free TX resources. */
1206 for (i = 0; i < URL_TX_LIST_CNT; i++) {
1207 if (sc->sc_cdata.url_tx_chain[i].url_mbuf != NULL) {
1208 m_freem(sc->sc_cdata.url_tx_chain[i].url_mbuf);
1209 sc->sc_cdata.url_tx_chain[i].url_mbuf = NULL;
1210 }
1211 if (sc->sc_cdata.url_tx_chain[i].url_xfer != NULL) {
1212 usbd_destroy_xfer(sc->sc_cdata.url_tx_chain[i].url_xfer);
1213 sc->sc_cdata.url_tx_chain[i].url_xfer = NULL;
1214 }
1215 }
1216
1217 /* Close pipes */
1218 /* RX endpoint */
1219 if (sc->sc_pipe_rx != NULL) {
1220 err = usbd_close_pipe(sc->sc_pipe_rx);
1221 if (err)
1222 printf("%s: close rx pipe failed: %s\n",
1223 device_xname(sc->sc_dev), usbd_errstr(err));
1224 sc->sc_pipe_rx = NULL;
1225 }
1226
1227 /* TX endpoint */
1228 if (sc->sc_pipe_tx != NULL) {
1229 err = usbd_close_pipe(sc->sc_pipe_tx);
1230 if (err)
1231 printf("%s: close tx pipe failed: %s\n",
1232 device_xname(sc->sc_dev), usbd_errstr(err));
1233 sc->sc_pipe_tx = NULL;
1234 }
1235
1236 sc->sc_link = 0;
1237 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1238 }
1239
1240 /* Set media options */
1241 Static int
1242 url_ifmedia_change(struct ifnet *ifp)
1243 {
1244 struct url_softc *sc = ifp->if_softc;
1245 struct mii_data *mii = GET_MII(sc);
1246 int rc;
1247
1248 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1249
1250 if (sc->sc_dying)
1251 return 0;
1252
1253 sc->sc_link = 0;
1254 if ((rc = mii_mediachg(mii)) == ENXIO)
1255 return 0;
1256 return rc;
1257 }
1258
1259 /* Report current media status. */
1260 Static void
1261 url_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1262 {
1263 struct url_softc *sc = ifp->if_softc;
1264
1265 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1266
1267 if (sc->sc_dying)
1268 return;
1269
1270 ether_mediastatus(ifp, ifmr);
1271 }
1272
1273 Static void
1274 url_tick(void *xsc)
1275 {
1276 struct url_softc *sc = xsc;
1277
1278 if (sc == NULL)
1279 return;
1280
1281 DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev),
1282 __func__));
1283
1284 if (sc->sc_dying)
1285 return;
1286
1287 /* Perform periodic stuff in process context */
1288 usb_add_task(sc->sc_udev, &sc->sc_tick_task, USB_TASKQ_DRIVER);
1289 }
1290
1291 Static void
1292 url_tick_task(void *xsc)
1293 {
1294 struct url_softc *sc = xsc;
1295 struct ifnet *ifp;
1296 struct mii_data *mii;
1297 int s;
1298
1299 if (sc == NULL)
1300 return;
1301
1302 DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev),
1303 __func__));
1304
1305 if (sc->sc_dying)
1306 return;
1307
1308 ifp = GET_IFP(sc);
1309 mii = GET_MII(sc);
1310
1311 if (mii == NULL)
1312 return;
1313
1314 s = splnet();
1315
1316 mii_tick(mii);
1317 if (!sc->sc_link) {
1318 mii_pollstat(mii);
1319 if (mii->mii_media_status & IFM_ACTIVE &&
1320 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1321 DPRINTF(("%s: %s: got link\n",
1322 device_xname(sc->sc_dev), __func__));
1323 sc->sc_link++;
1324 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1325 url_start(ifp);
1326 }
1327 }
1328
1329 callout_reset(&sc->sc_stat_ch, hz, url_tick, sc);
1330
1331 splx(s);
1332 }
1333
1334 /* Get exclusive access to the MII registers */
1335 Static void
1336 url_lock_mii(struct url_softc *sc)
1337 {
1338 DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev),
1339 __func__));
1340
1341 sc->sc_refcnt++;
1342 rw_enter(&sc->sc_mii_rwlock, RW_WRITER);
1343 }
1344
1345 Static void
1346 url_unlock_mii(struct url_softc *sc)
1347 {
1348 DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev),
1349 __func__));
1350
1351 rw_exit(&sc->sc_mii_rwlock);
1352 if (--sc->sc_refcnt < 0)
1353 usb_detach_wakeupold(sc->sc_dev);
1354 }
1355
1356 Static int
1357 url_int_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val)
1358 {
1359 struct url_softc *sc;
1360 uint16_t data;
1361 int rv = 0;
1362
1363 if (dev == NULL)
1364 return 0;
1365
1366 sc = device_private(dev);
1367
1368 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n",
1369 device_xname(sc->sc_dev), __func__, phy, reg));
1370
1371 if (sc->sc_dying) {
1372 #ifdef DIAGNOSTIC
1373 printf("%s: %s: dying\n", device_xname(sc->sc_dev),
1374 __func__);
1375 #endif
1376 return -1;
1377 }
1378
1379 /* XXX: one PHY only for the RTL8150 internal PHY */
1380 if (phy != 0) {
1381 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
1382 device_xname(sc->sc_dev), __func__, phy));
1383 return -1;
1384 }
1385
1386 url_lock_mii(sc);
1387
1388 switch (reg) {
1389 case MII_BMCR: /* Control Register */
1390 reg = URL_BMCR;
1391 break;
1392 case MII_BMSR: /* Status Register */
1393 reg = URL_BMSR;
1394 break;
1395 case MII_PHYIDR1:
1396 case MII_PHYIDR2:
1397 *val = 0;
1398 goto R_DONE;
1399 break;
1400 case MII_ANAR: /* Autonegotiation advertisement */
1401 reg = URL_ANAR;
1402 break;
1403 case MII_ANLPAR: /* Autonegotiation link partner abilities */
1404 reg = URL_ANLP;
1405 break;
1406 case URLPHY_MSR: /* Media Status Register */
1407 reg = URL_MSR;
1408 break;
1409 default:
1410 printf("%s: %s: bad register %04x\n",
1411 device_xname(sc->sc_dev), __func__, reg);
1412 rv = -1;
1413 goto R_DONE;
1414 break;
1415 }
1416
1417 if (reg == URL_MSR)
1418 data = url_csr_read_1(sc, reg);
1419 else
1420 data = url_csr_read_2(sc, reg);
1421 *val = data;
1422
1423 R_DONE:
1424 DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04hx\n",
1425 device_xname(sc->sc_dev), __func__, phy, reg, *val));
1426
1427 url_unlock_mii(sc);
1428 return rv;
1429 }
1430
1431 Static int
1432 url_int_miibus_writereg(device_t dev, int phy, int reg, uint16_t val)
1433 {
1434 struct url_softc *sc;
1435 int rv = 0;
1436
1437 if (dev == NULL)
1438 return -1;
1439
1440 sc = device_private(dev);
1441
1442 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x val=0x%04hx\n",
1443 device_xname(sc->sc_dev), __func__, phy, reg, val));
1444
1445 if (sc->sc_dying) {
1446 #ifdef DIAGNOSTIC
1447 printf("%s: %s: dying\n", device_xname(sc->sc_dev),
1448 __func__);
1449 #endif
1450 return -1;
1451 }
1452
1453 /* XXX: one PHY only for the RTL8150 internal PHY */
1454 if (phy != 0) {
1455 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
1456 device_xname(sc->sc_dev), __func__, phy));
1457 return -1;
1458 }
1459
1460 url_lock_mii(sc);
1461
1462 switch (reg) {
1463 case MII_BMCR: /* Control Register */
1464 reg = URL_BMCR;
1465 break;
1466 case MII_BMSR: /* Status Register */
1467 reg = URL_BMSR;
1468 break;
1469 case MII_PHYIDR1:
1470 case MII_PHYIDR2:
1471 goto W_DONE;
1472 break;
1473 case MII_ANAR: /* Autonegotiation advertisement */
1474 reg = URL_ANAR;
1475 break;
1476 case MII_ANLPAR: /* Autonegotiation link partner abilities */
1477 reg = URL_ANLP;
1478 break;
1479 case URLPHY_MSR: /* Media Status Register */
1480 reg = URL_MSR;
1481 break;
1482 default:
1483 printf("%s: %s: bad register %04x\n",
1484 device_xname(sc->sc_dev), __func__, reg);
1485 rv = -1;
1486 goto W_DONE;
1487 break;
1488 }
1489
1490 if (reg == URL_MSR)
1491 url_csr_write_1(sc, reg, val);
1492 else
1493 url_csr_write_2(sc, reg, val);
1494 W_DONE:
1495
1496 url_unlock_mii(sc);
1497 return rv;
1498 }
1499
1500 Static void
1501 url_miibus_statchg(struct ifnet *ifp)
1502 {
1503 #ifdef URL_DEBUG
1504 if (ifp == NULL)
1505 return;
1506
1507 DPRINTF(("%s: %s: enter\n", ifp->if_xname, __func__));
1508 #endif
1509 /* Nothing to do */
1510 }
1511
1512 #if 0
1513 /*
1514 * external PHYs support, but not test.
1515 */
1516 Static int
1517 url_ext_miibus_redreg(device_t dev, int phy, int reg)
1518 {
1519 struct url_softc *sc = device_private(dev);
1520 uint16_t val;
1521
1522 DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x\n",
1523 device_xname(sc->sc_dev), __func__, phy, reg));
1524
1525 if (sc->sc_dying) {
1526 #ifdef DIAGNOSTIC
1527 printf("%s: %s: dying\n", device_xname(sc->sc_dev),
1528 __func__);
1529 #endif
1530 return 0;
1531 }
1532
1533 url_lock_mii(sc);
1534
1535 url_csr_write_1(sc, URL_PHYADD, phy & URL_PHYADD_MASK);
1536 /*
1537 * RTL8150L will initiate a MII management data transaction
1538 * if PHYCNT_OWN bit is set 1 by software. After transaction,
1539 * this bit is auto cleared by TRL8150L.
1540 */
1541 url_csr_write_1(sc, URL_PHYCNT,
1542 (reg | URL_PHYCNT_PHYOWN) & ~URL_PHYCNT_RWCR);
1543 for (i = 0; i < URL_TIMEOUT; i++) {
1544 if ((url_csr_read_1(sc, URL_PHYCNT) & URL_PHYCNT_PHYOWN) == 0)
1545 break;
1546 }
1547 if (i == URL_TIMEOUT) {
1548 printf("%s: MII read timed out\n", device_xname(sc->sc_dev));
1549 }
1550
1551 val = url_csr_read_2(sc, URL_PHYDAT);
1552
1553 DPRINTF(("%s: %s: phy=%d reg=0x%04x => 0x%04x\n",
1554 device_xname(sc->sc_dev), __func__, phy, reg, val));
1555
1556 url_unlock_mii(sc);
1557 return val;
1558 }
1559
1560 Static void
1561 url_ext_miibus_writereg(device_t dev, int phy, int reg, int data)
1562 {
1563 struct url_softc *sc = device_private(dev);
1564
1565 DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n",
1566 device_xname(sc->sc_dev), __func__, phy, reg, data));
1567
1568 if (sc->sc_dying) {
1569 #ifdef DIAGNOSTIC
1570 printf("%s: %s: dying\n", device_xname(sc->sc_dev),
1571 __func__);
1572 #endif
1573 return;
1574 }
1575
1576 url_lock_mii(sc);
1577
1578 url_csr_write_2(sc, URL_PHYDAT, data);
1579 url_csr_write_1(sc, URL_PHYADD, phy);
1580 url_csr_write_1(sc, URL_PHYCNT, reg | URL_PHYCNT_RWCR); /* Write */
1581
1582 for (i=0; i < URL_TIMEOUT; i++) {
1583 if (url_csr_read_1(sc, URL_PHYCNT) & URL_PHYCNT_PHYOWN)
1584 break;
1585 }
1586
1587 if (i == URL_TIMEOUT) {
1588 printf("%s: MII write timed out\n",
1589 device_xname(sc->sc_dev));
1590 }
1591
1592 url_unlock_mii(sc);
1593 return;
1594 }
1595 #endif
1596