1 /* bnx2fc_io.c: QLogic Linux FCoE offload driver.
2 * IO manager and SCSI IO processing.
4 * Copyright (c) 2008-2013 Broadcom Corporation
5 * Copyright (c) 2014-2016 QLogic Corporation
6 * Copyright (c) 2016-2017 Cavium Inc.
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.
12 * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
17 #define RESERVE_FREE_LIST_INDEX num_possible_cpus()
19 static int bnx2fc_split_bd(struct bnx2fc_cmd *io_req, u64 addr, int sg_len,
21 static int bnx2fc_map_sg(struct bnx2fc_cmd *io_req);
22 static int bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd *io_req);
23 static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd *io_req);
24 static void bnx2fc_free_mp_resc(struct bnx2fc_cmd *io_req);
25 static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd *io_req,
26 struct fcoe_fcp_rsp_payload *fcp_rsp,
27 u8 num_rq, unsigned char *rq_data);
29 void bnx2fc_cmd_timer_set(struct bnx2fc_cmd *io_req,
30 unsigned int timer_msec)
32 struct bnx2fc_interface *interface = io_req->port->priv;
34 if (queue_delayed_work(interface->timer_work_queue,
35 &io_req->timeout_work,
36 msecs_to_jiffies(timer_msec)))
37 kref_get(&io_req->refcount);
40 static void bnx2fc_cmd_timeout(struct work_struct *work)
42 struct bnx2fc_cmd *io_req = container_of(work, struct bnx2fc_cmd,
44 u8 cmd_type = io_req->cmd_type;
45 struct bnx2fc_rport *tgt = io_req->tgt;
48 BNX2FC_IO_DBG(io_req, "cmd_timeout, cmd_type = %d,"
49 "req_flags = %lx\n", cmd_type, io_req->req_flags);
51 spin_lock_bh(&tgt->tgt_lock);
52 if (test_and_clear_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags)) {
53 clear_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags);
55 * ideally we should hold the io_req until RRQ complets,
56 * and release io_req from timeout hold.
58 spin_unlock_bh(&tgt->tgt_lock);
59 bnx2fc_send_rrq(io_req);
62 if (test_and_clear_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags)) {
63 BNX2FC_IO_DBG(io_req, "IO ready for reuse now\n");
69 if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
70 &io_req->req_flags)) {
71 /* Handle eh_abort timeout */
72 BNX2FC_IO_DBG(io_req, "eh_abort timed out\n");
73 complete(&io_req->abts_done);
74 } else if (test_bit(BNX2FC_FLAG_ISSUE_ABTS,
75 &io_req->req_flags)) {
76 /* Handle internally generated ABTS timeout */
77 BNX2FC_IO_DBG(io_req, "ABTS timed out refcnt = %d\n",
78 kref_read(&io_req->refcount));
79 if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
80 &io_req->req_flags))) {
82 * Cleanup and return original command to
85 bnx2fc_initiate_cleanup(io_req);
86 kref_put(&io_req->refcount, bnx2fc_cmd_release);
87 spin_unlock_bh(&tgt->tgt_lock);
92 /* Hanlde IO timeout */
93 BNX2FC_IO_DBG(io_req, "IO timed out. issue ABTS\n");
94 if (test_and_set_bit(BNX2FC_FLAG_IO_COMPL,
95 &io_req->req_flags)) {
96 BNX2FC_IO_DBG(io_req, "IO completed before "
101 if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
102 &io_req->req_flags)) {
103 rc = bnx2fc_initiate_abts(io_req);
107 kref_put(&io_req->refcount, bnx2fc_cmd_release);
108 spin_unlock_bh(&tgt->tgt_lock);
112 BNX2FC_IO_DBG(io_req, "IO already in "
113 "ABTS processing\n");
119 if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags)) {
120 BNX2FC_IO_DBG(io_req, "ABTS for ELS timed out\n");
122 if (!test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
123 &io_req->req_flags)) {
124 kref_put(&io_req->refcount, bnx2fc_cmd_release);
125 spin_unlock_bh(&tgt->tgt_lock);
131 * Handle ELS timeout.
132 * tgt_lock is used to sync compl path and timeout
133 * path. If els compl path is processing this IO, we
134 * have nothing to do here, just release the timer hold
136 BNX2FC_IO_DBG(io_req, "ELS timed out\n");
137 if (test_and_set_bit(BNX2FC_FLAG_ELS_DONE,
141 /* Indicate the cb_func that this ELS is timed out */
142 set_bit(BNX2FC_FLAG_ELS_TIMEOUT, &io_req->req_flags);
144 if ((io_req->cb_func) && (io_req->cb_arg)) {
145 io_req->cb_func(io_req->cb_arg);
146 io_req->cb_arg = NULL;
151 printk(KERN_ERR PFX "cmd_timeout: invalid cmd_type %d\n",
157 /* release the cmd that was held when timer was set */
158 kref_put(&io_req->refcount, bnx2fc_cmd_release);
159 spin_unlock_bh(&tgt->tgt_lock);
162 static void bnx2fc_scsi_done(struct bnx2fc_cmd *io_req, int err_code)
164 /* Called with host lock held */
165 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
168 * active_cmd_queue may have other command types as well,
169 * and during flush operation, we want to error back only
172 if (io_req->cmd_type != BNX2FC_SCSI_CMD)
175 BNX2FC_IO_DBG(io_req, "scsi_done. err_code = 0x%x\n", err_code);
176 if (test_bit(BNX2FC_FLAG_CMD_LOST, &io_req->req_flags)) {
177 /* Do not call scsi done for this IO */
181 bnx2fc_unmap_sg_list(io_req);
182 io_req->sc_cmd = NULL;
184 /* Sanity checks before returning command to mid-layer */
186 printk(KERN_ERR PFX "scsi_done - sc_cmd NULL. "
187 "IO(0x%x) already cleaned up\n",
191 if (!sc_cmd->device) {
192 pr_err(PFX "0x%x: sc_cmd->device is NULL.\n", io_req->xid);
195 if (!sc_cmd->device->host) {
196 pr_err(PFX "0x%x: sc_cmd->device->host is NULL.\n",
201 sc_cmd->result = err_code << 16;
203 BNX2FC_IO_DBG(io_req, "sc=%p, result=0x%x, retries=%d, allowed=%d\n",
204 sc_cmd, host_byte(sc_cmd->result), sc_cmd->retries,
206 scsi_set_resid(sc_cmd, scsi_bufflen(sc_cmd));
207 bnx2fc_priv(sc_cmd)->io_req = NULL;
211 struct bnx2fc_cmd_mgr *bnx2fc_cmd_mgr_alloc(struct bnx2fc_hba *hba)
213 struct bnx2fc_cmd_mgr *cmgr;
214 struct io_bdt *bdt_info;
215 struct bnx2fc_cmd *io_req;
220 int num_ios, num_pri_ios;
222 int arr_sz = num_possible_cpus() + 1;
223 u16 min_xid = BNX2FC_MIN_XID;
224 u16 max_xid = hba->max_xid;
226 if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN) {
227 printk(KERN_ERR PFX "cmd_mgr_alloc: Invalid min_xid 0x%x \
228 and max_xid 0x%x\n", min_xid, max_xid);
231 BNX2FC_MISC_DBG("min xid 0x%x, max xid 0x%x\n", min_xid, max_xid);
233 num_ios = max_xid - min_xid + 1;
234 len = (num_ios * (sizeof(struct bnx2fc_cmd *)));
235 len += sizeof(struct bnx2fc_cmd_mgr);
237 cmgr = kzalloc(len, GFP_KERNEL);
239 printk(KERN_ERR PFX "failed to alloc cmgr\n");
244 cmgr->free_list = kcalloc(arr_sz, sizeof(*cmgr->free_list),
246 if (!cmgr->free_list) {
247 printk(KERN_ERR PFX "failed to alloc free_list\n");
251 cmgr->free_list_lock = kcalloc(arr_sz, sizeof(*cmgr->free_list_lock),
253 if (!cmgr->free_list_lock) {
254 printk(KERN_ERR PFX "failed to alloc free_list_lock\n");
255 kfree(cmgr->free_list);
256 cmgr->free_list = NULL;
260 cmgr->cmds = (struct bnx2fc_cmd **)(cmgr + 1);
262 for (i = 0; i < arr_sz; i++) {
263 INIT_LIST_HEAD(&cmgr->free_list[i]);
264 spin_lock_init(&cmgr->free_list_lock[i]);
268 * Pre-allocated pool of bnx2fc_cmds.
269 * Last entry in the free list array is the free list
270 * of slow path requests.
272 xid = BNX2FC_MIN_XID;
273 num_pri_ios = num_ios - hba->elstm_xids;
274 for (i = 0; i < num_ios; i++) {
275 io_req = kzalloc(sizeof(*io_req), GFP_KERNEL);
278 printk(KERN_ERR PFX "failed to alloc io_req\n");
282 INIT_LIST_HEAD(&io_req->link);
283 INIT_DELAYED_WORK(&io_req->timeout_work, bnx2fc_cmd_timeout);
287 list_add_tail(&io_req->link,
288 &cmgr->free_list[io_req->xid %
289 num_possible_cpus()]);
291 list_add_tail(&io_req->link,
292 &cmgr->free_list[num_possible_cpus()]);
296 /* Allocate pool of io_bdts - one for each bnx2fc_cmd */
297 mem_size = num_ios * sizeof(struct io_bdt *);
298 cmgr->io_bdt_pool = kzalloc(mem_size, GFP_KERNEL);
299 if (!cmgr->io_bdt_pool) {
300 printk(KERN_ERR PFX "failed to alloc io_bdt_pool\n");
304 mem_size = sizeof(struct io_bdt);
305 for (i = 0; i < num_ios; i++) {
306 cmgr->io_bdt_pool[i] = kmalloc(mem_size, GFP_KERNEL);
307 if (!cmgr->io_bdt_pool[i]) {
308 printk(KERN_ERR PFX "failed to alloc "
309 "io_bdt_pool[%d]\n", i);
314 /* Allocate an map fcoe_bdt_ctx structures */
315 bd_tbl_sz = BNX2FC_MAX_BDS_PER_CMD * sizeof(struct fcoe_bd_ctx);
316 for (i = 0; i < num_ios; i++) {
317 bdt_info = cmgr->io_bdt_pool[i];
318 bdt_info->bd_tbl = dma_alloc_coherent(&hba->pcidev->dev,
320 &bdt_info->bd_tbl_dma,
322 if (!bdt_info->bd_tbl) {
323 printk(KERN_ERR PFX "failed to alloc "
332 bnx2fc_cmd_mgr_free(cmgr);
336 void bnx2fc_cmd_mgr_free(struct bnx2fc_cmd_mgr *cmgr)
338 struct io_bdt *bdt_info;
339 struct bnx2fc_hba *hba = cmgr->hba;
341 u16 min_xid = BNX2FC_MIN_XID;
342 u16 max_xid = hba->max_xid;
346 num_ios = max_xid - min_xid + 1;
348 /* Free fcoe_bdt_ctx structures */
349 if (!cmgr->io_bdt_pool)
352 bd_tbl_sz = BNX2FC_MAX_BDS_PER_CMD * sizeof(struct fcoe_bd_ctx);
353 for (i = 0; i < num_ios; i++) {
354 bdt_info = cmgr->io_bdt_pool[i];
355 if (bdt_info->bd_tbl) {
356 dma_free_coherent(&hba->pcidev->dev, bd_tbl_sz,
358 bdt_info->bd_tbl_dma);
359 bdt_info->bd_tbl = NULL;
363 /* Destroy io_bdt pool */
364 for (i = 0; i < num_ios; i++) {
365 kfree(cmgr->io_bdt_pool[i]);
366 cmgr->io_bdt_pool[i] = NULL;
369 kfree(cmgr->io_bdt_pool);
370 cmgr->io_bdt_pool = NULL;
373 kfree(cmgr->free_list_lock);
375 /* Destroy cmd pool */
376 if (!cmgr->free_list)
379 for (i = 0; i < num_possible_cpus() + 1; i++) {
380 struct bnx2fc_cmd *tmp, *io_req;
382 list_for_each_entry_safe(io_req, tmp,
383 &cmgr->free_list[i], link) {
384 list_del(&io_req->link);
388 kfree(cmgr->free_list);
390 /* Free command manager itself */
394 struct bnx2fc_cmd *bnx2fc_elstm_alloc(struct bnx2fc_rport *tgt, int type)
396 struct fcoe_port *port = tgt->port;
397 struct bnx2fc_interface *interface = port->priv;
398 struct bnx2fc_cmd_mgr *cmd_mgr = interface->hba->cmd_mgr;
399 struct bnx2fc_cmd *io_req;
400 struct list_head *listp;
401 struct io_bdt *bd_tbl;
402 int index = RESERVE_FREE_LIST_INDEX;
407 max_sqes = tgt->max_sqes;
409 case BNX2FC_TASK_MGMT_CMD:
410 max_sqes = BNX2FC_TM_MAX_SQES;
413 max_sqes = BNX2FC_ELS_MAX_SQES;
420 * NOTE: Free list insertions and deletions are protected with
423 spin_lock_bh(&cmd_mgr->free_list_lock[index]);
424 free_sqes = atomic_read(&tgt->free_sqes);
425 if ((list_empty(&(cmd_mgr->free_list[index]))) ||
426 (tgt->num_active_ios.counter >= max_sqes) ||
427 (free_sqes + max_sqes <= BNX2FC_SQ_WQES_MAX)) {
428 BNX2FC_TGT_DBG(tgt, "No free els_tm cmds available "
429 "ios(%d):sqes(%d)\n",
430 tgt->num_active_ios.counter, tgt->max_sqes);
431 if (list_empty(&(cmd_mgr->free_list[index])))
432 printk(KERN_ERR PFX "elstm_alloc: list_empty\n");
433 spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
437 listp = (struct list_head *)
438 cmd_mgr->free_list[index].next;
439 list_del_init(listp);
440 io_req = (struct bnx2fc_cmd *) listp;
442 cmd_mgr->cmds[xid] = io_req;
443 atomic_inc(&tgt->num_active_ios);
444 atomic_dec(&tgt->free_sqes);
445 spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
447 INIT_LIST_HEAD(&io_req->link);
450 io_req->cmd_mgr = cmd_mgr;
451 io_req->req_flags = 0;
452 io_req->cmd_type = type;
454 /* Bind io_bdt for this io_req */
455 /* Have a static link between io_req and io_bdt_pool */
456 bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid];
457 bd_tbl->io_req = io_req;
459 /* Hold the io_req against deletion */
460 kref_init(&io_req->refcount);
464 struct bnx2fc_cmd *bnx2fc_cmd_alloc(struct bnx2fc_rport *tgt)
466 struct fcoe_port *port = tgt->port;
467 struct bnx2fc_interface *interface = port->priv;
468 struct bnx2fc_cmd_mgr *cmd_mgr = interface->hba->cmd_mgr;
469 struct bnx2fc_cmd *io_req;
470 struct list_head *listp;
471 struct io_bdt *bd_tbl;
475 int index = raw_smp_processor_id();
477 max_sqes = BNX2FC_SCSI_MAX_SQES;
479 * NOTE: Free list insertions and deletions are protected with
482 spin_lock_bh(&cmd_mgr->free_list_lock[index]);
483 free_sqes = atomic_read(&tgt->free_sqes);
484 if ((list_empty(&cmd_mgr->free_list[index])) ||
485 (tgt->num_active_ios.counter >= max_sqes) ||
486 (free_sqes + max_sqes <= BNX2FC_SQ_WQES_MAX)) {
487 spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
491 listp = (struct list_head *)
492 cmd_mgr->free_list[index].next;
493 list_del_init(listp);
494 io_req = (struct bnx2fc_cmd *) listp;
496 cmd_mgr->cmds[xid] = io_req;
497 atomic_inc(&tgt->num_active_ios);
498 atomic_dec(&tgt->free_sqes);
499 spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
501 INIT_LIST_HEAD(&io_req->link);
504 io_req->cmd_mgr = cmd_mgr;
505 io_req->req_flags = 0;
507 /* Bind io_bdt for this io_req */
508 /* Have a static link between io_req and io_bdt_pool */
509 bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid];
510 bd_tbl->io_req = io_req;
512 /* Hold the io_req against deletion */
513 kref_init(&io_req->refcount);
517 void bnx2fc_cmd_release(struct kref *ref)
519 struct bnx2fc_cmd *io_req = container_of(ref,
520 struct bnx2fc_cmd, refcount);
521 struct bnx2fc_cmd_mgr *cmd_mgr = io_req->cmd_mgr;
524 if (io_req->cmd_type == BNX2FC_SCSI_CMD)
525 index = io_req->xid % num_possible_cpus();
527 index = RESERVE_FREE_LIST_INDEX;
530 spin_lock_bh(&cmd_mgr->free_list_lock[index]);
531 if (io_req->cmd_type != BNX2FC_SCSI_CMD)
532 bnx2fc_free_mp_resc(io_req);
533 cmd_mgr->cmds[io_req->xid] = NULL;
534 /* Delete IO from retire queue */
535 list_del_init(&io_req->link);
536 /* Add it to the free list */
537 list_add(&io_req->link,
538 &cmd_mgr->free_list[index]);
539 atomic_dec(&io_req->tgt->num_active_ios);
540 spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
544 static void bnx2fc_free_mp_resc(struct bnx2fc_cmd *io_req)
546 struct bnx2fc_mp_req *mp_req = &(io_req->mp_req);
547 struct bnx2fc_interface *interface = io_req->port->priv;
548 struct bnx2fc_hba *hba = interface->hba;
549 size_t sz = sizeof(struct fcoe_bd_ctx);
552 mp_req->tm_flags = 0;
553 if (mp_req->mp_req_bd) {
554 dma_free_coherent(&hba->pcidev->dev, sz,
556 mp_req->mp_req_bd_dma);
557 mp_req->mp_req_bd = NULL;
559 if (mp_req->mp_resp_bd) {
560 dma_free_coherent(&hba->pcidev->dev, sz,
562 mp_req->mp_resp_bd_dma);
563 mp_req->mp_resp_bd = NULL;
565 if (mp_req->req_buf) {
566 dma_free_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
568 mp_req->req_buf_dma);
569 mp_req->req_buf = NULL;
571 if (mp_req->resp_buf) {
572 dma_free_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
574 mp_req->resp_buf_dma);
575 mp_req->resp_buf = NULL;
579 int bnx2fc_init_mp_req(struct bnx2fc_cmd *io_req)
581 struct bnx2fc_mp_req *mp_req;
582 struct fcoe_bd_ctx *mp_req_bd;
583 struct fcoe_bd_ctx *mp_resp_bd;
584 struct bnx2fc_interface *interface = io_req->port->priv;
585 struct bnx2fc_hba *hba = interface->hba;
589 mp_req = (struct bnx2fc_mp_req *)&(io_req->mp_req);
590 memset(mp_req, 0, sizeof(struct bnx2fc_mp_req));
592 if (io_req->cmd_type != BNX2FC_ELS) {
593 mp_req->req_len = sizeof(struct fcp_cmnd);
594 io_req->data_xfer_len = mp_req->req_len;
596 mp_req->req_len = io_req->data_xfer_len;
598 mp_req->req_buf = dma_alloc_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
599 &mp_req->req_buf_dma,
601 if (!mp_req->req_buf) {
602 printk(KERN_ERR PFX "unable to alloc MP req buffer\n");
603 bnx2fc_free_mp_resc(io_req);
607 mp_req->resp_buf = dma_alloc_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
608 &mp_req->resp_buf_dma,
610 if (!mp_req->resp_buf) {
611 printk(KERN_ERR PFX "unable to alloc TM resp buffer\n");
612 bnx2fc_free_mp_resc(io_req);
615 memset(mp_req->req_buf, 0, CNIC_PAGE_SIZE);
616 memset(mp_req->resp_buf, 0, CNIC_PAGE_SIZE);
618 /* Allocate and map mp_req_bd and mp_resp_bd */
619 sz = sizeof(struct fcoe_bd_ctx);
620 mp_req->mp_req_bd = dma_alloc_coherent(&hba->pcidev->dev, sz,
621 &mp_req->mp_req_bd_dma,
623 if (!mp_req->mp_req_bd) {
624 printk(KERN_ERR PFX "unable to alloc MP req bd\n");
625 bnx2fc_free_mp_resc(io_req);
628 mp_req->mp_resp_bd = dma_alloc_coherent(&hba->pcidev->dev, sz,
629 &mp_req->mp_resp_bd_dma,
631 if (!mp_req->mp_resp_bd) {
632 printk(KERN_ERR PFX "unable to alloc MP resp bd\n");
633 bnx2fc_free_mp_resc(io_req);
637 addr = mp_req->req_buf_dma;
638 mp_req_bd = mp_req->mp_req_bd;
639 mp_req_bd->buf_addr_lo = (u32)addr & 0xffffffff;
640 mp_req_bd->buf_addr_hi = (u32)((u64)addr >> 32);
641 mp_req_bd->buf_len = CNIC_PAGE_SIZE;
642 mp_req_bd->flags = 0;
645 * MP buffer is either a task mgmt command or an ELS.
646 * So the assumption is that it consumes a single bd
647 * entry in the bd table
649 mp_resp_bd = mp_req->mp_resp_bd;
650 addr = mp_req->resp_buf_dma;
651 mp_resp_bd->buf_addr_lo = (u32)addr & 0xffffffff;
652 mp_resp_bd->buf_addr_hi = (u32)((u64)addr >> 32);
653 mp_resp_bd->buf_len = CNIC_PAGE_SIZE;
654 mp_resp_bd->flags = 0;
659 static int bnx2fc_initiate_tmf(struct scsi_cmnd *sc_cmd, u8 tm_flags)
661 struct fc_lport *lport;
662 struct fc_rport *rport;
663 struct fc_rport_libfc_priv *rp;
664 struct fcoe_port *port;
665 struct bnx2fc_interface *interface;
666 struct bnx2fc_rport *tgt;
667 struct bnx2fc_cmd *io_req;
668 struct bnx2fc_mp_req *tm_req;
669 struct fcoe_task_ctx_entry *task;
670 struct fcoe_task_ctx_entry *task_page;
671 struct Scsi_Host *host = sc_cmd->device->host;
672 struct fc_frame_header *fc_hdr;
673 struct fcp_cmnd *fcp_cmnd;
678 unsigned long start = jiffies;
680 lport = shost_priv(host);
681 rport = starget_to_rport(scsi_target(sc_cmd->device));
682 port = lport_priv(lport);
683 interface = port->priv;
686 printk(KERN_ERR PFX "device_reset: rport is NULL\n");
692 rc = fc_block_scsi_eh(sc_cmd);
696 if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
697 printk(KERN_ERR PFX "device_reset: link is not ready\n");
701 /* rport and tgt are allocated together, so tgt should be non-NULL */
702 tgt = (struct bnx2fc_rport *)&rp[1];
704 if (!(test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
705 printk(KERN_ERR PFX "device_reset: tgt not offloaded\n");
710 io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_TASK_MGMT_CMD);
712 if (time_after(jiffies, start + HZ)) {
713 printk(KERN_ERR PFX "tmf: Failed TMF");
720 /* Initialize rest of io_req fields */
721 io_req->sc_cmd = sc_cmd;
725 tm_req = (struct bnx2fc_mp_req *)&(io_req->mp_req);
727 rc = bnx2fc_init_mp_req(io_req);
729 printk(KERN_ERR PFX "Task mgmt MP request init failed\n");
730 spin_lock_bh(&tgt->tgt_lock);
731 kref_put(&io_req->refcount, bnx2fc_cmd_release);
732 spin_unlock_bh(&tgt->tgt_lock);
737 io_req->io_req_flags = 0;
738 tm_req->tm_flags = tm_flags;
741 bnx2fc_build_fcp_cmnd(io_req, (struct fcp_cmnd *)tm_req->req_buf);
742 fcp_cmnd = (struct fcp_cmnd *)tm_req->req_buf;
743 memset(fcp_cmnd->fc_cdb, 0, sc_cmd->cmd_len);
747 fc_hdr = &(tm_req->req_fc_hdr);
749 did = rport->port_id;
750 __fc_fill_fc_hdr(fc_hdr, FC_RCTL_DD_UNSOL_CMD, did, sid,
751 FC_TYPE_FCP, FC_FC_FIRST_SEQ | FC_FC_END_SEQ |
753 /* Obtain exchange id */
756 BNX2FC_TGT_DBG(tgt, "Initiate TMF - xid = 0x%x\n", xid);
757 task_idx = xid/BNX2FC_TASKS_PER_PAGE;
758 index = xid % BNX2FC_TASKS_PER_PAGE;
760 /* Initialize task context for this IO request */
761 task_page = (struct fcoe_task_ctx_entry *)
762 interface->hba->task_ctx[task_idx];
763 task = &(task_page[index]);
764 bnx2fc_init_mp_task(io_req, task);
766 bnx2fc_priv(sc_cmd)->io_req = io_req;
768 /* Obtain free SQ entry */
769 spin_lock_bh(&tgt->tgt_lock);
770 bnx2fc_add_2_sq(tgt, xid);
772 /* Enqueue the io_req to active_tm_queue */
773 io_req->on_tmf_queue = 1;
774 list_add_tail(&io_req->link, &tgt->active_tm_queue);
776 init_completion(&io_req->abts_done);
777 io_req->wait_for_abts_comp = 1;
780 bnx2fc_ring_doorbell(tgt);
781 spin_unlock_bh(&tgt->tgt_lock);
783 rc = wait_for_completion_timeout(&io_req->abts_done,
784 interface->tm_timeout * HZ);
785 spin_lock_bh(&tgt->tgt_lock);
787 io_req->wait_for_abts_comp = 0;
788 if (!(test_bit(BNX2FC_FLAG_TM_COMPL, &io_req->req_flags))) {
789 set_bit(BNX2FC_FLAG_TM_TIMEOUT, &io_req->req_flags);
790 if (io_req->on_tmf_queue) {
791 list_del_init(&io_req->link);
792 io_req->on_tmf_queue = 0;
794 io_req->wait_for_cleanup_comp = 1;
795 init_completion(&io_req->cleanup_done);
796 bnx2fc_initiate_cleanup(io_req);
797 spin_unlock_bh(&tgt->tgt_lock);
798 rc = wait_for_completion_timeout(&io_req->cleanup_done,
800 spin_lock_bh(&tgt->tgt_lock);
801 io_req->wait_for_cleanup_comp = 0;
803 kref_put(&io_req->refcount, bnx2fc_cmd_release);
806 spin_unlock_bh(&tgt->tgt_lock);
809 BNX2FC_TGT_DBG(tgt, "task mgmt command failed...\n");
812 BNX2FC_TGT_DBG(tgt, "task mgmt command success...\n");
819 int bnx2fc_initiate_abts(struct bnx2fc_cmd *io_req)
821 struct fc_lport *lport;
822 struct bnx2fc_rport *tgt = io_req->tgt;
823 struct fc_rport *rport = tgt->rport;
824 struct fc_rport_priv *rdata = tgt->rdata;
825 struct bnx2fc_interface *interface;
826 struct fcoe_port *port;
827 struct bnx2fc_cmd *abts_io_req;
828 struct fcoe_task_ctx_entry *task;
829 struct fcoe_task_ctx_entry *task_page;
830 struct fc_frame_header *fc_hdr;
831 struct bnx2fc_mp_req *abts_req;
836 u32 r_a_tov = rdata->r_a_tov;
838 /* called with tgt_lock held */
839 BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_abts\n");
842 interface = port->priv;
845 if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
846 printk(KERN_ERR PFX "initiate_abts: tgt not offloaded\n");
852 printk(KERN_ERR PFX "initiate_abts: rport is NULL\n");
857 if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
858 printk(KERN_ERR PFX "initiate_abts: link is not ready\n");
863 abts_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_ABTS);
865 printk(KERN_ERR PFX "abts: couldn't allocate cmd\n");
870 /* Initialize rest of io_req fields */
871 abts_io_req->sc_cmd = NULL;
872 abts_io_req->port = port;
873 abts_io_req->tgt = tgt;
874 abts_io_req->data_xfer_len = 0; /* No data transfer for ABTS */
876 abts_req = (struct bnx2fc_mp_req *)&(abts_io_req->mp_req);
877 memset(abts_req, 0, sizeof(struct bnx2fc_mp_req));
880 fc_hdr = &(abts_req->req_fc_hdr);
882 /* Obtain oxid and rxid for the original exchange to be aborted */
883 fc_hdr->fh_ox_id = htons(io_req->xid);
884 fc_hdr->fh_rx_id = htons(io_req->task->rxwr_txrd.var_ctx.rx_id);
887 did = rport->port_id;
889 __fc_fill_fc_hdr(fc_hdr, FC_RCTL_BA_ABTS, did, sid,
890 FC_TYPE_BLS, FC_FC_FIRST_SEQ | FC_FC_END_SEQ |
893 xid = abts_io_req->xid;
894 BNX2FC_IO_DBG(abts_io_req, "ABTS io_req\n");
895 task_idx = xid/BNX2FC_TASKS_PER_PAGE;
896 index = xid % BNX2FC_TASKS_PER_PAGE;
898 /* Initialize task context for this IO request */
899 task_page = (struct fcoe_task_ctx_entry *)
900 interface->hba->task_ctx[task_idx];
901 task = &(task_page[index]);
902 bnx2fc_init_mp_task(abts_io_req, task);
905 * ABTS task is a temporary task that will be cleaned up
906 * irrespective of ABTS response. We need to start the timer
907 * for the original exchange, as the CQE is posted for the original
910 * Timer for ABTS is started only when it is originated by a
911 * TM request. For the ABTS issued as part of ULP timeout,
912 * scsi-ml maintains the timers.
915 /* if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags))*/
916 bnx2fc_cmd_timer_set(io_req, 2 * r_a_tov);
918 /* Obtain free SQ entry */
919 bnx2fc_add_2_sq(tgt, xid);
922 bnx2fc_ring_doorbell(tgt);
928 int bnx2fc_initiate_seq_cleanup(struct bnx2fc_cmd *orig_io_req, u32 offset,
931 struct bnx2fc_rport *tgt = orig_io_req->tgt;
932 struct bnx2fc_interface *interface;
933 struct fcoe_port *port;
934 struct bnx2fc_cmd *seq_clnp_req;
935 struct fcoe_task_ctx_entry *task;
936 struct fcoe_task_ctx_entry *task_page;
937 struct bnx2fc_els_cb_arg *cb_arg = NULL;
942 BNX2FC_IO_DBG(orig_io_req, "bnx2fc_initiate_seq_cleanup xid = 0x%x\n",
944 kref_get(&orig_io_req->refcount);
946 port = orig_io_req->port;
947 interface = port->priv;
949 cb_arg = kzalloc(sizeof(struct bnx2fc_els_cb_arg), GFP_ATOMIC);
951 printk(KERN_ERR PFX "Unable to alloc cb_arg for seq clnup\n");
956 seq_clnp_req = bnx2fc_elstm_alloc(tgt, BNX2FC_SEQ_CLEANUP);
958 printk(KERN_ERR PFX "cleanup: couldn't allocate cmd\n");
963 /* Initialize rest of io_req fields */
964 seq_clnp_req->sc_cmd = NULL;
965 seq_clnp_req->port = port;
966 seq_clnp_req->tgt = tgt;
967 seq_clnp_req->data_xfer_len = 0; /* No data transfer for cleanup */
969 xid = seq_clnp_req->xid;
971 task_idx = xid/BNX2FC_TASKS_PER_PAGE;
972 index = xid % BNX2FC_TASKS_PER_PAGE;
974 /* Initialize task context for this IO request */
975 task_page = (struct fcoe_task_ctx_entry *)
976 interface->hba->task_ctx[task_idx];
977 task = &(task_page[index]);
978 cb_arg->aborted_io_req = orig_io_req;
979 cb_arg->io_req = seq_clnp_req;
980 cb_arg->r_ctl = r_ctl;
981 cb_arg->offset = offset;
982 seq_clnp_req->cb_arg = cb_arg;
984 printk(KERN_ERR PFX "call init_seq_cleanup_task\n");
985 bnx2fc_init_seq_cleanup_task(seq_clnp_req, task, orig_io_req, offset);
987 /* Obtain free SQ entry */
988 bnx2fc_add_2_sq(tgt, xid);
991 bnx2fc_ring_doorbell(tgt);
996 int bnx2fc_initiate_cleanup(struct bnx2fc_cmd *io_req)
998 struct bnx2fc_rport *tgt = io_req->tgt;
999 struct bnx2fc_interface *interface;
1000 struct fcoe_port *port;
1001 struct bnx2fc_cmd *cleanup_io_req;
1002 struct fcoe_task_ctx_entry *task;
1003 struct fcoe_task_ctx_entry *task_page;
1004 int task_idx, index;
1008 /* ASSUMPTION: called with tgt_lock held */
1009 BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_cleanup\n");
1011 port = io_req->port;
1012 interface = port->priv;
1014 cleanup_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_CLEANUP);
1015 if (!cleanup_io_req) {
1016 printk(KERN_ERR PFX "cleanup: couldn't allocate cmd\n");
1021 /* Initialize rest of io_req fields */
1022 cleanup_io_req->sc_cmd = NULL;
1023 cleanup_io_req->port = port;
1024 cleanup_io_req->tgt = tgt;
1025 cleanup_io_req->data_xfer_len = 0; /* No data transfer for cleanup */
1027 xid = cleanup_io_req->xid;
1029 task_idx = xid/BNX2FC_TASKS_PER_PAGE;
1030 index = xid % BNX2FC_TASKS_PER_PAGE;
1032 /* Initialize task context for this IO request */
1033 task_page = (struct fcoe_task_ctx_entry *)
1034 interface->hba->task_ctx[task_idx];
1035 task = &(task_page[index]);
1036 orig_xid = io_req->xid;
1038 BNX2FC_IO_DBG(io_req, "CLEANUP io_req xid = 0x%x\n", xid);
1040 bnx2fc_init_cleanup_task(cleanup_io_req, task, orig_xid);
1042 /* Obtain free SQ entry */
1043 bnx2fc_add_2_sq(tgt, xid);
1045 /* Set flag that cleanup request is pending with the firmware */
1046 set_bit(BNX2FC_FLAG_ISSUE_CLEANUP_REQ, &io_req->req_flags);
1049 bnx2fc_ring_doorbell(tgt);
1056 * bnx2fc_eh_target_reset: Reset a target
1058 * @sc_cmd: SCSI command
1060 * Set from SCSI host template to send task mgmt command to the target
1061 * and wait for the response
1063 int bnx2fc_eh_target_reset(struct scsi_cmnd *sc_cmd)
1065 return bnx2fc_initiate_tmf(sc_cmd, FCP_TMF_TGT_RESET);
1069 * bnx2fc_eh_device_reset - Reset a single LUN
1071 * @sc_cmd: SCSI command
1073 * Set from SCSI host template to send task mgmt command to the target
1074 * and wait for the response
1076 int bnx2fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
1078 return bnx2fc_initiate_tmf(sc_cmd, FCP_TMF_LUN_RESET);
1081 static int bnx2fc_abts_cleanup(struct bnx2fc_cmd *io_req)
1082 __must_hold(&tgt->tgt_lock)
1084 struct bnx2fc_rport *tgt = io_req->tgt;
1085 unsigned int time_left;
1087 init_completion(&io_req->cleanup_done);
1088 io_req->wait_for_cleanup_comp = 1;
1089 bnx2fc_initiate_cleanup(io_req);
1091 spin_unlock_bh(&tgt->tgt_lock);
1094 * Can't wait forever on cleanup response lest we let the SCSI error
1095 * handler wait forever
1097 time_left = wait_for_completion_timeout(&io_req->cleanup_done,
1100 BNX2FC_IO_DBG(io_req, "%s(): Wait for cleanup timed out.\n",
1104 * Put the extra reference to the SCSI command since it would
1105 * not have been returned in this case.
1107 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1110 spin_lock_bh(&tgt->tgt_lock);
1111 io_req->wait_for_cleanup_comp = 0;
1116 * bnx2fc_eh_abort - eh_abort_handler api to abort an outstanding
1119 * @sc_cmd: SCSI_ML command pointer
1121 * SCSI abort request handler
1123 int bnx2fc_eh_abort(struct scsi_cmnd *sc_cmd)
1125 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1126 struct fc_rport_libfc_priv *rp = rport->dd_data;
1127 struct bnx2fc_cmd *io_req;
1128 struct fc_lport *lport;
1129 struct bnx2fc_rport *tgt;
1131 unsigned int time_left;
1133 rc = fc_block_scsi_eh(sc_cmd);
1137 lport = shost_priv(sc_cmd->device->host);
1138 if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
1139 printk(KERN_ERR PFX "eh_abort: link not ready\n");
1143 tgt = (struct bnx2fc_rport *)&rp[1];
1145 BNX2FC_TGT_DBG(tgt, "Entered bnx2fc_eh_abort\n");
1147 spin_lock_bh(&tgt->tgt_lock);
1148 io_req = bnx2fc_priv(sc_cmd)->io_req;
1150 /* Command might have just completed */
1151 printk(KERN_ERR PFX "eh_abort: io_req is NULL\n");
1152 spin_unlock_bh(&tgt->tgt_lock);
1155 BNX2FC_IO_DBG(io_req, "eh_abort - refcnt = %d\n",
1156 kref_read(&io_req->refcount));
1158 /* Hold IO request across abort processing */
1159 kref_get(&io_req->refcount);
1161 BUG_ON(tgt != io_req->tgt);
1163 /* Remove the io_req from the active_q. */
1165 * Task Mgmt functions (LUN RESET & TGT RESET) will not
1166 * issue an ABTS on this particular IO req, as the
1167 * io_req is no longer in the active_q.
1169 if (tgt->flush_in_prog) {
1170 printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
1171 "flush in progress\n", io_req->xid);
1172 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1173 spin_unlock_bh(&tgt->tgt_lock);
1177 if (io_req->on_active_queue == 0) {
1178 printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
1179 "not on active_q\n", io_req->xid);
1181 * The IO is still with the FW.
1182 * Return failure and let SCSI-ml retry eh_abort.
1184 spin_unlock_bh(&tgt->tgt_lock);
1189 * Only eh_abort processing will remove the IO from
1190 * active_cmd_q before processing the request. this is
1191 * done to avoid race conditions between IOs aborted
1192 * as part of task management completion and eh_abort
1195 list_del_init(&io_req->link);
1196 io_req->on_active_queue = 0;
1197 /* Move IO req to retire queue */
1198 list_add_tail(&io_req->link, &tgt->io_retire_queue);
1200 init_completion(&io_req->abts_done);
1201 init_completion(&io_req->cleanup_done);
1203 if (test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags)) {
1204 printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
1205 "already in abts processing\n", io_req->xid);
1206 if (cancel_delayed_work(&io_req->timeout_work))
1207 kref_put(&io_req->refcount,
1208 bnx2fc_cmd_release); /* drop timer hold */
1210 * We don't want to hold off the upper layer timer so simply
1211 * cleanup the command and return that I/O was successfully
1214 bnx2fc_abts_cleanup(io_req);
1215 /* This only occurs when an task abort was requested while ABTS
1216 is in progress. Setting the IO_CLEANUP flag will skip the
1217 RRQ process in the case when the fw generated SCSI_CMD cmpl
1218 was a result from the ABTS request rather than the CLEANUP
1220 set_bit(BNX2FC_FLAG_IO_CLEANUP, &io_req->req_flags);
1225 /* Cancel the current timer running on this io_req */
1226 if (cancel_delayed_work(&io_req->timeout_work))
1227 kref_put(&io_req->refcount,
1228 bnx2fc_cmd_release); /* drop timer hold */
1229 set_bit(BNX2FC_FLAG_EH_ABORT, &io_req->req_flags);
1230 io_req->wait_for_abts_comp = 1;
1231 rc = bnx2fc_initiate_abts(io_req);
1233 io_req->wait_for_cleanup_comp = 1;
1234 bnx2fc_initiate_cleanup(io_req);
1235 spin_unlock_bh(&tgt->tgt_lock);
1236 wait_for_completion(&io_req->cleanup_done);
1237 spin_lock_bh(&tgt->tgt_lock);
1238 io_req->wait_for_cleanup_comp = 0;
1241 spin_unlock_bh(&tgt->tgt_lock);
1243 /* Wait 2 * RA_TOV + 1 to be sure timeout function hasn't fired */
1244 time_left = wait_for_completion_timeout(&io_req->abts_done,
1245 msecs_to_jiffies(2 * rp->r_a_tov + 1));
1247 BNX2FC_IO_DBG(io_req,
1248 "Timed out in eh_abort waiting for abts_done");
1250 spin_lock_bh(&tgt->tgt_lock);
1251 io_req->wait_for_abts_comp = 0;
1252 if (test_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags)) {
1253 BNX2FC_IO_DBG(io_req, "IO completed in a different context\n");
1255 } else if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
1256 &io_req->req_flags))) {
1257 /* Let the scsi-ml try to recover this command */
1258 printk(KERN_ERR PFX "abort failed, xid = 0x%x\n",
1261 * Cleanup firmware residuals before returning control back
1264 rc = bnx2fc_abts_cleanup(io_req);
1268 * We come here even when there was a race condition
1269 * between timeout and abts completion, and abts
1270 * completion happens just in time.
1272 BNX2FC_IO_DBG(io_req, "abort succeeded\n");
1274 bnx2fc_scsi_done(io_req, DID_ABORT);
1275 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1278 /* release the reference taken in eh_abort */
1279 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1280 spin_unlock_bh(&tgt->tgt_lock);
1284 void bnx2fc_process_seq_cleanup_compl(struct bnx2fc_cmd *seq_clnp_req,
1285 struct fcoe_task_ctx_entry *task,
1288 struct bnx2fc_els_cb_arg *cb_arg = seq_clnp_req->cb_arg;
1289 struct bnx2fc_cmd *orig_io_req = cb_arg->aborted_io_req;
1290 u32 offset = cb_arg->offset;
1291 enum fc_rctl r_ctl = cb_arg->r_ctl;
1293 struct bnx2fc_rport *tgt = orig_io_req->tgt;
1295 BNX2FC_IO_DBG(orig_io_req, "Entered process_cleanup_compl xid = 0x%x"
1297 seq_clnp_req->xid, seq_clnp_req->cmd_type);
1299 if (rx_state == FCOE_TASK_RX_STATE_IGNORED_SEQUENCE_CLEANUP) {
1300 printk(KERN_ERR PFX "seq cleanup ignored - xid = 0x%x\n",
1305 spin_unlock_bh(&tgt->tgt_lock);
1306 rc = bnx2fc_send_srr(orig_io_req, offset, r_ctl);
1307 spin_lock_bh(&tgt->tgt_lock);
1310 printk(KERN_ERR PFX "clnup_compl: Unable to send SRR"
1311 " IO will abort\n");
1312 seq_clnp_req->cb_arg = NULL;
1313 kref_put(&orig_io_req->refcount, bnx2fc_cmd_release);
1319 void bnx2fc_process_cleanup_compl(struct bnx2fc_cmd *io_req,
1320 struct fcoe_task_ctx_entry *task,
1323 BNX2FC_IO_DBG(io_req, "Entered process_cleanup_compl "
1324 "refcnt = %d, cmd_type = %d\n",
1325 kref_read(&io_req->refcount), io_req->cmd_type);
1327 * Test whether there is a cleanup request pending. If not just
1330 if (!test_and_clear_bit(BNX2FC_FLAG_ISSUE_CLEANUP_REQ,
1331 &io_req->req_flags))
1334 * If we receive a cleanup completion for this request then the
1335 * firmware will not give us an abort completion for this request
1336 * so clear any ABTS pending flags.
1338 if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags) &&
1339 !test_bit(BNX2FC_FLAG_ABTS_DONE, &io_req->req_flags)) {
1340 set_bit(BNX2FC_FLAG_ABTS_DONE, &io_req->req_flags);
1341 if (io_req->wait_for_abts_comp)
1342 complete(&io_req->abts_done);
1345 bnx2fc_scsi_done(io_req, DID_ERROR);
1346 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1347 if (io_req->wait_for_cleanup_comp)
1348 complete(&io_req->cleanup_done);
1351 void bnx2fc_process_abts_compl(struct bnx2fc_cmd *io_req,
1352 struct fcoe_task_ctx_entry *task,
1356 u32 r_a_tov = FC_DEF_R_A_TOV;
1358 struct bnx2fc_rport *tgt = io_req->tgt;
1360 BNX2FC_IO_DBG(io_req, "Entered process_abts_compl xid = 0x%x"
1361 "refcnt = %d, cmd_type = %d\n",
1363 kref_read(&io_req->refcount), io_req->cmd_type);
1365 if (test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
1366 &io_req->req_flags)) {
1367 BNX2FC_IO_DBG(io_req, "Timer context finished processing"
1373 * If we receive an ABTS completion here then we will not receive
1374 * a cleanup completion so clear any cleanup pending flags.
1376 if (test_bit(BNX2FC_FLAG_ISSUE_CLEANUP_REQ, &io_req->req_flags)) {
1377 clear_bit(BNX2FC_FLAG_ISSUE_CLEANUP_REQ, &io_req->req_flags);
1378 if (io_req->wait_for_cleanup_comp)
1379 complete(&io_req->cleanup_done);
1382 /* Do not issue RRQ as this IO is already cleanedup */
1383 if (test_and_set_bit(BNX2FC_FLAG_IO_CLEANUP,
1384 &io_req->req_flags))
1388 * For ABTS issued due to SCSI eh_abort_handler, timeout
1389 * values are maintained by scsi-ml itself. Cancel timeout
1390 * in case ABTS issued as part of task management function
1391 * or due to FW error.
1393 if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags))
1394 if (cancel_delayed_work(&io_req->timeout_work))
1395 kref_put(&io_req->refcount,
1396 bnx2fc_cmd_release); /* drop timer hold */
1398 r_ctl = (u8)task->rxwr_only.union_ctx.comp_info.abts_rsp.r_ctl;
1401 case FC_RCTL_BA_ACC:
1403 * Dont release this cmd yet. It will be relesed
1404 * after we get RRQ response
1406 BNX2FC_IO_DBG(io_req, "ABTS response - ACC Send RRQ\n");
1410 case FC_RCTL_BA_RJT:
1411 BNX2FC_IO_DBG(io_req, "ABTS response - RJT\n");
1414 printk(KERN_ERR PFX "Unknown ABTS response\n");
1419 BNX2FC_IO_DBG(io_req, "Issue RRQ after R_A_TOV\n");
1420 set_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags);
1422 set_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags);
1423 bnx2fc_cmd_timer_set(io_req, r_a_tov);
1426 if (io_req->wait_for_abts_comp) {
1427 if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
1428 &io_req->req_flags))
1429 complete(&io_req->abts_done);
1432 * We end up here when ABTS is issued as
1433 * in asynchronous context, i.e., as part
1434 * of task management completion, or
1435 * when FW error is received or when the
1436 * ABTS is issued when the IO is timed
1440 if (io_req->on_active_queue) {
1441 list_del_init(&io_req->link);
1442 io_req->on_active_queue = 0;
1443 /* Move IO req to retire queue */
1444 list_add_tail(&io_req->link, &tgt->io_retire_queue);
1446 bnx2fc_scsi_done(io_req, DID_ERROR);
1447 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1451 static void bnx2fc_lun_reset_cmpl(struct bnx2fc_cmd *io_req)
1453 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1454 struct bnx2fc_rport *tgt = io_req->tgt;
1455 struct bnx2fc_cmd *cmd, *tmp;
1456 u64 tm_lun = sc_cmd->device->lun;
1460 /* called with tgt_lock held */
1461 BNX2FC_IO_DBG(io_req, "Entered bnx2fc_lun_reset_cmpl\n");
1463 * Walk thru the active_ios queue and ABORT the IO
1464 * that matches with the LUN that was reset
1466 list_for_each_entry_safe(cmd, tmp, &tgt->active_cmd_queue, link) {
1467 BNX2FC_TGT_DBG(tgt, "LUN RST cmpl: scan for pending IOs\n");
1468 lun = cmd->sc_cmd->device->lun;
1469 if (lun == tm_lun) {
1470 /* Initiate ABTS on this cmd */
1471 if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
1473 /* cancel the IO timeout */
1474 if (cancel_delayed_work(&io_req->timeout_work))
1475 kref_put(&io_req->refcount,
1476 bnx2fc_cmd_release);
1478 rc = bnx2fc_initiate_abts(cmd);
1479 /* abts shouldn't fail in this context */
1480 WARN_ON(rc != SUCCESS);
1482 printk(KERN_ERR PFX "lun_rst: abts already in"
1483 " progress for this IO 0x%x\n",
1489 static void bnx2fc_tgt_reset_cmpl(struct bnx2fc_cmd *io_req)
1491 struct bnx2fc_rport *tgt = io_req->tgt;
1492 struct bnx2fc_cmd *cmd, *tmp;
1495 /* called with tgt_lock held */
1496 BNX2FC_IO_DBG(io_req, "Entered bnx2fc_tgt_reset_cmpl\n");
1498 * Walk thru the active_ios queue and ABORT the IO
1499 * that matches with the LUN that was reset
1501 list_for_each_entry_safe(cmd, tmp, &tgt->active_cmd_queue, link) {
1502 BNX2FC_TGT_DBG(tgt, "TGT RST cmpl: scan for pending IOs\n");
1504 if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
1506 /* cancel the IO timeout */
1507 if (cancel_delayed_work(&io_req->timeout_work))
1508 kref_put(&io_req->refcount,
1509 bnx2fc_cmd_release); /* timer hold */
1510 rc = bnx2fc_initiate_abts(cmd);
1511 /* abts shouldn't fail in this context */
1512 WARN_ON(rc != SUCCESS);
1515 printk(KERN_ERR PFX "tgt_rst: abts already in progress"
1516 " for this IO 0x%x\n", cmd->xid);
1520 void bnx2fc_process_tm_compl(struct bnx2fc_cmd *io_req,
1521 struct fcoe_task_ctx_entry *task, u8 num_rq,
1522 unsigned char *rq_data)
1524 struct bnx2fc_mp_req *tm_req;
1525 struct fc_frame_header *fc_hdr;
1526 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1531 /* Called with tgt_lock held */
1532 BNX2FC_IO_DBG(io_req, "Entered process_tm_compl\n");
1534 if (!(test_bit(BNX2FC_FLAG_TM_TIMEOUT, &io_req->req_flags)))
1535 set_bit(BNX2FC_FLAG_TM_COMPL, &io_req->req_flags);
1537 /* TM has already timed out and we got
1538 * delayed completion. Ignore completion
1544 tm_req = &(io_req->mp_req);
1545 fc_hdr = &(tm_req->resp_fc_hdr);
1546 hdr = (u64 *)fc_hdr;
1548 &task->rxwr_only.union_ctx.comp_info.mp_rsp.fc_hdr;
1549 hdr[0] = cpu_to_be64(temp_hdr[0]);
1550 hdr[1] = cpu_to_be64(temp_hdr[1]);
1551 hdr[2] = cpu_to_be64(temp_hdr[2]);
1554 task->rxwr_only.union_ctx.comp_info.mp_rsp.mp_payload_len;
1556 rsp_buf = tm_req->resp_buf;
1558 if (fc_hdr->fh_r_ctl == FC_RCTL_DD_CMD_STATUS) {
1559 bnx2fc_parse_fcp_rsp(io_req,
1560 (struct fcoe_fcp_rsp_payload *)
1561 rsp_buf, num_rq, rq_data);
1562 if (io_req->fcp_rsp_code == 0) {
1564 if (tm_req->tm_flags & FCP_TMF_LUN_RESET)
1565 bnx2fc_lun_reset_cmpl(io_req);
1566 else if (tm_req->tm_flags & FCP_TMF_TGT_RESET)
1567 bnx2fc_tgt_reset_cmpl(io_req);
1570 printk(KERN_ERR PFX "tmf's fc_hdr r_ctl = 0x%x\n",
1573 if (!bnx2fc_priv(sc_cmd)->io_req) {
1574 printk(KERN_ERR PFX "tm_compl: io_req is NULL\n");
1577 switch (io_req->fcp_status) {
1579 if (io_req->cdb_status == 0) {
1580 /* Good IO completion */
1581 sc_cmd->result = DID_OK << 16;
1583 /* Transport status is good, SCSI status not good */
1584 sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
1586 if (io_req->fcp_resid)
1587 scsi_set_resid(sc_cmd, io_req->fcp_resid);
1591 BNX2FC_IO_DBG(io_req, "process_tm_compl: fcp_status = %d\n",
1592 io_req->fcp_status);
1596 sc_cmd = io_req->sc_cmd;
1597 io_req->sc_cmd = NULL;
1599 /* check if the io_req exists in tgt's tmf_q */
1600 if (io_req->on_tmf_queue) {
1602 list_del_init(&io_req->link);
1603 io_req->on_tmf_queue = 0;
1606 printk(KERN_ERR PFX "Command not on active_cmd_queue!\n");
1610 bnx2fc_priv(sc_cmd)->io_req = NULL;
1613 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1614 if (io_req->wait_for_abts_comp) {
1615 BNX2FC_IO_DBG(io_req, "tm_compl - wake up the waiter\n");
1616 complete(&io_req->abts_done);
1620 static int bnx2fc_split_bd(struct bnx2fc_cmd *io_req, u64 addr, int sg_len,
1623 struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
1624 int frag_size, sg_frags;
1628 if (sg_len >= BNX2FC_BD_SPLIT_SZ)
1629 frag_size = BNX2FC_BD_SPLIT_SZ;
1632 bd[bd_index + sg_frags].buf_addr_lo = addr & 0xffffffff;
1633 bd[bd_index + sg_frags].buf_addr_hi = addr >> 32;
1634 bd[bd_index + sg_frags].buf_len = (u16)frag_size;
1635 bd[bd_index + sg_frags].flags = 0;
1637 addr += (u64) frag_size;
1639 sg_len -= frag_size;
1645 static int bnx2fc_map_sg(struct bnx2fc_cmd *io_req)
1647 struct bnx2fc_interface *interface = io_req->port->priv;
1648 struct bnx2fc_hba *hba = interface->hba;
1649 struct scsi_cmnd *sc = io_req->sc_cmd;
1650 struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
1651 struct scatterlist *sg;
1656 unsigned int sg_len;
1660 WARN_ON(scsi_sg_count(sc) > BNX2FC_MAX_BDS_PER_CMD);
1662 * Use dma_map_sg directly to ensure we're using the correct
1663 * dev struct off of pcidev.
1665 sg_count = dma_map_sg(&hba->pcidev->dev, scsi_sglist(sc),
1666 scsi_sg_count(sc), sc->sc_data_direction);
1667 scsi_for_each_sg(sc, sg, sg_count, i) {
1668 sg_len = sg_dma_len(sg);
1669 addr = sg_dma_address(sg);
1670 if (sg_len > BNX2FC_MAX_BD_LEN) {
1671 sg_frags = bnx2fc_split_bd(io_req, addr, sg_len,
1676 bd[bd_count].buf_addr_lo = addr & 0xffffffff;
1677 bd[bd_count].buf_addr_hi = addr >> 32;
1678 bd[bd_count].buf_len = (u16)sg_len;
1679 bd[bd_count].flags = 0;
1681 bd_count += sg_frags;
1682 byte_count += sg_len;
1684 if (byte_count != scsi_bufflen(sc))
1685 printk(KERN_ERR PFX "byte_count = %d != scsi_bufflen = %d, "
1686 "task_id = 0x%x\n", byte_count, scsi_bufflen(sc),
1691 static int bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd *io_req)
1693 struct scsi_cmnd *sc = io_req->sc_cmd;
1694 struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
1697 if (scsi_sg_count(sc)) {
1698 bd_count = bnx2fc_map_sg(io_req);
1703 bd[0].buf_addr_lo = bd[0].buf_addr_hi = 0;
1704 bd[0].buf_len = bd[0].flags = 0;
1706 io_req->bd_tbl->bd_valid = bd_count;
1709 * Return the command to ML if BD count exceeds the max number
1710 * that can be handled by FW.
1712 if (bd_count > BNX2FC_FW_MAX_BDS_PER_CMD) {
1713 pr_err("bd_count = %d exceeded FW supported max BD(255), task_id = 0x%x\n",
1714 bd_count, io_req->xid);
1721 static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd *io_req)
1723 struct scsi_cmnd *sc = io_req->sc_cmd;
1724 struct bnx2fc_interface *interface = io_req->port->priv;
1725 struct bnx2fc_hba *hba = interface->hba;
1728 * Use dma_unmap_sg directly to ensure we're using the correct
1729 * dev struct off of pcidev.
1731 if (io_req->bd_tbl->bd_valid && sc && scsi_sg_count(sc)) {
1732 dma_unmap_sg(&hba->pcidev->dev, scsi_sglist(sc),
1733 scsi_sg_count(sc), sc->sc_data_direction);
1734 io_req->bd_tbl->bd_valid = 0;
1738 void bnx2fc_build_fcp_cmnd(struct bnx2fc_cmd *io_req,
1739 struct fcp_cmnd *fcp_cmnd)
1741 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1743 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
1745 int_to_scsilun(sc_cmd->device->lun, &fcp_cmnd->fc_lun);
1747 fcp_cmnd->fc_dl = htonl(io_req->data_xfer_len);
1748 memcpy(fcp_cmnd->fc_cdb, sc_cmd->cmnd, sc_cmd->cmd_len);
1750 fcp_cmnd->fc_cmdref = 0;
1751 fcp_cmnd->fc_pri_ta = 0;
1752 fcp_cmnd->fc_tm_flags = io_req->mp_req.tm_flags;
1753 fcp_cmnd->fc_flags = io_req->io_req_flags;
1754 fcp_cmnd->fc_pri_ta = FCP_PTA_SIMPLE;
1757 static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd *io_req,
1758 struct fcoe_fcp_rsp_payload *fcp_rsp,
1759 u8 num_rq, unsigned char *rq_data)
1761 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1762 u8 rsp_flags = fcp_rsp->fcp_flags.flags;
1763 u32 rq_buff_len = 0;
1764 int fcp_sns_len = 0;
1765 int fcp_rsp_len = 0;
1767 io_req->fcp_status = FC_GOOD;
1768 io_req->fcp_resid = 0;
1769 if (rsp_flags & (FCOE_FCP_RSP_FLAGS_FCP_RESID_OVER |
1770 FCOE_FCP_RSP_FLAGS_FCP_RESID_UNDER))
1771 io_req->fcp_resid = fcp_rsp->fcp_resid;
1773 io_req->scsi_comp_flags = rsp_flags;
1774 io_req->cdb_status = fcp_rsp->scsi_status_code;
1776 /* Fetch fcp_rsp_info and fcp_sns_info if available */
1780 * We do not anticipate num_rq >1, as the linux defined
1781 * SCSI_SENSE_BUFFERSIZE is 96 bytes + 8 bytes of FCP_RSP_INFO
1782 * 256 bytes of single rq buffer is good enough to hold this.
1786 FCOE_FCP_RSP_FLAGS_FCP_RSP_LEN_VALID) {
1787 fcp_rsp_len = rq_buff_len
1788 = fcp_rsp->fcp_rsp_len;
1792 FCOE_FCP_RSP_FLAGS_FCP_SNS_LEN_VALID) {
1793 fcp_sns_len = fcp_rsp->fcp_sns_len;
1794 rq_buff_len += fcp_rsp->fcp_sns_len;
1797 io_req->fcp_rsp_len = fcp_rsp_len;
1798 io_req->fcp_sns_len = fcp_sns_len;
1800 if (rq_buff_len > num_rq * BNX2FC_RQ_BUF_SZ) {
1801 /* Invalid sense sense length. */
1802 printk(KERN_ERR PFX "invalid sns length %d\n",
1804 /* reset rq_buff_len */
1805 rq_buff_len = num_rq * BNX2FC_RQ_BUF_SZ;
1808 /* fetch fcp_rsp_code */
1809 if ((fcp_rsp_len == 4) || (fcp_rsp_len == 8)) {
1810 /* Only for task management function */
1811 io_req->fcp_rsp_code = rq_data[3];
1812 BNX2FC_IO_DBG(io_req, "fcp_rsp_code = %d\n",
1813 io_req->fcp_rsp_code);
1816 /* fetch sense data */
1817 rq_data += fcp_rsp_len;
1819 if (fcp_sns_len > SCSI_SENSE_BUFFERSIZE) {
1820 printk(KERN_ERR PFX "Truncating sense buffer\n");
1821 fcp_sns_len = SCSI_SENSE_BUFFERSIZE;
1824 memset(sc_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1826 memcpy(sc_cmd->sense_buffer, rq_data, fcp_sns_len);
1832 * bnx2fc_queuecommand - Queuecommand function of the scsi template
1834 * @host: The Scsi_Host the command was issued to
1835 * @sc_cmd: struct scsi_cmnd to be executed
1837 * This is the IO strategy routine, called by SCSI-ML
1839 int bnx2fc_queuecommand(struct Scsi_Host *host,
1840 struct scsi_cmnd *sc_cmd)
1842 struct fc_lport *lport = shost_priv(host);
1843 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1844 struct fc_rport_libfc_priv *rp = rport->dd_data;
1845 struct bnx2fc_rport *tgt;
1846 struct bnx2fc_cmd *io_req;
1850 rval = fc_remote_port_chkready(rport);
1852 sc_cmd->result = rval;
1857 if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
1858 rc = SCSI_MLQUEUE_HOST_BUSY;
1862 /* rport and tgt are allocated together, so tgt should be non-NULL */
1863 tgt = (struct bnx2fc_rport *)&rp[1];
1865 if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
1867 * Session is not offloaded yet. Let SCSI-ml retry
1870 rc = SCSI_MLQUEUE_TARGET_BUSY;
1873 if (tgt->retry_delay_timestamp) {
1874 if (time_after(jiffies, tgt->retry_delay_timestamp)) {
1875 tgt->retry_delay_timestamp = 0;
1877 /* If retry_delay timer is active, flow off the ML */
1878 rc = SCSI_MLQUEUE_TARGET_BUSY;
1883 spin_lock_bh(&tgt->tgt_lock);
1885 io_req = bnx2fc_cmd_alloc(tgt);
1887 rc = SCSI_MLQUEUE_HOST_BUSY;
1888 goto exit_qcmd_tgtlock;
1890 io_req->sc_cmd = sc_cmd;
1892 if (bnx2fc_post_io_req(tgt, io_req)) {
1893 printk(KERN_ERR PFX "Unable to post io_req\n");
1894 rc = SCSI_MLQUEUE_HOST_BUSY;
1895 goto exit_qcmd_tgtlock;
1899 spin_unlock_bh(&tgt->tgt_lock);
1904 void bnx2fc_process_scsi_cmd_compl(struct bnx2fc_cmd *io_req,
1905 struct fcoe_task_ctx_entry *task,
1906 u8 num_rq, unsigned char *rq_data)
1908 struct fcoe_fcp_rsp_payload *fcp_rsp;
1909 struct bnx2fc_rport *tgt = io_req->tgt;
1910 struct scsi_cmnd *sc_cmd;
1911 u16 scope = 0, qualifier = 0;
1913 /* scsi_cmd_cmpl is called with tgt lock held */
1915 if (test_and_set_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags)) {
1916 /* we will not receive ABTS response for this IO */
1917 BNX2FC_IO_DBG(io_req, "Timer context finished processing "
1919 if (test_and_clear_bit(BNX2FC_FLAG_IO_CLEANUP,
1920 &io_req->req_flags)) {
1921 BNX2FC_IO_DBG(io_req,
1922 "Actual completion after cleanup request cleaning up\n");
1923 bnx2fc_process_cleanup_compl(io_req, task, num_rq);
1928 /* Cancel the timeout_work, as we received IO completion */
1929 if (cancel_delayed_work(&io_req->timeout_work))
1930 kref_put(&io_req->refcount,
1931 bnx2fc_cmd_release); /* drop timer hold */
1933 sc_cmd = io_req->sc_cmd;
1934 if (sc_cmd == NULL) {
1935 printk(KERN_ERR PFX "scsi_cmd_compl - sc_cmd is NULL\n");
1939 /* Fetch fcp_rsp from task context and perform cmd completion */
1940 fcp_rsp = (struct fcoe_fcp_rsp_payload *)
1941 &(task->rxwr_only.union_ctx.comp_info.fcp_rsp.payload);
1943 /* parse fcp_rsp and obtain sense data from RQ if available */
1944 bnx2fc_parse_fcp_rsp(io_req, fcp_rsp, num_rq, rq_data);
1946 if (!bnx2fc_priv(sc_cmd)->io_req) {
1947 printk(KERN_ERR PFX "io_req is NULL\n");
1951 if (io_req->on_active_queue) {
1952 list_del_init(&io_req->link);
1953 io_req->on_active_queue = 0;
1954 /* Move IO req to retire queue */
1955 list_add_tail(&io_req->link, &tgt->io_retire_queue);
1957 /* This should not happen, but could have been pulled
1958 * by bnx2fc_flush_active_ios(), or during a race
1959 * between command abort and (late) completion.
1961 BNX2FC_IO_DBG(io_req, "xid not on active_cmd_queue\n");
1962 if (io_req->wait_for_abts_comp)
1963 if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
1964 &io_req->req_flags))
1965 complete(&io_req->abts_done);
1968 bnx2fc_unmap_sg_list(io_req);
1969 io_req->sc_cmd = NULL;
1971 switch (io_req->fcp_status) {
1973 if (io_req->cdb_status == 0) {
1974 /* Good IO completion */
1975 sc_cmd->result = DID_OK << 16;
1977 /* Transport status is good, SCSI status not good */
1978 BNX2FC_IO_DBG(io_req, "scsi_cmpl: cdb_status = %d"
1979 " fcp_resid = 0x%x\n",
1980 io_req->cdb_status, io_req->fcp_resid);
1981 sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
1983 if (io_req->cdb_status == SAM_STAT_TASK_SET_FULL ||
1984 io_req->cdb_status == SAM_STAT_BUSY) {
1985 /* Newer array firmware with BUSY or
1986 * TASK_SET_FULL may return a status that needs
1987 * the scope bits masked.
1988 * Or a huge delay timestamp up to 27 minutes
1991 if (fcp_rsp->retry_delay_timer) {
1993 scope = fcp_rsp->retry_delay_timer
1996 qualifier = fcp_rsp->retry_delay_timer
1999 if (scope > 0 && qualifier > 0 &&
2000 qualifier <= 0x3FEF) {
2001 /* Set the jiffies +
2002 * retry_delay_timer * 100ms
2005 tgt->retry_delay_timestamp = jiffies +
2006 (qualifier * HZ / 10);
2010 if (io_req->fcp_resid)
2011 scsi_set_resid(sc_cmd, io_req->fcp_resid);
2014 printk(KERN_ERR PFX "scsi_cmd_compl: fcp_status = %d\n",
2015 io_req->fcp_status);
2018 bnx2fc_priv(sc_cmd)->io_req = NULL;
2020 kref_put(&io_req->refcount, bnx2fc_cmd_release);
2023 int bnx2fc_post_io_req(struct bnx2fc_rport *tgt,
2024 struct bnx2fc_cmd *io_req)
2026 struct fcoe_task_ctx_entry *task;
2027 struct fcoe_task_ctx_entry *task_page;
2028 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
2029 struct fcoe_port *port = tgt->port;
2030 struct bnx2fc_interface *interface = port->priv;
2031 struct bnx2fc_hba *hba = interface->hba;
2032 struct fc_lport *lport = port->lport;
2033 int task_idx, index;
2036 /* bnx2fc_post_io_req() is called with the tgt_lock held */
2038 /* Initialize rest of io_req fields */
2039 io_req->cmd_type = BNX2FC_SCSI_CMD;
2040 io_req->port = port;
2042 io_req->data_xfer_len = scsi_bufflen(sc_cmd);
2043 bnx2fc_priv(sc_cmd)->io_req = io_req;
2045 if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
2046 io_req->io_req_flags = BNX2FC_READ;
2047 this_cpu_inc(lport->stats->InputRequests);
2048 this_cpu_add(lport->stats->InputBytes, io_req->data_xfer_len);
2049 } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
2050 io_req->io_req_flags = BNX2FC_WRITE;
2051 this_cpu_inc(lport->stats->OutputRequests);
2052 this_cpu_add(lport->stats->OutputBytes, io_req->data_xfer_len);
2054 io_req->io_req_flags = 0;
2055 this_cpu_inc(lport->stats->ControlRequests);
2060 /* Build buffer descriptor list for firmware from sg list */
2061 if (bnx2fc_build_bd_list_from_sg(io_req)) {
2062 printk(KERN_ERR PFX "BD list creation failed\n");
2063 kref_put(&io_req->refcount, bnx2fc_cmd_release);
2067 task_idx = xid / BNX2FC_TASKS_PER_PAGE;
2068 index = xid % BNX2FC_TASKS_PER_PAGE;
2070 /* Initialize task context for this IO request */
2071 task_page = (struct fcoe_task_ctx_entry *) hba->task_ctx[task_idx];
2072 task = &(task_page[index]);
2073 bnx2fc_init_task(io_req, task);
2075 if (tgt->flush_in_prog) {
2076 printk(KERN_ERR PFX "Flush in progress..Host Busy\n");
2077 kref_put(&io_req->refcount, bnx2fc_cmd_release);
2081 if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
2082 printk(KERN_ERR PFX "Session not ready...post_io\n");
2083 kref_put(&io_req->refcount, bnx2fc_cmd_release);
2088 if (tgt->io_timeout)
2089 bnx2fc_cmd_timer_set(io_req, BNX2FC_IO_TIMEOUT);
2090 /* Obtain free SQ entry */
2091 bnx2fc_add_2_sq(tgt, xid);
2093 /* Enqueue the io_req to active_cmd_queue */
2095 io_req->on_active_queue = 1;
2096 /* move io_req from pending_queue to active_queue */
2097 list_add_tail(&io_req->link, &tgt->active_cmd_queue);
2100 bnx2fc_ring_doorbell(tgt);