FreeBSD/Linux Kernel Cross Reference
sys/dev/ata/ata-dma.c
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
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/ata/ata-all.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 if (ch->dma.alloc == NULL)
74 ch->dma.alloc = ata_dmaalloc;
75 if (ch->dma.free == NULL)
76 ch->dma.free = ata_dmafree;
77 if (ch->dma.setprd == NULL)
78 ch->dma.setprd = ata_dmasetprd;
79 if (ch->dma.load == NULL)
80 ch->dma.load = ata_dmaload;
81 if (ch->dma.unload == NULL)
82 ch->dma.unload = ata_dmaunload;
83 if (ch->dma.alignment == 0)
84 ch->dma.alignment = 2;
85 if (ch->dma.boundary == 0)
86 ch->dma.boundary = 65536;
87 if (ch->dma.segsize == 0)
88 ch->dma.segsize = 65536;
89 if (ch->dma.max_iosize == 0)
90 ch->dma.max_iosize = (ATA_DMA_ENTRIES - 1) * PAGE_SIZE;
91 if (ch->dma.max_address == 0)
92 ch->dma.max_address = BUS_SPACE_MAXADDR_32BIT;
93 if (ch->dma.dma_slots == 0)
94 ch->dma.dma_slots = 1;
95
96 if (bus_dma_tag_create(bus_get_dma_tag(dev), ch->dma.alignment, 0,
97 ch->dma.max_address, BUS_SPACE_MAXADDR,
98 NULL, NULL, ch->dma.max_iosize,
99 ATA_DMA_ENTRIES, ch->dma.segsize,
100 0, NULL, NULL, &ch->dma.dmatag))
101 goto error;
102
103 if (bus_dma_tag_create(ch->dma.dmatag, PAGE_SIZE, 64 * 1024,
104 ch->dma.max_address, BUS_SPACE_MAXADDR,
105 NULL, NULL, MAXWSPCSZ, 1, MAXWSPCSZ,
106 0, NULL, NULL, &ch->dma.work_tag))
107 goto error;
108
109 if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work,
110 BUS_DMA_WAITOK | BUS_DMA_COHERENT,
111 &ch->dma.work_map))
112 goto error;
113
114 if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work,
115 MAXWSPCSZ, ata_dmasetupc_cb, &dcba, 0) ||
116 dcba.error) {
117 bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
118 goto error;
119 }
120 ch->dma.work_bus = dcba.maddr;
121 return;
122
123 error:
124 device_printf(dev, "WARNING - DMA initialization failed, disabling DMA\n");
125 ata_dmafini(dev);
126 }
127
128 void
129 ata_dmafini(device_t dev)
130 {
131 struct ata_channel *ch = device_get_softc(dev);
132
133 if (ch->dma.work_bus) {
134 bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map);
135 bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
136 ch->dma.work_bus = 0;
137 ch->dma.work = NULL;
138 }
139 if (ch->dma.work_tag) {
140 bus_dma_tag_destroy(ch->dma.work_tag);
141 ch->dma.work_tag = NULL;
142 }
143 if (ch->dma.dmatag) {
144 bus_dma_tag_destroy(ch->dma.dmatag);
145 ch->dma.dmatag = NULL;
146 }
147 }
148
149 static void
150 ata_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
151 {
152 struct ata_dc_cb_args *dcba = (struct ata_dc_cb_args *)xsc;
153
154 if (!(dcba->error = error))
155 dcba->maddr = segs[0].ds_addr;
156 }
157
158 static void
159 ata_dmaalloc(device_t dev)
160 {
161 struct ata_channel *ch = device_get_softc(dev);
162 struct ata_dc_cb_args dcba;
163 int i;
164
165 /* alloc and setup needed dma slots */
166 bzero(ch->dma.slot, sizeof(struct ata_dmaslot) * ATA_DMA_SLOTS);
167 for (i = 0; i < ch->dma.dma_slots; i++) {
168 struct ata_dmaslot *slot = &ch->dma.slot[i];
169
170 if (bus_dma_tag_create(ch->dma.dmatag, PAGE_SIZE, PAGE_SIZE,
171 ch->dma.max_address, BUS_SPACE_MAXADDR,
172 NULL, NULL, PAGE_SIZE, 1, PAGE_SIZE,
173 0, NULL, NULL, &slot->sg_tag)) {
174 device_printf(ch->dev, "FAILURE - create sg_tag\n");
175 goto error;
176 }
177
178 if (bus_dmamem_alloc(slot->sg_tag, (void **)&slot->sg, BUS_DMA_WAITOK,
179 &slot->sg_map)) {
180 device_printf(ch->dev, "FAILURE - alloc sg_map\n");
181 goto error;
182 }
183
184 if (bus_dmamap_load(slot->sg_tag, slot->sg_map, slot->sg, MAXTABSZ,
185 ata_dmasetupc_cb, &dcba, 0) || dcba.error) {
186 device_printf(ch->dev, "FAILURE - load sg\n");
187 goto error;
188 }
189 slot->sg_bus = dcba.maddr;
190
191 if (bus_dma_tag_create(ch->dma.dmatag,
192 ch->dma.alignment, ch->dma.boundary,
193 ch->dma.max_address, BUS_SPACE_MAXADDR,
194 NULL, NULL, ch->dma.max_iosize,
195 ATA_DMA_ENTRIES, ch->dma.segsize,
196 BUS_DMA_ALLOCNOW, NULL, NULL, &slot->data_tag)) {
197 device_printf(ch->dev, "FAILURE - create data_tag\n");
198 goto error;
199 }
200
201 if (bus_dmamap_create(slot->data_tag, 0, &slot->data_map)) {
202 device_printf(ch->dev, "FAILURE - create data_map\n");
203 goto error;
204 }
205 }
206
207 return;
208
209 error:
210 device_printf(dev, "WARNING - DMA allocation failed, disabling DMA\n");
211 ata_dmafree(dev);
212 }
213
214 static void
215 ata_dmafree(device_t dev)
216 {
217 struct ata_channel *ch = device_get_softc(dev);
218 int i;
219
220 /* free all dma slots */
221 for (i = 0; i < ATA_DMA_SLOTS; i++) {
222 struct ata_dmaslot *slot = &ch->dma.slot[i];
223
224 if (slot->sg_bus) {
225 bus_dmamap_unload(slot->sg_tag, slot->sg_map);
226 slot->sg_bus = 0;
227 }
228 if (slot->sg) {
229 bus_dmamem_free(slot->sg_tag, slot->sg, slot->sg_map);
230 slot->sg = NULL;
231 }
232 if (slot->data_map) {
233 bus_dmamap_destroy(slot->data_tag, slot->data_map);
234 slot->data_map = NULL;
235 }
236 if (slot->sg_tag) {
237 bus_dma_tag_destroy(slot->sg_tag);
238 slot->sg_tag = NULL;
239 }
240 if (slot->data_tag) {
241 bus_dma_tag_destroy(slot->data_tag);
242 slot->data_tag = NULL;
243 }
244 }
245 }
246
247 static void
248 ata_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
249 {
250 struct ata_dmasetprd_args *args = xsc;
251 struct ata_dma_prdentry *prd = args->dmatab;
252 int i;
253
254 if ((args->error = error))
255 return;
256
257 for (i = 0; i < nsegs; i++) {
258 prd[i].addr = htole32(segs[i].ds_addr);
259 prd[i].count = htole32(segs[i].ds_len);
260 }
261 prd[i - 1].count |= htole32(ATA_DMA_EOT);
262 KASSERT(nsegs <= ATA_DMA_ENTRIES, ("too many DMA segment entries\n"));
263 args->nsegs = nsegs;
264 }
265
266 static int
267 ata_dmaload(struct ata_request *request, void *addr, int *entries)
268 {
269 struct ata_channel *ch = device_get_softc(request->parent);
270 struct ata_dmasetprd_args dspa;
271 int error;
272
273 ATA_DEBUG_RQ(request, "dmaload");
274
275 if (request->dma) {
276 device_printf(request->parent,
277 "FAILURE - already active DMA on this device\n");
278 return EIO;
279 }
280 if (!request->bytecount) {
281 device_printf(request->parent,
282 "FAILURE - zero length DMA transfer attempted\n");
283 return EIO;
284 }
285 if (request->bytecount & (ch->dma.alignment - 1)) {
286 device_printf(request->parent,
287 "FAILURE - odd-sized DMA transfer attempt %d %% %d\n",
288 request->bytecount, ch->dma.alignment);
289 return EIO;
290 }
291 if (request->bytecount > ch->dma.max_iosize) {
292 device_printf(request->parent,
293 "FAILURE - oversized DMA transfer attempt %d > %d\n",
294 request->bytecount, ch->dma.max_iosize);
295 return EIO;
296 }
297
298 /* set our slot. XXX SOS NCQ will change that */
299 request->dma = &ch->dma.slot[0];
300
301 if (addr)
302 dspa.dmatab = addr;
303 else
304 dspa.dmatab = request->dma->sg;
305
306 if (request->flags & ATA_R_DATA_IN_CCB)
307 error = bus_dmamap_load_ccb(request->dma->data_tag,
308 request->dma->data_map, request->ccb,
309 ch->dma.setprd, &dspa, BUS_DMA_NOWAIT);
310 else
311 error = bus_dmamap_load(request->dma->data_tag, request->dma->data_map,
312 request->data, request->bytecount,
313 ch->dma.setprd, &dspa, BUS_DMA_NOWAIT);
314 if (error || (error = dspa.error)) {
315 device_printf(request->parent, "FAILURE - load data\n");
316 goto error;
317 }
318
319 if (entries)
320 *entries = dspa.nsegs;
321
322 bus_dmamap_sync(request->dma->sg_tag, request->dma->sg_map,
323 BUS_DMASYNC_PREWRITE);
324 bus_dmamap_sync(request->dma->data_tag, request->dma->data_map,
325 (request->flags & ATA_R_READ) ?
326 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
327 return 0;
328
329 error:
330 ata_dmaunload(request);
331 return EIO;
332 }
333
334 int
335 ata_dmaunload(struct ata_request *request)
336 {
337 ATA_DEBUG_RQ(request, "dmaunload");
338
339 if (request->dma) {
340 bus_dmamap_sync(request->dma->sg_tag, request->dma->sg_map,
341 BUS_DMASYNC_POSTWRITE);
342 bus_dmamap_sync(request->dma->data_tag, request->dma->data_map,
343 (request->flags & ATA_R_READ) ?
344 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
345
346 bus_dmamap_unload(request->dma->data_tag, request->dma->data_map);
347 request->dma = NULL;
348 }
349 return 0;
350 }
Cache object: 20c794ec5ba0aa4478fc10579234060a
|