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 struct amr_ident
124 #define AMR_ID_PROBE_SIG (1<<0) /* generic i960RD, check signature */
125 #define AMR_ID_DO_SG64 (1<<1)
126 #define AMR_ID_QUARTZ (1<<2)
127 } amr_device_ids[] = {
130 {0x8086, 0x1960, AMR_ID_QUARTZ | AMR_ID_PROBE_SIG},
131 {0x101e, 0x1960, AMR_ID_QUARTZ},
132 {0x1000, 0x1960, AMR_ID_QUARTZ | AMR_ID_DO_SG64 | AMR_ID_PROBE_SIG},
133 {0x1000, 0x0407, AMR_ID_QUARTZ | AMR_ID_DO_SG64},
134 {0x1000, 0x0408, AMR_ID_QUARTZ | AMR_ID_DO_SG64},
135 {0x1000, 0x0409, AMR_ID_QUARTZ | AMR_ID_DO_SG64},
136 {0x1028, 0x000e, AMR_ID_QUARTZ | AMR_ID_DO_SG64 | AMR_ID_PROBE_SIG}, /* perc4/di i960 */
137 {0x1028, 0x000f, AMR_ID_QUARTZ | AMR_ID_DO_SG64}, /* perc4/di Verde*/
138 {0x1028, 0x0013, AMR_ID_QUARTZ | AMR_ID_DO_SG64}, /* perc4/di */
142 static devclass_t amr_devclass;
143 DRIVER_MODULE(amr, pci, amr_pci_driver, amr_devclass, 0, 0);
144 MODULE_PNP_INFO("U16:vendor;U16:device", pci, amr, amr_device_ids,
145 nitems(amr_device_ids) - 1);
146 MODULE_DEPEND(amr, pci, 1, 1, 1);
147 MODULE_DEPEND(amr, cam, 1, 1, 1);
149 static struct amr_ident *
150 amr_find_ident(device_t dev)
152 struct amr_ident *id;
155 for (id = amr_device_ids; id->vendor != 0; id++) {
156 if ((pci_get_vendor(dev) == id->vendor) &&
157 (pci_get_device(dev) == id->device)) {
159 /* do we need to test for a signature? */
160 if (id->flags & AMR_ID_PROBE_SIG) {
161 sig = pci_read_config(dev, AMR_CFG_SIG, 2);
162 if ((sig != AMR_SIGNATURE_1) && (sig != AMR_SIGNATURE_2))
172 amr_pci_probe(device_t dev)
177 if (amr_find_ident(dev) != NULL) {
178 device_set_desc(dev, LSI_DESC_PCI);
179 return(BUS_PROBE_DEFAULT);
185 amr_pci_attach(device_t dev)
187 struct amr_softc *sc;
188 struct amr_ident *id;
189 int rid, rtype, error;
196 sc = device_get_softc(dev);
197 bzero(sc, sizeof(*sc));
200 /* assume failure is 'not configured' */
204 * Determine board type.
206 if ((id = amr_find_ident(dev)) == NULL)
209 if (id->flags & AMR_ID_QUARTZ) {
210 sc->amr_type |= AMR_TYPE_QUARTZ;
213 if ((amr_force_sg32 == 0) && (id->flags & AMR_ID_DO_SG64) &&
214 (sizeof(vm_paddr_t) > 4)) {
215 device_printf(dev, "Using 64-bit DMA\n");
216 sc->amr_type |= AMR_TYPE_SG64;
219 /* force the busmaster enable bit on */
220 pci_enable_busmaster(dev);
223 * Allocate the PCI register window.
226 rtype = AMR_IS_QUARTZ(sc) ? SYS_RES_MEMORY : SYS_RES_IOPORT;
227 sc->amr_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
228 if (sc->amr_reg == NULL) {
229 device_printf(sc->amr_dev, "can't allocate register window\n");
232 sc->amr_btag = rman_get_bustag(sc->amr_reg);
233 sc->amr_bhandle = rman_get_bushandle(sc->amr_reg);
236 * Allocate and connect our interrupt.
239 sc->amr_irq = bus_alloc_resource_any(sc->amr_dev, SYS_RES_IRQ, &rid,
240 RF_SHAREABLE | RF_ACTIVE);
241 if (sc->amr_irq == NULL) {
242 device_printf(sc->amr_dev, "can't allocate interrupt\n");
245 if (bus_setup_intr(sc->amr_dev, sc->amr_irq,
246 INTR_TYPE_BIO | INTR_ENTROPY | INTR_MPSAFE, NULL, amr_pci_intr,
247 sc, &sc->amr_intr)) {
248 device_printf(sc->amr_dev, "can't set up interrupt\n");
252 debug(2, "interrupt attached");
254 /* assume failure is 'out of memory' */
258 * Allocate the parent bus DMA tag appropriate for PCI.
260 if (bus_dma_tag_create(bus_get_dma_tag(dev), /* PCI parent */
261 1, 0, /* alignment,boundary */
264 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
265 BUS_SPACE_MAXADDR, /* highaddr */
266 NULL, NULL, /* filter, filterarg */
267 BUS_SPACE_MAXSIZE, /* maxsize */
268 BUS_SPACE_UNRESTRICTED, /* nsegments */
269 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
271 NULL, NULL, /* lockfunc, lockarg */
272 &sc->amr_parent_dmat)) {
273 device_printf(dev, "can't allocate parent DMA tag\n");
278 * Create DMA tag for mapping buffers into controller-addressable space.
280 if (bus_dma_tag_create(sc->amr_parent_dmat, /* parent */
281 1, 0, /* alignment,boundary */
282 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
283 BUS_SPACE_MAXADDR, /* highaddr */
284 NULL, NULL, /* filter, filterarg */
285 DFLTPHYS, /* maxsize */
286 AMR_NSEG, /* nsegments */
287 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
289 busdma_lock_mutex, /* lockfunc */
290 &sc->amr_list_lock, /* lockarg */
291 &sc->amr_buffer_dmat)) {
292 device_printf(sc->amr_dev, "can't allocate buffer DMA tag\n");
296 if (bus_dma_tag_create(sc->amr_parent_dmat, /* parent */
297 1, 0, /* alignment,boundary */
298 BUS_SPACE_MAXADDR, /* lowaddr */
299 BUS_SPACE_MAXADDR, /* highaddr */
300 NULL, NULL, /* filter, filterarg */
301 DFLTPHYS, /* maxsize */
302 AMR_NSEG, /* nsegments */
303 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
305 busdma_lock_mutex, /* lockfunc */
306 &sc->amr_list_lock, /* lockarg */
307 &sc->amr_buffer64_dmat)) {
308 device_printf(sc->amr_dev, "can't allocate buffer DMA tag\n");
312 debug(2, "dma tag done");
315 * Allocate and set up mailbox in a bus-visible fashion.
317 mtx_init(&sc->amr_list_lock, "AMR List Lock", NULL, MTX_DEF);
318 mtx_init(&sc->amr_hw_lock, "AMR HW Lock", NULL, MTX_DEF);
319 if ((error = amr_setup_mbox(sc)) != 0)
322 debug(2, "mailbox setup");
325 * Build the scatter/gather buffers.
327 if ((error = amr_sglist_map(sc)) != 0)
329 debug(2, "s/g list mapped");
331 if ((error = amr_ccb_map(sc)) != 0)
333 debug(2, "ccb mapped");
337 * Do bus-independant initialisation, bring controller online.
339 error = amr_attach(sc);
347 /********************************************************************************
348 * Disconnect from the controller completely, in preparation for unload.
351 amr_pci_detach(device_t dev)
353 struct amr_softc *sc = device_get_softc(dev);
358 if (sc->amr_state & AMR_STATE_OPEN)
361 if ((error = amr_pci_shutdown(dev)))
369 /********************************************************************************
370 * Bring the controller down to a dormant state and detach all child devices.
372 * This function is called before detach, system shutdown, or before performing
373 * an operation which may add or delete system disks. (Call amr_startup to
374 * resume normal operation.)
376 * Note that we can assume that the bioq on the controller is empty, as we won't
377 * allow shutdown if any device is open.
380 amr_pci_shutdown(device_t dev)
382 struct amr_softc *sc = device_get_softc(dev);
387 /* mark ourselves as in-shutdown */
388 sc->amr_state |= AMR_STATE_SHUTDOWN;
391 /* flush controller */
392 device_printf(sc->amr_dev, "flushing cache...");
393 printf("%s\n", amr_flush(sc) ? "failed" : "done");
397 /* delete all our child devices */
398 for(i = 0 ; i < AMR_MAXLD; i++) {
399 if( sc->amr_drive[i].al_disk != 0) {
400 if((error = device_delete_child(sc->amr_dev,sc->amr_drive[i].al_disk)) != 0)
402 sc->amr_drive[i].al_disk = 0;
406 /* XXX disable interrupts? */
412 /********************************************************************************
413 * Bring the controller to a quiescent state, ready for system suspend.
416 amr_pci_suspend(device_t dev)
418 struct amr_softc *sc = device_get_softc(dev);
422 sc->amr_state |= AMR_STATE_SUSPEND;
424 /* flush controller */
425 device_printf(sc->amr_dev, "flushing cache...");
426 printf("%s\n", amr_flush(sc) ? "failed" : "done");
428 /* XXX disable interrupts? */
433 /********************************************************************************
434 * Bring the controller back to a state ready for operation.
437 amr_pci_resume(device_t dev)
439 struct amr_softc *sc = device_get_softc(dev);
443 sc->amr_state &= ~AMR_STATE_SUSPEND;
445 /* XXX enable interrupts? */
450 /*******************************************************************************
451 * Take an interrupt, or be poked by other code to look for interrupt-worthy
455 amr_pci_intr(void *arg)
457 struct amr_softc *sc = (struct amr_softc *)arg;
461 /* collect finished commands, queue anything waiting */
465 /********************************************************************************
466 * Free all of the resources associated with (sc)
468 * Should not be called if the controller is active.
471 amr_pci_free(struct amr_softc *sc)
479 /* destroy data-transfer DMA tag */
480 if (sc->amr_buffer_dmat)
481 bus_dma_tag_destroy(sc->amr_buffer_dmat);
482 if (sc->amr_buffer64_dmat)
483 bus_dma_tag_destroy(sc->amr_buffer64_dmat);
485 /* free and destroy DMA memory and tag for passthrough pool */
487 bus_dmamap_unload(sc->amr_ccb_dmat, sc->amr_ccb_dmamap);
488 bus_dmamem_free(sc->amr_ccb_dmat, sc->amr_ccb, sc->amr_ccb_dmamap);
490 if (sc->amr_ccb_dmat)
491 bus_dma_tag_destroy(sc->amr_ccb_dmat);
493 /* free and destroy DMA memory and tag for s/g lists */
494 if (sc->amr_sgtable) {
495 bus_dmamap_unload(sc->amr_sg_dmat, sc->amr_sg_dmamap);
496 bus_dmamem_free(sc->amr_sg_dmat, sc->amr_sgtable, sc->amr_sg_dmamap);
499 bus_dma_tag_destroy(sc->amr_sg_dmat);
501 /* free and destroy DMA memory and tag for mailbox */
502 p = (void *)(uintptr_t)(volatile void *)sc->amr_mailbox64;
503 if (sc->amr_mailbox) {
504 bus_dmamap_unload(sc->amr_mailbox_dmat, sc->amr_mailbox_dmamap);
505 bus_dmamem_free(sc->amr_mailbox_dmat, p, sc->amr_mailbox_dmamap);
507 if (sc->amr_mailbox_dmat)
508 bus_dma_tag_destroy(sc->amr_mailbox_dmat);
510 /* disconnect the interrupt handler */
512 bus_teardown_intr(sc->amr_dev, sc->amr_irq, sc->amr_intr);
513 if (sc->amr_irq != NULL)
514 bus_release_resource(sc->amr_dev, SYS_RES_IRQ, 0, sc->amr_irq);
516 /* destroy the parent DMA tag */
517 if (sc->amr_parent_dmat)
518 bus_dma_tag_destroy(sc->amr_parent_dmat);
520 /* release the register window mapping */
521 if (sc->amr_reg != NULL)
522 bus_release_resource(sc->amr_dev,
523 AMR_IS_QUARTZ(sc) ? SYS_RES_MEMORY : SYS_RES_IOPORT,
524 PCIR_BAR(0), sc->amr_reg);
527 /********************************************************************************
528 * Allocate and map the scatter/gather table in bus space.
531 amr_sglist_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
538 *addr = segs[0].ds_addr;
542 amr_sglist_map(struct amr_softc *sc)
551 * Create a single tag describing a region large enough to hold all of
552 * the s/g lists we will need.
554 * Note that we could probably use AMR_LIMITCMD here, but that may become
558 segsize = sizeof(struct amr_sg64entry) * AMR_NSEG * AMR_MAXCMD;
560 segsize = sizeof(struct amr_sgentry) * AMR_NSEG * AMR_MAXCMD;
562 error = bus_dma_tag_create(sc->amr_parent_dmat, /* parent */
563 512, 0, /* alignment,boundary */
564 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
565 BUS_SPACE_MAXADDR, /* highaddr */
566 NULL, NULL, /* filter, filterarg */
567 segsize, 1, /* maxsize, nsegments */
568 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
570 NULL, NULL, /* lockfunc, lockarg */
573 device_printf(sc->amr_dev, "can't allocate scatter/gather DMA tag\n");
578 * Allocate enough s/g maps for all commands and permanently map them into
579 * controller-visible space.
581 * XXX this assumes we can get enough space for all the s/g maps in one
582 * contiguous slab. We may need to switch to a more complex arrangement
583 * where we allocate in smaller chunks and keep a lookup table from slot
586 * XXX HACK ALERT: at least some controllers don't like the s/g memory
587 * being allocated below 0x2000. We leak some memory if
588 * we get some below this mark and allocate again. We
589 * should be able to avoid this with the tag setup, but
590 * that does't seem to work.
593 error = bus_dmamem_alloc(sc->amr_sg_dmat, (void **)&p, BUS_DMA_NOWAIT, &sc->amr_sg_dmamap);
595 device_printf(sc->amr_dev, "can't allocate s/g table\n");
598 bus_dmamap_load(sc->amr_sg_dmat, sc->amr_sg_dmamap, p, segsize, amr_sglist_helper, &sc->amr_sgbusaddr, 0);
599 if (sc->amr_sgbusaddr < 0x2000) {
600 debug(1, "s/g table too low (0x%x), reallocating\n", sc->amr_sgbusaddr);
605 sc->amr_sg64table = (struct amr_sg64entry *)p;
606 sc->amr_sgtable = (struct amr_sgentry *)p;
611 /********************************************************************************
612 * Allocate and set up mailbox areas for the controller (sc)
614 * The basic mailbox structure should be 16-byte aligned.
617 amr_setup_mbox(struct amr_softc *sc)
626 * Create a single tag describing a region large enough to hold the entire
629 error = bus_dma_tag_create(sc->amr_parent_dmat, /* parent */
630 16, 0, /* alignment,boundary */
631 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
632 BUS_SPACE_MAXADDR, /* highaddr */
633 NULL, NULL, /* filter, filterarg */
634 sizeof(struct amr_mailbox64), /* maxsize */
636 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
638 NULL, NULL, /* lockfunc, lockarg */
639 &sc->amr_mailbox_dmat);
641 device_printf(sc->amr_dev, "can't allocate mailbox tag\n");
646 * Allocate the mailbox structure and permanently map it into
647 * controller-visible space.
649 error = bus_dmamem_alloc(sc->amr_mailbox_dmat, (void **)&p, BUS_DMA_NOWAIT,
650 &sc->amr_mailbox_dmamap);
652 device_printf(sc->amr_dev, "can't allocate mailbox memory\n");
655 bus_dmamap_load(sc->amr_mailbox_dmat, sc->amr_mailbox_dmamap, p,
656 sizeof(struct amr_mailbox64), amr_sglist_helper, &baddr, 0);
658 * Conventional mailbox is inside the mailbox64 region.
660 /* save physical base of the basic mailbox structure */
661 sc->amr_mailboxphys = baddr + offsetof(struct amr_mailbox64, mb);
662 bzero(p, sizeof(struct amr_mailbox64));
663 sc->amr_mailbox64 = (struct amr_mailbox64 *)p;
664 sc->amr_mailbox = &sc->amr_mailbox64->mb;
670 amr_ccb_map(struct amr_softc *sc)
675 * Passthrough and Extended passthrough structures will share the same
678 ccbsize = sizeof(union amr_ccb) * AMR_MAXCMD;
679 error = bus_dma_tag_create(sc->amr_parent_dmat, /* parent */
680 128, 0, /* alignment,boundary */
681 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
682 BUS_SPACE_MAXADDR, /* highaddr */
683 NULL, NULL, /* filter, filterarg */
684 ccbsize, /* maxsize */
686 ccbsize, /* maxsegsize */
688 NULL, NULL, /* lockfunc, lockarg */
691 device_printf(sc->amr_dev, "can't allocate ccb tag\n");
695 error = bus_dmamem_alloc(sc->amr_ccb_dmat, (void **)&sc->amr_ccb,
696 BUS_DMA_NOWAIT, &sc->amr_ccb_dmamap);
698 device_printf(sc->amr_dev, "can't allocate ccb memory\n");
701 bus_dmamap_load(sc->amr_ccb_dmat, sc->amr_ccb_dmamap, sc->amr_ccb,
702 ccbsize, amr_sglist_helper, &sc->amr_ccb_busaddr, 0);
703 bzero(sc->amr_ccb, ccbsize);