xref: /freebsd/sys/dev/aacraid/aacraid_pci.c (revision f05cddf9)
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2001 Scott Long
4  * Copyright (c) 2000 BSDi
5  * Copyright (c) 2001-2010 Adaptec, Inc.
6  * Copyright (c) 2010-2012 PMC-Sierra, Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 /*
35  * PCI bus interface and resource allocation.
36  */
37 
38 #include "opt_aacraid.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44 
45 #include <sys/bio.h>
46 #include <sys/bus.h>
47 #include <sys/conf.h>
48 #include <sys/disk.h>
49 
50 #include <machine/bus.h>
51 #include <machine/resource.h>
52 #include <sys/rman.h>
53 
54 #include <dev/pci/pcireg.h>
55 #include <dev/pci/pcivar.h>
56 
57 #include <dev/aacraid/aacraid_reg.h>
58 #include <sys/aac_ioctl.h>
59 #include <dev/aacraid/aacraid_debug.h>
60 #include <dev/aacraid/aacraid_var.h>
61 
62 static int	aacraid_pci_probe(device_t dev);
63 static int	aacraid_pci_attach(device_t dev);
64 
65 static device_method_t aacraid_methods[] = {
66 	/* Device interface */
67 	DEVMETHOD(device_probe,		aacraid_pci_probe),
68 	DEVMETHOD(device_attach,	aacraid_pci_attach),
69 	DEVMETHOD(device_detach,	aacraid_detach),
70 	DEVMETHOD(device_suspend,	aacraid_suspend),
71 	DEVMETHOD(device_resume,	aacraid_resume),
72 
73 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
74 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
75 	{ 0, 0 }
76 };
77 
78 static driver_t aacraid_pci_driver = {
79 	"aacraid",
80 	aacraid_methods,
81 	sizeof(struct aac_softc)
82 };
83 
84 static devclass_t	aacraid_devclass;
85 
86 DRIVER_MODULE(aacraid, pci, aacraid_pci_driver, aacraid_devclass, 0, 0);
87 MODULE_DEPEND(aacraid, pci, 1, 1, 1);
88 
89 struct aac_ident
90 {
91 	u_int16_t		vendor;
92 	u_int16_t		device;
93 	u_int16_t		subvendor;
94 	u_int16_t		subdevice;
95 	int			hwif;
96 	int			quirks;
97 	char			*desc;
98 } aacraid_family_identifiers[] = {
99 	{0x9005, 0x028b, 0, 0, AAC_HWIF_SRC, 0,
100 	 "Adaptec RAID Controller"},
101 	{0x9005, 0x028c, 0, 0, AAC_HWIF_SRCV, 0,
102 	 "Adaptec RAID Controller"},
103 	{0x9005, 0x028d, 0, 0, AAC_HWIF_SRCV, 0,
104 	 "Adaptec RAID Controller"},
105 	{0x9005, 0x028f, 0, 0, AAC_HWIF_SRCV, 0,
106 	 "Adaptec RAID Controller"},
107 	{0, 0, 0, 0, 0, 0, 0}
108 };
109 
110 static struct aac_ident *
111 aac_find_ident(device_t dev)
112 {
113 	struct aac_ident *m;
114 	u_int16_t vendid, devid, sub_vendid, sub_devid;
115 
116 	vendid = pci_get_vendor(dev);
117 	devid = pci_get_device(dev);
118 	sub_vendid = pci_get_subvendor(dev);
119 	sub_devid = pci_get_subdevice(dev);
120 
121 	for (m = aacraid_family_identifiers; m->vendor != 0; m++) {
122 		if ((m->vendor == vendid) && (m->device == devid))
123 			return (m);
124 	}
125 
126 	return (NULL);
127 }
128 
129 /*
130  * Determine whether this is one of our supported adapters.
131  */
132 static int
133 aacraid_pci_probe(device_t dev)
134 {
135 	struct aac_ident *id;
136 
137 	fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
138 
139 	if ((id = aac_find_ident(dev)) != NULL) {
140 		device_set_desc(dev, id->desc);
141 		return(BUS_PROBE_DEFAULT);
142 	}
143 	return(ENXIO);
144 }
145 
146 /*
147  * Allocate resources for our device, set up the bus interface.
148  */
149 static int
150 aacraid_pci_attach(device_t dev)
151 {
152 	struct aac_softc *sc;
153 	struct aac_ident *id;
154 	int error;
155 	u_int32_t command;
156 
157 	fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
158 
159 	/*
160 	 * Initialise softc.
161 	 */
162 	sc = device_get_softc(dev);
163 	bzero(sc, sizeof(*sc));
164 	sc->aac_dev = dev;
165 
166 	/* assume failure is 'not configured' */
167 	error = ENXIO;
168 
169 	/*
170 	 * Verify that the adapter is correctly set up in PCI space.
171 	 */
172 	command = pci_read_config(sc->aac_dev, PCIR_COMMAND, 2);
173 	command |= PCIM_CMD_BUSMASTEREN;
174 	pci_write_config(dev, PCIR_COMMAND, command, 2);
175 	command = pci_read_config(sc->aac_dev, PCIR_COMMAND, 2);
176 	if (!(command & PCIM_CMD_BUSMASTEREN)) {
177 		device_printf(sc->aac_dev, "can't enable bus-master feature\n");
178 		goto out;
179 	}
180 	if ((command & PCIM_CMD_MEMEN) == 0) {
181 		device_printf(sc->aac_dev, "memory window not available\n");
182 		goto out;
183 	}
184 
185 	/*
186 	 * Detect the hardware interface version, set up the bus interface
187 	 * indirection.
188 	 */
189 	id = aac_find_ident(dev);
190 	sc->aac_hwif = id->hwif;
191 	switch(sc->aac_hwif) {
192 	case AAC_HWIF_SRC:
193 		fwprintf(sc, HBA_FLAGS_DBG_INIT_B, "set hardware up for PMC SRC");
194 		sc->aac_if = aacraid_src_interface;
195 		break;
196 	case AAC_HWIF_SRCV:
197 		fwprintf(sc, HBA_FLAGS_DBG_INIT_B, "set hardware up for PMC SRCv");
198 		sc->aac_if = aacraid_srcv_interface;
199 		break;
200 	default:
201 		sc->aac_hwif = AAC_HWIF_UNKNOWN;
202 		device_printf(sc->aac_dev, "unknown hardware type\n");
203 		error = ENXIO;
204 		goto out;
205 	}
206 
207 	/* assume failure is 'out of memory' */
208 	error = ENOMEM;
209 
210 	/*
211 	 * Allocate the PCI register window.
212 	 */
213 	sc->aac_regs_rid0 = PCIR_BAR(0);
214 	if ((sc->aac_regs_res0 = bus_alloc_resource_any(sc->aac_dev,
215 	    SYS_RES_MEMORY, &sc->aac_regs_rid0, RF_ACTIVE)) == NULL) {
216 		device_printf(sc->aac_dev,
217 		    "couldn't allocate register window 0\n");
218 		goto out;
219 	}
220 	sc->aac_btag0 = rman_get_bustag(sc->aac_regs_res0);
221 	sc->aac_bhandle0 = rman_get_bushandle(sc->aac_regs_res0);
222 
223 	sc->aac_regs_rid1 = PCIR_BAR(2);
224 	if ((sc->aac_regs_res1 = bus_alloc_resource_any(sc->aac_dev,
225 	    SYS_RES_MEMORY, &sc->aac_regs_rid1, RF_ACTIVE)) == NULL) {
226 		device_printf(sc->aac_dev,
227 		    "couldn't allocate register window 1\n");
228 		goto out;
229 	}
230 	sc->aac_btag1 = rman_get_bustag(sc->aac_regs_res1);
231 	sc->aac_bhandle1 = rman_get_bushandle(sc->aac_regs_res1);
232 
233 	/*
234 	 * Allocate the parent bus DMA tag appropriate for our PCI interface.
235 	 *
236 	 * Note that some of these controllers are 64-bit capable.
237 	 */
238 	if (bus_dma_tag_create(bus_get_dma_tag(dev), 	/* parent */
239 			       PAGE_SIZE, 0,		/* algnmnt, boundary */
240 			       BUS_SPACE_MAXADDR,	/* lowaddr */
241 			       BUS_SPACE_MAXADDR, 	/* highaddr */
242 			       NULL, NULL, 		/* filter, filterarg */
243 			       BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
244 			       BUS_SPACE_UNRESTRICTED,	/* nsegments */
245 			       BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
246 			       0,			/* flags */
247 			       NULL, NULL,		/* No locking needed */
248 			       &sc->aac_parent_dmat)) {
249 		device_printf(sc->aac_dev, "can't allocate parent DMA tag\n");
250 		goto out;
251 	}
252 
253 	/* Set up quirks */
254 	sc->flags = id->quirks;
255 
256 	/*
257 	 * Do bus-independent initialisation.
258 	 */
259 	error = aacraid_attach(sc);
260 
261 out:
262 	if (error)
263 		aacraid_free(sc);
264 	return(error);
265 }
266