Thu Mar 3 05:48:14 2022 UTC ()
usbnet: Remove usbnet_set_dying.

Not necessary for the one caller that did it (url(4)): usbnet_detach
handles failed attach just fine without it.


(riastradh)
diff -r1.77 -r1.78 src/sys/dev/usb/if_url.c
diff -r1.54 -r1.55 src/sys/dev/usb/usbnet.c
diff -r1.22 -r1.23 src/sys/dev/usb/usbnet.h

cvs diff -r1.77 -r1.78 src/sys/dev/usb/if_url.c (switch to unified diff)

--- src/sys/dev/usb/if_url.c 2020/04/02 04:09:36 1.77
+++ src/sys/dev/usb/if_url.c 2022/03/03 05:48:14 1.78
@@ -1,782 +1,776 @@ @@ -1,782 +1,776 @@
1/* $NetBSD: if_url.c,v 1.77 2020/04/02 04:09:36 nisimura Exp $ */ 1/* $NetBSD: if_url.c,v 1.78 2022/03/03 05:48:14 riastradh Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2001, 2002 4 * Copyright (c) 2001, 2002
5 * Shingo WATANABE <nabe@nabechan.org>. All rights reserved. 5 * Shingo WATANABE <nabe@nabechan.org>. All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the author nor the names of any co-contributors 15 * 3. Neither the name of the author nor the names of any co-contributors
16 * may be used to endorse or promote products derived from this software 16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission. 17 * without specific prior written permission.
18 * 18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE. 29 * SUCH DAMAGE.
30 * 30 *
31 */ 31 */
32 32
33/* 33/*
34 * The RTL8150L(Realtek USB to fast ethernet controller) spec can be found at 34 * The RTL8150L(Realtek USB to fast ethernet controller) spec can be found at
35 * ftp://ftp.realtek.com.tw/lancard/data_sheet/8150/8150v14.pdf 35 * ftp://ftp.realtek.com.tw/lancard/data_sheet/8150/8150v14.pdf
36 * ftp://152.104.125.40/lancard/data_sheet/8150/8150v14.pdf 36 * ftp://152.104.125.40/lancard/data_sheet/8150/8150v14.pdf
37 */ 37 */
38 38
39/* 39/*
40 * TODO: 40 * TODO:
41 * Interrupt Endpoint support 41 * Interrupt Endpoint support
42 * External PHYs 42 * External PHYs
43 * powerhook() support? 43 * powerhook() support?
44 */ 44 */
45 45
46#include <sys/cdefs.h> 46#include <sys/cdefs.h>
47__KERNEL_RCSID(0, "$NetBSD: if_url.c,v 1.77 2020/04/02 04:09:36 nisimura Exp $"); 47__KERNEL_RCSID(0, "$NetBSD: if_url.c,v 1.78 2022/03/03 05:48:14 riastradh Exp $");
48 48
49#ifdef _KERNEL_OPT 49#ifdef _KERNEL_OPT
50#include "opt_inet.h" 50#include "opt_inet.h"
51#include "opt_usb.h" 51#include "opt_usb.h"
52#endif 52#endif
53 53
54#include <sys/param.h> 54#include <sys/param.h>
55 55
56#include <net/if_ether.h> 56#include <net/if_ether.h>
57#ifdef INET 57#ifdef INET
58#include <netinet/in.h> 58#include <netinet/in.h>
59#include <netinet/if_inarp.h> 59#include <netinet/if_inarp.h>
60#endif 60#endif
61 61
62#include <dev/mii/urlphyreg.h> 62#include <dev/mii/urlphyreg.h>
63 63
64#include <dev/usb/usbnet.h> 64#include <dev/usb/usbnet.h>
65 65
66#include <dev/usb/if_urlreg.h> 66#include <dev/usb/if_urlreg.h>
67 67
68/* Function declarations */ 68/* Function declarations */
69static int url_match(device_t, cfdata_t, void *); 69static int url_match(device_t, cfdata_t, void *);
70static void url_attach(device_t, device_t, void *); 70static void url_attach(device_t, device_t, void *);
71 71
72CFATTACH_DECL_NEW(url, sizeof(struct usbnet), url_match, url_attach, 72CFATTACH_DECL_NEW(url, sizeof(struct usbnet), url_match, url_attach,
73 usbnet_detach, usbnet_activate); 73 usbnet_detach, usbnet_activate);
74 74
75static unsigned url_uno_tx_prepare(struct usbnet *, struct mbuf *, 75static unsigned url_uno_tx_prepare(struct usbnet *, struct mbuf *,
76 struct usbnet_chain *); 76 struct usbnet_chain *);
77static void url_uno_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t); 77static void url_uno_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t);
78static int url_uno_mii_read_reg(struct usbnet *, int, int, uint16_t *); 78static int url_uno_mii_read_reg(struct usbnet *, int, int, uint16_t *);
79static int url_uno_mii_write_reg(struct usbnet *, int, int, uint16_t); 79static int url_uno_mii_write_reg(struct usbnet *, int, int, uint16_t);
80static int url_uno_ioctl(struct ifnet *, u_long, void *); 80static int url_uno_ioctl(struct ifnet *, u_long, void *);
81static void url_uno_stop(struct ifnet *, int); 81static void url_uno_stop(struct ifnet *, int);
82static void url_uno_mii_statchg(struct ifnet *); 82static void url_uno_mii_statchg(struct ifnet *);
83static int url_uno_init(struct ifnet *); 83static int url_uno_init(struct ifnet *);
84static void url_rcvfilt_locked(struct usbnet *); 84static void url_rcvfilt_locked(struct usbnet *);
85static void url_reset(struct usbnet *); 85static void url_reset(struct usbnet *);
86 86
87static int url_csr_read_1(struct usbnet *, int); 87static int url_csr_read_1(struct usbnet *, int);
88static int url_csr_read_2(struct usbnet *, int); 88static int url_csr_read_2(struct usbnet *, int);
89static int url_csr_write_1(struct usbnet *, int, int); 89static int url_csr_write_1(struct usbnet *, int, int);
90static int url_csr_write_2(struct usbnet *, int, int); 90static int url_csr_write_2(struct usbnet *, int, int);
91static int url_csr_write_4(struct usbnet *, int, int); 91static int url_csr_write_4(struct usbnet *, int, int);
92static int url_mem(struct usbnet *, int, int, void *, int); 92static int url_mem(struct usbnet *, int, int, void *, int);
93 93
94static const struct usbnet_ops url_ops = { 94static const struct usbnet_ops url_ops = {
95 .uno_stop = url_uno_stop, 95 .uno_stop = url_uno_stop,
96 .uno_ioctl = url_uno_ioctl, 96 .uno_ioctl = url_uno_ioctl,
97 .uno_read_reg = url_uno_mii_read_reg, 97 .uno_read_reg = url_uno_mii_read_reg,
98 .uno_write_reg = url_uno_mii_write_reg, 98 .uno_write_reg = url_uno_mii_write_reg,
99 .uno_statchg = url_uno_mii_statchg, 99 .uno_statchg = url_uno_mii_statchg,
100 .uno_tx_prepare = url_uno_tx_prepare, 100 .uno_tx_prepare = url_uno_tx_prepare,
101 .uno_rx_loop = url_uno_rx_loop, 101 .uno_rx_loop = url_uno_rx_loop,
102 .uno_init = url_uno_init, 102 .uno_init = url_uno_init,
103}; 103};
104 104
105/* Macros */ 105/* Macros */
106#ifdef URL_DEBUG 106#ifdef URL_DEBUG
107#define DPRINTF(x) if (urldebug) printf x 107#define DPRINTF(x) if (urldebug) printf x
108#define DPRINTFN(n, x) if (urldebug >= (n)) printf x 108#define DPRINTFN(n, x) if (urldebug >= (n)) printf x
109int urldebug = 0; 109int urldebug = 0;
110#else 110#else
111#define DPRINTF(x) 111#define DPRINTF(x)
112#define DPRINTFN(n, x) 112#define DPRINTFN(n, x)
113#endif 113#endif
114 114
115#define URL_SETBIT(un, reg, x) \ 115#define URL_SETBIT(un, reg, x) \
116 url_csr_write_1(un, reg, url_csr_read_1(un, reg) | (x)) 116 url_csr_write_1(un, reg, url_csr_read_1(un, reg) | (x))
117 117
118#define URL_SETBIT2(un, reg, x) \ 118#define URL_SETBIT2(un, reg, x) \
119 url_csr_write_2(un, reg, url_csr_read_2(un, reg) | (x)) 119 url_csr_write_2(un, reg, url_csr_read_2(un, reg) | (x))
120 120
121#define URL_CLRBIT(un, reg, x) \ 121#define URL_CLRBIT(un, reg, x) \
122 url_csr_write_1(un, reg, url_csr_read_1(un, reg) & ~(x)) 122 url_csr_write_1(un, reg, url_csr_read_1(un, reg) & ~(x))
123 123
124#define URL_CLRBIT2(un, reg, x) \ 124#define URL_CLRBIT2(un, reg, x) \
125 url_csr_write_2(un, reg, url_csr_read_2(un, reg) & ~(x)) 125 url_csr_write_2(un, reg, url_csr_read_2(un, reg) & ~(x))
126 126
127static const struct url_type { 127static const struct url_type {
128 struct usb_devno url_dev; 128 struct usb_devno url_dev;
129 uint16_t url_flags; 129 uint16_t url_flags;
130#define URL_EXT_PHY 0x0001 130#define URL_EXT_PHY 0x0001
131} url_devs [] = { 131} url_devs [] = {
132 /* MELCO LUA-KTX */ 132 /* MELCO LUA-KTX */
133 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX }, 0}, 133 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX }, 0},
134 /* Realtek RTL8150L Generic (GREEN HOUSE USBKR100) */ 134 /* Realtek RTL8150L Generic (GREEN HOUSE USBKR100) */
135 {{ USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8150L}, 0}, 135 {{ USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8150L}, 0},
136 /* Longshine LCS-8138TX */ 136 /* Longshine LCS-8138TX */
137 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_LCS8138TX}, 0}, 137 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_LCS8138TX}, 0},
138 /* Micronet SP128AR */ 138 /* Micronet SP128AR */
139 {{ USB_VENDOR_MICRONET, USB_PRODUCT_MICRONET_SP128AR}, 0}, 139 {{ USB_VENDOR_MICRONET, USB_PRODUCT_MICRONET_SP128AR}, 0},
140 /* OQO model 01 */ 140 /* OQO model 01 */
141 {{ USB_VENDOR_OQO, USB_PRODUCT_OQO_ETHER01}, 0}, 141 {{ USB_VENDOR_OQO, USB_PRODUCT_OQO_ETHER01}, 0},
142}; 142};
143#define url_lookup(v, p) ((const struct url_type *)usb_lookup(url_devs, v, p)) 143#define url_lookup(v, p) ((const struct url_type *)usb_lookup(url_devs, v, p))
144 144
145 145
146/* Probe */ 146/* Probe */
147static int 147static int
148url_match(device_t parent, cfdata_t match, void *aux) 148url_match(device_t parent, cfdata_t match, void *aux)
149{ 149{
150 struct usb_attach_arg *uaa = aux; 150 struct usb_attach_arg *uaa = aux;
151 151
152 return url_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ? 152 return url_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
153 UMATCH_VENDOR_PRODUCT : UMATCH_NONE; 153 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
154} 154}
155/* Attach */ 155/* Attach */
156static void 156static void
157url_attach(device_t parent, device_t self, void *aux) 157url_attach(device_t parent, device_t self, void *aux)
158{ 158{
159 USBNET_MII_DECL_DEFAULT(unm); 159 USBNET_MII_DECL_DEFAULT(unm);
160 struct usbnet * const un = device_private(self); 160 struct usbnet * const un = device_private(self);
161 struct usb_attach_arg *uaa = aux; 161 struct usb_attach_arg *uaa = aux;
162 struct usbd_device *dev = uaa->uaa_device; 162 struct usbd_device *dev = uaa->uaa_device;
163 struct usbd_interface *iface; 163 struct usbd_interface *iface;
164 usbd_status err; 164 usbd_status err;
165 usb_interface_descriptor_t *id; 165 usb_interface_descriptor_t *id;
166 usb_endpoint_descriptor_t *ed; 166 usb_endpoint_descriptor_t *ed;
167 char *devinfop; 167 char *devinfop;
168 int i; 168 int i;
169 169
170 aprint_naive("\n"); 170 aprint_naive("\n");
171 aprint_normal("\n"); 171 aprint_normal("\n");
172 devinfop = usbd_devinfo_alloc(dev, 0); 172 devinfop = usbd_devinfo_alloc(dev, 0);
173 aprint_normal_dev(self, "%s\n", devinfop); 173 aprint_normal_dev(self, "%s\n", devinfop);
174 usbd_devinfo_free(devinfop); 174 usbd_devinfo_free(devinfop);
175 175
176 un->un_dev = self; 176 un->un_dev = self;
177 un->un_udev = dev; 177 un->un_udev = dev;
178 un->un_sc = un; 178 un->un_sc = un;
179 un->un_ops = &url_ops; 179 un->un_ops = &url_ops;
180 un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; 180 un->un_rx_xfer_flags = USBD_SHORT_XFER_OK;
181 un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; 181 un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER;
182 un->un_rx_list_cnt = URL_RX_LIST_CNT; 182 un->un_rx_list_cnt = URL_RX_LIST_CNT;
183 un->un_tx_list_cnt = URL_TX_LIST_CNT; 183 un->un_tx_list_cnt = URL_TX_LIST_CNT;
184 un->un_rx_bufsz = URL_BUFSZ; 184 un->un_rx_bufsz = URL_BUFSZ;
185 un->un_tx_bufsz = URL_BUFSZ; 185 un->un_tx_bufsz = URL_BUFSZ;
186 186
187 /* Move the device into the configured state. */ 187 /* Move the device into the configured state. */
188 err = usbd_set_config_no(dev, URL_CONFIG_NO, 1); 188 err = usbd_set_config_no(dev, URL_CONFIG_NO, 1);
189 if (err) { 189 if (err) {
190 aprint_error_dev(self, "failed to set configuration" 190 aprint_error_dev(self, "failed to set configuration"
191 ", err=%s\n", usbd_errstr(err)); 191 ", err=%s\n", usbd_errstr(err));
192 return; 192 return;
193 } 193 }
194 194
195 /* get control interface */ 195 /* get control interface */
196 err = usbd_device2interface_handle(dev, URL_IFACE_INDEX, &iface); 196 err = usbd_device2interface_handle(dev, URL_IFACE_INDEX, &iface);
197 if (err) { 197 if (err) {
198 aprint_error_dev(self, "failed to get interface, err=%s\n", 198 aprint_error_dev(self, "failed to get interface, err=%s\n",
199 usbd_errstr(err)); 199 usbd_errstr(err));
200 return; 200 return;
201 } 201 }
202 202
203 un->un_iface = iface; 203 un->un_iface = iface;
204 un->un_flags = url_lookup(uaa->uaa_vendor, uaa->uaa_product)->url_flags; 204 un->un_flags = url_lookup(uaa->uaa_vendor, uaa->uaa_product)->url_flags;
205#if 0 205#if 0
206 if (un->un_flags & URL_EXT_PHY) { 206 if (un->un_flags & URL_EXT_PHY) {
207 un->un_read_reg_cb = url_ext_mii_read_reg; 207 un->un_read_reg_cb = url_ext_mii_read_reg;
208 un->un_write_reg_cb = url_ext_mii_write_reg; 208 un->un_write_reg_cb = url_ext_mii_write_reg;
209 } 209 }
210#endif 210#endif
211 211
212 /* get interface descriptor */ 212 /* get interface descriptor */
213 id = usbd_get_interface_descriptor(un->un_iface); 213 id = usbd_get_interface_descriptor(un->un_iface);
214 214
215 /* find endpoints */ 215 /* find endpoints */
216 un->un_ed[USBNET_ENDPT_RX] = un->un_ed[USBNET_ENDPT_TX] = 216 un->un_ed[USBNET_ENDPT_RX] = un->un_ed[USBNET_ENDPT_TX] =
217 un->un_ed[USBNET_ENDPT_INTR] = 0; 217 un->un_ed[USBNET_ENDPT_INTR] = 0;
218 for (i = 0; i < id->bNumEndpoints; i++) { 218 for (i = 0; i < id->bNumEndpoints; i++) {
219 ed = usbd_interface2endpoint_descriptor(un->un_iface, i); 219 ed = usbd_interface2endpoint_descriptor(un->un_iface, i);
220 if (ed == NULL) { 220 if (ed == NULL) {
221 aprint_error_dev(self, 221 aprint_error_dev(self,
222 "couldn't get endpoint %d\n", i); 222 "couldn't get endpoint %d\n", i);
223 return; 223 return;
224 } 224 }
225 if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK && 225 if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
226 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) 226 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
227 un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress; 227 un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress;
228 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK && 228 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
229 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT) 229 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT)
230 un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress; 230 un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress;
231 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT && 231 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT &&
232 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) 232 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
233 un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress; 233 un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress;
234 } 234 }
235 235
236 if (un->un_ed[USBNET_ENDPT_RX] == 0 || 236 if (un->un_ed[USBNET_ENDPT_RX] == 0 ||
237 un->un_ed[USBNET_ENDPT_TX] == 0 || 237 un->un_ed[USBNET_ENDPT_TX] == 0 ||
238 un->un_ed[USBNET_ENDPT_INTR] == 0) { 238 un->un_ed[USBNET_ENDPT_INTR] == 0) {
239 aprint_error_dev(self, "missing endpoint\n"); 239 aprint_error_dev(self, "missing endpoint\n");
240 return; 240 return;
241 } 241 }
242 242
243 /* Set these up now for url_mem(). */ 243 /* Set these up now for url_mem(). */
244 usbnet_attach(un, "urldet"); 244 usbnet_attach(un, "urldet");
245 245
246 usbnet_lock_core(un); 246 usbnet_lock_core(un);
247 usbnet_busy(un); 247 usbnet_busy(un);
248 248
249 /* reset the adapter */ 249 /* reset the adapter */
250 url_reset(un); 250 url_reset(un);
251 251
252 /* Get Ethernet Address */ 252 /* Get Ethernet Address */
253 err = url_mem(un, URL_CMD_READMEM, URL_IDR0, (void *)un->un_eaddr, 253 err = url_mem(un, URL_CMD_READMEM, URL_IDR0, (void *)un->un_eaddr,
254 ETHER_ADDR_LEN); 254 ETHER_ADDR_LEN);
255 usbnet_unbusy(un); 255 usbnet_unbusy(un);
256 usbnet_unlock_core(un); 256 usbnet_unlock_core(un);
257 if (err) { 257 if (err) {
258 aprint_error_dev(self, "read MAC address failed\n"); 258 aprint_error_dev(self, "read MAC address failed\n");
259 goto bad; 259 return;
260 } 260 }
261 261
262 /* initialize interface information */ 262 /* initialize interface information */
263 usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, 263 usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST,
264 0, &unm); 264 0, &unm);
265 
266 return; 
267 
268 bad: 
269 usbnet_set_dying(un, true); 
270 return; 
271} 265}
272 266
273/* read/write memory */ 267/* read/write memory */
274static int 268static int
275url_mem(struct usbnet *un, int cmd, int offset, void *buf, int len) 269url_mem(struct usbnet *un, int cmd, int offset, void *buf, int len)
276{ 270{
277 usb_device_request_t req; 271 usb_device_request_t req;
278 usbd_status err; 272 usbd_status err;
279 273
280 usbnet_isowned_core(un); 274 usbnet_isowned_core(un);
281 275
282 DPRINTFN(0x200, 276 DPRINTFN(0x200,
283 ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); 277 ("%s: %s: enter\n", device_xname(un->un_dev), __func__));
284 278
285 if (usbnet_isdying(un)) 279 if (usbnet_isdying(un))
286 return 0; 280 return 0;
287 281
288 if (cmd == URL_CMD_READMEM) 282 if (cmd == URL_CMD_READMEM)
289 req.bmRequestType = UT_READ_VENDOR_DEVICE; 283 req.bmRequestType = UT_READ_VENDOR_DEVICE;
290 else 284 else
291 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 285 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
292 req.bRequest = URL_REQ_MEM; 286 req.bRequest = URL_REQ_MEM;
293 USETW(req.wValue, offset); 287 USETW(req.wValue, offset);
294 USETW(req.wIndex, 0x0000); 288 USETW(req.wIndex, 0x0000);
295 USETW(req.wLength, len); 289 USETW(req.wLength, len);
296 290
297 err = usbd_do_request(un->un_udev, &req, buf); 291 err = usbd_do_request(un->un_udev, &req, buf);
298 if (err) { 292 if (err) {
299 DPRINTF(("%s: url_mem(): %s failed. off=%04x, err=%d\n", 293 DPRINTF(("%s: url_mem(): %s failed. off=%04x, err=%d\n",
300 device_xname(un->un_dev), 294 device_xname(un->un_dev),
301 cmd == URL_CMD_READMEM ? "read" : "write", 295 cmd == URL_CMD_READMEM ? "read" : "write",
302 offset, err)); 296 offset, err));
303 } 297 }
304 298
305 return err; 299 return err;
306} 300}
307 301
308/* read 1byte from register */ 302/* read 1byte from register */
309static int 303static int
310url_csr_read_1(struct usbnet *un, int reg) 304url_csr_read_1(struct usbnet *un, int reg)
311{ 305{
312 uint8_t val = 0; 306 uint8_t val = 0;
313 307
314 DPRINTFN(0x100, 308 DPRINTFN(0x100,
315 ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); 309 ("%s: %s: enter\n", device_xname(un->un_dev), __func__));
316 310
317 return url_mem(un, URL_CMD_READMEM, reg, &val, 1) ? 0 : val; 311 return url_mem(un, URL_CMD_READMEM, reg, &val, 1) ? 0 : val;
318} 312}
319 313
320/* read 2bytes from register */ 314/* read 2bytes from register */
321static int 315static int
322url_csr_read_2(struct usbnet *un, int reg) 316url_csr_read_2(struct usbnet *un, int reg)
323{ 317{
324 uWord val; 318 uWord val;
325 319
326 DPRINTFN(0x100, 320 DPRINTFN(0x100,
327 ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); 321 ("%s: %s: enter\n", device_xname(un->un_dev), __func__));
328 322
329 USETW(val, 0); 323 USETW(val, 0);
330 return url_mem(un, URL_CMD_READMEM, reg, &val, 2) ? 0 : UGETW(val); 324 return url_mem(un, URL_CMD_READMEM, reg, &val, 2) ? 0 : UGETW(val);
331} 325}
332 326
333/* write 1byte to register */ 327/* write 1byte to register */
334static int 328static int
335url_csr_write_1(struct usbnet *un, int reg, int aval) 329url_csr_write_1(struct usbnet *un, int reg, int aval)
336{ 330{
337 uint8_t val = aval; 331 uint8_t val = aval;
338 332
339 DPRINTFN(0x100, 333 DPRINTFN(0x100,
340 ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); 334 ("%s: %s: enter\n", device_xname(un->un_dev), __func__));
341 335
342 return url_mem(un, URL_CMD_WRITEMEM, reg, &val, 1) ? -1 : 0; 336 return url_mem(un, URL_CMD_WRITEMEM, reg, &val, 1) ? -1 : 0;
343} 337}
344 338
345/* write 2bytes to register */ 339/* write 2bytes to register */
346static int 340static int
347url_csr_write_2(struct usbnet *un, int reg, int aval) 341url_csr_write_2(struct usbnet *un, int reg, int aval)
348{ 342{
349 uWord val; 343 uWord val;
350 344
351 DPRINTFN(0x100, 345 DPRINTFN(0x100,
352 ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); 346 ("%s: %s: enter\n", device_xname(un->un_dev), __func__));
353 347
354 USETW(val, aval); 348 USETW(val, aval);
355 349
356 return url_mem(un, URL_CMD_WRITEMEM, reg, &val, 2) ? -1 : 0; 350 return url_mem(un, URL_CMD_WRITEMEM, reg, &val, 2) ? -1 : 0;
357} 351}
358 352
359/* write 4bytes to register */ 353/* write 4bytes to register */
360static int 354static int
361url_csr_write_4(struct usbnet *un, int reg, int aval) 355url_csr_write_4(struct usbnet *un, int reg, int aval)
362{ 356{
363 uDWord val; 357 uDWord val;
364 358
365 DPRINTFN(0x100, 359 DPRINTFN(0x100,
366 ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); 360 ("%s: %s: enter\n", device_xname(un->un_dev), __func__));
367 361
368 USETDW(val, aval); 362 USETDW(val, aval);
369 363
370 return url_mem(un, URL_CMD_WRITEMEM, reg, &val, 4) ? -1 : 0; 364 return url_mem(un, URL_CMD_WRITEMEM, reg, &val, 4) ? -1 : 0;
371} 365}
372 366
373static int 367static int
374url_init_locked(struct ifnet *ifp) 368url_init_locked(struct ifnet *ifp)
375{ 369{
376 struct usbnet * const un = ifp->if_softc; 370 struct usbnet * const un = ifp->if_softc;
377 const u_char *eaddr; 371 const u_char *eaddr;
378 int i; 372 int i;
379 373
380 DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__)); 374 DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
381 375
382 usbnet_isowned_core(un); 376 usbnet_isowned_core(un);
383 377
384 if (usbnet_isdying(un)) 378 if (usbnet_isdying(un))
385 return EIO; 379 return EIO;
386 380
387 /* Cancel pending I/O and free all TX/RX buffers */ 381 /* Cancel pending I/O and free all TX/RX buffers */
388 usbnet_stop(un, ifp, 1); 382 usbnet_stop(un, ifp, 1);
389 383
390 eaddr = CLLADDR(ifp->if_sadl); 384 eaddr = CLLADDR(ifp->if_sadl);
391 for (i = 0; i < ETHER_ADDR_LEN; i++) 385 for (i = 0; i < ETHER_ADDR_LEN; i++)
392 url_csr_write_1(un, URL_IDR0 + i, eaddr[i]); 386 url_csr_write_1(un, URL_IDR0 + i, eaddr[i]);
393 387
394 /* Init transmission control register */ 388 /* Init transmission control register */
395 URL_CLRBIT(un, URL_TCR, 389 URL_CLRBIT(un, URL_TCR,
396 URL_TCR_TXRR1 | URL_TCR_TXRR0 | 390 URL_TCR_TXRR1 | URL_TCR_TXRR0 |
397 URL_TCR_IFG1 | URL_TCR_IFG0 | 391 URL_TCR_IFG1 | URL_TCR_IFG0 |
398 URL_TCR_NOCRC); 392 URL_TCR_NOCRC);
399 393
400 /* Init receive control register */ 394 /* Init receive control register */
401 URL_SETBIT2(un, URL_RCR, URL_RCR_TAIL | URL_RCR_AD | URL_RCR_AB); 395 URL_SETBIT2(un, URL_RCR, URL_RCR_TAIL | URL_RCR_AD | URL_RCR_AB);
402 396
403 /* Accept multicast frame or run promisc. mode */ 397 /* Accept multicast frame or run promisc. mode */
404 url_rcvfilt_locked(un); 398 url_rcvfilt_locked(un);
405 399
406 /* Enable RX and TX */ 400 /* Enable RX and TX */
407 URL_SETBIT(un, URL_CR, URL_CR_TE | URL_CR_RE); 401 URL_SETBIT(un, URL_CR, URL_CR_TE | URL_CR_RE);
408 402
409 return usbnet_init_rx_tx(un); 403 return usbnet_init_rx_tx(un);
410} 404}
411 405
412static int 406static int
413url_uno_init(struct ifnet *ifp) 407url_uno_init(struct ifnet *ifp)
414{ 408{
415 struct usbnet * const un = ifp->if_softc; 409 struct usbnet * const un = ifp->if_softc;
416 410
417 usbnet_lock_core(un); 411 usbnet_lock_core(un);
418 usbnet_busy(un); 412 usbnet_busy(un);
419 int ret = url_init_locked(ifp); 413 int ret = url_init_locked(ifp);
420 usbnet_unbusy(un); 414 usbnet_unbusy(un);
421 usbnet_unlock_core(un); 415 usbnet_unlock_core(un);
422 416
423 return ret; 417 return ret;
424} 418}
425 419
426static void 420static void
427url_reset(struct usbnet *un) 421url_reset(struct usbnet *un)
428{ 422{
429 int i; 423 int i;
430 424
431 DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__)); 425 DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
432 426
433 if (usbnet_isdying(un)) 427 if (usbnet_isdying(un))
434 return; 428 return;
435 429
436 URL_SETBIT(un, URL_CR, URL_CR_SOFT_RST); 430 URL_SETBIT(un, URL_CR, URL_CR_SOFT_RST);
437 431
438 for (i = 0; i < URL_TX_TIMEOUT; i++) { 432 for (i = 0; i < URL_TX_TIMEOUT; i++) {
439 if (!(url_csr_read_1(un, URL_CR) & URL_CR_SOFT_RST)) 433 if (!(url_csr_read_1(un, URL_CR) & URL_CR_SOFT_RST))
440 break; 434 break;
441 delay(10); /* XXX */ 435 delay(10); /* XXX */
442 } 436 }
443 437
444 delay(10000); /* XXX */ 438 delay(10000); /* XXX */
445} 439}
446 440
447static void 441static void
448url_rcvfilt_locked(struct usbnet *un) 442url_rcvfilt_locked(struct usbnet *un)
449{ 443{
450 struct ifnet * const ifp = usbnet_ifp(un); 444 struct ifnet * const ifp = usbnet_ifp(un);
451 struct ethercom *ec = usbnet_ec(un); 445 struct ethercom *ec = usbnet_ec(un);
452 struct ether_multi *enm; 446 struct ether_multi *enm;
453 struct ether_multistep step; 447 struct ether_multistep step;
454 uint32_t mchash[2] = { 0, 0 }; 448 uint32_t mchash[2] = { 0, 0 };
455 int h = 0, rcr; 449 int h = 0, rcr;
456 450
457 DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__)); 451 DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
458 452
459 usbnet_isowned_core(un); 453 usbnet_isowned_core(un);
460 454
461 if (usbnet_isdying(un)) 455 if (usbnet_isdying(un))
462 return; 456 return;
463 457
464 rcr = url_csr_read_2(un, URL_RCR); 458 rcr = url_csr_read_2(un, URL_RCR);
465 rcr &= ~(URL_RCR_AAP | URL_RCR_AAM | URL_RCR_AM); 459 rcr &= ~(URL_RCR_AAP | URL_RCR_AAM | URL_RCR_AM);
466 460
467 ETHER_LOCK(ec); 461 ETHER_LOCK(ec);
468 if (ifp->if_flags & IFF_PROMISC) { 462 if (ifp->if_flags & IFF_PROMISC) {
469 ec->ec_flags |= ETHER_F_ALLMULTI; 463 ec->ec_flags |= ETHER_F_ALLMULTI;
470 ETHER_UNLOCK(ec); 464 ETHER_UNLOCK(ec);
471 /* run promisc. mode */ 465 /* run promisc. mode */
472 rcr |= URL_RCR_AAM; /* ??? */ 466 rcr |= URL_RCR_AAM; /* ??? */
473 rcr |= URL_RCR_AAP; 467 rcr |= URL_RCR_AAP;
474 goto update; 468 goto update;
475 } 469 }
476 ec->ec_flags &= ~ETHER_F_ALLMULTI; 470 ec->ec_flags &= ~ETHER_F_ALLMULTI;
477 ETHER_FIRST_MULTI(step, ec, enm); 471 ETHER_FIRST_MULTI(step, ec, enm);
478 while (enm != NULL) { 472 while (enm != NULL) {
479 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) { 473 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
480 ec->ec_flags |= ETHER_F_ALLMULTI; 474 ec->ec_flags |= ETHER_F_ALLMULTI;
481 ETHER_UNLOCK(ec); 475 ETHER_UNLOCK(ec);
482 /* accept all multicast frames */ 476 /* accept all multicast frames */
483 rcr |= URL_RCR_AAM; 477 rcr |= URL_RCR_AAM;
484 goto update; 478 goto update;
485 } 479 }
486 h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN); 480 h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
487 /* 1(31) and 5(30:26) bit sampling */ 481 /* 1(31) and 5(30:26) bit sampling */
488 mchash[h >> 31] |= 1 << ((h >> 26) & 0x1f); 482 mchash[h >> 31] |= 1 << ((h >> 26) & 0x1f);
489 ETHER_NEXT_MULTI(step, enm); 483 ETHER_NEXT_MULTI(step, enm);
490 } 484 }
491 ETHER_UNLOCK(ec); 485 ETHER_UNLOCK(ec);
492 if (h != 0) 486 if (h != 0)
493 rcr |= URL_RCR_AM; /* activate mcast hash filter */ 487 rcr |= URL_RCR_AM; /* activate mcast hash filter */
494 url_csr_write_4(un, URL_MAR0, mchash[0]); 488 url_csr_write_4(un, URL_MAR0, mchash[0]);
495 url_csr_write_4(un, URL_MAR4, mchash[1]); 489 url_csr_write_4(un, URL_MAR4, mchash[1]);
496 update: 490 update:
497 url_csr_write_2(un, URL_RCR, rcr); 491 url_csr_write_2(un, URL_RCR, rcr);
498} 492}
499 493
500static unsigned 494static unsigned
501url_uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) 495url_uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
502{ 496{
503 int total_len; 497 int total_len;
504 498
505 DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev),__func__)); 499 DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev),__func__));
506 500
507 KASSERT(un->un_tx_bufsz >= URL_MIN_FRAME_LEN); 501 KASSERT(un->un_tx_bufsz >= URL_MIN_FRAME_LEN);
508 if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz) 502 if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz)
509 return 0; 503 return 0;
510 504
511 /* Copy the mbuf data into a contiguous buffer */ 505 /* Copy the mbuf data into a contiguous buffer */
512 m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf); 506 m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf);
513 total_len = m->m_pkthdr.len; 507 total_len = m->m_pkthdr.len;
514 508
515 if (total_len < URL_MIN_FRAME_LEN) { 509 if (total_len < URL_MIN_FRAME_LEN) {
516 memset(c->unc_buf + total_len, 0, 510 memset(c->unc_buf + total_len, 0,
517 URL_MIN_FRAME_LEN - total_len); 511 URL_MIN_FRAME_LEN - total_len);
518 total_len = URL_MIN_FRAME_LEN; 512 total_len = URL_MIN_FRAME_LEN;
519 } 513 }
520 514
521 DPRINTF(("%s: %s: send %d bytes\n", device_xname(un->un_dev), 515 DPRINTF(("%s: %s: send %d bytes\n", device_xname(un->un_dev),
522 __func__, total_len)); 516 __func__, total_len));
523 517
524 return total_len; 518 return total_len;
525} 519}
526 520
527static void 521static void
528url_uno_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len) 522url_uno_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len)
529{ 523{
530 struct ifnet *ifp = usbnet_ifp(un); 524 struct ifnet *ifp = usbnet_ifp(un);
531 url_rxhdr_t rxhdr; 525 url_rxhdr_t rxhdr;
532 526
533 DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev),__func__)); 527 DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev),__func__));
534 528
535 if (total_len <= ETHER_CRC_LEN || total_len <= sizeof(rxhdr)) { 529 if (total_len <= ETHER_CRC_LEN || total_len <= sizeof(rxhdr)) {
536 if_statinc(ifp, if_ierrors); 530 if_statinc(ifp, if_ierrors);
537 return; 531 return;
538 } 532 }
539 533
540 memcpy(&rxhdr, c->unc_buf + total_len - ETHER_CRC_LEN, sizeof(rxhdr)); 534 memcpy(&rxhdr, c->unc_buf + total_len - ETHER_CRC_LEN, sizeof(rxhdr));
541 535
542 DPRINTF(("%s: RX Status: %dbytes%s%s%s%s packets\n", 536 DPRINTF(("%s: RX Status: %dbytes%s%s%s%s packets\n",
543 device_xname(un->un_dev), 537 device_xname(un->un_dev),
544 UGETW(rxhdr) & URL_RXHDR_BYTEC_MASK, 538 UGETW(rxhdr) & URL_RXHDR_BYTEC_MASK,
545 UGETW(rxhdr) & URL_RXHDR_VALID_MASK ? ", Valid" : "", 539 UGETW(rxhdr) & URL_RXHDR_VALID_MASK ? ", Valid" : "",
546 UGETW(rxhdr) & URL_RXHDR_RUNTPKT_MASK ? ", Runt" : "", 540 UGETW(rxhdr) & URL_RXHDR_RUNTPKT_MASK ? ", Runt" : "",
547 UGETW(rxhdr) & URL_RXHDR_PHYPKT_MASK ? ", Physical match" : "", 541 UGETW(rxhdr) & URL_RXHDR_PHYPKT_MASK ? ", Physical match" : "",
548 UGETW(rxhdr) & URL_RXHDR_MCASTPKT_MASK ? ", Multicast" : "")); 542 UGETW(rxhdr) & URL_RXHDR_MCASTPKT_MASK ? ", Multicast" : ""));
549 543
550 if ((UGETW(rxhdr) & URL_RXHDR_VALID_MASK) == 0) { 544 if ((UGETW(rxhdr) & URL_RXHDR_VALID_MASK) == 0) {
551 if_statinc(ifp, if_ierrors); 545 if_statinc(ifp, if_ierrors);
552 return; 546 return;
553 } 547 }
554 548
555 total_len -= ETHER_CRC_LEN; 549 total_len -= ETHER_CRC_LEN;
556 550
557 DPRINTF(("%s: %s: deliver %d\n", device_xname(un->un_dev), 551 DPRINTF(("%s: %s: deliver %d\n", device_xname(un->un_dev),
558 __func__, total_len)); 552 __func__, total_len));
559 usbnet_enqueue(un, c->unc_buf, total_len, 0, 0, 0); 553 usbnet_enqueue(un, c->unc_buf, total_len, 0, 0, 0);
560} 554}
561 555
562#if 0 556#if 0
563static void url_intr(void) 557static void url_intr(void)
564{ 558{
565} 559}
566#endif 560#endif
567 561
568static int 562static int
569url_uno_ioctl(struct ifnet *ifp, u_long cmd, void *data) 563url_uno_ioctl(struct ifnet *ifp, u_long cmd, void *data)
570{ 564{
571 struct usbnet * const un = ifp->if_softc; 565 struct usbnet * const un = ifp->if_softc;
572 566
573 usbnet_lock_core(un); 567 usbnet_lock_core(un);
574 usbnet_busy(un); 568 usbnet_busy(un);
575 569
576 switch (cmd) { 570 switch (cmd) {
577 case SIOCADDMULTI: 571 case SIOCADDMULTI:
578 case SIOCDELMULTI: 572 case SIOCDELMULTI:
579 url_rcvfilt_locked(un); 573 url_rcvfilt_locked(un);
580 break; 574 break;
581 default: 575 default:
582 break; 576 break;
583 } 577 }
584 578
585 usbnet_unbusy(un); 579 usbnet_unbusy(un);
586 usbnet_unlock_core(un); 580 usbnet_unlock_core(un);
587 581
588 return 0; 582 return 0;
589} 583}
590 584
591/* Stop the adapter and free any mbufs allocated to the RX and TX lists. */ 585/* Stop the adapter and free any mbufs allocated to the RX and TX lists. */
592static void 586static void
593url_uno_stop(struct ifnet *ifp, int disable) 587url_uno_stop(struct ifnet *ifp, int disable)
594{ 588{
595 struct usbnet * const un = ifp->if_softc; 589 struct usbnet * const un = ifp->if_softc;
596 590
597 DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__)); 591 DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
598 592
599 url_reset(un); 593 url_reset(un);
600} 594}
601 595
602static int 596static int
603url_uno_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val) 597url_uno_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val)
604{ 598{
605 uint16_t data; 599 uint16_t data;
606 usbd_status err = USBD_NORMAL_COMPLETION; 600 usbd_status err = USBD_NORMAL_COMPLETION;
607 601
608 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n", 602 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n",
609 device_xname(un->un_dev), __func__, phy, reg)); 603 device_xname(un->un_dev), __func__, phy, reg));
610 604
611 /* XXX: one PHY only for the RTL8150 internal PHY */ 605 /* XXX: one PHY only for the RTL8150 internal PHY */
612 if (phy != 0) { 606 if (phy != 0) {
613 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n", 607 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
614 device_xname(un->un_dev), __func__, phy)); 608 device_xname(un->un_dev), __func__, phy));
615 return EINVAL; 609 return EINVAL;
616 } 610 }
617 611
618 switch (reg) { 612 switch (reg) {
619 case MII_BMCR: /* Control Register */ 613 case MII_BMCR: /* Control Register */
620 reg = URL_BMCR; 614 reg = URL_BMCR;
621 break; 615 break;
622 case MII_BMSR: /* Status Register */ 616 case MII_BMSR: /* Status Register */
623 reg = URL_BMSR; 617 reg = URL_BMSR;
624 break; 618 break;
625 case MII_PHYIDR1: 619 case MII_PHYIDR1:
626 case MII_PHYIDR2: 620 case MII_PHYIDR2:
627 *val = 0; 621 *val = 0;
628 goto R_DONE; 622 goto R_DONE;
629 break; 623 break;
630 case MII_ANAR: /* Autonegotiation advertisement */ 624 case MII_ANAR: /* Autonegotiation advertisement */
631 reg = URL_ANAR; 625 reg = URL_ANAR;
632 break; 626 break;
633 case MII_ANLPAR: /* Autonegotiation link partner abilities */ 627 case MII_ANLPAR: /* Autonegotiation link partner abilities */
634 reg = URL_ANLP; 628 reg = URL_ANLP;
635 break; 629 break;
636 case URLPHY_MSR: /* Media Status Register */ 630 case URLPHY_MSR: /* Media Status Register */
637 reg = URL_MSR; 631 reg = URL_MSR;
638 break; 632 break;
639 default: 633 default:
640 printf("%s: %s: bad register %04x\n", 634 printf("%s: %s: bad register %04x\n",
641 device_xname(un->un_dev), __func__, reg); 635 device_xname(un->un_dev), __func__, reg);
642 return EINVAL; 636 return EINVAL;
643 } 637 }
644 638
645 if (reg == URL_MSR) 639 if (reg == URL_MSR)
646 data = url_csr_read_1(un, reg); 640 data = url_csr_read_1(un, reg);
647 else 641 else
648 data = url_csr_read_2(un, reg); 642 data = url_csr_read_2(un, reg);
649 *val = data; 643 *val = data;
650 644
651 R_DONE: 645 R_DONE:
652 DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04hx\n", 646 DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04hx\n",
653 device_xname(un->un_dev), __func__, phy, reg, *val)); 647 device_xname(un->un_dev), __func__, phy, reg, *val));
654 648
655 return err; 649 return err;
656} 650}
657 651
658static int 652static int
659url_uno_mii_write_reg(struct usbnet *un, int phy, int reg, uint16_t val) 653url_uno_mii_write_reg(struct usbnet *un, int phy, int reg, uint16_t val)
660{ 654{
661 655
662 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x val=0x%04hx\n", 656 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x val=0x%04hx\n",
663 device_xname(un->un_dev), __func__, phy, reg, val)); 657 device_xname(un->un_dev), __func__, phy, reg, val));
664 658
665 /* XXX: one PHY only for the RTL8150 internal PHY */ 659 /* XXX: one PHY only for the RTL8150 internal PHY */
666 if (phy != 0) { 660 if (phy != 0) {
667 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n", 661 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
668 device_xname(un->un_dev), __func__, phy)); 662 device_xname(un->un_dev), __func__, phy));
669 return EINVAL; 663 return EINVAL;
670 } 664 }
671 665
672 switch (reg) { 666 switch (reg) {
673 case MII_BMCR: /* Control Register */ 667 case MII_BMCR: /* Control Register */
674 reg = URL_BMCR; 668 reg = URL_BMCR;
675 break; 669 break;
676 case MII_BMSR: /* Status Register */ 670 case MII_BMSR: /* Status Register */
677 reg = URL_BMSR; 671 reg = URL_BMSR;
678 break; 672 break;
679 case MII_PHYIDR1: 673 case MII_PHYIDR1:
680 case MII_PHYIDR2: 674 case MII_PHYIDR2:
681 return 0; 675 return 0;
682 case MII_ANAR: /* Autonegotiation advertisement */ 676 case MII_ANAR: /* Autonegotiation advertisement */
683 reg = URL_ANAR; 677 reg = URL_ANAR;
684 break; 678 break;
685 case MII_ANLPAR: /* Autonegotiation link partner abilities */ 679 case MII_ANLPAR: /* Autonegotiation link partner abilities */
686 reg = URL_ANLP; 680 reg = URL_ANLP;
687 break; 681 break;
688 case URLPHY_MSR: /* Media Status Register */ 682 case URLPHY_MSR: /* Media Status Register */
689 reg = URL_MSR; 683 reg = URL_MSR;
690 break; 684 break;
691 default: 685 default:
692 printf("%s: %s: bad register %04x\n", 686 printf("%s: %s: bad register %04x\n",
693 device_xname(un->un_dev), __func__, reg); 687 device_xname(un->un_dev), __func__, reg);
694 return EINVAL; 688 return EINVAL;
695 } 689 }
696 690
697 if (reg == URL_MSR) 691 if (reg == URL_MSR)
698 url_csr_write_1(un, reg, val); 692 url_csr_write_1(un, reg, val);
699 else 693 else
700 url_csr_write_2(un, reg, val); 694 url_csr_write_2(un, reg, val);
701 695
702 return 0; 696 return 0;
703} 697}
704 698
705static void 699static void
706url_uno_mii_statchg(struct ifnet *ifp) 700url_uno_mii_statchg(struct ifnet *ifp)
707{ 701{
708 struct usbnet * const un = ifp->if_softc; 702 struct usbnet * const un = ifp->if_softc;
709 703
710 DPRINTF(("%s: %s: enter\n", ifp->if_xname, __func__)); 704 DPRINTF(("%s: %s: enter\n", ifp->if_xname, __func__));
711 705
712 /* XXX */ 706 /* XXX */
713 usbnet_set_link(un, true); 707 usbnet_set_link(un, true);
714} 708}
715 709
716#if 0 710#if 0
717/* 711/*
718 * external PHYs support, but not test. 712 * external PHYs support, but not test.
719 */ 713 */
720static usbd_status 714static usbd_status
721url_ext_mii_read_reg(struct usbnet *un, int phy, int reg) 715url_ext_mii_read_reg(struct usbnet *un, int phy, int reg)
722{ 716{
723 uint16_t val; 717 uint16_t val;
724 718
725 DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x\n", 719 DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x\n",
726 device_xname(un->un_dev), __func__, phy, reg)); 720 device_xname(un->un_dev), __func__, phy, reg));
727 721
728 url_csr_write_1(un, URL_PHYADD, phy & URL_PHYADD_MASK); 722 url_csr_write_1(un, URL_PHYADD, phy & URL_PHYADD_MASK);
729 /* 723 /*
730 * RTL8150L will initiate a MII management data transaction 724 * RTL8150L will initiate a MII management data transaction
731 * if PHYCNT_OWN bit is set 1 by software. After transaction, 725 * if PHYCNT_OWN bit is set 1 by software. After transaction,
732 * this bit is auto cleared by TRL8150L. 726 * this bit is auto cleared by TRL8150L.
733 */ 727 */
734 url_csr_write_1(un, URL_PHYCNT, 728 url_csr_write_1(un, URL_PHYCNT,
735 (reg | URL_PHYCNT_PHYOWN) & ~URL_PHYCNT_RWCR); 729 (reg | URL_PHYCNT_PHYOWN) & ~URL_PHYCNT_RWCR);
736 for (i = 0; i < URL_TIMEOUT; i++) { 730 for (i = 0; i < URL_TIMEOUT; i++) {
737 if ((url_csr_read_1(un, URL_PHYCNT) & URL_PHYCNT_PHYOWN) == 0) 731 if ((url_csr_read_1(un, URL_PHYCNT) & URL_PHYCNT_PHYOWN) == 0)
738 break; 732 break;
739 } 733 }
740 if (i == URL_TIMEOUT) { 734 if (i == URL_TIMEOUT) {
741 printf("%s: MII read timed out\n", device_xname(un->un_dev)); 735 printf("%s: MII read timed out\n", device_xname(un->un_dev));
742 } 736 }
743 737
744 val = url_csr_read_2(un, URL_PHYDAT); 738 val = url_csr_read_2(un, URL_PHYDAT);
745 739
746 DPRINTF(("%s: %s: phy=%d reg=0x%04x => 0x%04x\n", 740 DPRINTF(("%s: %s: phy=%d reg=0x%04x => 0x%04x\n",
747 device_xname(un->un_dev), __func__, phy, reg, val)); 741 device_xname(un->un_dev), __func__, phy, reg, val));
748 742
749 return USBD_NORMAL_COMPLETION; 743 return USBD_NORMAL_COMPLETION;
750} 744}
751 745
752static usbd_status 746static usbd_status
753url_ext_mii_write_reg(struct usbnet *un, int phy, int reg, int data) 747url_ext_mii_write_reg(struct usbnet *un, int phy, int reg, int data)
754{ 748{
755 749
756 DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n", 750 DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n",
757 device_xname(un->un_dev), __func__, phy, reg, data)); 751 device_xname(un->un_dev), __func__, phy, reg, data));
758 752
759 url_csr_write_2(un, URL_PHYDAT, data); 753 url_csr_write_2(un, URL_PHYDAT, data);
760 url_csr_write_1(un, URL_PHYADD, phy); 754 url_csr_write_1(un, URL_PHYADD, phy);
761 url_csr_write_1(un, URL_PHYCNT, reg | URL_PHYCNT_RWCR); /* Write */ 755 url_csr_write_1(un, URL_PHYCNT, reg | URL_PHYCNT_RWCR); /* Write */
762 756
763 for (i=0; i < URL_TIMEOUT; i++) { 757 for (i=0; i < URL_TIMEOUT; i++) {
764 if (url_csr_read_1(un, URL_PHYCNT) & URL_PHYCNT_PHYOWN) 758 if (url_csr_read_1(un, URL_PHYCNT) & URL_PHYCNT_PHYOWN)
765 break; 759 break;
766 } 760 }
767 761
768 if (i == URL_TIMEOUT) { 762 if (i == URL_TIMEOUT) {
769 printf("%s: MII write timed out\n", 763 printf("%s: MII write timed out\n",
770 device_xname(un->un_dev)); 764 device_xname(un->un_dev));
771 return USBD_TIMEOUT; 765 return USBD_TIMEOUT;
772 } 766 }
773 767
774 return USBD_NORMAL_COMPLETION; 768 return USBD_NORMAL_COMPLETION;
775} 769}
776#endif 770#endif
777 771
778#ifdef _MODULE 772#ifdef _MODULE
779#include "ioconf.c" 773#include "ioconf.c"
780#endif 774#endif
781 775
782USBNET_MODULE(url) 776USBNET_MODULE(url)

