ata-dma.c revision 134090
1105760Srwatson/*-
2105760Srwatson * Copyright (c) 1998 - 2004 S�ren Schmidt <sos@FreeBSD.org>
3105760Srwatson * All rights reserved.
4105760Srwatson *
5105760Srwatson * Redistribution and use in source and binary forms, with or without
6105760Srwatson * modification, are permitted provided that the following conditions
7105760Srwatson * are met:
8105760Srwatson * 1. Redistributions of source code must retain the above copyright
9105760Srwatson *    notice, this list of conditions and the following disclaimer,
10105760Srwatson *    without modification, immediately at the beginning of the file.
11105760Srwatson * 2. Redistributions in binary form must reproduce the above copyright
12105760Srwatson *    notice, this list of conditions and the following disclaimer in the
13105760Srwatson *    documentation and/or other materials provided with the distribution.
14105760Srwatson * 3. The name of the author may not be used to endorse or promote products
15105760Srwatson *    derived from this software without specific prior written permission.
16105760Srwatson *
17105760Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18105760Srwatson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19105760Srwatson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20105760Srwatson * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21105760Srwatson * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22105760Srwatson * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23105760Srwatson * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24105760Srwatson * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25105760Srwatson * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26105760Srwatson * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27105760Srwatson */
28105760Srwatson
29105760Srwatson#include <sys/cdefs.h>
30105760Srwatson__FBSDID("$FreeBSD: head/sys/dev/ata/ata-dma.c 134090 2004-08-20 19:05:22Z sos $");
31105760Srwatson
32105760Srwatson#include <sys/param.h>
33105760Srwatson#include <sys/systm.h>
34105760Srwatson#include <sys/ata.h>
35105760Srwatson#include <sys/kernel.h>
36105760Srwatson#include <sys/endian.h>
37105760Srwatson#include <sys/malloc.h>
38105760Srwatson#include <sys/lock.h>
39105760Srwatson#include <sys/sema.h>
40105760Srwatson#include <sys/taskqueue.h>
41105760Srwatson#include <vm/uma.h>
42105760Srwatson#include <sys/bus.h>
43105760Srwatson#include <machine/bus.h>
44105760Srwatson#include <sys/rman.h>
45105760Srwatson#include <dev/pci/pcivar.h>
46105760Srwatson#include <dev/ata/ata-all.h>
47105760Srwatson#include <dev/ata/ata-pci.h>
48105760Srwatson
49105760Srwatson/* prototypes */
50105760Srwatsonstatic void ata_dmaalloc(struct ata_channel *);
51105760Srwatsonstatic void ata_dmafree(struct ata_channel *);
52138593Ssamstatic void ata_dmasetprd(void *, bus_dma_segment_t *, int, int);
53139494Ssamstatic int ata_dmaload(struct ata_device *, caddr_t, int32_t, int);
54105760Srwatsonstatic int ata_dmaunload(struct ata_channel *);
55105760Srwatson
56105760Srwatson/* local vars */
57105760Srwatsonstatic MALLOC_DEFINE(M_ATADMA, "ATA DMA", "ATA driver DMA");
58105760Srwatson
59105760Srwatson/* misc defines */
60105760Srwatson#define MAXTABSZ	PAGE_SIZE
61105760Srwatson#define MAXWSPCSZ	PAGE_SIZE
62105760Srwatson
63105760Srwatsonstruct ata_dc_cb_args {
64105760Srwatson    bus_addr_t maddr;
65105760Srwatson    int error;
66105760Srwatson};
67105760Srwatson
68105760Srwatsonvoid
69105760Srwatsonata_dmainit(struct ata_channel *ch)
70105760Srwatson{
71105760Srwatson    if ((ch->dma = malloc(sizeof(struct ata_dma), M_ATADMA, M_NOWAIT|M_ZERO))) {
72105760Srwatson	ch->dma->alloc = ata_dmaalloc;
73105825Srwatson	ch->dma->free = ata_dmafree;
74105760Srwatson	ch->dma->setprd = ata_dmasetprd;
75105760Srwatson	ch->dma->load = ata_dmaload;
76105760Srwatson	ch->dma->unload = ata_dmaunload;
77105760Srwatson	ch->dma->alignment = 2;
78105760Srwatson	ch->dma->max_iosize = 64 * 1024;
79105760Srwatson	ch->dma->boundary = 64 * 1024;
80138593Ssam    }
81105825Srwatson}
82105760Srwatson
83105760Srwatsonstatic void
84105760Srwatsonata_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
85105760Srwatson{
86105760Srwatson    struct ata_dc_cb_args *cba = (struct ata_dc_cb_args *)xsc;
87105760Srwatson
88105760Srwatson    if (!(cba->error = error))
89105760Srwatson	cba->maddr = segs[0].ds_addr;
90105760Srwatson}
91105760Srwatson
92105760Srwatsonstatic void
93105760Srwatsonata_dmaalloc(struct ata_channel *ch)
94105760Srwatson{
95105760Srwatson    struct ata_dc_cb_args ccba;
96105760Srwatson
97105760Srwatson    if (bus_dma_tag_create(NULL, ch->dma->alignment, 0,
98105760Srwatson			   BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
99105760Srwatson			   NULL, NULL, 16*1024*1024,
100105760Srwatson			   ATA_DMA_ENTRIES, ch->dma->max_iosize,
101138593Ssam			   BUS_DMA_ALLOCNOW, NULL, NULL, &ch->dma->dmatag))
102138593Ssam	goto error;
103138593Ssam
104138593Ssam    if (bus_dma_tag_create(ch->dma->dmatag, PAGE_SIZE, PAGE_SIZE,
105138593Ssam			   BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
106138593Ssam			   NULL, NULL, MAXTABSZ, 1, MAXTABSZ,
107138593Ssam			   BUS_DMA_ALLOCNOW, NULL, NULL, &ch->dma->cdmatag))
108139494Ssam	goto error;
109138593Ssam
110138593Ssam    if (bus_dma_tag_create(ch->dma->dmatag,ch->dma->alignment,ch->dma->boundary,
111138593Ssam			   BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
112138593Ssam			   NULL, NULL, 16*1024*1024,
113138593Ssam			   ATA_DMA_ENTRIES, ch->dma->max_iosize,
114138593Ssam			   BUS_DMA_ALLOCNOW, NULL, NULL, &ch->dma->ddmatag))
115194799Sdelphij	goto error;
116138593Ssam
117138593Ssam    if (bus_dmamem_alloc(ch->dma->cdmatag, (void **)&ch->dma->dmatab, 0,
118138593Ssam			 &ch->dma->cdmamap))
119138593Ssam	goto error;
120138593Ssam
121138593Ssam    if (bus_dmamap_load(ch->dma->cdmatag, ch->dma->cdmamap, ch->dma->dmatab,
122			MAXTABSZ, ata_dmasetupc_cb, &ccba, 0) || ccba.error) {
123	bus_dmamem_free(ch->dma->cdmatag, ch->dma->dmatab, ch->dma->cdmamap);
124	goto error;
125    }
126    ch->dma->mdmatab = ccba.maddr;
127
128    if (bus_dmamap_create(ch->dma->ddmatag, 0, &ch->dma->ddmamap))
129	goto error;
130
131    if (bus_dma_tag_create(ch->dma->dmatag, PAGE_SIZE, PAGE_SIZE,
132			   BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
133			   NULL, NULL, MAXWSPCSZ, 1, MAXWSPCSZ,
134			   BUS_DMA_ALLOCNOW, NULL, NULL, &ch->dma->wdmatag))
135	goto error;
136
137    if (bus_dmamem_alloc(ch->dma->wdmatag, (void **)&ch->dma->workspace, 0,
138			 &ch->dma->wdmamap))
139	goto error;
140
141    if (bus_dmamap_load(ch->dma->wdmatag, ch->dma->wdmamap, ch->dma->workspace,
142			MAXWSPCSZ, ata_dmasetupc_cb, &ccba, 0) || ccba.error) {
143	bus_dmamem_free(ch->dma->wdmatag, ch->dma->workspace, ch->dma->wdmamap);
144	goto error;
145    }
146    ch->dma->wdmatab = ccba.maddr;
147
148    return;
149
150error:
151    ata_printf(ch, -1, "WARNING - DMA allocation failed, disabling DMA\n");
152    ata_dmafree(ch);
153    free(ch->dma, M_ATADMA);
154    ch->dma = NULL;
155}
156
157static void
158ata_dmafree(struct ata_channel *ch)
159{
160    if (ch->dma->wdmatab) {
161	bus_dmamap_unload(ch->dma->wdmatag, ch->dma->wdmamap);
162	bus_dmamem_free(ch->dma->wdmatag, ch->dma->workspace, ch->dma->wdmamap);
163	ch->dma->wdmatab = 0;
164	ch->dma->wdmamap = NULL;
165	ch->dma->workspace = NULL;
166    }
167    if (ch->dma->wdmatag) {
168	bus_dma_tag_destroy(ch->dma->wdmatag);
169	ch->dma->wdmatag = NULL;
170    }
171    if (ch->dma->mdmatab) {
172	bus_dmamap_unload(ch->dma->cdmatag, ch->dma->cdmamap);
173	bus_dmamem_free(ch->dma->cdmatag, ch->dma->dmatab, ch->dma->cdmamap);
174	ch->dma->mdmatab = 0;
175	ch->dma->cdmamap = NULL;
176	ch->dma->dmatab = NULL;
177    }
178    if (ch->dma->ddmamap) {
179	bus_dmamap_destroy(ch->dma->ddmatag, ch->dma->ddmamap);
180	ch->dma->ddmamap = NULL;
181    }
182    if (ch->dma->cdmatag) {
183	bus_dma_tag_destroy(ch->dma->cdmatag);
184	ch->dma->cdmatag = NULL;
185    }
186    if (ch->dma->ddmatag) {
187	bus_dma_tag_destroy(ch->dma->ddmatag);
188	ch->dma->ddmatag = NULL;
189    }
190    if (ch->dma->dmatag) {
191	bus_dma_tag_destroy(ch->dma->dmatag);
192	ch->dma->dmatag = NULL;
193    }
194}
195
196static void
197ata_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
198{
199    struct ata_dmasetprd_args *args = xsc;
200    struct ata_dma_prdentry *prd = args->dmatab;
201    int i;
202
203    if ((args->error = error))
204	return;
205
206    for (i = 0; i < nsegs; i++) {
207	prd[i].addr = htole32(segs[i].ds_addr);
208	prd[i].count = htole32(segs[i].ds_len);
209    }
210    prd[i - 1].count |= htole32(ATA_DMA_EOT);
211}
212
213static int
214ata_dmaload(struct ata_device *atadev, caddr_t data, int32_t count, int dir)
215{
216    struct ata_channel *ch = atadev->channel;
217    struct ata_dmasetprd_args cba;
218
219    if (ch->dma->flags & ATA_DMA_LOADED) {
220	ata_prtdev(atadev, "FAILURE - already active DMA on this device\n");
221	return -1;
222    }
223    if (!count) {
224	ata_prtdev(atadev, "FAILURE - zero length DMA transfer attempted\n");
225	return -1;
226    }
227    if (((uintptr_t)data & (ch->dma->alignment - 1)) ||
228	(count & (ch->dma->alignment - 1))) {
229	ata_prtdev(atadev, "FAILURE - non aligned DMA transfer attempted\n");
230	return -1;
231    }
232    if (count > ch->dma->max_iosize) {
233	ata_prtdev(atadev,
234		   "FAILURE - oversized DMA transfer attempted %d > %d\n",
235		   count, ch->dma->max_iosize);
236	return -1;
237    }
238
239    cba.dmatab = ch->dma->dmatab;
240
241    bus_dmamap_sync(ch->dma->cdmatag, ch->dma->cdmamap, BUS_DMASYNC_PREWRITE);
242
243    if (bus_dmamap_load(ch->dma->ddmatag, ch->dma->ddmamap, data, count,
244			ch->dma->setprd, &cba, 0) || cba.error)
245	return -1;
246
247    bus_dmamap_sync(ch->dma->ddmatag, ch->dma->ddmamap,
248		    dir ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
249
250    ch->dma->cur_iosize = count;
251    ch->dma->flags = dir ? (ATA_DMA_LOADED | ATA_DMA_READ) : ATA_DMA_LOADED;
252    return 0;
253}
254
255int
256ata_dmaunload(struct ata_channel *ch)
257{
258    bus_dmamap_sync(ch->dma->cdmatag, ch->dma->cdmamap, BUS_DMASYNC_POSTWRITE);
259
260    bus_dmamap_sync(ch->dma->ddmatag, ch->dma->ddmamap,
261		    (ch->dma->flags & ATA_DMA_READ) != 0 ?
262		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
263    bus_dmamap_unload(ch->dma->ddmatag, ch->dma->ddmamap);
264
265    ch->dma->cur_iosize = 0;
266    ch->dma->flags &= ~ATA_DMA_LOADED;
267    return 0;
268}
269