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