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 struct fc_rport_priv *rdata;
443 spinlock_t *io_lock = NULL;
444 int io_lock_acquired = 0;
446 if (unlikely(fnic_chk_state_flags_locked(fnic, FNIC_FLAGS_IO_BLOCKED)))
447 return SCSI_MLQUEUE_HOST_BUSY;
449 if (unlikely(fnic_chk_state_flags_locked(fnic, FNIC_FLAGS_FWRESET)))
450 return SCSI_MLQUEUE_HOST_BUSY;
452 rport = starget_to_rport(scsi_target(sc->device));
453 ret = fc_remote_port_chkready(rport);
455 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
461 rdata = lp->tt.rport_lookup(lp, rport->port_id);
462 if (!rdata || (rdata->rp_state == RPORT_ST_DELETE)) {
463 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
464 "returning IO as rport is removed\n");
465 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
466 sc->result = DID_NO_CONNECT;
471 if (lp->state != LPORT_ST_READY || !(lp->link_up))
472 return SCSI_MLQUEUE_HOST_BUSY;
474 atomic_inc(&fnic->in_flight);
477 * Release host lock, use driver resource specific locks from here.
478 * Don't re-enable interrupts in case they were disabled prior to the
479 * caller disabling them.
481 spin_unlock(lp->host->host_lock);
482 CMD_STATE(sc) = FNIC_IOREQ_NOT_INITED;
483 CMD_FLAGS(sc) = FNIC_NO_FLAGS;
485 /* Get a new io_req for this SCSI IO */
486 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
488 atomic64_inc(&fnic_stats->io_stats.alloc_failures);
489 ret = SCSI_MLQUEUE_HOST_BUSY;
492 memset(io_req, 0, sizeof(*io_req));
494 /* Map the data buffer */
495 sg_count = scsi_dma_map(sc);
497 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
498 sc->request->tag, sc, 0, sc->cmnd[0],
499 sg_count, CMD_STATE(sc));
500 mempool_free(io_req, fnic->io_req_pool);
504 /* Determine the type of scatter/gather list we need */
505 io_req->sgl_cnt = sg_count;
506 io_req->sgl_type = FNIC_SGL_CACHE_DFLT;
507 if (sg_count > FNIC_DFLT_SG_DESC_CNT)
508 io_req->sgl_type = FNIC_SGL_CACHE_MAX;
512 mempool_alloc(fnic->io_sgl_pool[io_req->sgl_type],
514 if (!io_req->sgl_list) {
515 atomic64_inc(&fnic_stats->io_stats.alloc_failures);
516 ret = SCSI_MLQUEUE_HOST_BUSY;
518 mempool_free(io_req, fnic->io_req_pool);
522 /* Cache sgl list allocated address before alignment */
523 io_req->sgl_list_alloc = io_req->sgl_list;
524 ptr = (unsigned long) io_req->sgl_list;
525 if (ptr % FNIC_SG_DESC_ALIGN) {
526 io_req->sgl_list = (struct host_sg_desc *)
527 (((unsigned long) ptr
528 + FNIC_SG_DESC_ALIGN - 1)
529 & ~(FNIC_SG_DESC_ALIGN - 1));
534 * Will acquire lock defore setting to IO initialized.
537 io_lock = fnic_io_lock_hash(fnic, sc);
538 spin_lock_irqsave(io_lock, flags);
540 /* initialize rest of io_req */
541 io_lock_acquired = 1;
542 io_req->port_id = rport->port_id;
543 io_req->start_time = jiffies;
544 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
545 CMD_SP(sc) = (char *)io_req;
546 CMD_FLAGS(sc) |= FNIC_IO_INITIALIZED;
547 sc->scsi_done = done;
549 /* create copy wq desc and enqueue it */
550 wq = &fnic->wq_copy[0];
551 ret = fnic_queue_wq_copy_desc(fnic, wq, io_req, sc, sg_count);
554 * In case another thread cancelled the request,
555 * refetch the pointer under the lock.
557 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
558 sc->request->tag, sc, 0, 0, 0,
559 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
560 io_req = (struct fnic_io_req *)CMD_SP(sc);
562 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
563 spin_unlock_irqrestore(io_lock, flags);
565 fnic_release_ioreq_buf(fnic, io_req, sc);
566 mempool_free(io_req, fnic->io_req_pool);
568 atomic_dec(&fnic->in_flight);
569 /* acquire host lock before returning to SCSI */
570 spin_lock(lp->host->host_lock);
573 atomic64_inc(&fnic_stats->io_stats.active_ios);
574 atomic64_inc(&fnic_stats->io_stats.num_ios);
575 if (atomic64_read(&fnic_stats->io_stats.active_ios) >
576 atomic64_read(&fnic_stats->io_stats.max_active_ios))
577 atomic64_set(&fnic_stats->io_stats.max_active_ios,
578 atomic64_read(&fnic_stats->io_stats.active_ios));
580 /* REVISIT: Use per IO lock in the final code */
581 CMD_FLAGS(sc) |= FNIC_IO_ISSUED;
584 cmd_trace = ((u64)sc->cmnd[0] << 56 | (u64)sc->cmnd[7] << 40 |
585 (u64)sc->cmnd[8] << 32 | (u64)sc->cmnd[2] << 24 |
586 (u64)sc->cmnd[3] << 16 | (u64)sc->cmnd[4] << 8 |
589 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
590 sc->request->tag, sc, io_req,
592 (((u64)CMD_FLAGS(sc) >> 32) | CMD_STATE(sc)));
594 /* if only we issued IO, will we have the io lock */
595 if (io_lock_acquired)
596 spin_unlock_irqrestore(io_lock, flags);
598 atomic_dec(&fnic->in_flight);
599 /* acquire host lock before returning to SCSI */
600 spin_lock(lp->host->host_lock);
604 DEF_SCSI_QCMD(fnic_queuecommand)
607 * fnic_fcpio_fw_reset_cmpl_handler
608 * Routine to handle fw reset completion
610 static int fnic_fcpio_fw_reset_cmpl_handler(struct fnic *fnic,
611 struct fcpio_fw_req *desc)
615 struct fcpio_tag tag;
618 struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
620 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
622 atomic64_inc(&reset_stats->fw_reset_completions);
624 /* Clean up all outstanding io requests */
625 fnic_cleanup_io(fnic, SCSI_NO_TAG);
627 atomic64_set(&fnic->fnic_stats.fw_stats.active_fw_reqs, 0);
628 atomic64_set(&fnic->fnic_stats.io_stats.active_ios, 0);
630 spin_lock_irqsave(&fnic->fnic_lock, flags);
632 /* fnic should be in FC_TRANS_ETH_MODE */
633 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) {
634 /* Check status of reset completion */
636 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
637 "reset cmpl success\n");
638 /* Ready to send flogi out */
639 fnic->state = FNIC_IN_ETH_MODE;
641 FNIC_SCSI_DBG(KERN_DEBUG,
643 "fnic fw_reset : failed %s\n",
644 fnic_fcpio_status_to_str(hdr_status));
647 * Unable to change to eth mode, cannot send out flogi
648 * Change state to fc mode, so that subsequent Flogi
649 * requests from libFC will cause more attempts to
650 * reset the firmware. Free the cached flogi
652 fnic->state = FNIC_IN_FC_MODE;
653 atomic64_inc(&reset_stats->fw_reset_failures);
657 FNIC_SCSI_DBG(KERN_DEBUG,
659 "Unexpected state %s while processing"
660 " reset cmpl\n", fnic_state_to_str(fnic->state));
661 atomic64_inc(&reset_stats->fw_reset_failures);
665 /* Thread removing device blocks till firmware reset is complete */
666 if (fnic->remove_wait)
667 complete(fnic->remove_wait);
670 * If fnic is being removed, or fw reset failed
671 * free the flogi frame. Else, send it out
673 if (fnic->remove_wait || ret) {
674 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
675 skb_queue_purge(&fnic->tx_queue);
676 goto reset_cmpl_handler_end;
679 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
683 reset_cmpl_handler_end:
684 fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
690 * fnic_fcpio_flogi_reg_cmpl_handler
691 * Routine to handle flogi register completion
693 static int fnic_fcpio_flogi_reg_cmpl_handler(struct fnic *fnic,
694 struct fcpio_fw_req *desc)
698 struct fcpio_tag tag;
702 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
704 /* Update fnic state based on status of flogi reg completion */
705 spin_lock_irqsave(&fnic->fnic_lock, flags);
707 if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE) {
709 /* Check flogi registration completion status */
711 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
712 "flog reg succeeded\n");
713 fnic->state = FNIC_IN_FC_MODE;
715 FNIC_SCSI_DBG(KERN_DEBUG,
717 "fnic flogi reg :failed %s\n",
718 fnic_fcpio_status_to_str(hdr_status));
719 fnic->state = FNIC_IN_ETH_MODE;
723 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
724 "Unexpected fnic state %s while"
725 " processing flogi reg completion\n",
726 fnic_state_to_str(fnic->state));
731 if (fnic->stop_rx_link_events) {
732 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
733 goto reg_cmpl_handler_end;
735 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
738 queue_work(fnic_event_queue, &fnic->frame_work);
740 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
743 reg_cmpl_handler_end:
747 static inline int is_ack_index_in_range(struct vnic_wq_copy *wq,
750 if (wq->to_clean_index <= wq->to_use_index) {
751 /* out of range, stale request_out index */
752 if (request_out < wq->to_clean_index ||
753 request_out >= wq->to_use_index)
756 /* out of range, stale request_out index */
757 if (request_out < wq->to_clean_index &&
758 request_out >= wq->to_use_index)
761 /* request_out index is in range */
767 * Mark that ack received and store the Ack index. If there are multiple
768 * acks received before Tx thread cleans it up, the latest value will be
769 * used which is correct behavior. This state should be in the copy Wq
770 * instead of in the fnic
772 static inline void fnic_fcpio_ack_handler(struct fnic *fnic,
773 unsigned int cq_index,
774 struct fcpio_fw_req *desc)
776 struct vnic_wq_copy *wq;
777 u16 request_out = desc->u.ack.request_out;
779 u64 *ox_id_tag = (u64 *)(void *)desc;
781 /* mark the ack state */
782 wq = &fnic->wq_copy[cq_index - fnic->raw_wq_count - fnic->rq_count];
783 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
785 fnic->fnic_stats.misc_stats.last_ack_time = jiffies;
786 if (is_ack_index_in_range(wq, request_out)) {
787 fnic->fw_ack_index[0] = request_out;
788 fnic->fw_ack_recd[0] = 1;
791 &fnic->fnic_stats.misc_stats.ack_index_out_of_range);
793 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
794 FNIC_TRACE(fnic_fcpio_ack_handler,
795 fnic->lport->host->host_no, 0, 0, ox_id_tag[2], ox_id_tag[3],
796 ox_id_tag[4], ox_id_tag[5]);
800 * fnic_fcpio_icmnd_cmpl_handler
801 * Routine to handle icmnd completions
803 static void fnic_fcpio_icmnd_cmpl_handler(struct fnic *fnic,
804 struct fcpio_fw_req *desc)
808 struct fcpio_tag tag;
811 struct fcpio_icmnd_cmpl *icmnd_cmpl;
812 struct fnic_io_req *io_req;
813 struct scsi_cmnd *sc;
814 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
818 unsigned long start_time;
820 /* Decode the cmpl description to get the io_req id */
821 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
822 fcpio_tag_id_dec(&tag, &id);
823 icmnd_cmpl = &desc->u.icmnd_cmpl;
825 if (id >= fnic->fnic_max_tag_id) {
826 shost_printk(KERN_ERR, fnic->lport->host,
827 "Tag out of range tag %x hdr status = %s\n",
828 id, fnic_fcpio_status_to_str(hdr_status));
832 sc = scsi_host_find_tag(fnic->lport->host, id);
835 atomic64_inc(&fnic_stats->io_stats.sc_null);
836 shost_printk(KERN_ERR, fnic->lport->host,
837 "icmnd_cmpl sc is null - "
838 "hdr status = %s tag = 0x%x desc = 0x%p\n",
839 fnic_fcpio_status_to_str(hdr_status), id, desc);
840 FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
841 fnic->lport->host->host_no, id,
842 ((u64)icmnd_cmpl->_resvd0[1] << 16 |
843 (u64)icmnd_cmpl->_resvd0[0]),
844 ((u64)hdr_status << 16 |
845 (u64)icmnd_cmpl->scsi_status << 8 |
846 (u64)icmnd_cmpl->flags), desc,
847 (u64)icmnd_cmpl->residual, 0);
851 io_lock = fnic_io_lock_hash(fnic, sc);
852 spin_lock_irqsave(io_lock, flags);
853 io_req = (struct fnic_io_req *)CMD_SP(sc);
854 WARN_ON_ONCE(!io_req);
856 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
857 CMD_FLAGS(sc) |= FNIC_IO_REQ_NULL;
858 spin_unlock_irqrestore(io_lock, flags);
859 shost_printk(KERN_ERR, fnic->lport->host,
860 "icmnd_cmpl io_req is null - "
861 "hdr status = %s tag = 0x%x sc 0x%p\n",
862 fnic_fcpio_status_to_str(hdr_status), id, sc);
865 start_time = io_req->start_time;
867 /* firmware completed the io */
868 io_req->io_completed = 1;
871 * if SCSI-ML has already issued abort on this command,
872 * ignore completion of the IO. The abts path will clean it up
874 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
875 spin_unlock_irqrestore(io_lock, flags);
876 CMD_FLAGS(sc) |= FNIC_IO_ABTS_PENDING;
877 switch (hdr_status) {
879 CMD_FLAGS(sc) |= FNIC_IO_DONE;
880 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
881 "icmnd_cmpl ABTS pending hdr status = %s "
882 "sc 0x%p scsi_status %x residual %d\n",
883 fnic_fcpio_status_to_str(hdr_status), sc,
884 icmnd_cmpl->scsi_status,
885 icmnd_cmpl->residual);
888 CMD_FLAGS(sc) |= FNIC_IO_ABORTED;
891 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
892 "icmnd_cmpl abts pending "
893 "hdr status = %s tag = 0x%x sc = 0x%p\n",
894 fnic_fcpio_status_to_str(hdr_status),
901 /* Mark the IO as complete */
902 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
904 icmnd_cmpl = &desc->u.icmnd_cmpl;
906 switch (hdr_status) {
908 sc->result = (DID_OK << 16) | icmnd_cmpl->scsi_status;
909 xfer_len = scsi_bufflen(sc);
910 scsi_set_resid(sc, icmnd_cmpl->residual);
912 if (icmnd_cmpl->flags & FCPIO_ICMND_CMPL_RESID_UNDER)
913 xfer_len -= icmnd_cmpl->residual;
915 if (icmnd_cmpl->scsi_status == SAM_STAT_TASK_SET_FULL)
916 atomic64_inc(&fnic_stats->misc_stats.queue_fulls);
919 case FCPIO_TIMEOUT: /* request was timed out */
920 atomic64_inc(&fnic_stats->misc_stats.fcpio_timeout);
921 sc->result = (DID_TIME_OUT << 16) | icmnd_cmpl->scsi_status;
924 case FCPIO_ABORTED: /* request was aborted */
925 atomic64_inc(&fnic_stats->misc_stats.fcpio_aborted);
926 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
929 case FCPIO_DATA_CNT_MISMATCH: /* recv/sent more/less data than exp. */
930 atomic64_inc(&fnic_stats->misc_stats.data_count_mismatch);
931 scsi_set_resid(sc, icmnd_cmpl->residual);
932 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
935 case FCPIO_OUT_OF_RESOURCE: /* out of resources to complete request */
936 atomic64_inc(&fnic_stats->fw_stats.fw_out_of_resources);
937 sc->result = (DID_REQUEUE << 16) | icmnd_cmpl->scsi_status;
940 case FCPIO_IO_NOT_FOUND: /* requested I/O was not found */
941 atomic64_inc(&fnic_stats->io_stats.io_not_found);
942 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
945 case FCPIO_SGL_INVALID: /* request was aborted due to sgl error */
946 atomic64_inc(&fnic_stats->misc_stats.sgl_invalid);
947 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
950 case FCPIO_FW_ERR: /* request was terminated due fw error */
951 atomic64_inc(&fnic_stats->fw_stats.io_fw_errs);
952 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
955 case FCPIO_MSS_INVALID: /* request was aborted due to mss error */
956 atomic64_inc(&fnic_stats->misc_stats.mss_invalid);
957 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
960 case FCPIO_INVALID_HEADER: /* header contains invalid data */
961 case FCPIO_INVALID_PARAM: /* some parameter in request invalid */
962 case FCPIO_REQ_NOT_SUPPORTED:/* request type is not supported */
964 shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
965 fnic_fcpio_status_to_str(hdr_status));
966 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
970 if (hdr_status != FCPIO_SUCCESS) {
971 atomic64_inc(&fnic_stats->io_stats.io_failures);
972 shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
973 fnic_fcpio_status_to_str(hdr_status));
975 /* Break link with the SCSI command */
977 CMD_FLAGS(sc) |= FNIC_IO_DONE;
979 spin_unlock_irqrestore(io_lock, flags);
981 fnic_release_ioreq_buf(fnic, io_req, sc);
983 mempool_free(io_req, fnic->io_req_pool);
985 cmd_trace = ((u64)hdr_status << 56) |
986 (u64)icmnd_cmpl->scsi_status << 48 |
987 (u64)icmnd_cmpl->flags << 40 | (u64)sc->cmnd[0] << 32 |
988 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
989 (u64)sc->cmnd[4] << 8 | sc->cmnd[5];
991 FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
992 sc->device->host->host_no, id, sc,
993 ((u64)icmnd_cmpl->_resvd0[1] << 56 |
994 (u64)icmnd_cmpl->_resvd0[0] << 48 |
995 jiffies_to_msecs(jiffies - start_time)),
997 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
999 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
1000 fnic->lport->host_stats.fcp_input_requests++;
1001 fnic->fcp_input_bytes += xfer_len;
1002 } else if (sc->sc_data_direction == DMA_TO_DEVICE) {
1003 fnic->lport->host_stats.fcp_output_requests++;
1004 fnic->fcp_output_bytes += xfer_len;
1006 fnic->lport->host_stats.fcp_control_requests++;
1008 atomic64_dec(&fnic_stats->io_stats.active_ios);
1009 if (atomic64_read(&fnic->io_cmpl_skip))
1010 atomic64_dec(&fnic->io_cmpl_skip);
1012 atomic64_inc(&fnic_stats->io_stats.io_completions);
1014 /* Call SCSI completion function to complete the IO */
1019 /* fnic_fcpio_itmf_cmpl_handler
1020 * Routine to handle itmf completions
1022 static void fnic_fcpio_itmf_cmpl_handler(struct fnic *fnic,
1023 struct fcpio_fw_req *desc)
1027 struct fcpio_tag tag;
1029 struct scsi_cmnd *sc;
1030 struct fnic_io_req *io_req;
1031 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1032 struct abort_stats *abts_stats = &fnic->fnic_stats.abts_stats;
1033 struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
1034 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
1035 unsigned long flags;
1036 spinlock_t *io_lock;
1037 unsigned long start_time;
1039 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
1040 fcpio_tag_id_dec(&tag, &id);
1042 if ((id & FNIC_TAG_MASK) >= fnic->fnic_max_tag_id) {
1043 shost_printk(KERN_ERR, fnic->lport->host,
1044 "Tag out of range tag %x hdr status = %s\n",
1045 id, fnic_fcpio_status_to_str(hdr_status));
1049 sc = scsi_host_find_tag(fnic->lport->host, id & FNIC_TAG_MASK);
1052 atomic64_inc(&fnic_stats->io_stats.sc_null);
1053 shost_printk(KERN_ERR, fnic->lport->host,
1054 "itmf_cmpl sc is null - hdr status = %s tag = 0x%x\n",
1055 fnic_fcpio_status_to_str(hdr_status), id);
1058 io_lock = fnic_io_lock_hash(fnic, sc);
1059 spin_lock_irqsave(io_lock, flags);
1060 io_req = (struct fnic_io_req *)CMD_SP(sc);
1061 WARN_ON_ONCE(!io_req);
1063 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
1064 spin_unlock_irqrestore(io_lock, flags);
1065 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
1066 shost_printk(KERN_ERR, fnic->lport->host,
1067 "itmf_cmpl io_req is null - "
1068 "hdr status = %s tag = 0x%x sc 0x%p\n",
1069 fnic_fcpio_status_to_str(hdr_status), id, sc);
1072 start_time = io_req->start_time;
1074 if ((id & FNIC_TAG_ABORT) && (id & FNIC_TAG_DEV_RST)) {
1075 /* Abort and terminate completion of device reset req */
1076 /* REVISIT : Add asserts about various flags */
1077 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1078 "dev reset abts cmpl recd. id %x status %s\n",
1079 id, fnic_fcpio_status_to_str(hdr_status));
1080 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1081 CMD_ABTS_STATUS(sc) = hdr_status;
1082 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1083 if (io_req->abts_done)
1084 complete(io_req->abts_done);
1085 spin_unlock_irqrestore(io_lock, flags);
1086 } else if (id & FNIC_TAG_ABORT) {
1087 /* Completion of abort cmd */
1088 switch (hdr_status) {
1092 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1093 atomic64_inc(&abts_stats->abort_fw_timeouts);
1096 &term_stats->terminate_fw_timeouts);
1098 case FCPIO_IO_NOT_FOUND:
1099 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1100 atomic64_inc(&abts_stats->abort_io_not_found);
1103 &term_stats->terminate_io_not_found);
1106 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1107 atomic64_inc(&abts_stats->abort_failures);
1110 &term_stats->terminate_failures);
1113 if (CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING) {
1114 /* This is a late completion. Ignore it */
1115 spin_unlock_irqrestore(io_lock, flags);
1118 CMD_ABTS_STATUS(sc) = hdr_status;
1119 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
1121 atomic64_dec(&fnic_stats->io_stats.active_ios);
1122 if (atomic64_read(&fnic->io_cmpl_skip))
1123 atomic64_dec(&fnic->io_cmpl_skip);
1125 atomic64_inc(&fnic_stats->io_stats.io_completions);
1127 if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE)))
1128 atomic64_inc(&misc_stats->no_icmnd_itmf_cmpls);
1130 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1131 "abts cmpl recd. id %d status %s\n",
1132 (int)(id & FNIC_TAG_MASK),
1133 fnic_fcpio_status_to_str(hdr_status));
1136 * If scsi_eh thread is blocked waiting for abts to complete,
1137 * signal completion to it. IO will be cleaned in the thread
1138 * else clean it in this context
1140 if (io_req->abts_done) {
1141 complete(io_req->abts_done);
1142 spin_unlock_irqrestore(io_lock, flags);
1144 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1145 "abts cmpl, completing IO\n");
1147 sc->result = (DID_ERROR << 16);
1149 spin_unlock_irqrestore(io_lock, flags);
1151 fnic_release_ioreq_buf(fnic, io_req, sc);
1152 mempool_free(io_req, fnic->io_req_pool);
1153 if (sc->scsi_done) {
1154 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1155 sc->device->host->host_no, id,
1157 jiffies_to_msecs(jiffies - start_time),
1159 (((u64)hdr_status << 40) |
1160 (u64)sc->cmnd[0] << 32 |
1161 (u64)sc->cmnd[2] << 24 |
1162 (u64)sc->cmnd[3] << 16 |
1163 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1164 (((u64)CMD_FLAGS(sc) << 32) |
1170 } else if (id & FNIC_TAG_DEV_RST) {
1171 /* Completion of device reset */
1172 CMD_LR_STATUS(sc) = hdr_status;
1173 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1174 spin_unlock_irqrestore(io_lock, flags);
1175 CMD_FLAGS(sc) |= FNIC_DEV_RST_ABTS_PENDING;
1176 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1177 sc->device->host->host_no, id, sc,
1178 jiffies_to_msecs(jiffies - start_time),
1180 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1181 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1182 "Terminate pending "
1183 "dev reset cmpl recd. id %d status %s\n",
1184 (int)(id & FNIC_TAG_MASK),
1185 fnic_fcpio_status_to_str(hdr_status));
1188 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TIMED_OUT) {
1189 /* Need to wait for terminate completion */
1190 spin_unlock_irqrestore(io_lock, flags);
1191 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1192 sc->device->host->host_no, id, sc,
1193 jiffies_to_msecs(jiffies - start_time),
1195 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1196 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1197 "dev reset cmpl recd after time out. "
1198 "id %d status %s\n",
1199 (int)(id & FNIC_TAG_MASK),
1200 fnic_fcpio_status_to_str(hdr_status));
1203 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
1204 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1205 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1206 "dev reset cmpl recd. id %d status %s\n",
1207 (int)(id & FNIC_TAG_MASK),
1208 fnic_fcpio_status_to_str(hdr_status));
1209 if (io_req->dr_done)
1210 complete(io_req->dr_done);
1211 spin_unlock_irqrestore(io_lock, flags);
1214 shost_printk(KERN_ERR, fnic->lport->host,
1215 "Unexpected itmf io state %s tag %x\n",
1216 fnic_ioreq_state_to_str(CMD_STATE(sc)), id);
1217 spin_unlock_irqrestore(io_lock, flags);
1223 * fnic_fcpio_cmpl_handler
1224 * Routine to service the cq for wq_copy
1226 static int fnic_fcpio_cmpl_handler(struct vnic_dev *vdev,
1227 unsigned int cq_index,
1228 struct fcpio_fw_req *desc)
1230 struct fnic *fnic = vnic_dev_priv(vdev);
1232 switch (desc->hdr.type) {
1233 case FCPIO_ICMND_CMPL: /* fw completed a command */
1234 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1235 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
1236 case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
1237 case FCPIO_RESET_CMPL: /* fw completed reset */
1238 atomic64_dec(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1244 switch (desc->hdr.type) {
1245 case FCPIO_ACK: /* fw copied copy wq desc to its queue */
1246 fnic_fcpio_ack_handler(fnic, cq_index, desc);
1249 case FCPIO_ICMND_CMPL: /* fw completed a command */
1250 fnic_fcpio_icmnd_cmpl_handler(fnic, desc);
1253 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1254 fnic_fcpio_itmf_cmpl_handler(fnic, desc);
1257 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
1258 case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
1259 fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
1262 case FCPIO_RESET_CMPL: /* fw completed reset */
1263 fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
1267 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1268 "firmware completion type %d\n",
1277 * fnic_wq_copy_cmpl_handler
1278 * Routine to process wq copy
1280 int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
1282 unsigned int wq_work_done = 0;
1283 unsigned int i, cq_index;
1284 unsigned int cur_work_done;
1286 for (i = 0; i < fnic->wq_copy_count; i++) {
1287 cq_index = i + fnic->raw_wq_count + fnic->rq_count;
1288 cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
1289 fnic_fcpio_cmpl_handler,
1291 wq_work_done += cur_work_done;
1293 return wq_work_done;
1296 static void fnic_cleanup_io(struct fnic *fnic, int exclude_id)
1299 struct fnic_io_req *io_req;
1300 unsigned long flags = 0;
1301 struct scsi_cmnd *sc;
1302 spinlock_t *io_lock;
1303 unsigned long start_time = 0;
1304 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1306 for (i = 0; i < fnic->fnic_max_tag_id; i++) {
1307 if (i == exclude_id)
1310 io_lock = fnic_io_lock_tag(fnic, i);
1311 spin_lock_irqsave(io_lock, flags);
1312 sc = scsi_host_find_tag(fnic->lport->host, i);
1314 spin_unlock_irqrestore(io_lock, flags);
1318 io_req = (struct fnic_io_req *)CMD_SP(sc);
1319 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1320 !(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
1322 * We will be here only when FW completes reset
1323 * without sending completions for outstanding ios.
1325 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1326 if (io_req && io_req->dr_done)
1327 complete(io_req->dr_done);
1328 else if (io_req && io_req->abts_done)
1329 complete(io_req->abts_done);
1330 spin_unlock_irqrestore(io_lock, flags);
1332 } else if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1333 spin_unlock_irqrestore(io_lock, flags);
1337 spin_unlock_irqrestore(io_lock, flags);
1338 goto cleanup_scsi_cmd;
1343 spin_unlock_irqrestore(io_lock, flags);
1346 * If there is a scsi_cmnd associated with this io_req, then
1347 * free the corresponding state
1349 start_time = io_req->start_time;
1350 fnic_release_ioreq_buf(fnic, io_req, sc);
1351 mempool_free(io_req, fnic->io_req_pool);
1354 sc->result = DID_TRANSPORT_DISRUPTED << 16;
1355 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1356 "%s: sc duration = %lu DID_TRANSPORT_DISRUPTED\n",
1357 __func__, (jiffies - start_time));
1359 if (atomic64_read(&fnic->io_cmpl_skip))
1360 atomic64_dec(&fnic->io_cmpl_skip);
1362 atomic64_inc(&fnic_stats->io_stats.io_completions);
1364 /* Complete the command to SCSI */
1365 if (sc->scsi_done) {
1366 FNIC_TRACE(fnic_cleanup_io,
1367 sc->device->host->host_no, i, sc,
1368 jiffies_to_msecs(jiffies - start_time),
1369 0, ((u64)sc->cmnd[0] << 32 |
1370 (u64)sc->cmnd[2] << 24 |
1371 (u64)sc->cmnd[3] << 16 |
1372 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1373 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1380 void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
1381 struct fcpio_host_req *desc)
1384 struct fnic *fnic = vnic_dev_priv(wq->vdev);
1385 struct fnic_io_req *io_req;
1386 struct scsi_cmnd *sc;
1387 unsigned long flags;
1388 spinlock_t *io_lock;
1389 unsigned long start_time = 0;
1391 /* get the tag reference */
1392 fcpio_tag_id_dec(&desc->hdr.tag, &id);
1393 id &= FNIC_TAG_MASK;
1395 if (id >= fnic->fnic_max_tag_id)
1398 sc = scsi_host_find_tag(fnic->lport->host, id);
1402 io_lock = fnic_io_lock_hash(fnic, sc);
1403 spin_lock_irqsave(io_lock, flags);
1405 /* Get the IO context which this desc refers to */
1406 io_req = (struct fnic_io_req *)CMD_SP(sc);
1408 /* fnic interrupts are turned off by now */
1411 spin_unlock_irqrestore(io_lock, flags);
1412 goto wq_copy_cleanup_scsi_cmd;
1417 spin_unlock_irqrestore(io_lock, flags);
1419 start_time = io_req->start_time;
1420 fnic_release_ioreq_buf(fnic, io_req, sc);
1421 mempool_free(io_req, fnic->io_req_pool);
1423 wq_copy_cleanup_scsi_cmd:
1424 sc->result = DID_NO_CONNECT << 16;
1425 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
1426 " DID_NO_CONNECT\n");
1428 if (sc->scsi_done) {
1429 FNIC_TRACE(fnic_wq_copy_cleanup_handler,
1430 sc->device->host->host_no, id, sc,
1431 jiffies_to_msecs(jiffies - start_time),
1432 0, ((u64)sc->cmnd[0] << 32 |
1433 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1434 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1435 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1441 static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
1442 u32 task_req, u8 *fc_lun,
1443 struct fnic_io_req *io_req)
1445 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1446 struct Scsi_Host *host = fnic->lport->host;
1447 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
1448 unsigned long flags;
1450 spin_lock_irqsave(host->host_lock, flags);
1451 if (unlikely(fnic_chk_state_flags_locked(fnic,
1452 FNIC_FLAGS_IO_BLOCKED))) {
1453 spin_unlock_irqrestore(host->host_lock, flags);
1456 atomic_inc(&fnic->in_flight);
1457 spin_unlock_irqrestore(host->host_lock, flags);
1459 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
1461 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1462 free_wq_copy_descs(fnic, wq);
1464 if (!vnic_wq_copy_desc_avail(wq)) {
1465 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1466 atomic_dec(&fnic->in_flight);
1467 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1468 "fnic_queue_abort_io_req: failure: no descriptors\n");
1469 atomic64_inc(&misc_stats->abts_cpwq_alloc_failures);
1472 fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
1473 0, task_req, tag, fc_lun, io_req->port_id,
1474 fnic->config.ra_tov, fnic->config.ed_tov);
1476 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1477 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
1478 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
1479 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
1480 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
1482 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1483 atomic_dec(&fnic->in_flight);
1488 static void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
1493 struct fnic_io_req *io_req;
1494 spinlock_t *io_lock;
1495 unsigned long flags;
1496 struct scsi_cmnd *sc;
1497 struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
1498 struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
1499 struct scsi_lun fc_lun;
1500 enum fnic_ioreq_state old_ioreq_state;
1502 FNIC_SCSI_DBG(KERN_DEBUG,
1504 "fnic_rport_exch_reset called portid 0x%06x\n",
1507 if (fnic->in_remove)
1510 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
1512 io_lock = fnic_io_lock_tag(fnic, tag);
1513 spin_lock_irqsave(io_lock, flags);
1514 sc = scsi_host_find_tag(fnic->lport->host, tag);
1516 spin_unlock_irqrestore(io_lock, flags);
1520 io_req = (struct fnic_io_req *)CMD_SP(sc);
1522 if (!io_req || io_req->port_id != port_id) {
1523 spin_unlock_irqrestore(io_lock, flags);
1527 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1528 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
1529 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1530 "fnic_rport_exch_reset dev rst not pending sc 0x%p\n",
1532 spin_unlock_irqrestore(io_lock, flags);
1537 * Found IO that is still pending with firmware and
1538 * belongs to rport that went away
1540 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1541 spin_unlock_irqrestore(io_lock, flags);
1544 if (io_req->abts_done) {
1545 shost_printk(KERN_ERR, fnic->lport->host,
1546 "fnic_rport_exch_reset: io_req->abts_done is set "
1548 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1551 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1552 shost_printk(KERN_ERR, fnic->lport->host,
1554 "IO not yet issued %p tag 0x%x flags "
1556 sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1558 old_ioreq_state = CMD_STATE(sc);
1559 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1560 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1561 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1562 atomic64_inc(&reset_stats->device_reset_terminates);
1563 abt_tag = (tag | FNIC_TAG_DEV_RST);
1564 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1565 "fnic_rport_exch_reset dev rst sc 0x%p\n",
1569 BUG_ON(io_req->abts_done);
1571 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1572 "fnic_rport_reset_exch: Issuing abts\n");
1574 spin_unlock_irqrestore(io_lock, flags);
1576 /* Now queue the abort command to firmware */
1577 int_to_scsilun(sc->device->lun, &fc_lun);
1579 if (fnic_queue_abort_io_req(fnic, abt_tag,
1580 FCPIO_ITMF_ABT_TASK_TERM,
1581 fc_lun.scsi_lun, io_req)) {
1583 * Revert the cmd state back to old state, if
1584 * it hasn't changed in between. This cmd will get
1585 * aborted later by scsi_eh, or cleaned up during
1588 spin_lock_irqsave(io_lock, flags);
1589 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1590 CMD_STATE(sc) = old_ioreq_state;
1591 spin_unlock_irqrestore(io_lock, flags);
1593 spin_lock_irqsave(io_lock, flags);
1594 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1595 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
1597 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
1598 spin_unlock_irqrestore(io_lock, flags);
1599 atomic64_inc(&term_stats->terminates);
1603 if (term_cnt > atomic64_read(&term_stats->max_terminates))
1604 atomic64_set(&term_stats->max_terminates, term_cnt);
1608 void fnic_terminate_rport_io(struct fc_rport *rport)
1613 struct fnic_io_req *io_req;
1614 spinlock_t *io_lock;
1615 unsigned long flags;
1616 struct scsi_cmnd *sc;
1617 struct scsi_lun fc_lun;
1618 struct fc_rport_libfc_priv *rdata;
1619 struct fc_lport *lport;
1621 struct fc_rport *cmd_rport;
1622 struct reset_stats *reset_stats;
1623 struct terminate_stats *term_stats;
1624 enum fnic_ioreq_state old_ioreq_state;
1627 printk(KERN_ERR "fnic_terminate_rport_io: rport is NULL\n");
1630 rdata = rport->dd_data;
1633 printk(KERN_ERR "fnic_terminate_rport_io: rdata is NULL\n");
1636 lport = rdata->local_port;
1639 printk(KERN_ERR "fnic_terminate_rport_io: lport is NULL\n");
1642 fnic = lport_priv(lport);
1643 FNIC_SCSI_DBG(KERN_DEBUG,
1644 fnic->lport->host, "fnic_terminate_rport_io called"
1645 " wwpn 0x%llx, wwnn0x%llx, rport 0x%p, portid 0x%06x\n",
1646 rport->port_name, rport->node_name, rport,
1649 if (fnic->in_remove)
1652 reset_stats = &fnic->fnic_stats.reset_stats;
1653 term_stats = &fnic->fnic_stats.term_stats;
1655 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
1657 io_lock = fnic_io_lock_tag(fnic, tag);
1658 spin_lock_irqsave(io_lock, flags);
1659 sc = scsi_host_find_tag(fnic->lport->host, tag);
1661 spin_unlock_irqrestore(io_lock, flags);
1665 cmd_rport = starget_to_rport(scsi_target(sc->device));
1666 if (rport != cmd_rport) {
1667 spin_unlock_irqrestore(io_lock, flags);
1671 io_req = (struct fnic_io_req *)CMD_SP(sc);
1673 if (!io_req || rport != cmd_rport) {
1674 spin_unlock_irqrestore(io_lock, flags);
1678 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1679 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
1680 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1681 "fnic_terminate_rport_io dev rst not pending sc 0x%p\n",
1683 spin_unlock_irqrestore(io_lock, flags);
1687 * Found IO that is still pending with firmware and
1688 * belongs to rport that went away
1690 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1691 spin_unlock_irqrestore(io_lock, flags);
1694 if (io_req->abts_done) {
1695 shost_printk(KERN_ERR, fnic->lport->host,
1696 "fnic_terminate_rport_io: io_req->abts_done is set "
1698 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1700 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1701 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1702 "fnic_terminate_rport_io "
1703 "IO not yet issued %p tag 0x%x flags "
1705 sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1707 old_ioreq_state = CMD_STATE(sc);
1708 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1709 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1710 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1711 atomic64_inc(&reset_stats->device_reset_terminates);
1712 abt_tag = (tag | FNIC_TAG_DEV_RST);
1713 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1714 "fnic_terminate_rport_io dev rst sc 0x%p\n", sc);
1717 BUG_ON(io_req->abts_done);
1719 FNIC_SCSI_DBG(KERN_DEBUG,
1721 "fnic_terminate_rport_io: Issuing abts\n");
1723 spin_unlock_irqrestore(io_lock, flags);
1725 /* Now queue the abort command to firmware */
1726 int_to_scsilun(sc->device->lun, &fc_lun);
1728 if (fnic_queue_abort_io_req(fnic, abt_tag,
1729 FCPIO_ITMF_ABT_TASK_TERM,
1730 fc_lun.scsi_lun, io_req)) {
1732 * Revert the cmd state back to old state, if
1733 * it hasn't changed in between. This cmd will get
1734 * aborted later by scsi_eh, or cleaned up during
1737 spin_lock_irqsave(io_lock, flags);
1738 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1739 CMD_STATE(sc) = old_ioreq_state;
1740 spin_unlock_irqrestore(io_lock, flags);
1742 spin_lock_irqsave(io_lock, flags);
1743 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1744 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
1746 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
1747 spin_unlock_irqrestore(io_lock, flags);
1748 atomic64_inc(&term_stats->terminates);
1752 if (term_cnt > atomic64_read(&term_stats->max_terminates))
1753 atomic64_set(&term_stats->max_terminates, term_cnt);
1758 * This function is exported to SCSI for sending abort cmnds.
1759 * A SCSI IO is represented by a io_req in the driver.
1760 * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
1762 int fnic_abort_cmd(struct scsi_cmnd *sc)
1764 struct fc_lport *lp;
1766 struct fnic_io_req *io_req = NULL;
1767 struct fc_rport *rport;
1768 spinlock_t *io_lock;
1769 unsigned long flags;
1770 unsigned long start_time = 0;
1773 struct scsi_lun fc_lun;
1774 struct fnic_stats *fnic_stats;
1775 struct abort_stats *abts_stats;
1776 struct terminate_stats *term_stats;
1777 enum fnic_ioreq_state old_ioreq_state;
1779 DECLARE_COMPLETION_ONSTACK(tm_done);
1781 /* Wait for rport to unblock */
1782 fc_block_scsi_eh(sc);
1784 /* Get local-port, check ready and link up */
1785 lp = shost_priv(sc->device->host);
1787 fnic = lport_priv(lp);
1788 fnic_stats = &fnic->fnic_stats;
1789 abts_stats = &fnic->fnic_stats.abts_stats;
1790 term_stats = &fnic->fnic_stats.term_stats;
1792 rport = starget_to_rport(scsi_target(sc->device));
1793 tag = sc->request->tag;
1794 FNIC_SCSI_DBG(KERN_DEBUG,
1796 "Abort Cmd called FCID 0x%x, LUN 0x%llx TAG %x flags %x\n",
1797 rport->port_id, sc->device->lun, tag, CMD_FLAGS(sc));
1799 CMD_FLAGS(sc) = FNIC_NO_FLAGS;
1801 if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
1803 goto fnic_abort_cmd_end;
1807 * Avoid a race between SCSI issuing the abort and the device
1808 * completing the command.
1810 * If the command is already completed by the fw cmpl code,
1811 * we just return SUCCESS from here. This means that the abort
1812 * succeeded. In the SCSI ML, since the timeout for command has
1813 * happened, the completion wont actually complete the command
1814 * and it will be considered as an aborted command
1816 * The CMD_SP will not be cleared except while holding io_req_lock.
1818 io_lock = fnic_io_lock_hash(fnic, sc);
1819 spin_lock_irqsave(io_lock, flags);
1820 io_req = (struct fnic_io_req *)CMD_SP(sc);
1822 spin_unlock_irqrestore(io_lock, flags);
1823 goto fnic_abort_cmd_end;
1826 io_req->abts_done = &tm_done;
1828 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1829 spin_unlock_irqrestore(io_lock, flags);
1833 * Command is still pending, need to abort it
1834 * If the firmware completes the command after this point,
1835 * the completion wont be done till mid-layer, since abort
1836 * has already started.
1838 old_ioreq_state = CMD_STATE(sc);
1839 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1840 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1842 spin_unlock_irqrestore(io_lock, flags);
1845 * Check readiness of the remote port. If the path to remote
1846 * port is up, then send abts to the remote port to terminate
1847 * the IO. Else, just locally terminate the IO in the firmware
1849 if (fc_remote_port_chkready(rport) == 0)
1850 task_req = FCPIO_ITMF_ABT_TASK;
1852 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
1853 task_req = FCPIO_ITMF_ABT_TASK_TERM;
1856 /* Now queue the abort command to firmware */
1857 int_to_scsilun(sc->device->lun, &fc_lun);
1859 if (fnic_queue_abort_io_req(fnic, sc->request->tag, task_req,
1860 fc_lun.scsi_lun, io_req)) {
1861 spin_lock_irqsave(io_lock, flags);
1862 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1863 CMD_STATE(sc) = old_ioreq_state;
1864 io_req = (struct fnic_io_req *)CMD_SP(sc);
1866 io_req->abts_done = NULL;
1867 spin_unlock_irqrestore(io_lock, flags);
1869 goto fnic_abort_cmd_end;
1871 if (task_req == FCPIO_ITMF_ABT_TASK) {
1872 CMD_FLAGS(sc) |= FNIC_IO_ABTS_ISSUED;
1873 atomic64_inc(&fnic_stats->abts_stats.aborts);
1875 CMD_FLAGS(sc) |= FNIC_IO_TERM_ISSUED;
1876 atomic64_inc(&fnic_stats->term_stats.terminates);
1880 * We queued an abort IO, wait for its completion.
1881 * Once the firmware completes the abort command, it will
1882 * wake up this thread.
1885 wait_for_completion_timeout(&tm_done,
1887 (2 * fnic->config.ra_tov +
1888 fnic->config.ed_tov));
1890 /* Check the abort status */
1891 spin_lock_irqsave(io_lock, flags);
1893 io_req = (struct fnic_io_req *)CMD_SP(sc);
1895 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
1896 spin_unlock_irqrestore(io_lock, flags);
1897 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
1899 goto fnic_abort_cmd_end;
1901 io_req->abts_done = NULL;
1903 /* fw did not complete abort, timed out */
1904 if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
1905 spin_unlock_irqrestore(io_lock, flags);
1906 if (task_req == FCPIO_ITMF_ABT_TASK) {
1907 atomic64_inc(&abts_stats->abort_drv_timeouts);
1909 atomic64_inc(&term_stats->terminate_drv_timeouts);
1911 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_TIMED_OUT;
1913 goto fnic_abort_cmd_end;
1916 /* IO out of order */
1918 if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE))) {
1919 spin_unlock_irqrestore(io_lock, flags);
1920 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1921 "Issuing Host reset due to out of order IO\n");
1923 if (fnic_host_reset(sc) == FAILED) {
1924 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1925 "fnic_host_reset failed.\n");
1928 goto fnic_abort_cmd_end;
1931 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1934 * firmware completed the abort, check the status,
1935 * free the io_req irrespective of failure or success
1937 if (CMD_ABTS_STATUS(sc) != FCPIO_SUCCESS)
1942 spin_unlock_irqrestore(io_lock, flags);
1944 start_time = io_req->start_time;
1945 fnic_release_ioreq_buf(fnic, io_req, sc);
1946 mempool_free(io_req, fnic->io_req_pool);
1949 FNIC_TRACE(fnic_abort_cmd, sc->device->host->host_no,
1950 sc->request->tag, sc,
1951 jiffies_to_msecs(jiffies - start_time),
1952 0, ((u64)sc->cmnd[0] << 32 |
1953 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1954 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1955 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1957 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1958 "Returning from abort cmd type %x %s\n", task_req,
1960 "SUCCESS" : "FAILED");
1964 static inline int fnic_queue_dr_io_req(struct fnic *fnic,
1965 struct scsi_cmnd *sc,
1966 struct fnic_io_req *io_req)
1968 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1969 struct Scsi_Host *host = fnic->lport->host;
1970 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
1971 struct scsi_lun fc_lun;
1973 unsigned long intr_flags;
1975 spin_lock_irqsave(host->host_lock, intr_flags);
1976 if (unlikely(fnic_chk_state_flags_locked(fnic,
1977 FNIC_FLAGS_IO_BLOCKED))) {
1978 spin_unlock_irqrestore(host->host_lock, intr_flags);
1981 atomic_inc(&fnic->in_flight);
1982 spin_unlock_irqrestore(host->host_lock, intr_flags);
1984 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
1986 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1987 free_wq_copy_descs(fnic, wq);
1989 if (!vnic_wq_copy_desc_avail(wq)) {
1990 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1991 "queue_dr_io_req failure - no descriptors\n");
1992 atomic64_inc(&misc_stats->devrst_cpwq_alloc_failures);
1997 /* fill in the lun info */
1998 int_to_scsilun(sc->device->lun, &fc_lun);
2000 fnic_queue_wq_copy_desc_itmf(wq, sc->request->tag | FNIC_TAG_DEV_RST,
2001 0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
2002 fc_lun.scsi_lun, io_req->port_id,
2003 fnic->config.ra_tov, fnic->config.ed_tov);
2005 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
2006 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
2007 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
2008 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
2009 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
2012 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
2013 atomic_dec(&fnic->in_flight);
2019 * Clean up any pending aborts on the lun
2020 * For each outstanding IO on this lun, whose abort is not completed by fw,
2021 * issue a local abort. Wait for abort to complete. Return 0 if all commands
2022 * successfully aborted, 1 otherwise
2024 static int fnic_clean_pending_aborts(struct fnic *fnic,
2025 struct scsi_cmnd *lr_sc)
2028 struct fnic_io_req *io_req;
2029 spinlock_t *io_lock;
2030 unsigned long flags;
2032 struct scsi_cmnd *sc;
2033 struct scsi_lun fc_lun;
2034 struct scsi_device *lun_dev = lr_sc->device;
2035 DECLARE_COMPLETION_ONSTACK(tm_done);
2036 enum fnic_ioreq_state old_ioreq_state;
2038 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
2039 io_lock = fnic_io_lock_tag(fnic, tag);
2040 spin_lock_irqsave(io_lock, flags);
2041 sc = scsi_host_find_tag(fnic->lport->host, tag);
2043 * ignore this lun reset cmd or cmds that do not belong to
2046 if (!sc || sc == lr_sc || sc->device != lun_dev) {
2047 spin_unlock_irqrestore(io_lock, flags);
2051 io_req = (struct fnic_io_req *)CMD_SP(sc);
2053 if (!io_req || sc->device != lun_dev) {
2054 spin_unlock_irqrestore(io_lock, flags);
2059 * Found IO that is still pending with firmware and
2060 * belongs to the LUN that we are resetting
2062 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2063 "Found IO in %s on lun\n",
2064 fnic_ioreq_state_to_str(CMD_STATE(sc)));
2066 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
2067 spin_unlock_irqrestore(io_lock, flags);
2070 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
2071 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
2072 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2073 "%s dev rst not pending sc 0x%p\n", __func__,
2075 spin_unlock_irqrestore(io_lock, flags);
2079 if (io_req->abts_done)
2080 shost_printk(KERN_ERR, fnic->lport->host,
2081 "%s: io_req->abts_done is set state is %s\n",
2082 __func__, fnic_ioreq_state_to_str(CMD_STATE(sc)));
2083 old_ioreq_state = CMD_STATE(sc);
2085 * Any pending IO issued prior to reset is expected to be
2086 * in abts pending state, if not we need to set
2087 * FNIC_IOREQ_ABTS_PENDING to indicate the IO is abort pending.
2088 * When IO is completed, the IO will be handed over and
2089 * handled in this function.
2091 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2093 BUG_ON(io_req->abts_done);
2096 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
2097 abt_tag |= FNIC_TAG_DEV_RST;
2098 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2099 "%s: dev rst sc 0x%p\n", __func__, sc);
2102 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
2103 io_req->abts_done = &tm_done;
2104 spin_unlock_irqrestore(io_lock, flags);
2106 /* Now queue the abort command to firmware */
2107 int_to_scsilun(sc->device->lun, &fc_lun);
2109 if (fnic_queue_abort_io_req(fnic, abt_tag,
2110 FCPIO_ITMF_ABT_TASK_TERM,
2111 fc_lun.scsi_lun, io_req)) {
2112 spin_lock_irqsave(io_lock, flags);
2113 io_req = (struct fnic_io_req *)CMD_SP(sc);
2115 io_req->abts_done = NULL;
2116 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
2117 CMD_STATE(sc) = old_ioreq_state;
2118 spin_unlock_irqrestore(io_lock, flags);
2120 goto clean_pending_aborts_end;
2122 spin_lock_irqsave(io_lock, flags);
2123 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
2124 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2125 spin_unlock_irqrestore(io_lock, flags);
2127 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
2129 wait_for_completion_timeout(&tm_done,
2131 (fnic->config.ed_tov));
2133 /* Recheck cmd state to check if it is now aborted */
2134 spin_lock_irqsave(io_lock, flags);
2135 io_req = (struct fnic_io_req *)CMD_SP(sc);
2137 spin_unlock_irqrestore(io_lock, flags);
2138 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
2142 io_req->abts_done = NULL;
2144 /* if abort is still pending with fw, fail */
2145 if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
2146 spin_unlock_irqrestore(io_lock, flags);
2147 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
2149 goto clean_pending_aborts_end;
2151 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
2153 spin_unlock_irqrestore(io_lock, flags);
2155 fnic_release_ioreq_buf(fnic, io_req, sc);
2156 mempool_free(io_req, fnic->io_req_pool);
2159 schedule_timeout(msecs_to_jiffies(2 * fnic->config.ed_tov));
2161 /* walk again to check, if IOs are still pending in fw */
2162 if (fnic_is_abts_pending(fnic, lr_sc))
2165 clean_pending_aborts_end:
2170 * fnic_scsi_host_start_tag
2171 * Allocates tagid from host's tag list
2174 fnic_scsi_host_start_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2176 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2177 int tag, ret = SCSI_NO_TAG;
2181 pr_err("Tags are not supported\n");
2186 tag = find_next_zero_bit(bqt->tag_map, bqt->max_depth, 1);
2187 if (tag >= bqt->max_depth) {
2188 pr_err("Tag allocation failure\n");
2191 } while (test_and_set_bit(tag, bqt->tag_map));
2193 bqt->tag_index[tag] = sc->request;
2194 sc->request->tag = tag;
2196 if (!sc->request->special)
2197 sc->request->special = sc;
2206 * fnic_scsi_host_end_tag
2207 * frees tag allocated by fnic_scsi_host_start_tag.
2210 fnic_scsi_host_end_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2212 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2213 int tag = sc->request->tag;
2215 if (tag == SCSI_NO_TAG)
2218 BUG_ON(!bqt || !bqt->tag_index[tag]);
2222 bqt->tag_index[tag] = NULL;
2223 clear_bit(tag, bqt->tag_map);
2229 * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
2230 * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
2233 int fnic_device_reset(struct scsi_cmnd *sc)
2235 struct fc_lport *lp;
2237 struct fnic_io_req *io_req = NULL;
2238 struct fc_rport *rport;
2241 spinlock_t *io_lock;
2242 unsigned long flags;
2243 unsigned long start_time = 0;
2244 struct scsi_lun fc_lun;
2245 struct fnic_stats *fnic_stats;
2246 struct reset_stats *reset_stats;
2248 DECLARE_COMPLETION_ONSTACK(tm_done);
2249 int tag_gen_flag = 0; /*to track tags allocated by fnic driver*/
2251 /* Wait for rport to unblock */
2252 fc_block_scsi_eh(sc);
2254 /* Get local-port, check ready and link up */
2255 lp = shost_priv(sc->device->host);
2257 fnic = lport_priv(lp);
2258 fnic_stats = &fnic->fnic_stats;
2259 reset_stats = &fnic->fnic_stats.reset_stats;
2261 atomic64_inc(&reset_stats->device_resets);
2263 rport = starget_to_rport(scsi_target(sc->device));
2264 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2265 "Device reset called FCID 0x%x, LUN 0x%llx sc 0x%p\n",
2266 rport->port_id, sc->device->lun, sc);
2268 if (lp->state != LPORT_ST_READY || !(lp->link_up))
2269 goto fnic_device_reset_end;
2271 /* Check if remote port up */
2272 if (fc_remote_port_chkready(rport)) {
2273 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
2274 goto fnic_device_reset_end;
2277 CMD_FLAGS(sc) = FNIC_DEVICE_RESET;
2278 /* Allocate tag if not present */
2280 tag = sc->request->tag;
2281 if (unlikely(tag < 0)) {
2283 * XXX(hch): current the midlayer fakes up a struct
2284 * request for the explicit reset ioctls, and those
2285 * don't have a tag allocated to them. The below
2286 * code pokes into midlayer structures to paper over
2287 * this design issue, but that won't work for blk-mq.
2289 * Either someone who can actually test the hardware
2290 * will have to come up with a similar hack for the
2291 * blk-mq case, or we'll have to bite the bullet and
2292 * fix the way the EH ioctls work for real, but until
2293 * that happens we fail these explicit requests here.
2295 if (shost_use_blk_mq(sc->device->host))
2296 goto fnic_device_reset_end;
2298 tag = fnic_scsi_host_start_tag(fnic, sc);
2299 if (unlikely(tag == SCSI_NO_TAG))
2300 goto fnic_device_reset_end;
2303 io_lock = fnic_io_lock_hash(fnic, sc);
2304 spin_lock_irqsave(io_lock, flags);
2305 io_req = (struct fnic_io_req *)CMD_SP(sc);
2308 * If there is a io_req attached to this command, then use it,
2309 * else allocate a new one.
2312 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
2314 spin_unlock_irqrestore(io_lock, flags);
2315 goto fnic_device_reset_end;
2317 memset(io_req, 0, sizeof(*io_req));
2318 io_req->port_id = rport->port_id;
2319 CMD_SP(sc) = (char *)io_req;
2321 io_req->dr_done = &tm_done;
2322 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
2323 CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
2324 spin_unlock_irqrestore(io_lock, flags);
2326 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %x\n", tag);
2329 * issue the device reset, if enqueue failed, clean up the ioreq
2330 * and break assoc with scsi cmd
2332 if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
2333 spin_lock_irqsave(io_lock, flags);
2334 io_req = (struct fnic_io_req *)CMD_SP(sc);
2336 io_req->dr_done = NULL;
2337 goto fnic_device_reset_clean;
2339 spin_lock_irqsave(io_lock, flags);
2340 CMD_FLAGS(sc) |= FNIC_DEV_RST_ISSUED;
2341 spin_unlock_irqrestore(io_lock, flags);
2344 * Wait on the local completion for LUN reset. The io_req may be
2345 * freed while we wait since we hold no lock.
2347 wait_for_completion_timeout(&tm_done,
2348 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2350 spin_lock_irqsave(io_lock, flags);
2351 io_req = (struct fnic_io_req *)CMD_SP(sc);
2353 spin_unlock_irqrestore(io_lock, flags);
2354 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2355 "io_req is null tag 0x%x sc 0x%p\n", tag, sc);
2356 goto fnic_device_reset_end;
2358 io_req->dr_done = NULL;
2360 status = CMD_LR_STATUS(sc);
2363 * If lun reset not completed, bail out with failed. io_req
2364 * gets cleaned up during higher levels of EH
2366 if (status == FCPIO_INVALID_CODE) {
2367 atomic64_inc(&reset_stats->device_reset_timeouts);
2368 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2369 "Device reset timed out\n");
2370 CMD_FLAGS(sc) |= FNIC_DEV_RST_TIMED_OUT;
2371 spin_unlock_irqrestore(io_lock, flags);
2372 int_to_scsilun(sc->device->lun, &fc_lun);
2374 * Issue abort and terminate on device reset request.
2375 * If q'ing of terminate fails, retry it after a delay.
2378 spin_lock_irqsave(io_lock, flags);
2379 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TERM_ISSUED) {
2380 spin_unlock_irqrestore(io_lock, flags);
2383 spin_unlock_irqrestore(io_lock, flags);
2384 if (fnic_queue_abort_io_req(fnic,
2385 tag | FNIC_TAG_DEV_RST,
2386 FCPIO_ITMF_ABT_TASK_TERM,
2387 fc_lun.scsi_lun, io_req)) {
2388 wait_for_completion_timeout(&tm_done,
2389 msecs_to_jiffies(FNIC_ABT_TERM_DELAY_TIMEOUT));
2391 spin_lock_irqsave(io_lock, flags);
2392 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2393 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2394 io_req->abts_done = &tm_done;
2395 spin_unlock_irqrestore(io_lock, flags);
2396 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2397 "Abort and terminate issued on Device reset "
2398 "tag 0x%x sc 0x%p\n", tag, sc);
2403 spin_lock_irqsave(io_lock, flags);
2404 if (!(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
2405 spin_unlock_irqrestore(io_lock, flags);
2406 wait_for_completion_timeout(&tm_done,
2407 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2410 io_req = (struct fnic_io_req *)CMD_SP(sc);
2411 io_req->abts_done = NULL;
2412 goto fnic_device_reset_clean;
2416 spin_unlock_irqrestore(io_lock, flags);
2419 /* Completed, but not successful, clean up the io_req, return fail */
2420 if (status != FCPIO_SUCCESS) {
2421 spin_lock_irqsave(io_lock, flags);
2422 FNIC_SCSI_DBG(KERN_DEBUG,
2424 "Device reset completed - failed\n");
2425 io_req = (struct fnic_io_req *)CMD_SP(sc);
2426 goto fnic_device_reset_clean;
2430 * Clean up any aborts on this lun that have still not
2431 * completed. If any of these fail, then LUN reset fails.
2432 * clean_pending_aborts cleans all cmds on this lun except
2433 * the lun reset cmd. If all cmds get cleaned, the lun reset
2436 if (fnic_clean_pending_aborts(fnic, sc)) {
2437 spin_lock_irqsave(io_lock, flags);
2438 io_req = (struct fnic_io_req *)CMD_SP(sc);
2439 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2440 "Device reset failed"
2441 " since could not abort all IOs\n");
2442 goto fnic_device_reset_clean;
2445 /* Clean lun reset command */
2446 spin_lock_irqsave(io_lock, flags);
2447 io_req = (struct fnic_io_req *)CMD_SP(sc);
2449 /* Completed, and successful */
2452 fnic_device_reset_clean:
2456 spin_unlock_irqrestore(io_lock, flags);
2459 start_time = io_req->start_time;
2460 fnic_release_ioreq_buf(fnic, io_req, sc);
2461 mempool_free(io_req, fnic->io_req_pool);
2464 fnic_device_reset_end:
2465 FNIC_TRACE(fnic_device_reset, sc->device->host->host_no,
2466 sc->request->tag, sc,
2467 jiffies_to_msecs(jiffies - start_time),
2468 0, ((u64)sc->cmnd[0] << 32 |
2469 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
2470 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
2471 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
2473 /* free tag if it is allocated */
2474 if (unlikely(tag_gen_flag))
2475 fnic_scsi_host_end_tag(fnic, sc);
2477 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2478 "Returning from device reset %s\n",
2480 "SUCCESS" : "FAILED");
2483 atomic64_inc(&reset_stats->device_reset_failures);
2488 /* Clean up all IOs, clean up libFC local port */
2489 int fnic_reset(struct Scsi_Host *shost)
2491 struct fc_lport *lp;
2494 struct reset_stats *reset_stats;
2496 lp = shost_priv(shost);
2497 fnic = lport_priv(lp);
2498 reset_stats = &fnic->fnic_stats.reset_stats;
2500 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2501 "fnic_reset called\n");
2503 atomic64_inc(&reset_stats->fnic_resets);
2506 * Reset local port, this will clean up libFC exchanges,
2507 * reset remote port sessions, and if link is up, begin flogi
2509 ret = lp->tt.lport_reset(lp);
2511 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2512 "Returning from fnic reset %s\n",
2514 "SUCCESS" : "FAILED");
2517 atomic64_inc(&reset_stats->fnic_reset_completions);
2519 atomic64_inc(&reset_stats->fnic_reset_failures);
2525 * SCSI Error handling calls driver's eh_host_reset if all prior
2526 * error handling levels return FAILED. If host reset completes
2527 * successfully, and if link is up, then Fabric login begins.
2529 * Host Reset is the highest level of error recovery. If this fails, then
2530 * host is offlined by SCSI.
2533 int fnic_host_reset(struct scsi_cmnd *sc)
2536 unsigned long wait_host_tmo;
2537 struct Scsi_Host *shost = sc->device->host;
2538 struct fc_lport *lp = shost_priv(shost);
2539 struct fnic *fnic = lport_priv(lp);
2540 unsigned long flags;
2542 spin_lock_irqsave(&fnic->fnic_lock, flags);
2543 if (fnic->internal_reset_inprogress == 0) {
2544 fnic->internal_reset_inprogress = 1;
2546 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2547 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2548 "host reset in progress skipping another host reset\n");
2551 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2554 * If fnic_reset is successful, wait for fabric login to complete
2555 * scsi-ml tries to send a TUR to every device if host reset is
2556 * successful, so before returning to scsi, fabric should be up
2558 ret = (fnic_reset(shost) == 0) ? SUCCESS : FAILED;
2559 if (ret == SUCCESS) {
2560 wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
2562 while (time_before(jiffies, wait_host_tmo)) {
2563 if ((lp->state == LPORT_ST_READY) &&
2572 spin_lock_irqsave(&fnic->fnic_lock, flags);
2573 fnic->internal_reset_inprogress = 0;
2574 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2579 * This fxn is called from libFC when host is removed
2581 void fnic_scsi_abort_io(struct fc_lport *lp)
2584 unsigned long flags;
2585 enum fnic_state old_state;
2586 struct fnic *fnic = lport_priv(lp);
2587 DECLARE_COMPLETION_ONSTACK(remove_wait);
2589 /* Issue firmware reset for fnic, wait for reset to complete */
2591 spin_lock_irqsave(&fnic->fnic_lock, flags);
2592 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2593 /* fw reset is in progress, poll for its completion */
2594 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2595 schedule_timeout(msecs_to_jiffies(100));
2596 goto retry_fw_reset;
2599 fnic->remove_wait = &remove_wait;
2600 old_state = fnic->state;
2601 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
2602 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
2603 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2605 err = fnic_fw_reset_handler(fnic);
2607 spin_lock_irqsave(&fnic->fnic_lock, flags);
2608 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2609 fnic->state = old_state;
2610 fnic->remove_wait = NULL;
2611 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2615 /* Wait for firmware reset to complete */
2616 wait_for_completion_timeout(&remove_wait,
2617 msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
2619 spin_lock_irqsave(&fnic->fnic_lock, flags);
2620 fnic->remove_wait = NULL;
2621 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2622 "fnic_scsi_abort_io %s\n",
2623 (fnic->state == FNIC_IN_ETH_MODE) ?
2624 "SUCCESS" : "FAILED");
2625 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2630 * This fxn called from libFC to clean up driver IO state on link down
2632 void fnic_scsi_cleanup(struct fc_lport *lp)
2634 unsigned long flags;
2635 enum fnic_state old_state;
2636 struct fnic *fnic = lport_priv(lp);
2638 /* issue fw reset */
2640 spin_lock_irqsave(&fnic->fnic_lock, flags);
2641 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2642 /* fw reset is in progress, poll for its completion */
2643 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2644 schedule_timeout(msecs_to_jiffies(100));
2645 goto retry_fw_reset;
2647 old_state = fnic->state;
2648 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
2649 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
2650 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2652 if (fnic_fw_reset_handler(fnic)) {
2653 spin_lock_irqsave(&fnic->fnic_lock, flags);
2654 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2655 fnic->state = old_state;
2656 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2661 void fnic_empty_scsi_cleanup(struct fc_lport *lp)
2665 void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
2667 struct fnic *fnic = lport_priv(lp);
2669 /* Non-zero sid, nothing to do */
2671 goto call_fc_exch_mgr_reset;
2674 fnic_rport_exch_reset(fnic, did);
2675 goto call_fc_exch_mgr_reset;
2680 * link down or device being removed
2682 if (!fnic->in_remove)
2683 fnic_scsi_cleanup(lp);
2685 fnic_scsi_abort_io(lp);
2687 /* call libFC exch mgr reset to reset its exchanges */
2688 call_fc_exch_mgr_reset:
2689 fc_exch_mgr_reset(lp, sid, did);
2694 * fnic_is_abts_pending() is a helper function that
2695 * walks through tag map to check if there is any IOs pending,if there is one,
2696 * then it returns 1 (true), otherwise 0 (false)
2697 * if @lr_sc is non NULL, then it checks IOs specific to particular LUN,
2698 * otherwise, it checks for all IOs.
2700 int fnic_is_abts_pending(struct fnic *fnic, struct scsi_cmnd *lr_sc)
2703 struct fnic_io_req *io_req;
2704 spinlock_t *io_lock;
2705 unsigned long flags;
2707 struct scsi_cmnd *sc;
2708 struct scsi_device *lun_dev = NULL;
2711 lun_dev = lr_sc->device;
2713 /* walk again to check, if IOs are still pending in fw */
2714 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
2715 sc = scsi_host_find_tag(fnic->lport->host, tag);
2717 * ignore this lun reset cmd or cmds that do not belong to
2720 if (!sc || (lr_sc && (sc->device != lun_dev || sc == lr_sc)))
2723 io_lock = fnic_io_lock_hash(fnic, sc);
2724 spin_lock_irqsave(io_lock, flags);
2726 io_req = (struct fnic_io_req *)CMD_SP(sc);
2728 if (!io_req || sc->device != lun_dev) {
2729 spin_unlock_irqrestore(io_lock, flags);
2734 * Found IO that is still pending with firmware and
2735 * belongs to the LUN that we are resetting
2737 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2738 "Found IO in %s on lun\n",
2739 fnic_ioreq_state_to_str(CMD_STATE(sc)));
2741 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
2743 spin_unlock_irqrestore(io_lock, flags);