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