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