/drivers/scsi/bnx2fc/bnx2fc_io.c

http://github.com/mirrors/linux · C · 2106 lines · 1510 code · 279 blank · 317 comment · 223 complexity · 71a63b5b4083420f4256a14aa4d3998c MD5 · raw file

Large files are truncated click here to view the full file

  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, unsigned char *rq_data);
  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->abts_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->abts_done);
  679. io_req->wait_for_abts_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->abts_done,
  684. interface->tm_timeout * HZ);
  685. spin_lock_bh(&tgt->tgt_lock);
  686. io_req->wait_for_abts_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_cleanup_comp = 1;
  694. init_completion(&io_req->cleanup_done);
  695. bnx2fc_initiate_cleanup(io_req);
  696. spin_unlock_bh(&tgt->tgt_lock);
  697. rc = wait_for_completion_timeout(&io_req->cleanup_done,
  698. BNX2FC_FW_TIMEOUT);
  699. spin_lock_bh(&tgt->tgt_lock);
  700. io_req->wait_for_cleanup_comp = 0;
  701. if (!rc)
  702. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  703. }
  704. spin_unlock_bh(&tgt->tgt_lock);
  705. if (!rc) {
  706. BNX2FC_TGT_DBG(tgt, "task mgmt command failed...\n");
  707. rc = FAILED;
  708. } else {
  709. BNX2FC_TGT_DBG(tgt, "task mgmt command success...\n");
  710. rc = SUCCESS;
  711. }
  712. tmf_err:
  713. return rc;
  714. }
  715. int bnx2fc_initiate_abts(struct bnx2fc_cmd *io_req)
  716. {
  717. struct fc_lport *lport;
  718. struct bnx2fc_rport *tgt = io_req->tgt;
  719. struct fc_rport *rport = tgt->rport;
  720. struct fc_rport_priv *rdata = tgt->rdata;
  721. struct bnx2fc_interface *interface;
  722. struct fcoe_port *port;
  723. struct bnx2fc_cmd *abts_io_req;
  724. struct fcoe_task_ctx_entry *task;
  725. struct fcoe_task_ctx_entry *task_page;
  726. struct fc_frame_header *fc_hdr;
  727. struct bnx2fc_mp_req *abts_req;
  728. int task_idx, index;
  729. u32 sid, did;
  730. u16 xid;
  731. int rc = SUCCESS;
  732. u32 r_a_tov = rdata->r_a_tov;
  733. /* called with tgt_lock held */
  734. BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_abts\n");
  735. port = io_req->port;
  736. interface = port->priv;
  737. lport = port->lport;
  738. if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
  739. printk(KERN_ERR PFX "initiate_abts: tgt not offloaded\n");
  740. rc = FAILED;
  741. goto abts_err;
  742. }
  743. if (rport == NULL) {
  744. printk(KERN_ERR PFX "initiate_abts: rport is NULL\n");
  745. rc = FAILED;
  746. goto abts_err;
  747. }
  748. if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
  749. printk(KERN_ERR PFX "initiate_abts: link is not ready\n");
  750. rc = FAILED;
  751. goto abts_err;
  752. }
  753. abts_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_ABTS);
  754. if (!abts_io_req) {
  755. printk(KERN_ERR PFX "abts: couldnt allocate cmd\n");
  756. rc = FAILED;
  757. goto abts_err;
  758. }
  759. /* Initialize rest of io_req fields */
  760. abts_io_req->sc_cmd = NULL;
  761. abts_io_req->port = port;
  762. abts_io_req->tgt = tgt;
  763. abts_io_req->data_xfer_len = 0; /* No data transfer for ABTS */
  764. abts_req = (struct bnx2fc_mp_req *)&(abts_io_req->mp_req);
  765. memset(abts_req, 0, sizeof(struct bnx2fc_mp_req));
  766. /* Fill FC header */
  767. fc_hdr = &(abts_req->req_fc_hdr);
  768. /* Obtain oxid and rxid for the original exchange to be aborted */
  769. fc_hdr->fh_ox_id = htons(io_req->xid);
  770. fc_hdr->fh_rx_id = htons(io_req->task->rxwr_txrd.var_ctx.rx_id);
  771. sid = tgt->sid;
  772. did = rport->port_id;
  773. __fc_fill_fc_hdr(fc_hdr, FC_RCTL_BA_ABTS, did, sid,
  774. FC_TYPE_BLS, FC_FC_FIRST_SEQ | FC_FC_END_SEQ |
  775. FC_FC_SEQ_INIT, 0);
  776. xid = abts_io_req->xid;
  777. BNX2FC_IO_DBG(abts_io_req, "ABTS io_req\n");
  778. task_idx = xid/BNX2FC_TASKS_PER_PAGE;
  779. index = xid % BNX2FC_TASKS_PER_PAGE;
  780. /* Initialize task context for this IO request */
  781. task_page = (struct fcoe_task_ctx_entry *)
  782. interface->hba->task_ctx[task_idx];
  783. task = &(task_page[index]);
  784. bnx2fc_init_mp_task(abts_io_req, task);
  785. /*
  786. * ABTS task is a temporary task that will be cleaned up
  787. * irrespective of ABTS response. We need to start the timer
  788. * for the original exchange, as the CQE is posted for the original
  789. * IO request.
  790. *
  791. * Timer for ABTS is started only when it is originated by a
  792. * TM request. For the ABTS issued as part of ULP timeout,
  793. * scsi-ml maintains the timers.
  794. */
  795. /* if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags))*/
  796. bnx2fc_cmd_timer_set(io_req, 2 * r_a_tov);
  797. /* Obtain free SQ entry */
  798. bnx2fc_add_2_sq(tgt, xid);
  799. /* Ring doorbell */
  800. bnx2fc_ring_doorbell(tgt);
  801. abts_err:
  802. return rc;
  803. }
  804. int bnx2fc_initiate_seq_cleanup(struct bnx2fc_cmd *orig_io_req, u32 offset,
  805. enum fc_rctl r_ctl)
  806. {
  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. cb_arg = kzalloc(sizeof(struct bnx2fc_els_cb_arg), GFP_ATOMIC);
  823. if (!cb_arg) {
  824. printk(KERN_ERR PFX "Unable to alloc cb_arg for seq clnup\n");
  825. rc = -ENOMEM;
  826. goto cleanup_err;
  827. }
  828. seq_clnp_req = bnx2fc_elstm_alloc(tgt, BNX2FC_SEQ_CLEANUP);
  829. if (!seq_clnp_req) {
  830. printk(KERN_ERR PFX "cleanup: couldnt allocate cmd\n");
  831. rc = -ENOMEM;
  832. kfree(cb_arg);
  833. goto cleanup_err;
  834. }
  835. /* Initialize rest of io_req fields */
  836. seq_clnp_req->sc_cmd = NULL;
  837. seq_clnp_req->port = port;
  838. seq_clnp_req->tgt = tgt;
  839. seq_clnp_req->data_xfer_len = 0; /* No data transfer for cleanup */
  840. xid = seq_clnp_req->xid;
  841. task_idx = xid/BNX2FC_TASKS_PER_PAGE;
  842. index = xid % BNX2FC_TASKS_PER_PAGE;
  843. /* Initialize task context for this IO request */
  844. task_page = (struct fcoe_task_ctx_entry *)
  845. interface->hba->task_ctx[task_idx];
  846. task = &(task_page[index]);
  847. cb_arg->aborted_io_req = orig_io_req;
  848. cb_arg->io_req = seq_clnp_req;
  849. cb_arg->r_ctl = r_ctl;
  850. cb_arg->offset = offset;
  851. seq_clnp_req->cb_arg = cb_arg;
  852. printk(KERN_ERR PFX "call init_seq_cleanup_task\n");
  853. bnx2fc_init_seq_cleanup_task(seq_clnp_req, task, orig_io_req, offset);
  854. /* Obtain free SQ entry */
  855. bnx2fc_add_2_sq(tgt, xid);
  856. /* Ring doorbell */
  857. bnx2fc_ring_doorbell(tgt);
  858. cleanup_err:
  859. return rc;
  860. }
  861. int bnx2fc_initiate_cleanup(struct bnx2fc_cmd *io_req)
  862. {
  863. struct bnx2fc_rport *tgt = io_req->tgt;
  864. struct bnx2fc_interface *interface;
  865. struct fcoe_port *port;
  866. struct bnx2fc_cmd *cleanup_io_req;
  867. struct fcoe_task_ctx_entry *task;
  868. struct fcoe_task_ctx_entry *task_page;
  869. int task_idx, index;
  870. u16 xid, orig_xid;
  871. int rc = 0;
  872. /* ASSUMPTION: called with tgt_lock held */
  873. BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_cleanup\n");
  874. port = io_req->port;
  875. interface = port->priv;
  876. cleanup_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_CLEANUP);
  877. if (!cleanup_io_req) {
  878. printk(KERN_ERR PFX "cleanup: couldnt allocate cmd\n");
  879. rc = -1;
  880. goto cleanup_err;
  881. }
  882. /* Initialize rest of io_req fields */
  883. cleanup_io_req->sc_cmd = NULL;
  884. cleanup_io_req->port = port;
  885. cleanup_io_req->tgt = tgt;
  886. cleanup_io_req->data_xfer_len = 0; /* No data transfer for cleanup */
  887. xid = cleanup_io_req->xid;
  888. task_idx = xid/BNX2FC_TASKS_PER_PAGE;
  889. index = xid % BNX2FC_TASKS_PER_PAGE;
  890. /* Initialize task context for this IO request */
  891. task_page = (struct fcoe_task_ctx_entry *)
  892. interface->hba->task_ctx[task_idx];
  893. task = &(task_page[index]);
  894. orig_xid = io_req->xid;
  895. BNX2FC_IO_DBG(io_req, "CLEANUP io_req xid = 0x%x\n", xid);
  896. bnx2fc_init_cleanup_task(cleanup_io_req, task, orig_xid);
  897. /* Obtain free SQ entry */
  898. bnx2fc_add_2_sq(tgt, xid);
  899. /* Set flag that cleanup request is pending with the firmware */
  900. set_bit(BNX2FC_FLAG_ISSUE_CLEANUP_REQ, &io_req->req_flags);
  901. /* Ring doorbell */
  902. bnx2fc_ring_doorbell(tgt);
  903. cleanup_err:
  904. return rc;
  905. }
  906. /**
  907. * bnx2fc_eh_target_reset: Reset a target
  908. *
  909. * @sc_cmd: SCSI command
  910. *
  911. * Set from SCSI host template to send task mgmt command to the target
  912. * and wait for the response
  913. */
  914. int bnx2fc_eh_target_reset(struct scsi_cmnd *sc_cmd)
  915. {
  916. return bnx2fc_initiate_tmf(sc_cmd, FCP_TMF_TGT_RESET);
  917. }
  918. /**
  919. * bnx2fc_eh_device_reset - Reset a single LUN
  920. *
  921. * @sc_cmd: SCSI command
  922. *
  923. * Set from SCSI host template to send task mgmt command to the target
  924. * and wait for the response
  925. */
  926. int bnx2fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
  927. {
  928. return bnx2fc_initiate_tmf(sc_cmd, FCP_TMF_LUN_RESET);
  929. }
  930. static int bnx2fc_abts_cleanup(struct bnx2fc_cmd *io_req)
  931. {
  932. struct bnx2fc_rport *tgt = io_req->tgt;
  933. unsigned int time_left;
  934. init_completion(&io_req->cleanup_done);
  935. io_req->wait_for_cleanup_comp = 1;
  936. bnx2fc_initiate_cleanup(io_req);
  937. spin_unlock_bh(&tgt->tgt_lock);
  938. /*
  939. * Can't wait forever on cleanup response lest we let the SCSI error
  940. * handler wait forever
  941. */
  942. time_left = wait_for_completion_timeout(&io_req->cleanup_done,
  943. BNX2FC_FW_TIMEOUT);
  944. if (!time_left) {
  945. BNX2FC_IO_DBG(io_req, "%s(): Wait for cleanup timed out.\n",
  946. __func__);
  947. /*
  948. * Put the extra reference to the SCSI command since it would
  949. * not have been returned in this case.
  950. */
  951. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  952. }
  953. spin_lock_bh(&tgt->tgt_lock);
  954. io_req->wait_for_cleanup_comp = 0;
  955. return SUCCESS;
  956. }
  957. /**
  958. * bnx2fc_eh_abort - eh_abort_handler api to abort an outstanding
  959. * SCSI command
  960. *
  961. * @sc_cmd: SCSI_ML command pointer
  962. *
  963. * SCSI abort request handler
  964. */
  965. int bnx2fc_eh_abort(struct scsi_cmnd *sc_cmd)
  966. {
  967. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  968. struct fc_rport_libfc_priv *rp = rport->dd_data;
  969. struct bnx2fc_cmd *io_req;
  970. struct fc_lport *lport;
  971. struct bnx2fc_rport *tgt;
  972. int rc;
  973. unsigned int time_left;
  974. rc = fc_block_scsi_eh(sc_cmd);
  975. if (rc)
  976. return rc;
  977. lport = shost_priv(sc_cmd->device->host);
  978. if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
  979. printk(KERN_ERR PFX "eh_abort: link not ready\n");
  980. return FAILED;
  981. }
  982. tgt = (struct bnx2fc_rport *)&rp[1];
  983. BNX2FC_TGT_DBG(tgt, "Entered bnx2fc_eh_abort\n");
  984. spin_lock_bh(&tgt->tgt_lock);
  985. io_req = (struct bnx2fc_cmd *)sc_cmd->SCp.ptr;
  986. if (!io_req) {
  987. /* Command might have just completed */
  988. printk(KERN_ERR PFX "eh_abort: io_req is NULL\n");
  989. spin_unlock_bh(&tgt->tgt_lock);
  990. return SUCCESS;
  991. }
  992. BNX2FC_IO_DBG(io_req, "eh_abort - refcnt = %d\n",
  993. kref_read(&io_req->refcount));
  994. /* Hold IO request across abort processing */
  995. kref_get(&io_req->refcount);
  996. BUG_ON(tgt != io_req->tgt);
  997. /* Remove the io_req from the active_q. */
  998. /*
  999. * Task Mgmt functions (LUN RESET & TGT RESET) will not
  1000. * issue an ABTS on this particular IO req, as the
  1001. * io_req is no longer in the active_q.
  1002. */
  1003. if (tgt->flush_in_prog) {
  1004. printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
  1005. "flush in progress\n", io_req->xid);
  1006. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1007. spin_unlock_bh(&tgt->tgt_lock);
  1008. return SUCCESS;
  1009. }
  1010. if (io_req->on_active_queue == 0) {
  1011. printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
  1012. "not on active_q\n", io_req->xid);
  1013. /*
  1014. * The IO is still with the FW.
  1015. * Return failure and let SCSI-ml retry eh_abort.
  1016. */
  1017. spin_unlock_bh(&tgt->tgt_lock);
  1018. return FAILED;
  1019. }
  1020. /*
  1021. * Only eh_abort processing will remove the IO from
  1022. * active_cmd_q before processing the request. this is
  1023. * done to avoid race conditions between IOs aborted
  1024. * as part of task management completion and eh_abort
  1025. * processing
  1026. */
  1027. list_del_init(&io_req->link);
  1028. io_req->on_active_queue = 0;
  1029. /* Move IO req to retire queue */
  1030. list_add_tail(&io_req->link, &tgt->io_retire_queue);
  1031. init_completion(&io_req->abts_done);
  1032. init_completion(&io_req->cleanup_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_abts_comp = 1;
  1059. rc = bnx2fc_initiate_abts(io_req);
  1060. if (rc == FAILED) {
  1061. io_req->wait_for_cleanup_comp = 1;
  1062. bnx2fc_initiate_cleanup(io_req);
  1063. spin_unlock_bh(&tgt->tgt_lock);
  1064. wait_for_completion(&io_req->cleanup_done);
  1065. spin_lock_bh(&tgt->tgt_lock);
  1066. io_req->wait_for_cleanup_comp = 0;
  1067. goto done;
  1068. }
  1069. spin_unlock_bh(&tgt->tgt_lock);
  1070. /* Wait 2 * RA_TOV + 1 to be sure timeout function hasn't fired */
  1071. time_left = wait_for_completion_timeout(&io_req->abts_done,
  1072. msecs_to_jiffies(2 * rp->r_a_tov + 1));
  1073. if (time_left)
  1074. BNX2FC_IO_DBG(io_req,
  1075. "Timed out in eh_abort waiting for abts_done");
  1076. spin_lock_bh(&tgt->tgt_lock);
  1077. io_req->wait_for_abts_comp = 0;
  1078. if (test_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags)) {
  1079. BNX2FC_IO_DBG(io_req, "IO completed in a different context\n");
  1080. rc = SUCCESS;
  1081. } else if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
  1082. &io_req->req_flags))) {
  1083. /* Let the scsi-ml try to recover this command */
  1084. printk(KERN_ERR PFX "abort failed, xid = 0x%x\n",
  1085. io_req->xid);
  1086. /*
  1087. * Cleanup firmware residuals before returning control back
  1088. * to SCSI ML.
  1089. */
  1090. rc = bnx2fc_abts_cleanup(io_req);
  1091. goto done;
  1092. } else {
  1093. /*
  1094. * We come here even when there was a race condition
  1095. * between timeout and abts completion, and abts
  1096. * completion happens just in time.
  1097. */
  1098. BNX2FC_IO_DBG(io_req, "abort succeeded\n");
  1099. rc = SUCCESS;
  1100. bnx2fc_scsi_done(io_req, DID_ABORT);
  1101. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1102. }
  1103. done:
  1104. /* release the reference taken in eh_abort */
  1105. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1106. spin_unlock_bh(&tgt->tgt_lock);
  1107. return rc;
  1108. }
  1109. void bnx2fc_process_seq_cleanup_compl(struct bnx2fc_cmd *seq_clnp_req,
  1110. struct fcoe_task_ctx_entry *task,
  1111. u8 rx_state)
  1112. {
  1113. struct bnx2fc_els_cb_arg *cb_arg = seq_clnp_req->cb_arg;
  1114. struct bnx2fc_cmd *orig_io_req = cb_arg->aborted_io_req;
  1115. u32 offset = cb_arg->offset;
  1116. enum fc_rctl r_ctl = cb_arg->r_ctl;
  1117. int rc = 0;
  1118. struct bnx2fc_rport *tgt = orig_io_req->tgt;
  1119. BNX2FC_IO_DBG(orig_io_req, "Entered process_cleanup_compl xid = 0x%x"
  1120. "cmd_type = %d\n",
  1121. seq_clnp_req->xid, seq_clnp_req->cmd_type);
  1122. if (rx_state == FCOE_TASK_RX_STATE_IGNORED_SEQUENCE_CLEANUP) {
  1123. printk(KERN_ERR PFX "seq cleanup ignored - xid = 0x%x\n",
  1124. seq_clnp_req->xid);
  1125. goto free_cb_arg;
  1126. }
  1127. spin_unlock_bh(&tgt->tgt_lock);
  1128. rc = bnx2fc_send_srr(orig_io_req, offset, r_ctl);
  1129. spin_lock_bh(&tgt->tgt_lock);
  1130. if (rc)
  1131. printk(KERN_ERR PFX "clnup_compl: Unable to send SRR"
  1132. " IO will abort\n");
  1133. seq_clnp_req->cb_arg = NULL;
  1134. kref_put(&orig_io_req->refcount, bnx2fc_cmd_release);
  1135. free_cb_arg:
  1136. kfree(cb_arg);
  1137. return;
  1138. }
  1139. void bnx2fc_process_cleanup_compl(struct bnx2fc_cmd *io_req,
  1140. struct fcoe_task_ctx_entry *task,
  1141. u8 num_rq)
  1142. {
  1143. BNX2FC_IO_DBG(io_req, "Entered process_cleanup_compl "
  1144. "refcnt = %d, cmd_type = %d\n",
  1145. kref_read(&io_req->refcount), io_req->cmd_type);
  1146. /*
  1147. * Test whether there is a cleanup request pending. If not just
  1148. * exit.
  1149. */
  1150. if (!test_and_clear_bit(BNX2FC_FLAG_ISSUE_CLEANUP_REQ,
  1151. &io_req->req_flags))
  1152. return;
  1153. /*
  1154. * If we receive a cleanup completion for this request then the
  1155. * firmware will not give us an abort completion for this request
  1156. * so clear any ABTS pending flags.
  1157. */
  1158. if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags) &&
  1159. !test_bit(BNX2FC_FLAG_ABTS_DONE, &io_req->req_flags)) {
  1160. set_bit(BNX2FC_FLAG_ABTS_DONE, &io_req->req_flags);
  1161. if (io_req->wait_for_abts_comp)
  1162. complete(&io_req->abts_done);
  1163. }
  1164. bnx2fc_scsi_done(io_req, DID_ERROR);
  1165. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1166. if (io_req->wait_for_cleanup_comp)
  1167. complete(&io_req->cleanup_done);
  1168. }
  1169. void bnx2fc_process_abts_compl(struct bnx2fc_cmd *io_req,
  1170. struct fcoe_task_ctx_entry *task,
  1171. u8 num_rq)
  1172. {
  1173. u32 r_ctl;
  1174. u32 r_a_tov = FC_DEF_R_A_TOV;
  1175. u8 issue_rrq = 0;
  1176. struct bnx2fc_rport *tgt = io_req->tgt;
  1177. BNX2FC_IO_DBG(io_req, "Entered process_abts_compl xid = 0x%x"
  1178. "refcnt = %d, cmd_type = %d\n",
  1179. io_req->xid,
  1180. kref_read(&io_req->refcount), io_req->cmd_type);
  1181. if (test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
  1182. &io_req->req_flags)) {
  1183. BNX2FC_IO_DBG(io_req, "Timer context finished processing"
  1184. " this io\n");
  1185. return;
  1186. }
  1187. /*
  1188. * If we receive an ABTS completion here then we will not receive
  1189. * a cleanup completion so clear any cleanup pending flags.
  1190. */
  1191. if (test_bit(BNX2FC_FLAG_ISSUE_CLEANUP_REQ, &io_req->req_flags)) {
  1192. clear_bit(BNX2FC_FLAG_ISSUE_CLEANUP_REQ, &io_req->req_flags);
  1193. if (io_req->wait_for_cleanup_comp)
  1194. complete(&io_req->cleanup_done);
  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_abts_comp) {
  1235. if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
  1236. &io_req->req_flags))
  1237. complete(&io_req->abts_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. unsigned char *rq_data)
  1324. {
  1325. struct bnx2fc_mp_req *tm_req;
  1326. struct fc_frame_header *fc_hdr;
  1327. struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
  1328. u64 *hdr;
  1329. u64 *temp_hdr;
  1330. void *rsp_buf;
  1331. /* Called with tgt_lock held */
  1332. BNX2FC_IO_DBG(io_req, "Entered process_tm_compl\n");
  1333. if (!(test_bit(BNX2FC_FLAG_TM_TIMEOUT, &io_req->req_flags)))
  1334. set_bit(BNX2FC_FLAG_TM_COMPL, &io_req->req_flags);
  1335. else {
  1336. /* TM has already timed out and we got
  1337. * delayed completion. Ignore completion
  1338. * processing.
  1339. */
  1340. return;
  1341. }
  1342. tm_req = &(io_req->mp_req);
  1343. fc_hdr = &(tm_req->resp_fc_hdr);
  1344. hdr = (u64 *)fc_hdr;
  1345. temp_hdr = (u64 *)
  1346. &task->rxwr_only.union_ctx.comp_info.mp_rsp.fc_hdr;
  1347. hdr[0] = cpu_to_be64(temp_hdr[0]);
  1348. hdr[1] = cpu_to_be64(temp_hdr[1]);
  1349. hdr[2] = cpu_to_be64(temp_hdr[2]);
  1350. tm_req->resp_len =
  1351. task->rxwr_only.union_ctx.comp_info.mp_rsp.mp_payload_len;
  1352. rsp_buf = tm_req->resp_buf;
  1353. if (fc_hdr->fh_r_ctl == FC_RCTL_DD_CMD_STATUS) {
  1354. bnx2fc_parse_fcp_rsp(io_req,
  1355. (struct fcoe_fcp_rsp_payload *)
  1356. rsp_buf, num_rq, rq_data);
  1357. if (io_req->fcp_rsp_code == 0) {
  1358. /* TM successful */
  1359. if (tm_req->tm_flags & FCP_TMF_LUN_RESET)
  1360. bnx2fc_lun_reset_cmpl(io_req);
  1361. else if (tm_req->tm_flags & FCP_TMF_TGT_RESET)
  1362. bnx2fc_tgt_reset_cmpl(io_req);
  1363. }
  1364. } else {
  1365. printk(KERN_ERR PFX "tmf's fc_hdr r_ctl = 0x%x\n",
  1366. fc_hdr->fh_r_ctl);
  1367. }
  1368. if (!sc_cmd->SCp.ptr) {
  1369. printk(KERN_ERR PFX "tm_compl: SCp.ptr is NULL\n");
  1370. return;
  1371. }
  1372. switch (io_req->fcp_status) {
  1373. case FC_GOOD:
  1374. if (io_req->cdb_status == 0) {
  1375. /* Good IO completion */
  1376. sc_cmd->result = DID_OK << 16;
  1377. } else {
  1378. /* Transport status is good, SCSI status not good */
  1379. sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
  1380. }
  1381. if (io_req->fcp_resid)
  1382. scsi_set_resid(sc_cmd, io_req->fcp_resid);
  1383. break;
  1384. default:
  1385. BNX2FC_IO_DBG(io_req, "process_tm_compl: fcp_status = %d\n",
  1386. io_req->fcp_status);
  1387. break;
  1388. }
  1389. sc_cmd = io_req->sc_cmd;
  1390. io_req->sc_cmd = NULL;
  1391. /* check if the io_req exists in tgt's tmf_q */
  1392. if (io_req->on_tmf_queue) {
  1393. list_del_init(&io_req->link);
  1394. io_req->on_tmf_queue = 0;
  1395. } else {
  1396. printk(KERN_ERR PFX "Command not on active_cmd_queue!\n");
  1397. return;
  1398. }
  1399. sc_cmd->SCp.ptr = NULL;
  1400. sc_cmd->scsi_done(sc_cmd);
  1401. kref_put(&io_req->refcount, bnx2fc_cmd_release);
  1402. if (io_req->wait_for_abts_comp) {
  1403. BNX2FC_IO_DBG(io_req, "tm_compl - wake up the waiter\n");
  1404. complete(&io_req->abts_done);
  1405. }
  1406. }
  1407. static int bnx2fc_split_bd(struct bnx2fc_cmd *io_req, u64 addr, int sg_len,
  1408. int bd_index)
  1409. {
  1410. struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
  1411. int frag_size, sg_frags;
  1412. sg_frags = 0;
  1413. while (sg_len) {
  1414. if (sg_len >= BNX2FC_BD_SPLIT_SZ)
  1415. frag_size = BNX2FC_BD_SPLIT_SZ;
  1416. else
  1417. frag_size = sg_len;
  1418. bd[bd_index + sg_frags].buf_addr_lo = addr & 0xffffffff;
  1419. bd[bd_index + sg_frags].buf_addr_hi = addr >> 32;
  1420. bd[bd_index + sg_frags].buf_len = (u16)frag_size;
  1421. bd[bd_index + sg_frags].flags = 0;
  1422. addr += (u64) frag_size;
  1423. sg_frags++;
  1424. sg_len -= frag_size;
  1425. }
  1426. return sg_frags;
  1427. }
  1428. static int bnx2fc_map_sg(struct bnx2fc_cmd *io_req)
  1429. {
  1430. struct bnx2fc_interface *interface = io_req->port->priv;
  1431. struct bnx2fc_hba *hba = interface->hba;
  1432. struct scsi_cmnd *sc = io_req->sc_cmd;
  1433. struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
  1434. struct scatterlist *sg;
  1435. int byte_count = 0;
  1436. int sg_count = 0;
  1437. int bd_count = 0;
  1438. int sg_frags;
  1439. unsigned int sg_len;
  1440. u64 addr;
  1441. int i;
  1442. WARN_ON(scsi_sg_count(sc) > BNX2FC_MAX_BDS_PER_CMD);
  1443. /*
  1444. * Use dma_map_sg directly to ensure we're using the correct
  1445. * dev struct off of pcidev.
  1446. */
  1447. sg_count = dma_map_sg(&hba->pcidev->dev, scsi_sglist(sc),
  1448. scsi_sg_count(sc), sc->sc_data_direction);
  1449. scsi_for_each_sg(sc, sg, sg_count, i) {
  1450. sg_len = sg_dma_len(sg);
  1451. addr = sg_dma_address(sg);
  1452. if (sg_len > BNX2FC_MAX_BD_LEN) {
  1453. sg_frags = bnx2fc_split_bd(io_req, addr, sg_len,
  1454. bd_count);
  1455. } else {
  1456. sg_frags = 1;
  1457. bd[bd_count].buf_addr_lo = addr & 0xffffffff;
  1458. bd[bd_count].buf_addr_hi = addr >> 32;
  1459. bd[bd_count].buf_len = (u16)sg_len;
  1460. bd[bd_count].flags = 0;
  1461. }
  1462. bd_count += sg_frags;
  1463. byte_count += sg_len;
  1464. }
  1465. if (byte_count != scsi_bufflen(sc))
  1466. printk(KERN_ERR PFX "byte_count = %d != scsi_bufflen = %d, "
  1467. "task_id = 0x%x\n", byte_count, scsi_bufflen(sc),
  1468. io_req->xid);
  1469. return bd_count;
  1470. }
  1471. static int bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd *io_req)
  1472. {
  1473. struct scsi_cmnd *sc = io_req->sc_cmd;
  1474. struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
  1475. int bd_count;
  1476. if (scsi_sg_count(sc)) {
  1477. bd_count = bnx2fc_map_sg(io_req);
  1478. if (bd_count == 0)
  1479. return -ENOMEM;
  1480. } else {
  1481. bd_count = 0;
  1482. bd[0].buf_addr_lo = bd[0].buf_addr_hi = 0;
  1483. bd[0].buf_len = bd[0].flags = 0;
  1484. }
  1485. io_req->bd_tbl->bd_valid = bd_count;
  1486. /*
  1487. * Return the command to ML if BD count exceeds the max number
  1488. * that can be handled by FW.
  1489. */
  1490. if (bd_count > BNX2FC_FW_MAX_BDS_PER_CMD) {
  1491. pr_err("bd_count = %d exceeded FW supported max BD(255), task_id = 0x%x\n",
  1492. bd_count, io_req->xid);
  1493. return -ENOMEM;
  1494. }
  1495. return 0;
  1496. }
  1497. static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd *io_req)
  1498. {
  1499. struct scsi_cmnd *sc = io_req->sc_cmd;
  1500. struct bnx2fc_interface *interface = io_req->port->priv;
  1501. struct bnx2fc_hba *hba = interface->hba;
  1502. /*
  1503. * Use dma_unmap_sg directly to ensure we're using the correct
  1504. * dev struct off of pcidev.
  1505. */
  1506. if (io_req->bd_tbl->bd_valid && sc && scsi_sg_count(sc)) {
  1507. dma_unmap_sg(&hba->pcidev->dev, scsi_sglist(sc),
  1508. scsi_sg_count(sc), sc->sc_data_direction);
  1509. io_req->bd_tbl->bd_valid = 0;
  1510. }
  1511. }
  1512. void bnx2fc_build_fcp_cmnd(struct bnx2fc_cmd *io_req,
  1513. struct fcp_cmnd *fcp_cmnd)
  1514. {
  1515. struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
  1516. memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
  1517. int_to_scsilun(sc_cmd->device->lun, &fcp_cmnd->fc_lun);
  1518. fcp_cmnd->fc_dl = htonl(io_req->data_xfer_len);
  1519. memcpy(fcp_cmnd->fc_cdb, sc_cmd->cmnd, sc_cmd->cmd_len);
  1520. fcp_cmnd->fc_cmdref = 0;
  1521. fcp_cmnd->fc_pri_ta = 0;
  1522. fcp_cmnd->fc_tm_flags = io_req->mp_req.tm_flags;
  1523. fcp_cmnd->fc_flags = io_req->io_req_flags;
  1524. fcp_cmnd->fc_pri_ta = FCP_PTA_SIMPLE;
  1525. }
  1526. static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd *io_req,
  1527. struct fcoe_fcp_rsp_payload *fcp_rsp,
  1528. u8 num_rq, unsigned char *rq_data)
  1529. {
  1530. struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
  1531. u8 rsp_flags = fcp_rsp->fcp_flags.flags;
  1532. u32 rq_buff_len = 0;
  1533. int fcp_sns_len = 0;
  1534. int fcp_rsp_len = 0;
  1535. io_req->fcp_status = FC_GOOD;
  1536. io_req->fcp_resid = 0;
  1537. if (rsp_flags & (FCOE_FCP_RSP_FLAGS_FCP_RESID_OVER |
  1538. FCOE_FCP_RSP_FLAGS_FCP_RESID_UNDER))
  1539. io_req->fcp_resid = fcp_rsp->fcp_resid;
  1540. io_req->scsi_comp_flags = rsp_flags;
  1541. CMD_SCSI_STATUS(sc_cmd) = io_req->cdb_status =
  1542. fcp_rsp->scsi_status_code;
  1543. /* Fetch fcp_rsp_info and fcp_sns_info if available */
  1544. if (num_rq) {
  1545. /*
  1546. * We do not anticipate num_rq >1, as the linux defined
  1547. * SCSI_SENSE_BUFFERSIZE is 96 bytes + 8 bytes of FCP_RSP_INFO
  1548. * 256 bytes of single rq buffer is good enough to hold this.
  1549. */
  1550. if (rsp_flags &
  1551. FCOE_FCP_RSP_FLAGS_FCP_RSP_LEN_VALID) {
  1552. fcp_rsp_len = rq_buff_len
  1553. = fcp_rsp->fcp_rsp_len;
  1554. }
  1555. if (rsp_flags &
  1556. FCOE_FCP_RSP_FLAGS_FCP_SNS_LEN_VALID) {
  1557. fcp_sns_len = fcp_rsp->fcp_sns_len;
  1558. rq_buff_len += fcp_rsp->fcp_sns_len;
  1559. }
  1560. io_req->fcp_rsp_len = fcp_rsp_len;
  1561. io_req->fcp_sns_len = fcp_sns_len;
  1562. if (rq_buff_len > num_rq * BNX2FC_RQ_BUF_SZ) {
  1563. /* Invalid sense sense length. */
  1564. printk(KERN_ERR PFX "invalid sns length %d\n",
  1565. rq_buff_len);
  1566. /* reset rq_buff_len */
  1567. rq_buff_len = num_rq * BNX2FC_RQ_BUF_SZ;
  1568. }
  1569. /* fetch fcp_rsp_code */
  1570. if ((fcp_rsp_len == 4) || (fcp_rsp_len == 8)) {
  1571. /* Only for task management function */
  1572. io_req->fcp_rsp_code = rq_data[3];
  1573. BNX2FC_IO_DBG(io_req, "fcp_rsp_code = %d\n",
  1574. io_req->fcp_rsp_