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;
445 if (unlikely(fnic_chk_state_flags_locked(fnic, FNIC_FLAGS_IO_BLOCKED)))
446 return SCSI_MLQUEUE_HOST_BUSY;
448 rport = starget_to_rport(scsi_target(sc->device));
449 ret = fc_remote_port_chkready(rport);
451 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
458 struct fc_rport_libfc_priv *rp = rport->dd_data;
460 if (!rp || rp->rp_state != RPORT_ST_READY) {
461 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
462 "returning DID_NO_CONNECT for IO as rport is removed\n");
463 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
464 sc->result = DID_NO_CONNECT<<16;
470 if (lp->state != LPORT_ST_READY || !(lp->link_up))
471 return SCSI_MLQUEUE_HOST_BUSY;
473 atomic_inc(&fnic->in_flight);
476 * Release host lock, use driver resource specific locks from here.
477 * Don't re-enable interrupts in case they were disabled prior to the
478 * caller disabling them.
480 spin_unlock(lp->host->host_lock);
481 CMD_STATE(sc) = FNIC_IOREQ_NOT_INITED;
482 CMD_FLAGS(sc) = FNIC_NO_FLAGS;
484 /* Get a new io_req for this SCSI IO */
485 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
487 atomic64_inc(&fnic_stats->io_stats.alloc_failures);
488 ret = SCSI_MLQUEUE_HOST_BUSY;
491 memset(io_req, 0, sizeof(*io_req));
493 /* Map the data buffer */
494 sg_count = scsi_dma_map(sc);
496 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
497 sc->request->tag, sc, 0, sc->cmnd[0],
498 sg_count, CMD_STATE(sc));
499 mempool_free(io_req, fnic->io_req_pool);
503 /* Determine the type of scatter/gather list we need */
504 io_req->sgl_cnt = sg_count;
505 io_req->sgl_type = FNIC_SGL_CACHE_DFLT;
506 if (sg_count > FNIC_DFLT_SG_DESC_CNT)
507 io_req->sgl_type = FNIC_SGL_CACHE_MAX;
511 mempool_alloc(fnic->io_sgl_pool[io_req->sgl_type],
513 if (!io_req->sgl_list) {
514 atomic64_inc(&fnic_stats->io_stats.alloc_failures);
515 ret = SCSI_MLQUEUE_HOST_BUSY;
517 mempool_free(io_req, fnic->io_req_pool);
521 /* Cache sgl list allocated address before alignment */
522 io_req->sgl_list_alloc = io_req->sgl_list;
523 ptr = (unsigned long) io_req->sgl_list;
524 if (ptr % FNIC_SG_DESC_ALIGN) {
525 io_req->sgl_list = (struct host_sg_desc *)
526 (((unsigned long) ptr
527 + FNIC_SG_DESC_ALIGN - 1)
528 & ~(FNIC_SG_DESC_ALIGN - 1));
533 * Will acquire lock defore setting to IO initialized.
536 io_lock = fnic_io_lock_hash(fnic, sc);
537 spin_lock_irqsave(io_lock, flags);
539 /* initialize rest of io_req */
540 io_lock_acquired = 1;
541 io_req->port_id = rport->port_id;
542 io_req->start_time = jiffies;
543 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
544 CMD_SP(sc) = (char *)io_req;
545 CMD_FLAGS(sc) |= FNIC_IO_INITIALIZED;
546 sc->scsi_done = done;
548 /* create copy wq desc and enqueue it */
549 wq = &fnic->wq_copy[0];
550 ret = fnic_queue_wq_copy_desc(fnic, wq, io_req, sc, sg_count);
553 * In case another thread cancelled the request,
554 * refetch the pointer under the lock.
556 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
557 sc->request->tag, sc, 0, 0, 0,
558 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
559 io_req = (struct fnic_io_req *)CMD_SP(sc);
561 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
562 spin_unlock_irqrestore(io_lock, flags);
564 fnic_release_ioreq_buf(fnic, io_req, sc);
565 mempool_free(io_req, fnic->io_req_pool);
567 atomic_dec(&fnic->in_flight);
568 /* acquire host lock before returning to SCSI */
569 spin_lock(lp->host->host_lock);
572 atomic64_inc(&fnic_stats->io_stats.active_ios);
573 atomic64_inc(&fnic_stats->io_stats.num_ios);
574 if (atomic64_read(&fnic_stats->io_stats.active_ios) >
575 atomic64_read(&fnic_stats->io_stats.max_active_ios))
576 atomic64_set(&fnic_stats->io_stats.max_active_ios,
577 atomic64_read(&fnic_stats->io_stats.active_ios));
579 /* REVISIT: Use per IO lock in the final code */
580 CMD_FLAGS(sc) |= FNIC_IO_ISSUED;
583 cmd_trace = ((u64)sc->cmnd[0] << 56 | (u64)sc->cmnd[7] << 40 |
584 (u64)sc->cmnd[8] << 32 | (u64)sc->cmnd[2] << 24 |
585 (u64)sc->cmnd[3] << 16 | (u64)sc->cmnd[4] << 8 |
588 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
589 sc->request->tag, sc, io_req,
591 (((u64)CMD_FLAGS(sc) >> 32) | CMD_STATE(sc)));
593 /* if only we issued IO, will we have the io lock */
594 if (io_lock_acquired)
595 spin_unlock_irqrestore(io_lock, flags);
597 atomic_dec(&fnic->in_flight);
598 /* acquire host lock before returning to SCSI */
599 spin_lock(lp->host->host_lock);
603 DEF_SCSI_QCMD(fnic_queuecommand)
606 * fnic_fcpio_fw_reset_cmpl_handler
607 * Routine to handle fw reset completion
609 static int fnic_fcpio_fw_reset_cmpl_handler(struct fnic *fnic,
610 struct fcpio_fw_req *desc)
614 struct fcpio_tag tag;
617 struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
619 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
621 atomic64_inc(&reset_stats->fw_reset_completions);
623 /* Clean up all outstanding io requests */
624 fnic_cleanup_io(fnic, SCSI_NO_TAG);
626 atomic64_set(&fnic->fnic_stats.fw_stats.active_fw_reqs, 0);
627 atomic64_set(&fnic->fnic_stats.io_stats.active_ios, 0);
629 spin_lock_irqsave(&fnic->fnic_lock, flags);
631 /* fnic should be in FC_TRANS_ETH_MODE */
632 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) {
633 /* Check status of reset completion */
635 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
636 "reset cmpl success\n");
637 /* Ready to send flogi out */
638 fnic->state = FNIC_IN_ETH_MODE;
640 FNIC_SCSI_DBG(KERN_DEBUG,
642 "fnic fw_reset : failed %s\n",
643 fnic_fcpio_status_to_str(hdr_status));
646 * Unable to change to eth mode, cannot send out flogi
647 * Change state to fc mode, so that subsequent Flogi
648 * requests from libFC will cause more attempts to
649 * reset the firmware. Free the cached flogi
651 fnic->state = FNIC_IN_FC_MODE;
652 atomic64_inc(&reset_stats->fw_reset_failures);
656 FNIC_SCSI_DBG(KERN_DEBUG,
658 "Unexpected state %s while processing"
659 " reset cmpl\n", fnic_state_to_str(fnic->state));
660 atomic64_inc(&reset_stats->fw_reset_failures);
664 /* Thread removing device blocks till firmware reset is complete */
665 if (fnic->remove_wait)
666 complete(fnic->remove_wait);
669 * If fnic is being removed, or fw reset failed
670 * free the flogi frame. Else, send it out
672 if (fnic->remove_wait || ret) {
673 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
674 skb_queue_purge(&fnic->tx_queue);
675 goto reset_cmpl_handler_end;
678 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
682 reset_cmpl_handler_end:
683 fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
689 * fnic_fcpio_flogi_reg_cmpl_handler
690 * Routine to handle flogi register completion
692 static int fnic_fcpio_flogi_reg_cmpl_handler(struct fnic *fnic,
693 struct fcpio_fw_req *desc)
697 struct fcpio_tag tag;
701 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
703 /* Update fnic state based on status of flogi reg completion */
704 spin_lock_irqsave(&fnic->fnic_lock, flags);
706 if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE) {
708 /* Check flogi registration completion status */
710 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
711 "flog reg succeeded\n");
712 fnic->state = FNIC_IN_FC_MODE;
714 FNIC_SCSI_DBG(KERN_DEBUG,
716 "fnic flogi reg :failed %s\n",
717 fnic_fcpio_status_to_str(hdr_status));
718 fnic->state = FNIC_IN_ETH_MODE;
722 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
723 "Unexpected fnic state %s while"
724 " processing flogi reg completion\n",
725 fnic_state_to_str(fnic->state));
730 if (fnic->stop_rx_link_events) {
731 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
732 goto reg_cmpl_handler_end;
734 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
737 queue_work(fnic_event_queue, &fnic->frame_work);
739 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
742 reg_cmpl_handler_end:
746 static inline int is_ack_index_in_range(struct vnic_wq_copy *wq,
749 if (wq->to_clean_index <= wq->to_use_index) {
750 /* out of range, stale request_out index */
751 if (request_out < wq->to_clean_index ||
752 request_out >= wq->to_use_index)
755 /* out of range, stale request_out index */
756 if (request_out < wq->to_clean_index &&
757 request_out >= wq->to_use_index)
760 /* request_out index is in range */
766 * Mark that ack received and store the Ack index. If there are multiple
767 * acks received before Tx thread cleans it up, the latest value will be
768 * used which is correct behavior. This state should be in the copy Wq
769 * instead of in the fnic
771 static inline void fnic_fcpio_ack_handler(struct fnic *fnic,
772 unsigned int cq_index,
773 struct fcpio_fw_req *desc)
775 struct vnic_wq_copy *wq;
776 u16 request_out = desc->u.ack.request_out;
778 u64 *ox_id_tag = (u64 *)(void *)desc;
780 /* mark the ack state */
781 wq = &fnic->wq_copy[cq_index - fnic->raw_wq_count - fnic->rq_count];
782 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
784 fnic->fnic_stats.misc_stats.last_ack_time = jiffies;
785 if (is_ack_index_in_range(wq, request_out)) {
786 fnic->fw_ack_index[0] = request_out;
787 fnic->fw_ack_recd[0] = 1;
790 &fnic->fnic_stats.misc_stats.ack_index_out_of_range);
792 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
793 FNIC_TRACE(fnic_fcpio_ack_handler,
794 fnic->lport->host->host_no, 0, 0, ox_id_tag[2], ox_id_tag[3],
795 ox_id_tag[4], ox_id_tag[5]);
799 * fnic_fcpio_icmnd_cmpl_handler
800 * Routine to handle icmnd completions
802 static void fnic_fcpio_icmnd_cmpl_handler(struct fnic *fnic,
803 struct fcpio_fw_req *desc)
807 struct fcpio_tag tag;
810 struct fcpio_icmnd_cmpl *icmnd_cmpl;
811 struct fnic_io_req *io_req;
812 struct scsi_cmnd *sc;
813 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
817 unsigned long start_time;
819 /* Decode the cmpl description to get the io_req id */
820 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
821 fcpio_tag_id_dec(&tag, &id);
822 icmnd_cmpl = &desc->u.icmnd_cmpl;
824 if (id >= fnic->fnic_max_tag_id) {
825 shost_printk(KERN_ERR, fnic->lport->host,
826 "Tag out of range tag %x hdr status = %s\n",
827 id, fnic_fcpio_status_to_str(hdr_status));
831 sc = scsi_host_find_tag(fnic->lport->host, id);
834 atomic64_inc(&fnic_stats->io_stats.sc_null);
835 shost_printk(KERN_ERR, fnic->lport->host,
836 "icmnd_cmpl sc is null - "
837 "hdr status = %s tag = 0x%x desc = 0x%p\n",
838 fnic_fcpio_status_to_str(hdr_status), id, desc);
839 FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
840 fnic->lport->host->host_no, id,
841 ((u64)icmnd_cmpl->_resvd0[1] << 16 |
842 (u64)icmnd_cmpl->_resvd0[0]),
843 ((u64)hdr_status << 16 |
844 (u64)icmnd_cmpl->scsi_status << 8 |
845 (u64)icmnd_cmpl->flags), desc,
846 (u64)icmnd_cmpl->residual, 0);
850 io_lock = fnic_io_lock_hash(fnic, sc);
851 spin_lock_irqsave(io_lock, flags);
852 io_req = (struct fnic_io_req *)CMD_SP(sc);
853 WARN_ON_ONCE(!io_req);
855 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
856 CMD_FLAGS(sc) |= FNIC_IO_REQ_NULL;
857 spin_unlock_irqrestore(io_lock, flags);
858 shost_printk(KERN_ERR, fnic->lport->host,
859 "icmnd_cmpl io_req is null - "
860 "hdr status = %s tag = 0x%x sc 0x%p\n",
861 fnic_fcpio_status_to_str(hdr_status), id, sc);
864 start_time = io_req->start_time;
866 /* firmware completed the io */
867 io_req->io_completed = 1;
870 * if SCSI-ML has already issued abort on this command,
871 * ignore completion of the IO. The abts path will clean it up
873 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
874 spin_unlock_irqrestore(io_lock, flags);
875 CMD_FLAGS(sc) |= FNIC_IO_ABTS_PENDING;
876 switch (hdr_status) {
878 CMD_FLAGS(sc) |= FNIC_IO_DONE;
879 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
880 "icmnd_cmpl ABTS pending hdr status = %s "
881 "sc 0x%p scsi_status %x residual %d\n",
882 fnic_fcpio_status_to_str(hdr_status), sc,
883 icmnd_cmpl->scsi_status,
884 icmnd_cmpl->residual);
887 CMD_FLAGS(sc) |= FNIC_IO_ABORTED;
890 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
891 "icmnd_cmpl abts pending "
892 "hdr status = %s tag = 0x%x sc = 0x%p\n",
893 fnic_fcpio_status_to_str(hdr_status),
900 /* Mark the IO as complete */
901 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
903 icmnd_cmpl = &desc->u.icmnd_cmpl;
905 switch (hdr_status) {
907 sc->result = (DID_OK << 16) | icmnd_cmpl->scsi_status;
908 xfer_len = scsi_bufflen(sc);
909 scsi_set_resid(sc, icmnd_cmpl->residual);
911 if (icmnd_cmpl->flags & FCPIO_ICMND_CMPL_RESID_UNDER)
912 xfer_len -= icmnd_cmpl->residual;
914 if (icmnd_cmpl->scsi_status == SAM_STAT_TASK_SET_FULL)
915 atomic64_inc(&fnic_stats->misc_stats.queue_fulls);
918 case FCPIO_TIMEOUT: /* request was timed out */
919 atomic64_inc(&fnic_stats->misc_stats.fcpio_timeout);
920 sc->result = (DID_TIME_OUT << 16) | icmnd_cmpl->scsi_status;
923 case FCPIO_ABORTED: /* request was aborted */
924 atomic64_inc(&fnic_stats->misc_stats.fcpio_aborted);
925 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
928 case FCPIO_DATA_CNT_MISMATCH: /* recv/sent more/less data than exp. */
929 atomic64_inc(&fnic_stats->misc_stats.data_count_mismatch);
930 scsi_set_resid(sc, icmnd_cmpl->residual);
931 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
934 case FCPIO_OUT_OF_RESOURCE: /* out of resources to complete request */
935 atomic64_inc(&fnic_stats->fw_stats.fw_out_of_resources);
936 sc->result = (DID_REQUEUE << 16) | icmnd_cmpl->scsi_status;
939 case FCPIO_IO_NOT_FOUND: /* requested I/O was not found */
940 atomic64_inc(&fnic_stats->io_stats.io_not_found);
941 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
944 case FCPIO_SGL_INVALID: /* request was aborted due to sgl error */
945 atomic64_inc(&fnic_stats->misc_stats.sgl_invalid);
946 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
949 case FCPIO_FW_ERR: /* request was terminated due fw error */
950 atomic64_inc(&fnic_stats->fw_stats.io_fw_errs);
951 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
954 case FCPIO_MSS_INVALID: /* request was aborted due to mss error */
955 atomic64_inc(&fnic_stats->misc_stats.mss_invalid);
956 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
959 case FCPIO_INVALID_HEADER: /* header contains invalid data */
960 case FCPIO_INVALID_PARAM: /* some parameter in request invalid */
961 case FCPIO_REQ_NOT_SUPPORTED:/* request type is not supported */
963 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
967 /* Break link with the SCSI command */
969 CMD_FLAGS(sc) |= FNIC_IO_DONE;
971 spin_unlock_irqrestore(io_lock, flags);
973 if (hdr_status != FCPIO_SUCCESS) {
974 atomic64_inc(&fnic_stats->io_stats.io_failures);
975 shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
976 fnic_fcpio_status_to_str(hdr_status));
979 fnic_release_ioreq_buf(fnic, io_req, sc);
981 mempool_free(io_req, fnic->io_req_pool);
983 cmd_trace = ((u64)hdr_status << 56) |
984 (u64)icmnd_cmpl->scsi_status << 48 |
985 (u64)icmnd_cmpl->flags << 40 | (u64)sc->cmnd[0] << 32 |
986 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
987 (u64)sc->cmnd[4] << 8 | sc->cmnd[5];
989 FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
990 sc->device->host->host_no, id, sc,
991 ((u64)icmnd_cmpl->_resvd0[1] << 56 |
992 (u64)icmnd_cmpl->_resvd0[0] << 48 |
993 jiffies_to_msecs(jiffies - start_time)),
995 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
997 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
998 fnic->lport->host_stats.fcp_input_requests++;
999 fnic->fcp_input_bytes += xfer_len;
1000 } else if (sc->sc_data_direction == DMA_TO_DEVICE) {
1001 fnic->lport->host_stats.fcp_output_requests++;
1002 fnic->fcp_output_bytes += xfer_len;
1004 fnic->lport->host_stats.fcp_control_requests++;
1006 atomic64_dec(&fnic_stats->io_stats.active_ios);
1007 if (atomic64_read(&fnic->io_cmpl_skip))
1008 atomic64_dec(&fnic->io_cmpl_skip);
1010 atomic64_inc(&fnic_stats->io_stats.io_completions);
1012 /* Call SCSI completion function to complete the IO */
1017 /* fnic_fcpio_itmf_cmpl_handler
1018 * Routine to handle itmf completions
1020 static void fnic_fcpio_itmf_cmpl_handler(struct fnic *fnic,
1021 struct fcpio_fw_req *desc)
1025 struct fcpio_tag tag;
1027 struct scsi_cmnd *sc;
1028 struct fnic_io_req *io_req;
1029 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1030 struct abort_stats *abts_stats = &fnic->fnic_stats.abts_stats;
1031 struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
1032 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
1033 unsigned long flags;
1034 spinlock_t *io_lock;
1035 unsigned long start_time;
1037 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
1038 fcpio_tag_id_dec(&tag, &id);
1040 if ((id & FNIC_TAG_MASK) >= fnic->fnic_max_tag_id) {
1041 shost_printk(KERN_ERR, fnic->lport->host,
1042 "Tag out of range tag %x hdr status = %s\n",
1043 id, fnic_fcpio_status_to_str(hdr_status));
1047 sc = scsi_host_find_tag(fnic->lport->host, id & FNIC_TAG_MASK);
1050 atomic64_inc(&fnic_stats->io_stats.sc_null);
1051 shost_printk(KERN_ERR, fnic->lport->host,
1052 "itmf_cmpl sc is null - hdr status = %s tag = 0x%x\n",
1053 fnic_fcpio_status_to_str(hdr_status), id);
1056 io_lock = fnic_io_lock_hash(fnic, sc);
1057 spin_lock_irqsave(io_lock, flags);
1058 io_req = (struct fnic_io_req *)CMD_SP(sc);
1059 WARN_ON_ONCE(!io_req);
1061 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
1062 spin_unlock_irqrestore(io_lock, flags);
1063 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
1064 shost_printk(KERN_ERR, fnic->lport->host,
1065 "itmf_cmpl io_req is null - "
1066 "hdr status = %s tag = 0x%x sc 0x%p\n",
1067 fnic_fcpio_status_to_str(hdr_status), id, sc);
1070 start_time = io_req->start_time;
1072 if ((id & FNIC_TAG_ABORT) && (id & FNIC_TAG_DEV_RST)) {
1073 /* Abort and terminate completion of device reset req */
1074 /* REVISIT : Add asserts about various flags */
1075 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1076 "dev reset abts cmpl recd. id %x status %s\n",
1077 id, fnic_fcpio_status_to_str(hdr_status));
1078 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1079 CMD_ABTS_STATUS(sc) = hdr_status;
1080 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1081 if (io_req->abts_done)
1082 complete(io_req->abts_done);
1083 spin_unlock_irqrestore(io_lock, flags);
1084 } else if (id & FNIC_TAG_ABORT) {
1085 /* Completion of abort cmd */
1086 switch (hdr_status) {
1090 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1091 atomic64_inc(&abts_stats->abort_fw_timeouts);
1094 &term_stats->terminate_fw_timeouts);
1096 case FCPIO_ITMF_REJECTED:
1097 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1098 "abort reject recd. id %d\n",
1099 (int)(id & FNIC_TAG_MASK));
1101 case FCPIO_IO_NOT_FOUND:
1102 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1103 atomic64_inc(&abts_stats->abort_io_not_found);
1106 &term_stats->terminate_io_not_found);
1109 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1110 atomic64_inc(&abts_stats->abort_failures);
1113 &term_stats->terminate_failures);
1116 if (CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING) {
1117 /* This is a late completion. Ignore it */
1118 spin_unlock_irqrestore(io_lock, flags);
1122 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
1124 /* If the status is IO not found consider it as success */
1125 if (hdr_status == FCPIO_IO_NOT_FOUND)
1126 CMD_ABTS_STATUS(sc) = FCPIO_SUCCESS;
1128 CMD_ABTS_STATUS(sc) = hdr_status;
1130 atomic64_dec(&fnic_stats->io_stats.active_ios);
1131 if (atomic64_read(&fnic->io_cmpl_skip))
1132 atomic64_dec(&fnic->io_cmpl_skip);
1134 atomic64_inc(&fnic_stats->io_stats.io_completions);
1136 if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE)))
1137 atomic64_inc(&misc_stats->no_icmnd_itmf_cmpls);
1139 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1140 "abts cmpl recd. id %d status %s\n",
1141 (int)(id & FNIC_TAG_MASK),
1142 fnic_fcpio_status_to_str(hdr_status));
1145 * If scsi_eh thread is blocked waiting for abts to complete,
1146 * signal completion to it. IO will be cleaned in the thread
1147 * else clean it in this context
1149 if (io_req->abts_done) {
1150 complete(io_req->abts_done);
1151 spin_unlock_irqrestore(io_lock, flags);
1153 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1154 "abts cmpl, completing IO\n");
1156 sc->result = (DID_ERROR << 16);
1158 spin_unlock_irqrestore(io_lock, flags);
1160 fnic_release_ioreq_buf(fnic, io_req, sc);
1161 mempool_free(io_req, fnic->io_req_pool);
1162 if (sc->scsi_done) {
1163 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1164 sc->device->host->host_no, id,
1166 jiffies_to_msecs(jiffies - start_time),
1168 (((u64)hdr_status << 40) |
1169 (u64)sc->cmnd[0] << 32 |
1170 (u64)sc->cmnd[2] << 24 |
1171 (u64)sc->cmnd[3] << 16 |
1172 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1173 (((u64)CMD_FLAGS(sc) << 32) |
1179 } else if (id & FNIC_TAG_DEV_RST) {
1180 /* Completion of device reset */
1181 CMD_LR_STATUS(sc) = hdr_status;
1182 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1183 spin_unlock_irqrestore(io_lock, flags);
1184 CMD_FLAGS(sc) |= FNIC_DEV_RST_ABTS_PENDING;
1185 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1186 sc->device->host->host_no, id, sc,
1187 jiffies_to_msecs(jiffies - start_time),
1189 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1190 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1191 "Terminate pending "
1192 "dev reset cmpl recd. id %d status %s\n",
1193 (int)(id & FNIC_TAG_MASK),
1194 fnic_fcpio_status_to_str(hdr_status));
1197 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TIMED_OUT) {
1198 /* Need to wait for terminate completion */
1199 spin_unlock_irqrestore(io_lock, flags);
1200 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1201 sc->device->host->host_no, id, sc,
1202 jiffies_to_msecs(jiffies - start_time),
1204 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1205 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1206 "dev reset cmpl recd after time out. "
1207 "id %d status %s\n",
1208 (int)(id & FNIC_TAG_MASK),
1209 fnic_fcpio_status_to_str(hdr_status));
1212 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
1213 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1214 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1215 "dev reset cmpl recd. id %d status %s\n",
1216 (int)(id & FNIC_TAG_MASK),
1217 fnic_fcpio_status_to_str(hdr_status));
1218 if (io_req->dr_done)
1219 complete(io_req->dr_done);
1220 spin_unlock_irqrestore(io_lock, flags);
1223 shost_printk(KERN_ERR, fnic->lport->host,
1224 "Unexpected itmf io state %s tag %x\n",
1225 fnic_ioreq_state_to_str(CMD_STATE(sc)), id);
1226 spin_unlock_irqrestore(io_lock, flags);
1232 * fnic_fcpio_cmpl_handler
1233 * Routine to service the cq for wq_copy
1235 static int fnic_fcpio_cmpl_handler(struct vnic_dev *vdev,
1236 unsigned int cq_index,
1237 struct fcpio_fw_req *desc)
1239 struct fnic *fnic = vnic_dev_priv(vdev);
1241 switch (desc->hdr.type) {
1242 case FCPIO_ICMND_CMPL: /* fw completed a command */
1243 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1244 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
1245 case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
1246 case FCPIO_RESET_CMPL: /* fw completed reset */
1247 atomic64_dec(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1253 switch (desc->hdr.type) {
1254 case FCPIO_ACK: /* fw copied copy wq desc to its queue */
1255 fnic_fcpio_ack_handler(fnic, cq_index, desc);
1258 case FCPIO_ICMND_CMPL: /* fw completed a command */
1259 fnic_fcpio_icmnd_cmpl_handler(fnic, desc);
1262 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1263 fnic_fcpio_itmf_cmpl_handler(fnic, desc);
1266 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
1267 case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
1268 fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
1271 case FCPIO_RESET_CMPL: /* fw completed reset */
1272 fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
1276 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1277 "firmware completion type %d\n",
1286 * fnic_wq_copy_cmpl_handler
1287 * Routine to process wq copy
1289 int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
1291 unsigned int wq_work_done = 0;
1292 unsigned int i, cq_index;
1293 unsigned int cur_work_done;
1295 for (i = 0; i < fnic->wq_copy_count; i++) {
1296 cq_index = i + fnic->raw_wq_count + fnic->rq_count;
1297 cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
1298 fnic_fcpio_cmpl_handler,
1300 wq_work_done += cur_work_done;
1302 return wq_work_done;
1305 static void fnic_cleanup_io(struct fnic *fnic, int exclude_id)
1308 struct fnic_io_req *io_req;
1309 unsigned long flags = 0;
1310 struct scsi_cmnd *sc;
1311 spinlock_t *io_lock;
1312 unsigned long start_time = 0;
1313 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1315 for (i = 0; i < fnic->fnic_max_tag_id; i++) {
1316 if (i == exclude_id)
1319 io_lock = fnic_io_lock_tag(fnic, i);
1320 spin_lock_irqsave(io_lock, flags);
1321 sc = scsi_host_find_tag(fnic->lport->host, i);
1323 spin_unlock_irqrestore(io_lock, flags);
1327 io_req = (struct fnic_io_req *)CMD_SP(sc);
1328 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1329 !(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
1331 * We will be here only when FW completes reset
1332 * without sending completions for outstanding ios.
1334 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1335 if (io_req && io_req->dr_done)
1336 complete(io_req->dr_done);
1337 else if (io_req && io_req->abts_done)
1338 complete(io_req->abts_done);
1339 spin_unlock_irqrestore(io_lock, flags);
1341 } else if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1342 spin_unlock_irqrestore(io_lock, flags);
1346 spin_unlock_irqrestore(io_lock, flags);
1347 goto cleanup_scsi_cmd;
1352 spin_unlock_irqrestore(io_lock, flags);
1355 * If there is a scsi_cmnd associated with this io_req, then
1356 * free the corresponding state
1358 start_time = io_req->start_time;
1359 fnic_release_ioreq_buf(fnic, io_req, sc);
1360 mempool_free(io_req, fnic->io_req_pool);
1363 sc->result = DID_TRANSPORT_DISRUPTED << 16;
1364 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1365 "%s: sc duration = %lu DID_TRANSPORT_DISRUPTED\n",
1366 __func__, (jiffies - start_time));
1368 if (atomic64_read(&fnic->io_cmpl_skip))
1369 atomic64_dec(&fnic->io_cmpl_skip);
1371 atomic64_inc(&fnic_stats->io_stats.io_completions);
1373 /* Complete the command to SCSI */
1374 if (sc->scsi_done) {
1375 FNIC_TRACE(fnic_cleanup_io,
1376 sc->device->host->host_no, i, sc,
1377 jiffies_to_msecs(jiffies - start_time),
1378 0, ((u64)sc->cmnd[0] << 32 |
1379 (u64)sc->cmnd[2] << 24 |
1380 (u64)sc->cmnd[3] << 16 |
1381 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1382 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1389 void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
1390 struct fcpio_host_req *desc)
1393 struct fnic *fnic = vnic_dev_priv(wq->vdev);
1394 struct fnic_io_req *io_req;
1395 struct scsi_cmnd *sc;
1396 unsigned long flags;
1397 spinlock_t *io_lock;
1398 unsigned long start_time = 0;
1400 /* get the tag reference */
1401 fcpio_tag_id_dec(&desc->hdr.tag, &id);
1402 id &= FNIC_TAG_MASK;
1404 if (id >= fnic->fnic_max_tag_id)
1407 sc = scsi_host_find_tag(fnic->lport->host, id);
1411 io_lock = fnic_io_lock_hash(fnic, sc);
1412 spin_lock_irqsave(io_lock, flags);
1414 /* Get the IO context which this desc refers to */
1415 io_req = (struct fnic_io_req *)CMD_SP(sc);
1417 /* fnic interrupts are turned off by now */
1420 spin_unlock_irqrestore(io_lock, flags);
1421 goto wq_copy_cleanup_scsi_cmd;
1426 spin_unlock_irqrestore(io_lock, flags);
1428 start_time = io_req->start_time;
1429 fnic_release_ioreq_buf(fnic, io_req, sc);
1430 mempool_free(io_req, fnic->io_req_pool);
1432 wq_copy_cleanup_scsi_cmd:
1433 sc->result = DID_NO_CONNECT << 16;
1434 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
1435 " DID_NO_CONNECT\n");
1437 if (sc->scsi_done) {
1438 FNIC_TRACE(fnic_wq_copy_cleanup_handler,
1439 sc->device->host->host_no, id, sc,
1440 jiffies_to_msecs(jiffies - start_time),
1441 0, ((u64)sc->cmnd[0] << 32 |
1442 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1443 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1444 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1450 static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
1451 u32 task_req, u8 *fc_lun,
1452 struct fnic_io_req *io_req)
1454 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1455 struct Scsi_Host *host = fnic->lport->host;
1456 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
1457 unsigned long flags;
1459 spin_lock_irqsave(host->host_lock, flags);
1460 if (unlikely(fnic_chk_state_flags_locked(fnic,
1461 FNIC_FLAGS_IO_BLOCKED))) {
1462 spin_unlock_irqrestore(host->host_lock, flags);
1465 atomic_inc(&fnic->in_flight);
1466 spin_unlock_irqrestore(host->host_lock, flags);
1468 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
1470 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1471 free_wq_copy_descs(fnic, wq);
1473 if (!vnic_wq_copy_desc_avail(wq)) {
1474 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1475 atomic_dec(&fnic->in_flight);
1476 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1477 "fnic_queue_abort_io_req: failure: no descriptors\n");
1478 atomic64_inc(&misc_stats->abts_cpwq_alloc_failures);
1481 fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
1482 0, task_req, tag, fc_lun, io_req->port_id,
1483 fnic->config.ra_tov, fnic->config.ed_tov);
1485 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1486 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
1487 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
1488 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
1489 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
1491 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1492 atomic_dec(&fnic->in_flight);
1497 static void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
1502 struct fnic_io_req *io_req;
1503 spinlock_t *io_lock;
1504 unsigned long flags;
1505 struct scsi_cmnd *sc;
1506 struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
1507 struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
1508 struct scsi_lun fc_lun;
1509 enum fnic_ioreq_state old_ioreq_state;
1511 FNIC_SCSI_DBG(KERN_DEBUG,
1513 "fnic_rport_exch_reset called portid 0x%06x\n",
1516 if (fnic->in_remove)
1519 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
1521 io_lock = fnic_io_lock_tag(fnic, tag);
1522 spin_lock_irqsave(io_lock, flags);
1523 sc = scsi_host_find_tag(fnic->lport->host, tag);
1525 spin_unlock_irqrestore(io_lock, flags);
1529 io_req = (struct fnic_io_req *)CMD_SP(sc);
1531 if (!io_req || io_req->port_id != port_id) {
1532 spin_unlock_irqrestore(io_lock, flags);
1536 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1537 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
1538 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1539 "fnic_rport_exch_reset dev rst not pending sc 0x%p\n",
1541 spin_unlock_irqrestore(io_lock, flags);
1546 * Found IO that is still pending with firmware and
1547 * belongs to rport that went away
1549 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1550 spin_unlock_irqrestore(io_lock, flags);
1553 if (io_req->abts_done) {
1554 shost_printk(KERN_ERR, fnic->lport->host,
1555 "fnic_rport_exch_reset: io_req->abts_done is set "
1557 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1560 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1561 shost_printk(KERN_ERR, fnic->lport->host,
1563 "IO not yet issued %p tag 0x%x flags "
1565 sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1567 old_ioreq_state = CMD_STATE(sc);
1568 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1569 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1570 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1571 atomic64_inc(&reset_stats->device_reset_terminates);
1572 abt_tag = (tag | FNIC_TAG_DEV_RST);
1573 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1574 "fnic_rport_exch_reset dev rst sc 0x%p\n",
1578 BUG_ON(io_req->abts_done);
1580 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1581 "fnic_rport_reset_exch: Issuing abts\n");
1583 spin_unlock_irqrestore(io_lock, flags);
1585 /* Now queue the abort command to firmware */
1586 int_to_scsilun(sc->device->lun, &fc_lun);
1588 if (fnic_queue_abort_io_req(fnic, abt_tag,
1589 FCPIO_ITMF_ABT_TASK_TERM,
1590 fc_lun.scsi_lun, io_req)) {
1592 * Revert the cmd state back to old state, if
1593 * it hasn't changed in between. This cmd will get
1594 * aborted later by scsi_eh, or cleaned up during
1597 spin_lock_irqsave(io_lock, flags);
1598 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1599 CMD_STATE(sc) = old_ioreq_state;
1600 spin_unlock_irqrestore(io_lock, flags);
1602 spin_lock_irqsave(io_lock, flags);
1603 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1604 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
1606 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
1607 spin_unlock_irqrestore(io_lock, flags);
1608 atomic64_inc(&term_stats->terminates);
1612 if (term_cnt > atomic64_read(&term_stats->max_terminates))
1613 atomic64_set(&term_stats->max_terminates, term_cnt);
1617 void fnic_terminate_rport_io(struct fc_rport *rport)
1622 struct fnic_io_req *io_req;
1623 spinlock_t *io_lock;
1624 unsigned long flags;
1625 struct scsi_cmnd *sc;
1626 struct scsi_lun fc_lun;
1627 struct fc_rport_libfc_priv *rdata;
1628 struct fc_lport *lport;
1630 struct fc_rport *cmd_rport;
1631 struct reset_stats *reset_stats;
1632 struct terminate_stats *term_stats;
1633 enum fnic_ioreq_state old_ioreq_state;
1636 printk(KERN_ERR "fnic_terminate_rport_io: rport is NULL\n");
1639 rdata = rport->dd_data;
1642 printk(KERN_ERR "fnic_terminate_rport_io: rdata is NULL\n");
1645 lport = rdata->local_port;
1648 printk(KERN_ERR "fnic_terminate_rport_io: lport is NULL\n");
1651 fnic = lport_priv(lport);
1652 FNIC_SCSI_DBG(KERN_DEBUG,
1653 fnic->lport->host, "fnic_terminate_rport_io called"
1654 " wwpn 0x%llx, wwnn0x%llx, rport 0x%p, portid 0x%06x\n",
1655 rport->port_name, rport->node_name, rport,
1658 if (fnic->in_remove)
1661 reset_stats = &fnic->fnic_stats.reset_stats;
1662 term_stats = &fnic->fnic_stats.term_stats;
1664 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
1666 io_lock = fnic_io_lock_tag(fnic, tag);
1667 spin_lock_irqsave(io_lock, flags);
1668 sc = scsi_host_find_tag(fnic->lport->host, tag);
1670 spin_unlock_irqrestore(io_lock, flags);
1674 cmd_rport = starget_to_rport(scsi_target(sc->device));
1675 if (rport != cmd_rport) {
1676 spin_unlock_irqrestore(io_lock, flags);
1680 io_req = (struct fnic_io_req *)CMD_SP(sc);
1682 if (!io_req || rport != cmd_rport) {
1683 spin_unlock_irqrestore(io_lock, flags);
1687 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1688 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
1689 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1690 "fnic_terminate_rport_io dev rst not pending sc 0x%p\n",
1692 spin_unlock_irqrestore(io_lock, flags);
1696 * Found IO that is still pending with firmware and
1697 * belongs to rport that went away
1699 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1700 spin_unlock_irqrestore(io_lock, flags);
1703 if (io_req->abts_done) {
1704 shost_printk(KERN_ERR, fnic->lport->host,
1705 "fnic_terminate_rport_io: io_req->abts_done is set "
1707 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1709 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1710 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1711 "fnic_terminate_rport_io "
1712 "IO not yet issued %p tag 0x%x flags "
1714 sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1716 old_ioreq_state = CMD_STATE(sc);
1717 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1718 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1719 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1720 atomic64_inc(&reset_stats->device_reset_terminates);
1721 abt_tag = (tag | FNIC_TAG_DEV_RST);
1722 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1723 "fnic_terminate_rport_io dev rst sc 0x%p\n", sc);
1726 BUG_ON(io_req->abts_done);
1728 FNIC_SCSI_DBG(KERN_DEBUG,
1730 "fnic_terminate_rport_io: Issuing abts\n");
1732 spin_unlock_irqrestore(io_lock, flags);
1734 /* Now queue the abort command to firmware */
1735 int_to_scsilun(sc->device->lun, &fc_lun);
1737 if (fnic_queue_abort_io_req(fnic, abt_tag,
1738 FCPIO_ITMF_ABT_TASK_TERM,
1739 fc_lun.scsi_lun, io_req)) {
1741 * Revert the cmd state back to old state, if
1742 * it hasn't changed in between. This cmd will get
1743 * aborted later by scsi_eh, or cleaned up during
1746 spin_lock_irqsave(io_lock, flags);
1747 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1748 CMD_STATE(sc) = old_ioreq_state;
1749 spin_unlock_irqrestore(io_lock, flags);
1751 spin_lock_irqsave(io_lock, flags);
1752 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1753 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
1755 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
1756 spin_unlock_irqrestore(io_lock, flags);
1757 atomic64_inc(&term_stats->terminates);
1761 if (term_cnt > atomic64_read(&term_stats->max_terminates))
1762 atomic64_set(&term_stats->max_terminates, term_cnt);
1767 * This function is exported to SCSI for sending abort cmnds.
1768 * A SCSI IO is represented by a io_req in the driver.
1769 * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
1771 int fnic_abort_cmd(struct scsi_cmnd *sc)
1773 struct fc_lport *lp;
1775 struct fnic_io_req *io_req = NULL;
1776 struct fc_rport *rport;
1777 spinlock_t *io_lock;
1778 unsigned long flags;
1779 unsigned long start_time = 0;
1782 struct scsi_lun fc_lun;
1783 struct fnic_stats *fnic_stats;
1784 struct abort_stats *abts_stats;
1785 struct terminate_stats *term_stats;
1786 enum fnic_ioreq_state old_ioreq_state;
1788 DECLARE_COMPLETION_ONSTACK(tm_done);
1790 /* Wait for rport to unblock */
1791 fc_block_scsi_eh(sc);
1793 /* Get local-port, check ready and link up */
1794 lp = shost_priv(sc->device->host);
1796 fnic = lport_priv(lp);
1797 fnic_stats = &fnic->fnic_stats;
1798 abts_stats = &fnic->fnic_stats.abts_stats;
1799 term_stats = &fnic->fnic_stats.term_stats;
1801 rport = starget_to_rport(scsi_target(sc->device));
1802 tag = sc->request->tag;
1803 FNIC_SCSI_DBG(KERN_DEBUG,
1805 "Abort Cmd called FCID 0x%x, LUN 0x%llx TAG %x flags %x\n",
1806 rport->port_id, sc->device->lun, tag, CMD_FLAGS(sc));
1808 CMD_FLAGS(sc) = FNIC_NO_FLAGS;
1810 if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
1812 goto fnic_abort_cmd_end;
1816 * Avoid a race between SCSI issuing the abort and the device
1817 * completing the command.
1819 * If the command is already completed by the fw cmpl code,
1820 * we just return SUCCESS from here. This means that the abort
1821 * succeeded. In the SCSI ML, since the timeout for command has
1822 * happened, the completion wont actually complete the command
1823 * and it will be considered as an aborted command
1825 * The CMD_SP will not be cleared except while holding io_req_lock.
1827 io_lock = fnic_io_lock_hash(fnic, sc);
1828 spin_lock_irqsave(io_lock, flags);
1829 io_req = (struct fnic_io_req *)CMD_SP(sc);
1831 spin_unlock_irqrestore(io_lock, flags);
1832 goto fnic_abort_cmd_end;
1835 io_req->abts_done = &tm_done;
1837 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1838 spin_unlock_irqrestore(io_lock, flags);
1842 * Command is still pending, need to abort it
1843 * If the firmware completes the command after this point,
1844 * the completion wont be done till mid-layer, since abort
1845 * has already started.
1847 old_ioreq_state = CMD_STATE(sc);
1848 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1849 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1851 spin_unlock_irqrestore(io_lock, flags);
1854 * Check readiness of the remote port. If the path to remote
1855 * port is up, then send abts to the remote port to terminate
1856 * the IO. Else, just locally terminate the IO in the firmware
1858 if (fc_remote_port_chkready(rport) == 0)
1859 task_req = FCPIO_ITMF_ABT_TASK;
1861 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
1862 task_req = FCPIO_ITMF_ABT_TASK_TERM;
1865 /* Now queue the abort command to firmware */
1866 int_to_scsilun(sc->device->lun, &fc_lun);
1868 if (fnic_queue_abort_io_req(fnic, sc->request->tag, task_req,
1869 fc_lun.scsi_lun, io_req)) {
1870 spin_lock_irqsave(io_lock, flags);
1871 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1872 CMD_STATE(sc) = old_ioreq_state;
1873 io_req = (struct fnic_io_req *)CMD_SP(sc);
1875 io_req->abts_done = NULL;
1876 spin_unlock_irqrestore(io_lock, flags);
1878 goto fnic_abort_cmd_end;
1880 if (task_req == FCPIO_ITMF_ABT_TASK) {
1881 CMD_FLAGS(sc) |= FNIC_IO_ABTS_ISSUED;
1882 atomic64_inc(&fnic_stats->abts_stats.aborts);
1884 CMD_FLAGS(sc) |= FNIC_IO_TERM_ISSUED;
1885 atomic64_inc(&fnic_stats->term_stats.terminates);
1889 * We queued an abort IO, wait for its completion.
1890 * Once the firmware completes the abort command, it will
1891 * wake up this thread.
1894 wait_for_completion_timeout(&tm_done,
1896 (2 * fnic->config.ra_tov +
1897 fnic->config.ed_tov));
1899 /* Check the abort status */
1900 spin_lock_irqsave(io_lock, flags);
1902 io_req = (struct fnic_io_req *)CMD_SP(sc);
1904 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
1905 spin_unlock_irqrestore(io_lock, flags);
1906 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
1908 goto fnic_abort_cmd_end;
1910 io_req->abts_done = NULL;
1912 /* fw did not complete abort, timed out */
1913 if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
1914 spin_unlock_irqrestore(io_lock, flags);
1915 if (task_req == FCPIO_ITMF_ABT_TASK) {
1916 atomic64_inc(&abts_stats->abort_drv_timeouts);
1918 atomic64_inc(&term_stats->terminate_drv_timeouts);
1920 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_TIMED_OUT;
1922 goto fnic_abort_cmd_end;
1925 /* IO out of order */
1927 if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE))) {
1928 spin_unlock_irqrestore(io_lock, flags);
1929 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1930 "Issuing Host reset due to out of order IO\n");
1932 if (fnic_host_reset(sc) == FAILED) {
1933 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1934 "fnic_host_reset failed.\n");
1937 goto fnic_abort_cmd_end;
1940 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1942 start_time = io_req->start_time;
1944 * firmware completed the abort, check the status,
1945 * free the io_req if successful. If abort fails,
1946 * Device reset will clean the I/O.
1948 if (CMD_ABTS_STATUS(sc) == FCPIO_SUCCESS)
1952 spin_unlock_irqrestore(io_lock, flags);
1953 goto fnic_abort_cmd_end;
1956 spin_unlock_irqrestore(io_lock, flags);
1958 fnic_release_ioreq_buf(fnic, io_req, sc);
1959 mempool_free(io_req, fnic->io_req_pool);
1961 if (sc->scsi_done) {
1962 /* Call SCSI completion function to complete the IO */
1963 sc->result = (DID_ABORT << 16);
1968 FNIC_TRACE(fnic_abort_cmd, sc->device->host->host_no,
1969 sc->request->tag, sc,
1970 jiffies_to_msecs(jiffies - start_time),
1971 0, ((u64)sc->cmnd[0] << 32 |
1972 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1973 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1974 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1976 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1977 "Returning from abort cmd type %x %s\n", task_req,
1979 "SUCCESS" : "FAILED");
1983 static inline int fnic_queue_dr_io_req(struct fnic *fnic,
1984 struct scsi_cmnd *sc,
1985 struct fnic_io_req *io_req)
1987 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1988 struct Scsi_Host *host = fnic->lport->host;
1989 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
1990 struct scsi_lun fc_lun;
1992 unsigned long intr_flags;
1994 spin_lock_irqsave(host->host_lock, intr_flags);
1995 if (unlikely(fnic_chk_state_flags_locked(fnic,
1996 FNIC_FLAGS_IO_BLOCKED))) {
1997 spin_unlock_irqrestore(host->host_lock, intr_flags);
2000 atomic_inc(&fnic->in_flight);
2001 spin_unlock_irqrestore(host->host_lock, intr_flags);
2003 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
2005 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
2006 free_wq_copy_descs(fnic, wq);
2008 if (!vnic_wq_copy_desc_avail(wq)) {
2009 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2010 "queue_dr_io_req failure - no descriptors\n");
2011 atomic64_inc(&misc_stats->devrst_cpwq_alloc_failures);
2016 /* fill in the lun info */
2017 int_to_scsilun(sc->device->lun, &fc_lun);
2019 fnic_queue_wq_copy_desc_itmf(wq, sc->request->tag | FNIC_TAG_DEV_RST,
2020 0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
2021 fc_lun.scsi_lun, io_req->port_id,
2022 fnic->config.ra_tov, fnic->config.ed_tov);
2024 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
2025 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
2026 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
2027 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
2028 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
2031 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
2032 atomic_dec(&fnic->in_flight);
2038 * Clean up any pending aborts on the lun
2039 * For each outstanding IO on this lun, whose abort is not completed by fw,
2040 * issue a local abort. Wait for abort to complete. Return 0 if all commands
2041 * successfully aborted, 1 otherwise
2043 static int fnic_clean_pending_aborts(struct fnic *fnic,
2044 struct scsi_cmnd *lr_sc,
2049 struct fnic_io_req *io_req;
2050 spinlock_t *io_lock;
2051 unsigned long flags;
2053 struct scsi_cmnd *sc;
2054 struct scsi_lun fc_lun;
2055 struct scsi_device *lun_dev = lr_sc->device;
2056 DECLARE_COMPLETION_ONSTACK(tm_done);
2057 enum fnic_ioreq_state old_ioreq_state;
2059 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
2060 io_lock = fnic_io_lock_tag(fnic, tag);
2061 spin_lock_irqsave(io_lock, flags);
2062 sc = scsi_host_find_tag(fnic->lport->host, tag);
2064 * ignore this lun reset cmd if issued using new SC
2065 * or cmds that do not belong to this lun
2067 if (!sc || ((sc == lr_sc) && new_sc) || sc->device != lun_dev) {
2068 spin_unlock_irqrestore(io_lock, flags);
2072 io_req = (struct fnic_io_req *)CMD_SP(sc);
2074 if (!io_req || sc->device != lun_dev) {
2075 spin_unlock_irqrestore(io_lock, flags);
2080 * Found IO that is still pending with firmware and
2081 * belongs to the LUN that we are resetting
2083 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2084 "Found IO in %s on lun\n",
2085 fnic_ioreq_state_to_str(CMD_STATE(sc)));
2087 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
2088 spin_unlock_irqrestore(io_lock, flags);
2091 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
2092 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
2093 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2094 "%s dev rst not pending sc 0x%p\n", __func__,
2096 spin_unlock_irqrestore(io_lock, flags);
2100 if (io_req->abts_done)
2101 shost_printk(KERN_ERR, fnic->lport->host,
2102 "%s: io_req->abts_done is set state is %s\n",
2103 __func__, fnic_ioreq_state_to_str(CMD_STATE(sc)));
2104 old_ioreq_state = CMD_STATE(sc);
2106 * Any pending IO issued prior to reset is expected to be
2107 * in abts pending state, if not we need to set
2108 * FNIC_IOREQ_ABTS_PENDING to indicate the IO is abort pending.
2109 * When IO is completed, the IO will be handed over and
2110 * handled in this function.
2112 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2114 BUG_ON(io_req->abts_done);
2117 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
2118 abt_tag |= FNIC_TAG_DEV_RST;
2119 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2120 "%s: dev rst sc 0x%p\n", __func__, sc);
2123 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
2124 io_req->abts_done = &tm_done;
2125 spin_unlock_irqrestore(io_lock, flags);
2127 /* Now queue the abort command to firmware */
2128 int_to_scsilun(sc->device->lun, &fc_lun);
2130 if (fnic_queue_abort_io_req(fnic, abt_tag,
2131 FCPIO_ITMF_ABT_TASK_TERM,
2132 fc_lun.scsi_lun, io_req)) {
2133 spin_lock_irqsave(io_lock, flags);
2134 io_req = (struct fnic_io_req *)CMD_SP(sc);
2136 io_req->abts_done = NULL;
2137 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
2138 CMD_STATE(sc) = old_ioreq_state;
2139 spin_unlock_irqrestore(io_lock, flags);
2141 goto clean_pending_aborts_end;
2143 spin_lock_irqsave(io_lock, flags);
2144 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
2145 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2146 spin_unlock_irqrestore(io_lock, flags);
2148 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
2150 wait_for_completion_timeout(&tm_done,
2152 (fnic->config.ed_tov));
2154 /* Recheck cmd state to check if it is now aborted */
2155 spin_lock_irqsave(io_lock, flags);
2156 io_req = (struct fnic_io_req *)CMD_SP(sc);
2158 spin_unlock_irqrestore(io_lock, flags);
2159 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
2163 io_req->abts_done = NULL;
2165 /* if abort is still pending with fw, fail */
2166 if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
2167 spin_unlock_irqrestore(io_lock, flags);
2168 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
2170 goto clean_pending_aborts_end;
2172 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
2174 /* original sc used for lr is handled by dev reset code */
2177 spin_unlock_irqrestore(io_lock, flags);
2179 /* original sc used for lr is handled by dev reset code */
2181 fnic_release_ioreq_buf(fnic, io_req, sc);
2182 mempool_free(io_req, fnic->io_req_pool);
2186 * Any IO is returned during reset, it needs to call scsi_done
2187 * to return the scsi_cmnd to upper layer.
2189 if (sc->scsi_done) {
2190 /* Set result to let upper SCSI layer retry */
2191 sc->result = DID_RESET << 16;
2196 schedule_timeout(msecs_to_jiffies(2 * fnic->config.ed_tov));
2198 /* walk again to check, if IOs are still pending in fw */
2199 if (fnic_is_abts_pending(fnic, lr_sc))
2202 clean_pending_aborts_end:
2207 * fnic_scsi_host_start_tag
2208 * Allocates tagid from host's tag list
2211 fnic_scsi_host_start_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2213 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2214 int tag, ret = SCSI_NO_TAG;
2218 pr_err("Tags are not supported\n");
2223 tag = find_next_zero_bit(bqt->tag_map, bqt->max_depth, 1);
2224 if (tag >= bqt->max_depth) {
2225 pr_err("Tag allocation failure\n");
2228 } while (test_and_set_bit(tag, bqt->tag_map));
2230 bqt->tag_index[tag] = sc->request;
2231 sc->request->tag = tag;
2233 if (!sc->request->special)
2234 sc->request->special = sc;
2243 * fnic_scsi_host_end_tag
2244 * frees tag allocated by fnic_scsi_host_start_tag.
2247 fnic_scsi_host_end_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2249 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2250 int tag = sc->request->tag;
2252 if (tag == SCSI_NO_TAG)
2255 BUG_ON(!bqt || !bqt->tag_index[tag]);
2259 bqt->tag_index[tag] = NULL;
2260 clear_bit(tag, bqt->tag_map);
2266 * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
2267 * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
2270 int fnic_device_reset(struct scsi_cmnd *sc)
2272 struct fc_lport *lp;
2274 struct fnic_io_req *io_req = NULL;
2275 struct fc_rport *rport;
2278 spinlock_t *io_lock;
2279 unsigned long flags;
2280 unsigned long start_time = 0;
2281 struct scsi_lun fc_lun;
2282 struct fnic_stats *fnic_stats;
2283 struct reset_stats *reset_stats;
2285 DECLARE_COMPLETION_ONSTACK(tm_done);
2286 int tag_gen_flag = 0; /*to track tags allocated by fnic driver*/
2289 /* Wait for rport to unblock */
2290 fc_block_scsi_eh(sc);
2292 /* Get local-port, check ready and link up */
2293 lp = shost_priv(sc->device->host);
2295 fnic = lport_priv(lp);
2296 fnic_stats = &fnic->fnic_stats;
2297 reset_stats = &fnic->fnic_stats.reset_stats;
2299 atomic64_inc(&reset_stats->device_resets);
2301 rport = starget_to_rport(scsi_target(sc->device));
2302 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2303 "Device reset called FCID 0x%x, LUN 0x%llx sc 0x%p\n",
2304 rport->port_id, sc->device->lun, sc);
2306 if (lp->state != LPORT_ST_READY || !(lp->link_up))
2307 goto fnic_device_reset_end;
2309 /* Check if remote port up */
2310 if (fc_remote_port_chkready(rport)) {
2311 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
2312 goto fnic_device_reset_end;
2315 CMD_FLAGS(sc) = FNIC_DEVICE_RESET;
2316 /* Allocate tag if not present */
2318 tag = sc->request->tag;
2319 if (unlikely(tag < 0)) {
2321 * XXX(hch): current the midlayer fakes up a struct
2322 * request for the explicit reset ioctls, and those
2323 * don't have a tag allocated to them. The below
2324 * code pokes into midlayer structures to paper over
2325 * this design issue, but that won't work for blk-mq.
2327 * Either someone who can actually test the hardware
2328 * will have to come up with a similar hack for the
2329 * blk-mq case, or we'll have to bite the bullet and
2330 * fix the way the EH ioctls work for real, but until
2331 * that happens we fail these explicit requests here.
2334 tag = fnic_scsi_host_start_tag(fnic, sc);
2335 if (unlikely(tag == SCSI_NO_TAG))
2336 goto fnic_device_reset_end;
2340 io_lock = fnic_io_lock_hash(fnic, sc);
2341 spin_lock_irqsave(io_lock, flags);
2342 io_req = (struct fnic_io_req *)CMD_SP(sc);
2345 * If there is a io_req attached to this command, then use it,
2346 * else allocate a new one.
2349 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
2351 spin_unlock_irqrestore(io_lock, flags);
2352 goto fnic_device_reset_end;
2354 memset(io_req, 0, sizeof(*io_req));
2355 io_req->port_id = rport->port_id;
2356 CMD_SP(sc) = (char *)io_req;
2358 io_req->dr_done = &tm_done;
2359 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
2360 CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
2361 spin_unlock_irqrestore(io_lock, flags);
2363 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %x\n", tag);
2366 * issue the device reset, if enqueue failed, clean up the ioreq
2367 * and break assoc with scsi cmd
2369 if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
2370 spin_lock_irqsave(io_lock, flags);
2371 io_req = (struct fnic_io_req *)CMD_SP(sc);
2373 io_req->dr_done = NULL;
2374 goto fnic_device_reset_clean;
2376 spin_lock_irqsave(io_lock, flags);
2377 CMD_FLAGS(sc) |= FNIC_DEV_RST_ISSUED;
2378 spin_unlock_irqrestore(io_lock, flags);
2381 * Wait on the local completion for LUN reset. The io_req may be
2382 * freed while we wait since we hold no lock.
2384 wait_for_completion_timeout(&tm_done,
2385 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2387 spin_lock_irqsave(io_lock, flags);
2388 io_req = (struct fnic_io_req *)CMD_SP(sc);
2390 spin_unlock_irqrestore(io_lock, flags);
2391 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2392 "io_req is null tag 0x%x sc 0x%p\n", tag, sc);
2393 goto fnic_device_reset_end;
2395 io_req->dr_done = NULL;
2397 status = CMD_LR_STATUS(sc);
2400 * If lun reset not completed, bail out with failed. io_req
2401 * gets cleaned up during higher levels of EH
2403 if (status == FCPIO_INVALID_CODE) {
2404 atomic64_inc(&reset_stats->device_reset_timeouts);
2405 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2406 "Device reset timed out\n");
2407 CMD_FLAGS(sc) |= FNIC_DEV_RST_TIMED_OUT;
2408 spin_unlock_irqrestore(io_lock, flags);
2409 int_to_scsilun(sc->device->lun, &fc_lun);
2411 * Issue abort and terminate on device reset request.
2412 * If q'ing of terminate fails, retry it after a delay.
2415 spin_lock_irqsave(io_lock, flags);
2416 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TERM_ISSUED) {
2417 spin_unlock_irqrestore(io_lock, flags);
2420 spin_unlock_irqrestore(io_lock, flags);
2421 if (fnic_queue_abort_io_req(fnic,
2422 tag | FNIC_TAG_DEV_RST,
2423 FCPIO_ITMF_ABT_TASK_TERM,
2424 fc_lun.scsi_lun, io_req)) {
2425 wait_for_completion_timeout(&tm_done,
2426 msecs_to_jiffies(FNIC_ABT_TERM_DELAY_TIMEOUT));
2428 spin_lock_irqsave(io_lock, flags);
2429 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2430 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2431 io_req->abts_done = &tm_done;
2432 spin_unlock_irqrestore(io_lock, flags);
2433 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2434 "Abort and terminate issued on Device reset "
2435 "tag 0x%x sc 0x%p\n", tag, sc);
2440 spin_lock_irqsave(io_lock, flags);
2441 if (!(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
2442 spin_unlock_irqrestore(io_lock, flags);
2443 wait_for_completion_timeout(&tm_done,
2444 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2447 io_req = (struct fnic_io_req *)CMD_SP(sc);
2448 io_req->abts_done = NULL;
2449 goto fnic_device_reset_clean;
2453 spin_unlock_irqrestore(io_lock, flags);
2456 /* Completed, but not successful, clean up the io_req, return fail */
2457 if (status != FCPIO_SUCCESS) {
2458 spin_lock_irqsave(io_lock, flags);
2459 FNIC_SCSI_DBG(KERN_DEBUG,
2461 "Device reset completed - failed\n");
2462 io_req = (struct fnic_io_req *)CMD_SP(sc);
2463 goto fnic_device_reset_clean;
2467 * Clean up any aborts on this lun that have still not
2468 * completed. If any of these fail, then LUN reset fails.
2469 * clean_pending_aborts cleans all cmds on this lun except
2470 * the lun reset cmd. If all cmds get cleaned, the lun reset
2473 if (fnic_clean_pending_aborts(fnic, sc, new_sc)) {
2474 spin_lock_irqsave(io_lock, flags);
2475 io_req = (struct fnic_io_req *)CMD_SP(sc);
2476 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2477 "Device reset failed"
2478 " since could not abort all IOs\n");
2479 goto fnic_device_reset_clean;
2482 /* Clean lun reset command */
2483 spin_lock_irqsave(io_lock, flags);
2484 io_req = (struct fnic_io_req *)CMD_SP(sc);
2486 /* Completed, and successful */
2489 fnic_device_reset_clean:
2493 spin_unlock_irqrestore(io_lock, flags);
2496 start_time = io_req->start_time;
2497 fnic_release_ioreq_buf(fnic, io_req, sc);
2498 mempool_free(io_req, fnic->io_req_pool);
2501 fnic_device_reset_end:
2502 FNIC_TRACE(fnic_device_reset, sc->device->host->host_no,
2503 sc->request->tag, sc,
2504 jiffies_to_msecs(jiffies - start_time),
2505 0, ((u64)sc->cmnd[0] << 32 |
2506 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
2507 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
2508 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
2510 /* free tag if it is allocated */
2511 if (unlikely(tag_gen_flag))
2512 fnic_scsi_host_end_tag(fnic, sc);
2514 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2515 "Returning from device reset %s\n",
2517 "SUCCESS" : "FAILED");
2520 atomic64_inc(&reset_stats->device_reset_failures);
2525 /* Clean up all IOs, clean up libFC local port */
2526 int fnic_reset(struct Scsi_Host *shost)
2528 struct fc_lport *lp;
2531 struct reset_stats *reset_stats;
2533 lp = shost_priv(shost);
2534 fnic = lport_priv(lp);
2535 reset_stats = &fnic->fnic_stats.reset_stats;
2537 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2538 "fnic_reset called\n");
2540 atomic64_inc(&reset_stats->fnic_resets);
2543 * Reset local port, this will clean up libFC exchanges,
2544 * reset remote port sessions, and if link is up, begin flogi
2546 ret = lp->tt.lport_reset(lp);
2548 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2549 "Returning from fnic reset %s\n",
2551 "SUCCESS" : "FAILED");
2554 atomic64_inc(&reset_stats->fnic_reset_completions);
2556 atomic64_inc(&reset_stats->fnic_reset_failures);
2562 * SCSI Error handling calls driver's eh_host_reset if all prior
2563 * error handling levels return FAILED. If host reset completes
2564 * successfully, and if link is up, then Fabric login begins.
2566 * Host Reset is the highest level of error recovery. If this fails, then
2567 * host is offlined by SCSI.
2570 int fnic_host_reset(struct scsi_cmnd *sc)
2573 unsigned long wait_host_tmo;
2574 struct Scsi_Host *shost = sc->device->host;
2575 struct fc_lport *lp = shost_priv(shost);
2578 * If fnic_reset is successful, wait for fabric login to complete
2579 * scsi-ml tries to send a TUR to every device if host reset is
2580 * successful, so before returning to scsi, fabric should be up
2582 ret = (fnic_reset(shost) == 0) ? SUCCESS : FAILED;
2583 if (ret == SUCCESS) {
2584 wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
2586 while (time_before(jiffies, wait_host_tmo)) {
2587 if ((lp->state == LPORT_ST_READY) &&
2600 * This fxn is called from libFC when host is removed
2602 void fnic_scsi_abort_io(struct fc_lport *lp)
2605 unsigned long flags;
2606 enum fnic_state old_state;
2607 struct fnic *fnic = lport_priv(lp);
2608 DECLARE_COMPLETION_ONSTACK(remove_wait);
2610 /* Issue firmware reset for fnic, wait for reset to complete */
2612 spin_lock_irqsave(&fnic->fnic_lock, flags);
2613 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2614 /* fw reset is in progress, poll for its completion */
2615 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2616 schedule_timeout(msecs_to_jiffies(100));
2617 goto retry_fw_reset;
2620 fnic->remove_wait = &remove_wait;
2621 old_state = fnic->state;
2622 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
2623 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
2624 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2626 err = fnic_fw_reset_handler(fnic);
2628 spin_lock_irqsave(&fnic->fnic_lock, flags);
2629 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2630 fnic->state = old_state;
2631 fnic->remove_wait = NULL;
2632 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2636 /* Wait for firmware reset to complete */
2637 wait_for_completion_timeout(&remove_wait,
2638 msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
2640 spin_lock_irqsave(&fnic->fnic_lock, flags);
2641 fnic->remove_wait = NULL;
2642 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2643 "fnic_scsi_abort_io %s\n",
2644 (fnic->state == FNIC_IN_ETH_MODE) ?
2645 "SUCCESS" : "FAILED");
2646 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2651 * This fxn called from libFC to clean up driver IO state on link down
2653 void fnic_scsi_cleanup(struct fc_lport *lp)
2655 unsigned long flags;
2656 enum fnic_state old_state;
2657 struct fnic *fnic = lport_priv(lp);
2659 /* issue fw reset */
2661 spin_lock_irqsave(&fnic->fnic_lock, flags);
2662 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2663 /* fw reset is in progress, poll for its completion */
2664 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2665 schedule_timeout(msecs_to_jiffies(100));
2666 goto retry_fw_reset;
2668 old_state = fnic->state;
2669 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
2670 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
2671 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2673 if (fnic_fw_reset_handler(fnic)) {
2674 spin_lock_irqsave(&fnic->fnic_lock, flags);
2675 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2676 fnic->state = old_state;
2677 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2682 void fnic_empty_scsi_cleanup(struct fc_lport *lp)
2686 void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
2688 struct fnic *fnic = lport_priv(lp);
2690 /* Non-zero sid, nothing to do */
2692 goto call_fc_exch_mgr_reset;
2695 fnic_rport_exch_reset(fnic, did);
2696 goto call_fc_exch_mgr_reset;
2701 * link down or device being removed
2703 if (!fnic->in_remove)
2704 fnic_scsi_cleanup(lp);
2706 fnic_scsi_abort_io(lp);
2708 /* call libFC exch mgr reset to reset its exchanges */
2709 call_fc_exch_mgr_reset:
2710 fc_exch_mgr_reset(lp, sid, did);
2715 * fnic_is_abts_pending() is a helper function that
2716 * walks through tag map to check if there is any IOs pending,if there is one,
2717 * then it returns 1 (true), otherwise 0 (false)
2718 * if @lr_sc is non NULL, then it checks IOs specific to particular LUN,
2719 * otherwise, it checks for all IOs.
2721 int fnic_is_abts_pending(struct fnic *fnic, struct scsi_cmnd *lr_sc)
2724 struct fnic_io_req *io_req;
2725 spinlock_t *io_lock;
2726 unsigned long flags;
2728 struct scsi_cmnd *sc;
2729 struct scsi_device *lun_dev = NULL;
2732 lun_dev = lr_sc->device;
2734 /* walk again to check, if IOs are still pending in fw */
2735 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
2736 sc = scsi_host_find_tag(fnic->lport->host, tag);
2738 * ignore this lun reset cmd or cmds that do not belong to
2741 if (!sc || (lr_sc && (sc->device != lun_dev || sc == lr_sc)))
2744 io_lock = fnic_io_lock_hash(fnic, sc);
2745 spin_lock_irqsave(io_lock, flags);
2747 io_req = (struct fnic_io_req *)CMD_SP(sc);
2749 if (!io_req || sc->device != lun_dev) {
2750 spin_unlock_irqrestore(io_lock, flags);
2755 * Found IO that is still pending with firmware and
2756 * belongs to the LUN that we are resetting
2758 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2759 "Found IO in %s on lun\n",
2760 fnic_ioreq_state_to_str(CMD_STATE(sc)));
2762 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
2764 spin_unlock_irqrestore(io_lock, flags);