1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright 2014 Cisco Systems, Inc. All rights reserved.
4 #include <linux/mempool.h>
5 #include <linux/errno.h>
6 #include <linux/init.h>
7 #include <linux/workqueue.h>
9 #include <linux/spinlock.h>
10 #include <linux/delay.h>
11 #include <linux/gfp.h>
12 #include <scsi/scsi.h>
13 #include <scsi/scsi_host.h>
14 #include <scsi/scsi_device.h>
15 #include <scsi/scsi_cmnd.h>
16 #include <scsi/scsi_tcq.h>
17 #include <scsi/scsi_dbg.h>
22 #define snic_cmd_tag(sc) (scsi_cmd_to_rq(sc)->tag)
24 const char *snic_state_str
[] = {
25 [SNIC_INIT
] = "SNIC_INIT",
26 [SNIC_ERROR
] = "SNIC_ERROR",
27 [SNIC_ONLINE
] = "SNIC_ONLINE",
28 [SNIC_OFFLINE
] = "SNIC_OFFLINE",
29 [SNIC_FWRESET
] = "SNIC_FWRESET",
32 static const char * const snic_req_state_str
[] = {
33 [SNIC_IOREQ_NOT_INITED
] = "SNIC_IOREQ_NOT_INITED",
34 [SNIC_IOREQ_PENDING
] = "SNIC_IOREQ_PENDING",
35 [SNIC_IOREQ_ABTS_PENDING
] = "SNIC_IOREQ_ABTS_PENDING",
36 [SNIC_IOREQ_ABTS_COMPLETE
] = "SNIC_IOREQ_ABTS_COMPLETE",
37 [SNIC_IOREQ_LR_PENDING
] = "SNIC_IOREQ_LR_PENDING",
38 [SNIC_IOREQ_LR_COMPLETE
] = "SNIC_IOREQ_LR_COMPLETE",
39 [SNIC_IOREQ_COMPLETE
] = "SNIC_IOREQ_CMD_COMPLETE",
42 /* snic cmd status strings */
43 static const char * const snic_io_status_str
[] = {
44 [SNIC_STAT_IO_SUCCESS
] = "SNIC_STAT_IO_SUCCESS", /* 0x0 */
45 [SNIC_STAT_INVALID_HDR
] = "SNIC_STAT_INVALID_HDR",
46 [SNIC_STAT_OUT_OF_RES
] = "SNIC_STAT_OUT_OF_RES",
47 [SNIC_STAT_INVALID_PARM
] = "SNIC_STAT_INVALID_PARM",
48 [SNIC_STAT_REQ_NOT_SUP
] = "SNIC_STAT_REQ_NOT_SUP",
49 [SNIC_STAT_IO_NOT_FOUND
] = "SNIC_STAT_IO_NOT_FOUND",
50 [SNIC_STAT_ABORTED
] = "SNIC_STAT_ABORTED",
51 [SNIC_STAT_TIMEOUT
] = "SNIC_STAT_TIMEOUT",
52 [SNIC_STAT_SGL_INVALID
] = "SNIC_STAT_SGL_INVALID",
53 [SNIC_STAT_DATA_CNT_MISMATCH
] = "SNIC_STAT_DATA_CNT_MISMATCH",
54 [SNIC_STAT_FW_ERR
] = "SNIC_STAT_FW_ERR",
55 [SNIC_STAT_ITMF_REJECT
] = "SNIC_STAT_ITMF_REJECT",
56 [SNIC_STAT_ITMF_FAIL
] = "SNIC_STAT_ITMF_FAIL",
57 [SNIC_STAT_ITMF_INCORRECT_LUN
] = "SNIC_STAT_ITMF_INCORRECT_LUN",
58 [SNIC_STAT_CMND_REJECT
] = "SNIC_STAT_CMND_REJECT",
59 [SNIC_STAT_DEV_OFFLINE
] = "SNIC_STAT_DEV_OFFLINE",
60 [SNIC_STAT_NO_BOOTLUN
] = "SNIC_STAT_NO_BOOTLUN",
61 [SNIC_STAT_SCSI_ERR
] = "SNIC_STAT_SCSI_ERR",
62 [SNIC_STAT_NOT_READY
] = "SNIC_STAT_NOT_READY",
63 [SNIC_STAT_FATAL_ERROR
] = "SNIC_STAT_FATAL_ERROR",
66 static void snic_scsi_cleanup(struct snic
*, int);
69 snic_state_to_str(unsigned int state
)
71 if (state
>= ARRAY_SIZE(snic_state_str
) || !snic_state_str
[state
])
74 return snic_state_str
[state
];
78 snic_io_status_to_str(unsigned int state
)
80 if ((state
>= ARRAY_SIZE(snic_io_status_str
)) ||
81 (!snic_io_status_str
[state
]))
84 return snic_io_status_str
[state
];
88 snic_ioreq_state_to_str(unsigned int state
)
90 if (state
>= ARRAY_SIZE(snic_req_state_str
) ||
91 !snic_req_state_str
[state
])
94 return snic_req_state_str
[state
];
97 static inline spinlock_t
*
98 snic_io_lock_hash(struct snic
*snic
, struct scsi_cmnd
*sc
)
100 u32 hash
= snic_cmd_tag(sc
) & (SNIC_IO_LOCKS
- 1);
102 return &snic
->io_req_lock
[hash
];
105 static inline spinlock_t
*
106 snic_io_lock_tag(struct snic
*snic
, int tag
)
108 return &snic
->io_req_lock
[tag
& (SNIC_IO_LOCKS
- 1)];
111 /* snic_release_req_buf : Releases snic_req_info */
113 snic_release_req_buf(struct snic
*snic
,
114 struct snic_req_info
*rqi
,
115 struct scsi_cmnd
*sc
)
117 struct snic_host_req
*req
= rqi_to_req(rqi
);
119 /* Freeing cmd without marking completion, not okay */
120 SNIC_BUG_ON(!((CMD_STATE(sc
) == SNIC_IOREQ_COMPLETE
) ||
121 (CMD_STATE(sc
) == SNIC_IOREQ_ABTS_COMPLETE
) ||
122 (CMD_FLAGS(sc
) & SNIC_DEV_RST_NOTSUP
) ||
123 (CMD_FLAGS(sc
) & SNIC_IO_INTERNAL_TERM_ISSUED
) ||
124 (CMD_FLAGS(sc
) & SNIC_DEV_RST_TERM_ISSUED
) ||
125 (CMD_FLAGS(sc
) & SNIC_SCSI_CLEANUP
) ||
126 (CMD_STATE(sc
) == SNIC_IOREQ_LR_COMPLETE
)));
128 SNIC_SCSI_DBG(snic
->shost
,
129 "Rel_req:sc %p:tag %x:rqi %p:ioreq %p:abt %p:dr %p: state %s:flags 0x%llx\n",
130 sc
, snic_cmd_tag(sc
), rqi
, rqi
->req
, rqi
->abort_req
,
131 rqi
->dr_req
, snic_ioreq_state_to_str(CMD_STATE(sc
)),
134 if (req
->u
.icmnd
.sense_addr
)
135 dma_unmap_single(&snic
->pdev
->dev
,
136 le64_to_cpu(req
->u
.icmnd
.sense_addr
),
137 SCSI_SENSE_BUFFERSIZE
,
142 snic_req_free(snic
, rqi
);
143 } /* end of snic_release_req_buf */
146 * snic_queue_icmnd_req : Queues snic_icmnd request
149 snic_queue_icmnd_req(struct snic
*snic
,
150 struct snic_req_info
*rqi
,
151 struct scsi_cmnd
*sc
,
154 struct scatterlist
*sg
;
155 struct snic_sg_desc
*sgd
;
163 flags
= SNIC_ICMND_ESGL
;
164 sgd
= (struct snic_sg_desc
*) req_to_sgl(rqi
->req
);
166 for_each_sg(scsi_sglist(sc
), sg
, sg_cnt
, i
) {
167 sgd
->addr
= cpu_to_le64(sg_dma_address(sg
));
168 sgd
->len
= cpu_to_le32(sg_dma_len(sg
));
174 pa
= dma_map_single(&snic
->pdev
->dev
,
176 SCSI_SENSE_BUFFERSIZE
,
178 if (dma_mapping_error(&snic
->pdev
->dev
, pa
)) {
179 SNIC_HOST_ERR(snic
->shost
,
180 "QIcmnd:PCI Map Failed for sns buf %p tag %x\n",
181 sc
->sense_buffer
, snic_cmd_tag(sc
));
187 int_to_scsilun(sc
->device
->lun
, &lun
);
188 if (sc
->sc_data_direction
== DMA_FROM_DEVICE
)
189 flags
|= SNIC_ICMND_RD
;
190 if (sc
->sc_data_direction
== DMA_TO_DEVICE
)
191 flags
|= SNIC_ICMND_WR
;
193 /* Initialize icmnd */
194 snic_icmnd_init(rqi
->req
,
196 snic
->config
.hid
, /* hid */
198 flags
, /* command flags */
205 (ulong
) req_to_sgl(rqi
->req
),
206 pa
, /* sense buffer pa */
207 SCSI_SENSE_BUFFERSIZE
);
209 atomic64_inc(&snic
->s_stats
.io
.active
);
210 ret
= snic_queue_wq_desc(snic
, rqi
->req
, rqi
->req_len
);
212 atomic64_dec(&snic
->s_stats
.io
.active
);
213 SNIC_HOST_ERR(snic
->shost
,
214 "QIcmnd: Queuing Icmnd Failed. ret = %d\n",
217 snic_stats_update_active_ios(&snic
->s_stats
);
220 } /* end of snic_queue_icmnd_req */
223 * snic_issue_scsi_req : Prepares IO request and Issues to FW.
226 snic_issue_scsi_req(struct snic
*snic
,
227 struct snic_tgt
*tgt
,
228 struct scsi_cmnd
*sc
)
230 struct snic_req_info
*rqi
= NULL
;
233 u32 tag
= snic_cmd_tag(sc
);
234 u64 cmd_trc
= 0, cmd_st_flags
= 0;
235 spinlock_t
*io_lock
= NULL
;
238 CMD_STATE(sc
) = SNIC_IOREQ_NOT_INITED
;
239 CMD_FLAGS(sc
) = SNIC_NO_FLAGS
;
240 sg_cnt
= scsi_dma_map(sc
);
242 SNIC_TRC((u16
)snic
->shost
->host_no
, tag
, (ulong
) sc
, 0,
243 sc
->cmnd
[0], sg_cnt
, CMD_STATE(sc
));
245 SNIC_HOST_ERR(snic
->shost
, "issue_sc:Failed to map SG List.\n");
251 rqi
= snic_req_init(snic
, sg_cnt
);
259 rqi
->tgt_id
= tgt
->id
;
262 CMD_STATE(sc
) = SNIC_IOREQ_PENDING
;
263 CMD_SP(sc
) = (char *) rqi
;
264 cmd_trc
= SNIC_TRC_CMD(sc
);
265 CMD_FLAGS(sc
) |= (SNIC_IO_INITIALIZED
| SNIC_IO_ISSUED
);
266 cmd_st_flags
= SNIC_TRC_CMD_STATE_FLAGS(sc
);
267 io_lock
= snic_io_lock_hash(snic
, sc
);
269 /* create wq desc and enqueue it */
270 ret
= snic_queue_icmnd_req(snic
, rqi
, sc
, sg_cnt
);
272 SNIC_HOST_ERR(snic
->shost
,
273 "issue_sc: icmnd qing Failed for sc %p, err %d\n",
276 spin_lock_irqsave(io_lock
, flags
);
277 rqi
= (struct snic_req_info
*) CMD_SP(sc
);
279 CMD_STATE(sc
) = SNIC_IOREQ_COMPLETE
;
280 CMD_FLAGS(sc
) &= ~SNIC_IO_ISSUED
; /* turn off the flag */
281 spin_unlock_irqrestore(io_lock
, flags
);
284 snic_release_req_buf(snic
, rqi
, sc
);
286 SNIC_TRC(snic
->shost
->host_no
, tag
, (ulong
) sc
, 0, 0, 0,
287 SNIC_TRC_CMD_STATE_FLAGS(sc
));
289 u32 io_sz
= scsi_bufflen(sc
) >> 9;
290 u32 qtime
= jiffies
- rqi
->start_time
;
291 struct snic_io_stats
*iostats
= &snic
->s_stats
.io
;
293 if (io_sz
> atomic64_read(&iostats
->max_io_sz
))
294 atomic64_set(&iostats
->max_io_sz
, io_sz
);
296 if (qtime
> atomic64_read(&iostats
->max_qtime
))
297 atomic64_set(&iostats
->max_qtime
, qtime
);
299 SNIC_SCSI_DBG(snic
->shost
,
300 "issue_sc:sc %p, tag %d queued to WQ.\n",
303 SNIC_TRC(snic
->shost
->host_no
, tag
, (ulong
) sc
, (ulong
) rqi
,
304 sg_cnt
, cmd_trc
, cmd_st_flags
);
310 } /* end of snic_issue_scsi_req */
315 * Routine to send a scsi cdb to LLD
316 * Called with host_lock held and interrupts disabled
319 snic_queuecommand(struct Scsi_Host
*shost
, struct scsi_cmnd
*sc
)
321 struct snic_tgt
*tgt
= NULL
;
322 struct snic
*snic
= shost_priv(shost
);
325 tgt
= starget_to_tgt(scsi_target(sc
->device
));
326 ret
= snic_tgt_chkready(tgt
);
328 SNIC_HOST_ERR(shost
, "Tgt %p id %d Not Ready.\n", tgt
, tgt
->id
);
329 atomic64_inc(&snic
->s_stats
.misc
.tgt_not_rdy
);
336 if (snic_get_state(snic
) != SNIC_ONLINE
) {
337 SNIC_HOST_ERR(shost
, "snic state is %s\n",
338 snic_state_str
[snic_get_state(snic
)]);
340 return SCSI_MLQUEUE_HOST_BUSY
;
342 atomic_inc(&snic
->ios_inflight
);
344 SNIC_SCSI_DBG(shost
, "sc %p Tag %d (sc %0x) lun %lld in snic_qcmd\n",
345 sc
, snic_cmd_tag(sc
), sc
->cmnd
[0], sc
->device
->lun
);
347 ret
= snic_issue_scsi_req(snic
, tgt
, sc
);
349 SNIC_HOST_ERR(shost
, "Failed to Q, Scsi Req w/ err %d.\n", ret
);
350 ret
= SCSI_MLQUEUE_HOST_BUSY
;
353 atomic_dec(&snic
->ios_inflight
);
356 } /* end of snic_queuecommand */
359 * snic_process_abts_pending_state:
360 * caller should hold IO lock
363 snic_proc_tmreq_pending_state(struct snic
*snic
,
364 struct scsi_cmnd
*sc
,
367 int state
= CMD_STATE(sc
);
369 if (state
== SNIC_IOREQ_ABTS_PENDING
)
370 CMD_FLAGS(sc
) |= SNIC_IO_ABTS_PENDING
;
371 else if (state
== SNIC_IOREQ_LR_PENDING
)
372 CMD_FLAGS(sc
) |= SNIC_DEV_RST_PENDING
;
376 switch (cmpl_status
) {
377 case SNIC_STAT_IO_SUCCESS
:
378 CMD_FLAGS(sc
) |= SNIC_IO_DONE
;
381 case SNIC_STAT_ABORTED
:
382 CMD_FLAGS(sc
) |= SNIC_IO_ABORTED
;
391 * snic_process_io_failed_state:
392 * Processes IO's error states
395 snic_process_io_failed_state(struct snic
*snic
,
396 struct snic_icmnd_cmpl
*icmnd_cmpl
,
397 struct scsi_cmnd
*sc
,
403 case SNIC_STAT_TIMEOUT
: /* Req was timedout */
404 atomic64_inc(&snic
->s_stats
.misc
.io_tmo
);
408 case SNIC_STAT_ABORTED
: /* Req was aborted */
409 atomic64_inc(&snic
->s_stats
.misc
.io_aborted
);
413 case SNIC_STAT_DATA_CNT_MISMATCH
:/* Recv/Sent more/less data than exp */
414 atomic64_inc(&snic
->s_stats
.misc
.data_cnt_mismat
);
415 scsi_set_resid(sc
, le32_to_cpu(icmnd_cmpl
->resid
));
419 case SNIC_STAT_OUT_OF_RES
: /* Out of resources to complete request */
420 atomic64_inc(&snic
->s_stats
.fw
.out_of_res
);
424 case SNIC_STAT_IO_NOT_FOUND
: /* Requested I/O was not found */
425 atomic64_inc(&snic
->s_stats
.io
.io_not_found
);
429 case SNIC_STAT_SGL_INVALID
: /* Req was aborted to due to sgl error*/
430 atomic64_inc(&snic
->s_stats
.misc
.sgl_inval
);
434 case SNIC_STAT_FW_ERR
: /* Req terminated due to FW Error */
435 atomic64_inc(&snic
->s_stats
.fw
.io_errs
);
439 case SNIC_STAT_SCSI_ERR
: /* FW hits SCSI Error */
440 atomic64_inc(&snic
->s_stats
.fw
.scsi_errs
);
443 case SNIC_STAT_NOT_READY
: /* XPT yet to initialize */
444 case SNIC_STAT_DEV_OFFLINE
: /* Device offline */
445 res
= DID_NO_CONNECT
;
448 case SNIC_STAT_INVALID_HDR
: /* Hdr contains invalid data */
449 case SNIC_STAT_INVALID_PARM
: /* Some param in req is invalid */
450 case SNIC_STAT_REQ_NOT_SUP
: /* Req type is not supported */
451 case SNIC_STAT_CMND_REJECT
: /* Req rejected */
452 case SNIC_STAT_FATAL_ERROR
: /* XPT Error */
454 SNIC_SCSI_DBG(snic
->shost
,
455 "Invalid Hdr/Param or Req Not Supported or Cmnd Rejected or Device Offline. or Unknown\n");
460 SNIC_HOST_ERR(snic
->shost
, "fw returns failed status %s flags 0x%llx\n",
461 snic_io_status_to_str(cmpl_stat
), CMD_FLAGS(sc
));
464 sc
->result
= (res
<< 16) | icmnd_cmpl
->scsi_status
;
465 } /* end of snic_process_io_failed_state */
468 * snic_tmreq_pending : is task management in progress.
471 snic_tmreq_pending(struct scsi_cmnd
*sc
)
473 int state
= CMD_STATE(sc
);
475 return ((state
== SNIC_IOREQ_ABTS_PENDING
) ||
476 (state
== SNIC_IOREQ_LR_PENDING
));
480 * snic_process_icmnd_cmpl_status:
481 * Caller should hold io_lock
484 snic_process_icmnd_cmpl_status(struct snic
*snic
,
485 struct snic_icmnd_cmpl
*icmnd_cmpl
,
487 struct scsi_cmnd
*sc
)
489 u8 scsi_stat
= icmnd_cmpl
->scsi_status
;
492 /* Mark the IO as complete */
493 CMD_STATE(sc
) = SNIC_IOREQ_COMPLETE
;
495 if (likely(cmpl_stat
== SNIC_STAT_IO_SUCCESS
)) {
496 sc
->result
= (DID_OK
<< 16) | scsi_stat
;
498 /* Update SCSI Cmd with resid value */
499 scsi_set_resid(sc
, le32_to_cpu(icmnd_cmpl
->resid
));
501 if (icmnd_cmpl
->flags
& SNIC_ICMND_CMPL_UNDR_RUN
)
502 atomic64_inc(&snic
->s_stats
.misc
.io_under_run
);
504 if (icmnd_cmpl
->scsi_status
== SAM_STAT_TASK_SET_FULL
)
505 atomic64_inc(&snic
->s_stats
.misc
.qfull
);
509 snic_process_io_failed_state(snic
, icmnd_cmpl
, sc
, cmpl_stat
);
510 atomic64_inc(&snic
->s_stats
.io
.fail
);
511 SNIC_HOST_ERR(snic
->shost
,
512 "icmnd_cmpl: IO Failed : Hdr Status %s flags 0x%llx\n",
513 snic_io_status_to_str(cmpl_stat
), CMD_FLAGS(sc
));
518 } /* end of snic_process_icmnd_cmpl_status */
522 * snic_icmnd_cmpl_handler
523 * Routine to handle icmnd completions
526 snic_icmnd_cmpl_handler(struct snic
*snic
, struct snic_fw_req
*fwreq
)
531 struct scsi_cmnd
*sc
= NULL
;
532 struct snic_icmnd_cmpl
*icmnd_cmpl
= NULL
;
533 struct snic_host_req
*req
= NULL
;
534 struct snic_req_info
*rqi
= NULL
;
535 unsigned long flags
, start_time
;
539 snic_io_hdr_dec(&fwreq
->hdr
, &typ
, &hdr_stat
, &cmnd_id
, &hid
, &ctx
);
540 icmnd_cmpl
= &fwreq
->u
.icmnd_cmpl
;
541 sc_stat
= icmnd_cmpl
->scsi_status
;
543 SNIC_SCSI_DBG(snic
->shost
,
544 "Icmnd_cmpl: type = %x, hdr_stat = %x, cmnd_id = %x, hid = %x,i ctx = %lx\n",
545 typ
, hdr_stat
, cmnd_id
, hid
, ctx
);
547 if (cmnd_id
>= snic
->max_tag_id
) {
548 SNIC_HOST_ERR(snic
->shost
,
549 "Icmnd_cmpl:Tag Error:Out of Range Tag %d, hdr status = %s\n",
550 cmnd_id
, snic_io_status_to_str(hdr_stat
));
554 sc
= scsi_host_find_tag(snic
->shost
, cmnd_id
);
558 atomic64_inc(&snic
->s_stats
.io
.sc_null
);
559 SNIC_HOST_ERR(snic
->shost
,
560 "Icmnd_cmpl: Scsi Cmnd Not found, sc = NULL Hdr Status = %s tag = 0x%x fwreq = 0x%p\n",
561 snic_io_status_to_str(hdr_stat
),
565 SNIC_TRC(snic
->shost
->host_no
, cmnd_id
, 0,
566 ((u64
)hdr_stat
<< 16 |
567 (u64
)sc_stat
<< 8 | (u64
)icmnd_cmpl
->flags
),
568 (ulong
) fwreq
, le32_to_cpu(icmnd_cmpl
->resid
), ctx
);
573 io_lock
= snic_io_lock_hash(snic
, sc
);
575 spin_lock_irqsave(io_lock
, flags
);
576 rqi
= (struct snic_req_info
*) CMD_SP(sc
);
577 SNIC_SCSI_DBG(snic
->shost
,
578 "Icmnd_cmpl:lun %lld sc %p cmd %xtag %d flags 0x%llx rqi %p\n",
579 sc
->device
->lun
, sc
, sc
->cmnd
[0], snic_cmd_tag(sc
),
582 if (CMD_FLAGS(sc
) & SNIC_HOST_RESET_CMD_TERM
) {
583 spin_unlock_irqrestore(io_lock
, flags
);
588 SNIC_BUG_ON(rqi
!= (struct snic_req_info
*)ctx
);
591 atomic64_inc(&snic
->s_stats
.io
.req_null
);
592 CMD_FLAGS(sc
) |= SNIC_IO_REQ_NULL
;
593 spin_unlock_irqrestore(io_lock
, flags
);
595 SNIC_HOST_ERR(snic
->shost
,
596 "Icmnd_cmpl:Host Req Not Found(null), Hdr Status %s, Tag 0x%x, sc 0x%p flags 0x%llx\n",
597 snic_io_status_to_str(hdr_stat
),
598 cmnd_id
, sc
, CMD_FLAGS(sc
));
602 rqi
= (struct snic_req_info
*) ctx
;
603 start_time
= rqi
->start_time
;
605 /* firmware completed the io */
609 * if SCSI-ML has already issued abort on this command,
610 * ignore completion of the IO. The abts path will clean it up
612 if (unlikely(snic_tmreq_pending(sc
))) {
613 snic_proc_tmreq_pending_state(snic
, sc
, hdr_stat
);
614 spin_unlock_irqrestore(io_lock
, flags
);
616 snic_stats_update_io_cmpl(&snic
->s_stats
);
618 /* Expected value is SNIC_STAT_ABORTED */
619 if (likely(hdr_stat
== SNIC_STAT_ABORTED
))
622 SNIC_SCSI_DBG(snic
->shost
,
623 "icmnd_cmpl:TM Req Pending(%s), Hdr Status %s sc 0x%p scsi status %x resid %d flags 0x%llx\n",
624 snic_ioreq_state_to_str(CMD_STATE(sc
)),
625 snic_io_status_to_str(hdr_stat
),
626 sc
, sc_stat
, le32_to_cpu(icmnd_cmpl
->resid
),
629 SNIC_TRC(snic
->shost
->host_no
, cmnd_id
, (ulong
) sc
,
630 jiffies_to_msecs(jiffies
- start_time
), (ulong
) fwreq
,
631 SNIC_TRC_CMD(sc
), SNIC_TRC_CMD_STATE_FLAGS(sc
));
636 if (snic_process_icmnd_cmpl_status(snic
, icmnd_cmpl
, hdr_stat
, sc
)) {
637 scsi_print_command(sc
);
638 SNIC_HOST_ERR(snic
->shost
,
639 "icmnd_cmpl:IO Failed, sc 0x%p Tag %d Cmd %x Hdr Status %s flags 0x%llx\n",
640 sc
, sc
->cmnd
[0], cmnd_id
,
641 snic_io_status_to_str(hdr_stat
), CMD_FLAGS(sc
));
644 /* Break link with the SCSI Command */
646 CMD_FLAGS(sc
) |= SNIC_IO_DONE
;
648 spin_unlock_irqrestore(io_lock
, flags
);
650 /* For now, consider only successful IO. */
651 snic_calc_io_process_time(snic
, rqi
);
653 snic_release_req_buf(snic
, rqi
, sc
);
655 SNIC_TRC(snic
->shost
->host_no
, cmnd_id
, (ulong
) sc
,
656 jiffies_to_msecs(jiffies
- start_time
), (ulong
) fwreq
,
657 SNIC_TRC_CMD(sc
), SNIC_TRC_CMD_STATE_FLAGS(sc
));
662 snic_stats_update_io_cmpl(&snic
->s_stats
);
663 } /* end of snic_icmnd_cmpl_handler */
666 snic_proc_dr_cmpl_locked(struct snic
*snic
,
667 struct snic_fw_req
*fwreq
,
670 struct scsi_cmnd
*sc
)
672 struct snic_req_info
*rqi
= (struct snic_req_info
*) CMD_SP(sc
);
673 u32 start_time
= rqi
->start_time
;
675 CMD_LR_STATUS(sc
) = cmpl_stat
;
677 SNIC_SCSI_DBG(snic
->shost
, "itmf_cmpl: Cmd State = %s\n",
678 snic_ioreq_state_to_str(CMD_STATE(sc
)));
680 if (CMD_STATE(sc
) == SNIC_IOREQ_ABTS_PENDING
) {
681 CMD_FLAGS(sc
) |= SNIC_DEV_RST_ABTS_PENDING
;
683 SNIC_TRC(snic
->shost
->host_no
, cmnd_id
, (ulong
) sc
,
684 jiffies_to_msecs(jiffies
- start_time
),
685 (ulong
) fwreq
, 0, SNIC_TRC_CMD_STATE_FLAGS(sc
));
687 SNIC_SCSI_DBG(snic
->shost
,
688 "itmf_cmpl: Terminate Pending Dev Reset Cmpl Recvd.id %x, status %s flags 0x%llx\n",
689 (int)(cmnd_id
& SNIC_TAG_MASK
),
690 snic_io_status_to_str(cmpl_stat
),
697 if (CMD_FLAGS(sc
) & SNIC_DEV_RST_TIMEDOUT
) {
698 SNIC_TRC(snic
->shost
->host_no
, cmnd_id
, (ulong
) sc
,
699 jiffies_to_msecs(jiffies
- start_time
),
700 (ulong
) fwreq
, 0, SNIC_TRC_CMD_STATE_FLAGS(sc
));
702 SNIC_SCSI_DBG(snic
->shost
,
703 "itmf_cmpl:Dev Reset Completion Received after timeout. id %d cmpl status %s flags 0x%llx\n",
704 (int)(cmnd_id
& SNIC_TAG_MASK
),
705 snic_io_status_to_str(cmpl_stat
),
711 CMD_STATE(sc
) = SNIC_IOREQ_LR_COMPLETE
;
712 CMD_FLAGS(sc
) |= SNIC_DEV_RST_DONE
;
714 SNIC_SCSI_DBG(snic
->shost
,
715 "itmf_cmpl:Dev Reset Cmpl Recvd id %d cmpl status %s flags 0x%llx\n",
716 (int)(cmnd_id
& SNIC_TAG_MASK
),
717 snic_io_status_to_str(cmpl_stat
),
721 complete(rqi
->dr_done
);
722 } /* end of snic_proc_dr_cmpl_locked */
725 * snic_update_abort_stats : Updates abort stats based on completion status.
728 snic_update_abort_stats(struct snic
*snic
, u8 cmpl_stat
)
730 struct snic_abort_stats
*abt_stats
= &snic
->s_stats
.abts
;
732 SNIC_SCSI_DBG(snic
->shost
, "Updating Abort stats.\n");
735 case SNIC_STAT_IO_SUCCESS
:
738 case SNIC_STAT_TIMEOUT
:
739 atomic64_inc(&abt_stats
->fw_tmo
);
742 case SNIC_STAT_IO_NOT_FOUND
:
743 atomic64_inc(&abt_stats
->io_not_found
);
747 atomic64_inc(&abt_stats
->fail
);
753 snic_process_itmf_cmpl(struct snic
*snic
,
754 struct snic_fw_req
*fwreq
,
757 struct scsi_cmnd
*sc
)
759 struct snic_req_info
*rqi
= NULL
;
761 spinlock_t
*io_lock
= NULL
;
766 io_lock
= snic_io_lock_hash(snic
, sc
);
767 spin_lock_irqsave(io_lock
, flags
);
768 if (CMD_FLAGS(sc
) & SNIC_HOST_RESET_CMD_TERM
) {
769 spin_unlock_irqrestore(io_lock
, flags
);
773 rqi
= (struct snic_req_info
*) CMD_SP(sc
);
777 atomic64_inc(&snic
->s_stats
.io
.req_null
);
778 spin_unlock_irqrestore(io_lock
, flags
);
779 CMD_FLAGS(sc
) |= SNIC_IO_ABTS_TERM_REQ_NULL
;
780 SNIC_HOST_ERR(snic
->shost
,
781 "itmf_cmpl: rqi is null,Hdr stat = %s Tag = 0x%x sc = 0x%p flags 0x%llx\n",
782 snic_io_status_to_str(cmpl_stat
), cmnd_id
, sc
,
788 /* Extract task management flags */
789 tm_tags
= cmnd_id
& ~(SNIC_TAG_MASK
);
791 start_time
= rqi
->start_time
;
792 cmnd_id
&= (SNIC_TAG_MASK
);
796 /* Abort only issued on cmd */
797 snic_update_abort_stats(snic
, cmpl_stat
);
799 if (CMD_STATE(sc
) != SNIC_IOREQ_ABTS_PENDING
) {
800 /* This is a late completion. Ignore it. */
802 spin_unlock_irqrestore(io_lock
, flags
);
806 CMD_STATE(sc
) = SNIC_IOREQ_ABTS_COMPLETE
;
807 CMD_ABTS_STATUS(sc
) = cmpl_stat
;
808 CMD_FLAGS(sc
) |= SNIC_IO_ABTS_TERM_DONE
;
810 SNIC_SCSI_DBG(snic
->shost
,
811 "itmf_cmpl:Abort Cmpl Recvd.Tag 0x%x Status %s flags 0x%llx\n",
813 snic_io_status_to_str(cmpl_stat
),
817 * If scsi_eh thread is blocked waiting for abts complete,
818 * signal completion to it. IO will be cleaned in the thread,
819 * else clean it in this context.
821 if (rqi
->abts_done
) {
822 complete(rqi
->abts_done
);
823 spin_unlock_irqrestore(io_lock
, flags
);
825 break; /* jump out */
829 sc
->result
= (DID_ERROR
<< 16);
830 SNIC_SCSI_DBG(snic
->shost
,
831 "itmf_cmpl: Completing IO. sc %p flags 0x%llx\n",
834 spin_unlock_irqrestore(io_lock
, flags
);
836 snic_release_req_buf(snic
, rqi
, sc
);
838 SNIC_TRC(snic
->shost
->host_no
, cmnd_id
, (ulong
) sc
,
839 jiffies_to_msecs(jiffies
- start_time
),
840 (ulong
) fwreq
, SNIC_TRC_CMD(sc
),
841 SNIC_TRC_CMD_STATE_FLAGS(sc
));
847 case SNIC_TAG_DEV_RST
:
848 case SNIC_TAG_DEV_RST
| SNIC_TAG_IOCTL_DEV_RST
:
849 snic_proc_dr_cmpl_locked(snic
, fwreq
, cmpl_stat
, cmnd_id
, sc
);
850 spin_unlock_irqrestore(io_lock
, flags
);
855 case SNIC_TAG_ABORT
| SNIC_TAG_DEV_RST
:
856 /* Abort and terminate completion of device reset req */
858 CMD_STATE(sc
) = SNIC_IOREQ_ABTS_COMPLETE
;
859 CMD_ABTS_STATUS(sc
) = cmpl_stat
;
860 CMD_FLAGS(sc
) |= SNIC_DEV_RST_DONE
;
862 SNIC_SCSI_DBG(snic
->shost
,
863 "itmf_cmpl:dev reset abts cmpl recvd. id %d status %s flags 0x%llx\n",
864 cmnd_id
, snic_io_status_to_str(cmpl_stat
),
868 complete(rqi
->abts_done
);
870 spin_unlock_irqrestore(io_lock
, flags
);
875 spin_unlock_irqrestore(io_lock
, flags
);
876 SNIC_HOST_ERR(snic
->shost
,
877 "itmf_cmpl: Unknown TM tag bit 0x%x\n", tm_tags
);
879 SNIC_HOST_ERR(snic
->shost
,
880 "itmf_cmpl:Unexpected itmf io stat %s Tag = 0x%x flags 0x%llx\n",
881 snic_ioreq_state_to_str(CMD_STATE(sc
)),
891 } /* end of snic_process_itmf_cmpl_status */
894 * snic_itmf_cmpl_handler.
895 * Routine to handle itmf completions.
898 snic_itmf_cmpl_handler(struct snic
*snic
, struct snic_fw_req
*fwreq
)
900 struct scsi_cmnd
*sc
= NULL
;
901 struct snic_req_info
*rqi
= NULL
;
902 struct snic_itmf_cmpl
*itmf_cmpl
= NULL
;
909 snic_io_hdr_dec(&fwreq
->hdr
, &typ
, &hdr_stat
, &cmnd_id
, &hid
, &ctx
);
910 SNIC_SCSI_DBG(snic
->shost
,
911 "Itmf_cmpl: %s: type = %x, hdr_stat = %x, cmnd_id = %x, hid = %x,ctx = %lx\n",
912 __func__
, typ
, hdr_stat
, cmnd_id
, hid
, ctx
);
914 itmf_cmpl
= &fwreq
->u
.itmf_cmpl
;
915 SNIC_SCSI_DBG(snic
->shost
,
916 "Itmf_cmpl: nterm %u , flags 0x%x\n",
917 le32_to_cpu(itmf_cmpl
->nterminated
), itmf_cmpl
->flags
);
919 /* spl case, dev reset issued through ioctl */
920 if (cmnd_id
& SNIC_TAG_IOCTL_DEV_RST
) {
921 rqi
= (struct snic_req_info
*) ctx
;
927 if ((cmnd_id
& SNIC_TAG_MASK
) >= snic
->max_tag_id
) {
928 SNIC_HOST_ERR(snic
->shost
,
929 "Itmf_cmpl: Tag 0x%x out of Range,HdrStat %s\n",
930 cmnd_id
, snic_io_status_to_str(hdr_stat
));
936 sc
= scsi_host_find_tag(snic
->shost
, cmnd_id
& SNIC_TAG_MASK
);
941 atomic64_inc(&snic
->s_stats
.io
.sc_null
);
942 SNIC_HOST_ERR(snic
->shost
,
943 "Itmf_cmpl: sc is NULL - Hdr Stat %s Tag 0x%x\n",
944 snic_io_status_to_str(hdr_stat
), cmnd_id
);
949 snic_process_itmf_cmpl(snic
, fwreq
, cmnd_id
, hdr_stat
, sc
);
950 } /* end of snic_itmf_cmpl_handler */
955 snic_hba_reset_scsi_cleanup(struct snic
*snic
, struct scsi_cmnd
*sc
)
957 struct snic_stats
*st
= &snic
->s_stats
;
958 long act_ios
= 0, act_fwreqs
= 0;
960 SNIC_SCSI_DBG(snic
->shost
, "HBA Reset scsi cleanup.\n");
961 snic_scsi_cleanup(snic
, snic_cmd_tag(sc
));
963 /* Update stats on pending IOs */
964 act_ios
= atomic64_read(&st
->io
.active
);
965 atomic64_add(act_ios
, &st
->io
.compl);
966 atomic64_sub(act_ios
, &st
->io
.active
);
968 act_fwreqs
= atomic64_read(&st
->fw
.actv_reqs
);
969 atomic64_sub(act_fwreqs
, &st
->fw
.actv_reqs
);
973 * snic_hba_reset_cmpl_handler :
976 * 1. Cleanup all the scsi cmds, release all snic specific cmds
977 * 2. Issue Report Targets in case of SAN targets
980 snic_hba_reset_cmpl_handler(struct snic
*snic
, struct snic_fw_req
*fwreq
)
987 struct scsi_cmnd
*sc
= NULL
;
988 struct snic_req_info
*rqi
= NULL
;
989 spinlock_t
*io_lock
= NULL
;
990 unsigned long flags
, gflags
;
993 snic_io_hdr_dec(&fwreq
->hdr
, &typ
, &hdr_stat
, &cmnd_id
, &hid
, &ctx
);
994 SNIC_HOST_INFO(snic
->shost
,
995 "reset_cmpl:Tag %d ctx %lx cmpl status %s HBA Reset Completion received.\n",
996 cmnd_id
, ctx
, snic_io_status_to_str(hdr_stat
));
998 SNIC_SCSI_DBG(snic
->shost
,
999 "reset_cmpl: type = %x, hdr_stat = %x, cmnd_id = %x, hid = %x, ctx = %lx\n",
1000 typ
, hdr_stat
, cmnd_id
, hid
, ctx
);
1002 /* spl case, host reset issued through ioctl */
1003 if (cmnd_id
== SCSI_NO_TAG
) {
1004 rqi
= (struct snic_req_info
*) ctx
;
1005 SNIC_HOST_INFO(snic
->shost
,
1006 "reset_cmpl:Tag %d ctx %lx cmpl stat %s\n",
1007 cmnd_id
, ctx
, snic_io_status_to_str(hdr_stat
));
1013 if (cmnd_id
>= snic
->max_tag_id
) {
1014 SNIC_HOST_ERR(snic
->shost
,
1015 "reset_cmpl: Tag 0x%x out of Range,HdrStat %s\n",
1016 cmnd_id
, snic_io_status_to_str(hdr_stat
));
1022 sc
= scsi_host_find_tag(snic
->shost
, cmnd_id
);
1025 atomic64_inc(&snic
->s_stats
.io
.sc_null
);
1026 SNIC_HOST_ERR(snic
->shost
,
1027 "reset_cmpl: sc is NULL - Hdr Stat %s Tag 0x%x\n",
1028 snic_io_status_to_str(hdr_stat
), cmnd_id
);
1034 SNIC_HOST_INFO(snic
->shost
,
1035 "reset_cmpl: sc %p rqi %p Tag %d flags 0x%llx\n",
1036 sc
, rqi
, cmnd_id
, CMD_FLAGS(sc
));
1038 io_lock
= snic_io_lock_hash(snic
, sc
);
1039 spin_lock_irqsave(io_lock
, flags
);
1041 if (!snic
->remove_wait
) {
1042 spin_unlock_irqrestore(io_lock
, flags
);
1043 SNIC_HOST_ERR(snic
->shost
,
1044 "reset_cmpl:host reset completed after timeout\n");
1050 rqi
= (struct snic_req_info
*) CMD_SP(sc
);
1054 atomic64_inc(&snic
->s_stats
.io
.req_null
);
1055 spin_unlock_irqrestore(io_lock
, flags
);
1056 CMD_FLAGS(sc
) |= SNIC_IO_ABTS_TERM_REQ_NULL
;
1057 SNIC_HOST_ERR(snic
->shost
,
1058 "reset_cmpl: rqi is null,Hdr stat %s Tag 0x%x sc 0x%p flags 0x%llx\n",
1059 snic_io_status_to_str(hdr_stat
), cmnd_id
, sc
,
1067 spin_unlock_irqrestore(io_lock
, flags
);
1070 snic_hba_reset_scsi_cleanup(snic
, sc
);
1072 SNIC_BUG_ON(snic_get_state(snic
) != SNIC_OFFLINE
&&
1073 snic_get_state(snic
) != SNIC_FWRESET
);
1075 /* Careful locking between snic_lock and io lock */
1076 spin_lock_irqsave(io_lock
, flags
);
1077 spin_lock_irqsave(&snic
->snic_lock
, gflags
);
1078 if (snic_get_state(snic
) == SNIC_FWRESET
)
1079 snic_set_state(snic
, SNIC_ONLINE
);
1080 spin_unlock_irqrestore(&snic
->snic_lock
, gflags
);
1082 if (snic
->remove_wait
)
1083 complete(snic
->remove_wait
);
1085 spin_unlock_irqrestore(io_lock
, flags
);
1086 atomic64_inc(&snic
->s_stats
.reset
.hba_reset_cmpl
);
1089 /* Rediscovery is for SAN */
1090 if (snic
->config
.xpt_type
== SNIC_DAS
)
1093 SNIC_SCSI_DBG(snic
->shost
, "reset_cmpl: Queuing discovery work.\n");
1094 queue_work(snic_glob
->event_q
, &snic
->disc_work
);
1100 snic_msg_ack_handler(struct snic
*snic
, struct snic_fw_req
*fwreq
)
1102 SNIC_HOST_INFO(snic
->shost
, "Message Ack Received.\n");
1104 SNIC_ASSERT_NOT_IMPL(1);
1108 snic_aen_handler(struct snic
*snic
, struct snic_fw_req
*fwreq
)
1113 struct snic_async_evnotify
*aen
= &fwreq
->u
.async_ev
;
1116 snic_io_hdr_dec(&fwreq
->hdr
, &typ
, &hdr_stat
, &cmnd_id
, &hid
, &ctx
);
1117 SNIC_SCSI_DBG(snic
->shost
,
1118 "aen: type = %x, hdr_stat = %x, cmnd_id = %x, hid = %x, ctx = %lx\n",
1119 typ
, hdr_stat
, cmnd_id
, hid
, ctx
);
1121 event_id
= le32_to_cpu(aen
->ev_id
);
1124 case SNIC_EV_TGT_OFFLINE
:
1125 SNIC_HOST_INFO(snic
->shost
, "aen:TGT_OFFLINE Event Recvd.\n");
1128 case SNIC_EV_TGT_ONLINE
:
1129 SNIC_HOST_INFO(snic
->shost
, "aen:TGT_ONLINE Event Recvd.\n");
1132 case SNIC_EV_LUN_OFFLINE
:
1133 SNIC_HOST_INFO(snic
->shost
, "aen:LUN_OFFLINE Event Recvd.\n");
1136 case SNIC_EV_LUN_ONLINE
:
1137 SNIC_HOST_INFO(snic
->shost
, "aen:LUN_ONLINE Event Recvd.\n");
1140 case SNIC_EV_CONF_CHG
:
1141 SNIC_HOST_INFO(snic
->shost
, "aen:Config Change Event Recvd.\n");
1144 case SNIC_EV_TGT_ADDED
:
1145 SNIC_HOST_INFO(snic
->shost
, "aen:TGT_ADD Event Recvd.\n");
1148 case SNIC_EV_TGT_DELTD
:
1149 SNIC_HOST_INFO(snic
->shost
, "aen:TGT_DEL Event Recvd.\n");
1152 case SNIC_EV_LUN_ADDED
:
1153 SNIC_HOST_INFO(snic
->shost
, "aen:LUN_ADD Event Recvd.\n");
1156 case SNIC_EV_LUN_DELTD
:
1157 SNIC_HOST_INFO(snic
->shost
, "aen:LUN_DEL Event Recvd.\n");
1160 case SNIC_EV_DISC_CMPL
:
1161 SNIC_HOST_INFO(snic
->shost
, "aen:DISC_CMPL Event Recvd.\n");
1165 SNIC_HOST_INFO(snic
->shost
, "aen:Unknown Event Recvd.\n");
1170 SNIC_ASSERT_NOT_IMPL(1);
1171 } /* end of snic_aen_handler */
1174 * snic_io_cmpl_handler
1175 * Routine to process CQ entries(IO Completions) posted by fw.
1178 snic_io_cmpl_handler(struct vnic_dev
*vdev
,
1179 unsigned int cq_idx
,
1180 struct snic_fw_req
*fwreq
)
1182 struct snic
*snic
= svnic_dev_priv(vdev
);
1183 u64 start
= jiffies
, cmpl_time
;
1185 snic_print_desc(__func__
, (char *)fwreq
, sizeof(*fwreq
));
1187 /* Update FW Stats */
1188 if ((fwreq
->hdr
.type
>= SNIC_RSP_REPORT_TGTS_CMPL
) &&
1189 (fwreq
->hdr
.type
<= SNIC_RSP_BOOT_LUNS_CMPL
))
1190 atomic64_dec(&snic
->s_stats
.fw
.actv_reqs
);
1192 SNIC_BUG_ON((fwreq
->hdr
.type
> SNIC_RSP_BOOT_LUNS_CMPL
) &&
1193 (fwreq
->hdr
.type
< SNIC_MSG_ASYNC_EVNOTIFY
));
1195 /* Check for snic subsys errors */
1196 switch (fwreq
->hdr
.status
) {
1197 case SNIC_STAT_NOT_READY
: /* XPT yet to initialize */
1198 SNIC_HOST_ERR(snic
->shost
,
1199 "sNIC SubSystem is NOT Ready.\n");
1202 case SNIC_STAT_FATAL_ERROR
: /* XPT Error */
1203 SNIC_HOST_ERR(snic
->shost
,
1204 "sNIC SubSystem in Unrecoverable State.\n");
1208 switch (fwreq
->hdr
.type
) {
1209 case SNIC_RSP_EXCH_VER_CMPL
:
1210 snic_io_exch_ver_cmpl_handler(snic
, fwreq
);
1213 case SNIC_RSP_REPORT_TGTS_CMPL
:
1214 snic_report_tgt_cmpl_handler(snic
, fwreq
);
1217 case SNIC_RSP_ICMND_CMPL
:
1218 snic_icmnd_cmpl_handler(snic
, fwreq
);
1221 case SNIC_RSP_ITMF_CMPL
:
1222 snic_itmf_cmpl_handler(snic
, fwreq
);
1225 case SNIC_RSP_HBA_RESET_CMPL
:
1226 snic_hba_reset_cmpl_handler(snic
, fwreq
);
1230 snic_msg_ack_handler(snic
, fwreq
);
1233 case SNIC_MSG_ASYNC_EVNOTIFY
:
1234 snic_aen_handler(snic
, fwreq
);
1239 SNIC_SCSI_DBG(snic
->shost
,
1240 "Unknown Firmware completion request type %d\n",
1246 cmpl_time
= jiffies
- start
;
1247 if (cmpl_time
> atomic64_read(&snic
->s_stats
.io
.max_cmpl_time
))
1248 atomic64_set(&snic
->s_stats
.io
.max_cmpl_time
, cmpl_time
);
1251 } /* end of snic_io_cmpl_handler */
1254 * snic_fwcq_cmpl_handler
1255 * Routine to process fwCQ
1256 * This CQ is independent, and not associated with wq/rq/wq_copy queues
1259 snic_fwcq_cmpl_handler(struct snic
*snic
, int io_cmpl_work
)
1261 unsigned int num_ent
= 0; /* number cq entries processed */
1262 unsigned int cq_idx
;
1263 unsigned int nent_per_cq
;
1264 struct snic_misc_stats
*misc_stats
= &snic
->s_stats
.misc
;
1266 for (cq_idx
= snic
->wq_count
; cq_idx
< snic
->cq_count
; cq_idx
++) {
1267 nent_per_cq
= vnic_cq_fw_service(&snic
->cq
[cq_idx
],
1268 snic_io_cmpl_handler
,
1270 num_ent
+= nent_per_cq
;
1272 if (nent_per_cq
> atomic64_read(&misc_stats
->max_cq_ents
))
1273 atomic64_set(&misc_stats
->max_cq_ents
, nent_per_cq
);
1277 } /* end of snic_fwcq_cmpl_handler */
1280 * snic_queue_itmf_req: Common API to queue Task Management requests.
1281 * Use rqi->tm_tag for passing special tags.
1282 * @req_id : aborted request's tag, -1 for lun reset.
1285 snic_queue_itmf_req(struct snic
*snic
,
1286 struct snic_host_req
*tmreq
,
1287 struct scsi_cmnd
*sc
,
1291 struct snic_req_info
*rqi
= req_to_rqi(tmreq
);
1292 struct scsi_lun lun
;
1293 int tm_tag
= snic_cmd_tag(sc
) | rqi
->tm_tag
;
1297 SNIC_BUG_ON(!rqi
->tm_tag
);
1299 /* fill in lun info */
1300 int_to_scsilun(sc
->device
->lun
, &lun
);
1302 /* Initialize snic_host_req: itmf */
1303 snic_itmf_init(tmreq
,
1308 req_id
, /* Command to be aborted. */
1314 * In case of multiple aborts on same cmd,
1315 * use try_wait_for_completion and completion_done() to check
1316 * whether it queues aborts even after completion of abort issued
1317 * prior.SNIC_BUG_ON(completion_done(&rqi->done));
1320 ret
= snic_queue_wq_desc(snic
, tmreq
, sizeof(*tmreq
));
1322 SNIC_HOST_ERR(snic
->shost
,
1323 "qitmf:Queuing ITMF(%d) Req sc %p, rqi %p, req_id %d tag %d Failed, ret = %d\n",
1324 tmf
, sc
, rqi
, req_id
, snic_cmd_tag(sc
), ret
);
1326 SNIC_SCSI_DBG(snic
->shost
,
1327 "qitmf:Queuing ITMF(%d) Req sc %p, rqi %p, req_id %d, tag %d (req_id)- Success.",
1328 tmf
, sc
, rqi
, req_id
, snic_cmd_tag(sc
));
1331 } /* end of snic_queue_itmf_req */
1334 snic_issue_tm_req(struct snic
*snic
,
1335 struct snic_req_info
*rqi
,
1336 struct scsi_cmnd
*sc
,
1339 struct snic_host_req
*tmreq
= NULL
;
1340 int req_id
= 0, tag
= snic_cmd_tag(sc
);
1343 if (snic_get_state(snic
) == SNIC_FWRESET
)
1346 atomic_inc(&snic
->ios_inflight
);
1348 SNIC_SCSI_DBG(snic
->shost
,
1349 "issu_tmreq: Task mgmt req %d. rqi %p w/ tag %x\n",
1353 if (tmf
== SNIC_ITMF_LUN_RESET
) {
1354 tmreq
= snic_dr_req_init(snic
, rqi
);
1355 req_id
= SCSI_NO_TAG
;
1357 tmreq
= snic_abort_req_init(snic
, rqi
);
1367 ret
= snic_queue_itmf_req(snic
, tmreq
, sc
, tmf
, req_id
);
1371 SNIC_HOST_ERR(snic
->shost
,
1372 "issu_tmreq: Queueing ITMF(%d) Req, sc %p rqi %p req_id %d tag %x fails err = %d\n",
1373 tmf
, sc
, rqi
, req_id
, tag
, ret
);
1375 SNIC_SCSI_DBG(snic
->shost
,
1376 "issu_tmreq: Queueing ITMF(%d) Req, sc %p, rqi %p, req_id %d tag %x - Success.\n",
1377 tmf
, sc
, rqi
, req_id
, tag
);
1380 atomic_dec(&snic
->ios_inflight
);
1386 * snic_queue_abort_req : Queues abort req to WQ
1389 snic_queue_abort_req(struct snic
*snic
,
1390 struct snic_req_info
*rqi
,
1391 struct scsi_cmnd
*sc
,
1394 SNIC_SCSI_DBG(snic
->shost
, "q_abtreq: sc %p, rqi %p, tag %x, tmf %d\n",
1395 sc
, rqi
, snic_cmd_tag(sc
), tmf
);
1397 /* Add special tag for abort */
1398 rqi
->tm_tag
|= SNIC_TAG_ABORT
;
1400 return snic_issue_tm_req(snic
, rqi
, sc
, tmf
);
1404 * snic_abort_finish : called by snic_abort_cmd on queuing abort successfully.
1407 snic_abort_finish(struct snic
*snic
, struct scsi_cmnd
*sc
)
1409 struct snic_req_info
*rqi
= NULL
;
1410 spinlock_t
*io_lock
= NULL
;
1411 unsigned long flags
;
1412 int ret
= 0, tag
= snic_cmd_tag(sc
);
1414 io_lock
= snic_io_lock_hash(snic
, sc
);
1415 spin_lock_irqsave(io_lock
, flags
);
1416 rqi
= (struct snic_req_info
*) CMD_SP(sc
);
1418 atomic64_inc(&snic
->s_stats
.io
.req_null
);
1419 CMD_FLAGS(sc
) |= SNIC_IO_ABTS_TERM_REQ_NULL
;
1421 SNIC_SCSI_DBG(snic
->shost
,
1422 "abt_fini:req info is null tag 0x%x, sc 0x%p flags 0x%llx\n",
1423 tag
, sc
, CMD_FLAGS(sc
));
1429 rqi
->abts_done
= NULL
;
1433 /* Check the abort status. */
1434 switch (CMD_ABTS_STATUS(sc
)) {
1435 case SNIC_INVALID_CODE
:
1436 /* Firmware didn't complete abort req, timedout */
1437 CMD_FLAGS(sc
) |= SNIC_IO_ABTS_TIMEDOUT
;
1438 atomic64_inc(&snic
->s_stats
.abts
.drv_tmo
);
1439 SNIC_SCSI_DBG(snic
->shost
,
1440 "abt_fini:sc %p Tag %x Driver Timeout.flags 0x%llx\n",
1441 sc
, snic_cmd_tag(sc
), CMD_FLAGS(sc
));
1442 /* do not release snic request in timedout case */
1447 case SNIC_STAT_IO_SUCCESS
:
1448 case SNIC_STAT_IO_NOT_FOUND
:
1451 * If abort path doesn't call scsi_done(),
1452 * the # IO timeouts == 2, will cause the LUN offline.
1453 * Call scsi_done to complete the IO.
1455 sc
->result
= (DID_ERROR
<< 16);
1460 /* Firmware completed abort with error */
1467 SNIC_HOST_INFO(snic
->shost
,
1468 "abt_fini: Tag %x, Cmpl Status %s flags 0x%llx\n",
1469 tag
, snic_io_status_to_str(CMD_ABTS_STATUS(sc
)),
1473 spin_unlock_irqrestore(io_lock
, flags
);
1475 snic_release_req_buf(snic
, rqi
, sc
);
1478 } /* end of snic_abort_finish */
1481 * snic_send_abort_and_wait : Issues Abort, and Waits
1484 snic_send_abort_and_wait(struct snic
*snic
, struct scsi_cmnd
*sc
)
1486 struct snic_req_info
*rqi
= NULL
;
1487 enum snic_ioreq_state sv_state
;
1488 struct snic_tgt
*tgt
= NULL
;
1489 spinlock_t
*io_lock
= NULL
;
1490 DECLARE_COMPLETION_ONSTACK(tm_done
);
1491 unsigned long flags
;
1492 int ret
= 0, tmf
= 0, tag
= snic_cmd_tag(sc
);
1494 tgt
= starget_to_tgt(scsi_target(sc
->device
));
1495 if ((snic_tgt_chkready(tgt
) != 0) && (tgt
->tdata
.typ
== SNIC_TGT_SAN
))
1496 tmf
= SNIC_ITMF_ABTS_TASK_TERM
;
1498 tmf
= SNIC_ITMF_ABTS_TASK
;
1502 io_lock
= snic_io_lock_hash(snic
, sc
);
1505 * Avoid a race between SCSI issuing the abort and the device
1506 * completing the command.
1508 * If the command is already completed by fw_cmpl code,
1509 * we just return SUCCESS from here. This means that the abort
1510 * succeeded. In the SCSI ML, since the timeout for command has
1511 * happend, the completion wont actually complete the command
1512 * and it will be considered as an aborted command
1514 * The CMD_SP will not be cleared except while holding io_lock
1516 spin_lock_irqsave(io_lock
, flags
);
1517 rqi
= (struct snic_req_info
*) CMD_SP(sc
);
1519 spin_unlock_irqrestore(io_lock
, flags
);
1521 SNIC_HOST_ERR(snic
->shost
,
1522 "abt_cmd: rqi is null. Tag %d flags 0x%llx\n",
1523 tag
, CMD_FLAGS(sc
));
1530 rqi
->abts_done
= &tm_done
;
1531 if (CMD_STATE(sc
) == SNIC_IOREQ_ABTS_PENDING
) {
1532 spin_unlock_irqrestore(io_lock
, flags
);
1537 SNIC_BUG_ON(!rqi
->abts_done
);
1539 /* Save Command State, should be restored on failed to Queue. */
1540 sv_state
= CMD_STATE(sc
);
1543 * Command is still pending, need to abort it
1544 * If the fw completes the command after this point,
1545 * the completion won't be done till mid-layer, since abot
1546 * has already started.
1548 CMD_STATE(sc
) = SNIC_IOREQ_ABTS_PENDING
;
1549 CMD_ABTS_STATUS(sc
) = SNIC_INVALID_CODE
;
1551 SNIC_SCSI_DBG(snic
->shost
, "send_abt_cmd: TAG 0x%x\n", tag
);
1553 spin_unlock_irqrestore(io_lock
, flags
);
1555 /* Now Queue the abort command to firmware */
1556 ret
= snic_queue_abort_req(snic
, rqi
, sc
, tmf
);
1558 atomic64_inc(&snic
->s_stats
.abts
.q_fail
);
1559 SNIC_HOST_ERR(snic
->shost
,
1560 "send_abt_cmd: IO w/ Tag 0x%x fail w/ err %d flags 0x%llx\n",
1561 tag
, ret
, CMD_FLAGS(sc
));
1563 spin_lock_irqsave(io_lock
, flags
);
1564 /* Restore Command's previous state */
1565 CMD_STATE(sc
) = sv_state
;
1566 rqi
= (struct snic_req_info
*) CMD_SP(sc
);
1568 rqi
->abts_done
= NULL
;
1569 spin_unlock_irqrestore(io_lock
, flags
);
1575 spin_lock_irqsave(io_lock
, flags
);
1576 if (tmf
== SNIC_ITMF_ABTS_TASK
) {
1577 CMD_FLAGS(sc
) |= SNIC_IO_ABTS_ISSUED
;
1578 atomic64_inc(&snic
->s_stats
.abts
.num
);
1581 CMD_FLAGS(sc
) |= SNIC_IO_TERM_ISSUED
;
1583 spin_unlock_irqrestore(io_lock
, flags
);
1585 SNIC_SCSI_DBG(snic
->shost
,
1586 "send_abt_cmd: sc %p Tag %x flags 0x%llx\n",
1587 sc
, tag
, CMD_FLAGS(sc
));
1594 * Queued an abort IO, wait for its completion.
1595 * Once the fw completes the abort command, it will
1596 * wakeup this thread.
1598 wait_for_completion_timeout(&tm_done
, SNIC_ABTS_TIMEOUT
);
1602 } /* end of snic_send_abort_and_wait */
1605 * This function is exported to SCSI for sending abort cmnds.
1606 * A SCSI IO is represent by snic_ioreq in the driver.
1607 * The snic_ioreq is linked to the SCSI Cmd, thus a link with the ULP'S IO
1610 snic_abort_cmd(struct scsi_cmnd
*sc
)
1612 struct snic
*snic
= shost_priv(sc
->device
->host
);
1613 int ret
= SUCCESS
, tag
= snic_cmd_tag(sc
);
1614 u32 start_time
= jiffies
;
1616 SNIC_SCSI_DBG(snic
->shost
, "abt_cmd:sc %p :0x%x :req = %p :tag = %d\n",
1617 sc
, sc
->cmnd
[0], scsi_cmd_to_rq(sc
), tag
);
1619 if (unlikely(snic_get_state(snic
) != SNIC_ONLINE
)) {
1620 SNIC_HOST_ERR(snic
->shost
,
1621 "abt_cmd: tag %x Parent Devs are not rdy\n",
1629 ret
= snic_send_abort_and_wait(snic
, sc
);
1633 ret
= snic_abort_finish(snic
, sc
);
1636 SNIC_TRC(snic
->shost
->host_no
, tag
, (ulong
) sc
,
1637 jiffies_to_msecs(jiffies
- start_time
), 0,
1638 SNIC_TRC_CMD(sc
), SNIC_TRC_CMD_STATE_FLAGS(sc
));
1640 SNIC_SCSI_DBG(snic
->shost
,
1641 "abts: Abort Req Status = %s\n",
1642 (ret
== SUCCESS
) ? "SUCCESS" :
1643 ((ret
== FAST_IO_FAIL
) ? "FAST_IO_FAIL" : "FAILED"));
1651 snic_is_abts_pending(struct snic
*snic
, struct scsi_cmnd
*lr_sc
)
1653 struct snic_req_info
*rqi
= NULL
;
1654 struct scsi_cmnd
*sc
= NULL
;
1655 struct scsi_device
*lr_sdev
= NULL
;
1656 spinlock_t
*io_lock
= NULL
;
1658 unsigned long flags
;
1661 lr_sdev
= lr_sc
->device
;
1663 /* walk through the tag map, an dcheck if IOs are still pending in fw*/
1664 for (tag
= 0; tag
< snic
->max_tag_id
; tag
++) {
1665 io_lock
= snic_io_lock_tag(snic
, tag
);
1667 spin_lock_irqsave(io_lock
, flags
);
1668 sc
= scsi_host_find_tag(snic
->shost
, tag
);
1670 if (!sc
|| (lr_sc
&& (sc
->device
!= lr_sdev
|| sc
== lr_sc
))) {
1671 spin_unlock_irqrestore(io_lock
, flags
);
1676 rqi
= (struct snic_req_info
*) CMD_SP(sc
);
1678 spin_unlock_irqrestore(io_lock
, flags
);
1684 * Found IO that is still pending w/ firmware and belongs to
1685 * the LUN that is under reset, if lr_sc != NULL
1687 SNIC_SCSI_DBG(snic
->shost
, "Found IO in %s on LUN\n",
1688 snic_ioreq_state_to_str(CMD_STATE(sc
)));
1690 if (CMD_STATE(sc
) == SNIC_IOREQ_ABTS_PENDING
) {
1691 spin_unlock_irqrestore(io_lock
, flags
);
1696 spin_unlock_irqrestore(io_lock
, flags
);
1700 } /* end of snic_is_abts_pending */
1703 snic_dr_clean_single_req(struct snic
*snic
,
1705 struct scsi_device
*lr_sdev
)
1707 struct snic_req_info
*rqi
= NULL
;
1708 struct snic_tgt
*tgt
= NULL
;
1709 struct scsi_cmnd
*sc
= NULL
;
1710 spinlock_t
*io_lock
= NULL
;
1711 u32 sv_state
= 0, tmf
= 0;
1712 DECLARE_COMPLETION_ONSTACK(tm_done
);
1713 unsigned long flags
;
1716 io_lock
= snic_io_lock_tag(snic
, tag
);
1717 spin_lock_irqsave(io_lock
, flags
);
1718 sc
= scsi_host_find_tag(snic
->shost
, tag
);
1720 /* Ignore Cmd that don't belong to Lun Reset device */
1721 if (!sc
|| sc
->device
!= lr_sdev
)
1724 rqi
= (struct snic_req_info
*) CMD_SP(sc
);
1730 if (CMD_STATE(sc
) == SNIC_IOREQ_ABTS_PENDING
)
1734 if ((CMD_FLAGS(sc
) & SNIC_DEVICE_RESET
) &&
1735 (!(CMD_FLAGS(sc
) & SNIC_DEV_RST_ISSUED
))) {
1737 SNIC_SCSI_DBG(snic
->shost
,
1738 "clean_single_req: devrst is not pending sc 0x%p\n",
1744 SNIC_SCSI_DBG(snic
->shost
,
1745 "clean_single_req: Found IO in %s on lun\n",
1746 snic_ioreq_state_to_str(CMD_STATE(sc
)));
1748 /* Save Command State */
1749 sv_state
= CMD_STATE(sc
);
1752 * Any pending IO issued prior to reset is expected to be
1753 * in abts pending state, if not we need to set SNIC_IOREQ_ABTS_PENDING
1754 * to indicate the IO is abort pending.
1755 * When IO is completed, the IO will be handed over and handled
1759 CMD_STATE(sc
) = SNIC_IOREQ_ABTS_PENDING
;
1760 SNIC_BUG_ON(rqi
->abts_done
);
1762 if (CMD_FLAGS(sc
) & SNIC_DEVICE_RESET
) {
1763 rqi
->tm_tag
= SNIC_TAG_DEV_RST
;
1765 SNIC_SCSI_DBG(snic
->shost
,
1766 "clean_single_req:devrst sc 0x%p\n", sc
);
1769 CMD_ABTS_STATUS(sc
) = SNIC_INVALID_CODE
;
1770 rqi
->abts_done
= &tm_done
;
1771 spin_unlock_irqrestore(io_lock
, flags
);
1773 tgt
= starget_to_tgt(scsi_target(sc
->device
));
1774 if ((snic_tgt_chkready(tgt
) != 0) && (tgt
->tdata
.typ
== SNIC_TGT_SAN
))
1775 tmf
= SNIC_ITMF_ABTS_TASK_TERM
;
1777 tmf
= SNIC_ITMF_ABTS_TASK
;
1779 /* Now queue the abort command to firmware */
1780 ret
= snic_queue_abort_req(snic
, rqi
, sc
, tmf
);
1782 SNIC_HOST_ERR(snic
->shost
,
1783 "clean_single_req_err:sc %p, tag %d abt failed. tm_tag %d flags 0x%llx\n",
1784 sc
, tag
, rqi
->tm_tag
, CMD_FLAGS(sc
));
1786 spin_lock_irqsave(io_lock
, flags
);
1787 rqi
= (struct snic_req_info
*) CMD_SP(sc
);
1789 rqi
->abts_done
= NULL
;
1791 /* Restore Command State */
1792 if (CMD_STATE(sc
) == SNIC_IOREQ_ABTS_PENDING
)
1793 CMD_STATE(sc
) = sv_state
;
1799 spin_lock_irqsave(io_lock
, flags
);
1800 if (CMD_FLAGS(sc
) & SNIC_DEVICE_RESET
)
1801 CMD_FLAGS(sc
) |= SNIC_DEV_RST_TERM_ISSUED
;
1803 CMD_FLAGS(sc
) |= SNIC_IO_INTERNAL_TERM_ISSUED
;
1804 spin_unlock_irqrestore(io_lock
, flags
);
1806 wait_for_completion_timeout(&tm_done
, SNIC_ABTS_TIMEOUT
);
1808 /* Recheck cmd state to check if it now aborted. */
1809 spin_lock_irqsave(io_lock
, flags
);
1810 rqi
= (struct snic_req_info
*) CMD_SP(sc
);
1812 CMD_FLAGS(sc
) |= SNIC_IO_ABTS_TERM_REQ_NULL
;
1815 rqi
->abts_done
= NULL
;
1817 /* if abort is still pending w/ fw, fail */
1818 if (CMD_ABTS_STATUS(sc
) == SNIC_INVALID_CODE
) {
1819 SNIC_HOST_ERR(snic
->shost
,
1820 "clean_single_req_err:sc %p tag %d abt still pending w/ fw, tm_tag %d flags 0x%llx\n",
1821 sc
, tag
, rqi
->tm_tag
, CMD_FLAGS(sc
));
1823 CMD_FLAGS(sc
) |= SNIC_IO_ABTS_TERM_DONE
;
1829 CMD_STATE(sc
) = SNIC_IOREQ_ABTS_COMPLETE
;
1831 spin_unlock_irqrestore(io_lock
, flags
);
1833 snic_release_req_buf(snic
, rqi
, sc
);
1835 sc
->result
= (DID_ERROR
<< 16);
1843 spin_unlock_irqrestore(io_lock
, flags
);
1846 } /* end of snic_dr_clean_single_req */
1849 snic_dr_clean_pending_req(struct snic
*snic
, struct scsi_cmnd
*lr_sc
)
1851 struct scsi_device
*lr_sdev
= lr_sc
->device
;
1855 for (tag
= 0; tag
< snic
->max_tag_id
; tag
++) {
1856 if (tag
== snic_cmd_tag(lr_sc
))
1859 ret
= snic_dr_clean_single_req(snic
, tag
, lr_sdev
);
1861 SNIC_HOST_ERR(snic
->shost
, "clean_err:tag = %d\n", tag
);
1866 schedule_timeout(msecs_to_jiffies(100));
1868 /* Walk through all the cmds and check abts status. */
1869 if (snic_is_abts_pending(snic
, lr_sc
))
1872 SNIC_SCSI_DBG(snic
->shost
, "clean_pending_req: Success.\n");
1877 SNIC_HOST_ERR(snic
->shost
,
1878 "Failed to Clean Pending IOs on %s device.\n",
1879 dev_name(&lr_sdev
->sdev_gendev
));
1883 } /* end of snic_dr_clean_pending_req */
1886 * snic_dr_finish : Called by snic_device_reset
1889 snic_dr_finish(struct snic
*snic
, struct scsi_cmnd
*sc
)
1891 struct snic_req_info
*rqi
= NULL
;
1892 spinlock_t
*io_lock
= NULL
;
1893 unsigned long flags
;
1897 io_lock
= snic_io_lock_hash(snic
, sc
);
1898 spin_lock_irqsave(io_lock
, flags
);
1899 rqi
= (struct snic_req_info
*) CMD_SP(sc
);
1901 spin_unlock_irqrestore(io_lock
, flags
);
1902 SNIC_SCSI_DBG(snic
->shost
,
1903 "dr_fini: rqi is null tag 0x%x sc 0x%p flags 0x%llx\n",
1904 snic_cmd_tag(sc
), sc
, CMD_FLAGS(sc
));
1910 rqi
->dr_done
= NULL
;
1912 lr_res
= CMD_LR_STATUS(sc
);
1915 case SNIC_INVALID_CODE
:
1917 SNIC_SCSI_DBG(snic
->shost
,
1918 "dr_fini: Tag %x Dev Reset Timedout. flags 0x%llx\n",
1919 snic_cmd_tag(sc
), CMD_FLAGS(sc
));
1921 CMD_FLAGS(sc
) |= SNIC_DEV_RST_TIMEDOUT
;
1926 case SNIC_STAT_IO_SUCCESS
:
1927 SNIC_SCSI_DBG(snic
->shost
,
1928 "dr_fini: Tag %x Dev Reset cmpl\n",
1934 SNIC_HOST_ERR(snic
->shost
,
1935 "dr_fini:Device Reset completed& failed.Tag = %x lr_status %s flags 0x%llx\n",
1937 snic_io_status_to_str(lr_res
), CMD_FLAGS(sc
));
1941 spin_unlock_irqrestore(io_lock
, flags
);
1944 * Cleanup any IOs on this LUN that have still not completed.
1945 * If any of these fail, then LUN Reset fails.
1946 * Cleanup cleans all commands on this LUN except
1947 * the lun reset command. If all cmds get cleaned, the LUN Reset
1951 ret
= snic_dr_clean_pending_req(snic
, sc
);
1953 spin_lock_irqsave(io_lock
, flags
);
1954 SNIC_SCSI_DBG(snic
->shost
,
1955 "dr_fini: Device Reset Failed since could not abort all IOs. Tag = %x.\n",
1957 rqi
= (struct snic_req_info
*) CMD_SP(sc
);
1961 /* Cleanup LUN Reset Command */
1962 spin_lock_irqsave(io_lock
, flags
);
1963 rqi
= (struct snic_req_info
*) CMD_SP(sc
);
1965 ret
= SUCCESS
; /* Completed Successfully */
1971 lockdep_assert_held(io_lock
);
1974 spin_unlock_irqrestore(io_lock
, flags
);
1977 snic_release_req_buf(snic
, rqi
, sc
);
1981 } /* end of snic_dr_finish */
1984 snic_queue_dr_req(struct snic
*snic
,
1985 struct snic_req_info
*rqi
,
1986 struct scsi_cmnd
*sc
)
1988 /* Add special tag for device reset */
1989 rqi
->tm_tag
|= SNIC_TAG_DEV_RST
;
1991 return snic_issue_tm_req(snic
, rqi
, sc
, SNIC_ITMF_LUN_RESET
);
1995 snic_send_dr_and_wait(struct snic
*snic
, struct scsi_cmnd
*sc
)
1997 struct snic_req_info
*rqi
= NULL
;
1998 enum snic_ioreq_state sv_state
;
1999 spinlock_t
*io_lock
= NULL
;
2000 unsigned long flags
;
2001 DECLARE_COMPLETION_ONSTACK(tm_done
);
2002 int ret
= FAILED
, tag
= snic_cmd_tag(sc
);
2004 io_lock
= snic_io_lock_hash(snic
, sc
);
2005 spin_lock_irqsave(io_lock
, flags
);
2006 CMD_FLAGS(sc
) |= SNIC_DEVICE_RESET
;
2007 rqi
= (struct snic_req_info
*) CMD_SP(sc
);
2009 SNIC_HOST_ERR(snic
->shost
,
2010 "send_dr: rqi is null, Tag 0x%x flags 0x%llx\n",
2011 tag
, CMD_FLAGS(sc
));
2012 spin_unlock_irqrestore(io_lock
, flags
);
2018 /* Save Command state to restore in case Queuing failed. */
2019 sv_state
= CMD_STATE(sc
);
2021 CMD_STATE(sc
) = SNIC_IOREQ_LR_PENDING
;
2022 CMD_LR_STATUS(sc
) = SNIC_INVALID_CODE
;
2024 SNIC_SCSI_DBG(snic
->shost
, "dr: TAG = %x\n", tag
);
2026 rqi
->dr_done
= &tm_done
;
2027 SNIC_BUG_ON(!rqi
->dr_done
);
2029 spin_unlock_irqrestore(io_lock
, flags
);
2031 * The Command state is changed to IOREQ_PENDING,
2032 * in this case, if the command is completed, the icmnd_cmpl will
2033 * mark the cmd as completed.
2034 * This logic still makes LUN Reset is inevitable.
2037 ret
= snic_queue_dr_req(snic
, rqi
, sc
);
2039 SNIC_HOST_ERR(snic
->shost
,
2040 "send_dr: IO w/ Tag 0x%x Failed err = %d. flags 0x%llx\n",
2041 tag
, ret
, CMD_FLAGS(sc
));
2043 spin_lock_irqsave(io_lock
, flags
);
2045 CMD_STATE(sc
) = sv_state
;
2046 rqi
= (struct snic_req_info
*) CMD_SP(sc
);
2048 rqi
->dr_done
= NULL
;
2049 /* rqi is freed in caller. */
2050 spin_unlock_irqrestore(io_lock
, flags
);
2056 spin_lock_irqsave(io_lock
, flags
);
2057 CMD_FLAGS(sc
) |= SNIC_DEV_RST_ISSUED
;
2058 spin_unlock_irqrestore(io_lock
, flags
);
2062 wait_for_completion_timeout(&tm_done
, SNIC_LUN_RESET_TIMEOUT
);
2069 * auxillary funciton to check lun reset op is supported or not
2070 * Not supported if returns 0
2073 snic_dev_reset_supported(struct scsi_device
*sdev
)
2075 struct snic_tgt
*tgt
= starget_to_tgt(scsi_target(sdev
));
2077 if (tgt
->tdata
.typ
== SNIC_TGT_DAS
)
2084 snic_unlink_and_release_req(struct snic
*snic
, struct scsi_cmnd
*sc
, int flag
)
2086 struct snic_req_info
*rqi
= NULL
;
2087 spinlock_t
*io_lock
= NULL
;
2088 unsigned long flags
;
2089 u32 start_time
= jiffies
;
2091 io_lock
= snic_io_lock_hash(snic
, sc
);
2092 spin_lock_irqsave(io_lock
, flags
);
2093 rqi
= (struct snic_req_info
*) CMD_SP(sc
);
2095 start_time
= rqi
->start_time
;
2099 CMD_FLAGS(sc
) |= flag
;
2100 spin_unlock_irqrestore(io_lock
, flags
);
2103 snic_release_req_buf(snic
, rqi
, sc
);
2105 SNIC_TRC(snic
->shost
->host_no
, snic_cmd_tag(sc
), (ulong
) sc
,
2106 jiffies_to_msecs(jiffies
- start_time
), (ulong
) rqi
,
2107 SNIC_TRC_CMD(sc
), SNIC_TRC_CMD_STATE_FLAGS(sc
));
2111 * SCSI Eh thread issues a LUN Reset when one or more commands on a LUN
2112 * fail to get aborted. It calls driver's eh_device_reset with a SCSI
2113 * command on the LUN.
2116 snic_device_reset(struct scsi_cmnd
*sc
)
2118 struct Scsi_Host
*shost
= sc
->device
->host
;
2119 struct snic
*snic
= shost_priv(shost
);
2120 struct snic_req_info
*rqi
= NULL
;
2121 int tag
= snic_cmd_tag(sc
);
2122 int start_time
= jiffies
;
2126 SNIC_SCSI_DBG(shost
, "dev_reset:sc %p :0x%x :req = %p :tag = %d\n",
2127 sc
, sc
->cmnd
[0], scsi_cmd_to_rq(sc
),
2129 dr_supp
= snic_dev_reset_supported(sc
->device
);
2131 /* device reset op is not supported */
2132 SNIC_HOST_INFO(shost
, "LUN Reset Op not supported.\n");
2133 snic_unlink_and_release_req(snic
, sc
, SNIC_DEV_RST_NOTSUP
);
2138 if (unlikely(snic_get_state(snic
) != SNIC_ONLINE
)) {
2139 snic_unlink_and_release_req(snic
, sc
, 0);
2140 SNIC_HOST_ERR(shost
, "Devrst: Parent Devs are not online.\n");
2145 /* There is no tag when lun reset is issue through ioctl. */
2146 if (unlikely(tag
<= SNIC_NO_TAG
)) {
2147 SNIC_HOST_INFO(snic
->shost
,
2148 "Devrst: LUN Reset Recvd thru IOCTL.\n");
2150 rqi
= snic_req_init(snic
, 0);
2154 memset(scsi_cmd_priv(sc
), 0,
2155 sizeof(struct snic_internal_io_state
));
2156 CMD_SP(sc
) = (char *)rqi
;
2157 CMD_FLAGS(sc
) = SNIC_NO_FLAGS
;
2159 /* Add special tag for dr coming from user spc */
2160 rqi
->tm_tag
= SNIC_TAG_IOCTL_DEV_RST
;
2164 ret
= snic_send_dr_and_wait(snic
, sc
);
2166 SNIC_HOST_ERR(snic
->shost
,
2167 "Devrst: IO w/ Tag %x Failed w/ err = %d\n",
2170 snic_unlink_and_release_req(snic
, sc
, 0);
2175 ret
= snic_dr_finish(snic
, sc
);
2178 SNIC_TRC(snic
->shost
->host_no
, tag
, (ulong
) sc
,
2179 jiffies_to_msecs(jiffies
- start_time
),
2180 0, SNIC_TRC_CMD(sc
), SNIC_TRC_CMD_STATE_FLAGS(sc
));
2182 SNIC_SCSI_DBG(snic
->shost
,
2183 "Devrst: Returning from Device Reset : %s\n",
2184 (ret
== SUCCESS
) ? "SUCCESS" : "FAILED");
2187 } /* end of snic_device_reset */
2190 * SCSI Error handling calls driver's eh_host_reset if all prior
2191 * error handling levels return FAILED.
2193 * Host Reset is the highest level of error recovery. If this fails, then
2194 * host is offlined by SCSI.
2197 * snic_issue_hba_reset : Queues FW Reset Request.
2200 snic_issue_hba_reset(struct snic
*snic
, struct scsi_cmnd
*sc
)
2202 struct snic_req_info
*rqi
= NULL
;
2203 struct snic_host_req
*req
= NULL
;
2204 spinlock_t
*io_lock
= NULL
;
2205 DECLARE_COMPLETION_ONSTACK(wait
);
2206 unsigned long flags
;
2209 rqi
= snic_req_init(snic
, 0);
2216 if (snic_cmd_tag(sc
) == SCSI_NO_TAG
) {
2217 memset(scsi_cmd_priv(sc
), 0,
2218 sizeof(struct snic_internal_io_state
));
2219 SNIC_HOST_INFO(snic
->shost
, "issu_hr:Host reset thru ioctl.\n");
2223 req
= rqi_to_req(rqi
);
2225 io_lock
= snic_io_lock_hash(snic
, sc
);
2226 spin_lock_irqsave(io_lock
, flags
);
2227 SNIC_BUG_ON(CMD_SP(sc
) != NULL
);
2228 CMD_STATE(sc
) = SNIC_IOREQ_PENDING
;
2229 CMD_SP(sc
) = (char *) rqi
;
2230 CMD_FLAGS(sc
) |= SNIC_IO_INITIALIZED
;
2231 snic
->remove_wait
= &wait
;
2232 spin_unlock_irqrestore(io_lock
, flags
);
2234 /* Initialize Request */
2235 snic_io_hdr_enc(&req
->hdr
, SNIC_REQ_HBA_RESET
, 0, snic_cmd_tag(sc
),
2236 snic
->config
.hid
, 0, (ulong
) rqi
);
2238 req
->u
.reset
.flags
= 0;
2240 ret
= snic_queue_wq_desc(snic
, req
, sizeof(*req
));
2242 SNIC_HOST_ERR(snic
->shost
,
2243 "issu_hr:Queuing HBA Reset Failed. w err %d\n",
2249 spin_lock_irqsave(io_lock
, flags
);
2250 CMD_FLAGS(sc
) |= SNIC_HOST_RESET_ISSUED
;
2251 spin_unlock_irqrestore(io_lock
, flags
);
2252 atomic64_inc(&snic
->s_stats
.reset
.hba_resets
);
2253 SNIC_HOST_INFO(snic
->shost
, "Queued HBA Reset Successfully.\n");
2255 wait_for_completion_timeout(snic
->remove_wait
,
2256 SNIC_HOST_RESET_TIMEOUT
);
2258 if (snic_get_state(snic
) == SNIC_FWRESET
) {
2259 SNIC_HOST_ERR(snic
->shost
, "reset_cmpl: Reset Timedout.\n");
2265 spin_lock_irqsave(io_lock
, flags
);
2266 snic
->remove_wait
= NULL
;
2267 rqi
= (struct snic_req_info
*) CMD_SP(sc
);
2269 spin_unlock_irqrestore(io_lock
, flags
);
2272 snic_req_free(snic
, rqi
);
2279 spin_lock_irqsave(io_lock
, flags
);
2280 snic
->remove_wait
= NULL
;
2281 rqi
= (struct snic_req_info
*) CMD_SP(sc
);
2283 spin_unlock_irqrestore(io_lock
, flags
);
2286 snic_req_free(snic
, rqi
);
2289 SNIC_HOST_ERR(snic
->shost
,
2290 "reset:HBA Reset Failed w/ err = %d.\n",
2294 } /* end of snic_issue_hba_reset */
2297 snic_reset(struct Scsi_Host
*shost
, struct scsi_cmnd
*sc
)
2299 struct snic
*snic
= shost_priv(shost
);
2300 enum snic_state sv_state
;
2301 unsigned long flags
;
2304 /* Set snic state as SNIC_FWRESET*/
2305 sv_state
= snic_get_state(snic
);
2307 spin_lock_irqsave(&snic
->snic_lock
, flags
);
2308 if (snic_get_state(snic
) == SNIC_FWRESET
) {
2309 spin_unlock_irqrestore(&snic
->snic_lock
, flags
);
2310 SNIC_HOST_INFO(shost
, "reset:prev reset is in progress\n");
2312 msleep(SNIC_HOST_RESET_TIMEOUT
);
2318 snic_set_state(snic
, SNIC_FWRESET
);
2319 spin_unlock_irqrestore(&snic
->snic_lock
, flags
);
2322 /* Wait for all the IOs that are entered in Qcmd */
2323 while (atomic_read(&snic
->ios_inflight
))
2324 schedule_timeout(msecs_to_jiffies(1));
2326 ret
= snic_issue_hba_reset(snic
, sc
);
2328 SNIC_HOST_ERR(shost
,
2329 "reset:Host Reset Failed w/ err %d.\n",
2331 spin_lock_irqsave(&snic
->snic_lock
, flags
);
2332 snic_set_state(snic
, sv_state
);
2333 spin_unlock_irqrestore(&snic
->snic_lock
, flags
);
2334 atomic64_inc(&snic
->s_stats
.reset
.hba_reset_fail
);
2344 } /* end of snic_reset */
2347 * SCSI Error handling calls driver's eh_host_reset if all prior
2348 * error handling levels return FAILED.
2350 * Host Reset is the highest level of error recovery. If this fails, then
2351 * host is offlined by SCSI.
2354 snic_host_reset(struct scsi_cmnd
*sc
)
2356 struct Scsi_Host
*shost
= sc
->device
->host
;
2357 u32 start_time
= jiffies
;
2360 SNIC_SCSI_DBG(shost
,
2361 "host reset:sc %p sc_cmd 0x%x req %p tag %d flags 0x%llx\n",
2362 sc
, sc
->cmnd
[0], scsi_cmd_to_rq(sc
),
2363 snic_cmd_tag(sc
), CMD_FLAGS(sc
));
2365 ret
= snic_reset(shost
, sc
);
2367 SNIC_TRC(shost
->host_no
, snic_cmd_tag(sc
), (ulong
) sc
,
2368 jiffies_to_msecs(jiffies
- start_time
),
2369 0, SNIC_TRC_CMD(sc
), SNIC_TRC_CMD_STATE_FLAGS(sc
));
2372 } /* end of snic_host_reset */
2375 * snic_cmpl_pending_tmreq : Caller should hold io_lock
2378 snic_cmpl_pending_tmreq(struct snic
*snic
, struct scsi_cmnd
*sc
)
2380 struct snic_req_info
*rqi
= NULL
;
2382 SNIC_SCSI_DBG(snic
->shost
,
2383 "Completing Pending TM Req sc %p, state %s flags 0x%llx\n",
2384 sc
, snic_io_status_to_str(CMD_STATE(sc
)), CMD_FLAGS(sc
));
2387 * CASE : FW didn't post itmf completion due to PCIe Errors.
2388 * Marking the abort status as Success to call scsi completion
2389 * in snic_abort_finish()
2391 CMD_ABTS_STATUS(sc
) = SNIC_STAT_IO_SUCCESS
;
2393 rqi
= (struct snic_req_info
*) CMD_SP(sc
);
2398 complete(rqi
->dr_done
);
2399 else if (rqi
->abts_done
)
2400 complete(rqi
->abts_done
);
2404 * snic_scsi_cleanup: Walks through tag map and releases the reqs
2407 snic_scsi_cleanup(struct snic
*snic
, int ex_tag
)
2409 struct snic_req_info
*rqi
= NULL
;
2410 struct scsi_cmnd
*sc
= NULL
;
2411 spinlock_t
*io_lock
= NULL
;
2412 unsigned long flags
;
2416 SNIC_SCSI_DBG(snic
->shost
, "sc_clean: scsi cleanup.\n");
2418 for (tag
= 0; tag
< snic
->max_tag_id
; tag
++) {
2423 io_lock
= snic_io_lock_tag(snic
, tag
);
2424 spin_lock_irqsave(io_lock
, flags
);
2425 sc
= scsi_host_find_tag(snic
->shost
, tag
);
2427 spin_unlock_irqrestore(io_lock
, flags
);
2432 if (unlikely(snic_tmreq_pending(sc
))) {
2434 * When FW Completes reset w/o sending completions
2435 * for outstanding ios.
2437 snic_cmpl_pending_tmreq(snic
, sc
);
2438 spin_unlock_irqrestore(io_lock
, flags
);
2443 rqi
= (struct snic_req_info
*) CMD_SP(sc
);
2445 spin_unlock_irqrestore(io_lock
, flags
);
2450 SNIC_SCSI_DBG(snic
->shost
,
2451 "sc_clean: sc %p, rqi %p, tag %d flags 0x%llx\n",
2452 sc
, rqi
, tag
, CMD_FLAGS(sc
));
2455 CMD_FLAGS(sc
) |= SNIC_SCSI_CLEANUP
;
2456 spin_unlock_irqrestore(io_lock
, flags
);
2457 st_time
= rqi
->start_time
;
2459 SNIC_HOST_INFO(snic
->shost
,
2460 "sc_clean: Releasing rqi %p : flags 0x%llx\n",
2461 rqi
, CMD_FLAGS(sc
));
2463 snic_release_req_buf(snic
, rqi
, sc
);
2466 sc
->result
= DID_TRANSPORT_DISRUPTED
<< 16;
2467 SNIC_HOST_INFO(snic
->shost
,
2468 "sc_clean: DID_TRANSPORT_DISRUPTED for sc %p, Tag %d flags 0x%llx rqi %p duration %u msecs\n",
2469 sc
, scsi_cmd_to_rq(sc
)->tag
, CMD_FLAGS(sc
), rqi
,
2470 jiffies_to_msecs(jiffies
- st_time
));
2472 /* Update IO stats */
2473 snic_stats_update_io_cmpl(&snic
->s_stats
);
2475 SNIC_TRC(snic
->shost
->host_no
, tag
, (ulong
) sc
,
2476 jiffies_to_msecs(jiffies
- st_time
), 0,
2478 SNIC_TRC_CMD_STATE_FLAGS(sc
));
2482 } /* end of snic_scsi_cleanup */
2485 snic_shutdown_scsi_cleanup(struct snic
*snic
)
2487 SNIC_HOST_INFO(snic
->shost
, "Shutdown time SCSI Cleanup.\n");
2489 snic_scsi_cleanup(snic
, SCSI_NO_TAG
);
2490 } /* end of snic_shutdown_scsi_cleanup */
2493 * snic_internal_abort_io
2494 * called by : snic_tgt_scsi_abort_io
2497 snic_internal_abort_io(struct snic
*snic
, struct scsi_cmnd
*sc
, int tmf
)
2499 struct snic_req_info
*rqi
= NULL
;
2500 spinlock_t
*io_lock
= NULL
;
2501 unsigned long flags
;
2505 io_lock
= snic_io_lock_hash(snic
, sc
);
2506 spin_lock_irqsave(io_lock
, flags
);
2507 rqi
= (struct snic_req_info
*) CMD_SP(sc
);
2509 goto skip_internal_abts
;
2511 if (CMD_STATE(sc
) == SNIC_IOREQ_ABTS_PENDING
)
2512 goto skip_internal_abts
;
2514 if ((CMD_FLAGS(sc
) & SNIC_DEVICE_RESET
) &&
2515 (!(CMD_FLAGS(sc
) & SNIC_DEV_RST_ISSUED
))) {
2517 SNIC_SCSI_DBG(snic
->shost
,
2518 "internal_abts: dev rst not pending sc 0x%p\n",
2521 goto skip_internal_abts
;
2525 if (!(CMD_FLAGS(sc
) & SNIC_IO_ISSUED
)) {
2526 SNIC_SCSI_DBG(snic
->shost
,
2527 "internal_abts: IO not yet issued sc 0x%p tag 0x%x flags 0x%llx state %d\n",
2528 sc
, snic_cmd_tag(sc
), CMD_FLAGS(sc
), CMD_STATE(sc
));
2530 goto skip_internal_abts
;
2533 sv_state
= CMD_STATE(sc
);
2534 CMD_STATE(sc
) = SNIC_IOREQ_ABTS_PENDING
;
2535 CMD_ABTS_STATUS(sc
) = SNIC_INVALID_CODE
;
2536 CMD_FLAGS(sc
) |= SNIC_IO_INTERNAL_TERM_PENDING
;
2538 if (CMD_FLAGS(sc
) & SNIC_DEVICE_RESET
) {
2540 rqi
->tm_tag
= SNIC_TAG_DEV_RST
;
2541 SNIC_SCSI_DBG(snic
->shost
, "internal_abts:dev rst sc %p\n", sc
);
2544 SNIC_SCSI_DBG(snic
->shost
, "internal_abts: Issuing abts tag %x\n",
2546 SNIC_BUG_ON(rqi
->abts_done
);
2547 spin_unlock_irqrestore(io_lock
, flags
);
2549 ret
= snic_queue_abort_req(snic
, rqi
, sc
, tmf
);
2551 SNIC_HOST_ERR(snic
->shost
,
2552 "internal_abts: Tag = %x , Failed w/ err = %d\n",
2553 snic_cmd_tag(sc
), ret
);
2555 spin_lock_irqsave(io_lock
, flags
);
2557 if (CMD_STATE(sc
) == SNIC_IOREQ_ABTS_PENDING
)
2558 CMD_STATE(sc
) = sv_state
;
2560 goto skip_internal_abts
;
2563 spin_lock_irqsave(io_lock
, flags
);
2564 if (CMD_FLAGS(sc
) & SNIC_DEVICE_RESET
)
2565 CMD_FLAGS(sc
) |= SNIC_DEV_RST_TERM_ISSUED
;
2567 CMD_FLAGS(sc
) |= SNIC_IO_INTERNAL_TERM_ISSUED
;
2572 lockdep_assert_held(io_lock
);
2573 spin_unlock_irqrestore(io_lock
, flags
);
2576 } /* end of snic_internal_abort_io */
2579 * snic_tgt_scsi_abort_io : called by snic_tgt_del
2582 snic_tgt_scsi_abort_io(struct snic_tgt
*tgt
)
2584 struct snic
*snic
= NULL
;
2585 struct scsi_cmnd
*sc
= NULL
;
2586 struct snic_tgt
*sc_tgt
= NULL
;
2587 spinlock_t
*io_lock
= NULL
;
2588 unsigned long flags
;
2589 int ret
= 0, tag
, abt_cnt
= 0, tmf
= 0;
2594 snic
= shost_priv(snic_tgt_to_shost(tgt
));
2595 SNIC_SCSI_DBG(snic
->shost
, "tgt_abt_io: Cleaning Pending IOs.\n");
2597 if (tgt
->tdata
.typ
== SNIC_TGT_DAS
)
2598 tmf
= SNIC_ITMF_ABTS_TASK
;
2600 tmf
= SNIC_ITMF_ABTS_TASK_TERM
;
2602 for (tag
= 0; tag
< snic
->max_tag_id
; tag
++) {
2603 io_lock
= snic_io_lock_tag(snic
, tag
);
2605 spin_lock_irqsave(io_lock
, flags
);
2606 sc
= scsi_host_find_tag(snic
->shost
, tag
);
2608 spin_unlock_irqrestore(io_lock
, flags
);
2613 sc_tgt
= starget_to_tgt(scsi_target(sc
->device
));
2614 if (sc_tgt
!= tgt
) {
2615 spin_unlock_irqrestore(io_lock
, flags
);
2619 spin_unlock_irqrestore(io_lock
, flags
);
2621 ret
= snic_internal_abort_io(snic
, sc
, tmf
);
2623 SNIC_HOST_ERR(snic
->shost
,
2624 "tgt_abt_io: Tag %x, Failed w err = %d\n",
2634 SNIC_SCSI_DBG(snic
->shost
, "tgt_abt_io: abt_cnt = %d\n", abt_cnt
);
2637 } /* end of snic_tgt_scsi_abort_io */