dpt_pci.c revision 46024
1/*
2 *       Copyright (c) 1997 by Simon Shapiro
3 *       All Rights Reserved
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions, and the following disclaimer,
10 *    without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30
31/*
32 *  dptpci.c:  PCI Bus Attachment for DPT SCSI HBAs
33 */
34
35#ident "$Id: dpt_pci.c,v 1.11 1998/12/14 06:32:55 dillon Exp $"
36
37#include "opt_devfs.h"
38#include "opt_dpt.h"
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/malloc.h>
43#include <sys/buf.h>
44#include <sys/kernel.h>
45
46#include <pci/pcireg.h>
47#include <pci/pcivar.h>
48
49#include <machine/bus_memio.h>
50#include <machine/bus_pio.h>
51#include <machine/bus.h>
52
53#include <cam/scsi/scsi_all.h>
54
55#include <dev/dpt/dpt.h>
56#include <pci/dpt_pci.h>
57
58#define PCI_BASEADR0  PCI_MAP_REG_START      /* I/O Address */
59#define PCI_BASEADR1  PCI_MAP_REG_START + 4  /* Mem I/O Address */
60
61#define ISA_PRIMARY_WD_ADDRESS    0x1f8
62
63/* Global variables */
64
65/* Function Prototypes */
66
67static const char    *dpt_pci_probe(pcici_t tag, pcidi_t type);
68static void     dpt_pci_attach(pcici_t config_id, int unit);
69
70extern struct cdevsw dpt_cdevsw;
71
72static  struct pci_device dpt_pci_driver =
73{
74	"dpt",
75	dpt_pci_probe,
76	dpt_pci_attach,
77	&dpt_unit,
78	NULL
79};
80
81#ifdef COMPAT_PCI_DRIVER
82COMPAT_PCI_DRIVER(dpt_pci, dpt_pci_driver);
83#else
84DATA_SET(pcidevice_set, dpt_pci_driver);
85#endif /* COMPAT_PCI_DRIVER */
86
87/*
88 * Probe the PCI device.
89 * Some of this work will have to be duplicated in _attach
90 * because we do not know for sure how the two relate.
91 */
92
93static const char *
94dpt_pci_probe(pcici_t tag, pcidi_t type)
95{
96	u_int32_t  class;
97
98#ifndef PCI_COMMAND_MASTER_ENABLE
99#define PCI_COMMAND_MASTER_ENABLE 0x00000004
100#endif
101
102#ifndef PCI_SUBCLASS_MASS_STORAGE_SCSI
103#define PCI_SUBCLASS_MASS_STORAGE_SCSI 0x00000000
104#endif
105
106	class = pci_conf_read(tag, PCI_CLASS_REG);
107	if (((type & 0xffff0000) >> 16) == DPT_DEVICE_ID
108	 && (class & PCI_CLASS_MASK) == PCI_CLASS_MASS_STORAGE
109	 && (class & PCI_SUBCLASS_MASK) == PCI_SUBCLASS_MASS_STORAGE_SCSI)
110		return ("DPT Caching SCSI RAID Controller");
111	return (NULL);
112}
113
114static void
115dpt_pci_attach(pcici_t config_id, int unit)
116{
117	dpt_softc_t	   *dpt;
118	vm_offset_t	    vaddr;
119#ifdef DPT_ALLOW_MEMIO
120	vm_offset_t	    paddr;
121#endif
122	u_int16_t	    io_base;
123	bus_space_tag_t     tag;
124	bus_space_handle_t  bsh;
125	u_int32_t	    command;
126	int		    s;
127
128	vaddr = NULL;
129	command = pci_conf_read(config_id, PCI_COMMAND_STATUS_REG);
130#ifdef DPT_ALLOW_MEMIO
131	if ((command & PCI_COMMAND_MEM_ENABLE) == 0
132	 || (pci_map_mem(config_id, PCI_BASEADR1, &vaddr, &paddr)) == 0)
133#endif
134		if ((command & PCI_COMMAND_IO_ENABLE) == 0
135		 || (pci_map_port(config_id, PCI_BASEADR0, &io_base)) == 0)
136			return;
137
138	/*
139	 * If the DPT is mapped as an IDE controller,
140	 * let it be IDE controller
141	 */
142	if (io_base == ISA_PRIMARY_WD_ADDRESS - 0x10) {
143#ifdef DPT_DEBUG_WARN
144		printf("dpt%d: Mapped as an IDE controller.  "
145		       "Disabling SCSI setup\n", unit);
146#endif
147		return;
148	}
149
150	/* XXX Should be passed in by parent bus */
151	/* XXX Why isn't the 0x10 offset incorporated into the reg defs? */
152	if (vaddr != 0) {
153		tag = I386_BUS_SPACE_MEM;
154		bsh = vaddr + 0x10;
155	} else {
156		tag = I386_BUS_SPACE_IO;
157		bsh = io_base + 0x10;
158	}
159
160	if ((dpt = dpt_alloc(unit, tag, bsh)) == NULL)
161		return;  /* XXX PCI code should take return status */
162
163	/* Allocate a dmatag representing the capabilities of this attachment */
164	/* XXX Should be a child of the PCI bus dma tag */
165	if (bus_dma_tag_create(/*parent*/NULL, /*alignemnt*/0, /*boundary*/0,
166			       /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
167			       /*highaddr*/BUS_SPACE_MAXADDR,
168			       /*filter*/NULL, /*filterarg*/NULL,
169			       /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
170			       /*nsegments*/BUS_SPACE_UNRESTRICTED,
171			       /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
172			       /*flags*/0, &dpt->parent_dmat) != 0) {
173		dpt_free(dpt);
174		return;
175	}
176
177	if (pci_map_int(config_id, dpt_intr, (void *)dpt, &cam_imask) == 0) {
178		dpt_free(dpt);
179		return;
180	}
181
182	s = splcam();
183	if (dpt_init(dpt) != 0) {
184		dpt_free(dpt);
185		return;
186	}
187
188	/* Register with the XPT */
189	dpt_attach(dpt);
190	splx(s);
191}
192