xref: /freebsd/sys/kern/subr_bus_dma.c (revision ddd5b8e9b4d8957fce018c520657cdfa4ecffad3)
1 /*-
2  * Copyright (c) 2012 EMC Corp.
3  * All rights reserved.
4  *
5  * Copyright (c) 1997, 1998 Justin T. Gibbs.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
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 AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include "opt_bus.h"
34 
35 #include <sys/param.h>
36 #include <sys/conf.h>
37 #include <sys/systm.h>
38 #include <sys/bio.h>
39 #include <sys/bus.h>
40 #include <sys/callout.h>
41 #include <sys/mbuf.h>
42 #include <sys/memdesc.h>
43 #include <sys/proc.h>
44 #include <sys/uio.h>
45 
46 #include <vm/vm.h>
47 #include <vm/vm_page.h>
48 #include <vm/vm_map.h>
49 #include <vm/pmap.h>
50 
51 #include <cam/cam.h>
52 #include <cam/cam_ccb.h>
53 
54 #include <machine/bus.h>
55 
56 /*
57  * Load a list of virtual addresses.
58  */
59 static int
60 _bus_dmamap_load_vlist(bus_dma_tag_t dmat, bus_dmamap_t map,
61     bus_dma_segment_t *list, int sglist_cnt, struct pmap *pmap, int *nsegs,
62     int flags)
63 {
64 	int error;
65 
66 	error = 0;
67 	for (; sglist_cnt > 0; sglist_cnt--, list++) {
68 		error = _bus_dmamap_load_buffer(dmat, map,
69 		    (void *)list->ds_addr, list->ds_len, pmap, flags, NULL,
70 		    nsegs);
71 		if (error)
72 			break;
73 	}
74 	return (error);
75 }
76 
77 /*
78  * Load a list of physical addresses.
79  */
80 static int
81 _bus_dmamap_load_plist(bus_dma_tag_t dmat, bus_dmamap_t map,
82     bus_dma_segment_t *list, int sglist_cnt, int *nsegs, int flags)
83 {
84 	int error;
85 
86 	error = 0;
87 	for (; sglist_cnt > 0; sglist_cnt--, list++) {
88 		error = _bus_dmamap_load_phys(dmat, map,
89 		    (vm_paddr_t)list->ds_addr, list->ds_len, flags, NULL,
90 		    nsegs);
91 		if (error)
92 			break;
93 	}
94 	return (error);
95 }
96 
97 /*
98  * Load an mbuf chain.
99  */
100 static int
101 _bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
102     struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs, int flags)
103 {
104 	struct mbuf *m;
105 	int error;
106 
107 	M_ASSERTPKTHDR(m0);
108 
109 	error = 0;
110 	for (m = m0; m != NULL && error == 0; m = m->m_next) {
111 		if (m->m_len > 0) {
112 			error = _bus_dmamap_load_buffer(dmat, map, m->m_data,
113 			    m->m_len, kernel_pmap, flags | BUS_DMA_LOAD_MBUF,
114 			    segs, nsegs);
115 		}
116 	}
117 	CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
118 	    __func__, dmat, flags, error, *nsegs);
119 	return (error);
120 }
121 
122 /*
123  * Load from block io.
124  */
125 static int
126 _bus_dmamap_load_bio(bus_dma_tag_t dmat, bus_dmamap_t map, struct bio *bio,
127     int *nsegs, int flags)
128 {
129 	vm_paddr_t paddr;
130 	bus_size_t len, tlen;
131 	int error, i, ma_offs;
132 
133 	if ((bio->bio_flags & BIO_UNMAPPED) == 0) {
134 		error = _bus_dmamap_load_buffer(dmat, map, bio->bio_data,
135 		    bio->bio_bcount, kernel_pmap, flags, NULL, nsegs);
136 		return (error);
137 	}
138 
139 	error = 0;
140 	tlen = bio->bio_bcount;
141 	ma_offs = bio->bio_ma_offset;
142 	for (i = 0; tlen > 0; i++, tlen -= len) {
143 		len = min(PAGE_SIZE - ma_offs, tlen);
144 		paddr = VM_PAGE_TO_PHYS(bio->bio_ma[i]) + ma_offs;
145 		error = _bus_dmamap_load_phys(dmat, map, paddr, len,
146 		    flags, NULL, nsegs);
147 		if (error != 0)
148 			break;
149 		ma_offs = 0;
150 	}
151 	return (error);
152 }
153 
154 /*
155  * Load a cam control block.
156  */
157 static int
158 _bus_dmamap_load_ccb(bus_dma_tag_t dmat, bus_dmamap_t map, union ccb *ccb,
159 		    int *nsegs, int flags)
160 {
161 	struct ccb_hdr *ccb_h;
162 	void *data_ptr;
163 	int error;
164 	uint32_t dxfer_len;
165 	uint16_t sglist_cnt;
166 
167 	error = 0;
168 	ccb_h = &ccb->ccb_h;
169 	switch (ccb_h->func_code) {
170 	case XPT_SCSI_IO: {
171 		struct ccb_scsiio *csio;
172 
173 		csio = &ccb->csio;
174 		data_ptr = csio->data_ptr;
175 		dxfer_len = csio->dxfer_len;
176 		sglist_cnt = csio->sglist_cnt;
177 		break;
178 	}
179 	case XPT_CONT_TARGET_IO: {
180 		struct ccb_scsiio *ctio;
181 
182 		ctio = &ccb->ctio;
183 		data_ptr = ctio->data_ptr;
184 		dxfer_len = ctio->dxfer_len;
185 		sglist_cnt = ctio->sglist_cnt;
186 		break;
187 	}
188 	case XPT_ATA_IO: {
189 		struct ccb_ataio *ataio;
190 
191 		ataio = &ccb->ataio;
192 		data_ptr = ataio->data_ptr;
193 		dxfer_len = ataio->dxfer_len;
194 		sglist_cnt = 0;
195 		break;
196 	}
197 	default:
198 		panic("_bus_dmamap_load_ccb: Unsupported func code %d",
199 		    ccb_h->func_code);
200 	}
201 
202 	switch ((ccb_h->flags & CAM_DATA_MASK)) {
203 	case CAM_DATA_VADDR:
204 		error = _bus_dmamap_load_buffer(dmat, map, data_ptr, dxfer_len,
205 		    kernel_pmap, flags, NULL, nsegs);
206 		break;
207 	case CAM_DATA_PADDR:
208 		error = _bus_dmamap_load_phys(dmat, map,
209 		    (vm_paddr_t)(uintptr_t)data_ptr, dxfer_len, flags, NULL,
210 		    nsegs);
211 		break;
212 	case CAM_DATA_SG:
213 		error = _bus_dmamap_load_vlist(dmat, map,
214 		    (bus_dma_segment_t *)data_ptr, sglist_cnt, kernel_pmap,
215 		    nsegs, flags);
216 		break;
217 	case CAM_DATA_SG_PADDR:
218 		error = _bus_dmamap_load_plist(dmat, map,
219 		    (bus_dma_segment_t *)data_ptr, sglist_cnt, nsegs, flags);
220 		break;
221 	case CAM_DATA_BIO:
222 		error = _bus_dmamap_load_bio(dmat, map, (struct bio *)data_ptr,
223 		    nsegs, flags);
224 		break;
225 	default:
226 		panic("_bus_dmamap_load_ccb: flags 0x%X unimplemented",
227 		    ccb_h->flags);
228 	}
229 	return (error);
230 }
231 
232 /*
233  * Load a uio.
234  */
235 static int
236 _bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map, struct uio *uio,
237     int *nsegs, int flags)
238 {
239 	bus_size_t resid;
240 	bus_size_t minlen;
241 	struct iovec *iov;
242 	pmap_t pmap;
243 	caddr_t addr;
244 	int error, i;
245 
246 	if (uio->uio_segflg == UIO_USERSPACE) {
247 		KASSERT(uio->uio_td != NULL,
248 			("bus_dmamap_load_uio: USERSPACE but no proc"));
249 		pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace);
250 	} else
251 		pmap = kernel_pmap;
252 	resid = uio->uio_resid;
253 	iov = uio->uio_iov;
254 	error = 0;
255 
256 	for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) {
257 		/*
258 		 * Now at the first iovec to load.  Load each iovec
259 		 * until we have exhausted the residual count.
260 		 */
261 
262 		addr = (caddr_t) iov[i].iov_base;
263 		minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len;
264 		if (minlen > 0) {
265 			error = _bus_dmamap_load_buffer(dmat, map, addr,
266 			    minlen, pmap, flags, NULL, nsegs);
267 			resid -= minlen;
268 		}
269 	}
270 
271 	return (error);
272 }
273 
274 /*
275  * Map the buffer buf into bus space using the dmamap map.
276  */
277 int
278 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
279     bus_size_t buflen, bus_dmamap_callback_t *callback,
280     void *callback_arg, int flags)
281 {
282 	bus_dma_segment_t *segs;
283 	struct memdesc mem;
284 	int error;
285 	int nsegs;
286 
287 	if ((flags & BUS_DMA_NOWAIT) == 0) {
288 		mem = memdesc_vaddr(buf, buflen);
289 		_bus_dmamap_waitok(dmat, map, &mem, callback, callback_arg);
290 	}
291 
292 	nsegs = -1;
293 	error = _bus_dmamap_load_buffer(dmat, map, buf, buflen, kernel_pmap,
294 	    flags, NULL, &nsegs);
295 	nsegs++;
296 
297 	CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
298 	    __func__, dmat, flags, error, nsegs);
299 
300 	if (error == EINPROGRESS)
301 		return (error);
302 
303 	segs = _bus_dmamap_complete(dmat, map, NULL, nsegs, error);
304 	if (error)
305 		(*callback)(callback_arg, segs, 0, error);
306 	else
307 		(*callback)(callback_arg, segs, nsegs, 0);
308 
309 	/*
310 	 * Return ENOMEM to the caller so that it can pass it up the stack.
311 	 * This error only happens when NOWAIT is set, so deferral is disabled.
312 	 */
313 	if (error == ENOMEM)
314 		return (error);
315 
316 	return (0);
317 }
318 
319 int
320 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map, struct mbuf *m0,
321     bus_dmamap_callback2_t *callback, void *callback_arg, int flags)
322 {
323 	bus_dma_segment_t *segs;
324 	int nsegs, error;
325 
326 	flags |= BUS_DMA_NOWAIT;
327 	nsegs = -1;
328 	error = _bus_dmamap_load_mbuf_sg(dmat, map, m0, NULL, &nsegs, flags);
329 	++nsegs;
330 
331 	segs = _bus_dmamap_complete(dmat, map, NULL, nsegs, error);
332 	if (error)
333 		(*callback)(callback_arg, segs, 0, 0, error);
334 	else
335 		(*callback)(callback_arg, segs, nsegs, m0->m_pkthdr.len, error);
336 
337 	CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
338 	    __func__, dmat, flags, error, nsegs);
339 	return (error);
340 }
341 
342 int
343 bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map, struct mbuf *m0,
344     bus_dma_segment_t *segs, int *nsegs, int flags)
345 {
346 	int error;
347 
348 	flags |= BUS_DMA_NOWAIT;
349 	*nsegs = -1;
350 	error = _bus_dmamap_load_mbuf_sg(dmat, map, m0, segs, nsegs, flags);
351 	++*nsegs;
352 	_bus_dmamap_complete(dmat, map, segs, *nsegs, error);
353 	return (error);
354 }
355 
356 int
357 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map, struct uio *uio,
358     bus_dmamap_callback2_t *callback, void *callback_arg, int flags)
359 {
360 	bus_dma_segment_t *segs;
361 	int nsegs, error;
362 
363 	flags |= BUS_DMA_NOWAIT;
364 	nsegs = -1;
365 	error = _bus_dmamap_load_uio(dmat, map, uio, &nsegs, flags);
366 	nsegs++;
367 
368 	segs = _bus_dmamap_complete(dmat, map, NULL, nsegs, error);
369 	if (error)
370 		(*callback)(callback_arg, segs, 0, 0, error);
371 	else
372 		(*callback)(callback_arg, segs, nsegs, uio->uio_resid, error);
373 
374 	CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
375 	    __func__, dmat, flags, error, nsegs);
376 	return (error);
377 }
378 
379 int
380 bus_dmamap_load_ccb(bus_dma_tag_t dmat, bus_dmamap_t map, union ccb *ccb,
381 		    bus_dmamap_callback_t *callback, void *callback_arg,
382 		    int flags)
383 {
384 	bus_dma_segment_t *segs;
385 	struct ccb_hdr *ccb_h;
386 	struct memdesc mem;
387 	int error;
388 	int nsegs;
389 
390 	ccb_h = &ccb->ccb_h;
391 	if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_NONE) {
392 		callback(callback_arg, NULL, 0, 0);
393 		return (0);
394 	}
395 	if ((flags & BUS_DMA_NOWAIT) == 0) {
396 		mem = memdesc_ccb(ccb);
397 		_bus_dmamap_waitok(dmat, map, &mem, callback, callback_arg);
398 	}
399 	nsegs = -1;
400 	error = _bus_dmamap_load_ccb(dmat, map, ccb, &nsegs, flags);
401 	nsegs++;
402 
403 	CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
404 	    __func__, dmat, flags, error, nsegs);
405 
406 	if (error == EINPROGRESS)
407 		return (error);
408 
409 	segs = _bus_dmamap_complete(dmat, map, NULL, nsegs, error);
410 	if (error)
411 		(*callback)(callback_arg, segs, 0, error);
412 	else
413 		(*callback)(callback_arg, segs, nsegs, error);
414 	/*
415 	 * Return ENOMEM to the caller so that it can pass it up the stack.
416 	 * This error only happens when NOWAIT is set, so deferral is disabled.
417 	 */
418 	if (error == ENOMEM)
419 		return (error);
420 
421 	return (0);
422 }
423 
424 int
425 bus_dmamap_load_bio(bus_dma_tag_t dmat, bus_dmamap_t map, struct bio *bio,
426 		    bus_dmamap_callback_t *callback, void *callback_arg,
427 		    int flags)
428 {
429 	bus_dma_segment_t *segs;
430 	struct memdesc mem;
431 	int error;
432 	int nsegs;
433 
434 	if ((flags & BUS_DMA_NOWAIT) == 0) {
435 		mem = memdesc_bio(bio);
436 		_bus_dmamap_waitok(dmat, map, &mem, callback, callback_arg);
437 	}
438 	nsegs = -1;
439 	error = _bus_dmamap_load_bio(dmat, map, bio, &nsegs, flags);
440 	nsegs++;
441 
442 	CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
443 	    __func__, dmat, flags, error, nsegs);
444 
445 	if (error == EINPROGRESS)
446 		return (error);
447 
448 	segs = _bus_dmamap_complete(dmat, map, NULL, nsegs, error);
449 	if (error)
450 		(*callback)(callback_arg, segs, 0, error);
451 	else
452 		(*callback)(callback_arg, segs, nsegs, error);
453 	/*
454 	 * Return ENOMEM to the caller so that it can pass it up the stack.
455 	 * This error only happens when NOWAIT is set, so deferral is disabled.
456 	 */
457 	if (error == ENOMEM)
458 		return (error);
459 
460 	return (0);
461 }
462 
463 int
464 bus_dmamap_load_mem(bus_dma_tag_t dmat, bus_dmamap_t map,
465     struct memdesc *mem, bus_dmamap_callback_t *callback,
466     void *callback_arg, int flags)
467 {
468 	bus_dma_segment_t *segs;
469 	int error;
470 	int nsegs;
471 
472 	if ((flags & BUS_DMA_NOWAIT) == 0)
473 		_bus_dmamap_waitok(dmat, map, mem, callback, callback_arg);
474 
475 	nsegs = -1;
476 	error = 0;
477 	switch (mem->md_type) {
478 	case MEMDESC_VADDR:
479 		error = _bus_dmamap_load_buffer(dmat, map, mem->u.md_vaddr,
480 		    mem->md_opaque, kernel_pmap, flags, NULL, &nsegs);
481 		break;
482 	case MEMDESC_PADDR:
483 		error = _bus_dmamap_load_phys(dmat, map, mem->u.md_paddr,
484 		    mem->md_opaque, flags, NULL, &nsegs);
485 		break;
486 	case MEMDESC_VLIST:
487 		error = _bus_dmamap_load_vlist(dmat, map, mem->u.md_list,
488 		    mem->md_opaque, kernel_pmap, &nsegs, flags);
489 		break;
490 	case MEMDESC_PLIST:
491 		error = _bus_dmamap_load_plist(dmat, map, mem->u.md_list,
492 		    mem->md_opaque, &nsegs, flags);
493 		break;
494 	case MEMDESC_BIO:
495 		error = _bus_dmamap_load_bio(dmat, map, mem->u.md_bio,
496 		    &nsegs, flags);
497 		break;
498 	case MEMDESC_UIO:
499 		error = _bus_dmamap_load_uio(dmat, map, mem->u.md_uio,
500 		    &nsegs, flags);
501 		break;
502 	case MEMDESC_MBUF:
503 		error = _bus_dmamap_load_mbuf_sg(dmat, map, mem->u.md_mbuf,
504 		    NULL, &nsegs, flags);
505 		break;
506 	case MEMDESC_CCB:
507 		error = _bus_dmamap_load_ccb(dmat, map, mem->u.md_ccb, &nsegs,
508 		    flags);
509 		break;
510 	}
511 	nsegs++;
512 
513 	CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d",
514 	    __func__, dmat, flags, error, nsegs);
515 
516 	if (error == EINPROGRESS)
517 		return (error);
518 
519 	segs = _bus_dmamap_complete(dmat, map, NULL, nsegs, error);
520 	if (error)
521 		(*callback)(callback_arg, segs, 0, error);
522 	else
523 		(*callback)(callback_arg, segs, nsegs, 0);
524 
525 	/*
526 	 * Return ENOMEM to the caller so that it can pass it up the stack.
527 	 * This error only happens when NOWAIT is set, so deferral is disabled.
528 	 */
529 	if (error == ENOMEM)
530 		return (error);
531 
532 	return (0);
533 }
534