fc.c 88 KB

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