bnx2fc_io.c 55 KB

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