2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/endian.h>
37 #include <sys/malloc.h>
40 #include <sys/taskqueue.h>
43 #include <machine/bus.h>
45 #include <dev/ata/ata-all.h>
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);
56 static MALLOC_DEFINE(M_ATADMA, "ata_dma", "ATA driver DMA");
59 #define MAXTABSZ PAGE_SIZE
60 #define MAXWSPCSZ PAGE_SIZE*2
62 struct ata_dc_cb_args {
68 ata_dmainit(device_t dev)
70 struct ata_channel *ch = device_get_softc(dev);
71 struct ata_dc_cb_args dcba;
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;
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))
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))
109 if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work,
110 BUS_DMA_WAITOK | BUS_DMA_COHERENT,
114 if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work,
115 MAXWSPCSZ, ata_dmasetupc_cb, &dcba, 0) ||
117 bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
120 ch->dma.work_bus = dcba.maddr;
124 device_printf(dev, "WARNING - DMA initialization failed, disabling DMA\n");
129 ata_dmafini(device_t dev)
131 struct ata_channel *ch = device_get_softc(dev);
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;
139 if (ch->dma.work_tag) {
140 bus_dma_tag_destroy(ch->dma.work_tag);
141 ch->dma.work_tag = NULL;
143 if (ch->dma.dmatag) {
144 bus_dma_tag_destroy(ch->dma.dmatag);
145 ch->dma.dmatag = NULL;
150 ata_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
152 struct ata_dc_cb_args *dcba = (struct ata_dc_cb_args *)xsc;
154 if (!(dcba->error = error))
155 dcba->maddr = segs[0].ds_addr;
159 ata_dmaalloc(device_t dev)
161 struct ata_channel *ch = device_get_softc(dev);
162 struct ata_dc_cb_args dcba;
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];
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");
178 if (bus_dmamem_alloc(slot->sg_tag, (void **)&slot->sg, BUS_DMA_WAITOK,
180 device_printf(ch->dev, "FAILURE - alloc sg_map\n");
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");
189 slot->sg_bus = dcba.maddr;
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");
201 if (bus_dmamap_create(slot->data_tag, 0, &slot->data_map)) {
202 device_printf(ch->dev, "FAILURE - create data_map\n");
210 device_printf(dev, "WARNING - DMA allocation failed, disabling DMA\n");
215 ata_dmafree(device_t dev)
217 struct ata_channel *ch = device_get_softc(dev);
220 /* free all dma slots */
221 for (i = 0; i < ATA_DMA_SLOTS; i++) {
222 struct ata_dmaslot *slot = &ch->dma.slot[i];
225 bus_dmamap_unload(slot->sg_tag, slot->sg_map);
229 bus_dmamem_free(slot->sg_tag, slot->sg, slot->sg_map);
232 if (slot->data_map) {
233 bus_dmamap_destroy(slot->data_tag, slot->data_map);
234 slot->data_map = NULL;
237 bus_dma_tag_destroy(slot->sg_tag);
240 if (slot->data_tag) {
241 bus_dma_tag_destroy(slot->data_tag);
242 slot->data_tag = NULL;
248 ata_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
250 struct ata_dmasetprd_args *args = xsc;
251 struct ata_dma_prdentry *prd = args->dmatab;
254 if ((args->error = error))
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);
261 prd[i - 1].count |= htole32(ATA_DMA_EOT);
262 KASSERT(nsegs <= ATA_DMA_ENTRIES, ("too many DMA segment entries\n"));
267 ata_dmaload(struct ata_request *request, void *addr, int *entries)
269 struct ata_channel *ch = device_get_softc(request->parent);
270 struct ata_dmasetprd_args dspa;
273 ATA_DEBUG_RQ(request, "dmaload");
276 device_printf(request->parent,
277 "FAILURE - already active DMA on this device\n");
280 if (!request->bytecount) {
281 device_printf(request->parent,
282 "FAILURE - zero length DMA transfer attempted\n");
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);
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);
298 /* set our slot. XXX SOS NCQ will change that */
299 request->dma = &ch->dma.slot[0];
304 dspa.dmatab = request->dma->sg;
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);
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");
320 *entries = dspa.nsegs;
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);
330 ata_dmaunload(request);
335 ata_dmaunload(struct ata_request *request)
337 ATA_DEBUG_RQ(request, "dmaunload");
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);
346 bus_dmamap_unload(request->dma->data_tag, request->dma->data_map);