xref: /freebsd/sys/dev/ata/ata-dma.c (revision 3193579b66fd7067f898dbc54bdea81a0e6f9bd0)
1 /*-
2  * Copyright (c) 1998 - 2003 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/mutex.h>
40 #include <sys/taskqueue.h>
41 #include <sys/bus.h>
42 #include <machine/bus.h>
43 #include <sys/rman.h>
44 #include <dev/pci/pcivar.h>
45 #include <dev/ata/ata-all.h>
46 #include <dev/ata/ata-pci.h>
47 
48 /* prototypes */
49 static void ata_dmaalloc(struct ata_channel *);
50 static void ata_dmafree(struct ata_channel *);
51 static void ata_dmasetupd_cb(void *, bus_dma_segment_t *, int, int);
52 static int ata_dmaload(struct ata_device *, caddr_t, int32_t, int);
53 static int ata_dmaunload(struct ata_channel *);
54 
55 /* local vars */
56 static MALLOC_DEFINE(M_ATADMA, "ATA DMA", "ATA driver DMA");
57 
58 /* misc defines */
59 #define MAXSEGSZ	PAGE_SIZE
60 #define MAXTABSZ	PAGE_SIZE
61 #define MAXCTLDMASZ	(2 * (MAXTABSZ + MAXPHYS))
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->load = ata_dmaload;
75 	ch->dma->unload = ata_dmaunload;
76 	ch->dma->alignment = 2;
77 	ch->dma->max_iosize = 64*1024;
78     }
79 }
80 
81 
82 static void
83 ata_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
84 {
85     struct ata_dc_cb_args *cba = (struct ata_dc_cb_args *)xsc;
86 
87     if (!(cba->error = error))
88 	cba->maddr = segs[0].ds_addr;
89 }
90 
91 static void
92 ata_dmaalloc(struct ata_channel *ch)
93 {
94     struct ata_dc_cb_args ccba;
95 
96     if (bus_dma_tag_create(NULL, 1, 0,
97 			   BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
98 			   NULL, NULL, MAXCTLDMASZ,
99 			   ATA_DMA_ENTRIES, BUS_SPACE_MAXSIZE_32BIT,
100 			   BUS_DMA_ALLOCNOW, NULL, NULL, &ch->dma->dmatag))
101 	goto error;
102 
103     if (bus_dma_tag_create(ch->dma->dmatag, PAGE_SIZE, PAGE_SIZE,
104 			   BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
105 			   NULL, NULL, MAXTABSZ, 1, MAXTABSZ,
106 			   BUS_DMA_ALLOCNOW, NULL, NULL, &ch->dma->cdmatag))
107 	goto error;
108 
109     if (bus_dma_tag_create(ch->dma->dmatag, ch->dma->alignment, 64*1024,
110 			   BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
111 			   NULL, NULL, MAXPHYS, ATA_DMA_ENTRIES, MAXSEGSZ,
112 			   BUS_DMA_ALLOCNOW, NULL, NULL, &ch->dma->ddmatag))
113 	goto error;
114 
115     if (bus_dmamem_alloc(ch->dma->cdmatag, (void **)&ch->dma->dmatab, 0,
116 			 &ch->dma->cdmamap))
117 	goto error;
118 
119     if (bus_dmamap_load(ch->dma->cdmatag, ch->dma->cdmamap, ch->dma->dmatab,
120 			MAXTABSZ, ata_dmasetupc_cb, &ccba, 0) || ccba.error) {
121 	bus_dmamem_free(ch->dma->cdmatag, ch->dma->dmatab,ch->dma->cdmamap);
122 	goto error;
123     }
124     ch->dma->mdmatab = ccba.maddr;
125     if (bus_dmamap_create(ch->dma->ddmatag, 0, &ch->dma->ddmamap))
126 	goto error;
127     return;
128 
129 error:
130     ata_printf(ch, -1, "WARNING - DMA tag allocation failed, disabling DMA\n");
131     ata_dmafree(ch);
132     free(ch->dma, M_ATADMA);
133     ch->dma = NULL;
134 }
135 
136 static void
137 ata_dmafree(struct ata_channel *ch)
138 {
139     if (ch->dma->mdmatab) {
140 	bus_dmamap_unload(ch->dma->cdmatag, ch->dma->cdmamap);
141 	bus_dmamem_free(ch->dma->cdmatag, ch->dma->dmatab, ch->dma->cdmamap);
142 	ch->dma->mdmatab = 0;
143 	ch->dma->cdmamap = NULL;
144 	ch->dma->dmatab = NULL;
145     }
146     if (ch->dma->ddmamap) {
147 	bus_dmamap_destroy(ch->dma->ddmatag, ch->dma->ddmamap);
148 	ch->dma->ddmamap = NULL;
149     }
150     if (ch->dma->cdmatag) {
151 	bus_dma_tag_destroy(ch->dma->cdmatag);
152 	ch->dma->cdmatag = NULL;
153     }
154     if (ch->dma->ddmatag) {
155 	bus_dma_tag_destroy(ch->dma->ddmatag);
156 	ch->dma->ddmatag = NULL;
157     }
158     if (ch->dma->dmatag) {
159 	bus_dma_tag_destroy(ch->dma->dmatag);
160 	ch->dma->dmatag = NULL;
161     }
162 }
163 
164 struct ata_dmasetup_data_cb_args {
165     struct ata_dmaentry *dmatab;
166     int error;
167 };
168 
169 static void
170 ata_dmasetupd_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
171 {
172     struct ata_dmasetup_data_cb_args *cba =
173 	(struct ata_dmasetup_data_cb_args *)xsc;
174     bus_size_t cnt;
175     u_int32_t lastcount;
176     int i, j;
177 
178     cba->error = error;
179     if (error != 0)
180 	return;
181     lastcount = j = 0;
182     for (i = 0; i < nsegs; i++) {
183 	/*
184 	 * A maximum segment size was specified for bus_dma_tag_create, but
185 	 * some busdma code does not seem to honor this, so fix up if needed.
186 	 */
187 	for (cnt = 0; cnt < segs[i].ds_len; cnt += MAXSEGSZ, j++) {
188 	    cba->dmatab[j].base = htole32(segs[i].ds_addr + cnt);
189 	    lastcount = ulmin(segs[i].ds_len - cnt, MAXSEGSZ) & 0xffff;
190 	    cba->dmatab[j].count = htole32(lastcount);
191 	}
192     }
193     cba->dmatab[j - 1].count = htole32(lastcount | ATA_DMA_EOT);
194 }
195 
196 static int
197 ata_dmaload(struct ata_device *atadev, caddr_t data, int32_t count, int dir)
198 {
199     struct ata_channel *ch = atadev->channel;
200     struct ata_dmasetup_data_cb_args cba;
201 
202     if (ch->dma->flags & ATA_DMA_ACTIVE) {
203 	ata_prtdev(atadev, "FAILURE - already active DMA on this device\n");
204 	return -1;
205     }
206     if (!count) {
207 	ata_prtdev(atadev, "FAILURE - zero length DMA transfer attempted\n");
208 	return -1;
209     }
210     if (((uintptr_t)data & (ch->dma->alignment - 1)) ||
211 	(count & (ch->dma->alignment - 1))) {
212 	ata_prtdev(atadev, "FAILURE - non aligned DMA transfer attempted\n");
213 	return -1;
214     }
215     if (count > ch->dma->max_iosize) {
216 	ata_prtdev(atadev,
217 		   "FAILURE - oversized DMA transfer attempted %d > %d\n",
218 		   count, ch->dma->max_iosize);
219 	return -1;
220     }
221 
222     cba.dmatab = ch->dma->dmatab;
223 
224     bus_dmamap_sync(ch->dma->cdmatag, ch->dma->cdmamap, BUS_DMASYNC_PREWRITE);
225 
226     if (bus_dmamap_load(ch->dma->ddmatag, ch->dma->ddmamap, data, count,
227 			ata_dmasetupd_cb, &cba, 0) || cba.error)
228 	return -1;
229 
230     bus_dmamap_sync(ch->dma->ddmatag, ch->dma->ddmamap,
231 		    dir ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
232 
233     ch->dma->cur_iosize = count;
234     ch->dma->flags = dir ? (ATA_DMA_ACTIVE | ATA_DMA_READ) : ATA_DMA_ACTIVE;
235 
236     return 0;
237 }
238 
239 int
240 ata_dmaunload(struct ata_channel *ch)
241 {
242     bus_dmamap_sync(ch->dma->cdmatag, ch->dma->cdmamap, BUS_DMASYNC_POSTWRITE);
243 
244     bus_dmamap_sync(ch->dma->ddmatag, ch->dma->ddmamap,
245 		    (ch->dma->flags & ATA_DMA_READ) != 0 ?
246 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
247     bus_dmamap_unload(ch->dma->ddmatag, ch->dma->ddmamap);
248 
249     ch->dma->cur_iosize = 0;
250     ch->dma->flags = 0;
251 
252     return 0;
253 }
254