fc.c 89 KB

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