tfc_sess.c 12 KB

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