fcloop.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2016 Avago Technologies. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/module.h>
  7. #include <linux/parser.h>
  8. #include <uapi/scsi/fc/fc_fs.h>
  9. #include "../host/nvme.h"
  10. #include "../target/nvmet.h"
  11. #include <linux/nvme-fc-driver.h>
  12. #include <linux/nvme-fc.h>
  13. enum {
  14. NVMF_OPT_ERR = 0,
  15. NVMF_OPT_WWNN = 1 << 0,
  16. NVMF_OPT_WWPN = 1 << 1,
  17. NVMF_OPT_ROLES = 1 << 2,
  18. NVMF_OPT_FCADDR = 1 << 3,
  19. NVMF_OPT_LPWWNN = 1 << 4,
  20. NVMF_OPT_LPWWPN = 1 << 5,
  21. };
  22. struct fcloop_ctrl_options {
  23. int mask;
  24. u64 wwnn;
  25. u64 wwpn;
  26. u32 roles;
  27. u32 fcaddr;
  28. u64 lpwwnn;
  29. u64 lpwwpn;
  30. };
  31. static const match_table_t opt_tokens = {
  32. { NVMF_OPT_WWNN, "wwnn=%s" },
  33. { NVMF_OPT_WWPN, "wwpn=%s" },
  34. { NVMF_OPT_ROLES, "roles=%d" },
  35. { NVMF_OPT_FCADDR, "fcaddr=%x" },
  36. { NVMF_OPT_LPWWNN, "lpwwnn=%s" },
  37. { NVMF_OPT_LPWWPN, "lpwwpn=%s" },
  38. { NVMF_OPT_ERR, NULL }
  39. };
  40. static int fcloop_verify_addr(substring_t *s)
  41. {
  42. size_t blen = s->to - s->from + 1;
  43. if (strnlen(s->from, blen) != NVME_FC_TRADDR_HEXNAMELEN + 2 ||
  44. strncmp(s->from, "0x", 2))
  45. return -EINVAL;
  46. return 0;
  47. }
  48. static int
  49. fcloop_parse_options(struct fcloop_ctrl_options *opts,
  50. const char *buf)
  51. {
  52. substring_t args[MAX_OPT_ARGS];
  53. char *options, *o, *p;
  54. int token, ret = 0;
  55. u64 token64;
  56. options = o = kstrdup(buf, GFP_KERNEL);
  57. if (!options)
  58. return -ENOMEM;
  59. while ((p = strsep(&o, ",\n")) != NULL) {
  60. if (!*p)
  61. continue;
  62. token = match_token(p, opt_tokens, args);
  63. opts->mask |= token;
  64. switch (token) {
  65. case NVMF_OPT_WWNN:
  66. if (fcloop_verify_addr(args) ||
  67. match_u64(args, &token64)) {
  68. ret = -EINVAL;
  69. goto out_free_options;
  70. }
  71. opts->wwnn = token64;
  72. break;
  73. case NVMF_OPT_WWPN:
  74. if (fcloop_verify_addr(args) ||
  75. match_u64(args, &token64)) {
  76. ret = -EINVAL;
  77. goto out_free_options;
  78. }
  79. opts->wwpn = token64;
  80. break;
  81. case NVMF_OPT_ROLES:
  82. if (match_int(args, &token)) {
  83. ret = -EINVAL;
  84. goto out_free_options;
  85. }
  86. opts->roles = token;
  87. break;
  88. case NVMF_OPT_FCADDR:
  89. if (match_hex(args, &token)) {
  90. ret = -EINVAL;
  91. goto out_free_options;
  92. }
  93. opts->fcaddr = token;
  94. break;
  95. case NVMF_OPT_LPWWNN:
  96. if (fcloop_verify_addr(args) ||
  97. match_u64(args, &token64)) {
  98. ret = -EINVAL;
  99. goto out_free_options;
  100. }
  101. opts->lpwwnn = token64;
  102. break;
  103. case NVMF_OPT_LPWWPN:
  104. if (fcloop_verify_addr(args) ||
  105. match_u64(args, &token64)) {
  106. ret = -EINVAL;
  107. goto out_free_options;
  108. }
  109. opts->lpwwpn = token64;
  110. break;
  111. default:
  112. pr_warn("unknown parameter or missing value '%s'\n", p);
  113. ret = -EINVAL;
  114. goto out_free_options;
  115. }
  116. }
  117. out_free_options:
  118. kfree(options);
  119. return ret;
  120. }
  121. static int
  122. fcloop_parse_nm_options(struct device *dev, u64 *nname, u64 *pname,
  123. const char *buf)
  124. {
  125. substring_t args[MAX_OPT_ARGS];
  126. char *options, *o, *p;
  127. int token, ret = 0;
  128. u64 token64;
  129. *nname = -1;
  130. *pname = -1;
  131. options = o = kstrdup(buf, GFP_KERNEL);
  132. if (!options)
  133. return -ENOMEM;
  134. while ((p = strsep(&o, ",\n")) != NULL) {
  135. if (!*p)
  136. continue;
  137. token = match_token(p, opt_tokens, args);
  138. switch (token) {
  139. case NVMF_OPT_WWNN:
  140. if (fcloop_verify_addr(args) ||
  141. match_u64(args, &token64)) {
  142. ret = -EINVAL;
  143. goto out_free_options;
  144. }
  145. *nname = token64;
  146. break;
  147. case NVMF_OPT_WWPN:
  148. if (fcloop_verify_addr(args) ||
  149. match_u64(args, &token64)) {
  150. ret = -EINVAL;
  151. goto out_free_options;
  152. }
  153. *pname = token64;
  154. break;
  155. default:
  156. pr_warn("unknown parameter or missing value '%s'\n", p);
  157. ret = -EINVAL;
  158. goto out_free_options;
  159. }
  160. }
  161. out_free_options:
  162. kfree(options);
  163. if (!ret) {
  164. if (*nname == -1)
  165. return -EINVAL;
  166. if (*pname == -1)
  167. return -EINVAL;
  168. }
  169. return ret;
  170. }
  171. #define LPORT_OPTS (NVMF_OPT_WWNN | NVMF_OPT_WWPN)
  172. #define RPORT_OPTS (NVMF_OPT_WWNN | NVMF_OPT_WWPN | \
  173. NVMF_OPT_LPWWNN | NVMF_OPT_LPWWPN)
  174. #define TGTPORT_OPTS (NVMF_OPT_WWNN | NVMF_OPT_WWPN)
  175. static DEFINE_SPINLOCK(fcloop_lock);
  176. static LIST_HEAD(fcloop_lports);
  177. static LIST_HEAD(fcloop_nports);
  178. struct fcloop_lport {
  179. struct nvme_fc_local_port *localport;
  180. struct list_head lport_list;
  181. struct completion unreg_done;
  182. };
  183. struct fcloop_lport_priv {
  184. struct fcloop_lport *lport;
  185. };
  186. struct fcloop_rport {
  187. struct nvme_fc_remote_port *remoteport;
  188. struct nvmet_fc_target_port *targetport;
  189. struct fcloop_nport *nport;
  190. struct fcloop_lport *lport;
  191. spinlock_t lock;
  192. struct list_head ls_list;
  193. struct work_struct ls_work;
  194. };
  195. struct fcloop_tport {
  196. struct nvmet_fc_target_port *targetport;
  197. struct nvme_fc_remote_port *remoteport;
  198. struct fcloop_nport *nport;
  199. struct fcloop_lport *lport;
  200. spinlock_t lock;
  201. struct list_head ls_list;
  202. struct work_struct ls_work;
  203. };
  204. struct fcloop_nport {
  205. struct fcloop_rport *rport;
  206. struct fcloop_tport *tport;
  207. struct fcloop_lport *lport;
  208. struct list_head nport_list;
  209. struct kref ref;
  210. u64 node_name;
  211. u64 port_name;
  212. u32 port_role;
  213. u32 port_id;
  214. };
  215. struct fcloop_lsreq {
  216. struct nvmefc_ls_req *lsreq;
  217. struct nvmefc_ls_rsp ls_rsp;
  218. int lsdir; /* H2T or T2H */
  219. int status;
  220. struct list_head ls_list; /* fcloop_rport->ls_list */
  221. };
  222. struct fcloop_rscn {
  223. struct fcloop_tport *tport;
  224. struct work_struct work;
  225. };
  226. enum {
  227. INI_IO_START = 0,
  228. INI_IO_ACTIVE = 1,
  229. INI_IO_ABORTED = 2,
  230. INI_IO_COMPLETED = 3,
  231. };
  232. struct fcloop_fcpreq {
  233. struct fcloop_tport *tport;
  234. struct nvmefc_fcp_req *fcpreq;
  235. spinlock_t reqlock;
  236. u16 status;
  237. u32 inistate;
  238. bool active;
  239. bool aborted;
  240. struct kref ref;
  241. struct work_struct fcp_rcv_work;
  242. struct work_struct abort_rcv_work;
  243. struct work_struct tio_done_work;
  244. struct nvmefc_tgt_fcp_req tgt_fcp_req;
  245. };
  246. struct fcloop_ini_fcpreq {
  247. struct nvmefc_fcp_req *fcpreq;
  248. struct fcloop_fcpreq *tfcp_req;
  249. spinlock_t inilock;
  250. };
  251. static inline struct fcloop_lsreq *
  252. ls_rsp_to_lsreq(struct nvmefc_ls_rsp *lsrsp)
  253. {
  254. return container_of(lsrsp, struct fcloop_lsreq, ls_rsp);
  255. }
  256. static inline struct fcloop_fcpreq *
  257. tgt_fcp_req_to_fcpreq(struct nvmefc_tgt_fcp_req *tgt_fcpreq)
  258. {
  259. return container_of(tgt_fcpreq, struct fcloop_fcpreq, tgt_fcp_req);
  260. }
  261. static int
  262. fcloop_create_queue(struct nvme_fc_local_port *localport,
  263. unsigned int qidx, u16 qsize,
  264. void **handle)
  265. {
  266. *handle = localport;
  267. return 0;
  268. }
  269. static void
  270. fcloop_delete_queue(struct nvme_fc_local_port *localport,
  271. unsigned int idx, void *handle)
  272. {
  273. }
  274. static void
  275. fcloop_rport_lsrqst_work(struct work_struct *work)
  276. {
  277. struct fcloop_rport *rport =
  278. container_of(work, struct fcloop_rport, ls_work);
  279. struct fcloop_lsreq *tls_req;
  280. spin_lock(&rport->lock);
  281. for (;;) {
  282. tls_req = list_first_entry_or_null(&rport->ls_list,
  283. struct fcloop_lsreq, ls_list);
  284. if (!tls_req)
  285. break;
  286. list_del(&tls_req->ls_list);
  287. spin_unlock(&rport->lock);
  288. tls_req->lsreq->done(tls_req->lsreq, tls_req->status);
  289. /*
  290. * callee may free memory containing tls_req.
  291. * do not reference lsreq after this.
  292. */
  293. spin_lock(&rport->lock);
  294. }
  295. spin_unlock(&rport->lock);
  296. }
  297. static int
  298. fcloop_h2t_ls_req(struct nvme_fc_local_port *localport,
  299. struct nvme_fc_remote_port *remoteport,
  300. struct nvmefc_ls_req *lsreq)
  301. {
  302. struct fcloop_lsreq *tls_req = lsreq->private;
  303. struct fcloop_rport *rport = remoteport->private;
  304. int ret = 0;
  305. tls_req->lsreq = lsreq;
  306. INIT_LIST_HEAD(&tls_req->ls_list);
  307. if (!rport->targetport) {
  308. tls_req->status = -ECONNREFUSED;
  309. spin_lock(&rport->lock);
  310. list_add_tail(&rport->ls_list, &tls_req->ls_list);
  311. spin_unlock(&rport->lock);
  312. schedule_work(&rport->ls_work);
  313. return ret;
  314. }
  315. tls_req->status = 0;
  316. ret = nvmet_fc_rcv_ls_req(rport->targetport, rport,
  317. &tls_req->ls_rsp,
  318. lsreq->rqstaddr, lsreq->rqstlen);
  319. return ret;
  320. }
  321. static int
  322. fcloop_h2t_xmt_ls_rsp(struct nvmet_fc_target_port *targetport,
  323. struct nvmefc_ls_rsp *lsrsp)
  324. {
  325. struct fcloop_lsreq *tls_req = ls_rsp_to_lsreq(lsrsp);
  326. struct nvmefc_ls_req *lsreq = tls_req->lsreq;
  327. struct fcloop_tport *tport = targetport->private;
  328. struct nvme_fc_remote_port *remoteport = tport->remoteport;
  329. struct fcloop_rport *rport;
  330. memcpy(lsreq->rspaddr, lsrsp->rspbuf,
  331. ((lsreq->rsplen < lsrsp->rsplen) ?
  332. lsreq->rsplen : lsrsp->rsplen));
  333. lsrsp->done(lsrsp);
  334. if (remoteport) {
  335. rport = remoteport->private;
  336. spin_lock(&rport->lock);
  337. list_add_tail(&rport->ls_list, &tls_req->ls_list);
  338. spin_unlock(&rport->lock);
  339. schedule_work(&rport->ls_work);
  340. }
  341. return 0;
  342. }
  343. static void
  344. fcloop_tport_lsrqst_work(struct work_struct *work)
  345. {
  346. struct fcloop_tport *tport =
  347. container_of(work, struct fcloop_tport, ls_work);
  348. struct fcloop_lsreq *tls_req;
  349. spin_lock(&tport->lock);
  350. for (;;) {
  351. tls_req = list_first_entry_or_null(&tport->ls_list,
  352. struct fcloop_lsreq, ls_list);
  353. if (!tls_req)
  354. break;
  355. list_del(&tls_req->ls_list);
  356. spin_unlock(&tport->lock);
  357. tls_req->lsreq->done(tls_req->lsreq, tls_req->status);
  358. /*
  359. * callee may free memory containing tls_req.
  360. * do not reference lsreq after this.
  361. */
  362. spin_lock(&tport->lock);
  363. }
  364. spin_unlock(&tport->lock);
  365. }
  366. static int
  367. fcloop_t2h_ls_req(struct nvmet_fc_target_port *targetport, void *hosthandle,
  368. struct nvmefc_ls_req *lsreq)
  369. {
  370. struct fcloop_lsreq *tls_req = lsreq->private;
  371. struct fcloop_tport *tport = targetport->private;
  372. int ret = 0;
  373. /*
  374. * hosthandle should be the dst.rport value.
  375. * hosthandle ignored as fcloop currently is
  376. * 1:1 tgtport vs remoteport
  377. */
  378. tls_req->lsreq = lsreq;
  379. INIT_LIST_HEAD(&tls_req->ls_list);
  380. if (!tport->remoteport) {
  381. tls_req->status = -ECONNREFUSED;
  382. spin_lock(&tport->lock);
  383. list_add_tail(&tport->ls_list, &tls_req->ls_list);
  384. spin_unlock(&tport->lock);
  385. schedule_work(&tport->ls_work);
  386. return ret;
  387. }
  388. tls_req->status = 0;
  389. ret = nvme_fc_rcv_ls_req(tport->remoteport, &tls_req->ls_rsp,
  390. lsreq->rqstaddr, lsreq->rqstlen);
  391. return ret;
  392. }
  393. static int
  394. fcloop_t2h_xmt_ls_rsp(struct nvme_fc_local_port *localport,
  395. struct nvme_fc_remote_port *remoteport,
  396. struct nvmefc_ls_rsp *lsrsp)
  397. {
  398. struct fcloop_lsreq *tls_req = ls_rsp_to_lsreq(lsrsp);
  399. struct nvmefc_ls_req *lsreq = tls_req->lsreq;
  400. struct fcloop_rport *rport = remoteport->private;
  401. struct nvmet_fc_target_port *targetport = rport->targetport;
  402. struct fcloop_tport *tport;
  403. memcpy(lsreq->rspaddr, lsrsp->rspbuf,
  404. ((lsreq->rsplen < lsrsp->rsplen) ?
  405. lsreq->rsplen : lsrsp->rsplen));
  406. lsrsp->done(lsrsp);
  407. if (targetport) {
  408. tport = targetport->private;
  409. spin_lock(&tport->lock);
  410. list_add_tail(&tport->ls_list, &tls_req->ls_list);
  411. spin_unlock(&tport->lock);
  412. schedule_work(&tport->ls_work);
  413. }
  414. return 0;
  415. }
  416. static void
  417. fcloop_t2h_host_release(void *hosthandle)
  418. {
  419. /* host handle ignored for now */
  420. }
  421. /*
  422. * Simulate reception of RSCN and converting it to a initiator transport
  423. * call to rescan a remote port.
  424. */
  425. static void
  426. fcloop_tgt_rscn_work(struct work_struct *work)
  427. {
  428. struct fcloop_rscn *tgt_rscn =
  429. container_of(work, struct fcloop_rscn, work);
  430. struct fcloop_tport *tport = tgt_rscn->tport;
  431. if (tport->remoteport)
  432. nvme_fc_rescan_remoteport(tport->remoteport);
  433. kfree(tgt_rscn);
  434. }
  435. static void
  436. fcloop_tgt_discovery_evt(struct nvmet_fc_target_port *tgtport)
  437. {
  438. struct fcloop_rscn *tgt_rscn;
  439. tgt_rscn = kzalloc(sizeof(*tgt_rscn), GFP_KERNEL);
  440. if (!tgt_rscn)
  441. return;
  442. tgt_rscn->tport = tgtport->private;
  443. INIT_WORK(&tgt_rscn->work, fcloop_tgt_rscn_work);
  444. schedule_work(&tgt_rscn->work);
  445. }
  446. static void
  447. fcloop_tfcp_req_free(struct kref *ref)
  448. {
  449. struct fcloop_fcpreq *tfcp_req =
  450. container_of(ref, struct fcloop_fcpreq, ref);
  451. kfree(tfcp_req);
  452. }
  453. static void
  454. fcloop_tfcp_req_put(struct fcloop_fcpreq *tfcp_req)
  455. {
  456. kref_put(&tfcp_req->ref, fcloop_tfcp_req_free);
  457. }
  458. static int
  459. fcloop_tfcp_req_get(struct fcloop_fcpreq *tfcp_req)
  460. {
  461. return kref_get_unless_zero(&tfcp_req->ref);
  462. }
  463. static void
  464. fcloop_call_host_done(struct nvmefc_fcp_req *fcpreq,
  465. struct fcloop_fcpreq *tfcp_req, int status)
  466. {
  467. struct fcloop_ini_fcpreq *inireq = NULL;
  468. if (fcpreq) {
  469. inireq = fcpreq->private;
  470. spin_lock(&inireq->inilock);
  471. inireq->tfcp_req = NULL;
  472. spin_unlock(&inireq->inilock);
  473. fcpreq->status = status;
  474. fcpreq->done(fcpreq);
  475. }
  476. /* release original io reference on tgt struct */
  477. fcloop_tfcp_req_put(tfcp_req);
  478. }
  479. static void
  480. fcloop_fcp_recv_work(struct work_struct *work)
  481. {
  482. struct fcloop_fcpreq *tfcp_req =
  483. container_of(work, struct fcloop_fcpreq, fcp_rcv_work);
  484. struct nvmefc_fcp_req *fcpreq = tfcp_req->fcpreq;
  485. int ret = 0;
  486. bool aborted = false;
  487. spin_lock_irq(&tfcp_req->reqlock);
  488. switch (tfcp_req->inistate) {
  489. case INI_IO_START:
  490. tfcp_req->inistate = INI_IO_ACTIVE;
  491. break;
  492. case INI_IO_ABORTED:
  493. aborted = true;
  494. break;
  495. default:
  496. spin_unlock_irq(&tfcp_req->reqlock);
  497. WARN_ON(1);
  498. return;
  499. }
  500. spin_unlock_irq(&tfcp_req->reqlock);
  501. if (unlikely(aborted))
  502. ret = -ECANCELED;
  503. else
  504. ret = nvmet_fc_rcv_fcp_req(tfcp_req->tport->targetport,
  505. &tfcp_req->tgt_fcp_req,
  506. fcpreq->cmdaddr, fcpreq->cmdlen);
  507. if (ret)
  508. fcloop_call_host_done(fcpreq, tfcp_req, ret);
  509. return;
  510. }
  511. static void
  512. fcloop_fcp_abort_recv_work(struct work_struct *work)
  513. {
  514. struct fcloop_fcpreq *tfcp_req =
  515. container_of(work, struct fcloop_fcpreq, abort_rcv_work);
  516. struct nvmefc_fcp_req *fcpreq;
  517. bool completed = false;
  518. spin_lock_irq(&tfcp_req->reqlock);
  519. fcpreq = tfcp_req->fcpreq;
  520. switch (tfcp_req->inistate) {
  521. case INI_IO_ABORTED:
  522. break;
  523. case INI_IO_COMPLETED:
  524. completed = true;
  525. break;
  526. default:
  527. spin_unlock_irq(&tfcp_req->reqlock);
  528. WARN_ON(1);
  529. return;
  530. }
  531. spin_unlock_irq(&tfcp_req->reqlock);
  532. if (unlikely(completed)) {
  533. /* remove reference taken in original abort downcall */
  534. fcloop_tfcp_req_put(tfcp_req);
  535. return;
  536. }
  537. if (tfcp_req->tport->targetport)
  538. nvmet_fc_rcv_fcp_abort(tfcp_req->tport->targetport,
  539. &tfcp_req->tgt_fcp_req);
  540. spin_lock_irq(&tfcp_req->reqlock);
  541. tfcp_req->fcpreq = NULL;
  542. spin_unlock_irq(&tfcp_req->reqlock);
  543. fcloop_call_host_done(fcpreq, tfcp_req, -ECANCELED);
  544. /* call_host_done releases reference for abort downcall */
  545. }
  546. /*
  547. * FCP IO operation done by target completion.
  548. * call back up initiator "done" flows.
  549. */
  550. static void
  551. fcloop_tgt_fcprqst_done_work(struct work_struct *work)
  552. {
  553. struct fcloop_fcpreq *tfcp_req =
  554. container_of(work, struct fcloop_fcpreq, tio_done_work);
  555. struct nvmefc_fcp_req *fcpreq;
  556. spin_lock_irq(&tfcp_req->reqlock);
  557. fcpreq = tfcp_req->fcpreq;
  558. tfcp_req->inistate = INI_IO_COMPLETED;
  559. spin_unlock_irq(&tfcp_req->reqlock);
  560. fcloop_call_host_done(fcpreq, tfcp_req, tfcp_req->status);
  561. }
  562. static int
  563. fcloop_fcp_req(struct nvme_fc_local_port *localport,
  564. struct nvme_fc_remote_port *remoteport,
  565. void *hw_queue_handle,
  566. struct nvmefc_fcp_req *fcpreq)
  567. {
  568. struct fcloop_rport *rport = remoteport->private;
  569. struct fcloop_ini_fcpreq *inireq = fcpreq->private;
  570. struct fcloop_fcpreq *tfcp_req;
  571. if (!rport->targetport)
  572. return -ECONNREFUSED;
  573. tfcp_req = kzalloc(sizeof(*tfcp_req), GFP_ATOMIC);
  574. if (!tfcp_req)
  575. return -ENOMEM;
  576. inireq->fcpreq = fcpreq;
  577. inireq->tfcp_req = tfcp_req;
  578. spin_lock_init(&inireq->inilock);
  579. tfcp_req->fcpreq = fcpreq;
  580. tfcp_req->tport = rport->targetport->private;
  581. tfcp_req->inistate = INI_IO_START;
  582. spin_lock_init(&tfcp_req->reqlock);
  583. INIT_WORK(&tfcp_req->fcp_rcv_work, fcloop_fcp_recv_work);
  584. INIT_WORK(&tfcp_req->abort_rcv_work, fcloop_fcp_abort_recv_work);
  585. INIT_WORK(&tfcp_req->tio_done_work, fcloop_tgt_fcprqst_done_work);
  586. kref_init(&tfcp_req->ref);
  587. schedule_work(&tfcp_req->fcp_rcv_work);
  588. return 0;
  589. }
  590. static void
  591. fcloop_fcp_copy_data(u8 op, struct scatterlist *data_sg,
  592. struct scatterlist *io_sg, u32 offset, u32 length)
  593. {
  594. void *data_p, *io_p;
  595. u32 data_len, io_len, tlen;
  596. io_p = sg_virt(io_sg);
  597. io_len = io_sg->length;
  598. for ( ; offset; ) {
  599. tlen = min_t(u32, offset, io_len);
  600. offset -= tlen;
  601. io_len -= tlen;
  602. if (!io_len) {
  603. io_sg = sg_next(io_sg);
  604. io_p = sg_virt(io_sg);
  605. io_len = io_sg->length;
  606. } else
  607. io_p += tlen;
  608. }
  609. data_p = sg_virt(data_sg);
  610. data_len = data_sg->length;
  611. for ( ; length; ) {
  612. tlen = min_t(u32, io_len, data_len);
  613. tlen = min_t(u32, tlen, length);
  614. if (op == NVMET_FCOP_WRITEDATA)
  615. memcpy(data_p, io_p, tlen);
  616. else
  617. memcpy(io_p, data_p, tlen);
  618. length -= tlen;
  619. io_len -= tlen;
  620. if ((!io_len) && (length)) {
  621. io_sg = sg_next(io_sg);
  622. io_p = sg_virt(io_sg);
  623. io_len = io_sg->length;
  624. } else
  625. io_p += tlen;
  626. data_len -= tlen;
  627. if ((!data_len) && (length)) {
  628. data_sg = sg_next(data_sg);
  629. data_p = sg_virt(data_sg);
  630. data_len = data_sg->length;
  631. } else
  632. data_p += tlen;
  633. }
  634. }
  635. static int
  636. fcloop_fcp_op(struct nvmet_fc_target_port *tgtport,
  637. struct nvmefc_tgt_fcp_req *tgt_fcpreq)
  638. {
  639. struct fcloop_fcpreq *tfcp_req = tgt_fcp_req_to_fcpreq(tgt_fcpreq);
  640. struct nvmefc_fcp_req *fcpreq;
  641. u32 rsplen = 0, xfrlen = 0;
  642. int fcp_err = 0, active, aborted;
  643. u8 op = tgt_fcpreq->op;
  644. spin_lock_irq(&tfcp_req->reqlock);
  645. fcpreq = tfcp_req->fcpreq;
  646. active = tfcp_req->active;
  647. aborted = tfcp_req->aborted;
  648. tfcp_req->active = true;
  649. spin_unlock_irq(&tfcp_req->reqlock);
  650. if (unlikely(active))
  651. /* illegal - call while i/o active */
  652. return -EALREADY;
  653. if (unlikely(aborted)) {
  654. /* target transport has aborted i/o prior */
  655. spin_lock_irq(&tfcp_req->reqlock);
  656. tfcp_req->active = false;
  657. spin_unlock_irq(&tfcp_req->reqlock);
  658. tgt_fcpreq->transferred_length = 0;
  659. tgt_fcpreq->fcp_error = -ECANCELED;
  660. tgt_fcpreq->done(tgt_fcpreq);
  661. return 0;
  662. }
  663. /*
  664. * if fcpreq is NULL, the I/O has been aborted (from
  665. * initiator side). For the target side, act as if all is well
  666. * but don't actually move data.
  667. */
  668. switch (op) {
  669. case NVMET_FCOP_WRITEDATA:
  670. xfrlen = tgt_fcpreq->transfer_length;
  671. if (fcpreq) {
  672. fcloop_fcp_copy_data(op, tgt_fcpreq->sg,
  673. fcpreq->first_sgl, tgt_fcpreq->offset,
  674. xfrlen);
  675. fcpreq->transferred_length += xfrlen;
  676. }
  677. break;
  678. case NVMET_FCOP_READDATA:
  679. case NVMET_FCOP_READDATA_RSP:
  680. xfrlen = tgt_fcpreq->transfer_length;
  681. if (fcpreq) {
  682. fcloop_fcp_copy_data(op, tgt_fcpreq->sg,
  683. fcpreq->first_sgl, tgt_fcpreq->offset,
  684. xfrlen);
  685. fcpreq->transferred_length += xfrlen;
  686. }
  687. if (op == NVMET_FCOP_READDATA)
  688. break;
  689. /* Fall-Thru to RSP handling */
  690. fallthrough;
  691. case NVMET_FCOP_RSP:
  692. if (fcpreq) {
  693. rsplen = ((fcpreq->rsplen < tgt_fcpreq->rsplen) ?
  694. fcpreq->rsplen : tgt_fcpreq->rsplen);
  695. memcpy(fcpreq->rspaddr, tgt_fcpreq->rspaddr, rsplen);
  696. if (rsplen < tgt_fcpreq->rsplen)
  697. fcp_err = -E2BIG;
  698. fcpreq->rcv_rsplen = rsplen;
  699. fcpreq->status = 0;
  700. }
  701. tfcp_req->status = 0;
  702. break;
  703. default:
  704. fcp_err = -EINVAL;
  705. break;
  706. }
  707. spin_lock_irq(&tfcp_req->reqlock);
  708. tfcp_req->active = false;
  709. spin_unlock_irq(&tfcp_req->reqlock);
  710. tgt_fcpreq->transferred_length = xfrlen;
  711. tgt_fcpreq->fcp_error = fcp_err;
  712. tgt_fcpreq->done(tgt_fcpreq);
  713. return 0;
  714. }
  715. static void
  716. fcloop_tgt_fcp_abort(struct nvmet_fc_target_port *tgtport,
  717. struct nvmefc_tgt_fcp_req *tgt_fcpreq)
  718. {
  719. struct fcloop_fcpreq *tfcp_req = tgt_fcp_req_to_fcpreq(tgt_fcpreq);
  720. /*
  721. * mark aborted only in case there were 2 threads in transport
  722. * (one doing io, other doing abort) and only kills ops posted
  723. * after the abort request
  724. */
  725. spin_lock_irq(&tfcp_req->reqlock);
  726. tfcp_req->aborted = true;
  727. spin_unlock_irq(&tfcp_req->reqlock);
  728. tfcp_req->status = NVME_SC_INTERNAL;
  729. /*
  730. * nothing more to do. If io wasn't active, the transport should
  731. * immediately call the req_release. If it was active, the op
  732. * will complete, and the lldd should call req_release.
  733. */
  734. }
  735. static void
  736. fcloop_fcp_req_release(struct nvmet_fc_target_port *tgtport,
  737. struct nvmefc_tgt_fcp_req *tgt_fcpreq)
  738. {
  739. struct fcloop_fcpreq *tfcp_req = tgt_fcp_req_to_fcpreq(tgt_fcpreq);
  740. schedule_work(&tfcp_req->tio_done_work);
  741. }
  742. static void
  743. fcloop_h2t_ls_abort(struct nvme_fc_local_port *localport,
  744. struct nvme_fc_remote_port *remoteport,
  745. struct nvmefc_ls_req *lsreq)
  746. {
  747. }
  748. static void
  749. fcloop_t2h_ls_abort(struct nvmet_fc_target_port *targetport,
  750. void *hosthandle, struct nvmefc_ls_req *lsreq)
  751. {
  752. }
  753. static void
  754. fcloop_fcp_abort(struct nvme_fc_local_port *localport,
  755. struct nvme_fc_remote_port *remoteport,
  756. void *hw_queue_handle,
  757. struct nvmefc_fcp_req *fcpreq)
  758. {
  759. struct fcloop_ini_fcpreq *inireq = fcpreq->private;
  760. struct fcloop_fcpreq *tfcp_req;
  761. bool abortio = true;
  762. spin_lock(&inireq->inilock);
  763. tfcp_req = inireq->tfcp_req;
  764. if (tfcp_req)
  765. fcloop_tfcp_req_get(tfcp_req);
  766. spin_unlock(&inireq->inilock);
  767. if (!tfcp_req)
  768. /* abort has already been called */
  769. return;
  770. /* break initiator/target relationship for io */
  771. spin_lock_irq(&tfcp_req->reqlock);
  772. switch (tfcp_req->inistate) {
  773. case INI_IO_START:
  774. case INI_IO_ACTIVE:
  775. tfcp_req->inistate = INI_IO_ABORTED;
  776. break;
  777. case INI_IO_COMPLETED:
  778. abortio = false;
  779. break;
  780. default:
  781. spin_unlock_irq(&tfcp_req->reqlock);
  782. WARN_ON(1);
  783. return;
  784. }
  785. spin_unlock_irq(&tfcp_req->reqlock);
  786. if (abortio)
  787. /* leave the reference while the work item is scheduled */
  788. WARN_ON(!schedule_work(&tfcp_req->abort_rcv_work));
  789. else {
  790. /*
  791. * as the io has already had the done callback made,
  792. * nothing more to do. So release the reference taken above
  793. */
  794. fcloop_tfcp_req_put(tfcp_req);
  795. }
  796. }
  797. static void
  798. fcloop_nport_free(struct kref *ref)
  799. {
  800. struct fcloop_nport *nport =
  801. container_of(ref, struct fcloop_nport, ref);
  802. unsigned long flags;
  803. spin_lock_irqsave(&fcloop_lock, flags);
  804. list_del(&nport->nport_list);
  805. spin_unlock_irqrestore(&fcloop_lock, flags);
  806. kfree(nport);
  807. }
  808. static void
  809. fcloop_nport_put(struct fcloop_nport *nport)
  810. {
  811. kref_put(&nport->ref, fcloop_nport_free);
  812. }
  813. static int
  814. fcloop_nport_get(struct fcloop_nport *nport)
  815. {
  816. return kref_get_unless_zero(&nport->ref);
  817. }
  818. static void
  819. fcloop_localport_delete(struct nvme_fc_local_port *localport)
  820. {
  821. struct fcloop_lport_priv *lport_priv = localport->private;
  822. struct fcloop_lport *lport = lport_priv->lport;
  823. /* release any threads waiting for the unreg to complete */
  824. complete(&lport->unreg_done);
  825. }
  826. static void
  827. fcloop_remoteport_delete(struct nvme_fc_remote_port *remoteport)
  828. {
  829. struct fcloop_rport *rport = remoteport->private;
  830. flush_work(&rport->ls_work);
  831. fcloop_nport_put(rport->nport);
  832. }
  833. static void
  834. fcloop_targetport_delete(struct nvmet_fc_target_port *targetport)
  835. {
  836. struct fcloop_tport *tport = targetport->private;
  837. flush_work(&tport->ls_work);
  838. fcloop_nport_put(tport->nport);
  839. }
  840. #define FCLOOP_HW_QUEUES 4
  841. #define FCLOOP_SGL_SEGS 256
  842. #define FCLOOP_DMABOUND_4G 0xFFFFFFFF
  843. static struct nvme_fc_port_template fctemplate = {
  844. .localport_delete = fcloop_localport_delete,
  845. .remoteport_delete = fcloop_remoteport_delete,
  846. .create_queue = fcloop_create_queue,
  847. .delete_queue = fcloop_delete_queue,
  848. .ls_req = fcloop_h2t_ls_req,
  849. .fcp_io = fcloop_fcp_req,
  850. .ls_abort = fcloop_h2t_ls_abort,
  851. .fcp_abort = fcloop_fcp_abort,
  852. .xmt_ls_rsp = fcloop_t2h_xmt_ls_rsp,
  853. .max_hw_queues = FCLOOP_HW_QUEUES,
  854. .max_sgl_segments = FCLOOP_SGL_SEGS,
  855. .max_dif_sgl_segments = FCLOOP_SGL_SEGS,
  856. .dma_boundary = FCLOOP_DMABOUND_4G,
  857. /* sizes of additional private data for data structures */
  858. .local_priv_sz = sizeof(struct fcloop_lport_priv),
  859. .remote_priv_sz = sizeof(struct fcloop_rport),
  860. .lsrqst_priv_sz = sizeof(struct fcloop_lsreq),
  861. .fcprqst_priv_sz = sizeof(struct fcloop_ini_fcpreq),
  862. };
  863. static struct nvmet_fc_target_template tgttemplate = {
  864. .targetport_delete = fcloop_targetport_delete,
  865. .xmt_ls_rsp = fcloop_h2t_xmt_ls_rsp,
  866. .fcp_op = fcloop_fcp_op,
  867. .fcp_abort = fcloop_tgt_fcp_abort,
  868. .fcp_req_release = fcloop_fcp_req_release,
  869. .discovery_event = fcloop_tgt_discovery_evt,
  870. .ls_req = fcloop_t2h_ls_req,
  871. .ls_abort = fcloop_t2h_ls_abort,
  872. .host_release = fcloop_t2h_host_release,
  873. .max_hw_queues = FCLOOP_HW_QUEUES,
  874. .max_sgl_segments = FCLOOP_SGL_SEGS,
  875. .max_dif_sgl_segments = FCLOOP_SGL_SEGS,
  876. .dma_boundary = FCLOOP_DMABOUND_4G,
  877. /* optional features */
  878. .target_features = 0,
  879. /* sizes of additional private data for data structures */
  880. .target_priv_sz = sizeof(struct fcloop_tport),
  881. .lsrqst_priv_sz = sizeof(struct fcloop_lsreq),
  882. };
  883. static ssize_t
  884. fcloop_create_local_port(struct device *dev, struct device_attribute *attr,
  885. const char *buf, size_t count)
  886. {
  887. struct nvme_fc_port_info pinfo;
  888. struct fcloop_ctrl_options *opts;
  889. struct nvme_fc_local_port *localport;
  890. struct fcloop_lport *lport;
  891. struct fcloop_lport_priv *lport_priv;
  892. unsigned long flags;
  893. int ret = -ENOMEM;
  894. lport = kzalloc(sizeof(*lport), GFP_KERNEL);
  895. if (!lport)
  896. return -ENOMEM;
  897. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  898. if (!opts)
  899. goto out_free_lport;
  900. ret = fcloop_parse_options(opts, buf);
  901. if (ret)
  902. goto out_free_opts;
  903. /* everything there ? */
  904. if ((opts->mask & LPORT_OPTS) != LPORT_OPTS) {
  905. ret = -EINVAL;
  906. goto out_free_opts;
  907. }
  908. memset(&pinfo, 0, sizeof(pinfo));
  909. pinfo.node_name = opts->wwnn;
  910. pinfo.port_name = opts->wwpn;
  911. pinfo.port_role = opts->roles;
  912. pinfo.port_id = opts->fcaddr;
  913. ret = nvme_fc_register_localport(&pinfo, &fctemplate, NULL, &localport);
  914. if (!ret) {
  915. /* success */
  916. lport_priv = localport->private;
  917. lport_priv->lport = lport;
  918. lport->localport = localport;
  919. INIT_LIST_HEAD(&lport->lport_list);
  920. spin_lock_irqsave(&fcloop_lock, flags);
  921. list_add_tail(&lport->lport_list, &fcloop_lports);
  922. spin_unlock_irqrestore(&fcloop_lock, flags);
  923. }
  924. out_free_opts:
  925. kfree(opts);
  926. out_free_lport:
  927. /* free only if we're going to fail */
  928. if (ret)
  929. kfree(lport);
  930. return ret ? ret : count;
  931. }
  932. static void
  933. __unlink_local_port(struct fcloop_lport *lport)
  934. {
  935. list_del(&lport->lport_list);
  936. }
  937. static int
  938. __wait_localport_unreg(struct fcloop_lport *lport)
  939. {
  940. int ret;
  941. init_completion(&lport->unreg_done);
  942. ret = nvme_fc_unregister_localport(lport->localport);
  943. wait_for_completion(&lport->unreg_done);
  944. kfree(lport);
  945. return ret;
  946. }
  947. static ssize_t
  948. fcloop_delete_local_port(struct device *dev, struct device_attribute *attr,
  949. const char *buf, size_t count)
  950. {
  951. struct fcloop_lport *tlport, *lport = NULL;
  952. u64 nodename, portname;
  953. unsigned long flags;
  954. int ret;
  955. ret = fcloop_parse_nm_options(dev, &nodename, &portname, buf);
  956. if (ret)
  957. return ret;
  958. spin_lock_irqsave(&fcloop_lock, flags);
  959. list_for_each_entry(tlport, &fcloop_lports, lport_list) {
  960. if (tlport->localport->node_name == nodename &&
  961. tlport->localport->port_name == portname) {
  962. lport = tlport;
  963. __unlink_local_port(lport);
  964. break;
  965. }
  966. }
  967. spin_unlock_irqrestore(&fcloop_lock, flags);
  968. if (!lport)
  969. return -ENOENT;
  970. ret = __wait_localport_unreg(lport);
  971. return ret ? ret : count;
  972. }
  973. static struct fcloop_nport *
  974. fcloop_alloc_nport(const char *buf, size_t count, bool remoteport)
  975. {
  976. struct fcloop_nport *newnport, *nport = NULL;
  977. struct fcloop_lport *tmplport, *lport = NULL;
  978. struct fcloop_ctrl_options *opts;
  979. unsigned long flags;
  980. u32 opts_mask = (remoteport) ? RPORT_OPTS : TGTPORT_OPTS;
  981. int ret;
  982. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  983. if (!opts)
  984. return NULL;
  985. ret = fcloop_parse_options(opts, buf);
  986. if (ret)
  987. goto out_free_opts;
  988. /* everything there ? */
  989. if ((opts->mask & opts_mask) != opts_mask) {
  990. ret = -EINVAL;
  991. goto out_free_opts;
  992. }
  993. newnport = kzalloc(sizeof(*newnport), GFP_KERNEL);
  994. if (!newnport)
  995. goto out_free_opts;
  996. INIT_LIST_HEAD(&newnport->nport_list);
  997. newnport->node_name = opts->wwnn;
  998. newnport->port_name = opts->wwpn;
  999. if (opts->mask & NVMF_OPT_ROLES)
  1000. newnport->port_role = opts->roles;
  1001. if (opts->mask & NVMF_OPT_FCADDR)
  1002. newnport->port_id = opts->fcaddr;
  1003. kref_init(&newnport->ref);
  1004. spin_lock_irqsave(&fcloop_lock, flags);
  1005. list_for_each_entry(tmplport, &fcloop_lports, lport_list) {
  1006. if (tmplport->localport->node_name == opts->wwnn &&
  1007. tmplport->localport->port_name == opts->wwpn)
  1008. goto out_invalid_opts;
  1009. if (tmplport->localport->node_name == opts->lpwwnn &&
  1010. tmplport->localport->port_name == opts->lpwwpn)
  1011. lport = tmplport;
  1012. }
  1013. if (remoteport) {
  1014. if (!lport)
  1015. goto out_invalid_opts;
  1016. newnport->lport = lport;
  1017. }
  1018. list_for_each_entry(nport, &fcloop_nports, nport_list) {
  1019. if (nport->node_name == opts->wwnn &&
  1020. nport->port_name == opts->wwpn) {
  1021. if ((remoteport && nport->rport) ||
  1022. (!remoteport && nport->tport)) {
  1023. nport = NULL;
  1024. goto out_invalid_opts;
  1025. }
  1026. fcloop_nport_get(nport);
  1027. spin_unlock_irqrestore(&fcloop_lock, flags);
  1028. if (remoteport)
  1029. nport->lport = lport;
  1030. if (opts->mask & NVMF_OPT_ROLES)
  1031. nport->port_role = opts->roles;
  1032. if (opts->mask & NVMF_OPT_FCADDR)
  1033. nport->port_id = opts->fcaddr;
  1034. goto out_free_newnport;
  1035. }
  1036. }
  1037. list_add_tail(&newnport->nport_list, &fcloop_nports);
  1038. spin_unlock_irqrestore(&fcloop_lock, flags);
  1039. kfree(opts);
  1040. return newnport;
  1041. out_invalid_opts:
  1042. spin_unlock_irqrestore(&fcloop_lock, flags);
  1043. out_free_newnport:
  1044. kfree(newnport);
  1045. out_free_opts:
  1046. kfree(opts);
  1047. return nport;
  1048. }
  1049. static ssize_t
  1050. fcloop_create_remote_port(struct device *dev, struct device_attribute *attr,
  1051. const char *buf, size_t count)
  1052. {
  1053. struct nvme_fc_remote_port *remoteport;
  1054. struct fcloop_nport *nport;
  1055. struct fcloop_rport *rport;
  1056. struct nvme_fc_port_info pinfo;
  1057. int ret;
  1058. nport = fcloop_alloc_nport(buf, count, true);
  1059. if (!nport)
  1060. return -EIO;
  1061. memset(&pinfo, 0, sizeof(pinfo));
  1062. pinfo.node_name = nport->node_name;
  1063. pinfo.port_name = nport->port_name;
  1064. pinfo.port_role = nport->port_role;
  1065. pinfo.port_id = nport->port_id;
  1066. ret = nvme_fc_register_remoteport(nport->lport->localport,
  1067. &pinfo, &remoteport);
  1068. if (ret || !remoteport) {
  1069. fcloop_nport_put(nport);
  1070. return ret;
  1071. }
  1072. /* success */
  1073. rport = remoteport->private;
  1074. rport->remoteport = remoteport;
  1075. rport->targetport = (nport->tport) ? nport->tport->targetport : NULL;
  1076. if (nport->tport) {
  1077. nport->tport->remoteport = remoteport;
  1078. nport->tport->lport = nport->lport;
  1079. }
  1080. rport->nport = nport;
  1081. rport->lport = nport->lport;
  1082. nport->rport = rport;
  1083. spin_lock_init(&rport->lock);
  1084. INIT_WORK(&rport->ls_work, fcloop_rport_lsrqst_work);
  1085. INIT_LIST_HEAD(&rport->ls_list);
  1086. return count;
  1087. }
  1088. static struct fcloop_rport *
  1089. __unlink_remote_port(struct fcloop_nport *nport)
  1090. {
  1091. struct fcloop_rport *rport = nport->rport;
  1092. if (rport && nport->tport)
  1093. nport->tport->remoteport = NULL;
  1094. nport->rport = NULL;
  1095. return rport;
  1096. }
  1097. static int
  1098. __remoteport_unreg(struct fcloop_nport *nport, struct fcloop_rport *rport)
  1099. {
  1100. if (!rport)
  1101. return -EALREADY;
  1102. return nvme_fc_unregister_remoteport(rport->remoteport);
  1103. }
  1104. static ssize_t
  1105. fcloop_delete_remote_port(struct device *dev, struct device_attribute *attr,
  1106. const char *buf, size_t count)
  1107. {
  1108. struct fcloop_nport *nport = NULL, *tmpport;
  1109. static struct fcloop_rport *rport;
  1110. u64 nodename, portname;
  1111. unsigned long flags;
  1112. int ret;
  1113. ret = fcloop_parse_nm_options(dev, &nodename, &portname, buf);
  1114. if (ret)
  1115. return ret;
  1116. spin_lock_irqsave(&fcloop_lock, flags);
  1117. list_for_each_entry(tmpport, &fcloop_nports, nport_list) {
  1118. if (tmpport->node_name == nodename &&
  1119. tmpport->port_name == portname && tmpport->rport) {
  1120. nport = tmpport;
  1121. rport = __unlink_remote_port(nport);
  1122. break;
  1123. }
  1124. }
  1125. spin_unlock_irqrestore(&fcloop_lock, flags);
  1126. if (!nport)
  1127. return -ENOENT;
  1128. ret = __remoteport_unreg(nport, rport);
  1129. return ret ? ret : count;
  1130. }
  1131. static ssize_t
  1132. fcloop_create_target_port(struct device *dev, struct device_attribute *attr,
  1133. const char *buf, size_t count)
  1134. {
  1135. struct nvmet_fc_target_port *targetport;
  1136. struct fcloop_nport *nport;
  1137. struct fcloop_tport *tport;
  1138. struct nvmet_fc_port_info tinfo;
  1139. int ret;
  1140. nport = fcloop_alloc_nport(buf, count, false);
  1141. if (!nport)
  1142. return -EIO;
  1143. tinfo.node_name = nport->node_name;
  1144. tinfo.port_name = nport->port_name;
  1145. tinfo.port_id = nport->port_id;
  1146. ret = nvmet_fc_register_targetport(&tinfo, &tgttemplate, NULL,
  1147. &targetport);
  1148. if (ret) {
  1149. fcloop_nport_put(nport);
  1150. return ret;
  1151. }
  1152. /* success */
  1153. tport = targetport->private;
  1154. tport->targetport = targetport;
  1155. tport->remoteport = (nport->rport) ? nport->rport->remoteport : NULL;
  1156. if (nport->rport)
  1157. nport->rport->targetport = targetport;
  1158. tport->nport = nport;
  1159. tport->lport = nport->lport;
  1160. nport->tport = tport;
  1161. spin_lock_init(&tport->lock);
  1162. INIT_WORK(&tport->ls_work, fcloop_tport_lsrqst_work);
  1163. INIT_LIST_HEAD(&tport->ls_list);
  1164. return count;
  1165. }
  1166. static struct fcloop_tport *
  1167. __unlink_target_port(struct fcloop_nport *nport)
  1168. {
  1169. struct fcloop_tport *tport = nport->tport;
  1170. if (tport && nport->rport)
  1171. nport->rport->targetport = NULL;
  1172. nport->tport = NULL;
  1173. return tport;
  1174. }
  1175. static int
  1176. __targetport_unreg(struct fcloop_nport *nport, struct fcloop_tport *tport)
  1177. {
  1178. if (!tport)
  1179. return -EALREADY;
  1180. return nvmet_fc_unregister_targetport(tport->targetport);
  1181. }
  1182. static ssize_t
  1183. fcloop_delete_target_port(struct device *dev, struct device_attribute *attr,
  1184. const char *buf, size_t count)
  1185. {
  1186. struct fcloop_nport *nport = NULL, *tmpport;
  1187. struct fcloop_tport *tport = NULL;
  1188. u64 nodename, portname;
  1189. unsigned long flags;
  1190. int ret;
  1191. ret = fcloop_parse_nm_options(dev, &nodename, &portname, buf);
  1192. if (ret)
  1193. return ret;
  1194. spin_lock_irqsave(&fcloop_lock, flags);
  1195. list_for_each_entry(tmpport, &fcloop_nports, nport_list) {
  1196. if (tmpport->node_name == nodename &&
  1197. tmpport->port_name == portname && tmpport->tport) {
  1198. nport = tmpport;
  1199. tport = __unlink_target_port(nport);
  1200. break;
  1201. }
  1202. }
  1203. spin_unlock_irqrestore(&fcloop_lock, flags);
  1204. if (!nport)
  1205. return -ENOENT;
  1206. ret = __targetport_unreg(nport, tport);
  1207. return ret ? ret : count;
  1208. }
  1209. static DEVICE_ATTR(add_local_port, 0200, NULL, fcloop_create_local_port);
  1210. static DEVICE_ATTR(del_local_port, 0200, NULL, fcloop_delete_local_port);
  1211. static DEVICE_ATTR(add_remote_port, 0200, NULL, fcloop_create_remote_port);
  1212. static DEVICE_ATTR(del_remote_port, 0200, NULL, fcloop_delete_remote_port);
  1213. static DEVICE_ATTR(add_target_port, 0200, NULL, fcloop_create_target_port);
  1214. static DEVICE_ATTR(del_target_port, 0200, NULL, fcloop_delete_target_port);
  1215. static struct attribute *fcloop_dev_attrs[] = {
  1216. &dev_attr_add_local_port.attr,
  1217. &dev_attr_del_local_port.attr,
  1218. &dev_attr_add_remote_port.attr,
  1219. &dev_attr_del_remote_port.attr,
  1220. &dev_attr_add_target_port.attr,
  1221. &dev_attr_del_target_port.attr,
  1222. NULL
  1223. };
  1224. static struct attribute_group fclopp_dev_attrs_group = {
  1225. .attrs = fcloop_dev_attrs,
  1226. };
  1227. static const struct attribute_group *fcloop_dev_attr_groups[] = {
  1228. &fclopp_dev_attrs_group,
  1229. NULL,
  1230. };
  1231. static struct class *fcloop_class;
  1232. static struct device *fcloop_device;
  1233. static int __init fcloop_init(void)
  1234. {
  1235. int ret;
  1236. fcloop_class = class_create(THIS_MODULE, "fcloop");
  1237. if (IS_ERR(fcloop_class)) {
  1238. pr_err("couldn't register class fcloop\n");
  1239. ret = PTR_ERR(fcloop_class);
  1240. return ret;
  1241. }
  1242. fcloop_device = device_create_with_groups(
  1243. fcloop_class, NULL, MKDEV(0, 0), NULL,
  1244. fcloop_dev_attr_groups, "ctl");
  1245. if (IS_ERR(fcloop_device)) {
  1246. pr_err("couldn't create ctl device!\n");
  1247. ret = PTR_ERR(fcloop_device);
  1248. goto out_destroy_class;
  1249. }
  1250. get_device(fcloop_device);
  1251. return 0;
  1252. out_destroy_class:
  1253. class_destroy(fcloop_class);
  1254. return ret;
  1255. }
  1256. static void __exit fcloop_exit(void)
  1257. {
  1258. struct fcloop_lport *lport;
  1259. struct fcloop_nport *nport;
  1260. struct fcloop_tport *tport;
  1261. struct fcloop_rport *rport;
  1262. unsigned long flags;
  1263. int ret;
  1264. spin_lock_irqsave(&fcloop_lock, flags);
  1265. for (;;) {
  1266. nport = list_first_entry_or_null(&fcloop_nports,
  1267. typeof(*nport), nport_list);
  1268. if (!nport)
  1269. break;
  1270. tport = __unlink_target_port(nport);
  1271. rport = __unlink_remote_port(nport);
  1272. spin_unlock_irqrestore(&fcloop_lock, flags);
  1273. ret = __targetport_unreg(nport, tport);
  1274. if (ret)
  1275. pr_warn("%s: Failed deleting target port\n", __func__);
  1276. ret = __remoteport_unreg(nport, rport);
  1277. if (ret)
  1278. pr_warn("%s: Failed deleting remote port\n", __func__);
  1279. spin_lock_irqsave(&fcloop_lock, flags);
  1280. }
  1281. for (;;) {
  1282. lport = list_first_entry_or_null(&fcloop_lports,
  1283. typeof(*lport), lport_list);
  1284. if (!lport)
  1285. break;
  1286. __unlink_local_port(lport);
  1287. spin_unlock_irqrestore(&fcloop_lock, flags);
  1288. ret = __wait_localport_unreg(lport);
  1289. if (ret)
  1290. pr_warn("%s: Failed deleting local port\n", __func__);
  1291. spin_lock_irqsave(&fcloop_lock, flags);
  1292. }
  1293. spin_unlock_irqrestore(&fcloop_lock, flags);
  1294. put_device(fcloop_device);
  1295. device_destroy(fcloop_class, MKDEV(0, 0));
  1296. class_destroy(fcloop_class);
  1297. }
  1298. module_init(fcloop_init);
  1299. module_exit(fcloop_exit);
  1300. MODULE_LICENSE("GPL v2");