fc.c 91 KB

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