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 <opencrypto/cryptodev.h> 58 59 #include <machine/bus.h> 60 61 /* 62 * Load up data starting at offset within a region specified by a 63 * list of virtual address ranges until either length or the region 64 * are exhausted. 65 */ 66 static int 67 _bus_dmamap_load_vlist(bus_dma_tag_t dmat, bus_dmamap_t map, 68 bus_dma_segment_t *list, int sglist_cnt, struct pmap *pmap, int *nsegs, 69 int flags, size_t offset, size_t length) 70 { 71 int error; 72 73 error = 0; 74 for (; sglist_cnt > 0 && length != 0; sglist_cnt--, list++) { 75 char *addr; 76 size_t ds_len; 77 78 KASSERT((offset < list->ds_len), 79 ("Invalid mid-segment offset")); 80 addr = (char *)(uintptr_t)list->ds_addr + offset; 81 ds_len = list->ds_len - offset; 82 offset = 0; 83 if (ds_len > length) 84 ds_len = length; 85 length -= ds_len; 86 KASSERT((ds_len != 0), ("Segment length is zero")); 87 error = _bus_dmamap_load_buffer(dmat, map, addr, ds_len, pmap, 88 flags, NULL, nsegs); 89 if (error) 90 break; 91 } 92 return (error); 93 } 94 95 /* 96 * Load a list of physical addresses. 97 */ 98 static int 99 _bus_dmamap_load_plist(bus_dma_tag_t dmat, bus_dmamap_t map, 100 bus_dma_segment_t *list, int sglist_cnt, int *nsegs, int flags) 101 { 102 int error; 103 104 error = 0; 105 for (; sglist_cnt > 0; sglist_cnt--, list++) { 106 error = _bus_dmamap_load_phys(dmat, map, 107 (vm_paddr_t)list->ds_addr, list->ds_len, flags, NULL, 108 nsegs); 109 if (error) 110 break; 111 } 112 return (error); 113 } 114 115 /* 116 * Load an unmapped mbuf 117 */ 118 static int 119 _bus_dmamap_load_unmapped_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map, 120 struct mbuf *m, bus_dma_segment_t *segs, int *nsegs, int flags) 121 { 122 struct mbuf_ext_pgs *ext_pgs; 123 int error, i, off, len, pglen, pgoff, seglen, segoff; 124 125 MBUF_EXT_PGS_ASSERT(m); 126 ext_pgs = &m->m_ext_pgs; 127 128 len = m->m_len; 129 error = 0; 130 131 /* Skip over any data removed from the front. */ 132 off = mtod(m, vm_offset_t); 133 134 if (ext_pgs->hdr_len != 0) { 135 if (off >= ext_pgs->hdr_len) { 136 off -= ext_pgs->hdr_len; 137 } else { 138 seglen = ext_pgs->hdr_len - off; 139 segoff = off; 140 seglen = min(seglen, len); 141 off = 0; 142 len -= seglen; 143 error = _bus_dmamap_load_buffer(dmat, map, 144 &ext_pgs->m_epg_hdr[segoff], seglen, kernel_pmap, 145 flags, segs, nsegs); 146 } 147 } 148 pgoff = ext_pgs->first_pg_off; 149 for (i = 0; i < ext_pgs->npgs && error == 0 && len > 0; i++) { 150 pglen = mbuf_ext_pg_len(ext_pgs, i, pgoff); 151 if (off >= pglen) { 152 off -= pglen; 153 pgoff = 0; 154 continue; 155 } 156 seglen = pglen - off; 157 segoff = pgoff + off; 158 off = 0; 159 seglen = min(seglen, len); 160 len -= seglen; 161 error = _bus_dmamap_load_phys(dmat, map, 162 ext_pgs->m_epg_pa[i] + segoff, seglen, flags, segs, nsegs); 163 pgoff = 0; 164 }; 165 if (len != 0 && error == 0) { 166 KASSERT((off + len) <= ext_pgs->trail_len, 167 ("off + len > trail (%d + %d > %d)", off, len, 168 ext_pgs->trail_len)); 169 error = _bus_dmamap_load_buffer(dmat, map, 170 &ext_pgs->m_epg_trail[off], len, kernel_pmap, flags, segs, 171 nsegs); 172 } 173 return (error); 174 } 175 176 /* 177 * Load an mbuf chain. 178 */ 179 static int 180 _bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map, 181 struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs, int flags) 182 { 183 struct mbuf *m; 184 int error; 185 186 error = 0; 187 for (m = m0; m != NULL && error == 0; m = m->m_next) { 188 if (m->m_len > 0) { 189 if ((m->m_flags & M_NOMAP) != 0) 190 error = _bus_dmamap_load_unmapped_mbuf_sg(dmat, 191 map, m, segs, nsegs, flags); 192 else 193 error = _bus_dmamap_load_buffer(dmat, map, 194 m->m_data, m->m_len, kernel_pmap, 195 flags | BUS_DMA_LOAD_MBUF, segs, nsegs); 196 } 197 } 198 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d", 199 __func__, dmat, flags, error, *nsegs); 200 return (error); 201 } 202 203 /* 204 * Load from block io. 205 */ 206 static int 207 _bus_dmamap_load_bio(bus_dma_tag_t dmat, bus_dmamap_t map, struct bio *bio, 208 int *nsegs, int flags) 209 { 210 211 if ((bio->bio_flags & BIO_VLIST) != 0) { 212 bus_dma_segment_t *segs = (bus_dma_segment_t *)bio->bio_data; 213 return (_bus_dmamap_load_vlist(dmat, map, segs, bio->bio_ma_n, 214 kernel_pmap, nsegs, flags, bio->bio_ma_offset, 215 bio->bio_bcount)); 216 } 217 218 if ((bio->bio_flags & BIO_UNMAPPED) != 0) 219 return (_bus_dmamap_load_ma(dmat, map, bio->bio_ma, 220 bio->bio_bcount, bio->bio_ma_offset, flags, NULL, nsegs)); 221 222 return (_bus_dmamap_load_buffer(dmat, map, bio->bio_data, 223 bio->bio_bcount, kernel_pmap, flags, NULL, nsegs)); 224 } 225 226 int 227 bus_dmamap_load_ma_triv(bus_dma_tag_t dmat, bus_dmamap_t map, 228 struct vm_page **ma, bus_size_t tlen, int ma_offs, int flags, 229 bus_dma_segment_t *segs, int *segp) 230 { 231 vm_paddr_t paddr; 232 bus_size_t len; 233 int error, i; 234 235 error = 0; 236 for (i = 0; tlen > 0; i++, tlen -= len) { 237 len = min(PAGE_SIZE - ma_offs, tlen); 238 paddr = VM_PAGE_TO_PHYS(ma[i]) + ma_offs; 239 error = _bus_dmamap_load_phys(dmat, map, paddr, len, 240 flags, segs, segp); 241 if (error != 0) 242 break; 243 ma_offs = 0; 244 } 245 return (error); 246 } 247 248 /* 249 * Load a cam control block. 250 */ 251 static int 252 _bus_dmamap_load_ccb(bus_dma_tag_t dmat, bus_dmamap_t map, union ccb *ccb, 253 int *nsegs, int flags) 254 { 255 struct ccb_hdr *ccb_h; 256 void *data_ptr; 257 int error; 258 uint32_t dxfer_len; 259 uint16_t sglist_cnt; 260 261 error = 0; 262 ccb_h = &ccb->ccb_h; 263 switch (ccb_h->func_code) { 264 case XPT_SCSI_IO: { 265 struct ccb_scsiio *csio; 266 267 csio = &ccb->csio; 268 data_ptr = csio->data_ptr; 269 dxfer_len = csio->dxfer_len; 270 sglist_cnt = csio->sglist_cnt; 271 break; 272 } 273 case XPT_CONT_TARGET_IO: { 274 struct ccb_scsiio *ctio; 275 276 ctio = &ccb->ctio; 277 data_ptr = ctio->data_ptr; 278 dxfer_len = ctio->dxfer_len; 279 sglist_cnt = ctio->sglist_cnt; 280 break; 281 } 282 case XPT_ATA_IO: { 283 struct ccb_ataio *ataio; 284 285 ataio = &ccb->ataio; 286 data_ptr = ataio->data_ptr; 287 dxfer_len = ataio->dxfer_len; 288 sglist_cnt = 0; 289 break; 290 } 291 case XPT_NVME_IO: 292 case XPT_NVME_ADMIN: { 293 struct ccb_nvmeio *nvmeio; 294 295 nvmeio = &ccb->nvmeio; 296 data_ptr = nvmeio->data_ptr; 297 dxfer_len = nvmeio->dxfer_len; 298 sglist_cnt = nvmeio->sglist_cnt; 299 break; 300 } 301 default: 302 panic("_bus_dmamap_load_ccb: Unsupported func code %d", 303 ccb_h->func_code); 304 } 305 306 switch ((ccb_h->flags & CAM_DATA_MASK)) { 307 case CAM_DATA_VADDR: 308 error = _bus_dmamap_load_buffer(dmat, map, data_ptr, dxfer_len, 309 kernel_pmap, flags, NULL, nsegs); 310 break; 311 case CAM_DATA_PADDR: 312 error = _bus_dmamap_load_phys(dmat, map, 313 (vm_paddr_t)(uintptr_t)data_ptr, dxfer_len, flags, NULL, 314 nsegs); 315 break; 316 case CAM_DATA_SG: 317 error = _bus_dmamap_load_vlist(dmat, map, 318 (bus_dma_segment_t *)data_ptr, sglist_cnt, kernel_pmap, 319 nsegs, flags, 0, dxfer_len); 320 break; 321 case CAM_DATA_SG_PADDR: 322 error = _bus_dmamap_load_plist(dmat, map, 323 (bus_dma_segment_t *)data_ptr, sglist_cnt, nsegs, flags); 324 break; 325 case CAM_DATA_BIO: 326 error = _bus_dmamap_load_bio(dmat, map, (struct bio *)data_ptr, 327 nsegs, flags); 328 break; 329 default: 330 panic("_bus_dmamap_load_ccb: flags 0x%X unimplemented", 331 ccb_h->flags); 332 } 333 return (error); 334 } 335 336 /* 337 * Load a uio. 338 */ 339 static int 340 _bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map, struct uio *uio, 341 int *nsegs, int flags) 342 { 343 bus_size_t resid; 344 bus_size_t minlen; 345 struct iovec *iov; 346 pmap_t pmap; 347 caddr_t addr; 348 int error, i; 349 350 if (uio->uio_segflg == UIO_USERSPACE) { 351 KASSERT(uio->uio_td != NULL, 352 ("bus_dmamap_load_uio: USERSPACE but no proc")); 353 pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace); 354 } else 355 pmap = kernel_pmap; 356 resid = uio->uio_resid; 357 iov = uio->uio_iov; 358 error = 0; 359 360 for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) { 361 /* 362 * Now at the first iovec to load. Load each iovec 363 * until we have exhausted the residual count. 364 */ 365 366 addr = (caddr_t) iov[i].iov_base; 367 minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len; 368 if (minlen > 0) { 369 error = _bus_dmamap_load_buffer(dmat, map, addr, 370 minlen, pmap, flags, NULL, nsegs); 371 resid -= minlen; 372 } 373 } 374 375 return (error); 376 } 377 378 /* 379 * Map the buffer buf into bus space using the dmamap map. 380 */ 381 int 382 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf, 383 bus_size_t buflen, bus_dmamap_callback_t *callback, 384 void *callback_arg, int flags) 385 { 386 bus_dma_segment_t *segs; 387 struct memdesc mem; 388 int error; 389 int nsegs; 390 391 if ((flags & BUS_DMA_NOWAIT) == 0) { 392 mem = memdesc_vaddr(buf, buflen); 393 _bus_dmamap_waitok(dmat, map, &mem, callback, callback_arg); 394 } 395 396 nsegs = -1; 397 error = _bus_dmamap_load_buffer(dmat, map, buf, buflen, kernel_pmap, 398 flags, NULL, &nsegs); 399 nsegs++; 400 401 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d", 402 __func__, dmat, flags, error, nsegs); 403 404 if (error == EINPROGRESS) 405 return (error); 406 407 segs = _bus_dmamap_complete(dmat, map, NULL, nsegs, error); 408 if (error) 409 (*callback)(callback_arg, segs, 0, error); 410 else 411 (*callback)(callback_arg, segs, nsegs, 0); 412 413 /* 414 * Return ENOMEM to the caller so that it can pass it up the stack. 415 * This error only happens when NOWAIT is set, so deferral is disabled. 416 */ 417 if (error == ENOMEM) 418 return (error); 419 420 return (0); 421 } 422 423 int 424 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map, struct mbuf *m0, 425 bus_dmamap_callback2_t *callback, void *callback_arg, int flags) 426 { 427 bus_dma_segment_t *segs; 428 int nsegs, error; 429 430 M_ASSERTPKTHDR(m0); 431 432 flags |= BUS_DMA_NOWAIT; 433 nsegs = -1; 434 error = _bus_dmamap_load_mbuf_sg(dmat, map, m0, NULL, &nsegs, flags); 435 ++nsegs; 436 437 segs = _bus_dmamap_complete(dmat, map, NULL, nsegs, error); 438 if (error) 439 (*callback)(callback_arg, segs, 0, 0, error); 440 else 441 (*callback)(callback_arg, segs, nsegs, m0->m_pkthdr.len, error); 442 443 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d", 444 __func__, dmat, flags, error, nsegs); 445 return (error); 446 } 447 448 int 449 bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map, struct mbuf *m0, 450 bus_dma_segment_t *segs, int *nsegs, int flags) 451 { 452 int error; 453 454 flags |= BUS_DMA_NOWAIT; 455 *nsegs = -1; 456 error = _bus_dmamap_load_mbuf_sg(dmat, map, m0, segs, nsegs, flags); 457 ++*nsegs; 458 _bus_dmamap_complete(dmat, map, segs, *nsegs, error); 459 return (error); 460 } 461 462 int 463 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map, struct uio *uio, 464 bus_dmamap_callback2_t *callback, void *callback_arg, int flags) 465 { 466 bus_dma_segment_t *segs; 467 int nsegs, error; 468 469 flags |= BUS_DMA_NOWAIT; 470 nsegs = -1; 471 error = _bus_dmamap_load_uio(dmat, map, uio, &nsegs, flags); 472 nsegs++; 473 474 segs = _bus_dmamap_complete(dmat, map, NULL, nsegs, error); 475 if (error) 476 (*callback)(callback_arg, segs, 0, 0, error); 477 else 478 (*callback)(callback_arg, segs, nsegs, uio->uio_resid, error); 479 480 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d", 481 __func__, dmat, flags, error, nsegs); 482 return (error); 483 } 484 485 int 486 bus_dmamap_load_ccb(bus_dma_tag_t dmat, bus_dmamap_t map, union ccb *ccb, 487 bus_dmamap_callback_t *callback, void *callback_arg, 488 int flags) 489 { 490 bus_dma_segment_t *segs; 491 struct ccb_hdr *ccb_h; 492 struct memdesc mem; 493 int error; 494 int nsegs; 495 496 ccb_h = &ccb->ccb_h; 497 if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_NONE) { 498 callback(callback_arg, NULL, 0, 0); 499 return (0); 500 } 501 if ((flags & BUS_DMA_NOWAIT) == 0) { 502 mem = memdesc_ccb(ccb); 503 _bus_dmamap_waitok(dmat, map, &mem, callback, callback_arg); 504 } 505 nsegs = -1; 506 error = _bus_dmamap_load_ccb(dmat, map, ccb, &nsegs, flags); 507 nsegs++; 508 509 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d", 510 __func__, dmat, flags, error, nsegs); 511 512 if (error == EINPROGRESS) 513 return (error); 514 515 segs = _bus_dmamap_complete(dmat, map, NULL, nsegs, error); 516 if (error) 517 (*callback)(callback_arg, segs, 0, error); 518 else 519 (*callback)(callback_arg, segs, nsegs, error); 520 /* 521 * Return ENOMEM to the caller so that it can pass it up the stack. 522 * This error only happens when NOWAIT is set, so deferral is disabled. 523 */ 524 if (error == ENOMEM) 525 return (error); 526 527 return (0); 528 } 529 530 int 531 bus_dmamap_load_bio(bus_dma_tag_t dmat, bus_dmamap_t map, struct bio *bio, 532 bus_dmamap_callback_t *callback, void *callback_arg, 533 int flags) 534 { 535 bus_dma_segment_t *segs; 536 struct memdesc mem; 537 int error; 538 int nsegs; 539 540 if ((flags & BUS_DMA_NOWAIT) == 0) { 541 mem = memdesc_bio(bio); 542 _bus_dmamap_waitok(dmat, map, &mem, callback, callback_arg); 543 } 544 nsegs = -1; 545 error = _bus_dmamap_load_bio(dmat, map, bio, &nsegs, flags); 546 nsegs++; 547 548 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d", 549 __func__, dmat, flags, error, nsegs); 550 551 if (error == EINPROGRESS) 552 return (error); 553 554 segs = _bus_dmamap_complete(dmat, map, NULL, nsegs, error); 555 if (error) 556 (*callback)(callback_arg, segs, 0, error); 557 else 558 (*callback)(callback_arg, segs, nsegs, error); 559 /* 560 * Return ENOMEM to the caller so that it can pass it up the stack. 561 * This error only happens when NOWAIT is set, so deferral is disabled. 562 */ 563 if (error == ENOMEM) 564 return (error); 565 566 return (0); 567 } 568 569 int 570 bus_dmamap_load_mem(bus_dma_tag_t dmat, bus_dmamap_t map, 571 struct memdesc *mem, bus_dmamap_callback_t *callback, 572 void *callback_arg, int flags) 573 { 574 bus_dma_segment_t *segs; 575 int error; 576 int nsegs; 577 578 if ((flags & BUS_DMA_NOWAIT) == 0) 579 _bus_dmamap_waitok(dmat, map, mem, callback, callback_arg); 580 581 nsegs = -1; 582 error = 0; 583 switch (mem->md_type) { 584 case MEMDESC_VADDR: 585 error = _bus_dmamap_load_buffer(dmat, map, mem->u.md_vaddr, 586 mem->md_opaque, kernel_pmap, flags, NULL, &nsegs); 587 break; 588 case MEMDESC_PADDR: 589 error = _bus_dmamap_load_phys(dmat, map, mem->u.md_paddr, 590 mem->md_opaque, flags, NULL, &nsegs); 591 break; 592 case MEMDESC_VLIST: 593 error = _bus_dmamap_load_vlist(dmat, map, mem->u.md_list, 594 mem->md_opaque, kernel_pmap, &nsegs, flags, 0, SIZE_T_MAX); 595 break; 596 case MEMDESC_PLIST: 597 error = _bus_dmamap_load_plist(dmat, map, mem->u.md_list, 598 mem->md_opaque, &nsegs, flags); 599 break; 600 case MEMDESC_BIO: 601 error = _bus_dmamap_load_bio(dmat, map, mem->u.md_bio, 602 &nsegs, flags); 603 break; 604 case MEMDESC_UIO: 605 error = _bus_dmamap_load_uio(dmat, map, mem->u.md_uio, 606 &nsegs, flags); 607 break; 608 case MEMDESC_MBUF: 609 error = _bus_dmamap_load_mbuf_sg(dmat, map, mem->u.md_mbuf, 610 NULL, &nsegs, flags); 611 break; 612 case MEMDESC_CCB: 613 error = _bus_dmamap_load_ccb(dmat, map, mem->u.md_ccb, &nsegs, 614 flags); 615 break; 616 } 617 nsegs++; 618 619 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d", 620 __func__, dmat, flags, error, nsegs); 621 622 if (error == EINPROGRESS) 623 return (error); 624 625 segs = _bus_dmamap_complete(dmat, map, NULL, nsegs, error); 626 if (error) 627 (*callback)(callback_arg, segs, 0, error); 628 else 629 (*callback)(callback_arg, segs, nsegs, 0); 630 631 /* 632 * Return ENOMEM to the caller so that it can pass it up the stack. 633 * This error only happens when NOWAIT is set, so deferral is disabled. 634 */ 635 if (error == ENOMEM) 636 return (error); 637 638 return (0); 639 } 640 641 int 642 bus_dmamap_load_crp(bus_dma_tag_t dmat, bus_dmamap_t map, struct cryptop *crp, 643 bus_dmamap_callback_t *callback, void *callback_arg, int flags) 644 { 645 bus_dma_segment_t *segs; 646 int error; 647 int nsegs; 648 649 flags |= BUS_DMA_NOWAIT; 650 nsegs = -1; 651 error = 0; 652 switch (crp->crp_buf_type) { 653 case CRYPTO_BUF_CONTIG: 654 error = _bus_dmamap_load_buffer(dmat, map, crp->crp_buf, 655 crp->crp_ilen, kernel_pmap, flags, NULL, &nsegs); 656 break; 657 case CRYPTO_BUF_MBUF: 658 error = _bus_dmamap_load_mbuf_sg(dmat, map, crp->crp_mbuf, 659 NULL, &nsegs, flags); 660 break; 661 case CRYPTO_BUF_UIO: 662 error = _bus_dmamap_load_uio(dmat, map, crp->crp_uio, &nsegs, 663 flags); 664 break; 665 } 666 nsegs++; 667 668 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d", 669 __func__, dmat, flags, error, nsegs); 670 671 if (error == EINPROGRESS) 672 return (error); 673 674 segs = _bus_dmamap_complete(dmat, map, NULL, nsegs, error); 675 if (error) 676 (*callback)(callback_arg, segs, 0, error); 677 else 678 (*callback)(callback_arg, segs, nsegs, 0); 679 680 /* 681 * Return ENOMEM to the caller so that it can pass it up the stack. 682 * This error only happens when NOWAIT is set, so deferral is disabled. 683 */ 684 if (error == ENOMEM) 685 return (error); 686 687 return (0); 688 } 689