ata-dma.c revision 216013
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: head/sys/dev/ata/ata-dma.c 216013 2010-11-28 18:53:29Z marius $");
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/ata/ata-all.h>
44
45/* prototypes */
46static void ata_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
47static void ata_dmaalloc(device_t dev);
48static void ata_dmafree(device_t dev);
49static void ata_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
50static int ata_dmaload(struct ata_request *request, void *addr, int *nsegs);
51static int ata_dmaunload(struct ata_request *request);
52
53/* local vars */
54static MALLOC_DEFINE(M_ATADMA, "ata_dma", "ATA driver DMA");
55
56/* misc defines */
57#define MAXTABSZ        PAGE_SIZE
58#define MAXWSPCSZ       PAGE_SIZE*2
59
60struct ata_dc_cb_args {
61    bus_addr_t maddr;
62    int error;
63};
64
65void
66ata_dmainit(device_t dev)
67{
68    struct ata_channel *ch = device_get_softc(dev);
69    struct ata_dc_cb_args dcba;
70
71    if (ch->dma.alloc == NULL)
72	ch->dma.alloc = ata_dmaalloc;
73    if (ch->dma.free == NULL)
74	ch->dma.free = ata_dmafree;
75    if (ch->dma.setprd == NULL)
76	ch->dma.setprd = ata_dmasetprd;
77    if (ch->dma.load == NULL)
78	ch->dma.load = ata_dmaload;
79    if (ch->dma.unload == NULL)
80	ch->dma.unload = ata_dmaunload;
81    if (ch->dma.alignment == 0)
82	ch->dma.alignment = 2;
83    if (ch->dma.boundary == 0)
84	ch->dma.boundary = 65536;
85    if (ch->dma.segsize == 0)
86	ch->dma.segsize = 65536;
87    if (ch->dma.max_iosize == 0)
88	ch->dma.max_iosize = MIN((ATA_DMA_ENTRIES - 1) * PAGE_SIZE, MAXPHYS);
89    if (ch->dma.max_address == 0)
90	ch->dma.max_address = BUS_SPACE_MAXADDR_32BIT;
91    if (ch->dma.dma_slots == 0)
92	ch->dma.dma_slots = 1;
93
94    if (bus_dma_tag_create(bus_get_dma_tag(dev), ch->dma.alignment, 0,
95			   ch->dma.max_address, BUS_SPACE_MAXADDR,
96			   NULL, NULL, ch->dma.max_iosize,
97			   ATA_DMA_ENTRIES, ch->dma.segsize,
98			   0, NULL, NULL, &ch->dma.dmatag))
99	goto error;
100
101    if (bus_dma_tag_create(ch->dma.dmatag, PAGE_SIZE, 64 * 1024,
102			   ch->dma.max_address, BUS_SPACE_MAXADDR,
103			   NULL, NULL, MAXWSPCSZ, 1, MAXWSPCSZ,
104			   0, NULL, NULL, &ch->dma.work_tag))
105	goto error;
106
107    if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work, 0,
108			 &ch->dma.work_map))
109	goto error;
110
111    if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work,
112			MAXWSPCSZ, ata_dmasetupc_cb, &dcba, 0) ||
113			dcba.error) {
114	bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
115	goto error;
116    }
117    ch->dma.work_bus = dcba.maddr;
118    return;
119
120error:
121    device_printf(dev, "WARNING - DMA initialization failed, disabling DMA\n");
122    ata_dmafini(dev);
123}
124
125void
126ata_dmafini(device_t dev)
127{
128    struct ata_channel *ch = device_get_softc(dev);
129
130    if (ch->dma.work_bus) {
131	bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map);
132	bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
133	ch->dma.work_bus = 0;
134	ch->dma.work_map = NULL;
135	ch->dma.work = NULL;
136    }
137    if (ch->dma.work_tag) {
138	bus_dma_tag_destroy(ch->dma.work_tag);
139	ch->dma.work_tag = NULL;
140    }
141    if (ch->dma.dmatag) {
142	bus_dma_tag_destroy(ch->dma.dmatag);
143	ch->dma.dmatag = NULL;
144    }
145}
146
147static void
148ata_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
149{
150    struct ata_dc_cb_args *dcba = (struct ata_dc_cb_args *)xsc;
151
152    if (!(dcba->error = error))
153	dcba->maddr = segs[0].ds_addr;
154}
155
156static void
157ata_dmaalloc(device_t dev)
158{
159    struct ata_channel *ch = device_get_softc(dev);
160    struct ata_dc_cb_args dcba;
161    int i;
162
163    /* alloc and setup needed dma slots */
164    bzero(ch->dma.slot, sizeof(struct ata_dmaslot) * ATA_DMA_SLOTS);
165    for (i = 0; i < ch->dma.dma_slots; i++) {
166	struct ata_dmaslot *slot = &ch->dma.slot[i];
167
168	if (bus_dma_tag_create(ch->dma.dmatag, PAGE_SIZE, PAGE_SIZE,
169			       ch->dma.max_address, BUS_SPACE_MAXADDR,
170			       NULL, NULL, PAGE_SIZE, 1, PAGE_SIZE,
171			       0, NULL, NULL, &slot->sg_tag)) {
172            device_printf(ch->dev, "FAILURE - create sg_tag\n");
173            goto error;
174	}
175
176	if (bus_dmamem_alloc(slot->sg_tag, (void **)&slot->sg,
177			     0, &slot->sg_map)) {
178	    device_printf(ch->dev, "FAILURE - alloc sg_map\n");
179	    goto error;
180        }
181
182	if (bus_dmamap_load(slot->sg_tag, slot->sg_map, slot->sg, MAXTABSZ,
183			    ata_dmasetupc_cb, &dcba, 0) || dcba.error) {
184	    device_printf(ch->dev, "FAILURE - load sg\n");
185	    goto error;
186	}
187	slot->sg_bus = dcba.maddr;
188
189	if (bus_dma_tag_create(ch->dma.dmatag,
190			       ch->dma.alignment, ch->dma.boundary,
191                               ch->dma.max_address, BUS_SPACE_MAXADDR,
192                               NULL, NULL, ch->dma.max_iosize,
193                               ATA_DMA_ENTRIES, ch->dma.segsize,
194                               BUS_DMA_ALLOCNOW, NULL, NULL, &slot->data_tag)) {
195	    device_printf(ch->dev, "FAILURE - create data_tag\n");
196	    goto error;
197	}
198
199	if (bus_dmamap_create(slot->data_tag, 0, &slot->data_map)) {
200	    device_printf(ch->dev, "FAILURE - create data_map\n");
201	    goto error;
202        }
203    }
204
205    return;
206
207error:
208    device_printf(dev, "WARNING - DMA allocation failed, disabling DMA\n");
209    ata_dmafree(dev);
210}
211
212static void
213ata_dmafree(device_t dev)
214{
215    struct ata_channel *ch = device_get_softc(dev);
216    int i;
217
218    /* free all dma slots */
219    for (i = 0; i < ATA_DMA_SLOTS; i++) {
220	struct ata_dmaslot *slot = &ch->dma.slot[i];
221
222	if (slot->sg_bus) {
223            bus_dmamap_unload(slot->sg_tag, slot->sg_map);
224            slot->sg_bus = 0;
225	}
226	if (slot->sg_map) {
227            bus_dmamem_free(slot->sg_tag, slot->sg, slot->sg_map);
228            bus_dmamap_destroy(slot->sg_tag, slot->sg_map);
229            slot->sg = NULL;
230            slot->sg_map = 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
247static void
248ata_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
266static int
267ata_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 ((error = bus_dmamap_load(request->dma->data_tag, request->dma->data_map,
307				 request->data, request->bytecount,
308				 ch->dma.setprd, &dspa, BUS_DMA_NOWAIT)) ||
309				 (error = dspa.error)) {
310	device_printf(request->parent, "FAILURE - load data\n");
311	goto error;
312    }
313
314    if (entries)
315	*entries = dspa.nsegs;
316
317    bus_dmamap_sync(request->dma->sg_tag, request->dma->sg_map,
318		    BUS_DMASYNC_PREWRITE);
319    bus_dmamap_sync(request->dma->data_tag, request->dma->data_map,
320		    (request->flags & ATA_R_READ) ?
321		    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
322    return 0;
323
324error:
325    ata_dmaunload(request);
326    return EIO;
327}
328
329int
330ata_dmaunload(struct ata_request *request)
331{
332    ATA_DEBUG_RQ(request, "dmaunload");
333
334    if (request->dma) {
335	bus_dmamap_sync(request->dma->sg_tag, request->dma->sg_map,
336			BUS_DMASYNC_POSTWRITE);
337	bus_dmamap_sync(request->dma->data_tag, request->dma->data_map,
338			(request->flags & ATA_R_READ) ?
339			BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
340
341	bus_dmamap_unload(request->dma->data_tag, request->dma->data_map);
342        request->dma = NULL;
343    }
344    return 0;
345}
346