1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2002 Takeshi Shibagaki
5  * 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD: stable/12/sys/dev/xe/if_xe_pccard.c 326255 2017-11-27 14:52:40Z pfg $");
31 
32 /* xe pccard interface driver */
33 
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/socket.h>
41 #include <sys/systm.h>
42 
43 #include <machine/bus.h>
44 #include <machine/resource.h>
45 #include <sys/rman.h>
46 
47 #include <net/ethernet.h>
48 #include <net/if.h>
49 #include <net/if_arp.h>
50 #include <net/if_media.h>
51 #include <net/if_mib.h>
52 
53 #include <dev/xe/if_xereg.h>
54 #include <dev/xe/if_xevar.h>
55 
56 #include <dev/pccard/pccardvar.h>
57 #include <dev/pccard/pccard_cis.h>
58 
59 #include "card_if.h"
60 #include "pccarddevs.h"
61 
62 /*
63  * Debug logging levels - set with hw.xe.debug sysctl
64  * 0 = None
65  * 1 = More hardware details, probe/attach progress
66  * 2 = Most function calls, ioctls and media selection progress
67  * 3 = Everything - interrupts, packets in/out and multicast address setup
68  */
69 #define XE_DEBUG
70 #ifdef XE_DEBUG
71 
72 extern int xe_debug;
73 
74 #define	DEVPRINTF(level, arg)	if (xe_debug >= (level)) device_printf arg
75 #define	DPRINTF(level, arg)	if (xe_debug >= (level)) printf arg
76 #else
77 #define	DEVPRINTF(level, arg)
78 #define	DPRINTF(level, arg)
79 #endif
80 
81 
82 #define	XE_CARD_TYPE_FLAGS_NO		0x0
83 #define	XE_CARD_TYPE_FLAGS_CE2		0x1
84 #define	XE_CARD_TYPE_FLAGS_MOHAWK	0x2
85 #define	XE_CARD_TYPE_FLAGS_DINGO	0x4
86 #define	XE_PROD_ETHER_MASK		0x0100
87 #define	XE_PROD_MODEM_MASK		0x1000
88 
89 #define	XE_BOGUS_MAC_OFFSET		0x90
90 
91 /* MAC vendor prefix used by most Xircom cards is 00:80:c7 */
92 #define	XE_MAC_ADDR_0			0x00
93 #define	XE_MAC_ADDR_1			0x80
94 #define	XE_MAC_ADDR_2			0xc7
95 
96 /* Some (all?) REM56 cards have vendor prefix 00:10:a4 */
97 #define	XE_REM56_MAC_ADDR_0		0x00
98 #define	XE_REM56_MAC_ADDR_1		0x10
99 #define	XE_REM56_MAC_ADDR_2		0xa4
100 
101 struct xe_pccard_product {
102 	struct pccard_product product;
103 	uint16_t	prodext;
104 	uint16_t	flags;
105 };
106 
107 static const struct xe_pccard_product xe_pccard_products[] = {
108 	{ PCMCIA_CARD_D(COMPAQ, CPQ550),      0x43, XE_CARD_TYPE_FLAGS_CE2 },
109 	{ PCMCIA_CARD_D(COMPAQ2, CPQ_10_100), 0x43, XE_CARD_TYPE_FLAGS_MOHAWK },
110 	{ PCMCIA_CARD_D(INTEL, EEPRO100),     0x43, XE_CARD_TYPE_FLAGS_MOHAWK },
111 	{ PCMCIA_CARD_D(INTEL, PRO100LAN56),  0x46, XE_CARD_TYPE_FLAGS_DINGO },
112 	{ PCMCIA_CARD_D(RACORE, ACCTON_EN2226),0x43, XE_CARD_TYPE_FLAGS_MOHAWK },
113 	{ PCMCIA_CARD_D(XIRCOM, CE),          0x41, XE_CARD_TYPE_FLAGS_NO },
114 	{ PCMCIA_CARD_D(XIRCOM, CE2),         0x41, XE_CARD_TYPE_FLAGS_CE2 },
115 	{ PCMCIA_CARD_D(XIRCOM, CE2),         0x42, XE_CARD_TYPE_FLAGS_CE2 },
116 	{ PCMCIA_CARD_D(XIRCOM, CE2_2),       0x41, XE_CARD_TYPE_FLAGS_CE2 },
117 	{ PCMCIA_CARD_D(XIRCOM, CE2_2),       0x42, XE_CARD_TYPE_FLAGS_CE2 },
118 	{ PCMCIA_CARD_D(XIRCOM, CE3),         0x43, XE_CARD_TYPE_FLAGS_MOHAWK },
119 	{ PCMCIA_CARD_D(XIRCOM, CEM),         0x41, XE_CARD_TYPE_FLAGS_NO },
120 	{ PCMCIA_CARD_D(XIRCOM, CEM2),        0x42, XE_CARD_TYPE_FLAGS_CE2 },
121 	{ PCMCIA_CARD_D(XIRCOM, CEM28),       0x43, XE_CARD_TYPE_FLAGS_CE2 },
122 	{ PCMCIA_CARD_D(XIRCOM, CEM33),       0x44, XE_CARD_TYPE_FLAGS_CE2 },
123 	{ PCMCIA_CARD_D(XIRCOM, CEM33_2),     0x44, XE_CARD_TYPE_FLAGS_CE2 },
124 	{ PCMCIA_CARD_D(XIRCOM, CEM56),       0x45, XE_CARD_TYPE_FLAGS_DINGO },
125 	{ PCMCIA_CARD_D(XIRCOM, CEM56_2),     0x46, XE_CARD_TYPE_FLAGS_DINGO },
126 	{ PCMCIA_CARD_D(XIRCOM, REM56),       0x46, XE_CARD_TYPE_FLAGS_DINGO },
127 	{ PCMCIA_CARD_D(XIRCOM, REM10),       0x47, XE_CARD_TYPE_FLAGS_DINGO },
128 	{ PCMCIA_CARD_D(XIRCOM, XEM5600),     0x56, XE_CARD_TYPE_FLAGS_DINGO },
129 	{ { NULL }, 0, 0 }
130 };
131 
132 /*
133  * Fixing for CEM2, CEM3 and CEM56/REM56 cards.  These need some magic to
134  * enable the Ethernet function, which isn't mentioned anywhere in the CIS.
135  * Despite the register names, most of this isn't Dingo-specific.
136  */
137 static int
xe_cemfix(device_t dev)138 xe_cemfix(device_t dev)
139 {
140 	struct xe_softc *sc = device_get_softc(dev);
141 	int ioport;
142 
143 	DEVPRINTF(2, (dev, "cemfix\n"));
144 
145 	DEVPRINTF(1, (dev, "CEM I/O port 0x%0jx, size 0x%0jx\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 	pccard_attr_write_1(dev, DINGO_ECOR, DINGO_ECOR_IRQ_LEVEL |
150 	    DINGO_ECOR_INT_ENABLE | DINGO_ECOR_IOB_ENABLE |
151 	    DINGO_ECOR_ETH_ENABLE);
152 	ioport = bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid);
153 	pccard_attr_write_1(dev, DINGO_EBAR0, ioport & 0xff);
154 	pccard_attr_write_1(dev, DINGO_EBAR1, (ioport >> 8) & 0xff);
155 
156 	if (sc->dingo) {
157 		pccard_attr_write_1(dev, DINGO_DCOR0, DINGO_DCOR0_SF_INT);
158 		pccard_attr_write_1(dev, DINGO_DCOR1, DINGO_DCOR1_INT_LEVEL |
159 		    DINGO_DCOR1_EEDIO);
160 		pccard_attr_write_1(dev, DINGO_DCOR2, 0x00);
161 		pccard_attr_write_1(dev, DINGO_DCOR3, 0x00);
162 		pccard_attr_write_1(dev, DINGO_DCOR4, 0x00);
163 	}
164 	/* success! */
165 	return (0);
166 }
167 
168 static int
xe_pccard_product_match(device_t dev,const struct pccard_product * ent,int vpfmatch)169 xe_pccard_product_match(device_t dev, const struct pccard_product* ent,
170     int vpfmatch)
171 {
172 	const struct xe_pccard_product* xpp;
173 	uint16_t prodext;
174 
175 	if (vpfmatch == 0)
176 		return (0);
177 
178 	xpp = (const struct xe_pccard_product*)ent;
179 	pccard_get_prodext(dev, &prodext);
180 	if (xpp->prodext != prodext)
181 		vpfmatch = 0;
182 	else
183 		vpfmatch++;
184 	return (vpfmatch);
185 }
186 
187 static const struct xe_pccard_product *
xe_pccard_get_product(device_t dev)188 xe_pccard_get_product(device_t dev)
189 {
190 	return ((const struct xe_pccard_product *)pccard_product_lookup(dev,
191 	    (const struct pccard_product *)xe_pccard_products,
192 	    sizeof(xe_pccard_products[0]), xe_pccard_product_match));
193 }
194 
195 /*
196  * Fixing for CE2-class cards with bogus CIS entry for MAC address.
197  */
198 static int
xe_pccard_mac(const struct pccard_tuple * tuple,void * argp)199 xe_pccard_mac(const struct pccard_tuple *tuple, void *argp)
200 {
201 	uint8_t *enaddr = argp, test;
202 	int i;
203 
204 	/* Code 0x89 is Xircom special cis node contianing the MAC */
205 	if (tuple->code != 0x89)
206 		return (0);
207 
208 	/* Make sure this is a sane node */
209 	if (tuple->length != ETHER_ADDR_LEN + 2)
210 		return (0);
211 	test = pccard_tuple_read_1(tuple, 0);
212 	if (test != PCCARD_TPLFE_TYPE_LAN_NID)
213 		return (0);
214 	test = pccard_tuple_read_1(tuple, 1);
215 	if (test != ETHER_ADDR_LEN)
216 		return (0);
217 
218 	/* Copy the MAC ADDR and return success */
219 	for (i = 0; i < ETHER_ADDR_LEN; i++)
220 		enaddr[i] = pccard_tuple_read_1(tuple, i + 2);
221 	return (1);
222 }
223 
224 static int
xe_bad_mac(uint8_t * enaddr)225 xe_bad_mac(uint8_t *enaddr)
226 {
227 	int i;
228 	uint8_t sum;
229 
230 	for (i = 0, sum = 0; i < ETHER_ADDR_LEN; i++)
231 		sum |= enaddr[i];
232 	return (sum == 0);
233 }
234 
235 /*
236  * PCMCIA attach routine.
237  * Identify the device.  Called from the bus driver when the card is
238  * inserted or otherwise powers up.
239  */
240 static int
xe_pccard_attach(device_t dev)241 xe_pccard_attach(device_t dev)
242 {
243 	struct xe_softc *scp = device_get_softc(dev);
244 	uint32_t vendor,product;
245 	uint16_t prodext;
246 	const char* vendor_str = NULL;
247 	const char* product_str = NULL;
248 	const char* cis4_str = NULL;
249 	const char *cis3_str=NULL;
250 	const struct xe_pccard_product *xpp;
251 	int err;
252 
253 	DEVPRINTF(2, (dev, "pccard_attach\n"));
254 
255 	pccard_get_vendor(dev, &vendor);
256 	pccard_get_product(dev, &product);
257 	pccard_get_prodext(dev, &prodext);
258 	pccard_get_vendor_str(dev, &vendor_str);
259 	pccard_get_product_str(dev, &product_str);
260 	pccard_get_cis3_str(dev, &cis3_str);
261 	pccard_get_cis4_str(dev, &cis4_str);
262 
263 	DEVPRINTF(1, (dev, "vendor = 0x%04x\n", vendor));
264 	DEVPRINTF(1, (dev, "product = 0x%04x\n", product));
265 	DEVPRINTF(1, (dev, "prodext = 0x%02x\n", prodext));
266 	DEVPRINTF(1, (dev, "vendor_str = %s\n", vendor_str));
267 	DEVPRINTF(1, (dev, "product_str = %s\n", product_str));
268 	DEVPRINTF(1, (dev, "cis3_str = %s\n", cis3_str));
269 	DEVPRINTF(1, (dev, "cis4_str = %s\n", cis4_str));
270 
271 	xpp = xe_pccard_get_product(dev);
272 	if (xpp == NULL)
273 		return (ENXIO);
274 
275 	/* Set various card ID fields in softc */
276 	scp->vendor = vendor_str;
277 	scp->card_type = product_str;
278 	if (xpp->flags & XE_CARD_TYPE_FLAGS_CE2)
279 		scp->ce2 = 1;
280 	if (xpp->flags & XE_CARD_TYPE_FLAGS_MOHAWK)
281 		scp->mohawk = 1;
282 	if (xpp->flags & XE_CARD_TYPE_FLAGS_DINGO) {
283 		scp->dingo = 1;
284 		scp->mohawk = 1;
285 	}
286 	if (xpp->product.pp_product & XE_PROD_MODEM_MASK)
287 		scp->modem = 1;
288 
289 	/* Get MAC address */
290 	pccard_get_ether(dev, scp->enaddr);
291 
292 	/* Deal with bogus MAC address */
293 	if (xe_bad_mac(scp->enaddr) &&
294 	    !pccard_cis_scan(dev, xe_pccard_mac, scp->enaddr)) {
295 		device_printf(dev,
296 		    "Unable to find MAC address for your %s card\n",
297 		    device_get_desc(dev));
298 		return (ENXIO);
299 	}
300 
301 	if ((err = xe_activate(dev)) != 0)
302 		return (err);
303 
304 	/* Hack RealPorts into submission */
305 	if (scp->modem && xe_cemfix(dev) < 0) {
306 		device_printf(dev, "Unable to fix your %s combo card\n",
307 		    device_get_desc(dev));
308 		xe_deactivate(dev);
309 		return (ENXIO);
310 	}
311 	if ((err = xe_attach(dev))) {
312 		device_printf(dev, "xe_attach() failed! (%d)\n", err);
313 		xe_deactivate(dev);
314 		return (err);
315 	}
316 	return (0);
317 }
318 
319 /*
320  * The device entry is being removed, probably because someone ejected the
321  * card.  The interface should have been brought down manually before calling
322  * this function; if not you may well lose packets.  In any case, I shut down
323  * the card and the interface, and hope for the best.
324  */
325 static int
xe_pccard_detach(device_t dev)326 xe_pccard_detach(device_t dev)
327 {
328 	struct xe_softc *sc = device_get_softc(dev);
329 
330 	DEVPRINTF(2, (dev, "pccard_detach\n"));
331 
332 	XE_LOCK(sc);
333 	xe_stop(sc);
334 	XE_UNLOCK(sc);
335 	callout_drain(&sc->media_timer);
336 	callout_drain(&sc->wdog_timer);
337 	ether_ifdetach(sc->ifp);
338 	xe_deactivate(dev);
339 	mtx_destroy(&sc->lock);
340 	return (0);
341 }
342 
343 static int
xe_pccard_probe(device_t dev)344 xe_pccard_probe(device_t dev)
345 {
346 	const struct xe_pccard_product *xpp;
347 
348 	DEVPRINTF(2, (dev, "pccard_probe\n"));
349 
350 	/*
351 	 * Xircom cards aren't proper MFC cards, so we have to take all
352 	 * cards that match, not just ones that are network.
353 	 */
354 
355 	/* If we match something in the table, it is our device. */
356 	if ((xpp = xe_pccard_get_product(dev)) == NULL)
357 		return (ENXIO);
358 
359 	/* Set card name for logging later */
360 	if (xpp->product.pp_name != NULL)
361 		device_set_desc(dev, xpp->product.pp_name);
362 
363 	/* Reject known but unsupported cards */
364 	if (xpp->flags & XE_CARD_TYPE_FLAGS_NO) {
365 		device_printf(dev, "Sorry, your %s card is not supported :(\n",
366 		    device_get_desc(dev));
367 		return (ENXIO);
368 	}
369 
370 	return (0);
371 }
372 
373 static device_method_t xe_pccard_methods[] = {
374         /* Device interface */
375         DEVMETHOD(device_probe,         xe_pccard_probe),
376         DEVMETHOD(device_attach,        xe_pccard_attach),
377         DEVMETHOD(device_detach,        xe_pccard_detach),
378 
379         { 0, 0 }
380 };
381 
382 static driver_t xe_pccard_driver = {
383         "xe",
384         xe_pccard_methods,
385         sizeof(struct xe_softc),
386 };
387 
388 devclass_t xe_devclass;
389 
390 DRIVER_MODULE(xe, pccard, xe_pccard_driver, xe_devclass, 0, 0);
391 PCCARD_PNP_INFO(xe_pccard_products);
392