1 /* $NetBSD: mpt_pci.c,v 1.2 2003/07/14 15:47:26 lukem 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.2 2003/07/14 15:47:26 lukem 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 110 { 0, 0, 111 0, 112 NULL }, 113 }; 114 115 static const struct mpt_pci_product * 116 mpt_pci_lookup(const struct pci_attach_args *pa) 117 { 118 const struct mpt_pci_product *mpp; 119 120 for (mpp = mpt_pci_products; mpp->mpp_name != NULL; mpp++) { 121 if (PCI_VENDOR(pa->pa_id) == mpp->mpp_vendor && 122 PCI_PRODUCT(pa->pa_id) == mpp->mpp_product) 123 return (mpp); 124 } 125 return (NULL); 126 } 127 128 static int 129 mpt_pci_match(struct device *parent, struct cfdata *cf, void *aux) 130 { 131 struct pci_attach_args *pa = aux; 132 133 if (mpt_pci_lookup(pa) != NULL) 134 return (1); 135 136 return (0); 137 } 138 139 static void 140 mpt_pci_attach(struct device *parent, struct device *self, void *aux) 141 { 142 struct mpt_pci_softc *psc = (void *) self; 143 mpt_softc_t *mpt = &psc->sc_mpt; 144 struct pci_attach_args *pa = aux; 145 const struct mpt_pci_product *mpp; 146 pci_intr_handle_t ih; 147 const char *intrstr; 148 pcireg_t reg, memtype; 149 bus_space_tag_t memt; 150 bus_space_handle_t memh; 151 int memh_valid; 152 153 mpp = mpt_pci_lookup(pa); 154 if (mpp == NULL) { 155 printf("\n"); 156 panic("mpt_pci_attach"); 157 } 158 159 if (mpp->mpp_flags & MPP_F_FC) { 160 mpt->is_fc = 1; 161 aprint_naive(": Fibre Channel controller\n"); 162 } else 163 aprint_naive(": SCSI controller\n"); 164 aprint_normal(": %s\n", mpp->mpp_name); 165 166 psc->sc_pc = pa->pa_pc; 167 psc->sc_tag = pa->pa_tag; 168 169 mpt->sc_dmat = pa->pa_dmat; 170 mpt->sc_set_config_regs = mpt_pci_set_config_regs; 171 172 /* 173 * Map the device. 174 */ 175 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, MPT_PCI_MMBA); 176 switch (memtype) { 177 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT: 178 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT: 179 memh_valid = (pci_mapreg_map(pa, MPT_PCI_MMBA, 180 memtype, 0, &memt, &memh, NULL, NULL) == 0); 181 break; 182 183 default: 184 memh_valid = 0; 185 } 186 187 if (memh_valid) { 188 mpt->sc_st = memt; 189 mpt->sc_sh = memh; 190 } else { 191 aprint_error("%s: unable to map device registers\n", 192 mpt->sc_dev.dv_xname); 193 return; 194 } 195 196 /* 197 * Make sure the PCI command register is properly configured. 198 */ 199 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); 200 reg |= PCI_COMMAND_MASTER_ENABLE; 201 /* XXX PCI_COMMAND_INVALIDATE_ENABLE */ 202 /* XXX PCI_COMMAND_PARITY_ENABLE */ 203 /* XXX PCI_COMMAND_SERR_ENABLE */ 204 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg); 205 206 /* 207 * Ensure that the ROM is diabled. 208 */ 209 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM); 210 reg &= ~1; 211 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM, reg); 212 213 /* 214 * Map and establish our interrupt. 215 */ 216 if (pci_intr_map(pa, &ih) != 0) { 217 aprint_error("%s: unable to map interrupt\n", 218 mpt->sc_dev.dv_xname); 219 return; 220 } 221 intrstr = pci_intr_string(pa->pa_pc, ih); 222 psc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO, mpt_intr, mpt); 223 if (psc->sc_ih == NULL) { 224 aprint_error("%s: unable to establish interrupt", 225 mpt->sc_dev.dv_xname); 226 if (intrstr != NULL) 227 aprint_normal(" at %s", intrstr); 228 aprint_normal("\n"); 229 return; 230 } 231 aprint_normal("%s: interrupting at %s\n", mpt->sc_dev.dv_xname, 232 intrstr); 233 234 /* Disable interrupts on the part. */ 235 mpt_disable_ints(mpt); 236 237 /* Allocate DMA memory. */ 238 if (mpt_dma_mem_alloc(mpt) != 0) { 239 aprint_error("%s: unable to allocate DMA memory\n", 240 mpt->sc_dev.dv_xname); 241 return; 242 } 243 244 /* 245 * Save the PCI config register values. 246 * 247 * Hard resets are known to screw up the BAR for diagnostic 248 * memory accesses (Mem1). 249 * 250 * Using Mem1 is know to make the chip stop responding to 251 * configuration cycles, so we need to save it now. 252 */ 253 mpt_pci_read_config_regs(mpt); 254 255 /* 256 * If we're a dual-port adapter, try to find our peer. We 257 * need to fix his PCI config registers, too. 258 */ 259 if (mpp->mpp_flags & MPP_F_DUAL) 260 mpt_pci_link_peer(mpt); 261 262 /* Initialize the hardware. */ 263 if (mpt_init(mpt, MPT_DB_INIT_HOST) != 0) { 264 /* Error message already printed. */ 265 return; 266 } 267 268 /* Attach to scsipi. */ 269 mpt_scsipi_attach(mpt); 270 } 271 272 CFATTACH_DECL(mpt_pci, sizeof(struct mpt_pci_softc), 273 mpt_pci_match, mpt_pci_attach, NULL, NULL); 274 275 /* 276 * Find and remember our peer PCI function on a dual-port device. 277 */ 278 static void 279 mpt_pci_link_peer(mpt_softc_t *mpt) 280 { 281 extern struct cfdriver mpt_cd; 282 283 struct mpt_pci_softc *peer_psc, *psc = (void *) mpt; 284 struct device *dev; 285 int unit, b, d, f, peer_b, peer_d, peer_f; 286 287 pci_decompose_tag(psc->sc_pc, psc->sc_tag, &b, &d, &f); 288 289 for (unit = 0; unit < mpt_cd.cd_ndevs; unit++) { 290 if (unit == mpt->sc_dev.dv_unit) 291 continue; 292 dev = device_lookup(&mpt_cd, unit); 293 if (dev == NULL) 294 continue; 295 if (dev->dv_cfattach != &mpt_pci_ca) 296 continue; 297 peer_psc = (void *) dev; 298 if (peer_psc->sc_pc != psc->sc_pc) 299 continue; 300 pci_decompose_tag(peer_psc->sc_pc, peer_psc->sc_tag, 301 &peer_b, &peer_d, &peer_f); 302 if (peer_b == b && peer_d == d) { 303 if (mpt->verbose) 304 mpt_prt(mpt, "linking with peer: %s", 305 peer_psc->sc_mpt.sc_dev.dv_xname); 306 mpt->mpt2 = (mpt_softc_t *) peer_psc; 307 peer_psc->sc_mpt.mpt2 = mpt; 308 return; 309 } 310 } 311 } 312 313 /* 314 * Save the volatile PCI configuration registers. 315 */ 316 static void 317 mpt_pci_read_config_regs(mpt_softc_t *mpt) 318 { 319 struct mpt_pci_softc *psc = (void *) mpt; 320 321 psc->sc_pci_csr = pci_conf_read(psc->sc_pc, psc->sc_tag, 322 PCI_COMMAND_STATUS_REG); 323 psc->sc_pci_bhlc = pci_conf_read(psc->sc_pc, psc->sc_tag, 324 PCI_BHLC_REG); 325 psc->sc_pci_io_bar = pci_conf_read(psc->sc_pc, psc->sc_tag, 326 PCI_MAPREG_START); 327 psc->sc_pci_mem0_bar[0] = pci_conf_read(psc->sc_pc, psc->sc_tag, 328 PCI_MAPREG_START+0x04); 329 psc->sc_pci_mem0_bar[1] = pci_conf_read(psc->sc_pc, psc->sc_tag, 330 PCI_MAPREG_START+0x08); 331 psc->sc_pci_mem1_bar[0] = pci_conf_read(psc->sc_pc, psc->sc_tag, 332 PCI_MAPREG_START+0x0c); 333 psc->sc_pci_mem1_bar[1] = pci_conf_read(psc->sc_pc, psc->sc_tag, 334 PCI_MAPREG_START+0x10); 335 psc->sc_pci_rom_bar = pci_conf_read(psc->sc_pc, psc->sc_tag, 336 PCI_MAPREG_ROM); 337 psc->sc_pci_int = pci_conf_read(psc->sc_pc, psc->sc_tag, 338 PCI_INTERRUPT_REG); 339 psc->sc_pci_pmcsr = pci_conf_read(psc->sc_pc, psc->sc_tag, 0x44); 340 } 341 342 /* 343 * Restore the volatile PCI configuration registers. 344 */ 345 static void 346 mpt_pci_set_config_regs(mpt_softc_t *mpt) 347 { 348 struct mpt_pci_softc *psc = (void *) mpt; 349 350 pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_COMMAND_STATUS_REG, 351 psc->sc_pci_csr); 352 pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_BHLC_REG, 353 psc->sc_pci_bhlc); 354 pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_MAPREG_START, 355 psc->sc_pci_io_bar); 356 pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_MAPREG_START+0x04, 357 psc->sc_pci_mem0_bar[0]); 358 pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_MAPREG_START+0x08, 359 psc->sc_pci_mem0_bar[1]); 360 pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_MAPREG_START+0x0c, 361 psc->sc_pci_mem1_bar[0]); 362 pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_MAPREG_START+0x10, 363 psc->sc_pci_mem1_bar[1]); 364 pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_MAPREG_ROM, 365 psc->sc_pci_rom_bar); 366 pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_INTERRUPT_REG, 367 psc->sc_pci_int); 368 pci_conf_write(psc->sc_pc, psc->sc_tag, 0x44, psc->sc_pci_pmcsr); 369 } 370