xref: /freebsd/sys/dev/ata/ata-dma.c (revision 6fd05b64b5b65dd4ba9b86482a0634a5f0b96c29)
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_dmasetupd_cb(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 MAXSEGSZ	PAGE_SIZE
61 #define MAXTABSZ	PAGE_SIZE
62 #define MAXWSPCSZ	256
63 #define MAXCTLDMASZ	(2 * (MAXTABSZ + MAXPHYS))
64 
65 struct ata_dc_cb_args {
66     bus_addr_t maddr;
67     int error;
68 };
69 
70 void
71 ata_dmainit(struct ata_channel *ch)
72 {
73     if ((ch->dma = malloc(sizeof(struct ata_dma), M_ATADMA, M_NOWAIT|M_ZERO))) {
74 	ch->dma->alloc = ata_dmaalloc;
75 	ch->dma->free = ata_dmafree;
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 
85 static void
86 ata_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
87 {
88     struct ata_dc_cb_args *cba = (struct ata_dc_cb_args *)xsc;
89 
90     if (!(cba->error = error))
91 	cba->maddr = segs[0].ds_addr;
92 }
93 
94 static void
95 ata_dmaalloc(struct ata_channel *ch)
96 {
97     struct ata_dc_cb_args ccba;
98 
99     if (bus_dma_tag_create(NULL, 1, 0,
100 			   BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
101 			   NULL, NULL, MAXCTLDMASZ,
102 			   ATA_DMA_ENTRIES, BUS_SPACE_MAXSIZE_32BIT,
103 			   BUS_DMA_ALLOCNOW, NULL, NULL, &ch->dma->dmatag))
104 	goto error;
105 
106     if (bus_dma_tag_create(ch->dma->dmatag, PAGE_SIZE, PAGE_SIZE,
107 			   BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
108 			   NULL, NULL, MAXTABSZ, 1, MAXTABSZ,
109 			   BUS_DMA_ALLOCNOW, NULL, NULL, &ch->dma->cdmatag))
110 	goto error;
111 
112     if (bus_dma_tag_create(ch->dma->dmatag,ch->dma->alignment,ch->dma->boundary,
113 			   BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
114 			   NULL, NULL, ch->dma->max_iosize,
115 			   ATA_DMA_ENTRIES, ch->dma->boundary,
116 			   BUS_DMA_ALLOCNOW, NULL, NULL, &ch->dma->ddmatag))
117 	goto error;
118 
119     if (bus_dmamem_alloc(ch->dma->cdmatag, (void **)&ch->dma->dmatab, 0,
120 			 &ch->dma->cdmamap))
121 	goto error;
122 
123     if (bus_dmamap_load(ch->dma->cdmatag, ch->dma->cdmamap, ch->dma->dmatab,
124 			MAXTABSZ, ata_dmasetupc_cb, &ccba, 0) || ccba.error) {
125 	bus_dmamem_free(ch->dma->cdmatag, ch->dma->dmatab, ch->dma->cdmamap);
126 	goto error;
127     }
128     ch->dma->mdmatab = ccba.maddr;
129 
130     if (bus_dmamap_create(ch->dma->ddmatag, 0, &ch->dma->ddmamap))
131 	goto error;
132 
133     if (bus_dma_tag_create(ch->dma->dmatag, PAGE_SIZE, PAGE_SIZE,
134 			   BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
135 			   NULL, NULL, MAXWSPCSZ, 1, MAXWSPCSZ,
136 			   BUS_DMA_ALLOCNOW, NULL, NULL, &ch->dma->wdmatag))
137 	goto error;
138 
139     if (bus_dmamem_alloc(ch->dma->wdmatag, (void **)&ch->dma->workspace, 0,
140 			 &ch->dma->wdmamap))
141 	goto error;
142 
143     if (bus_dmamap_load(ch->dma->wdmatag, ch->dma->wdmamap, ch->dma->workspace,
144 			MAXWSPCSZ, ata_dmasetupc_cb, &ccba, 0) || ccba.error) {
145 	bus_dmamem_free(ch->dma->wdmatag, ch->dma->workspace, ch->dma->wdmamap);
146 	goto error;
147     }
148     ch->dma->wdmatab = ccba.maddr;
149 
150     return;
151 
152 error:
153     ata_printf(ch, -1, "WARNING - DMA allocation failed, disabling DMA\n");
154     ata_dmafree(ch);
155     free(ch->dma, M_ATADMA);
156     ch->dma = NULL;
157 }
158 
159 static void
160 ata_dmafree(struct ata_channel *ch)
161 {
162     if (ch->dma->wdmatab) {
163 	bus_dmamap_unload(ch->dma->wdmatag, ch->dma->wdmamap);
164 	bus_dmamem_free(ch->dma->wdmatag, ch->dma->workspace, ch->dma->wdmamap);
165 	ch->dma->wdmatab = 0;
166 	ch->dma->wdmamap = NULL;
167 	ch->dma->workspace = NULL;
168     }
169     if (ch->dma->wdmatag) {
170 	bus_dma_tag_destroy(ch->dma->wdmatag);
171 	ch->dma->wdmatag = NULL;
172     }
173     if (ch->dma->mdmatab) {
174 	bus_dmamap_unload(ch->dma->cdmatag, ch->dma->cdmamap);
175 	bus_dmamem_free(ch->dma->cdmatag, ch->dma->dmatab, ch->dma->cdmamap);
176 	ch->dma->mdmatab = 0;
177 	ch->dma->cdmamap = NULL;
178 	ch->dma->dmatab = NULL;
179     }
180     if (ch->dma->ddmamap) {
181 	bus_dmamap_destroy(ch->dma->ddmatag, ch->dma->ddmamap);
182 	ch->dma->ddmamap = NULL;
183     }
184     if (ch->dma->cdmatag) {
185 	bus_dma_tag_destroy(ch->dma->cdmatag);
186 	ch->dma->cdmatag = NULL;
187     }
188     if (ch->dma->ddmatag) {
189 	bus_dma_tag_destroy(ch->dma->ddmatag);
190 	ch->dma->ddmatag = NULL;
191     }
192     if (ch->dma->dmatag) {
193 	bus_dma_tag_destroy(ch->dma->dmatag);
194 	ch->dma->dmatag = NULL;
195     }
196 }
197 
198 struct ata_dmasetup_data_cb_args {
199     struct ata_dmaentry *dmatab;
200     int error;
201 };
202 
203 static void
204 ata_dmasetupd_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
205 {
206     struct ata_dmasetup_data_cb_args *cba =
207 	(struct ata_dmasetup_data_cb_args *)xsc;
208     bus_size_t cnt;
209     u_int32_t lastcount;
210     int i, j;
211 
212     cba->error = error;
213     if (error != 0)
214 	return;
215     lastcount = j = 0;
216     for (i = 0; i < nsegs; i++) {
217 	/*
218 	 * A maximum segment size was specified for bus_dma_tag_create, but
219 	 * some busdma code does not seem to honor this, so fix up if needed.
220 	 */
221 	for (cnt = 0; cnt < segs[i].ds_len; cnt += MAXSEGSZ, j++) {
222 	    cba->dmatab[j].base = htole32(segs[i].ds_addr + cnt);
223 	    lastcount = ulmin(segs[i].ds_len - cnt, MAXSEGSZ) & 0xffff;
224 	    cba->dmatab[j].count = htole32(lastcount);
225 	}
226     }
227     cba->dmatab[j - 1].count = htole32(lastcount | ATA_DMA_EOT);
228 }
229 
230 static int
231 ata_dmaload(struct ata_device *atadev, caddr_t data, int32_t count, int dir)
232 {
233     struct ata_channel *ch = atadev->channel;
234     struct ata_dmasetup_data_cb_args cba;
235 
236     if (ch->dma->flags & ATA_DMA_ACTIVE) {
237 	ata_prtdev(atadev, "FAILURE - already active DMA on this device\n");
238 	return -1;
239     }
240     if (!count) {
241 	ata_prtdev(atadev, "FAILURE - zero length DMA transfer attempted\n");
242 	return -1;
243     }
244     if (((uintptr_t)data & (ch->dma->alignment - 1)) ||
245 	(count & (ch->dma->alignment - 1))) {
246 	ata_prtdev(atadev, "FAILURE - non aligned DMA transfer attempted\n");
247 	return -1;
248     }
249     if (count > ch->dma->max_iosize) {
250 	ata_prtdev(atadev,
251 		   "FAILURE - oversized DMA transfer attempted %d > %d\n",
252 		   count, ch->dma->max_iosize);
253 	return -1;
254     }
255 
256     cba.dmatab = ch->dma->dmatab;
257 
258     bus_dmamap_sync(ch->dma->cdmatag, ch->dma->cdmamap, BUS_DMASYNC_PREWRITE);
259 
260     if (bus_dmamap_load(ch->dma->ddmatag, ch->dma->ddmamap, data, count,
261 			ata_dmasetupd_cb, &cba, 0) || cba.error)
262 	return -1;
263 
264     bus_dmamap_sync(ch->dma->ddmatag, ch->dma->ddmamap,
265 		    dir ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
266 
267     ch->dma->cur_iosize = count;
268     ch->dma->flags = dir ? (ATA_DMA_ACTIVE | ATA_DMA_READ) : ATA_DMA_ACTIVE;
269 
270     return 0;
271 }
272 
273 int
274 ata_dmaunload(struct ata_channel *ch)
275 {
276     bus_dmamap_sync(ch->dma->cdmatag, ch->dma->cdmamap, BUS_DMASYNC_POSTWRITE);
277 
278     bus_dmamap_sync(ch->dma->ddmatag, ch->dma->ddmamap,
279 		    (ch->dma->flags & ATA_DMA_READ) != 0 ?
280 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
281     bus_dmamap_unload(ch->dma->ddmatag, ch->dma->ddmamap);
282 
283     ch->dma->cur_iosize = 0;
284     ch->dma->flags = 0;
285 
286     return 0;
287 }
288