diff options
author | Robert Love <robert.w.love@intel.com> | 2009-11-03 14:47:28 -0500 |
---|---|---|
committer | James Bottomley <James.Bottomley@suse.de> | 2009-12-04 13:01:05 -0500 |
commit | 5868287460b0fc243e828a0b856cd53d8bf45739 (patch) | |
tree | 81b18b5e3a104b3202b8e2583d9bd3ffc5546895 /drivers/scsi/libfc/fc_libfc.c | |
parent | 07aac328342d6ca1725d901e1c5da8a1aa88f557 (diff) |
[SCSI] libfc: Add routine to copy data from a buffer to a SG list
When handling the multi-frame responses of fc pass-thru requests,
a code segment similar to fc_fcp_recv_data (routine to receive
inbound SCSI data) is used in the response handler. This patch
is to add a routine, called fc_copy_buffer_to_sglist(), to handle
the common function of copying data from a buffer to a scatter-
gather list in order to avoid code duplication.
Signed-off-by: Robert Love <robert.w.love@intel.com>
Signed-off-by: James Bottomley <James.Bottomley@suse.de>
Diffstat (limited to 'drivers/scsi/libfc/fc_libfc.c')
-rw-r--r-- | drivers/scsi/libfc/fc_libfc.c | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/drivers/scsi/libfc/fc_libfc.c b/drivers/scsi/libfc/fc_libfc.c index 01418ae8cb84..295eafb0316f 100644 --- a/drivers/scsi/libfc/fc_libfc.c +++ b/drivers/scsi/libfc/fc_libfc.c | |||
@@ -72,3 +72,63 @@ static void __exit libfc_exit(void) | |||
72 | fc_destroy_rport(); | 72 | fc_destroy_rport(); |
73 | } | 73 | } |
74 | module_exit(libfc_exit); | 74 | module_exit(libfc_exit); |
75 | |||
76 | /** | ||
77 | * fc_copy_buffer_to_sglist() - This routine copies the data of a buffer | ||
78 | * into a scatter-gather list (SG list). | ||
79 | * | ||
80 | * @buf: pointer to the data buffer. | ||
81 | * @len: the byte-length of the data buffer. | ||
82 | * @sg: pointer to the pointer of the SG list. | ||
83 | * @nents: pointer to the remaining number of entries in the SG list. | ||
84 | * @offset: pointer to the current offset in the SG list. | ||
85 | * @km_type: dedicated page table slot type for kmap_atomic. | ||
86 | * @crc: pointer to the 32-bit crc value. | ||
87 | * If crc is NULL, CRC is not calculated. | ||
88 | */ | ||
89 | u32 fc_copy_buffer_to_sglist(void *buf, size_t len, | ||
90 | struct scatterlist *sg, | ||
91 | u32 *nents, size_t *offset, | ||
92 | enum km_type km_type, u32 *crc) | ||
93 | { | ||
94 | size_t remaining = len; | ||
95 | u32 copy_len = 0; | ||
96 | |||
97 | while (remaining > 0 && sg) { | ||
98 | size_t off, sg_bytes; | ||
99 | void *page_addr; | ||
100 | |||
101 | if (*offset >= sg->length) { | ||
102 | /* | ||
103 | * Check for end and drop resources | ||
104 | * from the last iteration. | ||
105 | */ | ||
106 | if (!(*nents)) | ||
107 | break; | ||
108 | --(*nents); | ||
109 | *offset -= sg->length; | ||
110 | sg = sg_next(sg); | ||
111 | continue; | ||
112 | } | ||
113 | sg_bytes = min(remaining, sg->length - *offset); | ||
114 | |||
115 | /* | ||
116 | * The scatterlist item may be bigger than PAGE_SIZE, | ||
117 | * but we are limited to mapping PAGE_SIZE at a time. | ||
118 | */ | ||
119 | off = *offset + sg->offset; | ||
120 | sg_bytes = min(sg_bytes, | ||
121 | (size_t)(PAGE_SIZE - (off & ~PAGE_MASK))); | ||
122 | page_addr = kmap_atomic(sg_page(sg) + (off >> PAGE_SHIFT), | ||
123 | km_type); | ||
124 | if (crc) | ||
125 | *crc = crc32(*crc, buf, sg_bytes); | ||
126 | memcpy((char *)page_addr + (off & ~PAGE_MASK), buf, sg_bytes); | ||
127 | kunmap_atomic(page_addr, km_type); | ||
128 | buf += sg_bytes; | ||
129 | *offset += sg_bytes; | ||
130 | remaining -= sg_bytes; | ||
131 | copy_len += sg_bytes; | ||
132 | } | ||
133 | return copy_len; | ||
134 | } | ||