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 | |
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')
-rw-r--r-- | drivers/scsi/libfc/fc_fcp.c | 56 | ||||
-rw-r--r-- | drivers/scsi/libfc/fc_libfc.c | 60 | ||||
-rw-r--r-- | drivers/scsi/libfc/fc_libfc.h | 8 |
3 files changed, 78 insertions, 46 deletions
diff --git a/drivers/scsi/libfc/fc_fcp.c b/drivers/scsi/libfc/fc_fcp.c index 866f78ac4ec2..98279fe0d0c7 100644 --- a/drivers/scsi/libfc/fc_fcp.c +++ b/drivers/scsi/libfc/fc_fcp.c | |||
@@ -323,7 +323,7 @@ static void fc_fcp_recv_data(struct fc_fcp_pkt *fsp, struct fc_frame *fp) | |||
323 | size_t len; | 323 | size_t len; |
324 | void *buf; | 324 | void *buf; |
325 | struct scatterlist *sg; | 325 | struct scatterlist *sg; |
326 | size_t remaining; | 326 | u32 nents; |
327 | 327 | ||
328 | fh = fc_frame_header_get(fp); | 328 | fh = fc_frame_header_get(fp); |
329 | offset = ntohl(fh->fh_parm_offset); | 329 | offset = ntohl(fh->fh_parm_offset); |
@@ -347,55 +347,19 @@ static void fc_fcp_recv_data(struct fc_fcp_pkt *fsp, struct fc_frame *fp) | |||
347 | if (offset != fsp->xfer_len) | 347 | if (offset != fsp->xfer_len) |
348 | fsp->state |= FC_SRB_DISCONTIG; | 348 | fsp->state |= FC_SRB_DISCONTIG; |
349 | 349 | ||
350 | crc = 0; | ||
351 | if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) | ||
352 | crc = crc32(~0, (u8 *) fh, sizeof(*fh)); | ||
353 | |||
354 | sg = scsi_sglist(sc); | 350 | sg = scsi_sglist(sc); |
355 | remaining = len; | 351 | nents = scsi_sg_count(sc); |
356 | |||
357 | while (remaining > 0 && sg) { | ||
358 | size_t off; | ||
359 | void *page_addr; | ||
360 | size_t sg_bytes; | ||
361 | |||
362 | if (offset >= sg->length) { | ||
363 | offset -= sg->length; | ||
364 | sg = sg_next(sg); | ||
365 | continue; | ||
366 | } | ||
367 | sg_bytes = min(remaining, sg->length - offset); | ||
368 | |||
369 | /* | ||
370 | * The scatterlist item may be bigger than PAGE_SIZE, | ||
371 | * but we are limited to mapping PAGE_SIZE at a time. | ||
372 | */ | ||
373 | off = offset + sg->offset; | ||
374 | sg_bytes = min(sg_bytes, (size_t) | ||
375 | (PAGE_SIZE - (off & ~PAGE_MASK))); | ||
376 | page_addr = kmap_atomic(sg_page(sg) + (off >> PAGE_SHIFT), | ||
377 | KM_SOFTIRQ0); | ||
378 | if (!page_addr) | ||
379 | break; /* XXX panic? */ | ||
380 | |||
381 | if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) | ||
382 | crc = crc32(crc, buf, sg_bytes); | ||
383 | memcpy((char *)page_addr + (off & ~PAGE_MASK), buf, | ||
384 | sg_bytes); | ||
385 | |||
386 | kunmap_atomic(page_addr, KM_SOFTIRQ0); | ||
387 | buf += sg_bytes; | ||
388 | offset += sg_bytes; | ||
389 | remaining -= sg_bytes; | ||
390 | copy_len += sg_bytes; | ||
391 | } | ||
392 | 352 | ||
393 | if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) { | 353 | if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED)) { |
354 | copy_len = fc_copy_buffer_to_sglist(buf, len, sg, &nents, | ||
355 | &offset, KM_SOFTIRQ0, NULL); | ||
356 | } else { | ||
357 | crc = crc32(~0, (u8 *) fh, sizeof(*fh)); | ||
358 | copy_len = fc_copy_buffer_to_sglist(buf, len, sg, &nents, | ||
359 | &offset, KM_SOFTIRQ0, &crc); | ||
394 | buf = fc_frame_payload_get(fp, 0); | 360 | buf = fc_frame_payload_get(fp, 0); |
395 | if (len % 4) { | 361 | if (len % 4) |
396 | crc = crc32(crc, buf + len, 4 - (len % 4)); | 362 | crc = crc32(crc, buf + len, 4 - (len % 4)); |
397 | len += 4 - (len % 4); | ||
398 | } | ||
399 | 363 | ||
400 | if (~crc != le32_to_cpu(fr_crc(fp))) { | 364 | if (~crc != le32_to_cpu(fr_crc(fp))) { |
401 | crc_err: | 365 | crc_err: |
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 | } | ||
diff --git a/drivers/scsi/libfc/fc_libfc.h b/drivers/scsi/libfc/fc_libfc.h index 0530149ac174..e4b5e9280cb0 100644 --- a/drivers/scsi/libfc/fc_libfc.h +++ b/drivers/scsi/libfc/fc_libfc.h | |||
@@ -101,4 +101,12 @@ void fc_destroy_fcp(void); | |||
101 | */ | 101 | */ |
102 | const char *fc_els_resp_type(struct fc_frame *); | 102 | const char *fc_els_resp_type(struct fc_frame *); |
103 | 103 | ||
104 | /* | ||
105 | * Copies a buffer into an sg list | ||
106 | */ | ||
107 | u32 fc_copy_buffer_to_sglist(void *buf, size_t len, | ||
108 | struct scatterlist *sg, | ||
109 | u32 *nents, size_t *offset, | ||
110 | enum km_type km_type, u32 *crc); | ||
111 | |||
104 | #endif /* _FC_LIBFC_H_ */ | 112 | #endif /* _FC_LIBFC_H_ */ |