if_axe.c revision 1.128 1 /* $NetBSD: if_axe.c,v 1.128 2020/03/01 10:16:17 nisimura Exp $ */
2 /* $OpenBSD: if_axe.c,v 1.137 2016/04/13 11:03:37 mpi Exp $ */
3
4 /*
5 * Copyright (c) 2005, 2006, 2007 Jonathan Gray <jsg (at) openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 /*
21 * Copyright (c) 1997, 1998, 1999, 2000-2003
22 * Bill Paul <wpaul (at) windriver.com>. All rights reserved.
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This product includes software developed by Bill Paul.
35 * 4. Neither the name of the author nor the names of any co-contributors
36 * may be used to endorse or promote products derived from this software
37 * without specific prior written permission.
38 *
39 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
40 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
43 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
49 * THE POSSIBILITY OF SUCH DAMAGE.
50 */
51
52 /*
53 * ASIX Electronics AX88172/AX88178/AX88778 USB 2.0 ethernet driver.
54 * Used in the LinkSys USB200M and various other adapters.
55 *
56 * Written by Bill Paul <wpaul (at) windriver.com>
57 * Senior Engineer
58 * Wind River Systems
59 */
60
61 /*
62 * The AX88172 provides USB ethernet supports at 10 and 100Mbps.
63 * It uses an external PHY (reference designs use a RealTek chip),
64 * and has a 64-bit multicast hash filter. There is some information
65 * missing from the manual which one needs to know in order to make
66 * the chip function:
67 *
68 * - You must set bit 7 in the RX control register, otherwise the
69 * chip won't receive any packets.
70 * - You must initialize all 3 IPG registers, or you won't be able
71 * to send any packets.
72 *
73 * Note that this device appears to only support loading the station
74 * address via autoload from the EEPROM (i.e. there's no way to manually
75 * set it).
76 *
77 * (Adam Weinberger wanted me to name this driver if_gir.c.)
78 */
79
80 /*
81 * Ax88178 and Ax88772 support backported from the OpenBSD driver.
82 * 2007/02/12, J.R. Oldroyd, fbsd (at) opal.com
83 *
84 * Manual here:
85 * http://www.asix.com.tw/FrootAttach/datasheet/AX88178_datasheet_Rev10.pdf
86 * http://www.asix.com.tw/FrootAttach/datasheet/AX88772_datasheet_Rev10.pdf
87 */
88
89 #include <sys/cdefs.h>
90 __KERNEL_RCSID(0, "$NetBSD: if_axe.c,v 1.128 2020/03/01 10:16:17 nisimura Exp $");
91
92 #ifdef _KERNEL_OPT
93 #include "opt_usb.h"
94 #include "opt_net_mpsafe.h"
95 #endif
96
97 #include <sys/param.h>
98
99 #include <dev/usb/usbnet.h>
100 #include <dev/usb/usbhist.h>
101 #include <dev/usb/if_axereg.h>
102
103 struct axe_type {
104 struct usb_devno axe_dev;
105 uint16_t axe_flags;
106 };
107
108 struct axe_softc {
109 struct usbnet axe_un;
110
111 /* usbnet:un_flags values */
112 #define AX178 __BIT(0) /* AX88178 */
113 #define AX772 __BIT(1) /* AX88772 */
114 #define AX772A __BIT(2) /* AX88772A */
115 #define AX772B __BIT(3) /* AX88772B */
116 #define AXSTD_FRAME __BIT(12)
117 #define AXCSUM_FRAME __BIT(13)
118
119 uint8_t axe_ipgs[3];
120 uint8_t axe_phyaddrs[2];
121 uint16_t sc_pwrcfg;
122 uint16_t sc_lenmask;
123
124 };
125
126 #define AXE_IS_178_FAMILY(un) \
127 ((un)->un_flags & (AX772 | AX772A | AX772B | AX178))
128
129 #define AXE_IS_772(un) \
130 ((un)->un_flags & (AX772 | AX772A | AX772B))
131
132 #define AX_RXCSUM \
133 (IFCAP_CSUM_IPv4_Rx | \
134 IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx | \
135 IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx)
136
137 #define AX_TXCSUM \
138 (IFCAP_CSUM_IPv4_Tx | \
139 IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_UDPv4_Tx | \
140 IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_UDPv6_Tx)
141
142 /*
143 * AXE_178_MAX_FRAME_BURST
144 * max frame burst size for Ax88178 and Ax88772
145 * 0 2048 bytes
146 * 1 4096 bytes
147 * 2 8192 bytes
148 * 3 16384 bytes
149 * use the largest your system can handle without USB stalling.
150 *
151 * NB: 88772 parts appear to generate lots of input errors with
152 * a 2K rx buffer and 8K is only slightly faster than 4K on an
153 * EHCI port on a T42 so change at your own risk.
154 */
155 #define AXE_178_MAX_FRAME_BURST 1
156
157
158 #ifdef USB_DEBUG
159 #ifndef AXE_DEBUG
160 #define axedebug 0
161 #else
162 static int axedebug = 0;
163
164 SYSCTL_SETUP(sysctl_hw_axe_setup, "sysctl hw.axe setup")
165 {
166 int err;
167 const struct sysctlnode *rnode;
168 const struct sysctlnode *cnode;
169
170 err = sysctl_createv(clog, 0, NULL, &rnode,
171 CTLFLAG_PERMANENT, CTLTYPE_NODE, "axe",
172 SYSCTL_DESCR("axe global controls"),
173 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
174
175 if (err)
176 goto fail;
177
178 /* control debugging printfs */
179 err = sysctl_createv(clog, 0, &rnode, &cnode,
180 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
181 "debug", SYSCTL_DESCR("Enable debugging output"),
182 NULL, 0, &axedebug, sizeof(axedebug), CTL_CREATE, CTL_EOL);
183 if (err)
184 goto fail;
185
186 return;
187 fail:
188 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
189 }
190
191 #endif /* AXE_DEBUG */
192 #endif /* USB_DEBUG */
193
194 #define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(axedebug,1,FMT,A,B,C,D)
195 #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(axedebug,N,FMT,A,B,C,D)
196 #define AXEHIST_FUNC() USBHIST_FUNC()
197 #define AXEHIST_CALLED(name) USBHIST_CALLED(axedebug)
198
199 /*
200 * Various supported device vendors/products.
201 */
202 static const struct axe_type axe_devs[] = {
203 { { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_UFE2000}, 0 },
204 { { USB_VENDOR_ACERCM, USB_PRODUCT_ACERCM_EP1427X2}, 0 },
205 { { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_ETHERNET }, AX772 },
206 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88172}, 0 },
207 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88772}, AX772 },
208 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88772A}, AX772 },
209 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88772B}, AX772B },
210 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88772B_1}, AX772B },
211 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88178}, AX178 },
212 { { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC210T}, 0 },
213 { { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D5055 }, AX178 },
214 { { USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB2AR}, 0},
215 { { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_USB200MV2}, AX772A },
216 { { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB2_TX }, 0},
217 { { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100}, 0 },
218 { { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100B1 }, AX772 },
219 { { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DUBE100B1 }, AX772 },
220 { { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100C1 }, AX772B },
221 { { USB_VENDOR_GOODWAY, USB_PRODUCT_GOODWAY_GWUSB2E}, 0 },
222 { { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_ETGUS2 }, AX178 },
223 { { USB_VENDOR_JVC, USB_PRODUCT_JVC_MP_PRX1}, 0 },
224 { { USB_VENDOR_LENOVO, USB_PRODUCT_LENOVO_ETHERNET }, AX772B },
225 { { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_HG20F9}, AX772B },
226 { { USB_VENDOR_LINKSYS2, USB_PRODUCT_LINKSYS2_USB200M}, 0 },
227 { { USB_VENDOR_LINKSYS4, USB_PRODUCT_LINKSYS4_USB1000 }, AX178 },
228 { { USB_VENDOR_LOGITEC, USB_PRODUCT_LOGITEC_LAN_GTJU2}, AX178 },
229 { { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAU2GT}, AX178 },
230 { { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAU2KTX}, 0 },
231 { { USB_VENDOR_MSI, USB_PRODUCT_MSI_AX88772A}, AX772 },
232 { { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_FA120}, 0 },
233 { { USB_VENDOR_OQO, USB_PRODUCT_OQO_ETHER01PLUS }, AX772 },
234 { { USB_VENDOR_PLANEX3, USB_PRODUCT_PLANEX3_GU1000T }, AX178 },
235 { { USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_LN029}, 0 },
236 { { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_LN028 }, AX178 },
237 { { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_LN031 }, AX178 },
238 { { USB_VENDOR_SYSTEMTALKS, USB_PRODUCT_SYSTEMTALKS_SGCX2UL}, 0 },
239 };
240 #define axe_lookup(v, p) ((const struct axe_type *)usb_lookup(axe_devs, v, p))
241
242 static const struct ax88772b_mfb ax88772b_mfb_table[] = {
243 { 0x8000, 0x8001, 2048 },
244 { 0x8100, 0x8147, 4096 },
245 { 0x8200, 0x81EB, 6144 },
246 { 0x8300, 0x83D7, 8192 },
247 { 0x8400, 0x851E, 16384 },
248 { 0x8500, 0x8666, 20480 },
249 { 0x8600, 0x87AE, 24576 },
250 { 0x8700, 0x8A3D, 32768 }
251 };
252
253 static int axe_match(device_t, cfdata_t, void *);
254 static void axe_attach(device_t, device_t, void *);
255
256 CFATTACH_DECL_NEW(axe, sizeof(struct axe_softc),
257 axe_match, axe_attach, usbnet_detach, usbnet_activate);
258
259 static void axe_stop(struct ifnet *, int);
260 static int axe_ioctl(struct ifnet *, u_long, void *);
261 static int axe_init(struct ifnet *);
262 static int axe_mii_read_reg(struct usbnet *, int, int, uint16_t *);
263 static int axe_mii_write_reg(struct usbnet *, int, int, uint16_t);
264 static void axe_mii_statchg(struct ifnet *);
265 static void axe_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t);
266 static unsigned axe_tx_prepare(struct usbnet *, struct mbuf *,
267 struct usbnet_chain *);
268
269 static void axe_ax88178_init(struct axe_softc *);
270 static void axe_ax88772_init(struct axe_softc *);
271 static void axe_ax88772a_init(struct axe_softc *);
272 static void axe_ax88772b_init(struct axe_softc *);
273
274 static const struct usbnet_ops axe_ops = {
275 .uno_stop = axe_stop,
276 .uno_ioctl = axe_ioctl,
277 .uno_read_reg = axe_mii_read_reg,
278 .uno_write_reg = axe_mii_write_reg,
279 .uno_statchg = axe_mii_statchg,
280 .uno_tx_prepare = axe_tx_prepare,
281 .uno_rx_loop = axe_rx_loop,
282 .uno_init = axe_init,
283 };
284
285 static usbd_status
286 axe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
287 {
288 AXEHIST_FUNC(); AXEHIST_CALLED();
289 struct usbnet * const un = &sc->axe_un;
290 usb_device_request_t req;
291 usbd_status err;
292
293 usbnet_isowned_mii(un);
294
295 if (usbnet_isdying(un))
296 return -1;
297
298 DPRINTFN(20, "cmd %#jx index %#jx val %#jx", cmd, index, val, 0);
299
300 if (AXE_CMD_DIR(cmd))
301 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
302 else
303 req.bmRequestType = UT_READ_VENDOR_DEVICE;
304 req.bRequest = AXE_CMD_CMD(cmd);
305 USETW(req.wValue, val);
306 USETW(req.wIndex, index);
307 USETW(req.wLength, AXE_CMD_LEN(cmd));
308
309 err = usbd_do_request(un->un_udev, &req, buf);
310 if (err)
311 DPRINTF("cmd %jd err %jd", cmd, err, 0, 0);
312
313 return err;
314 }
315
316 static int
317 axe_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val)
318 {
319 AXEHIST_FUNC(); AXEHIST_CALLED();
320 struct axe_softc * const sc = usbnet_softc(un);
321 usbd_status err;
322 uint16_t data;
323
324 DPRINTFN(30, "phy %#jx reg %#jx\n", phy, reg, 0, 0);
325
326 if (un->un_phyno != phy)
327 return EINVAL;
328
329 axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
330
331 err = axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, &data);
332 axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
333
334 if (err) {
335 aprint_error_dev(un->un_dev, "read PHY failed\n");
336 return EIO;
337 }
338
339 *val = le16toh(data);
340 if (AXE_IS_772(un) && reg == MII_BMSR) {
341 /*
342 * BMSR of AX88772 indicates that it supports extended
343 * capability but the extended status register is
344 * reserved for embedded ethernet PHY. So clear the
345 * extended capability bit of BMSR.
346 */
347 *val &= ~BMSR_EXTCAP;
348 }
349
350 DPRINTFN(30, "phy %#jx reg %#jx val %#jx", phy, reg, *val, 0);
351
352 return 0;
353 }
354
355 static int
356 axe_mii_write_reg(struct usbnet *un, int phy, int reg, uint16_t val)
357 {
358 struct axe_softc * const sc = usbnet_softc(un);
359 usbd_status err;
360 uint16_t aval;
361
362 if (un->un_phyno != phy)
363 return EINVAL;
364
365 aval = htole16(val);
366
367 axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
368 err = axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, &aval);
369 axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
370
371 if (err)
372 return EIO;
373 return 0;
374 }
375
376 static void
377 axe_mii_statchg(struct ifnet *ifp)
378 {
379 AXEHIST_FUNC(); AXEHIST_CALLED();
380
381 struct usbnet * const un = ifp->if_softc;
382 struct axe_softc * const sc = usbnet_softc(un);
383 struct mii_data *mii = usbnet_mii(un);
384 int val, err;
385
386 if (usbnet_isdying(un))
387 return;
388
389 val = 0;
390 if (mii->mii_media_active & IFM_FDX) {
391 val |= AXE_MEDIA_FULL_DUPLEX;
392 if (AXE_IS_178_FAMILY(un)) {
393 if (mii->mii_media_active & IFM_ETH_TXPAUSE)
394 val |= AXE_178_MEDIA_TXFLOW_CONTROL_EN;
395 if (mii->mii_media_active & IFM_ETH_RXPAUSE)
396 val |= AXE_178_MEDIA_RXFLOW_CONTROL_EN;
397 }
398 }
399 if (AXE_IS_178_FAMILY(un)) {
400 val |= AXE_178_MEDIA_RX_EN | AXE_178_MEDIA_MAGIC;
401 if (un->un_flags & AX178)
402 val |= AXE_178_MEDIA_ENCK;
403 switch (IFM_SUBTYPE(mii->mii_media_active)) {
404 case IFM_1000_T:
405 val |= AXE_178_MEDIA_GMII | AXE_178_MEDIA_ENCK;
406 usbnet_set_link(un, true);
407 break;
408 case IFM_100_TX:
409 val |= AXE_178_MEDIA_100TX;
410 usbnet_set_link(un, true);
411 break;
412 case IFM_10_T:
413 usbnet_set_link(un, true);
414 break;
415 }
416 }
417
418 DPRINTF("val=%#jx", val, 0, 0, 0);
419 usbnet_lock_mii(un);
420 err = axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL);
421 usbnet_unlock_mii(un);
422 if (err)
423 aprint_error_dev(un->un_dev, "media change failed\n");
424 }
425
426 static void
427 axe_setiff_locked(struct usbnet *un)
428 {
429 AXEHIST_FUNC(); AXEHIST_CALLED();
430 struct axe_softc * const sc = usbnet_softc(un);
431 struct ifnet * const ifp = usbnet_ifp(un);
432 struct ethercom *ec = usbnet_ec(un);
433 struct ether_multi *enm;
434 struct ether_multistep step;
435 uint32_t h = 0;
436 uint16_t rxmode;
437 uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
438
439 usbnet_isowned_mii(un);
440
441 if (usbnet_isdying(un))
442 return;
443
444 if (axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, &rxmode)) {
445 aprint_error_dev(un->un_dev, "can't read rxmode");
446 return;
447 }
448 rxmode = le16toh(rxmode);
449
450 rxmode &=
451 ~(AXE_RXCMD_ALLMULTI | AXE_RXCMD_PROMISC | AXE_RXCMD_MULTICAST);
452
453 if (ifp->if_flags & IFF_PROMISC) {
454 ifp->if_flags |= IFF_ALLMULTI;
455 goto allmulti;
456 }
457 ifp->if_flags &= ~IFF_ALLMULTI;
458
459 /* Now program new ones */
460 ETHER_LOCK(ec);
461 ETHER_FIRST_MULTI(step, ec, enm);
462 while (enm != NULL) {
463 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
464 ETHER_ADDR_LEN) != 0) {
465 ETHER_UNLOCK(ec);
466 ifp->if_flags |= IFF_ALLMULTI;
467 goto allmulti;
468 }
469
470 h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26;
471 hashtbl[h >> 3] |= 1U << (h & 7);
472 ETHER_NEXT_MULTI(step, enm);
473 }
474 ETHER_UNLOCK(ec);
475
476 rxmode |= AXE_RXCMD_MULTICAST; /* activate mcast hash filter */
477 axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, hashtbl);
478 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
479 return;
480
481 allmulti:
482 if (ifp->if_flags & IFF_PROMISC)
483 rxmode |= AXE_RXCMD_PROMISC; /* run promisc. mode */
484 rxmode |= AXE_RXCMD_ALLMULTI; /* accept all mcast frames */
485 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
486 }
487
488 static void
489 axe_setiff(struct usbnet *un)
490 {
491 usbnet_lock_mii(un);
492 axe_setiff_locked(un);
493 usbnet_unlock_mii(un);
494 }
495
496 static void
497 axe_ax_init(struct usbnet *un)
498 {
499 struct axe_softc * const sc = usbnet_softc(un);
500
501 int cmd = AXE_178_CMD_READ_NODEID;
502
503 if (un->un_flags & AX178) {
504 axe_ax88178_init(sc);
505 } else if (un->un_flags & AX772) {
506 axe_ax88772_init(sc);
507 } else if (un->un_flags & AX772A) {
508 axe_ax88772a_init(sc);
509 } else if (un->un_flags & AX772B) {
510 axe_ax88772b_init(sc);
511 return;
512 } else {
513 cmd = AXE_172_CMD_READ_NODEID;
514 }
515
516 if (axe_cmd(sc, cmd, 0, 0, un->un_eaddr)) {
517 aprint_error_dev(un->un_dev,
518 "failed to read ethernet address\n");
519 }
520 }
521
522
523 static void
524 axe_reset(struct usbnet *un)
525 {
526
527 usbnet_isowned_mii(un);
528
529 if (usbnet_isdying(un))
530 return;
531
532 /*
533 * softnet_lock can be taken when NET_MPAFE is not defined when calling
534 * if_addr_init -> if_init. This doesn't mix well with the
535 * usbd_delay_ms calls in the init routines as things like nd6_slowtimo
536 * can fire during the wait and attempt to take softnet_lock and then
537 * block the softclk thread meaning the wait never ends.
538 */
539 #ifndef NET_MPSAFE
540 /* XXX What to reset? */
541
542 /* Wait a little while for the chip to get its brains in order. */
543 DELAY(1000);
544 #else
545 axe_ax_init(un);
546 #endif
547 }
548
549 static int
550 axe_get_phyno(struct axe_softc *sc, int sel)
551 {
552 int phyno;
553
554 switch (AXE_PHY_TYPE(sc->axe_phyaddrs[sel])) {
555 case PHY_TYPE_100_HOME:
556 /* FALLTHROUGH */
557 case PHY_TYPE_GIG:
558 phyno = AXE_PHY_NO(sc->axe_phyaddrs[sel]);
559 break;
560 case PHY_TYPE_SPECIAL:
561 /* FALLTHROUGH */
562 case PHY_TYPE_RSVD:
563 /* FALLTHROUGH */
564 case PHY_TYPE_NON_SUP:
565 /* FALLTHROUGH */
566 default:
567 phyno = -1;
568 break;
569 }
570
571 return phyno;
572 }
573
574 #define AXE_GPIO_WRITE(x, y) do { \
575 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, (x), NULL); \
576 usbd_delay_ms(sc->axe_un.un_udev, hztoms(y)); \
577 } while (0)
578
579 static void
580 axe_ax88178_init(struct axe_softc *sc)
581 {
582 AXEHIST_FUNC(); AXEHIST_CALLED();
583 struct usbnet * const un = &sc->axe_un;
584 int gpio0, ledmode, phymode;
585 uint16_t eeprom, val;
586
587 axe_cmd(sc, AXE_CMD_SROM_WR_ENABLE, 0, 0, NULL);
588 /* XXX magic */
589 if (axe_cmd(sc, AXE_CMD_SROM_READ, 0, 0x0017, &eeprom) != 0)
590 eeprom = 0xffff;
591 axe_cmd(sc, AXE_CMD_SROM_WR_DISABLE, 0, 0, NULL);
592
593 eeprom = le16toh(eeprom);
594
595 DPRINTF("EEPROM is %#jx", eeprom, 0, 0, 0);
596
597 /* if EEPROM is invalid we have to use to GPIO0 */
598 if (eeprom == 0xffff) {
599 phymode = AXE_PHY_MODE_MARVELL;
600 gpio0 = 1;
601 ledmode = 0;
602 } else {
603 phymode = eeprom & 0x7f;
604 gpio0 = (eeprom & 0x80) ? 0 : 1;
605 ledmode = eeprom >> 8;
606 }
607
608 DPRINTF("use gpio0: %jd, phymode %jd", gpio0, phymode, 0, 0);
609
610 /* Program GPIOs depending on PHY hardware. */
611 switch (phymode) {
612 case AXE_PHY_MODE_MARVELL:
613 if (gpio0 == 1) {
614 AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO0_EN,
615 hz / 32);
616 AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2 | AXE_GPIO2_EN,
617 hz / 32);
618 AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2_EN, hz / 4);
619 AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2 | AXE_GPIO2_EN,
620 hz / 32);
621 } else {
622 AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 |
623 AXE_GPIO1_EN, hz / 3);
624 if (ledmode == 1) {
625 AXE_GPIO_WRITE(AXE_GPIO1_EN, hz / 3);
626 AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN,
627 hz / 3);
628 } else {
629 AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN |
630 AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
631 AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN |
632 AXE_GPIO2_EN, hz / 4);
633 AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN |
634 AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
635 }
636 }
637 break;
638 case AXE_PHY_MODE_CICADA:
639 case AXE_PHY_MODE_CICADA_V2:
640 case AXE_PHY_MODE_CICADA_V2_ASIX:
641 if (gpio0 == 1)
642 AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO0 |
643 AXE_GPIO0_EN, hz / 32);
644 else
645 AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 |
646 AXE_GPIO1_EN, hz / 32);
647 break;
648 case AXE_PHY_MODE_AGERE:
649 AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 |
650 AXE_GPIO1_EN, hz / 32);
651 AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2 |
652 AXE_GPIO2_EN, hz / 32);
653 AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2_EN, hz / 4);
654 AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2 |
655 AXE_GPIO2_EN, hz / 32);
656 break;
657 case AXE_PHY_MODE_REALTEK_8211CL:
658 case AXE_PHY_MODE_REALTEK_8211BN:
659 case AXE_PHY_MODE_REALTEK_8251CL:
660 val = gpio0 == 1 ? AXE_GPIO0 | AXE_GPIO0_EN :
661 AXE_GPIO1 | AXE_GPIO1_EN;
662 AXE_GPIO_WRITE(val, hz / 32);
663 AXE_GPIO_WRITE(val | AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
664 AXE_GPIO_WRITE(val | AXE_GPIO2_EN, hz / 4);
665 AXE_GPIO_WRITE(val | AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
666 if (phymode == AXE_PHY_MODE_REALTEK_8211CL) {
667 axe_mii_write_reg(un, un->un_phyno, 0x1F, 0x0005);
668 axe_mii_write_reg(un, un->un_phyno, 0x0C, 0x0000);
669 axe_mii_read_reg(un, un->un_phyno, 0x0001, &val);
670 axe_mii_write_reg(un, un->un_phyno, 0x01, val | 0x0080);
671 axe_mii_write_reg(un, un->un_phyno, 0x1F, 0x0000);
672 }
673 break;
674 default:
675 /* Unknown PHY model or no need to program GPIOs. */
676 break;
677 }
678
679 /* soft reset */
680 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
681 usbd_delay_ms(un->un_udev, 150);
682 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
683 AXE_SW_RESET_PRL | AXE_178_RESET_MAGIC, NULL);
684 usbd_delay_ms(un->un_udev, 150);
685 /* Enable MII/GMII/RGMII interface to work with external PHY. */
686 axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0, NULL);
687 usbd_delay_ms(un->un_udev, 10);
688 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
689 }
690
691 static void
692 axe_ax88772_init(struct axe_softc *sc)
693 {
694 AXEHIST_FUNC(); AXEHIST_CALLED();
695 struct usbnet * const un = &sc->axe_un;
696
697 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x00b0, NULL);
698 usbd_delay_ms(un->un_udev, 40);
699
700 if (un->un_phyno == AXE_772_PHY_NO_EPHY) {
701 /* ask for the embedded PHY */
702 axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0,
703 AXE_SW_PHY_SELECT_EMBEDDED, NULL);
704 usbd_delay_ms(un->un_udev, 10);
705
706 /* power down and reset state, pin reset state */
707 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
708 usbd_delay_ms(un->un_udev, 60);
709
710 /* power down/reset state, pin operating state */
711 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
712 AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
713 usbd_delay_ms(un->un_udev, 150);
714
715 /* power up, reset */
716 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_PRL, NULL);
717
718 /* power up, operating */
719 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
720 AXE_SW_RESET_IPRL | AXE_SW_RESET_PRL, NULL);
721 } else {
722 /* ask for external PHY */
723 axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, AXE_SW_PHY_SELECT_EXT,
724 NULL);
725 usbd_delay_ms(un->un_udev, 10);
726
727 /* power down internal PHY */
728 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
729 AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
730 }
731
732 usbd_delay_ms(un->un_udev, 150);
733 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
734 }
735
736 static void
737 axe_ax88772_phywake(struct axe_softc *sc)
738 {
739 AXEHIST_FUNC(); AXEHIST_CALLED();
740 struct usbnet * const un = &sc->axe_un;
741
742 if (un->un_phyno == AXE_772_PHY_NO_EPHY) {
743 /* Manually select internal(embedded) PHY - MAC mode. */
744 axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0,
745 AXE_SW_PHY_SELECT_EMBEDDED, NULL);
746 usbd_delay_ms(un->un_udev, hztoms(hz / 32));
747 } else {
748 /*
749 * Manually select external PHY - MAC mode.
750 * Reverse MII/RMII is for AX88772A PHY mode.
751 */
752 axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, AXE_SW_PHY_SELECT_SS_ENB |
753 AXE_SW_PHY_SELECT_EXT | AXE_SW_PHY_SELECT_SS_MII, NULL);
754 usbd_delay_ms(un->un_udev, hztoms(hz / 32));
755 }
756
757 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPPD |
758 AXE_SW_RESET_IPRL, NULL);
759
760 /* T1 = min 500ns everywhere */
761 usbd_delay_ms(un->un_udev, 150);
762
763 /* Take PHY out of power down. */
764 if (un->un_phyno == AXE_772_PHY_NO_EPHY) {
765 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPRL, NULL);
766 } else {
767 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_PRTE, NULL);
768 }
769
770 /* 772 T2 is 60ms. 772A T2 is 160ms, 772B T2 is 600ms */
771 usbd_delay_ms(un->un_udev, 600);
772
773 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
774
775 /* T3 = 500ns everywhere */
776 usbd_delay_ms(un->un_udev, hztoms(hz / 32));
777 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPRL, NULL);
778 usbd_delay_ms(un->un_udev, hztoms(hz / 32));
779 }
780
781 static void
782 axe_ax88772a_init(struct axe_softc *sc)
783 {
784 AXEHIST_FUNC(); AXEHIST_CALLED();
785
786 /* Reload EEPROM. */
787 AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM, hz / 32);
788 axe_ax88772_phywake(sc);
789 /* Stop MAC. */
790 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
791 }
792
793 static void
794 axe_ax88772b_init(struct axe_softc *sc)
795 {
796 AXEHIST_FUNC(); AXEHIST_CALLED();
797 struct usbnet * const un = &sc->axe_un;
798 uint16_t eeprom;
799 int i;
800
801 /* Reload EEPROM. */
802 AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM , hz / 32);
803
804 /*
805 * Save PHY power saving configuration(high byte) and
806 * clear EEPROM checksum value(low byte).
807 */
808 if (axe_cmd(sc, AXE_CMD_SROM_READ, 0, AXE_EEPROM_772B_PHY_PWRCFG,
809 &eeprom)) {
810 aprint_error_dev(un->un_dev, "failed to read eeprom\n");
811 return;
812 }
813
814 sc->sc_pwrcfg = le16toh(eeprom) & 0xFF00;
815
816 /*
817 * Auto-loaded default station address from internal ROM is
818 * 00:00:00:00:00:00 such that an explicit access to EEPROM
819 * is required to get real station address.
820 */
821 uint8_t *eaddr = un->un_eaddr;
822 for (i = 0; i < ETHER_ADDR_LEN / 2; i++) {
823 if (axe_cmd(sc, AXE_CMD_SROM_READ, 0,
824 AXE_EEPROM_772B_NODE_ID + i, &eeprom)) {
825 aprint_error_dev(un->un_dev,
826 "failed to read eeprom\n");
827 eeprom = 0;
828 }
829 eeprom = le16toh(eeprom);
830 *eaddr++ = (uint8_t)(eeprom & 0xFF);
831 *eaddr++ = (uint8_t)((eeprom >> 8) & 0xFF);
832 }
833 /* Wakeup PHY. */
834 axe_ax88772_phywake(sc);
835 /* Stop MAC. */
836 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
837 }
838
839 #undef AXE_GPIO_WRITE
840
841 /*
842 * Probe for a AX88172 chip.
843 */
844 static int
845 axe_match(device_t parent, cfdata_t match, void *aux)
846 {
847 struct usb_attach_arg *uaa = aux;
848
849 return axe_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
850 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
851 }
852
853 /*
854 * Attach the interface. Allocate softc structures, do ifmedia
855 * setup and ethernet/BPF attach.
856 */
857 static void
858 axe_attach(device_t parent, device_t self, void *aux)
859 {
860 AXEHIST_FUNC(); AXEHIST_CALLED();
861 USBNET_MII_DECL_DEFAULT(unm);
862 struct axe_softc *sc = device_private(self);
863 struct usbnet * const un = &sc->axe_un;
864 struct usb_attach_arg *uaa = aux;
865 struct usbd_device *dev = uaa->uaa_device;
866 usbd_status err;
867 usb_interface_descriptor_t *id;
868 usb_endpoint_descriptor_t *ed;
869 char *devinfop;
870 unsigned bufsz;
871 int i;
872
873 KASSERT((void *)sc == un);
874
875 aprint_naive("\n");
876 aprint_normal("\n");
877 devinfop = usbd_devinfo_alloc(dev, 0);
878 aprint_normal_dev(self, "%s\n", devinfop);
879 usbd_devinfo_free(devinfop);
880
881 un->un_dev = self;
882 un->un_udev = dev;
883 un->un_sc = sc;
884 un->un_ops = &axe_ops;
885 un->un_rx_xfer_flags = USBD_SHORT_XFER_OK;
886 un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER;
887 un->un_rx_list_cnt = AXE_RX_LIST_CNT;
888 un->un_tx_list_cnt = AXE_TX_LIST_CNT;
889
890 err = usbd_set_config_no(dev, AXE_CONFIG_NO, 1);
891 if (err) {
892 aprint_error_dev(self, "failed to set configuration"
893 ", err=%s\n", usbd_errstr(err));
894 return;
895 }
896
897 un->un_flags = axe_lookup(uaa->uaa_vendor, uaa->uaa_product)->axe_flags;
898
899 err = usbd_device2interface_handle(dev, AXE_IFACE_IDX, &un->un_iface);
900 if (err) {
901 aprint_error_dev(self, "getting interface handle failed\n");
902 return;
903 }
904
905 id = usbd_get_interface_descriptor(un->un_iface);
906
907 /* decide on what our bufsize will be */
908 if (AXE_IS_178_FAMILY(un))
909 bufsz = (un->un_udev->ud_speed == USB_SPEED_HIGH) ?
910 AXE_178_MAX_BUFSZ : AXE_178_MIN_BUFSZ;
911 else
912 bufsz = AXE_172_BUFSZ;
913 un->un_rx_bufsz = un->un_tx_bufsz = bufsz;
914
915 un->un_ed[USBNET_ENDPT_RX] = 0;
916 un->un_ed[USBNET_ENDPT_TX] = 0;
917 un->un_ed[USBNET_ENDPT_INTR] = 0;
918
919 /* Find endpoints. */
920 for (i = 0; i < id->bNumEndpoints; i++) {
921 ed = usbd_interface2endpoint_descriptor(un->un_iface, i);
922 if (ed == NULL) {
923 aprint_error_dev(self, "couldn't get ep %d\n", i);
924 return;
925 }
926 const uint8_t xt = UE_GET_XFERTYPE(ed->bmAttributes);
927 const uint8_t dir = UE_GET_DIR(ed->bEndpointAddress);
928
929 if (dir == UE_DIR_IN && xt == UE_BULK &&
930 un->un_ed[USBNET_ENDPT_RX] == 0) {
931 un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress;
932 } else if (dir == UE_DIR_OUT && xt == UE_BULK &&
933 un->un_ed[USBNET_ENDPT_TX] == 0) {
934 un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress;
935 } else if (dir == UE_DIR_IN && xt == UE_INTERRUPT) {
936 un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress;
937 }
938 }
939
940 /* Set these up now for axe_cmd(). */
941 usbnet_attach(un, "axedet");
942
943 /* We need the PHYID for init dance in some cases */
944 usbnet_lock_mii(un);
945 if (axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, &sc->axe_phyaddrs)) {
946 aprint_error_dev(self, "failed to read phyaddrs\n");
947
948 return;
949 }
950
951 DPRINTF(" phyaddrs[0]: %jx phyaddrs[1]: %jx",
952 sc->axe_phyaddrs[0], sc->axe_phyaddrs[1], 0, 0);
953 un->un_phyno = axe_get_phyno(sc, AXE_PHY_SEL_PRI);
954 if (un->un_phyno == -1)
955 un->un_phyno = axe_get_phyno(sc, AXE_PHY_SEL_SEC);
956 if (un->un_phyno == -1) {
957 DPRINTF(" no valid PHY address found, assuming PHY address 0",
958 0, 0, 0, 0);
959 un->un_phyno = 0;
960 }
961
962 /* Initialize controller and get station address. */
963
964 axe_ax_init(un);
965
966 /*
967 * Fetch IPG values.
968 */
969 if (un->un_flags & (AX772A | AX772B)) {
970 /* Set IPG values. */
971 sc->axe_ipgs[0] = AXE_IPG0_DEFAULT;
972 sc->axe_ipgs[1] = AXE_IPG1_DEFAULT;
973 sc->axe_ipgs[2] = AXE_IPG2_DEFAULT;
974 } else {
975 if (axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, sc->axe_ipgs)) {
976 aprint_error_dev(self, "failed to read ipg\n");
977 usbnet_unlock_mii(un);
978 return;
979 }
980 }
981
982 usbnet_unlock_mii(un);
983
984 if (AXE_IS_178_FAMILY(un))
985 usbnet_ec(un)->ec_capabilities = ETHERCAP_VLAN_MTU;
986 if (un->un_flags & AX772B) {
987 struct ifnet *ifp = usbnet_ifp(un);
988
989 ifp->if_capabilities =
990 IFCAP_CSUM_IPv4_Rx |
991 IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx |
992 IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx;
993 /*
994 * Checksum offloading of AX88772B also works with VLAN
995 * tagged frames but there is no way to take advantage
996 * of the feature because vlan(4) assumes
997 * IFCAP_VLAN_HWTAGGING is prerequisite condition to
998 * support checksum offloading with VLAN. VLAN hardware
999 * tagging support of AX88772B is very limited so it's
1000 * not possible to announce IFCAP_VLAN_HWTAGGING.
1001 */
1002 }
1003 if (un->un_flags & (AX772A | AX772B | AX178))
1004 unm.un_mii_flags = MIIF_DOPAUSE;
1005
1006 usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST,
1007 0, &unm);
1008 }
1009
1010 static void
1011 axe_rx_loop(struct usbnet * un, struct usbnet_chain *c, uint32_t total_len)
1012 {
1013 AXEHIST_FUNC(); AXEHIST_CALLED();
1014 struct axe_softc * const sc = usbnet_softc(un);
1015 struct ifnet *ifp = usbnet_ifp(un);
1016 uint8_t *buf = c->unc_buf;
1017
1018 do {
1019 u_int pktlen = 0;
1020 u_int rxlen = 0;
1021 int flags = 0;
1022
1023 if ((un->un_flags & AXSTD_FRAME) != 0) {
1024 struct axe_sframe_hdr hdr;
1025
1026 if (total_len < sizeof(hdr)) {
1027 if_statinc(ifp, if_ierrors);
1028 break;
1029 }
1030
1031 memcpy(&hdr, buf, sizeof(hdr));
1032
1033 DPRINTFN(20, "total_len %#jx len %#jx ilen %#jx",
1034 total_len,
1035 (le16toh(hdr.len) & AXE_RH1M_RXLEN_MASK),
1036 (le16toh(hdr.ilen) & AXE_RH1M_RXLEN_MASK), 0);
1037
1038 total_len -= sizeof(hdr);
1039 buf += sizeof(hdr);
1040
1041 if (((le16toh(hdr.len) & AXE_RH1M_RXLEN_MASK) ^
1042 (le16toh(hdr.ilen) & AXE_RH1M_RXLEN_MASK)) !=
1043 AXE_RH1M_RXLEN_MASK) {
1044 if_statinc(ifp, if_ierrors);
1045 break;
1046 }
1047
1048 rxlen = le16toh(hdr.len) & AXE_RH1M_RXLEN_MASK;
1049 if (total_len < rxlen) {
1050 pktlen = total_len;
1051 total_len = 0;
1052 } else {
1053 pktlen = rxlen;
1054 rxlen = roundup2(rxlen, 2);
1055 total_len -= rxlen;
1056 }
1057
1058 } else if ((un->un_flags & AXCSUM_FRAME) != 0) {
1059 struct axe_csum_hdr csum_hdr;
1060
1061 if (total_len < sizeof(csum_hdr)) {
1062 if_statinc(ifp, if_ierrors);
1063 break;
1064 }
1065
1066 memcpy(&csum_hdr, buf, sizeof(csum_hdr));
1067
1068 csum_hdr.len = le16toh(csum_hdr.len);
1069 csum_hdr.ilen = le16toh(csum_hdr.ilen);
1070 csum_hdr.cstatus = le16toh(csum_hdr.cstatus);
1071
1072 DPRINTFN(20, "total_len %#jx len %#jx ilen %#jx"
1073 " cstatus %#jx", total_len,
1074 csum_hdr.len, csum_hdr.ilen, csum_hdr.cstatus);
1075
1076 if ((AXE_CSUM_RXBYTES(csum_hdr.len) ^
1077 AXE_CSUM_RXBYTES(csum_hdr.ilen)) !=
1078 sc->sc_lenmask) {
1079 /* we lost sync */
1080 if_statinc(ifp, if_ierrors);
1081 DPRINTFN(20, "len %#jx ilen %#jx lenmask %#jx "
1082 "err",
1083 AXE_CSUM_RXBYTES(csum_hdr.len),
1084 AXE_CSUM_RXBYTES(csum_hdr.ilen),
1085 sc->sc_lenmask, 0);
1086 break;
1087 }
1088 /*
1089 * Get total transferred frame length including
1090 * checksum header. The length should be multiple
1091 * of 4.
1092 */
1093 pktlen = AXE_CSUM_RXBYTES(csum_hdr.len);
1094 u_int len = sizeof(csum_hdr) + pktlen;
1095 len = (len + 3) & ~3;
1096 if (total_len < len) {
1097 DPRINTFN(20, "total_len %#jx < len %#jx",
1098 total_len, len, 0, 0);
1099 /* invalid length */
1100 if_statinc(ifp, if_ierrors);
1101 break;
1102 }
1103 buf += sizeof(csum_hdr);
1104
1105 const uint16_t cstatus = csum_hdr.cstatus;
1106
1107 if (cstatus & AXE_CSUM_HDR_L3_TYPE_IPV4) {
1108 if (cstatus & AXE_CSUM_HDR_L4_CSUM_ERR)
1109 flags |= M_CSUM_TCP_UDP_BAD;
1110 if (cstatus & AXE_CSUM_HDR_L3_CSUM_ERR)
1111 flags |= M_CSUM_IPv4_BAD;
1112
1113 const uint16_t l4type =
1114 cstatus & AXE_CSUM_HDR_L4_TYPE_MASK;
1115
1116 if (l4type == AXE_CSUM_HDR_L4_TYPE_TCP)
1117 flags |= M_CSUM_TCPv4;
1118 if (l4type == AXE_CSUM_HDR_L4_TYPE_UDP)
1119 flags |= M_CSUM_UDPv4;
1120 }
1121 if (total_len < len) {
1122 pktlen = total_len;
1123 total_len = 0;
1124 } else {
1125 total_len -= len;
1126 rxlen = len - sizeof(csum_hdr);
1127 }
1128 DPRINTFN(20, "total_len %#jx len %#jx pktlen %#jx"
1129 " rxlen %#jx", total_len, len, pktlen, rxlen);
1130 } else { /* AX172 */
1131 pktlen = rxlen = total_len;
1132 total_len = 0;
1133 }
1134
1135 usbnet_enqueue(un, buf, pktlen, flags, 0, 0);
1136 buf += rxlen;
1137
1138 } while (total_len > 0);
1139
1140 DPRINTFN(10, "start rx", 0, 0, 0, 0);
1141 }
1142
1143 static unsigned
1144 axe_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
1145 {
1146 AXEHIST_FUNC(); AXEHIST_CALLED();
1147 struct axe_sframe_hdr hdr, tlr;
1148 size_t hdr_len = 0, tlr_len = 0;
1149 int length, boundary;
1150
1151 usbnet_isowned_tx(un);
1152
1153 if (AXE_IS_178_FAMILY(un)) {
1154 /*
1155 * Copy the mbuf data into a contiguous buffer, leaving two
1156 * bytes at the beginning to hold the frame length.
1157 */
1158 boundary = (un->un_udev->ud_speed == USB_SPEED_HIGH) ? 512 : 64;
1159
1160 hdr.len = htole16(m->m_pkthdr.len);
1161 hdr.ilen = ~hdr.len;
1162 hdr_len = sizeof(hdr);
1163
1164 length = hdr_len + m->m_pkthdr.len;
1165
1166 if ((length % boundary) == 0) {
1167 tlr.len = 0x0000;
1168 tlr.ilen = 0xffff;
1169 tlr_len = sizeof(tlr);
1170 }
1171 DPRINTFN(20, "length %jx m_pkthdr.len %jx hdrsize %#jx",
1172 length, m->m_pkthdr.len, sizeof(hdr), 0);
1173 }
1174
1175 if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz - hdr_len - tlr_len)
1176 return 0;
1177 length = hdr_len + m->m_pkthdr.len + tlr_len;
1178
1179 if (hdr_len)
1180 memcpy(c->unc_buf, &hdr, hdr_len);
1181 m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf + hdr_len);
1182 if (tlr_len)
1183 memcpy(c->unc_buf + length - tlr_len, &tlr, tlr_len);
1184
1185 return length;
1186 }
1187
1188 static void
1189 axe_csum_cfg(struct axe_softc *sc)
1190 {
1191 struct usbnet * const un = &sc->axe_un;
1192 struct ifnet * const ifp = usbnet_ifp(un);
1193 uint16_t csum1, csum2;
1194
1195 if ((un->un_flags & AX772B) != 0) {
1196 csum1 = 0;
1197 csum2 = 0;
1198 if ((ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) != 0)
1199 csum1 |= AXE_TXCSUM_IP;
1200 if ((ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) != 0)
1201 csum1 |= AXE_TXCSUM_TCP;
1202 if ((ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) != 0)
1203 csum1 |= AXE_TXCSUM_UDP;
1204 if ((ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) != 0)
1205 csum1 |= AXE_TXCSUM_TCPV6;
1206 if ((ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) != 0)
1207 csum1 |= AXE_TXCSUM_UDPV6;
1208 axe_cmd(sc, AXE_772B_CMD_WRITE_TXCSUM, csum2, csum1, NULL);
1209 csum1 = 0;
1210 csum2 = 0;
1211
1212 if ((ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) != 0)
1213 csum1 |= AXE_RXCSUM_IP;
1214 if ((ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) != 0)
1215 csum1 |= AXE_RXCSUM_TCP;
1216 if ((ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) != 0)
1217 csum1 |= AXE_RXCSUM_UDP;
1218 if ((ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) != 0)
1219 csum1 |= AXE_RXCSUM_TCPV6;
1220 if ((ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) != 0)
1221 csum1 |= AXE_RXCSUM_UDPV6;
1222 axe_cmd(sc, AXE_772B_CMD_WRITE_RXCSUM, csum2, csum1, NULL);
1223 }
1224 }
1225
1226 static int
1227 axe_init_locked(struct ifnet *ifp)
1228 {
1229 AXEHIST_FUNC(); AXEHIST_CALLED();
1230 struct usbnet * const un = ifp->if_softc;
1231 struct axe_softc * const sc = usbnet_softc(un);
1232 int rxmode;
1233
1234 usbnet_isowned(un);
1235
1236 if (usbnet_isdying(un))
1237 return EIO;
1238
1239 /* Cancel pending I/O */
1240 usbnet_stop(un, ifp, 1);
1241
1242 usbnet_lock_mii_un_locked(un);
1243
1244 /* Reset the ethernet interface. */
1245 axe_reset(un);
1246
1247 #if 0
1248 ret = asix_write_gpio(dev, AX_GPIO_RSE | AX_GPIO_GPO_2 |
1249 AX_GPIO_GPO2EN, 5, in_pm);
1250 #endif
1251 /* Set MAC address and transmitter IPG values. */
1252 if (AXE_IS_178_FAMILY(un)) {
1253 axe_cmd(sc, AXE_178_CMD_WRITE_NODEID, 0, 0, un->un_eaddr);
1254 axe_cmd(sc, AXE_178_CMD_WRITE_IPG012, sc->axe_ipgs[2],
1255 (sc->axe_ipgs[1] << 8) | (sc->axe_ipgs[0]), NULL);
1256 } else {
1257 axe_cmd(sc, AXE_172_CMD_WRITE_NODEID, 0, 0, un->un_eaddr);
1258 axe_cmd(sc, AXE_172_CMD_WRITE_IPG0, 0, sc->axe_ipgs[0], NULL);
1259 axe_cmd(sc, AXE_172_CMD_WRITE_IPG1, 0, sc->axe_ipgs[1], NULL);
1260 axe_cmd(sc, AXE_172_CMD_WRITE_IPG2, 0, sc->axe_ipgs[2], NULL);
1261 }
1262 if (AXE_IS_178_FAMILY(un)) {
1263 un->un_flags &= ~(AXSTD_FRAME | AXCSUM_FRAME);
1264 if ((un->un_flags & AX772B) != 0 &&
1265 (ifp->if_capenable & AX_RXCSUM) != 0) {
1266 sc->sc_lenmask = AXE_CSUM_HDR_LEN_MASK;
1267 un->un_flags |= AXCSUM_FRAME;
1268 } else {
1269 sc->sc_lenmask = AXE_HDR_LEN_MASK;
1270 un->un_flags |= AXSTD_FRAME;
1271 }
1272 }
1273
1274 /* Configure TX/RX checksum offloading. */
1275 axe_csum_cfg(sc);
1276
1277 if (un->un_flags & AX772B) {
1278 /* AX88772B uses different maximum frame burst configuration. */
1279 axe_cmd(sc, AXE_772B_CMD_RXCTL_WRITE_CFG,
1280 ax88772b_mfb_table[AX88772B_MFB_16K].threshold,
1281 ax88772b_mfb_table[AX88772B_MFB_16K].byte_cnt, NULL);
1282 }
1283 /* Enable receiver, set RX mode */
1284 rxmode = (AXE_RXCMD_BROADCAST | AXE_RXCMD_MULTICAST | AXE_RXCMD_ENABLE);
1285 if (AXE_IS_178_FAMILY(un)) {
1286 if (un->un_flags & AX772B) {
1287 /*
1288 * Select RX header format type 1. Aligning IP
1289 * header on 4 byte boundary is not needed when
1290 * checksum offloading feature is not used
1291 * because we always copy the received frame in
1292 * RX handler. When RX checksum offloading is
1293 * active, aligning IP header is required to
1294 * reflect actual frame length including RX
1295 * header size.
1296 */
1297 rxmode |= AXE_772B_RXCMD_HDR_TYPE_1;
1298 if (un->un_flags & AXCSUM_FRAME)
1299 rxmode |= AXE_772B_RXCMD_IPHDR_ALIGN;
1300 } else {
1301 /*
1302 * Default Rx buffer size is too small to get
1303 * maximum performance.
1304 */
1305 #if 0
1306 if (un->un_udev->ud_speed == USB_SPEED_HIGH) {
1307 /* Largest possible USB buffer size for AX88178 */
1308 }
1309 #endif
1310 rxmode |= AXE_178_RXCMD_MFB_16384;
1311 }
1312 } else {
1313 rxmode |= AXE_172_RXCMD_UNICAST;
1314 }
1315
1316 DPRINTF("rxmode %#jx", rxmode, 0, 0, 0);
1317
1318 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
1319
1320 /* Accept multicast frame or run promisc. */
1321 axe_setiff_locked(un);
1322
1323 usbnet_unlock_mii_un_locked(un);
1324
1325 return usbnet_init_rx_tx(un);
1326 }
1327
1328 static int
1329 axe_init(struct ifnet *ifp)
1330 {
1331 struct usbnet * const un = ifp->if_softc;
1332
1333 usbnet_lock(un);
1334 int ret = axe_init_locked(ifp);
1335 usbnet_unlock(un);
1336
1337 return ret;
1338 }
1339
1340 static int
1341 axe_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1342 {
1343 struct usbnet * const un = ifp->if_softc;
1344
1345 switch (cmd) {
1346 case SIOCADDMULTI:
1347 case SIOCDELMULTI:
1348 axe_setiff(un);
1349 break;
1350 default:
1351 break;
1352 }
1353
1354 return 0;
1355 }
1356
1357 static void
1358 axe_stop(struct ifnet *ifp, int disable)
1359 {
1360 struct usbnet * const un = ifp->if_softc;
1361
1362 usbnet_lock_mii_un_locked(un);
1363 axe_reset(un);
1364 usbnet_unlock_mii_un_locked(un);
1365 }
1366
1367 #ifdef _MODULE
1368 #include "ioconf.c"
1369 #endif
1370
1371 USBNET_MODULE(axe)
1372