dpt_pci.c revision 104710
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 * 2959078Smdodd * $FreeBSD: head/sys/dev/dpt/dpt_pci.c 104710 2002-10-09 08:54:32Z peter $ 3032801Sjulian */ 3132801Sjulian 3232801Sjulian#include <sys/param.h> 3332801Sjulian#include <sys/systm.h> 3432801Sjulian#include <sys/kernel.h> 3559078Smdodd#include <sys/module.h> 3659078Smdodd#include <sys/bus.h> 3732801Sjulian 3839234Sgibbs#include <machine/bus_memio.h> 3939234Sgibbs#include <machine/bus_pio.h> 4039234Sgibbs#include <machine/bus.h> 4159078Smdodd#include <machine/resource.h> 4259078Smdodd#include <sys/rman.h> 4339234Sgibbs 4459078Smdodd#include <pci/pcireg.h> 4559078Smdodd#include <pci/pcivar.h> 4659078Smdodd 4739234Sgibbs#include <cam/scsi/scsi_all.h> 4839234Sgibbs 4939234Sgibbs#include <dev/dpt/dpt.h> 5032801Sjulian 5159078Smdodd#define DPT_VENDOR_ID 0x1044 5259078Smdodd#define DPT_DEVICE_ID 0xa400 5332801Sjulian 5459078Smdodd#define DPT_PCI_IOADDR PCIR_MAPS /* I/O Address */ 5559078Smdodd#define DPT_PCI_MEMADDR (PCIR_MAPS + 4) /* Mem I/O Address */ 5632801Sjulian 5759078Smdodd#define ISA_PRIMARY_WD_ADDRESS 0x1f8 5832801Sjulian 5959078Smdoddstatic int dpt_pci_probe (device_t); 6059078Smdoddstatic int dpt_pci_attach (device_t); 6132801Sjulian 6259078Smdoddstatic int 6359078Smdodddpt_pci_probe (device_t dev) 6459078Smdodd{ 6559078Smdodd if ((pci_get_vendor(dev) == DPT_VENDOR_ID) && 6659078Smdodd (pci_get_device(dev) == DPT_DEVICE_ID)) { 6759078Smdodd device_set_desc(dev, "DPT Caching SCSI RAID Controller"); 6859078Smdodd return (0); 6959078Smdodd } 7059078Smdodd return (ENXIO); 7159078Smdodd} 7232801Sjulian 7359078Smdoddstatic int 7459078Smdodddpt_pci_attach (device_t dev) 7532801Sjulian{ 7659078Smdodd dpt_softc_t * dpt; 7759078Smdodd struct resource *io = 0; 7859078Smdodd struct resource *irq = 0; 7959078Smdodd int s; 8059078Smdodd int rid; 8159078Smdodd void * ih; 8259078Smdodd int error = 0; 8332801Sjulian 8459078Smdodd int iotype = 0; 8559078Smdodd u_int32_t command; 8632801Sjulian 8759078Smdodd command = pci_read_config(dev, PCIR_COMMAND, /*bytes*/1); 8832801Sjulian 8959078Smdodd#ifdef DPT_ALLOW_MMIO 9059078Smdodd if ((command & PCIM_CMD_MEMEN) != 0) { 9159078Smdodd rid = DPT_PCI_MEMADDR; 9259078Smdodd iotype = SYS_RES_MEMORY; 9359078Smdodd io = bus_alloc_resource(dev, iotype, &rid, 0, ~0, 1, RF_ACTIVE); 9459078Smdodd } 9532801Sjulian#endif 9661037Speter if (io == NULL && (command & PCIM_CMD_PORTEN) != 0) { 9759078Smdodd rid = DPT_PCI_IOADDR; 9859078Smdodd iotype = SYS_RES_IOPORT; 9959078Smdodd io = bus_alloc_resource(dev, iotype, &rid, 0, ~0, 1, RF_ACTIVE); 10059078Smdodd } 10132801Sjulian 10259078Smdodd if (io == NULL) { 10359078Smdodd device_printf(dev, "can't allocate register resources\n"); 10459078Smdodd error = ENOMEM; 10559078Smdodd goto bad; 10659078Smdodd } 10732801Sjulian 10859078Smdodd rid = 0; 10965120Speter irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 11065120Speter RF_ACTIVE | RF_SHAREABLE); 11159078Smdodd if (!irq) { 11259078Smdodd device_printf(dev, "No irq?!\n"); 11359078Smdodd error = ENOMEM; 11459078Smdodd goto bad; 11559078Smdodd } 11632801Sjulian 11759078Smdodd /* Ensure busmastering is enabled */ 11859078Smdodd command |= PCIM_CMD_BUSMASTEREN; 11959078Smdodd pci_write_config(dev, PCIR_COMMAND, command, /*bytes*/1); 12032801Sjulian 12159078Smdodd if (rman_get_start(io) == (ISA_PRIMARY_WD_ADDRESS - 0x10)) { 12232801Sjulian#ifdef DPT_DEBUG_WARN 12359078Smdodd device_printf(dev, "Mapped as an IDE controller. " 12459078Smdodd "Disabling SCSI setup\n"); 12532801Sjulian#endif 12659078Smdodd error = ENXIO; 12759078Smdodd goto bad; 12839234Sgibbs } 12932801Sjulian 13059079Smdodd /* Device registers are offset 0x10 into the register window. FEH */ 13159079Smdodd dpt = dpt_alloc(dev, rman_get_bustag(io), rman_get_bushandle(io) + 0x10); 13259078Smdodd if (dpt == NULL) { 13359078Smdodd error = ENXIO; 13459078Smdodd goto bad; 13539234Sgibbs } 13632801Sjulian 13739234Sgibbs /* Allocate a dmatag representing the capabilities of this attachment */ 13839234Sgibbs /* XXX Should be a child of the PCI bus dma tag */ 13959078Smdodd if (bus_dma_tag_create( /* parent */ NULL, 14059078Smdodd /* alignemnt */ 1, 14159078Smdodd /* boundary */ 0, 14259078Smdodd /* lowaddr */ BUS_SPACE_MAXADDR_32BIT, 14359078Smdodd /* highaddr */ BUS_SPACE_MAXADDR, 14459078Smdodd /* filter */ NULL, 14559078Smdodd /* filterarg */ NULL, 14659078Smdodd /* maxsize */ BUS_SPACE_MAXSIZE_32BIT, 147104710Speter /* nsegments */ ~0, 14859078Smdodd /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT, 14959078Smdodd /* flags */ 0, 15059078Smdodd &dpt->parent_dmat) != 0) { 15139234Sgibbs dpt_free(dpt); 15259078Smdodd error = ENXIO; 15359078Smdodd goto bad; 15439234Sgibbs } 15532801Sjulian 15659078Smdodd s = splcam(); 15732801Sjulian 15839234Sgibbs if (dpt_init(dpt) != 0) { 15939234Sgibbs dpt_free(dpt); 16059078Smdodd error = ENXIO; 16159078Smdodd goto bad; 16239234Sgibbs } 16332801Sjulian 16439234Sgibbs /* Register with the XPT */ 16539234Sgibbs dpt_attach(dpt); 16659078Smdodd 16739234Sgibbs splx(s); 16859078Smdodd 16973280Smarkm if (bus_setup_intr(dev, irq, INTR_TYPE_CAM | INTR_ENTROPY, dpt_intr, 17073280Smarkm dpt, &ih)) { 17159078Smdodd device_printf(dev, "Unable to register interrupt handler\n"); 17259078Smdodd error = ENXIO; 17359078Smdodd goto bad; 17459078Smdodd } 17559078Smdodd 17659078Smdodd return (error); 17759078Smdodd 17859078Smdoddbad: 17959078Smdodd if (io) 18059078Smdodd bus_release_resource(dev, iotype, 0, io); 18159078Smdodd if (irq) 18259078Smdodd bus_release_resource(dev, SYS_RES_IRQ, 0, irq); 18359078Smdodd 18459078Smdodd return (error); 18532801Sjulian} 18659078Smdodd 18759078Smdoddstatic device_method_t dpt_pci_methods[] = { 18859078Smdodd /* Device interface */ 18959078Smdodd DEVMETHOD(device_probe, dpt_pci_probe), 19059078Smdodd DEVMETHOD(device_attach, dpt_pci_attach), 19159078Smdodd 19259078Smdodd { 0, 0 } 19359078Smdodd}; 19459078Smdodd 19559078Smdoddstatic driver_t dpt_pci_driver = { 19659078Smdodd "dpt", 19759078Smdodd dpt_pci_methods, 19859078Smdodd sizeof(dpt_softc_t), 19959078Smdodd}; 20059078Smdodd 20159078Smdoddstatic devclass_t dpt_devclass; 20259078Smdodd 20359078SmdoddDRIVER_MODULE(dpt, pci, dpt_pci_driver, dpt_devclass, 0, 0); 204