tfc_sess.c 12 KB

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