bnx2fc_io.c 49 KB

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