xref: /freebsd/sys/dev/ata/ata-dma.c (revision ceaec73d406831b1251babb61675df0a1aa54a31)
1 /*-
2  * Copyright (c) 1998 - 2005 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 
63 struct ata_dc_cb_args {
64     bus_addr_t maddr;
65     int error;
66 };
67 
68 void
69 ata_dmainit(struct ata_channel *ch)
70 {
71     if ((ch->dma = malloc(sizeof(struct ata_dma), M_ATADMA, M_NOWAIT|M_ZERO))) {
72 	ch->dma->alloc = ata_dmaalloc;
73 	ch->dma->free = ata_dmafree;
74 	ch->dma->setprd = ata_dmasetprd;
75 	ch->dma->load = ata_dmaload;
76 	ch->dma->unload = ata_dmaunload;
77 	ch->dma->alignment = 2;
78 	ch->dma->max_iosize = 128 * DEV_BSIZE;
79 	ch->dma->boundary = 128 * DEV_BSIZE;
80     }
81 }
82 
83 static void
84 ata_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
85 {
86     struct ata_dc_cb_args *cba = (struct ata_dc_cb_args *)xsc;
87 
88     if (!(cba->error = error))
89 	cba->maddr = segs[0].ds_addr;
90 }
91 
92 static void
93 ata_dmaalloc(struct ata_channel *ch)
94 {
95     struct ata_dc_cb_args ccba;
96 
97     if (bus_dma_tag_create(NULL, ch->dma->alignment, 0,
98 			   BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
99 			   NULL, NULL, 256 * DEV_BSIZE,
100 			   ATA_DMA_ENTRIES, ch->dma->max_iosize,
101 			   0, NULL, NULL, &ch->dma->dmatag))
102 	goto error;
103 
104     if (bus_dma_tag_create(ch->dma->dmatag, PAGE_SIZE, PAGE_SIZE,
105 			   BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
106 			   NULL, NULL, MAXTABSZ, 1, MAXTABSZ,
107 			   0, NULL, NULL, &ch->dma->sg_tag))
108 	goto error;
109 
110     if (bus_dma_tag_create(ch->dma->dmatag,ch->dma->alignment,ch->dma->boundary,
111 			   BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
112 			   NULL, NULL, 256 * DEV_BSIZE,
113 			   ATA_DMA_ENTRIES, ch->dma->max_iosize,
114 			   BUS_DMA_ALLOCNOW, NULL, NULL, &ch->dma->data_tag))
115 	goto error;
116 
117     if (bus_dmamem_alloc(ch->dma->sg_tag, (void **)&ch->dma->sg, 0,
118 			 &ch->dma->sg_map))
119 	goto error;
120 
121     if (bus_dmamap_load(ch->dma->sg_tag, ch->dma->sg_map, ch->dma->sg,
122 			MAXTABSZ, ata_dmasetupc_cb, &ccba, 0) || ccba.error) {
123 	bus_dmamem_free(ch->dma->sg_tag, ch->dma->sg, ch->dma->sg_map);
124 	goto error;
125     }
126     ch->dma->sg_bus = ccba.maddr;
127 
128     if (bus_dmamap_create(ch->dma->data_tag, 0, &ch->dma->data_map))
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 			   0, NULL, NULL, &ch->dma->work_tag))
135 	goto error;
136 
137     if (bus_dmamem_alloc(ch->dma->work_tag, (void **)&ch->dma->work, 0,
138 			 &ch->dma->work_map))
139 	goto error;
140 
141     if (bus_dmamap_load(ch->dma->work_tag, ch->dma->work_map,ch->dma->work,
142 			MAXWSPCSZ, ata_dmasetupc_cb, &ccba, 0) || ccba.error) {
143 	bus_dmamem_free(ch->dma->work_tag,ch->dma->work, ch->dma->work_map);
144 	goto error;
145     }
146     ch->dma->work_bus = ccba.maddr;
147 
148     return;
149 
150 error:
151     device_printf(ch->dev, "WARNING - DMA allocation failed, disabling DMA\n");
152     ata_dmafree(ch);
153     free(ch->dma, M_ATADMA);
154     ch->dma = NULL;
155 }
156 
157 static void
158 ata_dmafree(struct ata_channel *ch)
159 {
160     if (ch->dma->work_bus) {
161 	bus_dmamap_unload(ch->dma->work_tag, ch->dma->work_map);
162 	bus_dmamem_free(ch->dma->work_tag, ch->dma->work, ch->dma->work_map);
163 	ch->dma->work_bus = 0;
164 	ch->dma->work_map = NULL;
165 	ch->dma->work = NULL;
166     }
167     if (ch->dma->work_tag) {
168 	bus_dma_tag_destroy(ch->dma->work_tag);
169 	ch->dma->work_tag = NULL;
170     }
171     if (ch->dma->sg_bus) {
172 	bus_dmamap_unload(ch->dma->sg_tag, ch->dma->sg_map);
173 	bus_dmamem_free(ch->dma->sg_tag, ch->dma->sg, ch->dma->sg_map);
174 	ch->dma->sg_bus = 0;
175 	ch->dma->sg_map = NULL;
176 	ch->dma->sg = NULL;
177     }
178     if (ch->dma->data_map) {
179 	bus_dmamap_destroy(ch->dma->data_tag, ch->dma->data_map);
180 	ch->dma->data_map = NULL;
181     }
182     if (ch->dma->sg_tag) {
183 	bus_dma_tag_destroy(ch->dma->sg_tag);
184 	ch->dma->sg_tag = NULL;
185     }
186     if (ch->dma->data_tag) {
187 	bus_dma_tag_destroy(ch->dma->data_tag);
188 	ch->dma->data_tag = NULL;
189     }
190     if (ch->dma->dmatag) {
191 	bus_dma_tag_destroy(ch->dma->dmatag);
192 	ch->dma->dmatag = NULL;
193     }
194 }
195 
196 static void
197 ata_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 
213 static int
214 ata_dmaload(struct ata_device *atadev, caddr_t data, int32_t count, int dir)
215 {
216     struct ata_channel *ch = device_get_softc(device_get_parent(atadev->dev));
217     struct ata_dmasetprd_args cba;
218 
219     if (ch->dma->flags & ATA_DMA_LOADED) {
220 	device_printf(atadev->dev,
221 		      "FAILURE - already active DMA on this device\n");
222 	return -1;
223     }
224     if (!count) {
225 	device_printf(atadev->dev,
226 		      "FAILURE - zero length DMA transfer attempted\n");
227 	return -1;
228     }
229     if (((uintptr_t)data & (ch->dma->alignment - 1)) ||
230 	(count & (ch->dma->alignment - 1))) {
231 	device_printf(atadev->dev,
232 		      "FAILURE - non aligned DMA transfer attempted\n");
233 	return -1;
234     }
235     if (count > ch->dma->max_iosize) {
236 	device_printf(atadev->dev,
237 		      "FAILURE - oversized DMA transfer attempted %d > %d\n",
238 		      count, ch->dma->max_iosize);
239 	return -1;
240     }
241 
242     cba.dmatab = ch->dma->sg;
243 
244     if (bus_dmamap_load(ch->dma->data_tag, ch->dma->data_map, data, count,
245 			ch->dma->setprd, &cba, 0) || cba.error)
246 	return -1;
247 
248     bus_dmamap_sync(ch->dma->sg_tag, ch->dma->sg_map, BUS_DMASYNC_PREWRITE);
249 
250     bus_dmamap_sync(ch->dma->data_tag, ch->dma->data_map,
251 		    dir ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
252 
253     ch->dma->cur_iosize = count;
254     ch->dma->flags = dir ? (ATA_DMA_LOADED | ATA_DMA_READ) : ATA_DMA_LOADED;
255     return 0;
256 }
257 
258 int
259 ata_dmaunload(struct ata_channel *ch)
260 {
261     bus_dmamap_sync(ch->dma->sg_tag, ch->dma->sg_map, BUS_DMASYNC_POSTWRITE);
262 
263     bus_dmamap_sync(ch->dma->data_tag, ch->dma->data_map,
264 		    (ch->dma->flags & ATA_DMA_READ) != 0 ?
265 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
266     bus_dmamap_unload(ch->dma->data_tag, ch->dma->data_map);
267 
268     ch->dma->cur_iosize = 0;
269     ch->dma->flags &= ~ATA_DMA_LOADED;
270     return 0;
271 }
272