Merge branch 'vendor/OPENSSL'
[dragonfly.git] / sys / dev / raid / aac / aac_pci.c
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2001 Scott Long
4  * Copyright (c) 2000 BSDi
5  * Copyright (c) 2001 Adaptec, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
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
21  * FOR 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  *      $FreeBSD: src/sys/dev/aac/aac_pci.c,v 1.3.2.19 2003/11/01 18:44:51 scottl Exp $
30  *      $DragonFly: src/sys/dev/raid/aac/aac_pci.c,v 1.13 2008/01/20 03:40:35 pavalos Exp $
31  */
32
33 /*
34  * PCI bus interface and resource allocation.
35  */
36
37 #include "opt_aac.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43
44 #include <sys/bus.h>
45 #include <sys/conf.h>
46 #include <sys/devicestat.h>
47 #include <sys/disk.h>
48 #include <sys/rman.h>
49
50 #include <bus/pci/pcireg.h>
51 #include <bus/pci/pcivar.h>
52
53 #include "aacreg.h"
54 #include "aac_ioctl.h"
55 #include "aacvar.h"
56
57 static int      aac_pci_probe(device_t dev);
58 static int      aac_pci_attach(device_t dev);
59
60 static device_method_t aac_methods[] = {
61         /* Device interface */
62         DEVMETHOD(device_probe,         aac_pci_probe),
63         DEVMETHOD(device_attach,        aac_pci_attach),
64         DEVMETHOD(device_detach,        aac_detach),
65         DEVMETHOD(device_suspend,       aac_suspend),
66         DEVMETHOD(device_resume,        aac_resume),
67
68         DEVMETHOD(bus_print_child,      bus_generic_print_child),
69         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
70         { 0, 0 }
71 };
72
73 static driver_t aac_pci_driver = {
74         "aac",
75         aac_methods,
76         sizeof(struct aac_softc)
77 };
78
79 devclass_t      aac_devclass;
80
81 DRIVER_MODULE(aac, pci, aac_pci_driver, aac_devclass, 0, 0);
82
83 static struct aac_ident
84 {
85         u_int16_t               vendor;
86         u_int16_t               device;
87         u_int16_t               subvendor;
88         u_int16_t               subdevice;
89         int                     hwif;
90         int                     quirks;
91         char                    *desc;
92 } aac_identifiers[] = {
93         {0x1028, 0x0001, 0x1028, 0x0001, AAC_HWIF_I960RX, 0,
94         "Dell PERC 2/Si"},
95         {0x1028, 0x0002, 0x1028, 0x0002, AAC_HWIF_I960RX, 0,
96         "Dell PERC 3/Di"},
97         {0x1028, 0x0003, 0x1028, 0x0003, AAC_HWIF_I960RX, 0,
98         "Dell PERC 3/Si"},
99         {0x1028, 0x0004, 0x1028, 0x00d0, AAC_HWIF_I960RX, 0,
100         "Dell PERC 3/Si"},
101         {0x1028, 0x0002, 0x1028, 0x00d1, AAC_HWIF_I960RX, 0,
102         "Dell PERC 3/Di"},
103         {0x1028, 0x0002, 0x1028, 0x00d9, AAC_HWIF_I960RX, 0,
104         "Dell PERC 3/Di"},
105         {0x1028, 0x000a, 0x1028, 0x0106, AAC_HWIF_I960RX, 0,
106         "Dell PERC 3/Di"},
107         {0x1028, 0x000a, 0x1028, 0x011b, AAC_HWIF_I960RX, 0,
108         "Dell PERC 3/Di"},
109         {0x1028, 0x000a, 0x1028, 0x0121, AAC_HWIF_I960RX, 0,
110         "Dell PERC 3/Di"},
111         {0x1011, 0x0046, 0x9005, 0x0364, AAC_HWIF_STRONGARM, 0,
112         "Adaptec AAC-364"},
113         {0x1011, 0x0046, 0x9005, 0x0365, AAC_HWIF_STRONGARM,
114          AAC_FLAGS_BROKEN_MEMMAP, "Adaptec SCSI RAID 5400S"},
115         {0x1011, 0x0046, 0x9005, 0x1364, AAC_HWIF_STRONGARM, AAC_FLAGS_PERC2QC,
116         "Dell PERC 2/QC"},
117         {0x1011, 0x0046, 0x103c, 0x10c2, AAC_HWIF_STRONGARM, 0,
118         "HP NetRaid-4M"},
119         {0x9005, 0x0285, 0x9005, 0x0285, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB,
120         "Adaptec SCSI RAID 2200S"},
121         {0x9005, 0x0285, 0x1028, 0x0287, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB,
122         "Dell PERC 320/DC"},
123         {0x9005, 0x0285, 0x9005, 0x0286, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB,
124         "Adaptec SCSI RAID 2120S"},
125         {0x9005, 0x0285, 0x9005, 0x0290, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB,
126          "Adaptec SATA RAID 2410SA"},
127         {0x9005, 0x0285, 0x1028, 0x0291, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB,
128          "Dell CERC SATA RAID 2"},
129         {0x9005, 0x0285, 0x9005, 0x0292, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB,
130          "Adaptec SATA RAID 2810SA"},
131         {0x9005, 0x0285, 0x9005, 0x0293, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB,
132          "Adaptec SATA RAID 21610SA"},
133         {0x9005, 0x0285, 0x103c, 0x3227, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB,
134          "HP ML110 G2 (Adaptec 2610SA)"},
135         {0x9005, 0x0286, 0x9005, 0x028c, AAC_HWIF_RKT, 0,
136          "Adaptec SCSI RAID 2230S"},
137         {0x9005, 0x0286, 0x9005, 0x028d, AAC_HWIF_RKT, 0,
138          "Adaptec SCSI RAID 2130S"},
139
140         {0x9005, 0x0285, 0x9005, 0x0287, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB |
141          AAC_FLAGS_256FIBS, "Adaptec SCSI RAID 2200S"},
142         {0x9005, 0x0285, 0x17aa, 0x0286, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB |
143          AAC_FLAGS_256FIBS, "Legend S220"},
144         {0x9005, 0x0285, 0x17aa, 0x0287, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB |
145          AAC_FLAGS_256FIBS, "Legend S230"},
146         {0x9005, 0x0285, 0x9005, 0x0288, AAC_HWIF_I960RX, 0,
147          "Adaptec SCSI RAID 3230S"},
148         {0x9005, 0x0285, 0x9005, 0x0289, AAC_HWIF_I960RX, 0,
149          "Adaptec SCSI RAID 3240S"},
150         {0x9005, 0x0285, 0x9005, 0x028a, AAC_HWIF_I960RX, 0,
151          "Adaptec SCSI RAID 2020ZCR"},
152         {0x9005, 0x0285, 0x9005, 0x028b, AAC_HWIF_I960RX, 0,
153          "Adaptec SCSI RAID 2025ZCR"},
154         {0x9005, 0x0286, 0x9005, 0x029b, AAC_HWIF_RKT, 0,
155          "Adaptec SATA RAID 2820SA"},
156         {0x9005, 0x0286, 0x9005, 0x029c, AAC_HWIF_RKT, 0,
157          "Adaptec SATA RAID 2620SA"},
158         {0x9005, 0x0286, 0x9005, 0x029d, AAC_HWIF_RKT, 0,
159          "Adaptec SATA RAID 2420SA"},
160         {0x9005, 0x0286, 0x9005, 0x029e, AAC_HWIF_RKT, 0,
161          "ICP ICP9024RO SCSI RAID"},
162         {0x9005, 0x0286, 0x9005, 0x029f, AAC_HWIF_RKT, 0,
163          "ICP ICP9014RO SCSI RAID"},
164         {0x9005, 0x0285, 0x9005, 0x0294, AAC_HWIF_I960RX, 0,
165          "Adaptec SATA RAID 2026ZCR"},
166         {0x9005, 0x0285, 0x103c, 0x3227, AAC_HWIF_I960RX, 0,
167          "Adaptec SATA RAID 2610SA"},
168         {0x9005, 0x0285, 0x9005, 0x0296, AAC_HWIF_I960RX, 0,
169          "Adaptec SCSI RAID 2240S"},
170         {0x9005, 0x0285, 0x9005, 0x0297, AAC_HWIF_I960RX, 0,
171          "Adaptec SAS RAID 4005SAS"},
172         {0x9005, 0x0285, 0x1014, 0x02f2, AAC_HWIF_I960RX, 0,
173          "IBM ServeRAID 8i"},
174         {0x9005, 0x0285, 0x9005, 0x0298, AAC_HWIF_I960RX, 0,
175          "Adaptec SAS RAID 4000SAS"},
176         {0x9005, 0x0285, 0x9005, 0x0299, AAC_HWIF_I960RX, 0,
177          "Adaptec SAS RAID 4800SAS"},
178         {0x9005, 0x0285, 0x9005, 0x029a, AAC_HWIF_I960RX, 0,
179          "Adaptec SAS RAID 4805SAS"},
180         {0x9005, 0x0285, 0x9005, 0x028e, AAC_HWIF_I960RX, 0,
181          "Adaptec SATA RAID 2020SA ZCR"},
182         {0x9005, 0x0285, 0x9005, 0x028f, AAC_HWIF_I960RX, 0,
183          "Adaptec SATA RAID 2025SA ZCR"},
184         {0x9005, 0x0285, 0x9005, 0x02a4, AAC_HWIF_I960RX, 0,
185          "ICP ICP9085LI SAS RAID"},
186         {0x9005, 0x0285, 0x9005, 0x02a5, AAC_HWIF_I960RX, 0,
187          "ICP ICP5085BR SAS RAID"},
188         {0x9005, 0x0286, 0x9005, 0x02a0, AAC_HWIF_RKT, 0,
189          "ICP ICP9047MA SATA RAID"},
190         {0x9005, 0x0286, 0x9005, 0x02a1, AAC_HWIF_RKT, 0,
191          "ICP ICP9087MA SATA RAID"},
192         {0x9005, 0x0285, 0x9005, 0x02bb, AAC_HWIF_I960RX, 0,
193          "Adaptec RAID 3405"},
194         {0x9005, 0x0285, 0x9005, 0x02bc, AAC_HWIF_I960RX, 0,
195          "Adaptec RAID 3805"},
196         {0x9005, 0x0285, 0x9005, 0x02d1, AAC_HWIF_I960RX, 0,
197          "Adaptec RAID 5405"},
198         {0x9005, 0x0286, 0x1014, 0x9580, AAC_HWIF_RKT, 0,
199          "IBM ServeRAID-8k"},
200         {0, 0, 0, 0, 0, 0, 0}
201 };
202
203 static struct aac_ident *
204 aac_find_ident(device_t dev)
205 {
206         struct aac_ident *m;
207
208         for (m = aac_identifiers; m->vendor != 0; m++) {
209                 if ((m->vendor == pci_get_vendor(dev)) &&
210                     (m->device == pci_get_device(dev)) &&
211                     (m->subvendor == pci_get_subvendor(dev)) &&
212                     (m->subdevice == pci_get_subdevice(dev)))
213                 return (m);
214         }
215
216         return (NULL);
217 }
218
219 /*
220  * Determine whether this is one of our supported adapters.
221  */
222 static int
223 aac_pci_probe(device_t dev)
224 {
225         struct aac_ident *id;
226
227         debug_called(1);
228
229         if ((id = aac_find_ident(dev)) != NULL) {
230                 device_set_desc(dev, id->desc);
231                 return(BUS_PROBE_DEFAULT);
232         }
233         return(ENXIO);
234 }
235
236 /*
237  * Allocate resources for our device, set up the bus interface.
238  */
239 static int
240 aac_pci_attach(device_t dev)
241 {
242         struct aac_softc *sc;
243         struct aac_ident *id;
244         int error;
245         u_int32_t command;
246
247         debug_called(1);
248
249         /*
250          * Initialise softc.
251          */
252         sc = device_get_softc(dev);
253         bzero(sc, sizeof(*sc));
254         sc->aac_dev = dev;
255
256         /* assume failure is 'not configured' */
257         error = ENXIO;
258
259         /* 
260          * Verify that the adapter is correctly set up in PCI space.
261          */
262         command = pci_read_config(sc->aac_dev, PCIR_COMMAND, 2);
263         command |= PCIM_CMD_BUSMASTEREN;
264         pci_write_config(dev, PCIR_COMMAND, command, 2);
265         command = pci_read_config(sc->aac_dev, PCIR_COMMAND, 2);
266         if (!(command & PCIM_CMD_BUSMASTEREN)) {
267                 device_printf(sc->aac_dev, "can't enable bus-master feature\n");
268                 goto out;
269         }
270         if ((command & PCIM_CMD_MEMEN) == 0) {
271                 device_printf(sc->aac_dev, "memory window not available\n");
272                 goto out;
273         }
274
275         /*
276          * Allocate the PCI register window.
277          */
278         sc->aac_regs_rid = PCIR_BAR(0);
279         if ((sc->aac_regs_resource = bus_alloc_resource_any(sc->aac_dev,
280                                                             SYS_RES_MEMORY,
281                                                             &sc->aac_regs_rid,
282                                                             RF_ACTIVE)) ==
283                                                             NULL) {
284                 device_printf(sc->aac_dev,
285                               "couldn't allocate register window\n");
286                 goto out;
287         }
288         sc->aac_btag = rman_get_bustag(sc->aac_regs_resource);
289         sc->aac_bhandle = rman_get_bushandle(sc->aac_regs_resource);
290
291         /* assume failure is 'out of memory' */
292         error = ENOMEM;
293
294         /*
295          * Allocate the parent bus DMA tag appropriate for our PCI interface.
296          * 
297          * Note that some of these controllers are 64-bit capable.
298          */
299         if (bus_dma_tag_create(NULL,                    /* parent */
300                                PAGE_SIZE, 0,            /* algnmnt, boundary */
301                                BUS_SPACE_MAXADDR,       /* lowaddr */
302                                BUS_SPACE_MAXADDR,       /* highaddr */
303                                NULL, NULL,              /* filter, filterarg */
304                                BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
305                                BUS_SPACE_UNRESTRICTED,  /* nsegments */
306                                BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
307                                0,                       /* flags */
308                                &sc->aac_parent_dmat)) {
309                 device_printf(sc->aac_dev, "can't allocate parent DMA tag\n");
310                 goto out;
311         }
312
313         /* 
314          * Detect the hardware interface version, set up the bus interface
315          * indirection.
316          */
317         id = aac_find_ident(dev);
318         sc->aac_hwif = id->hwif;
319         switch(sc->aac_hwif) {
320         case AAC_HWIF_I960RX:
321                 debug(2, "set hardware up for i960Rx");
322                 sc->aac_if = aac_rx_interface;
323                 break;
324         case AAC_HWIF_STRONGARM:
325                 debug(2, "set hardware up for StrongARM");
326                 sc->aac_if = aac_sa_interface;
327                 break;
328         case AAC_HWIF_FALCON:
329                 debug(2, "set hardware up for Falcon/PPC");
330                 sc->aac_if = aac_fa_interface;
331                 break;
332         case AAC_HWIF_RKT:
333                 debug(2, "set hardware up for Rocket/MIPS");
334                 sc->aac_if = aac_rkt_interface;
335                 break;
336         default:
337                 sc->aac_hwif = AAC_HWIF_UNKNOWN;
338                 device_printf(sc->aac_dev, "unknown hardware type\n");
339                 error = ENXIO;
340                 goto out;
341         }
342
343         /* Set up quirks */
344         sc->flags = id->quirks;
345
346         /*
347          * Do bus-independent initialisation.
348          */
349         error = aac_attach(sc);
350
351 out:
352         if (error)
353                 aac_free(sc);
354         return(error);
355 }
356
357 /*
358  * Do nothing driver that will attach to the SCSI channels of a Dell PERC
359  * controller.  This is needed to keep the power management subsystem from
360  * trying to power down these devices.
361  */
362 static int aacch_probe(device_t dev);
363 static int aacch_attach(device_t dev);
364 static int aacch_detach(device_t dev);
365
366 static device_method_t aacch_methods[] = {
367         /* Device interface */
368         DEVMETHOD(device_probe,         aacch_probe),
369         DEVMETHOD(device_attach,        aacch_attach),
370         DEVMETHOD(device_detach,        aacch_detach),
371         { 0, 0 }
372 };
373
374 struct aacch_softc {
375         device_t        dev;
376 };
377
378 static driver_t aacch_driver = {
379         "aacch",
380         aacch_methods,
381         sizeof(struct aacch_softc)
382 };
383
384 static devclass_t       aacch_devclass;
385 DRIVER_MODULE(aacch, pci, aacch_driver, aacch_devclass, 0, 0);
386
387 static int
388 aacch_probe(device_t dev)
389 {
390
391         if ((pci_get_vendor(dev) != 0x9005) ||
392             (pci_get_device(dev) != 0x00c5))
393                 return (ENXIO);
394
395         device_set_desc(dev, "AAC RAID Channel");
396         return (-10);
397 }
398
399 static int
400 aacch_attach(device_t dev)
401 {
402         struct aacch_softc *sc;
403
404         sc = device_get_softc(dev);
405
406         sc->dev = dev;
407
408         return (0);
409 }
410
411 static int
412 aacch_detach(device_t dev)
413 {
414
415         return (0);
416 }