xref: /openbsd/sys/dev/pcmcia/pcmcia_cis.c (revision a6445c1d)
1 /*	$OpenBSD: pcmcia_cis.c,v 1.20 2014/07/12 18:48:52 tedu Exp $	*/
2 /*	$NetBSD: pcmcia_cis.c,v 1.9 1998/08/22 23:41:48 msaitoh Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Marc Horowitz.  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 Marc Horowitz.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/device.h>
37 #include <sys/malloc.h>
38 
39 #include <dev/pcmcia/pcmciareg.h>
40 #include <dev/pcmcia/pcmciachip.h>
41 #include <dev/pcmcia/pcmciavar.h>
42 
43 #ifdef PCMCIACISDEBUG
44 #define	DPRINTF(arg) printf arg
45 #else
46 #define	DPRINTF(arg)
47 #endif
48 
49 #define	PCMCIA_CIS_SIZE		1024
50 
51 struct cis_state {
52 	int	count;
53 	int	gotmfc;
54 	struct pcmcia_config_entry temp_cfe;
55 	struct pcmcia_config_entry *default_cfe;
56 	struct pcmcia_card *card;
57 	struct pcmcia_function *pf;
58 };
59 
60 int	pcmcia_parse_cis_tuple(struct pcmcia_tuple *, void *);
61 
62 uint8_t
63 pcmcia_cis_read_1(struct pcmcia_tuple *tuple, bus_size_t idx)
64 {
65 	if (tuple->flags & PTF_INDIRECT) {
66 		bus_space_write_1(tuple->memt, tuple->memh,
67 		    tuple->indirect_ptr + PCMCIA_INDR_CONTROL, PCMCIA_ICR_ATTR);
68 		idx <<= tuple->addrshift;
69 		bus_space_write_1(tuple->memt, tuple->memh,
70 		    tuple->indirect_ptr + PCMCIA_INDR_ADDRESS + 0, idx >> 0);
71 		bus_space_write_1(tuple->memt, tuple->memh,
72 		    tuple->indirect_ptr + PCMCIA_INDR_ADDRESS + 1, idx >> 8);
73 		bus_space_write_1(tuple->memt, tuple->memh,
74 		    tuple->indirect_ptr + PCMCIA_INDR_ADDRESS + 2, idx >> 16);
75 		bus_space_write_1(tuple->memt, tuple->memh,
76 		    tuple->indirect_ptr + PCMCIA_INDR_ADDRESS + 3, idx >> 24);
77 		return bus_space_read_1(tuple->memt, tuple->memh,
78 		    tuple->indirect_ptr + PCMCIA_INDR_DATA);
79 	} else
80 		return bus_space_read_1(tuple->memt, tuple->memh,
81 		    idx << tuple->addrshift);
82 }
83 
84 void
85 pcmcia_read_cis(sc)
86 	struct pcmcia_softc *sc;
87 {
88 	struct cis_state state;
89 
90 	memset(&state, 0, sizeof state);
91 
92 	state.card = &sc->card;
93 
94 	state.card->error = 0;
95 	state.card->cis1_major = -1;
96 	state.card->cis1_minor = -1;
97 	state.card->cis1_info[0] = NULL;
98 	state.card->cis1_info[1] = NULL;
99 	state.card->cis1_info[2] = NULL;
100 	state.card->cis1_info[3] = NULL;
101 	state.card->manufacturer = PCMCIA_VENDOR_INVALID;
102 	state.card->product = PCMCIA_PRODUCT_INVALID;
103 	SIMPLEQ_INIT(&state.card->pf_head);
104 
105 	state.pf = NULL;
106 
107 	if (pcmcia_scan_cis((struct device *)sc, pcmcia_parse_cis_tuple,
108 	    &state) == -1)
109 		state.card->error++;
110 }
111 
112 int
113 pcmcia_scan_cis(dev, fct, arg)
114 	struct device *dev;
115 	int (*fct)(struct pcmcia_tuple *, void *);
116 	void *arg;
117 {
118 	struct pcmcia_softc *sc = (struct pcmcia_softc *) dev;
119 	pcmcia_chipset_tag_t pct;
120 	pcmcia_chipset_handle_t pch;
121 	int window;
122 	struct pcmcia_mem_handle pcmh;
123 	struct pcmcia_tuple tuple;
124 	int indirect_present;
125 	int longlink_present;
126 	int longlink_common;
127 	u_long longlink_addr;
128 	int mfc_count;
129 	int mfc_index;
130 	struct {
131 		int	common;
132 		u_long	addr;
133 	} mfc[256 / 5];
134 	int ret;
135 
136 	ret = 0;
137 
138 	pct = sc->pct;
139 	pch = sc->pch;
140 
141 	/* allocate some memory */
142 
143 	if (pcmcia_chip_mem_alloc(pct, pch, PCMCIA_CIS_SIZE, &pcmh)) {
144 #ifdef DIAGNOSTIC
145 		printf("%s: can't alloc memory to read attributes\n",
146 		    sc->dev.dv_xname);
147 #endif
148 		return -1;
149 	}
150 
151 	/* initialize state for the primary tuple chain */
152 	if (pcmcia_chip_mem_map(pct, pch, PCMCIA_MEM_ATTR, 0,
153 	    PCMCIA_CIS_SIZE, &pcmh, &tuple.ptr, &window)) {
154 		pcmcia_chip_mem_free(pct, pch, &pcmh);
155 #ifdef DIAGNOSTIC
156 		printf("%s: can't map memory to read attributes\n",
157 		    sc->dev.dv_xname);
158 #endif
159 		return -1;
160 	}
161 	tuple.memt = pcmh.memt;
162 	tuple.memh = pcmh.memh;
163 
164 	DPRINTF(("cis mem map %x\n", (unsigned int) tuple.memh));
165 
166 	tuple.addrshift = 1;
167 	tuple.flags = 0;
168 
169 	indirect_present = 0;
170 	longlink_present = 1;
171 	longlink_common = 1;
172 	longlink_addr = 0;
173 
174 	mfc_count = 0;
175 	mfc_index = 0;
176 
177 	DPRINTF(("%s: CIS tuple chain:\n", sc->dev.dv_xname));
178 
179 	while (1) {
180 		while (1) {
181 			/*
182 			 * Perform boundary check for insane cards.
183 			 * If CIS is too long, simulate CIS end.
184 			 * (This check may not be sufficient for
185 			 * malicious cards.)
186 			 */
187 			if ((tuple.ptr << tuple.addrshift) >=
188 			    PCMCIA_CIS_SIZE - 1 - 32 /* ad hoc value */) {
189 				DPRINTF(("CISTPL_END (too long CIS)\n"));
190 				tuple.code = PCMCIA_CISTPL_END;
191 				goto cis_end;
192 			}
193 
194 			/* get the tuple code */
195 
196 			tuple.code = pcmcia_cis_read_1(&tuple, tuple.ptr);
197 
198 			/* two special-case tuples */
199 
200 			if (tuple.code == PCMCIA_CISTPL_NULL) {
201 				DPRINTF(("CISTPL_NONE\n 00\n"));
202 				tuple.ptr++;
203 				continue;
204 			} else if (tuple.code == PCMCIA_CISTPL_END) {
205 				DPRINTF(("CISTPL_END\n ff\n"));
206 			cis_end:
207 				/* Call the function for the END tuple, since
208 				   the CIS semantics depend on it */
209 				if ((*fct) (&tuple, arg)) {
210 					pcmcia_chip_mem_unmap(pct, pch,
211 							      window);
212 					ret = 1;
213 					goto done;
214 				}
215 				tuple.ptr++;
216 				break;
217 			}
218 			/* now all the normal tuples */
219 
220 			tuple.length = pcmcia_cis_read_1(&tuple, tuple.ptr + 1);
221 			switch (tuple.code) {
222 			case PCMCIA_CISTPL_INDIRECT:
223 				indirect_present = 1;
224 				DPRINTF(("CISTPL_INDIRECT\n"));
225 				break;
226 			case PCMCIA_CISTPL_LONGLINK_A:
227 			case PCMCIA_CISTPL_LONGLINK_C:
228 				if (tuple.length < 4) {
229 					DPRINTF(("CISTPL_LONGLINK_%s too "
230 					    "short %d\n",
231 					    longlink_common ? "C" : "A",
232 					    tuple.length));
233 					break;
234 				}
235 				longlink_present = 1;
236 				longlink_common = (tuple.code ==
237 				    PCMCIA_CISTPL_LONGLINK_C) ? 1 : 0;
238 				longlink_addr = pcmcia_tuple_read_4(&tuple, 0);
239 				DPRINTF(("CISTPL_LONGLINK_%s %lx\n",
240 				    longlink_common ? "C" : "A",
241 				    longlink_addr));
242 				break;
243 			case PCMCIA_CISTPL_NO_LINK:
244 				longlink_present = 0;
245 				DPRINTF(("CISTPL_NO_LINK\n"));
246 				break;
247 			case PCMCIA_CISTPL_CHECKSUM:
248 				if (tuple.length < 5) {
249 					DPRINTF(("CISTPL_CHECKSUM too "
250 					    "short %d\n", tuple.length));
251 					break;
252 				} {
253 					int16_t offset;
254 					u_long addr, length;
255 					u_int cksum, sum;
256 					int i;
257 
258 					*((u_int16_t *) & offset) =
259 					    pcmcia_tuple_read_2(&tuple, 0);
260 					length = pcmcia_tuple_read_2(&tuple, 2);
261 					cksum = pcmcia_tuple_read_1(&tuple, 4);
262 
263 					addr = tuple.ptr + offset;
264 
265 					DPRINTF(("CISTPL_CHECKSUM addr=%lx "
266 					    "len=%lx cksum=%x",
267 					    addr, length, cksum));
268 
269 					/*
270 					 * XXX do more work to deal with
271 					 * distant regions
272 					 */
273 					if ((addr >= PCMCIA_CIS_SIZE) ||
274 					    ((addr + length) >=
275 					      PCMCIA_CIS_SIZE)) {
276 						DPRINTF((" skipped, "
277 						    "too distant\n"));
278 						break;
279 					}
280 					sum = 0;
281 					for (i = 0; i < length; i++)
282 						sum += pcmcia_cis_read_1(&tuple,
283 						    addr + i);
284 					if (cksum != (sum & 0xff)) {
285 						DPRINTF((" failed sum=%x\n",
286 						    sum));
287 						printf("%s: CIS checksum "
288 						    "failed\n",
289 						    sc->dev.dv_xname);
290 #if 0
291 						/*
292 						 * XXX Some working cards have
293 						 * XXX bad checksums!!
294 						 */
295 						ret = -1;
296 #endif
297 					} else {
298 						DPRINTF((" ok\n"));
299 					}
300 				}
301 				break;
302 			case PCMCIA_CISTPL_LONGLINK_MFC:
303 				if (tuple.length < 6) {
304 					DPRINTF(("CISTPL_LONGLINK_MFC too "
305 					    "short %d\n", tuple.length));
306 					break;
307 				}
308 				if (((tuple.length - 1) % 5) != 0) {
309 					DPRINTF(("CISTPL_LONGLINK_MFC bogus "
310 					    "length %d\n", tuple.length));
311 					break;
312 				}
313 				{
314 					int i, tmp_count;
315 
316 					/*
317 					 * put count into tmp var so that
318 					 * if we have to bail (because it's
319 					 * a bogus count) it won't be
320 					 * remembered for later use.
321 					 */
322 					tmp_count =
323 					    pcmcia_tuple_read_1(&tuple, 0);
324 					DPRINTF(("CISTPL_LONGLINK_MFC %d",
325 					    tmp_count));
326 
327 					/*
328 					 * make _sure_ it's the right size;
329 					 * if too short, it may be a weird
330 					 * (unknown/undefined) format
331 					 */
332 					if (tuple.length != (tmp_count*5 + 1)) {
333 						DPRINTF((" bogus length %d\n",
334 						    tuple.length));
335 						break;
336 					}
337 
338 #ifdef PCMCIACISDEBUG	/* maybe enable all the time? */
339 					/*
340 					 * sanity check for a programming
341 					 * error which is difficult to find
342 					 * when debugging.
343 					 */
344 					if (tmp_count >
345 					    howmany(sizeof mfc, sizeof mfc[0]))
346 						panic("CISTPL_LONGLINK_MFC mfc "
347 						    "count would blow stack");
348 #endif
349 
350 					mfc_count = tmp_count;
351 					for (i = 0; i < mfc_count; i++) {
352 						mfc[i].common =
353 						    (pcmcia_tuple_read_1(&tuple,
354 						    1 + 5 * i) ==
355 						    PCMCIA_MFC_MEM_COMMON) ?
356 						    1 : 0;
357 						mfc[i].addr =
358 						    pcmcia_tuple_read_4(&tuple,
359 						    1 + 5 * i + 1);
360 						DPRINTF((" %s:%lx",
361 						    mfc[i].common ? "common" :
362 						    "attr", mfc[i].addr));
363 					}
364 					DPRINTF(("\n"));
365 				}
366 				/*
367 				 * for LONGLINK_MFC, fall through to the
368 				 * function.  This tuple has structural and
369 				 * semantic content.
370 				 */
371 			default:
372 				{
373 					if ((*fct) (&tuple, arg)) {
374 						pcmcia_chip_mem_unmap(pct,
375 						    pch, window);
376 						ret = 1;
377 						goto done;
378 					}
379 				}
380 				break;
381 			}	/* switch */
382 #ifdef PCMCIACISDEBUG
383 			/* print the tuple */
384 			{
385 				int i;
386 
387 				DPRINTF((" %02x %02x", tuple.code,
388 				    tuple.length));
389 
390 				for (i = 0; i < tuple.length; i++) {
391 					DPRINTF((" %02x",
392 					    pcmcia_tuple_read_1(&tuple, i)));
393 					if ((i % 16) == 13)
394 						DPRINTF(("\n"));
395 				}
396 				if ((i % 16) != 14)
397 					DPRINTF(("\n"));
398 			}
399 #endif
400 			/* skip to the next tuple */
401 			tuple.ptr += 2 + tuple.length;
402 		}
403 
404 		/*
405 		 * the chain is done.  Clean up and move onto the next one,
406 		 * if any.  The loop is here in the case that there is an MFC
407 		 * card with no longlink (which defaults to existing, == 0).
408 		 * In general, this means that if one pointer fails, it will
409 		 * try the next one, instead of just bailing.
410 		 */
411 
412 		while (1) {
413 			pcmcia_chip_mem_unmap(pct, pch, window);
414 
415 			if (indirect_present) {
416 				/*
417 				 * Indirect CIS data needs to be obtained
418 				 * from specific registers accessible at
419 				 * a fixed location in the common window,
420 				 * but otherwise is similar to longlink
421 				 * in attribute memory.
422 				 */
423 
424 				pcmcia_chip_mem_map(pct, pch, PCMCIA_MEM_COMMON,
425 				    0, PCMCIA_INDR_SIZE,
426 				    &pcmh, &tuple.indirect_ptr, &window);
427 
428 				DPRINTF(("cis mem map %x ind %x\n",
429 				    (unsigned int) tuple.memh,
430 				    (unsigned int) tuple.indirect_ptr));
431 
432 				tuple.addrshift = 1;
433 				tuple.flags |= PTF_INDIRECT;
434 				tuple.ptr = 0;
435 				longlink_present = 0;
436 				indirect_present = 0;
437 			} else if (longlink_present) {
438 				/*
439 				 * if the longlink is to attribute memory,
440 				 * then it is unindexed.  That is, if the
441 				 * link value is 0x100, then the actual
442 				 * memory address is 0x200.  This means that
443 				 * we need to multiply by 2 before calling
444 				 * mem_map, and then divide the resulting ptr
445 				 * by 2 after.
446 				 */
447 
448 				if (!longlink_common)
449 					longlink_addr *= 2;
450 
451 				pcmcia_chip_mem_map(pct, pch, longlink_common ?
452 				    PCMCIA_MEM_COMMON : PCMCIA_MEM_ATTR,
453 				    longlink_addr, PCMCIA_CIS_SIZE,
454 				    &pcmh, &tuple.ptr, &window);
455 
456 				if (!longlink_common)
457 					tuple.ptr /= 2;
458 
459 				DPRINTF(("cis mem map %x\n",
460 				    (unsigned int) tuple.memh));
461 
462 				tuple.addrshift = longlink_common ? 0 : 1;
463 				longlink_present = 0;
464 				longlink_common = 1;
465 				longlink_addr = 0;
466 			} else if (mfc_count && (mfc_index < mfc_count)) {
467 				if (!mfc[mfc_index].common)
468 					mfc[mfc_index].addr *= 2;
469 
470 				pcmcia_chip_mem_map(pct, pch,
471 				    mfc[mfc_index].common ?
472 				    PCMCIA_MEM_COMMON : PCMCIA_MEM_ATTR,
473 				    mfc[mfc_index].addr, PCMCIA_CIS_SIZE,
474 				    &pcmh, &tuple.ptr, &window);
475 
476 				if (!mfc[mfc_index].common)
477 					tuple.ptr /= 2;
478 
479 				DPRINTF(("cis mem map %x\n",
480 				    (unsigned int) tuple.memh));
481 
482 				/* set parse state, and point at the next one */
483 
484 				tuple.addrshift = mfc[mfc_index].common ? 0 : 1;
485 
486 				mfc_index++;
487 			} else {
488 				goto done;
489 			}
490 
491 			/* make sure that the link is valid */
492 			tuple.code = pcmcia_cis_read_1(&tuple, tuple.ptr);
493 			if (tuple.code != PCMCIA_CISTPL_LINKTARGET) {
494 				DPRINTF(("CISTPL_LINKTARGET expected, "
495 				    "code %02x observed\n", tuple.code));
496 				continue;
497 			}
498 			tuple.length = pcmcia_cis_read_1(&tuple, tuple.ptr + 1);
499 			if (tuple.length < 3) {
500 				DPRINTF(("CISTPL_LINKTARGET too short %d\n",
501 				    tuple.length));
502 				continue;
503 			}
504 			if ((pcmcia_tuple_read_1(&tuple, 0) != 'C') ||
505 			    (pcmcia_tuple_read_1(&tuple, 1) != 'I') ||
506 			    (pcmcia_tuple_read_1(&tuple, 2) != 'S')) {
507 				DPRINTF(("CISTPL_LINKTARGET magic "
508 				    "%02x%02x%02x incorrect\n",
509 				    pcmcia_tuple_read_1(&tuple, 0),
510 				    pcmcia_tuple_read_1(&tuple, 1),
511 				    pcmcia_tuple_read_1(&tuple, 2)));
512 				continue;
513 			}
514 			tuple.ptr += 2 + tuple.length;
515 
516 			break;
517 		}
518 	}
519 
520 	pcmcia_chip_mem_unmap(pct, pch, window);
521 
522 done:
523 	/* Last, free the allocated memory block */
524 	pcmcia_chip_mem_free(pct, pch, &pcmh);
525 
526 	return (ret);
527 }
528 
529 /* XXX this is incredibly verbose.  Not sure what trt is */
530 
531 void
532 pcmcia_print_cis(sc)
533 	struct pcmcia_softc *sc;
534 {
535 	struct pcmcia_card *card = &sc->card;
536 	struct pcmcia_function *pf;
537 	struct pcmcia_config_entry *cfe;
538 	int i;
539 
540 	printf("%s: CIS version ", sc->dev.dv_xname);
541 	if (card->cis1_major == 4) {
542 		if (card->cis1_minor == 0)
543 			printf("PCMCIA 1.0\n");
544 		else if (card->cis1_minor == 1)
545 			printf("PCMCIA 2.0 or 2.1\n");
546 	} else if (card->cis1_major >= 5)
547 		printf("PC Card Standard %d.%d\n", card->cis1_major,
548 		    card->cis1_minor);
549 	else
550 		printf("unknown (major=%d, minor=%d)\n",
551 		    card->cis1_major, card->cis1_minor);
552 
553 	printf("%s: CIS info: ", sc->dev.dv_xname);
554 	for (i = 0; i < 4; i++) {
555 		if (card->cis1_info[i] == NULL)
556 			break;
557 		if (i)
558 			printf(", ");
559 		printf("%s", card->cis1_info[i]);
560 	}
561 	printf("\n");
562 
563 	printf("%s: Manufacturer code 0x%x, product 0x%x\n",
564 	       sc->dev.dv_xname, card->manufacturer, card->product);
565 
566 	SIMPLEQ_FOREACH(pf, &card->pf_head, pf_list) {
567 		printf("%s: function %d: ", sc->dev.dv_xname, pf->number);
568 
569 		switch (pf->function) {
570 		case PCMCIA_FUNCTION_UNSPEC:
571 			printf("unspecified");
572 			break;
573 		case PCMCIA_FUNCTION_MULTIFUNCTION:
574 			printf("multi-function");
575 			break;
576 		case PCMCIA_FUNCTION_MEMORY:
577 			printf("memory");
578 			break;
579 		case PCMCIA_FUNCTION_SERIAL:
580 			printf("serial port");
581 			break;
582 		case PCMCIA_FUNCTION_PARALLEL:
583 			printf("parallel port");
584 			break;
585 		case PCMCIA_FUNCTION_DISK:
586 			printf("fixed disk");
587 			break;
588 		case PCMCIA_FUNCTION_VIDEO:
589 			printf("video adapter");
590 			break;
591 		case PCMCIA_FUNCTION_NETWORK:
592 			printf("network adapter");
593 			break;
594 		case PCMCIA_FUNCTION_AIMS:
595 			printf("auto incrementing mass storage");
596 			break;
597 		case PCMCIA_FUNCTION_SCSI:
598 			printf("SCSI bridge");
599 			break;
600 		case PCMCIA_FUNCTION_SECURITY:
601 			printf("Security services");
602 			break;
603 		case PCMCIA_FUNCTION_INSTRUMENT:
604 			printf("Instrument");
605 			break;
606 		case PCMCIA_FUNCTION_IOBUS:
607 			printf("Serial I/O Bus Adapter");
608 			break;
609 		default:
610 			printf("unknown (%d)", pf->function);
611 			break;
612 		}
613 
614 		printf(", ccr addr %lx mask %lx\n", pf->ccr_base, pf->ccr_mask);
615 
616 		SIMPLEQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
617 			printf("%s: function %d, config table entry %d: ",
618 			    sc->dev.dv_xname, pf->number, cfe->number);
619 
620 			switch (cfe->iftype) {
621 			case PCMCIA_IFTYPE_MEMORY:
622 				printf("memory card");
623 				break;
624 			case PCMCIA_IFTYPE_IO:
625 				printf("I/O card");
626 				break;
627 			default:
628 				printf("card type unknown");
629 				break;
630 			}
631 
632 			printf("; irq mask %x", cfe->irqmask);
633 
634 			if (cfe->num_iospace) {
635 				printf("; iomask %lx, iospace", cfe->iomask);
636 
637 				for (i = 0; i < cfe->num_iospace; i++)
638 					printf(" %lx%s%lx",
639 					    cfe->iospace[i].start,
640 					    cfe->iospace[i].length ? "-" : "",
641 					    cfe->iospace[i].start +
642 					      cfe->iospace[i].length - 1);
643 			}
644 			if (cfe->num_memspace) {
645 				printf("; memspace");
646 
647 				for (i = 0; i < cfe->num_memspace; i++)
648 					printf(" %lx%s%lx%s%lx",
649 					    cfe->memspace[i].cardaddr,
650 					    cfe->memspace[i].length ? "-" : "",
651 					    cfe->memspace[i].cardaddr +
652 					      cfe->memspace[i].length - 1,
653 					    cfe->memspace[i].hostaddr ?
654 					      "@" : "",
655 					    cfe->memspace[i].hostaddr);
656 			}
657 			if (cfe->maxtwins)
658 				printf("; maxtwins %d", cfe->maxtwins);
659 
660 			printf(";");
661 
662 			if (cfe->flags & PCMCIA_CFE_MWAIT_REQUIRED)
663 				printf(" mwait_required");
664 			if (cfe->flags & PCMCIA_CFE_RDYBSY_ACTIVE)
665 				printf(" rdybsy_active");
666 			if (cfe->flags & PCMCIA_CFE_WP_ACTIVE)
667 				printf(" wp_active");
668 			if (cfe->flags & PCMCIA_CFE_BVD_ACTIVE)
669 				printf(" bvd_active");
670 			if (cfe->flags & PCMCIA_CFE_IO8)
671 				printf(" io8");
672 			if (cfe->flags & PCMCIA_CFE_IO16)
673 				printf(" io16");
674 			if (cfe->flags & PCMCIA_CFE_IRQSHARE)
675 				printf(" irqshare");
676 			if (cfe->flags & PCMCIA_CFE_IRQPULSE)
677 				printf(" irqpulse");
678 			if (cfe->flags & PCMCIA_CFE_IRQLEVEL)
679 				printf(" irqlevel");
680 			if (cfe->flags & PCMCIA_CFE_POWERDOWN)
681 				printf(" powerdown");
682 			if (cfe->flags & PCMCIA_CFE_READONLY)
683 				printf(" readonly");
684 			if (cfe->flags & PCMCIA_CFE_AUDIO)
685 				printf(" audio");
686 
687 			printf("\n");
688 		}
689 	}
690 
691 	if (card->error)
692 		printf("%s: %d errors found while parsing CIS\n",
693 		    sc->dev.dv_xname, card->error);
694 }
695 
696 int
697 pcmcia_parse_cis_tuple(tuple, arg)
698 	struct pcmcia_tuple *tuple;
699 	void *arg;
700 {
701 	/* most of these are educated guesses */
702 	static struct pcmcia_config_entry init_cfe = {
703 		-1, PCMCIA_CFE_RDYBSY_ACTIVE | PCMCIA_CFE_WP_ACTIVE |
704 		PCMCIA_CFE_BVD_ACTIVE, PCMCIA_IFTYPE_MEMORY,
705 	};
706 
707 	struct cis_state *state = arg;
708 
709 	switch (tuple->code) {
710 	case PCMCIA_CISTPL_END:
711 		/*
712 		 * If we've seen a LONGLINK_MFC, and this is the first
713 		 * END after it, reset the function list.
714 		 *
715 		 * XXX This might also be the right place to start a
716 		 * new function, but that assumes that a function
717 		 * definition never crosses any longlink, and I'm not
718 		 * sure about that.  This is probably safe for MFC
719 		 * cards, but what we have now isn't broken, so I'd
720 		 * rather not change it.
721 		 */
722 		if (state->gotmfc == 1) {
723 			struct pcmcia_function *pf, *pfnext;
724 
725 			for (pf = SIMPLEQ_FIRST(&state->card->pf_head);
726 			    pf != NULL; pf = pfnext) {
727 				pfnext = SIMPLEQ_NEXT(pf, pf_list);
728 				free(pf, M_DEVBUF, 0);
729 			}
730 
731 			SIMPLEQ_INIT(&state->card->pf_head);
732 
733 			state->count = 0;
734 			state->gotmfc = 2;
735 			state->pf = NULL;
736 		}
737 		break;
738 
739 	case PCMCIA_CISTPL_LONGLINK_MFC:
740 		/*
741 		 * This tuple's structure was dealt with in scan_cis.  here,
742 		 * record the fact that the MFC tuple was seen, so that
743 		 * functions declared before the MFC link can be cleaned
744 		 * up.
745 		 */
746 		state->gotmfc = 1;
747 		break;
748 
749 #ifdef PCMCIACISDEBUG
750 	case PCMCIA_CISTPL_DEVICE:
751 	case PCMCIA_CISTPL_DEVICE_A:
752 		{
753 			u_int reg, dtype, dspeed;
754 
755 			reg = pcmcia_tuple_read_1(tuple, 0);
756 			dtype = reg & PCMCIA_DTYPE_MASK;
757 			dspeed = reg & PCMCIA_DSPEED_MASK;
758 
759 			DPRINTF(("CISTPL_DEVICE%s type=",
760 			(tuple->code == PCMCIA_CISTPL_DEVICE) ? "" : "_A"));
761 			switch (dtype) {
762 			case PCMCIA_DTYPE_NULL:
763 				DPRINTF(("null"));
764 				break;
765 			case PCMCIA_DTYPE_ROM:
766 				DPRINTF(("rom"));
767 				break;
768 			case PCMCIA_DTYPE_OTPROM:
769 				DPRINTF(("otprom"));
770 				break;
771 			case PCMCIA_DTYPE_EPROM:
772 				DPRINTF(("eprom"));
773 				break;
774 			case PCMCIA_DTYPE_EEPROM:
775 				DPRINTF(("eeprom"));
776 				break;
777 			case PCMCIA_DTYPE_FLASH:
778 				DPRINTF(("flash"));
779 				break;
780 			case PCMCIA_DTYPE_SRAM:
781 				DPRINTF(("sram"));
782 				break;
783 			case PCMCIA_DTYPE_DRAM:
784 				DPRINTF(("dram"));
785 				break;
786 			case PCMCIA_DTYPE_FUNCSPEC:
787 				DPRINTF(("funcspec"));
788 				break;
789 			case PCMCIA_DTYPE_EXTEND:
790 				DPRINTF(("extend"));
791 				break;
792 			default:
793 				DPRINTF(("reserved"));
794 				break;
795 			}
796 			DPRINTF((" speed="));
797 			switch (dspeed) {
798 			case PCMCIA_DSPEED_NULL:
799 				DPRINTF(("null"));
800 				break;
801 			case PCMCIA_DSPEED_250NS:
802 				DPRINTF(("250ns"));
803 				break;
804 			case PCMCIA_DSPEED_200NS:
805 				DPRINTF(("200ns"));
806 				break;
807 			case PCMCIA_DSPEED_150NS:
808 				DPRINTF(("150ns"));
809 				break;
810 			case PCMCIA_DSPEED_100NS:
811 				DPRINTF(("100ns"));
812 				break;
813 			case PCMCIA_DSPEED_EXT:
814 				DPRINTF(("ext"));
815 				break;
816 			default:
817 				DPRINTF(("reserved"));
818 				break;
819 			}
820 		}
821 		DPRINTF(("\n"));
822 		break;
823 #endif
824 
825 	case PCMCIA_CISTPL_VERS_1:
826 		if (tuple->length < 6) {
827 			DPRINTF(("CISTPL_VERS_1 too short %d\n",
828 			    tuple->length));
829 			break;
830 		} {
831 			int start, i, ch, count;
832 
833 			state->card->cis1_major = pcmcia_tuple_read_1(tuple, 0);
834 			state->card->cis1_minor = pcmcia_tuple_read_1(tuple, 1);
835 
836 			for (count = 0, start = 0, i = 0;
837 			    (count < 4) && ((i + 4) < 256); i++) {
838 				ch = pcmcia_tuple_read_1(tuple, 2 + i);
839 				if (ch == 0xff)
840 					break;
841 				state->card->cis1_info_buf[i] = ch;
842 				if (ch == 0) {
843 					state->card->cis1_info[count] =
844 					    state->card->cis1_info_buf + start;
845 					start = i + 1;
846 					count++;
847 				}
848 			}
849 			DPRINTF(("CISTPL_VERS_1\n"));
850 		}
851 		break;
852 
853 	case PCMCIA_CISTPL_MANFID:
854 		if (tuple->length < 4) {
855 			DPRINTF(("CISTPL_MANFID too short %d\n",
856 			    tuple->length));
857 			break;
858 		}
859 		state->card->manufacturer = pcmcia_tuple_read_2(tuple, 0);
860 		state->card->product = pcmcia_tuple_read_2(tuple, 2);
861 		DPRINTF(("CISTPL_MANFID\n"));
862 		break;
863 
864 	case PCMCIA_CISTPL_FUNCID:
865 		if (tuple->length < 2) {
866 			DPRINTF(("CISTPL_FUNCID too short %d\n",
867 			    tuple->length));
868 			break;
869 		}
870 
871 		/*
872 		 * As far as I understand this, manufacturers do multifunction
873 		 * cards in various ways.  Sadly enough I do not have the
874 		 * PC-Card standard (donate!) so I can only guess what can
875 		 * be done.
876 		 * The original code implies FUNCID nodes are above CONFIG
877 		 * nodes in the CIS tree, however Xircom does it the other
878 		 * way round, which of course makes things a bit hard.
879 		 * --niklas@openbsd.org
880 		 */
881 		if (state->pf) {
882 			if (state->pf->function == PCMCIA_FUNCTION_UNSPEC) {
883 				/*
884 				 * This looks like a opportunistic function
885 				 * created by a CONFIG tuple.  Just keep it.
886 				 */
887 			} else {
888 				/*
889 				 * A function is being defined, end it.
890 				 */
891 				state->pf = NULL;
892 			}
893 		}
894 		if (state->pf == NULL) {
895 			state->pf = malloc(sizeof(*state->pf), M_DEVBUF,
896 			    M_NOWAIT | M_ZERO);
897 			if (state->pf == NULL)
898 				panic("pcmcia_parse_cis_tuple");
899 			state->pf->number = state->count++;
900 			state->pf->last_config_index = -1;
901 			SIMPLEQ_INIT(&state->pf->cfe_head);
902 
903 			SIMPLEQ_INSERT_TAIL(&state->card->pf_head, state->pf,
904 			    pf_list);
905 		}
906 		state->pf->function = pcmcia_tuple_read_1(tuple, 0);
907 
908 		DPRINTF(("CISTPL_FUNCID\n"));
909 		break;
910 
911 	case PCMCIA_CISTPL_CONFIG:
912 		if (tuple->length < 3) {
913 			DPRINTF(("CISTPL_CONFIG too short %d\n",
914 			    tuple->length));
915 			break;
916 		} {
917 			u_int reg, rasz, rmsz, rfsz;
918 			int i;
919 
920 			reg = pcmcia_tuple_read_1(tuple, 0);
921 			rasz = 1 + ((reg & PCMCIA_TPCC_RASZ_MASK) >>
922 			    PCMCIA_TPCC_RASZ_SHIFT);
923 			rmsz = 1 + ((reg & PCMCIA_TPCC_RMSZ_MASK) >>
924 			    PCMCIA_TPCC_RMSZ_SHIFT);
925 			rfsz = ((reg & PCMCIA_TPCC_RFSZ_MASK) >>
926 			    PCMCIA_TPCC_RFSZ_SHIFT);
927 
928 			if (tuple->length < 2 + rasz + rmsz + rfsz) {
929 				DPRINTF(("CISTPL_CONFIG (%d,%d,%d) too "
930 				    "short %d\n", rasz, rmsz, rfsz,
931 				    tuple->length));
932 				break;
933 			}
934 			if (state->pf == NULL) {
935 				state->pf = malloc(sizeof(*state->pf),
936 				    M_DEVBUF, M_NOWAIT | M_ZERO);
937 				if (state->pf == NULL)
938 					panic("pcmcia_parse_cis_tuple");
939 				state->pf->number = state->count++;
940 				state->pf->last_config_index = -1;
941 				SIMPLEQ_INIT(&state->pf->cfe_head);
942 
943 				SIMPLEQ_INSERT_TAIL(&state->card->pf_head,
944 				    state->pf, pf_list);
945 
946 				state->pf->function = PCMCIA_FUNCTION_UNSPEC;
947 			}
948 			state->pf->last_config_index =
949 			    pcmcia_tuple_read_1(tuple, 1);
950 
951 			state->pf->ccr_base = 0;
952 			for (i = 0; i < rasz; i++)
953 				state->pf->ccr_base |=
954 				    ((pcmcia_tuple_read_1(tuple, 2 + i)) <<
955 				    (i * 8));
956 
957 			state->pf->ccr_mask = 0;
958 			for (i = 0; i < rmsz; i++)
959 				state->pf->ccr_mask |=
960 				    ((pcmcia_tuple_read_1(tuple,
961 				    2 + rasz + i)) << (i * 8));
962 
963 			/* skip the reserved area and subtuples */
964 
965 			/* reset the default cfe for each cfe list */
966 			state->temp_cfe = init_cfe;
967 			state->default_cfe = &state->temp_cfe;
968 		}
969 		DPRINTF(("CISTPL_CONFIG\n"));
970 		break;
971 
972 	case PCMCIA_CISTPL_CFTABLE_ENTRY:
973 		if (tuple->length < 2) {
974 			DPRINTF(("CISTPL_CFTABLE_ENTRY too short %d\n",
975 			    tuple->length));
976 			break;
977 		} {
978 			int idx, i, j;
979 			u_int reg, reg2;
980 			u_int intface, def, num;
981 			u_int power, timing, iospace, irq, memspace, misc;
982 			struct pcmcia_config_entry *cfe;
983 
984 			idx = 0;
985 
986 			reg = pcmcia_tuple_read_1(tuple, idx);
987 			idx++;
988 			intface = reg & PCMCIA_TPCE_INDX_INTFACE;
989 			def = reg & PCMCIA_TPCE_INDX_DEFAULT;
990 			num = reg & PCMCIA_TPCE_INDX_NUM_MASK;
991 
992 			/*
993 			 * this is a little messy.  Some cards have only a
994 			 * cfentry with the default bit set.  So, as we go
995 			 * through the list, we add new indexes to the queue,
996 			 * and keep a pointer to the last one with the
997 			 * default bit set.  if we see a record with the same
998 			 * index, as the default, we stash the default and
999 			 * replace the queue entry. otherwise, we just add
1000 			 * new entries to the queue, pointing the default ptr
1001 			 * at them if the default bit is set.  if we get to
1002 			 * the end with the default pointer pointing at a
1003 			 * record which hasn't had a matching index, that's
1004 			 * ok; it just becomes a cfentry like any other.
1005 			 */
1006 
1007 			/*
1008 			 * if the index in the cis differs from the default
1009 			 * cis, create new entry in the queue and start it
1010 			 * with the current default
1011 			 */
1012 			if (state->default_cfe == NULL) {
1013 				DPRINTF(("CISTPL_CFTABLE_ENTRY with no "
1014 				    "default\n"));
1015 				break;
1016 			}
1017 			if (num != state->default_cfe->number) {
1018 				cfe = (struct pcmcia_config_entry *)
1019 				    malloc(sizeof(*cfe), M_DEVBUF, M_NOWAIT);
1020 				if (cfe == NULL)
1021 					panic("pcmcia_parse_cis_tuple");
1022 
1023 				*cfe = *state->default_cfe;
1024 
1025 				SIMPLEQ_INSERT_TAIL(&state->pf->cfe_head,
1026 				    cfe, cfe_list);
1027 
1028 				cfe->number = num;
1029 
1030 				/*
1031 				 * if the default bit is set in the cis, then
1032 				 * point the new default at whatever is being
1033 				 * filled in
1034 				 */
1035 				if (def)
1036 					state->default_cfe = cfe;
1037 			} else {
1038 				/*
1039 				 * the cis index matches the default index,
1040 				 * fill in the default cfentry.  It is
1041 				 * assumed that the cfdefault index is in the
1042 				 * queue.  For it to be otherwise, the cis
1043 				 * index would have to be -1 (initial
1044 				 * condition) which is not possible, or there
1045 				 * would have to be a preceding cis entry
1046 				 * which had the same cis index and had the
1047 				 * default bit unset. Neither condition
1048 				 * should happen.  If it does, this cfentry
1049 				 * is lost (written into temp space), which
1050 				 * is an acceptable failure mode.
1051 				 */
1052 
1053 				cfe = state->default_cfe;
1054 
1055 				/*
1056 				 * if the cis entry does not have the default
1057 				 * bit set, copy the default out of the way
1058 				 * first.
1059 				 */
1060 				if (!def) {
1061 					state->temp_cfe = *state->default_cfe;
1062 					state->default_cfe = &state->temp_cfe;
1063 				}
1064 			}
1065 
1066 			if (intface) {
1067 				reg = pcmcia_tuple_read_1(tuple, idx);
1068 				idx++;
1069 				cfe->flags &= ~(PCMCIA_CFE_MWAIT_REQUIRED
1070 				    | PCMCIA_CFE_RDYBSY_ACTIVE
1071 				    | PCMCIA_CFE_WP_ACTIVE
1072 				    | PCMCIA_CFE_BVD_ACTIVE);
1073 				if (reg & PCMCIA_TPCE_IF_MWAIT)
1074 					cfe->flags |= PCMCIA_CFE_MWAIT_REQUIRED;
1075 				if (reg & PCMCIA_TPCE_IF_RDYBSY)
1076 					cfe->flags |= PCMCIA_CFE_RDYBSY_ACTIVE;
1077 				if (reg & PCMCIA_TPCE_IF_WP)
1078 					cfe->flags |= PCMCIA_CFE_WP_ACTIVE;
1079 				if (reg & PCMCIA_TPCE_IF_BVD)
1080 					cfe->flags |= PCMCIA_CFE_BVD_ACTIVE;
1081 				cfe->iftype = reg & PCMCIA_TPCE_IF_IFTYPE;
1082 			}
1083 			reg = pcmcia_tuple_read_1(tuple, idx);
1084 			idx++;
1085 
1086 			power = reg & PCMCIA_TPCE_FS_POWER_MASK;
1087 			timing = reg & PCMCIA_TPCE_FS_TIMING;
1088 			iospace = reg & PCMCIA_TPCE_FS_IOSPACE;
1089 			irq = reg & PCMCIA_TPCE_FS_IRQ;
1090 			memspace = reg & PCMCIA_TPCE_FS_MEMSPACE_MASK;
1091 			misc = reg & PCMCIA_TPCE_FS_MISC;
1092 
1093 			if (power) {
1094 				/* skip over power, don't save */
1095 				/* for each parameter selection byte */
1096 				for (i = 0; i < power; i++) {
1097 					reg = pcmcia_tuple_read_1(tuple, idx);
1098 					idx++;
1099 					/* for each bit */
1100 					for (j = 0; j < 7; j++) {
1101 						/* if the bit is set */
1102 						if ((reg >> j) & 0x01) {
1103 							/* skip over bytes */
1104 							do {
1105 								reg2 = pcmcia_tuple_read_1(tuple, idx);
1106 								idx++;
1107 								/*
1108 								 * until
1109 								 * non-
1110 								 * extension
1111 								 * byte
1112 								 */
1113 							} while (reg2 & 0x80);
1114 						}
1115 					}
1116 				}
1117 			}
1118 			if (timing) {
1119 				/* skip over timing, don't save */
1120 				reg = pcmcia_tuple_read_1(tuple, idx);
1121 				idx++;
1122 
1123 				if ((reg & PCMCIA_TPCE_TD_RESERVED_MASK) !=
1124 				    PCMCIA_TPCE_TD_RESERVED_MASK)
1125 					idx++;
1126 				if ((reg & PCMCIA_TPCE_TD_RDYBSY_MASK) !=
1127 				    PCMCIA_TPCE_TD_RDYBSY_MASK)
1128 					idx++;
1129 				if ((reg & PCMCIA_TPCE_TD_WAIT_MASK) !=
1130 				    PCMCIA_TPCE_TD_WAIT_MASK)
1131 					idx++;
1132 			}
1133 			if (iospace) {
1134 				if (tuple->length <= idx) {
1135 					DPRINTF(("ran out of space before TPCE_IO\n"));
1136 
1137 					goto abort_cfe;
1138 				}
1139 
1140 				reg = pcmcia_tuple_read_1(tuple, idx);
1141 				idx++;
1142 
1143 				cfe->flags &=
1144 				    ~(PCMCIA_CFE_IO8 | PCMCIA_CFE_IO16);
1145 				if (reg & PCMCIA_TPCE_IO_BUSWIDTH_8BIT)
1146 					cfe->flags |= PCMCIA_CFE_IO8;
1147 				if (reg & PCMCIA_TPCE_IO_BUSWIDTH_16BIT)
1148 					cfe->flags |= PCMCIA_CFE_IO16;
1149 				cfe->iomask =
1150 				    reg & PCMCIA_TPCE_IO_IOADDRLINES_MASK;
1151 
1152 				if (reg & PCMCIA_TPCE_IO_HASRANGE) {
1153 					reg = pcmcia_tuple_read_1(tuple, idx);
1154 					idx++;
1155 
1156 					cfe->num_iospace = 1 + (reg &
1157 					    PCMCIA_TPCE_IO_RANGE_COUNT);
1158 
1159 					if (cfe->num_iospace >
1160 					    (sizeof(cfe->iospace) /
1161 					     sizeof(cfe->iospace[0]))) {
1162 						DPRINTF(("too many io "
1163 						    "spaces %d",
1164 						    cfe->num_iospace));
1165 						state->card->error++;
1166 						break;
1167 					}
1168 					for (i = 0; i < cfe->num_iospace; i++) {
1169 						switch (reg & PCMCIA_TPCE_IO_RANGE_ADDRSIZE_MASK) {
1170 						case PCMCIA_TPCE_IO_RANGE_ADDRSIZE_ONE:
1171 							cfe->iospace[i].start =
1172 								pcmcia_tuple_read_1(tuple, idx);
1173 							idx++;
1174 							break;
1175 						case PCMCIA_TPCE_IO_RANGE_ADDRSIZE_TWO:
1176 							cfe->iospace[i].start =
1177 								pcmcia_tuple_read_2(tuple, idx);
1178 							idx += 2;
1179 							break;
1180 						case PCMCIA_TPCE_IO_RANGE_ADDRSIZE_FOUR:
1181 							cfe->iospace[i].start =
1182 								pcmcia_tuple_read_4(tuple, idx);
1183 							idx += 4;
1184 							break;
1185 						}
1186 						switch (reg &
1187 							PCMCIA_TPCE_IO_RANGE_LENGTHSIZE_MASK) {
1188 						case PCMCIA_TPCE_IO_RANGE_LENGTHSIZE_ONE:
1189 							cfe->iospace[i].length =
1190 								pcmcia_tuple_read_1(tuple, idx);
1191 							idx++;
1192 							break;
1193 						case PCMCIA_TPCE_IO_RANGE_LENGTHSIZE_TWO:
1194 							cfe->iospace[i].length =
1195 								pcmcia_tuple_read_2(tuple, idx);
1196 							idx += 2;
1197 							break;
1198 						case PCMCIA_TPCE_IO_RANGE_LENGTHSIZE_FOUR:
1199 							cfe->iospace[i].length =
1200 								pcmcia_tuple_read_4(tuple, idx);
1201 							idx += 4;
1202 							break;
1203 						}
1204 						cfe->iospace[i].length++;
1205 					}
1206 				} else {
1207 					cfe->num_iospace = 1;
1208 					cfe->iospace[0].start = 0;
1209 					cfe->iospace[0].length =
1210 					    (1 << cfe->iomask);
1211 				}
1212 			}
1213 
1214 			if (irq) {
1215 				if (tuple->length <= idx) {
1216 					DPRINTF(("ran out of space before TPCE_IR\n"));
1217 
1218 					goto abort_cfe;
1219 				}
1220 
1221 				reg = pcmcia_tuple_read_1(tuple, idx);
1222 				idx++;
1223 
1224 				cfe->flags &= ~(PCMCIA_CFE_IRQSHARE
1225 				    | PCMCIA_CFE_IRQPULSE
1226 				    | PCMCIA_CFE_IRQLEVEL);
1227 				if (reg & PCMCIA_TPCE_IR_SHARE)
1228 					cfe->flags |= PCMCIA_CFE_IRQSHARE;
1229 				if (reg & PCMCIA_TPCE_IR_PULSE)
1230 					cfe->flags |= PCMCIA_CFE_IRQPULSE;
1231 				if (reg & PCMCIA_TPCE_IR_LEVEL)
1232 					cfe->flags |= PCMCIA_CFE_IRQLEVEL;
1233 
1234 				if (reg & PCMCIA_TPCE_IR_HASMASK) {
1235 					/*
1236 					 * it's legal to ignore the
1237 					 * special-interrupt bits, so I will
1238 					 */
1239 
1240 					cfe->irqmask =
1241 					    pcmcia_tuple_read_2(tuple, idx);
1242 					idx += 2;
1243 				} else {
1244 					cfe->irqmask =
1245 					    (1 << (reg & PCMCIA_TPCE_IR_IRQ));
1246 				}
1247 			}
1248 			if (memspace) {
1249 				if (tuple->length <= idx) {
1250 					DPRINTF(("ran out of space before TPCE_MS\n"));
1251 					goto abort_cfe;
1252 				}
1253 
1254 				if (memspace == PCMCIA_TPCE_FS_MEMSPACE_NONE) {
1255 					cfe->num_memspace = 0;
1256 				} else if (memspace == PCMCIA_TPCE_FS_MEMSPACE_LENGTH) {
1257 					cfe->num_memspace = 1;
1258 					cfe->memspace[0].length = 256 *
1259 					    pcmcia_tuple_read_2(tuple, idx);
1260 					idx += 2;
1261 					cfe->memspace[0].cardaddr = 0;
1262 					cfe->memspace[0].hostaddr = 0;
1263 				} else if (memspace ==
1264 				    PCMCIA_TPCE_FS_MEMSPACE_LENGTHADDR) {
1265 					cfe->num_memspace = 1;
1266 					cfe->memspace[0].length = 256 *
1267 					    pcmcia_tuple_read_2(tuple, idx);
1268 					idx += 2;
1269 					cfe->memspace[0].cardaddr = 256 *
1270 					    pcmcia_tuple_read_2(tuple, idx);
1271 					idx += 2;
1272 					cfe->memspace[0].hostaddr = cfe->memspace[0].cardaddr;
1273 				} else {
1274 					int lengthsize;
1275 					int cardaddrsize;
1276 					int hostaddrsize;
1277 
1278 					reg = pcmcia_tuple_read_1(tuple, idx);
1279 					idx++;
1280 
1281 					cfe->num_memspace = (reg &
1282 					    PCMCIA_TPCE_MS_COUNT) + 1;
1283 
1284 					if (cfe->num_memspace >
1285 					    (sizeof(cfe->memspace) /
1286 					     sizeof(cfe->memspace[0]))) {
1287 						DPRINTF(("too many mem "
1288 						    "spaces %d",
1289 						    cfe->num_memspace));
1290 						state->card->error++;
1291 						break;
1292 					}
1293 					lengthsize =
1294 						((reg & PCMCIA_TPCE_MS_LENGTH_SIZE_MASK) >>
1295 						 PCMCIA_TPCE_MS_LENGTH_SIZE_SHIFT);
1296 					cardaddrsize =
1297 						((reg & PCMCIA_TPCE_MS_CARDADDR_SIZE_MASK) >>
1298 						 PCMCIA_TPCE_MS_CARDADDR_SIZE_SHIFT);
1299 					hostaddrsize =
1300 						(reg & PCMCIA_TPCE_MS_HOSTADDR) ? cardaddrsize : 0;
1301 
1302 					if (lengthsize == 0) {
1303 						DPRINTF(("cfe memspace "
1304 						    "lengthsize == 0"));
1305 						state->card->error++;
1306 					}
1307 					for (i = 0; i < cfe->num_memspace; i++) {
1308 						if (lengthsize) {
1309 							cfe->memspace[i].length =
1310 								256 * pcmcia_tuple_read_n(tuple, lengthsize,
1311 								       idx);
1312 							idx += lengthsize;
1313 						} else {
1314 							cfe->memspace[i].length = 0;
1315 						}
1316 						if (cfe->memspace[i].length == 0) {
1317 							DPRINTF(("cfe->memspace[%d].length == 0",
1318 								 i));
1319 							state->card->error++;
1320 						}
1321 						if (cardaddrsize) {
1322 							cfe->memspace[i].cardaddr =
1323 								256 * pcmcia_tuple_read_n(tuple, cardaddrsize,
1324 								       idx);
1325 							idx += cardaddrsize;
1326 						} else {
1327 							cfe->memspace[i].cardaddr = 0;
1328 						}
1329 						if (hostaddrsize) {
1330 							cfe->memspace[i].hostaddr =
1331 								256 * pcmcia_tuple_read_n(tuple, hostaddrsize,
1332 								       idx);
1333 							idx += hostaddrsize;
1334 						} else {
1335 							cfe->memspace[i].hostaddr = 0;
1336 						}
1337 					}
1338 				}
1339 			}
1340 			if (misc) {
1341 				if (tuple->length <= idx) {
1342 					DPRINTF(("ran out of space before TPCE_MI\n"));
1343 
1344 					goto abort_cfe;
1345 				}
1346 
1347 				reg = pcmcia_tuple_read_1(tuple, idx);
1348 				idx++;
1349 
1350 				cfe->flags &= ~(PCMCIA_CFE_POWERDOWN
1351 				    | PCMCIA_CFE_READONLY
1352 				    | PCMCIA_CFE_AUDIO);
1353 				if (reg & PCMCIA_TPCE_MI_PWRDOWN)
1354 					cfe->flags |= PCMCIA_CFE_POWERDOWN;
1355 				if (reg & PCMCIA_TPCE_MI_READONLY)
1356 					cfe->flags |= PCMCIA_CFE_READONLY;
1357 				if (reg & PCMCIA_TPCE_MI_AUDIO)
1358 					cfe->flags |= PCMCIA_CFE_AUDIO;
1359 				cfe->maxtwins = reg & PCMCIA_TPCE_MI_MAXTWINS;
1360 
1361 				while (reg & PCMCIA_TPCE_MI_EXT) {
1362 					reg = pcmcia_tuple_read_1(tuple, idx);
1363 					idx++;
1364 				}
1365 			}
1366 			/* skip all the subtuples */
1367 		}
1368 
1369 	abort_cfe:
1370 		DPRINTF(("CISTPL_CFTABLE_ENTRY\n"));
1371 		break;
1372 
1373 	default:
1374 		DPRINTF(("unhandled CISTPL %x\n", tuple->code));
1375 		break;
1376 	}
1377 
1378 	return (0);
1379 }
1380