fcloop.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. /*
  2. * Copyright (c) 2016 Avago Technologies. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful.
  9. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
  10. * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
  11. * PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO
  12. * THE EXTENT THAT SUCH DISCLAIMERS ARE HELD TO BE LEGALLY INVALID.
  13. * See the GNU General Public License for more details, a copy of which
  14. * can be found in the file COPYING included with this package
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/module.h>
  18. #include <linux/parser.h>
  19. #include <uapi/scsi/fc/fc_fs.h>
  20. #include "../host/nvme.h"
  21. #include "../target/nvmet.h"
  22. #include <linux/nvme-fc-driver.h>
  23. #include <linux/nvme-fc.h>
  24. enum {
  25. NVMF_OPT_ERR = 0,
  26. NVMF_OPT_WWNN = 1 << 0,
  27. NVMF_OPT_WWPN = 1 << 1,
  28. NVMF_OPT_ROLES = 1 << 2,
  29. NVMF_OPT_FCADDR = 1 << 3,
  30. NVMF_OPT_LPWWNN = 1 << 4,
  31. NVMF_OPT_LPWWPN = 1 << 5,
  32. };
  33. struct fcloop_ctrl_options {
  34. int mask;
  35. u64 wwnn;
  36. u64 wwpn;
  37. u32 roles;
  38. u32 fcaddr;
  39. u64 lpwwnn;
  40. u64 lpwwpn;
  41. };
  42. static const match_table_t opt_tokens = {
  43. { NVMF_OPT_WWNN, "wwnn=%s" },
  44. { NVMF_OPT_WWPN, "wwpn=%s" },
  45. { NVMF_OPT_ROLES, "roles=%d" },
  46. { NVMF_OPT_FCADDR, "fcaddr=%x" },
  47. { NVMF_OPT_LPWWNN, "lpwwnn=%s" },
  48. { NVMF_OPT_LPWWPN, "lpwwpn=%s" },
  49. { NVMF_OPT_ERR, NULL }
  50. };
  51. static int
  52. fcloop_parse_options(struct fcloop_ctrl_options *opts,
  53. const char *buf)
  54. {
  55. substring_t args[MAX_OPT_ARGS];
  56. char *options, *o, *p;
  57. int token, ret = 0;
  58. u64 token64;
  59. options = o = kstrdup(buf, GFP_KERNEL);
  60. if (!options)
  61. return -ENOMEM;
  62. while ((p = strsep(&o, ",\n")) != NULL) {
  63. if (!*p)
  64. continue;
  65. token = match_token(p, opt_tokens, args);
  66. opts->mask |= token;
  67. switch (token) {
  68. case NVMF_OPT_WWNN:
  69. if (match_u64(args, &token64)) {
  70. ret = -EINVAL;
  71. goto out_free_options;
  72. }
  73. opts->wwnn = token64;
  74. break;
  75. case NVMF_OPT_WWPN:
  76. if (match_u64(args, &token64)) {
  77. ret = -EINVAL;
  78. goto out_free_options;
  79. }
  80. opts->wwpn = token64;
  81. break;
  82. case NVMF_OPT_ROLES:
  83. if (match_int(args, &token)) {
  84. ret = -EINVAL;
  85. goto out_free_options;
  86. }
  87. opts->roles = token;
  88. break;
  89. case NVMF_OPT_FCADDR:
  90. if (match_hex(args, &token)) {
  91. ret = -EINVAL;
  92. goto out_free_options;
  93. }
  94. opts->fcaddr = token;
  95. break;
  96. case NVMF_OPT_LPWWNN:
  97. if (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 (match_u64(args, &token64)) {
  105. ret = -EINVAL;
  106. goto out_free_options;
  107. }
  108. opts->lpwwpn = token64;
  109. break;
  110. default:
  111. pr_warn("unknown parameter or missing value '%s'\n", p);
  112. ret = -EINVAL;
  113. goto out_free_options;
  114. }
  115. }
  116. out_free_options:
  117. kfree(options);
  118. return ret;
  119. }
  120. static int
  121. fcloop_parse_nm_options(struct device *dev, u64 *nname, u64 *pname,
  122. const char *buf)
  123. {
  124. substring_t args[MAX_OPT_ARGS];
  125. char *options, *o, *p;
  126. int token, ret = 0;
  127. u64 token64;
  128. *nname = -1;
  129. *pname = -1;
  130. options = o = kstrdup(buf, GFP_KERNEL);
  131. if (!options)
  132. return -ENOMEM;
  133. while ((p = strsep(&o, ",\n")) != NULL) {
  134. if (!*p)
  135. continue;
  136. token = match_token(p, opt_tokens, args);
  137. switch (token) {
  138. case NVMF_OPT_WWNN:
  139. if (match_u64(args, &token64)) {
  140. ret = -EINVAL;
  141. goto out_free_options;
  142. }
  143. *nname = token64;
  144. break;
  145. case NVMF_OPT_WWPN:
  146. if (match_u64(args, &token64)) {
  147. ret = -EINVAL;
  148. goto out_free_options;
  149. }
  150. *pname = token64;
  151. break;
  152. default:
  153. pr_warn("unknown parameter or missing value '%s'\n", p);
  154. ret = -EINVAL;
  155. goto out_free_options;
  156. }
  157. }
  158. out_free_options:
  159. kfree(options);
  160. if (!ret) {
  161. if (*nname == -1)
  162. return -EINVAL;
  163. if (*pname == -1)
  164. return -EINVAL;
  165. }
  166. return ret;
  167. }
  168. #define LPORT_OPTS (NVMF_OPT_WWNN | NVMF_OPT_WWPN)
  169. #define RPORT_OPTS (NVMF_OPT_WWNN | NVMF_OPT_WWPN | \
  170. NVMF_OPT_LPWWNN | NVMF_OPT_LPWWPN)
  171. #define TGTPORT_OPTS (NVMF_OPT_WWNN | NVMF_OPT_WWPN)
  172. #define ALL_OPTS (NVMF_OPT_WWNN | NVMF_OPT_WWPN | NVMF_OPT_ROLES | \
  173. NVMF_OPT_FCADDR | NVMF_OPT_LPWWNN | NVMF_OPT_LPWWPN)
  174. static DEFINE_SPINLOCK(fcloop_lock);
  175. static LIST_HEAD(fcloop_lports);
  176. static LIST_HEAD(fcloop_nports);
  177. struct fcloop_lport {
  178. struct nvme_fc_local_port *localport;
  179. struct list_head lport_list;
  180. struct completion unreg_done;
  181. };
  182. struct fcloop_rport {
  183. struct nvme_fc_remote_port *remoteport;
  184. struct nvmet_fc_target_port *targetport;
  185. struct fcloop_nport *nport;
  186. struct fcloop_lport *lport;
  187. };
  188. struct fcloop_tport {
  189. struct nvmet_fc_target_port *targetport;
  190. struct nvme_fc_remote_port *remoteport;
  191. struct fcloop_nport *nport;
  192. struct fcloop_lport *lport;
  193. };
  194. struct fcloop_nport {
  195. struct fcloop_rport *rport;
  196. struct fcloop_tport *tport;
  197. struct fcloop_lport *lport;
  198. struct list_head nport_list;
  199. struct kref ref;
  200. struct completion rport_unreg_done;
  201. struct completion tport_unreg_done;
  202. u64 node_name;
  203. u64 port_name;
  204. u32 port_role;
  205. u32 port_id;
  206. };
  207. struct fcloop_lsreq {
  208. struct fcloop_tport *tport;
  209. struct nvmefc_ls_req *lsreq;
  210. struct work_struct work;
  211. struct nvmefc_tgt_ls_req tgt_ls_req;
  212. int status;
  213. };
  214. struct fcloop_fcpreq {
  215. struct fcloop_tport *tport;
  216. struct nvmefc_fcp_req *fcpreq;
  217. u16 status;
  218. struct work_struct work;
  219. struct nvmefc_tgt_fcp_req tgt_fcp_req;
  220. };
  221. static inline struct fcloop_lsreq *
  222. tgt_ls_req_to_lsreq(struct nvmefc_tgt_ls_req *tgt_lsreq)
  223. {
  224. return container_of(tgt_lsreq, struct fcloop_lsreq, tgt_ls_req);
  225. }
  226. static inline struct fcloop_fcpreq *
  227. tgt_fcp_req_to_fcpreq(struct nvmefc_tgt_fcp_req *tgt_fcpreq)
  228. {
  229. return container_of(tgt_fcpreq, struct fcloop_fcpreq, tgt_fcp_req);
  230. }
  231. static int
  232. fcloop_create_queue(struct nvme_fc_local_port *localport,
  233. unsigned int qidx, u16 qsize,
  234. void **handle)
  235. {
  236. *handle = localport;
  237. return 0;
  238. }
  239. static void
  240. fcloop_delete_queue(struct nvme_fc_local_port *localport,
  241. unsigned int idx, void *handle)
  242. {
  243. }
  244. /*
  245. * Transmit of LS RSP done (e.g. buffers all set). call back up
  246. * initiator "done" flows.
  247. */
  248. static void
  249. fcloop_tgt_lsrqst_done_work(struct work_struct *work)
  250. {
  251. struct fcloop_lsreq *tls_req =
  252. container_of(work, struct fcloop_lsreq, work);
  253. struct fcloop_tport *tport = tls_req->tport;
  254. struct nvmefc_ls_req *lsreq = tls_req->lsreq;
  255. if (tport->remoteport)
  256. lsreq->done(lsreq, tls_req->status);
  257. }
  258. static int
  259. fcloop_ls_req(struct nvme_fc_local_port *localport,
  260. struct nvme_fc_remote_port *remoteport,
  261. struct nvmefc_ls_req *lsreq)
  262. {
  263. struct fcloop_lsreq *tls_req = lsreq->private;
  264. struct fcloop_rport *rport = remoteport->private;
  265. int ret = 0;
  266. tls_req->lsreq = lsreq;
  267. INIT_WORK(&tls_req->work, fcloop_tgt_lsrqst_done_work);
  268. if (!rport->targetport) {
  269. tls_req->status = -ECONNREFUSED;
  270. schedule_work(&tls_req->work);
  271. return ret;
  272. }
  273. tls_req->status = 0;
  274. tls_req->tport = rport->targetport->private;
  275. ret = nvmet_fc_rcv_ls_req(rport->targetport, &tls_req->tgt_ls_req,
  276. lsreq->rqstaddr, lsreq->rqstlen);
  277. return ret;
  278. }
  279. static int
  280. fcloop_xmt_ls_rsp(struct nvmet_fc_target_port *tport,
  281. struct nvmefc_tgt_ls_req *tgt_lsreq)
  282. {
  283. struct fcloop_lsreq *tls_req = tgt_ls_req_to_lsreq(tgt_lsreq);
  284. struct nvmefc_ls_req *lsreq = tls_req->lsreq;
  285. memcpy(lsreq->rspaddr, tgt_lsreq->rspbuf,
  286. ((lsreq->rsplen < tgt_lsreq->rsplen) ?
  287. lsreq->rsplen : tgt_lsreq->rsplen));
  288. tgt_lsreq->done(tgt_lsreq);
  289. schedule_work(&tls_req->work);
  290. return 0;
  291. }
  292. /*
  293. * FCP IO operation done. call back up initiator "done" flows.
  294. */
  295. static void
  296. fcloop_tgt_fcprqst_done_work(struct work_struct *work)
  297. {
  298. struct fcloop_fcpreq *tfcp_req =
  299. container_of(work, struct fcloop_fcpreq, work);
  300. struct fcloop_tport *tport = tfcp_req->tport;
  301. struct nvmefc_fcp_req *fcpreq = tfcp_req->fcpreq;
  302. if (tport->remoteport) {
  303. fcpreq->status = tfcp_req->status;
  304. fcpreq->done(fcpreq);
  305. }
  306. }
  307. static int
  308. fcloop_fcp_req(struct nvme_fc_local_port *localport,
  309. struct nvme_fc_remote_port *remoteport,
  310. void *hw_queue_handle,
  311. struct nvmefc_fcp_req *fcpreq)
  312. {
  313. struct fcloop_fcpreq *tfcp_req = fcpreq->private;
  314. struct fcloop_rport *rport = remoteport->private;
  315. int ret = 0;
  316. INIT_WORK(&tfcp_req->work, fcloop_tgt_fcprqst_done_work);
  317. if (!rport->targetport) {
  318. tfcp_req->status = NVME_SC_FC_TRANSPORT_ERROR;
  319. schedule_work(&tfcp_req->work);
  320. return ret;
  321. }
  322. tfcp_req->fcpreq = fcpreq;
  323. tfcp_req->tport = rport->targetport->private;
  324. ret = nvmet_fc_rcv_fcp_req(rport->targetport, &tfcp_req->tgt_fcp_req,
  325. fcpreq->cmdaddr, fcpreq->cmdlen);
  326. return ret;
  327. }
  328. static void
  329. fcloop_fcp_copy_data(u8 op, struct scatterlist *data_sg,
  330. struct scatterlist *io_sg, u32 offset, u32 length)
  331. {
  332. void *data_p, *io_p;
  333. u32 data_len, io_len, tlen;
  334. io_p = sg_virt(io_sg);
  335. io_len = io_sg->length;
  336. for ( ; offset; ) {
  337. tlen = min_t(u32, offset, io_len);
  338. offset -= tlen;
  339. io_len -= tlen;
  340. if (!io_len) {
  341. io_sg = sg_next(io_sg);
  342. io_p = sg_virt(io_sg);
  343. io_len = io_sg->length;
  344. } else
  345. io_p += tlen;
  346. }
  347. data_p = sg_virt(data_sg);
  348. data_len = data_sg->length;
  349. for ( ; length; ) {
  350. tlen = min_t(u32, io_len, data_len);
  351. tlen = min_t(u32, tlen, length);
  352. if (op == NVMET_FCOP_WRITEDATA)
  353. memcpy(data_p, io_p, tlen);
  354. else
  355. memcpy(io_p, data_p, tlen);
  356. length -= tlen;
  357. io_len -= tlen;
  358. if ((!io_len) && (length)) {
  359. io_sg = sg_next(io_sg);
  360. io_p = sg_virt(io_sg);
  361. io_len = io_sg->length;
  362. } else
  363. io_p += tlen;
  364. data_len -= tlen;
  365. if ((!data_len) && (length)) {
  366. data_sg = sg_next(data_sg);
  367. data_p = sg_virt(data_sg);
  368. data_len = data_sg->length;
  369. } else
  370. data_p += tlen;
  371. }
  372. }
  373. static int
  374. fcloop_fcp_op(struct nvmet_fc_target_port *tgtport,
  375. struct nvmefc_tgt_fcp_req *tgt_fcpreq)
  376. {
  377. struct fcloop_fcpreq *tfcp_req = tgt_fcp_req_to_fcpreq(tgt_fcpreq);
  378. struct nvmefc_fcp_req *fcpreq = tfcp_req->fcpreq;
  379. u32 rsplen = 0, xfrlen = 0;
  380. int fcp_err = 0;
  381. u8 op = tgt_fcpreq->op;
  382. switch (op) {
  383. case NVMET_FCOP_WRITEDATA:
  384. xfrlen = tgt_fcpreq->transfer_length;
  385. fcloop_fcp_copy_data(op, tgt_fcpreq->sg, fcpreq->first_sgl,
  386. tgt_fcpreq->offset, xfrlen);
  387. fcpreq->transferred_length += xfrlen;
  388. break;
  389. case NVMET_FCOP_READDATA:
  390. case NVMET_FCOP_READDATA_RSP:
  391. xfrlen = tgt_fcpreq->transfer_length;
  392. fcloop_fcp_copy_data(op, tgt_fcpreq->sg, fcpreq->first_sgl,
  393. tgt_fcpreq->offset, xfrlen);
  394. fcpreq->transferred_length += xfrlen;
  395. if (op == NVMET_FCOP_READDATA)
  396. break;
  397. /* Fall-Thru to RSP handling */
  398. case NVMET_FCOP_RSP:
  399. rsplen = ((fcpreq->rsplen < tgt_fcpreq->rsplen) ?
  400. fcpreq->rsplen : tgt_fcpreq->rsplen);
  401. memcpy(fcpreq->rspaddr, tgt_fcpreq->rspaddr, rsplen);
  402. if (rsplen < tgt_fcpreq->rsplen)
  403. fcp_err = -E2BIG;
  404. fcpreq->rcv_rsplen = rsplen;
  405. fcpreq->status = 0;
  406. tfcp_req->status = 0;
  407. break;
  408. case NVMET_FCOP_ABORT:
  409. tfcp_req->status = NVME_SC_FC_TRANSPORT_ABORTED;
  410. break;
  411. default:
  412. fcp_err = -EINVAL;
  413. break;
  414. }
  415. tgt_fcpreq->transferred_length = xfrlen;
  416. tgt_fcpreq->fcp_error = fcp_err;
  417. tgt_fcpreq->done(tgt_fcpreq);
  418. if ((!fcp_err) && (op == NVMET_FCOP_RSP ||
  419. op == NVMET_FCOP_READDATA_RSP ||
  420. op == NVMET_FCOP_ABORT))
  421. schedule_work(&tfcp_req->work);
  422. return 0;
  423. }
  424. static void
  425. fcloop_ls_abort(struct nvme_fc_local_port *localport,
  426. struct nvme_fc_remote_port *remoteport,
  427. struct nvmefc_ls_req *lsreq)
  428. {
  429. }
  430. static void
  431. fcloop_fcp_abort(struct nvme_fc_local_port *localport,
  432. struct nvme_fc_remote_port *remoteport,
  433. void *hw_queue_handle,
  434. struct nvmefc_fcp_req *fcpreq)
  435. {
  436. }
  437. static void
  438. fcloop_localport_delete(struct nvme_fc_local_port *localport)
  439. {
  440. struct fcloop_lport *lport = localport->private;
  441. /* release any threads waiting for the unreg to complete */
  442. complete(&lport->unreg_done);
  443. }
  444. static void
  445. fcloop_remoteport_delete(struct nvme_fc_remote_port *remoteport)
  446. {
  447. struct fcloop_rport *rport = remoteport->private;
  448. /* release any threads waiting for the unreg to complete */
  449. complete(&rport->nport->rport_unreg_done);
  450. }
  451. static void
  452. fcloop_targetport_delete(struct nvmet_fc_target_port *targetport)
  453. {
  454. struct fcloop_tport *tport = targetport->private;
  455. /* release any threads waiting for the unreg to complete */
  456. complete(&tport->nport->tport_unreg_done);
  457. }
  458. #define FCLOOP_HW_QUEUES 4
  459. #define FCLOOP_SGL_SEGS 256
  460. #define FCLOOP_DMABOUND_4G 0xFFFFFFFF
  461. struct nvme_fc_port_template fctemplate = {
  462. .localport_delete = fcloop_localport_delete,
  463. .remoteport_delete = fcloop_remoteport_delete,
  464. .create_queue = fcloop_create_queue,
  465. .delete_queue = fcloop_delete_queue,
  466. .ls_req = fcloop_ls_req,
  467. .fcp_io = fcloop_fcp_req,
  468. .ls_abort = fcloop_ls_abort,
  469. .fcp_abort = fcloop_fcp_abort,
  470. .max_hw_queues = FCLOOP_HW_QUEUES,
  471. .max_sgl_segments = FCLOOP_SGL_SEGS,
  472. .max_dif_sgl_segments = FCLOOP_SGL_SEGS,
  473. .dma_boundary = FCLOOP_DMABOUND_4G,
  474. /* sizes of additional private data for data structures */
  475. .local_priv_sz = sizeof(struct fcloop_lport),
  476. .remote_priv_sz = sizeof(struct fcloop_rport),
  477. .lsrqst_priv_sz = sizeof(struct fcloop_lsreq),
  478. .fcprqst_priv_sz = sizeof(struct fcloop_fcpreq),
  479. };
  480. struct nvmet_fc_target_template tgttemplate = {
  481. .targetport_delete = fcloop_targetport_delete,
  482. .xmt_ls_rsp = fcloop_xmt_ls_rsp,
  483. .fcp_op = fcloop_fcp_op,
  484. .max_hw_queues = FCLOOP_HW_QUEUES,
  485. .max_sgl_segments = FCLOOP_SGL_SEGS,
  486. .max_dif_sgl_segments = FCLOOP_SGL_SEGS,
  487. .dma_boundary = FCLOOP_DMABOUND_4G,
  488. /* optional features */
  489. .target_features = NVMET_FCTGTFEAT_READDATA_RSP |
  490. NVMET_FCTGTFEAT_NEEDS_CMD_CPUSCHED,
  491. /* sizes of additional private data for data structures */
  492. .target_priv_sz = sizeof(struct fcloop_tport),
  493. };
  494. static ssize_t
  495. fcloop_create_local_port(struct device *dev, struct device_attribute *attr,
  496. const char *buf, size_t count)
  497. {
  498. struct nvme_fc_port_info pinfo;
  499. struct fcloop_ctrl_options *opts;
  500. struct nvme_fc_local_port *localport;
  501. struct fcloop_lport *lport;
  502. int ret;
  503. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  504. if (!opts)
  505. return -ENOMEM;
  506. ret = fcloop_parse_options(opts, buf);
  507. if (ret)
  508. goto out_free_opts;
  509. /* everything there ? */
  510. if ((opts->mask & LPORT_OPTS) != LPORT_OPTS) {
  511. ret = -EINVAL;
  512. goto out_free_opts;
  513. }
  514. pinfo.node_name = opts->wwnn;
  515. pinfo.port_name = opts->wwpn;
  516. pinfo.port_role = opts->roles;
  517. pinfo.port_id = opts->fcaddr;
  518. ret = nvme_fc_register_localport(&pinfo, &fctemplate, NULL, &localport);
  519. if (!ret) {
  520. unsigned long flags;
  521. /* success */
  522. lport = localport->private;
  523. lport->localport = localport;
  524. INIT_LIST_HEAD(&lport->lport_list);
  525. spin_lock_irqsave(&fcloop_lock, flags);
  526. list_add_tail(&lport->lport_list, &fcloop_lports);
  527. spin_unlock_irqrestore(&fcloop_lock, flags);
  528. /* mark all of the input buffer consumed */
  529. ret = count;
  530. }
  531. out_free_opts:
  532. kfree(opts);
  533. return ret ? ret : count;
  534. }
  535. static void
  536. __unlink_local_port(struct fcloop_lport *lport)
  537. {
  538. list_del(&lport->lport_list);
  539. }
  540. static int
  541. __wait_localport_unreg(struct fcloop_lport *lport)
  542. {
  543. int ret;
  544. init_completion(&lport->unreg_done);
  545. ret = nvme_fc_unregister_localport(lport->localport);
  546. wait_for_completion(&lport->unreg_done);
  547. return ret;
  548. }
  549. static ssize_t
  550. fcloop_delete_local_port(struct device *dev, struct device_attribute *attr,
  551. const char *buf, size_t count)
  552. {
  553. struct fcloop_lport *tlport, *lport = NULL;
  554. u64 nodename, portname;
  555. unsigned long flags;
  556. int ret;
  557. ret = fcloop_parse_nm_options(dev, &nodename, &portname, buf);
  558. if (ret)
  559. return ret;
  560. spin_lock_irqsave(&fcloop_lock, flags);
  561. list_for_each_entry(tlport, &fcloop_lports, lport_list) {
  562. if (tlport->localport->node_name == nodename &&
  563. tlport->localport->port_name == portname) {
  564. lport = tlport;
  565. __unlink_local_port(lport);
  566. break;
  567. }
  568. }
  569. spin_unlock_irqrestore(&fcloop_lock, flags);
  570. if (!lport)
  571. return -ENOENT;
  572. ret = __wait_localport_unreg(lport);
  573. return ret ? ret : count;
  574. }
  575. static void
  576. fcloop_nport_free(struct kref *ref)
  577. {
  578. struct fcloop_nport *nport =
  579. container_of(ref, struct fcloop_nport, ref);
  580. unsigned long flags;
  581. spin_lock_irqsave(&fcloop_lock, flags);
  582. list_del(&nport->nport_list);
  583. spin_unlock_irqrestore(&fcloop_lock, flags);
  584. kfree(nport);
  585. }
  586. static void
  587. fcloop_nport_put(struct fcloop_nport *nport)
  588. {
  589. kref_put(&nport->ref, fcloop_nport_free);
  590. }
  591. static int
  592. fcloop_nport_get(struct fcloop_nport *nport)
  593. {
  594. return kref_get_unless_zero(&nport->ref);
  595. }
  596. static struct fcloop_nport *
  597. fcloop_alloc_nport(const char *buf, size_t count, bool remoteport)
  598. {
  599. struct fcloop_nport *newnport, *nport = NULL;
  600. struct fcloop_lport *tmplport, *lport = NULL;
  601. struct fcloop_ctrl_options *opts;
  602. unsigned long flags;
  603. u32 opts_mask = (remoteport) ? RPORT_OPTS : TGTPORT_OPTS;
  604. int ret;
  605. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  606. if (!opts)
  607. return NULL;
  608. ret = fcloop_parse_options(opts, buf);
  609. if (ret)
  610. goto out_free_opts;
  611. /* everything there ? */
  612. if ((opts->mask & opts_mask) != opts_mask) {
  613. ret = -EINVAL;
  614. goto out_free_opts;
  615. }
  616. newnport = kzalloc(sizeof(*newnport), GFP_KERNEL);
  617. if (!newnport)
  618. goto out_free_opts;
  619. INIT_LIST_HEAD(&newnport->nport_list);
  620. newnport->node_name = opts->wwnn;
  621. newnport->port_name = opts->wwpn;
  622. if (opts->mask & NVMF_OPT_ROLES)
  623. newnport->port_role = opts->roles;
  624. if (opts->mask & NVMF_OPT_FCADDR)
  625. newnport->port_id = opts->fcaddr;
  626. kref_init(&newnport->ref);
  627. spin_lock_irqsave(&fcloop_lock, flags);
  628. list_for_each_entry(tmplport, &fcloop_lports, lport_list) {
  629. if (tmplport->localport->node_name == opts->wwnn &&
  630. tmplport->localport->port_name == opts->wwpn)
  631. goto out_invalid_opts;
  632. if (tmplport->localport->node_name == opts->lpwwnn &&
  633. tmplport->localport->port_name == opts->lpwwpn)
  634. lport = tmplport;
  635. }
  636. if (remoteport) {
  637. if (!lport)
  638. goto out_invalid_opts;
  639. newnport->lport = lport;
  640. }
  641. list_for_each_entry(nport, &fcloop_nports, nport_list) {
  642. if (nport->node_name == opts->wwnn &&
  643. nport->port_name == opts->wwpn) {
  644. if ((remoteport && nport->rport) ||
  645. (!remoteport && nport->tport)) {
  646. nport = NULL;
  647. goto out_invalid_opts;
  648. }
  649. fcloop_nport_get(nport);
  650. spin_unlock_irqrestore(&fcloop_lock, flags);
  651. if (remoteport)
  652. nport->lport = lport;
  653. if (opts->mask & NVMF_OPT_ROLES)
  654. nport->port_role = opts->roles;
  655. if (opts->mask & NVMF_OPT_FCADDR)
  656. nport->port_id = opts->fcaddr;
  657. goto out_free_newnport;
  658. }
  659. }
  660. list_add_tail(&newnport->nport_list, &fcloop_nports);
  661. spin_unlock_irqrestore(&fcloop_lock, flags);
  662. kfree(opts);
  663. return newnport;
  664. out_invalid_opts:
  665. spin_unlock_irqrestore(&fcloop_lock, flags);
  666. out_free_newnport:
  667. kfree(newnport);
  668. out_free_opts:
  669. kfree(opts);
  670. return nport;
  671. }
  672. static ssize_t
  673. fcloop_create_remote_port(struct device *dev, struct device_attribute *attr,
  674. const char *buf, size_t count)
  675. {
  676. struct nvme_fc_remote_port *remoteport;
  677. struct fcloop_nport *nport;
  678. struct fcloop_rport *rport;
  679. struct nvme_fc_port_info pinfo;
  680. int ret;
  681. nport = fcloop_alloc_nport(buf, count, true);
  682. if (!nport)
  683. return -EIO;
  684. pinfo.node_name = nport->node_name;
  685. pinfo.port_name = nport->port_name;
  686. pinfo.port_role = nport->port_role;
  687. pinfo.port_id = nport->port_id;
  688. ret = nvme_fc_register_remoteport(nport->lport->localport,
  689. &pinfo, &remoteport);
  690. if (ret || !remoteport) {
  691. fcloop_nport_put(nport);
  692. return ret;
  693. }
  694. /* success */
  695. rport = remoteport->private;
  696. rport->remoteport = remoteport;
  697. rport->targetport = (nport->tport) ? nport->tport->targetport : NULL;
  698. if (nport->tport) {
  699. nport->tport->remoteport = remoteport;
  700. nport->tport->lport = nport->lport;
  701. }
  702. rport->nport = nport;
  703. rport->lport = nport->lport;
  704. nport->rport = rport;
  705. return count;
  706. }
  707. static struct fcloop_rport *
  708. __unlink_remote_port(struct fcloop_nport *nport)
  709. {
  710. struct fcloop_rport *rport = nport->rport;
  711. if (rport && nport->tport)
  712. nport->tport->remoteport = NULL;
  713. nport->rport = NULL;
  714. return rport;
  715. }
  716. static int
  717. __wait_remoteport_unreg(struct fcloop_nport *nport, struct fcloop_rport *rport)
  718. {
  719. int ret;
  720. if (!rport)
  721. return -EALREADY;
  722. init_completion(&nport->rport_unreg_done);
  723. ret = nvme_fc_unregister_remoteport(rport->remoteport);
  724. if (ret)
  725. return ret;
  726. wait_for_completion(&nport->rport_unreg_done);
  727. fcloop_nport_put(nport);
  728. return ret;
  729. }
  730. static ssize_t
  731. fcloop_delete_remote_port(struct device *dev, struct device_attribute *attr,
  732. const char *buf, size_t count)
  733. {
  734. struct fcloop_nport *nport = NULL, *tmpport;
  735. static struct fcloop_rport *rport;
  736. u64 nodename, portname;
  737. unsigned long flags;
  738. int ret;
  739. ret = fcloop_parse_nm_options(dev, &nodename, &portname, buf);
  740. if (ret)
  741. return ret;
  742. spin_lock_irqsave(&fcloop_lock, flags);
  743. list_for_each_entry(tmpport, &fcloop_nports, nport_list) {
  744. if (tmpport->node_name == nodename &&
  745. tmpport->port_name == portname && tmpport->rport) {
  746. nport = tmpport;
  747. rport = __unlink_remote_port(nport);
  748. break;
  749. }
  750. }
  751. spin_unlock_irqrestore(&fcloop_lock, flags);
  752. if (!nport)
  753. return -ENOENT;
  754. ret = __wait_remoteport_unreg(nport, rport);
  755. return ret ? ret : count;
  756. }
  757. static ssize_t
  758. fcloop_create_target_port(struct device *dev, struct device_attribute *attr,
  759. const char *buf, size_t count)
  760. {
  761. struct nvmet_fc_target_port *targetport;
  762. struct fcloop_nport *nport;
  763. struct fcloop_tport *tport;
  764. struct nvmet_fc_port_info tinfo;
  765. int ret;
  766. nport = fcloop_alloc_nport(buf, count, false);
  767. if (!nport)
  768. return -EIO;
  769. tinfo.node_name = nport->node_name;
  770. tinfo.port_name = nport->port_name;
  771. tinfo.port_id = nport->port_id;
  772. ret = nvmet_fc_register_targetport(&tinfo, &tgttemplate, NULL,
  773. &targetport);
  774. if (ret) {
  775. fcloop_nport_put(nport);
  776. return ret;
  777. }
  778. /* success */
  779. tport = targetport->private;
  780. tport->targetport = targetport;
  781. tport->remoteport = (nport->rport) ? nport->rport->remoteport : NULL;
  782. if (nport->rport)
  783. nport->rport->targetport = targetport;
  784. tport->nport = nport;
  785. tport->lport = nport->lport;
  786. nport->tport = tport;
  787. return count;
  788. }
  789. static struct fcloop_tport *
  790. __unlink_target_port(struct fcloop_nport *nport)
  791. {
  792. struct fcloop_tport *tport = nport->tport;
  793. if (tport && nport->rport)
  794. nport->rport->targetport = NULL;
  795. nport->tport = NULL;
  796. return tport;
  797. }
  798. static int
  799. __wait_targetport_unreg(struct fcloop_nport *nport, struct fcloop_tport *tport)
  800. {
  801. int ret;
  802. if (!tport)
  803. return -EALREADY;
  804. init_completion(&nport->tport_unreg_done);
  805. ret = nvmet_fc_unregister_targetport(tport->targetport);
  806. if (ret)
  807. return ret;
  808. wait_for_completion(&nport->tport_unreg_done);
  809. fcloop_nport_put(nport);
  810. return ret;
  811. }
  812. static ssize_t
  813. fcloop_delete_target_port(struct device *dev, struct device_attribute *attr,
  814. const char *buf, size_t count)
  815. {
  816. struct fcloop_nport *nport = NULL, *tmpport;
  817. struct fcloop_tport *tport;
  818. u64 nodename, portname;
  819. unsigned long flags;
  820. int ret;
  821. ret = fcloop_parse_nm_options(dev, &nodename, &portname, buf);
  822. if (ret)
  823. return ret;
  824. spin_lock_irqsave(&fcloop_lock, flags);
  825. list_for_each_entry(tmpport, &fcloop_nports, nport_list) {
  826. if (tmpport->node_name == nodename &&
  827. tmpport->port_name == portname && tmpport->tport) {
  828. nport = tmpport;
  829. tport = __unlink_target_port(nport);
  830. break;
  831. }
  832. }
  833. spin_unlock_irqrestore(&fcloop_lock, flags);
  834. if (!nport)
  835. return -ENOENT;
  836. ret = __wait_targetport_unreg(nport, tport);
  837. return ret ? ret : count;
  838. }
  839. static DEVICE_ATTR(add_local_port, 0200, NULL, fcloop_create_local_port);
  840. static DEVICE_ATTR(del_local_port, 0200, NULL, fcloop_delete_local_port);
  841. static DEVICE_ATTR(add_remote_port, 0200, NULL, fcloop_create_remote_port);
  842. static DEVICE_ATTR(del_remote_port, 0200, NULL, fcloop_delete_remote_port);
  843. static DEVICE_ATTR(add_target_port, 0200, NULL, fcloop_create_target_port);
  844. static DEVICE_ATTR(del_target_port, 0200, NULL, fcloop_delete_target_port);
  845. static struct attribute *fcloop_dev_attrs[] = {
  846. &dev_attr_add_local_port.attr,
  847. &dev_attr_del_local_port.attr,
  848. &dev_attr_add_remote_port.attr,
  849. &dev_attr_del_remote_port.attr,
  850. &dev_attr_add_target_port.attr,
  851. &dev_attr_del_target_port.attr,
  852. NULL
  853. };
  854. static struct attribute_group fclopp_dev_attrs_group = {
  855. .attrs = fcloop_dev_attrs,
  856. };
  857. static const struct attribute_group *fcloop_dev_attr_groups[] = {
  858. &fclopp_dev_attrs_group,
  859. NULL,
  860. };
  861. static struct class *fcloop_class;
  862. static struct device *fcloop_device;
  863. static int __init fcloop_init(void)
  864. {
  865. int ret;
  866. fcloop_class = class_create(THIS_MODULE, "fcloop");
  867. if (IS_ERR(fcloop_class)) {
  868. pr_err("couldn't register class fcloop\n");
  869. ret = PTR_ERR(fcloop_class);
  870. return ret;
  871. }
  872. fcloop_device = device_create_with_groups(
  873. fcloop_class, NULL, MKDEV(0, 0), NULL,
  874. fcloop_dev_attr_groups, "ctl");
  875. if (IS_ERR(fcloop_device)) {
  876. pr_err("couldn't create ctl device!\n");
  877. ret = PTR_ERR(fcloop_device);
  878. goto out_destroy_class;
  879. }
  880. get_device(fcloop_device);
  881. return 0;
  882. out_destroy_class:
  883. class_destroy(fcloop_class);
  884. return ret;
  885. }
  886. static void __exit fcloop_exit(void)
  887. {
  888. struct fcloop_lport *lport;
  889. struct fcloop_nport *nport;
  890. struct fcloop_tport *tport;
  891. struct fcloop_rport *rport;
  892. unsigned long flags;
  893. int ret;
  894. spin_lock_irqsave(&fcloop_lock, flags);
  895. for (;;) {
  896. nport = list_first_entry_or_null(&fcloop_nports,
  897. typeof(*nport), nport_list);
  898. if (!nport)
  899. break;
  900. tport = __unlink_target_port(nport);
  901. rport = __unlink_remote_port(nport);
  902. spin_unlock_irqrestore(&fcloop_lock, flags);
  903. ret = __wait_targetport_unreg(nport, tport);
  904. if (ret)
  905. pr_warn("%s: Failed deleting target port\n", __func__);
  906. ret = __wait_remoteport_unreg(nport, rport);
  907. if (ret)
  908. pr_warn("%s: Failed deleting remote port\n", __func__);
  909. spin_lock_irqsave(&fcloop_lock, flags);
  910. }
  911. for (;;) {
  912. lport = list_first_entry_or_null(&fcloop_lports,
  913. typeof(*lport), lport_list);
  914. if (!lport)
  915. break;
  916. __unlink_local_port(lport);
  917. spin_unlock_irqrestore(&fcloop_lock, flags);
  918. ret = __wait_localport_unreg(lport);
  919. if (ret)
  920. pr_warn("%s: Failed deleting local port\n", __func__);
  921. spin_lock_irqsave(&fcloop_lock, flags);
  922. }
  923. spin_unlock_irqrestore(&fcloop_lock, flags);
  924. put_device(fcloop_device);
  925. device_destroy(fcloop_class, MKDEV(0, 0));
  926. class_destroy(fcloop_class);
  927. }
  928. module_init(fcloop_init);
  929. module_exit(fcloop_exit);
  930. MODULE_LICENSE("GPL v2");