if_smsc.c revision 1.7.6.3 1 1.7.6.2 tls /* $NetBSD: if_smsc.c,v 1.7.6.3 2013/06/23 06:20:22 tls Exp $ */
2 1.7.6.2 tls
3 1.7.6.2 tls /* $OpenBSD: if_smsc.c,v 1.4 2012/09/27 12:38:11 jsg Exp $ */
4 1.7.6.2 tls /* $FreeBSD: src/sys/dev/usb/net/if_smsc.c,v 1.1 2012/08/15 04:03:55 gonzo Exp $ */
5 1.7.6.2 tls /*-
6 1.7.6.2 tls * Copyright (c) 2012
7 1.7.6.2 tls * Ben Gray <bgray (at) freebsd.org>.
8 1.7.6.2 tls * All rights reserved.
9 1.7.6.2 tls *
10 1.7.6.2 tls * Redistribution and use in source and binary forms, with or without
11 1.7.6.2 tls * modification, are permitted provided that the following conditions
12 1.7.6.2 tls * are met:
13 1.7.6.2 tls * 1. Redistributions of source code must retain the above copyright
14 1.7.6.2 tls * notice, this list of conditions and the following disclaimer.
15 1.7.6.2 tls * 2. Redistributions in binary form must reproduce the above copyright
16 1.7.6.2 tls * notice, this list of conditions and the following disclaimer in the
17 1.7.6.2 tls * documentation and/or other materials provided with the distribution.
18 1.7.6.2 tls *
19 1.7.6.2 tls * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 1.7.6.2 tls * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.7.6.2 tls * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.7.6.2 tls * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.7.6.2 tls * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 1.7.6.2 tls * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 1.7.6.2 tls * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 1.7.6.2 tls * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 1.7.6.2 tls * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 1.7.6.2 tls * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 1.7.6.2 tls */
30 1.7.6.2 tls
31 1.7.6.2 tls /*
32 1.7.6.2 tls * SMSC LAN9xxx devices (http://www.smsc.com/)
33 1.7.6.2 tls *
34 1.7.6.2 tls * The LAN9500 & LAN9500A devices are stand-alone USB to Ethernet chips that
35 1.7.6.2 tls * support USB 2.0 and 10/100 Mbps Ethernet.
36 1.7.6.2 tls *
37 1.7.6.2 tls * The LAN951x devices are an integrated USB hub and USB to Ethernet adapter.
38 1.7.6.2 tls * The driver only covers the Ethernet part, the standard USB hub driver
39 1.7.6.2 tls * supports the hub part.
40 1.7.6.2 tls *
41 1.7.6.2 tls * This driver is closely modelled on the Linux driver written and copyrighted
42 1.7.6.2 tls * by SMSC.
43 1.7.6.2 tls *
44 1.7.6.2 tls * H/W TCP & UDP Checksum Offloading
45 1.7.6.2 tls * ---------------------------------
46 1.7.6.2 tls * The chip supports both tx and rx offloading of UDP & TCP checksums, this
47 1.7.6.2 tls * feature can be dynamically enabled/disabled.
48 1.7.6.2 tls *
49 1.7.6.2 tls * RX checksuming is performed across bytes after the IPv4 header to the end of
50 1.7.6.2 tls * the Ethernet frame, this means if the frame is padded with non-zero values
51 1.7.6.2 tls * the H/W checksum will be incorrect, however the rx code compensates for this.
52 1.7.6.2 tls *
53 1.7.6.2 tls * TX checksuming is more complicated, the device requires a special header to
54 1.7.6.2 tls * be prefixed onto the start of the frame which indicates the start and end
55 1.7.6.2 tls * positions of the UDP or TCP frame. This requires the driver to manually
56 1.7.6.2 tls * go through the packet data and decode the headers prior to sending.
57 1.7.6.2 tls * On Linux they generally provide cues to the location of the csum and the
58 1.7.6.2 tls * area to calculate it over, on FreeBSD we seem to have to do it all ourselves,
59 1.7.6.3 tls * hence this is not as optimal and therefore h/w TX checksum is currently not
60 1.7.6.2 tls * implemented.
61 1.7.6.2 tls */
62 1.7.6.2 tls
63 1.7.6.2 tls #include "vlan.h"
64 1.7.6.2 tls #include "opt_usb.h"
65 1.7.6.2 tls
66 1.7.6.2 tls #include <sys/param.h>
67 1.7.6.2 tls #include <sys/bus.h>
68 1.7.6.2 tls #include <sys/systm.h>
69 1.7.6.2 tls #include <sys/sockio.h>
70 1.7.6.2 tls #include <sys/mbuf.h>
71 1.7.6.2 tls #include <sys/mutex.h>
72 1.7.6.2 tls #include <sys/kernel.h>
73 1.7.6.2 tls #include <sys/proc.h>
74 1.7.6.2 tls #include <sys/socket.h>
75 1.7.6.2 tls
76 1.7.6.2 tls #include <sys/device.h>
77 1.7.6.2 tls
78 1.7.6.2 tls #include <sys/rnd.h>
79 1.7.6.2 tls
80 1.7.6.2 tls #include <net/if.h>
81 1.7.6.2 tls #include <net/if_dl.h>
82 1.7.6.2 tls #include <net/if_media.h>
83 1.7.6.2 tls #include <net/if_ether.h>
84 1.7.6.2 tls
85 1.7.6.2 tls #include <net/bpf.h>
86 1.7.6.2 tls
87 1.7.6.2 tls #ifdef INET
88 1.7.6.2 tls #include <netinet/in.h>
89 1.7.6.2 tls #include <netinet/in_systm.h>
90 1.7.6.2 tls #include <netinet/in_var.h>
91 1.7.6.2 tls #include <netinet/ip.h>
92 1.7.6.2 tls #include <netinet/if_ether.h>
93 1.7.6.2 tls #endif
94 1.7.6.2 tls
95 1.7.6.2 tls #include <dev/mii/mii.h>
96 1.7.6.2 tls #include <dev/mii/miivar.h>
97 1.7.6.2 tls
98 1.7.6.2 tls #include <dev/usb/usb.h>
99 1.7.6.2 tls #include <dev/usb/usbdi.h>
100 1.7.6.2 tls #include <dev/usb/usbdi_util.h>
101 1.7.6.2 tls #include <dev/usb/usbdivar.h>
102 1.7.6.2 tls #include <dev/usb/usbdevs.h>
103 1.7.6.2 tls
104 1.7.6.2 tls #include <dev/usb/if_smscreg.h>
105 1.7.6.2 tls #include <dev/usb/if_smscvar.h>
106 1.7.6.2 tls
107 1.7.6.2 tls #include "ioconf.h"
108 1.7.6.2 tls
109 1.7.6.2 tls #ifdef USB_DEBUG
110 1.7.6.2 tls int smsc_debug = 0;
111 1.7.6.2 tls #endif
112 1.7.6.2 tls
113 1.7.6.2 tls /*
114 1.7.6.2 tls * Various supported device vendors/products.
115 1.7.6.2 tls */
116 1.7.6.2 tls static const struct usb_devno smsc_devs[] = {
117 1.7.6.2 tls { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN89530 },
118 1.7.6.2 tls { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN9530 },
119 1.7.6.2 tls { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN9730 },
120 1.7.6.2 tls { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500 },
121 1.7.6.2 tls { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500A },
122 1.7.6.2 tls { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500A_ALT },
123 1.7.6.2 tls { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500A_HAL },
124 1.7.6.2 tls { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500A_SAL10 },
125 1.7.6.2 tls { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500_ALT },
126 1.7.6.2 tls { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500_SAL10 },
127 1.7.6.2 tls { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9505 },
128 1.7.6.2 tls { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9505A },
129 1.7.6.2 tls { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9505A_HAL },
130 1.7.6.2 tls { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9505A_SAL10 },
131 1.7.6.2 tls { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9505_SAL10 },
132 1.7.6.2 tls { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9512_14 },
133 1.7.6.2 tls { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9512_14_ALT },
134 1.7.6.2 tls { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9512_14_SAL10 }
135 1.7.6.2 tls };
136 1.7.6.2 tls
137 1.7.6.2 tls #ifdef USB_DEBUG
138 1.7.6.2 tls #define smsc_dbg_printf(sc, fmt, args...) \
139 1.7.6.2 tls do { \
140 1.7.6.2 tls if (smsc_debug > 0) \
141 1.7.6.2 tls printf("debug: " fmt, ##args); \
142 1.7.6.2 tls } while(0)
143 1.7.6.2 tls #else
144 1.7.6.2 tls #define smsc_dbg_printf(sc, fmt, args...)
145 1.7.6.2 tls #endif
146 1.7.6.2 tls
147 1.7.6.2 tls #define smsc_warn_printf(sc, fmt, args...) \
148 1.7.6.2 tls printf("%s: warning: " fmt, device_xname((sc)->sc_dev), ##args)
149 1.7.6.2 tls
150 1.7.6.2 tls #define smsc_err_printf(sc, fmt, args...) \
151 1.7.6.2 tls printf("%s: error: " fmt, device_xname((sc)->sc_dev), ##args)
152 1.7.6.2 tls
153 1.7.6.2 tls /* Function declarations */
154 1.7.6.2 tls int smsc_chip_init(struct smsc_softc *);
155 1.7.6.2 tls void smsc_setmulti(struct smsc_softc *);
156 1.7.6.2 tls int smsc_setmacaddress(struct smsc_softc *, const uint8_t *);
157 1.7.6.2 tls
158 1.7.6.2 tls int smsc_match(device_t, cfdata_t, void *);
159 1.7.6.2 tls void smsc_attach(device_t, device_t, void *);
160 1.7.6.2 tls int smsc_detach(device_t, int);
161 1.7.6.2 tls int smsc_activate(device_t, enum devact);
162 1.7.6.2 tls
163 1.7.6.2 tls int smsc_init(struct ifnet *);
164 1.7.6.2 tls void smsc_start(struct ifnet *);
165 1.7.6.2 tls int smsc_ioctl(struct ifnet *, u_long, void *);
166 1.7.6.2 tls void smsc_stop(struct ifnet *, int);
167 1.7.6.2 tls
168 1.7.6.2 tls void smsc_reset(struct smsc_softc *);
169 1.7.6.2 tls struct mbuf *smsc_newbuf(void);
170 1.7.6.2 tls
171 1.7.6.2 tls void smsc_tick(void *);
172 1.7.6.2 tls void smsc_tick_task(void *);
173 1.7.6.2 tls void smsc_miibus_statchg(struct ifnet *);
174 1.7.6.2 tls int smsc_miibus_readreg(device_t, int, int);
175 1.7.6.2 tls void smsc_miibus_writereg(device_t, int, int, int);
176 1.7.6.2 tls int smsc_ifmedia_upd(struct ifnet *);
177 1.7.6.2 tls void smsc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
178 1.7.6.2 tls void smsc_lock_mii(struct smsc_softc *);
179 1.7.6.2 tls void smsc_unlock_mii(struct smsc_softc *);
180 1.7.6.2 tls
181 1.7.6.2 tls int smsc_tx_list_init(struct smsc_softc *);
182 1.7.6.2 tls int smsc_rx_list_init(struct smsc_softc *);
183 1.7.6.2 tls int smsc_encap(struct smsc_softc *, struct mbuf *, int);
184 1.7.6.2 tls void smsc_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
185 1.7.6.2 tls void smsc_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
186 1.7.6.2 tls
187 1.7.6.2 tls int smsc_read_reg(struct smsc_softc *, uint32_t, uint32_t *);
188 1.7.6.2 tls int smsc_write_reg(struct smsc_softc *, uint32_t, uint32_t);
189 1.7.6.2 tls int smsc_wait_for_bits(struct smsc_softc *, uint32_t, uint32_t);
190 1.7.6.2 tls int smsc_sethwcsum(struct smsc_softc *);
191 1.7.6.2 tls
192 1.7.6.2 tls CFATTACH_DECL_NEW(usmsc, sizeof(struct smsc_softc), smsc_match, smsc_attach,
193 1.7.6.2 tls smsc_detach, smsc_activate);
194 1.7.6.2 tls
195 1.7.6.2 tls int
196 1.7.6.2 tls smsc_read_reg(struct smsc_softc *sc, uint32_t off, uint32_t *data)
197 1.7.6.2 tls {
198 1.7.6.2 tls usb_device_request_t req;
199 1.7.6.2 tls uint32_t buf;
200 1.7.6.2 tls usbd_status err;
201 1.7.6.2 tls
202 1.7.6.2 tls req.bmRequestType = UT_READ_VENDOR_DEVICE;
203 1.7.6.2 tls req.bRequest = SMSC_UR_READ_REG;
204 1.7.6.2 tls USETW(req.wValue, 0);
205 1.7.6.2 tls USETW(req.wIndex, off);
206 1.7.6.2 tls USETW(req.wLength, 4);
207 1.7.6.2 tls
208 1.7.6.2 tls err = usbd_do_request(sc->sc_udev, &req, &buf);
209 1.7.6.2 tls if (err != 0)
210 1.7.6.2 tls smsc_warn_printf(sc, "Failed to read register 0x%0x\n", off);
211 1.7.6.2 tls
212 1.7.6.2 tls *data = le32toh(buf);
213 1.7.6.2 tls
214 1.7.6.2 tls return (err);
215 1.7.6.2 tls }
216 1.7.6.2 tls
217 1.7.6.2 tls int
218 1.7.6.2 tls smsc_write_reg(struct smsc_softc *sc, uint32_t off, uint32_t data)
219 1.7.6.2 tls {
220 1.7.6.2 tls usb_device_request_t req;
221 1.7.6.2 tls uint32_t buf;
222 1.7.6.2 tls usbd_status err;
223 1.7.6.2 tls
224 1.7.6.2 tls buf = htole32(data);
225 1.7.6.2 tls
226 1.7.6.2 tls req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
227 1.7.6.2 tls req.bRequest = SMSC_UR_WRITE_REG;
228 1.7.6.2 tls USETW(req.wValue, 0);
229 1.7.6.2 tls USETW(req.wIndex, off);
230 1.7.6.2 tls USETW(req.wLength, 4);
231 1.7.6.2 tls
232 1.7.6.2 tls err = usbd_do_request(sc->sc_udev, &req, &buf);
233 1.7.6.2 tls if (err != 0)
234 1.7.6.2 tls smsc_warn_printf(sc, "Failed to write register 0x%0x\n", off);
235 1.7.6.2 tls
236 1.7.6.2 tls return (err);
237 1.7.6.2 tls }
238 1.7.6.2 tls
239 1.7.6.2 tls int
240 1.7.6.2 tls smsc_wait_for_bits(struct smsc_softc *sc, uint32_t reg, uint32_t bits)
241 1.7.6.2 tls {
242 1.7.6.2 tls uint32_t val;
243 1.7.6.2 tls int err, i;
244 1.7.6.2 tls
245 1.7.6.2 tls for (i = 0; i < 100; i++) {
246 1.7.6.2 tls if ((err = smsc_read_reg(sc, reg, &val)) != 0)
247 1.7.6.2 tls return (err);
248 1.7.6.2 tls if (!(val & bits))
249 1.7.6.2 tls return (0);
250 1.7.6.2 tls DELAY(5);
251 1.7.6.2 tls }
252 1.7.6.2 tls
253 1.7.6.2 tls return (1);
254 1.7.6.2 tls }
255 1.7.6.2 tls
256 1.7.6.2 tls int
257 1.7.6.2 tls smsc_miibus_readreg(device_t dev, int phy, int reg)
258 1.7.6.2 tls {
259 1.7.6.2 tls struct smsc_softc *sc = device_private(dev);
260 1.7.6.2 tls uint32_t addr;
261 1.7.6.2 tls uint32_t val = 0;
262 1.7.6.2 tls
263 1.7.6.2 tls smsc_lock_mii(sc);
264 1.7.6.2 tls if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
265 1.7.6.2 tls smsc_warn_printf(sc, "MII is busy\n");
266 1.7.6.2 tls goto done;
267 1.7.6.2 tls }
268 1.7.6.2 tls
269 1.7.6.2 tls addr = (phy << 11) | (reg << 6) | SMSC_MII_READ;
270 1.7.6.2 tls smsc_write_reg(sc, SMSC_MII_ADDR, addr);
271 1.7.6.2 tls
272 1.7.6.2 tls if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
273 1.7.6.2 tls smsc_warn_printf(sc, "MII read timeout\n");
274 1.7.6.2 tls
275 1.7.6.2 tls smsc_read_reg(sc, SMSC_MII_DATA, &val);
276 1.7.6.2 tls
277 1.7.6.2 tls done:
278 1.7.6.2 tls smsc_unlock_mii(sc);
279 1.7.6.2 tls
280 1.7.6.2 tls return (val & 0xFFFF);
281 1.7.6.2 tls }
282 1.7.6.2 tls
283 1.7.6.2 tls void
284 1.7.6.2 tls smsc_miibus_writereg(device_t dev, int phy, int reg, int val)
285 1.7.6.2 tls {
286 1.7.6.2 tls struct smsc_softc *sc = device_private(dev);
287 1.7.6.2 tls uint32_t addr;
288 1.7.6.2 tls
289 1.7.6.2 tls if (sc->sc_phyno != phy)
290 1.7.6.2 tls return;
291 1.7.6.2 tls
292 1.7.6.2 tls smsc_lock_mii(sc);
293 1.7.6.2 tls if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
294 1.7.6.2 tls smsc_warn_printf(sc, "MII is busy\n");
295 1.7.6.2 tls smsc_unlock_mii(sc);
296 1.7.6.2 tls return;
297 1.7.6.2 tls }
298 1.7.6.2 tls
299 1.7.6.2 tls smsc_write_reg(sc, SMSC_MII_DATA, val);
300 1.7.6.2 tls
301 1.7.6.2 tls addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE;
302 1.7.6.2 tls smsc_write_reg(sc, SMSC_MII_ADDR, addr);
303 1.7.6.2 tls smsc_unlock_mii(sc);
304 1.7.6.2 tls
305 1.7.6.2 tls if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
306 1.7.6.2 tls smsc_warn_printf(sc, "MII write timeout\n");
307 1.7.6.2 tls }
308 1.7.6.2 tls
309 1.7.6.2 tls void
310 1.7.6.2 tls smsc_miibus_statchg(struct ifnet *ifp)
311 1.7.6.2 tls {
312 1.7.6.2 tls struct smsc_softc *sc = ifp->if_softc;
313 1.7.6.2 tls struct mii_data *mii = &sc->sc_mii;
314 1.7.6.2 tls int err;
315 1.7.6.2 tls uint32_t flow;
316 1.7.6.2 tls uint32_t afc_cfg;
317 1.7.6.2 tls
318 1.7.6.2 tls if (mii == NULL || ifp == NULL ||
319 1.7.6.2 tls (ifp->if_flags & IFF_RUNNING) == 0)
320 1.7.6.2 tls return;
321 1.7.6.2 tls
322 1.7.6.2 tls /* Use the MII status to determine link status */
323 1.7.6.2 tls sc->sc_flags &= ~SMSC_FLAG_LINK;
324 1.7.6.2 tls if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
325 1.7.6.2 tls (IFM_ACTIVE | IFM_AVALID)) {
326 1.7.6.2 tls switch (IFM_SUBTYPE(mii->mii_media_active)) {
327 1.7.6.2 tls case IFM_10_T:
328 1.7.6.2 tls case IFM_100_TX:
329 1.7.6.2 tls sc->sc_flags |= SMSC_FLAG_LINK;
330 1.7.6.2 tls break;
331 1.7.6.2 tls case IFM_1000_T:
332 1.7.6.2 tls /* Gigabit ethernet not supported by chipset */
333 1.7.6.2 tls break;
334 1.7.6.2 tls default:
335 1.7.6.2 tls break;
336 1.7.6.2 tls }
337 1.7.6.2 tls }
338 1.7.6.2 tls
339 1.7.6.2 tls /* Lost link, do nothing. */
340 1.7.6.2 tls if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
341 1.7.6.2 tls smsc_dbg_printf(sc, "link flag not set\n");
342 1.7.6.2 tls return;
343 1.7.6.2 tls }
344 1.7.6.2 tls
345 1.7.6.2 tls err = smsc_read_reg(sc, SMSC_AFC_CFG, &afc_cfg);
346 1.7.6.2 tls if (err) {
347 1.7.6.2 tls smsc_warn_printf(sc, "failed to read initial AFC_CFG, "
348 1.7.6.2 tls "error %d\n", err);
349 1.7.6.2 tls return;
350 1.7.6.2 tls }
351 1.7.6.2 tls
352 1.7.6.2 tls /* Enable/disable full duplex operation and TX/RX pause */
353 1.7.6.2 tls if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
354 1.7.6.2 tls smsc_dbg_printf(sc, "full duplex operation\n");
355 1.7.6.2 tls sc->sc_mac_csr &= ~SMSC_MAC_CSR_RCVOWN;
356 1.7.6.2 tls sc->sc_mac_csr |= SMSC_MAC_CSR_FDPX;
357 1.7.6.2 tls
358 1.7.6.2 tls if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
359 1.7.6.2 tls flow = 0xffff0002;
360 1.7.6.2 tls else
361 1.7.6.2 tls flow = 0;
362 1.7.6.2 tls
363 1.7.6.2 tls if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
364 1.7.6.2 tls afc_cfg |= 0xf;
365 1.7.6.2 tls else
366 1.7.6.2 tls afc_cfg &= ~0xf;
367 1.7.6.2 tls
368 1.7.6.2 tls } else {
369 1.7.6.2 tls smsc_dbg_printf(sc, "half duplex operation\n");
370 1.7.6.2 tls sc->sc_mac_csr &= ~SMSC_MAC_CSR_FDPX;
371 1.7.6.2 tls sc->sc_mac_csr |= SMSC_MAC_CSR_RCVOWN;
372 1.7.6.2 tls
373 1.7.6.2 tls flow = 0;
374 1.7.6.2 tls afc_cfg |= 0xf;
375 1.7.6.2 tls }
376 1.7.6.2 tls
377 1.7.6.2 tls err = smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
378 1.7.6.2 tls err += smsc_write_reg(sc, SMSC_FLOW, flow);
379 1.7.6.2 tls err += smsc_write_reg(sc, SMSC_AFC_CFG, afc_cfg);
380 1.7.6.2 tls if (err)
381 1.7.6.2 tls smsc_warn_printf(sc, "media change failed, error %d\n", err);
382 1.7.6.2 tls }
383 1.7.6.2 tls
384 1.7.6.2 tls int
385 1.7.6.2 tls smsc_ifmedia_upd(struct ifnet *ifp)
386 1.7.6.2 tls {
387 1.7.6.2 tls struct smsc_softc *sc = ifp->if_softc;
388 1.7.6.2 tls struct mii_data *mii = &sc->sc_mii;
389 1.7.6.2 tls int err;
390 1.7.6.2 tls
391 1.7.6.2 tls if (mii->mii_instance) {
392 1.7.6.2 tls struct mii_softc *miisc;
393 1.7.6.2 tls
394 1.7.6.2 tls LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
395 1.7.6.2 tls mii_phy_reset(miisc);
396 1.7.6.2 tls }
397 1.7.6.2 tls err = mii_mediachg(mii);
398 1.7.6.2 tls return (err);
399 1.7.6.2 tls }
400 1.7.6.2 tls
401 1.7.6.2 tls void
402 1.7.6.2 tls smsc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
403 1.7.6.2 tls {
404 1.7.6.2 tls struct smsc_softc *sc = ifp->if_softc;
405 1.7.6.2 tls struct mii_data *mii = &sc->sc_mii;
406 1.7.6.2 tls
407 1.7.6.2 tls mii_pollstat(mii);
408 1.7.6.2 tls
409 1.7.6.2 tls ifmr->ifm_active = mii->mii_media_active;
410 1.7.6.2 tls ifmr->ifm_status = mii->mii_media_status;
411 1.7.6.2 tls }
412 1.7.6.2 tls
413 1.7.6.2 tls static inline uint32_t
414 1.7.6.2 tls smsc_hash(uint8_t addr[ETHER_ADDR_LEN])
415 1.7.6.2 tls {
416 1.7.6.2 tls return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 26) & 0x3f;
417 1.7.6.2 tls }
418 1.7.6.2 tls
419 1.7.6.2 tls void
420 1.7.6.2 tls smsc_setmulti(struct smsc_softc *sc)
421 1.7.6.2 tls {
422 1.7.6.2 tls struct ifnet *ifp = &sc->sc_ec.ec_if;
423 1.7.6.2 tls struct ether_multi *enm;
424 1.7.6.2 tls struct ether_multistep step;
425 1.7.6.2 tls uint32_t hashtbl[2] = { 0, 0 };
426 1.7.6.2 tls uint32_t hash;
427 1.7.6.2 tls
428 1.7.6.2 tls if (sc->sc_dying)
429 1.7.6.2 tls return;
430 1.7.6.2 tls
431 1.7.6.2 tls if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
432 1.7.6.2 tls allmulti:
433 1.7.6.2 tls smsc_dbg_printf(sc, "receive all multicast enabled\n");
434 1.7.6.2 tls sc->sc_mac_csr |= SMSC_MAC_CSR_MCPAS;
435 1.7.6.2 tls sc->sc_mac_csr &= ~SMSC_MAC_CSR_HPFILT;
436 1.7.6.2 tls smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
437 1.7.6.2 tls return;
438 1.7.6.2 tls } else {
439 1.7.6.2 tls sc->sc_mac_csr |= SMSC_MAC_CSR_HPFILT;
440 1.7.6.2 tls sc->sc_mac_csr &= ~(SMSC_MAC_CSR_PRMS | SMSC_MAC_CSR_MCPAS);
441 1.7.6.2 tls }
442 1.7.6.2 tls
443 1.7.6.2 tls ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
444 1.7.6.2 tls while (enm != NULL) {
445 1.7.6.2 tls if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
446 1.7.6.2 tls ETHER_ADDR_LEN) != 0)
447 1.7.6.2 tls goto allmulti;
448 1.7.6.2 tls
449 1.7.6.2 tls hash = smsc_hash(enm->enm_addrlo);
450 1.7.6.2 tls hashtbl[hash >> 5] |= 1 << (hash & 0x1F);
451 1.7.6.2 tls ETHER_NEXT_MULTI(step, enm);
452 1.7.6.2 tls }
453 1.7.6.2 tls
454 1.7.6.2 tls /* Debug */
455 1.7.6.2 tls if (sc->sc_mac_csr & SMSC_MAC_CSR_HPFILT) {
456 1.7.6.2 tls smsc_dbg_printf(sc, "receive select group of macs\n");
457 1.7.6.2 tls } else {
458 1.7.6.2 tls smsc_dbg_printf(sc, "receive own packets only\n");
459 1.7.6.2 tls }
460 1.7.6.2 tls
461 1.7.6.2 tls /* Write the hash table and mac control registers */
462 1.7.6.2 tls ifp->if_flags &= ~IFF_ALLMULTI;
463 1.7.6.2 tls smsc_write_reg(sc, SMSC_HASHH, hashtbl[1]);
464 1.7.6.2 tls smsc_write_reg(sc, SMSC_HASHL, hashtbl[0]);
465 1.7.6.2 tls smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
466 1.7.6.2 tls }
467 1.7.6.2 tls
468 1.7.6.2 tls int
469 1.7.6.2 tls smsc_sethwcsum(struct smsc_softc *sc)
470 1.7.6.2 tls {
471 1.7.6.2 tls struct ifnet *ifp = &sc->sc_ec.ec_if;
472 1.7.6.2 tls uint32_t val;
473 1.7.6.2 tls int err;
474 1.7.6.2 tls
475 1.7.6.2 tls if (!ifp)
476 1.7.6.2 tls return EIO;
477 1.7.6.2 tls
478 1.7.6.2 tls err = smsc_read_reg(sc, SMSC_COE_CTRL, &val);
479 1.7.6.2 tls if (err != 0) {
480 1.7.6.2 tls smsc_warn_printf(sc, "failed to read SMSC_COE_CTRL (err=%d)\n",
481 1.7.6.2 tls err);
482 1.7.6.2 tls return (err);
483 1.7.6.2 tls }
484 1.7.6.2 tls
485 1.7.6.2 tls /* Enable/disable the Rx checksum */
486 1.7.6.2 tls if (ifp->if_capabilities & IFCAP_CSUM_IPv4_Rx)
487 1.7.6.2 tls val |= SMSC_COE_CTRL_RX_EN;
488 1.7.6.2 tls else
489 1.7.6.2 tls val &= ~SMSC_COE_CTRL_RX_EN;
490 1.7.6.2 tls
491 1.7.6.2 tls /* Enable/disable the Tx checksum (currently not supported) */
492 1.7.6.2 tls if (ifp->if_capabilities & IFCAP_CSUM_IPv4_Tx)
493 1.7.6.2 tls val |= SMSC_COE_CTRL_TX_EN;
494 1.7.6.2 tls else
495 1.7.6.2 tls val &= ~SMSC_COE_CTRL_TX_EN;
496 1.7.6.2 tls
497 1.7.6.2 tls err = smsc_write_reg(sc, SMSC_COE_CTRL, val);
498 1.7.6.2 tls if (err != 0) {
499 1.7.6.2 tls smsc_warn_printf(sc, "failed to write SMSC_COE_CTRL (err=%d)\n",
500 1.7.6.2 tls err);
501 1.7.6.2 tls return (err);
502 1.7.6.2 tls }
503 1.7.6.2 tls
504 1.7.6.2 tls return (0);
505 1.7.6.2 tls }
506 1.7.6.2 tls
507 1.7.6.2 tls int
508 1.7.6.2 tls smsc_setmacaddress(struct smsc_softc *sc, const uint8_t *addr)
509 1.7.6.2 tls {
510 1.7.6.2 tls int err;
511 1.7.6.2 tls uint32_t val;
512 1.7.6.2 tls
513 1.7.6.2 tls smsc_dbg_printf(sc, "setting mac address to "
514 1.7.6.2 tls "%02x:%02x:%02x:%02x:%02x:%02x\n",
515 1.7.6.2 tls addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
516 1.7.6.2 tls
517 1.7.6.2 tls val = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
518 1.7.6.2 tls if ((err = smsc_write_reg(sc, SMSC_MAC_ADDRL, val)) != 0)
519 1.7.6.2 tls goto done;
520 1.7.6.2 tls
521 1.7.6.2 tls val = (addr[5] << 8) | addr[4];
522 1.7.6.2 tls err = smsc_write_reg(sc, SMSC_MAC_ADDRH, val);
523 1.7.6.2 tls
524 1.7.6.2 tls done:
525 1.7.6.2 tls return (err);
526 1.7.6.2 tls }
527 1.7.6.2 tls
528 1.7.6.2 tls void
529 1.7.6.2 tls smsc_reset(struct smsc_softc *sc)
530 1.7.6.2 tls {
531 1.7.6.2 tls if (sc->sc_dying)
532 1.7.6.2 tls return;
533 1.7.6.2 tls
534 1.7.6.2 tls /* Wait a little while for the chip to get its brains in order. */
535 1.7.6.2 tls DELAY(1000);
536 1.7.6.2 tls
537 1.7.6.2 tls /* Reinitialize controller to achieve full reset. */
538 1.7.6.2 tls smsc_chip_init(sc);
539 1.7.6.2 tls }
540 1.7.6.2 tls
541 1.7.6.2 tls int
542 1.7.6.2 tls smsc_init(struct ifnet *ifp)
543 1.7.6.2 tls {
544 1.7.6.2 tls struct smsc_softc *sc = ifp->if_softc;
545 1.7.6.2 tls struct smsc_chain *c;
546 1.7.6.2 tls usbd_status err;
547 1.7.6.2 tls int s, i;
548 1.7.6.2 tls
549 1.7.6.2 tls if (sc->sc_dying)
550 1.7.6.2 tls return EIO;
551 1.7.6.2 tls
552 1.7.6.2 tls s = splnet();
553 1.7.6.2 tls
554 1.7.6.2 tls /* Cancel pending I/O */
555 1.7.6.2 tls if (ifp->if_flags & IFF_RUNNING)
556 1.7.6.2 tls smsc_stop(ifp, 1);
557 1.7.6.2 tls
558 1.7.6.2 tls /* Reset the ethernet interface. */
559 1.7.6.2 tls smsc_reset(sc);
560 1.7.6.2 tls
561 1.7.6.2 tls /* Init RX ring. */
562 1.7.6.2 tls if (smsc_rx_list_init(sc) == ENOBUFS) {
563 1.7.6.2 tls aprint_error_dev(sc->sc_dev, "rx list init failed\n");
564 1.7.6.2 tls splx(s);
565 1.7.6.2 tls return EIO;
566 1.7.6.2 tls }
567 1.7.6.2 tls
568 1.7.6.2 tls /* Init TX ring. */
569 1.7.6.2 tls if (smsc_tx_list_init(sc) == ENOBUFS) {
570 1.7.6.2 tls aprint_error_dev(sc->sc_dev, "tx list init failed\n");
571 1.7.6.2 tls splx(s);
572 1.7.6.2 tls return EIO;
573 1.7.6.2 tls }
574 1.7.6.2 tls
575 1.7.6.2 tls /* Load the multicast filter. */
576 1.7.6.2 tls smsc_setmulti(sc);
577 1.7.6.3 tls
578 1.7.6.2 tls /* Open RX and TX pipes. */
579 1.7.6.2 tls err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_RX],
580 1.7.6.2 tls USBD_EXCLUSIVE_USE, &sc->sc_ep[SMSC_ENDPT_RX]);
581 1.7.6.2 tls if (err) {
582 1.7.6.2 tls printf("%s: open rx pipe failed: %s\n",
583 1.7.6.2 tls device_xname(sc->sc_dev), usbd_errstr(err));
584 1.7.6.2 tls splx(s);
585 1.7.6.2 tls return EIO;
586 1.7.6.2 tls }
587 1.7.6.2 tls
588 1.7.6.2 tls err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_TX],
589 1.7.6.2 tls USBD_EXCLUSIVE_USE, &sc->sc_ep[SMSC_ENDPT_TX]);
590 1.7.6.2 tls if (err) {
591 1.7.6.2 tls printf("%s: open tx pipe failed: %s\n",
592 1.7.6.2 tls device_xname(sc->sc_dev), usbd_errstr(err));
593 1.7.6.2 tls splx(s);
594 1.7.6.2 tls return EIO;
595 1.7.6.2 tls }
596 1.7.6.2 tls
597 1.7.6.2 tls /* Start up the receive pipe. */
598 1.7.6.2 tls for (i = 0; i < SMSC_RX_LIST_CNT; i++) {
599 1.7.6.2 tls c = &sc->sc_cdata.rx_chain[i];
600 1.7.6.2 tls usbd_setup_xfer(c->sc_xfer, sc->sc_ep[SMSC_ENDPT_RX],
601 1.7.6.2 tls c, c->sc_buf, sc->sc_bufsz,
602 1.7.6.2 tls USBD_SHORT_XFER_OK | USBD_NO_COPY,
603 1.7.6.2 tls USBD_NO_TIMEOUT, smsc_rxeof);
604 1.7.6.2 tls usbd_transfer(c->sc_xfer);
605 1.7.6.2 tls }
606 1.7.6.2 tls
607 1.7.6.2 tls /* TCP/UDP checksum offload engines. */
608 1.7.6.2 tls smsc_sethwcsum(sc);
609 1.7.6.2 tls
610 1.7.6.2 tls /* Indicate we are up and running. */
611 1.7.6.2 tls ifp->if_flags |= IFF_RUNNING;
612 1.7.6.2 tls ifp->if_flags &= ~IFF_OACTIVE;
613 1.7.6.2 tls
614 1.7.6.2 tls splx(s);
615 1.7.6.2 tls
616 1.7.6.2 tls callout_reset(&sc->sc_stat_ch, hz, smsc_tick, sc);
617 1.7.6.2 tls
618 1.7.6.2 tls return 0;
619 1.7.6.2 tls }
620 1.7.6.2 tls
621 1.7.6.2 tls void
622 1.7.6.2 tls smsc_start(struct ifnet *ifp)
623 1.7.6.2 tls {
624 1.7.6.2 tls struct smsc_softc *sc = ifp->if_softc;
625 1.7.6.2 tls struct mbuf *m_head = NULL;
626 1.7.6.2 tls
627 1.7.6.2 tls /* Don't send anything if there is no link or controller is busy. */
628 1.7.6.2 tls if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
629 1.7.6.2 tls return;
630 1.7.6.2 tls }
631 1.7.6.2 tls
632 1.7.6.2 tls if ((ifp->if_flags & (IFF_OACTIVE|IFF_RUNNING)) != IFF_RUNNING)
633 1.7.6.2 tls return;
634 1.7.6.2 tls
635 1.7.6.2 tls IFQ_POLL(&ifp->if_snd, m_head);
636 1.7.6.2 tls if (m_head == NULL)
637 1.7.6.2 tls return;
638 1.7.6.2 tls
639 1.7.6.2 tls if (smsc_encap(sc, m_head, 0)) {
640 1.7.6.2 tls ifp->if_flags |= IFF_OACTIVE;
641 1.7.6.2 tls return;
642 1.7.6.2 tls }
643 1.7.6.2 tls IFQ_DEQUEUE(&ifp->if_snd, m_head);
644 1.7.6.2 tls
645 1.7.6.2 tls bpf_mtap(ifp, m_head);
646 1.7.6.2 tls
647 1.7.6.2 tls ifp->if_flags |= IFF_OACTIVE;
648 1.7.6.2 tls
649 1.7.6.2 tls /*
650 1.7.6.2 tls * Set a timeout in case the chip goes out to lunch.
651 1.7.6.2 tls */
652 1.7.6.2 tls ifp->if_timer = 5;
653 1.7.6.2 tls }
654 1.7.6.2 tls
655 1.7.6.2 tls void
656 1.7.6.2 tls smsc_tick(void *xsc)
657 1.7.6.2 tls {
658 1.7.6.2 tls struct smsc_softc *sc = xsc;
659 1.7.6.2 tls
660 1.7.6.2 tls if (sc == NULL)
661 1.7.6.2 tls return;
662 1.7.6.2 tls
663 1.7.6.2 tls if (sc->sc_dying)
664 1.7.6.2 tls return;
665 1.7.6.2 tls
666 1.7.6.2 tls usb_add_task(sc->sc_udev, &sc->sc_tick_task, USB_TASKQ_DRIVER);
667 1.7.6.2 tls }
668 1.7.6.2 tls
669 1.7.6.2 tls void
670 1.7.6.2 tls smsc_stop(struct ifnet *ifp, int disable)
671 1.7.6.2 tls {
672 1.7.6.2 tls usbd_status err;
673 1.7.6.2 tls struct smsc_softc *sc = ifp->if_softc;
674 1.7.6.2 tls int i;
675 1.7.6.2 tls
676 1.7.6.2 tls smsc_reset(sc);
677 1.7.6.2 tls
678 1.7.6.2 tls ifp = &sc->sc_ec.ec_if;
679 1.7.6.2 tls ifp->if_timer = 0;
680 1.7.6.2 tls ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
681 1.7.6.2 tls
682 1.7.6.2 tls callout_stop(&sc->sc_stat_ch);
683 1.7.6.2 tls
684 1.7.6.2 tls /* Stop transfers. */
685 1.7.6.2 tls if (sc->sc_ep[SMSC_ENDPT_RX] != NULL) {
686 1.7.6.2 tls err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_RX]);
687 1.7.6.2 tls if (err) {
688 1.7.6.2 tls printf("%s: abort rx pipe failed: %s\n",
689 1.7.6.2 tls device_xname(sc->sc_dev), usbd_errstr(err));
690 1.7.6.2 tls }
691 1.7.6.2 tls err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_RX]);
692 1.7.6.2 tls if (err) {
693 1.7.6.2 tls printf("%s: close rx pipe failed: %s\n",
694 1.7.6.2 tls device_xname(sc->sc_dev), usbd_errstr(err));
695 1.7.6.2 tls }
696 1.7.6.2 tls sc->sc_ep[SMSC_ENDPT_RX] = NULL;
697 1.7.6.2 tls }
698 1.7.6.2 tls
699 1.7.6.2 tls if (sc->sc_ep[SMSC_ENDPT_TX] != NULL) {
700 1.7.6.2 tls err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_TX]);
701 1.7.6.2 tls if (err) {
702 1.7.6.2 tls printf("%s: abort tx pipe failed: %s\n",
703 1.7.6.2 tls device_xname(sc->sc_dev), usbd_errstr(err));
704 1.7.6.2 tls }
705 1.7.6.2 tls err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_TX]);
706 1.7.6.2 tls if (err) {
707 1.7.6.2 tls printf("%s: close tx pipe failed: %s\n",
708 1.7.6.2 tls device_xname(sc->sc_dev), usbd_errstr(err));
709 1.7.6.2 tls }
710 1.7.6.2 tls sc->sc_ep[SMSC_ENDPT_TX] = NULL;
711 1.7.6.2 tls }
712 1.7.6.2 tls
713 1.7.6.2 tls if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL) {
714 1.7.6.2 tls err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_INTR]);
715 1.7.6.2 tls if (err) {
716 1.7.6.2 tls printf("%s: abort intr pipe failed: %s\n",
717 1.7.6.2 tls device_xname(sc->sc_dev), usbd_errstr(err));
718 1.7.6.2 tls }
719 1.7.6.2 tls err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_INTR]);
720 1.7.6.2 tls if (err) {
721 1.7.6.2 tls printf("%s: close intr pipe failed: %s\n",
722 1.7.6.2 tls device_xname(sc->sc_dev), usbd_errstr(err));
723 1.7.6.2 tls }
724 1.7.6.2 tls sc->sc_ep[SMSC_ENDPT_INTR] = NULL;
725 1.7.6.2 tls }
726 1.7.6.2 tls
727 1.7.6.2 tls /* Free RX resources. */
728 1.7.6.2 tls for (i = 0; i < SMSC_RX_LIST_CNT; i++) {
729 1.7.6.2 tls if (sc->sc_cdata.rx_chain[i].sc_mbuf != NULL) {
730 1.7.6.2 tls m_freem(sc->sc_cdata.rx_chain[i].sc_mbuf);
731 1.7.6.2 tls sc->sc_cdata.rx_chain[i].sc_mbuf = NULL;
732 1.7.6.2 tls }
733 1.7.6.2 tls if (sc->sc_cdata.rx_chain[i].sc_xfer != NULL) {
734 1.7.6.2 tls usbd_free_xfer(sc->sc_cdata.rx_chain[i].sc_xfer);
735 1.7.6.2 tls sc->sc_cdata.rx_chain[i].sc_xfer = NULL;
736 1.7.6.2 tls }
737 1.7.6.2 tls }
738 1.7.6.2 tls
739 1.7.6.2 tls /* Free TX resources. */
740 1.7.6.2 tls for (i = 0; i < SMSC_TX_LIST_CNT; i++) {
741 1.7.6.2 tls if (sc->sc_cdata.tx_chain[i].sc_mbuf != NULL) {
742 1.7.6.2 tls m_freem(sc->sc_cdata.tx_chain[i].sc_mbuf);
743 1.7.6.2 tls sc->sc_cdata.tx_chain[i].sc_mbuf = NULL;
744 1.7.6.2 tls }
745 1.7.6.2 tls if (sc->sc_cdata.tx_chain[i].sc_xfer != NULL) {
746 1.7.6.2 tls usbd_free_xfer(sc->sc_cdata.tx_chain[i].sc_xfer);
747 1.7.6.2 tls sc->sc_cdata.tx_chain[i].sc_xfer = NULL;
748 1.7.6.2 tls }
749 1.7.6.2 tls }
750 1.7.6.2 tls }
751 1.7.6.2 tls
752 1.7.6.2 tls int
753 1.7.6.2 tls smsc_chip_init(struct smsc_softc *sc)
754 1.7.6.2 tls {
755 1.7.6.2 tls int err;
756 1.7.6.2 tls uint32_t reg_val;
757 1.7.6.2 tls int burst_cap;
758 1.7.6.2 tls
759 1.7.6.2 tls /* Enter H/W config mode */
760 1.7.6.2 tls smsc_write_reg(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST);
761 1.7.6.2 tls
762 1.7.6.2 tls if ((err = smsc_wait_for_bits(sc, SMSC_HW_CFG,
763 1.7.6.2 tls SMSC_HW_CFG_LRST)) != 0) {
764 1.7.6.2 tls smsc_warn_printf(sc, "timed-out waiting for reset to "
765 1.7.6.2 tls "complete\n");
766 1.7.6.2 tls goto init_failed;
767 1.7.6.2 tls }
768 1.7.6.2 tls
769 1.7.6.2 tls /* Reset the PHY */
770 1.7.6.2 tls smsc_write_reg(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST);
771 1.7.6.2 tls
772 1.7.6.2 tls if ((err = smsc_wait_for_bits(sc, SMSC_PM_CTRL,
773 1.7.6.2 tls SMSC_PM_CTRL_PHY_RST) != 0)) {
774 1.7.6.2 tls smsc_warn_printf(sc, "timed-out waiting for phy reset to "
775 1.7.6.2 tls "complete\n");
776 1.7.6.2 tls goto init_failed;
777 1.7.6.2 tls }
778 1.7.6.2 tls usbd_delay_ms(sc->sc_udev, 40);
779 1.7.6.2 tls
780 1.7.6.2 tls /* Set the mac address */
781 1.7.6.2 tls if ((err = smsc_setmacaddress(sc, sc->sc_enaddr)) != 0) {
782 1.7.6.2 tls smsc_warn_printf(sc, "failed to set the MAC address\n");
783 1.7.6.2 tls goto init_failed;
784 1.7.6.2 tls }
785 1.7.6.2 tls
786 1.7.6.2 tls /*
787 1.7.6.2 tls * Don't know what the HW_CFG_BIR bit is, but following the reset
788 1.7.6.2 tls * sequence as used in the Linux driver.
789 1.7.6.2 tls */
790 1.7.6.2 tls if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) != 0) {
791 1.7.6.2 tls smsc_warn_printf(sc, "failed to read HW_CFG: %d\n", err);
792 1.7.6.2 tls goto init_failed;
793 1.7.6.2 tls }
794 1.7.6.2 tls reg_val |= SMSC_HW_CFG_BIR;
795 1.7.6.2 tls smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
796 1.7.6.2 tls
797 1.7.6.2 tls /*
798 1.7.6.2 tls * There is a so called 'turbo mode' that the linux driver supports, it
799 1.7.6.2 tls * seems to allow you to jam multiple frames per Rx transaction.
800 1.7.6.2 tls * By default this driver supports that and therefore allows multiple
801 1.7.6.3 tls * frames per USB transfer.
802 1.7.6.2 tls *
803 1.7.6.2 tls * The xfer buffer size needs to reflect this as well, therefore based
804 1.7.6.2 tls * on the calculations in the Linux driver the RX bufsize is set to
805 1.7.6.2 tls * 18944,
806 1.7.6.2 tls * bufsz = (16 * 1024 + 5 * 512)
807 1.7.6.2 tls *
808 1.7.6.2 tls * Burst capability is the number of URBs that can be in a burst of
809 1.7.6.2 tls * data/ethernet frames.
810 1.7.6.2 tls */
811 1.7.6.2 tls #ifdef SMSC_TURBO
812 1.7.6.2 tls if (sc->sc_udev->speed == USB_SPEED_HIGH)
813 1.7.6.2 tls burst_cap = 37;
814 1.7.6.2 tls else
815 1.7.6.2 tls burst_cap = 128;
816 1.7.6.2 tls #else
817 1.7.6.2 tls burst_cap = 0;
818 1.7.6.2 tls #endif
819 1.7.6.2 tls
820 1.7.6.2 tls smsc_write_reg(sc, SMSC_BURST_CAP, burst_cap);
821 1.7.6.2 tls
822 1.7.6.2 tls /* Set the default bulk in delay (magic value from Linux driver) */
823 1.7.6.2 tls smsc_write_reg(sc, SMSC_BULK_IN_DLY, 0x00002000);
824 1.7.6.2 tls
825 1.7.6.2 tls /*
826 1.7.6.2 tls * Initialise the RX interface
827 1.7.6.2 tls */
828 1.7.6.2 tls if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) < 0) {
829 1.7.6.2 tls smsc_warn_printf(sc, "failed to read HW_CFG: (err = %d)\n",
830 1.7.6.2 tls err);
831 1.7.6.2 tls goto init_failed;
832 1.7.6.2 tls }
833 1.7.6.2 tls
834 1.7.6.2 tls /*
835 1.7.6.3 tls * The following settings are used for 'turbo mode', a.k.a multiple
836 1.7.6.2 tls * frames per Rx transaction (again info taken form Linux driver).
837 1.7.6.2 tls */
838 1.7.6.2 tls #ifdef SMSC_TURBO
839 1.7.6.2 tls reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE);
840 1.7.6.2 tls #endif
841 1.7.6.2 tls
842 1.7.6.2 tls smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
843 1.7.6.2 tls
844 1.7.6.2 tls /* Clear the status register ? */
845 1.7.6.2 tls smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff);
846 1.7.6.2 tls
847 1.7.6.2 tls /* Read and display the revision register */
848 1.7.6.2 tls if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) {
849 1.7.6.2 tls smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err);
850 1.7.6.2 tls goto init_failed;
851 1.7.6.2 tls }
852 1.7.6.2 tls
853 1.7.6.2 tls /* GPIO/LED setup */
854 1.7.6.2 tls reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED |
855 1.7.6.2 tls SMSC_LED_GPIO_CFG_FDX_LED;
856 1.7.6.2 tls smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val);
857 1.7.6.2 tls
858 1.7.6.2 tls /*
859 1.7.6.2 tls * Initialise the TX interface
860 1.7.6.2 tls */
861 1.7.6.2 tls smsc_write_reg(sc, SMSC_FLOW, 0);
862 1.7.6.2 tls
863 1.7.6.2 tls smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT);
864 1.7.6.2 tls
865 1.7.6.2 tls /* Read the current MAC configuration */
866 1.7.6.2 tls if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) {
867 1.7.6.2 tls smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err);
868 1.7.6.2 tls goto init_failed;
869 1.7.6.2 tls }
870 1.7.6.2 tls
871 1.7.6.2 tls /* Vlan */
872 1.7.6.2 tls smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN);
873 1.7.6.2 tls
874 1.7.6.2 tls /*
875 1.7.6.2 tls * Start TX
876 1.7.6.2 tls */
877 1.7.6.2 tls sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN;
878 1.7.6.2 tls smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
879 1.7.6.2 tls smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON);
880 1.7.6.2 tls
881 1.7.6.2 tls /*
882 1.7.6.2 tls * Start RX
883 1.7.6.2 tls */
884 1.7.6.2 tls sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN;
885 1.7.6.2 tls smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
886 1.7.6.2 tls
887 1.7.6.2 tls return (0);
888 1.7.6.2 tls
889 1.7.6.2 tls init_failed:
890 1.7.6.2 tls smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err);
891 1.7.6.2 tls return (err);
892 1.7.6.2 tls }
893 1.7.6.2 tls
894 1.7.6.2 tls int
895 1.7.6.2 tls smsc_ioctl(struct ifnet *ifp, u_long cmd, void *data)
896 1.7.6.2 tls {
897 1.7.6.2 tls struct smsc_softc *sc = ifp->if_softc;
898 1.7.6.2 tls struct ifreq /*const*/ *ifr = data;
899 1.7.6.2 tls int s, error = 0;
900 1.7.6.2 tls
901 1.7.6.2 tls if (sc->sc_dying)
902 1.7.6.2 tls return EIO;
903 1.7.6.2 tls
904 1.7.6.2 tls s = splnet();
905 1.7.6.2 tls
906 1.7.6.2 tls switch(cmd) {
907 1.7.6.2 tls case SIOCSIFFLAGS:
908 1.7.6.2 tls if ((error = ifioctl_common(ifp, cmd, data)) != 0)
909 1.7.6.2 tls break;
910 1.7.6.2 tls
911 1.7.6.2 tls switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
912 1.7.6.2 tls case IFF_RUNNING:
913 1.7.6.2 tls smsc_stop(ifp, 1);
914 1.7.6.2 tls break;
915 1.7.6.2 tls case IFF_UP:
916 1.7.6.2 tls smsc_init(ifp);
917 1.7.6.2 tls break;
918 1.7.6.2 tls case IFF_UP | IFF_RUNNING:
919 1.7.6.2 tls if (ifp->if_flags & IFF_PROMISC &&
920 1.7.6.2 tls !(sc->sc_if_flags & IFF_PROMISC)) {
921 1.7.6.2 tls sc->sc_mac_csr |= SMSC_MAC_CSR_PRMS;
922 1.7.6.2 tls smsc_write_reg(sc, SMSC_MAC_CSR,
923 1.7.6.2 tls sc->sc_mac_csr);
924 1.7.6.2 tls smsc_setmulti(sc);
925 1.7.6.2 tls } else if (!(ifp->if_flags & IFF_PROMISC) &&
926 1.7.6.2 tls sc->sc_if_flags & IFF_PROMISC) {
927 1.7.6.2 tls sc->sc_mac_csr &= ~SMSC_MAC_CSR_PRMS;
928 1.7.6.2 tls smsc_write_reg(sc, SMSC_MAC_CSR,
929 1.7.6.2 tls sc->sc_mac_csr);
930 1.7.6.2 tls smsc_setmulti(sc);
931 1.7.6.2 tls } else {
932 1.7.6.2 tls smsc_init(ifp);
933 1.7.6.2 tls }
934 1.7.6.2 tls break;
935 1.7.6.2 tls }
936 1.7.6.2 tls sc->sc_if_flags = ifp->if_flags;
937 1.7.6.2 tls break;
938 1.7.6.2 tls
939 1.7.6.2 tls case SIOCGIFMEDIA:
940 1.7.6.2 tls case SIOCSIFMEDIA:
941 1.7.6.2 tls error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
942 1.7.6.2 tls break;
943 1.7.6.2 tls
944 1.7.6.2 tls default:
945 1.7.6.2 tls if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
946 1.7.6.2 tls break;
947 1.7.6.2 tls
948 1.7.6.2 tls error = 0;
949 1.7.6.2 tls
950 1.7.6.2 tls if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI)
951 1.7.6.2 tls smsc_setmulti(sc);
952 1.7.6.2 tls
953 1.7.6.2 tls }
954 1.7.6.2 tls splx(s);
955 1.7.6.2 tls
956 1.7.6.2 tls return error;
957 1.7.6.2 tls }
958 1.7.6.2 tls
959 1.7.6.2 tls int
960 1.7.6.2 tls smsc_match(device_t parent, cfdata_t match, void *aux)
961 1.7.6.2 tls {
962 1.7.6.2 tls struct usb_attach_arg *uaa = aux;
963 1.7.6.2 tls
964 1.7.6.2 tls return (usb_lookup(smsc_devs, uaa->vendor, uaa->product) != NULL) ?
965 1.7.6.2 tls UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
966 1.7.6.2 tls }
967 1.7.6.2 tls
968 1.7.6.2 tls void
969 1.7.6.2 tls smsc_attach(device_t parent, device_t self, void *aux)
970 1.7.6.2 tls {
971 1.7.6.2 tls struct smsc_softc *sc = device_private(self);
972 1.7.6.2 tls struct usb_attach_arg *uaa = aux;
973 1.7.6.2 tls usbd_device_handle dev = uaa->device;
974 1.7.6.2 tls usb_interface_descriptor_t *id;
975 1.7.6.2 tls usb_endpoint_descriptor_t *ed;
976 1.7.6.2 tls char *devinfop;
977 1.7.6.2 tls struct mii_data *mii;
978 1.7.6.2 tls struct ifnet *ifp;
979 1.7.6.2 tls int err, s, i;
980 1.7.6.2 tls uint32_t mac_h, mac_l;
981 1.7.6.2 tls
982 1.7.6.2 tls sc->sc_dev = self;
983 1.7.6.2 tls sc->sc_udev = dev;
984 1.7.6.2 tls
985 1.7.6.2 tls aprint_naive("\n");
986 1.7.6.2 tls aprint_normal("\n");
987 1.7.6.2 tls
988 1.7.6.2 tls devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
989 1.7.6.2 tls aprint_normal_dev(self, "%s\n", devinfop);
990 1.7.6.2 tls usbd_devinfo_free(devinfop);
991 1.7.6.2 tls
992 1.7.6.2 tls err = usbd_set_config_no(dev, SMSC_CONFIG_INDEX, 1);
993 1.7.6.2 tls if (err) {
994 1.7.6.2 tls aprint_error_dev(self, "failed to set configuration"
995 1.7.6.2 tls ", err=%s\n", usbd_errstr(err));
996 1.7.6.2 tls return;
997 1.7.6.2 tls }
998 1.7.6.2 tls /* Setup the endpoints for the SMSC LAN95xx device(s) */
999 1.7.6.2 tls usb_init_task(&sc->sc_tick_task, smsc_tick_task, sc, 0);
1000 1.7.6.2 tls usb_init_task(&sc->sc_stop_task, (void (*)(void *))smsc_stop, sc, 0);
1001 1.7.6.2 tls mutex_init(&sc->sc_mii_lock, MUTEX_DEFAULT, IPL_NONE);
1002 1.7.6.2 tls
1003 1.7.6.2 tls err = usbd_device2interface_handle(dev, SMSC_IFACE_IDX, &sc->sc_iface);
1004 1.7.6.2 tls if (err) {
1005 1.7.6.2 tls aprint_error_dev(self, "getting interface handle failed\n");
1006 1.7.6.2 tls return;
1007 1.7.6.2 tls }
1008 1.7.6.2 tls
1009 1.7.6.2 tls id = usbd_get_interface_descriptor(sc->sc_iface);
1010 1.7.6.2 tls
1011 1.7.6.2 tls if (sc->sc_udev->speed >= USB_SPEED_HIGH)
1012 1.7.6.2 tls sc->sc_bufsz = SMSC_MAX_BUFSZ;
1013 1.7.6.2 tls else
1014 1.7.6.2 tls sc->sc_bufsz = SMSC_MIN_BUFSZ;
1015 1.7.6.2 tls
1016 1.7.6.2 tls /* Find endpoints. */
1017 1.7.6.2 tls for (i = 0; i < id->bNumEndpoints; i++) {
1018 1.7.6.2 tls ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
1019 1.7.6.2 tls if (!ed) {
1020 1.7.6.2 tls aprint_error_dev(self, "couldn't get ep %d\n", i);
1021 1.7.6.2 tls return;
1022 1.7.6.2 tls }
1023 1.7.6.2 tls if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
1024 1.7.6.2 tls UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
1025 1.7.6.2 tls sc->sc_ed[SMSC_ENDPT_RX] = ed->bEndpointAddress;
1026 1.7.6.2 tls } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
1027 1.7.6.2 tls UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
1028 1.7.6.2 tls sc->sc_ed[SMSC_ENDPT_TX] = ed->bEndpointAddress;
1029 1.7.6.2 tls } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
1030 1.7.6.2 tls UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
1031 1.7.6.2 tls sc->sc_ed[SMSC_ENDPT_INTR] = ed->bEndpointAddress;
1032 1.7.6.2 tls }
1033 1.7.6.2 tls }
1034 1.7.6.2 tls
1035 1.7.6.2 tls s = splnet();
1036 1.7.6.2 tls
1037 1.7.6.2 tls ifp = &sc->sc_ec.ec_if;
1038 1.7.6.2 tls ifp->if_softc = sc;
1039 1.7.6.2 tls strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
1040 1.7.6.2 tls ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1041 1.7.6.2 tls ifp->if_init = smsc_init;
1042 1.7.6.2 tls ifp->if_ioctl = smsc_ioctl;
1043 1.7.6.2 tls ifp->if_start = smsc_start;
1044 1.7.6.2 tls ifp->if_stop = smsc_stop;
1045 1.7.6.2 tls
1046 1.7.6.2 tls sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU;
1047 1.7.6.3 tls
1048 1.7.6.2 tls /* Setup some of the basics */
1049 1.7.6.2 tls sc->sc_phyno = 1;
1050 1.7.6.2 tls
1051 1.7.6.2 tls /*
1052 1.7.6.2 tls * Attempt to get the mac address, if an EEPROM is not attached this
1053 1.7.6.2 tls * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC
1054 1.7.6.2 tls * address based on urandom.
1055 1.7.6.2 tls */
1056 1.7.6.2 tls memset(sc->sc_enaddr, 0xff, ETHER_ADDR_LEN);
1057 1.7.6.2 tls
1058 1.7.6.2 tls prop_dictionary_t dict = device_properties(self);
1059 1.7.6.2 tls prop_data_t eaprop = prop_dictionary_get(dict, "mac-address");
1060 1.7.6.2 tls
1061 1.7.6.2 tls if (eaprop != NULL) {
1062 1.7.6.2 tls KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA);
1063 1.7.6.2 tls KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN);
1064 1.7.6.2 tls memcpy(sc->sc_enaddr, prop_data_data_nocopy(eaprop),
1065 1.7.6.2 tls ETHER_ADDR_LEN);
1066 1.7.6.2 tls } else
1067 1.7.6.2 tls /* Check if there is already a MAC address in the register */
1068 1.7.6.2 tls if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) &&
1069 1.7.6.2 tls (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) {
1070 1.7.6.2 tls sc->sc_enaddr[5] = (uint8_t)((mac_h >> 8) & 0xff);
1071 1.7.6.2 tls sc->sc_enaddr[4] = (uint8_t)((mac_h) & 0xff);
1072 1.7.6.2 tls sc->sc_enaddr[3] = (uint8_t)((mac_l >> 24) & 0xff);
1073 1.7.6.2 tls sc->sc_enaddr[2] = (uint8_t)((mac_l >> 16) & 0xff);
1074 1.7.6.2 tls sc->sc_enaddr[1] = (uint8_t)((mac_l >> 8) & 0xff);
1075 1.7.6.2 tls sc->sc_enaddr[0] = (uint8_t)((mac_l) & 0xff);
1076 1.7.6.2 tls }
1077 1.7.6.2 tls
1078 1.7.6.2 tls aprint_normal_dev(self, " Ethernet address %s\n", ether_sprintf(sc->sc_enaddr));
1079 1.7.6.2 tls
1080 1.7.6.2 tls IFQ_SET_READY(&ifp->if_snd);
1081 1.7.6.2 tls
1082 1.7.6.2 tls /* Initialize MII/media info. */
1083 1.7.6.2 tls mii = &sc->sc_mii;
1084 1.7.6.2 tls mii->mii_ifp = ifp;
1085 1.7.6.2 tls mii->mii_readreg = smsc_miibus_readreg;
1086 1.7.6.2 tls mii->mii_writereg = smsc_miibus_writereg;
1087 1.7.6.2 tls mii->mii_statchg = smsc_miibus_statchg;
1088 1.7.6.2 tls mii->mii_flags = MIIF_AUTOTSLEEP;
1089 1.7.6.2 tls sc->sc_ec.ec_mii = mii;
1090 1.7.6.2 tls ifmedia_init(&mii->mii_media, 0, smsc_ifmedia_upd, smsc_ifmedia_sts);
1091 1.7.6.2 tls mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
1092 1.7.6.2 tls
1093 1.7.6.2 tls if (LIST_FIRST(&mii->mii_phys) == NULL) {
1094 1.7.6.2 tls ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
1095 1.7.6.2 tls ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
1096 1.7.6.2 tls } else
1097 1.7.6.2 tls ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
1098 1.7.6.2 tls
1099 1.7.6.2 tls if_attach(ifp);
1100 1.7.6.2 tls ether_ifattach(ifp, sc->sc_enaddr);
1101 1.7.6.2 tls
1102 1.7.6.2 tls rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev),
1103 1.7.6.2 tls RND_TYPE_NET, 0);
1104 1.7.6.2 tls
1105 1.7.6.2 tls callout_init(&sc->sc_stat_ch, 0);
1106 1.7.6.2 tls
1107 1.7.6.2 tls splx(s);
1108 1.7.6.2 tls
1109 1.7.6.2 tls usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
1110 1.7.6.2 tls }
1111 1.7.6.2 tls
1112 1.7.6.2 tls int
1113 1.7.6.2 tls smsc_detach(device_t self, int flags)
1114 1.7.6.2 tls {
1115 1.7.6.2 tls struct smsc_softc *sc = device_private(self);
1116 1.7.6.2 tls struct ifnet *ifp = &sc->sc_ec.ec_if;
1117 1.7.6.2 tls int s;
1118 1.7.6.2 tls
1119 1.7.6.2 tls callout_stop(&sc->sc_stat_ch);
1120 1.7.6.2 tls
1121 1.7.6.2 tls if (sc->sc_ep[SMSC_ENDPT_TX] != NULL)
1122 1.7.6.2 tls usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_TX]);
1123 1.7.6.2 tls if (sc->sc_ep[SMSC_ENDPT_RX] != NULL)
1124 1.7.6.2 tls usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_RX]);
1125 1.7.6.2 tls if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL)
1126 1.7.6.2 tls usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_INTR]);
1127 1.7.6.2 tls
1128 1.7.6.2 tls /*
1129 1.7.6.2 tls * Remove any pending tasks. They cannot be executing because they run
1130 1.7.6.2 tls * in the same thread as detach.
1131 1.7.6.2 tls */
1132 1.7.6.2 tls usb_rem_task(sc->sc_udev, &sc->sc_tick_task);
1133 1.7.6.2 tls usb_rem_task(sc->sc_udev, &sc->sc_stop_task);
1134 1.7.6.2 tls
1135 1.7.6.2 tls s = splusb();
1136 1.7.6.2 tls
1137 1.7.6.2 tls if (--sc->sc_refcnt >= 0) {
1138 1.7.6.2 tls /* Wait for processes to go away */
1139 1.7.6.2 tls usb_detach_waitold(sc->sc_dev);
1140 1.7.6.2 tls }
1141 1.7.6.2 tls
1142 1.7.6.2 tls if (ifp->if_flags & IFF_RUNNING)
1143 1.7.6.2 tls smsc_stop(ifp ,1);
1144 1.7.6.2 tls
1145 1.7.6.2 tls rnd_detach_source(&sc->sc_rnd_source);
1146 1.7.6.2 tls mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
1147 1.7.6.2 tls ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
1148 1.7.6.2 tls if (ifp->if_softc != NULL) {
1149 1.7.6.2 tls ether_ifdetach(ifp);
1150 1.7.6.2 tls if_detach(ifp);
1151 1.7.6.2 tls }
1152 1.7.6.2 tls
1153 1.7.6.2 tls #ifdef DIAGNOSTIC
1154 1.7.6.2 tls if (sc->sc_ep[SMSC_ENDPT_TX] != NULL ||
1155 1.7.6.2 tls sc->sc_ep[SMSC_ENDPT_RX] != NULL ||
1156 1.7.6.2 tls sc->sc_ep[SMSC_ENDPT_INTR] != NULL)
1157 1.7.6.2 tls printf("%s: detach has active endpoints\n",
1158 1.7.6.2 tls device_xname(sc->sc_dev));
1159 1.7.6.2 tls #endif
1160 1.7.6.2 tls
1161 1.7.6.2 tls if (--sc->sc_refcnt >= 0) {
1162 1.7.6.2 tls /* Wait for processes to go away. */
1163 1.7.6.2 tls usb_detach_waitold(sc->sc_dev);
1164 1.7.6.2 tls }
1165 1.7.6.2 tls splx(s);
1166 1.7.6.2 tls
1167 1.7.6.2 tls usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
1168 1.7.6.2 tls
1169 1.7.6.2 tls mutex_destroy(&sc->sc_mii_lock);
1170 1.7.6.2 tls
1171 1.7.6.2 tls return (0);
1172 1.7.6.2 tls }
1173 1.7.6.2 tls
1174 1.7.6.2 tls void
1175 1.7.6.2 tls smsc_tick_task(void *xsc)
1176 1.7.6.2 tls {
1177 1.7.6.2 tls int s;
1178 1.7.6.2 tls struct smsc_softc *sc = xsc;
1179 1.7.6.2 tls struct ifnet *ifp;
1180 1.7.6.2 tls struct mii_data *mii;
1181 1.7.6.2 tls
1182 1.7.6.2 tls if (sc == NULL)
1183 1.7.6.2 tls return;
1184 1.7.6.2 tls
1185 1.7.6.2 tls if (sc->sc_dying)
1186 1.7.6.2 tls return;
1187 1.7.6.2 tls ifp = &sc->sc_ec.ec_if;
1188 1.7.6.2 tls mii = &sc->sc_mii;
1189 1.7.6.2 tls if (mii == NULL)
1190 1.7.6.2 tls return;
1191 1.7.6.2 tls
1192 1.7.6.2 tls s = splnet();
1193 1.7.6.2 tls
1194 1.7.6.2 tls mii_tick(mii);
1195 1.7.6.2 tls if ((sc->sc_flags & SMSC_FLAG_LINK) == 0)
1196 1.7.6.2 tls smsc_miibus_statchg(ifp);
1197 1.7.6.2 tls callout_reset(&sc->sc_stat_ch, hz, smsc_tick, sc);
1198 1.7.6.2 tls
1199 1.7.6.2 tls splx(s);
1200 1.7.6.2 tls }
1201 1.7.6.2 tls
1202 1.7.6.2 tls int
1203 1.7.6.2 tls smsc_activate(device_t self, enum devact act)
1204 1.7.6.2 tls {
1205 1.7.6.2 tls struct smsc_softc *sc = device_private(self);
1206 1.7.6.2 tls
1207 1.7.6.2 tls switch (act) {
1208 1.7.6.2 tls case DVACT_DEACTIVATE:
1209 1.7.6.2 tls if_deactivate(&sc->sc_ec.ec_if);
1210 1.7.6.2 tls sc->sc_dying = 1;
1211 1.7.6.2 tls return 0;
1212 1.7.6.2 tls default:
1213 1.7.6.2 tls return EOPNOTSUPP;
1214 1.7.6.2 tls }
1215 1.7.6.2 tls return (0);
1216 1.7.6.2 tls }
1217 1.7.6.2 tls
1218 1.7.6.2 tls void
1219 1.7.6.2 tls smsc_lock_mii(struct smsc_softc *sc)
1220 1.7.6.2 tls {
1221 1.7.6.2 tls sc->sc_refcnt++;
1222 1.7.6.2 tls mutex_enter(&sc->sc_mii_lock);
1223 1.7.6.2 tls }
1224 1.7.6.2 tls
1225 1.7.6.2 tls void
1226 1.7.6.2 tls smsc_unlock_mii(struct smsc_softc *sc)
1227 1.7.6.2 tls {
1228 1.7.6.2 tls mutex_exit(&sc->sc_mii_lock);
1229 1.7.6.2 tls if (--sc->sc_refcnt < 0)
1230 1.7.6.2 tls usb_detach_wakeupold(sc->sc_dev);
1231 1.7.6.2 tls }
1232 1.7.6.2 tls
1233 1.7.6.2 tls void
1234 1.7.6.2 tls smsc_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1235 1.7.6.2 tls {
1236 1.7.6.2 tls struct smsc_chain *c = (struct smsc_chain *)priv;
1237 1.7.6.2 tls struct smsc_softc *sc = c->sc_sc;
1238 1.7.6.2 tls struct ifnet *ifp = &sc->sc_ec.ec_if;
1239 1.7.6.2 tls u_char *buf = c->sc_buf;
1240 1.7.6.2 tls uint32_t total_len;
1241 1.7.6.2 tls uint16_t pktlen = 0;
1242 1.7.6.2 tls struct mbuf *m;
1243 1.7.6.2 tls int s;
1244 1.7.6.2 tls uint32_t rxhdr;
1245 1.7.6.2 tls
1246 1.7.6.2 tls if (sc->sc_dying)
1247 1.7.6.2 tls return;
1248 1.7.6.2 tls
1249 1.7.6.2 tls if (!(ifp->if_flags & IFF_RUNNING))
1250 1.7.6.2 tls return;
1251 1.7.6.2 tls
1252 1.7.6.2 tls if (status != USBD_NORMAL_COMPLETION) {
1253 1.7.6.2 tls if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1254 1.7.6.2 tls return;
1255 1.7.6.2 tls if (usbd_ratecheck(&sc->sc_rx_notice)) {
1256 1.7.6.2 tls printf("%s: usb errors on rx: %s\n",
1257 1.7.6.2 tls device_xname(sc->sc_dev), usbd_errstr(status));
1258 1.7.6.2 tls }
1259 1.7.6.2 tls if (status == USBD_STALLED)
1260 1.7.6.2 tls usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_RX]);
1261 1.7.6.2 tls goto done;
1262 1.7.6.2 tls }
1263 1.7.6.2 tls
1264 1.7.6.2 tls usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1265 1.7.6.2 tls smsc_dbg_printf(sc, "xfer status total_len %d\n", total_len);
1266 1.7.6.2 tls
1267 1.7.6.2 tls do {
1268 1.7.6.2 tls if (total_len < sizeof(rxhdr)) {
1269 1.7.6.2 tls smsc_dbg_printf(sc, "total_len %d < sizeof(rxhdr) %zu\n",
1270 1.7.6.2 tls total_len, sizeof(rxhdr));
1271 1.7.6.2 tls ifp->if_ierrors++;
1272 1.7.6.2 tls goto done;
1273 1.7.6.2 tls }
1274 1.7.6.2 tls
1275 1.7.6.2 tls buf += pktlen;
1276 1.7.6.2 tls
1277 1.7.6.2 tls memcpy(&rxhdr, buf, sizeof(rxhdr));
1278 1.7.6.2 tls rxhdr = le32toh(rxhdr);
1279 1.7.6.2 tls total_len -= sizeof(rxhdr);
1280 1.7.6.2 tls
1281 1.7.6.2 tls if (rxhdr & SMSC_RX_STAT_ERROR) {
1282 1.7.6.2 tls smsc_dbg_printf(sc, "rx error (hdr 0x%08x)\n", rxhdr);
1283 1.7.6.2 tls ifp->if_ierrors++;
1284 1.7.6.2 tls goto done;
1285 1.7.6.2 tls }
1286 1.7.6.2 tls
1287 1.7.6.2 tls pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr);
1288 1.7.6.2 tls smsc_dbg_printf(sc, "rxeof total_len %d pktlen %d rxhdr "
1289 1.7.6.2 tls "0x%08x\n", total_len, pktlen, rxhdr);
1290 1.7.6.2 tls if (pktlen > total_len) {
1291 1.7.6.2 tls smsc_dbg_printf(sc, "pktlen %d > total_len %d\n",
1292 1.7.6.2 tls pktlen, total_len);
1293 1.7.6.2 tls ifp->if_ierrors++;
1294 1.7.6.2 tls goto done;
1295 1.7.6.2 tls }
1296 1.7.6.2 tls
1297 1.7.6.2 tls buf += sizeof(rxhdr);
1298 1.7.6.2 tls total_len -= pktlen;
1299 1.7.6.2 tls
1300 1.7.6.2 tls m = smsc_newbuf();
1301 1.7.6.2 tls if (m == NULL) {
1302 1.7.6.2 tls smsc_dbg_printf(sc, "smc_newbuf returned NULL\n");
1303 1.7.6.2 tls ifp->if_ierrors++;
1304 1.7.6.2 tls goto done;
1305 1.7.6.2 tls }
1306 1.7.6.2 tls
1307 1.7.6.2 tls ifp->if_ipackets++;
1308 1.7.6.2 tls m->m_pkthdr.rcvif = ifp;
1309 1.7.6.2 tls
1310 1.7.6.2 tls pktlen -= 2; // JDM
1311 1.7.6.3 tls
1312 1.7.6.2 tls m->m_pkthdr.len = m->m_len = pktlen;
1313 1.7.6.2 tls #define ETHER_ALIGN 2
1314 1.7.6.2 tls m_adj(m, ETHER_ALIGN);
1315 1.7.6.2 tls
1316 1.7.6.2 tls memcpy(mtod(m, char *), buf, pktlen);
1317 1.7.6.2 tls
1318 1.7.6.2 tls /* push the packet up */
1319 1.7.6.2 tls s = splnet();
1320 1.7.6.2 tls bpf_mtap(ifp, m);
1321 1.7.6.2 tls ifp->if_input(ifp, m);
1322 1.7.6.2 tls splx(s);
1323 1.7.6.2 tls } while (total_len > 0);
1324 1.7.6.2 tls
1325 1.7.6.2 tls done:
1326 1.7.6.2 tls memset(c->sc_buf, 0, sc->sc_bufsz);
1327 1.7.6.2 tls
1328 1.7.6.2 tls /* Setup new transfer. */
1329 1.7.6.2 tls usbd_setup_xfer(xfer, sc->sc_ep[SMSC_ENDPT_RX],
1330 1.7.6.2 tls c, c->sc_buf, sc->sc_bufsz,
1331 1.7.6.2 tls USBD_SHORT_XFER_OK | USBD_NO_COPY,
1332 1.7.6.2 tls USBD_NO_TIMEOUT, smsc_rxeof);
1333 1.7.6.2 tls usbd_transfer(xfer);
1334 1.7.6.2 tls
1335 1.7.6.2 tls return;
1336 1.7.6.2 tls }
1337 1.7.6.2 tls
1338 1.7.6.2 tls void
1339 1.7.6.2 tls smsc_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1340 1.7.6.2 tls {
1341 1.7.6.2 tls struct smsc_softc *sc;
1342 1.7.6.2 tls struct smsc_chain *c;
1343 1.7.6.2 tls struct ifnet *ifp;
1344 1.7.6.2 tls int s;
1345 1.7.6.2 tls
1346 1.7.6.2 tls c = priv;
1347 1.7.6.2 tls sc = c->sc_sc;
1348 1.7.6.2 tls ifp = &sc->sc_ec.ec_if;
1349 1.7.6.2 tls
1350 1.7.6.2 tls if (sc->sc_dying)
1351 1.7.6.2 tls return;
1352 1.7.6.2 tls
1353 1.7.6.2 tls s = splnet();
1354 1.7.6.2 tls
1355 1.7.6.2 tls ifp->if_timer = 0;
1356 1.7.6.2 tls ifp->if_flags &= ~IFF_OACTIVE;
1357 1.7.6.2 tls
1358 1.7.6.2 tls if (status != USBD_NORMAL_COMPLETION) {
1359 1.7.6.2 tls if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1360 1.7.6.2 tls splx(s);
1361 1.7.6.2 tls return;
1362 1.7.6.2 tls }
1363 1.7.6.2 tls ifp->if_oerrors++;
1364 1.7.6.2 tls printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev),
1365 1.7.6.2 tls usbd_errstr(status));
1366 1.7.6.2 tls if (status == USBD_STALLED)
1367 1.7.6.2 tls usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_TX]);
1368 1.7.6.2 tls splx(s);
1369 1.7.6.2 tls return;
1370 1.7.6.2 tls }
1371 1.7.6.2 tls ifp->if_opackets++;
1372 1.7.6.2 tls
1373 1.7.6.2 tls m_freem(c->sc_mbuf);
1374 1.7.6.2 tls c->sc_mbuf = NULL;
1375 1.7.6.2 tls
1376 1.7.6.2 tls if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1377 1.7.6.2 tls smsc_start(ifp);
1378 1.7.6.2 tls
1379 1.7.6.2 tls splx(s);
1380 1.7.6.2 tls }
1381 1.7.6.2 tls
1382 1.7.6.2 tls int
1383 1.7.6.2 tls smsc_tx_list_init(struct smsc_softc *sc)
1384 1.7.6.2 tls {
1385 1.7.6.2 tls struct smsc_cdata *cd;
1386 1.7.6.2 tls struct smsc_chain *c;
1387 1.7.6.2 tls int i;
1388 1.7.6.2 tls
1389 1.7.6.2 tls cd = &sc->sc_cdata;
1390 1.7.6.2 tls for (i = 0; i < SMSC_TX_LIST_CNT; i++) {
1391 1.7.6.2 tls c = &cd->tx_chain[i];
1392 1.7.6.2 tls c->sc_sc = sc;
1393 1.7.6.2 tls c->sc_idx = i;
1394 1.7.6.2 tls c->sc_mbuf = NULL;
1395 1.7.6.2 tls if (c->sc_xfer == NULL) {
1396 1.7.6.2 tls c->sc_xfer = usbd_alloc_xfer(sc->sc_udev);
1397 1.7.6.2 tls if (c->sc_xfer == NULL)
1398 1.7.6.2 tls return (ENOBUFS);
1399 1.7.6.2 tls c->sc_buf = usbd_alloc_buffer(c->sc_xfer,
1400 1.7.6.2 tls sc->sc_bufsz);
1401 1.7.6.2 tls if (c->sc_buf == NULL) {
1402 1.7.6.2 tls usbd_free_xfer(c->sc_xfer);
1403 1.7.6.2 tls return (ENOBUFS);
1404 1.7.6.2 tls }
1405 1.7.6.2 tls }
1406 1.7.6.2 tls }
1407 1.7.6.2 tls
1408 1.7.6.2 tls return (0);
1409 1.7.6.2 tls }
1410 1.7.6.2 tls
1411 1.7.6.2 tls int
1412 1.7.6.2 tls smsc_rx_list_init(struct smsc_softc *sc)
1413 1.7.6.2 tls {
1414 1.7.6.2 tls struct smsc_cdata *cd;
1415 1.7.6.2 tls struct smsc_chain *c;
1416 1.7.6.2 tls int i;
1417 1.7.6.2 tls
1418 1.7.6.2 tls cd = &sc->sc_cdata;
1419 1.7.6.2 tls for (i = 0; i < SMSC_RX_LIST_CNT; i++) {
1420 1.7.6.2 tls c = &cd->rx_chain[i];
1421 1.7.6.2 tls c->sc_sc = sc;
1422 1.7.6.2 tls c->sc_idx = i;
1423 1.7.6.2 tls c->sc_mbuf = NULL;
1424 1.7.6.2 tls if (c->sc_xfer == NULL) {
1425 1.7.6.2 tls c->sc_xfer = usbd_alloc_xfer(sc->sc_udev);
1426 1.7.6.2 tls if (c->sc_xfer == NULL)
1427 1.7.6.2 tls return (ENOBUFS);
1428 1.7.6.2 tls c->sc_buf = usbd_alloc_buffer(c->sc_xfer,
1429 1.7.6.2 tls sc->sc_bufsz);
1430 1.7.6.2 tls if (c->sc_buf == NULL) {
1431 1.7.6.2 tls usbd_free_xfer(c->sc_xfer);
1432 1.7.6.2 tls return (ENOBUFS);
1433 1.7.6.2 tls }
1434 1.7.6.2 tls }
1435 1.7.6.2 tls }
1436 1.7.6.2 tls
1437 1.7.6.2 tls return (0);
1438 1.7.6.2 tls }
1439 1.7.6.2 tls
1440 1.7.6.2 tls struct mbuf *
1441 1.7.6.2 tls smsc_newbuf(void)
1442 1.7.6.2 tls {
1443 1.7.6.2 tls struct mbuf *m;
1444 1.7.6.2 tls
1445 1.7.6.2 tls MGETHDR(m, M_DONTWAIT, MT_DATA);
1446 1.7.6.2 tls if (m == NULL)
1447 1.7.6.2 tls return (NULL);
1448 1.7.6.2 tls
1449 1.7.6.2 tls MCLGET(m, M_DONTWAIT);
1450 1.7.6.2 tls if (!(m->m_flags & M_EXT)) {
1451 1.7.6.2 tls m_freem(m);
1452 1.7.6.2 tls return (NULL);
1453 1.7.6.2 tls }
1454 1.7.6.2 tls
1455 1.7.6.2 tls return (m);
1456 1.7.6.2 tls }
1457 1.7.6.2 tls
1458 1.7.6.2 tls int
1459 1.7.6.2 tls smsc_encap(struct smsc_softc *sc, struct mbuf *m, int idx)
1460 1.7.6.2 tls {
1461 1.7.6.2 tls struct ifnet *ifp = &sc->sc_ec.ec_if;
1462 1.7.6.2 tls struct smsc_chain *c;
1463 1.7.6.2 tls usbd_status err;
1464 1.7.6.2 tls uint32_t txhdr;
1465 1.7.6.2 tls uint32_t frm_len = 0;
1466 1.7.6.2 tls
1467 1.7.6.2 tls c = &sc->sc_cdata.tx_chain[idx];
1468 1.7.6.2 tls
1469 1.7.6.2 tls /*
1470 1.7.6.2 tls * Each frame is prefixed with two 32-bit values describing the
1471 1.7.6.2 tls * length of the packet and buffer.
1472 1.7.6.2 tls */
1473 1.7.6.2 tls txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) |
1474 1.7.6.2 tls SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG;
1475 1.7.6.2 tls txhdr = htole32(txhdr);
1476 1.7.6.2 tls memcpy(c->sc_buf, &txhdr, sizeof(txhdr));
1477 1.7.6.2 tls
1478 1.7.6.2 tls txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len);
1479 1.7.6.2 tls txhdr = htole32(txhdr);
1480 1.7.6.2 tls memcpy(c->sc_buf + 4, &txhdr, sizeof(txhdr));
1481 1.7.6.2 tls
1482 1.7.6.2 tls frm_len += 8;
1483 1.7.6.2 tls
1484 1.7.6.2 tls /* Next copy in the actual packet */
1485 1.7.6.2 tls m_copydata(m, 0, m->m_pkthdr.len, c->sc_buf + frm_len);
1486 1.7.6.2 tls frm_len += m->m_pkthdr.len;
1487 1.7.6.2 tls
1488 1.7.6.2 tls c->sc_mbuf = m;
1489 1.7.6.2 tls
1490 1.7.6.2 tls usbd_setup_xfer(c->sc_xfer, sc->sc_ep[SMSC_ENDPT_TX],
1491 1.7.6.2 tls c, c->sc_buf, frm_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1492 1.7.6.2 tls 10000, smsc_txeof);
1493 1.7.6.2 tls
1494 1.7.6.2 tls err = usbd_transfer(c->sc_xfer);
1495 1.7.6.2 tls /* XXXNH get task to stop interface */
1496 1.7.6.2 tls if (err != USBD_IN_PROGRESS) {
1497 1.7.6.2 tls smsc_stop(ifp, 0);
1498 1.7.6.2 tls return (EIO);
1499 1.7.6.2 tls }
1500 1.7.6.2 tls
1501 1.7.6.2 tls sc->sc_cdata.tx_cnt++;
1502 1.7.6.2 tls
1503 1.7.6.2 tls return (0);
1504 1.7.6.2 tls }
1505