]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/amr/amr_pci.c
MFH r338661 through r339200.
[FreeBSD/FreeBSD.git] / sys / dev / 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 /*-
28  * SPDX-License-Identifier: BSD-3-Clause
29  *
30  * Copyright (c) 2002 Eric Moore
31  * Copyright (c) 2002, 2004 LSI Logic Corporation
32  * All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions
36  * are met:
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
44  *    herein.
45  *
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
56  * SUCH DAMAGE.
57  */
58
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
61
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>
67
68 #include <sys/bio.h>
69 #include <sys/bus.h>
70 #include <sys/conf.h>
71
72 #include <machine/bus.h>
73 #include <machine/resource.h>
74 #include <sys/rman.h>
75
76 #include <dev/pci/pcireg.h>
77 #include <dev/pci/pcivar.h>
78
79 #include <dev/amr/amrio.h>
80 #include <dev/amr/amrreg.h>
81 #include <dev/amr/amrvar.h>
82
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);
95
96 static u_int amr_force_sg32 = 0;
97 SYSCTL_DECL(_hw_amr);
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");
100
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),
109
110     DEVMETHOD_END
111 };
112
113 static driver_t amr_pci_driver = {
114         "amr",
115         amr_methods,
116         sizeof(struct amr_softc)
117 };
118
119 static struct amr_ident
120 {
121     uint16_t            vendor;
122     uint16_t            device;
123     int         flags;
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[] = {
128     {0x101e, 0x9010, 0},
129     {0x101e, 0x9060, 0},
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 */
139     {0, 0, 0}
140 };
141
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);
148
149 static struct amr_ident *
150 amr_find_ident(device_t dev)
151 {
152     struct amr_ident *id;
153     int sig;
154
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)) {
158
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))
163                     continue;
164             }
165             return (id);
166         }
167     }
168     return (NULL);
169 }
170         
171 static int
172 amr_pci_probe(device_t dev)
173 {
174
175     debug_called(1);
176
177     if (amr_find_ident(dev) != NULL) {
178         device_set_desc(dev, LSI_DESC_PCI);
179         return(BUS_PROBE_DEFAULT);
180     }
181     return(ENXIO);
182 }
183
184 static int
185 amr_pci_attach(device_t dev)
186 {
187     struct amr_softc    *sc;
188     struct amr_ident    *id;
189     int                 rid, rtype, error;
190
191     debug_called(1);
192
193     /*
194      * Initialise softc.
195      */
196     sc = device_get_softc(dev);
197     bzero(sc, sizeof(*sc));
198     sc->amr_dev = dev;
199
200     /* assume failure is 'not configured' */
201     error = ENXIO;
202
203     /*
204      * Determine board type.
205      */
206     if ((id = amr_find_ident(dev)) == NULL)
207         return (ENXIO);
208
209     if (id->flags & AMR_ID_QUARTZ) {
210         sc->amr_type |= AMR_TYPE_QUARTZ;
211     }
212
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;
217     }
218
219     /* force the busmaster enable bit on */
220     pci_enable_busmaster(dev);
221
222     /*
223      * Allocate the PCI register window.
224      */
225     rid = PCIR_BAR(0);
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");
230         goto out;
231     }
232     sc->amr_btag = rman_get_bustag(sc->amr_reg);
233     sc->amr_bhandle = rman_get_bushandle(sc->amr_reg);
234
235     /*
236      * Allocate and connect our interrupt.
237      */
238     rid = 0;
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");
243         goto out;
244     }
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");
249         goto out;
250     }
251
252     debug(2, "interrupt attached");
253
254     /* assume failure is 'out of memory' */
255     error = ENOMEM;
256
257     /*
258      * Allocate the parent bus DMA tag appropriate for PCI.
259      */
260     if (bus_dma_tag_create(bus_get_dma_tag(dev),        /* PCI parent */
261                            1, 0,                        /* alignment,boundary */
262                            AMR_IS_SG64(sc) ?
263                            BUS_SPACE_MAXADDR :
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 */
270                            0,                           /* flags */
271                            NULL, NULL,                  /* lockfunc, lockarg */
272                            &sc->amr_parent_dmat)) {
273         device_printf(dev, "can't allocate parent DMA tag\n");
274         goto out;
275     }
276
277     /*
278      * Create DMA tag for mapping buffers into controller-addressable space.
279      */
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 */
288                            0,           /* flags */
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");
293         goto out;
294     }
295
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 */
304                            0,           /* flags */
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");
309         goto out;
310     }
311
312     debug(2, "dma tag done");
313
314     /*
315      * Allocate and set up mailbox in a bus-visible fashion.
316      */
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)
320         goto out;
321
322     debug(2, "mailbox setup");
323
324     /*
325      * Build the scatter/gather buffers.
326      */
327     if ((error = amr_sglist_map(sc)) != 0)
328         goto out;
329     debug(2, "s/g list mapped");
330
331     if ((error = amr_ccb_map(sc)) != 0)
332         goto out;
333     debug(2, "ccb mapped");
334
335
336     /*
337      * Do bus-independant initialisation, bring controller online.
338      */
339     error = amr_attach(sc);
340
341 out:
342     if (error)
343         amr_pci_free(sc);
344     return(error);
345 }
346
347 /********************************************************************************
348  * Disconnect from the controller completely, in preparation for unload.
349  */
350 static int
351 amr_pci_detach(device_t dev)
352 {
353     struct amr_softc    *sc = device_get_softc(dev);
354     int                 error;
355
356     debug_called(1);
357
358     if (sc->amr_state & AMR_STATE_OPEN)
359         return(EBUSY);
360
361     if ((error = amr_pci_shutdown(dev)))
362         return(error);
363
364     amr_pci_free(sc);
365
366     return(0);
367 }
368
369 /********************************************************************************
370  * Bring the controller down to a dormant state and detach all child devices.
371  *
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.)
375  *
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.
378  */
379 static int
380 amr_pci_shutdown(device_t dev)
381 {
382     struct amr_softc    *sc = device_get_softc(dev);
383     int                 i,error;
384
385     debug_called(1);
386
387     /* mark ourselves as in-shutdown */
388     sc->amr_state |= AMR_STATE_SHUTDOWN;
389
390
391     /* flush controller */
392     device_printf(sc->amr_dev, "flushing cache...");
393     printf("%s\n", amr_flush(sc) ? "failed" : "done");
394
395     error = 0;
396
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)
401                 goto shutdown_out;
402             sc->amr_drive[i].al_disk = 0;
403         }
404     }
405
406     /* XXX disable interrupts? */
407
408 shutdown_out:
409     return(error);
410 }
411
412 /********************************************************************************
413  * Bring the controller to a quiescent state, ready for system suspend.
414  */
415 static int
416 amr_pci_suspend(device_t dev)
417 {
418     struct amr_softc    *sc = device_get_softc(dev);
419
420     debug_called(1);
421
422     sc->amr_state |= AMR_STATE_SUSPEND;
423
424     /* flush controller */
425     device_printf(sc->amr_dev, "flushing cache...");
426     printf("%s\n", amr_flush(sc) ? "failed" : "done");
427     
428     /* XXX disable interrupts? */
429
430     return(0);
431 }
432
433 /********************************************************************************
434  * Bring the controller back to a state ready for operation.
435  */
436 static int
437 amr_pci_resume(device_t dev)
438 {
439     struct amr_softc    *sc = device_get_softc(dev);
440
441     debug_called(1);
442
443     sc->amr_state &= ~AMR_STATE_SUSPEND;
444
445     /* XXX enable interrupts? */
446
447     return(0);
448 }
449
450 /*******************************************************************************
451  * Take an interrupt, or be poked by other code to look for interrupt-worthy
452  * status.
453  */
454 static void
455 amr_pci_intr(void *arg)
456 {
457     struct amr_softc    *sc = (struct amr_softc *)arg;
458
459     debug_called(3);
460
461     /* collect finished commands, queue anything waiting */
462     amr_done(sc);
463 }
464
465 /********************************************************************************
466  * Free all of the resources associated with (sc)
467  *
468  * Should not be called if the controller is active.
469  */
470 static void
471 amr_pci_free(struct amr_softc *sc)
472 {
473     void *p;
474
475     debug_called(1);
476
477     amr_free(sc);
478
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);
484
485     /* free and destroy DMA memory and tag for passthrough pool */
486     if (sc->amr_ccb) {
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);
489     }
490     if (sc->amr_ccb_dmat)
491         bus_dma_tag_destroy(sc->amr_ccb_dmat);
492
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);
497     }
498     if (sc->amr_sg_dmat)
499         bus_dma_tag_destroy(sc->amr_sg_dmat);
500
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);
506     }
507     if (sc->amr_mailbox_dmat)
508         bus_dma_tag_destroy(sc->amr_mailbox_dmat);
509
510     /* disconnect the interrupt handler */
511     if (sc->amr_intr)
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);
515
516     /* destroy the parent DMA tag */
517     if (sc->amr_parent_dmat)
518         bus_dma_tag_destroy(sc->amr_parent_dmat);
519
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);
525 }
526
527 /********************************************************************************
528  * Allocate and map the scatter/gather table in bus space.
529  */
530 static void
531 amr_sglist_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
532 {
533     uint32_t *addr;
534
535     debug_called(1);
536
537     addr = arg;
538     *addr = segs[0].ds_addr;
539 }
540
541 static int
542 amr_sglist_map(struct amr_softc *sc)
543 {
544     size_t      segsize;
545     void        *p;
546     int         error;
547
548     debug_called(1);
549
550     /*
551      * Create a single tag describing a region large enough to hold all of
552      * the s/g lists we will need.
553      *
554      * Note that we could probably use AMR_LIMITCMD here, but that may become
555      * tunable.
556      */
557     if (AMR_IS_SG64(sc))
558         segsize = sizeof(struct amr_sg64entry) * AMR_NSEG * AMR_MAXCMD;
559     else
560         segsize = sizeof(struct amr_sgentry) * AMR_NSEG * AMR_MAXCMD;
561
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 */
569                                0,                       /* flags */
570                                NULL, NULL,              /* lockfunc, lockarg */
571                                &sc->amr_sg_dmat);
572     if (error != 0) {
573         device_printf(sc->amr_dev, "can't allocate scatter/gather DMA tag\n");
574         return(ENOMEM);
575     }
576
577     /*
578      * Allocate enough s/g maps for all commands and permanently map them into
579      * controller-visible space.
580      *  
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
584      * to bus address.
585      *
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.
591      */
592 retry:
593     error = bus_dmamem_alloc(sc->amr_sg_dmat, (void **)&p, BUS_DMA_NOWAIT, &sc->amr_sg_dmamap);
594     if (error) {
595         device_printf(sc->amr_dev, "can't allocate s/g table\n");
596         return(ENOMEM);
597     }
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);
601         goto retry;
602     }
603
604     if (AMR_IS_SG64(sc))
605         sc->amr_sg64table = (struct amr_sg64entry *)p;
606     sc->amr_sgtable = (struct amr_sgentry *)p;
607
608     return(0);
609 }
610
611 /********************************************************************************
612  * Allocate and set up mailbox areas for the controller (sc)
613  *
614  * The basic mailbox structure should be 16-byte aligned.
615  */
616 static int
617 amr_setup_mbox(struct amr_softc *sc)
618 {
619     int         error;
620     void        *p;
621     uint32_t    baddr;
622     
623     debug_called(1);
624
625     /*
626      * Create a single tag describing a region large enough to hold the entire
627      * mailbox.
628      */
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 */
635                                1,                       /* nsegments */
636                                BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
637                                0,                       /* flags */
638                                NULL, NULL,              /* lockfunc, lockarg */
639                                &sc->amr_mailbox_dmat);
640     if (error != 0) {
641         device_printf(sc->amr_dev, "can't allocate mailbox tag\n");
642         return(ENOMEM);
643     }
644
645     /*
646      * Allocate the mailbox structure and permanently map it into
647      * controller-visible space.
648      */
649     error = bus_dmamem_alloc(sc->amr_mailbox_dmat, (void **)&p, BUS_DMA_NOWAIT,
650                              &sc->amr_mailbox_dmamap);
651     if (error) {
652         device_printf(sc->amr_dev, "can't allocate mailbox memory\n");
653         return(ENOMEM);
654     }
655     bus_dmamap_load(sc->amr_mailbox_dmat, sc->amr_mailbox_dmamap, p,
656                     sizeof(struct amr_mailbox64), amr_sglist_helper, &baddr, 0);
657     /*
658      * Conventional mailbox is inside the mailbox64 region.
659      */
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;
665
666     return(0);
667 }
668
669 static int
670 amr_ccb_map(struct amr_softc *sc)
671 {
672     int         ccbsize, error;
673
674     /*
675      * Passthrough and Extended passthrough structures will share the same
676      * memory.
677      */
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 */
685                                 1,                      /* nsegments */
686                                 ccbsize,                /* maxsegsize */
687                                 0,                      /* flags */
688                                 NULL, NULL,             /* lockfunc, lockarg */
689                                 &sc->amr_ccb_dmat);
690     if (error != 0) {
691         device_printf(sc->amr_dev, "can't allocate ccb tag\n");
692         return (ENOMEM);
693     }
694
695     error = bus_dmamem_alloc(sc->amr_ccb_dmat, (void **)&sc->amr_ccb,
696                              BUS_DMA_NOWAIT, &sc->amr_ccb_dmamap);
697     if (error) {
698         device_printf(sc->amr_dev, "can't allocate ccb memory\n");
699         return (ENOMEM);
700     }
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);
704
705     return (0);
706 }
707