fc.c 67 KB

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