bnx2fc_io.c 54 KB

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