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