1de909d87SChad Dupuis /* bnx2fc_io.c: QLogic Linux FCoE offload driver. 2853e2bd2SBhanu Gollapudi * IO manager and SCSI IO processing. 3853e2bd2SBhanu Gollapudi * 4cf122191SBhanu Prakash Gollapudi * Copyright (c) 2008-2013 Broadcom Corporation 597586090SChad Dupuis * Copyright (c) 2014-2015 QLogic Corporation 6853e2bd2SBhanu Gollapudi * 7853e2bd2SBhanu Gollapudi * This program is free software; you can redistribute it and/or modify 8853e2bd2SBhanu Gollapudi * it under the terms of the GNU General Public License as published by 9853e2bd2SBhanu Gollapudi * the Free Software Foundation. 10853e2bd2SBhanu Gollapudi * 11853e2bd2SBhanu Gollapudi * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com) 12853e2bd2SBhanu Gollapudi */ 13853e2bd2SBhanu Gollapudi 14853e2bd2SBhanu Gollapudi #include "bnx2fc.h" 150ea5c275SBhanu Gollapudi 160ea5c275SBhanu Gollapudi #define RESERVE_FREE_LIST_INDEX num_possible_cpus() 170ea5c275SBhanu Gollapudi 18853e2bd2SBhanu Gollapudi static int bnx2fc_split_bd(struct bnx2fc_cmd *io_req, u64 addr, int sg_len, 19853e2bd2SBhanu Gollapudi int bd_index); 20853e2bd2SBhanu Gollapudi static int bnx2fc_map_sg(struct bnx2fc_cmd *io_req); 21822f2903SBhanu Prakash Gollapudi static int bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd *io_req); 22853e2bd2SBhanu Gollapudi static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd *io_req); 23853e2bd2SBhanu Gollapudi static void bnx2fc_free_mp_resc(struct bnx2fc_cmd *io_req); 24853e2bd2SBhanu Gollapudi static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd *io_req, 25853e2bd2SBhanu Gollapudi struct fcoe_fcp_rsp_payload *fcp_rsp, 26853e2bd2SBhanu Gollapudi u8 num_rq); 27853e2bd2SBhanu Gollapudi 28853e2bd2SBhanu Gollapudi void bnx2fc_cmd_timer_set(struct bnx2fc_cmd *io_req, 29853e2bd2SBhanu Gollapudi unsigned int timer_msec) 30853e2bd2SBhanu Gollapudi { 31aea71a02SBhanu Prakash Gollapudi struct bnx2fc_interface *interface = io_req->port->priv; 32853e2bd2SBhanu Gollapudi 33aea71a02SBhanu Prakash Gollapudi if (queue_delayed_work(interface->timer_work_queue, 34aea71a02SBhanu Prakash Gollapudi &io_req->timeout_work, 35853e2bd2SBhanu Gollapudi msecs_to_jiffies(timer_msec))) 36853e2bd2SBhanu Gollapudi kref_get(&io_req->refcount); 37853e2bd2SBhanu Gollapudi } 38853e2bd2SBhanu Gollapudi 39853e2bd2SBhanu Gollapudi static void bnx2fc_cmd_timeout(struct work_struct *work) 40853e2bd2SBhanu Gollapudi { 41853e2bd2SBhanu Gollapudi struct bnx2fc_cmd *io_req = container_of(work, struct bnx2fc_cmd, 42853e2bd2SBhanu Gollapudi timeout_work.work); 43853e2bd2SBhanu Gollapudi u8 cmd_type = io_req->cmd_type; 44853e2bd2SBhanu Gollapudi struct bnx2fc_rport *tgt = io_req->tgt; 45853e2bd2SBhanu Gollapudi int rc; 46853e2bd2SBhanu Gollapudi 47853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "cmd_timeout, cmd_type = %d," 48853e2bd2SBhanu Gollapudi "req_flags = %lx\n", cmd_type, io_req->req_flags); 49853e2bd2SBhanu Gollapudi 50853e2bd2SBhanu Gollapudi spin_lock_bh(&tgt->tgt_lock); 51853e2bd2SBhanu Gollapudi if (test_and_clear_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags)) { 52853e2bd2SBhanu Gollapudi clear_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags); 53853e2bd2SBhanu Gollapudi /* 54853e2bd2SBhanu Gollapudi * ideally we should hold the io_req until RRQ complets, 55853e2bd2SBhanu Gollapudi * and release io_req from timeout hold. 56853e2bd2SBhanu Gollapudi */ 57853e2bd2SBhanu Gollapudi spin_unlock_bh(&tgt->tgt_lock); 58853e2bd2SBhanu Gollapudi bnx2fc_send_rrq(io_req); 59853e2bd2SBhanu Gollapudi return; 60853e2bd2SBhanu Gollapudi } 61853e2bd2SBhanu Gollapudi if (test_and_clear_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags)) { 62853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "IO ready for reuse now\n"); 63853e2bd2SBhanu Gollapudi goto done; 64853e2bd2SBhanu Gollapudi } 65853e2bd2SBhanu Gollapudi 66853e2bd2SBhanu Gollapudi switch (cmd_type) { 67853e2bd2SBhanu Gollapudi case BNX2FC_SCSI_CMD: 68853e2bd2SBhanu Gollapudi if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT, 69853e2bd2SBhanu Gollapudi &io_req->req_flags)) { 70853e2bd2SBhanu Gollapudi /* Handle eh_abort timeout */ 71853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "eh_abort timed out\n"); 72853e2bd2SBhanu Gollapudi complete(&io_req->tm_done); 73853e2bd2SBhanu Gollapudi } else if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, 74853e2bd2SBhanu Gollapudi &io_req->req_flags)) { 75853e2bd2SBhanu Gollapudi /* Handle internally generated ABTS timeout */ 76853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "ABTS timed out refcnt = %d\n", 77853e2bd2SBhanu Gollapudi io_req->refcount.refcount.counter); 78853e2bd2SBhanu Gollapudi if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE, 79853e2bd2SBhanu Gollapudi &io_req->req_flags))) { 8050a87414SChad Dupuis /* 8150a87414SChad Dupuis * Cleanup and return original command to 8250a87414SChad Dupuis * mid-layer. 8350a87414SChad Dupuis */ 8450a87414SChad Dupuis bnx2fc_initiate_cleanup(io_req); 85853e2bd2SBhanu Gollapudi kref_put(&io_req->refcount, bnx2fc_cmd_release); 86853e2bd2SBhanu Gollapudi spin_unlock_bh(&tgt->tgt_lock); 87853e2bd2SBhanu Gollapudi 88853e2bd2SBhanu Gollapudi return; 89853e2bd2SBhanu Gollapudi } 90853e2bd2SBhanu Gollapudi } else { 91853e2bd2SBhanu Gollapudi /* Hanlde IO timeout */ 92853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "IO timed out. issue ABTS\n"); 93853e2bd2SBhanu Gollapudi if (test_and_set_bit(BNX2FC_FLAG_IO_COMPL, 94853e2bd2SBhanu Gollapudi &io_req->req_flags)) { 95853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "IO completed before " 96853e2bd2SBhanu Gollapudi " timer expiry\n"); 97853e2bd2SBhanu Gollapudi goto done; 98853e2bd2SBhanu Gollapudi } 99853e2bd2SBhanu Gollapudi 100853e2bd2SBhanu Gollapudi if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS, 101853e2bd2SBhanu Gollapudi &io_req->req_flags)) { 102853e2bd2SBhanu Gollapudi rc = bnx2fc_initiate_abts(io_req); 103853e2bd2SBhanu Gollapudi if (rc == SUCCESS) 104853e2bd2SBhanu Gollapudi goto done; 10550a87414SChad Dupuis 106853e2bd2SBhanu Gollapudi kref_put(&io_req->refcount, bnx2fc_cmd_release); 107853e2bd2SBhanu Gollapudi spin_unlock_bh(&tgt->tgt_lock); 108853e2bd2SBhanu Gollapudi 109853e2bd2SBhanu Gollapudi return; 110853e2bd2SBhanu Gollapudi } else { 111853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "IO already in " 112853e2bd2SBhanu Gollapudi "ABTS processing\n"); 113853e2bd2SBhanu Gollapudi } 114853e2bd2SBhanu Gollapudi } 115853e2bd2SBhanu Gollapudi break; 116853e2bd2SBhanu Gollapudi case BNX2FC_ELS: 117853e2bd2SBhanu Gollapudi 118853e2bd2SBhanu Gollapudi if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags)) { 119853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "ABTS for ELS timed out\n"); 120853e2bd2SBhanu Gollapudi 121853e2bd2SBhanu Gollapudi if (!test_and_set_bit(BNX2FC_FLAG_ABTS_DONE, 122853e2bd2SBhanu Gollapudi &io_req->req_flags)) { 123853e2bd2SBhanu Gollapudi kref_put(&io_req->refcount, bnx2fc_cmd_release); 124853e2bd2SBhanu Gollapudi spin_unlock_bh(&tgt->tgt_lock); 125853e2bd2SBhanu Gollapudi 126853e2bd2SBhanu Gollapudi return; 127853e2bd2SBhanu Gollapudi } 128853e2bd2SBhanu Gollapudi } else { 129853e2bd2SBhanu Gollapudi /* 130853e2bd2SBhanu Gollapudi * Handle ELS timeout. 131853e2bd2SBhanu Gollapudi * tgt_lock is used to sync compl path and timeout 132853e2bd2SBhanu Gollapudi * path. If els compl path is processing this IO, we 133853e2bd2SBhanu Gollapudi * have nothing to do here, just release the timer hold 134853e2bd2SBhanu Gollapudi */ 135853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "ELS timed out\n"); 136853e2bd2SBhanu Gollapudi if (test_and_set_bit(BNX2FC_FLAG_ELS_DONE, 137853e2bd2SBhanu Gollapudi &io_req->req_flags)) 138853e2bd2SBhanu Gollapudi goto done; 139853e2bd2SBhanu Gollapudi 140853e2bd2SBhanu Gollapudi /* Indicate the cb_func that this ELS is timed out */ 141853e2bd2SBhanu Gollapudi set_bit(BNX2FC_FLAG_ELS_TIMEOUT, &io_req->req_flags); 142853e2bd2SBhanu Gollapudi 143853e2bd2SBhanu Gollapudi if ((io_req->cb_func) && (io_req->cb_arg)) { 144853e2bd2SBhanu Gollapudi io_req->cb_func(io_req->cb_arg); 145853e2bd2SBhanu Gollapudi io_req->cb_arg = NULL; 146853e2bd2SBhanu Gollapudi } 147853e2bd2SBhanu Gollapudi } 148853e2bd2SBhanu Gollapudi break; 149853e2bd2SBhanu Gollapudi default: 150853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "cmd_timeout: invalid cmd_type %d\n", 151853e2bd2SBhanu Gollapudi cmd_type); 152853e2bd2SBhanu Gollapudi break; 153853e2bd2SBhanu Gollapudi } 154853e2bd2SBhanu Gollapudi 155853e2bd2SBhanu Gollapudi done: 156853e2bd2SBhanu Gollapudi /* release the cmd that was held when timer was set */ 157853e2bd2SBhanu Gollapudi kref_put(&io_req->refcount, bnx2fc_cmd_release); 158853e2bd2SBhanu Gollapudi spin_unlock_bh(&tgt->tgt_lock); 159853e2bd2SBhanu Gollapudi } 160853e2bd2SBhanu Gollapudi 161853e2bd2SBhanu Gollapudi static void bnx2fc_scsi_done(struct bnx2fc_cmd *io_req, int err_code) 162853e2bd2SBhanu Gollapudi { 163853e2bd2SBhanu Gollapudi /* Called with host lock held */ 164853e2bd2SBhanu Gollapudi struct scsi_cmnd *sc_cmd = io_req->sc_cmd; 165853e2bd2SBhanu Gollapudi 166853e2bd2SBhanu Gollapudi /* 167853e2bd2SBhanu Gollapudi * active_cmd_queue may have other command types as well, 168853e2bd2SBhanu Gollapudi * and during flush operation, we want to error back only 169853e2bd2SBhanu Gollapudi * scsi commands. 170853e2bd2SBhanu Gollapudi */ 171853e2bd2SBhanu Gollapudi if (io_req->cmd_type != BNX2FC_SCSI_CMD) 172853e2bd2SBhanu Gollapudi return; 173853e2bd2SBhanu Gollapudi 174853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "scsi_done. err_code = 0x%x\n", err_code); 17574446954SBhanu Prakash Gollapudi if (test_bit(BNX2FC_FLAG_CMD_LOST, &io_req->req_flags)) { 17674446954SBhanu Prakash Gollapudi /* Do not call scsi done for this IO */ 17774446954SBhanu Prakash Gollapudi return; 17874446954SBhanu Prakash Gollapudi } 17974446954SBhanu Prakash Gollapudi 180853e2bd2SBhanu Gollapudi bnx2fc_unmap_sg_list(io_req); 181853e2bd2SBhanu Gollapudi io_req->sc_cmd = NULL; 182d02327a8SChad Dupuis 183d02327a8SChad Dupuis /* Sanity checks before returning command to mid-layer */ 184853e2bd2SBhanu Gollapudi if (!sc_cmd) { 185853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "scsi_done - sc_cmd NULL. " 186853e2bd2SBhanu Gollapudi "IO(0x%x) already cleaned up\n", 187853e2bd2SBhanu Gollapudi io_req->xid); 188853e2bd2SBhanu Gollapudi return; 189853e2bd2SBhanu Gollapudi } 190d02327a8SChad Dupuis if (!sc_cmd->device) { 191d02327a8SChad Dupuis pr_err(PFX "0x%x: sc_cmd->device is NULL.\n", io_req->xid); 192d02327a8SChad Dupuis return; 193d02327a8SChad Dupuis } 194d02327a8SChad Dupuis if (!sc_cmd->device->host) { 195d02327a8SChad Dupuis pr_err(PFX "0x%x: sc_cmd->device->host is NULL.\n", 196d02327a8SChad Dupuis io_req->xid); 197d02327a8SChad Dupuis return; 198d02327a8SChad Dupuis } 199d02327a8SChad Dupuis 200853e2bd2SBhanu Gollapudi sc_cmd->result = err_code << 16; 201853e2bd2SBhanu Gollapudi 202853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "sc=%p, result=0x%x, retries=%d, allowed=%d\n", 203853e2bd2SBhanu Gollapudi sc_cmd, host_byte(sc_cmd->result), sc_cmd->retries, 204853e2bd2SBhanu Gollapudi sc_cmd->allowed); 205853e2bd2SBhanu Gollapudi scsi_set_resid(sc_cmd, scsi_bufflen(sc_cmd)); 206853e2bd2SBhanu Gollapudi sc_cmd->SCp.ptr = NULL; 207853e2bd2SBhanu Gollapudi sc_cmd->scsi_done(sc_cmd); 208853e2bd2SBhanu Gollapudi } 209853e2bd2SBhanu Gollapudi 2100eb43b4bSBhanu Prakash Gollapudi struct bnx2fc_cmd_mgr *bnx2fc_cmd_mgr_alloc(struct bnx2fc_hba *hba) 211853e2bd2SBhanu Gollapudi { 212853e2bd2SBhanu Gollapudi struct bnx2fc_cmd_mgr *cmgr; 213853e2bd2SBhanu Gollapudi struct io_bdt *bdt_info; 214853e2bd2SBhanu Gollapudi struct bnx2fc_cmd *io_req; 215853e2bd2SBhanu Gollapudi size_t len; 216853e2bd2SBhanu Gollapudi u32 mem_size; 217853e2bd2SBhanu Gollapudi u16 xid; 218853e2bd2SBhanu Gollapudi int i; 2190ea5c275SBhanu Gollapudi int num_ios, num_pri_ios; 220853e2bd2SBhanu Gollapudi size_t bd_tbl_sz; 2210ea5c275SBhanu Gollapudi int arr_sz = num_possible_cpus() + 1; 2220eb43b4bSBhanu Prakash Gollapudi u16 min_xid = BNX2FC_MIN_XID; 2230eb43b4bSBhanu Prakash Gollapudi u16 max_xid = hba->max_xid; 224853e2bd2SBhanu Gollapudi 225853e2bd2SBhanu Gollapudi if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN) { 226853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "cmd_mgr_alloc: Invalid min_xid 0x%x \ 227853e2bd2SBhanu Gollapudi and max_xid 0x%x\n", min_xid, max_xid); 228853e2bd2SBhanu Gollapudi return NULL; 229853e2bd2SBhanu Gollapudi } 230853e2bd2SBhanu Gollapudi BNX2FC_MISC_DBG("min xid 0x%x, max xid 0x%x\n", min_xid, max_xid); 231853e2bd2SBhanu Gollapudi 232853e2bd2SBhanu Gollapudi num_ios = max_xid - min_xid + 1; 233853e2bd2SBhanu Gollapudi len = (num_ios * (sizeof(struct bnx2fc_cmd *))); 234853e2bd2SBhanu Gollapudi len += sizeof(struct bnx2fc_cmd_mgr); 235853e2bd2SBhanu Gollapudi 236853e2bd2SBhanu Gollapudi cmgr = kzalloc(len, GFP_KERNEL); 237853e2bd2SBhanu Gollapudi if (!cmgr) { 238853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "failed to alloc cmgr\n"); 239853e2bd2SBhanu Gollapudi return NULL; 240853e2bd2SBhanu Gollapudi } 241853e2bd2SBhanu Gollapudi 242853e2bd2SBhanu Gollapudi cmgr->free_list = kzalloc(sizeof(*cmgr->free_list) * 2430ea5c275SBhanu Gollapudi arr_sz, GFP_KERNEL); 244853e2bd2SBhanu Gollapudi if (!cmgr->free_list) { 245853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "failed to alloc free_list\n"); 246853e2bd2SBhanu Gollapudi goto mem_err; 247853e2bd2SBhanu Gollapudi } 248853e2bd2SBhanu Gollapudi 249853e2bd2SBhanu Gollapudi cmgr->free_list_lock = kzalloc(sizeof(*cmgr->free_list_lock) * 2500ea5c275SBhanu Gollapudi arr_sz, GFP_KERNEL); 251853e2bd2SBhanu Gollapudi if (!cmgr->free_list_lock) { 252853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "failed to alloc free_list_lock\n"); 2539172b763SMaurizio Lombardi kfree(cmgr->free_list); 2549172b763SMaurizio Lombardi cmgr->free_list = NULL; 255853e2bd2SBhanu Gollapudi goto mem_err; 256853e2bd2SBhanu Gollapudi } 257853e2bd2SBhanu Gollapudi 258853e2bd2SBhanu Gollapudi cmgr->hba = hba; 259853e2bd2SBhanu Gollapudi cmgr->cmds = (struct bnx2fc_cmd **)(cmgr + 1); 260853e2bd2SBhanu Gollapudi 2610ea5c275SBhanu Gollapudi for (i = 0; i < arr_sz; i++) { 262853e2bd2SBhanu Gollapudi INIT_LIST_HEAD(&cmgr->free_list[i]); 263853e2bd2SBhanu Gollapudi spin_lock_init(&cmgr->free_list_lock[i]); 264853e2bd2SBhanu Gollapudi } 265853e2bd2SBhanu Gollapudi 2660ea5c275SBhanu Gollapudi /* 2670ea5c275SBhanu Gollapudi * Pre-allocated pool of bnx2fc_cmds. 2680ea5c275SBhanu Gollapudi * Last entry in the free list array is the free list 2690ea5c275SBhanu Gollapudi * of slow path requests. 2700ea5c275SBhanu Gollapudi */ 271853e2bd2SBhanu Gollapudi xid = BNX2FC_MIN_XID; 2720eb43b4bSBhanu Prakash Gollapudi num_pri_ios = num_ios - hba->elstm_xids; 273853e2bd2SBhanu Gollapudi for (i = 0; i < num_ios; i++) { 274853e2bd2SBhanu Gollapudi io_req = kzalloc(sizeof(*io_req), GFP_KERNEL); 275853e2bd2SBhanu Gollapudi 276853e2bd2SBhanu Gollapudi if (!io_req) { 277853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "failed to alloc io_req\n"); 278853e2bd2SBhanu Gollapudi goto mem_err; 279853e2bd2SBhanu Gollapudi } 280853e2bd2SBhanu Gollapudi 281853e2bd2SBhanu Gollapudi INIT_LIST_HEAD(&io_req->link); 282853e2bd2SBhanu Gollapudi INIT_DELAYED_WORK(&io_req->timeout_work, bnx2fc_cmd_timeout); 283853e2bd2SBhanu Gollapudi 284853e2bd2SBhanu Gollapudi io_req->xid = xid++; 2850ea5c275SBhanu Gollapudi if (i < num_pri_ios) 286853e2bd2SBhanu Gollapudi list_add_tail(&io_req->link, 2870ea5c275SBhanu Gollapudi &cmgr->free_list[io_req->xid % 2880ea5c275SBhanu Gollapudi num_possible_cpus()]); 2890ea5c275SBhanu Gollapudi else 2900ea5c275SBhanu Gollapudi list_add_tail(&io_req->link, 2910ea5c275SBhanu Gollapudi &cmgr->free_list[num_possible_cpus()]); 292853e2bd2SBhanu Gollapudi io_req++; 293853e2bd2SBhanu Gollapudi } 294853e2bd2SBhanu Gollapudi 295853e2bd2SBhanu Gollapudi /* Allocate pool of io_bdts - one for each bnx2fc_cmd */ 296853e2bd2SBhanu Gollapudi mem_size = num_ios * sizeof(struct io_bdt *); 297853e2bd2SBhanu Gollapudi cmgr->io_bdt_pool = kmalloc(mem_size, GFP_KERNEL); 298853e2bd2SBhanu Gollapudi if (!cmgr->io_bdt_pool) { 299853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "failed to alloc io_bdt_pool\n"); 300853e2bd2SBhanu Gollapudi goto mem_err; 301853e2bd2SBhanu Gollapudi } 302853e2bd2SBhanu Gollapudi 303853e2bd2SBhanu Gollapudi mem_size = sizeof(struct io_bdt); 304853e2bd2SBhanu Gollapudi for (i = 0; i < num_ios; i++) { 305853e2bd2SBhanu Gollapudi cmgr->io_bdt_pool[i] = kmalloc(mem_size, GFP_KERNEL); 306853e2bd2SBhanu Gollapudi if (!cmgr->io_bdt_pool[i]) { 307853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "failed to alloc " 308853e2bd2SBhanu Gollapudi "io_bdt_pool[%d]\n", i); 309853e2bd2SBhanu Gollapudi goto mem_err; 310853e2bd2SBhanu Gollapudi } 311853e2bd2SBhanu Gollapudi } 312853e2bd2SBhanu Gollapudi 313853e2bd2SBhanu Gollapudi /* Allocate an map fcoe_bdt_ctx structures */ 314853e2bd2SBhanu Gollapudi bd_tbl_sz = BNX2FC_MAX_BDS_PER_CMD * sizeof(struct fcoe_bd_ctx); 315853e2bd2SBhanu Gollapudi for (i = 0; i < num_ios; i++) { 316853e2bd2SBhanu Gollapudi bdt_info = cmgr->io_bdt_pool[i]; 317853e2bd2SBhanu Gollapudi bdt_info->bd_tbl = dma_alloc_coherent(&hba->pcidev->dev, 318853e2bd2SBhanu Gollapudi bd_tbl_sz, 319853e2bd2SBhanu Gollapudi &bdt_info->bd_tbl_dma, 320853e2bd2SBhanu Gollapudi GFP_KERNEL); 321853e2bd2SBhanu Gollapudi if (!bdt_info->bd_tbl) { 322853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "failed to alloc " 323853e2bd2SBhanu Gollapudi "bdt_tbl[%d]\n", i); 324853e2bd2SBhanu Gollapudi goto mem_err; 325853e2bd2SBhanu Gollapudi } 326853e2bd2SBhanu Gollapudi } 327853e2bd2SBhanu Gollapudi 328853e2bd2SBhanu Gollapudi return cmgr; 329853e2bd2SBhanu Gollapudi 330853e2bd2SBhanu Gollapudi mem_err: 331853e2bd2SBhanu Gollapudi bnx2fc_cmd_mgr_free(cmgr); 332853e2bd2SBhanu Gollapudi return NULL; 333853e2bd2SBhanu Gollapudi } 334853e2bd2SBhanu Gollapudi 335853e2bd2SBhanu Gollapudi void bnx2fc_cmd_mgr_free(struct bnx2fc_cmd_mgr *cmgr) 336853e2bd2SBhanu Gollapudi { 337853e2bd2SBhanu Gollapudi struct io_bdt *bdt_info; 338853e2bd2SBhanu Gollapudi struct bnx2fc_hba *hba = cmgr->hba; 339853e2bd2SBhanu Gollapudi size_t bd_tbl_sz; 340853e2bd2SBhanu Gollapudi u16 min_xid = BNX2FC_MIN_XID; 3410eb43b4bSBhanu Prakash Gollapudi u16 max_xid = hba->max_xid; 342853e2bd2SBhanu Gollapudi int num_ios; 343853e2bd2SBhanu Gollapudi int i; 344853e2bd2SBhanu Gollapudi 345853e2bd2SBhanu Gollapudi num_ios = max_xid - min_xid + 1; 346853e2bd2SBhanu Gollapudi 347853e2bd2SBhanu Gollapudi /* Free fcoe_bdt_ctx structures */ 348853e2bd2SBhanu Gollapudi if (!cmgr->io_bdt_pool) 349853e2bd2SBhanu Gollapudi goto free_cmd_pool; 350853e2bd2SBhanu Gollapudi 351853e2bd2SBhanu Gollapudi bd_tbl_sz = BNX2FC_MAX_BDS_PER_CMD * sizeof(struct fcoe_bd_ctx); 352853e2bd2SBhanu Gollapudi for (i = 0; i < num_ios; i++) { 353853e2bd2SBhanu Gollapudi bdt_info = cmgr->io_bdt_pool[i]; 354853e2bd2SBhanu Gollapudi if (bdt_info->bd_tbl) { 355853e2bd2SBhanu Gollapudi dma_free_coherent(&hba->pcidev->dev, bd_tbl_sz, 356853e2bd2SBhanu Gollapudi bdt_info->bd_tbl, 357853e2bd2SBhanu Gollapudi bdt_info->bd_tbl_dma); 358853e2bd2SBhanu Gollapudi bdt_info->bd_tbl = NULL; 359853e2bd2SBhanu Gollapudi } 360853e2bd2SBhanu Gollapudi } 361853e2bd2SBhanu Gollapudi 362853e2bd2SBhanu Gollapudi /* Destroy io_bdt pool */ 363853e2bd2SBhanu Gollapudi for (i = 0; i < num_ios; i++) { 364853e2bd2SBhanu Gollapudi kfree(cmgr->io_bdt_pool[i]); 365853e2bd2SBhanu Gollapudi cmgr->io_bdt_pool[i] = NULL; 366853e2bd2SBhanu Gollapudi } 367853e2bd2SBhanu Gollapudi 368853e2bd2SBhanu Gollapudi kfree(cmgr->io_bdt_pool); 369853e2bd2SBhanu Gollapudi cmgr->io_bdt_pool = NULL; 370853e2bd2SBhanu Gollapudi 371853e2bd2SBhanu Gollapudi free_cmd_pool: 372853e2bd2SBhanu Gollapudi kfree(cmgr->free_list_lock); 373853e2bd2SBhanu Gollapudi 374853e2bd2SBhanu Gollapudi /* Destroy cmd pool */ 375853e2bd2SBhanu Gollapudi if (!cmgr->free_list) 376853e2bd2SBhanu Gollapudi goto free_cmgr; 377853e2bd2SBhanu Gollapudi 3780ea5c275SBhanu Gollapudi for (i = 0; i < num_possible_cpus() + 1; i++) { 379d71fb3bdSBhanu Prakash Gollapudi struct bnx2fc_cmd *tmp, *io_req; 380853e2bd2SBhanu Gollapudi 381d71fb3bdSBhanu Prakash Gollapudi list_for_each_entry_safe(io_req, tmp, 382d71fb3bdSBhanu Prakash Gollapudi &cmgr->free_list[i], link) { 383853e2bd2SBhanu Gollapudi list_del(&io_req->link); 384853e2bd2SBhanu Gollapudi kfree(io_req); 385853e2bd2SBhanu Gollapudi } 386853e2bd2SBhanu Gollapudi } 387853e2bd2SBhanu Gollapudi kfree(cmgr->free_list); 388853e2bd2SBhanu Gollapudi free_cmgr: 389853e2bd2SBhanu Gollapudi /* Free command manager itself */ 390853e2bd2SBhanu Gollapudi kfree(cmgr); 391853e2bd2SBhanu Gollapudi } 392853e2bd2SBhanu Gollapudi 393853e2bd2SBhanu Gollapudi struct bnx2fc_cmd *bnx2fc_elstm_alloc(struct bnx2fc_rport *tgt, int type) 394853e2bd2SBhanu Gollapudi { 395853e2bd2SBhanu Gollapudi struct fcoe_port *port = tgt->port; 396aea71a02SBhanu Prakash Gollapudi struct bnx2fc_interface *interface = port->priv; 397aea71a02SBhanu Prakash Gollapudi struct bnx2fc_cmd_mgr *cmd_mgr = interface->hba->cmd_mgr; 398853e2bd2SBhanu Gollapudi struct bnx2fc_cmd *io_req; 399853e2bd2SBhanu Gollapudi struct list_head *listp; 400853e2bd2SBhanu Gollapudi struct io_bdt *bd_tbl; 4010ea5c275SBhanu Gollapudi int index = RESERVE_FREE_LIST_INDEX; 402619c5cb6SVlad Zolotarov u32 free_sqes; 403853e2bd2SBhanu Gollapudi u32 max_sqes; 404853e2bd2SBhanu Gollapudi u16 xid; 405853e2bd2SBhanu Gollapudi 406853e2bd2SBhanu Gollapudi max_sqes = tgt->max_sqes; 407853e2bd2SBhanu Gollapudi switch (type) { 408853e2bd2SBhanu Gollapudi case BNX2FC_TASK_MGMT_CMD: 409853e2bd2SBhanu Gollapudi max_sqes = BNX2FC_TM_MAX_SQES; 410853e2bd2SBhanu Gollapudi break; 411853e2bd2SBhanu Gollapudi case BNX2FC_ELS: 412853e2bd2SBhanu Gollapudi max_sqes = BNX2FC_ELS_MAX_SQES; 413853e2bd2SBhanu Gollapudi break; 414853e2bd2SBhanu Gollapudi default: 415853e2bd2SBhanu Gollapudi break; 416853e2bd2SBhanu Gollapudi } 417853e2bd2SBhanu Gollapudi 418853e2bd2SBhanu Gollapudi /* 419853e2bd2SBhanu Gollapudi * NOTE: Free list insertions and deletions are protected with 420853e2bd2SBhanu Gollapudi * cmgr lock 421853e2bd2SBhanu Gollapudi */ 4220ea5c275SBhanu Gollapudi spin_lock_bh(&cmd_mgr->free_list_lock[index]); 423619c5cb6SVlad Zolotarov free_sqes = atomic_read(&tgt->free_sqes); 4240ea5c275SBhanu Gollapudi if ((list_empty(&(cmd_mgr->free_list[index]))) || 425619c5cb6SVlad Zolotarov (tgt->num_active_ios.counter >= max_sqes) || 426619c5cb6SVlad Zolotarov (free_sqes + max_sqes <= BNX2FC_SQ_WQES_MAX)) { 427853e2bd2SBhanu Gollapudi BNX2FC_TGT_DBG(tgt, "No free els_tm cmds available " 428853e2bd2SBhanu Gollapudi "ios(%d):sqes(%d)\n", 429853e2bd2SBhanu Gollapudi tgt->num_active_ios.counter, tgt->max_sqes); 4300ea5c275SBhanu Gollapudi if (list_empty(&(cmd_mgr->free_list[index]))) 431853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "elstm_alloc: list_empty\n"); 4320ea5c275SBhanu Gollapudi spin_unlock_bh(&cmd_mgr->free_list_lock[index]); 433853e2bd2SBhanu Gollapudi return NULL; 434853e2bd2SBhanu Gollapudi } 435853e2bd2SBhanu Gollapudi 436853e2bd2SBhanu Gollapudi listp = (struct list_head *) 4370ea5c275SBhanu Gollapudi cmd_mgr->free_list[index].next; 438853e2bd2SBhanu Gollapudi list_del_init(listp); 439853e2bd2SBhanu Gollapudi io_req = (struct bnx2fc_cmd *) listp; 440853e2bd2SBhanu Gollapudi xid = io_req->xid; 441853e2bd2SBhanu Gollapudi cmd_mgr->cmds[xid] = io_req; 442853e2bd2SBhanu Gollapudi atomic_inc(&tgt->num_active_ios); 443619c5cb6SVlad Zolotarov atomic_dec(&tgt->free_sqes); 4440ea5c275SBhanu Gollapudi spin_unlock_bh(&cmd_mgr->free_list_lock[index]); 445853e2bd2SBhanu Gollapudi 446853e2bd2SBhanu Gollapudi INIT_LIST_HEAD(&io_req->link); 447853e2bd2SBhanu Gollapudi 448853e2bd2SBhanu Gollapudi io_req->port = port; 449853e2bd2SBhanu Gollapudi io_req->cmd_mgr = cmd_mgr; 450853e2bd2SBhanu Gollapudi io_req->req_flags = 0; 451853e2bd2SBhanu Gollapudi io_req->cmd_type = type; 452853e2bd2SBhanu Gollapudi 453853e2bd2SBhanu Gollapudi /* Bind io_bdt for this io_req */ 454853e2bd2SBhanu Gollapudi /* Have a static link between io_req and io_bdt_pool */ 455853e2bd2SBhanu Gollapudi bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid]; 456853e2bd2SBhanu Gollapudi bd_tbl->io_req = io_req; 457853e2bd2SBhanu Gollapudi 458853e2bd2SBhanu Gollapudi /* Hold the io_req against deletion */ 459853e2bd2SBhanu Gollapudi kref_init(&io_req->refcount); 460853e2bd2SBhanu Gollapudi return io_req; 461853e2bd2SBhanu Gollapudi } 462aea71a02SBhanu Prakash Gollapudi 463aea71a02SBhanu Prakash Gollapudi struct bnx2fc_cmd *bnx2fc_cmd_alloc(struct bnx2fc_rport *tgt) 464853e2bd2SBhanu Gollapudi { 465853e2bd2SBhanu Gollapudi struct fcoe_port *port = tgt->port; 466aea71a02SBhanu Prakash Gollapudi struct bnx2fc_interface *interface = port->priv; 467aea71a02SBhanu Prakash Gollapudi struct bnx2fc_cmd_mgr *cmd_mgr = interface->hba->cmd_mgr; 468853e2bd2SBhanu Gollapudi struct bnx2fc_cmd *io_req; 469853e2bd2SBhanu Gollapudi struct list_head *listp; 470853e2bd2SBhanu Gollapudi struct io_bdt *bd_tbl; 471619c5cb6SVlad Zolotarov u32 free_sqes; 472853e2bd2SBhanu Gollapudi u32 max_sqes; 473853e2bd2SBhanu Gollapudi u16 xid; 4740ea5c275SBhanu Gollapudi int index = get_cpu(); 475853e2bd2SBhanu Gollapudi 476853e2bd2SBhanu Gollapudi max_sqes = BNX2FC_SCSI_MAX_SQES; 477853e2bd2SBhanu Gollapudi /* 478853e2bd2SBhanu Gollapudi * NOTE: Free list insertions and deletions are protected with 479853e2bd2SBhanu Gollapudi * cmgr lock 480853e2bd2SBhanu Gollapudi */ 4810ea5c275SBhanu Gollapudi spin_lock_bh(&cmd_mgr->free_list_lock[index]); 482619c5cb6SVlad Zolotarov free_sqes = atomic_read(&tgt->free_sqes); 4830ea5c275SBhanu Gollapudi if ((list_empty(&cmd_mgr->free_list[index])) || 484619c5cb6SVlad Zolotarov (tgt->num_active_ios.counter >= max_sqes) || 485619c5cb6SVlad Zolotarov (free_sqes + max_sqes <= BNX2FC_SQ_WQES_MAX)) { 4860ea5c275SBhanu Gollapudi spin_unlock_bh(&cmd_mgr->free_list_lock[index]); 4870ea5c275SBhanu Gollapudi put_cpu(); 488853e2bd2SBhanu Gollapudi return NULL; 489853e2bd2SBhanu Gollapudi } 490853e2bd2SBhanu Gollapudi 491853e2bd2SBhanu Gollapudi listp = (struct list_head *) 4920ea5c275SBhanu Gollapudi cmd_mgr->free_list[index].next; 493853e2bd2SBhanu Gollapudi list_del_init(listp); 494853e2bd2SBhanu Gollapudi io_req = (struct bnx2fc_cmd *) listp; 495853e2bd2SBhanu Gollapudi xid = io_req->xid; 496853e2bd2SBhanu Gollapudi cmd_mgr->cmds[xid] = io_req; 497853e2bd2SBhanu Gollapudi atomic_inc(&tgt->num_active_ios); 498619c5cb6SVlad Zolotarov atomic_dec(&tgt->free_sqes); 4990ea5c275SBhanu Gollapudi spin_unlock_bh(&cmd_mgr->free_list_lock[index]); 5000ea5c275SBhanu Gollapudi put_cpu(); 501853e2bd2SBhanu Gollapudi 502853e2bd2SBhanu Gollapudi INIT_LIST_HEAD(&io_req->link); 503853e2bd2SBhanu Gollapudi 504853e2bd2SBhanu Gollapudi io_req->port = port; 505853e2bd2SBhanu Gollapudi io_req->cmd_mgr = cmd_mgr; 506853e2bd2SBhanu Gollapudi io_req->req_flags = 0; 507853e2bd2SBhanu Gollapudi 508853e2bd2SBhanu Gollapudi /* Bind io_bdt for this io_req */ 509853e2bd2SBhanu Gollapudi /* Have a static link between io_req and io_bdt_pool */ 510853e2bd2SBhanu Gollapudi bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid]; 511853e2bd2SBhanu Gollapudi bd_tbl->io_req = io_req; 512853e2bd2SBhanu Gollapudi 513853e2bd2SBhanu Gollapudi /* Hold the io_req against deletion */ 514853e2bd2SBhanu Gollapudi kref_init(&io_req->refcount); 515853e2bd2SBhanu Gollapudi return io_req; 516853e2bd2SBhanu Gollapudi } 517853e2bd2SBhanu Gollapudi 518853e2bd2SBhanu Gollapudi void bnx2fc_cmd_release(struct kref *ref) 519853e2bd2SBhanu Gollapudi { 520853e2bd2SBhanu Gollapudi struct bnx2fc_cmd *io_req = container_of(ref, 521853e2bd2SBhanu Gollapudi struct bnx2fc_cmd, refcount); 522853e2bd2SBhanu Gollapudi struct bnx2fc_cmd_mgr *cmd_mgr = io_req->cmd_mgr; 5230ea5c275SBhanu Gollapudi int index; 524853e2bd2SBhanu Gollapudi 5250ea5c275SBhanu Gollapudi if (io_req->cmd_type == BNX2FC_SCSI_CMD) 5260ea5c275SBhanu Gollapudi index = io_req->xid % num_possible_cpus(); 5270ea5c275SBhanu Gollapudi else 5280ea5c275SBhanu Gollapudi index = RESERVE_FREE_LIST_INDEX; 5290ea5c275SBhanu Gollapudi 5300ea5c275SBhanu Gollapudi 5310ea5c275SBhanu Gollapudi spin_lock_bh(&cmd_mgr->free_list_lock[index]); 532853e2bd2SBhanu Gollapudi if (io_req->cmd_type != BNX2FC_SCSI_CMD) 533853e2bd2SBhanu Gollapudi bnx2fc_free_mp_resc(io_req); 534853e2bd2SBhanu Gollapudi cmd_mgr->cmds[io_req->xid] = NULL; 535853e2bd2SBhanu Gollapudi /* Delete IO from retire queue */ 536853e2bd2SBhanu Gollapudi list_del_init(&io_req->link); 537853e2bd2SBhanu Gollapudi /* Add it to the free list */ 538853e2bd2SBhanu Gollapudi list_add(&io_req->link, 5390ea5c275SBhanu Gollapudi &cmd_mgr->free_list[index]); 540853e2bd2SBhanu Gollapudi atomic_dec(&io_req->tgt->num_active_ios); 5410ea5c275SBhanu Gollapudi spin_unlock_bh(&cmd_mgr->free_list_lock[index]); 5420ea5c275SBhanu Gollapudi 543853e2bd2SBhanu Gollapudi } 544853e2bd2SBhanu Gollapudi 545853e2bd2SBhanu Gollapudi static void bnx2fc_free_mp_resc(struct bnx2fc_cmd *io_req) 546853e2bd2SBhanu Gollapudi { 547853e2bd2SBhanu Gollapudi struct bnx2fc_mp_req *mp_req = &(io_req->mp_req); 548aea71a02SBhanu Prakash Gollapudi struct bnx2fc_interface *interface = io_req->port->priv; 549aea71a02SBhanu Prakash Gollapudi struct bnx2fc_hba *hba = interface->hba; 550853e2bd2SBhanu Gollapudi size_t sz = sizeof(struct fcoe_bd_ctx); 551853e2bd2SBhanu Gollapudi 552853e2bd2SBhanu Gollapudi /* clear tm flags */ 553853e2bd2SBhanu Gollapudi mp_req->tm_flags = 0; 554853e2bd2SBhanu Gollapudi if (mp_req->mp_req_bd) { 555853e2bd2SBhanu Gollapudi dma_free_coherent(&hba->pcidev->dev, sz, 556853e2bd2SBhanu Gollapudi mp_req->mp_req_bd, 557853e2bd2SBhanu Gollapudi mp_req->mp_req_bd_dma); 558853e2bd2SBhanu Gollapudi mp_req->mp_req_bd = NULL; 559853e2bd2SBhanu Gollapudi } 560853e2bd2SBhanu Gollapudi if (mp_req->mp_resp_bd) { 561853e2bd2SBhanu Gollapudi dma_free_coherent(&hba->pcidev->dev, sz, 562853e2bd2SBhanu Gollapudi mp_req->mp_resp_bd, 563853e2bd2SBhanu Gollapudi mp_req->mp_resp_bd_dma); 564853e2bd2SBhanu Gollapudi mp_req->mp_resp_bd = NULL; 565853e2bd2SBhanu Gollapudi } 566853e2bd2SBhanu Gollapudi if (mp_req->req_buf) { 567be1fefc2SMichael Chan dma_free_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE, 568853e2bd2SBhanu Gollapudi mp_req->req_buf, 569853e2bd2SBhanu Gollapudi mp_req->req_buf_dma); 570853e2bd2SBhanu Gollapudi mp_req->req_buf = NULL; 571853e2bd2SBhanu Gollapudi } 572853e2bd2SBhanu Gollapudi if (mp_req->resp_buf) { 573be1fefc2SMichael Chan dma_free_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE, 574853e2bd2SBhanu Gollapudi mp_req->resp_buf, 575853e2bd2SBhanu Gollapudi mp_req->resp_buf_dma); 576853e2bd2SBhanu Gollapudi mp_req->resp_buf = NULL; 577853e2bd2SBhanu Gollapudi } 578853e2bd2SBhanu Gollapudi } 579853e2bd2SBhanu Gollapudi 580853e2bd2SBhanu Gollapudi int bnx2fc_init_mp_req(struct bnx2fc_cmd *io_req) 581853e2bd2SBhanu Gollapudi { 582853e2bd2SBhanu Gollapudi struct bnx2fc_mp_req *mp_req; 583853e2bd2SBhanu Gollapudi struct fcoe_bd_ctx *mp_req_bd; 584853e2bd2SBhanu Gollapudi struct fcoe_bd_ctx *mp_resp_bd; 585aea71a02SBhanu Prakash Gollapudi struct bnx2fc_interface *interface = io_req->port->priv; 586aea71a02SBhanu Prakash Gollapudi struct bnx2fc_hba *hba = interface->hba; 587853e2bd2SBhanu Gollapudi dma_addr_t addr; 588853e2bd2SBhanu Gollapudi size_t sz; 589853e2bd2SBhanu Gollapudi 590853e2bd2SBhanu Gollapudi mp_req = (struct bnx2fc_mp_req *)&(io_req->mp_req); 591853e2bd2SBhanu Gollapudi memset(mp_req, 0, sizeof(struct bnx2fc_mp_req)); 592853e2bd2SBhanu Gollapudi 5931fffa199SChad Dupuis if (io_req->cmd_type != BNX2FC_ELS) { 594853e2bd2SBhanu Gollapudi mp_req->req_len = sizeof(struct fcp_cmnd); 595853e2bd2SBhanu Gollapudi io_req->data_xfer_len = mp_req->req_len; 5961fffa199SChad Dupuis } else 5971fffa199SChad Dupuis mp_req->req_len = io_req->data_xfer_len; 5981fffa199SChad Dupuis 599be1fefc2SMichael Chan mp_req->req_buf = dma_alloc_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE, 600853e2bd2SBhanu Gollapudi &mp_req->req_buf_dma, 601853e2bd2SBhanu Gollapudi GFP_ATOMIC); 602853e2bd2SBhanu Gollapudi if (!mp_req->req_buf) { 603853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "unable to alloc MP req buffer\n"); 604853e2bd2SBhanu Gollapudi bnx2fc_free_mp_resc(io_req); 605853e2bd2SBhanu Gollapudi return FAILED; 606853e2bd2SBhanu Gollapudi } 607853e2bd2SBhanu Gollapudi 608be1fefc2SMichael Chan mp_req->resp_buf = dma_alloc_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE, 609853e2bd2SBhanu Gollapudi &mp_req->resp_buf_dma, 610853e2bd2SBhanu Gollapudi GFP_ATOMIC); 611853e2bd2SBhanu Gollapudi if (!mp_req->resp_buf) { 612853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "unable to alloc TM resp buffer\n"); 613853e2bd2SBhanu Gollapudi bnx2fc_free_mp_resc(io_req); 614853e2bd2SBhanu Gollapudi return FAILED; 615853e2bd2SBhanu Gollapudi } 616be1fefc2SMichael Chan memset(mp_req->req_buf, 0, CNIC_PAGE_SIZE); 617be1fefc2SMichael Chan memset(mp_req->resp_buf, 0, CNIC_PAGE_SIZE); 618853e2bd2SBhanu Gollapudi 619853e2bd2SBhanu Gollapudi /* Allocate and map mp_req_bd and mp_resp_bd */ 620853e2bd2SBhanu Gollapudi sz = sizeof(struct fcoe_bd_ctx); 621853e2bd2SBhanu Gollapudi mp_req->mp_req_bd = dma_alloc_coherent(&hba->pcidev->dev, sz, 622853e2bd2SBhanu Gollapudi &mp_req->mp_req_bd_dma, 623853e2bd2SBhanu Gollapudi GFP_ATOMIC); 624853e2bd2SBhanu Gollapudi if (!mp_req->mp_req_bd) { 625853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "unable to alloc MP req bd\n"); 626853e2bd2SBhanu Gollapudi bnx2fc_free_mp_resc(io_req); 627853e2bd2SBhanu Gollapudi return FAILED; 628853e2bd2SBhanu Gollapudi } 629853e2bd2SBhanu Gollapudi mp_req->mp_resp_bd = dma_alloc_coherent(&hba->pcidev->dev, sz, 630853e2bd2SBhanu Gollapudi &mp_req->mp_resp_bd_dma, 631853e2bd2SBhanu Gollapudi GFP_ATOMIC); 632b0d5e15cSJulia Lawall if (!mp_req->mp_resp_bd) { 633853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "unable to alloc MP resp bd\n"); 634853e2bd2SBhanu Gollapudi bnx2fc_free_mp_resc(io_req); 635853e2bd2SBhanu Gollapudi return FAILED; 636853e2bd2SBhanu Gollapudi } 637853e2bd2SBhanu Gollapudi /* Fill bd table */ 638853e2bd2SBhanu Gollapudi addr = mp_req->req_buf_dma; 639853e2bd2SBhanu Gollapudi mp_req_bd = mp_req->mp_req_bd; 640853e2bd2SBhanu Gollapudi mp_req_bd->buf_addr_lo = (u32)addr & 0xffffffff; 641853e2bd2SBhanu Gollapudi mp_req_bd->buf_addr_hi = (u32)((u64)addr >> 32); 642be1fefc2SMichael Chan mp_req_bd->buf_len = CNIC_PAGE_SIZE; 643853e2bd2SBhanu Gollapudi mp_req_bd->flags = 0; 644853e2bd2SBhanu Gollapudi 645853e2bd2SBhanu Gollapudi /* 646853e2bd2SBhanu Gollapudi * MP buffer is either a task mgmt command or an ELS. 647853e2bd2SBhanu Gollapudi * So the assumption is that it consumes a single bd 648853e2bd2SBhanu Gollapudi * entry in the bd table 649853e2bd2SBhanu Gollapudi */ 650853e2bd2SBhanu Gollapudi mp_resp_bd = mp_req->mp_resp_bd; 651853e2bd2SBhanu Gollapudi addr = mp_req->resp_buf_dma; 652853e2bd2SBhanu Gollapudi mp_resp_bd->buf_addr_lo = (u32)addr & 0xffffffff; 653853e2bd2SBhanu Gollapudi mp_resp_bd->buf_addr_hi = (u32)((u64)addr >> 32); 654be1fefc2SMichael Chan mp_resp_bd->buf_len = CNIC_PAGE_SIZE; 655853e2bd2SBhanu Gollapudi mp_resp_bd->flags = 0; 656853e2bd2SBhanu Gollapudi 657853e2bd2SBhanu Gollapudi return SUCCESS; 658853e2bd2SBhanu Gollapudi } 659853e2bd2SBhanu Gollapudi 660853e2bd2SBhanu Gollapudi static int bnx2fc_initiate_tmf(struct scsi_cmnd *sc_cmd, u8 tm_flags) 661853e2bd2SBhanu Gollapudi { 662853e2bd2SBhanu Gollapudi struct fc_lport *lport; 66333c7da05SJulia Lawall struct fc_rport *rport; 66433c7da05SJulia Lawall struct fc_rport_libfc_priv *rp; 665853e2bd2SBhanu Gollapudi struct fcoe_port *port; 666aea71a02SBhanu Prakash Gollapudi struct bnx2fc_interface *interface; 667853e2bd2SBhanu Gollapudi struct bnx2fc_rport *tgt; 668853e2bd2SBhanu Gollapudi struct bnx2fc_cmd *io_req; 669853e2bd2SBhanu Gollapudi struct bnx2fc_mp_req *tm_req; 670853e2bd2SBhanu Gollapudi struct fcoe_task_ctx_entry *task; 671853e2bd2SBhanu Gollapudi struct fcoe_task_ctx_entry *task_page; 672853e2bd2SBhanu Gollapudi struct Scsi_Host *host = sc_cmd->device->host; 673853e2bd2SBhanu Gollapudi struct fc_frame_header *fc_hdr; 674853e2bd2SBhanu Gollapudi struct fcp_cmnd *fcp_cmnd; 675853e2bd2SBhanu Gollapudi int task_idx, index; 676853e2bd2SBhanu Gollapudi int rc = SUCCESS; 677853e2bd2SBhanu Gollapudi u16 xid; 678853e2bd2SBhanu Gollapudi u32 sid, did; 679853e2bd2SBhanu Gollapudi unsigned long start = jiffies; 680853e2bd2SBhanu Gollapudi 681853e2bd2SBhanu Gollapudi lport = shost_priv(host); 68233c7da05SJulia Lawall rport = starget_to_rport(scsi_target(sc_cmd->device)); 683853e2bd2SBhanu Gollapudi port = lport_priv(lport); 684aea71a02SBhanu Prakash Gollapudi interface = port->priv; 685853e2bd2SBhanu Gollapudi 686853e2bd2SBhanu Gollapudi if (rport == NULL) { 687b2a554ffSBhanu Prakash Gollapudi printk(KERN_ERR PFX "device_reset: rport is NULL\n"); 688853e2bd2SBhanu Gollapudi rc = FAILED; 689853e2bd2SBhanu Gollapudi goto tmf_err; 690853e2bd2SBhanu Gollapudi } 69133c7da05SJulia Lawall rp = rport->dd_data; 692853e2bd2SBhanu Gollapudi 693853e2bd2SBhanu Gollapudi rc = fc_block_scsi_eh(sc_cmd); 694853e2bd2SBhanu Gollapudi if (rc) 695853e2bd2SBhanu Gollapudi return rc; 696853e2bd2SBhanu Gollapudi 697853e2bd2SBhanu Gollapudi if (lport->state != LPORT_ST_READY || !(lport->link_up)) { 698853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "device_reset: link is not ready\n"); 699853e2bd2SBhanu Gollapudi rc = FAILED; 700853e2bd2SBhanu Gollapudi goto tmf_err; 701853e2bd2SBhanu Gollapudi } 702853e2bd2SBhanu Gollapudi /* rport and tgt are allocated together, so tgt should be non-NULL */ 703853e2bd2SBhanu Gollapudi tgt = (struct bnx2fc_rport *)&rp[1]; 704853e2bd2SBhanu Gollapudi 705853e2bd2SBhanu Gollapudi if (!(test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) { 706853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "device_reset: tgt not offloaded\n"); 707853e2bd2SBhanu Gollapudi rc = FAILED; 708853e2bd2SBhanu Gollapudi goto tmf_err; 709853e2bd2SBhanu Gollapudi } 710853e2bd2SBhanu Gollapudi retry_tmf: 711853e2bd2SBhanu Gollapudi io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_TASK_MGMT_CMD); 712853e2bd2SBhanu Gollapudi if (!io_req) { 713853e2bd2SBhanu Gollapudi if (time_after(jiffies, start + HZ)) { 714853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "tmf: Failed TMF"); 715853e2bd2SBhanu Gollapudi rc = FAILED; 716853e2bd2SBhanu Gollapudi goto tmf_err; 717853e2bd2SBhanu Gollapudi } 718853e2bd2SBhanu Gollapudi msleep(20); 719853e2bd2SBhanu Gollapudi goto retry_tmf; 720853e2bd2SBhanu Gollapudi } 721853e2bd2SBhanu Gollapudi /* Initialize rest of io_req fields */ 722853e2bd2SBhanu Gollapudi io_req->sc_cmd = sc_cmd; 723853e2bd2SBhanu Gollapudi io_req->port = port; 724853e2bd2SBhanu Gollapudi io_req->tgt = tgt; 725853e2bd2SBhanu Gollapudi 726853e2bd2SBhanu Gollapudi tm_req = (struct bnx2fc_mp_req *)&(io_req->mp_req); 727853e2bd2SBhanu Gollapudi 728853e2bd2SBhanu Gollapudi rc = bnx2fc_init_mp_req(io_req); 729853e2bd2SBhanu Gollapudi if (rc == FAILED) { 730853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "Task mgmt MP request init failed\n"); 731bd4d5de8SBhanu Prakash Gollapudi spin_lock_bh(&tgt->tgt_lock); 732853e2bd2SBhanu Gollapudi kref_put(&io_req->refcount, bnx2fc_cmd_release); 733bd4d5de8SBhanu Prakash Gollapudi spin_unlock_bh(&tgt->tgt_lock); 734853e2bd2SBhanu Gollapudi goto tmf_err; 735853e2bd2SBhanu Gollapudi } 736853e2bd2SBhanu Gollapudi 737853e2bd2SBhanu Gollapudi /* Set TM flags */ 738853e2bd2SBhanu Gollapudi io_req->io_req_flags = 0; 739853e2bd2SBhanu Gollapudi tm_req->tm_flags = tm_flags; 740853e2bd2SBhanu Gollapudi 741853e2bd2SBhanu Gollapudi /* Fill FCP_CMND */ 742853e2bd2SBhanu Gollapudi bnx2fc_build_fcp_cmnd(io_req, (struct fcp_cmnd *)tm_req->req_buf); 743853e2bd2SBhanu Gollapudi fcp_cmnd = (struct fcp_cmnd *)tm_req->req_buf; 744853e2bd2SBhanu Gollapudi memset(fcp_cmnd->fc_cdb, 0, sc_cmd->cmd_len); 745853e2bd2SBhanu Gollapudi fcp_cmnd->fc_dl = 0; 746853e2bd2SBhanu Gollapudi 747853e2bd2SBhanu Gollapudi /* Fill FC header */ 748853e2bd2SBhanu Gollapudi fc_hdr = &(tm_req->req_fc_hdr); 749853e2bd2SBhanu Gollapudi sid = tgt->sid; 750853e2bd2SBhanu Gollapudi did = rport->port_id; 751853e2bd2SBhanu Gollapudi __fc_fill_fc_hdr(fc_hdr, FC_RCTL_DD_UNSOL_CMD, did, sid, 752853e2bd2SBhanu Gollapudi FC_TYPE_FCP, FC_FC_FIRST_SEQ | FC_FC_END_SEQ | 753853e2bd2SBhanu Gollapudi FC_FC_SEQ_INIT, 0); 754853e2bd2SBhanu Gollapudi /* Obtain exchange id */ 755853e2bd2SBhanu Gollapudi xid = io_req->xid; 756853e2bd2SBhanu Gollapudi 757853e2bd2SBhanu Gollapudi BNX2FC_TGT_DBG(tgt, "Initiate TMF - xid = 0x%x\n", xid); 758853e2bd2SBhanu Gollapudi task_idx = xid/BNX2FC_TASKS_PER_PAGE; 759853e2bd2SBhanu Gollapudi index = xid % BNX2FC_TASKS_PER_PAGE; 760853e2bd2SBhanu Gollapudi 761853e2bd2SBhanu Gollapudi /* Initialize task context for this IO request */ 762aea71a02SBhanu Prakash Gollapudi task_page = (struct fcoe_task_ctx_entry *) 763aea71a02SBhanu Prakash Gollapudi interface->hba->task_ctx[task_idx]; 764853e2bd2SBhanu Gollapudi task = &(task_page[index]); 765853e2bd2SBhanu Gollapudi bnx2fc_init_mp_task(io_req, task); 766853e2bd2SBhanu Gollapudi 767853e2bd2SBhanu Gollapudi sc_cmd->SCp.ptr = (char *)io_req; 768853e2bd2SBhanu Gollapudi 769853e2bd2SBhanu Gollapudi /* Obtain free SQ entry */ 770853e2bd2SBhanu Gollapudi spin_lock_bh(&tgt->tgt_lock); 771853e2bd2SBhanu Gollapudi bnx2fc_add_2_sq(tgt, xid); 772853e2bd2SBhanu Gollapudi 773853e2bd2SBhanu Gollapudi /* Enqueue the io_req to active_tm_queue */ 774853e2bd2SBhanu Gollapudi io_req->on_tmf_queue = 1; 775853e2bd2SBhanu Gollapudi list_add_tail(&io_req->link, &tgt->active_tm_queue); 776853e2bd2SBhanu Gollapudi 777853e2bd2SBhanu Gollapudi init_completion(&io_req->tm_done); 778853e2bd2SBhanu Gollapudi io_req->wait_for_comp = 1; 779853e2bd2SBhanu Gollapudi 780853e2bd2SBhanu Gollapudi /* Ring doorbell */ 781853e2bd2SBhanu Gollapudi bnx2fc_ring_doorbell(tgt); 782853e2bd2SBhanu Gollapudi spin_unlock_bh(&tgt->tgt_lock); 783853e2bd2SBhanu Gollapudi 784853e2bd2SBhanu Gollapudi rc = wait_for_completion_timeout(&io_req->tm_done, 78510755d3fSJoe Carnuccio interface->tm_timeout * HZ); 786853e2bd2SBhanu Gollapudi spin_lock_bh(&tgt->tgt_lock); 787853e2bd2SBhanu Gollapudi 788853e2bd2SBhanu Gollapudi io_req->wait_for_comp = 0; 78992886c9cSBhanu Prakash Gollapudi if (!(test_bit(BNX2FC_FLAG_TM_COMPL, &io_req->req_flags))) { 790853e2bd2SBhanu Gollapudi set_bit(BNX2FC_FLAG_TM_TIMEOUT, &io_req->req_flags); 79192886c9cSBhanu Prakash Gollapudi if (io_req->on_tmf_queue) { 79292886c9cSBhanu Prakash Gollapudi list_del_init(&io_req->link); 79392886c9cSBhanu Prakash Gollapudi io_req->on_tmf_queue = 0; 79492886c9cSBhanu Prakash Gollapudi } 79592886c9cSBhanu Prakash Gollapudi io_req->wait_for_comp = 1; 79692886c9cSBhanu Prakash Gollapudi bnx2fc_initiate_cleanup(io_req); 79792886c9cSBhanu Prakash Gollapudi spin_unlock_bh(&tgt->tgt_lock); 79892886c9cSBhanu Prakash Gollapudi rc = wait_for_completion_timeout(&io_req->tm_done, 79992886c9cSBhanu Prakash Gollapudi BNX2FC_FW_TIMEOUT); 80092886c9cSBhanu Prakash Gollapudi spin_lock_bh(&tgt->tgt_lock); 80192886c9cSBhanu Prakash Gollapudi io_req->wait_for_comp = 0; 80292886c9cSBhanu Prakash Gollapudi if (!rc) 80392886c9cSBhanu Prakash Gollapudi kref_put(&io_req->refcount, bnx2fc_cmd_release); 80492886c9cSBhanu Prakash Gollapudi } 805853e2bd2SBhanu Gollapudi 806853e2bd2SBhanu Gollapudi spin_unlock_bh(&tgt->tgt_lock); 807853e2bd2SBhanu Gollapudi 808853e2bd2SBhanu Gollapudi if (!rc) { 809b2a554ffSBhanu Prakash Gollapudi BNX2FC_TGT_DBG(tgt, "task mgmt command failed...\n"); 810853e2bd2SBhanu Gollapudi rc = FAILED; 811853e2bd2SBhanu Gollapudi } else { 812b2a554ffSBhanu Prakash Gollapudi BNX2FC_TGT_DBG(tgt, "task mgmt command success...\n"); 813853e2bd2SBhanu Gollapudi rc = SUCCESS; 814853e2bd2SBhanu Gollapudi } 815853e2bd2SBhanu Gollapudi tmf_err: 816853e2bd2SBhanu Gollapudi return rc; 817853e2bd2SBhanu Gollapudi } 818853e2bd2SBhanu Gollapudi 819853e2bd2SBhanu Gollapudi int bnx2fc_initiate_abts(struct bnx2fc_cmd *io_req) 820853e2bd2SBhanu Gollapudi { 821853e2bd2SBhanu Gollapudi struct fc_lport *lport; 822853e2bd2SBhanu Gollapudi struct bnx2fc_rport *tgt = io_req->tgt; 823853e2bd2SBhanu Gollapudi struct fc_rport *rport = tgt->rport; 824853e2bd2SBhanu Gollapudi struct fc_rport_priv *rdata = tgt->rdata; 825aea71a02SBhanu Prakash Gollapudi struct bnx2fc_interface *interface; 826853e2bd2SBhanu Gollapudi struct fcoe_port *port; 827853e2bd2SBhanu Gollapudi struct bnx2fc_cmd *abts_io_req; 828853e2bd2SBhanu Gollapudi struct fcoe_task_ctx_entry *task; 829853e2bd2SBhanu Gollapudi struct fcoe_task_ctx_entry *task_page; 830853e2bd2SBhanu Gollapudi struct fc_frame_header *fc_hdr; 831853e2bd2SBhanu Gollapudi struct bnx2fc_mp_req *abts_req; 832853e2bd2SBhanu Gollapudi int task_idx, index; 833853e2bd2SBhanu Gollapudi u32 sid, did; 834853e2bd2SBhanu Gollapudi u16 xid; 835853e2bd2SBhanu Gollapudi int rc = SUCCESS; 836853e2bd2SBhanu Gollapudi u32 r_a_tov = rdata->r_a_tov; 837853e2bd2SBhanu Gollapudi 838853e2bd2SBhanu Gollapudi /* called with tgt_lock held */ 839853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_abts\n"); 840853e2bd2SBhanu Gollapudi 841853e2bd2SBhanu Gollapudi port = io_req->port; 842aea71a02SBhanu Prakash Gollapudi interface = port->priv; 843853e2bd2SBhanu Gollapudi lport = port->lport; 844853e2bd2SBhanu Gollapudi 845853e2bd2SBhanu Gollapudi if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) { 846853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "initiate_abts: tgt not offloaded\n"); 847853e2bd2SBhanu Gollapudi rc = FAILED; 848853e2bd2SBhanu Gollapudi goto abts_err; 849853e2bd2SBhanu Gollapudi } 850853e2bd2SBhanu Gollapudi 851853e2bd2SBhanu Gollapudi if (rport == NULL) { 852b2a554ffSBhanu Prakash Gollapudi printk(KERN_ERR PFX "initiate_abts: rport is NULL\n"); 853853e2bd2SBhanu Gollapudi rc = FAILED; 854853e2bd2SBhanu Gollapudi goto abts_err; 855853e2bd2SBhanu Gollapudi } 856853e2bd2SBhanu Gollapudi 857853e2bd2SBhanu Gollapudi if (lport->state != LPORT_ST_READY || !(lport->link_up)) { 858853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "initiate_abts: link is not ready\n"); 859853e2bd2SBhanu Gollapudi rc = FAILED; 860853e2bd2SBhanu Gollapudi goto abts_err; 861853e2bd2SBhanu Gollapudi } 862853e2bd2SBhanu Gollapudi 863853e2bd2SBhanu Gollapudi abts_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_ABTS); 864853e2bd2SBhanu Gollapudi if (!abts_io_req) { 865853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "abts: couldnt allocate cmd\n"); 866853e2bd2SBhanu Gollapudi rc = FAILED; 867853e2bd2SBhanu Gollapudi goto abts_err; 868853e2bd2SBhanu Gollapudi } 869853e2bd2SBhanu Gollapudi 870853e2bd2SBhanu Gollapudi /* Initialize rest of io_req fields */ 871853e2bd2SBhanu Gollapudi abts_io_req->sc_cmd = NULL; 872853e2bd2SBhanu Gollapudi abts_io_req->port = port; 873853e2bd2SBhanu Gollapudi abts_io_req->tgt = tgt; 874853e2bd2SBhanu Gollapudi abts_io_req->data_xfer_len = 0; /* No data transfer for ABTS */ 875853e2bd2SBhanu Gollapudi 876853e2bd2SBhanu Gollapudi abts_req = (struct bnx2fc_mp_req *)&(abts_io_req->mp_req); 877853e2bd2SBhanu Gollapudi memset(abts_req, 0, sizeof(struct bnx2fc_mp_req)); 878853e2bd2SBhanu Gollapudi 879853e2bd2SBhanu Gollapudi /* Fill FC header */ 880853e2bd2SBhanu Gollapudi fc_hdr = &(abts_req->req_fc_hdr); 881853e2bd2SBhanu Gollapudi 882853e2bd2SBhanu Gollapudi /* Obtain oxid and rxid for the original exchange to be aborted */ 883853e2bd2SBhanu Gollapudi fc_hdr->fh_ox_id = htons(io_req->xid); 884619c5cb6SVlad Zolotarov fc_hdr->fh_rx_id = htons(io_req->task->rxwr_txrd.var_ctx.rx_id); 885853e2bd2SBhanu Gollapudi 886853e2bd2SBhanu Gollapudi sid = tgt->sid; 887853e2bd2SBhanu Gollapudi did = rport->port_id; 888853e2bd2SBhanu Gollapudi 889853e2bd2SBhanu Gollapudi __fc_fill_fc_hdr(fc_hdr, FC_RCTL_BA_ABTS, did, sid, 890853e2bd2SBhanu Gollapudi FC_TYPE_BLS, FC_FC_FIRST_SEQ | FC_FC_END_SEQ | 891853e2bd2SBhanu Gollapudi FC_FC_SEQ_INIT, 0); 892853e2bd2SBhanu Gollapudi 893853e2bd2SBhanu Gollapudi xid = abts_io_req->xid; 894853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(abts_io_req, "ABTS io_req\n"); 895853e2bd2SBhanu Gollapudi task_idx = xid/BNX2FC_TASKS_PER_PAGE; 896853e2bd2SBhanu Gollapudi index = xid % BNX2FC_TASKS_PER_PAGE; 897853e2bd2SBhanu Gollapudi 898853e2bd2SBhanu Gollapudi /* Initialize task context for this IO request */ 899aea71a02SBhanu Prakash Gollapudi task_page = (struct fcoe_task_ctx_entry *) 900aea71a02SBhanu Prakash Gollapudi interface->hba->task_ctx[task_idx]; 901853e2bd2SBhanu Gollapudi task = &(task_page[index]); 902853e2bd2SBhanu Gollapudi bnx2fc_init_mp_task(abts_io_req, task); 903853e2bd2SBhanu Gollapudi 904853e2bd2SBhanu Gollapudi /* 905853e2bd2SBhanu Gollapudi * ABTS task is a temporary task that will be cleaned up 906853e2bd2SBhanu Gollapudi * irrespective of ABTS response. We need to start the timer 907853e2bd2SBhanu Gollapudi * for the original exchange, as the CQE is posted for the original 908853e2bd2SBhanu Gollapudi * IO request. 909853e2bd2SBhanu Gollapudi * 910853e2bd2SBhanu Gollapudi * Timer for ABTS is started only when it is originated by a 911853e2bd2SBhanu Gollapudi * TM request. For the ABTS issued as part of ULP timeout, 912853e2bd2SBhanu Gollapudi * scsi-ml maintains the timers. 913853e2bd2SBhanu Gollapudi */ 914853e2bd2SBhanu Gollapudi 915853e2bd2SBhanu Gollapudi /* if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags))*/ 916853e2bd2SBhanu Gollapudi bnx2fc_cmd_timer_set(io_req, 2 * r_a_tov); 917853e2bd2SBhanu Gollapudi 918853e2bd2SBhanu Gollapudi /* Obtain free SQ entry */ 919853e2bd2SBhanu Gollapudi bnx2fc_add_2_sq(tgt, xid); 920853e2bd2SBhanu Gollapudi 921853e2bd2SBhanu Gollapudi /* Ring doorbell */ 922853e2bd2SBhanu Gollapudi bnx2fc_ring_doorbell(tgt); 923853e2bd2SBhanu Gollapudi 924853e2bd2SBhanu Gollapudi abts_err: 925853e2bd2SBhanu Gollapudi return rc; 926853e2bd2SBhanu Gollapudi } 927853e2bd2SBhanu Gollapudi 9286c5a7ce4SBhanu Prakash Gollapudi int bnx2fc_initiate_seq_cleanup(struct bnx2fc_cmd *orig_io_req, u32 offset, 9296c5a7ce4SBhanu Prakash Gollapudi enum fc_rctl r_ctl) 9306c5a7ce4SBhanu Prakash Gollapudi { 9316c5a7ce4SBhanu Prakash Gollapudi struct fc_lport *lport; 9326c5a7ce4SBhanu Prakash Gollapudi struct bnx2fc_rport *tgt = orig_io_req->tgt; 9336c5a7ce4SBhanu Prakash Gollapudi struct bnx2fc_interface *interface; 9346c5a7ce4SBhanu Prakash Gollapudi struct fcoe_port *port; 9356c5a7ce4SBhanu Prakash Gollapudi struct bnx2fc_cmd *seq_clnp_req; 9366c5a7ce4SBhanu Prakash Gollapudi struct fcoe_task_ctx_entry *task; 9376c5a7ce4SBhanu Prakash Gollapudi struct fcoe_task_ctx_entry *task_page; 9386c5a7ce4SBhanu Prakash Gollapudi struct bnx2fc_els_cb_arg *cb_arg = NULL; 9396c5a7ce4SBhanu Prakash Gollapudi int task_idx, index; 9406c5a7ce4SBhanu Prakash Gollapudi u16 xid; 9416c5a7ce4SBhanu Prakash Gollapudi int rc = 0; 9426c5a7ce4SBhanu Prakash Gollapudi 9436c5a7ce4SBhanu Prakash Gollapudi BNX2FC_IO_DBG(orig_io_req, "bnx2fc_initiate_seq_cleanup xid = 0x%x\n", 9446c5a7ce4SBhanu Prakash Gollapudi orig_io_req->xid); 9456c5a7ce4SBhanu Prakash Gollapudi kref_get(&orig_io_req->refcount); 9466c5a7ce4SBhanu Prakash Gollapudi 9476c5a7ce4SBhanu Prakash Gollapudi port = orig_io_req->port; 9486c5a7ce4SBhanu Prakash Gollapudi interface = port->priv; 9496c5a7ce4SBhanu Prakash Gollapudi lport = port->lport; 9506c5a7ce4SBhanu Prakash Gollapudi 9516c5a7ce4SBhanu Prakash Gollapudi cb_arg = kzalloc(sizeof(struct bnx2fc_els_cb_arg), GFP_ATOMIC); 9526c5a7ce4SBhanu Prakash Gollapudi if (!cb_arg) { 9536c5a7ce4SBhanu Prakash Gollapudi printk(KERN_ERR PFX "Unable to alloc cb_arg for seq clnup\n"); 9546c5a7ce4SBhanu Prakash Gollapudi rc = -ENOMEM; 9556c5a7ce4SBhanu Prakash Gollapudi goto cleanup_err; 9566c5a7ce4SBhanu Prakash Gollapudi } 9576c5a7ce4SBhanu Prakash Gollapudi 9586c5a7ce4SBhanu Prakash Gollapudi seq_clnp_req = bnx2fc_elstm_alloc(tgt, BNX2FC_SEQ_CLEANUP); 9596c5a7ce4SBhanu Prakash Gollapudi if (!seq_clnp_req) { 9606c5a7ce4SBhanu Prakash Gollapudi printk(KERN_ERR PFX "cleanup: couldnt allocate cmd\n"); 9616c5a7ce4SBhanu Prakash Gollapudi rc = -ENOMEM; 9626c5a7ce4SBhanu Prakash Gollapudi kfree(cb_arg); 9636c5a7ce4SBhanu Prakash Gollapudi goto cleanup_err; 9646c5a7ce4SBhanu Prakash Gollapudi } 9656c5a7ce4SBhanu Prakash Gollapudi /* Initialize rest of io_req fields */ 9666c5a7ce4SBhanu Prakash Gollapudi seq_clnp_req->sc_cmd = NULL; 9676c5a7ce4SBhanu Prakash Gollapudi seq_clnp_req->port = port; 9686c5a7ce4SBhanu Prakash Gollapudi seq_clnp_req->tgt = tgt; 9696c5a7ce4SBhanu Prakash Gollapudi seq_clnp_req->data_xfer_len = 0; /* No data transfer for cleanup */ 9706c5a7ce4SBhanu Prakash Gollapudi 9716c5a7ce4SBhanu Prakash Gollapudi xid = seq_clnp_req->xid; 9726c5a7ce4SBhanu Prakash Gollapudi 9736c5a7ce4SBhanu Prakash Gollapudi task_idx = xid/BNX2FC_TASKS_PER_PAGE; 9746c5a7ce4SBhanu Prakash Gollapudi index = xid % BNX2FC_TASKS_PER_PAGE; 9756c5a7ce4SBhanu Prakash Gollapudi 9766c5a7ce4SBhanu Prakash Gollapudi /* Initialize task context for this IO request */ 9776c5a7ce4SBhanu Prakash Gollapudi task_page = (struct fcoe_task_ctx_entry *) 9786c5a7ce4SBhanu Prakash Gollapudi interface->hba->task_ctx[task_idx]; 9796c5a7ce4SBhanu Prakash Gollapudi task = &(task_page[index]); 9806c5a7ce4SBhanu Prakash Gollapudi cb_arg->aborted_io_req = orig_io_req; 9816c5a7ce4SBhanu Prakash Gollapudi cb_arg->io_req = seq_clnp_req; 9826c5a7ce4SBhanu Prakash Gollapudi cb_arg->r_ctl = r_ctl; 9836c5a7ce4SBhanu Prakash Gollapudi cb_arg->offset = offset; 9846c5a7ce4SBhanu Prakash Gollapudi seq_clnp_req->cb_arg = cb_arg; 9856c5a7ce4SBhanu Prakash Gollapudi 9866c5a7ce4SBhanu Prakash Gollapudi printk(KERN_ERR PFX "call init_seq_cleanup_task\n"); 9876c5a7ce4SBhanu Prakash Gollapudi bnx2fc_init_seq_cleanup_task(seq_clnp_req, task, orig_io_req, offset); 9886c5a7ce4SBhanu Prakash Gollapudi 9896c5a7ce4SBhanu Prakash Gollapudi /* Obtain free SQ entry */ 9906c5a7ce4SBhanu Prakash Gollapudi bnx2fc_add_2_sq(tgt, xid); 9916c5a7ce4SBhanu Prakash Gollapudi 9926c5a7ce4SBhanu Prakash Gollapudi /* Ring doorbell */ 9936c5a7ce4SBhanu Prakash Gollapudi bnx2fc_ring_doorbell(tgt); 9946c5a7ce4SBhanu Prakash Gollapudi cleanup_err: 9956c5a7ce4SBhanu Prakash Gollapudi return rc; 9966c5a7ce4SBhanu Prakash Gollapudi } 9976c5a7ce4SBhanu Prakash Gollapudi 998853e2bd2SBhanu Gollapudi int bnx2fc_initiate_cleanup(struct bnx2fc_cmd *io_req) 999853e2bd2SBhanu Gollapudi { 1000853e2bd2SBhanu Gollapudi struct fc_lport *lport; 1001853e2bd2SBhanu Gollapudi struct bnx2fc_rport *tgt = io_req->tgt; 1002aea71a02SBhanu Prakash Gollapudi struct bnx2fc_interface *interface; 1003853e2bd2SBhanu Gollapudi struct fcoe_port *port; 1004853e2bd2SBhanu Gollapudi struct bnx2fc_cmd *cleanup_io_req; 1005853e2bd2SBhanu Gollapudi struct fcoe_task_ctx_entry *task; 1006853e2bd2SBhanu Gollapudi struct fcoe_task_ctx_entry *task_page; 1007853e2bd2SBhanu Gollapudi int task_idx, index; 1008853e2bd2SBhanu Gollapudi u16 xid, orig_xid; 1009853e2bd2SBhanu Gollapudi int rc = 0; 1010853e2bd2SBhanu Gollapudi 1011853e2bd2SBhanu Gollapudi /* ASSUMPTION: called with tgt_lock held */ 1012853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_cleanup\n"); 1013853e2bd2SBhanu Gollapudi 1014853e2bd2SBhanu Gollapudi port = io_req->port; 1015aea71a02SBhanu Prakash Gollapudi interface = port->priv; 1016853e2bd2SBhanu Gollapudi lport = port->lport; 1017853e2bd2SBhanu Gollapudi 1018853e2bd2SBhanu Gollapudi cleanup_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_CLEANUP); 1019853e2bd2SBhanu Gollapudi if (!cleanup_io_req) { 1020853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "cleanup: couldnt allocate cmd\n"); 1021853e2bd2SBhanu Gollapudi rc = -1; 1022853e2bd2SBhanu Gollapudi goto cleanup_err; 1023853e2bd2SBhanu Gollapudi } 1024853e2bd2SBhanu Gollapudi 1025853e2bd2SBhanu Gollapudi /* Initialize rest of io_req fields */ 1026853e2bd2SBhanu Gollapudi cleanup_io_req->sc_cmd = NULL; 1027853e2bd2SBhanu Gollapudi cleanup_io_req->port = port; 1028853e2bd2SBhanu Gollapudi cleanup_io_req->tgt = tgt; 1029853e2bd2SBhanu Gollapudi cleanup_io_req->data_xfer_len = 0; /* No data transfer for cleanup */ 1030853e2bd2SBhanu Gollapudi 1031853e2bd2SBhanu Gollapudi xid = cleanup_io_req->xid; 1032853e2bd2SBhanu Gollapudi 1033853e2bd2SBhanu Gollapudi task_idx = xid/BNX2FC_TASKS_PER_PAGE; 1034853e2bd2SBhanu Gollapudi index = xid % BNX2FC_TASKS_PER_PAGE; 1035853e2bd2SBhanu Gollapudi 1036853e2bd2SBhanu Gollapudi /* Initialize task context for this IO request */ 1037aea71a02SBhanu Prakash Gollapudi task_page = (struct fcoe_task_ctx_entry *) 1038aea71a02SBhanu Prakash Gollapudi interface->hba->task_ctx[task_idx]; 1039853e2bd2SBhanu Gollapudi task = &(task_page[index]); 1040853e2bd2SBhanu Gollapudi orig_xid = io_req->xid; 1041853e2bd2SBhanu Gollapudi 1042853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "CLEANUP io_req xid = 0x%x\n", xid); 1043853e2bd2SBhanu Gollapudi 1044853e2bd2SBhanu Gollapudi bnx2fc_init_cleanup_task(cleanup_io_req, task, orig_xid); 1045853e2bd2SBhanu Gollapudi 1046853e2bd2SBhanu Gollapudi /* Obtain free SQ entry */ 1047853e2bd2SBhanu Gollapudi bnx2fc_add_2_sq(tgt, xid); 1048853e2bd2SBhanu Gollapudi 1049853e2bd2SBhanu Gollapudi /* Ring doorbell */ 1050853e2bd2SBhanu Gollapudi bnx2fc_ring_doorbell(tgt); 1051853e2bd2SBhanu Gollapudi 1052853e2bd2SBhanu Gollapudi cleanup_err: 1053853e2bd2SBhanu Gollapudi return rc; 1054853e2bd2SBhanu Gollapudi } 1055853e2bd2SBhanu Gollapudi 1056853e2bd2SBhanu Gollapudi /** 1057853e2bd2SBhanu Gollapudi * bnx2fc_eh_target_reset: Reset a target 1058853e2bd2SBhanu Gollapudi * 1059853e2bd2SBhanu Gollapudi * @sc_cmd: SCSI command 1060853e2bd2SBhanu Gollapudi * 1061853e2bd2SBhanu Gollapudi * Set from SCSI host template to send task mgmt command to the target 1062853e2bd2SBhanu Gollapudi * and wait for the response 1063853e2bd2SBhanu Gollapudi */ 1064853e2bd2SBhanu Gollapudi int bnx2fc_eh_target_reset(struct scsi_cmnd *sc_cmd) 1065853e2bd2SBhanu Gollapudi { 1066853e2bd2SBhanu Gollapudi return bnx2fc_initiate_tmf(sc_cmd, FCP_TMF_TGT_RESET); 1067853e2bd2SBhanu Gollapudi } 1068853e2bd2SBhanu Gollapudi 1069853e2bd2SBhanu Gollapudi /** 1070853e2bd2SBhanu Gollapudi * bnx2fc_eh_device_reset - Reset a single LUN 1071853e2bd2SBhanu Gollapudi * 1072853e2bd2SBhanu Gollapudi * @sc_cmd: SCSI command 1073853e2bd2SBhanu Gollapudi * 1074853e2bd2SBhanu Gollapudi * Set from SCSI host template to send task mgmt command to the target 1075853e2bd2SBhanu Gollapudi * and wait for the response 1076853e2bd2SBhanu Gollapudi */ 1077853e2bd2SBhanu Gollapudi int bnx2fc_eh_device_reset(struct scsi_cmnd *sc_cmd) 1078853e2bd2SBhanu Gollapudi { 1079853e2bd2SBhanu Gollapudi return bnx2fc_initiate_tmf(sc_cmd, FCP_TMF_LUN_RESET); 1080853e2bd2SBhanu Gollapudi } 1081853e2bd2SBhanu Gollapudi 108250a87414SChad Dupuis int bnx2fc_abts_cleanup(struct bnx2fc_cmd *io_req) 1083c1bb4f33SBhanu Prakash Gollapudi { 1084c1bb4f33SBhanu Prakash Gollapudi struct bnx2fc_rport *tgt = io_req->tgt; 1085c1bb4f33SBhanu Prakash Gollapudi int rc = SUCCESS; 1086c1bb4f33SBhanu Prakash Gollapudi 1087c1bb4f33SBhanu Prakash Gollapudi io_req->wait_for_comp = 1; 1088c1bb4f33SBhanu Prakash Gollapudi bnx2fc_initiate_cleanup(io_req); 1089c1bb4f33SBhanu Prakash Gollapudi 1090c1bb4f33SBhanu Prakash Gollapudi spin_unlock_bh(&tgt->tgt_lock); 1091c1bb4f33SBhanu Prakash Gollapudi 1092c1bb4f33SBhanu Prakash Gollapudi wait_for_completion(&io_req->tm_done); 1093c1bb4f33SBhanu Prakash Gollapudi 1094c1bb4f33SBhanu Prakash Gollapudi io_req->wait_for_comp = 0; 1095c1bb4f33SBhanu Prakash Gollapudi /* 1096c1bb4f33SBhanu Prakash Gollapudi * release the reference taken in eh_abort to allow the 1097c1bb4f33SBhanu Prakash Gollapudi * target to re-login after flushing IOs 1098c1bb4f33SBhanu Prakash Gollapudi */ 1099c1bb4f33SBhanu Prakash Gollapudi kref_put(&io_req->refcount, bnx2fc_cmd_release); 1100c1bb4f33SBhanu Prakash Gollapudi 1101c1bb4f33SBhanu Prakash Gollapudi spin_lock_bh(&tgt->tgt_lock); 1102c1bb4f33SBhanu Prakash Gollapudi return rc; 1103c1bb4f33SBhanu Prakash Gollapudi } 1104853e2bd2SBhanu Gollapudi /** 1105853e2bd2SBhanu Gollapudi * bnx2fc_eh_abort - eh_abort_handler api to abort an outstanding 1106853e2bd2SBhanu Gollapudi * SCSI command 1107853e2bd2SBhanu Gollapudi * 1108853e2bd2SBhanu Gollapudi * @sc_cmd: SCSI_ML command pointer 1109853e2bd2SBhanu Gollapudi * 1110853e2bd2SBhanu Gollapudi * SCSI abort request handler 1111853e2bd2SBhanu Gollapudi */ 1112853e2bd2SBhanu Gollapudi int bnx2fc_eh_abort(struct scsi_cmnd *sc_cmd) 1113853e2bd2SBhanu Gollapudi { 1114853e2bd2SBhanu Gollapudi struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device)); 1115853e2bd2SBhanu Gollapudi struct fc_rport_libfc_priv *rp = rport->dd_data; 1116853e2bd2SBhanu Gollapudi struct bnx2fc_cmd *io_req; 1117853e2bd2SBhanu Gollapudi struct fc_lport *lport; 1118853e2bd2SBhanu Gollapudi struct bnx2fc_rport *tgt; 1119617757deSMaurizio Lombardi int rc; 1120853e2bd2SBhanu Gollapudi 1121853e2bd2SBhanu Gollapudi rc = fc_block_scsi_eh(sc_cmd); 1122853e2bd2SBhanu Gollapudi if (rc) 1123853e2bd2SBhanu Gollapudi return rc; 1124853e2bd2SBhanu Gollapudi 1125853e2bd2SBhanu Gollapudi lport = shost_priv(sc_cmd->device->host); 1126853e2bd2SBhanu Gollapudi if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) { 1127b2a554ffSBhanu Prakash Gollapudi printk(KERN_ERR PFX "eh_abort: link not ready\n"); 1128617757deSMaurizio Lombardi return FAILED; 1129853e2bd2SBhanu Gollapudi } 1130853e2bd2SBhanu Gollapudi 1131853e2bd2SBhanu Gollapudi tgt = (struct bnx2fc_rport *)&rp[1]; 1132853e2bd2SBhanu Gollapudi 1133853e2bd2SBhanu Gollapudi BNX2FC_TGT_DBG(tgt, "Entered bnx2fc_eh_abort\n"); 1134853e2bd2SBhanu Gollapudi 1135853e2bd2SBhanu Gollapudi spin_lock_bh(&tgt->tgt_lock); 1136853e2bd2SBhanu Gollapudi io_req = (struct bnx2fc_cmd *)sc_cmd->SCp.ptr; 1137853e2bd2SBhanu Gollapudi if (!io_req) { 1138853e2bd2SBhanu Gollapudi /* Command might have just completed */ 1139853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "eh_abort: io_req is NULL\n"); 1140853e2bd2SBhanu Gollapudi spin_unlock_bh(&tgt->tgt_lock); 1141853e2bd2SBhanu Gollapudi return SUCCESS; 1142853e2bd2SBhanu Gollapudi } 1143853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "eh_abort - refcnt = %d\n", 1144853e2bd2SBhanu Gollapudi io_req->refcount.refcount.counter); 1145853e2bd2SBhanu Gollapudi 1146853e2bd2SBhanu Gollapudi /* Hold IO request across abort processing */ 1147853e2bd2SBhanu Gollapudi kref_get(&io_req->refcount); 1148853e2bd2SBhanu Gollapudi 1149853e2bd2SBhanu Gollapudi BUG_ON(tgt != io_req->tgt); 1150853e2bd2SBhanu Gollapudi 1151853e2bd2SBhanu Gollapudi /* Remove the io_req from the active_q. */ 1152853e2bd2SBhanu Gollapudi /* 1153853e2bd2SBhanu Gollapudi * Task Mgmt functions (LUN RESET & TGT RESET) will not 1154853e2bd2SBhanu Gollapudi * issue an ABTS on this particular IO req, as the 1155853e2bd2SBhanu Gollapudi * io_req is no longer in the active_q. 1156853e2bd2SBhanu Gollapudi */ 1157853e2bd2SBhanu Gollapudi if (tgt->flush_in_prog) { 1158b2a554ffSBhanu Prakash Gollapudi printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) " 1159853e2bd2SBhanu Gollapudi "flush in progress\n", io_req->xid); 1160853e2bd2SBhanu Gollapudi kref_put(&io_req->refcount, bnx2fc_cmd_release); 1161853e2bd2SBhanu Gollapudi spin_unlock_bh(&tgt->tgt_lock); 1162853e2bd2SBhanu Gollapudi return SUCCESS; 1163853e2bd2SBhanu Gollapudi } 1164853e2bd2SBhanu Gollapudi 1165853e2bd2SBhanu Gollapudi if (io_req->on_active_queue == 0) { 1166b2a554ffSBhanu Prakash Gollapudi printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) " 1167853e2bd2SBhanu Gollapudi "not on active_q\n", io_req->xid); 1168853e2bd2SBhanu Gollapudi /* 1169853e2bd2SBhanu Gollapudi * This condition can happen only due to the FW bug, 1170853e2bd2SBhanu Gollapudi * where we do not receive cleanup response from 1171853e2bd2SBhanu Gollapudi * the FW. Handle this case gracefully by erroring 1172853e2bd2SBhanu Gollapudi * back the IO request to SCSI-ml 1173853e2bd2SBhanu Gollapudi */ 1174853e2bd2SBhanu Gollapudi bnx2fc_scsi_done(io_req, DID_ABORT); 1175853e2bd2SBhanu Gollapudi 1176853e2bd2SBhanu Gollapudi kref_put(&io_req->refcount, bnx2fc_cmd_release); 1177853e2bd2SBhanu Gollapudi spin_unlock_bh(&tgt->tgt_lock); 1178853e2bd2SBhanu Gollapudi return SUCCESS; 1179853e2bd2SBhanu Gollapudi } 1180853e2bd2SBhanu Gollapudi 1181853e2bd2SBhanu Gollapudi /* 1182853e2bd2SBhanu Gollapudi * Only eh_abort processing will remove the IO from 1183853e2bd2SBhanu Gollapudi * active_cmd_q before processing the request. this is 1184853e2bd2SBhanu Gollapudi * done to avoid race conditions between IOs aborted 1185853e2bd2SBhanu Gollapudi * as part of task management completion and eh_abort 1186853e2bd2SBhanu Gollapudi * processing 1187853e2bd2SBhanu Gollapudi */ 1188853e2bd2SBhanu Gollapudi list_del_init(&io_req->link); 1189853e2bd2SBhanu Gollapudi io_req->on_active_queue = 0; 1190853e2bd2SBhanu Gollapudi /* Move IO req to retire queue */ 1191853e2bd2SBhanu Gollapudi list_add_tail(&io_req->link, &tgt->io_retire_queue); 1192853e2bd2SBhanu Gollapudi 1193853e2bd2SBhanu Gollapudi init_completion(&io_req->tm_done); 1194853e2bd2SBhanu Gollapudi 1195c1bb4f33SBhanu Prakash Gollapudi if (test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags)) { 1196b2a554ffSBhanu Prakash Gollapudi printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) " 1197853e2bd2SBhanu Gollapudi "already in abts processing\n", io_req->xid); 119899cc600cSBhanu Prakash Gollapudi if (cancel_delayed_work(&io_req->timeout_work)) 119999cc600cSBhanu Prakash Gollapudi kref_put(&io_req->refcount, 120099cc600cSBhanu Prakash Gollapudi bnx2fc_cmd_release); /* drop timer hold */ 120150a87414SChad Dupuis rc = bnx2fc_abts_cleanup(io_req); 120221837896SEddie Wai /* This only occurs when an task abort was requested while ABTS 120321837896SEddie Wai is in progress. Setting the IO_CLEANUP flag will skip the 120421837896SEddie Wai RRQ process in the case when the fw generated SCSI_CMD cmpl 120521837896SEddie Wai was a result from the ABTS request rather than the CLEANUP 120621837896SEddie Wai request */ 120721837896SEddie Wai set_bit(BNX2FC_FLAG_IO_CLEANUP, &io_req->req_flags); 1208c1bb4f33SBhanu Prakash Gollapudi goto out; 1209c1bb4f33SBhanu Prakash Gollapudi } 1210c1bb4f33SBhanu Prakash Gollapudi 1211c1bb4f33SBhanu Prakash Gollapudi /* Cancel the current timer running on this io_req */ 1212c1bb4f33SBhanu Prakash Gollapudi if (cancel_delayed_work(&io_req->timeout_work)) 1213c1bb4f33SBhanu Prakash Gollapudi kref_put(&io_req->refcount, 1214c1bb4f33SBhanu Prakash Gollapudi bnx2fc_cmd_release); /* drop timer hold */ 1215c1bb4f33SBhanu Prakash Gollapudi set_bit(BNX2FC_FLAG_EH_ABORT, &io_req->req_flags); 1216c1bb4f33SBhanu Prakash Gollapudi io_req->wait_for_comp = 1; 1217c1bb4f33SBhanu Prakash Gollapudi rc = bnx2fc_initiate_abts(io_req); 1218c1bb4f33SBhanu Prakash Gollapudi if (rc == FAILED) { 121999cc600cSBhanu Prakash Gollapudi bnx2fc_initiate_cleanup(io_req); 122099cc600cSBhanu Prakash Gollapudi spin_unlock_bh(&tgt->tgt_lock); 122199cc600cSBhanu Prakash Gollapudi wait_for_completion(&io_req->tm_done); 122299cc600cSBhanu Prakash Gollapudi spin_lock_bh(&tgt->tgt_lock); 122399cc600cSBhanu Prakash Gollapudi io_req->wait_for_comp = 0; 1224c1bb4f33SBhanu Prakash Gollapudi goto done; 1225853e2bd2SBhanu Gollapudi } 1226853e2bd2SBhanu Gollapudi spin_unlock_bh(&tgt->tgt_lock); 1227853e2bd2SBhanu Gollapudi 1228853e2bd2SBhanu Gollapudi wait_for_completion(&io_req->tm_done); 1229853e2bd2SBhanu Gollapudi 1230853e2bd2SBhanu Gollapudi spin_lock_bh(&tgt->tgt_lock); 1231853e2bd2SBhanu Gollapudi io_req->wait_for_comp = 0; 12325d78f175SBhanu Prakash Gollapudi if (test_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags)) { 12335d78f175SBhanu Prakash Gollapudi BNX2FC_IO_DBG(io_req, "IO completed in a different context\n"); 12345d78f175SBhanu Prakash Gollapudi rc = SUCCESS; 12355d78f175SBhanu Prakash Gollapudi } else if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE, 1236853e2bd2SBhanu Gollapudi &io_req->req_flags))) { 1237853e2bd2SBhanu Gollapudi /* Let the scsi-ml try to recover this command */ 1238853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "abort failed, xid = 0x%x\n", 1239853e2bd2SBhanu Gollapudi io_req->xid); 124050a87414SChad Dupuis rc = bnx2fc_abts_cleanup(io_req); 1241c1bb4f33SBhanu Prakash Gollapudi goto out; 1242853e2bd2SBhanu Gollapudi } else { 1243853e2bd2SBhanu Gollapudi /* 1244853e2bd2SBhanu Gollapudi * We come here even when there was a race condition 1245853e2bd2SBhanu Gollapudi * between timeout and abts completion, and abts 1246853e2bd2SBhanu Gollapudi * completion happens just in time. 1247853e2bd2SBhanu Gollapudi */ 1248853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "abort succeeded\n"); 1249853e2bd2SBhanu Gollapudi rc = SUCCESS; 1250853e2bd2SBhanu Gollapudi bnx2fc_scsi_done(io_req, DID_ABORT); 1251853e2bd2SBhanu Gollapudi kref_put(&io_req->refcount, bnx2fc_cmd_release); 1252853e2bd2SBhanu Gollapudi } 1253c1bb4f33SBhanu Prakash Gollapudi done: 1254853e2bd2SBhanu Gollapudi /* release the reference taken in eh_abort */ 1255853e2bd2SBhanu Gollapudi kref_put(&io_req->refcount, bnx2fc_cmd_release); 1256c1bb4f33SBhanu Prakash Gollapudi out: 1257853e2bd2SBhanu Gollapudi spin_unlock_bh(&tgt->tgt_lock); 1258853e2bd2SBhanu Gollapudi return rc; 1259853e2bd2SBhanu Gollapudi } 1260853e2bd2SBhanu Gollapudi 12616c5a7ce4SBhanu Prakash Gollapudi void bnx2fc_process_seq_cleanup_compl(struct bnx2fc_cmd *seq_clnp_req, 12626c5a7ce4SBhanu Prakash Gollapudi struct fcoe_task_ctx_entry *task, 12636c5a7ce4SBhanu Prakash Gollapudi u8 rx_state) 12646c5a7ce4SBhanu Prakash Gollapudi { 12656c5a7ce4SBhanu Prakash Gollapudi struct bnx2fc_els_cb_arg *cb_arg = seq_clnp_req->cb_arg; 12666c5a7ce4SBhanu Prakash Gollapudi struct bnx2fc_cmd *orig_io_req = cb_arg->aborted_io_req; 12676c5a7ce4SBhanu Prakash Gollapudi u32 offset = cb_arg->offset; 12686c5a7ce4SBhanu Prakash Gollapudi enum fc_rctl r_ctl = cb_arg->r_ctl; 12696c5a7ce4SBhanu Prakash Gollapudi int rc = 0; 12706c5a7ce4SBhanu Prakash Gollapudi struct bnx2fc_rport *tgt = orig_io_req->tgt; 12716c5a7ce4SBhanu Prakash Gollapudi 12726c5a7ce4SBhanu Prakash Gollapudi BNX2FC_IO_DBG(orig_io_req, "Entered process_cleanup_compl xid = 0x%x" 12736c5a7ce4SBhanu Prakash Gollapudi "cmd_type = %d\n", 12746c5a7ce4SBhanu Prakash Gollapudi seq_clnp_req->xid, seq_clnp_req->cmd_type); 12756c5a7ce4SBhanu Prakash Gollapudi 12766c5a7ce4SBhanu Prakash Gollapudi if (rx_state == FCOE_TASK_RX_STATE_IGNORED_SEQUENCE_CLEANUP) { 12776c5a7ce4SBhanu Prakash Gollapudi printk(KERN_ERR PFX "seq cleanup ignored - xid = 0x%x\n", 12786c5a7ce4SBhanu Prakash Gollapudi seq_clnp_req->xid); 12796c5a7ce4SBhanu Prakash Gollapudi goto free_cb_arg; 12806c5a7ce4SBhanu Prakash Gollapudi } 12816c5a7ce4SBhanu Prakash Gollapudi 12826c5a7ce4SBhanu Prakash Gollapudi spin_unlock_bh(&tgt->tgt_lock); 12836c5a7ce4SBhanu Prakash Gollapudi rc = bnx2fc_send_srr(orig_io_req, offset, r_ctl); 12846c5a7ce4SBhanu Prakash Gollapudi spin_lock_bh(&tgt->tgt_lock); 12856c5a7ce4SBhanu Prakash Gollapudi 12866c5a7ce4SBhanu Prakash Gollapudi if (rc) 12876c5a7ce4SBhanu Prakash Gollapudi printk(KERN_ERR PFX "clnup_compl: Unable to send SRR" 12886c5a7ce4SBhanu Prakash Gollapudi " IO will abort\n"); 12896c5a7ce4SBhanu Prakash Gollapudi seq_clnp_req->cb_arg = NULL; 12906c5a7ce4SBhanu Prakash Gollapudi kref_put(&orig_io_req->refcount, bnx2fc_cmd_release); 12916c5a7ce4SBhanu Prakash Gollapudi free_cb_arg: 12926c5a7ce4SBhanu Prakash Gollapudi kfree(cb_arg); 12936c5a7ce4SBhanu Prakash Gollapudi return; 12946c5a7ce4SBhanu Prakash Gollapudi } 12956c5a7ce4SBhanu Prakash Gollapudi 1296853e2bd2SBhanu Gollapudi void bnx2fc_process_cleanup_compl(struct bnx2fc_cmd *io_req, 1297853e2bd2SBhanu Gollapudi struct fcoe_task_ctx_entry *task, 1298853e2bd2SBhanu Gollapudi u8 num_rq) 1299853e2bd2SBhanu Gollapudi { 1300853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "Entered process_cleanup_compl " 1301853e2bd2SBhanu Gollapudi "refcnt = %d, cmd_type = %d\n", 1302853e2bd2SBhanu Gollapudi io_req->refcount.refcount.counter, io_req->cmd_type); 1303853e2bd2SBhanu Gollapudi bnx2fc_scsi_done(io_req, DID_ERROR); 1304853e2bd2SBhanu Gollapudi kref_put(&io_req->refcount, bnx2fc_cmd_release); 130599cc600cSBhanu Prakash Gollapudi if (io_req->wait_for_comp) 130699cc600cSBhanu Prakash Gollapudi complete(&io_req->tm_done); 1307853e2bd2SBhanu Gollapudi } 1308853e2bd2SBhanu Gollapudi 1309853e2bd2SBhanu Gollapudi void bnx2fc_process_abts_compl(struct bnx2fc_cmd *io_req, 1310853e2bd2SBhanu Gollapudi struct fcoe_task_ctx_entry *task, 1311853e2bd2SBhanu Gollapudi u8 num_rq) 1312853e2bd2SBhanu Gollapudi { 1313853e2bd2SBhanu Gollapudi u32 r_ctl; 1314853e2bd2SBhanu Gollapudi u32 r_a_tov = FC_DEF_R_A_TOV; 1315853e2bd2SBhanu Gollapudi u8 issue_rrq = 0; 1316853e2bd2SBhanu Gollapudi struct bnx2fc_rport *tgt = io_req->tgt; 1317853e2bd2SBhanu Gollapudi 1318853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "Entered process_abts_compl xid = 0x%x" 1319853e2bd2SBhanu Gollapudi "refcnt = %d, cmd_type = %d\n", 1320853e2bd2SBhanu Gollapudi io_req->xid, 1321853e2bd2SBhanu Gollapudi io_req->refcount.refcount.counter, io_req->cmd_type); 1322853e2bd2SBhanu Gollapudi 1323853e2bd2SBhanu Gollapudi if (test_and_set_bit(BNX2FC_FLAG_ABTS_DONE, 1324853e2bd2SBhanu Gollapudi &io_req->req_flags)) { 1325853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "Timer context finished processing" 1326853e2bd2SBhanu Gollapudi " this io\n"); 1327853e2bd2SBhanu Gollapudi return; 1328853e2bd2SBhanu Gollapudi } 1329853e2bd2SBhanu Gollapudi 1330853e2bd2SBhanu Gollapudi /* Do not issue RRQ as this IO is already cleanedup */ 1331853e2bd2SBhanu Gollapudi if (test_and_set_bit(BNX2FC_FLAG_IO_CLEANUP, 1332853e2bd2SBhanu Gollapudi &io_req->req_flags)) 1333853e2bd2SBhanu Gollapudi goto io_compl; 1334853e2bd2SBhanu Gollapudi 1335853e2bd2SBhanu Gollapudi /* 1336853e2bd2SBhanu Gollapudi * For ABTS issued due to SCSI eh_abort_handler, timeout 1337853e2bd2SBhanu Gollapudi * values are maintained by scsi-ml itself. Cancel timeout 1338853e2bd2SBhanu Gollapudi * in case ABTS issued as part of task management function 1339853e2bd2SBhanu Gollapudi * or due to FW error. 1340853e2bd2SBhanu Gollapudi */ 1341853e2bd2SBhanu Gollapudi if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags)) 1342853e2bd2SBhanu Gollapudi if (cancel_delayed_work(&io_req->timeout_work)) 1343853e2bd2SBhanu Gollapudi kref_put(&io_req->refcount, 1344853e2bd2SBhanu Gollapudi bnx2fc_cmd_release); /* drop timer hold */ 1345853e2bd2SBhanu Gollapudi 1346619c5cb6SVlad Zolotarov r_ctl = (u8)task->rxwr_only.union_ctx.comp_info.abts_rsp.r_ctl; 1347853e2bd2SBhanu Gollapudi 1348853e2bd2SBhanu Gollapudi switch (r_ctl) { 1349853e2bd2SBhanu Gollapudi case FC_RCTL_BA_ACC: 1350853e2bd2SBhanu Gollapudi /* 1351853e2bd2SBhanu Gollapudi * Dont release this cmd yet. It will be relesed 1352853e2bd2SBhanu Gollapudi * after we get RRQ response 1353853e2bd2SBhanu Gollapudi */ 1354853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "ABTS response - ACC Send RRQ\n"); 1355853e2bd2SBhanu Gollapudi issue_rrq = 1; 1356853e2bd2SBhanu Gollapudi break; 1357853e2bd2SBhanu Gollapudi 1358853e2bd2SBhanu Gollapudi case FC_RCTL_BA_RJT: 1359853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "ABTS response - RJT\n"); 1360853e2bd2SBhanu Gollapudi break; 1361853e2bd2SBhanu Gollapudi default: 1362853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "Unknown ABTS response\n"); 1363853e2bd2SBhanu Gollapudi break; 1364853e2bd2SBhanu Gollapudi } 1365853e2bd2SBhanu Gollapudi 1366853e2bd2SBhanu Gollapudi if (issue_rrq) { 1367853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "Issue RRQ after R_A_TOV\n"); 1368853e2bd2SBhanu Gollapudi set_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags); 1369853e2bd2SBhanu Gollapudi } 1370853e2bd2SBhanu Gollapudi set_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags); 1371853e2bd2SBhanu Gollapudi bnx2fc_cmd_timer_set(io_req, r_a_tov); 1372853e2bd2SBhanu Gollapudi 1373853e2bd2SBhanu Gollapudi io_compl: 1374853e2bd2SBhanu Gollapudi if (io_req->wait_for_comp) { 1375853e2bd2SBhanu Gollapudi if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT, 1376853e2bd2SBhanu Gollapudi &io_req->req_flags)) 1377853e2bd2SBhanu Gollapudi complete(&io_req->tm_done); 1378853e2bd2SBhanu Gollapudi } else { 1379853e2bd2SBhanu Gollapudi /* 1380853e2bd2SBhanu Gollapudi * We end up here when ABTS is issued as 1381853e2bd2SBhanu Gollapudi * in asynchronous context, i.e., as part 1382853e2bd2SBhanu Gollapudi * of task management completion, or 1383853e2bd2SBhanu Gollapudi * when FW error is received or when the 1384853e2bd2SBhanu Gollapudi * ABTS is issued when the IO is timed 1385853e2bd2SBhanu Gollapudi * out. 1386853e2bd2SBhanu Gollapudi */ 1387853e2bd2SBhanu Gollapudi 1388853e2bd2SBhanu Gollapudi if (io_req->on_active_queue) { 1389853e2bd2SBhanu Gollapudi list_del_init(&io_req->link); 1390853e2bd2SBhanu Gollapudi io_req->on_active_queue = 0; 1391853e2bd2SBhanu Gollapudi /* Move IO req to retire queue */ 1392853e2bd2SBhanu Gollapudi list_add_tail(&io_req->link, &tgt->io_retire_queue); 1393853e2bd2SBhanu Gollapudi } 1394853e2bd2SBhanu Gollapudi bnx2fc_scsi_done(io_req, DID_ERROR); 1395853e2bd2SBhanu Gollapudi kref_put(&io_req->refcount, bnx2fc_cmd_release); 1396853e2bd2SBhanu Gollapudi } 1397853e2bd2SBhanu Gollapudi } 1398853e2bd2SBhanu Gollapudi 1399853e2bd2SBhanu Gollapudi static void bnx2fc_lun_reset_cmpl(struct bnx2fc_cmd *io_req) 1400853e2bd2SBhanu Gollapudi { 1401853e2bd2SBhanu Gollapudi struct scsi_cmnd *sc_cmd = io_req->sc_cmd; 1402853e2bd2SBhanu Gollapudi struct bnx2fc_rport *tgt = io_req->tgt; 1403d71fb3bdSBhanu Prakash Gollapudi struct bnx2fc_cmd *cmd, *tmp; 14049cb78c16SHannes Reinecke u64 tm_lun = sc_cmd->device->lun; 14059cb78c16SHannes Reinecke u64 lun; 1406853e2bd2SBhanu Gollapudi int rc = 0; 1407853e2bd2SBhanu Gollapudi 1408853e2bd2SBhanu Gollapudi /* called with tgt_lock held */ 1409853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "Entered bnx2fc_lun_reset_cmpl\n"); 1410853e2bd2SBhanu Gollapudi /* 1411853e2bd2SBhanu Gollapudi * Walk thru the active_ios queue and ABORT the IO 1412853e2bd2SBhanu Gollapudi * that matches with the LUN that was reset 1413853e2bd2SBhanu Gollapudi */ 1414d71fb3bdSBhanu Prakash Gollapudi list_for_each_entry_safe(cmd, tmp, &tgt->active_cmd_queue, link) { 1415853e2bd2SBhanu Gollapudi BNX2FC_TGT_DBG(tgt, "LUN RST cmpl: scan for pending IOs\n"); 1416853e2bd2SBhanu Gollapudi lun = cmd->sc_cmd->device->lun; 1417853e2bd2SBhanu Gollapudi if (lun == tm_lun) { 1418853e2bd2SBhanu Gollapudi /* Initiate ABTS on this cmd */ 1419853e2bd2SBhanu Gollapudi if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS, 1420853e2bd2SBhanu Gollapudi &cmd->req_flags)) { 1421853e2bd2SBhanu Gollapudi /* cancel the IO timeout */ 1422853e2bd2SBhanu Gollapudi if (cancel_delayed_work(&io_req->timeout_work)) 1423853e2bd2SBhanu Gollapudi kref_put(&io_req->refcount, 1424853e2bd2SBhanu Gollapudi bnx2fc_cmd_release); 1425853e2bd2SBhanu Gollapudi /* timer hold */ 1426853e2bd2SBhanu Gollapudi rc = bnx2fc_initiate_abts(cmd); 142725985edcSLucas De Marchi /* abts shouldn't fail in this context */ 1428853e2bd2SBhanu Gollapudi WARN_ON(rc != SUCCESS); 1429853e2bd2SBhanu Gollapudi } else 1430853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "lun_rst: abts already in" 1431853e2bd2SBhanu Gollapudi " progress for this IO 0x%x\n", 1432853e2bd2SBhanu Gollapudi cmd->xid); 1433853e2bd2SBhanu Gollapudi } 1434853e2bd2SBhanu Gollapudi } 1435853e2bd2SBhanu Gollapudi } 1436853e2bd2SBhanu Gollapudi 1437853e2bd2SBhanu Gollapudi static void bnx2fc_tgt_reset_cmpl(struct bnx2fc_cmd *io_req) 1438853e2bd2SBhanu Gollapudi { 1439853e2bd2SBhanu Gollapudi struct bnx2fc_rport *tgt = io_req->tgt; 1440d71fb3bdSBhanu Prakash Gollapudi struct bnx2fc_cmd *cmd, *tmp; 1441853e2bd2SBhanu Gollapudi int rc = 0; 1442853e2bd2SBhanu Gollapudi 1443853e2bd2SBhanu Gollapudi /* called with tgt_lock held */ 1444853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "Entered bnx2fc_tgt_reset_cmpl\n"); 1445853e2bd2SBhanu Gollapudi /* 1446853e2bd2SBhanu Gollapudi * Walk thru the active_ios queue and ABORT the IO 1447853e2bd2SBhanu Gollapudi * that matches with the LUN that was reset 1448853e2bd2SBhanu Gollapudi */ 1449d71fb3bdSBhanu Prakash Gollapudi list_for_each_entry_safe(cmd, tmp, &tgt->active_cmd_queue, link) { 1450853e2bd2SBhanu Gollapudi BNX2FC_TGT_DBG(tgt, "TGT RST cmpl: scan for pending IOs\n"); 1451853e2bd2SBhanu Gollapudi /* Initiate ABTS */ 1452853e2bd2SBhanu Gollapudi if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS, 1453853e2bd2SBhanu Gollapudi &cmd->req_flags)) { 1454853e2bd2SBhanu Gollapudi /* cancel the IO timeout */ 1455853e2bd2SBhanu Gollapudi if (cancel_delayed_work(&io_req->timeout_work)) 1456853e2bd2SBhanu Gollapudi kref_put(&io_req->refcount, 1457853e2bd2SBhanu Gollapudi bnx2fc_cmd_release); /* timer hold */ 1458853e2bd2SBhanu Gollapudi rc = bnx2fc_initiate_abts(cmd); 145925985edcSLucas De Marchi /* abts shouldn't fail in this context */ 1460853e2bd2SBhanu Gollapudi WARN_ON(rc != SUCCESS); 1461853e2bd2SBhanu Gollapudi 1462853e2bd2SBhanu Gollapudi } else 1463853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "tgt_rst: abts already in progress" 1464853e2bd2SBhanu Gollapudi " for this IO 0x%x\n", cmd->xid); 1465853e2bd2SBhanu Gollapudi } 1466853e2bd2SBhanu Gollapudi } 1467853e2bd2SBhanu Gollapudi 1468853e2bd2SBhanu Gollapudi void bnx2fc_process_tm_compl(struct bnx2fc_cmd *io_req, 1469853e2bd2SBhanu Gollapudi struct fcoe_task_ctx_entry *task, u8 num_rq) 1470853e2bd2SBhanu Gollapudi { 1471853e2bd2SBhanu Gollapudi struct bnx2fc_mp_req *tm_req; 1472853e2bd2SBhanu Gollapudi struct fc_frame_header *fc_hdr; 1473853e2bd2SBhanu Gollapudi struct scsi_cmnd *sc_cmd = io_req->sc_cmd; 1474853e2bd2SBhanu Gollapudi u64 *hdr; 1475853e2bd2SBhanu Gollapudi u64 *temp_hdr; 1476853e2bd2SBhanu Gollapudi void *rsp_buf; 1477853e2bd2SBhanu Gollapudi 1478853e2bd2SBhanu Gollapudi /* Called with tgt_lock held */ 1479853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "Entered process_tm_compl\n"); 1480853e2bd2SBhanu Gollapudi 1481853e2bd2SBhanu Gollapudi if (!(test_bit(BNX2FC_FLAG_TM_TIMEOUT, &io_req->req_flags))) 1482853e2bd2SBhanu Gollapudi set_bit(BNX2FC_FLAG_TM_COMPL, &io_req->req_flags); 1483853e2bd2SBhanu Gollapudi else { 1484853e2bd2SBhanu Gollapudi /* TM has already timed out and we got 1485853e2bd2SBhanu Gollapudi * delayed completion. Ignore completion 1486853e2bd2SBhanu Gollapudi * processing. 1487853e2bd2SBhanu Gollapudi */ 1488853e2bd2SBhanu Gollapudi return; 1489853e2bd2SBhanu Gollapudi } 1490853e2bd2SBhanu Gollapudi 1491853e2bd2SBhanu Gollapudi tm_req = &(io_req->mp_req); 1492853e2bd2SBhanu Gollapudi fc_hdr = &(tm_req->resp_fc_hdr); 1493853e2bd2SBhanu Gollapudi hdr = (u64 *)fc_hdr; 1494853e2bd2SBhanu Gollapudi temp_hdr = (u64 *) 1495619c5cb6SVlad Zolotarov &task->rxwr_only.union_ctx.comp_info.mp_rsp.fc_hdr; 1496853e2bd2SBhanu Gollapudi hdr[0] = cpu_to_be64(temp_hdr[0]); 1497853e2bd2SBhanu Gollapudi hdr[1] = cpu_to_be64(temp_hdr[1]); 1498853e2bd2SBhanu Gollapudi hdr[2] = cpu_to_be64(temp_hdr[2]); 1499853e2bd2SBhanu Gollapudi 1500619c5cb6SVlad Zolotarov tm_req->resp_len = 1501619c5cb6SVlad Zolotarov task->rxwr_only.union_ctx.comp_info.mp_rsp.mp_payload_len; 1502853e2bd2SBhanu Gollapudi 1503853e2bd2SBhanu Gollapudi rsp_buf = tm_req->resp_buf; 1504853e2bd2SBhanu Gollapudi 1505853e2bd2SBhanu Gollapudi if (fc_hdr->fh_r_ctl == FC_RCTL_DD_CMD_STATUS) { 1506853e2bd2SBhanu Gollapudi bnx2fc_parse_fcp_rsp(io_req, 1507853e2bd2SBhanu Gollapudi (struct fcoe_fcp_rsp_payload *) 1508853e2bd2SBhanu Gollapudi rsp_buf, num_rq); 1509853e2bd2SBhanu Gollapudi if (io_req->fcp_rsp_code == 0) { 1510853e2bd2SBhanu Gollapudi /* TM successful */ 1511853e2bd2SBhanu Gollapudi if (tm_req->tm_flags & FCP_TMF_LUN_RESET) 1512853e2bd2SBhanu Gollapudi bnx2fc_lun_reset_cmpl(io_req); 1513853e2bd2SBhanu Gollapudi else if (tm_req->tm_flags & FCP_TMF_TGT_RESET) 1514853e2bd2SBhanu Gollapudi bnx2fc_tgt_reset_cmpl(io_req); 1515853e2bd2SBhanu Gollapudi } 1516853e2bd2SBhanu Gollapudi } else { 1517853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "tmf's fc_hdr r_ctl = 0x%x\n", 1518853e2bd2SBhanu Gollapudi fc_hdr->fh_r_ctl); 1519853e2bd2SBhanu Gollapudi } 1520853e2bd2SBhanu Gollapudi if (!sc_cmd->SCp.ptr) { 1521b2a554ffSBhanu Prakash Gollapudi printk(KERN_ERR PFX "tm_compl: SCp.ptr is NULL\n"); 1522853e2bd2SBhanu Gollapudi return; 1523853e2bd2SBhanu Gollapudi } 1524853e2bd2SBhanu Gollapudi switch (io_req->fcp_status) { 1525853e2bd2SBhanu Gollapudi case FC_GOOD: 1526853e2bd2SBhanu Gollapudi if (io_req->cdb_status == 0) { 1527853e2bd2SBhanu Gollapudi /* Good IO completion */ 1528853e2bd2SBhanu Gollapudi sc_cmd->result = DID_OK << 16; 1529853e2bd2SBhanu Gollapudi } else { 1530853e2bd2SBhanu Gollapudi /* Transport status is good, SCSI status not good */ 1531853e2bd2SBhanu Gollapudi sc_cmd->result = (DID_OK << 16) | io_req->cdb_status; 1532853e2bd2SBhanu Gollapudi } 1533853e2bd2SBhanu Gollapudi if (io_req->fcp_resid) 1534853e2bd2SBhanu Gollapudi scsi_set_resid(sc_cmd, io_req->fcp_resid); 1535853e2bd2SBhanu Gollapudi break; 1536853e2bd2SBhanu Gollapudi 1537853e2bd2SBhanu Gollapudi default: 1538853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "process_tm_compl: fcp_status = %d\n", 1539853e2bd2SBhanu Gollapudi io_req->fcp_status); 1540853e2bd2SBhanu Gollapudi break; 1541853e2bd2SBhanu Gollapudi } 1542853e2bd2SBhanu Gollapudi 1543853e2bd2SBhanu Gollapudi sc_cmd = io_req->sc_cmd; 1544853e2bd2SBhanu Gollapudi io_req->sc_cmd = NULL; 1545853e2bd2SBhanu Gollapudi 1546853e2bd2SBhanu Gollapudi /* check if the io_req exists in tgt's tmf_q */ 1547853e2bd2SBhanu Gollapudi if (io_req->on_tmf_queue) { 1548853e2bd2SBhanu Gollapudi 1549853e2bd2SBhanu Gollapudi list_del_init(&io_req->link); 1550853e2bd2SBhanu Gollapudi io_req->on_tmf_queue = 0; 1551853e2bd2SBhanu Gollapudi } else { 1552853e2bd2SBhanu Gollapudi 1553b2a554ffSBhanu Prakash Gollapudi printk(KERN_ERR PFX "Command not on active_cmd_queue!\n"); 1554853e2bd2SBhanu Gollapudi return; 1555853e2bd2SBhanu Gollapudi } 1556853e2bd2SBhanu Gollapudi 1557853e2bd2SBhanu Gollapudi sc_cmd->SCp.ptr = NULL; 1558853e2bd2SBhanu Gollapudi sc_cmd->scsi_done(sc_cmd); 1559853e2bd2SBhanu Gollapudi 1560853e2bd2SBhanu Gollapudi kref_put(&io_req->refcount, bnx2fc_cmd_release); 1561853e2bd2SBhanu Gollapudi if (io_req->wait_for_comp) { 1562853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "tm_compl - wake up the waiter\n"); 1563853e2bd2SBhanu Gollapudi complete(&io_req->tm_done); 1564853e2bd2SBhanu Gollapudi } 1565853e2bd2SBhanu Gollapudi } 1566853e2bd2SBhanu Gollapudi 1567853e2bd2SBhanu Gollapudi static int bnx2fc_split_bd(struct bnx2fc_cmd *io_req, u64 addr, int sg_len, 1568853e2bd2SBhanu Gollapudi int bd_index) 1569853e2bd2SBhanu Gollapudi { 1570853e2bd2SBhanu Gollapudi struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl; 1571853e2bd2SBhanu Gollapudi int frag_size, sg_frags; 1572853e2bd2SBhanu Gollapudi 1573853e2bd2SBhanu Gollapudi sg_frags = 0; 1574853e2bd2SBhanu Gollapudi while (sg_len) { 1575853e2bd2SBhanu Gollapudi if (sg_len >= BNX2FC_BD_SPLIT_SZ) 1576853e2bd2SBhanu Gollapudi frag_size = BNX2FC_BD_SPLIT_SZ; 1577853e2bd2SBhanu Gollapudi else 1578853e2bd2SBhanu Gollapudi frag_size = sg_len; 1579853e2bd2SBhanu Gollapudi bd[bd_index + sg_frags].buf_addr_lo = addr & 0xffffffff; 1580853e2bd2SBhanu Gollapudi bd[bd_index + sg_frags].buf_addr_hi = addr >> 32; 1581853e2bd2SBhanu Gollapudi bd[bd_index + sg_frags].buf_len = (u16)frag_size; 1582853e2bd2SBhanu Gollapudi bd[bd_index + sg_frags].flags = 0; 1583853e2bd2SBhanu Gollapudi 1584853e2bd2SBhanu Gollapudi addr += (u64) frag_size; 1585853e2bd2SBhanu Gollapudi sg_frags++; 1586853e2bd2SBhanu Gollapudi sg_len -= frag_size; 1587853e2bd2SBhanu Gollapudi } 1588853e2bd2SBhanu Gollapudi return sg_frags; 1589853e2bd2SBhanu Gollapudi 1590853e2bd2SBhanu Gollapudi } 1591853e2bd2SBhanu Gollapudi 1592853e2bd2SBhanu Gollapudi static int bnx2fc_map_sg(struct bnx2fc_cmd *io_req) 1593853e2bd2SBhanu Gollapudi { 15943ce41ea1SBhanu Prakash Gollapudi struct bnx2fc_interface *interface = io_req->port->priv; 15953ce41ea1SBhanu Prakash Gollapudi struct bnx2fc_hba *hba = interface->hba; 1596853e2bd2SBhanu Gollapudi struct scsi_cmnd *sc = io_req->sc_cmd; 1597853e2bd2SBhanu Gollapudi struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl; 1598853e2bd2SBhanu Gollapudi struct scatterlist *sg; 1599853e2bd2SBhanu Gollapudi int byte_count = 0; 1600853e2bd2SBhanu Gollapudi int sg_count = 0; 1601853e2bd2SBhanu Gollapudi int bd_count = 0; 1602853e2bd2SBhanu Gollapudi int sg_frags; 1603853e2bd2SBhanu Gollapudi unsigned int sg_len; 1604853e2bd2SBhanu Gollapudi u64 addr; 1605853e2bd2SBhanu Gollapudi int i; 1606853e2bd2SBhanu Gollapudi 16075565461eSChad Dupuis /* 16085565461eSChad Dupuis * Use dma_map_sg directly to ensure we're using the correct 16095565461eSChad Dupuis * dev struct off of pcidev. 16105565461eSChad Dupuis */ 16113ce41ea1SBhanu Prakash Gollapudi sg_count = dma_map_sg(&hba->pcidev->dev, scsi_sglist(sc), 16123ce41ea1SBhanu Prakash Gollapudi scsi_sg_count(sc), sc->sc_data_direction); 1613853e2bd2SBhanu Gollapudi scsi_for_each_sg(sc, sg, sg_count, i) { 1614853e2bd2SBhanu Gollapudi sg_len = sg_dma_len(sg); 1615853e2bd2SBhanu Gollapudi addr = sg_dma_address(sg); 1616853e2bd2SBhanu Gollapudi if (sg_len > BNX2FC_MAX_BD_LEN) { 1617853e2bd2SBhanu Gollapudi sg_frags = bnx2fc_split_bd(io_req, addr, sg_len, 1618853e2bd2SBhanu Gollapudi bd_count); 1619853e2bd2SBhanu Gollapudi } else { 1620853e2bd2SBhanu Gollapudi 1621853e2bd2SBhanu Gollapudi sg_frags = 1; 1622853e2bd2SBhanu Gollapudi bd[bd_count].buf_addr_lo = addr & 0xffffffff; 1623853e2bd2SBhanu Gollapudi bd[bd_count].buf_addr_hi = addr >> 32; 1624853e2bd2SBhanu Gollapudi bd[bd_count].buf_len = (u16)sg_len; 1625853e2bd2SBhanu Gollapudi bd[bd_count].flags = 0; 1626853e2bd2SBhanu Gollapudi } 1627853e2bd2SBhanu Gollapudi bd_count += sg_frags; 1628853e2bd2SBhanu Gollapudi byte_count += sg_len; 1629853e2bd2SBhanu Gollapudi } 1630853e2bd2SBhanu Gollapudi if (byte_count != scsi_bufflen(sc)) 1631853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "byte_count = %d != scsi_bufflen = %d, " 1632853e2bd2SBhanu Gollapudi "task_id = 0x%x\n", byte_count, scsi_bufflen(sc), 1633853e2bd2SBhanu Gollapudi io_req->xid); 1634853e2bd2SBhanu Gollapudi return bd_count; 1635853e2bd2SBhanu Gollapudi } 1636853e2bd2SBhanu Gollapudi 1637822f2903SBhanu Prakash Gollapudi static int bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd *io_req) 1638853e2bd2SBhanu Gollapudi { 1639853e2bd2SBhanu Gollapudi struct scsi_cmnd *sc = io_req->sc_cmd; 1640853e2bd2SBhanu Gollapudi struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl; 1641853e2bd2SBhanu Gollapudi int bd_count; 1642853e2bd2SBhanu Gollapudi 1643822f2903SBhanu Prakash Gollapudi if (scsi_sg_count(sc)) { 1644853e2bd2SBhanu Gollapudi bd_count = bnx2fc_map_sg(io_req); 1645822f2903SBhanu Prakash Gollapudi if (bd_count == 0) 1646822f2903SBhanu Prakash Gollapudi return -ENOMEM; 1647822f2903SBhanu Prakash Gollapudi } else { 1648853e2bd2SBhanu Gollapudi bd_count = 0; 1649853e2bd2SBhanu Gollapudi bd[0].buf_addr_lo = bd[0].buf_addr_hi = 0; 1650853e2bd2SBhanu Gollapudi bd[0].buf_len = bd[0].flags = 0; 1651853e2bd2SBhanu Gollapudi } 1652853e2bd2SBhanu Gollapudi io_req->bd_tbl->bd_valid = bd_count; 1653822f2903SBhanu Prakash Gollapudi 1654822f2903SBhanu Prakash Gollapudi return 0; 1655853e2bd2SBhanu Gollapudi } 1656853e2bd2SBhanu Gollapudi 1657853e2bd2SBhanu Gollapudi static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd *io_req) 1658853e2bd2SBhanu Gollapudi { 1659853e2bd2SBhanu Gollapudi struct scsi_cmnd *sc = io_req->sc_cmd; 16605565461eSChad Dupuis struct bnx2fc_interface *interface = io_req->port->priv; 16615565461eSChad Dupuis struct bnx2fc_hba *hba = interface->hba; 1662853e2bd2SBhanu Gollapudi 16635565461eSChad Dupuis /* 16645565461eSChad Dupuis * Use dma_unmap_sg directly to ensure we're using the correct 16655565461eSChad Dupuis * dev struct off of pcidev. 16665565461eSChad Dupuis */ 16675565461eSChad Dupuis if (io_req->bd_tbl->bd_valid && sc && scsi_sg_count(sc)) { 16685565461eSChad Dupuis dma_unmap_sg(&hba->pcidev->dev, scsi_sglist(sc), 16695565461eSChad Dupuis scsi_sg_count(sc), sc->sc_data_direction); 1670853e2bd2SBhanu Gollapudi io_req->bd_tbl->bd_valid = 0; 1671853e2bd2SBhanu Gollapudi } 1672853e2bd2SBhanu Gollapudi } 1673853e2bd2SBhanu Gollapudi 1674853e2bd2SBhanu Gollapudi void bnx2fc_build_fcp_cmnd(struct bnx2fc_cmd *io_req, 1675853e2bd2SBhanu Gollapudi struct fcp_cmnd *fcp_cmnd) 1676853e2bd2SBhanu Gollapudi { 1677853e2bd2SBhanu Gollapudi struct scsi_cmnd *sc_cmd = io_req->sc_cmd; 1678853e2bd2SBhanu Gollapudi 1679853e2bd2SBhanu Gollapudi memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd)); 1680853e2bd2SBhanu Gollapudi 1681e35fa8c2SAndy Grover int_to_scsilun(sc_cmd->device->lun, &fcp_cmnd->fc_lun); 1682853e2bd2SBhanu Gollapudi 1683853e2bd2SBhanu Gollapudi fcp_cmnd->fc_dl = htonl(io_req->data_xfer_len); 1684853e2bd2SBhanu Gollapudi memcpy(fcp_cmnd->fc_cdb, sc_cmd->cmnd, sc_cmd->cmd_len); 1685853e2bd2SBhanu Gollapudi 1686853e2bd2SBhanu Gollapudi fcp_cmnd->fc_cmdref = 0; 1687853e2bd2SBhanu Gollapudi fcp_cmnd->fc_pri_ta = 0; 1688853e2bd2SBhanu Gollapudi fcp_cmnd->fc_tm_flags = io_req->mp_req.tm_flags; 1689853e2bd2SBhanu Gollapudi fcp_cmnd->fc_flags = io_req->io_req_flags; 1690853e2bd2SBhanu Gollapudi fcp_cmnd->fc_pri_ta = FCP_PTA_SIMPLE; 1691853e2bd2SBhanu Gollapudi } 1692853e2bd2SBhanu Gollapudi 1693853e2bd2SBhanu Gollapudi static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd *io_req, 1694853e2bd2SBhanu Gollapudi struct fcoe_fcp_rsp_payload *fcp_rsp, 1695853e2bd2SBhanu Gollapudi u8 num_rq) 1696853e2bd2SBhanu Gollapudi { 1697853e2bd2SBhanu Gollapudi struct scsi_cmnd *sc_cmd = io_req->sc_cmd; 1698853e2bd2SBhanu Gollapudi struct bnx2fc_rport *tgt = io_req->tgt; 1699853e2bd2SBhanu Gollapudi u8 rsp_flags = fcp_rsp->fcp_flags.flags; 1700853e2bd2SBhanu Gollapudi u32 rq_buff_len = 0; 1701853e2bd2SBhanu Gollapudi int i; 1702853e2bd2SBhanu Gollapudi unsigned char *rq_data; 1703853e2bd2SBhanu Gollapudi unsigned char *dummy; 1704853e2bd2SBhanu Gollapudi int fcp_sns_len = 0; 1705853e2bd2SBhanu Gollapudi int fcp_rsp_len = 0; 1706853e2bd2SBhanu Gollapudi 1707853e2bd2SBhanu Gollapudi io_req->fcp_status = FC_GOOD; 1708adcf7dfbSChad Dupuis io_req->fcp_resid = 0; 1709adcf7dfbSChad Dupuis if (rsp_flags & (FCOE_FCP_RSP_FLAGS_FCP_RESID_OVER | 1710adcf7dfbSChad Dupuis FCOE_FCP_RSP_FLAGS_FCP_RESID_UNDER)) 1711853e2bd2SBhanu Gollapudi io_req->fcp_resid = fcp_rsp->fcp_resid; 1712853e2bd2SBhanu Gollapudi 1713853e2bd2SBhanu Gollapudi io_req->scsi_comp_flags = rsp_flags; 1714853e2bd2SBhanu Gollapudi CMD_SCSI_STATUS(sc_cmd) = io_req->cdb_status = 1715853e2bd2SBhanu Gollapudi fcp_rsp->scsi_status_code; 1716853e2bd2SBhanu Gollapudi 1717853e2bd2SBhanu Gollapudi /* Fetch fcp_rsp_info and fcp_sns_info if available */ 1718853e2bd2SBhanu Gollapudi if (num_rq) { 1719853e2bd2SBhanu Gollapudi 1720853e2bd2SBhanu Gollapudi /* 1721853e2bd2SBhanu Gollapudi * We do not anticipate num_rq >1, as the linux defined 1722853e2bd2SBhanu Gollapudi * SCSI_SENSE_BUFFERSIZE is 96 bytes + 8 bytes of FCP_RSP_INFO 1723853e2bd2SBhanu Gollapudi * 256 bytes of single rq buffer is good enough to hold this. 1724853e2bd2SBhanu Gollapudi */ 1725853e2bd2SBhanu Gollapudi 1726853e2bd2SBhanu Gollapudi if (rsp_flags & 1727853e2bd2SBhanu Gollapudi FCOE_FCP_RSP_FLAGS_FCP_RSP_LEN_VALID) { 1728853e2bd2SBhanu Gollapudi fcp_rsp_len = rq_buff_len 1729853e2bd2SBhanu Gollapudi = fcp_rsp->fcp_rsp_len; 1730853e2bd2SBhanu Gollapudi } 1731853e2bd2SBhanu Gollapudi 1732853e2bd2SBhanu Gollapudi if (rsp_flags & 1733853e2bd2SBhanu Gollapudi FCOE_FCP_RSP_FLAGS_FCP_SNS_LEN_VALID) { 1734853e2bd2SBhanu Gollapudi fcp_sns_len = fcp_rsp->fcp_sns_len; 1735853e2bd2SBhanu Gollapudi rq_buff_len += fcp_rsp->fcp_sns_len; 1736853e2bd2SBhanu Gollapudi } 1737853e2bd2SBhanu Gollapudi 1738853e2bd2SBhanu Gollapudi io_req->fcp_rsp_len = fcp_rsp_len; 1739853e2bd2SBhanu Gollapudi io_req->fcp_sns_len = fcp_sns_len; 1740853e2bd2SBhanu Gollapudi 1741853e2bd2SBhanu Gollapudi if (rq_buff_len > num_rq * BNX2FC_RQ_BUF_SZ) { 1742853e2bd2SBhanu Gollapudi /* Invalid sense sense length. */ 1743b2a554ffSBhanu Prakash Gollapudi printk(KERN_ERR PFX "invalid sns length %d\n", 1744853e2bd2SBhanu Gollapudi rq_buff_len); 1745853e2bd2SBhanu Gollapudi /* reset rq_buff_len */ 1746853e2bd2SBhanu Gollapudi rq_buff_len = num_rq * BNX2FC_RQ_BUF_SZ; 1747853e2bd2SBhanu Gollapudi } 1748853e2bd2SBhanu Gollapudi 1749853e2bd2SBhanu Gollapudi rq_data = bnx2fc_get_next_rqe(tgt, 1); 1750853e2bd2SBhanu Gollapudi 1751853e2bd2SBhanu Gollapudi if (num_rq > 1) { 1752853e2bd2SBhanu Gollapudi /* We do not need extra sense data */ 1753853e2bd2SBhanu Gollapudi for (i = 1; i < num_rq; i++) 1754853e2bd2SBhanu Gollapudi dummy = bnx2fc_get_next_rqe(tgt, 1); 1755853e2bd2SBhanu Gollapudi } 1756853e2bd2SBhanu Gollapudi 1757853e2bd2SBhanu Gollapudi /* fetch fcp_rsp_code */ 1758853e2bd2SBhanu Gollapudi if ((fcp_rsp_len == 4) || (fcp_rsp_len == 8)) { 1759853e2bd2SBhanu Gollapudi /* Only for task management function */ 1760853e2bd2SBhanu Gollapudi io_req->fcp_rsp_code = rq_data[3]; 1761853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "fcp_rsp_code = %d\n", 1762853e2bd2SBhanu Gollapudi io_req->fcp_rsp_code); 1763853e2bd2SBhanu Gollapudi } 1764853e2bd2SBhanu Gollapudi 1765853e2bd2SBhanu Gollapudi /* fetch sense data */ 1766853e2bd2SBhanu Gollapudi rq_data += fcp_rsp_len; 1767853e2bd2SBhanu Gollapudi 1768853e2bd2SBhanu Gollapudi if (fcp_sns_len > SCSI_SENSE_BUFFERSIZE) { 1769853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "Truncating sense buffer\n"); 1770853e2bd2SBhanu Gollapudi fcp_sns_len = SCSI_SENSE_BUFFERSIZE; 1771853e2bd2SBhanu Gollapudi } 1772853e2bd2SBhanu Gollapudi 177316da05b1SAndi Kleen memset(sc_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE); 1774853e2bd2SBhanu Gollapudi if (fcp_sns_len) 1775853e2bd2SBhanu Gollapudi memcpy(sc_cmd->sense_buffer, rq_data, fcp_sns_len); 1776853e2bd2SBhanu Gollapudi 1777853e2bd2SBhanu Gollapudi /* return RQ entries */ 1778853e2bd2SBhanu Gollapudi for (i = 0; i < num_rq; i++) 1779853e2bd2SBhanu Gollapudi bnx2fc_return_rqe(tgt, 1); 1780853e2bd2SBhanu Gollapudi } 1781853e2bd2SBhanu Gollapudi } 1782853e2bd2SBhanu Gollapudi 1783853e2bd2SBhanu Gollapudi /** 1784853e2bd2SBhanu Gollapudi * bnx2fc_queuecommand - Queuecommand function of the scsi template 1785853e2bd2SBhanu Gollapudi * 1786853e2bd2SBhanu Gollapudi * @host: The Scsi_Host the command was issued to 1787853e2bd2SBhanu Gollapudi * @sc_cmd: struct scsi_cmnd to be executed 1788853e2bd2SBhanu Gollapudi * 1789853e2bd2SBhanu Gollapudi * This is the IO strategy routine, called by SCSI-ML 1790853e2bd2SBhanu Gollapudi **/ 1791853e2bd2SBhanu Gollapudi int bnx2fc_queuecommand(struct Scsi_Host *host, 1792853e2bd2SBhanu Gollapudi struct scsi_cmnd *sc_cmd) 1793853e2bd2SBhanu Gollapudi { 1794853e2bd2SBhanu Gollapudi struct fc_lport *lport = shost_priv(host); 1795853e2bd2SBhanu Gollapudi struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device)); 1796853e2bd2SBhanu Gollapudi struct fc_rport_libfc_priv *rp = rport->dd_data; 1797853e2bd2SBhanu Gollapudi struct bnx2fc_rport *tgt; 1798853e2bd2SBhanu Gollapudi struct bnx2fc_cmd *io_req; 1799853e2bd2SBhanu Gollapudi int rc = 0; 1800853e2bd2SBhanu Gollapudi int rval; 1801853e2bd2SBhanu Gollapudi 1802853e2bd2SBhanu Gollapudi rval = fc_remote_port_chkready(rport); 1803853e2bd2SBhanu Gollapudi if (rval) { 1804853e2bd2SBhanu Gollapudi sc_cmd->result = rval; 1805853e2bd2SBhanu Gollapudi sc_cmd->scsi_done(sc_cmd); 1806853e2bd2SBhanu Gollapudi return 0; 1807853e2bd2SBhanu Gollapudi } 1808853e2bd2SBhanu Gollapudi 1809853e2bd2SBhanu Gollapudi if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) { 1810853e2bd2SBhanu Gollapudi rc = SCSI_MLQUEUE_HOST_BUSY; 1811853e2bd2SBhanu Gollapudi goto exit_qcmd; 1812853e2bd2SBhanu Gollapudi } 1813853e2bd2SBhanu Gollapudi 1814853e2bd2SBhanu Gollapudi /* rport and tgt are allocated together, so tgt should be non-NULL */ 1815853e2bd2SBhanu Gollapudi tgt = (struct bnx2fc_rport *)&rp[1]; 1816853e2bd2SBhanu Gollapudi 1817853e2bd2SBhanu Gollapudi if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) { 1818853e2bd2SBhanu Gollapudi /* 1819853e2bd2SBhanu Gollapudi * Session is not offloaded yet. Let SCSI-ml retry 1820853e2bd2SBhanu Gollapudi * the command. 1821853e2bd2SBhanu Gollapudi */ 1822853e2bd2SBhanu Gollapudi rc = SCSI_MLQUEUE_TARGET_BUSY; 1823853e2bd2SBhanu Gollapudi goto exit_qcmd; 1824853e2bd2SBhanu Gollapudi } 1825245a5754SEddie Wai if (tgt->retry_delay_timestamp) { 1826245a5754SEddie Wai if (time_after(jiffies, tgt->retry_delay_timestamp)) { 1827245a5754SEddie Wai tgt->retry_delay_timestamp = 0; 1828245a5754SEddie Wai } else { 1829245a5754SEddie Wai /* If retry_delay timer is active, flow off the ML */ 1830245a5754SEddie Wai rc = SCSI_MLQUEUE_TARGET_BUSY; 1831245a5754SEddie Wai goto exit_qcmd; 1832245a5754SEddie Wai } 1833245a5754SEddie Wai } 1834dc6311ddSMaurizio Lombardi 1835dc6311ddSMaurizio Lombardi spin_lock_bh(&tgt->tgt_lock); 1836dc6311ddSMaurizio Lombardi 1837853e2bd2SBhanu Gollapudi io_req = bnx2fc_cmd_alloc(tgt); 1838853e2bd2SBhanu Gollapudi if (!io_req) { 1839853e2bd2SBhanu Gollapudi rc = SCSI_MLQUEUE_HOST_BUSY; 1840dc6311ddSMaurizio Lombardi goto exit_qcmd_tgtlock; 1841853e2bd2SBhanu Gollapudi } 1842853e2bd2SBhanu Gollapudi io_req->sc_cmd = sc_cmd; 1843853e2bd2SBhanu Gollapudi 1844853e2bd2SBhanu Gollapudi if (bnx2fc_post_io_req(tgt, io_req)) { 1845853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "Unable to post io_req\n"); 1846853e2bd2SBhanu Gollapudi rc = SCSI_MLQUEUE_HOST_BUSY; 1847dc6311ddSMaurizio Lombardi goto exit_qcmd_tgtlock; 1848853e2bd2SBhanu Gollapudi } 1849dc6311ddSMaurizio Lombardi 1850dc6311ddSMaurizio Lombardi exit_qcmd_tgtlock: 1851dc6311ddSMaurizio Lombardi spin_unlock_bh(&tgt->tgt_lock); 1852853e2bd2SBhanu Gollapudi exit_qcmd: 1853853e2bd2SBhanu Gollapudi return rc; 1854853e2bd2SBhanu Gollapudi } 1855853e2bd2SBhanu Gollapudi 1856853e2bd2SBhanu Gollapudi void bnx2fc_process_scsi_cmd_compl(struct bnx2fc_cmd *io_req, 1857853e2bd2SBhanu Gollapudi struct fcoe_task_ctx_entry *task, 1858853e2bd2SBhanu Gollapudi u8 num_rq) 1859853e2bd2SBhanu Gollapudi { 1860853e2bd2SBhanu Gollapudi struct fcoe_fcp_rsp_payload *fcp_rsp; 1861853e2bd2SBhanu Gollapudi struct bnx2fc_rport *tgt = io_req->tgt; 1862853e2bd2SBhanu Gollapudi struct scsi_cmnd *sc_cmd; 1863853e2bd2SBhanu Gollapudi struct Scsi_Host *host; 1864853e2bd2SBhanu Gollapudi 1865853e2bd2SBhanu Gollapudi 1866853e2bd2SBhanu Gollapudi /* scsi_cmd_cmpl is called with tgt lock held */ 1867853e2bd2SBhanu Gollapudi 1868853e2bd2SBhanu Gollapudi if (test_and_set_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags)) { 1869853e2bd2SBhanu Gollapudi /* we will not receive ABTS response for this IO */ 1870853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "Timer context finished processing " 1871853e2bd2SBhanu Gollapudi "this scsi cmd\n"); 1872853e2bd2SBhanu Gollapudi } 1873853e2bd2SBhanu Gollapudi 1874853e2bd2SBhanu Gollapudi /* Cancel the timeout_work, as we received IO completion */ 1875853e2bd2SBhanu Gollapudi if (cancel_delayed_work(&io_req->timeout_work)) 1876853e2bd2SBhanu Gollapudi kref_put(&io_req->refcount, 1877853e2bd2SBhanu Gollapudi bnx2fc_cmd_release); /* drop timer hold */ 1878853e2bd2SBhanu Gollapudi 1879853e2bd2SBhanu Gollapudi sc_cmd = io_req->sc_cmd; 1880853e2bd2SBhanu Gollapudi if (sc_cmd == NULL) { 1881853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "scsi_cmd_compl - sc_cmd is NULL\n"); 1882853e2bd2SBhanu Gollapudi return; 1883853e2bd2SBhanu Gollapudi } 1884853e2bd2SBhanu Gollapudi 1885853e2bd2SBhanu Gollapudi /* Fetch fcp_rsp from task context and perform cmd completion */ 1886853e2bd2SBhanu Gollapudi fcp_rsp = (struct fcoe_fcp_rsp_payload *) 1887619c5cb6SVlad Zolotarov &(task->rxwr_only.union_ctx.comp_info.fcp_rsp.payload); 1888853e2bd2SBhanu Gollapudi 1889853e2bd2SBhanu Gollapudi /* parse fcp_rsp and obtain sense data from RQ if available */ 1890853e2bd2SBhanu Gollapudi bnx2fc_parse_fcp_rsp(io_req, fcp_rsp, num_rq); 1891853e2bd2SBhanu Gollapudi 1892853e2bd2SBhanu Gollapudi host = sc_cmd->device->host; 1893853e2bd2SBhanu Gollapudi if (!sc_cmd->SCp.ptr) { 1894853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "SCp.ptr is NULL\n"); 1895853e2bd2SBhanu Gollapudi return; 1896853e2bd2SBhanu Gollapudi } 1897853e2bd2SBhanu Gollapudi 1898853e2bd2SBhanu Gollapudi if (io_req->on_active_queue) { 1899853e2bd2SBhanu Gollapudi list_del_init(&io_req->link); 1900853e2bd2SBhanu Gollapudi io_req->on_active_queue = 0; 1901853e2bd2SBhanu Gollapudi /* Move IO req to retire queue */ 1902853e2bd2SBhanu Gollapudi list_add_tail(&io_req->link, &tgt->io_retire_queue); 1903853e2bd2SBhanu Gollapudi } else { 1904853e2bd2SBhanu Gollapudi /* This should not happen, but could have been pulled 1905853e2bd2SBhanu Gollapudi * by bnx2fc_flush_active_ios(), or during a race 1906853e2bd2SBhanu Gollapudi * between command abort and (late) completion. 1907853e2bd2SBhanu Gollapudi */ 1908853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "xid not on active_cmd_queue\n"); 1909853e2bd2SBhanu Gollapudi if (io_req->wait_for_comp) 1910853e2bd2SBhanu Gollapudi if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT, 1911853e2bd2SBhanu Gollapudi &io_req->req_flags)) 1912853e2bd2SBhanu Gollapudi complete(&io_req->tm_done); 1913853e2bd2SBhanu Gollapudi } 1914853e2bd2SBhanu Gollapudi 1915853e2bd2SBhanu Gollapudi bnx2fc_unmap_sg_list(io_req); 1916b5a95fe7SBhanu Prakash Gollapudi io_req->sc_cmd = NULL; 1917853e2bd2SBhanu Gollapudi 1918853e2bd2SBhanu Gollapudi switch (io_req->fcp_status) { 1919853e2bd2SBhanu Gollapudi case FC_GOOD: 1920853e2bd2SBhanu Gollapudi if (io_req->cdb_status == 0) { 1921853e2bd2SBhanu Gollapudi /* Good IO completion */ 1922853e2bd2SBhanu Gollapudi sc_cmd->result = DID_OK << 16; 1923853e2bd2SBhanu Gollapudi } else { 1924853e2bd2SBhanu Gollapudi /* Transport status is good, SCSI status not good */ 1925853e2bd2SBhanu Gollapudi BNX2FC_IO_DBG(io_req, "scsi_cmpl: cdb_status = %d" 1926853e2bd2SBhanu Gollapudi " fcp_resid = 0x%x\n", 1927853e2bd2SBhanu Gollapudi io_req->cdb_status, io_req->fcp_resid); 1928853e2bd2SBhanu Gollapudi sc_cmd->result = (DID_OK << 16) | io_req->cdb_status; 1929245a5754SEddie Wai 1930245a5754SEddie Wai if (io_req->cdb_status == SAM_STAT_TASK_SET_FULL || 1931245a5754SEddie Wai io_req->cdb_status == SAM_STAT_BUSY) { 1932245a5754SEddie Wai /* Set the jiffies + retry_delay_timer * 100ms 1933245a5754SEddie Wai for the rport/tgt */ 1934245a5754SEddie Wai tgt->retry_delay_timestamp = jiffies + 1935245a5754SEddie Wai fcp_rsp->retry_delay_timer * HZ / 10; 1936245a5754SEddie Wai } 1937245a5754SEddie Wai 1938853e2bd2SBhanu Gollapudi } 1939853e2bd2SBhanu Gollapudi if (io_req->fcp_resid) 1940853e2bd2SBhanu Gollapudi scsi_set_resid(sc_cmd, io_req->fcp_resid); 1941853e2bd2SBhanu Gollapudi break; 1942853e2bd2SBhanu Gollapudi default: 1943b2a554ffSBhanu Prakash Gollapudi printk(KERN_ERR PFX "scsi_cmd_compl: fcp_status = %d\n", 1944853e2bd2SBhanu Gollapudi io_req->fcp_status); 1945853e2bd2SBhanu Gollapudi break; 1946853e2bd2SBhanu Gollapudi } 1947853e2bd2SBhanu Gollapudi sc_cmd->SCp.ptr = NULL; 1948853e2bd2SBhanu Gollapudi sc_cmd->scsi_done(sc_cmd); 1949853e2bd2SBhanu Gollapudi kref_put(&io_req->refcount, bnx2fc_cmd_release); 1950853e2bd2SBhanu Gollapudi } 1951853e2bd2SBhanu Gollapudi 195274446954SBhanu Prakash Gollapudi int bnx2fc_post_io_req(struct bnx2fc_rport *tgt, 1953853e2bd2SBhanu Gollapudi struct bnx2fc_cmd *io_req) 1954853e2bd2SBhanu Gollapudi { 1955853e2bd2SBhanu Gollapudi struct fcoe_task_ctx_entry *task; 1956853e2bd2SBhanu Gollapudi struct fcoe_task_ctx_entry *task_page; 1957853e2bd2SBhanu Gollapudi struct scsi_cmnd *sc_cmd = io_req->sc_cmd; 1958853e2bd2SBhanu Gollapudi struct fcoe_port *port = tgt->port; 1959aea71a02SBhanu Prakash Gollapudi struct bnx2fc_interface *interface = port->priv; 1960aea71a02SBhanu Prakash Gollapudi struct bnx2fc_hba *hba = interface->hba; 1961853e2bd2SBhanu Gollapudi struct fc_lport *lport = port->lport; 19621bd49b48SVasu Dev struct fc_stats *stats; 1963853e2bd2SBhanu Gollapudi int task_idx, index; 1964853e2bd2SBhanu Gollapudi u16 xid; 1965853e2bd2SBhanu Gollapudi 1966dc6311ddSMaurizio Lombardi /* bnx2fc_post_io_req() is called with the tgt_lock held */ 1967dc6311ddSMaurizio Lombardi 1968853e2bd2SBhanu Gollapudi /* Initialize rest of io_req fields */ 1969853e2bd2SBhanu Gollapudi io_req->cmd_type = BNX2FC_SCSI_CMD; 1970853e2bd2SBhanu Gollapudi io_req->port = port; 1971853e2bd2SBhanu Gollapudi io_req->tgt = tgt; 1972853e2bd2SBhanu Gollapudi io_req->data_xfer_len = scsi_bufflen(sc_cmd); 1973853e2bd2SBhanu Gollapudi sc_cmd->SCp.ptr = (char *)io_req; 1974853e2bd2SBhanu Gollapudi 19751bd49b48SVasu Dev stats = per_cpu_ptr(lport->stats, get_cpu()); 1976853e2bd2SBhanu Gollapudi if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) { 1977853e2bd2SBhanu Gollapudi io_req->io_req_flags = BNX2FC_READ; 1978853e2bd2SBhanu Gollapudi stats->InputRequests++; 1979853e2bd2SBhanu Gollapudi stats->InputBytes += io_req->data_xfer_len; 1980853e2bd2SBhanu Gollapudi } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) { 1981853e2bd2SBhanu Gollapudi io_req->io_req_flags = BNX2FC_WRITE; 1982853e2bd2SBhanu Gollapudi stats->OutputRequests++; 1983853e2bd2SBhanu Gollapudi stats->OutputBytes += io_req->data_xfer_len; 1984853e2bd2SBhanu Gollapudi } else { 1985853e2bd2SBhanu Gollapudi io_req->io_req_flags = 0; 1986853e2bd2SBhanu Gollapudi stats->ControlRequests++; 1987853e2bd2SBhanu Gollapudi } 1988853e2bd2SBhanu Gollapudi put_cpu(); 1989853e2bd2SBhanu Gollapudi 1990853e2bd2SBhanu Gollapudi xid = io_req->xid; 1991853e2bd2SBhanu Gollapudi 1992853e2bd2SBhanu Gollapudi /* Build buffer descriptor list for firmware from sg list */ 1993822f2903SBhanu Prakash Gollapudi if (bnx2fc_build_bd_list_from_sg(io_req)) { 1994822f2903SBhanu Prakash Gollapudi printk(KERN_ERR PFX "BD list creation failed\n"); 1995822f2903SBhanu Prakash Gollapudi kref_put(&io_req->refcount, bnx2fc_cmd_release); 1996822f2903SBhanu Prakash Gollapudi return -EAGAIN; 1997822f2903SBhanu Prakash Gollapudi } 1998853e2bd2SBhanu Gollapudi 1999853e2bd2SBhanu Gollapudi task_idx = xid / BNX2FC_TASKS_PER_PAGE; 2000853e2bd2SBhanu Gollapudi index = xid % BNX2FC_TASKS_PER_PAGE; 2001853e2bd2SBhanu Gollapudi 2002853e2bd2SBhanu Gollapudi /* Initialize task context for this IO request */ 2003853e2bd2SBhanu Gollapudi task_page = (struct fcoe_task_ctx_entry *) hba->task_ctx[task_idx]; 2004853e2bd2SBhanu Gollapudi task = &(task_page[index]); 2005853e2bd2SBhanu Gollapudi bnx2fc_init_task(io_req, task); 2006853e2bd2SBhanu Gollapudi 2007853e2bd2SBhanu Gollapudi if (tgt->flush_in_prog) { 2008853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "Flush in progress..Host Busy\n"); 2009853e2bd2SBhanu Gollapudi kref_put(&io_req->refcount, bnx2fc_cmd_release); 2010853e2bd2SBhanu Gollapudi return -EAGAIN; 2011853e2bd2SBhanu Gollapudi } 2012853e2bd2SBhanu Gollapudi 2013853e2bd2SBhanu Gollapudi if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) { 2014853e2bd2SBhanu Gollapudi printk(KERN_ERR PFX "Session not ready...post_io\n"); 2015853e2bd2SBhanu Gollapudi kref_put(&io_req->refcount, bnx2fc_cmd_release); 2016853e2bd2SBhanu Gollapudi return -EAGAIN; 2017853e2bd2SBhanu Gollapudi } 2018853e2bd2SBhanu Gollapudi 2019853e2bd2SBhanu Gollapudi /* Time IO req */ 2020b252f4c7SBhanu Prakash Gollapudi if (tgt->io_timeout) 2021853e2bd2SBhanu Gollapudi bnx2fc_cmd_timer_set(io_req, BNX2FC_IO_TIMEOUT); 2022853e2bd2SBhanu Gollapudi /* Obtain free SQ entry */ 2023853e2bd2SBhanu Gollapudi bnx2fc_add_2_sq(tgt, xid); 2024853e2bd2SBhanu Gollapudi 2025853e2bd2SBhanu Gollapudi /* Enqueue the io_req to active_cmd_queue */ 2026853e2bd2SBhanu Gollapudi 2027853e2bd2SBhanu Gollapudi io_req->on_active_queue = 1; 2028853e2bd2SBhanu Gollapudi /* move io_req from pending_queue to active_queue */ 2029853e2bd2SBhanu Gollapudi list_add_tail(&io_req->link, &tgt->active_cmd_queue); 2030853e2bd2SBhanu Gollapudi 2031853e2bd2SBhanu Gollapudi /* Ring doorbell */ 2032853e2bd2SBhanu Gollapudi bnx2fc_ring_doorbell(tgt); 2033853e2bd2SBhanu Gollapudi return 0; 2034853e2bd2SBhanu Gollapudi } 2035