bnx2fc_io.c 49 KB

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