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