fc.c 106 KB

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