xref: /linux/drivers/scsi/snic/snic_scsi.c (revision 3652117f854819a148ff0fbe4492587d3520b5e5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright 2014 Cisco Systems, Inc.  All rights reserved.
3 
4 #include <linux/mempool.h>
5 #include <linux/errno.h>
6 #include <linux/init.h>
7 #include <linux/workqueue.h>
8 #include <linux/pci.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>
18 
19 #include "snic_io.h"
20 #include "snic.h"
21 
22 #define snic_cmd_tag(sc)	(scsi_cmd_to_rq(sc)->tag)
23 
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",
30 };
31 
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",
40 };
41 
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",
64 };
65 
66 static void snic_scsi_cleanup(struct snic *, int);
67 
68 const char *
69 snic_state_to_str(unsigned int state)
70 {
71 	if (state >= ARRAY_SIZE(snic_state_str) || !snic_state_str[state])
72 		return "Unknown";
73 
74 	return snic_state_str[state];
75 }
76 
77 static const char *
78 snic_io_status_to_str(unsigned int state)
79 {
80 	if ((state >= ARRAY_SIZE(snic_io_status_str)) ||
81 	     (!snic_io_status_str[state]))
82 		return "Unknown";
83 
84 	return snic_io_status_str[state];
85 }
86 
87 static const char *
88 snic_ioreq_state_to_str(unsigned int state)
89 {
90 	if (state >= ARRAY_SIZE(snic_req_state_str) ||
91 			!snic_req_state_str[state])
92 		return "Unknown";
93 
94 	return snic_req_state_str[state];
95 }
96 
97 static inline spinlock_t *
98 snic_io_lock_hash(struct snic *snic, struct scsi_cmnd *sc)
99 {
100 	u32 hash = snic_cmd_tag(sc) & (SNIC_IO_LOCKS - 1);
101 
102 	return &snic->io_req_lock[hash];
103 }
104 
105 static inline spinlock_t *
106 snic_io_lock_tag(struct snic *snic, int tag)
107 {
108 	return &snic->io_req_lock[tag & (SNIC_IO_LOCKS - 1)];
109 }
110 
111 /* snic_release_req_buf : Releases snic_req_info */
112 static void
113 snic_release_req_buf(struct snic *snic,
114 		   struct snic_req_info *rqi,
115 		   struct scsi_cmnd *sc)
116 {
117 	struct snic_host_req *req = rqi_to_req(rqi);
118 
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)));
127 
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)),
132 		      CMD_FLAGS(sc));
133 
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,
138 				 DMA_FROM_DEVICE);
139 
140 	scsi_dma_unmap(sc);
141 
142 	snic_req_free(snic, rqi);
143 } /* end of snic_release_req_buf */
144 
145 /*
146  * snic_queue_icmnd_req : Queues snic_icmnd request
147  */
148 static int
149 snic_queue_icmnd_req(struct snic *snic,
150 		     struct snic_req_info *rqi,
151 		     struct scsi_cmnd *sc,
152 		     int sg_cnt)
153 {
154 	struct scatterlist *sg;
155 	struct snic_sg_desc *sgd;
156 	dma_addr_t pa = 0;
157 	struct scsi_lun lun;
158 	u16 flags = 0;
159 	int ret = 0;
160 	unsigned int i;
161 
162 	if (sg_cnt) {
163 		flags = SNIC_ICMND_ESGL;
164 		sgd = (struct snic_sg_desc *) req_to_sgl(rqi->req);
165 
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));
169 			sgd->_resvd = 0;
170 			sgd++;
171 		}
172 	}
173 
174 	pa = dma_map_single(&snic->pdev->dev,
175 			    sc->sense_buffer,
176 			    SCSI_SENSE_BUFFERSIZE,
177 			    DMA_FROM_DEVICE);
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));
182 		ret = -ENOMEM;
183 
184 		return ret;
185 	}
186 
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;
192 
193 	/* Initialize icmnd */
194 	snic_icmnd_init(rqi->req,
195 			snic_cmd_tag(sc),
196 			snic->config.hid, /* hid */
197 			(ulong) rqi,
198 			flags, /* command flags */
199 			rqi->tgt_id,
200 			lun.scsi_lun,
201 			sc->cmnd,
202 			sc->cmd_len,
203 			scsi_bufflen(sc),
204 			sg_cnt,
205 			(ulong) req_to_sgl(rqi->req),
206 			pa, /* sense buffer pa */
207 			SCSI_SENSE_BUFFERSIZE);
208 
209 	atomic64_inc(&snic->s_stats.io.active);
210 	ret = snic_queue_wq_desc(snic, rqi->req, rqi->req_len);
211 	if (ret) {
212 		atomic64_dec(&snic->s_stats.io.active);
213 		SNIC_HOST_ERR(snic->shost,
214 			      "QIcmnd: Queuing Icmnd Failed. ret = %d\n",
215 			      ret);
216 	} else
217 		snic_stats_update_active_ios(&snic->s_stats);
218 
219 	return ret;
220 } /* end of snic_queue_icmnd_req */
221 
222 /*
223  * snic_issue_scsi_req : Prepares IO request and Issues to FW.
224  */
225 static int
226 snic_issue_scsi_req(struct snic *snic,
227 		      struct snic_tgt *tgt,
228 		      struct scsi_cmnd *sc)
229 {
230 	struct snic_req_info *rqi = NULL;
231 	int sg_cnt = 0;
232 	int ret = 0;
233 	u32 tag = snic_cmd_tag(sc);
234 	u64 cmd_trc = 0, cmd_st_flags = 0;
235 	spinlock_t *io_lock = NULL;
236 	unsigned long flags;
237 
238 	CMD_STATE(sc) = SNIC_IOREQ_NOT_INITED;
239 	CMD_FLAGS(sc) = SNIC_NO_FLAGS;
240 	sg_cnt = scsi_dma_map(sc);
241 	if (sg_cnt < 0) {
242 		SNIC_TRC((u16)snic->shost->host_no, tag, (ulong) sc, 0,
243 			 sc->cmnd[0], sg_cnt, CMD_STATE(sc));
244 
245 		SNIC_HOST_ERR(snic->shost, "issue_sc:Failed to map SG List.\n");
246 		ret = -ENOMEM;
247 
248 		goto issue_sc_end;
249 	}
250 
251 	rqi = snic_req_init(snic, sg_cnt);
252 	if (!rqi) {
253 		scsi_dma_unmap(sc);
254 		ret = -ENOMEM;
255 
256 		goto issue_sc_end;
257 	}
258 
259 	rqi->tgt_id = tgt->id;
260 	rqi->sc = sc;
261 
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);
268 
269 	/* create wq desc and enqueue it */
270 	ret = snic_queue_icmnd_req(snic, rqi, sc, sg_cnt);
271 	if (ret) {
272 		SNIC_HOST_ERR(snic->shost,
273 			      "issue_sc: icmnd qing Failed for sc %p, err %d\n",
274 			      sc, ret);
275 
276 		spin_lock_irqsave(io_lock, flags);
277 		rqi = (struct snic_req_info *) CMD_SP(sc);
278 		CMD_SP(sc) = NULL;
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);
282 
283 		if (rqi)
284 			snic_release_req_buf(snic, rqi, sc);
285 
286 		SNIC_TRC(snic->shost->host_no, tag, (ulong) sc, 0, 0, 0,
287 			 SNIC_TRC_CMD_STATE_FLAGS(sc));
288 	} else {
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;
292 
293 		if (io_sz > atomic64_read(&iostats->max_io_sz))
294 			atomic64_set(&iostats->max_io_sz, io_sz);
295 
296 		if (qtime > atomic64_read(&iostats->max_qtime))
297 			atomic64_set(&iostats->max_qtime, qtime);
298 
299 		SNIC_SCSI_DBG(snic->shost,
300 			      "issue_sc:sc %p, tag %d queued to WQ.\n",
301 			      sc, tag);
302 
303 		SNIC_TRC(snic->shost->host_no, tag, (ulong) sc, (ulong) rqi,
304 			 sg_cnt, cmd_trc, cmd_st_flags);
305 	}
306 
307 issue_sc_end:
308 
309 	return ret;
310 } /* end of snic_issue_scsi_req */
311 
312 
313 /*
314  * snic_queuecommand
315  * Routine to send a scsi cdb to LLD
316  * Called with host_lock held and interrupts disabled
317  */
318 int
319 snic_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *sc)
320 {
321 	struct snic_tgt *tgt = NULL;
322 	struct snic *snic = shost_priv(shost);
323 	int ret;
324 
325 	tgt = starget_to_tgt(scsi_target(sc->device));
326 	ret = snic_tgt_chkready(tgt);
327 	if (ret) {
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);
330 		sc->result = ret;
331 		scsi_done(sc);
332 
333 		return 0;
334 	}
335 
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)]);
339 
340 		return SCSI_MLQUEUE_HOST_BUSY;
341 	}
342 	atomic_inc(&snic->ios_inflight);
343 
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);
346 
347 	ret = snic_issue_scsi_req(snic, tgt, sc);
348 	if (ret) {
349 		SNIC_HOST_ERR(shost, "Failed to Q, Scsi Req w/ err %d.\n", ret);
350 		ret = SCSI_MLQUEUE_HOST_BUSY;
351 	}
352 
353 	atomic_dec(&snic->ios_inflight);
354 
355 	return ret;
356 } /* end of snic_queuecommand */
357 
358 /*
359  * snic_process_abts_pending_state:
360  * caller should hold IO lock
361  */
362 static void
363 snic_proc_tmreq_pending_state(struct snic *snic,
364 			      struct scsi_cmnd *sc,
365 			      u8 cmpl_status)
366 {
367 	int state = CMD_STATE(sc);
368 
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;
373 	else
374 		SNIC_BUG_ON(1);
375 
376 	switch (cmpl_status) {
377 	case SNIC_STAT_IO_SUCCESS:
378 		CMD_FLAGS(sc) |= SNIC_IO_DONE;
379 		break;
380 
381 	case SNIC_STAT_ABORTED:
382 		CMD_FLAGS(sc) |= SNIC_IO_ABORTED;
383 		break;
384 
385 	default:
386 		SNIC_BUG_ON(1);
387 	}
388 }
389 
390 /*
391  * snic_process_io_failed_state:
392  * Processes IO's error states
393  */
394 static void
395 snic_process_io_failed_state(struct snic *snic,
396 			     struct snic_icmnd_cmpl *icmnd_cmpl,
397 			     struct scsi_cmnd *sc,
398 			     u8 cmpl_stat)
399 {
400 	int res = 0;
401 
402 	switch (cmpl_stat) {
403 	case SNIC_STAT_TIMEOUT:		/* Req was timedout */
404 		atomic64_inc(&snic->s_stats.misc.io_tmo);
405 		res = DID_TIME_OUT;
406 		break;
407 
408 	case SNIC_STAT_ABORTED:		/* Req was aborted */
409 		atomic64_inc(&snic->s_stats.misc.io_aborted);
410 		res = DID_ABORT;
411 		break;
412 
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));
416 		res = DID_ERROR;
417 		break;
418 
419 	case SNIC_STAT_OUT_OF_RES: /* Out of resources to complete request */
420 		atomic64_inc(&snic->s_stats.fw.out_of_res);
421 		res = DID_REQUEUE;
422 		break;
423 
424 	case SNIC_STAT_IO_NOT_FOUND:	/* Requested I/O was not found */
425 		atomic64_inc(&snic->s_stats.io.io_not_found);
426 		res = DID_ERROR;
427 		break;
428 
429 	case SNIC_STAT_SGL_INVALID:	/* Req was aborted to due to sgl error*/
430 		atomic64_inc(&snic->s_stats.misc.sgl_inval);
431 		res = DID_ERROR;
432 		break;
433 
434 	case SNIC_STAT_FW_ERR:		/* Req terminated due to FW Error */
435 		atomic64_inc(&snic->s_stats.fw.io_errs);
436 		res = DID_ERROR;
437 		break;
438 
439 	case SNIC_STAT_SCSI_ERR:	/* FW hits SCSI Error */
440 		atomic64_inc(&snic->s_stats.fw.scsi_errs);
441 		break;
442 
443 	case SNIC_STAT_NOT_READY:	/* XPT yet to initialize */
444 	case SNIC_STAT_DEV_OFFLINE:	/* Device offline */
445 		res = DID_NO_CONNECT;
446 		break;
447 
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 */
453 	default:
454 		SNIC_SCSI_DBG(snic->shost,
455 			      "Invalid Hdr/Param or Req Not Supported or Cmnd Rejected or Device Offline. or Unknown\n");
456 		res = DID_ERROR;
457 		break;
458 	}
459 
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));
462 
463 	/* Set sc->result */
464 	sc->result = (res << 16) | icmnd_cmpl->scsi_status;
465 } /* end of snic_process_io_failed_state */
466 
467 /*
468  * snic_tmreq_pending : is task management in progress.
469  */
470 static int
471 snic_tmreq_pending(struct scsi_cmnd *sc)
472 {
473 	int state = CMD_STATE(sc);
474 
475 	return ((state == SNIC_IOREQ_ABTS_PENDING) ||
476 			(state == SNIC_IOREQ_LR_PENDING));
477 }
478 
479 /*
480  * snic_process_icmnd_cmpl_status:
481  * Caller should hold io_lock
482  */
483 static int
484 snic_process_icmnd_cmpl_status(struct snic *snic,
485 			       struct snic_icmnd_cmpl *icmnd_cmpl,
486 			       u8 cmpl_stat,
487 			       struct scsi_cmnd *sc)
488 {
489 	u8 scsi_stat = icmnd_cmpl->scsi_status;
490 	int ret = 0;
491 
492 	/* Mark the IO as complete */
493 	CMD_STATE(sc) = SNIC_IOREQ_COMPLETE;
494 
495 	if (likely(cmpl_stat == SNIC_STAT_IO_SUCCESS)) {
496 		sc->result = (DID_OK << 16) | scsi_stat;
497 
498 		/* Update SCSI Cmd with resid value */
499 		scsi_set_resid(sc, le32_to_cpu(icmnd_cmpl->resid));
500 
501 		if (icmnd_cmpl->flags & SNIC_ICMND_CMPL_UNDR_RUN)
502 			atomic64_inc(&snic->s_stats.misc.io_under_run);
503 
504 		if (icmnd_cmpl->scsi_status == SAM_STAT_TASK_SET_FULL)
505 			atomic64_inc(&snic->s_stats.misc.qfull);
506 
507 		ret = 0;
508 	} else {
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));
514 		ret = 1;
515 	}
516 
517 	return ret;
518 } /* end of snic_process_icmnd_cmpl_status */
519 
520 
521 /*
522  * snic_icmnd_cmpl_handler
523  * Routine to handle icmnd completions
524  */
525 static void
526 snic_icmnd_cmpl_handler(struct snic *snic, struct snic_fw_req *fwreq)
527 {
528 	u8 typ, hdr_stat;
529 	u32 cmnd_id, hid;
530 	ulong ctx;
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;
536 	spinlock_t *io_lock;
537 	u8 sc_stat = 0;
538 
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;
542 
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);
546 
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));
551 		return;
552 	}
553 
554 	sc = scsi_host_find_tag(snic->shost, cmnd_id);
555 	WARN_ON_ONCE(!sc);
556 
557 	if (!sc) {
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),
562 			      cmnd_id,
563 			      fwreq);
564 
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);
569 
570 		return;
571 	}
572 
573 	io_lock = snic_io_lock_hash(snic, sc);
574 
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),
580 		      CMD_FLAGS(sc), rqi);
581 
582 	if (CMD_FLAGS(sc) & SNIC_HOST_RESET_CMD_TERM) {
583 		spin_unlock_irqrestore(io_lock, flags);
584 
585 		return;
586 	}
587 
588 	SNIC_BUG_ON(rqi != (struct snic_req_info *)ctx);
589 	WARN_ON_ONCE(req);
590 	if (!rqi) {
591 		atomic64_inc(&snic->s_stats.io.req_null);
592 		CMD_FLAGS(sc) |= SNIC_IO_REQ_NULL;
593 		spin_unlock_irqrestore(io_lock, flags);
594 
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));
599 		return;
600 	}
601 
602 	rqi = (struct snic_req_info *) ctx;
603 	start_time = rqi->start_time;
604 
605 	/* firmware completed the io */
606 	rqi->io_cmpl = 1;
607 
608 	/*
609 	 * if SCSI-ML has already issued abort on this command,
610 	 * ignore completion of the IO. The abts path will clean it up
611 	 */
612 	if (unlikely(snic_tmreq_pending(sc))) {
613 		snic_proc_tmreq_pending_state(snic, sc, hdr_stat);
614 		spin_unlock_irqrestore(io_lock, flags);
615 
616 		snic_stats_update_io_cmpl(&snic->s_stats);
617 
618 		/* Expected value is SNIC_STAT_ABORTED */
619 		if (likely(hdr_stat == SNIC_STAT_ABORTED))
620 			return;
621 
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),
627 			      CMD_FLAGS(sc));
628 
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));
632 
633 		return;
634 	}
635 
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));
642 	}
643 
644 	/* Break link with the SCSI Command */
645 	CMD_SP(sc) = NULL;
646 	CMD_FLAGS(sc) |= SNIC_IO_DONE;
647 
648 	spin_unlock_irqrestore(io_lock, flags);
649 
650 	/* For now, consider only successful IO. */
651 	snic_calc_io_process_time(snic, rqi);
652 
653 	snic_release_req_buf(snic, rqi, sc);
654 
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));
658 
659 
660 	scsi_done(sc);
661 
662 	snic_stats_update_io_cmpl(&snic->s_stats);
663 } /* end of snic_icmnd_cmpl_handler */
664 
665 static void
666 snic_proc_dr_cmpl_locked(struct snic *snic,
667 			 struct snic_fw_req *fwreq,
668 			 u8 cmpl_stat,
669 			 u32 cmnd_id,
670 			 struct scsi_cmnd *sc)
671 {
672 	struct snic_req_info *rqi = (struct snic_req_info *) CMD_SP(sc);
673 	u32 start_time = rqi->start_time;
674 
675 	CMD_LR_STATUS(sc) = cmpl_stat;
676 
677 	SNIC_SCSI_DBG(snic->shost, "itmf_cmpl: Cmd State = %s\n",
678 		      snic_ioreq_state_to_str(CMD_STATE(sc)));
679 
680 	if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING) {
681 		CMD_FLAGS(sc) |= SNIC_DEV_RST_ABTS_PENDING;
682 
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));
686 
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),
691 			      CMD_FLAGS(sc));
692 
693 		return;
694 	}
695 
696 
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));
701 
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),
706 			      CMD_FLAGS(sc));
707 
708 		return;
709 	}
710 
711 	CMD_STATE(sc) = SNIC_IOREQ_LR_COMPLETE;
712 	CMD_FLAGS(sc) |= SNIC_DEV_RST_DONE;
713 
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),
718 		      CMD_FLAGS(sc));
719 
720 	if (rqi->dr_done)
721 		complete(rqi->dr_done);
722 } /* end of snic_proc_dr_cmpl_locked */
723 
724 /*
725  * snic_update_abort_stats : Updates abort stats based on completion status.
726  */
727 static void
728 snic_update_abort_stats(struct snic *snic, u8 cmpl_stat)
729 {
730 	struct snic_abort_stats *abt_stats = &snic->s_stats.abts;
731 
732 	SNIC_SCSI_DBG(snic->shost, "Updating Abort stats.\n");
733 
734 	switch (cmpl_stat) {
735 	case  SNIC_STAT_IO_SUCCESS:
736 		break;
737 
738 	case SNIC_STAT_TIMEOUT:
739 		atomic64_inc(&abt_stats->fw_tmo);
740 		break;
741 
742 	case SNIC_STAT_IO_NOT_FOUND:
743 		atomic64_inc(&abt_stats->io_not_found);
744 		break;
745 
746 	default:
747 		atomic64_inc(&abt_stats->fail);
748 		break;
749 	}
750 }
751 
752 static int
753 snic_process_itmf_cmpl(struct snic *snic,
754 		       struct snic_fw_req *fwreq,
755 		       u32 cmnd_id,
756 		       u8 cmpl_stat,
757 		       struct scsi_cmnd *sc)
758 {
759 	struct snic_req_info *rqi = NULL;
760 	u32 tm_tags = 0;
761 	spinlock_t *io_lock = NULL;
762 	unsigned long flags;
763 	u32 start_time = 0;
764 	int ret = 0;
765 
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);
770 
771 		return ret;
772 	}
773 	rqi = (struct snic_req_info *) CMD_SP(sc);
774 	WARN_ON_ONCE(!rqi);
775 
776 	if (!rqi) {
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,
783 			      CMD_FLAGS(sc));
784 
785 		return ret;
786 	}
787 
788 	/* Extract task management flags */
789 	tm_tags = cmnd_id & ~(SNIC_TAG_MASK);
790 
791 	start_time = rqi->start_time;
792 	cmnd_id &= (SNIC_TAG_MASK);
793 
794 	switch (tm_tags) {
795 	case SNIC_TAG_ABORT:
796 		/* Abort only issued on cmd */
797 		snic_update_abort_stats(snic, cmpl_stat);
798 
799 		if (CMD_STATE(sc) != SNIC_IOREQ_ABTS_PENDING) {
800 			/* This is a late completion. Ignore it. */
801 			ret = -1;
802 			spin_unlock_irqrestore(io_lock, flags);
803 			break;
804 		}
805 
806 		CMD_STATE(sc) = SNIC_IOREQ_ABTS_COMPLETE;
807 		CMD_ABTS_STATUS(sc) = cmpl_stat;
808 		CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_DONE;
809 
810 		SNIC_SCSI_DBG(snic->shost,
811 			      "itmf_cmpl:Abort Cmpl Recvd.Tag 0x%x Status %s flags 0x%llx\n",
812 			      cmnd_id,
813 			      snic_io_status_to_str(cmpl_stat),
814 			      CMD_FLAGS(sc));
815 
816 		/*
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.
820 		 */
821 		if (rqi->abts_done) {
822 			complete(rqi->abts_done);
823 			spin_unlock_irqrestore(io_lock, flags);
824 
825 			break; /* jump out */
826 		}
827 
828 		CMD_SP(sc) = NULL;
829 		sc->result = (DID_ERROR << 16);
830 		SNIC_SCSI_DBG(snic->shost,
831 			      "itmf_cmpl: Completing IO. sc %p flags 0x%llx\n",
832 			      sc, CMD_FLAGS(sc));
833 
834 		spin_unlock_irqrestore(io_lock, flags);
835 
836 		snic_release_req_buf(snic, rqi, sc);
837 
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));
842 
843 		scsi_done(sc);
844 
845 		break;
846 
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);
851 		ret = 0;
852 
853 		break;
854 
855 	case SNIC_TAG_ABORT | SNIC_TAG_DEV_RST:
856 		/* Abort and terminate completion of device reset req */
857 
858 		CMD_STATE(sc) = SNIC_IOREQ_ABTS_COMPLETE;
859 		CMD_ABTS_STATUS(sc) = cmpl_stat;
860 		CMD_FLAGS(sc) |= SNIC_DEV_RST_DONE;
861 
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),
865 			      CMD_FLAGS(sc));
866 
867 		if (rqi->abts_done)
868 			complete(rqi->abts_done);
869 
870 		spin_unlock_irqrestore(io_lock, flags);
871 
872 		break;
873 
874 	default:
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);
878 
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)),
882 			      cmnd_id,
883 			      CMD_FLAGS(sc));
884 		ret = -1;
885 		SNIC_BUG_ON(1);
886 
887 		break;
888 	}
889 
890 	return ret;
891 } /* end of snic_process_itmf_cmpl_status */
892 
893 /*
894  * snic_itmf_cmpl_handler.
895  * Routine to handle itmf completions.
896  */
897 static void
898 snic_itmf_cmpl_handler(struct snic *snic, struct snic_fw_req *fwreq)
899 {
900 	struct scsi_cmnd  *sc = NULL;
901 	struct snic_req_info *rqi = NULL;
902 	struct snic_itmf_cmpl *itmf_cmpl = NULL;
903 	ulong ctx;
904 	u32 cmnd_id;
905 	u32 hid;
906 	u8 typ;
907 	u8 hdr_stat;
908 
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);
913 
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);
918 
919 	/* spl case, dev reset issued through ioctl */
920 	if (cmnd_id & SNIC_TAG_IOCTL_DEV_RST) {
921 		rqi = (struct snic_req_info *) ctx;
922 		sc = rqi->sc;
923 
924 		goto ioctl_dev_rst;
925 	}
926 
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));
931 		SNIC_BUG_ON(1);
932 
933 		return;
934 	}
935 
936 	sc = scsi_host_find_tag(snic->shost, cmnd_id & SNIC_TAG_MASK);
937 	WARN_ON_ONCE(!sc);
938 
939 ioctl_dev_rst:
940 	if (!sc) {
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);
945 
946 		return;
947 	}
948 
949 	snic_process_itmf_cmpl(snic, fwreq, cmnd_id, hdr_stat, sc);
950 } /* end of snic_itmf_cmpl_handler */
951 
952 
953 
954 static void
955 snic_hba_reset_scsi_cleanup(struct snic *snic, struct scsi_cmnd *sc)
956 {
957 	struct snic_stats *st = &snic->s_stats;
958 	long act_ios = 0, act_fwreqs = 0;
959 
960 	SNIC_SCSI_DBG(snic->shost, "HBA Reset scsi cleanup.\n");
961 	snic_scsi_cleanup(snic, snic_cmd_tag(sc));
962 
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);
967 
968 	act_fwreqs = atomic64_read(&st->fw.actv_reqs);
969 	atomic64_sub(act_fwreqs, &st->fw.actv_reqs);
970 }
971 
972 /*
973  * snic_hba_reset_cmpl_handler :
974  *
975  * Notes :
976  * 1. Cleanup all the scsi cmds, release all snic specific cmds
977  * 2. Issue Report Targets in case of SAN targets
978  */
979 static int
980 snic_hba_reset_cmpl_handler(struct snic *snic, struct snic_fw_req *fwreq)
981 {
982 	ulong ctx;
983 	u32 cmnd_id;
984 	u32 hid;
985 	u8 typ;
986 	u8 hdr_stat;
987 	struct scsi_cmnd *sc = NULL;
988 	struct snic_req_info *rqi = NULL;
989 	spinlock_t *io_lock = NULL;
990 	unsigned long flags, gflags;
991 	int ret = 0;
992 
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));
997 
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);
1001 
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));
1008 		sc = rqi->sc;
1009 
1010 		goto ioctl_hba_rst;
1011 	}
1012 
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));
1017 		SNIC_BUG_ON(1);
1018 
1019 		return 1;
1020 	}
1021 
1022 	sc = scsi_host_find_tag(snic->shost, cmnd_id);
1023 ioctl_hba_rst:
1024 	if (!sc) {
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);
1029 		ret = 1;
1030 
1031 		return ret;
1032 	}
1033 
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));
1037 
1038 	io_lock = snic_io_lock_hash(snic, sc);
1039 	spin_lock_irqsave(io_lock, flags);
1040 
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");
1045 		ret = 1;
1046 
1047 		return ret;
1048 	}
1049 
1050 	rqi = (struct snic_req_info *) CMD_SP(sc);
1051 	WARN_ON_ONCE(!rqi);
1052 
1053 	if (!rqi) {
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,
1060 			      CMD_FLAGS(sc));
1061 
1062 		ret = 1;
1063 
1064 		return ret;
1065 	}
1066 	/* stats */
1067 	spin_unlock_irqrestore(io_lock, flags);
1068 
1069 	/* scsi cleanup */
1070 	snic_hba_reset_scsi_cleanup(snic, sc);
1071 
1072 	SNIC_BUG_ON(snic_get_state(snic) != SNIC_OFFLINE &&
1073 		    snic_get_state(snic) != SNIC_FWRESET);
1074 
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);
1081 
1082 	if (snic->remove_wait)
1083 		complete(snic->remove_wait);
1084 
1085 	spin_unlock_irqrestore(io_lock, flags);
1086 	atomic64_inc(&snic->s_stats.reset.hba_reset_cmpl);
1087 
1088 	ret = 0;
1089 	/* Rediscovery is for SAN */
1090 	if (snic->config.xpt_type == SNIC_DAS)
1091 			return ret;
1092 
1093 	SNIC_SCSI_DBG(snic->shost, "reset_cmpl: Queuing discovery work.\n");
1094 	queue_work(snic_glob->event_q, &snic->disc_work);
1095 
1096 	return ret;
1097 }
1098 
1099 static void
1100 snic_msg_ack_handler(struct snic *snic, struct snic_fw_req *fwreq)
1101 {
1102 	SNIC_HOST_INFO(snic->shost, "Message Ack Received.\n");
1103 
1104 	SNIC_ASSERT_NOT_IMPL(1);
1105 }
1106 
1107 static void
1108 snic_aen_handler(struct snic *snic, struct snic_fw_req *fwreq)
1109 {
1110 	u8 typ, hdr_stat;
1111 	u32 cmnd_id, hid;
1112 	ulong ctx;
1113 	struct snic_async_evnotify *aen = &fwreq->u.async_ev;
1114 	u32 event_id = 0;
1115 
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);
1120 
1121 	event_id = le32_to_cpu(aen->ev_id);
1122 
1123 	switch (event_id) {
1124 	case SNIC_EV_TGT_OFFLINE:
1125 		SNIC_HOST_INFO(snic->shost, "aen:TGT_OFFLINE Event Recvd.\n");
1126 		break;
1127 
1128 	case SNIC_EV_TGT_ONLINE:
1129 		SNIC_HOST_INFO(snic->shost, "aen:TGT_ONLINE Event Recvd.\n");
1130 		break;
1131 
1132 	case SNIC_EV_LUN_OFFLINE:
1133 		SNIC_HOST_INFO(snic->shost, "aen:LUN_OFFLINE Event Recvd.\n");
1134 		break;
1135 
1136 	case SNIC_EV_LUN_ONLINE:
1137 		SNIC_HOST_INFO(snic->shost, "aen:LUN_ONLINE Event Recvd.\n");
1138 		break;
1139 
1140 	case SNIC_EV_CONF_CHG:
1141 		SNIC_HOST_INFO(snic->shost, "aen:Config Change Event Recvd.\n");
1142 		break;
1143 
1144 	case SNIC_EV_TGT_ADDED:
1145 		SNIC_HOST_INFO(snic->shost, "aen:TGT_ADD Event Recvd.\n");
1146 		break;
1147 
1148 	case SNIC_EV_TGT_DELTD:
1149 		SNIC_HOST_INFO(snic->shost, "aen:TGT_DEL Event Recvd.\n");
1150 		break;
1151 
1152 	case SNIC_EV_LUN_ADDED:
1153 		SNIC_HOST_INFO(snic->shost, "aen:LUN_ADD Event Recvd.\n");
1154 		break;
1155 
1156 	case SNIC_EV_LUN_DELTD:
1157 		SNIC_HOST_INFO(snic->shost, "aen:LUN_DEL Event Recvd.\n");
1158 		break;
1159 
1160 	case SNIC_EV_DISC_CMPL:
1161 		SNIC_HOST_INFO(snic->shost, "aen:DISC_CMPL Event Recvd.\n");
1162 		break;
1163 
1164 	default:
1165 		SNIC_HOST_INFO(snic->shost, "aen:Unknown Event Recvd.\n");
1166 		SNIC_BUG_ON(1);
1167 		break;
1168 	}
1169 
1170 	SNIC_ASSERT_NOT_IMPL(1);
1171 } /* end of snic_aen_handler */
1172 
1173 /*
1174  * snic_io_cmpl_handler
1175  * Routine to process CQ entries(IO Completions) posted by fw.
1176  */
1177 static int
1178 snic_io_cmpl_handler(struct vnic_dev *vdev,
1179 		     unsigned int cq_idx,
1180 		     struct snic_fw_req *fwreq)
1181 {
1182 	struct snic *snic = svnic_dev_priv(vdev);
1183 	u64 start = jiffies, cmpl_time;
1184 
1185 	snic_print_desc(__func__, (char *)fwreq, sizeof(*fwreq));
1186 
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);
1191 
1192 	SNIC_BUG_ON((fwreq->hdr.type > SNIC_RSP_BOOT_LUNS_CMPL) &&
1193 		    (fwreq->hdr.type < SNIC_MSG_ASYNC_EVNOTIFY));
1194 
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");
1200 		break;
1201 
1202 	case SNIC_STAT_FATAL_ERROR:	/* XPT Error */
1203 		SNIC_HOST_ERR(snic->shost,
1204 			      "sNIC SubSystem in Unrecoverable State.\n");
1205 		break;
1206 	}
1207 
1208 	switch (fwreq->hdr.type) {
1209 	case SNIC_RSP_EXCH_VER_CMPL:
1210 		snic_io_exch_ver_cmpl_handler(snic, fwreq);
1211 		break;
1212 
1213 	case SNIC_RSP_REPORT_TGTS_CMPL:
1214 		snic_report_tgt_cmpl_handler(snic, fwreq);
1215 		break;
1216 
1217 	case SNIC_RSP_ICMND_CMPL:
1218 		snic_icmnd_cmpl_handler(snic, fwreq);
1219 		break;
1220 
1221 	case SNIC_RSP_ITMF_CMPL:
1222 		snic_itmf_cmpl_handler(snic, fwreq);
1223 		break;
1224 
1225 	case SNIC_RSP_HBA_RESET_CMPL:
1226 		snic_hba_reset_cmpl_handler(snic, fwreq);
1227 		break;
1228 
1229 	case SNIC_MSG_ACK:
1230 		snic_msg_ack_handler(snic, fwreq);
1231 		break;
1232 
1233 	case SNIC_MSG_ASYNC_EVNOTIFY:
1234 		snic_aen_handler(snic, fwreq);
1235 		break;
1236 
1237 	default:
1238 		SNIC_BUG_ON(1);
1239 		SNIC_SCSI_DBG(snic->shost,
1240 			      "Unknown Firmware completion request type %d\n",
1241 			      fwreq->hdr.type);
1242 		break;
1243 	}
1244 
1245 	/* Update Stats */
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);
1249 
1250 	return 0;
1251 } /* end of snic_io_cmpl_handler */
1252 
1253 /*
1254  * snic_fwcq_cmpl_handler
1255  * Routine to process fwCQ
1256  * This CQ is independent, and not associated with wq/rq/wq_copy queues
1257  */
1258 int
1259 snic_fwcq_cmpl_handler(struct snic *snic, int io_cmpl_work)
1260 {
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;
1265 
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,
1269 						 io_cmpl_work);
1270 		num_ent += nent_per_cq;
1271 
1272 		if (nent_per_cq > atomic64_read(&misc_stats->max_cq_ents))
1273 			atomic64_set(&misc_stats->max_cq_ents, nent_per_cq);
1274 	}
1275 
1276 	return num_ent;
1277 } /* end of snic_fwcq_cmpl_handler */
1278 
1279 /*
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.
1283  */
1284 static int
1285 snic_queue_itmf_req(struct snic *snic,
1286 		    struct snic_host_req *tmreq,
1287 		    struct scsi_cmnd *sc,
1288 		    u32 tmf,
1289 		    u32 req_id)
1290 {
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;
1294 	int ret = 0;
1295 
1296 	SNIC_BUG_ON(!rqi);
1297 	SNIC_BUG_ON(!rqi->tm_tag);
1298 
1299 	/* fill in lun info */
1300 	int_to_scsilun(sc->device->lun, &lun);
1301 
1302 	/* Initialize snic_host_req: itmf */
1303 	snic_itmf_init(tmreq,
1304 		       tm_tag,
1305 		       snic->config.hid,
1306 		       (ulong) rqi,
1307 		       0 /* flags */,
1308 		       req_id, /* Command to be aborted. */
1309 		       rqi->tgt_id,
1310 		       lun.scsi_lun,
1311 		       tmf);
1312 
1313 	/*
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));
1318 	 */
1319 
1320 	ret = snic_queue_wq_desc(snic, tmreq, sizeof(*tmreq));
1321 	if (ret)
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);
1325 	else
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));
1329 
1330 	return ret;
1331 } /* end of snic_queue_itmf_req */
1332 
1333 static int
1334 snic_issue_tm_req(struct snic *snic,
1335 		    struct snic_req_info *rqi,
1336 		    struct scsi_cmnd *sc,
1337 		    int tmf)
1338 {
1339 	struct snic_host_req *tmreq = NULL;
1340 	int req_id = 0, tag = snic_cmd_tag(sc);
1341 	int ret = 0;
1342 
1343 	if (snic_get_state(snic) == SNIC_FWRESET)
1344 		return -EBUSY;
1345 
1346 	atomic_inc(&snic->ios_inflight);
1347 
1348 	SNIC_SCSI_DBG(snic->shost,
1349 		      "issu_tmreq: Task mgmt req %d. rqi %p w/ tag %x\n",
1350 		      tmf, rqi, tag);
1351 
1352 
1353 	if (tmf == SNIC_ITMF_LUN_RESET) {
1354 		tmreq = snic_dr_req_init(snic, rqi);
1355 		req_id = SCSI_NO_TAG;
1356 	} else {
1357 		tmreq = snic_abort_req_init(snic, rqi);
1358 		req_id = tag;
1359 	}
1360 
1361 	if (!tmreq) {
1362 		ret = -ENOMEM;
1363 
1364 		goto tmreq_err;
1365 	}
1366 
1367 	ret = snic_queue_itmf_req(snic, tmreq, sc, tmf, req_id);
1368 
1369 tmreq_err:
1370 	if (ret) {
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);
1374 	} else {
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);
1378 	}
1379 
1380 	atomic_dec(&snic->ios_inflight);
1381 
1382 	return ret;
1383 }
1384 
1385 /*
1386  * snic_queue_abort_req : Queues abort req to WQ
1387  */
1388 static int
1389 snic_queue_abort_req(struct snic *snic,
1390 		     struct snic_req_info *rqi,
1391 		     struct scsi_cmnd *sc,
1392 		     int tmf)
1393 {
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);
1396 
1397 	/* Add special tag for abort */
1398 	rqi->tm_tag |= SNIC_TAG_ABORT;
1399 
1400 	return snic_issue_tm_req(snic, rqi, sc, tmf);
1401 }
1402 
1403 /*
1404  * snic_abort_finish : called by snic_abort_cmd on queuing abort successfully.
1405  */
1406 static int
1407 snic_abort_finish(struct snic *snic, struct scsi_cmnd *sc)
1408 {
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);
1413 
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);
1417 	if (!rqi) {
1418 		atomic64_inc(&snic->s_stats.io.req_null);
1419 		CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_REQ_NULL;
1420 
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));
1424 		ret = FAILED;
1425 
1426 		goto abort_fail;
1427 	}
1428 
1429 	rqi->abts_done = NULL;
1430 
1431 	ret = FAILED;
1432 
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 */
1443 		rqi = NULL;
1444 
1445 		goto abort_fail;
1446 
1447 	case SNIC_STAT_IO_SUCCESS:
1448 	case SNIC_STAT_IO_NOT_FOUND:
1449 		ret = SUCCESS;
1450 		/*
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.
1454 		 */
1455 		sc->result = (DID_ERROR << 16);
1456 		scsi_done(sc);
1457 		break;
1458 
1459 	default:
1460 		/* Firmware completed abort with error */
1461 		ret = FAILED;
1462 		rqi = NULL;
1463 		break;
1464 	}
1465 
1466 	CMD_SP(sc) = NULL;
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)),
1470 		       CMD_FLAGS(sc));
1471 
1472 abort_fail:
1473 	spin_unlock_irqrestore(io_lock, flags);
1474 	if (rqi)
1475 		snic_release_req_buf(snic, rqi, sc);
1476 
1477 	return ret;
1478 } /* end of snic_abort_finish */
1479 
1480 /*
1481  * snic_send_abort_and_wait : Issues Abort, and Waits
1482  */
1483 static int
1484 snic_send_abort_and_wait(struct snic *snic, struct scsi_cmnd *sc)
1485 {
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);
1493 
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;
1497 	else
1498 		tmf = SNIC_ITMF_ABTS_TASK;
1499 
1500 	/* stats */
1501 
1502 	io_lock = snic_io_lock_hash(snic, sc);
1503 
1504 	/*
1505 	 * Avoid a race between SCSI issuing the abort and the device
1506 	 * completing the command.
1507 	 *
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
1513 	 *
1514 	 * The CMD_SP will not be cleared except while holding io_lock
1515 	 */
1516 	spin_lock_irqsave(io_lock, flags);
1517 	rqi = (struct snic_req_info *) CMD_SP(sc);
1518 	if (!rqi) {
1519 		spin_unlock_irqrestore(io_lock, flags);
1520 
1521 		SNIC_HOST_ERR(snic->shost,
1522 			      "abt_cmd: rqi is null. Tag %d flags 0x%llx\n",
1523 			      tag, CMD_FLAGS(sc));
1524 
1525 		ret = SUCCESS;
1526 
1527 		goto send_abts_end;
1528 	}
1529 
1530 	rqi->abts_done = &tm_done;
1531 	if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING) {
1532 		spin_unlock_irqrestore(io_lock, flags);
1533 
1534 		ret = 0;
1535 		goto abts_pending;
1536 	}
1537 	SNIC_BUG_ON(!rqi->abts_done);
1538 
1539 	/* Save Command State, should be restored on failed to Queue. */
1540 	sv_state = CMD_STATE(sc);
1541 
1542 	/*
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.
1547 	 */
1548 	CMD_STATE(sc) = SNIC_IOREQ_ABTS_PENDING;
1549 	CMD_ABTS_STATUS(sc) = SNIC_INVALID_CODE;
1550 
1551 	SNIC_SCSI_DBG(snic->shost, "send_abt_cmd: TAG 0x%x\n", tag);
1552 
1553 	spin_unlock_irqrestore(io_lock, flags);
1554 
1555 	/* Now Queue the abort command to firmware */
1556 	ret = snic_queue_abort_req(snic, rqi, sc, tmf);
1557 	if (ret) {
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));
1562 
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);
1567 		if (rqi)
1568 			rqi->abts_done = NULL;
1569 		spin_unlock_irqrestore(io_lock, flags);
1570 		ret = FAILED;
1571 
1572 		goto send_abts_end;
1573 	}
1574 
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);
1579 	} else {
1580 		/* term stats */
1581 		CMD_FLAGS(sc) |= SNIC_IO_TERM_ISSUED;
1582 	}
1583 	spin_unlock_irqrestore(io_lock, flags);
1584 
1585 	SNIC_SCSI_DBG(snic->shost,
1586 		      "send_abt_cmd: sc %p Tag %x flags 0x%llx\n",
1587 		      sc, tag, CMD_FLAGS(sc));
1588 
1589 
1590 	ret = 0;
1591 
1592 abts_pending:
1593 	/*
1594 	 * Queued an abort IO, wait for its completion.
1595 	 * Once the fw completes the abort command, it will
1596 	 * wakeup this thread.
1597 	 */
1598 	wait_for_completion_timeout(&tm_done, SNIC_ABTS_TIMEOUT);
1599 
1600 send_abts_end:
1601 	return ret;
1602 } /* end of snic_send_abort_and_wait */
1603 
1604 /*
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
1608  */
1609 int
1610 snic_abort_cmd(struct scsi_cmnd *sc)
1611 {
1612 	struct snic *snic = shost_priv(sc->device->host);
1613 	int ret = SUCCESS, tag = snic_cmd_tag(sc);
1614 	u32 start_time = jiffies;
1615 
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);
1618 
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",
1622 			      tag);
1623 		ret = FAST_IO_FAIL;
1624 
1625 		goto abort_end;
1626 	}
1627 
1628 
1629 	ret = snic_send_abort_and_wait(snic, sc);
1630 	if (ret)
1631 		goto abort_end;
1632 
1633 	ret = snic_abort_finish(snic, sc);
1634 
1635 abort_end:
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));
1639 
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"));
1644 
1645 	return ret;
1646 }
1647 
1648 
1649 
1650 static int
1651 snic_is_abts_pending(struct snic *snic, struct scsi_cmnd *lr_sc)
1652 {
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;
1657 	u32 tag;
1658 	unsigned long flags;
1659 
1660 	if (lr_sc)
1661 		lr_sdev = lr_sc->device;
1662 
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);
1666 
1667 		spin_lock_irqsave(io_lock, flags);
1668 		sc = scsi_host_find_tag(snic->shost, tag);
1669 
1670 		if (!sc || (lr_sc && (sc->device != lr_sdev || sc == lr_sc))) {
1671 			spin_unlock_irqrestore(io_lock, flags);
1672 
1673 			continue;
1674 		}
1675 
1676 		rqi = (struct snic_req_info *) CMD_SP(sc);
1677 		if (!rqi) {
1678 			spin_unlock_irqrestore(io_lock, flags);
1679 
1680 			continue;
1681 		}
1682 
1683 		/*
1684 		 * Found IO that is still pending w/ firmware and belongs to
1685 		 * the LUN that is under reset, if lr_sc != NULL
1686 		 */
1687 		SNIC_SCSI_DBG(snic->shost, "Found IO in %s on LUN\n",
1688 			      snic_ioreq_state_to_str(CMD_STATE(sc)));
1689 
1690 		if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING) {
1691 			spin_unlock_irqrestore(io_lock, flags);
1692 
1693 			return 1;
1694 		}
1695 
1696 		spin_unlock_irqrestore(io_lock, flags);
1697 	}
1698 
1699 	return 0;
1700 } /* end of snic_is_abts_pending */
1701 
1702 static int
1703 snic_dr_clean_single_req(struct snic *snic,
1704 			 u32 tag,
1705 			 struct scsi_device *lr_sdev)
1706 {
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;
1714 	int ret = 0;
1715 
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);
1719 
1720 	/* Ignore Cmd that don't belong to Lun Reset device */
1721 	if (!sc || sc->device != lr_sdev)
1722 		goto skip_clean;
1723 
1724 	rqi = (struct snic_req_info *) CMD_SP(sc);
1725 
1726 	if (!rqi)
1727 		goto skip_clean;
1728 
1729 
1730 	if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
1731 		goto skip_clean;
1732 
1733 
1734 	if ((CMD_FLAGS(sc) & SNIC_DEVICE_RESET) &&
1735 			(!(CMD_FLAGS(sc) & SNIC_DEV_RST_ISSUED))) {
1736 
1737 		SNIC_SCSI_DBG(snic->shost,
1738 			      "clean_single_req: devrst is not pending sc 0x%p\n",
1739 			      sc);
1740 
1741 		goto skip_clean;
1742 	}
1743 
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)));
1747 
1748 	/* Save Command State */
1749 	sv_state = CMD_STATE(sc);
1750 
1751 	/*
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
1756 	 * in this function.
1757 	 */
1758 
1759 	CMD_STATE(sc) = SNIC_IOREQ_ABTS_PENDING;
1760 	SNIC_BUG_ON(rqi->abts_done);
1761 
1762 	if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET) {
1763 		rqi->tm_tag = SNIC_TAG_DEV_RST;
1764 
1765 		SNIC_SCSI_DBG(snic->shost,
1766 			      "clean_single_req:devrst sc 0x%p\n", sc);
1767 	}
1768 
1769 	CMD_ABTS_STATUS(sc) = SNIC_INVALID_CODE;
1770 	rqi->abts_done = &tm_done;
1771 	spin_unlock_irqrestore(io_lock, flags);
1772 
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;
1776 	else
1777 		tmf = SNIC_ITMF_ABTS_TASK;
1778 
1779 	/* Now queue the abort command to firmware */
1780 	ret = snic_queue_abort_req(snic, rqi, sc, tmf);
1781 	if (ret) {
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));
1785 
1786 		spin_lock_irqsave(io_lock, flags);
1787 		rqi = (struct snic_req_info *) CMD_SP(sc);
1788 		if (rqi)
1789 			rqi->abts_done = NULL;
1790 
1791 		/* Restore Command State */
1792 		if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
1793 			CMD_STATE(sc) = sv_state;
1794 
1795 		ret = 1;
1796 		goto skip_clean;
1797 	}
1798 
1799 	spin_lock_irqsave(io_lock, flags);
1800 	if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET)
1801 		CMD_FLAGS(sc) |= SNIC_DEV_RST_TERM_ISSUED;
1802 
1803 	CMD_FLAGS(sc) |= SNIC_IO_INTERNAL_TERM_ISSUED;
1804 	spin_unlock_irqrestore(io_lock, flags);
1805 
1806 	wait_for_completion_timeout(&tm_done, SNIC_ABTS_TIMEOUT);
1807 
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);
1811 	if (!rqi) {
1812 		CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_REQ_NULL;
1813 		goto skip_clean;
1814 	}
1815 	rqi->abts_done = NULL;
1816 
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));
1822 
1823 		CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_DONE;
1824 		ret = 1;
1825 
1826 		goto skip_clean;
1827 	}
1828 
1829 	CMD_STATE(sc) = SNIC_IOREQ_ABTS_COMPLETE;
1830 	CMD_SP(sc) = NULL;
1831 	spin_unlock_irqrestore(io_lock, flags);
1832 
1833 	snic_release_req_buf(snic, rqi, sc);
1834 
1835 	sc->result = (DID_ERROR << 16);
1836 	scsi_done(sc);
1837 
1838 	ret = 0;
1839 
1840 	return ret;
1841 
1842 skip_clean:
1843 	spin_unlock_irqrestore(io_lock, flags);
1844 
1845 	return ret;
1846 } /* end of snic_dr_clean_single_req */
1847 
1848 static int
1849 snic_dr_clean_pending_req(struct snic *snic, struct scsi_cmnd *lr_sc)
1850 {
1851 	struct scsi_device *lr_sdev = lr_sc->device;
1852 	u32 tag = 0;
1853 	int ret;
1854 
1855 	for (tag = 0; tag < snic->max_tag_id; tag++) {
1856 		if (tag == snic_cmd_tag(lr_sc))
1857 			continue;
1858 
1859 		ret = snic_dr_clean_single_req(snic, tag, lr_sdev);
1860 		if (ret) {
1861 			SNIC_HOST_ERR(snic->shost, "clean_err:tag = %d\n", tag);
1862 			goto clean_err;
1863 		}
1864 	}
1865 
1866 	schedule_timeout(msecs_to_jiffies(100));
1867 
1868 	/* Walk through all the cmds and check abts status. */
1869 	if (snic_is_abts_pending(snic, lr_sc))
1870 		goto clean_err;
1871 
1872 	SNIC_SCSI_DBG(snic->shost, "clean_pending_req: Success.\n");
1873 
1874 	return 0;
1875 
1876 clean_err:
1877 	SNIC_HOST_ERR(snic->shost,
1878 		      "Failed to Clean Pending IOs on %s device.\n",
1879 		      dev_name(&lr_sdev->sdev_gendev));
1880 
1881 	return FAILED;
1882 
1883 } /* end of snic_dr_clean_pending_req */
1884 
1885 /*
1886  * snic_dr_finish : Called by snic_device_reset
1887  */
1888 static int
1889 snic_dr_finish(struct snic *snic, struct scsi_cmnd *sc)
1890 {
1891 	struct snic_req_info *rqi = NULL;
1892 	spinlock_t *io_lock = NULL;
1893 	unsigned long flags;
1894 	int lr_res = 0;
1895 	int ret = FAILED;
1896 
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);
1900 	if (!rqi) {
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));
1905 
1906 		ret = FAILED;
1907 		goto dr_fini_end;
1908 	}
1909 
1910 	rqi->dr_done = NULL;
1911 
1912 	lr_res = CMD_LR_STATUS(sc);
1913 
1914 	switch (lr_res) {
1915 	case SNIC_INVALID_CODE:
1916 		/* stats */
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));
1920 
1921 		CMD_FLAGS(sc) |= SNIC_DEV_RST_TIMEDOUT;
1922 		ret = FAILED;
1923 
1924 		goto dr_failed;
1925 
1926 	case SNIC_STAT_IO_SUCCESS:
1927 		SNIC_SCSI_DBG(snic->shost,
1928 			      "dr_fini: Tag %x Dev Reset cmpl\n",
1929 			      snic_cmd_tag(sc));
1930 		ret = 0;
1931 		break;
1932 
1933 	default:
1934 		SNIC_HOST_ERR(snic->shost,
1935 			      "dr_fini:Device Reset completed& failed.Tag = %x lr_status %s flags 0x%llx\n",
1936 			      snic_cmd_tag(sc),
1937 			      snic_io_status_to_str(lr_res), CMD_FLAGS(sc));
1938 		ret = FAILED;
1939 		goto dr_failed;
1940 	}
1941 	spin_unlock_irqrestore(io_lock, flags);
1942 
1943 	/*
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
1948 	 * succeeds.
1949 	 */
1950 
1951 	ret = snic_dr_clean_pending_req(snic, sc);
1952 	if (ret) {
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",
1956 			      snic_cmd_tag(sc));
1957 		rqi = (struct snic_req_info *) CMD_SP(sc);
1958 
1959 		goto dr_failed;
1960 	} else {
1961 		/* Cleanup LUN Reset Command */
1962 		spin_lock_irqsave(io_lock, flags);
1963 		rqi = (struct snic_req_info *) CMD_SP(sc);
1964 		if (rqi)
1965 			ret = SUCCESS; /* Completed Successfully */
1966 		else
1967 			ret = FAILED;
1968 	}
1969 
1970 dr_failed:
1971 	lockdep_assert_held(io_lock);
1972 	if (rqi)
1973 		CMD_SP(sc) = NULL;
1974 	spin_unlock_irqrestore(io_lock, flags);
1975 
1976 	if (rqi)
1977 		snic_release_req_buf(snic, rqi, sc);
1978 
1979 dr_fini_end:
1980 	return ret;
1981 } /* end of snic_dr_finish */
1982 
1983 static int
1984 snic_queue_dr_req(struct snic *snic,
1985 		  struct snic_req_info *rqi,
1986 		  struct scsi_cmnd *sc)
1987 {
1988 	/* Add special tag for device reset */
1989 	rqi->tm_tag |= SNIC_TAG_DEV_RST;
1990 
1991 	return snic_issue_tm_req(snic, rqi, sc, SNIC_ITMF_LUN_RESET);
1992 }
1993 
1994 static int
1995 snic_send_dr_and_wait(struct snic *snic, struct scsi_cmnd *sc)
1996 {
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);
2003 
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);
2008 	if (!rqi) {
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);
2013 
2014 		ret = FAILED;
2015 		goto send_dr_end;
2016 	}
2017 
2018 	/* Save Command state to restore in case Queuing failed. */
2019 	sv_state = CMD_STATE(sc);
2020 
2021 	CMD_STATE(sc) = SNIC_IOREQ_LR_PENDING;
2022 	CMD_LR_STATUS(sc) = SNIC_INVALID_CODE;
2023 
2024 	SNIC_SCSI_DBG(snic->shost, "dr: TAG = %x\n", tag);
2025 
2026 	rqi->dr_done = &tm_done;
2027 	SNIC_BUG_ON(!rqi->dr_done);
2028 
2029 	spin_unlock_irqrestore(io_lock, flags);
2030 	/*
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.
2035 	 */
2036 
2037 	ret = snic_queue_dr_req(snic, rqi, sc);
2038 	if (ret) {
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));
2042 
2043 		spin_lock_irqsave(io_lock, flags);
2044 		/* Restore State */
2045 		CMD_STATE(sc) = sv_state;
2046 		rqi = (struct snic_req_info *) CMD_SP(sc);
2047 		if (rqi)
2048 			rqi->dr_done = NULL;
2049 		/* rqi is freed in caller. */
2050 		spin_unlock_irqrestore(io_lock, flags);
2051 		ret = FAILED;
2052 
2053 		goto send_dr_end;
2054 	}
2055 
2056 	spin_lock_irqsave(io_lock, flags);
2057 	CMD_FLAGS(sc) |= SNIC_DEV_RST_ISSUED;
2058 	spin_unlock_irqrestore(io_lock, flags);
2059 
2060 	ret = 0;
2061 
2062 	wait_for_completion_timeout(&tm_done, SNIC_LUN_RESET_TIMEOUT);
2063 
2064 send_dr_end:
2065 	return ret;
2066 }
2067 
2068 /*
2069  * auxillary funciton to check lun reset op is supported or not
2070  * Not supported if returns 0
2071  */
2072 static int
2073 snic_dev_reset_supported(struct scsi_device *sdev)
2074 {
2075 	struct snic_tgt *tgt = starget_to_tgt(scsi_target(sdev));
2076 
2077 	if (tgt->tdata.typ == SNIC_TGT_DAS)
2078 		return 0;
2079 
2080 	return 1;
2081 }
2082 
2083 static void
2084 snic_unlink_and_release_req(struct snic *snic, struct scsi_cmnd *sc, int flag)
2085 {
2086 	struct snic_req_info *rqi = NULL;
2087 	spinlock_t *io_lock = NULL;
2088 	unsigned long flags;
2089 	u32 start_time = jiffies;
2090 
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);
2094 	if (rqi) {
2095 		start_time = rqi->start_time;
2096 		CMD_SP(sc) = NULL;
2097 	}
2098 
2099 	CMD_FLAGS(sc) |= flag;
2100 	spin_unlock_irqrestore(io_lock, flags);
2101 
2102 	if (rqi)
2103 		snic_release_req_buf(snic, rqi, sc);
2104 
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));
2108 }
2109 
2110 /*
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.
2114  */
2115 int
2116 snic_device_reset(struct scsi_cmnd *sc)
2117 {
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;
2123 	int ret = FAILED;
2124 	int dr_supp = 0;
2125 
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),
2128 		      snic_cmd_tag(sc));
2129 	dr_supp = snic_dev_reset_supported(sc->device);
2130 	if (!dr_supp) {
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);
2134 
2135 		goto dev_rst_end;
2136 	}
2137 
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");
2141 
2142 		goto dev_rst_end;
2143 	}
2144 
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");
2149 
2150 		rqi = snic_req_init(snic, 0);
2151 		if (!rqi)
2152 			goto dev_rst_end;
2153 
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;
2158 
2159 		/* Add special tag for dr coming from user spc */
2160 		rqi->tm_tag = SNIC_TAG_IOCTL_DEV_RST;
2161 		rqi->sc = sc;
2162 	}
2163 
2164 	ret = snic_send_dr_and_wait(snic, sc);
2165 	if (ret) {
2166 		SNIC_HOST_ERR(snic->shost,
2167 			      "Devrst: IO w/ Tag %x Failed w/ err = %d\n",
2168 			      tag, ret);
2169 
2170 		snic_unlink_and_release_req(snic, sc, 0);
2171 
2172 		goto dev_rst_end;
2173 	}
2174 
2175 	ret = snic_dr_finish(snic, sc);
2176 
2177 dev_rst_end:
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));
2181 
2182 	SNIC_SCSI_DBG(snic->shost,
2183 		      "Devrst: Returning from Device Reset : %s\n",
2184 		      (ret == SUCCESS) ? "SUCCESS" : "FAILED");
2185 
2186 	return ret;
2187 } /* end of snic_device_reset */
2188 
2189 /*
2190  * SCSI Error handling calls driver's eh_host_reset if all prior
2191  * error handling levels return FAILED.
2192  *
2193  * Host Reset is the highest level of error recovery. If this fails, then
2194  * host is offlined by SCSI.
2195  */
2196 /*
2197  * snic_issue_hba_reset : Queues FW Reset Request.
2198  */
2199 static int
2200 snic_issue_hba_reset(struct snic *snic, struct scsi_cmnd *sc)
2201 {
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;
2207 	int ret = -ENOMEM;
2208 
2209 	rqi = snic_req_init(snic, 0);
2210 	if (!rqi) {
2211 		ret = -ENOMEM;
2212 
2213 		goto hba_rst_end;
2214 	}
2215 
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");
2220 		rqi->sc = sc;
2221 	}
2222 
2223 	req = rqi_to_req(rqi);
2224 
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);
2233 
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);
2237 
2238 	req->u.reset.flags = 0;
2239 
2240 	ret = snic_queue_wq_desc(snic, req, sizeof(*req));
2241 	if (ret) {
2242 		SNIC_HOST_ERR(snic->shost,
2243 			      "issu_hr:Queuing HBA Reset Failed. w err %d\n",
2244 			      ret);
2245 
2246 		goto hba_rst_err;
2247 	}
2248 
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");
2254 
2255 	wait_for_completion_timeout(snic->remove_wait,
2256 				    SNIC_HOST_RESET_TIMEOUT);
2257 
2258 	if (snic_get_state(snic) == SNIC_FWRESET) {
2259 		SNIC_HOST_ERR(snic->shost, "reset_cmpl: Reset Timedout.\n");
2260 		ret = -ETIMEDOUT;
2261 
2262 		goto hba_rst_err;
2263 	}
2264 
2265 	spin_lock_irqsave(io_lock, flags);
2266 	snic->remove_wait = NULL;
2267 	rqi = (struct snic_req_info *) CMD_SP(sc);
2268 	CMD_SP(sc) = NULL;
2269 	spin_unlock_irqrestore(io_lock, flags);
2270 
2271 	if (rqi)
2272 		snic_req_free(snic, rqi);
2273 
2274 	ret = 0;
2275 
2276 	return ret;
2277 
2278 hba_rst_err:
2279 	spin_lock_irqsave(io_lock, flags);
2280 	snic->remove_wait = NULL;
2281 	rqi = (struct snic_req_info *) CMD_SP(sc);
2282 	CMD_SP(sc) = NULL;
2283 	spin_unlock_irqrestore(io_lock, flags);
2284 
2285 	if (rqi)
2286 		snic_req_free(snic, rqi);
2287 
2288 hba_rst_end:
2289 	SNIC_HOST_ERR(snic->shost,
2290 		      "reset:HBA Reset Failed w/ err = %d.\n",
2291 		      ret);
2292 
2293 	return ret;
2294 } /* end of snic_issue_hba_reset */
2295 
2296 int
2297 snic_reset(struct Scsi_Host *shost, struct scsi_cmnd *sc)
2298 {
2299 	struct snic *snic = shost_priv(shost);
2300 	enum snic_state sv_state;
2301 	unsigned long flags;
2302 	int ret = FAILED;
2303 
2304 	/* Set snic state as SNIC_FWRESET*/
2305 	sv_state = snic_get_state(snic);
2306 
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");
2311 
2312 		msleep(SNIC_HOST_RESET_TIMEOUT);
2313 		ret = SUCCESS;
2314 
2315 		goto reset_end;
2316 	}
2317 
2318 	snic_set_state(snic, SNIC_FWRESET);
2319 	spin_unlock_irqrestore(&snic->snic_lock, flags);
2320 
2321 
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));
2325 
2326 	ret = snic_issue_hba_reset(snic, sc);
2327 	if (ret) {
2328 		SNIC_HOST_ERR(shost,
2329 			      "reset:Host Reset Failed w/ err %d.\n",
2330 			      ret);
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);
2335 		ret = FAILED;
2336 
2337 		goto reset_end;
2338 	}
2339 
2340 	ret = SUCCESS;
2341 
2342 reset_end:
2343 	return ret;
2344 } /* end of snic_reset */
2345 
2346 /*
2347  * SCSI Error handling calls driver's eh_host_reset if all prior
2348  * error handling levels return FAILED.
2349  *
2350  * Host Reset is the highest level of error recovery. If this fails, then
2351  * host is offlined by SCSI.
2352  */
2353 int
2354 snic_host_reset(struct scsi_cmnd *sc)
2355 {
2356 	struct Scsi_Host *shost = sc->device->host;
2357 	u32 start_time  = jiffies;
2358 	int ret;
2359 
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));
2364 
2365 	ret = snic_reset(shost, sc);
2366 
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));
2370 
2371 	return ret;
2372 } /* end of snic_host_reset */
2373 
2374 /*
2375  * snic_cmpl_pending_tmreq : Caller should hold io_lock
2376  */
2377 static void
2378 snic_cmpl_pending_tmreq(struct snic *snic, struct scsi_cmnd *sc)
2379 {
2380 	struct snic_req_info *rqi = NULL;
2381 
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));
2385 
2386 	/*
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()
2390 	 */
2391 	CMD_ABTS_STATUS(sc) = SNIC_STAT_IO_SUCCESS;
2392 
2393 	rqi = (struct snic_req_info *) CMD_SP(sc);
2394 	if (!rqi)
2395 		return;
2396 
2397 	if (rqi->dr_done)
2398 		complete(rqi->dr_done);
2399 	else if (rqi->abts_done)
2400 		complete(rqi->abts_done);
2401 }
2402 
2403 /*
2404  * snic_scsi_cleanup: Walks through tag map and releases the reqs
2405  */
2406 static void
2407 snic_scsi_cleanup(struct snic *snic, int ex_tag)
2408 {
2409 	struct snic_req_info *rqi = NULL;
2410 	struct scsi_cmnd *sc = NULL;
2411 	spinlock_t *io_lock = NULL;
2412 	unsigned long flags;
2413 	int tag;
2414 	u64 st_time = 0;
2415 
2416 	SNIC_SCSI_DBG(snic->shost, "sc_clean: scsi cleanup.\n");
2417 
2418 	for (tag = 0; tag < snic->max_tag_id; tag++) {
2419 		/* Skip ex_tag */
2420 		if (tag == ex_tag)
2421 			continue;
2422 
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);
2426 		if (!sc) {
2427 			spin_unlock_irqrestore(io_lock, flags);
2428 
2429 			continue;
2430 		}
2431 
2432 		if (unlikely(snic_tmreq_pending(sc))) {
2433 			/*
2434 			 * When FW Completes reset w/o sending completions
2435 			 * for outstanding ios.
2436 			 */
2437 			snic_cmpl_pending_tmreq(snic, sc);
2438 			spin_unlock_irqrestore(io_lock, flags);
2439 
2440 			continue;
2441 		}
2442 
2443 		rqi = (struct snic_req_info *) CMD_SP(sc);
2444 		if (!rqi) {
2445 			spin_unlock_irqrestore(io_lock, flags);
2446 
2447 			goto cleanup;
2448 		}
2449 
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));
2453 
2454 		CMD_SP(sc) = NULL;
2455 		CMD_FLAGS(sc) |= SNIC_SCSI_CLEANUP;
2456 		spin_unlock_irqrestore(io_lock, flags);
2457 		st_time = rqi->start_time;
2458 
2459 		SNIC_HOST_INFO(snic->shost,
2460 			       "sc_clean: Releasing rqi %p : flags 0x%llx\n",
2461 			       rqi, CMD_FLAGS(sc));
2462 
2463 		snic_release_req_buf(snic, rqi, sc);
2464 
2465 cleanup:
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));
2471 
2472 		/* Update IO stats */
2473 		snic_stats_update_io_cmpl(&snic->s_stats);
2474 
2475 		SNIC_TRC(snic->shost->host_no, tag, (ulong) sc,
2476 			 jiffies_to_msecs(jiffies - st_time), 0,
2477 			 SNIC_TRC_CMD(sc),
2478 			 SNIC_TRC_CMD_STATE_FLAGS(sc));
2479 
2480 		scsi_done(sc);
2481 	}
2482 } /* end of snic_scsi_cleanup */
2483 
2484 void
2485 snic_shutdown_scsi_cleanup(struct snic *snic)
2486 {
2487 	SNIC_HOST_INFO(snic->shost, "Shutdown time SCSI Cleanup.\n");
2488 
2489 	snic_scsi_cleanup(snic, SCSI_NO_TAG);
2490 } /* end of snic_shutdown_scsi_cleanup */
2491 
2492 /*
2493  * snic_internal_abort_io
2494  * called by : snic_tgt_scsi_abort_io
2495  */
2496 static int
2497 snic_internal_abort_io(struct snic *snic, struct scsi_cmnd *sc, int tmf)
2498 {
2499 	struct snic_req_info *rqi = NULL;
2500 	spinlock_t *io_lock = NULL;
2501 	unsigned long flags;
2502 	u32 sv_state = 0;
2503 	int ret = 0;
2504 
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);
2508 	if (!rqi)
2509 		goto skip_internal_abts;
2510 
2511 	if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
2512 		goto skip_internal_abts;
2513 
2514 	if ((CMD_FLAGS(sc) & SNIC_DEVICE_RESET) &&
2515 		(!(CMD_FLAGS(sc) & SNIC_DEV_RST_ISSUED))) {
2516 
2517 		SNIC_SCSI_DBG(snic->shost,
2518 			      "internal_abts: dev rst not pending sc 0x%p\n",
2519 			      sc);
2520 
2521 		goto skip_internal_abts;
2522 	}
2523 
2524 
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));
2529 
2530 		goto skip_internal_abts;
2531 	}
2532 
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;
2537 
2538 	if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET) {
2539 		/* stats */
2540 		rqi->tm_tag = SNIC_TAG_DEV_RST;
2541 		SNIC_SCSI_DBG(snic->shost, "internal_abts:dev rst sc %p\n", sc);
2542 	}
2543 
2544 	SNIC_SCSI_DBG(snic->shost, "internal_abts: Issuing abts tag %x\n",
2545 		      snic_cmd_tag(sc));
2546 	SNIC_BUG_ON(rqi->abts_done);
2547 	spin_unlock_irqrestore(io_lock, flags);
2548 
2549 	ret = snic_queue_abort_req(snic, rqi, sc, tmf);
2550 	if (ret) {
2551 		SNIC_HOST_ERR(snic->shost,
2552 			      "internal_abts: Tag = %x , Failed w/ err = %d\n",
2553 			      snic_cmd_tag(sc), ret);
2554 
2555 		spin_lock_irqsave(io_lock, flags);
2556 
2557 		if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
2558 			CMD_STATE(sc) = sv_state;
2559 
2560 		goto skip_internal_abts;
2561 	}
2562 
2563 	spin_lock_irqsave(io_lock, flags);
2564 	if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET)
2565 		CMD_FLAGS(sc) |= SNIC_DEV_RST_TERM_ISSUED;
2566 	else
2567 		CMD_FLAGS(sc) |= SNIC_IO_INTERNAL_TERM_ISSUED;
2568 
2569 	ret = SUCCESS;
2570 
2571 skip_internal_abts:
2572 	lockdep_assert_held(io_lock);
2573 	spin_unlock_irqrestore(io_lock, flags);
2574 
2575 	return ret;
2576 } /* end of snic_internal_abort_io */
2577 
2578 /*
2579  * snic_tgt_scsi_abort_io : called by snic_tgt_del
2580  */
2581 int
2582 snic_tgt_scsi_abort_io(struct snic_tgt *tgt)
2583 {
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;
2590 
2591 	if (!tgt)
2592 		return -1;
2593 
2594 	snic = shost_priv(snic_tgt_to_shost(tgt));
2595 	SNIC_SCSI_DBG(snic->shost, "tgt_abt_io: Cleaning Pending IOs.\n");
2596 
2597 	if (tgt->tdata.typ == SNIC_TGT_DAS)
2598 		tmf = SNIC_ITMF_ABTS_TASK;
2599 	else
2600 		tmf = SNIC_ITMF_ABTS_TASK_TERM;
2601 
2602 	for (tag = 0; tag < snic->max_tag_id; tag++) {
2603 		io_lock = snic_io_lock_tag(snic, tag);
2604 
2605 		spin_lock_irqsave(io_lock, flags);
2606 		sc = scsi_host_find_tag(snic->shost, tag);
2607 		if (!sc) {
2608 			spin_unlock_irqrestore(io_lock, flags);
2609 
2610 			continue;
2611 		}
2612 
2613 		sc_tgt = starget_to_tgt(scsi_target(sc->device));
2614 		if (sc_tgt != tgt) {
2615 			spin_unlock_irqrestore(io_lock, flags);
2616 
2617 			continue;
2618 		}
2619 		spin_unlock_irqrestore(io_lock, flags);
2620 
2621 		ret = snic_internal_abort_io(snic, sc, tmf);
2622 		if (ret < 0) {
2623 			SNIC_HOST_ERR(snic->shost,
2624 				      "tgt_abt_io: Tag %x, Failed w err = %d\n",
2625 				      tag, ret);
2626 
2627 			continue;
2628 		}
2629 
2630 		if (ret == SUCCESS)
2631 			abt_cnt++;
2632 	}
2633 
2634 	SNIC_SCSI_DBG(snic->shost, "tgt_abt_io: abt_cnt = %d\n", abt_cnt);
2635 
2636 	return 0;
2637 } /* end of snic_tgt_scsi_abort_io */
2638