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