0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include "bnx2fc.h"
0016
0017 #define RESERVE_FREE_LIST_INDEX num_possible_cpus()
0018
0019 static int bnx2fc_split_bd(struct bnx2fc_cmd *io_req, u64 addr, int sg_len,
0020 int bd_index);
0021 static int bnx2fc_map_sg(struct bnx2fc_cmd *io_req);
0022 static int bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd *io_req);
0023 static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd *io_req);
0024 static void bnx2fc_free_mp_resc(struct bnx2fc_cmd *io_req);
0025 static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd *io_req,
0026 struct fcoe_fcp_rsp_payload *fcp_rsp,
0027 u8 num_rq, unsigned char *rq_data);
0028
0029 void bnx2fc_cmd_timer_set(struct bnx2fc_cmd *io_req,
0030 unsigned int timer_msec)
0031 {
0032 struct bnx2fc_interface *interface = io_req->port->priv;
0033
0034 if (queue_delayed_work(interface->timer_work_queue,
0035 &io_req->timeout_work,
0036 msecs_to_jiffies(timer_msec)))
0037 kref_get(&io_req->refcount);
0038 }
0039
0040 static void bnx2fc_cmd_timeout(struct work_struct *work)
0041 {
0042 struct bnx2fc_cmd *io_req = container_of(work, struct bnx2fc_cmd,
0043 timeout_work.work);
0044 u8 cmd_type = io_req->cmd_type;
0045 struct bnx2fc_rport *tgt = io_req->tgt;
0046 int rc;
0047
0048 BNX2FC_IO_DBG(io_req, "cmd_timeout, cmd_type = %d,"
0049 "req_flags = %lx\n", cmd_type, io_req->req_flags);
0050
0051 spin_lock_bh(&tgt->tgt_lock);
0052 if (test_and_clear_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags)) {
0053 clear_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags);
0054
0055
0056
0057
0058 spin_unlock_bh(&tgt->tgt_lock);
0059 bnx2fc_send_rrq(io_req);
0060 return;
0061 }
0062 if (test_and_clear_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags)) {
0063 BNX2FC_IO_DBG(io_req, "IO ready for reuse now\n");
0064 goto done;
0065 }
0066
0067 switch (cmd_type) {
0068 case BNX2FC_SCSI_CMD:
0069 if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
0070 &io_req->req_flags)) {
0071
0072 BNX2FC_IO_DBG(io_req, "eh_abort timed out\n");
0073 complete(&io_req->abts_done);
0074 } else if (test_bit(BNX2FC_FLAG_ISSUE_ABTS,
0075 &io_req->req_flags)) {
0076
0077 BNX2FC_IO_DBG(io_req, "ABTS timed out refcnt = %d\n",
0078 kref_read(&io_req->refcount));
0079 if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
0080 &io_req->req_flags))) {
0081
0082
0083
0084
0085 bnx2fc_initiate_cleanup(io_req);
0086 kref_put(&io_req->refcount, bnx2fc_cmd_release);
0087 spin_unlock_bh(&tgt->tgt_lock);
0088
0089 return;
0090 }
0091 } else {
0092
0093 BNX2FC_IO_DBG(io_req, "IO timed out. issue ABTS\n");
0094 if (test_and_set_bit(BNX2FC_FLAG_IO_COMPL,
0095 &io_req->req_flags)) {
0096 BNX2FC_IO_DBG(io_req, "IO completed before "
0097 " timer expiry\n");
0098 goto done;
0099 }
0100
0101 if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
0102 &io_req->req_flags)) {
0103 rc = bnx2fc_initiate_abts(io_req);
0104 if (rc == SUCCESS)
0105 goto done;
0106
0107 kref_put(&io_req->refcount, bnx2fc_cmd_release);
0108 spin_unlock_bh(&tgt->tgt_lock);
0109
0110 return;
0111 } else {
0112 BNX2FC_IO_DBG(io_req, "IO already in "
0113 "ABTS processing\n");
0114 }
0115 }
0116 break;
0117 case BNX2FC_ELS:
0118
0119 if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags)) {
0120 BNX2FC_IO_DBG(io_req, "ABTS for ELS timed out\n");
0121
0122 if (!test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
0123 &io_req->req_flags)) {
0124 kref_put(&io_req->refcount, bnx2fc_cmd_release);
0125 spin_unlock_bh(&tgt->tgt_lock);
0126
0127 return;
0128 }
0129 } else {
0130
0131
0132
0133
0134
0135
0136 BNX2FC_IO_DBG(io_req, "ELS timed out\n");
0137 if (test_and_set_bit(BNX2FC_FLAG_ELS_DONE,
0138 &io_req->req_flags))
0139 goto done;
0140
0141
0142 set_bit(BNX2FC_FLAG_ELS_TIMEOUT, &io_req->req_flags);
0143
0144 if ((io_req->cb_func) && (io_req->cb_arg)) {
0145 io_req->cb_func(io_req->cb_arg);
0146 io_req->cb_arg = NULL;
0147 }
0148 }
0149 break;
0150 default:
0151 printk(KERN_ERR PFX "cmd_timeout: invalid cmd_type %d\n",
0152 cmd_type);
0153 break;
0154 }
0155
0156 done:
0157
0158 kref_put(&io_req->refcount, bnx2fc_cmd_release);
0159 spin_unlock_bh(&tgt->tgt_lock);
0160 }
0161
0162 static void bnx2fc_scsi_done(struct bnx2fc_cmd *io_req, int err_code)
0163 {
0164
0165 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
0166
0167
0168
0169
0170
0171
0172 if (io_req->cmd_type != BNX2FC_SCSI_CMD)
0173 return;
0174
0175 BNX2FC_IO_DBG(io_req, "scsi_done. err_code = 0x%x\n", err_code);
0176 if (test_bit(BNX2FC_FLAG_CMD_LOST, &io_req->req_flags)) {
0177
0178 return;
0179 }
0180
0181 bnx2fc_unmap_sg_list(io_req);
0182 io_req->sc_cmd = NULL;
0183
0184
0185 if (!sc_cmd) {
0186 printk(KERN_ERR PFX "scsi_done - sc_cmd NULL. "
0187 "IO(0x%x) already cleaned up\n",
0188 io_req->xid);
0189 return;
0190 }
0191 if (!sc_cmd->device) {
0192 pr_err(PFX "0x%x: sc_cmd->device is NULL.\n", io_req->xid);
0193 return;
0194 }
0195 if (!sc_cmd->device->host) {
0196 pr_err(PFX "0x%x: sc_cmd->device->host is NULL.\n",
0197 io_req->xid);
0198 return;
0199 }
0200
0201 sc_cmd->result = err_code << 16;
0202
0203 BNX2FC_IO_DBG(io_req, "sc=%p, result=0x%x, retries=%d, allowed=%d\n",
0204 sc_cmd, host_byte(sc_cmd->result), sc_cmd->retries,
0205 sc_cmd->allowed);
0206 scsi_set_resid(sc_cmd, scsi_bufflen(sc_cmd));
0207 bnx2fc_priv(sc_cmd)->io_req = NULL;
0208 scsi_done(sc_cmd);
0209 }
0210
0211 struct bnx2fc_cmd_mgr *bnx2fc_cmd_mgr_alloc(struct bnx2fc_hba *hba)
0212 {
0213 struct bnx2fc_cmd_mgr *cmgr;
0214 struct io_bdt *bdt_info;
0215 struct bnx2fc_cmd *io_req;
0216 size_t len;
0217 u32 mem_size;
0218 u16 xid;
0219 int i;
0220 int num_ios, num_pri_ios;
0221 size_t bd_tbl_sz;
0222 int arr_sz = num_possible_cpus() + 1;
0223 u16 min_xid = BNX2FC_MIN_XID;
0224 u16 max_xid = hba->max_xid;
0225
0226 if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN) {
0227 printk(KERN_ERR PFX "cmd_mgr_alloc: Invalid min_xid 0x%x \
0228 and max_xid 0x%x\n", min_xid, max_xid);
0229 return NULL;
0230 }
0231 BNX2FC_MISC_DBG("min xid 0x%x, max xid 0x%x\n", min_xid, max_xid);
0232
0233 num_ios = max_xid - min_xid + 1;
0234 len = (num_ios * (sizeof(struct bnx2fc_cmd *)));
0235 len += sizeof(struct bnx2fc_cmd_mgr);
0236
0237 cmgr = kzalloc(len, GFP_KERNEL);
0238 if (!cmgr) {
0239 printk(KERN_ERR PFX "failed to alloc cmgr\n");
0240 return NULL;
0241 }
0242
0243 cmgr->hba = hba;
0244 cmgr->free_list = kcalloc(arr_sz, sizeof(*cmgr->free_list),
0245 GFP_KERNEL);
0246 if (!cmgr->free_list) {
0247 printk(KERN_ERR PFX "failed to alloc free_list\n");
0248 goto mem_err;
0249 }
0250
0251 cmgr->free_list_lock = kcalloc(arr_sz, sizeof(*cmgr->free_list_lock),
0252 GFP_KERNEL);
0253 if (!cmgr->free_list_lock) {
0254 printk(KERN_ERR PFX "failed to alloc free_list_lock\n");
0255 kfree(cmgr->free_list);
0256 cmgr->free_list = NULL;
0257 goto mem_err;
0258 }
0259
0260 cmgr->cmds = (struct bnx2fc_cmd **)(cmgr + 1);
0261
0262 for (i = 0; i < arr_sz; i++) {
0263 INIT_LIST_HEAD(&cmgr->free_list[i]);
0264 spin_lock_init(&cmgr->free_list_lock[i]);
0265 }
0266
0267
0268
0269
0270
0271
0272 xid = BNX2FC_MIN_XID;
0273 num_pri_ios = num_ios - hba->elstm_xids;
0274 for (i = 0; i < num_ios; i++) {
0275 io_req = kzalloc(sizeof(*io_req), GFP_KERNEL);
0276
0277 if (!io_req) {
0278 printk(KERN_ERR PFX "failed to alloc io_req\n");
0279 goto mem_err;
0280 }
0281
0282 INIT_LIST_HEAD(&io_req->link);
0283 INIT_DELAYED_WORK(&io_req->timeout_work, bnx2fc_cmd_timeout);
0284
0285 io_req->xid = xid++;
0286 if (i < num_pri_ios)
0287 list_add_tail(&io_req->link,
0288 &cmgr->free_list[io_req->xid %
0289 num_possible_cpus()]);
0290 else
0291 list_add_tail(&io_req->link,
0292 &cmgr->free_list[num_possible_cpus()]);
0293 io_req++;
0294 }
0295
0296
0297 mem_size = num_ios * sizeof(struct io_bdt *);
0298 cmgr->io_bdt_pool = kzalloc(mem_size, GFP_KERNEL);
0299 if (!cmgr->io_bdt_pool) {
0300 printk(KERN_ERR PFX "failed to alloc io_bdt_pool\n");
0301 goto mem_err;
0302 }
0303
0304 mem_size = sizeof(struct io_bdt);
0305 for (i = 0; i < num_ios; i++) {
0306 cmgr->io_bdt_pool[i] = kmalloc(mem_size, GFP_KERNEL);
0307 if (!cmgr->io_bdt_pool[i]) {
0308 printk(KERN_ERR PFX "failed to alloc "
0309 "io_bdt_pool[%d]\n", i);
0310 goto mem_err;
0311 }
0312 }
0313
0314
0315 bd_tbl_sz = BNX2FC_MAX_BDS_PER_CMD * sizeof(struct fcoe_bd_ctx);
0316 for (i = 0; i < num_ios; i++) {
0317 bdt_info = cmgr->io_bdt_pool[i];
0318 bdt_info->bd_tbl = dma_alloc_coherent(&hba->pcidev->dev,
0319 bd_tbl_sz,
0320 &bdt_info->bd_tbl_dma,
0321 GFP_KERNEL);
0322 if (!bdt_info->bd_tbl) {
0323 printk(KERN_ERR PFX "failed to alloc "
0324 "bdt_tbl[%d]\n", i);
0325 goto mem_err;
0326 }
0327 }
0328
0329 return cmgr;
0330
0331 mem_err:
0332 bnx2fc_cmd_mgr_free(cmgr);
0333 return NULL;
0334 }
0335
0336 void bnx2fc_cmd_mgr_free(struct bnx2fc_cmd_mgr *cmgr)
0337 {
0338 struct io_bdt *bdt_info;
0339 struct bnx2fc_hba *hba = cmgr->hba;
0340 size_t bd_tbl_sz;
0341 u16 min_xid = BNX2FC_MIN_XID;
0342 u16 max_xid = hba->max_xid;
0343 int num_ios;
0344 int i;
0345
0346 num_ios = max_xid - min_xid + 1;
0347
0348
0349 if (!cmgr->io_bdt_pool)
0350 goto free_cmd_pool;
0351
0352 bd_tbl_sz = BNX2FC_MAX_BDS_PER_CMD * sizeof(struct fcoe_bd_ctx);
0353 for (i = 0; i < num_ios; i++) {
0354 bdt_info = cmgr->io_bdt_pool[i];
0355 if (bdt_info->bd_tbl) {
0356 dma_free_coherent(&hba->pcidev->dev, bd_tbl_sz,
0357 bdt_info->bd_tbl,
0358 bdt_info->bd_tbl_dma);
0359 bdt_info->bd_tbl = NULL;
0360 }
0361 }
0362
0363
0364 for (i = 0; i < num_ios; i++) {
0365 kfree(cmgr->io_bdt_pool[i]);
0366 cmgr->io_bdt_pool[i] = NULL;
0367 }
0368
0369 kfree(cmgr->io_bdt_pool);
0370 cmgr->io_bdt_pool = NULL;
0371
0372 free_cmd_pool:
0373 kfree(cmgr->free_list_lock);
0374
0375
0376 if (!cmgr->free_list)
0377 goto free_cmgr;
0378
0379 for (i = 0; i < num_possible_cpus() + 1; i++) {
0380 struct bnx2fc_cmd *tmp, *io_req;
0381
0382 list_for_each_entry_safe(io_req, tmp,
0383 &cmgr->free_list[i], link) {
0384 list_del(&io_req->link);
0385 kfree(io_req);
0386 }
0387 }
0388 kfree(cmgr->free_list);
0389 free_cmgr:
0390
0391 kfree(cmgr);
0392 }
0393
0394 struct bnx2fc_cmd *bnx2fc_elstm_alloc(struct bnx2fc_rport *tgt, int type)
0395 {
0396 struct fcoe_port *port = tgt->port;
0397 struct bnx2fc_interface *interface = port->priv;
0398 struct bnx2fc_cmd_mgr *cmd_mgr = interface->hba->cmd_mgr;
0399 struct bnx2fc_cmd *io_req;
0400 struct list_head *listp;
0401 struct io_bdt *bd_tbl;
0402 int index = RESERVE_FREE_LIST_INDEX;
0403 u32 free_sqes;
0404 u32 max_sqes;
0405 u16 xid;
0406
0407 max_sqes = tgt->max_sqes;
0408 switch (type) {
0409 case BNX2FC_TASK_MGMT_CMD:
0410 max_sqes = BNX2FC_TM_MAX_SQES;
0411 break;
0412 case BNX2FC_ELS:
0413 max_sqes = BNX2FC_ELS_MAX_SQES;
0414 break;
0415 default:
0416 break;
0417 }
0418
0419
0420
0421
0422
0423 spin_lock_bh(&cmd_mgr->free_list_lock[index]);
0424 free_sqes = atomic_read(&tgt->free_sqes);
0425 if ((list_empty(&(cmd_mgr->free_list[index]))) ||
0426 (tgt->num_active_ios.counter >= max_sqes) ||
0427 (free_sqes + max_sqes <= BNX2FC_SQ_WQES_MAX)) {
0428 BNX2FC_TGT_DBG(tgt, "No free els_tm cmds available "
0429 "ios(%d):sqes(%d)\n",
0430 tgt->num_active_ios.counter, tgt->max_sqes);
0431 if (list_empty(&(cmd_mgr->free_list[index])))
0432 printk(KERN_ERR PFX "elstm_alloc: list_empty\n");
0433 spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
0434 return NULL;
0435 }
0436
0437 listp = (struct list_head *)
0438 cmd_mgr->free_list[index].next;
0439 list_del_init(listp);
0440 io_req = (struct bnx2fc_cmd *) listp;
0441 xid = io_req->xid;
0442 cmd_mgr->cmds[xid] = io_req;
0443 atomic_inc(&tgt->num_active_ios);
0444 atomic_dec(&tgt->free_sqes);
0445 spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
0446
0447 INIT_LIST_HEAD(&io_req->link);
0448
0449 io_req->port = port;
0450 io_req->cmd_mgr = cmd_mgr;
0451 io_req->req_flags = 0;
0452 io_req->cmd_type = type;
0453
0454
0455
0456 bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid];
0457 bd_tbl->io_req = io_req;
0458
0459
0460 kref_init(&io_req->refcount);
0461 return io_req;
0462 }
0463
0464 struct bnx2fc_cmd *bnx2fc_cmd_alloc(struct bnx2fc_rport *tgt)
0465 {
0466 struct fcoe_port *port = tgt->port;
0467 struct bnx2fc_interface *interface = port->priv;
0468 struct bnx2fc_cmd_mgr *cmd_mgr = interface->hba->cmd_mgr;
0469 struct bnx2fc_cmd *io_req;
0470 struct list_head *listp;
0471 struct io_bdt *bd_tbl;
0472 u32 free_sqes;
0473 u32 max_sqes;
0474 u16 xid;
0475 int index = raw_smp_processor_id();
0476
0477 max_sqes = BNX2FC_SCSI_MAX_SQES;
0478
0479
0480
0481
0482 spin_lock_bh(&cmd_mgr->free_list_lock[index]);
0483 free_sqes = atomic_read(&tgt->free_sqes);
0484 if ((list_empty(&cmd_mgr->free_list[index])) ||
0485 (tgt->num_active_ios.counter >= max_sqes) ||
0486 (free_sqes + max_sqes <= BNX2FC_SQ_WQES_MAX)) {
0487 spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
0488 return NULL;
0489 }
0490
0491 listp = (struct list_head *)
0492 cmd_mgr->free_list[index].next;
0493 list_del_init(listp);
0494 io_req = (struct bnx2fc_cmd *) listp;
0495 xid = io_req->xid;
0496 cmd_mgr->cmds[xid] = io_req;
0497 atomic_inc(&tgt->num_active_ios);
0498 atomic_dec(&tgt->free_sqes);
0499 spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
0500
0501 INIT_LIST_HEAD(&io_req->link);
0502
0503 io_req->port = port;
0504 io_req->cmd_mgr = cmd_mgr;
0505 io_req->req_flags = 0;
0506
0507
0508
0509 bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid];
0510 bd_tbl->io_req = io_req;
0511
0512
0513 kref_init(&io_req->refcount);
0514 return io_req;
0515 }
0516
0517 void bnx2fc_cmd_release(struct kref *ref)
0518 {
0519 struct bnx2fc_cmd *io_req = container_of(ref,
0520 struct bnx2fc_cmd, refcount);
0521 struct bnx2fc_cmd_mgr *cmd_mgr = io_req->cmd_mgr;
0522 int index;
0523
0524 if (io_req->cmd_type == BNX2FC_SCSI_CMD)
0525 index = io_req->xid % num_possible_cpus();
0526 else
0527 index = RESERVE_FREE_LIST_INDEX;
0528
0529
0530 spin_lock_bh(&cmd_mgr->free_list_lock[index]);
0531 if (io_req->cmd_type != BNX2FC_SCSI_CMD)
0532 bnx2fc_free_mp_resc(io_req);
0533 cmd_mgr->cmds[io_req->xid] = NULL;
0534
0535 list_del_init(&io_req->link);
0536
0537 list_add(&io_req->link,
0538 &cmd_mgr->free_list[index]);
0539 atomic_dec(&io_req->tgt->num_active_ios);
0540 spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
0541
0542 }
0543
0544 static void bnx2fc_free_mp_resc(struct bnx2fc_cmd *io_req)
0545 {
0546 struct bnx2fc_mp_req *mp_req = &(io_req->mp_req);
0547 struct bnx2fc_interface *interface = io_req->port->priv;
0548 struct bnx2fc_hba *hba = interface->hba;
0549 size_t sz = sizeof(struct fcoe_bd_ctx);
0550
0551
0552 mp_req->tm_flags = 0;
0553 if (mp_req->mp_req_bd) {
0554 dma_free_coherent(&hba->pcidev->dev, sz,
0555 mp_req->mp_req_bd,
0556 mp_req->mp_req_bd_dma);
0557 mp_req->mp_req_bd = NULL;
0558 }
0559 if (mp_req->mp_resp_bd) {
0560 dma_free_coherent(&hba->pcidev->dev, sz,
0561 mp_req->mp_resp_bd,
0562 mp_req->mp_resp_bd_dma);
0563 mp_req->mp_resp_bd = NULL;
0564 }
0565 if (mp_req->req_buf) {
0566 dma_free_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
0567 mp_req->req_buf,
0568 mp_req->req_buf_dma);
0569 mp_req->req_buf = NULL;
0570 }
0571 if (mp_req->resp_buf) {
0572 dma_free_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
0573 mp_req->resp_buf,
0574 mp_req->resp_buf_dma);
0575 mp_req->resp_buf = NULL;
0576 }
0577 }
0578
0579 int bnx2fc_init_mp_req(struct bnx2fc_cmd *io_req)
0580 {
0581 struct bnx2fc_mp_req *mp_req;
0582 struct fcoe_bd_ctx *mp_req_bd;
0583 struct fcoe_bd_ctx *mp_resp_bd;
0584 struct bnx2fc_interface *interface = io_req->port->priv;
0585 struct bnx2fc_hba *hba = interface->hba;
0586 dma_addr_t addr;
0587 size_t sz;
0588
0589 mp_req = (struct bnx2fc_mp_req *)&(io_req->mp_req);
0590 memset(mp_req, 0, sizeof(struct bnx2fc_mp_req));
0591
0592 if (io_req->cmd_type != BNX2FC_ELS) {
0593 mp_req->req_len = sizeof(struct fcp_cmnd);
0594 io_req->data_xfer_len = mp_req->req_len;
0595 } else
0596 mp_req->req_len = io_req->data_xfer_len;
0597
0598 mp_req->req_buf = dma_alloc_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
0599 &mp_req->req_buf_dma,
0600 GFP_ATOMIC);
0601 if (!mp_req->req_buf) {
0602 printk(KERN_ERR PFX "unable to alloc MP req buffer\n");
0603 bnx2fc_free_mp_resc(io_req);
0604 return FAILED;
0605 }
0606
0607 mp_req->resp_buf = dma_alloc_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
0608 &mp_req->resp_buf_dma,
0609 GFP_ATOMIC);
0610 if (!mp_req->resp_buf) {
0611 printk(KERN_ERR PFX "unable to alloc TM resp buffer\n");
0612 bnx2fc_free_mp_resc(io_req);
0613 return FAILED;
0614 }
0615 memset(mp_req->req_buf, 0, CNIC_PAGE_SIZE);
0616 memset(mp_req->resp_buf, 0, CNIC_PAGE_SIZE);
0617
0618
0619 sz = sizeof(struct fcoe_bd_ctx);
0620 mp_req->mp_req_bd = dma_alloc_coherent(&hba->pcidev->dev, sz,
0621 &mp_req->mp_req_bd_dma,
0622 GFP_ATOMIC);
0623 if (!mp_req->mp_req_bd) {
0624 printk(KERN_ERR PFX "unable to alloc MP req bd\n");
0625 bnx2fc_free_mp_resc(io_req);
0626 return FAILED;
0627 }
0628 mp_req->mp_resp_bd = dma_alloc_coherent(&hba->pcidev->dev, sz,
0629 &mp_req->mp_resp_bd_dma,
0630 GFP_ATOMIC);
0631 if (!mp_req->mp_resp_bd) {
0632 printk(KERN_ERR PFX "unable to alloc MP resp bd\n");
0633 bnx2fc_free_mp_resc(io_req);
0634 return FAILED;
0635 }
0636
0637 addr = mp_req->req_buf_dma;
0638 mp_req_bd = mp_req->mp_req_bd;
0639 mp_req_bd->buf_addr_lo = (u32)addr & 0xffffffff;
0640 mp_req_bd->buf_addr_hi = (u32)((u64)addr >> 32);
0641 mp_req_bd->buf_len = CNIC_PAGE_SIZE;
0642 mp_req_bd->flags = 0;
0643
0644
0645
0646
0647
0648
0649 mp_resp_bd = mp_req->mp_resp_bd;
0650 addr = mp_req->resp_buf_dma;
0651 mp_resp_bd->buf_addr_lo = (u32)addr & 0xffffffff;
0652 mp_resp_bd->buf_addr_hi = (u32)((u64)addr >> 32);
0653 mp_resp_bd->buf_len = CNIC_PAGE_SIZE;
0654 mp_resp_bd->flags = 0;
0655
0656 return SUCCESS;
0657 }
0658
0659 static int bnx2fc_initiate_tmf(struct scsi_cmnd *sc_cmd, u8 tm_flags)
0660 {
0661 struct fc_lport *lport;
0662 struct fc_rport *rport;
0663 struct fc_rport_libfc_priv *rp;
0664 struct fcoe_port *port;
0665 struct bnx2fc_interface *interface;
0666 struct bnx2fc_rport *tgt;
0667 struct bnx2fc_cmd *io_req;
0668 struct bnx2fc_mp_req *tm_req;
0669 struct fcoe_task_ctx_entry *task;
0670 struct fcoe_task_ctx_entry *task_page;
0671 struct Scsi_Host *host = sc_cmd->device->host;
0672 struct fc_frame_header *fc_hdr;
0673 struct fcp_cmnd *fcp_cmnd;
0674 int task_idx, index;
0675 int rc = SUCCESS;
0676 u16 xid;
0677 u32 sid, did;
0678 unsigned long start = jiffies;
0679
0680 lport = shost_priv(host);
0681 rport = starget_to_rport(scsi_target(sc_cmd->device));
0682 port = lport_priv(lport);
0683 interface = port->priv;
0684
0685 if (rport == NULL) {
0686 printk(KERN_ERR PFX "device_reset: rport is NULL\n");
0687 rc = FAILED;
0688 goto tmf_err;
0689 }
0690 rp = rport->dd_data;
0691
0692 rc = fc_block_scsi_eh(sc_cmd);
0693 if (rc)
0694 return rc;
0695
0696 if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
0697 printk(KERN_ERR PFX "device_reset: link is not ready\n");
0698 rc = FAILED;
0699 goto tmf_err;
0700 }
0701
0702 tgt = (struct bnx2fc_rport *)&rp[1];
0703
0704 if (!(test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
0705 printk(KERN_ERR PFX "device_reset: tgt not offloaded\n");
0706 rc = FAILED;
0707 goto tmf_err;
0708 }
0709 retry_tmf:
0710 io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_TASK_MGMT_CMD);
0711 if (!io_req) {
0712 if (time_after(jiffies, start + HZ)) {
0713 printk(KERN_ERR PFX "tmf: Failed TMF");
0714 rc = FAILED;
0715 goto tmf_err;
0716 }
0717 msleep(20);
0718 goto retry_tmf;
0719 }
0720
0721 io_req->sc_cmd = sc_cmd;
0722 io_req->port = port;
0723 io_req->tgt = tgt;
0724
0725 tm_req = (struct bnx2fc_mp_req *)&(io_req->mp_req);
0726
0727 rc = bnx2fc_init_mp_req(io_req);
0728 if (rc == FAILED) {
0729 printk(KERN_ERR PFX "Task mgmt MP request init failed\n");
0730 spin_lock_bh(&tgt->tgt_lock);
0731 kref_put(&io_req->refcount, bnx2fc_cmd_release);
0732 spin_unlock_bh(&tgt->tgt_lock);
0733 goto tmf_err;
0734 }
0735
0736
0737 io_req->io_req_flags = 0;
0738 tm_req->tm_flags = tm_flags;
0739
0740
0741 bnx2fc_build_fcp_cmnd(io_req, (struct fcp_cmnd *)tm_req->req_buf);
0742 fcp_cmnd = (struct fcp_cmnd *)tm_req->req_buf;
0743 memset(fcp_cmnd->fc_cdb, 0, sc_cmd->cmd_len);
0744 fcp_cmnd->fc_dl = 0;
0745
0746
0747 fc_hdr = &(tm_req->req_fc_hdr);
0748 sid = tgt->sid;
0749 did = rport->port_id;
0750 __fc_fill_fc_hdr(fc_hdr, FC_RCTL_DD_UNSOL_CMD, did, sid,
0751 FC_TYPE_FCP, FC_FC_FIRST_SEQ | FC_FC_END_SEQ |
0752 FC_FC_SEQ_INIT, 0);
0753
0754 xid = io_req->xid;
0755
0756 BNX2FC_TGT_DBG(tgt, "Initiate TMF - xid = 0x%x\n", xid);
0757 task_idx = xid/BNX2FC_TASKS_PER_PAGE;
0758 index = xid % BNX2FC_TASKS_PER_PAGE;
0759
0760
0761 task_page = (struct fcoe_task_ctx_entry *)
0762 interface->hba->task_ctx[task_idx];
0763 task = &(task_page[index]);
0764 bnx2fc_init_mp_task(io_req, task);
0765
0766 bnx2fc_priv(sc_cmd)->io_req = io_req;
0767
0768
0769 spin_lock_bh(&tgt->tgt_lock);
0770 bnx2fc_add_2_sq(tgt, xid);
0771
0772
0773 io_req->on_tmf_queue = 1;
0774 list_add_tail(&io_req->link, &tgt->active_tm_queue);
0775
0776 init_completion(&io_req->abts_done);
0777 io_req->wait_for_abts_comp = 1;
0778
0779
0780 bnx2fc_ring_doorbell(tgt);
0781 spin_unlock_bh(&tgt->tgt_lock);
0782
0783 rc = wait_for_completion_timeout(&io_req->abts_done,
0784 interface->tm_timeout * HZ);
0785 spin_lock_bh(&tgt->tgt_lock);
0786
0787 io_req->wait_for_abts_comp = 0;
0788 if (!(test_bit(BNX2FC_FLAG_TM_COMPL, &io_req->req_flags))) {
0789 set_bit(BNX2FC_FLAG_TM_TIMEOUT, &io_req->req_flags);
0790 if (io_req->on_tmf_queue) {
0791 list_del_init(&io_req->link);
0792 io_req->on_tmf_queue = 0;
0793 }
0794 io_req->wait_for_cleanup_comp = 1;
0795 init_completion(&io_req->cleanup_done);
0796 bnx2fc_initiate_cleanup(io_req);
0797 spin_unlock_bh(&tgt->tgt_lock);
0798 rc = wait_for_completion_timeout(&io_req->cleanup_done,
0799 BNX2FC_FW_TIMEOUT);
0800 spin_lock_bh(&tgt->tgt_lock);
0801 io_req->wait_for_cleanup_comp = 0;
0802 if (!rc)
0803 kref_put(&io_req->refcount, bnx2fc_cmd_release);
0804 }
0805
0806 spin_unlock_bh(&tgt->tgt_lock);
0807
0808 if (!rc) {
0809 BNX2FC_TGT_DBG(tgt, "task mgmt command failed...\n");
0810 rc = FAILED;
0811 } else {
0812 BNX2FC_TGT_DBG(tgt, "task mgmt command success...\n");
0813 rc = SUCCESS;
0814 }
0815 tmf_err:
0816 return rc;
0817 }
0818
0819 int bnx2fc_initiate_abts(struct bnx2fc_cmd *io_req)
0820 {
0821 struct fc_lport *lport;
0822 struct bnx2fc_rport *tgt = io_req->tgt;
0823 struct fc_rport *rport = tgt->rport;
0824 struct fc_rport_priv *rdata = tgt->rdata;
0825 struct bnx2fc_interface *interface;
0826 struct fcoe_port *port;
0827 struct bnx2fc_cmd *abts_io_req;
0828 struct fcoe_task_ctx_entry *task;
0829 struct fcoe_task_ctx_entry *task_page;
0830 struct fc_frame_header *fc_hdr;
0831 struct bnx2fc_mp_req *abts_req;
0832 int task_idx, index;
0833 u32 sid, did;
0834 u16 xid;
0835 int rc = SUCCESS;
0836 u32 r_a_tov = rdata->r_a_tov;
0837
0838
0839 BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_abts\n");
0840
0841 port = io_req->port;
0842 interface = port->priv;
0843 lport = port->lport;
0844
0845 if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
0846 printk(KERN_ERR PFX "initiate_abts: tgt not offloaded\n");
0847 rc = FAILED;
0848 goto abts_err;
0849 }
0850
0851 if (rport == NULL) {
0852 printk(KERN_ERR PFX "initiate_abts: rport is NULL\n");
0853 rc = FAILED;
0854 goto abts_err;
0855 }
0856
0857 if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
0858 printk(KERN_ERR PFX "initiate_abts: link is not ready\n");
0859 rc = FAILED;
0860 goto abts_err;
0861 }
0862
0863 abts_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_ABTS);
0864 if (!abts_io_req) {
0865 printk(KERN_ERR PFX "abts: couldn't allocate cmd\n");
0866 rc = FAILED;
0867 goto abts_err;
0868 }
0869
0870
0871 abts_io_req->sc_cmd = NULL;
0872 abts_io_req->port = port;
0873 abts_io_req->tgt = tgt;
0874 abts_io_req->data_xfer_len = 0;
0875
0876 abts_req = (struct bnx2fc_mp_req *)&(abts_io_req->mp_req);
0877 memset(abts_req, 0, sizeof(struct bnx2fc_mp_req));
0878
0879
0880 fc_hdr = &(abts_req->req_fc_hdr);
0881
0882
0883 fc_hdr->fh_ox_id = htons(io_req->xid);
0884 fc_hdr->fh_rx_id = htons(io_req->task->rxwr_txrd.var_ctx.rx_id);
0885
0886 sid = tgt->sid;
0887 did = rport->port_id;
0888
0889 __fc_fill_fc_hdr(fc_hdr, FC_RCTL_BA_ABTS, did, sid,
0890 FC_TYPE_BLS, FC_FC_FIRST_SEQ | FC_FC_END_SEQ |
0891 FC_FC_SEQ_INIT, 0);
0892
0893 xid = abts_io_req->xid;
0894 BNX2FC_IO_DBG(abts_io_req, "ABTS io_req\n");
0895 task_idx = xid/BNX2FC_TASKS_PER_PAGE;
0896 index = xid % BNX2FC_TASKS_PER_PAGE;
0897
0898
0899 task_page = (struct fcoe_task_ctx_entry *)
0900 interface->hba->task_ctx[task_idx];
0901 task = &(task_page[index]);
0902 bnx2fc_init_mp_task(abts_io_req, task);
0903
0904
0905
0906
0907
0908
0909
0910
0911
0912
0913
0914
0915
0916 bnx2fc_cmd_timer_set(io_req, 2 * r_a_tov);
0917
0918
0919 bnx2fc_add_2_sq(tgt, xid);
0920
0921
0922 bnx2fc_ring_doorbell(tgt);
0923
0924 abts_err:
0925 return rc;
0926 }
0927
0928 int bnx2fc_initiate_seq_cleanup(struct bnx2fc_cmd *orig_io_req, u32 offset,
0929 enum fc_rctl r_ctl)
0930 {
0931 struct bnx2fc_rport *tgt = orig_io_req->tgt;
0932 struct bnx2fc_interface *interface;
0933 struct fcoe_port *port;
0934 struct bnx2fc_cmd *seq_clnp_req;
0935 struct fcoe_task_ctx_entry *task;
0936 struct fcoe_task_ctx_entry *task_page;
0937 struct bnx2fc_els_cb_arg *cb_arg = NULL;
0938 int task_idx, index;
0939 u16 xid;
0940 int rc = 0;
0941
0942 BNX2FC_IO_DBG(orig_io_req, "bnx2fc_initiate_seq_cleanup xid = 0x%x\n",
0943 orig_io_req->xid);
0944 kref_get(&orig_io_req->refcount);
0945
0946 port = orig_io_req->port;
0947 interface = port->priv;
0948
0949 cb_arg = kzalloc(sizeof(struct bnx2fc_els_cb_arg), GFP_ATOMIC);
0950 if (!cb_arg) {
0951 printk(KERN_ERR PFX "Unable to alloc cb_arg for seq clnup\n");
0952 rc = -ENOMEM;
0953 goto cleanup_err;
0954 }
0955
0956 seq_clnp_req = bnx2fc_elstm_alloc(tgt, BNX2FC_SEQ_CLEANUP);
0957 if (!seq_clnp_req) {
0958 printk(KERN_ERR PFX "cleanup: couldn't allocate cmd\n");
0959 rc = -ENOMEM;
0960 kfree(cb_arg);
0961 goto cleanup_err;
0962 }
0963
0964 seq_clnp_req->sc_cmd = NULL;
0965 seq_clnp_req->port = port;
0966 seq_clnp_req->tgt = tgt;
0967 seq_clnp_req->data_xfer_len = 0;
0968
0969 xid = seq_clnp_req->xid;
0970
0971 task_idx = xid/BNX2FC_TASKS_PER_PAGE;
0972 index = xid % BNX2FC_TASKS_PER_PAGE;
0973
0974
0975 task_page = (struct fcoe_task_ctx_entry *)
0976 interface->hba->task_ctx[task_idx];
0977 task = &(task_page[index]);
0978 cb_arg->aborted_io_req = orig_io_req;
0979 cb_arg->io_req = seq_clnp_req;
0980 cb_arg->r_ctl = r_ctl;
0981 cb_arg->offset = offset;
0982 seq_clnp_req->cb_arg = cb_arg;
0983
0984 printk(KERN_ERR PFX "call init_seq_cleanup_task\n");
0985 bnx2fc_init_seq_cleanup_task(seq_clnp_req, task, orig_io_req, offset);
0986
0987
0988 bnx2fc_add_2_sq(tgt, xid);
0989
0990
0991 bnx2fc_ring_doorbell(tgt);
0992 cleanup_err:
0993 return rc;
0994 }
0995
0996 int bnx2fc_initiate_cleanup(struct bnx2fc_cmd *io_req)
0997 {
0998 struct bnx2fc_rport *tgt = io_req->tgt;
0999 struct bnx2fc_interface *interface;
1000 struct fcoe_port *port;
1001 struct bnx2fc_cmd *cleanup_io_req;
1002 struct fcoe_task_ctx_entry *task;
1003 struct fcoe_task_ctx_entry *task_page;
1004 int task_idx, index;
1005 u16 xid, orig_xid;
1006 int rc = 0;
1007
1008
1009 BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_cleanup\n");
1010
1011 port = io_req->port;
1012 interface = port->priv;
1013
1014 cleanup_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_CLEANUP);
1015 if (!cleanup_io_req) {
1016 printk(KERN_ERR PFX "cleanup: couldn't allocate cmd\n");
1017 rc = -1;
1018 goto cleanup_err;
1019 }
1020
1021
1022 cleanup_io_req->sc_cmd = NULL;
1023 cleanup_io_req->port = port;
1024 cleanup_io_req->tgt = tgt;
1025 cleanup_io_req->data_xfer_len = 0;
1026
1027 xid = cleanup_io_req->xid;
1028
1029 task_idx = xid/BNX2FC_TASKS_PER_PAGE;
1030 index = xid % BNX2FC_TASKS_PER_PAGE;
1031
1032
1033 task_page = (struct fcoe_task_ctx_entry *)
1034 interface->hba->task_ctx[task_idx];
1035 task = &(task_page[index]);
1036 orig_xid = io_req->xid;
1037
1038 BNX2FC_IO_DBG(io_req, "CLEANUP io_req xid = 0x%x\n", xid);
1039
1040 bnx2fc_init_cleanup_task(cleanup_io_req, task, orig_xid);
1041
1042
1043 bnx2fc_add_2_sq(tgt, xid);
1044
1045
1046 set_bit(BNX2FC_FLAG_ISSUE_CLEANUP_REQ, &io_req->req_flags);
1047
1048
1049 bnx2fc_ring_doorbell(tgt);
1050
1051 cleanup_err:
1052 return rc;
1053 }
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063 int bnx2fc_eh_target_reset(struct scsi_cmnd *sc_cmd)
1064 {
1065 return bnx2fc_initiate_tmf(sc_cmd, FCP_TMF_TGT_RESET);
1066 }
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076 int bnx2fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
1077 {
1078 return bnx2fc_initiate_tmf(sc_cmd, FCP_TMF_LUN_RESET);
1079 }
1080
1081 static int bnx2fc_abts_cleanup(struct bnx2fc_cmd *io_req)
1082 __must_hold(&tgt->tgt_lock)
1083 {
1084 struct bnx2fc_rport *tgt = io_req->tgt;
1085 unsigned int time_left;
1086
1087 init_completion(&io_req->cleanup_done);
1088 io_req->wait_for_cleanup_comp = 1;
1089 bnx2fc_initiate_cleanup(io_req);
1090
1091 spin_unlock_bh(&tgt->tgt_lock);
1092
1093
1094
1095
1096
1097 time_left = wait_for_completion_timeout(&io_req->cleanup_done,
1098 BNX2FC_FW_TIMEOUT);
1099 if (!time_left) {
1100 BNX2FC_IO_DBG(io_req, "%s(): Wait for cleanup timed out.\n",
1101 __func__);
1102
1103
1104
1105
1106
1107 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1108 }
1109
1110 spin_lock_bh(&tgt->tgt_lock);
1111 io_req->wait_for_cleanup_comp = 0;
1112 return SUCCESS;
1113 }
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123 int bnx2fc_eh_abort(struct scsi_cmnd *sc_cmd)
1124 {
1125 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1126 struct fc_rport_libfc_priv *rp = rport->dd_data;
1127 struct bnx2fc_cmd *io_req;
1128 struct fc_lport *lport;
1129 struct bnx2fc_rport *tgt;
1130 int rc;
1131 unsigned int time_left;
1132
1133 rc = fc_block_scsi_eh(sc_cmd);
1134 if (rc)
1135 return rc;
1136
1137 lport = shost_priv(sc_cmd->device->host);
1138 if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
1139 printk(KERN_ERR PFX "eh_abort: link not ready\n");
1140 return FAILED;
1141 }
1142
1143 tgt = (struct bnx2fc_rport *)&rp[1];
1144
1145 BNX2FC_TGT_DBG(tgt, "Entered bnx2fc_eh_abort\n");
1146
1147 spin_lock_bh(&tgt->tgt_lock);
1148 io_req = bnx2fc_priv(sc_cmd)->io_req;
1149 if (!io_req) {
1150
1151 printk(KERN_ERR PFX "eh_abort: io_req is NULL\n");
1152 spin_unlock_bh(&tgt->tgt_lock);
1153 return SUCCESS;
1154 }
1155 BNX2FC_IO_DBG(io_req, "eh_abort - refcnt = %d\n",
1156 kref_read(&io_req->refcount));
1157
1158
1159 kref_get(&io_req->refcount);
1160
1161 BUG_ON(tgt != io_req->tgt);
1162
1163
1164
1165
1166
1167
1168
1169 if (tgt->flush_in_prog) {
1170 printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
1171 "flush in progress\n", io_req->xid);
1172 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1173 spin_unlock_bh(&tgt->tgt_lock);
1174 return SUCCESS;
1175 }
1176
1177 if (io_req->on_active_queue == 0) {
1178 printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
1179 "not on active_q\n", io_req->xid);
1180
1181
1182
1183
1184 spin_unlock_bh(&tgt->tgt_lock);
1185 return FAILED;
1186 }
1187
1188
1189
1190
1191
1192
1193
1194
1195 list_del_init(&io_req->link);
1196 io_req->on_active_queue = 0;
1197
1198 list_add_tail(&io_req->link, &tgt->io_retire_queue);
1199
1200 init_completion(&io_req->abts_done);
1201 init_completion(&io_req->cleanup_done);
1202
1203 if (test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags)) {
1204 printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
1205 "already in abts processing\n", io_req->xid);
1206 if (cancel_delayed_work(&io_req->timeout_work))
1207 kref_put(&io_req->refcount,
1208 bnx2fc_cmd_release);
1209
1210
1211
1212
1213
1214 bnx2fc_abts_cleanup(io_req);
1215
1216
1217
1218
1219
1220 set_bit(BNX2FC_FLAG_IO_CLEANUP, &io_req->req_flags);
1221 rc = FAILED;
1222 goto done;
1223 }
1224
1225
1226 if (cancel_delayed_work(&io_req->timeout_work))
1227 kref_put(&io_req->refcount,
1228 bnx2fc_cmd_release);
1229 set_bit(BNX2FC_FLAG_EH_ABORT, &io_req->req_flags);
1230 io_req->wait_for_abts_comp = 1;
1231 rc = bnx2fc_initiate_abts(io_req);
1232 if (rc == FAILED) {
1233 io_req->wait_for_cleanup_comp = 1;
1234 bnx2fc_initiate_cleanup(io_req);
1235 spin_unlock_bh(&tgt->tgt_lock);
1236 wait_for_completion(&io_req->cleanup_done);
1237 spin_lock_bh(&tgt->tgt_lock);
1238 io_req->wait_for_cleanup_comp = 0;
1239 goto done;
1240 }
1241 spin_unlock_bh(&tgt->tgt_lock);
1242
1243
1244 time_left = wait_for_completion_timeout(&io_req->abts_done,
1245 msecs_to_jiffies(2 * rp->r_a_tov + 1));
1246 if (time_left)
1247 BNX2FC_IO_DBG(io_req,
1248 "Timed out in eh_abort waiting for abts_done");
1249
1250 spin_lock_bh(&tgt->tgt_lock);
1251 io_req->wait_for_abts_comp = 0;
1252 if (test_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags)) {
1253 BNX2FC_IO_DBG(io_req, "IO completed in a different context\n");
1254 rc = SUCCESS;
1255 } else if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
1256 &io_req->req_flags))) {
1257
1258 printk(KERN_ERR PFX "abort failed, xid = 0x%x\n",
1259 io_req->xid);
1260
1261
1262
1263
1264 rc = bnx2fc_abts_cleanup(io_req);
1265 goto done;
1266 } else {
1267
1268
1269
1270
1271
1272 BNX2FC_IO_DBG(io_req, "abort succeeded\n");
1273 rc = SUCCESS;
1274 bnx2fc_scsi_done(io_req, DID_ABORT);
1275 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1276 }
1277 done:
1278
1279 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1280 spin_unlock_bh(&tgt->tgt_lock);
1281 return rc;
1282 }
1283
1284 void bnx2fc_process_seq_cleanup_compl(struct bnx2fc_cmd *seq_clnp_req,
1285 struct fcoe_task_ctx_entry *task,
1286 u8 rx_state)
1287 {
1288 struct bnx2fc_els_cb_arg *cb_arg = seq_clnp_req->cb_arg;
1289 struct bnx2fc_cmd *orig_io_req = cb_arg->aborted_io_req;
1290 u32 offset = cb_arg->offset;
1291 enum fc_rctl r_ctl = cb_arg->r_ctl;
1292 int rc = 0;
1293 struct bnx2fc_rport *tgt = orig_io_req->tgt;
1294
1295 BNX2FC_IO_DBG(orig_io_req, "Entered process_cleanup_compl xid = 0x%x"
1296 "cmd_type = %d\n",
1297 seq_clnp_req->xid, seq_clnp_req->cmd_type);
1298
1299 if (rx_state == FCOE_TASK_RX_STATE_IGNORED_SEQUENCE_CLEANUP) {
1300 printk(KERN_ERR PFX "seq cleanup ignored - xid = 0x%x\n",
1301 seq_clnp_req->xid);
1302 goto free_cb_arg;
1303 }
1304
1305 spin_unlock_bh(&tgt->tgt_lock);
1306 rc = bnx2fc_send_srr(orig_io_req, offset, r_ctl);
1307 spin_lock_bh(&tgt->tgt_lock);
1308
1309 if (rc)
1310 printk(KERN_ERR PFX "clnup_compl: Unable to send SRR"
1311 " IO will abort\n");
1312 seq_clnp_req->cb_arg = NULL;
1313 kref_put(&orig_io_req->refcount, bnx2fc_cmd_release);
1314 free_cb_arg:
1315 kfree(cb_arg);
1316 return;
1317 }
1318
1319 void bnx2fc_process_cleanup_compl(struct bnx2fc_cmd *io_req,
1320 struct fcoe_task_ctx_entry *task,
1321 u8 num_rq)
1322 {
1323 BNX2FC_IO_DBG(io_req, "Entered process_cleanup_compl "
1324 "refcnt = %d, cmd_type = %d\n",
1325 kref_read(&io_req->refcount), io_req->cmd_type);
1326
1327
1328
1329
1330 if (!test_and_clear_bit(BNX2FC_FLAG_ISSUE_CLEANUP_REQ,
1331 &io_req->req_flags))
1332 return;
1333
1334
1335
1336
1337
1338 if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags) &&
1339 !test_bit(BNX2FC_FLAG_ABTS_DONE, &io_req->req_flags)) {
1340 set_bit(BNX2FC_FLAG_ABTS_DONE, &io_req->req_flags);
1341 if (io_req->wait_for_abts_comp)
1342 complete(&io_req->abts_done);
1343 }
1344
1345 bnx2fc_scsi_done(io_req, DID_ERROR);
1346 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1347 if (io_req->wait_for_cleanup_comp)
1348 complete(&io_req->cleanup_done);
1349 }
1350
1351 void bnx2fc_process_abts_compl(struct bnx2fc_cmd *io_req,
1352 struct fcoe_task_ctx_entry *task,
1353 u8 num_rq)
1354 {
1355 u32 r_ctl;
1356 u32 r_a_tov = FC_DEF_R_A_TOV;
1357 u8 issue_rrq = 0;
1358 struct bnx2fc_rport *tgt = io_req->tgt;
1359
1360 BNX2FC_IO_DBG(io_req, "Entered process_abts_compl xid = 0x%x"
1361 "refcnt = %d, cmd_type = %d\n",
1362 io_req->xid,
1363 kref_read(&io_req->refcount), io_req->cmd_type);
1364
1365 if (test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
1366 &io_req->req_flags)) {
1367 BNX2FC_IO_DBG(io_req, "Timer context finished processing"
1368 " this io\n");
1369 return;
1370 }
1371
1372
1373
1374
1375
1376 if (test_bit(BNX2FC_FLAG_ISSUE_CLEANUP_REQ, &io_req->req_flags)) {
1377 clear_bit(BNX2FC_FLAG_ISSUE_CLEANUP_REQ, &io_req->req_flags);
1378 if (io_req->wait_for_cleanup_comp)
1379 complete(&io_req->cleanup_done);
1380 }
1381
1382
1383 if (test_and_set_bit(BNX2FC_FLAG_IO_CLEANUP,
1384 &io_req->req_flags))
1385 goto io_compl;
1386
1387
1388
1389
1390
1391
1392
1393 if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags))
1394 if (cancel_delayed_work(&io_req->timeout_work))
1395 kref_put(&io_req->refcount,
1396 bnx2fc_cmd_release);
1397
1398 r_ctl = (u8)task->rxwr_only.union_ctx.comp_info.abts_rsp.r_ctl;
1399
1400 switch (r_ctl) {
1401 case FC_RCTL_BA_ACC:
1402
1403
1404
1405
1406 BNX2FC_IO_DBG(io_req, "ABTS response - ACC Send RRQ\n");
1407 issue_rrq = 1;
1408 break;
1409
1410 case FC_RCTL_BA_RJT:
1411 BNX2FC_IO_DBG(io_req, "ABTS response - RJT\n");
1412 break;
1413 default:
1414 printk(KERN_ERR PFX "Unknown ABTS response\n");
1415 break;
1416 }
1417
1418 if (issue_rrq) {
1419 BNX2FC_IO_DBG(io_req, "Issue RRQ after R_A_TOV\n");
1420 set_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags);
1421 }
1422 set_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags);
1423 bnx2fc_cmd_timer_set(io_req, r_a_tov);
1424
1425 io_compl:
1426 if (io_req->wait_for_abts_comp) {
1427 if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
1428 &io_req->req_flags))
1429 complete(&io_req->abts_done);
1430 } else {
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440 if (io_req->on_active_queue) {
1441 list_del_init(&io_req->link);
1442 io_req->on_active_queue = 0;
1443
1444 list_add_tail(&io_req->link, &tgt->io_retire_queue);
1445 }
1446 bnx2fc_scsi_done(io_req, DID_ERROR);
1447 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1448 }
1449 }
1450
1451 static void bnx2fc_lun_reset_cmpl(struct bnx2fc_cmd *io_req)
1452 {
1453 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1454 struct bnx2fc_rport *tgt = io_req->tgt;
1455 struct bnx2fc_cmd *cmd, *tmp;
1456 u64 tm_lun = sc_cmd->device->lun;
1457 u64 lun;
1458 int rc = 0;
1459
1460
1461 BNX2FC_IO_DBG(io_req, "Entered bnx2fc_lun_reset_cmpl\n");
1462
1463
1464
1465
1466 list_for_each_entry_safe(cmd, tmp, &tgt->active_cmd_queue, link) {
1467 BNX2FC_TGT_DBG(tgt, "LUN RST cmpl: scan for pending IOs\n");
1468 lun = cmd->sc_cmd->device->lun;
1469 if (lun == tm_lun) {
1470
1471 if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
1472 &cmd->req_flags)) {
1473
1474 if (cancel_delayed_work(&io_req->timeout_work))
1475 kref_put(&io_req->refcount,
1476 bnx2fc_cmd_release);
1477
1478 rc = bnx2fc_initiate_abts(cmd);
1479
1480 WARN_ON(rc != SUCCESS);
1481 } else
1482 printk(KERN_ERR PFX "lun_rst: abts already in"
1483 " progress for this IO 0x%x\n",
1484 cmd->xid);
1485 }
1486 }
1487 }
1488
1489 static void bnx2fc_tgt_reset_cmpl(struct bnx2fc_cmd *io_req)
1490 {
1491 struct bnx2fc_rport *tgt = io_req->tgt;
1492 struct bnx2fc_cmd *cmd, *tmp;
1493 int rc = 0;
1494
1495
1496 BNX2FC_IO_DBG(io_req, "Entered bnx2fc_tgt_reset_cmpl\n");
1497
1498
1499
1500
1501 list_for_each_entry_safe(cmd, tmp, &tgt->active_cmd_queue, link) {
1502 BNX2FC_TGT_DBG(tgt, "TGT RST cmpl: scan for pending IOs\n");
1503
1504 if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
1505 &cmd->req_flags)) {
1506
1507 if (cancel_delayed_work(&io_req->timeout_work))
1508 kref_put(&io_req->refcount,
1509 bnx2fc_cmd_release);
1510 rc = bnx2fc_initiate_abts(cmd);
1511
1512 WARN_ON(rc != SUCCESS);
1513
1514 } else
1515 printk(KERN_ERR PFX "tgt_rst: abts already in progress"
1516 " for this IO 0x%x\n", cmd->xid);
1517 }
1518 }
1519
1520 void bnx2fc_process_tm_compl(struct bnx2fc_cmd *io_req,
1521 struct fcoe_task_ctx_entry *task, u8 num_rq,
1522 unsigned char *rq_data)
1523 {
1524 struct bnx2fc_mp_req *tm_req;
1525 struct fc_frame_header *fc_hdr;
1526 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1527 u64 *hdr;
1528 u64 *temp_hdr;
1529 void *rsp_buf;
1530
1531
1532 BNX2FC_IO_DBG(io_req, "Entered process_tm_compl\n");
1533
1534 if (!(test_bit(BNX2FC_FLAG_TM_TIMEOUT, &io_req->req_flags)))
1535 set_bit(BNX2FC_FLAG_TM_COMPL, &io_req->req_flags);
1536 else {
1537
1538
1539
1540
1541 return;
1542 }
1543
1544 tm_req = &(io_req->mp_req);
1545 fc_hdr = &(tm_req->resp_fc_hdr);
1546 hdr = (u64 *)fc_hdr;
1547 temp_hdr = (u64 *)
1548 &task->rxwr_only.union_ctx.comp_info.mp_rsp.fc_hdr;
1549 hdr[0] = cpu_to_be64(temp_hdr[0]);
1550 hdr[1] = cpu_to_be64(temp_hdr[1]);
1551 hdr[2] = cpu_to_be64(temp_hdr[2]);
1552
1553 tm_req->resp_len =
1554 task->rxwr_only.union_ctx.comp_info.mp_rsp.mp_payload_len;
1555
1556 rsp_buf = tm_req->resp_buf;
1557
1558 if (fc_hdr->fh_r_ctl == FC_RCTL_DD_CMD_STATUS) {
1559 bnx2fc_parse_fcp_rsp(io_req,
1560 (struct fcoe_fcp_rsp_payload *)
1561 rsp_buf, num_rq, rq_data);
1562 if (io_req->fcp_rsp_code == 0) {
1563
1564 if (tm_req->tm_flags & FCP_TMF_LUN_RESET)
1565 bnx2fc_lun_reset_cmpl(io_req);
1566 else if (tm_req->tm_flags & FCP_TMF_TGT_RESET)
1567 bnx2fc_tgt_reset_cmpl(io_req);
1568 }
1569 } else {
1570 printk(KERN_ERR PFX "tmf's fc_hdr r_ctl = 0x%x\n",
1571 fc_hdr->fh_r_ctl);
1572 }
1573 if (!bnx2fc_priv(sc_cmd)->io_req) {
1574 printk(KERN_ERR PFX "tm_compl: io_req is NULL\n");
1575 return;
1576 }
1577 switch (io_req->fcp_status) {
1578 case FC_GOOD:
1579 if (io_req->cdb_status == 0) {
1580
1581 sc_cmd->result = DID_OK << 16;
1582 } else {
1583
1584 sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
1585 }
1586 if (io_req->fcp_resid)
1587 scsi_set_resid(sc_cmd, io_req->fcp_resid);
1588 break;
1589
1590 default:
1591 BNX2FC_IO_DBG(io_req, "process_tm_compl: fcp_status = %d\n",
1592 io_req->fcp_status);
1593 break;
1594 }
1595
1596 sc_cmd = io_req->sc_cmd;
1597 io_req->sc_cmd = NULL;
1598
1599
1600 if (io_req->on_tmf_queue) {
1601
1602 list_del_init(&io_req->link);
1603 io_req->on_tmf_queue = 0;
1604 } else {
1605
1606 printk(KERN_ERR PFX "Command not on active_cmd_queue!\n");
1607 return;
1608 }
1609
1610 bnx2fc_priv(sc_cmd)->io_req = NULL;
1611 scsi_done(sc_cmd);
1612
1613 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1614 if (io_req->wait_for_abts_comp) {
1615 BNX2FC_IO_DBG(io_req, "tm_compl - wake up the waiter\n");
1616 complete(&io_req->abts_done);
1617 }
1618 }
1619
1620 static int bnx2fc_split_bd(struct bnx2fc_cmd *io_req, u64 addr, int sg_len,
1621 int bd_index)
1622 {
1623 struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
1624 int frag_size, sg_frags;
1625
1626 sg_frags = 0;
1627 while (sg_len) {
1628 if (sg_len >= BNX2FC_BD_SPLIT_SZ)
1629 frag_size = BNX2FC_BD_SPLIT_SZ;
1630 else
1631 frag_size = sg_len;
1632 bd[bd_index + sg_frags].buf_addr_lo = addr & 0xffffffff;
1633 bd[bd_index + sg_frags].buf_addr_hi = addr >> 32;
1634 bd[bd_index + sg_frags].buf_len = (u16)frag_size;
1635 bd[bd_index + sg_frags].flags = 0;
1636
1637 addr += (u64) frag_size;
1638 sg_frags++;
1639 sg_len -= frag_size;
1640 }
1641 return sg_frags;
1642
1643 }
1644
1645 static int bnx2fc_map_sg(struct bnx2fc_cmd *io_req)
1646 {
1647 struct bnx2fc_interface *interface = io_req->port->priv;
1648 struct bnx2fc_hba *hba = interface->hba;
1649 struct scsi_cmnd *sc = io_req->sc_cmd;
1650 struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
1651 struct scatterlist *sg;
1652 int byte_count = 0;
1653 int sg_count = 0;
1654 int bd_count = 0;
1655 int sg_frags;
1656 unsigned int sg_len;
1657 u64 addr;
1658 int i;
1659
1660 WARN_ON(scsi_sg_count(sc) > BNX2FC_MAX_BDS_PER_CMD);
1661
1662
1663
1664
1665 sg_count = dma_map_sg(&hba->pcidev->dev, scsi_sglist(sc),
1666 scsi_sg_count(sc), sc->sc_data_direction);
1667 scsi_for_each_sg(sc, sg, sg_count, i) {
1668 sg_len = sg_dma_len(sg);
1669 addr = sg_dma_address(sg);
1670 if (sg_len > BNX2FC_MAX_BD_LEN) {
1671 sg_frags = bnx2fc_split_bd(io_req, addr, sg_len,
1672 bd_count);
1673 } else {
1674
1675 sg_frags = 1;
1676 bd[bd_count].buf_addr_lo = addr & 0xffffffff;
1677 bd[bd_count].buf_addr_hi = addr >> 32;
1678 bd[bd_count].buf_len = (u16)sg_len;
1679 bd[bd_count].flags = 0;
1680 }
1681 bd_count += sg_frags;
1682 byte_count += sg_len;
1683 }
1684 if (byte_count != scsi_bufflen(sc))
1685 printk(KERN_ERR PFX "byte_count = %d != scsi_bufflen = %d, "
1686 "task_id = 0x%x\n", byte_count, scsi_bufflen(sc),
1687 io_req->xid);
1688 return bd_count;
1689 }
1690
1691 static int bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd *io_req)
1692 {
1693 struct scsi_cmnd *sc = io_req->sc_cmd;
1694 struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
1695 int bd_count;
1696
1697 if (scsi_sg_count(sc)) {
1698 bd_count = bnx2fc_map_sg(io_req);
1699 if (bd_count == 0)
1700 return -ENOMEM;
1701 } else {
1702 bd_count = 0;
1703 bd[0].buf_addr_lo = bd[0].buf_addr_hi = 0;
1704 bd[0].buf_len = bd[0].flags = 0;
1705 }
1706 io_req->bd_tbl->bd_valid = bd_count;
1707
1708
1709
1710
1711
1712 if (bd_count > BNX2FC_FW_MAX_BDS_PER_CMD) {
1713 pr_err("bd_count = %d exceeded FW supported max BD(255), task_id = 0x%x\n",
1714 bd_count, io_req->xid);
1715 return -ENOMEM;
1716 }
1717
1718 return 0;
1719 }
1720
1721 static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd *io_req)
1722 {
1723 struct scsi_cmnd *sc = io_req->sc_cmd;
1724 struct bnx2fc_interface *interface = io_req->port->priv;
1725 struct bnx2fc_hba *hba = interface->hba;
1726
1727
1728
1729
1730
1731 if (io_req->bd_tbl->bd_valid && sc && scsi_sg_count(sc)) {
1732 dma_unmap_sg(&hba->pcidev->dev, scsi_sglist(sc),
1733 scsi_sg_count(sc), sc->sc_data_direction);
1734 io_req->bd_tbl->bd_valid = 0;
1735 }
1736 }
1737
1738 void bnx2fc_build_fcp_cmnd(struct bnx2fc_cmd *io_req,
1739 struct fcp_cmnd *fcp_cmnd)
1740 {
1741 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1742
1743 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
1744
1745 int_to_scsilun(sc_cmd->device->lun, &fcp_cmnd->fc_lun);
1746
1747 fcp_cmnd->fc_dl = htonl(io_req->data_xfer_len);
1748 memcpy(fcp_cmnd->fc_cdb, sc_cmd->cmnd, sc_cmd->cmd_len);
1749
1750 fcp_cmnd->fc_cmdref = 0;
1751 fcp_cmnd->fc_pri_ta = 0;
1752 fcp_cmnd->fc_tm_flags = io_req->mp_req.tm_flags;
1753 fcp_cmnd->fc_flags = io_req->io_req_flags;
1754 fcp_cmnd->fc_pri_ta = FCP_PTA_SIMPLE;
1755 }
1756
1757 static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd *io_req,
1758 struct fcoe_fcp_rsp_payload *fcp_rsp,
1759 u8 num_rq, unsigned char *rq_data)
1760 {
1761 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1762 u8 rsp_flags = fcp_rsp->fcp_flags.flags;
1763 u32 rq_buff_len = 0;
1764 int fcp_sns_len = 0;
1765 int fcp_rsp_len = 0;
1766
1767 io_req->fcp_status = FC_GOOD;
1768 io_req->fcp_resid = 0;
1769 if (rsp_flags & (FCOE_FCP_RSP_FLAGS_FCP_RESID_OVER |
1770 FCOE_FCP_RSP_FLAGS_FCP_RESID_UNDER))
1771 io_req->fcp_resid = fcp_rsp->fcp_resid;
1772
1773 io_req->scsi_comp_flags = rsp_flags;
1774 io_req->cdb_status = fcp_rsp->scsi_status_code;
1775
1776
1777 if (num_rq) {
1778
1779
1780
1781
1782
1783
1784
1785 if (rsp_flags &
1786 FCOE_FCP_RSP_FLAGS_FCP_RSP_LEN_VALID) {
1787 fcp_rsp_len = rq_buff_len
1788 = fcp_rsp->fcp_rsp_len;
1789 }
1790
1791 if (rsp_flags &
1792 FCOE_FCP_RSP_FLAGS_FCP_SNS_LEN_VALID) {
1793 fcp_sns_len = fcp_rsp->fcp_sns_len;
1794 rq_buff_len += fcp_rsp->fcp_sns_len;
1795 }
1796
1797 io_req->fcp_rsp_len = fcp_rsp_len;
1798 io_req->fcp_sns_len = fcp_sns_len;
1799
1800 if (rq_buff_len > num_rq * BNX2FC_RQ_BUF_SZ) {
1801
1802 printk(KERN_ERR PFX "invalid sns length %d\n",
1803 rq_buff_len);
1804
1805 rq_buff_len = num_rq * BNX2FC_RQ_BUF_SZ;
1806 }
1807
1808
1809 if ((fcp_rsp_len == 4) || (fcp_rsp_len == 8)) {
1810
1811 io_req->fcp_rsp_code = rq_data[3];
1812 BNX2FC_IO_DBG(io_req, "fcp_rsp_code = %d\n",
1813 io_req->fcp_rsp_code);
1814 }
1815
1816
1817 rq_data += fcp_rsp_len;
1818
1819 if (fcp_sns_len > SCSI_SENSE_BUFFERSIZE) {
1820 printk(KERN_ERR PFX "Truncating sense buffer\n");
1821 fcp_sns_len = SCSI_SENSE_BUFFERSIZE;
1822 }
1823
1824 memset(sc_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1825 if (fcp_sns_len)
1826 memcpy(sc_cmd->sense_buffer, rq_data, fcp_sns_len);
1827
1828 }
1829 }
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839 int bnx2fc_queuecommand(struct Scsi_Host *host,
1840 struct scsi_cmnd *sc_cmd)
1841 {
1842 struct fc_lport *lport = shost_priv(host);
1843 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1844 struct fc_rport_libfc_priv *rp = rport->dd_data;
1845 struct bnx2fc_rport *tgt;
1846 struct bnx2fc_cmd *io_req;
1847 int rc = 0;
1848 int rval;
1849
1850 rval = fc_remote_port_chkready(rport);
1851 if (rval) {
1852 sc_cmd->result = rval;
1853 scsi_done(sc_cmd);
1854 return 0;
1855 }
1856
1857 if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
1858 rc = SCSI_MLQUEUE_HOST_BUSY;
1859 goto exit_qcmd;
1860 }
1861
1862
1863 tgt = (struct bnx2fc_rport *)&rp[1];
1864
1865 if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
1866
1867
1868
1869
1870 rc = SCSI_MLQUEUE_TARGET_BUSY;
1871 goto exit_qcmd;
1872 }
1873 if (tgt->retry_delay_timestamp) {
1874 if (time_after(jiffies, tgt->retry_delay_timestamp)) {
1875 tgt->retry_delay_timestamp = 0;
1876 } else {
1877
1878 rc = SCSI_MLQUEUE_TARGET_BUSY;
1879 goto exit_qcmd;
1880 }
1881 }
1882
1883 spin_lock_bh(&tgt->tgt_lock);
1884
1885 io_req = bnx2fc_cmd_alloc(tgt);
1886 if (!io_req) {
1887 rc = SCSI_MLQUEUE_HOST_BUSY;
1888 goto exit_qcmd_tgtlock;
1889 }
1890 io_req->sc_cmd = sc_cmd;
1891
1892 if (bnx2fc_post_io_req(tgt, io_req)) {
1893 printk(KERN_ERR PFX "Unable to post io_req\n");
1894 rc = SCSI_MLQUEUE_HOST_BUSY;
1895 goto exit_qcmd_tgtlock;
1896 }
1897
1898 exit_qcmd_tgtlock:
1899 spin_unlock_bh(&tgt->tgt_lock);
1900 exit_qcmd:
1901 return rc;
1902 }
1903
1904 void bnx2fc_process_scsi_cmd_compl(struct bnx2fc_cmd *io_req,
1905 struct fcoe_task_ctx_entry *task,
1906 u8 num_rq, unsigned char *rq_data)
1907 {
1908 struct fcoe_fcp_rsp_payload *fcp_rsp;
1909 struct bnx2fc_rport *tgt = io_req->tgt;
1910 struct scsi_cmnd *sc_cmd;
1911 u16 scope = 0, qualifier = 0;
1912
1913
1914
1915 if (test_and_set_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags)) {
1916
1917 BNX2FC_IO_DBG(io_req, "Timer context finished processing "
1918 "this scsi cmd\n");
1919 if (test_and_clear_bit(BNX2FC_FLAG_IO_CLEANUP,
1920 &io_req->req_flags)) {
1921 BNX2FC_IO_DBG(io_req,
1922 "Actual completion after cleanup request cleaning up\n");
1923 bnx2fc_process_cleanup_compl(io_req, task, num_rq);
1924 }
1925 return;
1926 }
1927
1928
1929 if (cancel_delayed_work(&io_req->timeout_work))
1930 kref_put(&io_req->refcount,
1931 bnx2fc_cmd_release);
1932
1933 sc_cmd = io_req->sc_cmd;
1934 if (sc_cmd == NULL) {
1935 printk(KERN_ERR PFX "scsi_cmd_compl - sc_cmd is NULL\n");
1936 return;
1937 }
1938
1939
1940 fcp_rsp = (struct fcoe_fcp_rsp_payload *)
1941 &(task->rxwr_only.union_ctx.comp_info.fcp_rsp.payload);
1942
1943
1944 bnx2fc_parse_fcp_rsp(io_req, fcp_rsp, num_rq, rq_data);
1945
1946 if (!bnx2fc_priv(sc_cmd)->io_req) {
1947 printk(KERN_ERR PFX "io_req is NULL\n");
1948 return;
1949 }
1950
1951 if (io_req->on_active_queue) {
1952 list_del_init(&io_req->link);
1953 io_req->on_active_queue = 0;
1954
1955 list_add_tail(&io_req->link, &tgt->io_retire_queue);
1956 } else {
1957
1958
1959
1960
1961 BNX2FC_IO_DBG(io_req, "xid not on active_cmd_queue\n");
1962 if (io_req->wait_for_abts_comp)
1963 if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
1964 &io_req->req_flags))
1965 complete(&io_req->abts_done);
1966 }
1967
1968 bnx2fc_unmap_sg_list(io_req);
1969 io_req->sc_cmd = NULL;
1970
1971 switch (io_req->fcp_status) {
1972 case FC_GOOD:
1973 if (io_req->cdb_status == 0) {
1974
1975 sc_cmd->result = DID_OK << 16;
1976 } else {
1977
1978 BNX2FC_IO_DBG(io_req, "scsi_cmpl: cdb_status = %d"
1979 " fcp_resid = 0x%x\n",
1980 io_req->cdb_status, io_req->fcp_resid);
1981 sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
1982
1983 if (io_req->cdb_status == SAM_STAT_TASK_SET_FULL ||
1984 io_req->cdb_status == SAM_STAT_BUSY) {
1985
1986
1987
1988
1989
1990
1991 if (fcp_rsp->retry_delay_timer) {
1992
1993 scope = fcp_rsp->retry_delay_timer
1994 & 0xC000;
1995
1996 qualifier = fcp_rsp->retry_delay_timer
1997 & 0x3FFF;
1998 }
1999 if (scope > 0 && qualifier > 0 &&
2000 qualifier <= 0x3FEF) {
2001
2002
2003
2004
2005 tgt->retry_delay_timestamp = jiffies +
2006 (qualifier * HZ / 10);
2007 }
2008 }
2009 }
2010 if (io_req->fcp_resid)
2011 scsi_set_resid(sc_cmd, io_req->fcp_resid);
2012 break;
2013 default:
2014 printk(KERN_ERR PFX "scsi_cmd_compl: fcp_status = %d\n",
2015 io_req->fcp_status);
2016 break;
2017 }
2018 bnx2fc_priv(sc_cmd)->io_req = NULL;
2019 scsi_done(sc_cmd);
2020 kref_put(&io_req->refcount, bnx2fc_cmd_release);
2021 }
2022
2023 int bnx2fc_post_io_req(struct bnx2fc_rport *tgt,
2024 struct bnx2fc_cmd *io_req)
2025 {
2026 struct fcoe_task_ctx_entry *task;
2027 struct fcoe_task_ctx_entry *task_page;
2028 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
2029 struct fcoe_port *port = tgt->port;
2030 struct bnx2fc_interface *interface = port->priv;
2031 struct bnx2fc_hba *hba = interface->hba;
2032 struct fc_lport *lport = port->lport;
2033 int task_idx, index;
2034 u16 xid;
2035
2036
2037
2038
2039 io_req->cmd_type = BNX2FC_SCSI_CMD;
2040 io_req->port = port;
2041 io_req->tgt = tgt;
2042 io_req->data_xfer_len = scsi_bufflen(sc_cmd);
2043 bnx2fc_priv(sc_cmd)->io_req = io_req;
2044
2045 if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
2046 io_req->io_req_flags = BNX2FC_READ;
2047 this_cpu_inc(lport->stats->InputRequests);
2048 this_cpu_add(lport->stats->InputBytes, io_req->data_xfer_len);
2049 } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
2050 io_req->io_req_flags = BNX2FC_WRITE;
2051 this_cpu_inc(lport->stats->OutputRequests);
2052 this_cpu_add(lport->stats->OutputBytes, io_req->data_xfer_len);
2053 } else {
2054 io_req->io_req_flags = 0;
2055 this_cpu_inc(lport->stats->ControlRequests);
2056 }
2057
2058 xid = io_req->xid;
2059
2060
2061 if (bnx2fc_build_bd_list_from_sg(io_req)) {
2062 printk(KERN_ERR PFX "BD list creation failed\n");
2063 kref_put(&io_req->refcount, bnx2fc_cmd_release);
2064 return -EAGAIN;
2065 }
2066
2067 task_idx = xid / BNX2FC_TASKS_PER_PAGE;
2068 index = xid % BNX2FC_TASKS_PER_PAGE;
2069
2070
2071 task_page = (struct fcoe_task_ctx_entry *) hba->task_ctx[task_idx];
2072 task = &(task_page[index]);
2073 bnx2fc_init_task(io_req, task);
2074
2075 if (tgt->flush_in_prog) {
2076 printk(KERN_ERR PFX "Flush in progress..Host Busy\n");
2077 kref_put(&io_req->refcount, bnx2fc_cmd_release);
2078 return -EAGAIN;
2079 }
2080
2081 if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
2082 printk(KERN_ERR PFX "Session not ready...post_io\n");
2083 kref_put(&io_req->refcount, bnx2fc_cmd_release);
2084 return -EAGAIN;
2085 }
2086
2087
2088 if (tgt->io_timeout)
2089 bnx2fc_cmd_timer_set(io_req, BNX2FC_IO_TIMEOUT);
2090
2091 bnx2fc_add_2_sq(tgt, xid);
2092
2093
2094
2095 io_req->on_active_queue = 1;
2096
2097 list_add_tail(&io_req->link, &tgt->active_cmd_queue);
2098
2099
2100 bnx2fc_ring_doorbell(tgt);
2101 return 0;
2102 }