]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/dev/amr/amr_pci.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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_END
110 };
111
112 static driver_t amr_pci_driver = {
113         "amr",
114         amr_methods,
115         sizeof(struct amr_softc)
116 };
117
118 static devclass_t       amr_devclass;
119 DRIVER_MODULE(amr, pci, amr_pci_driver, amr_devclass, 0, 0);
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_TYPE_BIO | INTR_ENTROPY | INTR_MPSAFE, NULL, amr_pci_intr,
265         sc, &sc->amr_intr)) {
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(bus_get_dma_tag(dev),        /* PCI 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                            NULL, NULL,                  /* lockfunc, lockarg */
289                            &sc->amr_parent_dmat)) {
290         device_printf(dev, "can't allocate parent DMA tag\n");
291         goto out;
292     }
293
294     /*
295      * Create DMA tag for mapping buffers into controller-addressable space.
296      */
297     if (bus_dma_tag_create(sc->amr_parent_dmat,         /* parent */
298                            1, 0,                        /* alignment,boundary */
299                            BUS_SPACE_MAXADDR_32BIT,     /* lowaddr */
300                            BUS_SPACE_MAXADDR,           /* highaddr */
301                            NULL, NULL,                  /* filter, filterarg */
302                            MAXBSIZE, AMR_NSEG,          /* maxsize, nsegments */
303                            MAXBSIZE,                    /* maxsegsize */
304                            0,           /* flags */
305                            busdma_lock_mutex,           /* lockfunc */
306                            &sc->amr_list_lock,          /* lockarg */
307                            &sc->amr_buffer_dmat)) {
308         device_printf(sc->amr_dev, "can't allocate buffer DMA tag\n");
309         goto out;
310     }
311
312     if (bus_dma_tag_create(sc->amr_parent_dmat,         /* parent */
313                            1, 0,                        /* alignment,boundary */
314                            BUS_SPACE_MAXADDR,           /* lowaddr */
315                            BUS_SPACE_MAXADDR,           /* highaddr */
316                            NULL, NULL,                  /* filter, filterarg */
317                            MAXBSIZE, AMR_NSEG,          /* maxsize, nsegments */
318                            MAXBSIZE,                    /* maxsegsize */
319                            0,           /* flags */
320                            busdma_lock_mutex,           /* lockfunc */
321                            &sc->amr_list_lock,          /* lockarg */
322                            &sc->amr_buffer64_dmat)) {
323         device_printf(sc->amr_dev, "can't allocate buffer DMA tag\n");
324         goto out;
325     }
326
327     debug(2, "dma tag done");
328
329     /*
330      * Allocate and set up mailbox in a bus-visible fashion.
331      */
332     mtx_init(&sc->amr_list_lock, "AMR List Lock", NULL, MTX_DEF);
333     mtx_init(&sc->amr_hw_lock, "AMR HW Lock", NULL, MTX_DEF);
334     if ((error = amr_setup_mbox(sc)) != 0)
335         goto out;
336
337     debug(2, "mailbox setup");
338
339     /*
340      * Build the scatter/gather buffers.
341      */
342     if (amr_sglist_map(sc))
343         goto out;
344     debug(2, "s/g list mapped");
345
346     if (amr_ccb_map(sc))
347         goto out;
348     debug(2, "ccb mapped");
349
350
351     /*
352      * Do bus-independant initialisation, bring controller online.
353      */
354     error = amr_attach(sc);
355
356 out:
357     if (error)
358         amr_pci_free(sc);
359     return(error);
360 }
361
362 /********************************************************************************
363  * Disconnect from the controller completely, in preparation for unload.
364  */
365 static int
366 amr_pci_detach(device_t dev)
367 {
368     struct amr_softc    *sc = device_get_softc(dev);
369     int                 error;
370
371     debug_called(1);
372
373     if (sc->amr_state & AMR_STATE_OPEN)
374         return(EBUSY);
375
376     if ((error = amr_pci_shutdown(dev)))
377         return(error);
378
379     amr_pci_free(sc);
380
381     return(0);
382 }
383
384 /********************************************************************************
385  * Bring the controller down to a dormant state and detach all child devices.
386  *
387  * This function is called before detach, system shutdown, or before performing
388  * an operation which may add or delete system disks.  (Call amr_startup to
389  * resume normal operation.)
390  *
391  * Note that we can assume that the bioq on the controller is empty, as we won't
392  * allow shutdown if any device is open.
393  */
394 static int
395 amr_pci_shutdown(device_t dev)
396 {
397     struct amr_softc    *sc = device_get_softc(dev);
398     int                 i,error;
399
400     debug_called(1);
401
402     /* mark ourselves as in-shutdown */
403     sc->amr_state |= AMR_STATE_SHUTDOWN;
404
405
406     /* flush controller */
407     device_printf(sc->amr_dev, "flushing cache...");
408     printf("%s\n", amr_flush(sc) ? "failed" : "done");
409
410     error = 0;
411
412     /* delete all our child devices */
413     for(i = 0 ; i < AMR_MAXLD; i++) {
414         if( sc->amr_drive[i].al_disk != 0) {
415             if((error = device_delete_child(sc->amr_dev,sc->amr_drive[i].al_disk)) != 0)
416                 goto shutdown_out;
417             sc->amr_drive[i].al_disk = 0;
418         }
419     }
420
421     /* XXX disable interrupts? */
422
423 shutdown_out:
424     return(error);
425 }
426
427 /********************************************************************************
428  * Bring the controller to a quiescent state, ready for system suspend.
429  */
430 static int
431 amr_pci_suspend(device_t dev)
432 {
433     struct amr_softc    *sc = device_get_softc(dev);
434
435     debug_called(1);
436
437     sc->amr_state |= AMR_STATE_SUSPEND;
438
439     /* flush controller */
440     device_printf(sc->amr_dev, "flushing cache...");
441     printf("%s\n", amr_flush(sc) ? "failed" : "done");
442     
443     /* XXX disable interrupts? */
444
445     return(0);
446 }
447
448 /********************************************************************************
449  * Bring the controller back to a state ready for operation.
450  */
451 static int
452 amr_pci_resume(device_t dev)
453 {
454     struct amr_softc    *sc = device_get_softc(dev);
455
456     debug_called(1);
457
458     sc->amr_state &= ~AMR_STATE_SUSPEND;
459
460     /* XXX enable interrupts? */
461
462     return(0);
463 }
464
465 /*******************************************************************************
466  * Take an interrupt, or be poked by other code to look for interrupt-worthy
467  * status.
468  */
469 static void
470 amr_pci_intr(void *arg)
471 {
472     struct amr_softc    *sc = (struct amr_softc *)arg;
473
474     debug_called(3);
475
476     /* collect finished commands, queue anything waiting */
477     amr_done(sc);
478 }
479
480 /********************************************************************************
481  * Free all of the resources associated with (sc)
482  *
483  * Should not be called if the controller is active.
484  */
485 static void
486 amr_pci_free(struct amr_softc *sc)
487 {
488     void *p;
489
490     debug_called(1);
491
492     amr_free(sc);
493
494     /* destroy data-transfer DMA tag */
495     if (sc->amr_buffer_dmat)
496         bus_dma_tag_destroy(sc->amr_buffer_dmat);
497     if (sc->amr_buffer64_dmat)
498         bus_dma_tag_destroy(sc->amr_buffer64_dmat);
499
500     /* free and destroy DMA memory and tag for passthrough pool */
501     if (sc->amr_ccb)
502         bus_dmamem_free(sc->amr_ccb_dmat, sc->amr_ccb, sc->amr_ccb_dmamap);
503     if (sc->amr_ccb_dmat)
504         bus_dma_tag_destroy(sc->amr_ccb_dmat);
505
506     /* free and destroy DMA memory and tag for s/g lists */
507     if (sc->amr_sgtable)
508         bus_dmamem_free(sc->amr_sg_dmat, sc->amr_sgtable, sc->amr_sg_dmamap);
509     if (sc->amr_sg_dmat)
510         bus_dma_tag_destroy(sc->amr_sg_dmat);
511
512     /* free and destroy DMA memory and tag for mailbox */
513     p = (void *)(uintptr_t)(volatile void *)sc->amr_mailbox64;
514     if (sc->amr_mailbox) {
515         bus_dmamem_free(sc->amr_mailbox_dmat, p, sc->amr_mailbox_dmamap);
516     }
517     if (sc->amr_mailbox_dmat)
518         bus_dma_tag_destroy(sc->amr_mailbox_dmat);
519
520     /* disconnect the interrupt handler */
521     if (sc->amr_intr)
522         bus_teardown_intr(sc->amr_dev, sc->amr_irq, sc->amr_intr);
523     if (sc->amr_irq != NULL)
524         bus_release_resource(sc->amr_dev, SYS_RES_IRQ, 0, sc->amr_irq);
525
526     /* destroy the parent DMA tag */
527     if (sc->amr_parent_dmat)
528         bus_dma_tag_destroy(sc->amr_parent_dmat);
529
530     /* release the register window mapping */
531     if (sc->amr_reg != NULL)
532         bus_release_resource(sc->amr_dev,
533                              AMR_IS_QUARTZ(sc) ? SYS_RES_MEMORY : SYS_RES_IOPORT,
534                              PCIR_BAR(0), sc->amr_reg);
535 }
536
537 /********************************************************************************
538  * Allocate and map the scatter/gather table in bus space.
539  */
540 static void
541 amr_sglist_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
542 {
543     uint32_t *addr;
544
545     debug_called(1);
546
547     addr = arg;
548     *addr = segs[0].ds_addr;
549 }
550
551 static int
552 amr_sglist_map(struct amr_softc *sc)
553 {
554     size_t      segsize;
555     void        *p;
556     int         error;
557
558     debug_called(1);
559
560     /*
561      * Create a single tag describing a region large enough to hold all of
562      * the s/g lists we will need.
563      *
564      * Note that we could probably use AMR_LIMITCMD here, but that may become
565      * tunable.
566      */
567     if (AMR_IS_SG64(sc))
568         segsize = sizeof(struct amr_sg64entry) * AMR_NSEG * AMR_MAXCMD;
569     else
570         segsize = sizeof(struct amr_sgentry) * AMR_NSEG * AMR_MAXCMD;
571
572     error = bus_dma_tag_create(sc->amr_parent_dmat,     /* parent */
573                                512, 0,                  /* alignment,boundary */
574                                BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
575                                BUS_SPACE_MAXADDR,       /* highaddr */
576                                NULL, NULL,              /* filter, filterarg */
577                                segsize, 1,              /* maxsize, nsegments */
578                                BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
579                                0,                       /* flags */
580                                NULL, NULL,              /* lockfunc, lockarg */
581                                &sc->amr_sg_dmat);
582     if (error != 0) {
583         device_printf(sc->amr_dev, "can't allocate scatter/gather DMA tag\n");
584         return(ENOMEM);
585     }
586
587     /*
588      * Allocate enough s/g maps for all commands and permanently map them into
589      * controller-visible space.
590      *  
591      * XXX this assumes we can get enough space for all the s/g maps in one 
592      * contiguous slab.  We may need to switch to a more complex arrangement
593      * where we allocate in smaller chunks and keep a lookup table from slot
594      * to bus address.
595      *
596      * XXX HACK ALERT:  at least some controllers don't like the s/g memory
597      *                  being allocated below 0x2000.  We leak some memory if
598      *                  we get some below this mark and allocate again.  We
599      *                  should be able to avoid this with the tag setup, but
600      *                  that does't seem to work.
601      */
602 retry:
603     error = bus_dmamem_alloc(sc->amr_sg_dmat, (void **)&p, BUS_DMA_NOWAIT, &sc->amr_sg_dmamap);
604     if (error) {
605         device_printf(sc->amr_dev, "can't allocate s/g table\n");
606         return(ENOMEM);
607     }
608     bus_dmamap_load(sc->amr_sg_dmat, sc->amr_sg_dmamap, p, segsize, amr_sglist_helper, &sc->amr_sgbusaddr, 0);
609     if (sc->amr_sgbusaddr < 0x2000) {
610         debug(1, "s/g table too low (0x%x), reallocating\n", sc->amr_sgbusaddr);
611         goto retry;
612     }
613
614     if (AMR_IS_SG64(sc))
615         sc->amr_sg64table = (struct amr_sg64entry *)p;
616     sc->amr_sgtable = (struct amr_sgentry *)p;
617
618     return(0);
619 }
620
621 /********************************************************************************
622  * Allocate and set up mailbox areas for the controller (sc)
623  *
624  * The basic mailbox structure should be 16-byte aligned.
625  */
626 static int
627 amr_setup_mbox(struct amr_softc *sc)
628 {
629     int         error;
630     void        *p;
631     uint32_t    baddr;
632     
633     debug_called(1);
634
635     /*
636      * Create a single tag describing a region large enough to hold the entire
637      * mailbox.
638      */
639     error = bus_dma_tag_create(sc->amr_parent_dmat,     /* parent */
640                                16, 0,                   /* alignment,boundary */
641                                BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
642                                BUS_SPACE_MAXADDR,       /* highaddr */
643                                NULL, NULL,              /* filter, filterarg */
644                                sizeof(struct amr_mailbox64), /* maxsize */
645                                1,                       /* nsegments */
646                                BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
647                                0,                       /* flags */
648                                NULL, NULL,              /* lockfunc, lockarg */
649                                &sc->amr_mailbox_dmat);
650     if (error != 0) {
651         device_printf(sc->amr_dev, "can't allocate mailbox tag\n");
652         return(ENOMEM);
653     }
654
655     /*
656      * Allocate the mailbox structure and permanently map it into
657      * controller-visible space.
658      */
659     error = bus_dmamem_alloc(sc->amr_mailbox_dmat, (void **)&p, BUS_DMA_NOWAIT,
660                              &sc->amr_mailbox_dmamap);
661     if (error) {
662         device_printf(sc->amr_dev, "can't allocate mailbox memory\n");
663         return(ENOMEM);
664     }
665     bus_dmamap_load(sc->amr_mailbox_dmat, sc->amr_mailbox_dmamap, p,
666                     sizeof(struct amr_mailbox64), amr_sglist_helper, &baddr, 0);
667     /*
668      * Conventional mailbox is inside the mailbox64 region.
669      */
670     /* save physical base of the basic mailbox structure */
671     sc->amr_mailboxphys = baddr + offsetof(struct amr_mailbox64, mb);
672     bzero(p, sizeof(struct amr_mailbox64));
673     sc->amr_mailbox64 = (struct amr_mailbox64 *)p;
674     sc->amr_mailbox = &sc->amr_mailbox64->mb;
675
676     return(0);
677 }
678
679 static int
680 amr_ccb_map(struct amr_softc *sc)
681 {
682     int         ccbsize, error;
683
684     /*
685      * Passthrough and Extended passthrough structures will share the same
686      * memory.
687      */
688     ccbsize = sizeof(union amr_ccb) * AMR_MAXCMD;
689     error = bus_dma_tag_create(sc->amr_parent_dmat,     /* parent */
690                                 128, 0,                 /* alignment,boundary */
691                                 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
692                                 BUS_SPACE_MAXADDR,      /* highaddr */
693                                 NULL, NULL,             /* filter, filterarg */
694                                 ccbsize,                /* maxsize */
695                                 1,                      /* nsegments */
696                                 ccbsize,                /* maxsegsize */
697                                 0,                      /* flags */
698                                 NULL, NULL,             /* lockfunc, lockarg */
699                                 &sc->amr_ccb_dmat);
700     if (error != 0) {
701         device_printf(sc->amr_dev, "can't allocate ccb tag\n");
702         return (ENOMEM);
703     }
704
705     error = bus_dmamem_alloc(sc->amr_ccb_dmat, (void **)&sc->amr_ccb,
706                              BUS_DMA_NOWAIT, &sc->amr_ccb_dmamap);
707     if (error) {
708         device_printf(sc->amr_dev, "can't allocate ccb memory\n");
709         return (ENOMEM);
710     }
711     bus_dmamap_load(sc->amr_ccb_dmat, sc->amr_ccb_dmamap, sc->amr_ccb,
712                     ccbsize, amr_sglist_helper, &sc->amr_ccb_busaddr, 0);
713     bzero(sc->amr_ccb, ccbsize);
714
715     return (0);
716 }
717