dpt_pci.c revision 59078
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 59078 2000-04-07 02:50:24Z 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	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
9659078Smdodd	if (io == NULL && (command & PCI_COMMAND_IO_ENABLE) != 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;
10959078Smdodd	irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_ACTIVE);
11059078Smdodd	if (!irq) {
11159078Smdodd		device_printf(dev, "No irq?!\n");
11259078Smdodd		error = ENOMEM;
11359078Smdodd		goto bad;
11459078Smdodd	}
11532801Sjulian
11659078Smdodd	/* Ensure busmastering is enabled */
11759078Smdodd	command |= PCIM_CMD_BUSMASTEREN;
11859078Smdodd	pci_write_config(dev, PCIR_COMMAND, command, /*bytes*/1);
11932801Sjulian
12059078Smdodd	if (rman_get_start(io) == (ISA_PRIMARY_WD_ADDRESS - 0x10)) {
12132801Sjulian#ifdef DPT_DEBUG_WARN
12259078Smdodd		device_printf(dev, "Mapped as an IDE controller.  "
12359078Smdodd				   "Disabling SCSI setup\n");
12432801Sjulian#endif
12559078Smdodd		error = ENXIO;
12659078Smdodd		goto bad;
12739234Sgibbs	}
12832801Sjulian
12959078Smdodd	dpt = dpt_alloc(dev, rman_get_bustag(io), rman_get_bushandle(io));
13059078Smdodd	if (dpt == NULL) {
13159078Smdodd		error = ENXIO;
13259078Smdodd		goto bad;
13339234Sgibbs	}
13432801Sjulian
13539234Sgibbs	/* Allocate a dmatag representing the capabilities of this attachment */
13639234Sgibbs	/* XXX Should be a child of the PCI bus dma tag */
13759078Smdodd	if (bus_dma_tag_create(	/* parent    */	NULL,
13859078Smdodd				/* alignemnt */	1,
13959078Smdodd				/* boundary  */	0,
14059078Smdodd				/* lowaddr   */	BUS_SPACE_MAXADDR_32BIT,
14159078Smdodd				/* highaddr  */	BUS_SPACE_MAXADDR,
14259078Smdodd				/* filter    */	NULL,
14359078Smdodd				/* filterarg */	NULL,
14459078Smdodd				/* maxsize   */	BUS_SPACE_MAXSIZE_32BIT,
14559078Smdodd				/* nsegments */	BUS_SPACE_UNRESTRICTED,
14659078Smdodd				/* maxsegsz  */	BUS_SPACE_MAXSIZE_32BIT,
14759078Smdodd				/* flags     */	0,
14859078Smdodd				&dpt->parent_dmat) != 0) {
14939234Sgibbs		dpt_free(dpt);
15059078Smdodd		error = ENXIO;
15159078Smdodd		goto bad;
15239234Sgibbs	}
15332801Sjulian
15459078Smdodd	s = splcam();
15532801Sjulian
15639234Sgibbs	if (dpt_init(dpt) != 0) {
15739234Sgibbs		dpt_free(dpt);
15859078Smdodd		error = ENXIO;
15959078Smdodd		goto bad;
16039234Sgibbs	}
16132801Sjulian
16239234Sgibbs	/* Register with the XPT */
16339234Sgibbs	dpt_attach(dpt);
16459078Smdodd
16539234Sgibbs	splx(s);
16659078Smdodd
16759078Smdodd	if (bus_setup_intr(dev, irq, INTR_TYPE_CAM, dpt_intr, 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:
17659078Smdodd	if (io)
17759078Smdodd		bus_release_resource(dev, iotype, 0, io);
17859078Smdodd	if (irq)
17959078Smdodd		bus_release_resource(dev, SYS_RES_IRQ, 0, irq);
18059078Smdodd
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),
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
19859078Smdoddstatic devclass_t dpt_devclass;
19959078Smdodd
20059078SmdoddDRIVER_MODULE(dpt, pci, dpt_pci_driver, dpt_devclass, 0, 0);
201