treewide: Replace GPLv2 boilerplate/reference with SPDX - gpl-2.0_168.RULE (part 1)
[platform/kernel/linux-starfive.git] / drivers / scsi / fnic / fnic_scsi.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2008 Cisco Systems, Inc.  All rights reserved.
4  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
5  */
6 #include <linux/mempool.h>
7 #include <linux/errno.h>
8 #include <linux/init.h>
9 #include <linux/workqueue.h>
10 #include <linux/pci.h>
11 #include <linux/scatterlist.h>
12 #include <linux/skbuff.h>
13 #include <linux/spinlock.h>
14 #include <linux/etherdevice.h>
15 #include <linux/if_ether.h>
16 #include <linux/if_vlan.h>
17 #include <linux/delay.h>
18 #include <linux/gfp.h>
19 #include <scsi/scsi.h>
20 #include <scsi/scsi_host.h>
21 #include <scsi/scsi_device.h>
22 #include <scsi/scsi_cmnd.h>
23 #include <scsi/scsi_tcq.h>
24 #include <scsi/fc/fc_els.h>
25 #include <scsi/fc/fc_fcoe.h>
26 #include <scsi/libfc.h>
27 #include <scsi/fc_frame.h>
28 #include "fnic_io.h"
29 #include "fnic.h"
30
31 const char *fnic_state_str[] = {
32         [FNIC_IN_FC_MODE] =           "FNIC_IN_FC_MODE",
33         [FNIC_IN_FC_TRANS_ETH_MODE] = "FNIC_IN_FC_TRANS_ETH_MODE",
34         [FNIC_IN_ETH_MODE] =          "FNIC_IN_ETH_MODE",
35         [FNIC_IN_ETH_TRANS_FC_MODE] = "FNIC_IN_ETH_TRANS_FC_MODE",
36 };
37
38 static const char *fnic_ioreq_state_str[] = {
39         [FNIC_IOREQ_NOT_INITED] = "FNIC_IOREQ_NOT_INITED",
40         [FNIC_IOREQ_CMD_PENDING] = "FNIC_IOREQ_CMD_PENDING",
41         [FNIC_IOREQ_ABTS_PENDING] = "FNIC_IOREQ_ABTS_PENDING",
42         [FNIC_IOREQ_ABTS_COMPLETE] = "FNIC_IOREQ_ABTS_COMPLETE",
43         [FNIC_IOREQ_CMD_COMPLETE] = "FNIC_IOREQ_CMD_COMPLETE",
44 };
45
46 static const char *fcpio_status_str[] =  {
47         [FCPIO_SUCCESS] = "FCPIO_SUCCESS", /*0x0*/
48         [FCPIO_INVALID_HEADER] = "FCPIO_INVALID_HEADER",
49         [FCPIO_OUT_OF_RESOURCE] = "FCPIO_OUT_OF_RESOURCE",
50         [FCPIO_INVALID_PARAM] = "FCPIO_INVALID_PARAM]",
51         [FCPIO_REQ_NOT_SUPPORTED] = "FCPIO_REQ_NOT_SUPPORTED",
52         [FCPIO_IO_NOT_FOUND] = "FCPIO_IO_NOT_FOUND",
53         [FCPIO_ABORTED] = "FCPIO_ABORTED", /*0x41*/
54         [FCPIO_TIMEOUT] = "FCPIO_TIMEOUT",
55         [FCPIO_SGL_INVALID] = "FCPIO_SGL_INVALID",
56         [FCPIO_MSS_INVALID] = "FCPIO_MSS_INVALID",
57         [FCPIO_DATA_CNT_MISMATCH] = "FCPIO_DATA_CNT_MISMATCH",
58         [FCPIO_FW_ERR] = "FCPIO_FW_ERR",
59         [FCPIO_ITMF_REJECTED] = "FCPIO_ITMF_REJECTED",
60         [FCPIO_ITMF_FAILED] = "FCPIO_ITMF_FAILED",
61         [FCPIO_ITMF_INCORRECT_LUN] = "FCPIO_ITMF_INCORRECT_LUN",
62         [FCPIO_CMND_REJECTED] = "FCPIO_CMND_REJECTED",
63         [FCPIO_NO_PATH_AVAIL] = "FCPIO_NO_PATH_AVAIL",
64         [FCPIO_PATH_FAILED] = "FCPIO_PATH_FAILED",
65         [FCPIO_LUNMAP_CHNG_PEND] = "FCPIO_LUNHMAP_CHNG_PEND",
66 };
67
68 const char *fnic_state_to_str(unsigned int state)
69 {
70         if (state >= ARRAY_SIZE(fnic_state_str) || !fnic_state_str[state])
71                 return "unknown";
72
73         return fnic_state_str[state];
74 }
75
76 static const char *fnic_ioreq_state_to_str(unsigned int state)
77 {
78         if (state >= ARRAY_SIZE(fnic_ioreq_state_str) ||
79             !fnic_ioreq_state_str[state])
80                 return "unknown";
81
82         return fnic_ioreq_state_str[state];
83 }
84
85 static const char *fnic_fcpio_status_to_str(unsigned int status)
86 {
87         if (status >= ARRAY_SIZE(fcpio_status_str) || !fcpio_status_str[status])
88                 return "unknown";
89
90         return fcpio_status_str[status];
91 }
92
93 static void fnic_cleanup_io(struct fnic *fnic);
94
95 static inline spinlock_t *fnic_io_lock_hash(struct fnic *fnic,
96                                             struct scsi_cmnd *sc)
97 {
98         u32 hash = scsi_cmd_to_rq(sc)->tag & (FNIC_IO_LOCKS - 1);
99
100         return &fnic->io_req_lock[hash];
101 }
102
103 static inline spinlock_t *fnic_io_lock_tag(struct fnic *fnic,
104                                             int tag)
105 {
106         return &fnic->io_req_lock[tag & (FNIC_IO_LOCKS - 1)];
107 }
108
109 /*
110  * Unmap the data buffer and sense buffer for an io_req,
111  * also unmap and free the device-private scatter/gather list.
112  */
113 static void fnic_release_ioreq_buf(struct fnic *fnic,
114                                    struct fnic_io_req *io_req,
115                                    struct scsi_cmnd *sc)
116 {
117         if (io_req->sgl_list_pa)
118                 dma_unmap_single(&fnic->pdev->dev, io_req->sgl_list_pa,
119                                  sizeof(io_req->sgl_list[0]) * io_req->sgl_cnt,
120                                  DMA_TO_DEVICE);
121         scsi_dma_unmap(sc);
122
123         if (io_req->sgl_cnt)
124                 mempool_free(io_req->sgl_list_alloc,
125                              fnic->io_sgl_pool[io_req->sgl_type]);
126         if (io_req->sense_buf_pa)
127                 dma_unmap_single(&fnic->pdev->dev, io_req->sense_buf_pa,
128                                  SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
129 }
130
131 /* Free up Copy Wq descriptors. Called with copy_wq lock held */
132 static int free_wq_copy_descs(struct fnic *fnic, struct vnic_wq_copy *wq)
133 {
134         /* if no Ack received from firmware, then nothing to clean */
135         if (!fnic->fw_ack_recd[0])
136                 return 1;
137
138         /*
139          * Update desc_available count based on number of freed descriptors
140          * Account for wraparound
141          */
142         if (wq->to_clean_index <= fnic->fw_ack_index[0])
143                 wq->ring.desc_avail += (fnic->fw_ack_index[0]
144                                         - wq->to_clean_index + 1);
145         else
146                 wq->ring.desc_avail += (wq->ring.desc_count
147                                         - wq->to_clean_index
148                                         + fnic->fw_ack_index[0] + 1);
149
150         /*
151          * just bump clean index to ack_index+1 accounting for wraparound
152          * this will essentially free up all descriptors between
153          * to_clean_index and fw_ack_index, both inclusive
154          */
155         wq->to_clean_index =
156                 (fnic->fw_ack_index[0] + 1) % wq->ring.desc_count;
157
158         /* we have processed the acks received so far */
159         fnic->fw_ack_recd[0] = 0;
160         return 0;
161 }
162
163
164 /*
165  * __fnic_set_state_flags
166  * Sets/Clears bits in fnic's state_flags
167  **/
168 void
169 __fnic_set_state_flags(struct fnic *fnic, unsigned long st_flags,
170                         unsigned long clearbits)
171 {
172         unsigned long flags = 0;
173         unsigned long host_lock_flags = 0;
174
175         spin_lock_irqsave(&fnic->fnic_lock, flags);
176         spin_lock_irqsave(fnic->lport->host->host_lock, host_lock_flags);
177
178         if (clearbits)
179                 fnic->state_flags &= ~st_flags;
180         else
181                 fnic->state_flags |= st_flags;
182
183         spin_unlock_irqrestore(fnic->lport->host->host_lock, host_lock_flags);
184         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
185
186         return;
187 }
188
189
190 /*
191  * fnic_fw_reset_handler
192  * Routine to send reset msg to fw
193  */
194 int fnic_fw_reset_handler(struct fnic *fnic)
195 {
196         struct vnic_wq_copy *wq = &fnic->wq_copy[0];
197         int ret = 0;
198         unsigned long flags;
199
200         /* indicate fwreset to io path */
201         fnic_set_state_flags(fnic, FNIC_FLAGS_FWRESET);
202
203         skb_queue_purge(&fnic->frame_queue);
204         skb_queue_purge(&fnic->tx_queue);
205
206         /* wait for io cmpl */
207         while (atomic_read(&fnic->in_flight))
208                 schedule_timeout(msecs_to_jiffies(1));
209
210         spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
211
212         if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
213                 free_wq_copy_descs(fnic, wq);
214
215         if (!vnic_wq_copy_desc_avail(wq))
216                 ret = -EAGAIN;
217         else {
218                 fnic_queue_wq_copy_desc_fw_reset(wq, SCSI_NO_TAG);
219                 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
220                 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
221                           atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
222                         atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
223                                 atomic64_read(
224                                   &fnic->fnic_stats.fw_stats.active_fw_reqs));
225         }
226
227         spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
228
229         if (!ret) {
230                 atomic64_inc(&fnic->fnic_stats.reset_stats.fw_resets);
231                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
232                               "Issued fw reset\n");
233         } else {
234                 fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
235                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
236                               "Failed to issue fw reset\n");
237         }
238
239         return ret;
240 }
241
242
243 /*
244  * fnic_flogi_reg_handler
245  * Routine to send flogi register msg to fw
246  */
247 int fnic_flogi_reg_handler(struct fnic *fnic, u32 fc_id)
248 {
249         struct vnic_wq_copy *wq = &fnic->wq_copy[0];
250         enum fcpio_flogi_reg_format_type format;
251         struct fc_lport *lp = fnic->lport;
252         u8 gw_mac[ETH_ALEN];
253         int ret = 0;
254         unsigned long flags;
255
256         spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
257
258         if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
259                 free_wq_copy_descs(fnic, wq);
260
261         if (!vnic_wq_copy_desc_avail(wq)) {
262                 ret = -EAGAIN;
263                 goto flogi_reg_ioreq_end;
264         }
265
266         if (fnic->ctlr.map_dest) {
267                 eth_broadcast_addr(gw_mac);
268                 format = FCPIO_FLOGI_REG_DEF_DEST;
269         } else {
270                 memcpy(gw_mac, fnic->ctlr.dest_addr, ETH_ALEN);
271                 format = FCPIO_FLOGI_REG_GW_DEST;
272         }
273
274         if ((fnic->config.flags & VFCF_FIP_CAPABLE) && !fnic->ctlr.map_dest) {
275                 fnic_queue_wq_copy_desc_fip_reg(wq, SCSI_NO_TAG,
276                                                 fc_id, gw_mac,
277                                                 fnic->data_src_addr,
278                                                 lp->r_a_tov, lp->e_d_tov);
279                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
280                               "FLOGI FIP reg issued fcid %x src %pM dest %pM\n",
281                               fc_id, fnic->data_src_addr, gw_mac);
282         } else {
283                 fnic_queue_wq_copy_desc_flogi_reg(wq, SCSI_NO_TAG,
284                                                   format, fc_id, gw_mac);
285                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
286                               "FLOGI reg issued fcid %x map %d dest %pM\n",
287                               fc_id, fnic->ctlr.map_dest, gw_mac);
288         }
289
290         atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
291         if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
292                   atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
293                 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
294                   atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
295
296 flogi_reg_ioreq_end:
297         spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
298         return ret;
299 }
300
301 /*
302  * fnic_queue_wq_copy_desc
303  * Routine to enqueue a wq copy desc
304  */
305 static inline int fnic_queue_wq_copy_desc(struct fnic *fnic,
306                                           struct vnic_wq_copy *wq,
307                                           struct fnic_io_req *io_req,
308                                           struct scsi_cmnd *sc,
309                                           int sg_count)
310 {
311         struct scatterlist *sg;
312         struct fc_rport *rport = starget_to_rport(scsi_target(sc->device));
313         struct fc_rport_libfc_priv *rp = rport->dd_data;
314         struct host_sg_desc *desc;
315         struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
316         unsigned int i;
317         unsigned long intr_flags;
318         int flags;
319         u8 exch_flags;
320         struct scsi_lun fc_lun;
321
322         if (sg_count) {
323                 /* For each SGE, create a device desc entry */
324                 desc = io_req->sgl_list;
325                 for_each_sg(scsi_sglist(sc), sg, sg_count, i) {
326                         desc->addr = cpu_to_le64(sg_dma_address(sg));
327                         desc->len = cpu_to_le32(sg_dma_len(sg));
328                         desc->_resvd = 0;
329                         desc++;
330                 }
331
332                 io_req->sgl_list_pa = dma_map_single(&fnic->pdev->dev,
333                                 io_req->sgl_list,
334                                 sizeof(io_req->sgl_list[0]) * sg_count,
335                                 DMA_TO_DEVICE);
336                 if (dma_mapping_error(&fnic->pdev->dev, io_req->sgl_list_pa)) {
337                         printk(KERN_ERR "DMA mapping failed\n");
338                         return SCSI_MLQUEUE_HOST_BUSY;
339                 }
340         }
341
342         io_req->sense_buf_pa = dma_map_single(&fnic->pdev->dev,
343                                               sc->sense_buffer,
344                                               SCSI_SENSE_BUFFERSIZE,
345                                               DMA_FROM_DEVICE);
346         if (dma_mapping_error(&fnic->pdev->dev, io_req->sense_buf_pa)) {
347                 dma_unmap_single(&fnic->pdev->dev, io_req->sgl_list_pa,
348                                 sizeof(io_req->sgl_list[0]) * sg_count,
349                                 DMA_TO_DEVICE);
350                 printk(KERN_ERR "DMA mapping failed\n");
351                 return SCSI_MLQUEUE_HOST_BUSY;
352         }
353
354         int_to_scsilun(sc->device->lun, &fc_lun);
355
356         /* Enqueue the descriptor in the Copy WQ */
357         spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
358
359         if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
360                 free_wq_copy_descs(fnic, wq);
361
362         if (unlikely(!vnic_wq_copy_desc_avail(wq))) {
363                 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
364                 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
365                           "fnic_queue_wq_copy_desc failure - no descriptors\n");
366                 atomic64_inc(&misc_stats->io_cpwq_alloc_failures);
367                 return SCSI_MLQUEUE_HOST_BUSY;
368         }
369
370         flags = 0;
371         if (sc->sc_data_direction == DMA_FROM_DEVICE)
372                 flags = FCPIO_ICMND_RDDATA;
373         else if (sc->sc_data_direction == DMA_TO_DEVICE)
374                 flags = FCPIO_ICMND_WRDATA;
375
376         exch_flags = 0;
377         if ((fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR) &&
378             (rp->flags & FC_RP_FLAGS_RETRY))
379                 exch_flags |= FCPIO_ICMND_SRFLAG_RETRY;
380
381         fnic_queue_wq_copy_desc_icmnd_16(wq, scsi_cmd_to_rq(sc)->tag,
382                                          0, exch_flags, io_req->sgl_cnt,
383                                          SCSI_SENSE_BUFFERSIZE,
384                                          io_req->sgl_list_pa,
385                                          io_req->sense_buf_pa,
386                                          0, /* scsi cmd ref, always 0 */
387                                          FCPIO_ICMND_PTA_SIMPLE,
388                                                 /* scsi pri and tag */
389                                          flags, /* command flags */
390                                          sc->cmnd, sc->cmd_len,
391                                          scsi_bufflen(sc),
392                                          fc_lun.scsi_lun, io_req->port_id,
393                                          rport->maxframe_size, rp->r_a_tov,
394                                          rp->e_d_tov);
395
396         atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
397         if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
398                   atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
399                 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
400                   atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
401
402         spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
403         return 0;
404 }
405
406 /*
407  * fnic_queuecommand
408  * Routine to send a scsi cdb
409  * Called with host_lock held and interrupts disabled.
410  */
411 static int fnic_queuecommand_lck(struct scsi_cmnd *sc)
412 {
413         void (*done)(struct scsi_cmnd *) = scsi_done;
414         const int tag = scsi_cmd_to_rq(sc)->tag;
415         struct fc_lport *lp = shost_priv(sc->device->host);
416         struct fc_rport *rport;
417         struct fnic_io_req *io_req = NULL;
418         struct fnic *fnic = lport_priv(lp);
419         struct fnic_stats *fnic_stats = &fnic->fnic_stats;
420         struct vnic_wq_copy *wq;
421         int ret;
422         u64 cmd_trace;
423         int sg_count = 0;
424         unsigned long flags = 0;
425         unsigned long ptr;
426         spinlock_t *io_lock = NULL;
427         int io_lock_acquired = 0;
428         struct fc_rport_libfc_priv *rp;
429
430         if (unlikely(fnic_chk_state_flags_locked(fnic, FNIC_FLAGS_IO_BLOCKED)))
431                 return SCSI_MLQUEUE_HOST_BUSY;
432
433         if (unlikely(fnic_chk_state_flags_locked(fnic, FNIC_FLAGS_FWRESET)))
434                 return SCSI_MLQUEUE_HOST_BUSY;
435
436         rport = starget_to_rport(scsi_target(sc->device));
437         if (!rport) {
438                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
439                                 "returning DID_NO_CONNECT for IO as rport is NULL\n");
440                 sc->result = DID_NO_CONNECT << 16;
441                 done(sc);
442                 return 0;
443         }
444
445         ret = fc_remote_port_chkready(rport);
446         if (ret) {
447                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
448                                 "rport is not ready\n");
449                 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
450                 sc->result = ret;
451                 done(sc);
452                 return 0;
453         }
454
455         rp = rport->dd_data;
456         if (!rp || rp->rp_state == RPORT_ST_DELETE) {
457                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
458                         "rport 0x%x removed, returning DID_NO_CONNECT\n",
459                         rport->port_id);
460
461                 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
462                 sc->result = DID_NO_CONNECT<<16;
463                 done(sc);
464                 return 0;
465         }
466
467         if (rp->rp_state != RPORT_ST_READY) {
468                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
469                         "rport 0x%x in state 0x%x, returning DID_IMM_RETRY\n",
470                         rport->port_id, rp->rp_state);
471
472                 sc->result = DID_IMM_RETRY << 16;
473                 done(sc);
474                 return 0;
475         }
476
477         if (lp->state != LPORT_ST_READY || !(lp->link_up))
478                 return SCSI_MLQUEUE_HOST_BUSY;
479
480         atomic_inc(&fnic->in_flight);
481
482         /*
483          * Release host lock, use driver resource specific locks from here.
484          * Don't re-enable interrupts in case they were disabled prior to the
485          * caller disabling them.
486          */
487         spin_unlock(lp->host->host_lock);
488         fnic_priv(sc)->state = FNIC_IOREQ_NOT_INITED;
489         fnic_priv(sc)->flags = FNIC_NO_FLAGS;
490
491         /* Get a new io_req for this SCSI IO */
492         io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
493         if (!io_req) {
494                 atomic64_inc(&fnic_stats->io_stats.alloc_failures);
495                 ret = SCSI_MLQUEUE_HOST_BUSY;
496                 goto out;
497         }
498         memset(io_req, 0, sizeof(*io_req));
499
500         /* Map the data buffer */
501         sg_count = scsi_dma_map(sc);
502         if (sg_count < 0) {
503                 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
504                           tag, sc, 0, sc->cmnd[0], sg_count, fnic_priv(sc)->state);
505                 mempool_free(io_req, fnic->io_req_pool);
506                 goto out;
507         }
508
509         /* Determine the type of scatter/gather list we need */
510         io_req->sgl_cnt = sg_count;
511         io_req->sgl_type = FNIC_SGL_CACHE_DFLT;
512         if (sg_count > FNIC_DFLT_SG_DESC_CNT)
513                 io_req->sgl_type = FNIC_SGL_CACHE_MAX;
514
515         if (sg_count) {
516                 io_req->sgl_list =
517                         mempool_alloc(fnic->io_sgl_pool[io_req->sgl_type],
518                                       GFP_ATOMIC);
519                 if (!io_req->sgl_list) {
520                         atomic64_inc(&fnic_stats->io_stats.alloc_failures);
521                         ret = SCSI_MLQUEUE_HOST_BUSY;
522                         scsi_dma_unmap(sc);
523                         mempool_free(io_req, fnic->io_req_pool);
524                         goto out;
525                 }
526
527                 /* Cache sgl list allocated address before alignment */
528                 io_req->sgl_list_alloc = io_req->sgl_list;
529                 ptr = (unsigned long) io_req->sgl_list;
530                 if (ptr % FNIC_SG_DESC_ALIGN) {
531                         io_req->sgl_list = (struct host_sg_desc *)
532                                 (((unsigned long) ptr
533                                   + FNIC_SG_DESC_ALIGN - 1)
534                                  & ~(FNIC_SG_DESC_ALIGN - 1));
535                 }
536         }
537
538         /*
539         * Will acquire lock defore setting to IO initialized.
540         */
541
542         io_lock = fnic_io_lock_hash(fnic, sc);
543         spin_lock_irqsave(io_lock, flags);
544
545         /* initialize rest of io_req */
546         io_lock_acquired = 1;
547         io_req->port_id = rport->port_id;
548         io_req->start_time = jiffies;
549         fnic_priv(sc)->state = FNIC_IOREQ_CMD_PENDING;
550         fnic_priv(sc)->io_req = io_req;
551         fnic_priv(sc)->flags |= FNIC_IO_INITIALIZED;
552
553         /* create copy wq desc and enqueue it */
554         wq = &fnic->wq_copy[0];
555         ret = fnic_queue_wq_copy_desc(fnic, wq, io_req, sc, sg_count);
556         if (ret) {
557                 /*
558                  * In case another thread cancelled the request,
559                  * refetch the pointer under the lock.
560                  */
561                 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
562                           tag, sc, 0, 0, 0, fnic_flags_and_state(sc));
563                 io_req = fnic_priv(sc)->io_req;
564                 fnic_priv(sc)->io_req = NULL;
565                 fnic_priv(sc)->state = FNIC_IOREQ_CMD_COMPLETE;
566                 spin_unlock_irqrestore(io_lock, flags);
567                 if (io_req) {
568                         fnic_release_ioreq_buf(fnic, io_req, sc);
569                         mempool_free(io_req, fnic->io_req_pool);
570                 }
571                 atomic_dec(&fnic->in_flight);
572                 /* acquire host lock before returning to SCSI */
573                 spin_lock(lp->host->host_lock);
574                 return ret;
575         } else {
576                 atomic64_inc(&fnic_stats->io_stats.active_ios);
577                 atomic64_inc(&fnic_stats->io_stats.num_ios);
578                 if (atomic64_read(&fnic_stats->io_stats.active_ios) >
579                           atomic64_read(&fnic_stats->io_stats.max_active_ios))
580                         atomic64_set(&fnic_stats->io_stats.max_active_ios,
581                              atomic64_read(&fnic_stats->io_stats.active_ios));
582
583                 /* REVISIT: Use per IO lock in the final code */
584                 fnic_priv(sc)->flags |= FNIC_IO_ISSUED;
585         }
586 out:
587         cmd_trace = ((u64)sc->cmnd[0] << 56 | (u64)sc->cmnd[7] << 40 |
588                         (u64)sc->cmnd[8] << 32 | (u64)sc->cmnd[2] << 24 |
589                         (u64)sc->cmnd[3] << 16 | (u64)sc->cmnd[4] << 8 |
590                         sc->cmnd[5]);
591
592         FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
593                    tag, sc, io_req, sg_count, cmd_trace,
594                    fnic_flags_and_state(sc));
595
596         /* if only we issued IO, will we have the io lock */
597         if (io_lock_acquired)
598                 spin_unlock_irqrestore(io_lock, flags);
599
600         atomic_dec(&fnic->in_flight);
601         /* acquire host lock before returning to SCSI */
602         spin_lock(lp->host->host_lock);
603         return ret;
604 }
605
606 DEF_SCSI_QCMD(fnic_queuecommand)
607
608 /*
609  * fnic_fcpio_fw_reset_cmpl_handler
610  * Routine to handle fw reset completion
611  */
612 static int fnic_fcpio_fw_reset_cmpl_handler(struct fnic *fnic,
613                                             struct fcpio_fw_req *desc)
614 {
615         u8 type;
616         u8 hdr_status;
617         struct fcpio_tag tag;
618         int ret = 0;
619         unsigned long flags;
620         struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
621
622         fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
623
624         atomic64_inc(&reset_stats->fw_reset_completions);
625
626         /* Clean up all outstanding io requests */
627         fnic_cleanup_io(fnic);
628
629         atomic64_set(&fnic->fnic_stats.fw_stats.active_fw_reqs, 0);
630         atomic64_set(&fnic->fnic_stats.io_stats.active_ios, 0);
631         atomic64_set(&fnic->io_cmpl_skip, 0);
632
633         spin_lock_irqsave(&fnic->fnic_lock, flags);
634
635         /* fnic should be in FC_TRANS_ETH_MODE */
636         if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) {
637                 /* Check status of reset completion */
638                 if (!hdr_status) {
639                         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
640                                       "reset cmpl success\n");
641                         /* Ready to send flogi out */
642                         fnic->state = FNIC_IN_ETH_MODE;
643                 } else {
644                         FNIC_SCSI_DBG(KERN_DEBUG,
645                                       fnic->lport->host,
646                                       "fnic fw_reset : failed %s\n",
647                                       fnic_fcpio_status_to_str(hdr_status));
648
649                         /*
650                          * Unable to change to eth mode, cannot send out flogi
651                          * Change state to fc mode, so that subsequent Flogi
652                          * requests from libFC will cause more attempts to
653                          * reset the firmware. Free the cached flogi
654                          */
655                         fnic->state = FNIC_IN_FC_MODE;
656                         atomic64_inc(&reset_stats->fw_reset_failures);
657                         ret = -1;
658                 }
659         } else {
660                 FNIC_SCSI_DBG(KERN_DEBUG,
661                               fnic->lport->host,
662                               "Unexpected state %s while processing"
663                               " reset cmpl\n", fnic_state_to_str(fnic->state));
664                 atomic64_inc(&reset_stats->fw_reset_failures);
665                 ret = -1;
666         }
667
668         /* Thread removing device blocks till firmware reset is complete */
669         if (fnic->remove_wait)
670                 complete(fnic->remove_wait);
671
672         /*
673          * If fnic is being removed, or fw reset failed
674          * free the flogi frame. Else, send it out
675          */
676         if (fnic->remove_wait || ret) {
677                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
678                 skb_queue_purge(&fnic->tx_queue);
679                 goto reset_cmpl_handler_end;
680         }
681
682         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
683
684         fnic_flush_tx(fnic);
685
686  reset_cmpl_handler_end:
687         fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
688
689         return ret;
690 }
691
692 /*
693  * fnic_fcpio_flogi_reg_cmpl_handler
694  * Routine to handle flogi register completion
695  */
696 static int fnic_fcpio_flogi_reg_cmpl_handler(struct fnic *fnic,
697                                              struct fcpio_fw_req *desc)
698 {
699         u8 type;
700         u8 hdr_status;
701         struct fcpio_tag tag;
702         int ret = 0;
703         unsigned long flags;
704
705         fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
706
707         /* Update fnic state based on status of flogi reg completion */
708         spin_lock_irqsave(&fnic->fnic_lock, flags);
709
710         if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE) {
711
712                 /* Check flogi registration completion status */
713                 if (!hdr_status) {
714                         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
715                                       "flog reg succeeded\n");
716                         fnic->state = FNIC_IN_FC_MODE;
717                 } else {
718                         FNIC_SCSI_DBG(KERN_DEBUG,
719                                       fnic->lport->host,
720                                       "fnic flogi reg :failed %s\n",
721                                       fnic_fcpio_status_to_str(hdr_status));
722                         fnic->state = FNIC_IN_ETH_MODE;
723                         ret = -1;
724                 }
725         } else {
726                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
727                               "Unexpected fnic state %s while"
728                               " processing flogi reg completion\n",
729                               fnic_state_to_str(fnic->state));
730                 ret = -1;
731         }
732
733         if (!ret) {
734                 if (fnic->stop_rx_link_events) {
735                         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
736                         goto reg_cmpl_handler_end;
737                 }
738                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
739
740                 fnic_flush_tx(fnic);
741                 queue_work(fnic_event_queue, &fnic->frame_work);
742         } else {
743                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
744         }
745
746 reg_cmpl_handler_end:
747         return ret;
748 }
749
750 static inline int is_ack_index_in_range(struct vnic_wq_copy *wq,
751                                         u16 request_out)
752 {
753         if (wq->to_clean_index <= wq->to_use_index) {
754                 /* out of range, stale request_out index */
755                 if (request_out < wq->to_clean_index ||
756                     request_out >= wq->to_use_index)
757                         return 0;
758         } else {
759                 /* out of range, stale request_out index */
760                 if (request_out < wq->to_clean_index &&
761                     request_out >= wq->to_use_index)
762                         return 0;
763         }
764         /* request_out index is in range */
765         return 1;
766 }
767
768
769 /*
770  * Mark that ack received and store the Ack index. If there are multiple
771  * acks received before Tx thread cleans it up, the latest value will be
772  * used which is correct behavior. This state should be in the copy Wq
773  * instead of in the fnic
774  */
775 static inline void fnic_fcpio_ack_handler(struct fnic *fnic,
776                                           unsigned int cq_index,
777                                           struct fcpio_fw_req *desc)
778 {
779         struct vnic_wq_copy *wq;
780         u16 request_out = desc->u.ack.request_out;
781         unsigned long flags;
782         u64 *ox_id_tag = (u64 *)(void *)desc;
783
784         /* mark the ack state */
785         wq = &fnic->wq_copy[cq_index - fnic->raw_wq_count - fnic->rq_count];
786         spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
787
788         fnic->fnic_stats.misc_stats.last_ack_time = jiffies;
789         if (is_ack_index_in_range(wq, request_out)) {
790                 fnic->fw_ack_index[0] = request_out;
791                 fnic->fw_ack_recd[0] = 1;
792         } else
793                 atomic64_inc(
794                         &fnic->fnic_stats.misc_stats.ack_index_out_of_range);
795
796         spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
797         FNIC_TRACE(fnic_fcpio_ack_handler,
798                   fnic->lport->host->host_no, 0, 0, ox_id_tag[2], ox_id_tag[3],
799                   ox_id_tag[4], ox_id_tag[5]);
800 }
801
802 /*
803  * fnic_fcpio_icmnd_cmpl_handler
804  * Routine to handle icmnd completions
805  */
806 static void fnic_fcpio_icmnd_cmpl_handler(struct fnic *fnic,
807                                          struct fcpio_fw_req *desc)
808 {
809         u8 type;
810         u8 hdr_status;
811         struct fcpio_tag tag;
812         u32 id;
813         u64 xfer_len = 0;
814         struct fcpio_icmnd_cmpl *icmnd_cmpl;
815         struct fnic_io_req *io_req;
816         struct scsi_cmnd *sc;
817         struct fnic_stats *fnic_stats = &fnic->fnic_stats;
818         unsigned long flags;
819         spinlock_t *io_lock;
820         u64 cmd_trace;
821         unsigned long start_time;
822         unsigned long io_duration_time;
823
824         /* Decode the cmpl description to get the io_req id */
825         fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
826         fcpio_tag_id_dec(&tag, &id);
827         icmnd_cmpl = &desc->u.icmnd_cmpl;
828
829         if (id >= fnic->fnic_max_tag_id) {
830                 shost_printk(KERN_ERR, fnic->lport->host,
831                         "Tag out of range tag %x hdr status = %s\n",
832                              id, fnic_fcpio_status_to_str(hdr_status));
833                 return;
834         }
835
836         sc = scsi_host_find_tag(fnic->lport->host, id);
837         WARN_ON_ONCE(!sc);
838         if (!sc) {
839                 atomic64_inc(&fnic_stats->io_stats.sc_null);
840                 shost_printk(KERN_ERR, fnic->lport->host,
841                           "icmnd_cmpl sc is null - "
842                           "hdr status = %s tag = 0x%x desc = 0x%p\n",
843                           fnic_fcpio_status_to_str(hdr_status), id, desc);
844                 FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
845                           fnic->lport->host->host_no, id,
846                           ((u64)icmnd_cmpl->_resvd0[1] << 16 |
847                           (u64)icmnd_cmpl->_resvd0[0]),
848                           ((u64)hdr_status << 16 |
849                           (u64)icmnd_cmpl->scsi_status << 8 |
850                           (u64)icmnd_cmpl->flags), desc,
851                           (u64)icmnd_cmpl->residual, 0);
852                 return;
853         }
854
855         io_lock = fnic_io_lock_hash(fnic, sc);
856         spin_lock_irqsave(io_lock, flags);
857         io_req = fnic_priv(sc)->io_req;
858         WARN_ON_ONCE(!io_req);
859         if (!io_req) {
860                 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
861                 fnic_priv(sc)->flags |= FNIC_IO_REQ_NULL;
862                 spin_unlock_irqrestore(io_lock, flags);
863                 shost_printk(KERN_ERR, fnic->lport->host,
864                           "icmnd_cmpl io_req is null - "
865                           "hdr status = %s tag = 0x%x sc 0x%p\n",
866                           fnic_fcpio_status_to_str(hdr_status), id, sc);
867                 return;
868         }
869         start_time = io_req->start_time;
870
871         /* firmware completed the io */
872         io_req->io_completed = 1;
873
874         /*
875          *  if SCSI-ML has already issued abort on this command,
876          *  set completion of the IO. The abts path will clean it up
877          */
878         if (fnic_priv(sc)->state == FNIC_IOREQ_ABTS_PENDING) {
879
880                 /*
881                  * set the FNIC_IO_DONE so that this doesn't get
882                  * flagged as 'out of order' if it was not aborted
883                  */
884                 fnic_priv(sc)->flags |= FNIC_IO_DONE;
885                 fnic_priv(sc)->flags |= FNIC_IO_ABTS_PENDING;
886                 spin_unlock_irqrestore(io_lock, flags);
887                 if(FCPIO_ABORTED == hdr_status)
888                         fnic_priv(sc)->flags |= FNIC_IO_ABORTED;
889
890                 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
891                         "icmnd_cmpl abts pending "
892                           "hdr status = %s tag = 0x%x sc = 0x%p "
893                           "scsi_status = %x residual = %d\n",
894                           fnic_fcpio_status_to_str(hdr_status),
895                           id, sc,
896                           icmnd_cmpl->scsi_status,
897                           icmnd_cmpl->residual);
898                 return;
899         }
900
901         /* Mark the IO as complete */
902         fnic_priv(sc)->state = FNIC_IOREQ_CMD_COMPLETE;
903
904         icmnd_cmpl = &desc->u.icmnd_cmpl;
905
906         switch (hdr_status) {
907         case FCPIO_SUCCESS:
908                 sc->result = (DID_OK << 16) | icmnd_cmpl->scsi_status;
909                 xfer_len = scsi_bufflen(sc);
910
911                 if (icmnd_cmpl->flags & FCPIO_ICMND_CMPL_RESID_UNDER) {
912                         xfer_len -= icmnd_cmpl->residual;
913                         scsi_set_resid(sc, icmnd_cmpl->residual);
914                 }
915
916                 if (icmnd_cmpl->scsi_status == SAM_STAT_CHECK_CONDITION)
917                         atomic64_inc(&fnic_stats->misc_stats.check_condition);
918
919                 if (icmnd_cmpl->scsi_status == SAM_STAT_TASK_SET_FULL)
920                         atomic64_inc(&fnic_stats->misc_stats.queue_fulls);
921                 break;
922
923         case FCPIO_TIMEOUT:          /* request was timed out */
924                 atomic64_inc(&fnic_stats->misc_stats.fcpio_timeout);
925                 sc->result = (DID_TIME_OUT << 16) | icmnd_cmpl->scsi_status;
926                 break;
927
928         case FCPIO_ABORTED:          /* request was aborted */
929                 atomic64_inc(&fnic_stats->misc_stats.fcpio_aborted);
930                 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
931                 break;
932
933         case FCPIO_DATA_CNT_MISMATCH: /* recv/sent more/less data than exp. */
934                 atomic64_inc(&fnic_stats->misc_stats.data_count_mismatch);
935                 scsi_set_resid(sc, icmnd_cmpl->residual);
936                 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
937                 break;
938
939         case FCPIO_OUT_OF_RESOURCE:  /* out of resources to complete request */
940                 atomic64_inc(&fnic_stats->fw_stats.fw_out_of_resources);
941                 sc->result = (DID_REQUEUE << 16) | icmnd_cmpl->scsi_status;
942                 break;
943
944         case FCPIO_IO_NOT_FOUND:     /* requested I/O was not found */
945                 atomic64_inc(&fnic_stats->io_stats.io_not_found);
946                 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
947                 break;
948
949         case FCPIO_SGL_INVALID:      /* request was aborted due to sgl error */
950                 atomic64_inc(&fnic_stats->misc_stats.sgl_invalid);
951                 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
952                 break;
953
954         case FCPIO_FW_ERR:           /* request was terminated due fw error */
955                 atomic64_inc(&fnic_stats->fw_stats.io_fw_errs);
956                 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
957                 break;
958
959         case FCPIO_MSS_INVALID:      /* request was aborted due to mss error */
960                 atomic64_inc(&fnic_stats->misc_stats.mss_invalid);
961                 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
962                 break;
963
964         case FCPIO_INVALID_HEADER:   /* header contains invalid data */
965         case FCPIO_INVALID_PARAM:    /* some parameter in request invalid */
966         case FCPIO_REQ_NOT_SUPPORTED:/* request type is not supported */
967         default:
968                 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
969                 break;
970         }
971
972         /* Break link with the SCSI command */
973         fnic_priv(sc)->io_req = NULL;
974         fnic_priv(sc)->flags |= FNIC_IO_DONE;
975
976         if (hdr_status != FCPIO_SUCCESS) {
977                 atomic64_inc(&fnic_stats->io_stats.io_failures);
978                 shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
979                              fnic_fcpio_status_to_str(hdr_status));
980         }
981
982         fnic_release_ioreq_buf(fnic, io_req, sc);
983
984         cmd_trace = ((u64)hdr_status << 56) |
985                   (u64)icmnd_cmpl->scsi_status << 48 |
986                   (u64)icmnd_cmpl->flags << 40 | (u64)sc->cmnd[0] << 32 |
987                   (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
988                   (u64)sc->cmnd[4] << 8 | sc->cmnd[5];
989
990         FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
991                   sc->device->host->host_no, id, sc,
992                   ((u64)icmnd_cmpl->_resvd0[1] << 56 |
993                   (u64)icmnd_cmpl->_resvd0[0] << 48 |
994                   jiffies_to_msecs(jiffies - start_time)),
995                   desc, cmd_trace, fnic_flags_and_state(sc));
996
997         if (sc->sc_data_direction == DMA_FROM_DEVICE) {
998                 fnic->lport->host_stats.fcp_input_requests++;
999                 fnic->fcp_input_bytes += xfer_len;
1000         } else if (sc->sc_data_direction == DMA_TO_DEVICE) {
1001                 fnic->lport->host_stats.fcp_output_requests++;
1002                 fnic->fcp_output_bytes += xfer_len;
1003         } else
1004                 fnic->lport->host_stats.fcp_control_requests++;
1005
1006         /* Call SCSI completion function to complete the IO */
1007         scsi_done(sc);
1008         spin_unlock_irqrestore(io_lock, flags);
1009
1010         mempool_free(io_req, fnic->io_req_pool);
1011
1012         atomic64_dec(&fnic_stats->io_stats.active_ios);
1013         if (atomic64_read(&fnic->io_cmpl_skip))
1014                 atomic64_dec(&fnic->io_cmpl_skip);
1015         else
1016                 atomic64_inc(&fnic_stats->io_stats.io_completions);
1017
1018
1019         io_duration_time = jiffies_to_msecs(jiffies) -
1020                                                 jiffies_to_msecs(start_time);
1021
1022         if(io_duration_time <= 10)
1023                 atomic64_inc(&fnic_stats->io_stats.io_btw_0_to_10_msec);
1024         else if(io_duration_time <= 100)
1025                 atomic64_inc(&fnic_stats->io_stats.io_btw_10_to_100_msec);
1026         else if(io_duration_time <= 500)
1027                 atomic64_inc(&fnic_stats->io_stats.io_btw_100_to_500_msec);
1028         else if(io_duration_time <= 5000)
1029                 atomic64_inc(&fnic_stats->io_stats.io_btw_500_to_5000_msec);
1030         else if(io_duration_time <= 10000)
1031                 atomic64_inc(&fnic_stats->io_stats.io_btw_5000_to_10000_msec);
1032         else if(io_duration_time <= 30000)
1033                 atomic64_inc(&fnic_stats->io_stats.io_btw_10000_to_30000_msec);
1034         else {
1035                 atomic64_inc(&fnic_stats->io_stats.io_greater_than_30000_msec);
1036
1037                 if(io_duration_time > atomic64_read(&fnic_stats->io_stats.current_max_io_time))
1038                         atomic64_set(&fnic_stats->io_stats.current_max_io_time, io_duration_time);
1039         }
1040 }
1041
1042 /* fnic_fcpio_itmf_cmpl_handler
1043  * Routine to handle itmf completions
1044  */
1045 static void fnic_fcpio_itmf_cmpl_handler(struct fnic *fnic,
1046                                         struct fcpio_fw_req *desc)
1047 {
1048         u8 type;
1049         u8 hdr_status;
1050         struct fcpio_tag tag;
1051         u32 id;
1052         struct scsi_cmnd *sc;
1053         struct fnic_io_req *io_req;
1054         struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1055         struct abort_stats *abts_stats = &fnic->fnic_stats.abts_stats;
1056         struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
1057         struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
1058         unsigned long flags;
1059         spinlock_t *io_lock;
1060         unsigned long start_time;
1061
1062         fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
1063         fcpio_tag_id_dec(&tag, &id);
1064
1065         if ((id & FNIC_TAG_MASK) >= fnic->fnic_max_tag_id) {
1066                 shost_printk(KERN_ERR, fnic->lport->host,
1067                 "Tag out of range tag %x hdr status = %s\n",
1068                 id, fnic_fcpio_status_to_str(hdr_status));
1069                 return;
1070         }
1071
1072         sc = scsi_host_find_tag(fnic->lport->host, id & FNIC_TAG_MASK);
1073         WARN_ON_ONCE(!sc);
1074         if (!sc) {
1075                 atomic64_inc(&fnic_stats->io_stats.sc_null);
1076                 shost_printk(KERN_ERR, fnic->lport->host,
1077                           "itmf_cmpl sc is null - hdr status = %s tag = 0x%x\n",
1078                           fnic_fcpio_status_to_str(hdr_status), id);
1079                 return;
1080         }
1081         io_lock = fnic_io_lock_hash(fnic, sc);
1082         spin_lock_irqsave(io_lock, flags);
1083         io_req = fnic_priv(sc)->io_req;
1084         WARN_ON_ONCE(!io_req);
1085         if (!io_req) {
1086                 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
1087                 spin_unlock_irqrestore(io_lock, flags);
1088                 fnic_priv(sc)->flags |= FNIC_IO_ABT_TERM_REQ_NULL;
1089                 shost_printk(KERN_ERR, fnic->lport->host,
1090                           "itmf_cmpl io_req is null - "
1091                           "hdr status = %s tag = 0x%x sc 0x%p\n",
1092                           fnic_fcpio_status_to_str(hdr_status), id, sc);
1093                 return;
1094         }
1095         start_time = io_req->start_time;
1096
1097         if ((id & FNIC_TAG_ABORT) && (id & FNIC_TAG_DEV_RST)) {
1098                 /* Abort and terminate completion of device reset req */
1099                 /* REVISIT : Add asserts about various flags */
1100                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1101                               "dev reset abts cmpl recd. id %x status %s\n",
1102                               id, fnic_fcpio_status_to_str(hdr_status));
1103                 fnic_priv(sc)->state = FNIC_IOREQ_ABTS_COMPLETE;
1104                 fnic_priv(sc)->abts_status = hdr_status;
1105                 fnic_priv(sc)->flags |= FNIC_DEV_RST_DONE;
1106                 if (io_req->abts_done)
1107                         complete(io_req->abts_done);
1108                 spin_unlock_irqrestore(io_lock, flags);
1109         } else if (id & FNIC_TAG_ABORT) {
1110                 /* Completion of abort cmd */
1111                 switch (hdr_status) {
1112                 case FCPIO_SUCCESS:
1113                         break;
1114                 case FCPIO_TIMEOUT:
1115                         if (fnic_priv(sc)->flags & FNIC_IO_ABTS_ISSUED)
1116                                 atomic64_inc(&abts_stats->abort_fw_timeouts);
1117                         else
1118                                 atomic64_inc(
1119                                         &term_stats->terminate_fw_timeouts);
1120                         break;
1121                 case FCPIO_ITMF_REJECTED:
1122                         FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1123                                 "abort reject recd. id %d\n",
1124                                 (int)(id & FNIC_TAG_MASK));
1125                         break;
1126                 case FCPIO_IO_NOT_FOUND:
1127                         if (fnic_priv(sc)->flags & FNIC_IO_ABTS_ISSUED)
1128                                 atomic64_inc(&abts_stats->abort_io_not_found);
1129                         else
1130                                 atomic64_inc(
1131                                         &term_stats->terminate_io_not_found);
1132                         break;
1133                 default:
1134                         if (fnic_priv(sc)->flags & FNIC_IO_ABTS_ISSUED)
1135                                 atomic64_inc(&abts_stats->abort_failures);
1136                         else
1137                                 atomic64_inc(
1138                                         &term_stats->terminate_failures);
1139                         break;
1140                 }
1141                 if (fnic_priv(sc)->state != FNIC_IOREQ_ABTS_PENDING) {
1142                         /* This is a late completion. Ignore it */
1143                         spin_unlock_irqrestore(io_lock, flags);
1144                         return;
1145                 }
1146
1147                 fnic_priv(sc)->flags |= FNIC_IO_ABT_TERM_DONE;
1148                 fnic_priv(sc)->abts_status = hdr_status;
1149
1150                 /* If the status is IO not found consider it as success */
1151                 if (hdr_status == FCPIO_IO_NOT_FOUND)
1152                         fnic_priv(sc)->abts_status = FCPIO_SUCCESS;
1153
1154                 if (!(fnic_priv(sc)->flags & (FNIC_IO_ABORTED | FNIC_IO_DONE)))
1155                         atomic64_inc(&misc_stats->no_icmnd_itmf_cmpls);
1156
1157                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1158                               "abts cmpl recd. id %d status %s\n",
1159                               (int)(id & FNIC_TAG_MASK),
1160                               fnic_fcpio_status_to_str(hdr_status));
1161
1162                 /*
1163                  * If scsi_eh thread is blocked waiting for abts to complete,
1164                  * signal completion to it. IO will be cleaned in the thread
1165                  * else clean it in this context
1166                  */
1167                 if (io_req->abts_done) {
1168                         complete(io_req->abts_done);
1169                         spin_unlock_irqrestore(io_lock, flags);
1170                 } else {
1171                         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1172                                       "abts cmpl, completing IO\n");
1173                         fnic_priv(sc)->io_req = NULL;
1174                         sc->result = (DID_ERROR << 16);
1175
1176                         spin_unlock_irqrestore(io_lock, flags);
1177
1178                         fnic_release_ioreq_buf(fnic, io_req, sc);
1179                         mempool_free(io_req, fnic->io_req_pool);
1180                         FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1181                                    sc->device->host->host_no, id,
1182                                    sc,
1183                                    jiffies_to_msecs(jiffies - start_time),
1184                                    desc,
1185                                    (((u64)hdr_status << 40) |
1186                                     (u64)sc->cmnd[0] << 32 |
1187                                     (u64)sc->cmnd[2] << 24 |
1188                                     (u64)sc->cmnd[3] << 16 |
1189                                     (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1190                                    fnic_flags_and_state(sc));
1191                         scsi_done(sc);
1192                         atomic64_dec(&fnic_stats->io_stats.active_ios);
1193                         if (atomic64_read(&fnic->io_cmpl_skip))
1194                                 atomic64_dec(&fnic->io_cmpl_skip);
1195                         else
1196                                 atomic64_inc(&fnic_stats->io_stats.io_completions);
1197                 }
1198         } else if (id & FNIC_TAG_DEV_RST) {
1199                 /* Completion of device reset */
1200                 fnic_priv(sc)->lr_status = hdr_status;
1201                 if (fnic_priv(sc)->state == FNIC_IOREQ_ABTS_PENDING) {
1202                         spin_unlock_irqrestore(io_lock, flags);
1203                         fnic_priv(sc)->flags |= FNIC_DEV_RST_ABTS_PENDING;
1204                         FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1205                                   sc->device->host->host_no, id, sc,
1206                                   jiffies_to_msecs(jiffies - start_time),
1207                                   desc, 0, fnic_flags_and_state(sc));
1208                         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1209                                 "Terminate pending "
1210                                 "dev reset cmpl recd. id %d status %s\n",
1211                                 (int)(id & FNIC_TAG_MASK),
1212                                 fnic_fcpio_status_to_str(hdr_status));
1213                         return;
1214                 }
1215                 if (fnic_priv(sc)->flags & FNIC_DEV_RST_TIMED_OUT) {
1216                         /* Need to wait for terminate completion */
1217                         spin_unlock_irqrestore(io_lock, flags);
1218                         FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1219                                   sc->device->host->host_no, id, sc,
1220                                   jiffies_to_msecs(jiffies - start_time),
1221                                   desc, 0, fnic_flags_and_state(sc));
1222                         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1223                                 "dev reset cmpl recd after time out. "
1224                                 "id %d status %s\n",
1225                                 (int)(id & FNIC_TAG_MASK),
1226                                 fnic_fcpio_status_to_str(hdr_status));
1227                         return;
1228                 }
1229                 fnic_priv(sc)->state = FNIC_IOREQ_CMD_COMPLETE;
1230                 fnic_priv(sc)->flags |= FNIC_DEV_RST_DONE;
1231                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1232                               "dev reset cmpl recd. id %d status %s\n",
1233                               (int)(id & FNIC_TAG_MASK),
1234                               fnic_fcpio_status_to_str(hdr_status));
1235                 if (io_req->dr_done)
1236                         complete(io_req->dr_done);
1237                 spin_unlock_irqrestore(io_lock, flags);
1238
1239         } else {
1240                 shost_printk(KERN_ERR, fnic->lport->host,
1241                              "Unexpected itmf io state %s tag %x\n",
1242                              fnic_ioreq_state_to_str(fnic_priv(sc)->state), id);
1243                 spin_unlock_irqrestore(io_lock, flags);
1244         }
1245
1246 }
1247
1248 /*
1249  * fnic_fcpio_cmpl_handler
1250  * Routine to service the cq for wq_copy
1251  */
1252 static int fnic_fcpio_cmpl_handler(struct vnic_dev *vdev,
1253                                    unsigned int cq_index,
1254                                    struct fcpio_fw_req *desc)
1255 {
1256         struct fnic *fnic = vnic_dev_priv(vdev);
1257
1258         switch (desc->hdr.type) {
1259         case FCPIO_ICMND_CMPL: /* fw completed a command */
1260         case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1261         case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
1262         case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
1263         case FCPIO_RESET_CMPL: /* fw completed reset */
1264                 atomic64_dec(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1265                 break;
1266         default:
1267                 break;
1268         }
1269
1270         switch (desc->hdr.type) {
1271         case FCPIO_ACK: /* fw copied copy wq desc to its queue */
1272                 fnic_fcpio_ack_handler(fnic, cq_index, desc);
1273                 break;
1274
1275         case FCPIO_ICMND_CMPL: /* fw completed a command */
1276                 fnic_fcpio_icmnd_cmpl_handler(fnic, desc);
1277                 break;
1278
1279         case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1280                 fnic_fcpio_itmf_cmpl_handler(fnic, desc);
1281                 break;
1282
1283         case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
1284         case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
1285                 fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
1286                 break;
1287
1288         case FCPIO_RESET_CMPL: /* fw completed reset */
1289                 fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
1290                 break;
1291
1292         default:
1293                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1294                               "firmware completion type %d\n",
1295                               desc->hdr.type);
1296                 break;
1297         }
1298
1299         return 0;
1300 }
1301
1302 /*
1303  * fnic_wq_copy_cmpl_handler
1304  * Routine to process wq copy
1305  */
1306 int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
1307 {
1308         unsigned int wq_work_done = 0;
1309         unsigned int i, cq_index;
1310         unsigned int cur_work_done;
1311         struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
1312         u64 start_jiffies = 0;
1313         u64 end_jiffies = 0;
1314         u64 delta_jiffies = 0;
1315         u64 delta_ms = 0;
1316
1317         for (i = 0; i < fnic->wq_copy_count; i++) {
1318                 cq_index = i + fnic->raw_wq_count + fnic->rq_count;
1319
1320                 start_jiffies = jiffies;
1321                 cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
1322                                                      fnic_fcpio_cmpl_handler,
1323                                                      copy_work_to_do);
1324                 end_jiffies = jiffies;
1325
1326                 wq_work_done += cur_work_done;
1327                 delta_jiffies = end_jiffies - start_jiffies;
1328                 if (delta_jiffies >
1329                         (u64) atomic64_read(&misc_stats->max_isr_jiffies)) {
1330                         atomic64_set(&misc_stats->max_isr_jiffies,
1331                                         delta_jiffies);
1332                         delta_ms = jiffies_to_msecs(delta_jiffies);
1333                         atomic64_set(&misc_stats->max_isr_time_ms, delta_ms);
1334                         atomic64_set(&misc_stats->corr_work_done,
1335                                         cur_work_done);
1336                 }
1337         }
1338         return wq_work_done;
1339 }
1340
1341 static bool fnic_cleanup_io_iter(struct scsi_cmnd *sc, void *data,
1342                                  bool reserved)
1343 {
1344         const int tag = scsi_cmd_to_rq(sc)->tag;
1345         struct fnic *fnic = data;
1346         struct fnic_io_req *io_req;
1347         unsigned long flags = 0;
1348         spinlock_t *io_lock;
1349         unsigned long start_time = 0;
1350         struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1351
1352         io_lock = fnic_io_lock_tag(fnic, tag);
1353         spin_lock_irqsave(io_lock, flags);
1354
1355         io_req = fnic_priv(sc)->io_req;
1356         if ((fnic_priv(sc)->flags & FNIC_DEVICE_RESET) &&
1357             !(fnic_priv(sc)->flags & FNIC_DEV_RST_DONE)) {
1358                 /*
1359                  * We will be here only when FW completes reset
1360                  * without sending completions for outstanding ios.
1361                  */
1362                 fnic_priv(sc)->flags |= FNIC_DEV_RST_DONE;
1363                 if (io_req && io_req->dr_done)
1364                         complete(io_req->dr_done);
1365                 else if (io_req && io_req->abts_done)
1366                         complete(io_req->abts_done);
1367                 spin_unlock_irqrestore(io_lock, flags);
1368                 return true;
1369         } else if (fnic_priv(sc)->flags & FNIC_DEVICE_RESET) {
1370                 spin_unlock_irqrestore(io_lock, flags);
1371                 return true;
1372         }
1373         if (!io_req) {
1374                 spin_unlock_irqrestore(io_lock, flags);
1375                 goto cleanup_scsi_cmd;
1376         }
1377
1378         fnic_priv(sc)->io_req = NULL;
1379
1380         spin_unlock_irqrestore(io_lock, flags);
1381
1382         /*
1383          * If there is a scsi_cmnd associated with this io_req, then
1384          * free the corresponding state
1385          */
1386         start_time = io_req->start_time;
1387         fnic_release_ioreq_buf(fnic, io_req, sc);
1388         mempool_free(io_req, fnic->io_req_pool);
1389
1390 cleanup_scsi_cmd:
1391         sc->result = DID_TRANSPORT_DISRUPTED << 16;
1392         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1393                       "fnic_cleanup_io: tag:0x%x : sc:0x%p duration = %lu DID_TRANSPORT_DISRUPTED\n",
1394                       tag, sc, jiffies - start_time);
1395
1396         if (atomic64_read(&fnic->io_cmpl_skip))
1397                 atomic64_dec(&fnic->io_cmpl_skip);
1398         else
1399                 atomic64_inc(&fnic_stats->io_stats.io_completions);
1400
1401         /* Complete the command to SCSI */
1402         if (!(fnic_priv(sc)->flags & FNIC_IO_ISSUED))
1403                 shost_printk(KERN_ERR, fnic->lport->host,
1404                              "Calling done for IO not issued to fw: tag:0x%x sc:0x%p\n",
1405                              tag, sc);
1406
1407         FNIC_TRACE(fnic_cleanup_io,
1408                    sc->device->host->host_no, tag, sc,
1409                    jiffies_to_msecs(jiffies - start_time),
1410                    0, ((u64)sc->cmnd[0] << 32 |
1411                        (u64)sc->cmnd[2] << 24 |
1412                        (u64)sc->cmnd[3] << 16 |
1413                        (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1414                    fnic_flags_and_state(sc));
1415
1416         scsi_done(sc);
1417
1418         return true;
1419 }
1420
1421 static void fnic_cleanup_io(struct fnic *fnic)
1422 {
1423         scsi_host_busy_iter(fnic->lport->host,
1424                             fnic_cleanup_io_iter, fnic);
1425 }
1426
1427 void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
1428                                   struct fcpio_host_req *desc)
1429 {
1430         u32 id;
1431         struct fnic *fnic = vnic_dev_priv(wq->vdev);
1432         struct fnic_io_req *io_req;
1433         struct scsi_cmnd *sc;
1434         unsigned long flags;
1435         spinlock_t *io_lock;
1436         unsigned long start_time = 0;
1437
1438         /* get the tag reference */
1439         fcpio_tag_id_dec(&desc->hdr.tag, &id);
1440         id &= FNIC_TAG_MASK;
1441
1442         if (id >= fnic->fnic_max_tag_id)
1443                 return;
1444
1445         sc = scsi_host_find_tag(fnic->lport->host, id);
1446         if (!sc)
1447                 return;
1448
1449         io_lock = fnic_io_lock_hash(fnic, sc);
1450         spin_lock_irqsave(io_lock, flags);
1451
1452         /* Get the IO context which this desc refers to */
1453         io_req = fnic_priv(sc)->io_req;
1454
1455         /* fnic interrupts are turned off by now */
1456
1457         if (!io_req) {
1458                 spin_unlock_irqrestore(io_lock, flags);
1459                 goto wq_copy_cleanup_scsi_cmd;
1460         }
1461
1462         fnic_priv(sc)->io_req = NULL;
1463
1464         spin_unlock_irqrestore(io_lock, flags);
1465
1466         start_time = io_req->start_time;
1467         fnic_release_ioreq_buf(fnic, io_req, sc);
1468         mempool_free(io_req, fnic->io_req_pool);
1469
1470 wq_copy_cleanup_scsi_cmd:
1471         sc->result = DID_NO_CONNECT << 16;
1472         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
1473                       " DID_NO_CONNECT\n");
1474
1475         FNIC_TRACE(fnic_wq_copy_cleanup_handler,
1476                    sc->device->host->host_no, id, sc,
1477                    jiffies_to_msecs(jiffies - start_time),
1478                    0, ((u64)sc->cmnd[0] << 32 |
1479                        (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1480                        (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1481                    fnic_flags_and_state(sc));
1482
1483         scsi_done(sc);
1484 }
1485
1486 static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
1487                                           u32 task_req, u8 *fc_lun,
1488                                           struct fnic_io_req *io_req)
1489 {
1490         struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1491         struct Scsi_Host *host = fnic->lport->host;
1492         struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
1493         unsigned long flags;
1494
1495         spin_lock_irqsave(host->host_lock, flags);
1496         if (unlikely(fnic_chk_state_flags_locked(fnic,
1497                                                 FNIC_FLAGS_IO_BLOCKED))) {
1498                 spin_unlock_irqrestore(host->host_lock, flags);
1499                 return 1;
1500         } else
1501                 atomic_inc(&fnic->in_flight);
1502         spin_unlock_irqrestore(host->host_lock, flags);
1503
1504         spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
1505
1506         if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1507                 free_wq_copy_descs(fnic, wq);
1508
1509         if (!vnic_wq_copy_desc_avail(wq)) {
1510                 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1511                 atomic_dec(&fnic->in_flight);
1512                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1513                         "fnic_queue_abort_io_req: failure: no descriptors\n");
1514                 atomic64_inc(&misc_stats->abts_cpwq_alloc_failures);
1515                 return 1;
1516         }
1517         fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
1518                                      0, task_req, tag, fc_lun, io_req->port_id,
1519                                      fnic->config.ra_tov, fnic->config.ed_tov);
1520
1521         atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1522         if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
1523                   atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
1524                 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
1525                   atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
1526
1527         spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1528         atomic_dec(&fnic->in_flight);
1529
1530         return 0;
1531 }
1532
1533 struct fnic_rport_abort_io_iter_data {
1534         struct fnic *fnic;
1535         u32 port_id;
1536         int term_cnt;
1537 };
1538
1539 static bool fnic_rport_abort_io_iter(struct scsi_cmnd *sc, void *data,
1540                                      bool reserved)
1541 {
1542         struct fnic_rport_abort_io_iter_data *iter_data = data;
1543         struct fnic *fnic = iter_data->fnic;
1544         int abt_tag = scsi_cmd_to_rq(sc)->tag;
1545         struct fnic_io_req *io_req;
1546         spinlock_t *io_lock;
1547         unsigned long flags;
1548         struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
1549         struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
1550         struct scsi_lun fc_lun;
1551         enum fnic_ioreq_state old_ioreq_state;
1552
1553         io_lock = fnic_io_lock_tag(fnic, abt_tag);
1554         spin_lock_irqsave(io_lock, flags);
1555
1556         io_req = fnic_priv(sc)->io_req;
1557
1558         if (!io_req || io_req->port_id != iter_data->port_id) {
1559                 spin_unlock_irqrestore(io_lock, flags);
1560                 return true;
1561         }
1562
1563         if ((fnic_priv(sc)->flags & FNIC_DEVICE_RESET) &&
1564             !(fnic_priv(sc)->flags & FNIC_DEV_RST_ISSUED)) {
1565                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1566                         "fnic_rport_exch_reset dev rst not pending sc 0x%p\n",
1567                         sc);
1568                 spin_unlock_irqrestore(io_lock, flags);
1569                 return true;
1570         }
1571
1572         /*
1573          * Found IO that is still pending with firmware and
1574          * belongs to rport that went away
1575          */
1576         if (fnic_priv(sc)->state == FNIC_IOREQ_ABTS_PENDING) {
1577                 spin_unlock_irqrestore(io_lock, flags);
1578                 return true;
1579         }
1580         if (io_req->abts_done) {
1581                 shost_printk(KERN_ERR, fnic->lport->host,
1582                         "fnic_rport_exch_reset: io_req->abts_done is set "
1583                         "state is %s\n",
1584                         fnic_ioreq_state_to_str(fnic_priv(sc)->state));
1585         }
1586
1587         if (!(fnic_priv(sc)->flags & FNIC_IO_ISSUED)) {
1588                 shost_printk(KERN_ERR, fnic->lport->host,
1589                              "rport_exch_reset "
1590                              "IO not yet issued %p tag 0x%x flags "
1591                              "%x state %d\n",
1592                              sc, abt_tag, fnic_priv(sc)->flags, fnic_priv(sc)->state);
1593         }
1594         old_ioreq_state = fnic_priv(sc)->state;
1595         fnic_priv(sc)->state = FNIC_IOREQ_ABTS_PENDING;
1596         fnic_priv(sc)->abts_status = FCPIO_INVALID_CODE;
1597         if (fnic_priv(sc)->flags & FNIC_DEVICE_RESET) {
1598                 atomic64_inc(&reset_stats->device_reset_terminates);
1599                 abt_tag |= FNIC_TAG_DEV_RST;
1600         }
1601         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1602                       "fnic_rport_exch_reset dev rst sc 0x%p\n", sc);
1603         BUG_ON(io_req->abts_done);
1604
1605         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1606                       "fnic_rport_reset_exch: Issuing abts\n");
1607
1608         spin_unlock_irqrestore(io_lock, flags);
1609
1610         /* Now queue the abort command to firmware */
1611         int_to_scsilun(sc->device->lun, &fc_lun);
1612
1613         if (fnic_queue_abort_io_req(fnic, abt_tag,
1614                                     FCPIO_ITMF_ABT_TASK_TERM,
1615                                     fc_lun.scsi_lun, io_req)) {
1616                 /*
1617                  * Revert the cmd state back to old state, if
1618                  * it hasn't changed in between. This cmd will get
1619                  * aborted later by scsi_eh, or cleaned up during
1620                  * lun reset
1621                  */
1622                 spin_lock_irqsave(io_lock, flags);
1623                 if (fnic_priv(sc)->state == FNIC_IOREQ_ABTS_PENDING)
1624                         fnic_priv(sc)->state = old_ioreq_state;
1625                 spin_unlock_irqrestore(io_lock, flags);
1626         } else {
1627                 spin_lock_irqsave(io_lock, flags);
1628                 if (fnic_priv(sc)->flags & FNIC_DEVICE_RESET)
1629                         fnic_priv(sc)->flags |= FNIC_DEV_RST_TERM_ISSUED;
1630                 else
1631                         fnic_priv(sc)->flags |= FNIC_IO_INTERNAL_TERM_ISSUED;
1632                 spin_unlock_irqrestore(io_lock, flags);
1633                 atomic64_inc(&term_stats->terminates);
1634                 iter_data->term_cnt++;
1635         }
1636         return true;
1637 }
1638
1639 static void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
1640 {
1641         struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
1642         struct fnic_rport_abort_io_iter_data iter_data = {
1643                 .fnic = fnic,
1644                 .port_id = port_id,
1645                 .term_cnt = 0,
1646         };
1647
1648         FNIC_SCSI_DBG(KERN_DEBUG,
1649                       fnic->lport->host,
1650                       "fnic_rport_exch_reset called portid 0x%06x\n",
1651                       port_id);
1652
1653         if (fnic->in_remove)
1654                 return;
1655
1656         scsi_host_busy_iter(fnic->lport->host, fnic_rport_abort_io_iter,
1657                             &iter_data);
1658         if (iter_data.term_cnt > atomic64_read(&term_stats->max_terminates))
1659                 atomic64_set(&term_stats->max_terminates, iter_data.term_cnt);
1660
1661 }
1662
1663 void fnic_terminate_rport_io(struct fc_rport *rport)
1664 {
1665         struct fc_rport_libfc_priv *rdata;
1666         struct fc_lport *lport;
1667         struct fnic *fnic;
1668
1669         if (!rport) {
1670                 printk(KERN_ERR "fnic_terminate_rport_io: rport is NULL\n");
1671                 return;
1672         }
1673         rdata = rport->dd_data;
1674
1675         if (!rdata) {
1676                 printk(KERN_ERR "fnic_terminate_rport_io: rdata is NULL\n");
1677                 return;
1678         }
1679         lport = rdata->local_port;
1680
1681         if (!lport) {
1682                 printk(KERN_ERR "fnic_terminate_rport_io: lport is NULL\n");
1683                 return;
1684         }
1685         fnic = lport_priv(lport);
1686         FNIC_SCSI_DBG(KERN_DEBUG,
1687                       fnic->lport->host, "fnic_terminate_rport_io called"
1688                       " wwpn 0x%llx, wwnn0x%llx, rport 0x%p, portid 0x%06x\n",
1689                       rport->port_name, rport->node_name, rport,
1690                       rport->port_id);
1691
1692         if (fnic->in_remove)
1693                 return;
1694
1695         fnic_rport_exch_reset(fnic, rport->port_id);
1696 }
1697
1698 /*
1699  * This function is exported to SCSI for sending abort cmnds.
1700  * A SCSI IO is represented by a io_req in the driver.
1701  * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
1702  */
1703 int fnic_abort_cmd(struct scsi_cmnd *sc)
1704 {
1705         struct request *const rq = scsi_cmd_to_rq(sc);
1706         struct fc_lport *lp;
1707         struct fnic *fnic;
1708         struct fnic_io_req *io_req = NULL;
1709         struct fc_rport *rport;
1710         spinlock_t *io_lock;
1711         unsigned long flags;
1712         unsigned long start_time = 0;
1713         int ret = SUCCESS;
1714         u32 task_req = 0;
1715         struct scsi_lun fc_lun;
1716         struct fnic_stats *fnic_stats;
1717         struct abort_stats *abts_stats;
1718         struct terminate_stats *term_stats;
1719         enum fnic_ioreq_state old_ioreq_state;
1720         const int tag = rq->tag;
1721         unsigned long abt_issued_time;
1722         DECLARE_COMPLETION_ONSTACK(tm_done);
1723
1724         /* Wait for rport to unblock */
1725         fc_block_scsi_eh(sc);
1726
1727         /* Get local-port, check ready and link up */
1728         lp = shost_priv(sc->device->host);
1729
1730         fnic = lport_priv(lp);
1731         fnic_stats = &fnic->fnic_stats;
1732         abts_stats = &fnic->fnic_stats.abts_stats;
1733         term_stats = &fnic->fnic_stats.term_stats;
1734
1735         rport = starget_to_rport(scsi_target(sc->device));
1736         FNIC_SCSI_DBG(KERN_DEBUG,
1737                 fnic->lport->host,
1738                 "Abort Cmd called FCID 0x%x, LUN 0x%llx TAG %x flags %x\n",
1739                 rport->port_id, sc->device->lun, tag, fnic_priv(sc)->flags);
1740
1741         fnic_priv(sc)->flags = FNIC_NO_FLAGS;
1742
1743         if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
1744                 ret = FAILED;
1745                 goto fnic_abort_cmd_end;
1746         }
1747
1748         /*
1749          * Avoid a race between SCSI issuing the abort and the device
1750          * completing the command.
1751          *
1752          * If the command is already completed by the fw cmpl code,
1753          * we just return SUCCESS from here. This means that the abort
1754          * succeeded. In the SCSI ML, since the timeout for command has
1755          * happened, the completion wont actually complete the command
1756          * and it will be considered as an aborted command
1757          *
1758          * .io_req will not be cleared except while holding io_req_lock.
1759          */
1760         io_lock = fnic_io_lock_hash(fnic, sc);
1761         spin_lock_irqsave(io_lock, flags);
1762         io_req = fnic_priv(sc)->io_req;
1763         if (!io_req) {
1764                 spin_unlock_irqrestore(io_lock, flags);
1765                 goto fnic_abort_cmd_end;
1766         }
1767
1768         io_req->abts_done = &tm_done;
1769
1770         if (fnic_priv(sc)->state == FNIC_IOREQ_ABTS_PENDING) {
1771                 spin_unlock_irqrestore(io_lock, flags);
1772                 goto wait_pending;
1773         }
1774
1775         abt_issued_time = jiffies_to_msecs(jiffies) - jiffies_to_msecs(io_req->start_time);
1776         if (abt_issued_time <= 6000)
1777                 atomic64_inc(&abts_stats->abort_issued_btw_0_to_6_sec);
1778         else if (abt_issued_time > 6000 && abt_issued_time <= 20000)
1779                 atomic64_inc(&abts_stats->abort_issued_btw_6_to_20_sec);
1780         else if (abt_issued_time > 20000 && abt_issued_time <= 30000)
1781                 atomic64_inc(&abts_stats->abort_issued_btw_20_to_30_sec);
1782         else if (abt_issued_time > 30000 && abt_issued_time <= 40000)
1783                 atomic64_inc(&abts_stats->abort_issued_btw_30_to_40_sec);
1784         else if (abt_issued_time > 40000 && abt_issued_time <= 50000)
1785                 atomic64_inc(&abts_stats->abort_issued_btw_40_to_50_sec);
1786         else if (abt_issued_time > 50000 && abt_issued_time <= 60000)
1787                 atomic64_inc(&abts_stats->abort_issued_btw_50_to_60_sec);
1788         else
1789                 atomic64_inc(&abts_stats->abort_issued_greater_than_60_sec);
1790
1791         FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1792                 "CBD Opcode: %02x Abort issued time: %lu msec\n", sc->cmnd[0], abt_issued_time);
1793         /*
1794          * Command is still pending, need to abort it
1795          * If the firmware completes the command after this point,
1796          * the completion wont be done till mid-layer, since abort
1797          * has already started.
1798          */
1799         old_ioreq_state = fnic_priv(sc)->state;
1800         fnic_priv(sc)->state = FNIC_IOREQ_ABTS_PENDING;
1801         fnic_priv(sc)->abts_status = FCPIO_INVALID_CODE;
1802
1803         spin_unlock_irqrestore(io_lock, flags);
1804
1805         /*
1806          * Check readiness of the remote port. If the path to remote
1807          * port is up, then send abts to the remote port to terminate
1808          * the IO. Else, just locally terminate the IO in the firmware
1809          */
1810         if (fc_remote_port_chkready(rport) == 0)
1811                 task_req = FCPIO_ITMF_ABT_TASK;
1812         else {
1813                 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
1814                 task_req = FCPIO_ITMF_ABT_TASK_TERM;
1815         }
1816
1817         /* Now queue the abort command to firmware */
1818         int_to_scsilun(sc->device->lun, &fc_lun);
1819
1820         if (fnic_queue_abort_io_req(fnic, tag, task_req, fc_lun.scsi_lun,
1821                                     io_req)) {
1822                 spin_lock_irqsave(io_lock, flags);
1823                 if (fnic_priv(sc)->state == FNIC_IOREQ_ABTS_PENDING)
1824                         fnic_priv(sc)->state = old_ioreq_state;
1825                 io_req = fnic_priv(sc)->io_req;
1826                 if (io_req)
1827                         io_req->abts_done = NULL;
1828                 spin_unlock_irqrestore(io_lock, flags);
1829                 ret = FAILED;
1830                 goto fnic_abort_cmd_end;
1831         }
1832         if (task_req == FCPIO_ITMF_ABT_TASK) {
1833                 fnic_priv(sc)->flags |= FNIC_IO_ABTS_ISSUED;
1834                 atomic64_inc(&fnic_stats->abts_stats.aborts);
1835         } else {
1836                 fnic_priv(sc)->flags |= FNIC_IO_TERM_ISSUED;
1837                 atomic64_inc(&fnic_stats->term_stats.terminates);
1838         }
1839
1840         /*
1841          * We queued an abort IO, wait for its completion.
1842          * Once the firmware completes the abort command, it will
1843          * wake up this thread.
1844          */
1845  wait_pending:
1846         wait_for_completion_timeout(&tm_done,
1847                                     msecs_to_jiffies
1848                                     (2 * fnic->config.ra_tov +
1849                                      fnic->config.ed_tov));
1850
1851         /* Check the abort status */
1852         spin_lock_irqsave(io_lock, flags);
1853
1854         io_req = fnic_priv(sc)->io_req;
1855         if (!io_req) {
1856                 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
1857                 spin_unlock_irqrestore(io_lock, flags);
1858                 fnic_priv(sc)->flags |= FNIC_IO_ABT_TERM_REQ_NULL;
1859                 ret = FAILED;
1860                 goto fnic_abort_cmd_end;
1861         }
1862         io_req->abts_done = NULL;
1863
1864         /* fw did not complete abort, timed out */
1865         if (fnic_priv(sc)->abts_status == FCPIO_INVALID_CODE) {
1866                 spin_unlock_irqrestore(io_lock, flags);
1867                 if (task_req == FCPIO_ITMF_ABT_TASK) {
1868                         atomic64_inc(&abts_stats->abort_drv_timeouts);
1869                 } else {
1870                         atomic64_inc(&term_stats->terminate_drv_timeouts);
1871                 }
1872                 fnic_priv(sc)->flags |= FNIC_IO_ABT_TERM_TIMED_OUT;
1873                 ret = FAILED;
1874                 goto fnic_abort_cmd_end;
1875         }
1876
1877         /* IO out of order */
1878
1879         if (!(fnic_priv(sc)->flags & (FNIC_IO_ABORTED | FNIC_IO_DONE))) {
1880                 spin_unlock_irqrestore(io_lock, flags);
1881                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1882                         "Issuing Host reset due to out of order IO\n");
1883
1884                 ret = FAILED;
1885                 goto fnic_abort_cmd_end;
1886         }
1887
1888         fnic_priv(sc)->state = FNIC_IOREQ_ABTS_COMPLETE;
1889
1890         start_time = io_req->start_time;
1891         /*
1892          * firmware completed the abort, check the status,
1893          * free the io_req if successful. If abort fails,
1894          * Device reset will clean the I/O.
1895          */
1896         if (fnic_priv(sc)->abts_status == FCPIO_SUCCESS) {
1897                 fnic_priv(sc)->io_req = NULL;
1898         } else {
1899                 ret = FAILED;
1900                 spin_unlock_irqrestore(io_lock, flags);
1901                 goto fnic_abort_cmd_end;
1902         }
1903
1904         spin_unlock_irqrestore(io_lock, flags);
1905
1906         fnic_release_ioreq_buf(fnic, io_req, sc);
1907         mempool_free(io_req, fnic->io_req_pool);
1908
1909         /* Call SCSI completion function to complete the IO */
1910         sc->result = DID_ABORT << 16;
1911         scsi_done(sc);
1912         atomic64_dec(&fnic_stats->io_stats.active_ios);
1913         if (atomic64_read(&fnic->io_cmpl_skip))
1914                 atomic64_dec(&fnic->io_cmpl_skip);
1915         else
1916                 atomic64_inc(&fnic_stats->io_stats.io_completions);
1917
1918 fnic_abort_cmd_end:
1919         FNIC_TRACE(fnic_abort_cmd, sc->device->host->host_no, tag, sc,
1920                   jiffies_to_msecs(jiffies - start_time),
1921                   0, ((u64)sc->cmnd[0] << 32 |
1922                   (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1923                   (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1924                   fnic_flags_and_state(sc));
1925
1926         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1927                       "Returning from abort cmd type %x %s\n", task_req,
1928                       (ret == SUCCESS) ?
1929                       "SUCCESS" : "FAILED");
1930         return ret;
1931 }
1932
1933 static inline int fnic_queue_dr_io_req(struct fnic *fnic,
1934                                        struct scsi_cmnd *sc,
1935                                        struct fnic_io_req *io_req)
1936 {
1937         struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1938         struct Scsi_Host *host = fnic->lport->host;
1939         struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
1940         struct scsi_lun fc_lun;
1941         int ret = 0;
1942         unsigned long intr_flags;
1943
1944         spin_lock_irqsave(host->host_lock, intr_flags);
1945         if (unlikely(fnic_chk_state_flags_locked(fnic,
1946                                                 FNIC_FLAGS_IO_BLOCKED))) {
1947                 spin_unlock_irqrestore(host->host_lock, intr_flags);
1948                 return FAILED;
1949         } else
1950                 atomic_inc(&fnic->in_flight);
1951         spin_unlock_irqrestore(host->host_lock, intr_flags);
1952
1953         spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
1954
1955         if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1956                 free_wq_copy_descs(fnic, wq);
1957
1958         if (!vnic_wq_copy_desc_avail(wq)) {
1959                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1960                           "queue_dr_io_req failure - no descriptors\n");
1961                 atomic64_inc(&misc_stats->devrst_cpwq_alloc_failures);
1962                 ret = -EAGAIN;
1963                 goto lr_io_req_end;
1964         }
1965
1966         /* fill in the lun info */
1967         int_to_scsilun(sc->device->lun, &fc_lun);
1968
1969         fnic_queue_wq_copy_desc_itmf(wq, scsi_cmd_to_rq(sc)->tag | FNIC_TAG_DEV_RST,
1970                                      0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
1971                                      fc_lun.scsi_lun, io_req->port_id,
1972                                      fnic->config.ra_tov, fnic->config.ed_tov);
1973
1974         atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1975         if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
1976                   atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
1977                 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
1978                   atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
1979
1980 lr_io_req_end:
1981         spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
1982         atomic_dec(&fnic->in_flight);
1983
1984         return ret;
1985 }
1986
1987 struct fnic_pending_aborts_iter_data {
1988         struct fnic *fnic;
1989         struct scsi_cmnd *lr_sc;
1990         struct scsi_device *lun_dev;
1991         int ret;
1992 };
1993
1994 static bool fnic_pending_aborts_iter(struct scsi_cmnd *sc,
1995                                      void *data, bool reserved)
1996 {
1997         struct fnic_pending_aborts_iter_data *iter_data = data;
1998         struct fnic *fnic = iter_data->fnic;
1999         struct scsi_device *lun_dev = iter_data->lun_dev;
2000         int abt_tag = scsi_cmd_to_rq(sc)->tag;
2001         struct fnic_io_req *io_req;
2002         spinlock_t *io_lock;
2003         unsigned long flags;
2004         struct scsi_lun fc_lun;
2005         DECLARE_COMPLETION_ONSTACK(tm_done);
2006         enum fnic_ioreq_state old_ioreq_state;
2007
2008         if (sc == iter_data->lr_sc || sc->device != lun_dev)
2009                 return true;
2010         if (reserved)
2011                 return true;
2012
2013         io_lock = fnic_io_lock_tag(fnic, abt_tag);
2014         spin_lock_irqsave(io_lock, flags);
2015         io_req = fnic_priv(sc)->io_req;
2016         if (!io_req) {
2017                 spin_unlock_irqrestore(io_lock, flags);
2018                 return true;
2019         }
2020
2021         /*
2022          * Found IO that is still pending with firmware and
2023          * belongs to the LUN that we are resetting
2024          */
2025         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2026                       "Found IO in %s on lun\n",
2027                       fnic_ioreq_state_to_str(fnic_priv(sc)->state));
2028
2029         if (fnic_priv(sc)->state == FNIC_IOREQ_ABTS_PENDING) {
2030                 spin_unlock_irqrestore(io_lock, flags);
2031                 return true;
2032         }
2033         if ((fnic_priv(sc)->flags & FNIC_DEVICE_RESET) &&
2034             (!(fnic_priv(sc)->flags & FNIC_DEV_RST_ISSUED))) {
2035                 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2036                               "%s dev rst not pending sc 0x%p\n", __func__,
2037                               sc);
2038                 spin_unlock_irqrestore(io_lock, flags);
2039                 return true;
2040         }
2041
2042         if (io_req->abts_done)
2043                 shost_printk(KERN_ERR, fnic->lport->host,
2044                              "%s: io_req->abts_done is set state is %s\n",
2045                              __func__, fnic_ioreq_state_to_str(fnic_priv(sc)->state));
2046         old_ioreq_state = fnic_priv(sc)->state;
2047         /*
2048          * Any pending IO issued prior to reset is expected to be
2049          * in abts pending state, if not we need to set
2050          * FNIC_IOREQ_ABTS_PENDING to indicate the IO is abort pending.
2051          * When IO is completed, the IO will be handed over and
2052          * handled in this function.
2053          */
2054         fnic_priv(sc)->state = FNIC_IOREQ_ABTS_PENDING;
2055
2056         BUG_ON(io_req->abts_done);
2057
2058         if (fnic_priv(sc)->flags & FNIC_DEVICE_RESET) {
2059                 abt_tag |= FNIC_TAG_DEV_RST;
2060                 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2061                               "%s: dev rst sc 0x%p\n", __func__, sc);
2062         }
2063
2064         fnic_priv(sc)->abts_status = FCPIO_INVALID_CODE;
2065         io_req->abts_done = &tm_done;
2066         spin_unlock_irqrestore(io_lock, flags);
2067
2068         /* Now queue the abort command to firmware */
2069         int_to_scsilun(sc->device->lun, &fc_lun);
2070
2071         if (fnic_queue_abort_io_req(fnic, abt_tag,
2072                                     FCPIO_ITMF_ABT_TASK_TERM,
2073                                     fc_lun.scsi_lun, io_req)) {
2074                 spin_lock_irqsave(io_lock, flags);
2075                 io_req = fnic_priv(sc)->io_req;
2076                 if (io_req)
2077                         io_req->abts_done = NULL;
2078                 if (fnic_priv(sc)->state == FNIC_IOREQ_ABTS_PENDING)
2079                         fnic_priv(sc)->state = old_ioreq_state;
2080                 spin_unlock_irqrestore(io_lock, flags);
2081                 iter_data->ret = FAILED;
2082                 return false;
2083         } else {
2084                 spin_lock_irqsave(io_lock, flags);
2085                 if (fnic_priv(sc)->flags & FNIC_DEVICE_RESET)
2086                         fnic_priv(sc)->flags |= FNIC_DEV_RST_TERM_ISSUED;
2087                 spin_unlock_irqrestore(io_lock, flags);
2088         }
2089         fnic_priv(sc)->flags |= FNIC_IO_INTERNAL_TERM_ISSUED;
2090
2091         wait_for_completion_timeout(&tm_done, msecs_to_jiffies
2092                                     (fnic->config.ed_tov));
2093
2094         /* Recheck cmd state to check if it is now aborted */
2095         spin_lock_irqsave(io_lock, flags);
2096         io_req = fnic_priv(sc)->io_req;
2097         if (!io_req) {
2098                 spin_unlock_irqrestore(io_lock, flags);
2099                 fnic_priv(sc)->flags |= FNIC_IO_ABT_TERM_REQ_NULL;
2100                 return true;
2101         }
2102
2103         io_req->abts_done = NULL;
2104
2105         /* if abort is still pending with fw, fail */
2106         if (fnic_priv(sc)->abts_status == FCPIO_INVALID_CODE) {
2107                 spin_unlock_irqrestore(io_lock, flags);
2108                 fnic_priv(sc)->flags |= FNIC_IO_ABT_TERM_DONE;
2109                 iter_data->ret = FAILED;
2110                 return false;
2111         }
2112         fnic_priv(sc)->state = FNIC_IOREQ_ABTS_COMPLETE;
2113
2114         /* original sc used for lr is handled by dev reset code */
2115         if (sc != iter_data->lr_sc)
2116                 fnic_priv(sc)->io_req = NULL;
2117         spin_unlock_irqrestore(io_lock, flags);
2118
2119         /* original sc used for lr is handled by dev reset code */
2120         if (sc != iter_data->lr_sc) {
2121                 fnic_release_ioreq_buf(fnic, io_req, sc);
2122                 mempool_free(io_req, fnic->io_req_pool);
2123         }
2124
2125         /*
2126          * Any IO is returned during reset, it needs to call scsi_done
2127          * to return the scsi_cmnd to upper layer.
2128          */
2129         /* Set result to let upper SCSI layer retry */
2130         sc->result = DID_RESET << 16;
2131         scsi_done(sc);
2132
2133         return true;
2134 }
2135
2136 /*
2137  * Clean up any pending aborts on the lun
2138  * For each outstanding IO on this lun, whose abort is not completed by fw,
2139  * issue a local abort. Wait for abort to complete. Return 0 if all commands
2140  * successfully aborted, 1 otherwise
2141  */
2142 static int fnic_clean_pending_aborts(struct fnic *fnic,
2143                                      struct scsi_cmnd *lr_sc,
2144                                      bool new_sc)
2145
2146 {
2147         int ret = SUCCESS;
2148         struct fnic_pending_aborts_iter_data iter_data = {
2149                 .fnic = fnic,
2150                 .lun_dev = lr_sc->device,
2151                 .ret = SUCCESS,
2152         };
2153
2154         if (new_sc)
2155                 iter_data.lr_sc = lr_sc;
2156
2157         scsi_host_busy_iter(fnic->lport->host,
2158                             fnic_pending_aborts_iter, &iter_data);
2159         if (iter_data.ret == FAILED) {
2160                 ret = iter_data.ret;
2161                 goto clean_pending_aborts_end;
2162         }
2163         schedule_timeout(msecs_to_jiffies(2 * fnic->config.ed_tov));
2164
2165         /* walk again to check, if IOs are still pending in fw */
2166         if (fnic_is_abts_pending(fnic, lr_sc))
2167                 ret = FAILED;
2168
2169 clean_pending_aborts_end:
2170         return ret;
2171 }
2172
2173 /*
2174  * fnic_scsi_host_start_tag
2175  * Allocates tagid from host's tag list
2176  **/
2177 static inline int
2178 fnic_scsi_host_start_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2179 {
2180         struct request *rq = scsi_cmd_to_rq(sc);
2181         struct request_queue *q = rq->q;
2182         struct request *dummy;
2183
2184         dummy = blk_mq_alloc_request(q, REQ_OP_WRITE, BLK_MQ_REQ_NOWAIT);
2185         if (IS_ERR(dummy))
2186                 return SCSI_NO_TAG;
2187
2188         rq->tag = dummy->tag;
2189         sc->host_scribble = (unsigned char *)dummy;
2190
2191         return dummy->tag;
2192 }
2193
2194 /*
2195  * fnic_scsi_host_end_tag
2196  * frees tag allocated by fnic_scsi_host_start_tag.
2197  **/
2198 static inline void
2199 fnic_scsi_host_end_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2200 {
2201         struct request *dummy = (struct request *)sc->host_scribble;
2202
2203         blk_mq_free_request(dummy);
2204 }
2205
2206 /*
2207  * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
2208  * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
2209  * on the LUN.
2210  */
2211 int fnic_device_reset(struct scsi_cmnd *sc)
2212 {
2213         struct request *rq = scsi_cmd_to_rq(sc);
2214         struct fc_lport *lp;
2215         struct fnic *fnic;
2216         struct fnic_io_req *io_req = NULL;
2217         struct fc_rport *rport;
2218         int status;
2219         int ret = FAILED;
2220         spinlock_t *io_lock;
2221         unsigned long flags;
2222         unsigned long start_time = 0;
2223         struct scsi_lun fc_lun;
2224         struct fnic_stats *fnic_stats;
2225         struct reset_stats *reset_stats;
2226         int tag = rq->tag;
2227         DECLARE_COMPLETION_ONSTACK(tm_done);
2228         int tag_gen_flag = 0;   /*to track tags allocated by fnic driver*/
2229         bool new_sc = 0;
2230
2231         /* Wait for rport to unblock */
2232         fc_block_scsi_eh(sc);
2233
2234         /* Get local-port, check ready and link up */
2235         lp = shost_priv(sc->device->host);
2236
2237         fnic = lport_priv(lp);
2238         fnic_stats = &fnic->fnic_stats;
2239         reset_stats = &fnic->fnic_stats.reset_stats;
2240
2241         atomic64_inc(&reset_stats->device_resets);
2242
2243         rport = starget_to_rport(scsi_target(sc->device));
2244         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2245                       "Device reset called FCID 0x%x, LUN 0x%llx sc 0x%p\n",
2246                       rport->port_id, sc->device->lun, sc);
2247
2248         if (lp->state != LPORT_ST_READY || !(lp->link_up))
2249                 goto fnic_device_reset_end;
2250
2251         /* Check if remote port up */
2252         if (fc_remote_port_chkready(rport)) {
2253                 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
2254                 goto fnic_device_reset_end;
2255         }
2256
2257         fnic_priv(sc)->flags = FNIC_DEVICE_RESET;
2258         /* Allocate tag if not present */
2259
2260         if (unlikely(tag < 0)) {
2261                 /*
2262                  * Really should fix the midlayer to pass in a proper
2263                  * request for ioctls...
2264                  */
2265                 tag = fnic_scsi_host_start_tag(fnic, sc);
2266                 if (unlikely(tag == SCSI_NO_TAG))
2267                         goto fnic_device_reset_end;
2268                 tag_gen_flag = 1;
2269                 new_sc = 1;
2270         }
2271         io_lock = fnic_io_lock_hash(fnic, sc);
2272         spin_lock_irqsave(io_lock, flags);
2273         io_req = fnic_priv(sc)->io_req;
2274
2275         /*
2276          * If there is a io_req attached to this command, then use it,
2277          * else allocate a new one.
2278          */
2279         if (!io_req) {
2280                 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
2281                 if (!io_req) {
2282                         spin_unlock_irqrestore(io_lock, flags);
2283                         goto fnic_device_reset_end;
2284                 }
2285                 memset(io_req, 0, sizeof(*io_req));
2286                 io_req->port_id = rport->port_id;
2287                 fnic_priv(sc)->io_req = io_req;
2288         }
2289         io_req->dr_done = &tm_done;
2290         fnic_priv(sc)->state = FNIC_IOREQ_CMD_PENDING;
2291         fnic_priv(sc)->lr_status = FCPIO_INVALID_CODE;
2292         spin_unlock_irqrestore(io_lock, flags);
2293
2294         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %x\n", tag);
2295
2296         /*
2297          * issue the device reset, if enqueue failed, clean up the ioreq
2298          * and break assoc with scsi cmd
2299          */
2300         if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
2301                 spin_lock_irqsave(io_lock, flags);
2302                 io_req = fnic_priv(sc)->io_req;
2303                 if (io_req)
2304                         io_req->dr_done = NULL;
2305                 goto fnic_device_reset_clean;
2306         }
2307         spin_lock_irqsave(io_lock, flags);
2308         fnic_priv(sc)->flags |= FNIC_DEV_RST_ISSUED;
2309         spin_unlock_irqrestore(io_lock, flags);
2310
2311         /*
2312          * Wait on the local completion for LUN reset.  The io_req may be
2313          * freed while we wait since we hold no lock.
2314          */
2315         wait_for_completion_timeout(&tm_done,
2316                                     msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2317
2318         spin_lock_irqsave(io_lock, flags);
2319         io_req = fnic_priv(sc)->io_req;
2320         if (!io_req) {
2321                 spin_unlock_irqrestore(io_lock, flags);
2322                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2323                                 "io_req is null tag 0x%x sc 0x%p\n", tag, sc);
2324                 goto fnic_device_reset_end;
2325         }
2326         io_req->dr_done = NULL;
2327
2328         status = fnic_priv(sc)->lr_status;
2329
2330         /*
2331          * If lun reset not completed, bail out with failed. io_req
2332          * gets cleaned up during higher levels of EH
2333          */
2334         if (status == FCPIO_INVALID_CODE) {
2335                 atomic64_inc(&reset_stats->device_reset_timeouts);
2336                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2337                               "Device reset timed out\n");
2338                 fnic_priv(sc)->flags |= FNIC_DEV_RST_TIMED_OUT;
2339                 spin_unlock_irqrestore(io_lock, flags);
2340                 int_to_scsilun(sc->device->lun, &fc_lun);
2341                 /*
2342                  * Issue abort and terminate on device reset request.
2343                  * If q'ing of terminate fails, retry it after a delay.
2344                  */
2345                 while (1) {
2346                         spin_lock_irqsave(io_lock, flags);
2347                         if (fnic_priv(sc)->flags & FNIC_DEV_RST_TERM_ISSUED) {
2348                                 spin_unlock_irqrestore(io_lock, flags);
2349                                 break;
2350                         }
2351                         spin_unlock_irqrestore(io_lock, flags);
2352                         if (fnic_queue_abort_io_req(fnic,
2353                                 tag | FNIC_TAG_DEV_RST,
2354                                 FCPIO_ITMF_ABT_TASK_TERM,
2355                                 fc_lun.scsi_lun, io_req)) {
2356                                 wait_for_completion_timeout(&tm_done,
2357                                 msecs_to_jiffies(FNIC_ABT_TERM_DELAY_TIMEOUT));
2358                         } else {
2359                                 spin_lock_irqsave(io_lock, flags);
2360                                 fnic_priv(sc)->flags |= FNIC_DEV_RST_TERM_ISSUED;
2361                                 fnic_priv(sc)->state = FNIC_IOREQ_ABTS_PENDING;
2362                                 io_req->abts_done = &tm_done;
2363                                 spin_unlock_irqrestore(io_lock, flags);
2364                                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2365                                 "Abort and terminate issued on Device reset "
2366                                 "tag 0x%x sc 0x%p\n", tag, sc);
2367                                 break;
2368                         }
2369                 }
2370                 while (1) {
2371                         spin_lock_irqsave(io_lock, flags);
2372                         if (!(fnic_priv(sc)->flags & FNIC_DEV_RST_DONE)) {
2373                                 spin_unlock_irqrestore(io_lock, flags);
2374                                 wait_for_completion_timeout(&tm_done,
2375                                 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2376                                 break;
2377                         } else {
2378                                 io_req = fnic_priv(sc)->io_req;
2379                                 io_req->abts_done = NULL;
2380                                 goto fnic_device_reset_clean;
2381                         }
2382                 }
2383         } else {
2384                 spin_unlock_irqrestore(io_lock, flags);
2385         }
2386
2387         /* Completed, but not successful, clean up the io_req, return fail */
2388         if (status != FCPIO_SUCCESS) {
2389                 spin_lock_irqsave(io_lock, flags);
2390                 FNIC_SCSI_DBG(KERN_DEBUG,
2391                               fnic->lport->host,
2392                               "Device reset completed - failed\n");
2393                 io_req = fnic_priv(sc)->io_req;
2394                 goto fnic_device_reset_clean;
2395         }
2396
2397         /*
2398          * Clean up any aborts on this lun that have still not
2399          * completed. If any of these fail, then LUN reset fails.
2400          * clean_pending_aborts cleans all cmds on this lun except
2401          * the lun reset cmd. If all cmds get cleaned, the lun reset
2402          * succeeds
2403          */
2404         if (fnic_clean_pending_aborts(fnic, sc, new_sc)) {
2405                 spin_lock_irqsave(io_lock, flags);
2406                 io_req = fnic_priv(sc)->io_req;
2407                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2408                               "Device reset failed"
2409                               " since could not abort all IOs\n");
2410                 goto fnic_device_reset_clean;
2411         }
2412
2413         /* Clean lun reset command */
2414         spin_lock_irqsave(io_lock, flags);
2415         io_req = fnic_priv(sc)->io_req;
2416         if (io_req)
2417                 /* Completed, and successful */
2418                 ret = SUCCESS;
2419
2420 fnic_device_reset_clean:
2421         if (io_req)
2422                 fnic_priv(sc)->io_req = NULL;
2423
2424         spin_unlock_irqrestore(io_lock, flags);
2425
2426         if (io_req) {
2427                 start_time = io_req->start_time;
2428                 fnic_release_ioreq_buf(fnic, io_req, sc);
2429                 mempool_free(io_req, fnic->io_req_pool);
2430         }
2431
2432 fnic_device_reset_end:
2433         FNIC_TRACE(fnic_device_reset, sc->device->host->host_no, rq->tag, sc,
2434                   jiffies_to_msecs(jiffies - start_time),
2435                   0, ((u64)sc->cmnd[0] << 32 |
2436                   (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
2437                   (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
2438                   fnic_flags_and_state(sc));
2439
2440         /* free tag if it is allocated */
2441         if (unlikely(tag_gen_flag))
2442                 fnic_scsi_host_end_tag(fnic, sc);
2443
2444         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2445                       "Returning from device reset %s\n",
2446                       (ret == SUCCESS) ?
2447                       "SUCCESS" : "FAILED");
2448
2449         if (ret == FAILED)
2450                 atomic64_inc(&reset_stats->device_reset_failures);
2451
2452         return ret;
2453 }
2454
2455 /* Clean up all IOs, clean up libFC local port */
2456 int fnic_reset(struct Scsi_Host *shost)
2457 {
2458         struct fc_lport *lp;
2459         struct fnic *fnic;
2460         int ret = 0;
2461         struct reset_stats *reset_stats;
2462
2463         lp = shost_priv(shost);
2464         fnic = lport_priv(lp);
2465         reset_stats = &fnic->fnic_stats.reset_stats;
2466
2467         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2468                       "fnic_reset called\n");
2469
2470         atomic64_inc(&reset_stats->fnic_resets);
2471
2472         /*
2473          * Reset local port, this will clean up libFC exchanges,
2474          * reset remote port sessions, and if link is up, begin flogi
2475          */
2476         ret = fc_lport_reset(lp);
2477
2478         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2479                       "Returning from fnic reset %s\n",
2480                       (ret == 0) ?
2481                       "SUCCESS" : "FAILED");
2482
2483         if (ret == 0)
2484                 atomic64_inc(&reset_stats->fnic_reset_completions);
2485         else
2486                 atomic64_inc(&reset_stats->fnic_reset_failures);
2487
2488         return ret;
2489 }
2490
2491 /*
2492  * SCSI Error handling calls driver's eh_host_reset if all prior
2493  * error handling levels return FAILED. If host reset completes
2494  * successfully, and if link is up, then Fabric login begins.
2495  *
2496  * Host Reset is the highest level of error recovery. If this fails, then
2497  * host is offlined by SCSI.
2498  *
2499  */
2500 int fnic_host_reset(struct scsi_cmnd *sc)
2501 {
2502         int ret;
2503         unsigned long wait_host_tmo;
2504         struct Scsi_Host *shost = sc->device->host;
2505         struct fc_lport *lp = shost_priv(shost);
2506         struct fnic *fnic = lport_priv(lp);
2507         unsigned long flags;
2508
2509         spin_lock_irqsave(&fnic->fnic_lock, flags);
2510         if (!fnic->internal_reset_inprogress) {
2511                 fnic->internal_reset_inprogress = true;
2512         } else {
2513                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2514                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2515                         "host reset in progress skipping another host reset\n");
2516                 return SUCCESS;
2517         }
2518         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2519
2520         /*
2521          * If fnic_reset is successful, wait for fabric login to complete
2522          * scsi-ml tries to send a TUR to every device if host reset is
2523          * successful, so before returning to scsi, fabric should be up
2524          */
2525         ret = (fnic_reset(shost) == 0) ? SUCCESS : FAILED;
2526         if (ret == SUCCESS) {
2527                 wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
2528                 ret = FAILED;
2529                 while (time_before(jiffies, wait_host_tmo)) {
2530                         if ((lp->state == LPORT_ST_READY) &&
2531                             (lp->link_up)) {
2532                                 ret = SUCCESS;
2533                                 break;
2534                         }
2535                         ssleep(1);
2536                 }
2537         }
2538
2539         spin_lock_irqsave(&fnic->fnic_lock, flags);
2540         fnic->internal_reset_inprogress = false;
2541         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2542         return ret;
2543 }
2544
2545 /*
2546  * This fxn is called from libFC when host is removed
2547  */
2548 void fnic_scsi_abort_io(struct fc_lport *lp)
2549 {
2550         int err = 0;
2551         unsigned long flags;
2552         enum fnic_state old_state;
2553         struct fnic *fnic = lport_priv(lp);
2554         DECLARE_COMPLETION_ONSTACK(remove_wait);
2555
2556         /* Issue firmware reset for fnic, wait for reset to complete */
2557 retry_fw_reset:
2558         spin_lock_irqsave(&fnic->fnic_lock, flags);
2559         if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) &&
2560                      fnic->link_events) {
2561                 /* fw reset is in progress, poll for its completion */
2562                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2563                 schedule_timeout(msecs_to_jiffies(100));
2564                 goto retry_fw_reset;
2565         }
2566
2567         fnic->remove_wait = &remove_wait;
2568         old_state = fnic->state;
2569         fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
2570         fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
2571         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2572
2573         err = fnic_fw_reset_handler(fnic);
2574         if (err) {
2575                 spin_lock_irqsave(&fnic->fnic_lock, flags);
2576                 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2577                         fnic->state = old_state;
2578                 fnic->remove_wait = NULL;
2579                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2580                 return;
2581         }
2582
2583         /* Wait for firmware reset to complete */
2584         wait_for_completion_timeout(&remove_wait,
2585                                     msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
2586
2587         spin_lock_irqsave(&fnic->fnic_lock, flags);
2588         fnic->remove_wait = NULL;
2589         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2590                       "fnic_scsi_abort_io %s\n",
2591                       (fnic->state == FNIC_IN_ETH_MODE) ?
2592                       "SUCCESS" : "FAILED");
2593         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2594
2595 }
2596
2597 /*
2598  * This fxn called from libFC to clean up driver IO state on link down
2599  */
2600 void fnic_scsi_cleanup(struct fc_lport *lp)
2601 {
2602         unsigned long flags;
2603         enum fnic_state old_state;
2604         struct fnic *fnic = lport_priv(lp);
2605
2606         /* issue fw reset */
2607 retry_fw_reset:
2608         spin_lock_irqsave(&fnic->fnic_lock, flags);
2609         if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2610                 /* fw reset is in progress, poll for its completion */
2611                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2612                 schedule_timeout(msecs_to_jiffies(100));
2613                 goto retry_fw_reset;
2614         }
2615         old_state = fnic->state;
2616         fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
2617         fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
2618         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2619
2620         if (fnic_fw_reset_handler(fnic)) {
2621                 spin_lock_irqsave(&fnic->fnic_lock, flags);
2622                 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2623                         fnic->state = old_state;
2624                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2625         }
2626
2627 }
2628
2629 void fnic_empty_scsi_cleanup(struct fc_lport *lp)
2630 {
2631 }
2632
2633 void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
2634 {
2635         struct fnic *fnic = lport_priv(lp);
2636
2637         /* Non-zero sid, nothing to do */
2638         if (sid)
2639                 goto call_fc_exch_mgr_reset;
2640
2641         if (did) {
2642                 fnic_rport_exch_reset(fnic, did);
2643                 goto call_fc_exch_mgr_reset;
2644         }
2645
2646         /*
2647          * sid = 0, did = 0
2648          * link down or device being removed
2649          */
2650         if (!fnic->in_remove)
2651                 fnic_scsi_cleanup(lp);
2652         else
2653                 fnic_scsi_abort_io(lp);
2654
2655         /* call libFC exch mgr reset to reset its exchanges */
2656 call_fc_exch_mgr_reset:
2657         fc_exch_mgr_reset(lp, sid, did);
2658
2659 }
2660
2661 static bool fnic_abts_pending_iter(struct scsi_cmnd *sc, void *data,
2662                                    bool reserved)
2663 {
2664         struct fnic_pending_aborts_iter_data *iter_data = data;
2665         struct fnic *fnic = iter_data->fnic;
2666         int cmd_state;
2667         struct fnic_io_req *io_req;
2668         spinlock_t *io_lock;
2669         unsigned long flags;
2670
2671         /*
2672          * ignore this lun reset cmd or cmds that do not belong to
2673          * this lun
2674          */
2675         if (iter_data->lr_sc && sc == iter_data->lr_sc)
2676                 return true;
2677         if (iter_data->lun_dev && sc->device != iter_data->lun_dev)
2678                 return true;
2679
2680         io_lock = fnic_io_lock_hash(fnic, sc);
2681         spin_lock_irqsave(io_lock, flags);
2682
2683         io_req = fnic_priv(sc)->io_req;
2684         if (!io_req) {
2685                 spin_unlock_irqrestore(io_lock, flags);
2686                 return true;
2687         }
2688
2689         /*
2690          * Found IO that is still pending with firmware and
2691          * belongs to the LUN that we are resetting
2692          */
2693         FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2694                       "Found IO in %s on lun\n",
2695                       fnic_ioreq_state_to_str(fnic_priv(sc)->state));
2696         cmd_state = fnic_priv(sc)->state;
2697         spin_unlock_irqrestore(io_lock, flags);
2698         if (cmd_state == FNIC_IOREQ_ABTS_PENDING)
2699                 iter_data->ret = 1;
2700
2701         return iter_data->ret ? false : true;
2702 }
2703
2704 /*
2705  * fnic_is_abts_pending() is a helper function that
2706  * walks through tag map to check if there is any IOs pending,if there is one,
2707  * then it returns 1 (true), otherwise 0 (false)
2708  * if @lr_sc is non NULL, then it checks IOs specific to particular LUN,
2709  * otherwise, it checks for all IOs.
2710  */
2711 int fnic_is_abts_pending(struct fnic *fnic, struct scsi_cmnd *lr_sc)
2712 {
2713         struct fnic_pending_aborts_iter_data iter_data = {
2714                 .fnic = fnic,
2715                 .lun_dev = NULL,
2716                 .ret = 0,
2717         };
2718
2719         if (lr_sc) {
2720                 iter_data.lun_dev = lr_sc->device;
2721                 iter_data.lr_sc = lr_sc;
2722         }
2723
2724         /* walk again to check, if IOs are still pending in fw */
2725         scsi_host_busy_iter(fnic->lport->host,
2726                             fnic_abts_pending_iter, &iter_data);
2727
2728         return iter_data.ret;
2729 }