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