]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/scsi/fnic/fnic_scsi.c
Merge remote-tracking branch 'kvm/linux-next'
[karo-tx-linux.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
232         spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
233
234         if (!ret)
235                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
236                               "Issued fw reset\n");
237         else {
238                 fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
239                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
240                               "Failed to issue fw reset\n");
241         }
242
243         return ret;
244 }
245
246
247 /*
248  * fnic_flogi_reg_handler
249  * Routine to send flogi register msg to fw
250  */
251 int fnic_flogi_reg_handler(struct fnic *fnic, u32 fc_id)
252 {
253         struct vnic_wq_copy *wq = &fnic->wq_copy[0];
254         enum fcpio_flogi_reg_format_type format;
255         struct fc_lport *lp = fnic->lport;
256         u8 gw_mac[ETH_ALEN];
257         int ret = 0;
258         unsigned long flags;
259
260         spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
261
262         if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
263                 free_wq_copy_descs(fnic, wq);
264
265         if (!vnic_wq_copy_desc_avail(wq)) {
266                 ret = -EAGAIN;
267                 goto flogi_reg_ioreq_end;
268         }
269
270         if (fnic->ctlr.map_dest) {
271                 memset(gw_mac, 0xff, ETH_ALEN);
272                 format = FCPIO_FLOGI_REG_DEF_DEST;
273         } else {
274                 memcpy(gw_mac, fnic->ctlr.dest_addr, ETH_ALEN);
275                 format = FCPIO_FLOGI_REG_GW_DEST;
276         }
277
278         if ((fnic->config.flags & VFCF_FIP_CAPABLE) && !fnic->ctlr.map_dest) {
279                 fnic_queue_wq_copy_desc_fip_reg(wq, SCSI_NO_TAG,
280                                                 fc_id, gw_mac,
281                                                 fnic->data_src_addr,
282                                                 lp->r_a_tov, lp->e_d_tov);
283                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
284                               "FLOGI FIP reg issued fcid %x src %pM dest %pM\n",
285                               fc_id, fnic->data_src_addr, gw_mac);
286         } else {
287                 fnic_queue_wq_copy_desc_flogi_reg(wq, SCSI_NO_TAG,
288                                                   format, fc_id, gw_mac);
289                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
290                               "FLOGI reg issued fcid %x map %d dest %pM\n",
291                               fc_id, fnic->ctlr.map_dest, gw_mac);
292         }
293
294 flogi_reg_ioreq_end:
295         spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
296         return ret;
297 }
298
299 /*
300  * fnic_queue_wq_copy_desc
301  * Routine to enqueue a wq copy desc
302  */
303 static inline int fnic_queue_wq_copy_desc(struct fnic *fnic,
304                                           struct vnic_wq_copy *wq,
305                                           struct fnic_io_req *io_req,
306                                           struct scsi_cmnd *sc,
307                                           int sg_count)
308 {
309         struct scatterlist *sg;
310         struct fc_rport *rport = starget_to_rport(scsi_target(sc->device));
311         struct fc_rport_libfc_priv *rp = rport->dd_data;
312         struct host_sg_desc *desc;
313         u8 pri_tag = 0;
314         unsigned int i;
315         unsigned long intr_flags;
316         int flags;
317         u8 exch_flags;
318         struct scsi_lun fc_lun;
319         char msg[2];
320
321         if (sg_count) {
322                 /* For each SGE, create a device desc entry */
323                 desc = io_req->sgl_list;
324                 for_each_sg(scsi_sglist(sc), sg, sg_count, i) {
325                         desc->addr = cpu_to_le64(sg_dma_address(sg));
326                         desc->len = cpu_to_le32(sg_dma_len(sg));
327                         desc->_resvd = 0;
328                         desc++;
329                 }
330
331                 io_req->sgl_list_pa = pci_map_single
332                         (fnic->pdev,
333                          io_req->sgl_list,
334                          sizeof(io_req->sgl_list[0]) * sg_count,
335                          PCI_DMA_TODEVICE);
336         }
337
338         io_req->sense_buf_pa = pci_map_single(fnic->pdev,
339                                               sc->sense_buffer,
340                                               SCSI_SENSE_BUFFERSIZE,
341                                               PCI_DMA_FROMDEVICE);
342
343         int_to_scsilun(sc->device->lun, &fc_lun);
344
345         pri_tag = FCPIO_ICMND_PTA_SIMPLE;
346         msg[0] = MSG_SIMPLE_TAG;
347         scsi_populate_tag_msg(sc, msg);
348         if (msg[0] == MSG_ORDERED_TAG)
349                 pri_tag = FCPIO_ICMND_PTA_ORDERED;
350
351         /* Enqueue the descriptor in the Copy WQ */
352         spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
353
354         if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
355                 free_wq_copy_descs(fnic, wq);
356
357         if (unlikely(!vnic_wq_copy_desc_avail(wq))) {
358                 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
359                 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
360                           "fnic_queue_wq_copy_desc failure - no descriptors\n");
361                 return SCSI_MLQUEUE_HOST_BUSY;
362         }
363
364         flags = 0;
365         if (sc->sc_data_direction == DMA_FROM_DEVICE)
366                 flags = FCPIO_ICMND_RDDATA;
367         else if (sc->sc_data_direction == DMA_TO_DEVICE)
368                 flags = FCPIO_ICMND_WRDATA;
369
370         exch_flags = 0;
371         if ((fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR) &&
372             (rp->flags & FC_RP_FLAGS_RETRY))
373                 exch_flags |= FCPIO_ICMND_SRFLAG_RETRY;
374
375         fnic_queue_wq_copy_desc_icmnd_16(wq, sc->request->tag,
376                                          0, exch_flags, io_req->sgl_cnt,
377                                          SCSI_SENSE_BUFFERSIZE,
378                                          io_req->sgl_list_pa,
379                                          io_req->sense_buf_pa,
380                                          0, /* scsi cmd ref, always 0 */
381                                          pri_tag, /* scsi pri and tag */
382                                          flags, /* command flags */
383                                          sc->cmnd, sc->cmd_len,
384                                          scsi_bufflen(sc),
385                                          fc_lun.scsi_lun, io_req->port_id,
386                                          rport->maxframe_size, rp->r_a_tov,
387                                          rp->e_d_tov);
388
389         spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
390         return 0;
391 }
392
393 /*
394  * fnic_queuecommand
395  * Routine to send a scsi cdb
396  * Called with host_lock held and interrupts disabled.
397  */
398 static int fnic_queuecommand_lck(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
399 {
400         struct fc_lport *lp = shost_priv(sc->device->host);
401         struct fc_rport *rport;
402         struct fnic_io_req *io_req = NULL;
403         struct fnic *fnic = lport_priv(lp);
404         struct vnic_wq_copy *wq;
405         int ret;
406         u64 cmd_trace;
407         int sg_count = 0;
408         unsigned long flags;
409         unsigned long ptr;
410
411         if (unlikely(fnic_chk_state_flags_locked(fnic, FNIC_FLAGS_IO_BLOCKED)))
412                 return SCSI_MLQUEUE_HOST_BUSY;
413
414         rport = starget_to_rport(scsi_target(sc->device));
415         ret = fc_remote_port_chkready(rport);
416         if (ret) {
417                 sc->result = ret;
418                 done(sc);
419                 return 0;
420         }
421
422         if (lp->state != LPORT_ST_READY || !(lp->link_up))
423                 return SCSI_MLQUEUE_HOST_BUSY;
424
425         atomic_inc(&fnic->in_flight);
426
427         /*
428          * Release host lock, use driver resource specific locks from here.
429          * Don't re-enable interrupts in case they were disabled prior to the
430          * caller disabling them.
431          */
432         spin_unlock(lp->host->host_lock);
433         CMD_STATE(sc) = FNIC_IOREQ_NOT_INITED;
434         CMD_FLAGS(sc) = FNIC_NO_FLAGS;
435
436         /* Get a new io_req for this SCSI IO */
437         io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
438         if (!io_req) {
439                 ret = SCSI_MLQUEUE_HOST_BUSY;
440                 goto out;
441         }
442         memset(io_req, 0, sizeof(*io_req));
443
444         /* Map the data buffer */
445         sg_count = scsi_dma_map(sc);
446         if (sg_count < 0) {
447                 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
448                           sc->request->tag, sc, 0, sc->cmnd[0],
449                           sg_count, CMD_STATE(sc));
450                 mempool_free(io_req, fnic->io_req_pool);
451                 goto out;
452         }
453
454         /* Determine the type of scatter/gather list we need */
455         io_req->sgl_cnt = sg_count;
456         io_req->sgl_type = FNIC_SGL_CACHE_DFLT;
457         if (sg_count > FNIC_DFLT_SG_DESC_CNT)
458                 io_req->sgl_type = FNIC_SGL_CACHE_MAX;
459
460         if (sg_count) {
461                 io_req->sgl_list =
462                         mempool_alloc(fnic->io_sgl_pool[io_req->sgl_type],
463                                       GFP_ATOMIC);
464                 if (!io_req->sgl_list) {
465                         ret = SCSI_MLQUEUE_HOST_BUSY;
466                         scsi_dma_unmap(sc);
467                         mempool_free(io_req, fnic->io_req_pool);
468                         goto out;
469                 }
470
471                 /* Cache sgl list allocated address before alignment */
472                 io_req->sgl_list_alloc = io_req->sgl_list;
473                 ptr = (unsigned long) io_req->sgl_list;
474                 if (ptr % FNIC_SG_DESC_ALIGN) {
475                         io_req->sgl_list = (struct host_sg_desc *)
476                                 (((unsigned long) ptr
477                                   + FNIC_SG_DESC_ALIGN - 1)
478                                  & ~(FNIC_SG_DESC_ALIGN - 1));
479                 }
480         }
481
482         /* initialize rest of io_req */
483         io_req->port_id = rport->port_id;
484         io_req->start_time = jiffies;
485         CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
486         CMD_SP(sc) = (char *)io_req;
487         CMD_FLAGS(sc) |= FNIC_IO_INITIALIZED;
488         sc->scsi_done = done;
489
490         /* create copy wq desc and enqueue it */
491         wq = &fnic->wq_copy[0];
492         ret = fnic_queue_wq_copy_desc(fnic, wq, io_req, sc, sg_count);
493         if (ret) {
494                 /*
495                  * In case another thread cancelled the request,
496                  * refetch the pointer under the lock.
497                  */
498                 spinlock_t *io_lock = fnic_io_lock_hash(fnic, sc);
499                 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
500                           sc->request->tag, sc, 0, 0, 0,
501                           (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
502                 spin_lock_irqsave(io_lock, flags);
503                 io_req = (struct fnic_io_req *)CMD_SP(sc);
504                 CMD_SP(sc) = NULL;
505                 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
506                 spin_unlock_irqrestore(io_lock, flags);
507                 if (io_req) {
508                         fnic_release_ioreq_buf(fnic, io_req, sc);
509                         mempool_free(io_req, fnic->io_req_pool);
510                 }
511         } else {
512                 /* REVISIT: Use per IO lock in the final code */
513                 CMD_FLAGS(sc) |= FNIC_IO_ISSUED;
514         }
515 out:
516         cmd_trace = ((u64)sc->cmnd[0] << 56 | (u64)sc->cmnd[7] << 40 |
517                         (u64)sc->cmnd[8] << 32 | (u64)sc->cmnd[2] << 24 |
518                         (u64)sc->cmnd[3] << 16 | (u64)sc->cmnd[4] << 8 |
519                         sc->cmnd[5]);
520
521         FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
522                   sc->request->tag, sc, io_req,
523                   sg_count, cmd_trace,
524                   (((u64)CMD_FLAGS(sc) >> 32) | CMD_STATE(sc)));
525         atomic_dec(&fnic->in_flight);
526         /* acquire host lock before returning to SCSI */
527         spin_lock(lp->host->host_lock);
528         return ret;
529 }
530
531 DEF_SCSI_QCMD(fnic_queuecommand)
532
533 /*
534  * fnic_fcpio_fw_reset_cmpl_handler
535  * Routine to handle fw reset completion
536  */
537 static int fnic_fcpio_fw_reset_cmpl_handler(struct fnic *fnic,
538                                             struct fcpio_fw_req *desc)
539 {
540         u8 type;
541         u8 hdr_status;
542         struct fcpio_tag tag;
543         int ret = 0;
544         unsigned long flags;
545
546         fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
547
548         /* Clean up all outstanding io requests */
549         fnic_cleanup_io(fnic, SCSI_NO_TAG);
550
551         spin_lock_irqsave(&fnic->fnic_lock, flags);
552
553         /* fnic should be in FC_TRANS_ETH_MODE */
554         if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) {
555                 /* Check status of reset completion */
556                 if (!hdr_status) {
557                         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
558                                       "reset cmpl success\n");
559                         /* Ready to send flogi out */
560                         fnic->state = FNIC_IN_ETH_MODE;
561                 } else {
562                         FNIC_SCSI_DBG(KERN_DEBUG,
563                                       fnic->lport->host,
564                                       "fnic fw_reset : failed %s\n",
565                                       fnic_fcpio_status_to_str(hdr_status));
566
567                         /*
568                          * Unable to change to eth mode, cannot send out flogi
569                          * Change state to fc mode, so that subsequent Flogi
570                          * requests from libFC will cause more attempts to
571                          * reset the firmware. Free the cached flogi
572                          */
573                         fnic->state = FNIC_IN_FC_MODE;
574                         ret = -1;
575                 }
576         } else {
577                 FNIC_SCSI_DBG(KERN_DEBUG,
578                               fnic->lport->host,
579                               "Unexpected state %s while processing"
580                               " reset cmpl\n", fnic_state_to_str(fnic->state));
581                 ret = -1;
582         }
583
584         /* Thread removing device blocks till firmware reset is complete */
585         if (fnic->remove_wait)
586                 complete(fnic->remove_wait);
587
588         /*
589          * If fnic is being removed, or fw reset failed
590          * free the flogi frame. Else, send it out
591          */
592         if (fnic->remove_wait || ret) {
593                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
594                 skb_queue_purge(&fnic->tx_queue);
595                 goto reset_cmpl_handler_end;
596         }
597
598         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
599
600         fnic_flush_tx(fnic);
601
602  reset_cmpl_handler_end:
603         fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
604
605         return ret;
606 }
607
608 /*
609  * fnic_fcpio_flogi_reg_cmpl_handler
610  * Routine to handle flogi register completion
611  */
612 static int fnic_fcpio_flogi_reg_cmpl_handler(struct fnic *fnic,
613                                              struct fcpio_fw_req *desc)
614 {
615         u8 type;
616         u8 hdr_status;
617         struct fcpio_tag tag;
618         int ret = 0;
619         unsigned long flags;
620
621         fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
622
623         /* Update fnic state based on status of flogi reg completion */
624         spin_lock_irqsave(&fnic->fnic_lock, flags);
625
626         if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE) {
627
628                 /* Check flogi registration completion status */
629                 if (!hdr_status) {
630                         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
631                                       "flog reg succeeded\n");
632                         fnic->state = FNIC_IN_FC_MODE;
633                 } else {
634                         FNIC_SCSI_DBG(KERN_DEBUG,
635                                       fnic->lport->host,
636                                       "fnic flogi reg :failed %s\n",
637                                       fnic_fcpio_status_to_str(hdr_status));
638                         fnic->state = FNIC_IN_ETH_MODE;
639                         ret = -1;
640                 }
641         } else {
642                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
643                               "Unexpected fnic state %s while"
644                               " processing flogi reg completion\n",
645                               fnic_state_to_str(fnic->state));
646                 ret = -1;
647         }
648
649         if (!ret) {
650                 if (fnic->stop_rx_link_events) {
651                         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
652                         goto reg_cmpl_handler_end;
653                 }
654                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
655
656                 fnic_flush_tx(fnic);
657                 queue_work(fnic_event_queue, &fnic->frame_work);
658         } else {
659                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
660         }
661
662 reg_cmpl_handler_end:
663         return ret;
664 }
665
666 static inline int is_ack_index_in_range(struct vnic_wq_copy *wq,
667                                         u16 request_out)
668 {
669         if (wq->to_clean_index <= wq->to_use_index) {
670                 /* out of range, stale request_out index */
671                 if (request_out < wq->to_clean_index ||
672                     request_out >= wq->to_use_index)
673                         return 0;
674         } else {
675                 /* out of range, stale request_out index */
676                 if (request_out < wq->to_clean_index &&
677                     request_out >= wq->to_use_index)
678                         return 0;
679         }
680         /* request_out index is in range */
681         return 1;
682 }
683
684
685 /*
686  * Mark that ack received and store the Ack index. If there are multiple
687  * acks received before Tx thread cleans it up, the latest value will be
688  * used which is correct behavior. This state should be in the copy Wq
689  * instead of in the fnic
690  */
691 static inline void fnic_fcpio_ack_handler(struct fnic *fnic,
692                                           unsigned int cq_index,
693                                           struct fcpio_fw_req *desc)
694 {
695         struct vnic_wq_copy *wq;
696         u16 request_out = desc->u.ack.request_out;
697         unsigned long flags;
698         u64 *ox_id_tag = (u64 *)(void *)desc;
699
700         /* mark the ack state */
701         wq = &fnic->wq_copy[cq_index - fnic->raw_wq_count - fnic->rq_count];
702         spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
703
704         if (is_ack_index_in_range(wq, request_out)) {
705                 fnic->fw_ack_index[0] = request_out;
706                 fnic->fw_ack_recd[0] = 1;
707         }
708         spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
709         FNIC_TRACE(fnic_fcpio_ack_handler,
710                   fnic->lport->host->host_no, 0, 0, ox_id_tag[2], ox_id_tag[3],
711                   ox_id_tag[4], ox_id_tag[5]);
712 }
713
714 /*
715  * fnic_fcpio_icmnd_cmpl_handler
716  * Routine to handle icmnd completions
717  */
718 static void fnic_fcpio_icmnd_cmpl_handler(struct fnic *fnic,
719                                          struct fcpio_fw_req *desc)
720 {
721         u8 type;
722         u8 hdr_status;
723         struct fcpio_tag tag;
724         u32 id;
725         u64 xfer_len = 0;
726         struct fcpio_icmnd_cmpl *icmnd_cmpl;
727         struct fnic_io_req *io_req;
728         struct scsi_cmnd *sc;
729         unsigned long flags;
730         spinlock_t *io_lock;
731         u64 cmd_trace;
732         unsigned long start_time;
733
734         /* Decode the cmpl description to get the io_req id */
735         fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
736         fcpio_tag_id_dec(&tag, &id);
737         icmnd_cmpl = &desc->u.icmnd_cmpl;
738
739         if (id >= fnic->fnic_max_tag_id) {
740                 shost_printk(KERN_ERR, fnic->lport->host,
741                         "Tag out of range tag %x hdr status = %s\n",
742                              id, fnic_fcpio_status_to_str(hdr_status));
743                 return;
744         }
745
746         sc = scsi_host_find_tag(fnic->lport->host, id);
747         WARN_ON_ONCE(!sc);
748         if (!sc) {
749                 shost_printk(KERN_ERR, fnic->lport->host,
750                           "icmnd_cmpl sc is null - "
751                           "hdr status = %s tag = 0x%x desc = 0x%p\n",
752                           fnic_fcpio_status_to_str(hdr_status), id, desc);
753                 FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
754                           fnic->lport->host->host_no, id,
755                           ((u64)icmnd_cmpl->_resvd0[1] << 16 |
756                           (u64)icmnd_cmpl->_resvd0[0]),
757                           ((u64)hdr_status << 16 |
758                           (u64)icmnd_cmpl->scsi_status << 8 |
759                           (u64)icmnd_cmpl->flags), desc,
760                           (u64)icmnd_cmpl->residual, 0);
761                 return;
762         }
763
764         io_lock = fnic_io_lock_hash(fnic, sc);
765         spin_lock_irqsave(io_lock, flags);
766         io_req = (struct fnic_io_req *)CMD_SP(sc);
767         WARN_ON_ONCE(!io_req);
768         if (!io_req) {
769                 CMD_FLAGS(sc) |= FNIC_IO_REQ_NULL;
770                 spin_unlock_irqrestore(io_lock, flags);
771                 shost_printk(KERN_ERR, fnic->lport->host,
772                           "icmnd_cmpl io_req is null - "
773                           "hdr status = %s tag = 0x%x sc 0x%p\n",
774                           fnic_fcpio_status_to_str(hdr_status), id, sc);
775                 return;
776         }
777         start_time = io_req->start_time;
778
779         /* firmware completed the io */
780         io_req->io_completed = 1;
781
782         /*
783          *  if SCSI-ML has already issued abort on this command,
784          * ignore completion of the IO. The abts path will clean it up
785          */
786         if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
787                 spin_unlock_irqrestore(io_lock, flags);
788                 CMD_FLAGS(sc) |= FNIC_IO_ABTS_PENDING;
789                 switch (hdr_status) {
790                 case FCPIO_SUCCESS:
791                         CMD_FLAGS(sc) |= FNIC_IO_DONE;
792                         FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
793                                   "icmnd_cmpl ABTS pending hdr status = %s "
794                                   "sc  0x%p scsi_status %x  residual %d\n",
795                                   fnic_fcpio_status_to_str(hdr_status), sc,
796                                   icmnd_cmpl->scsi_status,
797                                   icmnd_cmpl->residual);
798                         break;
799                 case FCPIO_ABORTED:
800                         CMD_FLAGS(sc) |= FNIC_IO_ABORTED;
801                         break;
802                 default:
803                         FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
804                                           "icmnd_cmpl abts pending "
805                                           "hdr status = %s tag = 0x%x sc = 0x%p\n",
806                                           fnic_fcpio_status_to_str(hdr_status),
807                                           id, sc);
808                         break;
809                 }
810                 return;
811         }
812
813         /* Mark the IO as complete */
814         CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
815
816         icmnd_cmpl = &desc->u.icmnd_cmpl;
817
818         switch (hdr_status) {
819         case FCPIO_SUCCESS:
820                 sc->result = (DID_OK << 16) | icmnd_cmpl->scsi_status;
821                 xfer_len = scsi_bufflen(sc);
822                 scsi_set_resid(sc, icmnd_cmpl->residual);
823
824                 if (icmnd_cmpl->flags & FCPIO_ICMND_CMPL_RESID_UNDER)
825                         xfer_len -= icmnd_cmpl->residual;
826
827                 break;
828
829         case FCPIO_TIMEOUT:          /* request was timed out */
830                 sc->result = (DID_TIME_OUT << 16) | icmnd_cmpl->scsi_status;
831                 break;
832
833         case FCPIO_ABORTED:          /* request was aborted */
834                 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
835                 break;
836
837         case FCPIO_DATA_CNT_MISMATCH: /* recv/sent more/less data than exp. */
838                 scsi_set_resid(sc, icmnd_cmpl->residual);
839                 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
840                 break;
841
842         case FCPIO_OUT_OF_RESOURCE:  /* out of resources to complete request */
843                 sc->result = (DID_REQUEUE << 16) | icmnd_cmpl->scsi_status;
844                 break;
845         case FCPIO_INVALID_HEADER:   /* header contains invalid data */
846         case FCPIO_INVALID_PARAM:    /* some parameter in request invalid */
847         case FCPIO_REQ_NOT_SUPPORTED:/* request type is not supported */
848         case FCPIO_IO_NOT_FOUND:     /* requested I/O was not found */
849         case FCPIO_SGL_INVALID:      /* request was aborted due to sgl error */
850         case FCPIO_MSS_INVALID:      /* request was aborted due to mss error */
851         case FCPIO_FW_ERR:           /* request was terminated due fw error */
852         default:
853                 shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
854                              fnic_fcpio_status_to_str(hdr_status));
855                 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
856                 break;
857         }
858
859         /* Break link with the SCSI command */
860         CMD_SP(sc) = NULL;
861         CMD_FLAGS(sc) |= FNIC_IO_DONE;
862
863         spin_unlock_irqrestore(io_lock, flags);
864
865         fnic_release_ioreq_buf(fnic, io_req, sc);
866
867         mempool_free(io_req, fnic->io_req_pool);
868
869         cmd_trace = ((u64)hdr_status << 56) |
870                   (u64)icmnd_cmpl->scsi_status << 48 |
871                   (u64)icmnd_cmpl->flags << 40 | (u64)sc->cmnd[0] << 32 |
872                   (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
873                   (u64)sc->cmnd[4] << 8 | sc->cmnd[5];
874
875         FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
876                   sc->device->host->host_no, id, sc,
877                   ((u64)icmnd_cmpl->_resvd0[1] << 56 |
878                   (u64)icmnd_cmpl->_resvd0[0] << 48 |
879                   jiffies_to_msecs(jiffies - start_time)),
880                   desc, cmd_trace,
881                   (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
882
883         if (sc->sc_data_direction == DMA_FROM_DEVICE) {
884                 fnic->lport->host_stats.fcp_input_requests++;
885                 fnic->fcp_input_bytes += xfer_len;
886         } else if (sc->sc_data_direction == DMA_TO_DEVICE) {
887                 fnic->lport->host_stats.fcp_output_requests++;
888                 fnic->fcp_output_bytes += xfer_len;
889         } else
890                 fnic->lport->host_stats.fcp_control_requests++;
891
892         /* Call SCSI completion function to complete the IO */
893         if (sc->scsi_done)
894                 sc->scsi_done(sc);
895 }
896
897 /* fnic_fcpio_itmf_cmpl_handler
898  * Routine to handle itmf completions
899  */
900 static void fnic_fcpio_itmf_cmpl_handler(struct fnic *fnic,
901                                         struct fcpio_fw_req *desc)
902 {
903         u8 type;
904         u8 hdr_status;
905         struct fcpio_tag tag;
906         u32 id;
907         struct scsi_cmnd *sc;
908         struct fnic_io_req *io_req;
909         unsigned long flags;
910         spinlock_t *io_lock;
911         unsigned long start_time;
912
913         fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
914         fcpio_tag_id_dec(&tag, &id);
915
916         if ((id & FNIC_TAG_MASK) >= fnic->fnic_max_tag_id) {
917                 shost_printk(KERN_ERR, fnic->lport->host,
918                 "Tag out of range tag %x hdr status = %s\n",
919                 id, fnic_fcpio_status_to_str(hdr_status));
920                 return;
921         }
922
923         sc = scsi_host_find_tag(fnic->lport->host, id & FNIC_TAG_MASK);
924         WARN_ON_ONCE(!sc);
925         if (!sc) {
926                 shost_printk(KERN_ERR, fnic->lport->host,
927                           "itmf_cmpl sc is null - hdr status = %s tag = 0x%x\n",
928                           fnic_fcpio_status_to_str(hdr_status), id);
929                 return;
930         }
931         io_lock = fnic_io_lock_hash(fnic, sc);
932         spin_lock_irqsave(io_lock, flags);
933         io_req = (struct fnic_io_req *)CMD_SP(sc);
934         WARN_ON_ONCE(!io_req);
935         if (!io_req) {
936                 spin_unlock_irqrestore(io_lock, flags);
937                 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
938                 shost_printk(KERN_ERR, fnic->lport->host,
939                           "itmf_cmpl io_req is null - "
940                           "hdr status = %s tag = 0x%x sc 0x%p\n",
941                           fnic_fcpio_status_to_str(hdr_status), id, sc);
942                 return;
943         }
944         start_time = io_req->start_time;
945
946         if ((id & FNIC_TAG_ABORT) && (id & FNIC_TAG_DEV_RST)) {
947                 /* Abort and terminate completion of device reset req */
948                 /* REVISIT : Add asserts about various flags */
949                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
950                               "dev reset abts cmpl recd. id %x status %s\n",
951                               id, fnic_fcpio_status_to_str(hdr_status));
952                 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
953                 CMD_ABTS_STATUS(sc) = hdr_status;
954                 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
955                 if (io_req->abts_done)
956                         complete(io_req->abts_done);
957                 spin_unlock_irqrestore(io_lock, flags);
958         } else if (id & FNIC_TAG_ABORT) {
959                 /* Completion of abort cmd */
960                 if (CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING) {
961                         /* This is a late completion. Ignore it */
962                         spin_unlock_irqrestore(io_lock, flags);
963                         return;
964                 }
965                 CMD_ABTS_STATUS(sc) = hdr_status;
966                 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
967                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
968                               "abts cmpl recd. id %d status %s\n",
969                               (int)(id & FNIC_TAG_MASK),
970                               fnic_fcpio_status_to_str(hdr_status));
971
972                 /*
973                  * If scsi_eh thread is blocked waiting for abts to complete,
974                  * signal completion to it. IO will be cleaned in the thread
975                  * else clean it in this context
976                  */
977                 if (io_req->abts_done) {
978                         complete(io_req->abts_done);
979                         spin_unlock_irqrestore(io_lock, flags);
980                 } else {
981                         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
982                                       "abts cmpl, completing IO\n");
983                         CMD_SP(sc) = NULL;
984                         sc->result = (DID_ERROR << 16);
985
986                         spin_unlock_irqrestore(io_lock, flags);
987
988                         fnic_release_ioreq_buf(fnic, io_req, sc);
989                         mempool_free(io_req, fnic->io_req_pool);
990                         if (sc->scsi_done) {
991                                 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
992                                         sc->device->host->host_no, id,
993                                         sc,
994                                         jiffies_to_msecs(jiffies - start_time),
995                                         desc,
996                                         (((u64)hdr_status << 40) |
997                                         (u64)sc->cmnd[0] << 32 |
998                                         (u64)sc->cmnd[2] << 24 |
999                                         (u64)sc->cmnd[3] << 16 |
1000                                         (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1001                                         (((u64)CMD_FLAGS(sc) << 32) |
1002                                         CMD_STATE(sc)));
1003                                 sc->scsi_done(sc);
1004                         }
1005                 }
1006
1007         } else if (id & FNIC_TAG_DEV_RST) {
1008                 /* Completion of device reset */
1009                 CMD_LR_STATUS(sc) = hdr_status;
1010                 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1011                         spin_unlock_irqrestore(io_lock, flags);
1012                         CMD_FLAGS(sc) |= FNIC_DEV_RST_ABTS_PENDING;
1013                         FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1014                                   sc->device->host->host_no, id, sc,
1015                                   jiffies_to_msecs(jiffies - start_time),
1016                                   desc, 0,
1017                                   (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1018                         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1019                                 "Terminate pending "
1020                                 "dev reset cmpl recd. id %d status %s\n",
1021                                 (int)(id & FNIC_TAG_MASK),
1022                                 fnic_fcpio_status_to_str(hdr_status));
1023                         return;
1024                 }
1025                 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TIMED_OUT) {
1026                         /* Need to wait for terminate completion */
1027                         spin_unlock_irqrestore(io_lock, flags);
1028                         FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1029                                   sc->device->host->host_no, id, sc,
1030                                   jiffies_to_msecs(jiffies - start_time),
1031                                   desc, 0,
1032                                   (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1033                         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1034                                 "dev reset cmpl recd after time out. "
1035                                 "id %d status %s\n",
1036                                 (int)(id & FNIC_TAG_MASK),
1037                                 fnic_fcpio_status_to_str(hdr_status));
1038                         return;
1039                 }
1040                 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
1041                 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1042                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1043                               "dev reset cmpl recd. id %d status %s\n",
1044                               (int)(id & FNIC_TAG_MASK),
1045                               fnic_fcpio_status_to_str(hdr_status));
1046                 if (io_req->dr_done)
1047                         complete(io_req->dr_done);
1048                 spin_unlock_irqrestore(io_lock, flags);
1049
1050         } else {
1051                 shost_printk(KERN_ERR, fnic->lport->host,
1052                              "Unexpected itmf io state %s tag %x\n",
1053                              fnic_ioreq_state_to_str(CMD_STATE(sc)), id);
1054                 spin_unlock_irqrestore(io_lock, flags);
1055         }
1056
1057 }
1058
1059 /*
1060  * fnic_fcpio_cmpl_handler
1061  * Routine to service the cq for wq_copy
1062  */
1063 static int fnic_fcpio_cmpl_handler(struct vnic_dev *vdev,
1064                                    unsigned int cq_index,
1065                                    struct fcpio_fw_req *desc)
1066 {
1067         struct fnic *fnic = vnic_dev_priv(vdev);
1068
1069         switch (desc->hdr.type) {
1070         case FCPIO_ACK: /* fw copied copy wq desc to its queue */
1071                 fnic_fcpio_ack_handler(fnic, cq_index, desc);
1072                 break;
1073
1074         case FCPIO_ICMND_CMPL: /* fw completed a command */
1075                 fnic_fcpio_icmnd_cmpl_handler(fnic, desc);
1076                 break;
1077
1078         case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1079                 fnic_fcpio_itmf_cmpl_handler(fnic, desc);
1080                 break;
1081
1082         case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
1083         case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
1084                 fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
1085                 break;
1086
1087         case FCPIO_RESET_CMPL: /* fw completed reset */
1088                 fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
1089                 break;
1090
1091         default:
1092                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1093                               "firmware completion type %d\n",
1094                               desc->hdr.type);
1095                 break;
1096         }
1097
1098         return 0;
1099 }
1100
1101 /*
1102  * fnic_wq_copy_cmpl_handler
1103  * Routine to process wq copy
1104  */
1105 int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
1106 {
1107         unsigned int wq_work_done = 0;
1108         unsigned int i, cq_index;
1109         unsigned int cur_work_done;
1110
1111         for (i = 0; i < fnic->wq_copy_count; i++) {
1112                 cq_index = i + fnic->raw_wq_count + fnic->rq_count;
1113                 cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
1114                                                      fnic_fcpio_cmpl_handler,
1115                                                      copy_work_to_do);
1116                 wq_work_done += cur_work_done;
1117         }
1118         return wq_work_done;
1119 }
1120
1121 static void fnic_cleanup_io(struct fnic *fnic, int exclude_id)
1122 {
1123         int i;
1124         struct fnic_io_req *io_req;
1125         unsigned long flags = 0;
1126         struct scsi_cmnd *sc;
1127         spinlock_t *io_lock;
1128         unsigned long start_time = 0;
1129
1130         for (i = 0; i < fnic->fnic_max_tag_id; i++) {
1131                 if (i == exclude_id)
1132                         continue;
1133
1134                 io_lock = fnic_io_lock_tag(fnic, i);
1135                 spin_lock_irqsave(io_lock, flags);
1136                 sc = scsi_host_find_tag(fnic->lport->host, i);
1137                 if (!sc) {
1138                         spin_unlock_irqrestore(io_lock, flags);
1139                         continue;
1140                 }
1141
1142                 io_req = (struct fnic_io_req *)CMD_SP(sc);
1143                 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1144                         !(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
1145                         /*
1146                          * We will be here only when FW completes reset
1147                          * without sending completions for outstanding ios.
1148                          */
1149                         CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1150                         if (io_req && io_req->dr_done)
1151                                 complete(io_req->dr_done);
1152                         else if (io_req && io_req->abts_done)
1153                                 complete(io_req->abts_done);
1154                         spin_unlock_irqrestore(io_lock, flags);
1155                         continue;
1156                 } else if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1157                         spin_unlock_irqrestore(io_lock, flags);
1158                         continue;
1159                 }
1160                 if (!io_req) {
1161                         spin_unlock_irqrestore(io_lock, flags);
1162                         goto cleanup_scsi_cmd;
1163                 }
1164
1165                 CMD_SP(sc) = NULL;
1166
1167                 spin_unlock_irqrestore(io_lock, flags);
1168
1169                 /*
1170                  * If there is a scsi_cmnd associated with this io_req, then
1171                  * free the corresponding state
1172                  */
1173                 start_time = io_req->start_time;
1174                 fnic_release_ioreq_buf(fnic, io_req, sc);
1175                 mempool_free(io_req, fnic->io_req_pool);
1176
1177 cleanup_scsi_cmd:
1178                 sc->result = DID_TRANSPORT_DISRUPTED << 16;
1179                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "fnic_cleanup_io:"
1180                               " DID_TRANSPORT_DISRUPTED\n");
1181
1182                 /* Complete the command to SCSI */
1183                 if (sc->scsi_done) {
1184                         FNIC_TRACE(fnic_cleanup_io,
1185                                   sc->device->host->host_no, i, sc,
1186                                   jiffies_to_msecs(jiffies - start_time),
1187                                   0, ((u64)sc->cmnd[0] << 32 |
1188                                   (u64)sc->cmnd[2] << 24 |
1189                                   (u64)sc->cmnd[3] << 16 |
1190                                   (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1191                                   (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1192
1193                         sc->scsi_done(sc);
1194                 }
1195         }
1196 }
1197
1198 void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
1199                                   struct fcpio_host_req *desc)
1200 {
1201         u32 id;
1202         struct fnic *fnic = vnic_dev_priv(wq->vdev);
1203         struct fnic_io_req *io_req;
1204         struct scsi_cmnd *sc;
1205         unsigned long flags;
1206         spinlock_t *io_lock;
1207         unsigned long start_time = 0;
1208
1209         /* get the tag reference */
1210         fcpio_tag_id_dec(&desc->hdr.tag, &id);
1211         id &= FNIC_TAG_MASK;
1212
1213         if (id >= fnic->fnic_max_tag_id)
1214                 return;
1215
1216         sc = scsi_host_find_tag(fnic->lport->host, id);
1217         if (!sc)
1218                 return;
1219
1220         io_lock = fnic_io_lock_hash(fnic, sc);
1221         spin_lock_irqsave(io_lock, flags);
1222
1223         /* Get the IO context which this desc refers to */
1224         io_req = (struct fnic_io_req *)CMD_SP(sc);
1225
1226         /* fnic interrupts are turned off by now */
1227
1228         if (!io_req) {
1229                 spin_unlock_irqrestore(io_lock, flags);
1230                 goto wq_copy_cleanup_scsi_cmd;
1231         }
1232
1233         CMD_SP(sc) = NULL;
1234
1235         spin_unlock_irqrestore(io_lock, flags);
1236
1237         start_time = io_req->start_time;
1238         fnic_release_ioreq_buf(fnic, io_req, sc);
1239         mempool_free(io_req, fnic->io_req_pool);
1240
1241 wq_copy_cleanup_scsi_cmd:
1242         sc->result = DID_NO_CONNECT << 16;
1243         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
1244                       " DID_NO_CONNECT\n");
1245
1246         if (sc->scsi_done) {
1247                 FNIC_TRACE(fnic_wq_copy_cleanup_handler,
1248                           sc->device->host->host_no, id, sc,
1249                           jiffies_to_msecs(jiffies - start_time),
1250                           0, ((u64)sc->cmnd[0] << 32 |
1251                           (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1252                           (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1253                           (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1254
1255                 sc->scsi_done(sc);
1256         }
1257 }
1258
1259 static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
1260                                           u32 task_req, u8 *fc_lun,
1261                                           struct fnic_io_req *io_req)
1262 {
1263         struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1264         struct Scsi_Host *host = fnic->lport->host;
1265         unsigned long flags;
1266
1267         spin_lock_irqsave(host->host_lock, flags);
1268         if (unlikely(fnic_chk_state_flags_locked(fnic,
1269                                                 FNIC_FLAGS_IO_BLOCKED))) {
1270                 spin_unlock_irqrestore(host->host_lock, flags);
1271                 return 1;
1272         } else
1273                 atomic_inc(&fnic->in_flight);
1274         spin_unlock_irqrestore(host->host_lock, flags);
1275
1276         spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
1277
1278         if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1279                 free_wq_copy_descs(fnic, wq);
1280
1281         if (!vnic_wq_copy_desc_avail(wq)) {
1282                 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1283                 atomic_dec(&fnic->in_flight);
1284                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1285                         "fnic_queue_abort_io_req: failure: no descriptors\n");
1286                 return 1;
1287         }
1288         fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
1289                                      0, task_req, tag, fc_lun, io_req->port_id,
1290                                      fnic->config.ra_tov, fnic->config.ed_tov);
1291
1292         spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1293         atomic_dec(&fnic->in_flight);
1294
1295         return 0;
1296 }
1297
1298 static void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
1299 {
1300         int tag;
1301         int abt_tag;
1302         struct fnic_io_req *io_req;
1303         spinlock_t *io_lock;
1304         unsigned long flags;
1305         struct scsi_cmnd *sc;
1306         struct scsi_lun fc_lun;
1307         enum fnic_ioreq_state old_ioreq_state;
1308
1309         FNIC_SCSI_DBG(KERN_DEBUG,
1310                       fnic->lport->host,
1311                       "fnic_rport_exch_reset called portid 0x%06x\n",
1312                       port_id);
1313
1314         if (fnic->in_remove)
1315                 return;
1316
1317         for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
1318                 abt_tag = tag;
1319                 io_lock = fnic_io_lock_tag(fnic, tag);
1320                 spin_lock_irqsave(io_lock, flags);
1321                 sc = scsi_host_find_tag(fnic->lport->host, tag);
1322                 if (!sc) {
1323                         spin_unlock_irqrestore(io_lock, flags);
1324                         continue;
1325                 }
1326
1327                 io_req = (struct fnic_io_req *)CMD_SP(sc);
1328
1329                 if (!io_req || io_req->port_id != port_id) {
1330                         spin_unlock_irqrestore(io_lock, flags);
1331                         continue;
1332                 }
1333
1334                 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1335                         (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
1336                         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1337                         "fnic_rport_exch_reset dev rst not pending sc 0x%p\n",
1338                         sc);
1339                         spin_unlock_irqrestore(io_lock, flags);
1340                         continue;
1341                 }
1342
1343                 /*
1344                  * Found IO that is still pending with firmware and
1345                  * belongs to rport that went away
1346                  */
1347                 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1348                         spin_unlock_irqrestore(io_lock, flags);
1349                         continue;
1350                 }
1351                 if (io_req->abts_done) {
1352                         shost_printk(KERN_ERR, fnic->lport->host,
1353                         "fnic_rport_exch_reset: io_req->abts_done is set "
1354                         "state is %s\n",
1355                         fnic_ioreq_state_to_str(CMD_STATE(sc)));
1356                 }
1357
1358                 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1359                         shost_printk(KERN_ERR, fnic->lport->host,
1360                                   "rport_exch_reset "
1361                                   "IO not yet issued %p tag 0x%x flags "
1362                                   "%x state %d\n",
1363                                   sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1364                 }
1365                 old_ioreq_state = CMD_STATE(sc);
1366                 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1367                 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1368                 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1369                         abt_tag = (tag | FNIC_TAG_DEV_RST);
1370                         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1371                         "fnic_rport_exch_reset dev rst sc 0x%p\n",
1372                         sc);
1373                 }
1374
1375                 BUG_ON(io_req->abts_done);
1376
1377                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1378                               "fnic_rport_reset_exch: Issuing abts\n");
1379
1380                 spin_unlock_irqrestore(io_lock, flags);
1381
1382                 /* Now queue the abort command to firmware */
1383                 int_to_scsilun(sc->device->lun, &fc_lun);
1384
1385                 if (fnic_queue_abort_io_req(fnic, abt_tag,
1386                                             FCPIO_ITMF_ABT_TASK_TERM,
1387                                             fc_lun.scsi_lun, io_req)) {
1388                         /*
1389                          * Revert the cmd state back to old state, if
1390                          * it hasn't changed in between. This cmd will get
1391                          * aborted later by scsi_eh, or cleaned up during
1392                          * lun reset
1393                          */
1394                         spin_lock_irqsave(io_lock, flags);
1395                         if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1396                                 CMD_STATE(sc) = old_ioreq_state;
1397                         spin_unlock_irqrestore(io_lock, flags);
1398                 } else {
1399                         spin_lock_irqsave(io_lock, flags);
1400                         if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1401                                 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
1402                         else
1403                                 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
1404                         spin_unlock_irqrestore(io_lock, flags);
1405                 }
1406         }
1407
1408 }
1409
1410 void fnic_terminate_rport_io(struct fc_rport *rport)
1411 {
1412         int tag;
1413         int abt_tag;
1414         struct fnic_io_req *io_req;
1415         spinlock_t *io_lock;
1416         unsigned long flags;
1417         struct scsi_cmnd *sc;
1418         struct scsi_lun fc_lun;
1419         struct fc_rport_libfc_priv *rdata;
1420         struct fc_lport *lport;
1421         struct fnic *fnic;
1422         struct fc_rport *cmd_rport;
1423         enum fnic_ioreq_state old_ioreq_state;
1424
1425         if (!rport) {
1426                 printk(KERN_ERR "fnic_terminate_rport_io: rport is NULL\n");
1427                 return;
1428         }
1429         rdata = rport->dd_data;
1430
1431         if (!rdata) {
1432                 printk(KERN_ERR "fnic_terminate_rport_io: rdata is NULL\n");
1433                 return;
1434         }
1435         lport = rdata->local_port;
1436
1437         if (!lport) {
1438                 printk(KERN_ERR "fnic_terminate_rport_io: lport is NULL\n");
1439                 return;
1440         }
1441         fnic = lport_priv(lport);
1442         FNIC_SCSI_DBG(KERN_DEBUG,
1443                       fnic->lport->host, "fnic_terminate_rport_io called"
1444                       " wwpn 0x%llx, wwnn0x%llx, rport 0x%p, portid 0x%06x\n",
1445                       rport->port_name, rport->node_name, rport,
1446                       rport->port_id);
1447
1448         if (fnic->in_remove)
1449                 return;
1450
1451         for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
1452                 abt_tag = tag;
1453                 io_lock = fnic_io_lock_tag(fnic, tag);
1454                 spin_lock_irqsave(io_lock, flags);
1455                 sc = scsi_host_find_tag(fnic->lport->host, tag);
1456                 if (!sc) {
1457                         spin_unlock_irqrestore(io_lock, flags);
1458                         continue;
1459                 }
1460
1461                 cmd_rport = starget_to_rport(scsi_target(sc->device));
1462                 if (rport != cmd_rport) {
1463                         spin_unlock_irqrestore(io_lock, flags);
1464                         continue;
1465                 }
1466
1467                 io_req = (struct fnic_io_req *)CMD_SP(sc);
1468
1469                 if (!io_req || rport != cmd_rport) {
1470                         spin_unlock_irqrestore(io_lock, flags);
1471                         continue;
1472                 }
1473
1474                 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1475                         (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
1476                         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1477                         "fnic_terminate_rport_io dev rst not pending sc 0x%p\n",
1478                         sc);
1479                         spin_unlock_irqrestore(io_lock, flags);
1480                         continue;
1481                 }
1482                 /*
1483                  * Found IO that is still pending with firmware and
1484                  * belongs to rport that went away
1485                  */
1486                 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1487                         spin_unlock_irqrestore(io_lock, flags);
1488                         continue;
1489                 }
1490                 if (io_req->abts_done) {
1491                         shost_printk(KERN_ERR, fnic->lport->host,
1492                         "fnic_terminate_rport_io: io_req->abts_done is set "
1493                         "state is %s\n",
1494                         fnic_ioreq_state_to_str(CMD_STATE(sc)));
1495                 }
1496                 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1497                         FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1498                                   "fnic_terminate_rport_io "
1499                                   "IO not yet issued %p tag 0x%x flags "
1500                                   "%x state %d\n",
1501                                   sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1502                 }
1503                 old_ioreq_state = CMD_STATE(sc);
1504                 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1505                 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1506                 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1507                         abt_tag = (tag | FNIC_TAG_DEV_RST);
1508                         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1509                         "fnic_terminate_rport_io dev rst sc 0x%p\n", sc);
1510                 }
1511
1512                 BUG_ON(io_req->abts_done);
1513
1514                 FNIC_SCSI_DBG(KERN_DEBUG,
1515                               fnic->lport->host,
1516                               "fnic_terminate_rport_io: Issuing abts\n");
1517
1518                 spin_unlock_irqrestore(io_lock, flags);
1519
1520                 /* Now queue the abort command to firmware */
1521                 int_to_scsilun(sc->device->lun, &fc_lun);
1522
1523                 if (fnic_queue_abort_io_req(fnic, abt_tag,
1524                                             FCPIO_ITMF_ABT_TASK_TERM,
1525                                             fc_lun.scsi_lun, io_req)) {
1526                         /*
1527                          * Revert the cmd state back to old state, if
1528                          * it hasn't changed in between. This cmd will get
1529                          * aborted later by scsi_eh, or cleaned up during
1530                          * lun reset
1531                          */
1532                         spin_lock_irqsave(io_lock, flags);
1533                         if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1534                                 CMD_STATE(sc) = old_ioreq_state;
1535                         spin_unlock_irqrestore(io_lock, flags);
1536                 } else {
1537                         spin_lock_irqsave(io_lock, flags);
1538                         if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1539                                 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
1540                         else
1541                                 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
1542                         spin_unlock_irqrestore(io_lock, flags);
1543                 }
1544         }
1545
1546 }
1547
1548 /*
1549  * This function is exported to SCSI for sending abort cmnds.
1550  * A SCSI IO is represented by a io_req in the driver.
1551  * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
1552  */
1553 int fnic_abort_cmd(struct scsi_cmnd *sc)
1554 {
1555         struct fc_lport *lp;
1556         struct fnic *fnic;
1557         struct fnic_io_req *io_req = NULL;
1558         struct fc_rport *rport;
1559         spinlock_t *io_lock;
1560         unsigned long flags;
1561         unsigned long start_time = 0;
1562         int ret = SUCCESS;
1563         u32 task_req = 0;
1564         struct scsi_lun fc_lun;
1565         int tag;
1566         DECLARE_COMPLETION_ONSTACK(tm_done);
1567
1568         /* Wait for rport to unblock */
1569         fc_block_scsi_eh(sc);
1570
1571         /* Get local-port, check ready and link up */
1572         lp = shost_priv(sc->device->host);
1573
1574         fnic = lport_priv(lp);
1575         rport = starget_to_rport(scsi_target(sc->device));
1576         tag = sc->request->tag;
1577         FNIC_SCSI_DBG(KERN_DEBUG,
1578                 fnic->lport->host,
1579                 "Abort Cmd called FCID 0x%x, LUN 0x%x TAG %x flags %x\n",
1580                 rport->port_id, sc->device->lun, tag, CMD_FLAGS(sc));
1581
1582         CMD_FLAGS(sc) = FNIC_NO_FLAGS;
1583
1584         if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
1585                 ret = FAILED;
1586                 goto fnic_abort_cmd_end;
1587         }
1588
1589         /*
1590          * Avoid a race between SCSI issuing the abort and the device
1591          * completing the command.
1592          *
1593          * If the command is already completed by the fw cmpl code,
1594          * we just return SUCCESS from here. This means that the abort
1595          * succeeded. In the SCSI ML, since the timeout for command has
1596          * happened, the completion wont actually complete the command
1597          * and it will be considered as an aborted command
1598          *
1599          * The CMD_SP will not be cleared except while holding io_req_lock.
1600          */
1601         io_lock = fnic_io_lock_hash(fnic, sc);
1602         spin_lock_irqsave(io_lock, flags);
1603         io_req = (struct fnic_io_req *)CMD_SP(sc);
1604         if (!io_req) {
1605                 spin_unlock_irqrestore(io_lock, flags);
1606                 goto fnic_abort_cmd_end;
1607         }
1608
1609         io_req->abts_done = &tm_done;
1610
1611         if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1612                 spin_unlock_irqrestore(io_lock, flags);
1613                 goto wait_pending;
1614         }
1615         /*
1616          * Command is still pending, need to abort it
1617          * If the firmware completes the command after this point,
1618          * the completion wont be done till mid-layer, since abort
1619          * has already started.
1620          */
1621         CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1622         CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1623
1624         spin_unlock_irqrestore(io_lock, flags);
1625
1626         /*
1627          * Check readiness of the remote port. If the path to remote
1628          * port is up, then send abts to the remote port to terminate
1629          * the IO. Else, just locally terminate the IO in the firmware
1630          */
1631         if (fc_remote_port_chkready(rport) == 0)
1632                 task_req = FCPIO_ITMF_ABT_TASK;
1633         else
1634                 task_req = FCPIO_ITMF_ABT_TASK_TERM;
1635
1636         /* Now queue the abort command to firmware */
1637         int_to_scsilun(sc->device->lun, &fc_lun);
1638
1639         if (fnic_queue_abort_io_req(fnic, sc->request->tag, task_req,
1640                                     fc_lun.scsi_lun, io_req)) {
1641                 spin_lock_irqsave(io_lock, flags);
1642                 io_req = (struct fnic_io_req *)CMD_SP(sc);
1643                 if (io_req)
1644                         io_req->abts_done = NULL;
1645                 spin_unlock_irqrestore(io_lock, flags);
1646                 ret = FAILED;
1647                 goto fnic_abort_cmd_end;
1648         }
1649         if (task_req == FCPIO_ITMF_ABT_TASK)
1650                 CMD_FLAGS(sc) |= FNIC_IO_ABTS_ISSUED;
1651         else
1652                 CMD_FLAGS(sc) |= FNIC_IO_TERM_ISSUED;
1653
1654         /*
1655          * We queued an abort IO, wait for its completion.
1656          * Once the firmware completes the abort command, it will
1657          * wake up this thread.
1658          */
1659  wait_pending:
1660         wait_for_completion_timeout(&tm_done,
1661                                     msecs_to_jiffies
1662                                     (2 * fnic->config.ra_tov +
1663                                      fnic->config.ed_tov));
1664
1665         /* Check the abort status */
1666         spin_lock_irqsave(io_lock, flags);
1667
1668         io_req = (struct fnic_io_req *)CMD_SP(sc);
1669         if (!io_req) {
1670                 spin_unlock_irqrestore(io_lock, flags);
1671                 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
1672                 ret = FAILED;
1673                 goto fnic_abort_cmd_end;
1674         }
1675         io_req->abts_done = NULL;
1676
1677         /* fw did not complete abort, timed out */
1678         if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
1679                 spin_unlock_irqrestore(io_lock, flags);
1680                 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_TIMED_OUT;
1681                 ret = FAILED;
1682                 goto fnic_abort_cmd_end;
1683         }
1684
1685         CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1686
1687         /*
1688          * firmware completed the abort, check the status,
1689          * free the io_req irrespective of failure or success
1690          */
1691         if (CMD_ABTS_STATUS(sc) != FCPIO_SUCCESS)
1692                 ret = FAILED;
1693
1694         CMD_SP(sc) = NULL;
1695
1696         spin_unlock_irqrestore(io_lock, flags);
1697
1698         start_time = io_req->start_time;
1699         fnic_release_ioreq_buf(fnic, io_req, sc);
1700         mempool_free(io_req, fnic->io_req_pool);
1701
1702 fnic_abort_cmd_end:
1703         FNIC_TRACE(fnic_abort_cmd, sc->device->host->host_no,
1704                   sc->request->tag, sc,
1705                   jiffies_to_msecs(jiffies - start_time),
1706                   0, ((u64)sc->cmnd[0] << 32 |
1707                   (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1708                   (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1709                   (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1710
1711         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1712                       "Returning from abort cmd type %x %s\n", task_req,
1713                       (ret == SUCCESS) ?
1714                       "SUCCESS" : "FAILED");
1715         return ret;
1716 }
1717
1718 static inline int fnic_queue_dr_io_req(struct fnic *fnic,
1719                                        struct scsi_cmnd *sc,
1720                                        struct fnic_io_req *io_req)
1721 {
1722         struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1723         struct Scsi_Host *host = fnic->lport->host;
1724         struct scsi_lun fc_lun;
1725         int ret = 0;
1726         unsigned long intr_flags;
1727
1728         spin_lock_irqsave(host->host_lock, intr_flags);
1729         if (unlikely(fnic_chk_state_flags_locked(fnic,
1730                                                 FNIC_FLAGS_IO_BLOCKED))) {
1731                 spin_unlock_irqrestore(host->host_lock, intr_flags);
1732                 return FAILED;
1733         } else
1734                 atomic_inc(&fnic->in_flight);
1735         spin_unlock_irqrestore(host->host_lock, intr_flags);
1736
1737         spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
1738
1739         if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1740                 free_wq_copy_descs(fnic, wq);
1741
1742         if (!vnic_wq_copy_desc_avail(wq)) {
1743                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1744                           "queue_dr_io_req failure - no descriptors\n");
1745                 ret = -EAGAIN;
1746                 goto lr_io_req_end;
1747         }
1748
1749         /* fill in the lun info */
1750         int_to_scsilun(sc->device->lun, &fc_lun);
1751
1752         fnic_queue_wq_copy_desc_itmf(wq, sc->request->tag | FNIC_TAG_DEV_RST,
1753                                      0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
1754                                      fc_lun.scsi_lun, io_req->port_id,
1755                                      fnic->config.ra_tov, fnic->config.ed_tov);
1756
1757 lr_io_req_end:
1758         spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
1759         atomic_dec(&fnic->in_flight);
1760
1761         return ret;
1762 }
1763
1764 /*
1765  * Clean up any pending aborts on the lun
1766  * For each outstanding IO on this lun, whose abort is not completed by fw,
1767  * issue a local abort. Wait for abort to complete. Return 0 if all commands
1768  * successfully aborted, 1 otherwise
1769  */
1770 static int fnic_clean_pending_aborts(struct fnic *fnic,
1771                                      struct scsi_cmnd *lr_sc)
1772 {
1773         int tag, abt_tag;
1774         struct fnic_io_req *io_req;
1775         spinlock_t *io_lock;
1776         unsigned long flags;
1777         int ret = 0;
1778         struct scsi_cmnd *sc;
1779         struct scsi_lun fc_lun;
1780         struct scsi_device *lun_dev = lr_sc->device;
1781         DECLARE_COMPLETION_ONSTACK(tm_done);
1782         enum fnic_ioreq_state old_ioreq_state;
1783
1784         for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
1785                 io_lock = fnic_io_lock_tag(fnic, tag);
1786                 spin_lock_irqsave(io_lock, flags);
1787                 sc = scsi_host_find_tag(fnic->lport->host, tag);
1788                 /*
1789                  * ignore this lun reset cmd or cmds that do not belong to
1790                  * this lun
1791                  */
1792                 if (!sc || sc == lr_sc || sc->device != lun_dev) {
1793                         spin_unlock_irqrestore(io_lock, flags);
1794                         continue;
1795                 }
1796
1797                 io_req = (struct fnic_io_req *)CMD_SP(sc);
1798
1799                 if (!io_req || sc->device != lun_dev) {
1800                         spin_unlock_irqrestore(io_lock, flags);
1801                         continue;
1802                 }
1803
1804                 /*
1805                  * Found IO that is still pending with firmware and
1806                  * belongs to the LUN that we are resetting
1807                  */
1808                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1809                               "Found IO in %s on lun\n",
1810                               fnic_ioreq_state_to_str(CMD_STATE(sc)));
1811
1812                 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1813                         spin_unlock_irqrestore(io_lock, flags);
1814                         continue;
1815                 }
1816                 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1817                         (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
1818                         FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1819                                 "%s dev rst not pending sc 0x%p\n", __func__,
1820                                 sc);
1821                         spin_unlock_irqrestore(io_lock, flags);
1822                         continue;
1823                 }
1824
1825                 if (io_req->abts_done)
1826                         shost_printk(KERN_ERR, fnic->lport->host,
1827                           "%s: io_req->abts_done is set state is %s\n",
1828                           __func__, fnic_ioreq_state_to_str(CMD_STATE(sc)));
1829                 old_ioreq_state = CMD_STATE(sc);
1830                 /*
1831                  * Any pending IO issued prior to reset is expected to be
1832                  * in abts pending state, if not we need to set
1833                  * FNIC_IOREQ_ABTS_PENDING to indicate the IO is abort pending.
1834                  * When IO is completed, the IO will be handed over and
1835                  * handled in this function.
1836                  */
1837                 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1838
1839                 BUG_ON(io_req->abts_done);
1840
1841                 abt_tag = tag;
1842                 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1843                         abt_tag |= FNIC_TAG_DEV_RST;
1844                         FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1845                                   "%s: dev rst sc 0x%p\n", __func__, sc);
1846                 }
1847
1848                 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1849                 io_req->abts_done = &tm_done;
1850                 spin_unlock_irqrestore(io_lock, flags);
1851
1852                 /* Now queue the abort command to firmware */
1853                 int_to_scsilun(sc->device->lun, &fc_lun);
1854
1855                 if (fnic_queue_abort_io_req(fnic, abt_tag,
1856                                             FCPIO_ITMF_ABT_TASK_TERM,
1857                                             fc_lun.scsi_lun, io_req)) {
1858                         spin_lock_irqsave(io_lock, flags);
1859                         io_req = (struct fnic_io_req *)CMD_SP(sc);
1860                         if (io_req)
1861                                 io_req->abts_done = NULL;
1862                         if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1863                                 CMD_STATE(sc) = old_ioreq_state;
1864                         spin_unlock_irqrestore(io_lock, flags);
1865                         ret = 1;
1866                         goto clean_pending_aborts_end;
1867                 } else {
1868                         spin_lock_irqsave(io_lock, flags);
1869                         if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1870                                 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
1871                         spin_unlock_irqrestore(io_lock, flags);
1872                 }
1873                 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
1874
1875                 wait_for_completion_timeout(&tm_done,
1876                                             msecs_to_jiffies
1877                                             (fnic->config.ed_tov));
1878
1879                 /* Recheck cmd state to check if it is now aborted */
1880                 spin_lock_irqsave(io_lock, flags);
1881                 io_req = (struct fnic_io_req *)CMD_SP(sc);
1882                 if (!io_req) {
1883                         spin_unlock_irqrestore(io_lock, flags);
1884                         CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
1885                         continue;
1886                 }
1887
1888                 io_req->abts_done = NULL;
1889
1890                 /* if abort is still pending with fw, fail */
1891                 if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
1892                         spin_unlock_irqrestore(io_lock, flags);
1893                         CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
1894                         ret = 1;
1895                         goto clean_pending_aborts_end;
1896                 }
1897                 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1898                 CMD_SP(sc) = NULL;
1899                 spin_unlock_irqrestore(io_lock, flags);
1900
1901                 fnic_release_ioreq_buf(fnic, io_req, sc);
1902                 mempool_free(io_req, fnic->io_req_pool);
1903         }
1904
1905         schedule_timeout(msecs_to_jiffies(2 * fnic->config.ed_tov));
1906
1907         /* walk again to check, if IOs are still pending in fw */
1908         if (fnic_is_abts_pending(fnic, lr_sc))
1909                 ret = FAILED;
1910
1911 clean_pending_aborts_end:
1912         return ret;
1913 }
1914
1915 /**
1916  * fnic_scsi_host_start_tag
1917  * Allocates tagid from host's tag list
1918  **/
1919 static inline int
1920 fnic_scsi_host_start_tag(struct fnic *fnic, struct scsi_cmnd *sc)
1921 {
1922         struct blk_queue_tag *bqt = fnic->lport->host->bqt;
1923         int tag, ret = SCSI_NO_TAG;
1924
1925         BUG_ON(!bqt);
1926         if (!bqt) {
1927                 pr_err("Tags are not supported\n");
1928                 goto end;
1929         }
1930
1931         do {
1932                 tag = find_next_zero_bit(bqt->tag_map, bqt->max_depth, 1);
1933                 if (tag >= bqt->max_depth) {
1934                         pr_err("Tag allocation failure\n");
1935                         goto end;
1936                 }
1937         } while (test_and_set_bit(tag, bqt->tag_map));
1938
1939         bqt->tag_index[tag] = sc->request;
1940         sc->request->tag = tag;
1941         sc->tag = tag;
1942         if (!sc->request->special)
1943                 sc->request->special = sc;
1944
1945         ret = tag;
1946
1947 end:
1948         return ret;
1949 }
1950
1951 /**
1952  * fnic_scsi_host_end_tag
1953  * frees tag allocated by fnic_scsi_host_start_tag.
1954  **/
1955 static inline void
1956 fnic_scsi_host_end_tag(struct fnic *fnic, struct scsi_cmnd *sc)
1957 {
1958         struct blk_queue_tag *bqt = fnic->lport->host->bqt;
1959         int tag = sc->request->tag;
1960
1961         if (tag == SCSI_NO_TAG)
1962                 return;
1963
1964         BUG_ON(!bqt || !bqt->tag_index[tag]);
1965         if (!bqt)
1966                 return;
1967
1968         bqt->tag_index[tag] = NULL;
1969         clear_bit(tag, bqt->tag_map);
1970
1971         return;
1972 }
1973
1974 /*
1975  * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
1976  * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
1977  * on the LUN.
1978  */
1979 int fnic_device_reset(struct scsi_cmnd *sc)
1980 {
1981         struct fc_lport *lp;
1982         struct fnic *fnic;
1983         struct fnic_io_req *io_req = NULL;
1984         struct fc_rport *rport;
1985         int status;
1986         int ret = FAILED;
1987         spinlock_t *io_lock;
1988         unsigned long flags;
1989         unsigned long start_time = 0;
1990         struct scsi_lun fc_lun;
1991         int tag = 0;
1992         DECLARE_COMPLETION_ONSTACK(tm_done);
1993         int tag_gen_flag = 0;   /*to track tags allocated by fnic driver*/
1994
1995         /* Wait for rport to unblock */
1996         fc_block_scsi_eh(sc);
1997
1998         /* Get local-port, check ready and link up */
1999         lp = shost_priv(sc->device->host);
2000
2001         fnic = lport_priv(lp);
2002
2003         rport = starget_to_rport(scsi_target(sc->device));
2004         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2005                       "Device reset called FCID 0x%x, LUN 0x%x sc 0x%p\n",
2006                       rport->port_id, sc->device->lun, sc);
2007
2008         if (lp->state != LPORT_ST_READY || !(lp->link_up))
2009                 goto fnic_device_reset_end;
2010
2011         /* Check if remote port up */
2012         if (fc_remote_port_chkready(rport))
2013                 goto fnic_device_reset_end;
2014
2015         CMD_FLAGS(sc) = FNIC_DEVICE_RESET;
2016         /* Allocate tag if not present */
2017
2018         tag = sc->request->tag;
2019         if (unlikely(tag < 0)) {
2020                 tag = fnic_scsi_host_start_tag(fnic, sc);
2021                 if (unlikely(tag == SCSI_NO_TAG))
2022                         goto fnic_device_reset_end;
2023                 tag_gen_flag = 1;
2024         }
2025         io_lock = fnic_io_lock_hash(fnic, sc);
2026         spin_lock_irqsave(io_lock, flags);
2027         io_req = (struct fnic_io_req *)CMD_SP(sc);
2028
2029         /*
2030          * If there is a io_req attached to this command, then use it,
2031          * else allocate a new one.
2032          */
2033         if (!io_req) {
2034                 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
2035                 if (!io_req) {
2036                         spin_unlock_irqrestore(io_lock, flags);
2037                         goto fnic_device_reset_end;
2038                 }
2039                 memset(io_req, 0, sizeof(*io_req));
2040                 io_req->port_id = rport->port_id;
2041                 CMD_SP(sc) = (char *)io_req;
2042         }
2043         io_req->dr_done = &tm_done;
2044         CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
2045         CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
2046         spin_unlock_irqrestore(io_lock, flags);
2047
2048         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %x\n", tag);
2049
2050         /*
2051          * issue the device reset, if enqueue failed, clean up the ioreq
2052          * and break assoc with scsi cmd
2053          */
2054         if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
2055                 spin_lock_irqsave(io_lock, flags);
2056                 io_req = (struct fnic_io_req *)CMD_SP(sc);
2057                 if (io_req)
2058                         io_req->dr_done = NULL;
2059                 goto fnic_device_reset_clean;
2060         }
2061         spin_lock_irqsave(io_lock, flags);
2062         CMD_FLAGS(sc) |= FNIC_DEV_RST_ISSUED;
2063         spin_unlock_irqrestore(io_lock, flags);
2064
2065         /*
2066          * Wait on the local completion for LUN reset.  The io_req may be
2067          * freed while we wait since we hold no lock.
2068          */
2069         wait_for_completion_timeout(&tm_done,
2070                                     msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2071
2072         spin_lock_irqsave(io_lock, flags);
2073         io_req = (struct fnic_io_req *)CMD_SP(sc);
2074         if (!io_req) {
2075                 spin_unlock_irqrestore(io_lock, flags);
2076                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2077                                 "io_req is null tag 0x%x sc 0x%p\n", tag, sc);
2078                 goto fnic_device_reset_end;
2079         }
2080         io_req->dr_done = NULL;
2081
2082         status = CMD_LR_STATUS(sc);
2083
2084         /*
2085          * If lun reset not completed, bail out with failed. io_req
2086          * gets cleaned up during higher levels of EH
2087          */
2088         if (status == FCPIO_INVALID_CODE) {
2089                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2090                               "Device reset timed out\n");
2091                 CMD_FLAGS(sc) |= FNIC_DEV_RST_TIMED_OUT;
2092                 spin_unlock_irqrestore(io_lock, flags);
2093                 int_to_scsilun(sc->device->lun, &fc_lun);
2094                 /*
2095                  * Issue abort and terminate on device reset request.
2096                  * If q'ing of terminate fails, retry it after a delay.
2097                  */
2098                 while (1) {
2099                         spin_lock_irqsave(io_lock, flags);
2100                         if (CMD_FLAGS(sc) & FNIC_DEV_RST_TERM_ISSUED) {
2101                                 spin_unlock_irqrestore(io_lock, flags);
2102                                 break;
2103                         }
2104                         spin_unlock_irqrestore(io_lock, flags);
2105                         if (fnic_queue_abort_io_req(fnic,
2106                                 tag | FNIC_TAG_DEV_RST,
2107                                 FCPIO_ITMF_ABT_TASK_TERM,
2108                                 fc_lun.scsi_lun, io_req)) {
2109                                 wait_for_completion_timeout(&tm_done,
2110                                 msecs_to_jiffies(FNIC_ABT_TERM_DELAY_TIMEOUT));
2111                         } else {
2112                                 spin_lock_irqsave(io_lock, flags);
2113                                 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2114                                 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2115                                 io_req->abts_done = &tm_done;
2116                                 spin_unlock_irqrestore(io_lock, flags);
2117                                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2118                                 "Abort and terminate issued on Device reset "
2119                                 "tag 0x%x sc 0x%p\n", tag, sc);
2120                                 break;
2121                         }
2122                 }
2123                 while (1) {
2124                         spin_lock_irqsave(io_lock, flags);
2125                         if (!(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
2126                                 spin_unlock_irqrestore(io_lock, flags);
2127                                 wait_for_completion_timeout(&tm_done,
2128                                 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2129                                 break;
2130                         } else {
2131                                 io_req = (struct fnic_io_req *)CMD_SP(sc);
2132                                 io_req->abts_done = NULL;
2133                                 goto fnic_device_reset_clean;
2134                         }
2135                 }
2136         } else {
2137                 spin_unlock_irqrestore(io_lock, flags);
2138         }
2139
2140         /* Completed, but not successful, clean up the io_req, return fail */
2141         if (status != FCPIO_SUCCESS) {
2142                 spin_lock_irqsave(io_lock, flags);
2143                 FNIC_SCSI_DBG(KERN_DEBUG,
2144                               fnic->lport->host,
2145                               "Device reset completed - failed\n");
2146                 io_req = (struct fnic_io_req *)CMD_SP(sc);
2147                 goto fnic_device_reset_clean;
2148         }
2149
2150         /*
2151          * Clean up any aborts on this lun that have still not
2152          * completed. If any of these fail, then LUN reset fails.
2153          * clean_pending_aborts cleans all cmds on this lun except
2154          * the lun reset cmd. If all cmds get cleaned, the lun reset
2155          * succeeds
2156          */
2157         if (fnic_clean_pending_aborts(fnic, sc)) {
2158                 spin_lock_irqsave(io_lock, flags);
2159                 io_req = (struct fnic_io_req *)CMD_SP(sc);
2160                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2161                               "Device reset failed"
2162                               " since could not abort all IOs\n");
2163                 goto fnic_device_reset_clean;
2164         }
2165
2166         /* Clean lun reset command */
2167         spin_lock_irqsave(io_lock, flags);
2168         io_req = (struct fnic_io_req *)CMD_SP(sc);
2169         if (io_req)
2170                 /* Completed, and successful */
2171                 ret = SUCCESS;
2172
2173 fnic_device_reset_clean:
2174         if (io_req)
2175                 CMD_SP(sc) = NULL;
2176
2177         spin_unlock_irqrestore(io_lock, flags);
2178
2179         if (io_req) {
2180                 start_time = io_req->start_time;
2181                 fnic_release_ioreq_buf(fnic, io_req, sc);
2182                 mempool_free(io_req, fnic->io_req_pool);
2183         }
2184
2185 fnic_device_reset_end:
2186         FNIC_TRACE(fnic_device_reset, sc->device->host->host_no,
2187                   sc->request->tag, sc,
2188                   jiffies_to_msecs(jiffies - start_time),
2189                   0, ((u64)sc->cmnd[0] << 32 |
2190                   (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
2191                   (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
2192                   (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
2193
2194         /* free tag if it is allocated */
2195         if (unlikely(tag_gen_flag))
2196                 fnic_scsi_host_end_tag(fnic, sc);
2197
2198         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2199                       "Returning from device reset %s\n",
2200                       (ret == SUCCESS) ?
2201                       "SUCCESS" : "FAILED");
2202         return ret;
2203 }
2204
2205 /* Clean up all IOs, clean up libFC local port */
2206 int fnic_reset(struct Scsi_Host *shost)
2207 {
2208         struct fc_lport *lp;
2209         struct fnic *fnic;
2210         int ret = SUCCESS;
2211
2212         lp = shost_priv(shost);
2213         fnic = lport_priv(lp);
2214
2215         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2216                       "fnic_reset called\n");
2217
2218         /*
2219          * Reset local port, this will clean up libFC exchanges,
2220          * reset remote port sessions, and if link is up, begin flogi
2221          */
2222         if (lp->tt.lport_reset(lp))
2223                 ret = FAILED;
2224
2225         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2226                       "Returning from fnic reset %s\n",
2227                       (ret == SUCCESS) ?
2228                       "SUCCESS" : "FAILED");
2229
2230         return ret;
2231 }
2232
2233 /*
2234  * SCSI Error handling calls driver's eh_host_reset if all prior
2235  * error handling levels return FAILED. If host reset completes
2236  * successfully, and if link is up, then Fabric login begins.
2237  *
2238  * Host Reset is the highest level of error recovery. If this fails, then
2239  * host is offlined by SCSI.
2240  *
2241  */
2242 int fnic_host_reset(struct scsi_cmnd *sc)
2243 {
2244         int ret;
2245         unsigned long wait_host_tmo;
2246         struct Scsi_Host *shost = sc->device->host;
2247         struct fc_lport *lp = shost_priv(shost);
2248
2249         /*
2250          * If fnic_reset is successful, wait for fabric login to complete
2251          * scsi-ml tries to send a TUR to every device if host reset is
2252          * successful, so before returning to scsi, fabric should be up
2253          */
2254         ret = fnic_reset(shost);
2255         if (ret == SUCCESS) {
2256                 wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
2257                 ret = FAILED;
2258                 while (time_before(jiffies, wait_host_tmo)) {
2259                         if ((lp->state == LPORT_ST_READY) &&
2260                             (lp->link_up)) {
2261                                 ret = SUCCESS;
2262                                 break;
2263                         }
2264                         ssleep(1);
2265                 }
2266         }
2267
2268         return ret;
2269 }
2270
2271 /*
2272  * This fxn is called from libFC when host is removed
2273  */
2274 void fnic_scsi_abort_io(struct fc_lport *lp)
2275 {
2276         int err = 0;
2277         unsigned long flags;
2278         enum fnic_state old_state;
2279         struct fnic *fnic = lport_priv(lp);
2280         DECLARE_COMPLETION_ONSTACK(remove_wait);
2281
2282         /* Issue firmware reset for fnic, wait for reset to complete */
2283 retry_fw_reset:
2284         spin_lock_irqsave(&fnic->fnic_lock, flags);
2285         if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2286                 /* fw reset is in progress, poll for its completion */
2287                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2288                 schedule_timeout(msecs_to_jiffies(100));
2289                 goto retry_fw_reset;
2290         }
2291
2292         fnic->remove_wait = &remove_wait;
2293         old_state = fnic->state;
2294         fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
2295         fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
2296         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2297
2298         err = fnic_fw_reset_handler(fnic);
2299         if (err) {
2300                 spin_lock_irqsave(&fnic->fnic_lock, flags);
2301                 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2302                         fnic->state = old_state;
2303                 fnic->remove_wait = NULL;
2304                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2305                 return;
2306         }
2307
2308         /* Wait for firmware reset to complete */
2309         wait_for_completion_timeout(&remove_wait,
2310                                     msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
2311
2312         spin_lock_irqsave(&fnic->fnic_lock, flags);
2313         fnic->remove_wait = NULL;
2314         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2315                       "fnic_scsi_abort_io %s\n",
2316                       (fnic->state == FNIC_IN_ETH_MODE) ?
2317                       "SUCCESS" : "FAILED");
2318         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2319
2320 }
2321
2322 /*
2323  * This fxn called from libFC to clean up driver IO state on link down
2324  */
2325 void fnic_scsi_cleanup(struct fc_lport *lp)
2326 {
2327         unsigned long flags;
2328         enum fnic_state old_state;
2329         struct fnic *fnic = lport_priv(lp);
2330
2331         /* issue fw reset */
2332 retry_fw_reset:
2333         spin_lock_irqsave(&fnic->fnic_lock, flags);
2334         if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2335                 /* fw reset is in progress, poll for its completion */
2336                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2337                 schedule_timeout(msecs_to_jiffies(100));
2338                 goto retry_fw_reset;
2339         }
2340         old_state = fnic->state;
2341         fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
2342         fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
2343         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2344
2345         if (fnic_fw_reset_handler(fnic)) {
2346                 spin_lock_irqsave(&fnic->fnic_lock, flags);
2347                 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2348                         fnic->state = old_state;
2349                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2350         }
2351
2352 }
2353
2354 void fnic_empty_scsi_cleanup(struct fc_lport *lp)
2355 {
2356 }
2357
2358 void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
2359 {
2360         struct fnic *fnic = lport_priv(lp);
2361
2362         /* Non-zero sid, nothing to do */
2363         if (sid)
2364                 goto call_fc_exch_mgr_reset;
2365
2366         if (did) {
2367                 fnic_rport_exch_reset(fnic, did);
2368                 goto call_fc_exch_mgr_reset;
2369         }
2370
2371         /*
2372          * sid = 0, did = 0
2373          * link down or device being removed
2374          */
2375         if (!fnic->in_remove)
2376                 fnic_scsi_cleanup(lp);
2377         else
2378                 fnic_scsi_abort_io(lp);
2379
2380         /* call libFC exch mgr reset to reset its exchanges */
2381 call_fc_exch_mgr_reset:
2382         fc_exch_mgr_reset(lp, sid, did);
2383
2384 }
2385
2386 /*
2387  * fnic_is_abts_pending() is a helper function that
2388  * walks through tag map to check if there is any IOs pending,if there is one,
2389  * then it returns 1 (true), otherwise 0 (false)
2390  * if @lr_sc is non NULL, then it checks IOs specific to particular LUN,
2391  * otherwise, it checks for all IOs.
2392  */
2393 int fnic_is_abts_pending(struct fnic *fnic, struct scsi_cmnd *lr_sc)
2394 {
2395         int tag;
2396         struct fnic_io_req *io_req;
2397         spinlock_t *io_lock;
2398         unsigned long flags;
2399         int ret = 0;
2400         struct scsi_cmnd *sc;
2401         struct scsi_device *lun_dev = NULL;
2402
2403         if (lr_sc)
2404                 lun_dev = lr_sc->device;
2405
2406         /* walk again to check, if IOs are still pending in fw */
2407         for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
2408                 sc = scsi_host_find_tag(fnic->lport->host, tag);
2409                 /*
2410                  * ignore this lun reset cmd or cmds that do not belong to
2411                  * this lun
2412                  */
2413                 if (!sc || (lr_sc && (sc->device != lun_dev || sc == lr_sc)))
2414                         continue;
2415
2416                 io_lock = fnic_io_lock_hash(fnic, sc);
2417                 spin_lock_irqsave(io_lock, flags);
2418
2419                 io_req = (struct fnic_io_req *)CMD_SP(sc);
2420
2421                 if (!io_req || sc->device != lun_dev) {
2422                         spin_unlock_irqrestore(io_lock, flags);
2423                         continue;
2424                 }
2425
2426                 /*
2427                  * Found IO that is still pending with firmware and
2428                  * belongs to the LUN that we are resetting
2429                  */
2430                 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2431                               "Found IO in %s on lun\n",
2432                               fnic_ioreq_state_to_str(CMD_STATE(sc)));
2433
2434                 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
2435                         ret = 1;
2436                 spin_unlock_irqrestore(io_lock, flags);
2437         }
2438
2439         return ret;
2440 }