bnx2fc_io.c 56 KB

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