1 /* bnx2fc_io.c: Broadcom NetXtreme II Linux FCoE offload driver.
2 * IO manager and SCSI IO processing.
4 * Copyright (c) 2008 - 2011 Broadcom Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation.
10 * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
15 #define RESERVE_FREE_LIST_INDEX num_possible_cpus()
17 static int bnx2fc_split_bd(struct bnx2fc_cmd *io_req, u64 addr, int sg_len,
19 static int bnx2fc_map_sg(struct bnx2fc_cmd *io_req);
20 static int bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd *io_req);
21 static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd *io_req);
22 static void bnx2fc_free_mp_resc(struct bnx2fc_cmd *io_req);
23 static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd *io_req,
24 struct fcoe_fcp_rsp_payload *fcp_rsp,
27 void bnx2fc_cmd_timer_set(struct bnx2fc_cmd *io_req,
28 unsigned int timer_msec)
30 struct bnx2fc_interface *interface = io_req->port->priv;
32 if (queue_delayed_work(interface->timer_work_queue,
33 &io_req->timeout_work,
34 msecs_to_jiffies(timer_msec)))
35 kref_get(&io_req->refcount);
38 static void bnx2fc_cmd_timeout(struct work_struct *work)
40 struct bnx2fc_cmd *io_req = container_of(work, struct bnx2fc_cmd,
42 struct fc_lport *lport;
43 struct fc_rport_priv *rdata;
44 u8 cmd_type = io_req->cmd_type;
45 struct bnx2fc_rport *tgt = io_req->tgt;
49 BNX2FC_IO_DBG(io_req, "cmd_timeout, cmd_type = %d,"
50 "req_flags = %lx\n", cmd_type, io_req->req_flags);
52 spin_lock_bh(&tgt->tgt_lock);
53 if (test_and_clear_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags)) {
54 clear_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags);
56 * ideally we should hold the io_req until RRQ complets,
57 * and release io_req from timeout hold.
59 spin_unlock_bh(&tgt->tgt_lock);
60 bnx2fc_send_rrq(io_req);
63 if (test_and_clear_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags)) {
64 BNX2FC_IO_DBG(io_req, "IO ready for reuse now\n");
70 if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
71 &io_req->req_flags)) {
72 /* Handle eh_abort timeout */
73 BNX2FC_IO_DBG(io_req, "eh_abort timed out\n");
74 complete(&io_req->tm_done);
75 } else if (test_bit(BNX2FC_FLAG_ISSUE_ABTS,
76 &io_req->req_flags)) {
77 /* Handle internally generated ABTS timeout */
78 BNX2FC_IO_DBG(io_req, "ABTS timed out refcnt = %d\n",
79 io_req->refcount.refcount.counter);
80 if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
81 &io_req->req_flags))) {
83 lport = io_req->port->lport;
84 rdata = io_req->tgt->rdata;
85 logo_issued = test_and_set_bit(
86 BNX2FC_FLAG_EXPL_LOGO,
88 kref_put(&io_req->refcount, bnx2fc_cmd_release);
89 spin_unlock_bh(&tgt->tgt_lock);
91 /* Explicitly logo the target */
93 BNX2FC_IO_DBG(io_req, "Explicit "
94 "logo - tgt flags = 0x%lx\n",
97 mutex_lock(&lport->disc.disc_mutex);
98 lport->tt.rport_logoff(rdata);
99 mutex_unlock(&lport->disc.disc_mutex);
104 /* Hanlde IO timeout */
105 BNX2FC_IO_DBG(io_req, "IO timed out. issue ABTS\n");
106 if (test_and_set_bit(BNX2FC_FLAG_IO_COMPL,
107 &io_req->req_flags)) {
108 BNX2FC_IO_DBG(io_req, "IO completed before "
113 if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
114 &io_req->req_flags)) {
115 rc = bnx2fc_initiate_abts(io_req);
119 * Explicitly logo the target if
120 * abts initiation fails
122 lport = io_req->port->lport;
123 rdata = io_req->tgt->rdata;
124 logo_issued = test_and_set_bit(
125 BNX2FC_FLAG_EXPL_LOGO,
127 kref_put(&io_req->refcount, bnx2fc_cmd_release);
128 spin_unlock_bh(&tgt->tgt_lock);
131 BNX2FC_IO_DBG(io_req, "Explicit "
132 "logo - tgt flags = 0x%lx\n",
136 mutex_lock(&lport->disc.disc_mutex);
137 lport->tt.rport_logoff(rdata);
138 mutex_unlock(&lport->disc.disc_mutex);
142 BNX2FC_IO_DBG(io_req, "IO already in "
143 "ABTS processing\n");
149 if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags)) {
150 BNX2FC_IO_DBG(io_req, "ABTS for ELS timed out\n");
152 if (!test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
153 &io_req->req_flags)) {
154 lport = io_req->port->lport;
155 rdata = io_req->tgt->rdata;
156 logo_issued = test_and_set_bit(
157 BNX2FC_FLAG_EXPL_LOGO,
159 kref_put(&io_req->refcount, bnx2fc_cmd_release);
160 spin_unlock_bh(&tgt->tgt_lock);
162 /* Explicitly logo the target */
164 BNX2FC_IO_DBG(io_req, "Explicitly logo"
166 mutex_lock(&lport->disc.disc_mutex);
167 lport->tt.rport_logoff(rdata);
168 mutex_unlock(&lport->disc.disc_mutex);
174 * Handle ELS timeout.
175 * tgt_lock is used to sync compl path and timeout
176 * path. If els compl path is processing this IO, we
177 * have nothing to do here, just release the timer hold
179 BNX2FC_IO_DBG(io_req, "ELS timed out\n");
180 if (test_and_set_bit(BNX2FC_FLAG_ELS_DONE,
184 /* Indicate the cb_func that this ELS is timed out */
185 set_bit(BNX2FC_FLAG_ELS_TIMEOUT, &io_req->req_flags);
187 if ((io_req->cb_func) && (io_req->cb_arg)) {
188 io_req->cb_func(io_req->cb_arg);
189 io_req->cb_arg = NULL;
194 printk(KERN_ERR PFX "cmd_timeout: invalid cmd_type %d\n",
200 /* release the cmd that was held when timer was set */
201 kref_put(&io_req->refcount, bnx2fc_cmd_release);
202 spin_unlock_bh(&tgt->tgt_lock);
205 static void bnx2fc_scsi_done(struct bnx2fc_cmd *io_req, int err_code)
207 /* Called with host lock held */
208 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
211 * active_cmd_queue may have other command types as well,
212 * and during flush operation, we want to error back only
215 if (io_req->cmd_type != BNX2FC_SCSI_CMD)
218 BNX2FC_IO_DBG(io_req, "scsi_done. err_code = 0x%x\n", err_code);
219 if (test_bit(BNX2FC_FLAG_CMD_LOST, &io_req->req_flags)) {
220 /* Do not call scsi done for this IO */
224 bnx2fc_unmap_sg_list(io_req);
225 io_req->sc_cmd = NULL;
227 printk(KERN_ERR PFX "scsi_done - sc_cmd NULL. "
228 "IO(0x%x) already cleaned up\n",
232 sc_cmd->result = err_code << 16;
234 BNX2FC_IO_DBG(io_req, "sc=%p, result=0x%x, retries=%d, allowed=%d\n",
235 sc_cmd, host_byte(sc_cmd->result), sc_cmd->retries,
237 scsi_set_resid(sc_cmd, scsi_bufflen(sc_cmd));
238 sc_cmd->SCp.ptr = NULL;
239 sc_cmd->scsi_done(sc_cmd);
242 struct bnx2fc_cmd_mgr *bnx2fc_cmd_mgr_alloc(struct bnx2fc_hba *hba)
244 struct bnx2fc_cmd_mgr *cmgr;
245 struct io_bdt *bdt_info;
246 struct bnx2fc_cmd *io_req;
251 int num_ios, num_pri_ios;
253 int arr_sz = num_possible_cpus() + 1;
254 u16 min_xid = BNX2FC_MIN_XID;
255 u16 max_xid = hba->max_xid;
257 if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN) {
258 printk(KERN_ERR PFX "cmd_mgr_alloc: Invalid min_xid 0x%x \
259 and max_xid 0x%x\n", min_xid, max_xid);
262 BNX2FC_MISC_DBG("min xid 0x%x, max xid 0x%x\n", min_xid, max_xid);
264 num_ios = max_xid - min_xid + 1;
265 len = (num_ios * (sizeof(struct bnx2fc_cmd *)));
266 len += sizeof(struct bnx2fc_cmd_mgr);
268 cmgr = kzalloc(len, GFP_KERNEL);
270 printk(KERN_ERR PFX "failed to alloc cmgr\n");
274 cmgr->free_list = kzalloc(sizeof(*cmgr->free_list) *
276 if (!cmgr->free_list) {
277 printk(KERN_ERR PFX "failed to alloc free_list\n");
281 cmgr->free_list_lock = kzalloc(sizeof(*cmgr->free_list_lock) *
283 if (!cmgr->free_list_lock) {
284 printk(KERN_ERR PFX "failed to alloc free_list_lock\n");
289 cmgr->cmds = (struct bnx2fc_cmd **)(cmgr + 1);
291 for (i = 0; i < arr_sz; i++) {
292 INIT_LIST_HEAD(&cmgr->free_list[i]);
293 spin_lock_init(&cmgr->free_list_lock[i]);
297 * Pre-allocated pool of bnx2fc_cmds.
298 * Last entry in the free list array is the free list
299 * of slow path requests.
301 xid = BNX2FC_MIN_XID;
302 num_pri_ios = num_ios - hba->elstm_xids;
303 for (i = 0; i < num_ios; i++) {
304 io_req = kzalloc(sizeof(*io_req), GFP_KERNEL);
307 printk(KERN_ERR PFX "failed to alloc io_req\n");
311 INIT_LIST_HEAD(&io_req->link);
312 INIT_DELAYED_WORK(&io_req->timeout_work, bnx2fc_cmd_timeout);
316 list_add_tail(&io_req->link,
317 &cmgr->free_list[io_req->xid %
318 num_possible_cpus()]);
320 list_add_tail(&io_req->link,
321 &cmgr->free_list[num_possible_cpus()]);
325 /* Allocate pool of io_bdts - one for each bnx2fc_cmd */
326 mem_size = num_ios * sizeof(struct io_bdt *);
327 cmgr->io_bdt_pool = kmalloc(mem_size, GFP_KERNEL);
328 if (!cmgr->io_bdt_pool) {
329 printk(KERN_ERR PFX "failed to alloc io_bdt_pool\n");
333 mem_size = sizeof(struct io_bdt);
334 for (i = 0; i < num_ios; i++) {
335 cmgr->io_bdt_pool[i] = kmalloc(mem_size, GFP_KERNEL);
336 if (!cmgr->io_bdt_pool[i]) {
337 printk(KERN_ERR PFX "failed to alloc "
338 "io_bdt_pool[%d]\n", i);
343 /* Allocate an map fcoe_bdt_ctx structures */
344 bd_tbl_sz = BNX2FC_MAX_BDS_PER_CMD * sizeof(struct fcoe_bd_ctx);
345 for (i = 0; i < num_ios; i++) {
346 bdt_info = cmgr->io_bdt_pool[i];
347 bdt_info->bd_tbl = dma_alloc_coherent(&hba->pcidev->dev,
349 &bdt_info->bd_tbl_dma,
351 if (!bdt_info->bd_tbl) {
352 printk(KERN_ERR PFX "failed to alloc "
361 bnx2fc_cmd_mgr_free(cmgr);
365 void bnx2fc_cmd_mgr_free(struct bnx2fc_cmd_mgr *cmgr)
367 struct io_bdt *bdt_info;
368 struct bnx2fc_hba *hba = cmgr->hba;
370 u16 min_xid = BNX2FC_MIN_XID;
371 u16 max_xid = hba->max_xid;
375 num_ios = max_xid - min_xid + 1;
377 /* Free fcoe_bdt_ctx structures */
378 if (!cmgr->io_bdt_pool)
381 bd_tbl_sz = BNX2FC_MAX_BDS_PER_CMD * sizeof(struct fcoe_bd_ctx);
382 for (i = 0; i < num_ios; i++) {
383 bdt_info = cmgr->io_bdt_pool[i];
384 if (bdt_info->bd_tbl) {
385 dma_free_coherent(&hba->pcidev->dev, bd_tbl_sz,
387 bdt_info->bd_tbl_dma);
388 bdt_info->bd_tbl = NULL;
392 /* Destroy io_bdt pool */
393 for (i = 0; i < num_ios; i++) {
394 kfree(cmgr->io_bdt_pool[i]);
395 cmgr->io_bdt_pool[i] = NULL;
398 kfree(cmgr->io_bdt_pool);
399 cmgr->io_bdt_pool = NULL;
402 kfree(cmgr->free_list_lock);
404 /* Destroy cmd pool */
405 if (!cmgr->free_list)
408 for (i = 0; i < num_possible_cpus() + 1; i++) {
409 struct bnx2fc_cmd *tmp, *io_req;
411 list_for_each_entry_safe(io_req, tmp,
412 &cmgr->free_list[i], link) {
413 list_del(&io_req->link);
417 kfree(cmgr->free_list);
419 /* Free command manager itself */
423 struct bnx2fc_cmd *bnx2fc_elstm_alloc(struct bnx2fc_rport *tgt, int type)
425 struct fcoe_port *port = tgt->port;
426 struct bnx2fc_interface *interface = port->priv;
427 struct bnx2fc_cmd_mgr *cmd_mgr = interface->hba->cmd_mgr;
428 struct bnx2fc_cmd *io_req;
429 struct list_head *listp;
430 struct io_bdt *bd_tbl;
431 int index = RESERVE_FREE_LIST_INDEX;
436 max_sqes = tgt->max_sqes;
438 case BNX2FC_TASK_MGMT_CMD:
439 max_sqes = BNX2FC_TM_MAX_SQES;
442 max_sqes = BNX2FC_ELS_MAX_SQES;
449 * NOTE: Free list insertions and deletions are protected with
452 spin_lock_bh(&cmd_mgr->free_list_lock[index]);
453 free_sqes = atomic_read(&tgt->free_sqes);
454 if ((list_empty(&(cmd_mgr->free_list[index]))) ||
455 (tgt->num_active_ios.counter >= max_sqes) ||
456 (free_sqes + max_sqes <= BNX2FC_SQ_WQES_MAX)) {
457 BNX2FC_TGT_DBG(tgt, "No free els_tm cmds available "
458 "ios(%d):sqes(%d)\n",
459 tgt->num_active_ios.counter, tgt->max_sqes);
460 if (list_empty(&(cmd_mgr->free_list[index])))
461 printk(KERN_ERR PFX "elstm_alloc: list_empty\n");
462 spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
466 listp = (struct list_head *)
467 cmd_mgr->free_list[index].next;
468 list_del_init(listp);
469 io_req = (struct bnx2fc_cmd *) listp;
471 cmd_mgr->cmds[xid] = io_req;
472 atomic_inc(&tgt->num_active_ios);
473 atomic_dec(&tgt->free_sqes);
474 spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
476 INIT_LIST_HEAD(&io_req->link);
479 io_req->cmd_mgr = cmd_mgr;
480 io_req->req_flags = 0;
481 io_req->cmd_type = type;
483 /* Bind io_bdt for this io_req */
484 /* Have a static link between io_req and io_bdt_pool */
485 bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid];
486 bd_tbl->io_req = io_req;
488 /* Hold the io_req against deletion */
489 kref_init(&io_req->refcount);
493 struct bnx2fc_cmd *bnx2fc_cmd_alloc(struct bnx2fc_rport *tgt)
495 struct fcoe_port *port = tgt->port;
496 struct bnx2fc_interface *interface = port->priv;
497 struct bnx2fc_cmd_mgr *cmd_mgr = interface->hba->cmd_mgr;
498 struct bnx2fc_cmd *io_req;
499 struct list_head *listp;
500 struct io_bdt *bd_tbl;
504 int index = get_cpu();
506 max_sqes = BNX2FC_SCSI_MAX_SQES;
508 * NOTE: Free list insertions and deletions are protected with
511 spin_lock_bh(&cmd_mgr->free_list_lock[index]);
512 free_sqes = atomic_read(&tgt->free_sqes);
513 if ((list_empty(&cmd_mgr->free_list[index])) ||
514 (tgt->num_active_ios.counter >= max_sqes) ||
515 (free_sqes + max_sqes <= BNX2FC_SQ_WQES_MAX)) {
516 spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
521 listp = (struct list_head *)
522 cmd_mgr->free_list[index].next;
523 list_del_init(listp);
524 io_req = (struct bnx2fc_cmd *) listp;
526 cmd_mgr->cmds[xid] = io_req;
527 atomic_inc(&tgt->num_active_ios);
528 atomic_dec(&tgt->free_sqes);
529 spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
532 INIT_LIST_HEAD(&io_req->link);
535 io_req->cmd_mgr = cmd_mgr;
536 io_req->req_flags = 0;
538 /* Bind io_bdt for this io_req */
539 /* Have a static link between io_req and io_bdt_pool */
540 bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid];
541 bd_tbl->io_req = io_req;
543 /* Hold the io_req against deletion */
544 kref_init(&io_req->refcount);
548 void bnx2fc_cmd_release(struct kref *ref)
550 struct bnx2fc_cmd *io_req = container_of(ref,
551 struct bnx2fc_cmd, refcount);
552 struct bnx2fc_cmd_mgr *cmd_mgr = io_req->cmd_mgr;
555 if (io_req->cmd_type == BNX2FC_SCSI_CMD)
556 index = io_req->xid % num_possible_cpus();
558 index = RESERVE_FREE_LIST_INDEX;
561 spin_lock_bh(&cmd_mgr->free_list_lock[index]);
562 if (io_req->cmd_type != BNX2FC_SCSI_CMD)
563 bnx2fc_free_mp_resc(io_req);
564 cmd_mgr->cmds[io_req->xid] = NULL;
565 /* Delete IO from retire queue */
566 list_del_init(&io_req->link);
567 /* Add it to the free list */
568 list_add(&io_req->link,
569 &cmd_mgr->free_list[index]);
570 atomic_dec(&io_req->tgt->num_active_ios);
571 spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
575 static void bnx2fc_free_mp_resc(struct bnx2fc_cmd *io_req)
577 struct bnx2fc_mp_req *mp_req = &(io_req->mp_req);
578 struct bnx2fc_interface *interface = io_req->port->priv;
579 struct bnx2fc_hba *hba = interface->hba;
580 size_t sz = sizeof(struct fcoe_bd_ctx);
583 mp_req->tm_flags = 0;
584 if (mp_req->mp_req_bd) {
585 dma_free_coherent(&hba->pcidev->dev, sz,
587 mp_req->mp_req_bd_dma);
588 mp_req->mp_req_bd = NULL;
590 if (mp_req->mp_resp_bd) {
591 dma_free_coherent(&hba->pcidev->dev, sz,
593 mp_req->mp_resp_bd_dma);
594 mp_req->mp_resp_bd = NULL;
596 if (mp_req->req_buf) {
597 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
599 mp_req->req_buf_dma);
600 mp_req->req_buf = NULL;
602 if (mp_req->resp_buf) {
603 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
605 mp_req->resp_buf_dma);
606 mp_req->resp_buf = NULL;
610 int bnx2fc_init_mp_req(struct bnx2fc_cmd *io_req)
612 struct bnx2fc_mp_req *mp_req;
613 struct fcoe_bd_ctx *mp_req_bd;
614 struct fcoe_bd_ctx *mp_resp_bd;
615 struct bnx2fc_interface *interface = io_req->port->priv;
616 struct bnx2fc_hba *hba = interface->hba;
620 mp_req = (struct bnx2fc_mp_req *)&(io_req->mp_req);
621 memset(mp_req, 0, sizeof(struct bnx2fc_mp_req));
623 mp_req->req_len = sizeof(struct fcp_cmnd);
624 io_req->data_xfer_len = mp_req->req_len;
625 mp_req->req_buf = dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
626 &mp_req->req_buf_dma,
628 if (!mp_req->req_buf) {
629 printk(KERN_ERR PFX "unable to alloc MP req buffer\n");
630 bnx2fc_free_mp_resc(io_req);
634 mp_req->resp_buf = dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
635 &mp_req->resp_buf_dma,
637 if (!mp_req->resp_buf) {
638 printk(KERN_ERR PFX "unable to alloc TM resp buffer\n");
639 bnx2fc_free_mp_resc(io_req);
642 memset(mp_req->req_buf, 0, PAGE_SIZE);
643 memset(mp_req->resp_buf, 0, PAGE_SIZE);
645 /* Allocate and map mp_req_bd and mp_resp_bd */
646 sz = sizeof(struct fcoe_bd_ctx);
647 mp_req->mp_req_bd = dma_alloc_coherent(&hba->pcidev->dev, sz,
648 &mp_req->mp_req_bd_dma,
650 if (!mp_req->mp_req_bd) {
651 printk(KERN_ERR PFX "unable to alloc MP req bd\n");
652 bnx2fc_free_mp_resc(io_req);
655 mp_req->mp_resp_bd = dma_alloc_coherent(&hba->pcidev->dev, sz,
656 &mp_req->mp_resp_bd_dma,
658 if (!mp_req->mp_resp_bd) {
659 printk(KERN_ERR PFX "unable to alloc MP resp bd\n");
660 bnx2fc_free_mp_resc(io_req);
664 addr = mp_req->req_buf_dma;
665 mp_req_bd = mp_req->mp_req_bd;
666 mp_req_bd->buf_addr_lo = (u32)addr & 0xffffffff;
667 mp_req_bd->buf_addr_hi = (u32)((u64)addr >> 32);
668 mp_req_bd->buf_len = PAGE_SIZE;
669 mp_req_bd->flags = 0;
672 * MP buffer is either a task mgmt command or an ELS.
673 * So the assumption is that it consumes a single bd
674 * entry in the bd table
676 mp_resp_bd = mp_req->mp_resp_bd;
677 addr = mp_req->resp_buf_dma;
678 mp_resp_bd->buf_addr_lo = (u32)addr & 0xffffffff;
679 mp_resp_bd->buf_addr_hi = (u32)((u64)addr >> 32);
680 mp_resp_bd->buf_len = PAGE_SIZE;
681 mp_resp_bd->flags = 0;
686 static int bnx2fc_initiate_tmf(struct scsi_cmnd *sc_cmd, u8 tm_flags)
688 struct fc_lport *lport;
689 struct fc_rport *rport;
690 struct fc_rport_libfc_priv *rp;
691 struct fcoe_port *port;
692 struct bnx2fc_interface *interface;
693 struct bnx2fc_rport *tgt;
694 struct bnx2fc_cmd *io_req;
695 struct bnx2fc_mp_req *tm_req;
696 struct fcoe_task_ctx_entry *task;
697 struct fcoe_task_ctx_entry *task_page;
698 struct Scsi_Host *host = sc_cmd->device->host;
699 struct fc_frame_header *fc_hdr;
700 struct fcp_cmnd *fcp_cmnd;
705 unsigned long start = jiffies;
707 lport = shost_priv(host);
708 rport = starget_to_rport(scsi_target(sc_cmd->device));
709 port = lport_priv(lport);
710 interface = port->priv;
713 printk(KERN_ERR PFX "device_reset: rport is NULL\n");
719 rc = fc_block_scsi_eh(sc_cmd);
723 if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
724 printk(KERN_ERR PFX "device_reset: link is not ready\n");
728 /* rport and tgt are allocated together, so tgt should be non-NULL */
729 tgt = (struct bnx2fc_rport *)&rp[1];
731 if (!(test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
732 printk(KERN_ERR PFX "device_reset: tgt not offloaded\n");
737 io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_TASK_MGMT_CMD);
739 if (time_after(jiffies, start + HZ)) {
740 printk(KERN_ERR PFX "tmf: Failed TMF");
747 /* Initialize rest of io_req fields */
748 io_req->sc_cmd = sc_cmd;
752 tm_req = (struct bnx2fc_mp_req *)&(io_req->mp_req);
754 rc = bnx2fc_init_mp_req(io_req);
756 printk(KERN_ERR PFX "Task mgmt MP request init failed\n");
757 spin_lock_bh(&tgt->tgt_lock);
758 kref_put(&io_req->refcount, bnx2fc_cmd_release);
759 spin_unlock_bh(&tgt->tgt_lock);
764 io_req->io_req_flags = 0;
765 tm_req->tm_flags = tm_flags;
768 bnx2fc_build_fcp_cmnd(io_req, (struct fcp_cmnd *)tm_req->req_buf);
769 fcp_cmnd = (struct fcp_cmnd *)tm_req->req_buf;
770 memset(fcp_cmnd->fc_cdb, 0, sc_cmd->cmd_len);
774 fc_hdr = &(tm_req->req_fc_hdr);
776 did = rport->port_id;
777 __fc_fill_fc_hdr(fc_hdr, FC_RCTL_DD_UNSOL_CMD, did, sid,
778 FC_TYPE_FCP, FC_FC_FIRST_SEQ | FC_FC_END_SEQ |
780 /* Obtain exchange id */
783 BNX2FC_TGT_DBG(tgt, "Initiate TMF - xid = 0x%x\n", xid);
784 task_idx = xid/BNX2FC_TASKS_PER_PAGE;
785 index = xid % BNX2FC_TASKS_PER_PAGE;
787 /* Initialize task context for this IO request */
788 task_page = (struct fcoe_task_ctx_entry *)
789 interface->hba->task_ctx[task_idx];
790 task = &(task_page[index]);
791 bnx2fc_init_mp_task(io_req, task);
793 sc_cmd->SCp.ptr = (char *)io_req;
795 /* Obtain free SQ entry */
796 spin_lock_bh(&tgt->tgt_lock);
797 bnx2fc_add_2_sq(tgt, xid);
799 /* Enqueue the io_req to active_tm_queue */
800 io_req->on_tmf_queue = 1;
801 list_add_tail(&io_req->link, &tgt->active_tm_queue);
803 init_completion(&io_req->tm_done);
804 io_req->wait_for_comp = 1;
807 bnx2fc_ring_doorbell(tgt);
808 spin_unlock_bh(&tgt->tgt_lock);
810 rc = wait_for_completion_timeout(&io_req->tm_done,
811 BNX2FC_TM_TIMEOUT * HZ);
812 spin_lock_bh(&tgt->tgt_lock);
814 io_req->wait_for_comp = 0;
815 if (!(test_bit(BNX2FC_FLAG_TM_COMPL, &io_req->req_flags))) {
816 set_bit(BNX2FC_FLAG_TM_TIMEOUT, &io_req->req_flags);
817 if (io_req->on_tmf_queue) {
818 list_del_init(&io_req->link);
819 io_req->on_tmf_queue = 0;
821 io_req->wait_for_comp = 1;
822 bnx2fc_initiate_cleanup(io_req);
823 spin_unlock_bh(&tgt->tgt_lock);
824 rc = wait_for_completion_timeout(&io_req->tm_done,
826 spin_lock_bh(&tgt->tgt_lock);
827 io_req->wait_for_comp = 0;
829 kref_put(&io_req->refcount, bnx2fc_cmd_release);
832 spin_unlock_bh(&tgt->tgt_lock);
835 BNX2FC_TGT_DBG(tgt, "task mgmt command failed...\n");
838 BNX2FC_TGT_DBG(tgt, "task mgmt command success...\n");
845 int bnx2fc_initiate_abts(struct bnx2fc_cmd *io_req)
847 struct fc_lport *lport;
848 struct bnx2fc_rport *tgt = io_req->tgt;
849 struct fc_rport *rport = tgt->rport;
850 struct fc_rport_priv *rdata = tgt->rdata;
851 struct bnx2fc_interface *interface;
852 struct fcoe_port *port;
853 struct bnx2fc_cmd *abts_io_req;
854 struct fcoe_task_ctx_entry *task;
855 struct fcoe_task_ctx_entry *task_page;
856 struct fc_frame_header *fc_hdr;
857 struct bnx2fc_mp_req *abts_req;
862 u32 r_a_tov = rdata->r_a_tov;
864 /* called with tgt_lock held */
865 BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_abts\n");
868 interface = port->priv;
871 if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
872 printk(KERN_ERR PFX "initiate_abts: tgt not offloaded\n");
878 printk(KERN_ERR PFX "initiate_abts: rport is NULL\n");
883 if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
884 printk(KERN_ERR PFX "initiate_abts: link is not ready\n");
889 abts_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_ABTS);
891 printk(KERN_ERR PFX "abts: couldnt allocate cmd\n");
896 /* Initialize rest of io_req fields */
897 abts_io_req->sc_cmd = NULL;
898 abts_io_req->port = port;
899 abts_io_req->tgt = tgt;
900 abts_io_req->data_xfer_len = 0; /* No data transfer for ABTS */
902 abts_req = (struct bnx2fc_mp_req *)&(abts_io_req->mp_req);
903 memset(abts_req, 0, sizeof(struct bnx2fc_mp_req));
906 fc_hdr = &(abts_req->req_fc_hdr);
908 /* Obtain oxid and rxid for the original exchange to be aborted */
909 fc_hdr->fh_ox_id = htons(io_req->xid);
910 fc_hdr->fh_rx_id = htons(io_req->task->rxwr_txrd.var_ctx.rx_id);
913 did = rport->port_id;
915 __fc_fill_fc_hdr(fc_hdr, FC_RCTL_BA_ABTS, did, sid,
916 FC_TYPE_BLS, FC_FC_FIRST_SEQ | FC_FC_END_SEQ |
919 xid = abts_io_req->xid;
920 BNX2FC_IO_DBG(abts_io_req, "ABTS io_req\n");
921 task_idx = xid/BNX2FC_TASKS_PER_PAGE;
922 index = xid % BNX2FC_TASKS_PER_PAGE;
924 /* Initialize task context for this IO request */
925 task_page = (struct fcoe_task_ctx_entry *)
926 interface->hba->task_ctx[task_idx];
927 task = &(task_page[index]);
928 bnx2fc_init_mp_task(abts_io_req, task);
931 * ABTS task is a temporary task that will be cleaned up
932 * irrespective of ABTS response. We need to start the timer
933 * for the original exchange, as the CQE is posted for the original
936 * Timer for ABTS is started only when it is originated by a
937 * TM request. For the ABTS issued as part of ULP timeout,
938 * scsi-ml maintains the timers.
941 /* if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags))*/
942 bnx2fc_cmd_timer_set(io_req, 2 * r_a_tov);
944 /* Obtain free SQ entry */
945 bnx2fc_add_2_sq(tgt, xid);
948 bnx2fc_ring_doorbell(tgt);
954 int bnx2fc_initiate_seq_cleanup(struct bnx2fc_cmd *orig_io_req, u32 offset,
957 struct fc_lport *lport;
958 struct bnx2fc_rport *tgt = orig_io_req->tgt;
959 struct bnx2fc_interface *interface;
960 struct fcoe_port *port;
961 struct bnx2fc_cmd *seq_clnp_req;
962 struct fcoe_task_ctx_entry *task;
963 struct fcoe_task_ctx_entry *task_page;
964 struct bnx2fc_els_cb_arg *cb_arg = NULL;
969 BNX2FC_IO_DBG(orig_io_req, "bnx2fc_initiate_seq_cleanup xid = 0x%x\n",
971 kref_get(&orig_io_req->refcount);
973 port = orig_io_req->port;
974 interface = port->priv;
977 cb_arg = kzalloc(sizeof(struct bnx2fc_els_cb_arg), GFP_ATOMIC);
979 printk(KERN_ERR PFX "Unable to alloc cb_arg for seq clnup\n");
984 seq_clnp_req = bnx2fc_elstm_alloc(tgt, BNX2FC_SEQ_CLEANUP);
986 printk(KERN_ERR PFX "cleanup: couldnt allocate cmd\n");
991 /* Initialize rest of io_req fields */
992 seq_clnp_req->sc_cmd = NULL;
993 seq_clnp_req->port = port;
994 seq_clnp_req->tgt = tgt;
995 seq_clnp_req->data_xfer_len = 0; /* No data transfer for cleanup */
997 xid = seq_clnp_req->xid;
999 task_idx = xid/BNX2FC_TASKS_PER_PAGE;
1000 index = xid % BNX2FC_TASKS_PER_PAGE;
1002 /* Initialize task context for this IO request */
1003 task_page = (struct fcoe_task_ctx_entry *)
1004 interface->hba->task_ctx[task_idx];
1005 task = &(task_page[index]);
1006 cb_arg->aborted_io_req = orig_io_req;
1007 cb_arg->io_req = seq_clnp_req;
1008 cb_arg->r_ctl = r_ctl;
1009 cb_arg->offset = offset;
1010 seq_clnp_req->cb_arg = cb_arg;
1012 printk(KERN_ERR PFX "call init_seq_cleanup_task\n");
1013 bnx2fc_init_seq_cleanup_task(seq_clnp_req, task, orig_io_req, offset);
1015 /* Obtain free SQ entry */
1016 bnx2fc_add_2_sq(tgt, xid);
1019 bnx2fc_ring_doorbell(tgt);
1024 int bnx2fc_initiate_cleanup(struct bnx2fc_cmd *io_req)
1026 struct fc_lport *lport;
1027 struct bnx2fc_rport *tgt = io_req->tgt;
1028 struct bnx2fc_interface *interface;
1029 struct fcoe_port *port;
1030 struct bnx2fc_cmd *cleanup_io_req;
1031 struct fcoe_task_ctx_entry *task;
1032 struct fcoe_task_ctx_entry *task_page;
1033 int task_idx, index;
1037 /* ASSUMPTION: called with tgt_lock held */
1038 BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_cleanup\n");
1040 port = io_req->port;
1041 interface = port->priv;
1042 lport = port->lport;
1044 cleanup_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_CLEANUP);
1045 if (!cleanup_io_req) {
1046 printk(KERN_ERR PFX "cleanup: couldnt allocate cmd\n");
1051 /* Initialize rest of io_req fields */
1052 cleanup_io_req->sc_cmd = NULL;
1053 cleanup_io_req->port = port;
1054 cleanup_io_req->tgt = tgt;
1055 cleanup_io_req->data_xfer_len = 0; /* No data transfer for cleanup */
1057 xid = cleanup_io_req->xid;
1059 task_idx = xid/BNX2FC_TASKS_PER_PAGE;
1060 index = xid % BNX2FC_TASKS_PER_PAGE;
1062 /* Initialize task context for this IO request */
1063 task_page = (struct fcoe_task_ctx_entry *)
1064 interface->hba->task_ctx[task_idx];
1065 task = &(task_page[index]);
1066 orig_xid = io_req->xid;
1068 BNX2FC_IO_DBG(io_req, "CLEANUP io_req xid = 0x%x\n", xid);
1070 bnx2fc_init_cleanup_task(cleanup_io_req, task, orig_xid);
1072 /* Obtain free SQ entry */
1073 bnx2fc_add_2_sq(tgt, xid);
1076 bnx2fc_ring_doorbell(tgt);
1083 * bnx2fc_eh_target_reset: Reset a target
1085 * @sc_cmd: SCSI command
1087 * Set from SCSI host template to send task mgmt command to the target
1088 * and wait for the response
1090 int bnx2fc_eh_target_reset(struct scsi_cmnd *sc_cmd)
1092 return bnx2fc_initiate_tmf(sc_cmd, FCP_TMF_TGT_RESET);
1096 * bnx2fc_eh_device_reset - Reset a single LUN
1098 * @sc_cmd: SCSI command
1100 * Set from SCSI host template to send task mgmt command to the target
1101 * and wait for the response
1103 int bnx2fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
1105 return bnx2fc_initiate_tmf(sc_cmd, FCP_TMF_LUN_RESET);
1108 int bnx2fc_expl_logo(struct fc_lport *lport, struct bnx2fc_cmd *io_req)
1110 struct bnx2fc_rport *tgt = io_req->tgt;
1111 struct fc_rport_priv *rdata = tgt->rdata;
1116 BNX2FC_IO_DBG(io_req, "Expl logo - tgt flags = 0x%lx\n",
1118 logo_issued = test_and_set_bit(BNX2FC_FLAG_EXPL_LOGO,
1120 io_req->wait_for_comp = 1;
1121 bnx2fc_initiate_cleanup(io_req);
1123 spin_unlock_bh(&tgt->tgt_lock);
1125 wait_for_completion(&io_req->tm_done);
1127 io_req->wait_for_comp = 0;
1129 * release the reference taken in eh_abort to allow the
1130 * target to re-login after flushing IOs
1132 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1135 clear_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags);
1136 mutex_lock(&lport->disc.disc_mutex);
1137 lport->tt.rport_logoff(rdata);
1138 mutex_unlock(&lport->disc.disc_mutex);
1140 msleep(BNX2FC_RELOGIN_WAIT_TIME);
1141 if (wait_cnt++ > BNX2FC_RELOGIN_WAIT_CNT) {
1145 } while (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags));
1147 spin_lock_bh(&tgt->tgt_lock);
1151 * bnx2fc_eh_abort - eh_abort_handler api to abort an outstanding
1154 * @sc_cmd: SCSI_ML command pointer
1156 * SCSI abort request handler
1158 int bnx2fc_eh_abort(struct scsi_cmnd *sc_cmd)
1160 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1161 struct fc_rport_libfc_priv *rp = rport->dd_data;
1162 struct bnx2fc_cmd *io_req;
1163 struct fc_lport *lport;
1164 struct bnx2fc_rport *tgt;
1168 rc = fc_block_scsi_eh(sc_cmd);
1172 lport = shost_priv(sc_cmd->device->host);
1173 if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
1174 printk(KERN_ERR PFX "eh_abort: link not ready\n");
1178 tgt = (struct bnx2fc_rport *)&rp[1];
1180 BNX2FC_TGT_DBG(tgt, "Entered bnx2fc_eh_abort\n");
1182 spin_lock_bh(&tgt->tgt_lock);
1183 io_req = (struct bnx2fc_cmd *)sc_cmd->SCp.ptr;
1185 /* Command might have just completed */
1186 printk(KERN_ERR PFX "eh_abort: io_req is NULL\n");
1187 spin_unlock_bh(&tgt->tgt_lock);
1190 BNX2FC_IO_DBG(io_req, "eh_abort - refcnt = %d\n",
1191 io_req->refcount.refcount.counter);
1193 /* Hold IO request across abort processing */
1194 kref_get(&io_req->refcount);
1196 BUG_ON(tgt != io_req->tgt);
1198 /* Remove the io_req from the active_q. */
1200 * Task Mgmt functions (LUN RESET & TGT RESET) will not
1201 * issue an ABTS on this particular IO req, as the
1202 * io_req is no longer in the active_q.
1204 if (tgt->flush_in_prog) {
1205 printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
1206 "flush in progress\n", io_req->xid);
1207 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1208 spin_unlock_bh(&tgt->tgt_lock);
1212 if (io_req->on_active_queue == 0) {
1213 printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
1214 "not on active_q\n", io_req->xid);
1216 * This condition can happen only due to the FW bug,
1217 * where we do not receive cleanup response from
1218 * the FW. Handle this case gracefully by erroring
1219 * back the IO request to SCSI-ml
1221 bnx2fc_scsi_done(io_req, DID_ABORT);
1223 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1224 spin_unlock_bh(&tgt->tgt_lock);
1229 * Only eh_abort processing will remove the IO from
1230 * active_cmd_q before processing the request. this is
1231 * done to avoid race conditions between IOs aborted
1232 * as part of task management completion and eh_abort
1235 list_del_init(&io_req->link);
1236 io_req->on_active_queue = 0;
1237 /* Move IO req to retire queue */
1238 list_add_tail(&io_req->link, &tgt->io_retire_queue);
1240 init_completion(&io_req->tm_done);
1242 if (test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags)) {
1243 printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
1244 "already in abts processing\n", io_req->xid);
1245 if (cancel_delayed_work(&io_req->timeout_work))
1246 kref_put(&io_req->refcount,
1247 bnx2fc_cmd_release); /* drop timer hold */
1248 rc = bnx2fc_expl_logo(lport, io_req);
1252 /* Cancel the current timer running on this io_req */
1253 if (cancel_delayed_work(&io_req->timeout_work))
1254 kref_put(&io_req->refcount,
1255 bnx2fc_cmd_release); /* drop timer hold */
1256 set_bit(BNX2FC_FLAG_EH_ABORT, &io_req->req_flags);
1257 io_req->wait_for_comp = 1;
1258 rc = bnx2fc_initiate_abts(io_req);
1260 bnx2fc_initiate_cleanup(io_req);
1261 spin_unlock_bh(&tgt->tgt_lock);
1262 wait_for_completion(&io_req->tm_done);
1263 spin_lock_bh(&tgt->tgt_lock);
1264 io_req->wait_for_comp = 0;
1267 spin_unlock_bh(&tgt->tgt_lock);
1269 wait_for_completion(&io_req->tm_done);
1271 spin_lock_bh(&tgt->tgt_lock);
1272 io_req->wait_for_comp = 0;
1273 if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
1274 &io_req->req_flags))) {
1275 /* Let the scsi-ml try to recover this command */
1276 printk(KERN_ERR PFX "abort failed, xid = 0x%x\n",
1278 rc = bnx2fc_expl_logo(lport, io_req);
1282 * We come here even when there was a race condition
1283 * between timeout and abts completion, and abts
1284 * completion happens just in time.
1286 BNX2FC_IO_DBG(io_req, "abort succeeded\n");
1288 bnx2fc_scsi_done(io_req, DID_ABORT);
1289 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1292 /* release the reference taken in eh_abort */
1293 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1295 spin_unlock_bh(&tgt->tgt_lock);
1299 void bnx2fc_process_seq_cleanup_compl(struct bnx2fc_cmd *seq_clnp_req,
1300 struct fcoe_task_ctx_entry *task,
1303 struct bnx2fc_els_cb_arg *cb_arg = seq_clnp_req->cb_arg;
1304 struct bnx2fc_cmd *orig_io_req = cb_arg->aborted_io_req;
1305 u32 offset = cb_arg->offset;
1306 enum fc_rctl r_ctl = cb_arg->r_ctl;
1308 struct bnx2fc_rport *tgt = orig_io_req->tgt;
1310 BNX2FC_IO_DBG(orig_io_req, "Entered process_cleanup_compl xid = 0x%x"
1312 seq_clnp_req->xid, seq_clnp_req->cmd_type);
1314 if (rx_state == FCOE_TASK_RX_STATE_IGNORED_SEQUENCE_CLEANUP) {
1315 printk(KERN_ERR PFX "seq cleanup ignored - xid = 0x%x\n",
1320 spin_unlock_bh(&tgt->tgt_lock);
1321 rc = bnx2fc_send_srr(orig_io_req, offset, r_ctl);
1322 spin_lock_bh(&tgt->tgt_lock);
1325 printk(KERN_ERR PFX "clnup_compl: Unable to send SRR"
1326 " IO will abort\n");
1327 seq_clnp_req->cb_arg = NULL;
1328 kref_put(&orig_io_req->refcount, bnx2fc_cmd_release);
1334 void bnx2fc_process_cleanup_compl(struct bnx2fc_cmd *io_req,
1335 struct fcoe_task_ctx_entry *task,
1338 BNX2FC_IO_DBG(io_req, "Entered process_cleanup_compl "
1339 "refcnt = %d, cmd_type = %d\n",
1340 io_req->refcount.refcount.counter, io_req->cmd_type);
1341 bnx2fc_scsi_done(io_req, DID_ERROR);
1342 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1343 if (io_req->wait_for_comp)
1344 complete(&io_req->tm_done);
1347 void bnx2fc_process_abts_compl(struct bnx2fc_cmd *io_req,
1348 struct fcoe_task_ctx_entry *task,
1352 u32 r_a_tov = FC_DEF_R_A_TOV;
1354 struct bnx2fc_rport *tgt = io_req->tgt;
1356 BNX2FC_IO_DBG(io_req, "Entered process_abts_compl xid = 0x%x"
1357 "refcnt = %d, cmd_type = %d\n",
1359 io_req->refcount.refcount.counter, io_req->cmd_type);
1361 if (test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
1362 &io_req->req_flags)) {
1363 BNX2FC_IO_DBG(io_req, "Timer context finished processing"
1368 /* Do not issue RRQ as this IO is already cleanedup */
1369 if (test_and_set_bit(BNX2FC_FLAG_IO_CLEANUP,
1370 &io_req->req_flags))
1374 * For ABTS issued due to SCSI eh_abort_handler, timeout
1375 * values are maintained by scsi-ml itself. Cancel timeout
1376 * in case ABTS issued as part of task management function
1377 * or due to FW error.
1379 if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags))
1380 if (cancel_delayed_work(&io_req->timeout_work))
1381 kref_put(&io_req->refcount,
1382 bnx2fc_cmd_release); /* drop timer hold */
1384 r_ctl = (u8)task->rxwr_only.union_ctx.comp_info.abts_rsp.r_ctl;
1387 case FC_RCTL_BA_ACC:
1389 * Dont release this cmd yet. It will be relesed
1390 * after we get RRQ response
1392 BNX2FC_IO_DBG(io_req, "ABTS response - ACC Send RRQ\n");
1396 case FC_RCTL_BA_RJT:
1397 BNX2FC_IO_DBG(io_req, "ABTS response - RJT\n");
1400 printk(KERN_ERR PFX "Unknown ABTS response\n");
1405 BNX2FC_IO_DBG(io_req, "Issue RRQ after R_A_TOV\n");
1406 set_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags);
1408 set_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags);
1409 bnx2fc_cmd_timer_set(io_req, r_a_tov);
1412 if (io_req->wait_for_comp) {
1413 if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
1414 &io_req->req_flags))
1415 complete(&io_req->tm_done);
1418 * We end up here when ABTS is issued as
1419 * in asynchronous context, i.e., as part
1420 * of task management completion, or
1421 * when FW error is received or when the
1422 * ABTS is issued when the IO is timed
1426 if (io_req->on_active_queue) {
1427 list_del_init(&io_req->link);
1428 io_req->on_active_queue = 0;
1429 /* Move IO req to retire queue */
1430 list_add_tail(&io_req->link, &tgt->io_retire_queue);
1432 bnx2fc_scsi_done(io_req, DID_ERROR);
1433 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1437 static void bnx2fc_lun_reset_cmpl(struct bnx2fc_cmd *io_req)
1439 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1440 struct bnx2fc_rport *tgt = io_req->tgt;
1441 struct bnx2fc_cmd *cmd, *tmp;
1442 int tm_lun = sc_cmd->device->lun;
1446 /* called with tgt_lock held */
1447 BNX2FC_IO_DBG(io_req, "Entered bnx2fc_lun_reset_cmpl\n");
1449 * Walk thru the active_ios queue and ABORT the IO
1450 * that matches with the LUN that was reset
1452 list_for_each_entry_safe(cmd, tmp, &tgt->active_cmd_queue, link) {
1453 BNX2FC_TGT_DBG(tgt, "LUN RST cmpl: scan for pending IOs\n");
1454 lun = cmd->sc_cmd->device->lun;
1455 if (lun == tm_lun) {
1456 /* Initiate ABTS on this cmd */
1457 if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
1459 /* cancel the IO timeout */
1460 if (cancel_delayed_work(&io_req->timeout_work))
1461 kref_put(&io_req->refcount,
1462 bnx2fc_cmd_release);
1464 rc = bnx2fc_initiate_abts(cmd);
1465 /* abts shouldn't fail in this context */
1466 WARN_ON(rc != SUCCESS);
1468 printk(KERN_ERR PFX "lun_rst: abts already in"
1469 " progress for this IO 0x%x\n",
1475 static void bnx2fc_tgt_reset_cmpl(struct bnx2fc_cmd *io_req)
1477 struct bnx2fc_rport *tgt = io_req->tgt;
1478 struct bnx2fc_cmd *cmd, *tmp;
1481 /* called with tgt_lock held */
1482 BNX2FC_IO_DBG(io_req, "Entered bnx2fc_tgt_reset_cmpl\n");
1484 * Walk thru the active_ios queue and ABORT the IO
1485 * that matches with the LUN that was reset
1487 list_for_each_entry_safe(cmd, tmp, &tgt->active_cmd_queue, link) {
1488 BNX2FC_TGT_DBG(tgt, "TGT RST cmpl: scan for pending IOs\n");
1490 if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
1492 /* cancel the IO timeout */
1493 if (cancel_delayed_work(&io_req->timeout_work))
1494 kref_put(&io_req->refcount,
1495 bnx2fc_cmd_release); /* timer hold */
1496 rc = bnx2fc_initiate_abts(cmd);
1497 /* abts shouldn't fail in this context */
1498 WARN_ON(rc != SUCCESS);
1501 printk(KERN_ERR PFX "tgt_rst: abts already in progress"
1502 " for this IO 0x%x\n", cmd->xid);
1506 void bnx2fc_process_tm_compl(struct bnx2fc_cmd *io_req,
1507 struct fcoe_task_ctx_entry *task, u8 num_rq)
1509 struct bnx2fc_mp_req *tm_req;
1510 struct fc_frame_header *fc_hdr;
1511 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1516 /* Called with tgt_lock held */
1517 BNX2FC_IO_DBG(io_req, "Entered process_tm_compl\n");
1519 if (!(test_bit(BNX2FC_FLAG_TM_TIMEOUT, &io_req->req_flags)))
1520 set_bit(BNX2FC_FLAG_TM_COMPL, &io_req->req_flags);
1522 /* TM has already timed out and we got
1523 * delayed completion. Ignore completion
1529 tm_req = &(io_req->mp_req);
1530 fc_hdr = &(tm_req->resp_fc_hdr);
1531 hdr = (u64 *)fc_hdr;
1533 &task->rxwr_only.union_ctx.comp_info.mp_rsp.fc_hdr;
1534 hdr[0] = cpu_to_be64(temp_hdr[0]);
1535 hdr[1] = cpu_to_be64(temp_hdr[1]);
1536 hdr[2] = cpu_to_be64(temp_hdr[2]);
1539 task->rxwr_only.union_ctx.comp_info.mp_rsp.mp_payload_len;
1541 rsp_buf = tm_req->resp_buf;
1543 if (fc_hdr->fh_r_ctl == FC_RCTL_DD_CMD_STATUS) {
1544 bnx2fc_parse_fcp_rsp(io_req,
1545 (struct fcoe_fcp_rsp_payload *)
1547 if (io_req->fcp_rsp_code == 0) {
1549 if (tm_req->tm_flags & FCP_TMF_LUN_RESET)
1550 bnx2fc_lun_reset_cmpl(io_req);
1551 else if (tm_req->tm_flags & FCP_TMF_TGT_RESET)
1552 bnx2fc_tgt_reset_cmpl(io_req);
1555 printk(KERN_ERR PFX "tmf's fc_hdr r_ctl = 0x%x\n",
1558 if (!sc_cmd->SCp.ptr) {
1559 printk(KERN_ERR PFX "tm_compl: SCp.ptr is NULL\n");
1562 switch (io_req->fcp_status) {
1564 if (io_req->cdb_status == 0) {
1565 /* Good IO completion */
1566 sc_cmd->result = DID_OK << 16;
1568 /* Transport status is good, SCSI status not good */
1569 sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
1571 if (io_req->fcp_resid)
1572 scsi_set_resid(sc_cmd, io_req->fcp_resid);
1576 BNX2FC_IO_DBG(io_req, "process_tm_compl: fcp_status = %d\n",
1577 io_req->fcp_status);
1581 sc_cmd = io_req->sc_cmd;
1582 io_req->sc_cmd = NULL;
1584 /* check if the io_req exists in tgt's tmf_q */
1585 if (io_req->on_tmf_queue) {
1587 list_del_init(&io_req->link);
1588 io_req->on_tmf_queue = 0;
1591 printk(KERN_ERR PFX "Command not on active_cmd_queue!\n");
1595 sc_cmd->SCp.ptr = NULL;
1596 sc_cmd->scsi_done(sc_cmd);
1598 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1599 if (io_req->wait_for_comp) {
1600 BNX2FC_IO_DBG(io_req, "tm_compl - wake up the waiter\n");
1601 complete(&io_req->tm_done);
1605 static int bnx2fc_split_bd(struct bnx2fc_cmd *io_req, u64 addr, int sg_len,
1608 struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
1609 int frag_size, sg_frags;
1613 if (sg_len >= BNX2FC_BD_SPLIT_SZ)
1614 frag_size = BNX2FC_BD_SPLIT_SZ;
1617 bd[bd_index + sg_frags].buf_addr_lo = addr & 0xffffffff;
1618 bd[bd_index + sg_frags].buf_addr_hi = addr >> 32;
1619 bd[bd_index + sg_frags].buf_len = (u16)frag_size;
1620 bd[bd_index + sg_frags].flags = 0;
1622 addr += (u64) frag_size;
1624 sg_len -= frag_size;
1630 static int bnx2fc_map_sg(struct bnx2fc_cmd *io_req)
1632 struct bnx2fc_interface *interface = io_req->port->priv;
1633 struct bnx2fc_hba *hba = interface->hba;
1634 struct scsi_cmnd *sc = io_req->sc_cmd;
1635 struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
1636 struct scatterlist *sg;
1641 unsigned int sg_len;
1645 sg_count = dma_map_sg(&hba->pcidev->dev, scsi_sglist(sc),
1646 scsi_sg_count(sc), sc->sc_data_direction);
1647 scsi_for_each_sg(sc, sg, sg_count, i) {
1648 sg_len = sg_dma_len(sg);
1649 addr = sg_dma_address(sg);
1650 if (sg_len > BNX2FC_MAX_BD_LEN) {
1651 sg_frags = bnx2fc_split_bd(io_req, addr, sg_len,
1656 bd[bd_count].buf_addr_lo = addr & 0xffffffff;
1657 bd[bd_count].buf_addr_hi = addr >> 32;
1658 bd[bd_count].buf_len = (u16)sg_len;
1659 bd[bd_count].flags = 0;
1661 bd_count += sg_frags;
1662 byte_count += sg_len;
1664 if (byte_count != scsi_bufflen(sc))
1665 printk(KERN_ERR PFX "byte_count = %d != scsi_bufflen = %d, "
1666 "task_id = 0x%x\n", byte_count, scsi_bufflen(sc),
1671 static int bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd *io_req)
1673 struct scsi_cmnd *sc = io_req->sc_cmd;
1674 struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
1677 if (scsi_sg_count(sc)) {
1678 bd_count = bnx2fc_map_sg(io_req);
1683 bd[0].buf_addr_lo = bd[0].buf_addr_hi = 0;
1684 bd[0].buf_len = bd[0].flags = 0;
1686 io_req->bd_tbl->bd_valid = bd_count;
1691 static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd *io_req)
1693 struct scsi_cmnd *sc = io_req->sc_cmd;
1695 if (io_req->bd_tbl->bd_valid && sc) {
1697 io_req->bd_tbl->bd_valid = 0;
1701 void bnx2fc_build_fcp_cmnd(struct bnx2fc_cmd *io_req,
1702 struct fcp_cmnd *fcp_cmnd)
1704 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1707 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
1709 int_to_scsilun(sc_cmd->device->lun, &fcp_cmnd->fc_lun);
1711 fcp_cmnd->fc_dl = htonl(io_req->data_xfer_len);
1712 memcpy(fcp_cmnd->fc_cdb, sc_cmd->cmnd, sc_cmd->cmd_len);
1714 fcp_cmnd->fc_cmdref = 0;
1715 fcp_cmnd->fc_pri_ta = 0;
1716 fcp_cmnd->fc_tm_flags = io_req->mp_req.tm_flags;
1717 fcp_cmnd->fc_flags = io_req->io_req_flags;
1719 if (scsi_populate_tag_msg(sc_cmd, tag)) {
1721 case HEAD_OF_QUEUE_TAG:
1722 fcp_cmnd->fc_pri_ta = FCP_PTA_HEADQ;
1724 case ORDERED_QUEUE_TAG:
1725 fcp_cmnd->fc_pri_ta = FCP_PTA_ORDERED;
1728 fcp_cmnd->fc_pri_ta = FCP_PTA_SIMPLE;
1732 fcp_cmnd->fc_pri_ta = 0;
1736 static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd *io_req,
1737 struct fcoe_fcp_rsp_payload *fcp_rsp,
1740 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1741 struct bnx2fc_rport *tgt = io_req->tgt;
1742 u8 rsp_flags = fcp_rsp->fcp_flags.flags;
1743 u32 rq_buff_len = 0;
1745 unsigned char *rq_data;
1746 unsigned char *dummy;
1747 int fcp_sns_len = 0;
1748 int fcp_rsp_len = 0;
1750 io_req->fcp_status = FC_GOOD;
1751 io_req->fcp_resid = fcp_rsp->fcp_resid;
1753 io_req->scsi_comp_flags = rsp_flags;
1754 CMD_SCSI_STATUS(sc_cmd) = io_req->cdb_status =
1755 fcp_rsp->scsi_status_code;
1757 /* Fetch fcp_rsp_info and fcp_sns_info if available */
1761 * We do not anticipate num_rq >1, as the linux defined
1762 * SCSI_SENSE_BUFFERSIZE is 96 bytes + 8 bytes of FCP_RSP_INFO
1763 * 256 bytes of single rq buffer is good enough to hold this.
1767 FCOE_FCP_RSP_FLAGS_FCP_RSP_LEN_VALID) {
1768 fcp_rsp_len = rq_buff_len
1769 = fcp_rsp->fcp_rsp_len;
1773 FCOE_FCP_RSP_FLAGS_FCP_SNS_LEN_VALID) {
1774 fcp_sns_len = fcp_rsp->fcp_sns_len;
1775 rq_buff_len += fcp_rsp->fcp_sns_len;
1778 io_req->fcp_rsp_len = fcp_rsp_len;
1779 io_req->fcp_sns_len = fcp_sns_len;
1781 if (rq_buff_len > num_rq * BNX2FC_RQ_BUF_SZ) {
1782 /* Invalid sense sense length. */
1783 printk(KERN_ERR PFX "invalid sns length %d\n",
1785 /* reset rq_buff_len */
1786 rq_buff_len = num_rq * BNX2FC_RQ_BUF_SZ;
1789 rq_data = bnx2fc_get_next_rqe(tgt, 1);
1792 /* We do not need extra sense data */
1793 for (i = 1; i < num_rq; i++)
1794 dummy = bnx2fc_get_next_rqe(tgt, 1);
1797 /* fetch fcp_rsp_code */
1798 if ((fcp_rsp_len == 4) || (fcp_rsp_len == 8)) {
1799 /* Only for task management function */
1800 io_req->fcp_rsp_code = rq_data[3];
1801 printk(KERN_ERR PFX "fcp_rsp_code = %d\n",
1802 io_req->fcp_rsp_code);
1805 /* fetch sense data */
1806 rq_data += fcp_rsp_len;
1808 if (fcp_sns_len > SCSI_SENSE_BUFFERSIZE) {
1809 printk(KERN_ERR PFX "Truncating sense buffer\n");
1810 fcp_sns_len = SCSI_SENSE_BUFFERSIZE;
1813 memset(sc_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1815 memcpy(sc_cmd->sense_buffer, rq_data, fcp_sns_len);
1817 /* return RQ entries */
1818 for (i = 0; i < num_rq; i++)
1819 bnx2fc_return_rqe(tgt, 1);
1824 * bnx2fc_queuecommand - Queuecommand function of the scsi template
1826 * @host: The Scsi_Host the command was issued to
1827 * @sc_cmd: struct scsi_cmnd to be executed
1829 * This is the IO strategy routine, called by SCSI-ML
1831 int bnx2fc_queuecommand(struct Scsi_Host *host,
1832 struct scsi_cmnd *sc_cmd)
1834 struct fc_lport *lport = shost_priv(host);
1835 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1836 struct fc_rport_libfc_priv *rp = rport->dd_data;
1837 struct bnx2fc_rport *tgt;
1838 struct bnx2fc_cmd *io_req;
1842 rval = fc_remote_port_chkready(rport);
1844 sc_cmd->result = rval;
1845 sc_cmd->scsi_done(sc_cmd);
1849 if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
1850 rc = SCSI_MLQUEUE_HOST_BUSY;
1854 /* rport and tgt are allocated together, so tgt should be non-NULL */
1855 tgt = (struct bnx2fc_rport *)&rp[1];
1857 if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
1859 * Session is not offloaded yet. Let SCSI-ml retry
1862 rc = SCSI_MLQUEUE_TARGET_BUSY;
1866 io_req = bnx2fc_cmd_alloc(tgt);
1868 rc = SCSI_MLQUEUE_HOST_BUSY;
1871 io_req->sc_cmd = sc_cmd;
1873 if (bnx2fc_post_io_req(tgt, io_req)) {
1874 printk(KERN_ERR PFX "Unable to post io_req\n");
1875 rc = SCSI_MLQUEUE_HOST_BUSY;
1882 void bnx2fc_process_scsi_cmd_compl(struct bnx2fc_cmd *io_req,
1883 struct fcoe_task_ctx_entry *task,
1886 struct fcoe_fcp_rsp_payload *fcp_rsp;
1887 struct bnx2fc_rport *tgt = io_req->tgt;
1888 struct scsi_cmnd *sc_cmd;
1889 struct Scsi_Host *host;
1892 /* scsi_cmd_cmpl is called with tgt lock held */
1894 if (test_and_set_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags)) {
1895 /* we will not receive ABTS response for this IO */
1896 BNX2FC_IO_DBG(io_req, "Timer context finished processing "
1900 /* Cancel the timeout_work, as we received IO completion */
1901 if (cancel_delayed_work(&io_req->timeout_work))
1902 kref_put(&io_req->refcount,
1903 bnx2fc_cmd_release); /* drop timer hold */
1905 sc_cmd = io_req->sc_cmd;
1906 if (sc_cmd == NULL) {
1907 printk(KERN_ERR PFX "scsi_cmd_compl - sc_cmd is NULL\n");
1911 /* Fetch fcp_rsp from task context and perform cmd completion */
1912 fcp_rsp = (struct fcoe_fcp_rsp_payload *)
1913 &(task->rxwr_only.union_ctx.comp_info.fcp_rsp.payload);
1915 /* parse fcp_rsp and obtain sense data from RQ if available */
1916 bnx2fc_parse_fcp_rsp(io_req, fcp_rsp, num_rq);
1918 host = sc_cmd->device->host;
1919 if (!sc_cmd->SCp.ptr) {
1920 printk(KERN_ERR PFX "SCp.ptr is NULL\n");
1924 if (io_req->on_active_queue) {
1925 list_del_init(&io_req->link);
1926 io_req->on_active_queue = 0;
1927 /* Move IO req to retire queue */
1928 list_add_tail(&io_req->link, &tgt->io_retire_queue);
1930 /* This should not happen, but could have been pulled
1931 * by bnx2fc_flush_active_ios(), or during a race
1932 * between command abort and (late) completion.
1934 BNX2FC_IO_DBG(io_req, "xid not on active_cmd_queue\n");
1935 if (io_req->wait_for_comp)
1936 if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
1937 &io_req->req_flags))
1938 complete(&io_req->tm_done);
1941 bnx2fc_unmap_sg_list(io_req);
1942 io_req->sc_cmd = NULL;
1944 switch (io_req->fcp_status) {
1946 if (io_req->cdb_status == 0) {
1947 /* Good IO completion */
1948 sc_cmd->result = DID_OK << 16;
1950 /* Transport status is good, SCSI status not good */
1951 BNX2FC_IO_DBG(io_req, "scsi_cmpl: cdb_status = %d"
1952 " fcp_resid = 0x%x\n",
1953 io_req->cdb_status, io_req->fcp_resid);
1954 sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
1956 if (io_req->fcp_resid)
1957 scsi_set_resid(sc_cmd, io_req->fcp_resid);
1960 printk(KERN_ERR PFX "scsi_cmd_compl: fcp_status = %d\n",
1961 io_req->fcp_status);
1964 sc_cmd->SCp.ptr = NULL;
1965 sc_cmd->scsi_done(sc_cmd);
1966 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1969 int bnx2fc_post_io_req(struct bnx2fc_rport *tgt,
1970 struct bnx2fc_cmd *io_req)
1972 struct fcoe_task_ctx_entry *task;
1973 struct fcoe_task_ctx_entry *task_page;
1974 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1975 struct fcoe_port *port = tgt->port;
1976 struct bnx2fc_interface *interface = port->priv;
1977 struct bnx2fc_hba *hba = interface->hba;
1978 struct fc_lport *lport = port->lport;
1979 struct fc_stats *stats;
1980 int task_idx, index;
1983 /* Initialize rest of io_req fields */
1984 io_req->cmd_type = BNX2FC_SCSI_CMD;
1985 io_req->port = port;
1987 io_req->data_xfer_len = scsi_bufflen(sc_cmd);
1988 sc_cmd->SCp.ptr = (char *)io_req;
1990 stats = per_cpu_ptr(lport->stats, get_cpu());
1991 if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
1992 io_req->io_req_flags = BNX2FC_READ;
1993 stats->InputRequests++;
1994 stats->InputBytes += io_req->data_xfer_len;
1995 } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
1996 io_req->io_req_flags = BNX2FC_WRITE;
1997 stats->OutputRequests++;
1998 stats->OutputBytes += io_req->data_xfer_len;
2000 io_req->io_req_flags = 0;
2001 stats->ControlRequests++;
2007 /* Build buffer descriptor list for firmware from sg list */
2008 if (bnx2fc_build_bd_list_from_sg(io_req)) {
2009 printk(KERN_ERR PFX "BD list creation failed\n");
2010 spin_lock_bh(&tgt->tgt_lock);
2011 kref_put(&io_req->refcount, bnx2fc_cmd_release);
2012 spin_unlock_bh(&tgt->tgt_lock);
2016 task_idx = xid / BNX2FC_TASKS_PER_PAGE;
2017 index = xid % BNX2FC_TASKS_PER_PAGE;
2019 /* Initialize task context for this IO request */
2020 task_page = (struct fcoe_task_ctx_entry *) hba->task_ctx[task_idx];
2021 task = &(task_page[index]);
2022 bnx2fc_init_task(io_req, task);
2024 spin_lock_bh(&tgt->tgt_lock);
2026 if (tgt->flush_in_prog) {
2027 printk(KERN_ERR PFX "Flush in progress..Host Busy\n");
2028 kref_put(&io_req->refcount, bnx2fc_cmd_release);
2029 spin_unlock_bh(&tgt->tgt_lock);
2033 if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
2034 printk(KERN_ERR PFX "Session not ready...post_io\n");
2035 kref_put(&io_req->refcount, bnx2fc_cmd_release);
2036 spin_unlock_bh(&tgt->tgt_lock);
2041 if (tgt->io_timeout)
2042 bnx2fc_cmd_timer_set(io_req, BNX2FC_IO_TIMEOUT);
2043 /* Obtain free SQ entry */
2044 bnx2fc_add_2_sq(tgt, xid);
2046 /* Enqueue the io_req to active_cmd_queue */
2048 io_req->on_active_queue = 1;
2049 /* move io_req from pending_queue to active_queue */
2050 list_add_tail(&io_req->link, &tgt->active_cmd_queue);
2053 bnx2fc_ring_doorbell(tgt);
2054 spin_unlock_bh(&tgt->tgt_lock);