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