tfc_sess.c 12 KB

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