tfc_sess.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * Copyright (c) 2010 Cisco Systems, Inc.
  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. /* XXX TBD some includes may be extraneous */
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/utsname.h>
  21. #include <linux/init.h>
  22. #include <linux/slab.h>
  23. #include <linux/kthread.h>
  24. #include <linux/types.h>
  25. #include <linux/string.h>
  26. #include <linux/configfs.h>
  27. #include <linux/ctype.h>
  28. #include <linux/hash.h>
  29. #include <linux/rcupdate.h>
  30. #include <linux/rculist.h>
  31. #include <linux/kref.h>
  32. #include <asm/unaligned.h>
  33. #include <scsi/libfc.h>
  34. #include <target/target_core_base.h>
  35. #include <target/target_core_fabric.h>
  36. #include <target/configfs_macros.h>
  37. #include "tcm_fc.h"
  38. static void ft_sess_delete_all(struct ft_tport *);
  39. /*
  40. * Lookup or allocate target local port.
  41. * Caller holds ft_lport_lock.
  42. */
  43. static struct ft_tport *ft_tport_get(struct fc_lport *lport)
  44. {
  45. struct ft_tpg *tpg;
  46. struct ft_tport *tport;
  47. int i;
  48. tport = rcu_dereference_protected(lport->prov[FC_TYPE_FCP],
  49. lockdep_is_held(&ft_lport_lock));
  50. if (tport && tport->tpg)
  51. return tport;
  52. tpg = ft_lport_find_tpg(lport);
  53. if (!tpg)
  54. return NULL;
  55. if (tport) {
  56. tport->tpg = tpg;
  57. tpg->tport = tport;
  58. return tport;
  59. }
  60. tport = kzalloc(sizeof(*tport), GFP_KERNEL);
  61. if (!tport)
  62. return NULL;
  63. tport->lport = lport;
  64. tport->tpg = tpg;
  65. tpg->tport = tport;
  66. for (i = 0; i < FT_SESS_HASH_SIZE; i++)
  67. INIT_HLIST_HEAD(&tport->hash[i]);
  68. rcu_assign_pointer(lport->prov[FC_TYPE_FCP], tport);
  69. return tport;
  70. }
  71. /*
  72. * Delete a target local port.
  73. * Caller holds ft_lport_lock.
  74. */
  75. static void ft_tport_delete(struct ft_tport *tport)
  76. {
  77. struct fc_lport *lport;
  78. struct ft_tpg *tpg;
  79. ft_sess_delete_all(tport);
  80. lport = tport->lport;
  81. BUG_ON(tport != lport->prov[FC_TYPE_FCP]);
  82. RCU_INIT_POINTER(lport->prov[FC_TYPE_FCP], NULL);
  83. tpg = tport->tpg;
  84. if (tpg) {
  85. tpg->tport = NULL;
  86. tport->tpg = NULL;
  87. }
  88. kfree_rcu(tport, rcu);
  89. }
  90. /*
  91. * Add local port.
  92. * Called thru fc_lport_iterate().
  93. */
  94. void ft_lport_add(struct fc_lport *lport, void *arg)
  95. {
  96. mutex_lock(&ft_lport_lock);
  97. ft_tport_get(lport);
  98. mutex_unlock(&ft_lport_lock);
  99. }
  100. /*
  101. * Delete local port.
  102. * Called thru fc_lport_iterate().
  103. */
  104. void ft_lport_del(struct fc_lport *lport, void *arg)
  105. {
  106. struct ft_tport *tport;
  107. mutex_lock(&ft_lport_lock);
  108. tport = lport->prov[FC_TYPE_FCP];
  109. if (tport)
  110. ft_tport_delete(tport);
  111. mutex_unlock(&ft_lport_lock);
  112. }
  113. /*
  114. * Notification of local port change from libfc.
  115. * Create or delete local port and associated tport.
  116. */
  117. int ft_lport_notify(struct notifier_block *nb, unsigned long event, void *arg)
  118. {
  119. struct fc_lport *lport = arg;
  120. switch (event) {
  121. case FC_LPORT_EV_ADD:
  122. ft_lport_add(lport, NULL);
  123. break;
  124. case FC_LPORT_EV_DEL:
  125. ft_lport_del(lport, NULL);
  126. break;
  127. }
  128. return NOTIFY_DONE;
  129. }
  130. /*
  131. * Hash function for FC_IDs.
  132. */
  133. static u32 ft_sess_hash(u32 port_id)
  134. {
  135. return hash_32(port_id, FT_SESS_HASH_BITS);
  136. }
  137. /*
  138. * Find session in local port.
  139. * Sessions and hash lists are RCU-protected.
  140. * A reference is taken which must be eventually freed.
  141. */
  142. static struct ft_sess *ft_sess_get(struct fc_lport *lport, u32 port_id)
  143. {
  144. struct ft_tport *tport;
  145. struct hlist_head *head;
  146. struct ft_sess *sess;
  147. rcu_read_lock();
  148. tport = rcu_dereference(lport->prov[FC_TYPE_FCP]);
  149. if (!tport)
  150. goto out;
  151. head = &tport->hash[ft_sess_hash(port_id)];
  152. hlist_for_each_entry_rcu(sess, head, hash) {
  153. if (sess->port_id == port_id) {
  154. kref_get(&sess->kref);
  155. rcu_read_unlock();
  156. pr_debug("port_id %x found %p\n", port_id, sess);
  157. return sess;
  158. }
  159. }
  160. out:
  161. rcu_read_unlock();
  162. pr_debug("port_id %x not found\n", port_id);
  163. return NULL;
  164. }
  165. /*
  166. * Allocate session and enter it in the hash for the local port.
  167. * Caller holds ft_lport_lock.
  168. */
  169. static struct ft_sess *ft_sess_create(struct ft_tport *tport, u32 port_id,
  170. struct ft_node_acl *acl)
  171. {
  172. struct ft_sess *sess;
  173. struct hlist_head *head;
  174. head = &tport->hash[ft_sess_hash(port_id)];
  175. hlist_for_each_entry_rcu(sess, head, hash)
  176. if (sess->port_id == port_id)
  177. return sess;
  178. sess = kzalloc(sizeof(*sess), GFP_KERNEL);
  179. if (!sess)
  180. return NULL;
  181. sess->se_sess = transport_init_session_tags(TCM_FC_DEFAULT_TAGS,
  182. sizeof(struct ft_cmd),
  183. TARGET_PROT_NORMAL);
  184. if (IS_ERR(sess->se_sess)) {
  185. kfree(sess);
  186. return NULL;
  187. }
  188. sess->se_sess->se_node_acl = &acl->se_node_acl;
  189. sess->tport = tport;
  190. sess->port_id = port_id;
  191. kref_init(&sess->kref); /* ref for table entry */
  192. hlist_add_head_rcu(&sess->hash, head);
  193. tport->sess_count++;
  194. pr_debug("port_id %x sess %p\n", port_id, sess);
  195. transport_register_session(&tport->tpg->se_tpg, &acl->se_node_acl,
  196. sess->se_sess, sess);
  197. return sess;
  198. }
  199. /*
  200. * Unhash the session.
  201. * Caller holds ft_lport_lock.
  202. */
  203. static void ft_sess_unhash(struct ft_sess *sess)
  204. {
  205. struct ft_tport *tport = sess->tport;
  206. hlist_del_rcu(&sess->hash);
  207. BUG_ON(!tport->sess_count);
  208. tport->sess_count--;
  209. sess->port_id = -1;
  210. sess->params = 0;
  211. }
  212. /*
  213. * Delete session from hash.
  214. * Caller holds ft_lport_lock.
  215. */
  216. static struct ft_sess *ft_sess_delete(struct ft_tport *tport, u32 port_id)
  217. {
  218. struct hlist_head *head;
  219. struct ft_sess *sess;
  220. head = &tport->hash[ft_sess_hash(port_id)];
  221. hlist_for_each_entry_rcu(sess, head, hash) {
  222. if (sess->port_id == port_id) {
  223. ft_sess_unhash(sess);
  224. return sess;
  225. }
  226. }
  227. return NULL;
  228. }
  229. /*
  230. * Delete all sessions from tport.
  231. * Caller holds ft_lport_lock.
  232. */
  233. static void ft_sess_delete_all(struct ft_tport *tport)
  234. {
  235. struct hlist_head *head;
  236. struct ft_sess *sess;
  237. for (head = tport->hash;
  238. head < &tport->hash[FT_SESS_HASH_SIZE]; head++) {
  239. hlist_for_each_entry_rcu(sess, head, hash) {
  240. ft_sess_unhash(sess);
  241. transport_deregister_session_configfs(sess->se_sess);
  242. ft_sess_put(sess); /* release from table */
  243. }
  244. }
  245. }
  246. /*
  247. * TCM ops for sessions.
  248. */
  249. /*
  250. * Determine whether session is allowed to be shutdown in the current context.
  251. * Returns non-zero if the session should be shutdown.
  252. */
  253. int ft_sess_shutdown(struct se_session *se_sess)
  254. {
  255. struct ft_sess *sess = se_sess->fabric_sess_ptr;
  256. pr_debug("port_id %x\n", sess->port_id);
  257. return 1;
  258. }
  259. /*
  260. * Remove session and send PRLO.
  261. * This is called when the ACL is being deleted or queue depth is changing.
  262. */
  263. void ft_sess_close(struct se_session *se_sess)
  264. {
  265. struct ft_sess *sess = se_sess->fabric_sess_ptr;
  266. u32 port_id;
  267. mutex_lock(&ft_lport_lock);
  268. port_id = sess->port_id;
  269. if (port_id == -1) {
  270. mutex_unlock(&ft_lport_lock);
  271. return;
  272. }
  273. pr_debug("port_id %x\n", port_id);
  274. ft_sess_unhash(sess);
  275. mutex_unlock(&ft_lport_lock);
  276. transport_deregister_session_configfs(se_sess);
  277. ft_sess_put(sess);
  278. /* XXX Send LOGO or PRLO */
  279. synchronize_rcu(); /* let transport deregister happen */
  280. }
  281. u32 ft_sess_get_index(struct se_session *se_sess)
  282. {
  283. struct ft_sess *sess = se_sess->fabric_sess_ptr;
  284. return sess->port_id; /* XXX TBD probably not what is needed */
  285. }
  286. u32 ft_sess_get_port_name(struct se_session *se_sess,
  287. unsigned char *buf, u32 len)
  288. {
  289. struct ft_sess *sess = se_sess->fabric_sess_ptr;
  290. return ft_format_wwn(buf, len, sess->port_name);
  291. }
  292. /*
  293. * libfc ops involving sessions.
  294. */
  295. static int ft_prli_locked(struct fc_rport_priv *rdata, u32 spp_len,
  296. const struct fc_els_spp *rspp, struct fc_els_spp *spp)
  297. {
  298. struct ft_tport *tport;
  299. struct ft_sess *sess;
  300. struct ft_node_acl *acl;
  301. u32 fcp_parm;
  302. tport = ft_tport_get(rdata->local_port);
  303. if (!tport)
  304. goto not_target; /* not a target for this local port */
  305. acl = ft_acl_get(tport->tpg, rdata);
  306. if (!acl)
  307. goto not_target; /* no target for this remote */
  308. if (!rspp)
  309. goto fill;
  310. if (rspp->spp_flags & (FC_SPP_OPA_VAL | FC_SPP_RPA_VAL))
  311. return FC_SPP_RESP_NO_PA;
  312. /*
  313. * If both target and initiator bits are off, the SPP is invalid.
  314. */
  315. fcp_parm = ntohl(rspp->spp_params);
  316. if (!(fcp_parm & (FCP_SPPF_INIT_FCN | FCP_SPPF_TARG_FCN)))
  317. return FC_SPP_RESP_INVL;
  318. /*
  319. * Create session (image pair) only if requested by
  320. * EST_IMG_PAIR flag and if the requestor is an initiator.
  321. */
  322. if (rspp->spp_flags & FC_SPP_EST_IMG_PAIR) {
  323. spp->spp_flags |= FC_SPP_EST_IMG_PAIR;
  324. if (!(fcp_parm & FCP_SPPF_INIT_FCN))
  325. return FC_SPP_RESP_CONF;
  326. sess = ft_sess_create(tport, rdata->ids.port_id, acl);
  327. if (!sess)
  328. return FC_SPP_RESP_RES;
  329. if (!sess->params)
  330. rdata->prli_count++;
  331. sess->params = fcp_parm;
  332. sess->port_name = rdata->ids.port_name;
  333. sess->max_frame = rdata->maxframe_size;
  334. /* XXX TBD - clearing actions. unit attn, see 4.10 */
  335. }
  336. /*
  337. * OR in our service parameters with other provider (initiator), if any.
  338. */
  339. fill:
  340. fcp_parm = ntohl(spp->spp_params);
  341. fcp_parm &= ~FCP_SPPF_RETRY;
  342. spp->spp_params = htonl(fcp_parm | FCP_SPPF_TARG_FCN);
  343. return FC_SPP_RESP_ACK;
  344. not_target:
  345. fcp_parm = ntohl(spp->spp_params);
  346. fcp_parm &= ~FCP_SPPF_TARG_FCN;
  347. spp->spp_params = htonl(fcp_parm);
  348. return 0;
  349. }
  350. /**
  351. * tcm_fcp_prli() - Handle incoming or outgoing PRLI for the FCP target
  352. * @rdata: remote port private
  353. * @spp_len: service parameter page length
  354. * @rspp: received service parameter page (NULL for outgoing PRLI)
  355. * @spp: response service parameter page
  356. *
  357. * Returns spp response code.
  358. */
  359. static int ft_prli(struct fc_rport_priv *rdata, u32 spp_len,
  360. const struct fc_els_spp *rspp, struct fc_els_spp *spp)
  361. {
  362. int ret;
  363. mutex_lock(&ft_lport_lock);
  364. ret = ft_prli_locked(rdata, spp_len, rspp, spp);
  365. mutex_unlock(&ft_lport_lock);
  366. pr_debug("port_id %x flags %x ret %x\n",
  367. rdata->ids.port_id, rspp ? rspp->spp_flags : 0, ret);
  368. return ret;
  369. }
  370. static void ft_sess_free(struct kref *kref)
  371. {
  372. struct ft_sess *sess = container_of(kref, struct ft_sess, kref);
  373. transport_deregister_session(sess->se_sess);
  374. kfree_rcu(sess, rcu);
  375. }
  376. void ft_sess_put(struct ft_sess *sess)
  377. {
  378. int sess_held = atomic_read(&sess->kref.refcount);
  379. BUG_ON(!sess_held);
  380. kref_put(&sess->kref, ft_sess_free);
  381. }
  382. static void ft_prlo(struct fc_rport_priv *rdata)
  383. {
  384. struct ft_sess *sess;
  385. struct ft_tport *tport;
  386. mutex_lock(&ft_lport_lock);
  387. tport = rcu_dereference_protected(rdata->local_port->prov[FC_TYPE_FCP],
  388. lockdep_is_held(&ft_lport_lock));
  389. if (!tport) {
  390. mutex_unlock(&ft_lport_lock);
  391. return;
  392. }
  393. sess = ft_sess_delete(tport, rdata->ids.port_id);
  394. if (!sess) {
  395. mutex_unlock(&ft_lport_lock);
  396. return;
  397. }
  398. mutex_unlock(&ft_lport_lock);
  399. transport_deregister_session_configfs(sess->se_sess);
  400. ft_sess_put(sess); /* release from table */
  401. rdata->prli_count--;
  402. /* XXX TBD - clearing actions. unit attn, see 4.10 */
  403. }
  404. /*
  405. * Handle incoming FCP request.
  406. * Caller has verified that the frame is type FCP.
  407. */
  408. static void ft_recv(struct fc_lport *lport, struct fc_frame *fp)
  409. {
  410. struct ft_sess *sess;
  411. u32 sid = fc_frame_sid(fp);
  412. pr_debug("sid %x\n", sid);
  413. sess = ft_sess_get(lport, sid);
  414. if (!sess) {
  415. pr_debug("sid %x sess lookup failed\n", sid);
  416. /* TBD XXX - if FCP_CMND, send PRLO */
  417. fc_frame_free(fp);
  418. return;
  419. }
  420. ft_recv_req(sess, fp); /* must do ft_sess_put() */
  421. }
  422. /*
  423. * Provider ops for libfc.
  424. */
  425. struct fc4_prov ft_prov = {
  426. .prli = ft_prli,
  427. .prlo = ft_prlo,
  428. .recv = ft_recv,
  429. .module = THIS_MODULE,
  430. };