2 * Copyright 2014 Cisco Systems, Inc. All rights reserved.
4 * This program is free software; you may redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
9 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
10 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
11 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
12 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
13 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
14 * 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/spinlock.h>
24 #include <linux/delay.h>
25 #include <linux/gfp.h>
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_host.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_tcq.h>
31 #include <scsi/scsi_dbg.h>
36 #define snic_cmd_tag(sc) (((struct scsi_cmnd *) sc)->request->tag)
38 const char *snic_state_str[] = {
39 [SNIC_INIT] = "SNIC_INIT",
40 [SNIC_ERROR] = "SNIC_ERROR",
41 [SNIC_ONLINE] = "SNIC_ONLINE",
42 [SNIC_OFFLINE] = "SNIC_OFFLINE",
43 [SNIC_FWRESET] = "SNIC_FWRESET",
46 static const char * const snic_req_state_str[] = {
47 [SNIC_IOREQ_NOT_INITED] = "SNIC_IOREQ_NOT_INITED",
48 [SNIC_IOREQ_PENDING] = "SNIC_IOREQ_PENDING",
49 [SNIC_IOREQ_ABTS_PENDING] = "SNIC_IOREQ_ABTS_PENDING",
50 [SNIC_IOREQ_ABTS_COMPLETE] = "SNIC_IOREQ_ABTS_COMPLETE",
51 [SNIC_IOREQ_LR_PENDING] = "SNIC_IOREQ_LR_PENDING",
52 [SNIC_IOREQ_LR_COMPLETE] = "SNIC_IOREQ_LR_COMPLETE",
53 [SNIC_IOREQ_COMPLETE] = "SNIC_IOREQ_CMD_COMPLETE",
56 /* snic cmd status strings */
57 static const char * const snic_io_status_str[] = {
58 [SNIC_STAT_IO_SUCCESS] = "SNIC_STAT_IO_SUCCESS", /* 0x0 */
59 [SNIC_STAT_INVALID_HDR] = "SNIC_STAT_INVALID_HDR",
60 [SNIC_STAT_OUT_OF_RES] = "SNIC_STAT_OUT_OF_RES",
61 [SNIC_STAT_INVALID_PARM] = "SNIC_STAT_INVALID_PARM",
62 [SNIC_STAT_REQ_NOT_SUP] = "SNIC_STAT_REQ_NOT_SUP",
63 [SNIC_STAT_IO_NOT_FOUND] = "SNIC_STAT_IO_NOT_FOUND",
64 [SNIC_STAT_ABORTED] = "SNIC_STAT_ABORTED",
65 [SNIC_STAT_TIMEOUT] = "SNIC_STAT_TIMEOUT",
66 [SNIC_STAT_SGL_INVALID] = "SNIC_STAT_SGL_INVALID",
67 [SNIC_STAT_DATA_CNT_MISMATCH] = "SNIC_STAT_DATA_CNT_MISMATCH",
68 [SNIC_STAT_FW_ERR] = "SNIC_STAT_FW_ERR",
69 [SNIC_STAT_ITMF_REJECT] = "SNIC_STAT_ITMF_REJECT",
70 [SNIC_STAT_ITMF_FAIL] = "SNIC_STAT_ITMF_FAIL",
71 [SNIC_STAT_ITMF_INCORRECT_LUN] = "SNIC_STAT_ITMF_INCORRECT_LUN",
72 [SNIC_STAT_CMND_REJECT] = "SNIC_STAT_CMND_REJECT",
73 [SNIC_STAT_DEV_OFFLINE] = "SNIC_STAT_DEV_OFFLINE",
74 [SNIC_STAT_NO_BOOTLUN] = "SNIC_STAT_NO_BOOTLUN",
75 [SNIC_STAT_SCSI_ERR] = "SNIC_STAT_SCSI_ERR",
76 [SNIC_STAT_NOT_READY] = "SNIC_STAT_NOT_READY",
77 [SNIC_STAT_FATAL_ERROR] = "SNIC_STAT_FATAL_ERROR",
80 static void snic_scsi_cleanup(struct snic *, int);
83 snic_state_to_str(unsigned int state)
85 if (state >= ARRAY_SIZE(snic_state_str) || !snic_state_str[state])
88 return snic_state_str[state];
92 snic_io_status_to_str(unsigned int state)
94 if ((state >= ARRAY_SIZE(snic_io_status_str)) ||
95 (!snic_io_status_str[state]))
98 return snic_io_status_str[state];
102 snic_ioreq_state_to_str(unsigned int state)
104 if (state >= ARRAY_SIZE(snic_req_state_str) ||
105 !snic_req_state_str[state])
108 return snic_req_state_str[state];
111 static inline spinlock_t *
112 snic_io_lock_hash(struct snic *snic, struct scsi_cmnd *sc)
114 u32 hash = snic_cmd_tag(sc) & (SNIC_IO_LOCKS - 1);
116 return &snic->io_req_lock[hash];
119 static inline spinlock_t *
120 snic_io_lock_tag(struct snic *snic, int tag)
122 return &snic->io_req_lock[tag & (SNIC_IO_LOCKS - 1)];
125 /* snic_release_req_buf : Releases snic_req_info */
127 snic_release_req_buf(struct snic *snic,
128 struct snic_req_info *rqi,
129 struct scsi_cmnd *sc)
131 struct snic_host_req *req = rqi_to_req(rqi);
133 /* Freeing cmd without marking completion, not okay */
134 SNIC_BUG_ON(!((CMD_STATE(sc) == SNIC_IOREQ_COMPLETE) ||
135 (CMD_STATE(sc) == SNIC_IOREQ_ABTS_COMPLETE) ||
136 (CMD_FLAGS(sc) & SNIC_DEV_RST_NOTSUP) ||
137 (CMD_FLAGS(sc) & SNIC_IO_INTERNAL_TERM_ISSUED) ||
138 (CMD_FLAGS(sc) & SNIC_DEV_RST_TERM_ISSUED) ||
139 (CMD_FLAGS(sc) & SNIC_SCSI_CLEANUP) ||
140 (CMD_STATE(sc) == SNIC_IOREQ_LR_COMPLETE)));
142 SNIC_SCSI_DBG(snic->shost,
143 "Rel_req:sc %p:tag %x:rqi %p:ioreq %p:abt %p:dr %p: state %s:flags 0x%llx\n",
144 sc, snic_cmd_tag(sc), rqi, rqi->req, rqi->abort_req,
145 rqi->dr_req, snic_ioreq_state_to_str(CMD_STATE(sc)),
148 if (req->u.icmnd.sense_addr)
149 dma_unmap_single(&snic->pdev->dev,
150 le64_to_cpu(req->u.icmnd.sense_addr),
151 SCSI_SENSE_BUFFERSIZE,
156 snic_req_free(snic, rqi);
157 } /* end of snic_release_req_buf */
160 * snic_queue_icmnd_req : Queues snic_icmnd request
163 snic_queue_icmnd_req(struct snic *snic,
164 struct snic_req_info *rqi,
165 struct scsi_cmnd *sc,
168 struct scatterlist *sg;
169 struct snic_sg_desc *sgd;
177 flags = SNIC_ICMND_ESGL;
178 sgd = (struct snic_sg_desc *) req_to_sgl(rqi->req);
180 for_each_sg(scsi_sglist(sc), sg, sg_cnt, i) {
181 sgd->addr = cpu_to_le64(sg_dma_address(sg));
182 sgd->len = cpu_to_le32(sg_dma_len(sg));
188 pa = dma_map_single(&snic->pdev->dev,
190 SCSI_SENSE_BUFFERSIZE,
192 if (dma_mapping_error(&snic->pdev->dev, pa)) {
193 SNIC_HOST_ERR(snic->shost,
194 "QIcmnd:PCI Map Failed for sns buf %p tag %x\n",
195 sc->sense_buffer, snic_cmd_tag(sc));
201 int_to_scsilun(sc->device->lun, &lun);
202 if (sc->sc_data_direction == DMA_FROM_DEVICE)
203 flags |= SNIC_ICMND_RD;
204 if (sc->sc_data_direction == DMA_TO_DEVICE)
205 flags |= SNIC_ICMND_WR;
207 /* Initialize icmnd */
208 snic_icmnd_init(rqi->req,
210 snic->config.hid, /* hid */
212 flags, /* command flags */
219 (ulong) req_to_sgl(rqi->req),
220 pa, /* sense buffer pa */
221 SCSI_SENSE_BUFFERSIZE);
223 atomic64_inc(&snic->s_stats.io.active);
224 ret = snic_queue_wq_desc(snic, rqi->req, rqi->req_len);
226 atomic64_dec(&snic->s_stats.io.active);
227 SNIC_HOST_ERR(snic->shost,
228 "QIcmnd: Queuing Icmnd Failed. ret = %d\n",
231 snic_stats_update_active_ios(&snic->s_stats);
234 } /* end of snic_queue_icmnd_req */
237 * snic_issue_scsi_req : Prepares IO request and Issues to FW.
240 snic_issue_scsi_req(struct snic *snic,
241 struct snic_tgt *tgt,
242 struct scsi_cmnd *sc)
244 struct snic_req_info *rqi = NULL;
247 u32 tag = snic_cmd_tag(sc);
248 u64 cmd_trc = 0, cmd_st_flags = 0;
249 spinlock_t *io_lock = NULL;
252 CMD_STATE(sc) = SNIC_IOREQ_NOT_INITED;
253 CMD_FLAGS(sc) = SNIC_NO_FLAGS;
254 sg_cnt = scsi_dma_map(sc);
256 SNIC_TRC((u16)snic->shost->host_no, tag, (ulong) sc, 0,
257 sc->cmnd[0], sg_cnt, CMD_STATE(sc));
259 SNIC_HOST_ERR(snic->shost, "issue_sc:Failed to map SG List.\n");
265 rqi = snic_req_init(snic, sg_cnt);
273 rqi->tgt_id = tgt->id;
276 CMD_STATE(sc) = SNIC_IOREQ_PENDING;
277 CMD_SP(sc) = (char *) rqi;
278 cmd_trc = SNIC_TRC_CMD(sc);
279 CMD_FLAGS(sc) |= (SNIC_IO_INITIALIZED | SNIC_IO_ISSUED);
280 cmd_st_flags = SNIC_TRC_CMD_STATE_FLAGS(sc);
281 io_lock = snic_io_lock_hash(snic, sc);
283 /* create wq desc and enqueue it */
284 ret = snic_queue_icmnd_req(snic, rqi, sc, sg_cnt);
286 SNIC_HOST_ERR(snic->shost,
287 "issue_sc: icmnd qing Failed for sc %p, err %d\n",
290 spin_lock_irqsave(io_lock, flags);
291 rqi = (struct snic_req_info *) CMD_SP(sc);
293 CMD_STATE(sc) = SNIC_IOREQ_COMPLETE;
294 CMD_FLAGS(sc) &= ~SNIC_IO_ISSUED; /* turn off the flag */
295 spin_unlock_irqrestore(io_lock, flags);
298 snic_release_req_buf(snic, rqi, sc);
300 SNIC_TRC(snic->shost->host_no, tag, (ulong) sc, 0, 0, 0,
301 SNIC_TRC_CMD_STATE_FLAGS(sc));
303 u32 io_sz = scsi_bufflen(sc) >> 9;
304 u32 qtime = jiffies - rqi->start_time;
305 struct snic_io_stats *iostats = &snic->s_stats.io;
307 if (io_sz > atomic64_read(&iostats->max_io_sz))
308 atomic64_set(&iostats->max_io_sz, io_sz);
310 if (qtime > atomic64_read(&iostats->max_qtime))
311 atomic64_set(&iostats->max_qtime, qtime);
313 SNIC_SCSI_DBG(snic->shost,
314 "issue_sc:sc %p, tag %d queued to WQ.\n",
317 SNIC_TRC(snic->shost->host_no, tag, (ulong) sc, (ulong) rqi,
318 sg_cnt, cmd_trc, cmd_st_flags);
324 } /* end of snic_issue_scsi_req */
329 * Routine to send a scsi cdb to LLD
330 * Called with host_lock held and interrupts disabled
333 snic_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *sc)
335 struct snic_tgt *tgt = NULL;
336 struct snic *snic = shost_priv(shost);
339 tgt = starget_to_tgt(scsi_target(sc->device));
340 ret = snic_tgt_chkready(tgt);
342 SNIC_HOST_ERR(shost, "Tgt %p id %d Not Ready.\n", tgt, tgt->id);
343 atomic64_inc(&snic->s_stats.misc.tgt_not_rdy);
350 if (snic_get_state(snic) != SNIC_ONLINE) {
351 SNIC_HOST_ERR(shost, "snic state is %s\n",
352 snic_state_str[snic_get_state(snic)]);
354 return SCSI_MLQUEUE_HOST_BUSY;
356 atomic_inc(&snic->ios_inflight);
358 SNIC_SCSI_DBG(shost, "sc %p Tag %d (sc %0x) lun %lld in snic_qcmd\n",
359 sc, snic_cmd_tag(sc), sc->cmnd[0], sc->device->lun);
361 ret = snic_issue_scsi_req(snic, tgt, sc);
363 SNIC_HOST_ERR(shost, "Failed to Q, Scsi Req w/ err %d.\n", ret);
364 ret = SCSI_MLQUEUE_HOST_BUSY;
367 atomic_dec(&snic->ios_inflight);
370 } /* end of snic_queuecommand */
373 * snic_process_abts_pending_state:
374 * caller should hold IO lock
377 snic_proc_tmreq_pending_state(struct snic *snic,
378 struct scsi_cmnd *sc,
381 int state = CMD_STATE(sc);
383 if (state == SNIC_IOREQ_ABTS_PENDING)
384 CMD_FLAGS(sc) |= SNIC_IO_ABTS_PENDING;
385 else if (state == SNIC_IOREQ_LR_PENDING)
386 CMD_FLAGS(sc) |= SNIC_DEV_RST_PENDING;
390 switch (cmpl_status) {
391 case SNIC_STAT_IO_SUCCESS:
392 CMD_FLAGS(sc) |= SNIC_IO_DONE;
395 case SNIC_STAT_ABORTED:
396 CMD_FLAGS(sc) |= SNIC_IO_ABORTED;
405 * snic_process_io_failed_state:
406 * Processes IO's error states
409 snic_process_io_failed_state(struct snic *snic,
410 struct snic_icmnd_cmpl *icmnd_cmpl,
411 struct scsi_cmnd *sc,
417 case SNIC_STAT_TIMEOUT: /* Req was timedout */
418 atomic64_inc(&snic->s_stats.misc.io_tmo);
422 case SNIC_STAT_ABORTED: /* Req was aborted */
423 atomic64_inc(&snic->s_stats.misc.io_aborted);
427 case SNIC_STAT_DATA_CNT_MISMATCH:/* Recv/Sent more/less data than exp */
428 atomic64_inc(&snic->s_stats.misc.data_cnt_mismat);
429 scsi_set_resid(sc, le32_to_cpu(icmnd_cmpl->resid));
433 case SNIC_STAT_OUT_OF_RES: /* Out of resources to complete request */
434 atomic64_inc(&snic->s_stats.fw.out_of_res);
438 case SNIC_STAT_IO_NOT_FOUND: /* Requested I/O was not found */
439 atomic64_inc(&snic->s_stats.io.io_not_found);
443 case SNIC_STAT_SGL_INVALID: /* Req was aborted to due to sgl error*/
444 atomic64_inc(&snic->s_stats.misc.sgl_inval);
448 case SNIC_STAT_FW_ERR: /* Req terminated due to FW Error */
449 atomic64_inc(&snic->s_stats.fw.io_errs);
453 case SNIC_STAT_SCSI_ERR: /* FW hits SCSI Error */
454 atomic64_inc(&snic->s_stats.fw.scsi_errs);
457 case SNIC_STAT_NOT_READY: /* XPT yet to initialize */
458 case SNIC_STAT_DEV_OFFLINE: /* Device offline */
459 res = DID_NO_CONNECT;
462 case SNIC_STAT_INVALID_HDR: /* Hdr contains invalid data */
463 case SNIC_STAT_INVALID_PARM: /* Some param in req is invalid */
464 case SNIC_STAT_REQ_NOT_SUP: /* Req type is not supported */
465 case SNIC_STAT_CMND_REJECT: /* Req rejected */
466 case SNIC_STAT_FATAL_ERROR: /* XPT Error */
468 SNIC_SCSI_DBG(snic->shost,
469 "Invalid Hdr/Param or Req Not Supported or Cmnd Rejected or Device Offline. or Unknown\n");
474 SNIC_HOST_ERR(snic->shost, "fw returns failed status %s flags 0x%llx\n",
475 snic_io_status_to_str(cmpl_stat), CMD_FLAGS(sc));
478 sc->result = (res << 16) | icmnd_cmpl->scsi_status;
479 } /* end of snic_process_io_failed_state */
482 * snic_tmreq_pending : is task management in progress.
485 snic_tmreq_pending(struct scsi_cmnd *sc)
487 int state = CMD_STATE(sc);
489 return ((state == SNIC_IOREQ_ABTS_PENDING) ||
490 (state == SNIC_IOREQ_LR_PENDING));
494 * snic_process_icmnd_cmpl_status:
495 * Caller should hold io_lock
498 snic_process_icmnd_cmpl_status(struct snic *snic,
499 struct snic_icmnd_cmpl *icmnd_cmpl,
501 struct scsi_cmnd *sc)
503 u8 scsi_stat = icmnd_cmpl->scsi_status;
507 /* Mark the IO as complete */
508 CMD_STATE(sc) = SNIC_IOREQ_COMPLETE;
510 if (likely(cmpl_stat == SNIC_STAT_IO_SUCCESS)) {
511 sc->result = (DID_OK << 16) | scsi_stat;
513 xfer_len = scsi_bufflen(sc);
515 /* Update SCSI Cmd with resid value */
516 scsi_set_resid(sc, le32_to_cpu(icmnd_cmpl->resid));
518 if (icmnd_cmpl->flags & SNIC_ICMND_CMPL_UNDR_RUN) {
519 xfer_len -= le32_to_cpu(icmnd_cmpl->resid);
520 atomic64_inc(&snic->s_stats.misc.io_under_run);
523 if (icmnd_cmpl->scsi_status == SAM_STAT_TASK_SET_FULL)
524 atomic64_inc(&snic->s_stats.misc.qfull);
528 snic_process_io_failed_state(snic, icmnd_cmpl, sc, cmpl_stat);
529 atomic64_inc(&snic->s_stats.io.fail);
530 SNIC_HOST_ERR(snic->shost,
531 "icmnd_cmpl: IO Failed : Hdr Status %s flags 0x%llx\n",
532 snic_io_status_to_str(cmpl_stat), CMD_FLAGS(sc));
537 } /* end of snic_process_icmnd_cmpl_status */
541 * snic_icmnd_cmpl_handler
542 * Routine to handle icmnd completions
545 snic_icmnd_cmpl_handler(struct snic *snic, struct snic_fw_req *fwreq)
550 struct scsi_cmnd *sc = NULL;
551 struct snic_icmnd_cmpl *icmnd_cmpl = NULL;
552 struct snic_host_req *req = NULL;
553 struct snic_req_info *rqi = NULL;
554 unsigned long flags, start_time;
558 snic_io_hdr_dec(&fwreq->hdr, &typ, &hdr_stat, &cmnd_id, &hid, &ctx);
559 icmnd_cmpl = &fwreq->u.icmnd_cmpl;
560 sc_stat = icmnd_cmpl->scsi_status;
562 SNIC_SCSI_DBG(snic->shost,
563 "Icmnd_cmpl: type = %x, hdr_stat = %x, cmnd_id = %x, hid = %x,i ctx = %lx\n",
564 typ, hdr_stat, cmnd_id, hid, ctx);
566 if (cmnd_id >= snic->max_tag_id) {
567 SNIC_HOST_ERR(snic->shost,
568 "Icmnd_cmpl:Tag Error:Out of Range Tag %d, hdr status = %s\n",
569 cmnd_id, snic_io_status_to_str(hdr_stat));
573 sc = scsi_host_find_tag(snic->shost, cmnd_id);
577 atomic64_inc(&snic->s_stats.io.sc_null);
578 SNIC_HOST_ERR(snic->shost,
579 "Icmnd_cmpl: Scsi Cmnd Not found, sc = NULL Hdr Status = %s tag = 0x%x fwreq = 0x%p\n",
580 snic_io_status_to_str(hdr_stat),
584 SNIC_TRC(snic->shost->host_no, cmnd_id, 0,
585 ((u64)hdr_stat << 16 |
586 (u64)sc_stat << 8 | (u64)icmnd_cmpl->flags),
587 (ulong) fwreq, le32_to_cpu(icmnd_cmpl->resid), ctx);
592 io_lock = snic_io_lock_hash(snic, sc);
594 spin_lock_irqsave(io_lock, flags);
595 rqi = (struct snic_req_info *) CMD_SP(sc);
596 SNIC_SCSI_DBG(snic->shost,
597 "Icmnd_cmpl:lun %lld sc %p cmd %xtag %d flags 0x%llx rqi %p\n",
598 sc->device->lun, sc, sc->cmnd[0], snic_cmd_tag(sc),
601 if (CMD_FLAGS(sc) & SNIC_HOST_RESET_CMD_TERM) {
602 spin_unlock_irqrestore(io_lock, flags);
607 SNIC_BUG_ON(rqi != (struct snic_req_info *)ctx);
610 atomic64_inc(&snic->s_stats.io.req_null);
611 CMD_FLAGS(sc) |= SNIC_IO_REQ_NULL;
612 spin_unlock_irqrestore(io_lock, flags);
614 SNIC_HOST_ERR(snic->shost,
615 "Icmnd_cmpl:Host Req Not Found(null), Hdr Status %s, Tag 0x%x, sc 0x%p flags 0x%llx\n",
616 snic_io_status_to_str(hdr_stat),
617 cmnd_id, sc, CMD_FLAGS(sc));
621 rqi = (struct snic_req_info *) ctx;
622 start_time = rqi->start_time;
624 /* firmware completed the io */
628 * if SCSI-ML has already issued abort on this command,
629 * ignore completion of the IO. The abts path will clean it up
631 if (unlikely(snic_tmreq_pending(sc))) {
632 snic_proc_tmreq_pending_state(snic, sc, hdr_stat);
633 spin_unlock_irqrestore(io_lock, flags);
635 snic_stats_update_io_cmpl(&snic->s_stats);
637 /* Expected value is SNIC_STAT_ABORTED */
638 if (likely(hdr_stat == SNIC_STAT_ABORTED))
641 SNIC_SCSI_DBG(snic->shost,
642 "icmnd_cmpl:TM Req Pending(%s), Hdr Status %s sc 0x%p scsi status %x resid %d flags 0x%llx\n",
643 snic_ioreq_state_to_str(CMD_STATE(sc)),
644 snic_io_status_to_str(hdr_stat),
645 sc, sc_stat, le32_to_cpu(icmnd_cmpl->resid),
648 SNIC_TRC(snic->shost->host_no, cmnd_id, (ulong) sc,
649 jiffies_to_msecs(jiffies - start_time), (ulong) fwreq,
650 SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
655 if (snic_process_icmnd_cmpl_status(snic, icmnd_cmpl, hdr_stat, sc)) {
656 scsi_print_command(sc);
657 SNIC_HOST_ERR(snic->shost,
658 "icmnd_cmpl:IO Failed, sc 0x%p Tag %d Cmd %x Hdr Status %s flags 0x%llx\n",
659 sc, sc->cmnd[0], cmnd_id,
660 snic_io_status_to_str(hdr_stat), CMD_FLAGS(sc));
663 /* Break link with the SCSI Command */
665 CMD_FLAGS(sc) |= SNIC_IO_DONE;
667 spin_unlock_irqrestore(io_lock, flags);
669 /* For now, consider only successful IO. */
670 snic_calc_io_process_time(snic, rqi);
672 snic_release_req_buf(snic, rqi, sc);
674 SNIC_TRC(snic->shost->host_no, cmnd_id, (ulong) sc,
675 jiffies_to_msecs(jiffies - start_time), (ulong) fwreq,
676 SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
682 snic_stats_update_io_cmpl(&snic->s_stats);
683 } /* end of snic_icmnd_cmpl_handler */
686 snic_proc_dr_cmpl_locked(struct snic *snic,
687 struct snic_fw_req *fwreq,
690 struct scsi_cmnd *sc)
692 struct snic_req_info *rqi = (struct snic_req_info *) CMD_SP(sc);
693 u32 start_time = rqi->start_time;
695 CMD_LR_STATUS(sc) = cmpl_stat;
697 SNIC_SCSI_DBG(snic->shost, "itmf_cmpl: Cmd State = %s\n",
698 snic_ioreq_state_to_str(CMD_STATE(sc)));
700 if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING) {
701 CMD_FLAGS(sc) |= SNIC_DEV_RST_ABTS_PENDING;
703 SNIC_TRC(snic->shost->host_no, cmnd_id, (ulong) sc,
704 jiffies_to_msecs(jiffies - start_time),
705 (ulong) fwreq, 0, SNIC_TRC_CMD_STATE_FLAGS(sc));
707 SNIC_SCSI_DBG(snic->shost,
708 "itmf_cmpl: Terminate Pending Dev Reset Cmpl Recvd.id %x, status %s flags 0x%llx\n",
709 (int)(cmnd_id & SNIC_TAG_MASK),
710 snic_io_status_to_str(cmpl_stat),
717 if (CMD_FLAGS(sc) & SNIC_DEV_RST_TIMEDOUT) {
718 SNIC_TRC(snic->shost->host_no, cmnd_id, (ulong) sc,
719 jiffies_to_msecs(jiffies - start_time),
720 (ulong) fwreq, 0, SNIC_TRC_CMD_STATE_FLAGS(sc));
722 SNIC_SCSI_DBG(snic->shost,
723 "itmf_cmpl:Dev Reset Completion Received after timeout. id %d cmpl status %s flags 0x%llx\n",
724 (int)(cmnd_id & SNIC_TAG_MASK),
725 snic_io_status_to_str(cmpl_stat),
731 CMD_STATE(sc) = SNIC_IOREQ_LR_COMPLETE;
732 CMD_FLAGS(sc) |= SNIC_DEV_RST_DONE;
734 SNIC_SCSI_DBG(snic->shost,
735 "itmf_cmpl:Dev Reset Cmpl Recvd id %d cmpl status %s flags 0x%llx\n",
736 (int)(cmnd_id & SNIC_TAG_MASK),
737 snic_io_status_to_str(cmpl_stat),
741 complete(rqi->dr_done);
742 } /* end of snic_proc_dr_cmpl_locked */
745 * snic_update_abort_stats : Updates abort stats based on completion status.
748 snic_update_abort_stats(struct snic *snic, u8 cmpl_stat)
750 struct snic_abort_stats *abt_stats = &snic->s_stats.abts;
752 SNIC_SCSI_DBG(snic->shost, "Updating Abort stats.\n");
755 case SNIC_STAT_IO_SUCCESS:
758 case SNIC_STAT_TIMEOUT:
759 atomic64_inc(&abt_stats->fw_tmo);
762 case SNIC_STAT_IO_NOT_FOUND:
763 atomic64_inc(&abt_stats->io_not_found);
767 atomic64_inc(&abt_stats->fail);
773 snic_process_itmf_cmpl(struct snic *snic,
774 struct snic_fw_req *fwreq,
777 struct scsi_cmnd *sc)
779 struct snic_req_info *rqi = NULL;
781 spinlock_t *io_lock = NULL;
786 io_lock = snic_io_lock_hash(snic, sc);
787 spin_lock_irqsave(io_lock, flags);
788 if (CMD_FLAGS(sc) & SNIC_HOST_RESET_CMD_TERM) {
789 spin_unlock_irqrestore(io_lock, flags);
793 rqi = (struct snic_req_info *) CMD_SP(sc);
797 atomic64_inc(&snic->s_stats.io.req_null);
798 spin_unlock_irqrestore(io_lock, flags);
799 CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_REQ_NULL;
800 SNIC_HOST_ERR(snic->shost,
801 "itmf_cmpl: rqi is null,Hdr stat = %s Tag = 0x%x sc = 0x%p flags 0x%llx\n",
802 snic_io_status_to_str(cmpl_stat), cmnd_id, sc,
808 /* Extract task management flags */
809 tm_tags = cmnd_id & ~(SNIC_TAG_MASK);
811 start_time = rqi->start_time;
812 cmnd_id &= (SNIC_TAG_MASK);
816 /* Abort only issued on cmd */
817 snic_update_abort_stats(snic, cmpl_stat);
819 if (CMD_STATE(sc) != SNIC_IOREQ_ABTS_PENDING) {
820 /* This is a late completion. Ignore it. */
822 spin_unlock_irqrestore(io_lock, flags);
826 CMD_STATE(sc) = SNIC_IOREQ_ABTS_COMPLETE;
827 CMD_ABTS_STATUS(sc) = cmpl_stat;
828 CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_DONE;
830 SNIC_SCSI_DBG(snic->shost,
831 "itmf_cmpl:Abort Cmpl Recvd.Tag 0x%x Status %s flags 0x%llx\n",
833 snic_io_status_to_str(cmpl_stat),
837 * If scsi_eh thread is blocked waiting for abts complete,
838 * signal completion to it. IO will be cleaned in the thread,
839 * else clean it in this context.
841 if (rqi->abts_done) {
842 complete(rqi->abts_done);
843 spin_unlock_irqrestore(io_lock, flags);
845 break; /* jump out */
849 sc->result = (DID_ERROR << 16);
850 SNIC_SCSI_DBG(snic->shost,
851 "itmf_cmpl: Completing IO. sc %p flags 0x%llx\n",
854 spin_unlock_irqrestore(io_lock, flags);
856 snic_release_req_buf(snic, rqi, sc);
859 SNIC_TRC(snic->shost->host_no, cmnd_id, (ulong) sc,
860 jiffies_to_msecs(jiffies - start_time),
861 (ulong) fwreq, SNIC_TRC_CMD(sc),
862 SNIC_TRC_CMD_STATE_FLAGS(sc));
869 case SNIC_TAG_DEV_RST:
870 case SNIC_TAG_DEV_RST | SNIC_TAG_IOCTL_DEV_RST:
871 snic_proc_dr_cmpl_locked(snic, fwreq, cmpl_stat, cmnd_id, sc);
872 spin_unlock_irqrestore(io_lock, flags);
877 case SNIC_TAG_ABORT | SNIC_TAG_DEV_RST:
878 /* Abort and terminate completion of device reset req */
880 CMD_STATE(sc) = SNIC_IOREQ_ABTS_COMPLETE;
881 CMD_ABTS_STATUS(sc) = cmpl_stat;
882 CMD_FLAGS(sc) |= SNIC_DEV_RST_DONE;
884 SNIC_SCSI_DBG(snic->shost,
885 "itmf_cmpl:dev reset abts cmpl recvd. id %d status %s flags 0x%llx\n",
886 cmnd_id, snic_io_status_to_str(cmpl_stat),
890 complete(rqi->abts_done);
892 spin_unlock_irqrestore(io_lock, flags);
897 spin_unlock_irqrestore(io_lock, flags);
898 SNIC_HOST_ERR(snic->shost,
899 "itmf_cmpl: Unknown TM tag bit 0x%x\n", tm_tags);
901 SNIC_HOST_ERR(snic->shost,
902 "itmf_cmpl:Unexpected itmf io stat %s Tag = 0x%x flags 0x%llx\n",
903 snic_ioreq_state_to_str(CMD_STATE(sc)),
913 } /* end of snic_process_itmf_cmpl_status */
916 * snic_itmf_cmpl_handler.
917 * Routine to handle itmf completions.
920 snic_itmf_cmpl_handler(struct snic *snic, struct snic_fw_req *fwreq)
922 struct scsi_cmnd *sc = NULL;
923 struct snic_req_info *rqi = NULL;
924 struct snic_itmf_cmpl *itmf_cmpl = NULL;
931 snic_io_hdr_dec(&fwreq->hdr, &typ, &hdr_stat, &cmnd_id, &hid, &ctx);
932 SNIC_SCSI_DBG(snic->shost,
933 "Itmf_cmpl: %s: type = %x, hdr_stat = %x, cmnd_id = %x, hid = %x,ctx = %lx\n",
934 __func__, typ, hdr_stat, cmnd_id, hid, ctx);
936 itmf_cmpl = &fwreq->u.itmf_cmpl;
937 SNIC_SCSI_DBG(snic->shost,
938 "Itmf_cmpl: nterm %u , flags 0x%x\n",
939 le32_to_cpu(itmf_cmpl->nterminated), itmf_cmpl->flags);
941 /* spl case, dev reset issued through ioctl */
942 if (cmnd_id & SNIC_TAG_IOCTL_DEV_RST) {
943 rqi = (struct snic_req_info *) ctx;
949 if ((cmnd_id & SNIC_TAG_MASK) >= snic->max_tag_id) {
950 SNIC_HOST_ERR(snic->shost,
951 "Itmf_cmpl: Tag 0x%x out of Range,HdrStat %s\n",
952 cmnd_id, snic_io_status_to_str(hdr_stat));
958 sc = scsi_host_find_tag(snic->shost, cmnd_id & SNIC_TAG_MASK);
963 atomic64_inc(&snic->s_stats.io.sc_null);
964 SNIC_HOST_ERR(snic->shost,
965 "Itmf_cmpl: sc is NULL - Hdr Stat %s Tag 0x%x\n",
966 snic_io_status_to_str(hdr_stat), cmnd_id);
971 snic_process_itmf_cmpl(snic, fwreq, cmnd_id, hdr_stat, sc);
972 } /* end of snic_itmf_cmpl_handler */
977 snic_hba_reset_scsi_cleanup(struct snic *snic, struct scsi_cmnd *sc)
979 struct snic_stats *st = &snic->s_stats;
980 long act_ios = 0, act_fwreqs = 0;
982 SNIC_SCSI_DBG(snic->shost, "HBA Reset scsi cleanup.\n");
983 snic_scsi_cleanup(snic, snic_cmd_tag(sc));
985 /* Update stats on pending IOs */
986 act_ios = atomic64_read(&st->io.active);
987 atomic64_add(act_ios, &st->io.compl);
988 atomic64_sub(act_ios, &st->io.active);
990 act_fwreqs = atomic64_read(&st->fw.actv_reqs);
991 atomic64_sub(act_fwreqs, &st->fw.actv_reqs);
995 * snic_hba_reset_cmpl_handler :
998 * 1. Cleanup all the scsi cmds, release all snic specific cmds
999 * 2. Issue Report Targets in case of SAN targets
1002 snic_hba_reset_cmpl_handler(struct snic *snic, struct snic_fw_req *fwreq)
1009 struct scsi_cmnd *sc = NULL;
1010 struct snic_req_info *rqi = NULL;
1011 spinlock_t *io_lock = NULL;
1012 unsigned long flags, gflags;
1015 snic_io_hdr_dec(&fwreq->hdr, &typ, &hdr_stat, &cmnd_id, &hid, &ctx);
1016 SNIC_HOST_INFO(snic->shost,
1017 "reset_cmpl:Tag %d ctx %lx cmpl status %s HBA Reset Completion received.\n",
1018 cmnd_id, ctx, snic_io_status_to_str(hdr_stat));
1020 SNIC_SCSI_DBG(snic->shost,
1021 "reset_cmpl: type = %x, hdr_stat = %x, cmnd_id = %x, hid = %x, ctx = %lx\n",
1022 typ, hdr_stat, cmnd_id, hid, ctx);
1024 /* spl case, host reset issued through ioctl */
1025 if (cmnd_id == SCSI_NO_TAG) {
1026 rqi = (struct snic_req_info *) ctx;
1027 SNIC_HOST_INFO(snic->shost,
1028 "reset_cmpl:Tag %d ctx %lx cmpl stat %s\n",
1029 cmnd_id, ctx, snic_io_status_to_str(hdr_stat));
1035 if (cmnd_id >= snic->max_tag_id) {
1036 SNIC_HOST_ERR(snic->shost,
1037 "reset_cmpl: Tag 0x%x out of Range,HdrStat %s\n",
1038 cmnd_id, snic_io_status_to_str(hdr_stat));
1044 sc = scsi_host_find_tag(snic->shost, cmnd_id);
1047 atomic64_inc(&snic->s_stats.io.sc_null);
1048 SNIC_HOST_ERR(snic->shost,
1049 "reset_cmpl: sc is NULL - Hdr Stat %s Tag 0x%x\n",
1050 snic_io_status_to_str(hdr_stat), cmnd_id);
1056 SNIC_HOST_INFO(snic->shost,
1057 "reset_cmpl: sc %p rqi %p Tag %d flags 0x%llx\n",
1058 sc, rqi, cmnd_id, CMD_FLAGS(sc));
1060 io_lock = snic_io_lock_hash(snic, sc);
1061 spin_lock_irqsave(io_lock, flags);
1063 if (!snic->remove_wait) {
1064 spin_unlock_irqrestore(io_lock, flags);
1065 SNIC_HOST_ERR(snic->shost,
1066 "reset_cmpl:host reset completed after timeout\n");
1072 rqi = (struct snic_req_info *) CMD_SP(sc);
1076 atomic64_inc(&snic->s_stats.io.req_null);
1077 spin_unlock_irqrestore(io_lock, flags);
1078 CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_REQ_NULL;
1079 SNIC_HOST_ERR(snic->shost,
1080 "reset_cmpl: rqi is null,Hdr stat %s Tag 0x%x sc 0x%p flags 0x%llx\n",
1081 snic_io_status_to_str(hdr_stat), cmnd_id, sc,
1089 spin_unlock_irqrestore(io_lock, flags);
1092 snic_hba_reset_scsi_cleanup(snic, sc);
1094 SNIC_BUG_ON(snic_get_state(snic) != SNIC_OFFLINE &&
1095 snic_get_state(snic) != SNIC_FWRESET);
1097 /* Careful locking between snic_lock and io lock */
1098 spin_lock_irqsave(io_lock, flags);
1099 spin_lock_irqsave(&snic->snic_lock, gflags);
1100 if (snic_get_state(snic) == SNIC_FWRESET)
1101 snic_set_state(snic, SNIC_ONLINE);
1102 spin_unlock_irqrestore(&snic->snic_lock, gflags);
1104 if (snic->remove_wait)
1105 complete(snic->remove_wait);
1107 spin_unlock_irqrestore(io_lock, flags);
1108 atomic64_inc(&snic->s_stats.reset.hba_reset_cmpl);
1111 /* Rediscovery is for SAN */
1112 if (snic->config.xpt_type == SNIC_DAS)
1115 SNIC_SCSI_DBG(snic->shost, "reset_cmpl: Queuing discovery work.\n");
1116 queue_work(snic_glob->event_q, &snic->disc_work);
1122 snic_msg_ack_handler(struct snic *snic, struct snic_fw_req *fwreq)
1124 SNIC_HOST_INFO(snic->shost, "Message Ack Received.\n");
1126 SNIC_ASSERT_NOT_IMPL(1);
1130 snic_aen_handler(struct snic *snic, struct snic_fw_req *fwreq)
1135 struct snic_async_evnotify *aen = &fwreq->u.async_ev;
1138 snic_io_hdr_dec(&fwreq->hdr, &typ, &hdr_stat, &cmnd_id, &hid, &ctx);
1139 SNIC_SCSI_DBG(snic->shost,
1140 "aen: type = %x, hdr_stat = %x, cmnd_id = %x, hid = %x, ctx = %lx\n",
1141 typ, hdr_stat, cmnd_id, hid, ctx);
1143 event_id = le32_to_cpu(aen->ev_id);
1146 case SNIC_EV_TGT_OFFLINE:
1147 SNIC_HOST_INFO(snic->shost, "aen:TGT_OFFLINE Event Recvd.\n");
1150 case SNIC_EV_TGT_ONLINE:
1151 SNIC_HOST_INFO(snic->shost, "aen:TGT_ONLINE Event Recvd.\n");
1154 case SNIC_EV_LUN_OFFLINE:
1155 SNIC_HOST_INFO(snic->shost, "aen:LUN_OFFLINE Event Recvd.\n");
1158 case SNIC_EV_LUN_ONLINE:
1159 SNIC_HOST_INFO(snic->shost, "aen:LUN_ONLINE Event Recvd.\n");
1162 case SNIC_EV_CONF_CHG:
1163 SNIC_HOST_INFO(snic->shost, "aen:Config Change Event Recvd.\n");
1166 case SNIC_EV_TGT_ADDED:
1167 SNIC_HOST_INFO(snic->shost, "aen:TGT_ADD Event Recvd.\n");
1170 case SNIC_EV_TGT_DELTD:
1171 SNIC_HOST_INFO(snic->shost, "aen:TGT_DEL Event Recvd.\n");
1174 case SNIC_EV_LUN_ADDED:
1175 SNIC_HOST_INFO(snic->shost, "aen:LUN_ADD Event Recvd.\n");
1178 case SNIC_EV_LUN_DELTD:
1179 SNIC_HOST_INFO(snic->shost, "aen:LUN_DEL Event Recvd.\n");
1182 case SNIC_EV_DISC_CMPL:
1183 SNIC_HOST_INFO(snic->shost, "aen:DISC_CMPL Event Recvd.\n");
1187 SNIC_HOST_INFO(snic->shost, "aen:Unknown Event Recvd.\n");
1192 SNIC_ASSERT_NOT_IMPL(1);
1193 } /* end of snic_aen_handler */
1196 * snic_io_cmpl_handler
1197 * Routine to process CQ entries(IO Completions) posted by fw.
1200 snic_io_cmpl_handler(struct vnic_dev *vdev,
1201 unsigned int cq_idx,
1202 struct snic_fw_req *fwreq)
1204 struct snic *snic = svnic_dev_priv(vdev);
1205 u64 start = jiffies, cmpl_time;
1207 snic_print_desc(__func__, (char *)fwreq, sizeof(*fwreq));
1209 /* Update FW Stats */
1210 if ((fwreq->hdr.type >= SNIC_RSP_REPORT_TGTS_CMPL) &&
1211 (fwreq->hdr.type <= SNIC_RSP_BOOT_LUNS_CMPL))
1212 atomic64_dec(&snic->s_stats.fw.actv_reqs);
1214 SNIC_BUG_ON((fwreq->hdr.type > SNIC_RSP_BOOT_LUNS_CMPL) &&
1215 (fwreq->hdr.type < SNIC_MSG_ASYNC_EVNOTIFY));
1217 /* Check for snic subsys errors */
1218 switch (fwreq->hdr.status) {
1219 case SNIC_STAT_NOT_READY: /* XPT yet to initialize */
1220 SNIC_HOST_ERR(snic->shost,
1221 "sNIC SubSystem is NOT Ready.\n");
1224 case SNIC_STAT_FATAL_ERROR: /* XPT Error */
1225 SNIC_HOST_ERR(snic->shost,
1226 "sNIC SubSystem in Unrecoverable State.\n");
1230 switch (fwreq->hdr.type) {
1231 case SNIC_RSP_EXCH_VER_CMPL:
1232 snic_io_exch_ver_cmpl_handler(snic, fwreq);
1235 case SNIC_RSP_REPORT_TGTS_CMPL:
1236 snic_report_tgt_cmpl_handler(snic, fwreq);
1239 case SNIC_RSP_ICMND_CMPL:
1240 snic_icmnd_cmpl_handler(snic, fwreq);
1243 case SNIC_RSP_ITMF_CMPL:
1244 snic_itmf_cmpl_handler(snic, fwreq);
1247 case SNIC_RSP_HBA_RESET_CMPL:
1248 snic_hba_reset_cmpl_handler(snic, fwreq);
1252 snic_msg_ack_handler(snic, fwreq);
1255 case SNIC_MSG_ASYNC_EVNOTIFY:
1256 snic_aen_handler(snic, fwreq);
1261 SNIC_SCSI_DBG(snic->shost,
1262 "Unknown Firmware completion request type %d\n",
1268 cmpl_time = jiffies - start;
1269 if (cmpl_time > atomic64_read(&snic->s_stats.io.max_cmpl_time))
1270 atomic64_set(&snic->s_stats.io.max_cmpl_time, cmpl_time);
1273 } /* end of snic_io_cmpl_handler */
1276 * snic_fwcq_cmpl_handler
1277 * Routine to process fwCQ
1278 * This CQ is independent, and not associated with wq/rq/wq_copy queues
1281 snic_fwcq_cmpl_handler(struct snic *snic, int io_cmpl_work)
1283 unsigned int num_ent = 0; /* number cq entries processed */
1284 unsigned int cq_idx;
1285 unsigned int nent_per_cq;
1286 struct snic_misc_stats *misc_stats = &snic->s_stats.misc;
1288 for (cq_idx = snic->wq_count; cq_idx < snic->cq_count; cq_idx++) {
1289 nent_per_cq = vnic_cq_fw_service(&snic->cq[cq_idx],
1290 snic_io_cmpl_handler,
1292 num_ent += nent_per_cq;
1294 if (nent_per_cq > atomic64_read(&misc_stats->max_cq_ents))
1295 atomic64_set(&misc_stats->max_cq_ents, nent_per_cq);
1299 } /* end of snic_fwcq_cmpl_handler */
1302 * snic_queue_itmf_req: Common API to queue Task Management requests.
1303 * Use rqi->tm_tag for passing special tags.
1304 * @req_id : aborted request's tag, -1 for lun reset.
1307 snic_queue_itmf_req(struct snic *snic,
1308 struct snic_host_req *tmreq,
1309 struct scsi_cmnd *sc,
1313 struct snic_req_info *rqi = req_to_rqi(tmreq);
1314 struct scsi_lun lun;
1315 int tm_tag = snic_cmd_tag(sc) | rqi->tm_tag;
1319 SNIC_BUG_ON(!rqi->tm_tag);
1321 /* fill in lun info */
1322 int_to_scsilun(sc->device->lun, &lun);
1324 /* Initialize snic_host_req: itmf */
1325 snic_itmf_init(tmreq,
1330 req_id, /* Command to be aborted. */
1336 * In case of multiple aborts on same cmd,
1337 * use try_wait_for_completion and completion_done() to check
1338 * whether it queues aborts even after completion of abort issued
1339 * prior.SNIC_BUG_ON(completion_done(&rqi->done));
1342 ret = snic_queue_wq_desc(snic, tmreq, sizeof(*tmreq));
1344 SNIC_HOST_ERR(snic->shost,
1345 "qitmf:Queuing ITMF(%d) Req sc %p, rqi %p, req_id %d tag %d Failed, ret = %d\n",
1346 tmf, sc, rqi, req_id, snic_cmd_tag(sc), ret);
1348 SNIC_SCSI_DBG(snic->shost,
1349 "qitmf:Queuing ITMF(%d) Req sc %p, rqi %p, req_id %d, tag %d (req_id)- Success.",
1350 tmf, sc, rqi, req_id, snic_cmd_tag(sc));
1353 } /* end of snic_queue_itmf_req */
1356 snic_issue_tm_req(struct snic *snic,
1357 struct snic_req_info *rqi,
1358 struct scsi_cmnd *sc,
1361 struct snic_host_req *tmreq = NULL;
1362 int req_id = 0, tag = snic_cmd_tag(sc);
1365 if (snic_get_state(snic) == SNIC_FWRESET)
1368 atomic_inc(&snic->ios_inflight);
1370 SNIC_SCSI_DBG(snic->shost,
1371 "issu_tmreq: Task mgmt req %d. rqi %p w/ tag %x\n",
1375 if (tmf == SNIC_ITMF_LUN_RESET) {
1376 tmreq = snic_dr_req_init(snic, rqi);
1377 req_id = SCSI_NO_TAG;
1379 tmreq = snic_abort_req_init(snic, rqi);
1389 ret = snic_queue_itmf_req(snic, tmreq, sc, tmf, req_id);
1393 SNIC_HOST_ERR(snic->shost,
1394 "issu_tmreq: Queueing ITMF(%d) Req, sc %p rqi %p req_id %d tag %x fails err = %d\n",
1395 tmf, sc, rqi, req_id, tag, ret);
1397 SNIC_SCSI_DBG(snic->shost,
1398 "issu_tmreq: Queueing ITMF(%d) Req, sc %p, rqi %p, req_id %d tag %x - Success.\n",
1399 tmf, sc, rqi, req_id, tag);
1402 atomic_dec(&snic->ios_inflight);
1408 * snic_queue_abort_req : Queues abort req to WQ
1411 snic_queue_abort_req(struct snic *snic,
1412 struct snic_req_info *rqi,
1413 struct scsi_cmnd *sc,
1416 SNIC_SCSI_DBG(snic->shost, "q_abtreq: sc %p, rqi %p, tag %x, tmf %d\n",
1417 sc, rqi, snic_cmd_tag(sc), tmf);
1419 /* Add special tag for abort */
1420 rqi->tm_tag |= SNIC_TAG_ABORT;
1422 return snic_issue_tm_req(snic, rqi, sc, tmf);
1426 * snic_abort_finish : called by snic_abort_cmd on queuing abort successfully.
1429 snic_abort_finish(struct snic *snic, struct scsi_cmnd *sc)
1431 struct snic_req_info *rqi = NULL;
1432 spinlock_t *io_lock = NULL;
1433 unsigned long flags;
1434 int ret = 0, tag = snic_cmd_tag(sc);
1436 io_lock = snic_io_lock_hash(snic, sc);
1437 spin_lock_irqsave(io_lock, flags);
1438 rqi = (struct snic_req_info *) CMD_SP(sc);
1440 atomic64_inc(&snic->s_stats.io.req_null);
1441 CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_REQ_NULL;
1443 SNIC_SCSI_DBG(snic->shost,
1444 "abt_fini:req info is null tag 0x%x, sc 0x%p flags 0x%llx\n",
1445 tag, sc, CMD_FLAGS(sc));
1451 rqi->abts_done = NULL;
1455 /* Check the abort status. */
1456 switch (CMD_ABTS_STATUS(sc)) {
1457 case SNIC_INVALID_CODE:
1458 /* Firmware didn't complete abort req, timedout */
1459 CMD_FLAGS(sc) |= SNIC_IO_ABTS_TIMEDOUT;
1460 atomic64_inc(&snic->s_stats.abts.drv_tmo);
1461 SNIC_SCSI_DBG(snic->shost,
1462 "abt_fini:sc %p Tag %x Driver Timeout.flags 0x%llx\n",
1463 sc, snic_cmd_tag(sc), CMD_FLAGS(sc));
1464 /* do not release snic request in timedout case */
1469 case SNIC_STAT_IO_SUCCESS:
1470 case SNIC_STAT_IO_NOT_FOUND:
1473 * If abort path doesn't call scsi_done(),
1474 * the # IO timeouts == 2, will cause the LUN offline.
1475 * Call scsi_done to complete the IO.
1477 sc->result = (DID_ERROR << 16);
1482 /* Firmware completed abort with error */
1489 SNIC_HOST_INFO(snic->shost,
1490 "abt_fini: Tag %x, Cmpl Status %s flags 0x%llx\n",
1491 tag, snic_io_status_to_str(CMD_ABTS_STATUS(sc)),
1495 spin_unlock_irqrestore(io_lock, flags);
1497 snic_release_req_buf(snic, rqi, sc);
1500 } /* end of snic_abort_finish */
1503 * snic_send_abort_and_wait : Issues Abort, and Waits
1506 snic_send_abort_and_wait(struct snic *snic, struct scsi_cmnd *sc)
1508 struct snic_req_info *rqi = NULL;
1509 enum snic_ioreq_state sv_state;
1510 struct snic_tgt *tgt = NULL;
1511 spinlock_t *io_lock = NULL;
1512 DECLARE_COMPLETION_ONSTACK(tm_done);
1513 unsigned long flags;
1514 int ret = 0, tmf = 0, tag = snic_cmd_tag(sc);
1516 tgt = starget_to_tgt(scsi_target(sc->device));
1517 if ((snic_tgt_chkready(tgt) != 0) && (tgt->tdata.typ == SNIC_TGT_SAN))
1518 tmf = SNIC_ITMF_ABTS_TASK_TERM;
1520 tmf = SNIC_ITMF_ABTS_TASK;
1524 io_lock = snic_io_lock_hash(snic, sc);
1527 * Avoid a race between SCSI issuing the abort and the device
1528 * completing the command.
1530 * If the command is already completed by fw_cmpl code,
1531 * we just return SUCCESS from here. This means that the abort
1532 * succeeded. In the SCSI ML, since the timeout for command has
1533 * happend, the completion wont actually complete the command
1534 * and it will be considered as an aborted command
1536 * The CMD_SP will not be cleared except while holding io_lock
1538 spin_lock_irqsave(io_lock, flags);
1539 rqi = (struct snic_req_info *) CMD_SP(sc);
1541 spin_unlock_irqrestore(io_lock, flags);
1543 SNIC_HOST_ERR(snic->shost,
1544 "abt_cmd: rqi is null. Tag %d flags 0x%llx\n",
1545 tag, CMD_FLAGS(sc));
1552 rqi->abts_done = &tm_done;
1553 if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING) {
1554 spin_unlock_irqrestore(io_lock, flags);
1559 SNIC_BUG_ON(!rqi->abts_done);
1561 /* Save Command State, should be restored on failed to Queue. */
1562 sv_state = CMD_STATE(sc);
1565 * Command is still pending, need to abort it
1566 * If the fw completes the command after this point,
1567 * the completion won't be done till mid-layer, since abot
1568 * has already started.
1570 CMD_STATE(sc) = SNIC_IOREQ_ABTS_PENDING;
1571 CMD_ABTS_STATUS(sc) = SNIC_INVALID_CODE;
1573 SNIC_SCSI_DBG(snic->shost, "send_abt_cmd: TAG 0x%x\n", tag);
1575 spin_unlock_irqrestore(io_lock, flags);
1577 /* Now Queue the abort command to firmware */
1578 ret = snic_queue_abort_req(snic, rqi, sc, tmf);
1580 atomic64_inc(&snic->s_stats.abts.q_fail);
1581 SNIC_HOST_ERR(snic->shost,
1582 "send_abt_cmd: IO w/ Tag 0x%x fail w/ err %d flags 0x%llx\n",
1583 tag, ret, CMD_FLAGS(sc));
1585 spin_lock_irqsave(io_lock, flags);
1586 /* Restore Command's previous state */
1587 CMD_STATE(sc) = sv_state;
1588 rqi = (struct snic_req_info *) CMD_SP(sc);
1590 rqi->abts_done = NULL;
1591 spin_unlock_irqrestore(io_lock, flags);
1597 spin_lock_irqsave(io_lock, flags);
1598 if (tmf == SNIC_ITMF_ABTS_TASK) {
1599 CMD_FLAGS(sc) |= SNIC_IO_ABTS_ISSUED;
1600 atomic64_inc(&snic->s_stats.abts.num);
1603 CMD_FLAGS(sc) |= SNIC_IO_TERM_ISSUED;
1605 spin_unlock_irqrestore(io_lock, flags);
1607 SNIC_SCSI_DBG(snic->shost,
1608 "send_abt_cmd: sc %p Tag %x flags 0x%llx\n",
1609 sc, tag, CMD_FLAGS(sc));
1616 * Queued an abort IO, wait for its completion.
1617 * Once the fw completes the abort command, it will
1618 * wakeup this thread.
1620 wait_for_completion_timeout(&tm_done, SNIC_ABTS_TIMEOUT);
1624 } /* end of snic_send_abort_and_wait */
1627 * This function is exported to SCSI for sending abort cmnds.
1628 * A SCSI IO is represent by snic_ioreq in the driver.
1629 * The snic_ioreq is linked to the SCSI Cmd, thus a link with the ULP'S IO
1632 snic_abort_cmd(struct scsi_cmnd *sc)
1634 struct snic *snic = shost_priv(sc->device->host);
1635 int ret = SUCCESS, tag = snic_cmd_tag(sc);
1636 u32 start_time = jiffies;
1638 SNIC_SCSI_DBG(snic->shost, "abt_cmd:sc %p :0x%x :req = %p :tag = %d\n",
1639 sc, sc->cmnd[0], sc->request, tag);
1641 if (unlikely(snic_get_state(snic) != SNIC_ONLINE)) {
1642 SNIC_HOST_ERR(snic->shost,
1643 "abt_cmd: tag %x Parent Devs are not rdy\n",
1651 ret = snic_send_abort_and_wait(snic, sc);
1655 ret = snic_abort_finish(snic, sc);
1658 SNIC_TRC(snic->shost->host_no, tag, (ulong) sc,
1659 jiffies_to_msecs(jiffies - start_time), 0,
1660 SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
1662 SNIC_SCSI_DBG(snic->shost,
1663 "abts: Abort Req Status = %s\n",
1664 (ret == SUCCESS) ? "SUCCESS" :
1665 ((ret == FAST_IO_FAIL) ? "FAST_IO_FAIL" : "FAILED"));
1673 snic_is_abts_pending(struct snic *snic, struct scsi_cmnd *lr_sc)
1675 struct snic_req_info *rqi = NULL;
1676 struct scsi_cmnd *sc = NULL;
1677 struct scsi_device *lr_sdev = NULL;
1678 spinlock_t *io_lock = NULL;
1680 unsigned long flags;
1683 lr_sdev = lr_sc->device;
1685 /* walk through the tag map, an dcheck if IOs are still pending in fw*/
1686 for (tag = 0; tag < snic->max_tag_id; tag++) {
1687 io_lock = snic_io_lock_tag(snic, tag);
1689 spin_lock_irqsave(io_lock, flags);
1690 sc = scsi_host_find_tag(snic->shost, tag);
1692 if (!sc || (lr_sc && (sc->device != lr_sdev || sc == lr_sc))) {
1693 spin_unlock_irqrestore(io_lock, flags);
1698 rqi = (struct snic_req_info *) CMD_SP(sc);
1700 spin_unlock_irqrestore(io_lock, flags);
1706 * Found IO that is still pending w/ firmware and belongs to
1707 * the LUN that is under reset, if lr_sc != NULL
1709 SNIC_SCSI_DBG(snic->shost, "Found IO in %s on LUN\n",
1710 snic_ioreq_state_to_str(CMD_STATE(sc)));
1712 if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING) {
1713 spin_unlock_irqrestore(io_lock, flags);
1718 spin_unlock_irqrestore(io_lock, flags);
1722 } /* end of snic_is_abts_pending */
1725 snic_dr_clean_single_req(struct snic *snic,
1727 struct scsi_device *lr_sdev)
1729 struct snic_req_info *rqi = NULL;
1730 struct snic_tgt *tgt = NULL;
1731 struct scsi_cmnd *sc = NULL;
1732 spinlock_t *io_lock = NULL;
1733 u32 sv_state = 0, tmf = 0;
1734 DECLARE_COMPLETION_ONSTACK(tm_done);
1735 unsigned long flags;
1738 io_lock = snic_io_lock_tag(snic, tag);
1739 spin_lock_irqsave(io_lock, flags);
1740 sc = scsi_host_find_tag(snic->shost, tag);
1742 /* Ignore Cmd that don't belong to Lun Reset device */
1743 if (!sc || sc->device != lr_sdev)
1746 rqi = (struct snic_req_info *) CMD_SP(sc);
1752 if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
1756 if ((CMD_FLAGS(sc) & SNIC_DEVICE_RESET) &&
1757 (!(CMD_FLAGS(sc) & SNIC_DEV_RST_ISSUED))) {
1759 SNIC_SCSI_DBG(snic->shost,
1760 "clean_single_req: devrst is not pending sc 0x%p\n",
1766 SNIC_SCSI_DBG(snic->shost,
1767 "clean_single_req: Found IO in %s on lun\n",
1768 snic_ioreq_state_to_str(CMD_STATE(sc)));
1770 /* Save Command State */
1771 sv_state = CMD_STATE(sc);
1774 * Any pending IO issued prior to reset is expected to be
1775 * in abts pending state, if not we need to set SNIC_IOREQ_ABTS_PENDING
1776 * to indicate the IO is abort pending.
1777 * When IO is completed, the IO will be handed over and handled
1781 CMD_STATE(sc) = SNIC_IOREQ_ABTS_PENDING;
1782 SNIC_BUG_ON(rqi->abts_done);
1784 if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET) {
1785 rqi->tm_tag = SNIC_TAG_DEV_RST;
1787 SNIC_SCSI_DBG(snic->shost,
1788 "clean_single_req:devrst sc 0x%p\n", sc);
1791 CMD_ABTS_STATUS(sc) = SNIC_INVALID_CODE;
1792 rqi->abts_done = &tm_done;
1793 spin_unlock_irqrestore(io_lock, flags);
1795 tgt = starget_to_tgt(scsi_target(sc->device));
1796 if ((snic_tgt_chkready(tgt) != 0) && (tgt->tdata.typ == SNIC_TGT_SAN))
1797 tmf = SNIC_ITMF_ABTS_TASK_TERM;
1799 tmf = SNIC_ITMF_ABTS_TASK;
1801 /* Now queue the abort command to firmware */
1802 ret = snic_queue_abort_req(snic, rqi, sc, tmf);
1804 SNIC_HOST_ERR(snic->shost,
1805 "clean_single_req_err:sc %p, tag %d abt failed. tm_tag %d flags 0x%llx\n",
1806 sc, tag, rqi->tm_tag, CMD_FLAGS(sc));
1808 spin_lock_irqsave(io_lock, flags);
1809 rqi = (struct snic_req_info *) CMD_SP(sc);
1811 rqi->abts_done = NULL;
1813 /* Restore Command State */
1814 if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
1815 CMD_STATE(sc) = sv_state;
1821 spin_lock_irqsave(io_lock, flags);
1822 if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET)
1823 CMD_FLAGS(sc) |= SNIC_DEV_RST_TERM_ISSUED;
1825 CMD_FLAGS(sc) |= SNIC_IO_INTERNAL_TERM_ISSUED;
1826 spin_unlock_irqrestore(io_lock, flags);
1828 wait_for_completion_timeout(&tm_done, SNIC_ABTS_TIMEOUT);
1830 /* Recheck cmd state to check if it now aborted. */
1831 spin_lock_irqsave(io_lock, flags);
1832 rqi = (struct snic_req_info *) CMD_SP(sc);
1834 CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_REQ_NULL;
1837 rqi->abts_done = NULL;
1839 /* if abort is still pending w/ fw, fail */
1840 if (CMD_ABTS_STATUS(sc) == SNIC_INVALID_CODE) {
1841 SNIC_HOST_ERR(snic->shost,
1842 "clean_single_req_err:sc %p tag %d abt still pending w/ fw, tm_tag %d flags 0x%llx\n",
1843 sc, tag, rqi->tm_tag, CMD_FLAGS(sc));
1845 CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_DONE;
1851 CMD_STATE(sc) = SNIC_IOREQ_ABTS_COMPLETE;
1853 spin_unlock_irqrestore(io_lock, flags);
1855 snic_release_req_buf(snic, rqi, sc);
1857 sc->result = (DID_ERROR << 16);
1865 spin_unlock_irqrestore(io_lock, flags);
1868 } /* end of snic_dr_clean_single_req */
1871 snic_dr_clean_pending_req(struct snic *snic, struct scsi_cmnd *lr_sc)
1873 struct scsi_device *lr_sdev = lr_sc->device;
1877 for (tag = 0; tag < snic->max_tag_id; tag++) {
1878 if (tag == snic_cmd_tag(lr_sc))
1881 ret = snic_dr_clean_single_req(snic, tag, lr_sdev);
1883 SNIC_HOST_ERR(snic->shost, "clean_err:tag = %d\n", tag);
1889 schedule_timeout(msecs_to_jiffies(100));
1891 /* Walk through all the cmds and check abts status. */
1892 if (snic_is_abts_pending(snic, lr_sc)) {
1899 SNIC_SCSI_DBG(snic->shost, "clean_pending_req: Success.\n");
1905 SNIC_HOST_ERR(snic->shost,
1906 "Failed to Clean Pending IOs on %s device.\n",
1907 dev_name(&lr_sdev->sdev_gendev));
1911 } /* end of snic_dr_clean_pending_req */
1914 * snic_dr_finish : Called by snic_device_reset
1917 snic_dr_finish(struct snic *snic, struct scsi_cmnd *sc)
1919 struct snic_req_info *rqi = NULL;
1920 spinlock_t *io_lock = NULL;
1921 unsigned long flags;
1925 io_lock = snic_io_lock_hash(snic, sc);
1926 spin_lock_irqsave(io_lock, flags);
1927 rqi = (struct snic_req_info *) CMD_SP(sc);
1929 spin_unlock_irqrestore(io_lock, flags);
1930 SNIC_SCSI_DBG(snic->shost,
1931 "dr_fini: rqi is null tag 0x%x sc 0x%p flags 0x%llx\n",
1932 snic_cmd_tag(sc), sc, CMD_FLAGS(sc));
1938 rqi->dr_done = NULL;
1940 lr_res = CMD_LR_STATUS(sc);
1943 case SNIC_INVALID_CODE:
1945 SNIC_SCSI_DBG(snic->shost,
1946 "dr_fini: Tag %x Dev Reset Timedout. flags 0x%llx\n",
1947 snic_cmd_tag(sc), CMD_FLAGS(sc));
1949 CMD_FLAGS(sc) |= SNIC_DEV_RST_TIMEDOUT;
1954 case SNIC_STAT_IO_SUCCESS:
1955 SNIC_SCSI_DBG(snic->shost,
1956 "dr_fini: Tag %x Dev Reset cmpl\n",
1962 SNIC_HOST_ERR(snic->shost,
1963 "dr_fini:Device Reset completed& failed.Tag = %x lr_status %s flags 0x%llx\n",
1965 snic_io_status_to_str(lr_res), CMD_FLAGS(sc));
1969 spin_unlock_irqrestore(io_lock, flags);
1972 * Cleanup any IOs on this LUN that have still not completed.
1973 * If any of these fail, then LUN Reset fails.
1974 * Cleanup cleans all commands on this LUN except
1975 * the lun reset command. If all cmds get cleaned, the LUN Reset
1979 ret = snic_dr_clean_pending_req(snic, sc);
1981 spin_lock_irqsave(io_lock, flags);
1982 SNIC_SCSI_DBG(snic->shost,
1983 "dr_fini: Device Reset Failed since could not abort all IOs. Tag = %x.\n",
1985 rqi = (struct snic_req_info *) CMD_SP(sc);
1989 /* Cleanup LUN Reset Command */
1990 spin_lock_irqsave(io_lock, flags);
1991 rqi = (struct snic_req_info *) CMD_SP(sc);
1993 ret = SUCCESS; /* Completed Successfully */
1999 lockdep_assert_held(io_lock);
2002 spin_unlock_irqrestore(io_lock, flags);
2005 snic_release_req_buf(snic, rqi, sc);
2009 } /* end of snic_dr_finish */
2012 snic_queue_dr_req(struct snic *snic,
2013 struct snic_req_info *rqi,
2014 struct scsi_cmnd *sc)
2016 /* Add special tag for device reset */
2017 rqi->tm_tag |= SNIC_TAG_DEV_RST;
2019 return snic_issue_tm_req(snic, rqi, sc, SNIC_ITMF_LUN_RESET);
2023 snic_send_dr_and_wait(struct snic *snic, struct scsi_cmnd *sc)
2025 struct snic_req_info *rqi = NULL;
2026 enum snic_ioreq_state sv_state;
2027 spinlock_t *io_lock = NULL;
2028 unsigned long flags;
2029 DECLARE_COMPLETION_ONSTACK(tm_done);
2030 int ret = FAILED, tag = snic_cmd_tag(sc);
2032 io_lock = snic_io_lock_hash(snic, sc);
2033 spin_lock_irqsave(io_lock, flags);
2034 CMD_FLAGS(sc) |= SNIC_DEVICE_RESET;
2035 rqi = (struct snic_req_info *) CMD_SP(sc);
2037 SNIC_HOST_ERR(snic->shost,
2038 "send_dr: rqi is null, Tag 0x%x flags 0x%llx\n",
2039 tag, CMD_FLAGS(sc));
2040 spin_unlock_irqrestore(io_lock, flags);
2046 /* Save Command state to restore in case Queuing failed. */
2047 sv_state = CMD_STATE(sc);
2049 CMD_STATE(sc) = SNIC_IOREQ_LR_PENDING;
2050 CMD_LR_STATUS(sc) = SNIC_INVALID_CODE;
2052 SNIC_SCSI_DBG(snic->shost, "dr: TAG = %x\n", tag);
2054 rqi->dr_done = &tm_done;
2055 SNIC_BUG_ON(!rqi->dr_done);
2057 spin_unlock_irqrestore(io_lock, flags);
2059 * The Command state is changed to IOREQ_PENDING,
2060 * in this case, if the command is completed, the icmnd_cmpl will
2061 * mark the cmd as completed.
2062 * This logic still makes LUN Reset is inevitable.
2065 ret = snic_queue_dr_req(snic, rqi, sc);
2067 SNIC_HOST_ERR(snic->shost,
2068 "send_dr: IO w/ Tag 0x%x Failed err = %d. flags 0x%llx\n",
2069 tag, ret, CMD_FLAGS(sc));
2071 spin_lock_irqsave(io_lock, flags);
2073 CMD_STATE(sc) = sv_state;
2074 rqi = (struct snic_req_info *) CMD_SP(sc);
2076 rqi->dr_done = NULL;
2077 /* rqi is freed in caller. */
2078 spin_unlock_irqrestore(io_lock, flags);
2084 spin_lock_irqsave(io_lock, flags);
2085 CMD_FLAGS(sc) |= SNIC_DEV_RST_ISSUED;
2086 spin_unlock_irqrestore(io_lock, flags);
2090 wait_for_completion_timeout(&tm_done, SNIC_LUN_RESET_TIMEOUT);
2097 * auxillary funciton to check lun reset op is supported or not
2098 * Not supported if returns 0
2101 snic_dev_reset_supported(struct scsi_device *sdev)
2103 struct snic_tgt *tgt = starget_to_tgt(scsi_target(sdev));
2105 if (tgt->tdata.typ == SNIC_TGT_DAS)
2112 snic_unlink_and_release_req(struct snic *snic, struct scsi_cmnd *sc, int flag)
2114 struct snic_req_info *rqi = NULL;
2115 spinlock_t *io_lock = NULL;
2116 unsigned long flags;
2117 u32 start_time = jiffies;
2119 io_lock = snic_io_lock_hash(snic, sc);
2120 spin_lock_irqsave(io_lock, flags);
2121 rqi = (struct snic_req_info *) CMD_SP(sc);
2123 start_time = rqi->start_time;
2127 CMD_FLAGS(sc) |= flag;
2128 spin_unlock_irqrestore(io_lock, flags);
2131 snic_release_req_buf(snic, rqi, sc);
2133 SNIC_TRC(snic->shost->host_no, snic_cmd_tag(sc), (ulong) sc,
2134 jiffies_to_msecs(jiffies - start_time), (ulong) rqi,
2135 SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
2139 * SCSI Eh thread issues a LUN Reset when one or more commands on a LUN
2140 * fail to get aborted. It calls driver's eh_device_reset with a SCSI
2141 * command on the LUN.
2144 snic_device_reset(struct scsi_cmnd *sc)
2146 struct Scsi_Host *shost = sc->device->host;
2147 struct snic *snic = shost_priv(shost);
2148 struct snic_req_info *rqi = NULL;
2149 int tag = snic_cmd_tag(sc);
2150 int start_time = jiffies;
2154 SNIC_SCSI_DBG(shost, "dev_reset:sc %p :0x%x :req = %p :tag = %d\n",
2155 sc, sc->cmnd[0], sc->request,
2157 dr_supp = snic_dev_reset_supported(sc->device);
2159 /* device reset op is not supported */
2160 SNIC_HOST_INFO(shost, "LUN Reset Op not supported.\n");
2161 snic_unlink_and_release_req(snic, sc, SNIC_DEV_RST_NOTSUP);
2166 if (unlikely(snic_get_state(snic) != SNIC_ONLINE)) {
2167 snic_unlink_and_release_req(snic, sc, 0);
2168 SNIC_HOST_ERR(shost, "Devrst: Parent Devs are not online.\n");
2173 /* There is no tag when lun reset is issue through ioctl. */
2174 if (unlikely(tag <= SNIC_NO_TAG)) {
2175 SNIC_HOST_INFO(snic->shost,
2176 "Devrst: LUN Reset Recvd thru IOCTL.\n");
2178 rqi = snic_req_init(snic, 0);
2182 memset(scsi_cmd_priv(sc), 0,
2183 sizeof(struct snic_internal_io_state));
2184 CMD_SP(sc) = (char *)rqi;
2185 CMD_FLAGS(sc) = SNIC_NO_FLAGS;
2187 /* Add special tag for dr coming from user spc */
2188 rqi->tm_tag = SNIC_TAG_IOCTL_DEV_RST;
2192 ret = snic_send_dr_and_wait(snic, sc);
2194 SNIC_HOST_ERR(snic->shost,
2195 "Devrst: IO w/ Tag %x Failed w/ err = %d\n",
2198 snic_unlink_and_release_req(snic, sc, 0);
2203 ret = snic_dr_finish(snic, sc);
2206 SNIC_TRC(snic->shost->host_no, tag, (ulong) sc,
2207 jiffies_to_msecs(jiffies - start_time),
2208 0, SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
2210 SNIC_SCSI_DBG(snic->shost,
2211 "Devrst: Returning from Device Reset : %s\n",
2212 (ret == SUCCESS) ? "SUCCESS" : "FAILED");
2215 } /* end of snic_device_reset */
2218 * SCSI Error handling calls driver's eh_host_reset if all prior
2219 * error handling levels return FAILED.
2221 * Host Reset is the highest level of error recovery. If this fails, then
2222 * host is offlined by SCSI.
2225 * snic_issue_hba_reset : Queues FW Reset Request.
2228 snic_issue_hba_reset(struct snic *snic, struct scsi_cmnd *sc)
2230 struct snic_req_info *rqi = NULL;
2231 struct snic_host_req *req = NULL;
2232 spinlock_t *io_lock = NULL;
2233 DECLARE_COMPLETION_ONSTACK(wait);
2234 unsigned long flags;
2237 rqi = snic_req_init(snic, 0);
2244 if (snic_cmd_tag(sc) == SCSI_NO_TAG) {
2245 memset(scsi_cmd_priv(sc), 0,
2246 sizeof(struct snic_internal_io_state));
2247 SNIC_HOST_INFO(snic->shost, "issu_hr:Host reset thru ioctl.\n");
2251 req = rqi_to_req(rqi);
2253 io_lock = snic_io_lock_hash(snic, sc);
2254 spin_lock_irqsave(io_lock, flags);
2255 SNIC_BUG_ON(CMD_SP(sc) != NULL);
2256 CMD_STATE(sc) = SNIC_IOREQ_PENDING;
2257 CMD_SP(sc) = (char *) rqi;
2258 CMD_FLAGS(sc) |= SNIC_IO_INITIALIZED;
2259 snic->remove_wait = &wait;
2260 spin_unlock_irqrestore(io_lock, flags);
2262 /* Initialize Request */
2263 snic_io_hdr_enc(&req->hdr, SNIC_REQ_HBA_RESET, 0, snic_cmd_tag(sc),
2264 snic->config.hid, 0, (ulong) rqi);
2266 req->u.reset.flags = 0;
2268 ret = snic_queue_wq_desc(snic, req, sizeof(*req));
2270 SNIC_HOST_ERR(snic->shost,
2271 "issu_hr:Queuing HBA Reset Failed. w err %d\n",
2277 spin_lock_irqsave(io_lock, flags);
2278 CMD_FLAGS(sc) |= SNIC_HOST_RESET_ISSUED;
2279 spin_unlock_irqrestore(io_lock, flags);
2280 atomic64_inc(&snic->s_stats.reset.hba_resets);
2281 SNIC_HOST_INFO(snic->shost, "Queued HBA Reset Successfully.\n");
2283 wait_for_completion_timeout(snic->remove_wait,
2284 SNIC_HOST_RESET_TIMEOUT);
2286 if (snic_get_state(snic) == SNIC_FWRESET) {
2287 SNIC_HOST_ERR(snic->shost, "reset_cmpl: Reset Timedout.\n");
2293 spin_lock_irqsave(io_lock, flags);
2294 snic->remove_wait = NULL;
2295 rqi = (struct snic_req_info *) CMD_SP(sc);
2297 spin_unlock_irqrestore(io_lock, flags);
2300 snic_req_free(snic, rqi);
2307 spin_lock_irqsave(io_lock, flags);
2308 snic->remove_wait = NULL;
2309 rqi = (struct snic_req_info *) CMD_SP(sc);
2311 spin_unlock_irqrestore(io_lock, flags);
2314 snic_req_free(snic, rqi);
2317 SNIC_HOST_ERR(snic->shost,
2318 "reset:HBA Reset Failed w/ err = %d.\n",
2322 } /* end of snic_issue_hba_reset */
2325 snic_reset(struct Scsi_Host *shost, struct scsi_cmnd *sc)
2327 struct snic *snic = shost_priv(shost);
2328 enum snic_state sv_state;
2329 unsigned long flags;
2332 /* Set snic state as SNIC_FWRESET*/
2333 sv_state = snic_get_state(snic);
2335 spin_lock_irqsave(&snic->snic_lock, flags);
2336 if (snic_get_state(snic) == SNIC_FWRESET) {
2337 spin_unlock_irqrestore(&snic->snic_lock, flags);
2338 SNIC_HOST_INFO(shost, "reset:prev reset is in progres\n");
2340 msleep(SNIC_HOST_RESET_TIMEOUT);
2346 snic_set_state(snic, SNIC_FWRESET);
2347 spin_unlock_irqrestore(&snic->snic_lock, flags);
2350 /* Wait for all the IOs that are entered in Qcmd */
2351 while (atomic_read(&snic->ios_inflight))
2352 schedule_timeout(msecs_to_jiffies(1));
2354 ret = snic_issue_hba_reset(snic, sc);
2356 SNIC_HOST_ERR(shost,
2357 "reset:Host Reset Failed w/ err %d.\n",
2359 spin_lock_irqsave(&snic->snic_lock, flags);
2360 snic_set_state(snic, sv_state);
2361 spin_unlock_irqrestore(&snic->snic_lock, flags);
2362 atomic64_inc(&snic->s_stats.reset.hba_reset_fail);
2372 } /* end of snic_reset */
2375 * SCSI Error handling calls driver's eh_host_reset if all prior
2376 * error handling levels return FAILED.
2378 * Host Reset is the highest level of error recovery. If this fails, then
2379 * host is offlined by SCSI.
2382 snic_host_reset(struct scsi_cmnd *sc)
2384 struct Scsi_Host *shost = sc->device->host;
2385 u32 start_time = jiffies;
2388 SNIC_SCSI_DBG(shost,
2389 "host reset:sc %p sc_cmd 0x%x req %p tag %d flags 0x%llx\n",
2390 sc, sc->cmnd[0], sc->request,
2391 snic_cmd_tag(sc), CMD_FLAGS(sc));
2393 ret = snic_reset(shost, sc);
2395 SNIC_TRC(shost->host_no, snic_cmd_tag(sc), (ulong) sc,
2396 jiffies_to_msecs(jiffies - start_time),
2397 0, SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
2400 } /* end of snic_host_reset */
2403 * snic_cmpl_pending_tmreq : Caller should hold io_lock
2406 snic_cmpl_pending_tmreq(struct snic *snic, struct scsi_cmnd *sc)
2408 struct snic_req_info *rqi = NULL;
2410 SNIC_SCSI_DBG(snic->shost,
2411 "Completing Pending TM Req sc %p, state %s flags 0x%llx\n",
2412 sc, snic_io_status_to_str(CMD_STATE(sc)), CMD_FLAGS(sc));
2415 * CASE : FW didn't post itmf completion due to PCIe Errors.
2416 * Marking the abort status as Success to call scsi completion
2417 * in snic_abort_finish()
2419 CMD_ABTS_STATUS(sc) = SNIC_STAT_IO_SUCCESS;
2421 rqi = (struct snic_req_info *) CMD_SP(sc);
2426 complete(rqi->dr_done);
2427 else if (rqi->abts_done)
2428 complete(rqi->abts_done);
2432 * snic_scsi_cleanup: Walks through tag map and releases the reqs
2435 snic_scsi_cleanup(struct snic *snic, int ex_tag)
2437 struct snic_req_info *rqi = NULL;
2438 struct scsi_cmnd *sc = NULL;
2439 spinlock_t *io_lock = NULL;
2440 unsigned long flags;
2444 SNIC_SCSI_DBG(snic->shost, "sc_clean: scsi cleanup.\n");
2446 for (tag = 0; tag < snic->max_tag_id; tag++) {
2451 io_lock = snic_io_lock_tag(snic, tag);
2452 spin_lock_irqsave(io_lock, flags);
2453 sc = scsi_host_find_tag(snic->shost, tag);
2455 spin_unlock_irqrestore(io_lock, flags);
2460 if (unlikely(snic_tmreq_pending(sc))) {
2462 * When FW Completes reset w/o sending completions
2463 * for outstanding ios.
2465 snic_cmpl_pending_tmreq(snic, sc);
2466 spin_unlock_irqrestore(io_lock, flags);
2471 rqi = (struct snic_req_info *) CMD_SP(sc);
2473 spin_unlock_irqrestore(io_lock, flags);
2478 SNIC_SCSI_DBG(snic->shost,
2479 "sc_clean: sc %p, rqi %p, tag %d flags 0x%llx\n",
2480 sc, rqi, tag, CMD_FLAGS(sc));
2483 CMD_FLAGS(sc) |= SNIC_SCSI_CLEANUP;
2484 spin_unlock_irqrestore(io_lock, flags);
2485 st_time = rqi->start_time;
2487 SNIC_HOST_INFO(snic->shost,
2488 "sc_clean: Releasing rqi %p : flags 0x%llx\n",
2489 rqi, CMD_FLAGS(sc));
2491 snic_release_req_buf(snic, rqi, sc);
2494 sc->result = DID_TRANSPORT_DISRUPTED << 16;
2495 SNIC_HOST_INFO(snic->shost,
2496 "sc_clean: DID_TRANSPORT_DISRUPTED for sc %p, Tag %d flags 0x%llx rqi %p duration %u msecs\n",
2497 sc, sc->request->tag, CMD_FLAGS(sc), rqi,
2498 jiffies_to_msecs(jiffies - st_time));
2500 /* Update IO stats */
2501 snic_stats_update_io_cmpl(&snic->s_stats);
2503 if (sc->scsi_done) {
2504 SNIC_TRC(snic->shost->host_no, tag, (ulong) sc,
2505 jiffies_to_msecs(jiffies - st_time), 0,
2507 SNIC_TRC_CMD_STATE_FLAGS(sc));
2512 } /* end of snic_scsi_cleanup */
2515 snic_shutdown_scsi_cleanup(struct snic *snic)
2517 SNIC_HOST_INFO(snic->shost, "Shutdown time SCSI Cleanup.\n");
2519 snic_scsi_cleanup(snic, SCSI_NO_TAG);
2520 } /* end of snic_shutdown_scsi_cleanup */
2523 * snic_internal_abort_io
2524 * called by : snic_tgt_scsi_abort_io
2527 snic_internal_abort_io(struct snic *snic, struct scsi_cmnd *sc, int tmf)
2529 struct snic_req_info *rqi = NULL;
2530 spinlock_t *io_lock = NULL;
2531 unsigned long flags;
2535 io_lock = snic_io_lock_hash(snic, sc);
2536 spin_lock_irqsave(io_lock, flags);
2537 rqi = (struct snic_req_info *) CMD_SP(sc);
2539 goto skip_internal_abts;
2541 if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
2542 goto skip_internal_abts;
2544 if ((CMD_FLAGS(sc) & SNIC_DEVICE_RESET) &&
2545 (!(CMD_FLAGS(sc) & SNIC_DEV_RST_ISSUED))) {
2547 SNIC_SCSI_DBG(snic->shost,
2548 "internal_abts: dev rst not pending sc 0x%p\n",
2551 goto skip_internal_abts;
2555 if (!(CMD_FLAGS(sc) & SNIC_IO_ISSUED)) {
2556 SNIC_SCSI_DBG(snic->shost,
2557 "internal_abts: IO not yet issued sc 0x%p tag 0x%x flags 0x%llx state %d\n",
2558 sc, snic_cmd_tag(sc), CMD_FLAGS(sc), CMD_STATE(sc));
2560 goto skip_internal_abts;
2563 sv_state = CMD_STATE(sc);
2564 CMD_STATE(sc) = SNIC_IOREQ_ABTS_PENDING;
2565 CMD_ABTS_STATUS(sc) = SNIC_INVALID_CODE;
2566 CMD_FLAGS(sc) |= SNIC_IO_INTERNAL_TERM_PENDING;
2568 if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET) {
2570 rqi->tm_tag = SNIC_TAG_DEV_RST;
2571 SNIC_SCSI_DBG(snic->shost, "internal_abts:dev rst sc %p\n", sc);
2574 SNIC_SCSI_DBG(snic->shost, "internal_abts: Issuing abts tag %x\n",
2576 SNIC_BUG_ON(rqi->abts_done);
2577 spin_unlock_irqrestore(io_lock, flags);
2579 ret = snic_queue_abort_req(snic, rqi, sc, tmf);
2581 SNIC_HOST_ERR(snic->shost,
2582 "internal_abts: Tag = %x , Failed w/ err = %d\n",
2583 snic_cmd_tag(sc), ret);
2585 spin_lock_irqsave(io_lock, flags);
2587 if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
2588 CMD_STATE(sc) = sv_state;
2590 goto skip_internal_abts;
2593 spin_lock_irqsave(io_lock, flags);
2594 if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET)
2595 CMD_FLAGS(sc) |= SNIC_DEV_RST_TERM_ISSUED;
2597 CMD_FLAGS(sc) |= SNIC_IO_INTERNAL_TERM_ISSUED;
2602 lockdep_assert_held(io_lock);
2603 spin_unlock_irqrestore(io_lock, flags);
2606 } /* end of snic_internal_abort_io */
2609 * snic_tgt_scsi_abort_io : called by snic_tgt_del
2612 snic_tgt_scsi_abort_io(struct snic_tgt *tgt)
2614 struct snic *snic = NULL;
2615 struct scsi_cmnd *sc = NULL;
2616 struct snic_tgt *sc_tgt = NULL;
2617 spinlock_t *io_lock = NULL;
2618 unsigned long flags;
2619 int ret = 0, tag, abt_cnt = 0, tmf = 0;
2624 snic = shost_priv(snic_tgt_to_shost(tgt));
2625 SNIC_SCSI_DBG(snic->shost, "tgt_abt_io: Cleaning Pending IOs.\n");
2627 if (tgt->tdata.typ == SNIC_TGT_DAS)
2628 tmf = SNIC_ITMF_ABTS_TASK;
2630 tmf = SNIC_ITMF_ABTS_TASK_TERM;
2632 for (tag = 0; tag < snic->max_tag_id; tag++) {
2633 io_lock = snic_io_lock_tag(snic, tag);
2635 spin_lock_irqsave(io_lock, flags);
2636 sc = scsi_host_find_tag(snic->shost, tag);
2638 spin_unlock_irqrestore(io_lock, flags);
2643 sc_tgt = starget_to_tgt(scsi_target(sc->device));
2644 if (sc_tgt != tgt) {
2645 spin_unlock_irqrestore(io_lock, flags);
2649 spin_unlock_irqrestore(io_lock, flags);
2651 ret = snic_internal_abort_io(snic, sc, tmf);
2653 SNIC_HOST_ERR(snic->shost,
2654 "tgt_abt_io: Tag %x, Failed w err = %d\n",
2664 SNIC_SCSI_DBG(snic->shost, "tgt_abt_io: abt_cnt = %d\n", abt_cnt);
2667 } /* end of snic_tgt_scsi_abort_io */