fc_libfc.c 8.8 KB

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