tfc_sess.c 12 KB

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