tfc_sess.c 12 KB

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