]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ata/ata-dma.c
This commit was generated by cvs2svn to compensate for changes in r146899,
[FreeBSD/FreeBSD.git] / sys / dev / ata / ata-dma.c
1 /*-
2  * Copyright (c) 1998 - 2005 Søren Schmidt <sos@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
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  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/ata.h>
35 #include <sys/kernel.h>
36 #include <sys/endian.h>
37 #include <sys/malloc.h> 
38 #include <sys/lock.h>
39 #include <sys/sema.h>
40 #include <sys/taskqueue.h>
41 #include <vm/uma.h>
42 #include <sys/bus.h>
43 #include <machine/bus.h>
44 #include <sys/rman.h>
45 #include <dev/pci/pcivar.h>
46 #include <dev/ata/ata-all.h>
47 #include <dev/ata/ata-pci.h>
48
49 /* prototypes */
50 static void ata_dmaalloc(device_t);
51 static void ata_dmafree(device_t);
52 static void ata_dmasetprd(void *, bus_dma_segment_t *, int, int);
53 static int ata_dmaload(device_t, caddr_t, int32_t, int, void *, int *);
54 static int ata_dmaunload(device_t);
55
56 /* local vars */
57 static MALLOC_DEFINE(M_ATADMA, "ATA DMA", "ATA driver DMA");
58
59 /* misc defines */
60 #define MAXTABSZ        PAGE_SIZE
61 #define MAXWSPCSZ       PAGE_SIZE*2
62
63 struct ata_dc_cb_args {
64     bus_addr_t maddr;
65     int error;
66 };
67
68 void 
69 ata_dmainit(device_t dev)
70 {
71     struct ata_channel *ch = device_get_softc(dev);
72
73     if ((ch->dma = malloc(sizeof(struct ata_dma), M_ATADMA, M_NOWAIT|M_ZERO))) {
74         ch->dma->alloc = ata_dmaalloc;
75         ch->dma->free = ata_dmafree;
76         ch->dma->setprd = ata_dmasetprd;
77         ch->dma->load = ata_dmaload;
78         ch->dma->unload = ata_dmaunload;
79         ch->dma->alignment = 2;
80         ch->dma->max_iosize = 128 * DEV_BSIZE;
81         ch->dma->boundary = 128 * DEV_BSIZE;
82     }
83 }
84
85 static void
86 ata_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
87 {
88     struct ata_dc_cb_args *cba = (struct ata_dc_cb_args *)xsc;
89
90     if (!(cba->error = error))
91         cba->maddr = segs[0].ds_addr;
92 }
93
94 static void
95 ata_dmaalloc(device_t dev)
96 {
97     struct ata_channel *ch = device_get_softc(dev);
98     struct ata_dc_cb_args ccba;
99
100     if (bus_dma_tag_create(NULL, ch->dma->alignment, 0,
101                            BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
102                            NULL, NULL, 256 * DEV_BSIZE,
103                            ATA_DMA_ENTRIES, ch->dma->max_iosize,
104                            0, NULL, NULL, &ch->dma->dmatag))
105         goto error;
106
107     if (bus_dma_tag_create(ch->dma->dmatag, PAGE_SIZE, PAGE_SIZE,
108                            BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
109                            NULL, NULL, MAXTABSZ, 1, MAXTABSZ,
110                            0, NULL, NULL, &ch->dma->sg_tag))
111         goto error;
112
113     if (bus_dma_tag_create(ch->dma->dmatag,ch->dma->alignment,ch->dma->boundary,
114                            BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
115                            NULL, NULL, 256 * DEV_BSIZE,
116                            ATA_DMA_ENTRIES, ch->dma->max_iosize,
117                            BUS_DMA_ALLOCNOW, NULL, NULL, &ch->dma->data_tag))
118         goto error;
119
120     if (bus_dmamem_alloc(ch->dma->sg_tag, (void **)&ch->dma->sg, 0,
121                          &ch->dma->sg_map))
122         goto error;
123
124     if (bus_dmamap_load(ch->dma->sg_tag, ch->dma->sg_map, ch->dma->sg,
125                         MAXTABSZ, ata_dmasetupc_cb, &ccba, 0) || ccba.error) {
126         bus_dmamem_free(ch->dma->sg_tag, ch->dma->sg, ch->dma->sg_map);
127         goto error;
128     }
129     ch->dma->sg_bus = ccba.maddr;
130
131     if (bus_dmamap_create(ch->dma->data_tag, 0, &ch->dma->data_map))
132         goto error;
133
134     if (bus_dma_tag_create(ch->dma->dmatag, PAGE_SIZE, 64 * 1024,
135                            BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
136                            NULL, NULL, MAXWSPCSZ, 1, MAXWSPCSZ,
137                            0, NULL, NULL, &ch->dma->work_tag))
138         goto error;
139
140     if (bus_dmamem_alloc(ch->dma->work_tag, (void **)&ch->dma->work, 0,
141                          &ch->dma->work_map))
142         goto error;
143
144     if (bus_dmamap_load(ch->dma->work_tag, ch->dma->work_map,ch->dma->work,
145                         MAXWSPCSZ, ata_dmasetupc_cb, &ccba, 0) || ccba.error) {
146         bus_dmamem_free(ch->dma->work_tag,ch->dma->work, ch->dma->work_map);
147         goto error;
148     }
149     ch->dma->work_bus = ccba.maddr;
150
151     return;
152
153 error:
154     device_printf(dev, "WARNING - DMA allocation failed, disabling DMA\n");
155     ata_dmafree(dev);
156     free(ch->dma, M_ATADMA);
157     ch->dma = NULL;
158 }
159
160 static void
161 ata_dmafree(device_t dev)
162 {
163     struct ata_channel *ch = device_get_softc(dev);
164
165     if (ch->dma->work_bus) {
166         bus_dmamap_unload(ch->dma->work_tag, ch->dma->work_map);
167         bus_dmamem_free(ch->dma->work_tag, ch->dma->work, ch->dma->work_map);
168         ch->dma->work_bus = 0;
169         ch->dma->work_map = NULL;
170         ch->dma->work = NULL;
171     }
172     if (ch->dma->work_tag) {
173         bus_dma_tag_destroy(ch->dma->work_tag);
174         ch->dma->work_tag = NULL;
175     }
176     if (ch->dma->sg_bus) {
177         bus_dmamap_unload(ch->dma->sg_tag, ch->dma->sg_map);
178         bus_dmamem_free(ch->dma->sg_tag, ch->dma->sg, ch->dma->sg_map);
179         ch->dma->sg_bus = 0;
180         ch->dma->sg_map = NULL;
181         ch->dma->sg = NULL;
182     }
183     if (ch->dma->data_map) {
184         bus_dmamap_destroy(ch->dma->data_tag, ch->dma->data_map);
185         ch->dma->data_map = NULL;
186     }
187     if (ch->dma->sg_tag) {
188         bus_dma_tag_destroy(ch->dma->sg_tag);
189         ch->dma->sg_tag = NULL;
190     }
191     if (ch->dma->data_tag) {
192         bus_dma_tag_destroy(ch->dma->data_tag);
193         ch->dma->data_tag = NULL;
194     }
195     if (ch->dma->dmatag) {
196         bus_dma_tag_destroy(ch->dma->dmatag);
197         ch->dma->dmatag = NULL;
198     }
199 }
200
201 static void
202 ata_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
203 {
204     struct ata_dmasetprd_args *args = xsc;
205     struct ata_dma_prdentry *prd = args->dmatab;
206     int i;
207
208     if ((args->error = error))
209         return;
210
211     for (i = 0; i < nsegs; i++) {
212         prd[i].addr = htole32(segs[i].ds_addr);
213         prd[i].count = htole32(segs[i].ds_len);
214     }
215     prd[i - 1].count |= htole32(ATA_DMA_EOT);
216     args->nsegs = nsegs;
217 }
218
219 static int
220 ata_dmaload(device_t dev, caddr_t data, int32_t count, int dir,
221             void *addr, int *entries)
222 {
223     struct ata_channel *ch = device_get_softc(dev);
224     struct ata_dmasetprd_args cba;
225
226     if (ch->dma->flags & ATA_DMA_LOADED) {
227         device_printf(dev, "FAILURE - already active DMA on this device\n");
228         return -1;
229     }
230     if (!count) {
231         device_printf(dev, "FAILURE - zero length DMA transfer attempted\n");
232         return -1;
233     }
234     if (((uintptr_t)data & (ch->dma->alignment - 1)) ||
235         (count & (ch->dma->alignment - 1))) {
236         device_printf(dev, "FAILURE - non aligned DMA transfer attempted\n");
237         return -1;
238     }
239     if (count > ch->dma->max_iosize) {
240         device_printf(dev, "FAILURE - oversized DMA transfer attempt %d > %d\n",
241                       count, ch->dma->max_iosize);
242         return -1;
243     }
244
245     cba.dmatab = addr;
246
247     if (bus_dmamap_load(ch->dma->data_tag, ch->dma->data_map, data, count,
248                         ch->dma->setprd, &cba, 0) || cba.error)
249         return -1;
250
251     *entries = cba.nsegs;
252
253     bus_dmamap_sync(ch->dma->sg_tag, ch->dma->sg_map, BUS_DMASYNC_PREWRITE);
254
255     bus_dmamap_sync(ch->dma->data_tag, ch->dma->data_map,
256                     dir ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
257
258     ch->dma->cur_iosize = count;
259     ch->dma->flags = dir ? (ATA_DMA_LOADED | ATA_DMA_READ) : ATA_DMA_LOADED;
260     return 0;
261 }
262
263 int
264 ata_dmaunload(device_t dev)
265 {
266     struct ata_channel *ch = device_get_softc(dev);
267
268     if (ch->dma->flags & ATA_DMA_LOADED) {
269         bus_dmamap_sync(ch->dma->sg_tag, ch->dma->sg_map,
270                         BUS_DMASYNC_POSTWRITE);
271
272         bus_dmamap_sync(ch->dma->data_tag, ch->dma->data_map,
273                         (ch->dma->flags & ATA_DMA_READ) != 0 ?
274                         BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
275         bus_dmamap_unload(ch->dma->data_tag, ch->dma->data_map);
276
277         ch->dma->cur_iosize = 0;
278         ch->dma->flags &= ~ATA_DMA_LOADED;
279     }
280     return 0;
281 }