xref: /openbmc/linux/drivers/scsi/snic/snic_scsi.c (revision 0c6dfa75)
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 = FAILED;
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 
1863 			goto clean_err;
1864 		}
1865 	}
1866 
1867 	schedule_timeout(msecs_to_jiffies(100));
1868 
1869 	/* Walk through all the cmds and check abts status. */
1870 	if (snic_is_abts_pending(snic, lr_sc)) {
1871 		ret = FAILED;
1872 
1873 		goto clean_err;
1874 	}
1875 
1876 	ret = 0;
1877 	SNIC_SCSI_DBG(snic->shost, "clean_pending_req: Success.\n");
1878 
1879 	return ret;
1880 
1881 clean_err:
1882 	ret = FAILED;
1883 	SNIC_HOST_ERR(snic->shost,
1884 		      "Failed to Clean Pending IOs on %s device.\n",
1885 		      dev_name(&lr_sdev->sdev_gendev));
1886 
1887 	return ret;
1888 
1889 } /* end of snic_dr_clean_pending_req */
1890 
1891 /*
1892  * snic_dr_finish : Called by snic_device_reset
1893  */
1894 static int
1895 snic_dr_finish(struct snic *snic, struct scsi_cmnd *sc)
1896 {
1897 	struct snic_req_info *rqi = NULL;
1898 	spinlock_t *io_lock = NULL;
1899 	unsigned long flags;
1900 	int lr_res = 0;
1901 	int ret = FAILED;
1902 
1903 	io_lock = snic_io_lock_hash(snic, sc);
1904 	spin_lock_irqsave(io_lock, flags);
1905 	rqi = (struct snic_req_info *) CMD_SP(sc);
1906 	if (!rqi) {
1907 		spin_unlock_irqrestore(io_lock, flags);
1908 		SNIC_SCSI_DBG(snic->shost,
1909 			      "dr_fini: rqi is null tag 0x%x sc 0x%p flags 0x%llx\n",
1910 			      snic_cmd_tag(sc), sc, CMD_FLAGS(sc));
1911 
1912 		ret = FAILED;
1913 		goto dr_fini_end;
1914 	}
1915 
1916 	rqi->dr_done = NULL;
1917 
1918 	lr_res = CMD_LR_STATUS(sc);
1919 
1920 	switch (lr_res) {
1921 	case SNIC_INVALID_CODE:
1922 		/* stats */
1923 		SNIC_SCSI_DBG(snic->shost,
1924 			      "dr_fini: Tag %x Dev Reset Timedout. flags 0x%llx\n",
1925 			      snic_cmd_tag(sc), CMD_FLAGS(sc));
1926 
1927 		CMD_FLAGS(sc) |= SNIC_DEV_RST_TIMEDOUT;
1928 		ret = FAILED;
1929 
1930 		goto dr_failed;
1931 
1932 	case SNIC_STAT_IO_SUCCESS:
1933 		SNIC_SCSI_DBG(snic->shost,
1934 			      "dr_fini: Tag %x Dev Reset cmpl\n",
1935 			      snic_cmd_tag(sc));
1936 		ret = 0;
1937 		break;
1938 
1939 	default:
1940 		SNIC_HOST_ERR(snic->shost,
1941 			      "dr_fini:Device Reset completed& failed.Tag = %x lr_status %s flags 0x%llx\n",
1942 			      snic_cmd_tag(sc),
1943 			      snic_io_status_to_str(lr_res), CMD_FLAGS(sc));
1944 		ret = FAILED;
1945 		goto dr_failed;
1946 	}
1947 	spin_unlock_irqrestore(io_lock, flags);
1948 
1949 	/*
1950 	 * Cleanup any IOs on this LUN that have still not completed.
1951 	 * If any of these fail, then LUN Reset fails.
1952 	 * Cleanup cleans all commands on this LUN except
1953 	 * the lun reset command. If all cmds get cleaned, the LUN Reset
1954 	 * succeeds.
1955 	 */
1956 
1957 	ret = snic_dr_clean_pending_req(snic, sc);
1958 	if (ret) {
1959 		spin_lock_irqsave(io_lock, flags);
1960 		SNIC_SCSI_DBG(snic->shost,
1961 			      "dr_fini: Device Reset Failed since could not abort all IOs. Tag = %x.\n",
1962 			      snic_cmd_tag(sc));
1963 		rqi = (struct snic_req_info *) CMD_SP(sc);
1964 
1965 		goto dr_failed;
1966 	} else {
1967 		/* Cleanup LUN Reset Command */
1968 		spin_lock_irqsave(io_lock, flags);
1969 		rqi = (struct snic_req_info *) CMD_SP(sc);
1970 		if (rqi)
1971 			ret = SUCCESS; /* Completed Successfully */
1972 		else
1973 			ret = FAILED;
1974 	}
1975 
1976 dr_failed:
1977 	lockdep_assert_held(io_lock);
1978 	if (rqi)
1979 		CMD_SP(sc) = NULL;
1980 	spin_unlock_irqrestore(io_lock, flags);
1981 
1982 	if (rqi)
1983 		snic_release_req_buf(snic, rqi, sc);
1984 
1985 dr_fini_end:
1986 	return ret;
1987 } /* end of snic_dr_finish */
1988 
1989 static int
1990 snic_queue_dr_req(struct snic *snic,
1991 		  struct snic_req_info *rqi,
1992 		  struct scsi_cmnd *sc)
1993 {
1994 	/* Add special tag for device reset */
1995 	rqi->tm_tag |= SNIC_TAG_DEV_RST;
1996 
1997 	return snic_issue_tm_req(snic, rqi, sc, SNIC_ITMF_LUN_RESET);
1998 }
1999 
2000 static int
2001 snic_send_dr_and_wait(struct snic *snic, struct scsi_cmnd *sc)
2002 {
2003 	struct snic_req_info *rqi = NULL;
2004 	enum snic_ioreq_state sv_state;
2005 	spinlock_t *io_lock = NULL;
2006 	unsigned long flags;
2007 	DECLARE_COMPLETION_ONSTACK(tm_done);
2008 	int ret = FAILED, tag = snic_cmd_tag(sc);
2009 
2010 	io_lock = snic_io_lock_hash(snic, sc);
2011 	spin_lock_irqsave(io_lock, flags);
2012 	CMD_FLAGS(sc) |= SNIC_DEVICE_RESET;
2013 	rqi = (struct snic_req_info *) CMD_SP(sc);
2014 	if (!rqi) {
2015 		SNIC_HOST_ERR(snic->shost,
2016 			      "send_dr: rqi is null, Tag 0x%x flags 0x%llx\n",
2017 			      tag, CMD_FLAGS(sc));
2018 		spin_unlock_irqrestore(io_lock, flags);
2019 
2020 		ret = FAILED;
2021 		goto send_dr_end;
2022 	}
2023 
2024 	/* Save Command state to restore in case Queuing failed. */
2025 	sv_state = CMD_STATE(sc);
2026 
2027 	CMD_STATE(sc) = SNIC_IOREQ_LR_PENDING;
2028 	CMD_LR_STATUS(sc) = SNIC_INVALID_CODE;
2029 
2030 	SNIC_SCSI_DBG(snic->shost, "dr: TAG = %x\n", tag);
2031 
2032 	rqi->dr_done = &tm_done;
2033 	SNIC_BUG_ON(!rqi->dr_done);
2034 
2035 	spin_unlock_irqrestore(io_lock, flags);
2036 	/*
2037 	 * The Command state is changed to IOREQ_PENDING,
2038 	 * in this case, if the command is completed, the icmnd_cmpl will
2039 	 * mark the cmd as completed.
2040 	 * This logic still makes LUN Reset is inevitable.
2041 	 */
2042 
2043 	ret = snic_queue_dr_req(snic, rqi, sc);
2044 	if (ret) {
2045 		SNIC_HOST_ERR(snic->shost,
2046 			      "send_dr: IO w/ Tag 0x%x Failed err = %d. flags 0x%llx\n",
2047 			      tag, ret, CMD_FLAGS(sc));
2048 
2049 		spin_lock_irqsave(io_lock, flags);
2050 		/* Restore State */
2051 		CMD_STATE(sc) = sv_state;
2052 		rqi = (struct snic_req_info *) CMD_SP(sc);
2053 		if (rqi)
2054 			rqi->dr_done = NULL;
2055 		/* rqi is freed in caller. */
2056 		spin_unlock_irqrestore(io_lock, flags);
2057 		ret = FAILED;
2058 
2059 		goto send_dr_end;
2060 	}
2061 
2062 	spin_lock_irqsave(io_lock, flags);
2063 	CMD_FLAGS(sc) |= SNIC_DEV_RST_ISSUED;
2064 	spin_unlock_irqrestore(io_lock, flags);
2065 
2066 	ret = 0;
2067 
2068 	wait_for_completion_timeout(&tm_done, SNIC_LUN_RESET_TIMEOUT);
2069 
2070 send_dr_end:
2071 	return ret;
2072 }
2073 
2074 /*
2075  * auxillary funciton to check lun reset op is supported or not
2076  * Not supported if returns 0
2077  */
2078 static int
2079 snic_dev_reset_supported(struct scsi_device *sdev)
2080 {
2081 	struct snic_tgt *tgt = starget_to_tgt(scsi_target(sdev));
2082 
2083 	if (tgt->tdata.typ == SNIC_TGT_DAS)
2084 		return 0;
2085 
2086 	return 1;
2087 }
2088 
2089 static void
2090 snic_unlink_and_release_req(struct snic *snic, struct scsi_cmnd *sc, int flag)
2091 {
2092 	struct snic_req_info *rqi = NULL;
2093 	spinlock_t *io_lock = NULL;
2094 	unsigned long flags;
2095 	u32 start_time = jiffies;
2096 
2097 	io_lock = snic_io_lock_hash(snic, sc);
2098 	spin_lock_irqsave(io_lock, flags);
2099 	rqi = (struct snic_req_info *) CMD_SP(sc);
2100 	if (rqi) {
2101 		start_time = rqi->start_time;
2102 		CMD_SP(sc) = NULL;
2103 	}
2104 
2105 	CMD_FLAGS(sc) |= flag;
2106 	spin_unlock_irqrestore(io_lock, flags);
2107 
2108 	if (rqi)
2109 		snic_release_req_buf(snic, rqi, sc);
2110 
2111 	SNIC_TRC(snic->shost->host_no, snic_cmd_tag(sc), (ulong) sc,
2112 		 jiffies_to_msecs(jiffies - start_time), (ulong) rqi,
2113 		 SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
2114 }
2115 
2116 /*
2117  * SCSI Eh thread issues a LUN Reset when one or more commands on a LUN
2118  * fail to get aborted. It calls driver's eh_device_reset with a SCSI
2119  * command on the LUN.
2120  */
2121 int
2122 snic_device_reset(struct scsi_cmnd *sc)
2123 {
2124 	struct Scsi_Host *shost = sc->device->host;
2125 	struct snic *snic = shost_priv(shost);
2126 	struct snic_req_info *rqi = NULL;
2127 	int tag = snic_cmd_tag(sc);
2128 	int start_time = jiffies;
2129 	int ret = FAILED;
2130 	int dr_supp = 0;
2131 
2132 	SNIC_SCSI_DBG(shost, "dev_reset:sc %p :0x%x :req = %p :tag = %d\n",
2133 		      sc, sc->cmnd[0], scsi_cmd_to_rq(sc),
2134 		      snic_cmd_tag(sc));
2135 	dr_supp = snic_dev_reset_supported(sc->device);
2136 	if (!dr_supp) {
2137 		/* device reset op is not supported */
2138 		SNIC_HOST_INFO(shost, "LUN Reset Op not supported.\n");
2139 		snic_unlink_and_release_req(snic, sc, SNIC_DEV_RST_NOTSUP);
2140 
2141 		goto dev_rst_end;
2142 	}
2143 
2144 	if (unlikely(snic_get_state(snic) != SNIC_ONLINE)) {
2145 		snic_unlink_and_release_req(snic, sc, 0);
2146 		SNIC_HOST_ERR(shost, "Devrst: Parent Devs are not online.\n");
2147 
2148 		goto dev_rst_end;
2149 	}
2150 
2151 	/* There is no tag when lun reset is issue through ioctl. */
2152 	if (unlikely(tag <= SNIC_NO_TAG)) {
2153 		SNIC_HOST_INFO(snic->shost,
2154 			       "Devrst: LUN Reset Recvd thru IOCTL.\n");
2155 
2156 		rqi = snic_req_init(snic, 0);
2157 		if (!rqi)
2158 			goto dev_rst_end;
2159 
2160 		memset(scsi_cmd_priv(sc), 0,
2161 			sizeof(struct snic_internal_io_state));
2162 		CMD_SP(sc) = (char *)rqi;
2163 		CMD_FLAGS(sc) = SNIC_NO_FLAGS;
2164 
2165 		/* Add special tag for dr coming from user spc */
2166 		rqi->tm_tag = SNIC_TAG_IOCTL_DEV_RST;
2167 		rqi->sc = sc;
2168 	}
2169 
2170 	ret = snic_send_dr_and_wait(snic, sc);
2171 	if (ret) {
2172 		SNIC_HOST_ERR(snic->shost,
2173 			      "Devrst: IO w/ Tag %x Failed w/ err = %d\n",
2174 			      tag, ret);
2175 
2176 		snic_unlink_and_release_req(snic, sc, 0);
2177 
2178 		goto dev_rst_end;
2179 	}
2180 
2181 	ret = snic_dr_finish(snic, sc);
2182 
2183 dev_rst_end:
2184 	SNIC_TRC(snic->shost->host_no, tag, (ulong) sc,
2185 		 jiffies_to_msecs(jiffies - start_time),
2186 		 0, SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
2187 
2188 	SNIC_SCSI_DBG(snic->shost,
2189 		      "Devrst: Returning from Device Reset : %s\n",
2190 		      (ret == SUCCESS) ? "SUCCESS" : "FAILED");
2191 
2192 	return ret;
2193 } /* end of snic_device_reset */
2194 
2195 /*
2196  * SCSI Error handling calls driver's eh_host_reset if all prior
2197  * error handling levels return FAILED.
2198  *
2199  * Host Reset is the highest level of error recovery. If this fails, then
2200  * host is offlined by SCSI.
2201  */
2202 /*
2203  * snic_issue_hba_reset : Queues FW Reset Request.
2204  */
2205 static int
2206 snic_issue_hba_reset(struct snic *snic, struct scsi_cmnd *sc)
2207 {
2208 	struct snic_req_info *rqi = NULL;
2209 	struct snic_host_req *req = NULL;
2210 	spinlock_t *io_lock = NULL;
2211 	DECLARE_COMPLETION_ONSTACK(wait);
2212 	unsigned long flags;
2213 	int ret = -ENOMEM;
2214 
2215 	rqi = snic_req_init(snic, 0);
2216 	if (!rqi) {
2217 		ret = -ENOMEM;
2218 
2219 		goto hba_rst_end;
2220 	}
2221 
2222 	if (snic_cmd_tag(sc) == SCSI_NO_TAG) {
2223 		memset(scsi_cmd_priv(sc), 0,
2224 			sizeof(struct snic_internal_io_state));
2225 		SNIC_HOST_INFO(snic->shost, "issu_hr:Host reset thru ioctl.\n");
2226 		rqi->sc = sc;
2227 	}
2228 
2229 	req = rqi_to_req(rqi);
2230 
2231 	io_lock = snic_io_lock_hash(snic, sc);
2232 	spin_lock_irqsave(io_lock, flags);
2233 	SNIC_BUG_ON(CMD_SP(sc) != NULL);
2234 	CMD_STATE(sc) = SNIC_IOREQ_PENDING;
2235 	CMD_SP(sc) = (char *) rqi;
2236 	CMD_FLAGS(sc) |= SNIC_IO_INITIALIZED;
2237 	snic->remove_wait = &wait;
2238 	spin_unlock_irqrestore(io_lock, flags);
2239 
2240 	/* Initialize Request */
2241 	snic_io_hdr_enc(&req->hdr, SNIC_REQ_HBA_RESET, 0, snic_cmd_tag(sc),
2242 			snic->config.hid, 0, (ulong) rqi);
2243 
2244 	req->u.reset.flags = 0;
2245 
2246 	ret = snic_queue_wq_desc(snic, req, sizeof(*req));
2247 	if (ret) {
2248 		SNIC_HOST_ERR(snic->shost,
2249 			      "issu_hr:Queuing HBA Reset Failed. w err %d\n",
2250 			      ret);
2251 
2252 		goto hba_rst_err;
2253 	}
2254 
2255 	spin_lock_irqsave(io_lock, flags);
2256 	CMD_FLAGS(sc) |= SNIC_HOST_RESET_ISSUED;
2257 	spin_unlock_irqrestore(io_lock, flags);
2258 	atomic64_inc(&snic->s_stats.reset.hba_resets);
2259 	SNIC_HOST_INFO(snic->shost, "Queued HBA Reset Successfully.\n");
2260 
2261 	wait_for_completion_timeout(snic->remove_wait,
2262 				    SNIC_HOST_RESET_TIMEOUT);
2263 
2264 	if (snic_get_state(snic) == SNIC_FWRESET) {
2265 		SNIC_HOST_ERR(snic->shost, "reset_cmpl: Reset Timedout.\n");
2266 		ret = -ETIMEDOUT;
2267 
2268 		goto hba_rst_err;
2269 	}
2270 
2271 	spin_lock_irqsave(io_lock, flags);
2272 	snic->remove_wait = NULL;
2273 	rqi = (struct snic_req_info *) CMD_SP(sc);
2274 	CMD_SP(sc) = NULL;
2275 	spin_unlock_irqrestore(io_lock, flags);
2276 
2277 	if (rqi)
2278 		snic_req_free(snic, rqi);
2279 
2280 	ret = 0;
2281 
2282 	return ret;
2283 
2284 hba_rst_err:
2285 	spin_lock_irqsave(io_lock, flags);
2286 	snic->remove_wait = NULL;
2287 	rqi = (struct snic_req_info *) CMD_SP(sc);
2288 	CMD_SP(sc) = NULL;
2289 	spin_unlock_irqrestore(io_lock, flags);
2290 
2291 	if (rqi)
2292 		snic_req_free(snic, rqi);
2293 
2294 hba_rst_end:
2295 	SNIC_HOST_ERR(snic->shost,
2296 		      "reset:HBA Reset Failed w/ err = %d.\n",
2297 		      ret);
2298 
2299 	return ret;
2300 } /* end of snic_issue_hba_reset */
2301 
2302 int
2303 snic_reset(struct Scsi_Host *shost, struct scsi_cmnd *sc)
2304 {
2305 	struct snic *snic = shost_priv(shost);
2306 	enum snic_state sv_state;
2307 	unsigned long flags;
2308 	int ret = FAILED;
2309 
2310 	/* Set snic state as SNIC_FWRESET*/
2311 	sv_state = snic_get_state(snic);
2312 
2313 	spin_lock_irqsave(&snic->snic_lock, flags);
2314 	if (snic_get_state(snic) == SNIC_FWRESET) {
2315 		spin_unlock_irqrestore(&snic->snic_lock, flags);
2316 		SNIC_HOST_INFO(shost, "reset:prev reset is in progress\n");
2317 
2318 		msleep(SNIC_HOST_RESET_TIMEOUT);
2319 		ret = SUCCESS;
2320 
2321 		goto reset_end;
2322 	}
2323 
2324 	snic_set_state(snic, SNIC_FWRESET);
2325 	spin_unlock_irqrestore(&snic->snic_lock, flags);
2326 
2327 
2328 	/* Wait for all the IOs that are entered in Qcmd */
2329 	while (atomic_read(&snic->ios_inflight))
2330 		schedule_timeout(msecs_to_jiffies(1));
2331 
2332 	ret = snic_issue_hba_reset(snic, sc);
2333 	if (ret) {
2334 		SNIC_HOST_ERR(shost,
2335 			      "reset:Host Reset Failed w/ err %d.\n",
2336 			      ret);
2337 		spin_lock_irqsave(&snic->snic_lock, flags);
2338 		snic_set_state(snic, sv_state);
2339 		spin_unlock_irqrestore(&snic->snic_lock, flags);
2340 		atomic64_inc(&snic->s_stats.reset.hba_reset_fail);
2341 		ret = FAILED;
2342 
2343 		goto reset_end;
2344 	}
2345 
2346 	ret = SUCCESS;
2347 
2348 reset_end:
2349 	return ret;
2350 } /* end of snic_reset */
2351 
2352 /*
2353  * SCSI Error handling calls driver's eh_host_reset if all prior
2354  * error handling levels return FAILED.
2355  *
2356  * Host Reset is the highest level of error recovery. If this fails, then
2357  * host is offlined by SCSI.
2358  */
2359 int
2360 snic_host_reset(struct scsi_cmnd *sc)
2361 {
2362 	struct Scsi_Host *shost = sc->device->host;
2363 	u32 start_time  = jiffies;
2364 	int ret;
2365 
2366 	SNIC_SCSI_DBG(shost,
2367 		      "host reset:sc %p sc_cmd 0x%x req %p tag %d flags 0x%llx\n",
2368 		      sc, sc->cmnd[0], scsi_cmd_to_rq(sc),
2369 		      snic_cmd_tag(sc), CMD_FLAGS(sc));
2370 
2371 	ret = snic_reset(shost, sc);
2372 
2373 	SNIC_TRC(shost->host_no, snic_cmd_tag(sc), (ulong) sc,
2374 		 jiffies_to_msecs(jiffies - start_time),
2375 		 0, SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
2376 
2377 	return ret;
2378 } /* end of snic_host_reset */
2379 
2380 /*
2381  * snic_cmpl_pending_tmreq : Caller should hold io_lock
2382  */
2383 static void
2384 snic_cmpl_pending_tmreq(struct snic *snic, struct scsi_cmnd *sc)
2385 {
2386 	struct snic_req_info *rqi = NULL;
2387 
2388 	SNIC_SCSI_DBG(snic->shost,
2389 		      "Completing Pending TM Req sc %p, state %s flags 0x%llx\n",
2390 		      sc, snic_io_status_to_str(CMD_STATE(sc)), CMD_FLAGS(sc));
2391 
2392 	/*
2393 	 * CASE : FW didn't post itmf completion due to PCIe Errors.
2394 	 * Marking the abort status as Success to call scsi completion
2395 	 * in snic_abort_finish()
2396 	 */
2397 	CMD_ABTS_STATUS(sc) = SNIC_STAT_IO_SUCCESS;
2398 
2399 	rqi = (struct snic_req_info *) CMD_SP(sc);
2400 	if (!rqi)
2401 		return;
2402 
2403 	if (rqi->dr_done)
2404 		complete(rqi->dr_done);
2405 	else if (rqi->abts_done)
2406 		complete(rqi->abts_done);
2407 }
2408 
2409 /*
2410  * snic_scsi_cleanup: Walks through tag map and releases the reqs
2411  */
2412 static void
2413 snic_scsi_cleanup(struct snic *snic, int ex_tag)
2414 {
2415 	struct snic_req_info *rqi = NULL;
2416 	struct scsi_cmnd *sc = NULL;
2417 	spinlock_t *io_lock = NULL;
2418 	unsigned long flags;
2419 	int tag;
2420 	u64 st_time = 0;
2421 
2422 	SNIC_SCSI_DBG(snic->shost, "sc_clean: scsi cleanup.\n");
2423 
2424 	for (tag = 0; tag < snic->max_tag_id; tag++) {
2425 		/* Skip ex_tag */
2426 		if (tag == ex_tag)
2427 			continue;
2428 
2429 		io_lock = snic_io_lock_tag(snic, tag);
2430 		spin_lock_irqsave(io_lock, flags);
2431 		sc = scsi_host_find_tag(snic->shost, tag);
2432 		if (!sc) {
2433 			spin_unlock_irqrestore(io_lock, flags);
2434 
2435 			continue;
2436 		}
2437 
2438 		if (unlikely(snic_tmreq_pending(sc))) {
2439 			/*
2440 			 * When FW Completes reset w/o sending completions
2441 			 * for outstanding ios.
2442 			 */
2443 			snic_cmpl_pending_tmreq(snic, sc);
2444 			spin_unlock_irqrestore(io_lock, flags);
2445 
2446 			continue;
2447 		}
2448 
2449 		rqi = (struct snic_req_info *) CMD_SP(sc);
2450 		if (!rqi) {
2451 			spin_unlock_irqrestore(io_lock, flags);
2452 
2453 			goto cleanup;
2454 		}
2455 
2456 		SNIC_SCSI_DBG(snic->shost,
2457 			      "sc_clean: sc %p, rqi %p, tag %d flags 0x%llx\n",
2458 			      sc, rqi, tag, CMD_FLAGS(sc));
2459 
2460 		CMD_SP(sc) = NULL;
2461 		CMD_FLAGS(sc) |= SNIC_SCSI_CLEANUP;
2462 		spin_unlock_irqrestore(io_lock, flags);
2463 		st_time = rqi->start_time;
2464 
2465 		SNIC_HOST_INFO(snic->shost,
2466 			       "sc_clean: Releasing rqi %p : flags 0x%llx\n",
2467 			       rqi, CMD_FLAGS(sc));
2468 
2469 		snic_release_req_buf(snic, rqi, sc);
2470 
2471 cleanup:
2472 		sc->result = DID_TRANSPORT_DISRUPTED << 16;
2473 		SNIC_HOST_INFO(snic->shost,
2474 			       "sc_clean: DID_TRANSPORT_DISRUPTED for sc %p, Tag %d flags 0x%llx rqi %p duration %u msecs\n",
2475 			       sc, scsi_cmd_to_rq(sc)->tag, CMD_FLAGS(sc), rqi,
2476 			       jiffies_to_msecs(jiffies - st_time));
2477 
2478 		/* Update IO stats */
2479 		snic_stats_update_io_cmpl(&snic->s_stats);
2480 
2481 		SNIC_TRC(snic->shost->host_no, tag, (ulong) sc,
2482 			 jiffies_to_msecs(jiffies - st_time), 0,
2483 			 SNIC_TRC_CMD(sc),
2484 			 SNIC_TRC_CMD_STATE_FLAGS(sc));
2485 
2486 		scsi_done(sc);
2487 	}
2488 } /* end of snic_scsi_cleanup */
2489 
2490 void
2491 snic_shutdown_scsi_cleanup(struct snic *snic)
2492 {
2493 	SNIC_HOST_INFO(snic->shost, "Shutdown time SCSI Cleanup.\n");
2494 
2495 	snic_scsi_cleanup(snic, SCSI_NO_TAG);
2496 } /* end of snic_shutdown_scsi_cleanup */
2497 
2498 /*
2499  * snic_internal_abort_io
2500  * called by : snic_tgt_scsi_abort_io
2501  */
2502 static int
2503 snic_internal_abort_io(struct snic *snic, struct scsi_cmnd *sc, int tmf)
2504 {
2505 	struct snic_req_info *rqi = NULL;
2506 	spinlock_t *io_lock = NULL;
2507 	unsigned long flags;
2508 	u32 sv_state = 0;
2509 	int ret = 0;
2510 
2511 	io_lock = snic_io_lock_hash(snic, sc);
2512 	spin_lock_irqsave(io_lock, flags);
2513 	rqi = (struct snic_req_info *) CMD_SP(sc);
2514 	if (!rqi)
2515 		goto skip_internal_abts;
2516 
2517 	if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
2518 		goto skip_internal_abts;
2519 
2520 	if ((CMD_FLAGS(sc) & SNIC_DEVICE_RESET) &&
2521 		(!(CMD_FLAGS(sc) & SNIC_DEV_RST_ISSUED))) {
2522 
2523 		SNIC_SCSI_DBG(snic->shost,
2524 			      "internal_abts: dev rst not pending sc 0x%p\n",
2525 			      sc);
2526 
2527 		goto skip_internal_abts;
2528 	}
2529 
2530 
2531 	if (!(CMD_FLAGS(sc) & SNIC_IO_ISSUED)) {
2532 		SNIC_SCSI_DBG(snic->shost,
2533 			"internal_abts: IO not yet issued sc 0x%p tag 0x%x flags 0x%llx state %d\n",
2534 			sc, snic_cmd_tag(sc), CMD_FLAGS(sc), CMD_STATE(sc));
2535 
2536 		goto skip_internal_abts;
2537 	}
2538 
2539 	sv_state = CMD_STATE(sc);
2540 	CMD_STATE(sc) = SNIC_IOREQ_ABTS_PENDING;
2541 	CMD_ABTS_STATUS(sc) = SNIC_INVALID_CODE;
2542 	CMD_FLAGS(sc) |= SNIC_IO_INTERNAL_TERM_PENDING;
2543 
2544 	if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET) {
2545 		/* stats */
2546 		rqi->tm_tag = SNIC_TAG_DEV_RST;
2547 		SNIC_SCSI_DBG(snic->shost, "internal_abts:dev rst sc %p\n", sc);
2548 	}
2549 
2550 	SNIC_SCSI_DBG(snic->shost, "internal_abts: Issuing abts tag %x\n",
2551 		      snic_cmd_tag(sc));
2552 	SNIC_BUG_ON(rqi->abts_done);
2553 	spin_unlock_irqrestore(io_lock, flags);
2554 
2555 	ret = snic_queue_abort_req(snic, rqi, sc, tmf);
2556 	if (ret) {
2557 		SNIC_HOST_ERR(snic->shost,
2558 			      "internal_abts: Tag = %x , Failed w/ err = %d\n",
2559 			      snic_cmd_tag(sc), ret);
2560 
2561 		spin_lock_irqsave(io_lock, flags);
2562 
2563 		if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
2564 			CMD_STATE(sc) = sv_state;
2565 
2566 		goto skip_internal_abts;
2567 	}
2568 
2569 	spin_lock_irqsave(io_lock, flags);
2570 	if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET)
2571 		CMD_FLAGS(sc) |= SNIC_DEV_RST_TERM_ISSUED;
2572 	else
2573 		CMD_FLAGS(sc) |= SNIC_IO_INTERNAL_TERM_ISSUED;
2574 
2575 	ret = SUCCESS;
2576 
2577 skip_internal_abts:
2578 	lockdep_assert_held(io_lock);
2579 	spin_unlock_irqrestore(io_lock, flags);
2580 
2581 	return ret;
2582 } /* end of snic_internal_abort_io */
2583 
2584 /*
2585  * snic_tgt_scsi_abort_io : called by snic_tgt_del
2586  */
2587 int
2588 snic_tgt_scsi_abort_io(struct snic_tgt *tgt)
2589 {
2590 	struct snic *snic = NULL;
2591 	struct scsi_cmnd *sc = NULL;
2592 	struct snic_tgt *sc_tgt = NULL;
2593 	spinlock_t *io_lock = NULL;
2594 	unsigned long flags;
2595 	int ret = 0, tag, abt_cnt = 0, tmf = 0;
2596 
2597 	if (!tgt)
2598 		return -1;
2599 
2600 	snic = shost_priv(snic_tgt_to_shost(tgt));
2601 	SNIC_SCSI_DBG(snic->shost, "tgt_abt_io: Cleaning Pending IOs.\n");
2602 
2603 	if (tgt->tdata.typ == SNIC_TGT_DAS)
2604 		tmf = SNIC_ITMF_ABTS_TASK;
2605 	else
2606 		tmf = SNIC_ITMF_ABTS_TASK_TERM;
2607 
2608 	for (tag = 0; tag < snic->max_tag_id; tag++) {
2609 		io_lock = snic_io_lock_tag(snic, tag);
2610 
2611 		spin_lock_irqsave(io_lock, flags);
2612 		sc = scsi_host_find_tag(snic->shost, tag);
2613 		if (!sc) {
2614 			spin_unlock_irqrestore(io_lock, flags);
2615 
2616 			continue;
2617 		}
2618 
2619 		sc_tgt = starget_to_tgt(scsi_target(sc->device));
2620 		if (sc_tgt != tgt) {
2621 			spin_unlock_irqrestore(io_lock, flags);
2622 
2623 			continue;
2624 		}
2625 		spin_unlock_irqrestore(io_lock, flags);
2626 
2627 		ret = snic_internal_abort_io(snic, sc, tmf);
2628 		if (ret < 0) {
2629 			SNIC_HOST_ERR(snic->shost,
2630 				      "tgt_abt_io: Tag %x, Failed w err = %d\n",
2631 				      tag, ret);
2632 
2633 			continue;
2634 		}
2635 
2636 		if (ret == SUCCESS)
2637 			abt_cnt++;
2638 	}
2639 
2640 	SNIC_SCSI_DBG(snic->shost, "tgt_abt_io: abt_cnt = %d\n", abt_cnt);
2641 
2642 	return 0;
2643 } /* end of snic_tgt_scsi_abort_io */
2644