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