Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/dev/ata/ata-dma.c
39507 views
1
/*-
2
* SPDX-License-Identifier: BSD-2-Clause
3
*
4
* Copyright (c) 1998 - 2008 Søren Schmidt <[email protected]>
5
* All rights reserved.
6
*
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
9
* are met:
10
* 1. Redistributions of source code must retain the above copyright
11
* notice, this list of conditions and the following disclaimer,
12
* without modification, immediately at the beginning of the file.
13
* 2. Redistributions in binary form must reproduce the above copyright
14
* notice, this list of conditions and the following disclaimer in the
15
* documentation and/or other materials provided with the distribution.
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/param.h>
30
#include <sys/systm.h>
31
#include <sys/ata.h>
32
#include <sys/kernel.h>
33
#include <sys/endian.h>
34
#include <sys/malloc.h>
35
#include <sys/lock.h>
36
#include <sys/sema.h>
37
#include <sys/taskqueue.h>
38
#include <vm/uma.h>
39
#include <sys/bus.h>
40
#include <machine/bus.h>
41
#include <sys/rman.h>
42
#include <dev/ata/ata-all.h>
43
44
/* prototypes */
45
static void ata_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
46
static void ata_dmaalloc(device_t dev);
47
static void ata_dmafree(device_t dev);
48
static void ata_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
49
static int ata_dmaload(struct ata_request *request, void *addr, int *nsegs);
50
static int ata_dmaunload(struct ata_request *request);
51
52
/* local vars */
53
static MALLOC_DEFINE(M_ATADMA, "ata_dma", "ATA driver DMA");
54
55
/* misc defines */
56
#define MAXTABSZ PAGE_SIZE
57
#define MAXWSPCSZ PAGE_SIZE*2
58
59
struct ata_dc_cb_args {
60
bus_addr_t maddr;
61
int error;
62
};
63
64
void
65
ata_dmainit(device_t dev)
66
{
67
struct ata_channel *ch = device_get_softc(dev);
68
struct ata_dc_cb_args dcba;
69
70
if (ch->dma.alloc == NULL)
71
ch->dma.alloc = ata_dmaalloc;
72
if (ch->dma.free == NULL)
73
ch->dma.free = ata_dmafree;
74
if (ch->dma.setprd == NULL)
75
ch->dma.setprd = ata_dmasetprd;
76
if (ch->dma.load == NULL)
77
ch->dma.load = ata_dmaload;
78
if (ch->dma.unload == NULL)
79
ch->dma.unload = ata_dmaunload;
80
if (ch->dma.alignment == 0)
81
ch->dma.alignment = 2;
82
if (ch->dma.boundary == 0)
83
ch->dma.boundary = 65536;
84
if (ch->dma.segsize == 0)
85
ch->dma.segsize = 65536;
86
if (ch->dma.max_iosize == 0)
87
ch->dma.max_iosize = (ATA_DMA_ENTRIES - 1) * PAGE_SIZE;
88
if (ch->dma.max_address == 0)
89
ch->dma.max_address = BUS_SPACE_MAXADDR_32BIT;
90
if (ch->dma.dma_slots == 0)
91
ch->dma.dma_slots = 1;
92
93
if (bus_dma_tag_create(bus_get_dma_tag(dev), ch->dma.alignment, 0,
94
ch->dma.max_address, BUS_SPACE_MAXADDR,
95
NULL, NULL, ch->dma.max_iosize,
96
ATA_DMA_ENTRIES, ch->dma.segsize,
97
0, NULL, NULL, &ch->dma.dmatag))
98
goto error;
99
100
if (bus_dma_tag_create(ch->dma.dmatag, PAGE_SIZE, 64 * 1024,
101
ch->dma.max_address, BUS_SPACE_MAXADDR,
102
NULL, NULL, MAXWSPCSZ, 1, MAXWSPCSZ,
103
0, NULL, NULL, &ch->dma.work_tag))
104
goto error;
105
106
if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work,
107
BUS_DMA_WAITOK | BUS_DMA_COHERENT,
108
&ch->dma.work_map))
109
goto error;
110
111
if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work,
112
MAXWSPCSZ, ata_dmasetupc_cb, &dcba, 0) ||
113
dcba.error) {
114
bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
115
goto error;
116
}
117
ch->dma.work_bus = dcba.maddr;
118
return;
119
120
error:
121
device_printf(dev, "WARNING - DMA initialization failed, disabling DMA\n");
122
ata_dmafini(dev);
123
}
124
125
void
126
ata_dmafini(device_t dev)
127
{
128
struct ata_channel *ch = device_get_softc(dev);
129
130
if (ch->dma.work_bus) {
131
bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map);
132
bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
133
ch->dma.work_bus = 0;
134
ch->dma.work = NULL;
135
}
136
if (ch->dma.work_tag) {
137
bus_dma_tag_destroy(ch->dma.work_tag);
138
ch->dma.work_tag = NULL;
139
}
140
if (ch->dma.dmatag) {
141
bus_dma_tag_destroy(ch->dma.dmatag);
142
ch->dma.dmatag = NULL;
143
}
144
}
145
146
static void
147
ata_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
148
{
149
struct ata_dc_cb_args *dcba = (struct ata_dc_cb_args *)xsc;
150
151
if (!(dcba->error = error))
152
dcba->maddr = segs[0].ds_addr;
153
}
154
155
static void
156
ata_dmaalloc(device_t dev)
157
{
158
struct ata_channel *ch = device_get_softc(dev);
159
struct ata_dc_cb_args dcba;
160
int i;
161
162
/* alloc and setup needed dma slots */
163
bzero(ch->dma.slot, sizeof(struct ata_dmaslot) * ATA_DMA_SLOTS);
164
for (i = 0; i < ch->dma.dma_slots; i++) {
165
struct ata_dmaslot *slot = &ch->dma.slot[i];
166
167
if (bus_dma_tag_create(ch->dma.dmatag, PAGE_SIZE, PAGE_SIZE,
168
ch->dma.max_address, BUS_SPACE_MAXADDR,
169
NULL, NULL, PAGE_SIZE, 1, PAGE_SIZE,
170
0, NULL, NULL, &slot->sg_tag)) {
171
device_printf(ch->dev, "FAILURE - create sg_tag\n");
172
goto error;
173
}
174
175
if (bus_dmamem_alloc(slot->sg_tag, (void **)&slot->sg, BUS_DMA_WAITOK,
176
&slot->sg_map)) {
177
device_printf(ch->dev, "FAILURE - alloc sg_map\n");
178
goto error;
179
}
180
181
if (bus_dmamap_load(slot->sg_tag, slot->sg_map, slot->sg, MAXTABSZ,
182
ata_dmasetupc_cb, &dcba, 0) || dcba.error) {
183
device_printf(ch->dev, "FAILURE - load sg\n");
184
goto error;
185
}
186
slot->sg_bus = dcba.maddr;
187
188
if (bus_dma_tag_create(ch->dma.dmatag,
189
ch->dma.alignment, ch->dma.boundary,
190
ch->dma.max_address, BUS_SPACE_MAXADDR,
191
NULL, NULL, ch->dma.max_iosize,
192
ATA_DMA_ENTRIES, ch->dma.segsize,
193
BUS_DMA_ALLOCNOW, NULL, NULL, &slot->data_tag)) {
194
device_printf(ch->dev, "FAILURE - create data_tag\n");
195
goto error;
196
}
197
198
if (bus_dmamap_create(slot->data_tag, 0, &slot->data_map)) {
199
device_printf(ch->dev, "FAILURE - create data_map\n");
200
goto error;
201
}
202
}
203
204
return;
205
206
error:
207
device_printf(dev, "WARNING - DMA allocation failed, disabling DMA\n");
208
ata_dmafree(dev);
209
}
210
211
static void
212
ata_dmafree(device_t dev)
213
{
214
struct ata_channel *ch = device_get_softc(dev);
215
int i;
216
217
/* free all dma slots */
218
for (i = 0; i < ATA_DMA_SLOTS; i++) {
219
struct ata_dmaslot *slot = &ch->dma.slot[i];
220
221
if (slot->sg_bus) {
222
bus_dmamap_unload(slot->sg_tag, slot->sg_map);
223
slot->sg_bus = 0;
224
}
225
if (slot->sg) {
226
bus_dmamem_free(slot->sg_tag, slot->sg, slot->sg_map);
227
slot->sg = NULL;
228
}
229
if (slot->data_map) {
230
bus_dmamap_destroy(slot->data_tag, slot->data_map);
231
slot->data_map = NULL;
232
}
233
if (slot->sg_tag) {
234
bus_dma_tag_destroy(slot->sg_tag);
235
slot->sg_tag = NULL;
236
}
237
if (slot->data_tag) {
238
bus_dma_tag_destroy(slot->data_tag);
239
slot->data_tag = NULL;
240
}
241
}
242
}
243
244
static void
245
ata_dmasetprd(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
246
{
247
struct ata_dmasetprd_args *args = xsc;
248
struct ata_dma_prdentry *prd = args->dmatab;
249
int i;
250
251
if ((args->error = error))
252
return;
253
254
for (i = 0; i < nsegs; i++) {
255
prd[i].addr = htole32(segs[i].ds_addr);
256
prd[i].count = htole32(segs[i].ds_len);
257
}
258
prd[i - 1].count |= htole32(ATA_DMA_EOT);
259
KASSERT(nsegs <= ATA_DMA_ENTRIES, ("too many DMA segment entries\n"));
260
args->nsegs = nsegs;
261
}
262
263
static int
264
ata_dmaload(struct ata_request *request, void *addr, int *entries)
265
{
266
struct ata_channel *ch = device_get_softc(request->parent);
267
struct ata_dmasetprd_args dspa;
268
int error;
269
270
ATA_DEBUG_RQ(request, "dmaload");
271
272
if (request->dma) {
273
device_printf(request->parent,
274
"FAILURE - already active DMA on this device\n");
275
return EIO;
276
}
277
if (!request->bytecount) {
278
device_printf(request->parent,
279
"FAILURE - zero length DMA transfer attempted\n");
280
return EIO;
281
}
282
if (request->bytecount & (ch->dma.alignment - 1)) {
283
device_printf(request->parent,
284
"FAILURE - odd-sized DMA transfer attempt %d %% %d\n",
285
request->bytecount, ch->dma.alignment);
286
return EIO;
287
}
288
if (request->bytecount > ch->dma.max_iosize) {
289
device_printf(request->parent,
290
"FAILURE - oversized DMA transfer attempt %d > %d\n",
291
request->bytecount, ch->dma.max_iosize);
292
return EIO;
293
}
294
295
/* set our slot. XXX SOS NCQ will change that */
296
request->dma = &ch->dma.slot[0];
297
298
if (addr)
299
dspa.dmatab = addr;
300
else
301
dspa.dmatab = request->dma->sg;
302
303
if (request->flags & ATA_R_DATA_IN_CCB)
304
error = bus_dmamap_load_ccb(request->dma->data_tag,
305
request->dma->data_map, request->ccb,
306
ch->dma.setprd, &dspa, BUS_DMA_NOWAIT);
307
else
308
error = bus_dmamap_load(request->dma->data_tag, request->dma->data_map,
309
request->data, request->bytecount,
310
ch->dma.setprd, &dspa, BUS_DMA_NOWAIT);
311
if (error || (error = dspa.error)) {
312
device_printf(request->parent, "FAILURE - load data\n");
313
goto error;
314
}
315
316
if (entries)
317
*entries = dspa.nsegs;
318
319
bus_dmamap_sync(request->dma->sg_tag, request->dma->sg_map,
320
BUS_DMASYNC_PREWRITE);
321
bus_dmamap_sync(request->dma->data_tag, request->dma->data_map,
322
(request->flags & ATA_R_READ) ?
323
BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
324
return 0;
325
326
error:
327
ata_dmaunload(request);
328
return EIO;
329
}
330
331
int
332
ata_dmaunload(struct ata_request *request)
333
{
334
ATA_DEBUG_RQ(request, "dmaunload");
335
336
if (request->dma) {
337
bus_dmamap_sync(request->dma->sg_tag, request->dma->sg_map,
338
BUS_DMASYNC_POSTWRITE);
339
bus_dmamap_sync(request->dma->data_tag, request->dma->data_map,
340
(request->flags & ATA_R_READ) ?
341
BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
342
343
bus_dmamap_unload(request->dma->data_tag, request->dma->data_map);
344
request->dma = NULL;
345
}
346
return 0;
347
}
348
349