fc.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586
  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. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/module.h>
  19. #include <linux/parser.h>
  20. #include <uapi/scsi/fc/fc_fs.h>
  21. #include <uapi/scsi/fc/fc_els.h>
  22. #include "nvme.h"
  23. #include "fabrics.h"
  24. #include <linux/nvme-fc-driver.h>
  25. #include <linux/nvme-fc.h>
  26. /* *************************** Data Structures/Defines ****************** */
  27. /*
  28. * We handle AEN commands ourselves and don't even let the
  29. * block layer know about them.
  30. */
  31. #define NVME_FC_NR_AEN_COMMANDS 1
  32. #define NVME_FC_AQ_BLKMQ_DEPTH \
  33. (NVMF_AQ_DEPTH - NVME_FC_NR_AEN_COMMANDS)
  34. #define AEN_CMDID_BASE (NVME_FC_AQ_BLKMQ_DEPTH + 1)
  35. enum nvme_fc_queue_flags {
  36. NVME_FC_Q_CONNECTED = (1 << 0),
  37. };
  38. #define NVMEFC_QUEUE_DELAY 3 /* ms units */
  39. struct nvme_fc_queue {
  40. struct nvme_fc_ctrl *ctrl;
  41. struct device *dev;
  42. struct blk_mq_hw_ctx *hctx;
  43. void *lldd_handle;
  44. int queue_size;
  45. size_t cmnd_capsule_len;
  46. u32 qnum;
  47. u32 rqcnt;
  48. u32 seqno;
  49. u64 connection_id;
  50. atomic_t csn;
  51. unsigned long flags;
  52. } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
  53. struct nvmefc_ls_req_op {
  54. struct nvmefc_ls_req ls_req;
  55. struct nvme_fc_ctrl *ctrl;
  56. struct nvme_fc_queue *queue;
  57. struct request *rq;
  58. int ls_error;
  59. struct completion ls_done;
  60. struct list_head lsreq_list; /* ctrl->ls_req_list */
  61. bool req_queued;
  62. };
  63. enum nvme_fcpop_state {
  64. FCPOP_STATE_UNINIT = 0,
  65. FCPOP_STATE_IDLE = 1,
  66. FCPOP_STATE_ACTIVE = 2,
  67. FCPOP_STATE_ABORTED = 3,
  68. };
  69. struct nvme_fc_fcp_op {
  70. struct nvme_request nreq; /*
  71. * nvme/host/core.c
  72. * requires this to be
  73. * the 1st element in the
  74. * private structure
  75. * associated with the
  76. * request.
  77. */
  78. struct nvmefc_fcp_req fcp_req;
  79. struct nvme_fc_ctrl *ctrl;
  80. struct nvme_fc_queue *queue;
  81. struct request *rq;
  82. atomic_t state;
  83. u32 rqno;
  84. u32 nents;
  85. struct nvme_fc_cmd_iu cmd_iu;
  86. struct nvme_fc_ersp_iu rsp_iu;
  87. };
  88. struct nvme_fc_lport {
  89. struct nvme_fc_local_port localport;
  90. struct ida endp_cnt;
  91. struct list_head port_list; /* nvme_fc_port_list */
  92. struct list_head endp_list;
  93. struct device *dev; /* physical device for dma */
  94. struct nvme_fc_port_template *ops;
  95. struct kref ref;
  96. } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
  97. struct nvme_fc_rport {
  98. struct nvme_fc_remote_port remoteport;
  99. struct list_head endp_list; /* for lport->endp_list */
  100. struct list_head ctrl_list;
  101. spinlock_t lock;
  102. struct kref ref;
  103. } __aligned(sizeof(u64)); /* alignment for other things alloc'd with */
  104. enum nvme_fcctrl_state {
  105. FCCTRL_INIT = 0,
  106. FCCTRL_ACTIVE = 1,
  107. };
  108. struct nvme_fc_ctrl {
  109. spinlock_t lock;
  110. struct nvme_fc_queue *queues;
  111. u32 queue_count;
  112. struct device *dev;
  113. struct nvme_fc_lport *lport;
  114. struct nvme_fc_rport *rport;
  115. u32 cnum;
  116. u64 association_id;
  117. u64 cap;
  118. struct list_head ctrl_list; /* rport->ctrl_list */
  119. struct list_head ls_req_list;
  120. struct blk_mq_tag_set admin_tag_set;
  121. struct blk_mq_tag_set tag_set;
  122. struct work_struct delete_work;
  123. struct kref ref;
  124. int state;
  125. struct nvme_fc_fcp_op aen_ops[NVME_FC_NR_AEN_COMMANDS];
  126. struct nvme_ctrl ctrl;
  127. };
  128. static inline struct nvme_fc_ctrl *
  129. to_fc_ctrl(struct nvme_ctrl *ctrl)
  130. {
  131. return container_of(ctrl, struct nvme_fc_ctrl, ctrl);
  132. }
  133. static inline struct nvme_fc_lport *
  134. localport_to_lport(struct nvme_fc_local_port *portptr)
  135. {
  136. return container_of(portptr, struct nvme_fc_lport, localport);
  137. }
  138. static inline struct nvme_fc_rport *
  139. remoteport_to_rport(struct nvme_fc_remote_port *portptr)
  140. {
  141. return container_of(portptr, struct nvme_fc_rport, remoteport);
  142. }
  143. static inline struct nvmefc_ls_req_op *
  144. ls_req_to_lsop(struct nvmefc_ls_req *lsreq)
  145. {
  146. return container_of(lsreq, struct nvmefc_ls_req_op, ls_req);
  147. }
  148. static inline struct nvme_fc_fcp_op *
  149. fcp_req_to_fcp_op(struct nvmefc_fcp_req *fcpreq)
  150. {
  151. return container_of(fcpreq, struct nvme_fc_fcp_op, fcp_req);
  152. }
  153. /* *************************** Globals **************************** */
  154. static DEFINE_SPINLOCK(nvme_fc_lock);
  155. static LIST_HEAD(nvme_fc_lport_list);
  156. static DEFINE_IDA(nvme_fc_local_port_cnt);
  157. static DEFINE_IDA(nvme_fc_ctrl_cnt);
  158. static struct workqueue_struct *nvme_fc_wq;
  159. /* *********************** FC-NVME Port Management ************************ */
  160. static int __nvme_fc_del_ctrl(struct nvme_fc_ctrl *);
  161. static void __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *,
  162. struct nvme_fc_queue *, unsigned int);
  163. /**
  164. * nvme_fc_register_localport - transport entry point called by an
  165. * LLDD to register the existence of a NVME
  166. * host FC port.
  167. * @pinfo: pointer to information about the port to be registered
  168. * @template: LLDD entrypoints and operational parameters for the port
  169. * @dev: physical hardware device node port corresponds to. Will be
  170. * used for DMA mappings
  171. * @lport_p: pointer to a local port pointer. Upon success, the routine
  172. * will allocate a nvme_fc_local_port structure and place its
  173. * address in the local port pointer. Upon failure, local port
  174. * pointer will be set to 0.
  175. *
  176. * Returns:
  177. * a completion status. Must be 0 upon success; a negative errno
  178. * (ex: -ENXIO) upon failure.
  179. */
  180. int
  181. nvme_fc_register_localport(struct nvme_fc_port_info *pinfo,
  182. struct nvme_fc_port_template *template,
  183. struct device *dev,
  184. struct nvme_fc_local_port **portptr)
  185. {
  186. struct nvme_fc_lport *newrec;
  187. unsigned long flags;
  188. int ret, idx;
  189. if (!template->localport_delete || !template->remoteport_delete ||
  190. !template->ls_req || !template->fcp_io ||
  191. !template->ls_abort || !template->fcp_abort ||
  192. !template->max_hw_queues || !template->max_sgl_segments ||
  193. !template->max_dif_sgl_segments || !template->dma_boundary) {
  194. ret = -EINVAL;
  195. goto out_reghost_failed;
  196. }
  197. newrec = kmalloc((sizeof(*newrec) + template->local_priv_sz),
  198. GFP_KERNEL);
  199. if (!newrec) {
  200. ret = -ENOMEM;
  201. goto out_reghost_failed;
  202. }
  203. idx = ida_simple_get(&nvme_fc_local_port_cnt, 0, 0, GFP_KERNEL);
  204. if (idx < 0) {
  205. ret = -ENOSPC;
  206. goto out_fail_kfree;
  207. }
  208. if (!get_device(dev) && dev) {
  209. ret = -ENODEV;
  210. goto out_ida_put;
  211. }
  212. INIT_LIST_HEAD(&newrec->port_list);
  213. INIT_LIST_HEAD(&newrec->endp_list);
  214. kref_init(&newrec->ref);
  215. newrec->ops = template;
  216. newrec->dev = dev;
  217. ida_init(&newrec->endp_cnt);
  218. newrec->localport.private = &newrec[1];
  219. newrec->localport.node_name = pinfo->node_name;
  220. newrec->localport.port_name = pinfo->port_name;
  221. newrec->localport.port_role = pinfo->port_role;
  222. newrec->localport.port_id = pinfo->port_id;
  223. newrec->localport.port_state = FC_OBJSTATE_ONLINE;
  224. newrec->localport.port_num = idx;
  225. spin_lock_irqsave(&nvme_fc_lock, flags);
  226. list_add_tail(&newrec->port_list, &nvme_fc_lport_list);
  227. spin_unlock_irqrestore(&nvme_fc_lock, flags);
  228. if (dev)
  229. dma_set_seg_boundary(dev, template->dma_boundary);
  230. *portptr = &newrec->localport;
  231. return 0;
  232. out_ida_put:
  233. ida_simple_remove(&nvme_fc_local_port_cnt, idx);
  234. out_fail_kfree:
  235. kfree(newrec);
  236. out_reghost_failed:
  237. *portptr = NULL;
  238. return ret;
  239. }
  240. EXPORT_SYMBOL_GPL(nvme_fc_register_localport);
  241. static void
  242. nvme_fc_free_lport(struct kref *ref)
  243. {
  244. struct nvme_fc_lport *lport =
  245. container_of(ref, struct nvme_fc_lport, ref);
  246. unsigned long flags;
  247. WARN_ON(lport->localport.port_state != FC_OBJSTATE_DELETED);
  248. WARN_ON(!list_empty(&lport->endp_list));
  249. /* remove from transport list */
  250. spin_lock_irqsave(&nvme_fc_lock, flags);
  251. list_del(&lport->port_list);
  252. spin_unlock_irqrestore(&nvme_fc_lock, flags);
  253. /* let the LLDD know we've finished tearing it down */
  254. lport->ops->localport_delete(&lport->localport);
  255. ida_simple_remove(&nvme_fc_local_port_cnt, lport->localport.port_num);
  256. ida_destroy(&lport->endp_cnt);
  257. put_device(lport->dev);
  258. kfree(lport);
  259. }
  260. static void
  261. nvme_fc_lport_put(struct nvme_fc_lport *lport)
  262. {
  263. kref_put(&lport->ref, nvme_fc_free_lport);
  264. }
  265. static int
  266. nvme_fc_lport_get(struct nvme_fc_lport *lport)
  267. {
  268. return kref_get_unless_zero(&lport->ref);
  269. }
  270. /**
  271. * nvme_fc_unregister_localport - transport entry point called by an
  272. * LLDD to deregister/remove a previously
  273. * registered a NVME host FC port.
  274. * @localport: pointer to the (registered) local port that is to be
  275. * deregistered.
  276. *
  277. * Returns:
  278. * a completion status. Must be 0 upon success; a negative errno
  279. * (ex: -ENXIO) upon failure.
  280. */
  281. int
  282. nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr)
  283. {
  284. struct nvme_fc_lport *lport = localport_to_lport(portptr);
  285. unsigned long flags;
  286. if (!portptr)
  287. return -EINVAL;
  288. spin_lock_irqsave(&nvme_fc_lock, flags);
  289. if (portptr->port_state != FC_OBJSTATE_ONLINE) {
  290. spin_unlock_irqrestore(&nvme_fc_lock, flags);
  291. return -EINVAL;
  292. }
  293. portptr->port_state = FC_OBJSTATE_DELETED;
  294. spin_unlock_irqrestore(&nvme_fc_lock, flags);
  295. nvme_fc_lport_put(lport);
  296. return 0;
  297. }
  298. EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport);
  299. /**
  300. * nvme_fc_register_remoteport - transport entry point called by an
  301. * LLDD to register the existence of a NVME
  302. * subsystem FC port on its fabric.
  303. * @localport: pointer to the (registered) local port that the remote
  304. * subsystem port is connected to.
  305. * @pinfo: pointer to information about the port to be registered
  306. * @rport_p: pointer to a remote port pointer. Upon success, the routine
  307. * will allocate a nvme_fc_remote_port structure and place its
  308. * address in the remote port pointer. Upon failure, remote port
  309. * pointer will be set to 0.
  310. *
  311. * Returns:
  312. * a completion status. Must be 0 upon success; a negative errno
  313. * (ex: -ENXIO) upon failure.
  314. */
  315. int
  316. nvme_fc_register_remoteport(struct nvme_fc_local_port *localport,
  317. struct nvme_fc_port_info *pinfo,
  318. struct nvme_fc_remote_port **portptr)
  319. {
  320. struct nvme_fc_lport *lport = localport_to_lport(localport);
  321. struct nvme_fc_rport *newrec;
  322. unsigned long flags;
  323. int ret, idx;
  324. newrec = kmalloc((sizeof(*newrec) + lport->ops->remote_priv_sz),
  325. GFP_KERNEL);
  326. if (!newrec) {
  327. ret = -ENOMEM;
  328. goto out_reghost_failed;
  329. }
  330. if (!nvme_fc_lport_get(lport)) {
  331. ret = -ESHUTDOWN;
  332. goto out_kfree_rport;
  333. }
  334. idx = ida_simple_get(&lport->endp_cnt, 0, 0, GFP_KERNEL);
  335. if (idx < 0) {
  336. ret = -ENOSPC;
  337. goto out_lport_put;
  338. }
  339. INIT_LIST_HEAD(&newrec->endp_list);
  340. INIT_LIST_HEAD(&newrec->ctrl_list);
  341. kref_init(&newrec->ref);
  342. spin_lock_init(&newrec->lock);
  343. newrec->remoteport.localport = &lport->localport;
  344. newrec->remoteport.private = &newrec[1];
  345. newrec->remoteport.port_role = pinfo->port_role;
  346. newrec->remoteport.node_name = pinfo->node_name;
  347. newrec->remoteport.port_name = pinfo->port_name;
  348. newrec->remoteport.port_id = pinfo->port_id;
  349. newrec->remoteport.port_state = FC_OBJSTATE_ONLINE;
  350. newrec->remoteport.port_num = idx;
  351. spin_lock_irqsave(&nvme_fc_lock, flags);
  352. list_add_tail(&newrec->endp_list, &lport->endp_list);
  353. spin_unlock_irqrestore(&nvme_fc_lock, flags);
  354. *portptr = &newrec->remoteport;
  355. return 0;
  356. out_lport_put:
  357. nvme_fc_lport_put(lport);
  358. out_kfree_rport:
  359. kfree(newrec);
  360. out_reghost_failed:
  361. *portptr = NULL;
  362. return ret;
  363. }
  364. EXPORT_SYMBOL_GPL(nvme_fc_register_remoteport);
  365. static void
  366. nvme_fc_free_rport(struct kref *ref)
  367. {
  368. struct nvme_fc_rport *rport =
  369. container_of(ref, struct nvme_fc_rport, ref);
  370. struct nvme_fc_lport *lport =
  371. localport_to_lport(rport->remoteport.localport);
  372. unsigned long flags;
  373. WARN_ON(rport->remoteport.port_state != FC_OBJSTATE_DELETED);
  374. WARN_ON(!list_empty(&rport->ctrl_list));
  375. /* remove from lport list */
  376. spin_lock_irqsave(&nvme_fc_lock, flags);
  377. list_del(&rport->endp_list);
  378. spin_unlock_irqrestore(&nvme_fc_lock, flags);
  379. /* let the LLDD know we've finished tearing it down */
  380. lport->ops->remoteport_delete(&rport->remoteport);
  381. ida_simple_remove(&lport->endp_cnt, rport->remoteport.port_num);
  382. kfree(rport);
  383. nvme_fc_lport_put(lport);
  384. }
  385. static void
  386. nvme_fc_rport_put(struct nvme_fc_rport *rport)
  387. {
  388. kref_put(&rport->ref, nvme_fc_free_rport);
  389. }
  390. static int
  391. nvme_fc_rport_get(struct nvme_fc_rport *rport)
  392. {
  393. return kref_get_unless_zero(&rport->ref);
  394. }
  395. /**
  396. * nvme_fc_unregister_remoteport - transport entry point called by an
  397. * LLDD to deregister/remove a previously
  398. * registered a NVME subsystem FC port.
  399. * @remoteport: pointer to the (registered) remote port that is to be
  400. * deregistered.
  401. *
  402. * Returns:
  403. * a completion status. Must be 0 upon success; a negative errno
  404. * (ex: -ENXIO) upon failure.
  405. */
  406. int
  407. nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr)
  408. {
  409. struct nvme_fc_rport *rport = remoteport_to_rport(portptr);
  410. struct nvme_fc_ctrl *ctrl;
  411. unsigned long flags;
  412. if (!portptr)
  413. return -EINVAL;
  414. spin_lock_irqsave(&rport->lock, flags);
  415. if (portptr->port_state != FC_OBJSTATE_ONLINE) {
  416. spin_unlock_irqrestore(&rport->lock, flags);
  417. return -EINVAL;
  418. }
  419. portptr->port_state = FC_OBJSTATE_DELETED;
  420. /* tear down all associations to the remote port */
  421. list_for_each_entry(ctrl, &rport->ctrl_list, ctrl_list)
  422. __nvme_fc_del_ctrl(ctrl);
  423. spin_unlock_irqrestore(&rport->lock, flags);
  424. nvme_fc_rport_put(rport);
  425. return 0;
  426. }
  427. EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport);
  428. /* *********************** FC-NVME DMA Handling **************************** */
  429. /*
  430. * The fcloop device passes in a NULL device pointer. Real LLD's will
  431. * pass in a valid device pointer. If NULL is passed to the dma mapping
  432. * routines, depending on the platform, it may or may not succeed, and
  433. * may crash.
  434. *
  435. * As such:
  436. * Wrapper all the dma routines and check the dev pointer.
  437. *
  438. * If simple mappings (return just a dma address, we'll noop them,
  439. * returning a dma address of 0.
  440. *
  441. * On more complex mappings (dma_map_sg), a pseudo routine fills
  442. * in the scatter list, setting all dma addresses to 0.
  443. */
  444. static inline dma_addr_t
  445. fc_dma_map_single(struct device *dev, void *ptr, size_t size,
  446. enum dma_data_direction dir)
  447. {
  448. return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
  449. }
  450. static inline int
  451. fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  452. {
  453. return dev ? dma_mapping_error(dev, dma_addr) : 0;
  454. }
  455. static inline void
  456. fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
  457. enum dma_data_direction dir)
  458. {
  459. if (dev)
  460. dma_unmap_single(dev, addr, size, dir);
  461. }
  462. static inline void
  463. fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
  464. enum dma_data_direction dir)
  465. {
  466. if (dev)
  467. dma_sync_single_for_cpu(dev, addr, size, dir);
  468. }
  469. static inline void
  470. fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
  471. enum dma_data_direction dir)
  472. {
  473. if (dev)
  474. dma_sync_single_for_device(dev, addr, size, dir);
  475. }
  476. /* pseudo dma_map_sg call */
  477. static int
  478. fc_map_sg(struct scatterlist *sg, int nents)
  479. {
  480. struct scatterlist *s;
  481. int i;
  482. WARN_ON(nents == 0 || sg[0].length == 0);
  483. for_each_sg(sg, s, nents, i) {
  484. s->dma_address = 0L;
  485. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  486. s->dma_length = s->length;
  487. #endif
  488. }
  489. return nents;
  490. }
  491. static inline int
  492. fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  493. enum dma_data_direction dir)
  494. {
  495. return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
  496. }
  497. static inline void
  498. fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  499. enum dma_data_direction dir)
  500. {
  501. if (dev)
  502. dma_unmap_sg(dev, sg, nents, dir);
  503. }
  504. /* *********************** FC-NVME LS Handling **************************** */
  505. static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
  506. static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
  507. static void
  508. __nvme_fc_finish_ls_req(struct nvme_fc_ctrl *ctrl,
  509. struct nvmefc_ls_req_op *lsop)
  510. {
  511. struct nvmefc_ls_req *lsreq = &lsop->ls_req;
  512. unsigned long flags;
  513. spin_lock_irqsave(&ctrl->lock, flags);
  514. if (!lsop->req_queued) {
  515. spin_unlock_irqrestore(&ctrl->lock, flags);
  516. return;
  517. }
  518. list_del(&lsop->lsreq_list);
  519. lsop->req_queued = false;
  520. spin_unlock_irqrestore(&ctrl->lock, flags);
  521. fc_dma_unmap_single(ctrl->dev, lsreq->rqstdma,
  522. (lsreq->rqstlen + lsreq->rsplen),
  523. DMA_BIDIRECTIONAL);
  524. nvme_fc_ctrl_put(ctrl);
  525. }
  526. static int
  527. __nvme_fc_send_ls_req(struct nvme_fc_ctrl *ctrl,
  528. struct nvmefc_ls_req_op *lsop,
  529. void (*done)(struct nvmefc_ls_req *req, int status))
  530. {
  531. struct nvmefc_ls_req *lsreq = &lsop->ls_req;
  532. unsigned long flags;
  533. int ret;
  534. if (!nvme_fc_ctrl_get(ctrl))
  535. return -ESHUTDOWN;
  536. lsreq->done = done;
  537. lsop->ctrl = ctrl;
  538. lsop->req_queued = false;
  539. INIT_LIST_HEAD(&lsop->lsreq_list);
  540. init_completion(&lsop->ls_done);
  541. lsreq->rqstdma = fc_dma_map_single(ctrl->dev, lsreq->rqstaddr,
  542. lsreq->rqstlen + lsreq->rsplen,
  543. DMA_BIDIRECTIONAL);
  544. if (fc_dma_mapping_error(ctrl->dev, lsreq->rqstdma)) {
  545. nvme_fc_ctrl_put(ctrl);
  546. dev_err(ctrl->dev,
  547. "els request command failed EFAULT.\n");
  548. return -EFAULT;
  549. }
  550. lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
  551. spin_lock_irqsave(&ctrl->lock, flags);
  552. list_add_tail(&lsop->lsreq_list, &ctrl->ls_req_list);
  553. lsop->req_queued = true;
  554. spin_unlock_irqrestore(&ctrl->lock, flags);
  555. ret = ctrl->lport->ops->ls_req(&ctrl->lport->localport,
  556. &ctrl->rport->remoteport, lsreq);
  557. if (ret)
  558. lsop->ls_error = ret;
  559. return ret;
  560. }
  561. static void
  562. nvme_fc_send_ls_req_done(struct nvmefc_ls_req *lsreq, int status)
  563. {
  564. struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
  565. lsop->ls_error = status;
  566. complete(&lsop->ls_done);
  567. }
  568. static int
  569. nvme_fc_send_ls_req(struct nvme_fc_ctrl *ctrl, struct nvmefc_ls_req_op *lsop)
  570. {
  571. struct nvmefc_ls_req *lsreq = &lsop->ls_req;
  572. struct fcnvme_ls_rjt *rjt = lsreq->rspaddr;
  573. int ret;
  574. ret = __nvme_fc_send_ls_req(ctrl, lsop, nvme_fc_send_ls_req_done);
  575. if (!ret)
  576. /*
  577. * No timeout/not interruptible as we need the struct
  578. * to exist until the lldd calls us back. Thus mandate
  579. * wait until driver calls back. lldd responsible for
  580. * the timeout action
  581. */
  582. wait_for_completion(&lsop->ls_done);
  583. __nvme_fc_finish_ls_req(ctrl, lsop);
  584. if (ret) {
  585. dev_err(ctrl->dev,
  586. "ls request command failed (%d).\n", ret);
  587. return ret;
  588. }
  589. /* ACC or RJT payload ? */
  590. if (rjt->w0.ls_cmd == FCNVME_LS_RJT)
  591. return -ENXIO;
  592. return 0;
  593. }
  594. static void
  595. nvme_fc_send_ls_req_async(struct nvme_fc_ctrl *ctrl,
  596. struct nvmefc_ls_req_op *lsop,
  597. void (*done)(struct nvmefc_ls_req *req, int status))
  598. {
  599. int ret;
  600. ret = __nvme_fc_send_ls_req(ctrl, lsop, done);
  601. /* don't wait for completion */
  602. if (ret)
  603. done(&lsop->ls_req, ret);
  604. }
  605. /* Validation Error indexes into the string table below */
  606. enum {
  607. VERR_NO_ERROR = 0,
  608. VERR_LSACC = 1,
  609. VERR_LSDESC_RQST = 2,
  610. VERR_LSDESC_RQST_LEN = 3,
  611. VERR_ASSOC_ID = 4,
  612. VERR_ASSOC_ID_LEN = 5,
  613. VERR_CONN_ID = 6,
  614. VERR_CONN_ID_LEN = 7,
  615. VERR_CR_ASSOC = 8,
  616. VERR_CR_ASSOC_ACC_LEN = 9,
  617. VERR_CR_CONN = 10,
  618. VERR_CR_CONN_ACC_LEN = 11,
  619. VERR_DISCONN = 12,
  620. VERR_DISCONN_ACC_LEN = 13,
  621. };
  622. static char *validation_errors[] = {
  623. "OK",
  624. "Not LS_ACC",
  625. "Not LSDESC_RQST",
  626. "Bad LSDESC_RQST Length",
  627. "Not Association ID",
  628. "Bad Association ID Length",
  629. "Not Connection ID",
  630. "Bad Connection ID Length",
  631. "Not CR_ASSOC Rqst",
  632. "Bad CR_ASSOC ACC Length",
  633. "Not CR_CONN Rqst",
  634. "Bad CR_CONN ACC Length",
  635. "Not Disconnect Rqst",
  636. "Bad Disconnect ACC Length",
  637. };
  638. static int
  639. nvme_fc_connect_admin_queue(struct nvme_fc_ctrl *ctrl,
  640. struct nvme_fc_queue *queue, u16 qsize, u16 ersp_ratio)
  641. {
  642. struct nvmefc_ls_req_op *lsop;
  643. struct nvmefc_ls_req *lsreq;
  644. struct fcnvme_ls_cr_assoc_rqst *assoc_rqst;
  645. struct fcnvme_ls_cr_assoc_acc *assoc_acc;
  646. int ret, fcret = 0;
  647. lsop = kzalloc((sizeof(*lsop) +
  648. ctrl->lport->ops->lsrqst_priv_sz +
  649. sizeof(*assoc_rqst) + sizeof(*assoc_acc)), GFP_KERNEL);
  650. if (!lsop) {
  651. ret = -ENOMEM;
  652. goto out_no_memory;
  653. }
  654. lsreq = &lsop->ls_req;
  655. lsreq->private = (void *)&lsop[1];
  656. assoc_rqst = (struct fcnvme_ls_cr_assoc_rqst *)
  657. (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
  658. assoc_acc = (struct fcnvme_ls_cr_assoc_acc *)&assoc_rqst[1];
  659. assoc_rqst->w0.ls_cmd = FCNVME_LS_CREATE_ASSOCIATION;
  660. assoc_rqst->desc_list_len =
  661. cpu_to_be32(sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
  662. assoc_rqst->assoc_cmd.desc_tag =
  663. cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD);
  664. assoc_rqst->assoc_cmd.desc_len =
  665. fcnvme_lsdesc_len(
  666. sizeof(struct fcnvme_lsdesc_cr_assoc_cmd));
  667. assoc_rqst->assoc_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
  668. assoc_rqst->assoc_cmd.sqsize = cpu_to_be16(qsize);
  669. /* Linux supports only Dynamic controllers */
  670. assoc_rqst->assoc_cmd.cntlid = cpu_to_be16(0xffff);
  671. memcpy(&assoc_rqst->assoc_cmd.hostid, &ctrl->ctrl.opts->host->id,
  672. min_t(size_t, FCNVME_ASSOC_HOSTID_LEN, sizeof(uuid_be)));
  673. strncpy(assoc_rqst->assoc_cmd.hostnqn, ctrl->ctrl.opts->host->nqn,
  674. min(FCNVME_ASSOC_HOSTNQN_LEN, NVMF_NQN_SIZE));
  675. strncpy(assoc_rqst->assoc_cmd.subnqn, ctrl->ctrl.opts->subsysnqn,
  676. min(FCNVME_ASSOC_SUBNQN_LEN, NVMF_NQN_SIZE));
  677. lsop->queue = queue;
  678. lsreq->rqstaddr = assoc_rqst;
  679. lsreq->rqstlen = sizeof(*assoc_rqst);
  680. lsreq->rspaddr = assoc_acc;
  681. lsreq->rsplen = sizeof(*assoc_acc);
  682. lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
  683. ret = nvme_fc_send_ls_req(ctrl, lsop);
  684. if (ret)
  685. goto out_free_buffer;
  686. /* process connect LS completion */
  687. /* validate the ACC response */
  688. if (assoc_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
  689. fcret = VERR_LSACC;
  690. if (assoc_acc->hdr.desc_list_len !=
  691. fcnvme_lsdesc_len(
  692. sizeof(struct fcnvme_ls_cr_assoc_acc)))
  693. fcret = VERR_CR_ASSOC_ACC_LEN;
  694. if (assoc_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST))
  695. fcret = VERR_LSDESC_RQST;
  696. else if (assoc_acc->hdr.rqst.desc_len !=
  697. fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
  698. fcret = VERR_LSDESC_RQST_LEN;
  699. else if (assoc_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_ASSOCIATION)
  700. fcret = VERR_CR_ASSOC;
  701. else if (assoc_acc->associd.desc_tag !=
  702. cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
  703. fcret = VERR_ASSOC_ID;
  704. else if (assoc_acc->associd.desc_len !=
  705. fcnvme_lsdesc_len(
  706. sizeof(struct fcnvme_lsdesc_assoc_id)))
  707. fcret = VERR_ASSOC_ID_LEN;
  708. else if (assoc_acc->connectid.desc_tag !=
  709. cpu_to_be32(FCNVME_LSDESC_CONN_ID))
  710. fcret = VERR_CONN_ID;
  711. else if (assoc_acc->connectid.desc_len !=
  712. fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
  713. fcret = VERR_CONN_ID_LEN;
  714. if (fcret) {
  715. ret = -EBADF;
  716. dev_err(ctrl->dev,
  717. "q %d connect failed: %s\n",
  718. queue->qnum, validation_errors[fcret]);
  719. } else {
  720. ctrl->association_id =
  721. be64_to_cpu(assoc_acc->associd.association_id);
  722. queue->connection_id =
  723. be64_to_cpu(assoc_acc->connectid.connection_id);
  724. set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
  725. }
  726. out_free_buffer:
  727. kfree(lsop);
  728. out_no_memory:
  729. if (ret)
  730. dev_err(ctrl->dev,
  731. "queue %d connect admin queue failed (%d).\n",
  732. queue->qnum, ret);
  733. return ret;
  734. }
  735. static int
  736. nvme_fc_connect_queue(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
  737. u16 qsize, u16 ersp_ratio)
  738. {
  739. struct nvmefc_ls_req_op *lsop;
  740. struct nvmefc_ls_req *lsreq;
  741. struct fcnvme_ls_cr_conn_rqst *conn_rqst;
  742. struct fcnvme_ls_cr_conn_acc *conn_acc;
  743. int ret, fcret = 0;
  744. lsop = kzalloc((sizeof(*lsop) +
  745. ctrl->lport->ops->lsrqst_priv_sz +
  746. sizeof(*conn_rqst) + sizeof(*conn_acc)), GFP_KERNEL);
  747. if (!lsop) {
  748. ret = -ENOMEM;
  749. goto out_no_memory;
  750. }
  751. lsreq = &lsop->ls_req;
  752. lsreq->private = (void *)&lsop[1];
  753. conn_rqst = (struct fcnvme_ls_cr_conn_rqst *)
  754. (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
  755. conn_acc = (struct fcnvme_ls_cr_conn_acc *)&conn_rqst[1];
  756. conn_rqst->w0.ls_cmd = FCNVME_LS_CREATE_CONNECTION;
  757. conn_rqst->desc_list_len = cpu_to_be32(
  758. sizeof(struct fcnvme_lsdesc_assoc_id) +
  759. sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
  760. conn_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
  761. conn_rqst->associd.desc_len =
  762. fcnvme_lsdesc_len(
  763. sizeof(struct fcnvme_lsdesc_assoc_id));
  764. conn_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
  765. conn_rqst->connect_cmd.desc_tag =
  766. cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD);
  767. conn_rqst->connect_cmd.desc_len =
  768. fcnvme_lsdesc_len(
  769. sizeof(struct fcnvme_lsdesc_cr_conn_cmd));
  770. conn_rqst->connect_cmd.ersp_ratio = cpu_to_be16(ersp_ratio);
  771. conn_rqst->connect_cmd.qid = cpu_to_be16(queue->qnum);
  772. conn_rqst->connect_cmd.sqsize = cpu_to_be16(qsize);
  773. lsop->queue = queue;
  774. lsreq->rqstaddr = conn_rqst;
  775. lsreq->rqstlen = sizeof(*conn_rqst);
  776. lsreq->rspaddr = conn_acc;
  777. lsreq->rsplen = sizeof(*conn_acc);
  778. lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
  779. ret = nvme_fc_send_ls_req(ctrl, lsop);
  780. if (ret)
  781. goto out_free_buffer;
  782. /* process connect LS completion */
  783. /* validate the ACC response */
  784. if (conn_acc->hdr.w0.ls_cmd != FCNVME_LS_ACC)
  785. fcret = VERR_LSACC;
  786. if (conn_acc->hdr.desc_list_len !=
  787. fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)))
  788. fcret = VERR_CR_CONN_ACC_LEN;
  789. if (conn_acc->hdr.rqst.desc_tag != cpu_to_be32(FCNVME_LSDESC_RQST))
  790. fcret = VERR_LSDESC_RQST;
  791. else if (conn_acc->hdr.rqst.desc_len !=
  792. fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_rqst)))
  793. fcret = VERR_LSDESC_RQST_LEN;
  794. else if (conn_acc->hdr.rqst.w0.ls_cmd != FCNVME_LS_CREATE_CONNECTION)
  795. fcret = VERR_CR_CONN;
  796. else if (conn_acc->connectid.desc_tag !=
  797. cpu_to_be32(FCNVME_LSDESC_CONN_ID))
  798. fcret = VERR_CONN_ID;
  799. else if (conn_acc->connectid.desc_len !=
  800. fcnvme_lsdesc_len(sizeof(struct fcnvme_lsdesc_conn_id)))
  801. fcret = VERR_CONN_ID_LEN;
  802. if (fcret) {
  803. ret = -EBADF;
  804. dev_err(ctrl->dev,
  805. "q %d connect failed: %s\n",
  806. queue->qnum, validation_errors[fcret]);
  807. } else {
  808. queue->connection_id =
  809. be64_to_cpu(conn_acc->connectid.connection_id);
  810. set_bit(NVME_FC_Q_CONNECTED, &queue->flags);
  811. }
  812. out_free_buffer:
  813. kfree(lsop);
  814. out_no_memory:
  815. if (ret)
  816. dev_err(ctrl->dev,
  817. "queue %d connect command failed (%d).\n",
  818. queue->qnum, ret);
  819. return ret;
  820. }
  821. static void
  822. nvme_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
  823. {
  824. struct nvmefc_ls_req_op *lsop = ls_req_to_lsop(lsreq);
  825. struct nvme_fc_ctrl *ctrl = lsop->ctrl;
  826. __nvme_fc_finish_ls_req(ctrl, lsop);
  827. if (status)
  828. dev_err(ctrl->dev,
  829. "disconnect assoc ls request command failed (%d).\n",
  830. status);
  831. /* fc-nvme iniator doesn't care about success or failure of cmd */
  832. kfree(lsop);
  833. }
  834. /*
  835. * This routine sends a FC-NVME LS to disconnect (aka terminate)
  836. * the FC-NVME Association. Terminating the association also
  837. * terminates the FC-NVME connections (per queue, both admin and io
  838. * queues) that are part of the association. E.g. things are torn
  839. * down, and the related FC-NVME Association ID and Connection IDs
  840. * become invalid.
  841. *
  842. * The behavior of the fc-nvme initiator is such that it's
  843. * understanding of the association and connections will implicitly
  844. * be torn down. The action is implicit as it may be due to a loss of
  845. * connectivity with the fc-nvme target, so you may never get a
  846. * response even if you tried. As such, the action of this routine
  847. * is to asynchronously send the LS, ignore any results of the LS, and
  848. * continue on with terminating the association. If the fc-nvme target
  849. * is present and receives the LS, it too can tear down.
  850. */
  851. static void
  852. nvme_fc_xmt_disconnect_assoc(struct nvme_fc_ctrl *ctrl)
  853. {
  854. struct fcnvme_ls_disconnect_rqst *discon_rqst;
  855. struct fcnvme_ls_disconnect_acc *discon_acc;
  856. struct nvmefc_ls_req_op *lsop;
  857. struct nvmefc_ls_req *lsreq;
  858. lsop = kzalloc((sizeof(*lsop) +
  859. ctrl->lport->ops->lsrqst_priv_sz +
  860. sizeof(*discon_rqst) + sizeof(*discon_acc)),
  861. GFP_KERNEL);
  862. if (!lsop)
  863. /* couldn't sent it... too bad */
  864. return;
  865. lsreq = &lsop->ls_req;
  866. lsreq->private = (void *)&lsop[1];
  867. discon_rqst = (struct fcnvme_ls_disconnect_rqst *)
  868. (lsreq->private + ctrl->lport->ops->lsrqst_priv_sz);
  869. discon_acc = (struct fcnvme_ls_disconnect_acc *)&discon_rqst[1];
  870. discon_rqst->w0.ls_cmd = FCNVME_LS_DISCONNECT;
  871. discon_rqst->desc_list_len = cpu_to_be32(
  872. sizeof(struct fcnvme_lsdesc_assoc_id) +
  873. sizeof(struct fcnvme_lsdesc_disconn_cmd));
  874. discon_rqst->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
  875. discon_rqst->associd.desc_len =
  876. fcnvme_lsdesc_len(
  877. sizeof(struct fcnvme_lsdesc_assoc_id));
  878. discon_rqst->associd.association_id = cpu_to_be64(ctrl->association_id);
  879. discon_rqst->discon_cmd.desc_tag = cpu_to_be32(
  880. FCNVME_LSDESC_DISCONN_CMD);
  881. discon_rqst->discon_cmd.desc_len =
  882. fcnvme_lsdesc_len(
  883. sizeof(struct fcnvme_lsdesc_disconn_cmd));
  884. discon_rqst->discon_cmd.scope = FCNVME_DISCONN_ASSOCIATION;
  885. discon_rqst->discon_cmd.id = cpu_to_be64(ctrl->association_id);
  886. lsreq->rqstaddr = discon_rqst;
  887. lsreq->rqstlen = sizeof(*discon_rqst);
  888. lsreq->rspaddr = discon_acc;
  889. lsreq->rsplen = sizeof(*discon_acc);
  890. lsreq->timeout = NVME_FC_CONNECT_TIMEOUT_SEC;
  891. nvme_fc_send_ls_req_async(ctrl, lsop, nvme_fc_disconnect_assoc_done);
  892. /* only meaningful part to terminating the association */
  893. ctrl->association_id = 0;
  894. }
  895. /* *********************** NVME Ctrl Routines **************************** */
  896. static int
  897. nvme_fc_reinit_request(void *data, struct request *rq)
  898. {
  899. struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
  900. struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
  901. memset(cmdiu, 0, sizeof(*cmdiu));
  902. cmdiu->scsi_id = NVME_CMD_SCSI_ID;
  903. cmdiu->fc_id = NVME_CMD_FC_ID;
  904. cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
  905. memset(&op->rsp_iu, 0, sizeof(op->rsp_iu));
  906. return 0;
  907. }
  908. static void
  909. __nvme_fc_exit_request(struct nvme_fc_ctrl *ctrl,
  910. struct nvme_fc_fcp_op *op)
  911. {
  912. fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.rspdma,
  913. sizeof(op->rsp_iu), DMA_FROM_DEVICE);
  914. fc_dma_unmap_single(ctrl->lport->dev, op->fcp_req.cmddma,
  915. sizeof(op->cmd_iu), DMA_TO_DEVICE);
  916. atomic_set(&op->state, FCPOP_STATE_UNINIT);
  917. }
  918. static void
  919. nvme_fc_exit_request(void *data, struct request *rq,
  920. unsigned int hctx_idx, unsigned int rq_idx)
  921. {
  922. struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
  923. return __nvme_fc_exit_request(data, op);
  924. }
  925. static void
  926. nvme_fc_exit_aen_ops(struct nvme_fc_ctrl *ctrl)
  927. {
  928. struct nvme_fc_fcp_op *aen_op = ctrl->aen_ops;
  929. int i;
  930. for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
  931. if (atomic_read(&aen_op->state) == FCPOP_STATE_UNINIT)
  932. continue;
  933. __nvme_fc_exit_request(ctrl, aen_op);
  934. nvme_fc_ctrl_put(ctrl);
  935. }
  936. }
  937. void
  938. nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
  939. {
  940. struct nvme_fc_fcp_op *op = fcp_req_to_fcp_op(req);
  941. struct request *rq = op->rq;
  942. struct nvmefc_fcp_req *freq = &op->fcp_req;
  943. struct nvme_fc_ctrl *ctrl = op->ctrl;
  944. struct nvme_fc_queue *queue = op->queue;
  945. struct nvme_completion *cqe = &op->rsp_iu.cqe;
  946. u16 status;
  947. /*
  948. * WARNING:
  949. * The current linux implementation of a nvme controller
  950. * allocates a single tag set for all io queues and sizes
  951. * the io queues to fully hold all possible tags. Thus, the
  952. * implementation does not reference or care about the sqhd
  953. * value as it never needs to use the sqhd/sqtail pointers
  954. * for submission pacing.
  955. *
  956. * This affects the FC-NVME implementation in two ways:
  957. * 1) As the value doesn't matter, we don't need to waste
  958. * cycles extracting it from ERSPs and stamping it in the
  959. * cases where the transport fabricates CQEs on successful
  960. * completions.
  961. * 2) The FC-NVME implementation requires that delivery of
  962. * ERSP completions are to go back to the nvme layer in order
  963. * relative to the rsn, such that the sqhd value will always
  964. * be "in order" for the nvme layer. As the nvme layer in
  965. * linux doesn't care about sqhd, there's no need to return
  966. * them in order.
  967. *
  968. * Additionally:
  969. * As the core nvme layer in linux currently does not look at
  970. * every field in the cqe - in cases where the FC transport must
  971. * fabricate a CQE, the following fields will not be set as they
  972. * are not referenced:
  973. * cqe.sqid, cqe.sqhd, cqe.command_id
  974. */
  975. fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
  976. sizeof(op->rsp_iu), DMA_FROM_DEVICE);
  977. if (atomic_read(&op->state) == FCPOP_STATE_ABORTED)
  978. status = NVME_SC_ABORT_REQ | NVME_SC_DNR;
  979. else
  980. status = freq->status;
  981. /*
  982. * For the linux implementation, if we have an unsuccesful
  983. * status, they blk-mq layer can typically be called with the
  984. * non-zero status and the content of the cqe isn't important.
  985. */
  986. if (status)
  987. goto done;
  988. /*
  989. * command completed successfully relative to the wire
  990. * protocol. However, validate anything received and
  991. * extract the status and result from the cqe (create it
  992. * where necessary).
  993. */
  994. switch (freq->rcv_rsplen) {
  995. case 0:
  996. case NVME_FC_SIZEOF_ZEROS_RSP:
  997. /*
  998. * No response payload or 12 bytes of payload (which
  999. * should all be zeros) are considered successful and
  1000. * no payload in the CQE by the transport.
  1001. */
  1002. if (freq->transferred_length !=
  1003. be32_to_cpu(op->cmd_iu.data_len)) {
  1004. status = -EIO;
  1005. goto done;
  1006. }
  1007. op->nreq.result.u64 = 0;
  1008. break;
  1009. case sizeof(struct nvme_fc_ersp_iu):
  1010. /*
  1011. * The ERSP IU contains a full completion with CQE.
  1012. * Validate ERSP IU and look at cqe.
  1013. */
  1014. if (unlikely(be16_to_cpu(op->rsp_iu.iu_len) !=
  1015. (freq->rcv_rsplen / 4) ||
  1016. be32_to_cpu(op->rsp_iu.xfrd_len) !=
  1017. freq->transferred_length ||
  1018. op->rqno != le16_to_cpu(cqe->command_id))) {
  1019. status = -EIO;
  1020. goto done;
  1021. }
  1022. op->nreq.result = cqe->result;
  1023. status = le16_to_cpu(cqe->status) >> 1;
  1024. break;
  1025. default:
  1026. status = -EIO;
  1027. goto done;
  1028. }
  1029. done:
  1030. if (!queue->qnum && op->rqno >= AEN_CMDID_BASE) {
  1031. nvme_complete_async_event(&queue->ctrl->ctrl, status,
  1032. &op->nreq.result);
  1033. nvme_fc_ctrl_put(ctrl);
  1034. return;
  1035. }
  1036. blk_mq_complete_request(rq, status);
  1037. }
  1038. static int
  1039. __nvme_fc_init_request(struct nvme_fc_ctrl *ctrl,
  1040. struct nvme_fc_queue *queue, struct nvme_fc_fcp_op *op,
  1041. struct request *rq, u32 rqno)
  1042. {
  1043. struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
  1044. int ret = 0;
  1045. memset(op, 0, sizeof(*op));
  1046. op->fcp_req.cmdaddr = &op->cmd_iu;
  1047. op->fcp_req.cmdlen = sizeof(op->cmd_iu);
  1048. op->fcp_req.rspaddr = &op->rsp_iu;
  1049. op->fcp_req.rsplen = sizeof(op->rsp_iu);
  1050. op->fcp_req.done = nvme_fc_fcpio_done;
  1051. op->fcp_req.first_sgl = (struct scatterlist *)&op[1];
  1052. op->fcp_req.private = &op->fcp_req.first_sgl[SG_CHUNK_SIZE];
  1053. op->ctrl = ctrl;
  1054. op->queue = queue;
  1055. op->rq = rq;
  1056. op->rqno = rqno;
  1057. cmdiu->scsi_id = NVME_CMD_SCSI_ID;
  1058. cmdiu->fc_id = NVME_CMD_FC_ID;
  1059. cmdiu->iu_len = cpu_to_be16(sizeof(*cmdiu) / sizeof(u32));
  1060. op->fcp_req.cmddma = fc_dma_map_single(ctrl->lport->dev,
  1061. &op->cmd_iu, sizeof(op->cmd_iu), DMA_TO_DEVICE);
  1062. if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.cmddma)) {
  1063. dev_err(ctrl->dev,
  1064. "FCP Op failed - cmdiu dma mapping failed.\n");
  1065. ret = EFAULT;
  1066. goto out_on_error;
  1067. }
  1068. op->fcp_req.rspdma = fc_dma_map_single(ctrl->lport->dev,
  1069. &op->rsp_iu, sizeof(op->rsp_iu),
  1070. DMA_FROM_DEVICE);
  1071. if (fc_dma_mapping_error(ctrl->lport->dev, op->fcp_req.rspdma)) {
  1072. dev_err(ctrl->dev,
  1073. "FCP Op failed - rspiu dma mapping failed.\n");
  1074. ret = EFAULT;
  1075. }
  1076. atomic_set(&op->state, FCPOP_STATE_IDLE);
  1077. out_on_error:
  1078. return ret;
  1079. }
  1080. static int
  1081. nvme_fc_init_request(void *data, struct request *rq,
  1082. unsigned int hctx_idx, unsigned int rq_idx,
  1083. unsigned int numa_node)
  1084. {
  1085. struct nvme_fc_ctrl *ctrl = data;
  1086. struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
  1087. struct nvme_fc_queue *queue = &ctrl->queues[hctx_idx+1];
  1088. return __nvme_fc_init_request(ctrl, queue, op, rq, queue->rqcnt++);
  1089. }
  1090. static int
  1091. nvme_fc_init_admin_request(void *data, struct request *rq,
  1092. unsigned int hctx_idx, unsigned int rq_idx,
  1093. unsigned int numa_node)
  1094. {
  1095. struct nvme_fc_ctrl *ctrl = data;
  1096. struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
  1097. struct nvme_fc_queue *queue = &ctrl->queues[0];
  1098. return __nvme_fc_init_request(ctrl, queue, op, rq, queue->rqcnt++);
  1099. }
  1100. static int
  1101. nvme_fc_init_aen_ops(struct nvme_fc_ctrl *ctrl)
  1102. {
  1103. struct nvme_fc_fcp_op *aen_op;
  1104. struct nvme_fc_cmd_iu *cmdiu;
  1105. struct nvme_command *sqe;
  1106. int i, ret;
  1107. aen_op = ctrl->aen_ops;
  1108. for (i = 0; i < NVME_FC_NR_AEN_COMMANDS; i++, aen_op++) {
  1109. cmdiu = &aen_op->cmd_iu;
  1110. sqe = &cmdiu->sqe;
  1111. ret = __nvme_fc_init_request(ctrl, &ctrl->queues[0],
  1112. aen_op, (struct request *)NULL,
  1113. (AEN_CMDID_BASE + i));
  1114. if (ret)
  1115. return ret;
  1116. memset(sqe, 0, sizeof(*sqe));
  1117. sqe->common.opcode = nvme_admin_async_event;
  1118. sqe->common.command_id = AEN_CMDID_BASE + i;
  1119. }
  1120. return 0;
  1121. }
  1122. static inline void
  1123. __nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, struct nvme_fc_ctrl *ctrl,
  1124. unsigned int qidx)
  1125. {
  1126. struct nvme_fc_queue *queue = &ctrl->queues[qidx];
  1127. hctx->driver_data = queue;
  1128. queue->hctx = hctx;
  1129. }
  1130. static int
  1131. nvme_fc_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
  1132. unsigned int hctx_idx)
  1133. {
  1134. struct nvme_fc_ctrl *ctrl = data;
  1135. __nvme_fc_init_hctx(hctx, ctrl, hctx_idx + 1);
  1136. return 0;
  1137. }
  1138. static int
  1139. nvme_fc_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
  1140. unsigned int hctx_idx)
  1141. {
  1142. struct nvme_fc_ctrl *ctrl = data;
  1143. __nvme_fc_init_hctx(hctx, ctrl, hctx_idx);
  1144. return 0;
  1145. }
  1146. static void
  1147. nvme_fc_init_queue(struct nvme_fc_ctrl *ctrl, int idx, size_t queue_size)
  1148. {
  1149. struct nvme_fc_queue *queue;
  1150. queue = &ctrl->queues[idx];
  1151. memset(queue, 0, sizeof(*queue));
  1152. queue->ctrl = ctrl;
  1153. queue->qnum = idx;
  1154. atomic_set(&queue->csn, 1);
  1155. queue->dev = ctrl->dev;
  1156. if (idx > 0)
  1157. queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
  1158. else
  1159. queue->cmnd_capsule_len = sizeof(struct nvme_command);
  1160. queue->queue_size = queue_size;
  1161. /*
  1162. * Considered whether we should allocate buffers for all SQEs
  1163. * and CQEs and dma map them - mapping their respective entries
  1164. * into the request structures (kernel vm addr and dma address)
  1165. * thus the driver could use the buffers/mappings directly.
  1166. * It only makes sense if the LLDD would use them for its
  1167. * messaging api. It's very unlikely most adapter api's would use
  1168. * a native NVME sqe/cqe. More reasonable if FC-NVME IU payload
  1169. * structures were used instead.
  1170. */
  1171. }
  1172. /*
  1173. * This routine terminates a queue at the transport level.
  1174. * The transport has already ensured that all outstanding ios on
  1175. * the queue have been terminated.
  1176. * The transport will send a Disconnect LS request to terminate
  1177. * the queue's connection. Termination of the admin queue will also
  1178. * terminate the association at the target.
  1179. */
  1180. static void
  1181. nvme_fc_free_queue(struct nvme_fc_queue *queue)
  1182. {
  1183. if (!test_and_clear_bit(NVME_FC_Q_CONNECTED, &queue->flags))
  1184. return;
  1185. /*
  1186. * Current implementation never disconnects a single queue.
  1187. * It always terminates a whole association. So there is never
  1188. * a disconnect(queue) LS sent to the target.
  1189. */
  1190. queue->connection_id = 0;
  1191. clear_bit(NVME_FC_Q_CONNECTED, &queue->flags);
  1192. }
  1193. static void
  1194. __nvme_fc_delete_hw_queue(struct nvme_fc_ctrl *ctrl,
  1195. struct nvme_fc_queue *queue, unsigned int qidx)
  1196. {
  1197. if (ctrl->lport->ops->delete_queue)
  1198. ctrl->lport->ops->delete_queue(&ctrl->lport->localport, qidx,
  1199. queue->lldd_handle);
  1200. queue->lldd_handle = NULL;
  1201. }
  1202. static void
  1203. nvme_fc_destroy_admin_queue(struct nvme_fc_ctrl *ctrl)
  1204. {
  1205. __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
  1206. blk_cleanup_queue(ctrl->ctrl.admin_q);
  1207. blk_mq_free_tag_set(&ctrl->admin_tag_set);
  1208. nvme_fc_free_queue(&ctrl->queues[0]);
  1209. }
  1210. static void
  1211. nvme_fc_free_io_queues(struct nvme_fc_ctrl *ctrl)
  1212. {
  1213. int i;
  1214. for (i = 1; i < ctrl->queue_count; i++)
  1215. nvme_fc_free_queue(&ctrl->queues[i]);
  1216. }
  1217. static int
  1218. __nvme_fc_create_hw_queue(struct nvme_fc_ctrl *ctrl,
  1219. struct nvme_fc_queue *queue, unsigned int qidx, u16 qsize)
  1220. {
  1221. int ret = 0;
  1222. queue->lldd_handle = NULL;
  1223. if (ctrl->lport->ops->create_queue)
  1224. ret = ctrl->lport->ops->create_queue(&ctrl->lport->localport,
  1225. qidx, qsize, &queue->lldd_handle);
  1226. return ret;
  1227. }
  1228. static void
  1229. nvme_fc_delete_hw_io_queues(struct nvme_fc_ctrl *ctrl)
  1230. {
  1231. struct nvme_fc_queue *queue = &ctrl->queues[ctrl->queue_count - 1];
  1232. int i;
  1233. for (i = ctrl->queue_count - 1; i >= 1; i--, queue--)
  1234. __nvme_fc_delete_hw_queue(ctrl, queue, i);
  1235. }
  1236. static int
  1237. nvme_fc_create_hw_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
  1238. {
  1239. struct nvme_fc_queue *queue = &ctrl->queues[1];
  1240. int i, ret;
  1241. for (i = 1; i < ctrl->queue_count; i++, queue++) {
  1242. ret = __nvme_fc_create_hw_queue(ctrl, queue, i, qsize);
  1243. if (ret)
  1244. goto delete_queues;
  1245. }
  1246. return 0;
  1247. delete_queues:
  1248. for (; i >= 0; i--)
  1249. __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[i], i);
  1250. return ret;
  1251. }
  1252. static int
  1253. nvme_fc_connect_io_queues(struct nvme_fc_ctrl *ctrl, u16 qsize)
  1254. {
  1255. int i, ret = 0;
  1256. for (i = 1; i < ctrl->queue_count; i++) {
  1257. ret = nvme_fc_connect_queue(ctrl, &ctrl->queues[i], qsize,
  1258. (qsize / 5));
  1259. if (ret)
  1260. break;
  1261. ret = nvmf_connect_io_queue(&ctrl->ctrl, i);
  1262. if (ret)
  1263. break;
  1264. }
  1265. return ret;
  1266. }
  1267. static void
  1268. nvme_fc_init_io_queues(struct nvme_fc_ctrl *ctrl)
  1269. {
  1270. int i;
  1271. for (i = 1; i < ctrl->queue_count; i++)
  1272. nvme_fc_init_queue(ctrl, i, ctrl->ctrl.sqsize);
  1273. }
  1274. static void
  1275. nvme_fc_ctrl_free(struct kref *ref)
  1276. {
  1277. struct nvme_fc_ctrl *ctrl =
  1278. container_of(ref, struct nvme_fc_ctrl, ref);
  1279. unsigned long flags;
  1280. if (ctrl->state != FCCTRL_INIT) {
  1281. /* remove from rport list */
  1282. spin_lock_irqsave(&ctrl->rport->lock, flags);
  1283. list_del(&ctrl->ctrl_list);
  1284. spin_unlock_irqrestore(&ctrl->rport->lock, flags);
  1285. }
  1286. put_device(ctrl->dev);
  1287. nvme_fc_rport_put(ctrl->rport);
  1288. kfree(ctrl->queues);
  1289. ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
  1290. nvmf_free_options(ctrl->ctrl.opts);
  1291. kfree(ctrl);
  1292. }
  1293. static void
  1294. nvme_fc_ctrl_put(struct nvme_fc_ctrl *ctrl)
  1295. {
  1296. kref_put(&ctrl->ref, nvme_fc_ctrl_free);
  1297. }
  1298. static int
  1299. nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl)
  1300. {
  1301. return kref_get_unless_zero(&ctrl->ref);
  1302. }
  1303. /*
  1304. * All accesses from nvme core layer done - can now free the
  1305. * controller. Called after last nvme_put_ctrl() call
  1306. */
  1307. static void
  1308. nvme_fc_free_nvme_ctrl(struct nvme_ctrl *nctrl)
  1309. {
  1310. struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
  1311. WARN_ON(nctrl != &ctrl->ctrl);
  1312. /*
  1313. * Tear down the association, which will generate link
  1314. * traffic to terminate connections
  1315. */
  1316. if (ctrl->state != FCCTRL_INIT) {
  1317. /* send a Disconnect(association) LS to fc-nvme target */
  1318. nvme_fc_xmt_disconnect_assoc(ctrl);
  1319. if (ctrl->ctrl.tagset) {
  1320. blk_cleanup_queue(ctrl->ctrl.connect_q);
  1321. blk_mq_free_tag_set(&ctrl->tag_set);
  1322. nvme_fc_delete_hw_io_queues(ctrl);
  1323. nvme_fc_free_io_queues(ctrl);
  1324. }
  1325. nvme_fc_exit_aen_ops(ctrl);
  1326. nvme_fc_destroy_admin_queue(ctrl);
  1327. }
  1328. nvme_fc_ctrl_put(ctrl);
  1329. }
  1330. static int
  1331. __nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op)
  1332. {
  1333. int state;
  1334. state = atomic_xchg(&op->state, FCPOP_STATE_ABORTED);
  1335. if (state != FCPOP_STATE_ACTIVE) {
  1336. atomic_set(&op->state, state);
  1337. return -ECANCELED; /* fail */
  1338. }
  1339. ctrl->lport->ops->fcp_abort(&ctrl->lport->localport,
  1340. &ctrl->rport->remoteport,
  1341. op->queue->lldd_handle,
  1342. &op->fcp_req);
  1343. return 0;
  1344. }
  1345. enum blk_eh_timer_return
  1346. nvme_fc_timeout(struct request *rq, bool reserved)
  1347. {
  1348. struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
  1349. struct nvme_fc_ctrl *ctrl = op->ctrl;
  1350. int ret;
  1351. if (reserved)
  1352. return BLK_EH_RESET_TIMER;
  1353. ret = __nvme_fc_abort_op(ctrl, op);
  1354. if (ret)
  1355. /* io wasn't active to abort consider it done */
  1356. return BLK_EH_HANDLED;
  1357. /*
  1358. * TODO: force a controller reset
  1359. * when that happens, queues will be torn down and outstanding
  1360. * ios will be terminated, and the above abort, on a single io
  1361. * will no longer be needed.
  1362. */
  1363. return BLK_EH_HANDLED;
  1364. }
  1365. static int
  1366. nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
  1367. struct nvme_fc_fcp_op *op)
  1368. {
  1369. struct nvmefc_fcp_req *freq = &op->fcp_req;
  1370. enum dma_data_direction dir;
  1371. int ret;
  1372. freq->sg_cnt = 0;
  1373. if (!blk_rq_payload_bytes(rq))
  1374. return 0;
  1375. freq->sg_table.sgl = freq->first_sgl;
  1376. ret = sg_alloc_table_chained(&freq->sg_table, rq->nr_phys_segments,
  1377. freq->sg_table.sgl);
  1378. if (ret)
  1379. return -ENOMEM;
  1380. op->nents = blk_rq_map_sg(rq->q, rq, freq->sg_table.sgl);
  1381. WARN_ON(op->nents > rq->nr_phys_segments);
  1382. dir = (rq_data_dir(rq) == WRITE) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
  1383. freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
  1384. op->nents, dir);
  1385. if (unlikely(freq->sg_cnt <= 0)) {
  1386. sg_free_table_chained(&freq->sg_table, true);
  1387. freq->sg_cnt = 0;
  1388. return -EFAULT;
  1389. }
  1390. /*
  1391. * TODO: blk_integrity_rq(rq) for DIF
  1392. */
  1393. return 0;
  1394. }
  1395. static void
  1396. nvme_fc_unmap_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
  1397. struct nvme_fc_fcp_op *op)
  1398. {
  1399. struct nvmefc_fcp_req *freq = &op->fcp_req;
  1400. if (!freq->sg_cnt)
  1401. return;
  1402. fc_dma_unmap_sg(ctrl->lport->dev, freq->sg_table.sgl, op->nents,
  1403. ((rq_data_dir(rq) == WRITE) ?
  1404. DMA_TO_DEVICE : DMA_FROM_DEVICE));
  1405. nvme_cleanup_cmd(rq);
  1406. sg_free_table_chained(&freq->sg_table, true);
  1407. freq->sg_cnt = 0;
  1408. }
  1409. /*
  1410. * In FC, the queue is a logical thing. At transport connect, the target
  1411. * creates its "queue" and returns a handle that is to be given to the
  1412. * target whenever it posts something to the corresponding SQ. When an
  1413. * SQE is sent on a SQ, FC effectively considers the SQE, or rather the
  1414. * command contained within the SQE, an io, and assigns a FC exchange
  1415. * to it. The SQE and the associated SQ handle are sent in the initial
  1416. * CMD IU sents on the exchange. All transfers relative to the io occur
  1417. * as part of the exchange. The CQE is the last thing for the io,
  1418. * which is transferred (explicitly or implicitly) with the RSP IU
  1419. * sent on the exchange. After the CQE is received, the FC exchange is
  1420. * terminaed and the Exchange may be used on a different io.
  1421. *
  1422. * The transport to LLDD api has the transport making a request for a
  1423. * new fcp io request to the LLDD. The LLDD then allocates a FC exchange
  1424. * resource and transfers the command. The LLDD will then process all
  1425. * steps to complete the io. Upon completion, the transport done routine
  1426. * is called.
  1427. *
  1428. * So - while the operation is outstanding to the LLDD, there is a link
  1429. * level FC exchange resource that is also outstanding. This must be
  1430. * considered in all cleanup operations.
  1431. */
  1432. static int
  1433. nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
  1434. struct nvme_fc_fcp_op *op, u32 data_len,
  1435. enum nvmefc_fcp_datadir io_dir)
  1436. {
  1437. struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
  1438. struct nvme_command *sqe = &cmdiu->sqe;
  1439. u32 csn;
  1440. int ret;
  1441. if (!nvme_fc_ctrl_get(ctrl))
  1442. return BLK_MQ_RQ_QUEUE_ERROR;
  1443. /* format the FC-NVME CMD IU and fcp_req */
  1444. cmdiu->connection_id = cpu_to_be64(queue->connection_id);
  1445. csn = atomic_inc_return(&queue->csn);
  1446. cmdiu->csn = cpu_to_be32(csn);
  1447. cmdiu->data_len = cpu_to_be32(data_len);
  1448. switch (io_dir) {
  1449. case NVMEFC_FCP_WRITE:
  1450. cmdiu->flags = FCNVME_CMD_FLAGS_WRITE;
  1451. break;
  1452. case NVMEFC_FCP_READ:
  1453. cmdiu->flags = FCNVME_CMD_FLAGS_READ;
  1454. break;
  1455. case NVMEFC_FCP_NODATA:
  1456. cmdiu->flags = 0;
  1457. break;
  1458. }
  1459. op->fcp_req.payload_length = data_len;
  1460. op->fcp_req.io_dir = io_dir;
  1461. op->fcp_req.transferred_length = 0;
  1462. op->fcp_req.rcv_rsplen = 0;
  1463. op->fcp_req.status = 0;
  1464. op->fcp_req.sqid = cpu_to_le16(queue->qnum);
  1465. /*
  1466. * validate per fabric rules, set fields mandated by fabric spec
  1467. * as well as those by FC-NVME spec.
  1468. */
  1469. WARN_ON_ONCE(sqe->common.metadata);
  1470. WARN_ON_ONCE(sqe->common.dptr.prp1);
  1471. WARN_ON_ONCE(sqe->common.dptr.prp2);
  1472. sqe->common.flags |= NVME_CMD_SGL_METABUF;
  1473. /*
  1474. * format SQE DPTR field per FC-NVME rules
  1475. * type=data block descr; subtype=offset;
  1476. * offset is currently 0.
  1477. */
  1478. sqe->rw.dptr.sgl.type = NVME_SGL_FMT_OFFSET;
  1479. sqe->rw.dptr.sgl.length = cpu_to_le32(data_len);
  1480. sqe->rw.dptr.sgl.addr = 0;
  1481. /* odd that we set the command_id - should come from nvme-fabrics */
  1482. WARN_ON_ONCE(sqe->common.command_id != cpu_to_le16(op->rqno));
  1483. if (op->rq) { /* skipped on aens */
  1484. ret = nvme_fc_map_data(ctrl, op->rq, op);
  1485. if (ret < 0) {
  1486. dev_err(queue->ctrl->ctrl.device,
  1487. "Failed to map data (%d)\n", ret);
  1488. nvme_cleanup_cmd(op->rq);
  1489. nvme_fc_ctrl_put(ctrl);
  1490. return (ret == -ENOMEM || ret == -EAGAIN) ?
  1491. BLK_MQ_RQ_QUEUE_BUSY : BLK_MQ_RQ_QUEUE_ERROR;
  1492. }
  1493. }
  1494. fc_dma_sync_single_for_device(ctrl->lport->dev, op->fcp_req.cmddma,
  1495. sizeof(op->cmd_iu), DMA_TO_DEVICE);
  1496. atomic_set(&op->state, FCPOP_STATE_ACTIVE);
  1497. if (op->rq)
  1498. blk_mq_start_request(op->rq);
  1499. ret = ctrl->lport->ops->fcp_io(&ctrl->lport->localport,
  1500. &ctrl->rport->remoteport,
  1501. queue->lldd_handle, &op->fcp_req);
  1502. if (ret) {
  1503. dev_err(ctrl->dev,
  1504. "Send nvme command failed - lldd returned %d.\n", ret);
  1505. if (op->rq) { /* normal request */
  1506. nvme_fc_unmap_data(ctrl, op->rq, op);
  1507. nvme_cleanup_cmd(op->rq);
  1508. }
  1509. /* else - aen. no cleanup needed */
  1510. nvme_fc_ctrl_put(ctrl);
  1511. if (ret != -EBUSY)
  1512. return BLK_MQ_RQ_QUEUE_ERROR;
  1513. if (op->rq) {
  1514. blk_mq_stop_hw_queues(op->rq->q);
  1515. blk_mq_delay_queue(queue->hctx, NVMEFC_QUEUE_DELAY);
  1516. }
  1517. return BLK_MQ_RQ_QUEUE_BUSY;
  1518. }
  1519. return BLK_MQ_RQ_QUEUE_OK;
  1520. }
  1521. static int
  1522. nvme_fc_queue_rq(struct blk_mq_hw_ctx *hctx,
  1523. const struct blk_mq_queue_data *bd)
  1524. {
  1525. struct nvme_ns *ns = hctx->queue->queuedata;
  1526. struct nvme_fc_queue *queue = hctx->driver_data;
  1527. struct nvme_fc_ctrl *ctrl = queue->ctrl;
  1528. struct request *rq = bd->rq;
  1529. struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
  1530. struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
  1531. struct nvme_command *sqe = &cmdiu->sqe;
  1532. enum nvmefc_fcp_datadir io_dir;
  1533. u32 data_len;
  1534. int ret;
  1535. ret = nvme_setup_cmd(ns, rq, sqe);
  1536. if (ret)
  1537. return ret;
  1538. data_len = blk_rq_payload_bytes(rq);
  1539. if (data_len)
  1540. io_dir = ((rq_data_dir(rq) == WRITE) ?
  1541. NVMEFC_FCP_WRITE : NVMEFC_FCP_READ);
  1542. else
  1543. io_dir = NVMEFC_FCP_NODATA;
  1544. return nvme_fc_start_fcp_op(ctrl, queue, op, data_len, io_dir);
  1545. }
  1546. static struct blk_mq_tags *
  1547. nvme_fc_tagset(struct nvme_fc_queue *queue)
  1548. {
  1549. if (queue->qnum == 0)
  1550. return queue->ctrl->admin_tag_set.tags[queue->qnum];
  1551. return queue->ctrl->tag_set.tags[queue->qnum - 1];
  1552. }
  1553. static int
  1554. nvme_fc_poll(struct blk_mq_hw_ctx *hctx, unsigned int tag)
  1555. {
  1556. struct nvme_fc_queue *queue = hctx->driver_data;
  1557. struct nvme_fc_ctrl *ctrl = queue->ctrl;
  1558. struct request *req;
  1559. struct nvme_fc_fcp_op *op;
  1560. req = blk_mq_tag_to_rq(nvme_fc_tagset(queue), tag);
  1561. if (!req) {
  1562. dev_err(queue->ctrl->ctrl.device,
  1563. "tag 0x%x on QNum %#x not found\n",
  1564. tag, queue->qnum);
  1565. return 0;
  1566. }
  1567. op = blk_mq_rq_to_pdu(req);
  1568. if ((atomic_read(&op->state) == FCPOP_STATE_ACTIVE) &&
  1569. (ctrl->lport->ops->poll_queue))
  1570. ctrl->lport->ops->poll_queue(&ctrl->lport->localport,
  1571. queue->lldd_handle);
  1572. return ((atomic_read(&op->state) != FCPOP_STATE_ACTIVE));
  1573. }
  1574. static void
  1575. nvme_fc_submit_async_event(struct nvme_ctrl *arg, int aer_idx)
  1576. {
  1577. struct nvme_fc_ctrl *ctrl = to_fc_ctrl(arg);
  1578. struct nvme_fc_fcp_op *aen_op;
  1579. int ret;
  1580. if (aer_idx > NVME_FC_NR_AEN_COMMANDS)
  1581. return;
  1582. aen_op = &ctrl->aen_ops[aer_idx];
  1583. ret = nvme_fc_start_fcp_op(ctrl, aen_op->queue, aen_op, 0,
  1584. NVMEFC_FCP_NODATA);
  1585. if (ret)
  1586. dev_err(ctrl->ctrl.device,
  1587. "failed async event work [%d]\n", aer_idx);
  1588. }
  1589. static void
  1590. nvme_fc_complete_rq(struct request *rq)
  1591. {
  1592. struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
  1593. struct nvme_fc_ctrl *ctrl = op->ctrl;
  1594. int error = 0, state;
  1595. state = atomic_xchg(&op->state, FCPOP_STATE_IDLE);
  1596. nvme_cleanup_cmd(rq);
  1597. nvme_fc_unmap_data(ctrl, rq, op);
  1598. if (unlikely(rq->errors)) {
  1599. if (nvme_req_needs_retry(rq, rq->errors)) {
  1600. nvme_requeue_req(rq);
  1601. return;
  1602. }
  1603. if (rq->cmd_type == REQ_TYPE_DRV_PRIV)
  1604. error = rq->errors;
  1605. else
  1606. error = nvme_error_status(rq->errors);
  1607. }
  1608. nvme_fc_ctrl_put(ctrl);
  1609. blk_mq_end_request(rq, error);
  1610. }
  1611. static struct blk_mq_ops nvme_fc_mq_ops = {
  1612. .queue_rq = nvme_fc_queue_rq,
  1613. .complete = nvme_fc_complete_rq,
  1614. .init_request = nvme_fc_init_request,
  1615. .exit_request = nvme_fc_exit_request,
  1616. .reinit_request = nvme_fc_reinit_request,
  1617. .init_hctx = nvme_fc_init_hctx,
  1618. .poll = nvme_fc_poll,
  1619. .timeout = nvme_fc_timeout,
  1620. };
  1621. static struct blk_mq_ops nvme_fc_admin_mq_ops = {
  1622. .queue_rq = nvme_fc_queue_rq,
  1623. .complete = nvme_fc_complete_rq,
  1624. .init_request = nvme_fc_init_admin_request,
  1625. .exit_request = nvme_fc_exit_request,
  1626. .reinit_request = nvme_fc_reinit_request,
  1627. .init_hctx = nvme_fc_init_admin_hctx,
  1628. .timeout = nvme_fc_timeout,
  1629. };
  1630. static int
  1631. nvme_fc_configure_admin_queue(struct nvme_fc_ctrl *ctrl)
  1632. {
  1633. u32 segs;
  1634. int error;
  1635. nvme_fc_init_queue(ctrl, 0, NVME_FC_AQ_BLKMQ_DEPTH);
  1636. error = nvme_fc_connect_admin_queue(ctrl, &ctrl->queues[0],
  1637. NVME_FC_AQ_BLKMQ_DEPTH,
  1638. (NVME_FC_AQ_BLKMQ_DEPTH / 4));
  1639. if (error)
  1640. return error;
  1641. memset(&ctrl->admin_tag_set, 0, sizeof(ctrl->admin_tag_set));
  1642. ctrl->admin_tag_set.ops = &nvme_fc_admin_mq_ops;
  1643. ctrl->admin_tag_set.queue_depth = NVME_FC_AQ_BLKMQ_DEPTH;
  1644. ctrl->admin_tag_set.reserved_tags = 2; /* fabric connect + Keep-Alive */
  1645. ctrl->admin_tag_set.numa_node = NUMA_NO_NODE;
  1646. ctrl->admin_tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) +
  1647. (SG_CHUNK_SIZE *
  1648. sizeof(struct scatterlist)) +
  1649. ctrl->lport->ops->fcprqst_priv_sz;
  1650. ctrl->admin_tag_set.driver_data = ctrl;
  1651. ctrl->admin_tag_set.nr_hw_queues = 1;
  1652. ctrl->admin_tag_set.timeout = ADMIN_TIMEOUT;
  1653. error = blk_mq_alloc_tag_set(&ctrl->admin_tag_set);
  1654. if (error)
  1655. goto out_free_queue;
  1656. ctrl->ctrl.admin_q = blk_mq_init_queue(&ctrl->admin_tag_set);
  1657. if (IS_ERR(ctrl->ctrl.admin_q)) {
  1658. error = PTR_ERR(ctrl->ctrl.admin_q);
  1659. goto out_free_tagset;
  1660. }
  1661. error = __nvme_fc_create_hw_queue(ctrl, &ctrl->queues[0], 0,
  1662. NVME_FC_AQ_BLKMQ_DEPTH);
  1663. if (error)
  1664. goto out_cleanup_queue;
  1665. error = nvmf_connect_admin_queue(&ctrl->ctrl);
  1666. if (error)
  1667. goto out_delete_hw_queue;
  1668. error = nvmf_reg_read64(&ctrl->ctrl, NVME_REG_CAP, &ctrl->cap);
  1669. if (error) {
  1670. dev_err(ctrl->ctrl.device,
  1671. "prop_get NVME_REG_CAP failed\n");
  1672. goto out_delete_hw_queue;
  1673. }
  1674. ctrl->ctrl.sqsize =
  1675. min_t(int, NVME_CAP_MQES(ctrl->cap) + 1, ctrl->ctrl.sqsize);
  1676. error = nvme_enable_ctrl(&ctrl->ctrl, ctrl->cap);
  1677. if (error)
  1678. goto out_delete_hw_queue;
  1679. segs = min_t(u32, NVME_FC_MAX_SEGMENTS,
  1680. ctrl->lport->ops->max_sgl_segments);
  1681. ctrl->ctrl.max_hw_sectors = (segs - 1) << (PAGE_SHIFT - 9);
  1682. error = nvme_init_identify(&ctrl->ctrl);
  1683. if (error)
  1684. goto out_delete_hw_queue;
  1685. nvme_start_keep_alive(&ctrl->ctrl);
  1686. return 0;
  1687. out_delete_hw_queue:
  1688. __nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
  1689. out_cleanup_queue:
  1690. blk_cleanup_queue(ctrl->ctrl.admin_q);
  1691. out_free_tagset:
  1692. blk_mq_free_tag_set(&ctrl->admin_tag_set);
  1693. out_free_queue:
  1694. nvme_fc_free_queue(&ctrl->queues[0]);
  1695. return error;
  1696. }
  1697. /*
  1698. * This routine is used by the transport when it needs to find active
  1699. * io on a queue that is to be terminated. The transport uses
  1700. * blk_mq_tagset_busy_itr() to find the busy requests, which then invoke
  1701. * this routine to kill them on a 1 by 1 basis.
  1702. *
  1703. * As FC allocates FC exchange for each io, the transport must contact
  1704. * the LLDD to terminate the exchange, thus releasing the FC exchange.
  1705. * After terminating the exchange the LLDD will call the transport's
  1706. * normal io done path for the request, but it will have an aborted
  1707. * status. The done path will return the io request back to the block
  1708. * layer with an error status.
  1709. */
  1710. static void
  1711. nvme_fc_terminate_exchange(struct request *req, void *data, bool reserved)
  1712. {
  1713. struct nvme_ctrl *nctrl = data;
  1714. struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
  1715. struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
  1716. int status;
  1717. if (!blk_mq_request_started(req))
  1718. return;
  1719. /* this performs an ABTS-LS on the FC exchange for the io */
  1720. status = __nvme_fc_abort_op(ctrl, op);
  1721. /*
  1722. * if __nvme_fc_abort_op failed: io wasn't active to abort
  1723. * consider it done. Assume completion path already completing
  1724. * in parallel
  1725. */
  1726. if (status)
  1727. /* io wasn't active to abort consider it done */
  1728. /* assume completion path already completing in parallel */
  1729. return;
  1730. }
  1731. /*
  1732. * This routine stops operation of the controller. Admin and IO queues
  1733. * are stopped, outstanding ios on them terminated, and the nvme ctrl
  1734. * is shutdown.
  1735. */
  1736. static void
  1737. nvme_fc_shutdown_ctrl(struct nvme_fc_ctrl *ctrl)
  1738. {
  1739. /*
  1740. * If io queues are present, stop them and terminate all outstanding
  1741. * ios on them. As FC allocates FC exchange for each io, the
  1742. * transport must contact the LLDD to terminate the exchange,
  1743. * thus releasing the FC exchange. We use blk_mq_tagset_busy_itr()
  1744. * to tell us what io's are busy and invoke a transport routine
  1745. * to kill them with the LLDD. After terminating the exchange
  1746. * the LLDD will call the transport's normal io done path, but it
  1747. * will have an aborted status. The done path will return the
  1748. * io requests back to the block layer as part of normal completions
  1749. * (but with error status).
  1750. */
  1751. if (ctrl->queue_count > 1) {
  1752. nvme_stop_queues(&ctrl->ctrl);
  1753. blk_mq_tagset_busy_iter(&ctrl->tag_set,
  1754. nvme_fc_terminate_exchange, &ctrl->ctrl);
  1755. }
  1756. if (ctrl->ctrl.state == NVME_CTRL_LIVE)
  1757. nvme_shutdown_ctrl(&ctrl->ctrl);
  1758. /*
  1759. * now clean up the admin queue. Same thing as above.
  1760. * use blk_mq_tagset_busy_itr() and the transport routine to
  1761. * terminate the exchanges.
  1762. */
  1763. blk_mq_stop_hw_queues(ctrl->ctrl.admin_q);
  1764. blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
  1765. nvme_fc_terminate_exchange, &ctrl->ctrl);
  1766. }
  1767. /*
  1768. * Called to teardown an association.
  1769. * May be called with association fully in place or partially in place.
  1770. */
  1771. static void
  1772. __nvme_fc_remove_ctrl(struct nvme_fc_ctrl *ctrl)
  1773. {
  1774. nvme_stop_keep_alive(&ctrl->ctrl);
  1775. /* stop and terminate ios on admin and io queues */
  1776. nvme_fc_shutdown_ctrl(ctrl);
  1777. /*
  1778. * tear down the controller
  1779. * This will result in the last reference on the nvme ctrl to
  1780. * expire, calling the transport nvme_fc_free_nvme_ctrl() callback.
  1781. * From there, the transport will tear down it's logical queues and
  1782. * association.
  1783. */
  1784. nvme_uninit_ctrl(&ctrl->ctrl);
  1785. nvme_put_ctrl(&ctrl->ctrl);
  1786. }
  1787. static void
  1788. nvme_fc_del_ctrl_work(struct work_struct *work)
  1789. {
  1790. struct nvme_fc_ctrl *ctrl =
  1791. container_of(work, struct nvme_fc_ctrl, delete_work);
  1792. __nvme_fc_remove_ctrl(ctrl);
  1793. }
  1794. static int
  1795. __nvme_fc_del_ctrl(struct nvme_fc_ctrl *ctrl)
  1796. {
  1797. if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_DELETING))
  1798. return -EBUSY;
  1799. if (!queue_work(nvme_fc_wq, &ctrl->delete_work))
  1800. return -EBUSY;
  1801. return 0;
  1802. }
  1803. /*
  1804. * Request from nvme core layer to delete the controller
  1805. */
  1806. static int
  1807. nvme_fc_del_nvme_ctrl(struct nvme_ctrl *nctrl)
  1808. {
  1809. struct nvme_fc_ctrl *ctrl = to_fc_ctrl(nctrl);
  1810. struct nvme_fc_rport *rport = ctrl->rport;
  1811. unsigned long flags;
  1812. int ret;
  1813. spin_lock_irqsave(&rport->lock, flags);
  1814. ret = __nvme_fc_del_ctrl(ctrl);
  1815. spin_unlock_irqrestore(&rport->lock, flags);
  1816. if (ret)
  1817. return ret;
  1818. flush_work(&ctrl->delete_work);
  1819. return 0;
  1820. }
  1821. static int
  1822. nvme_fc_reset_nvme_ctrl(struct nvme_ctrl *nctrl)
  1823. {
  1824. return -EIO;
  1825. }
  1826. static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
  1827. .name = "fc",
  1828. .module = THIS_MODULE,
  1829. .is_fabrics = true,
  1830. .reg_read32 = nvmf_reg_read32,
  1831. .reg_read64 = nvmf_reg_read64,
  1832. .reg_write32 = nvmf_reg_write32,
  1833. .reset_ctrl = nvme_fc_reset_nvme_ctrl,
  1834. .free_ctrl = nvme_fc_free_nvme_ctrl,
  1835. .submit_async_event = nvme_fc_submit_async_event,
  1836. .delete_ctrl = nvme_fc_del_nvme_ctrl,
  1837. .get_subsysnqn = nvmf_get_subsysnqn,
  1838. .get_address = nvmf_get_address,
  1839. };
  1840. static int
  1841. nvme_fc_create_io_queues(struct nvme_fc_ctrl *ctrl)
  1842. {
  1843. struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
  1844. int ret;
  1845. ret = nvme_set_queue_count(&ctrl->ctrl, &opts->nr_io_queues);
  1846. if (ret) {
  1847. dev_info(ctrl->ctrl.device,
  1848. "set_queue_count failed: %d\n", ret);
  1849. return ret;
  1850. }
  1851. ctrl->queue_count = opts->nr_io_queues + 1;
  1852. if (!opts->nr_io_queues)
  1853. return 0;
  1854. dev_info(ctrl->ctrl.device, "creating %d I/O queues.\n",
  1855. opts->nr_io_queues);
  1856. nvme_fc_init_io_queues(ctrl);
  1857. memset(&ctrl->tag_set, 0, sizeof(ctrl->tag_set));
  1858. ctrl->tag_set.ops = &nvme_fc_mq_ops;
  1859. ctrl->tag_set.queue_depth = ctrl->ctrl.opts->queue_size;
  1860. ctrl->tag_set.reserved_tags = 1; /* fabric connect */
  1861. ctrl->tag_set.numa_node = NUMA_NO_NODE;
  1862. ctrl->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
  1863. ctrl->tag_set.cmd_size = sizeof(struct nvme_fc_fcp_op) +
  1864. (SG_CHUNK_SIZE *
  1865. sizeof(struct scatterlist)) +
  1866. ctrl->lport->ops->fcprqst_priv_sz;
  1867. ctrl->tag_set.driver_data = ctrl;
  1868. ctrl->tag_set.nr_hw_queues = ctrl->queue_count - 1;
  1869. ctrl->tag_set.timeout = NVME_IO_TIMEOUT;
  1870. ret = blk_mq_alloc_tag_set(&ctrl->tag_set);
  1871. if (ret)
  1872. return ret;
  1873. ctrl->ctrl.tagset = &ctrl->tag_set;
  1874. ctrl->ctrl.connect_q = blk_mq_init_queue(&ctrl->tag_set);
  1875. if (IS_ERR(ctrl->ctrl.connect_q)) {
  1876. ret = PTR_ERR(ctrl->ctrl.connect_q);
  1877. goto out_free_tag_set;
  1878. }
  1879. ret = nvme_fc_create_hw_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
  1880. if (ret)
  1881. goto out_cleanup_blk_queue;
  1882. ret = nvme_fc_connect_io_queues(ctrl, ctrl->ctrl.opts->queue_size);
  1883. if (ret)
  1884. goto out_delete_hw_queues;
  1885. return 0;
  1886. out_delete_hw_queues:
  1887. nvme_fc_delete_hw_io_queues(ctrl);
  1888. out_cleanup_blk_queue:
  1889. nvme_stop_keep_alive(&ctrl->ctrl);
  1890. blk_cleanup_queue(ctrl->ctrl.connect_q);
  1891. out_free_tag_set:
  1892. blk_mq_free_tag_set(&ctrl->tag_set);
  1893. nvme_fc_free_io_queues(ctrl);
  1894. /* force put free routine to ignore io queues */
  1895. ctrl->ctrl.tagset = NULL;
  1896. return ret;
  1897. }
  1898. static struct nvme_ctrl *
  1899. __nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
  1900. struct nvme_fc_lport *lport, struct nvme_fc_rport *rport)
  1901. {
  1902. struct nvme_fc_ctrl *ctrl;
  1903. unsigned long flags;
  1904. int ret, idx;
  1905. bool changed;
  1906. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  1907. if (!ctrl) {
  1908. ret = -ENOMEM;
  1909. goto out_fail;
  1910. }
  1911. idx = ida_simple_get(&nvme_fc_ctrl_cnt, 0, 0, GFP_KERNEL);
  1912. if (idx < 0) {
  1913. ret = -ENOSPC;
  1914. goto out_free_ctrl;
  1915. }
  1916. ctrl->ctrl.opts = opts;
  1917. INIT_LIST_HEAD(&ctrl->ctrl_list);
  1918. INIT_LIST_HEAD(&ctrl->ls_req_list);
  1919. ctrl->lport = lport;
  1920. ctrl->rport = rport;
  1921. ctrl->dev = lport->dev;
  1922. ctrl->state = FCCTRL_INIT;
  1923. ctrl->cnum = idx;
  1924. ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_fc_ctrl_ops, 0);
  1925. if (ret)
  1926. goto out_free_ida;
  1927. get_device(ctrl->dev);
  1928. kref_init(&ctrl->ref);
  1929. INIT_WORK(&ctrl->delete_work, nvme_fc_del_ctrl_work);
  1930. spin_lock_init(&ctrl->lock);
  1931. /* io queue count */
  1932. ctrl->queue_count = min_t(unsigned int,
  1933. opts->nr_io_queues,
  1934. lport->ops->max_hw_queues);
  1935. opts->nr_io_queues = ctrl->queue_count; /* so opts has valid value */
  1936. ctrl->queue_count++; /* +1 for admin queue */
  1937. ctrl->ctrl.sqsize = opts->queue_size - 1;
  1938. ctrl->ctrl.kato = opts->kato;
  1939. ret = -ENOMEM;
  1940. ctrl->queues = kcalloc(ctrl->queue_count, sizeof(struct nvme_fc_queue),
  1941. GFP_KERNEL);
  1942. if (!ctrl->queues)
  1943. goto out_uninit_ctrl;
  1944. ret = nvme_fc_configure_admin_queue(ctrl);
  1945. if (ret)
  1946. goto out_uninit_ctrl;
  1947. /* sanity checks */
  1948. /* FC-NVME supports 64-byte SQE only */
  1949. if (ctrl->ctrl.ioccsz != 4) {
  1950. dev_err(ctrl->ctrl.device, "ioccsz %d is not supported!\n",
  1951. ctrl->ctrl.ioccsz);
  1952. goto out_remove_admin_queue;
  1953. }
  1954. /* FC-NVME supports 16-byte CQE only */
  1955. if (ctrl->ctrl.iorcsz != 1) {
  1956. dev_err(ctrl->ctrl.device, "iorcsz %d is not supported!\n",
  1957. ctrl->ctrl.iorcsz);
  1958. goto out_remove_admin_queue;
  1959. }
  1960. /* FC-NVME does not have other data in the capsule */
  1961. if (ctrl->ctrl.icdoff) {
  1962. dev_err(ctrl->ctrl.device, "icdoff %d is not supported!\n",
  1963. ctrl->ctrl.icdoff);
  1964. goto out_remove_admin_queue;
  1965. }
  1966. /* FC-NVME supports normal SGL Data Block Descriptors */
  1967. if (opts->queue_size > ctrl->ctrl.maxcmd) {
  1968. /* warn if maxcmd is lower than queue_size */
  1969. dev_warn(ctrl->ctrl.device,
  1970. "queue_size %zu > ctrl maxcmd %u, reducing "
  1971. "to queue_size\n",
  1972. opts->queue_size, ctrl->ctrl.maxcmd);
  1973. opts->queue_size = ctrl->ctrl.maxcmd;
  1974. }
  1975. ret = nvme_fc_init_aen_ops(ctrl);
  1976. if (ret)
  1977. goto out_exit_aen_ops;
  1978. if (ctrl->queue_count > 1) {
  1979. ret = nvme_fc_create_io_queues(ctrl);
  1980. if (ret)
  1981. goto out_exit_aen_ops;
  1982. }
  1983. spin_lock_irqsave(&ctrl->lock, flags);
  1984. ctrl->state = FCCTRL_ACTIVE;
  1985. spin_unlock_irqrestore(&ctrl->lock, flags);
  1986. changed = nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_LIVE);
  1987. WARN_ON_ONCE(!changed);
  1988. dev_info(ctrl->ctrl.device,
  1989. "NVME-FC{%d}: new ctrl: NQN \"%s\"\n",
  1990. ctrl->cnum, ctrl->ctrl.opts->subsysnqn);
  1991. kref_get(&ctrl->ctrl.kref);
  1992. spin_lock_irqsave(&rport->lock, flags);
  1993. list_add_tail(&ctrl->ctrl_list, &rport->ctrl_list);
  1994. spin_unlock_irqrestore(&rport->lock, flags);
  1995. if (opts->nr_io_queues) {
  1996. nvme_queue_scan(&ctrl->ctrl);
  1997. nvme_queue_async_events(&ctrl->ctrl);
  1998. }
  1999. return &ctrl->ctrl;
  2000. out_exit_aen_ops:
  2001. nvme_fc_exit_aen_ops(ctrl);
  2002. out_remove_admin_queue:
  2003. /* send a Disconnect(association) LS to fc-nvme target */
  2004. nvme_fc_xmt_disconnect_assoc(ctrl);
  2005. nvme_stop_keep_alive(&ctrl->ctrl);
  2006. nvme_fc_destroy_admin_queue(ctrl);
  2007. out_uninit_ctrl:
  2008. nvme_uninit_ctrl(&ctrl->ctrl);
  2009. nvme_put_ctrl(&ctrl->ctrl);
  2010. if (ret > 0)
  2011. ret = -EIO;
  2012. /* exit via here will follow ctlr ref point callbacks to free */
  2013. return ERR_PTR(ret);
  2014. out_free_ida:
  2015. ida_simple_remove(&nvme_fc_ctrl_cnt, ctrl->cnum);
  2016. out_free_ctrl:
  2017. kfree(ctrl);
  2018. out_fail:
  2019. nvme_fc_rport_put(rport);
  2020. /* exit via here doesn't follow ctlr ref points */
  2021. return ERR_PTR(ret);
  2022. }
  2023. enum {
  2024. FCT_TRADDR_ERR = 0,
  2025. FCT_TRADDR_WWNN = 1 << 0,
  2026. FCT_TRADDR_WWPN = 1 << 1,
  2027. };
  2028. struct nvmet_fc_traddr {
  2029. u64 nn;
  2030. u64 pn;
  2031. };
  2032. static const match_table_t traddr_opt_tokens = {
  2033. { FCT_TRADDR_WWNN, "nn-%s" },
  2034. { FCT_TRADDR_WWPN, "pn-%s" },
  2035. { FCT_TRADDR_ERR, NULL }
  2036. };
  2037. static int
  2038. nvme_fc_parse_address(struct nvmet_fc_traddr *traddr, char *buf)
  2039. {
  2040. substring_t args[MAX_OPT_ARGS];
  2041. char *options, *o, *p;
  2042. int token, ret = 0;
  2043. u64 token64;
  2044. options = o = kstrdup(buf, GFP_KERNEL);
  2045. if (!options)
  2046. return -ENOMEM;
  2047. while ((p = strsep(&o, ":\n")) != NULL) {
  2048. if (!*p)
  2049. continue;
  2050. token = match_token(p, traddr_opt_tokens, args);
  2051. switch (token) {
  2052. case FCT_TRADDR_WWNN:
  2053. if (match_u64(args, &token64)) {
  2054. ret = -EINVAL;
  2055. goto out;
  2056. }
  2057. traddr->nn = token64;
  2058. break;
  2059. case FCT_TRADDR_WWPN:
  2060. if (match_u64(args, &token64)) {
  2061. ret = -EINVAL;
  2062. goto out;
  2063. }
  2064. traddr->pn = token64;
  2065. break;
  2066. default:
  2067. pr_warn("unknown traddr token or missing value '%s'\n",
  2068. p);
  2069. ret = -EINVAL;
  2070. goto out;
  2071. }
  2072. }
  2073. out:
  2074. kfree(options);
  2075. return ret;
  2076. }
  2077. static struct nvme_ctrl *
  2078. nvme_fc_create_ctrl(struct device *dev, struct nvmf_ctrl_options *opts)
  2079. {
  2080. struct nvme_fc_lport *lport;
  2081. struct nvme_fc_rport *rport;
  2082. struct nvmet_fc_traddr laddr = { 0L, 0L };
  2083. struct nvmet_fc_traddr raddr = { 0L, 0L };
  2084. unsigned long flags;
  2085. int ret;
  2086. ret = nvme_fc_parse_address(&raddr, opts->traddr);
  2087. if (ret || !raddr.nn || !raddr.pn)
  2088. return ERR_PTR(-EINVAL);
  2089. ret = nvme_fc_parse_address(&laddr, opts->host_traddr);
  2090. if (ret || !laddr.nn || !laddr.pn)
  2091. return ERR_PTR(-EINVAL);
  2092. /* find the host and remote ports to connect together */
  2093. spin_lock_irqsave(&nvme_fc_lock, flags);
  2094. list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
  2095. if (lport->localport.node_name != laddr.nn ||
  2096. lport->localport.port_name != laddr.pn)
  2097. continue;
  2098. list_for_each_entry(rport, &lport->endp_list, endp_list) {
  2099. if (rport->remoteport.node_name != raddr.nn ||
  2100. rport->remoteport.port_name != raddr.pn)
  2101. continue;
  2102. /* if fail to get reference fall through. Will error */
  2103. if (!nvme_fc_rport_get(rport))
  2104. break;
  2105. spin_unlock_irqrestore(&nvme_fc_lock, flags);
  2106. return __nvme_fc_create_ctrl(dev, opts, lport, rport);
  2107. }
  2108. }
  2109. spin_unlock_irqrestore(&nvme_fc_lock, flags);
  2110. return ERR_PTR(-ENOENT);
  2111. }
  2112. static struct nvmf_transport_ops nvme_fc_transport = {
  2113. .name = "fc",
  2114. .required_opts = NVMF_OPT_TRADDR | NVMF_OPT_HOST_TRADDR,
  2115. .allowed_opts = NVMF_OPT_RECONNECT_DELAY,
  2116. .create_ctrl = nvme_fc_create_ctrl,
  2117. };
  2118. static int __init nvme_fc_init_module(void)
  2119. {
  2120. nvme_fc_wq = create_workqueue("nvme_fc_wq");
  2121. if (!nvme_fc_wq)
  2122. return -ENOMEM;
  2123. nvmf_register_transport(&nvme_fc_transport);
  2124. return 0;
  2125. }
  2126. static void __exit nvme_fc_exit_module(void)
  2127. {
  2128. /* sanity check - all lports should be removed */
  2129. if (!list_empty(&nvme_fc_lport_list))
  2130. pr_warn("%s: localport list not empty\n", __func__);
  2131. nvmf_unregister_transport(&nvme_fc_transport);
  2132. destroy_workqueue(nvme_fc_wq);
  2133. ida_destroy(&nvme_fc_local_port_cnt);
  2134. ida_destroy(&nvme_fc_ctrl_cnt);
  2135. }
  2136. module_init(nvme_fc_init_module);
  2137. module_exit(nvme_fc_exit_module);
  2138. MODULE_LICENSE("GPL v2");