diff options
Diffstat (limited to 'drivers/scsi/scsi_lib.c')
| -rw-r--r-- | drivers/scsi/scsi_lib.c | 238 |
1 files changed, 185 insertions, 53 deletions
diff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c index 207f1aa08869..aac8a02cbe80 100644 --- a/drivers/scsi/scsi_lib.c +++ b/drivers/scsi/scsi_lib.c | |||
| @@ -17,6 +17,7 @@ | |||
| 17 | #include <linux/pci.h> | 17 | #include <linux/pci.h> |
| 18 | #include <linux/delay.h> | 18 | #include <linux/delay.h> |
| 19 | #include <linux/hardirq.h> | 19 | #include <linux/hardirq.h> |
| 20 | #include <linux/scatterlist.h> | ||
| 20 | 21 | ||
| 21 | #include <scsi/scsi.h> | 22 | #include <scsi/scsi.h> |
| 22 | #include <scsi/scsi_cmnd.h> | 23 | #include <scsi/scsi_cmnd.h> |
| @@ -33,35 +34,34 @@ | |||
| 33 | #define SG_MEMPOOL_NR ARRAY_SIZE(scsi_sg_pools) | 34 | #define SG_MEMPOOL_NR ARRAY_SIZE(scsi_sg_pools) |
| 34 | #define SG_MEMPOOL_SIZE 2 | 35 | #define SG_MEMPOOL_SIZE 2 |
| 35 | 36 | ||
| 37 | /* | ||
| 38 | * The maximum number of SG segments that we will put inside a scatterlist | ||
| 39 | * (unless chaining is used). Should ideally fit inside a single page, to | ||
| 40 | * avoid a higher order allocation. | ||
| 41 | */ | ||
| 42 | #define SCSI_MAX_SG_SEGMENTS 128 | ||
| 43 | |||
| 36 | struct scsi_host_sg_pool { | 44 | struct scsi_host_sg_pool { |
| 37 | size_t size; | 45 | size_t size; |
| 38 | char *name; | 46 | char *name; |
| 39 | struct kmem_cache *slab; | 47 | struct kmem_cache *slab; |
| 40 | mempool_t *pool; | 48 | mempool_t *pool; |
| 41 | }; | 49 | }; |
| 42 | 50 | ||
| 43 | #if (SCSI_MAX_PHYS_SEGMENTS < 32) | 51 | #define SP(x) { x, "sgpool-" #x } |
| 44 | #error SCSI_MAX_PHYS_SEGMENTS is too small | ||
| 45 | #endif | ||
| 46 | |||
| 47 | #define SP(x) { x, "sgpool-" #x } | ||
| 48 | static struct scsi_host_sg_pool scsi_sg_pools[] = { | 52 | static struct scsi_host_sg_pool scsi_sg_pools[] = { |
| 49 | SP(8), | 53 | SP(8), |
| 50 | SP(16), | 54 | SP(16), |
| 55 | #if (SCSI_MAX_SG_SEGMENTS > 16) | ||
| 51 | SP(32), | 56 | SP(32), |
| 52 | #if (SCSI_MAX_PHYS_SEGMENTS > 32) | 57 | #if (SCSI_MAX_SG_SEGMENTS > 32) |
| 53 | SP(64), | 58 | SP(64), |
| 54 | #if (SCSI_MAX_PHYS_SEGMENTS > 64) | 59 | #if (SCSI_MAX_SG_SEGMENTS > 64) |
| 55 | SP(128), | 60 | SP(128), |
| 56 | #if (SCSI_MAX_PHYS_SEGMENTS > 128) | ||
| 57 | SP(256), | ||
| 58 | #if (SCSI_MAX_PHYS_SEGMENTS > 256) | ||
| 59 | #error SCSI_MAX_PHYS_SEGMENTS is too large | ||
| 60 | #endif | ||
| 61 | #endif | 61 | #endif |
| 62 | #endif | 62 | #endif |
| 63 | #endif | 63 | #endif |
| 64 | }; | 64 | }; |
| 65 | #undef SP | 65 | #undef SP |
| 66 | 66 | ||
| 67 | static void scsi_run_queue(struct request_queue *q); | 67 | static void scsi_run_queue(struct request_queue *q); |
| @@ -289,14 +289,16 @@ static int scsi_req_map_sg(struct request *rq, struct scatterlist *sgl, | |||
| 289 | struct request_queue *q = rq->q; | 289 | struct request_queue *q = rq->q; |
| 290 | int nr_pages = (bufflen + sgl[0].offset + PAGE_SIZE - 1) >> PAGE_SHIFT; | 290 | int nr_pages = (bufflen + sgl[0].offset + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 291 | unsigned int data_len = bufflen, len, bytes, off; | 291 | unsigned int data_len = bufflen, len, bytes, off; |
| 292 | struct scatterlist *sg; | ||
| 292 | struct page *page; | 293 | struct page *page; |
| 293 | struct bio *bio = NULL; | 294 | struct bio *bio = NULL; |
| 294 | int i, err, nr_vecs = 0; | 295 | int i, err, nr_vecs = 0; |
| 295 | 296 | ||
| 296 | for (i = 0; i < nsegs; i++) { | 297 | for_each_sg(sgl, sg, nsegs, i) { |
| 297 | page = sgl[i].page; | 298 | page = sg->page; |
| 298 | off = sgl[i].offset; | 299 | off = sg->offset; |
| 299 | len = sgl[i].length; | 300 | len = sg->length; |
| 301 | data_len += len; | ||
| 300 | 302 | ||
| 301 | while (len > 0 && data_len > 0) { | 303 | while (len > 0 && data_len > 0) { |
| 302 | /* | 304 | /* |
| @@ -695,56 +697,170 @@ static struct scsi_cmnd *scsi_end_request(struct scsi_cmnd *cmd, int uptodate, | |||
| 695 | return NULL; | 697 | return NULL; |
| 696 | } | 698 | } |
| 697 | 699 | ||
| 698 | struct scatterlist *scsi_alloc_sgtable(struct scsi_cmnd *cmd, gfp_t gfp_mask) | 700 | /* |
| 699 | { | 701 | * Like SCSI_MAX_SG_SEGMENTS, but for archs that have sg chaining. This limit |
| 700 | struct scsi_host_sg_pool *sgp; | 702 | * is totally arbitrary, a setting of 2048 will get you at least 8mb ios. |
| 701 | struct scatterlist *sgl; | 703 | */ |
| 704 | #define SCSI_MAX_SG_CHAIN_SEGMENTS 2048 | ||
| 702 | 705 | ||
| 703 | BUG_ON(!cmd->use_sg); | 706 | static inline unsigned int scsi_sgtable_index(unsigned short nents) |
| 707 | { | ||
| 708 | unsigned int index; | ||
| 704 | 709 | ||
| 705 | switch (cmd->use_sg) { | 710 | switch (nents) { |
| 706 | case 1 ... 8: | 711 | case 1 ... 8: |
| 707 | cmd->sglist_len = 0; | 712 | index = 0; |
| 708 | break; | 713 | break; |
| 709 | case 9 ... 16: | 714 | case 9 ... 16: |
| 710 | cmd->sglist_len = 1; | 715 | index = 1; |
| 711 | break; | 716 | break; |
| 717 | #if (SCSI_MAX_SG_SEGMENTS > 16) | ||
| 712 | case 17 ... 32: | 718 | case 17 ... 32: |
| 713 | cmd->sglist_len = 2; | 719 | index = 2; |
| 714 | break; | 720 | break; |
| 715 | #if (SCSI_MAX_PHYS_SEGMENTS > 32) | 721 | #if (SCSI_MAX_SG_SEGMENTS > 32) |
| 716 | case 33 ... 64: | 722 | case 33 ... 64: |
| 717 | cmd->sglist_len = 3; | 723 | index = 3; |
| 718 | break; | 724 | break; |
| 719 | #if (SCSI_MAX_PHYS_SEGMENTS > 64) | 725 | #if (SCSI_MAX_SG_SEGMENTS > 64) |
| 720 | case 65 ... 128: | 726 | case 65 ... 128: |
| 721 | cmd->sglist_len = 4; | 727 | index = 4; |
| 722 | break; | ||
| 723 | #if (SCSI_MAX_PHYS_SEGMENTS > 128) | ||
| 724 | case 129 ... 256: | ||
| 725 | cmd->sglist_len = 5; | ||
| 726 | break; | 728 | break; |
| 727 | #endif | 729 | #endif |
| 728 | #endif | 730 | #endif |
| 729 | #endif | 731 | #endif |
| 730 | default: | 732 | default: |
| 731 | return NULL; | 733 | printk(KERN_ERR "scsi: bad segment count=%d\n", nents); |
| 734 | BUG(); | ||
| 732 | } | 735 | } |
| 733 | 736 | ||
| 734 | sgp = scsi_sg_pools + cmd->sglist_len; | 737 | return index; |
| 735 | sgl = mempool_alloc(sgp->pool, gfp_mask); | 738 | } |
| 736 | return sgl; | 739 | |
| 740 | struct scatterlist *scsi_alloc_sgtable(struct scsi_cmnd *cmd, gfp_t gfp_mask) | ||
| 741 | { | ||
| 742 | struct scsi_host_sg_pool *sgp; | ||
| 743 | struct scatterlist *sgl, *prev, *ret; | ||
| 744 | unsigned int index; | ||
| 745 | int this, left; | ||
| 746 | |||
| 747 | BUG_ON(!cmd->use_sg); | ||
| 748 | |||
| 749 | left = cmd->use_sg; | ||
| 750 | ret = prev = NULL; | ||
| 751 | do { | ||
| 752 | this = left; | ||
| 753 | if (this > SCSI_MAX_SG_SEGMENTS) { | ||
| 754 | this = SCSI_MAX_SG_SEGMENTS - 1; | ||
| 755 | index = SG_MEMPOOL_NR - 1; | ||
| 756 | } else | ||
| 757 | index = scsi_sgtable_index(this); | ||
| 758 | |||
| 759 | left -= this; | ||
| 760 | |||
| 761 | sgp = scsi_sg_pools + index; | ||
| 762 | |||
| 763 | sgl = mempool_alloc(sgp->pool, gfp_mask); | ||
| 764 | if (unlikely(!sgl)) | ||
| 765 | goto enomem; | ||
| 766 | |||
| 767 | memset(sgl, 0, sizeof(*sgl) * sgp->size); | ||
| 768 | |||
| 769 | /* | ||
| 770 | * first loop through, set initial index and return value | ||
| 771 | */ | ||
| 772 | if (!ret) | ||
| 773 | ret = sgl; | ||
| 774 | |||
| 775 | /* | ||
| 776 | * chain previous sglist, if any. we know the previous | ||
| 777 | * sglist must be the biggest one, or we would not have | ||
| 778 | * ended up doing another loop. | ||
| 779 | */ | ||
| 780 | if (prev) | ||
| 781 | sg_chain(prev, SCSI_MAX_SG_SEGMENTS, sgl); | ||
| 782 | |||
| 783 | /* | ||
| 784 | * don't allow subsequent mempool allocs to sleep, it would | ||
| 785 | * violate the mempool principle. | ||
| 786 | */ | ||
| 787 | gfp_mask &= ~__GFP_WAIT; | ||
| 788 | gfp_mask |= __GFP_HIGH; | ||
| 789 | prev = sgl; | ||
| 790 | } while (left); | ||
| 791 | |||
| 792 | /* | ||
| 793 | * ->use_sg may get modified after dma mapping has potentially | ||
| 794 | * shrunk the number of segments, so keep a copy of it for free. | ||
| 795 | */ | ||
| 796 | cmd->__use_sg = cmd->use_sg; | ||
| 797 | return ret; | ||
| 798 | enomem: | ||
| 799 | if (ret) { | ||
| 800 | /* | ||
| 801 | * Free entries chained off ret. Since we were trying to | ||
| 802 | * allocate another sglist, we know that all entries are of | ||
| 803 | * the max size. | ||
| 804 | */ | ||
| 805 | sgp = scsi_sg_pools + SG_MEMPOOL_NR - 1; | ||
| 806 | prev = ret; | ||
| 807 | ret = &ret[SCSI_MAX_SG_SEGMENTS - 1]; | ||
| 808 | |||
| 809 | while ((sgl = sg_chain_ptr(ret)) != NULL) { | ||
| 810 | ret = &sgl[SCSI_MAX_SG_SEGMENTS - 1]; | ||
| 811 | mempool_free(sgl, sgp->pool); | ||
| 812 | } | ||
| 813 | |||
| 814 | mempool_free(prev, sgp->pool); | ||
| 815 | } | ||
| 816 | return NULL; | ||
| 737 | } | 817 | } |
| 738 | 818 | ||
| 739 | EXPORT_SYMBOL(scsi_alloc_sgtable); | 819 | EXPORT_SYMBOL(scsi_alloc_sgtable); |
| 740 | 820 | ||
| 741 | void scsi_free_sgtable(struct scatterlist *sgl, int index) | 821 | void scsi_free_sgtable(struct scsi_cmnd *cmd) |
| 742 | { | 822 | { |
| 823 | struct scatterlist *sgl = cmd->request_buffer; | ||
| 743 | struct scsi_host_sg_pool *sgp; | 824 | struct scsi_host_sg_pool *sgp; |
| 744 | 825 | ||
| 745 | BUG_ON(index >= SG_MEMPOOL_NR); | 826 | /* |
| 827 | * if this is the biggest size sglist, check if we have | ||
| 828 | * chained parts we need to free | ||
| 829 | */ | ||
| 830 | if (cmd->__use_sg > SCSI_MAX_SG_SEGMENTS) { | ||
| 831 | unsigned short this, left; | ||
| 832 | struct scatterlist *next; | ||
| 833 | unsigned int index; | ||
| 834 | |||
| 835 | left = cmd->__use_sg - (SCSI_MAX_SG_SEGMENTS - 1); | ||
| 836 | next = sg_chain_ptr(&sgl[SCSI_MAX_SG_SEGMENTS - 1]); | ||
| 837 | while (left && next) { | ||
| 838 | sgl = next; | ||
| 839 | this = left; | ||
| 840 | if (this > SCSI_MAX_SG_SEGMENTS) { | ||
| 841 | this = SCSI_MAX_SG_SEGMENTS - 1; | ||
| 842 | index = SG_MEMPOOL_NR - 1; | ||
| 843 | } else | ||
| 844 | index = scsi_sgtable_index(this); | ||
| 845 | |||
| 846 | left -= this; | ||
| 847 | |||
| 848 | sgp = scsi_sg_pools + index; | ||
| 849 | |||
| 850 | if (left) | ||
| 851 | next = sg_chain_ptr(&sgl[sgp->size - 1]); | ||
| 852 | |||
| 853 | mempool_free(sgl, sgp->pool); | ||
| 854 | } | ||
| 855 | |||
| 856 | /* | ||
| 857 | * Restore original, will be freed below | ||
| 858 | */ | ||
| 859 | sgl = cmd->request_buffer; | ||
| 860 | sgp = scsi_sg_pools + SG_MEMPOOL_NR - 1; | ||
| 861 | } else | ||
| 862 | sgp = scsi_sg_pools + scsi_sgtable_index(cmd->__use_sg); | ||
| 746 | 863 | ||
| 747 | sgp = scsi_sg_pools + index; | ||
| 748 | mempool_free(sgl, sgp->pool); | 864 | mempool_free(sgl, sgp->pool); |
| 749 | } | 865 | } |
| 750 | 866 | ||
| @@ -770,7 +886,7 @@ EXPORT_SYMBOL(scsi_free_sgtable); | |||
| 770 | static void scsi_release_buffers(struct scsi_cmnd *cmd) | 886 | static void scsi_release_buffers(struct scsi_cmnd *cmd) |
| 771 | { | 887 | { |
| 772 | if (cmd->use_sg) | 888 | if (cmd->use_sg) |
| 773 | scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len); | 889 | scsi_free_sgtable(cmd); |
| 774 | 890 | ||
| 775 | /* | 891 | /* |
| 776 | * Zero these out. They now point to freed memory, and it is | 892 | * Zero these out. They now point to freed memory, and it is |
| @@ -984,7 +1100,6 @@ void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes) | |||
| 984 | static int scsi_init_io(struct scsi_cmnd *cmd) | 1100 | static int scsi_init_io(struct scsi_cmnd *cmd) |
| 985 | { | 1101 | { |
| 986 | struct request *req = cmd->request; | 1102 | struct request *req = cmd->request; |
| 987 | struct scatterlist *sgpnt; | ||
| 988 | int count; | 1103 | int count; |
| 989 | 1104 | ||
| 990 | /* | 1105 | /* |
| @@ -997,14 +1112,13 @@ static int scsi_init_io(struct scsi_cmnd *cmd) | |||
| 997 | /* | 1112 | /* |
| 998 | * If sg table allocation fails, requeue request later. | 1113 | * If sg table allocation fails, requeue request later. |
| 999 | */ | 1114 | */ |
| 1000 | sgpnt = scsi_alloc_sgtable(cmd, GFP_ATOMIC); | 1115 | cmd->request_buffer = scsi_alloc_sgtable(cmd, GFP_ATOMIC); |
| 1001 | if (unlikely(!sgpnt)) { | 1116 | if (unlikely(!cmd->request_buffer)) { |
| 1002 | scsi_unprep_request(req); | 1117 | scsi_unprep_request(req); |
| 1003 | return BLKPREP_DEFER; | 1118 | return BLKPREP_DEFER; |
| 1004 | } | 1119 | } |
| 1005 | 1120 | ||
| 1006 | req->buffer = NULL; | 1121 | req->buffer = NULL; |
| 1007 | cmd->request_buffer = (char *) sgpnt; | ||
| 1008 | if (blk_pc_request(req)) | 1122 | if (blk_pc_request(req)) |
| 1009 | cmd->request_bufflen = req->data_len; | 1123 | cmd->request_bufflen = req->data_len; |
| 1010 | else | 1124 | else |
| @@ -1529,8 +1643,25 @@ struct request_queue *__scsi_alloc_queue(struct Scsi_Host *shost, | |||
| 1529 | if (!q) | 1643 | if (!q) |
| 1530 | return NULL; | 1644 | return NULL; |
| 1531 | 1645 | ||
| 1646 | /* | ||
| 1647 | * this limit is imposed by hardware restrictions | ||
| 1648 | */ | ||
| 1532 | blk_queue_max_hw_segments(q, shost->sg_tablesize); | 1649 | blk_queue_max_hw_segments(q, shost->sg_tablesize); |
| 1533 | blk_queue_max_phys_segments(q, SCSI_MAX_PHYS_SEGMENTS); | 1650 | |
| 1651 | /* | ||
| 1652 | * In the future, sg chaining support will be mandatory and this | ||
| 1653 | * ifdef can then go away. Right now we don't have all archs | ||
| 1654 | * converted, so better keep it safe. | ||
| 1655 | */ | ||
| 1656 | #ifdef ARCH_HAS_SG_CHAIN | ||
| 1657 | if (shost->use_sg_chaining) | ||
| 1658 | blk_queue_max_phys_segments(q, SCSI_MAX_SG_CHAIN_SEGMENTS); | ||
| 1659 | else | ||
| 1660 | blk_queue_max_phys_segments(q, SCSI_MAX_SG_SEGMENTS); | ||
| 1661 | #else | ||
| 1662 | blk_queue_max_phys_segments(q, SCSI_MAX_SG_SEGMENTS); | ||
| 1663 | #endif | ||
| 1664 | |||
| 1534 | blk_queue_max_sectors(q, shost->max_sectors); | 1665 | blk_queue_max_sectors(q, shost->max_sectors); |
| 1535 | blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost)); | 1666 | blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost)); |
| 1536 | blk_queue_segment_boundary(q, shost->dma_boundary); | 1667 | blk_queue_segment_boundary(q, shost->dma_boundary); |
| @@ -2193,18 +2324,19 @@ EXPORT_SYMBOL_GPL(scsi_target_unblock); | |||
| 2193 | * | 2324 | * |
| 2194 | * Returns virtual address of the start of the mapped page | 2325 | * Returns virtual address of the start of the mapped page |
| 2195 | */ | 2326 | */ |
| 2196 | void *scsi_kmap_atomic_sg(struct scatterlist *sg, int sg_count, | 2327 | void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count, |
| 2197 | size_t *offset, size_t *len) | 2328 | size_t *offset, size_t *len) |
| 2198 | { | 2329 | { |
| 2199 | int i; | 2330 | int i; |
| 2200 | size_t sg_len = 0, len_complete = 0; | 2331 | size_t sg_len = 0, len_complete = 0; |
| 2332 | struct scatterlist *sg; | ||
| 2201 | struct page *page; | 2333 | struct page *page; |
| 2202 | 2334 | ||
| 2203 | WARN_ON(!irqs_disabled()); | 2335 | WARN_ON(!irqs_disabled()); |
| 2204 | 2336 | ||
| 2205 | for (i = 0; i < sg_count; i++) { | 2337 | for_each_sg(sgl, sg, sg_count, i) { |
| 2206 | len_complete = sg_len; /* Complete sg-entries */ | 2338 | len_complete = sg_len; /* Complete sg-entries */ |
| 2207 | sg_len += sg[i].length; | 2339 | sg_len += sg->length; |
| 2208 | if (sg_len > *offset) | 2340 | if (sg_len > *offset) |
| 2209 | break; | 2341 | break; |
| 2210 | } | 2342 | } |
| @@ -2218,10 +2350,10 @@ void *scsi_kmap_atomic_sg(struct scatterlist *sg, int sg_count, | |||
| 2218 | } | 2350 | } |
| 2219 | 2351 | ||
| 2220 | /* Offset starting from the beginning of first page in this sg-entry */ | 2352 | /* Offset starting from the beginning of first page in this sg-entry */ |
| 2221 | *offset = *offset - len_complete + sg[i].offset; | 2353 | *offset = *offset - len_complete + sg->offset; |
| 2222 | 2354 | ||
| 2223 | /* Assumption: contiguous pages can be accessed as "page + i" */ | 2355 | /* Assumption: contiguous pages can be accessed as "page + i" */ |
| 2224 | page = nth_page(sg[i].page, (*offset >> PAGE_SHIFT)); | 2356 | page = nth_page(sg->page, (*offset >> PAGE_SHIFT)); |
| 2225 | *offset &= ~PAGE_MASK; | 2357 | *offset &= ~PAGE_MASK; |
| 2226 | 2358 | ||
| 2227 | /* Bytes in this sg-entry from *offset to the end of the page */ | 2359 | /* Bytes in this sg-entry from *offset to the end of the page */ |
