mpt_pci.c revision 119690
1119418Sobrien/*- 2102596Smjacob * PCI specific probe and attach routines for LSI Fusion Adapters 3101704Smjacob * FreeBSD Version. 4101704Smjacob * 5119418Sobrien * Copyright (c) 2000, 2001 by Greg Ansley 6101704Smjacob * Partially derived from Matt Jacob's ISP driver. 7119418Sobrien * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002 by Matthew Jacob 8119418Sobrien * Feral Software 9119418Sobrien * All rights reserved. 10101704Smjacob * 11101704Smjacob * Redistribution and use in source and binary forms, with or without 12101704Smjacob * modification, are permitted provided that the following conditions 13101704Smjacob * are met: 14101704Smjacob * 1. Redistributions of source code must retain the above copyright 15101704Smjacob * notice immediately at the beginning of the file, without modification, 16101704Smjacob * this list of conditions, and the following disclaimer. 17101704Smjacob * 2. The name of the author may not be used to endorse or promote products 18101704Smjacob * derived from this software without specific prior written permission. 19101704Smjacob * 20101704Smjacob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21101704Smjacob * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22101704Smjacob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23101704Smjacob * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 24101704Smjacob * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25101704Smjacob * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26101704Smjacob * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27101704Smjacob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28101704Smjacob * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29101704Smjacob * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30101704Smjacob * SUCH DAMAGE. 31101704Smjacob */ 32101704Smjacob 33119418Sobrien#include <sys/cdefs.h> 34119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/mpt/mpt_pci.c 119690 2003-09-02 17:30:40Z jhb $"); 35119418Sobrien 36101704Smjacob#include <sys/param.h> 37101704Smjacob#include <sys/systm.h> 38101704Smjacob#include <sys/kernel.h> 39102199Smjacob#include <sys/module.h> 40101704Smjacob#include <sys/bus.h> 41102199Smjacob 42119285Simp#include <dev/pci/pcireg.h> 43119285Simp#include <dev/pci/pcivar.h> 44101704Smjacob 45101704Smjacob#include <machine/bus_memio.h> 46101704Smjacob#include <machine/bus_pio.h> 47101704Smjacob#include <machine/bus.h> 48101704Smjacob#include <machine/resource.h> 49101704Smjacob#include <sys/rman.h> 50102199Smjacob#include <sys/malloc.h> 51101704Smjacob 52101704Smjacob#include <dev/mpt/mpt_freebsd.h> 53101704Smjacob 54101704Smjacob#ifndef PCI_VENDOR_LSI 55101704Smjacob#define PCI_VENDOR_LSI 0x1000 56101704Smjacob#endif 57101704Smjacob 58101704Smjacob#ifndef PCI_PRODUCT_LSI_FC909 59101704Smjacob#define PCI_PRODUCT_LSI_FC909 0x0620 60101704Smjacob#endif 61101704Smjacob 62102596Smjacob#ifndef PCI_PRODUCT_LSI_FC909A 63102596Smjacob#define PCI_PRODUCT_LSI_FC909A 0x0621 64102596Smjacob#endif 65102596Smjacob 66103829Smjacob#ifndef PCI_PRODUCT_LSI_FC919 67103829Smjacob#define PCI_PRODUCT_LSI_FC919 0x0624 68103829Smjacob#endif 69103829Smjacob 70101704Smjacob#ifndef PCI_PRODUCT_LSI_FC929 71101704Smjacob#define PCI_PRODUCT_LSI_FC929 0x0622 72101704Smjacob#endif 73101704Smjacob 74101704Smjacob#ifndef PCI_PRODUCT_LSI_1030 75101704Smjacob#define PCI_PRODUCT_LSI_1030 0x0030 76101704Smjacob#endif 77101704Smjacob 78102303Smjacob#ifndef PCIM_CMD_SERRESPEN 79102303Smjacob#define PCIM_CMD_SERRESPEN 0x0100 80102303Smjacob#endif 81101704Smjacob 82101704Smjacob 83102303Smjacob 84101704Smjacob#define MEM_MAP_REG 0x14 85101704Smjacob#define MEM_MAP_SRAM 0x1C 86101704Smjacob 87101704Smjacobstatic int mpt_probe(device_t); 88101704Smjacobstatic int mpt_attach(device_t); 89102199Smjacobstatic void mpt_free_bus_resources(mpt_softc_t *mpt); 90101704Smjacobstatic int mpt_detach(device_t); 91101704Smjacobstatic int mpt_shutdown(device_t); 92102199Smjacobstatic int mpt_dma_mem_alloc(mpt_softc_t *mpt); 93102199Smjacobstatic void mpt_dma_mem_free(mpt_softc_t *mpt); 94102199Smjacobstatic void mpt_read_config_regs(mpt_softc_t *mpt); 95102199Smjacobstatic void mpt_pci_intr(void *); 96101704Smjacob 97101704Smjacobstatic device_method_t mpt_methods[] = { 98101704Smjacob /* Device interface */ 99101704Smjacob DEVMETHOD(device_probe, mpt_probe), 100101704Smjacob DEVMETHOD(device_attach, mpt_attach), 101101704Smjacob DEVMETHOD(device_detach, mpt_detach), 102101704Smjacob DEVMETHOD(device_shutdown, mpt_shutdown), 103101704Smjacob { 0, 0 } 104101704Smjacob}; 105101704Smjacob 106101704Smjacobstatic driver_t mpt_driver = { 107102199Smjacob "mpt", mpt_methods, sizeof (mpt_softc_t) 108101704Smjacob}; 109101704Smjacobstatic devclass_t mpt_devclass; 110101704SmjacobDRIVER_MODULE(mpt, pci, mpt_driver, mpt_devclass, 0, 0); 111101704SmjacobMODULE_VERSION(mpt, 1); 112101704Smjacob 113101704Smjacobint 114101704Smjacobmpt_intr(void *dummy) 115101704Smjacob{ 116103829Smjacob int nrepl = 0; 117101704Smjacob u_int32_t reply; 118102199Smjacob mpt_softc_t *mpt = (mpt_softc_t *)dummy; 119101704Smjacob 120103829Smjacob if ((mpt_read(mpt, MPT_OFFSET_INTR_STATUS) & MPT_INTR_REPLY_READY) == 0) 121103829Smjacob return (0); 122101704Smjacob reply = mpt_pop_reply_queue(mpt); 123101704Smjacob while (reply != MPT_REPLY_EMPTY) { 124103829Smjacob nrepl++; 125101704Smjacob if (mpt->verbose > 1) { 126101704Smjacob if ((reply & MPT_CONTEXT_REPLY) != 0) { 127101704Smjacob /* Address reply; IOC has something to say */ 128101704Smjacob mpt_print_reply(MPT_REPLY_PTOV(mpt, reply)); 129101704Smjacob } else { 130101704Smjacob /* Context reply ; all went well */ 131103914Smjacob mpt_prt(mpt, "context %u reply OK", reply); 132101704Smjacob } 133101704Smjacob } 134101704Smjacob mpt_done(mpt, reply); 135101704Smjacob reply = mpt_pop_reply_queue(mpt); 136101704Smjacob } 137103829Smjacob return (nrepl != 0); 138101704Smjacob} 139101704Smjacob 140101704Smjacobstatic int 141101704Smjacobmpt_probe(device_t dev) 142101704Smjacob{ 143101704Smjacob char *desc; 144101704Smjacob 145101704Smjacob if (pci_get_vendor(dev) != PCI_VENDOR_LSI) 146101704Smjacob return (ENXIO); 147101704Smjacob 148101704Smjacob switch ((pci_get_device(dev) & ~1)) { 149101704Smjacob case PCI_PRODUCT_LSI_FC909: 150101704Smjacob desc = "LSILogic FC909 FC Adapter"; 151101704Smjacob break; 152102596Smjacob case PCI_PRODUCT_LSI_FC909A: 153102596Smjacob desc = "LSILogic FC909A FC Adapter"; 154102596Smjacob break; 155103829Smjacob case PCI_PRODUCT_LSI_FC919: 156103829Smjacob desc = "LSILogic FC919 FC Adapter"; 157103829Smjacob break; 158101704Smjacob case PCI_PRODUCT_LSI_FC929: 159101704Smjacob desc = "LSILogic FC929 FC Adapter"; 160101704Smjacob break; 161101704Smjacob case PCI_PRODUCT_LSI_1030: 162101704Smjacob desc = "LSILogic 1030 Ultra4 Adapter"; 163101704Smjacob break; 164101704Smjacob default: 165101704Smjacob return (ENXIO); 166101704Smjacob } 167101704Smjacob 168101704Smjacob device_set_desc(dev, desc); 169101704Smjacob return (0); 170101704Smjacob} 171101704Smjacob 172101704Smjacob#ifdef RELENG_4 173101704Smjacobstatic void 174102199Smjacobmpt_set_options(mpt_softc_t *mpt) 175101704Smjacob{ 176101704Smjacob int bitmap; 177101704Smjacob 178101704Smjacob bitmap = 0; 179101704Smjacob if (getenv_int("mpt_disable", &bitmap)) { 180101704Smjacob if (bitmap & (1 << mpt->unit)) { 181101704Smjacob mpt->disabled = 1; 182101704Smjacob } 183101704Smjacob } 184101704Smjacob 185101704Smjacob bitmap = 0; 186101704Smjacob if (getenv_int("mpt_debug", &bitmap)) { 187101704Smjacob if (bitmap & (1 << mpt->unit)) { 188101704Smjacob mpt->verbose = 2; 189101704Smjacob } 190101704Smjacob } 191101704Smjacob 192101704Smjacob} 193101704Smjacob#else 194101704Smjacobstatic void 195102199Smjacobmpt_set_options(mpt_softc_t *mpt) 196101704Smjacob{ 197101704Smjacob int tval; 198101704Smjacob 199101704Smjacob tval = 0; 200101704Smjacob if (resource_int_value(device_get_name(mpt->dev), 201101704Smjacob device_get_unit(mpt->dev), "disable", &tval) == 0 && tval != 0) { 202101704Smjacob mpt->disabled = 1; 203101704Smjacob } 204101704Smjacob tval = 0; 205101704Smjacob if (resource_int_value(device_get_name(mpt->dev), 206101704Smjacob device_get_unit(mpt->dev), "debug", &tval) == 0 && tval != 0) { 207101704Smjacob mpt->verbose += tval; 208101704Smjacob } 209101704Smjacob} 210101704Smjacob#endif 211101704Smjacob 212101704Smjacob 213102303Smjacobstatic void 214102303Smjacobmpt_link_peer(mpt_softc_t *mpt) 215102303Smjacob{ 216102303Smjacob mpt_softc_t *mpt2; 217102303Smjacob 218102303Smjacob if (mpt->unit == 0) { 219102303Smjacob return; 220102303Smjacob } 221102303Smjacob 222102303Smjacob /* 223102303Smjacob * XXX: depends on probe order 224102303Smjacob */ 225102303Smjacob mpt2 = (mpt_softc_t *) devclass_get_softc(mpt_devclass, mpt->unit-1); 226102303Smjacob 227102303Smjacob if (mpt2 == NULL) { 228102303Smjacob return; 229102303Smjacob } 230102303Smjacob if (pci_get_vendor(mpt2->dev) != pci_get_vendor(mpt->dev)) { 231102303Smjacob return; 232102303Smjacob } 233102303Smjacob if (pci_get_device(mpt2->dev) != pci_get_device(mpt->dev)) { 234102303Smjacob return; 235102303Smjacob } 236102303Smjacob mpt->mpt2 = mpt2; 237102303Smjacob mpt2->mpt2 = mpt; 238102303Smjacob if (mpt->verbose) { 239103914Smjacob mpt_prt(mpt, "linking with peer (mpt%d)", 240102303Smjacob device_get_unit(mpt2->dev)); 241102303Smjacob } 242102303Smjacob} 243102303Smjacob 244102303Smjacob 245101704Smjacobstatic int 246101704Smjacobmpt_attach(device_t dev) 247101704Smjacob{ 248101704Smjacob int iqd; 249101704Smjacob u_int32_t data, cmd; 250102199Smjacob mpt_softc_t *mpt; 251101704Smjacob 252101704Smjacob /* Allocate the softc structure */ 253102199Smjacob mpt = (mpt_softc_t*) device_get_softc(dev); 254101704Smjacob if (mpt == NULL) { 255101704Smjacob device_printf(dev, "cannot allocate softc\n"); 256101704Smjacob return (ENOMEM); 257101704Smjacob } 258102199Smjacob bzero(mpt, sizeof (mpt_softc_t)); 259101704Smjacob switch ((pci_get_device(dev) & ~1)) { 260101704Smjacob case PCI_PRODUCT_LSI_FC909: 261102596Smjacob case PCI_PRODUCT_LSI_FC909A: 262103829Smjacob case PCI_PRODUCT_LSI_FC919: 263101704Smjacob case PCI_PRODUCT_LSI_FC929: 264101704Smjacob mpt->is_fc = 1; 265101704Smjacob break; 266101704Smjacob default: 267101704Smjacob break; 268101704Smjacob } 269101704Smjacob mpt->dev = dev; 270101704Smjacob mpt->unit = device_get_unit(dev); 271101704Smjacob mpt_set_options(mpt); 272101704Smjacob mpt->verbose += (bootverbose != 0)? 1 : 0; 273101704Smjacob 274101704Smjacob /* Make sure memory access decoders are enabled */ 275101704Smjacob cmd = pci_read_config(dev, PCIR_COMMAND, 2); 276101704Smjacob if ((cmd & PCIM_CMD_MEMEN) == 0) { 277101704Smjacob device_printf(dev, "Memory accesses disabled"); 278101704Smjacob goto bad; 279101704Smjacob } 280101704Smjacob 281101704Smjacob /* 282101704Smjacob * Make sure that SERR, PERR, WRITE INVALIDATE and BUSMASTER are set. 283101704Smjacob */ 284101704Smjacob cmd |= 285101704Smjacob PCIM_CMD_SERRESPEN | PCIM_CMD_PERRESPEN | 286101704Smjacob PCIM_CMD_BUSMASTEREN | PCIM_CMD_MWRICEN; 287101704Smjacob pci_write_config(dev, PCIR_COMMAND, cmd, 2); 288101704Smjacob 289101704Smjacob /* 290101704Smjacob * Make sure we've disabled the ROM. 291101704Smjacob */ 292101704Smjacob data = pci_read_config(dev, PCIR_BIOS, 4); 293101704Smjacob data &= ~1; 294101704Smjacob pci_write_config(dev, PCIR_BIOS, data, 4); 295101704Smjacob 296101704Smjacob 297102303Smjacob /* 298102303Smjacob * Is this part a dual? 299102303Smjacob * If so, link with our partner (around yet) 300102303Smjacob */ 301102303Smjacob if ((pci_get_device(dev) & ~1) == PCI_PRODUCT_LSI_FC929 || 302102303Smjacob (pci_get_device(dev) & ~1) == PCI_PRODUCT_LSI_1030) { 303102303Smjacob mpt_link_peer(mpt); 304101704Smjacob } 305101704Smjacob 306101704Smjacob /* Set up the memory regions */ 307101704Smjacob /* Allocate kernel virtual memory for the 9x9's Mem0 region */ 308101704Smjacob mpt->pci_reg_id = MEM_MAP_REG; 309101704Smjacob mpt->pci_reg = bus_alloc_resource(dev, SYS_RES_MEMORY, 310101704Smjacob &mpt->pci_reg_id, 0, ~0, 0, RF_ACTIVE); 311101704Smjacob if (mpt->pci_reg == NULL) { 312101704Smjacob device_printf(dev, "unable to map any ports\n"); 313101704Smjacob goto bad; 314101704Smjacob } 315101704Smjacob mpt->pci_st = rman_get_bustag(mpt->pci_reg); 316101704Smjacob mpt->pci_sh = rman_get_bushandle(mpt->pci_reg); 317101704Smjacob /* Get the Physical Address */ 318101704Smjacob mpt->pci_pa = rman_get_start(mpt->pci_reg); 319101704Smjacob 320101704Smjacob /* Get a handle to the interrupt */ 321101704Smjacob iqd = 0; 322101704Smjacob mpt->pci_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &iqd, 0, ~0, 323101704Smjacob 1, RF_ACTIVE | RF_SHAREABLE); 324101704Smjacob if (mpt->pci_irq == NULL) { 325101704Smjacob device_printf(dev, "could not allocate interrupt\n"); 326101704Smjacob goto bad; 327101704Smjacob } 328101704Smjacob 329101704Smjacob /* Register the interrupt handler */ 330102199Smjacob if (bus_setup_intr(dev, mpt->pci_irq, MPT_IFLAGS, mpt_pci_intr, 331101704Smjacob mpt, &mpt->ih)) { 332101704Smjacob device_printf(dev, "could not setup interrupt\n"); 333101704Smjacob goto bad; 334101704Smjacob } 335101704Smjacob 336102199Smjacob MPT_LOCK_SETUP(mpt); 337102199Smjacob 338101704Smjacob /* Disable interrupts at the part */ 339101704Smjacob mpt_disable_ints(mpt); 340101704Smjacob 341101704Smjacob /* Allocate dma memory */ 342101704Smjacob if (mpt_dma_mem_alloc(mpt)) { 343101704Smjacob device_printf(dev, "Could not allocate DMA memory\n"); 344101704Smjacob goto bad; 345101704Smjacob } 346101704Smjacob 347102199Smjacob /* 348102199Smjacob * Save the PCI config register values 349102199Smjacob * 350102199Smjacob * Hard resets are known to screw up the BAR for diagnostic 351102199Smjacob * memory accesses (Mem1). 352102199Smjacob * 353102199Smjacob * Using Mem1 is known to make the chip stop responding to 354102199Smjacob * configuration space transfers, so we need to save it now 355102199Smjacob */ 356101704Smjacob 357101704Smjacob mpt_read_config_regs(mpt); 358101704Smjacob 359101704Smjacob /* Initialize the hardware */ 360101704Smjacob if (mpt->disabled == 0) { 361102199Smjacob MPT_LOCK(mpt); 362102199Smjacob if (mpt_init(mpt, MPT_DB_INIT_HOST) != 0) { 363102199Smjacob MPT_UNLOCK(mpt); 364101704Smjacob goto bad; 365102199Smjacob } 366101704Smjacob 367102199Smjacob /* 368102199Smjacob * Attach to CAM 369102199Smjacob */ 370102199Smjacob MPTLOCK_2_CAMLOCK(mpt); 371101704Smjacob mpt_cam_attach(mpt); 372102199Smjacob CAMLOCK_2_MPTLOCK(mpt); 373102199Smjacob MPT_UNLOCK(mpt); 374101704Smjacob } 375101704Smjacob 376101704Smjacob return (0); 377101704Smjacob 378101704Smjacobbad: 379101704Smjacob mpt_dma_mem_free(mpt); 380101704Smjacob mpt_free_bus_resources(mpt); 381101704Smjacob 382101704Smjacob /* 383101704Smjacob * but return zero to preserve unit numbering 384101704Smjacob */ 385101704Smjacob return (0); 386101704Smjacob} 387101704Smjacob 388102199Smjacob/* 389101704Smjacob * Free bus resources 390101704Smjacob */ 391101704Smjacobstatic void 392102199Smjacobmpt_free_bus_resources(mpt_softc_t *mpt) 393101704Smjacob{ 394101704Smjacob if (mpt->ih) { 395101704Smjacob bus_teardown_intr(mpt->dev, mpt->pci_irq, mpt->ih); 396101704Smjacob mpt->ih = 0; 397101704Smjacob } 398101704Smjacob 399101704Smjacob if (mpt->pci_irq) { 400101704Smjacob bus_release_resource(mpt->dev, SYS_RES_IRQ, 0, mpt->pci_irq); 401101704Smjacob mpt->pci_irq = 0; 402101704Smjacob } 403101704Smjacob 404101704Smjacob if (mpt->pci_reg) { 405101704Smjacob bus_release_resource(mpt->dev, SYS_RES_MEMORY, mpt->pci_reg_id, 406101704Smjacob mpt->pci_reg); 407101704Smjacob mpt->pci_reg = 0; 408101704Smjacob } 409102199Smjacob MPT_LOCK_DESTROY(mpt); 410101704Smjacob} 411101704Smjacob 412101704Smjacob 413102199Smjacob/* 414101704Smjacob * Disconnect ourselves from the system. 415101704Smjacob */ 416101704Smjacobstatic int 417101704Smjacobmpt_detach(device_t dev) 418101704Smjacob{ 419102199Smjacob mpt_softc_t *mpt; 420102199Smjacob mpt = (mpt_softc_t*) device_get_softc(dev); 421101704Smjacob 422103914Smjacob mpt_prt(mpt, "mpt_detach"); 423101704Smjacob 424101704Smjacob if (mpt) { 425101704Smjacob mpt_disable_ints(mpt); 426101704Smjacob mpt_cam_detach(mpt); 427101704Smjacob mpt_reset(mpt); 428101704Smjacob mpt_dma_mem_free(mpt); 429101704Smjacob mpt_free_bus_resources(mpt); 430101704Smjacob } 431101704Smjacob return(0); 432101704Smjacob} 433101704Smjacob 434101704Smjacob 435102199Smjacob/* 436101704Smjacob * Disable the hardware 437101704Smjacob */ 438101704Smjacobstatic int 439101704Smjacobmpt_shutdown(device_t dev) 440101704Smjacob{ 441102199Smjacob mpt_softc_t *mpt; 442102199Smjacob mpt = (mpt_softc_t*) device_get_softc(dev); 443101704Smjacob 444101704Smjacob if (mpt) { 445101704Smjacob mpt_reset(mpt); 446101704Smjacob } 447101704Smjacob return(0); 448101704Smjacob} 449101704Smjacob 450101704Smjacob 451101704Smjacobstruct imush { 452102199Smjacob mpt_softc_t *mpt; 453101704Smjacob int error; 454101704Smjacob u_int32_t phys; 455101704Smjacob}; 456101704Smjacob 457101704Smjacobstatic void 458101704Smjacobmpt_map_rquest(void *arg, bus_dma_segment_t *segs, int nseg, int error) 459101704Smjacob{ 460101704Smjacob struct imush *imushp = (struct imush *) arg; 461101704Smjacob imushp->error = error; 462101704Smjacob imushp->phys = segs->ds_addr; 463101704Smjacob} 464101704Smjacob 465101704Smjacob 466101704Smjacobstatic int 467102199Smjacobmpt_dma_mem_alloc(mpt_softc_t *mpt) 468101704Smjacob{ 469101704Smjacob int i, error; 470101704Smjacob u_char *vptr; 471101704Smjacob u_int32_t pptr, end; 472103871Smjacob size_t len; 473101704Smjacob struct imush im; 474101704Smjacob device_t dev = mpt->dev; 475101704Smjacob 476101704Smjacob /* Check if we alreay have allocated the reply memory */ 477103871Smjacob if (mpt->reply_phys != NULL) { 478101704Smjacob return 0; 479103871Smjacob } 480101704Smjacob 481103871Smjacob len = sizeof (request_t *) * MPT_REQ_MEM_SIZE(mpt); 482103871Smjacob#ifdef RELENG_4 483111119Simp mpt->request_pool = (request_t *) malloc(len, M_DEVBUF, M_WAITOK); 484103871Smjacob if (mpt->request_pool == NULL) { 485103871Smjacob device_printf(dev, "cannot allocate request pool\n"); 486103871Smjacob return (1); 487103871Smjacob } 488103871Smjacob bzero(mpt->request_pool, len); 489103871Smjacob#else 490103871Smjacob mpt->request_pool = (request_t *) 491111119Simp malloc(len, M_DEVBUF, M_WAITOK | M_ZERO); 492103871Smjacob if (mpt->request_pool == NULL) { 493103871Smjacob device_printf(dev, "cannot allocate request pool\n"); 494103871Smjacob return (1); 495103871Smjacob } 496103871Smjacob#endif 497103871Smjacob 498101704Smjacob /* 499101704Smjacob * Create a dma tag for this device 500101704Smjacob * 501101704Smjacob * Align at page boundaries, limit to 32-bit addressing 502101704Smjacob * (The chip supports 64-bit addressing, but this driver doesn't) 503101704Smjacob */ 504101704Smjacob if (bus_dma_tag_create(NULL, PAGE_SIZE, 0, BUS_SPACE_MAXADDR_32BIT, 505101704Smjacob BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE_32BIT, 506101704Smjacob BUS_SPACE_MAXSIZE_32BIT, BUS_SPACE_UNRESTRICTED, 0, 507117126Sscottl busdma_lock_mutex, &Giant, &mpt->parent_dmat) != 0) { 508101704Smjacob device_printf(dev, "cannot create parent dma tag\n"); 509101704Smjacob return (1); 510101704Smjacob } 511101704Smjacob 512101704Smjacob /* Create a child tag for reply buffers */ 513101704Smjacob if (bus_dma_tag_create(mpt->parent_dmat, PAGE_SIZE, 514101704Smjacob 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 515101704Smjacob NULL, NULL, PAGE_SIZE, 1, BUS_SPACE_MAXSIZE_32BIT, 0, 516117126Sscottl busdma_lock_mutex, &Giant, &mpt->reply_dmat) != 0) { 517101704Smjacob device_printf(dev, "cannot create a dma tag for replies\n"); 518101704Smjacob return (1); 519101704Smjacob } 520101704Smjacob 521101704Smjacob /* Allocate some DMA accessable memory for replies */ 522101704Smjacob if (bus_dmamem_alloc(mpt->reply_dmat, (void **)&mpt->reply, 523101704Smjacob BUS_DMA_NOWAIT, &mpt->reply_dmap) != 0) { 524111346Sobrien device_printf(dev, "cannot allocate %lu bytes of reply memory\n", 525111346Sobrien (u_long)PAGE_SIZE); 526101704Smjacob return (1); 527101704Smjacob } 528101704Smjacob 529101704Smjacob im.mpt = mpt; 530101704Smjacob im.error = 0; 531101704Smjacob 532101704Smjacob /* Load and lock it into "bus space" */ 533101704Smjacob bus_dmamap_load(mpt->reply_dmat, mpt->reply_dmap, mpt->reply, 534101704Smjacob PAGE_SIZE, mpt_map_rquest, &im, 0); 535101704Smjacob 536101704Smjacob if (im.error) { 537101704Smjacob device_printf(dev, 538101704Smjacob "error %d loading dma map for DMA reply queue\n", im.error); 539101704Smjacob return (1); 540101704Smjacob } 541101704Smjacob mpt->reply_phys = im.phys; 542101704Smjacob 543101704Smjacob /* Create a child tag for data buffers */ 544101704Smjacob if (bus_dma_tag_create(mpt->parent_dmat, PAGE_SIZE, 545101704Smjacob 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 546101704Smjacob NULL, NULL, MAXBSIZE, MPT_SGL_MAX, BUS_SPACE_MAXSIZE_32BIT, 0, 547117126Sscottl busdma_lock_mutex, &Giant, &mpt->buffer_dmat) != 0) { 548101704Smjacob device_printf(dev, 549101704Smjacob "cannot create a dma tag for data buffers\n"); 550101704Smjacob return (1); 551101704Smjacob } 552101704Smjacob 553101704Smjacob /* Create a child tag for request buffers */ 554101704Smjacob if (bus_dma_tag_create(mpt->parent_dmat, PAGE_SIZE, 555101704Smjacob 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 556103871Smjacob NULL, NULL, MPT_REQ_MEM_SIZE(mpt), 1, BUS_SPACE_MAXSIZE_32BIT, 0, 557117126Sscottl busdma_lock_mutex, &Giant, &mpt->request_dmat) != 0) { 558101704Smjacob device_printf(dev, "cannot create a dma tag for requests\n"); 559101704Smjacob return (1); 560101704Smjacob } 561101704Smjacob 562101704Smjacob /* Allocate some DMA accessable memory for requests */ 563101704Smjacob if (bus_dmamem_alloc(mpt->request_dmat, (void **)&mpt->request, 564101704Smjacob BUS_DMA_NOWAIT, &mpt->request_dmap) != 0) { 565101704Smjacob device_printf(dev, 566101704Smjacob "cannot allocate %d bytes of request memory\n", 567103871Smjacob MPT_REQ_MEM_SIZE(mpt)); 568101704Smjacob return (1); 569101704Smjacob } 570101704Smjacob 571101704Smjacob im.mpt = mpt; 572101704Smjacob im.error = 0; 573101704Smjacob 574101704Smjacob /* Load and lock it into "bus space" */ 575101704Smjacob bus_dmamap_load(mpt->request_dmat, mpt->request_dmap, mpt->request, 576103871Smjacob MPT_REQ_MEM_SIZE(mpt), mpt_map_rquest, &im, 0); 577101704Smjacob 578101704Smjacob if (im.error) { 579101704Smjacob device_printf(dev, 580101704Smjacob "error %d loading dma map for DMA request queue\n", 581101704Smjacob im.error); 582101704Smjacob return (1); 583101704Smjacob } 584101704Smjacob mpt->request_phys = im.phys; 585101704Smjacob 586101704Smjacob i = 0; 587101704Smjacob pptr = mpt->request_phys; 588101704Smjacob vptr = mpt->request; 589103871Smjacob end = pptr + MPT_REQ_MEM_SIZE(mpt); 590101704Smjacob while(pptr < end) { 591103871Smjacob request_t *req = &mpt->request_pool[i]; 592101704Smjacob req->index = i++; 593101704Smjacob 594101704Smjacob /* Store location of Request Data */ 595101704Smjacob req->req_pbuf = pptr; 596101704Smjacob req->req_vbuf = vptr; 597101704Smjacob 598101704Smjacob pptr += MPT_REQUEST_AREA; 599101704Smjacob vptr += MPT_REQUEST_AREA; 600101704Smjacob 601101704Smjacob req->sense_pbuf = (pptr - MPT_SENSE_SIZE); 602101704Smjacob req->sense_vbuf = (vptr - MPT_SENSE_SIZE); 603101704Smjacob 604101704Smjacob error = bus_dmamap_create(mpt->buffer_dmat, 0, &req->dmap); 605101704Smjacob if (error) { 606101704Smjacob device_printf(dev, 607101704Smjacob "error %d creating per-cmd DMA maps\n", error); 608101704Smjacob return (1); 609101704Smjacob } 610101704Smjacob } 611101704Smjacob return (0); 612101704Smjacob} 613101704Smjacob 614101704Smjacob 615101704Smjacob 616101704Smjacob/* Deallocate memory that was allocated by mpt_dma_mem_alloc 617101704Smjacob */ 618101704Smjacobstatic void 619102199Smjacobmpt_dma_mem_free(mpt_softc_t *mpt) 620101704Smjacob{ 621101704Smjacob int i; 622101704Smjacob 623101704Smjacob /* Make sure we aren't double destroying */ 624101704Smjacob if (mpt->reply_dmat == 0) { 625101704Smjacob if (mpt->verbose) 626101704Smjacob device_printf(mpt->dev,"Already released dma memory\n"); 627101704Smjacob return; 628101704Smjacob } 629101704Smjacob 630103871Smjacob for (i = 0; i < MPT_MAX_REQUESTS(mpt); i++) { 631103871Smjacob bus_dmamap_destroy(mpt->buffer_dmat, mpt->request_pool[i].dmap); 632101704Smjacob } 633101704Smjacob bus_dmamap_unload(mpt->request_dmat, mpt->request_dmap); 634101704Smjacob bus_dmamem_free(mpt->request_dmat, mpt->request, mpt->request_dmap); 635101704Smjacob bus_dma_tag_destroy(mpt->request_dmat); 636101704Smjacob bus_dma_tag_destroy(mpt->buffer_dmat); 637101704Smjacob bus_dmamap_unload(mpt->reply_dmat, mpt->reply_dmap); 638101704Smjacob bus_dmamem_free(mpt->reply_dmat, mpt->reply, mpt->reply_dmap); 639101704Smjacob bus_dma_tag_destroy(mpt->reply_dmat); 640101704Smjacob bus_dma_tag_destroy(mpt->parent_dmat); 641101704Smjacob mpt->reply_dmat = 0; 642103871Smjacob free(mpt->request_pool, M_DEVBUF); 643103871Smjacob mpt->request_pool = 0; 644101704Smjacob 645101704Smjacob} 646101704Smjacob 647101704Smjacob 648101704Smjacob 649101704Smjacob/* Reads modifiable (via PCI transactions) config registers */ 650101704Smjacobstatic void 651102199Smjacobmpt_read_config_regs(mpt_softc_t *mpt) 652101704Smjacob{ 653101704Smjacob mpt->pci_cfg.Command = pci_read_config(mpt->dev, PCIR_COMMAND, 2); 654101704Smjacob mpt->pci_cfg.LatencyTimer_LineSize = 655101704Smjacob pci_read_config(mpt->dev, PCIR_CACHELNSZ, 2); 656119690Sjhb mpt->pci_cfg.IO_BAR = pci_read_config(mpt->dev, PCIR_BAR(0), 4); 657119690Sjhb mpt->pci_cfg.Mem0_BAR[0] = pci_read_config(mpt->dev, PCIR_BAR(1), 4); 658119690Sjhb mpt->pci_cfg.Mem0_BAR[1] = pci_read_config(mpt->dev, PCIR_BAR(2), 4); 659119690Sjhb mpt->pci_cfg.Mem1_BAR[0] = pci_read_config(mpt->dev, PCIR_BAR(3), 4); 660119690Sjhb mpt->pci_cfg.Mem1_BAR[1] = pci_read_config(mpt->dev, PCIR_BAR(4), 4); 661101704Smjacob mpt->pci_cfg.ROM_BAR = pci_read_config(mpt->dev, PCIR_BIOS, 4); 662101704Smjacob mpt->pci_cfg.IntLine = pci_read_config(mpt->dev, PCIR_INTLINE, 1); 663101704Smjacob mpt->pci_cfg.PMCSR = pci_read_config(mpt->dev, 0x44, 4); 664101704Smjacob} 665101704Smjacob 666101704Smjacob/* Sets modifiable config registers */ 667101704Smjacobvoid 668102199Smjacobmpt_set_config_regs(mpt_softc_t *mpt) 669101704Smjacob{ 670101704Smjacob u_int32_t val; 671101704Smjacob 672101704Smjacob#define MPT_CHECK(reg, offset, size) \ 673101704Smjacob val = pci_read_config(mpt->dev, offset, size); \ 674101704Smjacob if (mpt->pci_cfg.reg != val) { \ 675103914Smjacob mpt_prt(mpt, \ 676101704Smjacob "Restoring " #reg " to 0x%X from 0x%X\n", \ 677101704Smjacob mpt->pci_cfg.reg, val); \ 678101704Smjacob } 679101704Smjacob 680101704Smjacob if (mpt->verbose) { 681101704Smjacob MPT_CHECK(Command, PCIR_COMMAND, 2); 682101704Smjacob MPT_CHECK(LatencyTimer_LineSize, PCIR_CACHELNSZ, 2); 683119690Sjhb MPT_CHECK(IO_BAR, PCIR_BAR(0), 4); 684119690Sjhb MPT_CHECK(Mem0_BAR[0], PCIR_BAR(1), 4); 685119690Sjhb MPT_CHECK(Mem0_BAR[1], PCIR_BAR(2), 4); 686119690Sjhb MPT_CHECK(Mem1_BAR[0], PCIR_BAR(3), 4); 687119690Sjhb MPT_CHECK(Mem1_BAR[1], PCIR_BAR(4), 4); 688101704Smjacob MPT_CHECK(ROM_BAR, PCIR_BIOS, 4); 689101704Smjacob MPT_CHECK(IntLine, PCIR_INTLINE, 1); 690101704Smjacob MPT_CHECK(PMCSR, 0x44, 4); 691101704Smjacob } 692101704Smjacob#undef MPT_CHECK 693101704Smjacob 694101704Smjacob pci_write_config(mpt->dev, PCIR_COMMAND, mpt->pci_cfg.Command, 2); 695101704Smjacob pci_write_config(mpt->dev, PCIR_CACHELNSZ, 696101704Smjacob mpt->pci_cfg.LatencyTimer_LineSize, 2); 697119690Sjhb pci_write_config(mpt->dev, PCIR_BAR(0), mpt->pci_cfg.IO_BAR, 4); 698119690Sjhb pci_write_config(mpt->dev, PCIR_BAR(1), mpt->pci_cfg.Mem0_BAR[0], 4); 699119690Sjhb pci_write_config(mpt->dev, PCIR_BAR(2), mpt->pci_cfg.Mem0_BAR[1], 4); 700119690Sjhb pci_write_config(mpt->dev, PCIR_BAR(3), mpt->pci_cfg.Mem1_BAR[0], 4); 701119690Sjhb pci_write_config(mpt->dev, PCIR_BAR(4), mpt->pci_cfg.Mem1_BAR[1], 4); 702101704Smjacob pci_write_config(mpt->dev, PCIR_BIOS, mpt->pci_cfg.ROM_BAR, 4); 703101704Smjacob pci_write_config(mpt->dev, PCIR_INTLINE, mpt->pci_cfg.IntLine, 1); 704101704Smjacob pci_write_config(mpt->dev, 0x44, mpt->pci_cfg.PMCSR, 4); 705101704Smjacob} 706102199Smjacob 707102199Smjacobstatic void 708102199Smjacobmpt_pci_intr(void *arg) 709102199Smjacob{ 710102199Smjacob mpt_softc_t *mpt = arg; 711102199Smjacob MPT_LOCK(mpt); 712103829Smjacob (void) mpt_intr(mpt); 713102199Smjacob MPT_UNLOCK(mpt); 714102199Smjacob} 715