1 /*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc. 5 * Copyright (c) 2013-2017 Mellanox Technologies, Ltd. 6 * Copyright (c) 2015 Matthew Dillon <dillon@backplane.com> 7 * Copyright (c) 2016 Matthew Macy 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 unmodified, this list of conditions, and the following 15 * disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 #ifndef _LINUXKPI_LINUX_SCATTERLIST_H_ 32 #define _LINUXKPI_LINUX_SCATTERLIST_H_ 33 34 #include <sys/types.h> 35 #include <sys/sf_buf.h> 36 37 #include <linux/page.h> 38 #include <linux/slab.h> 39 #include <linux/mm.h> 40 41 struct bus_dmamap; 42 struct scatterlist { 43 unsigned long page_link; 44 #define SG_PAGE_LINK_CHAIN 0x1UL 45 #define SG_PAGE_LINK_LAST 0x2UL 46 #define SG_PAGE_LINK_MASK 0x3UL 47 unsigned int offset; 48 unsigned int length; 49 dma_addr_t dma_address; 50 struct bus_dmamap *dma_map; /* FreeBSD specific */ 51 }; 52 53 CTASSERT((sizeof(struct scatterlist) & SG_PAGE_LINK_MASK) == 0); 54 55 struct sg_table { 56 struct scatterlist *sgl; 57 unsigned int nents; 58 unsigned int orig_nents; 59 }; 60 61 struct sg_page_iter { 62 struct scatterlist *sg; 63 unsigned int sg_pgoffset; 64 unsigned int maxents; 65 struct { 66 unsigned int nents; 67 int pg_advance; 68 } internal; 69 }; 70 71 struct sg_dma_page_iter { 72 struct sg_page_iter base; 73 }; 74 75 #define SCATTERLIST_MAX_SEGMENT (-1U & ~(PAGE_SIZE - 1)) 76 77 #define SG_MAX_SINGLE_ALLOC (PAGE_SIZE / sizeof(struct scatterlist)) 78 79 #define SG_MAGIC 0x87654321UL 80 #define SG_CHAIN SG_PAGE_LINK_CHAIN 81 #define SG_END SG_PAGE_LINK_LAST 82 83 #define sg_is_chain(sg) ((sg)->page_link & SG_PAGE_LINK_CHAIN) 84 #define sg_is_last(sg) ((sg)->page_link & SG_PAGE_LINK_LAST) 85 #define sg_chain_ptr(sg) \ 86 ((struct scatterlist *) ((sg)->page_link & ~SG_PAGE_LINK_MASK)) 87 88 #define sg_dma_address(sg) (sg)->dma_address 89 #define sg_dma_len(sg) (sg)->length 90 91 #define for_each_sg_page(sgl, iter, nents, pgoffset) \ 92 for (_sg_iter_init(sgl, iter, nents, pgoffset); \ 93 (iter)->sg; _sg_iter_next(iter)) 94 #define for_each_sg_dma_page(sgl, iter, nents, pgoffset) \ 95 for_each_sg_page(sgl, &(iter)->base, nents, pgoffset) 96 97 #define for_each_sg(sglist, sg, sgmax, iter) \ 98 for (iter = 0, sg = (sglist); iter < (sgmax); iter++, sg = sg_next(sg)) 99 100 #define for_each_sgtable_sg(sgt, sg, i) \ 101 for_each_sg((sgt)->sgl, sg, (sgt)->orig_nents, i) 102 103 #define for_each_sgtable_page(sgt, iter, pgoffset) \ 104 for_each_sg_page((sgt)->sgl, iter, (sgt)->orig_nents, pgoffset) 105 106 #define for_each_sgtable_dma_sg(sgt, sg, iter) \ 107 for_each_sg((sgt)->sgl, sg, (sgt)->nents, iter) 108 109 #define for_each_sgtable_dma_page(sgt, iter, pgoffset) \ 110 for_each_sg_dma_page((sgt)->sgl, iter, (sgt)->nents, pgoffset) 111 112 typedef struct scatterlist *(sg_alloc_fn) (unsigned int, gfp_t); 113 typedef void (sg_free_fn) (struct scatterlist *, unsigned int); 114 115 static inline void 116 sg_assign_page(struct scatterlist *sg, struct page *page) 117 { 118 unsigned long page_link = sg->page_link & SG_PAGE_LINK_MASK; 119 120 sg->page_link = page_link | (unsigned long)page; 121 } 122 123 static inline void 124 sg_set_page(struct scatterlist *sg, struct page *page, unsigned int len, 125 unsigned int offset) 126 { 127 sg_assign_page(sg, page); 128 sg->offset = offset; 129 sg->length = len; 130 } 131 132 static inline struct page * 133 sg_page(struct scatterlist *sg) 134 { 135 return ((struct page *)((sg)->page_link & ~SG_PAGE_LINK_MASK)); 136 } 137 138 static inline void 139 sg_set_buf(struct scatterlist *sg, const void *buf, unsigned int buflen) 140 { 141 sg_set_page(sg, virt_to_page(buf), buflen, 142 ((uintptr_t)buf) & (PAGE_SIZE - 1)); 143 } 144 145 static inline struct scatterlist * 146 sg_next(struct scatterlist *sg) 147 { 148 if (sg_is_last(sg)) 149 return (NULL); 150 sg++; 151 if (sg_is_chain(sg)) 152 sg = sg_chain_ptr(sg); 153 return (sg); 154 } 155 156 static inline vm_paddr_t 157 sg_phys(struct scatterlist *sg) 158 { 159 return (page_to_phys(sg_page(sg)) + sg->offset); 160 } 161 162 static inline void * 163 sg_virt(struct scatterlist *sg) 164 { 165 166 return ((void *)((unsigned long)page_address(sg_page(sg)) + sg->offset)); 167 } 168 169 static inline void 170 sg_chain(struct scatterlist *prv, unsigned int prv_nents, 171 struct scatterlist *sgl) 172 { 173 struct scatterlist *sg = &prv[prv_nents - 1]; 174 175 sg->offset = 0; 176 sg->length = 0; 177 sg->page_link = ((unsigned long)sgl | 178 SG_PAGE_LINK_CHAIN) & ~SG_PAGE_LINK_LAST; 179 } 180 181 static inline void 182 sg_mark_end(struct scatterlist *sg) 183 { 184 sg->page_link |= SG_PAGE_LINK_LAST; 185 sg->page_link &= ~SG_PAGE_LINK_CHAIN; 186 } 187 188 static inline void 189 sg_init_table(struct scatterlist *sg, unsigned int nents) 190 { 191 bzero(sg, sizeof(*sg) * nents); 192 sg_mark_end(&sg[nents - 1]); 193 } 194 195 static inline void 196 sg_init_one(struct scatterlist *sg, const void *buf, unsigned int buflen) 197 { 198 sg_init_table(sg, 1); 199 sg_set_buf(sg, buf, buflen); 200 } 201 202 static struct scatterlist * 203 sg_kmalloc(unsigned int nents, gfp_t gfp_mask) 204 { 205 if (nents == SG_MAX_SINGLE_ALLOC) { 206 return ((void *)__get_free_page(gfp_mask)); 207 } else 208 return (kmalloc(nents * sizeof(struct scatterlist), gfp_mask)); 209 } 210 211 static inline void 212 sg_kfree(struct scatterlist *sg, unsigned int nents) 213 { 214 if (nents == SG_MAX_SINGLE_ALLOC) { 215 free_page((unsigned long)sg); 216 } else 217 kfree(sg); 218 } 219 220 static inline void 221 __sg_free_table(struct sg_table *table, unsigned int max_ents, 222 bool skip_first_chunk, sg_free_fn * free_fn) 223 { 224 struct scatterlist *sgl, *next; 225 226 if (unlikely(!table->sgl)) 227 return; 228 229 sgl = table->sgl; 230 while (table->orig_nents) { 231 unsigned int alloc_size = table->orig_nents; 232 unsigned int sg_size; 233 234 if (alloc_size > max_ents) { 235 next = sg_chain_ptr(&sgl[max_ents - 1]); 236 alloc_size = max_ents; 237 sg_size = alloc_size - 1; 238 } else { 239 sg_size = alloc_size; 240 next = NULL; 241 } 242 243 table->orig_nents -= sg_size; 244 if (skip_first_chunk) 245 skip_first_chunk = 0; 246 else 247 free_fn(sgl, alloc_size); 248 sgl = next; 249 } 250 251 table->sgl = NULL; 252 } 253 254 static inline void 255 sg_free_table(struct sg_table *table) 256 { 257 __sg_free_table(table, SG_MAX_SINGLE_ALLOC, 0, sg_kfree); 258 } 259 260 static inline int 261 __sg_alloc_table(struct sg_table *table, unsigned int nents, 262 unsigned int max_ents, struct scatterlist *first_chunk, 263 gfp_t gfp_mask, sg_alloc_fn *alloc_fn) 264 { 265 struct scatterlist *sg, *prv; 266 unsigned int left; 267 268 memset(table, 0, sizeof(*table)); 269 270 if (nents == 0) 271 return (-EINVAL); 272 left = nents; 273 prv = NULL; 274 do { 275 unsigned int sg_size; 276 unsigned int alloc_size = left; 277 278 if (alloc_size > max_ents) { 279 alloc_size = max_ents; 280 sg_size = alloc_size - 1; 281 } else 282 sg_size = alloc_size; 283 284 left -= sg_size; 285 286 if (first_chunk) { 287 sg = first_chunk; 288 first_chunk = NULL; 289 } else { 290 sg = alloc_fn(alloc_size, gfp_mask); 291 } 292 if (unlikely(!sg)) { 293 if (prv) 294 table->nents = ++table->orig_nents; 295 296 return (-ENOMEM); 297 } 298 sg_init_table(sg, alloc_size); 299 table->nents = table->orig_nents += sg_size; 300 301 if (prv) 302 sg_chain(prv, max_ents, sg); 303 else 304 table->sgl = sg; 305 306 if (!left) 307 sg_mark_end(&sg[sg_size - 1]); 308 309 prv = sg; 310 } while (left); 311 312 return (0); 313 } 314 315 static inline int 316 sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask) 317 { 318 int ret; 319 320 ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC, 321 NULL, gfp_mask, sg_kmalloc); 322 if (unlikely(ret)) 323 __sg_free_table(table, SG_MAX_SINGLE_ALLOC, 0, sg_kfree); 324 325 return (ret); 326 } 327 328 #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 51300 329 static inline struct scatterlist * 330 __sg_alloc_table_from_pages(struct sg_table *sgt, 331 struct page **pages, unsigned int count, 332 unsigned long off, unsigned long size, 333 unsigned int max_segment, 334 struct scatterlist *prv, unsigned int left_pages, 335 gfp_t gfp_mask) 336 #else 337 static inline int 338 __sg_alloc_table_from_pages(struct sg_table *sgt, 339 struct page **pages, unsigned int count, 340 unsigned long off, unsigned long size, 341 unsigned int max_segment, gfp_t gfp_mask) 342 #endif 343 { 344 unsigned int i, segs, cur, len; 345 int rc; 346 struct scatterlist *s; 347 348 #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 51300 349 if (prv != NULL) { 350 panic( 351 "Support for prv != NULL not implemented in " 352 "__sg_alloc_table_from_pages()"); 353 } 354 #endif 355 356 if (__predict_false(!max_segment || offset_in_page(max_segment))) 357 #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 51300 358 return (ERR_PTR(-EINVAL)); 359 #else 360 return (-EINVAL); 361 #endif 362 363 len = 0; 364 for (segs = i = 1; i < count; ++i) { 365 len += PAGE_SIZE; 366 if (len >= max_segment || 367 page_to_pfn(pages[i]) != page_to_pfn(pages[i - 1]) + 1) { 368 ++segs; 369 len = 0; 370 } 371 } 372 if (__predict_false((rc = sg_alloc_table(sgt, segs, gfp_mask)))) 373 #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 51300 374 return (ERR_PTR(rc)); 375 #else 376 return (rc); 377 #endif 378 379 cur = 0; 380 for (i = 0, s = sgt->sgl; i < sgt->orig_nents; i++) { 381 unsigned long seg_size; 382 unsigned int j; 383 384 len = 0; 385 for (j = cur + 1; j < count; ++j) { 386 len += PAGE_SIZE; 387 if (len >= max_segment || page_to_pfn(pages[j]) != 388 page_to_pfn(pages[j - 1]) + 1) 389 break; 390 } 391 392 seg_size = ((j - cur) << PAGE_SHIFT) - off; 393 sg_set_page(s, pages[cur], MIN(size, seg_size), off); 394 size -= seg_size; 395 off = 0; 396 cur = j; 397 398 s = sg_next(s); 399 } 400 KASSERT(s != NULL, ("s is NULL after loop in __sg_alloc_table_from_pages()")); 401 402 #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 51300 403 if (left_pages == 0) 404 sg_mark_end(s); 405 406 return (s); 407 #else 408 return (0); 409 #endif 410 } 411 412 static inline int 413 sg_alloc_table_from_pages(struct sg_table *sgt, 414 struct page **pages, unsigned int count, 415 unsigned long off, unsigned long size, 416 gfp_t gfp_mask) 417 { 418 419 #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 51300 420 return (PTR_ERR_OR_ZERO(__sg_alloc_table_from_pages(sgt, pages, count, off, size, 421 SCATTERLIST_MAX_SEGMENT, NULL, 0, gfp_mask))); 422 #else 423 return (__sg_alloc_table_from_pages(sgt, pages, count, off, size, 424 SCATTERLIST_MAX_SEGMENT, gfp_mask)); 425 #endif 426 } 427 428 static inline int 429 sg_alloc_table_from_pages_segment(struct sg_table *sgt, 430 struct page **pages, unsigned int count, unsigned int off, 431 unsigned long size, unsigned int max_segment, gfp_t gfp_mask) 432 { 433 #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 51300 434 return (PTR_ERR_OR_ZERO(__sg_alloc_table_from_pages(sgt, pages, count, off, size, 435 max_segment, NULL, 0, gfp_mask))); 436 #else 437 return (__sg_alloc_table_from_pages(sgt, pages, count, off, size, 438 max_segment, gfp_mask)); 439 #endif 440 } 441 442 static inline int 443 sg_nents(struct scatterlist *sg) 444 { 445 int nents; 446 447 for (nents = 0; sg; sg = sg_next(sg)) 448 nents++; 449 return (nents); 450 } 451 452 static inline void 453 __sg_page_iter_start(struct sg_page_iter *piter, 454 struct scatterlist *sglist, unsigned int nents, 455 unsigned long pgoffset) 456 { 457 piter->internal.pg_advance = 0; 458 piter->internal.nents = nents; 459 460 piter->sg = sglist; 461 piter->sg_pgoffset = pgoffset; 462 } 463 464 static inline void 465 _sg_iter_next(struct sg_page_iter *iter) 466 { 467 struct scatterlist *sg; 468 unsigned int pgcount; 469 470 sg = iter->sg; 471 pgcount = (sg->offset + sg->length + PAGE_SIZE - 1) >> PAGE_SHIFT; 472 473 ++iter->sg_pgoffset; 474 while (iter->sg_pgoffset >= pgcount) { 475 iter->sg_pgoffset -= pgcount; 476 sg = sg_next(sg); 477 --iter->maxents; 478 if (sg == NULL || iter->maxents == 0) 479 break; 480 pgcount = (sg->offset + sg->length + PAGE_SIZE - 1) >> PAGE_SHIFT; 481 } 482 iter->sg = sg; 483 } 484 485 static inline int 486 sg_page_count(struct scatterlist *sg) 487 { 488 return (PAGE_ALIGN(sg->offset + sg->length) >> PAGE_SHIFT); 489 } 490 #define sg_dma_page_count(sg) \ 491 sg_page_count(sg) 492 493 static inline bool 494 __sg_page_iter_next(struct sg_page_iter *piter) 495 { 496 unsigned int pgcount; 497 498 if (piter->internal.nents == 0) 499 return (0); 500 if (piter->sg == NULL) 501 return (0); 502 503 piter->sg_pgoffset += piter->internal.pg_advance; 504 piter->internal.pg_advance = 1; 505 506 while (1) { 507 pgcount = sg_page_count(piter->sg); 508 if (likely(piter->sg_pgoffset < pgcount)) 509 break; 510 piter->sg_pgoffset -= pgcount; 511 piter->sg = sg_next(piter->sg); 512 if (--piter->internal.nents == 0) 513 return (0); 514 if (piter->sg == NULL) 515 return (0); 516 } 517 return (1); 518 } 519 #define __sg_page_iter_dma_next(itr) \ 520 __sg_page_iter_next(&(itr)->base) 521 522 static inline void 523 _sg_iter_init(struct scatterlist *sgl, struct sg_page_iter *iter, 524 unsigned int nents, unsigned long pgoffset) 525 { 526 if (nents) { 527 iter->sg = sgl; 528 iter->sg_pgoffset = pgoffset - 1; 529 iter->maxents = nents; 530 _sg_iter_next(iter); 531 } else { 532 iter->sg = NULL; 533 iter->sg_pgoffset = 0; 534 iter->maxents = 0; 535 } 536 } 537 538 /* 539 * sg_page_iter_dma_address() is implemented as a macro because it 540 * needs to accept two different and identical structure types. This 541 * allows both old and new code to co-exist. The compile time assert 542 * adds some safety, that the structure sizes match. 543 */ 544 #define sg_page_iter_dma_address(spi) ({ \ 545 struct sg_page_iter *__spi = (void *)(spi); \ 546 dma_addr_t __dma_address; \ 547 CTASSERT(sizeof(*(spi)) == sizeof(*__spi)); \ 548 __dma_address = __spi->sg->dma_address + \ 549 (__spi->sg_pgoffset << PAGE_SHIFT); \ 550 __dma_address; \ 551 }) 552 553 static inline struct page * 554 sg_page_iter_page(struct sg_page_iter *piter) 555 { 556 return (nth_page(sg_page(piter->sg), piter->sg_pgoffset)); 557 } 558 559 static __inline size_t 560 sg_pcopy_from_buffer(struct scatterlist *sgl, unsigned int nents, 561 const void *buf, size_t buflen, off_t skip) 562 { 563 struct sg_page_iter piter; 564 struct page *page; 565 struct sf_buf *sf; 566 size_t len, copied; 567 char *p, *b; 568 569 if (buflen == 0) 570 return (0); 571 572 b = __DECONST(char *, buf); 573 copied = 0; 574 sched_pin(); 575 for_each_sg_page(sgl, &piter, nents, 0) { 576 577 /* Skip to the start. */ 578 if (piter.sg->length <= skip) { 579 skip -= piter.sg->length; 580 continue; 581 } 582 583 /* See how much to copy. */ 584 KASSERT(((piter.sg->length - skip) != 0 && (buflen != 0)), 585 ("%s: sg len %u - skip %ju || buflen %zu is 0\n", 586 __func__, piter.sg->length, (uintmax_t)skip, buflen)); 587 len = min(piter.sg->length - skip, buflen); 588 589 page = sg_page_iter_page(&piter); 590 sf = sf_buf_alloc(page, SFB_CPUPRIVATE | SFB_NOWAIT); 591 if (sf == NULL) 592 break; 593 p = (char *)sf_buf_kva(sf) + piter.sg_pgoffset + skip; 594 memcpy(p, b, len); 595 sf_buf_free(sf); 596 597 /* We copied so nothing more to skip. */ 598 skip = 0; 599 copied += len; 600 /* Either we exactly filled the page, or we are done. */ 601 buflen -= len; 602 if (buflen == 0) 603 break; 604 b += len; 605 } 606 sched_unpin(); 607 608 return (copied); 609 } 610 611 static inline size_t 612 sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents, 613 const void *buf, size_t buflen) 614 { 615 return (sg_pcopy_from_buffer(sgl, nents, buf, buflen, 0)); 616 } 617 618 static inline size_t 619 sg_pcopy_to_buffer(struct scatterlist *sgl, unsigned int nents, 620 void *buf, size_t buflen, off_t offset) 621 { 622 struct sg_page_iter iter; 623 struct scatterlist *sg; 624 struct page *page; 625 struct sf_buf *sf; 626 char *vaddr; 627 size_t total = 0; 628 size_t len; 629 630 if (!PMAP_HAS_DMAP) 631 sched_pin(); 632 for_each_sg_page(sgl, &iter, nents, 0) { 633 sg = iter.sg; 634 635 if (offset >= sg->length) { 636 offset -= sg->length; 637 continue; 638 } 639 len = ulmin(buflen, sg->length - offset); 640 if (len == 0) 641 break; 642 643 page = sg_page_iter_page(&iter); 644 if (!PMAP_HAS_DMAP) { 645 sf = sf_buf_alloc(page, SFB_CPUPRIVATE | SFB_NOWAIT); 646 if (sf == NULL) 647 break; 648 vaddr = (char *)sf_buf_kva(sf); 649 } else 650 vaddr = (char *)PHYS_TO_DMAP(page_to_phys(page)); 651 memcpy(buf, vaddr + sg->offset + offset, len); 652 if (!PMAP_HAS_DMAP) 653 sf_buf_free(sf); 654 655 /* start at beginning of next page */ 656 offset = 0; 657 658 /* advance buffer */ 659 buf = (char *)buf + len; 660 buflen -= len; 661 total += len; 662 } 663 if (!PMAP_HAS_DMAP) 664 sched_unpin(); 665 return (total); 666 } 667 668 #endif /* _LINUXKPI_LINUX_SCATTERLIST_H_ */ 669