bnx2fc_io.c 54 KB

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