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