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