fc.c 107 KB

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