xref: /freebsd/sys/dev/ata/ata-dma.c (revision d5fc25e5d6c52b306312784663ccad85923a9c76)
1 /*-
2  * Copyright (c) 1998 - 2008 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_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
49 static void ata_dmaalloc(device_t dev);
50 static void ata_dmafree(device_t dev);
51 static void ata_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
52 static int ata_dmaload(struct ata_request *request, void *addr, int *nsegs);
53 static int ata_dmaunload(struct ata_request *request);
54 
55 /* local vars */
56 static MALLOC_DEFINE(M_ATADMA, "ata_dma", "ATA driver DMA");
57 
58 /* misc defines */
59 #define MAXTABSZ        PAGE_SIZE
60 #define MAXWSPCSZ       PAGE_SIZE*2
61 
62 struct ata_dc_cb_args {
63     bus_addr_t maddr;
64     int error;
65 };
66 
67 void
68 ata_dmainit(device_t dev)
69 {
70     struct ata_channel *ch = device_get_softc(dev);
71     struct ata_dc_cb_args dcba;
72 
73     ch->dma.alloc = ata_dmaalloc;
74     ch->dma.free = ata_dmafree;
75     ch->dma.setprd = ata_dmasetprd;
76     ch->dma.load = ata_dmaload;
77     ch->dma.unload = ata_dmaunload;
78     ch->dma.alignment = 2;
79     ch->dma.boundary = 65536;
80     ch->dma.segsize = 65536;
81     ch->dma.max_iosize = 128 * DEV_BSIZE;
82     ch->dma.max_address = BUS_SPACE_MAXADDR_32BIT;
83     ch->dma.dma_slots = 6;
84 
85     if (bus_dma_tag_create(bus_get_dma_tag(dev), ch->dma.alignment, 0,
86 			   ch->dma.max_address, BUS_SPACE_MAXADDR,
87 			   NULL, NULL, ch->dma.max_iosize,
88 			   ATA_DMA_ENTRIES, ch->dma.segsize,
89 			   0, NULL, NULL, &ch->dma.dmatag))
90 	goto error;
91 
92     if (bus_dma_tag_create(ch->dma.dmatag, PAGE_SIZE, 64 * 1024,
93 			   ch->dma.max_address, BUS_SPACE_MAXADDR,
94 			   NULL, NULL, MAXWSPCSZ, 1, MAXWSPCSZ,
95 			   0, NULL, NULL, &ch->dma.work_tag))
96 	goto error;
97 
98     if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work, 0,
99 			 &ch->dma.work_map))
100 	goto error;
101 
102     if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work,
103 			MAXWSPCSZ, ata_dmasetupc_cb, &dcba, 0) ||
104 			dcba.error) {
105 	bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
106 	goto error;
107     }
108     ch->dma.work_bus = dcba.maddr;
109     return;
110 
111 error:
112     device_printf(dev, "WARNING - DMA initialization failed, disabling DMA\n");
113     ata_dmafini(dev);
114 }
115 
116 void
117 ata_dmafini(device_t dev)
118 {
119     struct ata_channel *ch = device_get_softc(dev);
120 
121     if (ch->dma.work_bus) {
122 	bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map);
123 	bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
124 	ch->dma.work_bus = 0;
125 	ch->dma.work_map = NULL;
126 	ch->dma.work = NULL;
127     }
128     if (ch->dma.work_tag) {
129 	bus_dma_tag_destroy(ch->dma.work_tag);
130 	ch->dma.work_tag = NULL;
131     }
132     if (ch->dma.dmatag) {
133 	bus_dma_tag_destroy(ch->dma.dmatag);
134 	ch->dma.dmatag = NULL;
135     }
136 }
137 
138 static void
139 ata_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
140 {
141     struct ata_dc_cb_args *dcba = (struct ata_dc_cb_args *)xsc;
142 
143     if (!(dcba->error = error))
144 	dcba->maddr = segs[0].ds_addr;
145 }
146 
147 static void
148 ata_dmaalloc(device_t dev)
149 {
150     struct ata_channel *ch = device_get_softc(dev);
151     struct ata_dc_cb_args dcba;
152     int i;
153 
154     /* alloc and setup needed dma slots */
155     bzero(ch->dma.slot, sizeof(struct ata_dmaslot) * ATA_DMA_SLOTS);
156     for (i = 0; i < ch->dma.dma_slots; i++) {
157 	struct ata_dmaslot *slot = &ch->dma.slot[i];
158 
159 	if (bus_dma_tag_create(ch->dma.dmatag, PAGE_SIZE, PAGE_SIZE,
160 			       ch->dma.max_address, BUS_SPACE_MAXADDR,
161 			       NULL, NULL, PAGE_SIZE, 1, PAGE_SIZE,
162 			       0, NULL, NULL, &slot->sg_tag)) {
163             device_printf(ch->dev, "FAILURE - create sg_tag\n");
164             goto error;
165 	}
166 
167 	if (bus_dmamem_alloc(slot->sg_tag, (void **)&slot->sg,
168 			     0, &slot->sg_map)) {
169 	    device_printf(ch->dev, "FAILURE - alloc sg_map\n");
170 	    goto error;
171         }
172 
173 	if (bus_dmamap_load(slot->sg_tag, slot->sg_map, slot->sg, MAXTABSZ,
174 			    ata_dmasetupc_cb, &dcba, 0) || dcba.error) {
175 	    device_printf(ch->dev, "FAILURE - load sg\n");
176 	    goto error;
177 	}
178 	slot->sg_bus = dcba.maddr;
179 
180 	if (bus_dma_tag_create(ch->dma.dmatag,
181 			       ch->dma.alignment, ch->dma.boundary,
182                                ch->dma.max_address, BUS_SPACE_MAXADDR,
183                                NULL, NULL, ch->dma.max_iosize,
184                                ATA_DMA_ENTRIES, ch->dma.segsize,
185                                BUS_DMA_ALLOCNOW, NULL, NULL, &slot->data_tag)) {
186 	    device_printf(ch->dev, "FAILURE - create data_tag\n");
187 	    goto error;
188 	}
189 
190 	if (bus_dmamap_create(slot->data_tag, 0, &slot->data_map)) {
191 	    device_printf(ch->dev, "FAILURE - create data_map\n");
192 	    goto error;
193         }
194     }
195 
196     return;
197 
198 error:
199     device_printf(dev, "WARNING - DMA allocation failed, disabling DMA\n");
200     ata_dmafree(dev);
201 }
202 
203 static void
204 ata_dmafree(device_t dev)
205 {
206     struct ata_channel *ch = device_get_softc(dev);
207     int i;
208 
209     /* free all dma slots */
210     for (i = 0; i < ATA_DMA_SLOTS; i++) {
211 	struct ata_dmaslot *slot = &ch->dma.slot[i];
212 
213 	if (slot->sg_bus) {
214             bus_dmamap_unload(slot->sg_tag, slot->sg_map);
215             slot->sg_bus = 0;
216 	}
217 	if (slot->sg_map) {
218             bus_dmamem_free(slot->sg_tag, slot->sg, slot->sg_map);
219             bus_dmamap_destroy(slot->sg_tag, slot->sg_map);
220             slot->sg = NULL;
221             slot->sg_map = NULL;
222 	}
223 	if (slot->data_map) {
224             bus_dmamap_destroy(slot->data_tag, slot->data_map);
225             slot->data_map = NULL;
226 	}
227 	if (slot->sg_tag) {
228             bus_dma_tag_destroy(slot->sg_tag);
229             slot->sg_tag = NULL;
230 	}
231 	if (slot->data_tag) {
232             bus_dma_tag_destroy(slot->data_tag);
233             slot->data_tag = NULL;
234 	}
235     }
236 }
237 
238 static void
239 ata_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
240 {
241     struct ata_dmasetprd_args *args = xsc;
242     struct ata_dma_prdentry *prd = args->dmatab;
243     int i;
244 
245     if ((args->error = error))
246 	return;
247 
248     for (i = 0; i < nsegs; i++) {
249 	prd[i].addr = htole32(segs[i].ds_addr);
250 	prd[i].count = htole32(segs[i].ds_len);
251     }
252     prd[i - 1].count |= htole32(ATA_DMA_EOT);
253     KASSERT(nsegs <= ATA_DMA_ENTRIES, ("too many DMA segment entries\n"));
254     args->nsegs = nsegs;
255 }
256 
257 static int
258 ata_dmaload(struct ata_request *request, void *addr, int *entries)
259 {
260     struct ata_channel *ch = device_get_softc(request->parent);
261     struct ata_device *atadev = device_get_softc(request->dev);
262     struct ata_dmasetprd_args dspa;
263     int error;
264 
265     ATA_DEBUG_RQ(request, "dmaload");
266 
267     if (request->dma) {
268 	device_printf(request->dev,
269 		      "FAILURE - already active DMA on this device\n");
270 	return EIO;
271     }
272     if (!request->bytecount) {
273 	device_printf(request->dev,
274 		      "FAILURE - zero length DMA transfer attempted\n");
275 	return EIO;
276     }
277     if (((uintptr_t)(request->data) & (ch->dma.alignment - 1)) ||
278 	(request->bytecount & (ch->dma.alignment - 1))) {
279 	device_printf(request->dev,
280 		      "FAILURE - non aligned DMA transfer attempted\n");
281 	return EIO;
282     }
283     if (request->bytecount > ch->dma.max_iosize) {
284 	device_printf(request->dev,
285 		      "FAILURE - oversized DMA transfer attempt %d > %d\n",
286 		      request->bytecount, ch->dma.max_iosize);
287 	return EIO;
288     }
289 
290     /* set our slot, unit for simplicity XXX SOS NCQ will change that */
291     request->dma = &ch->dma.slot[atadev->unit];
292 
293     if (addr)
294 	dspa.dmatab = addr;
295     else
296 	dspa.dmatab = request->dma->sg;
297 
298     if ((error = bus_dmamap_load(request->dma->data_tag, request->dma->data_map,
299 				 request->data, request->bytecount,
300 				 ch->dma.setprd, &dspa, BUS_DMA_NOWAIT)) ||
301 				 (error = dspa.error)) {
302 	device_printf(request->dev, "FAILURE - load data\n");
303 	goto error;
304     }
305 
306     if (entries)
307 	*entries = dspa.nsegs;
308 
309     bus_dmamap_sync(request->dma->sg_tag, request->dma->sg_map,
310 		    BUS_DMASYNC_PREWRITE);
311     bus_dmamap_sync(request->dma->data_tag, request->dma->data_map,
312 		    (request->flags & ATA_R_READ) ?
313 		    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
314     return 0;
315 
316 error:
317     ata_dmaunload(request);
318     return EIO;
319 }
320 
321 int
322 ata_dmaunload(struct ata_request *request)
323 {
324     ATA_DEBUG_RQ(request, "dmaunload");
325 
326     if (request->dma) {
327 	bus_dmamap_sync(request->dma->sg_tag, request->dma->sg_map,
328 			BUS_DMASYNC_POSTWRITE);
329 	bus_dmamap_sync(request->dma->data_tag, request->dma->data_map,
330 			(request->flags & ATA_R_READ) ?
331 			BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
332 
333 	bus_dmamap_unload(request->dma->data_tag, request->dma->data_map);
334         request->dma = NULL;
335     }
336     return 0;
337 }
338