Home | History | Annotate | Line # | Download | only in pci
mpt_pci.c revision 1.5
      1 /*	$NetBSD: mpt_pci.c,v 1.5 2005/02/27 00:27:33 perry Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2003 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed for the NetBSD Project by
     20  *	Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * mpt_pci.c:
     40  *
     41  * NetBSD PCI-specific routines for LSI Fusion adapters.
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: mpt_pci.c,v 1.5 2005/02/27 00:27:33 perry Exp $");
     46 
     47 #include <dev/ic/mpt.h>			/* pulls in all headers */
     48 
     49 #include <dev/pci/pcireg.h>
     50 #include <dev/pci/pcivar.h>
     51 #include <dev/pci/pcidevs.h>
     52 
     53 #define	MPT_PCI_MMBA		(PCI_MAPREG_START+0x04)
     54 
     55 struct mpt_pci_softc {
     56 	mpt_softc_t sc_mpt;
     57 
     58 	pci_chipset_tag_t sc_pc;
     59 	pcitag_t sc_tag;
     60 
     61 	void *sc_ih;
     62 
     63 	/* Saved volatile PCI configuration registers. */
     64 	pcireg_t sc_pci_csr;
     65 	pcireg_t sc_pci_bhlc;
     66 	pcireg_t sc_pci_io_bar;
     67 	pcireg_t sc_pci_mem0_bar[2];
     68 	pcireg_t sc_pci_mem1_bar[2];
     69 	pcireg_t sc_pci_rom_bar;
     70 	pcireg_t sc_pci_int;
     71 	pcireg_t sc_pci_pmcsr;
     72 };
     73 
     74 static void	mpt_pci_link_peer(mpt_softc_t *);
     75 static void	mpt_pci_read_config_regs(mpt_softc_t *);
     76 static void	mpt_pci_set_config_regs(mpt_softc_t *);
     77 
     78 #define	MPP_F_FC	0x01	/* Fibre Channel adapter */
     79 #define	MPP_F_DUAL	0x02	/* Dual port adapter */
     80 
     81 static const struct mpt_pci_product {
     82 	pci_vendor_id_t		mpp_vendor;
     83 	pci_product_id_t	mpp_product;
     84 	int			mpp_flags;
     85 	const char		*mpp_name;
     86 } mpt_pci_products[] = {
     87 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_1030,
     88 	  MPP_F_DUAL,
     89 	  "LSI Logic 53c1030 Ultra320 SCSI" },
     90 
     91 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC909,
     92 	  MPP_F_FC,
     93 	  "LSI Logic FC909 FC Adapter" },
     94 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC909A,
     95 	  MPP_F_FC,
     96 	  "LSI Logic FC909A FC Adapter" },
     97 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC929,
     98 	  MPP_F_FC | MPP_F_DUAL,
     99 	  "LSI Logic FC929 FC Adapter" },
    100 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC929_1,
    101 	  MPP_F_FC | MPP_F_DUAL,
    102 	  "LSI Logic FC929 FC Adapter" },
    103 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC919,
    104 	  MPP_F_FC,
    105 	  "LSI Logic FC919 FC Adapter" },
    106 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC919_1,
    107 	  MPP_F_FC,
    108 	  "LSI Logic FC919 FC Adapter" },
    109 	{ PCI_VENDOR_SYMBIOS,   PCI_PRODUCT_SYMBIOS_FC929X,
    110 	  MPP_F_FC | MPP_F_DUAL,
    111 	  "LSI Logic FC929X FC Adapter" },
    112 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC919X,
    113 	  MPP_F_FC,
    114 	  "LSI Logic FC919X FC Adapter" },
    115 
    116 	{ 0,			0,
    117 	  0,
    118 	  NULL },
    119 };
    120 
    121 static const struct mpt_pci_product *
    122 mpt_pci_lookup(const struct pci_attach_args *pa)
    123 {
    124 	const struct mpt_pci_product *mpp;
    125 
    126 	for (mpp = mpt_pci_products; mpp->mpp_name != NULL; mpp++) {
    127 		if (PCI_VENDOR(pa->pa_id) == mpp->mpp_vendor &&
    128 		    PCI_PRODUCT(pa->pa_id) == mpp->mpp_product)
    129 			return (mpp);
    130 	}
    131 	return (NULL);
    132 }
    133 
    134 static int
    135 mpt_pci_match(struct device *parent, struct cfdata *cf, void *aux)
    136 {
    137 	struct pci_attach_args *pa = aux;
    138 
    139 	if (mpt_pci_lookup(pa) != NULL)
    140 		return (1);
    141 
    142 	return (0);
    143 }
    144 
    145 static void
    146 mpt_pci_attach(struct device *parent, struct device *self, void *aux)
    147 {
    148 	struct mpt_pci_softc *psc = (void *) self;
    149 	mpt_softc_t *mpt = &psc->sc_mpt;
    150 	struct pci_attach_args *pa = aux;
    151 	const struct mpt_pci_product *mpp;
    152 	pci_intr_handle_t ih;
    153 	const char *intrstr;
    154 	pcireg_t reg, memtype;
    155 	bus_space_tag_t memt;
    156 	bus_space_handle_t memh;
    157 	int memh_valid;
    158 
    159 	mpp = mpt_pci_lookup(pa);
    160 	if (mpp == NULL) {
    161 		printf("\n");
    162 		panic("mpt_pci_attach");
    163 	}
    164 
    165 	if (mpp->mpp_flags & MPP_F_FC) {
    166 		mpt->is_fc = 1;
    167 		aprint_naive(": Fibre Channel controller\n");
    168 	} else
    169 		aprint_naive(": SCSI controller\n");
    170 	aprint_normal(": %s\n", mpp->mpp_name);
    171 
    172 	psc->sc_pc = pa->pa_pc;
    173 	psc->sc_tag = pa->pa_tag;
    174 
    175 	mpt->sc_dmat = pa->pa_dmat;
    176 	mpt->sc_set_config_regs = mpt_pci_set_config_regs;
    177 
    178 	/*
    179 	 * Map the device.
    180 	 */
    181 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, MPT_PCI_MMBA);
    182 	switch (memtype) {
    183 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
    184 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
    185 		memh_valid = (pci_mapreg_map(pa, MPT_PCI_MMBA,
    186 		    memtype, 0, &memt, &memh, NULL, NULL) == 0);
    187 		break;
    188 
    189 	default:
    190 		memh_valid = 0;
    191 	}
    192 
    193 	if (memh_valid) {
    194 		mpt->sc_st = memt;
    195 		mpt->sc_sh = memh;
    196 	} else {
    197 		aprint_error("%s: unable to map device registers\n",
    198 		    mpt->sc_dev.dv_xname);
    199 		return;
    200 	}
    201 
    202 	/*
    203 	 * Make sure the PCI command register is properly configured.
    204 	 */
    205 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    206 	reg |= PCI_COMMAND_MASTER_ENABLE;
    207 	/* XXX PCI_COMMAND_INVALIDATE_ENABLE */
    208 	/* XXX PCI_COMMAND_PARITY_ENABLE */
    209 	/* XXX PCI_COMMAND_SERR_ENABLE */
    210 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
    211 
    212 	/*
    213 	 * Ensure that the ROM is diabled.
    214 	 */
    215 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM);
    216 	reg &= ~1;
    217 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM, reg);
    218 
    219 	/*
    220 	 * Map and establish our interrupt.
    221 	 */
    222 	if (pci_intr_map(pa, &ih) != 0) {
    223 		aprint_error("%s: unable to map interrupt\n",
    224 		    mpt->sc_dev.dv_xname);
    225 		return;
    226 	}
    227 	intrstr = pci_intr_string(pa->pa_pc, ih);
    228 	psc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO, mpt_intr, mpt);
    229 	if (psc->sc_ih == NULL) {
    230 		aprint_error("%s: unable to establish interrupt",
    231 		    mpt->sc_dev.dv_xname);
    232 		if (intrstr != NULL)
    233 			aprint_normal(" at %s", intrstr);
    234 		aprint_normal("\n");
    235 		return;
    236 	}
    237 	aprint_normal("%s: interrupting at %s\n", mpt->sc_dev.dv_xname,
    238 	    intrstr);
    239 
    240 	/* Disable interrupts on the part. */
    241 	mpt_disable_ints(mpt);
    242 
    243 	/* Allocate DMA memory. */
    244 	if (mpt_dma_mem_alloc(mpt) != 0) {
    245 		aprint_error("%s: unable to allocate DMA memory\n",
    246 		    mpt->sc_dev.dv_xname);
    247 		return;
    248 	}
    249 
    250 	/*
    251 	 * Save the PCI config register values.
    252 	 *
    253 	 * Hard resets are known to screw up the BAR for diagnostic
    254 	 * memory accesses (Mem1).
    255 	 *
    256 	 * Using Mem1 is know to make the chip stop responding to
    257 	 * configuration cycles, so we need to save it now.
    258 	 */
    259 	mpt_pci_read_config_regs(mpt);
    260 
    261 	/*
    262 	 * If we're a dual-port adapter, try to find our peer.  We
    263 	 * need to fix his PCI config registers, too.
    264 	 */
    265 	if (mpp->mpp_flags & MPP_F_DUAL)
    266 		mpt_pci_link_peer(mpt);
    267 
    268 	/* Initialize the hardware. */
    269 	if (mpt_init(mpt, MPT_DB_INIT_HOST) != 0) {
    270 		/* Error message already printed. */
    271 		return;
    272 	}
    273 
    274 	/* Attach to scsipi. */
    275 	mpt_scsipi_attach(mpt);
    276 }
    277 
    278 CFATTACH_DECL(mpt_pci, sizeof(struct mpt_pci_softc),
    279     mpt_pci_match, mpt_pci_attach, NULL, NULL);
    280 
    281 /*
    282  * Find and remember our peer PCI function on a dual-port device.
    283  */
    284 static void
    285 mpt_pci_link_peer(mpt_softc_t *mpt)
    286 {
    287 	extern struct cfdriver mpt_cd;
    288 
    289 	struct mpt_pci_softc *peer_psc, *psc = (void *) mpt;
    290 	struct device *dev;
    291 	int unit, b, d, f, peer_b, peer_d, peer_f;
    292 
    293 	pci_decompose_tag(psc->sc_pc, psc->sc_tag, &b, &d, &f);
    294 
    295 	for (unit = 0; unit < mpt_cd.cd_ndevs; unit++) {
    296 		if (unit == mpt->sc_dev.dv_unit)
    297 			continue;
    298 		dev = device_lookup(&mpt_cd, unit);
    299 		if (dev == NULL)
    300 			continue;
    301 		if (dev->dv_cfattach != &mpt_pci_ca)
    302 			continue;
    303 		peer_psc = (void *) dev;
    304 		if (peer_psc->sc_pc != psc->sc_pc)
    305 			continue;
    306 		pci_decompose_tag(peer_psc->sc_pc, peer_psc->sc_tag,
    307 		    &peer_b, &peer_d, &peer_f);
    308 		if (peer_b == b && peer_d == d) {
    309 			if (mpt->verbose)
    310 				mpt_prt(mpt, "linking with peer: %s",
    311 				    peer_psc->sc_mpt.sc_dev.dv_xname);
    312 			mpt->mpt2 = (mpt_softc_t *) peer_psc;
    313 			peer_psc->sc_mpt.mpt2 = mpt;
    314 			return;
    315 		}
    316 	}
    317 }
    318 
    319 /*
    320  * Save the volatile PCI configuration registers.
    321  */
    322 static void
    323 mpt_pci_read_config_regs(mpt_softc_t *mpt)
    324 {
    325 	struct mpt_pci_softc *psc = (void *) mpt;
    326 
    327 	psc->sc_pci_csr = pci_conf_read(psc->sc_pc, psc->sc_tag,
    328 	    PCI_COMMAND_STATUS_REG);
    329 	psc->sc_pci_bhlc = pci_conf_read(psc->sc_pc, psc->sc_tag,
    330 	    PCI_BHLC_REG);
    331 	psc->sc_pci_io_bar = pci_conf_read(psc->sc_pc, psc->sc_tag,
    332 	    PCI_MAPREG_START);
    333 	psc->sc_pci_mem0_bar[0] = pci_conf_read(psc->sc_pc, psc->sc_tag,
    334 	    PCI_MAPREG_START+0x04);
    335 	psc->sc_pci_mem0_bar[1] = pci_conf_read(psc->sc_pc, psc->sc_tag,
    336 	    PCI_MAPREG_START+0x08);
    337 	psc->sc_pci_mem1_bar[0] = pci_conf_read(psc->sc_pc, psc->sc_tag,
    338 	    PCI_MAPREG_START+0x0c);
    339 	psc->sc_pci_mem1_bar[1] = pci_conf_read(psc->sc_pc, psc->sc_tag,
    340 	    PCI_MAPREG_START+0x10);
    341 	psc->sc_pci_rom_bar = pci_conf_read(psc->sc_pc, psc->sc_tag,
    342 	    PCI_MAPREG_ROM);
    343 	psc->sc_pci_int = pci_conf_read(psc->sc_pc, psc->sc_tag,
    344 	    PCI_INTERRUPT_REG);
    345 	psc->sc_pci_pmcsr = pci_conf_read(psc->sc_pc, psc->sc_tag, 0x44);
    346 }
    347 
    348 /*
    349  * Restore the volatile PCI configuration registers.
    350  */
    351 static void
    352 mpt_pci_set_config_regs(mpt_softc_t *mpt)
    353 {
    354 	struct mpt_pci_softc *psc = (void *) mpt;
    355 
    356 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_COMMAND_STATUS_REG,
    357 	    psc->sc_pci_csr);
    358 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_BHLC_REG,
    359 	    psc->sc_pci_bhlc);
    360 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_MAPREG_START,
    361 	    psc->sc_pci_io_bar);
    362 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_MAPREG_START+0x04,
    363 	    psc->sc_pci_mem0_bar[0]);
    364 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_MAPREG_START+0x08,
    365 	    psc->sc_pci_mem0_bar[1]);
    366 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_MAPREG_START+0x0c,
    367 	    psc->sc_pci_mem1_bar[0]);
    368 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_MAPREG_START+0x10,
    369 	    psc->sc_pci_mem1_bar[1]);
    370 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_MAPREG_ROM,
    371 	    psc->sc_pci_rom_bar);
    372 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_INTERRUPT_REG,
    373 	    psc->sc_pci_int);
    374 	pci_conf_write(psc->sc_pc, psc->sc_tag, 0x44, psc->sc_pci_pmcsr);
    375 }
    376