qedf_io.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * QLogic FCoE Offload Driver
  4. * Copyright (c) 2016-2018 Cavium Inc.
  5. */
  6. #include <linux/spinlock.h>
  7. #include <linux/vmalloc.h>
  8. #include "qedf.h"
  9. #include <scsi/scsi_tcq.h>
  10. void qedf_cmd_timer_set(struct qedf_ctx *qedf, struct qedf_ioreq *io_req,
  11. unsigned int timer_msec)
  12. {
  13. queue_delayed_work(qedf->timer_work_queue, &io_req->timeout_work,
  14. msecs_to_jiffies(timer_msec));
  15. }
  16. static void qedf_cmd_timeout(struct work_struct *work)
  17. {
  18. struct qedf_ioreq *io_req =
  19. container_of(work, struct qedf_ioreq, timeout_work.work);
  20. struct qedf_ctx *qedf;
  21. struct qedf_rport *fcport;
  22. fcport = io_req->fcport;
  23. if (io_req->fcport == NULL) {
  24. QEDF_INFO(NULL, QEDF_LOG_IO, "fcport is NULL.\n");
  25. return;
  26. }
  27. qedf = fcport->qedf;
  28. switch (io_req->cmd_type) {
  29. case QEDF_ABTS:
  30. if (qedf == NULL) {
  31. QEDF_INFO(NULL, QEDF_LOG_IO,
  32. "qedf is NULL for ABTS xid=0x%x.\n",
  33. io_req->xid);
  34. return;
  35. }
  36. QEDF_ERR((&qedf->dbg_ctx), "ABTS timeout, xid=0x%x.\n",
  37. io_req->xid);
  38. /* Cleanup timed out ABTS */
  39. qedf_initiate_cleanup(io_req, true);
  40. complete(&io_req->abts_done);
  41. /*
  42. * Need to call kref_put for reference taken when initiate_abts
  43. * was called since abts_compl won't be called now that we've
  44. * cleaned up the task.
  45. */
  46. kref_put(&io_req->refcount, qedf_release_cmd);
  47. /* Clear in abort bit now that we're done with the command */
  48. clear_bit(QEDF_CMD_IN_ABORT, &io_req->flags);
  49. /*
  50. * Now that the original I/O and the ABTS are complete see
  51. * if we need to reconnect to the target.
  52. */
  53. qedf_restart_rport(fcport);
  54. break;
  55. case QEDF_ELS:
  56. if (!qedf) {
  57. QEDF_INFO(NULL, QEDF_LOG_IO,
  58. "qedf is NULL for ELS xid=0x%x.\n",
  59. io_req->xid);
  60. return;
  61. }
  62. /* ELS request no longer outstanding since it timed out */
  63. clear_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
  64. kref_get(&io_req->refcount);
  65. /*
  66. * Don't attempt to clean an ELS timeout as any subseqeunt
  67. * ABTS or cleanup requests just hang. For now just free
  68. * the resources of the original I/O and the RRQ
  69. */
  70. QEDF_ERR(&(qedf->dbg_ctx), "ELS timeout, xid=0x%x.\n",
  71. io_req->xid);
  72. qedf_initiate_cleanup(io_req, true);
  73. io_req->event = QEDF_IOREQ_EV_ELS_TMO;
  74. /* Call callback function to complete command */
  75. if (io_req->cb_func && io_req->cb_arg) {
  76. io_req->cb_func(io_req->cb_arg);
  77. io_req->cb_arg = NULL;
  78. }
  79. kref_put(&io_req->refcount, qedf_release_cmd);
  80. break;
  81. case QEDF_SEQ_CLEANUP:
  82. QEDF_ERR(&(qedf->dbg_ctx), "Sequence cleanup timeout, "
  83. "xid=0x%x.\n", io_req->xid);
  84. qedf_initiate_cleanup(io_req, true);
  85. io_req->event = QEDF_IOREQ_EV_ELS_TMO;
  86. qedf_process_seq_cleanup_compl(qedf, NULL, io_req);
  87. break;
  88. default:
  89. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
  90. "Hit default case, xid=0x%x.\n", io_req->xid);
  91. break;
  92. }
  93. }
  94. void qedf_cmd_mgr_free(struct qedf_cmd_mgr *cmgr)
  95. {
  96. struct io_bdt *bdt_info;
  97. struct qedf_ctx *qedf = cmgr->qedf;
  98. size_t bd_tbl_sz;
  99. u16 min_xid = 0;
  100. u16 max_xid = (FCOE_PARAMS_NUM_TASKS - 1);
  101. int num_ios;
  102. int i;
  103. struct qedf_ioreq *io_req;
  104. num_ios = max_xid - min_xid + 1;
  105. /* Free fcoe_bdt_ctx structures */
  106. if (!cmgr->io_bdt_pool) {
  107. QEDF_ERR(&qedf->dbg_ctx, "io_bdt_pool is NULL.\n");
  108. goto free_cmd_pool;
  109. }
  110. bd_tbl_sz = QEDF_MAX_BDS_PER_CMD * sizeof(struct scsi_sge);
  111. for (i = 0; i < num_ios; i++) {
  112. bdt_info = cmgr->io_bdt_pool[i];
  113. if (bdt_info->bd_tbl) {
  114. dma_free_coherent(&qedf->pdev->dev, bd_tbl_sz,
  115. bdt_info->bd_tbl, bdt_info->bd_tbl_dma);
  116. bdt_info->bd_tbl = NULL;
  117. }
  118. }
  119. /* Destroy io_bdt pool */
  120. for (i = 0; i < num_ios; i++) {
  121. kfree(cmgr->io_bdt_pool[i]);
  122. cmgr->io_bdt_pool[i] = NULL;
  123. }
  124. kfree(cmgr->io_bdt_pool);
  125. cmgr->io_bdt_pool = NULL;
  126. free_cmd_pool:
  127. for (i = 0; i < num_ios; i++) {
  128. io_req = &cmgr->cmds[i];
  129. kfree(io_req->sgl_task_params);
  130. kfree(io_req->task_params);
  131. /* Make sure we free per command sense buffer */
  132. if (io_req->sense_buffer)
  133. dma_free_coherent(&qedf->pdev->dev,
  134. QEDF_SCSI_SENSE_BUFFERSIZE, io_req->sense_buffer,
  135. io_req->sense_buffer_dma);
  136. cancel_delayed_work_sync(&io_req->rrq_work);
  137. }
  138. /* Free command manager itself */
  139. vfree(cmgr);
  140. }
  141. static void qedf_handle_rrq(struct work_struct *work)
  142. {
  143. struct qedf_ioreq *io_req =
  144. container_of(work, struct qedf_ioreq, rrq_work.work);
  145. atomic_set(&io_req->state, QEDFC_CMD_ST_RRQ_ACTIVE);
  146. qedf_send_rrq(io_req);
  147. }
  148. struct qedf_cmd_mgr *qedf_cmd_mgr_alloc(struct qedf_ctx *qedf)
  149. {
  150. struct qedf_cmd_mgr *cmgr;
  151. struct io_bdt *bdt_info;
  152. struct qedf_ioreq *io_req;
  153. u16 xid;
  154. int i;
  155. int num_ios;
  156. u16 min_xid = 0;
  157. u16 max_xid = (FCOE_PARAMS_NUM_TASKS - 1);
  158. /* Make sure num_queues is already set before calling this function */
  159. if (!qedf->num_queues) {
  160. QEDF_ERR(&(qedf->dbg_ctx), "num_queues is not set.\n");
  161. return NULL;
  162. }
  163. if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN) {
  164. QEDF_WARN(&(qedf->dbg_ctx), "Invalid min_xid 0x%x and "
  165. "max_xid 0x%x.\n", min_xid, max_xid);
  166. return NULL;
  167. }
  168. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "min xid 0x%x, max xid "
  169. "0x%x.\n", min_xid, max_xid);
  170. num_ios = max_xid - min_xid + 1;
  171. cmgr = vzalloc(sizeof(struct qedf_cmd_mgr));
  172. if (!cmgr) {
  173. QEDF_WARN(&(qedf->dbg_ctx), "Failed to alloc cmd mgr.\n");
  174. return NULL;
  175. }
  176. cmgr->qedf = qedf;
  177. spin_lock_init(&cmgr->lock);
  178. /*
  179. * Initialize I/O request fields.
  180. */
  181. xid = 0;
  182. for (i = 0; i < num_ios; i++) {
  183. io_req = &cmgr->cmds[i];
  184. INIT_DELAYED_WORK(&io_req->timeout_work, qedf_cmd_timeout);
  185. io_req->xid = xid++;
  186. INIT_DELAYED_WORK(&io_req->rrq_work, qedf_handle_rrq);
  187. /* Allocate DMA memory to hold sense buffer */
  188. io_req->sense_buffer = dma_alloc_coherent(&qedf->pdev->dev,
  189. QEDF_SCSI_SENSE_BUFFERSIZE, &io_req->sense_buffer_dma,
  190. GFP_KERNEL);
  191. if (!io_req->sense_buffer) {
  192. QEDF_ERR(&qedf->dbg_ctx,
  193. "Failed to alloc sense buffer.\n");
  194. goto mem_err;
  195. }
  196. /* Allocate task parameters to pass to f/w init funcions */
  197. io_req->task_params = kzalloc(sizeof(*io_req->task_params),
  198. GFP_KERNEL);
  199. if (!io_req->task_params) {
  200. QEDF_ERR(&(qedf->dbg_ctx),
  201. "Failed to allocate task_params for xid=0x%x\n",
  202. i);
  203. goto mem_err;
  204. }
  205. /*
  206. * Allocate scatter/gather list info to pass to f/w init
  207. * functions.
  208. */
  209. io_req->sgl_task_params = kzalloc(
  210. sizeof(struct scsi_sgl_task_params), GFP_KERNEL);
  211. if (!io_req->sgl_task_params) {
  212. QEDF_ERR(&(qedf->dbg_ctx),
  213. "Failed to allocate sgl_task_params for xid=0x%x\n",
  214. i);
  215. goto mem_err;
  216. }
  217. }
  218. /* Allocate pool of io_bdts - one for each qedf_ioreq */
  219. cmgr->io_bdt_pool = kmalloc_array(num_ios, sizeof(struct io_bdt *),
  220. GFP_KERNEL);
  221. if (!cmgr->io_bdt_pool) {
  222. QEDF_WARN(&(qedf->dbg_ctx), "Failed to alloc io_bdt_pool.\n");
  223. goto mem_err;
  224. }
  225. for (i = 0; i < num_ios; i++) {
  226. cmgr->io_bdt_pool[i] = kmalloc(sizeof(struct io_bdt),
  227. GFP_KERNEL);
  228. if (!cmgr->io_bdt_pool[i]) {
  229. QEDF_WARN(&(qedf->dbg_ctx),
  230. "Failed to alloc io_bdt_pool[%d].\n", i);
  231. goto mem_err;
  232. }
  233. }
  234. for (i = 0; i < num_ios; i++) {
  235. bdt_info = cmgr->io_bdt_pool[i];
  236. bdt_info->bd_tbl = dma_alloc_coherent(&qedf->pdev->dev,
  237. QEDF_MAX_BDS_PER_CMD * sizeof(struct scsi_sge),
  238. &bdt_info->bd_tbl_dma, GFP_KERNEL);
  239. if (!bdt_info->bd_tbl) {
  240. QEDF_WARN(&(qedf->dbg_ctx),
  241. "Failed to alloc bdt_tbl[%d].\n", i);
  242. goto mem_err;
  243. }
  244. }
  245. atomic_set(&cmgr->free_list_cnt, num_ios);
  246. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  247. "cmgr->free_list_cnt=%d.\n",
  248. atomic_read(&cmgr->free_list_cnt));
  249. return cmgr;
  250. mem_err:
  251. qedf_cmd_mgr_free(cmgr);
  252. return NULL;
  253. }
  254. struct qedf_ioreq *qedf_alloc_cmd(struct qedf_rport *fcport, u8 cmd_type)
  255. {
  256. struct qedf_ctx *qedf = fcport->qedf;
  257. struct qedf_cmd_mgr *cmd_mgr = qedf->cmd_mgr;
  258. struct qedf_ioreq *io_req = NULL;
  259. struct io_bdt *bd_tbl;
  260. u16 xid;
  261. uint32_t free_sqes;
  262. int i;
  263. unsigned long flags;
  264. free_sqes = atomic_read(&fcport->free_sqes);
  265. if (!free_sqes) {
  266. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  267. "Returning NULL, free_sqes=%d.\n ",
  268. free_sqes);
  269. goto out_failed;
  270. }
  271. /* Limit the number of outstanding R/W tasks */
  272. if ((atomic_read(&fcport->num_active_ios) >=
  273. NUM_RW_TASKS_PER_CONNECTION)) {
  274. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  275. "Returning NULL, num_active_ios=%d.\n",
  276. atomic_read(&fcport->num_active_ios));
  277. goto out_failed;
  278. }
  279. /* Limit global TIDs certain tasks */
  280. if (atomic_read(&cmd_mgr->free_list_cnt) <= GBL_RSVD_TASKS) {
  281. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  282. "Returning NULL, free_list_cnt=%d.\n",
  283. atomic_read(&cmd_mgr->free_list_cnt));
  284. goto out_failed;
  285. }
  286. spin_lock_irqsave(&cmd_mgr->lock, flags);
  287. for (i = 0; i < FCOE_PARAMS_NUM_TASKS; i++) {
  288. io_req = &cmd_mgr->cmds[cmd_mgr->idx];
  289. cmd_mgr->idx++;
  290. if (cmd_mgr->idx == FCOE_PARAMS_NUM_TASKS)
  291. cmd_mgr->idx = 0;
  292. /* Check to make sure command was previously freed */
  293. if (!io_req->alloc)
  294. break;
  295. }
  296. if (i == FCOE_PARAMS_NUM_TASKS) {
  297. spin_unlock_irqrestore(&cmd_mgr->lock, flags);
  298. goto out_failed;
  299. }
  300. if (test_bit(QEDF_CMD_DIRTY, &io_req->flags))
  301. QEDF_ERR(&qedf->dbg_ctx,
  302. "io_req found to be dirty ox_id = 0x%x.\n",
  303. io_req->xid);
  304. /* Clear any flags now that we've reallocated the xid */
  305. io_req->flags = 0;
  306. io_req->alloc = 1;
  307. spin_unlock_irqrestore(&cmd_mgr->lock, flags);
  308. atomic_inc(&fcport->num_active_ios);
  309. atomic_dec(&fcport->free_sqes);
  310. xid = io_req->xid;
  311. atomic_dec(&cmd_mgr->free_list_cnt);
  312. io_req->cmd_mgr = cmd_mgr;
  313. io_req->fcport = fcport;
  314. /* Clear any stale sc_cmd back pointer */
  315. io_req->sc_cmd = NULL;
  316. io_req->lun = -1;
  317. /* Hold the io_req against deletion */
  318. kref_init(&io_req->refcount); /* ID: 001 */
  319. atomic_set(&io_req->state, QEDFC_CMD_ST_IO_ACTIVE);
  320. /* Bind io_bdt for this io_req */
  321. /* Have a static link between io_req and io_bdt_pool */
  322. bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid];
  323. if (bd_tbl == NULL) {
  324. QEDF_ERR(&(qedf->dbg_ctx), "bd_tbl is NULL, xid=%x.\n", xid);
  325. kref_put(&io_req->refcount, qedf_release_cmd);
  326. goto out_failed;
  327. }
  328. bd_tbl->io_req = io_req;
  329. io_req->cmd_type = cmd_type;
  330. io_req->tm_flags = 0;
  331. /* Reset sequence offset data */
  332. io_req->rx_buf_off = 0;
  333. io_req->tx_buf_off = 0;
  334. io_req->rx_id = 0xffff; /* No OX_ID */
  335. return io_req;
  336. out_failed:
  337. /* Record failure for stats and return NULL to caller */
  338. qedf->alloc_failures++;
  339. return NULL;
  340. }
  341. static void qedf_free_mp_resc(struct qedf_ioreq *io_req)
  342. {
  343. struct qedf_mp_req *mp_req = &(io_req->mp_req);
  344. struct qedf_ctx *qedf = io_req->fcport->qedf;
  345. uint64_t sz = sizeof(struct scsi_sge);
  346. /* clear tm flags */
  347. if (mp_req->mp_req_bd) {
  348. dma_free_coherent(&qedf->pdev->dev, sz,
  349. mp_req->mp_req_bd, mp_req->mp_req_bd_dma);
  350. mp_req->mp_req_bd = NULL;
  351. }
  352. if (mp_req->mp_resp_bd) {
  353. dma_free_coherent(&qedf->pdev->dev, sz,
  354. mp_req->mp_resp_bd, mp_req->mp_resp_bd_dma);
  355. mp_req->mp_resp_bd = NULL;
  356. }
  357. if (mp_req->req_buf) {
  358. dma_free_coherent(&qedf->pdev->dev, QEDF_PAGE_SIZE,
  359. mp_req->req_buf, mp_req->req_buf_dma);
  360. mp_req->req_buf = NULL;
  361. }
  362. if (mp_req->resp_buf) {
  363. dma_free_coherent(&qedf->pdev->dev, QEDF_PAGE_SIZE,
  364. mp_req->resp_buf, mp_req->resp_buf_dma);
  365. mp_req->resp_buf = NULL;
  366. }
  367. }
  368. void qedf_release_cmd(struct kref *ref)
  369. {
  370. struct qedf_ioreq *io_req =
  371. container_of(ref, struct qedf_ioreq, refcount);
  372. struct qedf_cmd_mgr *cmd_mgr = io_req->cmd_mgr;
  373. struct qedf_rport *fcport = io_req->fcport;
  374. unsigned long flags;
  375. if (io_req->cmd_type == QEDF_SCSI_CMD) {
  376. QEDF_WARN(&fcport->qedf->dbg_ctx,
  377. "Cmd released called without scsi_done called, io_req %p xid=0x%x.\n",
  378. io_req, io_req->xid);
  379. WARN_ON(io_req->sc_cmd);
  380. }
  381. if (io_req->cmd_type == QEDF_ELS ||
  382. io_req->cmd_type == QEDF_TASK_MGMT_CMD)
  383. qedf_free_mp_resc(io_req);
  384. atomic_inc(&cmd_mgr->free_list_cnt);
  385. atomic_dec(&fcport->num_active_ios);
  386. atomic_set(&io_req->state, QEDF_CMD_ST_INACTIVE);
  387. if (atomic_read(&fcport->num_active_ios) < 0) {
  388. QEDF_WARN(&(fcport->qedf->dbg_ctx), "active_ios < 0.\n");
  389. WARN_ON(1);
  390. }
  391. /* Increment task retry identifier now that the request is released */
  392. io_req->task_retry_identifier++;
  393. io_req->fcport = NULL;
  394. clear_bit(QEDF_CMD_DIRTY, &io_req->flags);
  395. io_req->cpu = 0;
  396. spin_lock_irqsave(&cmd_mgr->lock, flags);
  397. io_req->fcport = NULL;
  398. io_req->alloc = 0;
  399. spin_unlock_irqrestore(&cmd_mgr->lock, flags);
  400. }
  401. static int qedf_map_sg(struct qedf_ioreq *io_req)
  402. {
  403. struct scsi_cmnd *sc = io_req->sc_cmd;
  404. struct Scsi_Host *host = sc->device->host;
  405. struct fc_lport *lport = shost_priv(host);
  406. struct qedf_ctx *qedf = lport_priv(lport);
  407. struct scsi_sge *bd = io_req->bd_tbl->bd_tbl;
  408. struct scatterlist *sg;
  409. int byte_count = 0;
  410. int sg_count = 0;
  411. int bd_count = 0;
  412. u32 sg_len;
  413. u64 addr;
  414. int i = 0;
  415. sg_count = dma_map_sg(&qedf->pdev->dev, scsi_sglist(sc),
  416. scsi_sg_count(sc), sc->sc_data_direction);
  417. sg = scsi_sglist(sc);
  418. io_req->sge_type = QEDF_IOREQ_UNKNOWN_SGE;
  419. if (sg_count <= 8 || io_req->io_req_flags == QEDF_READ)
  420. io_req->sge_type = QEDF_IOREQ_FAST_SGE;
  421. scsi_for_each_sg(sc, sg, sg_count, i) {
  422. sg_len = (u32)sg_dma_len(sg);
  423. addr = (u64)sg_dma_address(sg);
  424. /*
  425. * Intermediate s/g element so check if start address
  426. * is page aligned. Only required for writes and only if the
  427. * number of scatter/gather elements is 8 or more.
  428. */
  429. if (io_req->sge_type == QEDF_IOREQ_UNKNOWN_SGE && (i) &&
  430. (i != (sg_count - 1)) && sg_len < QEDF_PAGE_SIZE)
  431. io_req->sge_type = QEDF_IOREQ_SLOW_SGE;
  432. bd[bd_count].sge_addr.lo = cpu_to_le32(U64_LO(addr));
  433. bd[bd_count].sge_addr.hi = cpu_to_le32(U64_HI(addr));
  434. bd[bd_count].sge_len = cpu_to_le32(sg_len);
  435. bd_count++;
  436. byte_count += sg_len;
  437. }
  438. /* To catch a case where FAST and SLOW nothing is set, set FAST */
  439. if (io_req->sge_type == QEDF_IOREQ_UNKNOWN_SGE)
  440. io_req->sge_type = QEDF_IOREQ_FAST_SGE;
  441. if (byte_count != scsi_bufflen(sc))
  442. QEDF_ERR(&(qedf->dbg_ctx), "byte_count = %d != "
  443. "scsi_bufflen = %d, task_id = 0x%x.\n", byte_count,
  444. scsi_bufflen(sc), io_req->xid);
  445. return bd_count;
  446. }
  447. static int qedf_build_bd_list_from_sg(struct qedf_ioreq *io_req)
  448. {
  449. struct scsi_cmnd *sc = io_req->sc_cmd;
  450. struct scsi_sge *bd = io_req->bd_tbl->bd_tbl;
  451. int bd_count;
  452. if (scsi_sg_count(sc)) {
  453. bd_count = qedf_map_sg(io_req);
  454. if (bd_count == 0)
  455. return -ENOMEM;
  456. } else {
  457. bd_count = 0;
  458. bd[0].sge_addr.lo = bd[0].sge_addr.hi = 0;
  459. bd[0].sge_len = 0;
  460. }
  461. io_req->bd_tbl->bd_valid = bd_count;
  462. return 0;
  463. }
  464. static void qedf_build_fcp_cmnd(struct qedf_ioreq *io_req,
  465. struct fcp_cmnd *fcp_cmnd)
  466. {
  467. struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
  468. /* fcp_cmnd is 32 bytes */
  469. memset(fcp_cmnd, 0, FCP_CMND_LEN);
  470. /* 8 bytes: SCSI LUN info */
  471. int_to_scsilun(sc_cmd->device->lun,
  472. (struct scsi_lun *)&fcp_cmnd->fc_lun);
  473. /* 4 bytes: flag info */
  474. fcp_cmnd->fc_pri_ta = 0;
  475. fcp_cmnd->fc_tm_flags = io_req->tm_flags;
  476. fcp_cmnd->fc_flags = io_req->io_req_flags;
  477. fcp_cmnd->fc_cmdref = 0;
  478. /* Populate data direction */
  479. if (io_req->cmd_type == QEDF_TASK_MGMT_CMD) {
  480. fcp_cmnd->fc_flags |= FCP_CFL_RDDATA;
  481. } else {
  482. if (sc_cmd->sc_data_direction == DMA_TO_DEVICE)
  483. fcp_cmnd->fc_flags |= FCP_CFL_WRDATA;
  484. else if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE)
  485. fcp_cmnd->fc_flags |= FCP_CFL_RDDATA;
  486. }
  487. fcp_cmnd->fc_pri_ta = FCP_PTA_SIMPLE;
  488. /* 16 bytes: CDB information */
  489. if (io_req->cmd_type != QEDF_TASK_MGMT_CMD)
  490. memcpy(fcp_cmnd->fc_cdb, sc_cmd->cmnd, sc_cmd->cmd_len);
  491. /* 4 bytes: FCP data length */
  492. fcp_cmnd->fc_dl = htonl(io_req->data_xfer_len);
  493. }
  494. static void qedf_init_task(struct qedf_rport *fcport, struct fc_lport *lport,
  495. struct qedf_ioreq *io_req, struct fcoe_task_context *task_ctx,
  496. struct fcoe_wqe *sqe)
  497. {
  498. enum fcoe_task_type task_type;
  499. struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
  500. struct io_bdt *bd_tbl = io_req->bd_tbl;
  501. u8 fcp_cmnd[32];
  502. u32 tmp_fcp_cmnd[8];
  503. int bd_count = 0;
  504. struct qedf_ctx *qedf = fcport->qedf;
  505. uint16_t cq_idx = smp_processor_id() % qedf->num_queues;
  506. struct regpair sense_data_buffer_phys_addr;
  507. u32 tx_io_size = 0;
  508. u32 rx_io_size = 0;
  509. int i, cnt;
  510. /* Note init_initiator_rw_fcoe_task memsets the task context */
  511. io_req->task = task_ctx;
  512. memset(task_ctx, 0, sizeof(struct fcoe_task_context));
  513. memset(io_req->task_params, 0, sizeof(struct fcoe_task_params));
  514. memset(io_req->sgl_task_params, 0, sizeof(struct scsi_sgl_task_params));
  515. /* Set task type bassed on DMA directio of command */
  516. if (io_req->cmd_type == QEDF_TASK_MGMT_CMD) {
  517. task_type = FCOE_TASK_TYPE_READ_INITIATOR;
  518. } else {
  519. if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
  520. task_type = FCOE_TASK_TYPE_WRITE_INITIATOR;
  521. tx_io_size = io_req->data_xfer_len;
  522. } else {
  523. task_type = FCOE_TASK_TYPE_READ_INITIATOR;
  524. rx_io_size = io_req->data_xfer_len;
  525. }
  526. }
  527. /* Setup the fields for fcoe_task_params */
  528. io_req->task_params->context = task_ctx;
  529. io_req->task_params->sqe = sqe;
  530. io_req->task_params->task_type = task_type;
  531. io_req->task_params->tx_io_size = tx_io_size;
  532. io_req->task_params->rx_io_size = rx_io_size;
  533. io_req->task_params->conn_cid = fcport->fw_cid;
  534. io_req->task_params->itid = io_req->xid;
  535. io_req->task_params->cq_rss_number = cq_idx;
  536. io_req->task_params->is_tape_device = fcport->dev_type;
  537. /* Fill in information for scatter/gather list */
  538. if (io_req->cmd_type != QEDF_TASK_MGMT_CMD) {
  539. bd_count = bd_tbl->bd_valid;
  540. io_req->sgl_task_params->sgl = bd_tbl->bd_tbl;
  541. io_req->sgl_task_params->sgl_phys_addr.lo =
  542. U64_LO(bd_tbl->bd_tbl_dma);
  543. io_req->sgl_task_params->sgl_phys_addr.hi =
  544. U64_HI(bd_tbl->bd_tbl_dma);
  545. io_req->sgl_task_params->num_sges = bd_count;
  546. io_req->sgl_task_params->total_buffer_size =
  547. scsi_bufflen(io_req->sc_cmd);
  548. if (io_req->sge_type == QEDF_IOREQ_SLOW_SGE)
  549. io_req->sgl_task_params->small_mid_sge = 1;
  550. else
  551. io_req->sgl_task_params->small_mid_sge = 0;
  552. }
  553. /* Fill in physical address of sense buffer */
  554. sense_data_buffer_phys_addr.lo = U64_LO(io_req->sense_buffer_dma);
  555. sense_data_buffer_phys_addr.hi = U64_HI(io_req->sense_buffer_dma);
  556. /* fill FCP_CMND IU */
  557. qedf_build_fcp_cmnd(io_req, (struct fcp_cmnd *)tmp_fcp_cmnd);
  558. /* Swap fcp_cmnd since FC is big endian */
  559. cnt = sizeof(struct fcp_cmnd) / sizeof(u32);
  560. for (i = 0; i < cnt; i++) {
  561. tmp_fcp_cmnd[i] = cpu_to_be32(tmp_fcp_cmnd[i]);
  562. }
  563. memcpy(fcp_cmnd, tmp_fcp_cmnd, sizeof(struct fcp_cmnd));
  564. init_initiator_rw_fcoe_task(io_req->task_params,
  565. io_req->sgl_task_params,
  566. sense_data_buffer_phys_addr,
  567. io_req->task_retry_identifier, fcp_cmnd);
  568. /* Increment SGL type counters */
  569. if (io_req->sge_type == QEDF_IOREQ_SLOW_SGE)
  570. qedf->slow_sge_ios++;
  571. else
  572. qedf->fast_sge_ios++;
  573. }
  574. void qedf_init_mp_task(struct qedf_ioreq *io_req,
  575. struct fcoe_task_context *task_ctx, struct fcoe_wqe *sqe)
  576. {
  577. struct qedf_mp_req *mp_req = &(io_req->mp_req);
  578. struct qedf_rport *fcport = io_req->fcport;
  579. struct qedf_ctx *qedf = io_req->fcport->qedf;
  580. struct fc_frame_header *fc_hdr;
  581. struct fcoe_tx_mid_path_params task_fc_hdr;
  582. struct scsi_sgl_task_params tx_sgl_task_params;
  583. struct scsi_sgl_task_params rx_sgl_task_params;
  584. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  585. "Initializing MP task for cmd_type=%d\n",
  586. io_req->cmd_type);
  587. qedf->control_requests++;
  588. memset(&tx_sgl_task_params, 0, sizeof(struct scsi_sgl_task_params));
  589. memset(&rx_sgl_task_params, 0, sizeof(struct scsi_sgl_task_params));
  590. memset(task_ctx, 0, sizeof(struct fcoe_task_context));
  591. memset(&task_fc_hdr, 0, sizeof(struct fcoe_tx_mid_path_params));
  592. /* Setup the task from io_req for easy reference */
  593. io_req->task = task_ctx;
  594. /* Setup the fields for fcoe_task_params */
  595. io_req->task_params->context = task_ctx;
  596. io_req->task_params->sqe = sqe;
  597. io_req->task_params->task_type = FCOE_TASK_TYPE_MIDPATH;
  598. io_req->task_params->tx_io_size = io_req->data_xfer_len;
  599. /* rx_io_size tells the f/w how large a response buffer we have */
  600. io_req->task_params->rx_io_size = PAGE_SIZE;
  601. io_req->task_params->conn_cid = fcport->fw_cid;
  602. io_req->task_params->itid = io_req->xid;
  603. /* Return middle path commands on CQ 0 */
  604. io_req->task_params->cq_rss_number = 0;
  605. io_req->task_params->is_tape_device = fcport->dev_type;
  606. fc_hdr = &(mp_req->req_fc_hdr);
  607. /* Set OX_ID and RX_ID based on driver task id */
  608. fc_hdr->fh_ox_id = io_req->xid;
  609. fc_hdr->fh_rx_id = htons(0xffff);
  610. /* Set up FC header information */
  611. task_fc_hdr.parameter = fc_hdr->fh_parm_offset;
  612. task_fc_hdr.r_ctl = fc_hdr->fh_r_ctl;
  613. task_fc_hdr.type = fc_hdr->fh_type;
  614. task_fc_hdr.cs_ctl = fc_hdr->fh_cs_ctl;
  615. task_fc_hdr.df_ctl = fc_hdr->fh_df_ctl;
  616. task_fc_hdr.rx_id = fc_hdr->fh_rx_id;
  617. task_fc_hdr.ox_id = fc_hdr->fh_ox_id;
  618. /* Set up s/g list parameters for request buffer */
  619. tx_sgl_task_params.sgl = mp_req->mp_req_bd;
  620. tx_sgl_task_params.sgl_phys_addr.lo = U64_LO(mp_req->mp_req_bd_dma);
  621. tx_sgl_task_params.sgl_phys_addr.hi = U64_HI(mp_req->mp_req_bd_dma);
  622. tx_sgl_task_params.num_sges = 1;
  623. /* Set PAGE_SIZE for now since sg element is that size ??? */
  624. tx_sgl_task_params.total_buffer_size = io_req->data_xfer_len;
  625. tx_sgl_task_params.small_mid_sge = 0;
  626. /* Set up s/g list parameters for request buffer */
  627. rx_sgl_task_params.sgl = mp_req->mp_resp_bd;
  628. rx_sgl_task_params.sgl_phys_addr.lo = U64_LO(mp_req->mp_resp_bd_dma);
  629. rx_sgl_task_params.sgl_phys_addr.hi = U64_HI(mp_req->mp_resp_bd_dma);
  630. rx_sgl_task_params.num_sges = 1;
  631. /* Set PAGE_SIZE for now since sg element is that size ??? */
  632. rx_sgl_task_params.total_buffer_size = PAGE_SIZE;
  633. rx_sgl_task_params.small_mid_sge = 0;
  634. /*
  635. * Last arg is 0 as previous code did not set that we wanted the
  636. * fc header information.
  637. */
  638. init_initiator_midpath_unsolicited_fcoe_task(io_req->task_params,
  639. &task_fc_hdr,
  640. &tx_sgl_task_params,
  641. &rx_sgl_task_params, 0);
  642. }
  643. /* Presumed that fcport->rport_lock is held */
  644. u16 qedf_get_sqe_idx(struct qedf_rport *fcport)
  645. {
  646. uint16_t total_sqe = (fcport->sq_mem_size)/(sizeof(struct fcoe_wqe));
  647. u16 rval;
  648. rval = fcport->sq_prod_idx;
  649. /* Adjust ring index */
  650. fcport->sq_prod_idx++;
  651. fcport->fw_sq_prod_idx++;
  652. if (fcport->sq_prod_idx == total_sqe)
  653. fcport->sq_prod_idx = 0;
  654. return rval;
  655. }
  656. void qedf_ring_doorbell(struct qedf_rport *fcport)
  657. {
  658. struct fcoe_db_data dbell = { 0 };
  659. dbell.agg_flags = 0;
  660. dbell.params |= DB_DEST_XCM << FCOE_DB_DATA_DEST_SHIFT;
  661. dbell.params |= DB_AGG_CMD_SET << FCOE_DB_DATA_AGG_CMD_SHIFT;
  662. dbell.params |= DQ_XCM_FCOE_SQ_PROD_CMD <<
  663. FCOE_DB_DATA_AGG_VAL_SEL_SHIFT;
  664. dbell.sq_prod = fcport->fw_sq_prod_idx;
  665. /* wmb makes sure that the BDs data is updated before updating the
  666. * producer, otherwise FW may read old data from the BDs.
  667. */
  668. wmb();
  669. barrier();
  670. writel(*(u32 *)&dbell, fcport->p_doorbell);
  671. /*
  672. * Fence required to flush the write combined buffer, since another
  673. * CPU may write to the same doorbell address and data may be lost
  674. * due to relaxed order nature of write combined bar.
  675. */
  676. wmb();
  677. }
  678. static void qedf_trace_io(struct qedf_rport *fcport, struct qedf_ioreq *io_req,
  679. int8_t direction)
  680. {
  681. struct qedf_ctx *qedf = fcport->qedf;
  682. struct qedf_io_log *io_log;
  683. struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
  684. unsigned long flags;
  685. spin_lock_irqsave(&qedf->io_trace_lock, flags);
  686. io_log = &qedf->io_trace_buf[qedf->io_trace_idx];
  687. io_log->direction = direction;
  688. io_log->task_id = io_req->xid;
  689. io_log->port_id = fcport->rdata->ids.port_id;
  690. io_log->lun = sc_cmd->device->lun;
  691. io_log->op = sc_cmd->cmnd[0];
  692. io_log->lba[0] = sc_cmd->cmnd[2];
  693. io_log->lba[1] = sc_cmd->cmnd[3];
  694. io_log->lba[2] = sc_cmd->cmnd[4];
  695. io_log->lba[3] = sc_cmd->cmnd[5];
  696. io_log->bufflen = scsi_bufflen(sc_cmd);
  697. io_log->sg_count = scsi_sg_count(sc_cmd);
  698. io_log->result = sc_cmd->result;
  699. io_log->jiffies = jiffies;
  700. io_log->refcount = kref_read(&io_req->refcount);
  701. if (direction == QEDF_IO_TRACE_REQ) {
  702. /* For requests we only care abot the submission CPU */
  703. io_log->req_cpu = io_req->cpu;
  704. io_log->int_cpu = 0;
  705. io_log->rsp_cpu = 0;
  706. } else if (direction == QEDF_IO_TRACE_RSP) {
  707. io_log->req_cpu = io_req->cpu;
  708. io_log->int_cpu = io_req->int_cpu;
  709. io_log->rsp_cpu = smp_processor_id();
  710. }
  711. io_log->sge_type = io_req->sge_type;
  712. qedf->io_trace_idx++;
  713. if (qedf->io_trace_idx == QEDF_IO_TRACE_SIZE)
  714. qedf->io_trace_idx = 0;
  715. spin_unlock_irqrestore(&qedf->io_trace_lock, flags);
  716. }
  717. int qedf_post_io_req(struct qedf_rport *fcport, struct qedf_ioreq *io_req)
  718. {
  719. struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
  720. struct Scsi_Host *host = sc_cmd->device->host;
  721. struct fc_lport *lport = shost_priv(host);
  722. struct qedf_ctx *qedf = lport_priv(lport);
  723. struct fcoe_task_context *task_ctx;
  724. u16 xid;
  725. struct fcoe_wqe *sqe;
  726. u16 sqe_idx;
  727. /* Initialize rest of io_req fileds */
  728. io_req->data_xfer_len = scsi_bufflen(sc_cmd);
  729. qedf_priv(sc_cmd)->io_req = io_req;
  730. io_req->sge_type = QEDF_IOREQ_FAST_SGE; /* Assume fast SGL by default */
  731. /* Record which cpu this request is associated with */
  732. io_req->cpu = smp_processor_id();
  733. if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
  734. io_req->io_req_flags = QEDF_READ;
  735. qedf->input_requests++;
  736. } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
  737. io_req->io_req_flags = QEDF_WRITE;
  738. qedf->output_requests++;
  739. } else {
  740. io_req->io_req_flags = 0;
  741. qedf->control_requests++;
  742. }
  743. xid = io_req->xid;
  744. /* Build buffer descriptor list for firmware from sg list */
  745. if (qedf_build_bd_list_from_sg(io_req)) {
  746. QEDF_ERR(&(qedf->dbg_ctx), "BD list creation failed.\n");
  747. /* Release cmd will release io_req, but sc_cmd is assigned */
  748. io_req->sc_cmd = NULL;
  749. kref_put(&io_req->refcount, qedf_release_cmd);
  750. return -EAGAIN;
  751. }
  752. if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags) ||
  753. test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
  754. QEDF_ERR(&(qedf->dbg_ctx), "Session not offloaded yet.\n");
  755. /* Release cmd will release io_req, but sc_cmd is assigned */
  756. io_req->sc_cmd = NULL;
  757. kref_put(&io_req->refcount, qedf_release_cmd);
  758. return -EINVAL;
  759. }
  760. /* Record LUN number for later use if we need them */
  761. io_req->lun = (int)sc_cmd->device->lun;
  762. /* Obtain free SQE */
  763. sqe_idx = qedf_get_sqe_idx(fcport);
  764. sqe = &fcport->sq[sqe_idx];
  765. memset(sqe, 0, sizeof(struct fcoe_wqe));
  766. /* Get the task context */
  767. task_ctx = qedf_get_task_mem(&qedf->tasks, xid);
  768. if (!task_ctx) {
  769. QEDF_WARN(&(qedf->dbg_ctx), "task_ctx is NULL, xid=%d.\n",
  770. xid);
  771. /* Release cmd will release io_req, but sc_cmd is assigned */
  772. io_req->sc_cmd = NULL;
  773. kref_put(&io_req->refcount, qedf_release_cmd);
  774. return -EINVAL;
  775. }
  776. qedf_init_task(fcport, lport, io_req, task_ctx, sqe);
  777. /* Ring doorbell */
  778. qedf_ring_doorbell(fcport);
  779. /* Set that command is with the firmware now */
  780. set_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
  781. if (qedf_io_tracing && io_req->sc_cmd)
  782. qedf_trace_io(fcport, io_req, QEDF_IO_TRACE_REQ);
  783. return false;
  784. }
  785. int
  786. qedf_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc_cmd)
  787. {
  788. struct fc_lport *lport = shost_priv(host);
  789. struct qedf_ctx *qedf = lport_priv(lport);
  790. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  791. struct fc_rport_libfc_priv *rp = rport->dd_data;
  792. struct qedf_rport *fcport;
  793. struct qedf_ioreq *io_req;
  794. int rc = 0;
  795. int rval;
  796. unsigned long flags = 0;
  797. int num_sgs = 0;
  798. num_sgs = scsi_sg_count(sc_cmd);
  799. if (scsi_sg_count(sc_cmd) > QEDF_MAX_BDS_PER_CMD) {
  800. QEDF_ERR(&qedf->dbg_ctx,
  801. "Number of SG elements %d exceeds what hardware limitation of %d.\n",
  802. num_sgs, QEDF_MAX_BDS_PER_CMD);
  803. sc_cmd->result = DID_ERROR;
  804. scsi_done(sc_cmd);
  805. return 0;
  806. }
  807. if (test_bit(QEDF_UNLOADING, &qedf->flags) ||
  808. test_bit(QEDF_DBG_STOP_IO, &qedf->flags)) {
  809. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
  810. "Returning DNC as unloading or stop io, flags 0x%lx.\n",
  811. qedf->flags);
  812. sc_cmd->result = DID_NO_CONNECT << 16;
  813. scsi_done(sc_cmd);
  814. return 0;
  815. }
  816. if (!qedf->pdev->msix_enabled) {
  817. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  818. "Completing sc_cmd=%p DID_NO_CONNECT as MSI-X is not enabled.\n",
  819. sc_cmd);
  820. sc_cmd->result = DID_NO_CONNECT << 16;
  821. scsi_done(sc_cmd);
  822. return 0;
  823. }
  824. rval = fc_remote_port_chkready(rport);
  825. if (rval) {
  826. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
  827. "fc_remote_port_chkready failed=0x%x for port_id=0x%06x.\n",
  828. rval, rport->port_id);
  829. sc_cmd->result = rval;
  830. scsi_done(sc_cmd);
  831. return 0;
  832. }
  833. /* Retry command if we are doing a qed drain operation */
  834. if (test_bit(QEDF_DRAIN_ACTIVE, &qedf->flags)) {
  835. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO, "Drain active.\n");
  836. rc = SCSI_MLQUEUE_HOST_BUSY;
  837. goto exit_qcmd;
  838. }
  839. if (lport->state != LPORT_ST_READY ||
  840. atomic_read(&qedf->link_state) != QEDF_LINK_UP) {
  841. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO, "Link down.\n");
  842. rc = SCSI_MLQUEUE_HOST_BUSY;
  843. goto exit_qcmd;
  844. }
  845. /* rport and tgt are allocated together, so tgt should be non-NULL */
  846. fcport = (struct qedf_rport *)&rp[1];
  847. if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags) ||
  848. test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
  849. /*
  850. * Session is not offloaded yet. Let SCSI-ml retry
  851. * the command.
  852. */
  853. rc = SCSI_MLQUEUE_TARGET_BUSY;
  854. goto exit_qcmd;
  855. }
  856. atomic_inc(&fcport->ios_to_queue);
  857. if (fcport->retry_delay_timestamp) {
  858. /* Take fcport->rport_lock for resetting the delay_timestamp */
  859. spin_lock_irqsave(&fcport->rport_lock, flags);
  860. if (time_after(jiffies, fcport->retry_delay_timestamp)) {
  861. fcport->retry_delay_timestamp = 0;
  862. } else {
  863. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  864. /* If retry_delay timer is active, flow off the ML */
  865. rc = SCSI_MLQUEUE_TARGET_BUSY;
  866. atomic_dec(&fcport->ios_to_queue);
  867. goto exit_qcmd;
  868. }
  869. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  870. }
  871. io_req = qedf_alloc_cmd(fcport, QEDF_SCSI_CMD);
  872. if (!io_req) {
  873. rc = SCSI_MLQUEUE_HOST_BUSY;
  874. atomic_dec(&fcport->ios_to_queue);
  875. goto exit_qcmd;
  876. }
  877. io_req->sc_cmd = sc_cmd;
  878. /* Take fcport->rport_lock for posting to fcport send queue */
  879. spin_lock_irqsave(&fcport->rport_lock, flags);
  880. if (qedf_post_io_req(fcport, io_req)) {
  881. QEDF_WARN(&(qedf->dbg_ctx), "Unable to post io_req\n");
  882. /* Return SQE to pool */
  883. atomic_inc(&fcport->free_sqes);
  884. rc = SCSI_MLQUEUE_HOST_BUSY;
  885. }
  886. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  887. atomic_dec(&fcport->ios_to_queue);
  888. exit_qcmd:
  889. return rc;
  890. }
  891. static void qedf_parse_fcp_rsp(struct qedf_ioreq *io_req,
  892. struct fcoe_cqe_rsp_info *fcp_rsp)
  893. {
  894. struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
  895. struct qedf_ctx *qedf = io_req->fcport->qedf;
  896. u8 rsp_flags = fcp_rsp->rsp_flags.flags;
  897. int fcp_sns_len = 0;
  898. int fcp_rsp_len = 0;
  899. uint8_t *rsp_info, *sense_data;
  900. io_req->fcp_status = FC_GOOD;
  901. io_req->fcp_resid = 0;
  902. if (rsp_flags & (FCOE_FCP_RSP_FLAGS_FCP_RESID_OVER |
  903. FCOE_FCP_RSP_FLAGS_FCP_RESID_UNDER))
  904. io_req->fcp_resid = fcp_rsp->fcp_resid;
  905. io_req->scsi_comp_flags = rsp_flags;
  906. io_req->cdb_status = fcp_rsp->scsi_status_code;
  907. if (rsp_flags &
  908. FCOE_FCP_RSP_FLAGS_FCP_RSP_LEN_VALID)
  909. fcp_rsp_len = fcp_rsp->fcp_rsp_len;
  910. if (rsp_flags &
  911. FCOE_FCP_RSP_FLAGS_FCP_SNS_LEN_VALID)
  912. fcp_sns_len = fcp_rsp->fcp_sns_len;
  913. io_req->fcp_rsp_len = fcp_rsp_len;
  914. io_req->fcp_sns_len = fcp_sns_len;
  915. rsp_info = sense_data = io_req->sense_buffer;
  916. /* fetch fcp_rsp_code */
  917. if ((fcp_rsp_len == 4) || (fcp_rsp_len == 8)) {
  918. /* Only for task management function */
  919. io_req->fcp_rsp_code = rsp_info[3];
  920. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  921. "fcp_rsp_code = %d\n", io_req->fcp_rsp_code);
  922. /* Adjust sense-data location. */
  923. sense_data += fcp_rsp_len;
  924. }
  925. if (fcp_sns_len > SCSI_SENSE_BUFFERSIZE) {
  926. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  927. "Truncating sense buffer\n");
  928. fcp_sns_len = SCSI_SENSE_BUFFERSIZE;
  929. }
  930. /* The sense buffer can be NULL for TMF commands */
  931. if (sc_cmd->sense_buffer) {
  932. memset(sc_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  933. if (fcp_sns_len)
  934. memcpy(sc_cmd->sense_buffer, sense_data,
  935. fcp_sns_len);
  936. }
  937. }
  938. static void qedf_unmap_sg_list(struct qedf_ctx *qedf, struct qedf_ioreq *io_req)
  939. {
  940. struct scsi_cmnd *sc = io_req->sc_cmd;
  941. if (io_req->bd_tbl->bd_valid && sc && scsi_sg_count(sc)) {
  942. dma_unmap_sg(&qedf->pdev->dev, scsi_sglist(sc),
  943. scsi_sg_count(sc), sc->sc_data_direction);
  944. io_req->bd_tbl->bd_valid = 0;
  945. }
  946. }
  947. void qedf_scsi_completion(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
  948. struct qedf_ioreq *io_req)
  949. {
  950. struct scsi_cmnd *sc_cmd;
  951. struct fcoe_cqe_rsp_info *fcp_rsp;
  952. struct qedf_rport *fcport;
  953. int refcount;
  954. u16 scope, qualifier = 0;
  955. u8 fw_residual_flag = 0;
  956. unsigned long flags = 0;
  957. u16 chk_scope = 0;
  958. if (!io_req)
  959. return;
  960. if (!cqe)
  961. return;
  962. if (!test_bit(QEDF_CMD_OUTSTANDING, &io_req->flags) ||
  963. test_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags) ||
  964. test_bit(QEDF_CMD_IN_ABORT, &io_req->flags)) {
  965. QEDF_ERR(&qedf->dbg_ctx,
  966. "io_req xid=0x%x already in cleanup or abort processing or already completed.\n",
  967. io_req->xid);
  968. return;
  969. }
  970. sc_cmd = io_req->sc_cmd;
  971. fcp_rsp = &cqe->cqe_info.rsp_info;
  972. if (!sc_cmd) {
  973. QEDF_WARN(&(qedf->dbg_ctx), "sc_cmd is NULL!\n");
  974. return;
  975. }
  976. if (!qedf_priv(sc_cmd)->io_req) {
  977. QEDF_WARN(&(qedf->dbg_ctx),
  978. "io_req is NULL, returned in another context.\n");
  979. return;
  980. }
  981. if (!sc_cmd->device) {
  982. QEDF_ERR(&qedf->dbg_ctx,
  983. "Device for sc_cmd %p is NULL.\n", sc_cmd);
  984. return;
  985. }
  986. if (!scsi_cmd_to_rq(sc_cmd)->q) {
  987. QEDF_WARN(&(qedf->dbg_ctx), "request->q is NULL so request "
  988. "is not valid, sc_cmd=%p.\n", sc_cmd);
  989. return;
  990. }
  991. fcport = io_req->fcport;
  992. /*
  993. * When flush is active, let the cmds be completed from the cleanup
  994. * context
  995. */
  996. if (test_bit(QEDF_RPORT_IN_TARGET_RESET, &fcport->flags) ||
  997. (test_bit(QEDF_RPORT_IN_LUN_RESET, &fcport->flags) &&
  998. sc_cmd->device->lun == (u64)fcport->lun_reset_lun)) {
  999. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
  1000. "Dropping good completion xid=0x%x as fcport is flushing",
  1001. io_req->xid);
  1002. return;
  1003. }
  1004. qedf_parse_fcp_rsp(io_req, fcp_rsp);
  1005. qedf_unmap_sg_list(qedf, io_req);
  1006. /* Check for FCP transport error */
  1007. if (io_req->fcp_rsp_len > 3 && io_req->fcp_rsp_code) {
  1008. QEDF_ERR(&(qedf->dbg_ctx),
  1009. "FCP I/O protocol failure xid=0x%x fcp_rsp_len=%d "
  1010. "fcp_rsp_code=%d.\n", io_req->xid, io_req->fcp_rsp_len,
  1011. io_req->fcp_rsp_code);
  1012. sc_cmd->result = DID_BUS_BUSY << 16;
  1013. goto out;
  1014. }
  1015. fw_residual_flag = GET_FIELD(cqe->cqe_info.rsp_info.fw_error_flags,
  1016. FCOE_CQE_RSP_INFO_FW_UNDERRUN);
  1017. if (fw_residual_flag) {
  1018. QEDF_ERR(&qedf->dbg_ctx,
  1019. "Firmware detected underrun: xid=0x%x fcp_rsp.flags=0x%02x fcp_resid=%d fw_residual=0x%x lba=%02x%02x%02x%02x.\n",
  1020. io_req->xid, fcp_rsp->rsp_flags.flags,
  1021. io_req->fcp_resid,
  1022. cqe->cqe_info.rsp_info.fw_residual, sc_cmd->cmnd[2],
  1023. sc_cmd->cmnd[3], sc_cmd->cmnd[4], sc_cmd->cmnd[5]);
  1024. if (io_req->cdb_status == 0)
  1025. sc_cmd->result = (DID_ERROR << 16) | io_req->cdb_status;
  1026. else
  1027. sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
  1028. /*
  1029. * Set resid to the whole buffer length so we won't try to resue
  1030. * any previously data.
  1031. */
  1032. scsi_set_resid(sc_cmd, scsi_bufflen(sc_cmd));
  1033. goto out;
  1034. }
  1035. switch (io_req->fcp_status) {
  1036. case FC_GOOD:
  1037. if (io_req->cdb_status == 0) {
  1038. /* Good I/O completion */
  1039. sc_cmd->result = DID_OK << 16;
  1040. } else {
  1041. refcount = kref_read(&io_req->refcount);
  1042. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  1043. "%d:0:%d:%lld xid=0x%0x op=0x%02x "
  1044. "lba=%02x%02x%02x%02x cdb_status=%d "
  1045. "fcp_resid=0x%x refcount=%d.\n",
  1046. qedf->lport->host->host_no, sc_cmd->device->id,
  1047. sc_cmd->device->lun, io_req->xid,
  1048. sc_cmd->cmnd[0], sc_cmd->cmnd[2], sc_cmd->cmnd[3],
  1049. sc_cmd->cmnd[4], sc_cmd->cmnd[5],
  1050. io_req->cdb_status, io_req->fcp_resid,
  1051. refcount);
  1052. sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
  1053. if (io_req->cdb_status == SAM_STAT_TASK_SET_FULL ||
  1054. io_req->cdb_status == SAM_STAT_BUSY) {
  1055. /*
  1056. * Check whether we need to set retry_delay at
  1057. * all based on retry_delay module parameter
  1058. * and the status qualifier.
  1059. */
  1060. /* Upper 2 bits */
  1061. scope = fcp_rsp->retry_delay_timer & 0xC000;
  1062. /* Lower 14 bits */
  1063. qualifier = fcp_rsp->retry_delay_timer & 0x3FFF;
  1064. if (qedf_retry_delay)
  1065. chk_scope = 1;
  1066. /* Record stats */
  1067. if (io_req->cdb_status ==
  1068. SAM_STAT_TASK_SET_FULL)
  1069. qedf->task_set_fulls++;
  1070. else
  1071. qedf->busy++;
  1072. }
  1073. }
  1074. if (io_req->fcp_resid)
  1075. scsi_set_resid(sc_cmd, io_req->fcp_resid);
  1076. if (chk_scope == 1) {
  1077. if ((scope == 1 || scope == 2) &&
  1078. (qualifier > 0 && qualifier <= 0x3FEF)) {
  1079. /* Check we don't go over the max */
  1080. if (qualifier > QEDF_RETRY_DELAY_MAX) {
  1081. qualifier = QEDF_RETRY_DELAY_MAX;
  1082. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
  1083. "qualifier = %d\n",
  1084. (fcp_rsp->retry_delay_timer &
  1085. 0x3FFF));
  1086. }
  1087. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
  1088. "Scope = %d and qualifier = %d",
  1089. scope, qualifier);
  1090. /* Take fcport->rport_lock to
  1091. * update the retry_delay_timestamp
  1092. */
  1093. spin_lock_irqsave(&fcport->rport_lock, flags);
  1094. fcport->retry_delay_timestamp =
  1095. jiffies + (qualifier * HZ / 10);
  1096. spin_unlock_irqrestore(&fcport->rport_lock,
  1097. flags);
  1098. } else {
  1099. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
  1100. "combination of scope = %d and qualifier = %d is not handled in qedf.\n",
  1101. scope, qualifier);
  1102. }
  1103. }
  1104. break;
  1105. default:
  1106. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO, "fcp_status=%d.\n",
  1107. io_req->fcp_status);
  1108. break;
  1109. }
  1110. out:
  1111. if (qedf_io_tracing)
  1112. qedf_trace_io(fcport, io_req, QEDF_IO_TRACE_RSP);
  1113. /*
  1114. * We wait till the end of the function to clear the
  1115. * outstanding bit in case we need to send an abort
  1116. */
  1117. clear_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
  1118. io_req->sc_cmd = NULL;
  1119. qedf_priv(sc_cmd)->io_req = NULL;
  1120. scsi_done(sc_cmd);
  1121. kref_put(&io_req->refcount, qedf_release_cmd);
  1122. }
  1123. /* Return a SCSI command in some other context besides a normal completion */
  1124. void qedf_scsi_done(struct qedf_ctx *qedf, struct qedf_ioreq *io_req,
  1125. int result)
  1126. {
  1127. struct scsi_cmnd *sc_cmd;
  1128. int refcount;
  1129. if (!io_req) {
  1130. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO, "io_req is NULL\n");
  1131. return;
  1132. }
  1133. if (test_and_set_bit(QEDF_CMD_ERR_SCSI_DONE, &io_req->flags)) {
  1134. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
  1135. "io_req:%p scsi_done handling already done\n",
  1136. io_req);
  1137. return;
  1138. }
  1139. /*
  1140. * We will be done with this command after this call so clear the
  1141. * outstanding bit.
  1142. */
  1143. clear_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
  1144. sc_cmd = io_req->sc_cmd;
  1145. if (!sc_cmd) {
  1146. QEDF_WARN(&(qedf->dbg_ctx), "sc_cmd is NULL!\n");
  1147. return;
  1148. }
  1149. if (!virt_addr_valid(sc_cmd)) {
  1150. QEDF_ERR(&qedf->dbg_ctx, "sc_cmd=%p is not valid.", sc_cmd);
  1151. goto bad_scsi_ptr;
  1152. }
  1153. if (!qedf_priv(sc_cmd)->io_req) {
  1154. QEDF_WARN(&(qedf->dbg_ctx),
  1155. "io_req is NULL, returned in another context.\n");
  1156. return;
  1157. }
  1158. if (!sc_cmd->device) {
  1159. QEDF_ERR(&qedf->dbg_ctx, "Device for sc_cmd %p is NULL.\n",
  1160. sc_cmd);
  1161. goto bad_scsi_ptr;
  1162. }
  1163. if (!virt_addr_valid(sc_cmd->device)) {
  1164. QEDF_ERR(&qedf->dbg_ctx,
  1165. "Device pointer for sc_cmd %p is bad.\n", sc_cmd);
  1166. goto bad_scsi_ptr;
  1167. }
  1168. if (!sc_cmd->sense_buffer) {
  1169. QEDF_ERR(&qedf->dbg_ctx,
  1170. "sc_cmd->sense_buffer for sc_cmd %p is NULL.\n",
  1171. sc_cmd);
  1172. goto bad_scsi_ptr;
  1173. }
  1174. if (!virt_addr_valid(sc_cmd->sense_buffer)) {
  1175. QEDF_ERR(&qedf->dbg_ctx,
  1176. "sc_cmd->sense_buffer for sc_cmd %p is bad.\n",
  1177. sc_cmd);
  1178. goto bad_scsi_ptr;
  1179. }
  1180. qedf_unmap_sg_list(qedf, io_req);
  1181. sc_cmd->result = result << 16;
  1182. refcount = kref_read(&io_req->refcount);
  1183. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO, "%d:0:%d:%lld: Completing "
  1184. "sc_cmd=%p result=0x%08x op=0x%02x lba=0x%02x%02x%02x%02x, "
  1185. "allowed=%d retries=%d refcount=%d.\n",
  1186. qedf->lport->host->host_no, sc_cmd->device->id,
  1187. sc_cmd->device->lun, sc_cmd, sc_cmd->result, sc_cmd->cmnd[0],
  1188. sc_cmd->cmnd[2], sc_cmd->cmnd[3], sc_cmd->cmnd[4],
  1189. sc_cmd->cmnd[5], sc_cmd->allowed, sc_cmd->retries,
  1190. refcount);
  1191. /*
  1192. * Set resid to the whole buffer length so we won't try to resue any
  1193. * previously read data
  1194. */
  1195. scsi_set_resid(sc_cmd, scsi_bufflen(sc_cmd));
  1196. if (qedf_io_tracing)
  1197. qedf_trace_io(io_req->fcport, io_req, QEDF_IO_TRACE_RSP);
  1198. io_req->sc_cmd = NULL;
  1199. qedf_priv(sc_cmd)->io_req = NULL;
  1200. scsi_done(sc_cmd);
  1201. kref_put(&io_req->refcount, qedf_release_cmd);
  1202. return;
  1203. bad_scsi_ptr:
  1204. /*
  1205. * Clear the io_req->sc_cmd backpointer so we don't try to process
  1206. * this again
  1207. */
  1208. io_req->sc_cmd = NULL;
  1209. kref_put(&io_req->refcount, qedf_release_cmd); /* ID: 001 */
  1210. }
  1211. /*
  1212. * Handle warning type CQE completions. This is mainly used for REC timer
  1213. * popping.
  1214. */
  1215. void qedf_process_warning_compl(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
  1216. struct qedf_ioreq *io_req)
  1217. {
  1218. int rval, i;
  1219. struct qedf_rport *fcport = io_req->fcport;
  1220. u64 err_warn_bit_map;
  1221. u8 err_warn = 0xff;
  1222. if (!cqe) {
  1223. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
  1224. "cqe is NULL for io_req %p xid=0x%x\n",
  1225. io_req, io_req->xid);
  1226. return;
  1227. }
  1228. QEDF_ERR(&(io_req->fcport->qedf->dbg_ctx), "Warning CQE, "
  1229. "xid=0x%x\n", io_req->xid);
  1230. QEDF_ERR(&(io_req->fcport->qedf->dbg_ctx),
  1231. "err_warn_bitmap=%08x:%08x\n",
  1232. le32_to_cpu(cqe->cqe_info.err_info.err_warn_bitmap_hi),
  1233. le32_to_cpu(cqe->cqe_info.err_info.err_warn_bitmap_lo));
  1234. QEDF_ERR(&(io_req->fcport->qedf->dbg_ctx), "tx_buff_off=%08x, "
  1235. "rx_buff_off=%08x, rx_id=%04x\n",
  1236. le32_to_cpu(cqe->cqe_info.err_info.tx_buf_off),
  1237. le32_to_cpu(cqe->cqe_info.err_info.rx_buf_off),
  1238. le32_to_cpu(cqe->cqe_info.err_info.rx_id));
  1239. /* Normalize the error bitmap value to an just an unsigned int */
  1240. err_warn_bit_map = (u64)
  1241. ((u64)cqe->cqe_info.err_info.err_warn_bitmap_hi << 32) |
  1242. (u64)cqe->cqe_info.err_info.err_warn_bitmap_lo;
  1243. for (i = 0; i < 64; i++) {
  1244. if (err_warn_bit_map & (u64)((u64)1 << i)) {
  1245. err_warn = i;
  1246. break;
  1247. }
  1248. }
  1249. /* Check if REC TOV expired if this is a tape device */
  1250. if (fcport->dev_type == QEDF_RPORT_TYPE_TAPE) {
  1251. if (err_warn ==
  1252. FCOE_WARNING_CODE_REC_TOV_TIMER_EXPIRATION) {
  1253. QEDF_ERR(&(qedf->dbg_ctx), "REC timer expired.\n");
  1254. if (!test_bit(QEDF_CMD_SRR_SENT, &io_req->flags)) {
  1255. io_req->rx_buf_off =
  1256. cqe->cqe_info.err_info.rx_buf_off;
  1257. io_req->tx_buf_off =
  1258. cqe->cqe_info.err_info.tx_buf_off;
  1259. io_req->rx_id = cqe->cqe_info.err_info.rx_id;
  1260. rval = qedf_send_rec(io_req);
  1261. /*
  1262. * We only want to abort the io_req if we
  1263. * can't queue the REC command as we want to
  1264. * keep the exchange open for recovery.
  1265. */
  1266. if (rval)
  1267. goto send_abort;
  1268. }
  1269. return;
  1270. }
  1271. }
  1272. send_abort:
  1273. init_completion(&io_req->abts_done);
  1274. rval = qedf_initiate_abts(io_req, true);
  1275. if (rval)
  1276. QEDF_ERR(&(qedf->dbg_ctx), "Failed to queue ABTS.\n");
  1277. }
  1278. /* Cleanup a command when we receive an error detection completion */
  1279. void qedf_process_error_detect(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
  1280. struct qedf_ioreq *io_req)
  1281. {
  1282. int rval;
  1283. if (io_req == NULL) {
  1284. QEDF_INFO(NULL, QEDF_LOG_IO, "io_req is NULL.\n");
  1285. return;
  1286. }
  1287. if (io_req->fcport == NULL) {
  1288. QEDF_INFO(NULL, QEDF_LOG_IO, "fcport is NULL.\n");
  1289. return;
  1290. }
  1291. if (!cqe) {
  1292. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
  1293. "cqe is NULL for io_req %p\n", io_req);
  1294. return;
  1295. }
  1296. QEDF_ERR(&(io_req->fcport->qedf->dbg_ctx), "Error detection CQE, "
  1297. "xid=0x%x\n", io_req->xid);
  1298. QEDF_ERR(&(io_req->fcport->qedf->dbg_ctx),
  1299. "err_warn_bitmap=%08x:%08x\n",
  1300. le32_to_cpu(cqe->cqe_info.err_info.err_warn_bitmap_hi),
  1301. le32_to_cpu(cqe->cqe_info.err_info.err_warn_bitmap_lo));
  1302. QEDF_ERR(&(io_req->fcport->qedf->dbg_ctx), "tx_buff_off=%08x, "
  1303. "rx_buff_off=%08x, rx_id=%04x\n",
  1304. le32_to_cpu(cqe->cqe_info.err_info.tx_buf_off),
  1305. le32_to_cpu(cqe->cqe_info.err_info.rx_buf_off),
  1306. le32_to_cpu(cqe->cqe_info.err_info.rx_id));
  1307. /* When flush is active, let the cmds be flushed out from the cleanup context */
  1308. if (test_bit(QEDF_RPORT_IN_TARGET_RESET, &io_req->fcport->flags) ||
  1309. (test_bit(QEDF_RPORT_IN_LUN_RESET, &io_req->fcport->flags) &&
  1310. io_req->sc_cmd->device->lun == (u64)io_req->fcport->lun_reset_lun)) {
  1311. QEDF_ERR(&qedf->dbg_ctx,
  1312. "Dropping EQE for xid=0x%x as fcport is flushing",
  1313. io_req->xid);
  1314. return;
  1315. }
  1316. if (qedf->stop_io_on_error) {
  1317. qedf_stop_all_io(qedf);
  1318. return;
  1319. }
  1320. init_completion(&io_req->abts_done);
  1321. rval = qedf_initiate_abts(io_req, true);
  1322. if (rval)
  1323. QEDF_ERR(&(qedf->dbg_ctx), "Failed to queue ABTS.\n");
  1324. }
  1325. static void qedf_flush_els_req(struct qedf_ctx *qedf,
  1326. struct qedf_ioreq *els_req)
  1327. {
  1328. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  1329. "Flushing ELS request xid=0x%x refcount=%d.\n", els_req->xid,
  1330. kref_read(&els_req->refcount));
  1331. /*
  1332. * Need to distinguish this from a timeout when calling the
  1333. * els_req->cb_func.
  1334. */
  1335. els_req->event = QEDF_IOREQ_EV_ELS_FLUSH;
  1336. clear_bit(QEDF_CMD_OUTSTANDING, &els_req->flags);
  1337. /* Cancel the timer */
  1338. cancel_delayed_work_sync(&els_req->timeout_work);
  1339. /* Call callback function to complete command */
  1340. if (els_req->cb_func && els_req->cb_arg) {
  1341. els_req->cb_func(els_req->cb_arg);
  1342. els_req->cb_arg = NULL;
  1343. }
  1344. /* Release kref for original initiate_els */
  1345. kref_put(&els_req->refcount, qedf_release_cmd);
  1346. }
  1347. /* A value of -1 for lun is a wild card that means flush all
  1348. * active SCSI I/Os for the target.
  1349. */
  1350. void qedf_flush_active_ios(struct qedf_rport *fcport, int lun)
  1351. {
  1352. struct qedf_ioreq *io_req;
  1353. struct qedf_ctx *qedf;
  1354. struct qedf_cmd_mgr *cmd_mgr;
  1355. int i, rc;
  1356. unsigned long flags;
  1357. int flush_cnt = 0;
  1358. int wait_cnt = 100;
  1359. int refcount = 0;
  1360. if (!fcport) {
  1361. QEDF_ERR(NULL, "fcport is NULL\n");
  1362. return;
  1363. }
  1364. /* Check that fcport is still offloaded */
  1365. if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
  1366. QEDF_ERR(NULL, "fcport is no longer offloaded.\n");
  1367. return;
  1368. }
  1369. qedf = fcport->qedf;
  1370. if (!qedf) {
  1371. QEDF_ERR(NULL, "qedf is NULL.\n");
  1372. return;
  1373. }
  1374. /* Only wait for all commands to be queued in the Upload context */
  1375. if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags) &&
  1376. (lun == -1)) {
  1377. while (atomic_read(&fcport->ios_to_queue)) {
  1378. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
  1379. "Waiting for %d I/Os to be queued\n",
  1380. atomic_read(&fcport->ios_to_queue));
  1381. if (wait_cnt == 0) {
  1382. QEDF_ERR(NULL,
  1383. "%d IOs request could not be queued\n",
  1384. atomic_read(&fcport->ios_to_queue));
  1385. }
  1386. msleep(20);
  1387. wait_cnt--;
  1388. }
  1389. }
  1390. cmd_mgr = qedf->cmd_mgr;
  1391. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
  1392. "Flush active i/o's num=0x%x fcport=0x%p port_id=0x%06x scsi_id=%d.\n",
  1393. atomic_read(&fcport->num_active_ios), fcport,
  1394. fcport->rdata->ids.port_id, fcport->rport->scsi_target_id);
  1395. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO, "Locking flush mutex.\n");
  1396. mutex_lock(&qedf->flush_mutex);
  1397. if (lun == -1) {
  1398. set_bit(QEDF_RPORT_IN_TARGET_RESET, &fcport->flags);
  1399. } else {
  1400. set_bit(QEDF_RPORT_IN_LUN_RESET, &fcport->flags);
  1401. fcport->lun_reset_lun = lun;
  1402. }
  1403. for (i = 0; i < FCOE_PARAMS_NUM_TASKS; i++) {
  1404. io_req = &cmd_mgr->cmds[i];
  1405. if (!io_req)
  1406. continue;
  1407. if (!io_req->fcport)
  1408. continue;
  1409. spin_lock_irqsave(&cmd_mgr->lock, flags);
  1410. if (io_req->alloc) {
  1411. if (!test_bit(QEDF_CMD_OUTSTANDING, &io_req->flags)) {
  1412. if (io_req->cmd_type == QEDF_SCSI_CMD)
  1413. QEDF_ERR(&qedf->dbg_ctx,
  1414. "Allocated but not queued, xid=0x%x\n",
  1415. io_req->xid);
  1416. }
  1417. spin_unlock_irqrestore(&cmd_mgr->lock, flags);
  1418. } else {
  1419. spin_unlock_irqrestore(&cmd_mgr->lock, flags);
  1420. continue;
  1421. }
  1422. if (io_req->fcport != fcport)
  1423. continue;
  1424. /* In case of ABTS, CMD_OUTSTANDING is cleared on ABTS response,
  1425. * but RRQ is still pending.
  1426. * Workaround: Within qedf_send_rrq, we check if the fcport is
  1427. * NULL, and we drop the ref on the io_req to clean it up.
  1428. */
  1429. if (!test_bit(QEDF_CMD_OUTSTANDING, &io_req->flags)) {
  1430. refcount = kref_read(&io_req->refcount);
  1431. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
  1432. "Not outstanding, xid=0x%x, cmd_type=%d refcount=%d.\n",
  1433. io_req->xid, io_req->cmd_type, refcount);
  1434. /* If RRQ work has been queue, try to cancel it and
  1435. * free the io_req
  1436. */
  1437. if (atomic_read(&io_req->state) ==
  1438. QEDFC_CMD_ST_RRQ_WAIT) {
  1439. if (cancel_delayed_work_sync
  1440. (&io_req->rrq_work)) {
  1441. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
  1442. "Putting reference for pending RRQ work xid=0x%x.\n",
  1443. io_req->xid);
  1444. /* ID: 003 */
  1445. kref_put(&io_req->refcount,
  1446. qedf_release_cmd);
  1447. }
  1448. }
  1449. continue;
  1450. }
  1451. /* Only consider flushing ELS during target reset */
  1452. if (io_req->cmd_type == QEDF_ELS &&
  1453. lun == -1) {
  1454. rc = kref_get_unless_zero(&io_req->refcount);
  1455. if (!rc) {
  1456. QEDF_ERR(&(qedf->dbg_ctx),
  1457. "Could not get kref for ELS io_req=0x%p xid=0x%x.\n",
  1458. io_req, io_req->xid);
  1459. continue;
  1460. }
  1461. qedf_initiate_cleanup(io_req, false);
  1462. flush_cnt++;
  1463. qedf_flush_els_req(qedf, io_req);
  1464. /*
  1465. * Release the kref and go back to the top of the
  1466. * loop.
  1467. */
  1468. goto free_cmd;
  1469. }
  1470. if (io_req->cmd_type == QEDF_ABTS) {
  1471. /* ID: 004 */
  1472. rc = kref_get_unless_zero(&io_req->refcount);
  1473. if (!rc) {
  1474. QEDF_ERR(&(qedf->dbg_ctx),
  1475. "Could not get kref for abort io_req=0x%p xid=0x%x.\n",
  1476. io_req, io_req->xid);
  1477. continue;
  1478. }
  1479. if (lun != -1 && io_req->lun != lun)
  1480. goto free_cmd;
  1481. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
  1482. "Flushing abort xid=0x%x.\n", io_req->xid);
  1483. if (cancel_delayed_work_sync(&io_req->rrq_work)) {
  1484. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
  1485. "Putting ref for cancelled RRQ work xid=0x%x.\n",
  1486. io_req->xid);
  1487. kref_put(&io_req->refcount, qedf_release_cmd);
  1488. }
  1489. if (cancel_delayed_work_sync(&io_req->timeout_work)) {
  1490. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
  1491. "Putting ref for cancelled tmo work xid=0x%x.\n",
  1492. io_req->xid);
  1493. qedf_initiate_cleanup(io_req, true);
  1494. /* Notify eh_abort handler that ABTS is
  1495. * complete
  1496. */
  1497. complete(&io_req->abts_done);
  1498. clear_bit(QEDF_CMD_IN_ABORT, &io_req->flags);
  1499. /* ID: 002 */
  1500. kref_put(&io_req->refcount, qedf_release_cmd);
  1501. }
  1502. flush_cnt++;
  1503. goto free_cmd;
  1504. }
  1505. if (!io_req->sc_cmd)
  1506. continue;
  1507. if (!io_req->sc_cmd->device) {
  1508. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
  1509. "Device backpointer NULL for sc_cmd=%p.\n",
  1510. io_req->sc_cmd);
  1511. /* Put reference for non-existent scsi_cmnd */
  1512. io_req->sc_cmd = NULL;
  1513. qedf_initiate_cleanup(io_req, false);
  1514. kref_put(&io_req->refcount, qedf_release_cmd);
  1515. continue;
  1516. }
  1517. if (lun > -1) {
  1518. if (io_req->lun != lun)
  1519. continue;
  1520. }
  1521. /*
  1522. * Use kref_get_unless_zero in the unlikely case the command
  1523. * we're about to flush was completed in the normal SCSI path
  1524. */
  1525. rc = kref_get_unless_zero(&io_req->refcount);
  1526. if (!rc) {
  1527. QEDF_ERR(&(qedf->dbg_ctx), "Could not get kref for "
  1528. "io_req=0x%p xid=0x%x\n", io_req, io_req->xid);
  1529. continue;
  1530. }
  1531. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  1532. "Cleanup xid=0x%x.\n", io_req->xid);
  1533. flush_cnt++;
  1534. /* Cleanup task and return I/O mid-layer */
  1535. qedf_initiate_cleanup(io_req, true);
  1536. free_cmd:
  1537. kref_put(&io_req->refcount, qedf_release_cmd); /* ID: 004 */
  1538. }
  1539. wait_cnt = 60;
  1540. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
  1541. "Flushed 0x%x I/Os, active=0x%x.\n",
  1542. flush_cnt, atomic_read(&fcport->num_active_ios));
  1543. /* Only wait for all commands to complete in the Upload context */
  1544. if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags) &&
  1545. (lun == -1)) {
  1546. while (atomic_read(&fcport->num_active_ios)) {
  1547. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
  1548. "Flushed 0x%x I/Os, active=0x%x cnt=%d.\n",
  1549. flush_cnt,
  1550. atomic_read(&fcport->num_active_ios),
  1551. wait_cnt);
  1552. if (wait_cnt == 0) {
  1553. QEDF_ERR(&qedf->dbg_ctx,
  1554. "Flushed %d I/Os, active=%d.\n",
  1555. flush_cnt,
  1556. atomic_read(&fcport->num_active_ios));
  1557. for (i = 0; i < FCOE_PARAMS_NUM_TASKS; i++) {
  1558. io_req = &cmd_mgr->cmds[i];
  1559. if (io_req->fcport &&
  1560. io_req->fcport == fcport) {
  1561. refcount =
  1562. kref_read(&io_req->refcount);
  1563. set_bit(QEDF_CMD_DIRTY,
  1564. &io_req->flags);
  1565. QEDF_ERR(&qedf->dbg_ctx,
  1566. "Outstanding io_req =%p xid=0x%x flags=0x%lx, sc_cmd=%p refcount=%d cmd_type=%d.\n",
  1567. io_req, io_req->xid,
  1568. io_req->flags,
  1569. io_req->sc_cmd,
  1570. refcount,
  1571. io_req->cmd_type);
  1572. }
  1573. }
  1574. WARN_ON(1);
  1575. break;
  1576. }
  1577. msleep(500);
  1578. wait_cnt--;
  1579. }
  1580. }
  1581. clear_bit(QEDF_RPORT_IN_LUN_RESET, &fcport->flags);
  1582. clear_bit(QEDF_RPORT_IN_TARGET_RESET, &fcport->flags);
  1583. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO, "Unlocking flush mutex.\n");
  1584. mutex_unlock(&qedf->flush_mutex);
  1585. }
  1586. /*
  1587. * Initiate a ABTS middle path command. Note that we don't have to initialize
  1588. * the task context for an ABTS task.
  1589. */
  1590. int qedf_initiate_abts(struct qedf_ioreq *io_req, bool return_scsi_cmd_on_abts)
  1591. {
  1592. struct fc_lport *lport;
  1593. struct qedf_rport *fcport = io_req->fcport;
  1594. struct fc_rport_priv *rdata;
  1595. struct qedf_ctx *qedf;
  1596. u16 xid;
  1597. int rc = 0;
  1598. unsigned long flags;
  1599. struct fcoe_wqe *sqe;
  1600. u16 sqe_idx;
  1601. int refcount = 0;
  1602. /* Sanity check qedf_rport before dereferencing any pointers */
  1603. if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
  1604. QEDF_ERR(NULL, "tgt not offloaded\n");
  1605. rc = 1;
  1606. goto out;
  1607. }
  1608. qedf = fcport->qedf;
  1609. rdata = fcport->rdata;
  1610. if (!rdata || !kref_get_unless_zero(&rdata->kref)) {
  1611. QEDF_ERR(&qedf->dbg_ctx, "stale rport\n");
  1612. rc = 1;
  1613. goto out;
  1614. }
  1615. lport = qedf->lport;
  1616. if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
  1617. QEDF_ERR(&(qedf->dbg_ctx), "link is not ready\n");
  1618. rc = 1;
  1619. goto drop_rdata_kref;
  1620. }
  1621. if (atomic_read(&qedf->link_down_tmo_valid) > 0) {
  1622. QEDF_ERR(&(qedf->dbg_ctx), "link_down_tmo active.\n");
  1623. rc = 1;
  1624. goto drop_rdata_kref;
  1625. }
  1626. /* Ensure room on SQ */
  1627. if (!atomic_read(&fcport->free_sqes)) {
  1628. QEDF_ERR(&(qedf->dbg_ctx), "No SQ entries available\n");
  1629. rc = 1;
  1630. goto drop_rdata_kref;
  1631. }
  1632. if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
  1633. QEDF_ERR(&qedf->dbg_ctx, "fcport is uploading.\n");
  1634. rc = 1;
  1635. goto drop_rdata_kref;
  1636. }
  1637. spin_lock_irqsave(&fcport->rport_lock, flags);
  1638. if (!test_bit(QEDF_CMD_OUTSTANDING, &io_req->flags) ||
  1639. test_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags) ||
  1640. test_bit(QEDF_CMD_IN_ABORT, &io_req->flags)) {
  1641. QEDF_ERR(&qedf->dbg_ctx,
  1642. "io_req xid=0x%x sc_cmd=%p already in cleanup or abort processing or already completed.\n",
  1643. io_req->xid, io_req->sc_cmd);
  1644. rc = 1;
  1645. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1646. goto drop_rdata_kref;
  1647. }
  1648. /* Set the command type to abort */
  1649. io_req->cmd_type = QEDF_ABTS;
  1650. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1651. kref_get(&io_req->refcount);
  1652. xid = io_req->xid;
  1653. qedf->control_requests++;
  1654. qedf->packet_aborts++;
  1655. io_req->return_scsi_cmd_on_abts = return_scsi_cmd_on_abts;
  1656. set_bit(QEDF_CMD_IN_ABORT, &io_req->flags);
  1657. refcount = kref_read(&io_req->refcount);
  1658. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_SCSI_TM,
  1659. "ABTS io_req xid = 0x%x refcount=%d\n",
  1660. xid, refcount);
  1661. qedf_cmd_timer_set(qedf, io_req, QEDF_ABORT_TIMEOUT);
  1662. spin_lock_irqsave(&fcport->rport_lock, flags);
  1663. sqe_idx = qedf_get_sqe_idx(fcport);
  1664. sqe = &fcport->sq[sqe_idx];
  1665. memset(sqe, 0, sizeof(struct fcoe_wqe));
  1666. io_req->task_params->sqe = sqe;
  1667. init_initiator_abort_fcoe_task(io_req->task_params);
  1668. qedf_ring_doorbell(fcport);
  1669. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1670. drop_rdata_kref:
  1671. kref_put(&rdata->kref, fc_rport_destroy);
  1672. out:
  1673. return rc;
  1674. }
  1675. void qedf_process_abts_compl(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
  1676. struct qedf_ioreq *io_req)
  1677. {
  1678. uint32_t r_ctl;
  1679. int rc;
  1680. struct qedf_rport *fcport = io_req->fcport;
  1681. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_SCSI_TM, "Entered with xid = "
  1682. "0x%x cmd_type = %d\n", io_req->xid, io_req->cmd_type);
  1683. r_ctl = cqe->cqe_info.abts_info.r_ctl;
  1684. /* This was added at a point when we were scheduling abts_compl &
  1685. * cleanup_compl on different CPUs and there was a possibility of
  1686. * the io_req to be freed from the other context before we got here.
  1687. */
  1688. if (!fcport) {
  1689. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
  1690. "Dropping ABTS completion xid=0x%x as fcport is NULL",
  1691. io_req->xid);
  1692. return;
  1693. }
  1694. /*
  1695. * When flush is active, let the cmds be completed from the cleanup
  1696. * context
  1697. */
  1698. if (test_bit(QEDF_RPORT_IN_TARGET_RESET, &fcport->flags) ||
  1699. test_bit(QEDF_RPORT_IN_LUN_RESET, &fcport->flags)) {
  1700. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
  1701. "Dropping ABTS completion xid=0x%x as fcport is flushing",
  1702. io_req->xid);
  1703. return;
  1704. }
  1705. if (!cancel_delayed_work(&io_req->timeout_work)) {
  1706. QEDF_ERR(&qedf->dbg_ctx,
  1707. "Wasn't able to cancel abts timeout work.\n");
  1708. }
  1709. switch (r_ctl) {
  1710. case FC_RCTL_BA_ACC:
  1711. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_SCSI_TM,
  1712. "ABTS response - ACC Send RRQ after R_A_TOV\n");
  1713. io_req->event = QEDF_IOREQ_EV_ABORT_SUCCESS;
  1714. rc = kref_get_unless_zero(&io_req->refcount); /* ID: 003 */
  1715. if (!rc) {
  1716. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_SCSI_TM,
  1717. "kref is already zero so ABTS was already completed or flushed xid=0x%x.\n",
  1718. io_req->xid);
  1719. return;
  1720. }
  1721. /*
  1722. * Dont release this cmd yet. It will be relesed
  1723. * after we get RRQ response
  1724. */
  1725. queue_delayed_work(qedf->dpc_wq, &io_req->rrq_work,
  1726. msecs_to_jiffies(qedf->lport->r_a_tov));
  1727. atomic_set(&io_req->state, QEDFC_CMD_ST_RRQ_WAIT);
  1728. break;
  1729. /* For error cases let the cleanup return the command */
  1730. case FC_RCTL_BA_RJT:
  1731. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_SCSI_TM,
  1732. "ABTS response - RJT\n");
  1733. io_req->event = QEDF_IOREQ_EV_ABORT_FAILED;
  1734. break;
  1735. default:
  1736. QEDF_ERR(&(qedf->dbg_ctx), "Unknown ABTS response\n");
  1737. break;
  1738. }
  1739. clear_bit(QEDF_CMD_IN_ABORT, &io_req->flags);
  1740. if (io_req->sc_cmd) {
  1741. if (!io_req->return_scsi_cmd_on_abts)
  1742. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_SCSI_TM,
  1743. "Not call scsi_done for xid=0x%x.\n",
  1744. io_req->xid);
  1745. if (io_req->return_scsi_cmd_on_abts)
  1746. qedf_scsi_done(qedf, io_req, DID_ERROR);
  1747. }
  1748. /* Notify eh_abort handler that ABTS is complete */
  1749. complete(&io_req->abts_done);
  1750. kref_put(&io_req->refcount, qedf_release_cmd);
  1751. }
  1752. int qedf_init_mp_req(struct qedf_ioreq *io_req)
  1753. {
  1754. struct qedf_mp_req *mp_req;
  1755. struct scsi_sge *mp_req_bd;
  1756. struct scsi_sge *mp_resp_bd;
  1757. struct qedf_ctx *qedf = io_req->fcport->qedf;
  1758. dma_addr_t addr;
  1759. uint64_t sz;
  1760. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_MP_REQ, "Entered.\n");
  1761. mp_req = (struct qedf_mp_req *)&(io_req->mp_req);
  1762. memset(mp_req, 0, sizeof(struct qedf_mp_req));
  1763. if (io_req->cmd_type != QEDF_ELS) {
  1764. mp_req->req_len = sizeof(struct fcp_cmnd);
  1765. io_req->data_xfer_len = mp_req->req_len;
  1766. } else
  1767. mp_req->req_len = io_req->data_xfer_len;
  1768. mp_req->req_buf = dma_alloc_coherent(&qedf->pdev->dev, QEDF_PAGE_SIZE,
  1769. &mp_req->req_buf_dma, GFP_KERNEL);
  1770. if (!mp_req->req_buf) {
  1771. QEDF_ERR(&(qedf->dbg_ctx), "Unable to alloc MP req buffer\n");
  1772. qedf_free_mp_resc(io_req);
  1773. return -ENOMEM;
  1774. }
  1775. mp_req->resp_buf = dma_alloc_coherent(&qedf->pdev->dev,
  1776. QEDF_PAGE_SIZE, &mp_req->resp_buf_dma, GFP_KERNEL);
  1777. if (!mp_req->resp_buf) {
  1778. QEDF_ERR(&(qedf->dbg_ctx), "Unable to alloc TM resp "
  1779. "buffer\n");
  1780. qedf_free_mp_resc(io_req);
  1781. return -ENOMEM;
  1782. }
  1783. /* Allocate and map mp_req_bd and mp_resp_bd */
  1784. sz = sizeof(struct scsi_sge);
  1785. mp_req->mp_req_bd = dma_alloc_coherent(&qedf->pdev->dev, sz,
  1786. &mp_req->mp_req_bd_dma, GFP_KERNEL);
  1787. if (!mp_req->mp_req_bd) {
  1788. QEDF_ERR(&(qedf->dbg_ctx), "Unable to alloc MP req bd\n");
  1789. qedf_free_mp_resc(io_req);
  1790. return -ENOMEM;
  1791. }
  1792. mp_req->mp_resp_bd = dma_alloc_coherent(&qedf->pdev->dev, sz,
  1793. &mp_req->mp_resp_bd_dma, GFP_KERNEL);
  1794. if (!mp_req->mp_resp_bd) {
  1795. QEDF_ERR(&(qedf->dbg_ctx), "Unable to alloc MP resp bd\n");
  1796. qedf_free_mp_resc(io_req);
  1797. return -ENOMEM;
  1798. }
  1799. /* Fill bd table */
  1800. addr = mp_req->req_buf_dma;
  1801. mp_req_bd = mp_req->mp_req_bd;
  1802. mp_req_bd->sge_addr.lo = U64_LO(addr);
  1803. mp_req_bd->sge_addr.hi = U64_HI(addr);
  1804. mp_req_bd->sge_len = QEDF_PAGE_SIZE;
  1805. /*
  1806. * MP buffer is either a task mgmt command or an ELS.
  1807. * So the assumption is that it consumes a single bd
  1808. * entry in the bd table
  1809. */
  1810. mp_resp_bd = mp_req->mp_resp_bd;
  1811. addr = mp_req->resp_buf_dma;
  1812. mp_resp_bd->sge_addr.lo = U64_LO(addr);
  1813. mp_resp_bd->sge_addr.hi = U64_HI(addr);
  1814. mp_resp_bd->sge_len = QEDF_PAGE_SIZE;
  1815. return 0;
  1816. }
  1817. /*
  1818. * Last ditch effort to clear the port if it's stuck. Used only after a
  1819. * cleanup task times out.
  1820. */
  1821. static void qedf_drain_request(struct qedf_ctx *qedf)
  1822. {
  1823. if (test_bit(QEDF_DRAIN_ACTIVE, &qedf->flags)) {
  1824. QEDF_ERR(&(qedf->dbg_ctx), "MCP drain already active.\n");
  1825. return;
  1826. }
  1827. /* Set bit to return all queuecommand requests as busy */
  1828. set_bit(QEDF_DRAIN_ACTIVE, &qedf->flags);
  1829. /* Call qed drain request for function. Should be synchronous */
  1830. qed_ops->common->drain(qedf->cdev);
  1831. /* Settle time for CQEs to be returned */
  1832. msleep(100);
  1833. /* Unplug and continue */
  1834. clear_bit(QEDF_DRAIN_ACTIVE, &qedf->flags);
  1835. }
  1836. /*
  1837. * Returns SUCCESS if the cleanup task does not timeout, otherwise return
  1838. * FAILURE.
  1839. */
  1840. int qedf_initiate_cleanup(struct qedf_ioreq *io_req,
  1841. bool return_scsi_cmd_on_abts)
  1842. {
  1843. struct qedf_rport *fcport;
  1844. struct qedf_ctx *qedf;
  1845. int tmo = 0;
  1846. int rc = SUCCESS;
  1847. unsigned long flags;
  1848. struct fcoe_wqe *sqe;
  1849. u16 sqe_idx;
  1850. int refcount = 0;
  1851. fcport = io_req->fcport;
  1852. if (!fcport) {
  1853. QEDF_ERR(NULL, "fcport is NULL.\n");
  1854. return SUCCESS;
  1855. }
  1856. /* Sanity check qedf_rport before dereferencing any pointers */
  1857. if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
  1858. QEDF_ERR(NULL, "tgt not offloaded\n");
  1859. return SUCCESS;
  1860. }
  1861. qedf = fcport->qedf;
  1862. if (!qedf) {
  1863. QEDF_ERR(NULL, "qedf is NULL.\n");
  1864. return SUCCESS;
  1865. }
  1866. if (io_req->cmd_type == QEDF_ELS) {
  1867. goto process_els;
  1868. }
  1869. if (!test_bit(QEDF_CMD_OUTSTANDING, &io_req->flags) ||
  1870. test_and_set_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags)) {
  1871. QEDF_ERR(&(qedf->dbg_ctx), "io_req xid=0x%x already in "
  1872. "cleanup processing or already completed.\n",
  1873. io_req->xid);
  1874. return SUCCESS;
  1875. }
  1876. set_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags);
  1877. process_els:
  1878. /* Ensure room on SQ */
  1879. if (!atomic_read(&fcport->free_sqes)) {
  1880. QEDF_ERR(&(qedf->dbg_ctx), "No SQ entries available\n");
  1881. /* Need to make sure we clear the flag since it was set */
  1882. clear_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags);
  1883. return FAILED;
  1884. }
  1885. if (io_req->cmd_type == QEDF_CLEANUP) {
  1886. QEDF_ERR(&qedf->dbg_ctx,
  1887. "io_req=0x%x is already a cleanup command cmd_type=%d.\n",
  1888. io_req->xid, io_req->cmd_type);
  1889. clear_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags);
  1890. return SUCCESS;
  1891. }
  1892. refcount = kref_read(&io_req->refcount);
  1893. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_IO,
  1894. "Entered xid=0x%x sc_cmd=%p cmd_type=%d flags=0x%lx refcount=%d fcport=%p port_id=0x%06x\n",
  1895. io_req->xid, io_req->sc_cmd, io_req->cmd_type, io_req->flags,
  1896. refcount, fcport, fcport->rdata->ids.port_id);
  1897. /* Cleanup cmds re-use the same TID as the original I/O */
  1898. spin_lock_irqsave(&fcport->rport_lock, flags);
  1899. io_req->cmd_type = QEDF_CLEANUP;
  1900. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1901. io_req->return_scsi_cmd_on_abts = return_scsi_cmd_on_abts;
  1902. init_completion(&io_req->cleanup_done);
  1903. spin_lock_irqsave(&fcport->rport_lock, flags);
  1904. sqe_idx = qedf_get_sqe_idx(fcport);
  1905. sqe = &fcport->sq[sqe_idx];
  1906. memset(sqe, 0, sizeof(struct fcoe_wqe));
  1907. io_req->task_params->sqe = sqe;
  1908. init_initiator_cleanup_fcoe_task(io_req->task_params);
  1909. qedf_ring_doorbell(fcport);
  1910. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1911. tmo = wait_for_completion_timeout(&io_req->cleanup_done,
  1912. QEDF_CLEANUP_TIMEOUT * HZ);
  1913. if (!tmo) {
  1914. rc = FAILED;
  1915. /* Timeout case */
  1916. QEDF_ERR(&(qedf->dbg_ctx), "Cleanup command timeout, "
  1917. "xid=%x.\n", io_req->xid);
  1918. clear_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags);
  1919. /* Issue a drain request if cleanup task times out */
  1920. QEDF_ERR(&(qedf->dbg_ctx), "Issuing MCP drain request.\n");
  1921. qedf_drain_request(qedf);
  1922. }
  1923. /* If it TASK MGMT handle it, reference will be decreased
  1924. * in qedf_execute_tmf
  1925. */
  1926. if (io_req->tm_flags == FCP_TMF_LUN_RESET ||
  1927. io_req->tm_flags == FCP_TMF_TGT_RESET) {
  1928. clear_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
  1929. io_req->sc_cmd = NULL;
  1930. kref_put(&io_req->refcount, qedf_release_cmd);
  1931. complete(&io_req->tm_done);
  1932. }
  1933. if (io_req->sc_cmd) {
  1934. if (!io_req->return_scsi_cmd_on_abts)
  1935. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_SCSI_TM,
  1936. "Not call scsi_done for xid=0x%x.\n",
  1937. io_req->xid);
  1938. if (io_req->return_scsi_cmd_on_abts)
  1939. qedf_scsi_done(qedf, io_req, DID_ERROR);
  1940. }
  1941. if (rc == SUCCESS)
  1942. io_req->event = QEDF_IOREQ_EV_CLEANUP_SUCCESS;
  1943. else
  1944. io_req->event = QEDF_IOREQ_EV_CLEANUP_FAILED;
  1945. return rc;
  1946. }
  1947. void qedf_process_cleanup_compl(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
  1948. struct qedf_ioreq *io_req)
  1949. {
  1950. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO, "Entered xid = 0x%x\n",
  1951. io_req->xid);
  1952. clear_bit(QEDF_CMD_IN_CLEANUP, &io_req->flags);
  1953. /* Complete so we can finish cleaning up the I/O */
  1954. complete(&io_req->cleanup_done);
  1955. }
  1956. static int qedf_execute_tmf(struct qedf_rport *fcport, struct scsi_cmnd *sc_cmd,
  1957. uint8_t tm_flags)
  1958. {
  1959. struct qedf_ioreq *io_req;
  1960. struct fcoe_task_context *task;
  1961. struct qedf_ctx *qedf = fcport->qedf;
  1962. struct fc_lport *lport = qedf->lport;
  1963. int rc = 0;
  1964. uint16_t xid;
  1965. int tmo = 0;
  1966. int lun = 0;
  1967. unsigned long flags;
  1968. struct fcoe_wqe *sqe;
  1969. u16 sqe_idx;
  1970. if (!sc_cmd) {
  1971. QEDF_ERR(&qedf->dbg_ctx, "sc_cmd is NULL\n");
  1972. return FAILED;
  1973. }
  1974. lun = (int)sc_cmd->device->lun;
  1975. if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
  1976. QEDF_ERR(&(qedf->dbg_ctx), "fcport not offloaded\n");
  1977. rc = FAILED;
  1978. goto no_flush;
  1979. }
  1980. io_req = qedf_alloc_cmd(fcport, QEDF_TASK_MGMT_CMD);
  1981. if (!io_req) {
  1982. QEDF_ERR(&(qedf->dbg_ctx), "Failed TMF");
  1983. rc = -EAGAIN;
  1984. goto no_flush;
  1985. }
  1986. if (tm_flags == FCP_TMF_LUN_RESET)
  1987. qedf->lun_resets++;
  1988. else if (tm_flags == FCP_TMF_TGT_RESET)
  1989. qedf->target_resets++;
  1990. /* Initialize rest of io_req fields */
  1991. io_req->sc_cmd = sc_cmd;
  1992. io_req->fcport = fcport;
  1993. io_req->cmd_type = QEDF_TASK_MGMT_CMD;
  1994. /* Record which cpu this request is associated with */
  1995. io_req->cpu = smp_processor_id();
  1996. /* Set TM flags */
  1997. io_req->io_req_flags = QEDF_READ;
  1998. io_req->data_xfer_len = 0;
  1999. io_req->tm_flags = tm_flags;
  2000. /* Default is to return a SCSI command when an error occurs */
  2001. io_req->return_scsi_cmd_on_abts = false;
  2002. /* Obtain exchange id */
  2003. xid = io_req->xid;
  2004. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_SCSI_TM, "TMF io_req xid = "
  2005. "0x%x\n", xid);
  2006. /* Initialize task context for this IO request */
  2007. task = qedf_get_task_mem(&qedf->tasks, xid);
  2008. init_completion(&io_req->tm_done);
  2009. spin_lock_irqsave(&fcport->rport_lock, flags);
  2010. sqe_idx = qedf_get_sqe_idx(fcport);
  2011. sqe = &fcport->sq[sqe_idx];
  2012. memset(sqe, 0, sizeof(struct fcoe_wqe));
  2013. qedf_init_task(fcport, lport, io_req, task, sqe);
  2014. qedf_ring_doorbell(fcport);
  2015. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  2016. set_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
  2017. tmo = wait_for_completion_timeout(&io_req->tm_done,
  2018. QEDF_TM_TIMEOUT * HZ);
  2019. if (!tmo) {
  2020. rc = FAILED;
  2021. QEDF_ERR(&(qedf->dbg_ctx), "wait for tm_cmpl timeout!\n");
  2022. /* Clear outstanding bit since command timed out */
  2023. clear_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
  2024. io_req->sc_cmd = NULL;
  2025. } else {
  2026. /* Check TMF response code */
  2027. if (io_req->fcp_rsp_code == 0)
  2028. rc = SUCCESS;
  2029. else
  2030. rc = FAILED;
  2031. }
  2032. /*
  2033. * Double check that fcport has not gone into an uploading state before
  2034. * executing the command flush for the LUN/target.
  2035. */
  2036. if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
  2037. QEDF_ERR(&qedf->dbg_ctx,
  2038. "fcport is uploading, not executing flush.\n");
  2039. goto no_flush;
  2040. }
  2041. /* We do not need this io_req any more */
  2042. kref_put(&io_req->refcount, qedf_release_cmd);
  2043. if (tm_flags == FCP_TMF_LUN_RESET)
  2044. qedf_flush_active_ios(fcport, lun);
  2045. else
  2046. qedf_flush_active_ios(fcport, -1);
  2047. no_flush:
  2048. if (rc != SUCCESS) {
  2049. QEDF_ERR(&(qedf->dbg_ctx), "task mgmt command failed...\n");
  2050. rc = FAILED;
  2051. } else {
  2052. QEDF_ERR(&(qedf->dbg_ctx), "task mgmt command success...\n");
  2053. rc = SUCCESS;
  2054. }
  2055. return rc;
  2056. }
  2057. int qedf_initiate_tmf(struct scsi_cmnd *sc_cmd, u8 tm_flags)
  2058. {
  2059. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  2060. struct fc_rport_libfc_priv *rp = rport->dd_data;
  2061. struct qedf_rport *fcport = (struct qedf_rport *)&rp[1];
  2062. struct qedf_ctx *qedf;
  2063. struct fc_lport *lport = shost_priv(sc_cmd->device->host);
  2064. int rc = SUCCESS;
  2065. int rval;
  2066. struct qedf_ioreq *io_req = NULL;
  2067. int ref_cnt = 0;
  2068. struct fc_rport_priv *rdata = fcport->rdata;
  2069. QEDF_ERR(NULL,
  2070. "tm_flags 0x%x sc_cmd %p op = 0x%02x target_id = 0x%x lun=%d\n",
  2071. tm_flags, sc_cmd, sc_cmd->cmd_len ? sc_cmd->cmnd[0] : 0xff,
  2072. rport->scsi_target_id, (int)sc_cmd->device->lun);
  2073. if (!rdata || !kref_get_unless_zero(&rdata->kref)) {
  2074. QEDF_ERR(NULL, "stale rport\n");
  2075. return FAILED;
  2076. }
  2077. QEDF_ERR(NULL, "portid=%06x tm_flags =%s\n", rdata->ids.port_id,
  2078. (tm_flags == FCP_TMF_TGT_RESET) ? "TARGET RESET" :
  2079. "LUN RESET");
  2080. if (qedf_priv(sc_cmd)->io_req) {
  2081. io_req = qedf_priv(sc_cmd)->io_req;
  2082. ref_cnt = kref_read(&io_req->refcount);
  2083. QEDF_ERR(NULL,
  2084. "orig io_req = %p xid = 0x%x ref_cnt = %d.\n",
  2085. io_req, io_req->xid, ref_cnt);
  2086. }
  2087. rval = fc_remote_port_chkready(rport);
  2088. if (rval) {
  2089. QEDF_ERR(NULL, "device_reset rport not ready\n");
  2090. rc = FAILED;
  2091. goto tmf_err;
  2092. }
  2093. rc = fc_block_scsi_eh(sc_cmd);
  2094. if (rc)
  2095. goto tmf_err;
  2096. if (!fcport) {
  2097. QEDF_ERR(NULL, "device_reset: rport is NULL\n");
  2098. rc = FAILED;
  2099. goto tmf_err;
  2100. }
  2101. qedf = fcport->qedf;
  2102. if (!qedf) {
  2103. QEDF_ERR(NULL, "qedf is NULL.\n");
  2104. rc = FAILED;
  2105. goto tmf_err;
  2106. }
  2107. if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
  2108. QEDF_ERR(&qedf->dbg_ctx, "Connection is getting uploaded.\n");
  2109. rc = SUCCESS;
  2110. goto tmf_err;
  2111. }
  2112. if (test_bit(QEDF_UNLOADING, &qedf->flags) ||
  2113. test_bit(QEDF_DBG_STOP_IO, &qedf->flags)) {
  2114. rc = SUCCESS;
  2115. goto tmf_err;
  2116. }
  2117. if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
  2118. QEDF_ERR(&(qedf->dbg_ctx), "link is not ready\n");
  2119. rc = FAILED;
  2120. goto tmf_err;
  2121. }
  2122. if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
  2123. if (!fcport->rdata)
  2124. QEDF_ERR(&qedf->dbg_ctx, "fcport %p is uploading.\n",
  2125. fcport);
  2126. else
  2127. QEDF_ERR(&qedf->dbg_ctx,
  2128. "fcport %p port_id=%06x is uploading.\n",
  2129. fcport, fcport->rdata->ids.port_id);
  2130. rc = FAILED;
  2131. goto tmf_err;
  2132. }
  2133. rc = qedf_execute_tmf(fcport, sc_cmd, tm_flags);
  2134. tmf_err:
  2135. kref_put(&rdata->kref, fc_rport_destroy);
  2136. return rc;
  2137. }
  2138. void qedf_process_tmf_compl(struct qedf_ctx *qedf, struct fcoe_cqe *cqe,
  2139. struct qedf_ioreq *io_req)
  2140. {
  2141. struct fcoe_cqe_rsp_info *fcp_rsp;
  2142. clear_bit(QEDF_CMD_OUTSTANDING, &io_req->flags);
  2143. fcp_rsp = &cqe->cqe_info.rsp_info;
  2144. qedf_parse_fcp_rsp(io_req, fcp_rsp);
  2145. io_req->sc_cmd = NULL;
  2146. complete(&io_req->tm_done);
  2147. }
  2148. void qedf_process_unsol_compl(struct qedf_ctx *qedf, uint16_t que_idx,
  2149. struct fcoe_cqe *cqe)
  2150. {
  2151. unsigned long flags;
  2152. uint16_t pktlen = cqe->cqe_info.unsolic_info.pkt_len;
  2153. u32 payload_len, crc;
  2154. struct fc_frame_header *fh;
  2155. struct fc_frame *fp;
  2156. struct qedf_io_work *io_work;
  2157. u32 bdq_idx;
  2158. void *bdq_addr;
  2159. struct scsi_bd *p_bd_info;
  2160. p_bd_info = &cqe->cqe_info.unsolic_info.bd_info;
  2161. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_UNSOL,
  2162. "address.hi=%x, address.lo=%x, opaque_data.hi=%x, opaque_data.lo=%x, bdq_prod_idx=%u, len=%u\n",
  2163. le32_to_cpu(p_bd_info->address.hi),
  2164. le32_to_cpu(p_bd_info->address.lo),
  2165. le32_to_cpu(p_bd_info->opaque.fcoe_opaque.hi),
  2166. le32_to_cpu(p_bd_info->opaque.fcoe_opaque.lo),
  2167. qedf->bdq_prod_idx, pktlen);
  2168. bdq_idx = le32_to_cpu(p_bd_info->opaque.fcoe_opaque.lo);
  2169. if (bdq_idx >= QEDF_BDQ_SIZE) {
  2170. QEDF_ERR(&(qedf->dbg_ctx), "bdq_idx is out of range %d.\n",
  2171. bdq_idx);
  2172. goto increment_prod;
  2173. }
  2174. bdq_addr = qedf->bdq[bdq_idx].buf_addr;
  2175. if (!bdq_addr) {
  2176. QEDF_ERR(&(qedf->dbg_ctx), "bdq_addr is NULL, dropping "
  2177. "unsolicited packet.\n");
  2178. goto increment_prod;
  2179. }
  2180. if (qedf_dump_frames) {
  2181. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_UNSOL,
  2182. "BDQ frame is at addr=%p.\n", bdq_addr);
  2183. print_hex_dump(KERN_WARNING, "bdq ", DUMP_PREFIX_OFFSET, 16, 1,
  2184. (void *)bdq_addr, pktlen, false);
  2185. }
  2186. /* Allocate frame */
  2187. payload_len = pktlen - sizeof(struct fc_frame_header);
  2188. fp = fc_frame_alloc(qedf->lport, payload_len);
  2189. if (!fp) {
  2190. QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate fp.\n");
  2191. goto increment_prod;
  2192. }
  2193. /* Copy data from BDQ buffer into fc_frame struct */
  2194. fh = (struct fc_frame_header *)fc_frame_header_get(fp);
  2195. memcpy(fh, (void *)bdq_addr, pktlen);
  2196. QEDF_WARN(&qedf->dbg_ctx,
  2197. "Processing Unsolicated frame, src=%06x dest=%06x r_ctl=0x%x type=0x%x cmd=%02x\n",
  2198. ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id), fh->fh_r_ctl,
  2199. fh->fh_type, fc_frame_payload_op(fp));
  2200. /* Initialize the frame so libfc sees it as a valid frame */
  2201. crc = fcoe_fc_crc(fp);
  2202. fc_frame_init(fp);
  2203. fr_dev(fp) = qedf->lport;
  2204. fr_sof(fp) = FC_SOF_I3;
  2205. fr_eof(fp) = FC_EOF_T;
  2206. fr_crc(fp) = cpu_to_le32(~crc);
  2207. /*
  2208. * We need to return the frame back up to libfc in a non-atomic
  2209. * context
  2210. */
  2211. io_work = mempool_alloc(qedf->io_mempool, GFP_ATOMIC);
  2212. if (!io_work) {
  2213. QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate "
  2214. "work for I/O completion.\n");
  2215. fc_frame_free(fp);
  2216. goto increment_prod;
  2217. }
  2218. memset(io_work, 0, sizeof(struct qedf_io_work));
  2219. INIT_WORK(&io_work->work, qedf_fp_io_handler);
  2220. /* Copy contents of CQE for deferred processing */
  2221. memcpy(&io_work->cqe, cqe, sizeof(struct fcoe_cqe));
  2222. io_work->qedf = qedf;
  2223. io_work->fp = fp;
  2224. queue_work_on(smp_processor_id(), qedf_io_wq, &io_work->work);
  2225. increment_prod:
  2226. spin_lock_irqsave(&qedf->hba_lock, flags);
  2227. /* Increment producer to let f/w know we've handled the frame */
  2228. qedf->bdq_prod_idx++;
  2229. /* Producer index wraps at uint16_t boundary */
  2230. if (qedf->bdq_prod_idx == 0xffff)
  2231. qedf->bdq_prod_idx = 0;
  2232. writew(qedf->bdq_prod_idx, qedf->bdq_primary_prod);
  2233. readw(qedf->bdq_primary_prod);
  2234. writew(qedf->bdq_prod_idx, qedf->bdq_secondary_prod);
  2235. readw(qedf->bdq_secondary_prod);
  2236. spin_unlock_irqrestore(&qedf->hba_lock, flags);
  2237. }