if_xe_pccard.c revision 179551
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 179551 2008-06-04 20:26:57Z jhb $");
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/module.h>
36#include <sys/socket.h>
37#include <sys/systm.h>
38
39#include <machine/bus.h>
40#include <machine/resource.h>
41#include <sys/rman.h>
42
43#include <net/ethernet.h>
44#include <net/if.h>
45#include <net/if_arp.h>
46#include <net/if_media.h>
47#include <net/if_mib.h>
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
97struct xe_pccard_product {
98	struct pccard_product product;
99	uint16_t	prodext;
100	uint16_t	flags;
101};
102
103static const struct xe_pccard_product xe_pccard_products[] = {
104	{ PCMCIA_CARD_D(COMPAQ, CPQ550),      0x43, XE_CARD_TYPE_FLAGS_CE2 },
105	{ PCMCIA_CARD_D(COMPAQ2, CPQ_10_100), 0x43, XE_CARD_TYPE_FLAGS_MOHAWK },
106	{ PCMCIA_CARD_D(INTEL, EEPRO100),     0x43, XE_CARD_TYPE_FLAGS_MOHAWK },
107	{ PCMCIA_CARD_D(INTEL, PRO100LAN56),  0x46, XE_CARD_TYPE_FLAGS_DINGO },
108	{ PCMCIA_CARD_D(RACORE, ACCTON_EN2226),0x43, XE_CARD_TYPE_FLAGS_MOHAWK },
109	{ PCMCIA_CARD_D(XIRCOM, CE),          0x41, XE_CARD_TYPE_FLAGS_NO },
110	{ PCMCIA_CARD_D(XIRCOM, CE2),         0x41, XE_CARD_TYPE_FLAGS_CE2 },
111	{ PCMCIA_CARD_D(XIRCOM, CE2),         0x42, XE_CARD_TYPE_FLAGS_CE2 },
112	{ PCMCIA_CARD_D(XIRCOM, CE2_2),       0x41, XE_CARD_TYPE_FLAGS_CE2 },
113	{ PCMCIA_CARD_D(XIRCOM, CE2_2),       0x42, XE_CARD_TYPE_FLAGS_CE2 },
114	{ PCMCIA_CARD_D(XIRCOM, CE3),         0x43, XE_CARD_TYPE_FLAGS_MOHAWK },
115	{ PCMCIA_CARD_D(XIRCOM, CEM),         0x41, XE_CARD_TYPE_FLAGS_NO },
116	{ PCMCIA_CARD_D(XIRCOM, CEM2),        0x42, XE_CARD_TYPE_FLAGS_CE2 },
117	{ PCMCIA_CARD_D(XIRCOM, CEM28),       0x43, XE_CARD_TYPE_FLAGS_CE2 },
118	{ PCMCIA_CARD_D(XIRCOM, CEM33),       0x44, XE_CARD_TYPE_FLAGS_CE2 },
119	{ PCMCIA_CARD_D(XIRCOM, CEM33_2),     0x44, XE_CARD_TYPE_FLAGS_CE2 },
120	{ PCMCIA_CARD_D(XIRCOM, CEM56),       0x45, XE_CARD_TYPE_FLAGS_DINGO },
121	{ PCMCIA_CARD_D(XIRCOM, CEM56_2),     0x46, XE_CARD_TYPE_FLAGS_DINGO },
122	{ PCMCIA_CARD_D(XIRCOM, REM56),       0x46, XE_CARD_TYPE_FLAGS_DINGO },
123	{ PCMCIA_CARD_D(XIRCOM, REM10),       0x47, XE_CARD_TYPE_FLAGS_DINGO },
124	{ PCMCIA_CARD_D(XIRCOM, XEM5600),     0x56, XE_CARD_TYPE_FLAGS_DINGO },
125	{ { NULL }, 0, 0 }
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 = device_get_softc(dev);
137	int ioport;
138
139	DEVPRINTF(2, (dev, "cemfix\n"));
140
141	DEVPRINTF(1, (dev, "CEM I/O port 0x%0lx, size 0x%0lx\n",
142		bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
143		bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid)));
144
145	pccard_attr_write_1(dev, DINGO_ECOR, DINGO_ECOR_IRQ_LEVEL |
146	    DINGO_ECOR_INT_ENABLE | DINGO_ECOR_IOB_ENABLE |
147	    DINGO_ECOR_ETH_ENABLE);
148	ioport = bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid);
149	pccard_attr_write_1(dev, DINGO_EBAR0, ioport & 0xff);
150	pccard_attr_write_1(dev, DINGO_EBAR1, (ioport >> 8) & 0xff);
151
152	if (sc->dingo) {
153		pccard_attr_write_1(dev, DINGO_DCOR0, DINGO_DCOR0_SF_INT);
154		pccard_attr_write_1(dev, DINGO_DCOR1, DINGO_DCOR1_INT_LEVEL |
155		    DINGO_DCOR1_EEDIO);
156		pccard_attr_write_1(dev, DINGO_DCOR2, 0x00);
157		pccard_attr_write_1(dev, DINGO_DCOR3, 0x00);
158		pccard_attr_write_1(dev, DINGO_DCOR4, 0x00);
159	}
160	/* success! */
161	return (0);
162}
163
164static int
165xe_pccard_product_match(device_t dev, const struct pccard_product* ent,
166    int vpfmatch)
167{
168	const struct xe_pccard_product* xpp;
169	uint16_t prodext;
170
171	if (vpfmatch == 0)
172		return (0);
173
174	xpp = (const struct xe_pccard_product*)ent;
175	pccard_get_prodext(dev, &prodext);
176	if (xpp->prodext != prodext)
177		vpfmatch = 0;
178	else
179		vpfmatch++;
180	return (vpfmatch);
181}
182
183static const struct xe_pccard_product *
184xe_pccard_get_product(device_t dev)
185{
186	return ((const struct xe_pccard_product *)pccard_product_lookup(dev,
187	    (const struct pccard_product *)xe_pccard_products,
188	    sizeof(xe_pccard_products[0]), xe_pccard_product_match));
189}
190
191/*
192 * Fixing for CE2-class cards with bogus CIS entry for MAC address.
193 */
194static int
195xe_pccard_mac(const struct pccard_tuple *tuple, void *argp)
196{
197	uint8_t *enaddr = argp, test;
198	int i;
199
200	/* Code 0x89 is Xircom special cis node contianing the MAC */
201	if (tuple->code != 0x89)
202		return (0);
203
204	/* Make sure this is a sane node */
205	if (tuple->length != ETHER_ADDR_LEN + 2)
206		return (0);
207	test = pccard_tuple_read_1(tuple, 0);
208	if (test != PCCARD_TPLFE_TYPE_LAN_NID)
209		return (0);
210	test = pccard_tuple_read_1(tuple, 1);
211	if (test != ETHER_ADDR_LEN)
212		return (0);
213
214	/* Copy the MAC ADDR and return success */
215	for (i = 0; i < ETHER_ADDR_LEN; i++)
216		enaddr[i] = pccard_tuple_read_1(tuple, i + 2);
217	return (1);
218}
219
220static int
221xe_bad_mac(uint8_t *enaddr)
222{
223	int i;
224	uint8_t sum;
225
226	for (i = 0, sum = 0; i < ETHER_ADDR_LEN; i++)
227		sum |= enaddr[i];
228	return (sum == 0);
229}
230
231/*
232 * PCMCIA attach routine.
233 * Identify the device.  Called from the bus driver when the card is
234 * inserted or otherwise powers up.
235 */
236static int
237xe_pccard_attach(device_t dev)
238{
239	struct xe_softc *scp = device_get_softc(dev);
240	uint32_t vendor,product;
241	uint16_t prodext;
242	const char* vendor_str = NULL;
243	const char* product_str = NULL;
244	const char* cis4_str = NULL;
245	const char *cis3_str=NULL;
246	const struct xe_pccard_product *xpp;
247	int err;
248
249	DEVPRINTF(2, (dev, "pccard_attach\n"));
250
251	pccard_get_vendor(dev, &vendor);
252	pccard_get_product(dev, &product);
253	pccard_get_prodext(dev, &prodext);
254	pccard_get_vendor_str(dev, &vendor_str);
255	pccard_get_product_str(dev, &product_str);
256	pccard_get_cis3_str(dev, &cis3_str);
257	pccard_get_cis4_str(dev, &cis4_str);
258
259	DEVPRINTF(1, (dev, "vendor = 0x%04x\n", vendor));
260	DEVPRINTF(1, (dev, "product = 0x%04x\n", product));
261	DEVPRINTF(1, (dev, "prodext = 0x%02x\n", prodext));
262	DEVPRINTF(1, (dev, "vendor_str = %s\n", vendor_str));
263	DEVPRINTF(1, (dev, "product_str = %s\n", product_str));
264	DEVPRINTF(1, (dev, "cis3_str = %s\n", cis3_str));
265	DEVPRINTF(1, (dev, "cis4_str = %s\n", cis4_str));
266
267	xpp = xe_pccard_get_product(dev);
268	if (xpp == NULL)
269		return (ENXIO);
270
271	/* Set various card ID fields in softc */
272	scp->vendor = vendor_str;
273	scp->card_type = product_str;
274	if (xpp->flags & XE_CARD_TYPE_FLAGS_CE2)
275		scp->ce2 = 1;
276	if (xpp->flags & XE_CARD_TYPE_FLAGS_MOHAWK)
277		scp->mohawk = 1;
278	if (xpp->flags & XE_CARD_TYPE_FLAGS_DINGO) {
279		scp->dingo = 1;
280		scp->mohawk = 1;
281	}
282	if (xpp->product.pp_product & XE_PROD_MODEM_MASK)
283		scp->modem = 1;
284
285	/* Get MAC address */
286	pccard_get_ether(dev, scp->enaddr);
287
288	/* Deal with bogus MAC address */
289	if (xe_bad_mac(scp->enaddr) &&
290	    !pccard_cis_scan(dev, xe_pccard_mac, scp->enaddr)) {
291		device_printf(dev,
292		    "Unable to find MAC address for your %s card\n",
293		    device_get_desc(dev));
294		return (ENXIO);
295	}
296
297	if ((err = xe_activate(dev)) != 0)
298		return (err);
299
300	/* Hack RealPorts into submission */
301	if (scp->modem && xe_cemfix(dev) < 0) {
302		device_printf(dev, "Unable to fix your %s combo card\n",
303		    device_get_desc(dev));
304		xe_deactivate(dev);
305		return (ENXIO);
306	}
307	if ((err = xe_attach(dev))) {
308		device_printf(dev, "xe_attach() failed! (%d)\n", err);
309		xe_deactivate(dev);
310		return (err);
311	}
312	return (0);
313}
314
315/*
316 * The device entry is being removed, probably because someone ejected the
317 * card.  The interface should have been brought down manually before calling
318 * this function; if not you may well lose packets.  In any case, I shut down
319 * the card and the interface, and hope for the best.
320 */
321static int
322xe_pccard_detach(device_t dev)
323{
324	struct xe_softc *sc = device_get_softc(dev);
325
326	DEVPRINTF(2, (dev, "pccard_detach\n"));
327
328	XE_LOCK(sc);
329	xe_stop(sc);
330	XE_UNLOCK(sc);
331	callout_drain(&sc->media_timer);
332	callout_drain(&sc->wdog_timer);
333	ether_ifdetach(sc->ifp);
334	xe_deactivate(dev);
335	mtx_destroy(&sc->lock);
336	return (0);
337}
338
339static int
340xe_pccard_probe(device_t dev)
341{
342	const struct xe_pccard_product *xpp;
343
344	DEVPRINTF(2, (dev, "pccard_probe\n"));
345
346	/*
347	 * Xircom cards aren't proper MFC cards, so we have to take all
348	 * cards that match, not just ones that are network.
349	 */
350
351	/* If we match something in the table, it is our device. */
352	if ((xpp = xe_pccard_get_product(dev)) == NULL)
353		return (ENXIO);
354
355	/* Set card name for logging later */
356	if (xpp->product.pp_name != NULL)
357		device_set_desc(dev, xpp->product.pp_name);
358
359	/* Reject known but unsupported cards */
360	if (xpp->flags & XE_CARD_TYPE_FLAGS_NO) {
361		device_printf(dev, "Sorry, your %s card is not supported :(\n",
362		    device_get_desc(dev));
363		return (ENXIO);
364	}
365
366	return (0);
367}
368
369static device_method_t xe_pccard_methods[] = {
370        /* Device interface */
371        DEVMETHOD(device_probe,         xe_pccard_probe),
372        DEVMETHOD(device_attach,        xe_pccard_attach),
373        DEVMETHOD(device_detach,        xe_pccard_detach),
374
375        { 0, 0 }
376};
377
378static driver_t xe_pccard_driver = {
379        "xe",
380        xe_pccard_methods,
381        sizeof(struct xe_softc),
382};
383
384devclass_t xe_devclass;
385
386DRIVER_MODULE(xe, pccard, xe_pccard_driver, xe_devclass, 0, 0);
387