Merge branch 'vendor/ZLIB'
[dragonfly.git] / sys / dev / raid / 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  * Copyright (c) 2002 Eric Moore
29  * Copyright (c) 2002, 2004 LSI Logic Corporation
30  * All rights reserved.
31  *
32  * Redistribution and use in source and binary forms, with or without
33  * modification, are permitted provided that the following conditions
34  * are met:
35  * 1. Redistributions of source code must retain the above copyright
36  *    notice, this list of conditions and the following disclaimer.
37  * 2. Redistributions in binary form must reproduce the above copyright
38  *    notice, this list of conditions and the following disclaimer in the
39  *    documentation and/or other materials provided with the distribution.
40  * 3. The party using or redistributing the source code and binary forms
41  *    agrees to the disclaimer below and the terms and conditions set forth
42  *    herein.
43  *
44  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
45  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
48  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54  * SUCH DAMAGE.
55  *
56  * $FreeBSD: src/sys/dev/amr/amr_pci.c,v 1.40 2007/12/12 05:55:03 scottl Exp $
57  */
58
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/kernel.h>
62 #include <sys/module.h>
63 #include <sys/sysctl.h>
64
65 #include <sys/bio.h>
66 #include <sys/bus.h>
67 #include <sys/conf.h>
68
69 #include <sys/rman.h>
70
71 #include <bus/pci/pcireg.h>
72 #include <bus/pci/pcivar.h>
73
74 #include <dev/raid/amr/amrio.h>
75 #include <dev/raid/amr/amrreg.h>
76 #include <dev/raid/amr/amrvar.h>
77
78 static int              amr_pci_probe(device_t dev);
79 static int              amr_pci_attach(device_t dev);
80 static int              amr_pci_detach(device_t dev);
81 static int              amr_pci_shutdown(device_t dev);
82 static int              amr_pci_suspend(device_t dev);
83 static int              amr_pci_resume(device_t dev);
84 static void             amr_pci_intr(void *arg);
85 static void             amr_pci_free(struct amr_softc *sc);
86 static void             amr_sglist_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error);
87 static int              amr_sglist_map(struct amr_softc *sc);
88 static int              amr_setup_mbox(struct amr_softc *sc);
89 static int              amr_ccb_map(struct amr_softc *sc);
90
91 static u_int amr_force_sg32 = 0;
92 TUNABLE_INT("hw.amr.force_sg32", &amr_force_sg32);
93 SYSCTL_DECL(_hw_amr);
94 SYSCTL_UINT(_hw_amr, OID_AUTO, force_sg32, CTLFLAG_RD, &amr_force_sg32, 0,
95     "Force the AMR driver to use 32bit scatter gather");
96
97 static device_method_t amr_methods[] = {
98     /* Device interface */
99     DEVMETHOD(device_probe,     amr_pci_probe),
100     DEVMETHOD(device_attach,    amr_pci_attach),
101     DEVMETHOD(device_detach,    amr_pci_detach),
102     DEVMETHOD(device_shutdown,  amr_pci_shutdown),
103     DEVMETHOD(device_suspend,   amr_pci_suspend),
104     DEVMETHOD(device_resume,    amr_pci_resume),
105
106     DEVMETHOD(bus_print_child,  bus_generic_print_child),
107     DEVMETHOD(bus_driver_added, bus_generic_driver_added),
108     DEVMETHOD_END
109 };
110
111 static driver_t amr_pci_driver = {
112         "amr",
113         amr_methods,
114         sizeof(struct amr_softc)
115 };
116
117 static devclass_t       amr_devclass;
118 DRIVER_MODULE(amr, pci, amr_pci_driver, amr_devclass, NULL, NULL);
119 MODULE_VERSION(amr, 1);
120 MODULE_DEPEND(amr, pci, 1, 1, 1);
121 MODULE_DEPEND(amr, cam, 1, 1, 1);
122
123 static struct amr_ident
124 {
125     int         vendor;
126     int         device;
127     int         flags;
128 #define AMR_ID_PROBE_SIG        (1<<0)  /* generic i960RD, check signature */
129 #define AMR_ID_DO_SG64          (1<<1)
130 #define AMR_ID_QUARTZ           (1<<2)
131 } amr_device_ids[] = {
132     {0x101e, 0x9010, 0},
133     {0x101e, 0x9060, 0},
134     {0x8086, 0x1960, AMR_ID_QUARTZ | AMR_ID_PROBE_SIG},
135     {0x101e, 0x1960, AMR_ID_QUARTZ},
136     {0x1000, 0x1960, AMR_ID_QUARTZ | AMR_ID_DO_SG64 | AMR_ID_PROBE_SIG},
137     {0x1000, 0x0407, AMR_ID_QUARTZ | AMR_ID_DO_SG64},
138     {0x1000, 0x0408, AMR_ID_QUARTZ | AMR_ID_DO_SG64},
139     {0x1000, 0x0409, AMR_ID_QUARTZ | AMR_ID_DO_SG64},
140     {0x1028, 0x000e, AMR_ID_QUARTZ | AMR_ID_DO_SG64 | AMR_ID_PROBE_SIG}, /* perc4/di i960 */
141     {0x1028, 0x000f, AMR_ID_QUARTZ | AMR_ID_DO_SG64}, /* perc4/di Verde*/
142     {0x1028, 0x0013, AMR_ID_QUARTZ | AMR_ID_DO_SG64}, /* perc4/di */
143     {0, 0, 0}
144 };
145
146 static struct amr_ident *
147 amr_find_ident(device_t dev)
148 {
149     struct amr_ident *id;
150     int sig;
151
152     for (id = amr_device_ids; id->vendor != 0; id++) {
153         if ((pci_get_vendor(dev) == id->vendor) &&
154             (pci_get_device(dev) == id->device)) {
155
156             /* do we need to test for a signature? */
157             if (id->flags & AMR_ID_PROBE_SIG) {
158                 sig = pci_read_config(dev, AMR_CFG_SIG, 2);
159                 if ((sig != AMR_SIGNATURE_1) && (sig != AMR_SIGNATURE_2))
160                     continue;
161             }
162             return (id);
163         }
164     }
165     return (NULL);
166 }
167
168 static int
169 amr_pci_probe(device_t dev)
170 {
171
172     debug_called(1);
173
174     if (amr_find_ident(dev) != NULL) {
175         device_set_desc(dev, LSI_DESC_PCI);
176         return(BUS_PROBE_DEFAULT);
177     }
178     return(ENXIO);
179 }
180
181 static int
182 amr_pci_attach(device_t dev)
183 {
184     struct amr_softc    *sc;
185     struct amr_ident    *id;
186     int                 rid, rtype, error;
187     u_int32_t           command;
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     command = pci_read_config(dev, PCIR_COMMAND, 1);
208     if (id->flags & AMR_ID_QUARTZ) {
209         /*
210          * Make sure we are going to be able to talk to this board.
211          */
212         if ((command & PCIM_CMD_MEMEN) == 0) {
213             device_printf(dev, "memory window not available\n");
214             return (ENXIO);
215         }
216         sc->amr_type |= AMR_TYPE_QUARTZ;
217     } else {
218         /*
219          * Make sure we are going to be able to talk to this board.
220          */
221         if ((command & PCIM_CMD_PORTEN) == 0) {
222             device_printf(dev, "I/O window not available\n");
223             return (ENXIO);
224         }
225     }
226
227     if ((amr_force_sg32 == 0) && (id->flags & AMR_ID_DO_SG64) &&
228         (sizeof(vm_paddr_t) > 4)) {
229         device_printf(dev, "Using 64-bit DMA\n");
230         sc->amr_type |= AMR_TYPE_SG64;
231     }
232
233     /* force the busmaster enable bit on */
234     if (!(command & PCIM_CMD_BUSMASTEREN)) {
235         device_printf(dev, "busmaster bit not set, enabling\n");
236         command |= PCIM_CMD_BUSMASTEREN;
237         pci_write_config(dev, PCIR_COMMAND, command, 2);
238     }
239
240     /*
241      * Allocate the PCI register window.
242      */
243     rid = PCIR_BAR(0);
244     rtype = AMR_IS_QUARTZ(sc) ? SYS_RES_MEMORY : SYS_RES_IOPORT;
245     sc->amr_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
246     if (sc->amr_reg == NULL) {
247         device_printf(sc->amr_dev, "can't allocate register window\n");
248         goto out;
249     }
250     sc->amr_btag = rman_get_bustag(sc->amr_reg);
251     sc->amr_bhandle = rman_get_bushandle(sc->amr_reg);
252
253     /*
254      * Allocate and connect our interrupt.
255      */
256     rid = 0;
257     sc->amr_irq = bus_alloc_resource_any(sc->amr_dev, SYS_RES_IRQ, &rid,
258         RF_SHAREABLE | RF_ACTIVE);
259     if (sc->amr_irq == NULL) {
260         device_printf(sc->amr_dev, "can't allocate interrupt\n");
261         goto out;
262     }
263     if (bus_setup_intr(sc->amr_dev, sc->amr_irq,
264         INTR_MPSAFE, amr_pci_intr,
265         sc, &sc->amr_intr, NULL)) {
266         device_printf(sc->amr_dev, "can't set up interrupt\n");
267         goto out;
268     }
269
270     debug(2, "interrupt attached");
271
272     /* assume failure is 'out of memory' */
273     error = ENOMEM;
274
275     /*
276      * Allocate the parent bus DMA tag appropriate for PCI.
277      */
278     if (bus_dma_tag_create(NULL,                        /* parent */
279                            1, 0,                        /* alignment,boundary */
280                            AMR_IS_SG64(sc) ?
281                            BUS_SPACE_MAXADDR :
282                            BUS_SPACE_MAXADDR_32BIT,     /* lowaddr */
283                            BUS_SPACE_MAXADDR,           /* highaddr */
284                            NULL, NULL,                  /* filter, filterarg */
285                            MAXBSIZE, AMR_NSEG,          /* maxsize, nsegments */
286                            BUS_SPACE_MAXSIZE_32BIT,     /* maxsegsize */
287                            0,                           /* flags */
288                            &sc->amr_parent_dmat)) {
289         device_printf(dev, "can't allocate parent DMA tag\n");
290         goto out;
291     }
292
293     /*
294      * Create DMA tag for mapping buffers into controller-addressable space.
295      */
296     if (bus_dma_tag_create(sc->amr_parent_dmat,         /* parent */
297                            1, 0,                        /* alignment,boundary */
298                            BUS_SPACE_MAXADDR_32BIT,     /* lowaddr */
299                            BUS_SPACE_MAXADDR,           /* highaddr */
300                            NULL, NULL,                  /* filter, filterarg */
301                            MAXBSIZE, AMR_NSEG,          /* maxsize, nsegments */
302                            MAXBSIZE,                    /* maxsegsize */
303                            0,           /* flags */
304                            &sc->amr_buffer_dmat)) {
305         device_printf(sc->amr_dev, "can't allocate buffer DMA tag\n");
306         goto out;
307     }
308
309     if (bus_dma_tag_create(sc->amr_parent_dmat,         /* parent */
310                            1, 0,                        /* alignment,boundary */
311                            BUS_SPACE_MAXADDR,           /* lowaddr */
312                            BUS_SPACE_MAXADDR,           /* highaddr */
313                            NULL, NULL,                  /* filter, filterarg */
314                            MAXBSIZE, AMR_NSEG,          /* maxsize, nsegments */
315                            MAXBSIZE,                    /* maxsegsize */
316                            0,           /* flags */
317                            &sc->amr_buffer64_dmat)) {
318         device_printf(sc->amr_dev, "can't allocate buffer DMA tag\n");
319         goto out;
320     }
321
322     debug(2, "dma tag done");
323
324     /*
325      * Allocate and set up mailbox in a bus-visible fashion.
326      */
327     lockinit(&sc->amr_list_lock, "AMR List Lock", 0, LK_CANRECURSE);
328     lockinit(&sc->amr_hw_lock, "AMR HW Lock", 0, LK_CANRECURSE);
329     if ((error = amr_setup_mbox(sc)) != 0)
330         goto out;
331
332     debug(2, "mailbox setup");
333
334     /*
335      * Build the scatter/gather buffers.
336      */
337     if ((error = amr_sglist_map(sc)) != 0)
338         goto out;
339     debug(2, "s/g list mapped");
340
341     if ((error = amr_ccb_map(sc)) != 0)
342         goto out;
343     debug(2, "ccb mapped");
344
345
346     /*
347      * Do bus-independant initialisation, bring controller online.
348      */
349     error = amr_attach(sc);
350
351 out:
352     if (error)
353         amr_pci_free(sc);
354     return(error);
355 }
356
357 /********************************************************************************
358  * Disconnect from the controller completely, in preparation for unload.
359  */
360 static int
361 amr_pci_detach(device_t dev)
362 {
363     struct amr_softc    *sc = device_get_softc(dev);
364     int                 error;
365
366     debug_called(1);
367
368     if (sc->amr_state & AMR_STATE_OPEN)
369         return(EBUSY);
370
371     if ((error = amr_pci_shutdown(dev)))
372         return(error);
373
374     amr_pci_free(sc);
375
376     return(0);
377 }
378
379 /********************************************************************************
380  * Bring the controller down to a dormant state and detach all child devices.
381  *
382  * This function is called before detach, system shutdown, or before performing
383  * an operation which may add or delete system disks.  (Call amr_startup to
384  * resume normal operation.)
385  *
386  * Note that we can assume that the bioq on the controller is empty, as we won't
387  * allow shutdown if any device is open.
388  */
389 static int
390 amr_pci_shutdown(device_t dev)
391 {
392     struct amr_softc    *sc = device_get_softc(dev);
393     int                 i,error;
394
395     debug_called(1);
396
397     /* mark ourselves as in-shutdown */
398     sc->amr_state |= AMR_STATE_SHUTDOWN;
399
400
401     /* flush controller */
402     device_printf(sc->amr_dev, "flushing cache...");
403     kprintf("%s\n", amr_flush(sc) ? "failed" : "done");
404
405     error = 0;
406
407     /* delete all our child devices */
408     for(i = 0 ; i < AMR_MAXLD; i++) {
409         if( sc->amr_drive[i].al_disk != NULL) {
410             if((error = device_delete_child(sc->amr_dev,sc->amr_drive[i].al_disk)) != 0)
411                 goto shutdown_out;
412             sc->amr_drive[i].al_disk = NULL;
413         }
414     }
415
416     /* XXX disable interrupts? */
417
418 shutdown_out:
419     return(error);
420 }
421
422 /********************************************************************************
423  * Bring the controller to a quiescent state, ready for system suspend.
424  */
425 static int
426 amr_pci_suspend(device_t dev)
427 {
428     struct amr_softc    *sc = device_get_softc(dev);
429
430     debug_called(1);
431
432     sc->amr_state |= AMR_STATE_SUSPEND;
433
434     /* flush controller */
435     device_printf(sc->amr_dev, "flushing cache...");
436     kprintf("%s\n", amr_flush(sc) ? "failed" : "done");
437     
438     /* XXX disable interrupts? */
439
440     return(0);
441 }
442
443 /********************************************************************************
444  * Bring the controller back to a state ready for operation.
445  */
446 static int
447 amr_pci_resume(device_t dev)
448 {
449     struct amr_softc    *sc = device_get_softc(dev);
450
451     debug_called(1);
452
453     sc->amr_state &= ~AMR_STATE_SUSPEND;
454
455     /* XXX enable interrupts? */
456
457     return(0);
458 }
459
460 /*******************************************************************************
461  * Take an interrupt, or be poked by other code to look for interrupt-worthy
462  * status.
463  */
464 static void
465 amr_pci_intr(void *arg)
466 {
467     struct amr_softc    *sc = (struct amr_softc *)arg;
468
469     debug_called(3);
470
471     /* collect finished commands, queue anything waiting */
472     amr_done(sc);
473 }
474
475 /********************************************************************************
476  * Free all of the resources associated with (sc)
477  *
478  * Should not be called if the controller is active.
479  */
480 static void
481 amr_pci_free(struct amr_softc *sc)
482 {
483     void *p;
484
485     debug_called(1);
486
487     amr_free(sc);
488
489     /* destroy data-transfer DMA tag */
490     if (sc->amr_buffer_dmat)
491         bus_dma_tag_destroy(sc->amr_buffer_dmat);
492     if (sc->amr_buffer64_dmat)
493         bus_dma_tag_destroy(sc->amr_buffer64_dmat);
494
495     /* free and destroy DMA memory and tag for passthrough pool */
496     if (sc->amr_ccb)
497         bus_dmamem_free(sc->amr_ccb_dmat, sc->amr_ccb, sc->amr_ccb_dmamap);
498     if (sc->amr_ccb_dmat)
499         bus_dma_tag_destroy(sc->amr_ccb_dmat);
500
501     /* free and destroy DMA memory and tag for s/g lists */
502     if (sc->amr_sgtable)
503         bus_dmamem_free(sc->amr_sg_dmat, sc->amr_sgtable, sc->amr_sg_dmamap);
504     if (sc->amr_sg_dmat)
505         bus_dma_tag_destroy(sc->amr_sg_dmat);
506
507     /* free and destroy DMA memory and tag for mailbox */
508     p = (void *)(uintptr_t)(volatile void *)sc->amr_mailbox64;
509     if (sc->amr_mailbox) {
510         bus_dmamem_free(sc->amr_mailbox_dmat, p, sc->amr_mailbox_dmamap);
511     }
512     if (sc->amr_mailbox_dmat)
513         bus_dma_tag_destroy(sc->amr_mailbox_dmat);
514
515     /* disconnect the interrupt handler */
516     if (sc->amr_intr)
517         bus_teardown_intr(sc->amr_dev, sc->amr_irq, sc->amr_intr);
518     if (sc->amr_irq != NULL)
519         bus_release_resource(sc->amr_dev, SYS_RES_IRQ, 0, sc->amr_irq);
520
521     /* destroy the parent DMA tag */
522     if (sc->amr_parent_dmat)
523         bus_dma_tag_destroy(sc->amr_parent_dmat);
524
525     /* release the register window mapping */
526     if (sc->amr_reg != NULL)
527         bus_release_resource(sc->amr_dev,
528                              AMR_IS_QUARTZ(sc) ? SYS_RES_MEMORY : SYS_RES_IOPORT,
529                              PCIR_BAR(0), sc->amr_reg);
530 }
531
532 /********************************************************************************
533  * Allocate and map the scatter/gather table in bus space.
534  */
535 static void
536 amr_sglist_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
537 {
538     uint32_t *addr;
539
540     debug_called(1);
541
542     addr = arg;
543     *addr = segs[0].ds_addr;
544 }
545
546 static int
547 amr_sglist_map(struct amr_softc *sc)
548 {
549     size_t      segsize;
550     void        *p;
551     int         error;
552
553     debug_called(1);
554
555     /*
556      * Create a single tag describing a region large enough to hold all of
557      * the s/g lists we will need.
558      *
559      * Note that we could probably use AMR_LIMITCMD here, but that may become
560      * tunable.
561      */
562     if (AMR_IS_SG64(sc))
563         segsize = sizeof(struct amr_sg64entry) * AMR_NSEG * AMR_MAXCMD;
564     else
565         segsize = sizeof(struct amr_sgentry) * AMR_NSEG * AMR_MAXCMD;
566
567     error = bus_dma_tag_create(sc->amr_parent_dmat,     /* parent */
568                                512, 0,                  /* alignment,boundary */
569                                BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
570                                BUS_SPACE_MAXADDR,       /* highaddr */
571                                NULL, NULL,              /* filter, filterarg */
572                                segsize, 1,              /* maxsize, nsegments */
573                                BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
574                                0,                       /* flags */
575                                &sc->amr_sg_dmat);
576     if (error != 0) {
577         device_printf(sc->amr_dev, "can't allocate scatter/gather DMA tag\n");
578         return(ENOMEM);
579     }
580
581     /*
582      * Allocate enough s/g maps for all commands and permanently map them into
583      * controller-visible space.
584      *  
585      * XXX this assumes we can get enough space for all the s/g maps in one 
586      * contiguous slab.  We may need to switch to a more complex arrangement
587      * where we allocate in smaller chunks and keep a lookup table from slot
588      * to bus address.
589      *
590      * XXX HACK ALERT:  at least some controllers don't like the s/g memory
591      *                  being allocated below 0x2000.  We leak some memory if
592      *                  we get some below this mark and allocate again.  We
593      *                  should be able to avoid this with the tag setup, but
594      *                  that does't seem to work.
595      */
596 retry:
597     error = bus_dmamem_alloc(sc->amr_sg_dmat, &p, BUS_DMA_NOWAIT,
598                              &sc->amr_sg_dmamap);
599     if (error) {
600         device_printf(sc->amr_dev, "can't allocate s/g table\n");
601         return(ENOMEM);
602     }
603     bus_dmamap_load(sc->amr_sg_dmat, sc->amr_sg_dmamap, p, segsize, amr_sglist_helper, &sc->amr_sgbusaddr, 0);
604     if (sc->amr_sgbusaddr < 0x2000) {
605         debug(1, "s/g table too low (0x%x), reallocating\n", sc->amr_sgbusaddr);
606         goto retry;
607     }
608
609     if (AMR_IS_SG64(sc))
610         sc->amr_sg64table = (struct amr_sg64entry *)p;
611     sc->amr_sgtable = (struct amr_sgentry *)p;
612
613     return(0);
614 }
615
616 /********************************************************************************
617  * Allocate and set up mailbox areas for the controller (sc)
618  *
619  * The basic mailbox structure should be 16-byte aligned.
620  */
621 static int
622 amr_setup_mbox(struct amr_softc *sc)
623 {
624     int         error;
625     void        *p;
626     uint32_t    baddr;
627     
628     debug_called(1);
629
630     /*
631      * Create a single tag describing a region large enough to hold the entire
632      * mailbox.
633      */
634     error = bus_dma_tag_create(sc->amr_parent_dmat,     /* parent */
635                                16, 0,                   /* alignment,boundary */
636                                BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
637                                BUS_SPACE_MAXADDR,       /* highaddr */
638                                NULL, NULL,              /* filter, filterarg */
639                                sizeof(struct amr_mailbox64), /* maxsize */
640                                1,                       /* nsegments */
641                                BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
642                                0,                       /* flags */
643                                &sc->amr_mailbox_dmat);
644     if (error != 0) {
645         device_printf(sc->amr_dev, "can't allocate mailbox tag\n");
646         return(ENOMEM);
647     }
648
649     /*
650      * Allocate the mailbox structure and permanently map it into
651      * controller-visible space.
652      */
653     error = bus_dmamem_alloc(sc->amr_mailbox_dmat, &p, BUS_DMA_NOWAIT,
654                              &sc->amr_mailbox_dmamap);
655     if (error) {
656         device_printf(sc->amr_dev, "can't allocate mailbox memory\n");
657         return(ENOMEM);
658     }
659     bus_dmamap_load(sc->amr_mailbox_dmat, sc->amr_mailbox_dmamap, p,
660                     sizeof(struct amr_mailbox64), amr_sglist_helper, &baddr, 0);
661     /*
662      * Conventional mailbox is inside the mailbox64 region.
663      */
664     /* save physical base of the basic mailbox structure */
665     sc->amr_mailboxphys = baddr + offsetof(struct amr_mailbox64, mb);
666     bzero(p, sizeof(struct amr_mailbox64));
667     sc->amr_mailbox64 = (struct amr_mailbox64 *)p;
668     sc->amr_mailbox = &sc->amr_mailbox64->mb;
669
670     return(0);
671 }
672
673 static int
674 amr_ccb_map(struct amr_softc *sc)
675 {
676     int         ccbsize, error;
677
678     /*
679      * Passthrough and Extended passthrough structures will share the same
680      * memory.
681      */
682     ccbsize = sizeof(union amr_ccb) * AMR_MAXCMD;
683     error = bus_dma_tag_create(sc->amr_parent_dmat,     /* parent */
684                                 128, 0,                 /* alignment,boundary */
685                                 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
686                                 BUS_SPACE_MAXADDR,      /* highaddr */
687                                 NULL, NULL,             /* filter, filterarg */
688                                 ccbsize,                /* maxsize */
689                                 1,                      /* nsegments */
690                                 ccbsize,                /* maxsegsize */
691                                 0,                      /* flags */
692                                 &sc->amr_ccb_dmat);
693     if (error != 0) {
694         device_printf(sc->amr_dev, "can't allocate ccb tag\n");
695         return (ENOMEM);
696     }
697
698     error = bus_dmamem_alloc(sc->amr_ccb_dmat, (void **)&sc->amr_ccb,
699                              BUS_DMA_NOWAIT, &sc->amr_ccb_dmamap);
700     if (error) {
701         device_printf(sc->amr_dev, "can't allocate ccb memory\n");
702         return (ENOMEM);
703     }
704     bus_dmamap_load(sc->amr_ccb_dmat, sc->amr_ccb_dmamap, sc->amr_ccb,
705                     ccbsize, amr_sglist_helper, &sc->amr_ccb_busaddr, 0);
706     bzero(sc->amr_ccb, ccbsize);
707
708     return (0);
709 }