fc.c 81 KB

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