1 /* bnx2fc_io.c: QLogic NetXtreme II Linux FCoE offload driver.
2 * IO manager and SCSI IO processing.
4 * Copyright (c) 2008 - 2013 Broadcom Corporation
5 * Copyright (c) 2014, QLogic Corporation
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation.
11 * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
16 #define RESERVE_FREE_LIST_INDEX num_possible_cpus()
18 static int bnx2fc_split_bd(struct bnx2fc_cmd
*io_req
, u64 addr
, int sg_len
,
20 static int bnx2fc_map_sg(struct bnx2fc_cmd
*io_req
);
21 static int bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd
*io_req
);
22 static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd
*io_req
);
23 static void bnx2fc_free_mp_resc(struct bnx2fc_cmd
*io_req
);
24 static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd
*io_req
,
25 struct fcoe_fcp_rsp_payload
*fcp_rsp
,
28 void bnx2fc_cmd_timer_set(struct bnx2fc_cmd
*io_req
,
29 unsigned int timer_msec
)
31 struct bnx2fc_interface
*interface
= io_req
->port
->priv
;
33 if (queue_delayed_work(interface
->timer_work_queue
,
34 &io_req
->timeout_work
,
35 msecs_to_jiffies(timer_msec
)))
36 kref_get(&io_req
->refcount
);
39 static void bnx2fc_cmd_timeout(struct work_struct
*work
)
41 struct bnx2fc_cmd
*io_req
= container_of(work
, struct bnx2fc_cmd
,
43 struct fc_lport
*lport
;
44 struct fc_rport_priv
*rdata
;
45 u8 cmd_type
= io_req
->cmd_type
;
46 struct bnx2fc_rport
*tgt
= io_req
->tgt
;
50 BNX2FC_IO_DBG(io_req
, "cmd_timeout, cmd_type = %d,"
51 "req_flags = %lx\n", cmd_type
, io_req
->req_flags
);
53 spin_lock_bh(&tgt
->tgt_lock
);
54 if (test_and_clear_bit(BNX2FC_FLAG_ISSUE_RRQ
, &io_req
->req_flags
)) {
55 clear_bit(BNX2FC_FLAG_RETIRE_OXID
, &io_req
->req_flags
);
57 * ideally we should hold the io_req until RRQ complets,
58 * and release io_req from timeout hold.
60 spin_unlock_bh(&tgt
->tgt_lock
);
61 bnx2fc_send_rrq(io_req
);
64 if (test_and_clear_bit(BNX2FC_FLAG_RETIRE_OXID
, &io_req
->req_flags
)) {
65 BNX2FC_IO_DBG(io_req
, "IO ready for reuse now\n");
71 if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT
,
72 &io_req
->req_flags
)) {
73 /* Handle eh_abort timeout */
74 BNX2FC_IO_DBG(io_req
, "eh_abort timed out\n");
75 complete(&io_req
->tm_done
);
76 } else if (test_bit(BNX2FC_FLAG_ISSUE_ABTS
,
77 &io_req
->req_flags
)) {
78 /* Handle internally generated ABTS timeout */
79 BNX2FC_IO_DBG(io_req
, "ABTS timed out refcnt = %d\n",
80 io_req
->refcount
.refcount
.counter
);
81 if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE
,
82 &io_req
->req_flags
))) {
84 lport
= io_req
->port
->lport
;
85 rdata
= io_req
->tgt
->rdata
;
86 logo_issued
= test_and_set_bit(
87 BNX2FC_FLAG_EXPL_LOGO
,
89 kref_put(&io_req
->refcount
, bnx2fc_cmd_release
);
90 spin_unlock_bh(&tgt
->tgt_lock
);
92 /* Explicitly logo the target */
94 BNX2FC_IO_DBG(io_req
, "Explicit "
95 "logo - tgt flags = 0x%lx\n",
98 mutex_lock(&lport
->disc
.disc_mutex
);
99 lport
->tt
.rport_logoff(rdata
);
100 mutex_unlock(&lport
->disc
.disc_mutex
);
105 /* Hanlde IO timeout */
106 BNX2FC_IO_DBG(io_req
, "IO timed out. issue ABTS\n");
107 if (test_and_set_bit(BNX2FC_FLAG_IO_COMPL
,
108 &io_req
->req_flags
)) {
109 BNX2FC_IO_DBG(io_req
, "IO completed before "
114 if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS
,
115 &io_req
->req_flags
)) {
116 rc
= bnx2fc_initiate_abts(io_req
);
120 * Explicitly logo the target if
121 * abts initiation fails
123 lport
= io_req
->port
->lport
;
124 rdata
= io_req
->tgt
->rdata
;
125 logo_issued
= test_and_set_bit(
126 BNX2FC_FLAG_EXPL_LOGO
,
128 kref_put(&io_req
->refcount
, bnx2fc_cmd_release
);
129 spin_unlock_bh(&tgt
->tgt_lock
);
132 BNX2FC_IO_DBG(io_req
, "Explicit "
133 "logo - tgt flags = 0x%lx\n",
137 mutex_lock(&lport
->disc
.disc_mutex
);
138 lport
->tt
.rport_logoff(rdata
);
139 mutex_unlock(&lport
->disc
.disc_mutex
);
143 BNX2FC_IO_DBG(io_req
, "IO already in "
144 "ABTS processing\n");
150 if (test_bit(BNX2FC_FLAG_ISSUE_ABTS
, &io_req
->req_flags
)) {
151 BNX2FC_IO_DBG(io_req
, "ABTS for ELS timed out\n");
153 if (!test_and_set_bit(BNX2FC_FLAG_ABTS_DONE
,
154 &io_req
->req_flags
)) {
155 lport
= io_req
->port
->lport
;
156 rdata
= io_req
->tgt
->rdata
;
157 logo_issued
= test_and_set_bit(
158 BNX2FC_FLAG_EXPL_LOGO
,
160 kref_put(&io_req
->refcount
, bnx2fc_cmd_release
);
161 spin_unlock_bh(&tgt
->tgt_lock
);
163 /* Explicitly logo the target */
165 BNX2FC_IO_DBG(io_req
, "Explicitly logo"
167 mutex_lock(&lport
->disc
.disc_mutex
);
168 lport
->tt
.rport_logoff(rdata
);
169 mutex_unlock(&lport
->disc
.disc_mutex
);
175 * Handle ELS timeout.
176 * tgt_lock is used to sync compl path and timeout
177 * path. If els compl path is processing this IO, we
178 * have nothing to do here, just release the timer hold
180 BNX2FC_IO_DBG(io_req
, "ELS timed out\n");
181 if (test_and_set_bit(BNX2FC_FLAG_ELS_DONE
,
185 /* Indicate the cb_func that this ELS is timed out */
186 set_bit(BNX2FC_FLAG_ELS_TIMEOUT
, &io_req
->req_flags
);
188 if ((io_req
->cb_func
) && (io_req
->cb_arg
)) {
189 io_req
->cb_func(io_req
->cb_arg
);
190 io_req
->cb_arg
= NULL
;
195 printk(KERN_ERR PFX
"cmd_timeout: invalid cmd_type %d\n",
201 /* release the cmd that was held when timer was set */
202 kref_put(&io_req
->refcount
, bnx2fc_cmd_release
);
203 spin_unlock_bh(&tgt
->tgt_lock
);
206 static void bnx2fc_scsi_done(struct bnx2fc_cmd
*io_req
, int err_code
)
208 /* Called with host lock held */
209 struct scsi_cmnd
*sc_cmd
= io_req
->sc_cmd
;
212 * active_cmd_queue may have other command types as well,
213 * and during flush operation, we want to error back only
216 if (io_req
->cmd_type
!= BNX2FC_SCSI_CMD
)
219 BNX2FC_IO_DBG(io_req
, "scsi_done. err_code = 0x%x\n", err_code
);
220 if (test_bit(BNX2FC_FLAG_CMD_LOST
, &io_req
->req_flags
)) {
221 /* Do not call scsi done for this IO */
225 bnx2fc_unmap_sg_list(io_req
);
226 io_req
->sc_cmd
= NULL
;
228 printk(KERN_ERR PFX
"scsi_done - sc_cmd NULL. "
229 "IO(0x%x) already cleaned up\n",
233 sc_cmd
->result
= err_code
<< 16;
235 BNX2FC_IO_DBG(io_req
, "sc=%p, result=0x%x, retries=%d, allowed=%d\n",
236 sc_cmd
, host_byte(sc_cmd
->result
), sc_cmd
->retries
,
238 scsi_set_resid(sc_cmd
, scsi_bufflen(sc_cmd
));
239 sc_cmd
->SCp
.ptr
= NULL
;
240 sc_cmd
->scsi_done(sc_cmd
);
243 struct bnx2fc_cmd_mgr
*bnx2fc_cmd_mgr_alloc(struct bnx2fc_hba
*hba
)
245 struct bnx2fc_cmd_mgr
*cmgr
;
246 struct io_bdt
*bdt_info
;
247 struct bnx2fc_cmd
*io_req
;
252 int num_ios
, num_pri_ios
;
254 int arr_sz
= num_possible_cpus() + 1;
255 u16 min_xid
= BNX2FC_MIN_XID
;
256 u16 max_xid
= hba
->max_xid
;
258 if (max_xid
<= min_xid
|| max_xid
== FC_XID_UNKNOWN
) {
259 printk(KERN_ERR PFX
"cmd_mgr_alloc: Invalid min_xid 0x%x \
260 and max_xid 0x%x\n", min_xid
, max_xid
);
263 BNX2FC_MISC_DBG("min xid 0x%x, max xid 0x%x\n", min_xid
, max_xid
);
265 num_ios
= max_xid
- min_xid
+ 1;
266 len
= (num_ios
* (sizeof(struct bnx2fc_cmd
*)));
267 len
+= sizeof(struct bnx2fc_cmd_mgr
);
269 cmgr
= kzalloc(len
, GFP_KERNEL
);
271 printk(KERN_ERR PFX
"failed to alloc cmgr\n");
275 cmgr
->free_list
= kzalloc(sizeof(*cmgr
->free_list
) *
277 if (!cmgr
->free_list
) {
278 printk(KERN_ERR PFX
"failed to alloc free_list\n");
282 cmgr
->free_list_lock
= kzalloc(sizeof(*cmgr
->free_list_lock
) *
284 if (!cmgr
->free_list_lock
) {
285 printk(KERN_ERR PFX
"failed to alloc free_list_lock\n");
286 kfree(cmgr
->free_list
);
287 cmgr
->free_list
= NULL
;
292 cmgr
->cmds
= (struct bnx2fc_cmd
**)(cmgr
+ 1);
294 for (i
= 0; i
< arr_sz
; i
++) {
295 INIT_LIST_HEAD(&cmgr
->free_list
[i
]);
296 spin_lock_init(&cmgr
->free_list_lock
[i
]);
300 * Pre-allocated pool of bnx2fc_cmds.
301 * Last entry in the free list array is the free list
302 * of slow path requests.
304 xid
= BNX2FC_MIN_XID
;
305 num_pri_ios
= num_ios
- hba
->elstm_xids
;
306 for (i
= 0; i
< num_ios
; i
++) {
307 io_req
= kzalloc(sizeof(*io_req
), GFP_KERNEL
);
310 printk(KERN_ERR PFX
"failed to alloc io_req\n");
314 INIT_LIST_HEAD(&io_req
->link
);
315 INIT_DELAYED_WORK(&io_req
->timeout_work
, bnx2fc_cmd_timeout
);
319 list_add_tail(&io_req
->link
,
320 &cmgr
->free_list
[io_req
->xid
%
321 num_possible_cpus()]);
323 list_add_tail(&io_req
->link
,
324 &cmgr
->free_list
[num_possible_cpus()]);
328 /* Allocate pool of io_bdts - one for each bnx2fc_cmd */
329 mem_size
= num_ios
* sizeof(struct io_bdt
*);
330 cmgr
->io_bdt_pool
= kmalloc(mem_size
, GFP_KERNEL
);
331 if (!cmgr
->io_bdt_pool
) {
332 printk(KERN_ERR PFX
"failed to alloc io_bdt_pool\n");
336 mem_size
= sizeof(struct io_bdt
);
337 for (i
= 0; i
< num_ios
; i
++) {
338 cmgr
->io_bdt_pool
[i
] = kmalloc(mem_size
, GFP_KERNEL
);
339 if (!cmgr
->io_bdt_pool
[i
]) {
340 printk(KERN_ERR PFX
"failed to alloc "
341 "io_bdt_pool[%d]\n", i
);
346 /* Allocate an map fcoe_bdt_ctx structures */
347 bd_tbl_sz
= BNX2FC_MAX_BDS_PER_CMD
* sizeof(struct fcoe_bd_ctx
);
348 for (i
= 0; i
< num_ios
; i
++) {
349 bdt_info
= cmgr
->io_bdt_pool
[i
];
350 bdt_info
->bd_tbl
= dma_alloc_coherent(&hba
->pcidev
->dev
,
352 &bdt_info
->bd_tbl_dma
,
354 if (!bdt_info
->bd_tbl
) {
355 printk(KERN_ERR PFX
"failed to alloc "
364 bnx2fc_cmd_mgr_free(cmgr
);
368 void bnx2fc_cmd_mgr_free(struct bnx2fc_cmd_mgr
*cmgr
)
370 struct io_bdt
*bdt_info
;
371 struct bnx2fc_hba
*hba
= cmgr
->hba
;
373 u16 min_xid
= BNX2FC_MIN_XID
;
374 u16 max_xid
= hba
->max_xid
;
378 num_ios
= max_xid
- min_xid
+ 1;
380 /* Free fcoe_bdt_ctx structures */
381 if (!cmgr
->io_bdt_pool
)
384 bd_tbl_sz
= BNX2FC_MAX_BDS_PER_CMD
* sizeof(struct fcoe_bd_ctx
);
385 for (i
= 0; i
< num_ios
; i
++) {
386 bdt_info
= cmgr
->io_bdt_pool
[i
];
387 if (bdt_info
->bd_tbl
) {
388 dma_free_coherent(&hba
->pcidev
->dev
, bd_tbl_sz
,
390 bdt_info
->bd_tbl_dma
);
391 bdt_info
->bd_tbl
= NULL
;
395 /* Destroy io_bdt pool */
396 for (i
= 0; i
< num_ios
; i
++) {
397 kfree(cmgr
->io_bdt_pool
[i
]);
398 cmgr
->io_bdt_pool
[i
] = NULL
;
401 kfree(cmgr
->io_bdt_pool
);
402 cmgr
->io_bdt_pool
= NULL
;
405 kfree(cmgr
->free_list_lock
);
407 /* Destroy cmd pool */
408 if (!cmgr
->free_list
)
411 for (i
= 0; i
< num_possible_cpus() + 1; i
++) {
412 struct bnx2fc_cmd
*tmp
, *io_req
;
414 list_for_each_entry_safe(io_req
, tmp
,
415 &cmgr
->free_list
[i
], link
) {
416 list_del(&io_req
->link
);
420 kfree(cmgr
->free_list
);
422 /* Free command manager itself */
426 struct bnx2fc_cmd
*bnx2fc_elstm_alloc(struct bnx2fc_rport
*tgt
, int type
)
428 struct fcoe_port
*port
= tgt
->port
;
429 struct bnx2fc_interface
*interface
= port
->priv
;
430 struct bnx2fc_cmd_mgr
*cmd_mgr
= interface
->hba
->cmd_mgr
;
431 struct bnx2fc_cmd
*io_req
;
432 struct list_head
*listp
;
433 struct io_bdt
*bd_tbl
;
434 int index
= RESERVE_FREE_LIST_INDEX
;
439 max_sqes
= tgt
->max_sqes
;
441 case BNX2FC_TASK_MGMT_CMD
:
442 max_sqes
= BNX2FC_TM_MAX_SQES
;
445 max_sqes
= BNX2FC_ELS_MAX_SQES
;
452 * NOTE: Free list insertions and deletions are protected with
455 spin_lock_bh(&cmd_mgr
->free_list_lock
[index
]);
456 free_sqes
= atomic_read(&tgt
->free_sqes
);
457 if ((list_empty(&(cmd_mgr
->free_list
[index
]))) ||
458 (tgt
->num_active_ios
.counter
>= max_sqes
) ||
459 (free_sqes
+ max_sqes
<= BNX2FC_SQ_WQES_MAX
)) {
460 BNX2FC_TGT_DBG(tgt
, "No free els_tm cmds available "
461 "ios(%d):sqes(%d)\n",
462 tgt
->num_active_ios
.counter
, tgt
->max_sqes
);
463 if (list_empty(&(cmd_mgr
->free_list
[index
])))
464 printk(KERN_ERR PFX
"elstm_alloc: list_empty\n");
465 spin_unlock_bh(&cmd_mgr
->free_list_lock
[index
]);
469 listp
= (struct list_head
*)
470 cmd_mgr
->free_list
[index
].next
;
471 list_del_init(listp
);
472 io_req
= (struct bnx2fc_cmd
*) listp
;
474 cmd_mgr
->cmds
[xid
] = io_req
;
475 atomic_inc(&tgt
->num_active_ios
);
476 atomic_dec(&tgt
->free_sqes
);
477 spin_unlock_bh(&cmd_mgr
->free_list_lock
[index
]);
479 INIT_LIST_HEAD(&io_req
->link
);
482 io_req
->cmd_mgr
= cmd_mgr
;
483 io_req
->req_flags
= 0;
484 io_req
->cmd_type
= type
;
486 /* Bind io_bdt for this io_req */
487 /* Have a static link between io_req and io_bdt_pool */
488 bd_tbl
= io_req
->bd_tbl
= cmd_mgr
->io_bdt_pool
[xid
];
489 bd_tbl
->io_req
= io_req
;
491 /* Hold the io_req against deletion */
492 kref_init(&io_req
->refcount
);
496 struct bnx2fc_cmd
*bnx2fc_cmd_alloc(struct bnx2fc_rport
*tgt
)
498 struct fcoe_port
*port
= tgt
->port
;
499 struct bnx2fc_interface
*interface
= port
->priv
;
500 struct bnx2fc_cmd_mgr
*cmd_mgr
= interface
->hba
->cmd_mgr
;
501 struct bnx2fc_cmd
*io_req
;
502 struct list_head
*listp
;
503 struct io_bdt
*bd_tbl
;
507 int index
= get_cpu();
509 max_sqes
= BNX2FC_SCSI_MAX_SQES
;
511 * NOTE: Free list insertions and deletions are protected with
514 spin_lock_bh(&cmd_mgr
->free_list_lock
[index
]);
515 free_sqes
= atomic_read(&tgt
->free_sqes
);
516 if ((list_empty(&cmd_mgr
->free_list
[index
])) ||
517 (tgt
->num_active_ios
.counter
>= max_sqes
) ||
518 (free_sqes
+ max_sqes
<= BNX2FC_SQ_WQES_MAX
)) {
519 spin_unlock_bh(&cmd_mgr
->free_list_lock
[index
]);
524 listp
= (struct list_head
*)
525 cmd_mgr
->free_list
[index
].next
;
526 list_del_init(listp
);
527 io_req
= (struct bnx2fc_cmd
*) listp
;
529 cmd_mgr
->cmds
[xid
] = io_req
;
530 atomic_inc(&tgt
->num_active_ios
);
531 atomic_dec(&tgt
->free_sqes
);
532 spin_unlock_bh(&cmd_mgr
->free_list_lock
[index
]);
535 INIT_LIST_HEAD(&io_req
->link
);
538 io_req
->cmd_mgr
= cmd_mgr
;
539 io_req
->req_flags
= 0;
541 /* Bind io_bdt for this io_req */
542 /* Have a static link between io_req and io_bdt_pool */
543 bd_tbl
= io_req
->bd_tbl
= cmd_mgr
->io_bdt_pool
[xid
];
544 bd_tbl
->io_req
= io_req
;
546 /* Hold the io_req against deletion */
547 kref_init(&io_req
->refcount
);
551 void bnx2fc_cmd_release(struct kref
*ref
)
553 struct bnx2fc_cmd
*io_req
= container_of(ref
,
554 struct bnx2fc_cmd
, refcount
);
555 struct bnx2fc_cmd_mgr
*cmd_mgr
= io_req
->cmd_mgr
;
558 if (io_req
->cmd_type
== BNX2FC_SCSI_CMD
)
559 index
= io_req
->xid
% num_possible_cpus();
561 index
= RESERVE_FREE_LIST_INDEX
;
564 spin_lock_bh(&cmd_mgr
->free_list_lock
[index
]);
565 if (io_req
->cmd_type
!= BNX2FC_SCSI_CMD
)
566 bnx2fc_free_mp_resc(io_req
);
567 cmd_mgr
->cmds
[io_req
->xid
] = NULL
;
568 /* Delete IO from retire queue */
569 list_del_init(&io_req
->link
);
570 /* Add it to the free list */
571 list_add(&io_req
->link
,
572 &cmd_mgr
->free_list
[index
]);
573 atomic_dec(&io_req
->tgt
->num_active_ios
);
574 spin_unlock_bh(&cmd_mgr
->free_list_lock
[index
]);
578 static void bnx2fc_free_mp_resc(struct bnx2fc_cmd
*io_req
)
580 struct bnx2fc_mp_req
*mp_req
= &(io_req
->mp_req
);
581 struct bnx2fc_interface
*interface
= io_req
->port
->priv
;
582 struct bnx2fc_hba
*hba
= interface
->hba
;
583 size_t sz
= sizeof(struct fcoe_bd_ctx
);
586 mp_req
->tm_flags
= 0;
587 if (mp_req
->mp_req_bd
) {
588 dma_free_coherent(&hba
->pcidev
->dev
, sz
,
590 mp_req
->mp_req_bd_dma
);
591 mp_req
->mp_req_bd
= NULL
;
593 if (mp_req
->mp_resp_bd
) {
594 dma_free_coherent(&hba
->pcidev
->dev
, sz
,
596 mp_req
->mp_resp_bd_dma
);
597 mp_req
->mp_resp_bd
= NULL
;
599 if (mp_req
->req_buf
) {
600 dma_free_coherent(&hba
->pcidev
->dev
, CNIC_PAGE_SIZE
,
602 mp_req
->req_buf_dma
);
603 mp_req
->req_buf
= NULL
;
605 if (mp_req
->resp_buf
) {
606 dma_free_coherent(&hba
->pcidev
->dev
, CNIC_PAGE_SIZE
,
608 mp_req
->resp_buf_dma
);
609 mp_req
->resp_buf
= NULL
;
613 int bnx2fc_init_mp_req(struct bnx2fc_cmd
*io_req
)
615 struct bnx2fc_mp_req
*mp_req
;
616 struct fcoe_bd_ctx
*mp_req_bd
;
617 struct fcoe_bd_ctx
*mp_resp_bd
;
618 struct bnx2fc_interface
*interface
= io_req
->port
->priv
;
619 struct bnx2fc_hba
*hba
= interface
->hba
;
623 mp_req
= (struct bnx2fc_mp_req
*)&(io_req
->mp_req
);
624 memset(mp_req
, 0, sizeof(struct bnx2fc_mp_req
));
626 mp_req
->req_len
= sizeof(struct fcp_cmnd
);
627 io_req
->data_xfer_len
= mp_req
->req_len
;
628 mp_req
->req_buf
= dma_alloc_coherent(&hba
->pcidev
->dev
, CNIC_PAGE_SIZE
,
629 &mp_req
->req_buf_dma
,
631 if (!mp_req
->req_buf
) {
632 printk(KERN_ERR PFX
"unable to alloc MP req buffer\n");
633 bnx2fc_free_mp_resc(io_req
);
637 mp_req
->resp_buf
= dma_alloc_coherent(&hba
->pcidev
->dev
, CNIC_PAGE_SIZE
,
638 &mp_req
->resp_buf_dma
,
640 if (!mp_req
->resp_buf
) {
641 printk(KERN_ERR PFX
"unable to alloc TM resp buffer\n");
642 bnx2fc_free_mp_resc(io_req
);
645 memset(mp_req
->req_buf
, 0, CNIC_PAGE_SIZE
);
646 memset(mp_req
->resp_buf
, 0, CNIC_PAGE_SIZE
);
648 /* Allocate and map mp_req_bd and mp_resp_bd */
649 sz
= sizeof(struct fcoe_bd_ctx
);
650 mp_req
->mp_req_bd
= dma_alloc_coherent(&hba
->pcidev
->dev
, sz
,
651 &mp_req
->mp_req_bd_dma
,
653 if (!mp_req
->mp_req_bd
) {
654 printk(KERN_ERR PFX
"unable to alloc MP req bd\n");
655 bnx2fc_free_mp_resc(io_req
);
658 mp_req
->mp_resp_bd
= dma_alloc_coherent(&hba
->pcidev
->dev
, sz
,
659 &mp_req
->mp_resp_bd_dma
,
661 if (!mp_req
->mp_resp_bd
) {
662 printk(KERN_ERR PFX
"unable to alloc MP resp bd\n");
663 bnx2fc_free_mp_resc(io_req
);
667 addr
= mp_req
->req_buf_dma
;
668 mp_req_bd
= mp_req
->mp_req_bd
;
669 mp_req_bd
->buf_addr_lo
= (u32
)addr
& 0xffffffff;
670 mp_req_bd
->buf_addr_hi
= (u32
)((u64
)addr
>> 32);
671 mp_req_bd
->buf_len
= CNIC_PAGE_SIZE
;
672 mp_req_bd
->flags
= 0;
675 * MP buffer is either a task mgmt command or an ELS.
676 * So the assumption is that it consumes a single bd
677 * entry in the bd table
679 mp_resp_bd
= mp_req
->mp_resp_bd
;
680 addr
= mp_req
->resp_buf_dma
;
681 mp_resp_bd
->buf_addr_lo
= (u32
)addr
& 0xffffffff;
682 mp_resp_bd
->buf_addr_hi
= (u32
)((u64
)addr
>> 32);
683 mp_resp_bd
->buf_len
= CNIC_PAGE_SIZE
;
684 mp_resp_bd
->flags
= 0;
689 static int bnx2fc_initiate_tmf(struct scsi_cmnd
*sc_cmd
, u8 tm_flags
)
691 struct fc_lport
*lport
;
692 struct fc_rport
*rport
;
693 struct fc_rport_libfc_priv
*rp
;
694 struct fcoe_port
*port
;
695 struct bnx2fc_interface
*interface
;
696 struct bnx2fc_rport
*tgt
;
697 struct bnx2fc_cmd
*io_req
;
698 struct bnx2fc_mp_req
*tm_req
;
699 struct fcoe_task_ctx_entry
*task
;
700 struct fcoe_task_ctx_entry
*task_page
;
701 struct Scsi_Host
*host
= sc_cmd
->device
->host
;
702 struct fc_frame_header
*fc_hdr
;
703 struct fcp_cmnd
*fcp_cmnd
;
708 unsigned long start
= jiffies
;
710 lport
= shost_priv(host
);
711 rport
= starget_to_rport(scsi_target(sc_cmd
->device
));
712 port
= lport_priv(lport
);
713 interface
= port
->priv
;
716 printk(KERN_ERR PFX
"device_reset: rport is NULL\n");
722 rc
= fc_block_scsi_eh(sc_cmd
);
726 if (lport
->state
!= LPORT_ST_READY
|| !(lport
->link_up
)) {
727 printk(KERN_ERR PFX
"device_reset: link is not ready\n");
731 /* rport and tgt are allocated together, so tgt should be non-NULL */
732 tgt
= (struct bnx2fc_rport
*)&rp
[1];
734 if (!(test_bit(BNX2FC_FLAG_SESSION_READY
, &tgt
->flags
))) {
735 printk(KERN_ERR PFX
"device_reset: tgt not offloaded\n");
740 io_req
= bnx2fc_elstm_alloc(tgt
, BNX2FC_TASK_MGMT_CMD
);
742 if (time_after(jiffies
, start
+ HZ
)) {
743 printk(KERN_ERR PFX
"tmf: Failed TMF");
750 /* Initialize rest of io_req fields */
751 io_req
->sc_cmd
= sc_cmd
;
755 tm_req
= (struct bnx2fc_mp_req
*)&(io_req
->mp_req
);
757 rc
= bnx2fc_init_mp_req(io_req
);
759 printk(KERN_ERR PFX
"Task mgmt MP request init failed\n");
760 spin_lock_bh(&tgt
->tgt_lock
);
761 kref_put(&io_req
->refcount
, bnx2fc_cmd_release
);
762 spin_unlock_bh(&tgt
->tgt_lock
);
767 io_req
->io_req_flags
= 0;
768 tm_req
->tm_flags
= tm_flags
;
771 bnx2fc_build_fcp_cmnd(io_req
, (struct fcp_cmnd
*)tm_req
->req_buf
);
772 fcp_cmnd
= (struct fcp_cmnd
*)tm_req
->req_buf
;
773 memset(fcp_cmnd
->fc_cdb
, 0, sc_cmd
->cmd_len
);
777 fc_hdr
= &(tm_req
->req_fc_hdr
);
779 did
= rport
->port_id
;
780 __fc_fill_fc_hdr(fc_hdr
, FC_RCTL_DD_UNSOL_CMD
, did
, sid
,
781 FC_TYPE_FCP
, FC_FC_FIRST_SEQ
| FC_FC_END_SEQ
|
783 /* Obtain exchange id */
786 BNX2FC_TGT_DBG(tgt
, "Initiate TMF - xid = 0x%x\n", xid
);
787 task_idx
= xid
/BNX2FC_TASKS_PER_PAGE
;
788 index
= xid
% BNX2FC_TASKS_PER_PAGE
;
790 /* Initialize task context for this IO request */
791 task_page
= (struct fcoe_task_ctx_entry
*)
792 interface
->hba
->task_ctx
[task_idx
];
793 task
= &(task_page
[index
]);
794 bnx2fc_init_mp_task(io_req
, task
);
796 sc_cmd
->SCp
.ptr
= (char *)io_req
;
798 /* Obtain free SQ entry */
799 spin_lock_bh(&tgt
->tgt_lock
);
800 bnx2fc_add_2_sq(tgt
, xid
);
802 /* Enqueue the io_req to active_tm_queue */
803 io_req
->on_tmf_queue
= 1;
804 list_add_tail(&io_req
->link
, &tgt
->active_tm_queue
);
806 init_completion(&io_req
->tm_done
);
807 io_req
->wait_for_comp
= 1;
810 bnx2fc_ring_doorbell(tgt
);
811 spin_unlock_bh(&tgt
->tgt_lock
);
813 rc
= wait_for_completion_timeout(&io_req
->tm_done
,
814 BNX2FC_TM_TIMEOUT
* HZ
);
815 spin_lock_bh(&tgt
->tgt_lock
);
817 io_req
->wait_for_comp
= 0;
818 if (!(test_bit(BNX2FC_FLAG_TM_COMPL
, &io_req
->req_flags
))) {
819 set_bit(BNX2FC_FLAG_TM_TIMEOUT
, &io_req
->req_flags
);
820 if (io_req
->on_tmf_queue
) {
821 list_del_init(&io_req
->link
);
822 io_req
->on_tmf_queue
= 0;
824 io_req
->wait_for_comp
= 1;
825 bnx2fc_initiate_cleanup(io_req
);
826 spin_unlock_bh(&tgt
->tgt_lock
);
827 rc
= wait_for_completion_timeout(&io_req
->tm_done
,
829 spin_lock_bh(&tgt
->tgt_lock
);
830 io_req
->wait_for_comp
= 0;
832 kref_put(&io_req
->refcount
, bnx2fc_cmd_release
);
835 spin_unlock_bh(&tgt
->tgt_lock
);
838 BNX2FC_TGT_DBG(tgt
, "task mgmt command failed...\n");
841 BNX2FC_TGT_DBG(tgt
, "task mgmt command success...\n");
848 int bnx2fc_initiate_abts(struct bnx2fc_cmd
*io_req
)
850 struct fc_lport
*lport
;
851 struct bnx2fc_rport
*tgt
= io_req
->tgt
;
852 struct fc_rport
*rport
= tgt
->rport
;
853 struct fc_rport_priv
*rdata
= tgt
->rdata
;
854 struct bnx2fc_interface
*interface
;
855 struct fcoe_port
*port
;
856 struct bnx2fc_cmd
*abts_io_req
;
857 struct fcoe_task_ctx_entry
*task
;
858 struct fcoe_task_ctx_entry
*task_page
;
859 struct fc_frame_header
*fc_hdr
;
860 struct bnx2fc_mp_req
*abts_req
;
865 u32 r_a_tov
= rdata
->r_a_tov
;
867 /* called with tgt_lock held */
868 BNX2FC_IO_DBG(io_req
, "Entered bnx2fc_initiate_abts\n");
871 interface
= port
->priv
;
874 if (!test_bit(BNX2FC_FLAG_SESSION_READY
, &tgt
->flags
)) {
875 printk(KERN_ERR PFX
"initiate_abts: tgt not offloaded\n");
881 printk(KERN_ERR PFX
"initiate_abts: rport is NULL\n");
886 if (lport
->state
!= LPORT_ST_READY
|| !(lport
->link_up
)) {
887 printk(KERN_ERR PFX
"initiate_abts: link is not ready\n");
892 abts_io_req
= bnx2fc_elstm_alloc(tgt
, BNX2FC_ABTS
);
894 printk(KERN_ERR PFX
"abts: couldnt allocate cmd\n");
899 /* Initialize rest of io_req fields */
900 abts_io_req
->sc_cmd
= NULL
;
901 abts_io_req
->port
= port
;
902 abts_io_req
->tgt
= tgt
;
903 abts_io_req
->data_xfer_len
= 0; /* No data transfer for ABTS */
905 abts_req
= (struct bnx2fc_mp_req
*)&(abts_io_req
->mp_req
);
906 memset(abts_req
, 0, sizeof(struct bnx2fc_mp_req
));
909 fc_hdr
= &(abts_req
->req_fc_hdr
);
911 /* Obtain oxid and rxid for the original exchange to be aborted */
912 fc_hdr
->fh_ox_id
= htons(io_req
->xid
);
913 fc_hdr
->fh_rx_id
= htons(io_req
->task
->rxwr_txrd
.var_ctx
.rx_id
);
916 did
= rport
->port_id
;
918 __fc_fill_fc_hdr(fc_hdr
, FC_RCTL_BA_ABTS
, did
, sid
,
919 FC_TYPE_BLS
, FC_FC_FIRST_SEQ
| FC_FC_END_SEQ
|
922 xid
= abts_io_req
->xid
;
923 BNX2FC_IO_DBG(abts_io_req
, "ABTS io_req\n");
924 task_idx
= xid
/BNX2FC_TASKS_PER_PAGE
;
925 index
= xid
% BNX2FC_TASKS_PER_PAGE
;
927 /* Initialize task context for this IO request */
928 task_page
= (struct fcoe_task_ctx_entry
*)
929 interface
->hba
->task_ctx
[task_idx
];
930 task
= &(task_page
[index
]);
931 bnx2fc_init_mp_task(abts_io_req
, task
);
934 * ABTS task is a temporary task that will be cleaned up
935 * irrespective of ABTS response. We need to start the timer
936 * for the original exchange, as the CQE is posted for the original
939 * Timer for ABTS is started only when it is originated by a
940 * TM request. For the ABTS issued as part of ULP timeout,
941 * scsi-ml maintains the timers.
944 /* if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags))*/
945 bnx2fc_cmd_timer_set(io_req
, 2 * r_a_tov
);
947 /* Obtain free SQ entry */
948 bnx2fc_add_2_sq(tgt
, xid
);
951 bnx2fc_ring_doorbell(tgt
);
957 int bnx2fc_initiate_seq_cleanup(struct bnx2fc_cmd
*orig_io_req
, u32 offset
,
960 struct fc_lport
*lport
;
961 struct bnx2fc_rport
*tgt
= orig_io_req
->tgt
;
962 struct bnx2fc_interface
*interface
;
963 struct fcoe_port
*port
;
964 struct bnx2fc_cmd
*seq_clnp_req
;
965 struct fcoe_task_ctx_entry
*task
;
966 struct fcoe_task_ctx_entry
*task_page
;
967 struct bnx2fc_els_cb_arg
*cb_arg
= NULL
;
972 BNX2FC_IO_DBG(orig_io_req
, "bnx2fc_initiate_seq_cleanup xid = 0x%x\n",
974 kref_get(&orig_io_req
->refcount
);
976 port
= orig_io_req
->port
;
977 interface
= port
->priv
;
980 cb_arg
= kzalloc(sizeof(struct bnx2fc_els_cb_arg
), GFP_ATOMIC
);
982 printk(KERN_ERR PFX
"Unable to alloc cb_arg for seq clnup\n");
987 seq_clnp_req
= bnx2fc_elstm_alloc(tgt
, BNX2FC_SEQ_CLEANUP
);
989 printk(KERN_ERR PFX
"cleanup: couldnt allocate cmd\n");
994 /* Initialize rest of io_req fields */
995 seq_clnp_req
->sc_cmd
= NULL
;
996 seq_clnp_req
->port
= port
;
997 seq_clnp_req
->tgt
= tgt
;
998 seq_clnp_req
->data_xfer_len
= 0; /* No data transfer for cleanup */
1000 xid
= seq_clnp_req
->xid
;
1002 task_idx
= xid
/BNX2FC_TASKS_PER_PAGE
;
1003 index
= xid
% BNX2FC_TASKS_PER_PAGE
;
1005 /* Initialize task context for this IO request */
1006 task_page
= (struct fcoe_task_ctx_entry
*)
1007 interface
->hba
->task_ctx
[task_idx
];
1008 task
= &(task_page
[index
]);
1009 cb_arg
->aborted_io_req
= orig_io_req
;
1010 cb_arg
->io_req
= seq_clnp_req
;
1011 cb_arg
->r_ctl
= r_ctl
;
1012 cb_arg
->offset
= offset
;
1013 seq_clnp_req
->cb_arg
= cb_arg
;
1015 printk(KERN_ERR PFX
"call init_seq_cleanup_task\n");
1016 bnx2fc_init_seq_cleanup_task(seq_clnp_req
, task
, orig_io_req
, offset
);
1018 /* Obtain free SQ entry */
1019 bnx2fc_add_2_sq(tgt
, xid
);
1022 bnx2fc_ring_doorbell(tgt
);
1027 int bnx2fc_initiate_cleanup(struct bnx2fc_cmd
*io_req
)
1029 struct fc_lport
*lport
;
1030 struct bnx2fc_rport
*tgt
= io_req
->tgt
;
1031 struct bnx2fc_interface
*interface
;
1032 struct fcoe_port
*port
;
1033 struct bnx2fc_cmd
*cleanup_io_req
;
1034 struct fcoe_task_ctx_entry
*task
;
1035 struct fcoe_task_ctx_entry
*task_page
;
1036 int task_idx
, index
;
1040 /* ASSUMPTION: called with tgt_lock held */
1041 BNX2FC_IO_DBG(io_req
, "Entered bnx2fc_initiate_cleanup\n");
1043 port
= io_req
->port
;
1044 interface
= port
->priv
;
1045 lport
= port
->lport
;
1047 cleanup_io_req
= bnx2fc_elstm_alloc(tgt
, BNX2FC_CLEANUP
);
1048 if (!cleanup_io_req
) {
1049 printk(KERN_ERR PFX
"cleanup: couldnt allocate cmd\n");
1054 /* Initialize rest of io_req fields */
1055 cleanup_io_req
->sc_cmd
= NULL
;
1056 cleanup_io_req
->port
= port
;
1057 cleanup_io_req
->tgt
= tgt
;
1058 cleanup_io_req
->data_xfer_len
= 0; /* No data transfer for cleanup */
1060 xid
= cleanup_io_req
->xid
;
1062 task_idx
= xid
/BNX2FC_TASKS_PER_PAGE
;
1063 index
= xid
% BNX2FC_TASKS_PER_PAGE
;
1065 /* Initialize task context for this IO request */
1066 task_page
= (struct fcoe_task_ctx_entry
*)
1067 interface
->hba
->task_ctx
[task_idx
];
1068 task
= &(task_page
[index
]);
1069 orig_xid
= io_req
->xid
;
1071 BNX2FC_IO_DBG(io_req
, "CLEANUP io_req xid = 0x%x\n", xid
);
1073 bnx2fc_init_cleanup_task(cleanup_io_req
, task
, orig_xid
);
1075 /* Obtain free SQ entry */
1076 bnx2fc_add_2_sq(tgt
, xid
);
1079 bnx2fc_ring_doorbell(tgt
);
1086 * bnx2fc_eh_target_reset: Reset a target
1088 * @sc_cmd: SCSI command
1090 * Set from SCSI host template to send task mgmt command to the target
1091 * and wait for the response
1093 int bnx2fc_eh_target_reset(struct scsi_cmnd
*sc_cmd
)
1095 return bnx2fc_initiate_tmf(sc_cmd
, FCP_TMF_TGT_RESET
);
1099 * bnx2fc_eh_device_reset - Reset a single LUN
1101 * @sc_cmd: SCSI command
1103 * Set from SCSI host template to send task mgmt command to the target
1104 * and wait for the response
1106 int bnx2fc_eh_device_reset(struct scsi_cmnd
*sc_cmd
)
1108 return bnx2fc_initiate_tmf(sc_cmd
, FCP_TMF_LUN_RESET
);
1111 int bnx2fc_expl_logo(struct fc_lport
*lport
, struct bnx2fc_cmd
*io_req
)
1113 struct bnx2fc_rport
*tgt
= io_req
->tgt
;
1114 struct fc_rport_priv
*rdata
= tgt
->rdata
;
1119 BNX2FC_IO_DBG(io_req
, "Expl logo - tgt flags = 0x%lx\n",
1121 logo_issued
= test_and_set_bit(BNX2FC_FLAG_EXPL_LOGO
,
1123 io_req
->wait_for_comp
= 1;
1124 bnx2fc_initiate_cleanup(io_req
);
1126 spin_unlock_bh(&tgt
->tgt_lock
);
1128 wait_for_completion(&io_req
->tm_done
);
1130 io_req
->wait_for_comp
= 0;
1132 * release the reference taken in eh_abort to allow the
1133 * target to re-login after flushing IOs
1135 kref_put(&io_req
->refcount
, bnx2fc_cmd_release
);
1138 clear_bit(BNX2FC_FLAG_SESSION_READY
, &tgt
->flags
);
1139 mutex_lock(&lport
->disc
.disc_mutex
);
1140 lport
->tt
.rport_logoff(rdata
);
1141 mutex_unlock(&lport
->disc
.disc_mutex
);
1143 msleep(BNX2FC_RELOGIN_WAIT_TIME
);
1144 if (wait_cnt
++ > BNX2FC_RELOGIN_WAIT_CNT
) {
1148 } while (!test_bit(BNX2FC_FLAG_SESSION_READY
, &tgt
->flags
));
1150 spin_lock_bh(&tgt
->tgt_lock
);
1154 * bnx2fc_eh_abort - eh_abort_handler api to abort an outstanding
1157 * @sc_cmd: SCSI_ML command pointer
1159 * SCSI abort request handler
1161 int bnx2fc_eh_abort(struct scsi_cmnd
*sc_cmd
)
1163 struct fc_rport
*rport
= starget_to_rport(scsi_target(sc_cmd
->device
));
1164 struct fc_rport_libfc_priv
*rp
= rport
->dd_data
;
1165 struct bnx2fc_cmd
*io_req
;
1166 struct fc_lport
*lport
;
1167 struct bnx2fc_rport
*tgt
;
1171 rc
= fc_block_scsi_eh(sc_cmd
);
1175 lport
= shost_priv(sc_cmd
->device
->host
);
1176 if ((lport
->state
!= LPORT_ST_READY
) || !(lport
->link_up
)) {
1177 printk(KERN_ERR PFX
"eh_abort: link not ready\n");
1181 tgt
= (struct bnx2fc_rport
*)&rp
[1];
1183 BNX2FC_TGT_DBG(tgt
, "Entered bnx2fc_eh_abort\n");
1185 spin_lock_bh(&tgt
->tgt_lock
);
1186 io_req
= (struct bnx2fc_cmd
*)sc_cmd
->SCp
.ptr
;
1188 /* Command might have just completed */
1189 printk(KERN_ERR PFX
"eh_abort: io_req is NULL\n");
1190 spin_unlock_bh(&tgt
->tgt_lock
);
1193 BNX2FC_IO_DBG(io_req
, "eh_abort - refcnt = %d\n",
1194 io_req
->refcount
.refcount
.counter
);
1196 /* Hold IO request across abort processing */
1197 kref_get(&io_req
->refcount
);
1199 BUG_ON(tgt
!= io_req
->tgt
);
1201 /* Remove the io_req from the active_q. */
1203 * Task Mgmt functions (LUN RESET & TGT RESET) will not
1204 * issue an ABTS on this particular IO req, as the
1205 * io_req is no longer in the active_q.
1207 if (tgt
->flush_in_prog
) {
1208 printk(KERN_ERR PFX
"eh_abort: io_req (xid = 0x%x) "
1209 "flush in progress\n", io_req
->xid
);
1210 kref_put(&io_req
->refcount
, bnx2fc_cmd_release
);
1211 spin_unlock_bh(&tgt
->tgt_lock
);
1215 if (io_req
->on_active_queue
== 0) {
1216 printk(KERN_ERR PFX
"eh_abort: io_req (xid = 0x%x) "
1217 "not on active_q\n", io_req
->xid
);
1219 * This condition can happen only due to the FW bug,
1220 * where we do not receive cleanup response from
1221 * the FW. Handle this case gracefully by erroring
1222 * back the IO request to SCSI-ml
1224 bnx2fc_scsi_done(io_req
, DID_ABORT
);
1226 kref_put(&io_req
->refcount
, bnx2fc_cmd_release
);
1227 spin_unlock_bh(&tgt
->tgt_lock
);
1232 * Only eh_abort processing will remove the IO from
1233 * active_cmd_q before processing the request. this is
1234 * done to avoid race conditions between IOs aborted
1235 * as part of task management completion and eh_abort
1238 list_del_init(&io_req
->link
);
1239 io_req
->on_active_queue
= 0;
1240 /* Move IO req to retire queue */
1241 list_add_tail(&io_req
->link
, &tgt
->io_retire_queue
);
1243 init_completion(&io_req
->tm_done
);
1245 if (test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS
, &io_req
->req_flags
)) {
1246 printk(KERN_ERR PFX
"eh_abort: io_req (xid = 0x%x) "
1247 "already in abts processing\n", io_req
->xid
);
1248 if (cancel_delayed_work(&io_req
->timeout_work
))
1249 kref_put(&io_req
->refcount
,
1250 bnx2fc_cmd_release
); /* drop timer hold */
1251 rc
= bnx2fc_expl_logo(lport
, io_req
);
1252 /* This only occurs when an task abort was requested while ABTS
1253 is in progress. Setting the IO_CLEANUP flag will skip the
1254 RRQ process in the case when the fw generated SCSI_CMD cmpl
1255 was a result from the ABTS request rather than the CLEANUP
1257 set_bit(BNX2FC_FLAG_IO_CLEANUP
, &io_req
->req_flags
);
1261 /* Cancel the current timer running on this io_req */
1262 if (cancel_delayed_work(&io_req
->timeout_work
))
1263 kref_put(&io_req
->refcount
,
1264 bnx2fc_cmd_release
); /* drop timer hold */
1265 set_bit(BNX2FC_FLAG_EH_ABORT
, &io_req
->req_flags
);
1266 io_req
->wait_for_comp
= 1;
1267 rc
= bnx2fc_initiate_abts(io_req
);
1269 bnx2fc_initiate_cleanup(io_req
);
1270 spin_unlock_bh(&tgt
->tgt_lock
);
1271 wait_for_completion(&io_req
->tm_done
);
1272 spin_lock_bh(&tgt
->tgt_lock
);
1273 io_req
->wait_for_comp
= 0;
1276 spin_unlock_bh(&tgt
->tgt_lock
);
1278 wait_for_completion(&io_req
->tm_done
);
1280 spin_lock_bh(&tgt
->tgt_lock
);
1281 io_req
->wait_for_comp
= 0;
1282 if (test_bit(BNX2FC_FLAG_IO_COMPL
, &io_req
->req_flags
)) {
1283 BNX2FC_IO_DBG(io_req
, "IO completed in a different context\n");
1285 } else if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE
,
1286 &io_req
->req_flags
))) {
1287 /* Let the scsi-ml try to recover this command */
1288 printk(KERN_ERR PFX
"abort failed, xid = 0x%x\n",
1290 rc
= bnx2fc_expl_logo(lport
, io_req
);
1294 * We come here even when there was a race condition
1295 * between timeout and abts completion, and abts
1296 * completion happens just in time.
1298 BNX2FC_IO_DBG(io_req
, "abort succeeded\n");
1300 bnx2fc_scsi_done(io_req
, DID_ABORT
);
1301 kref_put(&io_req
->refcount
, bnx2fc_cmd_release
);
1304 /* release the reference taken in eh_abort */
1305 kref_put(&io_req
->refcount
, bnx2fc_cmd_release
);
1307 spin_unlock_bh(&tgt
->tgt_lock
);
1311 void bnx2fc_process_seq_cleanup_compl(struct bnx2fc_cmd
*seq_clnp_req
,
1312 struct fcoe_task_ctx_entry
*task
,
1315 struct bnx2fc_els_cb_arg
*cb_arg
= seq_clnp_req
->cb_arg
;
1316 struct bnx2fc_cmd
*orig_io_req
= cb_arg
->aborted_io_req
;
1317 u32 offset
= cb_arg
->offset
;
1318 enum fc_rctl r_ctl
= cb_arg
->r_ctl
;
1320 struct bnx2fc_rport
*tgt
= orig_io_req
->tgt
;
1322 BNX2FC_IO_DBG(orig_io_req
, "Entered process_cleanup_compl xid = 0x%x"
1324 seq_clnp_req
->xid
, seq_clnp_req
->cmd_type
);
1326 if (rx_state
== FCOE_TASK_RX_STATE_IGNORED_SEQUENCE_CLEANUP
) {
1327 printk(KERN_ERR PFX
"seq cleanup ignored - xid = 0x%x\n",
1332 spin_unlock_bh(&tgt
->tgt_lock
);
1333 rc
= bnx2fc_send_srr(orig_io_req
, offset
, r_ctl
);
1334 spin_lock_bh(&tgt
->tgt_lock
);
1337 printk(KERN_ERR PFX
"clnup_compl: Unable to send SRR"
1338 " IO will abort\n");
1339 seq_clnp_req
->cb_arg
= NULL
;
1340 kref_put(&orig_io_req
->refcount
, bnx2fc_cmd_release
);
1346 void bnx2fc_process_cleanup_compl(struct bnx2fc_cmd
*io_req
,
1347 struct fcoe_task_ctx_entry
*task
,
1350 BNX2FC_IO_DBG(io_req
, "Entered process_cleanup_compl "
1351 "refcnt = %d, cmd_type = %d\n",
1352 io_req
->refcount
.refcount
.counter
, io_req
->cmd_type
);
1353 bnx2fc_scsi_done(io_req
, DID_ERROR
);
1354 kref_put(&io_req
->refcount
, bnx2fc_cmd_release
);
1355 if (io_req
->wait_for_comp
)
1356 complete(&io_req
->tm_done
);
1359 void bnx2fc_process_abts_compl(struct bnx2fc_cmd
*io_req
,
1360 struct fcoe_task_ctx_entry
*task
,
1364 u32 r_a_tov
= FC_DEF_R_A_TOV
;
1366 struct bnx2fc_rport
*tgt
= io_req
->tgt
;
1368 BNX2FC_IO_DBG(io_req
, "Entered process_abts_compl xid = 0x%x"
1369 "refcnt = %d, cmd_type = %d\n",
1371 io_req
->refcount
.refcount
.counter
, io_req
->cmd_type
);
1373 if (test_and_set_bit(BNX2FC_FLAG_ABTS_DONE
,
1374 &io_req
->req_flags
)) {
1375 BNX2FC_IO_DBG(io_req
, "Timer context finished processing"
1380 /* Do not issue RRQ as this IO is already cleanedup */
1381 if (test_and_set_bit(BNX2FC_FLAG_IO_CLEANUP
,
1382 &io_req
->req_flags
))
1386 * For ABTS issued due to SCSI eh_abort_handler, timeout
1387 * values are maintained by scsi-ml itself. Cancel timeout
1388 * in case ABTS issued as part of task management function
1389 * or due to FW error.
1391 if (test_bit(BNX2FC_FLAG_ISSUE_ABTS
, &io_req
->req_flags
))
1392 if (cancel_delayed_work(&io_req
->timeout_work
))
1393 kref_put(&io_req
->refcount
,
1394 bnx2fc_cmd_release
); /* drop timer hold */
1396 r_ctl
= (u8
)task
->rxwr_only
.union_ctx
.comp_info
.abts_rsp
.r_ctl
;
1399 case FC_RCTL_BA_ACC
:
1401 * Dont release this cmd yet. It will be relesed
1402 * after we get RRQ response
1404 BNX2FC_IO_DBG(io_req
, "ABTS response - ACC Send RRQ\n");
1408 case FC_RCTL_BA_RJT
:
1409 BNX2FC_IO_DBG(io_req
, "ABTS response - RJT\n");
1412 printk(KERN_ERR PFX
"Unknown ABTS response\n");
1417 BNX2FC_IO_DBG(io_req
, "Issue RRQ after R_A_TOV\n");
1418 set_bit(BNX2FC_FLAG_ISSUE_RRQ
, &io_req
->req_flags
);
1420 set_bit(BNX2FC_FLAG_RETIRE_OXID
, &io_req
->req_flags
);
1421 bnx2fc_cmd_timer_set(io_req
, r_a_tov
);
1424 if (io_req
->wait_for_comp
) {
1425 if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT
,
1426 &io_req
->req_flags
))
1427 complete(&io_req
->tm_done
);
1430 * We end up here when ABTS is issued as
1431 * in asynchronous context, i.e., as part
1432 * of task management completion, or
1433 * when FW error is received or when the
1434 * ABTS is issued when the IO is timed
1438 if (io_req
->on_active_queue
) {
1439 list_del_init(&io_req
->link
);
1440 io_req
->on_active_queue
= 0;
1441 /* Move IO req to retire queue */
1442 list_add_tail(&io_req
->link
, &tgt
->io_retire_queue
);
1444 bnx2fc_scsi_done(io_req
, DID_ERROR
);
1445 kref_put(&io_req
->refcount
, bnx2fc_cmd_release
);
1449 static void bnx2fc_lun_reset_cmpl(struct bnx2fc_cmd
*io_req
)
1451 struct scsi_cmnd
*sc_cmd
= io_req
->sc_cmd
;
1452 struct bnx2fc_rport
*tgt
= io_req
->tgt
;
1453 struct bnx2fc_cmd
*cmd
, *tmp
;
1454 u64 tm_lun
= sc_cmd
->device
->lun
;
1458 /* called with tgt_lock held */
1459 BNX2FC_IO_DBG(io_req
, "Entered bnx2fc_lun_reset_cmpl\n");
1461 * Walk thru the active_ios queue and ABORT the IO
1462 * that matches with the LUN that was reset
1464 list_for_each_entry_safe(cmd
, tmp
, &tgt
->active_cmd_queue
, link
) {
1465 BNX2FC_TGT_DBG(tgt
, "LUN RST cmpl: scan for pending IOs\n");
1466 lun
= cmd
->sc_cmd
->device
->lun
;
1467 if (lun
== tm_lun
) {
1468 /* Initiate ABTS on this cmd */
1469 if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS
,
1471 /* cancel the IO timeout */
1472 if (cancel_delayed_work(&io_req
->timeout_work
))
1473 kref_put(&io_req
->refcount
,
1474 bnx2fc_cmd_release
);
1476 rc
= bnx2fc_initiate_abts(cmd
);
1477 /* abts shouldn't fail in this context */
1478 WARN_ON(rc
!= SUCCESS
);
1480 printk(KERN_ERR PFX
"lun_rst: abts already in"
1481 " progress for this IO 0x%x\n",
1487 static void bnx2fc_tgt_reset_cmpl(struct bnx2fc_cmd
*io_req
)
1489 struct bnx2fc_rport
*tgt
= io_req
->tgt
;
1490 struct bnx2fc_cmd
*cmd
, *tmp
;
1493 /* called with tgt_lock held */
1494 BNX2FC_IO_DBG(io_req
, "Entered bnx2fc_tgt_reset_cmpl\n");
1496 * Walk thru the active_ios queue and ABORT the IO
1497 * that matches with the LUN that was reset
1499 list_for_each_entry_safe(cmd
, tmp
, &tgt
->active_cmd_queue
, link
) {
1500 BNX2FC_TGT_DBG(tgt
, "TGT RST cmpl: scan for pending IOs\n");
1502 if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS
,
1504 /* cancel the IO timeout */
1505 if (cancel_delayed_work(&io_req
->timeout_work
))
1506 kref_put(&io_req
->refcount
,
1507 bnx2fc_cmd_release
); /* timer hold */
1508 rc
= bnx2fc_initiate_abts(cmd
);
1509 /* abts shouldn't fail in this context */
1510 WARN_ON(rc
!= SUCCESS
);
1513 printk(KERN_ERR PFX
"tgt_rst: abts already in progress"
1514 " for this IO 0x%x\n", cmd
->xid
);
1518 void bnx2fc_process_tm_compl(struct bnx2fc_cmd
*io_req
,
1519 struct fcoe_task_ctx_entry
*task
, u8 num_rq
)
1521 struct bnx2fc_mp_req
*tm_req
;
1522 struct fc_frame_header
*fc_hdr
;
1523 struct scsi_cmnd
*sc_cmd
= io_req
->sc_cmd
;
1528 /* Called with tgt_lock held */
1529 BNX2FC_IO_DBG(io_req
, "Entered process_tm_compl\n");
1531 if (!(test_bit(BNX2FC_FLAG_TM_TIMEOUT
, &io_req
->req_flags
)))
1532 set_bit(BNX2FC_FLAG_TM_COMPL
, &io_req
->req_flags
);
1534 /* TM has already timed out and we got
1535 * delayed completion. Ignore completion
1541 tm_req
= &(io_req
->mp_req
);
1542 fc_hdr
= &(tm_req
->resp_fc_hdr
);
1543 hdr
= (u64
*)fc_hdr
;
1545 &task
->rxwr_only
.union_ctx
.comp_info
.mp_rsp
.fc_hdr
;
1546 hdr
[0] = cpu_to_be64(temp_hdr
[0]);
1547 hdr
[1] = cpu_to_be64(temp_hdr
[1]);
1548 hdr
[2] = cpu_to_be64(temp_hdr
[2]);
1551 task
->rxwr_only
.union_ctx
.comp_info
.mp_rsp
.mp_payload_len
;
1553 rsp_buf
= tm_req
->resp_buf
;
1555 if (fc_hdr
->fh_r_ctl
== FC_RCTL_DD_CMD_STATUS
) {
1556 bnx2fc_parse_fcp_rsp(io_req
,
1557 (struct fcoe_fcp_rsp_payload
*)
1559 if (io_req
->fcp_rsp_code
== 0) {
1561 if (tm_req
->tm_flags
& FCP_TMF_LUN_RESET
)
1562 bnx2fc_lun_reset_cmpl(io_req
);
1563 else if (tm_req
->tm_flags
& FCP_TMF_TGT_RESET
)
1564 bnx2fc_tgt_reset_cmpl(io_req
);
1567 printk(KERN_ERR PFX
"tmf's fc_hdr r_ctl = 0x%x\n",
1570 if (!sc_cmd
->SCp
.ptr
) {
1571 printk(KERN_ERR PFX
"tm_compl: SCp.ptr is NULL\n");
1574 switch (io_req
->fcp_status
) {
1576 if (io_req
->cdb_status
== 0) {
1577 /* Good IO completion */
1578 sc_cmd
->result
= DID_OK
<< 16;
1580 /* Transport status is good, SCSI status not good */
1581 sc_cmd
->result
= (DID_OK
<< 16) | io_req
->cdb_status
;
1583 if (io_req
->fcp_resid
)
1584 scsi_set_resid(sc_cmd
, io_req
->fcp_resid
);
1588 BNX2FC_IO_DBG(io_req
, "process_tm_compl: fcp_status = %d\n",
1589 io_req
->fcp_status
);
1593 sc_cmd
= io_req
->sc_cmd
;
1594 io_req
->sc_cmd
= NULL
;
1596 /* check if the io_req exists in tgt's tmf_q */
1597 if (io_req
->on_tmf_queue
) {
1599 list_del_init(&io_req
->link
);
1600 io_req
->on_tmf_queue
= 0;
1603 printk(KERN_ERR PFX
"Command not on active_cmd_queue!\n");
1607 sc_cmd
->SCp
.ptr
= NULL
;
1608 sc_cmd
->scsi_done(sc_cmd
);
1610 kref_put(&io_req
->refcount
, bnx2fc_cmd_release
);
1611 if (io_req
->wait_for_comp
) {
1612 BNX2FC_IO_DBG(io_req
, "tm_compl - wake up the waiter\n");
1613 complete(&io_req
->tm_done
);
1617 static int bnx2fc_split_bd(struct bnx2fc_cmd
*io_req
, u64 addr
, int sg_len
,
1620 struct fcoe_bd_ctx
*bd
= io_req
->bd_tbl
->bd_tbl
;
1621 int frag_size
, sg_frags
;
1625 if (sg_len
>= BNX2FC_BD_SPLIT_SZ
)
1626 frag_size
= BNX2FC_BD_SPLIT_SZ
;
1629 bd
[bd_index
+ sg_frags
].buf_addr_lo
= addr
& 0xffffffff;
1630 bd
[bd_index
+ sg_frags
].buf_addr_hi
= addr
>> 32;
1631 bd
[bd_index
+ sg_frags
].buf_len
= (u16
)frag_size
;
1632 bd
[bd_index
+ sg_frags
].flags
= 0;
1634 addr
+= (u64
) frag_size
;
1636 sg_len
-= frag_size
;
1642 static int bnx2fc_map_sg(struct bnx2fc_cmd
*io_req
)
1644 struct bnx2fc_interface
*interface
= io_req
->port
->priv
;
1645 struct bnx2fc_hba
*hba
= interface
->hba
;
1646 struct scsi_cmnd
*sc
= io_req
->sc_cmd
;
1647 struct fcoe_bd_ctx
*bd
= io_req
->bd_tbl
->bd_tbl
;
1648 struct scatterlist
*sg
;
1653 unsigned int sg_len
;
1658 * Use dma_map_sg directly to ensure we're using the correct
1659 * dev struct off of pcidev.
1661 sg_count
= dma_map_sg(&hba
->pcidev
->dev
, scsi_sglist(sc
),
1662 scsi_sg_count(sc
), sc
->sc_data_direction
);
1663 scsi_for_each_sg(sc
, sg
, sg_count
, i
) {
1664 sg_len
= sg_dma_len(sg
);
1665 addr
= sg_dma_address(sg
);
1666 if (sg_len
> BNX2FC_MAX_BD_LEN
) {
1667 sg_frags
= bnx2fc_split_bd(io_req
, addr
, sg_len
,
1672 bd
[bd_count
].buf_addr_lo
= addr
& 0xffffffff;
1673 bd
[bd_count
].buf_addr_hi
= addr
>> 32;
1674 bd
[bd_count
].buf_len
= (u16
)sg_len
;
1675 bd
[bd_count
].flags
= 0;
1677 bd_count
+= sg_frags
;
1678 byte_count
+= sg_len
;
1680 if (byte_count
!= scsi_bufflen(sc
))
1681 printk(KERN_ERR PFX
"byte_count = %d != scsi_bufflen = %d, "
1682 "task_id = 0x%x\n", byte_count
, scsi_bufflen(sc
),
1687 static int bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd
*io_req
)
1689 struct scsi_cmnd
*sc
= io_req
->sc_cmd
;
1690 struct fcoe_bd_ctx
*bd
= io_req
->bd_tbl
->bd_tbl
;
1693 if (scsi_sg_count(sc
)) {
1694 bd_count
= bnx2fc_map_sg(io_req
);
1699 bd
[0].buf_addr_lo
= bd
[0].buf_addr_hi
= 0;
1700 bd
[0].buf_len
= bd
[0].flags
= 0;
1702 io_req
->bd_tbl
->bd_valid
= bd_count
;
1707 static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd
*io_req
)
1709 struct scsi_cmnd
*sc
= io_req
->sc_cmd
;
1710 struct bnx2fc_interface
*interface
= io_req
->port
->priv
;
1711 struct bnx2fc_hba
*hba
= interface
->hba
;
1714 * Use dma_unmap_sg directly to ensure we're using the correct
1715 * dev struct off of pcidev.
1717 if (io_req
->bd_tbl
->bd_valid
&& sc
&& scsi_sg_count(sc
)) {
1718 dma_unmap_sg(&hba
->pcidev
->dev
, scsi_sglist(sc
),
1719 scsi_sg_count(sc
), sc
->sc_data_direction
);
1720 io_req
->bd_tbl
->bd_valid
= 0;
1724 void bnx2fc_build_fcp_cmnd(struct bnx2fc_cmd
*io_req
,
1725 struct fcp_cmnd
*fcp_cmnd
)
1727 struct scsi_cmnd
*sc_cmd
= io_req
->sc_cmd
;
1729 memset(fcp_cmnd
, 0, sizeof(struct fcp_cmnd
));
1731 int_to_scsilun(sc_cmd
->device
->lun
, &fcp_cmnd
->fc_lun
);
1733 fcp_cmnd
->fc_dl
= htonl(io_req
->data_xfer_len
);
1734 memcpy(fcp_cmnd
->fc_cdb
, sc_cmd
->cmnd
, sc_cmd
->cmd_len
);
1736 fcp_cmnd
->fc_cmdref
= 0;
1737 fcp_cmnd
->fc_pri_ta
= 0;
1738 fcp_cmnd
->fc_tm_flags
= io_req
->mp_req
.tm_flags
;
1739 fcp_cmnd
->fc_flags
= io_req
->io_req_flags
;
1740 fcp_cmnd
->fc_pri_ta
= FCP_PTA_SIMPLE
;
1743 static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd
*io_req
,
1744 struct fcoe_fcp_rsp_payload
*fcp_rsp
,
1747 struct scsi_cmnd
*sc_cmd
= io_req
->sc_cmd
;
1748 struct bnx2fc_rport
*tgt
= io_req
->tgt
;
1749 u8 rsp_flags
= fcp_rsp
->fcp_flags
.flags
;
1750 u32 rq_buff_len
= 0;
1752 unsigned char *rq_data
;
1753 unsigned char *dummy
;
1754 int fcp_sns_len
= 0;
1755 int fcp_rsp_len
= 0;
1757 io_req
->fcp_status
= FC_GOOD
;
1758 io_req
->fcp_resid
= fcp_rsp
->fcp_resid
;
1760 io_req
->scsi_comp_flags
= rsp_flags
;
1761 CMD_SCSI_STATUS(sc_cmd
) = io_req
->cdb_status
=
1762 fcp_rsp
->scsi_status_code
;
1764 /* Fetch fcp_rsp_info and fcp_sns_info if available */
1768 * We do not anticipate num_rq >1, as the linux defined
1769 * SCSI_SENSE_BUFFERSIZE is 96 bytes + 8 bytes of FCP_RSP_INFO
1770 * 256 bytes of single rq buffer is good enough to hold this.
1774 FCOE_FCP_RSP_FLAGS_FCP_RSP_LEN_VALID
) {
1775 fcp_rsp_len
= rq_buff_len
1776 = fcp_rsp
->fcp_rsp_len
;
1780 FCOE_FCP_RSP_FLAGS_FCP_SNS_LEN_VALID
) {
1781 fcp_sns_len
= fcp_rsp
->fcp_sns_len
;
1782 rq_buff_len
+= fcp_rsp
->fcp_sns_len
;
1785 io_req
->fcp_rsp_len
= fcp_rsp_len
;
1786 io_req
->fcp_sns_len
= fcp_sns_len
;
1788 if (rq_buff_len
> num_rq
* BNX2FC_RQ_BUF_SZ
) {
1789 /* Invalid sense sense length. */
1790 printk(KERN_ERR PFX
"invalid sns length %d\n",
1792 /* reset rq_buff_len */
1793 rq_buff_len
= num_rq
* BNX2FC_RQ_BUF_SZ
;
1796 rq_data
= bnx2fc_get_next_rqe(tgt
, 1);
1799 /* We do not need extra sense data */
1800 for (i
= 1; i
< num_rq
; i
++)
1801 dummy
= bnx2fc_get_next_rqe(tgt
, 1);
1804 /* fetch fcp_rsp_code */
1805 if ((fcp_rsp_len
== 4) || (fcp_rsp_len
== 8)) {
1806 /* Only for task management function */
1807 io_req
->fcp_rsp_code
= rq_data
[3];
1808 printk(KERN_ERR PFX
"fcp_rsp_code = %d\n",
1809 io_req
->fcp_rsp_code
);
1812 /* fetch sense data */
1813 rq_data
+= fcp_rsp_len
;
1815 if (fcp_sns_len
> SCSI_SENSE_BUFFERSIZE
) {
1816 printk(KERN_ERR PFX
"Truncating sense buffer\n");
1817 fcp_sns_len
= SCSI_SENSE_BUFFERSIZE
;
1820 memset(sc_cmd
->sense_buffer
, 0, SCSI_SENSE_BUFFERSIZE
);
1822 memcpy(sc_cmd
->sense_buffer
, rq_data
, fcp_sns_len
);
1824 /* return RQ entries */
1825 for (i
= 0; i
< num_rq
; i
++)
1826 bnx2fc_return_rqe(tgt
, 1);
1831 * bnx2fc_queuecommand - Queuecommand function of the scsi template
1833 * @host: The Scsi_Host the command was issued to
1834 * @sc_cmd: struct scsi_cmnd to be executed
1836 * This is the IO strategy routine, called by SCSI-ML
1838 int bnx2fc_queuecommand(struct Scsi_Host
*host
,
1839 struct scsi_cmnd
*sc_cmd
)
1841 struct fc_lport
*lport
= shost_priv(host
);
1842 struct fc_rport
*rport
= starget_to_rport(scsi_target(sc_cmd
->device
));
1843 struct fc_rport_libfc_priv
*rp
= rport
->dd_data
;
1844 struct bnx2fc_rport
*tgt
;
1845 struct bnx2fc_cmd
*io_req
;
1849 rval
= fc_remote_port_chkready(rport
);
1851 sc_cmd
->result
= rval
;
1852 sc_cmd
->scsi_done(sc_cmd
);
1856 if ((lport
->state
!= LPORT_ST_READY
) || !(lport
->link_up
)) {
1857 rc
= SCSI_MLQUEUE_HOST_BUSY
;
1861 /* rport and tgt are allocated together, so tgt should be non-NULL */
1862 tgt
= (struct bnx2fc_rport
*)&rp
[1];
1864 if (!test_bit(BNX2FC_FLAG_SESSION_READY
, &tgt
->flags
)) {
1866 * Session is not offloaded yet. Let SCSI-ml retry
1869 rc
= SCSI_MLQUEUE_TARGET_BUSY
;
1872 if (tgt
->retry_delay_timestamp
) {
1873 if (time_after(jiffies
, tgt
->retry_delay_timestamp
)) {
1874 tgt
->retry_delay_timestamp
= 0;
1876 /* If retry_delay timer is active, flow off the ML */
1877 rc
= SCSI_MLQUEUE_TARGET_BUSY
;
1882 spin_lock_bh(&tgt
->tgt_lock
);
1884 io_req
= bnx2fc_cmd_alloc(tgt
);
1886 rc
= SCSI_MLQUEUE_HOST_BUSY
;
1887 goto exit_qcmd_tgtlock
;
1889 io_req
->sc_cmd
= sc_cmd
;
1891 if (bnx2fc_post_io_req(tgt
, io_req
)) {
1892 printk(KERN_ERR PFX
"Unable to post io_req\n");
1893 rc
= SCSI_MLQUEUE_HOST_BUSY
;
1894 goto exit_qcmd_tgtlock
;
1898 spin_unlock_bh(&tgt
->tgt_lock
);
1903 void bnx2fc_process_scsi_cmd_compl(struct bnx2fc_cmd
*io_req
,
1904 struct fcoe_task_ctx_entry
*task
,
1907 struct fcoe_fcp_rsp_payload
*fcp_rsp
;
1908 struct bnx2fc_rport
*tgt
= io_req
->tgt
;
1909 struct scsi_cmnd
*sc_cmd
;
1910 struct Scsi_Host
*host
;
1913 /* scsi_cmd_cmpl is called with tgt lock held */
1915 if (test_and_set_bit(BNX2FC_FLAG_IO_COMPL
, &io_req
->req_flags
)) {
1916 /* we will not receive ABTS response for this IO */
1917 BNX2FC_IO_DBG(io_req
, "Timer context finished processing "
1921 /* Cancel the timeout_work, as we received IO completion */
1922 if (cancel_delayed_work(&io_req
->timeout_work
))
1923 kref_put(&io_req
->refcount
,
1924 bnx2fc_cmd_release
); /* drop timer hold */
1926 sc_cmd
= io_req
->sc_cmd
;
1927 if (sc_cmd
== NULL
) {
1928 printk(KERN_ERR PFX
"scsi_cmd_compl - sc_cmd is NULL\n");
1932 /* Fetch fcp_rsp from task context and perform cmd completion */
1933 fcp_rsp
= (struct fcoe_fcp_rsp_payload
*)
1934 &(task
->rxwr_only
.union_ctx
.comp_info
.fcp_rsp
.payload
);
1936 /* parse fcp_rsp and obtain sense data from RQ if available */
1937 bnx2fc_parse_fcp_rsp(io_req
, fcp_rsp
, num_rq
);
1939 host
= sc_cmd
->device
->host
;
1940 if (!sc_cmd
->SCp
.ptr
) {
1941 printk(KERN_ERR PFX
"SCp.ptr is NULL\n");
1945 if (io_req
->on_active_queue
) {
1946 list_del_init(&io_req
->link
);
1947 io_req
->on_active_queue
= 0;
1948 /* Move IO req to retire queue */
1949 list_add_tail(&io_req
->link
, &tgt
->io_retire_queue
);
1951 /* This should not happen, but could have been pulled
1952 * by bnx2fc_flush_active_ios(), or during a race
1953 * between command abort and (late) completion.
1955 BNX2FC_IO_DBG(io_req
, "xid not on active_cmd_queue\n");
1956 if (io_req
->wait_for_comp
)
1957 if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT
,
1958 &io_req
->req_flags
))
1959 complete(&io_req
->tm_done
);
1962 bnx2fc_unmap_sg_list(io_req
);
1963 io_req
->sc_cmd
= NULL
;
1965 switch (io_req
->fcp_status
) {
1967 if (io_req
->cdb_status
== 0) {
1968 /* Good IO completion */
1969 sc_cmd
->result
= DID_OK
<< 16;
1971 /* Transport status is good, SCSI status not good */
1972 BNX2FC_IO_DBG(io_req
, "scsi_cmpl: cdb_status = %d"
1973 " fcp_resid = 0x%x\n",
1974 io_req
->cdb_status
, io_req
->fcp_resid
);
1975 sc_cmd
->result
= (DID_OK
<< 16) | io_req
->cdb_status
;
1977 if (io_req
->cdb_status
== SAM_STAT_TASK_SET_FULL
||
1978 io_req
->cdb_status
== SAM_STAT_BUSY
) {
1979 /* Set the jiffies + retry_delay_timer * 100ms
1980 for the rport/tgt */
1981 tgt
->retry_delay_timestamp
= jiffies
+
1982 fcp_rsp
->retry_delay_timer
* HZ
/ 10;
1986 if (io_req
->fcp_resid
)
1987 scsi_set_resid(sc_cmd
, io_req
->fcp_resid
);
1990 printk(KERN_ERR PFX
"scsi_cmd_compl: fcp_status = %d\n",
1991 io_req
->fcp_status
);
1994 sc_cmd
->SCp
.ptr
= NULL
;
1995 sc_cmd
->scsi_done(sc_cmd
);
1996 kref_put(&io_req
->refcount
, bnx2fc_cmd_release
);
1999 int bnx2fc_post_io_req(struct bnx2fc_rport
*tgt
,
2000 struct bnx2fc_cmd
*io_req
)
2002 struct fcoe_task_ctx_entry
*task
;
2003 struct fcoe_task_ctx_entry
*task_page
;
2004 struct scsi_cmnd
*sc_cmd
= io_req
->sc_cmd
;
2005 struct fcoe_port
*port
= tgt
->port
;
2006 struct bnx2fc_interface
*interface
= port
->priv
;
2007 struct bnx2fc_hba
*hba
= interface
->hba
;
2008 struct fc_lport
*lport
= port
->lport
;
2009 struct fc_stats
*stats
;
2010 int task_idx
, index
;
2013 /* bnx2fc_post_io_req() is called with the tgt_lock held */
2015 /* Initialize rest of io_req fields */
2016 io_req
->cmd_type
= BNX2FC_SCSI_CMD
;
2017 io_req
->port
= port
;
2019 io_req
->data_xfer_len
= scsi_bufflen(sc_cmd
);
2020 sc_cmd
->SCp
.ptr
= (char *)io_req
;
2022 stats
= per_cpu_ptr(lport
->stats
, get_cpu());
2023 if (sc_cmd
->sc_data_direction
== DMA_FROM_DEVICE
) {
2024 io_req
->io_req_flags
= BNX2FC_READ
;
2025 stats
->InputRequests
++;
2026 stats
->InputBytes
+= io_req
->data_xfer_len
;
2027 } else if (sc_cmd
->sc_data_direction
== DMA_TO_DEVICE
) {
2028 io_req
->io_req_flags
= BNX2FC_WRITE
;
2029 stats
->OutputRequests
++;
2030 stats
->OutputBytes
+= io_req
->data_xfer_len
;
2032 io_req
->io_req_flags
= 0;
2033 stats
->ControlRequests
++;
2039 /* Build buffer descriptor list for firmware from sg list */
2040 if (bnx2fc_build_bd_list_from_sg(io_req
)) {
2041 printk(KERN_ERR PFX
"BD list creation failed\n");
2042 kref_put(&io_req
->refcount
, bnx2fc_cmd_release
);
2046 task_idx
= xid
/ BNX2FC_TASKS_PER_PAGE
;
2047 index
= xid
% BNX2FC_TASKS_PER_PAGE
;
2049 /* Initialize task context for this IO request */
2050 task_page
= (struct fcoe_task_ctx_entry
*) hba
->task_ctx
[task_idx
];
2051 task
= &(task_page
[index
]);
2052 bnx2fc_init_task(io_req
, task
);
2054 if (tgt
->flush_in_prog
) {
2055 printk(KERN_ERR PFX
"Flush in progress..Host Busy\n");
2056 kref_put(&io_req
->refcount
, bnx2fc_cmd_release
);
2060 if (!test_bit(BNX2FC_FLAG_SESSION_READY
, &tgt
->flags
)) {
2061 printk(KERN_ERR PFX
"Session not ready...post_io\n");
2062 kref_put(&io_req
->refcount
, bnx2fc_cmd_release
);
2067 if (tgt
->io_timeout
)
2068 bnx2fc_cmd_timer_set(io_req
, BNX2FC_IO_TIMEOUT
);
2069 /* Obtain free SQ entry */
2070 bnx2fc_add_2_sq(tgt
, xid
);
2072 /* Enqueue the io_req to active_cmd_queue */
2074 io_req
->on_active_queue
= 1;
2075 /* move io_req from pending_queue to active_queue */
2076 list_add_tail(&io_req
->link
, &tgt
->active_cmd_queue
);
2079 bnx2fc_ring_doorbell(tgt
);