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