1/*-
2 * SPDX-License-Identifier: BSD-4-Clause
3 *
4 * Copyright (c) 1997, 1998, 1999, 2000
5 *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#include <sys/cdefs.h>
36/*
37 * CATC USB-EL1210A USB to ethernet driver. Used in the CATC Netmate
38 * adapters and others.
39 *
40 * Written by Bill Paul <wpaul@ee.columbia.edu>
41 * Electrical Engineering Department
42 * Columbia University, New York City
43 */
44
45/*
46 * The CATC USB-EL1210A provides USB ethernet support at 10Mbps. The
47 * RX filter uses a 512-bit multicast hash table, single perfect entry
48 * for the station address, and promiscuous mode. Unlike the ADMtek
49 * and KLSI chips, the CATC ASIC supports read and write combining
50 * mode where multiple packets can be transferred using a single bulk
51 * transaction, which helps performance a great deal.
52 */
53
54#include <sys/stdint.h>
55#include <sys/stddef.h>
56#include <sys/param.h>
57#include <sys/queue.h>
58#include <sys/types.h>
59#include <sys/systm.h>
60#include <sys/socket.h>
61#include <sys/kernel.h>
62#include <sys/bus.h>
63#include <sys/module.h>
64#include <sys/lock.h>
65#include <sys/mutex.h>
66#include <sys/condvar.h>
67#include <sys/sysctl.h>
68#include <sys/sx.h>
69#include <sys/unistd.h>
70#include <sys/callout.h>
71#include <sys/malloc.h>
72#include <sys/priv.h>
73
74#include <net/if.h>
75#include <net/if_var.h>
76
77#include <dev/usb/usb.h>
78#include <dev/usb/usbdi.h>
79#include <dev/usb/usbdi_util.h>
80#include "usbdevs.h"
81
82#define	USB_DEBUG_VAR cue_debug
83#include <dev/usb/usb_debug.h>
84#include <dev/usb/usb_process.h>
85
86#include <dev/usb/net/usb_ethernet.h>
87#include <dev/usb/net/if_cuereg.h>
88
89/*
90 * Various supported device vendors/products.
91 */
92
93/* Belkin F5U111 adapter covered by NETMATE entry */
94
95static const STRUCT_USB_HOST_ID cue_devs[] = {
96#define	CUE_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
97	CUE_DEV(CATC, NETMATE),
98	CUE_DEV(CATC, NETMATE2),
99	CUE_DEV(SMARTBRIDGES, SMARTLINK),
100#undef CUE_DEV
101};
102
103/* prototypes */
104
105static device_probe_t cue_probe;
106static device_attach_t cue_attach;
107static device_detach_t cue_detach;
108
109static usb_callback_t cue_bulk_read_callback;
110static usb_callback_t cue_bulk_write_callback;
111
112static uether_fn_t cue_attach_post;
113static uether_fn_t cue_init;
114static uether_fn_t cue_stop;
115static uether_fn_t cue_start;
116static uether_fn_t cue_tick;
117static uether_fn_t cue_setmulti;
118static uether_fn_t cue_setpromisc;
119
120static uint8_t	cue_csr_read_1(struct cue_softc *, uint16_t);
121static uint16_t	cue_csr_read_2(struct cue_softc *, uint8_t);
122static int	cue_csr_write_1(struct cue_softc *, uint16_t, uint16_t);
123static int	cue_mem(struct cue_softc *, uint8_t, uint16_t, void *, int);
124static int	cue_getmac(struct cue_softc *, void *);
125static uint32_t	cue_mchash(const uint8_t *);
126static void	cue_reset(struct cue_softc *);
127
128#ifdef USB_DEBUG
129static int cue_debug = 0;
130
131static SYSCTL_NODE(_hw_usb, OID_AUTO, cue, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
132    "USB cue");
133SYSCTL_INT(_hw_usb_cue, OID_AUTO, debug, CTLFLAG_RWTUN, &cue_debug, 0,
134    "Debug level");
135#endif
136
137static const struct usb_config cue_config[CUE_N_TRANSFER] = {
138	[CUE_BULK_DT_WR] = {
139		.type = UE_BULK,
140		.endpoint = UE_ADDR_ANY,
141		.direction = UE_DIR_OUT,
142		.bufsize = (MCLBYTES + 2),
143		.flags = {.pipe_bof = 1,},
144		.callback = cue_bulk_write_callback,
145		.timeout = 10000,	/* 10 seconds */
146	},
147
148	[CUE_BULK_DT_RD] = {
149		.type = UE_BULK,
150		.endpoint = UE_ADDR_ANY,
151		.direction = UE_DIR_IN,
152		.bufsize = (MCLBYTES + 2),
153		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
154		.callback = cue_bulk_read_callback,
155	},
156};
157
158static device_method_t cue_methods[] = {
159	/* Device interface */
160	DEVMETHOD(device_probe, cue_probe),
161	DEVMETHOD(device_attach, cue_attach),
162	DEVMETHOD(device_detach, cue_detach),
163
164	DEVMETHOD_END
165};
166
167static driver_t cue_driver = {
168	.name = "cue",
169	.methods = cue_methods,
170	.size = sizeof(struct cue_softc),
171};
172
173DRIVER_MODULE(cue, uhub, cue_driver, NULL, NULL);
174MODULE_DEPEND(cue, uether, 1, 1, 1);
175MODULE_DEPEND(cue, usb, 1, 1, 1);
176MODULE_DEPEND(cue, ether, 1, 1, 1);
177MODULE_VERSION(cue, 1);
178USB_PNP_HOST_INFO(cue_devs);
179
180static const struct usb_ether_methods cue_ue_methods = {
181	.ue_attach_post = cue_attach_post,
182	.ue_start = cue_start,
183	.ue_init = cue_init,
184	.ue_stop = cue_stop,
185	.ue_tick = cue_tick,
186	.ue_setmulti = cue_setmulti,
187	.ue_setpromisc = cue_setpromisc,
188};
189
190#define	CUE_SETBIT(sc, reg, x)				\
191	cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) | (x))
192
193#define	CUE_CLRBIT(sc, reg, x)				\
194	cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) & ~(x))
195
196static uint8_t
197cue_csr_read_1(struct cue_softc *sc, uint16_t reg)
198{
199	struct usb_device_request req;
200	uint8_t val;
201
202	req.bmRequestType = UT_READ_VENDOR_DEVICE;
203	req.bRequest = CUE_CMD_READREG;
204	USETW(req.wValue, 0);
205	USETW(req.wIndex, reg);
206	USETW(req.wLength, 1);
207
208	if (uether_do_request(&sc->sc_ue, &req, &val, 1000)) {
209		/* ignore any errors */
210	}
211	return (val);
212}
213
214static uint16_t
215cue_csr_read_2(struct cue_softc *sc, uint8_t reg)
216{
217	struct usb_device_request req;
218	uint16_t val;
219
220	req.bmRequestType = UT_READ_VENDOR_DEVICE;
221	req.bRequest = CUE_CMD_READREG;
222	USETW(req.wValue, 0);
223	USETW(req.wIndex, reg);
224	USETW(req.wLength, 2);
225
226	(void)uether_do_request(&sc->sc_ue, &req, &val, 1000);
227	return (le16toh(val));
228}
229
230static int
231cue_csr_write_1(struct cue_softc *sc, uint16_t reg, uint16_t val)
232{
233	struct usb_device_request req;
234
235	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
236	req.bRequest = CUE_CMD_WRITEREG;
237	USETW(req.wValue, val);
238	USETW(req.wIndex, reg);
239	USETW(req.wLength, 0);
240
241	return (uether_do_request(&sc->sc_ue, &req, NULL, 1000));
242}
243
244static int
245cue_mem(struct cue_softc *sc, uint8_t cmd, uint16_t addr, void *buf, int len)
246{
247	struct usb_device_request req;
248
249	if (cmd == CUE_CMD_READSRAM)
250		req.bmRequestType = UT_READ_VENDOR_DEVICE;
251	else
252		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
253	req.bRequest = cmd;
254	USETW(req.wValue, 0);
255	USETW(req.wIndex, addr);
256	USETW(req.wLength, len);
257
258	return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
259}
260
261static int
262cue_getmac(struct cue_softc *sc, void *buf)
263{
264	struct usb_device_request req;
265
266	req.bmRequestType = UT_READ_VENDOR_DEVICE;
267	req.bRequest = CUE_CMD_GET_MACADDR;
268	USETW(req.wValue, 0);
269	USETW(req.wIndex, 0);
270	USETW(req.wLength, ETHER_ADDR_LEN);
271
272	return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
273}
274
275#define	CUE_BITS 9
276
277static uint32_t
278cue_mchash(const uint8_t *addr)
279{
280	uint32_t crc;
281
282	/* Compute CRC for the address value. */
283	crc = ether_crc32_le(addr, ETHER_ADDR_LEN);
284
285	return (crc & ((1 << CUE_BITS) - 1));
286}
287
288static void
289cue_setpromisc(struct usb_ether *ue)
290{
291	struct cue_softc *sc = uether_getsc(ue);
292	if_t ifp = uether_getifp(ue);
293
294	CUE_LOCK_ASSERT(sc, MA_OWNED);
295
296	/* if we want promiscuous mode, set the allframes bit */
297	if (if_getflags(ifp) & IFF_PROMISC)
298		CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
299	else
300		CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
301
302	/* write multicast hash-bits */
303	cue_setmulti(ue);
304}
305
306static u_int
307cue_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
308{
309	uint8_t *hashtbl = arg;
310	uint32_t h;
311
312	h = cue_mchash(LLADDR(sdl));
313	hashtbl[h >> 3] |= 1 << (h & 0x7);
314
315	return (1);
316}
317
318static void
319cue_setmulti(struct usb_ether *ue)
320{
321	struct cue_softc *sc = uether_getsc(ue);
322	if_t ifp = uether_getifp(ue);
323	uint32_t h, i;
324	uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
325
326	CUE_LOCK_ASSERT(sc, MA_OWNED);
327
328	if (if_getflags(ifp) & IFF_ALLMULTI || if_getflags(ifp) & IFF_PROMISC) {
329		for (i = 0; i < 8; i++)
330			hashtbl[i] = 0xff;
331		cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
332		    &hashtbl, 8);
333		return;
334	}
335
336	/* now program new ones */
337	if_foreach_llmaddr(ifp, cue_hash_maddr, hashtbl);
338
339	/*
340	 * Also include the broadcast address in the filter
341	 * so we can receive broadcast frames.
342 	 */
343	if (if_getflags(ifp) & IFF_BROADCAST) {
344		h = cue_mchash(if_getbroadcastaddr(ifp));
345		hashtbl[h >> 3] |= 1 << (h & 0x7);
346	}
347
348	cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR, &hashtbl, 8);
349}
350
351static void
352cue_reset(struct cue_softc *sc)
353{
354	struct usb_device_request req;
355
356	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
357	req.bRequest = CUE_CMD_RESET;
358	USETW(req.wValue, 0);
359	USETW(req.wIndex, 0);
360	USETW(req.wLength, 0);
361
362	if (uether_do_request(&sc->sc_ue, &req, NULL, 1000)) {
363		/* ignore any errors */
364	}
365
366	/*
367	 * wait a little while for the chip to get its brains in order:
368	 */
369	uether_pause(&sc->sc_ue, hz / 100);
370}
371
372static void
373cue_attach_post(struct usb_ether *ue)
374{
375	struct cue_softc *sc = uether_getsc(ue);
376
377	cue_getmac(sc, ue->ue_eaddr);
378}
379
380static int
381cue_probe(device_t dev)
382{
383	struct usb_attach_arg *uaa = device_get_ivars(dev);
384
385	if (uaa->usb_mode != USB_MODE_HOST)
386		return (ENXIO);
387	if (uaa->info.bConfigIndex != CUE_CONFIG_IDX)
388		return (ENXIO);
389	if (uaa->info.bIfaceIndex != CUE_IFACE_IDX)
390		return (ENXIO);
391
392	return (usbd_lookup_id_by_uaa(cue_devs, sizeof(cue_devs), uaa));
393}
394
395/*
396 * Attach the interface. Allocate softc structures, do ifmedia
397 * setup and ethernet/BPF attach.
398 */
399static int
400cue_attach(device_t dev)
401{
402	struct usb_attach_arg *uaa = device_get_ivars(dev);
403	struct cue_softc *sc = device_get_softc(dev);
404	struct usb_ether *ue = &sc->sc_ue;
405	uint8_t iface_index;
406	int error;
407
408	device_set_usb_desc(dev);
409	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
410
411	iface_index = CUE_IFACE_IDX;
412	error = usbd_transfer_setup(uaa->device, &iface_index,
413	    sc->sc_xfer, cue_config, CUE_N_TRANSFER, sc, &sc->sc_mtx);
414	if (error) {
415		device_printf(dev, "allocating USB transfers failed\n");
416		goto detach;
417	}
418
419	ue->ue_sc = sc;
420	ue->ue_dev = dev;
421	ue->ue_udev = uaa->device;
422	ue->ue_mtx = &sc->sc_mtx;
423	ue->ue_methods = &cue_ue_methods;
424
425	error = uether_ifattach(ue);
426	if (error) {
427		device_printf(dev, "could not attach interface\n");
428		goto detach;
429	}
430	return (0);			/* success */
431
432detach:
433	cue_detach(dev);
434	return (ENXIO);			/* failure */
435}
436
437static int
438cue_detach(device_t dev)
439{
440	struct cue_softc *sc = device_get_softc(dev);
441	struct usb_ether *ue = &sc->sc_ue;
442
443	usbd_transfer_unsetup(sc->sc_xfer, CUE_N_TRANSFER);
444	uether_ifdetach(ue);
445	mtx_destroy(&sc->sc_mtx);
446
447	return (0);
448}
449
450static void
451cue_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
452{
453	struct cue_softc *sc = usbd_xfer_softc(xfer);
454	struct usb_ether *ue = &sc->sc_ue;
455	if_t ifp = uether_getifp(ue);
456	struct usb_page_cache *pc;
457	uint8_t buf[2];
458	int len;
459	int actlen;
460
461	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
462
463	switch (USB_GET_STATE(xfer)) {
464	case USB_ST_TRANSFERRED:
465
466		if (actlen <= (int)(2 + sizeof(struct ether_header))) {
467			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
468			goto tr_setup;
469		}
470		pc = usbd_xfer_get_frame(xfer, 0);
471		usbd_copy_out(pc, 0, buf, 2);
472		actlen -= 2;
473		len = buf[0] | (buf[1] << 8);
474		len = min(actlen, len);
475
476		uether_rxbuf(ue, pc, 2, len);
477		/* FALLTHROUGH */
478	case USB_ST_SETUP:
479tr_setup:
480		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
481		usbd_transfer_submit(xfer);
482		uether_rxflush(ue);
483		return;
484
485	default:			/* Error */
486		DPRINTF("bulk read error, %s\n",
487		    usbd_errstr(error));
488
489		if (error != USB_ERR_CANCELLED) {
490			/* try to clear stall first */
491			usbd_xfer_set_stall(xfer);
492			goto tr_setup;
493		}
494		return;
495	}
496}
497
498static void
499cue_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
500{
501	struct cue_softc *sc = usbd_xfer_softc(xfer);
502	if_t ifp = uether_getifp(&sc->sc_ue);
503	struct usb_page_cache *pc;
504	struct mbuf *m;
505	uint8_t buf[2];
506
507	switch (USB_GET_STATE(xfer)) {
508	case USB_ST_TRANSFERRED:
509		DPRINTFN(11, "transfer complete\n");
510		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
511
512		/* FALLTHROUGH */
513	case USB_ST_SETUP:
514tr_setup:
515		m = if_dequeue(ifp);
516
517		if (m == NULL)
518			return;
519		if (m->m_pkthdr.len > MCLBYTES)
520			m->m_pkthdr.len = MCLBYTES;
521		usbd_xfer_set_frame_len(xfer, 0, (m->m_pkthdr.len + 2));
522
523		/* the first two bytes are the frame length */
524
525		buf[0] = (uint8_t)(m->m_pkthdr.len);
526		buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
527
528		pc = usbd_xfer_get_frame(xfer, 0);
529		usbd_copy_in(pc, 0, buf, 2);
530		usbd_m_copy_in(pc, 2, m, 0, m->m_pkthdr.len);
531
532		/*
533		 * If there's a BPF listener, bounce a copy of this frame
534		 * to him.
535		 */
536		BPF_MTAP(ifp, m);
537
538		m_freem(m);
539
540		usbd_transfer_submit(xfer);
541
542		return;
543
544	default:			/* Error */
545		DPRINTFN(11, "transfer error, %s\n",
546		    usbd_errstr(error));
547
548		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
549
550		if (error != USB_ERR_CANCELLED) {
551			/* try to clear stall first */
552			usbd_xfer_set_stall(xfer);
553			goto tr_setup;
554		}
555		return;
556	}
557}
558
559static void
560cue_tick(struct usb_ether *ue)
561{
562	struct cue_softc *sc = uether_getsc(ue);
563	if_t ifp = uether_getifp(ue);
564
565	CUE_LOCK_ASSERT(sc, MA_OWNED);
566
567	if_inc_counter(ifp, IFCOUNTER_COLLISIONS, cue_csr_read_2(sc, CUE_TX_SINGLECOLL));
568	if_inc_counter(ifp, IFCOUNTER_COLLISIONS, cue_csr_read_2(sc, CUE_TX_MULTICOLL));
569	if_inc_counter(ifp, IFCOUNTER_COLLISIONS, cue_csr_read_2(sc, CUE_TX_EXCESSCOLL));
570
571	if (cue_csr_read_2(sc, CUE_RX_FRAMEERR))
572		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
573}
574
575static void
576cue_start(struct usb_ether *ue)
577{
578	struct cue_softc *sc = uether_getsc(ue);
579
580	/*
581	 * start the USB transfers, if not already started:
582	 */
583	usbd_transfer_start(sc->sc_xfer[CUE_BULK_DT_RD]);
584	usbd_transfer_start(sc->sc_xfer[CUE_BULK_DT_WR]);
585}
586
587static void
588cue_init(struct usb_ether *ue)
589{
590	struct cue_softc *sc = uether_getsc(ue);
591	if_t ifp = uether_getifp(ue);
592	int i;
593
594	CUE_LOCK_ASSERT(sc, MA_OWNED);
595
596	/*
597	 * Cancel pending I/O and free all RX/TX buffers.
598	 */
599	cue_stop(ue);
600#if 0
601	cue_reset(sc);
602#endif
603	/* Set MAC address */
604	for (i = 0; i < ETHER_ADDR_LEN; i++)
605		cue_csr_write_1(sc, CUE_PAR0 - i, if_getlladdr(ifp)[i]);
606
607	/* Enable RX logic. */
608	cue_csr_write_1(sc, CUE_ETHCTL, CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON);
609
610	/* Load the multicast filter */
611	cue_setpromisc(ue);
612
613	/*
614	 * Set the number of RX and TX buffers that we want
615	 * to reserve inside the ASIC.
616	 */
617	cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
618	cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
619
620	/* Set advanced operation modes. */
621	cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
622	    CUE_AOP_EMBED_RXLEN | 0x01);/* 1 wait state */
623
624	/* Program the LED operation. */
625	cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
626
627	usbd_xfer_set_stall(sc->sc_xfer[CUE_BULK_DT_WR]);
628
629	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
630	cue_start(ue);
631}
632
633/*
634 * Stop the adapter and free any mbufs allocated to the
635 * RX and TX lists.
636 */
637static void
638cue_stop(struct usb_ether *ue)
639{
640	struct cue_softc *sc = uether_getsc(ue);
641	if_t ifp = uether_getifp(ue);
642
643	CUE_LOCK_ASSERT(sc, MA_OWNED);
644
645	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
646
647	/*
648	 * stop all the transfers, if not already stopped:
649	 */
650	usbd_transfer_stop(sc->sc_xfer[CUE_BULK_DT_WR]);
651	usbd_transfer_stop(sc->sc_xfer[CUE_BULK_DT_RD]);
652
653	cue_csr_write_1(sc, CUE_ETHCTL, 0);
654	cue_reset(sc);
655}
656