if_ure.c revision 1.8.2.2 1 1.8.2.2 christos /* $NetBSD: if_ure.c,v 1.8.2.2 2019/06/10 22:07:34 christos Exp $ */
2 1.8.2.2 christos /* $OpenBSD: if_ure.c,v 1.10 2018/11/02 21:32:30 jcs Exp $ */
3 1.8.2.2 christos /*-
4 1.8.2.2 christos * Copyright (c) 2015-2016 Kevin Lo <kevlo (at) FreeBSD.org>
5 1.8.2.2 christos * All rights reserved.
6 1.8.2.2 christos *
7 1.8.2.2 christos * Redistribution and use in source and binary forms, with or without
8 1.8.2.2 christos * modification, are permitted provided that the following conditions
9 1.8.2.2 christos * are met:
10 1.8.2.2 christos * 1. Redistributions of source code must retain the above copyright
11 1.8.2.2 christos * notice, this list of conditions and the following disclaimer.
12 1.8.2.2 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.8.2.2 christos * notice, this list of conditions and the following disclaimer in the
14 1.8.2.2 christos * documentation and/or other materials provided with the distribution.
15 1.8.2.2 christos *
16 1.8.2.2 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.8.2.2 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.8.2.2 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.8.2.2 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.8.2.2 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.8.2.2 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.8.2.2 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.8.2.2 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.8.2.2 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.8.2.2 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.8.2.2 christos * SUCH DAMAGE.
27 1.8.2.2 christos */
28 1.8.2.2 christos
29 1.8.2.2 christos /* RealTek RTL8152/RTL8153 10/100/Gigabit USB Ethernet device */
30 1.8.2.2 christos
31 1.8.2.2 christos #include <sys/cdefs.h>
32 1.8.2.2 christos __KERNEL_RCSID(0, "$NetBSD: if_ure.c,v 1.8.2.2 2019/06/10 22:07:34 christos Exp $");
33 1.8.2.2 christos
34 1.8.2.2 christos #ifdef _KERNEL_OPT
35 1.8.2.2 christos #include "opt_usb.h"
36 1.8.2.2 christos #include "opt_inet.h"
37 1.8.2.2 christos #endif
38 1.8.2.2 christos
39 1.8.2.2 christos #include <sys/param.h>
40 1.8.2.2 christos #include <sys/bus.h>
41 1.8.2.2 christos #include <sys/systm.h>
42 1.8.2.2 christos #include <sys/sockio.h>
43 1.8.2.2 christos #include <sys/mbuf.h>
44 1.8.2.2 christos #include <sys/mutex.h>
45 1.8.2.2 christos #include <sys/kernel.h>
46 1.8.2.2 christos #include <sys/socket.h>
47 1.8.2.2 christos #include <sys/device.h>
48 1.8.2.2 christos
49 1.8.2.2 christos #include <sys/rndsource.h>
50 1.8.2.2 christos
51 1.8.2.2 christos #include <net/if.h>
52 1.8.2.2 christos #include <net/if_dl.h>
53 1.8.2.2 christos #include <net/if_ether.h>
54 1.8.2.2 christos #include <net/if_media.h>
55 1.8.2.2 christos
56 1.8.2.2 christos #include <net/bpf.h>
57 1.8.2.2 christos
58 1.8.2.2 christos #include <netinet/in.h>
59 1.8.2.2 christos
60 1.8.2.2 christos #include <netinet/in_offload.h> /* XXX for in_undefer_cksum() */
61 1.8.2.2 christos #ifdef INET6
62 1.8.2.2 christos #include <netinet6/in6_offload.h> /* XXX for in6_undefer_cksum() */
63 1.8.2.2 christos #endif
64 1.8.2.2 christos
65 1.8.2.2 christos #include <dev/mii/mii.h>
66 1.8.2.2 christos #include <dev/mii/miivar.h>
67 1.8.2.2 christos
68 1.8.2.2 christos #include <dev/usb/usb.h>
69 1.8.2.2 christos #include <dev/usb/usbdi.h>
70 1.8.2.2 christos #include <dev/usb/usbdi_util.h>
71 1.8.2.2 christos #include <dev/usb/usbdivar.h>
72 1.8.2.2 christos #include <dev/usb/usbdevs.h>
73 1.8.2.2 christos
74 1.8.2.2 christos #include <dev/ic/rtl81x9reg.h> /* XXX for RTK_GMEDIASTAT */
75 1.8.2.2 christos #include <dev/usb/if_urereg.h>
76 1.8.2.2 christos #include <dev/usb/if_urevar.h>
77 1.8.2.2 christos
78 1.8.2.2 christos #define URE_PRINTF(sc, fmt, args...) \
79 1.8.2.2 christos device_printf((sc)->ure_dev, "%s: " fmt, __func__, ##args);
80 1.8.2.2 christos
81 1.8.2.2 christos #define URE_DEBUG
82 1.8.2.2 christos #ifdef URE_DEBUG
83 1.8.2.2 christos #define DPRINTF(x) do { if (uredebug) printf x; } while (0)
84 1.8.2.2 christos #define DPRINTFN(n, x) do { if (uredebug >= (n)) printf x; } while (0)
85 1.8.2.2 christos int uredebug = 1;
86 1.8.2.2 christos #else
87 1.8.2.2 christos #define DPRINTF(x)
88 1.8.2.2 christos #define DPRINTFN(n, x)
89 1.8.2.2 christos #endif
90 1.8.2.2 christos
91 1.8.2.2 christos static const struct usb_devno ure_devs[] = {
92 1.8.2.2 christos { USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8152 },
93 1.8.2.2 christos { USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8153 }
94 1.8.2.2 christos };
95 1.8.2.2 christos
96 1.8.2.2 christos static int ure_match(device_t, cfdata_t, void *);
97 1.8.2.2 christos static void ure_attach(device_t, device_t, void *);
98 1.8.2.2 christos static int ure_detach(device_t, int);
99 1.8.2.2 christos static int ure_activate(device_t, enum devact);
100 1.8.2.2 christos
101 1.8.2.2 christos static int ure_ctl(struct ure_softc *, uint8_t, uint16_t, uint16_t,
102 1.8.2.2 christos void *, int);
103 1.8.2.2 christos static int ure_read_mem(struct ure_softc *, uint16_t, uint16_t, void *,
104 1.8.2.2 christos int);
105 1.8.2.2 christos static int ure_write_mem(struct ure_softc *, uint16_t, uint16_t, void *,
106 1.8.2.2 christos int);
107 1.8.2.2 christos static uint8_t ure_read_1(struct ure_softc *, uint16_t, uint16_t);
108 1.8.2.2 christos static uint16_t ure_read_2(struct ure_softc *, uint16_t, uint16_t);
109 1.8.2.2 christos static uint32_t ure_read_4(struct ure_softc *, uint16_t, uint16_t);
110 1.8.2.2 christos static int ure_write_1(struct ure_softc *, uint16_t, uint16_t, uint32_t);
111 1.8.2.2 christos static int ure_write_2(struct ure_softc *, uint16_t, uint16_t, uint32_t);
112 1.8.2.2 christos static int ure_write_4(struct ure_softc *, uint16_t, uint16_t, uint32_t);
113 1.8.2.2 christos static uint16_t ure_ocp_reg_read(struct ure_softc *, uint16_t);
114 1.8.2.2 christos static void ure_ocp_reg_write(struct ure_softc *, uint16_t, uint16_t);
115 1.8.2.2 christos
116 1.8.2.2 christos static int ure_init(struct ifnet *);
117 1.8.2.2 christos static void ure_stop(struct ifnet *, int);
118 1.8.2.2 christos static void ure_start(struct ifnet *);
119 1.8.2.2 christos static void ure_reset(struct ure_softc *);
120 1.8.2.2 christos static void ure_miibus_statchg(struct ifnet *);
121 1.8.2.2 christos static int ure_miibus_readreg(device_t, int, int, uint16_t *);
122 1.8.2.2 christos static int ure_miibus_writereg(device_t, int, int, uint16_t);
123 1.8.2.2 christos static void ure_lock_mii(struct ure_softc *);
124 1.8.2.2 christos static void ure_unlock_mii(struct ure_softc *);
125 1.8.2.2 christos
126 1.8.2.2 christos static int ure_encap(struct ure_softc *, struct mbuf *, int);
127 1.8.2.2 christos static uint32_t ure_txcsum(struct mbuf *);
128 1.8.2.2 christos static void ure_rxeof(struct usbd_xfer *, void *, usbd_status);
129 1.8.2.2 christos static int ure_rxcsum(struct ifnet *, struct ure_rxpkt *);
130 1.8.2.2 christos static void ure_txeof(struct usbd_xfer *, void *, usbd_status);
131 1.8.2.2 christos static int ure_rx_list_init(struct ure_softc *);
132 1.8.2.2 christos static int ure_tx_list_init(struct ure_softc *);
133 1.8.2.2 christos
134 1.8.2.2 christos static void ure_tick_task(void *);
135 1.8.2.2 christos static void ure_tick(void *);
136 1.8.2.2 christos
137 1.8.2.2 christos static int ure_ifmedia_upd(struct ifnet *);
138 1.8.2.2 christos static void ure_ifmedia_sts(struct ifnet *, struct ifmediareq *);
139 1.8.2.2 christos static int ure_ioctl(struct ifnet *, u_long, void *);
140 1.8.2.2 christos static void ure_rtl8152_init(struct ure_softc *);
141 1.8.2.2 christos static void ure_rtl8153_init(struct ure_softc *);
142 1.8.2.2 christos static void ure_disable_teredo(struct ure_softc *);
143 1.8.2.2 christos static void ure_init_fifo(struct ure_softc *);
144 1.8.2.2 christos
145 1.8.2.2 christos CFATTACH_DECL_NEW(ure, sizeof(struct ure_softc), ure_match, ure_attach,
146 1.8.2.2 christos ure_detach, ure_activate);
147 1.8.2.2 christos
148 1.8.2.2 christos static int
149 1.8.2.2 christos ure_ctl(struct ure_softc *sc, uint8_t rw, uint16_t val, uint16_t index,
150 1.8.2.2 christos void *buf, int len)
151 1.8.2.2 christos {
152 1.8.2.2 christos usb_device_request_t req;
153 1.8.2.2 christos usbd_status err;
154 1.8.2.2 christos
155 1.8.2.2 christos if (sc->ure_dying)
156 1.8.2.2 christos return 0;
157 1.8.2.2 christos
158 1.8.2.2 christos if (rw == URE_CTL_WRITE)
159 1.8.2.2 christos req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
160 1.8.2.2 christos else
161 1.8.2.2 christos req.bmRequestType = UT_READ_VENDOR_DEVICE;
162 1.8.2.2 christos req.bRequest = UR_SET_ADDRESS;
163 1.8.2.2 christos USETW(req.wValue, val);
164 1.8.2.2 christos USETW(req.wIndex, index);
165 1.8.2.2 christos USETW(req.wLength, len);
166 1.8.2.2 christos
167 1.8.2.2 christos DPRINTFN(5, ("ure_ctl: rw %d, val 0x%04hu, index 0x%04hu, len %d\n",
168 1.8.2.2 christos rw, val, index, len));
169 1.8.2.2 christos err = usbd_do_request(sc->ure_udev, &req, buf);
170 1.8.2.2 christos if (err) {
171 1.8.2.2 christos DPRINTF(("ure_ctl: error %d\n", err));
172 1.8.2.2 christos return -1;
173 1.8.2.2 christos }
174 1.8.2.2 christos
175 1.8.2.2 christos return 0;
176 1.8.2.2 christos }
177 1.8.2.2 christos
178 1.8.2.2 christos static int
179 1.8.2.2 christos ure_read_mem(struct ure_softc *sc, uint16_t addr, uint16_t index,
180 1.8.2.2 christos void *buf, int len)
181 1.8.2.2 christos {
182 1.8.2.2 christos
183 1.8.2.2 christos return ure_ctl(sc, URE_CTL_READ, addr, index, buf, len);
184 1.8.2.2 christos }
185 1.8.2.2 christos
186 1.8.2.2 christos static int
187 1.8.2.2 christos ure_write_mem(struct ure_softc *sc, uint16_t addr, uint16_t index,
188 1.8.2.2 christos void *buf, int len)
189 1.8.2.2 christos {
190 1.8.2.2 christos
191 1.8.2.2 christos return ure_ctl(sc, URE_CTL_WRITE, addr, index, buf, len);
192 1.8.2.2 christos }
193 1.8.2.2 christos
194 1.8.2.2 christos static uint8_t
195 1.8.2.2 christos ure_read_1(struct ure_softc *sc, uint16_t reg, uint16_t index)
196 1.8.2.2 christos {
197 1.8.2.2 christos uint32_t val;
198 1.8.2.2 christos uint8_t temp[4];
199 1.8.2.2 christos uint8_t shift;
200 1.8.2.2 christos
201 1.8.2.2 christos shift = (reg & 3) << 3;
202 1.8.2.2 christos reg &= ~3;
203 1.8.2.2 christos
204 1.8.2.2 christos ure_read_mem(sc, reg, index, &temp, 4);
205 1.8.2.2 christos val = UGETDW(temp);
206 1.8.2.2 christos val >>= shift;
207 1.8.2.2 christos
208 1.8.2.2 christos return val & 0xff;
209 1.8.2.2 christos }
210 1.8.2.2 christos
211 1.8.2.2 christos static uint16_t
212 1.8.2.2 christos ure_read_2(struct ure_softc *sc, uint16_t reg, uint16_t index)
213 1.8.2.2 christos {
214 1.8.2.2 christos uint32_t val;
215 1.8.2.2 christos uint8_t temp[4];
216 1.8.2.2 christos uint8_t shift;
217 1.8.2.2 christos
218 1.8.2.2 christos shift = (reg & 2) << 3;
219 1.8.2.2 christos reg &= ~3;
220 1.8.2.2 christos
221 1.8.2.2 christos ure_read_mem(sc, reg, index, &temp, 4);
222 1.8.2.2 christos val = UGETDW(temp);
223 1.8.2.2 christos val >>= shift;
224 1.8.2.2 christos
225 1.8.2.2 christos return val & 0xffff;
226 1.8.2.2 christos }
227 1.8.2.2 christos
228 1.8.2.2 christos static uint32_t
229 1.8.2.2 christos ure_read_4(struct ure_softc *sc, uint16_t reg, uint16_t index)
230 1.8.2.2 christos {
231 1.8.2.2 christos uint8_t temp[4];
232 1.8.2.2 christos
233 1.8.2.2 christos ure_read_mem(sc, reg, index, &temp, 4);
234 1.8.2.2 christos return UGETDW(temp);
235 1.8.2.2 christos }
236 1.8.2.2 christos
237 1.8.2.2 christos static int
238 1.8.2.2 christos ure_write_1(struct ure_softc *sc, uint16_t reg, uint16_t index, uint32_t val)
239 1.8.2.2 christos {
240 1.8.2.2 christos uint16_t byen;
241 1.8.2.2 christos uint8_t temp[4];
242 1.8.2.2 christos uint8_t shift;
243 1.8.2.2 christos
244 1.8.2.2 christos byen = URE_BYTE_EN_BYTE;
245 1.8.2.2 christos shift = reg & 3;
246 1.8.2.2 christos val &= 0xff;
247 1.8.2.2 christos
248 1.8.2.2 christos if (reg & 3) {
249 1.8.2.2 christos byen <<= shift;
250 1.8.2.2 christos val <<= (shift << 3);
251 1.8.2.2 christos reg &= ~3;
252 1.8.2.2 christos }
253 1.8.2.2 christos
254 1.8.2.2 christos USETDW(temp, val);
255 1.8.2.2 christos return ure_write_mem(sc, reg, index | byen, &temp, 4);
256 1.8.2.2 christos }
257 1.8.2.2 christos
258 1.8.2.2 christos static int
259 1.8.2.2 christos ure_write_2(struct ure_softc *sc, uint16_t reg, uint16_t index, uint32_t val)
260 1.8.2.2 christos {
261 1.8.2.2 christos uint16_t byen;
262 1.8.2.2 christos uint8_t temp[4];
263 1.8.2.2 christos uint8_t shift;
264 1.8.2.2 christos
265 1.8.2.2 christos byen = URE_BYTE_EN_WORD;
266 1.8.2.2 christos shift = reg & 2;
267 1.8.2.2 christos val &= 0xffff;
268 1.8.2.2 christos
269 1.8.2.2 christos if (reg & 2) {
270 1.8.2.2 christos byen <<= shift;
271 1.8.2.2 christos val <<= (shift << 3);
272 1.8.2.2 christos reg &= ~3;
273 1.8.2.2 christos }
274 1.8.2.2 christos
275 1.8.2.2 christos USETDW(temp, val);
276 1.8.2.2 christos return ure_write_mem(sc, reg, index | byen, &temp, 4);
277 1.8.2.2 christos }
278 1.8.2.2 christos
279 1.8.2.2 christos static int
280 1.8.2.2 christos ure_write_4(struct ure_softc *sc, uint16_t reg, uint16_t index, uint32_t val)
281 1.8.2.2 christos {
282 1.8.2.2 christos uint8_t temp[4];
283 1.8.2.2 christos
284 1.8.2.2 christos USETDW(temp, val);
285 1.8.2.2 christos return ure_write_mem(sc, reg, index | URE_BYTE_EN_DWORD, &temp, 4);
286 1.8.2.2 christos }
287 1.8.2.2 christos
288 1.8.2.2 christos static uint16_t
289 1.8.2.2 christos ure_ocp_reg_read(struct ure_softc *sc, uint16_t addr)
290 1.8.2.2 christos {
291 1.8.2.2 christos uint16_t reg;
292 1.8.2.2 christos
293 1.8.2.2 christos ure_write_2(sc, URE_PLA_OCP_GPHY_BASE, URE_MCU_TYPE_PLA, addr & 0xf000);
294 1.8.2.2 christos reg = (addr & 0x0fff) | 0xb000;
295 1.8.2.2 christos
296 1.8.2.2 christos return ure_read_2(sc, reg, URE_MCU_TYPE_PLA);
297 1.8.2.2 christos }
298 1.8.2.2 christos
299 1.8.2.2 christos static void
300 1.8.2.2 christos ure_ocp_reg_write(struct ure_softc *sc, uint16_t addr, uint16_t data)
301 1.8.2.2 christos {
302 1.8.2.2 christos uint16_t reg;
303 1.8.2.2 christos
304 1.8.2.2 christos ure_write_2(sc, URE_PLA_OCP_GPHY_BASE, URE_MCU_TYPE_PLA, addr & 0xf000);
305 1.8.2.2 christos reg = (addr & 0x0fff) | 0xb000;
306 1.8.2.2 christos
307 1.8.2.2 christos ure_write_2(sc, reg, URE_MCU_TYPE_PLA, data);
308 1.8.2.2 christos }
309 1.8.2.2 christos
310 1.8.2.2 christos static int
311 1.8.2.2 christos ure_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val)
312 1.8.2.2 christos {
313 1.8.2.2 christos struct ure_softc *sc = device_private(dev);
314 1.8.2.2 christos
315 1.8.2.2 christos if (sc->ure_dying || sc->ure_phyno != phy) /* XXX */
316 1.8.2.2 christos return -1;
317 1.8.2.2 christos
318 1.8.2.2 christos /* Let the rgephy driver read the URE_PLA_PHYSTATUS register. */
319 1.8.2.2 christos if (reg == RTK_GMEDIASTAT) {
320 1.8.2.2 christos *val = ure_read_1(sc, URE_PLA_PHYSTATUS, URE_MCU_TYPE_PLA);
321 1.8.2.2 christos return 0;
322 1.8.2.2 christos }
323 1.8.2.2 christos
324 1.8.2.2 christos ure_lock_mii(sc);
325 1.8.2.2 christos *val = ure_ocp_reg_read(sc, URE_OCP_BASE_MII + reg * 2);
326 1.8.2.2 christos ure_unlock_mii(sc);
327 1.8.2.2 christos
328 1.8.2.2 christos return 0;
329 1.8.2.2 christos }
330 1.8.2.2 christos
331 1.8.2.2 christos static int
332 1.8.2.2 christos ure_miibus_writereg(device_t dev, int phy, int reg, uint16_t val)
333 1.8.2.2 christos {
334 1.8.2.2 christos struct ure_softc *sc = device_private(dev);
335 1.8.2.2 christos
336 1.8.2.2 christos if (sc->ure_dying || sc->ure_phyno != phy) /* XXX */
337 1.8.2.2 christos return -1;
338 1.8.2.2 christos
339 1.8.2.2 christos ure_lock_mii(sc);
340 1.8.2.2 christos ure_ocp_reg_write(sc, URE_OCP_BASE_MII + reg * 2, val);
341 1.8.2.2 christos ure_unlock_mii(sc);
342 1.8.2.2 christos
343 1.8.2.2 christos return 0;
344 1.8.2.2 christos }
345 1.8.2.2 christos
346 1.8.2.2 christos static void
347 1.8.2.2 christos ure_miibus_statchg(struct ifnet *ifp)
348 1.8.2.2 christos {
349 1.8.2.2 christos struct ure_softc *sc;
350 1.8.2.2 christos struct mii_data *mii;
351 1.8.2.2 christos
352 1.8.2.2 christos if (ifp == NULL || (ifp->if_flags & IFF_RUNNING) == 0)
353 1.8.2.2 christos return;
354 1.8.2.2 christos
355 1.8.2.2 christos sc = ifp->if_softc;
356 1.8.2.2 christos mii = GET_MII(sc);
357 1.8.2.2 christos
358 1.8.2.2 christos if (mii == NULL)
359 1.8.2.2 christos return;
360 1.8.2.2 christos
361 1.8.2.2 christos sc->ure_flags &= ~URE_FLAG_LINK;
362 1.8.2.2 christos if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
363 1.8.2.2 christos (IFM_ACTIVE | IFM_AVALID)) {
364 1.8.2.2 christos switch (IFM_SUBTYPE(mii->mii_media_active)) {
365 1.8.2.2 christos case IFM_10_T:
366 1.8.2.2 christos case IFM_100_TX:
367 1.8.2.2 christos sc->ure_flags |= URE_FLAG_LINK;
368 1.8.2.2 christos break;
369 1.8.2.2 christos case IFM_1000_T:
370 1.8.2.2 christos if ((sc->ure_flags & URE_FLAG_8152) != 0)
371 1.8.2.2 christos break;
372 1.8.2.2 christos sc->ure_flags |= URE_FLAG_LINK;
373 1.8.2.2 christos break;
374 1.8.2.2 christos default:
375 1.8.2.2 christos break;
376 1.8.2.2 christos }
377 1.8.2.2 christos }
378 1.8.2.2 christos }
379 1.8.2.2 christos
380 1.8.2.2 christos static int
381 1.8.2.2 christos ure_ifmedia_upd(struct ifnet *ifp)
382 1.8.2.2 christos {
383 1.8.2.2 christos struct ure_softc *sc = ifp->if_softc;
384 1.8.2.2 christos struct mii_data *mii = GET_MII(sc);
385 1.8.2.2 christos int err;
386 1.8.2.2 christos
387 1.8.2.2 christos sc->ure_flags &= ~URE_FLAG_LINK;
388 1.8.2.2 christos if (mii->mii_instance) {
389 1.8.2.2 christos struct mii_softc *miisc;
390 1.8.2.2 christos LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
391 1.8.2.2 christos mii_phy_reset(miisc);
392 1.8.2.2 christos }
393 1.8.2.2 christos
394 1.8.2.2 christos err = mii_mediachg(mii);
395 1.8.2.2 christos if (err == ENXIO)
396 1.8.2.2 christos return 0; /* XXX */
397 1.8.2.2 christos else
398 1.8.2.2 christos return err;
399 1.8.2.2 christos }
400 1.8.2.2 christos
401 1.8.2.2 christos static void
402 1.8.2.2 christos ure_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
403 1.8.2.2 christos {
404 1.8.2.2 christos struct ure_softc *sc = ifp->if_softc;
405 1.8.2.2 christos struct mii_data *mii = GET_MII(sc);
406 1.8.2.2 christos
407 1.8.2.2 christos mii_pollstat(mii);
408 1.8.2.2 christos ifmr->ifm_active = mii->mii_media_active;
409 1.8.2.2 christos ifmr->ifm_status = mii->mii_media_status;
410 1.8.2.2 christos }
411 1.8.2.2 christos
412 1.8.2.2 christos static void
413 1.8.2.2 christos ure_iff(struct ure_softc *sc)
414 1.8.2.2 christos {
415 1.8.2.2 christos struct ethercom *ec = &sc->ure_ec;
416 1.8.2.2 christos struct ifnet *ifp = GET_IFP(sc);
417 1.8.2.2 christos struct ether_multi *enm;
418 1.8.2.2 christos struct ether_multistep step;
419 1.8.2.2 christos uint32_t hashes[2] = { 0, 0 };
420 1.8.2.2 christos uint32_t hash;
421 1.8.2.2 christos uint32_t rxmode;
422 1.8.2.2 christos
423 1.8.2.2 christos if (sc->ure_dying)
424 1.8.2.2 christos return;
425 1.8.2.2 christos
426 1.8.2.2 christos rxmode = ure_read_4(sc, URE_PLA_RCR, URE_MCU_TYPE_PLA);
427 1.8.2.2 christos rxmode &= ~URE_RCR_ACPT_ALL;
428 1.8.2.2 christos ifp->if_flags &= ~IFF_ALLMULTI;
429 1.8.2.2 christos
430 1.8.2.2 christos /*
431 1.8.2.2 christos * Always accept frames destined to our station address.
432 1.8.2.2 christos * Always accept broadcast frames.
433 1.8.2.2 christos */
434 1.8.2.2 christos rxmode |= URE_RCR_APM | URE_RCR_AB;
435 1.8.2.2 christos
436 1.8.2.2 christos if (ifp->if_flags & IFF_PROMISC) {
437 1.8.2.2 christos rxmode |= URE_RCR_AAP;
438 1.8.2.2 christos allmulti: ifp->if_flags |= IFF_ALLMULTI;
439 1.8.2.2 christos rxmode |= URE_RCR_AM;
440 1.8.2.2 christos hashes[0] = hashes[1] = 0xffffffff;
441 1.8.2.2 christos } else {
442 1.8.2.2 christos rxmode |= URE_RCR_AM;
443 1.8.2.2 christos
444 1.8.2.2 christos ETHER_LOCK(ec);
445 1.8.2.2 christos ETHER_FIRST_MULTI(step, ec, enm);
446 1.8.2.2 christos while (enm != NULL) {
447 1.8.2.2 christos if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
448 1.8.2.2 christos ETHER_ADDR_LEN)) {
449 1.8.2.2 christos ETHER_UNLOCK(ec);
450 1.8.2.2 christos goto allmulti;
451 1.8.2.2 christos }
452 1.8.2.2 christos
453 1.8.2.2 christos hash = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN)
454 1.8.2.2 christos >> 26;
455 1.8.2.2 christos if (hash < 32)
456 1.8.2.2 christos hashes[0] |= (1 << hash);
457 1.8.2.2 christos else
458 1.8.2.2 christos hashes[1] |= (1 << (hash - 32));
459 1.8.2.2 christos
460 1.8.2.2 christos ETHER_NEXT_MULTI(step, enm);
461 1.8.2.2 christos }
462 1.8.2.2 christos ETHER_UNLOCK(ec);
463 1.8.2.2 christos
464 1.8.2.2 christos hash = bswap32(hashes[0]);
465 1.8.2.2 christos hashes[0] = bswap32(hashes[1]);
466 1.8.2.2 christos hashes[1] = hash;
467 1.8.2.2 christos }
468 1.8.2.2 christos
469 1.8.2.2 christos ure_write_4(sc, URE_PLA_MAR0, URE_MCU_TYPE_PLA, hashes[0]);
470 1.8.2.2 christos ure_write_4(sc, URE_PLA_MAR4, URE_MCU_TYPE_PLA, hashes[1]);
471 1.8.2.2 christos ure_write_4(sc, URE_PLA_RCR, URE_MCU_TYPE_PLA, rxmode);
472 1.8.2.2 christos }
473 1.8.2.2 christos
474 1.8.2.2 christos static void
475 1.8.2.2 christos ure_reset(struct ure_softc *sc)
476 1.8.2.2 christos {
477 1.8.2.2 christos int i;
478 1.8.2.2 christos
479 1.8.2.2 christos ure_write_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA, URE_CR_RST);
480 1.8.2.2 christos
481 1.8.2.2 christos for (i = 0; i < URE_TIMEOUT; i++) {
482 1.8.2.2 christos if (!(ure_read_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA) &
483 1.8.2.2 christos URE_CR_RST))
484 1.8.2.2 christos break;
485 1.8.2.2 christos usbd_delay_ms(sc->ure_udev, 10);
486 1.8.2.2 christos }
487 1.8.2.2 christos if (i == URE_TIMEOUT)
488 1.8.2.2 christos URE_PRINTF(sc, "reset never completed\n");
489 1.8.2.2 christos }
490 1.8.2.2 christos
491 1.8.2.2 christos static int
492 1.8.2.2 christos ure_init(struct ifnet *ifp)
493 1.8.2.2 christos {
494 1.8.2.2 christos struct ure_softc *sc = ifp->if_softc;
495 1.8.2.2 christos struct ure_chain *c;
496 1.8.2.2 christos usbd_status err;
497 1.8.2.2 christos int s, i;
498 1.8.2.2 christos uint8_t eaddr[8];
499 1.8.2.2 christos
500 1.8.2.2 christos s = splnet();
501 1.8.2.2 christos
502 1.8.2.2 christos /* Cancel pending I/O. */
503 1.8.2.2 christos if (ifp->if_flags & IFF_RUNNING)
504 1.8.2.2 christos ure_stop(ifp, 1);
505 1.8.2.2 christos
506 1.8.2.2 christos /* Set MAC address. */
507 1.8.2.2 christos memset(eaddr, 0, sizeof(eaddr));
508 1.8.2.2 christos memcpy(eaddr, CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
509 1.8.2.2 christos ure_write_1(sc, URE_PLA_CRWECR, URE_MCU_TYPE_PLA, URE_CRWECR_CONFIG);
510 1.8.2.2 christos ure_write_mem(sc, URE_PLA_IDR, URE_MCU_TYPE_PLA | URE_BYTE_EN_SIX_BYTES,
511 1.8.2.2 christos eaddr, 8);
512 1.8.2.2 christos ure_write_1(sc, URE_PLA_CRWECR, URE_MCU_TYPE_PLA, URE_CRWECR_NORAML);
513 1.8.2.2 christos
514 1.8.2.2 christos /* Reset the packet filter. */
515 1.8.2.2 christos ure_write_2(sc, URE_PLA_FMC, URE_MCU_TYPE_PLA,
516 1.8.2.2 christos ure_read_2(sc, URE_PLA_FMC, URE_MCU_TYPE_PLA) &
517 1.8.2.2 christos ~URE_FMC_FCR_MCU_EN);
518 1.8.2.2 christos ure_write_2(sc, URE_PLA_FMC, URE_MCU_TYPE_PLA,
519 1.8.2.2 christos ure_read_2(sc, URE_PLA_FMC, URE_MCU_TYPE_PLA) |
520 1.8.2.2 christos URE_FMC_FCR_MCU_EN);
521 1.8.2.2 christos
522 1.8.2.2 christos /* Enable transmit and receive. */
523 1.8.2.2 christos ure_write_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA,
524 1.8.2.2 christos ure_read_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA) | URE_CR_RE |
525 1.8.2.2 christos URE_CR_TE);
526 1.8.2.2 christos
527 1.8.2.2 christos ure_write_2(sc, URE_PLA_MISC_1, URE_MCU_TYPE_PLA,
528 1.8.2.2 christos ure_read_2(sc, URE_PLA_MISC_1, URE_MCU_TYPE_PLA) &
529 1.8.2.2 christos ~URE_RXDY_GATED_EN);
530 1.8.2.2 christos
531 1.8.2.2 christos /* Load the multicast filter. */
532 1.8.2.2 christos ure_iff(sc);
533 1.8.2.2 christos
534 1.8.2.2 christos /* Open RX and TX pipes. */
535 1.8.2.2 christos err = usbd_open_pipe(sc->ure_iface, sc->ure_ed[URE_ENDPT_RX],
536 1.8.2.2 christos USBD_EXCLUSIVE_USE, &sc->ure_ep[URE_ENDPT_RX]);
537 1.8.2.2 christos if (err) {
538 1.8.2.2 christos URE_PRINTF(sc, "open rx pipe failed: %s\n", usbd_errstr(err));
539 1.8.2.2 christos splx(s);
540 1.8.2.2 christos return EIO;
541 1.8.2.2 christos }
542 1.8.2.2 christos
543 1.8.2.2 christos err = usbd_open_pipe(sc->ure_iface, sc->ure_ed[URE_ENDPT_TX],
544 1.8.2.2 christos USBD_EXCLUSIVE_USE, &sc->ure_ep[URE_ENDPT_TX]);
545 1.8.2.2 christos if (err) {
546 1.8.2.2 christos URE_PRINTF(sc, "open tx pipe failed: %s\n", usbd_errstr(err));
547 1.8.2.2 christos splx(s);
548 1.8.2.2 christos return EIO;
549 1.8.2.2 christos }
550 1.8.2.2 christos
551 1.8.2.2 christos if (ure_rx_list_init(sc)) {
552 1.8.2.2 christos URE_PRINTF(sc, "rx list init failed\n");
553 1.8.2.2 christos splx(s);
554 1.8.2.2 christos return ENOBUFS;
555 1.8.2.2 christos }
556 1.8.2.2 christos
557 1.8.2.2 christos if (ure_tx_list_init(sc)) {
558 1.8.2.2 christos URE_PRINTF(sc, "tx list init failed\n");
559 1.8.2.2 christos splx(s);
560 1.8.2.2 christos return ENOBUFS;
561 1.8.2.2 christos }
562 1.8.2.2 christos
563 1.8.2.2 christos /* Start up the receive pipe. */
564 1.8.2.2 christos for (i = 0; i < URE_RX_LIST_CNT; i++) {
565 1.8.2.2 christos c = &sc->ure_cdata.rx_chain[i];
566 1.8.2.2 christos usbd_setup_xfer(c->uc_xfer, c, c->uc_buf, sc->ure_bufsz,
567 1.8.2.2 christos USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ure_rxeof);
568 1.8.2.2 christos usbd_transfer(c->uc_xfer);
569 1.8.2.2 christos }
570 1.8.2.2 christos
571 1.8.2.2 christos /* Indicate we are up and running. */
572 1.8.2.2 christos ifp->if_flags |= IFF_RUNNING;
573 1.8.2.2 christos ifp->if_flags &= ~IFF_OACTIVE;
574 1.8.2.2 christos
575 1.8.2.2 christos splx(s);
576 1.8.2.2 christos
577 1.8.2.2 christos callout_reset(&sc->ure_stat_ch, hz, ure_tick, sc);
578 1.8.2.2 christos
579 1.8.2.2 christos return 0;
580 1.8.2.2 christos }
581 1.8.2.2 christos
582 1.8.2.2 christos static void
583 1.8.2.2 christos ure_start(struct ifnet *ifp)
584 1.8.2.2 christos {
585 1.8.2.2 christos struct ure_softc *sc = ifp->if_softc;
586 1.8.2.2 christos struct mbuf *m;
587 1.8.2.2 christos struct ure_cdata *cd = &sc->ure_cdata;
588 1.8.2.2 christos int idx;
589 1.8.2.2 christos
590 1.8.2.2 christos if ((sc->ure_flags & URE_FLAG_LINK) == 0 ||
591 1.8.2.2 christos (ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) != IFF_RUNNING) {
592 1.8.2.2 christos return;
593 1.8.2.2 christos }
594 1.8.2.2 christos
595 1.8.2.2 christos idx = cd->tx_prod;
596 1.8.2.2 christos while (cd->tx_cnt < URE_TX_LIST_CNT) {
597 1.8.2.2 christos IFQ_POLL(&ifp->if_snd, m);
598 1.8.2.2 christos if (m == NULL)
599 1.8.2.2 christos break;
600 1.8.2.2 christos
601 1.8.2.2 christos if (ure_encap(sc, m, idx)) {
602 1.8.2.2 christos ifp->if_oerrors++;
603 1.8.2.2 christos break;
604 1.8.2.2 christos }
605 1.8.2.2 christos IFQ_DEQUEUE(&ifp->if_snd, m);
606 1.8.2.2 christos
607 1.8.2.2 christos bpf_mtap(ifp, m, BPF_D_OUT);
608 1.8.2.2 christos m_freem(m);
609 1.8.2.2 christos
610 1.8.2.2 christos idx = (idx + 1) % URE_TX_LIST_CNT;
611 1.8.2.2 christos cd->tx_cnt++;
612 1.8.2.2 christos }
613 1.8.2.2 christos cd->tx_prod = idx;
614 1.8.2.2 christos
615 1.8.2.2 christos if (cd->tx_cnt >= URE_TX_LIST_CNT)
616 1.8.2.2 christos ifp->if_flags |= IFF_OACTIVE;
617 1.8.2.2 christos }
618 1.8.2.2 christos
619 1.8.2.2 christos static void
620 1.8.2.2 christos ure_tick(void *xsc)
621 1.8.2.2 christos {
622 1.8.2.2 christos struct ure_softc *sc = xsc;
623 1.8.2.2 christos
624 1.8.2.2 christos if (sc == NULL)
625 1.8.2.2 christos return;
626 1.8.2.2 christos
627 1.8.2.2 christos if (sc->ure_dying)
628 1.8.2.2 christos return;
629 1.8.2.2 christos
630 1.8.2.2 christos usb_add_task(sc->ure_udev, &sc->ure_tick_task, USB_TASKQ_DRIVER);
631 1.8.2.2 christos }
632 1.8.2.2 christos
633 1.8.2.2 christos static void
634 1.8.2.2 christos ure_stop(struct ifnet *ifp, int disable __unused)
635 1.8.2.2 christos {
636 1.8.2.2 christos struct ure_softc *sc = ifp->if_softc;
637 1.8.2.2 christos struct ure_chain *c;
638 1.8.2.2 christos usbd_status err;
639 1.8.2.2 christos int i;
640 1.8.2.2 christos
641 1.8.2.2 christos ure_reset(sc);
642 1.8.2.2 christos
643 1.8.2.2 christos ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
644 1.8.2.2 christos
645 1.8.2.2 christos callout_stop(&sc->ure_stat_ch);
646 1.8.2.2 christos
647 1.8.2.2 christos sc->ure_flags &= ~URE_FLAG_LINK; /* XXX */
648 1.8.2.2 christos
649 1.8.2.2 christos if (sc->ure_ep[URE_ENDPT_RX] != NULL) {
650 1.8.2.2 christos err = usbd_abort_pipe(sc->ure_ep[URE_ENDPT_RX]);
651 1.8.2.2 christos if (err)
652 1.8.2.2 christos URE_PRINTF(sc, "abort rx pipe failed: %s\n",
653 1.8.2.2 christos usbd_errstr(err));
654 1.8.2.2 christos }
655 1.8.2.2 christos
656 1.8.2.2 christos if (sc->ure_ep[URE_ENDPT_TX] != NULL) {
657 1.8.2.2 christos err = usbd_abort_pipe(sc->ure_ep[URE_ENDPT_TX]);
658 1.8.2.2 christos if (err)
659 1.8.2.2 christos URE_PRINTF(sc, "abort tx pipe failed: %s\n",
660 1.8.2.2 christos usbd_errstr(err));
661 1.8.2.2 christos }
662 1.8.2.2 christos
663 1.8.2.2 christos for (i = 0; i < URE_RX_LIST_CNT; i++) {
664 1.8.2.2 christos c = &sc->ure_cdata.rx_chain[i];
665 1.8.2.2 christos if (c->uc_xfer != NULL) {
666 1.8.2.2 christos usbd_destroy_xfer(c->uc_xfer);
667 1.8.2.2 christos c->uc_xfer = NULL;
668 1.8.2.2 christos }
669 1.8.2.2 christos }
670 1.8.2.2 christos
671 1.8.2.2 christos for (i = 0; i < URE_TX_LIST_CNT; i++) {
672 1.8.2.2 christos c = &sc->ure_cdata.tx_chain[i];
673 1.8.2.2 christos if (c->uc_xfer != NULL) {
674 1.8.2.2 christos usbd_destroy_xfer(c->uc_xfer);
675 1.8.2.2 christos c->uc_xfer = NULL;
676 1.8.2.2 christos }
677 1.8.2.2 christos }
678 1.8.2.2 christos
679 1.8.2.2 christos if (sc->ure_ep[URE_ENDPT_RX] != NULL) {
680 1.8.2.2 christos err = usbd_close_pipe(sc->ure_ep[URE_ENDPT_RX]);
681 1.8.2.2 christos if (err)
682 1.8.2.2 christos URE_PRINTF(sc, "close rx pipe failed: %s\n",
683 1.8.2.2 christos usbd_errstr(err));
684 1.8.2.2 christos sc->ure_ep[URE_ENDPT_RX] = NULL;
685 1.8.2.2 christos }
686 1.8.2.2 christos
687 1.8.2.2 christos if (sc->ure_ep[URE_ENDPT_TX] != NULL) {
688 1.8.2.2 christos err = usbd_close_pipe(sc->ure_ep[URE_ENDPT_TX]);
689 1.8.2.2 christos if (err)
690 1.8.2.2 christos URE_PRINTF(sc, "close tx pipe failed: %s\n",
691 1.8.2.2 christos usbd_errstr(err));
692 1.8.2.2 christos sc->ure_ep[URE_ENDPT_TX] = NULL;
693 1.8.2.2 christos }
694 1.8.2.2 christos }
695 1.8.2.2 christos
696 1.8.2.2 christos static void
697 1.8.2.2 christos ure_rtl8152_init(struct ure_softc *sc)
698 1.8.2.2 christos {
699 1.8.2.2 christos uint32_t pwrctrl;
700 1.8.2.2 christos
701 1.8.2.2 christos /* Disable ALDPS. */
702 1.8.2.2 christos ure_ocp_reg_write(sc, URE_OCP_ALDPS_CONFIG, URE_ENPDNPS | URE_LINKENA |
703 1.8.2.2 christos URE_DIS_SDSAVE);
704 1.8.2.2 christos usbd_delay_ms(sc->ure_udev, 20);
705 1.8.2.2 christos
706 1.8.2.2 christos if (sc->ure_chip & URE_CHIP_VER_4C00) {
707 1.8.2.2 christos ure_write_2(sc, URE_PLA_LED_FEATURE, URE_MCU_TYPE_PLA,
708 1.8.2.2 christos ure_read_2(sc, URE_PLA_LED_FEATURE, URE_MCU_TYPE_PLA) &
709 1.8.2.2 christos ~URE_LED_MODE_MASK);
710 1.8.2.2 christos }
711 1.8.2.2 christos
712 1.8.2.2 christos ure_write_2(sc, URE_USB_UPS_CTRL, URE_MCU_TYPE_USB,
713 1.8.2.2 christos ure_read_2(sc, URE_USB_UPS_CTRL, URE_MCU_TYPE_USB) &
714 1.8.2.2 christos ~URE_POWER_CUT);
715 1.8.2.2 christos ure_write_2(sc, URE_USB_PM_CTRL_STATUS, URE_MCU_TYPE_USB,
716 1.8.2.2 christos ure_read_2(sc, URE_USB_PM_CTRL_STATUS, URE_MCU_TYPE_USB) &
717 1.8.2.2 christos ~URE_RESUME_INDICATE);
718 1.8.2.2 christos
719 1.8.2.2 christos ure_write_2(sc, URE_PLA_PHY_PWR, URE_MCU_TYPE_PLA,
720 1.8.2.2 christos ure_read_2(sc, URE_PLA_PHY_PWR, URE_MCU_TYPE_PLA) |
721 1.8.2.2 christos URE_TX_10M_IDLE_EN | URE_PFM_PWM_SWITCH);
722 1.8.2.2 christos pwrctrl = ure_read_4(sc, URE_PLA_MAC_PWR_CTRL, URE_MCU_TYPE_PLA);
723 1.8.2.2 christos pwrctrl &= ~URE_MCU_CLK_RATIO_MASK;
724 1.8.2.2 christos pwrctrl |= URE_MCU_CLK_RATIO | URE_D3_CLK_GATED_EN;
725 1.8.2.2 christos ure_write_4(sc, URE_PLA_MAC_PWR_CTRL, URE_MCU_TYPE_PLA, pwrctrl);
726 1.8.2.2 christos ure_write_2(sc, URE_PLA_GPHY_INTR_IMR, URE_MCU_TYPE_PLA,
727 1.8.2.2 christos URE_GPHY_STS_MSK | URE_SPEED_DOWN_MSK | URE_SPDWN_RXDV_MSK |
728 1.8.2.2 christos URE_SPDWN_LINKCHG_MSK);
729 1.8.2.2 christos
730 1.8.2.2 christos /* Enable Rx aggregation. */
731 1.8.2.2 christos ure_write_2(sc, URE_USB_USB_CTRL, URE_MCU_TYPE_USB,
732 1.8.2.2 christos ure_read_2(sc, URE_USB_USB_CTRL, URE_MCU_TYPE_USB) &
733 1.8.2.2 christos ~URE_RX_AGG_DISABLE);
734 1.8.2.2 christos
735 1.8.2.2 christos /* Disable ALDPS. */
736 1.8.2.2 christos ure_ocp_reg_write(sc, URE_OCP_ALDPS_CONFIG, URE_ENPDNPS | URE_LINKENA |
737 1.8.2.2 christos URE_DIS_SDSAVE);
738 1.8.2.2 christos usbd_delay_ms(sc->ure_udev, 20);
739 1.8.2.2 christos
740 1.8.2.2 christos ure_init_fifo(sc);
741 1.8.2.2 christos
742 1.8.2.2 christos ure_write_1(sc, URE_USB_TX_AGG, URE_MCU_TYPE_USB,
743 1.8.2.2 christos URE_TX_AGG_MAX_THRESHOLD);
744 1.8.2.2 christos ure_write_4(sc, URE_USB_RX_BUF_TH, URE_MCU_TYPE_USB, URE_RX_THR_HIGH);
745 1.8.2.2 christos ure_write_4(sc, URE_USB_TX_DMA, URE_MCU_TYPE_USB,
746 1.8.2.2 christos URE_TEST_MODE_DISABLE | URE_TX_SIZE_ADJUST1);
747 1.8.2.2 christos }
748 1.8.2.2 christos
749 1.8.2.2 christos static void
750 1.8.2.2 christos ure_rtl8153_init(struct ure_softc *sc)
751 1.8.2.2 christos {
752 1.8.2.2 christos uint16_t val;
753 1.8.2.2 christos uint8_t u1u2[8];
754 1.8.2.2 christos int i;
755 1.8.2.2 christos
756 1.8.2.2 christos /* Disable ALDPS. */
757 1.8.2.2 christos ure_ocp_reg_write(sc, URE_OCP_POWER_CFG,
758 1.8.2.2 christos ure_ocp_reg_read(sc, URE_OCP_POWER_CFG) & ~URE_EN_ALDPS);
759 1.8.2.2 christos usbd_delay_ms(sc->ure_udev, 20);
760 1.8.2.2 christos
761 1.8.2.2 christos memset(u1u2, 0x00, sizeof(u1u2));
762 1.8.2.2 christos ure_write_mem(sc, URE_USB_TOLERANCE,
763 1.8.2.2 christos URE_MCU_TYPE_USB | URE_BYTE_EN_SIX_BYTES, u1u2, sizeof(u1u2));
764 1.8.2.2 christos
765 1.8.2.2 christos for (i = 0; i < URE_TIMEOUT; i++) {
766 1.8.2.2 christos if (ure_read_2(sc, URE_PLA_BOOT_CTRL, URE_MCU_TYPE_PLA) &
767 1.8.2.2 christos URE_AUTOLOAD_DONE)
768 1.8.2.2 christos break;
769 1.8.2.2 christos usbd_delay_ms(sc->ure_udev, 10);
770 1.8.2.2 christos }
771 1.8.2.2 christos if (i == URE_TIMEOUT)
772 1.8.2.2 christos URE_PRINTF(sc, "timeout waiting for chip autoload\n");
773 1.8.2.2 christos
774 1.8.2.2 christos for (i = 0; i < URE_TIMEOUT; i++) {
775 1.8.2.2 christos val = ure_ocp_reg_read(sc, URE_OCP_PHY_STATUS) &
776 1.8.2.2 christos URE_PHY_STAT_MASK;
777 1.8.2.2 christos if (val == URE_PHY_STAT_LAN_ON || val == URE_PHY_STAT_PWRDN)
778 1.8.2.2 christos break;
779 1.8.2.2 christos usbd_delay_ms(sc->ure_udev, 10);
780 1.8.2.2 christos }
781 1.8.2.2 christos if (i == URE_TIMEOUT)
782 1.8.2.2 christos URE_PRINTF(sc, "timeout waiting for phy to stabilize\n");
783 1.8.2.2 christos
784 1.8.2.2 christos ure_write_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB,
785 1.8.2.2 christos ure_read_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB) &
786 1.8.2.2 christos ~URE_U2P3_ENABLE);
787 1.8.2.2 christos
788 1.8.2.2 christos if (sc->ure_chip & URE_CHIP_VER_5C10) {
789 1.8.2.2 christos val = ure_read_2(sc, URE_USB_SSPHYLINK2, URE_MCU_TYPE_USB);
790 1.8.2.2 christos val &= ~URE_PWD_DN_SCALE_MASK;
791 1.8.2.2 christos val |= URE_PWD_DN_SCALE(96);
792 1.8.2.2 christos ure_write_2(sc, URE_USB_SSPHYLINK2, URE_MCU_TYPE_USB, val);
793 1.8.2.2 christos
794 1.8.2.2 christos ure_write_1(sc, URE_USB_USB2PHY, URE_MCU_TYPE_USB,
795 1.8.2.2 christos ure_read_1(sc, URE_USB_USB2PHY, URE_MCU_TYPE_USB) |
796 1.8.2.2 christos URE_USB2PHY_L1 | URE_USB2PHY_SUSPEND);
797 1.8.2.2 christos } else if (sc->ure_chip & URE_CHIP_VER_5C20) {
798 1.8.2.2 christos ure_write_1(sc, URE_PLA_DMY_REG0, URE_MCU_TYPE_PLA,
799 1.8.2.2 christos ure_read_1(sc, URE_PLA_DMY_REG0, URE_MCU_TYPE_PLA) &
800 1.8.2.2 christos ~URE_ECM_ALDPS);
801 1.8.2.2 christos }
802 1.8.2.2 christos if (sc->ure_chip & (URE_CHIP_VER_5C20 | URE_CHIP_VER_5C30)) {
803 1.8.2.2 christos val = ure_read_1(sc, URE_USB_CSR_DUMMY1, URE_MCU_TYPE_USB);
804 1.8.2.2 christos if (ure_read_2(sc, URE_USB_BURST_SIZE, URE_MCU_TYPE_USB) ==
805 1.8.2.2 christos 0)
806 1.8.2.2 christos val &= ~URE_DYNAMIC_BURST;
807 1.8.2.2 christos else
808 1.8.2.2 christos val |= URE_DYNAMIC_BURST;
809 1.8.2.2 christos ure_write_1(sc, URE_USB_CSR_DUMMY1, URE_MCU_TYPE_USB, val);
810 1.8.2.2 christos }
811 1.8.2.2 christos
812 1.8.2.2 christos ure_write_1(sc, URE_USB_CSR_DUMMY2, URE_MCU_TYPE_USB,
813 1.8.2.2 christos ure_read_1(sc, URE_USB_CSR_DUMMY2, URE_MCU_TYPE_USB) |
814 1.8.2.2 christos URE_EP4_FULL_FC);
815 1.8.2.2 christos
816 1.8.2.2 christos ure_write_2(sc, URE_USB_WDT11_CTRL, URE_MCU_TYPE_USB,
817 1.8.2.2 christos ure_read_2(sc, URE_USB_WDT11_CTRL, URE_MCU_TYPE_USB) &
818 1.8.2.2 christos ~URE_TIMER11_EN);
819 1.8.2.2 christos
820 1.8.2.2 christos ure_write_2(sc, URE_PLA_LED_FEATURE, URE_MCU_TYPE_PLA,
821 1.8.2.2 christos ure_read_2(sc, URE_PLA_LED_FEATURE, URE_MCU_TYPE_PLA) &
822 1.8.2.2 christos ~URE_LED_MODE_MASK);
823 1.8.2.2 christos
824 1.8.2.2 christos if ((sc->ure_chip & URE_CHIP_VER_5C10) &&
825 1.8.2.2 christos sc->ure_udev->ud_speed != USB_SPEED_SUPER)
826 1.8.2.2 christos val = URE_LPM_TIMER_500MS;
827 1.8.2.2 christos else
828 1.8.2.2 christos val = URE_LPM_TIMER_500US;
829 1.8.2.2 christos ure_write_1(sc, URE_USB_LPM_CTRL, URE_MCU_TYPE_USB,
830 1.8.2.2 christos val | URE_FIFO_EMPTY_1FB | URE_ROK_EXIT_LPM);
831 1.8.2.2 christos
832 1.8.2.2 christos val = ure_read_2(sc, URE_USB_AFE_CTRL2, URE_MCU_TYPE_USB);
833 1.8.2.2 christos val &= ~URE_SEN_VAL_MASK;
834 1.8.2.2 christos val |= URE_SEN_VAL_NORMAL | URE_SEL_RXIDLE;
835 1.8.2.2 christos ure_write_2(sc, URE_USB_AFE_CTRL2, URE_MCU_TYPE_USB, val);
836 1.8.2.2 christos
837 1.8.2.2 christos ure_write_2(sc, URE_USB_CONNECT_TIMER, URE_MCU_TYPE_USB, 0x0001);
838 1.8.2.2 christos
839 1.8.2.2 christos ure_write_2(sc, URE_USB_POWER_CUT, URE_MCU_TYPE_USB,
840 1.8.2.2 christos ure_read_2(sc, URE_USB_POWER_CUT, URE_MCU_TYPE_USB) &
841 1.8.2.2 christos ~(URE_PWR_EN | URE_PHASE2_EN));
842 1.8.2.2 christos ure_write_2(sc, URE_USB_MISC_0, URE_MCU_TYPE_USB,
843 1.8.2.2 christos ure_read_2(sc, URE_USB_MISC_0, URE_MCU_TYPE_USB) &
844 1.8.2.2 christos ~URE_PCUT_STATUS);
845 1.8.2.2 christos
846 1.8.2.2 christos memset(u1u2, 0xff, sizeof(u1u2));
847 1.8.2.2 christos ure_write_mem(sc, URE_USB_TOLERANCE,
848 1.8.2.2 christos URE_MCU_TYPE_USB | URE_BYTE_EN_SIX_BYTES, u1u2, sizeof(u1u2));
849 1.8.2.2 christos
850 1.8.2.2 christos ure_write_2(sc, URE_PLA_MAC_PWR_CTRL, URE_MCU_TYPE_PLA,
851 1.8.2.2 christos URE_ALDPS_SPDWN_RATIO);
852 1.8.2.2 christos ure_write_2(sc, URE_PLA_MAC_PWR_CTRL2, URE_MCU_TYPE_PLA,
853 1.8.2.2 christos URE_EEE_SPDWN_RATIO);
854 1.8.2.2 christos ure_write_2(sc, URE_PLA_MAC_PWR_CTRL3, URE_MCU_TYPE_PLA,
855 1.8.2.2 christos URE_PKT_AVAIL_SPDWN_EN | URE_SUSPEND_SPDWN_EN |
856 1.8.2.2 christos URE_U1U2_SPDWN_EN | URE_L1_SPDWN_EN);
857 1.8.2.2 christos ure_write_2(sc, URE_PLA_MAC_PWR_CTRL4, URE_MCU_TYPE_PLA,
858 1.8.2.2 christos URE_PWRSAVE_SPDWN_EN | URE_RXDV_SPDWN_EN | URE_TX10MIDLE_EN |
859 1.8.2.2 christos URE_TP100_SPDWN_EN | URE_TP500_SPDWN_EN | URE_TP1000_SPDWN_EN |
860 1.8.2.2 christos URE_EEE_SPDWN_EN);
861 1.8.2.2 christos
862 1.8.2.2 christos val = ure_read_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB);
863 1.8.2.2 christos if (!(sc->ure_chip & (URE_CHIP_VER_5C00 | URE_CHIP_VER_5C10)))
864 1.8.2.2 christos val |= URE_U2P3_ENABLE;
865 1.8.2.2 christos else
866 1.8.2.2 christos val &= ~URE_U2P3_ENABLE;
867 1.8.2.2 christos ure_write_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB, val);
868 1.8.2.2 christos
869 1.8.2.2 christos memset(u1u2, 0x00, sizeof(u1u2));
870 1.8.2.2 christos ure_write_mem(sc, URE_USB_TOLERANCE,
871 1.8.2.2 christos URE_MCU_TYPE_USB | URE_BYTE_EN_SIX_BYTES, u1u2, sizeof(u1u2));
872 1.8.2.2 christos
873 1.8.2.2 christos /* Disable ALDPS. */
874 1.8.2.2 christos ure_ocp_reg_write(sc, URE_OCP_POWER_CFG,
875 1.8.2.2 christos ure_ocp_reg_read(sc, URE_OCP_POWER_CFG) & ~URE_EN_ALDPS);
876 1.8.2.2 christos usbd_delay_ms(sc->ure_udev, 20);
877 1.8.2.2 christos
878 1.8.2.2 christos ure_init_fifo(sc);
879 1.8.2.2 christos
880 1.8.2.2 christos /* Enable Rx aggregation. */
881 1.8.2.2 christos ure_write_2(sc, URE_USB_USB_CTRL, URE_MCU_TYPE_USB,
882 1.8.2.2 christos ure_read_2(sc, URE_USB_USB_CTRL, URE_MCU_TYPE_USB) &
883 1.8.2.2 christos ~URE_RX_AGG_DISABLE);
884 1.8.2.2 christos
885 1.8.2.2 christos val = ure_read_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB);
886 1.8.2.2 christos if (!(sc->ure_chip & (URE_CHIP_VER_5C00 | URE_CHIP_VER_5C10)))
887 1.8.2.2 christos val |= URE_U2P3_ENABLE;
888 1.8.2.2 christos else
889 1.8.2.2 christos val &= ~URE_U2P3_ENABLE;
890 1.8.2.2 christos ure_write_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB, val);
891 1.8.2.2 christos
892 1.8.2.2 christos memset(u1u2, 0xff, sizeof(u1u2));
893 1.8.2.2 christos ure_write_mem(sc, URE_USB_TOLERANCE,
894 1.8.2.2 christos URE_MCU_TYPE_USB | URE_BYTE_EN_SIX_BYTES, u1u2, sizeof(u1u2));
895 1.8.2.2 christos }
896 1.8.2.2 christos
897 1.8.2.2 christos static void
898 1.8.2.2 christos ure_disable_teredo(struct ure_softc *sc)
899 1.8.2.2 christos {
900 1.8.2.2 christos
901 1.8.2.2 christos ure_write_4(sc, URE_PLA_TEREDO_CFG, URE_MCU_TYPE_PLA,
902 1.8.2.2 christos ure_read_4(sc, URE_PLA_TEREDO_CFG, URE_MCU_TYPE_PLA) &
903 1.8.2.2 christos ~(URE_TEREDO_SEL | URE_TEREDO_RS_EVENT_MASK | URE_OOB_TEREDO_EN));
904 1.8.2.2 christos ure_write_2(sc, URE_PLA_WDT6_CTRL, URE_MCU_TYPE_PLA,
905 1.8.2.2 christos URE_WDT6_SET_MODE);
906 1.8.2.2 christos ure_write_2(sc, URE_PLA_REALWOW_TIMER, URE_MCU_TYPE_PLA, 0);
907 1.8.2.2 christos ure_write_4(sc, URE_PLA_TEREDO_TIMER, URE_MCU_TYPE_PLA, 0);
908 1.8.2.2 christos }
909 1.8.2.2 christos
910 1.8.2.2 christos static void
911 1.8.2.2 christos ure_init_fifo(struct ure_softc *sc)
912 1.8.2.2 christos {
913 1.8.2.2 christos uint32_t rx_fifo1, rx_fifo2;
914 1.8.2.2 christos int i;
915 1.8.2.2 christos
916 1.8.2.2 christos ure_write_2(sc, URE_PLA_MISC_1, URE_MCU_TYPE_PLA,
917 1.8.2.2 christos ure_read_2(sc, URE_PLA_MISC_1, URE_MCU_TYPE_PLA) |
918 1.8.2.2 christos URE_RXDY_GATED_EN);
919 1.8.2.2 christos
920 1.8.2.2 christos ure_disable_teredo(sc);
921 1.8.2.2 christos
922 1.8.2.2 christos ure_write_4(sc, URE_PLA_RCR, URE_MCU_TYPE_PLA,
923 1.8.2.2 christos ure_read_4(sc, URE_PLA_RCR, URE_MCU_TYPE_PLA) &
924 1.8.2.2 christos ~URE_RCR_ACPT_ALL);
925 1.8.2.2 christos
926 1.8.2.2 christos if (!(sc->ure_flags & URE_FLAG_8152)) {
927 1.8.2.2 christos if (sc->ure_chip & (URE_CHIP_VER_5C00 | URE_CHIP_VER_5C10 |
928 1.8.2.2 christos URE_CHIP_VER_5C20))
929 1.8.2.2 christos ure_ocp_reg_write(sc, URE_OCP_ADC_CFG,
930 1.8.2.2 christos URE_CKADSEL_L | URE_ADC_EN | URE_EN_EMI_L);
931 1.8.2.2 christos if (sc->ure_chip & URE_CHIP_VER_5C00)
932 1.8.2.2 christos ure_ocp_reg_write(sc, URE_OCP_EEE_CFG,
933 1.8.2.2 christos ure_ocp_reg_read(sc, URE_OCP_EEE_CFG) &
934 1.8.2.2 christos ~URE_CTAP_SHORT_EN);
935 1.8.2.2 christos ure_ocp_reg_write(sc, URE_OCP_POWER_CFG,
936 1.8.2.2 christos ure_ocp_reg_read(sc, URE_OCP_POWER_CFG) |
937 1.8.2.2 christos URE_EEE_CLKDIV_EN);
938 1.8.2.2 christos ure_ocp_reg_write(sc, URE_OCP_DOWN_SPEED,
939 1.8.2.2 christos ure_ocp_reg_read(sc, URE_OCP_DOWN_SPEED) |
940 1.8.2.2 christos URE_EN_10M_BGOFF);
941 1.8.2.2 christos ure_ocp_reg_write(sc, URE_OCP_POWER_CFG,
942 1.8.2.2 christos ure_ocp_reg_read(sc, URE_OCP_POWER_CFG) |
943 1.8.2.2 christos URE_EN_10M_PLLOFF);
944 1.8.2.2 christos ure_ocp_reg_write(sc, URE_OCP_SRAM_ADDR, URE_SRAM_IMPEDANCE);
945 1.8.2.2 christos ure_ocp_reg_write(sc, URE_OCP_SRAM_DATA, 0x0b13);
946 1.8.2.2 christos ure_write_2(sc, URE_PLA_PHY_PWR, URE_MCU_TYPE_PLA,
947 1.8.2.2 christos ure_read_2(sc, URE_PLA_PHY_PWR, URE_MCU_TYPE_PLA) |
948 1.8.2.2 christos URE_PFM_PWM_SWITCH);
949 1.8.2.2 christos
950 1.8.2.2 christos /* Enable LPF corner auto tune. */
951 1.8.2.2 christos ure_ocp_reg_write(sc, URE_OCP_SRAM_ADDR, URE_SRAM_LPF_CFG);
952 1.8.2.2 christos ure_ocp_reg_write(sc, URE_OCP_SRAM_DATA, 0xf70f);
953 1.8.2.2 christos
954 1.8.2.2 christos /* Adjust 10M amplitude. */
955 1.8.2.2 christos ure_ocp_reg_write(sc, URE_OCP_SRAM_ADDR, URE_SRAM_10M_AMP1);
956 1.8.2.2 christos ure_ocp_reg_write(sc, URE_OCP_SRAM_DATA, 0x00af);
957 1.8.2.2 christos ure_ocp_reg_write(sc, URE_OCP_SRAM_ADDR, URE_SRAM_10M_AMP2);
958 1.8.2.2 christos ure_ocp_reg_write(sc, URE_OCP_SRAM_DATA, 0x0208);
959 1.8.2.2 christos }
960 1.8.2.2 christos
961 1.8.2.2 christos ure_reset(sc);
962 1.8.2.2 christos
963 1.8.2.2 christos ure_write_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA, 0);
964 1.8.2.2 christos
965 1.8.2.2 christos ure_write_1(sc, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA,
966 1.8.2.2 christos ure_read_1(sc, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA) &
967 1.8.2.2 christos ~URE_NOW_IS_OOB);
968 1.8.2.2 christos
969 1.8.2.2 christos ure_write_2(sc, URE_PLA_SFF_STS_7, URE_MCU_TYPE_PLA,
970 1.8.2.2 christos ure_read_2(sc, URE_PLA_SFF_STS_7, URE_MCU_TYPE_PLA) &
971 1.8.2.2 christos ~URE_MCU_BORW_EN);
972 1.8.2.2 christos for (i = 0; i < URE_TIMEOUT; i++) {
973 1.8.2.2 christos if (ure_read_1(sc, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA) &
974 1.8.2.2 christos URE_LINK_LIST_READY)
975 1.8.2.2 christos break;
976 1.8.2.2 christos usbd_delay_ms(sc->ure_udev, 10);
977 1.8.2.2 christos }
978 1.8.2.2 christos if (i == URE_TIMEOUT)
979 1.8.2.2 christos URE_PRINTF(sc, "timeout waiting for OOB control\n");
980 1.8.2.2 christos ure_write_2(sc, URE_PLA_SFF_STS_7, URE_MCU_TYPE_PLA,
981 1.8.2.2 christos ure_read_2(sc, URE_PLA_SFF_STS_7, URE_MCU_TYPE_PLA) |
982 1.8.2.2 christos URE_RE_INIT_LL);
983 1.8.2.2 christos for (i = 0; i < URE_TIMEOUT; i++) {
984 1.8.2.2 christos if (ure_read_1(sc, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA) &
985 1.8.2.2 christos URE_LINK_LIST_READY)
986 1.8.2.2 christos break;
987 1.8.2.2 christos usbd_delay_ms(sc->ure_udev, 10);
988 1.8.2.2 christos }
989 1.8.2.2 christos if (i == URE_TIMEOUT)
990 1.8.2.2 christos URE_PRINTF(sc, "timeout waiting for OOB control\n");
991 1.8.2.2 christos
992 1.8.2.2 christos ure_write_2(sc, URE_PLA_CPCR, URE_MCU_TYPE_PLA,
993 1.8.2.2 christos ure_read_2(sc, URE_PLA_CPCR, URE_MCU_TYPE_PLA) &
994 1.8.2.2 christos ~URE_CPCR_RX_VLAN);
995 1.8.2.2 christos ure_write_2(sc, URE_PLA_TCR0, URE_MCU_TYPE_PLA,
996 1.8.2.2 christos ure_read_2(sc, URE_PLA_TCR0, URE_MCU_TYPE_PLA) |
997 1.8.2.2 christos URE_TCR0_AUTO_FIFO);
998 1.8.2.2 christos
999 1.8.2.2 christos /* Configure Rx FIFO threshold and coalescing. */
1000 1.8.2.2 christos ure_write_4(sc, URE_PLA_RXFIFO_CTRL0, URE_MCU_TYPE_PLA,
1001 1.8.2.2 christos URE_RXFIFO_THR1_NORMAL);
1002 1.8.2.2 christos if (sc->ure_udev->ud_speed == USB_SPEED_FULL) {
1003 1.8.2.2 christos rx_fifo1 = URE_RXFIFO_THR2_FULL;
1004 1.8.2.2 christos rx_fifo2 = URE_RXFIFO_THR3_FULL;
1005 1.8.2.2 christos } else {
1006 1.8.2.2 christos rx_fifo1 = URE_RXFIFO_THR2_HIGH;
1007 1.8.2.2 christos rx_fifo2 = URE_RXFIFO_THR3_HIGH;
1008 1.8.2.2 christos }
1009 1.8.2.2 christos ure_write_4(sc, URE_PLA_RXFIFO_CTRL1, URE_MCU_TYPE_PLA, rx_fifo1);
1010 1.8.2.2 christos ure_write_4(sc, URE_PLA_RXFIFO_CTRL2, URE_MCU_TYPE_PLA, rx_fifo2);
1011 1.8.2.2 christos
1012 1.8.2.2 christos /* Configure Tx FIFO threshold. */
1013 1.8.2.2 christos ure_write_4(sc, URE_PLA_TXFIFO_CTRL, URE_MCU_TYPE_PLA,
1014 1.8.2.2 christos URE_TXFIFO_THR_NORMAL);
1015 1.8.2.2 christos }
1016 1.8.2.2 christos
1017 1.8.2.2 christos int
1018 1.8.2.2 christos ure_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1019 1.8.2.2 christos {
1020 1.8.2.2 christos struct ure_softc *sc = ifp->if_softc;
1021 1.8.2.2 christos int s, error = 0, oflags = ifp->if_flags;
1022 1.8.2.2 christos
1023 1.8.2.2 christos s = splnet();
1024 1.8.2.2 christos
1025 1.8.2.2 christos switch (cmd) {
1026 1.8.2.2 christos case SIOCSIFFLAGS:
1027 1.8.2.2 christos if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1028 1.8.2.2 christos break;
1029 1.8.2.2 christos switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
1030 1.8.2.2 christos case IFF_RUNNING:
1031 1.8.2.2 christos ure_stop(ifp, 1);
1032 1.8.2.2 christos break;
1033 1.8.2.2 christos case IFF_UP:
1034 1.8.2.2 christos ure_init(ifp);
1035 1.8.2.2 christos break;
1036 1.8.2.2 christos case IFF_UP | IFF_RUNNING:
1037 1.8.2.2 christos if ((ifp->if_flags ^ oflags) == IFF_PROMISC)
1038 1.8.2.2 christos ure_iff(sc);
1039 1.8.2.2 christos else
1040 1.8.2.2 christos ure_init(ifp);
1041 1.8.2.2 christos }
1042 1.8.2.2 christos break;
1043 1.8.2.2 christos default:
1044 1.8.2.2 christos if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
1045 1.8.2.2 christos break;
1046 1.8.2.2 christos error = 0;
1047 1.8.2.2 christos if ((ifp->if_flags & IFF_RUNNING) == 0)
1048 1.8.2.2 christos break;
1049 1.8.2.2 christos switch (cmd) {
1050 1.8.2.2 christos case SIOCADDMULTI:
1051 1.8.2.2 christos case SIOCDELMULTI:
1052 1.8.2.2 christos ure_iff(sc);
1053 1.8.2.2 christos break;
1054 1.8.2.2 christos default:
1055 1.8.2.2 christos break;
1056 1.8.2.2 christos }
1057 1.8.2.2 christos }
1058 1.8.2.2 christos
1059 1.8.2.2 christos splx(s);
1060 1.8.2.2 christos
1061 1.8.2.2 christos return error;
1062 1.8.2.2 christos }
1063 1.8.2.2 christos
1064 1.8.2.2 christos static int
1065 1.8.2.2 christos ure_match(device_t parent, cfdata_t match, void *aux)
1066 1.8.2.2 christos {
1067 1.8.2.2 christos struct usb_attach_arg *uaa = aux;
1068 1.8.2.2 christos
1069 1.8.2.2 christos return usb_lookup(ure_devs, uaa->uaa_vendor, uaa->uaa_product) != NULL ?
1070 1.8.2.2 christos UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
1071 1.8.2.2 christos }
1072 1.8.2.2 christos
1073 1.8.2.2 christos static void
1074 1.8.2.2 christos ure_attach(device_t parent, device_t self, void *aux)
1075 1.8.2.2 christos {
1076 1.8.2.2 christos struct ure_softc *sc = device_private(self);
1077 1.8.2.2 christos struct usb_attach_arg *uaa = aux;
1078 1.8.2.2 christos struct usbd_device *dev = uaa->uaa_device;
1079 1.8.2.2 christos usb_interface_descriptor_t *id;
1080 1.8.2.2 christos usb_endpoint_descriptor_t *ed;
1081 1.8.2.2 christos struct ifnet *ifp;
1082 1.8.2.2 christos struct mii_data *mii;
1083 1.8.2.2 christos int error, i, s;
1084 1.8.2.2 christos uint16_t ver;
1085 1.8.2.2 christos uint8_t eaddr[8]; /* 2byte padded */
1086 1.8.2.2 christos char *devinfop;
1087 1.8.2.2 christos
1088 1.8.2.2 christos aprint_naive("\n");
1089 1.8.2.2 christos aprint_normal("\n");
1090 1.8.2.2 christos
1091 1.8.2.2 christos sc->ure_dev = self;
1092 1.8.2.2 christos sc->ure_udev = dev;
1093 1.8.2.2 christos
1094 1.8.2.2 christos devinfop = usbd_devinfo_alloc(sc->ure_udev, 0);
1095 1.8.2.2 christos aprint_normal_dev(self, "%s\n", devinfop);
1096 1.8.2.2 christos usbd_devinfo_free(devinfop);
1097 1.8.2.2 christos
1098 1.8.2.2 christos callout_init(&sc->ure_stat_ch, 0);
1099 1.8.2.2 christos usb_init_task(&sc->ure_tick_task, ure_tick_task, sc, 0);
1100 1.8.2.2 christos mutex_init(&sc->ure_mii_lock, MUTEX_DEFAULT, IPL_NONE);
1101 1.8.2.2 christos
1102 1.8.2.2 christos /*
1103 1.8.2.2 christos * ure_phyno is set to 0 below when configuration has succeeded.
1104 1.8.2.2 christos * if it is still -1 in detach, then ifmedia/mii/etc was not
1105 1.8.2.2 christos * setup and should not be torn down.
1106 1.8.2.2 christos */
1107 1.8.2.2 christos sc->ure_phyno = -1;
1108 1.8.2.2 christos
1109 1.8.2.2 christos #define URE_CONFIG_NO 1 /* XXX */
1110 1.8.2.2 christos error = usbd_set_config_no(dev, URE_CONFIG_NO, 1);
1111 1.8.2.2 christos if (error) {
1112 1.8.2.2 christos aprint_error_dev(self, "failed to set configuration: %s\n",
1113 1.8.2.2 christos usbd_errstr(error));
1114 1.8.2.2 christos return; /* XXX */
1115 1.8.2.2 christos }
1116 1.8.2.2 christos
1117 1.8.2.2 christos if (uaa->uaa_product == USB_PRODUCT_REALTEK_RTL8152)
1118 1.8.2.2 christos sc->ure_flags |= URE_FLAG_8152;
1119 1.8.2.2 christos
1120 1.8.2.2 christos #define URE_IFACE_IDX 0 /* XXX */
1121 1.8.2.2 christos error = usbd_device2interface_handle(dev, URE_IFACE_IDX, &sc->ure_iface);
1122 1.8.2.2 christos if (error) {
1123 1.8.2.2 christos aprint_error_dev(self, "failed to get interface handle: %s\n",
1124 1.8.2.2 christos usbd_errstr(error));
1125 1.8.2.2 christos return; /* XXX */
1126 1.8.2.2 christos }
1127 1.8.2.2 christos
1128 1.8.2.2 christos sc->ure_bufsz = 16 * 1024;
1129 1.8.2.2 christos
1130 1.8.2.2 christos id = usbd_get_interface_descriptor(sc->ure_iface);
1131 1.8.2.2 christos for (i = 0; i < id->bNumEndpoints; i++) {
1132 1.8.2.2 christos ed = usbd_interface2endpoint_descriptor(sc->ure_iface, i);
1133 1.8.2.2 christos if (ed == NULL) {
1134 1.8.2.2 christos aprint_error_dev(self, "couldn't get ep %d\n", i);
1135 1.8.2.2 christos return; /* XXX */
1136 1.8.2.2 christos }
1137 1.8.2.2 christos if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
1138 1.8.2.2 christos UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
1139 1.8.2.2 christos sc->ure_ed[URE_ENDPT_RX] = ed->bEndpointAddress;
1140 1.8.2.2 christos } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
1141 1.8.2.2 christos UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
1142 1.8.2.2 christos sc->ure_ed[URE_ENDPT_TX] = ed->bEndpointAddress;
1143 1.8.2.2 christos }
1144 1.8.2.2 christos }
1145 1.8.2.2 christos
1146 1.8.2.2 christos s = splnet();
1147 1.8.2.2 christos
1148 1.8.2.2 christos sc->ure_phyno = 0;
1149 1.8.2.2 christos
1150 1.8.2.2 christos ver = ure_read_2(sc, URE_PLA_TCR1, URE_MCU_TYPE_PLA) & URE_VERSION_MASK;
1151 1.8.2.2 christos switch (ver) {
1152 1.8.2.2 christos case 0x4c00:
1153 1.8.2.2 christos sc->ure_chip |= URE_CHIP_VER_4C00;
1154 1.8.2.2 christos break;
1155 1.8.2.2 christos case 0x4c10:
1156 1.8.2.2 christos sc->ure_chip |= URE_CHIP_VER_4C10;
1157 1.8.2.2 christos break;
1158 1.8.2.2 christos case 0x5c00:
1159 1.8.2.2 christos sc->ure_chip |= URE_CHIP_VER_5C00;
1160 1.8.2.2 christos break;
1161 1.8.2.2 christos case 0x5c10:
1162 1.8.2.2 christos sc->ure_chip |= URE_CHIP_VER_5C10;
1163 1.8.2.2 christos break;
1164 1.8.2.2 christos case 0x5c20:
1165 1.8.2.2 christos sc->ure_chip |= URE_CHIP_VER_5C20;
1166 1.8.2.2 christos break;
1167 1.8.2.2 christos case 0x5c30:
1168 1.8.2.2 christos sc->ure_chip |= URE_CHIP_VER_5C30;
1169 1.8.2.2 christos break;
1170 1.8.2.2 christos default:
1171 1.8.2.2 christos /* fake addr? or just fail? */
1172 1.8.2.2 christos break;
1173 1.8.2.2 christos }
1174 1.8.2.2 christos aprint_normal_dev(self, "RTL%d %sver %04x\n",
1175 1.8.2.2 christos (sc->ure_flags & URE_FLAG_8152) ? 8152 : 8153,
1176 1.8.2.2 christos (sc->ure_chip != 0) ? "" : "unknown ",
1177 1.8.2.2 christos ver);
1178 1.8.2.2 christos
1179 1.8.2.2 christos if (sc->ure_flags & URE_FLAG_8152)
1180 1.8.2.2 christos ure_rtl8152_init(sc);
1181 1.8.2.2 christos else
1182 1.8.2.2 christos ure_rtl8153_init(sc);
1183 1.8.2.2 christos
1184 1.8.2.2 christos if (sc->ure_chip & URE_CHIP_VER_4C00)
1185 1.8.2.2 christos ure_read_mem(sc, URE_PLA_IDR, URE_MCU_TYPE_PLA, eaddr,
1186 1.8.2.2 christos sizeof(eaddr));
1187 1.8.2.2 christos else
1188 1.8.2.2 christos ure_read_mem(sc, URE_PLA_BACKUP, URE_MCU_TYPE_PLA, eaddr,
1189 1.8.2.2 christos sizeof(eaddr));
1190 1.8.2.2 christos
1191 1.8.2.2 christos aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
1192 1.8.2.2 christos
1193 1.8.2.2 christos ifp = GET_IFP(sc);
1194 1.8.2.2 christos ifp->if_softc = sc;
1195 1.8.2.2 christos strlcpy(ifp->if_xname, device_xname(sc->ure_dev), IFNAMSIZ);
1196 1.8.2.2 christos ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1197 1.8.2.2 christos ifp->if_init = ure_init;
1198 1.8.2.2 christos ifp->if_ioctl = ure_ioctl;
1199 1.8.2.2 christos ifp->if_start = ure_start;
1200 1.8.2.2 christos ifp->if_stop = ure_stop;
1201 1.8.2.2 christos
1202 1.8.2.2 christos /*
1203 1.8.2.2 christos * We don't support TSOv4 and v6 for now, that are required to
1204 1.8.2.2 christos * be handled in software for some cases.
1205 1.8.2.2 christos */
1206 1.8.2.2 christos ifp->if_capabilities = IFCAP_CSUM_IPv4_Tx |
1207 1.8.2.2 christos IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_UDPv4_Tx;
1208 1.8.2.2 christos #ifdef INET6
1209 1.8.2.2 christos ifp->if_capabilities |= IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_UDPv6_Tx;
1210 1.8.2.2 christos #endif
1211 1.8.2.2 christos if (sc->ure_chip & ~URE_CHIP_VER_4C00) {
1212 1.8.2.2 christos ifp->if_capabilities |= IFCAP_CSUM_IPv4_Rx |
1213 1.8.2.2 christos IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx |
1214 1.8.2.2 christos IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx;
1215 1.8.2.2 christos }
1216 1.8.2.2 christos sc->ure_ec.ec_capabilities = ETHERCAP_VLAN_MTU;
1217 1.8.2.2 christos #ifdef notyet
1218 1.8.2.2 christos sc->ure_ec.ec_capabilities |= ETHERCAP_JUMBO_MTU;
1219 1.8.2.2 christos #endif
1220 1.8.2.2 christos
1221 1.8.2.2 christos IFQ_SET_READY(&ifp->if_snd);
1222 1.8.2.2 christos
1223 1.8.2.2 christos mii = GET_MII(sc);
1224 1.8.2.2 christos mii->mii_ifp = ifp;
1225 1.8.2.2 christos mii->mii_readreg = ure_miibus_readreg;
1226 1.8.2.2 christos mii->mii_writereg = ure_miibus_writereg;
1227 1.8.2.2 christos mii->mii_statchg = ure_miibus_statchg;
1228 1.8.2.2 christos mii->mii_flags = MIIF_AUTOTSLEEP;
1229 1.8.2.2 christos
1230 1.8.2.2 christos sc->ure_ec.ec_mii = mii;
1231 1.8.2.2 christos ifmedia_init(&mii->mii_media, 0, ure_ifmedia_upd, ure_ifmedia_sts);
1232 1.8.2.2 christos mii_attach(self, mii, 0xffffffff, sc->ure_phyno, MII_OFFSET_ANY, 0);
1233 1.8.2.2 christos
1234 1.8.2.2 christos if (LIST_FIRST(&mii->mii_phys) == NULL) {
1235 1.8.2.2 christos ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
1236 1.8.2.2 christos ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
1237 1.8.2.2 christos } else
1238 1.8.2.2 christos ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
1239 1.8.2.2 christos
1240 1.8.2.2 christos if_attach(ifp);
1241 1.8.2.2 christos ether_ifattach(ifp, eaddr);
1242 1.8.2.2 christos
1243 1.8.2.2 christos rnd_attach_source(&sc->ure_rnd_source, device_xname(sc->ure_dev),
1244 1.8.2.2 christos RND_TYPE_NET, RND_FLAG_DEFAULT);
1245 1.8.2.2 christos
1246 1.8.2.2 christos splx(s);
1247 1.8.2.2 christos
1248 1.8.2.2 christos usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->ure_udev, sc->ure_dev);
1249 1.8.2.2 christos
1250 1.8.2.2 christos if (!pmf_device_register(self, NULL, NULL))
1251 1.8.2.2 christos aprint_error_dev(self, "couldn't establish power handler\n");
1252 1.8.2.2 christos }
1253 1.8.2.2 christos
1254 1.8.2.2 christos static int
1255 1.8.2.2 christos ure_detach(device_t self, int flags)
1256 1.8.2.2 christos {
1257 1.8.2.2 christos struct ure_softc *sc = device_private(self);
1258 1.8.2.2 christos struct ifnet *ifp = GET_IFP(sc);
1259 1.8.2.2 christos int s;
1260 1.8.2.2 christos
1261 1.8.2.2 christos pmf_device_deregister(self);
1262 1.8.2.2 christos
1263 1.8.2.2 christos sc->ure_dying = true;
1264 1.8.2.2 christos
1265 1.8.2.2 christos callout_halt(&sc->ure_stat_ch, NULL);
1266 1.8.2.2 christos
1267 1.8.2.2 christos if (sc->ure_ep[URE_ENDPT_TX] != NULL)
1268 1.8.2.2 christos usbd_abort_pipe(sc->ure_ep[URE_ENDPT_TX]);
1269 1.8.2.2 christos if (sc->ure_ep[URE_ENDPT_RX] != NULL)
1270 1.8.2.2 christos usbd_abort_pipe(sc->ure_ep[URE_ENDPT_RX]);
1271 1.8.2.2 christos
1272 1.8.2.2 christos usb_rem_task_wait(sc->ure_udev, &sc->ure_tick_task, USB_TASKQ_DRIVER,
1273 1.8.2.2 christos NULL);
1274 1.8.2.2 christos
1275 1.8.2.2 christos s = splusb();
1276 1.8.2.2 christos
1277 1.8.2.2 christos /* partial-attach, below items weren't configured. */
1278 1.8.2.2 christos if (sc->ure_phyno != -1) {
1279 1.8.2.2 christos if (ifp->if_flags & IFF_RUNNING)
1280 1.8.2.2 christos ure_stop(ifp, 1);
1281 1.8.2.2 christos
1282 1.8.2.2 christos rnd_detach_source(&sc->ure_rnd_source);
1283 1.8.2.2 christos mii_detach(&sc->ure_mii, MII_PHY_ANY, MII_OFFSET_ANY);
1284 1.8.2.2 christos ifmedia_delete_instance(&sc->ure_mii.mii_media, IFM_INST_ANY);
1285 1.8.2.2 christos if (ifp->if_softc != NULL) {
1286 1.8.2.2 christos ether_ifdetach(ifp);
1287 1.8.2.2 christos if_detach(ifp);
1288 1.8.2.2 christos }
1289 1.8.2.2 christos }
1290 1.8.2.2 christos
1291 1.8.2.2 christos if (--sc->ure_refcnt >= 0) {
1292 1.8.2.2 christos /* Wait for processes to go away. */
1293 1.8.2.2 christos usb_detach_waitold(sc->ure_dev);
1294 1.8.2.2 christos }
1295 1.8.2.2 christos splx(s);
1296 1.8.2.2 christos
1297 1.8.2.2 christos callout_destroy(&sc->ure_stat_ch);
1298 1.8.2.2 christos mutex_destroy(&sc->ure_mii_lock);
1299 1.8.2.2 christos
1300 1.8.2.2 christos usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->ure_udev, sc->ure_dev);
1301 1.8.2.2 christos
1302 1.8.2.2 christos return 0;
1303 1.8.2.2 christos }
1304 1.8.2.2 christos
1305 1.8.2.2 christos static int
1306 1.8.2.2 christos ure_activate(device_t self, enum devact act)
1307 1.8.2.2 christos {
1308 1.8.2.2 christos struct ure_softc *sc = device_private(self);
1309 1.8.2.2 christos struct ifnet *ifp = GET_IFP(sc);
1310 1.8.2.2 christos
1311 1.8.2.2 christos switch (act) {
1312 1.8.2.2 christos case DVACT_DEACTIVATE:
1313 1.8.2.2 christos if_deactivate(ifp);
1314 1.8.2.2 christos sc->ure_dying = true;
1315 1.8.2.2 christos return 0;
1316 1.8.2.2 christos default:
1317 1.8.2.2 christos return EOPNOTSUPP;
1318 1.8.2.2 christos }
1319 1.8.2.2 christos return 0;
1320 1.8.2.2 christos }
1321 1.8.2.2 christos
1322 1.8.2.2 christos static void
1323 1.8.2.2 christos ure_tick_task(void *xsc)
1324 1.8.2.2 christos {
1325 1.8.2.2 christos struct ure_softc *sc = xsc;
1326 1.8.2.2 christos struct ifnet *ifp = GET_IFP(sc);
1327 1.8.2.2 christos struct mii_data *mii;
1328 1.8.2.2 christos int s;
1329 1.8.2.2 christos
1330 1.8.2.2 christos if (sc == NULL)
1331 1.8.2.2 christos return;
1332 1.8.2.2 christos
1333 1.8.2.2 christos if (sc->ure_dying)
1334 1.8.2.2 christos return;
1335 1.8.2.2 christos
1336 1.8.2.2 christos mii = GET_MII(sc);
1337 1.8.2.2 christos
1338 1.8.2.2 christos s = splnet();
1339 1.8.2.2 christos mii_tick(mii);
1340 1.8.2.2 christos if ((sc->ure_flags & URE_FLAG_LINK) == 0)
1341 1.8.2.2 christos ure_miibus_statchg(ifp);
1342 1.8.2.2 christos callout_reset(&sc->ure_stat_ch, hz, ure_tick, sc);
1343 1.8.2.2 christos splx(s);
1344 1.8.2.2 christos }
1345 1.8.2.2 christos
1346 1.8.2.2 christos static void
1347 1.8.2.2 christos ure_lock_mii(struct ure_softc *sc)
1348 1.8.2.2 christos {
1349 1.8.2.2 christos
1350 1.8.2.2 christos sc->ure_refcnt++;
1351 1.8.2.2 christos mutex_enter(&sc->ure_mii_lock);
1352 1.8.2.2 christos }
1353 1.8.2.2 christos
1354 1.8.2.2 christos static void
1355 1.8.2.2 christos ure_unlock_mii(struct ure_softc *sc)
1356 1.8.2.2 christos {
1357 1.8.2.2 christos
1358 1.8.2.2 christos mutex_exit(&sc->ure_mii_lock);
1359 1.8.2.2 christos if (--sc->ure_refcnt < 0)
1360 1.8.2.2 christos usb_detach_wakeupold(sc->ure_dev);
1361 1.8.2.2 christos }
1362 1.8.2.2 christos
1363 1.8.2.2 christos static void
1364 1.8.2.2 christos ure_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1365 1.8.2.2 christos {
1366 1.8.2.2 christos struct ure_chain *c = (struct ure_chain *)priv;
1367 1.8.2.2 christos struct ure_softc *sc = c->uc_sc;
1368 1.8.2.2 christos struct ifnet *ifp = GET_IFP(sc);
1369 1.8.2.2 christos uint8_t *buf = c->uc_buf;
1370 1.8.2.2 christos uint32_t total_len;
1371 1.8.2.2 christos uint16_t pktlen = 0;
1372 1.8.2.2 christos struct mbuf *m;
1373 1.8.2.2 christos int s;
1374 1.8.2.2 christos struct ure_rxpkt rxhdr;
1375 1.8.2.2 christos
1376 1.8.2.2 christos if (sc->ure_dying)
1377 1.8.2.2 christos return;
1378 1.8.2.2 christos
1379 1.8.2.2 christos if (!(ifp->if_flags & IFF_RUNNING))
1380 1.8.2.2 christos return;
1381 1.8.2.2 christos
1382 1.8.2.2 christos if (status != USBD_NORMAL_COMPLETION) {
1383 1.8.2.2 christos if (status == USBD_INVAL)
1384 1.8.2.2 christos return; /* XXX plugged out or down */
1385 1.8.2.2 christos if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1386 1.8.2.2 christos return;
1387 1.8.2.2 christos if (usbd_ratecheck(&sc->ure_rx_notice))
1388 1.8.2.2 christos URE_PRINTF(sc, "usb errors on rx: %s\n",
1389 1.8.2.2 christos usbd_errstr(status));
1390 1.8.2.2 christos if (status == USBD_STALLED)
1391 1.8.2.2 christos usbd_clear_endpoint_stall_async(
1392 1.8.2.2 christos sc->ure_ep[URE_ENDPT_RX]);
1393 1.8.2.2 christos goto done;
1394 1.8.2.2 christos }
1395 1.8.2.2 christos
1396 1.8.2.2 christos usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1397 1.8.2.2 christos DPRINTFN(3, ("received %d bytes\n", total_len));
1398 1.8.2.2 christos
1399 1.8.2.2 christos KASSERTMSG(total_len <= sc->ure_bufsz, "%u vs %u",
1400 1.8.2.2 christos total_len, sc->ure_bufsz);
1401 1.8.2.2 christos
1402 1.8.2.2 christos do {
1403 1.8.2.2 christos if (total_len < sizeof(rxhdr)) {
1404 1.8.2.2 christos DPRINTF(("too few bytes left for a packet header\n"));
1405 1.8.2.2 christos ifp->if_ierrors++;
1406 1.8.2.2 christos goto done;
1407 1.8.2.2 christos }
1408 1.8.2.2 christos
1409 1.8.2.2 christos buf += roundup(pktlen, 8);
1410 1.8.2.2 christos
1411 1.8.2.2 christos memcpy(&rxhdr, buf, sizeof(rxhdr));
1412 1.8.2.2 christos total_len -= sizeof(rxhdr);
1413 1.8.2.2 christos
1414 1.8.2.2 christos pktlen = le32toh(rxhdr.ure_pktlen) & URE_RXPKT_LEN_MASK;
1415 1.8.2.2 christos DPRINTFN(4, ("next packet is %d bytes\n", pktlen));
1416 1.8.2.2 christos if (pktlen > total_len) {
1417 1.8.2.2 christos DPRINTF(("not enough bytes left for next packet\n"));
1418 1.8.2.2 christos ifp->if_ierrors++;
1419 1.8.2.2 christos goto done;
1420 1.8.2.2 christos }
1421 1.8.2.2 christos
1422 1.8.2.2 christos total_len -= roundup(pktlen, 8);
1423 1.8.2.2 christos buf += sizeof(rxhdr);
1424 1.8.2.2 christos
1425 1.8.2.2 christos m = m_devget(buf, pktlen - ETHER_CRC_LEN, 0, ifp);
1426 1.8.2.2 christos if (m == NULL) {
1427 1.8.2.2 christos DPRINTF(("unable to allocate mbuf for next packet\n"));
1428 1.8.2.2 christos ifp->if_ierrors++;
1429 1.8.2.2 christos goto done;
1430 1.8.2.2 christos }
1431 1.8.2.2 christos
1432 1.8.2.2 christos m->m_pkthdr.csum_flags = ure_rxcsum(ifp, &rxhdr);
1433 1.8.2.2 christos
1434 1.8.2.2 christos s = splnet();
1435 1.8.2.2 christos if_percpuq_enqueue(ifp->if_percpuq, m);
1436 1.8.2.2 christos splx(s);
1437 1.8.2.2 christos } while (total_len > 0);
1438 1.8.2.2 christos
1439 1.8.2.2 christos done:
1440 1.8.2.2 christos usbd_setup_xfer(xfer, c, c->uc_buf, sc->ure_bufsz,
1441 1.8.2.2 christos USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ure_rxeof);
1442 1.8.2.2 christos usbd_transfer(xfer);
1443 1.8.2.2 christos }
1444 1.8.2.2 christos
1445 1.8.2.2 christos static int
1446 1.8.2.2 christos ure_rxcsum(struct ifnet *ifp, struct ure_rxpkt *rp)
1447 1.8.2.2 christos {
1448 1.8.2.2 christos int enabled = ifp->if_csum_flags_rx, flags = 0;
1449 1.8.2.2 christos uint32_t csum, misc;
1450 1.8.2.2 christos
1451 1.8.2.2 christos if (enabled == 0)
1452 1.8.2.2 christos return 0;
1453 1.8.2.2 christos
1454 1.8.2.2 christos csum = le32toh(rp->ure_csum);
1455 1.8.2.2 christos misc = le32toh(rp->ure_misc);
1456 1.8.2.2 christos
1457 1.8.2.2 christos if (csum & URE_RXPKT_IPV4_CS) {
1458 1.8.2.2 christos flags |= M_CSUM_IPv4;
1459 1.8.2.2 christos if (csum & URE_RXPKT_TCP_CS)
1460 1.8.2.2 christos flags |= M_CSUM_TCPv4;
1461 1.8.2.2 christos if (csum & URE_RXPKT_UDP_CS)
1462 1.8.2.2 christos flags |= M_CSUM_UDPv4;
1463 1.8.2.2 christos } else if (csum & URE_RXPKT_IPV6_CS) {
1464 1.8.2.2 christos flags = 0;
1465 1.8.2.2 christos if (csum & URE_RXPKT_TCP_CS)
1466 1.8.2.2 christos flags |= M_CSUM_TCPv6;
1467 1.8.2.2 christos if (csum & URE_RXPKT_UDP_CS)
1468 1.8.2.2 christos flags |= M_CSUM_UDPv6;
1469 1.8.2.2 christos }
1470 1.8.2.2 christos
1471 1.8.2.2 christos flags &= enabled;
1472 1.8.2.2 christos if (__predict_false((flags & M_CSUM_IPv4) &&
1473 1.8.2.2 christos (misc & URE_RXPKT_IP_F)))
1474 1.8.2.2 christos flags |= M_CSUM_IPv4_BAD;
1475 1.8.2.2 christos if (__predict_false(
1476 1.8.2.2 christos ((flags & (M_CSUM_TCPv4 | M_CSUM_TCPv6)) && (misc & URE_RXPKT_TCP_F))
1477 1.8.2.2 christos || ((flags & (M_CSUM_UDPv4 | M_CSUM_UDPv6)) && (misc & URE_RXPKT_UDP_F))
1478 1.8.2.2 christos ))
1479 1.8.2.2 christos flags |= M_CSUM_TCP_UDP_BAD;
1480 1.8.2.2 christos
1481 1.8.2.2 christos return flags;
1482 1.8.2.2 christos }
1483 1.8.2.2 christos
1484 1.8.2.2 christos static void
1485 1.8.2.2 christos ure_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1486 1.8.2.2 christos {
1487 1.8.2.2 christos struct ure_chain *c = priv;
1488 1.8.2.2 christos struct ure_softc *sc = c->uc_sc;
1489 1.8.2.2 christos struct ure_cdata *cd = &sc->ure_cdata;
1490 1.8.2.2 christos struct ifnet *ifp = GET_IFP(sc);
1491 1.8.2.2 christos int s;
1492 1.8.2.2 christos
1493 1.8.2.2 christos if (sc->ure_dying)
1494 1.8.2.2 christos return;
1495 1.8.2.2 christos
1496 1.8.2.2 christos DPRINTFN(2, ("tx completion\n"));
1497 1.8.2.2 christos
1498 1.8.2.2 christos s = splnet();
1499 1.8.2.2 christos
1500 1.8.2.2 christos KASSERT(cd->tx_cnt > 0);
1501 1.8.2.2 christos cd->tx_cnt--;
1502 1.8.2.2 christos
1503 1.8.2.2 christos if (status != USBD_NORMAL_COMPLETION) {
1504 1.8.2.2 christos if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1505 1.8.2.2 christos splx(s);
1506 1.8.2.2 christos return;
1507 1.8.2.2 christos }
1508 1.8.2.2 christos ifp->if_oerrors++;
1509 1.8.2.2 christos if (usbd_ratecheck(&sc->ure_tx_notice))
1510 1.8.2.2 christos URE_PRINTF(sc, "usb error on tx: %s\n",
1511 1.8.2.2 christos usbd_errstr(status));
1512 1.8.2.2 christos if (status == USBD_STALLED)
1513 1.8.2.2 christos usbd_clear_endpoint_stall_async(
1514 1.8.2.2 christos sc->ure_ep[URE_ENDPT_TX]);
1515 1.8.2.2 christos splx(s);
1516 1.8.2.2 christos return;
1517 1.8.2.2 christos }
1518 1.8.2.2 christos
1519 1.8.2.2 christos ifp->if_flags &= ~IFF_OACTIVE;
1520 1.8.2.2 christos
1521 1.8.2.2 christos if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1522 1.8.2.2 christos ure_start(ifp);
1523 1.8.2.2 christos
1524 1.8.2.2 christos splx(s);
1525 1.8.2.2 christos }
1526 1.8.2.2 christos
1527 1.8.2.2 christos static int
1528 1.8.2.2 christos ure_tx_list_init(struct ure_softc *sc)
1529 1.8.2.2 christos {
1530 1.8.2.2 christos struct ure_cdata *cd;
1531 1.8.2.2 christos struct ure_chain *c;
1532 1.8.2.2 christos int i, error;
1533 1.8.2.2 christos
1534 1.8.2.2 christos cd = &sc->ure_cdata;
1535 1.8.2.2 christos for (i = 0; i < URE_TX_LIST_CNT; i++) {
1536 1.8.2.2 christos c = &cd->tx_chain[i];
1537 1.8.2.2 christos c->uc_sc = sc;
1538 1.8.2.2 christos if (c->uc_xfer == NULL) {
1539 1.8.2.2 christos error = usbd_create_xfer(sc->ure_ep[URE_ENDPT_TX],
1540 1.8.2.2 christos sc->ure_bufsz, USBD_FORCE_SHORT_XFER, 0,
1541 1.8.2.2 christos &c->uc_xfer);
1542 1.8.2.2 christos if (error)
1543 1.8.2.2 christos return error;
1544 1.8.2.2 christos c->uc_buf = usbd_get_buffer(c->uc_xfer);
1545 1.8.2.2 christos }
1546 1.8.2.2 christos }
1547 1.8.2.2 christos
1548 1.8.2.2 christos cd->tx_prod = cd->tx_cnt = 0;
1549 1.8.2.2 christos
1550 1.8.2.2 christos return 0;
1551 1.8.2.2 christos }
1552 1.8.2.2 christos
1553 1.8.2.2 christos static int
1554 1.8.2.2 christos ure_rx_list_init(struct ure_softc *sc)
1555 1.8.2.2 christos {
1556 1.8.2.2 christos struct ure_cdata *cd;
1557 1.8.2.2 christos struct ure_chain *c;
1558 1.8.2.2 christos int i, error;
1559 1.8.2.2 christos
1560 1.8.2.2 christos cd = &sc->ure_cdata;
1561 1.8.2.2 christos for (i = 0; i < URE_RX_LIST_CNT; i++) {
1562 1.8.2.2 christos c = &cd->rx_chain[i];
1563 1.8.2.2 christos c->uc_sc = sc;
1564 1.8.2.2 christos error = usbd_create_xfer(sc->ure_ep[URE_ENDPT_RX],
1565 1.8.2.2 christos sc->ure_bufsz, 0, 0, &c->uc_xfer);
1566 1.8.2.2 christos if (error)
1567 1.8.2.2 christos return error;
1568 1.8.2.2 christos c->uc_buf = usbd_get_buffer(c->uc_xfer);
1569 1.8.2.2 christos }
1570 1.8.2.2 christos
1571 1.8.2.2 christos return 0;
1572 1.8.2.2 christos }
1573 1.8.2.2 christos
1574 1.8.2.2 christos static int
1575 1.8.2.2 christos ure_encap(struct ure_softc *sc, struct mbuf *m, int idx)
1576 1.8.2.2 christos {
1577 1.8.2.2 christos struct ifnet *ifp = GET_IFP(sc);
1578 1.8.2.2 christos struct ure_chain *c;
1579 1.8.2.2 christos usbd_status err;
1580 1.8.2.2 christos struct ure_txpkt txhdr;
1581 1.8.2.2 christos uint32_t frm_len = 0;
1582 1.8.2.2 christos uint8_t *buf;
1583 1.8.2.2 christos
1584 1.8.2.2 christos c = &sc->ure_cdata.tx_chain[idx];
1585 1.8.2.2 christos buf = c->uc_buf;
1586 1.8.2.2 christos
1587 1.8.2.2 christos /* header */
1588 1.8.2.2 christos txhdr.ure_pktlen = htole32(m->m_pkthdr.len | URE_TXPKT_TX_FS |
1589 1.8.2.2 christos URE_TXPKT_TX_LS);
1590 1.8.2.2 christos txhdr.ure_csum = htole32(ure_txcsum(m));
1591 1.8.2.2 christos memcpy(buf, &txhdr, sizeof(txhdr));
1592 1.8.2.2 christos buf += sizeof(txhdr);
1593 1.8.2.2 christos frm_len = sizeof(txhdr);
1594 1.8.2.2 christos
1595 1.8.2.2 christos /* packet */
1596 1.8.2.2 christos m_copydata(m, 0, m->m_pkthdr.len, buf);
1597 1.8.2.2 christos frm_len += m->m_pkthdr.len;
1598 1.8.2.2 christos
1599 1.8.2.2 christos if (__predict_false(c->uc_xfer == NULL))
1600 1.8.2.2 christos return EIO; /* XXX plugged out or down */
1601 1.8.2.2 christos
1602 1.8.2.2 christos DPRINTFN(2, ("tx %d bytes\n", frm_len));
1603 1.8.2.2 christos usbd_setup_xfer(c->uc_xfer, c, c->uc_buf, frm_len,
1604 1.8.2.2 christos USBD_FORCE_SHORT_XFER, 10000, ure_txeof);
1605 1.8.2.2 christos
1606 1.8.2.2 christos err = usbd_transfer(c->uc_xfer);
1607 1.8.2.2 christos if (err != USBD_IN_PROGRESS) {
1608 1.8.2.2 christos ure_stop(ifp, 0);
1609 1.8.2.2 christos return EIO;
1610 1.8.2.2 christos }
1611 1.8.2.2 christos
1612 1.8.2.2 christos return 0;
1613 1.8.2.2 christos }
1614 1.8.2.2 christos
1615 1.8.2.2 christos /*
1616 1.8.2.2 christos * We need to calculate L4 checksum in software, if the offset of
1617 1.8.2.2 christos * L4 header is larger than 0x7ff = 2047.
1618 1.8.2.2 christos */
1619 1.8.2.2 christos static uint32_t
1620 1.8.2.2 christos ure_txcsum(struct mbuf *m)
1621 1.8.2.2 christos {
1622 1.8.2.2 christos struct ether_header *eh;
1623 1.8.2.2 christos int flags = m->m_pkthdr.csum_flags;
1624 1.8.2.2 christos uint32_t data = m->m_pkthdr.csum_data;
1625 1.8.2.2 christos uint32_t reg = 0;
1626 1.8.2.2 christos int l3off, l4off;
1627 1.8.2.2 christos uint16_t type;
1628 1.8.2.2 christos
1629 1.8.2.2 christos if (flags == 0)
1630 1.8.2.2 christos return 0;
1631 1.8.2.2 christos
1632 1.8.2.2 christos if (__predict_true(m->m_len >= (int)sizeof(*eh))) {
1633 1.8.2.2 christos eh = mtod(m, struct ether_header *);
1634 1.8.2.2 christos type = eh->ether_type;
1635 1.8.2.2 christos } else
1636 1.8.2.2 christos m_copydata(m, offsetof(struct ether_header, ether_type),
1637 1.8.2.2 christos sizeof(type), &type);
1638 1.8.2.2 christos switch (type = htons(type)) {
1639 1.8.2.2 christos case ETHERTYPE_IP:
1640 1.8.2.2 christos case ETHERTYPE_IPV6:
1641 1.8.2.2 christos l3off = ETHER_HDR_LEN;
1642 1.8.2.2 christos break;
1643 1.8.2.2 christos case ETHERTYPE_VLAN:
1644 1.8.2.2 christos l3off = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
1645 1.8.2.2 christos break;
1646 1.8.2.2 christos default:
1647 1.8.2.2 christos return 0;
1648 1.8.2.2 christos }
1649 1.8.2.2 christos
1650 1.8.2.2 christos if (flags & (M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
1651 1.8.2.2 christos l4off = l3off + M_CSUM_DATA_IPv4_IPHL(data);
1652 1.8.2.2 christos if (__predict_false(l4off > URE_L4_OFFSET_MAX)) {
1653 1.8.2.2 christos in_undefer_cksum(m, l3off, flags);
1654 1.8.2.2 christos return 0;
1655 1.8.2.2 christos }
1656 1.8.2.2 christos reg |= URE_TXPKT_IPV4_CS;
1657 1.8.2.2 christos if (flags & M_CSUM_TCPv4)
1658 1.8.2.2 christos reg |= URE_TXPKT_TCP_CS;
1659 1.8.2.2 christos else
1660 1.8.2.2 christos reg |= URE_TXPKT_UDP_CS;
1661 1.8.2.2 christos reg |= l4off << URE_L4_OFFSET_SHIFT;
1662 1.8.2.2 christos }
1663 1.8.2.2 christos #ifdef INET6
1664 1.8.2.2 christos else if (flags & (M_CSUM_TCPv6 | M_CSUM_UDPv6)) {
1665 1.8.2.2 christos l4off = l3off + M_CSUM_DATA_IPv6_IPHL(data);
1666 1.8.2.2 christos if (__predict_false(l4off > URE_L4_OFFSET_MAX)) {
1667 1.8.2.2 christos in6_undefer_cksum(m, l3off, flags);
1668 1.8.2.2 christos return 0;
1669 1.8.2.2 christos }
1670 1.8.2.2 christos reg |= URE_TXPKT_IPV6_CS;
1671 1.8.2.2 christos if (flags & M_CSUM_TCPv6)
1672 1.8.2.2 christos reg |= URE_TXPKT_TCP_CS;
1673 1.8.2.2 christos else
1674 1.8.2.2 christos reg |= URE_TXPKT_UDP_CS;
1675 1.8.2.2 christos reg |= l4off << URE_L4_OFFSET_SHIFT;
1676 1.8.2.2 christos }
1677 1.8.2.2 christos #endif
1678 1.8.2.2 christos else if (flags & M_CSUM_IPv4)
1679 1.8.2.2 christos reg |= URE_TXPKT_IPV4_CS;
1680 1.8.2.2 christos
1681 1.8.2.2 christos return reg;
1682 1.8.2.2 christos }
1683