tfc_sess.c 12 KB

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