dpt_pci.c revision 112780
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 112780 2003-03-29 08:30:45Z mdodd $
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	int		s;
7859078Smdodd	int		error = 0;
7932801Sjulian
8059078Smdodd	u_int32_t	command;
8132801Sjulian
82112780Smdodd	dpt = device_get_softc(dev);
83112780Smdodd
8459078Smdodd	command = pci_read_config(dev, PCIR_COMMAND, /*bytes*/1);
8532801Sjulian
8659078Smdodd#ifdef DPT_ALLOW_MMIO
8759078Smdodd	if ((command & PCIM_CMD_MEMEN) != 0) {
88112780Smdodd		dpt->io_rid = DPT_PCI_MEMADDR;
89112780Smdodd		dpt->io_type = SYS_RES_MEMORY;
90112780Smdodd		dpt->io_res = bus_alloc_resource(dev, dpt->io_type,
91112780Smdodd						 &dpt->io_rid,
92112780Smdodd						 0, ~0, 1, RF_ACTIVE);
9359078Smdodd	}
9432801Sjulian#endif
95112780Smdodd	if (dpt->io_res == NULL && (command &  PCIM_CMD_PORTEN) != 0) {
96112780Smdodd		dpt->io_rid = DPT_PCI_IOADDR;
97112780Smdodd		dpt->io_type = SYS_RES_IOPORT;
98112780Smdodd		dpt->io_res = bus_alloc_resource(dev, dpt->io_type,
99112780Smdodd						 &dpt->io_rid,
100112780Smdodd						 0, ~0, 1, 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;
111112780Smdodd	dpt->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &dpt->irq_rid,
112112780Smdodd					  0, ~0, 1, 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 */
13659078Smdodd	if (bus_dma_tag_create(	/* parent    */	NULL,
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,
14759078Smdodd				&dpt->parent_dmat) != 0) {
14839234Sgibbs		dpt_free(dpt);
14959078Smdodd		error = ENXIO;
15059078Smdodd		goto bad;
15139234Sgibbs	}
15232801Sjulian
15359078Smdodd	s = splcam();
15432801Sjulian
15539234Sgibbs	if (dpt_init(dpt) != 0) {
15639234Sgibbs		dpt_free(dpt);
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,
167112780Smdodd			   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
178112780Smdodd	if (dpt)
179112780Smdodd		dpt_free(dpt);
180112780Smdodd
18159078Smdodd	return (error);
18232801Sjulian}
18359078Smdodd
18459078Smdoddstatic device_method_t dpt_pci_methods[] = {
18559078Smdodd	/* Device interface */
18659078Smdodd	DEVMETHOD(device_probe,         dpt_pci_probe),
18759078Smdodd	DEVMETHOD(device_attach,        dpt_pci_attach),
188112780Smdodd	DEVMETHOD(device_detach,        dpt_detach),
18959078Smdodd
19059078Smdodd	{ 0, 0 }
19159078Smdodd};
19259078Smdodd
19359078Smdoddstatic driver_t dpt_pci_driver = {
19459078Smdodd	"dpt",
19559078Smdodd	dpt_pci_methods,
19659078Smdodd	sizeof(dpt_softc_t),
19759078Smdodd};
19859078Smdodd
19959078SmdoddDRIVER_MODULE(dpt, pci, dpt_pci_driver, dpt_devclass, 0, 0);
200