1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
35
36 /*
37 * PCI bus interface and resource allocation.
38 */
39
40 #include "opt_aacraid.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/module.h>
46
47 #include <sys/bio.h>
48 #include <sys/bus.h>
49 #include <sys/conf.h>
50 #include <sys/disk.h>
51
52 #include <machine/bus.h>
53 #include <machine/resource.h>
54 #include <sys/rman.h>
55
56 #include <dev/pci/pcireg.h>
57 #include <dev/pci/pcivar.h>
58
59 #include <dev/aacraid/aacraid_reg.h>
60 #include <sys/aac_ioctl.h>
61 #include <dev/aacraid/aacraid_debug.h>
62 #include <dev/aacraid/aacraid_var.h>
63
64 static int aacraid_pci_probe(device_t dev);
65 static int aacraid_pci_attach(device_t dev);
66
67 static device_method_t aacraid_methods[] = {
68 /* Device interface */
69 DEVMETHOD(device_probe, aacraid_pci_probe),
70 DEVMETHOD(device_attach, aacraid_pci_attach),
71 DEVMETHOD(device_detach, aacraid_detach),
72 DEVMETHOD(device_suspend, aacraid_suspend),
73 DEVMETHOD(device_resume, aacraid_resume),
74
75 DEVMETHOD(bus_print_child, bus_generic_print_child),
76 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
77 { 0, 0 }
78 };
79
80 static driver_t aacraid_pci_driver = {
81 "aacraid",
82 aacraid_methods,
83 sizeof(struct aac_softc)
84 };
85
86 static devclass_t aacraid_devclass;
87
88 struct aac_ident
89 {
90 u_int16_t vendor;
91 u_int16_t device;
92 u_int16_t subvendor;
93 u_int16_t subdevice;
94 int hwif;
95 int quirks;
96 char *desc;
97 } aacraid_family_identifiers[] = {
98 {0x9005, 0x028b, 0, 0, AAC_HWIF_SRC, 0,
99 "Adaptec RAID Controller"},
100 {0x9005, 0x028c, 0, 0, AAC_HWIF_SRCV, 0,
101 "Adaptec RAID Controller"},
102 {0x9005, 0x028d, 0, 0, AAC_HWIF_SRCV, 0,
103 "Adaptec RAID Controller"},
104 {0, 0, 0, 0, 0, 0, 0}
105 };
106
107 DRIVER_MODULE(aacraid, pci, aacraid_pci_driver, aacraid_devclass, 0, 0);
108 MODULE_PNP_INFO("U16:vendor;U16:device", pci, aacraid,
109 aacraid_family_identifiers,
110 nitems(aacraid_family_identifiers) - 1);
111 MODULE_DEPEND(aacraid, pci, 1, 1, 1);
112
113 static struct aac_ident *
aac_find_ident(device_t dev)114 aac_find_ident(device_t dev)
115 {
116 struct aac_ident *m;
117 u_int16_t vendid, devid;
118
119 vendid = pci_get_vendor(dev);
120 devid = pci_get_device(dev);
121
122 for (m = aacraid_family_identifiers; m->vendor != 0; m++) {
123 if ((m->vendor == vendid) && (m->device == devid))
124 return (m);
125 }
126
127 return (NULL);
128 }
129
130 /*
131 * Determine whether this is one of our supported adapters.
132 */
133 static int
aacraid_pci_probe(device_t dev)134 aacraid_pci_probe(device_t dev)
135 {
136 struct aac_ident *id;
137
138 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
139
140 if ((id = aac_find_ident(dev)) != NULL) {
141 device_set_desc(dev, id->desc);
142 return(BUS_PROBE_DEFAULT);
143 }
144 return(ENXIO);
145 }
146
147 /*
148 * Allocate resources for our device, set up the bus interface.
149 */
150 static int
aacraid_pci_attach(device_t dev)151 aacraid_pci_attach(device_t dev)
152 {
153 struct aac_softc *sc;
154 struct aac_ident *id;
155 int error;
156 u_int32_t command;
157
158 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
159
160 /*
161 * Initialise softc.
162 */
163 sc = device_get_softc(dev);
164 bzero(sc, sizeof(*sc));
165 sc->aac_dev = dev;
166
167 /* assume failure is 'not configured' */
168 error = ENXIO;
169
170 /*
171 * Verify that the adapter is correctly set up in PCI space.
172 */
173 pci_enable_busmaster(dev);
174 command = pci_read_config(sc->aac_dev, PCIR_COMMAND, 2);
175 if (!(command & PCIM_CMD_BUSMASTEREN)) {
176 device_printf(sc->aac_dev, "can't enable bus-master feature\n");
177 goto out;
178 }
179
180 /*
181 * Detect the hardware interface version, set up the bus interface
182 * indirection.
183 */
184 id = aac_find_ident(dev);
185 sc->aac_hwif = id->hwif;
186 switch(sc->aac_hwif) {
187 case AAC_HWIF_SRC:
188 fwprintf(sc, HBA_FLAGS_DBG_INIT_B, "set hardware up for PMC SRC");
189 sc->aac_if = aacraid_src_interface;
190 break;
191 case AAC_HWIF_SRCV:
192 fwprintf(sc, HBA_FLAGS_DBG_INIT_B, "set hardware up for PMC SRCv");
193 sc->aac_if = aacraid_srcv_interface;
194 break;
195 default:
196 sc->aac_hwif = AAC_HWIF_UNKNOWN;
197 device_printf(sc->aac_dev, "unknown hardware type\n");
198 error = ENXIO;
199 goto out;
200 }
201
202 /* assume failure is 'out of memory' */
203 error = ENOMEM;
204
205 /*
206 * Allocate the PCI register window.
207 */
208 sc->aac_regs_rid0 = PCIR_BAR(0);
209 if ((sc->aac_regs_res0 = bus_alloc_resource_any(sc->aac_dev,
210 SYS_RES_MEMORY, &sc->aac_regs_rid0, RF_ACTIVE)) == NULL) {
211 device_printf(sc->aac_dev,
212 "couldn't allocate register window 0\n");
213 goto out;
214 }
215 sc->aac_btag0 = rman_get_bustag(sc->aac_regs_res0);
216 sc->aac_bhandle0 = rman_get_bushandle(sc->aac_regs_res0);
217
218 sc->aac_regs_rid1 = PCIR_BAR(2);
219 if ((sc->aac_regs_res1 = bus_alloc_resource_any(sc->aac_dev,
220 SYS_RES_MEMORY, &sc->aac_regs_rid1, RF_ACTIVE)) == NULL) {
221 device_printf(sc->aac_dev,
222 "couldn't allocate register window 1\n");
223 goto out;
224 }
225 sc->aac_btag1 = rman_get_bustag(sc->aac_regs_res1);
226 sc->aac_bhandle1 = rman_get_bushandle(sc->aac_regs_res1);
227
228 /*
229 * Allocate the parent bus DMA tag appropriate for our PCI interface.
230 *
231 * Note that some of these controllers are 64-bit capable.
232 */
233 if (bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
234 PAGE_SIZE, 0, /* algnmnt, boundary */
235 BUS_SPACE_MAXADDR, /* lowaddr */
236 BUS_SPACE_MAXADDR, /* highaddr */
237 NULL, NULL, /* filter, filterarg */
238 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
239 BUS_SPACE_UNRESTRICTED, /* nsegments */
240 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
241 0, /* flags */
242 NULL, NULL, /* No locking needed */
243 &sc->aac_parent_dmat)) {
244 device_printf(sc->aac_dev, "can't allocate parent DMA tag\n");
245 goto out;
246 }
247
248 /* Set up quirks */
249 sc->flags = id->quirks;
250
251 /*
252 * Do bus-independent initialisation.
253 */
254 error = aacraid_attach(sc);
255
256 out:
257 if (error)
258 aacraid_free(sc);
259 return(error);
260 }
261