1 /* bnx2fc_io.c: Broadcom NetXtreme II Linux FCoE offload driver.
2 * IO manager and SCSI IO processing.
4 * Copyright (c) 2008 - 2010 Broadcom Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation.
10 * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
14 static int bnx2fc_split_bd(struct bnx2fc_cmd *io_req, u64 addr, int sg_len,
16 static int bnx2fc_map_sg(struct bnx2fc_cmd *io_req);
17 static void bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd *io_req);
18 static int bnx2fc_post_io_req(struct bnx2fc_rport *tgt,
19 struct bnx2fc_cmd *io_req);
20 static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd *io_req);
21 static void bnx2fc_free_mp_resc(struct bnx2fc_cmd *io_req);
22 static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd *io_req,
23 struct fcoe_fcp_rsp_payload *fcp_rsp,
26 void bnx2fc_cmd_timer_set(struct bnx2fc_cmd *io_req,
27 unsigned int timer_msec)
29 struct bnx2fc_hba *hba = io_req->port->priv;
31 if (queue_delayed_work(hba->timer_work_queue, &io_req->timeout_work,
32 msecs_to_jiffies(timer_msec)))
33 kref_get(&io_req->refcount);
36 static void bnx2fc_cmd_timeout(struct work_struct *work)
38 struct bnx2fc_cmd *io_req = container_of(work, struct bnx2fc_cmd,
40 struct fc_lport *lport;
41 struct fc_rport_priv *rdata;
42 u8 cmd_type = io_req->cmd_type;
43 struct bnx2fc_rport *tgt = io_req->tgt;
47 BNX2FC_IO_DBG(io_req, "cmd_timeout, cmd_type = %d,"
48 "req_flags = %lx\n", cmd_type, io_req->req_flags);
50 spin_lock_bh(&tgt->tgt_lock);
51 if (test_and_clear_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags)) {
52 clear_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags);
54 * ideally we should hold the io_req until RRQ complets,
55 * and release io_req from timeout hold.
57 spin_unlock_bh(&tgt->tgt_lock);
58 bnx2fc_send_rrq(io_req);
61 if (test_and_clear_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags)) {
62 BNX2FC_IO_DBG(io_req, "IO ready for reuse now\n");
68 if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
69 &io_req->req_flags)) {
70 /* Handle eh_abort timeout */
71 BNX2FC_IO_DBG(io_req, "eh_abort timed out\n");
72 complete(&io_req->tm_done);
73 } else if (test_bit(BNX2FC_FLAG_ISSUE_ABTS,
74 &io_req->req_flags)) {
75 /* Handle internally generated ABTS timeout */
76 BNX2FC_IO_DBG(io_req, "ABTS timed out refcnt = %d\n",
77 io_req->refcount.refcount.counter);
78 if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
79 &io_req->req_flags))) {
81 lport = io_req->port->lport;
82 rdata = io_req->tgt->rdata;
83 logo_issued = test_and_set_bit(
84 BNX2FC_FLAG_EXPL_LOGO,
86 kref_put(&io_req->refcount, bnx2fc_cmd_release);
87 spin_unlock_bh(&tgt->tgt_lock);
89 /* Explicitly logo the target */
91 BNX2FC_IO_DBG(io_req, "Explicit "
92 "logo - tgt flags = 0x%lx\n",
95 mutex_lock(&lport->disc.disc_mutex);
96 lport->tt.rport_logoff(rdata);
97 mutex_unlock(&lport->disc.disc_mutex);
102 /* Hanlde IO timeout */
103 BNX2FC_IO_DBG(io_req, "IO timed out. issue ABTS\n");
104 if (test_and_set_bit(BNX2FC_FLAG_IO_COMPL,
105 &io_req->req_flags)) {
106 BNX2FC_IO_DBG(io_req, "IO completed before "
111 if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
112 &io_req->req_flags)) {
113 rc = bnx2fc_initiate_abts(io_req);
117 * Explicitly logo the target if
118 * abts initiation fails
120 lport = io_req->port->lport;
121 rdata = io_req->tgt->rdata;
122 logo_issued = test_and_set_bit(
123 BNX2FC_FLAG_EXPL_LOGO,
125 kref_put(&io_req->refcount, bnx2fc_cmd_release);
126 spin_unlock_bh(&tgt->tgt_lock);
129 BNX2FC_IO_DBG(io_req, "Explicit "
130 "logo - tgt flags = 0x%lx\n",
134 mutex_lock(&lport->disc.disc_mutex);
135 lport->tt.rport_logoff(rdata);
136 mutex_unlock(&lport->disc.disc_mutex);
140 BNX2FC_IO_DBG(io_req, "IO already in "
141 "ABTS processing\n");
147 if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags)) {
148 BNX2FC_IO_DBG(io_req, "ABTS for ELS timed out\n");
150 if (!test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
151 &io_req->req_flags)) {
152 lport = io_req->port->lport;
153 rdata = io_req->tgt->rdata;
154 logo_issued = test_and_set_bit(
155 BNX2FC_FLAG_EXPL_LOGO,
157 kref_put(&io_req->refcount, bnx2fc_cmd_release);
158 spin_unlock_bh(&tgt->tgt_lock);
160 /* Explicitly logo the target */
162 BNX2FC_IO_DBG(io_req, "Explicitly logo"
164 mutex_lock(&lport->disc.disc_mutex);
165 lport->tt.rport_logoff(rdata);
166 mutex_unlock(&lport->disc.disc_mutex);
172 * Handle ELS timeout.
173 * tgt_lock is used to sync compl path and timeout
174 * path. If els compl path is processing this IO, we
175 * have nothing to do here, just release the timer hold
177 BNX2FC_IO_DBG(io_req, "ELS timed out\n");
178 if (test_and_set_bit(BNX2FC_FLAG_ELS_DONE,
182 /* Indicate the cb_func that this ELS is timed out */
183 set_bit(BNX2FC_FLAG_ELS_TIMEOUT, &io_req->req_flags);
185 if ((io_req->cb_func) && (io_req->cb_arg)) {
186 io_req->cb_func(io_req->cb_arg);
187 io_req->cb_arg = NULL;
192 printk(KERN_ERR PFX "cmd_timeout: invalid cmd_type %d\n",
198 /* release the cmd that was held when timer was set */
199 kref_put(&io_req->refcount, bnx2fc_cmd_release);
200 spin_unlock_bh(&tgt->tgt_lock);
203 static void bnx2fc_scsi_done(struct bnx2fc_cmd *io_req, int err_code)
205 /* Called with host lock held */
206 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
209 * active_cmd_queue may have other command types as well,
210 * and during flush operation, we want to error back only
213 if (io_req->cmd_type != BNX2FC_SCSI_CMD)
216 BNX2FC_IO_DBG(io_req, "scsi_done. err_code = 0x%x\n", err_code);
217 bnx2fc_unmap_sg_list(io_req);
218 io_req->sc_cmd = NULL;
220 printk(KERN_ERR PFX "scsi_done - sc_cmd NULL. "
221 "IO(0x%x) already cleaned up\n",
225 sc_cmd->result = err_code << 16;
227 BNX2FC_IO_DBG(io_req, "sc=%p, result=0x%x, retries=%d, allowed=%d\n",
228 sc_cmd, host_byte(sc_cmd->result), sc_cmd->retries,
230 scsi_set_resid(sc_cmd, scsi_bufflen(sc_cmd));
231 sc_cmd->SCp.ptr = NULL;
232 sc_cmd->scsi_done(sc_cmd);
235 struct bnx2fc_cmd_mgr *bnx2fc_cmd_mgr_alloc(struct bnx2fc_hba *hba,
236 u16 min_xid, u16 max_xid)
238 struct bnx2fc_cmd_mgr *cmgr;
239 struct io_bdt *bdt_info;
240 struct bnx2fc_cmd *io_req;
248 if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN) {
249 printk(KERN_ERR PFX "cmd_mgr_alloc: Invalid min_xid 0x%x \
250 and max_xid 0x%x\n", min_xid, max_xid);
253 BNX2FC_MISC_DBG("min xid 0x%x, max xid 0x%x\n", min_xid, max_xid);
255 num_ios = max_xid - min_xid + 1;
256 len = (num_ios * (sizeof(struct bnx2fc_cmd *)));
257 len += sizeof(struct bnx2fc_cmd_mgr);
259 cmgr = kzalloc(len, GFP_KERNEL);
261 printk(KERN_ERR PFX "failed to alloc cmgr\n");
265 cmgr->free_list = kzalloc(sizeof(*cmgr->free_list) *
266 num_possible_cpus(), GFP_KERNEL);
267 if (!cmgr->free_list) {
268 printk(KERN_ERR PFX "failed to alloc free_list\n");
272 cmgr->free_list_lock = kzalloc(sizeof(*cmgr->free_list_lock) *
273 num_possible_cpus(), GFP_KERNEL);
274 if (!cmgr->free_list_lock) {
275 printk(KERN_ERR PFX "failed to alloc free_list_lock\n");
280 cmgr->cmds = (struct bnx2fc_cmd **)(cmgr + 1);
282 for (i = 0; i < num_possible_cpus(); i++) {
283 INIT_LIST_HEAD(&cmgr->free_list[i]);
284 spin_lock_init(&cmgr->free_list_lock[i]);
287 /* Pre-allocated pool of bnx2fc_cmds */
288 xid = BNX2FC_MIN_XID;
289 for (i = 0; i < num_ios; i++) {
290 io_req = kzalloc(sizeof(*io_req), GFP_KERNEL);
293 printk(KERN_ERR PFX "failed to alloc io_req\n");
297 INIT_LIST_HEAD(&io_req->link);
298 INIT_DELAYED_WORK(&io_req->timeout_work, bnx2fc_cmd_timeout);
301 if (io_req->xid >= BNX2FC_MAX_OUTSTANDING_CMNDS)
302 printk(KERN_ERR PFX "ERROR allocating xids - 0x%x\n",
304 list_add_tail(&io_req->link,
305 &cmgr->free_list[io_req->xid % num_possible_cpus()]);
309 /* Allocate pool of io_bdts - one for each bnx2fc_cmd */
310 mem_size = num_ios * sizeof(struct io_bdt *);
311 cmgr->io_bdt_pool = kmalloc(mem_size, GFP_KERNEL);
312 if (!cmgr->io_bdt_pool) {
313 printk(KERN_ERR PFX "failed to alloc io_bdt_pool\n");
317 mem_size = sizeof(struct io_bdt);
318 for (i = 0; i < num_ios; i++) {
319 cmgr->io_bdt_pool[i] = kmalloc(mem_size, GFP_KERNEL);
320 if (!cmgr->io_bdt_pool[i]) {
321 printk(KERN_ERR PFX "failed to alloc "
322 "io_bdt_pool[%d]\n", i);
327 /* Allocate an map fcoe_bdt_ctx structures */
328 bd_tbl_sz = BNX2FC_MAX_BDS_PER_CMD * sizeof(struct fcoe_bd_ctx);
329 for (i = 0; i < num_ios; i++) {
330 bdt_info = cmgr->io_bdt_pool[i];
331 bdt_info->bd_tbl = dma_alloc_coherent(&hba->pcidev->dev,
333 &bdt_info->bd_tbl_dma,
335 if (!bdt_info->bd_tbl) {
336 printk(KERN_ERR PFX "failed to alloc "
345 bnx2fc_cmd_mgr_free(cmgr);
349 void bnx2fc_cmd_mgr_free(struct bnx2fc_cmd_mgr *cmgr)
351 struct io_bdt *bdt_info;
352 struct bnx2fc_hba *hba = cmgr->hba;
354 u16 min_xid = BNX2FC_MIN_XID;
355 u16 max_xid = BNX2FC_MAX_XID;
359 num_ios = max_xid - min_xid + 1;
361 /* Free fcoe_bdt_ctx structures */
362 if (!cmgr->io_bdt_pool)
365 bd_tbl_sz = BNX2FC_MAX_BDS_PER_CMD * sizeof(struct fcoe_bd_ctx);
366 for (i = 0; i < num_ios; i++) {
367 bdt_info = cmgr->io_bdt_pool[i];
368 if (bdt_info->bd_tbl) {
369 dma_free_coherent(&hba->pcidev->dev, bd_tbl_sz,
371 bdt_info->bd_tbl_dma);
372 bdt_info->bd_tbl = NULL;
376 /* Destroy io_bdt pool */
377 for (i = 0; i < num_ios; i++) {
378 kfree(cmgr->io_bdt_pool[i]);
379 cmgr->io_bdt_pool[i] = NULL;
382 kfree(cmgr->io_bdt_pool);
383 cmgr->io_bdt_pool = NULL;
386 kfree(cmgr->free_list_lock);
388 /* Destroy cmd pool */
389 if (!cmgr->free_list)
392 for (i = 0; i < num_possible_cpus(); i++) {
393 struct list_head *list;
394 struct list_head *tmp;
396 list_for_each_safe(list, tmp, &cmgr->free_list[i]) {
397 struct bnx2fc_cmd *io_req = (struct bnx2fc_cmd *)list;
398 list_del(&io_req->link);
402 kfree(cmgr->free_list);
404 /* Free command manager itself */
408 struct bnx2fc_cmd *bnx2fc_elstm_alloc(struct bnx2fc_rport *tgt, int type)
410 struct fcoe_port *port = tgt->port;
411 struct bnx2fc_hba *hba = port->priv;
412 struct bnx2fc_cmd_mgr *cmd_mgr = hba->cmd_mgr;
413 struct bnx2fc_cmd *io_req;
414 struct list_head *listp;
415 struct io_bdt *bd_tbl;
419 max_sqes = tgt->max_sqes;
421 case BNX2FC_TASK_MGMT_CMD:
422 max_sqes = BNX2FC_TM_MAX_SQES;
425 max_sqes = BNX2FC_ELS_MAX_SQES;
432 * NOTE: Free list insertions and deletions are protected with
435 spin_lock_bh(&cmd_mgr->free_list_lock[smp_processor_id()]);
436 if ((list_empty(&(cmd_mgr->free_list[smp_processor_id()]))) ||
437 (tgt->num_active_ios.counter >= max_sqes)) {
438 BNX2FC_TGT_DBG(tgt, "No free els_tm cmds available "
439 "ios(%d):sqes(%d)\n",
440 tgt->num_active_ios.counter, tgt->max_sqes);
441 if (list_empty(&(cmd_mgr->free_list[smp_processor_id()])))
442 printk(KERN_ERR PFX "elstm_alloc: list_empty\n");
443 spin_unlock_bh(&cmd_mgr->free_list_lock[smp_processor_id()]);
447 listp = (struct list_head *)
448 cmd_mgr->free_list[smp_processor_id()].next;
449 list_del_init(listp);
450 io_req = (struct bnx2fc_cmd *) listp;
452 cmd_mgr->cmds[xid] = io_req;
453 atomic_inc(&tgt->num_active_ios);
454 spin_unlock_bh(&cmd_mgr->free_list_lock[smp_processor_id()]);
456 INIT_LIST_HEAD(&io_req->link);
459 io_req->cmd_mgr = cmd_mgr;
460 io_req->req_flags = 0;
461 io_req->cmd_type = type;
463 /* Bind io_bdt for this io_req */
464 /* Have a static link between io_req and io_bdt_pool */
465 bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid];
466 bd_tbl->io_req = io_req;
468 /* Hold the io_req against deletion */
469 kref_init(&io_req->refcount);
472 static struct bnx2fc_cmd *bnx2fc_cmd_alloc(struct bnx2fc_rport *tgt)
474 struct fcoe_port *port = tgt->port;
475 struct bnx2fc_hba *hba = port->priv;
476 struct bnx2fc_cmd_mgr *cmd_mgr = hba->cmd_mgr;
477 struct bnx2fc_cmd *io_req;
478 struct list_head *listp;
479 struct io_bdt *bd_tbl;
483 max_sqes = BNX2FC_SCSI_MAX_SQES;
485 * NOTE: Free list insertions and deletions are protected with
488 spin_lock_bh(&cmd_mgr->free_list_lock[smp_processor_id()]);
489 if ((list_empty(&cmd_mgr->free_list[smp_processor_id()])) ||
490 (tgt->num_active_ios.counter >= max_sqes)) {
491 spin_unlock_bh(&cmd_mgr->free_list_lock[smp_processor_id()]);
495 listp = (struct list_head *)
496 cmd_mgr->free_list[smp_processor_id()].next;
497 list_del_init(listp);
498 io_req = (struct bnx2fc_cmd *) listp;
500 cmd_mgr->cmds[xid] = io_req;
501 atomic_inc(&tgt->num_active_ios);
502 spin_unlock_bh(&cmd_mgr->free_list_lock[smp_processor_id()]);
504 INIT_LIST_HEAD(&io_req->link);
507 io_req->cmd_mgr = cmd_mgr;
508 io_req->req_flags = 0;
510 /* Bind io_bdt for this io_req */
511 /* Have a static link between io_req and io_bdt_pool */
512 bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid];
513 bd_tbl->io_req = io_req;
515 /* Hold the io_req against deletion */
516 kref_init(&io_req->refcount);
520 void bnx2fc_cmd_release(struct kref *ref)
522 struct bnx2fc_cmd *io_req = container_of(ref,
523 struct bnx2fc_cmd, refcount);
524 struct bnx2fc_cmd_mgr *cmd_mgr = io_req->cmd_mgr;
526 spin_lock_bh(&cmd_mgr->free_list_lock[smp_processor_id()]);
527 if (io_req->cmd_type != BNX2FC_SCSI_CMD)
528 bnx2fc_free_mp_resc(io_req);
529 cmd_mgr->cmds[io_req->xid] = NULL;
530 /* Delete IO from retire queue */
531 list_del_init(&io_req->link);
532 /* Add it to the free list */
533 list_add(&io_req->link,
534 &cmd_mgr->free_list[smp_processor_id()]);
535 atomic_dec(&io_req->tgt->num_active_ios);
536 spin_unlock_bh(&cmd_mgr->free_list_lock[smp_processor_id()]);
539 static void bnx2fc_free_mp_resc(struct bnx2fc_cmd *io_req)
541 struct bnx2fc_mp_req *mp_req = &(io_req->mp_req);
542 struct bnx2fc_hba *hba = io_req->port->priv;
543 size_t sz = sizeof(struct fcoe_bd_ctx);
546 mp_req->tm_flags = 0;
547 if (mp_req->mp_req_bd) {
548 dma_free_coherent(&hba->pcidev->dev, sz,
550 mp_req->mp_req_bd_dma);
551 mp_req->mp_req_bd = NULL;
553 if (mp_req->mp_resp_bd) {
554 dma_free_coherent(&hba->pcidev->dev, sz,
556 mp_req->mp_resp_bd_dma);
557 mp_req->mp_resp_bd = NULL;
559 if (mp_req->req_buf) {
560 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
562 mp_req->req_buf_dma);
563 mp_req->req_buf = NULL;
565 if (mp_req->resp_buf) {
566 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
568 mp_req->resp_buf_dma);
569 mp_req->resp_buf = NULL;
573 int bnx2fc_init_mp_req(struct bnx2fc_cmd *io_req)
575 struct bnx2fc_mp_req *mp_req;
576 struct fcoe_bd_ctx *mp_req_bd;
577 struct fcoe_bd_ctx *mp_resp_bd;
578 struct bnx2fc_hba *hba = io_req->port->priv;
582 mp_req = (struct bnx2fc_mp_req *)&(io_req->mp_req);
583 memset(mp_req, 0, sizeof(struct bnx2fc_mp_req));
585 mp_req->req_len = sizeof(struct fcp_cmnd);
586 io_req->data_xfer_len = mp_req->req_len;
587 mp_req->req_buf = dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
588 &mp_req->req_buf_dma,
590 if (!mp_req->req_buf) {
591 printk(KERN_ERR PFX "unable to alloc MP req buffer\n");
592 bnx2fc_free_mp_resc(io_req);
596 mp_req->resp_buf = dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
597 &mp_req->resp_buf_dma,
599 if (!mp_req->resp_buf) {
600 printk(KERN_ERR PFX "unable to alloc TM resp buffer\n");
601 bnx2fc_free_mp_resc(io_req);
604 memset(mp_req->req_buf, 0, PAGE_SIZE);
605 memset(mp_req->resp_buf, 0, PAGE_SIZE);
607 /* Allocate and map mp_req_bd and mp_resp_bd */
608 sz = sizeof(struct fcoe_bd_ctx);
609 mp_req->mp_req_bd = dma_alloc_coherent(&hba->pcidev->dev, sz,
610 &mp_req->mp_req_bd_dma,
612 if (!mp_req->mp_req_bd) {
613 printk(KERN_ERR PFX "unable to alloc MP req bd\n");
614 bnx2fc_free_mp_resc(io_req);
617 mp_req->mp_resp_bd = dma_alloc_coherent(&hba->pcidev->dev, sz,
618 &mp_req->mp_resp_bd_dma,
620 if (!mp_req->mp_req_bd) {
621 printk(KERN_ERR PFX "unable to alloc MP resp bd\n");
622 bnx2fc_free_mp_resc(io_req);
626 addr = mp_req->req_buf_dma;
627 mp_req_bd = mp_req->mp_req_bd;
628 mp_req_bd->buf_addr_lo = (u32)addr & 0xffffffff;
629 mp_req_bd->buf_addr_hi = (u32)((u64)addr >> 32);
630 mp_req_bd->buf_len = PAGE_SIZE;
631 mp_req_bd->flags = 0;
634 * MP buffer is either a task mgmt command or an ELS.
635 * So the assumption is that it consumes a single bd
636 * entry in the bd table
638 mp_resp_bd = mp_req->mp_resp_bd;
639 addr = mp_req->resp_buf_dma;
640 mp_resp_bd->buf_addr_lo = (u32)addr & 0xffffffff;
641 mp_resp_bd->buf_addr_hi = (u32)((u64)addr >> 32);
642 mp_resp_bd->buf_len = PAGE_SIZE;
643 mp_resp_bd->flags = 0;
648 static int bnx2fc_initiate_tmf(struct scsi_cmnd *sc_cmd, u8 tm_flags)
650 struct fc_lport *lport;
651 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
652 struct fc_rport_libfc_priv *rp = rport->dd_data;
653 struct fcoe_port *port;
654 struct bnx2fc_hba *hba;
655 struct bnx2fc_rport *tgt;
656 struct bnx2fc_cmd *io_req;
657 struct bnx2fc_mp_req *tm_req;
658 struct fcoe_task_ctx_entry *task;
659 struct fcoe_task_ctx_entry *task_page;
660 struct Scsi_Host *host = sc_cmd->device->host;
661 struct fc_frame_header *fc_hdr;
662 struct fcp_cmnd *fcp_cmnd;
667 unsigned long start = jiffies;
669 lport = shost_priv(host);
670 port = lport_priv(lport);
674 printk(KERN_ALERT PFX "device_reset: rport is NULL\n");
679 rc = fc_block_scsi_eh(sc_cmd);
683 if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
684 printk(KERN_ERR PFX "device_reset: link is not ready\n");
688 /* rport and tgt are allocated together, so tgt should be non-NULL */
689 tgt = (struct bnx2fc_rport *)&rp[1];
691 if (!(test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
692 printk(KERN_ERR PFX "device_reset: tgt not offloaded\n");
697 io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_TASK_MGMT_CMD);
699 if (time_after(jiffies, start + HZ)) {
700 printk(KERN_ERR PFX "tmf: Failed TMF");
707 /* Initialize rest of io_req fields */
708 io_req->sc_cmd = sc_cmd;
712 tm_req = (struct bnx2fc_mp_req *)&(io_req->mp_req);
714 rc = bnx2fc_init_mp_req(io_req);
716 printk(KERN_ERR PFX "Task mgmt MP request init failed\n");
717 kref_put(&io_req->refcount, bnx2fc_cmd_release);
722 io_req->io_req_flags = 0;
723 tm_req->tm_flags = tm_flags;
726 bnx2fc_build_fcp_cmnd(io_req, (struct fcp_cmnd *)tm_req->req_buf);
727 fcp_cmnd = (struct fcp_cmnd *)tm_req->req_buf;
728 memset(fcp_cmnd->fc_cdb, 0, sc_cmd->cmd_len);
732 fc_hdr = &(tm_req->req_fc_hdr);
734 did = rport->port_id;
735 __fc_fill_fc_hdr(fc_hdr, FC_RCTL_DD_UNSOL_CMD, did, sid,
736 FC_TYPE_FCP, FC_FC_FIRST_SEQ | FC_FC_END_SEQ |
738 /* Obtain exchange id */
741 BNX2FC_TGT_DBG(tgt, "Initiate TMF - xid = 0x%x\n", xid);
742 task_idx = xid/BNX2FC_TASKS_PER_PAGE;
743 index = xid % BNX2FC_TASKS_PER_PAGE;
745 /* Initialize task context for this IO request */
746 task_page = (struct fcoe_task_ctx_entry *) hba->task_ctx[task_idx];
747 task = &(task_page[index]);
748 bnx2fc_init_mp_task(io_req, task);
750 sc_cmd->SCp.ptr = (char *)io_req;
752 /* Obtain free SQ entry */
753 spin_lock_bh(&tgt->tgt_lock);
754 bnx2fc_add_2_sq(tgt, xid);
756 /* Enqueue the io_req to active_tm_queue */
757 io_req->on_tmf_queue = 1;
758 list_add_tail(&io_req->link, &tgt->active_tm_queue);
760 init_completion(&io_req->tm_done);
761 io_req->wait_for_comp = 1;
764 bnx2fc_ring_doorbell(tgt);
765 spin_unlock_bh(&tgt->tgt_lock);
767 rc = wait_for_completion_timeout(&io_req->tm_done,
768 BNX2FC_TM_TIMEOUT * HZ);
769 spin_lock_bh(&tgt->tgt_lock);
771 io_req->wait_for_comp = 0;
772 if (!(test_bit(BNX2FC_FLAG_TM_COMPL, &io_req->req_flags)))
773 set_bit(BNX2FC_FLAG_TM_TIMEOUT, &io_req->req_flags);
775 spin_unlock_bh(&tgt->tgt_lock);
778 printk(KERN_ERR PFX "task mgmt command failed...\n");
781 printk(KERN_ERR PFX "task mgmt command success...\n");
788 int bnx2fc_initiate_abts(struct bnx2fc_cmd *io_req)
790 struct fc_lport *lport;
791 struct bnx2fc_rport *tgt = io_req->tgt;
792 struct fc_rport *rport = tgt->rport;
793 struct fc_rport_priv *rdata = tgt->rdata;
794 struct bnx2fc_hba *hba;
795 struct fcoe_port *port;
796 struct bnx2fc_cmd *abts_io_req;
797 struct fcoe_task_ctx_entry *task;
798 struct fcoe_task_ctx_entry *task_page;
799 struct fc_frame_header *fc_hdr;
800 struct bnx2fc_mp_req *abts_req;
805 u32 r_a_tov = rdata->r_a_tov;
807 /* called with tgt_lock held */
808 BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_abts\n");
814 if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
815 printk(KERN_ERR PFX "initiate_abts: tgt not offloaded\n");
821 printk(KERN_ALERT PFX "initiate_abts: rport is NULL\n");
826 if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
827 printk(KERN_ERR PFX "initiate_abts: link is not ready\n");
832 abts_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_ABTS);
834 printk(KERN_ERR PFX "abts: couldnt allocate cmd\n");
839 /* Initialize rest of io_req fields */
840 abts_io_req->sc_cmd = NULL;
841 abts_io_req->port = port;
842 abts_io_req->tgt = tgt;
843 abts_io_req->data_xfer_len = 0; /* No data transfer for ABTS */
845 abts_req = (struct bnx2fc_mp_req *)&(abts_io_req->mp_req);
846 memset(abts_req, 0, sizeof(struct bnx2fc_mp_req));
849 fc_hdr = &(abts_req->req_fc_hdr);
851 /* Obtain oxid and rxid for the original exchange to be aborted */
852 fc_hdr->fh_ox_id = htons(io_req->xid);
853 fc_hdr->fh_rx_id = htons(io_req->task->rx_wr_tx_rd.rx_id);
856 did = rport->port_id;
858 __fc_fill_fc_hdr(fc_hdr, FC_RCTL_BA_ABTS, did, sid,
859 FC_TYPE_BLS, FC_FC_FIRST_SEQ | FC_FC_END_SEQ |
862 xid = abts_io_req->xid;
863 BNX2FC_IO_DBG(abts_io_req, "ABTS io_req\n");
864 task_idx = xid/BNX2FC_TASKS_PER_PAGE;
865 index = xid % BNX2FC_TASKS_PER_PAGE;
867 /* Initialize task context for this IO request */
868 task_page = (struct fcoe_task_ctx_entry *) hba->task_ctx[task_idx];
869 task = &(task_page[index]);
870 bnx2fc_init_mp_task(abts_io_req, task);
873 * ABTS task is a temporary task that will be cleaned up
874 * irrespective of ABTS response. We need to start the timer
875 * for the original exchange, as the CQE is posted for the original
878 * Timer for ABTS is started only when it is originated by a
879 * TM request. For the ABTS issued as part of ULP timeout,
880 * scsi-ml maintains the timers.
883 /* if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags))*/
884 bnx2fc_cmd_timer_set(io_req, 2 * r_a_tov);
886 /* Obtain free SQ entry */
887 bnx2fc_add_2_sq(tgt, xid);
890 bnx2fc_ring_doorbell(tgt);
896 int bnx2fc_initiate_cleanup(struct bnx2fc_cmd *io_req)
898 struct fc_lport *lport;
899 struct bnx2fc_rport *tgt = io_req->tgt;
900 struct bnx2fc_hba *hba;
901 struct fcoe_port *port;
902 struct bnx2fc_cmd *cleanup_io_req;
903 struct fcoe_task_ctx_entry *task;
904 struct fcoe_task_ctx_entry *task_page;
909 /* ASSUMPTION: called with tgt_lock held */
910 BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_cleanup\n");
916 cleanup_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_CLEANUP);
917 if (!cleanup_io_req) {
918 printk(KERN_ERR PFX "cleanup: couldnt allocate cmd\n");
923 /* Initialize rest of io_req fields */
924 cleanup_io_req->sc_cmd = NULL;
925 cleanup_io_req->port = port;
926 cleanup_io_req->tgt = tgt;
927 cleanup_io_req->data_xfer_len = 0; /* No data transfer for cleanup */
929 xid = cleanup_io_req->xid;
931 task_idx = xid/BNX2FC_TASKS_PER_PAGE;
932 index = xid % BNX2FC_TASKS_PER_PAGE;
934 /* Initialize task context for this IO request */
935 task_page = (struct fcoe_task_ctx_entry *) hba->task_ctx[task_idx];
936 task = &(task_page[index]);
937 orig_xid = io_req->xid;
939 BNX2FC_IO_DBG(io_req, "CLEANUP io_req xid = 0x%x\n", xid);
941 bnx2fc_init_cleanup_task(cleanup_io_req, task, orig_xid);
943 /* Obtain free SQ entry */
944 bnx2fc_add_2_sq(tgt, xid);
947 bnx2fc_ring_doorbell(tgt);
954 * bnx2fc_eh_target_reset: Reset a target
956 * @sc_cmd: SCSI command
958 * Set from SCSI host template to send task mgmt command to the target
959 * and wait for the response
961 int bnx2fc_eh_target_reset(struct scsi_cmnd *sc_cmd)
963 return bnx2fc_initiate_tmf(sc_cmd, FCP_TMF_TGT_RESET);
967 * bnx2fc_eh_device_reset - Reset a single LUN
969 * @sc_cmd: SCSI command
971 * Set from SCSI host template to send task mgmt command to the target
972 * and wait for the response
974 int bnx2fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
976 return bnx2fc_initiate_tmf(sc_cmd, FCP_TMF_LUN_RESET);
980 * bnx2fc_eh_abort - eh_abort_handler api to abort an outstanding
983 * @sc_cmd: SCSI_ML command pointer
985 * SCSI abort request handler
987 int bnx2fc_eh_abort(struct scsi_cmnd *sc_cmd)
989 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
990 struct fc_rport_libfc_priv *rp = rport->dd_data;
991 struct bnx2fc_cmd *io_req;
992 struct fc_lport *lport;
993 struct bnx2fc_rport *tgt;
997 rc = fc_block_scsi_eh(sc_cmd);
1001 lport = shost_priv(sc_cmd->device->host);
1002 if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
1003 printk(KERN_ALERT PFX "eh_abort: link not ready\n");
1007 tgt = (struct bnx2fc_rport *)&rp[1];
1009 BNX2FC_TGT_DBG(tgt, "Entered bnx2fc_eh_abort\n");
1011 spin_lock_bh(&tgt->tgt_lock);
1012 io_req = (struct bnx2fc_cmd *)sc_cmd->SCp.ptr;
1014 /* Command might have just completed */
1015 printk(KERN_ERR PFX "eh_abort: io_req is NULL\n");
1016 spin_unlock_bh(&tgt->tgt_lock);
1019 BNX2FC_IO_DBG(io_req, "eh_abort - refcnt = %d\n",
1020 io_req->refcount.refcount.counter);
1022 /* Hold IO request across abort processing */
1023 kref_get(&io_req->refcount);
1025 BUG_ON(tgt != io_req->tgt);
1027 /* Remove the io_req from the active_q. */
1029 * Task Mgmt functions (LUN RESET & TGT RESET) will not
1030 * issue an ABTS on this particular IO req, as the
1031 * io_req is no longer in the active_q.
1033 if (tgt->flush_in_prog) {
1034 printk(KERN_ALERT PFX "eh_abort: io_req (xid = 0x%x) "
1035 "flush in progress\n", io_req->xid);
1036 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1037 spin_unlock_bh(&tgt->tgt_lock);
1041 if (io_req->on_active_queue == 0) {
1042 printk(KERN_ALERT PFX "eh_abort: io_req (xid = 0x%x) "
1043 "not on active_q\n", io_req->xid);
1045 * This condition can happen only due to the FW bug,
1046 * where we do not receive cleanup response from
1047 * the FW. Handle this case gracefully by erroring
1048 * back the IO request to SCSI-ml
1050 bnx2fc_scsi_done(io_req, DID_ABORT);
1052 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1053 spin_unlock_bh(&tgt->tgt_lock);
1058 * Only eh_abort processing will remove the IO from
1059 * active_cmd_q before processing the request. this is
1060 * done to avoid race conditions between IOs aborted
1061 * as part of task management completion and eh_abort
1064 list_del_init(&io_req->link);
1065 io_req->on_active_queue = 0;
1066 /* Move IO req to retire queue */
1067 list_add_tail(&io_req->link, &tgt->io_retire_queue);
1069 init_completion(&io_req->tm_done);
1070 io_req->wait_for_comp = 1;
1072 if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags)) {
1073 /* Cancel the current timer running on this io_req */
1074 if (cancel_delayed_work(&io_req->timeout_work))
1075 kref_put(&io_req->refcount,
1076 bnx2fc_cmd_release); /* drop timer hold */
1077 set_bit(BNX2FC_FLAG_EH_ABORT, &io_req->req_flags);
1078 rc = bnx2fc_initiate_abts(io_req);
1080 printk(KERN_ALERT PFX "eh_abort: io_req (xid = 0x%x) "
1081 "already in abts processing\n", io_req->xid);
1082 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1083 spin_unlock_bh(&tgt->tgt_lock);
1087 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1088 spin_unlock_bh(&tgt->tgt_lock);
1091 spin_unlock_bh(&tgt->tgt_lock);
1093 wait_for_completion(&io_req->tm_done);
1095 spin_lock_bh(&tgt->tgt_lock);
1096 io_req->wait_for_comp = 0;
1097 if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
1098 &io_req->req_flags))) {
1099 /* Let the scsi-ml try to recover this command */
1100 printk(KERN_ERR PFX "abort failed, xid = 0x%x\n",
1105 * We come here even when there was a race condition
1106 * between timeout and abts completion, and abts
1107 * completion happens just in time.
1109 BNX2FC_IO_DBG(io_req, "abort succeeded\n");
1111 bnx2fc_scsi_done(io_req, DID_ABORT);
1112 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1115 /* release the reference taken in eh_abort */
1116 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1117 spin_unlock_bh(&tgt->tgt_lock);
1121 void bnx2fc_process_cleanup_compl(struct bnx2fc_cmd *io_req,
1122 struct fcoe_task_ctx_entry *task,
1125 BNX2FC_IO_DBG(io_req, "Entered process_cleanup_compl "
1126 "refcnt = %d, cmd_type = %d\n",
1127 io_req->refcount.refcount.counter, io_req->cmd_type);
1128 bnx2fc_scsi_done(io_req, DID_ERROR);
1129 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1132 void bnx2fc_process_abts_compl(struct bnx2fc_cmd *io_req,
1133 struct fcoe_task_ctx_entry *task,
1137 u32 r_a_tov = FC_DEF_R_A_TOV;
1139 struct bnx2fc_rport *tgt = io_req->tgt;
1141 BNX2FC_IO_DBG(io_req, "Entered process_abts_compl xid = 0x%x"
1142 "refcnt = %d, cmd_type = %d\n",
1144 io_req->refcount.refcount.counter, io_req->cmd_type);
1146 if (test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
1147 &io_req->req_flags)) {
1148 BNX2FC_IO_DBG(io_req, "Timer context finished processing"
1153 /* Do not issue RRQ as this IO is already cleanedup */
1154 if (test_and_set_bit(BNX2FC_FLAG_IO_CLEANUP,
1155 &io_req->req_flags))
1159 * For ABTS issued due to SCSI eh_abort_handler, timeout
1160 * values are maintained by scsi-ml itself. Cancel timeout
1161 * in case ABTS issued as part of task management function
1162 * or due to FW error.
1164 if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags))
1165 if (cancel_delayed_work(&io_req->timeout_work))
1166 kref_put(&io_req->refcount,
1167 bnx2fc_cmd_release); /* drop timer hold */
1169 r_ctl = task->cmn.general.rsp_info.abts_rsp.r_ctl;
1172 case FC_RCTL_BA_ACC:
1174 * Dont release this cmd yet. It will be relesed
1175 * after we get RRQ response
1177 BNX2FC_IO_DBG(io_req, "ABTS response - ACC Send RRQ\n");
1181 case FC_RCTL_BA_RJT:
1182 BNX2FC_IO_DBG(io_req, "ABTS response - RJT\n");
1185 printk(KERN_ERR PFX "Unknown ABTS response\n");
1190 BNX2FC_IO_DBG(io_req, "Issue RRQ after R_A_TOV\n");
1191 set_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags);
1193 set_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags);
1194 bnx2fc_cmd_timer_set(io_req, r_a_tov);
1197 if (io_req->wait_for_comp) {
1198 if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
1199 &io_req->req_flags))
1200 complete(&io_req->tm_done);
1203 * We end up here when ABTS is issued as
1204 * in asynchronous context, i.e., as part
1205 * of task management completion, or
1206 * when FW error is received or when the
1207 * ABTS is issued when the IO is timed
1211 if (io_req->on_active_queue) {
1212 list_del_init(&io_req->link);
1213 io_req->on_active_queue = 0;
1214 /* Move IO req to retire queue */
1215 list_add_tail(&io_req->link, &tgt->io_retire_queue);
1217 bnx2fc_scsi_done(io_req, DID_ERROR);
1218 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1222 static void bnx2fc_lun_reset_cmpl(struct bnx2fc_cmd *io_req)
1224 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1225 struct bnx2fc_rport *tgt = io_req->tgt;
1226 struct list_head *list;
1227 struct list_head *tmp;
1228 struct bnx2fc_cmd *cmd;
1229 int tm_lun = sc_cmd->device->lun;
1233 /* called with tgt_lock held */
1234 BNX2FC_IO_DBG(io_req, "Entered bnx2fc_lun_reset_cmpl\n");
1236 * Walk thru the active_ios queue and ABORT the IO
1237 * that matches with the LUN that was reset
1239 list_for_each_safe(list, tmp, &tgt->active_cmd_queue) {
1240 BNX2FC_TGT_DBG(tgt, "LUN RST cmpl: scan for pending IOs\n");
1241 cmd = (struct bnx2fc_cmd *)list;
1242 lun = cmd->sc_cmd->device->lun;
1243 if (lun == tm_lun) {
1244 /* Initiate ABTS on this cmd */
1245 if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
1247 /* cancel the IO timeout */
1248 if (cancel_delayed_work(&io_req->timeout_work))
1249 kref_put(&io_req->refcount,
1250 bnx2fc_cmd_release);
1252 rc = bnx2fc_initiate_abts(cmd);
1253 /* abts shouldnt fail in this context */
1254 WARN_ON(rc != SUCCESS);
1256 printk(KERN_ERR PFX "lun_rst: abts already in"
1257 " progress for this IO 0x%x\n",
1263 static void bnx2fc_tgt_reset_cmpl(struct bnx2fc_cmd *io_req)
1265 struct bnx2fc_rport *tgt = io_req->tgt;
1266 struct list_head *list;
1267 struct list_head *tmp;
1268 struct bnx2fc_cmd *cmd;
1271 /* called with tgt_lock held */
1272 BNX2FC_IO_DBG(io_req, "Entered bnx2fc_tgt_reset_cmpl\n");
1274 * Walk thru the active_ios queue and ABORT the IO
1275 * that matches with the LUN that was reset
1277 list_for_each_safe(list, tmp, &tgt->active_cmd_queue) {
1278 BNX2FC_TGT_DBG(tgt, "TGT RST cmpl: scan for pending IOs\n");
1279 cmd = (struct bnx2fc_cmd *)list;
1281 if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
1283 /* cancel the IO timeout */
1284 if (cancel_delayed_work(&io_req->timeout_work))
1285 kref_put(&io_req->refcount,
1286 bnx2fc_cmd_release); /* timer hold */
1287 rc = bnx2fc_initiate_abts(cmd);
1288 /* abts shouldnt fail in this context */
1289 WARN_ON(rc != SUCCESS);
1292 printk(KERN_ERR PFX "tgt_rst: abts already in progress"
1293 " for this IO 0x%x\n", cmd->xid);
1297 void bnx2fc_process_tm_compl(struct bnx2fc_cmd *io_req,
1298 struct fcoe_task_ctx_entry *task, u8 num_rq)
1300 struct bnx2fc_mp_req *tm_req;
1301 struct fc_frame_header *fc_hdr;
1302 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1307 /* Called with tgt_lock held */
1308 BNX2FC_IO_DBG(io_req, "Entered process_tm_compl\n");
1310 if (!(test_bit(BNX2FC_FLAG_TM_TIMEOUT, &io_req->req_flags)))
1311 set_bit(BNX2FC_FLAG_TM_COMPL, &io_req->req_flags);
1313 /* TM has already timed out and we got
1314 * delayed completion. Ignore completion
1320 tm_req = &(io_req->mp_req);
1321 fc_hdr = &(tm_req->resp_fc_hdr);
1322 hdr = (u64 *)fc_hdr;
1324 &task->cmn.general.cmd_info.mp_fc_frame.fc_hdr;
1325 hdr[0] = cpu_to_be64(temp_hdr[0]);
1326 hdr[1] = cpu_to_be64(temp_hdr[1]);
1327 hdr[2] = cpu_to_be64(temp_hdr[2]);
1329 tm_req->resp_len = task->rx_wr_only.sgl_ctx.mul_sges.cur_sge_off;
1331 rsp_buf = tm_req->resp_buf;
1333 if (fc_hdr->fh_r_ctl == FC_RCTL_DD_CMD_STATUS) {
1334 bnx2fc_parse_fcp_rsp(io_req,
1335 (struct fcoe_fcp_rsp_payload *)
1337 if (io_req->fcp_rsp_code == 0) {
1339 if (tm_req->tm_flags & FCP_TMF_LUN_RESET)
1340 bnx2fc_lun_reset_cmpl(io_req);
1341 else if (tm_req->tm_flags & FCP_TMF_TGT_RESET)
1342 bnx2fc_tgt_reset_cmpl(io_req);
1345 printk(KERN_ERR PFX "tmf's fc_hdr r_ctl = 0x%x\n",
1348 if (!sc_cmd->SCp.ptr) {
1349 printk(KERN_ALERT PFX "tm_compl: SCp.ptr is NULL\n");
1352 switch (io_req->fcp_status) {
1354 if (io_req->cdb_status == 0) {
1355 /* Good IO completion */
1356 sc_cmd->result = DID_OK << 16;
1358 /* Transport status is good, SCSI status not good */
1359 sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
1361 if (io_req->fcp_resid)
1362 scsi_set_resid(sc_cmd, io_req->fcp_resid);
1366 BNX2FC_IO_DBG(io_req, "process_tm_compl: fcp_status = %d\n",
1367 io_req->fcp_status);
1371 sc_cmd = io_req->sc_cmd;
1372 io_req->sc_cmd = NULL;
1374 /* check if the io_req exists in tgt's tmf_q */
1375 if (io_req->on_tmf_queue) {
1377 list_del_init(&io_req->link);
1378 io_req->on_tmf_queue = 0;
1381 printk(KERN_ALERT PFX "Command not on active_cmd_queue!\n");
1385 sc_cmd->SCp.ptr = NULL;
1386 sc_cmd->scsi_done(sc_cmd);
1388 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1389 if (io_req->wait_for_comp) {
1390 BNX2FC_IO_DBG(io_req, "tm_compl - wake up the waiter\n");
1391 complete(&io_req->tm_done);
1395 static int bnx2fc_split_bd(struct bnx2fc_cmd *io_req, u64 addr, int sg_len,
1398 struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
1399 int frag_size, sg_frags;
1403 if (sg_len >= BNX2FC_BD_SPLIT_SZ)
1404 frag_size = BNX2FC_BD_SPLIT_SZ;
1407 bd[bd_index + sg_frags].buf_addr_lo = addr & 0xffffffff;
1408 bd[bd_index + sg_frags].buf_addr_hi = addr >> 32;
1409 bd[bd_index + sg_frags].buf_len = (u16)frag_size;
1410 bd[bd_index + sg_frags].flags = 0;
1412 addr += (u64) frag_size;
1414 sg_len -= frag_size;
1420 static int bnx2fc_map_sg(struct bnx2fc_cmd *io_req)
1422 struct scsi_cmnd *sc = io_req->sc_cmd;
1423 struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
1424 struct scatterlist *sg;
1429 unsigned int sg_len;
1433 sg_count = scsi_dma_map(sc);
1434 scsi_for_each_sg(sc, sg, sg_count, i) {
1435 sg_len = sg_dma_len(sg);
1436 addr = sg_dma_address(sg);
1437 if (sg_len > BNX2FC_MAX_BD_LEN) {
1438 sg_frags = bnx2fc_split_bd(io_req, addr, sg_len,
1443 bd[bd_count].buf_addr_lo = addr & 0xffffffff;
1444 bd[bd_count].buf_addr_hi = addr >> 32;
1445 bd[bd_count].buf_len = (u16)sg_len;
1446 bd[bd_count].flags = 0;
1448 bd_count += sg_frags;
1449 byte_count += sg_len;
1451 if (byte_count != scsi_bufflen(sc))
1452 printk(KERN_ERR PFX "byte_count = %d != scsi_bufflen = %d, "
1453 "task_id = 0x%x\n", byte_count, scsi_bufflen(sc),
1458 static void bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd *io_req)
1460 struct scsi_cmnd *sc = io_req->sc_cmd;
1461 struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
1464 if (scsi_sg_count(sc))
1465 bd_count = bnx2fc_map_sg(io_req);
1468 bd[0].buf_addr_lo = bd[0].buf_addr_hi = 0;
1469 bd[0].buf_len = bd[0].flags = 0;
1471 io_req->bd_tbl->bd_valid = bd_count;
1474 static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd *io_req)
1476 struct scsi_cmnd *sc = io_req->sc_cmd;
1478 if (io_req->bd_tbl->bd_valid && sc) {
1480 io_req->bd_tbl->bd_valid = 0;
1484 void bnx2fc_build_fcp_cmnd(struct bnx2fc_cmd *io_req,
1485 struct fcp_cmnd *fcp_cmnd)
1487 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1490 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
1492 int_to_scsilun(sc_cmd->device->lun,
1493 (struct scsi_lun *) fcp_cmnd->fc_lun);
1496 fcp_cmnd->fc_dl = htonl(io_req->data_xfer_len);
1497 memcpy(fcp_cmnd->fc_cdb, sc_cmd->cmnd, sc_cmd->cmd_len);
1499 fcp_cmnd->fc_cmdref = 0;
1500 fcp_cmnd->fc_pri_ta = 0;
1501 fcp_cmnd->fc_tm_flags = io_req->mp_req.tm_flags;
1502 fcp_cmnd->fc_flags = io_req->io_req_flags;
1504 if (scsi_populate_tag_msg(sc_cmd, tag)) {
1506 case HEAD_OF_QUEUE_TAG:
1507 fcp_cmnd->fc_pri_ta = FCP_PTA_HEADQ;
1509 case ORDERED_QUEUE_TAG:
1510 fcp_cmnd->fc_pri_ta = FCP_PTA_ORDERED;
1513 fcp_cmnd->fc_pri_ta = FCP_PTA_SIMPLE;
1517 fcp_cmnd->fc_pri_ta = 0;
1521 static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd *io_req,
1522 struct fcoe_fcp_rsp_payload *fcp_rsp,
1525 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1526 struct bnx2fc_rport *tgt = io_req->tgt;
1527 u8 rsp_flags = fcp_rsp->fcp_flags.flags;
1528 u32 rq_buff_len = 0;
1530 unsigned char *rq_data;
1531 unsigned char *dummy;
1532 int fcp_sns_len = 0;
1533 int fcp_rsp_len = 0;
1535 io_req->fcp_status = FC_GOOD;
1536 io_req->fcp_resid = fcp_rsp->fcp_resid;
1538 io_req->scsi_comp_flags = rsp_flags;
1539 CMD_SCSI_STATUS(sc_cmd) = io_req->cdb_status =
1540 fcp_rsp->scsi_status_code;
1542 /* Fetch fcp_rsp_info and fcp_sns_info if available */
1546 * We do not anticipate num_rq >1, as the linux defined
1547 * SCSI_SENSE_BUFFERSIZE is 96 bytes + 8 bytes of FCP_RSP_INFO
1548 * 256 bytes of single rq buffer is good enough to hold this.
1552 FCOE_FCP_RSP_FLAGS_FCP_RSP_LEN_VALID) {
1553 fcp_rsp_len = rq_buff_len
1554 = fcp_rsp->fcp_rsp_len;
1558 FCOE_FCP_RSP_FLAGS_FCP_SNS_LEN_VALID) {
1559 fcp_sns_len = fcp_rsp->fcp_sns_len;
1560 rq_buff_len += fcp_rsp->fcp_sns_len;
1563 io_req->fcp_rsp_len = fcp_rsp_len;
1564 io_req->fcp_sns_len = fcp_sns_len;
1566 if (rq_buff_len > num_rq * BNX2FC_RQ_BUF_SZ) {
1567 /* Invalid sense sense length. */
1568 printk(KERN_ALERT PFX "invalid sns length %d\n",
1570 /* reset rq_buff_len */
1571 rq_buff_len = num_rq * BNX2FC_RQ_BUF_SZ;
1574 rq_data = bnx2fc_get_next_rqe(tgt, 1);
1577 /* We do not need extra sense data */
1578 for (i = 1; i < num_rq; i++)
1579 dummy = bnx2fc_get_next_rqe(tgt, 1);
1582 /* fetch fcp_rsp_code */
1583 if ((fcp_rsp_len == 4) || (fcp_rsp_len == 8)) {
1584 /* Only for task management function */
1585 io_req->fcp_rsp_code = rq_data[3];
1586 printk(KERN_ERR PFX "fcp_rsp_code = %d\n",
1587 io_req->fcp_rsp_code);
1590 /* fetch sense data */
1591 rq_data += fcp_rsp_len;
1593 if (fcp_sns_len > SCSI_SENSE_BUFFERSIZE) {
1594 printk(KERN_ERR PFX "Truncating sense buffer\n");
1595 fcp_sns_len = SCSI_SENSE_BUFFERSIZE;
1598 memset(sc_cmd->sense_buffer, 0, sizeof(sc_cmd->sense_buffer));
1600 memcpy(sc_cmd->sense_buffer, rq_data, fcp_sns_len);
1602 /* return RQ entries */
1603 for (i = 0; i < num_rq; i++)
1604 bnx2fc_return_rqe(tgt, 1);
1609 * bnx2fc_queuecommand - Queuecommand function of the scsi template
1611 * @host: The Scsi_Host the command was issued to
1612 * @sc_cmd: struct scsi_cmnd to be executed
1614 * This is the IO strategy routine, called by SCSI-ML
1616 int bnx2fc_queuecommand(struct Scsi_Host *host,
1617 struct scsi_cmnd *sc_cmd)
1619 struct fc_lport *lport = shost_priv(host);
1620 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1621 struct fc_rport_libfc_priv *rp = rport->dd_data;
1622 struct bnx2fc_rport *tgt;
1623 struct bnx2fc_cmd *io_req;
1627 rval = fc_remote_port_chkready(rport);
1629 sc_cmd->result = rval;
1630 sc_cmd->scsi_done(sc_cmd);
1634 if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
1635 rc = SCSI_MLQUEUE_HOST_BUSY;
1639 /* rport and tgt are allocated together, so tgt should be non-NULL */
1640 tgt = (struct bnx2fc_rport *)&rp[1];
1642 if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
1644 * Session is not offloaded yet. Let SCSI-ml retry
1647 rc = SCSI_MLQUEUE_TARGET_BUSY;
1651 io_req = bnx2fc_cmd_alloc(tgt);
1653 rc = SCSI_MLQUEUE_HOST_BUSY;
1656 io_req->sc_cmd = sc_cmd;
1658 if (bnx2fc_post_io_req(tgt, io_req)) {
1659 printk(KERN_ERR PFX "Unable to post io_req\n");
1660 rc = SCSI_MLQUEUE_HOST_BUSY;
1667 void bnx2fc_process_scsi_cmd_compl(struct bnx2fc_cmd *io_req,
1668 struct fcoe_task_ctx_entry *task,
1671 struct fcoe_fcp_rsp_payload *fcp_rsp;
1672 struct bnx2fc_rport *tgt = io_req->tgt;
1673 struct scsi_cmnd *sc_cmd;
1674 struct Scsi_Host *host;
1677 /* scsi_cmd_cmpl is called with tgt lock held */
1679 if (test_and_set_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags)) {
1680 /* we will not receive ABTS response for this IO */
1681 BNX2FC_IO_DBG(io_req, "Timer context finished processing "
1685 /* Cancel the timeout_work, as we received IO completion */
1686 if (cancel_delayed_work(&io_req->timeout_work))
1687 kref_put(&io_req->refcount,
1688 bnx2fc_cmd_release); /* drop timer hold */
1690 sc_cmd = io_req->sc_cmd;
1691 if (sc_cmd == NULL) {
1692 printk(KERN_ERR PFX "scsi_cmd_compl - sc_cmd is NULL\n");
1696 /* Fetch fcp_rsp from task context and perform cmd completion */
1697 fcp_rsp = (struct fcoe_fcp_rsp_payload *)
1698 &(task->cmn.general.rsp_info.fcp_rsp.payload);
1700 /* parse fcp_rsp and obtain sense data from RQ if available */
1701 bnx2fc_parse_fcp_rsp(io_req, fcp_rsp, num_rq);
1703 host = sc_cmd->device->host;
1704 if (!sc_cmd->SCp.ptr) {
1705 printk(KERN_ERR PFX "SCp.ptr is NULL\n");
1708 io_req->sc_cmd = NULL;
1710 if (io_req->on_active_queue) {
1711 list_del_init(&io_req->link);
1712 io_req->on_active_queue = 0;
1713 /* Move IO req to retire queue */
1714 list_add_tail(&io_req->link, &tgt->io_retire_queue);
1716 /* This should not happen, but could have been pulled
1717 * by bnx2fc_flush_active_ios(), or during a race
1718 * between command abort and (late) completion.
1720 BNX2FC_IO_DBG(io_req, "xid not on active_cmd_queue\n");
1721 if (io_req->wait_for_comp)
1722 if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
1723 &io_req->req_flags))
1724 complete(&io_req->tm_done);
1727 bnx2fc_unmap_sg_list(io_req);
1729 switch (io_req->fcp_status) {
1731 if (io_req->cdb_status == 0) {
1732 /* Good IO completion */
1733 sc_cmd->result = DID_OK << 16;
1735 /* Transport status is good, SCSI status not good */
1736 BNX2FC_IO_DBG(io_req, "scsi_cmpl: cdb_status = %d"
1737 " fcp_resid = 0x%x\n",
1738 io_req->cdb_status, io_req->fcp_resid);
1739 sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
1741 if (io_req->fcp_resid)
1742 scsi_set_resid(sc_cmd, io_req->fcp_resid);
1745 printk(KERN_ALERT PFX "scsi_cmd_compl: fcp_status = %d\n",
1746 io_req->fcp_status);
1749 sc_cmd->SCp.ptr = NULL;
1750 sc_cmd->scsi_done(sc_cmd);
1751 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1754 static int bnx2fc_post_io_req(struct bnx2fc_rport *tgt,
1755 struct bnx2fc_cmd *io_req)
1757 struct fcoe_task_ctx_entry *task;
1758 struct fcoe_task_ctx_entry *task_page;
1759 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1760 struct fcoe_port *port = tgt->port;
1761 struct bnx2fc_hba *hba = port->priv;
1762 struct fc_lport *lport = port->lport;
1763 struct fcoe_dev_stats *stats;
1764 int task_idx, index;
1767 /* Initialize rest of io_req fields */
1768 io_req->cmd_type = BNX2FC_SCSI_CMD;
1769 io_req->port = port;
1771 io_req->data_xfer_len = scsi_bufflen(sc_cmd);
1772 sc_cmd->SCp.ptr = (char *)io_req;
1774 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1775 if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
1776 io_req->io_req_flags = BNX2FC_READ;
1777 stats->InputRequests++;
1778 stats->InputBytes += io_req->data_xfer_len;
1779 } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
1780 io_req->io_req_flags = BNX2FC_WRITE;
1781 stats->OutputRequests++;
1782 stats->OutputBytes += io_req->data_xfer_len;
1784 io_req->io_req_flags = 0;
1785 stats->ControlRequests++;
1791 /* Build buffer descriptor list for firmware from sg list */
1792 bnx2fc_build_bd_list_from_sg(io_req);
1794 task_idx = xid / BNX2FC_TASKS_PER_PAGE;
1795 index = xid % BNX2FC_TASKS_PER_PAGE;
1797 /* Initialize task context for this IO request */
1798 task_page = (struct fcoe_task_ctx_entry *) hba->task_ctx[task_idx];
1799 task = &(task_page[index]);
1800 bnx2fc_init_task(io_req, task);
1802 spin_lock_bh(&tgt->tgt_lock);
1804 if (tgt->flush_in_prog) {
1805 printk(KERN_ERR PFX "Flush in progress..Host Busy\n");
1806 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1807 spin_unlock_bh(&tgt->tgt_lock);
1811 if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
1812 printk(KERN_ERR PFX "Session not ready...post_io\n");
1813 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1814 spin_unlock_bh(&tgt->tgt_lock);
1819 bnx2fc_cmd_timer_set(io_req, BNX2FC_IO_TIMEOUT);
1820 /* Obtain free SQ entry */
1821 bnx2fc_add_2_sq(tgt, xid);
1823 /* Enqueue the io_req to active_cmd_queue */
1825 io_req->on_active_queue = 1;
1826 /* move io_req from pending_queue to active_queue */
1827 list_add_tail(&io_req->link, &tgt->active_cmd_queue);
1830 bnx2fc_ring_doorbell(tgt);
1831 spin_unlock_bh(&tgt->tgt_lock);