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