2 * Copyright (c) 1999,2000 Michael Smith
3 * Copyright (c) 2000 BSDi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
27 * Copyright (c) 2002 Eric Moore
28 * Copyright (c) 2002 LSI Logic Corporation
29 * All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
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
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
55 * $FreeBSD: src/sys/dev/amr/amr_pci.c,v 1.1.2.9 2002/12/20 15:12:04 emoore Exp $
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/kernel.h>
62 #include <dev/amr/amr_compat.h>
65 #include <sys/devicestat.h>
68 #include <machine/bus_memio.h>
69 #include <machine/bus_pio.h>
70 #include <machine/bus.h>
71 #include <machine/resource.h>
74 #include <pci/pcireg.h>
75 #include <pci/pcivar.h>
77 #include <dev/amr/amrio.h>
78 #include <dev/amr/amrreg.h>
79 #include <dev/amr/amrvar.h>
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);
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),
103 DEVMETHOD(bus_print_child, bus_generic_print_child),
104 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
108 static driver_t amr_pci_driver = {
111 sizeof(struct amr_softc)
114 static devclass_t amr_devclass;
115 DRIVER_MODULE(amr, pci, amr_pci_driver, amr_devclass, 0, 0);
122 #define PROBE_SIGNATURE (1<<0)
123 } amr_device_ids[] = {
126 {0x8086, 0x1960, PROBE_SIGNATURE},/* generic i960RD, check for signature */
128 {0x1000, 0x1960, PROBE_SIGNATURE},
130 {0x1028, 0x000e, PROBE_SIGNATURE}, /* perc4/di i960 */
131 {0x1028, 0x000f, 0}, /* perc4/di Verde*/
136 amr_pci_probe(device_t dev)
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)) {
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))
152 device_set_desc(dev, "LSILogic MegaRAID");
153 return(-10); /* allow room to be overridden */
160 amr_pci_attach(device_t dev)
162 struct amr_softc *sc;
163 int rid, rtype, error;
171 sc = device_get_softc(dev);
172 bzero(sc, sizeof(*sc));
175 /* assume failure is 'not configured' */
179 * Determine board type.
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)) {
185 * Make sure we are going to be able to talk to this board.
187 if ((command & PCIM_CMD_MEMEN) == 0) {
188 device_printf(dev, "memory window not available\n");
191 sc->amr_type |= AMR_TYPE_QUARTZ;
195 * Make sure we are going to be able to talk to this board.
197 if ((command & PCIM_CMD_PORTEN) == 0) {
198 device_printf(dev, "I/O window not available\n");
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);
211 * Allocate the PCI register window.
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");
220 sc->amr_btag = rman_get_bustag(sc->amr_reg);
221 sc->amr_bhandle = rman_get_bushandle(sc->amr_reg);
224 * Allocate and connect our interrupt.
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");
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");
237 debug(2, "interrupt attached");
239 /* assume failure is 'out of memory' */
243 * Allocate the parent bus DMA tag appropriate for PCI.
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");
259 * Create DMA tag for mapping buffers into controller-addressable space.
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 */
269 &sc->amr_buffer_dmat)) {
270 device_printf(sc->amr_dev, "can't allocate buffer DMA tag\n");
274 debug(2, "dma tag done");
277 * Allocate and set up mailbox in a bus-visible fashion.
279 if ((error = amr_setup_mbox(sc)) != 0)
282 debug(2, "mailbox setup");
285 * Build the scatter/gather buffers.
287 if (amr_sglist_map(sc))
290 debug(2, "s/g list mapped");
293 * Do bus-independant initialisation, bring controller online.
295 error = amr_attach(sc);
303 /********************************************************************************
304 * Disconnect from the controller completely, in preparation for unload.
307 amr_pci_detach(device_t dev)
309 struct amr_softc *sc = device_get_softc(dev);
314 if (sc->amr_state & AMR_STATE_OPEN)
317 if ((error = amr_pci_shutdown(dev)))
325 /********************************************************************************
326 * Bring the controller down to a dormant state and detach all child devices.
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.)
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.
336 amr_pci_shutdown(device_t dev)
338 struct amr_softc *sc = device_get_softc(dev);
343 /* mark ourselves as in-shutdown */
344 sc->amr_state |= AMR_STATE_SHUTDOWN;
347 /* flush controller */
348 device_printf(sc->amr_dev, "flushing cache...");
349 printf("%s\n", amr_flush(sc) ? "failed" : "done");
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)
359 sc->amr_drive[i].al_disk = 0;
363 /* XXX disable interrupts? */
370 /********************************************************************************
371 * Bring the controller to a quiescent state, ready for system suspend.
374 amr_pci_suspend(device_t dev)
376 struct amr_softc *sc = device_get_softc(dev);
380 sc->amr_state |= AMR_STATE_SUSPEND;
382 /* flush controller */
383 device_printf(sc->amr_dev, "flushing cache...");
384 printf("%s\n", amr_flush(sc) ? "failed" : "done");
386 /* XXX disable interrupts? */
391 /********************************************************************************
392 * Bring the controller back to a state ready for operation.
395 amr_pci_resume(device_t dev)
397 struct amr_softc *sc = device_get_softc(dev);
401 sc->amr_state &= ~AMR_STATE_SUSPEND;
403 /* XXX enable interrupts? */
408 /*******************************************************************************
409 * Take an interrupt, or be poked by other code to look for interrupt-worthy
413 amr_pci_intr(void *arg)
415 struct amr_softc *sc = (struct amr_softc *)arg;
419 /* collect finished commands, queue anything waiting */
423 /********************************************************************************
424 * Free all of the resources associated with (sc)
426 * Should not be called if the controller is active.
429 amr_pci_free(struct amr_softc *sc)
437 /* destroy data-transfer DMA tag */
438 if (sc->amr_buffer_dmat)
439 bus_dma_tag_destroy(sc->amr_buffer_dmat);
441 /* free and destroy DMA memory and tag for s/g lists */
443 bus_dmamem_free(sc->amr_sg_dmat, sc->amr_sgtable, sc->amr_sg_dmamap);
445 bus_dma_tag_destroy(sc->amr_sg_dmat);
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);
452 if (sc->amr_mailbox_dmat)
453 bus_dma_tag_destroy(sc->amr_mailbox_dmat);
455 /* disconnect the interrupt handler */
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);
461 /* destroy the parent DMA tag */
462 if (sc->amr_parent_dmat)
463 bus_dma_tag_destroy(sc->amr_parent_dmat);
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);
472 /********************************************************************************
473 * Allocate and map the scatter/gather table in bus space.
476 amr_sglist_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
478 struct amr_softc *sc = (struct amr_softc *)arg;
482 /* save base of s/g table's address in bus space */
483 sc->amr_sgbusaddr = segs->ds_addr;
487 amr_sglist_map(struct amr_softc *sc)
495 * Create a single tag describing a region large enough to hold all of
496 * the s/g lists we will need.
498 * Note that we could probably use AMR_LIMITCMD here, but that may become tunable.
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 */
511 device_printf(sc->amr_dev, "can't allocate scatter/gather DMA tag\n");
516 * Allocate enough s/g maps for all commands and permanently map them into
517 * controller-visible space.
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.
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.
529 error = bus_dmamem_alloc(sc->amr_sg_dmat, (void **)&sc->amr_sgtable, BUS_DMA_NOWAIT, &sc->amr_sg_dmamap);
531 device_printf(sc->amr_dev, "can't allocate s/g table\n");
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);
542 /********************************************************************************
543 * Allocate and set up mailbox areas for the controller (sc)
545 * The basic mailbox structure should be 16-byte aligned. This means that the
546 * mailbox64 structure has 4 bytes hanging off the bottom.
549 amr_setup_mbox_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
551 struct amr_softc *sc = (struct amr_softc *)arg;
555 /* save phsyical base of the basic mailbox structure */
556 sc->amr_mailboxphys = segs->ds_addr + 16;
560 amr_setup_mbox(struct amr_softc *sc)
568 * Create a single tag describing a region large enough to hold the entire
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 */
579 &sc->amr_mailbox_dmat);
581 device_printf(sc->amr_dev, "can't allocate mailbox tag\n");
586 * Allocate the mailbox structure and permanently map it into
587 * controller-visible space.
589 error = bus_dmamem_alloc(sc->amr_mailbox_dmat, (void **)&p, BUS_DMA_NOWAIT,
590 &sc->amr_mailbox_dmamap);
592 device_printf(sc->amr_dev, "can't allocate mailbox memory\n");
595 bus_dmamap_load(sc->amr_mailbox_dmat, sc->amr_mailbox_dmamap, p,
596 sizeof(struct amr_mailbox64), amr_setup_mbox_helper, sc, 0);
598 * Conventional mailbox is inside the mailbox64 region.
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);