cvs diff -r1.54 -r1.55 src/sys/dev/usb/usbnet.c (switch to unified diff)

--- src/sys/dev/usb/usbnet.c 2022/03/03 05:47:58 1.54
+++ src/sys/dev/usb/usbnet.c 2022/03/03 05:48:14 1.55
@@ -1,1700 +1,1694 @@ @@ -1,1700 +1,1694 @@
1/* $NetBSD: usbnet.c,v 1.54 2022/03/03 05:47:58 riastradh Exp $ */ 1/* $NetBSD: usbnet.c,v 1.55 2022/03/03 05:48:14 riastradh Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2019 Matthew R. Green 4 * Copyright (c) 2019 Matthew R. Green
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE. 26 * SUCH DAMAGE.
27 */ 27 */
28 28
29/* 29/*
30 * Common code shared between USB network drivers. 30 * Common code shared between USB network drivers.
31 */ 31 */
32 32
33#include <sys/cdefs.h> 33#include <sys/cdefs.h>
34__KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.54 2022/03/03 05:47:58 riastradh Exp $"); 34__KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.55 2022/03/03 05:48:14 riastradh Exp $");
35 35
36#include <sys/param.h> 36#include <sys/param.h>
37#include <sys/kernel.h> 37#include <sys/kernel.h>
38#include <sys/kmem.h> 38#include <sys/kmem.h>
39#include <sys/module.h> 39#include <sys/module.h>
40#include <sys/atomic.h> 40#include <sys/atomic.h>
41 41
42#include <dev/usb/usbnet.h> 42#include <dev/usb/usbnet.h>
43#include <dev/usb/usbhist.h> 43#include <dev/usb/usbhist.h>
44 44
45struct usbnet_cdata { 45struct usbnet_cdata {
46 struct usbnet_chain *uncd_tx_chain; 46 struct usbnet_chain *uncd_tx_chain;
47 struct usbnet_chain *uncd_rx_chain; 47 struct usbnet_chain *uncd_rx_chain;
48 48
49 int uncd_tx_prod; 49 int uncd_tx_prod;
50 int uncd_tx_cnt; 50 int uncd_tx_cnt;
51}; 51};
52 52
53struct usbnet_private { 53struct usbnet_private {
54 /* 54 /*
55 * - unp_core_lock protects most of this structure, the public one, 55 * - unp_core_lock protects most of this structure, the public one,
56 * and the MII / media data. 56 * and the MII / media data.
57 * - unp_rxlock protects the rx path and its data 57 * - unp_rxlock protects the rx path and its data
58 * - unp_txlock protects the tx path and its data 58 * - unp_txlock protects the tx path and its data
59 * - unp_detachcv handles detach vs open references 59 * - unp_detachcv handles detach vs open references
60 * 60 *
61 * the lock ordering is: 61 * the lock ordering is:
62 * ifnet lock -> unp_core_lock -> unp_rxlock -> unp_txlock 62 * ifnet lock -> unp_core_lock -> unp_rxlock -> unp_txlock
63 * - ifnet lock is not needed for unp_core_lock, but if ifnet lock is 63 * - ifnet lock is not needed for unp_core_lock, but if ifnet lock is
64 * involved, it must be taken first 64 * involved, it must be taken first
65 */ 65 */
66 kmutex_t unp_core_lock; 66 kmutex_t unp_core_lock;
67 kmutex_t unp_rxlock; 67 kmutex_t unp_rxlock;
68 kmutex_t unp_txlock; 68 kmutex_t unp_txlock;
69 kcondvar_t unp_detachcv; 69 kcondvar_t unp_detachcv;
70 70
71 struct usbnet_cdata unp_cdata; 71 struct usbnet_cdata unp_cdata;
72 72
73 struct ethercom unp_ec; 73 struct ethercom unp_ec;
74 struct mii_data unp_mii; 74 struct mii_data unp_mii;
75 struct usb_task unp_mcasttask; 75 struct usb_task unp_mcasttask;
76 struct usb_task unp_ticktask; 76 struct usb_task unp_ticktask;
77 struct callout unp_stat_ch; 77 struct callout unp_stat_ch;
78 struct usbd_pipe *unp_ep[USBNET_ENDPT_MAX]; 78 struct usbd_pipe *unp_ep[USBNET_ENDPT_MAX];
79 79
80 bool unp_dying; 80 bool unp_dying;
81 bool unp_stopping; 81 bool unp_stopping;
82 bool unp_attached; 82 bool unp_attached;
83 bool unp_ifp_attached; 83 bool unp_ifp_attached;
84 bool unp_link; 84 bool unp_link;
85 85
86 int unp_refcnt; 86 int unp_refcnt;
87 int unp_timer; 87 int unp_timer;
88 unsigned short unp_if_flags; 88 unsigned short unp_if_flags;
89 unsigned unp_number; 89 unsigned unp_number;
90 90
91 krndsource_t unp_rndsrc; 91 krndsource_t unp_rndsrc;
92 92
93 struct timeval unp_rx_notice; 93 struct timeval unp_rx_notice;
94 struct timeval unp_tx_notice; 94 struct timeval unp_tx_notice;
95 struct timeval unp_intr_notice; 95 struct timeval unp_intr_notice;
96}; 96};
97 97
98#define un_cdata(un) (&(un)->un_pri->unp_cdata) 98#define un_cdata(un) (&(un)->un_pri->unp_cdata)
99 99
100volatile unsigned usbnet_number; 100volatile unsigned usbnet_number;
101 101
102static int usbnet_modcmd(modcmd_t, void *); 102static int usbnet_modcmd(modcmd_t, void *);
103 103
104#ifdef USB_DEBUG 104#ifdef USB_DEBUG
105#ifndef USBNET_DEBUG 105#ifndef USBNET_DEBUG
106#define usbnetdebug 0 106#define usbnetdebug 0
107#else 107#else
108static int usbnetdebug = 0; 108static int usbnetdebug = 0;
109 109
110SYSCTL_SETUP(sysctl_hw_usbnet_setup, "sysctl hw.usbnet setup") 110SYSCTL_SETUP(sysctl_hw_usbnet_setup, "sysctl hw.usbnet setup")
111{ 111{
112 int err; 112 int err;
113 const struct sysctlnode *rnode; 113 const struct sysctlnode *rnode;
114 const struct sysctlnode *cnode; 114 const struct sysctlnode *cnode;
115 115
116 err = sysctl_createv(clog, 0, NULL, &rnode, 116 err = sysctl_createv(clog, 0, NULL, &rnode,
117 CTLFLAG_PERMANENT, CTLTYPE_NODE, "usbnet", 117 CTLFLAG_PERMANENT, CTLTYPE_NODE, "usbnet",
118 SYSCTL_DESCR("usbnet global controls"), 118 SYSCTL_DESCR("usbnet global controls"),
119 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL); 119 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
120 120
121 if (err) 121 if (err)
122 goto fail; 122 goto fail;
123 123
124 /* control debugging printfs */ 124 /* control debugging printfs */
125 err = sysctl_createv(clog, 0, &rnode, &cnode, 125 err = sysctl_createv(clog, 0, &rnode, &cnode,
126 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT, 126 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
127 "debug", SYSCTL_DESCR("Enable debugging output"), 127 "debug", SYSCTL_DESCR("Enable debugging output"),
128 NULL, 0, &usbnetdebug, sizeof(usbnetdebug), CTL_CREATE, CTL_EOL); 128 NULL, 0, &usbnetdebug, sizeof(usbnetdebug), CTL_CREATE, CTL_EOL);
129 if (err) 129 if (err)
130 goto fail; 130 goto fail;
131 131
132 return; 132 return;
133fail: 133fail:
134 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err); 134 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
135} 135}
136 136
137#endif /* USBNET_DEBUG */ 137#endif /* USBNET_DEBUG */
138#endif /* USB_DEBUG */ 138#endif /* USB_DEBUG */
139 139
140#define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(usbnetdebug,1,FMT,A,B,C,D) 140#define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(usbnetdebug,1,FMT,A,B,C,D)
141#define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(usbnetdebug,N,FMT,A,B,C,D) 141#define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(usbnetdebug,N,FMT,A,B,C,D)
142#define USBNETHIST_FUNC() USBHIST_FUNC() 142#define USBNETHIST_FUNC() USBHIST_FUNC()
143#define USBNETHIST_CALLED(name) USBHIST_CALLED(usbnetdebug) 143#define USBNETHIST_CALLED(name) USBHIST_CALLED(usbnetdebug)
144#define USBNETHIST_CALLARGS(FMT,A,B,C,D) \ 144#define USBNETHIST_CALLARGS(FMT,A,B,C,D) \
145 USBHIST_CALLARGS(usbnetdebug,FMT,A,B,C,D) 145 USBHIST_CALLARGS(usbnetdebug,FMT,A,B,C,D)
146#define USBNETHIST_CALLARGSN(N,FMT,A,B,C,D) \ 146#define USBNETHIST_CALLARGSN(N,FMT,A,B,C,D) \
147 USBHIST_CALLARGSN(usbnetdebug,N,FMT,A,B,C,D) 147 USBHIST_CALLARGSN(usbnetdebug,N,FMT,A,B,C,D)
148 148
149/* Callback vectors. */ 149/* Callback vectors. */
150 150
151static void 151static void
152uno_stop(struct usbnet *un, struct ifnet *ifp, int disable) 152uno_stop(struct usbnet *un, struct ifnet *ifp, int disable)
153{ 153{
154 KASSERTMSG(!un->un_pri->unp_ifp_attached || IFNET_LOCKED(ifp), 154 KASSERTMSG(!un->un_pri->unp_ifp_attached || IFNET_LOCKED(ifp),
155 "%s", ifp->if_xname); 155 "%s", ifp->if_xname);
156 usbnet_isowned_core(un); 156 usbnet_isowned_core(un);
157 if (un->un_ops->uno_stop) 157 if (un->un_ops->uno_stop)
158 (*un->un_ops->uno_stop)(ifp, disable); 158 (*un->un_ops->uno_stop)(ifp, disable);
159} 159}
160 160
161static int 161static int
162uno_ioctl(struct usbnet *un, struct ifnet *ifp, u_long cmd, void *data) 162uno_ioctl(struct usbnet *un, struct ifnet *ifp, u_long cmd, void *data)
163{ 163{
164 /* 164 /*
165 * There are cases where IFNET_LOCK will not be held when we 165 * There are cases where IFNET_LOCK will not be held when we
166 * are called (e.g. add/delete multicast address), so we can't 166 * are called (e.g. add/delete multicast address), so we can't
167 * assert it. 167 * assert it.
168 */ 168 */
169 if (un->un_ops->uno_ioctl) 169 if (un->un_ops->uno_ioctl)
170 return (*un->un_ops->uno_ioctl)(ifp, cmd, data); 170 return (*un->un_ops->uno_ioctl)(ifp, cmd, data);
171 return 0; 171 return 0;
172} 172}
173 173
174static int 174static int
175uno_override_ioctl(struct usbnet *un, struct ifnet *ifp, u_long cmd, void *data) 175uno_override_ioctl(struct usbnet *un, struct ifnet *ifp, u_long cmd, void *data)
176{ 176{
177 /* See above. */ 177 /* See above. */
178 return (*un->un_ops->uno_override_ioctl)(ifp, cmd, data); 178 return (*un->un_ops->uno_override_ioctl)(ifp, cmd, data);
179} 179}
180 180
181static int 181static int
182uno_init(struct usbnet *un, struct ifnet *ifp) 182uno_init(struct usbnet *un, struct ifnet *ifp)
183{ 183{
184 KASSERT(IFNET_LOCKED(ifp)); 184 KASSERT(IFNET_LOCKED(ifp));
185 return (*un->un_ops->uno_init)(ifp); 185 return (*un->un_ops->uno_init)(ifp);
186} 186}
187 187
188static int 188static int
189uno_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val) 189uno_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val)
190{ 190{
191 usbnet_isowned_core(un); 191 usbnet_isowned_core(un);
192 return (*un->un_ops->uno_read_reg)(un, phy, reg, val); 192 return (*un->un_ops->uno_read_reg)(un, phy, reg, val);
193} 193}
194 194
195static int 195static int
196uno_write_reg(struct usbnet *un, int phy, int reg, uint16_t val) 196uno_write_reg(struct usbnet *un, int phy, int reg, uint16_t val)
197{ 197{
198 usbnet_isowned_core(un); 198 usbnet_isowned_core(un);
199 return (*un->un_ops->uno_write_reg)(un, phy, reg, val); 199 return (*un->un_ops->uno_write_reg)(un, phy, reg, val);
200} 200}
201 201
202static void 202static void
203uno_mii_statchg(struct usbnet *un, struct ifnet *ifp) 203uno_mii_statchg(struct usbnet *un, struct ifnet *ifp)
204{ 204{
205 usbnet_isowned_core(un); 205 usbnet_isowned_core(un);
206 (*un->un_ops->uno_statchg)(ifp); 206 (*un->un_ops->uno_statchg)(ifp);
207} 207}
208 208
209static unsigned 209static unsigned
210uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) 210uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
211{ 211{
212 usbnet_isowned_tx(un); 212 usbnet_isowned_tx(un);
213 return (*un->un_ops->uno_tx_prepare)(un, m, c); 213 return (*un->un_ops->uno_tx_prepare)(un, m, c);
214} 214}
215 215
216static void 216static void
217uno_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len) 217uno_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len)
218{ 218{
219 usbnet_isowned_rx(un); 219 usbnet_isowned_rx(un);
220 (*un->un_ops->uno_rx_loop)(un, c, total_len); 220 (*un->un_ops->uno_rx_loop)(un, c, total_len);
221} 221}
222 222
223static void 223static void
224uno_tick(struct usbnet *un) 224uno_tick(struct usbnet *un)
225{ 225{
226 if (un->un_ops->uno_tick) 226 if (un->un_ops->uno_tick)
227 (*un->un_ops->uno_tick)(un); 227 (*un->un_ops->uno_tick)(un);
228} 228}
229 229
230static void 230static void
231uno_intr(struct usbnet *un, usbd_status status) 231uno_intr(struct usbnet *un, usbd_status status)
232{ 232{
233 if (un->un_ops->uno_intr) 233 if (un->un_ops->uno_intr)
234 (*un->un_ops->uno_intr)(un, status); 234 (*un->un_ops->uno_intr)(un, status);
235} 235}
236 236
237/* Interrupt handling. */ 237/* Interrupt handling. */
238 238
239static struct mbuf * 239static struct mbuf *
240usbnet_newbuf(size_t buflen) 240usbnet_newbuf(size_t buflen)
241{ 241{
242 struct mbuf *m; 242 struct mbuf *m;
243 243
244 if (buflen > MCLBYTES) 244 if (buflen > MCLBYTES)
245 return NULL; 245 return NULL;
246 246
247 MGETHDR(m, M_DONTWAIT, MT_DATA); 247 MGETHDR(m, M_DONTWAIT, MT_DATA);
248 if (m == NULL) 248 if (m == NULL)
249 return NULL; 249 return NULL;
250 250
251 if (buflen > MHLEN - ETHER_ALIGN) { 251 if (buflen > MHLEN - ETHER_ALIGN) {
252 MCLGET(m, M_DONTWAIT); 252 MCLGET(m, M_DONTWAIT);
253 if (!(m->m_flags & M_EXT)) { 253 if (!(m->m_flags & M_EXT)) {
254 m_freem(m); 254 m_freem(m);
255 return NULL; 255 return NULL;
256 } 256 }
257 } 257 }
258 258
259 m_adj(m, ETHER_ALIGN); 259 m_adj(m, ETHER_ALIGN);
260 m->m_len = m->m_pkthdr.len = buflen; 260 m->m_len = m->m_pkthdr.len = buflen;
261 261
262 return m; 262 return m;
263} 263}
264 264
265/* 265/*
266 * usbnet_rxeof() is designed to be the done callback for rx completion. 266 * usbnet_rxeof() is designed to be the done callback for rx completion.
267 * it provides generic setup and finalisation, calls a different usbnet 267 * it provides generic setup and finalisation, calls a different usbnet
268 * rx_loop callback in the middle, which can use usbnet_enqueue() to 268 * rx_loop callback in the middle, which can use usbnet_enqueue() to
269 * enqueue a packet for higher levels (or usbnet_input() if previously 269 * enqueue a packet for higher levels (or usbnet_input() if previously
270 * using if_input() path.) 270 * using if_input() path.)
271 */ 271 */
272void 272void
273usbnet_enqueue(struct usbnet * const un, uint8_t *buf, size_t buflen, 273usbnet_enqueue(struct usbnet * const un, uint8_t *buf, size_t buflen,
274 int csum_flags, uint32_t csum_data, int mbuf_flags) 274 int csum_flags, uint32_t csum_data, int mbuf_flags)
275{ 275{
276 USBNETHIST_FUNC(); 276 USBNETHIST_FUNC();
277 struct ifnet * const ifp = usbnet_ifp(un); 277 struct ifnet * const ifp = usbnet_ifp(un);
278 struct usbnet_private * const unp __unused = un->un_pri; 278 struct usbnet_private * const unp __unused = un->un_pri;
279 struct mbuf *m; 279 struct mbuf *m;
280 280
281 USBNETHIST_CALLARGSN(5, "%jd: enter: len=%ju csf %#jx mbf %#jx", 281 USBNETHIST_CALLARGSN(5, "%jd: enter: len=%ju csf %#jx mbf %#jx",
282 unp->unp_number, buflen, csum_flags, mbuf_flags); 282 unp->unp_number, buflen, csum_flags, mbuf_flags);
283 283
284 usbnet_isowned_rx(un); 284 usbnet_isowned_rx(un);
285 285
286 m = usbnet_newbuf(buflen); 286 m = usbnet_newbuf(buflen);
287 if (m == NULL) { 287 if (m == NULL) {
288 DPRINTF("%jd: no memory", unp->unp_number, 0, 0, 0); 288 DPRINTF("%jd: no memory", unp->unp_number, 0, 0, 0);
289 if_statinc(ifp, if_ierrors); 289 if_statinc(ifp, if_ierrors);
290 return; 290 return;
291 } 291 }
292 292
293 m_set_rcvif(m, ifp); 293 m_set_rcvif(m, ifp);
294 m->m_pkthdr.csum_flags = csum_flags; 294 m->m_pkthdr.csum_flags = csum_flags;
295 m->m_pkthdr.csum_data = csum_data; 295 m->m_pkthdr.csum_data = csum_data;
296 m->m_flags |= mbuf_flags; 296 m->m_flags |= mbuf_flags;
297 memcpy(mtod(m, uint8_t *), buf, buflen); 297 memcpy(mtod(m, uint8_t *), buf, buflen);
298 298
299 /* push the packet up */ 299 /* push the packet up */
300 if_percpuq_enqueue(ifp->if_percpuq, m); 300 if_percpuq_enqueue(ifp->if_percpuq, m);
301} 301}
302 302
303void 303void
304usbnet_input(struct usbnet * const un, uint8_t *buf, size_t buflen) 304usbnet_input(struct usbnet * const un, uint8_t *buf, size_t buflen)
305{ 305{
306 USBNETHIST_FUNC(); 306 USBNETHIST_FUNC();
307 struct ifnet * const ifp = usbnet_ifp(un); 307 struct ifnet * const ifp = usbnet_ifp(un);
308 struct usbnet_private * const unp __unused = un->un_pri; 308 struct usbnet_private * const unp __unused = un->un_pri;
309 struct mbuf *m; 309 struct mbuf *m;
310 310
311 USBNETHIST_CALLARGSN(5, "%jd: enter: buf %#jx len %ju", 311 USBNETHIST_CALLARGSN(5, "%jd: enter: buf %#jx len %ju",
312 unp->unp_number, (uintptr_t)buf, buflen, 0); 312 unp->unp_number, (uintptr_t)buf, buflen, 0);
313 313
314 usbnet_isowned_rx(un); 314 usbnet_isowned_rx(un);
315 315
316 m = usbnet_newbuf(buflen); 316 m = usbnet_newbuf(buflen);
317 if (m == NULL) { 317 if (m == NULL) {
318 if_statinc(ifp, if_ierrors); 318 if_statinc(ifp, if_ierrors);
319 return; 319 return;
320 } 320 }
321 321
322 m_set_rcvif(m, ifp); 322 m_set_rcvif(m, ifp);
323 memcpy(mtod(m, char *), buf, buflen); 323 memcpy(mtod(m, char *), buf, buflen);
324 324
325 /* push the packet up */ 325 /* push the packet up */
326 if_input(ifp, m); 326 if_input(ifp, m);
327} 327}
328 328
329/* 329/*
330 * A frame has been uploaded: pass the resulting mbuf chain up to 330 * A frame has been uploaded: pass the resulting mbuf chain up to
331 * the higher level protocols. 331 * the higher level protocols.
332 */ 332 */
333static void 333static void
334usbnet_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 334usbnet_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
335{ 335{
336 USBNETHIST_FUNC(); 336 USBNETHIST_FUNC();
337 struct usbnet_chain * const c = priv; 337 struct usbnet_chain * const c = priv;
338 struct usbnet * const un = c->unc_un; 338 struct usbnet * const un = c->unc_un;
339 struct usbnet_private * const unp = un->un_pri; 339 struct usbnet_private * const unp = un->un_pri;
340 uint32_t total_len; 340 uint32_t total_len;
341 341
342 USBNETHIST_CALLARGSN(5, "%jd: enter: status %#jx xfer %#jx", 342 USBNETHIST_CALLARGSN(5, "%jd: enter: status %#jx xfer %#jx",
343 unp->unp_number, status, (uintptr_t)xfer, 0); 343 unp->unp_number, status, (uintptr_t)xfer, 0);
344 344
345 mutex_enter(&unp->unp_rxlock); 345 mutex_enter(&unp->unp_rxlock);
346 346
347 if (unp->unp_dying || unp->unp_stopping || 347 if (unp->unp_dying || unp->unp_stopping ||
348 status == USBD_INVAL || status == USBD_NOT_STARTED || 348 status == USBD_INVAL || status == USBD_NOT_STARTED ||
349 status == USBD_CANCELLED) 349 status == USBD_CANCELLED)
350 goto out; 350 goto out;
351 351
352 if (status != USBD_NORMAL_COMPLETION) { 352 if (status != USBD_NORMAL_COMPLETION) {
353 if (usbd_ratecheck(&unp->unp_rx_notice)) 353 if (usbd_ratecheck(&unp->unp_rx_notice))
354 device_printf(un->un_dev, "usb errors on rx: %s\n", 354 device_printf(un->un_dev, "usb errors on rx: %s\n",
355 usbd_errstr(status)); 355 usbd_errstr(status));
356 if (status == USBD_STALLED) 356 if (status == USBD_STALLED)
357 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_RX]); 357 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_RX]);
358 goto done; 358 goto done;
359 } 359 }
360 360
361 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 361 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
362 362
363 if (total_len > un->un_rx_bufsz) { 363 if (total_len > un->un_rx_bufsz) {
364 aprint_error_dev(un->un_dev, 364 aprint_error_dev(un->un_dev,
365 "rxeof: too large transfer (%u > %u)\n", 365 "rxeof: too large transfer (%u > %u)\n",
366 total_len, un->un_rx_bufsz); 366 total_len, un->un_rx_bufsz);
367 goto done; 367 goto done;
368 } 368 }
369 369
370 uno_rx_loop(un, c, total_len); 370 uno_rx_loop(un, c, total_len);
371 usbnet_isowned_rx(un); 371 usbnet_isowned_rx(un);
372 372
373done: 373done:
374 if (unp->unp_dying || unp->unp_stopping) 374 if (unp->unp_dying || unp->unp_stopping)
375 goto out; 375 goto out;
376 376
377 mutex_exit(&unp->unp_rxlock); 377 mutex_exit(&unp->unp_rxlock);
378 378
379 /* Setup new transfer. */ 379 /* Setup new transfer. */
380 usbd_setup_xfer(xfer, c, c->unc_buf, un->un_rx_bufsz, 380 usbd_setup_xfer(xfer, c, c->unc_buf, un->un_rx_bufsz,
381 un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof); 381 un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof);
382 usbd_transfer(xfer); 382 usbd_transfer(xfer);
383 return; 383 return;
384 384
385out: 385out:
386 mutex_exit(&unp->unp_rxlock); 386 mutex_exit(&unp->unp_rxlock);
387} 387}
388 388
389static void 389static void
390usbnet_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 390usbnet_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
391{ 391{
392 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 392 USBNETHIST_FUNC(); USBNETHIST_CALLED();
393 struct usbnet_chain * const c = priv; 393 struct usbnet_chain * const c = priv;
394 struct usbnet * const un = c->unc_un; 394 struct usbnet * const un = c->unc_un;
395 struct usbnet_cdata * const cd = un_cdata(un); 395 struct usbnet_cdata * const cd = un_cdata(un);
396 struct usbnet_private * const unp = un->un_pri; 396 struct usbnet_private * const unp = un->un_pri;
397 struct ifnet * const ifp = usbnet_ifp(un); 397 struct ifnet * const ifp = usbnet_ifp(un);
398 398
399 USBNETHIST_CALLARGSN(5, "%jd: enter: status %#jx xfer %#jx", 399 USBNETHIST_CALLARGSN(5, "%jd: enter: status %#jx xfer %#jx",
400 unp->unp_number, status, (uintptr_t)xfer, 0); 400 unp->unp_number, status, (uintptr_t)xfer, 0);
401 401
402 mutex_enter(&unp->unp_txlock); 402 mutex_enter(&unp->unp_txlock);
403 if (unp->unp_stopping || unp->unp_dying) { 403 if (unp->unp_stopping || unp->unp_dying) {
404 mutex_exit(&unp->unp_txlock); 404 mutex_exit(&unp->unp_txlock);
405 return; 405 return;
406 } 406 }
407 407
408 KASSERT(cd->uncd_tx_cnt > 0); 408 KASSERT(cd->uncd_tx_cnt > 0);
409 cd->uncd_tx_cnt--; 409 cd->uncd_tx_cnt--;
410 410
411 unp->unp_timer = 0; 411 unp->unp_timer = 0;
412 412
413 switch (status) { 413 switch (status) {
414 case USBD_NOT_STARTED: 414 case USBD_NOT_STARTED:
415 case USBD_CANCELLED: 415 case USBD_CANCELLED:
416 break; 416 break;
417 417
418 case USBD_NORMAL_COMPLETION: 418 case USBD_NORMAL_COMPLETION:
419 if_statinc(ifp, if_opackets); 419 if_statinc(ifp, if_opackets);
420 break; 420 break;
421 421
422 default: 422 default:
423 423
424 if_statinc(ifp, if_oerrors); 424 if_statinc(ifp, if_oerrors);
425 if (usbd_ratecheck(&unp->unp_tx_notice)) 425 if (usbd_ratecheck(&unp->unp_tx_notice))
426 device_printf(un->un_dev, "usb error on tx: %s\n", 426 device_printf(un->un_dev, "usb error on tx: %s\n",
427 usbd_errstr(status)); 427 usbd_errstr(status));
428 if (status == USBD_STALLED) 428 if (status == USBD_STALLED)
429 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_TX]); 429 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_TX]);
430 break; 430 break;
431 } 431 }
432 432
433 mutex_exit(&unp->unp_txlock); 433 mutex_exit(&unp->unp_txlock);
434 434
435 if (status == USBD_NORMAL_COMPLETION && !IFQ_IS_EMPTY(&ifp->if_snd)) 435 if (status == USBD_NORMAL_COMPLETION && !IFQ_IS_EMPTY(&ifp->if_snd))
436 (*ifp->if_start)(ifp); 436 (*ifp->if_start)(ifp);
437} 437}
438 438
439static void 439static void
440usbnet_pipe_intr(struct usbd_xfer *xfer, void *priv, usbd_status status) 440usbnet_pipe_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
441{ 441{
442 USBNETHIST_FUNC(); 442 USBNETHIST_FUNC();
443 struct usbnet * const un = priv; 443 struct usbnet * const un = priv;
444 struct usbnet_private * const unp = un->un_pri; 444 struct usbnet_private * const unp = un->un_pri;
445 struct usbnet_intr * const uni = un->un_intr; 445 struct usbnet_intr * const uni = un->un_intr;
446 446
447 if (uni == NULL || unp->unp_dying || unp->unp_stopping || 447 if (uni == NULL || unp->unp_dying || unp->unp_stopping ||
448 status == USBD_INVAL || status == USBD_NOT_STARTED || 448 status == USBD_INVAL || status == USBD_NOT_STARTED ||
449 status == USBD_CANCELLED) { 449 status == USBD_CANCELLED) {
450 USBNETHIST_CALLARGS("%jd: uni %#jx d/s %#jx status %#jx", 450 USBNETHIST_CALLARGS("%jd: uni %#jx d/s %#jx status %#jx",
451 unp->unp_number, (uintptr_t)uni, 451 unp->unp_number, (uintptr_t)uni,
452 (unp->unp_dying << 8) | unp->unp_stopping, status); 452 (unp->unp_dying << 8) | unp->unp_stopping, status);
453 return; 453 return;
454 } 454 }
455 455
456 if (status != USBD_NORMAL_COMPLETION) { 456 if (status != USBD_NORMAL_COMPLETION) {
457 if (usbd_ratecheck(&unp->unp_intr_notice)) { 457 if (usbd_ratecheck(&unp->unp_intr_notice)) {
458 aprint_error_dev(un->un_dev, "usb error on intr: %s\n", 458 aprint_error_dev(un->un_dev, "usb error on intr: %s\n",
459 usbd_errstr(status)); 459 usbd_errstr(status));
460 } 460 }
461 if (status == USBD_STALLED) 461 if (status == USBD_STALLED)
462 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_INTR]); 462 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_INTR]);
463 USBNETHIST_CALLARGS("%jd: not normal status %#jx", 463 USBNETHIST_CALLARGS("%jd: not normal status %#jx",
464 unp->unp_number, status, 0, 0); 464 unp->unp_number, status, 0, 0);
465 return; 465 return;
466 } 466 }
467 467
468 uno_intr(un, status); 468 uno_intr(un, status);
469} 469}
470 470
471static void 471static void
472usbnet_start_locked(struct ifnet *ifp) 472usbnet_start_locked(struct ifnet *ifp)
473{ 473{
474 USBNETHIST_FUNC(); 474 USBNETHIST_FUNC();
475 struct usbnet * const un = ifp->if_softc; 475 struct usbnet * const un = ifp->if_softc;
476 struct usbnet_cdata * const cd = un_cdata(un); 476 struct usbnet_cdata * const cd = un_cdata(un);
477 struct usbnet_private * const unp = un->un_pri; 477 struct usbnet_private * const unp = un->un_pri;
478 struct mbuf *m; 478 struct mbuf *m;
479 unsigned length; 479 unsigned length;
480 bool done_transmit = false; 480 bool done_transmit = false;
481 int idx, count; 481 int idx, count;
482 482
483 USBNETHIST_CALLARGS("%jd: tx_cnt %jd list_cnt %jd link %jd", 483 USBNETHIST_CALLARGS("%jd: tx_cnt %jd list_cnt %jd link %jd",
484 unp->unp_number, cd->uncd_tx_cnt, un->un_tx_list_cnt, 484 unp->unp_number, cd->uncd_tx_cnt, un->un_tx_list_cnt,
485 unp->unp_link); 485 unp->unp_link);
486 486
487 usbnet_isowned_tx(un); 487 usbnet_isowned_tx(un);
488 KASSERT(cd->uncd_tx_cnt <= un->un_tx_list_cnt); 488 KASSERT(cd->uncd_tx_cnt <= un->un_tx_list_cnt);
489 489
490 if (!unp->unp_link || (ifp->if_flags & IFF_RUNNING) == 0) { 490 if (!unp->unp_link || (ifp->if_flags & IFF_RUNNING) == 0) {
491 DPRINTF("start called no link (%jx) or running (flags %jx)", 491 DPRINTF("start called no link (%jx) or running (flags %jx)",
492 unp->unp_link, ifp->if_flags, 0, 0); 492 unp->unp_link, ifp->if_flags, 0, 0);
493 return; 493 return;
494 } 494 }
495 495
496 if (cd->uncd_tx_cnt == un->un_tx_list_cnt) { 496 if (cd->uncd_tx_cnt == un->un_tx_list_cnt) {
497 DPRINTF("start called, tx busy (%#jx == %#jx)", 497 DPRINTF("start called, tx busy (%#jx == %#jx)",
498 cd->uncd_tx_cnt, un->un_tx_list_cnt, 0, 0); 498 cd->uncd_tx_cnt, un->un_tx_list_cnt, 0, 0);
499 return; 499 return;
500 } 500 }
501 501
502 idx = cd->uncd_tx_prod; 502 idx = cd->uncd_tx_prod;
503 count = 0; 503 count = 0;
504 while (cd->uncd_tx_cnt < un->un_tx_list_cnt) { 504 while (cd->uncd_tx_cnt < un->un_tx_list_cnt) {
505 IFQ_POLL(&ifp->if_snd, m); 505 IFQ_POLL(&ifp->if_snd, m);
506 if (m == NULL) { 506 if (m == NULL) {
507 DPRINTF("start called, queue empty", 0, 0, 0, 0); 507 DPRINTF("start called, queue empty", 0, 0, 0, 0);
508 break; 508 break;
509 } 509 }
510 KASSERT(m->m_pkthdr.len <= un->un_tx_bufsz); 510 KASSERT(m->m_pkthdr.len <= un->un_tx_bufsz);
511 511
512 struct usbnet_chain *c = &cd->uncd_tx_chain[idx]; 512 struct usbnet_chain *c = &cd->uncd_tx_chain[idx];
513 513
514 length = uno_tx_prepare(un, m, c); 514 length = uno_tx_prepare(un, m, c);
515 if (length == 0) { 515 if (length == 0) {
516 DPRINTF("uno_tx_prepare gave zero length", 0, 0, 0, 0); 516 DPRINTF("uno_tx_prepare gave zero length", 0, 0, 0, 0);
517 if_statinc(ifp, if_oerrors); 517 if_statinc(ifp, if_oerrors);
518 break; 518 break;
519 } 519 }
520 520
521 if (__predict_false(c->unc_xfer == NULL)) { 521 if (__predict_false(c->unc_xfer == NULL)) {
522 DPRINTF("unc_xfer is NULL", 0, 0, 0, 0); 522 DPRINTF("unc_xfer is NULL", 0, 0, 0, 0);
523 if_statinc(ifp, if_oerrors); 523 if_statinc(ifp, if_oerrors);
524 break; 524 break;
525 } 525 }
526 526
527 usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, length, 527 usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, length,
528 un->un_tx_xfer_flags, 10000, usbnet_txeof); 528 un->un_tx_xfer_flags, 10000, usbnet_txeof);
529 529
530 /* Transmit */ 530 /* Transmit */
531 usbd_status err = usbd_transfer(c->unc_xfer); 531 usbd_status err = usbd_transfer(c->unc_xfer);
532 if (err != USBD_IN_PROGRESS) { 532 if (err != USBD_IN_PROGRESS) {
533 DPRINTF("usbd_transfer on %#jx for %ju bytes: %jd", 533 DPRINTF("usbd_transfer on %#jx for %ju bytes: %jd",
534 (uintptr_t)c->unc_buf, length, err, 0); 534 (uintptr_t)c->unc_buf, length, err, 0);
535 if_statinc(ifp, if_oerrors); 535 if_statinc(ifp, if_oerrors);
536 break; 536 break;
537 } 537 }
538 done_transmit = true; 538 done_transmit = true;
539 539
540 IFQ_DEQUEUE(&ifp->if_snd, m); 540 IFQ_DEQUEUE(&ifp->if_snd, m);
541 541
542 /* 542 /*
543 * If there's a BPF listener, bounce a copy of this frame 543 * If there's a BPF listener, bounce a copy of this frame
544 * to him. 544 * to him.
545 */ 545 */
546 bpf_mtap(ifp, m, BPF_D_OUT); 546 bpf_mtap(ifp, m, BPF_D_OUT);
547 m_freem(m); 547 m_freem(m);
548 548
549 idx = (idx + 1) % un->un_tx_list_cnt; 549 idx = (idx + 1) % un->un_tx_list_cnt;
550 cd->uncd_tx_cnt++; 550 cd->uncd_tx_cnt++;
551 count++; 551 count++;
552 } 552 }
553 cd->uncd_tx_prod = idx; 553 cd->uncd_tx_prod = idx;
554 554
555 DPRINTF("finished with start; tx_cnt %jd list_cnt %jd link %jd", 555 DPRINTF("finished with start; tx_cnt %jd list_cnt %jd link %jd",
556 cd->uncd_tx_cnt, un->un_tx_list_cnt, unp->unp_link, 0); 556 cd->uncd_tx_cnt, un->un_tx_list_cnt, unp->unp_link, 0);
557 557
558 /* 558 /*
559 * Set a timeout in case the chip goes out to lunch. 559 * Set a timeout in case the chip goes out to lunch.
560 */ 560 */
561 if (done_transmit) 561 if (done_transmit)
562 unp->unp_timer = 5; 562 unp->unp_timer = 5;
563 563
564 if (count != 0) 564 if (count != 0)
565 rnd_add_uint32(&unp->unp_rndsrc, count); 565 rnd_add_uint32(&unp->unp_rndsrc, count);
566} 566}
567 567
568static void 568static void
569usbnet_if_start(struct ifnet *ifp) 569usbnet_if_start(struct ifnet *ifp)
570{ 570{
571 struct usbnet * const un = ifp->if_softc; 571 struct usbnet * const un = ifp->if_softc;
572 struct usbnet_private * const unp = un->un_pri; 572 struct usbnet_private * const unp = un->un_pri;
573 573
574 USBNETHIST_FUNC(); 574 USBNETHIST_FUNC();
575 USBNETHIST_CALLARGS("%jd: stopping %jd", 575 USBNETHIST_CALLARGS("%jd: stopping %jd",
576 unp->unp_number, unp->unp_stopping, 0, 0); 576 unp->unp_number, unp->unp_stopping, 0, 0);
577 577
578 mutex_enter(&unp->unp_txlock); 578 mutex_enter(&unp->unp_txlock);
579 if (!unp->unp_stopping) 579 if (!unp->unp_stopping)
580 usbnet_start_locked(ifp); 580 usbnet_start_locked(ifp);
581 mutex_exit(&unp->unp_txlock); 581 mutex_exit(&unp->unp_txlock);
582} 582}
583 583
584/* 584/*
585 * Chain management. 585 * Chain management.
586 * 586 *
587 * RX and TX are identical. Keep them that way. 587 * RX and TX are identical. Keep them that way.
588 */ 588 */
589 589
590/* Start of common RX functions */ 590/* Start of common RX functions */
591 591
592static size_t 592static size_t
593usbnet_rx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un) 593usbnet_rx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un)
594{ 594{
595 return sizeof(*cd->uncd_rx_chain) * un->un_rx_list_cnt; 595 return sizeof(*cd->uncd_rx_chain) * un->un_rx_list_cnt;
596} 596}
597 597
598static void 598static void
599usbnet_rx_list_alloc(struct usbnet * const un) 599usbnet_rx_list_alloc(struct usbnet * const un)
600{ 600{
601 struct usbnet_cdata * const cd = un_cdata(un); 601 struct usbnet_cdata * const cd = un_cdata(un);
602 602
603 cd->uncd_rx_chain = kmem_zalloc(usbnet_rx_list_size(cd, un), KM_SLEEP); 603 cd->uncd_rx_chain = kmem_zalloc(usbnet_rx_list_size(cd, un), KM_SLEEP);
604} 604}
605 605
606static void 606static void
607usbnet_rx_list_free(struct usbnet * const un) 607usbnet_rx_list_free(struct usbnet * const un)
608{ 608{
609 struct usbnet_cdata * const cd = un_cdata(un); 609 struct usbnet_cdata * const cd = un_cdata(un);
610 610
611 if (cd->uncd_rx_chain) { 611 if (cd->uncd_rx_chain) {
612 kmem_free(cd->uncd_rx_chain, usbnet_rx_list_size(cd, un)); 612 kmem_free(cd->uncd_rx_chain, usbnet_rx_list_size(cd, un));
613 cd->uncd_rx_chain = NULL; 613 cd->uncd_rx_chain = NULL;
614 } 614 }
615} 615}
616 616
617static int 617static int
618usbnet_rx_list_init(struct usbnet * const un) 618usbnet_rx_list_init(struct usbnet * const un)
619{ 619{
620 struct usbnet_cdata * const cd = un_cdata(un); 620 struct usbnet_cdata * const cd = un_cdata(un);
621 struct usbnet_private * const unp = un->un_pri; 621 struct usbnet_private * const unp = un->un_pri;
622 622
623 for (size_t i = 0; i < un->un_rx_list_cnt; i++) { 623 for (size_t i = 0; i < un->un_rx_list_cnt; i++) {
624 struct usbnet_chain *c = &cd->uncd_rx_chain[i]; 624 struct usbnet_chain *c = &cd->uncd_rx_chain[i];
625 625
626 c->unc_un = un; 626 c->unc_un = un;
627 if (c->unc_xfer == NULL) { 627 if (c->unc_xfer == NULL) {
628 int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_RX], 628 int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_RX],
629 un->un_rx_bufsz, un->un_rx_xfer_flags, 0, 629 un->un_rx_bufsz, un->un_rx_xfer_flags, 0,
630 &c->unc_xfer); 630 &c->unc_xfer);
631 if (err) 631 if (err)
632 return err; 632 return err;
633 c->unc_buf = usbd_get_buffer(c->unc_xfer); 633 c->unc_buf = usbd_get_buffer(c->unc_xfer);
634 } 634 }
635 } 635 }
636 636
637 return 0; 637 return 0;
638} 638}
639 639
640static void 640static void
641usbnet_rx_list_fini(struct usbnet * const un) 641usbnet_rx_list_fini(struct usbnet * const un)
642{ 642{
643 struct usbnet_cdata * const cd = un_cdata(un); 643 struct usbnet_cdata * const cd = un_cdata(un);
644 644
645 for (size_t i = 0; i < un->un_rx_list_cnt; i++) { 645 for (size_t i = 0; i < un->un_rx_list_cnt; i++) {
646 struct usbnet_chain *c = &cd->uncd_rx_chain[i]; 646 struct usbnet_chain *c = &cd->uncd_rx_chain[i];
647 647
648 if (c->unc_xfer != NULL) { 648 if (c->unc_xfer != NULL) {
649 usbd_destroy_xfer(c->unc_xfer); 649 usbd_destroy_xfer(c->unc_xfer);
650 c->unc_xfer = NULL; 650 c->unc_xfer = NULL;
651 c->unc_buf = NULL; 651 c->unc_buf = NULL;
652 } 652 }
653 } 653 }
654} 654}
655 655
656/* End of common RX functions */ 656/* End of common RX functions */
657 657
658static void 658static void
659usbnet_rx_start_pipes(struct usbnet * const un) 659usbnet_rx_start_pipes(struct usbnet * const un)
660{ 660{
661 struct usbnet_cdata * const cd = un_cdata(un); 661 struct usbnet_cdata * const cd = un_cdata(un);
662 struct usbnet_private * const unp = un->un_pri; 662 struct usbnet_private * const unp = un->un_pri;
663 663
664 mutex_enter(&unp->unp_rxlock); 664 mutex_enter(&unp->unp_rxlock);
665 mutex_enter(&unp->unp_txlock); 665 mutex_enter(&unp->unp_txlock);
666 unp->unp_stopping = false; 666 unp->unp_stopping = false;
667 667
668 for (size_t i = 0; i < un->un_rx_list_cnt; i++) { 668 for (size_t i = 0; i < un->un_rx_list_cnt; i++) {
669 struct usbnet_chain *c = &cd->uncd_rx_chain[i]; 669 struct usbnet_chain *c = &cd->uncd_rx_chain[i];
670 670
671 usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, un->un_rx_bufsz, 671 usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, un->un_rx_bufsz,
672 un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof); 672 un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof);
673 usbd_transfer(c->unc_xfer); 673 usbd_transfer(c->unc_xfer);
674 } 674 }
675 675
676 mutex_exit(&unp->unp_txlock); 676 mutex_exit(&unp->unp_txlock);
677 mutex_exit(&unp->unp_rxlock); 677 mutex_exit(&unp->unp_rxlock);
678} 678}
679 679
680/* Start of common TX functions */ 680/* Start of common TX functions */
681 681
682static size_t 682static size_t
683usbnet_tx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un) 683usbnet_tx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un)
684{ 684{
685 return sizeof(*cd->uncd_tx_chain) * un->un_tx_list_cnt; 685 return sizeof(*cd->uncd_tx_chain) * un->un_tx_list_cnt;
686} 686}
687 687
688static void 688static void
689usbnet_tx_list_alloc(struct usbnet * const un) 689usbnet_tx_list_alloc(struct usbnet * const un)
690{ 690{
691 struct usbnet_cdata * const cd = un_cdata(un); 691 struct usbnet_cdata * const cd = un_cdata(un);
692 692
693 cd->uncd_tx_chain = kmem_zalloc(usbnet_tx_list_size(cd, un), KM_SLEEP); 693 cd->uncd_tx_chain = kmem_zalloc(usbnet_tx_list_size(cd, un), KM_SLEEP);
694} 694}
695 695
696static void 696static void
697usbnet_tx_list_free(struct usbnet * const un) 697usbnet_tx_list_free(struct usbnet * const un)
698{ 698{
699 struct usbnet_cdata * const cd = un_cdata(un); 699 struct usbnet_cdata * const cd = un_cdata(un);
700 700
701 if (cd->uncd_tx_chain) { 701 if (cd->uncd_tx_chain) {
702 kmem_free(cd->uncd_tx_chain, usbnet_tx_list_size(cd, un)); 702 kmem_free(cd->uncd_tx_chain, usbnet_tx_list_size(cd, un));
703 cd->uncd_tx_chain = NULL; 703 cd->uncd_tx_chain = NULL;
704 } 704 }
705} 705}
706 706
707static int 707static int
708usbnet_tx_list_init(struct usbnet * const un) 708usbnet_tx_list_init(struct usbnet * const un)
709{ 709{
710 struct usbnet_cdata * const cd = un_cdata(un); 710 struct usbnet_cdata * const cd = un_cdata(un);
711 struct usbnet_private * const unp = un->un_pri; 711 struct usbnet_private * const unp = un->un_pri;
712 712
713 for (size_t i = 0; i < un->un_tx_list_cnt; i++) { 713 for (size_t i = 0; i < un->un_tx_list_cnt; i++) {
714 struct usbnet_chain *c = &cd->uncd_tx_chain[i]; 714 struct usbnet_chain *c = &cd->uncd_tx_chain[i];
715 715
716 c->unc_un = un; 716 c->unc_un = un;
717 if (c->unc_xfer == NULL) { 717 if (c->unc_xfer == NULL) {
718 int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_TX], 718 int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_TX],
719 un->un_tx_bufsz, un->un_tx_xfer_flags, 0, 719 un->un_tx_bufsz, un->un_tx_xfer_flags, 0,
720 &c->unc_xfer); 720 &c->unc_xfer);
721 if (err) 721 if (err)
722 return err; 722 return err;
723 c->unc_buf = usbd_get_buffer(c->unc_xfer); 723 c->unc_buf = usbd_get_buffer(c->unc_xfer);
724 } 724 }
725 } 725 }
726 726
727 return 0; 727 return 0;
728} 728}
729 729
730static void 730static void
731usbnet_tx_list_fini(struct usbnet * const un) 731usbnet_tx_list_fini(struct usbnet * const un)
732{ 732{
733 struct usbnet_cdata * const cd = un_cdata(un); 733 struct usbnet_cdata * const cd = un_cdata(un);
734 734
735 for (size_t i = 0; i < un->un_tx_list_cnt; i++) { 735 for (size_t i = 0; i < un->un_tx_list_cnt; i++) {
736 struct usbnet_chain *c = &cd->uncd_tx_chain[i]; 736 struct usbnet_chain *c = &cd->uncd_tx_chain[i];
737 737
738 if (c->unc_xfer != NULL) { 738 if (c->unc_xfer != NULL) {
739 usbd_destroy_xfer(c->unc_xfer); 739 usbd_destroy_xfer(c->unc_xfer);
740 c->unc_xfer = NULL; 740 c->unc_xfer = NULL;
741 c->unc_buf = NULL; 741 c->unc_buf = NULL;
742 } 742 }
743 } 743 }
744 cd->uncd_tx_prod = cd->uncd_tx_cnt = 0; 744 cd->uncd_tx_prod = cd->uncd_tx_cnt = 0;
745} 745}
746 746
747/* End of common TX functions */ 747/* End of common TX functions */
748 748
749/* Endpoint pipe management. */ 749/* Endpoint pipe management. */
750 750
751static void 751static void
752usbnet_ep_close_pipes(struct usbnet * const un) 752usbnet_ep_close_pipes(struct usbnet * const un)
753{ 753{
754 struct usbnet_private * const unp = un->un_pri; 754 struct usbnet_private * const unp = un->un_pri;
755 755
756 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) { 756 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) {
757 if (unp->unp_ep[i] == NULL) 757 if (unp->unp_ep[i] == NULL)
758 continue; 758 continue;
759 usbd_status err = usbd_close_pipe(unp->unp_ep[i]); 759 usbd_status err = usbd_close_pipe(unp->unp_ep[i]);
760 if (err) 760 if (err)
761 aprint_error_dev(un->un_dev, "close pipe %zu: %s\n", i, 761 aprint_error_dev(un->un_dev, "close pipe %zu: %s\n", i,
762 usbd_errstr(err)); 762 usbd_errstr(err));
763 unp->unp_ep[i] = NULL; 763 unp->unp_ep[i] = NULL;
764 } 764 }
765} 765}
766 766
767static usbd_status 767static usbd_status
768usbnet_ep_open_pipes(struct usbnet * const un) 768usbnet_ep_open_pipes(struct usbnet * const un)
769{ 769{
770 struct usbnet_intr * const uni = un->un_intr; 770 struct usbnet_intr * const uni = un->un_intr;
771 struct usbnet_private * const unp = un->un_pri; 771 struct usbnet_private * const unp = un->un_pri;
772 772
773 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) { 773 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) {
774 usbd_status err; 774 usbd_status err;
775 775
776 if (un->un_ed[i] == 0) 776 if (un->un_ed[i] == 0)
777 continue; 777 continue;
778 778
779 if (i == USBNET_ENDPT_INTR && uni) { 779 if (i == USBNET_ENDPT_INTR && uni) {
780 err = usbd_open_pipe_intr(un->un_iface, un->un_ed[i], 780 err = usbd_open_pipe_intr(un->un_iface, un->un_ed[i],
781 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i], un, 781 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i], un,
782 uni->uni_buf, uni->uni_bufsz, usbnet_pipe_intr, 782 uni->uni_buf, uni->uni_bufsz, usbnet_pipe_intr,
783 uni->uni_interval); 783 uni->uni_interval);
784 } else { 784 } else {
785 err = usbd_open_pipe(un->un_iface, un->un_ed[i], 785 err = usbd_open_pipe(un->un_iface, un->un_ed[i],
786 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i]); 786 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i]);
787 } 787 }
788 if (err) { 788 if (err) {
789 usbnet_ep_close_pipes(un); 789 usbnet_ep_close_pipes(un);
790 return err; 790 return err;
791 } 791 }
792 } 792 }
793 793
794 return USBD_NORMAL_COMPLETION; 794 return USBD_NORMAL_COMPLETION;
795} 795}
796 796
797static usbd_status 797static usbd_status
798usbnet_ep_stop_pipes(struct usbnet * const un) 798usbnet_ep_stop_pipes(struct usbnet * const un)
799{ 799{
800 struct usbnet_private * const unp = un->un_pri; 800 struct usbnet_private * const unp = un->un_pri;
801 usbd_status err = USBD_NORMAL_COMPLETION; 801 usbd_status err = USBD_NORMAL_COMPLETION;
802 802
803 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) { 803 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) {
804 if (unp->unp_ep[i] == NULL) 804 if (unp->unp_ep[i] == NULL)
805 continue; 805 continue;
806 usbd_status err2 = usbd_abort_pipe(unp->unp_ep[i]); 806 usbd_status err2 = usbd_abort_pipe(unp->unp_ep[i]);
807 if (err == USBD_NORMAL_COMPLETION && err2) 807 if (err == USBD_NORMAL_COMPLETION && err2)
808 err = err2; 808 err = err2;
809 } 809 }
810 810
811 return err; 811 return err;
812} 812}
813 813
814int 814int
815usbnet_init_rx_tx(struct usbnet * const un) 815usbnet_init_rx_tx(struct usbnet * const un)
816{ 816{
817 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 817 USBNETHIST_FUNC(); USBNETHIST_CALLED();
818 struct usbnet_private * const unp = un->un_pri; 818 struct usbnet_private * const unp = un->un_pri;
819 struct ifnet * const ifp = usbnet_ifp(un); 819 struct ifnet * const ifp = usbnet_ifp(un);
820 usbd_status err; 820 usbd_status err;
821 int error = 0; 821 int error = 0;
822 822
823 KASSERTMSG(!unp->unp_ifp_attached || IFNET_LOCKED(ifp), 823 KASSERTMSG(!unp->unp_ifp_attached || IFNET_LOCKED(ifp),
824 "%s", ifp->if_xname); 824 "%s", ifp->if_xname);
825 825
826 usbnet_isowned_core(un); 826 usbnet_isowned_core(un);
827 827
828 if (unp->unp_dying) { 828 if (unp->unp_dying) {
829 return EIO; 829 return EIO;
830 } 830 }
831 831
832 usbnet_busy(un); 832 usbnet_busy(un);
833 833
834 /* Open RX and TX pipes. */ 834 /* Open RX and TX pipes. */
835 err = usbnet_ep_open_pipes(un); 835 err = usbnet_ep_open_pipes(un);
836 if (err) { 836 if (err) {
837 aprint_error_dev(un->un_dev, "open rx/tx pipes failed: %s\n", 837 aprint_error_dev(un->un_dev, "open rx/tx pipes failed: %s\n",
838 usbd_errstr(err)); 838 usbd_errstr(err));
839 error = EIO; 839 error = EIO;
840 goto out; 840 goto out;
841 } 841 }
842 842
843 /* Init RX ring. */ 843 /* Init RX ring. */
844 if (usbnet_rx_list_init(un)) { 844 if (usbnet_rx_list_init(un)) {
845 aprint_error_dev(un->un_dev, "rx list init failed\n"); 845 aprint_error_dev(un->un_dev, "rx list init failed\n");
846 error = ENOBUFS; 846 error = ENOBUFS;
847 goto out; 847 goto out;
848 } 848 }
849 849
850 /* Init TX ring. */ 850 /* Init TX ring. */
851 if (usbnet_tx_list_init(un)) { 851 if (usbnet_tx_list_init(un)) {
852 aprint_error_dev(un->un_dev, "tx list init failed\n"); 852 aprint_error_dev(un->un_dev, "tx list init failed\n");
853 error = ENOBUFS; 853 error = ENOBUFS;
854 goto out; 854 goto out;
855 } 855 }
856 856
857 /* Indicate we are up and running. */ 857 /* Indicate we are up and running. */
858#if 0 858#if 0
859 /* XXX if_mcast_op() can call this without ifnet locked */ 859 /* XXX if_mcast_op() can call this without ifnet locked */
860 KASSERT(ifp->if_softc == NULL || IFNET_LOCKED(ifp)); 860 KASSERT(ifp->if_softc == NULL || IFNET_LOCKED(ifp));
861#endif 861#endif
862 ifp->if_flags |= IFF_RUNNING; 862 ifp->if_flags |= IFF_RUNNING;
863 863
864 /* Start up the receive pipe(s). */ 864 /* Start up the receive pipe(s). */
865 usbnet_rx_start_pipes(un); 865 usbnet_rx_start_pipes(un);
866 866
867 callout_schedule(&unp->unp_stat_ch, hz); 867 callout_schedule(&unp->unp_stat_ch, hz);
868 868
869out: 869out:
870 if (error) { 870 if (error) {
871 usbnet_rx_list_fini(un); 871 usbnet_rx_list_fini(un);
872 usbnet_tx_list_fini(un); 872 usbnet_tx_list_fini(un);
873 usbnet_ep_close_pipes(un); 873 usbnet_ep_close_pipes(un);
874 } 874 }
875 usbnet_unbusy(un); 875 usbnet_unbusy(un);
876 876
877 usbnet_isowned_core(un); 877 usbnet_isowned_core(un);
878 878
879 return error; 879 return error;
880} 880}
881 881
882void 882void
883usbnet_busy(struct usbnet *un) 883usbnet_busy(struct usbnet *un)
884{ 884{
885 struct usbnet_private * const unp = un->un_pri; 885 struct usbnet_private * const unp = un->un_pri;
886 886
887 usbnet_isowned_core(un); 887 usbnet_isowned_core(un);
888 888
889 unp->unp_refcnt++; 889 unp->unp_refcnt++;
890} 890}
891 891
892void 892void
893usbnet_unbusy(struct usbnet *un) 893usbnet_unbusy(struct usbnet *un)
894{ 894{
895 struct usbnet_private * const unp = un->un_pri; 895 struct usbnet_private * const unp = un->un_pri;
896 896
897 usbnet_isowned_core(un); 897 usbnet_isowned_core(un);
898 898
899 if (--unp->unp_refcnt < 0) 899 if (--unp->unp_refcnt < 0)
900 cv_broadcast(&unp->unp_detachcv); 900 cv_broadcast(&unp->unp_detachcv);
901} 901}
902 902
903/* MII management. */ 903/* MII management. */
904 904
905int 905int
906usbnet_mii_readreg(device_t dev, int phy, int reg, uint16_t *val) 906usbnet_mii_readreg(device_t dev, int phy, int reg, uint16_t *val)
907{ 907{
908 USBNETHIST_FUNC(); 908 USBNETHIST_FUNC();
909 struct usbnet * const un = device_private(dev); 909 struct usbnet * const un = device_private(dev);
910 struct usbnet_private * const unp = un->un_pri; 910 struct usbnet_private * const unp = un->un_pri;
911 int err; 911 int err;
912 912
913 /* MII layer ensures core_lock is held. */ 913 /* MII layer ensures core_lock is held. */
914 usbnet_isowned_core(un); 914 usbnet_isowned_core(un);
915 915
916 if (unp->unp_dying) { 916 if (unp->unp_dying) {
917 return EIO; 917 return EIO;
918 } 918 }
919 919
920 usbnet_busy(un); 920 usbnet_busy(un);
921 err = uno_read_reg(un, phy, reg, val); 921 err = uno_read_reg(un, phy, reg, val);
922 usbnet_unbusy(un); 922 usbnet_unbusy(un);
923 923
924 if (err) { 924 if (err) {
925 USBNETHIST_CALLARGS("%jd: read PHY failed: %jd", 925 USBNETHIST_CALLARGS("%jd: read PHY failed: %jd",
926 unp->unp_number, err, 0, 0); 926 unp->unp_number, err, 0, 0);
927 return err; 927 return err;
928 } 928 }
929 929
930 return 0; 930 return 0;
931} 931}
932 932
933int 933int
934usbnet_mii_writereg(device_t dev, int phy, int reg, uint16_t val) 934usbnet_mii_writereg(device_t dev, int phy, int reg, uint16_t val)
935{ 935{
936 USBNETHIST_FUNC(); 936 USBNETHIST_FUNC();
937 struct usbnet * const un = device_private(dev); 937 struct usbnet * const un = device_private(dev);
938 struct usbnet_private * const unp = un->un_pri; 938 struct usbnet_private * const unp = un->un_pri;
939 int err; 939 int err;
940 940
941 /* MII layer ensures core_lock is held. */ 941 /* MII layer ensures core_lock is held. */
942 usbnet_isowned_core(un); 942 usbnet_isowned_core(un);
943 943
944 if (unp->unp_dying) { 944 if (unp->unp_dying) {
945 return EIO; 945 return EIO;
946 } 946 }
947 947
948 usbnet_busy(un); 948 usbnet_busy(un);
949 err = uno_write_reg(un, phy, reg, val); 949 err = uno_write_reg(un, phy, reg, val);
950 usbnet_unbusy(un); 950 usbnet_unbusy(un);
951 951
952 if (err) { 952 if (err) {
953 USBNETHIST_CALLARGS("%jd: write PHY failed: %jd", 953 USBNETHIST_CALLARGS("%jd: write PHY failed: %jd",
954 unp->unp_number, err, 0, 0); 954 unp->unp_number, err, 0, 0);
955 return err; 955 return err;
956 } 956 }
957 957
958 return 0; 958 return 0;
959} 959}
960 960
961void 961void
962usbnet_mii_statchg(struct ifnet *ifp) 962usbnet_mii_statchg(struct ifnet *ifp)
963{ 963{
964 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 964 USBNETHIST_FUNC(); USBNETHIST_CALLED();
965 struct usbnet * const un = ifp->if_softc; 965 struct usbnet * const un = ifp->if_softc;
966 966
967 /* MII layer ensures core_lock is held. */ 967 /* MII layer ensures core_lock is held. */
968 usbnet_isowned_core(un); 968 usbnet_isowned_core(un);
969 969
970 usbnet_busy(un); 970 usbnet_busy(un);
971 uno_mii_statchg(un, ifp); 971 uno_mii_statchg(un, ifp);
972 usbnet_unbusy(un); 972 usbnet_unbusy(un);
973} 973}
974 974
975static int 975static int
976usbnet_media_upd(struct ifnet *ifp) 976usbnet_media_upd(struct ifnet *ifp)
977{ 977{
978 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 978 USBNETHIST_FUNC(); USBNETHIST_CALLED();
979 struct usbnet * const un = ifp->if_softc; 979 struct usbnet * const un = ifp->if_softc;
980 struct usbnet_private * const unp = un->un_pri; 980 struct usbnet_private * const unp = un->un_pri;
981 struct mii_data * const mii = usbnet_mii(un); 981 struct mii_data * const mii = usbnet_mii(un);
982 982
983 /* ifmedia layer ensures core_lock is held. */ 983 /* ifmedia layer ensures core_lock is held. */
984 usbnet_isowned_core(un); 984 usbnet_isowned_core(un);
985 985
986 if (unp->unp_dying) 986 if (unp->unp_dying)
987 return EIO; 987 return EIO;
988 988
989 unp->unp_link = false; 989 unp->unp_link = false;
990 990
991 if (mii->mii_instance) { 991 if (mii->mii_instance) {
992 struct mii_softc *miisc; 992 struct mii_softc *miisc;
993 993
994 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 994 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
995 mii_phy_reset(miisc); 995 mii_phy_reset(miisc);
996 } 996 }
997 997
998 return ether_mediachange(ifp); 998 return ether_mediachange(ifp);
999} 999}
1000 1000
1001/* ioctl */ 1001/* ioctl */
1002 1002
1003static int 1003static int
1004usbnet_ifflags_cb(struct ethercom *ec) 1004usbnet_ifflags_cb(struct ethercom *ec)
1005{ 1005{
1006 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1006 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1007 struct ifnet *ifp = &ec->ec_if; 1007 struct ifnet *ifp = &ec->ec_if;
1008 struct usbnet *un = ifp->if_softc; 1008 struct usbnet *un = ifp->if_softc;
1009 struct usbnet_private * const unp = un->un_pri; 1009 struct usbnet_private * const unp = un->un_pri;
1010 int rv = 0; 1010 int rv = 0;
1011 1011
1012 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 1012 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname);
1013 1013
1014 mutex_enter(&unp->unp_core_lock); 1014 mutex_enter(&unp->unp_core_lock);
1015 1015
1016 const u_short changed = ifp->if_flags ^ unp->unp_if_flags; 1016 const u_short changed = ifp->if_flags ^ unp->unp_if_flags;
1017 if ((changed & ~(IFF_CANTCHANGE | IFF_DEBUG)) == 0) { 1017 if ((changed & ~(IFF_CANTCHANGE | IFF_DEBUG)) == 0) {
1018 unp->unp_if_flags = ifp->if_flags; 1018 unp->unp_if_flags = ifp->if_flags;
1019 if ((changed & IFF_PROMISC) != 0) 1019 if ((changed & IFF_PROMISC) != 0)
1020 rv = ENETRESET; 1020 rv = ENETRESET;
1021 } else { 1021 } else {
1022 rv = ENETRESET; 1022 rv = ENETRESET;
1023 } 1023 }
1024 1024
1025 mutex_exit(&unp->unp_core_lock); 1025 mutex_exit(&unp->unp_core_lock);
1026 1026
1027 return rv; 1027 return rv;
1028} 1028}
1029 1029
1030static int 1030static int
1031usbnet_if_ioctl(struct ifnet *ifp, u_long cmd, void *data) 1031usbnet_if_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1032{ 1032{
1033 USBNETHIST_FUNC(); 1033 USBNETHIST_FUNC();
1034 struct usbnet * const un = ifp->if_softc; 1034 struct usbnet * const un = ifp->if_softc;
1035 struct usbnet_private * const unp __unused = un->un_pri; 1035 struct usbnet_private * const unp __unused = un->un_pri;
1036 int error; 1036 int error;
1037 1037
1038 USBNETHIST_CALLARGSN(11, "%jd: enter %#jx data %#jx", 1038 USBNETHIST_CALLARGSN(11, "%jd: enter %#jx data %#jx",
1039 unp->unp_number, cmd, (uintptr_t)data, 0); 1039 unp->unp_number, cmd, (uintptr_t)data, 0);
1040 1040
1041 if (un->un_ops->uno_override_ioctl) 1041 if (un->un_ops->uno_override_ioctl)
1042 return uno_override_ioctl(un, ifp, cmd, data); 1042 return uno_override_ioctl(un, ifp, cmd, data);
1043 1043
1044 error = ether_ioctl(ifp, cmd, data); 1044 error = ether_ioctl(ifp, cmd, data);
1045 if (error == ENETRESET) { 1045 if (error == ENETRESET) {
1046 switch (cmd) { 1046 switch (cmd) {
1047 case SIOCADDMULTI: 1047 case SIOCADDMULTI:
1048 case SIOCDELMULTI: 1048 case SIOCDELMULTI:
1049 usb_add_task(un->un_udev, &unp->unp_mcasttask, 1049 usb_add_task(un->un_udev, &unp->unp_mcasttask,
1050 USB_TASKQ_DRIVER); 1050 USB_TASKQ_DRIVER);
1051 error = 0; 1051 error = 0;
1052 break; 1052 break;
1053 default: 1053 default:
1054 error = uno_ioctl(un, ifp, cmd, data); 1054 error = uno_ioctl(un, ifp, cmd, data);
1055 } 1055 }
1056 } 1056 }
1057 1057
1058 return error; 1058 return error;
1059} 1059}
1060 1060
1061static void 1061static void
1062usbnet_mcast_task(void *arg) 1062usbnet_mcast_task(void *arg)
1063{ 1063{
1064 USBNETHIST_FUNC(); 1064 USBNETHIST_FUNC();
1065 struct usbnet * const un = arg; 1065 struct usbnet * const un = arg;
1066 struct usbnet_private * const unp = un->un_pri; 1066 struct usbnet_private * const unp = un->un_pri;
1067 struct ifnet * const ifp = usbnet_ifp(un); 1067 struct ifnet * const ifp = usbnet_ifp(un);
1068 bool dying; 1068 bool dying;
1069 struct ifreq ifr; 1069 struct ifreq ifr;
1070 1070
1071 USBNETHIST_CALLARGSN(10, "%jd: enter", unp->unp_number, 0, 0, 0); 1071 USBNETHIST_CALLARGSN(10, "%jd: enter", unp->unp_number, 0, 0, 0);
1072 1072
1073 /* 1073 /*
1074 * If we're detaching, we must check unp_dying _before_ 1074 * If we're detaching, we must check unp_dying _before_
1075 * touching IFNET_LOCK -- the ifnet may have been detached by 1075 * touching IFNET_LOCK -- the ifnet may have been detached by
1076 * the time this task runs. This is racy -- unp_dying may be 1076 * the time this task runs. This is racy -- unp_dying may be
1077 * set immediately after we test it -- but nevertheless safe, 1077 * set immediately after we test it -- but nevertheless safe,
1078 * because usbnet_detach waits for the task to complete before 1078 * because usbnet_detach waits for the task to complete before
1079 * issuing if_detach, and necessary, so that we don't touch 1079 * issuing if_detach, and necessary, so that we don't touch
1080 * IFNET_LOCK after if_detach. See usbnet_detach for details. 1080 * IFNET_LOCK after if_detach. See usbnet_detach for details.
1081 */ 1081 */
1082 mutex_enter(&unp->unp_core_lock); 1082 mutex_enter(&unp->unp_core_lock);
1083 dying = unp->unp_dying; 1083 dying = unp->unp_dying;
1084 mutex_exit(&unp->unp_core_lock); 1084 mutex_exit(&unp->unp_core_lock);
1085 if (dying) 1085 if (dying)
1086 return; 1086 return;
1087 1087
1088 /* 1088 /*
1089 * Pass a bogus ifr with SIOCDELMULTI -- the goal is to just 1089 * Pass a bogus ifr with SIOCDELMULTI -- the goal is to just
1090 * notify the driver to reprogram any hardware multicast 1090 * notify the driver to reprogram any hardware multicast
1091 * filter, according to what's already stored in the ethercom. 1091 * filter, according to what's already stored in the ethercom.
1092 * None of the drivers actually examine this argument, so it 1092 * None of the drivers actually examine this argument, so it
1093 * doesn't change the ABI as far as they can tell. 1093 * doesn't change the ABI as far as they can tell.
1094 */ 1094 */
1095 IFNET_LOCK(ifp); 1095 IFNET_LOCK(ifp);
1096 if (ifp->if_flags & IFF_RUNNING) { 1096 if (ifp->if_flags & IFF_RUNNING) {
1097 memset(&ifr, 0, sizeof(ifr)); 1097 memset(&ifr, 0, sizeof(ifr));
1098 (void)uno_ioctl(un, ifp, SIOCDELMULTI, &ifr); 1098 (void)uno_ioctl(un, ifp, SIOCDELMULTI, &ifr);
1099 } 1099 }
1100 IFNET_UNLOCK(ifp); 1100 IFNET_UNLOCK(ifp);
1101} 1101}
1102 1102
1103/* 1103/*
1104 * Generic stop network function: 1104 * Generic stop network function:
1105 * - mark as stopping 1105 * - mark as stopping
1106 * - call DD routine to stop the device 1106 * - call DD routine to stop the device
1107 * - turn off running, timer, statchg callout, link 1107 * - turn off running, timer, statchg callout, link
1108 * - stop transfers 1108 * - stop transfers
1109 * - free RX and TX resources 1109 * - free RX and TX resources
1110 * - close pipes 1110 * - close pipes
1111 * 1111 *
1112 * usbnet_stop() is exported for drivers to use, expects lock held. 1112 * usbnet_stop() is exported for drivers to use, expects lock held.
1113 * 1113 *
1114 * usbnet_if_stop() is for the if_stop handler. 1114 * usbnet_if_stop() is for the if_stop handler.
1115 */ 1115 */
1116void 1116void
1117usbnet_stop(struct usbnet *un, struct ifnet *ifp, int disable) 1117usbnet_stop(struct usbnet *un, struct ifnet *ifp, int disable)
1118{ 1118{
1119 struct usbnet_private * const unp = un->un_pri; 1119 struct usbnet_private * const unp = un->un_pri;
1120 1120
1121 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1121 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1122 1122
1123 KASSERTMSG(!unp->unp_ifp_attached || IFNET_LOCKED(ifp), 1123 KASSERTMSG(!unp->unp_ifp_attached || IFNET_LOCKED(ifp),
1124 "%s", ifp->if_xname); 1124 "%s", ifp->if_xname);
1125 usbnet_isowned_core(un); 1125 usbnet_isowned_core(un);
1126 1126
1127 usbnet_busy(un); 1127 usbnet_busy(un);
1128 1128
1129 mutex_enter(&unp->unp_rxlock); 1129 mutex_enter(&unp->unp_rxlock);
1130 mutex_enter(&unp->unp_txlock); 1130 mutex_enter(&unp->unp_txlock);
1131 unp->unp_stopping = true; 1131 unp->unp_stopping = true;
1132 mutex_exit(&unp->unp_txlock); 1132 mutex_exit(&unp->unp_txlock);
1133 mutex_exit(&unp->unp_rxlock); 1133 mutex_exit(&unp->unp_rxlock);
1134 1134
1135 uno_stop(un, ifp, disable); 1135 uno_stop(un, ifp, disable);
1136 1136
1137 mutex_enter(&unp->unp_txlock); 1137 mutex_enter(&unp->unp_txlock);
1138 unp->unp_timer = 0; 1138 unp->unp_timer = 0;
1139 mutex_exit(&unp->unp_txlock); 1139 mutex_exit(&unp->unp_txlock);
1140 1140
1141 callout_halt(&unp->unp_stat_ch, &unp->unp_core_lock); 1141 callout_halt(&unp->unp_stat_ch, &unp->unp_core_lock);
1142 usb_rem_task_wait(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER, 1142 usb_rem_task_wait(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER,
1143 &unp->unp_core_lock); 1143 &unp->unp_core_lock);
1144 1144
1145 /* Stop transfers. */ 1145 /* Stop transfers. */
1146 usbnet_ep_stop_pipes(un); 1146 usbnet_ep_stop_pipes(un);
1147 1147
1148 /* Free RX/TX resources. */ 1148 /* Free RX/TX resources. */
1149 usbnet_rx_list_fini(un); 1149 usbnet_rx_list_fini(un);
1150 usbnet_tx_list_fini(un); 1150 usbnet_tx_list_fini(un);
1151 1151
1152 /* Close pipes. */ 1152 /* Close pipes. */
1153 usbnet_ep_close_pipes(un); 1153 usbnet_ep_close_pipes(un);
1154 1154
1155 /* Everything is quesced now. */ 1155 /* Everything is quesced now. */
1156 KASSERTMSG(!unp->unp_ifp_attached || IFNET_LOCKED(ifp), 1156 KASSERTMSG(!unp->unp_ifp_attached || IFNET_LOCKED(ifp),
1157 "%s", ifp->if_xname); 1157 "%s", ifp->if_xname);
1158 ifp->if_flags &= ~IFF_RUNNING; 1158 ifp->if_flags &= ~IFF_RUNNING;
1159 1159
1160 usbnet_unbusy(un); 1160 usbnet_unbusy(un);
1161} 1161}
1162 1162
1163static void 1163static void
1164usbnet_if_stop(struct ifnet *ifp, int disable) 1164usbnet_if_stop(struct ifnet *ifp, int disable)
1165{ 1165{
1166 struct usbnet * const un = ifp->if_softc; 1166 struct usbnet * const un = ifp->if_softc;
1167 struct usbnet_private * const unp = un->un_pri; 1167 struct usbnet_private * const unp = un->un_pri;
1168 1168
1169 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 1169 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname);
1170 1170
1171 mutex_enter(&unp->unp_core_lock); 1171 mutex_enter(&unp->unp_core_lock);
1172 usbnet_stop(un, ifp, disable); 1172 usbnet_stop(un, ifp, disable);
1173 mutex_exit(&unp->unp_core_lock); 1173 mutex_exit(&unp->unp_core_lock);
1174} 1174}
1175 1175
1176/* 1176/*
1177 * Generic tick task function. 1177 * Generic tick task function.
1178 * 1178 *
1179 * usbnet_tick() is triggered from a callout, and triggers a call to 1179 * usbnet_tick() is triggered from a callout, and triggers a call to
1180 * usbnet_tick_task() from the usb_task subsystem. 1180 * usbnet_tick_task() from the usb_task subsystem.
1181 */ 1181 */
1182static void 1182static void
1183usbnet_tick(void *arg) 1183usbnet_tick(void *arg)
1184{ 1184{
1185 USBNETHIST_FUNC(); 1185 USBNETHIST_FUNC();
1186 struct usbnet * const un = arg; 1186 struct usbnet * const un = arg;
1187 struct usbnet_private * const unp = un->un_pri; 1187 struct usbnet_private * const unp = un->un_pri;
1188 1188
1189 USBNETHIST_CALLARGSN(10, "%jd: enter", unp->unp_number, 0, 0, 0); 1189 USBNETHIST_CALLARGSN(10, "%jd: enter", unp->unp_number, 0, 0, 0);
1190 1190
1191 if (unp != NULL && !unp->unp_stopping && !unp->unp_dying) { 1191 if (unp != NULL && !unp->unp_stopping && !unp->unp_dying) {
1192 /* Perform periodic stuff in process context */ 1192 /* Perform periodic stuff in process context */
1193 usb_add_task(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER); 1193 usb_add_task(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER);
1194 } 1194 }
1195} 1195}
1196 1196
1197static void 1197static void
1198usbnet_watchdog(struct ifnet *ifp) 1198usbnet_watchdog(struct ifnet *ifp)
1199{ 1199{
1200 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1200 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1201 struct usbnet * const un = ifp->if_softc; 1201 struct usbnet * const un = ifp->if_softc;
1202 struct usbnet_private * const unp = un->un_pri; 1202 struct usbnet_private * const unp = un->un_pri;
1203 struct usbnet_cdata * const cd = un_cdata(un); 1203 struct usbnet_cdata * const cd = un_cdata(un);
1204 usbd_status err; 1204 usbd_status err;
1205 1205
1206 if_statinc(ifp, if_oerrors); 1206 if_statinc(ifp, if_oerrors);
1207 device_printf(un->un_dev, "watchdog timeout\n"); 1207 device_printf(un->un_dev, "watchdog timeout\n");
1208 1208
1209 if (cd->uncd_tx_cnt > 0) { 1209 if (cd->uncd_tx_cnt > 0) {
1210 DPRINTF("uncd_tx_cnt=%ju non zero, aborting pipe", 0, 0, 0, 0); 1210 DPRINTF("uncd_tx_cnt=%ju non zero, aborting pipe", 0, 0, 0, 0);
1211 err = usbd_abort_pipe(unp->unp_ep[USBNET_ENDPT_TX]); 1211 err = usbd_abort_pipe(unp->unp_ep[USBNET_ENDPT_TX]);
1212 if (err) 1212 if (err)
1213 device_printf(un->un_dev, "pipe abort failed: %s\n", 1213 device_printf(un->un_dev, "pipe abort failed: %s\n",
1214 usbd_errstr(err)); 1214 usbd_errstr(err));
1215 if (cd->uncd_tx_cnt != 0) 1215 if (cd->uncd_tx_cnt != 0)
1216 DPRINTF("uncd_tx_cnt now %ju", cd->uncd_tx_cnt, 0, 0, 0); 1216 DPRINTF("uncd_tx_cnt now %ju", cd->uncd_tx_cnt, 0, 0, 0);
1217 } 1217 }
1218 1218
1219 if (!IFQ_IS_EMPTY(&ifp->if_snd)) 1219 if (!IFQ_IS_EMPTY(&ifp->if_snd))
1220 (*ifp->if_start)(ifp); 1220 (*ifp->if_start)(ifp);
1221} 1221}
1222 1222
1223static void 1223static void
1224usbnet_tick_task(void *arg) 1224usbnet_tick_task(void *arg)
1225{ 1225{
1226 USBNETHIST_FUNC(); 1226 USBNETHIST_FUNC();
1227 struct usbnet * const un = arg; 1227 struct usbnet * const un = arg;
1228 struct usbnet_private * const unp = un->un_pri; 1228 struct usbnet_private * const unp = un->un_pri;
1229 1229
1230 USBNETHIST_CALLARGSN(8, "%jd: enter", unp->unp_number, 0, 0, 0); 1230 USBNETHIST_CALLARGSN(8, "%jd: enter", unp->unp_number, 0, 0, 0);
1231 1231
1232 mutex_enter(&unp->unp_core_lock); 1232 mutex_enter(&unp->unp_core_lock);
1233 if (unp->unp_stopping || unp->unp_dying) { 1233 if (unp->unp_stopping || unp->unp_dying) {
1234 mutex_exit(&unp->unp_core_lock); 1234 mutex_exit(&unp->unp_core_lock);
1235 return; 1235 return;
1236 } 1236 }
1237 1237
1238 struct ifnet * const ifp = usbnet_ifp(un); 1238 struct ifnet * const ifp = usbnet_ifp(un);
1239 struct mii_data * const mii = usbnet_mii(un); 1239 struct mii_data * const mii = usbnet_mii(un);
1240 1240
1241 KASSERT(ifp != NULL); /* embedded member */ 1241 KASSERT(ifp != NULL); /* embedded member */
1242 1242
1243 usbnet_busy(un); 1243 usbnet_busy(un);
1244 mutex_exit(&unp->unp_core_lock); 1244 mutex_exit(&unp->unp_core_lock);
1245 1245
1246 mutex_enter(&unp->unp_txlock); 1246 mutex_enter(&unp->unp_txlock);
1247 const bool timeout = unp->unp_timer != 0 && --unp->unp_timer == 0; 1247 const bool timeout = unp->unp_timer != 0 && --unp->unp_timer == 0;
1248 mutex_exit(&unp->unp_txlock); 1248 mutex_exit(&unp->unp_txlock);
1249 if (timeout) 1249 if (timeout)
1250 usbnet_watchdog(ifp); 1250 usbnet_watchdog(ifp);
1251 1251
1252 DPRINTFN(8, "mii %#jx ifp %#jx", (uintptr_t)mii, (uintptr_t)ifp, 0, 0); 1252 DPRINTFN(8, "mii %#jx ifp %#jx", (uintptr_t)mii, (uintptr_t)ifp, 0, 0);
1253 if (mii) { 1253 if (mii) {
1254 mutex_enter(&unp->unp_core_lock); 1254 mutex_enter(&unp->unp_core_lock);
1255 mii_tick(mii); 1255 mii_tick(mii);
1256 if (!unp->unp_link) 1256 if (!unp->unp_link)
1257 (*mii->mii_statchg)(ifp); 1257 (*mii->mii_statchg)(ifp);
1258 mutex_exit(&unp->unp_core_lock); 1258 mutex_exit(&unp->unp_core_lock);
1259 } 1259 }
1260 1260
1261 /* Call driver if requested. */ 1261 /* Call driver if requested. */
1262 uno_tick(un); 1262 uno_tick(un);
1263 1263
1264 mutex_enter(&unp->unp_core_lock); 1264 mutex_enter(&unp->unp_core_lock);
1265 usbnet_unbusy(un); 1265 usbnet_unbusy(un);
1266 if (!unp->unp_stopping && !unp->unp_dying) 1266 if (!unp->unp_stopping && !unp->unp_dying)
1267 callout_schedule(&unp->unp_stat_ch, hz); 1267 callout_schedule(&unp->unp_stat_ch, hz);
1268 mutex_exit(&unp->unp_core_lock); 1268 mutex_exit(&unp->unp_core_lock);
1269} 1269}
1270 1270
1271static int 1271static int
1272usbnet_if_init(struct ifnet *ifp) 1272usbnet_if_init(struct ifnet *ifp)
1273{ 1273{
1274 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1274 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1275 struct usbnet * const un = ifp->if_softc; 1275 struct usbnet * const un = ifp->if_softc;
1276 1276
1277 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 1277 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname);
1278 1278
1279 return uno_init(un, ifp); 1279 return uno_init(un, ifp);
1280} 1280}
1281 1281
1282 1282
1283/* Various accessors. */ 1283/* Various accessors. */
1284 1284
1285void 1285void
1286usbnet_set_link(struct usbnet *un, bool link) 1286usbnet_set_link(struct usbnet *un, bool link)
1287{ 1287{
1288 un->un_pri->unp_link = link; 1288 un->un_pri->unp_link = link;
1289} 1289}
1290 1290
1291void 
1292usbnet_set_dying(struct usbnet *un, bool link) 
1293{ 
1294 un->un_pri->unp_dying = link; 
1295} 
1296 
1297struct ifnet * 1291struct ifnet *
1298usbnet_ifp(struct usbnet *un) 1292usbnet_ifp(struct usbnet *un)
1299{ 1293{
1300 return &un->un_pri->unp_ec.ec_if; 1294 return &un->un_pri->unp_ec.ec_if;
1301} 1295}
1302 1296
1303struct ethercom * 1297struct ethercom *
1304usbnet_ec(struct usbnet *un) 1298usbnet_ec(struct usbnet *un)
1305{ 1299{
1306 return &un->un_pri->unp_ec; 1300 return &un->un_pri->unp_ec;
1307} 1301}
1308 1302
1309struct mii_data * 1303struct mii_data *
1310usbnet_mii(struct usbnet *un) 1304usbnet_mii(struct usbnet *un)
1311{ 1305{
1312 return un->un_pri->unp_ec.ec_mii; 1306 return un->un_pri->unp_ec.ec_mii;
1313} 1307}
1314 1308
1315krndsource_t * 1309krndsource_t *
1316usbnet_rndsrc(struct usbnet *un) 1310usbnet_rndsrc(struct usbnet *un)
1317{ 1311{
1318 return &un->un_pri->unp_rndsrc; 1312 return &un->un_pri->unp_rndsrc;
1319} 1313}
1320 1314
1321void * 1315void *
1322usbnet_softc(struct usbnet *un) 1316usbnet_softc(struct usbnet *un)
1323{ 1317{
1324 return un->un_sc; 1318 return un->un_sc;
1325} 1319}
1326 1320
1327bool 1321bool
1328usbnet_havelink(struct usbnet *un) 1322usbnet_havelink(struct usbnet *un)
1329{ 1323{
1330 return un->un_pri->unp_link; 1324 return un->un_pri->unp_link;
1331} 1325}
1332 1326
1333bool 1327bool
1334usbnet_isdying(struct usbnet *un) 1328usbnet_isdying(struct usbnet *un)
1335{ 1329{
1336 return un->un_pri->unp_dying; 1330 return un->un_pri->unp_dying;
1337} 1331}
1338 1332
1339 1333
1340/* Locking. */ 1334/* Locking. */
1341 1335
1342void 1336void
1343usbnet_lock_core(struct usbnet *un) 1337usbnet_lock_core(struct usbnet *un)
1344{ 1338{
1345 mutex_enter(&un->un_pri->unp_core_lock); 1339 mutex_enter(&un->un_pri->unp_core_lock);
1346} 1340}
1347 1341
1348void 1342void
1349usbnet_unlock_core(struct usbnet *un) 1343usbnet_unlock_core(struct usbnet *un)
1350{ 1344{
1351 mutex_exit(&un->un_pri->unp_core_lock); 1345 mutex_exit(&un->un_pri->unp_core_lock);
1352} 1346}
1353 1347
1354kmutex_t * 1348kmutex_t *
1355usbnet_mutex_core(struct usbnet *un) 1349usbnet_mutex_core(struct usbnet *un)
1356{ 1350{
1357 return &un->un_pri->unp_core_lock; 1351 return &un->un_pri->unp_core_lock;
1358} 1352}
1359 1353
1360void 1354void
1361usbnet_lock_rx(struct usbnet *un) 1355usbnet_lock_rx(struct usbnet *un)
1362{ 1356{
1363 mutex_enter(&un->un_pri->unp_rxlock); 1357 mutex_enter(&un->un_pri->unp_rxlock);
1364} 1358}
1365 1359
1366void 1360void
1367usbnet_unlock_rx(struct usbnet *un) 1361usbnet_unlock_rx(struct usbnet *un)
1368{ 1362{
1369 mutex_exit(&un->un_pri->unp_rxlock); 1363 mutex_exit(&un->un_pri->unp_rxlock);
1370} 1364}
1371 1365
1372kmutex_t * 1366kmutex_t *
1373usbnet_mutex_rx(struct usbnet *un) 1367usbnet_mutex_rx(struct usbnet *un)
1374{ 1368{
1375 return &un->un_pri->unp_rxlock; 1369 return &un->un_pri->unp_rxlock;
1376} 1370}
1377 1371
1378void 1372void
1379usbnet_lock_tx(struct usbnet *un) 1373usbnet_lock_tx(struct usbnet *un)
1380{ 1374{
1381 mutex_enter(&un->un_pri->unp_txlock); 1375 mutex_enter(&un->un_pri->unp_txlock);
1382} 1376}
1383 1377
1384void 1378void
1385usbnet_unlock_tx(struct usbnet *un) 1379usbnet_unlock_tx(struct usbnet *un)
1386{ 1380{
1387 mutex_exit(&un->un_pri->unp_txlock); 1381 mutex_exit(&un->un_pri->unp_txlock);
1388} 1382}
1389 1383
1390kmutex_t * 1384kmutex_t *
1391usbnet_mutex_tx(struct usbnet *un) 1385usbnet_mutex_tx(struct usbnet *un)
1392{ 1386{
1393 return &un->un_pri->unp_txlock; 1387 return &un->un_pri->unp_txlock;
1394} 1388}
1395 1389
1396/* Autoconf management. */ 1390/* Autoconf management. */
1397 1391
1398static bool 1392static bool
1399usbnet_empty_eaddr(struct usbnet * const un) 1393usbnet_empty_eaddr(struct usbnet * const un)
1400{ 1394{
1401 return (un->un_eaddr[0] == 0 && un->un_eaddr[1] == 0 && 1395 return (un->un_eaddr[0] == 0 && un->un_eaddr[1] == 0 &&
1402 un->un_eaddr[2] == 0 && un->un_eaddr[3] == 0 && 1396 un->un_eaddr[2] == 0 && un->un_eaddr[3] == 0 &&
1403 un->un_eaddr[4] == 0 && un->un_eaddr[5] == 0); 1397 un->un_eaddr[4] == 0 && un->un_eaddr[5] == 0);
1404} 1398}
1405 1399
1406/* 1400/*
1407 * usbnet_attach() and usbnet_attach_ifp() perform setup of the relevant 1401 * usbnet_attach() and usbnet_attach_ifp() perform setup of the relevant
1408 * 'usbnet'. The first is enough to enable device access (eg, endpoints 1402 * 'usbnet'. The first is enough to enable device access (eg, endpoints
1409 * are connected and commands can be sent), and the second connects the 1403 * are connected and commands can be sent), and the second connects the
1410 * device to the system networking. 1404 * device to the system networking.
1411 * 1405 *
1412 * Always call usbnet_detach(), even if usbnet_attach_ifp() is skippped. 1406 * Always call usbnet_detach(), even if usbnet_attach_ifp() is skippped.
1413 * Also usable as driver detach directly. 1407 * Also usable as driver detach directly.
1414 * 1408 *
1415 * To skip ethernet configuration (eg, point-to-point), make sure that 1409 * To skip ethernet configuration (eg, point-to-point), make sure that
1416 * the un_eaddr[] is fully zero. 1410 * the un_eaddr[] is fully zero.
1417 */ 1411 */
1418 1412
1419void 1413void
1420usbnet_attach(struct usbnet *un, 1414usbnet_attach(struct usbnet *un,
1421 const char *detname) /* detach cv name */ 1415 const char *detname) /* detach cv name */
1422{ 1416{
1423 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1417 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1424 1418
1425 /* Required inputs. */ 1419 /* Required inputs. */
1426 KASSERT(un->un_ops->uno_tx_prepare); 1420 KASSERT(un->un_ops->uno_tx_prepare);
1427 KASSERT(un->un_ops->uno_rx_loop); 1421 KASSERT(un->un_ops->uno_rx_loop);
1428 KASSERT(un->un_ops->uno_init); 1422 KASSERT(un->un_ops->uno_init);
1429 KASSERT(un->un_rx_bufsz); 1423 KASSERT(un->un_rx_bufsz);
1430 KASSERT(un->un_tx_bufsz); 1424 KASSERT(un->un_tx_bufsz);
1431 KASSERT(un->un_rx_list_cnt); 1425 KASSERT(un->un_rx_list_cnt);
1432 KASSERT(un->un_tx_list_cnt); 1426 KASSERT(un->un_tx_list_cnt);
1433 1427
1434 /* Unfortunate fact. */ 1428 /* Unfortunate fact. */
1435 KASSERT(un == device_private(un->un_dev)); 1429 KASSERT(un == device_private(un->un_dev));
1436 1430
1437 un->un_pri = kmem_zalloc(sizeof(*un->un_pri), KM_SLEEP); 1431 un->un_pri = kmem_zalloc(sizeof(*un->un_pri), KM_SLEEP);
1438 struct usbnet_private * const unp = un->un_pri; 1432 struct usbnet_private * const unp = un->un_pri;
1439 1433
1440 usb_init_task(&unp->unp_mcasttask, usbnet_mcast_task, un, 1434 usb_init_task(&unp->unp_mcasttask, usbnet_mcast_task, un,
1441 USB_TASKQ_MPSAFE); 1435 USB_TASKQ_MPSAFE);
1442 usb_init_task(&unp->unp_ticktask, usbnet_tick_task, un, 1436 usb_init_task(&unp->unp_ticktask, usbnet_tick_task, un,
1443 USB_TASKQ_MPSAFE); 1437 USB_TASKQ_MPSAFE);
1444 callout_init(&unp->unp_stat_ch, CALLOUT_MPSAFE); 1438 callout_init(&unp->unp_stat_ch, CALLOUT_MPSAFE);
1445 callout_setfunc(&unp->unp_stat_ch, usbnet_tick, un); 1439 callout_setfunc(&unp->unp_stat_ch, usbnet_tick, un);
1446 1440
1447 mutex_init(&unp->unp_txlock, MUTEX_DEFAULT, IPL_SOFTUSB); 1441 mutex_init(&unp->unp_txlock, MUTEX_DEFAULT, IPL_SOFTUSB);
1448 mutex_init(&unp->unp_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB); 1442 mutex_init(&unp->unp_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB);
1449 mutex_init(&unp->unp_core_lock, MUTEX_DEFAULT, IPL_NONE); 1443 mutex_init(&unp->unp_core_lock, MUTEX_DEFAULT, IPL_NONE);
1450 cv_init(&unp->unp_detachcv, detname); 1444 cv_init(&unp->unp_detachcv, detname);
1451 1445
1452 rnd_attach_source(&unp->unp_rndsrc, device_xname(un->un_dev), 1446 rnd_attach_source(&unp->unp_rndsrc, device_xname(un->un_dev),
1453 RND_TYPE_NET, RND_FLAG_DEFAULT); 1447 RND_TYPE_NET, RND_FLAG_DEFAULT);
1454 1448
1455 usbnet_rx_list_alloc(un); 1449 usbnet_rx_list_alloc(un);
1456 usbnet_tx_list_alloc(un); 1450 usbnet_tx_list_alloc(un);
1457 1451
1458 unp->unp_number = atomic_inc_uint_nv(&usbnet_number); 1452 unp->unp_number = atomic_inc_uint_nv(&usbnet_number);
1459 1453
1460 unp->unp_attached = true; 1454 unp->unp_attached = true;
1461} 1455}
1462 1456
1463static void 1457static void
1464usbnet_attach_mii(struct usbnet *un, const struct usbnet_mii *unm) 1458usbnet_attach_mii(struct usbnet *un, const struct usbnet_mii *unm)
1465{ 1459{
1466 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1460 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1467 struct usbnet_private * const unp = un->un_pri; 1461 struct usbnet_private * const unp = un->un_pri;
1468 struct mii_data * const mii = &unp->unp_mii; 1462 struct mii_data * const mii = &unp->unp_mii;
1469 struct ifnet * const ifp = usbnet_ifp(un); 1463 struct ifnet * const ifp = usbnet_ifp(un);
1470 1464
1471 KASSERT(un->un_ops->uno_read_reg); 1465 KASSERT(un->un_ops->uno_read_reg);
1472 KASSERT(un->un_ops->uno_write_reg); 1466 KASSERT(un->un_ops->uno_write_reg);
1473 KASSERT(un->un_ops->uno_statchg); 1467 KASSERT(un->un_ops->uno_statchg);
1474 1468
1475 mii->mii_ifp = ifp; 1469 mii->mii_ifp = ifp;
1476 mii->mii_readreg = usbnet_mii_readreg; 1470 mii->mii_readreg = usbnet_mii_readreg;
1477 mii->mii_writereg = usbnet_mii_writereg; 1471 mii->mii_writereg = usbnet_mii_writereg;
1478 mii->mii_statchg = usbnet_mii_statchg; 1472 mii->mii_statchg = usbnet_mii_statchg;
1479 mii->mii_flags = MIIF_AUTOTSLEEP; 1473 mii->mii_flags = MIIF_AUTOTSLEEP;
1480 1474
1481 usbnet_ec(un)->ec_mii = mii; 1475 usbnet_ec(un)->ec_mii = mii;
1482 ifmedia_init_with_lock(&mii->mii_media, 0, 1476 ifmedia_init_with_lock(&mii->mii_media, 0,
1483 usbnet_media_upd, ether_mediastatus, usbnet_mutex_core(un)); 1477 usbnet_media_upd, ether_mediastatus, usbnet_mutex_core(un));
1484 mii_attach(un->un_dev, mii, unm->un_mii_capmask, unm->un_mii_phyloc, 1478 mii_attach(un->un_dev, mii, unm->un_mii_capmask, unm->un_mii_phyloc,
1485 unm->un_mii_offset, unm->un_mii_flags); 1479 unm->un_mii_offset, unm->un_mii_flags);
1486 1480
1487 if (LIST_FIRST(&mii->mii_phys) == NULL) { 1481 if (LIST_FIRST(&mii->mii_phys) == NULL) {
1488 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); 1482 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
1489 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); 1483 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
1490 } else 1484 } else
1491 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); 1485 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
1492} 1486}
1493 1487
1494void 1488void
1495usbnet_attach_ifp(struct usbnet *un, 1489usbnet_attach_ifp(struct usbnet *un,
1496 unsigned if_flags, /* additional if_flags */ 1490 unsigned if_flags, /* additional if_flags */
1497 unsigned if_extflags, /* additional if_extflags */ 1491 unsigned if_extflags, /* additional if_extflags */
1498 const struct usbnet_mii *unm) /* additional mii_attach flags */ 1492 const struct usbnet_mii *unm) /* additional mii_attach flags */
1499{ 1493{
1500 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1494 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1501 struct usbnet_private * const unp = un->un_pri; 1495 struct usbnet_private * const unp = un->un_pri;
1502 struct ifnet * const ifp = usbnet_ifp(un); 1496 struct ifnet * const ifp = usbnet_ifp(un);
1503 1497
1504 KASSERT(unp->unp_attached); 1498 KASSERT(unp->unp_attached);
1505 KASSERT(!unp->unp_ifp_attached); 1499 KASSERT(!unp->unp_ifp_attached);
1506 1500
1507 ifp->if_softc = un; 1501 ifp->if_softc = un;
1508 strlcpy(ifp->if_xname, device_xname(un->un_dev), IFNAMSIZ); 1502 strlcpy(ifp->if_xname, device_xname(un->un_dev), IFNAMSIZ);
1509 ifp->if_flags = if_flags; 1503 ifp->if_flags = if_flags;
1510 ifp->if_extflags = IFEF_MPSAFE | if_extflags; 1504 ifp->if_extflags = IFEF_MPSAFE | if_extflags;
1511 ifp->if_ioctl = usbnet_if_ioctl; 1505 ifp->if_ioctl = usbnet_if_ioctl;
1512 ifp->if_start = usbnet_if_start; 1506 ifp->if_start = usbnet_if_start;
1513 ifp->if_init = usbnet_if_init; 1507 ifp->if_init = usbnet_if_init;
1514 ifp->if_stop = usbnet_if_stop; 1508 ifp->if_stop = usbnet_if_stop;
1515 1509
1516 if (unm) 1510 if (unm)
1517 usbnet_attach_mii(un, unm); 1511 usbnet_attach_mii(un, unm);
1518 else 1512 else
1519 unp->unp_link = true; 1513 unp->unp_link = true;
1520 1514
1521 /* Attach the interface. */ 1515 /* Attach the interface. */
1522 if_initialize(ifp); 1516 if_initialize(ifp);
1523 if (ifp->_if_input == NULL) 1517 if (ifp->_if_input == NULL)
1524 ifp->if_percpuq = if_percpuq_create(ifp); 1518 ifp->if_percpuq = if_percpuq_create(ifp);
1525 if_register(ifp); 1519 if_register(ifp);
1526 unp->unp_ifp_attached = true; 1520 unp->unp_ifp_attached = true;
1527 1521
1528 /* 1522 /*
1529 * If ethernet address is all zero, skip ether_ifattach() and 1523 * If ethernet address is all zero, skip ether_ifattach() and
1530 * instead attach bpf here.. 1524 * instead attach bpf here..
1531 */ 1525 */
1532 if (!usbnet_empty_eaddr(un)) { 1526 if (!usbnet_empty_eaddr(un)) {
1533 ether_set_ifflags_cb(&unp->unp_ec, usbnet_ifflags_cb); 1527 ether_set_ifflags_cb(&unp->unp_ec, usbnet_ifflags_cb);
1534 aprint_normal_dev(un->un_dev, "Ethernet address %s\n", 1528 aprint_normal_dev(un->un_dev, "Ethernet address %s\n",
1535 ether_sprintf(un->un_eaddr)); 1529 ether_sprintf(un->un_eaddr));
1536 ether_ifattach(ifp, un->un_eaddr); 1530 ether_ifattach(ifp, un->un_eaddr);
1537 } else { 1531 } else {
1538 if_alloc_sadl(ifp); 1532 if_alloc_sadl(ifp);
1539 bpf_attach(ifp, DLT_RAW, 0); 1533 bpf_attach(ifp, DLT_RAW, 0);
1540 } 1534 }
1541 1535
1542 /* Now ready, and attached. */ 1536 /* Now ready, and attached. */
1543 IFQ_SET_READY(&ifp->if_snd); 1537 IFQ_SET_READY(&ifp->if_snd);
1544 1538
1545 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, un->un_udev, un->un_dev); 1539 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, un->un_udev, un->un_dev);
1546 1540
1547 if (!pmf_device_register(un->un_dev, NULL, NULL)) 1541 if (!pmf_device_register(un->un_dev, NULL, NULL))
1548 aprint_error_dev(un->un_dev, "couldn't establish power handler\n"); 1542 aprint_error_dev(un->un_dev, "couldn't establish power handler\n");
1549} 1543}
1550 1544
1551int 1545int
1552usbnet_detach(device_t self, int flags) 1546usbnet_detach(device_t self, int flags)
1553{ 1547{
1554 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1548 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1555 struct usbnet * const un = device_private(self); 1549 struct usbnet * const un = device_private(self);
1556 struct usbnet_private * const unp = un->un_pri; 1550 struct usbnet_private * const unp = un->un_pri;
1557 1551
1558 /* Detached before attached finished, so just bail out. */ 1552 /* Detached before attached finished, so just bail out. */
1559 if (unp == NULL || !unp->unp_attached) 1553 if (unp == NULL || !unp->unp_attached)
1560 return 0; 1554 return 0;
1561 1555
1562 struct ifnet * const ifp = usbnet_ifp(un); 1556 struct ifnet * const ifp = usbnet_ifp(un);
1563 struct mii_data * const mii = usbnet_mii(un); 1557 struct mii_data * const mii = usbnet_mii(un);
1564 1558
1565 mutex_enter(&unp->unp_core_lock); 1559 mutex_enter(&unp->unp_core_lock);
1566 unp->unp_dying = true; 1560 unp->unp_dying = true;
1567 mutex_exit(&unp->unp_core_lock); 1561 mutex_exit(&unp->unp_core_lock);
1568 1562
1569 IFNET_LOCK(ifp); 1563 IFNET_LOCK(ifp);
1570 if (ifp->if_flags & IFF_RUNNING) { 1564 if (ifp->if_flags & IFF_RUNNING) {
1571 usbnet_if_stop(ifp, 1); 1565 usbnet_if_stop(ifp, 1);
1572 } 1566 }
1573 IFNET_UNLOCK(ifp); 1567 IFNET_UNLOCK(ifp);
1574 1568
1575 callout_halt(&unp->unp_stat_ch, NULL); 1569 callout_halt(&unp->unp_stat_ch, NULL);
1576 usb_rem_task_wait(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER, 1570 usb_rem_task_wait(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER,
1577 NULL); 1571 NULL);
1578 usb_rem_task_wait(un->un_udev, &unp->unp_mcasttask, USB_TASKQ_DRIVER, 1572 usb_rem_task_wait(un->un_udev, &unp->unp_mcasttask, USB_TASKQ_DRIVER,
1579 NULL); 1573 NULL);
1580 1574
1581 mutex_enter(&unp->unp_core_lock); 1575 mutex_enter(&unp->unp_core_lock);
1582 unp->unp_refcnt--; 1576 unp->unp_refcnt--;
1583 while (unp->unp_refcnt >= 0) { 1577 while (unp->unp_refcnt >= 0) {
1584 /* Wait for processes to go away */ 1578 /* Wait for processes to go away */
1585 cv_wait(&unp->unp_detachcv, &unp->unp_core_lock); 1579 cv_wait(&unp->unp_detachcv, &unp->unp_core_lock);
1586 } 1580 }
1587 mutex_exit(&unp->unp_core_lock); 1581 mutex_exit(&unp->unp_core_lock);
1588 1582
1589 usbnet_rx_list_free(un); 1583 usbnet_rx_list_free(un);
1590 usbnet_tx_list_free(un); 1584 usbnet_tx_list_free(un);
1591 1585
1592 callout_destroy(&unp->unp_stat_ch); 1586 callout_destroy(&unp->unp_stat_ch);
1593 rnd_detach_source(&unp->unp_rndsrc); 1587 rnd_detach_source(&unp->unp_rndsrc);
1594 1588
1595 if (mii) { 1589 if (mii) {
1596 mii_detach(mii, MII_PHY_ANY, MII_OFFSET_ANY); 1590 mii_detach(mii, MII_PHY_ANY, MII_OFFSET_ANY);
1597 ifmedia_fini(&mii->mii_media); 1591 ifmedia_fini(&mii->mii_media);
1598 } 1592 }
1599 if (unp->unp_ifp_attached) { 1593 if (unp->unp_ifp_attached) {
1600 if (!usbnet_empty_eaddr(un)) 1594 if (!usbnet_empty_eaddr(un))
1601 ether_ifdetach(ifp); 1595 ether_ifdetach(ifp);
1602 else 1596 else
1603 bpf_detach(ifp); 1597 bpf_detach(ifp);
1604 if_detach(ifp); 1598 if_detach(ifp);
1605 } 1599 }
1606 usbnet_ec(un)->ec_mii = NULL; 1600 usbnet_ec(un)->ec_mii = NULL;
1607 1601
1608 /* 1602 /*
1609 * We have already waited for the multicast task to complete. 1603 * We have already waited for the multicast task to complete.
1610 * Unfortunately, until if_detach, nothing has prevented it 1604 * Unfortunately, until if_detach, nothing has prevented it
1611 * from running again -- another thread might issue if_mcast_op 1605 * from running again -- another thread might issue if_mcast_op
1612 * between the time of our first usb_rem_task_wait and the time 1606 * between the time of our first usb_rem_task_wait and the time
1613 * we actually get around to if_detach. 1607 * we actually get around to if_detach.
1614 * 1608 *
1615 * Fortunately, the first usb_rem_task_wait ensures that if the 1609 * Fortunately, the first usb_rem_task_wait ensures that if the
1616 * task is scheduled again, it will witness our setting of 1610 * task is scheduled again, it will witness our setting of
1617 * unp_dying to true[*]. So after that point, if the task is 1611 * unp_dying to true[*]. So after that point, if the task is
1618 * scheduled again, it will decline to touch IFNET_LOCK and do 1612 * scheduled again, it will decline to touch IFNET_LOCK and do
1619 * nothing. But we still need to wait for it to complete. 1613 * nothing. But we still need to wait for it to complete.
1620 * 1614 *
1621 * It would be nice if we could write 1615 * It would be nice if we could write
1622 * 1616 *
1623 * if_pleasestopissuingmcastopsthanks(ifp); 1617 * if_pleasestopissuingmcastopsthanks(ifp);
1624 * usb_rem_task_wait(..., &unp->unp_mcasttask, ...); 1618 * usb_rem_task_wait(..., &unp->unp_mcasttask, ...);
1625 * if_detach(ifp); 1619 * if_detach(ifp);
1626 * 1620 *
1627 * and then we would need only one usb_rem_task_wait. 1621 * and then we would need only one usb_rem_task_wait.
1628 * 1622 *
1629 * Unfortunately, there is no such operation available in 1623 * Unfortunately, there is no such operation available in
1630 * sys/net at the moment, and it would require a bit of 1624 * sys/net at the moment, and it would require a bit of
1631 * coordination with if_mcast_op and doifioctl probably under a 1625 * coordination with if_mcast_op and doifioctl probably under a
1632 * new lock. So we'll use this kludge until that mechanism is 1626 * new lock. So we'll use this kludge until that mechanism is
1633 * invented. 1627 * invented.
1634 * 1628 *
1635 * [*] This is not exactly a documented property of the API, 1629 * [*] This is not exactly a documented property of the API,
1636 * but it is implied by the single lock in the task queue 1630 * but it is implied by the single lock in the task queue
1637 * serializing changes to the task state. 1631 * serializing changes to the task state.
1638 */ 1632 */
1639 usb_rem_task_wait(un->un_udev, &unp->unp_mcasttask, USB_TASKQ_DRIVER, 1633 usb_rem_task_wait(un->un_udev, &unp->unp_mcasttask, USB_TASKQ_DRIVER,
1640 NULL); 1634 NULL);
1641 1635
1642 cv_destroy(&unp->unp_detachcv); 1636 cv_destroy(&unp->unp_detachcv);
1643 mutex_destroy(&unp->unp_core_lock); 1637 mutex_destroy(&unp->unp_core_lock);
1644 mutex_destroy(&unp->unp_rxlock); 1638 mutex_destroy(&unp->unp_rxlock);
1645 mutex_destroy(&unp->unp_txlock); 1639 mutex_destroy(&unp->unp_txlock);
1646 1640
1647 pmf_device_deregister(un->un_dev); 1641 pmf_device_deregister(un->un_dev);
1648 1642
1649 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, un->un_udev, un->un_dev); 1643 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, un->un_udev, un->un_dev);
1650 1644
1651 kmem_free(unp, sizeof(*unp)); 1645 kmem_free(unp, sizeof(*unp));
1652 un->un_pri = NULL; 1646 un->un_pri = NULL;
1653 1647
1654 return 0; 1648 return 0;
1655} 1649}
1656 1650
1657int 1651int
1658usbnet_activate(device_t self, devact_t act) 1652usbnet_activate(device_t self, devact_t act)
1659{ 1653{
1660 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1654 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1661 struct usbnet * const un = device_private(self); 1655 struct usbnet * const un = device_private(self);
1662 struct usbnet_private * const unp = un->un_pri; 1656 struct usbnet_private * const unp = un->un_pri;
1663 struct ifnet * const ifp = usbnet_ifp(un); 1657 struct ifnet * const ifp = usbnet_ifp(un);
1664 1658
1665 switch (act) { 1659 switch (act) {
1666 case DVACT_DEACTIVATE: 1660 case DVACT_DEACTIVATE:
1667 if_deactivate(ifp); 1661 if_deactivate(ifp);
1668 1662
1669 mutex_enter(&unp->unp_core_lock); 1663 mutex_enter(&unp->unp_core_lock);
1670 unp->unp_dying = true; 1664 unp->unp_dying = true;
1671 mutex_exit(&unp->unp_core_lock); 1665 mutex_exit(&unp->unp_core_lock);
1672 1666
1673 mutex_enter(&unp->unp_rxlock); 1667 mutex_enter(&unp->unp_rxlock);
1674 mutex_enter(&unp->unp_txlock); 1668 mutex_enter(&unp->unp_txlock);
1675 unp->unp_stopping = true; 1669 unp->unp_stopping = true;
1676 mutex_exit(&unp->unp_txlock); 1670 mutex_exit(&unp->unp_txlock);
1677 mutex_exit(&unp->unp_rxlock); 1671 mutex_exit(&unp->unp_rxlock);
1678 1672
1679 return 0; 1673 return 0;
1680 default: 1674 default:
1681 return EOPNOTSUPP; 1675 return EOPNOTSUPP;
1682 } 1676 }
1683} 1677}
1684 1678
1685MODULE(MODULE_CLASS_MISC, usbnet, NULL); 1679MODULE(MODULE_CLASS_MISC, usbnet, NULL);
1686 1680
1687static int 1681static int
1688usbnet_modcmd(modcmd_t cmd, void *arg) 1682usbnet_modcmd(modcmd_t cmd, void *arg)
1689{ 1683{
1690 switch (cmd) { 1684 switch (cmd) {
1691 case MODULE_CMD_INIT: 1685 case MODULE_CMD_INIT:
1692 return 0; 1686 return 0;
1693 case MODULE_CMD_FINI: 1687 case MODULE_CMD_FINI:
1694 return 0; 1688 return 0;
1695 case MODULE_CMD_STAT: 1689 case MODULE_CMD_STAT:
1696 case MODULE_CMD_AUTOUNLOAD: 1690 case MODULE_CMD_AUTOUNLOAD:
1697 default: 1691 default:
1698 return ENOTTY; 1692 return ENOTTY;
1699 } 1693 }
1700} 1694}

