if_xe_pccard.c revision 129764
1/*
2 * Copyright (c) 2002 Takeshi Shibagaki
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/dev/xe/if_xe_pccard.c 129764 2004-05-27 03:49:45Z imp $");
29
30/* xe pccard interface driver */
31
32#include <sys/param.h>
33#include <sys/bus.h>
34#include <sys/kernel.h>
35#include <sys/socket.h>
36#include <sys/systm.h>
37
38#include <machine/bus.h>
39#include <machine/resource.h>
40#include <sys/rman.h>
41
42#include <net/ethernet.h>
43#include <net/if.h>
44#include <net/if_arp.h>
45#include <net/if_media.h>
46#include <net/if_mib.h>
47
48
49#include <dev/xe/if_xereg.h>
50#include <dev/xe/if_xevar.h>
51
52#include <dev/pccard/pccardvar.h>
53#include <dev/pccard/pccard_cis.h>
54
55#include "card_if.h"
56#include "pccarddevs.h"
57
58/*
59 * Debug logging levels - set with hw.xe.debug sysctl
60 * 0 = None
61 * 1 = More hardware details, probe/attach progress
62 * 2 = Most function calls, ioctls and media selection progress
63 * 3 = Everything - interrupts, packets in/out and multicast address setup
64 */
65#define XE_DEBUG
66#ifdef XE_DEBUG
67
68extern int xe_debug;
69
70#define DEVPRINTF(level, arg) if (xe_debug >= (level)) device_printf arg
71#define DPRINTF(level, arg) if (xe_debug >= (level)) printf arg
72#else
73#define DEVPRINTF(level, arg)
74#define DPRINTF(level, arg)
75#endif
76
77
78#define XE_CARD_TYPE_FLAGS_NO 0x0
79#define XE_CARD_TYPE_FLAGS_CE2 0x1
80#define XE_CARD_TYPE_FLAGS_MOHAWK 0x2
81#define XE_CARD_TYPE_FLAGS_DINGO 0x4
82#define XE_PROD_ETHER_MASK 0x0100
83#define XE_PROD_MODEM_MASK 0x1000
84
85#define XE_BOGUS_MAC_OFFSET 0x90
86
87/* MAC vendor prefix used by most Xircom cards is 00:80:c7 */
88#define XE_MAC_ADDR_0 0x00
89#define XE_MAC_ADDR_1 0x80
90#define XE_MAC_ADDR_2 0xc7
91
92/* Some (all?) REM56 cards have vendor prefix 00:10:a4 */
93#define XE_REM56_MAC_ADDR_0 0x00
94#define XE_REM56_MAC_ADDR_1 0x10
95#define XE_REM56_MAC_ADDR_2 0xa4
96
97
98struct xe_pccard_product {
99	struct pccard_product product;
100	u_int16_t prodext;
101	u_int16_t flags;
102};
103
104static const struct xe_pccard_product xe_pccard_products[] = {
105	{ PCMCIA_CARD_D(ACCTON, EN2226, 0),      0x43, XE_CARD_TYPE_FLAGS_MOHAWK },
106	{ PCMCIA_CARD_D(COMPAQ2, CPQ_10_100, 0), 0x43, XE_CARD_TYPE_FLAGS_MOHAWK },
107	{ PCMCIA_CARD_D(INTEL, EEPRO100, 0),     0x43, XE_CARD_TYPE_FLAGS_MOHAWK },
108	{ PCMCIA_CARD_D(XIRCOM, CE, 0),          0x41, XE_CARD_TYPE_FLAGS_NO },
109	{ PCMCIA_CARD_D(XIRCOM, CE2, 0),         0x41, XE_CARD_TYPE_FLAGS_CE2 },
110	{ PCMCIA_CARD_D(XIRCOM, CE2, 0),         0x42, XE_CARD_TYPE_FLAGS_CE2 },
111	{ PCMCIA_CARD_D(XIRCOM, CE2_2, 0),       0x41, XE_CARD_TYPE_FLAGS_CE2 },
112	{ PCMCIA_CARD_D(XIRCOM, CE2_2, 0),       0x42, XE_CARD_TYPE_FLAGS_CE2 },
113	{ PCMCIA_CARD_D(XIRCOM, CE3, 0),         0x43, XE_CARD_TYPE_FLAGS_MOHAWK },
114	{ PCMCIA_CARD_D(XIRCOM, CEM, 0),         0x41, XE_CARD_TYPE_FLAGS_NO },
115	{ PCMCIA_CARD_D(XIRCOM, CEM2, 0),        0x42, XE_CARD_TYPE_FLAGS_CE2 },
116	{ PCMCIA_CARD_D(XIRCOM, CEM28, 0),       0x43, XE_CARD_TYPE_FLAGS_CE2 },
117	{ PCMCIA_CARD_D(XIRCOM, CEM33, 0),       0x44, XE_CARD_TYPE_FLAGS_CE2 },
118	{ PCMCIA_CARD_D(XIRCOM, CEM33_2, 0),     0x44, XE_CARD_TYPE_FLAGS_CE2 },
119	{ PCMCIA_CARD_D(XIRCOM, CEM56, 0),       0x45, XE_CARD_TYPE_FLAGS_DINGO },
120	{ PCMCIA_CARD_D(XIRCOM, CEM56_2, 0),     0x46, XE_CARD_TYPE_FLAGS_DINGO },
121	{ PCMCIA_CARD_D(XIRCOM, REM56, 0),       0x46, XE_CARD_TYPE_FLAGS_DINGO },
122	{ PCMCIA_CARD_D(XIRCOM, REM10, 0),       0x47, XE_CARD_TYPE_FLAGS_DINGO },
123	{ PCMCIA_CARD_D(XIRCOM, XEM5600, 0),     0x56, XE_CARD_TYPE_FLAGS_DINGO },
124	{ { NULL }, 0, 0 }
125};
126
127
128/*
129 * Fixing for CEM2, CEM3 and CEM56/REM56 cards.  These need some magic to
130 * enable the Ethernet function, which isn't mentioned anywhere in the CIS.
131 * Despite the register names, most of this isn't Dingo-specific.
132 */
133static int
134xe_cemfix(device_t dev)
135{
136	struct xe_softc *sc = (struct xe_softc *) device_get_softc(dev);
137	bus_space_tag_t bst;
138	bus_space_handle_t bsh;
139	struct resource *r;
140	int rid;
141	int ioport;
142
143	DEVPRINTF(2, (dev, "cemfix\n"));
144
145	DEVPRINTF(1, (dev, "CEM I/O port 0x%0lx, size 0x%0lx\n",
146		bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
147		bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid)));
148
149	rid = 0;
150	r = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 0,
151			       ~0, 4 << 10, RF_ACTIVE);
152	if (!r) {
153		device_printf(dev, "cemfix: Can't map in attribute memory\n");
154		return (-1);
155	}
156
157	bsh = rman_get_bushandle(r);
158	bst = rman_get_bustag(r);
159
160	CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY, rid,
161			   PCCARD_A_MEM_ATTR);
162
163	bus_space_write_1(bst, bsh, DINGO_ECOR, DINGO_ECOR_IRQ_LEVEL |
164						DINGO_ECOR_INT_ENABLE |
165						DINGO_ECOR_IOB_ENABLE |
166						DINGO_ECOR_ETH_ENABLE);
167	ioport = bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid);
168	bus_space_write_1(bst, bsh, DINGO_EBAR0, ioport & 0xff);
169	bus_space_write_1(bst, bsh, DINGO_EBAR1, (ioport >> 8) & 0xff);
170
171	if (sc->dingo) {
172		bus_space_write_1(bst, bsh, DINGO_DCOR0, DINGO_DCOR0_SF_INT);
173		bus_space_write_1(bst, bsh, DINGO_DCOR1, DINGO_DCOR1_INT_LEVEL |
174						  DINGO_DCOR1_EEDIO);
175		bus_space_write_1(bst, bsh, DINGO_DCOR2, 0x00);
176		bus_space_write_1(bst, bsh, DINGO_DCOR3, 0x00);
177		bus_space_write_1(bst, bsh, DINGO_DCOR4, 0x00);
178	}
179
180	bus_release_resource(dev, SYS_RES_MEMORY, rid, r);
181
182	/* success! */
183	return (0);
184}
185
186/*
187 * Fixing for CE2-class cards with bogus CIS entry for MAC address.  This
188 * should be in a type 0x22 tuple, but some cards seem to use 0x89.
189 * This function looks for a sensible MAC address tuple starting at the given
190 * offset in attribute memory, ignoring the tuple type field.
191 */
192static int
193xe_macfix(device_t dev, int offset)
194{
195	struct xe_softc *sc = (struct xe_softc *) device_get_softc(dev);
196	bus_space_tag_t bst;
197	bus_space_handle_t bsh;
198	struct resource *r;
199	int rid, i;
200	u_int8_t cisdata[9];
201	u_int8_t required[6] = { 0x08, PCCARD_TPLFE_TYPE_LAN_NID, ETHER_ADDR_LEN,
202				 XE_MAC_ADDR_0, XE_MAC_ADDR_1, XE_MAC_ADDR_2 };
203
204	DEVPRINTF(2, (dev, "macfix\n"));
205
206	rid = 0;
207	r = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 0,
208			       ~0, 4 << 10, RF_ACTIVE);
209	if (!r) {
210		device_printf(dev, "macfix: Can't map in attribute memory\n");
211		return (-1);
212	}
213
214	bsh = rman_get_bushandle(r);
215	bst = rman_get_bustag(r);
216
217	CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY, rid,
218			   PCCARD_A_MEM_ATTR);
219
220	/*
221	 * Looking for (relative to offset):
222	 *
223	 *  0x00	0x??	Tuple type (ignored)
224	 *  0x02 	0x08	Tuple length (must be 8)
225	 *  0x04	0x04	Address type? (must be 4)
226	 *  0x06	0x06	Address length (must be 6)
227	 *  0x08	0x00	Manufacturer ID, byte 1
228	 *  0x0a	0x80	Manufacturer ID, byte 2
229	 *  0x0c	0xc7	Manufacturer ID, byte 3
230	 *  0x0e	0x??	Card ID, byte 1
231	 *  0x10	0x??	Card ID, byte 2
232	 *  0x12	0x??	Card ID, byte 3
233	 */
234	for (i = 0; i < 9; i++) {
235		cisdata[i] = bus_space_read_1(bst, bsh, offset + (2 * i) + 2);
236		if (i < 6 && required[i] != cisdata[i]) {
237			device_printf(dev, "macfix: Can't find valid MAC address\n");
238			bus_release_resource(dev, SYS_RES_MEMORY, rid, r);
239			return (-1);
240		}
241	}
242
243	for (i = 0; i < ETHER_ADDR_LEN; i++) {
244		sc->arpcom.ac_enaddr[i] = cisdata[i + 3];
245	}
246
247	bus_release_resource(dev, SYS_RES_MEMORY, rid, r);
248
249	/* success! */
250	return (0);
251}
252
253/*
254 * PCMCIA probe routine.
255 * Identify the device.  Called from the bus driver when the card is
256 * inserted or otherwise powers up.
257 */
258static int
259xe_pccard_probe(device_t dev)
260{
261	struct xe_softc *scp = (struct xe_softc *) device_get_softc(dev);
262	u_int32_t vendor,product;
263	u_int16_t prodext;
264	const char* vendor_str = NULL;
265	const char* product_str = NULL;
266	const char* cis4_str = NULL;
267	const char *cis3_str=NULL;
268	const struct xe_pccard_product *xpp;
269
270	DEVPRINTF(2, (dev, "pccard_probe\n"));
271
272	pccard_get_vendor(dev, &vendor);
273	pccard_get_product(dev, &product);
274	pccard_get_prodext(dev, &prodext);
275	pccard_get_vendor_str(dev, &vendor_str);
276	pccard_get_product_str(dev, &product_str);
277	pccard_get_cis3_str(dev, &cis3_str);
278	pccard_get_cis4_str(dev, &cis4_str);
279
280	DEVPRINTF(1, (dev, "vendor = 0x%04x\n", vendor));
281	DEVPRINTF(1, (dev, "product = 0x%04x\n", product));
282	DEVPRINTF(1, (dev, "prodext = 0x%02x\n", prodext));
283	DEVPRINTF(1, (dev, "vendor_str = %s\n", vendor_str));
284	DEVPRINTF(1, (dev, "product_str = %s\n", product_str));
285	DEVPRINTF(1, (dev, "cis3_str = %s\n", cis3_str));
286	DEVPRINTF(1, (dev, "cis4_str = %s\n", cis4_str));
287
288
289	/*
290	 * Possibly already did this search in xe_pccard_match(),
291	 * but we need to do it here anyway to figure out which
292	 * card we have.
293	 */
294	for (xpp = xe_pccard_products; xpp->product.pp_vendor != 0; xpp++) {
295		if (vendor == xpp->product.pp_vendor &&
296		    product == xpp->product.pp_product &&
297		    prodext == xpp->prodext)
298			break;
299	}
300
301	/* Found a match? */
302	if (xpp->product.pp_vendor == 0)
303		return (ENODEV);
304
305
306	/* Set card name for logging later */
307	if (xpp->product.pp_name != NULL)
308		device_set_desc(dev, xpp->product.pp_name);
309
310	/* Reject known but unsupported cards */
311	if (xpp->flags & XE_CARD_TYPE_FLAGS_NO) {
312		device_printf(dev, "Sorry, your %s %s card is not supported :(\n",
313				vendor_str, product_str);
314		return (ENODEV);
315	}
316
317	/* Set various card ID fields in softc */
318	scp->vendor = vendor_str;
319	scp->card_type = product_str;
320	if (xpp->flags & XE_CARD_TYPE_FLAGS_CE2)
321		scp->ce2 = 1;
322	if (xpp->flags & XE_CARD_TYPE_FLAGS_MOHAWK)
323		scp->mohawk = 1;
324	if (xpp->flags & XE_CARD_TYPE_FLAGS_DINGO) {
325		scp->dingo = 1;
326		scp->mohawk = 1;
327	}
328	if (xpp->product.pp_product & XE_PROD_MODEM_MASK)
329		scp->modem = 1;
330
331	/* Get MAC address */
332	pccard_get_ether(dev, scp->arpcom.ac_enaddr);
333
334	/* Deal with bogus MAC address */
335	if (xpp->product.pp_vendor == PCMCIA_VENDOR_XIRCOM
336	    && scp->ce2
337	    && (scp->arpcom.ac_enaddr[0] != XE_MAC_ADDR_0
338		|| scp->arpcom.ac_enaddr[1] != XE_MAC_ADDR_1
339		|| scp->arpcom.ac_enaddr[2] != XE_MAC_ADDR_2)
340	    && xe_macfix(dev, XE_BOGUS_MAC_OFFSET) < 0) {
341		device_printf(dev,
342			      "Unable to find MAC address for your %s card\n",
343			      scp->card_type);
344		return (ENODEV);
345	}
346
347	/* Success */
348	return (0);
349}
350
351/*
352 * Attach a device.
353 */
354static int
355xe_pccard_attach(device_t dev)
356{
357	struct xe_softc *scp = device_get_softc(dev);
358	int err;
359
360	DEVPRINTF(2, (dev, "pccard_attach\n"));
361
362	if ((err = xe_activate(dev)) != 0)
363		return (err);
364
365	/* Hack RealPorts into submission */
366	if (scp->modem && xe_cemfix(dev) < 0) {
367		device_printf(dev, "Unable to fix your %s %s combo card\n",
368					  scp->vendor, scp->card_type);
369		xe_deactivate(dev);
370		return (ENODEV);
371	}
372	if ((err = xe_attach(dev))) {
373		device_printf(dev, "xe_attach() failed! (%d)\n", err);
374		return (err);
375	}
376	return (0);
377}
378
379/*
380 * The device entry is being removed, probably because someone ejected the
381 * card.  The interface should have been brought down manually before calling
382 * this function; if not you may well lose packets.  In any case, I shut down
383 * the card and the interface, and hope for the best.
384 */
385static int
386xe_pccard_detach(device_t dev)
387{
388	struct xe_softc *sc = device_get_softc(dev);
389
390	DEVPRINTF(2, (dev, "pccard_detach\n"));
391
392	sc->arpcom.ac_if.if_flags &= ~IFF_RUNNING;
393	ether_ifdetach(&sc->arpcom.ac_if);
394	xe_deactivate(dev);
395	return (0);
396}
397
398static int
399xe_pccard_product_match(device_t dev, const struct pccard_product* ent, int vpfmatch)
400{
401	const struct xe_pccard_product* xpp;
402	u_int16_t prodext;
403
404	DEVPRINTF(2, (dev, "pccard_product_match\n"));
405
406	xpp = (const struct xe_pccard_product*)ent;
407	pccard_get_prodext(dev, &prodext);
408
409	if (xpp->prodext != prodext)
410		vpfmatch = 0;
411
412	return (vpfmatch);
413}
414
415static int
416xe_pccard_match(device_t dev)
417{
418	const struct pccard_product *pp;
419
420	DEVPRINTF(2, (dev, "pccard_match\n"));
421
422	pp = (const struct pccard_product*)xe_pccard_products;
423
424	if ((pp = pccard_product_lookup(dev, pp,
425	     sizeof(xe_pccard_products[0]), xe_pccard_product_match)) != NULL)
426		return (0);
427
428	return (EIO);
429}
430
431static device_method_t xe_pccard_methods[] = {
432        /* Device interface */
433        DEVMETHOD(device_probe,         pccard_compat_probe),
434        DEVMETHOD(device_attach,        pccard_compat_attach),
435        DEVMETHOD(device_detach,        xe_pccard_detach),
436
437        /* Card interface */
438        DEVMETHOD(card_compat_match,    xe_pccard_match),
439        DEVMETHOD(card_compat_probe,    xe_pccard_probe),
440        DEVMETHOD(card_compat_attach,   xe_pccard_attach),
441
442        { 0, 0 }
443};
444
445static driver_t xe_pccard_driver = {
446        "xe",
447        xe_pccard_methods,
448        sizeof(struct xe_softc),
449};
450
451devclass_t xe_devclass;
452
453DRIVER_MODULE(xe, pccard, xe_pccard_driver, xe_devclass, 0, 0);
454