Home | History | Annotate | Line # | Download | only in cardbus
cardbus.c revision 1.85
      1 /*	$NetBSD: cardbus.c,v 1.85 2008/01/02 23:04:25 dyoung Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997, 1998, 1999 and 2000
      5  *     HAYAKAWA Koichi.  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 HAYAKAWA Koichi.
     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  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     25  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     26  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     30  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  * POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: cardbus.c,v 1.85 2008/01/02 23:04:25 dyoung Exp $");
     37 
     38 #include "opt_cardbus.h"
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/device.h>
     43 #include <sys/malloc.h>
     44 #include <sys/kernel.h>
     45 #include <sys/syslog.h>
     46 #include <sys/proc.h>
     47 #include <sys/reboot.h>		/* for AB_* needed by bootverbose */
     48 
     49 #include <sys/bus.h>
     50 
     51 #include <dev/cardbus/cardbusvar.h>
     52 #include <dev/pci/pcidevs.h>
     53 
     54 #include <dev/cardbus/cardbus_exrom.h>
     55 
     56 #include <dev/pci/pcivar.h>	/* XXX */
     57 #include <dev/pci/pcireg.h>	/* XXX */
     58 
     59 #include <dev/pcmcia/pcmciareg.h>
     60 
     61 #include "locators.h"
     62 
     63 #if defined CARDBUS_DEBUG
     64 #define STATIC
     65 #define DPRINTF(a) printf a
     66 #else
     67 #define STATIC static
     68 #define DPRINTF(a)
     69 #endif
     70 
     71 
     72 STATIC void cardbusattach(struct device *, struct device *, void *);
     73 STATIC int cardbusdetach(device_t, int);
     74 STATIC int cardbusmatch(struct device *, struct cfdata *, void *);
     75 int cardbus_rescan(struct device *, const char *, const int *);
     76 void cardbus_childdetached(struct device *, struct device *);
     77 static int cardbusprint(void *, const char *);
     78 
     79 typedef void (*tuple_decode_func)(u_int8_t*, int, void*);
     80 
     81 static int decode_tuples(u_int8_t *, int, tuple_decode_func, void*);
     82 #ifdef CARDBUS_DEBUG
     83 static void print_tuple(u_int8_t*, int, void*);
     84 #endif
     85 
     86 static int cardbus_read_tuples(struct cardbus_attach_args *,
     87     cardbusreg_t, u_int8_t *, size_t);
     88 
     89 static void enable_function(struct cardbus_softc *, int, int);
     90 static void disable_function(struct cardbus_softc *, int);
     91 
     92 static bool cardbus_child_register(device_t);
     93 
     94 CFATTACH_DECL2(cardbus, sizeof(struct cardbus_softc),
     95     cardbusmatch, cardbusattach, cardbusdetach, NULL,
     96     cardbus_rescan, cardbus_childdetached);
     97 
     98 #ifndef __NetBSD_Version__
     99 struct cfdriver cardbus_cd = {
    100 	NULL, "cardbus", DV_DULL
    101 };
    102 #endif
    103 
    104 
    105 STATIC int
    106 cardbusmatch(struct device *parent, struct cfdata *cf, void *aux)
    107 {
    108 	struct cbslot_attach_args *cba = aux;
    109 
    110 	if (strcmp(cba->cba_busname, cf->cf_name)) {
    111 		DPRINTF(("cardbusmatch: busname differs %s <=> %s\n",
    112 		    cba->cba_busname, cf->cf_name));
    113 		return (0);
    114 	}
    115 
    116 	return (1);
    117 }
    118 
    119 STATIC void
    120 cardbusattach(struct device *parent, struct device *self, void *aux)
    121 {
    122 	struct cardbus_softc *sc = device_private(self);
    123 	struct cbslot_attach_args *cba = aux;
    124 
    125 	sc->sc_bus = cba->cba_bus;
    126 	sc->sc_intrline = cba->cba_intrline;
    127 	sc->sc_cacheline = cba->cba_cacheline;
    128 	sc->sc_max_lattimer = MIN(0xf8, cba->cba_max_lattimer);
    129 
    130 	aprint_naive("\n");
    131 	aprint_normal(": bus %d", sc->sc_bus);
    132 	if (bootverbose)
    133 		aprint_normal(" cacheline 0x%x, lattimer 0x%x",
    134 		    sc->sc_cacheline, sc->sc_max_lattimer);
    135 	aprint_normal("\n");
    136 
    137 	sc->sc_iot = cba->cba_iot;	/* CardBus I/O space tag */
    138 	sc->sc_memt = cba->cba_memt;	/* CardBus MEM space tag */
    139 	sc->sc_dmat = cba->cba_dmat;	/* DMA tag */
    140 	sc->sc_cc = cba->cba_cc;
    141 	sc->sc_cf = cba->cba_cf;
    142 
    143 #if rbus
    144 	sc->sc_rbus_iot = cba->cba_rbus_iot;
    145 	sc->sc_rbus_memt = cba->cba_rbus_memt;
    146 #endif
    147 
    148 	if (!pmf_device_register(self, NULL, NULL))
    149 		aprint_error_dev(self, "couldn't establish power handler\n");
    150 }
    151 
    152 STATIC int
    153 cardbusdetach(device_t self, int flags)
    154 {
    155 	int rc;
    156 
    157 	if ((rc = config_detach_children(self, flags)) != 0)
    158 		return rc;
    159 
    160 	pmf_device_deregister(self);
    161 	return 0;
    162 }
    163 
    164 static int
    165 cardbus_read_tuples(struct cardbus_attach_args *ca, cardbusreg_t cis_ptr,
    166     u_int8_t *tuples, size_t len)
    167 {
    168 	struct cardbus_softc *sc = ca->ca_ct->ct_sc;
    169 	cardbus_chipset_tag_t cc = ca->ca_ct->ct_cc;
    170 	cardbus_function_tag_t cf = ca->ca_ct->ct_cf;
    171 	cardbustag_t tag = ca->ca_tag;
    172 	cardbusreg_t command;
    173 	bus_space_tag_t bar_tag;
    174 	bus_space_handle_t bar_memh;
    175 	bus_size_t bar_size;
    176 	bus_addr_t bar_addr;
    177 	cardbusreg_t reg;
    178 	int found = 0;
    179 	int cardbus_space = cis_ptr & CARDBUS_CIS_ASIMASK;
    180 	int i, j;
    181 
    182 	memset(tuples, 0, len);
    183 
    184 	cis_ptr = cis_ptr & CARDBUS_CIS_ADDRMASK;
    185 
    186 	switch (cardbus_space) {
    187 	case CARDBUS_CIS_ASI_TUPLE:
    188 		DPRINTF(("%s: reading CIS data from configuration space\n",
    189 		    sc->sc_dev.dv_xname));
    190 		for (i = cis_ptr, j = 0; i < 0xff; i += 4) {
    191 			u_int32_t e = (*cf->cardbus_conf_read)(cc, tag, i);
    192 			tuples[j] = 0xff & e;
    193 			e >>= 8;
    194 			tuples[j + 1] = 0xff & e;
    195 			e >>= 8;
    196 			tuples[j + 2] = 0xff & e;
    197 			e >>= 8;
    198 			tuples[j + 3] = 0xff & e;
    199 			j += 4;
    200 		}
    201 		found++;
    202 		break;
    203 
    204 	case CARDBUS_CIS_ASI_BAR0:
    205 	case CARDBUS_CIS_ASI_BAR1:
    206 	case CARDBUS_CIS_ASI_BAR2:
    207 	case CARDBUS_CIS_ASI_BAR3:
    208 	case CARDBUS_CIS_ASI_BAR4:
    209 	case CARDBUS_CIS_ASI_BAR5:
    210 	case CARDBUS_CIS_ASI_ROM:
    211 		if (cardbus_space == CARDBUS_CIS_ASI_ROM) {
    212 			reg = CARDBUS_ROM_REG;
    213 			DPRINTF(("%s: reading CIS data from ROM\n",
    214 			    sc->sc_dev.dv_xname));
    215 		} else {
    216 			reg = CARDBUS_CIS_ASI_BAR(cardbus_space);
    217 			DPRINTF(("%s: reading CIS data from BAR%d\n",
    218 			    sc->sc_dev.dv_xname, cardbus_space - 1));
    219 		}
    220 
    221 		/*
    222 		 * XXX zero register so mapreg_map doesn't get confused by old
    223 		 * contents.
    224 		 */
    225 		cardbus_conf_write(cc, cf, tag, reg, 0);
    226 		if (Cardbus_mapreg_map(ca->ca_ct, reg,
    227 		    CARDBUS_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
    228 		    0, &bar_tag, &bar_memh, &bar_addr, &bar_size)) {
    229 			printf("%s: failed to map memory\n",
    230 			    sc->sc_dev.dv_xname);
    231 			return (1);
    232 		}
    233 		aprint_debug_dev(&sc->sc_dev, "mapped %ju bytes at 0x%jx\n",
    234 		    (uintmax_t)bar_size, (uintmax_t)bar_addr);
    235 
    236 		if (cardbus_space == CARDBUS_CIS_ASI_ROM) {
    237 			cardbusreg_t exrom;
    238 			int save;
    239 			struct cardbus_rom_image_head rom_image;
    240 			struct cardbus_rom_image *p;
    241 
    242 			save = splhigh();
    243 			/* enable rom address decoder */
    244 			exrom = cardbus_conf_read(cc, cf, tag, reg);
    245 			cardbus_conf_write(cc, cf, tag, reg, exrom | 1);
    246 
    247 			command = cardbus_conf_read(cc, cf, tag,
    248 			    CARDBUS_COMMAND_STATUS_REG);
    249 			cardbus_conf_write(cc, cf, tag,
    250 			    CARDBUS_COMMAND_STATUS_REG,
    251 			    command | CARDBUS_COMMAND_MEM_ENABLE);
    252 
    253 			if (cardbus_read_exrom(bar_tag, bar_memh, &rom_image))
    254 				goto out;
    255 
    256 			SIMPLEQ_FOREACH(p, &rom_image, next) {
    257 				if (p->rom_image ==
    258 				    CARDBUS_CIS_ASI_ROM_IMAGE(cis_ptr)) {
    259 					bus_space_read_region_1(p->romt,
    260 					    p->romh, CARDBUS_CIS_ADDR(cis_ptr),
    261 					    tuples, MIN(p->image_size, len));
    262 					found++;
    263 					break;
    264 				}
    265 			}
    266 			while ((p = SIMPLEQ_FIRST(&rom_image)) != NULL) {
    267 				SIMPLEQ_REMOVE_HEAD(&rom_image, next);
    268 				free(p, M_DEVBUF);
    269 			}
    270 		out:
    271 			exrom = cardbus_conf_read(cc, cf, tag, reg);
    272 			cardbus_conf_write(cc, cf, tag, reg, exrom & ~1);
    273 			splx(save);
    274 		} else {
    275 			command = cardbus_conf_read(cc, cf, tag,
    276 			    CARDBUS_COMMAND_STATUS_REG);
    277 			cardbus_conf_write(cc, cf, tag,
    278 			    CARDBUS_COMMAND_STATUS_REG,
    279 			    command | CARDBUS_COMMAND_MEM_ENABLE);
    280 			/* XXX byte order? */
    281 			bus_space_read_region_1(bar_tag, bar_memh,
    282 			    cis_ptr, tuples,
    283 			    MIN(bar_size - MIN(bar_size, cis_ptr), len));
    284 			found++;
    285 		}
    286 		command = cardbus_conf_read(cc, cf, tag,
    287 		    CARDBUS_COMMAND_STATUS_REG);
    288 		cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG,
    289 		    command & ~CARDBUS_COMMAND_MEM_ENABLE);
    290 		cardbus_conf_write(cc, cf, tag, reg, 0);
    291 
    292 		Cardbus_mapreg_unmap(ca->ca_ct, reg, bar_tag, bar_memh,
    293 		    bar_size);
    294 		break;
    295 
    296 #ifdef DIAGNOSTIC
    297 	default:
    298 		panic("%s: bad CIS space (%d)", sc->sc_dev.dv_xname,
    299 		    cardbus_space);
    300 #endif
    301 	}
    302 	return (!found);
    303 }
    304 
    305 static void
    306 parse_tuple(u_int8_t *tuple, int len, void *data)
    307 {
    308 	struct cardbus_cis_info *cis = data;
    309 	char *p;
    310 	int i, bar_index;
    311 
    312 	switch (tuple[0]) {
    313 	case PCMCIA_CISTPL_MANFID:
    314 		if (tuple[1] != 4) {
    315 			DPRINTF(("%s: wrong length manufacturer id (%d)\n",
    316 			    __func__, tuple[1]));
    317 			break;
    318 		}
    319 		cis->manufacturer = tuple[2] | (tuple[3] << 8);
    320 		cis->product = tuple[4] | (tuple[5] << 8);
    321 		break;
    322 
    323 	case PCMCIA_CISTPL_VERS_1:
    324 		memcpy(cis->cis1_info_buf, tuple + 2, tuple[1]);
    325 		i = 0;
    326 		p = cis->cis1_info_buf + 2;
    327 		while (i <
    328 		    sizeof(cis->cis1_info) / sizeof(cis->cis1_info[0])) {
    329 			if (p >= cis->cis1_info_buf + tuple[1] || *p == '\xff')
    330 				break;
    331 			cis->cis1_info[i++] = p;
    332 			while (*p != '\0' && *p != '\xff')
    333 				p++;
    334 			if (*p == '\0')
    335 				p++;
    336 		}
    337 		break;
    338 
    339 	case PCMCIA_CISTPL_BAR:
    340 		if (tuple[1] != 6) {
    341 			DPRINTF(("%s: BAR with short length (%d)\n",
    342 			    __func__, tuple[1]));
    343 			break;
    344 		}
    345 		bar_index = tuple[2] & 7;
    346 		if (bar_index == 0) {
    347 			DPRINTF(("%s: invalid ASI in BAR tuple\n", __func__));
    348 			break;
    349 		}
    350 		bar_index--;
    351 		cis->bar[bar_index].flags = tuple[2];
    352 		cis->bar[bar_index].size =
    353 		    (tuple[4] << 0) |
    354 		    (tuple[5] << 8) |
    355 		    (tuple[6] << 16) |
    356 		    (tuple[7] << 24);
    357 		break;
    358 
    359 	case PCMCIA_CISTPL_FUNCID:
    360 		cis->funcid = tuple[2];
    361 		break;
    362 
    363 	case PCMCIA_CISTPL_FUNCE:
    364 		switch (cis->funcid) {
    365 		case PCMCIA_FUNCTION_SERIAL:
    366 			if (tuple[1] >= 2 &&
    367 			    /* XXX PCMCIA_TPLFE_TYPE_SERIAL_??? */
    368 			    tuple[2] == 0) {
    369 				cis->funce.serial.uart_type = tuple[3] & 0x1f;
    370 				cis->funce.serial.uart_present = 1;
    371 			}
    372 			break;
    373 
    374 		case PCMCIA_FUNCTION_NETWORK:
    375 			if (tuple[1] >= 8 &&
    376 			    tuple[2] == PCMCIA_TPLFE_TYPE_LAN_NID) {
    377 				if (tuple[3] >
    378 				    sizeof(cis->funce.network.netid)) {
    379 					DPRINTF(("%s: unknown network id type "
    380 					    "(len = %d)\n",
    381 					    __func__, tuple[3]));
    382 				} else {
    383 					cis->funce.network.netid_present = 1;
    384 					memcpy(cis->funce.network.netid,
    385 					    tuple + 4, tuple[3]);
    386 				}
    387 			}
    388 			break;
    389 		}
    390 		break;
    391 	}
    392 }
    393 
    394 /*
    395  * int cardbus_attach_card(struct cardbus_softc *sc)
    396  *
    397  *    This function attaches the card on the slot: turns on power,
    398  *    reads and analyses tuple, sets configuration index.
    399  *
    400  *    This function returns the number of recognised device functions.
    401  *    If no functions are recognised, return 0.
    402  */
    403 int
    404 cardbus_attach_card(struct cardbus_softc *sc)
    405 {
    406 	cardbus_chipset_tag_t cc;
    407 	cardbus_function_tag_t cf;
    408 	int cdstatus;
    409 	static int wildcard[CARDBUSCF_NLOCS] = {
    410 		CARDBUSCF_FUNCTION_DEFAULT
    411 	};
    412 
    413 	cc = sc->sc_cc;
    414 	cf = sc->sc_cf;
    415 
    416 	DPRINTF(("cardbus_attach_card: cb%d start\n",
    417 		 device_unit(&sc->sc_dev)));
    418 
    419 	/* inspect initial voltage */
    420 	if ((cdstatus = (*cf->cardbus_ctrl)(cc, CARDBUS_CD)) == 0) {
    421 		DPRINTF(("%s: no CardBus card on cb%d\n", __func__,
    422 		    device_unit(&sc->sc_dev)));
    423 		return (0);
    424 	}
    425 
    426 	device_pmf_driver_set_child_register(&sc->sc_dev, cardbus_child_register);
    427 	cardbus_rescan(&sc->sc_dev, "cardbus", wildcard);
    428 	return (1); /* XXX */
    429 }
    430 
    431 int
    432 cardbus_rescan(struct device *self, const char *ifattr,
    433     const int *locators)
    434 {
    435 	struct cardbus_softc *sc = device_private(self);
    436 	cardbus_chipset_tag_t cc;
    437 	cardbus_function_tag_t cf;
    438 	cardbustag_t tag;
    439 	cardbusreg_t id, class, cis_ptr;
    440 	cardbusreg_t bhlc, icr, lattimer;
    441 	int cdstatus;
    442 	int function, nfunction;
    443 	struct device *csc;
    444 	cardbus_devfunc_t ct;
    445 
    446 	cc = sc->sc_cc;
    447 	cf = sc->sc_cf;
    448 
    449 	/* inspect initial voltage */
    450 	if ((cdstatus = (*cf->cardbus_ctrl)(cc, CARDBUS_CD)) == 0) {
    451 		DPRINTF(("%s: no CardBus card on cb%d\n", __func__,
    452 		    device_unit(&sc->sc_dev)));
    453 		return (0);
    454 	}
    455 
    456 	/*
    457 	 * XXX use fake function 8 to keep power on during whole
    458 	 * configuration.
    459 	 */
    460 	enable_function(sc, cdstatus, 8);
    461 	function = 0;
    462 
    463 	tag = cardbus_make_tag(cc, cf, sc->sc_bus, function);
    464 
    465 	/*
    466 	 * Wait until power comes up.  Maxmum 500 ms.
    467 	 *
    468 	 * XXX What is this for?  The bridge driver ought to have waited
    469 	 * XXX already.
    470 	 */
    471 	{
    472 		int i;
    473 
    474 		for (i = 0; i < 5; ++i) {
    475 			id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
    476 			if (id != 0xffffffff && id != 0) {
    477 				break;
    478 			}
    479 			if (cold) {	/* before kernel thread invoked */
    480 				delay(100 * 1000);
    481 			} else {	/* thread context */
    482 				if (tsleep((void *)sc, PCATCH, "cardbus",
    483 				    hz / 10) != EWOULDBLOCK) {
    484 					break;
    485 				}
    486 			}
    487 		}
    488 		if (i == 5) {
    489 			return (EIO);
    490 		}
    491 	}
    492 
    493 	bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    494 	DPRINTF(("%s bhlc 0x%08x -> ", sc->sc_dev.dv_xname, bhlc));
    495 	nfunction = CARDBUS_HDRTYPE_MULTIFN(bhlc) ? 8 : 1;
    496 
    497 	for (function = 0; function < nfunction; function++) {
    498 		struct cardbus_attach_args ca;
    499 		int locs[CARDBUSCF_NLOCS];
    500 
    501 		if (locators[CARDBUSCF_FUNCTION] !=
    502 		    CARDBUSCF_FUNCTION_DEFAULT &&
    503 		    locators[CARDBUSCF_FUNCTION] != function)
    504 			continue;
    505 
    506 		if (sc->sc_funcs[function])
    507 			continue;
    508 
    509 		tag = cardbus_make_tag(cc, cf, sc->sc_bus, function);
    510 
    511 		id = cardbus_conf_read(cc, cf, tag, CARDBUS_ID_REG);
    512 		class = cardbus_conf_read(cc, cf, tag, CARDBUS_CLASS_REG);
    513 		cis_ptr = cardbus_conf_read(cc, cf, tag, CARDBUS_CIS_REG);
    514 
    515 		/* Invalid vendor ID value? */
    516 		if (CARDBUS_VENDOR(id) == PCI_VENDOR_INVALID) {
    517 			continue;
    518 		}
    519 
    520 		DPRINTF(("cardbus_attach_card: "
    521 		    "Vendor 0x%x, Product 0x%x, CIS 0x%x\n",
    522 		    CARDBUS_VENDOR(id), CARDBUS_PRODUCT(id), cis_ptr));
    523 
    524 		enable_function(sc, cdstatus, function);
    525 
    526 		/* clean up every BAR */
    527 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE0_REG, 0);
    528 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE1_REG, 0);
    529 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE2_REG, 0);
    530 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE3_REG, 0);
    531 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE4_REG, 0);
    532 		cardbus_conf_write(cc, cf, tag, CARDBUS_BASE5_REG, 0);
    533 		cardbus_conf_write(cc, cf, tag, CARDBUS_ROM_REG, 0);
    534 
    535 		/* set initial latency and cacheline size */
    536 		bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    537 		icr = cardbus_conf_read(cc, cf, tag, CARDBUS_INTERRUPT_REG);
    538 		DPRINTF(("%s func%d icr 0x%08x bhlc 0x%08x -> ",
    539 		    device_xname(&sc->sc_dev), function, icr, bhlc));
    540 		bhlc &= ~(CARDBUS_CACHELINE_MASK << CARDBUS_CACHELINE_SHIFT);
    541 		bhlc |= (sc->sc_cacheline & CARDBUS_CACHELINE_MASK) <<
    542 		    CARDBUS_CACHELINE_SHIFT;
    543 		/*
    544 		 * Set the initial value of the Latency Timer.
    545 		 *
    546 		 * While a PCI device owns the bus, its Latency
    547 		 * Timer counts down bus cycles from its initial
    548 		 * value to 0.  Minimum Grant tells for how long
    549 		 * the device wants to own the bus once it gets
    550 		 * access, in units of 250ns.
    551 		 *
    552 		 * On a 33 MHz bus, there are 8 cycles per 250ns.
    553 		 * So I multiply the Minimum Grant by 8 to find
    554 		 * out the initial value of the Latency Timer.
    555 		 *
    556 		 * Avoid setting a Latency Timer less than 0x10,
    557 		 * since the old code did not do that.
    558 		 */
    559 		lattimer =
    560 		    MIN(sc->sc_max_lattimer, MAX(0x10, 8 * PCI_MIN_GNT(icr)));
    561 		if (PCI_LATTIMER(bhlc) < lattimer) {
    562 			bhlc &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
    563 			bhlc |= (lattimer << PCI_LATTIMER_SHIFT);
    564 		}
    565 
    566 		cardbus_conf_write(cc, cf, tag, CARDBUS_BHLC_REG, bhlc);
    567 		bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    568 		DPRINTF(("0x%08x\n", bhlc));
    569 
    570 		/*
    571 		 * We need to allocate the ct here, since we might
    572 		 * need it when reading the CIS
    573 		 */
    574 		if ((ct = malloc(sizeof(struct cardbus_devfunc),
    575 		    M_DEVBUF, M_NOWAIT)) == NULL) {
    576 			panic("no room for cardbus_tag");
    577 		}
    578 
    579 		ct->ct_bhlc = bhlc;
    580 		ct->ct_cc = sc->sc_cc;
    581 		ct->ct_cf = sc->sc_cf;
    582 		ct->ct_bus = sc->sc_bus;
    583 		ct->ct_func = function;
    584 		ct->ct_sc = sc;
    585 		sc->sc_funcs[function] = ct;
    586 
    587 		memset(&ca, 0, sizeof(ca));
    588 
    589 		ca.ca_ct = ct;
    590 
    591 		ca.ca_iot = sc->sc_iot;
    592 		ca.ca_memt = sc->sc_memt;
    593 		ca.ca_dmat = sc->sc_dmat;
    594 
    595 #if rbus
    596 		ca.ca_rbus_iot = sc->sc_rbus_iot;
    597 		ca.ca_rbus_memt= sc->sc_rbus_memt;
    598 #endif
    599 
    600 		ca.ca_tag = tag;
    601 		ca.ca_bus = sc->sc_bus;
    602 		ca.ca_function = function;
    603 		ca.ca_id = id;
    604 		ca.ca_class = class;
    605 
    606 		ca.ca_intrline = sc->sc_intrline;
    607 
    608 		if (cis_ptr != 0) {
    609 #define TUPLESIZE 2048
    610 			u_int8_t *tuple = malloc(TUPLESIZE, M_DEVBUF, M_WAITOK);
    611 			if (cardbus_read_tuples(&ca, cis_ptr,
    612 			    tuple, TUPLESIZE)) {
    613 				printf("cardbus_attach_card: "
    614 				    "failed to read CIS\n");
    615 			} else {
    616 #ifdef CARDBUS_DEBUG
    617 				decode_tuples(tuple, TUPLESIZE,
    618 				    print_tuple, NULL);
    619 #endif
    620 				decode_tuples(tuple, TUPLESIZE,
    621 				    parse_tuple, &ca.ca_cis);
    622 			}
    623 			free(tuple, M_DEVBUF);
    624 		}
    625 
    626 		locs[CARDBUSCF_FUNCTION] = function;
    627 
    628 		if ((csc = config_found_sm_loc((void *)sc, "cardbus", locs,
    629 		    &ca, cardbusprint, config_stdsubmatch)) == NULL) {
    630 			/* do not match */
    631 			disable_function(sc, function);
    632 			sc->sc_funcs[function] = NULL;
    633 			free(ct, M_DEVBUF);
    634 		} else {
    635 			/* found */
    636 			ct->ct_device = csc;
    637 		}
    638 	}
    639 	/*
    640 	 * XXX power down pseudo function 8 (this will power down the card
    641 	 * if no functions were attached).
    642 	 */
    643 	disable_function(sc, 8);
    644 
    645 	return (0);
    646 }
    647 
    648 static int
    649 cardbusprint(void *aux, const char *pnp)
    650 {
    651 	struct cardbus_attach_args *ca = aux;
    652 	char devinfo[256];
    653 	int i;
    654 
    655 	if (pnp) {
    656 		pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo,
    657 		    sizeof(devinfo));
    658 		for (i = 0; i < 4; i++) {
    659 			if (ca->ca_cis.cis1_info[i] == NULL)
    660 				break;
    661 			if (i)
    662 				aprint_normal(", ");
    663 			aprint_normal("%s", ca->ca_cis.cis1_info[i]);
    664 		}
    665 		aprint_verbose("%s(manufacturer 0x%x, product 0x%x)",
    666 		    i ? " " : "",
    667 		    ca->ca_cis.manufacturer, ca->ca_cis.product);
    668 		aprint_normal(" %s at %s", devinfo, pnp);
    669 	}
    670 	aprint_normal(" function %d", ca->ca_function);
    671 
    672 	return (UNCONF);
    673 }
    674 
    675 /*
    676  * void cardbus_detach_card(struct cardbus_softc *sc)
    677  *
    678  *    This function detaches the card on the slot: detach device data
    679  *    structure and turns off the power.
    680  *
    681  *    This function must not be called under interrupt context.
    682  */
    683 void
    684 cardbus_detach_card(struct cardbus_softc *sc)
    685 {
    686 	int f;
    687 	struct cardbus_devfunc *ct;
    688 
    689 	for (f = 0; f < 8; f++) {
    690 		ct = sc->sc_funcs[f];
    691 		if (!ct)
    692 			continue;
    693 
    694 		DPRINTF(("%s: detaching %s\n", sc->sc_dev.dv_xname,
    695 		    ct->ct_device->dv_xname));
    696 		/* call device detach function */
    697 
    698 		if (config_detach(ct->ct_device, 0) != 0) {
    699 			printf("%s: cannot detach dev %s, function %d\n",
    700 			    sc->sc_dev.dv_xname, ct->ct_device->dv_xname,
    701 			    ct->ct_func);
    702 		}
    703 	}
    704 
    705 	sc->sc_poweron_func = 0;
    706 	(*sc->sc_cf->cardbus_power)(sc->sc_cc,
    707 	    CARDBUS_VCC_0V | CARDBUS_VPP_0V);
    708 }
    709 
    710 void
    711 cardbus_childdetached(struct device *self, struct device *child)
    712 {
    713 	struct cardbus_softc *sc = device_private(self);
    714 	struct cardbus_devfunc *ct;
    715 
    716 	ct = sc->sc_funcs[device_locator(child, CARDBUSCF_FUNCTION)];
    717 	KASSERT(ct->ct_device == child);
    718 
    719 	sc->sc_poweron_func &= ~(1 << ct->ct_func);
    720 	sc->sc_funcs[ct->ct_func] = NULL;
    721 	free(ct, M_DEVBUF);
    722 }
    723 
    724 /*
    725  * void *cardbus_intr_establish(cc, cf, irq, level, func, arg)
    726  *   Interrupt handler of pccard.
    727  *  args:
    728  *   cardbus_chipset_tag_t *cc
    729  *   int irq:
    730  */
    731 void *
    732 cardbus_intr_establish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
    733     cardbus_intr_handle_t irq, int level, int (*func)(void *), void *arg)
    734 {
    735 
    736 	DPRINTF(("- cardbus_intr_establish: irq %d\n", irq));
    737 	return ((*cf->cardbus_intr_establish)(cc, irq, level, func, arg));
    738 }
    739 
    740 /*
    741  * void cardbus_intr_disestablish(cc, cf, handler)
    742  *   Interrupt handler of pccard.
    743  *  args:
    744  *   cardbus_chipset_tag_t *cc
    745  */
    746 void
    747 cardbus_intr_disestablish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
    748     void *handler)
    749 {
    750 
    751 	DPRINTF(("- pccard_intr_disestablish\n"));
    752 	(*cf->cardbus_intr_disestablish)(cc, handler);
    753 }
    754 
    755 /*
    756  * XXX this should be merged with cardbus_function_{enable,disable},
    757  * but we don't have a ct when these functions are called.
    758  */
    759 static void
    760 enable_function(struct cardbus_softc *sc, int cdstatus, int function)
    761 {
    762 
    763 	if (sc->sc_poweron_func == 0) {
    764 		/* switch to 3V and/or wait for power to stabilize */
    765 		if (cdstatus & CARDBUS_3V_CARD) {
    766 			/*
    767 			 * sc_poweron_func must be substituted before
    768 			 * entering sleep, in order to avoid turn on
    769 			 * power twice.
    770 			 */
    771 			sc->sc_poweron_func |= (1 << function);
    772 			(*sc->sc_cf->cardbus_power)(sc->sc_cc, CARDBUS_VCC_3V);
    773 		} else {
    774 			/* No cards other than 3.3V cards. */
    775 			return;
    776 		}
    777 		(*sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_RESET);
    778 	}
    779 	sc->sc_poweron_func |= (1 << function);
    780 }
    781 
    782 static void
    783 disable_function(struct cardbus_softc *sc, int function)
    784 {
    785 
    786 	sc->sc_poweron_func &= ~(1 << function);
    787 	if (sc->sc_poweron_func == 0) {
    788 		/* power-off because no functions are enabled */
    789 		(*sc->sc_cf->cardbus_power)(sc->sc_cc, CARDBUS_VCC_0V);
    790 	}
    791 }
    792 
    793 /*
    794  * int cardbus_function_enable(struct cardbus_softc *sc, int func)
    795  *
    796  *   This function enables a function on a card.  When no power is
    797  *  applied on the card, power will be applied on it.
    798  */
    799 int
    800 cardbus_function_enable(struct cardbus_softc *sc, int func)
    801 {
    802 	cardbus_chipset_tag_t cc = sc->sc_cc;
    803 	cardbus_function_tag_t cf = sc->sc_cf;
    804 	cardbus_devfunc_t ct;
    805 	cardbusreg_t command;
    806 	cardbustag_t tag;
    807 
    808 	DPRINTF(("entering cardbus_function_enable...  "));
    809 
    810 	/* entering critical area */
    811 
    812 	/* XXX: sc_vold should be used */
    813 	enable_function(sc, CARDBUS_3V_CARD, func);
    814 
    815 	/* exiting critical area */
    816 
    817 	tag = cardbus_make_tag(cc, cf, sc->sc_bus, func);
    818 
    819 	command = cardbus_conf_read(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG);
    820 	command |= (CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_IO_ENABLE |
    821 	    CARDBUS_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */
    822 
    823 	cardbus_conf_write(cc, cf, tag, CARDBUS_COMMAND_STATUS_REG, command);
    824 
    825 	if ((ct = sc->sc_funcs[func]) != NULL)
    826 		Cardbus_conf_write(ct, tag, CARDBUS_BHLC_REG, ct->ct_bhlc);
    827 
    828 	cardbus_free_tag(cc, cf, tag);
    829 
    830 	DPRINTF(("%x\n", sc->sc_poweron_func));
    831 
    832 	return (0);
    833 }
    834 
    835 /*
    836  * int cardbus_function_disable(struct cardbus_softc *, int func)
    837  *
    838  *   This function disable a function on a card.  When no functions are
    839  *  enabled, it turns off the power.
    840  */
    841 int
    842 cardbus_function_disable(struct cardbus_softc *sc, int func)
    843 {
    844 
    845 	DPRINTF(("entering cardbus_function_disable...  "));
    846 
    847 	disable_function(sc, func);
    848 
    849 	return (0);
    850 }
    851 
    852 /*
    853  * int cardbus_get_capability(cardbus_chipset_tag_t cc,
    854  *	cardbus_function_tag_t cf, cardbustag_t tag, int capid, int *offset,
    855  *	cardbusreg_t *value)
    856  *
    857  *	Find the specified PCI capability.
    858  */
    859 int
    860 cardbus_get_capability(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
    861     cardbustag_t tag, int capid, int *offset, cardbusreg_t *value)
    862 {
    863 	cardbusreg_t reg;
    864 	unsigned int ofs;
    865 
    866 	reg = cardbus_conf_read(cc, cf, tag, PCI_COMMAND_STATUS_REG);
    867 	if (!(reg & PCI_STATUS_CAPLIST_SUPPORT))
    868 		return (0);
    869 
    870 	ofs = PCI_CAPLIST_PTR(cardbus_conf_read(cc, cf, tag,
    871 	    PCI_CAPLISTPTR_REG));
    872 	while (ofs != 0) {
    873 #ifdef DIAGNOSTIC
    874 		if ((ofs & 3) || (ofs < 0x40))
    875 			panic("cardbus_get_capability");
    876 #endif
    877 		reg = cardbus_conf_read(cc, cf, tag, ofs);
    878 		if (PCI_CAPLIST_CAP(reg) == capid) {
    879 			if (offset)
    880 				*offset = ofs;
    881 			if (value)
    882 				*value = reg;
    883 			return (1);
    884 		}
    885 		ofs = PCI_CAPLIST_NEXT(reg);
    886 	}
    887 
    888 	return (0);
    889 }
    890 
    891 /*
    892  * below this line, there are some functions for decoding tuples.
    893  * They should go out from this file.
    894  */
    895 
    896 static u_int8_t *
    897 decode_tuple(u_int8_t *, u_int8_t *, tuple_decode_func, void *);
    898 
    899 static int
    900 decode_tuples(u_int8_t *tuple, int buflen, tuple_decode_func func, void *data)
    901 {
    902 	u_int8_t *tp = tuple;
    903 
    904 	if (PCMCIA_CISTPL_LINKTARGET != *tuple) {
    905 		DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple));
    906 		return (0);
    907 	}
    908 
    909 	while ((tp = decode_tuple(tp, tuple + buflen, func, data)) != NULL)
    910 		;
    911 
    912 	return (1);
    913 }
    914 
    915 static u_int8_t *
    916 decode_tuple(u_int8_t *tuple, u_int8_t *end,
    917     tuple_decode_func func, void *data)
    918 {
    919 	u_int8_t type;
    920 	u_int8_t len;
    921 
    922 	type = tuple[0];
    923 	switch (type) {
    924 	case PCMCIA_CISTPL_NULL:
    925 	case PCMCIA_CISTPL_END:
    926 		len = 1;
    927 		break;
    928 	default:
    929 		if (tuple + 2 > end)
    930 			return (NULL);
    931 		len = tuple[1] + 2;
    932 		break;
    933 	}
    934 
    935 	if (tuple + len > end)
    936 		return (NULL);
    937 
    938 	(*func)(tuple, len, data);
    939 
    940 	if (type == PCMCIA_CISTPL_END || tuple + len == end)
    941 		return (NULL);
    942 
    943 	return (tuple + len);
    944 }
    945 
    946 /*
    947  * XXX: this is another reason why this code should be shared with PCI.
    948  */
    949 static int
    950 cardbus_get_powerstate_int(cardbus_devfunc_t ct, cardbustag_t tag,
    951     cardbusreg_t *state, int offset)
    952 {
    953 	cardbusreg_t value, now;
    954 	cardbus_chipset_tag_t cc = ct->ct_cc;
    955 	cardbus_function_tag_t cf = ct->ct_cf;
    956 
    957 	value = cardbus_conf_read(cc, cf, tag, offset + PCI_PMCSR);
    958 	now = value & PCI_PMCSR_STATE_MASK;
    959 	switch (now) {
    960 	case PCI_PMCSR_STATE_D0:
    961 	case PCI_PMCSR_STATE_D1:
    962 	case PCI_PMCSR_STATE_D2:
    963 	case PCI_PMCSR_STATE_D3:
    964 		*state = now;
    965 		return 0;
    966 	default:
    967 		return EINVAL;
    968 	}
    969 }
    970 
    971 int
    972 cardbus_get_powerstate(cardbus_devfunc_t ct, cardbustag_t tag,
    973     cardbusreg_t *state)
    974 {
    975 	cardbus_chipset_tag_t cc = ct->ct_cc;
    976 	cardbus_function_tag_t cf = ct->ct_cf;
    977 	int offset;
    978 	cardbusreg_t value;
    979 
    980 	if (!cardbus_get_capability(cc, cf, tag, PCI_CAP_PWRMGMT, &offset, &value))
    981 		return EOPNOTSUPP;
    982 
    983 	return cardbus_get_powerstate_int(ct, tag, state, offset);
    984 }
    985 
    986 static int
    987 cardbus_set_powerstate_int(cardbus_devfunc_t ct, cardbustag_t tag,
    988     cardbusreg_t state, int offset, cardbusreg_t cap_reg)
    989 {
    990 	cardbus_chipset_tag_t cc = ct->ct_cc;
    991 	cardbus_function_tag_t cf = ct->ct_cf;
    992 
    993 	cardbusreg_t value, cap, now;
    994 
    995 	cap = cap_reg >> PCI_PMCR_SHIFT;
    996 	value = cardbus_conf_read(cc, cf, tag, offset + PCI_PMCSR);
    997 	now = value & PCI_PMCSR_STATE_MASK;
    998 	value &= ~PCI_PMCSR_STATE_MASK;
    999 
   1000 	if (now == state)
   1001 		return 0;
   1002 	switch (state) {
   1003 	case PCI_PMCSR_STATE_D0:
   1004 		value |= PCI_PMCSR_STATE_D0;
   1005 		break;
   1006 	case PCI_PMCSR_STATE_D1:
   1007 		if (now == PCI_PMCSR_STATE_D2 || now == PCI_PMCSR_STATE_D3) {
   1008 			printf("invalid transition from %d to D1\n", (int)now);
   1009 			return EINVAL;
   1010 		}
   1011 		if (!(cap & PCI_PMCR_D1SUPP)) {
   1012 			printf("D1 not supported\n");
   1013 			return EOPNOTSUPP;
   1014 		}
   1015 		value |= PCI_PMCSR_STATE_D1;
   1016 		break;
   1017 	case PCI_PMCSR_STATE_D2:
   1018 		if (now == PCI_PMCSR_STATE_D3) {
   1019 			printf("invalid transition from %d to D2\n", (int)now);
   1020 			return EINVAL;
   1021 		}
   1022 		if (!(cap & PCI_PMCR_D2SUPP)) {
   1023 			printf("D2 not supported\n");
   1024 			return EOPNOTSUPP;
   1025 		}
   1026 		value |= PCI_PMCSR_STATE_D2;
   1027 		break;
   1028 	case PCI_PMCSR_STATE_D3:
   1029 		value |= PCI_PMCSR_STATE_D3;
   1030 		break;
   1031 	default:
   1032 		return EINVAL;
   1033 	}
   1034 
   1035 	cardbus_conf_write(cc, cf, tag, offset + PCI_PMCSR, value);
   1036 	DELAY(1000);
   1037 
   1038 	return 0;
   1039 }
   1040 
   1041 int
   1042 cardbus_set_powerstate(cardbus_devfunc_t ct, cardbustag_t tag, cardbusreg_t state)
   1043 {
   1044 	cardbus_chipset_tag_t cc = ct->ct_cc;
   1045 	cardbus_function_tag_t cf = ct->ct_cf;
   1046 	int offset;
   1047 	cardbusreg_t value;
   1048 
   1049 	if (!cardbus_get_capability(cc, cf, tag, PCI_CAP_PWRMGMT, &offset,
   1050 	    &value))
   1051 		return EOPNOTSUPP;
   1052 
   1053 	return cardbus_set_powerstate_int(ct, tag, state, offset, value);
   1054 }
   1055 
   1056 #ifdef CARDBUS_DEBUG
   1057 static const char *tuple_name(int);
   1058 static const char *tuple_names[] = {
   1059 	"TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */
   1060 	"CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */
   1061 	"Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */
   1062 	"Reserved", "Reserved", "Reserved", "Reserved", /* C-F */
   1063 	"CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET", /* 10-13 */
   1064 	"NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A",
   1065 	"JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY",
   1066 	"DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", "DEVICE_GEO_A",
   1067 	"MANFID", "FUNCID", "FUNCE", "SWIL", /* 20-23 */
   1068 	"Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */
   1069 	"Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */
   1070 	"Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */
   1071 	"Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */
   1072 	"Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */
   1073 	"Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */
   1074 	"Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */
   1075 	"VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER",
   1076 	"DATE", "BATTERY", "ORG"
   1077 };
   1078 #define NAME_LEN(x) (sizeof x / sizeof(x[0]))
   1079 
   1080 static const char *
   1081 tuple_name(int type)
   1082 {
   1083 
   1084 	if (0 <= type && type < NAME_LEN(tuple_names)) {
   1085 		return (tuple_names[type]);
   1086 	} else if (type == 0xff) {
   1087 		return ("END");
   1088 	} else {
   1089 		return ("Reserved");
   1090 	}
   1091 }
   1092 
   1093 static void
   1094 print_tuple(u_int8_t *tuple, int len, void *data)
   1095 {
   1096 	int i;
   1097 
   1098 	printf("tuple: %s len %d\n", tuple_name(tuple[0]), len);
   1099 
   1100 	for (i = 0; i < len; ++i) {
   1101 		if (i % 16 == 0) {
   1102 			printf("  0x%2x:", i);
   1103 		}
   1104 		printf(" %x", tuple[i]);
   1105 		if (i % 16 == 15) {
   1106 			printf("\n");
   1107 		}
   1108 	}
   1109 	if (i % 16 != 0) {
   1110 		printf("\n");
   1111 	}
   1112 }
   1113 #endif
   1114 
   1115 void
   1116 cardbus_conf_capture(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
   1117     cardbustag_t tag, struct cardbus_conf_state *pcs)
   1118 {
   1119 	int off;
   1120 
   1121 	for (off = 0; off < 16; off++)
   1122 		pcs->reg[off] = cardbus_conf_read(cc, cf, tag, (off * 4));
   1123 }
   1124 
   1125 void
   1126 cardbus_conf_restore(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
   1127     cardbustag_t tag, struct cardbus_conf_state *pcs)
   1128 {
   1129 	int off;
   1130 	cardbusreg_t val;
   1131 
   1132 	for (off = 15; off >= 0; off--) {
   1133 		val = cardbus_conf_read(cc, cf, tag, (off * 4));
   1134 		if (val != pcs->reg[off])
   1135 			cardbus_conf_write(cc, cf,tag, (off * 4), pcs->reg[off]);
   1136 	}
   1137 }
   1138 
   1139 void
   1140 cardbus_disable_retry(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf,
   1141     cardbustag_t tag)
   1142 {
   1143 	cardbusreg_t retry;
   1144 
   1145 	/*
   1146 	 * Disable retry timeout to keep PCI Tx retries from
   1147 	 * interfering with ACPI C3 CPU state.
   1148 	 */
   1149 	retry = cardbus_conf_read(cc, cf, tag, PCI_RETRY_TIMEOUT_REG);
   1150 	retry &= ~PCI_RETRY_TIMEOUT_REG_MASK;
   1151 	cardbus_conf_write(cc, cf, tag, PCI_RETRY_TIMEOUT_REG, retry);
   1152 }
   1153 
   1154 struct cardbus_child_power {
   1155 	struct cardbus_conf_state p_cardbusconf;
   1156 	cardbus_devfunc_t p_ct;
   1157 	cardbustag_t p_tag;
   1158 	cardbus_chipset_tag_t p_cc;
   1159 	cardbus_function_tag_t p_cf;
   1160 	cardbusreg_t p_pm_cap;
   1161 	bool p_has_pm;
   1162 	int p_pm_offset;
   1163 };
   1164 
   1165 static bool
   1166 cardbus_child_suspend(device_t dv)
   1167 {
   1168 	struct cardbus_child_power *priv = device_pmf_bus_private(dv);
   1169 
   1170 	cardbus_conf_capture(priv->p_cc, priv->p_cf, priv->p_tag,
   1171 	    &priv->p_cardbusconf);
   1172 
   1173 	if (priv->p_has_pm &&
   1174 	    cardbus_set_powerstate_int(priv->p_ct, priv->p_tag,
   1175 	    PCI_PMCSR_STATE_D3, priv->p_pm_offset, priv->p_pm_cap)) {
   1176 		aprint_error_dev(dv, "unsupported state, continuing.\n");
   1177 		return false;
   1178 	}
   1179 
   1180 	Cardbus_function_disable(priv->p_ct);
   1181 
   1182 	return true;
   1183 }
   1184 
   1185 static bool
   1186 cardbus_child_resume(device_t dv)
   1187 {
   1188 	struct cardbus_child_power *priv = device_pmf_bus_private(dv);
   1189 
   1190 	Cardbus_function_enable(priv->p_ct);
   1191 
   1192 	if (priv->p_has_pm &&
   1193 	    cardbus_set_powerstate_int(priv->p_ct, priv->p_tag,
   1194 	    PCI_PMCSR_STATE_D0, priv->p_pm_offset, priv->p_pm_cap)) {
   1195 		aprint_error_dev(dv, "unsupported state, continuing.\n");
   1196 		return false;
   1197 	}
   1198 
   1199 	cardbus_conf_restore(priv->p_cc, priv->p_cf, priv->p_tag,
   1200 	    &priv->p_cardbusconf);
   1201 
   1202 	return true;
   1203 }
   1204 
   1205 static void
   1206 cardbus_child_deregister(device_t dv)
   1207 {
   1208 	struct cardbus_child_power *priv = device_pmf_bus_private(dv);
   1209 
   1210 	free(priv, M_DEVBUF);
   1211 }
   1212 
   1213 static bool
   1214 cardbus_child_register(device_t child)
   1215 {
   1216 	device_t self = device_parent(child);
   1217 	struct cardbus_softc *sc = device_private(self);
   1218 	struct cardbus_devfunc *ct;
   1219 	struct cardbus_child_power *priv;
   1220 	int off;
   1221 	cardbusreg_t reg;
   1222 
   1223 	ct = sc->sc_funcs[device_locator(child, CARDBUSCF_FUNCTION)];
   1224 
   1225 	priv = malloc(sizeof(*priv), M_DEVBUF, M_WAITOK);
   1226 
   1227 	priv->p_ct = ct;
   1228 	priv->p_cc = ct->ct_cc;
   1229 	priv->p_cf = ct->ct_cf;
   1230 	priv->p_tag = cardbus_make_tag(priv->p_cc, priv->p_cf, ct->ct_bus,
   1231 	    ct->ct_func);
   1232 
   1233 	if (cardbus_get_capability(priv->p_cc, priv->p_cf, priv->p_tag,
   1234 				   PCI_CAP_PWRMGMT, &off, &reg)) {
   1235 		priv->p_has_pm = true;
   1236 		priv->p_pm_offset = off;
   1237 		priv->p_pm_cap = reg;
   1238 	} else {
   1239 		priv->p_has_pm = false;
   1240 		priv->p_pm_offset = -1;
   1241 	}
   1242 
   1243 	device_pmf_bus_register(child, priv, cardbus_child_suspend,
   1244 	    cardbus_child_resume, cardbus_child_deregister);
   1245 
   1246 	return true;
   1247 }
   1248