fc_libfc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright(c) 2009 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Maintained at www.Open-FCoE.org
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/types.h>
  21. #include <linux/scatterlist.h>
  22. #include <linux/crc32.h>
  23. #include <scsi/libfc.h>
  24. #include <scsi/fc_encode.h>
  25. #include "fc_libfc.h"
  26. MODULE_AUTHOR("Open-FCoE.org");
  27. MODULE_DESCRIPTION("libfc");
  28. MODULE_LICENSE("GPL v2");
  29. unsigned int fc_debug_logging;
  30. module_param_named(debug_logging, fc_debug_logging, int, S_IRUGO|S_IWUSR);
  31. MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
  32. /**
  33. * libfc_init() - Initialize libfc.ko
  34. */
  35. static int __init libfc_init(void)
  36. {
  37. int rc = 0;
  38. rc = fc_setup_fcp();
  39. if (rc)
  40. return rc;
  41. rc = fc_setup_exch_mgr();
  42. if (rc)
  43. goto destroy_pkt_cache;
  44. rc = fc_setup_rport();
  45. if (rc)
  46. goto destroy_em;
  47. return rc;
  48. destroy_em:
  49. fc_destroy_exch_mgr();
  50. destroy_pkt_cache:
  51. fc_destroy_fcp();
  52. return rc;
  53. }
  54. module_init(libfc_init);
  55. /**
  56. * libfc_exit() - Tear down libfc.ko
  57. */
  58. static void __exit libfc_exit(void)
  59. {
  60. fc_destroy_fcp();
  61. fc_destroy_exch_mgr();
  62. fc_destroy_rport();
  63. }
  64. module_exit(libfc_exit);
  65. /**
  66. * fc_copy_buffer_to_sglist() - This routine copies the data of a buffer
  67. * into a scatter-gather list (SG list).
  68. *
  69. * @buf: pointer to the data buffer.
  70. * @len: the byte-length of the data buffer.
  71. * @sg: pointer to the pointer of the SG list.
  72. * @nents: pointer to the remaining number of entries in the SG list.
  73. * @offset: pointer to the current offset in the SG list.
  74. * @km_type: dedicated page table slot type for kmap_atomic.
  75. * @crc: pointer to the 32-bit crc value.
  76. * If crc is NULL, CRC is not calculated.
  77. */
  78. u32 fc_copy_buffer_to_sglist(void *buf, size_t len,
  79. struct scatterlist *sg,
  80. u32 *nents, size_t *offset,
  81. enum km_type km_type, u32 *crc)
  82. {
  83. size_t remaining = len;
  84. u32 copy_len = 0;
  85. while (remaining > 0 && sg) {
  86. size_t off, sg_bytes;
  87. void *page_addr;
  88. if (*offset >= sg->length) {
  89. /*
  90. * Check for end and drop resources
  91. * from the last iteration.
  92. */
  93. if (!(*nents))
  94. break;
  95. --(*nents);
  96. *offset -= sg->length;
  97. sg = sg_next(sg);
  98. continue;
  99. }
  100. sg_bytes = min(remaining, sg->length - *offset);
  101. /*
  102. * The scatterlist item may be bigger than PAGE_SIZE,
  103. * but we are limited to mapping PAGE_SIZE at a time.
  104. */
  105. off = *offset + sg->offset;
  106. sg_bytes = min(sg_bytes,
  107. (size_t)(PAGE_SIZE - (off & ~PAGE_MASK)));
  108. page_addr = kmap_atomic(sg_page(sg) + (off >> PAGE_SHIFT),
  109. km_type);
  110. if (crc)
  111. *crc = crc32(*crc, buf, sg_bytes);
  112. memcpy((char *)page_addr + (off & ~PAGE_MASK), buf, sg_bytes);
  113. kunmap_atomic(page_addr, km_type);
  114. buf += sg_bytes;
  115. *offset += sg_bytes;
  116. remaining -= sg_bytes;
  117. copy_len += sg_bytes;
  118. }
  119. return copy_len;
  120. }
  121. /**
  122. * fc_fill_hdr() - fill FC header fields based on request
  123. * @fp: reply frame containing header to be filled in
  124. * @in_fp: request frame containing header to use in filling in reply
  125. * @r_ctl: R_CTL value for header
  126. * @f_ctl: F_CTL value for header, with 0 pad
  127. * @seq_cnt: sequence count for the header, ignored if frame has a sequence
  128. * @parm_offset: parameter / offset value
  129. */
  130. void fc_fill_hdr(struct fc_frame *fp, const struct fc_frame *in_fp,
  131. enum fc_rctl r_ctl, u32 f_ctl, u16 seq_cnt, u32 parm_offset)
  132. {
  133. struct fc_frame_header *fh;
  134. struct fc_frame_header *in_fh;
  135. struct fc_seq *sp;
  136. u32 fill;
  137. fh = __fc_frame_header_get(fp);
  138. in_fh = __fc_frame_header_get(in_fp);
  139. if (f_ctl & FC_FC_END_SEQ) {
  140. fill = -fr_len(fp) & 3;
  141. if (fill) {
  142. /* TODO, this may be a problem with fragmented skb */
  143. memset(skb_put(fp_skb(fp), fill), 0, fill);
  144. f_ctl |= fill;
  145. }
  146. fr_eof(fp) = FC_EOF_T;
  147. } else {
  148. WARN_ON(fr_len(fp) % 4 != 0); /* no pad to non last frame */
  149. fr_eof(fp) = FC_EOF_N;
  150. }
  151. fh->fh_r_ctl = r_ctl;
  152. memcpy(fh->fh_d_id, in_fh->fh_s_id, sizeof(fh->fh_d_id));
  153. memcpy(fh->fh_s_id, in_fh->fh_d_id, sizeof(fh->fh_s_id));
  154. fh->fh_type = in_fh->fh_type;
  155. hton24(fh->fh_f_ctl, f_ctl);
  156. fh->fh_ox_id = in_fh->fh_ox_id;
  157. fh->fh_rx_id = in_fh->fh_rx_id;
  158. fh->fh_cs_ctl = 0;
  159. fh->fh_df_ctl = 0;
  160. fh->fh_parm_offset = htonl(parm_offset);
  161. sp = fr_seq(in_fp);
  162. if (sp) {
  163. fr_seq(fp) = sp;
  164. fh->fh_seq_id = sp->id;
  165. seq_cnt = sp->cnt;
  166. } else {
  167. fh->fh_seq_id = 0;
  168. }
  169. fh->fh_seq_cnt = ntohs(seq_cnt);
  170. fr_sof(fp) = seq_cnt ? FC_SOF_N3 : FC_SOF_I3;
  171. fr_encaps(fp) = fr_encaps(in_fp);
  172. }
  173. EXPORT_SYMBOL(fc_fill_hdr);
  174. /**
  175. * fc_fill_reply_hdr() - fill FC reply header fields based on request
  176. * @fp: reply frame containing header to be filled in
  177. * @in_fp: request frame containing header to use in filling in reply
  178. * @r_ctl: R_CTL value for reply
  179. * @parm_offset: parameter / offset value
  180. */
  181. void fc_fill_reply_hdr(struct fc_frame *fp, const struct fc_frame *in_fp,
  182. enum fc_rctl r_ctl, u32 parm_offset)
  183. {
  184. struct fc_seq *sp;
  185. sp = fr_seq(in_fp);
  186. if (sp)
  187. fr_seq(fp) = fr_dev(in_fp)->tt.seq_start_next(sp);
  188. fc_fill_hdr(fp, in_fp, r_ctl, FC_FCTL_RESP, 0, parm_offset);
  189. }
  190. EXPORT_SYMBOL(fc_fill_reply_hdr);