19b631363SMatt Jacob /* $FreeBSD$ */ 29b631363SMatt Jacob /* 3eae4a35fSMatt Jacob * PCI specific probe and attach routines for LSI Fusion Adapters 49b631363SMatt Jacob * FreeBSD Version. 59b631363SMatt Jacob * 69b631363SMatt Jacob * Copyright (c) 2000, 2001 by Greg Ansley 79b631363SMatt Jacob * Partially derived from Matt Jacob's ISP driver. 89b631363SMatt Jacob * 99b631363SMatt Jacob * Redistribution and use in source and binary forms, with or without 109b631363SMatt Jacob * modification, are permitted provided that the following conditions 119b631363SMatt Jacob * are met: 129b631363SMatt Jacob * 1. Redistributions of source code must retain the above copyright 139b631363SMatt Jacob * notice immediately at the beginning of the file, without modification, 149b631363SMatt Jacob * this list of conditions, and the following disclaimer. 159b631363SMatt Jacob * 2. The name of the author may not be used to endorse or promote products 169b631363SMatt Jacob * derived from this software without specific prior written permission. 179b631363SMatt Jacob * 189b631363SMatt Jacob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 199b631363SMatt Jacob * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 209b631363SMatt Jacob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 219b631363SMatt Jacob * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 229b631363SMatt Jacob * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 239b631363SMatt Jacob * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 249b631363SMatt Jacob * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 259b631363SMatt Jacob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 269b631363SMatt Jacob * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 279b631363SMatt Jacob * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 289b631363SMatt Jacob * SUCH DAMAGE. 299b631363SMatt Jacob */ 309b631363SMatt Jacob /* 319b631363SMatt Jacob * Additional Copyright (c) 2002 by Matthew Jacob under same license. 329b631363SMatt Jacob */ 339b631363SMatt Jacob 349b631363SMatt Jacob #include <sys/param.h> 359b631363SMatt Jacob #include <sys/systm.h> 369b631363SMatt Jacob #include <sys/kernel.h> 379b631363SMatt Jacob #include <sys/module.h> 387104aeefSMatt Jacob #include <sys/bus.h> 397104aeefSMatt Jacob 409b631363SMatt Jacob #include <pci/pcireg.h> 419b631363SMatt Jacob #include <pci/pcivar.h> 429b631363SMatt Jacob 439b631363SMatt Jacob #include <machine/bus_memio.h> 449b631363SMatt Jacob #include <machine/bus_pio.h> 459b631363SMatt Jacob #include <machine/bus.h> 469b631363SMatt Jacob #include <machine/resource.h> 479b631363SMatt Jacob #include <sys/rman.h> 487104aeefSMatt Jacob #include <sys/malloc.h> 499b631363SMatt Jacob 509b631363SMatt Jacob #include <dev/mpt/mpt_freebsd.h> 519b631363SMatt Jacob 529b631363SMatt Jacob #ifndef PCI_VENDOR_LSI 539b631363SMatt Jacob #define PCI_VENDOR_LSI 0x1000 549b631363SMatt Jacob #endif 559b631363SMatt Jacob 569b631363SMatt Jacob #ifndef PCI_PRODUCT_LSI_FC909 579b631363SMatt Jacob #define PCI_PRODUCT_LSI_FC909 0x0620 589b631363SMatt Jacob #endif 599b631363SMatt Jacob 60eae4a35fSMatt Jacob #ifndef PCI_PRODUCT_LSI_FC909A 61eae4a35fSMatt Jacob #define PCI_PRODUCT_LSI_FC909A 0x0621 62eae4a35fSMatt Jacob #endif 63eae4a35fSMatt Jacob 64aca01e38SMatt Jacob #ifndef PCI_PRODUCT_LSI_FC919 65aca01e38SMatt Jacob #define PCI_PRODUCT_LSI_FC919 0x0624 66aca01e38SMatt Jacob #endif 67aca01e38SMatt Jacob 689b631363SMatt Jacob #ifndef PCI_PRODUCT_LSI_FC929 699b631363SMatt Jacob #define PCI_PRODUCT_LSI_FC929 0x0622 709b631363SMatt Jacob #endif 719b631363SMatt Jacob 729b631363SMatt Jacob #ifndef PCI_PRODUCT_LSI_1030 739b631363SMatt Jacob #define PCI_PRODUCT_LSI_1030 0x0030 749b631363SMatt Jacob #endif 759b631363SMatt Jacob 76019717e0SMatt Jacob #ifndef PCIM_CMD_SERRESPEN 77019717e0SMatt Jacob #define PCIM_CMD_SERRESPEN 0x0100 78019717e0SMatt Jacob #endif 79019717e0SMatt Jacob 809b631363SMatt Jacob 819b631363SMatt Jacob 829b631363SMatt Jacob #define MEM_MAP_REG 0x14 839b631363SMatt Jacob #define MEM_MAP_SRAM 0x1C 849b631363SMatt Jacob 859b631363SMatt Jacob static int mpt_probe(device_t); 869b631363SMatt Jacob static int mpt_attach(device_t); 877104aeefSMatt Jacob static void mpt_free_bus_resources(mpt_softc_t *mpt); 889b631363SMatt Jacob static int mpt_detach(device_t); 899b631363SMatt Jacob static int mpt_shutdown(device_t); 907104aeefSMatt Jacob static int mpt_dma_mem_alloc(mpt_softc_t *mpt); 917104aeefSMatt Jacob static void mpt_dma_mem_free(mpt_softc_t *mpt); 927104aeefSMatt Jacob static void mpt_read_config_regs(mpt_softc_t *mpt); 937104aeefSMatt Jacob static void mpt_pci_intr(void *); 949b631363SMatt Jacob 959b631363SMatt Jacob static device_method_t mpt_methods[] = { 969b631363SMatt Jacob /* Device interface */ 979b631363SMatt Jacob DEVMETHOD(device_probe, mpt_probe), 989b631363SMatt Jacob DEVMETHOD(device_attach, mpt_attach), 999b631363SMatt Jacob DEVMETHOD(device_detach, mpt_detach), 1009b631363SMatt Jacob DEVMETHOD(device_shutdown, mpt_shutdown), 1019b631363SMatt Jacob { 0, 0 } 1029b631363SMatt Jacob }; 1039b631363SMatt Jacob 1049b631363SMatt Jacob static driver_t mpt_driver = { 1057104aeefSMatt Jacob "mpt", mpt_methods, sizeof (mpt_softc_t) 1069b631363SMatt Jacob }; 1079b631363SMatt Jacob static devclass_t mpt_devclass; 1089b631363SMatt Jacob DRIVER_MODULE(mpt, pci, mpt_driver, mpt_devclass, 0, 0); 1099b631363SMatt Jacob MODULE_VERSION(mpt, 1); 1109b631363SMatt Jacob 1119b631363SMatt Jacob int 1129b631363SMatt Jacob mpt_intr(void *dummy) 1139b631363SMatt Jacob { 114aca01e38SMatt Jacob int nrepl = 0; 1159b631363SMatt Jacob u_int32_t reply; 1167104aeefSMatt Jacob mpt_softc_t *mpt = (mpt_softc_t *)dummy; 1179b631363SMatt Jacob 118aca01e38SMatt Jacob if ((mpt_read(mpt, MPT_OFFSET_INTR_STATUS) & MPT_INTR_REPLY_READY) == 0) 119aca01e38SMatt Jacob return (0); 1209b631363SMatt Jacob reply = mpt_pop_reply_queue(mpt); 1219b631363SMatt Jacob while (reply != MPT_REPLY_EMPTY) { 122aca01e38SMatt Jacob nrepl++; 1239b631363SMatt Jacob if (mpt->verbose > 1) { 1249b631363SMatt Jacob if ((reply & MPT_CONTEXT_REPLY) != 0) { 1259b631363SMatt Jacob /* Address reply; IOC has something to say */ 1269b631363SMatt Jacob mpt_print_reply(MPT_REPLY_PTOV(mpt, reply)); 1279b631363SMatt Jacob } else { 1289b631363SMatt Jacob /* Context reply ; all went well */ 129301472c2SMatt Jacob mpt_prt(mpt, "context %u reply OK", reply); 1309b631363SMatt Jacob } 1319b631363SMatt Jacob } 1329b631363SMatt Jacob mpt_done(mpt, reply); 1339b631363SMatt Jacob reply = mpt_pop_reply_queue(mpt); 1349b631363SMatt Jacob } 135aca01e38SMatt Jacob return (nrepl != 0); 1369b631363SMatt Jacob } 1379b631363SMatt Jacob 1389b631363SMatt Jacob static int 1399b631363SMatt Jacob mpt_probe(device_t dev) 1409b631363SMatt Jacob { 1419b631363SMatt Jacob char *desc; 1429b631363SMatt Jacob 1439b631363SMatt Jacob if (pci_get_vendor(dev) != PCI_VENDOR_LSI) 1449b631363SMatt Jacob return (ENXIO); 1459b631363SMatt Jacob 1469b631363SMatt Jacob switch ((pci_get_device(dev) & ~1)) { 1479b631363SMatt Jacob case PCI_PRODUCT_LSI_FC909: 1489b631363SMatt Jacob desc = "LSILogic FC909 FC Adapter"; 1499b631363SMatt Jacob break; 150eae4a35fSMatt Jacob case PCI_PRODUCT_LSI_FC909A: 151eae4a35fSMatt Jacob desc = "LSILogic FC909A FC Adapter"; 152eae4a35fSMatt Jacob break; 153aca01e38SMatt Jacob case PCI_PRODUCT_LSI_FC919: 154aca01e38SMatt Jacob desc = "LSILogic FC919 FC Adapter"; 155aca01e38SMatt Jacob break; 1569b631363SMatt Jacob case PCI_PRODUCT_LSI_FC929: 1579b631363SMatt Jacob desc = "LSILogic FC929 FC Adapter"; 1589b631363SMatt Jacob break; 1599b631363SMatt Jacob case PCI_PRODUCT_LSI_1030: 1609b631363SMatt Jacob desc = "LSILogic 1030 Ultra4 Adapter"; 1619b631363SMatt Jacob break; 1629b631363SMatt Jacob default: 1639b631363SMatt Jacob return (ENXIO); 1649b631363SMatt Jacob } 1659b631363SMatt Jacob 1669b631363SMatt Jacob device_set_desc(dev, desc); 1679b631363SMatt Jacob return (0); 1689b631363SMatt Jacob } 1699b631363SMatt Jacob 1709b631363SMatt Jacob #ifdef RELENG_4 1719b631363SMatt Jacob static void 1727104aeefSMatt Jacob mpt_set_options(mpt_softc_t *mpt) 1739b631363SMatt Jacob { 1749b631363SMatt Jacob int bitmap; 1759b631363SMatt Jacob 1769b631363SMatt Jacob bitmap = 0; 1779b631363SMatt Jacob if (getenv_int("mpt_disable", &bitmap)) { 1789b631363SMatt Jacob if (bitmap & (1 << mpt->unit)) { 1799b631363SMatt Jacob mpt->disabled = 1; 1809b631363SMatt Jacob } 1819b631363SMatt Jacob } 1829b631363SMatt Jacob 1839b631363SMatt Jacob bitmap = 0; 1849b631363SMatt Jacob if (getenv_int("mpt_debug", &bitmap)) { 1859b631363SMatt Jacob if (bitmap & (1 << mpt->unit)) { 1869b631363SMatt Jacob mpt->verbose = 2; 1879b631363SMatt Jacob } 1889b631363SMatt Jacob } 1899b631363SMatt Jacob 1909b631363SMatt Jacob } 1919b631363SMatt Jacob #else 1929b631363SMatt Jacob static void 1937104aeefSMatt Jacob mpt_set_options(mpt_softc_t *mpt) 1949b631363SMatt Jacob { 1959b631363SMatt Jacob int tval; 1969b631363SMatt Jacob 1979b631363SMatt Jacob tval = 0; 1989b631363SMatt Jacob if (resource_int_value(device_get_name(mpt->dev), 1999b631363SMatt Jacob device_get_unit(mpt->dev), "disable", &tval) == 0 && tval != 0) { 2009b631363SMatt Jacob mpt->disabled = 1; 2019b631363SMatt Jacob } 2029b631363SMatt Jacob tval = 0; 2039b631363SMatt Jacob if (resource_int_value(device_get_name(mpt->dev), 2049b631363SMatt Jacob device_get_unit(mpt->dev), "debug", &tval) == 0 && tval != 0) { 2059b631363SMatt Jacob mpt->verbose += tval; 2069b631363SMatt Jacob } 2079b631363SMatt Jacob } 2089b631363SMatt Jacob #endif 2099b631363SMatt Jacob 2109b631363SMatt Jacob 211019717e0SMatt Jacob static void 212019717e0SMatt Jacob mpt_link_peer(mpt_softc_t *mpt) 213019717e0SMatt Jacob { 214019717e0SMatt Jacob mpt_softc_t *mpt2; 215019717e0SMatt Jacob 216019717e0SMatt Jacob if (mpt->unit == 0) { 217019717e0SMatt Jacob return; 218019717e0SMatt Jacob } 219019717e0SMatt Jacob 220019717e0SMatt Jacob /* 221019717e0SMatt Jacob * XXX: depends on probe order 222019717e0SMatt Jacob */ 223019717e0SMatt Jacob mpt2 = (mpt_softc_t *) devclass_get_softc(mpt_devclass, mpt->unit-1); 224019717e0SMatt Jacob 225019717e0SMatt Jacob if (mpt2 == NULL) { 226019717e0SMatt Jacob return; 227019717e0SMatt Jacob } 228019717e0SMatt Jacob if (pci_get_vendor(mpt2->dev) != pci_get_vendor(mpt->dev)) { 229019717e0SMatt Jacob return; 230019717e0SMatt Jacob } 231019717e0SMatt Jacob if (pci_get_device(mpt2->dev) != pci_get_device(mpt->dev)) { 232019717e0SMatt Jacob return; 233019717e0SMatt Jacob } 234019717e0SMatt Jacob mpt->mpt2 = mpt2; 235019717e0SMatt Jacob mpt2->mpt2 = mpt; 236019717e0SMatt Jacob if (mpt->verbose) { 237301472c2SMatt Jacob mpt_prt(mpt, "linking with peer (mpt%d)", 238019717e0SMatt Jacob device_get_unit(mpt2->dev)); 239019717e0SMatt Jacob } 240019717e0SMatt Jacob } 241019717e0SMatt Jacob 242019717e0SMatt Jacob 2439b631363SMatt Jacob static int 2449b631363SMatt Jacob mpt_attach(device_t dev) 2459b631363SMatt Jacob { 2469b631363SMatt Jacob int iqd; 2479b631363SMatt Jacob u_int32_t data, cmd; 2487104aeefSMatt Jacob mpt_softc_t *mpt; 2499b631363SMatt Jacob 2509b631363SMatt Jacob /* Allocate the softc structure */ 2517104aeefSMatt Jacob mpt = (mpt_softc_t*) device_get_softc(dev); 2529b631363SMatt Jacob if (mpt == NULL) { 2539b631363SMatt Jacob device_printf(dev, "cannot allocate softc\n"); 2549b631363SMatt Jacob return (ENOMEM); 2559b631363SMatt Jacob } 2567104aeefSMatt Jacob bzero(mpt, sizeof (mpt_softc_t)); 2579b631363SMatt Jacob switch ((pci_get_device(dev) & ~1)) { 2589b631363SMatt Jacob case PCI_PRODUCT_LSI_FC909: 259eae4a35fSMatt Jacob case PCI_PRODUCT_LSI_FC909A: 260aca01e38SMatt Jacob case PCI_PRODUCT_LSI_FC919: 2619b631363SMatt Jacob case PCI_PRODUCT_LSI_FC929: 2629b631363SMatt Jacob mpt->is_fc = 1; 2639b631363SMatt Jacob break; 2649b631363SMatt Jacob default: 2659b631363SMatt Jacob break; 2669b631363SMatt Jacob } 2679b631363SMatt Jacob mpt->dev = dev; 2689b631363SMatt Jacob mpt->unit = device_get_unit(dev); 2699b631363SMatt Jacob mpt_set_options(mpt); 2709b631363SMatt Jacob mpt->verbose += (bootverbose != 0)? 1 : 0; 2719b631363SMatt Jacob 2729b631363SMatt Jacob /* Make sure memory access decoders are enabled */ 2739b631363SMatt Jacob cmd = pci_read_config(dev, PCIR_COMMAND, 2); 2749b631363SMatt Jacob if ((cmd & PCIM_CMD_MEMEN) == 0) { 2759b631363SMatt Jacob device_printf(dev, "Memory accesses disabled"); 2769b631363SMatt Jacob goto bad; 2779b631363SMatt Jacob } 2789b631363SMatt Jacob 2799b631363SMatt Jacob /* 2809b631363SMatt Jacob * Make sure that SERR, PERR, WRITE INVALIDATE and BUSMASTER are set. 2819b631363SMatt Jacob */ 2829b631363SMatt Jacob cmd |= 2839b631363SMatt Jacob PCIM_CMD_SERRESPEN | PCIM_CMD_PERRESPEN | 2849b631363SMatt Jacob PCIM_CMD_BUSMASTEREN | PCIM_CMD_MWRICEN; 2859b631363SMatt Jacob pci_write_config(dev, PCIR_COMMAND, cmd, 2); 2869b631363SMatt Jacob 2879b631363SMatt Jacob /* 2889b631363SMatt Jacob * Make sure we've disabled the ROM. 2899b631363SMatt Jacob */ 2909b631363SMatt Jacob data = pci_read_config(dev, PCIR_BIOS, 4); 2919b631363SMatt Jacob data &= ~1; 2929b631363SMatt Jacob pci_write_config(dev, PCIR_BIOS, data, 4); 2939b631363SMatt Jacob 2949b631363SMatt Jacob 295019717e0SMatt Jacob /* 296019717e0SMatt Jacob * Is this part a dual? 297019717e0SMatt Jacob * If so, link with our partner (around yet) 298019717e0SMatt Jacob */ 299019717e0SMatt Jacob if ((pci_get_device(dev) & ~1) == PCI_PRODUCT_LSI_FC929 || 300019717e0SMatt Jacob (pci_get_device(dev) & ~1) == PCI_PRODUCT_LSI_1030) { 301019717e0SMatt Jacob mpt_link_peer(mpt); 3029b631363SMatt Jacob } 3039b631363SMatt Jacob 3049b631363SMatt Jacob /* Set up the memory regions */ 3059b631363SMatt Jacob /* Allocate kernel virtual memory for the 9x9's Mem0 region */ 3069b631363SMatt Jacob mpt->pci_reg_id = MEM_MAP_REG; 3079b631363SMatt Jacob mpt->pci_reg = bus_alloc_resource(dev, SYS_RES_MEMORY, 3089b631363SMatt Jacob &mpt->pci_reg_id, 0, ~0, 0, RF_ACTIVE); 3099b631363SMatt Jacob if (mpt->pci_reg == NULL) { 3109b631363SMatt Jacob device_printf(dev, "unable to map any ports\n"); 3119b631363SMatt Jacob goto bad; 3129b631363SMatt Jacob } 3139b631363SMatt Jacob mpt->pci_st = rman_get_bustag(mpt->pci_reg); 3149b631363SMatt Jacob mpt->pci_sh = rman_get_bushandle(mpt->pci_reg); 3159b631363SMatt Jacob /* Get the Physical Address */ 3169b631363SMatt Jacob mpt->pci_pa = rman_get_start(mpt->pci_reg); 3179b631363SMatt Jacob 3189b631363SMatt Jacob /* Get a handle to the interrupt */ 3199b631363SMatt Jacob iqd = 0; 3209b631363SMatt Jacob mpt->pci_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &iqd, 0, ~0, 3219b631363SMatt Jacob 1, RF_ACTIVE | RF_SHAREABLE); 3229b631363SMatt Jacob if (mpt->pci_irq == NULL) { 3239b631363SMatt Jacob device_printf(dev, "could not allocate interrupt\n"); 3249b631363SMatt Jacob goto bad; 3259b631363SMatt Jacob } 3269b631363SMatt Jacob 3279b631363SMatt Jacob /* Register the interrupt handler */ 3287104aeefSMatt Jacob if (bus_setup_intr(dev, mpt->pci_irq, MPT_IFLAGS, mpt_pci_intr, 3299b631363SMatt Jacob mpt, &mpt->ih)) { 3309b631363SMatt Jacob device_printf(dev, "could not setup interrupt\n"); 3319b631363SMatt Jacob goto bad; 3329b631363SMatt Jacob } 3339b631363SMatt Jacob 3347104aeefSMatt Jacob MPT_LOCK_SETUP(mpt); 3357104aeefSMatt Jacob 3369b631363SMatt Jacob /* Disable interrupts at the part */ 3379b631363SMatt Jacob mpt_disable_ints(mpt); 3389b631363SMatt Jacob 3399b631363SMatt Jacob /* Allocate dma memory */ 3409b631363SMatt Jacob if (mpt_dma_mem_alloc(mpt)) { 3419b631363SMatt Jacob device_printf(dev, "Could not allocate DMA memory\n"); 3429b631363SMatt Jacob goto bad; 3439b631363SMatt Jacob } 3449b631363SMatt Jacob 3457104aeefSMatt Jacob /* 3467104aeefSMatt Jacob * Save the PCI config register values 3477104aeefSMatt Jacob * 3487104aeefSMatt Jacob * Hard resets are known to screw up the BAR for diagnostic 3497104aeefSMatt Jacob * memory accesses (Mem1). 3507104aeefSMatt Jacob * 3517104aeefSMatt Jacob * Using Mem1 is known to make the chip stop responding to 3527104aeefSMatt Jacob * configuration space transfers, so we need to save it now 3537104aeefSMatt Jacob */ 3549b631363SMatt Jacob 3559b631363SMatt Jacob mpt_read_config_regs(mpt); 3569b631363SMatt Jacob 3579b631363SMatt Jacob /* Initialize the hardware */ 3589b631363SMatt Jacob if (mpt->disabled == 0) { 3597104aeefSMatt Jacob MPT_LOCK(mpt); 3607104aeefSMatt Jacob if (mpt_init(mpt, MPT_DB_INIT_HOST) != 0) { 3617104aeefSMatt Jacob MPT_UNLOCK(mpt); 3629b631363SMatt Jacob goto bad; 3639b631363SMatt Jacob } 3649b631363SMatt Jacob 3657104aeefSMatt Jacob /* 3667104aeefSMatt Jacob * Attach to CAM 3677104aeefSMatt Jacob */ 3687104aeefSMatt Jacob MPTLOCK_2_CAMLOCK(mpt); 3697104aeefSMatt Jacob mpt_cam_attach(mpt); 3707104aeefSMatt Jacob CAMLOCK_2_MPTLOCK(mpt); 3717104aeefSMatt Jacob MPT_UNLOCK(mpt); 3727104aeefSMatt Jacob } 3737104aeefSMatt Jacob 3749b631363SMatt Jacob return (0); 3759b631363SMatt Jacob 3769b631363SMatt Jacob bad: 3779b631363SMatt Jacob mpt_dma_mem_free(mpt); 3789b631363SMatt Jacob mpt_free_bus_resources(mpt); 3799b631363SMatt Jacob 3809b631363SMatt Jacob /* 3819b631363SMatt Jacob * but return zero to preserve unit numbering 3829b631363SMatt Jacob */ 3839b631363SMatt Jacob return (0); 3849b631363SMatt Jacob } 3859b631363SMatt Jacob 3867104aeefSMatt Jacob /* 3879b631363SMatt Jacob * Free bus resources 3889b631363SMatt Jacob */ 3899b631363SMatt Jacob static void 3907104aeefSMatt Jacob mpt_free_bus_resources(mpt_softc_t *mpt) 3919b631363SMatt Jacob { 3929b631363SMatt Jacob if (mpt->ih) { 3939b631363SMatt Jacob bus_teardown_intr(mpt->dev, mpt->pci_irq, mpt->ih); 3949b631363SMatt Jacob mpt->ih = 0; 3959b631363SMatt Jacob } 3969b631363SMatt Jacob 3979b631363SMatt Jacob if (mpt->pci_irq) { 3989b631363SMatt Jacob bus_release_resource(mpt->dev, SYS_RES_IRQ, 0, mpt->pci_irq); 3999b631363SMatt Jacob mpt->pci_irq = 0; 4009b631363SMatt Jacob } 4019b631363SMatt Jacob 4029b631363SMatt Jacob if (mpt->pci_reg) { 4039b631363SMatt Jacob bus_release_resource(mpt->dev, SYS_RES_MEMORY, mpt->pci_reg_id, 4049b631363SMatt Jacob mpt->pci_reg); 4059b631363SMatt Jacob mpt->pci_reg = 0; 4069b631363SMatt Jacob } 4077104aeefSMatt Jacob MPT_LOCK_DESTROY(mpt); 4089b631363SMatt Jacob } 4099b631363SMatt Jacob 4109b631363SMatt Jacob 4117104aeefSMatt Jacob /* 4129b631363SMatt Jacob * Disconnect ourselves from the system. 4139b631363SMatt Jacob */ 4149b631363SMatt Jacob static int 4159b631363SMatt Jacob mpt_detach(device_t dev) 4169b631363SMatt Jacob { 4177104aeefSMatt Jacob mpt_softc_t *mpt; 4187104aeefSMatt Jacob mpt = (mpt_softc_t*) device_get_softc(dev); 4199b631363SMatt Jacob 420301472c2SMatt Jacob mpt_prt(mpt, "mpt_detach"); 4219b631363SMatt Jacob 4229b631363SMatt Jacob if (mpt) { 4239b631363SMatt Jacob mpt_disable_ints(mpt); 4249b631363SMatt Jacob mpt_cam_detach(mpt); 4259b631363SMatt Jacob mpt_reset(mpt); 4269b631363SMatt Jacob mpt_dma_mem_free(mpt); 4279b631363SMatt Jacob mpt_free_bus_resources(mpt); 4289b631363SMatt Jacob } 4299b631363SMatt Jacob return(0); 4309b631363SMatt Jacob } 4319b631363SMatt Jacob 4329b631363SMatt Jacob 4337104aeefSMatt Jacob /* 4349b631363SMatt Jacob * Disable the hardware 4359b631363SMatt Jacob */ 4369b631363SMatt Jacob static int 4379b631363SMatt Jacob mpt_shutdown(device_t dev) 4389b631363SMatt Jacob { 4397104aeefSMatt Jacob mpt_softc_t *mpt; 4407104aeefSMatt Jacob mpt = (mpt_softc_t*) device_get_softc(dev); 4419b631363SMatt Jacob 4429b631363SMatt Jacob if (mpt) { 4439b631363SMatt Jacob mpt_reset(mpt); 4449b631363SMatt Jacob } 4459b631363SMatt Jacob return(0); 4469b631363SMatt Jacob } 4479b631363SMatt Jacob 4489b631363SMatt Jacob 4499b631363SMatt Jacob struct imush { 4507104aeefSMatt Jacob mpt_softc_t *mpt; 4519b631363SMatt Jacob int error; 4529b631363SMatt Jacob u_int32_t phys; 4539b631363SMatt Jacob }; 4549b631363SMatt Jacob 4559b631363SMatt Jacob static void 4569b631363SMatt Jacob mpt_map_rquest(void *arg, bus_dma_segment_t *segs, int nseg, int error) 4579b631363SMatt Jacob { 4589b631363SMatt Jacob struct imush *imushp = (struct imush *) arg; 4599b631363SMatt Jacob imushp->error = error; 4609b631363SMatt Jacob imushp->phys = segs->ds_addr; 4619b631363SMatt Jacob } 4629b631363SMatt Jacob 4639b631363SMatt Jacob 4649b631363SMatt Jacob static int 4657104aeefSMatt Jacob mpt_dma_mem_alloc(mpt_softc_t *mpt) 4669b631363SMatt Jacob { 4679b631363SMatt Jacob int i, error; 4689b631363SMatt Jacob u_char *vptr; 4699b631363SMatt Jacob u_int32_t pptr, end; 4707dec90bcSMatt Jacob size_t len; 4719b631363SMatt Jacob struct imush im; 4729b631363SMatt Jacob device_t dev = mpt->dev; 4739b631363SMatt Jacob 4749b631363SMatt Jacob /* Check if we alreay have allocated the reply memory */ 4757dec90bcSMatt Jacob if (mpt->reply_phys != NULL) { 4769b631363SMatt Jacob return 0; 4777dec90bcSMatt Jacob } 4787dec90bcSMatt Jacob 4797dec90bcSMatt Jacob len = sizeof (request_t *) * MPT_REQ_MEM_SIZE(mpt); 4807dec90bcSMatt Jacob #ifdef RELENG_4 4817dec90bcSMatt Jacob mpt->request_pool = (request_t *) malloc(len, M_DEVBUF, M_WAITOK); 4827dec90bcSMatt Jacob if (mpt->request_pool == NULL) { 4837dec90bcSMatt Jacob device_printf(dev, "cannot allocate request pool\n"); 4847dec90bcSMatt Jacob return (1); 4857dec90bcSMatt Jacob } 4867dec90bcSMatt Jacob bzero(mpt->request_pool, len); 4877dec90bcSMatt Jacob #else 4887dec90bcSMatt Jacob mpt->request_pool = (request_t *) 4897dec90bcSMatt Jacob malloc(len, M_DEVBUF, M_WAITOK | M_ZERO); 4907dec90bcSMatt Jacob if (mpt->request_pool == NULL) { 4917dec90bcSMatt Jacob device_printf(dev, "cannot allocate request pool\n"); 4927dec90bcSMatt Jacob return (1); 4937dec90bcSMatt Jacob } 4947dec90bcSMatt Jacob #endif 4959b631363SMatt Jacob 4969b631363SMatt Jacob /* 4979b631363SMatt Jacob * Create a dma tag for this device 4989b631363SMatt Jacob * 4999b631363SMatt Jacob * Align at page boundaries, limit to 32-bit addressing 5009b631363SMatt Jacob * (The chip supports 64-bit addressing, but this driver doesn't) 5019b631363SMatt Jacob */ 5029b631363SMatt Jacob if (bus_dma_tag_create(NULL, PAGE_SIZE, 0, BUS_SPACE_MAXADDR_32BIT, 5039b631363SMatt Jacob BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE_32BIT, 5049b631363SMatt Jacob BUS_SPACE_MAXSIZE_32BIT, BUS_SPACE_UNRESTRICTED, 0, 5059b631363SMatt Jacob &mpt->parent_dmat) != 0) { 5069b631363SMatt Jacob device_printf(dev, "cannot create parent dma tag\n"); 5079b631363SMatt Jacob return (1); 5089b631363SMatt Jacob } 5099b631363SMatt Jacob 5109b631363SMatt Jacob /* Create a child tag for reply buffers */ 5119b631363SMatt Jacob if (bus_dma_tag_create(mpt->parent_dmat, PAGE_SIZE, 5129b631363SMatt Jacob 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 5139b631363SMatt Jacob NULL, NULL, PAGE_SIZE, 1, BUS_SPACE_MAXSIZE_32BIT, 0, 5149b631363SMatt Jacob &mpt->reply_dmat) != 0) { 5159b631363SMatt Jacob device_printf(dev, "cannot create a dma tag for replies\n"); 5169b631363SMatt Jacob return (1); 5179b631363SMatt Jacob } 5189b631363SMatt Jacob 5199b631363SMatt Jacob /* Allocate some DMA accessable memory for replies */ 5209b631363SMatt Jacob if (bus_dmamem_alloc(mpt->reply_dmat, (void **)&mpt->reply, 5219b631363SMatt Jacob BUS_DMA_NOWAIT, &mpt->reply_dmap) != 0) { 5229b631363SMatt Jacob device_printf(dev, "cannot allocate %d bytes of reply memory\n", 5239b631363SMatt Jacob PAGE_SIZE); 5249b631363SMatt Jacob return (1); 5259b631363SMatt Jacob } 5269b631363SMatt Jacob 5279b631363SMatt Jacob im.mpt = mpt; 5289b631363SMatt Jacob im.error = 0; 5299b631363SMatt Jacob 5309b631363SMatt Jacob /* Load and lock it into "bus space" */ 5319b631363SMatt Jacob bus_dmamap_load(mpt->reply_dmat, mpt->reply_dmap, mpt->reply, 5329b631363SMatt Jacob PAGE_SIZE, mpt_map_rquest, &im, 0); 5339b631363SMatt Jacob 5349b631363SMatt Jacob if (im.error) { 5359b631363SMatt Jacob device_printf(dev, 5369b631363SMatt Jacob "error %d loading dma map for DMA reply queue\n", im.error); 5379b631363SMatt Jacob return (1); 5389b631363SMatt Jacob } 5399b631363SMatt Jacob mpt->reply_phys = im.phys; 5409b631363SMatt Jacob 5419b631363SMatt Jacob /* Create a child tag for data buffers */ 5429b631363SMatt Jacob if (bus_dma_tag_create(mpt->parent_dmat, PAGE_SIZE, 5439b631363SMatt Jacob 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 5449b631363SMatt Jacob NULL, NULL, MAXBSIZE, MPT_SGL_MAX, BUS_SPACE_MAXSIZE_32BIT, 0, 5459b631363SMatt Jacob &mpt->buffer_dmat) != 0) { 5469b631363SMatt Jacob device_printf(dev, 5479b631363SMatt Jacob "cannot create a dma tag for data buffers\n"); 5489b631363SMatt Jacob return (1); 5499b631363SMatt Jacob } 5509b631363SMatt Jacob 5519b631363SMatt Jacob /* Create a child tag for request buffers */ 5529b631363SMatt Jacob if (bus_dma_tag_create(mpt->parent_dmat, PAGE_SIZE, 5539b631363SMatt Jacob 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 5547dec90bcSMatt Jacob NULL, NULL, MPT_REQ_MEM_SIZE(mpt), 1, BUS_SPACE_MAXSIZE_32BIT, 0, 5559b631363SMatt Jacob &mpt->request_dmat) != 0) { 5569b631363SMatt Jacob device_printf(dev, "cannot create a dma tag for requests\n"); 5579b631363SMatt Jacob return (1); 5589b631363SMatt Jacob } 5599b631363SMatt Jacob 5609b631363SMatt Jacob /* Allocate some DMA accessable memory for requests */ 5619b631363SMatt Jacob if (bus_dmamem_alloc(mpt->request_dmat, (void **)&mpt->request, 5629b631363SMatt Jacob BUS_DMA_NOWAIT, &mpt->request_dmap) != 0) { 5639b631363SMatt Jacob device_printf(dev, 5649b631363SMatt Jacob "cannot allocate %d bytes of request memory\n", 5657dec90bcSMatt Jacob MPT_REQ_MEM_SIZE(mpt)); 5669b631363SMatt Jacob return (1); 5679b631363SMatt Jacob } 5689b631363SMatt Jacob 5699b631363SMatt Jacob im.mpt = mpt; 5709b631363SMatt Jacob im.error = 0; 5719b631363SMatt Jacob 5729b631363SMatt Jacob /* Load and lock it into "bus space" */ 5739b631363SMatt Jacob bus_dmamap_load(mpt->request_dmat, mpt->request_dmap, mpt->request, 5747dec90bcSMatt Jacob MPT_REQ_MEM_SIZE(mpt), mpt_map_rquest, &im, 0); 5759b631363SMatt Jacob 5769b631363SMatt Jacob if (im.error) { 5779b631363SMatt Jacob device_printf(dev, 5789b631363SMatt Jacob "error %d loading dma map for DMA request queue\n", 5799b631363SMatt Jacob im.error); 5809b631363SMatt Jacob return (1); 5819b631363SMatt Jacob } 5829b631363SMatt Jacob mpt->request_phys = im.phys; 5839b631363SMatt Jacob 5849b631363SMatt Jacob i = 0; 5859b631363SMatt Jacob pptr = mpt->request_phys; 5869b631363SMatt Jacob vptr = mpt->request; 5877dec90bcSMatt Jacob end = pptr + MPT_REQ_MEM_SIZE(mpt); 5889b631363SMatt Jacob while(pptr < end) { 5897dec90bcSMatt Jacob request_t *req = &mpt->request_pool[i]; 5909b631363SMatt Jacob req->index = i++; 5919b631363SMatt Jacob 5929b631363SMatt Jacob /* Store location of Request Data */ 5939b631363SMatt Jacob req->req_pbuf = pptr; 5949b631363SMatt Jacob req->req_vbuf = vptr; 5959b631363SMatt Jacob 5969b631363SMatt Jacob pptr += MPT_REQUEST_AREA; 5979b631363SMatt Jacob vptr += MPT_REQUEST_AREA; 5989b631363SMatt Jacob 5999b631363SMatt Jacob req->sense_pbuf = (pptr - MPT_SENSE_SIZE); 6009b631363SMatt Jacob req->sense_vbuf = (vptr - MPT_SENSE_SIZE); 6019b631363SMatt Jacob 6029b631363SMatt Jacob error = bus_dmamap_create(mpt->buffer_dmat, 0, &req->dmap); 6039b631363SMatt Jacob if (error) { 6049b631363SMatt Jacob device_printf(dev, 6059b631363SMatt Jacob "error %d creating per-cmd DMA maps\n", error); 6069b631363SMatt Jacob return (1); 6079b631363SMatt Jacob } 6089b631363SMatt Jacob } 6099b631363SMatt Jacob return (0); 6109b631363SMatt Jacob } 6119b631363SMatt Jacob 6129b631363SMatt Jacob 6139b631363SMatt Jacob 6149b631363SMatt Jacob /* Deallocate memory that was allocated by mpt_dma_mem_alloc 6159b631363SMatt Jacob */ 6169b631363SMatt Jacob static void 6177104aeefSMatt Jacob mpt_dma_mem_free(mpt_softc_t *mpt) 6189b631363SMatt Jacob { 6199b631363SMatt Jacob int i; 6209b631363SMatt Jacob 6219b631363SMatt Jacob /* Make sure we aren't double destroying */ 6229b631363SMatt Jacob if (mpt->reply_dmat == 0) { 6239b631363SMatt Jacob if (mpt->verbose) 6249b631363SMatt Jacob device_printf(mpt->dev,"Already released dma memory\n"); 6259b631363SMatt Jacob return; 6269b631363SMatt Jacob } 6279b631363SMatt Jacob 6287dec90bcSMatt Jacob for (i = 0; i < MPT_MAX_REQUESTS(mpt); i++) { 6297dec90bcSMatt Jacob bus_dmamap_destroy(mpt->buffer_dmat, mpt->request_pool[i].dmap); 6309b631363SMatt Jacob } 6319b631363SMatt Jacob bus_dmamap_unload(mpt->request_dmat, mpt->request_dmap); 6329b631363SMatt Jacob bus_dmamem_free(mpt->request_dmat, mpt->request, mpt->request_dmap); 6339b631363SMatt Jacob bus_dma_tag_destroy(mpt->request_dmat); 6349b631363SMatt Jacob bus_dma_tag_destroy(mpt->buffer_dmat); 6359b631363SMatt Jacob bus_dmamap_unload(mpt->reply_dmat, mpt->reply_dmap); 6369b631363SMatt Jacob bus_dmamem_free(mpt->reply_dmat, mpt->reply, mpt->reply_dmap); 6379b631363SMatt Jacob bus_dma_tag_destroy(mpt->reply_dmat); 6389b631363SMatt Jacob bus_dma_tag_destroy(mpt->parent_dmat); 6399b631363SMatt Jacob mpt->reply_dmat = 0; 6407dec90bcSMatt Jacob free(mpt->request_pool, M_DEVBUF); 6417dec90bcSMatt Jacob mpt->request_pool = 0; 6429b631363SMatt Jacob 6439b631363SMatt Jacob } 6449b631363SMatt Jacob 6459b631363SMatt Jacob 6469b631363SMatt Jacob 6479b631363SMatt Jacob /* Reads modifiable (via PCI transactions) config registers */ 6489b631363SMatt Jacob static void 6497104aeefSMatt Jacob mpt_read_config_regs(mpt_softc_t *mpt) 6509b631363SMatt Jacob { 6519b631363SMatt Jacob mpt->pci_cfg.Command = pci_read_config(mpt->dev, PCIR_COMMAND, 2); 6529b631363SMatt Jacob mpt->pci_cfg.LatencyTimer_LineSize = 6539b631363SMatt Jacob pci_read_config(mpt->dev, PCIR_CACHELNSZ, 2); 6549b631363SMatt Jacob mpt->pci_cfg.IO_BAR = pci_read_config(mpt->dev, PCIR_MAPS, 4); 6559b631363SMatt Jacob mpt->pci_cfg.Mem0_BAR[0] = pci_read_config(mpt->dev, PCIR_MAPS+0x4, 4); 6569b631363SMatt Jacob mpt->pci_cfg.Mem0_BAR[1] = pci_read_config(mpt->dev, PCIR_MAPS+0x8, 4); 6579b631363SMatt Jacob mpt->pci_cfg.Mem1_BAR[0] = pci_read_config(mpt->dev, PCIR_MAPS+0xC, 4); 6589b631363SMatt Jacob mpt->pci_cfg.Mem1_BAR[1] = pci_read_config(mpt->dev, PCIR_MAPS+0x10, 4); 6599b631363SMatt Jacob mpt->pci_cfg.ROM_BAR = pci_read_config(mpt->dev, PCIR_BIOS, 4); 6609b631363SMatt Jacob mpt->pci_cfg.IntLine = pci_read_config(mpt->dev, PCIR_INTLINE, 1); 6619b631363SMatt Jacob mpt->pci_cfg.PMCSR = pci_read_config(mpt->dev, 0x44, 4); 6629b631363SMatt Jacob } 6639b631363SMatt Jacob 6649b631363SMatt Jacob /* Sets modifiable config registers */ 6659b631363SMatt Jacob void 6667104aeefSMatt Jacob mpt_set_config_regs(mpt_softc_t *mpt) 6679b631363SMatt Jacob { 6689b631363SMatt Jacob u_int32_t val; 6699b631363SMatt Jacob 6709b631363SMatt Jacob #define MPT_CHECK(reg, offset, size) \ 6719b631363SMatt Jacob val = pci_read_config(mpt->dev, offset, size); \ 6729b631363SMatt Jacob if (mpt->pci_cfg.reg != val) { \ 673301472c2SMatt Jacob mpt_prt(mpt, \ 6749b631363SMatt Jacob "Restoring " #reg " to 0x%X from 0x%X\n", \ 6759b631363SMatt Jacob mpt->pci_cfg.reg, val); \ 6769b631363SMatt Jacob } 6779b631363SMatt Jacob 6789b631363SMatt Jacob if (mpt->verbose) { 6799b631363SMatt Jacob MPT_CHECK(Command, PCIR_COMMAND, 2); 6809b631363SMatt Jacob MPT_CHECK(LatencyTimer_LineSize, PCIR_CACHELNSZ, 2); 6819b631363SMatt Jacob MPT_CHECK(IO_BAR, PCIR_MAPS, 4); 6829b631363SMatt Jacob MPT_CHECK(Mem0_BAR[0], PCIR_MAPS+0x4, 4); 6839b631363SMatt Jacob MPT_CHECK(Mem0_BAR[1], PCIR_MAPS+0x8, 4); 6849b631363SMatt Jacob MPT_CHECK(Mem1_BAR[0], PCIR_MAPS+0xC, 4); 6859b631363SMatt Jacob MPT_CHECK(Mem1_BAR[1], PCIR_MAPS+0x10, 4); 6869b631363SMatt Jacob MPT_CHECK(ROM_BAR, PCIR_BIOS, 4); 6879b631363SMatt Jacob MPT_CHECK(IntLine, PCIR_INTLINE, 1); 6889b631363SMatt Jacob MPT_CHECK(PMCSR, 0x44, 4); 6899b631363SMatt Jacob } 6909b631363SMatt Jacob #undef MPT_CHECK 6919b631363SMatt Jacob 6929b631363SMatt Jacob pci_write_config(mpt->dev, PCIR_COMMAND, mpt->pci_cfg.Command, 2); 6939b631363SMatt Jacob pci_write_config(mpt->dev, PCIR_CACHELNSZ, 6949b631363SMatt Jacob mpt->pci_cfg.LatencyTimer_LineSize, 2); 6959b631363SMatt Jacob pci_write_config(mpt->dev, PCIR_MAPS, mpt->pci_cfg.IO_BAR, 4); 6969b631363SMatt Jacob pci_write_config(mpt->dev, PCIR_MAPS+0x4, mpt->pci_cfg.Mem0_BAR[0], 4); 6979b631363SMatt Jacob pci_write_config(mpt->dev, PCIR_MAPS+0x8, mpt->pci_cfg.Mem0_BAR[1], 4); 6989b631363SMatt Jacob pci_write_config(mpt->dev, PCIR_MAPS+0xC, mpt->pci_cfg.Mem1_BAR[0], 4); 6999b631363SMatt Jacob pci_write_config(mpt->dev, PCIR_MAPS+0x10, mpt->pci_cfg.Mem1_BAR[1], 4); 7009b631363SMatt Jacob pci_write_config(mpt->dev, PCIR_BIOS, mpt->pci_cfg.ROM_BAR, 4); 7019b631363SMatt Jacob pci_write_config(mpt->dev, PCIR_INTLINE, mpt->pci_cfg.IntLine, 1); 7029b631363SMatt Jacob pci_write_config(mpt->dev, 0x44, mpt->pci_cfg.PMCSR, 4); 7039b631363SMatt Jacob } 7047104aeefSMatt Jacob 7057104aeefSMatt Jacob static void 7067104aeefSMatt Jacob mpt_pci_intr(void *arg) 7077104aeefSMatt Jacob { 7087104aeefSMatt Jacob mpt_softc_t *mpt = arg; 7097104aeefSMatt Jacob MPT_LOCK(mpt); 710aca01e38SMatt Jacob (void) mpt_intr(mpt); 7117104aeefSMatt Jacob MPT_UNLOCK(mpt); 7127104aeefSMatt Jacob } 713