Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jwessel...
[pandora-kernel.git] / drivers / scsi / bnx2fc / bnx2fc_io.c
1 /* bnx2fc_io.c: Broadcom NetXtreme II Linux FCoE offload driver.
2  * IO manager and SCSI IO processing.
3  *
4  * Copyright (c) 2008 - 2010 Broadcom Corporation
5  *
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.
9  *
10  * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
11  */
12
13 #include "bnx2fc.h"
14 static int bnx2fc_split_bd(struct bnx2fc_cmd *io_req, u64 addr, int sg_len,
15                            int bd_index);
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,
24                                  u8 num_rq);
25
26 void bnx2fc_cmd_timer_set(struct bnx2fc_cmd *io_req,
27                           unsigned int timer_msec)
28 {
29         struct bnx2fc_hba *hba = io_req->port->priv;
30
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);
34 }
35
36 static void bnx2fc_cmd_timeout(struct work_struct *work)
37 {
38         struct bnx2fc_cmd *io_req = container_of(work, struct bnx2fc_cmd,
39                                                  timeout_work.work);
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;
44         int logo_issued;
45         int rc;
46
47         BNX2FC_IO_DBG(io_req, "cmd_timeout, cmd_type = %d,"
48                       "req_flags = %lx\n", cmd_type, io_req->req_flags);
49
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);
53                 /*
54                  * ideally we should hold the io_req until RRQ complets,
55                  * and release io_req from timeout hold.
56                  */
57                 spin_unlock_bh(&tgt->tgt_lock);
58                 bnx2fc_send_rrq(io_req);
59                 return;
60         }
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");
63                 goto done;
64         }
65
66         switch (cmd_type) {
67         case BNX2FC_SCSI_CMD:
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))) {
80
81                                 lport = io_req->port->lport;
82                                 rdata = io_req->tgt->rdata;
83                                 logo_issued = test_and_set_bit(
84                                                 BNX2FC_FLAG_EXPL_LOGO,
85                                                 &tgt->flags);
86                                 kref_put(&io_req->refcount, bnx2fc_cmd_release);
87                                 spin_unlock_bh(&tgt->tgt_lock);
88
89                                 /* Explicitly logo the target */
90                                 if (!logo_issued) {
91                                         BNX2FC_IO_DBG(io_req, "Explicit "
92                                                    "logo - tgt flags = 0x%lx\n",
93                                                    tgt->flags);
94
95                                         mutex_lock(&lport->disc.disc_mutex);
96                                         lport->tt.rport_logoff(rdata);
97                                         mutex_unlock(&lport->disc.disc_mutex);
98                                 }
99                                 return;
100                         }
101                 } else {
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 "
107                                                            " timer expiry\n");
108                                 goto done;
109                         }
110
111                         if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
112                                               &io_req->req_flags)) {
113                                 rc = bnx2fc_initiate_abts(io_req);
114                                 if (rc == SUCCESS)
115                                         goto done;
116                                 /*
117                                  * Explicitly logo the target if
118                                  * abts initiation fails
119                                  */
120                                 lport = io_req->port->lport;
121                                 rdata = io_req->tgt->rdata;
122                                 logo_issued = test_and_set_bit(
123                                                 BNX2FC_FLAG_EXPL_LOGO,
124                                                 &tgt->flags);
125                                 kref_put(&io_req->refcount, bnx2fc_cmd_release);
126                                 spin_unlock_bh(&tgt->tgt_lock);
127
128                                 if (!logo_issued) {
129                                         BNX2FC_IO_DBG(io_req, "Explicit "
130                                                    "logo - tgt flags = 0x%lx\n",
131                                                    tgt->flags);
132
133
134                                         mutex_lock(&lport->disc.disc_mutex);
135                                         lport->tt.rport_logoff(rdata);
136                                         mutex_unlock(&lport->disc.disc_mutex);
137                                 }
138                                 return;
139                         } else {
140                                 BNX2FC_IO_DBG(io_req, "IO already in "
141                                                       "ABTS processing\n");
142                         }
143                 }
144                 break;
145         case BNX2FC_ELS:
146
147                 if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags)) {
148                         BNX2FC_IO_DBG(io_req, "ABTS for ELS timed out\n");
149
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,
156                                                 &tgt->flags);
157                                 kref_put(&io_req->refcount, bnx2fc_cmd_release);
158                                 spin_unlock_bh(&tgt->tgt_lock);
159
160                                 /* Explicitly logo the target */
161                                 if (!logo_issued) {
162                                         BNX2FC_IO_DBG(io_req, "Explicitly logo"
163                                                    "(els)\n");
164                                         mutex_lock(&lport->disc.disc_mutex);
165                                         lport->tt.rport_logoff(rdata);
166                                         mutex_unlock(&lport->disc.disc_mutex);
167                                 }
168                                 return;
169                         }
170                 } else {
171                         /*
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
176                          */
177                         BNX2FC_IO_DBG(io_req, "ELS timed out\n");
178                         if (test_and_set_bit(BNX2FC_FLAG_ELS_DONE,
179                                                &io_req->req_flags))
180                                 goto done;
181
182                         /* Indicate the cb_func that this ELS is timed out */
183                         set_bit(BNX2FC_FLAG_ELS_TIMEOUT, &io_req->req_flags);
184
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;
188                         }
189                 }
190                 break;
191         default:
192                 printk(KERN_ERR PFX "cmd_timeout: invalid cmd_type %d\n",
193                         cmd_type);
194                 break;
195         }
196
197 done:
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);
201 }
202
203 static void bnx2fc_scsi_done(struct bnx2fc_cmd *io_req, int err_code)
204 {
205         /* Called with host lock held */
206         struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
207
208         /*
209          * active_cmd_queue may have other command types as well,
210          * and during flush operation,  we want to error back only
211          * scsi commands.
212          */
213         if (io_req->cmd_type != BNX2FC_SCSI_CMD)
214                 return;
215
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;
219         if (!sc_cmd) {
220                 printk(KERN_ERR PFX "scsi_done - sc_cmd NULL. "
221                                     "IO(0x%x) already cleaned up\n",
222                        io_req->xid);
223                 return;
224         }
225         sc_cmd->result = err_code << 16;
226
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,
229                 sc_cmd->allowed);
230         scsi_set_resid(sc_cmd, scsi_bufflen(sc_cmd));
231         sc_cmd->SCp.ptr = NULL;
232         sc_cmd->scsi_done(sc_cmd);
233 }
234
235 struct bnx2fc_cmd_mgr *bnx2fc_cmd_mgr_alloc(struct bnx2fc_hba *hba,
236                                                 u16 min_xid, u16 max_xid)
237 {
238         struct bnx2fc_cmd_mgr *cmgr;
239         struct io_bdt *bdt_info;
240         struct bnx2fc_cmd *io_req;
241         size_t len;
242         u32 mem_size;
243         u16 xid;
244         int i;
245         int num_ios;
246         size_t bd_tbl_sz;
247
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);
251                 return NULL;
252         }
253         BNX2FC_MISC_DBG("min xid 0x%x, max xid 0x%x\n", min_xid, max_xid);
254
255         num_ios = max_xid - min_xid + 1;
256         len = (num_ios * (sizeof(struct bnx2fc_cmd *)));
257         len += sizeof(struct bnx2fc_cmd_mgr);
258
259         cmgr = kzalloc(len, GFP_KERNEL);
260         if (!cmgr) {
261                 printk(KERN_ERR PFX "failed to alloc cmgr\n");
262                 return NULL;
263         }
264
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");
269                 goto mem_err;
270         }
271
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");
276                 goto mem_err;
277         }
278
279         cmgr->hba = hba;
280         cmgr->cmds = (struct bnx2fc_cmd **)(cmgr + 1);
281
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]);
285         }
286
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);
291
292                 if (!io_req) {
293                         printk(KERN_ERR PFX "failed to alloc io_req\n");
294                         goto mem_err;
295                 }
296
297                 INIT_LIST_HEAD(&io_req->link);
298                 INIT_DELAYED_WORK(&io_req->timeout_work, bnx2fc_cmd_timeout);
299
300                 io_req->xid = xid++;
301                 if (io_req->xid >= BNX2FC_MAX_OUTSTANDING_CMNDS)
302                         printk(KERN_ERR PFX "ERROR allocating xids - 0x%x\n",
303                                 io_req->xid);
304                 list_add_tail(&io_req->link,
305                         &cmgr->free_list[io_req->xid % num_possible_cpus()]);
306                 io_req++;
307         }
308
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");
314                 goto mem_err;
315         }
316
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);
323                         goto mem_err;
324                 }
325         }
326
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,
332                                                       bd_tbl_sz,
333                                                       &bdt_info->bd_tbl_dma,
334                                                       GFP_KERNEL);
335                 if (!bdt_info->bd_tbl) {
336                         printk(KERN_ERR PFX "failed to alloc "
337                                 "bdt_tbl[%d]\n", i);
338                         goto mem_err;
339                 }
340         }
341
342         return cmgr;
343
344 mem_err:
345         bnx2fc_cmd_mgr_free(cmgr);
346         return NULL;
347 }
348
349 void bnx2fc_cmd_mgr_free(struct bnx2fc_cmd_mgr *cmgr)
350 {
351         struct io_bdt *bdt_info;
352         struct bnx2fc_hba *hba = cmgr->hba;
353         size_t bd_tbl_sz;
354         u16 min_xid = BNX2FC_MIN_XID;
355         u16 max_xid = BNX2FC_MAX_XID;
356         int num_ios;
357         int i;
358
359         num_ios = max_xid - min_xid + 1;
360
361         /* Free fcoe_bdt_ctx structures */
362         if (!cmgr->io_bdt_pool)
363                 goto free_cmd_pool;
364
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,
370                                             bdt_info->bd_tbl,
371                                             bdt_info->bd_tbl_dma);
372                         bdt_info->bd_tbl = NULL;
373                 }
374         }
375
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;
380         }
381
382         kfree(cmgr->io_bdt_pool);
383         cmgr->io_bdt_pool = NULL;
384
385 free_cmd_pool:
386         kfree(cmgr->free_list_lock);
387
388         /* Destroy cmd pool */
389         if (!cmgr->free_list)
390                 goto free_cmgr;
391
392         for (i = 0; i < num_possible_cpus(); i++)  {
393                 struct list_head *list;
394                 struct list_head *tmp;
395
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);
399                         kfree(io_req);
400                 }
401         }
402         kfree(cmgr->free_list);
403 free_cmgr:
404         /* Free command manager itself */
405         kfree(cmgr);
406 }
407
408 struct bnx2fc_cmd *bnx2fc_elstm_alloc(struct bnx2fc_rport *tgt, int type)
409 {
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;
416         u32 max_sqes;
417         u16 xid;
418
419         max_sqes = tgt->max_sqes;
420         switch (type) {
421         case BNX2FC_TASK_MGMT_CMD:
422                 max_sqes = BNX2FC_TM_MAX_SQES;
423                 break;
424         case BNX2FC_ELS:
425                 max_sqes = BNX2FC_ELS_MAX_SQES;
426                 break;
427         default:
428                 break;
429         }
430
431         /*
432          * NOTE: Free list insertions and deletions are protected with
433          * cmgr lock
434          */
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()]);
444                 return NULL;
445         }
446
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;
451         xid = io_req->xid;
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()]);
455
456         INIT_LIST_HEAD(&io_req->link);
457
458         io_req->port = port;
459         io_req->cmd_mgr = cmd_mgr;
460         io_req->req_flags = 0;
461         io_req->cmd_type = type;
462
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;
467
468         /* Hold the io_req  against deletion */
469         kref_init(&io_req->refcount);
470         return io_req;
471 }
472 static struct bnx2fc_cmd *bnx2fc_cmd_alloc(struct bnx2fc_rport *tgt)
473 {
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;
480         u32 max_sqes;
481         u16 xid;
482
483         max_sqes = BNX2FC_SCSI_MAX_SQES;
484         /*
485          * NOTE: Free list insertions and deletions are protected with
486          * cmgr lock
487          */
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()]);
492                 return NULL;
493         }
494
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;
499         xid = io_req->xid;
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()]);
503
504         INIT_LIST_HEAD(&io_req->link);
505
506         io_req->port = port;
507         io_req->cmd_mgr = cmd_mgr;
508         io_req->req_flags = 0;
509
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;
514
515         /* Hold the io_req  against deletion */
516         kref_init(&io_req->refcount);
517         return io_req;
518 }
519
520 void bnx2fc_cmd_release(struct kref *ref)
521 {
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;
525
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()]);
537 }
538
539 static void bnx2fc_free_mp_resc(struct bnx2fc_cmd *io_req)
540 {
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);
544
545         /* clear tm flags */
546         mp_req->tm_flags = 0;
547         if (mp_req->mp_req_bd) {
548                 dma_free_coherent(&hba->pcidev->dev, sz,
549                                      mp_req->mp_req_bd,
550                                      mp_req->mp_req_bd_dma);
551                 mp_req->mp_req_bd = NULL;
552         }
553         if (mp_req->mp_resp_bd) {
554                 dma_free_coherent(&hba->pcidev->dev, sz,
555                                      mp_req->mp_resp_bd,
556                                      mp_req->mp_resp_bd_dma);
557                 mp_req->mp_resp_bd = NULL;
558         }
559         if (mp_req->req_buf) {
560                 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
561                                      mp_req->req_buf,
562                                      mp_req->req_buf_dma);
563                 mp_req->req_buf = NULL;
564         }
565         if (mp_req->resp_buf) {
566                 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
567                                      mp_req->resp_buf,
568                                      mp_req->resp_buf_dma);
569                 mp_req->resp_buf = NULL;
570         }
571 }
572
573 int bnx2fc_init_mp_req(struct bnx2fc_cmd *io_req)
574 {
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;
579         dma_addr_t addr;
580         size_t sz;
581
582         mp_req = (struct bnx2fc_mp_req *)&(io_req->mp_req);
583         memset(mp_req, 0, sizeof(struct bnx2fc_mp_req));
584
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,
589                                              GFP_ATOMIC);
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);
593                 return FAILED;
594         }
595
596         mp_req->resp_buf = dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
597                                               &mp_req->resp_buf_dma,
598                                               GFP_ATOMIC);
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);
602                 return FAILED;
603         }
604         memset(mp_req->req_buf, 0, PAGE_SIZE);
605         memset(mp_req->resp_buf, 0, PAGE_SIZE);
606
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,
611                                                  GFP_ATOMIC);
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);
615                 return FAILED;
616         }
617         mp_req->mp_resp_bd = dma_alloc_coherent(&hba->pcidev->dev, sz,
618                                                  &mp_req->mp_resp_bd_dma,
619                                                  GFP_ATOMIC);
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);
623                 return FAILED;
624         }
625         /* Fill bd table */
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;
632
633         /*
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
637          */
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;
644
645         return SUCCESS;
646 }
647
648 static int bnx2fc_initiate_tmf(struct scsi_cmnd *sc_cmd, u8 tm_flags)
649 {
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;
663         int task_idx, index;
664         int rc = SUCCESS;
665         u16 xid;
666         u32 sid, did;
667         unsigned long start = jiffies;
668
669         lport = shost_priv(host);
670         port = lport_priv(lport);
671         hba = port->priv;
672
673         if (rport == NULL) {
674                 printk(KERN_ALERT PFX "device_reset: rport is NULL\n");
675                 rc = FAILED;
676                 goto tmf_err;
677         }
678
679         rc = fc_block_scsi_eh(sc_cmd);
680         if (rc)
681                 return rc;
682
683         if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
684                 printk(KERN_ERR PFX "device_reset: link is not ready\n");
685                 rc = FAILED;
686                 goto tmf_err;
687         }
688         /* rport and tgt are allocated together, so tgt should be non-NULL */
689         tgt = (struct bnx2fc_rport *)&rp[1];
690
691         if (!(test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
692                 printk(KERN_ERR PFX "device_reset: tgt not offloaded\n");
693                 rc = FAILED;
694                 goto tmf_err;
695         }
696 retry_tmf:
697         io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_TASK_MGMT_CMD);
698         if (!io_req) {
699                 if (time_after(jiffies, start + HZ)) {
700                         printk(KERN_ERR PFX "tmf: Failed TMF");
701                         rc = FAILED;
702                         goto tmf_err;
703                 }
704                 msleep(20);
705                 goto retry_tmf;
706         }
707         /* Initialize rest of io_req fields */
708         io_req->sc_cmd = sc_cmd;
709         io_req->port = port;
710         io_req->tgt = tgt;
711
712         tm_req = (struct bnx2fc_mp_req *)&(io_req->mp_req);
713
714         rc = bnx2fc_init_mp_req(io_req);
715         if (rc == FAILED) {
716                 printk(KERN_ERR PFX "Task mgmt MP request init failed\n");
717                 kref_put(&io_req->refcount, bnx2fc_cmd_release);
718                 goto tmf_err;
719         }
720
721         /* Set TM flags */
722         io_req->io_req_flags = 0;
723         tm_req->tm_flags = tm_flags;
724
725         /* Fill FCP_CMND */
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);
729         fcp_cmnd->fc_dl = 0;
730
731         /* Fill FC header */
732         fc_hdr = &(tm_req->req_fc_hdr);
733         sid = tgt->sid;
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 |
737                            FC_FC_SEQ_INIT, 0);
738         /* Obtain exchange id */
739         xid = io_req->xid;
740
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;
744
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);
749
750         sc_cmd->SCp.ptr = (char *)io_req;
751
752         /* Obtain free SQ entry */
753         spin_lock_bh(&tgt->tgt_lock);
754         bnx2fc_add_2_sq(tgt, xid);
755
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);
759
760         init_completion(&io_req->tm_done);
761         io_req->wait_for_comp = 1;
762
763         /* Ring doorbell */
764         bnx2fc_ring_doorbell(tgt);
765         spin_unlock_bh(&tgt->tgt_lock);
766
767         rc = wait_for_completion_timeout(&io_req->tm_done,
768                                          BNX2FC_TM_TIMEOUT * HZ);
769         spin_lock_bh(&tgt->tgt_lock);
770
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);
774
775         spin_unlock_bh(&tgt->tgt_lock);
776
777         if (!rc) {
778                 printk(KERN_ERR PFX "task mgmt command failed...\n");
779                 rc = FAILED;
780         } else {
781                 printk(KERN_ERR PFX "task mgmt command success...\n");
782                 rc = SUCCESS;
783         }
784 tmf_err:
785         return rc;
786 }
787
788 int bnx2fc_initiate_abts(struct bnx2fc_cmd *io_req)
789 {
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;
801         int task_idx, index;
802         u32 sid, did;
803         u16 xid;
804         int rc = SUCCESS;
805         u32 r_a_tov = rdata->r_a_tov;
806
807         /* called with tgt_lock held */
808         BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_abts\n");
809
810         port = io_req->port;
811         hba = port->priv;
812         lport = port->lport;
813
814         if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
815                 printk(KERN_ERR PFX "initiate_abts: tgt not offloaded\n");
816                 rc = FAILED;
817                 goto abts_err;
818         }
819
820         if (rport == NULL) {
821                 printk(KERN_ALERT PFX "initiate_abts: rport is NULL\n");
822                 rc = FAILED;
823                 goto abts_err;
824         }
825
826         if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
827                 printk(KERN_ERR PFX "initiate_abts: link is not ready\n");
828                 rc = FAILED;
829                 goto abts_err;
830         }
831
832         abts_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_ABTS);
833         if (!abts_io_req) {
834                 printk(KERN_ERR PFX "abts: couldnt allocate cmd\n");
835                 rc = FAILED;
836                 goto abts_err;
837         }
838
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 */
844
845         abts_req = (struct bnx2fc_mp_req *)&(abts_io_req->mp_req);
846         memset(abts_req, 0, sizeof(struct bnx2fc_mp_req));
847
848         /* Fill FC header */
849         fc_hdr = &(abts_req->req_fc_hdr);
850
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);
854
855         sid = tgt->sid;
856         did = rport->port_id;
857
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 |
860                            FC_FC_SEQ_INIT, 0);
861
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;
866
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);
871
872         /*
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
876          * IO request.
877          *
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.
881          */
882
883         /* if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags))*/
884         bnx2fc_cmd_timer_set(io_req, 2 * r_a_tov);
885
886         /* Obtain free SQ entry */
887         bnx2fc_add_2_sq(tgt, xid);
888
889         /* Ring doorbell */
890         bnx2fc_ring_doorbell(tgt);
891
892 abts_err:
893         return rc;
894 }
895
896 int bnx2fc_initiate_cleanup(struct bnx2fc_cmd *io_req)
897 {
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;
905         int task_idx, index;
906         u16 xid, orig_xid;
907         int rc = 0;
908
909         /* ASSUMPTION: called with tgt_lock held */
910         BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_cleanup\n");
911
912         port = io_req->port;
913         hba = port->priv;
914         lport = port->lport;
915
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");
919                 rc = -1;
920                 goto cleanup_err;
921         }
922
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 */
928
929         xid = cleanup_io_req->xid;
930
931         task_idx = xid/BNX2FC_TASKS_PER_PAGE;
932         index = xid % BNX2FC_TASKS_PER_PAGE;
933
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;
938
939         BNX2FC_IO_DBG(io_req, "CLEANUP io_req xid = 0x%x\n", xid);
940
941         bnx2fc_init_cleanup_task(cleanup_io_req, task, orig_xid);
942
943         /* Obtain free SQ entry */
944         bnx2fc_add_2_sq(tgt, xid);
945
946         /* Ring doorbell */
947         bnx2fc_ring_doorbell(tgt);
948
949 cleanup_err:
950         return rc;
951 }
952
953 /**
954  * bnx2fc_eh_target_reset: Reset a target
955  *
956  * @sc_cmd:     SCSI command
957  *
958  * Set from SCSI host template to send task mgmt command to the target
959  *      and wait for the response
960  */
961 int bnx2fc_eh_target_reset(struct scsi_cmnd *sc_cmd)
962 {
963         return bnx2fc_initiate_tmf(sc_cmd, FCP_TMF_TGT_RESET);
964 }
965
966 /**
967  * bnx2fc_eh_device_reset - Reset a single LUN
968  *
969  * @sc_cmd:     SCSI command
970  *
971  * Set from SCSI host template to send task mgmt command to the target
972  *      and wait for the response
973  */
974 int bnx2fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
975 {
976         return bnx2fc_initiate_tmf(sc_cmd, FCP_TMF_LUN_RESET);
977 }
978
979 /**
980  * bnx2fc_eh_abort - eh_abort_handler api to abort an outstanding
981  *                      SCSI command
982  *
983  * @sc_cmd:     SCSI_ML command pointer
984  *
985  * SCSI abort request handler
986  */
987 int bnx2fc_eh_abort(struct scsi_cmnd *sc_cmd)
988 {
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;
994         int rc = FAILED;
995
996
997         rc = fc_block_scsi_eh(sc_cmd);
998         if (rc)
999                 return rc;
1000
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");
1004                 return rc;
1005         }
1006
1007         tgt = (struct bnx2fc_rport *)&rp[1];
1008
1009         BNX2FC_TGT_DBG(tgt, "Entered bnx2fc_eh_abort\n");
1010
1011         spin_lock_bh(&tgt->tgt_lock);
1012         io_req = (struct bnx2fc_cmd *)sc_cmd->SCp.ptr;
1013         if (!io_req) {
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);
1017                 return SUCCESS;
1018         }
1019         BNX2FC_IO_DBG(io_req, "eh_abort - refcnt = %d\n",
1020                       io_req->refcount.refcount.counter);
1021
1022         /* Hold IO request across abort processing */
1023         kref_get(&io_req->refcount);
1024
1025         BUG_ON(tgt != io_req->tgt);
1026
1027         /* Remove the io_req from the active_q. */
1028         /*
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.
1032          */
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);
1038                 return SUCCESS;
1039         }
1040
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);
1044                 /*
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
1049                  */
1050                 bnx2fc_scsi_done(io_req, DID_ABORT);
1051
1052                 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1053                 spin_unlock_bh(&tgt->tgt_lock);
1054                 return SUCCESS;
1055         }
1056
1057         /*
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
1062          * processing
1063          */
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);
1068
1069         init_completion(&io_req->tm_done);
1070         io_req->wait_for_comp = 1;
1071
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);
1079         } else {
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);
1084                 return SUCCESS;
1085         }
1086         if (rc == FAILED) {
1087                 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1088                 spin_unlock_bh(&tgt->tgt_lock);
1089                 return rc;
1090         }
1091         spin_unlock_bh(&tgt->tgt_lock);
1092
1093         wait_for_completion(&io_req->tm_done);
1094
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",
1101                        io_req->xid);
1102                 rc = FAILED;
1103         } else {
1104                 /*
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.
1108                  */
1109                 BNX2FC_IO_DBG(io_req, "abort succeeded\n");
1110                 rc = SUCCESS;
1111                 bnx2fc_scsi_done(io_req, DID_ABORT);
1112                 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1113         }
1114
1115         /* release the reference taken in eh_abort */
1116         kref_put(&io_req->refcount, bnx2fc_cmd_release);
1117         spin_unlock_bh(&tgt->tgt_lock);
1118         return rc;
1119 }
1120
1121 void bnx2fc_process_cleanup_compl(struct bnx2fc_cmd *io_req,
1122                                   struct fcoe_task_ctx_entry *task,
1123                                   u8 num_rq)
1124 {
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);
1130 }
1131
1132 void bnx2fc_process_abts_compl(struct bnx2fc_cmd *io_req,
1133                                struct fcoe_task_ctx_entry *task,
1134                                u8 num_rq)
1135 {
1136         u32 r_ctl;
1137         u32 r_a_tov = FC_DEF_R_A_TOV;
1138         u8 issue_rrq = 0;
1139         struct bnx2fc_rport *tgt = io_req->tgt;
1140
1141         BNX2FC_IO_DBG(io_req, "Entered process_abts_compl xid = 0x%x"
1142                               "refcnt = %d, cmd_type = %d\n",
1143                    io_req->xid,
1144                    io_req->refcount.refcount.counter, io_req->cmd_type);
1145
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"
1149                                 " this io\n");
1150                 return;
1151         }
1152
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))
1156                 goto io_compl;
1157
1158         /*
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.
1163          */
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 */
1168
1169         r_ctl = task->cmn.general.rsp_info.abts_rsp.r_ctl;
1170
1171         switch (r_ctl) {
1172         case FC_RCTL_BA_ACC:
1173                 /*
1174                  * Dont release this cmd yet. It will be relesed
1175                  * after we get RRQ response
1176                  */
1177                 BNX2FC_IO_DBG(io_req, "ABTS response - ACC Send RRQ\n");
1178                 issue_rrq = 1;
1179                 break;
1180
1181         case FC_RCTL_BA_RJT:
1182                 BNX2FC_IO_DBG(io_req, "ABTS response - RJT\n");
1183                 break;
1184         default:
1185                 printk(KERN_ERR PFX "Unknown ABTS response\n");
1186                 break;
1187         }
1188
1189         if (issue_rrq) {
1190                 BNX2FC_IO_DBG(io_req, "Issue RRQ after R_A_TOV\n");
1191                 set_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags);
1192         }
1193         set_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags);
1194         bnx2fc_cmd_timer_set(io_req, r_a_tov);
1195
1196 io_compl:
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);
1201         } else {
1202                 /*
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
1208                  * out.
1209                  */
1210
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);
1216                 }
1217                 bnx2fc_scsi_done(io_req, DID_ERROR);
1218                 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1219         }
1220 }
1221
1222 static void bnx2fc_lun_reset_cmpl(struct bnx2fc_cmd *io_req)
1223 {
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;
1230         int rc = 0;
1231         int lun;
1232
1233         /* called with tgt_lock held */
1234         BNX2FC_IO_DBG(io_req, "Entered bnx2fc_lun_reset_cmpl\n");
1235         /*
1236          * Walk thru the active_ios queue and ABORT the IO
1237          * that matches with the LUN that was reset
1238          */
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,
1246                                               &cmd->req_flags)) {
1247                                 /* cancel the IO timeout */
1248                                 if (cancel_delayed_work(&io_req->timeout_work))
1249                                         kref_put(&io_req->refcount,
1250                                                  bnx2fc_cmd_release);
1251                                                         /* timer hold */
1252                                 rc = bnx2fc_initiate_abts(cmd);
1253                                 /* abts shouldnt fail in this context */
1254                                 WARN_ON(rc != SUCCESS);
1255                         } else
1256                                 printk(KERN_ERR PFX "lun_rst: abts already in"
1257                                         " progress for this IO 0x%x\n",
1258                                         cmd->xid);
1259                 }
1260         }
1261 }
1262
1263 static void bnx2fc_tgt_reset_cmpl(struct bnx2fc_cmd *io_req)
1264 {
1265         struct bnx2fc_rport *tgt = io_req->tgt;
1266         struct list_head *list;
1267         struct list_head *tmp;
1268         struct bnx2fc_cmd *cmd;
1269         int rc = 0;
1270
1271         /* called with tgt_lock held */
1272         BNX2FC_IO_DBG(io_req, "Entered bnx2fc_tgt_reset_cmpl\n");
1273         /*
1274          * Walk thru the active_ios queue and ABORT the IO
1275          * that matches with the LUN that was reset
1276          */
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;
1280                 /* Initiate ABTS */
1281                 if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
1282                                                         &cmd->req_flags)) {
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);
1290
1291                 } else
1292                         printk(KERN_ERR PFX "tgt_rst: abts already in progress"
1293                                 " for this IO 0x%x\n", cmd->xid);
1294         }
1295 }
1296
1297 void bnx2fc_process_tm_compl(struct bnx2fc_cmd *io_req,
1298                              struct fcoe_task_ctx_entry *task, u8 num_rq)
1299 {
1300         struct bnx2fc_mp_req *tm_req;
1301         struct fc_frame_header *fc_hdr;
1302         struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1303         u64 *hdr;
1304         u64 *temp_hdr;
1305         void *rsp_buf;
1306
1307         /* Called with tgt_lock held */
1308         BNX2FC_IO_DBG(io_req, "Entered process_tm_compl\n");
1309
1310         if (!(test_bit(BNX2FC_FLAG_TM_TIMEOUT, &io_req->req_flags)))
1311                 set_bit(BNX2FC_FLAG_TM_COMPL, &io_req->req_flags);
1312         else {
1313                 /* TM has already timed out and we got
1314                  * delayed completion. Ignore completion
1315                  * processing.
1316                  */
1317                 return;
1318         }
1319
1320         tm_req = &(io_req->mp_req);
1321         fc_hdr = &(tm_req->resp_fc_hdr);
1322         hdr = (u64 *)fc_hdr;
1323         temp_hdr = (u64 *)
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]);
1328
1329         tm_req->resp_len = task->rx_wr_only.sgl_ctx.mul_sges.cur_sge_off;
1330
1331         rsp_buf = tm_req->resp_buf;
1332
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 *)
1336                                      rsp_buf, num_rq);
1337                 if (io_req->fcp_rsp_code == 0) {
1338                         /* TM successful */
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);
1343                 }
1344         } else {
1345                 printk(KERN_ERR PFX "tmf's fc_hdr r_ctl = 0x%x\n",
1346                         fc_hdr->fh_r_ctl);
1347         }
1348         if (!sc_cmd->SCp.ptr) {
1349                 printk(KERN_ALERT PFX "tm_compl: SCp.ptr is NULL\n");
1350                 return;
1351         }
1352         switch (io_req->fcp_status) {
1353         case FC_GOOD:
1354                 if (io_req->cdb_status == 0) {
1355                         /* Good IO completion */
1356                         sc_cmd->result = DID_OK << 16;
1357                 } else {
1358                         /* Transport status is good, SCSI status not good */
1359                         sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
1360                 }
1361                 if (io_req->fcp_resid)
1362                         scsi_set_resid(sc_cmd, io_req->fcp_resid);
1363                 break;
1364
1365         default:
1366                 BNX2FC_IO_DBG(io_req, "process_tm_compl: fcp_status = %d\n",
1367                            io_req->fcp_status);
1368                 break;
1369         }
1370
1371         sc_cmd = io_req->sc_cmd;
1372         io_req->sc_cmd = NULL;
1373
1374         /* check if the io_req exists in tgt's tmf_q */
1375         if (io_req->on_tmf_queue) {
1376
1377                 list_del_init(&io_req->link);
1378                 io_req->on_tmf_queue = 0;
1379         } else {
1380
1381                 printk(KERN_ALERT PFX "Command not on active_cmd_queue!\n");
1382                 return;
1383         }
1384
1385         sc_cmd->SCp.ptr = NULL;
1386         sc_cmd->scsi_done(sc_cmd);
1387
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);
1392         }
1393 }
1394
1395 static int bnx2fc_split_bd(struct bnx2fc_cmd *io_req, u64 addr, int sg_len,
1396                            int bd_index)
1397 {
1398         struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
1399         int frag_size, sg_frags;
1400
1401         sg_frags = 0;
1402         while (sg_len) {
1403                 if (sg_len >= BNX2FC_BD_SPLIT_SZ)
1404                         frag_size = BNX2FC_BD_SPLIT_SZ;
1405                 else
1406                         frag_size = sg_len;
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;
1411
1412                 addr += (u64) frag_size;
1413                 sg_frags++;
1414                 sg_len -= frag_size;
1415         }
1416         return sg_frags;
1417
1418 }
1419
1420 static int bnx2fc_map_sg(struct bnx2fc_cmd *io_req)
1421 {
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;
1425         int byte_count = 0;
1426         int sg_count = 0;
1427         int bd_count = 0;
1428         int sg_frags;
1429         unsigned int sg_len;
1430         u64 addr;
1431         int i;
1432
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,
1439                                                    bd_count);
1440                 } else {
1441
1442                         sg_frags = 1;
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;
1447                 }
1448                 bd_count += sg_frags;
1449                 byte_count += sg_len;
1450         }
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),
1454                         io_req->xid);
1455         return bd_count;
1456 }
1457
1458 static void bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd *io_req)
1459 {
1460         struct scsi_cmnd *sc = io_req->sc_cmd;
1461         struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
1462         int bd_count;
1463
1464         if (scsi_sg_count(sc))
1465                 bd_count = bnx2fc_map_sg(io_req);
1466         else {
1467                 bd_count = 0;
1468                 bd[0].buf_addr_lo = bd[0].buf_addr_hi = 0;
1469                 bd[0].buf_len = bd[0].flags = 0;
1470         }
1471         io_req->bd_tbl->bd_valid = bd_count;
1472 }
1473
1474 static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd *io_req)
1475 {
1476         struct scsi_cmnd *sc = io_req->sc_cmd;
1477
1478         if (io_req->bd_tbl->bd_valid && sc) {
1479                 scsi_dma_unmap(sc);
1480                 io_req->bd_tbl->bd_valid = 0;
1481         }
1482 }
1483
1484 void bnx2fc_build_fcp_cmnd(struct bnx2fc_cmd *io_req,
1485                                   struct fcp_cmnd *fcp_cmnd)
1486 {
1487         struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1488         char tag[2];
1489
1490         memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
1491
1492         int_to_scsilun(sc_cmd->device->lun,
1493                         (struct scsi_lun *) fcp_cmnd->fc_lun);
1494
1495
1496         fcp_cmnd->fc_dl = htonl(io_req->data_xfer_len);
1497         memcpy(fcp_cmnd->fc_cdb, sc_cmd->cmnd, sc_cmd->cmd_len);
1498
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;
1503
1504         if (scsi_populate_tag_msg(sc_cmd, tag)) {
1505                 switch (tag[0]) {
1506                 case HEAD_OF_QUEUE_TAG:
1507                         fcp_cmnd->fc_pri_ta = FCP_PTA_HEADQ;
1508                         break;
1509                 case ORDERED_QUEUE_TAG:
1510                         fcp_cmnd->fc_pri_ta = FCP_PTA_ORDERED;
1511                         break;
1512                 default:
1513                         fcp_cmnd->fc_pri_ta = FCP_PTA_SIMPLE;
1514                         break;
1515                 }
1516         } else {
1517                 fcp_cmnd->fc_pri_ta = 0;
1518         }
1519 }
1520
1521 static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd *io_req,
1522                                  struct fcoe_fcp_rsp_payload *fcp_rsp,
1523                                  u8 num_rq)
1524 {
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;
1529         int i;
1530         unsigned char *rq_data;
1531         unsigned char *dummy;
1532         int fcp_sns_len = 0;
1533         int fcp_rsp_len = 0;
1534
1535         io_req->fcp_status = FC_GOOD;
1536         io_req->fcp_resid = fcp_rsp->fcp_resid;
1537
1538         io_req->scsi_comp_flags = rsp_flags;
1539         CMD_SCSI_STATUS(sc_cmd) = io_req->cdb_status =
1540                                 fcp_rsp->scsi_status_code;
1541
1542         /* Fetch fcp_rsp_info and fcp_sns_info if available */
1543         if (num_rq) {
1544
1545                 /*
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.
1549                  */
1550
1551                 if (rsp_flags &
1552                     FCOE_FCP_RSP_FLAGS_FCP_RSP_LEN_VALID) {
1553                         fcp_rsp_len = rq_buff_len
1554                                         = fcp_rsp->fcp_rsp_len;
1555                 }
1556
1557                 if (rsp_flags &
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;
1561                 }
1562
1563                 io_req->fcp_rsp_len = fcp_rsp_len;
1564                 io_req->fcp_sns_len = fcp_sns_len;
1565
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",
1569                                 rq_buff_len);
1570                         /* reset rq_buff_len */
1571                         rq_buff_len =  num_rq * BNX2FC_RQ_BUF_SZ;
1572                 }
1573
1574                 rq_data = bnx2fc_get_next_rqe(tgt, 1);
1575
1576                 if (num_rq > 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);
1580                 }
1581
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);
1588                 }
1589
1590                 /* fetch sense data */
1591                 rq_data += fcp_rsp_len;
1592
1593                 if (fcp_sns_len > SCSI_SENSE_BUFFERSIZE) {
1594                         printk(KERN_ERR PFX "Truncating sense buffer\n");
1595                         fcp_sns_len = SCSI_SENSE_BUFFERSIZE;
1596                 }
1597
1598                 memset(sc_cmd->sense_buffer, 0, sizeof(sc_cmd->sense_buffer));
1599                 if (fcp_sns_len)
1600                         memcpy(sc_cmd->sense_buffer, rq_data, fcp_sns_len);
1601
1602                 /* return RQ entries */
1603                 for (i = 0; i < num_rq; i++)
1604                         bnx2fc_return_rqe(tgt, 1);
1605         }
1606 }
1607
1608 /**
1609  * bnx2fc_queuecommand - Queuecommand function of the scsi template
1610  *
1611  * @host:       The Scsi_Host the command was issued to
1612  * @sc_cmd:     struct scsi_cmnd to be executed
1613  *
1614  * This is the IO strategy routine, called by SCSI-ML
1615  **/
1616 int bnx2fc_queuecommand(struct Scsi_Host *host,
1617                         struct scsi_cmnd *sc_cmd)
1618 {
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;
1624         int rc = 0;
1625         int rval;
1626
1627         rval = fc_remote_port_chkready(rport);
1628         if (rval) {
1629                 sc_cmd->result = rval;
1630                 sc_cmd->scsi_done(sc_cmd);
1631                 return 0;
1632         }
1633
1634         if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
1635                 rc = SCSI_MLQUEUE_HOST_BUSY;
1636                 goto exit_qcmd;
1637         }
1638
1639         /* rport and tgt are allocated together, so tgt should be non-NULL */
1640         tgt = (struct bnx2fc_rport *)&rp[1];
1641
1642         if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
1643                 /*
1644                  * Session is not offloaded yet. Let SCSI-ml retry
1645                  * the command.
1646                  */
1647                 rc = SCSI_MLQUEUE_TARGET_BUSY;
1648                 goto exit_qcmd;
1649         }
1650
1651         io_req = bnx2fc_cmd_alloc(tgt);
1652         if (!io_req) {
1653                 rc = SCSI_MLQUEUE_HOST_BUSY;
1654                 goto exit_qcmd;
1655         }
1656         io_req->sc_cmd = sc_cmd;
1657
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;
1661                 goto exit_qcmd;
1662         }
1663 exit_qcmd:
1664         return rc;
1665 }
1666
1667 void bnx2fc_process_scsi_cmd_compl(struct bnx2fc_cmd *io_req,
1668                                    struct fcoe_task_ctx_entry *task,
1669                                    u8 num_rq)
1670 {
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;
1675
1676
1677         /* scsi_cmd_cmpl is called with tgt lock held */
1678
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 "
1682                            "this scsi cmd\n");
1683         }
1684
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 */
1689
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");
1693                 return;
1694         }
1695
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);
1699
1700         /* parse fcp_rsp and obtain sense data from RQ if available */
1701         bnx2fc_parse_fcp_rsp(io_req, fcp_rsp, num_rq);
1702
1703         host = sc_cmd->device->host;
1704         if (!sc_cmd->SCp.ptr) {
1705                 printk(KERN_ERR PFX "SCp.ptr is NULL\n");
1706                 return;
1707         }
1708         io_req->sc_cmd = NULL;
1709
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);
1715         } else {
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.
1719                  */
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);
1725         }
1726
1727         bnx2fc_unmap_sg_list(io_req);
1728
1729         switch (io_req->fcp_status) {
1730         case FC_GOOD:
1731                 if (io_req->cdb_status == 0) {
1732                         /* Good IO completion */
1733                         sc_cmd->result = DID_OK << 16;
1734                 } else {
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;
1740                 }
1741                 if (io_req->fcp_resid)
1742                         scsi_set_resid(sc_cmd, io_req->fcp_resid);
1743                 break;
1744         default:
1745                 printk(KERN_ALERT PFX "scsi_cmd_compl: fcp_status = %d\n",
1746                         io_req->fcp_status);
1747                 break;
1748         }
1749         sc_cmd->SCp.ptr = NULL;
1750         sc_cmd->scsi_done(sc_cmd);
1751         kref_put(&io_req->refcount, bnx2fc_cmd_release);
1752 }
1753
1754 static int bnx2fc_post_io_req(struct bnx2fc_rport *tgt,
1755                                struct bnx2fc_cmd *io_req)
1756 {
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;
1765         u16 xid;
1766
1767         /* Initialize rest of io_req fields */
1768         io_req->cmd_type = BNX2FC_SCSI_CMD;
1769         io_req->port = port;
1770         io_req->tgt = tgt;
1771         io_req->data_xfer_len = scsi_bufflen(sc_cmd);
1772         sc_cmd->SCp.ptr = (char *)io_req;
1773
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;
1783         } else {
1784                 io_req->io_req_flags = 0;
1785                 stats->ControlRequests++;
1786         }
1787         put_cpu();
1788
1789         xid = io_req->xid;
1790
1791         /* Build buffer descriptor list for firmware from sg list */
1792         bnx2fc_build_bd_list_from_sg(io_req);
1793
1794         task_idx = xid / BNX2FC_TASKS_PER_PAGE;
1795         index = xid % BNX2FC_TASKS_PER_PAGE;
1796
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);
1801
1802         spin_lock_bh(&tgt->tgt_lock);
1803
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);
1808                 return -EAGAIN;
1809         }
1810
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);
1815                 return -EAGAIN;
1816         }
1817
1818         /* Time IO req */
1819         bnx2fc_cmd_timer_set(io_req, BNX2FC_IO_TIMEOUT);
1820         /* Obtain free SQ entry */
1821         bnx2fc_add_2_sq(tgt, xid);
1822
1823         /* Enqueue the io_req to active_cmd_queue */
1824
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);
1828
1829         /* Ring doorbell */
1830         bnx2fc_ring_doorbell(tgt);
1831         spin_unlock_bh(&tgt->tgt_lock);
1832         return 0;
1833 }