dpt_pci.c revision 232854
159078Smdodd/*- 259078Smdodd * Copyright (c) 2000 Matthew N. Dodd <winter@jurai.net> 359078Smdodd * All rights reserved. 432801Sjulian * 559078Smdodd * Copyright (c) 1997 Simon Shapiro 659078Smdodd * All Rights Reserved 759078Smdodd * 832801Sjulian * Redistribution and use in source and binary forms, with or without 932801Sjulian * modification, are permitted provided that the following conditions 1032801Sjulian * are met: 1132801Sjulian * 1. Redistributions of source code must retain the above copyright 1259078Smdodd * notice, this list of conditions and the following disclaimer. 1332801Sjulian * 2. Redistributions in binary form must reproduce the above copyright 1432801Sjulian * notice, this list of conditions and the following disclaimer in the 1532801Sjulian * documentation and/or other materials provided with the distribution. 1632801Sjulian * 1732801Sjulian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1832801Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1932801Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2059078Smdodd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2159078Smdodd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2232801Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2332801Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2432801Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2532801Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2632801Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2732801Sjulian * SUCH DAMAGE. 2832801Sjulian */ 2932801Sjulian 30119418Sobrien#include <sys/cdefs.h> 31119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/dpt/dpt_pci.c 232854 2012-03-12 08:03:51Z scottl $"); 32119418Sobrien 3332801Sjulian#include <sys/param.h> 3432801Sjulian#include <sys/systm.h> 3532801Sjulian#include <sys/kernel.h> 3659078Smdodd#include <sys/module.h> 37117126Sscottl#include <sys/lock.h> 38117126Sscottl#include <sys/mutex.h> 3959078Smdodd#include <sys/bus.h> 4032801Sjulian 4139234Sgibbs#include <machine/bus.h> 4259078Smdodd#include <machine/resource.h> 4359078Smdodd#include <sys/rman.h> 4439234Sgibbs 45119277Simp#include <dev/pci/pcireg.h> 46119277Simp#include <dev/pci/pcivar.h> 4759078Smdodd 4839234Sgibbs#include <cam/scsi/scsi_all.h> 4939234Sgibbs 5039234Sgibbs#include <dev/dpt/dpt.h> 5132801Sjulian 5259078Smdodd#define DPT_VENDOR_ID 0x1044 5359078Smdodd#define DPT_DEVICE_ID 0xa400 5432801Sjulian 55119690Sjhb#define DPT_PCI_IOADDR PCIR_BAR(0) /* I/O Address */ 56119690Sjhb#define DPT_PCI_MEMADDR PCIR_BAR(1) /* Mem I/O Address */ 5732801Sjulian 5859078Smdodd#define ISA_PRIMARY_WD_ADDRESS 0x1f8 5932801Sjulian 6059078Smdoddstatic int dpt_pci_probe (device_t); 6159078Smdoddstatic int dpt_pci_attach (device_t); 6232801Sjulian 6359078Smdoddstatic int 6459078Smdodddpt_pci_probe (device_t dev) 6559078Smdodd{ 6659078Smdodd if ((pci_get_vendor(dev) == DPT_VENDOR_ID) && 6759078Smdodd (pci_get_device(dev) == DPT_DEVICE_ID)) { 6859078Smdodd device_set_desc(dev, "DPT Caching SCSI RAID Controller"); 69143161Simp return (BUS_PROBE_DEFAULT); 7059078Smdodd } 7159078Smdodd return (ENXIO); 7259078Smdodd} 7332801Sjulian 7459078Smdoddstatic int 7559078Smdodddpt_pci_attach (device_t dev) 7632801Sjulian{ 7759078Smdodd dpt_softc_t * dpt; 7859078Smdodd int s; 7959078Smdodd int error = 0; 8032801Sjulian 8159078Smdodd u_int32_t command; 8232801Sjulian 83112780Smdodd dpt = device_get_softc(dev); 84170872Sscottl dpt->dev = dev; 85112780Smdodd 8659078Smdodd command = pci_read_config(dev, PCIR_COMMAND, /*bytes*/1); 8732801Sjulian 8859078Smdodd#ifdef DPT_ALLOW_MMIO 8959078Smdodd if ((command & PCIM_CMD_MEMEN) != 0) { 90112780Smdodd dpt->io_rid = DPT_PCI_MEMADDR; 91112780Smdodd dpt->io_type = SYS_RES_MEMORY; 92127135Snjl dpt->io_res = bus_alloc_resource_any(dev, dpt->io_type, 93127135Snjl &dpt->io_rid, RF_ACTIVE); 9459078Smdodd } 9532801Sjulian#endif 96112780Smdodd if (dpt->io_res == NULL && (command & PCIM_CMD_PORTEN) != 0) { 97112780Smdodd dpt->io_rid = DPT_PCI_IOADDR; 98112780Smdodd dpt->io_type = SYS_RES_IOPORT; 99127135Snjl dpt->io_res = bus_alloc_resource_any(dev, dpt->io_type, 100127135Snjl &dpt->io_rid, RF_ACTIVE); 10159078Smdodd } 10232801Sjulian 103112780Smdodd if (dpt->io_res == NULL) { 10459078Smdodd device_printf(dev, "can't allocate register resources\n"); 10559078Smdodd error = ENOMEM; 10659078Smdodd goto bad; 10759078Smdodd } 108112780Smdodd dpt->io_offset = 0x10; 10932801Sjulian 110112780Smdodd dpt->irq_rid = 0; 111127135Snjl dpt->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &dpt->irq_rid, 112127135Snjl RF_ACTIVE | RF_SHAREABLE); 113112780Smdodd if (dpt->irq_res == NULL) { 11459078Smdodd device_printf(dev, "No irq?!\n"); 11559078Smdodd error = ENOMEM; 11659078Smdodd goto bad; 11759078Smdodd } 11832801Sjulian 11959078Smdodd /* Ensure busmastering is enabled */ 12059078Smdodd command |= PCIM_CMD_BUSMASTEREN; 12159078Smdodd pci_write_config(dev, PCIR_COMMAND, command, /*bytes*/1); 12232801Sjulian 123112780Smdodd if (rman_get_start(dpt->io_res) == (ISA_PRIMARY_WD_ADDRESS - 0x10)) { 12432801Sjulian#ifdef DPT_DEBUG_WARN 12559078Smdodd device_printf(dev, "Mapped as an IDE controller. " 12659078Smdodd "Disabling SCSI setup\n"); 12732801Sjulian#endif 12859078Smdodd error = ENXIO; 12959078Smdodd goto bad; 13039234Sgibbs } 13132801Sjulian 132112780Smdodd dpt_alloc(dev); 13332801Sjulian 13439234Sgibbs /* Allocate a dmatag representing the capabilities of this attachment */ 13539234Sgibbs /* XXX Should be a child of the PCI bus dma tag */ 136232854Sscottl if (bus_dma_tag_create( /* PCI parent */ bus_get_dma_tag(dev), 13759078Smdodd /* alignemnt */ 1, 13859078Smdodd /* boundary */ 0, 13959078Smdodd /* lowaddr */ BUS_SPACE_MAXADDR_32BIT, 14059078Smdodd /* highaddr */ BUS_SPACE_MAXADDR, 14159078Smdodd /* filter */ NULL, 14259078Smdodd /* filterarg */ NULL, 14359078Smdodd /* maxsize */ BUS_SPACE_MAXSIZE_32BIT, 144104710Speter /* nsegments */ ~0, 14559078Smdodd /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT, 14659078Smdodd /* flags */ 0, 147117126Sscottl /* lockfunc */ busdma_lock_mutex, 148117126Sscottl /* lockarg */ &Giant, 14959078Smdodd &dpt->parent_dmat) != 0) { 15059078Smdodd error = ENXIO; 15159078Smdodd goto bad; 15239234Sgibbs } 15332801Sjulian 15459078Smdodd s = splcam(); 15532801Sjulian 15639234Sgibbs if (dpt_init(dpt) != 0) { 15759078Smdodd error = ENXIO; 15859078Smdodd goto bad; 15939234Sgibbs } 16032801Sjulian 16139234Sgibbs /* Register with the XPT */ 16239234Sgibbs dpt_attach(dpt); 16359078Smdodd 16439234Sgibbs splx(s); 16559078Smdodd 166112780Smdodd if (bus_setup_intr(dev, dpt->irq_res, INTR_TYPE_CAM | INTR_ENTROPY, 167166901Spiso NULL, dpt_intr, dpt, &dpt->ih)) { 16859078Smdodd device_printf(dev, "Unable to register interrupt handler\n"); 16959078Smdodd error = ENXIO; 17059078Smdodd goto bad; 17159078Smdodd } 17259078Smdodd 17359078Smdodd return (error); 17459078Smdodd 17559078Smdoddbad: 176112780Smdodd dpt_release_resources(dev); 17759078Smdodd 178142356Ssam dpt_free(dpt); 179112780Smdodd 18059078Smdodd return (error); 18132801Sjulian} 18259078Smdodd 18359078Smdoddstatic device_method_t dpt_pci_methods[] = { 18459078Smdodd /* Device interface */ 18559078Smdodd DEVMETHOD(device_probe, dpt_pci_probe), 18659078Smdodd DEVMETHOD(device_attach, dpt_pci_attach), 187112780Smdodd DEVMETHOD(device_detach, dpt_detach), 18859078Smdodd 18959078Smdodd { 0, 0 } 19059078Smdodd}; 19159078Smdodd 19259078Smdoddstatic driver_t dpt_pci_driver = { 19359078Smdodd "dpt", 19459078Smdodd dpt_pci_methods, 19559078Smdodd sizeof(dpt_softc_t), 19659078Smdodd}; 19759078Smdodd 19859078SmdoddDRIVER_MODULE(dpt, pci, dpt_pci_driver, dpt_devclass, 0, 0); 199165102SmjacobMODULE_DEPEND(dpt, pci, 1, 1, 1); 200165102SmjacobMODULE_DEPEND(dpt, cam, 1, 1, 1); 201