bnx2fc_io.c 50 KB

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