fc_libfc.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright(c) 2009 Intel Corporation. All rights reserved.
  4. *
  5. * Maintained at www.Open-FCoE.org
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/types.h>
  9. #include <linux/scatterlist.h>
  10. #include <linux/crc32.h>
  11. #include <linux/module.h>
  12. #include <scsi/libfc.h>
  13. #include "fc_encode.h"
  14. #include "fc_libfc.h"
  15. MODULE_AUTHOR("Open-FCoE.org");
  16. MODULE_DESCRIPTION("libfc");
  17. MODULE_LICENSE("GPL v2");
  18. unsigned int fc_debug_logging;
  19. module_param_named(debug_logging, fc_debug_logging, int, S_IRUGO|S_IWUSR);
  20. MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
  21. DEFINE_MUTEX(fc_prov_mutex);
  22. static LIST_HEAD(fc_local_ports);
  23. struct blocking_notifier_head fc_lport_notifier_head =
  24. BLOCKING_NOTIFIER_INIT(fc_lport_notifier_head);
  25. EXPORT_SYMBOL(fc_lport_notifier_head);
  26. /*
  27. * Providers which primarily send requests and PRLIs.
  28. */
  29. struct fc4_prov *fc_active_prov[FC_FC4_PROV_SIZE] = {
  30. [0] = &fc_rport_t0_prov,
  31. [FC_TYPE_FCP] = &fc_rport_fcp_init,
  32. };
  33. /*
  34. * Providers which receive requests.
  35. */
  36. struct fc4_prov *fc_passive_prov[FC_FC4_PROV_SIZE] = {
  37. [FC_TYPE_ELS] = &fc_lport_els_prov,
  38. };
  39. /**
  40. * libfc_init() - Initialize libfc.ko
  41. */
  42. static int __init libfc_init(void)
  43. {
  44. int rc = 0;
  45. rc = fc_setup_fcp();
  46. if (rc)
  47. return rc;
  48. rc = fc_setup_exch_mgr();
  49. if (rc)
  50. goto destroy_pkt_cache;
  51. rc = fc_setup_rport();
  52. if (rc)
  53. goto destroy_em;
  54. return rc;
  55. destroy_em:
  56. fc_destroy_exch_mgr();
  57. destroy_pkt_cache:
  58. fc_destroy_fcp();
  59. return rc;
  60. }
  61. module_init(libfc_init);
  62. /**
  63. * libfc_exit() - Tear down libfc.ko
  64. */
  65. static void __exit libfc_exit(void)
  66. {
  67. fc_destroy_fcp();
  68. fc_destroy_exch_mgr();
  69. fc_destroy_rport();
  70. }
  71. module_exit(libfc_exit);
  72. /**
  73. * fc_copy_buffer_to_sglist() - This routine copies the data of a buffer
  74. * into a scatter-gather list (SG list).
  75. *
  76. * @buf: pointer to the data buffer.
  77. * @len: the byte-length of the data buffer.
  78. * @sg: pointer to the pointer of the SG list.
  79. * @nents: pointer to the remaining number of entries in the SG list.
  80. * @offset: pointer to the current offset in the SG list.
  81. * @crc: pointer to the 32-bit crc value.
  82. * If crc is NULL, CRC is not calculated.
  83. */
  84. u32 fc_copy_buffer_to_sglist(void *buf, size_t len,
  85. struct scatterlist *sg,
  86. u32 *nents, size_t *offset,
  87. u32 *crc)
  88. {
  89. size_t remaining = len;
  90. u32 copy_len = 0;
  91. while (remaining > 0 && sg) {
  92. size_t off, sg_bytes;
  93. void *page_addr;
  94. if (*offset >= sg->length) {
  95. /*
  96. * Check for end and drop resources
  97. * from the last iteration.
  98. */
  99. if (!(*nents))
  100. break;
  101. --(*nents);
  102. *offset -= sg->length;
  103. sg = sg_next(sg);
  104. continue;
  105. }
  106. sg_bytes = min(remaining, sg->length - *offset);
  107. /*
  108. * The scatterlist item may be bigger than PAGE_SIZE,
  109. * but we are limited to mapping PAGE_SIZE at a time.
  110. */
  111. off = *offset + sg->offset;
  112. sg_bytes = min(sg_bytes,
  113. (size_t)(PAGE_SIZE - (off & ~PAGE_MASK)));
  114. page_addr = kmap_atomic(sg_page(sg) + (off >> PAGE_SHIFT));
  115. if (crc)
  116. *crc = crc32(*crc, buf, sg_bytes);
  117. memcpy((char *)page_addr + (off & ~PAGE_MASK), buf, sg_bytes);
  118. kunmap_atomic(page_addr);
  119. buf += sg_bytes;
  120. *offset += sg_bytes;
  121. remaining -= sg_bytes;
  122. copy_len += sg_bytes;
  123. }
  124. return copy_len;
  125. }
  126. /**
  127. * fc_fill_hdr() - fill FC header fields based on request
  128. * @fp: reply frame containing header to be filled in
  129. * @in_fp: request frame containing header to use in filling in reply
  130. * @r_ctl: R_CTL value for header
  131. * @f_ctl: F_CTL value for header, with 0 pad
  132. * @seq_cnt: sequence count for the header, ignored if frame has a sequence
  133. * @parm_offset: parameter / offset value
  134. */
  135. void fc_fill_hdr(struct fc_frame *fp, const struct fc_frame *in_fp,
  136. enum fc_rctl r_ctl, u32 f_ctl, u16 seq_cnt, u32 parm_offset)
  137. {
  138. struct fc_frame_header *fh;
  139. struct fc_frame_header *in_fh;
  140. struct fc_seq *sp;
  141. u32 fill;
  142. fh = __fc_frame_header_get(fp);
  143. in_fh = __fc_frame_header_get(in_fp);
  144. if (f_ctl & FC_FC_END_SEQ) {
  145. fill = -fr_len(fp) & 3;
  146. if (fill) {
  147. /* TODO, this may be a problem with fragmented skb */
  148. skb_put_zero(fp_skb(fp), fill);
  149. f_ctl |= fill;
  150. }
  151. fr_eof(fp) = FC_EOF_T;
  152. } else {
  153. WARN_ON(fr_len(fp) % 4 != 0); /* no pad to non last frame */
  154. fr_eof(fp) = FC_EOF_N;
  155. }
  156. fh->fh_r_ctl = r_ctl;
  157. memcpy(fh->fh_d_id, in_fh->fh_s_id, sizeof(fh->fh_d_id));
  158. memcpy(fh->fh_s_id, in_fh->fh_d_id, sizeof(fh->fh_s_id));
  159. fh->fh_type = in_fh->fh_type;
  160. hton24(fh->fh_f_ctl, f_ctl);
  161. fh->fh_ox_id = in_fh->fh_ox_id;
  162. fh->fh_rx_id = in_fh->fh_rx_id;
  163. fh->fh_cs_ctl = 0;
  164. fh->fh_df_ctl = 0;
  165. fh->fh_parm_offset = htonl(parm_offset);
  166. sp = fr_seq(in_fp);
  167. if (sp) {
  168. fr_seq(fp) = sp;
  169. fh->fh_seq_id = sp->id;
  170. seq_cnt = sp->cnt;
  171. } else {
  172. fh->fh_seq_id = 0;
  173. }
  174. fh->fh_seq_cnt = ntohs(seq_cnt);
  175. fr_sof(fp) = seq_cnt ? FC_SOF_N3 : FC_SOF_I3;
  176. fr_encaps(fp) = fr_encaps(in_fp);
  177. }
  178. EXPORT_SYMBOL(fc_fill_hdr);
  179. /**
  180. * fc_fill_reply_hdr() - fill FC reply header fields based on request
  181. * @fp: reply frame containing header to be filled in
  182. * @in_fp: request frame containing header to use in filling in reply
  183. * @r_ctl: R_CTL value for reply
  184. * @parm_offset: parameter / offset value
  185. */
  186. void fc_fill_reply_hdr(struct fc_frame *fp, const struct fc_frame *in_fp,
  187. enum fc_rctl r_ctl, u32 parm_offset)
  188. {
  189. struct fc_seq *sp;
  190. sp = fr_seq(in_fp);
  191. if (sp)
  192. fr_seq(fp) = fc_seq_start_next(sp);
  193. fc_fill_hdr(fp, in_fp, r_ctl, FC_FCTL_RESP, 0, parm_offset);
  194. }
  195. EXPORT_SYMBOL(fc_fill_reply_hdr);
  196. /**
  197. * fc_fc4_conf_lport_params() - Modify "service_params" of specified lport
  198. * if there is service provider (target provider) registered with libfc
  199. * for specified "fc_ft_type"
  200. * @lport: Local port which service_params needs to be modified
  201. * @type: FC-4 type, such as FC_TYPE_FCP
  202. */
  203. void fc_fc4_conf_lport_params(struct fc_lport *lport, enum fc_fh_type type)
  204. {
  205. struct fc4_prov *prov_entry;
  206. BUG_ON(type >= FC_FC4_PROV_SIZE);
  207. BUG_ON(!lport);
  208. prov_entry = fc_passive_prov[type];
  209. if (type == FC_TYPE_FCP) {
  210. if (prov_entry && prov_entry->recv)
  211. lport->service_params |= FCP_SPPF_TARG_FCN;
  212. }
  213. }
  214. void fc_lport_iterate(void (*notify)(struct fc_lport *, void *), void *arg)
  215. {
  216. struct fc_lport *lport;
  217. mutex_lock(&fc_prov_mutex);
  218. list_for_each_entry(lport, &fc_local_ports, lport_list)
  219. notify(lport, arg);
  220. mutex_unlock(&fc_prov_mutex);
  221. }
  222. EXPORT_SYMBOL(fc_lport_iterate);
  223. /**
  224. * fc_fc4_register_provider() - register FC-4 upper-level provider.
  225. * @type: FC-4 type, such as FC_TYPE_FCP
  226. * @prov: structure describing provider including ops vector.
  227. *
  228. * Returns 0 on success, negative error otherwise.
  229. */
  230. int fc_fc4_register_provider(enum fc_fh_type type, struct fc4_prov *prov)
  231. {
  232. struct fc4_prov **prov_entry;
  233. int ret = 0;
  234. if (type >= FC_FC4_PROV_SIZE)
  235. return -EINVAL;
  236. mutex_lock(&fc_prov_mutex);
  237. prov_entry = (prov->recv ? fc_passive_prov : fc_active_prov) + type;
  238. if (*prov_entry)
  239. ret = -EBUSY;
  240. else
  241. *prov_entry = prov;
  242. mutex_unlock(&fc_prov_mutex);
  243. return ret;
  244. }
  245. EXPORT_SYMBOL(fc_fc4_register_provider);
  246. /**
  247. * fc_fc4_deregister_provider() - deregister FC-4 upper-level provider.
  248. * @type: FC-4 type, such as FC_TYPE_FCP
  249. * @prov: structure describing provider including ops vector.
  250. */
  251. void fc_fc4_deregister_provider(enum fc_fh_type type, struct fc4_prov *prov)
  252. {
  253. BUG_ON(type >= FC_FC4_PROV_SIZE);
  254. mutex_lock(&fc_prov_mutex);
  255. if (prov->recv)
  256. RCU_INIT_POINTER(fc_passive_prov[type], NULL);
  257. else
  258. RCU_INIT_POINTER(fc_active_prov[type], NULL);
  259. mutex_unlock(&fc_prov_mutex);
  260. synchronize_rcu();
  261. }
  262. EXPORT_SYMBOL(fc_fc4_deregister_provider);
  263. /**
  264. * fc_fc4_add_lport() - add new local port to list and run notifiers.
  265. * @lport: The new local port.
  266. */
  267. void fc_fc4_add_lport(struct fc_lport *lport)
  268. {
  269. mutex_lock(&fc_prov_mutex);
  270. list_add_tail(&lport->lport_list, &fc_local_ports);
  271. blocking_notifier_call_chain(&fc_lport_notifier_head,
  272. FC_LPORT_EV_ADD, lport);
  273. mutex_unlock(&fc_prov_mutex);
  274. }
  275. /**
  276. * fc_fc4_del_lport() - remove local port from list and run notifiers.
  277. * @lport: The new local port.
  278. */
  279. void fc_fc4_del_lport(struct fc_lport *lport)
  280. {
  281. mutex_lock(&fc_prov_mutex);
  282. list_del(&lport->lport_list);
  283. blocking_notifier_call_chain(&fc_lport_notifier_head,
  284. FC_LPORT_EV_DEL, lport);
  285. mutex_unlock(&fc_prov_mutex);
  286. }