bnx2fc_io.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102
  1. /* bnx2fc_io.c: QLogic Linux FCoE offload driver.
  2. * IO manager and SCSI IO processing.
  3. *
  4. * Copyright (c) 2008-2013 Broadcom Corporation
  5. * Copyright (c) 2014-2016 QLogic Corporation
  6. * Copyright (c) 2016-2017 Cavium Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation.
  11. *
  12. * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
  13. */
  14. #include "bnx2fc.h"
  15. #define RESERVE_FREE_LIST_INDEX num_possible_cpus()
  16. static int bnx2fc_split_bd(struct bnx2fc_cmd *io_req, u64 addr, int sg_len,
  17. int bd_index);
  18. static int bnx2fc_map_sg(struct bnx2fc_cmd *io_req);
  19. static int bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd *io_req);
  20. static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd *io_req);
  21. static void bnx2fc_free_mp_resc(struct bnx2fc_cmd *io_req);
  22. static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd *io_req,
  23. struct fcoe_fcp_rsp_payload *fcp_rsp,
  24. u8 num_rq, unsigned char *rq_data);
  25. void bnx2fc_cmd_timer_set(struct bnx2fc_cmd *io_req,
  26. unsigned int timer_msec)
  27. {
  28. struct bnx2fc_interface *interface = io_req->port->priv;
  29. if (queue_delayed_work(interface->timer_work_queue,
  30. &io_req->timeout_work,
  31. msecs_to_jiffies(timer_msec)))
  32. kref_get(&io_req->refcount);
  33. }
  34. static void bnx2fc_cmd_timeout(struct work_struct *work)
  35. {
  36. struct bnx2fc_cmd *io_req = container_of(work, struct bnx2fc_cmd,
  37. timeout_work.work);
  38. u8 cmd_type = io_req->cmd_type;
  39. struct bnx2fc_rport *tgt = io_req->tgt;
  40. int rc;
  41. BNX2FC_IO_DBG(io_req, "cmd_timeout, cmd_type = %d,"
  42. "req_flags = %lx\n", cmd_type, io_req->req_flags);
  43. spin_lock_bh(&tgt->tgt_lock);
  44. if (test_and_clear_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags)) {
  45. clear_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags);
  46. /*
  47. * ideally we should hold the io_req until RRQ complets,
  48. * and release io_req from timeout hold.
  49. */
  50. spin_unlock_bh(&tgt->tgt_lock);
  51. bnx2fc_send_rrq(io_req);
  52. return;
  53. }
  54. if (test_and_clear_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags)) {
  55. BNX2FC_IO_DBG(io_req, "IO ready for reuse now\n");
  56. goto done;
  57. }
  58. switch (cmd_type) {
  59. case BNX2FC_SCSI_CMD:
  60. if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
  61. &io_req->req_flags)) {
  62. /* Handle eh_abort timeout */
  63. BNX2FC_IO_DBG(io_req, "eh_abort timed out\n");
  64. complete(&io_req->abts_done);
  65. } else if (test_bit(BNX2FC_FLAG_ISSUE_ABTS,
  66. &io_req->req_flags)) {
  67. /* Handle internally generated ABTS timeout */
  68. BNX2FC_IO_DBG(io_req, "ABTS timed out refcnt = %d\n",
  69. kref_read(&io_req->refcount));
  70. if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
  71. &io_req->req_flags))) {
  72. /*
  73. * Cleanup and return original command to
  74. * mid-layer.
  75. */
  76. bnx2fc_initiate_cleanup(io_req);
  77. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  78. spin_unlock_bh(&tgt->tgt_lock);
  79. return;
  80. }
  81. } else {
  82. /* Hanlde IO timeout */
  83. BNX2FC_IO_DBG(io_req, "IO timed out. issue ABTS\n");
  84. if (test_and_set_bit(BNX2FC_FLAG_IO_COMPL,
  85. &io_req->req_flags)) {
  86. BNX2FC_IO_DBG(io_req, "IO completed before "
  87. " timer expiry\n");
  88. goto done;
  89. }
  90. if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
  91. &io_req->req_flags)) {
  92. rc = bnx2fc_initiate_abts(io_req);
  93. if (rc == SUCCESS)
  94. goto done;
  95. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  96. spin_unlock_bh(&tgt->tgt_lock);
  97. return;
  98. } else {
  99. BNX2FC_IO_DBG(io_req, "IO already in "
  100. "ABTS processing\n");
  101. }
  102. }
  103. break;
  104. case BNX2FC_ELS:
  105. if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags)) {
  106. BNX2FC_IO_DBG(io_req, "ABTS for ELS timed out\n");
  107. if (!test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
  108. &io_req->req_flags)) {
  109. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  110. spin_unlock_bh(&tgt->tgt_lock);
  111. return;
  112. }
  113. } else {
  114. /*
  115. * Handle ELS timeout.
  116. * tgt_lock is used to sync compl path and timeout
  117. * path. If els compl path is processing this IO, we
  118. * have nothing to do here, just release the timer hold
  119. */
  120. BNX2FC_IO_DBG(io_req, "ELS timed out\n");
  121. if (test_and_set_bit(BNX2FC_FLAG_ELS_DONE,
  122. &io_req->req_flags))
  123. goto done;
  124. /* Indicate the cb_func that this ELS is timed out */
  125. set_bit(BNX2FC_FLAG_ELS_TIMEOUT, &io_req->req_flags);
  126. if ((io_req->cb_func) && (io_req->cb_arg)) {
  127. io_req->cb_func(io_req->cb_arg);
  128. io_req->cb_arg = NULL;
  129. }
  130. }
  131. break;
  132. default:
  133. printk(KERN_ERR PFX "cmd_timeout: invalid cmd_type %d\n",
  134. cmd_type);
  135. break;
  136. }
  137. done:
  138. /* release the cmd that was held when timer was set */
  139. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  140. spin_unlock_bh(&tgt->tgt_lock);
  141. }
  142. static void bnx2fc_scsi_done(struct bnx2fc_cmd *io_req, int err_code)
  143. {
  144. /* Called with host lock held */
  145. struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
  146. /*
  147. * active_cmd_queue may have other command types as well,
  148. * and during flush operation, we want to error back only
  149. * scsi commands.
  150. */
  151. if (io_req->cmd_type != BNX2FC_SCSI_CMD)
  152. return;
  153. BNX2FC_IO_DBG(io_req, "scsi_done. err_code = 0x%x\n", err_code);
  154. if (test_bit(BNX2FC_FLAG_CMD_LOST, &io_req->req_flags)) {
  155. /* Do not call scsi done for this IO */
  156. return;
  157. }
  158. bnx2fc_unmap_sg_list(io_req);
  159. io_req->sc_cmd = NULL;
  160. /* Sanity checks before returning command to mid-layer */
  161. if (!sc_cmd) {
  162. printk(KERN_ERR PFX "scsi_done - sc_cmd NULL. "
  163. "IO(0x%x) already cleaned up\n",
  164. io_req->xid);
  165. return;
  166. }
  167. if (!sc_cmd->device) {
  168. pr_err(PFX "0x%x: sc_cmd->device is NULL.\n", io_req->xid);
  169. return;
  170. }
  171. if (!sc_cmd->device->host) {
  172. pr_err(PFX "0x%x: sc_cmd->device->host is NULL.\n",
  173. io_req->xid);
  174. return;
  175. }
  176. sc_cmd->result = err_code << 16;
  177. BNX2FC_IO_DBG(io_req, "sc=%p, result=0x%x, retries=%d, allowed=%d\n",
  178. sc_cmd, host_byte(sc_cmd->result), sc_cmd->retries,
  179. sc_cmd->allowed);
  180. scsi_set_resid(sc_cmd, scsi_bufflen(sc_cmd));
  181. bnx2fc_priv(sc_cmd)->io_req = NULL;
  182. scsi_done(sc_cmd);
  183. }
  184. struct bnx2fc_cmd_mgr *bnx2fc_cmd_mgr_alloc(struct bnx2fc_hba *hba)
  185. {
  186. struct bnx2fc_cmd_mgr *cmgr;
  187. struct io_bdt *bdt_info;
  188. struct bnx2fc_cmd *io_req;
  189. size_t len;
  190. u32 mem_size;
  191. u16 xid;
  192. int i;
  193. int num_ios, num_pri_ios;
  194. size_t bd_tbl_sz;
  195. int arr_sz = num_possible_cpus() + 1;
  196. u16 min_xid = BNX2FC_MIN_XID;
  197. u16 max_xid = hba->max_xid;
  198. if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN) {
  199. printk(KERN_ERR PFX "cmd_mgr_alloc: Invalid min_xid 0x%x \
  200. and max_xid 0x%x\n", min_xid, max_xid);
  201. return NULL;
  202. }
  203. BNX2FC_MISC_DBG("min xid 0x%x, max xid 0x%x\n", min_xid, max_xid);
  204. num_ios = max_xid - min_xid + 1;
  205. len = (num_ios * (sizeof(struct bnx2fc_cmd *)));
  206. len += sizeof(struct bnx2fc_cmd_mgr);
  207. cmgr = kzalloc(len, GFP_KERNEL);
  208. if (!cmgr) {
  209. printk(KERN_ERR PFX "failed to alloc cmgr\n");
  210. return NULL;
  211. }
  212. cmgr->hba = hba;
  213. cmgr->free_list = kcalloc(arr_sz, sizeof(*cmgr->free_list),
  214. GFP_KERNEL);
  215. if (!cmgr->free_list) {
  216. printk(KERN_ERR PFX "failed to alloc free_list\n");
  217. goto mem_err;
  218. }
  219. cmgr->free_list_lock = kcalloc(arr_sz, sizeof(*cmgr->free_list_lock),
  220. GFP_KERNEL);
  221. if (!cmgr->free_list_lock) {
  222. printk(KERN_ERR PFX "failed to alloc free_list_lock\n");
  223. kfree(cmgr->free_list);
  224. cmgr->free_list = NULL;
  225. goto mem_err;
  226. }
  227. cmgr->cmds = (struct bnx2fc_cmd **)(cmgr + 1);
  228. for (i = 0; i < arr_sz; i++) {
  229. INIT_LIST_HEAD(&cmgr->free_list[i]);
  230. spin_lock_init(&cmgr->free_list_lock[i]);
  231. }
  232. /*
  233. * Pre-allocated pool of bnx2fc_cmds.
  234. * Last entry in the free list array is the free list
  235. * of slow path requests.
  236. */
  237. xid = BNX2FC_MIN_XID;
  238. num_pri_ios = num_ios - hba->elstm_xids;
  239. for (i = 0; i < num_ios; i++) {
  240. io_req = kzalloc(sizeof(*io_req), GFP_KERNEL);
  241. if (!io_req) {
  242. printk(KERN_ERR PFX "failed to alloc io_req\n");
  243. goto mem_err;
  244. }
  245. INIT_LIST_HEAD(&io_req->link);
  246. INIT_DELAYED_WORK(&io_req->timeout_work, bnx2fc_cmd_timeout);
  247. io_req->xid = xid++;
  248. if (i < num_pri_ios)
  249. list_add_tail(&io_req->link,
  250. &cmgr->free_list[io_req->xid %
  251. num_possible_cpus()]);
  252. else
  253. list_add_tail(&io_req->link,
  254. &cmgr->free_list[num_possible_cpus()]);
  255. io_req++;
  256. }
  257. /* Allocate pool of io_bdts - one for each bnx2fc_cmd */
  258. mem_size = num_ios * sizeof(struct io_bdt *);
  259. cmgr->io_bdt_pool = kzalloc(mem_size, GFP_KERNEL);
  260. if (!cmgr->io_bdt_pool) {
  261. printk(KERN_ERR PFX "failed to alloc io_bdt_pool\n");
  262. goto mem_err;
  263. }
  264. mem_size = sizeof(struct io_bdt);
  265. for (i = 0; i < num_ios; i++) {
  266. cmgr->io_bdt_pool[i] = kmalloc(mem_size, GFP_KERNEL);
  267. if (!cmgr->io_bdt_pool[i]) {
  268. printk(KERN_ERR PFX "failed to alloc "
  269. "io_bdt_pool[%d]\n", i);
  270. goto mem_err;
  271. }
  272. }
  273. /* Allocate an map fcoe_bdt_ctx structures */
  274. bd_tbl_sz = BNX2FC_MAX_BDS_PER_CMD * sizeof(struct fcoe_bd_ctx);
  275. for (i = 0; i < num_ios; i++) {
  276. bdt_info = cmgr->io_bdt_pool[i];
  277. bdt_info->bd_tbl = dma_alloc_coherent(&hba->pcidev->dev,
  278. bd_tbl_sz,
  279. &bdt_info->bd_tbl_dma,
  280. GFP_KERNEL);
  281. if (!bdt_info->bd_tbl) {
  282. printk(KERN_ERR PFX "failed to alloc "
  283. "bdt_tbl[%d]\n", i);
  284. goto mem_err;
  285. }
  286. }
  287. return cmgr;
  288. mem_err:
  289. bnx2fc_cmd_mgr_free(cmgr);
  290. return NULL;
  291. }
  292. void bnx2fc_cmd_mgr_free(struct bnx2fc_cmd_mgr *cmgr)
  293. {
  294. struct io_bdt *bdt_info;
  295. struct bnx2fc_hba *hba = cmgr->hba;
  296. size_t bd_tbl_sz;
  297. u16 min_xid = BNX2FC_MIN_XID;
  298. u16 max_xid = hba->max_xid;
  299. int num_ios;
  300. int i;
  301. num_ios = max_xid - min_xid + 1;
  302. /* Free fcoe_bdt_ctx structures */
  303. if (!cmgr->io_bdt_pool)
  304. goto free_cmd_pool;
  305. bd_tbl_sz = BNX2FC_MAX_BDS_PER_CMD * sizeof(struct fcoe_bd_ctx);
  306. for (i = 0; i < num_ios; i++) {
  307. bdt_info = cmgr->io_bdt_pool[i];
  308. if (bdt_info->bd_tbl) {
  309. dma_free_coherent(&hba->pcidev->dev, bd_tbl_sz,
  310. bdt_info->bd_tbl,
  311. bdt_info->bd_tbl_dma);
  312. bdt_info->bd_tbl = NULL;
  313. }
  314. }
  315. /* Destroy io_bdt pool */
  316. for (i = 0; i < num_ios; i++) {
  317. kfree(cmgr->io_bdt_pool[i]);
  318. cmgr->io_bdt_pool[i] = NULL;
  319. }
  320. kfree(cmgr->io_bdt_pool);
  321. cmgr->io_bdt_pool = NULL;
  322. free_cmd_pool:
  323. kfree(cmgr->free_list_lock);
  324. /* Destroy cmd pool */
  325. if (!cmgr->free_list)
  326. goto free_cmgr;
  327. for (i = 0; i < num_possible_cpus() + 1; i++) {
  328. struct bnx2fc_cmd *tmp, *io_req;
  329. list_for_each_entry_safe(io_req, tmp,
  330. &cmgr->free_list[i], link) {
  331. list_del(&io_req->link);
  332. kfree(io_req);
  333. }
  334. }
  335. kfree(cmgr->free_list);
  336. free_cmgr:
  337. /* Free command manager itself */
  338. kfree(cmgr);
  339. }
  340. struct bnx2fc_cmd *bnx2fc_elstm_alloc(struct bnx2fc_rport *tgt, int type)
  341. {
  342. struct fcoe_port *port = tgt->port;
  343. struct bnx2fc_interface *interface = port->priv;
  344. struct bnx2fc_cmd_mgr *cmd_mgr = interface->hba->cmd_mgr;
  345. struct bnx2fc_cmd *io_req;
  346. struct list_head *listp;
  347. struct io_bdt *bd_tbl;
  348. int index = RESERVE_FREE_LIST_INDEX;
  349. u32 free_sqes;
  350. u32 max_sqes;
  351. u16 xid;
  352. max_sqes = tgt->max_sqes;
  353. switch (type) {
  354. case BNX2FC_TASK_MGMT_CMD:
  355. max_sqes = BNX2FC_TM_MAX_SQES;
  356. break;
  357. case BNX2FC_ELS:
  358. max_sqes = BNX2FC_ELS_MAX_SQES;
  359. break;
  360. default:
  361. break;
  362. }
  363. /*
  364. * NOTE: Free list insertions and deletions are protected with
  365. * cmgr lock
  366. */
  367. spin_lock_bh(&cmd_mgr->free_list_lock[index]);
  368. free_sqes = atomic_read(&tgt->free_sqes);
  369. if ((list_empty(&(cmd_mgr->free_list[index]))) ||
  370. (tgt->num_active_ios.counter >= max_sqes) ||
  371. (free_sqes + max_sqes <= BNX2FC_SQ_WQES_MAX)) {
  372. BNX2FC_TGT_DBG(tgt, "No free els_tm cmds available "
  373. "ios(%d):sqes(%d)\n",
  374. tgt->num_active_ios.counter, tgt->max_sqes);
  375. if (list_empty(&(cmd_mgr->free_list[index])))
  376. printk(KERN_ERR PFX "elstm_alloc: list_empty\n");
  377. spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
  378. return NULL;
  379. }
  380. listp = (struct list_head *)
  381. cmd_mgr->free_list[index].next;
  382. list_del_init(listp);
  383. io_req = (struct bnx2fc_cmd *) listp;
  384. xid = io_req->xid;
  385. cmd_mgr->cmds[xid] = io_req;
  386. atomic_inc(&tgt->num_active_ios);
  387. atomic_dec(&tgt->free_sqes);
  388. spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
  389. INIT_LIST_HEAD(&io_req->link);
  390. io_req->port = port;
  391. io_req->cmd_mgr = cmd_mgr;
  392. io_req->req_flags = 0;
  393. io_req->cmd_type = type;
  394. /* Bind io_bdt for this io_req */
  395. /* Have a static link between io_req and io_bdt_pool */
  396. bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid];
  397. bd_tbl->io_req = io_req;
  398. /* Hold the io_req against deletion */
  399. kref_init(&io_req->refcount);
  400. return io_req;
  401. }
  402. struct bnx2fc_cmd *bnx2fc_cmd_alloc(struct bnx2fc_rport *tgt)
  403. {
  404. struct fcoe_port *port = tgt->port;
  405. struct bnx2fc_interface *interface = port->priv;
  406. struct bnx2fc_cmd_mgr *cmd_mgr = interface->hba->cmd_mgr;
  407. struct bnx2fc_cmd *io_req;
  408. struct list_head *listp;
  409. struct io_bdt *bd_tbl;
  410. u32 free_sqes;
  411. u32 max_sqes;
  412. u16 xid;
  413. int index = raw_smp_processor_id();
  414. max_sqes = BNX2FC_SCSI_MAX_SQES;
  415. /*
  416. * NOTE: Free list insertions and deletions are protected with
  417. * cmgr lock
  418. */
  419. spin_lock_bh(&cmd_mgr->free_list_lock[index]);
  420. free_sqes = atomic_read(&tgt->free_sqes);
  421. if ((list_empty(&cmd_mgr->free_list[index])) ||
  422. (tgt->num_active_ios.counter >= max_sqes) ||
  423. (free_sqes + max_sqes <= BNX2FC_SQ_WQES_MAX)) {
  424. spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
  425. return NULL;
  426. }
  427. listp = (struct list_head *)
  428. cmd_mgr->free_list[index].next;
  429. list_del_init(listp);
  430. io_req = (struct bnx2fc_cmd *) listp;
  431. xid = io_req->xid;
  432. cmd_mgr->cmds[xid] = io_req;
  433. atomic_inc(&tgt->num_active_ios);
  434. atomic_dec(&tgt->free_sqes);
  435. spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
  436. INIT_LIST_HEAD(&io_req->link);
  437. io_req->port = port;
  438. io_req->cmd_mgr = cmd_mgr;
  439. io_req->req_flags = 0;
  440. /* Bind io_bdt for this io_req */
  441. /* Have a static link between io_req and io_bdt_pool */
  442. bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid];
  443. bd_tbl->io_req = io_req;
  444. /* Hold the io_req against deletion */
  445. kref_init(&io_req->refcount);
  446. return io_req;
  447. }
  448. void bnx2fc_cmd_release(struct kref *ref)
  449. {
  450. struct bnx2fc_cmd *io_req = container_of(ref,
  451. struct bnx2fc_cmd, refcount);
  452. struct bnx2fc_cmd_mgr *cmd_mgr = io_req->cmd_mgr;
  453. int index;
  454. if (io_req->cmd_type == BNX2FC_SCSI_CMD)
  455. index = io_req->xid % num_possible_cpus();
  456. else
  457. index = RESERVE_FREE_LIST_INDEX;
  458. spin_lock_bh(&cmd_mgr->free_list_lock[index]);
  459. if (io_req->cmd_type != BNX2FC_SCSI_CMD)
  460. bnx2fc_free_mp_resc(io_req);
  461. cmd_mgr->cmds[io_req->xid] = NULL;
  462. /* Delete IO from retire queue */
  463. list_del_init(&io_req->link);
  464. /* Add it to the free list */
  465. list_add(&io_req->link,
  466. &cmd_mgr->free_list[index]);
  467. atomic_dec(&io_req->tgt->num_active_ios);
  468. spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
  469. }
  470. static void bnx2fc_free_mp_resc(struct bnx2fc_cmd *io_req)
  471. {
  472. struct bnx2fc_mp_req *mp_req = &(io_req->mp_req);
  473. struct bnx2fc_interface *interface = io_req->port->priv;
  474. struct bnx2fc_hba *hba = interface->hba;
  475. size_t sz = sizeof(struct fcoe_bd_ctx);
  476. /* clear tm flags */
  477. mp_req->tm_flags = 0;
  478. if (mp_req->mp_req_bd) {
  479. dma_free_coherent(&hba->pcidev->dev, sz,
  480. mp_req->mp_req_bd,
  481. mp_req->mp_req_bd_dma);
  482. mp_req->mp_req_bd = NULL;
  483. }
  484. if (mp_req->mp_resp_bd) {
  485. dma_free_coherent(&hba->pcidev->dev, sz,
  486. mp_req->mp_resp_bd,
  487. mp_req->mp_resp_bd_dma);
  488. mp_req->mp_resp_bd = NULL;
  489. }
  490. if (mp_req->req_buf) {
  491. dma_free_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
  492. mp_req->req_buf,
  493. mp_req->req_buf_dma);
  494. mp_req->req_buf = NULL;
  495. }
  496. if (mp_req->resp_buf) {
  497. dma_free_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
  498. mp_req->resp_buf,
  499. mp_req->resp_buf_dma);
  500. mp_req->resp_buf = NULL;
  501. }
  502. }
  503. int bnx2fc_init_mp_req(struct bnx2fc_cmd *io_req)
  504. {
  505. struct bnx2fc_mp_req *mp_req;
  506. struct fcoe_bd_ctx *mp_req_bd;
  507. struct fcoe_bd_ctx *mp_resp_bd;
  508. struct bnx2fc_interface *interface = io_req->port->priv;
  509. struct bnx2fc_hba *hba = interface->hba;
  510. dma_addr_t addr;
  511. size_t sz;
  512. mp_req = (struct bnx2fc_mp_req *)&(io_req->mp_req);
  513. memset(mp_req, 0, sizeof(struct bnx2fc_mp_req));
  514. if (io_req->cmd_type != BNX2FC_ELS) {
  515. mp_req->req_len = sizeof(struct fcp_cmnd);
  516. io_req->data_xfer_len = mp_req->req_len;
  517. } else
  518. mp_req->req_len = io_req->data_xfer_len;
  519. mp_req->req_buf = dma_alloc_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
  520. &mp_req->req_buf_dma,
  521. GFP_ATOMIC);
  522. if (!mp_req->req_buf) {
  523. printk(KERN_ERR PFX "unable to alloc MP req buffer\n");
  524. bnx2fc_free_mp_resc(io_req);
  525. return FAILED;
  526. }
  527. mp_req->resp_buf = dma_alloc_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
  528. &mp_req->resp_buf_dma,
  529. GFP_ATOMIC);
  530. if (!mp_req->resp_buf) {
  531. printk(KERN_ERR PFX "unable to alloc TM resp buffer\n");
  532. bnx2fc_free_mp_resc(io_req);
  533. return FAILED;
  534. }
  535. memset(mp_req->req_buf, 0, CNIC_PAGE_SIZE);
  536. memset(mp_req->resp_buf, 0, CNIC_PAGE_SIZE);
  537. /* Allocate and map mp_req_bd and mp_resp_bd */
  538. sz = sizeof(struct fcoe_bd_ctx);
  539. mp_req->mp_req_bd = dma_alloc_coherent(&hba->pcidev->dev, sz,
  540. &mp_req->mp_req_bd_dma,
  541. GFP_ATOMIC);
  542. if (!mp_req->mp_req_bd) {
  543. printk(KERN_ERR PFX "unable to alloc MP req bd\n");
  544. bnx2fc_free_mp_resc(io_req);
  545. return FAILED;
  546. }
  547. mp_req->mp_resp_bd = dma_alloc_coherent(&hba->pcidev->dev, sz,
  548. &mp_req->mp_resp_bd_dma,
  549. GFP_ATOMIC);
  550. if (!mp_req->mp_resp_bd) {
  551. printk(KERN_ERR PFX "unable to alloc MP resp bd\n");
  552. bnx2fc_free_mp_resc(io_req);
  553. return FAILED;
  554. }
  555. /* Fill bd table */
  556. addr = mp_req->req_buf_dma;
  557. mp_req_bd = mp_req->mp_req_bd;
  558. mp_req_bd->buf_addr_lo = (u32)addr & 0xffffffff;
  559. mp_req_bd->buf_addr_hi = (u32)((u64)addr >> 32);
  560. mp_req_bd->buf_len = CNIC_PAGE_SIZE;
  561. mp_req_bd->flags = 0;
  562. /*
  563. * MP buffer is either a task mgmt command or an ELS.
  564. * So the assumption is that it consumes a single bd
  565. * entry in the bd table
  566. */
  567. mp_resp_bd = mp_req->mp_resp_bd;
  568. addr = mp_req->resp_buf_dma;
  569. mp_resp_bd->buf_addr_lo = (u32)addr & 0xffffffff;
  570. mp_resp_bd->buf_addr_hi = (u32)((u64)addr >> 32);
  571. mp_resp_bd->buf_len = CNIC_PAGE_SIZE;
  572. mp_resp_bd->flags = 0;
  573. return SUCCESS;
  574. }
  575. static int bnx2fc_initiate_tmf(struct fc_lport *lport, struct fc_rport *rport,
  576. u64 tm_lun, u8 tm_flags)
  577. {
  578. struct fc_rport_libfc_priv *rp;
  579. struct fcoe_port *port;
  580. struct bnx2fc_interface *interface;
  581. struct bnx2fc_rport *tgt;
  582. struct bnx2fc_cmd *io_req;
  583. struct bnx2fc_mp_req *tm_req;
  584. struct fcoe_task_ctx_entry *task;
  585. struct fcoe_task_ctx_entry *task_page;
  586. struct fc_frame_header *fc_hdr;
  587. struct fcp_cmnd *fcp_cmnd;
  588. int task_idx, index;
  589. int rc = SUCCESS;
  590. u16 xid;
  591. u32 sid, did;
  592. unsigned long start = jiffies;
  593. port = lport_priv(lport);
  594. interface = port->priv;
  595. if (rport == NULL) {
  596. printk(KERN_ERR PFX "device_reset: rport is NULL\n");
  597. rc = FAILED;
  598. goto tmf_err;
  599. }
  600. rp = rport->dd_data;
  601. rc = fc_block_rport(rport);
  602. if (rc)
  603. return rc;
  604. if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
  605. printk(KERN_ERR PFX "device_reset: link is not ready\n");
  606. rc = FAILED;
  607. goto tmf_err;
  608. }
  609. /* rport and tgt are allocated together, so tgt should be non-NULL */
  610. tgt = (struct bnx2fc_rport *)&rp[1];
  611. if (!(test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
  612. printk(KERN_ERR PFX "device_reset: tgt not offloaded\n");
  613. rc = FAILED;
  614. goto tmf_err;
  615. }
  616. retry_tmf:
  617. io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_TASK_MGMT_CMD);
  618. if (!io_req) {
  619. if (time_after(jiffies, start + HZ)) {
  620. printk(KERN_ERR PFX "tmf: Failed TMF");
  621. rc = FAILED;
  622. goto tmf_err;
  623. }
  624. msleep(20);
  625. goto retry_tmf;
  626. }
  627. /* Initialize rest of io_req fields */
  628. io_req->sc_cmd = NULL;
  629. io_req->port = port;
  630. io_req->tgt = tgt;
  631. tm_req = (struct bnx2fc_mp_req *)&(io_req->mp_req);
  632. rc = bnx2fc_init_mp_req(io_req);
  633. if (rc == FAILED) {
  634. printk(KERN_ERR PFX "Task mgmt MP request init failed\n");
  635. spin_lock_bh(&tgt->tgt_lock);
  636. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  637. spin_unlock_bh(&tgt->tgt_lock);
  638. goto tmf_err;
  639. }
  640. /* Set TM flags */
  641. io_req->io_req_flags = 0;
  642. tm_req->tm_flags = tm_flags;
  643. tm_req->tm_lun = tm_lun;
  644. /* Fill FCP_CMND */
  645. bnx2fc_build_fcp_cmnd(io_req, (struct fcp_cmnd *)tm_req->req_buf);
  646. fcp_cmnd = (struct fcp_cmnd *)tm_req->req_buf;
  647. int_to_scsilun(tm_lun, &fcp_cmnd->fc_lun);
  648. memset(fcp_cmnd->fc_cdb, 0, BNX2FC_MAX_CMD_LEN);
  649. fcp_cmnd->fc_dl = 0;
  650. /* Fill FC header */
  651. fc_hdr = &(tm_req->req_fc_hdr);
  652. sid = tgt->sid;
  653. did = rport->port_id;
  654. __fc_fill_fc_hdr(fc_hdr, FC_RCTL_DD_UNSOL_CMD, did, sid,
  655. FC_TYPE_FCP, FC_FC_FIRST_SEQ | FC_FC_END_SEQ |
  656. FC_FC_SEQ_INIT, 0);
  657. /* Obtain exchange id */
  658. xid = io_req->xid;
  659. BNX2FC_TGT_DBG(tgt, "Initiate TMF - xid = 0x%x\n", xid);
  660. task_idx = xid/BNX2FC_TASKS_PER_PAGE;
  661. index = xid % BNX2FC_TASKS_PER_PAGE;
  662. /* Initialize task context for this IO request */
  663. task_page = (struct fcoe_task_ctx_entry *)
  664. interface->hba->task_ctx[task_idx];
  665. task = &(task_page[index]);
  666. bnx2fc_init_mp_task(io_req, task);
  667. /* Obtain free SQ entry */
  668. spin_lock_bh(&tgt->tgt_lock);
  669. bnx2fc_add_2_sq(tgt, xid);
  670. /* Enqueue the io_req to active_tm_queue */
  671. io_req->on_tmf_queue = 1;
  672. list_add_tail(&io_req->link, &tgt->active_tm_queue);
  673. init_completion(&io_req->abts_done);
  674. io_req->wait_for_abts_comp = 1;
  675. /* Ring doorbell */
  676. bnx2fc_ring_doorbell(tgt);
  677. spin_unlock_bh(&tgt->tgt_lock);
  678. rc = wait_for_completion_timeout(&io_req->abts_done,
  679. interface->tm_timeout * HZ);
  680. spin_lock_bh(&tgt->tgt_lock);
  681. io_req->wait_for_abts_comp = 0;
  682. if (!(test_bit(BNX2FC_FLAG_TM_COMPL, &io_req->req_flags))) {
  683. set_bit(BNX2FC_FLAG_TM_TIMEOUT, &io_req->req_flags);
  684. if (io_req->on_tmf_queue) {
  685. list_del_init(&io_req->link);
  686. io_req->on_tmf_queue = 0;
  687. }
  688. io_req->wait_for_cleanup_comp = 1;
  689. init_completion(&io_req->cleanup_done);
  690. bnx2fc_initiate_cleanup(io_req);
  691. spin_unlock_bh(&tgt->tgt_lock);
  692. rc = wait_for_completion_timeout(&io_req->cleanup_done,
  693. BNX2FC_FW_TIMEOUT);
  694. spin_lock_bh(&tgt->tgt_lock);
  695. io_req->wait_for_cleanup_comp = 0;
  696. if (!rc)
  697. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  698. }
  699. spin_unlock_bh(&tgt->tgt_lock);
  700. if (!rc) {
  701. BNX2FC_TGT_DBG(tgt, "task mgmt command failed...\n");
  702. rc = FAILED;
  703. } else {
  704. BNX2FC_TGT_DBG(tgt, "task mgmt command success...\n");
  705. rc = SUCCESS;
  706. }
  707. tmf_err:
  708. return rc;
  709. }
  710. int bnx2fc_initiate_abts(struct bnx2fc_cmd *io_req)
  711. {
  712. struct fc_lport *lport;
  713. struct bnx2fc_rport *tgt = io_req->tgt;
  714. struct fc_rport *rport = tgt->rport;
  715. struct fc_rport_priv *rdata = tgt->rdata;
  716. struct bnx2fc_interface *interface;
  717. struct fcoe_port *port;
  718. struct bnx2fc_cmd *abts_io_req;
  719. struct fcoe_task_ctx_entry *task;
  720. struct fcoe_task_ctx_entry *task_page;
  721. struct fc_frame_header *fc_hdr;
  722. struct bnx2fc_mp_req *abts_req;
  723. int task_idx, index;
  724. u32 sid, did;
  725. u16 xid;
  726. int rc = SUCCESS;
  727. u32 r_a_tov = rdata->r_a_tov;
  728. /* called with tgt_lock held */
  729. BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_abts\n");
  730. port = io_req->port;
  731. interface = port->priv;
  732. lport = port->lport;
  733. if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
  734. printk(KERN_ERR PFX "initiate_abts: tgt not offloaded\n");
  735. rc = FAILED;
  736. goto abts_err;
  737. }
  738. if (rport == NULL) {
  739. printk(KERN_ERR PFX "initiate_abts: rport is NULL\n");
  740. rc = FAILED;
  741. goto abts_err;
  742. }
  743. if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
  744. printk(KERN_ERR PFX "initiate_abts: link is not ready\n");
  745. rc = FAILED;
  746. goto abts_err;
  747. }
  748. abts_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_ABTS);
  749. if (!abts_io_req) {
  750. printk(KERN_ERR PFX "abts: couldn't allocate cmd\n");
  751. rc = FAILED;
  752. goto abts_err;
  753. }
  754. /* Initialize rest of io_req fields */
  755. abts_io_req->sc_cmd = NULL;
  756. abts_io_req->port = port;
  757. abts_io_req->tgt = tgt;
  758. abts_io_req->data_xfer_len = 0; /* No data transfer for ABTS */
  759. abts_req = (struct bnx2fc_mp_req *)&(abts_io_req->mp_req);
  760. memset(abts_req, 0, sizeof(struct bnx2fc_mp_req));
  761. /* Fill FC header */
  762. fc_hdr = &(abts_req->req_fc_hdr);
  763. /* Obtain oxid and rxid for the original exchange to be aborted */
  764. fc_hdr->fh_ox_id = htons(io_req->xid);
  765. fc_hdr->fh_rx_id = htons(io_req->task->rxwr_txrd.var_ctx.rx_id);
  766. sid = tgt->sid;
  767. did = rport->port_id;
  768. __fc_fill_fc_hdr(fc_hdr, FC_RCTL_BA_ABTS, did, sid,
  769. FC_TYPE_BLS, FC_FC_FIRST_SEQ | FC_FC_END_SEQ |
  770. FC_FC_SEQ_INIT, 0);
  771. xid = abts_io_req->xid;
  772. BNX2FC_IO_DBG(abts_io_req, "ABTS io_req\n");
  773. task_idx = xid/BNX2FC_TASKS_PER_PAGE;
  774. index = xid % BNX2FC_TASKS_PER_PAGE;
  775. /* Initialize task context for this IO request */
  776. task_page = (struct fcoe_task_ctx_entry *)
  777. interface->hba->task_ctx[task_idx];
  778. task = &(task_page[index]);
  779. bnx2fc_init_mp_task(abts_io_req, task);
  780. /*
  781. * ABTS task is a temporary task that will be cleaned up
  782. * irrespective of ABTS response. We need to start the timer
  783. * for the original exchange, as the CQE is posted for the original
  784. * IO request.
  785. *
  786. * Timer for ABTS is started only when it is originated by a
  787. * TM request. For the ABTS issued as part of ULP timeout,
  788. * scsi-ml maintains the timers.
  789. */
  790. /* if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags))*/
  791. bnx2fc_cmd_timer_set(io_req, 2 * r_a_tov);
  792. /* Obtain free SQ entry */
  793. bnx2fc_add_2_sq(tgt, xid);
  794. /* Ring doorbell */
  795. bnx2fc_ring_doorbell(tgt);
  796. abts_err:
  797. return rc;
  798. }
  799. int bnx2fc_initiate_seq_cleanup(struct bnx2fc_cmd *orig_io_req, u32 offset,
  800. enum fc_rctl r_ctl)
  801. {
  802. struct bnx2fc_rport *tgt = orig_io_req->tgt;
  803. struct bnx2fc_interface *interface;
  804. struct fcoe_port *port;
  805. struct bnx2fc_cmd *seq_clnp_req;
  806. struct fcoe_task_ctx_entry *task;
  807. struct fcoe_task_ctx_entry *task_page;
  808. struct bnx2fc_els_cb_arg *cb_arg = NULL;
  809. int task_idx, index;
  810. u16 xid;
  811. int rc = 0;
  812. BNX2FC_IO_DBG(orig_io_req, "bnx2fc_initiate_seq_cleanup xid = 0x%x\n",
  813. orig_io_req->xid);
  814. kref_get(&orig_io_req->refcount);
  815. port = orig_io_req->port;
  816. interface = port->priv;
  817. cb_arg = kzalloc(sizeof(struct bnx2fc_els_cb_arg), GFP_ATOMIC);
  818. if (!cb_arg) {
  819. printk(KERN_ERR PFX "Unable to alloc cb_arg for seq clnup\n");
  820. rc = -ENOMEM;
  821. goto cleanup_err;
  822. }
  823. seq_clnp_req = bnx2fc_elstm_alloc(tgt, BNX2FC_SEQ_CLEANUP);
  824. if (!seq_clnp_req) {
  825. printk(KERN_ERR PFX "cleanup: couldn't allocate cmd\n");
  826. rc = -ENOMEM;
  827. kfree(cb_arg);
  828. goto cleanup_err;
  829. }
  830. /* Initialize rest of io_req fields */
  831. seq_clnp_req->sc_cmd = NULL;
  832. seq_clnp_req->port = port;
  833. seq_clnp_req->tgt = tgt;
  834. seq_clnp_req->data_xfer_len = 0; /* No data transfer for cleanup */
  835. xid = seq_clnp_req->xid;
  836. task_idx = xid/BNX2FC_TASKS_PER_PAGE;
  837. index = xid % BNX2FC_TASKS_PER_PAGE;
  838. /* Initialize task context for this IO request */
  839. task_page = (struct fcoe_task_ctx_entry *)
  840. interface->hba->task_ctx[task_idx];
  841. task = &(task_page[index]);
  842. cb_arg->aborted_io_req = orig_io_req;
  843. cb_arg->io_req = seq_clnp_req;
  844. cb_arg->r_ctl = r_ctl;
  845. cb_arg->offset = offset;
  846. seq_clnp_req->cb_arg = cb_arg;
  847. printk(KERN_ERR PFX "call init_seq_cleanup_task\n");
  848. bnx2fc_init_seq_cleanup_task(seq_clnp_req, task, orig_io_req, offset);
  849. /* Obtain free SQ entry */
  850. bnx2fc_add_2_sq(tgt, xid);
  851. /* Ring doorbell */
  852. bnx2fc_ring_doorbell(tgt);
  853. cleanup_err:
  854. return rc;
  855. }
  856. int bnx2fc_initiate_cleanup(struct bnx2fc_cmd *io_req)
  857. {
  858. struct bnx2fc_rport *tgt = io_req->tgt;
  859. struct bnx2fc_interface *interface;
  860. struct fcoe_port *port;
  861. struct bnx2fc_cmd *cleanup_io_req;
  862. struct fcoe_task_ctx_entry *task;
  863. struct fcoe_task_ctx_entry *task_page;
  864. int task_idx, index;
  865. u16 xid, orig_xid;
  866. int rc = 0;
  867. /* ASSUMPTION: called with tgt_lock held */
  868. BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_cleanup\n");
  869. port = io_req->port;
  870. interface = port->priv;
  871. cleanup_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_CLEANUP);
  872. if (!cleanup_io_req) {
  873. printk(KERN_ERR PFX "cleanup: couldn't allocate cmd\n");
  874. rc = -1;
  875. goto cleanup_err;
  876. }
  877. /* Initialize rest of io_req fields */
  878. cleanup_io_req->sc_cmd = NULL;
  879. cleanup_io_req->port = port;
  880. cleanup_io_req->tgt = tgt;
  881. cleanup_io_req->data_xfer_len = 0; /* No data transfer for cleanup */
  882. xid = cleanup_io_req->xid;
  883. task_idx = xid/BNX2FC_TASKS_PER_PAGE;
  884. index = xid % BNX2FC_TASKS_PER_PAGE;
  885. /* Initialize task context for this IO request */
  886. task_page = (struct fcoe_task_ctx_entry *)
  887. interface->hba->task_ctx[task_idx];
  888. task = &(task_page[index]);
  889. orig_xid = io_req->xid;
  890. BNX2FC_IO_DBG(io_req, "CLEANUP io_req xid = 0x%x\n", xid);
  891. bnx2fc_init_cleanup_task(cleanup_io_req, task, orig_xid);
  892. /* Obtain free SQ entry */
  893. bnx2fc_add_2_sq(tgt, xid);
  894. /* Set flag that cleanup request is pending with the firmware */
  895. set_bit(BNX2FC_FLAG_ISSUE_CLEANUP_REQ, &io_req->req_flags);
  896. /* Ring doorbell */
  897. bnx2fc_ring_doorbell(tgt);
  898. cleanup_err:
  899. return rc;
  900. }
  901. /**
  902. * bnx2fc_eh_target_reset: Reset a target
  903. *
  904. * @sc_cmd: SCSI command
  905. *
  906. * Set from SCSI host template to send task mgmt command to the target
  907. * and wait for the response
  908. */
  909. int bnx2fc_eh_target_reset(struct scsi_cmnd *sc_cmd)
  910. {
  911. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  912. struct fc_lport *lport = shost_priv(rport_to_shost(rport));
  913. return bnx2fc_initiate_tmf(lport, rport, 0, FCP_TMF_TGT_RESET);
  914. }
  915. /**
  916. * bnx2fc_eh_device_reset - Reset a single LUN
  917. *
  918. * @sc_cmd: SCSI command
  919. *
  920. * Set from SCSI host template to send task mgmt command to the target
  921. * and wait for the response
  922. */
  923. int bnx2fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
  924. {
  925. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  926. struct fc_lport *lport = shost_priv(rport_to_shost(rport));
  927. return bnx2fc_initiate_tmf(lport, rport, sc_cmd->device->lun,
  928. FCP_TMF_LUN_RESET);
  929. }
  930. static int bnx2fc_abts_cleanup(struct bnx2fc_cmd *io_req)
  931. __must_hold(&tgt->tgt_lock)
  932. {
  933. struct bnx2fc_rport *tgt = io_req->tgt;
  934. unsigned int time_left;
  935. init_completion(&io_req->cleanup_done);
  936. io_req->wait_for_cleanup_comp = 1;
  937. bnx2fc_initiate_cleanup(io_req);
  938. spin_unlock_bh(&tgt->tgt_lock);
  939. /*
  940. * Can't wait forever on cleanup response lest we let the SCSI error
  941. * handler wait forever
  942. */
  943. time_left = wait_for_completion_timeout(&io_req->cleanup_done,
  944. BNX2FC_FW_TIMEOUT);
  945. if (!time_left) {
  946. BNX2FC_IO_DBG(io_req, "%s(): Wait for cleanup timed out.\n",
  947. __func__);
  948. /*
  949. * Put the extra reference to the SCSI command since it would
  950. * not have been returned in this case.
  951. */
  952. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  953. }
  954. spin_lock_bh(&tgt->tgt_lock);
  955. io_req->wait_for_cleanup_comp = 0;
  956. return SUCCESS;
  957. }
  958. /**
  959. * bnx2fc_eh_abort - eh_abort_handler api to abort an outstanding
  960. * SCSI command
  961. *
  962. * @sc_cmd: SCSI_ML command pointer
  963. *
  964. * SCSI abort request handler
  965. */
  966. int bnx2fc_eh_abort(struct scsi_cmnd *sc_cmd)
  967. {
  968. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  969. struct fc_rport_libfc_priv *rp = rport->dd_data;
  970. struct bnx2fc_cmd *io_req;
  971. struct fc_lport *lport;
  972. struct bnx2fc_rport *tgt;
  973. int rc;
  974. unsigned int time_left;
  975. rc = fc_block_scsi_eh(sc_cmd);
  976. if (rc)
  977. return rc;
  978. lport = shost_priv(sc_cmd->device->host);
  979. if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
  980. printk(KERN_ERR PFX "eh_abort: link not ready\n");
  981. return FAILED;
  982. }
  983. tgt = (struct bnx2fc_rport *)&rp[1];
  984. BNX2FC_TGT_DBG(tgt, "Entered bnx2fc_eh_abort\n");
  985. spin_lock_bh(&tgt->tgt_lock);
  986. io_req = bnx2fc_priv(sc_cmd)->io_req;
  987. if (!io_req) {
  988. /* Command might have just completed */
  989. printk(KERN_ERR PFX "eh_abort: io_req is NULL\n");
  990. spin_unlock_bh(&tgt->tgt_lock);
  991. return SUCCESS;
  992. }
  993. BNX2FC_IO_DBG(io_req, "eh_abort - refcnt = %d\n",
  994. kref_read(&io_req->refcount));
  995. /* Hold IO request across abort processing */
  996. kref_get(&io_req->refcount);
  997. BUG_ON(tgt != io_req->tgt);
  998. /* Remove the io_req from the active_q. */
  999. /*
  1000. * Task Mgmt functions (LUN RESET & TGT RESET) will not
  1001. * issue an ABTS on this particular IO req, as the
  1002. * io_req is no longer in the active_q.
  1003. */
  1004. if (tgt->flush_in_prog) {
  1005. printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
  1006. "flush in progress\n", io_req->xid);
  1007. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1008. spin_unlock_bh(&tgt->tgt_lock);
  1009. return SUCCESS;
  1010. }
  1011. if (io_req->on_active_queue == 0) {
  1012. printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
  1013. "not on active_q\n", io_req->xid);
  1014. /*
  1015. * The IO is still with the FW.
  1016. * Return failure and let SCSI-ml retry eh_abort.
  1017. */
  1018. spin_unlock_bh(&tgt->tgt_lock);
  1019. return FAILED;
  1020. }
  1021. /*
  1022. * Only eh_abort processing will remove the IO from
  1023. * active_cmd_q before processing the request. this is
  1024. * done to avoid race conditions between IOs aborted
  1025. * as part of task management completion and eh_abort
  1026. * processing
  1027. */
  1028. list_del_init(&io_req->link);
  1029. io_req->on_active_queue = 0;
  1030. /* Move IO req to retire queue */
  1031. list_add_tail(&io_req->link, &tgt->io_retire_queue);
  1032. init_completion(&io_req->abts_done);
  1033. init_completion(&io_req->cleanup_done);
  1034. if (test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags)) {
  1035. printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
  1036. "already in abts processing\n", io_req->xid);
  1037. if (cancel_delayed_work(&io_req->timeout_work))
  1038. kref_put(&io_req->refcount,
  1039. bnx2fc_cmd_release); /* drop timer hold */
  1040. /*
  1041. * We don't want to hold off the upper layer timer so simply
  1042. * cleanup the command and return that I/O was successfully
  1043. * aborted.
  1044. */
  1045. bnx2fc_abts_cleanup(io_req);
  1046. /* This only occurs when an task abort was requested while ABTS
  1047. is in progress. Setting the IO_CLEANUP flag will skip the
  1048. RRQ process in the case when the fw generated SCSI_CMD cmpl
  1049. was a result from the ABTS request rather than the CLEANUP
  1050. request */
  1051. set_bit(BNX2FC_FLAG_IO_CLEANUP, &io_req->req_flags);
  1052. rc = FAILED;
  1053. goto done;
  1054. }
  1055. /* Cancel the current timer running on this io_req */
  1056. if (cancel_delayed_work(&io_req->timeout_work))
  1057. kref_put(&io_req->refcount,
  1058. bnx2fc_cmd_release); /* drop timer hold */
  1059. set_bit(BNX2FC_FLAG_EH_ABORT, &io_req->req_flags);
  1060. io_req->wait_for_abts_comp = 1;
  1061. rc = bnx2fc_initiate_abts(io_req);
  1062. if (rc == FAILED) {
  1063. io_req->wait_for_cleanup_comp = 1;
  1064. bnx2fc_initiate_cleanup(io_req);
  1065. spin_unlock_bh(&tgt->tgt_lock);
  1066. wait_for_completion(&io_req->cleanup_done);
  1067. spin_lock_bh(&tgt->tgt_lock);
  1068. io_req->wait_for_cleanup_comp = 0;
  1069. goto done;
  1070. }
  1071. spin_unlock_bh(&tgt->tgt_lock);
  1072. /* Wait 2 * RA_TOV + 1 to be sure timeout function hasn't fired */
  1073. time_left = wait_for_completion_timeout(&io_req->abts_done,
  1074. msecs_to_jiffies(2 * rp->r_a_tov + 1));
  1075. if (time_left)
  1076. BNX2FC_IO_DBG(io_req,
  1077. "Timed out in eh_abort waiting for abts_done");
  1078. spin_lock_bh(&tgt->tgt_lock);
  1079. io_req->wait_for_abts_comp = 0;
  1080. if (test_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags)) {
  1081. BNX2FC_IO_DBG(io_req, "IO completed in a different context\n");
  1082. rc = SUCCESS;
  1083. } else if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
  1084. &io_req->req_flags))) {
  1085. /* Let the scsi-ml try to recover this command */
  1086. printk(KERN_ERR PFX "abort failed, xid = 0x%x\n",
  1087. io_req->xid);
  1088. /*
  1089. * Cleanup firmware residuals before returning control back
  1090. * to SCSI ML.
  1091. */
  1092. rc = bnx2fc_abts_cleanup(io_req);
  1093. goto done;
  1094. } else {
  1095. /*
  1096. * We come here even when there was a race condition
  1097. * between timeout and abts completion, and abts
  1098. * completion happens just in time.
  1099. */
  1100. BNX2FC_IO_DBG(io_req, "abort succeeded\n");
  1101. rc = SUCCESS;
  1102. bnx2fc_scsi_done(io_req, DID_ABORT);
  1103. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1104. }
  1105. done:
  1106. /* release the reference taken in eh_abort */
  1107. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1108. spin_unlock_bh(&tgt->tgt_lock);
  1109. return rc;
  1110. }
  1111. void bnx2fc_process_seq_cleanup_compl(struct bnx2fc_cmd *seq_clnp_req,
  1112. struct fcoe_task_ctx_entry *task,
  1113. u8 rx_state)
  1114. {
  1115. struct bnx2fc_els_cb_arg *cb_arg = seq_clnp_req->cb_arg;
  1116. struct bnx2fc_cmd *orig_io_req = cb_arg->aborted_io_req;
  1117. u32 offset = cb_arg->offset;
  1118. enum fc_rctl r_ctl = cb_arg->r_ctl;
  1119. int rc = 0;
  1120. struct bnx2fc_rport *tgt = orig_io_req->tgt;
  1121. BNX2FC_IO_DBG(orig_io_req, "Entered process_cleanup_compl xid = 0x%x"
  1122. "cmd_type = %d\n",
  1123. seq_clnp_req->xid, seq_clnp_req->cmd_type);
  1124. if (rx_state == FCOE_TASK_RX_STATE_IGNORED_SEQUENCE_CLEANUP) {
  1125. printk(KERN_ERR PFX "seq cleanup ignored - xid = 0x%x\n",
  1126. seq_clnp_req->xid);
  1127. goto free_cb_arg;
  1128. }
  1129. spin_unlock_bh(&tgt->tgt_lock);
  1130. rc = bnx2fc_send_srr(orig_io_req, offset, r_ctl);
  1131. spin_lock_bh(&tgt->tgt_lock);
  1132. if (rc)
  1133. printk(KERN_ERR PFX "clnup_compl: Unable to send SRR"
  1134. " IO will abort\n");
  1135. seq_clnp_req->cb_arg = NULL;
  1136. kref_put(&orig_io_req->refcount, bnx2fc_cmd_release);
  1137. free_cb_arg:
  1138. kfree(cb_arg);
  1139. return;
  1140. }
  1141. void bnx2fc_process_cleanup_compl(struct bnx2fc_cmd *io_req,
  1142. struct fcoe_task_ctx_entry *task,
  1143. u8 num_rq)
  1144. {
  1145. BNX2FC_IO_DBG(io_req, "Entered process_cleanup_compl "
  1146. "refcnt = %d, cmd_type = %d\n",
  1147. kref_read(&io_req->refcount), io_req->cmd_type);
  1148. /*
  1149. * Test whether there is a cleanup request pending. If not just
  1150. * exit.
  1151. */
  1152. if (!test_and_clear_bit(BNX2FC_FLAG_ISSUE_CLEANUP_REQ,
  1153. &io_req->req_flags))
  1154. return;
  1155. /*
  1156. * If we receive a cleanup completion for this request then the
  1157. * firmware will not give us an abort completion for this request
  1158. * so clear any ABTS pending flags.
  1159. */
  1160. if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags) &&
  1161. !test_bit(BNX2FC_FLAG_ABTS_DONE, &io_req->req_flags)) {
  1162. set_bit(BNX2FC_FLAG_ABTS_DONE, &io_req->req_flags);
  1163. if (io_req->wait_for_abts_comp)
  1164. complete(&io_req->abts_done);
  1165. }
  1166. bnx2fc_scsi_done(io_req, DID_ERROR);
  1167. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1168. if (io_req->wait_for_cleanup_comp)
  1169. complete(&io_req->cleanup_done);
  1170. }
  1171. void bnx2fc_process_abts_compl(struct bnx2fc_cmd *io_req,
  1172. struct fcoe_task_ctx_entry *task,
  1173. u8 num_rq)
  1174. {
  1175. u32 r_ctl;
  1176. u32 r_a_tov = FC_DEF_R_A_TOV;
  1177. u8 issue_rrq = 0;
  1178. struct bnx2fc_rport *tgt = io_req->tgt;
  1179. BNX2FC_IO_DBG(io_req, "Entered process_abts_compl xid = 0x%x"
  1180. "refcnt = %d, cmd_type = %d\n",
  1181. io_req->xid,
  1182. kref_read(&io_req->refcount), io_req->cmd_type);
  1183. if (test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
  1184. &io_req->req_flags)) {
  1185. BNX2FC_IO_DBG(io_req, "Timer context finished processing"
  1186. " this io\n");
  1187. return;
  1188. }
  1189. /*
  1190. * If we receive an ABTS completion here then we will not receive
  1191. * a cleanup completion so clear any cleanup pending flags.
  1192. */
  1193. if (test_bit(BNX2FC_FLAG_ISSUE_CLEANUP_REQ, &io_req->req_flags)) {
  1194. clear_bit(BNX2FC_FLAG_ISSUE_CLEANUP_REQ, &io_req->req_flags);
  1195. if (io_req->wait_for_cleanup_comp)
  1196. complete(&io_req->cleanup_done);
  1197. }
  1198. /* Do not issue RRQ as this IO is already cleanedup */
  1199. if (test_and_set_bit(BNX2FC_FLAG_IO_CLEANUP,
  1200. &io_req->req_flags))
  1201. goto io_compl;
  1202. /*
  1203. * For ABTS issued due to SCSI eh_abort_handler, timeout
  1204. * values are maintained by scsi-ml itself. Cancel timeout
  1205. * in case ABTS issued as part of task management function
  1206. * or due to FW error.
  1207. */
  1208. if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags))
  1209. if (cancel_delayed_work(&io_req->timeout_work))
  1210. kref_put(&io_req->refcount,
  1211. bnx2fc_cmd_release); /* drop timer hold */
  1212. r_ctl = (u8)task->rxwr_only.union_ctx.comp_info.abts_rsp.r_ctl;
  1213. switch (r_ctl) {
  1214. case FC_RCTL_BA_ACC:
  1215. /*
  1216. * Dont release this cmd yet. It will be relesed
  1217. * after we get RRQ response
  1218. */
  1219. BNX2FC_IO_DBG(io_req, "ABTS response - ACC Send RRQ\n");
  1220. issue_rrq = 1;
  1221. break;
  1222. case FC_RCTL_BA_RJT:
  1223. BNX2FC_IO_DBG(io_req, "ABTS response - RJT\n");
  1224. break;
  1225. default:
  1226. printk(KERN_ERR PFX "Unknown ABTS response\n");
  1227. break;
  1228. }
  1229. if (issue_rrq) {
  1230. BNX2FC_IO_DBG(io_req, "Issue RRQ after R_A_TOV\n");
  1231. set_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags);
  1232. }
  1233. set_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags);
  1234. bnx2fc_cmd_timer_set(io_req, r_a_tov);
  1235. io_compl:
  1236. if (io_req->wait_for_abts_comp) {
  1237. if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
  1238. &io_req->req_flags))
  1239. complete(&io_req->abts_done);
  1240. } else {
  1241. /*
  1242. * We end up here when ABTS is issued as
  1243. * in asynchronous context, i.e., as part
  1244. * of task management completion, or
  1245. * when FW error is received or when the
  1246. * ABTS is issued when the IO is timed
  1247. * out.
  1248. */
  1249. if (io_req->on_active_queue) {
  1250. list_del_init(&io_req->link);
  1251. io_req->on_active_queue = 0;
  1252. /* Move IO req to retire queue */
  1253. list_add_tail(&io_req->link, &tgt->io_retire_queue);
  1254. }
  1255. bnx2fc_scsi_done(io_req, DID_ERROR);
  1256. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1257. }
  1258. }
  1259. static void bnx2fc_lun_reset_cmpl(struct bnx2fc_cmd *io_req)
  1260. {
  1261. struct bnx2fc_rport *tgt = io_req->tgt;
  1262. struct bnx2fc_cmd *cmd, *tmp;
  1263. struct bnx2fc_mp_req *tm_req = &io_req->mp_req;
  1264. u64 lun;
  1265. int rc = 0;
  1266. /* called with tgt_lock held */
  1267. BNX2FC_IO_DBG(io_req, "Entered bnx2fc_lun_reset_cmpl\n");
  1268. /*
  1269. * Walk thru the active_ios queue and ABORT the IO
  1270. * that matches with the LUN that was reset
  1271. */
  1272. list_for_each_entry_safe(cmd, tmp, &tgt->active_cmd_queue, link) {
  1273. BNX2FC_TGT_DBG(tgt, "LUN RST cmpl: scan for pending IOs\n");
  1274. if (!cmd->sc_cmd)
  1275. continue;
  1276. lun = cmd->sc_cmd->device->lun;
  1277. if (lun == tm_req->tm_lun) {
  1278. /* Initiate ABTS on this cmd */
  1279. if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
  1280. &cmd->req_flags)) {
  1281. /* cancel the IO timeout */
  1282. if (cancel_delayed_work(&io_req->timeout_work))
  1283. kref_put(&io_req->refcount,
  1284. bnx2fc_cmd_release);
  1285. /* timer hold */
  1286. rc = bnx2fc_initiate_abts(cmd);
  1287. /* abts shouldn't fail in this context */
  1288. WARN_ON(rc != SUCCESS);
  1289. } else
  1290. printk(KERN_ERR PFX "lun_rst: abts already in"
  1291. " progress for this IO 0x%x\n",
  1292. cmd->xid);
  1293. }
  1294. }
  1295. }
  1296. static void bnx2fc_tgt_reset_cmpl(struct bnx2fc_cmd *io_req)
  1297. {
  1298. struct bnx2fc_rport *tgt = io_req->tgt;
  1299. struct bnx2fc_cmd *cmd, *tmp;
  1300. int rc = 0;
  1301. /* called with tgt_lock held */
  1302. BNX2FC_IO_DBG(io_req, "Entered bnx2fc_tgt_reset_cmpl\n");
  1303. /*
  1304. * Walk thru the active_ios queue and ABORT the IO
  1305. * that matches with the LUN that was reset
  1306. */
  1307. list_for_each_entry_safe(cmd, tmp, &tgt->active_cmd_queue, link) {
  1308. BNX2FC_TGT_DBG(tgt, "TGT RST cmpl: scan for pending IOs\n");
  1309. /* Initiate ABTS */
  1310. if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
  1311. &cmd->req_flags)) {
  1312. /* cancel the IO timeout */
  1313. if (cancel_delayed_work(&io_req->timeout_work))
  1314. kref_put(&io_req->refcount,
  1315. bnx2fc_cmd_release); /* timer hold */
  1316. rc = bnx2fc_initiate_abts(cmd);
  1317. /* abts shouldn't fail in this context */
  1318. WARN_ON(rc != SUCCESS);
  1319. } else
  1320. printk(KERN_ERR PFX "tgt_rst: abts already in progress"
  1321. " for this IO 0x%x\n", cmd->xid);
  1322. }
  1323. }
  1324. void bnx2fc_process_tm_compl(struct bnx2fc_cmd *io_req,
  1325. struct fcoe_task_ctx_entry *task, u8 num_rq,
  1326. unsigned char *rq_data)
  1327. {
  1328. struct bnx2fc_mp_req *tm_req;
  1329. struct fc_frame_header *fc_hdr;
  1330. struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
  1331. u64 *hdr;
  1332. u64 *temp_hdr;
  1333. void *rsp_buf;
  1334. /* Called with tgt_lock held */
  1335. BNX2FC_IO_DBG(io_req, "Entered process_tm_compl\n");
  1336. if (!(test_bit(BNX2FC_FLAG_TM_TIMEOUT, &io_req->req_flags)))
  1337. set_bit(BNX2FC_FLAG_TM_COMPL, &io_req->req_flags);
  1338. else {
  1339. /* TM has already timed out and we got
  1340. * delayed completion. Ignore completion
  1341. * processing.
  1342. */
  1343. return;
  1344. }
  1345. tm_req = &(io_req->mp_req);
  1346. fc_hdr = &(tm_req->resp_fc_hdr);
  1347. hdr = (u64 *)fc_hdr;
  1348. temp_hdr = (u64 *)
  1349. &task->rxwr_only.union_ctx.comp_info.mp_rsp.fc_hdr;
  1350. hdr[0] = cpu_to_be64(temp_hdr[0]);
  1351. hdr[1] = cpu_to_be64(temp_hdr[1]);
  1352. hdr[2] = cpu_to_be64(temp_hdr[2]);
  1353. tm_req->resp_len =
  1354. task->rxwr_only.union_ctx.comp_info.mp_rsp.mp_payload_len;
  1355. rsp_buf = tm_req->resp_buf;
  1356. if (fc_hdr->fh_r_ctl == FC_RCTL_DD_CMD_STATUS) {
  1357. bnx2fc_parse_fcp_rsp(io_req,
  1358. (struct fcoe_fcp_rsp_payload *)
  1359. rsp_buf, num_rq, rq_data);
  1360. if (io_req->fcp_rsp_code == 0) {
  1361. /* TM successful */
  1362. if (tm_req->tm_flags & FCP_TMF_LUN_RESET)
  1363. bnx2fc_lun_reset_cmpl(io_req);
  1364. else if (tm_req->tm_flags & FCP_TMF_TGT_RESET)
  1365. bnx2fc_tgt_reset_cmpl(io_req);
  1366. }
  1367. } else {
  1368. printk(KERN_ERR PFX "tmf's fc_hdr r_ctl = 0x%x\n",
  1369. fc_hdr->fh_r_ctl);
  1370. }
  1371. if (sc_cmd) {
  1372. if (!bnx2fc_priv(sc_cmd)->io_req) {
  1373. printk(KERN_ERR PFX "tm_compl: io_req is NULL\n");
  1374. return;
  1375. }
  1376. switch (io_req->fcp_status) {
  1377. case FC_GOOD:
  1378. if (io_req->cdb_status == 0) {
  1379. /* Good IO completion */
  1380. sc_cmd->result = DID_OK << 16;
  1381. } else {
  1382. /* Transport status is good, SCSI status not good */
  1383. sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
  1384. }
  1385. if (io_req->fcp_resid)
  1386. scsi_set_resid(sc_cmd, io_req->fcp_resid);
  1387. break;
  1388. default:
  1389. BNX2FC_IO_DBG(io_req, "process_tm_compl: fcp_status = %d\n",
  1390. io_req->fcp_status);
  1391. break;
  1392. }
  1393. sc_cmd = io_req->sc_cmd;
  1394. io_req->sc_cmd = NULL;
  1395. bnx2fc_priv(sc_cmd)->io_req = NULL;
  1396. scsi_done(sc_cmd);
  1397. }
  1398. /* check if the io_req exists in tgt's tmf_q */
  1399. if (io_req->on_tmf_queue) {
  1400. list_del_init(&io_req->link);
  1401. io_req->on_tmf_queue = 0;
  1402. } else {
  1403. printk(KERN_ERR PFX "Command not on active_cmd_queue!\n");
  1404. return;
  1405. }
  1406. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1407. if (io_req->wait_for_abts_comp) {
  1408. BNX2FC_IO_DBG(io_req, "tm_compl - wake up the waiter\n");
  1409. complete(&io_req->abts_done);
  1410. }
  1411. }
  1412. static int bnx2fc_split_bd(struct bnx2fc_cmd *io_req, u64 addr, int sg_len,
  1413. int bd_index)
  1414. {
  1415. struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
  1416. int frag_size, sg_frags;
  1417. sg_frags = 0;
  1418. while (sg_len) {
  1419. if (sg_len >= BNX2FC_BD_SPLIT_SZ)
  1420. frag_size = BNX2FC_BD_SPLIT_SZ;
  1421. else
  1422. frag_size = sg_len;
  1423. bd[bd_index + sg_frags].buf_addr_lo = addr & 0xffffffff;
  1424. bd[bd_index + sg_frags].buf_addr_hi = addr >> 32;
  1425. bd[bd_index + sg_frags].buf_len = (u16)frag_size;
  1426. bd[bd_index + sg_frags].flags = 0;
  1427. addr += (u64) frag_size;
  1428. sg_frags++;
  1429. sg_len -= frag_size;
  1430. }
  1431. return sg_frags;
  1432. }
  1433. static int bnx2fc_map_sg(struct bnx2fc_cmd *io_req)
  1434. {
  1435. struct bnx2fc_interface *interface = io_req->port->priv;
  1436. struct bnx2fc_hba *hba = interface->hba;
  1437. struct scsi_cmnd *sc = io_req->sc_cmd;
  1438. struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
  1439. struct scatterlist *sg;
  1440. int byte_count = 0;
  1441. int sg_count = 0;
  1442. int bd_count = 0;
  1443. int sg_frags;
  1444. unsigned int sg_len;
  1445. u64 addr;
  1446. int i;
  1447. WARN_ON(scsi_sg_count(sc) > BNX2FC_MAX_BDS_PER_CMD);
  1448. /*
  1449. * Use dma_map_sg directly to ensure we're using the correct
  1450. * dev struct off of pcidev.
  1451. */
  1452. sg_count = dma_map_sg(&hba->pcidev->dev, scsi_sglist(sc),
  1453. scsi_sg_count(sc), sc->sc_data_direction);
  1454. scsi_for_each_sg(sc, sg, sg_count, i) {
  1455. sg_len = sg_dma_len(sg);
  1456. addr = sg_dma_address(sg);
  1457. if (sg_len > BNX2FC_MAX_BD_LEN) {
  1458. sg_frags = bnx2fc_split_bd(io_req, addr, sg_len,
  1459. bd_count);
  1460. } else {
  1461. sg_frags = 1;
  1462. bd[bd_count].buf_addr_lo = addr & 0xffffffff;
  1463. bd[bd_count].buf_addr_hi = addr >> 32;
  1464. bd[bd_count].buf_len = (u16)sg_len;
  1465. bd[bd_count].flags = 0;
  1466. }
  1467. bd_count += sg_frags;
  1468. byte_count += sg_len;
  1469. }
  1470. if (byte_count != scsi_bufflen(sc))
  1471. printk(KERN_ERR PFX "byte_count = %d != scsi_bufflen = %d, "
  1472. "task_id = 0x%x\n", byte_count, scsi_bufflen(sc),
  1473. io_req->xid);
  1474. return bd_count;
  1475. }
  1476. static int bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd *io_req)
  1477. {
  1478. struct scsi_cmnd *sc = io_req->sc_cmd;
  1479. struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
  1480. int bd_count;
  1481. if (scsi_sg_count(sc)) {
  1482. bd_count = bnx2fc_map_sg(io_req);
  1483. if (bd_count == 0)
  1484. return -ENOMEM;
  1485. } else {
  1486. bd_count = 0;
  1487. bd[0].buf_addr_lo = bd[0].buf_addr_hi = 0;
  1488. bd[0].buf_len = bd[0].flags = 0;
  1489. }
  1490. io_req->bd_tbl->bd_valid = bd_count;
  1491. /*
  1492. * Return the command to ML if BD count exceeds the max number
  1493. * that can be handled by FW.
  1494. */
  1495. if (bd_count > BNX2FC_FW_MAX_BDS_PER_CMD) {
  1496. pr_err("bd_count = %d exceeded FW supported max BD(255), task_id = 0x%x\n",
  1497. bd_count, io_req->xid);
  1498. return -ENOMEM;
  1499. }
  1500. return 0;
  1501. }
  1502. static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd *io_req)
  1503. {
  1504. struct scsi_cmnd *sc = io_req->sc_cmd;
  1505. struct bnx2fc_interface *interface = io_req->port->priv;
  1506. struct bnx2fc_hba *hba = interface->hba;
  1507. /*
  1508. * Use dma_unmap_sg directly to ensure we're using the correct
  1509. * dev struct off of pcidev.
  1510. */
  1511. if (io_req->bd_tbl->bd_valid && sc && scsi_sg_count(sc)) {
  1512. dma_unmap_sg(&hba->pcidev->dev, scsi_sglist(sc),
  1513. scsi_sg_count(sc), sc->sc_data_direction);
  1514. io_req->bd_tbl->bd_valid = 0;
  1515. }
  1516. }
  1517. void bnx2fc_build_fcp_cmnd(struct bnx2fc_cmd *io_req,
  1518. struct fcp_cmnd *fcp_cmnd)
  1519. {
  1520. memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
  1521. fcp_cmnd->fc_dl = htonl(io_req->data_xfer_len);
  1522. fcp_cmnd->fc_cmdref = 0;
  1523. fcp_cmnd->fc_pri_ta = 0;
  1524. fcp_cmnd->fc_tm_flags = io_req->mp_req.tm_flags;
  1525. fcp_cmnd->fc_flags = io_req->io_req_flags;
  1526. fcp_cmnd->fc_pri_ta = FCP_PTA_SIMPLE;
  1527. }
  1528. static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd *io_req,
  1529. struct fcoe_fcp_rsp_payload *fcp_rsp,
  1530. u8 num_rq, unsigned char *rq_data)
  1531. {
  1532. struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
  1533. u8 rsp_flags = fcp_rsp->fcp_flags.flags;
  1534. u32 rq_buff_len = 0;
  1535. int fcp_sns_len = 0;
  1536. int fcp_rsp_len = 0;
  1537. io_req->fcp_status = FC_GOOD;
  1538. io_req->fcp_resid = 0;
  1539. if (rsp_flags & (FCOE_FCP_RSP_FLAGS_FCP_RESID_OVER |
  1540. FCOE_FCP_RSP_FLAGS_FCP_RESID_UNDER))
  1541. io_req->fcp_resid = fcp_rsp->fcp_resid;
  1542. io_req->scsi_comp_flags = rsp_flags;
  1543. io_req->cdb_status = fcp_rsp->scsi_status_code;
  1544. /* Fetch fcp_rsp_info and fcp_sns_info if available */
  1545. if (num_rq) {
  1546. /*
  1547. * We do not anticipate num_rq >1, as the linux defined
  1548. * SCSI_SENSE_BUFFERSIZE is 96 bytes + 8 bytes of FCP_RSP_INFO
  1549. * 256 bytes of single rq buffer is good enough to hold this.
  1550. */
  1551. if (rsp_flags &
  1552. FCOE_FCP_RSP_FLAGS_FCP_RSP_LEN_VALID) {
  1553. fcp_rsp_len = rq_buff_len
  1554. = fcp_rsp->fcp_rsp_len;
  1555. }
  1556. if (rsp_flags &
  1557. FCOE_FCP_RSP_FLAGS_FCP_SNS_LEN_VALID) {
  1558. fcp_sns_len = fcp_rsp->fcp_sns_len;
  1559. rq_buff_len += fcp_rsp->fcp_sns_len;
  1560. }
  1561. io_req->fcp_rsp_len = fcp_rsp_len;
  1562. io_req->fcp_sns_len = fcp_sns_len;
  1563. if (rq_buff_len > num_rq * BNX2FC_RQ_BUF_SZ) {
  1564. /* Invalid sense sense length. */
  1565. printk(KERN_ERR PFX "invalid sns length %d\n",
  1566. rq_buff_len);
  1567. /* reset rq_buff_len */
  1568. rq_buff_len = num_rq * BNX2FC_RQ_BUF_SZ;
  1569. }
  1570. /* fetch fcp_rsp_code */
  1571. if ((fcp_rsp_len == 4) || (fcp_rsp_len == 8)) {
  1572. /* Only for task management function */
  1573. io_req->fcp_rsp_code = rq_data[3];
  1574. BNX2FC_IO_DBG(io_req, "fcp_rsp_code = %d\n",
  1575. io_req->fcp_rsp_code);
  1576. }
  1577. /* fetch sense data */
  1578. rq_data += fcp_rsp_len;
  1579. if (fcp_sns_len > SCSI_SENSE_BUFFERSIZE) {
  1580. printk(KERN_ERR PFX "Truncating sense buffer\n");
  1581. fcp_sns_len = SCSI_SENSE_BUFFERSIZE;
  1582. }
  1583. memset(sc_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  1584. if (fcp_sns_len)
  1585. memcpy(sc_cmd->sense_buffer, rq_data, fcp_sns_len);
  1586. }
  1587. }
  1588. /**
  1589. * bnx2fc_queuecommand - Queuecommand function of the scsi template
  1590. *
  1591. * @host: The Scsi_Host the command was issued to
  1592. * @sc_cmd: struct scsi_cmnd to be executed
  1593. *
  1594. * This is the IO strategy routine, called by SCSI-ML
  1595. **/
  1596. int bnx2fc_queuecommand(struct Scsi_Host *host,
  1597. struct scsi_cmnd *sc_cmd)
  1598. {
  1599. struct fc_lport *lport = shost_priv(host);
  1600. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  1601. struct fc_rport_libfc_priv *rp = rport->dd_data;
  1602. struct bnx2fc_rport *tgt;
  1603. struct bnx2fc_cmd *io_req;
  1604. int rc = 0;
  1605. int rval;
  1606. rval = fc_remote_port_chkready(rport);
  1607. if (rval) {
  1608. sc_cmd->result = rval;
  1609. scsi_done(sc_cmd);
  1610. return 0;
  1611. }
  1612. if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
  1613. rc = SCSI_MLQUEUE_HOST_BUSY;
  1614. goto exit_qcmd;
  1615. }
  1616. /* rport and tgt are allocated together, so tgt should be non-NULL */
  1617. tgt = (struct bnx2fc_rport *)&rp[1];
  1618. if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
  1619. /*
  1620. * Session is not offloaded yet. Let SCSI-ml retry
  1621. * the command.
  1622. */
  1623. rc = SCSI_MLQUEUE_TARGET_BUSY;
  1624. goto exit_qcmd;
  1625. }
  1626. if (tgt->retry_delay_timestamp) {
  1627. if (time_after(jiffies, tgt->retry_delay_timestamp)) {
  1628. tgt->retry_delay_timestamp = 0;
  1629. } else {
  1630. /* If retry_delay timer is active, flow off the ML */
  1631. rc = SCSI_MLQUEUE_TARGET_BUSY;
  1632. goto exit_qcmd;
  1633. }
  1634. }
  1635. spin_lock_bh(&tgt->tgt_lock);
  1636. io_req = bnx2fc_cmd_alloc(tgt);
  1637. if (!io_req) {
  1638. rc = SCSI_MLQUEUE_HOST_BUSY;
  1639. goto exit_qcmd_tgtlock;
  1640. }
  1641. io_req->sc_cmd = sc_cmd;
  1642. if (bnx2fc_post_io_req(tgt, io_req)) {
  1643. printk(KERN_ERR PFX "Unable to post io_req\n");
  1644. rc = SCSI_MLQUEUE_HOST_BUSY;
  1645. goto exit_qcmd_tgtlock;
  1646. }
  1647. exit_qcmd_tgtlock:
  1648. spin_unlock_bh(&tgt->tgt_lock);
  1649. exit_qcmd:
  1650. return rc;
  1651. }
  1652. void bnx2fc_process_scsi_cmd_compl(struct bnx2fc_cmd *io_req,
  1653. struct fcoe_task_ctx_entry *task,
  1654. u8 num_rq, unsigned char *rq_data)
  1655. {
  1656. struct fcoe_fcp_rsp_payload *fcp_rsp;
  1657. struct bnx2fc_rport *tgt = io_req->tgt;
  1658. struct scsi_cmnd *sc_cmd;
  1659. u16 scope = 0, qualifier = 0;
  1660. /* scsi_cmd_cmpl is called with tgt lock held */
  1661. if (test_and_set_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags)) {
  1662. /* we will not receive ABTS response for this IO */
  1663. BNX2FC_IO_DBG(io_req, "Timer context finished processing "
  1664. "this scsi cmd\n");
  1665. if (test_and_clear_bit(BNX2FC_FLAG_IO_CLEANUP,
  1666. &io_req->req_flags)) {
  1667. BNX2FC_IO_DBG(io_req,
  1668. "Actual completion after cleanup request cleaning up\n");
  1669. bnx2fc_process_cleanup_compl(io_req, task, num_rq);
  1670. }
  1671. return;
  1672. }
  1673. /* Cancel the timeout_work, as we received IO completion */
  1674. if (cancel_delayed_work(&io_req->timeout_work))
  1675. kref_put(&io_req->refcount,
  1676. bnx2fc_cmd_release); /* drop timer hold */
  1677. sc_cmd = io_req->sc_cmd;
  1678. if (sc_cmd == NULL) {
  1679. printk(KERN_ERR PFX "scsi_cmd_compl - sc_cmd is NULL\n");
  1680. return;
  1681. }
  1682. /* Fetch fcp_rsp from task context and perform cmd completion */
  1683. fcp_rsp = (struct fcoe_fcp_rsp_payload *)
  1684. &(task->rxwr_only.union_ctx.comp_info.fcp_rsp.payload);
  1685. /* parse fcp_rsp and obtain sense data from RQ if available */
  1686. bnx2fc_parse_fcp_rsp(io_req, fcp_rsp, num_rq, rq_data);
  1687. if (!bnx2fc_priv(sc_cmd)->io_req) {
  1688. printk(KERN_ERR PFX "io_req is NULL\n");
  1689. return;
  1690. }
  1691. if (io_req->on_active_queue) {
  1692. list_del_init(&io_req->link);
  1693. io_req->on_active_queue = 0;
  1694. /* Move IO req to retire queue */
  1695. list_add_tail(&io_req->link, &tgt->io_retire_queue);
  1696. } else {
  1697. /* This should not happen, but could have been pulled
  1698. * by bnx2fc_flush_active_ios(), or during a race
  1699. * between command abort and (late) completion.
  1700. */
  1701. BNX2FC_IO_DBG(io_req, "xid not on active_cmd_queue\n");
  1702. if (io_req->wait_for_abts_comp)
  1703. if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
  1704. &io_req->req_flags))
  1705. complete(&io_req->abts_done);
  1706. }
  1707. bnx2fc_unmap_sg_list(io_req);
  1708. io_req->sc_cmd = NULL;
  1709. switch (io_req->fcp_status) {
  1710. case FC_GOOD:
  1711. if (io_req->cdb_status == 0) {
  1712. /* Good IO completion */
  1713. sc_cmd->result = DID_OK << 16;
  1714. } else {
  1715. /* Transport status is good, SCSI status not good */
  1716. BNX2FC_IO_DBG(io_req, "scsi_cmpl: cdb_status = %d"
  1717. " fcp_resid = 0x%x\n",
  1718. io_req->cdb_status, io_req->fcp_resid);
  1719. sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
  1720. if (io_req->cdb_status == SAM_STAT_TASK_SET_FULL ||
  1721. io_req->cdb_status == SAM_STAT_BUSY) {
  1722. /* Newer array firmware with BUSY or
  1723. * TASK_SET_FULL may return a status that needs
  1724. * the scope bits masked.
  1725. * Or a huge delay timestamp up to 27 minutes
  1726. * can result.
  1727. */
  1728. if (fcp_rsp->retry_delay_timer) {
  1729. /* Upper 2 bits */
  1730. scope = fcp_rsp->retry_delay_timer
  1731. & 0xC000;
  1732. /* Lower 14 bits */
  1733. qualifier = fcp_rsp->retry_delay_timer
  1734. & 0x3FFF;
  1735. }
  1736. if (scope > 0 && qualifier > 0 &&
  1737. qualifier <= 0x3FEF) {
  1738. /* Set the jiffies +
  1739. * retry_delay_timer * 100ms
  1740. * for the rport/tgt
  1741. */
  1742. tgt->retry_delay_timestamp = jiffies +
  1743. (qualifier * HZ / 10);
  1744. }
  1745. }
  1746. }
  1747. if (io_req->fcp_resid)
  1748. scsi_set_resid(sc_cmd, io_req->fcp_resid);
  1749. break;
  1750. default:
  1751. printk(KERN_ERR PFX "scsi_cmd_compl: fcp_status = %d\n",
  1752. io_req->fcp_status);
  1753. break;
  1754. }
  1755. bnx2fc_priv(sc_cmd)->io_req = NULL;
  1756. scsi_done(sc_cmd);
  1757. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1758. }
  1759. int bnx2fc_post_io_req(struct bnx2fc_rport *tgt,
  1760. struct bnx2fc_cmd *io_req)
  1761. {
  1762. struct fcoe_task_ctx_entry *task;
  1763. struct fcoe_task_ctx_entry *task_page;
  1764. struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
  1765. struct fcoe_port *port = tgt->port;
  1766. struct bnx2fc_interface *interface = port->priv;
  1767. struct bnx2fc_hba *hba = interface->hba;
  1768. struct fc_lport *lport = port->lport;
  1769. int task_idx, index;
  1770. u16 xid;
  1771. /* bnx2fc_post_io_req() is called with the tgt_lock held */
  1772. /* Initialize rest of io_req fields */
  1773. io_req->cmd_type = BNX2FC_SCSI_CMD;
  1774. io_req->port = port;
  1775. io_req->tgt = tgt;
  1776. io_req->data_xfer_len = scsi_bufflen(sc_cmd);
  1777. bnx2fc_priv(sc_cmd)->io_req = io_req;
  1778. if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
  1779. io_req->io_req_flags = BNX2FC_READ;
  1780. this_cpu_inc(lport->stats->InputRequests);
  1781. this_cpu_add(lport->stats->InputBytes, io_req->data_xfer_len);
  1782. } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
  1783. io_req->io_req_flags = BNX2FC_WRITE;
  1784. this_cpu_inc(lport->stats->OutputRequests);
  1785. this_cpu_add(lport->stats->OutputBytes, io_req->data_xfer_len);
  1786. } else {
  1787. io_req->io_req_flags = 0;
  1788. this_cpu_inc(lport->stats->ControlRequests);
  1789. }
  1790. xid = io_req->xid;
  1791. /* Build buffer descriptor list for firmware from sg list */
  1792. if (bnx2fc_build_bd_list_from_sg(io_req)) {
  1793. printk(KERN_ERR PFX "BD list creation failed\n");
  1794. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1795. return -EAGAIN;
  1796. }
  1797. task_idx = xid / BNX2FC_TASKS_PER_PAGE;
  1798. index = xid % BNX2FC_TASKS_PER_PAGE;
  1799. /* Initialize task context for this IO request */
  1800. task_page = (struct fcoe_task_ctx_entry *) hba->task_ctx[task_idx];
  1801. task = &(task_page[index]);
  1802. bnx2fc_init_task(io_req, task);
  1803. if (tgt->flush_in_prog) {
  1804. printk(KERN_ERR PFX "Flush in progress..Host Busy\n");
  1805. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1806. return -EAGAIN;
  1807. }
  1808. if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
  1809. printk(KERN_ERR PFX "Session not ready...post_io\n");
  1810. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1811. return -EAGAIN;
  1812. }
  1813. /* Time IO req */
  1814. if (tgt->io_timeout)
  1815. bnx2fc_cmd_timer_set(io_req, BNX2FC_IO_TIMEOUT);
  1816. /* Obtain free SQ entry */
  1817. bnx2fc_add_2_sq(tgt, xid);
  1818. /* Enqueue the io_req to active_cmd_queue */
  1819. io_req->on_active_queue = 1;
  1820. /* move io_req from pending_queue to active_queue */
  1821. list_add_tail(&io_req->link, &tgt->active_cmd_queue);
  1822. /* Ring doorbell */
  1823. bnx2fc_ring_doorbell(tgt);
  1824. return 0;
  1825. }