bnx2fc_io.c 54 KB

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