fc_libfc.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. DEFINE_MUTEX(fc_prov_mutex);
  33. /*
  34. * Providers which primarily send requests and PRLIs.
  35. */
  36. struct fc4_prov *fc_active_prov[FC_FC4_PROV_SIZE] = {
  37. [0] = &fc_rport_t0_prov,
  38. [FC_TYPE_FCP] = &fc_rport_fcp_init,
  39. };
  40. /*
  41. * Providers which receive requests.
  42. */
  43. struct fc4_prov *fc_passive_prov[FC_FC4_PROV_SIZE] = {
  44. [FC_TYPE_ELS] = &fc_lport_els_prov,
  45. };
  46. /**
  47. * libfc_init() - Initialize libfc.ko
  48. */
  49. static int __init libfc_init(void)
  50. {
  51. int rc = 0;
  52. rc = fc_setup_fcp();
  53. if (rc)
  54. return rc;
  55. rc = fc_setup_exch_mgr();
  56. if (rc)
  57. goto destroy_pkt_cache;
  58. rc = fc_setup_rport();
  59. if (rc)
  60. goto destroy_em;
  61. return rc;
  62. destroy_em:
  63. fc_destroy_exch_mgr();
  64. destroy_pkt_cache:
  65. fc_destroy_fcp();
  66. return rc;
  67. }
  68. module_init(libfc_init);
  69. /**
  70. * libfc_exit() - Tear down libfc.ko
  71. */
  72. static void __exit libfc_exit(void)
  73. {
  74. fc_destroy_fcp();
  75. fc_destroy_exch_mgr();
  76. fc_destroy_rport();
  77. }
  78. module_exit(libfc_exit);
  79. /**
  80. * fc_copy_buffer_to_sglist() - This routine copies the data of a buffer
  81. * into a scatter-gather list (SG list).
  82. *
  83. * @buf: pointer to the data buffer.
  84. * @len: the byte-length of the data buffer.
  85. * @sg: pointer to the pointer of the SG list.
  86. * @nents: pointer to the remaining number of entries in the SG list.
  87. * @offset: pointer to the current offset in the SG list.
  88. * @km_type: dedicated page table slot type for kmap_atomic.
  89. * @crc: pointer to the 32-bit crc value.
  90. * If crc is NULL, CRC is not calculated.
  91. */
  92. u32 fc_copy_buffer_to_sglist(void *buf, size_t len,
  93. struct scatterlist *sg,
  94. u32 *nents, size_t *offset,
  95. enum km_type km_type, u32 *crc)
  96. {
  97. size_t remaining = len;
  98. u32 copy_len = 0;
  99. while (remaining > 0 && sg) {
  100. size_t off, sg_bytes;
  101. void *page_addr;
  102. if (*offset >= sg->length) {
  103. /*
  104. * Check for end and drop resources
  105. * from the last iteration.
  106. */
  107. if (!(*nents))
  108. break;
  109. --(*nents);
  110. *offset -= sg->length;
  111. sg = sg_next(sg);
  112. continue;
  113. }
  114. sg_bytes = min(remaining, sg->length - *offset);
  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. }
  135. /**
  136. * fc_fill_hdr() - fill FC header fields based on request
  137. * @fp: reply frame containing header to be filled in
  138. * @in_fp: request frame containing header to use in filling in reply
  139. * @r_ctl: R_CTL value for header
  140. * @f_ctl: F_CTL value for header, with 0 pad
  141. * @seq_cnt: sequence count for the header, ignored if frame has a sequence
  142. * @parm_offset: parameter / offset value
  143. */
  144. void fc_fill_hdr(struct fc_frame *fp, const struct fc_frame *in_fp,
  145. enum fc_rctl r_ctl, u32 f_ctl, u16 seq_cnt, u32 parm_offset)
  146. {
  147. struct fc_frame_header *fh;
  148. struct fc_frame_header *in_fh;
  149. struct fc_seq *sp;
  150. u32 fill;
  151. fh = __fc_frame_header_get(fp);
  152. in_fh = __fc_frame_header_get(in_fp);
  153. if (f_ctl & FC_FC_END_SEQ) {
  154. fill = -fr_len(fp) & 3;
  155. if (fill) {
  156. /* TODO, this may be a problem with fragmented skb */
  157. memset(skb_put(fp_skb(fp), fill), 0, fill);
  158. f_ctl |= fill;
  159. }
  160. fr_eof(fp) = FC_EOF_T;
  161. } else {
  162. WARN_ON(fr_len(fp) % 4 != 0); /* no pad to non last frame */
  163. fr_eof(fp) = FC_EOF_N;
  164. }
  165. fh->fh_r_ctl = r_ctl;
  166. memcpy(fh->fh_d_id, in_fh->fh_s_id, sizeof(fh->fh_d_id));
  167. memcpy(fh->fh_s_id, in_fh->fh_d_id, sizeof(fh->fh_s_id));
  168. fh->fh_type = in_fh->fh_type;
  169. hton24(fh->fh_f_ctl, f_ctl);
  170. fh->fh_ox_id = in_fh->fh_ox_id;
  171. fh->fh_rx_id = in_fh->fh_rx_id;
  172. fh->fh_cs_ctl = 0;
  173. fh->fh_df_ctl = 0;
  174. fh->fh_parm_offset = htonl(parm_offset);
  175. sp = fr_seq(in_fp);
  176. if (sp) {
  177. fr_seq(fp) = sp;
  178. fh->fh_seq_id = sp->id;
  179. seq_cnt = sp->cnt;
  180. } else {
  181. fh->fh_seq_id = 0;
  182. }
  183. fh->fh_seq_cnt = ntohs(seq_cnt);
  184. fr_sof(fp) = seq_cnt ? FC_SOF_N3 : FC_SOF_I3;
  185. fr_encaps(fp) = fr_encaps(in_fp);
  186. }
  187. EXPORT_SYMBOL(fc_fill_hdr);
  188. /**
  189. * fc_fill_reply_hdr() - fill FC reply header fields based on request
  190. * @fp: reply frame containing header to be filled in
  191. * @in_fp: request frame containing header to use in filling in reply
  192. * @r_ctl: R_CTL value for reply
  193. * @parm_offset: parameter / offset value
  194. */
  195. void fc_fill_reply_hdr(struct fc_frame *fp, const struct fc_frame *in_fp,
  196. enum fc_rctl r_ctl, u32 parm_offset)
  197. {
  198. struct fc_seq *sp;
  199. sp = fr_seq(in_fp);
  200. if (sp)
  201. fr_seq(fp) = fr_dev(in_fp)->tt.seq_start_next(sp);
  202. fc_fill_hdr(fp, in_fp, r_ctl, FC_FCTL_RESP, 0, parm_offset);
  203. }
  204. EXPORT_SYMBOL(fc_fill_reply_hdr);
  205. /**
  206. * fc_fc4_register_provider() - register FC-4 upper-level provider.
  207. * @type: FC-4 type, such as FC_TYPE_FCP
  208. * @prov: structure describing provider including ops vector.
  209. *
  210. * Returns 0 on success, negative error otherwise.
  211. */
  212. int fc_fc4_register_provider(enum fc_fh_type type, struct fc4_prov *prov)
  213. {
  214. struct fc4_prov **prov_entry;
  215. int ret = 0;
  216. if (type >= FC_FC4_PROV_SIZE)
  217. return -EINVAL;
  218. mutex_lock(&fc_prov_mutex);
  219. prov_entry = (prov->recv ? fc_passive_prov : fc_active_prov) + type;
  220. if (*prov_entry)
  221. ret = -EBUSY;
  222. else
  223. *prov_entry = prov;
  224. mutex_unlock(&fc_prov_mutex);
  225. return ret;
  226. }
  227. EXPORT_SYMBOL(fc_fc4_register_provider);
  228. /**
  229. * fc_fc4_deregister_provider() - deregister FC-4 upper-level provider.
  230. * @type: FC-4 type, such as FC_TYPE_FCP
  231. * @prov: structure describing provider including ops vector.
  232. */
  233. void fc_fc4_deregister_provider(enum fc_fh_type type, struct fc4_prov *prov)
  234. {
  235. BUG_ON(type >= FC_FC4_PROV_SIZE);
  236. mutex_lock(&fc_prov_mutex);
  237. if (prov->recv)
  238. rcu_assign_pointer(fc_passive_prov[type], NULL);
  239. else
  240. rcu_assign_pointer(fc_active_prov[type], NULL);
  241. mutex_unlock(&fc_prov_mutex);
  242. synchronize_rcu();
  243. }
  244. EXPORT_SYMBOL(fc_fc4_deregister_provider);