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
28 * SPDX-License-Identifier: BSD-3-Clause
30 * Copyright (c) 2002 Eric Moore
31 * Copyright (c) 2002, 2004 LSI Logic Corporation
32 * All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. The party using or redistributing the source code and binary forms
43 * agrees to the disclaimer below and the terms and conditions set forth
46 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/module.h>
66 #include <sys/sysctl.h>
72 #include <machine/bus.h>
73 #include <machine/resource.h>
76 #include <dev/pci/pcireg.h>
77 #include <dev/pci/pcivar.h>
79 #include <dev/amr/amrio.h>
80 #include <dev/amr/amrreg.h>
81 #include <dev/amr/amrvar.h>
83 static int amr_pci_probe(device_t dev);
84 static int amr_pci_attach(device_t dev);
85 static int amr_pci_detach(device_t dev);
86 static int amr_pci_shutdown(device_t dev);
87 static int amr_pci_suspend(device_t dev);
88 static int amr_pci_resume(device_t dev);
89 static void amr_pci_intr(void *arg);
90 static void amr_pci_free(struct amr_softc *sc);
91 static void amr_sglist_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error);
92 static int amr_sglist_map(struct amr_softc *sc);
93 static int amr_setup_mbox(struct amr_softc *sc);
94 static int amr_ccb_map(struct amr_softc *sc);
96 static u_int amr_force_sg32 = 0;
98 SYSCTL_UINT(_hw_amr, OID_AUTO, force_sg32, CTLFLAG_RDTUN, &amr_force_sg32, 0,
99 "Force the AMR driver to use 32bit scatter gather");
101 static device_method_t amr_methods[] = {
102 /* Device interface */
103 DEVMETHOD(device_probe, amr_pci_probe),
104 DEVMETHOD(device_attach, amr_pci_attach),
105 DEVMETHOD(device_detach, amr_pci_detach),
106 DEVMETHOD(device_shutdown, amr_pci_shutdown),
107 DEVMETHOD(device_suspend, amr_pci_suspend),
108 DEVMETHOD(device_resume, amr_pci_resume),
113 static driver_t amr_pci_driver = {
116 sizeof(struct amr_softc)
119 static devclass_t amr_devclass;
120 DRIVER_MODULE(amr, pci, amr_pci_driver, amr_devclass, 0, 0);
121 MODULE_DEPEND(amr, pci, 1, 1, 1);
122 MODULE_DEPEND(amr, cam, 1, 1, 1);
124 static struct amr_ident
129 #define AMR_ID_PROBE_SIG (1<<0) /* generic i960RD, check signature */
130 #define AMR_ID_DO_SG64 (1<<1)
131 #define AMR_ID_QUARTZ (1<<2)
132 } amr_device_ids[] = {
135 {0x8086, 0x1960, AMR_ID_QUARTZ | AMR_ID_PROBE_SIG},
136 {0x101e, 0x1960, AMR_ID_QUARTZ},
137 {0x1000, 0x1960, AMR_ID_QUARTZ | AMR_ID_DO_SG64 | AMR_ID_PROBE_SIG},
138 {0x1000, 0x0407, AMR_ID_QUARTZ | AMR_ID_DO_SG64},
139 {0x1000, 0x0408, AMR_ID_QUARTZ | AMR_ID_DO_SG64},
140 {0x1000, 0x0409, AMR_ID_QUARTZ | AMR_ID_DO_SG64},
141 {0x1028, 0x000e, AMR_ID_QUARTZ | AMR_ID_DO_SG64 | AMR_ID_PROBE_SIG}, /* perc4/di i960 */
142 {0x1028, 0x000f, AMR_ID_QUARTZ | AMR_ID_DO_SG64}, /* perc4/di Verde*/
143 {0x1028, 0x0013, AMR_ID_QUARTZ | AMR_ID_DO_SG64}, /* perc4/di */
147 static struct amr_ident *
148 amr_find_ident(device_t dev)
150 struct amr_ident *id;
153 for (id = amr_device_ids; id->vendor != 0; id++) {
154 if ((pci_get_vendor(dev) == id->vendor) &&
155 (pci_get_device(dev) == id->device)) {
157 /* do we need to test for a signature? */
158 if (id->flags & AMR_ID_PROBE_SIG) {
159 sig = pci_read_config(dev, AMR_CFG_SIG, 2);
160 if ((sig != AMR_SIGNATURE_1) && (sig != AMR_SIGNATURE_2))
170 amr_pci_probe(device_t dev)
175 if (amr_find_ident(dev) != NULL) {
176 device_set_desc(dev, LSI_DESC_PCI);
177 return(BUS_PROBE_DEFAULT);
183 amr_pci_attach(device_t dev)
185 struct amr_softc *sc;
186 struct amr_ident *id;
187 int rid, rtype, error;
194 sc = device_get_softc(dev);
195 bzero(sc, sizeof(*sc));
198 /* assume failure is 'not configured' */
202 * Determine board type.
204 if ((id = amr_find_ident(dev)) == NULL)
207 if (id->flags & AMR_ID_QUARTZ) {
208 sc->amr_type |= AMR_TYPE_QUARTZ;
211 if ((amr_force_sg32 == 0) && (id->flags & AMR_ID_DO_SG64) &&
212 (sizeof(vm_paddr_t) > 4)) {
213 device_printf(dev, "Using 64-bit DMA\n");
214 sc->amr_type |= AMR_TYPE_SG64;
217 /* force the busmaster enable bit on */
218 pci_enable_busmaster(dev);
221 * Allocate the PCI register window.
224 rtype = AMR_IS_QUARTZ(sc) ? SYS_RES_MEMORY : SYS_RES_IOPORT;
225 sc->amr_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
226 if (sc->amr_reg == NULL) {
227 device_printf(sc->amr_dev, "can't allocate register window\n");
230 sc->amr_btag = rman_get_bustag(sc->amr_reg);
231 sc->amr_bhandle = rman_get_bushandle(sc->amr_reg);
234 * Allocate and connect our interrupt.
237 sc->amr_irq = bus_alloc_resource_any(sc->amr_dev, SYS_RES_IRQ, &rid,
238 RF_SHAREABLE | RF_ACTIVE);
239 if (sc->amr_irq == NULL) {
240 device_printf(sc->amr_dev, "can't allocate interrupt\n");
243 if (bus_setup_intr(sc->amr_dev, sc->amr_irq,
244 INTR_TYPE_BIO | INTR_ENTROPY | INTR_MPSAFE, NULL, amr_pci_intr,
245 sc, &sc->amr_intr)) {
246 device_printf(sc->amr_dev, "can't set up interrupt\n");
250 debug(2, "interrupt attached");
252 /* assume failure is 'out of memory' */
256 * Allocate the parent bus DMA tag appropriate for PCI.
258 if (bus_dma_tag_create(bus_get_dma_tag(dev), /* PCI parent */
259 1, 0, /* alignment,boundary */
262 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
263 BUS_SPACE_MAXADDR, /* highaddr */
264 NULL, NULL, /* filter, filterarg */
265 BUS_SPACE_MAXSIZE, /* maxsize */
266 BUS_SPACE_UNRESTRICTED, /* nsegments */
267 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
269 NULL, NULL, /* lockfunc, lockarg */
270 &sc->amr_parent_dmat)) {
271 device_printf(dev, "can't allocate parent DMA tag\n");
276 * Create DMA tag for mapping buffers into controller-addressable space.
278 if (bus_dma_tag_create(sc->amr_parent_dmat, /* parent */
279 1, 0, /* alignment,boundary */
280 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
281 BUS_SPACE_MAXADDR, /* highaddr */
282 NULL, NULL, /* filter, filterarg */
283 DFLTPHYS, /* maxsize */
284 AMR_NSEG, /* nsegments */
285 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
287 busdma_lock_mutex, /* lockfunc */
288 &sc->amr_list_lock, /* lockarg */
289 &sc->amr_buffer_dmat)) {
290 device_printf(sc->amr_dev, "can't allocate buffer DMA tag\n");
294 if (bus_dma_tag_create(sc->amr_parent_dmat, /* parent */
295 1, 0, /* alignment,boundary */
296 BUS_SPACE_MAXADDR, /* lowaddr */
297 BUS_SPACE_MAXADDR, /* highaddr */
298 NULL, NULL, /* filter, filterarg */
299 DFLTPHYS, /* maxsize */
300 AMR_NSEG, /* nsegments */
301 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
303 busdma_lock_mutex, /* lockfunc */
304 &sc->amr_list_lock, /* lockarg */
305 &sc->amr_buffer64_dmat)) {
306 device_printf(sc->amr_dev, "can't allocate buffer DMA tag\n");
310 debug(2, "dma tag done");
313 * Allocate and set up mailbox in a bus-visible fashion.
315 mtx_init(&sc->amr_list_lock, "AMR List Lock", NULL, MTX_DEF);
316 mtx_init(&sc->amr_hw_lock, "AMR HW Lock", NULL, MTX_DEF);
317 if ((error = amr_setup_mbox(sc)) != 0)
320 debug(2, "mailbox setup");
323 * Build the scatter/gather buffers.
325 if ((error = amr_sglist_map(sc)) != 0)
327 debug(2, "s/g list mapped");
329 if ((error = amr_ccb_map(sc)) != 0)
331 debug(2, "ccb mapped");
335 * Do bus-independant initialisation, bring controller online.
337 error = amr_attach(sc);
345 /********************************************************************************
346 * Disconnect from the controller completely, in preparation for unload.
349 amr_pci_detach(device_t dev)
351 struct amr_softc *sc = device_get_softc(dev);
356 if (sc->amr_state & AMR_STATE_OPEN)
359 if ((error = amr_pci_shutdown(dev)))
367 /********************************************************************************
368 * Bring the controller down to a dormant state and detach all child devices.
370 * This function is called before detach, system shutdown, or before performing
371 * an operation which may add or delete system disks. (Call amr_startup to
372 * resume normal operation.)
374 * Note that we can assume that the bioq on the controller is empty, as we won't
375 * allow shutdown if any device is open.
378 amr_pci_shutdown(device_t dev)
380 struct amr_softc *sc = device_get_softc(dev);
385 /* mark ourselves as in-shutdown */
386 sc->amr_state |= AMR_STATE_SHUTDOWN;
389 /* flush controller */
390 device_printf(sc->amr_dev, "flushing cache...");
391 printf("%s\n", amr_flush(sc) ? "failed" : "done");
395 /* delete all our child devices */
396 for(i = 0 ; i < AMR_MAXLD; i++) {
397 if( sc->amr_drive[i].al_disk != 0) {
398 if((error = device_delete_child(sc->amr_dev,sc->amr_drive[i].al_disk)) != 0)
400 sc->amr_drive[i].al_disk = 0;
404 /* XXX disable interrupts? */
410 /********************************************************************************
411 * Bring the controller to a quiescent state, ready for system suspend.
414 amr_pci_suspend(device_t dev)
416 struct amr_softc *sc = device_get_softc(dev);
420 sc->amr_state |= AMR_STATE_SUSPEND;
422 /* flush controller */
423 device_printf(sc->amr_dev, "flushing cache...");
424 printf("%s\n", amr_flush(sc) ? "failed" : "done");
426 /* XXX disable interrupts? */
431 /********************************************************************************
432 * Bring the controller back to a state ready for operation.
435 amr_pci_resume(device_t dev)
437 struct amr_softc *sc = device_get_softc(dev);
441 sc->amr_state &= ~AMR_STATE_SUSPEND;
443 /* XXX enable interrupts? */
448 /*******************************************************************************
449 * Take an interrupt, or be poked by other code to look for interrupt-worthy
453 amr_pci_intr(void *arg)
455 struct amr_softc *sc = (struct amr_softc *)arg;
459 /* collect finished commands, queue anything waiting */
463 /********************************************************************************
464 * Free all of the resources associated with (sc)
466 * Should not be called if the controller is active.
469 amr_pci_free(struct amr_softc *sc)
477 /* destroy data-transfer DMA tag */
478 if (sc->amr_buffer_dmat)
479 bus_dma_tag_destroy(sc->amr_buffer_dmat);
480 if (sc->amr_buffer64_dmat)
481 bus_dma_tag_destroy(sc->amr_buffer64_dmat);
483 /* free and destroy DMA memory and tag for passthrough pool */
485 bus_dmamap_unload(sc->amr_ccb_dmat, sc->amr_ccb_dmamap);
486 bus_dmamem_free(sc->amr_ccb_dmat, sc->amr_ccb, sc->amr_ccb_dmamap);
488 if (sc->amr_ccb_dmat)
489 bus_dma_tag_destroy(sc->amr_ccb_dmat);
491 /* free and destroy DMA memory and tag for s/g lists */
492 if (sc->amr_sgtable) {
493 bus_dmamap_unload(sc->amr_sg_dmat, sc->amr_sg_dmamap);
494 bus_dmamem_free(sc->amr_sg_dmat, sc->amr_sgtable, sc->amr_sg_dmamap);
497 bus_dma_tag_destroy(sc->amr_sg_dmat);
499 /* free and destroy DMA memory and tag for mailbox */
500 p = (void *)(uintptr_t)(volatile void *)sc->amr_mailbox64;
501 if (sc->amr_mailbox) {
502 bus_dmamap_unload(sc->amr_mailbox_dmat, sc->amr_mailbox_dmamap);
503 bus_dmamem_free(sc->amr_mailbox_dmat, p, sc->amr_mailbox_dmamap);
505 if (sc->amr_mailbox_dmat)
506 bus_dma_tag_destroy(sc->amr_mailbox_dmat);
508 /* disconnect the interrupt handler */
510 bus_teardown_intr(sc->amr_dev, sc->amr_irq, sc->amr_intr);
511 if (sc->amr_irq != NULL)
512 bus_release_resource(sc->amr_dev, SYS_RES_IRQ, 0, sc->amr_irq);
514 /* destroy the parent DMA tag */
515 if (sc->amr_parent_dmat)
516 bus_dma_tag_destroy(sc->amr_parent_dmat);
518 /* release the register window mapping */
519 if (sc->amr_reg != NULL)
520 bus_release_resource(sc->amr_dev,
521 AMR_IS_QUARTZ(sc) ? SYS_RES_MEMORY : SYS_RES_IOPORT,
522 PCIR_BAR(0), sc->amr_reg);
525 /********************************************************************************
526 * Allocate and map the scatter/gather table in bus space.
529 amr_sglist_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
536 *addr = segs[0].ds_addr;
540 amr_sglist_map(struct amr_softc *sc)
549 * Create a single tag describing a region large enough to hold all of
550 * the s/g lists we will need.
552 * Note that we could probably use AMR_LIMITCMD here, but that may become
556 segsize = sizeof(struct amr_sg64entry) * AMR_NSEG * AMR_MAXCMD;
558 segsize = sizeof(struct amr_sgentry) * AMR_NSEG * AMR_MAXCMD;
560 error = bus_dma_tag_create(sc->amr_parent_dmat, /* parent */
561 512, 0, /* alignment,boundary */
562 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
563 BUS_SPACE_MAXADDR, /* highaddr */
564 NULL, NULL, /* filter, filterarg */
565 segsize, 1, /* maxsize, nsegments */
566 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
568 NULL, NULL, /* lockfunc, lockarg */
571 device_printf(sc->amr_dev, "can't allocate scatter/gather DMA tag\n");
576 * Allocate enough s/g maps for all commands and permanently map them into
577 * controller-visible space.
579 * XXX this assumes we can get enough space for all the s/g maps in one
580 * contiguous slab. We may need to switch to a more complex arrangement
581 * where we allocate in smaller chunks and keep a lookup table from slot
584 * XXX HACK ALERT: at least some controllers don't like the s/g memory
585 * being allocated below 0x2000. We leak some memory if
586 * we get some below this mark and allocate again. We
587 * should be able to avoid this with the tag setup, but
588 * that does't seem to work.
591 error = bus_dmamem_alloc(sc->amr_sg_dmat, (void **)&p, BUS_DMA_NOWAIT, &sc->amr_sg_dmamap);
593 device_printf(sc->amr_dev, "can't allocate s/g table\n");
596 bus_dmamap_load(sc->amr_sg_dmat, sc->amr_sg_dmamap, p, segsize, amr_sglist_helper, &sc->amr_sgbusaddr, 0);
597 if (sc->amr_sgbusaddr < 0x2000) {
598 debug(1, "s/g table too low (0x%x), reallocating\n", sc->amr_sgbusaddr);
603 sc->amr_sg64table = (struct amr_sg64entry *)p;
604 sc->amr_sgtable = (struct amr_sgentry *)p;
609 /********************************************************************************
610 * Allocate and set up mailbox areas for the controller (sc)
612 * The basic mailbox structure should be 16-byte aligned.
615 amr_setup_mbox(struct amr_softc *sc)
624 * Create a single tag describing a region large enough to hold the entire
627 error = bus_dma_tag_create(sc->amr_parent_dmat, /* parent */
628 16, 0, /* alignment,boundary */
629 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
630 BUS_SPACE_MAXADDR, /* highaddr */
631 NULL, NULL, /* filter, filterarg */
632 sizeof(struct amr_mailbox64), /* maxsize */
634 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
636 NULL, NULL, /* lockfunc, lockarg */
637 &sc->amr_mailbox_dmat);
639 device_printf(sc->amr_dev, "can't allocate mailbox tag\n");
644 * Allocate the mailbox structure and permanently map it into
645 * controller-visible space.
647 error = bus_dmamem_alloc(sc->amr_mailbox_dmat, (void **)&p, BUS_DMA_NOWAIT,
648 &sc->amr_mailbox_dmamap);
650 device_printf(sc->amr_dev, "can't allocate mailbox memory\n");
653 bus_dmamap_load(sc->amr_mailbox_dmat, sc->amr_mailbox_dmamap, p,
654 sizeof(struct amr_mailbox64), amr_sglist_helper, &baddr, 0);
656 * Conventional mailbox is inside the mailbox64 region.
658 /* save physical base of the basic mailbox structure */
659 sc->amr_mailboxphys = baddr + offsetof(struct amr_mailbox64, mb);
660 bzero(p, sizeof(struct amr_mailbox64));
661 sc->amr_mailbox64 = (struct amr_mailbox64 *)p;
662 sc->amr_mailbox = &sc->amr_mailbox64->mb;
668 amr_ccb_map(struct amr_softc *sc)
673 * Passthrough and Extended passthrough structures will share the same
676 ccbsize = sizeof(union amr_ccb) * AMR_MAXCMD;
677 error = bus_dma_tag_create(sc->amr_parent_dmat, /* parent */
678 128, 0, /* alignment,boundary */
679 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
680 BUS_SPACE_MAXADDR, /* highaddr */
681 NULL, NULL, /* filter, filterarg */
682 ccbsize, /* maxsize */
684 ccbsize, /* maxsegsize */
686 NULL, NULL, /* lockfunc, lockarg */
689 device_printf(sc->amr_dev, "can't allocate ccb tag\n");
693 error = bus_dmamem_alloc(sc->amr_ccb_dmat, (void **)&sc->amr_ccb,
694 BUS_DMA_NOWAIT, &sc->amr_ccb_dmamap);
696 device_printf(sc->amr_dev, "can't allocate ccb memory\n");
699 bus_dmamap_load(sc->amr_ccb_dmat, sc->amr_ccb_dmamap, sc->amr_ccb,
700 ccbsize, amr_sglist_helper, &sc->amr_ccb_busaddr, 0);
701 bzero(sc->amr_ccb, ccbsize);