Sun Oct 2 10:39:29 2016 UTC ()
Use our if_percpupq... driver works again on rpi2


(skrll)
diff -r1.22.2.13 -r1.22.2.14 src/sys/dev/usb/if_smsc.c

cvs diff -r1.22.2.13 -r1.22.2.14 src/sys/dev/usb/if_smsc.c (switch to unified diff)

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