bnx2fc_io.c 54 KB

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