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