fc.c 79 KB

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