Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / raid / amr / amr_pci.c
1 /*-
2  * Copyright (c) 1999,2000 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * Copyright (c) 2002 Eric Moore
28  * Copyright (c) 2002 LSI Logic Corporation
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
34  * 1. Redistributions of source code must retain the above copyright
35  *    notice, this list of conditions and the following disclaimer.
36  * 2. Redistributions in binary form must reproduce the above copyright
37  *    notice, this list of conditions and the following disclaimer in the
38  *    documentation and/or other materials provided with the distribution.
39  * 3. The party using or redistributing the source code and binary forms
40  *    agrees to the disclaimer below and the terms and conditions set forth
41  *    herein.
42  *
43  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53  * SUCH DAMAGE.
54  *
55  *      $FreeBSD: src/sys/dev/amr/amr_pci.c,v 1.1.2.9 2002/12/20 15:12:04 emoore Exp $
56  */
57
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/kernel.h>
61
62 #include <dev/amr/amr_compat.h>
63 #include <sys/bus.h>
64 #include <sys/conf.h>
65 #include <sys/devicestat.h>
66 #include <sys/disk.h>
67
68 #include <machine/bus_memio.h>
69 #include <machine/bus_pio.h>
70 #include <machine/bus.h>
71 #include <machine/resource.h>
72 #include <sys/rman.h>
73
74 #include <pci/pcireg.h>
75 #include <pci/pcivar.h>
76
77 #include <dev/amr/amrio.h>
78 #include <dev/amr/amrreg.h>
79 #include <dev/amr/amrvar.h>
80
81 static int              amr_pci_probe(device_t dev);
82 static int              amr_pci_attach(device_t dev);
83 static int              amr_pci_detach(device_t dev);
84 static int              amr_pci_shutdown(device_t dev);
85 static int              amr_pci_suspend(device_t dev);
86 static int              amr_pci_resume(device_t dev);
87 static void             amr_pci_intr(void *arg);
88 static void             amr_pci_free(struct amr_softc *sc);
89 static void             amr_sglist_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error);
90 static int              amr_sglist_map(struct amr_softc *sc);
91 static void             amr_setup_mbox_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error);
92 static int              amr_setup_mbox(struct amr_softc *sc);
93
94 static device_method_t amr_methods[] = {
95     /* Device interface */
96     DEVMETHOD(device_probe,     amr_pci_probe),
97     DEVMETHOD(device_attach,    amr_pci_attach),
98     DEVMETHOD(device_detach,    amr_pci_detach),
99     DEVMETHOD(device_shutdown,  amr_pci_shutdown),
100     DEVMETHOD(device_suspend,   amr_pci_suspend),
101     DEVMETHOD(device_resume,    amr_pci_resume),
102
103     DEVMETHOD(bus_print_child,  bus_generic_print_child),
104     DEVMETHOD(bus_driver_added, bus_generic_driver_added),
105     { 0, 0 }
106 };
107
108 static driver_t amr_pci_driver = {
109         "amr",
110         amr_methods,
111         sizeof(struct amr_softc)
112 };
113
114 static devclass_t       amr_devclass;
115 DRIVER_MODULE(amr, pci, amr_pci_driver, amr_devclass, 0, 0);
116
117 static struct
118 {
119     int         vendor;
120     int         device;
121     int         flag;
122 #define PROBE_SIGNATURE (1<<0)
123 } amr_device_ids[] = {
124     {0x101e, 0x9010, 0},
125     {0x101e, 0x9060, 0},
126     {0x8086, 0x1960, PROBE_SIGNATURE},/* generic i960RD, check for signature */
127     {0x101e, 0x1960, 0},
128     {0x1000, 0x1960, PROBE_SIGNATURE},
129     {0x1000, 0x0407, 0},
130     {0x1028, 0x000e, PROBE_SIGNATURE}, /* perc4/di i960 */
131     {0x1028, 0x000f, 0}, /* perc4/di Verde*/
132     {0, 0, 0}
133 };
134
135 static int
136 amr_pci_probe(device_t dev)
137 {
138     int         i, sig;
139
140     debug_called(1);
141
142     for (i = 0; amr_device_ids[i].vendor != 0; i++) {
143         if ((pci_get_vendor(dev) == amr_device_ids[i].vendor) &&
144             (pci_get_device(dev) == amr_device_ids[i].device)) {
145
146             /* do we need to test for a signature? */
147             if (amr_device_ids[i].flag & PROBE_SIGNATURE) {
148                 sig = pci_read_config(dev, AMR_CFG_SIG, 2);
149                 if ((sig != AMR_SIGNATURE_1) && (sig != AMR_SIGNATURE_2))
150                     continue;
151             }
152             device_set_desc(dev, "LSILogic MegaRAID");
153             return(-10);        /* allow room to be overridden */
154         }
155     }
156     return(ENXIO);
157 }
158
159 static int
160 amr_pci_attach(device_t dev)
161 {
162     struct amr_softc    *sc;
163     int                 rid, rtype, error;
164     u_int32_t           command;
165
166     debug_called(1);
167
168     /*
169      * Initialise softc.
170      */
171     sc = device_get_softc(dev);
172     bzero(sc, sizeof(*sc));
173     sc->amr_dev = dev;
174
175     /* assume failure is 'not configured' */
176     error = ENXIO;
177
178     /*
179      * Determine board type.
180      */
181     command = pci_read_config(dev, PCIR_COMMAND, 1);
182     if ((pci_get_device(dev) == 0x1960) || (pci_get_device(dev) == 0x0407) ||
183         (pci_get_device(dev) == 0x000e) || (pci_get_device(dev) == 0x000f)) {
184         /*
185          * Make sure we are going to be able to talk to this board.
186          */
187         if ((command & PCIM_CMD_MEMEN) == 0) {
188             device_printf(dev, "memory window not available\n");
189             goto out;
190         }
191         sc->amr_type |= AMR_TYPE_QUARTZ;
192
193     } else {
194         /*
195          * Make sure we are going to be able to talk to this board.
196          */
197         if ((command & PCIM_CMD_PORTEN) == 0) {
198             device_printf(dev, "I/O window not available\n");
199             goto out;
200         }
201     }
202
203     /* force the busmaster enable bit on */
204     if (!(command & PCIM_CMD_BUSMASTEREN)) {
205         device_printf(dev, "busmaster bit not set, enabling\n");
206         command |= PCIM_CMD_BUSMASTEREN;
207         pci_write_config(dev, PCIR_COMMAND, command, 2);
208     }
209
210     /*
211      * Allocate the PCI register window.
212      */
213     rid = PCIR_MAPS;
214     rtype = AMR_IS_QUARTZ(sc) ? SYS_RES_MEMORY : SYS_RES_IOPORT;
215     sc->amr_reg = bus_alloc_resource(dev, rtype, &rid, 0, ~0, 1, RF_ACTIVE);
216     if (sc->amr_reg == NULL) {
217         device_printf(sc->amr_dev, "can't allocate register window\n");
218         goto out;
219     }
220     sc->amr_btag = rman_get_bustag(sc->amr_reg);
221     sc->amr_bhandle = rman_get_bushandle(sc->amr_reg);
222
223     /*
224      * Allocate and connect our interrupt.
225      */
226     rid = 0;
227     sc->amr_irq = bus_alloc_resource(sc->amr_dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE);
228     if (sc->amr_irq == NULL) {
229         device_printf(sc->amr_dev, "can't allocate interrupt\n");
230         goto out;
231     }
232     if (bus_setup_intr(sc->amr_dev, sc->amr_irq, INTR_TYPE_BIO | INTR_ENTROPY, amr_pci_intr, sc, &sc->amr_intr)) {
233         device_printf(sc->amr_dev, "can't set up interrupt\n");
234         goto out;
235     }
236
237     debug(2, "interrupt attached");
238
239     /* assume failure is 'out of memory' */
240     error = ENOMEM;
241
242     /*
243      * Allocate the parent bus DMA tag appropriate for PCI.
244      */
245     if (bus_dma_tag_create(NULL,                        /* parent */
246                            1, 0,                        /* alignment, boundary */
247                            BUS_SPACE_MAXADDR_32BIT,     /* lowaddr */
248                            BUS_SPACE_MAXADDR,           /* highaddr */
249                            NULL, NULL,                  /* filter, filterarg */
250                            MAXBSIZE, AMR_NSEG,          /* maxsize, nsegments */
251                            BUS_SPACE_MAXSIZE_32BIT,     /* maxsegsize */
252                            BUS_DMA_ALLOCNOW,            /* flags */
253                            &sc->amr_parent_dmat)) {
254         device_printf(dev, "can't allocate parent DMA tag\n");
255         goto out;
256     }
257
258     /*
259      * Create DMA tag for mapping buffers into controller-addressable space.
260      */
261     if (bus_dma_tag_create(sc->amr_parent_dmat,         /* parent */
262                            1, 0,                        /* alignment, boundary */
263                            BUS_SPACE_MAXADDR,           /* lowaddr */
264                            BUS_SPACE_MAXADDR,           /* highaddr */
265                            NULL, NULL,                  /* filter, filterarg */
266                            MAXBSIZE, AMR_NSEG,          /* maxsize, nsegments */
267                            BUS_SPACE_MAXSIZE_32BIT,     /* maxsegsize */
268                            0,                           /* flags */
269                            &sc->amr_buffer_dmat)) {
270         device_printf(sc->amr_dev, "can't allocate buffer DMA tag\n");
271         goto out;
272     }
273
274     debug(2, "dma tag done");
275
276     /*
277      * Allocate and set up mailbox in a bus-visible fashion.
278      */
279     if ((error = amr_setup_mbox(sc)) != 0)
280         goto out;
281
282     debug(2, "mailbox setup");
283
284     /*
285      * Build the scatter/gather buffers.
286      */
287     if (amr_sglist_map(sc))
288         goto out;
289
290     debug(2, "s/g list mapped");
291
292     /*
293      * Do bus-independant initialisation, bring controller online.
294      */
295     error = amr_attach(sc);
296
297 out:
298     if (error)
299         amr_pci_free(sc);
300     return(error);
301 }
302
303 /********************************************************************************
304  * Disconnect from the controller completely, in preparation for unload.
305  */
306 static int
307 amr_pci_detach(device_t dev)
308 {
309     struct amr_softc    *sc = device_get_softc(dev);
310     int                 error;
311
312     debug_called(1);
313
314     if (sc->amr_state & AMR_STATE_OPEN)
315         return(EBUSY);
316
317     if ((error = amr_pci_shutdown(dev)))
318         return(error);
319
320     amr_pci_free(sc);
321
322     return(0);
323 }
324
325 /********************************************************************************
326  * Bring the controller down to a dormant state and detach all child devices.
327  *
328  * This function is called before detach, system shutdown, or before performing
329  * an operation which may add or delete system disks.  (Call amr_startup to
330  * resume normal operation.)
331  *
332  * Note that we can assume that the bioq on the controller is empty, as we won't
333  * allow shutdown if any device is open.
334  */
335 static int
336 amr_pci_shutdown(device_t dev)
337 {
338     struct amr_softc    *sc = device_get_softc(dev);
339     int                 i,error,s;
340
341     debug_called(1);
342
343     /* mark ourselves as in-shutdown */
344     sc->amr_state |= AMR_STATE_SHUTDOWN;
345
346
347     /* flush controller */
348     device_printf(sc->amr_dev, "flushing cache...");
349     printf("%s\n", amr_flush(sc) ? "failed" : "done");
350
351     s = splbio();
352     error = 0;
353
354     /* delete all our child devices */
355     for(i = 0 ; i < AMR_MAXLD; i++) {
356         if( sc->amr_drive[i].al_disk != 0) {
357             if((error = device_delete_child(sc->amr_dev,sc->amr_drive[i].al_disk)) != 0)
358                 goto shutdown_out;
359             sc->amr_drive[i].al_disk = 0;
360         }
361     }
362
363     /* XXX disable interrupts? */
364
365 shutdown_out:
366     splx(s);
367     return(error);
368 }
369
370 /********************************************************************************
371  * Bring the controller to a quiescent state, ready for system suspend.
372  */
373 static int
374 amr_pci_suspend(device_t dev)
375 {
376     struct amr_softc    *sc = device_get_softc(dev);
377
378     debug_called(1);
379
380     sc->amr_state |= AMR_STATE_SUSPEND;
381
382     /* flush controller */
383     device_printf(sc->amr_dev, "flushing cache...");
384     printf("%s\n", amr_flush(sc) ? "failed" : "done");
385     
386     /* XXX disable interrupts? */
387
388     return(0);
389 }
390
391 /********************************************************************************
392  * Bring the controller back to a state ready for operation.
393  */
394 static int
395 amr_pci_resume(device_t dev)
396 {
397     struct amr_softc    *sc = device_get_softc(dev);
398
399     debug_called(1);
400
401     sc->amr_state &= ~AMR_STATE_SUSPEND;
402
403     /* XXX enable interrupts? */
404
405     return(0);
406 }
407
408 /*******************************************************************************
409  * Take an interrupt, or be poked by other code to look for interrupt-worthy
410  * status.
411  */
412 static void
413 amr_pci_intr(void *arg)
414 {
415     struct amr_softc    *sc = (struct amr_softc *)arg;
416
417     debug_called(2);
418
419     /* collect finished commands, queue anything waiting */
420     amr_done(sc);
421 }
422
423 /********************************************************************************
424  * Free all of the resources associated with (sc)
425  *
426  * Should not be called if the controller is active.
427  */
428 static void
429 amr_pci_free(struct amr_softc *sc)
430 {
431     u_int8_t                    *p;
432     
433     debug_called(1);
434
435     amr_free(sc);
436
437     /* destroy data-transfer DMA tag */
438     if (sc->amr_buffer_dmat)
439         bus_dma_tag_destroy(sc->amr_buffer_dmat);
440
441     /* free and destroy DMA memory and tag for s/g lists */
442     if (sc->amr_sgtable)
443         bus_dmamem_free(sc->amr_sg_dmat, sc->amr_sgtable, sc->amr_sg_dmamap);
444     if (sc->amr_sg_dmat)
445         bus_dma_tag_destroy(sc->amr_sg_dmat);
446
447     /* free and destroy DMA memory and tag for mailbox */
448     if (sc->amr_mailbox) {
449         p = (u_int8_t *)(uintptr_t)(volatile void *)sc->amr_mailbox;
450         bus_dmamem_free(sc->amr_mailbox_dmat, p - 16, sc->amr_mailbox_dmamap);
451     }
452     if (sc->amr_mailbox_dmat)
453         bus_dma_tag_destroy(sc->amr_mailbox_dmat);
454
455     /* disconnect the interrupt handler */
456     if (sc->amr_intr)
457         bus_teardown_intr(sc->amr_dev, sc->amr_irq, sc->amr_intr);
458     if (sc->amr_irq != NULL)
459         bus_release_resource(sc->amr_dev, SYS_RES_IRQ, 0, sc->amr_irq);
460
461     /* destroy the parent DMA tag */
462     if (sc->amr_parent_dmat)
463         bus_dma_tag_destroy(sc->amr_parent_dmat);
464
465     /* release the register window mapping */
466     if (sc->amr_reg != NULL)
467         bus_release_resource(sc->amr_dev,
468                              AMR_IS_QUARTZ(sc) ? SYS_RES_MEMORY : SYS_RES_IOPORT,
469                              PCIR_MAPS, sc->amr_reg);
470 }
471
472 /********************************************************************************
473  * Allocate and map the scatter/gather table in bus space.
474  */
475 static void
476 amr_sglist_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
477 {
478     struct amr_softc    *sc = (struct amr_softc *)arg;
479
480     debug_called(1);
481
482     /* save base of s/g table's address in bus space */
483     sc->amr_sgbusaddr = segs->ds_addr;
484 }
485
486 static int
487 amr_sglist_map(struct amr_softc *sc)
488 {
489     size_t      segsize;
490     int         error;
491
492     debug_called(1);
493
494     /*
495      * Create a single tag describing a region large enough to hold all of
496      * the s/g lists we will need.
497      *
498      * Note that we could probably use AMR_LIMITCMD here, but that may become tunable.
499      */
500     segsize = sizeof(struct amr_sgentry) * AMR_NSEG * AMR_MAXCMD;
501     error = bus_dma_tag_create(sc->amr_parent_dmat,     /* parent */
502                                1, 0,                    /* alignment, boundary */
503                                BUS_SPACE_MAXADDR,       /* lowaddr */
504                                BUS_SPACE_MAXADDR,       /* highaddr */
505                                NULL, NULL,              /* filter, filterarg */
506                                segsize, 1,              /* maxsize, nsegments */
507                                BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
508                                0,                       /* flags */
509                                &sc->amr_sg_dmat);
510     if (error != 0) {
511         device_printf(sc->amr_dev, "can't allocate scatter/gather DMA tag\n");
512         return(ENOMEM);
513     }
514
515     /*
516      * Allocate enough s/g maps for all commands and permanently map them into
517      * controller-visible space.
518      *  
519      * XXX this assumes we can get enough space for all the s/g maps in one 
520      * contiguous slab.  We may need to switch to a more complex arrangement where
521      * we allocate in smaller chunks and keep a lookup table from slot to bus address.
522      *
523      * XXX HACK ALERT: at least some controllers don't like the s/g memory being
524      *                 allocated below 0x2000.  We leak some memory if we get some
525      *                 below this mark and allocate again.  We should be able to
526      *                 avoid this with the tag setup, but that does't seem to work.
527      */
528 retry:
529     error = bus_dmamem_alloc(sc->amr_sg_dmat, (void **)&sc->amr_sgtable, BUS_DMA_NOWAIT, &sc->amr_sg_dmamap);
530     if (error) {
531         device_printf(sc->amr_dev, "can't allocate s/g table\n");
532         return(ENOMEM);
533     }
534     bus_dmamap_load(sc->amr_sg_dmat, sc->amr_sg_dmamap, sc->amr_sgtable, segsize, amr_sglist_map_helper, sc, 0);
535     if (sc->amr_sgbusaddr < 0x2000) {
536         debug(1, "s/g table too low (0x%x), reallocating\n", sc->amr_sgbusaddr);
537         goto retry;
538     }
539     return(0);
540 }
541
542 /********************************************************************************
543  * Allocate and set up mailbox areas for the controller (sc)
544  *
545  * The basic mailbox structure should be 16-byte aligned.  This means that the
546  * mailbox64 structure has 4 bytes hanging off the bottom.
547  */
548 static void
549 amr_setup_mbox_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
550 {
551     struct amr_softc    *sc = (struct amr_softc *)arg;
552     
553     debug_called(1);
554
555     /* save phsyical base of the basic mailbox structure */
556     sc->amr_mailboxphys = segs->ds_addr + 16;
557 }
558
559 static int
560 amr_setup_mbox(struct amr_softc *sc)
561 {
562     int         error;
563     u_int8_t    *p;
564     
565     debug_called(1);
566
567     /*
568      * Create a single tag describing a region large enough to hold the entire
569      * mailbox.
570      */
571     error = bus_dma_tag_create(sc->amr_parent_dmat,     /* parent */
572                                16, 0,                   /* alignment, boundary */
573                                BUS_SPACE_MAXADDR,       /* lowaddr */
574                                BUS_SPACE_MAXADDR,       /* highaddr */
575                                NULL, NULL,              /* filter, filterarg */
576                                sizeof(struct amr_mailbox) + 16, 1, /* maxsize, nsegments */
577                                BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
578                                0,                       /* flags */
579                                &sc->amr_mailbox_dmat);
580     if (error != 0) {
581         device_printf(sc->amr_dev, "can't allocate mailbox tag\n");
582         return(ENOMEM);
583     }
584
585     /*
586      * Allocate the mailbox structure and permanently map it into
587      * controller-visible space.
588      */
589     error = bus_dmamem_alloc(sc->amr_mailbox_dmat, (void **)&p, BUS_DMA_NOWAIT, 
590                              &sc->amr_mailbox_dmamap);
591     if (error) {
592         device_printf(sc->amr_dev, "can't allocate mailbox memory\n");
593         return(ENOMEM);
594     }
595     bus_dmamap_load(sc->amr_mailbox_dmat, sc->amr_mailbox_dmamap, p, 
596                     sizeof(struct amr_mailbox64), amr_setup_mbox_helper, sc, 0);
597     /*
598      * Conventional mailbox is inside the mailbox64 region.
599      */
600     bzero(p, sizeof(struct amr_mailbox64));
601     sc->amr_mailbox64 = (struct amr_mailbox64 *)(p + 12);
602     sc->amr_mailbox = (struct amr_mailbox *)(p + 16);
603
604     return(0);
605 }