if_mue.c revision 1.50.2.3 1 1.50.2.3 martin /* $NetBSD: if_mue.c,v 1.50.2.3 2020/04/13 08:04:49 martin Exp $ */
2 1.50.2.2 christos /* $OpenBSD: if_mue.c,v 1.3 2018/08/04 16:42:46 jsg Exp $ */
3 1.50.2.2 christos
4 1.50.2.2 christos /*
5 1.50.2.2 christos * Copyright (c) 2018 Kevin Lo <kevlo (at) openbsd.org>
6 1.50.2.2 christos *
7 1.50.2.2 christos * Permission to use, copy, modify, and distribute this software for any
8 1.50.2.2 christos * purpose with or without fee is hereby granted, provided that the above
9 1.50.2.2 christos * copyright notice and this permission notice appear in all copies.
10 1.50.2.2 christos *
11 1.50.2.2 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.50.2.2 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.50.2.2 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.50.2.2 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.50.2.2 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.50.2.2 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.50.2.2 christos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.50.2.2 christos */
19 1.50.2.2 christos
20 1.50.2.2 christos /* Driver for Microchip LAN7500/LAN7800 chipsets. */
21 1.50.2.2 christos
22 1.50.2.2 christos #include <sys/cdefs.h>
23 1.50.2.3 martin __KERNEL_RCSID(0, "$NetBSD: if_mue.c,v 1.50.2.3 2020/04/13 08:04:49 martin Exp $");
24 1.50.2.2 christos
25 1.50.2.2 christos #ifdef _KERNEL_OPT
26 1.50.2.2 christos #include "opt_usb.h"
27 1.50.2.2 christos #include "opt_inet.h"
28 1.50.2.2 christos #endif
29 1.50.2.2 christos
30 1.50.2.2 christos #include <sys/param.h>
31 1.50.2.3 martin
32 1.50.2.3 martin #include <dev/usb/usbnet.h>
33 1.50.2.2 christos
34 1.50.2.2 christos #include <dev/usb/if_muereg.h>
35 1.50.2.2 christos #include <dev/usb/if_muevar.h>
36 1.50.2.2 christos
37 1.50.2.3 martin #define MUE_PRINTF(un, fmt, args...) \
38 1.50.2.3 martin device_printf((un)->un_dev, "%s: " fmt, __func__, ##args);
39 1.50.2.2 christos
40 1.50.2.2 christos #ifdef USB_DEBUG
41 1.50.2.2 christos int muedebug = 0;
42 1.50.2.3 martin #define DPRINTF(un, fmt, args...) \
43 1.50.2.2 christos do { \
44 1.50.2.2 christos if (muedebug) \
45 1.50.2.3 martin MUE_PRINTF(un, fmt, ##args); \
46 1.50.2.2 christos } while (0 /* CONSTCOND */)
47 1.50.2.2 christos #else
48 1.50.2.3 martin #define DPRINTF(un, fmt, args...) __nothing
49 1.50.2.2 christos #endif
50 1.50.2.2 christos
51 1.50.2.2 christos /*
52 1.50.2.2 christos * Various supported device vendors/products.
53 1.50.2.2 christos */
54 1.50.2.2 christos struct mue_type {
55 1.50.2.2 christos struct usb_devno mue_dev;
56 1.50.2.2 christos uint16_t mue_flags;
57 1.50.2.2 christos #define LAN7500 0x0001 /* LAN7500 */
58 1.50.2.3 martin #define LAN7800 0x0002 /* LAN7800 */
59 1.50.2.3 martin #define LAN7801 0x0004 /* LAN7801 */
60 1.50.2.3 martin #define LAN7850 0x0008 /* LAN7850 */
61 1.50.2.2 christos };
62 1.50.2.2 christos
63 1.50.2.3 martin static const struct mue_type mue_devs[] = {
64 1.50.2.2 christos { { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN7500 }, LAN7500 },
65 1.50.2.2 christos { { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN7505 }, LAN7500 },
66 1.50.2.3 martin { { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN7800 }, LAN7800 },
67 1.50.2.3 martin { { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN7801 }, LAN7801 },
68 1.50.2.3 martin { { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN7850 }, LAN7850 }
69 1.50.2.2 christos };
70 1.50.2.2 christos
71 1.50.2.2 christos #define MUE_LOOKUP(uaa) ((const struct mue_type *)usb_lookup(mue_devs, \
72 1.50.2.2 christos uaa->uaa_vendor, uaa->uaa_product))
73 1.50.2.2 christos
74 1.50.2.2 christos #define MUE_ENADDR_LO(enaddr) \
75 1.50.2.2 christos ((enaddr[3] << 24) | (enaddr[2] << 16) | (enaddr[1] << 8) | enaddr[0])
76 1.50.2.2 christos #define MUE_ENADDR_HI(enaddr) \
77 1.50.2.2 christos ((enaddr[5] << 8) | enaddr[4])
78 1.50.2.2 christos
79 1.50.2.2 christos static int mue_match(device_t, cfdata_t, void *);
80 1.50.2.2 christos static void mue_attach(device_t, device_t, void *);
81 1.50.2.2 christos
82 1.50.2.3 martin static uint32_t mue_csr_read(struct usbnet *, uint32_t);
83 1.50.2.3 martin static int mue_csr_write(struct usbnet *, uint32_t, uint32_t);
84 1.50.2.3 martin static int mue_wait_for_bits(struct usbnet *, uint32_t, uint32_t,
85 1.50.2.2 christos uint32_t, uint32_t);
86 1.50.2.3 martin static uint8_t mue_eeprom_getbyte(struct usbnet *, int, uint8_t *);
87 1.50.2.3 martin static bool mue_eeprom_present(struct usbnet *);
88 1.50.2.3 martin static void mue_dataport_write(struct usbnet *, uint32_t, uint32_t,
89 1.50.2.2 christos uint32_t, uint32_t *);
90 1.50.2.3 martin static void mue_init_ltm(struct usbnet *);
91 1.50.2.3 martin static int mue_chip_init(struct usbnet *);
92 1.50.2.3 martin static void mue_set_macaddr(struct usbnet *);
93 1.50.2.3 martin static int mue_get_macaddr(struct usbnet *, prop_dictionary_t);
94 1.50.2.3 martin static int mue_prepare_tso(struct usbnet *, struct mbuf *);
95 1.50.2.3 martin static void mue_setiff_locked(struct usbnet *);
96 1.50.2.3 martin static void mue_sethwcsum_locked(struct usbnet *);
97 1.50.2.3 martin static void mue_setmtu_locked(struct usbnet *);
98 1.50.2.3 martin static void mue_reset(struct usbnet *);
99 1.50.2.3 martin
100 1.50.2.3 martin static void mue_uno_stop(struct ifnet *, int);
101 1.50.2.3 martin static int mue_uno_ioctl(struct ifnet *, u_long, void *);
102 1.50.2.3 martin static int mue_uno_mii_read_reg(struct usbnet *, int, int, uint16_t *);
103 1.50.2.3 martin static int mue_uno_mii_write_reg(struct usbnet *, int, int, uint16_t);
104 1.50.2.3 martin static void mue_uno_mii_statchg(struct ifnet *);
105 1.50.2.3 martin static void mue_uno_rx_loop(struct usbnet *, struct usbnet_chain *,
106 1.50.2.3 martin uint32_t);
107 1.50.2.3 martin static unsigned mue_uno_tx_prepare(struct usbnet *, struct mbuf *,
108 1.50.2.3 martin struct usbnet_chain *);
109 1.50.2.3 martin static int mue_uno_init(struct ifnet *);
110 1.50.2.3 martin
111 1.50.2.3 martin static const struct usbnet_ops mue_ops = {
112 1.50.2.3 martin .uno_stop = mue_uno_stop,
113 1.50.2.3 martin .uno_ioctl = mue_uno_ioctl,
114 1.50.2.3 martin .uno_read_reg = mue_uno_mii_read_reg,
115 1.50.2.3 martin .uno_write_reg = mue_uno_mii_write_reg,
116 1.50.2.3 martin .uno_statchg = mue_uno_mii_statchg,
117 1.50.2.3 martin .uno_tx_prepare = mue_uno_tx_prepare,
118 1.50.2.3 martin .uno_rx_loop = mue_uno_rx_loop,
119 1.50.2.3 martin .uno_init = mue_uno_init,
120 1.50.2.3 martin };
121 1.50.2.2 christos
122 1.50.2.3 martin #define MUE_SETBIT(un, reg, x) \
123 1.50.2.3 martin mue_csr_write(un, reg, mue_csr_read(un, reg) | (x))
124 1.50.2.2 christos
125 1.50.2.3 martin #define MUE_CLRBIT(un, reg, x) \
126 1.50.2.3 martin mue_csr_write(un, reg, mue_csr_read(un, reg) & ~(x))
127 1.50.2.2 christos
128 1.50.2.3 martin #define MUE_WAIT_SET(un, reg, set, fail) \
129 1.50.2.3 martin mue_wait_for_bits(un, reg, set, ~0, fail)
130 1.50.2.2 christos
131 1.50.2.3 martin #define MUE_WAIT_CLR(un, reg, clear, fail) \
132 1.50.2.3 martin mue_wait_for_bits(un, reg, 0, clear, fail)
133 1.50.2.2 christos
134 1.50.2.2 christos #define ETHER_IS_VALID(addr) \
135 1.50.2.2 christos (!ETHER_IS_MULTICAST(addr) && !ETHER_IS_ZERO(addr))
136 1.50.2.2 christos
137 1.50.2.2 christos #define ETHER_IS_ZERO(addr) \
138 1.50.2.2 christos (!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]))
139 1.50.2.2 christos
140 1.50.2.3 martin CFATTACH_DECL_NEW(mue, sizeof(struct usbnet), mue_match, mue_attach,
141 1.50.2.3 martin usbnet_detach, usbnet_activate);
142 1.50.2.2 christos
143 1.50.2.2 christos static uint32_t
144 1.50.2.3 martin mue_csr_read(struct usbnet *un, uint32_t reg)
145 1.50.2.2 christos {
146 1.50.2.2 christos usb_device_request_t req;
147 1.50.2.2 christos usbd_status err;
148 1.50.2.2 christos uDWord val;
149 1.50.2.2 christos
150 1.50.2.3 martin if (usbnet_isdying(un))
151 1.50.2.2 christos return 0;
152 1.50.2.2 christos
153 1.50.2.2 christos USETDW(val, 0);
154 1.50.2.2 christos req.bmRequestType = UT_READ_VENDOR_DEVICE;
155 1.50.2.2 christos req.bRequest = MUE_UR_READREG;
156 1.50.2.2 christos USETW(req.wValue, 0);
157 1.50.2.2 christos USETW(req.wIndex, reg);
158 1.50.2.2 christos USETW(req.wLength, 4);
159 1.50.2.2 christos
160 1.50.2.3 martin err = usbd_do_request(un->un_udev, &req, &val);
161 1.50.2.2 christos if (err) {
162 1.50.2.3 martin MUE_PRINTF(un, "reg = %#x: %s\n", reg, usbd_errstr(err));
163 1.50.2.2 christos return 0;
164 1.50.2.2 christos }
165 1.50.2.2 christos
166 1.50.2.2 christos return UGETDW(val);
167 1.50.2.2 christos }
168 1.50.2.2 christos
169 1.50.2.2 christos static int
170 1.50.2.3 martin mue_csr_write(struct usbnet *un, uint32_t reg, uint32_t aval)
171 1.50.2.2 christos {
172 1.50.2.2 christos usb_device_request_t req;
173 1.50.2.2 christos usbd_status err;
174 1.50.2.2 christos uDWord val;
175 1.50.2.2 christos
176 1.50.2.3 martin if (usbnet_isdying(un))
177 1.50.2.2 christos return 0;
178 1.50.2.2 christos
179 1.50.2.2 christos USETDW(val, aval);
180 1.50.2.2 christos req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
181 1.50.2.2 christos req.bRequest = MUE_UR_WRITEREG;
182 1.50.2.2 christos USETW(req.wValue, 0);
183 1.50.2.2 christos USETW(req.wIndex, reg);
184 1.50.2.2 christos USETW(req.wLength, 4);
185 1.50.2.2 christos
186 1.50.2.3 martin err = usbd_do_request(un->un_udev, &req, &val);
187 1.50.2.2 christos if (err) {
188 1.50.2.3 martin MUE_PRINTF(un, "reg = %#x: %s\n", reg, usbd_errstr(err));
189 1.50.2.2 christos return -1;
190 1.50.2.2 christos }
191 1.50.2.2 christos
192 1.50.2.2 christos return 0;
193 1.50.2.2 christos }
194 1.50.2.2 christos
195 1.50.2.2 christos static int
196 1.50.2.3 martin mue_wait_for_bits(struct usbnet *un, uint32_t reg,
197 1.50.2.2 christos uint32_t set, uint32_t clear, uint32_t fail)
198 1.50.2.2 christos {
199 1.50.2.2 christos uint32_t val;
200 1.50.2.2 christos int ntries;
201 1.50.2.2 christos
202 1.50.2.2 christos for (ntries = 0; ntries < 1000; ntries++) {
203 1.50.2.3 martin val = mue_csr_read(un, reg);
204 1.50.2.2 christos if ((val & set) || !(val & clear))
205 1.50.2.2 christos return 0;
206 1.50.2.2 christos if (val & fail)
207 1.50.2.2 christos return 1;
208 1.50.2.3 martin usbd_delay_ms(un->un_udev, 1);
209 1.50.2.2 christos }
210 1.50.2.2 christos
211 1.50.2.2 christos return 1;
212 1.50.2.2 christos }
213 1.50.2.2 christos
214 1.50.2.2 christos static int
215 1.50.2.3 martin mue_uno_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val)
216 1.50.2.2 christos {
217 1.50.2.2 christos uint32_t data;
218 1.50.2.2 christos
219 1.50.2.3 martin if (un->un_phyno != phy)
220 1.50.2.3 martin return EINVAL;
221 1.50.2.2 christos
222 1.50.2.3 martin if (MUE_WAIT_CLR(un, MUE_MII_ACCESS, MUE_MII_ACCESS_BUSY, 0)) {
223 1.50.2.3 martin MUE_PRINTF(un, "not ready\n");
224 1.50.2.3 martin return EBUSY;
225 1.50.2.2 christos }
226 1.50.2.2 christos
227 1.50.2.3 martin mue_csr_write(un, MUE_MII_ACCESS, MUE_MII_ACCESS_READ |
228 1.50.2.2 christos MUE_MII_ACCESS_BUSY | MUE_MII_ACCESS_REGADDR(reg) |
229 1.50.2.2 christos MUE_MII_ACCESS_PHYADDR(phy));
230 1.50.2.2 christos
231 1.50.2.3 martin if (MUE_WAIT_CLR(un, MUE_MII_ACCESS, MUE_MII_ACCESS_BUSY, 0)) {
232 1.50.2.3 martin MUE_PRINTF(un, "timed out\n");
233 1.50.2.3 martin return ETIMEDOUT;
234 1.50.2.2 christos }
235 1.50.2.2 christos
236 1.50.2.3 martin data = mue_csr_read(un, MUE_MII_DATA);
237 1.50.2.2 christos *val = data & 0xffff;
238 1.50.2.2 christos
239 1.50.2.3 martin return 0;
240 1.50.2.2 christos }
241 1.50.2.2 christos
242 1.50.2.2 christos static int
243 1.50.2.3 martin mue_uno_mii_write_reg(struct usbnet *un, int phy, int reg, uint16_t val)
244 1.50.2.2 christos {
245 1.50.2.2 christos
246 1.50.2.3 martin if (un->un_phyno != phy)
247 1.50.2.3 martin return EINVAL;
248 1.50.2.2 christos
249 1.50.2.3 martin if (MUE_WAIT_CLR(un, MUE_MII_ACCESS, MUE_MII_ACCESS_BUSY, 0)) {
250 1.50.2.3 martin MUE_PRINTF(un, "not ready\n");
251 1.50.2.3 martin return EBUSY;
252 1.50.2.2 christos }
253 1.50.2.2 christos
254 1.50.2.3 martin mue_csr_write(un, MUE_MII_DATA, val);
255 1.50.2.3 martin mue_csr_write(un, MUE_MII_ACCESS, MUE_MII_ACCESS_WRITE |
256 1.50.2.2 christos MUE_MII_ACCESS_BUSY | MUE_MII_ACCESS_REGADDR(reg) |
257 1.50.2.2 christos MUE_MII_ACCESS_PHYADDR(phy));
258 1.50.2.2 christos
259 1.50.2.3 martin if (MUE_WAIT_CLR(un, MUE_MII_ACCESS, MUE_MII_ACCESS_BUSY, 0)) {
260 1.50.2.3 martin MUE_PRINTF(un, "timed out\n");
261 1.50.2.3 martin return ETIMEDOUT;
262 1.50.2.2 christos }
263 1.50.2.3 martin
264 1.50.2.3 martin return 0;
265 1.50.2.2 christos }
266 1.50.2.2 christos
267 1.50.2.2 christos static void
268 1.50.2.3 martin mue_uno_mii_statchg(struct ifnet *ifp)
269 1.50.2.2 christos {
270 1.50.2.3 martin struct usbnet * const un = ifp->if_softc;
271 1.50.2.3 martin struct mii_data * const mii = usbnet_mii(un);
272 1.50.2.2 christos uint32_t flow, threshold;
273 1.50.2.2 christos
274 1.50.2.3 martin if (usbnet_isdying(un))
275 1.50.2.2 christos return;
276 1.50.2.2 christos
277 1.50.2.2 christos if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
278 1.50.2.2 christos (IFM_ACTIVE | IFM_AVALID)) {
279 1.50.2.2 christos switch (IFM_SUBTYPE(mii->mii_media_active)) {
280 1.50.2.2 christos case IFM_10_T:
281 1.50.2.2 christos case IFM_100_TX:
282 1.50.2.2 christos case IFM_1000_T:
283 1.50.2.3 martin usbnet_set_link(un, true);
284 1.50.2.2 christos break;
285 1.50.2.2 christos default:
286 1.50.2.2 christos break;
287 1.50.2.2 christos }
288 1.50.2.2 christos }
289 1.50.2.2 christos
290 1.50.2.2 christos /* Lost link, do nothing. */
291 1.50.2.3 martin if (!usbnet_havelink(un)) {
292 1.50.2.3 martin DPRINTF(un, "mii_media_status = %#x\n", mii->mii_media_status);
293 1.50.2.2 christos return;
294 1.50.2.2 christos }
295 1.50.2.2 christos
296 1.50.2.3 martin if (!(un->un_flags & LAN7500)) {
297 1.50.2.3 martin if (un->un_udev->ud_speed == USB_SPEED_SUPER) {
298 1.50.2.2 christos if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
299 1.50.2.2 christos /* Disable U2 and enable U1. */
300 1.50.2.3 martin MUE_CLRBIT(un, MUE_USB_CFG1,
301 1.50.2.2 christos MUE_USB_CFG1_DEV_U2_INIT_EN);
302 1.50.2.3 martin MUE_SETBIT(un, MUE_USB_CFG1,
303 1.50.2.2 christos MUE_USB_CFG1_DEV_U1_INIT_EN);
304 1.50.2.2 christos } else {
305 1.50.2.2 christos /* Enable U1 and U2. */
306 1.50.2.3 martin MUE_SETBIT(un, MUE_USB_CFG1,
307 1.50.2.2 christos MUE_USB_CFG1_DEV_U1_INIT_EN |
308 1.50.2.2 christos MUE_USB_CFG1_DEV_U2_INIT_EN);
309 1.50.2.2 christos }
310 1.50.2.2 christos }
311 1.50.2.2 christos }
312 1.50.2.2 christos
313 1.50.2.2 christos flow = 0;
314 1.50.2.2 christos /* XXX Linux does not check IFM_FDX flag for 7800. */
315 1.50.2.2 christos if (IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) {
316 1.50.2.2 christos if (IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE)
317 1.50.2.2 christos flow |= MUE_FLOW_TX_FCEN | MUE_FLOW_PAUSE_TIME;
318 1.50.2.2 christos if (IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE)
319 1.50.2.2 christos flow |= MUE_FLOW_RX_FCEN;
320 1.50.2.2 christos }
321 1.50.2.2 christos
322 1.50.2.2 christos /* XXX Magic numbers taken from Linux driver. */
323 1.50.2.3 martin if (un->un_flags & LAN7500)
324 1.50.2.2 christos threshold = 0x820;
325 1.50.2.2 christos else
326 1.50.2.3 martin switch (un->un_udev->ud_speed) {
327 1.50.2.2 christos case USB_SPEED_SUPER:
328 1.50.2.2 christos threshold = 0x817;
329 1.50.2.2 christos break;
330 1.50.2.2 christos case USB_SPEED_HIGH:
331 1.50.2.2 christos threshold = 0x211;
332 1.50.2.2 christos break;
333 1.50.2.2 christos default:
334 1.50.2.2 christos threshold = 0;
335 1.50.2.2 christos break;
336 1.50.2.2 christos }
337 1.50.2.2 christos
338 1.50.2.2 christos /* Threshold value should be set before enabling flow. */
339 1.50.2.3 martin mue_csr_write(un, (un->un_flags & LAN7500) ?
340 1.50.2.2 christos MUE_7500_FCT_FLOW : MUE_7800_FCT_FLOW, threshold);
341 1.50.2.3 martin mue_csr_write(un, MUE_FLOW, flow);
342 1.50.2.2 christos
343 1.50.2.3 martin DPRINTF(un, "done\n");
344 1.50.2.2 christos }
345 1.50.2.2 christos
346 1.50.2.2 christos static uint8_t
347 1.50.2.3 martin mue_eeprom_getbyte(struct usbnet *un, int off, uint8_t *dest)
348 1.50.2.2 christos {
349 1.50.2.2 christos uint32_t val;
350 1.50.2.2 christos
351 1.50.2.3 martin if (MUE_WAIT_CLR(un, MUE_E2P_CMD, MUE_E2P_CMD_BUSY, 0)) {
352 1.50.2.3 martin MUE_PRINTF(un, "not ready\n");
353 1.50.2.2 christos return ETIMEDOUT;
354 1.50.2.2 christos }
355 1.50.2.2 christos
356 1.50.2.2 christos KASSERT((off & ~MUE_E2P_CMD_ADDR_MASK) == 0);
357 1.50.2.3 martin mue_csr_write(un, MUE_E2P_CMD, MUE_E2P_CMD_READ | MUE_E2P_CMD_BUSY |
358 1.50.2.2 christos off);
359 1.50.2.2 christos
360 1.50.2.3 martin if (MUE_WAIT_CLR(un, MUE_E2P_CMD, MUE_E2P_CMD_BUSY,
361 1.50.2.2 christos MUE_E2P_CMD_TIMEOUT)) {
362 1.50.2.3 martin MUE_PRINTF(un, "timed out\n");
363 1.50.2.2 christos return ETIMEDOUT;
364 1.50.2.2 christos }
365 1.50.2.2 christos
366 1.50.2.3 martin val = mue_csr_read(un, MUE_E2P_DATA);
367 1.50.2.2 christos *dest = val & 0xff;
368 1.50.2.2 christos
369 1.50.2.2 christos return 0;
370 1.50.2.2 christos }
371 1.50.2.2 christos
372 1.50.2.2 christos static int
373 1.50.2.3 martin mue_read_eeprom(struct usbnet *un, uint8_t *dest, int off, int cnt)
374 1.50.2.2 christos {
375 1.50.2.2 christos uint32_t val = 0; /* XXX gcc */
376 1.50.2.2 christos uint8_t byte;
377 1.50.2.2 christos int i, err = 0;
378 1.50.2.2 christos
379 1.50.2.2 christos /*
380 1.50.2.2 christos * EEPROM pins are muxed with the LED function on LAN7800 device.
381 1.50.2.2 christos */
382 1.50.2.3 martin if (un->un_flags & LAN7800) {
383 1.50.2.3 martin val = mue_csr_read(un, MUE_HW_CFG);
384 1.50.2.3 martin mue_csr_write(un, MUE_HW_CFG,
385 1.50.2.2 christos val & ~(MUE_HW_CFG_LED0_EN | MUE_HW_CFG_LED1_EN));
386 1.50.2.2 christos }
387 1.50.2.2 christos
388 1.50.2.2 christos for (i = 0; i < cnt; i++) {
389 1.50.2.3 martin err = mue_eeprom_getbyte(un, off + i, &byte);
390 1.50.2.2 christos if (err)
391 1.50.2.2 christos break;
392 1.50.2.2 christos *(dest + i) = byte;
393 1.50.2.2 christos }
394 1.50.2.2 christos
395 1.50.2.3 martin if (un->un_flags & LAN7800)
396 1.50.2.3 martin mue_csr_write(un, MUE_HW_CFG, val);
397 1.50.2.2 christos
398 1.50.2.2 christos return err ? 1 : 0;
399 1.50.2.2 christos }
400 1.50.2.2 christos
401 1.50.2.2 christos static bool
402 1.50.2.3 martin mue_eeprom_present(struct usbnet *un)
403 1.50.2.2 christos {
404 1.50.2.2 christos uint32_t val;
405 1.50.2.2 christos uint8_t sig;
406 1.50.2.2 christos int ret;
407 1.50.2.2 christos
408 1.50.2.3 martin if (un->un_flags & LAN7500) {
409 1.50.2.3 martin val = mue_csr_read(un, MUE_E2P_CMD);
410 1.50.2.2 christos return val & MUE_E2P_CMD_LOADED;
411 1.50.2.2 christos } else {
412 1.50.2.3 martin ret = mue_read_eeprom(un, &sig, MUE_E2P_IND_OFFSET, 1);
413 1.50.2.2 christos return (ret == 0) && (sig == MUE_E2P_IND);
414 1.50.2.2 christos }
415 1.50.2.2 christos }
416 1.50.2.2 christos
417 1.50.2.2 christos static int
418 1.50.2.3 martin mue_read_otp_raw(struct usbnet *un, uint8_t *dest, int off, int cnt)
419 1.50.2.2 christos {
420 1.50.2.2 christos uint32_t val;
421 1.50.2.2 christos int i, err;
422 1.50.2.2 christos
423 1.50.2.3 martin val = mue_csr_read(un, MUE_OTP_PWR_DN);
424 1.50.2.2 christos
425 1.50.2.2 christos /* Checking if bit is set. */
426 1.50.2.2 christos if (val & MUE_OTP_PWR_DN_PWRDN_N) {
427 1.50.2.2 christos /* Clear it, then wait for it to be cleared. */
428 1.50.2.3 martin mue_csr_write(un, MUE_OTP_PWR_DN, 0);
429 1.50.2.3 martin err = MUE_WAIT_CLR(un, MUE_OTP_PWR_DN, MUE_OTP_PWR_DN_PWRDN_N,
430 1.50.2.2 christos 0);
431 1.50.2.2 christos if (err) {
432 1.50.2.3 martin MUE_PRINTF(un, "not ready\n");
433 1.50.2.2 christos return 1;
434 1.50.2.2 christos }
435 1.50.2.2 christos }
436 1.50.2.2 christos
437 1.50.2.2 christos /* Start reading the bytes, one at a time. */
438 1.50.2.2 christos for (i = 0; i < cnt; i++) {
439 1.50.2.3 martin mue_csr_write(un, MUE_OTP_ADDR1,
440 1.50.2.2 christos ((off + i) >> 8) & MUE_OTP_ADDR1_MASK);
441 1.50.2.3 martin mue_csr_write(un, MUE_OTP_ADDR2,
442 1.50.2.2 christos ((off + i) & MUE_OTP_ADDR2_MASK));
443 1.50.2.3 martin mue_csr_write(un, MUE_OTP_FUNC_CMD, MUE_OTP_FUNC_CMD_READ);
444 1.50.2.3 martin mue_csr_write(un, MUE_OTP_CMD_GO, MUE_OTP_CMD_GO_GO);
445 1.50.2.2 christos
446 1.50.2.3 martin err = MUE_WAIT_CLR(un, MUE_OTP_STATUS, MUE_OTP_STATUS_BUSY, 0);
447 1.50.2.2 christos if (err) {
448 1.50.2.3 martin MUE_PRINTF(un, "timed out\n");
449 1.50.2.2 christos return 1;
450 1.50.2.2 christos }
451 1.50.2.3 martin val = mue_csr_read(un, MUE_OTP_RD_DATA);
452 1.50.2.2 christos *(dest + i) = (uint8_t)(val & 0xff);
453 1.50.2.2 christos }
454 1.50.2.2 christos
455 1.50.2.2 christos return 0;
456 1.50.2.2 christos }
457 1.50.2.2 christos
458 1.50.2.2 christos static int
459 1.50.2.3 martin mue_read_otp(struct usbnet *un, uint8_t *dest, int off, int cnt)
460 1.50.2.2 christos {
461 1.50.2.2 christos uint8_t sig;
462 1.50.2.2 christos int err;
463 1.50.2.2 christos
464 1.50.2.3 martin if (un->un_flags & LAN7500)
465 1.50.2.2 christos return 1;
466 1.50.2.2 christos
467 1.50.2.3 martin err = mue_read_otp_raw(un, &sig, MUE_OTP_IND_OFFSET, 1);
468 1.50.2.2 christos if (err)
469 1.50.2.2 christos return 1;
470 1.50.2.2 christos switch (sig) {
471 1.50.2.2 christos case MUE_OTP_IND_1:
472 1.50.2.2 christos break;
473 1.50.2.2 christos case MUE_OTP_IND_2:
474 1.50.2.2 christos off += 0x100;
475 1.50.2.2 christos break;
476 1.50.2.2 christos default:
477 1.50.2.3 martin DPRINTF(un, "OTP not found\n");
478 1.50.2.2 christos return 1;
479 1.50.2.2 christos }
480 1.50.2.3 martin err = mue_read_otp_raw(un, dest, off, cnt);
481 1.50.2.2 christos return err;
482 1.50.2.2 christos }
483 1.50.2.2 christos
484 1.50.2.2 christos static void
485 1.50.2.3 martin mue_dataport_write(struct usbnet *un, uint32_t sel, uint32_t addr,
486 1.50.2.2 christos uint32_t cnt, uint32_t *data)
487 1.50.2.2 christos {
488 1.50.2.2 christos uint32_t i;
489 1.50.2.2 christos
490 1.50.2.3 martin if (MUE_WAIT_SET(un, MUE_DP_SEL, MUE_DP_SEL_DPRDY, 0)) {
491 1.50.2.3 martin MUE_PRINTF(un, "not ready\n");
492 1.50.2.2 christos return;
493 1.50.2.2 christos }
494 1.50.2.2 christos
495 1.50.2.3 martin mue_csr_write(un, MUE_DP_SEL,
496 1.50.2.3 martin (mue_csr_read(un, MUE_DP_SEL) & ~MUE_DP_SEL_RSEL_MASK) | sel);
497 1.50.2.2 christos
498 1.50.2.2 christos for (i = 0; i < cnt; i++) {
499 1.50.2.3 martin mue_csr_write(un, MUE_DP_ADDR, addr + i);
500 1.50.2.3 martin mue_csr_write(un, MUE_DP_DATA, data[i]);
501 1.50.2.3 martin mue_csr_write(un, MUE_DP_CMD, MUE_DP_CMD_WRITE);
502 1.50.2.3 martin if (MUE_WAIT_SET(un, MUE_DP_SEL, MUE_DP_SEL_DPRDY, 0)) {
503 1.50.2.3 martin MUE_PRINTF(un, "timed out\n");
504 1.50.2.2 christos return;
505 1.50.2.2 christos }
506 1.50.2.2 christos }
507 1.50.2.2 christos }
508 1.50.2.2 christos
509 1.50.2.2 christos static void
510 1.50.2.3 martin mue_init_ltm(struct usbnet *un)
511 1.50.2.2 christos {
512 1.50.2.2 christos uint32_t idx[MUE_NUM_LTM_INDEX] = { 0, 0, 0, 0, 0, 0 };
513 1.50.2.2 christos uint8_t temp[2];
514 1.50.2.2 christos size_t i;
515 1.50.2.2 christos
516 1.50.2.3 martin if (mue_csr_read(un, MUE_USB_CFG1) & MUE_USB_CFG1_LTM_ENABLE) {
517 1.50.2.3 martin if (mue_eeprom_present(un) &&
518 1.50.2.3 martin (mue_read_eeprom(un, temp, MUE_E2P_LTM_OFFSET, 2) == 0)) {
519 1.50.2.2 christos if (temp[0] != sizeof(idx)) {
520 1.50.2.3 martin DPRINTF(un, "EEPROM: unexpected size\n");
521 1.50.2.2 christos goto done;
522 1.50.2.2 christos }
523 1.50.2.3 martin if (mue_read_eeprom(un, (uint8_t *)idx, temp[1] << 1,
524 1.50.2.2 christos sizeof(idx))) {
525 1.50.2.3 martin DPRINTF(un, "EEPROM: failed to read\n");
526 1.50.2.2 christos goto done;
527 1.50.2.2 christos }
528 1.50.2.3 martin DPRINTF(un, "success\n");
529 1.50.2.3 martin } else if (mue_read_otp(un, temp, MUE_E2P_LTM_OFFSET, 2) == 0) {
530 1.50.2.2 christos if (temp[0] != sizeof(idx)) {
531 1.50.2.3 martin DPRINTF(un, "OTP: unexpected size\n");
532 1.50.2.2 christos goto done;
533 1.50.2.2 christos }
534 1.50.2.3 martin if (mue_read_otp(un, (uint8_t *)idx, temp[1] << 1,
535 1.50.2.2 christos sizeof(idx))) {
536 1.50.2.3 martin DPRINTF(un, "OTP: failed to read\n");
537 1.50.2.2 christos goto done;
538 1.50.2.2 christos }
539 1.50.2.3 martin DPRINTF(un, "success\n");
540 1.50.2.2 christos } else
541 1.50.2.3 martin DPRINTF(un, "nothing to do\n");
542 1.50.2.2 christos } else
543 1.50.2.3 martin DPRINTF(un, "nothing to do\n");
544 1.50.2.2 christos done:
545 1.50.2.2 christos for (i = 0; i < __arraycount(idx); i++)
546 1.50.2.3 martin mue_csr_write(un, MUE_LTM_INDEX(i), idx[i]);
547 1.50.2.2 christos }
548 1.50.2.2 christos
549 1.50.2.2 christos static int
550 1.50.2.3 martin mue_chip_init(struct usbnet *un)
551 1.50.2.2 christos {
552 1.50.2.2 christos uint32_t val;
553 1.50.2.2 christos
554 1.50.2.3 martin if ((un->un_flags & LAN7500) &&
555 1.50.2.3 martin MUE_WAIT_SET(un, MUE_PMT_CTL, MUE_PMT_CTL_READY, 0)) {
556 1.50.2.3 martin MUE_PRINTF(un, "not ready\n");
557 1.50.2.2 christos return ETIMEDOUT;
558 1.50.2.2 christos }
559 1.50.2.2 christos
560 1.50.2.3 martin MUE_SETBIT(un, MUE_HW_CFG, MUE_HW_CFG_LRST);
561 1.50.2.3 martin if (MUE_WAIT_CLR(un, MUE_HW_CFG, MUE_HW_CFG_LRST, 0)) {
562 1.50.2.3 martin MUE_PRINTF(un, "timed out\n");
563 1.50.2.2 christos return ETIMEDOUT;
564 1.50.2.2 christos }
565 1.50.2.2 christos
566 1.50.2.2 christos /* Respond to the IN token with a NAK. */
567 1.50.2.3 martin if (un->un_flags & LAN7500)
568 1.50.2.3 martin MUE_SETBIT(un, MUE_HW_CFG, MUE_HW_CFG_BIR);
569 1.50.2.2 christos else
570 1.50.2.3 martin MUE_SETBIT(un, MUE_USB_CFG0, MUE_USB_CFG0_BIR);
571 1.50.2.2 christos
572 1.50.2.3 martin if (un->un_flags & LAN7500) {
573 1.50.2.3 martin if (un->un_udev->ud_speed == USB_SPEED_HIGH)
574 1.50.2.2 christos val = MUE_7500_HS_RX_BUFSIZE /
575 1.50.2.2 christos MUE_HS_USB_PKT_SIZE;
576 1.50.2.2 christos else
577 1.50.2.2 christos val = MUE_7500_FS_RX_BUFSIZE /
578 1.50.2.2 christos MUE_FS_USB_PKT_SIZE;
579 1.50.2.3 martin mue_csr_write(un, MUE_7500_BURST_CAP, val);
580 1.50.2.3 martin mue_csr_write(un, MUE_7500_BULKIN_DELAY,
581 1.50.2.2 christos MUE_7500_DEFAULT_BULKIN_DELAY);
582 1.50.2.2 christos
583 1.50.2.3 martin MUE_SETBIT(un, MUE_HW_CFG, MUE_HW_CFG_BCE | MUE_HW_CFG_MEF);
584 1.50.2.2 christos
585 1.50.2.2 christos /* Set FIFO sizes. */
586 1.50.2.2 christos val = (MUE_7500_MAX_RX_FIFO_SIZE - 512) / 512;
587 1.50.2.3 martin mue_csr_write(un, MUE_7500_FCT_RX_FIFO_END, val);
588 1.50.2.2 christos val = (MUE_7500_MAX_TX_FIFO_SIZE - 512) / 512;
589 1.50.2.3 martin mue_csr_write(un, MUE_7500_FCT_TX_FIFO_END, val);
590 1.50.2.2 christos } else {
591 1.50.2.2 christos /* Init LTM. */
592 1.50.2.3 martin mue_init_ltm(un);
593 1.50.2.2 christos
594 1.50.2.2 christos val = MUE_7800_RX_BUFSIZE;
595 1.50.2.3 martin switch (un->un_udev->ud_speed) {
596 1.50.2.2 christos case USB_SPEED_SUPER:
597 1.50.2.2 christos val /= MUE_SS_USB_PKT_SIZE;
598 1.50.2.2 christos break;
599 1.50.2.2 christos case USB_SPEED_HIGH:
600 1.50.2.2 christos val /= MUE_HS_USB_PKT_SIZE;
601 1.50.2.2 christos break;
602 1.50.2.2 christos default:
603 1.50.2.2 christos val /= MUE_FS_USB_PKT_SIZE;
604 1.50.2.2 christos break;
605 1.50.2.2 christos }
606 1.50.2.3 martin mue_csr_write(un, MUE_7800_BURST_CAP, val);
607 1.50.2.3 martin mue_csr_write(un, MUE_7800_BULKIN_DELAY,
608 1.50.2.2 christos MUE_7800_DEFAULT_BULKIN_DELAY);
609 1.50.2.2 christos
610 1.50.2.3 martin MUE_SETBIT(un, MUE_HW_CFG, MUE_HW_CFG_MEF);
611 1.50.2.3 martin MUE_SETBIT(un, MUE_USB_CFG0, MUE_USB_CFG0_BCE);
612 1.50.2.2 christos
613 1.50.2.2 christos /*
614 1.50.2.2 christos * Set FCL's RX and TX FIFO sizes: according to data sheet this
615 1.50.2.2 christos * is already the default value. But we initialize it to the
616 1.50.2.2 christos * same value anyways, as that's what the Linux driver does.
617 1.50.2.2 christos */
618 1.50.2.2 christos val = (MUE_7800_MAX_RX_FIFO_SIZE - 512) / 512;
619 1.50.2.3 martin mue_csr_write(un, MUE_7800_FCT_RX_FIFO_END, val);
620 1.50.2.2 christos val = (MUE_7800_MAX_TX_FIFO_SIZE - 512) / 512;
621 1.50.2.3 martin mue_csr_write(un, MUE_7800_FCT_TX_FIFO_END, val);
622 1.50.2.2 christos }
623 1.50.2.2 christos
624 1.50.2.2 christos /* Enabling interrupts. */
625 1.50.2.3 martin mue_csr_write(un, MUE_INT_STATUS, ~0);
626 1.50.2.2 christos
627 1.50.2.3 martin mue_csr_write(un, (un->un_flags & LAN7500) ?
628 1.50.2.2 christos MUE_7500_FCT_FLOW : MUE_7800_FCT_FLOW, 0);
629 1.50.2.3 martin mue_csr_write(un, MUE_FLOW, 0);
630 1.50.2.2 christos
631 1.50.2.2 christos /* Reset PHY. */
632 1.50.2.3 martin MUE_SETBIT(un, MUE_PMT_CTL, MUE_PMT_CTL_PHY_RST);
633 1.50.2.3 martin if (MUE_WAIT_CLR(un, MUE_PMT_CTL, MUE_PMT_CTL_PHY_RST, 0)) {
634 1.50.2.3 martin MUE_PRINTF(un, "PHY not ready\n");
635 1.50.2.2 christos return ETIMEDOUT;
636 1.50.2.2 christos }
637 1.50.2.2 christos
638 1.50.2.2 christos /* LAN7801 only has RGMII mode. */
639 1.50.2.3 martin if (un->un_flags & LAN7801)
640 1.50.2.3 martin MUE_CLRBIT(un, MUE_MAC_CR, MUE_MAC_CR_GMII_EN);
641 1.50.2.2 christos
642 1.50.2.3 martin if ((un->un_flags & (LAN7500 | LAN7800)) ||
643 1.50.2.3 martin !mue_eeprom_present(un)) {
644 1.50.2.2 christos /* Allow MAC to detect speed and duplex from PHY. */
645 1.50.2.3 martin MUE_SETBIT(un, MUE_MAC_CR, MUE_MAC_CR_AUTO_SPEED |
646 1.50.2.2 christos MUE_MAC_CR_AUTO_DUPLEX);
647 1.50.2.2 christos }
648 1.50.2.2 christos
649 1.50.2.3 martin MUE_SETBIT(un, MUE_MAC_TX, MUE_MAC_TX_TXEN);
650 1.50.2.3 martin MUE_SETBIT(un, (un->un_flags & LAN7500) ?
651 1.50.2.2 christos MUE_7500_FCT_TX_CTL : MUE_7800_FCT_TX_CTL, MUE_FCT_TX_CTL_EN);
652 1.50.2.2 christos
653 1.50.2.3 martin MUE_SETBIT(un, (un->un_flags & LAN7500) ?
654 1.50.2.2 christos MUE_7500_FCT_RX_CTL : MUE_7800_FCT_RX_CTL, MUE_FCT_RX_CTL_EN);
655 1.50.2.2 christos
656 1.50.2.2 christos /* Set default GPIO/LED settings only if no EEPROM is detected. */
657 1.50.2.3 martin if ((un->un_flags & LAN7500) && !mue_eeprom_present(un)) {
658 1.50.2.3 martin MUE_CLRBIT(un, MUE_LED_CFG, MUE_LED_CFG_LED10_FUN_SEL);
659 1.50.2.3 martin MUE_SETBIT(un, MUE_LED_CFG,
660 1.50.2.2 christos MUE_LED_CFG_LEDGPIO_EN | MUE_LED_CFG_LED2_FUN_SEL);
661 1.50.2.2 christos }
662 1.50.2.2 christos
663 1.50.2.2 christos /* XXX We assume two LEDs at least when EEPROM is missing. */
664 1.50.2.3 martin if (un->un_flags & LAN7800 &&
665 1.50.2.3 martin !mue_eeprom_present(un))
666 1.50.2.3 martin MUE_SETBIT(un, MUE_HW_CFG,
667 1.50.2.2 christos MUE_HW_CFG_LED0_EN | MUE_HW_CFG_LED1_EN);
668 1.50.2.2 christos
669 1.50.2.2 christos return 0;
670 1.50.2.2 christos }
671 1.50.2.2 christos
672 1.50.2.2 christos static void
673 1.50.2.3 martin mue_set_macaddr(struct usbnet *un)
674 1.50.2.2 christos {
675 1.50.2.3 martin struct ifnet * const ifp = usbnet_ifp(un);
676 1.50.2.2 christos const uint8_t *enaddr = CLLADDR(ifp->if_sadl);
677 1.50.2.2 christos uint32_t lo, hi;
678 1.50.2.2 christos
679 1.50.2.2 christos lo = MUE_ENADDR_LO(enaddr);
680 1.50.2.2 christos hi = MUE_ENADDR_HI(enaddr);
681 1.50.2.2 christos
682 1.50.2.3 martin mue_csr_write(un, MUE_RX_ADDRL, lo);
683 1.50.2.3 martin mue_csr_write(un, MUE_RX_ADDRH, hi);
684 1.50.2.2 christos }
685 1.50.2.2 christos
686 1.50.2.2 christos static int
687 1.50.2.3 martin mue_get_macaddr(struct usbnet *un, prop_dictionary_t dict)
688 1.50.2.2 christos {
689 1.50.2.2 christos prop_data_t eaprop;
690 1.50.2.2 christos uint32_t low, high;
691 1.50.2.2 christos
692 1.50.2.3 martin if (!(un->un_flags & LAN7500)) {
693 1.50.2.3 martin low = mue_csr_read(un, MUE_RX_ADDRL);
694 1.50.2.3 martin high = mue_csr_read(un, MUE_RX_ADDRH);
695 1.50.2.3 martin un->un_eaddr[5] = (uint8_t)((high >> 8) & 0xff);
696 1.50.2.3 martin un->un_eaddr[4] = (uint8_t)((high) & 0xff);
697 1.50.2.3 martin un->un_eaddr[3] = (uint8_t)((low >> 24) & 0xff);
698 1.50.2.3 martin un->un_eaddr[2] = (uint8_t)((low >> 16) & 0xff);
699 1.50.2.3 martin un->un_eaddr[1] = (uint8_t)((low >> 8) & 0xff);
700 1.50.2.3 martin un->un_eaddr[0] = (uint8_t)((low) & 0xff);
701 1.50.2.3 martin if (ETHER_IS_VALID(un->un_eaddr))
702 1.50.2.2 christos return 0;
703 1.50.2.2 christos else
704 1.50.2.3 martin DPRINTF(un, "registers: %s\n",
705 1.50.2.3 martin ether_sprintf(un->un_eaddr));
706 1.50.2.2 christos }
707 1.50.2.2 christos
708 1.50.2.3 martin if (mue_eeprom_present(un) && !mue_read_eeprom(un, un->un_eaddr,
709 1.50.2.2 christos MUE_E2P_MAC_OFFSET, ETHER_ADDR_LEN)) {
710 1.50.2.3 martin if (ETHER_IS_VALID(un->un_eaddr))
711 1.50.2.2 christos return 0;
712 1.50.2.2 christos else
713 1.50.2.3 martin DPRINTF(un, "EEPROM: %s\n",
714 1.50.2.3 martin ether_sprintf(un->un_eaddr));
715 1.50.2.2 christos }
716 1.50.2.2 christos
717 1.50.2.3 martin if (mue_read_otp(un, un->un_eaddr, MUE_OTP_MAC_OFFSET,
718 1.50.2.2 christos ETHER_ADDR_LEN) == 0) {
719 1.50.2.3 martin if (ETHER_IS_VALID(un->un_eaddr))
720 1.50.2.2 christos return 0;
721 1.50.2.2 christos else
722 1.50.2.3 martin DPRINTF(un, "OTP: %s\n",
723 1.50.2.3 martin ether_sprintf(un->un_eaddr));
724 1.50.2.2 christos }
725 1.50.2.2 christos
726 1.50.2.2 christos /*
727 1.50.2.2 christos * Other MD methods. This should be tried only if other methods fail.
728 1.50.2.2 christos * Otherwise, MAC address for internal device can be assinged to
729 1.50.2.2 christos * external devices on Raspberry Pi, for example.
730 1.50.2.2 christos */
731 1.50.2.2 christos eaprop = prop_dictionary_get(dict, "mac-address");
732 1.50.2.2 christos if (eaprop != NULL) {
733 1.50.2.2 christos KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA);
734 1.50.2.2 christos KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN);
735 1.50.2.3 martin memcpy(un->un_eaddr, prop_data_data_nocopy(eaprop),
736 1.50.2.2 christos ETHER_ADDR_LEN);
737 1.50.2.3 martin if (ETHER_IS_VALID(un->un_eaddr))
738 1.50.2.2 christos return 0;
739 1.50.2.2 christos else
740 1.50.2.3 martin DPRINTF(un, "prop_dictionary_get: %s\n",
741 1.50.2.3 martin ether_sprintf(un->un_eaddr));
742 1.50.2.2 christos }
743 1.50.2.2 christos
744 1.50.2.2 christos return 1;
745 1.50.2.2 christos }
746 1.50.2.2 christos
747 1.50.2.2 christos
748 1.50.2.2 christos /*
749 1.50.2.2 christos * Probe for a Microchip chip.
750 1.50.2.2 christos */
751 1.50.2.2 christos static int
752 1.50.2.2 christos mue_match(device_t parent, cfdata_t match, void *aux)
753 1.50.2.2 christos {
754 1.50.2.2 christos struct usb_attach_arg *uaa = aux;
755 1.50.2.2 christos
756 1.50.2.2 christos return (MUE_LOOKUP(uaa) != NULL) ? UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
757 1.50.2.2 christos }
758 1.50.2.2 christos
759 1.50.2.2 christos static void
760 1.50.2.2 christos mue_attach(device_t parent, device_t self, void *aux)
761 1.50.2.2 christos {
762 1.50.2.3 martin USBNET_MII_DECL_DEFAULT(unm);
763 1.50.2.3 martin struct usbnet * const un = device_private(self);
764 1.50.2.2 christos prop_dictionary_t dict = device_properties(self);
765 1.50.2.2 christos struct usb_attach_arg *uaa = aux;
766 1.50.2.2 christos struct usbd_device *dev = uaa->uaa_device;
767 1.50.2.2 christos usb_interface_descriptor_t *id;
768 1.50.2.2 christos usb_endpoint_descriptor_t *ed;
769 1.50.2.2 christos char *devinfop;
770 1.50.2.2 christos usbd_status err;
771 1.50.2.2 christos const char *descr;
772 1.50.2.3 martin uint32_t id_rev;
773 1.50.2.2 christos uint8_t i;
774 1.50.2.3 martin unsigned rx_list_cnt, tx_list_cnt;
775 1.50.2.3 martin unsigned rx_bufsz;
776 1.50.2.2 christos
777 1.50.2.2 christos aprint_naive("\n");
778 1.50.2.2 christos aprint_normal("\n");
779 1.50.2.3 martin devinfop = usbd_devinfo_alloc(dev, 0);
780 1.50.2.2 christos aprint_normal_dev(self, "%s\n", devinfop);
781 1.50.2.2 christos usbd_devinfo_free(devinfop);
782 1.50.2.2 christos
783 1.50.2.3 martin un->un_dev = self;
784 1.50.2.3 martin un->un_udev = dev;
785 1.50.2.3 martin un->un_sc = un;
786 1.50.2.3 martin un->un_ops = &mue_ops;
787 1.50.2.3 martin un->un_rx_xfer_flags = USBD_SHORT_XFER_OK;
788 1.50.2.3 martin un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER;
789 1.50.2.2 christos
790 1.50.2.2 christos #define MUE_CONFIG_NO 1
791 1.50.2.2 christos err = usbd_set_config_no(dev, MUE_CONFIG_NO, 1);
792 1.50.2.2 christos if (err) {
793 1.50.2.2 christos aprint_error_dev(self, "failed to set configuration: %s\n",
794 1.50.2.2 christos usbd_errstr(err));
795 1.50.2.2 christos return;
796 1.50.2.2 christos }
797 1.50.2.2 christos
798 1.50.2.2 christos #define MUE_IFACE_IDX 0
799 1.50.2.3 martin err = usbd_device2interface_handle(dev, MUE_IFACE_IDX, &un->un_iface);
800 1.50.2.2 christos if (err) {
801 1.50.2.2 christos aprint_error_dev(self, "failed to get interface handle: %s\n",
802 1.50.2.2 christos usbd_errstr(err));
803 1.50.2.2 christos return;
804 1.50.2.2 christos }
805 1.50.2.2 christos
806 1.50.2.3 martin un->un_flags = MUE_LOOKUP(uaa)->mue_flags;
807 1.50.2.2 christos
808 1.50.2.2 christos /* Decide on what our bufsize will be. */
809 1.50.2.3 martin if (un->un_flags & LAN7500) {
810 1.50.2.3 martin rx_bufsz = (un->un_udev->ud_speed == USB_SPEED_HIGH) ?
811 1.50.2.2 christos MUE_7500_HS_RX_BUFSIZE : MUE_7500_FS_RX_BUFSIZE;
812 1.50.2.3 martin rx_list_cnt = 1;
813 1.50.2.3 martin tx_list_cnt = 1;
814 1.50.2.2 christos } else {
815 1.50.2.3 martin rx_bufsz = MUE_7800_RX_BUFSIZE;
816 1.50.2.3 martin rx_list_cnt = MUE_RX_LIST_CNT;
817 1.50.2.3 martin tx_list_cnt = MUE_TX_LIST_CNT;
818 1.50.2.2 christos }
819 1.50.2.3 martin
820 1.50.2.3 martin un->un_rx_list_cnt = rx_list_cnt;
821 1.50.2.3 martin un->un_tx_list_cnt = tx_list_cnt;
822 1.50.2.3 martin un->un_rx_bufsz = rx_bufsz;
823 1.50.2.3 martin un->un_tx_bufsz = MUE_TX_BUFSIZE;
824 1.50.2.2 christos
825 1.50.2.2 christos /* Find endpoints. */
826 1.50.2.3 martin id = usbd_get_interface_descriptor(un->un_iface);
827 1.50.2.2 christos for (i = 0; i < id->bNumEndpoints; i++) {
828 1.50.2.3 martin ed = usbd_interface2endpoint_descriptor(un->un_iface, i);
829 1.50.2.2 christos if (ed == NULL) {
830 1.50.2.2 christos aprint_error_dev(self, "failed to get ep %hhd\n", i);
831 1.50.2.2 christos return;
832 1.50.2.2 christos }
833 1.50.2.2 christos if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
834 1.50.2.2 christos UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
835 1.50.2.3 martin un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress;
836 1.50.2.2 christos } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
837 1.50.2.2 christos UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
838 1.50.2.3 martin un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress;
839 1.50.2.2 christos } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
840 1.50.2.2 christos UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
841 1.50.2.3 martin un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress;
842 1.50.2.2 christos }
843 1.50.2.2 christos }
844 1.50.2.3 martin if (un->un_ed[USBNET_ENDPT_RX] == 0 ||
845 1.50.2.3 martin un->un_ed[USBNET_ENDPT_TX] == 0 ||
846 1.50.2.3 martin un->un_ed[USBNET_ENDPT_INTR] == 0) {
847 1.50.2.3 martin aprint_error_dev(self, "failed to find endpoints\n");
848 1.50.2.3 martin return;
849 1.50.2.3 martin }
850 1.50.2.2 christos
851 1.50.2.3 martin /* Set these up now for mue_cmd(). */
852 1.50.2.3 martin usbnet_attach(un, "muedet");
853 1.50.2.2 christos
854 1.50.2.3 martin un->un_phyno = 1;
855 1.50.2.2 christos
856 1.50.2.3 martin if (mue_chip_init(un)) {
857 1.50.2.2 christos aprint_error_dev(self, "failed to initialize chip\n");
858 1.50.2.2 christos return;
859 1.50.2.2 christos }
860 1.50.2.2 christos
861 1.50.2.2 christos /* A Microchip chip was detected. Inform the world. */
862 1.50.2.3 martin id_rev = mue_csr_read(un, MUE_ID_REV);
863 1.50.2.3 martin descr = (un->un_flags & LAN7500) ? "LAN7500" : "LAN7800";
864 1.50.2.3 martin aprint_normal_dev(self, "%s id %#x rev %#x\n", descr,
865 1.50.2.3 martin (unsigned)__SHIFTOUT(id_rev, MUE_ID_REV_ID),
866 1.50.2.3 martin (unsigned)__SHIFTOUT(id_rev, MUE_ID_REV_REV));
867 1.50.2.2 christos
868 1.50.2.3 martin if (mue_get_macaddr(un, dict)) {
869 1.50.2.2 christos aprint_error_dev(self, "failed to read MAC address\n");
870 1.50.2.2 christos return;
871 1.50.2.2 christos }
872 1.50.2.2 christos
873 1.50.2.3 martin struct ifnet *ifp = usbnet_ifp(un);
874 1.50.2.2 christos ifp->if_capabilities = IFCAP_TSOv4 | IFCAP_TSOv6 |
875 1.50.2.2 christos IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
876 1.50.2.2 christos IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
877 1.50.2.2 christos IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx |
878 1.50.2.2 christos IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_TCPv6_Rx |
879 1.50.2.2 christos IFCAP_CSUM_UDPv6_Tx | IFCAP_CSUM_UDPv6_Rx;
880 1.50.2.2 christos
881 1.50.2.3 martin struct ethercom *ec = usbnet_ec(un);
882 1.50.2.3 martin ec->ec_capabilities = ETHERCAP_VLAN_MTU;
883 1.50.2.2 christos #if 0 /* XXX not yet */
884 1.50.2.3 martin ec->ec_capabilities = ETHERCAP_VLAN_MTU | ETHERCAP_JUMBO_MTU;
885 1.50.2.2 christos #endif
886 1.50.2.2 christos
887 1.50.2.3 martin usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST,
888 1.50.2.3 martin 0, &unm);
889 1.50.2.2 christos }
890 1.50.2.2 christos
891 1.50.2.3 martin static unsigned
892 1.50.2.3 martin mue_uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
893 1.50.2.2 christos {
894 1.50.2.3 martin struct ifnet * const ifp = usbnet_ifp(un);
895 1.50.2.2 christos struct mue_txbuf_hdr hdr;
896 1.50.2.2 christos uint32_t tx_cmd_a, tx_cmd_b;
897 1.50.2.2 christos int csum, len, rv;
898 1.50.2.2 christos bool tso, ipe, tpe;
899 1.50.2.2 christos
900 1.50.2.3 martin if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz - sizeof(hdr))
901 1.50.2.3 martin return 0;
902 1.50.2.3 martin
903 1.50.2.2 christos csum = m->m_pkthdr.csum_flags;
904 1.50.2.2 christos tso = csum & (M_CSUM_TSOv4 | M_CSUM_TSOv6);
905 1.50.2.2 christos ipe = csum & M_CSUM_IPv4;
906 1.50.2.2 christos tpe = csum & (M_CSUM_TCPv4 | M_CSUM_UDPv4 |
907 1.50.2.2 christos M_CSUM_TCPv6 | M_CSUM_UDPv6);
908 1.50.2.2 christos
909 1.50.2.2 christos len = m->m_pkthdr.len;
910 1.50.2.2 christos if (__predict_false((!tso && len > (int)MUE_FRAME_LEN(ifp->if_mtu)) ||
911 1.50.2.2 christos ( tso && len > MUE_TSO_FRAME_LEN))) {
912 1.50.2.3 martin MUE_PRINTF(un, "packet length %d\n too long", len);
913 1.50.2.3 martin return 0;
914 1.50.2.2 christos }
915 1.50.2.2 christos
916 1.50.2.2 christos KASSERT((len & ~MUE_TX_CMD_A_LEN_MASK) == 0);
917 1.50.2.2 christos tx_cmd_a = len | MUE_TX_CMD_A_FCS;
918 1.50.2.2 christos
919 1.50.2.2 christos if (tso) {
920 1.50.2.2 christos tx_cmd_a |= MUE_TX_CMD_A_LSO;
921 1.50.2.2 christos if (__predict_true(m->m_pkthdr.segsz > MUE_TX_MSS_MIN))
922 1.50.2.2 christos tx_cmd_b = m->m_pkthdr.segsz;
923 1.50.2.2 christos else
924 1.50.2.2 christos tx_cmd_b = MUE_TX_MSS_MIN;
925 1.50.2.2 christos tx_cmd_b <<= MUE_TX_CMD_B_MSS_SHIFT;
926 1.50.2.2 christos KASSERT((tx_cmd_b & ~MUE_TX_CMD_B_MSS_MASK) == 0);
927 1.50.2.3 martin rv = mue_prepare_tso(un, m);
928 1.50.2.2 christos if (__predict_false(rv))
929 1.50.2.3 martin return 0;
930 1.50.2.2 christos } else {
931 1.50.2.2 christos if (ipe)
932 1.50.2.2 christos tx_cmd_a |= MUE_TX_CMD_A_IPE;
933 1.50.2.2 christos if (tpe)
934 1.50.2.2 christos tx_cmd_a |= MUE_TX_CMD_A_TPE;
935 1.50.2.2 christos tx_cmd_b = 0;
936 1.50.2.2 christos }
937 1.50.2.2 christos
938 1.50.2.2 christos hdr.tx_cmd_a = htole32(tx_cmd_a);
939 1.50.2.2 christos hdr.tx_cmd_b = htole32(tx_cmd_b);
940 1.50.2.2 christos
941 1.50.2.3 martin memcpy(c->unc_buf, &hdr, sizeof(hdr));
942 1.50.2.3 martin m_copydata(m, 0, len, c->unc_buf + sizeof(hdr));
943 1.50.2.2 christos
944 1.50.2.3 martin return len + sizeof(hdr);
945 1.50.2.2 christos }
946 1.50.2.2 christos
947 1.50.2.2 christos /*
948 1.50.2.2 christos * L3 length field should be cleared.
949 1.50.2.2 christos */
950 1.50.2.2 christos static int
951 1.50.2.3 martin mue_prepare_tso(struct usbnet *un, struct mbuf *m)
952 1.50.2.2 christos {
953 1.50.2.2 christos struct ether_header *eh;
954 1.50.2.2 christos struct ip *ip;
955 1.50.2.2 christos struct ip6_hdr *ip6;
956 1.50.2.2 christos uint16_t type, len = 0;
957 1.50.2.2 christos int off;
958 1.50.2.2 christos
959 1.50.2.2 christos if (__predict_true(m->m_len >= (int)sizeof(*eh))) {
960 1.50.2.2 christos eh = mtod(m, struct ether_header *);
961 1.50.2.2 christos type = eh->ether_type;
962 1.50.2.2 christos } else
963 1.50.2.2 christos m_copydata(m, offsetof(struct ether_header, ether_type),
964 1.50.2.2 christos sizeof(type), &type);
965 1.50.2.2 christos switch (type = htons(type)) {
966 1.50.2.2 christos case ETHERTYPE_IP:
967 1.50.2.2 christos case ETHERTYPE_IPV6:
968 1.50.2.2 christos off = ETHER_HDR_LEN;
969 1.50.2.2 christos break;
970 1.50.2.2 christos case ETHERTYPE_VLAN:
971 1.50.2.2 christos off = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
972 1.50.2.2 christos break;
973 1.50.2.2 christos default:
974 1.50.2.2 christos return EINVAL;
975 1.50.2.2 christos }
976 1.50.2.2 christos
977 1.50.2.2 christos if (m->m_pkthdr.csum_flags & M_CSUM_TSOv4) {
978 1.50.2.2 christos if (__predict_true(m->m_len >= off + (int)sizeof(*ip))) {
979 1.50.2.2 christos ip = (void *)(mtod(m, char *) + off);
980 1.50.2.2 christos ip->ip_len = 0;
981 1.50.2.2 christos } else
982 1.50.2.2 christos m_copyback(m, off + offsetof(struct ip, ip_len),
983 1.50.2.2 christos sizeof(len), &len);
984 1.50.2.2 christos } else {
985 1.50.2.2 christos if (__predict_true(m->m_len >= off + (int)sizeof(*ip6))) {
986 1.50.2.2 christos ip6 = (void *)(mtod(m, char *) + off);
987 1.50.2.2 christos ip6->ip6_plen = 0;
988 1.50.2.2 christos } else
989 1.50.2.2 christos m_copyback(m, off + offsetof(struct ip6_hdr, ip6_plen),
990 1.50.2.2 christos sizeof(len), &len);
991 1.50.2.2 christos }
992 1.50.2.2 christos return 0;
993 1.50.2.2 christos }
994 1.50.2.2 christos
995 1.50.2.2 christos static void
996 1.50.2.3 martin mue_setiff_locked(struct usbnet *un)
997 1.50.2.2 christos {
998 1.50.2.3 martin struct ethercom *ec = usbnet_ec(un);
999 1.50.2.3 martin struct ifnet * const ifp = usbnet_ifp(un);
1000 1.50.2.2 christos const uint8_t *enaddr = CLLADDR(ifp->if_sadl);
1001 1.50.2.2 christos struct ether_multi *enm;
1002 1.50.2.2 christos struct ether_multistep step;
1003 1.50.2.2 christos uint32_t pfiltbl[MUE_NUM_ADDR_FILTX][2];
1004 1.50.2.2 christos uint32_t hashtbl[MUE_DP_SEL_VHF_HASH_LEN];
1005 1.50.2.2 christos uint32_t reg, rxfilt, h, hireg, loreg;
1006 1.50.2.2 christos size_t i;
1007 1.50.2.2 christos
1008 1.50.2.3 martin if (usbnet_isdying(un))
1009 1.50.2.2 christos return;
1010 1.50.2.2 christos
1011 1.50.2.2 christos /* Clear perfect filter and hash tables. */
1012 1.50.2.2 christos memset(pfiltbl, 0, sizeof(pfiltbl));
1013 1.50.2.2 christos memset(hashtbl, 0, sizeof(hashtbl));
1014 1.50.2.2 christos
1015 1.50.2.3 martin reg = (un->un_flags & LAN7500) ? MUE_7500_RFE_CTL : MUE_7800_RFE_CTL;
1016 1.50.2.3 martin rxfilt = mue_csr_read(un, reg);
1017 1.50.2.2 christos rxfilt &= ~(MUE_RFE_CTL_PERFECT | MUE_RFE_CTL_MULTICAST_HASH |
1018 1.50.2.2 christos MUE_RFE_CTL_UNICAST | MUE_RFE_CTL_MULTICAST);
1019 1.50.2.2 christos
1020 1.50.2.2 christos /* Always accept broadcast frames. */
1021 1.50.2.2 christos rxfilt |= MUE_RFE_CTL_BROADCAST;
1022 1.50.2.2 christos
1023 1.50.2.2 christos if (ifp->if_flags & IFF_PROMISC) {
1024 1.50.2.2 christos rxfilt |= MUE_RFE_CTL_UNICAST;
1025 1.50.2.2 christos allmulti: rxfilt |= MUE_RFE_CTL_MULTICAST;
1026 1.50.2.2 christos ifp->if_flags |= IFF_ALLMULTI;
1027 1.50.2.2 christos if (ifp->if_flags & IFF_PROMISC)
1028 1.50.2.3 martin DPRINTF(un, "promisc\n");
1029 1.50.2.2 christos else
1030 1.50.2.3 martin DPRINTF(un, "allmulti\n");
1031 1.50.2.2 christos } else {
1032 1.50.2.2 christos /* Now program new ones. */
1033 1.50.2.2 christos pfiltbl[0][0] = MUE_ENADDR_HI(enaddr) | MUE_ADDR_FILTX_VALID;
1034 1.50.2.2 christos pfiltbl[0][1] = MUE_ENADDR_LO(enaddr);
1035 1.50.2.2 christos i = 1;
1036 1.50.2.2 christos ETHER_LOCK(ec);
1037 1.50.2.2 christos ETHER_FIRST_MULTI(step, ec, enm);
1038 1.50.2.2 christos while (enm != NULL) {
1039 1.50.2.2 christos if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
1040 1.50.2.2 christos ETHER_ADDR_LEN)) {
1041 1.50.2.2 christos memset(pfiltbl, 0, sizeof(pfiltbl));
1042 1.50.2.2 christos memset(hashtbl, 0, sizeof(hashtbl));
1043 1.50.2.2 christos rxfilt &= ~MUE_RFE_CTL_MULTICAST_HASH;
1044 1.50.2.2 christos ETHER_UNLOCK(ec);
1045 1.50.2.2 christos goto allmulti;
1046 1.50.2.2 christos }
1047 1.50.2.2 christos if (i < MUE_NUM_ADDR_FILTX) {
1048 1.50.2.2 christos /* Use perfect address table if possible. */
1049 1.50.2.2 christos pfiltbl[i][0] = MUE_ENADDR_HI(enm->enm_addrlo) |
1050 1.50.2.2 christos MUE_ADDR_FILTX_VALID;
1051 1.50.2.2 christos pfiltbl[i][1] = MUE_ENADDR_LO(enm->enm_addrlo);
1052 1.50.2.2 christos } else {
1053 1.50.2.2 christos /* Otherwise, use hash table. */
1054 1.50.2.2 christos rxfilt |= MUE_RFE_CTL_MULTICAST_HASH;
1055 1.50.2.2 christos h = (ether_crc32_be(enm->enm_addrlo,
1056 1.50.2.2 christos ETHER_ADDR_LEN) >> 23) & 0x1ff;
1057 1.50.2.2 christos hashtbl[h / 32] |= 1 << (h % 32);
1058 1.50.2.2 christos }
1059 1.50.2.2 christos i++;
1060 1.50.2.2 christos ETHER_NEXT_MULTI(step, enm);
1061 1.50.2.2 christos }
1062 1.50.2.2 christos ETHER_UNLOCK(ec);
1063 1.50.2.2 christos rxfilt |= MUE_RFE_CTL_PERFECT;
1064 1.50.2.2 christos ifp->if_flags &= ~IFF_ALLMULTI;
1065 1.50.2.2 christos if (rxfilt & MUE_RFE_CTL_MULTICAST_HASH)
1066 1.50.2.3 martin DPRINTF(un, "perfect filter and hash tables\n");
1067 1.50.2.2 christos else
1068 1.50.2.3 martin DPRINTF(un, "perfect filter\n");
1069 1.50.2.2 christos }
1070 1.50.2.2 christos
1071 1.50.2.2 christos for (i = 0; i < MUE_NUM_ADDR_FILTX; i++) {
1072 1.50.2.3 martin hireg = (un->un_flags & LAN7500) ?
1073 1.50.2.2 christos MUE_7500_ADDR_FILTX(i) : MUE_7800_ADDR_FILTX(i);
1074 1.50.2.2 christos loreg = hireg + 4;
1075 1.50.2.3 martin mue_csr_write(un, hireg, 0);
1076 1.50.2.3 martin mue_csr_write(un, loreg, pfiltbl[i][1]);
1077 1.50.2.3 martin mue_csr_write(un, hireg, pfiltbl[i][0]);
1078 1.50.2.2 christos }
1079 1.50.2.2 christos
1080 1.50.2.3 martin mue_dataport_write(un, MUE_DP_SEL_VHF, MUE_DP_SEL_VHF_VLAN_LEN,
1081 1.50.2.2 christos MUE_DP_SEL_VHF_HASH_LEN, hashtbl);
1082 1.50.2.2 christos
1083 1.50.2.3 martin mue_csr_write(un, reg, rxfilt);
1084 1.50.2.2 christos }
1085 1.50.2.2 christos
1086 1.50.2.2 christos static void
1087 1.50.2.3 martin mue_sethwcsum_locked(struct usbnet *un)
1088 1.50.2.2 christos {
1089 1.50.2.3 martin struct ifnet * const ifp = usbnet_ifp(un);
1090 1.50.2.2 christos uint32_t reg, val;
1091 1.50.2.2 christos
1092 1.50.2.3 martin reg = (un->un_flags & LAN7500) ? MUE_7500_RFE_CTL : MUE_7800_RFE_CTL;
1093 1.50.2.3 martin val = mue_csr_read(un, reg);
1094 1.50.2.2 christos
1095 1.50.2.2 christos if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
1096 1.50.2.3 martin DPRINTF(un, "RX IPv4 hwcsum enabled\n");
1097 1.50.2.2 christos val |= MUE_RFE_CTL_IP_COE;
1098 1.50.2.2 christos } else {
1099 1.50.2.3 martin DPRINTF(un, "RX IPv4 hwcsum disabled\n");
1100 1.50.2.2 christos val &= ~MUE_RFE_CTL_IP_COE;
1101 1.50.2.2 christos }
1102 1.50.2.2 christos
1103 1.50.2.2 christos if (ifp->if_capenable &
1104 1.50.2.2 christos (IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx |
1105 1.50.2.2 christos IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx)) {
1106 1.50.2.3 martin DPRINTF(un, "RX L4 hwcsum enabled\n");
1107 1.50.2.2 christos val |= MUE_RFE_CTL_TCPUDP_COE;
1108 1.50.2.2 christos } else {
1109 1.50.2.3 martin DPRINTF(un, "RX L4 hwcsum disabled\n");
1110 1.50.2.2 christos val &= ~MUE_RFE_CTL_TCPUDP_COE;
1111 1.50.2.2 christos }
1112 1.50.2.2 christos
1113 1.50.2.2 christos val &= ~MUE_RFE_CTL_VLAN_FILTER;
1114 1.50.2.2 christos
1115 1.50.2.3 martin mue_csr_write(un, reg, val);
1116 1.50.2.2 christos }
1117 1.50.2.2 christos
1118 1.50.2.2 christos static void
1119 1.50.2.3 martin mue_setmtu_locked(struct usbnet *un)
1120 1.50.2.2 christos {
1121 1.50.2.3 martin struct ifnet * const ifp = usbnet_ifp(un);
1122 1.50.2.2 christos uint32_t val;
1123 1.50.2.2 christos
1124 1.50.2.2 christos /* Set the maximum frame size. */
1125 1.50.2.3 martin MUE_CLRBIT(un, MUE_MAC_RX, MUE_MAC_RX_RXEN);
1126 1.50.2.3 martin val = mue_csr_read(un, MUE_MAC_RX);
1127 1.50.2.2 christos val &= ~MUE_MAC_RX_MAX_SIZE_MASK;
1128 1.50.2.2 christos val |= MUE_MAC_RX_MAX_LEN(MUE_FRAME_LEN(ifp->if_mtu));
1129 1.50.2.3 martin mue_csr_write(un, MUE_MAC_RX, val);
1130 1.50.2.3 martin MUE_SETBIT(un, MUE_MAC_RX, MUE_MAC_RX_RXEN);
1131 1.50.2.2 christos }
1132 1.50.2.2 christos
1133 1.50.2.2 christos static void
1134 1.50.2.3 martin mue_uno_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len)
1135 1.50.2.2 christos {
1136 1.50.2.3 martin struct ifnet * const ifp = usbnet_ifp(un);
1137 1.50.2.2 christos struct mue_rxbuf_hdr *hdrp;
1138 1.50.2.3 martin uint32_t rx_cmd_a;
1139 1.50.2.2 christos uint16_t pktlen;
1140 1.50.2.2 christos int csum;
1141 1.50.2.3 martin uint8_t *buf = c->unc_buf;
1142 1.50.2.2 christos bool v6;
1143 1.50.2.2 christos
1144 1.50.2.3 martin KASSERTMSG(total_len <= un->un_rx_bufsz, "%u vs %u",
1145 1.50.2.3 martin total_len, un->un_rx_bufsz);
1146 1.50.2.2 christos
1147 1.50.2.2 christos do {
1148 1.50.2.3 martin if (__predict_false(total_len < sizeof(*hdrp))) {
1149 1.50.2.3 martin MUE_PRINTF(un, "packet length %u too short\n", total_len);
1150 1.50.2.3 martin if_statinc(ifp, if_ierrors);
1151 1.50.2.3 martin return;
1152 1.50.2.2 christos }
1153 1.50.2.2 christos
1154 1.50.2.2 christos hdrp = (struct mue_rxbuf_hdr *)buf;
1155 1.50.2.2 christos rx_cmd_a = le32toh(hdrp->rx_cmd_a);
1156 1.50.2.2 christos
1157 1.50.2.2 christos if (__predict_false(rx_cmd_a & MUE_RX_CMD_A_ERRORS)) {
1158 1.50.2.2 christos /*
1159 1.50.2.2 christos * We cannot use MUE_RX_CMD_A_RED bit here;
1160 1.50.2.2 christos * it is turned on in the cases of L3/L4
1161 1.50.2.2 christos * checksum errors which we handle below.
1162 1.50.2.2 christos */
1163 1.50.2.3 martin MUE_PRINTF(un, "rx_cmd_a: %#x\n", rx_cmd_a);
1164 1.50.2.3 martin if_statinc(ifp, if_ierrors);
1165 1.50.2.3 martin return;
1166 1.50.2.2 christos }
1167 1.50.2.2 christos
1168 1.50.2.2 christos pktlen = (uint16_t)(rx_cmd_a & MUE_RX_CMD_A_LEN_MASK);
1169 1.50.2.3 martin if (un->un_flags & LAN7500)
1170 1.50.2.2 christos pktlen -= 2;
1171 1.50.2.2 christos
1172 1.50.2.2 christos if (__predict_false(pktlen < ETHER_HDR_LEN + ETHER_CRC_LEN ||
1173 1.50.2.2 christos pktlen > MCLBYTES - ETHER_ALIGN || /* XXX */
1174 1.50.2.3 martin pktlen + sizeof(*hdrp) > total_len)) {
1175 1.50.2.3 martin MUE_PRINTF(un, "invalid packet length %d\n", pktlen);
1176 1.50.2.3 martin if_statinc(ifp, if_ierrors);
1177 1.50.2.3 martin return;
1178 1.50.2.2 christos }
1179 1.50.2.2 christos
1180 1.50.2.2 christos if (__predict_false(rx_cmd_a & MUE_RX_CMD_A_ICSM)) {
1181 1.50.2.2 christos csum = 0;
1182 1.50.2.2 christos } else {
1183 1.50.2.2 christos v6 = rx_cmd_a & MUE_RX_CMD_A_IPV;
1184 1.50.2.2 christos switch (rx_cmd_a & MUE_RX_CMD_A_PID) {
1185 1.50.2.2 christos case MUE_RX_CMD_A_PID_TCP:
1186 1.50.2.2 christos csum = v6 ?
1187 1.50.2.2 christos M_CSUM_TCPv6 : M_CSUM_IPv4 | M_CSUM_TCPv4;
1188 1.50.2.2 christos break;
1189 1.50.2.2 christos case MUE_RX_CMD_A_PID_UDP:
1190 1.50.2.2 christos csum = v6 ?
1191 1.50.2.2 christos M_CSUM_UDPv6 : M_CSUM_IPv4 | M_CSUM_UDPv4;
1192 1.50.2.2 christos break;
1193 1.50.2.2 christos case MUE_RX_CMD_A_PID_IP:
1194 1.50.2.2 christos csum = v6 ? 0 : M_CSUM_IPv4;
1195 1.50.2.2 christos break;
1196 1.50.2.2 christos default:
1197 1.50.2.2 christos csum = 0;
1198 1.50.2.2 christos break;
1199 1.50.2.2 christos }
1200 1.50.2.2 christos csum &= ifp->if_csum_flags_rx;
1201 1.50.2.2 christos if (__predict_false((csum & M_CSUM_IPv4) &&
1202 1.50.2.2 christos (rx_cmd_a & MUE_RX_CMD_A_ICE)))
1203 1.50.2.2 christos csum |= M_CSUM_IPv4_BAD;
1204 1.50.2.2 christos if (__predict_false((csum & ~M_CSUM_IPv4) &&
1205 1.50.2.2 christos (rx_cmd_a & MUE_RX_CMD_A_TCE)))
1206 1.50.2.2 christos csum |= M_CSUM_TCP_UDP_BAD;
1207 1.50.2.2 christos }
1208 1.50.2.3 martin
1209 1.50.2.3 martin usbnet_enqueue(un, buf + sizeof(*hdrp), pktlen, csum,
1210 1.50.2.3 martin 0, M_HASFCS);
1211 1.50.2.2 christos
1212 1.50.2.2 christos /* Attention: sizeof(hdr) = 10 */
1213 1.50.2.2 christos pktlen = roundup(pktlen + sizeof(*hdrp), 4);
1214 1.50.2.3 martin if (pktlen > total_len)
1215 1.50.2.3 martin pktlen = total_len;
1216 1.50.2.3 martin total_len -= pktlen;
1217 1.50.2.2 christos buf += pktlen;
1218 1.50.2.3 martin } while (total_len > 0);
1219 1.50.2.2 christos }
1220 1.50.2.2 christos
1221 1.50.2.2 christos static int
1222 1.50.2.3 martin mue_init_locked(struct ifnet *ifp)
1223 1.50.2.2 christos {
1224 1.50.2.3 martin struct usbnet * const un = ifp->if_softc;
1225 1.50.2.2 christos
1226 1.50.2.3 martin if (usbnet_isdying(un)) {
1227 1.50.2.3 martin DPRINTF(un, "dying\n");
1228 1.50.2.2 christos return EIO;
1229 1.50.2.2 christos }
1230 1.50.2.2 christos
1231 1.50.2.2 christos /* Cancel pending I/O and free all TX/RX buffers. */
1232 1.50.2.2 christos if (ifp->if_flags & IFF_RUNNING)
1233 1.50.2.3 martin usbnet_stop(un, ifp, 1);
1234 1.50.2.2 christos
1235 1.50.2.3 martin mue_reset(un);
1236 1.50.2.2 christos
1237 1.50.2.2 christos /* Set MAC address. */
1238 1.50.2.3 martin mue_set_macaddr(un);
1239 1.50.2.2 christos
1240 1.50.2.2 christos /* Load the multicast filter. */
1241 1.50.2.3 martin mue_setiff_locked(un);
1242 1.50.2.2 christos
1243 1.50.2.2 christos /* TCP/UDP checksum offload engines. */
1244 1.50.2.3 martin mue_sethwcsum_locked(un);
1245 1.50.2.2 christos
1246 1.50.2.2 christos /* Set MTU. */
1247 1.50.2.3 martin mue_setmtu_locked(un);
1248 1.50.2.2 christos
1249 1.50.2.3 martin return usbnet_init_rx_tx(un);
1250 1.50.2.3 martin }
1251 1.50.2.2 christos
1252 1.50.2.3 martin static int
1253 1.50.2.3 martin mue_uno_init(struct ifnet *ifp)
1254 1.50.2.3 martin {
1255 1.50.2.3 martin struct usbnet * const un = ifp->if_softc;
1256 1.50.2.3 martin int rv;
1257 1.50.2.2 christos
1258 1.50.2.3 martin usbnet_lock_core(un);
1259 1.50.2.3 martin usbnet_busy(un);
1260 1.50.2.3 martin rv = mue_init_locked(ifp);
1261 1.50.2.3 martin usbnet_unbusy(un);
1262 1.50.2.3 martin usbnet_unlock_core(un);
1263 1.50.2.2 christos
1264 1.50.2.3 martin return rv;
1265 1.50.2.2 christos }
1266 1.50.2.2 christos
1267 1.50.2.2 christos static int
1268 1.50.2.3 martin mue_uno_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1269 1.50.2.2 christos {
1270 1.50.2.3 martin struct usbnet * const un = ifp->if_softc;
1271 1.50.2.2 christos
1272 1.50.2.3 martin usbnet_lock_core(un);
1273 1.50.2.3 martin usbnet_busy(un);
1274 1.50.2.2 christos
1275 1.50.2.2 christos switch (cmd) {
1276 1.50.2.2 christos case SIOCSIFFLAGS:
1277 1.50.2.3 martin case SIOCSETHERCAP:
1278 1.50.2.3 martin case SIOCADDMULTI:
1279 1.50.2.3 martin case SIOCDELMULTI:
1280 1.50.2.3 martin mue_setiff_locked(un);
1281 1.50.2.3 martin break;
1282 1.50.2.3 martin case SIOCSIFCAP:
1283 1.50.2.3 martin mue_sethwcsum_locked(un);
1284 1.50.2.3 martin break;
1285 1.50.2.3 martin case SIOCSIFMTU:
1286 1.50.2.3 martin mue_setmtu_locked(un);
1287 1.50.2.2 christos break;
1288 1.50.2.2 christos default:
1289 1.50.2.2 christos break;
1290 1.50.2.2 christos }
1291 1.50.2.2 christos
1292 1.50.2.3 martin usbnet_unbusy(un);
1293 1.50.2.3 martin usbnet_unlock_core(un);
1294 1.50.2.2 christos
1295 1.50.2.3 martin return 0;
1296 1.50.2.2 christos }
1297 1.50.2.2 christos
1298 1.50.2.2 christos static void
1299 1.50.2.3 martin mue_reset(struct usbnet *un)
1300 1.50.2.2 christos {
1301 1.50.2.3 martin if (usbnet_isdying(un))
1302 1.50.2.2 christos return;
1303 1.50.2.2 christos
1304 1.50.2.2 christos /* Wait a little while for the chip to get its brains in order. */
1305 1.50.2.3 martin usbd_delay_ms(un->un_udev, 1);
1306 1.50.2.2 christos
1307 1.50.2.3 martin // mue_chip_init(un); /* XXX */
1308 1.50.2.2 christos }
1309 1.50.2.2 christos
1310 1.50.2.2 christos static void
1311 1.50.2.3 martin mue_uno_stop(struct ifnet *ifp, int disable)
1312 1.50.2.2 christos {
1313 1.50.2.3 martin struct usbnet * const un = ifp->if_softc;
1314 1.50.2.2 christos
1315 1.50.2.3 martin mue_reset(un);
1316 1.50.2.2 christos }
1317 1.50.2.2 christos
1318 1.50.2.3 martin #ifdef _MODULE
1319 1.50.2.3 martin #include "ioconf.c"
1320 1.50.2.3 martin #endif
1321 1.50.2.2 christos
1322 1.50.2.3 martin USBNET_MODULE(mue)
1323