cvs diff -r1.22 -r1.23 src/sys/dev/usb/usbnet.h (switch to unified diff)

--- src/sys/dev/usb/usbnet.h 2021/12/11 19:24:21 1.22
+++ src/sys/dev/usb/usbnet.h 2022/03/03 05:48:14 1.23
@@ -1,403 +1,402 @@ @@ -1,403 +1,402 @@
1/* $NetBSD: usbnet.h,v 1.22 2021/12/11 19:24:21 mrg Exp $ */ 1/* $NetBSD: usbnet.h,v 1.23 2022/03/03 05:48:14 riastradh Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2019 Matthew R. Green 4 * Copyright (c) 2019 Matthew R. Green
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE. 26 * SUCH DAMAGE.
27 */ 27 */
28 28
29#ifndef _DEV_USB_USBNET_H 29#ifndef _DEV_USB_USBNET_H
30#define _DEV_USB_USBNET_H 30#define _DEV_USB_USBNET_H
31 31
32/* 32/*
33 * Common code/data shared by all USB ethernet drivers (using these routines.) 33 * Common code/data shared by all USB ethernet drivers (using these routines.)
34 * 34 *
35 * This framework provides the following features for USB ethernet drivers: 35 * This framework provides the following features for USB ethernet drivers:
36 * 36 *
37 * - USB endpoint pipe handling 37 * - USB endpoint pipe handling
38 * - rx and tx chain handling 38 * - rx and tx chain handling
39 * - generic handlers or support for several struct ifnet callbacks 39 * - generic handlers or support for several struct ifnet callbacks
40 * - MII bus locking 40 * - MII bus locking
41 * - interrupt handling 41 * - interrupt handling
42 * - partial autoconf handling 42 * - partial autoconf handling
43 * 43 *
44 * Consumers of this interface need to: 44 * Consumers of this interface need to:
45 * 45 *
46 * - replace most softc members with "struct usbnet" usage, in particular 46 * - replace most softc members with "struct usbnet" usage, in particular
47 * use usbnet pointer for ifp->if_softc, and device_private (real softc 47 * use usbnet pointer for ifp->if_softc, and device_private (real softc
48 * can be stored in un_sc member) 48 * can be stored in un_sc member)
49 * - use MII bus lock / access methods 49 * - use MII bus lock / access methods
50 * - usbnet_attach() to initialise and allocate rx/tx chains 50 * - usbnet_attach() to initialise and allocate rx/tx chains
51 * - usbnet_attach_ifp() to attach the interface, and either ether_ifattach() 51 * - usbnet_attach_ifp() to attach the interface, and either ether_ifattach()
52 * for ethernet devices, or if_alloc_sadl()/bpf_attach() pair otherwise. 52 * for ethernet devices, or if_alloc_sadl()/bpf_attach() pair otherwise.
53 * - usbnet_detach() to clean them up 53 * - usbnet_detach() to clean them up
54 * - usbnet_activate() for autoconf 54 * - usbnet_activate() for autoconf
55 * - interface ioctl and start have direct frontends with callbacks for 55 * - interface ioctl and start have direct frontends with callbacks for
56 * device specific handling: 56 * device specific handling:
57 * - ioctl can use either a device-specific override (useful for special 57 * - ioctl can use either a device-specific override (useful for special
58 * cases), but provides a normal handler with callback to handle 58 * cases), but provides a normal handler with callback to handle
59 * ENETRESET conditions that should be sufficient for most users 59 * ENETRESET conditions that should be sufficient for most users
60 * - start uses usbnet transmit prepare callback (uno_tx_prepare) 60 * - start uses usbnet transmit prepare callback (uno_tx_prepare)
61 * - interface init and stop have helper functions 61 * - interface init and stop have helper functions
62 * - device specific init should use usbnet_init_rx_tx() to open pipes 62 * - device specific init should use usbnet_init_rx_tx() to open pipes
63 * to the device and setup the rx/tx chains for use after any device 63 * to the device and setup the rx/tx chains for use after any device
64 * specific setup 64 * specific setup
65 * - usbnet_stop() must be called with the un_lock held, and will 65 * - usbnet_stop() must be called with the un_lock held, and will
66 * call the device-specific usbnet stop callback, which enables the 66 * call the device-specific usbnet stop callback, which enables the
67 * standard init calls stop idiom. 67 * standard init calls stop idiom.
68 * - interrupt handling: 68 * - interrupt handling:
69 * - for rx, usbnet_init_rx_tx() will enable the receive pipes and 69 * - for rx, usbnet_init_rx_tx() will enable the receive pipes and
70 * call the rx_loop callback to handle device specific processing of 70 * call the rx_loop callback to handle device specific processing of
71 * packets, which can use usbnet_enqueue() to provide data to the 71 * packets, which can use usbnet_enqueue() to provide data to the
72 * higher layers 72 * higher layers
73 * - for tx, usbnet_start (if_start) will pull entries out of the 73 * - for tx, usbnet_start (if_start) will pull entries out of the
74 * transmit queue and use the transmit prepare callback (uno_tx_prepare) 74 * transmit queue and use the transmit prepare callback (uno_tx_prepare)
75 * for the given mbuf. the usb callback will use usbnet_txeof() for 75 * for the given mbuf. the usb callback will use usbnet_txeof() for
76 * the transmit completion function (internal to usbnet) 76 * the transmit completion function (internal to usbnet)
77 * - there is special interrupt pipe handling 77 * - there is special interrupt pipe handling
78 * - timer/tick: 78 * - timer/tick:
79 * - the uno_tick callback will be called once a second if present. 79 * - the uno_tick callback will be called once a second if present.
80 */ 80 */
81 81
82#include <sys/device.h> 82#include <sys/device.h>
83#include <sys/mbuf.h> 83#include <sys/mbuf.h>
84#include <sys/rndsource.h> 84#include <sys/rndsource.h>
85#include <sys/mutex.h> 85#include <sys/mutex.h>
86#include <sys/module.h> 86#include <sys/module.h>
87 87
88#include <net/bpf.h> 88#include <net/bpf.h>
89#include <net/if.h> 89#include <net/if.h>
90#include <net/if_arp.h> 90#include <net/if_arp.h>
91#include <net/if_dl.h> 91#include <net/if_dl.h>
92#include <net/if_ether.h> 92#include <net/if_ether.h>
93#include <net/if_media.h> 93#include <net/if_media.h>
94 94
95#include <dev/mii/mii.h> 95#include <dev/mii/mii.h>
96#include <dev/mii/miivar.h> 96#include <dev/mii/miivar.h>
97 97
98#include <dev/usb/usb.h> 98#include <dev/usb/usb.h>
99#include <dev/usb/usbdi.h> 99#include <dev/usb/usbdi.h>
100#include <dev/usb/usbdivar.h> 100#include <dev/usb/usbdivar.h>
101#include <dev/usb/usbdi_util.h> 101#include <dev/usb/usbdi_util.h>
102#include <dev/usb/usbdevs.h> 102#include <dev/usb/usbdevs.h>
103 103
104/* 104/*
105 * Per-transfer data. 105 * Per-transfer data.
106 * 106 *
107 * Front-end must set un_rx_list_cnt and un_tx_list_cnt before 107 * Front-end must set un_rx_list_cnt and un_tx_list_cnt before
108 * calling usbnet_attach(), and then call usbnet_rx_tx_init() 108 * calling usbnet_attach(), and then call usbnet_rx_tx_init()
109 * which will allocate the chain arrays, and must be NULL to 109 * which will allocate the chain arrays, and must be NULL to
110 * indicate the first call. 110 * indicate the first call.
111 */ 111 */
112struct usbnet; 112struct usbnet;
113struct usbnet_chain { 113struct usbnet_chain {
114 struct usbnet *unc_un; 114 struct usbnet *unc_un;
115 struct usbd_xfer *unc_xfer; 115 struct usbd_xfer *unc_xfer;
116 uint8_t *unc_buf; 116 uint8_t *unc_buf;
117}; 117};
118 118
119/* 119/*
120 * Extend this as necessary. axe(4) claims to want 6 total, but 120 * Extend this as necessary. axe(4) claims to want 6 total, but
121 * does not implement them. 121 * does not implement them.
122 */ 122 */
123enum usbnet_ep { 123enum usbnet_ep {
124 USBNET_ENDPT_RX, 124 USBNET_ENDPT_RX,
125 USBNET_ENDPT_TX, 125 USBNET_ENDPT_TX,
126 USBNET_ENDPT_INTR, 126 USBNET_ENDPT_INTR,
127 USBNET_ENDPT_MAX, 127 USBNET_ENDPT_MAX,
128}; 128};
129 129
130/* Interface stop callback. */ 130/* Interface stop callback. */
131typedef void (*usbnet_stop_cb)(struct ifnet *, int); 131typedef void (*usbnet_stop_cb)(struct ifnet *, int);
132/* Interface ioctl callback. */ 132/* Interface ioctl callback. */
133typedef int (*usbnet_ioctl_cb)(struct ifnet *, u_long, void *); 133typedef int (*usbnet_ioctl_cb)(struct ifnet *, u_long, void *);
134/* Initialise device callback. */ 134/* Initialise device callback. */
135typedef int (*usbnet_init_cb)(struct ifnet *); 135typedef int (*usbnet_init_cb)(struct ifnet *);
136 136
137/* MII read register callback. */ 137/* MII read register callback. */
138typedef int (*usbnet_mii_read_reg_cb)(struct usbnet *, int reg, 138typedef int (*usbnet_mii_read_reg_cb)(struct usbnet *, int reg,
139 int phy, uint16_t *val); 139 int phy, uint16_t *val);
140/* MII write register callback. */ 140/* MII write register callback. */
141typedef int (*usbnet_mii_write_reg_cb)(struct usbnet *, int reg, 141typedef int (*usbnet_mii_write_reg_cb)(struct usbnet *, int reg,
142 int phy, uint16_t val); 142 int phy, uint16_t val);
143/* MII status change callback. */ 143/* MII status change callback. */
144typedef void (*usbnet_mii_statchg_cb)(struct ifnet *); 144typedef void (*usbnet_mii_statchg_cb)(struct ifnet *);
145 145
146/* Prepare packet to send callback, returns length. */ 146/* Prepare packet to send callback, returns length. */
147typedef unsigned (*usbnet_tx_prepare_cb)(struct usbnet *, struct mbuf *, 147typedef unsigned (*usbnet_tx_prepare_cb)(struct usbnet *, struct mbuf *,
148 struct usbnet_chain *); 148 struct usbnet_chain *);
149/* Receive some packets callback. */ 149/* Receive some packets callback. */
150typedef void (*usbnet_rx_loop_cb)(struct usbnet *, struct usbnet_chain *, 150typedef void (*usbnet_rx_loop_cb)(struct usbnet *, struct usbnet_chain *,
151 uint32_t); 151 uint32_t);
152/* Tick callback. */ 152/* Tick callback. */
153typedef void (*usbnet_tick_cb)(struct usbnet *); 153typedef void (*usbnet_tick_cb)(struct usbnet *);
154/* Interrupt pipe callback. */ 154/* Interrupt pipe callback. */
155typedef void (*usbnet_intr_cb)(struct usbnet *, usbd_status); 155typedef void (*usbnet_intr_cb)(struct usbnet *, usbd_status);
156 156
157/* 157/*
158 * LOCKING 158 * LOCKING
159 * ======= 159 * =======
160 * 160 *
161 * The following annotations indicate which locks are held when 161 * The following annotations indicate which locks are held when
162 * usbnet_ops functions are invoked: 162 * usbnet_ops functions are invoked:
163 * 163 *
164 * I -> IFNET_LOCK (if_ioctl_lock) 164 * I -> IFNET_LOCK (if_ioctl_lock)
165 * C -> CORE_LOCK (usbnet core_lock) 165 * C -> CORE_LOCK (usbnet core_lock)
166 * T -> TX_LOCK (usbnet tx_lock) 166 * T -> TX_LOCK (usbnet tx_lock)
167 * R -> RX_LOCK (usbnet rx_lock) 167 * R -> RX_LOCK (usbnet rx_lock)
168 * n -> no locks held 168 * n -> no locks held
169 * 169 *
170 * Note that when CORE_LOCK is held, IFNET_LOCK may or may not also 170 * Note that when CORE_LOCK is held, IFNET_LOCK may or may not also
171 * be held. 171 * be held.
172 * 172 *
173 * Note that the IFNET_LOCK **may not be held** for some ioctl 173 * Note that the IFNET_LOCK **may not be held** for some ioctl
174 * operations (add/delete multicast addresses, for example). 174 * operations (add/delete multicast addresses, for example).
175 * 175 *
176 * Busy reference counts are maintained across calls to: uno_stop, 176 * Busy reference counts are maintained across calls to: uno_stop,
177 * uno_read_reg, uno_write_reg, uno_statchg, and uno_tick. 177 * uno_read_reg, uno_write_reg, uno_statchg, and uno_tick.
178 */ 178 */
179struct usbnet_ops { 179struct usbnet_ops {
180 usbnet_stop_cb uno_stop; /* C */ 180 usbnet_stop_cb uno_stop; /* C */
181 usbnet_ioctl_cb uno_ioctl; /* I (maybe) */ 181 usbnet_ioctl_cb uno_ioctl; /* I (maybe) */
182 usbnet_ioctl_cb uno_override_ioctl; /* I (maybe) */ 182 usbnet_ioctl_cb uno_override_ioctl; /* I (maybe) */
183 usbnet_init_cb uno_init; /* I */ 183 usbnet_init_cb uno_init; /* I */
184 usbnet_mii_read_reg_cb uno_read_reg; /* C */ 184 usbnet_mii_read_reg_cb uno_read_reg; /* C */
185 usbnet_mii_write_reg_cb uno_write_reg; /* C */ 185 usbnet_mii_write_reg_cb uno_write_reg; /* C */
186 usbnet_mii_statchg_cb uno_statchg; /* C */ 186 usbnet_mii_statchg_cb uno_statchg; /* C */
187 usbnet_tx_prepare_cb uno_tx_prepare; /* T */ 187 usbnet_tx_prepare_cb uno_tx_prepare; /* T */
188 usbnet_rx_loop_cb uno_rx_loop; /* R */ 188 usbnet_rx_loop_cb uno_rx_loop; /* R */
189 usbnet_tick_cb uno_tick; /* n */ 189 usbnet_tick_cb uno_tick; /* n */
190 usbnet_intr_cb uno_intr; /* n */ 190 usbnet_intr_cb uno_intr; /* n */
191}; 191};
192 192
193/* 193/*
194 * USB interrupt pipe support. Use this if usbd_open_pipe_intr() should 194 * USB interrupt pipe support. Use this if usbd_open_pipe_intr() should
195 * be used for the interrupt pipe. 195 * be used for the interrupt pipe.
196 */ 196 */
197struct usbnet_intr { 197struct usbnet_intr {
198 /* 198 /*
199 * Point un_intr to this structure to use usbd_open_pipe_intr() not 199 * Point un_intr to this structure to use usbd_open_pipe_intr() not
200 * usbd_open_pipe() for USBNET_ENDPT_INTR, with this buffer, size, 200 * usbd_open_pipe() for USBNET_ENDPT_INTR, with this buffer, size,
201 * and interval. 201 * and interval.
202 */ 202 */
203 void *uni_buf; 203 void *uni_buf;
204 unsigned uni_bufsz; 204 unsigned uni_bufsz;
205 unsigned uni_interval; 205 unsigned uni_interval;
206}; 206};
207 207
208/* 208/*
209 * Structure to setup MII. Use the USBNET_MII_DECL_DEFAULT() macro for 209 * Structure to setup MII. Use the USBNET_MII_DECL_DEFAULT() macro for
210 * sane default. Pass a copy to usbnet_attach_ifp(). Not used 210 * sane default. Pass a copy to usbnet_attach_ifp(). Not used
211 * after the usbnet_attach_ifp() function returns. 211 * after the usbnet_attach_ifp() function returns.
212 */ 212 */
213struct usbnet_mii { 213struct usbnet_mii {
214 int un_mii_flags; 214 int un_mii_flags;
215 int un_mii_capmask; 215 int un_mii_capmask;
216 int un_mii_phyloc; 216 int un_mii_phyloc;
217 int un_mii_offset; 217 int un_mii_offset;
218}; 218};
219 219
220#define USBNET_MII_DECL(name, capmask, loc, off, flags) \ 220#define USBNET_MII_DECL(name, capmask, loc, off, flags) \
221 struct usbnet_mii name = { \ 221 struct usbnet_mii name = { \
222 .un_mii_capmask = capmask, \ 222 .un_mii_capmask = capmask, \
223 .un_mii_phyloc = loc, \ 223 .un_mii_phyloc = loc, \
224 .un_mii_offset = off, \ 224 .un_mii_offset = off, \
225 .un_mii_flags = flags, \ 225 .un_mii_flags = flags, \
226 } 226 }
227#define USBNET_MII_DECL_DEFAULT(name) \ 227#define USBNET_MII_DECL_DEFAULT(name) \
228 USBNET_MII_DECL(name, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0) 228 USBNET_MII_DECL(name, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0)
229 229
230/* 230/*
231 * Generic USB ethernet structure. Use this as ifp->if_softc and set as 231 * Generic USB ethernet structure. Use this as ifp->if_softc and set as
232 * device_private() in attach unless already using struct usbnet here. 232 * device_private() in attach unless already using struct usbnet here.
233 * 233 *
234 * Devices without MII should call usbnet_attach_ifp() with have_mii set 234 * Devices without MII should call usbnet_attach_ifp() with have_mii set
235 * to true, and should ensure that the un_statchg_cb callback sets the 235 * to true, and should ensure that the un_statchg_cb callback sets the
236 * un_link member. Devices without MII have this forced to true. 236 * un_link member. Devices without MII have this forced to true.
237 */ 237 */
238struct usbnet_private; 238struct usbnet_private;
239struct usbnet { 239struct usbnet {
240 /* 240 /*
241 * This section should be filled in before calling 241 * This section should be filled in before calling
242 * usbnet_attach(). 242 * usbnet_attach().
243 */ 243 */
244 void *un_sc; /* real softc */ 244 void *un_sc; /* real softc */
245 device_t un_dev; 245 device_t un_dev;
246 struct usbd_interface *un_iface; 246 struct usbd_interface *un_iface;
247 struct usbd_device *un_udev; 247 struct usbd_device *un_udev;
248 const struct usbnet_ops *un_ops; 248 const struct usbnet_ops *un_ops;
249 struct usbnet_intr *un_intr; 249 struct usbnet_intr *un_intr;
250 250
251 /* Inputs for rx/tx chain control. */ 251 /* Inputs for rx/tx chain control. */
252 unsigned un_rx_bufsz; 252 unsigned un_rx_bufsz;
253 unsigned un_tx_bufsz; 253 unsigned un_tx_bufsz;
254 unsigned un_rx_list_cnt; 254 unsigned un_rx_list_cnt;
255 unsigned un_tx_list_cnt; 255 unsigned un_tx_list_cnt;
256 int un_rx_xfer_flags; 256 int un_rx_xfer_flags;
257 int un_tx_xfer_flags; 257 int un_tx_xfer_flags;
258 258
259 /* 259 /*
260 * This section should be filled in before calling 260 * This section should be filled in before calling
261 * usbnet_attach_ifp(). 261 * usbnet_attach_ifp().
262 * 262 *
263 * XXX This should be of type "uByte". enum usbnet_ep 263 * XXX This should be of type "uByte". enum usbnet_ep
264 * is the index. Fix this in a kernel version bump. 264 * is the index. Fix this in a kernel version bump.
265 */ 265 */
266 enum usbnet_ep un_ed[USBNET_ENDPT_MAX]; 266 enum usbnet_ep un_ed[USBNET_ENDPT_MAX];
267 267
268 /* MII specific. Not used without MII. */ 268 /* MII specific. Not used without MII. */
269 int un_phyno; 269 int un_phyno;
270 /* Ethernet specific. All zeroes indicates non-Ethernet. */ 270 /* Ethernet specific. All zeroes indicates non-Ethernet. */
271 uint8_t un_eaddr[ETHER_ADDR_LEN]; 271 uint8_t un_eaddr[ETHER_ADDR_LEN];
272 272
273 /* 273 /*
274 * This section is for driver to use, not touched by usbnet. 274 * This section is for driver to use, not touched by usbnet.
275 */ 275 */
276 unsigned un_flags; 276 unsigned un_flags;
277 277
278 /* 278 /*
279 * This section is private to usbnet. Don't touch. 279 * This section is private to usbnet. Don't touch.
280 */ 280 */
281 struct usbnet_private *un_pri; 281 struct usbnet_private *un_pri;
282}; 282};
283 283
284/* Various accessors. */ 284/* Various accessors. */
285 285
286void usbnet_set_link(struct usbnet *, bool); 286void usbnet_set_link(struct usbnet *, bool);
287void usbnet_set_dying(struct usbnet *, bool); 
288 287
289struct ifnet *usbnet_ifp(struct usbnet *); 288struct ifnet *usbnet_ifp(struct usbnet *);
290struct ethercom *usbnet_ec(struct usbnet *); 289struct ethercom *usbnet_ec(struct usbnet *);
291struct mii_data *usbnet_mii(struct usbnet *); 290struct mii_data *usbnet_mii(struct usbnet *);
292krndsource_t *usbnet_rndsrc(struct usbnet *); 291krndsource_t *usbnet_rndsrc(struct usbnet *);
293void *usbnet_softc(struct usbnet *); 292void *usbnet_softc(struct usbnet *);
294 293
295bool usbnet_havelink(struct usbnet *); 294bool usbnet_havelink(struct usbnet *);
296bool usbnet_isdying(struct usbnet *); 295bool usbnet_isdying(struct usbnet *);
297 296
298 297
299/* 298/*
300 * Locking. Note that the isowned() are implemented here so that 299 * Locking. Note that the isowned() are implemented here so that
301 * empty-KASSERT() causes them to be elided for non-DIAG builds. 300 * empty-KASSERT() causes them to be elided for non-DIAG builds.
302 */ 301 */
303void usbnet_lock_core(struct usbnet *); 302void usbnet_lock_core(struct usbnet *);
304void usbnet_unlock_core(struct usbnet *); 303void usbnet_unlock_core(struct usbnet *);
305kmutex_t *usbnet_mutex_core(struct usbnet *); 304kmutex_t *usbnet_mutex_core(struct usbnet *);
306static __inline__ void 305static __inline__ void
307usbnet_isowned_core(struct usbnet *un) 306usbnet_isowned_core(struct usbnet *un)
308{ 307{
309 KASSERT(mutex_owned(usbnet_mutex_core(un))); 308 KASSERT(mutex_owned(usbnet_mutex_core(un)));
310} 309}
311 310
312void usbnet_busy(struct usbnet *); 311void usbnet_busy(struct usbnet *);
313void usbnet_unbusy(struct usbnet *); 312void usbnet_unbusy(struct usbnet *);
314 313
315void usbnet_lock_rx(struct usbnet *); 314void usbnet_lock_rx(struct usbnet *);
316void usbnet_unlock_rx(struct usbnet *); 315void usbnet_unlock_rx(struct usbnet *);
317kmutex_t *usbnet_mutex_rx(struct usbnet *); 316kmutex_t *usbnet_mutex_rx(struct usbnet *);
318static __inline__ void 317static __inline__ void
319usbnet_isowned_rx(struct usbnet *un) 318usbnet_isowned_rx(struct usbnet *un)
320{ 319{
321 KASSERT(mutex_owned(usbnet_mutex_rx(un))); 320 KASSERT(mutex_owned(usbnet_mutex_rx(un)));
322} 321}
323 322
324void usbnet_lock_tx(struct usbnet *); 323void usbnet_lock_tx(struct usbnet *);
325void usbnet_unlock_tx(struct usbnet *); 324void usbnet_unlock_tx(struct usbnet *);
326kmutex_t *usbnet_mutex_tx(struct usbnet *); 325kmutex_t *usbnet_mutex_tx(struct usbnet *);
327static __inline__ void 326static __inline__ void
328usbnet_isowned_tx(struct usbnet *un) 327usbnet_isowned_tx(struct usbnet *un)
329{ 328{
330 KASSERT(mutex_owned(usbnet_mutex_tx(un))); 329 KASSERT(mutex_owned(usbnet_mutex_tx(un)));
331} 330}
332 331
333/* 332/*
334 * Endpoint / rx/tx chain management: 333 * Endpoint / rx/tx chain management:
335 * 334 *
336 * usbnet_attach() initialises usbnet and allocates rx and tx chains 335 * usbnet_attach() initialises usbnet and allocates rx and tx chains
337 * usbnet_init_rx_tx() open pipes, initialises the rx/tx chains for use 336 * usbnet_init_rx_tx() open pipes, initialises the rx/tx chains for use
338 * usbnet_stop() stops pipes, cleans (not frees) rx/tx chains, locked 337 * usbnet_stop() stops pipes, cleans (not frees) rx/tx chains, locked
339 * version assumes un_lock is held 338 * version assumes un_lock is held
340 * usbnet_detach() frees the rx/tx chains 339 * usbnet_detach() frees the rx/tx chains
341 * 340 *
342 * Setup un_ed[] with valid end points before calling usbnet_attach(). 341 * Setup un_ed[] with valid end points before calling usbnet_attach().
343 * Call usbnet_init_rx_tx() to initialise pipes, which will be open 342 * Call usbnet_init_rx_tx() to initialise pipes, which will be open
344 * upon success. 343 * upon success.
345 */ 344 */
346int usbnet_init_rx_tx(struct usbnet * const); 345int usbnet_init_rx_tx(struct usbnet * const);
347 346
348/* MII. */ 347/* MII. */
349int usbnet_mii_readreg(device_t, int, int, uint16_t *); 348int usbnet_mii_readreg(device_t, int, int, uint16_t *);
350int usbnet_mii_writereg(device_t, int, int, uint16_t); 349int usbnet_mii_writereg(device_t, int, int, uint16_t);
351void usbnet_mii_statchg(struct ifnet *); 350void usbnet_mii_statchg(struct ifnet *);
352 351
353/* interrupt handling */ 352/* interrupt handling */
354void usbnet_enqueue(struct usbnet * const, uint8_t *, size_t, int, 353void usbnet_enqueue(struct usbnet * const, uint8_t *, size_t, int,
355 uint32_t, int); 354 uint32_t, int);
356void usbnet_input(struct usbnet * const, uint8_t *, size_t); 355void usbnet_input(struct usbnet * const, uint8_t *, size_t);
357 356
358/* autoconf */ 357/* autoconf */
359void usbnet_attach(struct usbnet *un, const char *); 358void usbnet_attach(struct usbnet *un, const char *);
360void usbnet_attach_ifp(struct usbnet *, unsigned, unsigned, 359void usbnet_attach_ifp(struct usbnet *, unsigned, unsigned,
361 const struct usbnet_mii *); 360 const struct usbnet_mii *);
362int usbnet_detach(device_t, int); 361int usbnet_detach(device_t, int);
363int usbnet_activate(device_t, devact_t); 362int usbnet_activate(device_t, devact_t);
364 363
365/* stop backend */ 364/* stop backend */
366void usbnet_stop(struct usbnet *, struct ifnet *, int); 365void usbnet_stop(struct usbnet *, struct ifnet *, int);
367 366
368/* module hook up */ 367/* module hook up */
369 368
370#ifdef _MODULE 369#ifdef _MODULE
371#define USBNET_INIT(name) \ 370#define USBNET_INIT(name) \
372 error = config_init_component(cfdriver_ioconf_##name, \ 371 error = config_init_component(cfdriver_ioconf_##name, \
373 cfattach_ioconf_##name, cfdata_ioconf_##name); 372 cfattach_ioconf_##name, cfdata_ioconf_##name);
374#define USBNET_FINI(name) \ 373#define USBNET_FINI(name) \
375 error = config_fini_component(cfdriver_ioconf_##name, \ 374 error = config_fini_component(cfdriver_ioconf_##name, \
376 cfattach_ioconf_##name, cfdata_ioconf_##name); 375 cfattach_ioconf_##name, cfdata_ioconf_##name);
377#else 376#else
378#define USBNET_INIT(name) 377#define USBNET_INIT(name)
379#define USBNET_FINI(name) 378#define USBNET_FINI(name)
380#endif 379#endif
381 380
382#define USBNET_MODULE(name) \ 381#define USBNET_MODULE(name) \
383 \ 382 \
384MODULE(MODULE_CLASS_DRIVER, if_##name, "usbnet"); \ 383MODULE(MODULE_CLASS_DRIVER, if_##name, "usbnet"); \
385 \ 384 \
386static int \ 385static int \
387if_##name##_modcmd(modcmd_t cmd, void *aux) \ 386if_##name##_modcmd(modcmd_t cmd, void *aux) \
388{ \ 387{ \
389 int error = 0; \ 388 int error = 0; \
390 \ 389 \
391 switch (cmd) { \ 390 switch (cmd) { \
392 case MODULE_CMD_INIT: \ 391 case MODULE_CMD_INIT: \
393 USBNET_INIT(name) \ 392 USBNET_INIT(name) \
394 return error; \ 393 return error; \
395 case MODULE_CMD_FINI: \ 394 case MODULE_CMD_FINI: \
396 USBNET_FINI(name) \ 395 USBNET_FINI(name) \
397 return error; \ 396 return error; \
398 default: \ 397 default: \
399 return ENOTTY; \ 398 return ENOTTY; \
400 } \ 399 } \
401} 400}
402 401
403#endif /* _DEV_USB_USBNET_H */ 402#endif /* _DEV_USB_USBNET_H */