fc.c 92 KB

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