]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ata/ata-dma.c
Dont use the BUS_DMA_ALLOCNOW flag. Instead use BUS_DMA_NOWAIT and return
[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->boundary = 128 * DEV_BSIZE;
81         ch->dma->segsize = 128 * DEV_BSIZE;
82         ch->dma->max_iosize = 128 * DEV_BSIZE;
83     }
84 }
85
86 static void
87 ata_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
88 {
89     struct ata_dc_cb_args *cba = (struct ata_dc_cb_args *)xsc;
90
91     if (!(cba->error = error))
92         cba->maddr = segs[0].ds_addr;
93 }
94
95 static void
96 ata_dmaalloc(device_t dev)
97 {
98     struct ata_channel *ch = device_get_softc(dev);
99     struct ata_dc_cb_args ccba;
100
101     if (bus_dma_tag_create(NULL, ch->dma->alignment, 0,
102                            BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
103                            NULL, NULL, ch->dma->max_iosize,
104                            ATA_DMA_ENTRIES, ch->dma->segsize,
105                            0, NULL, NULL, &ch->dma->dmatag))
106         goto error;
107
108     if (bus_dma_tag_create(ch->dma->dmatag, PAGE_SIZE, PAGE_SIZE,
109                            BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
110                            NULL, NULL, MAXTABSZ, 1, MAXTABSZ,
111                            0, NULL, NULL, &ch->dma->sg_tag))
112         goto error;
113
114     if (bus_dma_tag_create(ch->dma->dmatag,ch->dma->alignment,ch->dma->boundary,
115                            BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
116                            NULL, NULL, ch->dma->max_iosize,
117                            ATA_DMA_ENTRIES, ch->dma->segsize,
118                            0, NULL, NULL, &ch->dma->data_tag))
119         goto error;
120
121     if (bus_dmamem_alloc(ch->dma->sg_tag, (void **)&ch->dma->sg, 0,
122                          &ch->dma->sg_map))
123         goto error;
124
125     if (bus_dmamap_load(ch->dma->sg_tag, ch->dma->sg_map, ch->dma->sg,
126                         MAXTABSZ, ata_dmasetupc_cb, &ccba, 0) || ccba.error) {
127         bus_dmamem_free(ch->dma->sg_tag, ch->dma->sg, ch->dma->sg_map);
128         goto error;
129     }
130     ch->dma->sg_bus = ccba.maddr;
131
132     if (bus_dmamap_create(ch->dma->data_tag, 0, &ch->dma->data_map))
133         goto error;
134
135     if (bus_dma_tag_create(ch->dma->dmatag, PAGE_SIZE, 64 * 1024,
136                            BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
137                            NULL, NULL, MAXWSPCSZ, 1, MAXWSPCSZ,
138                            0, NULL, NULL, &ch->dma->work_tag))
139         goto error;
140
141     if (bus_dmamem_alloc(ch->dma->work_tag, (void **)&ch->dma->work, 0,
142                          &ch->dma->work_map))
143         goto error;
144
145     if (bus_dmamap_load(ch->dma->work_tag, ch->dma->work_map,ch->dma->work,
146                         MAXWSPCSZ, ata_dmasetupc_cb, &ccba, 0) || ccba.error) {
147         bus_dmamem_free(ch->dma->work_tag,ch->dma->work, ch->dma->work_map);
148         goto error;
149     }
150     ch->dma->work_bus = ccba.maddr;
151
152     return;
153
154 error:
155     device_printf(dev, "WARNING - DMA allocation failed, disabling DMA\n");
156     ata_dmafree(dev);
157     free(ch->dma, M_ATADMA);
158     ch->dma = NULL;
159 }
160
161 static void
162 ata_dmafree(device_t dev)
163 {
164     struct ata_channel *ch = device_get_softc(dev);
165
166     if (ch->dma->work_bus) {
167         bus_dmamap_unload(ch->dma->work_tag, ch->dma->work_map);
168         bus_dmamem_free(ch->dma->work_tag, ch->dma->work, ch->dma->work_map);
169         ch->dma->work_bus = 0;
170         ch->dma->work_map = NULL;
171         ch->dma->work = NULL;
172     }
173     if (ch->dma->work_tag) {
174         bus_dma_tag_destroy(ch->dma->work_tag);
175         ch->dma->work_tag = NULL;
176     }
177     if (ch->dma->sg_bus) {
178         bus_dmamap_unload(ch->dma->sg_tag, ch->dma->sg_map);
179         bus_dmamem_free(ch->dma->sg_tag, ch->dma->sg, ch->dma->sg_map);
180         ch->dma->sg_bus = 0;
181         ch->dma->sg_map = NULL;
182         ch->dma->sg = NULL;
183     }
184     if (ch->dma->data_map) {
185         bus_dmamap_destroy(ch->dma->data_tag, ch->dma->data_map);
186         ch->dma->data_map = NULL;
187     }
188     if (ch->dma->sg_tag) {
189         bus_dma_tag_destroy(ch->dma->sg_tag);
190         ch->dma->sg_tag = NULL;
191     }
192     if (ch->dma->data_tag) {
193         bus_dma_tag_destroy(ch->dma->data_tag);
194         ch->dma->data_tag = NULL;
195     }
196     if (ch->dma->dmatag) {
197         bus_dma_tag_destroy(ch->dma->dmatag);
198         ch->dma->dmatag = NULL;
199     }
200 }
201
202 static void
203 ata_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
204 {
205     struct ata_dmasetprd_args *args = xsc;
206     struct ata_dma_prdentry *prd = args->dmatab;
207     int i;
208
209     if ((args->error = error))
210         return;
211
212     for (i = 0; i < nsegs; i++) {
213         prd[i].addr = htole32(segs[i].ds_addr);
214         prd[i].count = htole32(segs[i].ds_len);
215     }
216     prd[i - 1].count |= htole32(ATA_DMA_EOT);
217     args->nsegs = nsegs;
218 }
219
220 static int
221 ata_dmaload(device_t dev, caddr_t data, int32_t count, int dir,
222             void *addr, int *entries)
223 {
224     struct ata_channel *ch = device_get_softc(dev);
225     struct ata_dmasetprd_args cba;
226     int error;
227
228     if (ch->dma->flags & ATA_DMA_LOADED) {
229         device_printf(dev, "FAILURE - already active DMA on this device\n");
230         return EIO;
231     }
232     if (!count) {
233         device_printf(dev, "FAILURE - zero length DMA transfer attempted\n");
234         return EIO;
235     }
236     if (((uintptr_t)data & (ch->dma->alignment - 1)) ||
237         (count & (ch->dma->alignment - 1))) {
238         device_printf(dev, "FAILURE - non aligned DMA transfer attempted\n");
239         return EIO;
240     }
241     if (count > ch->dma->max_iosize) {
242         device_printf(dev, "FAILURE - oversized DMA transfer attempt %d > %d\n",
243                       count, ch->dma->max_iosize);
244         return EIO;
245     }
246
247     cba.dmatab = addr;
248
249     if ((error = bus_dmamap_load(ch->dma->data_tag, ch->dma->data_map,
250                                  data, count, ch->dma->setprd, &cba,
251                                  BUS_DMA_NOWAIT)) || (error = cba.error))
252         return error;
253
254     *entries = cba.nsegs;
255
256     bus_dmamap_sync(ch->dma->sg_tag, ch->dma->sg_map, BUS_DMASYNC_PREWRITE);
257
258     bus_dmamap_sync(ch->dma->data_tag, ch->dma->data_map,
259                     dir ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
260
261     ch->dma->cur_iosize = count;
262     ch->dma->flags = dir ? (ATA_DMA_LOADED | ATA_DMA_READ) : ATA_DMA_LOADED;
263     return 0;
264 }
265
266 int
267 ata_dmaunload(device_t dev)
268 {
269     struct ata_channel *ch = device_get_softc(dev);
270
271     if (ch->dma->flags & ATA_DMA_LOADED) {
272         bus_dmamap_sync(ch->dma->sg_tag, ch->dma->sg_map,
273                         BUS_DMASYNC_POSTWRITE);
274
275         bus_dmamap_sync(ch->dma->data_tag, ch->dma->data_map,
276                         (ch->dma->flags & ATA_DMA_READ) != 0 ?
277                         BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
278         bus_dmamap_unload(ch->dma->data_tag, ch->dma->data_map);
279
280         ch->dma->cur_iosize = 0;
281         ch->dma->flags &= ~ATA_DMA_LOADED;
282     }
283     return 0;
284 }