fc.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2016 Avago Technologies. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/module.h>
  7. #include <linux/slab.h>
  8. #include <linux/blk-mq.h>
  9. #include <linux/parser.h>
  10. #include <linux/random.h>
  11. #include <uapi/scsi/fc/fc_fs.h>
  12. #include <uapi/scsi/fc/fc_els.h>
  13. #include "nvmet.h"
  14. #include <linux/nvme-fc-driver.h>
  15. #include <linux/nvme-fc.h>
  16. #include "../host/fc.h"
  17. /* *************************** Data Structures/Defines ****************** */
  18. #define NVMET_LS_CTX_COUNT 256
  19. struct nvmet_fc_tgtport;
  20. struct nvmet_fc_tgt_assoc;
  21. struct nvmet_fc_ls_iod { /* for an LS RQST RCV */
  22. struct nvmefc_ls_rsp *lsrsp;
  23. struct nvmefc_tgt_fcp_req *fcpreq; /* only if RS */
  24. struct list_head ls_rcv_list; /* tgtport->ls_rcv_list */
  25. struct nvmet_fc_tgtport *tgtport;
  26. struct nvmet_fc_tgt_assoc *assoc;
  27. void *hosthandle;
  28. union nvmefc_ls_requests *rqstbuf;
  29. union nvmefc_ls_responses *rspbuf;
  30. u16 rqstdatalen;
  31. dma_addr_t rspdma;
  32. struct scatterlist sg[2];
  33. struct work_struct work;
  34. } __aligned(sizeof(unsigned long long));
  35. struct nvmet_fc_ls_req_op { /* for an LS RQST XMT */
  36. struct nvmefc_ls_req ls_req;
  37. struct nvmet_fc_tgtport *tgtport;
  38. void *hosthandle;
  39. int ls_error;
  40. struct list_head lsreq_list; /* tgtport->ls_req_list */
  41. bool req_queued;
  42. struct work_struct put_work;
  43. };
  44. /* desired maximum for a single sequence - if sg list allows it */
  45. #define NVMET_FC_MAX_SEQ_LENGTH (256 * 1024)
  46. enum nvmet_fcp_datadir {
  47. NVMET_FCP_NODATA,
  48. NVMET_FCP_WRITE,
  49. NVMET_FCP_READ,
  50. NVMET_FCP_ABORTED,
  51. };
  52. struct nvmet_fc_fcp_iod {
  53. struct nvmefc_tgt_fcp_req *fcpreq;
  54. struct nvme_fc_cmd_iu cmdiubuf;
  55. struct nvme_fc_ersp_iu rspiubuf;
  56. dma_addr_t rspdma;
  57. struct scatterlist *next_sg;
  58. struct scatterlist *data_sg;
  59. int data_sg_cnt;
  60. u32 offset;
  61. enum nvmet_fcp_datadir io_dir;
  62. bool active;
  63. bool abort;
  64. bool aborted;
  65. bool writedataactive;
  66. spinlock_t flock;
  67. struct nvmet_req req;
  68. struct work_struct defer_work;
  69. struct nvmet_fc_tgtport *tgtport;
  70. struct nvmet_fc_tgt_queue *queue;
  71. struct list_head fcp_list; /* tgtport->fcp_list */
  72. };
  73. struct nvmet_fc_tgtport {
  74. struct nvmet_fc_target_port fc_target_port;
  75. struct list_head tgt_list; /* nvmet_fc_target_list */
  76. struct device *dev; /* dev for dma mapping */
  77. struct nvmet_fc_target_template *ops;
  78. struct nvmet_fc_ls_iod *iod;
  79. spinlock_t lock;
  80. struct list_head ls_rcv_list;
  81. struct list_head ls_req_list;
  82. struct list_head ls_busylist;
  83. struct list_head assoc_list;
  84. struct list_head host_list;
  85. struct ida assoc_cnt;
  86. struct nvmet_fc_port_entry *pe;
  87. struct kref ref;
  88. u32 max_sg_cnt;
  89. };
  90. struct nvmet_fc_port_entry {
  91. struct nvmet_fc_tgtport *tgtport;
  92. struct nvmet_port *port;
  93. u64 node_name;
  94. u64 port_name;
  95. struct list_head pe_list;
  96. };
  97. struct nvmet_fc_defer_fcp_req {
  98. struct list_head req_list;
  99. struct nvmefc_tgt_fcp_req *fcp_req;
  100. };
  101. struct nvmet_fc_tgt_queue {
  102. bool ninetypercent;
  103. u16 qid;
  104. u16 sqsize;
  105. u16 ersp_ratio;
  106. __le16 sqhd;
  107. atomic_t connected;
  108. atomic_t sqtail;
  109. atomic_t zrspcnt;
  110. atomic_t rsn;
  111. spinlock_t qlock;
  112. struct nvmet_cq nvme_cq;
  113. struct nvmet_sq nvme_sq;
  114. struct nvmet_fc_tgt_assoc *assoc;
  115. struct list_head fod_list;
  116. struct list_head pending_cmd_list;
  117. struct list_head avail_defer_list;
  118. struct workqueue_struct *work_q;
  119. struct kref ref;
  120. /* array of fcp_iods */
  121. struct nvmet_fc_fcp_iod fod[] /* __counted_by(sqsize) */;
  122. } __aligned(sizeof(unsigned long long));
  123. struct nvmet_fc_hostport {
  124. struct nvmet_fc_tgtport *tgtport;
  125. void *hosthandle;
  126. struct list_head host_list;
  127. struct kref ref;
  128. u8 invalid;
  129. };
  130. struct nvmet_fc_tgt_assoc {
  131. u64 association_id;
  132. u32 a_id;
  133. atomic_t terminating;
  134. struct nvmet_fc_tgtport *tgtport;
  135. struct nvmet_fc_hostport *hostport;
  136. struct nvmet_fc_ls_iod *rcv_disconn;
  137. struct list_head a_list;
  138. struct nvmet_fc_tgt_queue *queues[NVMET_NR_QUEUES + 1];
  139. struct kref ref;
  140. struct work_struct del_work;
  141. };
  142. /*
  143. * Association and Connection IDs:
  144. *
  145. * Association ID will have random number in upper 6 bytes and zero
  146. * in lower 2 bytes
  147. *
  148. * Connection IDs will be Association ID with QID or'd in lower 2 bytes
  149. *
  150. * note: Association ID = Connection ID for queue 0
  151. */
  152. #define BYTES_FOR_QID sizeof(u16)
  153. #define BYTES_FOR_QID_SHIFT (BYTES_FOR_QID * 8)
  154. #define NVMET_FC_QUEUEID_MASK ((u64)((1 << BYTES_FOR_QID_SHIFT) - 1))
  155. static inline u64
  156. nvmet_fc_makeconnid(struct nvmet_fc_tgt_assoc *assoc, u16 qid)
  157. {
  158. return (assoc->association_id | qid);
  159. }
  160. static inline u64
  161. nvmet_fc_getassociationid(u64 connectionid)
  162. {
  163. return connectionid & ~NVMET_FC_QUEUEID_MASK;
  164. }
  165. static inline u16
  166. nvmet_fc_getqueueid(u64 connectionid)
  167. {
  168. return (u16)(connectionid & NVMET_FC_QUEUEID_MASK);
  169. }
  170. static inline struct nvmet_fc_tgtport *
  171. targetport_to_tgtport(struct nvmet_fc_target_port *targetport)
  172. {
  173. return container_of(targetport, struct nvmet_fc_tgtport,
  174. fc_target_port);
  175. }
  176. static inline struct nvmet_fc_fcp_iod *
  177. nvmet_req_to_fod(struct nvmet_req *nvme_req)
  178. {
  179. return container_of(nvme_req, struct nvmet_fc_fcp_iod, req);
  180. }
  181. /* *************************** Globals **************************** */
  182. static DEFINE_SPINLOCK(nvmet_fc_tgtlock);
  183. static LIST_HEAD(nvmet_fc_target_list);
  184. static DEFINE_IDA(nvmet_fc_tgtport_cnt);
  185. static LIST_HEAD(nvmet_fc_portentry_list);
  186. static void nvmet_fc_handle_ls_rqst_work(struct work_struct *work);
  187. static void nvmet_fc_fcp_rqst_op_defer_work(struct work_struct *work);
  188. static void nvmet_fc_tgt_a_put(struct nvmet_fc_tgt_assoc *assoc);
  189. static int nvmet_fc_tgt_a_get(struct nvmet_fc_tgt_assoc *assoc);
  190. static void nvmet_fc_tgt_q_put(struct nvmet_fc_tgt_queue *queue);
  191. static int nvmet_fc_tgt_q_get(struct nvmet_fc_tgt_queue *queue);
  192. static void nvmet_fc_tgtport_put(struct nvmet_fc_tgtport *tgtport);
  193. static void nvmet_fc_put_lsop_work(struct work_struct *work)
  194. {
  195. struct nvmet_fc_ls_req_op *lsop =
  196. container_of(work, struct nvmet_fc_ls_req_op, put_work);
  197. nvmet_fc_tgtport_put(lsop->tgtport);
  198. kfree(lsop);
  199. }
  200. static int nvmet_fc_tgtport_get(struct nvmet_fc_tgtport *tgtport);
  201. static void nvmet_fc_handle_fcp_rqst(struct nvmet_fc_tgtport *tgtport,
  202. struct nvmet_fc_fcp_iod *fod);
  203. static void nvmet_fc_delete_target_assoc(struct nvmet_fc_tgt_assoc *assoc);
  204. static void nvmet_fc_xmt_ls_rsp(struct nvmet_fc_tgtport *tgtport,
  205. struct nvmet_fc_ls_iod *iod);
  206. /* *********************** FC-NVME DMA Handling **************************** */
  207. /*
  208. * The fcloop device passes in a NULL device pointer. Real LLD's will
  209. * pass in a valid device pointer. If NULL is passed to the dma mapping
  210. * routines, depending on the platform, it may or may not succeed, and
  211. * may crash.
  212. *
  213. * As such:
  214. * Wrapper all the dma routines and check the dev pointer.
  215. *
  216. * If simple mappings (return just a dma address, we'll noop them,
  217. * returning a dma address of 0.
  218. *
  219. * On more complex mappings (dma_map_sg), a pseudo routine fills
  220. * in the scatter list, setting all dma addresses to 0.
  221. */
  222. static inline dma_addr_t
  223. fc_dma_map_single(struct device *dev, void *ptr, size_t size,
  224. enum dma_data_direction dir)
  225. {
  226. return dev ? dma_map_single(dev, ptr, size, dir) : (dma_addr_t)0L;
  227. }
  228. static inline int
  229. fc_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  230. {
  231. return dev ? dma_mapping_error(dev, dma_addr) : 0;
  232. }
  233. static inline void
  234. fc_dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
  235. enum dma_data_direction dir)
  236. {
  237. if (dev)
  238. dma_unmap_single(dev, addr, size, dir);
  239. }
  240. static inline void
  241. fc_dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr, size_t size,
  242. enum dma_data_direction dir)
  243. {
  244. if (dev)
  245. dma_sync_single_for_cpu(dev, addr, size, dir);
  246. }
  247. static inline void
  248. fc_dma_sync_single_for_device(struct device *dev, dma_addr_t addr, size_t size,
  249. enum dma_data_direction dir)
  250. {
  251. if (dev)
  252. dma_sync_single_for_device(dev, addr, size, dir);
  253. }
  254. /* pseudo dma_map_sg call */
  255. static int
  256. fc_map_sg(struct scatterlist *sg, int nents)
  257. {
  258. struct scatterlist *s;
  259. int i;
  260. WARN_ON(nents == 0 || sg[0].length == 0);
  261. for_each_sg(sg, s, nents, i) {
  262. s->dma_address = 0L;
  263. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  264. s->dma_length = s->length;
  265. #endif
  266. }
  267. return nents;
  268. }
  269. static inline int
  270. fc_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  271. enum dma_data_direction dir)
  272. {
  273. return dev ? dma_map_sg(dev, sg, nents, dir) : fc_map_sg(sg, nents);
  274. }
  275. static inline void
  276. fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  277. enum dma_data_direction dir)
  278. {
  279. if (dev)
  280. dma_unmap_sg(dev, sg, nents, dir);
  281. }
  282. /* ********************** FC-NVME LS XMT Handling ************************* */
  283. static void
  284. __nvmet_fc_finish_ls_req(struct nvmet_fc_ls_req_op *lsop)
  285. {
  286. struct nvmet_fc_tgtport *tgtport = lsop->tgtport;
  287. struct nvmefc_ls_req *lsreq = &lsop->ls_req;
  288. unsigned long flags;
  289. spin_lock_irqsave(&tgtport->lock, flags);
  290. if (!lsop->req_queued) {
  291. spin_unlock_irqrestore(&tgtport->lock, flags);
  292. goto out_putwork;
  293. }
  294. list_del(&lsop->lsreq_list);
  295. lsop->req_queued = false;
  296. spin_unlock_irqrestore(&tgtport->lock, flags);
  297. fc_dma_unmap_single(tgtport->dev, lsreq->rqstdma,
  298. (lsreq->rqstlen + lsreq->rsplen),
  299. DMA_BIDIRECTIONAL);
  300. out_putwork:
  301. queue_work(nvmet_wq, &lsop->put_work);
  302. }
  303. static int
  304. __nvmet_fc_send_ls_req(struct nvmet_fc_tgtport *tgtport,
  305. struct nvmet_fc_ls_req_op *lsop,
  306. void (*done)(struct nvmefc_ls_req *req, int status))
  307. {
  308. struct nvmefc_ls_req *lsreq = &lsop->ls_req;
  309. unsigned long flags;
  310. int ret = 0;
  311. if (!tgtport->ops->ls_req)
  312. return -EOPNOTSUPP;
  313. if (!nvmet_fc_tgtport_get(tgtport))
  314. return -ESHUTDOWN;
  315. lsreq->done = done;
  316. lsop->req_queued = false;
  317. INIT_LIST_HEAD(&lsop->lsreq_list);
  318. INIT_WORK(&lsop->put_work, nvmet_fc_put_lsop_work);
  319. lsreq->rqstdma = fc_dma_map_single(tgtport->dev, lsreq->rqstaddr,
  320. lsreq->rqstlen + lsreq->rsplen,
  321. DMA_BIDIRECTIONAL);
  322. if (fc_dma_mapping_error(tgtport->dev, lsreq->rqstdma)) {
  323. ret = -EFAULT;
  324. goto out_puttgtport;
  325. }
  326. lsreq->rspdma = lsreq->rqstdma + lsreq->rqstlen;
  327. spin_lock_irqsave(&tgtport->lock, flags);
  328. list_add_tail(&lsop->lsreq_list, &tgtport->ls_req_list);
  329. lsop->req_queued = true;
  330. spin_unlock_irqrestore(&tgtport->lock, flags);
  331. ret = tgtport->ops->ls_req(&tgtport->fc_target_port, lsop->hosthandle,
  332. lsreq);
  333. if (ret)
  334. goto out_unlink;
  335. return 0;
  336. out_unlink:
  337. lsop->ls_error = ret;
  338. spin_lock_irqsave(&tgtport->lock, flags);
  339. lsop->req_queued = false;
  340. list_del(&lsop->lsreq_list);
  341. spin_unlock_irqrestore(&tgtport->lock, flags);
  342. fc_dma_unmap_single(tgtport->dev, lsreq->rqstdma,
  343. (lsreq->rqstlen + lsreq->rsplen),
  344. DMA_BIDIRECTIONAL);
  345. out_puttgtport:
  346. nvmet_fc_tgtport_put(tgtport);
  347. return ret;
  348. }
  349. static int
  350. nvmet_fc_send_ls_req_async(struct nvmet_fc_tgtport *tgtport,
  351. struct nvmet_fc_ls_req_op *lsop,
  352. void (*done)(struct nvmefc_ls_req *req, int status))
  353. {
  354. /* don't wait for completion */
  355. return __nvmet_fc_send_ls_req(tgtport, lsop, done);
  356. }
  357. static void
  358. nvmet_fc_disconnect_assoc_done(struct nvmefc_ls_req *lsreq, int status)
  359. {
  360. struct nvmet_fc_ls_req_op *lsop =
  361. container_of(lsreq, struct nvmet_fc_ls_req_op, ls_req);
  362. __nvmet_fc_finish_ls_req(lsop);
  363. /* fc-nvme target doesn't care about success or failure of cmd */
  364. }
  365. /*
  366. * This routine sends a FC-NVME LS to disconnect (aka terminate)
  367. * the FC-NVME Association. Terminating the association also
  368. * terminates the FC-NVME connections (per queue, both admin and io
  369. * queues) that are part of the association. E.g. things are torn
  370. * down, and the related FC-NVME Association ID and Connection IDs
  371. * become invalid.
  372. *
  373. * The behavior of the fc-nvme target is such that it's
  374. * understanding of the association and connections will implicitly
  375. * be torn down. The action is implicit as it may be due to a loss of
  376. * connectivity with the fc-nvme host, so the target may never get a
  377. * response even if it tried. As such, the action of this routine
  378. * is to asynchronously send the LS, ignore any results of the LS, and
  379. * continue on with terminating the association. If the fc-nvme host
  380. * is present and receives the LS, it too can tear down.
  381. */
  382. static void
  383. nvmet_fc_xmt_disconnect_assoc(struct nvmet_fc_tgt_assoc *assoc)
  384. {
  385. struct nvmet_fc_tgtport *tgtport = assoc->tgtport;
  386. struct fcnvme_ls_disconnect_assoc_rqst *discon_rqst;
  387. struct fcnvme_ls_disconnect_assoc_acc *discon_acc;
  388. struct nvmet_fc_ls_req_op *lsop;
  389. struct nvmefc_ls_req *lsreq;
  390. int ret;
  391. /*
  392. * If ls_req is NULL or no hosthandle, it's an older lldd and no
  393. * message is normal. Otherwise, send unless the hostport has
  394. * already been invalidated by the lldd.
  395. */
  396. if (!tgtport->ops->ls_req || assoc->hostport->invalid)
  397. return;
  398. lsop = kzalloc((sizeof(*lsop) +
  399. sizeof(*discon_rqst) + sizeof(*discon_acc) +
  400. tgtport->ops->lsrqst_priv_sz), GFP_KERNEL);
  401. if (!lsop) {
  402. dev_info(tgtport->dev,
  403. "{%d:%d} send Disconnect Association failed: ENOMEM\n",
  404. tgtport->fc_target_port.port_num, assoc->a_id);
  405. return;
  406. }
  407. discon_rqst = (struct fcnvme_ls_disconnect_assoc_rqst *)&lsop[1];
  408. discon_acc = (struct fcnvme_ls_disconnect_assoc_acc *)&discon_rqst[1];
  409. lsreq = &lsop->ls_req;
  410. if (tgtport->ops->lsrqst_priv_sz)
  411. lsreq->private = (void *)&discon_acc[1];
  412. else
  413. lsreq->private = NULL;
  414. lsop->tgtport = tgtport;
  415. lsop->hosthandle = assoc->hostport->hosthandle;
  416. nvmefc_fmt_lsreq_discon_assoc(lsreq, discon_rqst, discon_acc,
  417. assoc->association_id);
  418. ret = nvmet_fc_send_ls_req_async(tgtport, lsop,
  419. nvmet_fc_disconnect_assoc_done);
  420. if (ret) {
  421. dev_info(tgtport->dev,
  422. "{%d:%d} XMT Disconnect Association failed: %d\n",
  423. tgtport->fc_target_port.port_num, assoc->a_id, ret);
  424. kfree(lsop);
  425. }
  426. }
  427. /* *********************** FC-NVME Port Management ************************ */
  428. static int
  429. nvmet_fc_alloc_ls_iodlist(struct nvmet_fc_tgtport *tgtport)
  430. {
  431. struct nvmet_fc_ls_iod *iod;
  432. int i;
  433. iod = kcalloc(NVMET_LS_CTX_COUNT, sizeof(struct nvmet_fc_ls_iod),
  434. GFP_KERNEL);
  435. if (!iod)
  436. return -ENOMEM;
  437. tgtport->iod = iod;
  438. for (i = 0; i < NVMET_LS_CTX_COUNT; iod++, i++) {
  439. INIT_WORK(&iod->work, nvmet_fc_handle_ls_rqst_work);
  440. iod->tgtport = tgtport;
  441. list_add_tail(&iod->ls_rcv_list, &tgtport->ls_rcv_list);
  442. iod->rqstbuf = kzalloc(sizeof(union nvmefc_ls_requests) +
  443. sizeof(union nvmefc_ls_responses),
  444. GFP_KERNEL);
  445. if (!iod->rqstbuf)
  446. goto out_fail;
  447. iod->rspbuf = (union nvmefc_ls_responses *)&iod->rqstbuf[1];
  448. iod->rspdma = fc_dma_map_single(tgtport->dev, iod->rspbuf,
  449. sizeof(*iod->rspbuf),
  450. DMA_TO_DEVICE);
  451. if (fc_dma_mapping_error(tgtport->dev, iod->rspdma))
  452. goto out_fail;
  453. }
  454. return 0;
  455. out_fail:
  456. kfree(iod->rqstbuf);
  457. list_del(&iod->ls_rcv_list);
  458. for (iod--, i--; i >= 0; iod--, i--) {
  459. fc_dma_unmap_single(tgtport->dev, iod->rspdma,
  460. sizeof(*iod->rspbuf), DMA_TO_DEVICE);
  461. kfree(iod->rqstbuf);
  462. list_del(&iod->ls_rcv_list);
  463. }
  464. kfree(iod);
  465. return -EFAULT;
  466. }
  467. static void
  468. nvmet_fc_free_ls_iodlist(struct nvmet_fc_tgtport *tgtport)
  469. {
  470. struct nvmet_fc_ls_iod *iod = tgtport->iod;
  471. int i;
  472. for (i = 0; i < NVMET_LS_CTX_COUNT; iod++, i++) {
  473. fc_dma_unmap_single(tgtport->dev,
  474. iod->rspdma, sizeof(*iod->rspbuf),
  475. DMA_TO_DEVICE);
  476. kfree(iod->rqstbuf);
  477. list_del(&iod->ls_rcv_list);
  478. }
  479. kfree(tgtport->iod);
  480. }
  481. static struct nvmet_fc_ls_iod *
  482. nvmet_fc_alloc_ls_iod(struct nvmet_fc_tgtport *tgtport)
  483. {
  484. struct nvmet_fc_ls_iod *iod;
  485. unsigned long flags;
  486. spin_lock_irqsave(&tgtport->lock, flags);
  487. iod = list_first_entry_or_null(&tgtport->ls_rcv_list,
  488. struct nvmet_fc_ls_iod, ls_rcv_list);
  489. if (iod)
  490. list_move_tail(&iod->ls_rcv_list, &tgtport->ls_busylist);
  491. spin_unlock_irqrestore(&tgtport->lock, flags);
  492. return iod;
  493. }
  494. static void
  495. nvmet_fc_free_ls_iod(struct nvmet_fc_tgtport *tgtport,
  496. struct nvmet_fc_ls_iod *iod)
  497. {
  498. unsigned long flags;
  499. spin_lock_irqsave(&tgtport->lock, flags);
  500. list_move(&iod->ls_rcv_list, &tgtport->ls_rcv_list);
  501. spin_unlock_irqrestore(&tgtport->lock, flags);
  502. }
  503. static void
  504. nvmet_fc_prep_fcp_iodlist(struct nvmet_fc_tgtport *tgtport,
  505. struct nvmet_fc_tgt_queue *queue)
  506. {
  507. struct nvmet_fc_fcp_iod *fod = queue->fod;
  508. int i;
  509. for (i = 0; i < queue->sqsize; fod++, i++) {
  510. INIT_WORK(&fod->defer_work, nvmet_fc_fcp_rqst_op_defer_work);
  511. fod->tgtport = tgtport;
  512. fod->queue = queue;
  513. fod->active = false;
  514. fod->abort = false;
  515. fod->aborted = false;
  516. fod->fcpreq = NULL;
  517. list_add_tail(&fod->fcp_list, &queue->fod_list);
  518. spin_lock_init(&fod->flock);
  519. fod->rspdma = fc_dma_map_single(tgtport->dev, &fod->rspiubuf,
  520. sizeof(fod->rspiubuf), DMA_TO_DEVICE);
  521. if (fc_dma_mapping_error(tgtport->dev, fod->rspdma)) {
  522. list_del(&fod->fcp_list);
  523. for (fod--, i--; i >= 0; fod--, i--) {
  524. fc_dma_unmap_single(tgtport->dev, fod->rspdma,
  525. sizeof(fod->rspiubuf),
  526. DMA_TO_DEVICE);
  527. fod->rspdma = 0L;
  528. list_del(&fod->fcp_list);
  529. }
  530. return;
  531. }
  532. }
  533. }
  534. static void
  535. nvmet_fc_destroy_fcp_iodlist(struct nvmet_fc_tgtport *tgtport,
  536. struct nvmet_fc_tgt_queue *queue)
  537. {
  538. struct nvmet_fc_fcp_iod *fod = queue->fod;
  539. int i;
  540. for (i = 0; i < queue->sqsize; fod++, i++) {
  541. if (fod->rspdma)
  542. fc_dma_unmap_single(tgtport->dev, fod->rspdma,
  543. sizeof(fod->rspiubuf), DMA_TO_DEVICE);
  544. }
  545. }
  546. static struct nvmet_fc_fcp_iod *
  547. nvmet_fc_alloc_fcp_iod(struct nvmet_fc_tgt_queue *queue)
  548. {
  549. struct nvmet_fc_fcp_iod *fod;
  550. lockdep_assert_held(&queue->qlock);
  551. fod = list_first_entry_or_null(&queue->fod_list,
  552. struct nvmet_fc_fcp_iod, fcp_list);
  553. if (fod) {
  554. list_del(&fod->fcp_list);
  555. fod->active = true;
  556. /*
  557. * no queue reference is taken, as it was taken by the
  558. * queue lookup just prior to the allocation. The iod
  559. * will "inherit" that reference.
  560. */
  561. }
  562. return fod;
  563. }
  564. static void
  565. nvmet_fc_queue_fcp_req(struct nvmet_fc_tgtport *tgtport,
  566. struct nvmet_fc_tgt_queue *queue,
  567. struct nvmefc_tgt_fcp_req *fcpreq)
  568. {
  569. struct nvmet_fc_fcp_iod *fod = fcpreq->nvmet_fc_private;
  570. /*
  571. * put all admin cmds on hw queue id 0. All io commands go to
  572. * the respective hw queue based on a modulo basis
  573. */
  574. fcpreq->hwqid = queue->qid ?
  575. ((queue->qid - 1) % tgtport->ops->max_hw_queues) : 0;
  576. nvmet_fc_handle_fcp_rqst(tgtport, fod);
  577. }
  578. static void
  579. nvmet_fc_fcp_rqst_op_defer_work(struct work_struct *work)
  580. {
  581. struct nvmet_fc_fcp_iod *fod =
  582. container_of(work, struct nvmet_fc_fcp_iod, defer_work);
  583. /* Submit deferred IO for processing */
  584. nvmet_fc_queue_fcp_req(fod->tgtport, fod->queue, fod->fcpreq);
  585. }
  586. static void
  587. nvmet_fc_free_fcp_iod(struct nvmet_fc_tgt_queue *queue,
  588. struct nvmet_fc_fcp_iod *fod)
  589. {
  590. struct nvmefc_tgt_fcp_req *fcpreq = fod->fcpreq;
  591. struct nvmet_fc_tgtport *tgtport = fod->tgtport;
  592. struct nvmet_fc_defer_fcp_req *deferfcp;
  593. unsigned long flags;
  594. fc_dma_sync_single_for_cpu(tgtport->dev, fod->rspdma,
  595. sizeof(fod->rspiubuf), DMA_TO_DEVICE);
  596. fcpreq->nvmet_fc_private = NULL;
  597. fod->active = false;
  598. fod->abort = false;
  599. fod->aborted = false;
  600. fod->writedataactive = false;
  601. fod->fcpreq = NULL;
  602. tgtport->ops->fcp_req_release(&tgtport->fc_target_port, fcpreq);
  603. /* release the queue lookup reference on the completed IO */
  604. nvmet_fc_tgt_q_put(queue);
  605. spin_lock_irqsave(&queue->qlock, flags);
  606. deferfcp = list_first_entry_or_null(&queue->pending_cmd_list,
  607. struct nvmet_fc_defer_fcp_req, req_list);
  608. if (!deferfcp) {
  609. list_add_tail(&fod->fcp_list, &fod->queue->fod_list);
  610. spin_unlock_irqrestore(&queue->qlock, flags);
  611. return;
  612. }
  613. /* Re-use the fod for the next pending cmd that was deferred */
  614. list_del(&deferfcp->req_list);
  615. fcpreq = deferfcp->fcp_req;
  616. /* deferfcp can be reused for another IO at a later date */
  617. list_add_tail(&deferfcp->req_list, &queue->avail_defer_list);
  618. spin_unlock_irqrestore(&queue->qlock, flags);
  619. /* Save NVME CMD IO in fod */
  620. memcpy(&fod->cmdiubuf, fcpreq->rspaddr, fcpreq->rsplen);
  621. /* Setup new fcpreq to be processed */
  622. fcpreq->rspaddr = NULL;
  623. fcpreq->rsplen = 0;
  624. fcpreq->nvmet_fc_private = fod;
  625. fod->fcpreq = fcpreq;
  626. fod->active = true;
  627. /* inform LLDD IO is now being processed */
  628. tgtport->ops->defer_rcv(&tgtport->fc_target_port, fcpreq);
  629. /*
  630. * Leave the queue lookup get reference taken when
  631. * fod was originally allocated.
  632. */
  633. queue_work(queue->work_q, &fod->defer_work);
  634. }
  635. static struct nvmet_fc_tgt_queue *
  636. nvmet_fc_alloc_target_queue(struct nvmet_fc_tgt_assoc *assoc,
  637. u16 qid, u16 sqsize)
  638. {
  639. struct nvmet_fc_tgt_queue *queue;
  640. int ret;
  641. if (qid > NVMET_NR_QUEUES)
  642. return NULL;
  643. queue = kzalloc(struct_size(queue, fod, sqsize), GFP_KERNEL);
  644. if (!queue)
  645. return NULL;
  646. queue->work_q = alloc_workqueue("ntfc%d.%d.%d", 0, 0,
  647. assoc->tgtport->fc_target_port.port_num,
  648. assoc->a_id, qid);
  649. if (!queue->work_q)
  650. goto out_free_queue;
  651. queue->qid = qid;
  652. queue->sqsize = sqsize;
  653. queue->assoc = assoc;
  654. INIT_LIST_HEAD(&queue->fod_list);
  655. INIT_LIST_HEAD(&queue->avail_defer_list);
  656. INIT_LIST_HEAD(&queue->pending_cmd_list);
  657. atomic_set(&queue->connected, 0);
  658. atomic_set(&queue->sqtail, 0);
  659. atomic_set(&queue->rsn, 1);
  660. atomic_set(&queue->zrspcnt, 0);
  661. spin_lock_init(&queue->qlock);
  662. kref_init(&queue->ref);
  663. nvmet_fc_prep_fcp_iodlist(assoc->tgtport, queue);
  664. ret = nvmet_sq_init(&queue->nvme_sq);
  665. if (ret)
  666. goto out_fail_iodlist;
  667. WARN_ON(assoc->queues[qid]);
  668. assoc->queues[qid] = queue;
  669. return queue;
  670. out_fail_iodlist:
  671. nvmet_fc_destroy_fcp_iodlist(assoc->tgtport, queue);
  672. destroy_workqueue(queue->work_q);
  673. out_free_queue:
  674. kfree(queue);
  675. return NULL;
  676. }
  677. static void
  678. nvmet_fc_tgt_queue_free(struct kref *ref)
  679. {
  680. struct nvmet_fc_tgt_queue *queue =
  681. container_of(ref, struct nvmet_fc_tgt_queue, ref);
  682. nvmet_fc_destroy_fcp_iodlist(queue->assoc->tgtport, queue);
  683. destroy_workqueue(queue->work_q);
  684. kfree(queue);
  685. }
  686. static void
  687. nvmet_fc_tgt_q_put(struct nvmet_fc_tgt_queue *queue)
  688. {
  689. kref_put(&queue->ref, nvmet_fc_tgt_queue_free);
  690. }
  691. static int
  692. nvmet_fc_tgt_q_get(struct nvmet_fc_tgt_queue *queue)
  693. {
  694. return kref_get_unless_zero(&queue->ref);
  695. }
  696. static void
  697. nvmet_fc_delete_target_queue(struct nvmet_fc_tgt_queue *queue)
  698. {
  699. struct nvmet_fc_tgtport *tgtport = queue->assoc->tgtport;
  700. struct nvmet_fc_fcp_iod *fod = queue->fod;
  701. struct nvmet_fc_defer_fcp_req *deferfcp, *tempptr;
  702. unsigned long flags;
  703. int i;
  704. bool disconnect;
  705. disconnect = atomic_xchg(&queue->connected, 0);
  706. /* if not connected, nothing to do */
  707. if (!disconnect)
  708. return;
  709. spin_lock_irqsave(&queue->qlock, flags);
  710. /* abort outstanding io's */
  711. for (i = 0; i < queue->sqsize; fod++, i++) {
  712. if (fod->active) {
  713. spin_lock(&fod->flock);
  714. fod->abort = true;
  715. /*
  716. * only call lldd abort routine if waiting for
  717. * writedata. other outstanding ops should finish
  718. * on their own.
  719. */
  720. if (fod->writedataactive) {
  721. fod->aborted = true;
  722. spin_unlock(&fod->flock);
  723. tgtport->ops->fcp_abort(
  724. &tgtport->fc_target_port, fod->fcpreq);
  725. } else
  726. spin_unlock(&fod->flock);
  727. }
  728. }
  729. /* Cleanup defer'ed IOs in queue */
  730. list_for_each_entry_safe(deferfcp, tempptr, &queue->avail_defer_list,
  731. req_list) {
  732. list_del(&deferfcp->req_list);
  733. kfree(deferfcp);
  734. }
  735. for (;;) {
  736. deferfcp = list_first_entry_or_null(&queue->pending_cmd_list,
  737. struct nvmet_fc_defer_fcp_req, req_list);
  738. if (!deferfcp)
  739. break;
  740. list_del(&deferfcp->req_list);
  741. spin_unlock_irqrestore(&queue->qlock, flags);
  742. tgtport->ops->defer_rcv(&tgtport->fc_target_port,
  743. deferfcp->fcp_req);
  744. tgtport->ops->fcp_abort(&tgtport->fc_target_port,
  745. deferfcp->fcp_req);
  746. tgtport->ops->fcp_req_release(&tgtport->fc_target_port,
  747. deferfcp->fcp_req);
  748. /* release the queue lookup reference */
  749. nvmet_fc_tgt_q_put(queue);
  750. kfree(deferfcp);
  751. spin_lock_irqsave(&queue->qlock, flags);
  752. }
  753. spin_unlock_irqrestore(&queue->qlock, flags);
  754. flush_workqueue(queue->work_q);
  755. nvmet_sq_destroy(&queue->nvme_sq);
  756. nvmet_fc_tgt_q_put(queue);
  757. }
  758. static struct nvmet_fc_tgt_queue *
  759. nvmet_fc_find_target_queue(struct nvmet_fc_tgtport *tgtport,
  760. u64 connection_id)
  761. {
  762. struct nvmet_fc_tgt_assoc *assoc;
  763. struct nvmet_fc_tgt_queue *queue;
  764. u64 association_id = nvmet_fc_getassociationid(connection_id);
  765. u16 qid = nvmet_fc_getqueueid(connection_id);
  766. if (qid > NVMET_NR_QUEUES)
  767. return NULL;
  768. rcu_read_lock();
  769. list_for_each_entry_rcu(assoc, &tgtport->assoc_list, a_list) {
  770. if (association_id == assoc->association_id) {
  771. queue = assoc->queues[qid];
  772. if (queue &&
  773. (!atomic_read(&queue->connected) ||
  774. !nvmet_fc_tgt_q_get(queue)))
  775. queue = NULL;
  776. rcu_read_unlock();
  777. return queue;
  778. }
  779. }
  780. rcu_read_unlock();
  781. return NULL;
  782. }
  783. static void
  784. nvmet_fc_hostport_free(struct kref *ref)
  785. {
  786. struct nvmet_fc_hostport *hostport =
  787. container_of(ref, struct nvmet_fc_hostport, ref);
  788. struct nvmet_fc_tgtport *tgtport = hostport->tgtport;
  789. unsigned long flags;
  790. spin_lock_irqsave(&tgtport->lock, flags);
  791. list_del(&hostport->host_list);
  792. spin_unlock_irqrestore(&tgtport->lock, flags);
  793. if (tgtport->ops->host_release && hostport->invalid)
  794. tgtport->ops->host_release(hostport->hosthandle);
  795. kfree(hostport);
  796. nvmet_fc_tgtport_put(tgtport);
  797. }
  798. static void
  799. nvmet_fc_hostport_put(struct nvmet_fc_hostport *hostport)
  800. {
  801. kref_put(&hostport->ref, nvmet_fc_hostport_free);
  802. }
  803. static int
  804. nvmet_fc_hostport_get(struct nvmet_fc_hostport *hostport)
  805. {
  806. return kref_get_unless_zero(&hostport->ref);
  807. }
  808. static void
  809. nvmet_fc_free_hostport(struct nvmet_fc_hostport *hostport)
  810. {
  811. /* if LLDD not implemented, leave as NULL */
  812. if (!hostport || !hostport->hosthandle)
  813. return;
  814. nvmet_fc_hostport_put(hostport);
  815. }
  816. static struct nvmet_fc_hostport *
  817. nvmet_fc_match_hostport(struct nvmet_fc_tgtport *tgtport, void *hosthandle)
  818. {
  819. struct nvmet_fc_hostport *host;
  820. lockdep_assert_held(&tgtport->lock);
  821. list_for_each_entry(host, &tgtport->host_list, host_list) {
  822. if (host->hosthandle == hosthandle && !host->invalid) {
  823. if (nvmet_fc_hostport_get(host))
  824. return host;
  825. }
  826. }
  827. return NULL;
  828. }
  829. static struct nvmet_fc_hostport *
  830. nvmet_fc_alloc_hostport(struct nvmet_fc_tgtport *tgtport, void *hosthandle)
  831. {
  832. struct nvmet_fc_hostport *newhost, *match = NULL;
  833. unsigned long flags;
  834. /*
  835. * Caller holds a reference on tgtport.
  836. */
  837. /* if LLDD not implemented, leave as NULL */
  838. if (!hosthandle)
  839. return NULL;
  840. spin_lock_irqsave(&tgtport->lock, flags);
  841. match = nvmet_fc_match_hostport(tgtport, hosthandle);
  842. spin_unlock_irqrestore(&tgtport->lock, flags);
  843. if (match)
  844. return match;
  845. newhost = kzalloc(sizeof(*newhost), GFP_KERNEL);
  846. if (!newhost)
  847. return ERR_PTR(-ENOMEM);
  848. spin_lock_irqsave(&tgtport->lock, flags);
  849. match = nvmet_fc_match_hostport(tgtport, hosthandle);
  850. if (match) {
  851. /* new allocation not needed */
  852. kfree(newhost);
  853. newhost = match;
  854. } else {
  855. nvmet_fc_tgtport_get(tgtport);
  856. newhost->tgtport = tgtport;
  857. newhost->hosthandle = hosthandle;
  858. INIT_LIST_HEAD(&newhost->host_list);
  859. kref_init(&newhost->ref);
  860. list_add_tail(&newhost->host_list, &tgtport->host_list);
  861. }
  862. spin_unlock_irqrestore(&tgtport->lock, flags);
  863. return newhost;
  864. }
  865. static void
  866. nvmet_fc_delete_assoc(struct nvmet_fc_tgt_assoc *assoc)
  867. {
  868. nvmet_fc_delete_target_assoc(assoc);
  869. nvmet_fc_tgt_a_put(assoc);
  870. }
  871. static void
  872. nvmet_fc_delete_assoc_work(struct work_struct *work)
  873. {
  874. struct nvmet_fc_tgt_assoc *assoc =
  875. container_of(work, struct nvmet_fc_tgt_assoc, del_work);
  876. struct nvmet_fc_tgtport *tgtport = assoc->tgtport;
  877. nvmet_fc_delete_assoc(assoc);
  878. nvmet_fc_tgtport_put(tgtport);
  879. }
  880. static void
  881. nvmet_fc_schedule_delete_assoc(struct nvmet_fc_tgt_assoc *assoc)
  882. {
  883. nvmet_fc_tgtport_get(assoc->tgtport);
  884. if (!queue_work(nvmet_wq, &assoc->del_work))
  885. nvmet_fc_tgtport_put(assoc->tgtport);
  886. }
  887. static bool
  888. nvmet_fc_assoc_exists(struct nvmet_fc_tgtport *tgtport, u64 association_id)
  889. {
  890. struct nvmet_fc_tgt_assoc *a;
  891. bool found = false;
  892. rcu_read_lock();
  893. list_for_each_entry_rcu(a, &tgtport->assoc_list, a_list) {
  894. if (association_id == a->association_id) {
  895. found = true;
  896. break;
  897. }
  898. }
  899. rcu_read_unlock();
  900. return found;
  901. }
  902. static struct nvmet_fc_tgt_assoc *
  903. nvmet_fc_alloc_target_assoc(struct nvmet_fc_tgtport *tgtport, void *hosthandle)
  904. {
  905. struct nvmet_fc_tgt_assoc *assoc;
  906. unsigned long flags;
  907. bool done;
  908. u64 ran;
  909. int idx;
  910. if (!tgtport->pe)
  911. return NULL;
  912. assoc = kzalloc(sizeof(*assoc), GFP_KERNEL);
  913. if (!assoc)
  914. return NULL;
  915. idx = ida_alloc(&tgtport->assoc_cnt, GFP_KERNEL);
  916. if (idx < 0)
  917. goto out_free_assoc;
  918. assoc->hostport = nvmet_fc_alloc_hostport(tgtport, hosthandle);
  919. if (IS_ERR(assoc->hostport))
  920. goto out_ida;
  921. assoc->tgtport = tgtport;
  922. assoc->a_id = idx;
  923. INIT_LIST_HEAD(&assoc->a_list);
  924. kref_init(&assoc->ref);
  925. INIT_WORK(&assoc->del_work, nvmet_fc_delete_assoc_work);
  926. atomic_set(&assoc->terminating, 0);
  927. done = false;
  928. do {
  929. get_random_bytes(&ran, sizeof(ran) - BYTES_FOR_QID);
  930. ran = ran << BYTES_FOR_QID_SHIFT;
  931. spin_lock_irqsave(&tgtport->lock, flags);
  932. if (!nvmet_fc_assoc_exists(tgtport, ran)) {
  933. assoc->association_id = ran;
  934. list_add_tail_rcu(&assoc->a_list, &tgtport->assoc_list);
  935. done = true;
  936. }
  937. spin_unlock_irqrestore(&tgtport->lock, flags);
  938. } while (!done);
  939. return assoc;
  940. out_ida:
  941. ida_free(&tgtport->assoc_cnt, idx);
  942. out_free_assoc:
  943. kfree(assoc);
  944. return NULL;
  945. }
  946. static void
  947. nvmet_fc_target_assoc_free(struct kref *ref)
  948. {
  949. struct nvmet_fc_tgt_assoc *assoc =
  950. container_of(ref, struct nvmet_fc_tgt_assoc, ref);
  951. struct nvmet_fc_tgtport *tgtport = assoc->tgtport;
  952. struct nvmet_fc_ls_iod *oldls;
  953. unsigned long flags;
  954. int i;
  955. for (i = NVMET_NR_QUEUES; i >= 0; i--) {
  956. if (assoc->queues[i])
  957. nvmet_fc_delete_target_queue(assoc->queues[i]);
  958. }
  959. /* Send Disconnect now that all i/o has completed */
  960. nvmet_fc_xmt_disconnect_assoc(assoc);
  961. nvmet_fc_free_hostport(assoc->hostport);
  962. spin_lock_irqsave(&tgtport->lock, flags);
  963. oldls = assoc->rcv_disconn;
  964. spin_unlock_irqrestore(&tgtport->lock, flags);
  965. /* if pending Rcv Disconnect Association LS, send rsp now */
  966. if (oldls)
  967. nvmet_fc_xmt_ls_rsp(tgtport, oldls);
  968. ida_free(&tgtport->assoc_cnt, assoc->a_id);
  969. dev_info(tgtport->dev,
  970. "{%d:%d} Association freed\n",
  971. tgtport->fc_target_port.port_num, assoc->a_id);
  972. kfree(assoc);
  973. }
  974. static void
  975. nvmet_fc_tgt_a_put(struct nvmet_fc_tgt_assoc *assoc)
  976. {
  977. kref_put(&assoc->ref, nvmet_fc_target_assoc_free);
  978. }
  979. static int
  980. nvmet_fc_tgt_a_get(struct nvmet_fc_tgt_assoc *assoc)
  981. {
  982. return kref_get_unless_zero(&assoc->ref);
  983. }
  984. static void
  985. nvmet_fc_delete_target_assoc(struct nvmet_fc_tgt_assoc *assoc)
  986. {
  987. struct nvmet_fc_tgtport *tgtport = assoc->tgtport;
  988. unsigned long flags;
  989. int i, terminating;
  990. terminating = atomic_xchg(&assoc->terminating, 1);
  991. /* if already terminating, do nothing */
  992. if (terminating)
  993. return;
  994. spin_lock_irqsave(&tgtport->lock, flags);
  995. list_del_rcu(&assoc->a_list);
  996. spin_unlock_irqrestore(&tgtport->lock, flags);
  997. synchronize_rcu();
  998. /* ensure all in-flight I/Os have been processed */
  999. for (i = NVMET_NR_QUEUES; i >= 0; i--) {
  1000. if (assoc->queues[i])
  1001. flush_workqueue(assoc->queues[i]->work_q);
  1002. }
  1003. dev_info(tgtport->dev,
  1004. "{%d:%d} Association deleted\n",
  1005. tgtport->fc_target_port.port_num, assoc->a_id);
  1006. }
  1007. static struct nvmet_fc_tgt_assoc *
  1008. nvmet_fc_find_target_assoc(struct nvmet_fc_tgtport *tgtport,
  1009. u64 association_id)
  1010. {
  1011. struct nvmet_fc_tgt_assoc *assoc;
  1012. struct nvmet_fc_tgt_assoc *ret = NULL;
  1013. rcu_read_lock();
  1014. list_for_each_entry_rcu(assoc, &tgtport->assoc_list, a_list) {
  1015. if (association_id == assoc->association_id) {
  1016. ret = assoc;
  1017. if (!nvmet_fc_tgt_a_get(assoc))
  1018. ret = NULL;
  1019. break;
  1020. }
  1021. }
  1022. rcu_read_unlock();
  1023. return ret;
  1024. }
  1025. static void
  1026. nvmet_fc_portentry_bind(struct nvmet_fc_tgtport *tgtport,
  1027. struct nvmet_fc_port_entry *pe,
  1028. struct nvmet_port *port)
  1029. {
  1030. lockdep_assert_held(&nvmet_fc_tgtlock);
  1031. pe->tgtport = tgtport;
  1032. tgtport->pe = pe;
  1033. pe->port = port;
  1034. port->priv = pe;
  1035. pe->node_name = tgtport->fc_target_port.node_name;
  1036. pe->port_name = tgtport->fc_target_port.port_name;
  1037. INIT_LIST_HEAD(&pe->pe_list);
  1038. list_add_tail(&pe->pe_list, &nvmet_fc_portentry_list);
  1039. }
  1040. static void
  1041. nvmet_fc_portentry_unbind(struct nvmet_fc_port_entry *pe)
  1042. {
  1043. unsigned long flags;
  1044. spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
  1045. if (pe->tgtport)
  1046. pe->tgtport->pe = NULL;
  1047. list_del(&pe->pe_list);
  1048. spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
  1049. }
  1050. /*
  1051. * called when a targetport deregisters. Breaks the relationship
  1052. * with the nvmet port, but leaves the port_entry in place so that
  1053. * re-registration can resume operation.
  1054. */
  1055. static void
  1056. nvmet_fc_portentry_unbind_tgt(struct nvmet_fc_tgtport *tgtport)
  1057. {
  1058. struct nvmet_fc_port_entry *pe;
  1059. unsigned long flags;
  1060. spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
  1061. pe = tgtport->pe;
  1062. if (pe)
  1063. pe->tgtport = NULL;
  1064. tgtport->pe = NULL;
  1065. spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
  1066. }
  1067. /*
  1068. * called when a new targetport is registered. Looks in the
  1069. * existing nvmet port_entries to see if the nvmet layer is
  1070. * configured for the targetport's wwn's. (the targetport existed,
  1071. * nvmet configured, the lldd unregistered the tgtport, and is now
  1072. * reregistering the same targetport). If so, set the nvmet port
  1073. * port entry on the targetport.
  1074. */
  1075. static void
  1076. nvmet_fc_portentry_rebind_tgt(struct nvmet_fc_tgtport *tgtport)
  1077. {
  1078. struct nvmet_fc_port_entry *pe;
  1079. unsigned long flags;
  1080. spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
  1081. list_for_each_entry(pe, &nvmet_fc_portentry_list, pe_list) {
  1082. if (tgtport->fc_target_port.node_name == pe->node_name &&
  1083. tgtport->fc_target_port.port_name == pe->port_name) {
  1084. WARN_ON(pe->tgtport);
  1085. tgtport->pe = pe;
  1086. pe->tgtport = tgtport;
  1087. break;
  1088. }
  1089. }
  1090. spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
  1091. }
  1092. /**
  1093. * nvmet_fc_register_targetport - transport entry point called by an
  1094. * LLDD to register the existence of a local
  1095. * NVME subystem FC port.
  1096. * @pinfo: pointer to information about the port to be registered
  1097. * @template: LLDD entrypoints and operational parameters for the port
  1098. * @dev: physical hardware device node port corresponds to. Will be
  1099. * used for DMA mappings
  1100. * @portptr: pointer to a local port pointer. Upon success, the routine
  1101. * will allocate a nvme_fc_local_port structure and place its
  1102. * address in the local port pointer. Upon failure, local port
  1103. * pointer will be set to NULL.
  1104. *
  1105. * Returns:
  1106. * a completion status. Must be 0 upon success; a negative errno
  1107. * (ex: -ENXIO) upon failure.
  1108. */
  1109. int
  1110. nvmet_fc_register_targetport(struct nvmet_fc_port_info *pinfo,
  1111. struct nvmet_fc_target_template *template,
  1112. struct device *dev,
  1113. struct nvmet_fc_target_port **portptr)
  1114. {
  1115. struct nvmet_fc_tgtport *newrec;
  1116. unsigned long flags;
  1117. int ret, idx;
  1118. if (!template->xmt_ls_rsp || !template->fcp_op ||
  1119. !template->fcp_abort ||
  1120. !template->fcp_req_release || !template->targetport_delete ||
  1121. !template->max_hw_queues || !template->max_sgl_segments ||
  1122. !template->max_dif_sgl_segments || !template->dma_boundary) {
  1123. ret = -EINVAL;
  1124. goto out_regtgt_failed;
  1125. }
  1126. newrec = kzalloc((sizeof(*newrec) + template->target_priv_sz),
  1127. GFP_KERNEL);
  1128. if (!newrec) {
  1129. ret = -ENOMEM;
  1130. goto out_regtgt_failed;
  1131. }
  1132. idx = ida_alloc(&nvmet_fc_tgtport_cnt, GFP_KERNEL);
  1133. if (idx < 0) {
  1134. ret = -ENOSPC;
  1135. goto out_fail_kfree;
  1136. }
  1137. if (!get_device(dev) && dev) {
  1138. ret = -ENODEV;
  1139. goto out_ida_put;
  1140. }
  1141. newrec->fc_target_port.node_name = pinfo->node_name;
  1142. newrec->fc_target_port.port_name = pinfo->port_name;
  1143. if (template->target_priv_sz)
  1144. newrec->fc_target_port.private = &newrec[1];
  1145. else
  1146. newrec->fc_target_port.private = NULL;
  1147. newrec->fc_target_port.port_id = pinfo->port_id;
  1148. newrec->fc_target_port.port_num = idx;
  1149. INIT_LIST_HEAD(&newrec->tgt_list);
  1150. newrec->dev = dev;
  1151. newrec->ops = template;
  1152. spin_lock_init(&newrec->lock);
  1153. INIT_LIST_HEAD(&newrec->ls_rcv_list);
  1154. INIT_LIST_HEAD(&newrec->ls_req_list);
  1155. INIT_LIST_HEAD(&newrec->ls_busylist);
  1156. INIT_LIST_HEAD(&newrec->assoc_list);
  1157. INIT_LIST_HEAD(&newrec->host_list);
  1158. kref_init(&newrec->ref);
  1159. ida_init(&newrec->assoc_cnt);
  1160. newrec->max_sg_cnt = template->max_sgl_segments;
  1161. ret = nvmet_fc_alloc_ls_iodlist(newrec);
  1162. if (ret) {
  1163. ret = -ENOMEM;
  1164. goto out_free_newrec;
  1165. }
  1166. nvmet_fc_portentry_rebind_tgt(newrec);
  1167. spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
  1168. list_add_tail(&newrec->tgt_list, &nvmet_fc_target_list);
  1169. spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
  1170. *portptr = &newrec->fc_target_port;
  1171. return 0;
  1172. out_free_newrec:
  1173. put_device(dev);
  1174. out_ida_put:
  1175. ida_free(&nvmet_fc_tgtport_cnt, idx);
  1176. out_fail_kfree:
  1177. kfree(newrec);
  1178. out_regtgt_failed:
  1179. *portptr = NULL;
  1180. return ret;
  1181. }
  1182. EXPORT_SYMBOL_GPL(nvmet_fc_register_targetport);
  1183. static void
  1184. nvmet_fc_free_tgtport(struct kref *ref)
  1185. {
  1186. struct nvmet_fc_tgtport *tgtport =
  1187. container_of(ref, struct nvmet_fc_tgtport, ref);
  1188. struct device *dev = tgtport->dev;
  1189. unsigned long flags;
  1190. spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
  1191. list_del(&tgtport->tgt_list);
  1192. spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
  1193. nvmet_fc_free_ls_iodlist(tgtport);
  1194. /* let the LLDD know we've finished tearing it down */
  1195. tgtport->ops->targetport_delete(&tgtport->fc_target_port);
  1196. ida_free(&nvmet_fc_tgtport_cnt,
  1197. tgtport->fc_target_port.port_num);
  1198. ida_destroy(&tgtport->assoc_cnt);
  1199. kfree(tgtport);
  1200. put_device(dev);
  1201. }
  1202. static void
  1203. nvmet_fc_tgtport_put(struct nvmet_fc_tgtport *tgtport)
  1204. {
  1205. kref_put(&tgtport->ref, nvmet_fc_free_tgtport);
  1206. }
  1207. static int
  1208. nvmet_fc_tgtport_get(struct nvmet_fc_tgtport *tgtport)
  1209. {
  1210. return kref_get_unless_zero(&tgtport->ref);
  1211. }
  1212. static void
  1213. __nvmet_fc_free_assocs(struct nvmet_fc_tgtport *tgtport)
  1214. {
  1215. struct nvmet_fc_tgt_assoc *assoc;
  1216. rcu_read_lock();
  1217. list_for_each_entry_rcu(assoc, &tgtport->assoc_list, a_list) {
  1218. if (!nvmet_fc_tgt_a_get(assoc))
  1219. continue;
  1220. nvmet_fc_schedule_delete_assoc(assoc);
  1221. nvmet_fc_tgt_a_put(assoc);
  1222. }
  1223. rcu_read_unlock();
  1224. }
  1225. /**
  1226. * nvmet_fc_invalidate_host - transport entry point called by an LLDD
  1227. * to remove references to a hosthandle for LS's.
  1228. *
  1229. * The nvmet-fc layer ensures that any references to the hosthandle
  1230. * on the targetport are forgotten (set to NULL). The LLDD will
  1231. * typically call this when a login with a remote host port has been
  1232. * lost, thus LS's for the remote host port are no longer possible.
  1233. *
  1234. * If an LS request is outstanding to the targetport/hosthandle (or
  1235. * issued concurrently with the call to invalidate the host), the
  1236. * LLDD is responsible for terminating/aborting the LS and completing
  1237. * the LS request. It is recommended that these terminations/aborts
  1238. * occur after calling to invalidate the host handle to avoid additional
  1239. * retries by the nvmet-fc transport. The nvmet-fc transport may
  1240. * continue to reference host handle while it cleans up outstanding
  1241. * NVME associations. The nvmet-fc transport will call the
  1242. * ops->host_release() callback to notify the LLDD that all references
  1243. * are complete and the related host handle can be recovered.
  1244. * Note: if there are no references, the callback may be called before
  1245. * the invalidate host call returns.
  1246. *
  1247. * @target_port: pointer to the (registered) target port that a prior
  1248. * LS was received on and which supplied the transport the
  1249. * hosthandle.
  1250. * @hosthandle: the handle (pointer) that represents the host port
  1251. * that no longer has connectivity and that LS's should
  1252. * no longer be directed to.
  1253. */
  1254. void
  1255. nvmet_fc_invalidate_host(struct nvmet_fc_target_port *target_port,
  1256. void *hosthandle)
  1257. {
  1258. struct nvmet_fc_tgtport *tgtport = targetport_to_tgtport(target_port);
  1259. struct nvmet_fc_tgt_assoc *assoc, *next;
  1260. unsigned long flags;
  1261. bool noassoc = true;
  1262. spin_lock_irqsave(&tgtport->lock, flags);
  1263. list_for_each_entry_safe(assoc, next,
  1264. &tgtport->assoc_list, a_list) {
  1265. if (assoc->hostport->hosthandle != hosthandle)
  1266. continue;
  1267. if (!nvmet_fc_tgt_a_get(assoc))
  1268. continue;
  1269. assoc->hostport->invalid = 1;
  1270. noassoc = false;
  1271. nvmet_fc_schedule_delete_assoc(assoc);
  1272. nvmet_fc_tgt_a_put(assoc);
  1273. }
  1274. spin_unlock_irqrestore(&tgtport->lock, flags);
  1275. /* if there's nothing to wait for - call the callback */
  1276. if (noassoc && tgtport->ops->host_release)
  1277. tgtport->ops->host_release(hosthandle);
  1278. }
  1279. EXPORT_SYMBOL_GPL(nvmet_fc_invalidate_host);
  1280. /*
  1281. * nvmet layer has called to terminate an association
  1282. */
  1283. static void
  1284. nvmet_fc_delete_ctrl(struct nvmet_ctrl *ctrl)
  1285. {
  1286. struct nvmet_fc_tgtport *tgtport, *next;
  1287. struct nvmet_fc_tgt_assoc *assoc;
  1288. struct nvmet_fc_tgt_queue *queue;
  1289. unsigned long flags;
  1290. bool found_ctrl = false;
  1291. /* this is a bit ugly, but don't want to make locks layered */
  1292. spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
  1293. list_for_each_entry_safe(tgtport, next, &nvmet_fc_target_list,
  1294. tgt_list) {
  1295. if (!nvmet_fc_tgtport_get(tgtport))
  1296. continue;
  1297. spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
  1298. rcu_read_lock();
  1299. list_for_each_entry_rcu(assoc, &tgtport->assoc_list, a_list) {
  1300. queue = assoc->queues[0];
  1301. if (queue && queue->nvme_sq.ctrl == ctrl) {
  1302. if (nvmet_fc_tgt_a_get(assoc))
  1303. found_ctrl = true;
  1304. break;
  1305. }
  1306. }
  1307. rcu_read_unlock();
  1308. nvmet_fc_tgtport_put(tgtport);
  1309. if (found_ctrl) {
  1310. nvmet_fc_schedule_delete_assoc(assoc);
  1311. nvmet_fc_tgt_a_put(assoc);
  1312. return;
  1313. }
  1314. spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
  1315. }
  1316. spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
  1317. }
  1318. /**
  1319. * nvmet_fc_unregister_targetport - transport entry point called by an
  1320. * LLDD to deregister/remove a previously
  1321. * registered a local NVME subsystem FC port.
  1322. * @target_port: pointer to the (registered) target port that is to be
  1323. * deregistered.
  1324. *
  1325. * Returns:
  1326. * a completion status. Must be 0 upon success; a negative errno
  1327. * (ex: -ENXIO) upon failure.
  1328. */
  1329. int
  1330. nvmet_fc_unregister_targetport(struct nvmet_fc_target_port *target_port)
  1331. {
  1332. struct nvmet_fc_tgtport *tgtport = targetport_to_tgtport(target_port);
  1333. nvmet_fc_portentry_unbind_tgt(tgtport);
  1334. /* terminate any outstanding associations */
  1335. __nvmet_fc_free_assocs(tgtport);
  1336. flush_workqueue(nvmet_wq);
  1337. /*
  1338. * should terminate LS's as well. However, LS's will be generated
  1339. * at the tail end of association termination, so they likely don't
  1340. * exist yet. And even if they did, it's worthwhile to just let
  1341. * them finish and targetport ref counting will clean things up.
  1342. */
  1343. nvmet_fc_tgtport_put(tgtport);
  1344. return 0;
  1345. }
  1346. EXPORT_SYMBOL_GPL(nvmet_fc_unregister_targetport);
  1347. /* ********************** FC-NVME LS RCV Handling ************************* */
  1348. static void
  1349. nvmet_fc_ls_create_association(struct nvmet_fc_tgtport *tgtport,
  1350. struct nvmet_fc_ls_iod *iod)
  1351. {
  1352. struct fcnvme_ls_cr_assoc_rqst *rqst = &iod->rqstbuf->rq_cr_assoc;
  1353. struct fcnvme_ls_cr_assoc_acc *acc = &iod->rspbuf->rsp_cr_assoc;
  1354. struct nvmet_fc_tgt_queue *queue;
  1355. int ret = 0;
  1356. memset(acc, 0, sizeof(*acc));
  1357. /*
  1358. * FC-NVME spec changes. There are initiators sending different
  1359. * lengths as padding sizes for Create Association Cmd descriptor
  1360. * was incorrect.
  1361. * Accept anything of "minimum" length. Assume format per 1.15
  1362. * spec (with HOSTID reduced to 16 bytes), ignore how long the
  1363. * trailing pad length is.
  1364. */
  1365. if (iod->rqstdatalen < FCNVME_LSDESC_CRA_RQST_MINLEN)
  1366. ret = VERR_CR_ASSOC_LEN;
  1367. else if (be32_to_cpu(rqst->desc_list_len) <
  1368. FCNVME_LSDESC_CRA_RQST_MIN_LISTLEN)
  1369. ret = VERR_CR_ASSOC_RQST_LEN;
  1370. else if (rqst->assoc_cmd.desc_tag !=
  1371. cpu_to_be32(FCNVME_LSDESC_CREATE_ASSOC_CMD))
  1372. ret = VERR_CR_ASSOC_CMD;
  1373. else if (be32_to_cpu(rqst->assoc_cmd.desc_len) <
  1374. FCNVME_LSDESC_CRA_CMD_DESC_MIN_DESCLEN)
  1375. ret = VERR_CR_ASSOC_CMD_LEN;
  1376. else if (!rqst->assoc_cmd.ersp_ratio ||
  1377. (be16_to_cpu(rqst->assoc_cmd.ersp_ratio) >=
  1378. be16_to_cpu(rqst->assoc_cmd.sqsize)))
  1379. ret = VERR_ERSP_RATIO;
  1380. else {
  1381. /* new association w/ admin queue */
  1382. iod->assoc = nvmet_fc_alloc_target_assoc(
  1383. tgtport, iod->hosthandle);
  1384. if (!iod->assoc)
  1385. ret = VERR_ASSOC_ALLOC_FAIL;
  1386. else {
  1387. queue = nvmet_fc_alloc_target_queue(iod->assoc, 0,
  1388. be16_to_cpu(rqst->assoc_cmd.sqsize));
  1389. if (!queue) {
  1390. ret = VERR_QUEUE_ALLOC_FAIL;
  1391. nvmet_fc_tgt_a_put(iod->assoc);
  1392. }
  1393. }
  1394. }
  1395. if (ret) {
  1396. dev_err(tgtport->dev,
  1397. "Create Association LS failed: %s\n",
  1398. validation_errors[ret]);
  1399. iod->lsrsp->rsplen = nvme_fc_format_rjt(acc,
  1400. sizeof(*acc), rqst->w0.ls_cmd,
  1401. FCNVME_RJT_RC_LOGIC,
  1402. FCNVME_RJT_EXP_NONE, 0);
  1403. return;
  1404. }
  1405. queue->ersp_ratio = be16_to_cpu(rqst->assoc_cmd.ersp_ratio);
  1406. atomic_set(&queue->connected, 1);
  1407. queue->sqhd = 0; /* best place to init value */
  1408. dev_info(tgtport->dev,
  1409. "{%d:%d} Association created\n",
  1410. tgtport->fc_target_port.port_num, iod->assoc->a_id);
  1411. /* format a response */
  1412. iod->lsrsp->rsplen = sizeof(*acc);
  1413. nvme_fc_format_rsp_hdr(acc, FCNVME_LS_ACC,
  1414. fcnvme_lsdesc_len(
  1415. sizeof(struct fcnvme_ls_cr_assoc_acc)),
  1416. FCNVME_LS_CREATE_ASSOCIATION);
  1417. acc->associd.desc_tag = cpu_to_be32(FCNVME_LSDESC_ASSOC_ID);
  1418. acc->associd.desc_len =
  1419. fcnvme_lsdesc_len(
  1420. sizeof(struct fcnvme_lsdesc_assoc_id));
  1421. acc->associd.association_id =
  1422. cpu_to_be64(nvmet_fc_makeconnid(iod->assoc, 0));
  1423. acc->connectid.desc_tag = cpu_to_be32(FCNVME_LSDESC_CONN_ID);
  1424. acc->connectid.desc_len =
  1425. fcnvme_lsdesc_len(
  1426. sizeof(struct fcnvme_lsdesc_conn_id));
  1427. acc->connectid.connection_id = acc->associd.association_id;
  1428. }
  1429. static void
  1430. nvmet_fc_ls_create_connection(struct nvmet_fc_tgtport *tgtport,
  1431. struct nvmet_fc_ls_iod *iod)
  1432. {
  1433. struct fcnvme_ls_cr_conn_rqst *rqst = &iod->rqstbuf->rq_cr_conn;
  1434. struct fcnvme_ls_cr_conn_acc *acc = &iod->rspbuf->rsp_cr_conn;
  1435. struct nvmet_fc_tgt_queue *queue;
  1436. int ret = 0;
  1437. memset(acc, 0, sizeof(*acc));
  1438. if (iod->rqstdatalen < sizeof(struct fcnvme_ls_cr_conn_rqst))
  1439. ret = VERR_CR_CONN_LEN;
  1440. else if (rqst->desc_list_len !=
  1441. fcnvme_lsdesc_len(
  1442. sizeof(struct fcnvme_ls_cr_conn_rqst)))
  1443. ret = VERR_CR_CONN_RQST_LEN;
  1444. else if (rqst->associd.desc_tag != cpu_to_be32(FCNVME_LSDESC_ASSOC_ID))
  1445. ret = VERR_ASSOC_ID;
  1446. else if (rqst->associd.desc_len !=
  1447. fcnvme_lsdesc_len(
  1448. sizeof(struct fcnvme_lsdesc_assoc_id)))
  1449. ret = VERR_ASSOC_ID_LEN;
  1450. else if (rqst->connect_cmd.desc_tag !=
  1451. cpu_to_be32(FCNVME_LSDESC_CREATE_CONN_CMD))
  1452. ret = VERR_CR_CONN_CMD;
  1453. else if (rqst->connect_cmd.desc_len !=
  1454. fcnvme_lsdesc_len(
  1455. sizeof(struct fcnvme_lsdesc_cr_conn_cmd)))
  1456. ret = VERR_CR_CONN_CMD_LEN;
  1457. else if (!rqst->connect_cmd.ersp_ratio ||
  1458. (be16_to_cpu(rqst->connect_cmd.ersp_ratio) >=
  1459. be16_to_cpu(rqst->connect_cmd.sqsize)))
  1460. ret = VERR_ERSP_RATIO;
  1461. else {
  1462. /* new io queue */
  1463. iod->assoc = nvmet_fc_find_target_assoc(tgtport,
  1464. be64_to_cpu(rqst->associd.association_id));
  1465. if (!iod->assoc)
  1466. ret = VERR_NO_ASSOC;
  1467. else {
  1468. queue = nvmet_fc_alloc_target_queue(iod->assoc,
  1469. be16_to_cpu(rqst->connect_cmd.qid),
  1470. be16_to_cpu(rqst->connect_cmd.sqsize));
  1471. if (!queue)
  1472. ret = VERR_QUEUE_ALLOC_FAIL;
  1473. /* release get taken in nvmet_fc_find_target_assoc */
  1474. nvmet_fc_tgt_a_put(iod->assoc);
  1475. }
  1476. }
  1477. if (ret) {
  1478. dev_err(tgtport->dev,
  1479. "Create Connection LS failed: %s\n",
  1480. validation_errors[ret]);
  1481. iod->lsrsp->rsplen = nvme_fc_format_rjt(acc,
  1482. sizeof(*acc), rqst->w0.ls_cmd,
  1483. (ret == VERR_NO_ASSOC) ?
  1484. FCNVME_RJT_RC_INV_ASSOC :
  1485. FCNVME_RJT_RC_LOGIC,
  1486. FCNVME_RJT_EXP_NONE, 0);
  1487. return;
  1488. }
  1489. queue->ersp_ratio = be16_to_cpu(rqst->connect_cmd.ersp_ratio);
  1490. atomic_set(&queue->connected, 1);
  1491. queue->sqhd = 0; /* best place to init value */
  1492. /* format a response */
  1493. iod->lsrsp->rsplen = sizeof(*acc);
  1494. nvme_fc_format_rsp_hdr(acc, FCNVME_LS_ACC,
  1495. fcnvme_lsdesc_len(sizeof(struct fcnvme_ls_cr_conn_acc)),
  1496. FCNVME_LS_CREATE_CONNECTION);
  1497. acc->connectid.desc_tag = cpu_to_be32(FCNVME_LSDESC_CONN_ID);
  1498. acc->connectid.desc_len =
  1499. fcnvme_lsdesc_len(
  1500. sizeof(struct fcnvme_lsdesc_conn_id));
  1501. acc->connectid.connection_id =
  1502. cpu_to_be64(nvmet_fc_makeconnid(iod->assoc,
  1503. be16_to_cpu(rqst->connect_cmd.qid)));
  1504. }
  1505. /*
  1506. * Returns true if the LS response is to be transmit
  1507. * Returns false if the LS response is to be delayed
  1508. */
  1509. static int
  1510. nvmet_fc_ls_disconnect(struct nvmet_fc_tgtport *tgtport,
  1511. struct nvmet_fc_ls_iod *iod)
  1512. {
  1513. struct fcnvme_ls_disconnect_assoc_rqst *rqst =
  1514. &iod->rqstbuf->rq_dis_assoc;
  1515. struct fcnvme_ls_disconnect_assoc_acc *acc =
  1516. &iod->rspbuf->rsp_dis_assoc;
  1517. struct nvmet_fc_tgt_assoc *assoc = NULL;
  1518. struct nvmet_fc_ls_iod *oldls = NULL;
  1519. unsigned long flags;
  1520. int ret = 0;
  1521. memset(acc, 0, sizeof(*acc));
  1522. ret = nvmefc_vldt_lsreq_discon_assoc(iod->rqstdatalen, rqst);
  1523. if (!ret) {
  1524. /* match an active association - takes an assoc ref if !NULL */
  1525. assoc = nvmet_fc_find_target_assoc(tgtport,
  1526. be64_to_cpu(rqst->associd.association_id));
  1527. iod->assoc = assoc;
  1528. if (!assoc)
  1529. ret = VERR_NO_ASSOC;
  1530. }
  1531. if (ret || !assoc) {
  1532. dev_err(tgtport->dev,
  1533. "Disconnect LS failed: %s\n",
  1534. validation_errors[ret]);
  1535. iod->lsrsp->rsplen = nvme_fc_format_rjt(acc,
  1536. sizeof(*acc), rqst->w0.ls_cmd,
  1537. (ret == VERR_NO_ASSOC) ?
  1538. FCNVME_RJT_RC_INV_ASSOC :
  1539. FCNVME_RJT_RC_LOGIC,
  1540. FCNVME_RJT_EXP_NONE, 0);
  1541. return true;
  1542. }
  1543. /* format a response */
  1544. iod->lsrsp->rsplen = sizeof(*acc);
  1545. nvme_fc_format_rsp_hdr(acc, FCNVME_LS_ACC,
  1546. fcnvme_lsdesc_len(
  1547. sizeof(struct fcnvme_ls_disconnect_assoc_acc)),
  1548. FCNVME_LS_DISCONNECT_ASSOC);
  1549. /*
  1550. * The rules for LS response says the response cannot
  1551. * go back until ABTS's have been sent for all outstanding
  1552. * I/O and a Disconnect Association LS has been sent.
  1553. * So... save off the Disconnect LS to send the response
  1554. * later. If there was a prior LS already saved, replace
  1555. * it with the newer one and send a can't perform reject
  1556. * on the older one.
  1557. */
  1558. spin_lock_irqsave(&tgtport->lock, flags);
  1559. oldls = assoc->rcv_disconn;
  1560. assoc->rcv_disconn = iod;
  1561. spin_unlock_irqrestore(&tgtport->lock, flags);
  1562. if (oldls) {
  1563. dev_info(tgtport->dev,
  1564. "{%d:%d} Multiple Disconnect Association LS's "
  1565. "received\n",
  1566. tgtport->fc_target_port.port_num, assoc->a_id);
  1567. /* overwrite good response with bogus failure */
  1568. oldls->lsrsp->rsplen = nvme_fc_format_rjt(oldls->rspbuf,
  1569. sizeof(*iod->rspbuf),
  1570. /* ok to use rqst, LS is same */
  1571. rqst->w0.ls_cmd,
  1572. FCNVME_RJT_RC_UNAB,
  1573. FCNVME_RJT_EXP_NONE, 0);
  1574. nvmet_fc_xmt_ls_rsp(tgtport, oldls);
  1575. }
  1576. nvmet_fc_schedule_delete_assoc(assoc);
  1577. nvmet_fc_tgt_a_put(assoc);
  1578. return false;
  1579. }
  1580. /* *********************** NVME Ctrl Routines **************************** */
  1581. static void nvmet_fc_fcp_nvme_cmd_done(struct nvmet_req *nvme_req);
  1582. static const struct nvmet_fabrics_ops nvmet_fc_tgt_fcp_ops;
  1583. static void
  1584. nvmet_fc_xmt_ls_rsp_done(struct nvmefc_ls_rsp *lsrsp)
  1585. {
  1586. struct nvmet_fc_ls_iod *iod = lsrsp->nvme_fc_private;
  1587. struct nvmet_fc_tgtport *tgtport = iod->tgtport;
  1588. fc_dma_sync_single_for_cpu(tgtport->dev, iod->rspdma,
  1589. sizeof(*iod->rspbuf), DMA_TO_DEVICE);
  1590. nvmet_fc_free_ls_iod(tgtport, iod);
  1591. nvmet_fc_tgtport_put(tgtport);
  1592. }
  1593. static void
  1594. nvmet_fc_xmt_ls_rsp(struct nvmet_fc_tgtport *tgtport,
  1595. struct nvmet_fc_ls_iod *iod)
  1596. {
  1597. int ret;
  1598. fc_dma_sync_single_for_device(tgtport->dev, iod->rspdma,
  1599. sizeof(*iod->rspbuf), DMA_TO_DEVICE);
  1600. ret = tgtport->ops->xmt_ls_rsp(&tgtport->fc_target_port, iod->lsrsp);
  1601. if (ret)
  1602. nvmet_fc_xmt_ls_rsp_done(iod->lsrsp);
  1603. }
  1604. /*
  1605. * Actual processing routine for received FC-NVME LS Requests from the LLD
  1606. */
  1607. static void
  1608. nvmet_fc_handle_ls_rqst(struct nvmet_fc_tgtport *tgtport,
  1609. struct nvmet_fc_ls_iod *iod)
  1610. {
  1611. struct fcnvme_ls_rqst_w0 *w0 = &iod->rqstbuf->rq_cr_assoc.w0;
  1612. bool sendrsp = true;
  1613. iod->lsrsp->nvme_fc_private = iod;
  1614. iod->lsrsp->rspbuf = iod->rspbuf;
  1615. iod->lsrsp->rspdma = iod->rspdma;
  1616. iod->lsrsp->done = nvmet_fc_xmt_ls_rsp_done;
  1617. /* Be preventative. handlers will later set to valid length */
  1618. iod->lsrsp->rsplen = 0;
  1619. iod->assoc = NULL;
  1620. /*
  1621. * handlers:
  1622. * parse request input, execute the request, and format the
  1623. * LS response
  1624. */
  1625. switch (w0->ls_cmd) {
  1626. case FCNVME_LS_CREATE_ASSOCIATION:
  1627. /* Creates Association and initial Admin Queue/Connection */
  1628. nvmet_fc_ls_create_association(tgtport, iod);
  1629. break;
  1630. case FCNVME_LS_CREATE_CONNECTION:
  1631. /* Creates an IO Queue/Connection */
  1632. nvmet_fc_ls_create_connection(tgtport, iod);
  1633. break;
  1634. case FCNVME_LS_DISCONNECT_ASSOC:
  1635. /* Terminate a Queue/Connection or the Association */
  1636. sendrsp = nvmet_fc_ls_disconnect(tgtport, iod);
  1637. break;
  1638. default:
  1639. iod->lsrsp->rsplen = nvme_fc_format_rjt(iod->rspbuf,
  1640. sizeof(*iod->rspbuf), w0->ls_cmd,
  1641. FCNVME_RJT_RC_INVAL, FCNVME_RJT_EXP_NONE, 0);
  1642. }
  1643. if (sendrsp)
  1644. nvmet_fc_xmt_ls_rsp(tgtport, iod);
  1645. }
  1646. /*
  1647. * Actual processing routine for received FC-NVME LS Requests from the LLD
  1648. */
  1649. static void
  1650. nvmet_fc_handle_ls_rqst_work(struct work_struct *work)
  1651. {
  1652. struct nvmet_fc_ls_iod *iod =
  1653. container_of(work, struct nvmet_fc_ls_iod, work);
  1654. struct nvmet_fc_tgtport *tgtport = iod->tgtport;
  1655. nvmet_fc_handle_ls_rqst(tgtport, iod);
  1656. }
  1657. /**
  1658. * nvmet_fc_rcv_ls_req - transport entry point called by an LLDD
  1659. * upon the reception of a NVME LS request.
  1660. *
  1661. * The nvmet-fc layer will copy payload to an internal structure for
  1662. * processing. As such, upon completion of the routine, the LLDD may
  1663. * immediately free/reuse the LS request buffer passed in the call.
  1664. *
  1665. * If this routine returns error, the LLDD should abort the exchange.
  1666. *
  1667. * @target_port: pointer to the (registered) target port the LS was
  1668. * received on.
  1669. * @hosthandle: pointer to the host specific data, gets stored in iod.
  1670. * @lsrsp: pointer to a lsrsp structure to be used to reference
  1671. * the exchange corresponding to the LS.
  1672. * @lsreqbuf: pointer to the buffer containing the LS Request
  1673. * @lsreqbuf_len: length, in bytes, of the received LS request
  1674. */
  1675. int
  1676. nvmet_fc_rcv_ls_req(struct nvmet_fc_target_port *target_port,
  1677. void *hosthandle,
  1678. struct nvmefc_ls_rsp *lsrsp,
  1679. void *lsreqbuf, u32 lsreqbuf_len)
  1680. {
  1681. struct nvmet_fc_tgtport *tgtport = targetport_to_tgtport(target_port);
  1682. struct nvmet_fc_ls_iod *iod;
  1683. struct fcnvme_ls_rqst_w0 *w0 = (struct fcnvme_ls_rqst_w0 *)lsreqbuf;
  1684. if (lsreqbuf_len > sizeof(union nvmefc_ls_requests)) {
  1685. dev_info(tgtport->dev,
  1686. "RCV %s LS failed: payload too large (%d)\n",
  1687. (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
  1688. nvmefc_ls_names[w0->ls_cmd] : "",
  1689. lsreqbuf_len);
  1690. return -E2BIG;
  1691. }
  1692. if (!nvmet_fc_tgtport_get(tgtport)) {
  1693. dev_info(tgtport->dev,
  1694. "RCV %s LS failed: target deleting\n",
  1695. (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
  1696. nvmefc_ls_names[w0->ls_cmd] : "");
  1697. return -ESHUTDOWN;
  1698. }
  1699. iod = nvmet_fc_alloc_ls_iod(tgtport);
  1700. if (!iod) {
  1701. dev_info(tgtport->dev,
  1702. "RCV %s LS failed: context allocation failed\n",
  1703. (w0->ls_cmd <= NVME_FC_LAST_LS_CMD_VALUE) ?
  1704. nvmefc_ls_names[w0->ls_cmd] : "");
  1705. nvmet_fc_tgtport_put(tgtport);
  1706. return -ENOENT;
  1707. }
  1708. iod->lsrsp = lsrsp;
  1709. iod->fcpreq = NULL;
  1710. memcpy(iod->rqstbuf, lsreqbuf, lsreqbuf_len);
  1711. iod->rqstdatalen = lsreqbuf_len;
  1712. iod->hosthandle = hosthandle;
  1713. queue_work(nvmet_wq, &iod->work);
  1714. return 0;
  1715. }
  1716. EXPORT_SYMBOL_GPL(nvmet_fc_rcv_ls_req);
  1717. /*
  1718. * **********************
  1719. * Start of FCP handling
  1720. * **********************
  1721. */
  1722. static int
  1723. nvmet_fc_alloc_tgt_pgs(struct nvmet_fc_fcp_iod *fod)
  1724. {
  1725. struct scatterlist *sg;
  1726. unsigned int nent;
  1727. sg = sgl_alloc(fod->req.transfer_len, GFP_KERNEL, &nent);
  1728. if (!sg)
  1729. goto out;
  1730. fod->data_sg = sg;
  1731. fod->data_sg_cnt = nent;
  1732. fod->data_sg_cnt = fc_dma_map_sg(fod->tgtport->dev, sg, nent,
  1733. ((fod->io_dir == NVMET_FCP_WRITE) ?
  1734. DMA_FROM_DEVICE : DMA_TO_DEVICE));
  1735. /* note: write from initiator perspective */
  1736. fod->next_sg = fod->data_sg;
  1737. return 0;
  1738. out:
  1739. return NVME_SC_INTERNAL;
  1740. }
  1741. static void
  1742. nvmet_fc_free_tgt_pgs(struct nvmet_fc_fcp_iod *fod)
  1743. {
  1744. if (!fod->data_sg || !fod->data_sg_cnt)
  1745. return;
  1746. fc_dma_unmap_sg(fod->tgtport->dev, fod->data_sg, fod->data_sg_cnt,
  1747. ((fod->io_dir == NVMET_FCP_WRITE) ?
  1748. DMA_FROM_DEVICE : DMA_TO_DEVICE));
  1749. sgl_free(fod->data_sg);
  1750. fod->data_sg = NULL;
  1751. fod->data_sg_cnt = 0;
  1752. }
  1753. static bool
  1754. queue_90percent_full(struct nvmet_fc_tgt_queue *q, u32 sqhd)
  1755. {
  1756. u32 sqtail, used;
  1757. /* egad, this is ugly. And sqtail is just a best guess */
  1758. sqtail = atomic_read(&q->sqtail) % q->sqsize;
  1759. used = (sqtail < sqhd) ? (sqtail + q->sqsize - sqhd) : (sqtail - sqhd);
  1760. return ((used * 10) >= (((u32)(q->sqsize - 1) * 9)));
  1761. }
  1762. /*
  1763. * Prep RSP payload.
  1764. * May be a NVMET_FCOP_RSP or NVMET_FCOP_READDATA_RSP op
  1765. */
  1766. static void
  1767. nvmet_fc_prep_fcp_rsp(struct nvmet_fc_tgtport *tgtport,
  1768. struct nvmet_fc_fcp_iod *fod)
  1769. {
  1770. struct nvme_fc_ersp_iu *ersp = &fod->rspiubuf;
  1771. struct nvme_common_command *sqe = &fod->cmdiubuf.sqe.common;
  1772. struct nvme_completion *cqe = &ersp->cqe;
  1773. u32 *cqewd = (u32 *)cqe;
  1774. bool send_ersp = false;
  1775. u32 rsn, rspcnt, xfr_length;
  1776. if (fod->fcpreq->op == NVMET_FCOP_READDATA_RSP)
  1777. xfr_length = fod->req.transfer_len;
  1778. else
  1779. xfr_length = fod->offset;
  1780. /*
  1781. * check to see if we can send a 0's rsp.
  1782. * Note: to send a 0's response, the NVME-FC host transport will
  1783. * recreate the CQE. The host transport knows: sq id, SQHD (last
  1784. * seen in an ersp), and command_id. Thus it will create a
  1785. * zero-filled CQE with those known fields filled in. Transport
  1786. * must send an ersp for any condition where the cqe won't match
  1787. * this.
  1788. *
  1789. * Here are the FC-NVME mandated cases where we must send an ersp:
  1790. * every N responses, where N=ersp_ratio
  1791. * force fabric commands to send ersp's (not in FC-NVME but good
  1792. * practice)
  1793. * normal cmds: any time status is non-zero, or status is zero
  1794. * but words 0 or 1 are non-zero.
  1795. * the SQ is 90% or more full
  1796. * the cmd is a fused command
  1797. * transferred data length not equal to cmd iu length
  1798. */
  1799. rspcnt = atomic_inc_return(&fod->queue->zrspcnt);
  1800. if (!(rspcnt % fod->queue->ersp_ratio) ||
  1801. nvme_is_fabrics((struct nvme_command *) sqe) ||
  1802. xfr_length != fod->req.transfer_len ||
  1803. (le16_to_cpu(cqe->status) & 0xFFFE) || cqewd[0] || cqewd[1] ||
  1804. (sqe->flags & (NVME_CMD_FUSE_FIRST | NVME_CMD_FUSE_SECOND)) ||
  1805. queue_90percent_full(fod->queue, le16_to_cpu(cqe->sq_head)))
  1806. send_ersp = true;
  1807. /* re-set the fields */
  1808. fod->fcpreq->rspaddr = ersp;
  1809. fod->fcpreq->rspdma = fod->rspdma;
  1810. if (!send_ersp) {
  1811. memset(ersp, 0, NVME_FC_SIZEOF_ZEROS_RSP);
  1812. fod->fcpreq->rsplen = NVME_FC_SIZEOF_ZEROS_RSP;
  1813. } else {
  1814. ersp->iu_len = cpu_to_be16(sizeof(*ersp)/sizeof(u32));
  1815. rsn = atomic_inc_return(&fod->queue->rsn);
  1816. ersp->rsn = cpu_to_be32(rsn);
  1817. ersp->xfrd_len = cpu_to_be32(xfr_length);
  1818. fod->fcpreq->rsplen = sizeof(*ersp);
  1819. }
  1820. fc_dma_sync_single_for_device(tgtport->dev, fod->rspdma,
  1821. sizeof(fod->rspiubuf), DMA_TO_DEVICE);
  1822. }
  1823. static void nvmet_fc_xmt_fcp_op_done(struct nvmefc_tgt_fcp_req *fcpreq);
  1824. static void
  1825. nvmet_fc_abort_op(struct nvmet_fc_tgtport *tgtport,
  1826. struct nvmet_fc_fcp_iod *fod)
  1827. {
  1828. struct nvmefc_tgt_fcp_req *fcpreq = fod->fcpreq;
  1829. /* data no longer needed */
  1830. nvmet_fc_free_tgt_pgs(fod);
  1831. /*
  1832. * if an ABTS was received or we issued the fcp_abort early
  1833. * don't call abort routine again.
  1834. */
  1835. /* no need to take lock - lock was taken earlier to get here */
  1836. if (!fod->aborted)
  1837. tgtport->ops->fcp_abort(&tgtport->fc_target_port, fcpreq);
  1838. nvmet_fc_free_fcp_iod(fod->queue, fod);
  1839. }
  1840. static void
  1841. nvmet_fc_xmt_fcp_rsp(struct nvmet_fc_tgtport *tgtport,
  1842. struct nvmet_fc_fcp_iod *fod)
  1843. {
  1844. int ret;
  1845. fod->fcpreq->op = NVMET_FCOP_RSP;
  1846. fod->fcpreq->timeout = 0;
  1847. nvmet_fc_prep_fcp_rsp(tgtport, fod);
  1848. ret = tgtport->ops->fcp_op(&tgtport->fc_target_port, fod->fcpreq);
  1849. if (ret)
  1850. nvmet_fc_abort_op(tgtport, fod);
  1851. }
  1852. static void
  1853. nvmet_fc_transfer_fcp_data(struct nvmet_fc_tgtport *tgtport,
  1854. struct nvmet_fc_fcp_iod *fod, u8 op)
  1855. {
  1856. struct nvmefc_tgt_fcp_req *fcpreq = fod->fcpreq;
  1857. struct scatterlist *sg = fod->next_sg;
  1858. unsigned long flags;
  1859. u32 remaininglen = fod->req.transfer_len - fod->offset;
  1860. u32 tlen = 0;
  1861. int ret;
  1862. fcpreq->op = op;
  1863. fcpreq->offset = fod->offset;
  1864. fcpreq->timeout = NVME_FC_TGTOP_TIMEOUT_SEC;
  1865. /*
  1866. * for next sequence:
  1867. * break at a sg element boundary
  1868. * attempt to keep sequence length capped at
  1869. * NVMET_FC_MAX_SEQ_LENGTH but allow sequence to
  1870. * be longer if a single sg element is larger
  1871. * than that amount. This is done to avoid creating
  1872. * a new sg list to use for the tgtport api.
  1873. */
  1874. fcpreq->sg = sg;
  1875. fcpreq->sg_cnt = 0;
  1876. while (tlen < remaininglen &&
  1877. fcpreq->sg_cnt < tgtport->max_sg_cnt &&
  1878. tlen + sg_dma_len(sg) < NVMET_FC_MAX_SEQ_LENGTH) {
  1879. fcpreq->sg_cnt++;
  1880. tlen += sg_dma_len(sg);
  1881. sg = sg_next(sg);
  1882. }
  1883. if (tlen < remaininglen && fcpreq->sg_cnt == 0) {
  1884. fcpreq->sg_cnt++;
  1885. tlen += min_t(u32, sg_dma_len(sg), remaininglen);
  1886. sg = sg_next(sg);
  1887. }
  1888. if (tlen < remaininglen)
  1889. fod->next_sg = sg;
  1890. else
  1891. fod->next_sg = NULL;
  1892. fcpreq->transfer_length = tlen;
  1893. fcpreq->transferred_length = 0;
  1894. fcpreq->fcp_error = 0;
  1895. fcpreq->rsplen = 0;
  1896. /*
  1897. * If the last READDATA request: check if LLDD supports
  1898. * combined xfr with response.
  1899. */
  1900. if ((op == NVMET_FCOP_READDATA) &&
  1901. ((fod->offset + fcpreq->transfer_length) == fod->req.transfer_len) &&
  1902. (tgtport->ops->target_features & NVMET_FCTGTFEAT_READDATA_RSP)) {
  1903. fcpreq->op = NVMET_FCOP_READDATA_RSP;
  1904. nvmet_fc_prep_fcp_rsp(tgtport, fod);
  1905. }
  1906. ret = tgtport->ops->fcp_op(&tgtport->fc_target_port, fod->fcpreq);
  1907. if (ret) {
  1908. /*
  1909. * should be ok to set w/o lock as its in the thread of
  1910. * execution (not an async timer routine) and doesn't
  1911. * contend with any clearing action
  1912. */
  1913. fod->abort = true;
  1914. if (op == NVMET_FCOP_WRITEDATA) {
  1915. spin_lock_irqsave(&fod->flock, flags);
  1916. fod->writedataactive = false;
  1917. spin_unlock_irqrestore(&fod->flock, flags);
  1918. nvmet_req_complete(&fod->req, NVME_SC_INTERNAL);
  1919. } else /* NVMET_FCOP_READDATA or NVMET_FCOP_READDATA_RSP */ {
  1920. fcpreq->fcp_error = ret;
  1921. fcpreq->transferred_length = 0;
  1922. nvmet_fc_xmt_fcp_op_done(fod->fcpreq);
  1923. }
  1924. }
  1925. }
  1926. static inline bool
  1927. __nvmet_fc_fod_op_abort(struct nvmet_fc_fcp_iod *fod, bool abort)
  1928. {
  1929. struct nvmefc_tgt_fcp_req *fcpreq = fod->fcpreq;
  1930. struct nvmet_fc_tgtport *tgtport = fod->tgtport;
  1931. /* if in the middle of an io and we need to tear down */
  1932. if (abort) {
  1933. if (fcpreq->op == NVMET_FCOP_WRITEDATA) {
  1934. nvmet_req_complete(&fod->req, NVME_SC_INTERNAL);
  1935. return true;
  1936. }
  1937. nvmet_fc_abort_op(tgtport, fod);
  1938. return true;
  1939. }
  1940. return false;
  1941. }
  1942. /*
  1943. * actual done handler for FCP operations when completed by the lldd
  1944. */
  1945. static void
  1946. nvmet_fc_fod_op_done(struct nvmet_fc_fcp_iod *fod)
  1947. {
  1948. struct nvmefc_tgt_fcp_req *fcpreq = fod->fcpreq;
  1949. struct nvmet_fc_tgtport *tgtport = fod->tgtport;
  1950. unsigned long flags;
  1951. bool abort;
  1952. spin_lock_irqsave(&fod->flock, flags);
  1953. abort = fod->abort;
  1954. fod->writedataactive = false;
  1955. spin_unlock_irqrestore(&fod->flock, flags);
  1956. switch (fcpreq->op) {
  1957. case NVMET_FCOP_WRITEDATA:
  1958. if (__nvmet_fc_fod_op_abort(fod, abort))
  1959. return;
  1960. if (fcpreq->fcp_error ||
  1961. fcpreq->transferred_length != fcpreq->transfer_length) {
  1962. spin_lock_irqsave(&fod->flock, flags);
  1963. fod->abort = true;
  1964. spin_unlock_irqrestore(&fod->flock, flags);
  1965. nvmet_req_complete(&fod->req, NVME_SC_INTERNAL);
  1966. return;
  1967. }
  1968. fod->offset += fcpreq->transferred_length;
  1969. if (fod->offset != fod->req.transfer_len) {
  1970. spin_lock_irqsave(&fod->flock, flags);
  1971. fod->writedataactive = true;
  1972. spin_unlock_irqrestore(&fod->flock, flags);
  1973. /* transfer the next chunk */
  1974. nvmet_fc_transfer_fcp_data(tgtport, fod,
  1975. NVMET_FCOP_WRITEDATA);
  1976. return;
  1977. }
  1978. /* data transfer complete, resume with nvmet layer */
  1979. fod->req.execute(&fod->req);
  1980. break;
  1981. case NVMET_FCOP_READDATA:
  1982. case NVMET_FCOP_READDATA_RSP:
  1983. if (__nvmet_fc_fod_op_abort(fod, abort))
  1984. return;
  1985. if (fcpreq->fcp_error ||
  1986. fcpreq->transferred_length != fcpreq->transfer_length) {
  1987. nvmet_fc_abort_op(tgtport, fod);
  1988. return;
  1989. }
  1990. /* success */
  1991. if (fcpreq->op == NVMET_FCOP_READDATA_RSP) {
  1992. /* data no longer needed */
  1993. nvmet_fc_free_tgt_pgs(fod);
  1994. nvmet_fc_free_fcp_iod(fod->queue, fod);
  1995. return;
  1996. }
  1997. fod->offset += fcpreq->transferred_length;
  1998. if (fod->offset != fod->req.transfer_len) {
  1999. /* transfer the next chunk */
  2000. nvmet_fc_transfer_fcp_data(tgtport, fod,
  2001. NVMET_FCOP_READDATA);
  2002. return;
  2003. }
  2004. /* data transfer complete, send response */
  2005. /* data no longer needed */
  2006. nvmet_fc_free_tgt_pgs(fod);
  2007. nvmet_fc_xmt_fcp_rsp(tgtport, fod);
  2008. break;
  2009. case NVMET_FCOP_RSP:
  2010. if (__nvmet_fc_fod_op_abort(fod, abort))
  2011. return;
  2012. nvmet_fc_free_fcp_iod(fod->queue, fod);
  2013. break;
  2014. default:
  2015. break;
  2016. }
  2017. }
  2018. static void
  2019. nvmet_fc_xmt_fcp_op_done(struct nvmefc_tgt_fcp_req *fcpreq)
  2020. {
  2021. struct nvmet_fc_fcp_iod *fod = fcpreq->nvmet_fc_private;
  2022. nvmet_fc_fod_op_done(fod);
  2023. }
  2024. /*
  2025. * actual completion handler after execution by the nvmet layer
  2026. */
  2027. static void
  2028. __nvmet_fc_fcp_nvme_cmd_done(struct nvmet_fc_tgtport *tgtport,
  2029. struct nvmet_fc_fcp_iod *fod, int status)
  2030. {
  2031. struct nvme_common_command *sqe = &fod->cmdiubuf.sqe.common;
  2032. struct nvme_completion *cqe = &fod->rspiubuf.cqe;
  2033. unsigned long flags;
  2034. bool abort;
  2035. spin_lock_irqsave(&fod->flock, flags);
  2036. abort = fod->abort;
  2037. spin_unlock_irqrestore(&fod->flock, flags);
  2038. /* if we have a CQE, snoop the last sq_head value */
  2039. if (!status)
  2040. fod->queue->sqhd = cqe->sq_head;
  2041. if (abort) {
  2042. nvmet_fc_abort_op(tgtport, fod);
  2043. return;
  2044. }
  2045. /* if an error handling the cmd post initial parsing */
  2046. if (status) {
  2047. /* fudge up a failed CQE status for our transport error */
  2048. memset(cqe, 0, sizeof(*cqe));
  2049. cqe->sq_head = fod->queue->sqhd; /* echo last cqe sqhd */
  2050. cqe->sq_id = cpu_to_le16(fod->queue->qid);
  2051. cqe->command_id = sqe->command_id;
  2052. cqe->status = cpu_to_le16(status);
  2053. } else {
  2054. /*
  2055. * try to push the data even if the SQE status is non-zero.
  2056. * There may be a status where data still was intended to
  2057. * be moved
  2058. */
  2059. if ((fod->io_dir == NVMET_FCP_READ) && (fod->data_sg_cnt)) {
  2060. /* push the data over before sending rsp */
  2061. nvmet_fc_transfer_fcp_data(tgtport, fod,
  2062. NVMET_FCOP_READDATA);
  2063. return;
  2064. }
  2065. /* writes & no data - fall thru */
  2066. }
  2067. /* data no longer needed */
  2068. nvmet_fc_free_tgt_pgs(fod);
  2069. nvmet_fc_xmt_fcp_rsp(tgtport, fod);
  2070. }
  2071. static void
  2072. nvmet_fc_fcp_nvme_cmd_done(struct nvmet_req *nvme_req)
  2073. {
  2074. struct nvmet_fc_fcp_iod *fod = nvmet_req_to_fod(nvme_req);
  2075. struct nvmet_fc_tgtport *tgtport = fod->tgtport;
  2076. __nvmet_fc_fcp_nvme_cmd_done(tgtport, fod, 0);
  2077. }
  2078. /*
  2079. * Actual processing routine for received FC-NVME I/O Requests from the LLD
  2080. */
  2081. static void
  2082. nvmet_fc_handle_fcp_rqst(struct nvmet_fc_tgtport *tgtport,
  2083. struct nvmet_fc_fcp_iod *fod)
  2084. {
  2085. struct nvme_fc_cmd_iu *cmdiu = &fod->cmdiubuf;
  2086. u32 xfrlen = be32_to_cpu(cmdiu->data_len);
  2087. int ret;
  2088. /*
  2089. * Fused commands are currently not supported in the linux
  2090. * implementation.
  2091. *
  2092. * As such, the implementation of the FC transport does not
  2093. * look at the fused commands and order delivery to the upper
  2094. * layer until we have both based on csn.
  2095. */
  2096. fod->fcpreq->done = nvmet_fc_xmt_fcp_op_done;
  2097. if (cmdiu->flags & FCNVME_CMD_FLAGS_WRITE) {
  2098. fod->io_dir = NVMET_FCP_WRITE;
  2099. if (!nvme_is_write(&cmdiu->sqe))
  2100. goto transport_error;
  2101. } else if (cmdiu->flags & FCNVME_CMD_FLAGS_READ) {
  2102. fod->io_dir = NVMET_FCP_READ;
  2103. if (nvme_is_write(&cmdiu->sqe))
  2104. goto transport_error;
  2105. } else {
  2106. fod->io_dir = NVMET_FCP_NODATA;
  2107. if (xfrlen)
  2108. goto transport_error;
  2109. }
  2110. fod->req.cmd = &fod->cmdiubuf.sqe;
  2111. fod->req.cqe = &fod->rspiubuf.cqe;
  2112. if (!tgtport->pe)
  2113. goto transport_error;
  2114. fod->req.port = tgtport->pe->port;
  2115. /* clear any response payload */
  2116. memset(&fod->rspiubuf, 0, sizeof(fod->rspiubuf));
  2117. fod->data_sg = NULL;
  2118. fod->data_sg_cnt = 0;
  2119. ret = nvmet_req_init(&fod->req,
  2120. &fod->queue->nvme_cq,
  2121. &fod->queue->nvme_sq,
  2122. &nvmet_fc_tgt_fcp_ops);
  2123. if (!ret) {
  2124. /* bad SQE content or invalid ctrl state */
  2125. /* nvmet layer has already called op done to send rsp. */
  2126. return;
  2127. }
  2128. fod->req.transfer_len = xfrlen;
  2129. /* keep a running counter of tail position */
  2130. atomic_inc(&fod->queue->sqtail);
  2131. if (fod->req.transfer_len) {
  2132. ret = nvmet_fc_alloc_tgt_pgs(fod);
  2133. if (ret) {
  2134. nvmet_req_complete(&fod->req, ret);
  2135. return;
  2136. }
  2137. }
  2138. fod->req.sg = fod->data_sg;
  2139. fod->req.sg_cnt = fod->data_sg_cnt;
  2140. fod->offset = 0;
  2141. if (fod->io_dir == NVMET_FCP_WRITE) {
  2142. /* pull the data over before invoking nvmet layer */
  2143. nvmet_fc_transfer_fcp_data(tgtport, fod, NVMET_FCOP_WRITEDATA);
  2144. return;
  2145. }
  2146. /*
  2147. * Reads or no data:
  2148. *
  2149. * can invoke the nvmet_layer now. If read data, cmd completion will
  2150. * push the data
  2151. */
  2152. fod->req.execute(&fod->req);
  2153. return;
  2154. transport_error:
  2155. nvmet_fc_abort_op(tgtport, fod);
  2156. }
  2157. /**
  2158. * nvmet_fc_rcv_fcp_req - transport entry point called by an LLDD
  2159. * upon the reception of a NVME FCP CMD IU.
  2160. *
  2161. * Pass a FC-NVME FCP CMD IU received from the FC link to the nvmet-fc
  2162. * layer for processing.
  2163. *
  2164. * The nvmet_fc layer allocates a local job structure (struct
  2165. * nvmet_fc_fcp_iod) from the queue for the io and copies the
  2166. * CMD IU buffer to the job structure. As such, on a successful
  2167. * completion (returns 0), the LLDD may immediately free/reuse
  2168. * the CMD IU buffer passed in the call.
  2169. *
  2170. * However, in some circumstances, due to the packetized nature of FC
  2171. * and the api of the FC LLDD which may issue a hw command to send the
  2172. * response, but the LLDD may not get the hw completion for that command
  2173. * and upcall the nvmet_fc layer before a new command may be
  2174. * asynchronously received - its possible for a command to be received
  2175. * before the LLDD and nvmet_fc have recycled the job structure. It gives
  2176. * the appearance of more commands received than fits in the sq.
  2177. * To alleviate this scenario, a temporary queue is maintained in the
  2178. * transport for pending LLDD requests waiting for a queue job structure.
  2179. * In these "overrun" cases, a temporary queue element is allocated
  2180. * the LLDD request and CMD iu buffer information remembered, and the
  2181. * routine returns a -EOVERFLOW status. Subsequently, when a queue job
  2182. * structure is freed, it is immediately reallocated for anything on the
  2183. * pending request list. The LLDDs defer_rcv() callback is called,
  2184. * informing the LLDD that it may reuse the CMD IU buffer, and the io
  2185. * is then started normally with the transport.
  2186. *
  2187. * The LLDD, when receiving an -EOVERFLOW completion status, is to treat
  2188. * the completion as successful but must not reuse the CMD IU buffer
  2189. * until the LLDD's defer_rcv() callback has been called for the
  2190. * corresponding struct nvmefc_tgt_fcp_req pointer.
  2191. *
  2192. * If there is any other condition in which an error occurs, the
  2193. * transport will return a non-zero status indicating the error.
  2194. * In all cases other than -EOVERFLOW, the transport has not accepted the
  2195. * request and the LLDD should abort the exchange.
  2196. *
  2197. * @target_port: pointer to the (registered) target port the FCP CMD IU
  2198. * was received on.
  2199. * @fcpreq: pointer to a fcpreq request structure to be used to reference
  2200. * the exchange corresponding to the FCP Exchange.
  2201. * @cmdiubuf: pointer to the buffer containing the FCP CMD IU
  2202. * @cmdiubuf_len: length, in bytes, of the received FCP CMD IU
  2203. */
  2204. int
  2205. nvmet_fc_rcv_fcp_req(struct nvmet_fc_target_port *target_port,
  2206. struct nvmefc_tgt_fcp_req *fcpreq,
  2207. void *cmdiubuf, u32 cmdiubuf_len)
  2208. {
  2209. struct nvmet_fc_tgtport *tgtport = targetport_to_tgtport(target_port);
  2210. struct nvme_fc_cmd_iu *cmdiu = cmdiubuf;
  2211. struct nvmet_fc_tgt_queue *queue;
  2212. struct nvmet_fc_fcp_iod *fod;
  2213. struct nvmet_fc_defer_fcp_req *deferfcp;
  2214. unsigned long flags;
  2215. /* validate iu, so the connection id can be used to find the queue */
  2216. if ((cmdiubuf_len != sizeof(*cmdiu)) ||
  2217. (cmdiu->format_id != NVME_CMD_FORMAT_ID) ||
  2218. (cmdiu->fc_id != NVME_CMD_FC_ID) ||
  2219. (be16_to_cpu(cmdiu->iu_len) != (sizeof(*cmdiu)/4)))
  2220. return -EIO;
  2221. queue = nvmet_fc_find_target_queue(tgtport,
  2222. be64_to_cpu(cmdiu->connection_id));
  2223. if (!queue)
  2224. return -ENOTCONN;
  2225. /*
  2226. * note: reference taken by find_target_queue
  2227. * After successful fod allocation, the fod will inherit the
  2228. * ownership of that reference and will remove the reference
  2229. * when the fod is freed.
  2230. */
  2231. spin_lock_irqsave(&queue->qlock, flags);
  2232. fod = nvmet_fc_alloc_fcp_iod(queue);
  2233. if (fod) {
  2234. spin_unlock_irqrestore(&queue->qlock, flags);
  2235. fcpreq->nvmet_fc_private = fod;
  2236. fod->fcpreq = fcpreq;
  2237. memcpy(&fod->cmdiubuf, cmdiubuf, cmdiubuf_len);
  2238. nvmet_fc_queue_fcp_req(tgtport, queue, fcpreq);
  2239. return 0;
  2240. }
  2241. if (!tgtport->ops->defer_rcv) {
  2242. spin_unlock_irqrestore(&queue->qlock, flags);
  2243. /* release the queue lookup reference */
  2244. nvmet_fc_tgt_q_put(queue);
  2245. return -ENOENT;
  2246. }
  2247. deferfcp = list_first_entry_or_null(&queue->avail_defer_list,
  2248. struct nvmet_fc_defer_fcp_req, req_list);
  2249. if (deferfcp) {
  2250. /* Just re-use one that was previously allocated */
  2251. list_del(&deferfcp->req_list);
  2252. } else {
  2253. spin_unlock_irqrestore(&queue->qlock, flags);
  2254. /* Now we need to dynamically allocate one */
  2255. deferfcp = kmalloc(sizeof(*deferfcp), GFP_KERNEL);
  2256. if (!deferfcp) {
  2257. /* release the queue lookup reference */
  2258. nvmet_fc_tgt_q_put(queue);
  2259. return -ENOMEM;
  2260. }
  2261. spin_lock_irqsave(&queue->qlock, flags);
  2262. }
  2263. /* For now, use rspaddr / rsplen to save payload information */
  2264. fcpreq->rspaddr = cmdiubuf;
  2265. fcpreq->rsplen = cmdiubuf_len;
  2266. deferfcp->fcp_req = fcpreq;
  2267. /* defer processing till a fod becomes available */
  2268. list_add_tail(&deferfcp->req_list, &queue->pending_cmd_list);
  2269. /* NOTE: the queue lookup reference is still valid */
  2270. spin_unlock_irqrestore(&queue->qlock, flags);
  2271. return -EOVERFLOW;
  2272. }
  2273. EXPORT_SYMBOL_GPL(nvmet_fc_rcv_fcp_req);
  2274. /**
  2275. * nvmet_fc_rcv_fcp_abort - transport entry point called by an LLDD
  2276. * upon the reception of an ABTS for a FCP command
  2277. *
  2278. * Notify the transport that an ABTS has been received for a FCP command
  2279. * that had been given to the transport via nvmet_fc_rcv_fcp_req(). The
  2280. * LLDD believes the command is still being worked on
  2281. * (template_ops->fcp_req_release() has not been called).
  2282. *
  2283. * The transport will wait for any outstanding work (an op to the LLDD,
  2284. * which the lldd should complete with error due to the ABTS; or the
  2285. * completion from the nvmet layer of the nvme command), then will
  2286. * stop processing and call the nvmet_fc_rcv_fcp_req() callback to
  2287. * return the i/o context to the LLDD. The LLDD may send the BA_ACC
  2288. * to the ABTS either after return from this function (assuming any
  2289. * outstanding op work has been terminated) or upon the callback being
  2290. * called.
  2291. *
  2292. * @target_port: pointer to the (registered) target port the FCP CMD IU
  2293. * was received on.
  2294. * @fcpreq: pointer to the fcpreq request structure that corresponds
  2295. * to the exchange that received the ABTS.
  2296. */
  2297. void
  2298. nvmet_fc_rcv_fcp_abort(struct nvmet_fc_target_port *target_port,
  2299. struct nvmefc_tgt_fcp_req *fcpreq)
  2300. {
  2301. struct nvmet_fc_fcp_iod *fod = fcpreq->nvmet_fc_private;
  2302. struct nvmet_fc_tgt_queue *queue;
  2303. unsigned long flags;
  2304. if (!fod || fod->fcpreq != fcpreq)
  2305. /* job appears to have already completed, ignore abort */
  2306. return;
  2307. queue = fod->queue;
  2308. spin_lock_irqsave(&queue->qlock, flags);
  2309. if (fod->active) {
  2310. /*
  2311. * mark as abort. The abort handler, invoked upon completion
  2312. * of any work, will detect the aborted status and do the
  2313. * callback.
  2314. */
  2315. spin_lock(&fod->flock);
  2316. fod->abort = true;
  2317. fod->aborted = true;
  2318. spin_unlock(&fod->flock);
  2319. }
  2320. spin_unlock_irqrestore(&queue->qlock, flags);
  2321. }
  2322. EXPORT_SYMBOL_GPL(nvmet_fc_rcv_fcp_abort);
  2323. struct nvmet_fc_traddr {
  2324. u64 nn;
  2325. u64 pn;
  2326. };
  2327. static int
  2328. __nvme_fc_parse_u64(substring_t *sstr, u64 *val)
  2329. {
  2330. u64 token64;
  2331. if (match_u64(sstr, &token64))
  2332. return -EINVAL;
  2333. *val = token64;
  2334. return 0;
  2335. }
  2336. /*
  2337. * This routine validates and extracts the WWN's from the TRADDR string.
  2338. * As kernel parsers need the 0x to determine number base, universally
  2339. * build string to parse with 0x prefix before parsing name strings.
  2340. */
  2341. static int
  2342. nvme_fc_parse_traddr(struct nvmet_fc_traddr *traddr, char *buf, size_t blen)
  2343. {
  2344. char name[2 + NVME_FC_TRADDR_HEXNAMELEN + 1];
  2345. substring_t wwn = { name, &name[sizeof(name)-1] };
  2346. int nnoffset, pnoffset;
  2347. /* validate if string is one of the 2 allowed formats */
  2348. if (strnlen(buf, blen) == NVME_FC_TRADDR_MAXLENGTH &&
  2349. !strncmp(buf, "nn-0x", NVME_FC_TRADDR_OXNNLEN) &&
  2350. !strncmp(&buf[NVME_FC_TRADDR_MAX_PN_OFFSET],
  2351. "pn-0x", NVME_FC_TRADDR_OXNNLEN)) {
  2352. nnoffset = NVME_FC_TRADDR_OXNNLEN;
  2353. pnoffset = NVME_FC_TRADDR_MAX_PN_OFFSET +
  2354. NVME_FC_TRADDR_OXNNLEN;
  2355. } else if ((strnlen(buf, blen) == NVME_FC_TRADDR_MINLENGTH &&
  2356. !strncmp(buf, "nn-", NVME_FC_TRADDR_NNLEN) &&
  2357. !strncmp(&buf[NVME_FC_TRADDR_MIN_PN_OFFSET],
  2358. "pn-", NVME_FC_TRADDR_NNLEN))) {
  2359. nnoffset = NVME_FC_TRADDR_NNLEN;
  2360. pnoffset = NVME_FC_TRADDR_MIN_PN_OFFSET + NVME_FC_TRADDR_NNLEN;
  2361. } else
  2362. goto out_einval;
  2363. name[0] = '0';
  2364. name[1] = 'x';
  2365. name[2 + NVME_FC_TRADDR_HEXNAMELEN] = 0;
  2366. memcpy(&name[2], &buf[nnoffset], NVME_FC_TRADDR_HEXNAMELEN);
  2367. if (__nvme_fc_parse_u64(&wwn, &traddr->nn))
  2368. goto out_einval;
  2369. memcpy(&name[2], &buf[pnoffset], NVME_FC_TRADDR_HEXNAMELEN);
  2370. if (__nvme_fc_parse_u64(&wwn, &traddr->pn))
  2371. goto out_einval;
  2372. return 0;
  2373. out_einval:
  2374. pr_warn("%s: bad traddr string\n", __func__);
  2375. return -EINVAL;
  2376. }
  2377. static int
  2378. nvmet_fc_add_port(struct nvmet_port *port)
  2379. {
  2380. struct nvmet_fc_tgtport *tgtport;
  2381. struct nvmet_fc_port_entry *pe;
  2382. struct nvmet_fc_traddr traddr = { 0L, 0L };
  2383. unsigned long flags;
  2384. int ret;
  2385. /* validate the address info */
  2386. if ((port->disc_addr.trtype != NVMF_TRTYPE_FC) ||
  2387. (port->disc_addr.adrfam != NVMF_ADDR_FAMILY_FC))
  2388. return -EINVAL;
  2389. /* map the traddr address info to a target port */
  2390. ret = nvme_fc_parse_traddr(&traddr, port->disc_addr.traddr,
  2391. sizeof(port->disc_addr.traddr));
  2392. if (ret)
  2393. return ret;
  2394. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  2395. if (!pe)
  2396. return -ENOMEM;
  2397. ret = -ENXIO;
  2398. spin_lock_irqsave(&nvmet_fc_tgtlock, flags);
  2399. list_for_each_entry(tgtport, &nvmet_fc_target_list, tgt_list) {
  2400. if ((tgtport->fc_target_port.node_name == traddr.nn) &&
  2401. (tgtport->fc_target_port.port_name == traddr.pn)) {
  2402. /* a FC port can only be 1 nvmet port id */
  2403. if (!tgtport->pe) {
  2404. nvmet_fc_portentry_bind(tgtport, pe, port);
  2405. ret = 0;
  2406. } else
  2407. ret = -EALREADY;
  2408. break;
  2409. }
  2410. }
  2411. spin_unlock_irqrestore(&nvmet_fc_tgtlock, flags);
  2412. if (ret)
  2413. kfree(pe);
  2414. return ret;
  2415. }
  2416. static void
  2417. nvmet_fc_remove_port(struct nvmet_port *port)
  2418. {
  2419. struct nvmet_fc_port_entry *pe = port->priv;
  2420. nvmet_fc_portentry_unbind(pe);
  2421. /* terminate any outstanding associations */
  2422. __nvmet_fc_free_assocs(pe->tgtport);
  2423. kfree(pe);
  2424. }
  2425. static void
  2426. nvmet_fc_discovery_chg(struct nvmet_port *port)
  2427. {
  2428. struct nvmet_fc_port_entry *pe = port->priv;
  2429. struct nvmet_fc_tgtport *tgtport = pe->tgtport;
  2430. if (tgtport && tgtport->ops->discovery_event)
  2431. tgtport->ops->discovery_event(&tgtport->fc_target_port);
  2432. }
  2433. static ssize_t
  2434. nvmet_fc_host_traddr(struct nvmet_ctrl *ctrl,
  2435. char *traddr, size_t traddr_size)
  2436. {
  2437. struct nvmet_sq *sq = ctrl->sqs[0];
  2438. struct nvmet_fc_tgt_queue *queue =
  2439. container_of(sq, struct nvmet_fc_tgt_queue, nvme_sq);
  2440. struct nvmet_fc_tgtport *tgtport = queue->assoc ? queue->assoc->tgtport : NULL;
  2441. struct nvmet_fc_hostport *hostport = queue->assoc ? queue->assoc->hostport : NULL;
  2442. u64 wwnn, wwpn;
  2443. ssize_t ret = 0;
  2444. if (!tgtport || !nvmet_fc_tgtport_get(tgtport))
  2445. return -ENODEV;
  2446. if (!hostport || !nvmet_fc_hostport_get(hostport)) {
  2447. ret = -ENODEV;
  2448. goto out_put;
  2449. }
  2450. if (tgtport->ops->host_traddr) {
  2451. ret = tgtport->ops->host_traddr(hostport->hosthandle, &wwnn, &wwpn);
  2452. if (ret)
  2453. goto out_put_host;
  2454. ret = snprintf(traddr, traddr_size, "nn-0x%llx:pn-0x%llx", wwnn, wwpn);
  2455. }
  2456. out_put_host:
  2457. nvmet_fc_hostport_put(hostport);
  2458. out_put:
  2459. nvmet_fc_tgtport_put(tgtport);
  2460. return ret;
  2461. }
  2462. static const struct nvmet_fabrics_ops nvmet_fc_tgt_fcp_ops = {
  2463. .owner = THIS_MODULE,
  2464. .type = NVMF_TRTYPE_FC,
  2465. .msdbd = 1,
  2466. .add_port = nvmet_fc_add_port,
  2467. .remove_port = nvmet_fc_remove_port,
  2468. .queue_response = nvmet_fc_fcp_nvme_cmd_done,
  2469. .delete_ctrl = nvmet_fc_delete_ctrl,
  2470. .discovery_chg = nvmet_fc_discovery_chg,
  2471. .host_traddr = nvmet_fc_host_traddr,
  2472. };
  2473. static int __init nvmet_fc_init_module(void)
  2474. {
  2475. return nvmet_register_transport(&nvmet_fc_tgt_fcp_ops);
  2476. }
  2477. static void __exit nvmet_fc_exit_module(void)
  2478. {
  2479. /* ensure any shutdown operation, e.g. delete ctrls have finished */
  2480. flush_workqueue(nvmet_wq);
  2481. /* sanity check - all lports should be removed */
  2482. if (!list_empty(&nvmet_fc_target_list))
  2483. pr_warn("%s: targetport list not empty\n", __func__);
  2484. nvmet_unregister_transport(&nvmet_fc_tgt_fcp_ops);
  2485. ida_destroy(&nvmet_fc_tgtport_cnt);
  2486. }
  2487. module_init(nvmet_fc_init_module);
  2488. module_exit(nvmet_fc_exit_module);
  2489. MODULE_DESCRIPTION("NVMe target FC transport driver");
  2490. MODULE_LICENSE("GPL v2");