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