19b631363SMatt Jacob /* $FreeBSD$ */ 29b631363SMatt Jacob /* 39b631363SMatt Jacob * PCI specific probe and attach routines for LSI '909 FC 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/bus.h> 389b631363SMatt Jacob #include <sys/conf.h> 399b631363SMatt Jacob #include <sys/malloc.h> 409b631363SMatt Jacob #include <sys/mbuf.h> 419b631363SMatt Jacob #include <sys/module.h> 429b631363SMatt Jacob #include <pci/pcireg.h> 439b631363SMatt Jacob #include <pci/pcivar.h> 449b631363SMatt Jacob 459b631363SMatt Jacob #include <machine/bus_memio.h> 469b631363SMatt Jacob #include <machine/bus_pio.h> 479b631363SMatt Jacob #include <machine/bus.h> 489b631363SMatt Jacob #include <machine/resource.h> 499b631363SMatt Jacob #include <sys/rman.h> 509b631363SMatt Jacob 519b631363SMatt Jacob #include <dev/mpt/mpt.h> 529b631363SMatt Jacob #include <dev/mpt/mpt_freebsd.h> 539b631363SMatt Jacob 549b631363SMatt Jacob #ifndef PCI_VENDOR_LSI 559b631363SMatt Jacob #define PCI_VENDOR_LSI 0x1000 569b631363SMatt Jacob #endif 579b631363SMatt Jacob 589b631363SMatt Jacob #ifndef PCI_PRODUCT_LSI_FC909 599b631363SMatt Jacob #define PCI_PRODUCT_LSI_FC909 0x0620 609b631363SMatt Jacob #endif 619b631363SMatt Jacob 629b631363SMatt Jacob #ifndef PCI_PRODUCT_LSI_FC929 639b631363SMatt Jacob #define PCI_PRODUCT_LSI_FC929 0x0622 649b631363SMatt Jacob #endif 659b631363SMatt Jacob 669b631363SMatt Jacob #ifndef PCI_PRODUCT_LSI_1030 679b631363SMatt Jacob #define PCI_PRODUCT_LSI_1030 0x0030 689b631363SMatt Jacob #endif 699b631363SMatt Jacob 709b631363SMatt Jacob 719b631363SMatt Jacob 729b631363SMatt Jacob #define MEM_MAP_REG 0x14 739b631363SMatt Jacob #define MEM_MAP_SRAM 0x1C 749b631363SMatt Jacob 759b631363SMatt Jacob static int mpt_probe(device_t); 769b631363SMatt Jacob static int mpt_attach(device_t); 779b631363SMatt Jacob static void mpt_free_bus_resources(struct mpt_softc *mpt); 789b631363SMatt Jacob static int mpt_detach(device_t); 799b631363SMatt Jacob static int mpt_shutdown(device_t); 809b631363SMatt Jacob static int mpt_dma_mem_alloc(struct mpt_softc *mpt); 819b631363SMatt Jacob static void mpt_dma_mem_free(struct mpt_softc *mpt); 829b631363SMatt Jacob static void mpt_read_config_regs(struct mpt_softc *mpt); 839b631363SMatt Jacob 849b631363SMatt Jacob static device_method_t mpt_methods[] = { 859b631363SMatt Jacob /* Device interface */ 869b631363SMatt Jacob DEVMETHOD(device_probe, mpt_probe), 879b631363SMatt Jacob DEVMETHOD(device_attach, mpt_attach), 889b631363SMatt Jacob DEVMETHOD(device_detach, mpt_detach), 899b631363SMatt Jacob DEVMETHOD(device_shutdown, mpt_shutdown), 909b631363SMatt Jacob { 0, 0 } 919b631363SMatt Jacob }; 929b631363SMatt Jacob 939b631363SMatt Jacob static driver_t mpt_driver = { 949b631363SMatt Jacob "mpt", mpt_methods, sizeof (struct mpt_softc) 959b631363SMatt Jacob }; 969b631363SMatt Jacob static devclass_t mpt_devclass; 979b631363SMatt Jacob DRIVER_MODULE(mpt, pci, mpt_driver, mpt_devclass, 0, 0); 989b631363SMatt Jacob MODULE_VERSION(mpt, 1); 999b631363SMatt Jacob 1009b631363SMatt Jacob int 1019b631363SMatt Jacob mpt_intr(void *dummy) 1029b631363SMatt Jacob { 1039b631363SMatt Jacob u_int32_t reply; 1049b631363SMatt Jacob struct mpt_softc *mpt = (struct mpt_softc *)dummy; 1059b631363SMatt Jacob 1069b631363SMatt Jacob reply = mpt_pop_reply_queue(mpt); 1079b631363SMatt Jacob while (reply != MPT_REPLY_EMPTY) { 1089b631363SMatt Jacob if (mpt->verbose > 1) { 1099b631363SMatt Jacob if ((reply & MPT_CONTEXT_REPLY) != 0) { 1109b631363SMatt Jacob /* Address reply; IOC has something to say */ 1119b631363SMatt Jacob mpt_print_reply(MPT_REPLY_PTOV(mpt, reply)); 1129b631363SMatt Jacob } else { 1139b631363SMatt Jacob /* Context reply ; all went well */ 1149b631363SMatt Jacob device_printf(mpt->dev, 1159b631363SMatt Jacob "context %u reply OK\n", reply); 1169b631363SMatt Jacob } 1179b631363SMatt Jacob } 1189b631363SMatt Jacob mpt_done(mpt, reply); 1199b631363SMatt Jacob reply = mpt_pop_reply_queue(mpt); 1209b631363SMatt Jacob } 1219b631363SMatt Jacob return 0; 1229b631363SMatt Jacob } 1239b631363SMatt Jacob 1249b631363SMatt Jacob static int 1259b631363SMatt Jacob mpt_probe(device_t dev) 1269b631363SMatt Jacob { 1279b631363SMatt Jacob char *desc; 1289b631363SMatt Jacob 1299b631363SMatt Jacob if (pci_get_vendor(dev) != PCI_VENDOR_LSI) 1309b631363SMatt Jacob return (ENXIO); 1319b631363SMatt Jacob 1329b631363SMatt Jacob switch ((pci_get_device(dev) & ~1)) { 1339b631363SMatt Jacob case PCI_PRODUCT_LSI_FC909: 1349b631363SMatt Jacob desc = "LSILogic FC909 FC Adapter"; 1359b631363SMatt Jacob break; 1369b631363SMatt Jacob case PCI_PRODUCT_LSI_FC929: 1379b631363SMatt Jacob desc = "LSILogic FC929 FC Adapter"; 1389b631363SMatt Jacob break; 1399b631363SMatt Jacob case PCI_PRODUCT_LSI_1030: 1409b631363SMatt Jacob desc = "LSILogic 1030 Ultra4 Adapter"; 1419b631363SMatt Jacob break; 1429b631363SMatt Jacob default: 1439b631363SMatt Jacob return (ENXIO); 1449b631363SMatt Jacob } 1459b631363SMatt Jacob 1469b631363SMatt Jacob device_set_desc(dev, desc); 1479b631363SMatt Jacob return (0); 1489b631363SMatt Jacob } 1499b631363SMatt Jacob 1509b631363SMatt Jacob #ifdef RELENG_4 1519b631363SMatt Jacob static void 1529b631363SMatt Jacob mpt_set_options(struct mpt_softc *mpt) 1539b631363SMatt Jacob { 1549b631363SMatt Jacob int bitmap; 1559b631363SMatt Jacob 1569b631363SMatt Jacob bitmap = 0; 1579b631363SMatt Jacob if (getenv_int("mpt_disable", &bitmap)) { 1589b631363SMatt Jacob if (bitmap & (1 << mpt->unit)) { 1599b631363SMatt Jacob mpt->disabled = 1; 1609b631363SMatt Jacob } 1619b631363SMatt Jacob } 1629b631363SMatt Jacob 1639b631363SMatt Jacob bitmap = 0; 1649b631363SMatt Jacob if (getenv_int("mpt_debug", &bitmap)) { 1659b631363SMatt Jacob if (bitmap & (1 << mpt->unit)) { 1669b631363SMatt Jacob mpt->verbose = 2; 1679b631363SMatt Jacob } 1689b631363SMatt Jacob } 1699b631363SMatt Jacob 1709b631363SMatt Jacob cmd = pci_read_config(dev, PCIR_COMMAND, 2); 1719b631363SMatt Jacob } 1729b631363SMatt Jacob #else 1739b631363SMatt Jacob static void 1749b631363SMatt Jacob mpt_set_options(struct mpt_softc *mpt) 1759b631363SMatt Jacob { 1769b631363SMatt Jacob int tval; 1779b631363SMatt Jacob 1789b631363SMatt Jacob tval = 0; 1799b631363SMatt Jacob if (resource_int_value(device_get_name(mpt->dev), 1809b631363SMatt Jacob device_get_unit(mpt->dev), "disable", &tval) == 0 && tval != 0) { 1819b631363SMatt Jacob mpt->disabled = 1; 1829b631363SMatt Jacob } 1839b631363SMatt Jacob tval = 0; 1849b631363SMatt Jacob if (resource_int_value(device_get_name(mpt->dev), 1859b631363SMatt Jacob device_get_unit(mpt->dev), "debug", &tval) == 0 && tval != 0) { 1869b631363SMatt Jacob mpt->verbose += tval; 1879b631363SMatt Jacob } 1889b631363SMatt Jacob } 1899b631363SMatt Jacob #endif 1909b631363SMatt Jacob 1919b631363SMatt Jacob 1929b631363SMatt Jacob static int 1939b631363SMatt Jacob mpt_attach(device_t dev) 1949b631363SMatt Jacob { 1959b631363SMatt Jacob int iqd; 1969b631363SMatt Jacob u_int32_t data, cmd; 1979b631363SMatt Jacob struct mpt_softc *mpt; 1989b631363SMatt Jacob 1999b631363SMatt Jacob /* Allocate the softc structure */ 2009b631363SMatt Jacob mpt = (struct mpt_softc*) device_get_softc(dev); 2019b631363SMatt Jacob if (mpt == NULL) { 2029b631363SMatt Jacob device_printf(dev, "cannot allocate softc\n"); 2039b631363SMatt Jacob return (ENOMEM); 2049b631363SMatt Jacob } 2059b631363SMatt Jacob bzero(mpt, sizeof (struct mpt_softc)); 2069b631363SMatt Jacob switch ((pci_get_device(dev) & ~1)) { 2079b631363SMatt Jacob case PCI_PRODUCT_LSI_FC909: 2089b631363SMatt Jacob case PCI_PRODUCT_LSI_FC929: 2099b631363SMatt Jacob mpt->is_fc = 1; 2109b631363SMatt Jacob break; 2119b631363SMatt Jacob default: 2129b631363SMatt Jacob break; 2139b631363SMatt Jacob } 2149b631363SMatt Jacob mpt->dev = dev; 2159b631363SMatt Jacob mpt->unit = device_get_unit(dev); 2169b631363SMatt Jacob mpt_set_options(mpt); 2179b631363SMatt Jacob mpt->verbose += (bootverbose != 0)? 1 : 0; 2189b631363SMatt Jacob 2199b631363SMatt Jacob /* Make sure memory access decoders are enabled */ 2209b631363SMatt Jacob cmd = pci_read_config(dev, PCIR_COMMAND, 2); 2219b631363SMatt Jacob if ((cmd & PCIM_CMD_MEMEN) == 0) { 2229b631363SMatt Jacob device_printf(dev, "Memory accesses disabled"); 2239b631363SMatt Jacob goto bad; 2249b631363SMatt Jacob } 2259b631363SMatt Jacob 2269b631363SMatt Jacob /* 2279b631363SMatt Jacob * Make sure that SERR, PERR, WRITE INVALIDATE and BUSMASTER are set. 2289b631363SMatt Jacob */ 2299b631363SMatt Jacob cmd |= 2309b631363SMatt Jacob PCIM_CMD_SERRESPEN | PCIM_CMD_PERRESPEN | 2319b631363SMatt Jacob PCIM_CMD_BUSMASTEREN | PCIM_CMD_MWRICEN; 2329b631363SMatt Jacob pci_write_config(dev, PCIR_COMMAND, cmd, 2); 2339b631363SMatt Jacob 2349b631363SMatt Jacob /* 2359b631363SMatt Jacob * Make sure we've disabled the ROM. 2369b631363SMatt Jacob */ 2379b631363SMatt Jacob data = pci_read_config(dev, PCIR_BIOS, 4); 2389b631363SMatt Jacob data &= ~1; 2399b631363SMatt Jacob pci_write_config(dev, PCIR_BIOS, data, 4); 2409b631363SMatt Jacob 2419b631363SMatt Jacob 2429b631363SMatt Jacob /* Is this part a dual? */ 2439b631363SMatt Jacob if ((pci_get_device(dev) & ~1) == PCI_PRODUCT_LSI_FC929) { 2449b631363SMatt Jacob /* Yes; is the previous device the counterpart? */ 2459b631363SMatt Jacob if (mpt->unit) { 2469b631363SMatt Jacob mpt->mpt2 = (struct mpt_softc *) 2479b631363SMatt Jacob devclass_get_softc(mpt_devclass, mpt->unit-1); 2489b631363SMatt Jacob 2499b631363SMatt Jacob if ((mpt->mpt2->mpt2 == NULL) 2509b631363SMatt Jacob && (pci_get_vendor(mpt->mpt2->dev) == PCI_VENDOR_LSI) 2519b631363SMatt Jacob && ((pci_get_device(mpt->mpt2->dev) & ~1) == PCI_PRODUCT_LSI_FC929) ) { 2529b631363SMatt Jacob /* Yes */ 2539b631363SMatt Jacob mpt->mpt2->mpt2 = mpt; 2549b631363SMatt Jacob if (mpt->verbose) { 2559b631363SMatt Jacob device_printf(dev, "Detected dual\n"); 2569b631363SMatt Jacob } 2579b631363SMatt Jacob } else { 2589b631363SMatt Jacob /* Nope */ 2599b631363SMatt Jacob mpt->mpt2 = NULL; 2609b631363SMatt Jacob } 2619b631363SMatt Jacob } 2629b631363SMatt Jacob } 2639b631363SMatt Jacob 2649b631363SMatt Jacob /* Set up the memory regions */ 2659b631363SMatt Jacob /* Allocate kernel virtual memory for the 9x9's Mem0 region */ 2669b631363SMatt Jacob mpt->pci_reg_id = MEM_MAP_REG; 2679b631363SMatt Jacob mpt->pci_reg = bus_alloc_resource(dev, SYS_RES_MEMORY, 2689b631363SMatt Jacob &mpt->pci_reg_id, 0, ~0, 0, RF_ACTIVE); 2699b631363SMatt Jacob if (mpt->pci_reg == NULL) { 2709b631363SMatt Jacob device_printf(dev, "unable to map any ports\n"); 2719b631363SMatt Jacob goto bad; 2729b631363SMatt Jacob } 2739b631363SMatt Jacob mpt->pci_st = rman_get_bustag(mpt->pci_reg); 2749b631363SMatt Jacob mpt->pci_sh = rman_get_bushandle(mpt->pci_reg); 2759b631363SMatt Jacob /* Get the Physical Address */ 2769b631363SMatt Jacob mpt->pci_pa = rman_get_start(mpt->pci_reg); 2779b631363SMatt Jacob 2789b631363SMatt Jacob /* Get a handle to the interrupt */ 2799b631363SMatt Jacob iqd = 0; 2809b631363SMatt Jacob mpt->pci_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &iqd, 0, ~0, 2819b631363SMatt Jacob 1, RF_ACTIVE | RF_SHAREABLE); 2829b631363SMatt Jacob if (mpt->pci_irq == NULL) { 2839b631363SMatt Jacob device_printf(dev, "could not allocate interrupt\n"); 2849b631363SMatt Jacob goto bad; 2859b631363SMatt Jacob } 2869b631363SMatt Jacob 2879b631363SMatt Jacob /* Register the interrupt handler */ 2889b631363SMatt Jacob if (bus_setup_intr(dev, mpt->pci_irq, 2899b631363SMatt Jacob INTR_TYPE_CAM, (void (*)(void *))mpt_intr, 2909b631363SMatt Jacob mpt, &mpt->ih)) { 2919b631363SMatt Jacob device_printf(dev, "could not setup interrupt\n"); 2929b631363SMatt Jacob goto bad; 2939b631363SMatt Jacob } 2949b631363SMatt Jacob 2959b631363SMatt Jacob /* Disable interrupts at the part */ 2969b631363SMatt Jacob mpt_disable_ints(mpt); 2979b631363SMatt Jacob 2989b631363SMatt Jacob /* Allocate dma memory */ 2999b631363SMatt Jacob if (mpt_dma_mem_alloc(mpt)) { 3009b631363SMatt Jacob device_printf(dev, "Could not allocate DMA memory\n"); 3019b631363SMatt Jacob goto bad; 3029b631363SMatt Jacob } 3039b631363SMatt Jacob 3049b631363SMatt Jacob /* Initialize character device */ 3059b631363SMatt Jacob /* currently closed */ 3069b631363SMatt Jacob mpt->open = 0; 3079b631363SMatt Jacob 3089b631363SMatt Jacob /* Save the PCI config register values */ 3099b631363SMatt Jacob /* Hard resets are known to screw up the BAR for diagnostic 3109b631363SMatt Jacob memory accesses (Mem1). */ 3119b631363SMatt Jacob /* Using Mem1 is known to make the chip stop responding to 3129b631363SMatt Jacob configuration space transfers, so we need to save it now */ 3139b631363SMatt Jacob mpt_read_config_regs(mpt); 3149b631363SMatt Jacob 3159b631363SMatt Jacob /* Initialize the hardware */ 3169b631363SMatt Jacob if (mpt->disabled == 0) { 3179b631363SMatt Jacob if (mpt_init(mpt, MPT_DB_INIT_HOST) != 0) 3189b631363SMatt Jacob goto bad; 3199b631363SMatt Jacob 3209b631363SMatt Jacob /* Attach to CAM */ 3219b631363SMatt Jacob mpt_cam_attach(mpt); 3229b631363SMatt Jacob } 3239b631363SMatt Jacob 3249b631363SMatt Jacob /* Done */ 3259b631363SMatt Jacob return (0); 3269b631363SMatt Jacob 3279b631363SMatt Jacob bad: 3289b631363SMatt Jacob mpt_dma_mem_free(mpt); 3299b631363SMatt Jacob mpt_free_bus_resources(mpt); 3309b631363SMatt Jacob 3319b631363SMatt Jacob /* 3329b631363SMatt Jacob * but return zero to preserve unit numbering 3339b631363SMatt Jacob */ 3349b631363SMatt Jacob return (0); 3359b631363SMatt Jacob } 3369b631363SMatt Jacob 3379b631363SMatt Jacob /****************************************************************************** 3389b631363SMatt Jacob * Free bus resources 3399b631363SMatt Jacob */ 3409b631363SMatt Jacob static void 3419b631363SMatt Jacob mpt_free_bus_resources(struct mpt_softc *mpt) 3429b631363SMatt Jacob { 3439b631363SMatt Jacob if (mpt->ih) { 3449b631363SMatt Jacob bus_teardown_intr(mpt->dev, mpt->pci_irq, mpt->ih); 3459b631363SMatt Jacob mpt->ih = 0; 3469b631363SMatt Jacob } 3479b631363SMatt Jacob 3489b631363SMatt Jacob if (mpt->pci_irq) { 3499b631363SMatt Jacob bus_release_resource(mpt->dev, SYS_RES_IRQ, 0, mpt->pci_irq); 3509b631363SMatt Jacob mpt->pci_irq = 0; 3519b631363SMatt Jacob } 3529b631363SMatt Jacob 3539b631363SMatt Jacob if (mpt->pci_reg) { 3549b631363SMatt Jacob bus_release_resource(mpt->dev, SYS_RES_MEMORY, mpt->pci_reg_id, 3559b631363SMatt Jacob mpt->pci_reg); 3569b631363SMatt Jacob mpt->pci_reg = 0; 3579b631363SMatt Jacob } 3589b631363SMatt Jacob if (mpt->pci_mem) { 3599b631363SMatt Jacob bus_release_resource(mpt->dev, SYS_RES_MEMORY, mpt->pci_mem_id, 3609b631363SMatt Jacob mpt->pci_mem); 3619b631363SMatt Jacob mpt->pci_mem = 0; 3629b631363SMatt Jacob } 3639b631363SMatt Jacob 3649b631363SMatt Jacob } 3659b631363SMatt Jacob 3669b631363SMatt Jacob 3679b631363SMatt Jacob /****************************************************************************** 3689b631363SMatt Jacob * Disconnect ourselves from the system. 3699b631363SMatt Jacob */ 3709b631363SMatt Jacob static int 3719b631363SMatt Jacob mpt_detach(device_t dev) 3729b631363SMatt Jacob { 3739b631363SMatt Jacob struct mpt_softc *mpt; 3749b631363SMatt Jacob mpt = (struct mpt_softc*) device_get_softc(dev); 3759b631363SMatt Jacob 3769b631363SMatt Jacob device_printf(mpt->dev,"mpt_detach!\n"); 3779b631363SMatt Jacob 3789b631363SMatt Jacob if (mpt) { 3799b631363SMatt Jacob mpt_disable_ints(mpt); 3809b631363SMatt Jacob mpt_cam_detach(mpt); 3819b631363SMatt Jacob mpt_reset(mpt); 3829b631363SMatt Jacob mpt_dma_mem_free(mpt); 3839b631363SMatt Jacob mpt_free_bus_resources(mpt); 3849b631363SMatt Jacob } 3859b631363SMatt Jacob return(0); 3869b631363SMatt Jacob } 3879b631363SMatt Jacob 3889b631363SMatt Jacob 3899b631363SMatt Jacob /****************************************************************************** 3909b631363SMatt Jacob * Disable the hardware 3919b631363SMatt Jacob */ 3929b631363SMatt Jacob static int 3939b631363SMatt Jacob mpt_shutdown(device_t dev) 3949b631363SMatt Jacob { 3959b631363SMatt Jacob struct mpt_softc *mpt; 3969b631363SMatt Jacob mpt = (struct mpt_softc*) device_get_softc(dev); 3979b631363SMatt Jacob 3989b631363SMatt Jacob if (mpt) { 3999b631363SMatt Jacob mpt_reset(mpt); 4009b631363SMatt Jacob } 4019b631363SMatt Jacob return(0); 4029b631363SMatt Jacob } 4039b631363SMatt Jacob 4049b631363SMatt Jacob 4059b631363SMatt Jacob struct imush { 4069b631363SMatt Jacob struct mpt_softc *mpt; 4079b631363SMatt Jacob int error; 4089b631363SMatt Jacob u_int32_t phys; 4099b631363SMatt Jacob }; 4109b631363SMatt Jacob 4119b631363SMatt Jacob static void 4129b631363SMatt Jacob mpt_map_rquest(void *arg, bus_dma_segment_t *segs, int nseg, int error) 4139b631363SMatt Jacob { 4149b631363SMatt Jacob struct imush *imushp = (struct imush *) arg; 4159b631363SMatt Jacob imushp->error = error; 4169b631363SMatt Jacob imushp->phys = segs->ds_addr; 4179b631363SMatt Jacob } 4189b631363SMatt Jacob 4199b631363SMatt Jacob 4209b631363SMatt Jacob static int 4219b631363SMatt Jacob mpt_dma_mem_alloc(struct mpt_softc *mpt) 4229b631363SMatt Jacob { 4239b631363SMatt Jacob int i, error; 4249b631363SMatt Jacob u_char *vptr; 4259b631363SMatt Jacob u_int32_t pptr, end; 4269b631363SMatt Jacob struct imush im; 4279b631363SMatt Jacob device_t dev = mpt->dev; 4289b631363SMatt Jacob 4299b631363SMatt Jacob /* Check if we alreay have allocated the reply memory */ 4309b631363SMatt Jacob if (mpt->reply_phys != NULL) 4319b631363SMatt Jacob return 0; 4329b631363SMatt Jacob 4339b631363SMatt Jacob /* 4349b631363SMatt Jacob * Create a dma tag for this device 4359b631363SMatt Jacob * 4369b631363SMatt Jacob * Align at page boundaries, limit to 32-bit addressing 4379b631363SMatt Jacob * (The chip supports 64-bit addressing, but this driver doesn't) 4389b631363SMatt Jacob */ 4399b631363SMatt Jacob if (bus_dma_tag_create(NULL, PAGE_SIZE, 0, BUS_SPACE_MAXADDR_32BIT, 4409b631363SMatt Jacob BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE_32BIT, 4419b631363SMatt Jacob BUS_SPACE_MAXSIZE_32BIT, BUS_SPACE_UNRESTRICTED, 0, 4429b631363SMatt Jacob &mpt->parent_dmat) != 0) { 4439b631363SMatt Jacob device_printf(dev, "cannot create parent dma tag\n"); 4449b631363SMatt Jacob return (1); 4459b631363SMatt Jacob } 4469b631363SMatt Jacob 4479b631363SMatt Jacob /* Create a child tag for reply buffers */ 4489b631363SMatt Jacob if (bus_dma_tag_create(mpt->parent_dmat, PAGE_SIZE, 4499b631363SMatt Jacob 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 4509b631363SMatt Jacob NULL, NULL, PAGE_SIZE, 1, BUS_SPACE_MAXSIZE_32BIT, 0, 4519b631363SMatt Jacob &mpt->reply_dmat) != 0) { 4529b631363SMatt Jacob device_printf(dev, "cannot create a dma tag for replies\n"); 4539b631363SMatt Jacob return (1); 4549b631363SMatt Jacob } 4559b631363SMatt Jacob 4569b631363SMatt Jacob /* Allocate some DMA accessable memory for replies */ 4579b631363SMatt Jacob if (bus_dmamem_alloc(mpt->reply_dmat, (void **)&mpt->reply, 4589b631363SMatt Jacob BUS_DMA_NOWAIT, &mpt->reply_dmap) != 0) { 4599b631363SMatt Jacob device_printf(dev, "cannot allocate %d bytes of reply memory\n", 4609b631363SMatt Jacob PAGE_SIZE); 4619b631363SMatt Jacob return (1); 4629b631363SMatt Jacob } 4639b631363SMatt Jacob 4649b631363SMatt Jacob im.mpt = mpt; 4659b631363SMatt Jacob im.error = 0; 4669b631363SMatt Jacob 4679b631363SMatt Jacob /* Load and lock it into "bus space" */ 4689b631363SMatt Jacob bus_dmamap_load(mpt->reply_dmat, mpt->reply_dmap, mpt->reply, 4699b631363SMatt Jacob PAGE_SIZE, mpt_map_rquest, &im, 0); 4709b631363SMatt Jacob 4719b631363SMatt Jacob if (im.error) { 4729b631363SMatt Jacob device_printf(dev, 4739b631363SMatt Jacob "error %d loading dma map for DMA reply queue\n", im.error); 4749b631363SMatt Jacob return (1); 4759b631363SMatt Jacob } 4769b631363SMatt Jacob mpt->reply_phys = im.phys; 4779b631363SMatt Jacob 4789b631363SMatt Jacob /* Create a child tag for data buffers */ 4799b631363SMatt Jacob if (bus_dma_tag_create(mpt->parent_dmat, PAGE_SIZE, 4809b631363SMatt Jacob 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 4819b631363SMatt Jacob NULL, NULL, MAXBSIZE, MPT_SGL_MAX, BUS_SPACE_MAXSIZE_32BIT, 0, 4829b631363SMatt Jacob &mpt->buffer_dmat) != 0) { 4839b631363SMatt Jacob device_printf(dev, 4849b631363SMatt Jacob "cannot create a dma tag for data buffers\n"); 4859b631363SMatt Jacob return (1); 4869b631363SMatt Jacob } 4879b631363SMatt Jacob 4889b631363SMatt Jacob /* Create a child tag for request buffers */ 4899b631363SMatt Jacob if (bus_dma_tag_create(mpt->parent_dmat, PAGE_SIZE, 4909b631363SMatt Jacob 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 4919b631363SMatt Jacob NULL, NULL, MPT_REQ_MEM_SIZE, 1, BUS_SPACE_MAXSIZE_32BIT, 0, 4929b631363SMatt Jacob &mpt->request_dmat) != 0) { 4939b631363SMatt Jacob device_printf(dev, "cannot create a dma tag for requests\n"); 4949b631363SMatt Jacob return (1); 4959b631363SMatt Jacob } 4969b631363SMatt Jacob 4979b631363SMatt Jacob /* Allocate some DMA accessable memory for requests */ 4989b631363SMatt Jacob if (bus_dmamem_alloc(mpt->request_dmat, (void **)&mpt->request, 4999b631363SMatt Jacob BUS_DMA_NOWAIT, &mpt->request_dmap) != 0) { 5009b631363SMatt Jacob device_printf(dev, 5019b631363SMatt Jacob "cannot allocate %d bytes of request memory\n", 5029b631363SMatt Jacob MPT_REQ_MEM_SIZE); 5039b631363SMatt Jacob return (1); 5049b631363SMatt Jacob } 5059b631363SMatt Jacob 5069b631363SMatt Jacob im.mpt = mpt; 5079b631363SMatt Jacob im.error = 0; 5089b631363SMatt Jacob 5099b631363SMatt Jacob /* Load and lock it into "bus space" */ 5109b631363SMatt Jacob bus_dmamap_load(mpt->request_dmat, mpt->request_dmap, mpt->request, 5119b631363SMatt Jacob MPT_REQ_MEM_SIZE, mpt_map_rquest, &im, 0); 5129b631363SMatt Jacob 5139b631363SMatt Jacob if (im.error) { 5149b631363SMatt Jacob device_printf(dev, 5159b631363SMatt Jacob "error %d loading dma map for DMA request queue\n", 5169b631363SMatt Jacob im.error); 5179b631363SMatt Jacob return (1); 5189b631363SMatt Jacob } 5199b631363SMatt Jacob mpt->request_phys = im.phys; 5209b631363SMatt Jacob 5219b631363SMatt Jacob i = 0; 5229b631363SMatt Jacob pptr = mpt->request_phys; 5239b631363SMatt Jacob vptr = mpt->request; 5249b631363SMatt Jacob end = pptr + MPT_REQ_MEM_SIZE; 5259b631363SMatt Jacob while(pptr < end) { 5269b631363SMatt Jacob request_t *req = &mpt->requests[i]; 5279b631363SMatt Jacob req->index = i++; 5289b631363SMatt Jacob 5299b631363SMatt Jacob /* Store location of Request Data */ 5309b631363SMatt Jacob req->req_pbuf = pptr; 5319b631363SMatt Jacob req->req_vbuf = vptr; 5329b631363SMatt Jacob 5339b631363SMatt Jacob pptr += MPT_REQUEST_AREA; 5349b631363SMatt Jacob vptr += MPT_REQUEST_AREA; 5359b631363SMatt Jacob 5369b631363SMatt Jacob req->sense_pbuf = (pptr - MPT_SENSE_SIZE); 5379b631363SMatt Jacob req->sense_vbuf = (vptr - MPT_SENSE_SIZE); 5389b631363SMatt Jacob 5399b631363SMatt Jacob error = bus_dmamap_create(mpt->buffer_dmat, 0, &req->dmap); 5409b631363SMatt Jacob if (error) { 5419b631363SMatt Jacob device_printf(dev, 5429b631363SMatt Jacob "error %d creating per-cmd DMA maps\n", error); 5439b631363SMatt Jacob return (1); 5449b631363SMatt Jacob } 5459b631363SMatt Jacob } 5469b631363SMatt Jacob return (0); 5479b631363SMatt Jacob } 5489b631363SMatt Jacob 5499b631363SMatt Jacob 5509b631363SMatt Jacob 5519b631363SMatt Jacob /* Deallocate memory that was allocated by mpt_dma_mem_alloc 5529b631363SMatt Jacob */ 5539b631363SMatt Jacob static void 5549b631363SMatt Jacob mpt_dma_mem_free(struct mpt_softc *mpt) 5559b631363SMatt Jacob { 5569b631363SMatt Jacob int i; 5579b631363SMatt Jacob 5589b631363SMatt Jacob /* Make sure we aren't double destroying */ 5599b631363SMatt Jacob if (mpt->reply_dmat == 0) { 5609b631363SMatt Jacob if (mpt->verbose) 5619b631363SMatt Jacob device_printf(mpt->dev,"Already released dma memory\n"); 5629b631363SMatt Jacob return; 5639b631363SMatt Jacob } 5649b631363SMatt Jacob 5659b631363SMatt Jacob for (i = 0; i < MPT_MAX_REQUESTS; i++) { 5669b631363SMatt Jacob bus_dmamap_destroy(mpt->buffer_dmat, mpt->requests[i].dmap); 5679b631363SMatt Jacob } 5689b631363SMatt Jacob bus_dmamap_unload(mpt->request_dmat, mpt->request_dmap); 5699b631363SMatt Jacob bus_dmamem_free(mpt->request_dmat, mpt->request, mpt->request_dmap); 5709b631363SMatt Jacob bus_dma_tag_destroy(mpt->request_dmat); 5719b631363SMatt Jacob bus_dma_tag_destroy(mpt->buffer_dmat); 5729b631363SMatt Jacob bus_dmamap_unload(mpt->reply_dmat, mpt->reply_dmap); 5739b631363SMatt Jacob bus_dmamem_free(mpt->reply_dmat, mpt->reply, mpt->reply_dmap); 5749b631363SMatt Jacob bus_dma_tag_destroy(mpt->reply_dmat); 5759b631363SMatt Jacob bus_dma_tag_destroy(mpt->parent_dmat); 5769b631363SMatt Jacob mpt->reply_dmat = 0; 5779b631363SMatt Jacob 5789b631363SMatt Jacob } 5799b631363SMatt Jacob 5809b631363SMatt Jacob 5819b631363SMatt Jacob 5829b631363SMatt Jacob /* Reads modifiable (via PCI transactions) config registers */ 5839b631363SMatt Jacob static void 5849b631363SMatt Jacob mpt_read_config_regs(struct mpt_softc *mpt) 5859b631363SMatt Jacob { 5869b631363SMatt Jacob mpt->pci_cfg.Command = pci_read_config(mpt->dev, PCIR_COMMAND, 2); 5879b631363SMatt Jacob mpt->pci_cfg.LatencyTimer_LineSize = 5889b631363SMatt Jacob pci_read_config(mpt->dev, PCIR_CACHELNSZ, 2); 5899b631363SMatt Jacob mpt->pci_cfg.IO_BAR = pci_read_config(mpt->dev, PCIR_MAPS, 4); 5909b631363SMatt Jacob mpt->pci_cfg.Mem0_BAR[0] = pci_read_config(mpt->dev, PCIR_MAPS+0x4, 4); 5919b631363SMatt Jacob mpt->pci_cfg.Mem0_BAR[1] = pci_read_config(mpt->dev, PCIR_MAPS+0x8, 4); 5929b631363SMatt Jacob mpt->pci_cfg.Mem1_BAR[0] = pci_read_config(mpt->dev, PCIR_MAPS+0xC, 4); 5939b631363SMatt Jacob mpt->pci_cfg.Mem1_BAR[1] = pci_read_config(mpt->dev, PCIR_MAPS+0x10, 4); 5949b631363SMatt Jacob mpt->pci_cfg.ROM_BAR = pci_read_config(mpt->dev, PCIR_BIOS, 4); 5959b631363SMatt Jacob mpt->pci_cfg.IntLine = pci_read_config(mpt->dev, PCIR_INTLINE, 1); 5969b631363SMatt Jacob mpt->pci_cfg.PMCSR = pci_read_config(mpt->dev, 0x44, 4); 5979b631363SMatt Jacob } 5989b631363SMatt Jacob 5999b631363SMatt Jacob /* Sets modifiable config registers */ 6009b631363SMatt Jacob void 6019b631363SMatt Jacob mpt_set_config_regs(struct mpt_softc *mpt) 6029b631363SMatt Jacob { 6039b631363SMatt Jacob u_int32_t val; 6049b631363SMatt Jacob 6059b631363SMatt Jacob #define MPT_CHECK(reg, offset, size) \ 6069b631363SMatt Jacob val = pci_read_config(mpt->dev, offset, size); \ 6079b631363SMatt Jacob if (mpt->pci_cfg.reg != val) { \ 6089b631363SMatt Jacob device_printf(mpt->dev, \ 6099b631363SMatt Jacob "Restoring " #reg " to 0x%X from 0x%X\n", \ 6109b631363SMatt Jacob mpt->pci_cfg.reg, val); \ 6119b631363SMatt Jacob } 6129b631363SMatt Jacob 6139b631363SMatt Jacob if (mpt->verbose) { 6149b631363SMatt Jacob MPT_CHECK(Command, PCIR_COMMAND, 2); 6159b631363SMatt Jacob MPT_CHECK(LatencyTimer_LineSize, PCIR_CACHELNSZ, 2); 6169b631363SMatt Jacob MPT_CHECK(IO_BAR, PCIR_MAPS, 4); 6179b631363SMatt Jacob MPT_CHECK(Mem0_BAR[0], PCIR_MAPS+0x4, 4); 6189b631363SMatt Jacob MPT_CHECK(Mem0_BAR[1], PCIR_MAPS+0x8, 4); 6199b631363SMatt Jacob MPT_CHECK(Mem1_BAR[0], PCIR_MAPS+0xC, 4); 6209b631363SMatt Jacob MPT_CHECK(Mem1_BAR[1], PCIR_MAPS+0x10, 4); 6219b631363SMatt Jacob MPT_CHECK(ROM_BAR, PCIR_BIOS, 4); 6229b631363SMatt Jacob MPT_CHECK(IntLine, PCIR_INTLINE, 1); 6239b631363SMatt Jacob MPT_CHECK(PMCSR, 0x44, 4); 6249b631363SMatt Jacob } 6259b631363SMatt Jacob #undef MPT_CHECK 6269b631363SMatt Jacob 6279b631363SMatt Jacob pci_write_config(mpt->dev, PCIR_COMMAND, mpt->pci_cfg.Command, 2); 6289b631363SMatt Jacob pci_write_config(mpt->dev, PCIR_CACHELNSZ, 6299b631363SMatt Jacob mpt->pci_cfg.LatencyTimer_LineSize, 2); 6309b631363SMatt Jacob pci_write_config(mpt->dev, PCIR_MAPS, mpt->pci_cfg.IO_BAR, 4); 6319b631363SMatt Jacob pci_write_config(mpt->dev, PCIR_MAPS+0x4, mpt->pci_cfg.Mem0_BAR[0], 4); 6329b631363SMatt Jacob pci_write_config(mpt->dev, PCIR_MAPS+0x8, mpt->pci_cfg.Mem0_BAR[1], 4); 6339b631363SMatt Jacob pci_write_config(mpt->dev, PCIR_MAPS+0xC, mpt->pci_cfg.Mem1_BAR[0], 4); 6349b631363SMatt Jacob pci_write_config(mpt->dev, PCIR_MAPS+0x10, mpt->pci_cfg.Mem1_BAR[1], 4); 6359b631363SMatt Jacob pci_write_config(mpt->dev, PCIR_BIOS, mpt->pci_cfg.ROM_BAR, 4); 6369b631363SMatt Jacob pci_write_config(mpt->dev, PCIR_INTLINE, mpt->pci_cfg.IntLine, 1); 6379b631363SMatt Jacob pci_write_config(mpt->dev, 0x44, mpt->pci_cfg.PMCSR, 4); 6389b631363SMatt Jacob } 639