2 * Copyright 2008 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
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.
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
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>
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",
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",
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",
79 const char *fnic_state_to_str(unsigned int state)
81 if (state >= ARRAY_SIZE(fnic_state_str) || !fnic_state_str[state])
84 return fnic_state_str[state];
87 static const char *fnic_ioreq_state_to_str(unsigned int state)
89 if (state >= ARRAY_SIZE(fnic_ioreq_state_str) ||
90 !fnic_ioreq_state_str[state])
93 return fnic_ioreq_state_str[state];
96 static const char *fnic_fcpio_status_to_str(unsigned int status)
98 if (status >= ARRAY_SIZE(fcpio_status_str) || !fcpio_status_str[status])
101 return fcpio_status_str[status];
104 static void fnic_cleanup_io(struct fnic *fnic, int exclude_id);
106 static inline spinlock_t *fnic_io_lock_hash(struct fnic *fnic,
107 struct scsi_cmnd *sc)
109 u32 hash = sc->request->tag & (FNIC_IO_LOCKS - 1);
111 return &fnic->io_req_lock[hash];
114 static inline spinlock_t *fnic_io_lock_tag(struct fnic *fnic,
117 return &fnic->io_req_lock[tag & (FNIC_IO_LOCKS - 1)];
121 * Unmap the data buffer and sense buffer for an io_req,
122 * also unmap and free the device-private scatter/gather list.
124 static void fnic_release_ioreq_buf(struct fnic *fnic,
125 struct fnic_io_req *io_req,
126 struct scsi_cmnd *sc)
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,
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);
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)
145 /* if no Ack received from firmware, then nothing to clean */
146 if (!fnic->fw_ack_recd[0])
150 * Update desc_available count based on number of freed descriptors
151 * Account for wraparound
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);
157 wq->ring.desc_avail += (wq->ring.desc_count
159 + fnic->fw_ack_index[0] + 1);
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
167 (fnic->fw_ack_index[0] + 1) % wq->ring.desc_count;
169 /* we have processed the acks received so far */
170 fnic->fw_ack_recd[0] = 0;
176 * __fnic_set_state_flags
177 * Sets/Clears bits in fnic's state_flags
180 __fnic_set_state_flags(struct fnic *fnic, unsigned long st_flags,
181 unsigned long clearbits)
183 struct Scsi_Host *host = fnic->lport->host;
184 int sh_locked = spin_is_locked(host->host_lock);
185 unsigned long flags = 0;
188 spin_lock_irqsave(host->host_lock, flags);
191 fnic->state_flags &= ~st_flags;
193 fnic->state_flags |= st_flags;
196 spin_unlock_irqrestore(host->host_lock, flags);
203 * fnic_fw_reset_handler
204 * Routine to send reset msg to fw
206 int fnic_fw_reset_handler(struct fnic *fnic)
208 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
212 /* indicate fwreset to io path */
213 fnic_set_state_flags(fnic, FNIC_FLAGS_FWRESET);
215 skb_queue_purge(&fnic->frame_queue);
216 skb_queue_purge(&fnic->tx_queue);
218 /* wait for io cmpl */
219 while (atomic_read(&fnic->in_flight))
220 schedule_timeout(msecs_to_jiffies(1));
222 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
224 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
225 free_wq_copy_descs(fnic, wq);
227 if (!vnic_wq_copy_desc_avail(wq))
230 fnic_queue_wq_copy_desc_fw_reset(wq, SCSI_NO_TAG);
231 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
232 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
233 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
234 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
236 &fnic->fnic_stats.fw_stats.active_fw_reqs));
239 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
242 atomic64_inc(&fnic->fnic_stats.reset_stats.fw_resets);
243 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
244 "Issued fw reset\n");
246 fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
247 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
248 "Failed to issue fw reset\n");
256 * fnic_flogi_reg_handler
257 * Routine to send flogi register msg to fw
259 int fnic_flogi_reg_handler(struct fnic *fnic, u32 fc_id)
261 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
262 enum fcpio_flogi_reg_format_type format;
263 struct fc_lport *lp = fnic->lport;
268 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
270 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
271 free_wq_copy_descs(fnic, wq);
273 if (!vnic_wq_copy_desc_avail(wq)) {
275 goto flogi_reg_ioreq_end;
278 if (fnic->ctlr.map_dest) {
279 memset(gw_mac, 0xff, ETH_ALEN);
280 format = FCPIO_FLOGI_REG_DEF_DEST;
282 memcpy(gw_mac, fnic->ctlr.dest_addr, ETH_ALEN);
283 format = FCPIO_FLOGI_REG_GW_DEST;
286 if ((fnic->config.flags & VFCF_FIP_CAPABLE) && !fnic->ctlr.map_dest) {
287 fnic_queue_wq_copy_desc_fip_reg(wq, SCSI_NO_TAG,
290 lp->r_a_tov, lp->e_d_tov);
291 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
292 "FLOGI FIP reg issued fcid %x src %pM dest %pM\n",
293 fc_id, fnic->data_src_addr, gw_mac);
295 fnic_queue_wq_copy_desc_flogi_reg(wq, SCSI_NO_TAG,
296 format, fc_id, gw_mac);
297 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
298 "FLOGI reg issued fcid %x map %d dest %pM\n",
299 fc_id, fnic->ctlr.map_dest, gw_mac);
302 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
303 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
304 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
305 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
306 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
309 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
314 * fnic_queue_wq_copy_desc
315 * Routine to enqueue a wq copy desc
317 static inline int fnic_queue_wq_copy_desc(struct fnic *fnic,
318 struct vnic_wq_copy *wq,
319 struct fnic_io_req *io_req,
320 struct scsi_cmnd *sc,
323 struct scatterlist *sg;
324 struct fc_rport *rport = starget_to_rport(scsi_target(sc->device));
325 struct fc_rport_libfc_priv *rp = rport->dd_data;
326 struct host_sg_desc *desc;
327 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
329 unsigned long intr_flags;
332 struct scsi_lun fc_lun;
336 /* For each SGE, create a device desc entry */
337 desc = io_req->sgl_list;
338 for_each_sg(scsi_sglist(sc), sg, sg_count, i) {
339 desc->addr = cpu_to_le64(sg_dma_address(sg));
340 desc->len = cpu_to_le32(sg_dma_len(sg));
345 io_req->sgl_list_pa = pci_map_single
348 sizeof(io_req->sgl_list[0]) * sg_count,
351 r = pci_dma_mapping_error(fnic->pdev, io_req->sgl_list_pa);
353 printk(KERN_ERR "PCI mapping failed with error %d\n", r);
354 return SCSI_MLQUEUE_HOST_BUSY;
358 io_req->sense_buf_pa = pci_map_single(fnic->pdev,
360 SCSI_SENSE_BUFFERSIZE,
363 r = pci_dma_mapping_error(fnic->pdev, io_req->sense_buf_pa);
365 pci_unmap_single(fnic->pdev, io_req->sgl_list_pa,
366 sizeof(io_req->sgl_list[0]) * sg_count,
368 printk(KERN_ERR "PCI mapping failed with error %d\n", r);
369 return SCSI_MLQUEUE_HOST_BUSY;
372 int_to_scsilun(sc->device->lun, &fc_lun);
374 /* Enqueue the descriptor in the Copy WQ */
375 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
377 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
378 free_wq_copy_descs(fnic, wq);
380 if (unlikely(!vnic_wq_copy_desc_avail(wq))) {
381 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
382 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
383 "fnic_queue_wq_copy_desc failure - no descriptors\n");
384 atomic64_inc(&misc_stats->io_cpwq_alloc_failures);
385 return SCSI_MLQUEUE_HOST_BUSY;
389 if (sc->sc_data_direction == DMA_FROM_DEVICE)
390 flags = FCPIO_ICMND_RDDATA;
391 else if (sc->sc_data_direction == DMA_TO_DEVICE)
392 flags = FCPIO_ICMND_WRDATA;
395 if ((fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR) &&
396 (rp->flags & FC_RP_FLAGS_RETRY))
397 exch_flags |= FCPIO_ICMND_SRFLAG_RETRY;
399 fnic_queue_wq_copy_desc_icmnd_16(wq, sc->request->tag,
400 0, exch_flags, io_req->sgl_cnt,
401 SCSI_SENSE_BUFFERSIZE,
403 io_req->sense_buf_pa,
404 0, /* scsi cmd ref, always 0 */
405 FCPIO_ICMND_PTA_SIMPLE,
406 /* scsi pri and tag */
407 flags, /* command flags */
408 sc->cmnd, sc->cmd_len,
410 fc_lun.scsi_lun, io_req->port_id,
411 rport->maxframe_size, rp->r_a_tov,
414 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
415 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
416 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
417 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
418 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
420 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
426 * Routine to send a scsi cdb
427 * Called with host_lock held and interrupts disabled.
429 static int fnic_queuecommand_lck(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
431 struct fc_lport *lp = shost_priv(sc->device->host);
432 struct fc_rport *rport;
433 struct fnic_io_req *io_req = NULL;
434 struct fnic *fnic = lport_priv(lp);
435 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
436 struct vnic_wq_copy *wq;
440 unsigned long flags = 0;
442 spinlock_t *io_lock = NULL;
443 int io_lock_acquired = 0;
444 struct fc_rport_libfc_priv *rp;
446 if (unlikely(fnic_chk_state_flags_locked(fnic, FNIC_FLAGS_IO_BLOCKED)))
447 return SCSI_MLQUEUE_HOST_BUSY;
449 rport = starget_to_rport(scsi_target(sc->device));
451 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
452 "returning DID_NO_CONNECT for IO as rport is NULL\n");
453 sc->result = DID_NO_CONNECT << 16;
458 ret = fc_remote_port_chkready(rport);
460 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
461 "rport is not ready\n");
462 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
469 if (!rp || rp->rp_state != RPORT_ST_READY) {
470 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
471 "returning DID_NO_CONNECT for IO as rport is removed\n");
472 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
473 sc->result = DID_NO_CONNECT<<16;
478 if (lp->state != LPORT_ST_READY || !(lp->link_up))
479 return SCSI_MLQUEUE_HOST_BUSY;
481 atomic_inc(&fnic->in_flight);
484 * Release host lock, use driver resource specific locks from here.
485 * Don't re-enable interrupts in case they were disabled prior to the
486 * caller disabling them.
488 spin_unlock(lp->host->host_lock);
489 CMD_STATE(sc) = FNIC_IOREQ_NOT_INITED;
490 CMD_FLAGS(sc) = FNIC_NO_FLAGS;
492 /* Get a new io_req for this SCSI IO */
493 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
495 atomic64_inc(&fnic_stats->io_stats.alloc_failures);
496 ret = SCSI_MLQUEUE_HOST_BUSY;
499 memset(io_req, 0, sizeof(*io_req));
501 /* Map the data buffer */
502 sg_count = scsi_dma_map(sc);
504 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
505 sc->request->tag, sc, 0, sc->cmnd[0],
506 sg_count, CMD_STATE(sc));
507 mempool_free(io_req, fnic->io_req_pool);
511 /* Determine the type of scatter/gather list we need */
512 io_req->sgl_cnt = sg_count;
513 io_req->sgl_type = FNIC_SGL_CACHE_DFLT;
514 if (sg_count > FNIC_DFLT_SG_DESC_CNT)
515 io_req->sgl_type = FNIC_SGL_CACHE_MAX;
519 mempool_alloc(fnic->io_sgl_pool[io_req->sgl_type],
521 if (!io_req->sgl_list) {
522 atomic64_inc(&fnic_stats->io_stats.alloc_failures);
523 ret = SCSI_MLQUEUE_HOST_BUSY;
525 mempool_free(io_req, fnic->io_req_pool);
529 /* Cache sgl list allocated address before alignment */
530 io_req->sgl_list_alloc = io_req->sgl_list;
531 ptr = (unsigned long) io_req->sgl_list;
532 if (ptr % FNIC_SG_DESC_ALIGN) {
533 io_req->sgl_list = (struct host_sg_desc *)
534 (((unsigned long) ptr
535 + FNIC_SG_DESC_ALIGN - 1)
536 & ~(FNIC_SG_DESC_ALIGN - 1));
541 * Will acquire lock defore setting to IO initialized.
544 io_lock = fnic_io_lock_hash(fnic, sc);
545 spin_lock_irqsave(io_lock, flags);
547 /* initialize rest of io_req */
548 io_lock_acquired = 1;
549 io_req->port_id = rport->port_id;
550 io_req->start_time = jiffies;
551 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
552 CMD_SP(sc) = (char *)io_req;
553 CMD_FLAGS(sc) |= FNIC_IO_INITIALIZED;
554 sc->scsi_done = done;
556 /* create copy wq desc and enqueue it */
557 wq = &fnic->wq_copy[0];
558 ret = fnic_queue_wq_copy_desc(fnic, wq, io_req, sc, sg_count);
561 * In case another thread cancelled the request,
562 * refetch the pointer under the lock.
564 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
565 sc->request->tag, sc, 0, 0, 0,
566 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
567 io_req = (struct fnic_io_req *)CMD_SP(sc);
569 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
570 spin_unlock_irqrestore(io_lock, flags);
572 fnic_release_ioreq_buf(fnic, io_req, sc);
573 mempool_free(io_req, fnic->io_req_pool);
575 atomic_dec(&fnic->in_flight);
576 /* acquire host lock before returning to SCSI */
577 spin_lock(lp->host->host_lock);
580 atomic64_inc(&fnic_stats->io_stats.active_ios);
581 atomic64_inc(&fnic_stats->io_stats.num_ios);
582 if (atomic64_read(&fnic_stats->io_stats.active_ios) >
583 atomic64_read(&fnic_stats->io_stats.max_active_ios))
584 atomic64_set(&fnic_stats->io_stats.max_active_ios,
585 atomic64_read(&fnic_stats->io_stats.active_ios));
587 /* REVISIT: Use per IO lock in the final code */
588 CMD_FLAGS(sc) |= FNIC_IO_ISSUED;
591 cmd_trace = ((u64)sc->cmnd[0] << 56 | (u64)sc->cmnd[7] << 40 |
592 (u64)sc->cmnd[8] << 32 | (u64)sc->cmnd[2] << 24 |
593 (u64)sc->cmnd[3] << 16 | (u64)sc->cmnd[4] << 8 |
596 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
597 sc->request->tag, sc, io_req,
599 (((u64)CMD_FLAGS(sc) >> 32) | CMD_STATE(sc)));
601 /* if only we issued IO, will we have the io lock */
602 if (io_lock_acquired)
603 spin_unlock_irqrestore(io_lock, flags);
605 atomic_dec(&fnic->in_flight);
606 /* acquire host lock before returning to SCSI */
607 spin_lock(lp->host->host_lock);
611 DEF_SCSI_QCMD(fnic_queuecommand)
614 * fnic_fcpio_fw_reset_cmpl_handler
615 * Routine to handle fw reset completion
617 static int fnic_fcpio_fw_reset_cmpl_handler(struct fnic *fnic,
618 struct fcpio_fw_req *desc)
622 struct fcpio_tag tag;
625 struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
627 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
629 atomic64_inc(&reset_stats->fw_reset_completions);
631 /* Clean up all outstanding io requests */
632 fnic_cleanup_io(fnic, SCSI_NO_TAG);
634 atomic64_set(&fnic->fnic_stats.fw_stats.active_fw_reqs, 0);
635 atomic64_set(&fnic->fnic_stats.io_stats.active_ios, 0);
636 atomic64_set(&fnic->io_cmpl_skip, 0);
638 spin_lock_irqsave(&fnic->fnic_lock, flags);
640 /* fnic should be in FC_TRANS_ETH_MODE */
641 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) {
642 /* Check status of reset completion */
644 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
645 "reset cmpl success\n");
646 /* Ready to send flogi out */
647 fnic->state = FNIC_IN_ETH_MODE;
649 FNIC_SCSI_DBG(KERN_DEBUG,
651 "fnic fw_reset : failed %s\n",
652 fnic_fcpio_status_to_str(hdr_status));
655 * Unable to change to eth mode, cannot send out flogi
656 * Change state to fc mode, so that subsequent Flogi
657 * requests from libFC will cause more attempts to
658 * reset the firmware. Free the cached flogi
660 fnic->state = FNIC_IN_FC_MODE;
661 atomic64_inc(&reset_stats->fw_reset_failures);
665 FNIC_SCSI_DBG(KERN_DEBUG,
667 "Unexpected state %s while processing"
668 " reset cmpl\n", fnic_state_to_str(fnic->state));
669 atomic64_inc(&reset_stats->fw_reset_failures);
673 /* Thread removing device blocks till firmware reset is complete */
674 if (fnic->remove_wait)
675 complete(fnic->remove_wait);
678 * If fnic is being removed, or fw reset failed
679 * free the flogi frame. Else, send it out
681 if (fnic->remove_wait || ret) {
682 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
683 skb_queue_purge(&fnic->tx_queue);
684 goto reset_cmpl_handler_end;
687 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
691 reset_cmpl_handler_end:
692 fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
698 * fnic_fcpio_flogi_reg_cmpl_handler
699 * Routine to handle flogi register completion
701 static int fnic_fcpio_flogi_reg_cmpl_handler(struct fnic *fnic,
702 struct fcpio_fw_req *desc)
706 struct fcpio_tag tag;
710 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
712 /* Update fnic state based on status of flogi reg completion */
713 spin_lock_irqsave(&fnic->fnic_lock, flags);
715 if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE) {
717 /* Check flogi registration completion status */
719 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
720 "flog reg succeeded\n");
721 fnic->state = FNIC_IN_FC_MODE;
723 FNIC_SCSI_DBG(KERN_DEBUG,
725 "fnic flogi reg :failed %s\n",
726 fnic_fcpio_status_to_str(hdr_status));
727 fnic->state = FNIC_IN_ETH_MODE;
731 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
732 "Unexpected fnic state %s while"
733 " processing flogi reg completion\n",
734 fnic_state_to_str(fnic->state));
739 if (fnic->stop_rx_link_events) {
740 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
741 goto reg_cmpl_handler_end;
743 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
746 queue_work(fnic_event_queue, &fnic->frame_work);
748 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
751 reg_cmpl_handler_end:
755 static inline int is_ack_index_in_range(struct vnic_wq_copy *wq,
758 if (wq->to_clean_index <= wq->to_use_index) {
759 /* out of range, stale request_out index */
760 if (request_out < wq->to_clean_index ||
761 request_out >= wq->to_use_index)
764 /* out of range, stale request_out index */
765 if (request_out < wq->to_clean_index &&
766 request_out >= wq->to_use_index)
769 /* request_out index is in range */
775 * Mark that ack received and store the Ack index. If there are multiple
776 * acks received before Tx thread cleans it up, the latest value will be
777 * used which is correct behavior. This state should be in the copy Wq
778 * instead of in the fnic
780 static inline void fnic_fcpio_ack_handler(struct fnic *fnic,
781 unsigned int cq_index,
782 struct fcpio_fw_req *desc)
784 struct vnic_wq_copy *wq;
785 u16 request_out = desc->u.ack.request_out;
787 u64 *ox_id_tag = (u64 *)(void *)desc;
789 /* mark the ack state */
790 wq = &fnic->wq_copy[cq_index - fnic->raw_wq_count - fnic->rq_count];
791 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
793 fnic->fnic_stats.misc_stats.last_ack_time = jiffies;
794 if (is_ack_index_in_range(wq, request_out)) {
795 fnic->fw_ack_index[0] = request_out;
796 fnic->fw_ack_recd[0] = 1;
799 &fnic->fnic_stats.misc_stats.ack_index_out_of_range);
801 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
802 FNIC_TRACE(fnic_fcpio_ack_handler,
803 fnic->lport->host->host_no, 0, 0, ox_id_tag[2], ox_id_tag[3],
804 ox_id_tag[4], ox_id_tag[5]);
808 * fnic_fcpio_icmnd_cmpl_handler
809 * Routine to handle icmnd completions
811 static void fnic_fcpio_icmnd_cmpl_handler(struct fnic *fnic,
812 struct fcpio_fw_req *desc)
816 struct fcpio_tag tag;
819 struct fcpio_icmnd_cmpl *icmnd_cmpl;
820 struct fnic_io_req *io_req;
821 struct scsi_cmnd *sc;
822 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
826 unsigned long start_time;
827 unsigned long io_duration_time;
829 /* Decode the cmpl description to get the io_req id */
830 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
831 fcpio_tag_id_dec(&tag, &id);
832 icmnd_cmpl = &desc->u.icmnd_cmpl;
834 if (id >= fnic->fnic_max_tag_id) {
835 shost_printk(KERN_ERR, fnic->lport->host,
836 "Tag out of range tag %x hdr status = %s\n",
837 id, fnic_fcpio_status_to_str(hdr_status));
841 sc = scsi_host_find_tag(fnic->lport->host, id);
844 atomic64_inc(&fnic_stats->io_stats.sc_null);
845 shost_printk(KERN_ERR, fnic->lport->host,
846 "icmnd_cmpl sc is null - "
847 "hdr status = %s tag = 0x%x desc = 0x%p\n",
848 fnic_fcpio_status_to_str(hdr_status), id, desc);
849 FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
850 fnic->lport->host->host_no, id,
851 ((u64)icmnd_cmpl->_resvd0[1] << 16 |
852 (u64)icmnd_cmpl->_resvd0[0]),
853 ((u64)hdr_status << 16 |
854 (u64)icmnd_cmpl->scsi_status << 8 |
855 (u64)icmnd_cmpl->flags), desc,
856 (u64)icmnd_cmpl->residual, 0);
860 io_lock = fnic_io_lock_hash(fnic, sc);
861 spin_lock_irqsave(io_lock, flags);
862 io_req = (struct fnic_io_req *)CMD_SP(sc);
863 WARN_ON_ONCE(!io_req);
865 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
866 CMD_FLAGS(sc) |= FNIC_IO_REQ_NULL;
867 spin_unlock_irqrestore(io_lock, flags);
868 shost_printk(KERN_ERR, fnic->lport->host,
869 "icmnd_cmpl io_req is null - "
870 "hdr status = %s tag = 0x%x sc 0x%p\n",
871 fnic_fcpio_status_to_str(hdr_status), id, sc);
874 start_time = io_req->start_time;
876 /* firmware completed the io */
877 io_req->io_completed = 1;
880 * if SCSI-ML has already issued abort on this command,
881 * set completion of the IO. The abts path will clean it up
883 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
886 * set the FNIC_IO_DONE so that this doesn't get
887 * flagged as 'out of order' if it was not aborted
889 CMD_FLAGS(sc) |= FNIC_IO_DONE;
890 CMD_FLAGS(sc) |= FNIC_IO_ABTS_PENDING;
891 spin_unlock_irqrestore(io_lock, flags);
892 if(FCPIO_ABORTED == hdr_status)
893 CMD_FLAGS(sc) |= FNIC_IO_ABORTED;
895 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
896 "icmnd_cmpl abts pending "
897 "hdr status = %s tag = 0x%x sc = 0x%p"
898 "scsi_status = %x residual = %d\n",
899 fnic_fcpio_status_to_str(hdr_status),
901 icmnd_cmpl->scsi_status,
902 icmnd_cmpl->residual);
906 /* Mark the IO as complete */
907 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
909 icmnd_cmpl = &desc->u.icmnd_cmpl;
911 switch (hdr_status) {
913 sc->result = (DID_OK << 16) | icmnd_cmpl->scsi_status;
914 xfer_len = scsi_bufflen(sc);
915 scsi_set_resid(sc, icmnd_cmpl->residual);
917 if (icmnd_cmpl->flags & FCPIO_ICMND_CMPL_RESID_UNDER)
918 xfer_len -= icmnd_cmpl->residual;
920 if (icmnd_cmpl->scsi_status == SAM_STAT_CHECK_CONDITION)
921 atomic64_inc(&fnic_stats->misc_stats.check_condition);
923 if (icmnd_cmpl->scsi_status == SAM_STAT_TASK_SET_FULL)
924 atomic64_inc(&fnic_stats->misc_stats.queue_fulls);
927 case FCPIO_TIMEOUT: /* request was timed out */
928 atomic64_inc(&fnic_stats->misc_stats.fcpio_timeout);
929 sc->result = (DID_TIME_OUT << 16) | icmnd_cmpl->scsi_status;
932 case FCPIO_ABORTED: /* request was aborted */
933 atomic64_inc(&fnic_stats->misc_stats.fcpio_aborted);
934 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
937 case FCPIO_DATA_CNT_MISMATCH: /* recv/sent more/less data than exp. */
938 atomic64_inc(&fnic_stats->misc_stats.data_count_mismatch);
939 scsi_set_resid(sc, icmnd_cmpl->residual);
940 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
943 case FCPIO_OUT_OF_RESOURCE: /* out of resources to complete request */
944 atomic64_inc(&fnic_stats->fw_stats.fw_out_of_resources);
945 sc->result = (DID_REQUEUE << 16) | icmnd_cmpl->scsi_status;
948 case FCPIO_IO_NOT_FOUND: /* requested I/O was not found */
949 atomic64_inc(&fnic_stats->io_stats.io_not_found);
950 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
953 case FCPIO_SGL_INVALID: /* request was aborted due to sgl error */
954 atomic64_inc(&fnic_stats->misc_stats.sgl_invalid);
955 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
958 case FCPIO_FW_ERR: /* request was terminated due fw error */
959 atomic64_inc(&fnic_stats->fw_stats.io_fw_errs);
960 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
963 case FCPIO_MSS_INVALID: /* request was aborted due to mss error */
964 atomic64_inc(&fnic_stats->misc_stats.mss_invalid);
965 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
968 case FCPIO_INVALID_HEADER: /* header contains invalid data */
969 case FCPIO_INVALID_PARAM: /* some parameter in request invalid */
970 case FCPIO_REQ_NOT_SUPPORTED:/* request type is not supported */
972 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
976 /* Break link with the SCSI command */
978 CMD_FLAGS(sc) |= FNIC_IO_DONE;
980 spin_unlock_irqrestore(io_lock, flags);
982 if (hdr_status != FCPIO_SUCCESS) {
983 atomic64_inc(&fnic_stats->io_stats.io_failures);
984 shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
985 fnic_fcpio_status_to_str(hdr_status));
988 fnic_release_ioreq_buf(fnic, io_req, sc);
990 mempool_free(io_req, fnic->io_req_pool);
992 cmd_trace = ((u64)hdr_status << 56) |
993 (u64)icmnd_cmpl->scsi_status << 48 |
994 (u64)icmnd_cmpl->flags << 40 | (u64)sc->cmnd[0] << 32 |
995 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
996 (u64)sc->cmnd[4] << 8 | sc->cmnd[5];
998 FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
999 sc->device->host->host_no, id, sc,
1000 ((u64)icmnd_cmpl->_resvd0[1] << 56 |
1001 (u64)icmnd_cmpl->_resvd0[0] << 48 |
1002 jiffies_to_msecs(jiffies - start_time)),
1004 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1006 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
1007 fnic->lport->host_stats.fcp_input_requests++;
1008 fnic->fcp_input_bytes += xfer_len;
1009 } else if (sc->sc_data_direction == DMA_TO_DEVICE) {
1010 fnic->lport->host_stats.fcp_output_requests++;
1011 fnic->fcp_output_bytes += xfer_len;
1013 fnic->lport->host_stats.fcp_control_requests++;
1015 atomic64_dec(&fnic_stats->io_stats.active_ios);
1016 if (atomic64_read(&fnic->io_cmpl_skip))
1017 atomic64_dec(&fnic->io_cmpl_skip);
1019 atomic64_inc(&fnic_stats->io_stats.io_completions);
1022 io_duration_time = jiffies_to_msecs(jiffies) - jiffies_to_msecs(io_req->start_time);
1024 if(io_duration_time <= 10)
1025 atomic64_inc(&fnic_stats->io_stats.io_btw_0_to_10_msec);
1026 else if(io_duration_time <= 100)
1027 atomic64_inc(&fnic_stats->io_stats.io_btw_10_to_100_msec);
1028 else if(io_duration_time <= 500)
1029 atomic64_inc(&fnic_stats->io_stats.io_btw_100_to_500_msec);
1030 else if(io_duration_time <= 5000)
1031 atomic64_inc(&fnic_stats->io_stats.io_btw_500_to_5000_msec);
1032 else if(io_duration_time <= 10000)
1033 atomic64_inc(&fnic_stats->io_stats.io_btw_5000_to_10000_msec);
1034 else if(io_duration_time <= 30000)
1035 atomic64_inc(&fnic_stats->io_stats.io_btw_10000_to_30000_msec);
1037 atomic64_inc(&fnic_stats->io_stats.io_greater_than_30000_msec);
1039 if(io_duration_time > atomic64_read(&fnic_stats->io_stats.current_max_io_time))
1040 atomic64_set(&fnic_stats->io_stats.current_max_io_time, io_duration_time);
1043 /* Call SCSI completion function to complete the IO */
1048 /* fnic_fcpio_itmf_cmpl_handler
1049 * Routine to handle itmf completions
1051 static void fnic_fcpio_itmf_cmpl_handler(struct fnic *fnic,
1052 struct fcpio_fw_req *desc)
1056 struct fcpio_tag tag;
1058 struct scsi_cmnd *sc;
1059 struct fnic_io_req *io_req;
1060 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1061 struct abort_stats *abts_stats = &fnic->fnic_stats.abts_stats;
1062 struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
1063 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
1064 unsigned long flags;
1065 spinlock_t *io_lock;
1066 unsigned long start_time;
1068 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
1069 fcpio_tag_id_dec(&tag, &id);
1071 if ((id & FNIC_TAG_MASK) >= fnic->fnic_max_tag_id) {
1072 shost_printk(KERN_ERR, fnic->lport->host,
1073 "Tag out of range tag %x hdr status = %s\n",
1074 id, fnic_fcpio_status_to_str(hdr_status));
1078 sc = scsi_host_find_tag(fnic->lport->host, id & FNIC_TAG_MASK);
1081 atomic64_inc(&fnic_stats->io_stats.sc_null);
1082 shost_printk(KERN_ERR, fnic->lport->host,
1083 "itmf_cmpl sc is null - hdr status = %s tag = 0x%x\n",
1084 fnic_fcpio_status_to_str(hdr_status), id);
1087 io_lock = fnic_io_lock_hash(fnic, sc);
1088 spin_lock_irqsave(io_lock, flags);
1089 io_req = (struct fnic_io_req *)CMD_SP(sc);
1090 WARN_ON_ONCE(!io_req);
1092 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
1093 spin_unlock_irqrestore(io_lock, flags);
1094 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
1095 shost_printk(KERN_ERR, fnic->lport->host,
1096 "itmf_cmpl io_req is null - "
1097 "hdr status = %s tag = 0x%x sc 0x%p\n",
1098 fnic_fcpio_status_to_str(hdr_status), id, sc);
1101 start_time = io_req->start_time;
1103 if ((id & FNIC_TAG_ABORT) && (id & FNIC_TAG_DEV_RST)) {
1104 /* Abort and terminate completion of device reset req */
1105 /* REVISIT : Add asserts about various flags */
1106 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1107 "dev reset abts cmpl recd. id %x status %s\n",
1108 id, fnic_fcpio_status_to_str(hdr_status));
1109 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1110 CMD_ABTS_STATUS(sc) = hdr_status;
1111 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1112 if (io_req->abts_done)
1113 complete(io_req->abts_done);
1114 spin_unlock_irqrestore(io_lock, flags);
1115 } else if (id & FNIC_TAG_ABORT) {
1116 /* Completion of abort cmd */
1117 switch (hdr_status) {
1121 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1122 atomic64_inc(&abts_stats->abort_fw_timeouts);
1125 &term_stats->terminate_fw_timeouts);
1127 case FCPIO_ITMF_REJECTED:
1128 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1129 "abort reject recd. id %d\n",
1130 (int)(id & FNIC_TAG_MASK));
1132 case FCPIO_IO_NOT_FOUND:
1133 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1134 atomic64_inc(&abts_stats->abort_io_not_found);
1137 &term_stats->terminate_io_not_found);
1140 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1141 atomic64_inc(&abts_stats->abort_failures);
1144 &term_stats->terminate_failures);
1147 if (CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING) {
1148 /* This is a late completion. Ignore it */
1149 spin_unlock_irqrestore(io_lock, flags);
1153 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
1154 CMD_ABTS_STATUS(sc) = hdr_status;
1156 /* If the status is IO not found consider it as success */
1157 if (hdr_status == FCPIO_IO_NOT_FOUND)
1158 CMD_ABTS_STATUS(sc) = FCPIO_SUCCESS;
1160 if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE)))
1161 atomic64_inc(&misc_stats->no_icmnd_itmf_cmpls);
1163 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1164 "abts cmpl recd. id %d status %s\n",
1165 (int)(id & FNIC_TAG_MASK),
1166 fnic_fcpio_status_to_str(hdr_status));
1169 * If scsi_eh thread is blocked waiting for abts to complete,
1170 * signal completion to it. IO will be cleaned in the thread
1171 * else clean it in this context
1173 if (io_req->abts_done) {
1174 complete(io_req->abts_done);
1175 spin_unlock_irqrestore(io_lock, flags);
1177 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1178 "abts cmpl, completing IO\n");
1180 sc->result = (DID_ERROR << 16);
1182 spin_unlock_irqrestore(io_lock, flags);
1184 fnic_release_ioreq_buf(fnic, io_req, sc);
1185 mempool_free(io_req, fnic->io_req_pool);
1186 if (sc->scsi_done) {
1187 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1188 sc->device->host->host_no, id,
1190 jiffies_to_msecs(jiffies - start_time),
1192 (((u64)hdr_status << 40) |
1193 (u64)sc->cmnd[0] << 32 |
1194 (u64)sc->cmnd[2] << 24 |
1195 (u64)sc->cmnd[3] << 16 |
1196 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1197 (((u64)CMD_FLAGS(sc) << 32) |
1200 atomic64_dec(&fnic_stats->io_stats.active_ios);
1201 if (atomic64_read(&fnic->io_cmpl_skip))
1202 atomic64_dec(&fnic->io_cmpl_skip);
1204 atomic64_inc(&fnic_stats->io_stats.io_completions);
1208 } else if (id & FNIC_TAG_DEV_RST) {
1209 /* Completion of device reset */
1210 CMD_LR_STATUS(sc) = hdr_status;
1211 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1212 spin_unlock_irqrestore(io_lock, flags);
1213 CMD_FLAGS(sc) |= FNIC_DEV_RST_ABTS_PENDING;
1214 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1215 sc->device->host->host_no, id, sc,
1216 jiffies_to_msecs(jiffies - start_time),
1218 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1219 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1220 "Terminate pending "
1221 "dev reset cmpl recd. id %d status %s\n",
1222 (int)(id & FNIC_TAG_MASK),
1223 fnic_fcpio_status_to_str(hdr_status));
1226 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TIMED_OUT) {
1227 /* Need to wait for terminate completion */
1228 spin_unlock_irqrestore(io_lock, flags);
1229 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1230 sc->device->host->host_no, id, sc,
1231 jiffies_to_msecs(jiffies - start_time),
1233 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1234 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1235 "dev reset cmpl recd after time out. "
1236 "id %d status %s\n",
1237 (int)(id & FNIC_TAG_MASK),
1238 fnic_fcpio_status_to_str(hdr_status));
1241 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
1242 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1243 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1244 "dev reset cmpl recd. id %d status %s\n",
1245 (int)(id & FNIC_TAG_MASK),
1246 fnic_fcpio_status_to_str(hdr_status));
1247 if (io_req->dr_done)
1248 complete(io_req->dr_done);
1249 spin_unlock_irqrestore(io_lock, flags);
1252 shost_printk(KERN_ERR, fnic->lport->host,
1253 "Unexpected itmf io state %s tag %x\n",
1254 fnic_ioreq_state_to_str(CMD_STATE(sc)), id);
1255 spin_unlock_irqrestore(io_lock, flags);
1261 * fnic_fcpio_cmpl_handler
1262 * Routine to service the cq for wq_copy
1264 static int fnic_fcpio_cmpl_handler(struct vnic_dev *vdev,
1265 unsigned int cq_index,
1266 struct fcpio_fw_req *desc)
1268 struct fnic *fnic = vnic_dev_priv(vdev);
1270 switch (desc->hdr.type) {
1271 case FCPIO_ICMND_CMPL: /* fw completed a command */
1272 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1273 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
1274 case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
1275 case FCPIO_RESET_CMPL: /* fw completed reset */
1276 atomic64_dec(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1282 switch (desc->hdr.type) {
1283 case FCPIO_ACK: /* fw copied copy wq desc to its queue */
1284 fnic_fcpio_ack_handler(fnic, cq_index, desc);
1287 case FCPIO_ICMND_CMPL: /* fw completed a command */
1288 fnic_fcpio_icmnd_cmpl_handler(fnic, desc);
1291 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1292 fnic_fcpio_itmf_cmpl_handler(fnic, desc);
1295 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
1296 case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
1297 fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
1300 case FCPIO_RESET_CMPL: /* fw completed reset */
1301 fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
1305 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1306 "firmware completion type %d\n",
1315 * fnic_wq_copy_cmpl_handler
1316 * Routine to process wq copy
1318 int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
1320 unsigned int wq_work_done = 0;
1321 unsigned int i, cq_index;
1322 unsigned int cur_work_done;
1324 for (i = 0; i < fnic->wq_copy_count; i++) {
1325 cq_index = i + fnic->raw_wq_count + fnic->rq_count;
1326 cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
1327 fnic_fcpio_cmpl_handler,
1329 wq_work_done += cur_work_done;
1331 return wq_work_done;
1334 static void fnic_cleanup_io(struct fnic *fnic, int exclude_id)
1337 struct fnic_io_req *io_req;
1338 unsigned long flags = 0;
1339 struct scsi_cmnd *sc;
1340 spinlock_t *io_lock;
1341 unsigned long start_time = 0;
1342 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1344 for (i = 0; i < fnic->fnic_max_tag_id; i++) {
1345 if (i == exclude_id)
1348 io_lock = fnic_io_lock_tag(fnic, i);
1349 spin_lock_irqsave(io_lock, flags);
1350 sc = scsi_host_find_tag(fnic->lport->host, i);
1352 spin_unlock_irqrestore(io_lock, flags);
1356 io_req = (struct fnic_io_req *)CMD_SP(sc);
1357 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1358 !(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
1360 * We will be here only when FW completes reset
1361 * without sending completions for outstanding ios.
1363 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1364 if (io_req && io_req->dr_done)
1365 complete(io_req->dr_done);
1366 else if (io_req && io_req->abts_done)
1367 complete(io_req->abts_done);
1368 spin_unlock_irqrestore(io_lock, flags);
1370 } else if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1371 spin_unlock_irqrestore(io_lock, flags);
1375 spin_unlock_irqrestore(io_lock, flags);
1376 goto cleanup_scsi_cmd;
1381 spin_unlock_irqrestore(io_lock, flags);
1384 * If there is a scsi_cmnd associated with this io_req, then
1385 * free the corresponding state
1387 start_time = io_req->start_time;
1388 fnic_release_ioreq_buf(fnic, io_req, sc);
1389 mempool_free(io_req, fnic->io_req_pool);
1392 sc->result = DID_TRANSPORT_DISRUPTED << 16;
1393 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1394 "%s: sc duration = %lu DID_TRANSPORT_DISRUPTED\n",
1395 __func__, (jiffies - start_time));
1397 if (atomic64_read(&fnic->io_cmpl_skip))
1398 atomic64_dec(&fnic->io_cmpl_skip);
1400 atomic64_inc(&fnic_stats->io_stats.io_completions);
1402 /* Complete the command to SCSI */
1403 if (sc->scsi_done) {
1404 FNIC_TRACE(fnic_cleanup_io,
1405 sc->device->host->host_no, i, sc,
1406 jiffies_to_msecs(jiffies - start_time),
1407 0, ((u64)sc->cmnd[0] << 32 |
1408 (u64)sc->cmnd[2] << 24 |
1409 (u64)sc->cmnd[3] << 16 |
1410 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1411 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1418 void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
1419 struct fcpio_host_req *desc)
1422 struct fnic *fnic = vnic_dev_priv(wq->vdev);
1423 struct fnic_io_req *io_req;
1424 struct scsi_cmnd *sc;
1425 unsigned long flags;
1426 spinlock_t *io_lock;
1427 unsigned long start_time = 0;
1429 /* get the tag reference */
1430 fcpio_tag_id_dec(&desc->hdr.tag, &id);
1431 id &= FNIC_TAG_MASK;
1433 if (id >= fnic->fnic_max_tag_id)
1436 sc = scsi_host_find_tag(fnic->lport->host, id);
1440 io_lock = fnic_io_lock_hash(fnic, sc);
1441 spin_lock_irqsave(io_lock, flags);
1443 /* Get the IO context which this desc refers to */
1444 io_req = (struct fnic_io_req *)CMD_SP(sc);
1446 /* fnic interrupts are turned off by now */
1449 spin_unlock_irqrestore(io_lock, flags);
1450 goto wq_copy_cleanup_scsi_cmd;
1455 spin_unlock_irqrestore(io_lock, flags);
1457 start_time = io_req->start_time;
1458 fnic_release_ioreq_buf(fnic, io_req, sc);
1459 mempool_free(io_req, fnic->io_req_pool);
1461 wq_copy_cleanup_scsi_cmd:
1462 sc->result = DID_NO_CONNECT << 16;
1463 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
1464 " DID_NO_CONNECT\n");
1466 if (sc->scsi_done) {
1467 FNIC_TRACE(fnic_wq_copy_cleanup_handler,
1468 sc->device->host->host_no, id, sc,
1469 jiffies_to_msecs(jiffies - start_time),
1470 0, ((u64)sc->cmnd[0] << 32 |
1471 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1472 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1473 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1479 static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
1480 u32 task_req, u8 *fc_lun,
1481 struct fnic_io_req *io_req)
1483 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1484 struct Scsi_Host *host = fnic->lport->host;
1485 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
1486 unsigned long flags;
1488 spin_lock_irqsave(host->host_lock, flags);
1489 if (unlikely(fnic_chk_state_flags_locked(fnic,
1490 FNIC_FLAGS_IO_BLOCKED))) {
1491 spin_unlock_irqrestore(host->host_lock, flags);
1494 atomic_inc(&fnic->in_flight);
1495 spin_unlock_irqrestore(host->host_lock, flags);
1497 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
1499 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1500 free_wq_copy_descs(fnic, wq);
1502 if (!vnic_wq_copy_desc_avail(wq)) {
1503 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1504 atomic_dec(&fnic->in_flight);
1505 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1506 "fnic_queue_abort_io_req: failure: no descriptors\n");
1507 atomic64_inc(&misc_stats->abts_cpwq_alloc_failures);
1510 fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
1511 0, task_req, tag, fc_lun, io_req->port_id,
1512 fnic->config.ra_tov, fnic->config.ed_tov);
1514 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1515 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
1516 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
1517 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
1518 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
1520 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1521 atomic_dec(&fnic->in_flight);
1526 static void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
1531 struct fnic_io_req *io_req;
1532 spinlock_t *io_lock;
1533 unsigned long flags;
1534 struct scsi_cmnd *sc;
1535 struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
1536 struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
1537 struct scsi_lun fc_lun;
1538 enum fnic_ioreq_state old_ioreq_state;
1540 FNIC_SCSI_DBG(KERN_DEBUG,
1542 "fnic_rport_exch_reset called portid 0x%06x\n",
1545 if (fnic->in_remove)
1548 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
1550 io_lock = fnic_io_lock_tag(fnic, tag);
1551 spin_lock_irqsave(io_lock, flags);
1552 sc = scsi_host_find_tag(fnic->lport->host, tag);
1554 spin_unlock_irqrestore(io_lock, flags);
1558 io_req = (struct fnic_io_req *)CMD_SP(sc);
1560 if (!io_req || io_req->port_id != port_id) {
1561 spin_unlock_irqrestore(io_lock, flags);
1565 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1566 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
1567 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1568 "fnic_rport_exch_reset dev rst not pending sc 0x%p\n",
1570 spin_unlock_irqrestore(io_lock, flags);
1575 * Found IO that is still pending with firmware and
1576 * belongs to rport that went away
1578 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1579 spin_unlock_irqrestore(io_lock, flags);
1582 if (io_req->abts_done) {
1583 shost_printk(KERN_ERR, fnic->lport->host,
1584 "fnic_rport_exch_reset: io_req->abts_done is set "
1586 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1589 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1590 shost_printk(KERN_ERR, fnic->lport->host,
1592 "IO not yet issued %p tag 0x%x flags "
1594 sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1596 old_ioreq_state = CMD_STATE(sc);
1597 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1598 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1599 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1600 atomic64_inc(&reset_stats->device_reset_terminates);
1601 abt_tag = (tag | FNIC_TAG_DEV_RST);
1602 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1603 "fnic_rport_exch_reset dev rst sc 0x%p\n",
1607 BUG_ON(io_req->abts_done);
1609 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1610 "fnic_rport_reset_exch: Issuing abts\n");
1612 spin_unlock_irqrestore(io_lock, flags);
1614 /* Now queue the abort command to firmware */
1615 int_to_scsilun(sc->device->lun, &fc_lun);
1617 if (fnic_queue_abort_io_req(fnic, abt_tag,
1618 FCPIO_ITMF_ABT_TASK_TERM,
1619 fc_lun.scsi_lun, io_req)) {
1621 * Revert the cmd state back to old state, if
1622 * it hasn't changed in between. This cmd will get
1623 * aborted later by scsi_eh, or cleaned up during
1626 spin_lock_irqsave(io_lock, flags);
1627 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1628 CMD_STATE(sc) = old_ioreq_state;
1629 spin_unlock_irqrestore(io_lock, flags);
1631 spin_lock_irqsave(io_lock, flags);
1632 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1633 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
1635 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
1636 spin_unlock_irqrestore(io_lock, flags);
1637 atomic64_inc(&term_stats->terminates);
1641 if (term_cnt > atomic64_read(&term_stats->max_terminates))
1642 atomic64_set(&term_stats->max_terminates, term_cnt);
1646 void fnic_terminate_rport_io(struct fc_rport *rport)
1651 struct fnic_io_req *io_req;
1652 spinlock_t *io_lock;
1653 unsigned long flags;
1654 struct scsi_cmnd *sc;
1655 struct scsi_lun fc_lun;
1656 struct fc_rport_libfc_priv *rdata;
1657 struct fc_lport *lport;
1659 struct fc_rport *cmd_rport;
1660 struct reset_stats *reset_stats;
1661 struct terminate_stats *term_stats;
1662 enum fnic_ioreq_state old_ioreq_state;
1665 printk(KERN_ERR "fnic_terminate_rport_io: rport is NULL\n");
1668 rdata = rport->dd_data;
1671 printk(KERN_ERR "fnic_terminate_rport_io: rdata is NULL\n");
1674 lport = rdata->local_port;
1677 printk(KERN_ERR "fnic_terminate_rport_io: lport is NULL\n");
1680 fnic = lport_priv(lport);
1681 FNIC_SCSI_DBG(KERN_DEBUG,
1682 fnic->lport->host, "fnic_terminate_rport_io called"
1683 " wwpn 0x%llx, wwnn0x%llx, rport 0x%p, portid 0x%06x\n",
1684 rport->port_name, rport->node_name, rport,
1687 if (fnic->in_remove)
1690 reset_stats = &fnic->fnic_stats.reset_stats;
1691 term_stats = &fnic->fnic_stats.term_stats;
1693 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
1695 io_lock = fnic_io_lock_tag(fnic, tag);
1696 spin_lock_irqsave(io_lock, flags);
1697 sc = scsi_host_find_tag(fnic->lport->host, tag);
1699 spin_unlock_irqrestore(io_lock, flags);
1703 cmd_rport = starget_to_rport(scsi_target(sc->device));
1704 if (rport != cmd_rport) {
1705 spin_unlock_irqrestore(io_lock, flags);
1709 io_req = (struct fnic_io_req *)CMD_SP(sc);
1711 if (!io_req || rport != cmd_rport) {
1712 spin_unlock_irqrestore(io_lock, flags);
1716 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1717 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
1718 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1719 "fnic_terminate_rport_io dev rst not pending sc 0x%p\n",
1721 spin_unlock_irqrestore(io_lock, flags);
1725 * Found IO that is still pending with firmware and
1726 * belongs to rport that went away
1728 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1729 spin_unlock_irqrestore(io_lock, flags);
1732 if (io_req->abts_done) {
1733 shost_printk(KERN_ERR, fnic->lport->host,
1734 "fnic_terminate_rport_io: io_req->abts_done is set "
1736 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1738 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1739 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1740 "fnic_terminate_rport_io "
1741 "IO not yet issued %p tag 0x%x flags "
1743 sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1745 old_ioreq_state = CMD_STATE(sc);
1746 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1747 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1748 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1749 atomic64_inc(&reset_stats->device_reset_terminates);
1750 abt_tag = (tag | FNIC_TAG_DEV_RST);
1751 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1752 "fnic_terminate_rport_io dev rst sc 0x%p\n", sc);
1755 BUG_ON(io_req->abts_done);
1757 FNIC_SCSI_DBG(KERN_DEBUG,
1759 "fnic_terminate_rport_io: Issuing abts\n");
1761 spin_unlock_irqrestore(io_lock, flags);
1763 /* Now queue the abort command to firmware */
1764 int_to_scsilun(sc->device->lun, &fc_lun);
1766 if (fnic_queue_abort_io_req(fnic, abt_tag,
1767 FCPIO_ITMF_ABT_TASK_TERM,
1768 fc_lun.scsi_lun, io_req)) {
1770 * Revert the cmd state back to old state, if
1771 * it hasn't changed in between. This cmd will get
1772 * aborted later by scsi_eh, or cleaned up during
1775 spin_lock_irqsave(io_lock, flags);
1776 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1777 CMD_STATE(sc) = old_ioreq_state;
1778 spin_unlock_irqrestore(io_lock, flags);
1780 spin_lock_irqsave(io_lock, flags);
1781 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1782 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
1784 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
1785 spin_unlock_irqrestore(io_lock, flags);
1786 atomic64_inc(&term_stats->terminates);
1790 if (term_cnt > atomic64_read(&term_stats->max_terminates))
1791 atomic64_set(&term_stats->max_terminates, term_cnt);
1796 * This function is exported to SCSI for sending abort cmnds.
1797 * A SCSI IO is represented by a io_req in the driver.
1798 * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
1800 int fnic_abort_cmd(struct scsi_cmnd *sc)
1802 struct fc_lport *lp;
1804 struct fnic_io_req *io_req = NULL;
1805 struct fc_rport *rport;
1806 spinlock_t *io_lock;
1807 unsigned long flags;
1808 unsigned long start_time = 0;
1811 struct scsi_lun fc_lun;
1812 struct fnic_stats *fnic_stats;
1813 struct abort_stats *abts_stats;
1814 struct terminate_stats *term_stats;
1815 enum fnic_ioreq_state old_ioreq_state;
1817 unsigned long abt_issued_time;
1818 DECLARE_COMPLETION_ONSTACK(tm_done);
1820 /* Wait for rport to unblock */
1821 fc_block_scsi_eh(sc);
1823 /* Get local-port, check ready and link up */
1824 lp = shost_priv(sc->device->host);
1826 fnic = lport_priv(lp);
1827 fnic_stats = &fnic->fnic_stats;
1828 abts_stats = &fnic->fnic_stats.abts_stats;
1829 term_stats = &fnic->fnic_stats.term_stats;
1831 rport = starget_to_rport(scsi_target(sc->device));
1832 tag = sc->request->tag;
1833 FNIC_SCSI_DBG(KERN_DEBUG,
1835 "Abort Cmd called FCID 0x%x, LUN 0x%llx TAG %x flags %x\n",
1836 rport->port_id, sc->device->lun, tag, CMD_FLAGS(sc));
1838 CMD_FLAGS(sc) = FNIC_NO_FLAGS;
1840 if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
1842 goto fnic_abort_cmd_end;
1846 * Avoid a race between SCSI issuing the abort and the device
1847 * completing the command.
1849 * If the command is already completed by the fw cmpl code,
1850 * we just return SUCCESS from here. This means that the abort
1851 * succeeded. In the SCSI ML, since the timeout for command has
1852 * happened, the completion wont actually complete the command
1853 * and it will be considered as an aborted command
1855 * The CMD_SP will not be cleared except while holding io_req_lock.
1857 io_lock = fnic_io_lock_hash(fnic, sc);
1858 spin_lock_irqsave(io_lock, flags);
1859 io_req = (struct fnic_io_req *)CMD_SP(sc);
1861 spin_unlock_irqrestore(io_lock, flags);
1862 goto fnic_abort_cmd_end;
1865 io_req->abts_done = &tm_done;
1867 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1868 spin_unlock_irqrestore(io_lock, flags);
1872 abt_issued_time = jiffies_to_msecs(jiffies) - jiffies_to_msecs(io_req->start_time);
1873 if (abt_issued_time <= 6000)
1874 atomic64_inc(&abts_stats->abort_issued_btw_0_to_6_sec);
1875 else if (abt_issued_time > 6000 && abt_issued_time <= 20000)
1876 atomic64_inc(&abts_stats->abort_issued_btw_6_to_20_sec);
1877 else if (abt_issued_time > 20000 && abt_issued_time <= 30000)
1878 atomic64_inc(&abts_stats->abort_issued_btw_20_to_30_sec);
1879 else if (abt_issued_time > 30000 && abt_issued_time <= 40000)
1880 atomic64_inc(&abts_stats->abort_issued_btw_30_to_40_sec);
1881 else if (abt_issued_time > 40000 && abt_issued_time <= 50000)
1882 atomic64_inc(&abts_stats->abort_issued_btw_40_to_50_sec);
1883 else if (abt_issued_time > 50000 && abt_issued_time <= 60000)
1884 atomic64_inc(&abts_stats->abort_issued_btw_50_to_60_sec);
1886 atomic64_inc(&abts_stats->abort_issued_greater_than_60_sec);
1888 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1889 "CBD Opcode: %02x Abort issued time: %lu msec\n", sc->cmnd[0], abt_issued_time);
1891 * Command is still pending, need to abort it
1892 * If the firmware completes the command after this point,
1893 * the completion wont be done till mid-layer, since abort
1894 * has already started.
1896 old_ioreq_state = CMD_STATE(sc);
1897 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1898 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1900 spin_unlock_irqrestore(io_lock, flags);
1903 * Check readiness of the remote port. If the path to remote
1904 * port is up, then send abts to the remote port to terminate
1905 * the IO. Else, just locally terminate the IO in the firmware
1907 if (fc_remote_port_chkready(rport) == 0)
1908 task_req = FCPIO_ITMF_ABT_TASK;
1910 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
1911 task_req = FCPIO_ITMF_ABT_TASK_TERM;
1914 /* Now queue the abort command to firmware */
1915 int_to_scsilun(sc->device->lun, &fc_lun);
1917 if (fnic_queue_abort_io_req(fnic, sc->request->tag, task_req,
1918 fc_lun.scsi_lun, io_req)) {
1919 spin_lock_irqsave(io_lock, flags);
1920 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1921 CMD_STATE(sc) = old_ioreq_state;
1922 io_req = (struct fnic_io_req *)CMD_SP(sc);
1924 io_req->abts_done = NULL;
1925 spin_unlock_irqrestore(io_lock, flags);
1927 goto fnic_abort_cmd_end;
1929 if (task_req == FCPIO_ITMF_ABT_TASK) {
1930 CMD_FLAGS(sc) |= FNIC_IO_ABTS_ISSUED;
1931 atomic64_inc(&fnic_stats->abts_stats.aborts);
1933 CMD_FLAGS(sc) |= FNIC_IO_TERM_ISSUED;
1934 atomic64_inc(&fnic_stats->term_stats.terminates);
1938 * We queued an abort IO, wait for its completion.
1939 * Once the firmware completes the abort command, it will
1940 * wake up this thread.
1943 wait_for_completion_timeout(&tm_done,
1945 (2 * fnic->config.ra_tov +
1946 fnic->config.ed_tov));
1948 /* Check the abort status */
1949 spin_lock_irqsave(io_lock, flags);
1951 io_req = (struct fnic_io_req *)CMD_SP(sc);
1953 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
1954 spin_unlock_irqrestore(io_lock, flags);
1955 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
1957 goto fnic_abort_cmd_end;
1959 io_req->abts_done = NULL;
1961 /* fw did not complete abort, timed out */
1962 if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
1963 spin_unlock_irqrestore(io_lock, flags);
1964 if (task_req == FCPIO_ITMF_ABT_TASK) {
1965 atomic64_inc(&abts_stats->abort_drv_timeouts);
1967 atomic64_inc(&term_stats->terminate_drv_timeouts);
1969 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_TIMED_OUT;
1971 goto fnic_abort_cmd_end;
1974 /* IO out of order */
1976 if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE))) {
1977 spin_unlock_irqrestore(io_lock, flags);
1978 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1979 "Issuing Host reset due to out of order IO\n");
1981 if (fnic_host_reset(sc) == FAILED) {
1982 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1983 "fnic_host_reset failed.\n");
1986 goto fnic_abort_cmd_end;
1989 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1991 start_time = io_req->start_time;
1993 * firmware completed the abort, check the status,
1994 * free the io_req if successful. If abort fails,
1995 * Device reset will clean the I/O.
1997 if (CMD_ABTS_STATUS(sc) == FCPIO_SUCCESS)
2001 spin_unlock_irqrestore(io_lock, flags);
2002 goto fnic_abort_cmd_end;
2005 spin_unlock_irqrestore(io_lock, flags);
2007 fnic_release_ioreq_buf(fnic, io_req, sc);
2008 mempool_free(io_req, fnic->io_req_pool);
2010 if (sc->scsi_done) {
2011 /* Call SCSI completion function to complete the IO */
2012 sc->result = (DID_ABORT << 16);
2014 atomic64_dec(&fnic_stats->io_stats.active_ios);
2015 if (atomic64_read(&fnic->io_cmpl_skip))
2016 atomic64_dec(&fnic->io_cmpl_skip);
2018 atomic64_inc(&fnic_stats->io_stats.io_completions);
2022 FNIC_TRACE(fnic_abort_cmd, sc->device->host->host_no,
2023 sc->request->tag, sc,
2024 jiffies_to_msecs(jiffies - start_time),
2025 0, ((u64)sc->cmnd[0] << 32 |
2026 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
2027 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
2028 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
2030 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2031 "Returning from abort cmd type %x %s\n", task_req,
2033 "SUCCESS" : "FAILED");
2037 static inline int fnic_queue_dr_io_req(struct fnic *fnic,
2038 struct scsi_cmnd *sc,
2039 struct fnic_io_req *io_req)
2041 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
2042 struct Scsi_Host *host = fnic->lport->host;
2043 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
2044 struct scsi_lun fc_lun;
2046 unsigned long intr_flags;
2048 spin_lock_irqsave(host->host_lock, intr_flags);
2049 if (unlikely(fnic_chk_state_flags_locked(fnic,
2050 FNIC_FLAGS_IO_BLOCKED))) {
2051 spin_unlock_irqrestore(host->host_lock, intr_flags);
2054 atomic_inc(&fnic->in_flight);
2055 spin_unlock_irqrestore(host->host_lock, intr_flags);
2057 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
2059 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
2060 free_wq_copy_descs(fnic, wq);
2062 if (!vnic_wq_copy_desc_avail(wq)) {
2063 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2064 "queue_dr_io_req failure - no descriptors\n");
2065 atomic64_inc(&misc_stats->devrst_cpwq_alloc_failures);
2070 /* fill in the lun info */
2071 int_to_scsilun(sc->device->lun, &fc_lun);
2073 fnic_queue_wq_copy_desc_itmf(wq, sc->request->tag | FNIC_TAG_DEV_RST,
2074 0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
2075 fc_lun.scsi_lun, io_req->port_id,
2076 fnic->config.ra_tov, fnic->config.ed_tov);
2078 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
2079 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
2080 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
2081 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
2082 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
2085 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
2086 atomic_dec(&fnic->in_flight);
2092 * Clean up any pending aborts on the lun
2093 * For each outstanding IO on this lun, whose abort is not completed by fw,
2094 * issue a local abort. Wait for abort to complete. Return 0 if all commands
2095 * successfully aborted, 1 otherwise
2097 static int fnic_clean_pending_aborts(struct fnic *fnic,
2098 struct scsi_cmnd *lr_sc,
2103 struct fnic_io_req *io_req;
2104 spinlock_t *io_lock;
2105 unsigned long flags;
2107 struct scsi_cmnd *sc;
2108 struct scsi_lun fc_lun;
2109 struct scsi_device *lun_dev = lr_sc->device;
2110 DECLARE_COMPLETION_ONSTACK(tm_done);
2111 enum fnic_ioreq_state old_ioreq_state;
2113 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
2114 io_lock = fnic_io_lock_tag(fnic, tag);
2115 spin_lock_irqsave(io_lock, flags);
2116 sc = scsi_host_find_tag(fnic->lport->host, tag);
2118 * ignore this lun reset cmd if issued using new SC
2119 * or cmds that do not belong to this lun
2121 if (!sc || ((sc == lr_sc) && new_sc) || sc->device != lun_dev) {
2122 spin_unlock_irqrestore(io_lock, flags);
2126 io_req = (struct fnic_io_req *)CMD_SP(sc);
2128 if (!io_req || sc->device != lun_dev) {
2129 spin_unlock_irqrestore(io_lock, flags);
2134 * Found IO that is still pending with firmware and
2135 * belongs to the LUN that we are resetting
2137 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2138 "Found IO in %s on lun\n",
2139 fnic_ioreq_state_to_str(CMD_STATE(sc)));
2141 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
2142 spin_unlock_irqrestore(io_lock, flags);
2145 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
2146 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
2147 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2148 "%s dev rst not pending sc 0x%p\n", __func__,
2150 spin_unlock_irqrestore(io_lock, flags);
2154 if (io_req->abts_done)
2155 shost_printk(KERN_ERR, fnic->lport->host,
2156 "%s: io_req->abts_done is set state is %s\n",
2157 __func__, fnic_ioreq_state_to_str(CMD_STATE(sc)));
2158 old_ioreq_state = CMD_STATE(sc);
2160 * Any pending IO issued prior to reset is expected to be
2161 * in abts pending state, if not we need to set
2162 * FNIC_IOREQ_ABTS_PENDING to indicate the IO is abort pending.
2163 * When IO is completed, the IO will be handed over and
2164 * handled in this function.
2166 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2168 BUG_ON(io_req->abts_done);
2171 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
2172 abt_tag |= FNIC_TAG_DEV_RST;
2173 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2174 "%s: dev rst sc 0x%p\n", __func__, sc);
2177 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
2178 io_req->abts_done = &tm_done;
2179 spin_unlock_irqrestore(io_lock, flags);
2181 /* Now queue the abort command to firmware */
2182 int_to_scsilun(sc->device->lun, &fc_lun);
2184 if (fnic_queue_abort_io_req(fnic, abt_tag,
2185 FCPIO_ITMF_ABT_TASK_TERM,
2186 fc_lun.scsi_lun, io_req)) {
2187 spin_lock_irqsave(io_lock, flags);
2188 io_req = (struct fnic_io_req *)CMD_SP(sc);
2190 io_req->abts_done = NULL;
2191 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
2192 CMD_STATE(sc) = old_ioreq_state;
2193 spin_unlock_irqrestore(io_lock, flags);
2195 goto clean_pending_aborts_end;
2197 spin_lock_irqsave(io_lock, flags);
2198 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
2199 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2200 spin_unlock_irqrestore(io_lock, flags);
2202 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
2204 wait_for_completion_timeout(&tm_done,
2206 (fnic->config.ed_tov));
2208 /* Recheck cmd state to check if it is now aborted */
2209 spin_lock_irqsave(io_lock, flags);
2210 io_req = (struct fnic_io_req *)CMD_SP(sc);
2212 spin_unlock_irqrestore(io_lock, flags);
2213 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
2217 io_req->abts_done = NULL;
2219 /* if abort is still pending with fw, fail */
2220 if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
2221 spin_unlock_irqrestore(io_lock, flags);
2222 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
2224 goto clean_pending_aborts_end;
2226 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
2228 /* original sc used for lr is handled by dev reset code */
2231 spin_unlock_irqrestore(io_lock, flags);
2233 /* original sc used for lr is handled by dev reset code */
2235 fnic_release_ioreq_buf(fnic, io_req, sc);
2236 mempool_free(io_req, fnic->io_req_pool);
2240 * Any IO is returned during reset, it needs to call scsi_done
2241 * to return the scsi_cmnd to upper layer.
2243 if (sc->scsi_done) {
2244 /* Set result to let upper SCSI layer retry */
2245 sc->result = DID_RESET << 16;
2250 schedule_timeout(msecs_to_jiffies(2 * fnic->config.ed_tov));
2252 /* walk again to check, if IOs are still pending in fw */
2253 if (fnic_is_abts_pending(fnic, lr_sc))
2256 clean_pending_aborts_end:
2261 * fnic_scsi_host_start_tag
2262 * Allocates tagid from host's tag list
2265 fnic_scsi_host_start_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2267 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2268 int tag, ret = SCSI_NO_TAG;
2272 pr_err("Tags are not supported\n");
2277 tag = find_next_zero_bit(bqt->tag_map, bqt->max_depth, 1);
2278 if (tag >= bqt->max_depth) {
2279 pr_err("Tag allocation failure\n");
2282 } while (test_and_set_bit(tag, bqt->tag_map));
2284 bqt->tag_index[tag] = sc->request;
2285 sc->request->tag = tag;
2287 if (!sc->request->special)
2288 sc->request->special = sc;
2297 * fnic_scsi_host_end_tag
2298 * frees tag allocated by fnic_scsi_host_start_tag.
2301 fnic_scsi_host_end_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2303 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2304 int tag = sc->request->tag;
2306 if (tag == SCSI_NO_TAG)
2309 BUG_ON(!bqt || !bqt->tag_index[tag]);
2313 bqt->tag_index[tag] = NULL;
2314 clear_bit(tag, bqt->tag_map);
2320 * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
2321 * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
2324 int fnic_device_reset(struct scsi_cmnd *sc)
2326 struct fc_lport *lp;
2328 struct fnic_io_req *io_req = NULL;
2329 struct fc_rport *rport;
2332 spinlock_t *io_lock;
2333 unsigned long flags;
2334 unsigned long start_time = 0;
2335 struct scsi_lun fc_lun;
2336 struct fnic_stats *fnic_stats;
2337 struct reset_stats *reset_stats;
2339 DECLARE_COMPLETION_ONSTACK(tm_done);
2340 int tag_gen_flag = 0; /*to track tags allocated by fnic driver*/
2343 /* Wait for rport to unblock */
2344 fc_block_scsi_eh(sc);
2346 /* Get local-port, check ready and link up */
2347 lp = shost_priv(sc->device->host);
2349 fnic = lport_priv(lp);
2350 fnic_stats = &fnic->fnic_stats;
2351 reset_stats = &fnic->fnic_stats.reset_stats;
2353 atomic64_inc(&reset_stats->device_resets);
2355 rport = starget_to_rport(scsi_target(sc->device));
2356 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2357 "Device reset called FCID 0x%x, LUN 0x%llx sc 0x%p\n",
2358 rport->port_id, sc->device->lun, sc);
2360 if (lp->state != LPORT_ST_READY || !(lp->link_up))
2361 goto fnic_device_reset_end;
2363 /* Check if remote port up */
2364 if (fc_remote_port_chkready(rport)) {
2365 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
2366 goto fnic_device_reset_end;
2369 CMD_FLAGS(sc) = FNIC_DEVICE_RESET;
2370 /* Allocate tag if not present */
2372 tag = sc->request->tag;
2373 if (unlikely(tag < 0)) {
2375 * XXX(hch): current the midlayer fakes up a struct
2376 * request for the explicit reset ioctls, and those
2377 * don't have a tag allocated to them. The below
2378 * code pokes into midlayer structures to paper over
2379 * this design issue, but that won't work for blk-mq.
2381 * Either someone who can actually test the hardware
2382 * will have to come up with a similar hack for the
2383 * blk-mq case, or we'll have to bite the bullet and
2384 * fix the way the EH ioctls work for real, but until
2385 * that happens we fail these explicit requests here.
2388 tag = fnic_scsi_host_start_tag(fnic, sc);
2389 if (unlikely(tag == SCSI_NO_TAG))
2390 goto fnic_device_reset_end;
2394 io_lock = fnic_io_lock_hash(fnic, sc);
2395 spin_lock_irqsave(io_lock, flags);
2396 io_req = (struct fnic_io_req *)CMD_SP(sc);
2399 * If there is a io_req attached to this command, then use it,
2400 * else allocate a new one.
2403 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
2405 spin_unlock_irqrestore(io_lock, flags);
2406 goto fnic_device_reset_end;
2408 memset(io_req, 0, sizeof(*io_req));
2409 io_req->port_id = rport->port_id;
2410 CMD_SP(sc) = (char *)io_req;
2412 io_req->dr_done = &tm_done;
2413 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
2414 CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
2415 spin_unlock_irqrestore(io_lock, flags);
2417 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %x\n", tag);
2420 * issue the device reset, if enqueue failed, clean up the ioreq
2421 * and break assoc with scsi cmd
2423 if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
2424 spin_lock_irqsave(io_lock, flags);
2425 io_req = (struct fnic_io_req *)CMD_SP(sc);
2427 io_req->dr_done = NULL;
2428 goto fnic_device_reset_clean;
2430 spin_lock_irqsave(io_lock, flags);
2431 CMD_FLAGS(sc) |= FNIC_DEV_RST_ISSUED;
2432 spin_unlock_irqrestore(io_lock, flags);
2435 * Wait on the local completion for LUN reset. The io_req may be
2436 * freed while we wait since we hold no lock.
2438 wait_for_completion_timeout(&tm_done,
2439 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2441 spin_lock_irqsave(io_lock, flags);
2442 io_req = (struct fnic_io_req *)CMD_SP(sc);
2444 spin_unlock_irqrestore(io_lock, flags);
2445 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2446 "io_req is null tag 0x%x sc 0x%p\n", tag, sc);
2447 goto fnic_device_reset_end;
2449 io_req->dr_done = NULL;
2451 status = CMD_LR_STATUS(sc);
2454 * If lun reset not completed, bail out with failed. io_req
2455 * gets cleaned up during higher levels of EH
2457 if (status == FCPIO_INVALID_CODE) {
2458 atomic64_inc(&reset_stats->device_reset_timeouts);
2459 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2460 "Device reset timed out\n");
2461 CMD_FLAGS(sc) |= FNIC_DEV_RST_TIMED_OUT;
2462 spin_unlock_irqrestore(io_lock, flags);
2463 int_to_scsilun(sc->device->lun, &fc_lun);
2465 * Issue abort and terminate on device reset request.
2466 * If q'ing of terminate fails, retry it after a delay.
2469 spin_lock_irqsave(io_lock, flags);
2470 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TERM_ISSUED) {
2471 spin_unlock_irqrestore(io_lock, flags);
2474 spin_unlock_irqrestore(io_lock, flags);
2475 if (fnic_queue_abort_io_req(fnic,
2476 tag | FNIC_TAG_DEV_RST,
2477 FCPIO_ITMF_ABT_TASK_TERM,
2478 fc_lun.scsi_lun, io_req)) {
2479 wait_for_completion_timeout(&tm_done,
2480 msecs_to_jiffies(FNIC_ABT_TERM_DELAY_TIMEOUT));
2482 spin_lock_irqsave(io_lock, flags);
2483 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2484 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2485 io_req->abts_done = &tm_done;
2486 spin_unlock_irqrestore(io_lock, flags);
2487 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2488 "Abort and terminate issued on Device reset "
2489 "tag 0x%x sc 0x%p\n", tag, sc);
2494 spin_lock_irqsave(io_lock, flags);
2495 if (!(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
2496 spin_unlock_irqrestore(io_lock, flags);
2497 wait_for_completion_timeout(&tm_done,
2498 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2501 io_req = (struct fnic_io_req *)CMD_SP(sc);
2502 io_req->abts_done = NULL;
2503 goto fnic_device_reset_clean;
2507 spin_unlock_irqrestore(io_lock, flags);
2510 /* Completed, but not successful, clean up the io_req, return fail */
2511 if (status != FCPIO_SUCCESS) {
2512 spin_lock_irqsave(io_lock, flags);
2513 FNIC_SCSI_DBG(KERN_DEBUG,
2515 "Device reset completed - failed\n");
2516 io_req = (struct fnic_io_req *)CMD_SP(sc);
2517 goto fnic_device_reset_clean;
2521 * Clean up any aborts on this lun that have still not
2522 * completed. If any of these fail, then LUN reset fails.
2523 * clean_pending_aborts cleans all cmds on this lun except
2524 * the lun reset cmd. If all cmds get cleaned, the lun reset
2527 if (fnic_clean_pending_aborts(fnic, sc, new_sc)) {
2528 spin_lock_irqsave(io_lock, flags);
2529 io_req = (struct fnic_io_req *)CMD_SP(sc);
2530 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2531 "Device reset failed"
2532 " since could not abort all IOs\n");
2533 goto fnic_device_reset_clean;
2536 /* Clean lun reset command */
2537 spin_lock_irqsave(io_lock, flags);
2538 io_req = (struct fnic_io_req *)CMD_SP(sc);
2540 /* Completed, and successful */
2543 fnic_device_reset_clean:
2547 spin_unlock_irqrestore(io_lock, flags);
2550 start_time = io_req->start_time;
2551 fnic_release_ioreq_buf(fnic, io_req, sc);
2552 mempool_free(io_req, fnic->io_req_pool);
2555 fnic_device_reset_end:
2556 FNIC_TRACE(fnic_device_reset, sc->device->host->host_no,
2557 sc->request->tag, sc,
2558 jiffies_to_msecs(jiffies - start_time),
2559 0, ((u64)sc->cmnd[0] << 32 |
2560 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
2561 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
2562 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
2564 /* free tag if it is allocated */
2565 if (unlikely(tag_gen_flag))
2566 fnic_scsi_host_end_tag(fnic, sc);
2568 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2569 "Returning from device reset %s\n",
2571 "SUCCESS" : "FAILED");
2574 atomic64_inc(&reset_stats->device_reset_failures);
2579 /* Clean up all IOs, clean up libFC local port */
2580 int fnic_reset(struct Scsi_Host *shost)
2582 struct fc_lport *lp;
2585 struct reset_stats *reset_stats;
2587 lp = shost_priv(shost);
2588 fnic = lport_priv(lp);
2589 reset_stats = &fnic->fnic_stats.reset_stats;
2591 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2592 "fnic_reset called\n");
2594 atomic64_inc(&reset_stats->fnic_resets);
2597 * Reset local port, this will clean up libFC exchanges,
2598 * reset remote port sessions, and if link is up, begin flogi
2600 ret = fc_lport_reset(lp);
2602 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2603 "Returning from fnic reset %s\n",
2605 "SUCCESS" : "FAILED");
2608 atomic64_inc(&reset_stats->fnic_reset_completions);
2610 atomic64_inc(&reset_stats->fnic_reset_failures);
2616 * SCSI Error handling calls driver's eh_host_reset if all prior
2617 * error handling levels return FAILED. If host reset completes
2618 * successfully, and if link is up, then Fabric login begins.
2620 * Host Reset is the highest level of error recovery. If this fails, then
2621 * host is offlined by SCSI.
2624 int fnic_host_reset(struct scsi_cmnd *sc)
2627 unsigned long wait_host_tmo;
2628 struct Scsi_Host *shost = sc->device->host;
2629 struct fc_lport *lp = shost_priv(shost);
2630 struct fnic *fnic = lport_priv(lp);
2631 unsigned long flags;
2633 spin_lock_irqsave(&fnic->fnic_lock, flags);
2634 if (fnic->internal_reset_inprogress == 0) {
2635 fnic->internal_reset_inprogress = 1;
2637 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2638 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2639 "host reset in progress skipping another host reset\n");
2642 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2645 * If fnic_reset is successful, wait for fabric login to complete
2646 * scsi-ml tries to send a TUR to every device if host reset is
2647 * successful, so before returning to scsi, fabric should be up
2649 ret = (fnic_reset(shost) == 0) ? SUCCESS : FAILED;
2650 if (ret == SUCCESS) {
2651 wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
2653 while (time_before(jiffies, wait_host_tmo)) {
2654 if ((lp->state == LPORT_ST_READY) &&
2663 spin_lock_irqsave(&fnic->fnic_lock, flags);
2664 fnic->internal_reset_inprogress = 0;
2665 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2670 * This fxn is called from libFC when host is removed
2672 void fnic_scsi_abort_io(struct fc_lport *lp)
2675 unsigned long flags;
2676 enum fnic_state old_state;
2677 struct fnic *fnic = lport_priv(lp);
2678 DECLARE_COMPLETION_ONSTACK(remove_wait);
2680 /* Issue firmware reset for fnic, wait for reset to complete */
2682 spin_lock_irqsave(&fnic->fnic_lock, flags);
2683 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2684 /* fw reset is in progress, poll for its completion */
2685 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2686 schedule_timeout(msecs_to_jiffies(100));
2687 goto retry_fw_reset;
2690 fnic->remove_wait = &remove_wait;
2691 old_state = fnic->state;
2692 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
2693 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
2694 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2696 err = fnic_fw_reset_handler(fnic);
2698 spin_lock_irqsave(&fnic->fnic_lock, flags);
2699 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2700 fnic->state = old_state;
2701 fnic->remove_wait = NULL;
2702 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2706 /* Wait for firmware reset to complete */
2707 wait_for_completion_timeout(&remove_wait,
2708 msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
2710 spin_lock_irqsave(&fnic->fnic_lock, flags);
2711 fnic->remove_wait = NULL;
2712 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2713 "fnic_scsi_abort_io %s\n",
2714 (fnic->state == FNIC_IN_ETH_MODE) ?
2715 "SUCCESS" : "FAILED");
2716 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2721 * This fxn called from libFC to clean up driver IO state on link down
2723 void fnic_scsi_cleanup(struct fc_lport *lp)
2725 unsigned long flags;
2726 enum fnic_state old_state;
2727 struct fnic *fnic = lport_priv(lp);
2729 /* issue fw reset */
2731 spin_lock_irqsave(&fnic->fnic_lock, flags);
2732 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2733 /* fw reset is in progress, poll for its completion */
2734 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2735 schedule_timeout(msecs_to_jiffies(100));
2736 goto retry_fw_reset;
2738 old_state = fnic->state;
2739 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
2740 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
2741 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2743 if (fnic_fw_reset_handler(fnic)) {
2744 spin_lock_irqsave(&fnic->fnic_lock, flags);
2745 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2746 fnic->state = old_state;
2747 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2752 void fnic_empty_scsi_cleanup(struct fc_lport *lp)
2756 void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
2758 struct fnic *fnic = lport_priv(lp);
2760 /* Non-zero sid, nothing to do */
2762 goto call_fc_exch_mgr_reset;
2765 fnic_rport_exch_reset(fnic, did);
2766 goto call_fc_exch_mgr_reset;
2771 * link down or device being removed
2773 if (!fnic->in_remove)
2774 fnic_scsi_cleanup(lp);
2776 fnic_scsi_abort_io(lp);
2778 /* call libFC exch mgr reset to reset its exchanges */
2779 call_fc_exch_mgr_reset:
2780 fc_exch_mgr_reset(lp, sid, did);
2785 * fnic_is_abts_pending() is a helper function that
2786 * walks through tag map to check if there is any IOs pending,if there is one,
2787 * then it returns 1 (true), otherwise 0 (false)
2788 * if @lr_sc is non NULL, then it checks IOs specific to particular LUN,
2789 * otherwise, it checks for all IOs.
2791 int fnic_is_abts_pending(struct fnic *fnic, struct scsi_cmnd *lr_sc)
2794 struct fnic_io_req *io_req;
2795 spinlock_t *io_lock;
2796 unsigned long flags;
2798 struct scsi_cmnd *sc;
2799 struct scsi_device *lun_dev = NULL;
2802 lun_dev = lr_sc->device;
2804 /* walk again to check, if IOs are still pending in fw */
2805 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
2806 sc = scsi_host_find_tag(fnic->lport->host, tag);
2808 * ignore this lun reset cmd or cmds that do not belong to
2811 if (!sc || (lr_sc && (sc->device != lun_dev || sc == lr_sc)))
2814 io_lock = fnic_io_lock_hash(fnic, sc);
2815 spin_lock_irqsave(io_lock, flags);
2817 io_req = (struct fnic_io_req *)CMD_SP(sc);
2819 if (!io_req || sc->device != lun_dev) {
2820 spin_unlock_irqrestore(io_lock, flags);
2825 * Found IO that is still pending with firmware and
2826 * belongs to the LUN that we are resetting
2828 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2829 "Found IO in %s on lun\n",
2830 fnic_ioreq_state_to_str(CMD_STATE(sc)));
2832 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
2834 spin_unlock_irqrestore(io_lock, flags);