fc.c 68 KB

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