fcloop.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667
  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(&tls_req->ls_list, &rport->ls_list);
  311. spin_unlock(&rport->lock);
  312. queue_work(nvmet_wq, &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(&tls_req->ls_list, &rport->ls_list);
  338. spin_unlock(&rport->lock);
  339. queue_work(nvmet_wq, &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(&tls_req->ls_list, &tport->ls_list);
  384. spin_unlock(&tport->lock);
  385. queue_work(nvmet_wq, &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(&tls_req->ls_list, &tport->ls_list);
  411. spin_unlock(&tport->lock);
  412. queue_work(nvmet_wq, &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. static int
  422. fcloop_t2h_host_traddr(void *hosthandle, u64 *wwnn, u64 *wwpn)
  423. {
  424. struct fcloop_rport *rport = hosthandle;
  425. *wwnn = rport->lport->localport->node_name;
  426. *wwpn = rport->lport->localport->port_name;
  427. return 0;
  428. }
  429. /*
  430. * Simulate reception of RSCN and converting it to a initiator transport
  431. * call to rescan a remote port.
  432. */
  433. static void
  434. fcloop_tgt_rscn_work(struct work_struct *work)
  435. {
  436. struct fcloop_rscn *tgt_rscn =
  437. container_of(work, struct fcloop_rscn, work);
  438. struct fcloop_tport *tport = tgt_rscn->tport;
  439. if (tport->remoteport)
  440. nvme_fc_rescan_remoteport(tport->remoteport);
  441. kfree(tgt_rscn);
  442. }
  443. static void
  444. fcloop_tgt_discovery_evt(struct nvmet_fc_target_port *tgtport)
  445. {
  446. struct fcloop_rscn *tgt_rscn;
  447. tgt_rscn = kzalloc(sizeof(*tgt_rscn), GFP_KERNEL);
  448. if (!tgt_rscn)
  449. return;
  450. tgt_rscn->tport = tgtport->private;
  451. INIT_WORK(&tgt_rscn->work, fcloop_tgt_rscn_work);
  452. queue_work(nvmet_wq, &tgt_rscn->work);
  453. }
  454. static void
  455. fcloop_tfcp_req_free(struct kref *ref)
  456. {
  457. struct fcloop_fcpreq *tfcp_req =
  458. container_of(ref, struct fcloop_fcpreq, ref);
  459. kfree(tfcp_req);
  460. }
  461. static void
  462. fcloop_tfcp_req_put(struct fcloop_fcpreq *tfcp_req)
  463. {
  464. kref_put(&tfcp_req->ref, fcloop_tfcp_req_free);
  465. }
  466. static int
  467. fcloop_tfcp_req_get(struct fcloop_fcpreq *tfcp_req)
  468. {
  469. return kref_get_unless_zero(&tfcp_req->ref);
  470. }
  471. static void
  472. fcloop_call_host_done(struct nvmefc_fcp_req *fcpreq,
  473. struct fcloop_fcpreq *tfcp_req, int status)
  474. {
  475. struct fcloop_ini_fcpreq *inireq = NULL;
  476. if (fcpreq) {
  477. inireq = fcpreq->private;
  478. spin_lock(&inireq->inilock);
  479. inireq->tfcp_req = NULL;
  480. spin_unlock(&inireq->inilock);
  481. fcpreq->status = status;
  482. fcpreq->done(fcpreq);
  483. }
  484. /* release original io reference on tgt struct */
  485. fcloop_tfcp_req_put(tfcp_req);
  486. }
  487. static bool drop_fabric_opcode;
  488. #define DROP_OPCODE_MASK 0x00FF
  489. /* fabrics opcode will have a bit set above 1st byte */
  490. static int drop_opcode = -1;
  491. static int drop_instance;
  492. static int drop_amount;
  493. static int drop_current_cnt;
  494. /*
  495. * Routine to parse io and determine if the io is to be dropped.
  496. * Returns:
  497. * 0 if io is not obstructed
  498. * 1 if io was dropped
  499. */
  500. static int check_for_drop(struct fcloop_fcpreq *tfcp_req)
  501. {
  502. struct nvmefc_fcp_req *fcpreq = tfcp_req->fcpreq;
  503. struct nvme_fc_cmd_iu *cmdiu = fcpreq->cmdaddr;
  504. struct nvme_command *sqe = &cmdiu->sqe;
  505. if (drop_opcode == -1)
  506. return 0;
  507. pr_info("%s: seq opcd x%02x fctype x%02x: drop F %s op x%02x "
  508. "inst %d start %d amt %d\n",
  509. __func__, sqe->common.opcode, sqe->fabrics.fctype,
  510. drop_fabric_opcode ? "y" : "n",
  511. drop_opcode, drop_current_cnt, drop_instance, drop_amount);
  512. if ((drop_fabric_opcode &&
  513. (sqe->common.opcode != nvme_fabrics_command ||
  514. sqe->fabrics.fctype != drop_opcode)) ||
  515. (!drop_fabric_opcode && sqe->common.opcode != drop_opcode))
  516. return 0;
  517. if (++drop_current_cnt >= drop_instance) {
  518. if (drop_current_cnt >= drop_instance + drop_amount)
  519. drop_opcode = -1;
  520. return 1;
  521. }
  522. return 0;
  523. }
  524. static void
  525. fcloop_fcp_recv_work(struct work_struct *work)
  526. {
  527. struct fcloop_fcpreq *tfcp_req =
  528. container_of(work, struct fcloop_fcpreq, fcp_rcv_work);
  529. struct nvmefc_fcp_req *fcpreq;
  530. unsigned long flags;
  531. int ret = 0;
  532. bool aborted = false;
  533. spin_lock_irqsave(&tfcp_req->reqlock, flags);
  534. fcpreq = tfcp_req->fcpreq;
  535. switch (tfcp_req->inistate) {
  536. case INI_IO_START:
  537. tfcp_req->inistate = INI_IO_ACTIVE;
  538. break;
  539. case INI_IO_ABORTED:
  540. aborted = true;
  541. break;
  542. default:
  543. spin_unlock_irqrestore(&tfcp_req->reqlock, flags);
  544. WARN_ON(1);
  545. return;
  546. }
  547. spin_unlock_irqrestore(&tfcp_req->reqlock, flags);
  548. if (unlikely(aborted)) {
  549. /* the abort handler will call fcloop_call_host_done */
  550. return;
  551. }
  552. if (unlikely(check_for_drop(tfcp_req))) {
  553. pr_info("%s: dropped command ********\n", __func__);
  554. return;
  555. }
  556. ret = nvmet_fc_rcv_fcp_req(tfcp_req->tport->targetport,
  557. &tfcp_req->tgt_fcp_req,
  558. fcpreq->cmdaddr, fcpreq->cmdlen);
  559. if (ret)
  560. fcloop_call_host_done(fcpreq, tfcp_req, ret);
  561. }
  562. static void
  563. fcloop_fcp_abort_recv_work(struct work_struct *work)
  564. {
  565. struct fcloop_fcpreq *tfcp_req =
  566. container_of(work, struct fcloop_fcpreq, abort_rcv_work);
  567. struct nvmefc_fcp_req *fcpreq;
  568. bool completed = false;
  569. unsigned long flags;
  570. spin_lock_irqsave(&tfcp_req->reqlock, flags);
  571. switch (tfcp_req->inistate) {
  572. case INI_IO_ABORTED:
  573. fcpreq = tfcp_req->fcpreq;
  574. tfcp_req->fcpreq = NULL;
  575. break;
  576. case INI_IO_COMPLETED:
  577. completed = true;
  578. break;
  579. default:
  580. spin_unlock_irqrestore(&tfcp_req->reqlock, flags);
  581. WARN_ON(1);
  582. return;
  583. }
  584. spin_unlock_irqrestore(&tfcp_req->reqlock, flags);
  585. if (unlikely(completed)) {
  586. /* remove reference taken in original abort downcall */
  587. fcloop_tfcp_req_put(tfcp_req);
  588. return;
  589. }
  590. if (tfcp_req->tport->targetport)
  591. nvmet_fc_rcv_fcp_abort(tfcp_req->tport->targetport,
  592. &tfcp_req->tgt_fcp_req);
  593. fcloop_call_host_done(fcpreq, tfcp_req, -ECANCELED);
  594. /* call_host_done releases reference for abort downcall */
  595. }
  596. /*
  597. * FCP IO operation done by target completion.
  598. * call back up initiator "done" flows.
  599. */
  600. static void
  601. fcloop_tgt_fcprqst_done_work(struct work_struct *work)
  602. {
  603. struct fcloop_fcpreq *tfcp_req =
  604. container_of(work, struct fcloop_fcpreq, tio_done_work);
  605. struct nvmefc_fcp_req *fcpreq;
  606. unsigned long flags;
  607. spin_lock_irqsave(&tfcp_req->reqlock, flags);
  608. fcpreq = tfcp_req->fcpreq;
  609. tfcp_req->inistate = INI_IO_COMPLETED;
  610. spin_unlock_irqrestore(&tfcp_req->reqlock, flags);
  611. fcloop_call_host_done(fcpreq, tfcp_req, tfcp_req->status);
  612. }
  613. static int
  614. fcloop_fcp_req(struct nvme_fc_local_port *localport,
  615. struct nvme_fc_remote_port *remoteport,
  616. void *hw_queue_handle,
  617. struct nvmefc_fcp_req *fcpreq)
  618. {
  619. struct fcloop_rport *rport = remoteport->private;
  620. struct fcloop_ini_fcpreq *inireq = fcpreq->private;
  621. struct fcloop_fcpreq *tfcp_req;
  622. if (!rport->targetport)
  623. return -ECONNREFUSED;
  624. tfcp_req = kzalloc(sizeof(*tfcp_req), GFP_ATOMIC);
  625. if (!tfcp_req)
  626. return -ENOMEM;
  627. inireq->fcpreq = fcpreq;
  628. inireq->tfcp_req = tfcp_req;
  629. spin_lock_init(&inireq->inilock);
  630. tfcp_req->fcpreq = fcpreq;
  631. tfcp_req->tport = rport->targetport->private;
  632. tfcp_req->inistate = INI_IO_START;
  633. spin_lock_init(&tfcp_req->reqlock);
  634. INIT_WORK(&tfcp_req->fcp_rcv_work, fcloop_fcp_recv_work);
  635. INIT_WORK(&tfcp_req->abort_rcv_work, fcloop_fcp_abort_recv_work);
  636. INIT_WORK(&tfcp_req->tio_done_work, fcloop_tgt_fcprqst_done_work);
  637. kref_init(&tfcp_req->ref);
  638. queue_work(nvmet_wq, &tfcp_req->fcp_rcv_work);
  639. return 0;
  640. }
  641. static void
  642. fcloop_fcp_copy_data(u8 op, struct scatterlist *data_sg,
  643. struct scatterlist *io_sg, u32 offset, u32 length)
  644. {
  645. void *data_p, *io_p;
  646. u32 data_len, io_len, tlen;
  647. io_p = sg_virt(io_sg);
  648. io_len = io_sg->length;
  649. for ( ; offset; ) {
  650. tlen = min_t(u32, offset, io_len);
  651. offset -= tlen;
  652. io_len -= tlen;
  653. if (!io_len) {
  654. io_sg = sg_next(io_sg);
  655. io_p = sg_virt(io_sg);
  656. io_len = io_sg->length;
  657. } else
  658. io_p += tlen;
  659. }
  660. data_p = sg_virt(data_sg);
  661. data_len = data_sg->length;
  662. for ( ; length; ) {
  663. tlen = min_t(u32, io_len, data_len);
  664. tlen = min_t(u32, tlen, length);
  665. if (op == NVMET_FCOP_WRITEDATA)
  666. memcpy(data_p, io_p, tlen);
  667. else
  668. memcpy(io_p, data_p, tlen);
  669. length -= tlen;
  670. io_len -= tlen;
  671. if ((!io_len) && (length)) {
  672. io_sg = sg_next(io_sg);
  673. io_p = sg_virt(io_sg);
  674. io_len = io_sg->length;
  675. } else
  676. io_p += tlen;
  677. data_len -= tlen;
  678. if ((!data_len) && (length)) {
  679. data_sg = sg_next(data_sg);
  680. data_p = sg_virt(data_sg);
  681. data_len = data_sg->length;
  682. } else
  683. data_p += tlen;
  684. }
  685. }
  686. static int
  687. fcloop_fcp_op(struct nvmet_fc_target_port *tgtport,
  688. struct nvmefc_tgt_fcp_req *tgt_fcpreq)
  689. {
  690. struct fcloop_fcpreq *tfcp_req = tgt_fcp_req_to_fcpreq(tgt_fcpreq);
  691. struct nvmefc_fcp_req *fcpreq;
  692. u32 rsplen = 0, xfrlen = 0;
  693. int fcp_err = 0, active, aborted;
  694. u8 op = tgt_fcpreq->op;
  695. unsigned long flags;
  696. spin_lock_irqsave(&tfcp_req->reqlock, flags);
  697. fcpreq = tfcp_req->fcpreq;
  698. active = tfcp_req->active;
  699. aborted = tfcp_req->aborted;
  700. tfcp_req->active = true;
  701. spin_unlock_irqrestore(&tfcp_req->reqlock, flags);
  702. if (unlikely(active))
  703. /* illegal - call while i/o active */
  704. return -EALREADY;
  705. if (unlikely(aborted)) {
  706. /* target transport has aborted i/o prior */
  707. spin_lock_irqsave(&tfcp_req->reqlock, flags);
  708. tfcp_req->active = false;
  709. spin_unlock_irqrestore(&tfcp_req->reqlock, flags);
  710. tgt_fcpreq->transferred_length = 0;
  711. tgt_fcpreq->fcp_error = -ECANCELED;
  712. tgt_fcpreq->done(tgt_fcpreq);
  713. return 0;
  714. }
  715. /*
  716. * if fcpreq is NULL, the I/O has been aborted (from
  717. * initiator side). For the target side, act as if all is well
  718. * but don't actually move data.
  719. */
  720. switch (op) {
  721. case NVMET_FCOP_WRITEDATA:
  722. xfrlen = tgt_fcpreq->transfer_length;
  723. if (fcpreq) {
  724. fcloop_fcp_copy_data(op, tgt_fcpreq->sg,
  725. fcpreq->first_sgl, tgt_fcpreq->offset,
  726. xfrlen);
  727. fcpreq->transferred_length += xfrlen;
  728. }
  729. break;
  730. case NVMET_FCOP_READDATA:
  731. case NVMET_FCOP_READDATA_RSP:
  732. xfrlen = tgt_fcpreq->transfer_length;
  733. if (fcpreq) {
  734. fcloop_fcp_copy_data(op, tgt_fcpreq->sg,
  735. fcpreq->first_sgl, tgt_fcpreq->offset,
  736. xfrlen);
  737. fcpreq->transferred_length += xfrlen;
  738. }
  739. if (op == NVMET_FCOP_READDATA)
  740. break;
  741. /* Fall-Thru to RSP handling */
  742. fallthrough;
  743. case NVMET_FCOP_RSP:
  744. if (fcpreq) {
  745. rsplen = ((fcpreq->rsplen < tgt_fcpreq->rsplen) ?
  746. fcpreq->rsplen : tgt_fcpreq->rsplen);
  747. memcpy(fcpreq->rspaddr, tgt_fcpreq->rspaddr, rsplen);
  748. if (rsplen < tgt_fcpreq->rsplen)
  749. fcp_err = -E2BIG;
  750. fcpreq->rcv_rsplen = rsplen;
  751. fcpreq->status = 0;
  752. }
  753. tfcp_req->status = 0;
  754. break;
  755. default:
  756. fcp_err = -EINVAL;
  757. break;
  758. }
  759. spin_lock_irqsave(&tfcp_req->reqlock, flags);
  760. tfcp_req->active = false;
  761. spin_unlock_irqrestore(&tfcp_req->reqlock, flags);
  762. tgt_fcpreq->transferred_length = xfrlen;
  763. tgt_fcpreq->fcp_error = fcp_err;
  764. tgt_fcpreq->done(tgt_fcpreq);
  765. return 0;
  766. }
  767. static void
  768. fcloop_tgt_fcp_abort(struct nvmet_fc_target_port *tgtport,
  769. struct nvmefc_tgt_fcp_req *tgt_fcpreq)
  770. {
  771. struct fcloop_fcpreq *tfcp_req = tgt_fcp_req_to_fcpreq(tgt_fcpreq);
  772. unsigned long flags;
  773. /*
  774. * mark aborted only in case there were 2 threads in transport
  775. * (one doing io, other doing abort) and only kills ops posted
  776. * after the abort request
  777. */
  778. spin_lock_irqsave(&tfcp_req->reqlock, flags);
  779. tfcp_req->aborted = true;
  780. spin_unlock_irqrestore(&tfcp_req->reqlock, flags);
  781. tfcp_req->status = NVME_SC_INTERNAL;
  782. /*
  783. * nothing more to do. If io wasn't active, the transport should
  784. * immediately call the req_release. If it was active, the op
  785. * will complete, and the lldd should call req_release.
  786. */
  787. }
  788. static void
  789. fcloop_fcp_req_release(struct nvmet_fc_target_port *tgtport,
  790. struct nvmefc_tgt_fcp_req *tgt_fcpreq)
  791. {
  792. struct fcloop_fcpreq *tfcp_req = tgt_fcp_req_to_fcpreq(tgt_fcpreq);
  793. queue_work(nvmet_wq, &tfcp_req->tio_done_work);
  794. }
  795. static void
  796. fcloop_h2t_ls_abort(struct nvme_fc_local_port *localport,
  797. struct nvme_fc_remote_port *remoteport,
  798. struct nvmefc_ls_req *lsreq)
  799. {
  800. }
  801. static void
  802. fcloop_t2h_ls_abort(struct nvmet_fc_target_port *targetport,
  803. void *hosthandle, struct nvmefc_ls_req *lsreq)
  804. {
  805. }
  806. static void
  807. fcloop_fcp_abort(struct nvme_fc_local_port *localport,
  808. struct nvme_fc_remote_port *remoteport,
  809. void *hw_queue_handle,
  810. struct nvmefc_fcp_req *fcpreq)
  811. {
  812. struct fcloop_ini_fcpreq *inireq = fcpreq->private;
  813. struct fcloop_fcpreq *tfcp_req;
  814. bool abortio = true;
  815. unsigned long flags;
  816. spin_lock(&inireq->inilock);
  817. tfcp_req = inireq->tfcp_req;
  818. if (tfcp_req)
  819. fcloop_tfcp_req_get(tfcp_req);
  820. spin_unlock(&inireq->inilock);
  821. if (!tfcp_req)
  822. /* abort has already been called */
  823. return;
  824. /* break initiator/target relationship for io */
  825. spin_lock_irqsave(&tfcp_req->reqlock, flags);
  826. switch (tfcp_req->inistate) {
  827. case INI_IO_START:
  828. case INI_IO_ACTIVE:
  829. tfcp_req->inistate = INI_IO_ABORTED;
  830. break;
  831. case INI_IO_COMPLETED:
  832. abortio = false;
  833. break;
  834. default:
  835. spin_unlock_irqrestore(&tfcp_req->reqlock, flags);
  836. WARN_ON(1);
  837. return;
  838. }
  839. spin_unlock_irqrestore(&tfcp_req->reqlock, flags);
  840. if (abortio)
  841. /* leave the reference while the work item is scheduled */
  842. WARN_ON(!queue_work(nvmet_wq, &tfcp_req->abort_rcv_work));
  843. else {
  844. /*
  845. * as the io has already had the done callback made,
  846. * nothing more to do. So release the reference taken above
  847. */
  848. fcloop_tfcp_req_put(tfcp_req);
  849. }
  850. }
  851. static void
  852. fcloop_nport_free(struct kref *ref)
  853. {
  854. struct fcloop_nport *nport =
  855. container_of(ref, struct fcloop_nport, ref);
  856. kfree(nport);
  857. }
  858. static void
  859. fcloop_nport_put(struct fcloop_nport *nport)
  860. {
  861. kref_put(&nport->ref, fcloop_nport_free);
  862. }
  863. static int
  864. fcloop_nport_get(struct fcloop_nport *nport)
  865. {
  866. return kref_get_unless_zero(&nport->ref);
  867. }
  868. static void
  869. fcloop_localport_delete(struct nvme_fc_local_port *localport)
  870. {
  871. struct fcloop_lport_priv *lport_priv = localport->private;
  872. struct fcloop_lport *lport = lport_priv->lport;
  873. /* release any threads waiting for the unreg to complete */
  874. complete(&lport->unreg_done);
  875. }
  876. static void
  877. fcloop_remoteport_delete(struct nvme_fc_remote_port *remoteport)
  878. {
  879. struct fcloop_rport *rport = remoteport->private;
  880. flush_work(&rport->ls_work);
  881. fcloop_nport_put(rport->nport);
  882. }
  883. static void
  884. fcloop_targetport_delete(struct nvmet_fc_target_port *targetport)
  885. {
  886. struct fcloop_tport *tport = targetport->private;
  887. flush_work(&tport->ls_work);
  888. fcloop_nport_put(tport->nport);
  889. }
  890. #define FCLOOP_HW_QUEUES 4
  891. #define FCLOOP_SGL_SEGS 256
  892. #define FCLOOP_DMABOUND_4G 0xFFFFFFFF
  893. static struct nvme_fc_port_template fctemplate = {
  894. .localport_delete = fcloop_localport_delete,
  895. .remoteport_delete = fcloop_remoteport_delete,
  896. .create_queue = fcloop_create_queue,
  897. .delete_queue = fcloop_delete_queue,
  898. .ls_req = fcloop_h2t_ls_req,
  899. .fcp_io = fcloop_fcp_req,
  900. .ls_abort = fcloop_h2t_ls_abort,
  901. .fcp_abort = fcloop_fcp_abort,
  902. .xmt_ls_rsp = fcloop_t2h_xmt_ls_rsp,
  903. .max_hw_queues = FCLOOP_HW_QUEUES,
  904. .max_sgl_segments = FCLOOP_SGL_SEGS,
  905. .max_dif_sgl_segments = FCLOOP_SGL_SEGS,
  906. .dma_boundary = FCLOOP_DMABOUND_4G,
  907. /* sizes of additional private data for data structures */
  908. .local_priv_sz = sizeof(struct fcloop_lport_priv),
  909. .remote_priv_sz = sizeof(struct fcloop_rport),
  910. .lsrqst_priv_sz = sizeof(struct fcloop_lsreq),
  911. .fcprqst_priv_sz = sizeof(struct fcloop_ini_fcpreq),
  912. };
  913. static struct nvmet_fc_target_template tgttemplate = {
  914. .targetport_delete = fcloop_targetport_delete,
  915. .xmt_ls_rsp = fcloop_h2t_xmt_ls_rsp,
  916. .fcp_op = fcloop_fcp_op,
  917. .fcp_abort = fcloop_tgt_fcp_abort,
  918. .fcp_req_release = fcloop_fcp_req_release,
  919. .discovery_event = fcloop_tgt_discovery_evt,
  920. .ls_req = fcloop_t2h_ls_req,
  921. .ls_abort = fcloop_t2h_ls_abort,
  922. .host_release = fcloop_t2h_host_release,
  923. .host_traddr = fcloop_t2h_host_traddr,
  924. .max_hw_queues = FCLOOP_HW_QUEUES,
  925. .max_sgl_segments = FCLOOP_SGL_SEGS,
  926. .max_dif_sgl_segments = FCLOOP_SGL_SEGS,
  927. .dma_boundary = FCLOOP_DMABOUND_4G,
  928. /* optional features */
  929. .target_features = 0,
  930. /* sizes of additional private data for data structures */
  931. .target_priv_sz = sizeof(struct fcloop_tport),
  932. .lsrqst_priv_sz = sizeof(struct fcloop_lsreq),
  933. };
  934. static ssize_t
  935. fcloop_create_local_port(struct device *dev, struct device_attribute *attr,
  936. const char *buf, size_t count)
  937. {
  938. struct nvme_fc_port_info pinfo;
  939. struct fcloop_ctrl_options *opts;
  940. struct nvme_fc_local_port *localport;
  941. struct fcloop_lport *lport;
  942. struct fcloop_lport_priv *lport_priv;
  943. unsigned long flags;
  944. int ret = -ENOMEM;
  945. lport = kzalloc(sizeof(*lport), GFP_KERNEL);
  946. if (!lport)
  947. return -ENOMEM;
  948. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  949. if (!opts)
  950. goto out_free_lport;
  951. ret = fcloop_parse_options(opts, buf);
  952. if (ret)
  953. goto out_free_opts;
  954. /* everything there ? */
  955. if ((opts->mask & LPORT_OPTS) != LPORT_OPTS) {
  956. ret = -EINVAL;
  957. goto out_free_opts;
  958. }
  959. memset(&pinfo, 0, sizeof(pinfo));
  960. pinfo.node_name = opts->wwnn;
  961. pinfo.port_name = opts->wwpn;
  962. pinfo.port_role = opts->roles;
  963. pinfo.port_id = opts->fcaddr;
  964. ret = nvme_fc_register_localport(&pinfo, &fctemplate, NULL, &localport);
  965. if (!ret) {
  966. /* success */
  967. lport_priv = localport->private;
  968. lport_priv->lport = lport;
  969. lport->localport = localport;
  970. INIT_LIST_HEAD(&lport->lport_list);
  971. spin_lock_irqsave(&fcloop_lock, flags);
  972. list_add_tail(&lport->lport_list, &fcloop_lports);
  973. spin_unlock_irqrestore(&fcloop_lock, flags);
  974. }
  975. out_free_opts:
  976. kfree(opts);
  977. out_free_lport:
  978. /* free only if we're going to fail */
  979. if (ret)
  980. kfree(lport);
  981. return ret ? ret : count;
  982. }
  983. static void
  984. __unlink_local_port(struct fcloop_lport *lport)
  985. {
  986. list_del(&lport->lport_list);
  987. }
  988. static int
  989. __wait_localport_unreg(struct fcloop_lport *lport)
  990. {
  991. int ret;
  992. init_completion(&lport->unreg_done);
  993. ret = nvme_fc_unregister_localport(lport->localport);
  994. if (!ret)
  995. wait_for_completion(&lport->unreg_done);
  996. kfree(lport);
  997. return ret;
  998. }
  999. static ssize_t
  1000. fcloop_delete_local_port(struct device *dev, struct device_attribute *attr,
  1001. const char *buf, size_t count)
  1002. {
  1003. struct fcloop_lport *tlport, *lport = NULL;
  1004. u64 nodename, portname;
  1005. unsigned long flags;
  1006. int ret;
  1007. ret = fcloop_parse_nm_options(dev, &nodename, &portname, buf);
  1008. if (ret)
  1009. return ret;
  1010. spin_lock_irqsave(&fcloop_lock, flags);
  1011. list_for_each_entry(tlport, &fcloop_lports, lport_list) {
  1012. if (tlport->localport->node_name == nodename &&
  1013. tlport->localport->port_name == portname) {
  1014. lport = tlport;
  1015. __unlink_local_port(lport);
  1016. break;
  1017. }
  1018. }
  1019. spin_unlock_irqrestore(&fcloop_lock, flags);
  1020. if (!lport)
  1021. return -ENOENT;
  1022. ret = __wait_localport_unreg(lport);
  1023. return ret ? ret : count;
  1024. }
  1025. static struct fcloop_nport *
  1026. fcloop_alloc_nport(const char *buf, size_t count, bool remoteport)
  1027. {
  1028. struct fcloop_nport *newnport, *nport = NULL;
  1029. struct fcloop_lport *tmplport, *lport = NULL;
  1030. struct fcloop_ctrl_options *opts;
  1031. unsigned long flags;
  1032. u32 opts_mask = (remoteport) ? RPORT_OPTS : TGTPORT_OPTS;
  1033. int ret;
  1034. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  1035. if (!opts)
  1036. return NULL;
  1037. ret = fcloop_parse_options(opts, buf);
  1038. if (ret)
  1039. goto out_free_opts;
  1040. /* everything there ? */
  1041. if ((opts->mask & opts_mask) != opts_mask) {
  1042. ret = -EINVAL;
  1043. goto out_free_opts;
  1044. }
  1045. newnport = kzalloc(sizeof(*newnport), GFP_KERNEL);
  1046. if (!newnport)
  1047. goto out_free_opts;
  1048. INIT_LIST_HEAD(&newnport->nport_list);
  1049. newnport->node_name = opts->wwnn;
  1050. newnport->port_name = opts->wwpn;
  1051. if (opts->mask & NVMF_OPT_ROLES)
  1052. newnport->port_role = opts->roles;
  1053. if (opts->mask & NVMF_OPT_FCADDR)
  1054. newnport->port_id = opts->fcaddr;
  1055. kref_init(&newnport->ref);
  1056. spin_lock_irqsave(&fcloop_lock, flags);
  1057. list_for_each_entry(tmplport, &fcloop_lports, lport_list) {
  1058. if (tmplport->localport->node_name == opts->wwnn &&
  1059. tmplport->localport->port_name == opts->wwpn)
  1060. goto out_invalid_opts;
  1061. if (tmplport->localport->node_name == opts->lpwwnn &&
  1062. tmplport->localport->port_name == opts->lpwwpn)
  1063. lport = tmplport;
  1064. }
  1065. if (remoteport) {
  1066. if (!lport)
  1067. goto out_invalid_opts;
  1068. newnport->lport = lport;
  1069. }
  1070. list_for_each_entry(nport, &fcloop_nports, nport_list) {
  1071. if (nport->node_name == opts->wwnn &&
  1072. nport->port_name == opts->wwpn) {
  1073. if ((remoteport && nport->rport) ||
  1074. (!remoteport && nport->tport)) {
  1075. nport = NULL;
  1076. goto out_invalid_opts;
  1077. }
  1078. fcloop_nport_get(nport);
  1079. spin_unlock_irqrestore(&fcloop_lock, flags);
  1080. if (remoteport)
  1081. nport->lport = lport;
  1082. if (opts->mask & NVMF_OPT_ROLES)
  1083. nport->port_role = opts->roles;
  1084. if (opts->mask & NVMF_OPT_FCADDR)
  1085. nport->port_id = opts->fcaddr;
  1086. goto out_free_newnport;
  1087. }
  1088. }
  1089. list_add_tail(&newnport->nport_list, &fcloop_nports);
  1090. spin_unlock_irqrestore(&fcloop_lock, flags);
  1091. kfree(opts);
  1092. return newnport;
  1093. out_invalid_opts:
  1094. spin_unlock_irqrestore(&fcloop_lock, flags);
  1095. out_free_newnport:
  1096. kfree(newnport);
  1097. out_free_opts:
  1098. kfree(opts);
  1099. return nport;
  1100. }
  1101. static ssize_t
  1102. fcloop_create_remote_port(struct device *dev, struct device_attribute *attr,
  1103. const char *buf, size_t count)
  1104. {
  1105. struct nvme_fc_remote_port *remoteport;
  1106. struct fcloop_nport *nport;
  1107. struct fcloop_rport *rport;
  1108. struct nvme_fc_port_info pinfo;
  1109. int ret;
  1110. nport = fcloop_alloc_nport(buf, count, true);
  1111. if (!nport)
  1112. return -EIO;
  1113. memset(&pinfo, 0, sizeof(pinfo));
  1114. pinfo.node_name = nport->node_name;
  1115. pinfo.port_name = nport->port_name;
  1116. pinfo.port_role = nport->port_role;
  1117. pinfo.port_id = nport->port_id;
  1118. ret = nvme_fc_register_remoteport(nport->lport->localport,
  1119. &pinfo, &remoteport);
  1120. if (ret || !remoteport) {
  1121. fcloop_nport_put(nport);
  1122. return ret;
  1123. }
  1124. /* success */
  1125. rport = remoteport->private;
  1126. rport->remoteport = remoteport;
  1127. rport->targetport = (nport->tport) ? nport->tport->targetport : NULL;
  1128. if (nport->tport) {
  1129. nport->tport->remoteport = remoteport;
  1130. nport->tport->lport = nport->lport;
  1131. }
  1132. rport->nport = nport;
  1133. rport->lport = nport->lport;
  1134. nport->rport = rport;
  1135. spin_lock_init(&rport->lock);
  1136. INIT_WORK(&rport->ls_work, fcloop_rport_lsrqst_work);
  1137. INIT_LIST_HEAD(&rport->ls_list);
  1138. return count;
  1139. }
  1140. static struct fcloop_rport *
  1141. __unlink_remote_port(struct fcloop_nport *nport)
  1142. {
  1143. struct fcloop_rport *rport = nport->rport;
  1144. if (rport && nport->tport)
  1145. nport->tport->remoteport = NULL;
  1146. nport->rport = NULL;
  1147. list_del(&nport->nport_list);
  1148. return rport;
  1149. }
  1150. static int
  1151. __remoteport_unreg(struct fcloop_nport *nport, struct fcloop_rport *rport)
  1152. {
  1153. if (!rport)
  1154. return -EALREADY;
  1155. return nvme_fc_unregister_remoteport(rport->remoteport);
  1156. }
  1157. static ssize_t
  1158. fcloop_delete_remote_port(struct device *dev, struct device_attribute *attr,
  1159. const char *buf, size_t count)
  1160. {
  1161. struct fcloop_nport *nport = NULL, *tmpport;
  1162. static struct fcloop_rport *rport;
  1163. u64 nodename, portname;
  1164. unsigned long flags;
  1165. int ret;
  1166. ret = fcloop_parse_nm_options(dev, &nodename, &portname, buf);
  1167. if (ret)
  1168. return ret;
  1169. spin_lock_irqsave(&fcloop_lock, flags);
  1170. list_for_each_entry(tmpport, &fcloop_nports, nport_list) {
  1171. if (tmpport->node_name == nodename &&
  1172. tmpport->port_name == portname && tmpport->rport) {
  1173. nport = tmpport;
  1174. rport = __unlink_remote_port(nport);
  1175. break;
  1176. }
  1177. }
  1178. spin_unlock_irqrestore(&fcloop_lock, flags);
  1179. if (!nport)
  1180. return -ENOENT;
  1181. ret = __remoteport_unreg(nport, rport);
  1182. return ret ? ret : count;
  1183. }
  1184. static ssize_t
  1185. fcloop_create_target_port(struct device *dev, struct device_attribute *attr,
  1186. const char *buf, size_t count)
  1187. {
  1188. struct nvmet_fc_target_port *targetport;
  1189. struct fcloop_nport *nport;
  1190. struct fcloop_tport *tport;
  1191. struct nvmet_fc_port_info tinfo;
  1192. int ret;
  1193. nport = fcloop_alloc_nport(buf, count, false);
  1194. if (!nport)
  1195. return -EIO;
  1196. tinfo.node_name = nport->node_name;
  1197. tinfo.port_name = nport->port_name;
  1198. tinfo.port_id = nport->port_id;
  1199. ret = nvmet_fc_register_targetport(&tinfo, &tgttemplate, NULL,
  1200. &targetport);
  1201. if (ret) {
  1202. fcloop_nport_put(nport);
  1203. return ret;
  1204. }
  1205. /* success */
  1206. tport = targetport->private;
  1207. tport->targetport = targetport;
  1208. tport->remoteport = (nport->rport) ? nport->rport->remoteport : NULL;
  1209. if (nport->rport)
  1210. nport->rport->targetport = targetport;
  1211. tport->nport = nport;
  1212. tport->lport = nport->lport;
  1213. nport->tport = tport;
  1214. spin_lock_init(&tport->lock);
  1215. INIT_WORK(&tport->ls_work, fcloop_tport_lsrqst_work);
  1216. INIT_LIST_HEAD(&tport->ls_list);
  1217. return count;
  1218. }
  1219. static struct fcloop_tport *
  1220. __unlink_target_port(struct fcloop_nport *nport)
  1221. {
  1222. struct fcloop_tport *tport = nport->tport;
  1223. if (tport && nport->rport)
  1224. nport->rport->targetport = NULL;
  1225. nport->tport = NULL;
  1226. return tport;
  1227. }
  1228. static int
  1229. __targetport_unreg(struct fcloop_nport *nport, struct fcloop_tport *tport)
  1230. {
  1231. if (!tport)
  1232. return -EALREADY;
  1233. return nvmet_fc_unregister_targetport(tport->targetport);
  1234. }
  1235. static ssize_t
  1236. fcloop_delete_target_port(struct device *dev, struct device_attribute *attr,
  1237. const char *buf, size_t count)
  1238. {
  1239. struct fcloop_nport *nport = NULL, *tmpport;
  1240. struct fcloop_tport *tport = NULL;
  1241. u64 nodename, portname;
  1242. unsigned long flags;
  1243. int ret;
  1244. ret = fcloop_parse_nm_options(dev, &nodename, &portname, buf);
  1245. if (ret)
  1246. return ret;
  1247. spin_lock_irqsave(&fcloop_lock, flags);
  1248. list_for_each_entry(tmpport, &fcloop_nports, nport_list) {
  1249. if (tmpport->node_name == nodename &&
  1250. tmpport->port_name == portname && tmpport->tport) {
  1251. nport = tmpport;
  1252. tport = __unlink_target_port(nport);
  1253. break;
  1254. }
  1255. }
  1256. spin_unlock_irqrestore(&fcloop_lock, flags);
  1257. if (!nport)
  1258. return -ENOENT;
  1259. ret = __targetport_unreg(nport, tport);
  1260. return ret ? ret : count;
  1261. }
  1262. static ssize_t
  1263. fcloop_set_cmd_drop(struct device *dev, struct device_attribute *attr,
  1264. const char *buf, size_t count)
  1265. {
  1266. unsigned int opcode;
  1267. int starting, amount;
  1268. if (sscanf(buf, "%x:%d:%d", &opcode, &starting, &amount) != 3)
  1269. return -EBADRQC;
  1270. drop_current_cnt = 0;
  1271. drop_fabric_opcode = (opcode & ~DROP_OPCODE_MASK) ? true : false;
  1272. drop_opcode = (opcode & DROP_OPCODE_MASK);
  1273. drop_instance = starting;
  1274. /* the check to drop routine uses instance + count to know when
  1275. * to end. Thus, if dropping 1 instance, count should be 0.
  1276. * so subtract 1 from the count.
  1277. */
  1278. drop_amount = amount - 1;
  1279. pr_info("%s: DROP: Starting at instance %d of%s opcode x%x drop +%d "
  1280. "instances\n",
  1281. __func__, drop_instance, drop_fabric_opcode ? " fabric" : "",
  1282. drop_opcode, drop_amount);
  1283. return count;
  1284. }
  1285. static DEVICE_ATTR(add_local_port, 0200, NULL, fcloop_create_local_port);
  1286. static DEVICE_ATTR(del_local_port, 0200, NULL, fcloop_delete_local_port);
  1287. static DEVICE_ATTR(add_remote_port, 0200, NULL, fcloop_create_remote_port);
  1288. static DEVICE_ATTR(del_remote_port, 0200, NULL, fcloop_delete_remote_port);
  1289. static DEVICE_ATTR(add_target_port, 0200, NULL, fcloop_create_target_port);
  1290. static DEVICE_ATTR(del_target_port, 0200, NULL, fcloop_delete_target_port);
  1291. static DEVICE_ATTR(set_cmd_drop, 0200, NULL, fcloop_set_cmd_drop);
  1292. static struct attribute *fcloop_dev_attrs[] = {
  1293. &dev_attr_add_local_port.attr,
  1294. &dev_attr_del_local_port.attr,
  1295. &dev_attr_add_remote_port.attr,
  1296. &dev_attr_del_remote_port.attr,
  1297. &dev_attr_add_target_port.attr,
  1298. &dev_attr_del_target_port.attr,
  1299. &dev_attr_set_cmd_drop.attr,
  1300. NULL
  1301. };
  1302. static const struct attribute_group fclopp_dev_attrs_group = {
  1303. .attrs = fcloop_dev_attrs,
  1304. };
  1305. static const struct attribute_group *fcloop_dev_attr_groups[] = {
  1306. &fclopp_dev_attrs_group,
  1307. NULL,
  1308. };
  1309. static const struct class fcloop_class = {
  1310. .name = "fcloop",
  1311. };
  1312. static struct device *fcloop_device;
  1313. static int __init fcloop_init(void)
  1314. {
  1315. int ret;
  1316. ret = class_register(&fcloop_class);
  1317. if (ret) {
  1318. pr_err("couldn't register class fcloop\n");
  1319. return ret;
  1320. }
  1321. fcloop_device = device_create_with_groups(
  1322. &fcloop_class, NULL, MKDEV(0, 0), NULL,
  1323. fcloop_dev_attr_groups, "ctl");
  1324. if (IS_ERR(fcloop_device)) {
  1325. pr_err("couldn't create ctl device!\n");
  1326. ret = PTR_ERR(fcloop_device);
  1327. goto out_destroy_class;
  1328. }
  1329. get_device(fcloop_device);
  1330. return 0;
  1331. out_destroy_class:
  1332. class_unregister(&fcloop_class);
  1333. return ret;
  1334. }
  1335. static void __exit fcloop_exit(void)
  1336. {
  1337. struct fcloop_lport *lport = NULL;
  1338. struct fcloop_nport *nport = NULL;
  1339. struct fcloop_tport *tport;
  1340. struct fcloop_rport *rport;
  1341. unsigned long flags;
  1342. int ret;
  1343. spin_lock_irqsave(&fcloop_lock, flags);
  1344. for (;;) {
  1345. nport = list_first_entry_or_null(&fcloop_nports,
  1346. typeof(*nport), nport_list);
  1347. if (!nport)
  1348. break;
  1349. tport = __unlink_target_port(nport);
  1350. rport = __unlink_remote_port(nport);
  1351. spin_unlock_irqrestore(&fcloop_lock, flags);
  1352. ret = __targetport_unreg(nport, tport);
  1353. if (ret)
  1354. pr_warn("%s: Failed deleting target port\n", __func__);
  1355. ret = __remoteport_unreg(nport, rport);
  1356. if (ret)
  1357. pr_warn("%s: Failed deleting remote port\n", __func__);
  1358. spin_lock_irqsave(&fcloop_lock, flags);
  1359. }
  1360. for (;;) {
  1361. lport = list_first_entry_or_null(&fcloop_lports,
  1362. typeof(*lport), lport_list);
  1363. if (!lport)
  1364. break;
  1365. __unlink_local_port(lport);
  1366. spin_unlock_irqrestore(&fcloop_lock, flags);
  1367. ret = __wait_localport_unreg(lport);
  1368. if (ret)
  1369. pr_warn("%s: Failed deleting local port\n", __func__);
  1370. spin_lock_irqsave(&fcloop_lock, flags);
  1371. }
  1372. spin_unlock_irqrestore(&fcloop_lock, flags);
  1373. put_device(fcloop_device);
  1374. device_destroy(&fcloop_class, MKDEV(0, 0));
  1375. class_unregister(&fcloop_class);
  1376. }
  1377. module_init(fcloop_init);
  1378. module_exit(fcloop_exit);
  1379. MODULE_DESCRIPTION("NVMe target FC loop transport driver");
  1380. MODULE_LICENSE("GPL v2");