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 ([email protected])
  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 scsi_cmnd *sc_cmd, u8 tm_flags)
  576. {
  577. struct fc_lport *lport;
  578. struct fc_rport *rport;
  579. struct fc_rport_libfc_priv *rp;
  580. struct fcoe_port *port;
  581. struct bnx2fc_interface *interface;
  582. struct bnx2fc_rport *tgt;
  583. struct bnx2fc_cmd *io_req;
  584. struct bnx2fc_mp_req *tm_req;
  585. struct fcoe_task_ctx_entry *task;
  586. struct fcoe_task_ctx_entry *task_page;
  587. struct Scsi_Host *host = sc_cmd->device->host;
  588. struct fc_frame_header *fc_hdr;
  589. struct fcp_cmnd *fcp_cmnd;
  590. int task_idx, index;
  591. int rc = SUCCESS;
  592. u16 xid;
  593. u32 sid, did;
  594. unsigned long start = jiffies;
  595. lport = shost_priv(host);
  596. rport = starget_to_rport(scsi_target(sc_cmd->device));
  597. port = lport_priv(lport);
  598. interface = port->priv;
  599. if (rport == NULL) {
  600. printk(KERN_ERR PFX "device_reset: rport is NULL\n");
  601. rc = FAILED;
  602. goto tmf_err;
  603. }
  604. rp = rport->dd_data;
  605. rc = fc_block_scsi_eh(sc_cmd);
  606. if (rc)
  607. return rc;
  608. if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
  609. printk(KERN_ERR PFX "device_reset: link is not ready\n");
  610. rc = FAILED;
  611. goto tmf_err;
  612. }
  613. /* rport and tgt are allocated together, so tgt should be non-NULL */
  614. tgt = (struct bnx2fc_rport *)&rp[1];
  615. if (!(test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
  616. printk(KERN_ERR PFX "device_reset: tgt not offloaded\n");
  617. rc = FAILED;
  618. goto tmf_err;
  619. }
  620. retry_tmf:
  621. io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_TASK_MGMT_CMD);
  622. if (!io_req) {
  623. if (time_after(jiffies, start + HZ)) {
  624. printk(KERN_ERR PFX "tmf: Failed TMF");
  625. rc = FAILED;
  626. goto tmf_err;
  627. }
  628. msleep(20);
  629. goto retry_tmf;
  630. }
  631. /* Initialize rest of io_req fields */
  632. io_req->sc_cmd = sc_cmd;
  633. io_req->port = port;
  634. io_req->tgt = tgt;
  635. tm_req = (struct bnx2fc_mp_req *)&(io_req->mp_req);
  636. rc = bnx2fc_init_mp_req(io_req);
  637. if (rc == FAILED) {
  638. printk(KERN_ERR PFX "Task mgmt MP request init failed\n");
  639. spin_lock_bh(&tgt->tgt_lock);
  640. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  641. spin_unlock_bh(&tgt->tgt_lock);
  642. goto tmf_err;
  643. }
  644. /* Set TM flags */
  645. io_req->io_req_flags = 0;
  646. tm_req->tm_flags = tm_flags;
  647. /* Fill FCP_CMND */
  648. bnx2fc_build_fcp_cmnd(io_req, (struct fcp_cmnd *)tm_req->req_buf);
  649. fcp_cmnd = (struct fcp_cmnd *)tm_req->req_buf;
  650. memset(fcp_cmnd->fc_cdb, 0, sc_cmd->cmd_len);
  651. fcp_cmnd->fc_dl = 0;
  652. /* Fill FC header */
  653. fc_hdr = &(tm_req->req_fc_hdr);
  654. sid = tgt->sid;
  655. did = rport->port_id;
  656. __fc_fill_fc_hdr(fc_hdr, FC_RCTL_DD_UNSOL_CMD, did, sid,
  657. FC_TYPE_FCP, FC_FC_FIRST_SEQ | FC_FC_END_SEQ |
  658. FC_FC_SEQ_INIT, 0);
  659. /* Obtain exchange id */
  660. xid = io_req->xid;
  661. BNX2FC_TGT_DBG(tgt, "Initiate TMF - xid = 0x%x\n", xid);
  662. task_idx = xid/BNX2FC_TASKS_PER_PAGE;
  663. index = xid % BNX2FC_TASKS_PER_PAGE;
  664. /* Initialize task context for this IO request */
  665. task_page = (struct fcoe_task_ctx_entry *)
  666. interface->hba->task_ctx[task_idx];
  667. task = &(task_page[index]);
  668. bnx2fc_init_mp_task(io_req, task);
  669. bnx2fc_priv(sc_cmd)->io_req = io_req;
  670. /* Obtain free SQ entry */
  671. spin_lock_bh(&tgt->tgt_lock);
  672. bnx2fc_add_2_sq(tgt, xid);
  673. /* Enqueue the io_req to active_tm_queue */
  674. io_req->on_tmf_queue = 1;
  675. list_add_tail(&io_req->link, &tgt->active_tm_queue);
  676. init_completion(&io_req->abts_done);
  677. io_req->wait_for_abts_comp = 1;
  678. /* Ring doorbell */
  679. bnx2fc_ring_doorbell(tgt);
  680. spin_unlock_bh(&tgt->tgt_lock);
  681. rc = wait_for_completion_timeout(&io_req->abts_done,
  682. interface->tm_timeout * HZ);
  683. spin_lock_bh(&tgt->tgt_lock);
  684. io_req->wait_for_abts_comp = 0;
  685. if (!(test_bit(BNX2FC_FLAG_TM_COMPL, &io_req->req_flags))) {
  686. set_bit(BNX2FC_FLAG_TM_TIMEOUT, &io_req->req_flags);
  687. if (io_req->on_tmf_queue) {
  688. list_del_init(&io_req->link);
  689. io_req->on_tmf_queue = 0;
  690. }
  691. io_req->wait_for_cleanup_comp = 1;
  692. init_completion(&io_req->cleanup_done);
  693. bnx2fc_initiate_cleanup(io_req);
  694. spin_unlock_bh(&tgt->tgt_lock);
  695. rc = wait_for_completion_timeout(&io_req->cleanup_done,
  696. BNX2FC_FW_TIMEOUT);
  697. spin_lock_bh(&tgt->tgt_lock);
  698. io_req->wait_for_cleanup_comp = 0;
  699. if (!rc)
  700. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  701. }
  702. spin_unlock_bh(&tgt->tgt_lock);
  703. if (!rc) {
  704. BNX2FC_TGT_DBG(tgt, "task mgmt command failed...\n");
  705. rc = FAILED;
  706. } else {
  707. BNX2FC_TGT_DBG(tgt, "task mgmt command success...\n");
  708. rc = SUCCESS;
  709. }
  710. tmf_err:
  711. return rc;
  712. }
  713. int bnx2fc_initiate_abts(struct bnx2fc_cmd *io_req)
  714. {
  715. struct fc_lport *lport;
  716. struct bnx2fc_rport *tgt = io_req->tgt;
  717. struct fc_rport *rport = tgt->rport;
  718. struct fc_rport_priv *rdata = tgt->rdata;
  719. struct bnx2fc_interface *interface;
  720. struct fcoe_port *port;
  721. struct bnx2fc_cmd *abts_io_req;
  722. struct fcoe_task_ctx_entry *task;
  723. struct fcoe_task_ctx_entry *task_page;
  724. struct fc_frame_header *fc_hdr;
  725. struct bnx2fc_mp_req *abts_req;
  726. int task_idx, index;
  727. u32 sid, did;
  728. u16 xid;
  729. int rc = SUCCESS;
  730. u32 r_a_tov = rdata->r_a_tov;
  731. /* called with tgt_lock held */
  732. BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_abts\n");
  733. port = io_req->port;
  734. interface = port->priv;
  735. lport = port->lport;
  736. if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
  737. printk(KERN_ERR PFX "initiate_abts: tgt not offloaded\n");
  738. rc = FAILED;
  739. goto abts_err;
  740. }
  741. if (rport == NULL) {
  742. printk(KERN_ERR PFX "initiate_abts: rport is NULL\n");
  743. rc = FAILED;
  744. goto abts_err;
  745. }
  746. if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
  747. printk(KERN_ERR PFX "initiate_abts: link is not ready\n");
  748. rc = FAILED;
  749. goto abts_err;
  750. }
  751. abts_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_ABTS);
  752. if (!abts_io_req) {
  753. printk(KERN_ERR PFX "abts: couldn't allocate cmd\n");
  754. rc = FAILED;
  755. goto abts_err;
  756. }
  757. /* Initialize rest of io_req fields */
  758. abts_io_req->sc_cmd = NULL;
  759. abts_io_req->port = port;
  760. abts_io_req->tgt = tgt;
  761. abts_io_req->data_xfer_len = 0; /* No data transfer for ABTS */
  762. abts_req = (struct bnx2fc_mp_req *)&(abts_io_req->mp_req);
  763. memset(abts_req, 0, sizeof(struct bnx2fc_mp_req));
  764. /* Fill FC header */
  765. fc_hdr = &(abts_req->req_fc_hdr);
  766. /* Obtain oxid and rxid for the original exchange to be aborted */
  767. fc_hdr->fh_ox_id = htons(io_req->xid);
  768. fc_hdr->fh_rx_id = htons(io_req->task->rxwr_txrd.var_ctx.rx_id);
  769. sid = tgt->sid;
  770. did = rport->port_id;
  771. __fc_fill_fc_hdr(fc_hdr, FC_RCTL_BA_ABTS, did, sid,
  772. FC_TYPE_BLS, FC_FC_FIRST_SEQ | FC_FC_END_SEQ |
  773. FC_FC_SEQ_INIT, 0);
  774. xid = abts_io_req->xid;
  775. BNX2FC_IO_DBG(abts_io_req, "ABTS io_req\n");
  776. task_idx = xid/BNX2FC_TASKS_PER_PAGE;
  777. index = xid % BNX2FC_TASKS_PER_PAGE;
  778. /* Initialize task context for this IO request */
  779. task_page = (struct fcoe_task_ctx_entry *)
  780. interface->hba->task_ctx[task_idx];
  781. task = &(task_page[index]);
  782. bnx2fc_init_mp_task(abts_io_req, task);
  783. /*
  784. * ABTS task is a temporary task that will be cleaned up
  785. * irrespective of ABTS response. We need to start the timer
  786. * for the original exchange, as the CQE is posted for the original
  787. * IO request.
  788. *
  789. * Timer for ABTS is started only when it is originated by a
  790. * TM request. For the ABTS issued as part of ULP timeout,
  791. * scsi-ml maintains the timers.
  792. */
  793. /* if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags))*/
  794. bnx2fc_cmd_timer_set(io_req, 2 * r_a_tov);
  795. /* Obtain free SQ entry */
  796. bnx2fc_add_2_sq(tgt, xid);
  797. /* Ring doorbell */
  798. bnx2fc_ring_doorbell(tgt);
  799. abts_err:
  800. return rc;
  801. }
  802. int bnx2fc_initiate_seq_cleanup(struct bnx2fc_cmd *orig_io_req, u32 offset,
  803. enum fc_rctl r_ctl)
  804. {
  805. struct bnx2fc_rport *tgt = orig_io_req->tgt;
  806. struct bnx2fc_interface *interface;
  807. struct fcoe_port *port;
  808. struct bnx2fc_cmd *seq_clnp_req;
  809. struct fcoe_task_ctx_entry *task;
  810. struct fcoe_task_ctx_entry *task_page;
  811. struct bnx2fc_els_cb_arg *cb_arg = NULL;
  812. int task_idx, index;
  813. u16 xid;
  814. int rc = 0;
  815. BNX2FC_IO_DBG(orig_io_req, "bnx2fc_initiate_seq_cleanup xid = 0x%x\n",
  816. orig_io_req->xid);
  817. kref_get(&orig_io_req->refcount);
  818. port = orig_io_req->port;
  819. interface = port->priv;
  820. cb_arg = kzalloc(sizeof(struct bnx2fc_els_cb_arg), GFP_ATOMIC);
  821. if (!cb_arg) {
  822. printk(KERN_ERR PFX "Unable to alloc cb_arg for seq clnup\n");
  823. rc = -ENOMEM;
  824. goto cleanup_err;
  825. }
  826. seq_clnp_req = bnx2fc_elstm_alloc(tgt, BNX2FC_SEQ_CLEANUP);
  827. if (!seq_clnp_req) {
  828. printk(KERN_ERR PFX "cleanup: couldn't allocate cmd\n");
  829. rc = -ENOMEM;
  830. kfree(cb_arg);
  831. goto cleanup_err;
  832. }
  833. /* Initialize rest of io_req fields */
  834. seq_clnp_req->sc_cmd = NULL;
  835. seq_clnp_req->port = port;
  836. seq_clnp_req->tgt = tgt;
  837. seq_clnp_req->data_xfer_len = 0; /* No data transfer for cleanup */
  838. xid = seq_clnp_req->xid;
  839. task_idx = xid/BNX2FC_TASKS_PER_PAGE;
  840. index = xid % BNX2FC_TASKS_PER_PAGE;
  841. /* Initialize task context for this IO request */
  842. task_page = (struct fcoe_task_ctx_entry *)
  843. interface->hba->task_ctx[task_idx];
  844. task = &(task_page[index]);
  845. cb_arg->aborted_io_req = orig_io_req;
  846. cb_arg->io_req = seq_clnp_req;
  847. cb_arg->r_ctl = r_ctl;
  848. cb_arg->offset = offset;
  849. seq_clnp_req->cb_arg = cb_arg;
  850. printk(KERN_ERR PFX "call init_seq_cleanup_task\n");
  851. bnx2fc_init_seq_cleanup_task(seq_clnp_req, task, orig_io_req, offset);
  852. /* Obtain free SQ entry */
  853. bnx2fc_add_2_sq(tgt, xid);
  854. /* Ring doorbell */
  855. bnx2fc_ring_doorbell(tgt);
  856. cleanup_err:
  857. return rc;
  858. }
  859. int bnx2fc_initiate_cleanup(struct bnx2fc_cmd *io_req)
  860. {
  861. struct bnx2fc_rport *tgt = io_req->tgt;
  862. struct bnx2fc_interface *interface;
  863. struct fcoe_port *port;
  864. struct bnx2fc_cmd *cleanup_io_req;
  865. struct fcoe_task_ctx_entry *task;
  866. struct fcoe_task_ctx_entry *task_page;
  867. int task_idx, index;
  868. u16 xid, orig_xid;
  869. int rc = 0;
  870. /* ASSUMPTION: called with tgt_lock held */
  871. BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_cleanup\n");
  872. port = io_req->port;
  873. interface = port->priv;
  874. cleanup_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_CLEANUP);
  875. if (!cleanup_io_req) {
  876. printk(KERN_ERR PFX "cleanup: couldn't allocate cmd\n");
  877. rc = -1;
  878. goto cleanup_err;
  879. }
  880. /* Initialize rest of io_req fields */
  881. cleanup_io_req->sc_cmd = NULL;
  882. cleanup_io_req->port = port;
  883. cleanup_io_req->tgt = tgt;
  884. cleanup_io_req->data_xfer_len = 0; /* No data transfer for cleanup */
  885. xid = cleanup_io_req->xid;
  886. task_idx = xid/BNX2FC_TASKS_PER_PAGE;
  887. index = xid % BNX2FC_TASKS_PER_PAGE;
  888. /* Initialize task context for this IO request */
  889. task_page = (struct fcoe_task_ctx_entry *)
  890. interface->hba->task_ctx[task_idx];
  891. task = &(task_page[index]);
  892. orig_xid = io_req->xid;
  893. BNX2FC_IO_DBG(io_req, "CLEANUP io_req xid = 0x%x\n", xid);
  894. bnx2fc_init_cleanup_task(cleanup_io_req, task, orig_xid);
  895. /* Obtain free SQ entry */
  896. bnx2fc_add_2_sq(tgt, xid);
  897. /* Set flag that cleanup request is pending with the firmware */
  898. set_bit(BNX2FC_FLAG_ISSUE_CLEANUP_REQ, &io_req->req_flags);
  899. /* Ring doorbell */
  900. bnx2fc_ring_doorbell(tgt);
  901. cleanup_err:
  902. return rc;
  903. }
  904. /**
  905. * bnx2fc_eh_target_reset: Reset a target
  906. *
  907. * @sc_cmd: SCSI command
  908. *
  909. * Set from SCSI host template to send task mgmt command to the target
  910. * and wait for the response
  911. */
  912. int bnx2fc_eh_target_reset(struct scsi_cmnd *sc_cmd)
  913. {
  914. return bnx2fc_initiate_tmf(sc_cmd, FCP_TMF_TGT_RESET);
  915. }
  916. /**
  917. * bnx2fc_eh_device_reset - Reset a single LUN
  918. *
  919. * @sc_cmd: SCSI command
  920. *
  921. * Set from SCSI host template to send task mgmt command to the target
  922. * and wait for the response
  923. */
  924. int bnx2fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
  925. {
  926. return bnx2fc_initiate_tmf(sc_cmd, FCP_TMF_LUN_RESET);
  927. }
  928. static int bnx2fc_abts_cleanup(struct bnx2fc_cmd *io_req)
  929. __must_hold(&tgt->tgt_lock)
  930. {
  931. struct bnx2fc_rport *tgt = io_req->tgt;
  932. unsigned int time_left;
  933. init_completion(&io_req->cleanup_done);
  934. io_req->wait_for_cleanup_comp = 1;
  935. bnx2fc_initiate_cleanup(io_req);
  936. spin_unlock_bh(&tgt->tgt_lock);
  937. /*
  938. * Can't wait forever on cleanup response lest we let the SCSI error
  939. * handler wait forever
  940. */
  941. time_left = wait_for_completion_timeout(&io_req->cleanup_done,
  942. BNX2FC_FW_TIMEOUT);
  943. if (!time_left) {
  944. BNX2FC_IO_DBG(io_req, "%s(): Wait for cleanup timed out.\n",
  945. __func__);
  946. /*
  947. * Put the extra reference to the SCSI command since it would
  948. * not have been returned in this case.
  949. */
  950. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  951. }
  952. spin_lock_bh(&tgt->tgt_lock);
  953. io_req->wait_for_cleanup_comp = 0;
  954. return SUCCESS;
  955. }
  956. /**
  957. * bnx2fc_eh_abort - eh_abort_handler api to abort an outstanding
  958. * SCSI command
  959. *
  960. * @sc_cmd: SCSI_ML command pointer
  961. *
  962. * SCSI abort request handler
  963. */
  964. int bnx2fc_eh_abort(struct scsi_cmnd *sc_cmd)
  965. {
  966. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  967. struct fc_rport_libfc_priv *rp = rport->dd_data;
  968. struct bnx2fc_cmd *io_req;
  969. struct fc_lport *lport;
  970. struct bnx2fc_rport *tgt;
  971. int rc;
  972. unsigned int time_left;
  973. rc = fc_block_scsi_eh(sc_cmd);
  974. if (rc)
  975. return rc;
  976. lport = shost_priv(sc_cmd->device->host);
  977. if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
  978. printk(KERN_ERR PFX "eh_abort: link not ready\n");
  979. return FAILED;
  980. }
  981. tgt = (struct bnx2fc_rport *)&rp[1];
  982. BNX2FC_TGT_DBG(tgt, "Entered bnx2fc_eh_abort\n");
  983. spin_lock_bh(&tgt->tgt_lock);
  984. io_req = bnx2fc_priv(sc_cmd)->io_req;
  985. if (!io_req) {
  986. /* Command might have just completed */
  987. printk(KERN_ERR PFX "eh_abort: io_req is NULL\n");
  988. spin_unlock_bh(&tgt->tgt_lock);
  989. return SUCCESS;
  990. }
  991. BNX2FC_IO_DBG(io_req, "eh_abort - refcnt = %d\n",
  992. kref_read(&io_req->refcount));
  993. /* Hold IO request across abort processing */
  994. kref_get(&io_req->refcount);
  995. BUG_ON(tgt != io_req->tgt);
  996. /* Remove the io_req from the active_q. */
  997. /*
  998. * Task Mgmt functions (LUN RESET & TGT RESET) will not
  999. * issue an ABTS on this particular IO req, as the
  1000. * io_req is no longer in the active_q.
  1001. */
  1002. if (tgt->flush_in_prog) {
  1003. printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
  1004. "flush in progress\n", io_req->xid);
  1005. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1006. spin_unlock_bh(&tgt->tgt_lock);
  1007. return SUCCESS;
  1008. }
  1009. if (io_req->on_active_queue == 0) {
  1010. printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
  1011. "not on active_q\n", io_req->xid);
  1012. /*
  1013. * The IO is still with the FW.
  1014. * Return failure and let SCSI-ml retry eh_abort.
  1015. */
  1016. spin_unlock_bh(&tgt->tgt_lock);
  1017. return FAILED;
  1018. }
  1019. /*
  1020. * Only eh_abort processing will remove the IO from
  1021. * active_cmd_q before processing the request. this is
  1022. * done to avoid race conditions between IOs aborted
  1023. * as part of task management completion and eh_abort
  1024. * processing
  1025. */
  1026. list_del_init(&io_req->link);
  1027. io_req->on_active_queue = 0;
  1028. /* Move IO req to retire queue */
  1029. list_add_tail(&io_req->link, &tgt->io_retire_queue);
  1030. init_completion(&io_req->abts_done);
  1031. init_completion(&io_req->cleanup_done);
  1032. if (test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags)) {
  1033. printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
  1034. "already in abts processing\n", io_req->xid);
  1035. if (cancel_delayed_work(&io_req->timeout_work))
  1036. kref_put(&io_req->refcount,
  1037. bnx2fc_cmd_release); /* drop timer hold */
  1038. /*
  1039. * We don't want to hold off the upper layer timer so simply
  1040. * cleanup the command and return that I/O was successfully
  1041. * aborted.
  1042. */
  1043. bnx2fc_abts_cleanup(io_req);
  1044. /* This only occurs when an task abort was requested while ABTS
  1045. is in progress. Setting the IO_CLEANUP flag will skip the
  1046. RRQ process in the case when the fw generated SCSI_CMD cmpl
  1047. was a result from the ABTS request rather than the CLEANUP
  1048. request */
  1049. set_bit(BNX2FC_FLAG_IO_CLEANUP, &io_req->req_flags);
  1050. rc = FAILED;
  1051. goto done;
  1052. }
  1053. /* Cancel the current timer running on this io_req */
  1054. if (cancel_delayed_work(&io_req->timeout_work))
  1055. kref_put(&io_req->refcount,
  1056. bnx2fc_cmd_release); /* drop timer hold */
  1057. set_bit(BNX2FC_FLAG_EH_ABORT, &io_req->req_flags);
  1058. io_req->wait_for_abts_comp = 1;
  1059. rc = bnx2fc_initiate_abts(io_req);
  1060. if (rc == FAILED) {
  1061. io_req->wait_for_cleanup_comp = 1;
  1062. bnx2fc_initiate_cleanup(io_req);
  1063. spin_unlock_bh(&tgt->tgt_lock);
  1064. wait_for_completion(&io_req->cleanup_done);
  1065. spin_lock_bh(&tgt->tgt_lock);
  1066. io_req->wait_for_cleanup_comp = 0;
  1067. goto done;
  1068. }
  1069. spin_unlock_bh(&tgt->tgt_lock);
  1070. /* Wait 2 * RA_TOV + 1 to be sure timeout function hasn't fired */
  1071. time_left = wait_for_completion_timeout(&io_req->abts_done,
  1072. msecs_to_jiffies(2 * rp->r_a_tov + 1));
  1073. if (time_left)
  1074. BNX2FC_IO_DBG(io_req,
  1075. "Timed out in eh_abort waiting for abts_done");
  1076. spin_lock_bh(&tgt->tgt_lock);
  1077. io_req->wait_for_abts_comp = 0;
  1078. if (test_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags)) {
  1079. BNX2FC_IO_DBG(io_req, "IO completed in a different context\n");
  1080. rc = SUCCESS;
  1081. } else if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
  1082. &io_req->req_flags))) {
  1083. /* Let the scsi-ml try to recover this command */
  1084. printk(KERN_ERR PFX "abort failed, xid = 0x%x\n",
  1085. io_req->xid);
  1086. /*
  1087. * Cleanup firmware residuals before returning control back
  1088. * to SCSI ML.
  1089. */
  1090. rc = bnx2fc_abts_cleanup(io_req);
  1091. goto done;
  1092. } else {
  1093. /*
  1094. * We come here even when there was a race condition
  1095. * between timeout and abts completion, and abts
  1096. * completion happens just in time.
  1097. */
  1098. BNX2FC_IO_DBG(io_req, "abort succeeded\n");
  1099. rc = SUCCESS;
  1100. bnx2fc_scsi_done(io_req, DID_ABORT);
  1101. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1102. }
  1103. done:
  1104. /* release the reference taken in eh_abort */
  1105. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1106. spin_unlock_bh(&tgt->tgt_lock);
  1107. return rc;
  1108. }
  1109. void bnx2fc_process_seq_cleanup_compl(struct bnx2fc_cmd *seq_clnp_req,
  1110. struct fcoe_task_ctx_entry *task,
  1111. u8 rx_state)
  1112. {
  1113. struct bnx2fc_els_cb_arg *cb_arg = seq_clnp_req->cb_arg;
  1114. struct bnx2fc_cmd *orig_io_req = cb_arg->aborted_io_req;
  1115. u32 offset = cb_arg->offset;
  1116. enum fc_rctl r_ctl = cb_arg->r_ctl;
  1117. int rc = 0;
  1118. struct bnx2fc_rport *tgt = orig_io_req->tgt;
  1119. BNX2FC_IO_DBG(orig_io_req, "Entered process_cleanup_compl xid = 0x%x"
  1120. "cmd_type = %d\n",
  1121. seq_clnp_req->xid, seq_clnp_req->cmd_type);
  1122. if (rx_state == FCOE_TASK_RX_STATE_IGNORED_SEQUENCE_CLEANUP) {
  1123. printk(KERN_ERR PFX "seq cleanup ignored - xid = 0x%x\n",
  1124. seq_clnp_req->xid);
  1125. goto free_cb_arg;
  1126. }
  1127. spin_unlock_bh(&tgt->tgt_lock);
  1128. rc = bnx2fc_send_srr(orig_io_req, offset, r_ctl);
  1129. spin_lock_bh(&tgt->tgt_lock);
  1130. if (rc)
  1131. printk(KERN_ERR PFX "clnup_compl: Unable to send SRR"
  1132. " IO will abort\n");
  1133. seq_clnp_req->cb_arg = NULL;
  1134. kref_put(&orig_io_req->refcount, bnx2fc_cmd_release);
  1135. free_cb_arg:
  1136. kfree(cb_arg);
  1137. return;
  1138. }
  1139. void bnx2fc_process_cleanup_compl(struct bnx2fc_cmd *io_req,
  1140. struct fcoe_task_ctx_entry *task,
  1141. u8 num_rq)
  1142. {
  1143. BNX2FC_IO_DBG(io_req, "Entered process_cleanup_compl "
  1144. "refcnt = %d, cmd_type = %d\n",
  1145. kref_read(&io_req->refcount), io_req->cmd_type);
  1146. /*
  1147. * Test whether there is a cleanup request pending. If not just
  1148. * exit.
  1149. */
  1150. if (!test_and_clear_bit(BNX2FC_FLAG_ISSUE_CLEANUP_REQ,
  1151. &io_req->req_flags))
  1152. return;
  1153. /*
  1154. * If we receive a cleanup completion for this request then the
  1155. * firmware will not give us an abort completion for this request
  1156. * so clear any ABTS pending flags.
  1157. */
  1158. if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags) &&
  1159. !test_bit(BNX2FC_FLAG_ABTS_DONE, &io_req->req_flags)) {
  1160. set_bit(BNX2FC_FLAG_ABTS_DONE, &io_req->req_flags);
  1161. if (io_req->wait_for_abts_comp)
  1162. complete(&io_req->abts_done);
  1163. }
  1164. bnx2fc_scsi_done(io_req, DID_ERROR);
  1165. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1166. if (io_req->wait_for_cleanup_comp)
  1167. complete(&io_req->cleanup_done);
  1168. }
  1169. void bnx2fc_process_abts_compl(struct bnx2fc_cmd *io_req,
  1170. struct fcoe_task_ctx_entry *task,
  1171. u8 num_rq)
  1172. {
  1173. u32 r_ctl;
  1174. u32 r_a_tov = FC_DEF_R_A_TOV;
  1175. u8 issue_rrq = 0;
  1176. struct bnx2fc_rport *tgt = io_req->tgt;
  1177. BNX2FC_IO_DBG(io_req, "Entered process_abts_compl xid = 0x%x"
  1178. "refcnt = %d, cmd_type = %d\n",
  1179. io_req->xid,
  1180. kref_read(&io_req->refcount), io_req->cmd_type);
  1181. if (test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
  1182. &io_req->req_flags)) {
  1183. BNX2FC_IO_DBG(io_req, "Timer context finished processing"
  1184. " this io\n");
  1185. return;
  1186. }
  1187. /*
  1188. * If we receive an ABTS completion here then we will not receive
  1189. * a cleanup completion so clear any cleanup pending flags.
  1190. */
  1191. if (test_bit(BNX2FC_FLAG_ISSUE_CLEANUP_REQ, &io_req->req_flags)) {
  1192. clear_bit(BNX2FC_FLAG_ISSUE_CLEANUP_REQ, &io_req->req_flags);
  1193. if (io_req->wait_for_cleanup_comp)
  1194. complete(&io_req->cleanup_done);
  1195. }
  1196. /* Do not issue RRQ as this IO is already cleanedup */
  1197. if (test_and_set_bit(BNX2FC_FLAG_IO_CLEANUP,
  1198. &io_req->req_flags))
  1199. goto io_compl;
  1200. /*
  1201. * For ABTS issued due to SCSI eh_abort_handler, timeout
  1202. * values are maintained by scsi-ml itself. Cancel timeout
  1203. * in case ABTS issued as part of task management function
  1204. * or due to FW error.
  1205. */
  1206. if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags))
  1207. if (cancel_delayed_work(&io_req->timeout_work))
  1208. kref_put(&io_req->refcount,
  1209. bnx2fc_cmd_release); /* drop timer hold */
  1210. r_ctl = (u8)task->rxwr_only.union_ctx.comp_info.abts_rsp.r_ctl;
  1211. switch (r_ctl) {
  1212. case FC_RCTL_BA_ACC:
  1213. /*
  1214. * Dont release this cmd yet. It will be relesed
  1215. * after we get RRQ response
  1216. */
  1217. BNX2FC_IO_DBG(io_req, "ABTS response - ACC Send RRQ\n");
  1218. issue_rrq = 1;
  1219. break;
  1220. case FC_RCTL_BA_RJT:
  1221. BNX2FC_IO_DBG(io_req, "ABTS response - RJT\n");
  1222. break;
  1223. default:
  1224. printk(KERN_ERR PFX "Unknown ABTS response\n");
  1225. break;
  1226. }
  1227. if (issue_rrq) {
  1228. BNX2FC_IO_DBG(io_req, "Issue RRQ after R_A_TOV\n");
  1229. set_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags);
  1230. }
  1231. set_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags);
  1232. bnx2fc_cmd_timer_set(io_req, r_a_tov);
  1233. io_compl:
  1234. if (io_req->wait_for_abts_comp) {
  1235. if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
  1236. &io_req->req_flags))
  1237. complete(&io_req->abts_done);
  1238. } else {
  1239. /*
  1240. * We end up here when ABTS is issued as
  1241. * in asynchronous context, i.e., as part
  1242. * of task management completion, or
  1243. * when FW error is received or when the
  1244. * ABTS is issued when the IO is timed
  1245. * out.
  1246. */
  1247. if (io_req->on_active_queue) {
  1248. list_del_init(&io_req->link);
  1249. io_req->on_active_queue = 0;
  1250. /* Move IO req to retire queue */
  1251. list_add_tail(&io_req->link, &tgt->io_retire_queue);
  1252. }
  1253. bnx2fc_scsi_done(io_req, DID_ERROR);
  1254. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1255. }
  1256. }
  1257. static void bnx2fc_lun_reset_cmpl(struct bnx2fc_cmd *io_req)
  1258. {
  1259. struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
  1260. struct bnx2fc_rport *tgt = io_req->tgt;
  1261. struct bnx2fc_cmd *cmd, *tmp;
  1262. u64 tm_lun = sc_cmd->device->lun;
  1263. u64 lun;
  1264. int rc = 0;
  1265. /* called with tgt_lock held */
  1266. BNX2FC_IO_DBG(io_req, "Entered bnx2fc_lun_reset_cmpl\n");
  1267. /*
  1268. * Walk thru the active_ios queue and ABORT the IO
  1269. * that matches with the LUN that was reset
  1270. */
  1271. list_for_each_entry_safe(cmd, tmp, &tgt->active_cmd_queue, link) {
  1272. BNX2FC_TGT_DBG(tgt, "LUN RST cmpl: scan for pending IOs\n");
  1273. lun = cmd->sc_cmd->device->lun;
  1274. if (lun == tm_lun) {
  1275. /* Initiate ABTS on this cmd */
  1276. if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
  1277. &cmd->req_flags)) {
  1278. /* cancel the IO timeout */
  1279. if (cancel_delayed_work(&io_req->timeout_work))
  1280. kref_put(&io_req->refcount,
  1281. bnx2fc_cmd_release);
  1282. /* timer hold */
  1283. rc = bnx2fc_initiate_abts(cmd);
  1284. /* abts shouldn't fail in this context */
  1285. WARN_ON(rc != SUCCESS);
  1286. } else
  1287. printk(KERN_ERR PFX "lun_rst: abts already in"
  1288. " progress for this IO 0x%x\n",
  1289. cmd->xid);
  1290. }
  1291. }
  1292. }
  1293. static void bnx2fc_tgt_reset_cmpl(struct bnx2fc_cmd *io_req)
  1294. {
  1295. struct bnx2fc_rport *tgt = io_req->tgt;
  1296. struct bnx2fc_cmd *cmd, *tmp;
  1297. int rc = 0;
  1298. /* called with tgt_lock held */
  1299. BNX2FC_IO_DBG(io_req, "Entered bnx2fc_tgt_reset_cmpl\n");
  1300. /*
  1301. * Walk thru the active_ios queue and ABORT the IO
  1302. * that matches with the LUN that was reset
  1303. */
  1304. list_for_each_entry_safe(cmd, tmp, &tgt->active_cmd_queue, link) {
  1305. BNX2FC_TGT_DBG(tgt, "TGT RST cmpl: scan for pending IOs\n");
  1306. /* Initiate ABTS */
  1307. if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
  1308. &cmd->req_flags)) {
  1309. /* cancel the IO timeout */
  1310. if (cancel_delayed_work(&io_req->timeout_work))
  1311. kref_put(&io_req->refcount,
  1312. bnx2fc_cmd_release); /* timer hold */
  1313. rc = bnx2fc_initiate_abts(cmd);
  1314. /* abts shouldn't fail in this context */
  1315. WARN_ON(rc != SUCCESS);
  1316. } else
  1317. printk(KERN_ERR PFX "tgt_rst: abts already in progress"
  1318. " for this IO 0x%x\n", cmd->xid);
  1319. }
  1320. }
  1321. void bnx2fc_process_tm_compl(struct bnx2fc_cmd *io_req,
  1322. struct fcoe_task_ctx_entry *task, u8 num_rq,
  1323. unsigned char *rq_data)
  1324. {
  1325. struct bnx2fc_mp_req *tm_req;
  1326. struct fc_frame_header *fc_hdr;
  1327. struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
  1328. u64 *hdr;
  1329. u64 *temp_hdr;
  1330. void *rsp_buf;
  1331. /* Called with tgt_lock held */
  1332. BNX2FC_IO_DBG(io_req, "Entered process_tm_compl\n");
  1333. if (!(test_bit(BNX2FC_FLAG_TM_TIMEOUT, &io_req->req_flags)))
  1334. set_bit(BNX2FC_FLAG_TM_COMPL, &io_req->req_flags);
  1335. else {
  1336. /* TM has already timed out and we got
  1337. * delayed completion. Ignore completion
  1338. * processing.
  1339. */
  1340. return;
  1341. }
  1342. tm_req = &(io_req->mp_req);
  1343. fc_hdr = &(tm_req->resp_fc_hdr);
  1344. hdr = (u64 *)fc_hdr;
  1345. temp_hdr = (u64 *)
  1346. &task->rxwr_only.union_ctx.comp_info.mp_rsp.fc_hdr;
  1347. hdr[0] = cpu_to_be64(temp_hdr[0]);
  1348. hdr[1] = cpu_to_be64(temp_hdr[1]);
  1349. hdr[2] = cpu_to_be64(temp_hdr[2]);
  1350. tm_req->resp_len =
  1351. task->rxwr_only.union_ctx.comp_info.mp_rsp.mp_payload_len;
  1352. rsp_buf = tm_req->resp_buf;
  1353. if (fc_hdr->fh_r_ctl == FC_RCTL_DD_CMD_STATUS) {
  1354. bnx2fc_parse_fcp_rsp(io_req,
  1355. (struct fcoe_fcp_rsp_payload *)
  1356. rsp_buf, num_rq, rq_data);
  1357. if (io_req->fcp_rsp_code == 0) {
  1358. /* TM successful */
  1359. if (tm_req->tm_flags & FCP_TMF_LUN_RESET)
  1360. bnx2fc_lun_reset_cmpl(io_req);
  1361. else if (tm_req->tm_flags & FCP_TMF_TGT_RESET)
  1362. bnx2fc_tgt_reset_cmpl(io_req);
  1363. }
  1364. } else {
  1365. printk(KERN_ERR PFX "tmf's fc_hdr r_ctl = 0x%x\n",
  1366. fc_hdr->fh_r_ctl);
  1367. }
  1368. if (!bnx2fc_priv(sc_cmd)->io_req) {
  1369. printk(KERN_ERR PFX "tm_compl: io_req is NULL\n");
  1370. return;
  1371. }
  1372. switch (io_req->fcp_status) {
  1373. case FC_GOOD:
  1374. if (io_req->cdb_status == 0) {
  1375. /* Good IO completion */
  1376. sc_cmd->result = DID_OK << 16;
  1377. } else {
  1378. /* Transport status is good, SCSI status not good */
  1379. sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
  1380. }
  1381. if (io_req->fcp_resid)
  1382. scsi_set_resid(sc_cmd, io_req->fcp_resid);
  1383. break;
  1384. default:
  1385. BNX2FC_IO_DBG(io_req, "process_tm_compl: fcp_status = %d\n",
  1386. io_req->fcp_status);
  1387. break;
  1388. }
  1389. sc_cmd = io_req->sc_cmd;
  1390. io_req->sc_cmd = NULL;
  1391. /* check if the io_req exists in tgt's tmf_q */
  1392. if (io_req->on_tmf_queue) {
  1393. list_del_init(&io_req->link);
  1394. io_req->on_tmf_queue = 0;
  1395. } else {
  1396. printk(KERN_ERR PFX "Command not on active_cmd_queue!\n");
  1397. return;
  1398. }
  1399. bnx2fc_priv(sc_cmd)->io_req = NULL;
  1400. scsi_done(sc_cmd);
  1401. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1402. if (io_req->wait_for_abts_comp) {
  1403. BNX2FC_IO_DBG(io_req, "tm_compl - wake up the waiter\n");
  1404. complete(&io_req->abts_done);
  1405. }
  1406. }
  1407. static int bnx2fc_split_bd(struct bnx2fc_cmd *io_req, u64 addr, int sg_len,
  1408. int bd_index)
  1409. {
  1410. struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
  1411. int frag_size, sg_frags;
  1412. sg_frags = 0;
  1413. while (sg_len) {
  1414. if (sg_len >= BNX2FC_BD_SPLIT_SZ)
  1415. frag_size = BNX2FC_BD_SPLIT_SZ;
  1416. else
  1417. frag_size = sg_len;
  1418. bd[bd_index + sg_frags].buf_addr_lo = addr & 0xffffffff;
  1419. bd[bd_index + sg_frags].buf_addr_hi = addr >> 32;
  1420. bd[bd_index + sg_frags].buf_len = (u16)frag_size;
  1421. bd[bd_index + sg_frags].flags = 0;
  1422. addr += (u64) frag_size;
  1423. sg_frags++;
  1424. sg_len -= frag_size;
  1425. }
  1426. return sg_frags;
  1427. }
  1428. static int bnx2fc_map_sg(struct bnx2fc_cmd *io_req)
  1429. {
  1430. struct bnx2fc_interface *interface = io_req->port->priv;
  1431. struct bnx2fc_hba *hba = interface->hba;
  1432. struct scsi_cmnd *sc = io_req->sc_cmd;
  1433. struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
  1434. struct scatterlist *sg;
  1435. int byte_count = 0;
  1436. int sg_count = 0;
  1437. int bd_count = 0;
  1438. int sg_frags;
  1439. unsigned int sg_len;
  1440. u64 addr;
  1441. int i;
  1442. WARN_ON(scsi_sg_count(sc) > BNX2FC_MAX_BDS_PER_CMD);
  1443. /*
  1444. * Use dma_map_sg directly to ensure we're using the correct
  1445. * dev struct off of pcidev.
  1446. */
  1447. sg_count = dma_map_sg(&hba->pcidev->dev, scsi_sglist(sc),
  1448. scsi_sg_count(sc), sc->sc_data_direction);
  1449. scsi_for_each_sg(sc, sg, sg_count, i) {
  1450. sg_len = sg_dma_len(sg);
  1451. addr = sg_dma_address(sg);
  1452. if (sg_len > BNX2FC_MAX_BD_LEN) {
  1453. sg_frags = bnx2fc_split_bd(io_req, addr, sg_len,
  1454. bd_count);
  1455. } else {
  1456. sg_frags = 1;
  1457. bd[bd_count].buf_addr_lo = addr & 0xffffffff;
  1458. bd[bd_count].buf_addr_hi = addr >> 32;
  1459. bd[bd_count].buf_len = (u16)sg_len;
  1460. bd[bd_count].flags = 0;
  1461. }
  1462. bd_count += sg_frags;
  1463. byte_count += sg_len;
  1464. }
  1465. if (byte_count != scsi_bufflen(sc))
  1466. printk(KERN_ERR PFX "byte_count = %d != scsi_bufflen = %d, "
  1467. "task_id = 0x%x\n", byte_count, scsi_bufflen(sc),
  1468. io_req->xid);
  1469. return bd_count;
  1470. }
  1471. static int bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd *io_req)
  1472. {
  1473. struct scsi_cmnd *sc = io_req->sc_cmd;
  1474. struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
  1475. int bd_count;
  1476. if (scsi_sg_count(sc)) {
  1477. bd_count = bnx2fc_map_sg(io_req);
  1478. if (bd_count == 0)
  1479. return -ENOMEM;
  1480. } else {
  1481. bd_count = 0;
  1482. bd[0].buf_addr_lo = bd[0].buf_addr_hi = 0;
  1483. bd[0].buf_len = bd[0].flags = 0;
  1484. }
  1485. io_req->bd_tbl->bd_valid = bd_count;
  1486. /*
  1487. * Return the command to ML if BD count exceeds the max number
  1488. * that can be handled by FW.
  1489. */
  1490. if (bd_count > BNX2FC_FW_MAX_BDS_PER_CMD) {
  1491. pr_err("bd_count = %d exceeded FW supported max BD(255), task_id = 0x%x\n",
  1492. bd_count, io_req->xid);
  1493. return -ENOMEM;
  1494. }
  1495. return 0;
  1496. }
  1497. static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd *io_req)
  1498. {
  1499. struct scsi_cmnd *sc = io_req->sc_cmd;
  1500. struct bnx2fc_interface *interface = io_req->port->priv;
  1501. struct bnx2fc_hba *hba = interface->hba;
  1502. /*
  1503. * Use dma_unmap_sg directly to ensure we're using the correct
  1504. * dev struct off of pcidev.
  1505. */
  1506. if (io_req->bd_tbl->bd_valid && sc && scsi_sg_count(sc)) {
  1507. dma_unmap_sg(&hba->pcidev->dev, scsi_sglist(sc),
  1508. scsi_sg_count(sc), sc->sc_data_direction);
  1509. io_req->bd_tbl->bd_valid = 0;
  1510. }
  1511. }
  1512. void bnx2fc_build_fcp_cmnd(struct bnx2fc_cmd *io_req,
  1513. struct fcp_cmnd *fcp_cmnd)
  1514. {
  1515. struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
  1516. memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
  1517. int_to_scsilun(sc_cmd->device->lun, &fcp_cmnd->fc_lun);
  1518. fcp_cmnd->fc_dl = htonl(io_req->data_xfer_len);
  1519. memcpy(fcp_cmnd->fc_cdb, sc_cmd->cmnd, sc_cmd->cmd_len);
  1520. fcp_cmnd->fc_cmdref = 0;
  1521. fcp_cmnd->fc_pri_ta = 0;
  1522. fcp_cmnd->fc_tm_flags = io_req->mp_req.tm_flags;
  1523. fcp_cmnd->fc_flags = io_req->io_req_flags;
  1524. fcp_cmnd->fc_pri_ta = FCP_PTA_SIMPLE;
  1525. }
  1526. static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd *io_req,
  1527. struct fcoe_fcp_rsp_payload *fcp_rsp,
  1528. u8 num_rq, unsigned char *rq_data)
  1529. {
  1530. struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
  1531. u8 rsp_flags = fcp_rsp->fcp_flags.flags;
  1532. u32 rq_buff_len = 0;
  1533. int fcp_sns_len = 0;
  1534. int fcp_rsp_len = 0;
  1535. io_req->fcp_status = FC_GOOD;
  1536. io_req->fcp_resid = 0;
  1537. if (rsp_flags & (FCOE_FCP_RSP_FLAGS_FCP_RESID_OVER |
  1538. FCOE_FCP_RSP_FLAGS_FCP_RESID_UNDER))
  1539. io_req->fcp_resid = fcp_rsp->fcp_resid;
  1540. io_req->scsi_comp_flags = rsp_flags;
  1541. io_req->cdb_status = fcp_rsp->scsi_status_code;
  1542. /* Fetch fcp_rsp_info and fcp_sns_info if available */
  1543. if (num_rq) {
  1544. /*
  1545. * We do not anticipate num_rq >1, as the linux defined
  1546. * SCSI_SENSE_BUFFERSIZE is 96 bytes + 8 bytes of FCP_RSP_INFO
  1547. * 256 bytes of single rq buffer is good enough to hold this.
  1548. */
  1549. if (rsp_flags &
  1550. FCOE_FCP_RSP_FLAGS_FCP_RSP_LEN_VALID) {
  1551. fcp_rsp_len = rq_buff_len
  1552. = fcp_rsp->fcp_rsp_len;
  1553. }
  1554. if (rsp_flags &
  1555. FCOE_FCP_RSP_FLAGS_FCP_SNS_LEN_VALID) {
  1556. fcp_sns_len = fcp_rsp->fcp_sns_len;
  1557. rq_buff_len += fcp_rsp->fcp_sns_len;
  1558. }
  1559. io_req->fcp_rsp_len = fcp_rsp_len;
  1560. io_req->fcp_sns_len = fcp_sns_len;
  1561. if (rq_buff_len > num_rq * BNX2FC_RQ_BUF_SZ) {
  1562. /* Invalid sense sense length. */
  1563. printk(KERN_ERR PFX "invalid sns length %d\n",
  1564. rq_buff_len);
  1565. /* reset rq_buff_len */
  1566. rq_buff_len = num_rq * BNX2FC_RQ_BUF_SZ;
  1567. }
  1568. /* fetch fcp_rsp_code */
  1569. if ((fcp_rsp_len == 4) || (fcp_rsp_len == 8)) {
  1570. /* Only for task management function */
  1571. io_req->fcp_rsp_code = rq_data[3];
  1572. BNX2FC_IO_DBG(io_req, "fcp_rsp_code = %d\n",
  1573. io_req->fcp_rsp_code);
  1574. }
  1575. /* fetch sense data */
  1576. rq_data += fcp_rsp_len;
  1577. if (fcp_sns_len > SCSI_SENSE_BUFFERSIZE) {
  1578. printk(KERN_ERR PFX "Truncating sense buffer\n");
  1579. fcp_sns_len = SCSI_SENSE_BUFFERSIZE;
  1580. }
  1581. memset(sc_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  1582. if (fcp_sns_len)
  1583. memcpy(sc_cmd->sense_buffer, rq_data, fcp_sns_len);
  1584. }
  1585. }
  1586. /**
  1587. * bnx2fc_queuecommand - Queuecommand function of the scsi template
  1588. *
  1589. * @host: The Scsi_Host the command was issued to
  1590. * @sc_cmd: struct scsi_cmnd to be executed
  1591. *
  1592. * This is the IO strategy routine, called by SCSI-ML
  1593. **/
  1594. int bnx2fc_queuecommand(struct Scsi_Host *host,
  1595. struct scsi_cmnd *sc_cmd)
  1596. {
  1597. struct fc_lport *lport = shost_priv(host);
  1598. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  1599. struct fc_rport_libfc_priv *rp = rport->dd_data;
  1600. struct bnx2fc_rport *tgt;
  1601. struct bnx2fc_cmd *io_req;
  1602. int rc = 0;
  1603. int rval;
  1604. rval = fc_remote_port_chkready(rport);
  1605. if (rval) {
  1606. sc_cmd->result = rval;
  1607. scsi_done(sc_cmd);
  1608. return 0;
  1609. }
  1610. if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
  1611. rc = SCSI_MLQUEUE_HOST_BUSY;
  1612. goto exit_qcmd;
  1613. }
  1614. /* rport and tgt are allocated together, so tgt should be non-NULL */
  1615. tgt = (struct bnx2fc_rport *)&rp[1];
  1616. if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
  1617. /*
  1618. * Session is not offloaded yet. Let SCSI-ml retry
  1619. * the command.
  1620. */
  1621. rc = SCSI_MLQUEUE_TARGET_BUSY;
  1622. goto exit_qcmd;
  1623. }
  1624. if (tgt->retry_delay_timestamp) {
  1625. if (time_after(jiffies, tgt->retry_delay_timestamp)) {
  1626. tgt->retry_delay_timestamp = 0;
  1627. } else {
  1628. /* If retry_delay timer is active, flow off the ML */
  1629. rc = SCSI_MLQUEUE_TARGET_BUSY;
  1630. goto exit_qcmd;
  1631. }
  1632. }
  1633. spin_lock_bh(&tgt->tgt_lock);
  1634. io_req = bnx2fc_cmd_alloc(tgt);
  1635. if (!io_req) {
  1636. rc = SCSI_MLQUEUE_HOST_BUSY;
  1637. goto exit_qcmd_tgtlock;
  1638. }
  1639. io_req->sc_cmd = sc_cmd;
  1640. if (bnx2fc_post_io_req(tgt, io_req)) {
  1641. printk(KERN_ERR PFX "Unable to post io_req\n");
  1642. rc = SCSI_MLQUEUE_HOST_BUSY;
  1643. goto exit_qcmd_tgtlock;
  1644. }
  1645. exit_qcmd_tgtlock:
  1646. spin_unlock_bh(&tgt->tgt_lock);
  1647. exit_qcmd:
  1648. return rc;
  1649. }
  1650. void bnx2fc_process_scsi_cmd_compl(struct bnx2fc_cmd *io_req,
  1651. struct fcoe_task_ctx_entry *task,
  1652. u8 num_rq, unsigned char *rq_data)
  1653. {
  1654. struct fcoe_fcp_rsp_payload *fcp_rsp;
  1655. struct bnx2fc_rport *tgt = io_req->tgt;
  1656. struct scsi_cmnd *sc_cmd;
  1657. u16 scope = 0, qualifier = 0;
  1658. /* scsi_cmd_cmpl is called with tgt lock held */
  1659. if (test_and_set_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags)) {
  1660. /* we will not receive ABTS response for this IO */
  1661. BNX2FC_IO_DBG(io_req, "Timer context finished processing "
  1662. "this scsi cmd\n");
  1663. if (test_and_clear_bit(BNX2FC_FLAG_IO_CLEANUP,
  1664. &io_req->req_flags)) {
  1665. BNX2FC_IO_DBG(io_req,
  1666. "Actual completion after cleanup request cleaning up\n");
  1667. bnx2fc_process_cleanup_compl(io_req, task, num_rq);
  1668. }
  1669. return;
  1670. }
  1671. /* Cancel the timeout_work, as we received IO completion */
  1672. if (cancel_delayed_work(&io_req->timeout_work))
  1673. kref_put(&io_req->refcount,
  1674. bnx2fc_cmd_release); /* drop timer hold */
  1675. sc_cmd = io_req->sc_cmd;
  1676. if (sc_cmd == NULL) {
  1677. printk(KERN_ERR PFX "scsi_cmd_compl - sc_cmd is NULL\n");
  1678. return;
  1679. }
  1680. /* Fetch fcp_rsp from task context and perform cmd completion */
  1681. fcp_rsp = (struct fcoe_fcp_rsp_payload *)
  1682. &(task->rxwr_only.union_ctx.comp_info.fcp_rsp.payload);
  1683. /* parse fcp_rsp and obtain sense data from RQ if available */
  1684. bnx2fc_parse_fcp_rsp(io_req, fcp_rsp, num_rq, rq_data);
  1685. if (!bnx2fc_priv(sc_cmd)->io_req) {
  1686. printk(KERN_ERR PFX "io_req is NULL\n");
  1687. return;
  1688. }
  1689. if (io_req->on_active_queue) {
  1690. list_del_init(&io_req->link);
  1691. io_req->on_active_queue = 0;
  1692. /* Move IO req to retire queue */
  1693. list_add_tail(&io_req->link, &tgt->io_retire_queue);
  1694. } else {
  1695. /* This should not happen, but could have been pulled
  1696. * by bnx2fc_flush_active_ios(), or during a race
  1697. * between command abort and (late) completion.
  1698. */
  1699. BNX2FC_IO_DBG(io_req, "xid not on active_cmd_queue\n");
  1700. if (io_req->wait_for_abts_comp)
  1701. if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
  1702. &io_req->req_flags))
  1703. complete(&io_req->abts_done);
  1704. }
  1705. bnx2fc_unmap_sg_list(io_req);
  1706. io_req->sc_cmd = NULL;
  1707. switch (io_req->fcp_status) {
  1708. case FC_GOOD:
  1709. if (io_req->cdb_status == 0) {
  1710. /* Good IO completion */
  1711. sc_cmd->result = DID_OK << 16;
  1712. } else {
  1713. /* Transport status is good, SCSI status not good */
  1714. BNX2FC_IO_DBG(io_req, "scsi_cmpl: cdb_status = %d"
  1715. " fcp_resid = 0x%x\n",
  1716. io_req->cdb_status, io_req->fcp_resid);
  1717. sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
  1718. if (io_req->cdb_status == SAM_STAT_TASK_SET_FULL ||
  1719. io_req->cdb_status == SAM_STAT_BUSY) {
  1720. /* Newer array firmware with BUSY or
  1721. * TASK_SET_FULL may return a status that needs
  1722. * the scope bits masked.
  1723. * Or a huge delay timestamp up to 27 minutes
  1724. * can result.
  1725. */
  1726. if (fcp_rsp->retry_delay_timer) {
  1727. /* Upper 2 bits */
  1728. scope = fcp_rsp->retry_delay_timer
  1729. & 0xC000;
  1730. /* Lower 14 bits */
  1731. qualifier = fcp_rsp->retry_delay_timer
  1732. & 0x3FFF;
  1733. }
  1734. if (scope > 0 && qualifier > 0 &&
  1735. qualifier <= 0x3FEF) {
  1736. /* Set the jiffies +
  1737. * retry_delay_timer * 100ms
  1738. * for the rport/tgt
  1739. */
  1740. tgt->retry_delay_timestamp = jiffies +
  1741. (qualifier * HZ / 10);
  1742. }
  1743. }
  1744. }
  1745. if (io_req->fcp_resid)
  1746. scsi_set_resid(sc_cmd, io_req->fcp_resid);
  1747. break;
  1748. default:
  1749. printk(KERN_ERR PFX "scsi_cmd_compl: fcp_status = %d\n",
  1750. io_req->fcp_status);
  1751. break;
  1752. }
  1753. bnx2fc_priv(sc_cmd)->io_req = NULL;
  1754. scsi_done(sc_cmd);
  1755. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1756. }
  1757. int bnx2fc_post_io_req(struct bnx2fc_rport *tgt,
  1758. struct bnx2fc_cmd *io_req)
  1759. {
  1760. struct fcoe_task_ctx_entry *task;
  1761. struct fcoe_task_ctx_entry *task_page;
  1762. struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
  1763. struct fcoe_port *port = tgt->port;
  1764. struct bnx2fc_interface *interface = port->priv;
  1765. struct bnx2fc_hba *hba = interface->hba;
  1766. struct fc_lport *lport = port->lport;
  1767. int task_idx, index;
  1768. u16 xid;
  1769. /* bnx2fc_post_io_req() is called with the tgt_lock held */
  1770. /* Initialize rest of io_req fields */
  1771. io_req->cmd_type = BNX2FC_SCSI_CMD;
  1772. io_req->port = port;
  1773. io_req->tgt = tgt;
  1774. io_req->data_xfer_len = scsi_bufflen(sc_cmd);
  1775. bnx2fc_priv(sc_cmd)->io_req = io_req;
  1776. if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
  1777. io_req->io_req_flags = BNX2FC_READ;
  1778. this_cpu_inc(lport->stats->InputRequests);
  1779. this_cpu_add(lport->stats->InputBytes, io_req->data_xfer_len);
  1780. } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
  1781. io_req->io_req_flags = BNX2FC_WRITE;
  1782. this_cpu_inc(lport->stats->OutputRequests);
  1783. this_cpu_add(lport->stats->OutputBytes, io_req->data_xfer_len);
  1784. } else {
  1785. io_req->io_req_flags = 0;
  1786. this_cpu_inc(lport->stats->ControlRequests);
  1787. }
  1788. xid = io_req->xid;
  1789. /* Build buffer descriptor list for firmware from sg list */
  1790. if (bnx2fc_build_bd_list_from_sg(io_req)) {
  1791. printk(KERN_ERR PFX "BD list creation failed\n");
  1792. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1793. return -EAGAIN;
  1794. }
  1795. task_idx = xid / BNX2FC_TASKS_PER_PAGE;
  1796. index = xid % BNX2FC_TASKS_PER_PAGE;
  1797. /* Initialize task context for this IO request */
  1798. task_page = (struct fcoe_task_ctx_entry *) hba->task_ctx[task_idx];
  1799. task = &(task_page[index]);
  1800. bnx2fc_init_task(io_req, task);
  1801. if (tgt->flush_in_prog) {
  1802. printk(KERN_ERR PFX "Flush in progress..Host Busy\n");
  1803. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1804. return -EAGAIN;
  1805. }
  1806. if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
  1807. printk(KERN_ERR PFX "Session not ready...post_io\n");
  1808. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1809. return -EAGAIN;
  1810. }
  1811. /* Time IO req */
  1812. if (tgt->io_timeout)
  1813. bnx2fc_cmd_timer_set(io_req, BNX2FC_IO_TIMEOUT);
  1814. /* Obtain free SQ entry */
  1815. bnx2fc_add_2_sq(tgt, xid);
  1816. /* Enqueue the io_req to active_cmd_queue */
  1817. io_req->on_active_queue = 1;
  1818. /* move io_req from pending_queue to active_queue */
  1819. list_add_tail(&io_req->link, &tgt->active_cmd_queue);
  1820. /* Ring doorbell */
  1821. bnx2fc_ring_doorbell(tgt);
  1822. return 0;
  1823. }