tfc_sess.c 12 KB

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