xref: /freebsd/sys/dev/ata/ata-dma.c (revision c37420b0d5b3b6ef875fbf0b84a13f6f09be56d6)
1 /*-
2  * Copyright (c) 1998 - 2004 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  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
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/pci/pcivar.h>
46 #include <dev/ata/ata-all.h>
47 #include <dev/ata/ata-pci.h>
48 
49 /* prototypes */
50 static void ata_dmaalloc(struct ata_channel *);
51 static void ata_dmafree(struct ata_channel *);
52 static void ata_dmasetprd(void *, bus_dma_segment_t *, int, int);
53 static int ata_dmaload(struct ata_device *, caddr_t, int32_t, int);
54 static int ata_dmaunload(struct ata_channel *);
55 
56 /* local vars */
57 static MALLOC_DEFINE(M_ATADMA, "ATA DMA", "ATA driver DMA");
58 
59 /* misc defines */
60 #define MAXTABSZ	PAGE_SIZE
61 #define MAXWSPCSZ	PAGE_SIZE
62 #define MAXCTLDMASZ	(2 * (MAXTABSZ + MAXPHYS))
63 
64 struct ata_dc_cb_args {
65     bus_addr_t maddr;
66     int error;
67 };
68 
69 void
70 ata_dmainit(struct ata_channel *ch)
71 {
72     if ((ch->dma = malloc(sizeof(struct ata_dma), M_ATADMA, M_NOWAIT|M_ZERO))) {
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->max_iosize = 64 * 1024;
80 	ch->dma->boundary = 64 * 1024;
81     }
82 }
83 
84 static void
85 ata_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
86 {
87     struct ata_dc_cb_args *cba = (struct ata_dc_cb_args *)xsc;
88 
89     if (!(cba->error = error))
90 	cba->maddr = segs[0].ds_addr;
91 }
92 
93 static void
94 ata_dmaalloc(struct ata_channel *ch)
95 {
96     struct ata_dc_cb_args ccba;
97 
98     if (bus_dma_tag_create(NULL, 1, 0,
99 			   BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
100 			   NULL, NULL, MAXCTLDMASZ,
101 			   ATA_DMA_ENTRIES, BUS_SPACE_MAXSIZE_32BIT,
102 			   BUS_DMA_ALLOCNOW, NULL, NULL, &ch->dma->dmatag))
103 	goto error;
104 
105     if (bus_dma_tag_create(ch->dma->dmatag, PAGE_SIZE, PAGE_SIZE,
106 			   BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
107 			   NULL, NULL, MAXTABSZ, 1, MAXTABSZ,
108 			   BUS_DMA_ALLOCNOW, NULL, NULL, &ch->dma->cdmatag))
109 	goto error;
110 
111     if (bus_dma_tag_create(ch->dma->dmatag,ch->dma->alignment,ch->dma->boundary,
112 			   BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
113 			   NULL, NULL, ch->dma->max_iosize,
114 			   ATA_DMA_ENTRIES, ch->dma->boundary,
115 			   BUS_DMA_ALLOCNOW, NULL, NULL, &ch->dma->ddmatag))
116 	goto error;
117 
118     if (bus_dmamem_alloc(ch->dma->cdmatag, (void **)&ch->dma->dmatab, 0,
119 			 &ch->dma->cdmamap))
120 	goto error;
121 
122     if (bus_dmamap_load(ch->dma->cdmatag, ch->dma->cdmamap, ch->dma->dmatab,
123 			MAXTABSZ, ata_dmasetupc_cb, &ccba, 0) || ccba.error) {
124 	bus_dmamem_free(ch->dma->cdmatag, ch->dma->dmatab, ch->dma->cdmamap);
125 	goto error;
126     }
127     ch->dma->mdmatab = ccba.maddr;
128 
129     if (bus_dmamap_create(ch->dma->ddmatag, 0, &ch->dma->ddmamap))
130 	goto error;
131 
132     if (bus_dma_tag_create(ch->dma->dmatag, PAGE_SIZE, PAGE_SIZE,
133 			   BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
134 			   NULL, NULL, MAXWSPCSZ, 1, MAXWSPCSZ,
135 			   BUS_DMA_ALLOCNOW, NULL, NULL, &ch->dma->wdmatag))
136 	goto error;
137 
138     if (bus_dmamem_alloc(ch->dma->wdmatag, (void **)&ch->dma->workspace, 0,
139 			 &ch->dma->wdmamap))
140 	goto error;
141 
142     if (bus_dmamap_load(ch->dma->wdmatag, ch->dma->wdmamap, ch->dma->workspace,
143 			MAXWSPCSZ, ata_dmasetupc_cb, &ccba, 0) || ccba.error) {
144 	bus_dmamem_free(ch->dma->wdmatag, ch->dma->workspace, ch->dma->wdmamap);
145 	goto error;
146     }
147     ch->dma->wdmatab = ccba.maddr;
148 
149     return;
150 
151 error:
152     ata_printf(ch, -1, "WARNING - DMA allocation failed, disabling DMA\n");
153     ata_dmafree(ch);
154     free(ch->dma, M_ATADMA);
155     ch->dma = NULL;
156 }
157 
158 static void
159 ata_dmafree(struct ata_channel *ch)
160 {
161     if (ch->dma->wdmatab) {
162 	bus_dmamap_unload(ch->dma->wdmatag, ch->dma->wdmamap);
163 	bus_dmamem_free(ch->dma->wdmatag, ch->dma->workspace, ch->dma->wdmamap);
164 	ch->dma->wdmatab = 0;
165 	ch->dma->wdmamap = NULL;
166 	ch->dma->workspace = NULL;
167     }
168     if (ch->dma->wdmatag) {
169 	bus_dma_tag_destroy(ch->dma->wdmatag);
170 	ch->dma->wdmatag = NULL;
171     }
172     if (ch->dma->mdmatab) {
173 	bus_dmamap_unload(ch->dma->cdmatag, ch->dma->cdmamap);
174 	bus_dmamem_free(ch->dma->cdmatag, ch->dma->dmatab, ch->dma->cdmamap);
175 	ch->dma->mdmatab = 0;
176 	ch->dma->cdmamap = NULL;
177 	ch->dma->dmatab = NULL;
178     }
179     if (ch->dma->ddmamap) {
180 	bus_dmamap_destroy(ch->dma->ddmatag, ch->dma->ddmamap);
181 	ch->dma->ddmamap = NULL;
182     }
183     if (ch->dma->cdmatag) {
184 	bus_dma_tag_destroy(ch->dma->cdmatag);
185 	ch->dma->cdmatag = NULL;
186     }
187     if (ch->dma->ddmatag) {
188 	bus_dma_tag_destroy(ch->dma->ddmatag);
189 	ch->dma->ddmatag = NULL;
190     }
191     if (ch->dma->dmatag) {
192 	bus_dma_tag_destroy(ch->dma->dmatag);
193 	ch->dma->dmatag = NULL;
194     }
195 }
196 
197 static void
198 ata_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
199 {
200     struct ata_dmasetprd_args *args = xsc;
201     struct ata_dma_prdentry *prd = args->dmatab;
202     int i;
203 
204     if ((args->error = error))
205 	return;
206 
207     for (i = 0; i < nsegs; i++) {
208 	prd[i].addr = htole32(segs[i].ds_addr);
209 	prd[i].count = htole32(segs[i].ds_len);
210     }
211     prd[i - 1].count |= htole32(ATA_DMA_EOT);
212 }
213 
214 static int
215 ata_dmaload(struct ata_device *atadev, caddr_t data, int32_t count, int dir)
216 {
217     struct ata_channel *ch = atadev->channel;
218     struct ata_dmasetprd_args cba;
219 
220     if (ch->dma->flags & ATA_DMA_LOADED) {
221 	ata_prtdev(atadev, "FAILURE - already active DMA on this device\n");
222 	return -1;
223     }
224     if (!count) {
225 	ata_prtdev(atadev, "FAILURE - zero length DMA transfer attempted\n");
226 	return -1;
227     }
228     if (((uintptr_t)data & (ch->dma->alignment - 1)) ||
229 	(count & (ch->dma->alignment - 1))) {
230 	ata_prtdev(atadev, "FAILURE - non aligned DMA transfer attempted\n");
231 	return -1;
232     }
233     if (count > ch->dma->max_iosize) {
234 	ata_prtdev(atadev,
235 		   "FAILURE - oversized DMA transfer attempted %d > %d\n",
236 		   count, ch->dma->max_iosize);
237 	return -1;
238     }
239 
240     cba.dmatab = ch->dma->dmatab;
241 
242     bus_dmamap_sync(ch->dma->cdmatag, ch->dma->cdmamap, BUS_DMASYNC_PREWRITE);
243 
244     if (bus_dmamap_load(ch->dma->ddmatag, ch->dma->ddmamap, data, count,
245 			ch->dma->setprd, &cba, 0) || cba.error)
246 	return -1;
247 
248     bus_dmamap_sync(ch->dma->ddmatag, ch->dma->ddmamap,
249 		    dir ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
250 
251     ch->dma->cur_iosize = count;
252     ch->dma->flags = dir ? (ATA_DMA_LOADED | ATA_DMA_READ) : ATA_DMA_LOADED;
253     return 0;
254 }
255 
256 int
257 ata_dmaunload(struct ata_channel *ch)
258 {
259     bus_dmamap_sync(ch->dma->cdmatag, ch->dma->cdmamap, BUS_DMASYNC_POSTWRITE);
260 
261     bus_dmamap_sync(ch->dma->ddmatag, ch->dma->ddmamap,
262 		    (ch->dma->flags & ATA_DMA_READ) != 0 ?
263 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
264     bus_dmamap_unload(ch->dma->ddmatag, ch->dma->ddmamap);
265 
266     ch->dma->cur_iosize = 0;
267     ch->dma->flags &= ~ATA_DMA_LOADED;
268     return 0;
269 }
270