fc.c 77 KB

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