if_cnw.c revision 1.52
1/*	$NetBSD: if_cnw.c,v 1.52 2009/11/22 21:18:42 dsl Exp $	*/
2
3/*-
4 * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Michael Eriksson.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * Copyright (c) 1996, 1997 Berkeley Software Design, Inc.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that this notice is retained,
38 * the conditions in the following notices are met, and terms applying
39 * to contributors in the following notices also apply to Berkeley
40 * Software Design, Inc.
41 *
42 * 1. Redistributions of source code must retain the above copyright
43 *    notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 *    notice, this list of conditions and the following disclaimer in the
46 *    documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 *    must display the following acknowledgement:
49 *      This product includes software developed by
50 *	Berkeley Software Design, Inc.
51 * 4. Neither the name of the Berkeley Software Design, Inc. nor the names
52 *    of its contributors may be used to endorse or promote products derived
53 *    from this software without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN, INC. ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN, INC. BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 * Paul Borman, December 1996
68 *
69 * This driver is derived from a generic frame work which is
70 * Copyright(c) 1994,1995,1996
71 * Yoichi Shinoda, Yoshitaka Tokugawa, WIDE Project, Wildboar Project
72 * and Foretune.  All rights reserved.
73 *
74 * A linux driver was used as the "hardware reference manual" (i.e.,
75 * to determine registers and a general outline of how the card works)
76 * That driver is publically available and copyright
77 *
78 * John Markus Bj�rndalen
79 * Department of Computer Science
80 * University of Troms�
81 * Norway
82 * johnm@staff.cs.uit.no, http://www.cs.uit.no/~johnm/
83 */
84
85/*
86 * This is a driver for the Xircom CreditCard Netwave (also known as
87 * the Netwave Airsurfer) wireless LAN PCMCIA adapter.
88 *
89 * When this driver was developed, the Linux Netwave driver was used
90 * as a hardware manual. That driver is Copyright (c) 1997 University
91 * of Troms�, Norway. It is part of the Linux pcmcia-cs package that
92 * can be found at http://pcmcia-cs.sourceforge.net/. The most recent
93 * version of the pcmcia-cs package when this driver was written was
94 * 3.0.6.
95 *
96 * Unfortunately, a lot of explicit numeric constants were used in the
97 * Linux driver. I have tried to use symbolic names whenever possible,
98 * but since I don't have any real hardware documentation, there's
99 * still one or two "magic numbers" :-(.
100 *
101 * Driver limitations: This driver doesn't do multicasting or receiver
102 * promiscuity, because of missing hardware documentation. I couldn't
103 * get receiver promiscuity to work, and I haven't even tried
104 * multicast. Volunteers are welcome, of course :-).
105 */
106
107#include <sys/cdefs.h>
108__KERNEL_RCSID(0, "$NetBSD: if_cnw.c,v 1.52 2009/11/22 21:18:42 dsl Exp $");
109
110#include "opt_inet.h"
111#include "bpfilter.h"
112
113#include <sys/param.h>
114#include <sys/systm.h>
115#include <sys/device.h>
116#include <sys/socket.h>
117#include <sys/mbuf.h>
118#include <sys/ioctl.h>
119#include <sys/proc.h>
120#include <sys/kauth.h>
121
122#include <net/if.h>
123
124#include <dev/pcmcia/if_cnwreg.h>
125#include <dev/pcmcia/if_cnwioctl.h>
126
127#include <dev/pcmcia/pcmciareg.h>
128#include <dev/pcmcia/pcmciavar.h>
129#include <dev/pcmcia/pcmciadevs.h>
130
131#include <net/if_dl.h>
132#include <net/if_ether.h>
133
134#ifdef INET
135#include <netinet/in.h>
136#include <netinet/in_systm.h>
137#include <netinet/in_var.h>
138#include <netinet/ip.h>
139#include <netinet/if_inarp.h>
140#endif
141
142#if NBPFILTER > 0
143#include <net/bpf.h>
144#include <net/bpfdesc.h>
145#endif
146
147/*
148 * Let these be patchable variables, initialized from macros that can
149 * be set in the kernel config file. Someone with lots of spare time
150 * could probably write a nice Netwave configuration program to do
151 * this a little bit more elegantly :-).
152 */
153#ifndef CNW_DOMAIN
154#define CNW_DOMAIN	0x100
155#endif
156int cnw_domain = CNW_DOMAIN;		/* Domain */
157#ifndef CNW_SCRAMBLEKEY
158#define CNW_SCRAMBLEKEY 0
159#endif
160int cnw_skey = CNW_SCRAMBLEKEY;		/* Scramble key */
161
162/*
163 * The card appears to work much better when we only allow one packet
164 * "in the air" at a time.  This is done by not allowing another packet
165 * on the card, even if there is room.  Turning this off will allow the
166 * driver to stuff packets on the card as soon as a transmit buffer is
167 * available.  This does increase the number of collisions, though.
168 * We can que a second packet if there are transmit buffers available,
169 * but we do not actually send the packet until the last packet has
170 * been written.
171 */
172#define	ONE_AT_A_TIME
173
174/*
175 * Netwave cards choke if we try to use io memory address >= 0x400.
176 * Even though, CIS tuple does not talk about this.
177 * Use memory mapped access.
178 */
179#define MEMORY_MAPPED
180
181int	cnw_match(device_t, cfdata_t, void *);
182void	cnw_attach(device_t, device_t, void *);
183int	cnw_detach(device_t, int);
184
185int	cnw_activate(device_t, enum devact);
186
187struct cnw_softc {
188	struct device sc_dev;		    /* Device glue (must be first) */
189	struct ethercom sc_ethercom;	    /* Ethernet common part */
190	int sc_domain;			    /* Netwave domain */
191	int sc_skey;			    /* Netwave scramble key */
192	struct cnwstats sc_stats;
193
194	/* PCMCIA-specific stuff */
195	struct pcmcia_function *sc_pf;	    /* PCMCIA function */
196#ifndef MEMORY_MAPPED
197	struct pcmcia_io_handle sc_pcioh;   /* PCMCIA I/O space handle */
198	int sc_iowin;			    /*   ...window */
199	bus_space_tag_t sc_iot;		    /*   ...bus_space tag */
200	bus_space_handle_t sc_ioh;	    /*   ...bus_space handle */
201#endif
202	struct pcmcia_mem_handle sc_pcmemh; /* PCMCIA memory handle */
203	bus_size_t sc_memoff;		    /*   ...offset */
204	int sc_memwin;			    /*   ...window */
205	bus_space_tag_t sc_memt;	    /*   ...bus_space tag */
206	bus_space_handle_t sc_memh;	    /*   ...bus_space handle */
207	void *sc_ih;			    /* Interrupt cookie */
208	struct timeval sc_txlast;	    /* When the last xmit was made */
209	int sc_active;			    /* Currently xmitting a packet */
210
211	int sc_resource;		    /* Resources alloc'ed on attach */
212#define CNW_RES_PCIC	1
213#define CNW_RES_IO	2
214#define CNW_RES_MEM	4
215#define CNW_RES_NET	8
216};
217
218CFATTACH_DECL(cnw, sizeof(struct cnw_softc),
219    cnw_match, cnw_attach, cnw_detach, cnw_activate);
220
221void cnw_reset(struct cnw_softc *);
222void cnw_init(struct cnw_softc *);
223int cnw_enable(struct cnw_softc *sc);
224void cnw_disable(struct cnw_softc *sc);
225void cnw_config(struct cnw_softc *sc, u_int8_t *);
226void cnw_start(struct ifnet *);
227void cnw_transmit(struct cnw_softc *, struct mbuf *);
228struct mbuf *cnw_read(struct cnw_softc *);
229void cnw_recv(struct cnw_softc *);
230int cnw_intr(void *arg);
231int cnw_ioctl(struct ifnet *, u_long, void *);
232void cnw_watchdog(struct ifnet *);
233static int cnw_setdomain(struct cnw_softc *, int);
234static int cnw_setkey(struct cnw_softc *, int);
235
236/* ---------------------------------------------------------------- */
237
238/* Help routines */
239static int wait_WOC(struct cnw_softc *, int);
240static int read16(struct cnw_softc *, int);
241static int cnw_cmd(struct cnw_softc *, int, int, int, int);
242
243/*
244 * Wait until the WOC (Write Operation Complete) bit in the
245 * ASR (Adapter Status Register) is asserted.
246 */
247static int
248wait_WOC(struct cnw_softc *sc, int line)
249{
250	int i, asr;
251
252	for (i = 0; i < 5000; i++) {
253#ifndef MEMORY_MAPPED
254		asr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, CNW_REG_ASR);
255#else
256		asr = bus_space_read_1(sc->sc_memt, sc->sc_memh,
257		    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_ASR);
258#endif
259		if (asr & CNW_ASR_WOC)
260			return (0);
261		DELAY(100);
262	}
263	if (line > 0)
264		printf("%s: wedged at line %d\n", device_xname(&sc->sc_dev), line);
265	return (1);
266}
267#define WAIT_WOC(sc) wait_WOC(sc, __LINE__)
268
269
270/*
271 * Read a 16 bit value from the card.
272 */
273static int
274read16(struct cnw_softc *sc, int offset)
275{
276	int hi, lo;
277	int offs = sc->sc_memoff + offset;
278
279	/* This could presumably be done more efficient with
280	 * bus_space_read_2(), but I don't know anything about the
281	 * byte sex guarantees... Besides, this is pretty cheap as
282	 * well :-)
283	 */
284	lo = bus_space_read_1(sc->sc_memt, sc->sc_memh, offs);
285	hi = bus_space_read_1(sc->sc_memt, sc->sc_memh, offs + 1);
286	return ((hi << 8) | lo);
287}
288
289
290/*
291 * Send a command to the card by writing it to the command buffer.
292 */
293int
294cnw_cmd(struct cnw_softc *sc, int cmd, int count, int arg1, int arg2)
295{
296	int ptr = sc->sc_memoff + CNW_EREG_CB;
297
298	if (wait_WOC(sc, 0)) {
299		printf("%s: wedged when issuing cmd 0x%x\n",
300		    device_xname(&sc->sc_dev), cmd);
301		/*
302		 * We'll continue anyway, as that's probably the best
303		 * thing we can do; at least the user knows there's a
304		 * problem, and can reset the interface with ifconfig
305		 * down/up.
306		 */
307	}
308
309	bus_space_write_1(sc->sc_memt, sc->sc_memh, ptr, cmd);
310	if (count > 0) {
311		bus_space_write_1(sc->sc_memt, sc->sc_memh, ptr + 1, arg1);
312		if (count > 1)
313			bus_space_write_1(sc->sc_memt, sc->sc_memh,
314			    ptr + 2, arg2);
315	}
316	bus_space_write_1(sc->sc_memt, sc->sc_memh,
317	    ptr + count + 1, CNW_CMD_EOC);
318	return (0);
319}
320#define CNW_CMD0(sc, cmd) \
321    do { cnw_cmd(sc, cmd, 0, 0, 0); } while (0)
322#define CNW_CMD1(sc, cmd, arg1)	\
323    do { cnw_cmd(sc, cmd, 1, arg1 , 0); } while (0)
324#define CNW_CMD2(sc, cmd, arg1, arg2) \
325    do { cnw_cmd(sc, cmd, 2, arg1, arg2); } while (0)
326
327/* ---------------------------------------------------------------- */
328
329/*
330 * Reset the hardware.
331 */
332void
333cnw_reset(struct cnw_softc *sc)
334{
335#ifdef CNW_DEBUG
336	if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
337		printf("%s: resetting\n", device_xname(&sc->sc_dev));
338#endif
339	wait_WOC(sc, 0);
340#ifndef MEMORY_MAPPED
341	bus_space_write_1(sc->sc_iot, sc->sc_ioh, CNW_REG_PMR, CNW_PMR_RESET);
342#else
343	bus_space_write_1(sc->sc_memt, sc->sc_memh,
344	    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_PMR, CNW_PMR_RESET);
345#endif
346	bus_space_write_1(sc->sc_memt, sc->sc_memh,
347	    sc->sc_memoff + CNW_EREG_ASCC, CNW_ASR_WOC);
348#ifndef MEMORY_MAPPED
349	bus_space_write_1(sc->sc_iot, sc->sc_ioh, CNW_REG_PMR, 0);
350#else
351	bus_space_write_1(sc->sc_memt, sc->sc_memh,
352	    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_PMR, 0);
353#endif
354}
355
356
357/*
358 * Initialize the card.
359 */
360void
361cnw_init(struct cnw_softc *sc)
362{
363	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
364	const u_int8_t rxmode =
365	    CNW_RXCONF_RXENA | CNW_RXCONF_BCAST | CNW_RXCONF_AMP;
366
367	/* Reset the card */
368	cnw_reset(sc);
369
370	/* Issue a NOP to check the card */
371	CNW_CMD0(sc, CNW_CMD_NOP);
372
373	/* Set up receive configuration */
374	CNW_CMD1(sc, CNW_CMD_SRC,
375	    rxmode | ((ifp->if_flags & IFF_PROMISC) ? CNW_RXCONF_PRO : 0));
376
377	/* Set up transmit configuration */
378	CNW_CMD1(sc, CNW_CMD_STC, CNW_TXCONF_TXENA);
379
380	/* Set domain */
381	CNW_CMD2(sc, CNW_CMD_SMD, sc->sc_domain, sc->sc_domain >> 8);
382
383	/* Set scramble key */
384	CNW_CMD2(sc, CNW_CMD_SSK, sc->sc_skey, sc->sc_skey >> 8);
385
386	/* Enable interrupts */
387	WAIT_WOC(sc);
388#ifndef MEMORY_MAPPED
389	bus_space_write_1(sc->sc_iot, sc->sc_ioh,
390	    CNW_REG_IMR, CNW_IMR_IENA | CNW_IMR_RFU1);
391#else
392	bus_space_write_1(sc->sc_memt, sc->sc_memh,
393	    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_IMR,
394	    CNW_IMR_IENA | CNW_IMR_RFU1);
395#endif
396
397	/* Enable receiver */
398	CNW_CMD0(sc, CNW_CMD_ER);
399
400	/* "Set the IENA bit in COR" */
401	WAIT_WOC(sc);
402#ifndef MEMORY_MAPPED
403	bus_space_write_1(sc->sc_iot, sc->sc_ioh, CNW_REG_COR,
404	    CNW_COR_IENA | CNW_COR_LVLREQ);
405#else
406	bus_space_write_1(sc->sc_memt, sc->sc_memh,
407	    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_COR,
408	    CNW_COR_IENA | CNW_COR_LVLREQ);
409#endif
410}
411
412
413/*
414 * Enable and initialize the card.
415 */
416int
417cnw_enable(struct cnw_softc *sc)
418{
419	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
420
421	if ((ifp->if_flags & IFF_RUNNING) != 0)
422		return (0);
423
424	sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_NET, cnw_intr, sc);
425	if (sc->sc_ih == NULL) {
426		aprint_error_dev(&sc->sc_dev, "couldn't establish interrupt handler\n");
427		return (EIO);
428	}
429	if (pcmcia_function_enable(sc->sc_pf) != 0) {
430		aprint_error_dev(&sc->sc_dev, "couldn't enable card\n");
431		return (EIO);
432	}
433	sc->sc_resource |= CNW_RES_PCIC;
434	cnw_init(sc);
435	ifp->if_flags &= ~IFF_OACTIVE;
436	ifp->if_flags |= IFF_RUNNING;
437	return (0);
438}
439
440
441/*
442 * Stop and disable the card.
443 */
444void
445cnw_disable(struct cnw_softc *sc)
446{
447	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
448
449	if ((ifp->if_flags & IFF_RUNNING) == 0)
450		return;
451
452	pcmcia_function_disable(sc->sc_pf);
453	sc->sc_resource &= ~CNW_RES_PCIC;
454	pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
455	ifp->if_flags &= ~IFF_RUNNING;
456	ifp->if_timer = 0;
457}
458
459
460/*
461 * Match the hardware we handle.
462 */
463int
464cnw_match(device_t parent, cfdata_t match,
465    void *aux)
466{
467	struct pcmcia_attach_args *pa = aux;
468
469	if (pa->manufacturer == PCMCIA_VENDOR_XIRCOM &&
470	    pa->product == PCMCIA_PRODUCT_XIRCOM_CNW_801)
471		return 1;
472	if (pa->manufacturer == PCMCIA_VENDOR_XIRCOM &&
473	    pa->product == PCMCIA_PRODUCT_XIRCOM_CNW_802)
474		return 1;
475	return 0;
476}
477
478
479/*
480 * Attach the card.
481 */
482void
483cnw_attach(struct device  *parent, device_t self, void *aux)
484{
485	struct cnw_softc *sc = (void *) self;
486	struct pcmcia_attach_args *pa = aux;
487	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
488	u_int8_t macaddr[ETHER_ADDR_LEN];
489	int i;
490	bus_size_t memsize;
491
492	sc->sc_resource = 0;
493
494	/* Enable the card */
495	sc->sc_pf = pa->pf;
496	pcmcia_function_init(sc->sc_pf, SIMPLEQ_FIRST(&sc->sc_pf->cfe_head));
497	if (pcmcia_function_enable(sc->sc_pf)) {
498		aprint_error_dev(self, "function enable failed\n");
499		return;
500	}
501	sc->sc_resource |= CNW_RES_PCIC;
502
503	/* Map I/O register and "memory" */
504#ifndef MEMORY_MAPPED
505	if (pcmcia_io_alloc(sc->sc_pf, 0, CNW_IO_SIZE, CNW_IO_SIZE,
506	    &sc->sc_pcioh) != 0) {
507		aprint_error_dev(self, "can't allocate i/o space\n");
508		goto fail;
509	}
510	if (pcmcia_io_map(sc->sc_pf, PCMCIA_WIDTH_IO16, &sc->sc_pcioh,
511	    &sc->sc_iowin) != 0) {
512		aprint_error_dev(self, "can't map i/o space\n");
513		pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
514		goto fail;
515	}
516	sc->sc_iot = sc->sc_pcioh.iot;
517	sc->sc_ioh = sc->sc_pcioh.ioh;
518	sc->sc_resource |= CNW_RES_IO;
519#endif
520#ifndef MEMORY_MAPPED
521	memsize = CNW_MEM_SIZE;
522#else
523	memsize = CNW_MEM_SIZE + CNW_IOM_SIZE;
524#endif
525	if (pcmcia_mem_alloc(sc->sc_pf, memsize, &sc->sc_pcmemh) != 0) {
526		aprint_error_dev(self, "can't allocate memory\n");
527		goto fail;
528	}
529	if (pcmcia_mem_map(sc->sc_pf, PCMCIA_WIDTH_MEM8|PCMCIA_MEM_COMMON,
530	    CNW_MEM_ADDR, memsize, &sc->sc_pcmemh, &sc->sc_memoff,
531	    &sc->sc_memwin) != 0) {
532		aprint_error_dev(self, "can't map memory\n");
533		pcmcia_mem_free(sc->sc_pf, &sc->sc_pcmemh);
534		goto fail;
535	}
536	sc->sc_memt = sc->sc_pcmemh.memt;
537	sc->sc_memh = sc->sc_pcmemh.memh;
538	sc->sc_resource |= CNW_RES_MEM;
539
540	/* Finish setup of softc */
541	sc->sc_domain = cnw_domain;
542	sc->sc_skey = cnw_skey;
543
544	/* Get MAC address */
545	cnw_reset(sc);
546	for (i = 0; i < ETHER_ADDR_LEN; i++)
547		macaddr[i] = bus_space_read_1(sc->sc_memt, sc->sc_memh,
548		    sc->sc_memoff + CNW_EREG_PA + i);
549	printf("%s: address %s\n", device_xname(&sc->sc_dev),
550	    ether_sprintf(macaddr));
551
552	/* Set up ifnet structure */
553	strlcpy(ifp->if_xname, device_xname(&sc->sc_dev), IFNAMSIZ);
554	ifp->if_softc = sc;
555	ifp->if_start = cnw_start;
556	ifp->if_ioctl = cnw_ioctl;
557	ifp->if_watchdog = cnw_watchdog;
558	ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX |
559	    IFF_NOTRAILERS;
560	IFQ_SET_READY(&ifp->if_snd);
561
562	/* Attach the interface */
563	if_attach(ifp);
564	ether_ifattach(ifp, macaddr);
565
566	sc->sc_resource |= CNW_RES_NET;
567
568	ifp->if_baudrate = IF_Mbps(1);
569
570	/* Disable the card now, and turn it on when the interface goes up */
571	pcmcia_function_disable(sc->sc_pf);
572	sc->sc_resource &= ~CNW_RES_PCIC;
573	return;
574
575fail:
576#ifndef MEMORY_MAPPED
577	if ((sc->sc_resource & CNW_RES_IO) != 0) {
578		pcmcia_io_unmap(sc->sc_pf, sc->sc_iowin);
579		pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
580		sc->sc_resource &= ~CNW_RES_IO;
581	}
582#endif
583	if ((sc->sc_resource & CNW_RES_PCIC) != 0) {
584		pcmcia_function_disable(sc->sc_pf);
585		sc->sc_resource &= ~CNW_RES_PCIC;
586	}
587}
588
589/*
590 * Start outputting on the interface.
591 */
592void
593cnw_start(struct ifnet *ifp)
594{
595	struct cnw_softc *sc = ifp->if_softc;
596	struct mbuf *m0;
597	int lif;
598	int asr;
599#ifdef ONE_AT_A_TIME
600	struct timeval now;
601#endif
602
603#ifdef CNW_DEBUG
604	if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
605		printf("%s: cnw_start\n", ifp->if_xname);
606	if (ifp->if_flags & IFF_OACTIVE)
607		printf("%s: cnw_start reentered\n", ifp->if_xname);
608#endif
609
610	ifp->if_flags |= IFF_OACTIVE;
611
612	for (;;) {
613#ifdef ONE_AT_A_TIME
614		microtime(&now);
615		now.tv_sec -= sc->sc_txlast.tv_sec;
616		now.tv_usec -= sc->sc_txlast.tv_usec;
617		if (now.tv_usec < 0) {
618			now.tv_usec += 1000000;
619			now.tv_sec--;
620		}
621
622		/*
623		 * Don't ship this packet out until the last
624		 * packet has left the building.
625		 * If we have not tried to send a packet for 1/5
626		 * a second then we assume we lost an interrupt,
627		 * lets go on and send the next packet anyhow.
628		 *
629		 * I suppose we could check to see if it is okay
630		 * to put additional packets on the card (beyond
631		 * the one already waiting to be sent) but I don't
632		 * think we would get any improvement in speed as
633		 * we should have ample time to put the next packet
634		 * on while this one is going out.
635		 */
636		if (sc->sc_active && now.tv_sec == 0 && now.tv_usec < 200000)
637			break;
638#endif
639
640		/* Make sure the link integrity field is on */
641		WAIT_WOC(sc);
642		lif = bus_space_read_1(sc->sc_memt, sc->sc_memh,
643		    sc->sc_memoff + CNW_EREG_LIF);
644		if (lif == 0) {
645#ifdef CNW_DEBUG
646			if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
647				printf("%s: link integrity %d\n", ifp->if_xname, lif);
648#endif
649			break;
650		}
651
652		/* Is there any buffer space available on the card? */
653		WAIT_WOC(sc);
654#ifndef MEMORY_MAPPED
655		asr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, CNW_REG_ASR);
656#else
657		asr = bus_space_read_1(sc->sc_memt, sc->sc_memh,
658		    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_ASR);
659#endif
660		if (!(asr & CNW_ASR_TXBA)) {
661#ifdef CNW_DEBUG
662			if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
663				printf("%s: no buffer space\n", ifp->if_xname);
664#endif
665			break;
666		}
667
668		sc->sc_stats.nws_tx++;
669
670		IFQ_DEQUEUE(&ifp->if_snd, m0);
671		if (m0 == 0)
672			break;
673
674#if NBPFILTER > 0
675		if (ifp->if_bpf)
676			bpf_mtap(ifp->if_bpf, m0);
677#endif
678
679		cnw_transmit(sc, m0);
680		++ifp->if_opackets;
681		ifp->if_timer = 3; /* start watchdog timer */
682
683		microtime(&sc->sc_txlast);
684		sc->sc_active = 1;
685	}
686
687	ifp->if_flags &= ~IFF_OACTIVE;
688}
689
690/*
691 * Transmit a packet.
692 */
693void
694cnw_transmit(struct cnw_softc *sc, struct mbuf *m0)
695{
696	int buffer, bufsize, bufoffset, bufptr, bufspace, len, mbytes, n;
697	struct mbuf *m;
698	u_int8_t *mptr;
699
700	/* Get buffer info from card */
701	buffer = read16(sc, CNW_EREG_TDP);
702	bufsize = read16(sc, CNW_EREG_TDP + 2);
703	bufoffset = read16(sc, CNW_EREG_TDP + 4);
704#ifdef CNW_DEBUG
705	if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
706		printf("%s: cnw_transmit b=0x%x s=%d o=0x%x\n",
707		    device_xname(&sc->sc_dev), buffer, bufsize, bufoffset);
708#endif
709
710	/* Copy data from mbuf chain to card buffers */
711	bufptr = sc->sc_memoff + buffer + bufoffset;
712	bufspace = bufsize;
713	len = 0;
714	for (m = m0; m; ) {
715		mptr = mtod(m, u_int8_t *);
716		mbytes = m->m_len;
717		len += mbytes;
718		while (mbytes > 0) {
719			if (bufspace == 0) {
720				buffer = read16(sc, buffer);
721				bufptr = sc->sc_memoff + buffer + bufoffset;
722				bufspace = bufsize;
723#ifdef CNW_DEBUG
724				if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
725					printf("%s:   next buffer @0x%x\n",
726					    device_xname(&sc->sc_dev), buffer);
727#endif
728			}
729			n = mbytes <= bufspace ? mbytes : bufspace;
730			bus_space_write_region_1(sc->sc_memt, sc->sc_memh,
731			    bufptr, mptr, n);
732			bufptr += n;
733			bufspace -= n;
734			mptr += n;
735			mbytes -= n;
736		}
737		MFREE(m, m0);
738		m = m0;
739	}
740
741	/* Issue transmit command */
742	CNW_CMD2(sc, CNW_CMD_TL, len, len >> 8);
743}
744
745
746/*
747 * Pull a packet from the card into an mbuf chain.
748 */
749struct mbuf *
750cnw_read(struct cnw_softc *sc)
751{
752	struct mbuf *m, *top, **mp;
753	int totbytes, buffer, bufbytes, bufptr, mbytes, n;
754	u_int8_t *mptr;
755
756	WAIT_WOC(sc);
757	totbytes = read16(sc, CNW_EREG_RDP);
758#ifdef CNW_DEBUG
759	if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
760		printf("%s: recv %d bytes\n", device_xname(&sc->sc_dev), totbytes);
761#endif
762	buffer = CNW_EREG_RDP + 2;
763	bufbytes = 0;
764	bufptr = 0; /* XXX make gcc happy */
765
766	MGETHDR(m, M_DONTWAIT, MT_DATA);
767	if (m == 0)
768		return (0);
769	m->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
770	m->m_pkthdr.len = totbytes;
771	mbytes = MHLEN;
772	top = 0;
773	mp = &top;
774
775	while (totbytes > 0) {
776		if (top) {
777			MGET(m, M_DONTWAIT, MT_DATA);
778			if (m == 0) {
779				m_freem(top);
780				return (0);
781			}
782			mbytes = MLEN;
783		}
784		if (totbytes >= MINCLSIZE) {
785			MCLGET(m, M_DONTWAIT);
786			if ((m->m_flags & M_EXT) == 0) {
787				m_free(m);
788				m_freem(top);
789				return (0);
790			}
791			mbytes = MCLBYTES;
792		}
793		if (!top) {
794			int pad = ALIGN(sizeof(struct ether_header)) -
795			    sizeof(struct ether_header);
796			m->m_data += pad;
797			mbytes -= pad;
798		}
799		mptr = mtod(m, u_int8_t *);
800		mbytes = m->m_len = min(totbytes, mbytes);
801		totbytes -= mbytes;
802		while (mbytes > 0) {
803			if (bufbytes == 0) {
804				buffer = read16(sc, buffer);
805				bufbytes = read16(sc, buffer + 2);
806				bufptr = sc->sc_memoff + buffer +
807				    read16(sc, buffer + 4);
808#ifdef CNW_DEBUG
809				if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
810					printf("%s:   %d bytes @0x%x+0x%lx\n",
811					    device_xname(&sc->sc_dev), bufbytes,
812					    buffer, (u_long)(bufptr - buffer -
813					    sc->sc_memoff));
814#endif
815			}
816			n = mbytes <= bufbytes ? mbytes : bufbytes;
817			bus_space_read_region_1(sc->sc_memt, sc->sc_memh,
818			    bufptr, mptr, n);
819			bufbytes -= n;
820			bufptr += n;
821			mbytes -= n;
822			mptr += n;
823		}
824		*mp = m;
825		mp = &m->m_next;
826	}
827
828	return (top);
829}
830
831
832/*
833 * Handle received packets.
834 */
835void
836cnw_recv(struct cnw_softc *sc)
837{
838	int rser;
839	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
840	struct mbuf *m;
841
842	for (;;) {
843		WAIT_WOC(sc);
844		rser = bus_space_read_1(sc->sc_memt, sc->sc_memh,
845		    sc->sc_memoff + CNW_EREG_RSER);
846		if (!(rser & CNW_RSER_RXAVAIL))
847			return;
848
849		/* Pull packet off card */
850		m = cnw_read(sc);
851
852		/* Acknowledge packet */
853		CNW_CMD0(sc, CNW_CMD_SRP);
854
855		/* Did we manage to get the packet from the interface? */
856		if (m == 0) {
857			++ifp->if_ierrors;
858			return;
859		}
860		++ifp->if_ipackets;
861
862#if NBPFILTER > 0
863		if (ifp->if_bpf)
864			bpf_mtap(ifp->if_bpf, m);
865#endif
866
867		/* Pass the packet up. */
868		(*ifp->if_input)(ifp, m);
869	}
870}
871
872
873/*
874 * Interrupt handler.
875 */
876int
877cnw_intr(void *arg)
878{
879	struct cnw_softc *sc = arg;
880	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
881	int ret, status, rser, tser;
882
883	if ((sc->sc_ethercom.ec_if.if_flags & IFF_RUNNING) == 0 ||
884	    !device_is_active(&sc->sc_dev))
885		return (0);
886	ifp->if_timer = 0;	/* stop watchdog timer */
887
888	ret = 0;
889	for (;;) {
890		WAIT_WOC(sc);
891#ifndef MEMORY_MAPPED
892		status = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
893		    CNW_REG_CCSR);
894#else
895		status = bus_space_read_1(sc->sc_memt, sc->sc_memh,
896		    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_CCSR);
897#endif
898		if (!(status & 0x02))
899			/* No more commands, or shared IRQ */
900			return (ret);
901		ret = 1;
902#ifndef MEMORY_MAPPED
903		status = bus_space_read_1(sc->sc_iot, sc->sc_ioh, CNW_REG_ASR);
904#else
905		status = bus_space_read_1(sc->sc_memt, sc->sc_memh,
906		    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_ASR);
907#endif
908
909		/* Anything to receive? */
910		if (status & CNW_ASR_RXRDY) {
911			sc->sc_stats.nws_rx++;
912			cnw_recv(sc);
913		}
914
915		/* Receive error */
916		if (status & CNW_ASR_RXERR) {
917			/*
918			 * I get a *lot* of spurious receive errors
919			 * (many per second), even when the interface
920			 * is quiescent, so we don't increment
921			 * if_ierrors here.
922			 */
923			rser = bus_space_read_1(sc->sc_memt, sc->sc_memh,
924			    sc->sc_memoff + CNW_EREG_RSER);
925
926			/* RX statistics */
927			sc->sc_stats.nws_rxerr++;
928			if (rser & CNW_RSER_RXBIG)
929				sc->sc_stats.nws_rxframe++;
930			if (rser & CNW_RSER_RXCRC)
931				sc->sc_stats.nws_rxcrcerror++;
932			if (rser & CNW_RSER_RXOVERRUN)
933				sc->sc_stats.nws_rxoverrun++;
934			if (rser & CNW_RSER_RXOVERFLOW)
935				sc->sc_stats.nws_rxoverflow++;
936			if (rser & CNW_RSER_RXERR)
937				sc->sc_stats.nws_rxerrors++;
938			if (rser & CNW_RSER_RXAVAIL)
939				sc->sc_stats.nws_rxavail++;
940
941			/* Clear error bits in RSER */
942			WAIT_WOC(sc);
943			bus_space_write_1(sc->sc_memt, sc->sc_memh,
944			    sc->sc_memoff + CNW_EREG_RSERW,
945			    CNW_RSER_RXERR |
946			    (rser & (CNW_RSER_RXCRC | CNW_RSER_RXBIG)));
947			/* Clear RXERR in ASR */
948			WAIT_WOC(sc);
949			bus_space_write_1(sc->sc_memt, sc->sc_memh,
950			    sc->sc_memoff + CNW_EREG_ASCC, CNW_ASR_RXERR);
951		}
952
953		/* Transmit done */
954		if (status & CNW_ASR_TXDN) {
955			tser = bus_space_read_1(sc->sc_memt, sc->sc_memh,
956						CNW_EREG_TSER);
957
958			/* TX statistics */
959			if (tser & CNW_TSER_TXERR)
960				sc->sc_stats.nws_txerrors++;
961			if (tser & CNW_TSER_TXNOAP)
962				sc->sc_stats.nws_txlostcd++;
963			if (tser & CNW_TSER_TXGU)
964				sc->sc_stats.nws_txabort++;
965
966			if (tser & CNW_TSER_TXOK) {
967				sc->sc_stats.nws_txokay++;
968				sc->sc_stats.nws_txretries[status & 0xf]++;
969				WAIT_WOC(sc);
970				bus_space_write_1(sc->sc_memt, sc->sc_memh,
971				    sc->sc_memoff + CNW_EREG_TSERW,
972				    CNW_TSER_TXOK | CNW_TSER_RTRY);
973			}
974
975			if (tser & CNW_TSER_ERROR) {
976				++ifp->if_oerrors;
977				WAIT_WOC(sc);
978				bus_space_write_1(sc->sc_memt, sc->sc_memh,
979				    sc->sc_memoff + CNW_EREG_TSERW,
980				    (tser & CNW_TSER_ERROR) |
981				    CNW_TSER_RTRY);
982			}
983
984			sc->sc_active = 0;
985			ifp->if_flags &= ~IFF_OACTIVE;
986
987			/* Continue to send packets from the queue */
988			cnw_start(&sc->sc_ethercom.ec_if);
989		}
990
991	}
992}
993
994
995/*
996 * Handle device ioctls.
997 */
998int
999cnw_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1000{
1001	struct cnw_softc *sc = ifp->if_softc;
1002	struct ifaddr *ifa = (struct ifaddr *)data;
1003	struct ifreq *ifr = (struct ifreq *)data;
1004	int s, error = 0;
1005	struct lwp *l = curlwp;	/*XXX*/
1006
1007	switch (cmd) {
1008	case SIOCINITIFADDR:
1009	case SIOCSIFFLAGS:
1010	case SIOCADDMULTI:
1011	case SIOCDELMULTI:
1012	case SIOCGCNWDOMAIN:
1013	case SIOCGCNWSTATS:
1014		break;
1015	case SIOCSCNWDOMAIN:
1016	case SIOCSCNWKEY:
1017		error = kauth_authorize_network(l->l_cred,
1018		    KAUTH_NETWORK_INTERFACE,
1019		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, KAUTH_ARG(cmd),
1020		    NULL);
1021		if (error)
1022			return (error);
1023		break;
1024	case SIOCGCNWSTATUS:
1025		error = kauth_authorize_network(l->l_cred,
1026		    KAUTH_NETWORK_INTERFACE,
1027		    KAUTH_REQ_NETWORK_INTERFACE_GETPRIV, ifp, KAUTH_ARG(cmd),
1028		    NULL);
1029		if (error)
1030			return (error);
1031		break;
1032	default:
1033		return (EINVAL);
1034	}
1035
1036	s = splnet();
1037
1038	switch (cmd) {
1039
1040	case SIOCINITIFADDR:
1041		if (!(ifp->if_flags & IFF_RUNNING) &&
1042		    (error = cnw_enable(sc)) != 0)
1043			break;
1044		ifp->if_flags |= IFF_UP;
1045		cnw_init(sc);
1046		switch (ifa->ifa_addr->sa_family) {
1047#ifdef INET
1048		case AF_INET:
1049			arp_ifinit(&sc->sc_ethercom.ec_if, ifa);
1050			break;
1051#endif
1052		default:
1053			break;
1054		}
1055		break;
1056
1057	case SIOCSIFFLAGS:
1058		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1059			break;
1060		/* XXX re-use ether_ioctl() */
1061		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
1062		case IFF_RUNNING:
1063			/*
1064			 * The interface is marked down and it is running, so
1065			 * stop it.
1066			 */
1067			cnw_disable(sc);
1068			break;
1069		case IFF_UP:
1070			/*
1071			 * The interface is marked up and it is stopped, so
1072			 * start it.
1073			 */
1074			error = cnw_enable(sc);
1075			break;
1076		default:
1077			/* IFF_PROMISC may be changed */
1078			cnw_init(sc);
1079			break;
1080		}
1081		break;
1082
1083	case SIOCADDMULTI:
1084	case SIOCDELMULTI:
1085		/* Update our multicast list. */
1086		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
1087			if (ifp->if_flags & IFF_RUNNING)
1088				cnw_init(sc);
1089			error = 0;
1090		}
1091		break;
1092
1093	case SIOCGCNWDOMAIN:
1094		ifr->ifr_domain = sc->sc_domain;
1095		break;
1096
1097	case SIOCSCNWDOMAIN:
1098		error = cnw_setdomain(sc, ifr->ifr_domain);
1099		break;
1100
1101	case SIOCSCNWKEY:
1102		error = cnw_setkey(sc, ifr->ifr_key);
1103		break;
1104
1105	case SIOCGCNWSTATUS:
1106		if ((ifp->if_flags & IFF_RUNNING) == 0)
1107			break;
1108		bus_space_read_region_1(sc->sc_memt, sc->sc_memh,
1109		    sc->sc_memoff + CNW_EREG_CB,
1110		    ((struct cnwstatus *)data)->data,
1111		    sizeof(((struct cnwstatus *)data)->data));
1112		break;
1113
1114	case SIOCGCNWSTATS:
1115		memcpy((void *)&(((struct cnwistats *)data)->stats),
1116		    (void *)&sc->sc_stats, sizeof(struct cnwstats));
1117			break;
1118
1119	default:
1120		error = ether_ioctl(ifp, cmd, data);
1121		break;
1122	}
1123
1124	splx(s);
1125	return (error);
1126}
1127
1128
1129/*
1130 * Device timeout/watchdog routine. Entered if the device neglects to
1131 * generate an interrupt after a transmit has been started on it.
1132 */
1133void
1134cnw_watchdog(struct ifnet *ifp)
1135{
1136	struct cnw_softc *sc = ifp->if_softc;
1137
1138	printf("%s: device timeout; card reset\n", device_xname(&sc->sc_dev));
1139	++ifp->if_oerrors;
1140	cnw_init(sc);
1141}
1142
1143int
1144cnw_setdomain(struct cnw_softc *sc, int domain)
1145{
1146	int s;
1147
1148	if (domain & ~0x1ff)
1149		return EINVAL;
1150
1151	s = splnet();
1152	CNW_CMD2(sc, CNW_CMD_SMD, domain, domain >> 8);
1153	splx(s);
1154
1155	sc->sc_domain = domain;
1156	return 0;
1157}
1158
1159int
1160cnw_setkey(struct cnw_softc *sc, int key)
1161{
1162	int s;
1163
1164	if (key & ~0xffff)
1165		return EINVAL;
1166
1167	s = splnet();
1168	CNW_CMD2(sc, CNW_CMD_SSK, key, key >> 8);
1169	splx(s);
1170
1171	sc->sc_skey = key;
1172	return 0;
1173}
1174
1175int
1176cnw_activate(device_t self, enum devact act)
1177{
1178	struct cnw_softc *sc = (struct cnw_softc *)self;
1179	int rv = 0, s;
1180
1181	s = splnet();
1182	switch (act) {
1183	case DVACT_ACTIVATE:
1184		rv = EOPNOTSUPP;
1185		break;
1186
1187	case DVACT_DEACTIVATE:
1188		if_deactivate(&sc->sc_ethercom.ec_if);
1189		break;
1190	}
1191	splx(s);
1192	return (rv);
1193}
1194
1195int
1196cnw_detach(device_t self, int flags)
1197{
1198	struct cnw_softc *sc = (struct cnw_softc *)self;
1199	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1200
1201	/* cnw_disable() checks IFF_RUNNING */
1202	cnw_disable(sc);
1203
1204	if ((sc->sc_resource & CNW_RES_NET) != 0) {
1205		ether_ifdetach(ifp);
1206		if_detach(ifp);
1207	}
1208
1209#ifndef MEMORY_MAPPED
1210	/* unmap and free our i/o windows */
1211	if ((sc->sc_resource & CNW_RES_IO) != 0) {
1212		pcmcia_io_unmap(sc->sc_pf, sc->sc_iowin);
1213		pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
1214	}
1215#endif
1216
1217	/* unmap and free our memory windows */
1218	if ((sc->sc_resource & CNW_RES_MEM) != 0) {
1219		pcmcia_mem_unmap(sc->sc_pf, sc->sc_memwin);
1220		pcmcia_mem_free(sc->sc_pf, &sc->sc_pcmemh);
1221	}
1222
1223	return (0);
1224}
1225