Home | History | Annotate | Line # | Download | only in pci
pci_map.c revision 1.29.4.1
      1  1.29.4.1      yamt /*	$NetBSD: pci_map.c,v 1.29.4.1 2012/10/30 17:21:37 yamt Exp $	*/
      2       1.1   mycroft 
      3       1.5   mycroft /*-
      4       1.7   thorpej  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
      5       1.5   mycroft  * All rights reserved.
      6       1.5   mycroft  *
      7       1.5   mycroft  * This code is derived from software contributed to The NetBSD Foundation
      8       1.7   thorpej  * by Charles M. Hannum; by William R. Studenmund; by Jason R. Thorpe.
      9       1.1   mycroft  *
     10       1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     11       1.1   mycroft  * modification, are permitted provided that the following conditions
     12       1.1   mycroft  * are met:
     13       1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     14       1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     15       1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     17       1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     18       1.1   mycroft  *
     19       1.5   mycroft  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.5   mycroft  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.5   mycroft  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.5   mycroft  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.5   mycroft  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.5   mycroft  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.5   mycroft  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.5   mycroft  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.5   mycroft  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.5   mycroft  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.5   mycroft  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1   mycroft  */
     31       1.1   mycroft 
     32       1.1   mycroft /*
     33       1.1   mycroft  * PCI device mapping.
     34       1.1   mycroft  */
     35      1.10     lukem 
     36      1.10     lukem #include <sys/cdefs.h>
     37  1.29.4.1      yamt __KERNEL_RCSID(0, "$NetBSD: pci_map.c,v 1.29.4.1 2012/10/30 17:21:37 yamt Exp $");
     38       1.1   mycroft 
     39       1.1   mycroft #include <sys/param.h>
     40       1.1   mycroft #include <sys/systm.h>
     41       1.1   mycroft #include <sys/device.h>
     42       1.1   mycroft 
     43       1.1   mycroft #include <dev/pci/pcireg.h>
     44       1.1   mycroft #include <dev/pci/pcivar.h>
     45       1.1   mycroft 
     46      1.28    dyoung static int pci_mapreg_submap(const struct pci_attach_args *, int, pcireg_t, int,
     47      1.28    dyoung     bus_size_t, bus_size_t, bus_space_tag_t *, bus_space_handle_t *,
     48      1.28    dyoung     bus_addr_t *, bus_size_t *);
     49      1.28    dyoung 
     50       1.1   mycroft static int
     51      1.18  christos pci_io_find(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t type,
     52       1.9   thorpej     bus_addr_t *basep, bus_size_t *sizep, int *flagsp)
     53       1.1   mycroft {
     54       1.1   mycroft 	pcireg_t address, mask;
     55       1.1   mycroft 	int s;
     56       1.1   mycroft 
     57       1.8   thorpej 	if (reg < PCI_MAPREG_START ||
     58       1.8   thorpej #if 0
     59       1.8   thorpej 	    /*
     60       1.8   thorpej 	     * Can't do this check; some devices have mapping registers
     61       1.8   thorpej 	     * way out in left field.
     62       1.8   thorpej 	     */
     63       1.8   thorpej 	    reg >= PCI_MAPREG_END ||
     64       1.8   thorpej #endif
     65       1.8   thorpej 	    (reg & 3))
     66       1.1   mycroft 		panic("pci_io_find: bad request");
     67       1.1   mycroft 
     68       1.1   mycroft 	/*
     69       1.1   mycroft 	 * Section 6.2.5.1, `Address Maps', tells us that:
     70       1.1   mycroft 	 *
     71       1.1   mycroft 	 * 1) The builtin software should have already mapped the device in a
     72       1.1   mycroft 	 * reasonable way.
     73       1.1   mycroft 	 *
     74       1.1   mycroft 	 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
     75       1.1   mycroft 	 * n bits of the address to 0.  As recommended, we write all 1s and see
     76       1.1   mycroft 	 * what we get back.
     77       1.1   mycroft 	 */
     78       1.1   mycroft 	s = splhigh();
     79       1.1   mycroft 	address = pci_conf_read(pc, tag, reg);
     80       1.1   mycroft 	pci_conf_write(pc, tag, reg, 0xffffffff);
     81       1.1   mycroft 	mask = pci_conf_read(pc, tag, reg);
     82       1.1   mycroft 	pci_conf_write(pc, tag, reg, address);
     83       1.1   mycroft 	splx(s);
     84       1.1   mycroft 
     85       1.1   mycroft 	if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_IO) {
     86      1.22  jmcneill 		aprint_debug("pci_io_find: expected type i/o, found mem\n");
     87      1.25    dyoung 		return 1;
     88       1.1   mycroft 	}
     89       1.1   mycroft 
     90       1.2   mycroft 	if (PCI_MAPREG_IO_SIZE(mask) == 0) {
     91      1.19  macallan 		aprint_debug("pci_io_find: void region\n");
     92      1.25    dyoung 		return 1;
     93       1.2   mycroft 	}
     94       1.2   mycroft 
     95      1.25    dyoung 	if (basep != NULL)
     96       1.1   mycroft 		*basep = PCI_MAPREG_IO_ADDR(address);
     97      1.25    dyoung 	if (sizep != NULL)
     98       1.1   mycroft 		*sizep = PCI_MAPREG_IO_SIZE(mask);
     99      1.25    dyoung 	if (flagsp != NULL)
    100       1.1   mycroft 		*flagsp = 0;
    101       1.1   mycroft 
    102      1.25    dyoung 	return 0;
    103       1.1   mycroft }
    104       1.1   mycroft 
    105       1.1   mycroft static int
    106       1.9   thorpej pci_mem_find(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t type,
    107       1.9   thorpej     bus_addr_t *basep, bus_size_t *sizep, int *flagsp)
    108       1.1   mycroft {
    109       1.7   thorpej 	pcireg_t address, mask, address1 = 0, mask1 = 0xffffffff;
    110       1.7   thorpej 	u_int64_t waddress, wmask;
    111      1.15   gdamore 	int s, is64bit, isrom;
    112       1.7   thorpej 
    113       1.7   thorpej 	is64bit = (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT);
    114      1.15   gdamore 	isrom = (reg == PCI_MAPREG_ROM);
    115       1.1   mycroft 
    116      1.15   gdamore 	if ((!isrom) && (reg < PCI_MAPREG_START ||
    117       1.8   thorpej #if 0
    118       1.8   thorpej 	    /*
    119       1.8   thorpej 	     * Can't do this check; some devices have mapping registers
    120       1.8   thorpej 	     * way out in left field.
    121       1.8   thorpej 	     */
    122       1.8   thorpej 	    reg >= PCI_MAPREG_END ||
    123       1.8   thorpej #endif
    124      1.15   gdamore 	    (reg & 3)))
    125       1.7   thorpej 		panic("pci_mem_find: bad request");
    126       1.7   thorpej 
    127       1.7   thorpej 	if (is64bit && (reg + 4) >= PCI_MAPREG_END)
    128       1.7   thorpej 		panic("pci_mem_find: bad 64-bit request");
    129       1.1   mycroft 
    130       1.1   mycroft 	/*
    131       1.1   mycroft 	 * Section 6.2.5.1, `Address Maps', tells us that:
    132       1.1   mycroft 	 *
    133       1.1   mycroft 	 * 1) The builtin software should have already mapped the device in a
    134       1.1   mycroft 	 * reasonable way.
    135       1.1   mycroft 	 *
    136       1.1   mycroft 	 * 2) A device which wants 2^n bytes of memory will hardwire the bottom
    137       1.1   mycroft 	 * n bits of the address to 0.  As recommended, we write all 1s and see
    138  1.29.4.1      yamt 	 * what we get back.  Only probe the upper BAR of a mem64 BAR if bit 31
    139  1.29.4.1      yamt 	 * is readonly.
    140       1.1   mycroft 	 */
    141       1.1   mycroft 	s = splhigh();
    142       1.1   mycroft 	address = pci_conf_read(pc, tag, reg);
    143       1.1   mycroft 	pci_conf_write(pc, tag, reg, 0xffffffff);
    144       1.1   mycroft 	mask = pci_conf_read(pc, tag, reg);
    145       1.1   mycroft 	pci_conf_write(pc, tag, reg, address);
    146       1.7   thorpej 	if (is64bit) {
    147       1.7   thorpej 		address1 = pci_conf_read(pc, tag, reg + 4);
    148  1.29.4.1      yamt 		if ((mask & 0x80000000) == 0) {
    149  1.29.4.1      yamt 			pci_conf_write(pc, tag, reg + 4, 0xffffffff);
    150  1.29.4.1      yamt 			mask1 = pci_conf_read(pc, tag, reg + 4);
    151  1.29.4.1      yamt 			pci_conf_write(pc, tag, reg + 4, address1);
    152  1.29.4.1      yamt 		}
    153       1.7   thorpej 	}
    154       1.1   mycroft 	splx(s);
    155       1.1   mycroft 
    156      1.15   gdamore 	if (!isrom) {
    157      1.15   gdamore 		/*
    158      1.15   gdamore 		 * roms should have an enable bit instead of a memory
    159      1.15   gdamore 		 * type decoder bit.  For normal BARs, make sure that
    160      1.15   gdamore 		 * the address decoder type matches what we asked for.
    161      1.15   gdamore 		 */
    162      1.15   gdamore 		if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_MEM) {
    163      1.15   gdamore 			printf("pci_mem_find: expected type mem, found i/o\n");
    164      1.25    dyoung 			return 1;
    165      1.15   gdamore 		}
    166      1.21     joerg 		/* XXX Allow 64bit bars for 32bit requests.*/
    167      1.15   gdamore 		if (PCI_MAPREG_MEM_TYPE(address) !=
    168      1.21     joerg 		    PCI_MAPREG_MEM_TYPE(type) &&
    169      1.21     joerg 		    PCI_MAPREG_MEM_TYPE(address) !=
    170      1.21     joerg 		    PCI_MAPREG_MEM_TYPE_64BIT) {
    171      1.15   gdamore 			printf("pci_mem_find: "
    172      1.15   gdamore 			    "expected mem type %08x, found %08x\n",
    173      1.15   gdamore 			    PCI_MAPREG_MEM_TYPE(type),
    174      1.15   gdamore 			    PCI_MAPREG_MEM_TYPE(address));
    175      1.25    dyoung 			return 1;
    176      1.15   gdamore 		}
    177       1.2   mycroft 	}
    178       1.2   mycroft 
    179       1.7   thorpej 	waddress = (u_int64_t)address1 << 32UL | address;
    180       1.7   thorpej 	wmask = (u_int64_t)mask1 << 32UL | mask;
    181       1.7   thorpej 
    182      1.11    bouyer 	if ((is64bit && PCI_MAPREG_MEM64_SIZE(wmask) == 0) ||
    183      1.11    bouyer 	    (!is64bit && PCI_MAPREG_MEM_SIZE(mask) == 0)) {
    184      1.27  jmcneill 		aprint_debug("pci_mem_find: void region\n");
    185      1.25    dyoung 		return 1;
    186       1.1   mycroft 	}
    187       1.1   mycroft 
    188       1.1   mycroft 	switch (PCI_MAPREG_MEM_TYPE(address)) {
    189       1.1   mycroft 	case PCI_MAPREG_MEM_TYPE_32BIT:
    190       1.1   mycroft 	case PCI_MAPREG_MEM_TYPE_32BIT_1M:
    191       1.1   mycroft 		break;
    192       1.1   mycroft 	case PCI_MAPREG_MEM_TYPE_64BIT:
    193       1.7   thorpej 		/*
    194       1.7   thorpej 		 * Handle the case of a 64-bit memory register on a
    195       1.7   thorpej 		 * platform with 32-bit addressing.  Make sure that
    196       1.7   thorpej 		 * the address assigned and the device's memory size
    197       1.7   thorpej 		 * fit in 32 bits.  We implicitly assume that if
    198       1.7   thorpej 		 * bus_addr_t is 64-bit, then so is bus_size_t.
    199       1.7   thorpej 		 */
    200       1.7   thorpej 		if (sizeof(u_int64_t) > sizeof(bus_addr_t) &&
    201       1.7   thorpej 		    (address1 != 0 || mask1 != 0xffffffff)) {
    202       1.7   thorpej 			printf("pci_mem_find: 64-bit memory map which is "
    203       1.7   thorpej 			    "inaccessible on a 32-bit platform\n");
    204      1.25    dyoung 			return 1;
    205       1.7   thorpej 		}
    206       1.7   thorpej 		break;
    207       1.1   mycroft 	default:
    208       1.1   mycroft 		printf("pci_mem_find: reserved mapping register type\n");
    209      1.25    dyoung 		return 1;
    210       1.1   mycroft 	}
    211       1.1   mycroft 
    212       1.7   thorpej 	if (sizeof(u_int64_t) > sizeof(bus_addr_t)) {
    213      1.25    dyoung 		if (basep != NULL)
    214       1.7   thorpej 			*basep = PCI_MAPREG_MEM_ADDR(address);
    215      1.25    dyoung 		if (sizep != NULL)
    216       1.7   thorpej 			*sizep = PCI_MAPREG_MEM_SIZE(mask);
    217       1.7   thorpej 	} else {
    218      1.25    dyoung 		if (basep != NULL)
    219       1.7   thorpej 			*basep = PCI_MAPREG_MEM64_ADDR(waddress);
    220      1.25    dyoung 		if (sizep != NULL)
    221       1.7   thorpej 			*sizep = PCI_MAPREG_MEM64_SIZE(wmask);
    222       1.7   thorpej 	}
    223      1.25    dyoung 	if (flagsp != NULL)
    224      1.15   gdamore 		*flagsp = (isrom || PCI_MAPREG_MEM_PREFETCHABLE(address)) ?
    225       1.6  drochner 		    BUS_SPACE_MAP_PREFETCHABLE : 0;
    226       1.1   mycroft 
    227      1.25    dyoung 	return 0;
    228       1.7   thorpej }
    229       1.7   thorpej 
    230      1.12  drochner #define _PCI_MAPREG_TYPEBITS(reg) \
    231      1.12  drochner 	(PCI_MAPREG_TYPE(reg) == PCI_MAPREG_TYPE_IO ? \
    232      1.12  drochner 	reg & PCI_MAPREG_TYPE_MASK : \
    233      1.12  drochner 	reg & (PCI_MAPREG_TYPE_MASK|PCI_MAPREG_MEM_TYPE_MASK))
    234      1.12  drochner 
    235       1.7   thorpej pcireg_t
    236       1.9   thorpej pci_mapreg_type(pci_chipset_tag_t pc, pcitag_t tag, int reg)
    237       1.7   thorpej {
    238       1.7   thorpej 
    239      1.25    dyoung 	return _PCI_MAPREG_TYPEBITS(pci_conf_read(pc, tag, reg));
    240      1.12  drochner }
    241      1.12  drochner 
    242      1.12  drochner int
    243      1.12  drochner pci_mapreg_probe(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t *typep)
    244      1.12  drochner {
    245      1.12  drochner 	pcireg_t address, mask;
    246      1.12  drochner 	int s;
    247      1.13     perry 
    248      1.12  drochner 	s = splhigh();
    249      1.12  drochner 	address = pci_conf_read(pc, tag, reg);
    250      1.12  drochner 	pci_conf_write(pc, tag, reg, 0xffffffff);
    251      1.12  drochner 	mask = pci_conf_read(pc, tag, reg);
    252      1.12  drochner 	pci_conf_write(pc, tag, reg, address);
    253      1.12  drochner 	splx(s);
    254      1.12  drochner 
    255      1.12  drochner 	if (mask == 0) /* unimplemented mapping register */
    256      1.25    dyoung 		return 0;
    257      1.12  drochner 
    258      1.25    dyoung 	if (typep != NULL)
    259      1.12  drochner 		*typep = _PCI_MAPREG_TYPEBITS(address);
    260      1.25    dyoung 	return 1;
    261       1.1   mycroft }
    262       1.1   mycroft 
    263       1.1   mycroft int
    264       1.9   thorpej pci_mapreg_info(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t type,
    265       1.9   thorpej     bus_addr_t *basep, bus_size_t *sizep, int *flagsp)
    266       1.1   mycroft {
    267       1.1   mycroft 
    268       1.1   mycroft 	if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO)
    269      1.25    dyoung 		return pci_io_find(pc, tag, reg, type, basep, sizep,
    270      1.25    dyoung 		    flagsp);
    271       1.1   mycroft 	else
    272      1.25    dyoung 		return pci_mem_find(pc, tag, reg, type, basep, sizep,
    273      1.25    dyoung 		    flagsp);
    274       1.1   mycroft }
    275       1.1   mycroft 
    276       1.1   mycroft int
    277      1.28    dyoung pci_mapreg_map(const struct pci_attach_args *pa, int reg, pcireg_t type,
    278       1.9   thorpej     int busflags, bus_space_tag_t *tagp, bus_space_handle_t *handlep,
    279       1.9   thorpej     bus_addr_t *basep, bus_size_t *sizep)
    280       1.1   mycroft {
    281      1.24       bjs 	return pci_mapreg_submap(pa, reg, type, busflags, 0, 0, tagp,
    282      1.24       bjs 	    handlep, basep, sizep);
    283      1.24       bjs }
    284      1.24       bjs 
    285      1.28    dyoung static int
    286      1.28    dyoung pci_mapreg_submap(const struct pci_attach_args *pa, int reg, pcireg_t type,
    287      1.24       bjs     int busflags, bus_size_t maxsize, bus_size_t offset, bus_space_tag_t *tagp,
    288      1.24       bjs 	bus_space_handle_t *handlep, bus_addr_t *basep, bus_size_t *sizep)
    289      1.24       bjs {
    290       1.1   mycroft 	bus_space_tag_t tag;
    291       1.1   mycroft 	bus_space_handle_t handle;
    292       1.1   mycroft 	bus_addr_t base;
    293       1.1   mycroft 	bus_size_t size;
    294       1.1   mycroft 	int flags;
    295       1.1   mycroft 
    296       1.1   mycroft 	if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO) {
    297      1.29    dyoung 		if ((pa->pa_flags & PCI_FLAGS_IO_OKAY) == 0)
    298      1.25    dyoung 			return 1;
    299       1.1   mycroft 		if (pci_io_find(pa->pa_pc, pa->pa_tag, reg, type, &base,
    300       1.1   mycroft 		    &size, &flags))
    301      1.25    dyoung 			return 1;
    302       1.1   mycroft 		tag = pa->pa_iot;
    303       1.1   mycroft 	} else {
    304      1.29    dyoung 		if ((pa->pa_flags & PCI_FLAGS_MEM_OKAY) == 0)
    305      1.25    dyoung 			return 1;
    306       1.1   mycroft 		if (pci_mem_find(pa->pa_pc, pa->pa_tag, reg, type, &base,
    307       1.1   mycroft 		    &size, &flags))
    308      1.25    dyoung 			return 1;
    309       1.1   mycroft 		tag = pa->pa_memt;
    310       1.1   mycroft 	}
    311       1.1   mycroft 
    312      1.15   gdamore 	if (reg == PCI_MAPREG_ROM) {
    313      1.15   gdamore 		pcireg_t 	mask;
    314      1.15   gdamore 		int		s;
    315      1.15   gdamore 		/* we have to enable the ROM address decoder... */
    316      1.15   gdamore 		s = splhigh();
    317      1.15   gdamore 		mask = pci_conf_read(pa->pa_pc, pa->pa_tag, reg);
    318      1.15   gdamore 		mask |= PCI_MAPREG_ROM_ENABLE;
    319      1.15   gdamore 		pci_conf_write(pa->pa_pc, pa->pa_tag, reg, mask);
    320      1.15   gdamore 		splx(s);
    321      1.15   gdamore 	}
    322      1.15   gdamore 
    323      1.24       bjs 	/* If we're called with maxsize/offset of 0, behave like
    324      1.24       bjs 	 * pci_mapreg_map.
    325      1.24       bjs 	 */
    326      1.24       bjs 
    327      1.24       bjs 	maxsize = (maxsize && offset) ? maxsize : size;
    328      1.24       bjs 	base += offset;
    329      1.24       bjs 
    330      1.24       bjs 	if ((maxsize < size && offset + maxsize <= size) || offset != 0)
    331      1.25    dyoung 		return 1;
    332      1.24       bjs 
    333      1.24       bjs 	if (bus_space_map(tag, base, maxsize, busflags | flags, &handle))
    334      1.25    dyoung 		return 1;
    335       1.1   mycroft 
    336      1.25    dyoung 	if (tagp != NULL)
    337       1.1   mycroft 		*tagp = tag;
    338      1.25    dyoung 	if (handlep != NULL)
    339       1.1   mycroft 		*handlep = handle;
    340      1.25    dyoung 	if (basep != NULL)
    341       1.1   mycroft 		*basep = base;
    342      1.25    dyoung 	if (sizep != NULL)
    343      1.24       bjs 		*sizep = maxsize;
    344       1.1   mycroft 
    345      1.25    dyoung 	return 0;
    346       1.1   mycroft }
    347      1.16   gdamore 
    348      1.16   gdamore int
    349      1.28    dyoung pci_find_rom(const struct pci_attach_args *pa, bus_space_tag_t bst,
    350      1.16   gdamore     bus_space_handle_t bsh, int type, bus_space_handle_t *romh, bus_size_t *sz)
    351      1.16   gdamore {
    352      1.16   gdamore 	bus_size_t	romsz, offset = 0, imagesz;
    353      1.16   gdamore 	uint16_t	ptr;
    354      1.16   gdamore 	int		done = 0;
    355      1.16   gdamore 
    356      1.16   gdamore 	if (pci_mem_find(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM,
    357      1.16   gdamore 	    PCI_MAPREG_TYPE_ROM, NULL, &romsz, NULL))
    358      1.16   gdamore 		return 1;
    359      1.16   gdamore 
    360      1.16   gdamore 	/*
    361      1.16   gdamore 	 * no upper bound check; i cannot imagine a 4GB ROM, but
    362      1.16   gdamore 	 * it appears the spec would allow it!
    363      1.16   gdamore 	 */
    364      1.16   gdamore 	if (romsz < 1024)
    365      1.16   gdamore 		return 1;
    366      1.16   gdamore 
    367      1.16   gdamore 	while (offset < romsz && !done){
    368      1.16   gdamore 		struct pci_rom_header	hdr;
    369      1.16   gdamore 		struct pci_rom		rom;
    370      1.16   gdamore 
    371      1.16   gdamore 		hdr.romh_magic = bus_space_read_2(bst, bsh,
    372      1.16   gdamore 		    offset + offsetof (struct pci_rom_header, romh_magic));
    373      1.16   gdamore 		hdr.romh_data_ptr = bus_space_read_2(bst, bsh,
    374      1.16   gdamore 		    offset + offsetof (struct pci_rom_header, romh_data_ptr));
    375      1.16   gdamore 
    376      1.16   gdamore 		/* no warning: quite possibly ROM is simply not populated */
    377      1.16   gdamore 		if (hdr.romh_magic != PCI_ROM_HEADER_MAGIC)
    378      1.16   gdamore 			return 1;
    379      1.16   gdamore 
    380      1.16   gdamore 		ptr = offset + hdr.romh_data_ptr;
    381      1.16   gdamore 
    382      1.16   gdamore 		if (ptr > romsz) {
    383      1.16   gdamore 			printf("pci_find_rom: rom data ptr out of range\n");
    384      1.16   gdamore 			return 1;
    385      1.16   gdamore 		}
    386      1.16   gdamore 
    387      1.16   gdamore 		rom.rom_signature = bus_space_read_4(bst, bsh, ptr);
    388      1.16   gdamore 		rom.rom_vendor = bus_space_read_2(bst, bsh, ptr +
    389      1.16   gdamore 		    offsetof(struct pci_rom, rom_vendor));
    390      1.16   gdamore 		rom.rom_product = bus_space_read_2(bst, bsh, ptr +
    391      1.16   gdamore 		    offsetof(struct pci_rom, rom_product));
    392      1.16   gdamore 		rom.rom_class = bus_space_read_1(bst, bsh,
    393      1.16   gdamore 		    ptr + offsetof (struct pci_rom, rom_class));
    394      1.16   gdamore 		rom.rom_subclass = bus_space_read_1(bst, bsh,
    395      1.16   gdamore 		    ptr + offsetof (struct pci_rom, rom_subclass));
    396      1.16   gdamore 		rom.rom_interface = bus_space_read_1(bst, bsh,
    397      1.16   gdamore 		    ptr + offsetof (struct pci_rom, rom_interface));
    398      1.16   gdamore 		rom.rom_len = bus_space_read_2(bst, bsh,
    399      1.16   gdamore 		    ptr + offsetof (struct pci_rom, rom_len));
    400      1.16   gdamore 		rom.rom_code_type = bus_space_read_1(bst, bsh,
    401      1.16   gdamore 		    ptr + offsetof (struct pci_rom, rom_code_type));
    402      1.16   gdamore 		rom.rom_indicator = bus_space_read_1(bst, bsh,
    403      1.16   gdamore 		    ptr + offsetof (struct pci_rom, rom_indicator));
    404      1.16   gdamore 
    405      1.16   gdamore 		if (rom.rom_signature != PCI_ROM_SIGNATURE) {
    406      1.16   gdamore 			printf("pci_find_rom: bad rom data signature\n");
    407      1.16   gdamore 			return 1;
    408      1.16   gdamore 		}
    409      1.16   gdamore 
    410      1.16   gdamore 		imagesz = rom.rom_len * 512;
    411      1.16   gdamore 
    412      1.16   gdamore 		if ((rom.rom_vendor == PCI_VENDOR(pa->pa_id)) &&
    413      1.16   gdamore 		    (rom.rom_product == PCI_PRODUCT(pa->pa_id)) &&
    414      1.16   gdamore 		    (rom.rom_class == PCI_CLASS(pa->pa_class)) &&
    415      1.16   gdamore 		    (rom.rom_subclass == PCI_SUBCLASS(pa->pa_class)) &&
    416      1.16   gdamore 		    (rom.rom_interface == PCI_INTERFACE(pa->pa_class)) &&
    417      1.16   gdamore 		    (rom.rom_code_type == type)) {
    418      1.16   gdamore 			*sz = imagesz;
    419      1.16   gdamore 			bus_space_subregion(bst, bsh, offset, imagesz, romh);
    420      1.16   gdamore 			return 0;
    421      1.16   gdamore 		}
    422      1.16   gdamore 
    423      1.16   gdamore 		/* last image check */
    424      1.16   gdamore 		if (rom.rom_indicator & PCI_ROM_INDICATOR_LAST)
    425      1.16   gdamore 			return 1;
    426      1.16   gdamore 
    427      1.16   gdamore 		/* offset by size */
    428      1.16   gdamore 		offset += imagesz;
    429      1.16   gdamore 	}
    430      1.16   gdamore 	return 1;
    431      1.16   gdamore }
    432