]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/amr/amr_pci.c
Upgrade Unbound to 1.6.4. More to follow.
[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 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);
123
124 static struct amr_ident
125 {
126     int         vendor;
127     int         device;
128     int         flags;
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[] = {
133     {0x101e, 0x9010, 0},
134     {0x101e, 0x9060, 0},
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 */
144     {0, 0, 0}
145 };
146
147 static struct amr_ident *
148 amr_find_ident(device_t dev)
149 {
150     struct amr_ident *id;
151     int sig;
152
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)) {
156
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))
161                     continue;
162             }
163             return (id);
164         }
165     }
166     return (NULL);
167 }
168         
169 static int
170 amr_pci_probe(device_t dev)
171 {
172
173     debug_called(1);
174
175     if (amr_find_ident(dev) != NULL) {
176         device_set_desc(dev, LSI_DESC_PCI);
177         return(BUS_PROBE_DEFAULT);
178     }
179     return(ENXIO);
180 }
181
182 static int
183 amr_pci_attach(device_t dev)
184 {
185     struct amr_softc    *sc;
186     struct amr_ident    *id;
187     int                 rid, rtype, error;
188
189     debug_called(1);
190
191     /*
192      * Initialise softc.
193      */
194     sc = device_get_softc(dev);
195     bzero(sc, sizeof(*sc));
196     sc->amr_dev = dev;
197
198     /* assume failure is 'not configured' */
199     error = ENXIO;
200
201     /*
202      * Determine board type.
203      */
204     if ((id = amr_find_ident(dev)) == NULL)
205         return (ENXIO);
206
207     if (id->flags & AMR_ID_QUARTZ) {
208         sc->amr_type |= AMR_TYPE_QUARTZ;
209     }
210
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;
215     }
216
217     /* force the busmaster enable bit on */
218     pci_enable_busmaster(dev);
219
220     /*
221      * Allocate the PCI register window.
222      */
223     rid = PCIR_BAR(0);
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");
228         goto out;
229     }
230     sc->amr_btag = rman_get_bustag(sc->amr_reg);
231     sc->amr_bhandle = rman_get_bushandle(sc->amr_reg);
232
233     /*
234      * Allocate and connect our interrupt.
235      */
236     rid = 0;
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");
241         goto out;
242     }
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");
247         goto out;
248     }
249
250     debug(2, "interrupt attached");
251
252     /* assume failure is 'out of memory' */
253     error = ENOMEM;
254
255     /*
256      * Allocate the parent bus DMA tag appropriate for PCI.
257      */
258     if (bus_dma_tag_create(bus_get_dma_tag(dev),        /* PCI parent */
259                            1, 0,                        /* alignment,boundary */
260                            AMR_IS_SG64(sc) ?
261                            BUS_SPACE_MAXADDR :
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 */
268                            0,                           /* flags */
269                            NULL, NULL,                  /* lockfunc, lockarg */
270                            &sc->amr_parent_dmat)) {
271         device_printf(dev, "can't allocate parent DMA tag\n");
272         goto out;
273     }
274
275     /*
276      * Create DMA tag for mapping buffers into controller-addressable space.
277      */
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 */
286                            0,           /* flags */
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");
291         goto out;
292     }
293
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 */
302                            0,           /* flags */
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");
307         goto out;
308     }
309
310     debug(2, "dma tag done");
311
312     /*
313      * Allocate and set up mailbox in a bus-visible fashion.
314      */
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)
318         goto out;
319
320     debug(2, "mailbox setup");
321
322     /*
323      * Build the scatter/gather buffers.
324      */
325     if ((error = amr_sglist_map(sc)) != 0)
326         goto out;
327     debug(2, "s/g list mapped");
328
329     if ((error = amr_ccb_map(sc)) != 0)
330         goto out;
331     debug(2, "ccb mapped");
332
333
334     /*
335      * Do bus-independant initialisation, bring controller online.
336      */
337     error = amr_attach(sc);
338
339 out:
340     if (error)
341         amr_pci_free(sc);
342     return(error);
343 }
344
345 /********************************************************************************
346  * Disconnect from the controller completely, in preparation for unload.
347  */
348 static int
349 amr_pci_detach(device_t dev)
350 {
351     struct amr_softc    *sc = device_get_softc(dev);
352     int                 error;
353
354     debug_called(1);
355
356     if (sc->amr_state & AMR_STATE_OPEN)
357         return(EBUSY);
358
359     if ((error = amr_pci_shutdown(dev)))
360         return(error);
361
362     amr_pci_free(sc);
363
364     return(0);
365 }
366
367 /********************************************************************************
368  * Bring the controller down to a dormant state and detach all child devices.
369  *
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.)
373  *
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.
376  */
377 static int
378 amr_pci_shutdown(device_t dev)
379 {
380     struct amr_softc    *sc = device_get_softc(dev);
381     int                 i,error;
382
383     debug_called(1);
384
385     /* mark ourselves as in-shutdown */
386     sc->amr_state |= AMR_STATE_SHUTDOWN;
387
388
389     /* flush controller */
390     device_printf(sc->amr_dev, "flushing cache...");
391     printf("%s\n", amr_flush(sc) ? "failed" : "done");
392
393     error = 0;
394
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)
399                 goto shutdown_out;
400             sc->amr_drive[i].al_disk = 0;
401         }
402     }
403
404     /* XXX disable interrupts? */
405
406 shutdown_out:
407     return(error);
408 }
409
410 /********************************************************************************
411  * Bring the controller to a quiescent state, ready for system suspend.
412  */
413 static int
414 amr_pci_suspend(device_t dev)
415 {
416     struct amr_softc    *sc = device_get_softc(dev);
417
418     debug_called(1);
419
420     sc->amr_state |= AMR_STATE_SUSPEND;
421
422     /* flush controller */
423     device_printf(sc->amr_dev, "flushing cache...");
424     printf("%s\n", amr_flush(sc) ? "failed" : "done");
425     
426     /* XXX disable interrupts? */
427
428     return(0);
429 }
430
431 /********************************************************************************
432  * Bring the controller back to a state ready for operation.
433  */
434 static int
435 amr_pci_resume(device_t dev)
436 {
437     struct amr_softc    *sc = device_get_softc(dev);
438
439     debug_called(1);
440
441     sc->amr_state &= ~AMR_STATE_SUSPEND;
442
443     /* XXX enable interrupts? */
444
445     return(0);
446 }
447
448 /*******************************************************************************
449  * Take an interrupt, or be poked by other code to look for interrupt-worthy
450  * status.
451  */
452 static void
453 amr_pci_intr(void *arg)
454 {
455     struct amr_softc    *sc = (struct amr_softc *)arg;
456
457     debug_called(3);
458
459     /* collect finished commands, queue anything waiting */
460     amr_done(sc);
461 }
462
463 /********************************************************************************
464  * Free all of the resources associated with (sc)
465  *
466  * Should not be called if the controller is active.
467  */
468 static void
469 amr_pci_free(struct amr_softc *sc)
470 {
471     void *p;
472
473     debug_called(1);
474
475     amr_free(sc);
476
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);
482
483     /* free and destroy DMA memory and tag for passthrough pool */
484     if (sc->amr_ccb) {
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);
487     }
488     if (sc->amr_ccb_dmat)
489         bus_dma_tag_destroy(sc->amr_ccb_dmat);
490
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);
495     }
496     if (sc->amr_sg_dmat)
497         bus_dma_tag_destroy(sc->amr_sg_dmat);
498
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);
504     }
505     if (sc->amr_mailbox_dmat)
506         bus_dma_tag_destroy(sc->amr_mailbox_dmat);
507
508     /* disconnect the interrupt handler */
509     if (sc->amr_intr)
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);
513
514     /* destroy the parent DMA tag */
515     if (sc->amr_parent_dmat)
516         bus_dma_tag_destroy(sc->amr_parent_dmat);
517
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);
523 }
524
525 /********************************************************************************
526  * Allocate and map the scatter/gather table in bus space.
527  */
528 static void
529 amr_sglist_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
530 {
531     uint32_t *addr;
532
533     debug_called(1);
534
535     addr = arg;
536     *addr = segs[0].ds_addr;
537 }
538
539 static int
540 amr_sglist_map(struct amr_softc *sc)
541 {
542     size_t      segsize;
543     void        *p;
544     int         error;
545
546     debug_called(1);
547
548     /*
549      * Create a single tag describing a region large enough to hold all of
550      * the s/g lists we will need.
551      *
552      * Note that we could probably use AMR_LIMITCMD here, but that may become
553      * tunable.
554      */
555     if (AMR_IS_SG64(sc))
556         segsize = sizeof(struct amr_sg64entry) * AMR_NSEG * AMR_MAXCMD;
557     else
558         segsize = sizeof(struct amr_sgentry) * AMR_NSEG * AMR_MAXCMD;
559
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 */
567                                0,                       /* flags */
568                                NULL, NULL,              /* lockfunc, lockarg */
569                                &sc->amr_sg_dmat);
570     if (error != 0) {
571         device_printf(sc->amr_dev, "can't allocate scatter/gather DMA tag\n");
572         return(ENOMEM);
573     }
574
575     /*
576      * Allocate enough s/g maps for all commands and permanently map them into
577      * controller-visible space.
578      *  
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
582      * to bus address.
583      *
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.
589      */
590 retry:
591     error = bus_dmamem_alloc(sc->amr_sg_dmat, (void **)&p, BUS_DMA_NOWAIT, &sc->amr_sg_dmamap);
592     if (error) {
593         device_printf(sc->amr_dev, "can't allocate s/g table\n");
594         return(ENOMEM);
595     }
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);
599         goto retry;
600     }
601
602     if (AMR_IS_SG64(sc))
603         sc->amr_sg64table = (struct amr_sg64entry *)p;
604     sc->amr_sgtable = (struct amr_sgentry *)p;
605
606     return(0);
607 }
608
609 /********************************************************************************
610  * Allocate and set up mailbox areas for the controller (sc)
611  *
612  * The basic mailbox structure should be 16-byte aligned.
613  */
614 static int
615 amr_setup_mbox(struct amr_softc *sc)
616 {
617     int         error;
618     void        *p;
619     uint32_t    baddr;
620     
621     debug_called(1);
622
623     /*
624      * Create a single tag describing a region large enough to hold the entire
625      * mailbox.
626      */
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 */
633                                1,                       /* nsegments */
634                                BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
635                                0,                       /* flags */
636                                NULL, NULL,              /* lockfunc, lockarg */
637                                &sc->amr_mailbox_dmat);
638     if (error != 0) {
639         device_printf(sc->amr_dev, "can't allocate mailbox tag\n");
640         return(ENOMEM);
641     }
642
643     /*
644      * Allocate the mailbox structure and permanently map it into
645      * controller-visible space.
646      */
647     error = bus_dmamem_alloc(sc->amr_mailbox_dmat, (void **)&p, BUS_DMA_NOWAIT,
648                              &sc->amr_mailbox_dmamap);
649     if (error) {
650         device_printf(sc->amr_dev, "can't allocate mailbox memory\n");
651         return(ENOMEM);
652     }
653     bus_dmamap_load(sc->amr_mailbox_dmat, sc->amr_mailbox_dmamap, p,
654                     sizeof(struct amr_mailbox64), amr_sglist_helper, &baddr, 0);
655     /*
656      * Conventional mailbox is inside the mailbox64 region.
657      */
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;
663
664     return(0);
665 }
666
667 static int
668 amr_ccb_map(struct amr_softc *sc)
669 {
670     int         ccbsize, error;
671
672     /*
673      * Passthrough and Extended passthrough structures will share the same
674      * memory.
675      */
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 */
683                                 1,                      /* nsegments */
684                                 ccbsize,                /* maxsegsize */
685                                 0,                      /* flags */
686                                 NULL, NULL,             /* lockfunc, lockarg */
687                                 &sc->amr_ccb_dmat);
688     if (error != 0) {
689         device_printf(sc->amr_dev, "can't allocate ccb tag\n");
690         return (ENOMEM);
691     }
692
693     error = bus_dmamem_alloc(sc->amr_ccb_dmat, (void **)&sc->amr_ccb,
694                              BUS_DMA_NOWAIT, &sc->amr_ccb_dmamap);
695     if (error) {
696         device_printf(sc->amr_dev, "can't allocate ccb memory\n");
697         return (ENOMEM);
698     }
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);
702
703     return (0);
704 }
705