fc.c 68 KB

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