qedf_io.c 58 KB

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