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 mbuf chain. 115 */ 116 static int 117 _bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map, 118 struct mbuf *m0, bus_dma_segment_t *segs, int *nsegs, int flags) 119 { 120 struct mbuf *m; 121 int error; 122 123 error = 0; 124 for (m = m0; m != NULL && error == 0; m = m->m_next) { 125 if (m->m_len > 0) { 126 error = _bus_dmamap_load_buffer(dmat, map, m->m_data, 127 m->m_len, kernel_pmap, flags | BUS_DMA_LOAD_MBUF, 128 segs, nsegs); 129 } 130 } 131 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d", 132 __func__, dmat, flags, error, *nsegs); 133 return (error); 134 } 135 136 /* 137 * Load from block io. 138 */ 139 static int 140 _bus_dmamap_load_bio(bus_dma_tag_t dmat, bus_dmamap_t map, struct bio *bio, 141 int *nsegs, int flags) 142 { 143 144 if ((bio->bio_flags & BIO_VLIST) != 0) { 145 bus_dma_segment_t *segs = (bus_dma_segment_t *)bio->bio_data; 146 return (_bus_dmamap_load_vlist(dmat, map, segs, bio->bio_ma_n, 147 kernel_pmap, nsegs, flags, bio->bio_ma_offset, 148 bio->bio_bcount)); 149 } 150 151 if ((bio->bio_flags & BIO_UNMAPPED) != 0) 152 return (_bus_dmamap_load_ma(dmat, map, bio->bio_ma, 153 bio->bio_bcount, bio->bio_ma_offset, flags, NULL, nsegs)); 154 155 return (_bus_dmamap_load_buffer(dmat, map, bio->bio_data, 156 bio->bio_bcount, kernel_pmap, flags, NULL, nsegs)); 157 } 158 159 int 160 bus_dmamap_load_ma_triv(bus_dma_tag_t dmat, bus_dmamap_t map, 161 struct vm_page **ma, bus_size_t tlen, int ma_offs, int flags, 162 bus_dma_segment_t *segs, int *segp) 163 { 164 vm_paddr_t paddr; 165 bus_size_t len; 166 int error, i; 167 168 error = 0; 169 for (i = 0; tlen > 0; i++, tlen -= len) { 170 len = min(PAGE_SIZE - ma_offs, tlen); 171 paddr = VM_PAGE_TO_PHYS(ma[i]) + ma_offs; 172 error = _bus_dmamap_load_phys(dmat, map, paddr, len, 173 flags, segs, segp); 174 if (error != 0) 175 break; 176 ma_offs = 0; 177 } 178 return (error); 179 } 180 181 /* 182 * Load a cam control block. 183 */ 184 static int 185 _bus_dmamap_load_ccb(bus_dma_tag_t dmat, bus_dmamap_t map, union ccb *ccb, 186 int *nsegs, int flags) 187 { 188 struct ccb_hdr *ccb_h; 189 void *data_ptr; 190 int error; 191 uint32_t dxfer_len; 192 uint16_t sglist_cnt; 193 194 error = 0; 195 ccb_h = &ccb->ccb_h; 196 switch (ccb_h->func_code) { 197 case XPT_SCSI_IO: { 198 struct ccb_scsiio *csio; 199 200 csio = &ccb->csio; 201 data_ptr = csio->data_ptr; 202 dxfer_len = csio->dxfer_len; 203 sglist_cnt = csio->sglist_cnt; 204 break; 205 } 206 case XPT_CONT_TARGET_IO: { 207 struct ccb_scsiio *ctio; 208 209 ctio = &ccb->ctio; 210 data_ptr = ctio->data_ptr; 211 dxfer_len = ctio->dxfer_len; 212 sglist_cnt = ctio->sglist_cnt; 213 break; 214 } 215 case XPT_ATA_IO: { 216 struct ccb_ataio *ataio; 217 218 ataio = &ccb->ataio; 219 data_ptr = ataio->data_ptr; 220 dxfer_len = ataio->dxfer_len; 221 sglist_cnt = 0; 222 break; 223 } 224 case XPT_NVME_IO: 225 case XPT_NVME_ADMIN: { 226 struct ccb_nvmeio *nvmeio; 227 228 nvmeio = &ccb->nvmeio; 229 data_ptr = nvmeio->data_ptr; 230 dxfer_len = nvmeio->dxfer_len; 231 sglist_cnt = nvmeio->sglist_cnt; 232 break; 233 } 234 default: 235 panic("_bus_dmamap_load_ccb: Unsupported func code %d", 236 ccb_h->func_code); 237 } 238 239 switch ((ccb_h->flags & CAM_DATA_MASK)) { 240 case CAM_DATA_VADDR: 241 error = _bus_dmamap_load_buffer(dmat, map, data_ptr, dxfer_len, 242 kernel_pmap, flags, NULL, nsegs); 243 break; 244 case CAM_DATA_PADDR: 245 error = _bus_dmamap_load_phys(dmat, map, 246 (vm_paddr_t)(uintptr_t)data_ptr, dxfer_len, flags, NULL, 247 nsegs); 248 break; 249 case CAM_DATA_SG: 250 error = _bus_dmamap_load_vlist(dmat, map, 251 (bus_dma_segment_t *)data_ptr, sglist_cnt, kernel_pmap, 252 nsegs, flags, 0, dxfer_len); 253 break; 254 case CAM_DATA_SG_PADDR: 255 error = _bus_dmamap_load_plist(dmat, map, 256 (bus_dma_segment_t *)data_ptr, sglist_cnt, nsegs, flags); 257 break; 258 case CAM_DATA_BIO: 259 error = _bus_dmamap_load_bio(dmat, map, (struct bio *)data_ptr, 260 nsegs, flags); 261 break; 262 default: 263 panic("_bus_dmamap_load_ccb: flags 0x%X unimplemented", 264 ccb_h->flags); 265 } 266 return (error); 267 } 268 269 /* 270 * Load a uio. 271 */ 272 static int 273 _bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map, struct uio *uio, 274 int *nsegs, int flags) 275 { 276 bus_size_t resid; 277 bus_size_t minlen; 278 struct iovec *iov; 279 pmap_t pmap; 280 caddr_t addr; 281 int error, i; 282 283 if (uio->uio_segflg == UIO_USERSPACE) { 284 KASSERT(uio->uio_td != NULL, 285 ("bus_dmamap_load_uio: USERSPACE but no proc")); 286 pmap = vmspace_pmap(uio->uio_td->td_proc->p_vmspace); 287 } else 288 pmap = kernel_pmap; 289 resid = uio->uio_resid; 290 iov = uio->uio_iov; 291 error = 0; 292 293 for (i = 0; i < uio->uio_iovcnt && resid != 0 && !error; i++) { 294 /* 295 * Now at the first iovec to load. Load each iovec 296 * until we have exhausted the residual count. 297 */ 298 299 addr = (caddr_t) iov[i].iov_base; 300 minlen = resid < iov[i].iov_len ? resid : iov[i].iov_len; 301 if (minlen > 0) { 302 error = _bus_dmamap_load_buffer(dmat, map, addr, 303 minlen, pmap, flags, NULL, nsegs); 304 resid -= minlen; 305 } 306 } 307 308 return (error); 309 } 310 311 /* 312 * Map the buffer buf into bus space using the dmamap map. 313 */ 314 int 315 bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf, 316 bus_size_t buflen, bus_dmamap_callback_t *callback, 317 void *callback_arg, int flags) 318 { 319 bus_dma_segment_t *segs; 320 struct memdesc mem; 321 int error; 322 int nsegs; 323 324 if ((flags & BUS_DMA_NOWAIT) == 0) { 325 mem = memdesc_vaddr(buf, buflen); 326 _bus_dmamap_waitok(dmat, map, &mem, callback, callback_arg); 327 } 328 329 nsegs = -1; 330 error = _bus_dmamap_load_buffer(dmat, map, buf, buflen, kernel_pmap, 331 flags, NULL, &nsegs); 332 nsegs++; 333 334 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d", 335 __func__, dmat, flags, error, nsegs); 336 337 if (error == EINPROGRESS) 338 return (error); 339 340 segs = _bus_dmamap_complete(dmat, map, NULL, nsegs, error); 341 if (error) 342 (*callback)(callback_arg, segs, 0, error); 343 else 344 (*callback)(callback_arg, segs, nsegs, 0); 345 346 /* 347 * Return ENOMEM to the caller so that it can pass it up the stack. 348 * This error only happens when NOWAIT is set, so deferral is disabled. 349 */ 350 if (error == ENOMEM) 351 return (error); 352 353 return (0); 354 } 355 356 int 357 bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map, struct mbuf *m0, 358 bus_dmamap_callback2_t *callback, void *callback_arg, int flags) 359 { 360 bus_dma_segment_t *segs; 361 int nsegs, error; 362 363 M_ASSERTPKTHDR(m0); 364 365 flags |= BUS_DMA_NOWAIT; 366 nsegs = -1; 367 error = _bus_dmamap_load_mbuf_sg(dmat, map, m0, NULL, &nsegs, flags); 368 ++nsegs; 369 370 segs = _bus_dmamap_complete(dmat, map, NULL, nsegs, error); 371 if (error) 372 (*callback)(callback_arg, segs, 0, 0, error); 373 else 374 (*callback)(callback_arg, segs, nsegs, m0->m_pkthdr.len, error); 375 376 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d", 377 __func__, dmat, flags, error, nsegs); 378 return (error); 379 } 380 381 int 382 bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map, struct mbuf *m0, 383 bus_dma_segment_t *segs, int *nsegs, int flags) 384 { 385 int error; 386 387 flags |= BUS_DMA_NOWAIT; 388 *nsegs = -1; 389 error = _bus_dmamap_load_mbuf_sg(dmat, map, m0, segs, nsegs, flags); 390 ++*nsegs; 391 _bus_dmamap_complete(dmat, map, segs, *nsegs, error); 392 return (error); 393 } 394 395 int 396 bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map, struct uio *uio, 397 bus_dmamap_callback2_t *callback, void *callback_arg, int flags) 398 { 399 bus_dma_segment_t *segs; 400 int nsegs, error; 401 402 flags |= BUS_DMA_NOWAIT; 403 nsegs = -1; 404 error = _bus_dmamap_load_uio(dmat, map, uio, &nsegs, flags); 405 nsegs++; 406 407 segs = _bus_dmamap_complete(dmat, map, NULL, nsegs, error); 408 if (error) 409 (*callback)(callback_arg, segs, 0, 0, error); 410 else 411 (*callback)(callback_arg, segs, nsegs, uio->uio_resid, error); 412 413 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d", 414 __func__, dmat, flags, error, nsegs); 415 return (error); 416 } 417 418 int 419 bus_dmamap_load_ccb(bus_dma_tag_t dmat, bus_dmamap_t map, union ccb *ccb, 420 bus_dmamap_callback_t *callback, void *callback_arg, 421 int flags) 422 { 423 bus_dma_segment_t *segs; 424 struct ccb_hdr *ccb_h; 425 struct memdesc mem; 426 int error; 427 int nsegs; 428 429 ccb_h = &ccb->ccb_h; 430 if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_NONE) { 431 callback(callback_arg, NULL, 0, 0); 432 return (0); 433 } 434 if ((flags & BUS_DMA_NOWAIT) == 0) { 435 mem = memdesc_ccb(ccb); 436 _bus_dmamap_waitok(dmat, map, &mem, callback, callback_arg); 437 } 438 nsegs = -1; 439 error = _bus_dmamap_load_ccb(dmat, map, ccb, &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_bio(bus_dma_tag_t dmat, bus_dmamap_t map, struct bio *bio, 465 bus_dmamap_callback_t *callback, void *callback_arg, 466 int flags) 467 { 468 bus_dma_segment_t *segs; 469 struct memdesc mem; 470 int error; 471 int nsegs; 472 473 if ((flags & BUS_DMA_NOWAIT) == 0) { 474 mem = memdesc_bio(bio); 475 _bus_dmamap_waitok(dmat, map, &mem, callback, callback_arg); 476 } 477 nsegs = -1; 478 error = _bus_dmamap_load_bio(dmat, map, bio, &nsegs, flags); 479 nsegs++; 480 481 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d", 482 __func__, dmat, flags, error, nsegs); 483 484 if (error == EINPROGRESS) 485 return (error); 486 487 segs = _bus_dmamap_complete(dmat, map, NULL, nsegs, error); 488 if (error) 489 (*callback)(callback_arg, segs, 0, error); 490 else 491 (*callback)(callback_arg, segs, nsegs, error); 492 /* 493 * Return ENOMEM to the caller so that it can pass it up the stack. 494 * This error only happens when NOWAIT is set, so deferral is disabled. 495 */ 496 if (error == ENOMEM) 497 return (error); 498 499 return (0); 500 } 501 502 int 503 bus_dmamap_load_mem(bus_dma_tag_t dmat, bus_dmamap_t map, 504 struct memdesc *mem, bus_dmamap_callback_t *callback, 505 void *callback_arg, int flags) 506 { 507 bus_dma_segment_t *segs; 508 int error; 509 int nsegs; 510 511 if ((flags & BUS_DMA_NOWAIT) == 0) 512 _bus_dmamap_waitok(dmat, map, mem, callback, callback_arg); 513 514 nsegs = -1; 515 error = 0; 516 switch (mem->md_type) { 517 case MEMDESC_VADDR: 518 error = _bus_dmamap_load_buffer(dmat, map, mem->u.md_vaddr, 519 mem->md_opaque, kernel_pmap, flags, NULL, &nsegs); 520 break; 521 case MEMDESC_PADDR: 522 error = _bus_dmamap_load_phys(dmat, map, mem->u.md_paddr, 523 mem->md_opaque, flags, NULL, &nsegs); 524 break; 525 case MEMDESC_VLIST: 526 error = _bus_dmamap_load_vlist(dmat, map, mem->u.md_list, 527 mem->md_opaque, kernel_pmap, &nsegs, flags, 0, SIZE_T_MAX); 528 break; 529 case MEMDESC_PLIST: 530 error = _bus_dmamap_load_plist(dmat, map, mem->u.md_list, 531 mem->md_opaque, &nsegs, flags); 532 break; 533 case MEMDESC_BIO: 534 error = _bus_dmamap_load_bio(dmat, map, mem->u.md_bio, 535 &nsegs, flags); 536 break; 537 case MEMDESC_UIO: 538 error = _bus_dmamap_load_uio(dmat, map, mem->u.md_uio, 539 &nsegs, flags); 540 break; 541 case MEMDESC_MBUF: 542 error = _bus_dmamap_load_mbuf_sg(dmat, map, mem->u.md_mbuf, 543 NULL, &nsegs, flags); 544 break; 545 case MEMDESC_CCB: 546 error = _bus_dmamap_load_ccb(dmat, map, mem->u.md_ccb, &nsegs, 547 flags); 548 break; 549 } 550 nsegs++; 551 552 CTR5(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d nsegs %d", 553 __func__, dmat, flags, error, nsegs); 554 555 if (error == EINPROGRESS) 556 return (error); 557 558 segs = _bus_dmamap_complete(dmat, map, NULL, nsegs, error); 559 if (error) 560 (*callback)(callback_arg, segs, 0, error); 561 else 562 (*callback)(callback_arg, segs, nsegs, 0); 563 564 /* 565 * Return ENOMEM to the caller so that it can pass it up the stack. 566 * This error only happens when NOWAIT is set, so deferral is disabled. 567 */ 568 if (error == ENOMEM) 569 return (error); 570 571 return (0); 572 } 573