xref: /openbmc/linux/drivers/scsi/snic/snic_scsi.c (revision 293d5b43)
1 /*
2  * Copyright 2014 Cisco Systems, Inc.  All rights reserved.
3  *
4  * This program is free software; you may redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
9  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
10  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
11  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
12  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
13  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
14  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
15  * SOFTWARE.
16  */
17 
18 #include <linux/mempool.h>
19 #include <linux/errno.h>
20 #include <linux/init.h>
21 #include <linux/workqueue.h>
22 #include <linux/pci.h>
23 #include <linux/spinlock.h>
24 #include <linux/delay.h>
25 #include <linux/gfp.h>
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_host.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_tcq.h>
31 #include <scsi/scsi_dbg.h>
32 
33 #include "snic_io.h"
34 #include "snic.h"
35 
36 #define snic_cmd_tag(sc)	(((struct scsi_cmnd *) sc)->request->tag)
37 
38 const char *snic_state_str[] = {
39 	[SNIC_INIT]	= "SNIC_INIT",
40 	[SNIC_ERROR]	= "SNIC_ERROR",
41 	[SNIC_ONLINE]	= "SNIC_ONLINE",
42 	[SNIC_OFFLINE]	= "SNIC_OFFLINE",
43 	[SNIC_FWRESET]	= "SNIC_FWRESET",
44 };
45 
46 static const char * const snic_req_state_str[] = {
47 	[SNIC_IOREQ_NOT_INITED]	= "SNIC_IOREQ_NOT_INITED",
48 	[SNIC_IOREQ_PENDING]	= "SNIC_IOREQ_PENDING",
49 	[SNIC_IOREQ_ABTS_PENDING] = "SNIC_IOREQ_ABTS_PENDING",
50 	[SNIC_IOREQ_ABTS_COMPLETE] = "SNIC_IOREQ_ABTS_COMPELTE",
51 	[SNIC_IOREQ_LR_PENDING]	= "SNIC_IOREQ_LR_PENDING",
52 	[SNIC_IOREQ_LR_COMPLETE] = "SNIC_IOREQ_LR_COMPELTE",
53 	[SNIC_IOREQ_COMPLETE]	= "SNIC_IOREQ_CMD_COMPELTE",
54 };
55 
56 /* snic cmd status strings */
57 static const char * const snic_io_status_str[] = {
58 	[SNIC_STAT_IO_SUCCESS]	= "SNIC_STAT_IO_SUCCESS", /* 0x0 */
59 	[SNIC_STAT_INVALID_HDR] = "SNIC_STAT_INVALID_HDR",
60 	[SNIC_STAT_OUT_OF_RES]	= "SNIC_STAT_OUT_OF_RES",
61 	[SNIC_STAT_INVALID_PARM] = "SNIC_STAT_INVALID_PARM",
62 	[SNIC_STAT_REQ_NOT_SUP]	= "SNIC_STAT_REQ_NOT_SUP",
63 	[SNIC_STAT_IO_NOT_FOUND] = "SNIC_STAT_IO_NOT_FOUND",
64 	[SNIC_STAT_ABORTED]	= "SNIC_STAT_ABORTED",
65 	[SNIC_STAT_TIMEOUT]	= "SNIC_STAT_TIMEOUT",
66 	[SNIC_STAT_SGL_INVALID] = "SNIC_STAT_SGL_INVALID",
67 	[SNIC_STAT_DATA_CNT_MISMATCH] = "SNIC_STAT_DATA_CNT_MISMATCH",
68 	[SNIC_STAT_FW_ERR]	= "SNIC_STAT_FW_ERR",
69 	[SNIC_STAT_ITMF_REJECT] = "SNIC_STAT_ITMF_REJECT",
70 	[SNIC_STAT_ITMF_FAIL]	= "SNIC_STAT_ITMF_FAIL",
71 	[SNIC_STAT_ITMF_INCORRECT_LUN] = "SNIC_STAT_ITMF_INCORRECT_LUN",
72 	[SNIC_STAT_CMND_REJECT] = "SNIC_STAT_CMND_REJECT",
73 	[SNIC_STAT_DEV_OFFLINE] = "SNIC_STAT_DEV_OFFLINE",
74 	[SNIC_STAT_NO_BOOTLUN]	= "SNIC_STAT_NO_BOOTLUN",
75 	[SNIC_STAT_SCSI_ERR]	= "SNIC_STAT_SCSI_ERR",
76 	[SNIC_STAT_NOT_READY]	= "SNIC_STAT_NOT_READY",
77 	[SNIC_STAT_FATAL_ERROR]	= "SNIC_STAT_FATAL_ERROR",
78 };
79 
80 static void snic_scsi_cleanup(struct snic *, int);
81 
82 const char *
83 snic_state_to_str(unsigned int state)
84 {
85 	if (state >= ARRAY_SIZE(snic_state_str) || !snic_state_str[state])
86 		return "Unknown";
87 
88 	return snic_state_str[state];
89 }
90 
91 static const char *
92 snic_io_status_to_str(unsigned int state)
93 {
94 	if ((state >= ARRAY_SIZE(snic_io_status_str)) ||
95 	     (!snic_io_status_str[state]))
96 		return "Unknown";
97 
98 	return snic_io_status_str[state];
99 }
100 
101 static const char *
102 snic_ioreq_state_to_str(unsigned int state)
103 {
104 	if (state >= ARRAY_SIZE(snic_req_state_str) ||
105 			!snic_req_state_str[state])
106 		return "Unknown";
107 
108 	return snic_req_state_str[state];
109 }
110 
111 static inline spinlock_t *
112 snic_io_lock_hash(struct snic *snic, struct scsi_cmnd *sc)
113 {
114 	u32 hash = snic_cmd_tag(sc) & (SNIC_IO_LOCKS - 1);
115 
116 	return &snic->io_req_lock[hash];
117 }
118 
119 static inline spinlock_t *
120 snic_io_lock_tag(struct snic *snic, int tag)
121 {
122 	return &snic->io_req_lock[tag & (SNIC_IO_LOCKS - 1)];
123 }
124 
125 /* snic_release_req_buf : Releases snic_req_info */
126 static void
127 snic_release_req_buf(struct snic *snic,
128 		   struct snic_req_info *rqi,
129 		   struct scsi_cmnd *sc)
130 {
131 	struct snic_host_req *req = rqi_to_req(rqi);
132 
133 	/* Freeing cmd without marking completion, not okay */
134 	SNIC_BUG_ON(!((CMD_STATE(sc) == SNIC_IOREQ_COMPLETE) ||
135 		      (CMD_STATE(sc) == SNIC_IOREQ_ABTS_COMPLETE) ||
136 		      (CMD_FLAGS(sc) & SNIC_DEV_RST_NOTSUP) ||
137 		      (CMD_FLAGS(sc) & SNIC_IO_INTERNAL_TERM_ISSUED) ||
138 		      (CMD_FLAGS(sc) & SNIC_DEV_RST_TERM_ISSUED) ||
139 		      (CMD_FLAGS(sc) & SNIC_SCSI_CLEANUP) ||
140 		      (CMD_STATE(sc) == SNIC_IOREQ_LR_COMPLETE)));
141 
142 	SNIC_SCSI_DBG(snic->shost,
143 		      "Rel_req:sc %p:tag %x:rqi %p:ioreq %p:abt %p:dr %p: state %s:flags 0x%llx\n",
144 		      sc, snic_cmd_tag(sc), rqi, rqi->req, rqi->abort_req,
145 		      rqi->dr_req, snic_ioreq_state_to_str(CMD_STATE(sc)),
146 		      CMD_FLAGS(sc));
147 
148 	if (req->u.icmnd.sense_addr)
149 		pci_unmap_single(snic->pdev,
150 				 le64_to_cpu(req->u.icmnd.sense_addr),
151 				 SCSI_SENSE_BUFFERSIZE,
152 				 PCI_DMA_FROMDEVICE);
153 
154 	scsi_dma_unmap(sc);
155 
156 	snic_req_free(snic, rqi);
157 } /* end of snic_release_req_buf */
158 
159 /*
160  * snic_queue_icmnd_req : Queues snic_icmnd request
161  */
162 static int
163 snic_queue_icmnd_req(struct snic *snic,
164 		     struct snic_req_info *rqi,
165 		     struct scsi_cmnd *sc,
166 		     int sg_cnt)
167 {
168 	struct scatterlist *sg;
169 	struct snic_sg_desc *sgd;
170 	dma_addr_t pa = 0;
171 	struct scsi_lun lun;
172 	u16 flags = 0;
173 	int ret = 0;
174 	unsigned int i;
175 
176 	if (sg_cnt) {
177 		flags = SNIC_ICMND_ESGL;
178 		sgd = (struct snic_sg_desc *) req_to_sgl(rqi->req);
179 
180 		for_each_sg(scsi_sglist(sc), sg, sg_cnt, i) {
181 			sgd->addr = cpu_to_le64(sg_dma_address(sg));
182 			sgd->len = cpu_to_le32(sg_dma_len(sg));
183 			sgd->_resvd = 0;
184 			sgd++;
185 		}
186 	}
187 
188 	pa = pci_map_single(snic->pdev,
189 			    sc->sense_buffer,
190 			    SCSI_SENSE_BUFFERSIZE,
191 			    PCI_DMA_FROMDEVICE);
192 
193 	if (pci_dma_mapping_error(snic->pdev, pa)) {
194 		SNIC_HOST_ERR(snic->shost,
195 			      "QIcmnd:PCI Map Failed for sns buf %p tag %x\n",
196 			      sc->sense_buffer, snic_cmd_tag(sc));
197 		ret = -ENOMEM;
198 
199 		return ret;
200 	}
201 
202 	int_to_scsilun(sc->device->lun, &lun);
203 	if (sc->sc_data_direction == DMA_FROM_DEVICE)
204 		flags |= SNIC_ICMND_RD;
205 	if (sc->sc_data_direction == DMA_TO_DEVICE)
206 		flags |= SNIC_ICMND_WR;
207 
208 	/* Initialize icmnd */
209 	snic_icmnd_init(rqi->req,
210 			snic_cmd_tag(sc),
211 			snic->config.hid, /* hid */
212 			(ulong) rqi,
213 			flags, /* command flags */
214 			rqi->tgt_id,
215 			lun.scsi_lun,
216 			sc->cmnd,
217 			sc->cmd_len,
218 			scsi_bufflen(sc),
219 			sg_cnt,
220 			(ulong) req_to_sgl(rqi->req),
221 			pa, /* sense buffer pa */
222 			SCSI_SENSE_BUFFERSIZE);
223 
224 	atomic64_inc(&snic->s_stats.io.active);
225 	ret = snic_queue_wq_desc(snic, rqi->req, rqi->req_len);
226 	if (ret) {
227 		atomic64_dec(&snic->s_stats.io.active);
228 		SNIC_HOST_ERR(snic->shost,
229 			      "QIcmnd: Queuing Icmnd Failed. ret = %d\n",
230 			      ret);
231 	} else
232 		snic_stats_update_active_ios(&snic->s_stats);
233 
234 	return ret;
235 } /* end of snic_queue_icmnd_req */
236 
237 /*
238  * snic_issue_scsi_req : Prepares IO request and Issues to FW.
239  */
240 static int
241 snic_issue_scsi_req(struct snic *snic,
242 		      struct snic_tgt *tgt,
243 		      struct scsi_cmnd *sc)
244 {
245 	struct snic_req_info *rqi = NULL;
246 	int sg_cnt = 0;
247 	int ret = 0;
248 	u32 tag = snic_cmd_tag(sc);
249 	u64 cmd_trc = 0, cmd_st_flags = 0;
250 	spinlock_t *io_lock = NULL;
251 	unsigned long flags;
252 
253 	CMD_STATE(sc) = SNIC_IOREQ_NOT_INITED;
254 	CMD_FLAGS(sc) = SNIC_NO_FLAGS;
255 	sg_cnt = scsi_dma_map(sc);
256 	if (sg_cnt < 0) {
257 		SNIC_TRC((u16)snic->shost->host_no, tag, (ulong) sc, 0,
258 			 sc->cmnd[0], sg_cnt, CMD_STATE(sc));
259 
260 		SNIC_HOST_ERR(snic->shost, "issue_sc:Failed to map SG List.\n");
261 		ret = -ENOMEM;
262 
263 		goto issue_sc_end;
264 	}
265 
266 	rqi = snic_req_init(snic, sg_cnt);
267 	if (!rqi) {
268 		scsi_dma_unmap(sc);
269 		ret = -ENOMEM;
270 
271 		goto issue_sc_end;
272 	}
273 
274 	rqi->tgt_id = tgt->id;
275 	rqi->sc = sc;
276 
277 	CMD_STATE(sc) = SNIC_IOREQ_PENDING;
278 	CMD_SP(sc) = (char *) rqi;
279 	cmd_trc = SNIC_TRC_CMD(sc);
280 	CMD_FLAGS(sc) |= (SNIC_IO_INITIALIZED | SNIC_IO_ISSUED);
281 	cmd_st_flags = SNIC_TRC_CMD_STATE_FLAGS(sc);
282 	io_lock = snic_io_lock_hash(snic, sc);
283 
284 	/* create wq desc and enqueue it */
285 	ret = snic_queue_icmnd_req(snic, rqi, sc, sg_cnt);
286 	if (ret) {
287 		SNIC_HOST_ERR(snic->shost,
288 			      "issue_sc: icmnd qing Failed for sc %p, err %d\n",
289 			      sc, ret);
290 
291 		spin_lock_irqsave(io_lock, flags);
292 		rqi = (struct snic_req_info *) CMD_SP(sc);
293 		CMD_SP(sc) = NULL;
294 		CMD_STATE(sc) = SNIC_IOREQ_COMPLETE;
295 		CMD_FLAGS(sc) &= ~SNIC_IO_ISSUED; /* turn off the flag */
296 		spin_unlock_irqrestore(io_lock, flags);
297 
298 		if (rqi)
299 			snic_release_req_buf(snic, rqi, sc);
300 
301 		SNIC_TRC(snic->shost->host_no, tag, (ulong) sc, 0, 0, 0,
302 			 SNIC_TRC_CMD_STATE_FLAGS(sc));
303 	} else {
304 		u32 io_sz = scsi_bufflen(sc) >> 9;
305 		u32 qtime = jiffies - rqi->start_time;
306 		struct snic_io_stats *iostats = &snic->s_stats.io;
307 
308 		if (io_sz > atomic64_read(&iostats->max_io_sz))
309 			atomic64_set(&iostats->max_io_sz, io_sz);
310 
311 		if (qtime > atomic64_read(&iostats->max_qtime))
312 			atomic64_set(&iostats->max_qtime, qtime);
313 
314 		SNIC_SCSI_DBG(snic->shost,
315 			      "issue_sc:sc %p, tag %d queued to WQ.\n",
316 			      sc, tag);
317 
318 		SNIC_TRC(snic->shost->host_no, tag, (ulong) sc, (ulong) rqi,
319 			 sg_cnt, cmd_trc, cmd_st_flags);
320 	}
321 
322 issue_sc_end:
323 
324 	return ret;
325 } /* end of snic_issue_scsi_req */
326 
327 
328 /*
329  * snic_queuecommand
330  * Routine to send a scsi cdb to LLD
331  * Called with host_lock held and interrupts disabled
332  */
333 int
334 snic_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *sc)
335 {
336 	struct snic_tgt *tgt = NULL;
337 	struct snic *snic = shost_priv(shost);
338 	int ret;
339 
340 	tgt = starget_to_tgt(scsi_target(sc->device));
341 	ret = snic_tgt_chkready(tgt);
342 	if (ret) {
343 		SNIC_HOST_ERR(shost, "Tgt %p id %d Not Ready.\n", tgt, tgt->id);
344 		atomic64_inc(&snic->s_stats.misc.tgt_not_rdy);
345 		sc->result = ret;
346 		sc->scsi_done(sc);
347 
348 		return 0;
349 	}
350 
351 	if (snic_get_state(snic) != SNIC_ONLINE) {
352 		SNIC_HOST_ERR(shost, "snic state is %s\n",
353 			      snic_state_str[snic_get_state(snic)]);
354 
355 		return SCSI_MLQUEUE_HOST_BUSY;
356 	}
357 	atomic_inc(&snic->ios_inflight);
358 
359 	SNIC_SCSI_DBG(shost, "sc %p Tag %d (sc %0x) lun %lld in snic_qcmd\n",
360 		      sc, snic_cmd_tag(sc), sc->cmnd[0], sc->device->lun);
361 
362 	memset(scsi_cmd_priv(sc), 0, sizeof(struct snic_internal_io_state));
363 
364 	ret = snic_issue_scsi_req(snic, tgt, sc);
365 	if (ret) {
366 		SNIC_HOST_ERR(shost, "Failed to Q, Scsi Req w/ err %d.\n", ret);
367 		ret = SCSI_MLQUEUE_HOST_BUSY;
368 	}
369 
370 	atomic_dec(&snic->ios_inflight);
371 
372 	return ret;
373 } /* end of snic_queuecommand */
374 
375 /*
376  * snic_process_abts_pending_state:
377  * caller should hold IO lock
378  */
379 static void
380 snic_proc_tmreq_pending_state(struct snic *snic,
381 			      struct scsi_cmnd *sc,
382 			      u8 cmpl_status)
383 {
384 	int state = CMD_STATE(sc);
385 
386 	if (state == SNIC_IOREQ_ABTS_PENDING)
387 		CMD_FLAGS(sc) |= SNIC_IO_ABTS_PENDING;
388 	else if (state == SNIC_IOREQ_LR_PENDING)
389 		CMD_FLAGS(sc) |= SNIC_DEV_RST_PENDING;
390 	else
391 		SNIC_BUG_ON(1);
392 
393 	switch (cmpl_status) {
394 	case SNIC_STAT_IO_SUCCESS:
395 		CMD_FLAGS(sc) |= SNIC_IO_DONE;
396 		break;
397 
398 	case SNIC_STAT_ABORTED:
399 		CMD_FLAGS(sc) |= SNIC_IO_ABORTED;
400 		break;
401 
402 	default:
403 		SNIC_BUG_ON(1);
404 	}
405 }
406 
407 /*
408  * snic_process_io_failed_state:
409  * Processes IO's error states
410  */
411 static void
412 snic_process_io_failed_state(struct snic *snic,
413 			     struct snic_icmnd_cmpl *icmnd_cmpl,
414 			     struct scsi_cmnd *sc,
415 			     u8 cmpl_stat)
416 {
417 	int res = 0;
418 
419 	switch (cmpl_stat) {
420 	case SNIC_STAT_TIMEOUT:		/* Req was timedout */
421 		atomic64_inc(&snic->s_stats.misc.io_tmo);
422 		res = DID_TIME_OUT;
423 		break;
424 
425 	case SNIC_STAT_ABORTED:		/* Req was aborted */
426 		atomic64_inc(&snic->s_stats.misc.io_aborted);
427 		res = DID_ABORT;
428 		break;
429 
430 	case SNIC_STAT_DATA_CNT_MISMATCH:/* Recv/Sent more/less data than exp */
431 		atomic64_inc(&snic->s_stats.misc.data_cnt_mismat);
432 		scsi_set_resid(sc, le32_to_cpu(icmnd_cmpl->resid));
433 		res = DID_ERROR;
434 		break;
435 
436 	case SNIC_STAT_OUT_OF_RES: /* Out of resources to complete request */
437 		atomic64_inc(&snic->s_stats.fw.out_of_res);
438 		res = DID_REQUEUE;
439 		break;
440 
441 	case SNIC_STAT_IO_NOT_FOUND:	/* Requested I/O was not found */
442 		atomic64_inc(&snic->s_stats.io.io_not_found);
443 		res = DID_ERROR;
444 		break;
445 
446 	case SNIC_STAT_SGL_INVALID:	/* Req was aborted to due to sgl error*/
447 		atomic64_inc(&snic->s_stats.misc.sgl_inval);
448 		res = DID_ERROR;
449 		break;
450 
451 	case SNIC_STAT_FW_ERR:		/* Req terminated due to FW Error */
452 		atomic64_inc(&snic->s_stats.fw.io_errs);
453 		res = DID_ERROR;
454 		break;
455 
456 	case SNIC_STAT_SCSI_ERR:	/* FW hits SCSI Error */
457 		atomic64_inc(&snic->s_stats.fw.scsi_errs);
458 		break;
459 
460 	case SNIC_STAT_NOT_READY:	/* XPT yet to initialize */
461 	case SNIC_STAT_DEV_OFFLINE:	/* Device offline */
462 		res = DID_NO_CONNECT;
463 		break;
464 
465 	case SNIC_STAT_INVALID_HDR:	/* Hdr contains invalid data */
466 	case SNIC_STAT_INVALID_PARM:	/* Some param in req is invalid */
467 	case SNIC_STAT_REQ_NOT_SUP:	/* Req type is not supported */
468 	case SNIC_STAT_CMND_REJECT:	/* Req rejected */
469 	case SNIC_STAT_FATAL_ERROR:	/* XPT Error */
470 	default:
471 		SNIC_SCSI_DBG(snic->shost,
472 			      "Invalid Hdr/Param or Req Not Supported or Cmnd Rejected or Device Offline. or Unknown\n");
473 		res = DID_ERROR;
474 		break;
475 	}
476 
477 	SNIC_HOST_ERR(snic->shost, "fw returns failed status %s flags 0x%llx\n",
478 		      snic_io_status_to_str(cmpl_stat), CMD_FLAGS(sc));
479 
480 	/* Set sc->result */
481 	sc->result = (res << 16) | icmnd_cmpl->scsi_status;
482 } /* end of snic_process_io_failed_state */
483 
484 /*
485  * snic_tmreq_pending : is task management in progress.
486  */
487 static int
488 snic_tmreq_pending(struct scsi_cmnd *sc)
489 {
490 	int state = CMD_STATE(sc);
491 
492 	return ((state == SNIC_IOREQ_ABTS_PENDING) ||
493 			(state == SNIC_IOREQ_LR_PENDING));
494 }
495 
496 /*
497  * snic_process_icmnd_cmpl_status:
498  * Caller should hold io_lock
499  */
500 static int
501 snic_process_icmnd_cmpl_status(struct snic *snic,
502 			       struct snic_icmnd_cmpl *icmnd_cmpl,
503 			       u8 cmpl_stat,
504 			       struct scsi_cmnd *sc)
505 {
506 	u8 scsi_stat = icmnd_cmpl->scsi_status;
507 	u64 xfer_len = 0;
508 	int ret = 0;
509 
510 	/* Mark the IO as complete */
511 	CMD_STATE(sc) = SNIC_IOREQ_COMPLETE;
512 
513 	if (likely(cmpl_stat == SNIC_STAT_IO_SUCCESS)) {
514 		sc->result = (DID_OK << 16) | scsi_stat;
515 
516 		xfer_len = scsi_bufflen(sc);
517 
518 		/* Update SCSI Cmd with resid value */
519 		scsi_set_resid(sc, le32_to_cpu(icmnd_cmpl->resid));
520 
521 		if (icmnd_cmpl->flags & SNIC_ICMND_CMPL_UNDR_RUN) {
522 			xfer_len -= le32_to_cpu(icmnd_cmpl->resid);
523 			atomic64_inc(&snic->s_stats.misc.io_under_run);
524 		}
525 
526 		if (icmnd_cmpl->scsi_status == SAM_STAT_TASK_SET_FULL)
527 			atomic64_inc(&snic->s_stats.misc.qfull);
528 
529 		ret = 0;
530 	} else {
531 		snic_process_io_failed_state(snic, icmnd_cmpl, sc, cmpl_stat);
532 		atomic64_inc(&snic->s_stats.io.fail);
533 		SNIC_HOST_ERR(snic->shost,
534 			      "icmnd_cmpl: IO Failed : Hdr Status %s flags 0x%llx\n",
535 			      snic_io_status_to_str(cmpl_stat), CMD_FLAGS(sc));
536 		ret = 1;
537 	}
538 
539 	return ret;
540 } /* end of snic_process_icmnd_cmpl_status */
541 
542 
543 /*
544  * snic_icmnd_cmpl_handler
545  * Routine to handle icmnd completions
546  */
547 static void
548 snic_icmnd_cmpl_handler(struct snic *snic, struct snic_fw_req *fwreq)
549 {
550 	u8 typ, hdr_stat;
551 	u32 cmnd_id, hid;
552 	ulong ctx;
553 	struct scsi_cmnd *sc = NULL;
554 	struct snic_icmnd_cmpl *icmnd_cmpl = NULL;
555 	struct snic_host_req *req = NULL;
556 	struct snic_req_info *rqi = NULL;
557 	unsigned long flags, start_time;
558 	spinlock_t *io_lock;
559 	u8 sc_stat = 0;
560 
561 	snic_io_hdr_dec(&fwreq->hdr, &typ, &hdr_stat, &cmnd_id, &hid, &ctx);
562 	icmnd_cmpl = &fwreq->u.icmnd_cmpl;
563 	sc_stat = icmnd_cmpl->scsi_status;
564 
565 	SNIC_SCSI_DBG(snic->shost,
566 		      "Icmnd_cmpl: type = %x, hdr_stat = %x, cmnd_id = %x, hid = %x,i ctx = %lx\n",
567 		      typ, hdr_stat, cmnd_id, hid, ctx);
568 
569 	if (cmnd_id >= snic->max_tag_id) {
570 		SNIC_HOST_ERR(snic->shost,
571 			      "Icmnd_cmpl:Tag Error:Out of Range Tag %d, hdr status = %s\n",
572 			      cmnd_id, snic_io_status_to_str(hdr_stat));
573 		return;
574 	}
575 
576 	sc = scsi_host_find_tag(snic->shost, cmnd_id);
577 	WARN_ON_ONCE(!sc);
578 
579 	if (!sc) {
580 		atomic64_inc(&snic->s_stats.io.sc_null);
581 		SNIC_HOST_ERR(snic->shost,
582 			      "Icmnd_cmpl: Scsi Cmnd Not found, sc = NULL Hdr Status = %s tag = 0x%x fwreq = 0x%p\n",
583 			      snic_io_status_to_str(hdr_stat),
584 			      cmnd_id,
585 			      fwreq);
586 
587 		SNIC_TRC(snic->shost->host_no, cmnd_id, 0,
588 			 ((u64)hdr_stat << 16 |
589 			  (u64)sc_stat << 8 | (u64)icmnd_cmpl->flags),
590 			 (ulong) fwreq, le32_to_cpu(icmnd_cmpl->resid), ctx);
591 
592 		return;
593 	}
594 
595 	io_lock = snic_io_lock_hash(snic, sc);
596 
597 	spin_lock_irqsave(io_lock, flags);
598 	rqi = (struct snic_req_info *) CMD_SP(sc);
599 	SNIC_SCSI_DBG(snic->shost,
600 		      "Icmnd_cmpl:lun %lld sc %p cmd %xtag %d flags 0x%llx rqi %p\n",
601 		      sc->device->lun, sc, sc->cmnd[0], snic_cmd_tag(sc),
602 		      CMD_FLAGS(sc), rqi);
603 
604 	if (CMD_FLAGS(sc) & SNIC_HOST_RESET_CMD_TERM) {
605 		spin_unlock_irqrestore(io_lock, flags);
606 
607 		return;
608 	}
609 
610 	SNIC_BUG_ON(rqi != (struct snic_req_info *)ctx);
611 	WARN_ON_ONCE(req);
612 	if (!rqi) {
613 		atomic64_inc(&snic->s_stats.io.req_null);
614 		CMD_FLAGS(sc) |= SNIC_IO_REQ_NULL;
615 		spin_unlock_irqrestore(io_lock, flags);
616 
617 		SNIC_HOST_ERR(snic->shost,
618 			      "Icmnd_cmpl:Host Req Not Found(null), Hdr Status %s, Tag 0x%x, sc 0x%p flags 0x%llx\n",
619 			      snic_io_status_to_str(hdr_stat),
620 			      cmnd_id, sc, CMD_FLAGS(sc));
621 		return;
622 	}
623 
624 	rqi = (struct snic_req_info *) ctx;
625 	start_time = rqi->start_time;
626 
627 	/* firmware completed the io */
628 	rqi->io_cmpl = 1;
629 
630 	/*
631 	 * if SCSI-ML has already issued abort on this command,
632 	 * ignore completion of the IO. The abts path will clean it up
633 	 */
634 	if (unlikely(snic_tmreq_pending(sc))) {
635 		snic_proc_tmreq_pending_state(snic, sc, hdr_stat);
636 		spin_unlock_irqrestore(io_lock, flags);
637 
638 		snic_stats_update_io_cmpl(&snic->s_stats);
639 
640 		/* Expected value is SNIC_STAT_ABORTED */
641 		if (likely(hdr_stat == SNIC_STAT_ABORTED))
642 			return;
643 
644 		SNIC_SCSI_DBG(snic->shost,
645 			      "icmnd_cmpl:TM Req Pending(%s), Hdr Status %s sc 0x%p scsi status %x resid %d flags 0x%llx\n",
646 			      snic_ioreq_state_to_str(CMD_STATE(sc)),
647 			      snic_io_status_to_str(hdr_stat),
648 			      sc, sc_stat, le32_to_cpu(icmnd_cmpl->resid),
649 			      CMD_FLAGS(sc));
650 
651 		SNIC_TRC(snic->shost->host_no, cmnd_id, (ulong) sc,
652 			 jiffies_to_msecs(jiffies - start_time), (ulong) fwreq,
653 			 SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
654 
655 		return;
656 	}
657 
658 	if (snic_process_icmnd_cmpl_status(snic, icmnd_cmpl, hdr_stat, sc)) {
659 		scsi_print_command(sc);
660 		SNIC_HOST_ERR(snic->shost,
661 			      "icmnd_cmpl:IO Failed, sc 0x%p Tag %d Cmd %x Hdr Status %s flags 0x%llx\n",
662 			      sc, sc->cmnd[0], cmnd_id,
663 			      snic_io_status_to_str(hdr_stat), CMD_FLAGS(sc));
664 	}
665 
666 	/* Break link with the SCSI Command */
667 	CMD_SP(sc) = NULL;
668 	CMD_FLAGS(sc) |= SNIC_IO_DONE;
669 
670 	spin_unlock_irqrestore(io_lock, flags);
671 
672 	/* For now, consider only successful IO. */
673 	snic_calc_io_process_time(snic, rqi);
674 
675 	snic_release_req_buf(snic, rqi, sc);
676 
677 	SNIC_TRC(snic->shost->host_no, cmnd_id, (ulong) sc,
678 		 jiffies_to_msecs(jiffies - start_time), (ulong) fwreq,
679 		 SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
680 
681 
682 	if (sc->scsi_done)
683 		sc->scsi_done(sc);
684 
685 	snic_stats_update_io_cmpl(&snic->s_stats);
686 } /* end of snic_icmnd_cmpl_handler */
687 
688 static void
689 snic_proc_dr_cmpl_locked(struct snic *snic,
690 			 struct snic_fw_req *fwreq,
691 			 u8 cmpl_stat,
692 			 u32 cmnd_id,
693 			 struct scsi_cmnd *sc)
694 {
695 	struct snic_req_info *rqi = (struct snic_req_info *) CMD_SP(sc);
696 	u32 start_time = rqi->start_time;
697 
698 	CMD_LR_STATUS(sc) = cmpl_stat;
699 
700 	SNIC_SCSI_DBG(snic->shost, "itmf_cmpl: Cmd State = %s\n",
701 		      snic_ioreq_state_to_str(CMD_STATE(sc)));
702 
703 	if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING) {
704 		CMD_FLAGS(sc) |= SNIC_DEV_RST_ABTS_PENDING;
705 
706 		SNIC_TRC(snic->shost->host_no, cmnd_id, (ulong) sc,
707 			 jiffies_to_msecs(jiffies - start_time),
708 			 (ulong) fwreq, 0, SNIC_TRC_CMD_STATE_FLAGS(sc));
709 
710 		SNIC_SCSI_DBG(snic->shost,
711 			      "itmf_cmpl: Terminate Pending Dev Reset Cmpl Recvd.id %x, status %s flags 0x%llx\n",
712 			      (int)(cmnd_id & SNIC_TAG_MASK),
713 			      snic_io_status_to_str(cmpl_stat),
714 			      CMD_FLAGS(sc));
715 
716 		return;
717 	}
718 
719 
720 	if (CMD_FLAGS(sc) & SNIC_DEV_RST_TIMEDOUT) {
721 		SNIC_TRC(snic->shost->host_no, cmnd_id, (ulong) sc,
722 			 jiffies_to_msecs(jiffies - start_time),
723 			 (ulong) fwreq, 0, SNIC_TRC_CMD_STATE_FLAGS(sc));
724 
725 		SNIC_SCSI_DBG(snic->shost,
726 			      "itmf_cmpl:Dev Reset Completion Received after timeout. id %d cmpl status %s flags 0x%llx\n",
727 			      (int)(cmnd_id & SNIC_TAG_MASK),
728 			      snic_io_status_to_str(cmpl_stat),
729 			      CMD_FLAGS(sc));
730 
731 		return;
732 	}
733 
734 	CMD_STATE(sc) = SNIC_IOREQ_LR_COMPLETE;
735 	CMD_FLAGS(sc) |= SNIC_DEV_RST_DONE;
736 
737 	SNIC_SCSI_DBG(snic->shost,
738 		      "itmf_cmpl:Dev Reset Cmpl Recvd id %d cmpl status %s flags 0x%llx\n",
739 		      (int)(cmnd_id & SNIC_TAG_MASK),
740 		      snic_io_status_to_str(cmpl_stat),
741 		      CMD_FLAGS(sc));
742 
743 	if (rqi->dr_done)
744 		complete(rqi->dr_done);
745 } /* end of snic_proc_dr_cmpl_locked */
746 
747 /*
748  * snic_update_abort_stats : Updates abort stats based on completion status.
749  */
750 static void
751 snic_update_abort_stats(struct snic *snic, u8 cmpl_stat)
752 {
753 	struct snic_abort_stats *abt_stats = &snic->s_stats.abts;
754 
755 	SNIC_SCSI_DBG(snic->shost, "Updating Abort stats.\n");
756 
757 	switch (cmpl_stat) {
758 	case  SNIC_STAT_IO_SUCCESS:
759 		break;
760 
761 	case SNIC_STAT_TIMEOUT:
762 		atomic64_inc(&abt_stats->fw_tmo);
763 		break;
764 
765 	case SNIC_STAT_IO_NOT_FOUND:
766 		atomic64_inc(&abt_stats->io_not_found);
767 		break;
768 
769 	default:
770 		atomic64_inc(&abt_stats->fail);
771 		break;
772 	}
773 }
774 
775 static int
776 snic_process_itmf_cmpl(struct snic *snic,
777 		       struct snic_fw_req *fwreq,
778 		       u32 cmnd_id,
779 		       u8 cmpl_stat,
780 		       struct scsi_cmnd *sc)
781 {
782 	struct snic_req_info *rqi = NULL;
783 	u32 tm_tags = 0;
784 	spinlock_t *io_lock = NULL;
785 	unsigned long flags;
786 	u32 start_time = 0;
787 	int ret = 0;
788 
789 	io_lock = snic_io_lock_hash(snic, sc);
790 	spin_lock_irqsave(io_lock, flags);
791 	if (CMD_FLAGS(sc) & SNIC_HOST_RESET_CMD_TERM) {
792 		spin_unlock_irqrestore(io_lock, flags);
793 
794 		return ret;
795 	}
796 	rqi = (struct snic_req_info *) CMD_SP(sc);
797 	WARN_ON_ONCE(!rqi);
798 
799 	if (!rqi) {
800 		atomic64_inc(&snic->s_stats.io.req_null);
801 		spin_unlock_irqrestore(io_lock, flags);
802 		CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_REQ_NULL;
803 		SNIC_HOST_ERR(snic->shost,
804 			      "itmf_cmpl: rqi is null,Hdr stat = %s Tag = 0x%x sc = 0x%p flags 0x%llx\n",
805 			      snic_io_status_to_str(cmpl_stat), cmnd_id, sc,
806 			      CMD_FLAGS(sc));
807 
808 		return ret;
809 	}
810 
811 	/* Extract task management flags */
812 	tm_tags = cmnd_id & ~(SNIC_TAG_MASK);
813 
814 	start_time = rqi->start_time;
815 	cmnd_id &= (SNIC_TAG_MASK);
816 
817 	switch (tm_tags) {
818 	case SNIC_TAG_ABORT:
819 		/* Abort only issued on cmd */
820 		snic_update_abort_stats(snic, cmpl_stat);
821 
822 		if (CMD_STATE(sc) != SNIC_IOREQ_ABTS_PENDING) {
823 			/* This is a late completion. Ignore it. */
824 			ret = -1;
825 			spin_unlock_irqrestore(io_lock, flags);
826 			break;
827 		}
828 
829 		CMD_STATE(sc) = SNIC_IOREQ_ABTS_COMPLETE;
830 		CMD_ABTS_STATUS(sc) = cmpl_stat;
831 		CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_DONE;
832 
833 		SNIC_SCSI_DBG(snic->shost,
834 			      "itmf_cmpl:Abort Cmpl Recvd.Tag 0x%x Status %s flags 0x%llx\n",
835 			      cmnd_id,
836 			      snic_io_status_to_str(cmpl_stat),
837 			      CMD_FLAGS(sc));
838 
839 		/*
840 		 * If scsi_eh thread is blocked waiting for abts complete,
841 		 * signal completion to it. IO will be cleaned in the thread,
842 		 * else clean it in this context.
843 		 */
844 		if (rqi->abts_done) {
845 			complete(rqi->abts_done);
846 			spin_unlock_irqrestore(io_lock, flags);
847 
848 			break; /* jump out */
849 		}
850 
851 		CMD_SP(sc) = NULL;
852 		sc->result = (DID_ERROR << 16);
853 		SNIC_SCSI_DBG(snic->shost,
854 			      "itmf_cmpl: Completing IO. sc %p flags 0x%llx\n",
855 			      sc, CMD_FLAGS(sc));
856 
857 		spin_unlock_irqrestore(io_lock, flags);
858 
859 		snic_release_req_buf(snic, rqi, sc);
860 
861 		if (sc->scsi_done) {
862 			SNIC_TRC(snic->shost->host_no, cmnd_id, (ulong) sc,
863 				 jiffies_to_msecs(jiffies - start_time),
864 				 (ulong) fwreq, SNIC_TRC_CMD(sc),
865 				 SNIC_TRC_CMD_STATE_FLAGS(sc));
866 
867 			sc->scsi_done(sc);
868 		}
869 
870 		break;
871 
872 	case SNIC_TAG_DEV_RST:
873 	case SNIC_TAG_DEV_RST | SNIC_TAG_IOCTL_DEV_RST:
874 		snic_proc_dr_cmpl_locked(snic, fwreq, cmpl_stat, cmnd_id, sc);
875 		spin_unlock_irqrestore(io_lock, flags);
876 		ret = 0;
877 
878 		break;
879 
880 	case SNIC_TAG_ABORT | SNIC_TAG_DEV_RST:
881 		/* Abort and terminate completion of device reset req */
882 
883 		CMD_STATE(sc) = SNIC_IOREQ_ABTS_COMPLETE;
884 		CMD_ABTS_STATUS(sc) = cmpl_stat;
885 		CMD_FLAGS(sc) |= SNIC_DEV_RST_DONE;
886 
887 		SNIC_SCSI_DBG(snic->shost,
888 			      "itmf_cmpl:dev reset abts cmpl recvd. id %d status %s flags 0x%llx\n",
889 			      cmnd_id, snic_io_status_to_str(cmpl_stat),
890 			      CMD_FLAGS(sc));
891 
892 		if (rqi->abts_done)
893 			complete(rqi->abts_done);
894 
895 		spin_unlock_irqrestore(io_lock, flags);
896 
897 		break;
898 
899 	default:
900 		spin_unlock_irqrestore(io_lock, flags);
901 		SNIC_HOST_ERR(snic->shost,
902 			      "itmf_cmpl: Unknown TM tag bit 0x%x\n", tm_tags);
903 
904 		SNIC_HOST_ERR(snic->shost,
905 			      "itmf_cmpl:Unexpected itmf io stat %s Tag = 0x%x flags 0x%llx\n",
906 			      snic_ioreq_state_to_str(CMD_STATE(sc)),
907 			      cmnd_id,
908 			      CMD_FLAGS(sc));
909 		ret = -1;
910 		SNIC_BUG_ON(1);
911 
912 		break;
913 	}
914 
915 	return ret;
916 } /* end of snic_process_itmf_cmpl_status */
917 
918 /*
919  * snic_itmf_cmpl_handler.
920  * Routine to handle itmf completions.
921  */
922 static void
923 snic_itmf_cmpl_handler(struct snic *snic, struct snic_fw_req *fwreq)
924 {
925 	struct scsi_cmnd  *sc = NULL;
926 	struct snic_req_info *rqi = NULL;
927 	struct snic_itmf_cmpl *itmf_cmpl = NULL;
928 	ulong ctx;
929 	u32 cmnd_id;
930 	u32 hid;
931 	u8 typ;
932 	u8 hdr_stat;
933 
934 	snic_io_hdr_dec(&fwreq->hdr, &typ, &hdr_stat, &cmnd_id, &hid, &ctx);
935 	SNIC_SCSI_DBG(snic->shost,
936 		      "Itmf_cmpl: %s: type = %x, hdr_stat = %x, cmnd_id = %x, hid = %x,ctx = %lx\n",
937 		      __func__, typ, hdr_stat, cmnd_id, hid, ctx);
938 
939 	itmf_cmpl = &fwreq->u.itmf_cmpl;
940 	SNIC_SCSI_DBG(snic->shost,
941 		      "Itmf_cmpl: nterm %u , flags 0x%x\n",
942 		      le32_to_cpu(itmf_cmpl->nterminated), itmf_cmpl->flags);
943 
944 	/* spl case, dev reset issued through ioctl */
945 	if (cmnd_id & SNIC_TAG_IOCTL_DEV_RST) {
946 		rqi = (struct snic_req_info *) ctx;
947 		sc = rqi->sc;
948 
949 		goto ioctl_dev_rst;
950 	}
951 
952 	if ((cmnd_id & SNIC_TAG_MASK) >= snic->max_tag_id) {
953 		SNIC_HOST_ERR(snic->shost,
954 			      "Itmf_cmpl: Tag 0x%x out of Range,HdrStat %s\n",
955 			      cmnd_id, snic_io_status_to_str(hdr_stat));
956 		SNIC_BUG_ON(1);
957 
958 		return;
959 	}
960 
961 	sc = scsi_host_find_tag(snic->shost, cmnd_id & SNIC_TAG_MASK);
962 	WARN_ON_ONCE(!sc);
963 
964 ioctl_dev_rst:
965 	if (!sc) {
966 		atomic64_inc(&snic->s_stats.io.sc_null);
967 		SNIC_HOST_ERR(snic->shost,
968 			      "Itmf_cmpl: sc is NULL - Hdr Stat %s Tag 0x%x\n",
969 			      snic_io_status_to_str(hdr_stat), cmnd_id);
970 
971 		return;
972 	}
973 
974 	snic_process_itmf_cmpl(snic, fwreq, cmnd_id, hdr_stat, sc);
975 } /* end of snic_itmf_cmpl_handler */
976 
977 
978 
979 static void
980 snic_hba_reset_scsi_cleanup(struct snic *snic, struct scsi_cmnd *sc)
981 {
982 	struct snic_stats *st = &snic->s_stats;
983 	long act_ios = 0, act_fwreqs = 0;
984 
985 	SNIC_SCSI_DBG(snic->shost, "HBA Reset scsi cleanup.\n");
986 	snic_scsi_cleanup(snic, snic_cmd_tag(sc));
987 
988 	/* Update stats on pending IOs */
989 	act_ios = atomic64_read(&st->io.active);
990 	atomic64_add(act_ios, &st->io.compl);
991 	atomic64_sub(act_ios, &st->io.active);
992 
993 	act_fwreqs = atomic64_read(&st->fw.actv_reqs);
994 	atomic64_sub(act_fwreqs, &st->fw.actv_reqs);
995 }
996 
997 /*
998  * snic_hba_reset_cmpl_handler :
999  *
1000  * Notes :
1001  * 1. Cleanup all the scsi cmds, release all snic specific cmds
1002  * 2. Issue Report Targets in case of SAN targets
1003  */
1004 static int
1005 snic_hba_reset_cmpl_handler(struct snic *snic, struct snic_fw_req *fwreq)
1006 {
1007 	ulong ctx;
1008 	u32 cmnd_id;
1009 	u32 hid;
1010 	u8 typ;
1011 	u8 hdr_stat;
1012 	struct scsi_cmnd *sc = NULL;
1013 	struct snic_req_info *rqi = NULL;
1014 	spinlock_t *io_lock = NULL;
1015 	unsigned long flags, gflags;
1016 	int ret = 0;
1017 
1018 	snic_io_hdr_dec(&fwreq->hdr, &typ, &hdr_stat, &cmnd_id, &hid, &ctx);
1019 	SNIC_HOST_INFO(snic->shost,
1020 		       "reset_cmpl:Tag %d ctx %lx cmpl status %s HBA Reset Completion received.\n",
1021 		       cmnd_id, ctx, snic_io_status_to_str(hdr_stat));
1022 
1023 	SNIC_SCSI_DBG(snic->shost,
1024 		      "reset_cmpl: type = %x, hdr_stat = %x, cmnd_id = %x, hid = %x, ctx = %lx\n",
1025 		      typ, hdr_stat, cmnd_id, hid, ctx);
1026 
1027 	/* spl case, host reset issued through ioctl */
1028 	if (cmnd_id == SCSI_NO_TAG) {
1029 		rqi = (struct snic_req_info *) ctx;
1030 		SNIC_HOST_INFO(snic->shost,
1031 			       "reset_cmpl:Tag %d ctx %lx cmpl stat %s\n",
1032 			       cmnd_id, ctx, snic_io_status_to_str(hdr_stat));
1033 		sc = rqi->sc;
1034 
1035 		goto ioctl_hba_rst;
1036 	}
1037 
1038 	if (cmnd_id >= snic->max_tag_id) {
1039 		SNIC_HOST_ERR(snic->shost,
1040 			      "reset_cmpl: Tag 0x%x out of Range,HdrStat %s\n",
1041 			      cmnd_id, snic_io_status_to_str(hdr_stat));
1042 		SNIC_BUG_ON(1);
1043 
1044 		return 1;
1045 	}
1046 
1047 	sc = scsi_host_find_tag(snic->shost, cmnd_id);
1048 ioctl_hba_rst:
1049 	if (!sc) {
1050 		atomic64_inc(&snic->s_stats.io.sc_null);
1051 		SNIC_HOST_ERR(snic->shost,
1052 			      "reset_cmpl: sc is NULL - Hdr Stat %s Tag 0x%x\n",
1053 			      snic_io_status_to_str(hdr_stat), cmnd_id);
1054 		ret = 1;
1055 
1056 		return ret;
1057 	}
1058 
1059 	SNIC_HOST_INFO(snic->shost,
1060 		       "reset_cmpl: sc %p rqi %p Tag %d flags 0x%llx\n",
1061 		       sc, rqi, cmnd_id, CMD_FLAGS(sc));
1062 
1063 	io_lock = snic_io_lock_hash(snic, sc);
1064 	spin_lock_irqsave(io_lock, flags);
1065 
1066 	if (!snic->remove_wait) {
1067 		spin_unlock_irqrestore(io_lock, flags);
1068 		SNIC_HOST_ERR(snic->shost,
1069 			      "reset_cmpl:host reset completed after timout\n");
1070 		ret = 1;
1071 
1072 		return ret;
1073 	}
1074 
1075 	rqi = (struct snic_req_info *) CMD_SP(sc);
1076 	WARN_ON_ONCE(!rqi);
1077 
1078 	if (!rqi) {
1079 		atomic64_inc(&snic->s_stats.io.req_null);
1080 		spin_unlock_irqrestore(io_lock, flags);
1081 		CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_REQ_NULL;
1082 		SNIC_HOST_ERR(snic->shost,
1083 			      "reset_cmpl: rqi is null,Hdr stat %s Tag 0x%x sc 0x%p flags 0x%llx\n",
1084 			      snic_io_status_to_str(hdr_stat), cmnd_id, sc,
1085 			      CMD_FLAGS(sc));
1086 
1087 		ret = 1;
1088 
1089 		return ret;
1090 	}
1091 	/* stats */
1092 	spin_unlock_irqrestore(io_lock, flags);
1093 
1094 	/* scsi cleanup */
1095 	snic_hba_reset_scsi_cleanup(snic, sc);
1096 
1097 	SNIC_BUG_ON(snic_get_state(snic) != SNIC_OFFLINE &&
1098 		    snic_get_state(snic) != SNIC_FWRESET);
1099 
1100 	/* Careful locking between snic_lock and io lock */
1101 	spin_lock_irqsave(io_lock, flags);
1102 	spin_lock_irqsave(&snic->snic_lock, gflags);
1103 	if (snic_get_state(snic) == SNIC_FWRESET)
1104 		snic_set_state(snic, SNIC_ONLINE);
1105 	spin_unlock_irqrestore(&snic->snic_lock, gflags);
1106 
1107 	if (snic->remove_wait)
1108 		complete(snic->remove_wait);
1109 
1110 	spin_unlock_irqrestore(io_lock, flags);
1111 	atomic64_inc(&snic->s_stats.reset.hba_reset_cmpl);
1112 
1113 	ret = 0;
1114 	/* Rediscovery is for SAN */
1115 	if (snic->config.xpt_type == SNIC_DAS)
1116 			return ret;
1117 
1118 	SNIC_SCSI_DBG(snic->shost, "reset_cmpl: Queuing discovery work.\n");
1119 	queue_work(snic_glob->event_q, &snic->disc_work);
1120 
1121 	return ret;
1122 }
1123 
1124 static void
1125 snic_msg_ack_handler(struct snic *snic, struct snic_fw_req *fwreq)
1126 {
1127 	SNIC_HOST_INFO(snic->shost, "Message Ack Received.\n");
1128 
1129 	SNIC_ASSERT_NOT_IMPL(1);
1130 }
1131 
1132 static void
1133 snic_aen_handler(struct snic *snic, struct snic_fw_req *fwreq)
1134 {
1135 	u8 typ, hdr_stat;
1136 	u32 cmnd_id, hid;
1137 	ulong ctx;
1138 	struct snic_async_evnotify *aen = &fwreq->u.async_ev;
1139 	u32 event_id = 0;
1140 
1141 	snic_io_hdr_dec(&fwreq->hdr, &typ, &hdr_stat, &cmnd_id, &hid, &ctx);
1142 	SNIC_SCSI_DBG(snic->shost,
1143 		      "aen: type = %x, hdr_stat = %x, cmnd_id = %x, hid = %x, ctx = %lx\n",
1144 		      typ, hdr_stat, cmnd_id, hid, ctx);
1145 
1146 	event_id = le32_to_cpu(aen->ev_id);
1147 
1148 	switch (event_id) {
1149 	case SNIC_EV_TGT_OFFLINE:
1150 		SNIC_HOST_INFO(snic->shost, "aen:TGT_OFFLINE Event Recvd.\n");
1151 		break;
1152 
1153 	case SNIC_EV_TGT_ONLINE:
1154 		SNIC_HOST_INFO(snic->shost, "aen:TGT_ONLINE Event Recvd.\n");
1155 		break;
1156 
1157 	case SNIC_EV_LUN_OFFLINE:
1158 		SNIC_HOST_INFO(snic->shost, "aen:LUN_OFFLINE Event Recvd.\n");
1159 		break;
1160 
1161 	case SNIC_EV_LUN_ONLINE:
1162 		SNIC_HOST_INFO(snic->shost, "aen:LUN_ONLINE Event Recvd.\n");
1163 		break;
1164 
1165 	case SNIC_EV_CONF_CHG:
1166 		SNIC_HOST_INFO(snic->shost, "aen:Config Change Event Recvd.\n");
1167 		break;
1168 
1169 	case SNIC_EV_TGT_ADDED:
1170 		SNIC_HOST_INFO(snic->shost, "aen:TGT_ADD Event Recvd.\n");
1171 		break;
1172 
1173 	case SNIC_EV_TGT_DELTD:
1174 		SNIC_HOST_INFO(snic->shost, "aen:TGT_DEL Event Recvd.\n");
1175 		break;
1176 
1177 	case SNIC_EV_LUN_ADDED:
1178 		SNIC_HOST_INFO(snic->shost, "aen:LUN_ADD Event Recvd.\n");
1179 		break;
1180 
1181 	case SNIC_EV_LUN_DELTD:
1182 		SNIC_HOST_INFO(snic->shost, "aen:LUN_DEL Event Recvd.\n");
1183 		break;
1184 
1185 	case SNIC_EV_DISC_CMPL:
1186 		SNIC_HOST_INFO(snic->shost, "aen:DISC_CMPL Event Recvd.\n");
1187 		break;
1188 
1189 	default:
1190 		SNIC_HOST_INFO(snic->shost, "aen:Unknown Event Recvd.\n");
1191 		SNIC_BUG_ON(1);
1192 		break;
1193 	}
1194 
1195 	SNIC_ASSERT_NOT_IMPL(1);
1196 } /* end of snic_aen_handler */
1197 
1198 /*
1199  * snic_io_cmpl_handler
1200  * Routine to process CQ entries(IO Completions) posted by fw.
1201  */
1202 static int
1203 snic_io_cmpl_handler(struct vnic_dev *vdev,
1204 		     unsigned int cq_idx,
1205 		     struct snic_fw_req *fwreq)
1206 {
1207 	struct snic *snic = svnic_dev_priv(vdev);
1208 	u64 start = jiffies, cmpl_time;
1209 
1210 	snic_print_desc(__func__, (char *)fwreq, sizeof(*fwreq));
1211 
1212 	/* Update FW Stats */
1213 	if ((fwreq->hdr.type >= SNIC_RSP_REPORT_TGTS_CMPL) &&
1214 		(fwreq->hdr.type <= SNIC_RSP_BOOT_LUNS_CMPL))
1215 		atomic64_dec(&snic->s_stats.fw.actv_reqs);
1216 
1217 	SNIC_BUG_ON((fwreq->hdr.type > SNIC_RSP_BOOT_LUNS_CMPL) &&
1218 		    (fwreq->hdr.type < SNIC_MSG_ASYNC_EVNOTIFY));
1219 
1220 	/* Check for snic subsys errors */
1221 	switch (fwreq->hdr.status) {
1222 	case SNIC_STAT_NOT_READY:	/* XPT yet to initialize */
1223 		SNIC_HOST_ERR(snic->shost,
1224 			      "sNIC SubSystem is NOT Ready.\n");
1225 		break;
1226 
1227 	case SNIC_STAT_FATAL_ERROR:	/* XPT Error */
1228 		SNIC_HOST_ERR(snic->shost,
1229 			      "sNIC SubSystem in Unrecoverable State.\n");
1230 		break;
1231 	}
1232 
1233 	switch (fwreq->hdr.type) {
1234 	case SNIC_RSP_EXCH_VER_CMPL:
1235 		snic_io_exch_ver_cmpl_handler(snic, fwreq);
1236 		break;
1237 
1238 	case SNIC_RSP_REPORT_TGTS_CMPL:
1239 		snic_report_tgt_cmpl_handler(snic, fwreq);
1240 		break;
1241 
1242 	case SNIC_RSP_ICMND_CMPL:
1243 		snic_icmnd_cmpl_handler(snic, fwreq);
1244 		break;
1245 
1246 	case SNIC_RSP_ITMF_CMPL:
1247 		snic_itmf_cmpl_handler(snic, fwreq);
1248 		break;
1249 
1250 	case SNIC_RSP_HBA_RESET_CMPL:
1251 		snic_hba_reset_cmpl_handler(snic, fwreq);
1252 		break;
1253 
1254 	case SNIC_MSG_ACK:
1255 		snic_msg_ack_handler(snic, fwreq);
1256 		break;
1257 
1258 	case SNIC_MSG_ASYNC_EVNOTIFY:
1259 		snic_aen_handler(snic, fwreq);
1260 		break;
1261 
1262 	default:
1263 		SNIC_BUG_ON(1);
1264 		SNIC_SCSI_DBG(snic->shost,
1265 			      "Unknown Firmwqre completion request type %d\n",
1266 			      fwreq->hdr.type);
1267 		break;
1268 	}
1269 
1270 	/* Update Stats */
1271 	cmpl_time = jiffies - start;
1272 	if (cmpl_time > atomic64_read(&snic->s_stats.io.max_cmpl_time))
1273 		atomic64_set(&snic->s_stats.io.max_cmpl_time, cmpl_time);
1274 
1275 	return 0;
1276 } /* end of snic_io_cmpl_handler */
1277 
1278 /*
1279  * snic_fwcq_cmpl_handler
1280  * Routine to process fwCQ
1281  * This CQ is independent, and not associated with wq/rq/wq_copy queues
1282  */
1283 int
1284 snic_fwcq_cmpl_handler(struct snic *snic, int io_cmpl_work)
1285 {
1286 	unsigned int num_ent = 0;	/* number cq entries processed */
1287 	unsigned int cq_idx;
1288 	unsigned int nent_per_cq;
1289 	struct snic_misc_stats *misc_stats = &snic->s_stats.misc;
1290 
1291 	for (cq_idx = snic->wq_count; cq_idx < snic->cq_count; cq_idx++) {
1292 		nent_per_cq = vnic_cq_fw_service(&snic->cq[cq_idx],
1293 						 snic_io_cmpl_handler,
1294 						 io_cmpl_work);
1295 		num_ent += nent_per_cq;
1296 
1297 		if (nent_per_cq > atomic64_read(&misc_stats->max_cq_ents))
1298 			atomic64_set(&misc_stats->max_cq_ents, nent_per_cq);
1299 	}
1300 
1301 	return num_ent;
1302 } /* end of snic_fwcq_cmpl_handler */
1303 
1304 /*
1305  * snic_queue_itmf_req: Common API to queue Task Management requests.
1306  * Use rqi->tm_tag for passing special tags.
1307  * @req_id : aborted request's tag, -1 for lun reset.
1308  */
1309 static int
1310 snic_queue_itmf_req(struct snic *snic,
1311 		    struct snic_host_req *tmreq,
1312 		    struct scsi_cmnd *sc,
1313 		    u32 tmf,
1314 		    u32 req_id)
1315 {
1316 	struct snic_req_info *rqi = req_to_rqi(tmreq);
1317 	struct scsi_lun lun;
1318 	int tm_tag = snic_cmd_tag(sc) | rqi->tm_tag;
1319 	int ret = 0;
1320 
1321 	SNIC_BUG_ON(!rqi);
1322 	SNIC_BUG_ON(!rqi->tm_tag);
1323 
1324 	/* fill in lun info */
1325 	int_to_scsilun(sc->device->lun, &lun);
1326 
1327 	/* Initialize snic_host_req: itmf */
1328 	snic_itmf_init(tmreq,
1329 		       tm_tag,
1330 		       snic->config.hid,
1331 		       (ulong) rqi,
1332 		       0 /* flags */,
1333 		       req_id, /* Command to be aborted. */
1334 		       rqi->tgt_id,
1335 		       lun.scsi_lun,
1336 		       tmf);
1337 
1338 	/*
1339 	 * In case of multiple aborts on same cmd,
1340 	 * use try_wait_for_completion and completion_done() to check
1341 	 * whether it queues aborts even after completion of abort issued
1342 	 * prior.SNIC_BUG_ON(completion_done(&rqi->done));
1343 	 */
1344 
1345 	ret = snic_queue_wq_desc(snic, tmreq, sizeof(*tmreq));
1346 	if (ret)
1347 		SNIC_HOST_ERR(snic->shost,
1348 			      "qitmf:Queuing ITMF(%d) Req sc %p, rqi %p, req_id %d tag %d Failed, ret = %d\n",
1349 			      tmf, sc, rqi, req_id, snic_cmd_tag(sc), ret);
1350 	else
1351 		SNIC_SCSI_DBG(snic->shost,
1352 			      "qitmf:Queuing ITMF(%d) Req sc %p, rqi %p, req_id %d, tag %d (req_id)- Success.",
1353 			      tmf, sc, rqi, req_id, snic_cmd_tag(sc));
1354 
1355 	return ret;
1356 } /* end of snic_queue_itmf_req */
1357 
1358 static int
1359 snic_issue_tm_req(struct snic *snic,
1360 		    struct snic_req_info *rqi,
1361 		    struct scsi_cmnd *sc,
1362 		    int tmf)
1363 {
1364 	struct snic_host_req *tmreq = NULL;
1365 	int req_id = 0, tag = snic_cmd_tag(sc);
1366 	int ret = 0;
1367 
1368 	if (snic_get_state(snic) == SNIC_FWRESET)
1369 		return -EBUSY;
1370 
1371 	atomic_inc(&snic->ios_inflight);
1372 
1373 	SNIC_SCSI_DBG(snic->shost,
1374 		      "issu_tmreq: Task mgmt req %d. rqi %p w/ tag %x\n",
1375 		      tmf, rqi, tag);
1376 
1377 
1378 	if (tmf == SNIC_ITMF_LUN_RESET) {
1379 		tmreq = snic_dr_req_init(snic, rqi);
1380 		req_id = SCSI_NO_TAG;
1381 	} else {
1382 		tmreq = snic_abort_req_init(snic, rqi);
1383 		req_id = tag;
1384 	}
1385 
1386 	if (!tmreq) {
1387 		ret = -ENOMEM;
1388 
1389 		goto tmreq_err;
1390 	}
1391 
1392 	ret = snic_queue_itmf_req(snic, tmreq, sc, tmf, req_id);
1393 	if (ret)
1394 		goto tmreq_err;
1395 
1396 	ret = 0;
1397 
1398 tmreq_err:
1399 	if (ret) {
1400 		SNIC_HOST_ERR(snic->shost,
1401 			      "issu_tmreq: Queing ITMF(%d) Req, sc %p rqi %p req_id %d tag %x fails err = %d\n",
1402 			      tmf, sc, rqi, req_id, tag, ret);
1403 	} else {
1404 		SNIC_SCSI_DBG(snic->shost,
1405 			      "issu_tmreq: Queuing ITMF(%d) Req, sc %p, rqi %p, req_id %d tag %x - Success.\n",
1406 			      tmf, sc, rqi, req_id, tag);
1407 	}
1408 
1409 	atomic_dec(&snic->ios_inflight);
1410 
1411 	return ret;
1412 }
1413 
1414 /*
1415  * snic_queue_abort_req : Queues abort req to WQ
1416  */
1417 static int
1418 snic_queue_abort_req(struct snic *snic,
1419 		     struct snic_req_info *rqi,
1420 		     struct scsi_cmnd *sc,
1421 		     int tmf)
1422 {
1423 	SNIC_SCSI_DBG(snic->shost, "q_abtreq: sc %p, rqi %p, tag %x, tmf %d\n",
1424 		      sc, rqi, snic_cmd_tag(sc), tmf);
1425 
1426 	/* Add special tag for abort */
1427 	rqi->tm_tag |= SNIC_TAG_ABORT;
1428 
1429 	return snic_issue_tm_req(snic, rqi, sc, tmf);
1430 }
1431 
1432 /*
1433  * snic_abort_finish : called by snic_abort_cmd on queuing abort successfully.
1434  */
1435 static int
1436 snic_abort_finish(struct snic *snic, struct scsi_cmnd *sc)
1437 {
1438 	struct snic_req_info *rqi = NULL;
1439 	spinlock_t *io_lock = NULL;
1440 	unsigned long flags;
1441 	int ret = 0, tag = snic_cmd_tag(sc);
1442 
1443 	io_lock = snic_io_lock_hash(snic, sc);
1444 	spin_lock_irqsave(io_lock, flags);
1445 	rqi = (struct snic_req_info *) CMD_SP(sc);
1446 	if (!rqi) {
1447 		atomic64_inc(&snic->s_stats.io.req_null);
1448 		CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_REQ_NULL;
1449 
1450 		SNIC_SCSI_DBG(snic->shost,
1451 			      "abt_fini:req info is null tag 0x%x, sc 0x%p flags 0x%llx\n",
1452 			      tag, sc, CMD_FLAGS(sc));
1453 		ret = FAILED;
1454 
1455 		goto abort_fail;
1456 	}
1457 
1458 	rqi->abts_done = NULL;
1459 
1460 	ret = FAILED;
1461 
1462 	/* Check the abort status. */
1463 	switch (CMD_ABTS_STATUS(sc)) {
1464 	case SNIC_INVALID_CODE:
1465 		/* Firmware didn't complete abort req, timedout */
1466 		CMD_FLAGS(sc) |= SNIC_IO_ABTS_TIMEDOUT;
1467 		atomic64_inc(&snic->s_stats.abts.drv_tmo);
1468 		SNIC_SCSI_DBG(snic->shost,
1469 			      "abt_fini:sc %p Tag %x Driver Timeout.flags 0x%llx\n",
1470 			      sc, snic_cmd_tag(sc), CMD_FLAGS(sc));
1471 		/* do not release snic request in timedout case */
1472 		rqi = NULL;
1473 
1474 		goto abort_fail;
1475 
1476 	case SNIC_STAT_IO_SUCCESS:
1477 	case SNIC_STAT_IO_NOT_FOUND:
1478 		ret = SUCCESS;
1479 		/*
1480 		 * If abort path doesn't call scsi_done(),
1481 		 * the # IO timeouts == 2, will cause the LUN offline.
1482 		 * Call scsi_done to complete the IO.
1483 		 */
1484 		sc->result = (DID_ERROR << 16);
1485 		sc->scsi_done(sc);
1486 		break;
1487 
1488 	default:
1489 		/* Firmware completed abort with error */
1490 		ret = FAILED;
1491 		rqi = NULL;
1492 		break;
1493 	}
1494 
1495 	CMD_SP(sc) = NULL;
1496 	SNIC_HOST_INFO(snic->shost,
1497 		       "abt_fini: Tag %x, Cmpl Status %s flags 0x%llx\n",
1498 		       tag, snic_io_status_to_str(CMD_ABTS_STATUS(sc)),
1499 		       CMD_FLAGS(sc));
1500 
1501 abort_fail:
1502 	spin_unlock_irqrestore(io_lock, flags);
1503 	if (rqi)
1504 		snic_release_req_buf(snic, rqi, sc);
1505 
1506 	return ret;
1507 } /* end of snic_abort_finish */
1508 
1509 /*
1510  * snic_send_abort_and_wait : Issues Abort, and Waits
1511  */
1512 static int
1513 snic_send_abort_and_wait(struct snic *snic, struct scsi_cmnd *sc)
1514 {
1515 	struct snic_req_info *rqi = NULL;
1516 	enum snic_ioreq_state sv_state;
1517 	struct snic_tgt *tgt = NULL;
1518 	spinlock_t *io_lock = NULL;
1519 	DECLARE_COMPLETION_ONSTACK(tm_done);
1520 	unsigned long flags;
1521 	int ret = 0, tmf = 0, tag = snic_cmd_tag(sc);
1522 
1523 	tgt = starget_to_tgt(scsi_target(sc->device));
1524 	if ((snic_tgt_chkready(tgt) != 0) && (tgt->tdata.typ == SNIC_TGT_SAN))
1525 		tmf = SNIC_ITMF_ABTS_TASK_TERM;
1526 	else
1527 		tmf = SNIC_ITMF_ABTS_TASK;
1528 
1529 	/* stats */
1530 
1531 	io_lock = snic_io_lock_hash(snic, sc);
1532 
1533 	/*
1534 	 * Avoid a race between SCSI issuing the abort and the device
1535 	 * completing the command.
1536 	 *
1537 	 * If the command is already completed by fw_cmpl code,
1538 	 * we just return SUCCESS from here. This means that the abort
1539 	 * succeeded. In the SCSI ML, since the timeout for command has
1540 	 * happend, the completion wont actually complete the command
1541 	 * and it will be considered as an aborted command
1542 	 *
1543 	 * The CMD_SP will not be cleared except while holding io_lock
1544 	 */
1545 	spin_lock_irqsave(io_lock, flags);
1546 	rqi = (struct snic_req_info *) CMD_SP(sc);
1547 	if (!rqi) {
1548 		spin_unlock_irqrestore(io_lock, flags);
1549 
1550 		SNIC_HOST_ERR(snic->shost,
1551 			      "abt_cmd: rqi is null. Tag %d flags 0x%llx\n",
1552 			      tag, CMD_FLAGS(sc));
1553 
1554 		ret = SUCCESS;
1555 
1556 		goto send_abts_end;
1557 	}
1558 
1559 	rqi->abts_done = &tm_done;
1560 	if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING) {
1561 		spin_unlock_irqrestore(io_lock, flags);
1562 
1563 		ret = 0;
1564 		goto abts_pending;
1565 	}
1566 	SNIC_BUG_ON(!rqi->abts_done);
1567 
1568 	/* Save Command State, should be restored on failed to Queue. */
1569 	sv_state = CMD_STATE(sc);
1570 
1571 	/*
1572 	 * Command is still pending, need to abort it
1573 	 * If the fw completes the command after this point,
1574 	 * the completion won't be done till mid-layer, since abot
1575 	 * has already started.
1576 	 */
1577 	CMD_STATE(sc) = SNIC_IOREQ_ABTS_PENDING;
1578 	CMD_ABTS_STATUS(sc) = SNIC_INVALID_CODE;
1579 
1580 	SNIC_SCSI_DBG(snic->shost, "send_abt_cmd: TAG 0x%x\n", tag);
1581 
1582 	spin_unlock_irqrestore(io_lock, flags);
1583 
1584 	/* Now Queue the abort command to firmware */
1585 	ret = snic_queue_abort_req(snic, rqi, sc, tmf);
1586 	if (ret) {
1587 		atomic64_inc(&snic->s_stats.abts.q_fail);
1588 		SNIC_HOST_ERR(snic->shost,
1589 			      "send_abt_cmd: IO w/ Tag 0x%x fail w/ err %d flags 0x%llx\n",
1590 			      tag, ret, CMD_FLAGS(sc));
1591 
1592 		spin_lock_irqsave(io_lock, flags);
1593 		/* Restore Command's previous state */
1594 		CMD_STATE(sc) = sv_state;
1595 		rqi = (struct snic_req_info *) CMD_SP(sc);
1596 		if (rqi)
1597 			rqi->abts_done = NULL;
1598 		spin_unlock_irqrestore(io_lock, flags);
1599 		ret = FAILED;
1600 
1601 		goto send_abts_end;
1602 	}
1603 
1604 	spin_lock_irqsave(io_lock, flags);
1605 	if (tmf == SNIC_ITMF_ABTS_TASK) {
1606 		CMD_FLAGS(sc) |= SNIC_IO_ABTS_ISSUED;
1607 		atomic64_inc(&snic->s_stats.abts.num);
1608 	} else {
1609 		/* term stats */
1610 		CMD_FLAGS(sc) |= SNIC_IO_TERM_ISSUED;
1611 	}
1612 	spin_unlock_irqrestore(io_lock, flags);
1613 
1614 	SNIC_SCSI_DBG(snic->shost,
1615 		      "send_abt_cmd: sc %p Tag %x flags 0x%llx\n",
1616 		      sc, tag, CMD_FLAGS(sc));
1617 
1618 
1619 	ret = 0;
1620 
1621 abts_pending:
1622 	/*
1623 	 * Queued an abort IO, wait for its completion.
1624 	 * Once the fw completes the abort command, it will
1625 	 * wakeup this thread.
1626 	 */
1627 	wait_for_completion_timeout(&tm_done, SNIC_ABTS_TIMEOUT);
1628 
1629 send_abts_end:
1630 	return ret;
1631 } /* end of snic_send_abort_and_wait */
1632 
1633 /*
1634  * This function is exported to SCSI for sending abort cmnds.
1635  * A SCSI IO is represent by snic_ioreq in the driver.
1636  * The snic_ioreq is linked to the SCSI Cmd, thus a link with the ULP'S IO
1637  */
1638 int
1639 snic_abort_cmd(struct scsi_cmnd *sc)
1640 {
1641 	struct snic *snic = shost_priv(sc->device->host);
1642 	int ret = SUCCESS, tag = snic_cmd_tag(sc);
1643 	u32 start_time = jiffies;
1644 
1645 	SNIC_SCSI_DBG(snic->shost, "abt_cmd:sc %p :0x%x :req = %p :tag = %d\n",
1646 		       sc, sc->cmnd[0], sc->request, tag);
1647 
1648 	if (unlikely(snic_get_state(snic) != SNIC_ONLINE)) {
1649 		SNIC_HOST_ERR(snic->shost,
1650 			      "abt_cmd: tag %x Parent Devs are not rdy\n",
1651 			      tag);
1652 		ret = FAST_IO_FAIL;
1653 
1654 		goto abort_end;
1655 	}
1656 
1657 
1658 	ret = snic_send_abort_and_wait(snic, sc);
1659 	if (ret)
1660 		goto abort_end;
1661 
1662 	ret = snic_abort_finish(snic, sc);
1663 
1664 abort_end:
1665 	SNIC_TRC(snic->shost->host_no, tag, (ulong) sc,
1666 		 jiffies_to_msecs(jiffies - start_time), 0,
1667 		 SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
1668 
1669 	SNIC_SCSI_DBG(snic->shost,
1670 		      "abts: Abort Req Status = %s\n",
1671 		      (ret == SUCCESS) ? "SUCCESS" :
1672 		       ((ret == FAST_IO_FAIL) ? "FAST_IO_FAIL" : "FAILED"));
1673 
1674 	return ret;
1675 }
1676 
1677 
1678 
1679 static int
1680 snic_is_abts_pending(struct snic *snic, struct scsi_cmnd *lr_sc)
1681 {
1682 	struct snic_req_info *rqi = NULL;
1683 	struct scsi_cmnd *sc = NULL;
1684 	struct scsi_device *lr_sdev = NULL;
1685 	spinlock_t *io_lock = NULL;
1686 	u32 tag;
1687 	unsigned long flags;
1688 
1689 	if (lr_sc)
1690 		lr_sdev = lr_sc->device;
1691 
1692 	/* walk through the tag map, an dcheck if IOs are still pending in fw*/
1693 	for (tag = 0; tag < snic->max_tag_id; tag++) {
1694 		io_lock = snic_io_lock_tag(snic, tag);
1695 
1696 		spin_lock_irqsave(io_lock, flags);
1697 		sc = scsi_host_find_tag(snic->shost, tag);
1698 
1699 		if (!sc || (lr_sc && (sc->device != lr_sdev || sc == lr_sc))) {
1700 			spin_unlock_irqrestore(io_lock, flags);
1701 
1702 			continue;
1703 		}
1704 
1705 		rqi = (struct snic_req_info *) CMD_SP(sc);
1706 		if (!rqi) {
1707 			spin_unlock_irqrestore(io_lock, flags);
1708 
1709 			continue;
1710 		}
1711 
1712 		/*
1713 		 * Found IO that is still pending w/ firmware and belongs to
1714 		 * the LUN that is under reset, if lr_sc != NULL
1715 		 */
1716 		SNIC_SCSI_DBG(snic->shost, "Found IO in %s on LUN\n",
1717 			      snic_ioreq_state_to_str(CMD_STATE(sc)));
1718 
1719 		if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING) {
1720 			spin_unlock_irqrestore(io_lock, flags);
1721 
1722 			return 1;
1723 		}
1724 
1725 		spin_unlock_irqrestore(io_lock, flags);
1726 	}
1727 
1728 	return 0;
1729 } /* end of snic_is_abts_pending */
1730 
1731 static int
1732 snic_dr_clean_single_req(struct snic *snic,
1733 			 u32 tag,
1734 			 struct scsi_device *lr_sdev)
1735 {
1736 	struct snic_req_info *rqi = NULL;
1737 	struct snic_tgt *tgt = NULL;
1738 	struct scsi_cmnd *sc = NULL;
1739 	spinlock_t *io_lock = NULL;
1740 	u32 sv_state = 0, tmf = 0;
1741 	DECLARE_COMPLETION_ONSTACK(tm_done);
1742 	unsigned long flags;
1743 	int ret = 0;
1744 
1745 	io_lock = snic_io_lock_tag(snic, tag);
1746 	spin_lock_irqsave(io_lock, flags);
1747 	sc = scsi_host_find_tag(snic->shost, tag);
1748 
1749 	/* Ignore Cmd that don't belong to Lun Reset device */
1750 	if (!sc || sc->device != lr_sdev)
1751 		goto skip_clean;
1752 
1753 	rqi = (struct snic_req_info *) CMD_SP(sc);
1754 
1755 	if (!rqi)
1756 		goto skip_clean;
1757 
1758 
1759 	if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
1760 		goto skip_clean;
1761 
1762 
1763 	if ((CMD_FLAGS(sc) & SNIC_DEVICE_RESET) &&
1764 			(!(CMD_FLAGS(sc) & SNIC_DEV_RST_ISSUED))) {
1765 
1766 		SNIC_SCSI_DBG(snic->shost,
1767 			      "clean_single_req: devrst is not pending sc 0x%p\n",
1768 			      sc);
1769 
1770 		goto skip_clean;
1771 	}
1772 
1773 	SNIC_SCSI_DBG(snic->shost,
1774 		"clean_single_req: Found IO in %s on lun\n",
1775 		snic_ioreq_state_to_str(CMD_STATE(sc)));
1776 
1777 	/* Save Command State */
1778 	sv_state = CMD_STATE(sc);
1779 
1780 	/*
1781 	 * Any pending IO issued prior to reset is expected to be
1782 	 * in abts pending state, if not we need to set SNIC_IOREQ_ABTS_PENDING
1783 	 * to indicate the IO is abort pending.
1784 	 * When IO is completed, the IO will be handed over and handled
1785 	 * in this function.
1786 	 */
1787 
1788 	CMD_STATE(sc) = SNIC_IOREQ_ABTS_PENDING;
1789 	SNIC_BUG_ON(rqi->abts_done);
1790 
1791 	if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET) {
1792 		rqi->tm_tag = SNIC_TAG_DEV_RST;
1793 
1794 		SNIC_SCSI_DBG(snic->shost,
1795 			      "clean_single_req:devrst sc 0x%p\n", sc);
1796 	}
1797 
1798 	CMD_ABTS_STATUS(sc) = SNIC_INVALID_CODE;
1799 	rqi->abts_done = &tm_done;
1800 	spin_unlock_irqrestore(io_lock, flags);
1801 
1802 	tgt = starget_to_tgt(scsi_target(sc->device));
1803 	if ((snic_tgt_chkready(tgt) != 0) && (tgt->tdata.typ == SNIC_TGT_SAN))
1804 		tmf = SNIC_ITMF_ABTS_TASK_TERM;
1805 	else
1806 		tmf = SNIC_ITMF_ABTS_TASK;
1807 
1808 	/* Now queue the abort command to firmware */
1809 	ret = snic_queue_abort_req(snic, rqi, sc, tmf);
1810 	if (ret) {
1811 		SNIC_HOST_ERR(snic->shost,
1812 			      "clean_single_req_err:sc %p, tag %d abt failed. tm_tag %d flags 0x%llx\n",
1813 			      sc, tag, rqi->tm_tag, CMD_FLAGS(sc));
1814 
1815 		spin_lock_irqsave(io_lock, flags);
1816 		rqi = (struct snic_req_info *) CMD_SP(sc);
1817 		if (rqi)
1818 			rqi->abts_done = NULL;
1819 
1820 		/* Restore Command State */
1821 		if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
1822 			CMD_STATE(sc) = sv_state;
1823 
1824 		ret = 1;
1825 		goto skip_clean;
1826 	}
1827 
1828 	spin_lock_irqsave(io_lock, flags);
1829 	if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET)
1830 		CMD_FLAGS(sc) |= SNIC_DEV_RST_TERM_ISSUED;
1831 
1832 	CMD_FLAGS(sc) |= SNIC_IO_INTERNAL_TERM_ISSUED;
1833 	spin_unlock_irqrestore(io_lock, flags);
1834 
1835 	wait_for_completion_timeout(&tm_done, SNIC_ABTS_TIMEOUT);
1836 
1837 	/* Recheck cmd state to check if it now aborted. */
1838 	spin_lock_irqsave(io_lock, flags);
1839 	rqi = (struct snic_req_info *) CMD_SP(sc);
1840 	if (!rqi) {
1841 		CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_REQ_NULL;
1842 		goto skip_clean;
1843 	}
1844 	rqi->abts_done = NULL;
1845 
1846 	/* if abort is still pending w/ fw, fail */
1847 	if (CMD_ABTS_STATUS(sc) == SNIC_INVALID_CODE) {
1848 		SNIC_HOST_ERR(snic->shost,
1849 			      "clean_single_req_err:sc %p tag %d abt still pending w/ fw, tm_tag %d flags 0x%llx\n",
1850 			      sc, tag, rqi->tm_tag, CMD_FLAGS(sc));
1851 
1852 		CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_DONE;
1853 		ret = 1;
1854 
1855 		goto skip_clean;
1856 	}
1857 
1858 	CMD_STATE(sc) = SNIC_IOREQ_ABTS_COMPLETE;
1859 	CMD_SP(sc) = NULL;
1860 	spin_unlock_irqrestore(io_lock, flags);
1861 
1862 	snic_release_req_buf(snic, rqi, sc);
1863 
1864 	sc->result = (DID_ERROR << 16);
1865 	sc->scsi_done(sc);
1866 
1867 	ret = 0;
1868 
1869 	return ret;
1870 
1871 skip_clean:
1872 	spin_unlock_irqrestore(io_lock, flags);
1873 
1874 	return ret;
1875 } /* end of snic_dr_clean_single_req */
1876 
1877 static int
1878 snic_dr_clean_pending_req(struct snic *snic, struct scsi_cmnd *lr_sc)
1879 {
1880 	struct scsi_device *lr_sdev = lr_sc->device;
1881 	u32 tag = 0;
1882 	int ret = FAILED;
1883 
1884 	for (tag = 0; tag < snic->max_tag_id; tag++) {
1885 		if (tag == snic_cmd_tag(lr_sc))
1886 			continue;
1887 
1888 		ret = snic_dr_clean_single_req(snic, tag, lr_sdev);
1889 		if (ret) {
1890 			SNIC_HOST_ERR(snic->shost, "clean_err:tag = %d\n", tag);
1891 
1892 			goto clean_err;
1893 		}
1894 	}
1895 
1896 	schedule_timeout(msecs_to_jiffies(100));
1897 
1898 	/* Walk through all the cmds and check abts status. */
1899 	if (snic_is_abts_pending(snic, lr_sc)) {
1900 		ret = FAILED;
1901 
1902 		goto clean_err;
1903 	}
1904 
1905 	ret = 0;
1906 	SNIC_SCSI_DBG(snic->shost, "clean_pending_req: Success.\n");
1907 
1908 	return ret;
1909 
1910 clean_err:
1911 	ret = FAILED;
1912 	SNIC_HOST_ERR(snic->shost,
1913 		      "Failed to Clean Pending IOs on %s device.\n",
1914 		      dev_name(&lr_sdev->sdev_gendev));
1915 
1916 	return ret;
1917 
1918 } /* end of snic_dr_clean_pending_req */
1919 
1920 /*
1921  * snic_dr_finish : Called by snic_device_reset
1922  */
1923 static int
1924 snic_dr_finish(struct snic *snic, struct scsi_cmnd *sc)
1925 {
1926 	struct snic_req_info *rqi = NULL;
1927 	spinlock_t *io_lock = NULL;
1928 	unsigned long flags;
1929 	int lr_res = 0;
1930 	int ret = FAILED;
1931 
1932 	io_lock = snic_io_lock_hash(snic, sc);
1933 	spin_lock_irqsave(io_lock, flags);
1934 	rqi = (struct snic_req_info *) CMD_SP(sc);
1935 	if (!rqi) {
1936 		spin_unlock_irqrestore(io_lock, flags);
1937 		SNIC_SCSI_DBG(snic->shost,
1938 			      "dr_fini: rqi is null tag 0x%x sc 0x%p flags 0x%llx\n",
1939 			      snic_cmd_tag(sc), sc, CMD_FLAGS(sc));
1940 
1941 		ret = FAILED;
1942 		goto dr_fini_end;
1943 	}
1944 
1945 	rqi->dr_done = NULL;
1946 
1947 	lr_res = CMD_LR_STATUS(sc);
1948 
1949 	switch (lr_res) {
1950 	case SNIC_INVALID_CODE:
1951 		/* stats */
1952 		SNIC_SCSI_DBG(snic->shost,
1953 			      "dr_fini: Tag %x Dev Reset Timedout. flags 0x%llx\n",
1954 			      snic_cmd_tag(sc), CMD_FLAGS(sc));
1955 
1956 		CMD_FLAGS(sc) |= SNIC_DEV_RST_TIMEDOUT;
1957 		ret = FAILED;
1958 
1959 		goto dr_failed;
1960 
1961 	case SNIC_STAT_IO_SUCCESS:
1962 		SNIC_SCSI_DBG(snic->shost,
1963 			      "dr_fini: Tag %x Dev Reset cmpl\n",
1964 			      snic_cmd_tag(sc));
1965 		ret = 0;
1966 		break;
1967 
1968 	default:
1969 		SNIC_HOST_ERR(snic->shost,
1970 			      "dr_fini:Device Reset completed& failed.Tag = %x lr_status %s flags 0x%llx\n",
1971 			      snic_cmd_tag(sc),
1972 			      snic_io_status_to_str(lr_res), CMD_FLAGS(sc));
1973 		ret = FAILED;
1974 		goto dr_failed;
1975 	}
1976 	spin_unlock_irqrestore(io_lock, flags);
1977 
1978 	/*
1979 	 * Cleanup any IOs on this LUN that have still not completed.
1980 	 * If any of these fail, then LUN Reset fails.
1981 	 * Cleanup cleans all commands on this LUN except
1982 	 * the lun reset command. If all cmds get cleaned, the LUN Reset
1983 	 * succeeds.
1984 	 */
1985 
1986 	ret = snic_dr_clean_pending_req(snic, sc);
1987 	if (ret) {
1988 		spin_lock_irqsave(io_lock, flags);
1989 		SNIC_SCSI_DBG(snic->shost,
1990 			      "dr_fini: Device Reset Failed since could not abort all IOs. Tag = %x.\n",
1991 			      snic_cmd_tag(sc));
1992 		rqi = (struct snic_req_info *) CMD_SP(sc);
1993 
1994 		goto dr_failed;
1995 	} else {
1996 		/* Cleanup LUN Reset Command */
1997 		spin_lock_irqsave(io_lock, flags);
1998 		rqi = (struct snic_req_info *) CMD_SP(sc);
1999 		if (rqi)
2000 			ret = SUCCESS; /* Completed Successfully */
2001 		else
2002 			ret = FAILED;
2003 	}
2004 
2005 dr_failed:
2006 	SNIC_BUG_ON(!spin_is_locked(io_lock));
2007 	if (rqi)
2008 		CMD_SP(sc) = NULL;
2009 	spin_unlock_irqrestore(io_lock, flags);
2010 
2011 	if (rqi)
2012 		snic_release_req_buf(snic, rqi, sc);
2013 
2014 dr_fini_end:
2015 	return ret;
2016 } /* end of snic_dr_finish */
2017 
2018 static int
2019 snic_queue_dr_req(struct snic *snic,
2020 		  struct snic_req_info *rqi,
2021 		  struct scsi_cmnd *sc)
2022 {
2023 	/* Add special tag for device reset */
2024 	rqi->tm_tag |= SNIC_TAG_DEV_RST;
2025 
2026 	return snic_issue_tm_req(snic, rqi, sc, SNIC_ITMF_LUN_RESET);
2027 }
2028 
2029 static int
2030 snic_send_dr_and_wait(struct snic *snic, struct scsi_cmnd *sc)
2031 {
2032 	struct snic_req_info *rqi = NULL;
2033 	enum snic_ioreq_state sv_state;
2034 	spinlock_t *io_lock = NULL;
2035 	unsigned long flags;
2036 	DECLARE_COMPLETION_ONSTACK(tm_done);
2037 	int ret = FAILED, tag = snic_cmd_tag(sc);
2038 
2039 	io_lock = snic_io_lock_hash(snic, sc);
2040 	spin_lock_irqsave(io_lock, flags);
2041 	CMD_FLAGS(sc) |= SNIC_DEVICE_RESET;
2042 	rqi = (struct snic_req_info *) CMD_SP(sc);
2043 	if (!rqi) {
2044 		SNIC_HOST_ERR(snic->shost,
2045 			      "send_dr: rqi is null, Tag 0x%x flags 0x%llx\n",
2046 			      tag, CMD_FLAGS(sc));
2047 		spin_unlock_irqrestore(io_lock, flags);
2048 
2049 		ret = FAILED;
2050 		goto send_dr_end;
2051 	}
2052 
2053 	/* Save Command state to restore in case Queuing failed. */
2054 	sv_state = CMD_STATE(sc);
2055 
2056 	CMD_STATE(sc) = SNIC_IOREQ_LR_PENDING;
2057 	CMD_LR_STATUS(sc) = SNIC_INVALID_CODE;
2058 
2059 	SNIC_SCSI_DBG(snic->shost, "dr: TAG = %x\n", tag);
2060 
2061 	rqi->dr_done = &tm_done;
2062 	SNIC_BUG_ON(!rqi->dr_done);
2063 
2064 	spin_unlock_irqrestore(io_lock, flags);
2065 	/*
2066 	 * The Command state is changed to IOREQ_PENDING,
2067 	 * in this case, if the command is completed, the icmnd_cmpl will
2068 	 * mark the cmd as completed.
2069 	 * This logic still makes LUN Reset is inevitable.
2070 	 */
2071 
2072 	ret = snic_queue_dr_req(snic, rqi, sc);
2073 	if (ret) {
2074 		SNIC_HOST_ERR(snic->shost,
2075 			      "send_dr: IO w/ Tag 0x%x Failed err = %d. flags 0x%llx\n",
2076 			      tag, ret, CMD_FLAGS(sc));
2077 
2078 		spin_lock_irqsave(io_lock, flags);
2079 		/* Restore State */
2080 		CMD_STATE(sc) = sv_state;
2081 		rqi = (struct snic_req_info *) CMD_SP(sc);
2082 		if (rqi)
2083 			rqi->dr_done = NULL;
2084 		/* rqi is freed in caller. */
2085 		spin_unlock_irqrestore(io_lock, flags);
2086 		ret = FAILED;
2087 
2088 		goto send_dr_end;
2089 	}
2090 
2091 	spin_lock_irqsave(io_lock, flags);
2092 	CMD_FLAGS(sc) |= SNIC_DEV_RST_ISSUED;
2093 	spin_unlock_irqrestore(io_lock, flags);
2094 
2095 	ret = 0;
2096 
2097 	wait_for_completion_timeout(&tm_done, SNIC_LUN_RESET_TIMEOUT);
2098 
2099 send_dr_end:
2100 	return ret;
2101 }
2102 
2103 /*
2104  * auxillary funciton to check lun reset op is supported or not
2105  * Not supported if returns 0
2106  */
2107 static int
2108 snic_dev_reset_supported(struct scsi_device *sdev)
2109 {
2110 	struct snic_tgt *tgt = starget_to_tgt(scsi_target(sdev));
2111 
2112 	if (tgt->tdata.typ == SNIC_TGT_DAS)
2113 		return 0;
2114 
2115 	return 1;
2116 }
2117 
2118 static void
2119 snic_unlink_and_release_req(struct snic *snic, struct scsi_cmnd *sc, int flag)
2120 {
2121 	struct snic_req_info *rqi = NULL;
2122 	spinlock_t *io_lock = NULL;
2123 	unsigned long flags;
2124 	u32 start_time = jiffies;
2125 
2126 	io_lock = snic_io_lock_hash(snic, sc);
2127 	spin_lock_irqsave(io_lock, flags);
2128 	rqi = (struct snic_req_info *) CMD_SP(sc);
2129 	if (rqi) {
2130 		start_time = rqi->start_time;
2131 		CMD_SP(sc) = NULL;
2132 	}
2133 
2134 	CMD_FLAGS(sc) |= flag;
2135 	spin_unlock_irqrestore(io_lock, flags);
2136 
2137 	if (rqi)
2138 		snic_release_req_buf(snic, rqi, sc);
2139 
2140 	SNIC_TRC(snic->shost->host_no, snic_cmd_tag(sc), (ulong) sc,
2141 		 jiffies_to_msecs(jiffies - start_time), (ulong) rqi,
2142 		 SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
2143 }
2144 
2145 /*
2146  * SCSI Eh thread issues a LUN Reset when one or more commands on a LUN
2147  * fail to get aborted. It calls driver's eh_device_reset with a SCSI
2148  * command on the LUN.
2149  */
2150 int
2151 snic_device_reset(struct scsi_cmnd *sc)
2152 {
2153 	struct Scsi_Host *shost = sc->device->host;
2154 	struct snic *snic = shost_priv(shost);
2155 	struct snic_req_info *rqi = NULL;
2156 	int tag = snic_cmd_tag(sc);
2157 	int start_time = jiffies;
2158 	int ret = FAILED;
2159 	int dr_supp = 0;
2160 
2161 	SNIC_SCSI_DBG(shost, "dev_reset:sc %p :0x%x :req = %p :tag = %d\n",
2162 		      sc, sc->cmnd[0], sc->request,
2163 		      snic_cmd_tag(sc));
2164 	dr_supp = snic_dev_reset_supported(sc->device);
2165 	if (!dr_supp) {
2166 		/* device reset op is not supported */
2167 		SNIC_HOST_INFO(shost, "LUN Reset Op not supported.\n");
2168 		snic_unlink_and_release_req(snic, sc, SNIC_DEV_RST_NOTSUP);
2169 
2170 		goto dev_rst_end;
2171 	}
2172 
2173 	if (unlikely(snic_get_state(snic) != SNIC_ONLINE)) {
2174 		snic_unlink_and_release_req(snic, sc, 0);
2175 		SNIC_HOST_ERR(shost, "Devrst: Parent Devs are not online.\n");
2176 
2177 		goto dev_rst_end;
2178 	}
2179 
2180 	/* There is no tag when lun reset is issue through ioctl. */
2181 	if (unlikely(tag <= SNIC_NO_TAG)) {
2182 		SNIC_HOST_INFO(snic->shost,
2183 			       "Devrst: LUN Reset Recvd thru IOCTL.\n");
2184 
2185 		rqi = snic_req_init(snic, 0);
2186 		if (!rqi)
2187 			goto dev_rst_end;
2188 
2189 		memset(scsi_cmd_priv(sc), 0,
2190 			sizeof(struct snic_internal_io_state));
2191 		CMD_SP(sc) = (char *)rqi;
2192 		CMD_FLAGS(sc) = SNIC_NO_FLAGS;
2193 
2194 		/* Add special tag for dr coming from user spc */
2195 		rqi->tm_tag = SNIC_TAG_IOCTL_DEV_RST;
2196 		rqi->sc = sc;
2197 	}
2198 
2199 	ret = snic_send_dr_and_wait(snic, sc);
2200 	if (ret) {
2201 		SNIC_HOST_ERR(snic->shost,
2202 			      "Devrst: IO w/ Tag %x Failed w/ err = %d\n",
2203 			      tag, ret);
2204 
2205 		snic_unlink_and_release_req(snic, sc, 0);
2206 
2207 		goto dev_rst_end;
2208 	}
2209 
2210 	ret = snic_dr_finish(snic, sc);
2211 
2212 dev_rst_end:
2213 	SNIC_TRC(snic->shost->host_no, tag, (ulong) sc,
2214 		 jiffies_to_msecs(jiffies - start_time),
2215 		 0, SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
2216 
2217 	SNIC_SCSI_DBG(snic->shost,
2218 		      "Devrst: Returning from Device Reset : %s\n",
2219 		      (ret == SUCCESS) ? "SUCCESS" : "FAILED");
2220 
2221 	return ret;
2222 } /* end of snic_device_reset */
2223 
2224 /*
2225  * SCSI Error handling calls driver's eh_host_reset if all prior
2226  * error handling levels return FAILED.
2227  *
2228  * Host Reset is the highest level of error recovery. If this fails, then
2229  * host is offlined by SCSI.
2230  */
2231 /*
2232  * snic_issue_hba_reset : Queues FW Reset Request.
2233  */
2234 static int
2235 snic_issue_hba_reset(struct snic *snic, struct scsi_cmnd *sc)
2236 {
2237 	struct snic_req_info *rqi = NULL;
2238 	struct snic_host_req *req = NULL;
2239 	spinlock_t *io_lock = NULL;
2240 	DECLARE_COMPLETION_ONSTACK(wait);
2241 	unsigned long flags;
2242 	int ret = -ENOMEM;
2243 
2244 	rqi = snic_req_init(snic, 0);
2245 	if (!rqi) {
2246 		ret = -ENOMEM;
2247 
2248 		goto hba_rst_end;
2249 	}
2250 
2251 	if (snic_cmd_tag(sc) == SCSI_NO_TAG) {
2252 		memset(scsi_cmd_priv(sc), 0,
2253 			sizeof(struct snic_internal_io_state));
2254 		SNIC_HOST_INFO(snic->shost, "issu_hr:Host reset thru ioctl.\n");
2255 		rqi->sc = sc;
2256 	}
2257 
2258 	req = rqi_to_req(rqi);
2259 
2260 	io_lock = snic_io_lock_hash(snic, sc);
2261 	spin_lock_irqsave(io_lock, flags);
2262 	SNIC_BUG_ON(CMD_SP(sc) != NULL);
2263 	CMD_STATE(sc) = SNIC_IOREQ_PENDING;
2264 	CMD_SP(sc) = (char *) rqi;
2265 	CMD_FLAGS(sc) |= SNIC_IO_INITIALIZED;
2266 	snic->remove_wait = &wait;
2267 	spin_unlock_irqrestore(io_lock, flags);
2268 
2269 	/* Initialize Request */
2270 	snic_io_hdr_enc(&req->hdr, SNIC_REQ_HBA_RESET, 0, snic_cmd_tag(sc),
2271 			snic->config.hid, 0, (ulong) rqi);
2272 
2273 	req->u.reset.flags = 0;
2274 
2275 	ret = snic_queue_wq_desc(snic, req, sizeof(*req));
2276 	if (ret) {
2277 		SNIC_HOST_ERR(snic->shost,
2278 			      "issu_hr:Queuing HBA Reset Failed. w err %d\n",
2279 			      ret);
2280 
2281 		goto hba_rst_err;
2282 	}
2283 
2284 	spin_lock_irqsave(io_lock, flags);
2285 	CMD_FLAGS(sc) |= SNIC_HOST_RESET_ISSUED;
2286 	spin_unlock_irqrestore(io_lock, flags);
2287 	atomic64_inc(&snic->s_stats.reset.hba_resets);
2288 	SNIC_HOST_INFO(snic->shost, "Queued HBA Reset Successfully.\n");
2289 
2290 	wait_for_completion_timeout(snic->remove_wait,
2291 				    SNIC_HOST_RESET_TIMEOUT);
2292 
2293 	if (snic_get_state(snic) == SNIC_FWRESET) {
2294 		SNIC_HOST_ERR(snic->shost, "reset_cmpl: Reset Timedout.\n");
2295 		ret = -ETIMEDOUT;
2296 
2297 		goto hba_rst_err;
2298 	}
2299 
2300 	spin_lock_irqsave(io_lock, flags);
2301 	snic->remove_wait = NULL;
2302 	rqi = (struct snic_req_info *) CMD_SP(sc);
2303 	CMD_SP(sc) = NULL;
2304 	spin_unlock_irqrestore(io_lock, flags);
2305 
2306 	if (rqi)
2307 		snic_req_free(snic, rqi);
2308 
2309 	ret = 0;
2310 
2311 	return ret;
2312 
2313 hba_rst_err:
2314 	spin_lock_irqsave(io_lock, flags);
2315 	snic->remove_wait = NULL;
2316 	rqi = (struct snic_req_info *) CMD_SP(sc);
2317 	CMD_SP(sc) = NULL;
2318 	spin_unlock_irqrestore(io_lock, flags);
2319 
2320 	if (rqi)
2321 		snic_req_free(snic, rqi);
2322 
2323 hba_rst_end:
2324 	SNIC_HOST_ERR(snic->shost,
2325 		      "reset:HBA Reset Failed w/ err = %d.\n",
2326 		      ret);
2327 
2328 	return ret;
2329 } /* end of snic_issue_hba_reset */
2330 
2331 int
2332 snic_reset(struct Scsi_Host *shost, struct scsi_cmnd *sc)
2333 {
2334 	struct snic *snic = shost_priv(shost);
2335 	enum snic_state sv_state;
2336 	unsigned long flags;
2337 	int ret = FAILED;
2338 
2339 	/* Set snic state as SNIC_FWRESET*/
2340 	sv_state = snic_get_state(snic);
2341 
2342 	spin_lock_irqsave(&snic->snic_lock, flags);
2343 	if (snic_get_state(snic) == SNIC_FWRESET) {
2344 		spin_unlock_irqrestore(&snic->snic_lock, flags);
2345 		SNIC_HOST_INFO(shost, "reset:prev reset is in progres\n");
2346 
2347 		msleep(SNIC_HOST_RESET_TIMEOUT);
2348 		ret = SUCCESS;
2349 
2350 		goto reset_end;
2351 	}
2352 
2353 	snic_set_state(snic, SNIC_FWRESET);
2354 	spin_unlock_irqrestore(&snic->snic_lock, flags);
2355 
2356 
2357 	/* Wait for all the IOs that are entered in Qcmd */
2358 	while (atomic_read(&snic->ios_inflight))
2359 		schedule_timeout(msecs_to_jiffies(1));
2360 
2361 	ret = snic_issue_hba_reset(snic, sc);
2362 	if (ret) {
2363 		SNIC_HOST_ERR(shost,
2364 			      "reset:Host Reset Failed w/ err %d.\n",
2365 			      ret);
2366 		spin_lock_irqsave(&snic->snic_lock, flags);
2367 		snic_set_state(snic, sv_state);
2368 		spin_unlock_irqrestore(&snic->snic_lock, flags);
2369 		atomic64_inc(&snic->s_stats.reset.hba_reset_fail);
2370 		ret = FAILED;
2371 
2372 		goto reset_end;
2373 	}
2374 
2375 	ret = SUCCESS;
2376 
2377 reset_end:
2378 	return ret;
2379 } /* end of snic_reset */
2380 
2381 /*
2382  * SCSI Error handling calls driver's eh_host_reset if all prior
2383  * error handling levels return FAILED.
2384  *
2385  * Host Reset is the highest level of error recovery. If this fails, then
2386  * host is offlined by SCSI.
2387  */
2388 int
2389 snic_host_reset(struct scsi_cmnd *sc)
2390 {
2391 	struct Scsi_Host *shost = sc->device->host;
2392 	u32 start_time  = jiffies;
2393 	int ret = FAILED;
2394 
2395 	SNIC_SCSI_DBG(shost,
2396 		      "host reset:sc %p sc_cmd 0x%x req %p tag %d flags 0x%llx\n",
2397 		      sc, sc->cmnd[0], sc->request,
2398 		      snic_cmd_tag(sc), CMD_FLAGS(sc));
2399 
2400 	ret = snic_reset(shost, sc);
2401 
2402 	SNIC_TRC(shost->host_no, snic_cmd_tag(sc), (ulong) sc,
2403 		 jiffies_to_msecs(jiffies - start_time),
2404 		 0, SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
2405 
2406 	return ret;
2407 } /* end of snic_host_reset */
2408 
2409 /*
2410  * snic_cmpl_pending_tmreq : Caller should hold io_lock
2411  */
2412 static void
2413 snic_cmpl_pending_tmreq(struct snic *snic, struct scsi_cmnd *sc)
2414 {
2415 	struct snic_req_info *rqi = NULL;
2416 
2417 	SNIC_SCSI_DBG(snic->shost,
2418 		      "Completing Pending TM Req sc %p, state %s flags 0x%llx\n",
2419 		      sc, snic_io_status_to_str(CMD_STATE(sc)), CMD_FLAGS(sc));
2420 
2421 	/*
2422 	 * CASE : FW didn't post itmf completion due to PCIe Errors.
2423 	 * Marking the abort status as Success to call scsi completion
2424 	 * in snic_abort_finish()
2425 	 */
2426 	CMD_ABTS_STATUS(sc) = SNIC_STAT_IO_SUCCESS;
2427 
2428 	rqi = (struct snic_req_info *) CMD_SP(sc);
2429 	if (!rqi)
2430 		return;
2431 
2432 	if (rqi->dr_done)
2433 		complete(rqi->dr_done);
2434 	else if (rqi->abts_done)
2435 		complete(rqi->abts_done);
2436 }
2437 
2438 /*
2439  * snic_scsi_cleanup: Walks through tag map and releases the reqs
2440  */
2441 static void
2442 snic_scsi_cleanup(struct snic *snic, int ex_tag)
2443 {
2444 	struct snic_req_info *rqi = NULL;
2445 	struct scsi_cmnd *sc = NULL;
2446 	spinlock_t *io_lock = NULL;
2447 	unsigned long flags;
2448 	int tag;
2449 	u64 st_time = 0;
2450 
2451 	SNIC_SCSI_DBG(snic->shost, "sc_clean: scsi cleanup.\n");
2452 
2453 	for (tag = 0; tag < snic->max_tag_id; tag++) {
2454 		/* Skip ex_tag */
2455 		if (tag == ex_tag)
2456 			continue;
2457 
2458 		io_lock = snic_io_lock_tag(snic, tag);
2459 		spin_lock_irqsave(io_lock, flags);
2460 		sc = scsi_host_find_tag(snic->shost, tag);
2461 		if (!sc) {
2462 			spin_unlock_irqrestore(io_lock, flags);
2463 
2464 			continue;
2465 		}
2466 
2467 		if (unlikely(snic_tmreq_pending(sc))) {
2468 			/*
2469 			 * When FW Completes reset w/o sending completions
2470 			 * for outstanding ios.
2471 			 */
2472 			snic_cmpl_pending_tmreq(snic, sc);
2473 			spin_unlock_irqrestore(io_lock, flags);
2474 
2475 			continue;
2476 		}
2477 
2478 		rqi = (struct snic_req_info *) CMD_SP(sc);
2479 		if (!rqi) {
2480 			spin_unlock_irqrestore(io_lock, flags);
2481 
2482 			goto cleanup;
2483 		}
2484 
2485 		SNIC_SCSI_DBG(snic->shost,
2486 			      "sc_clean: sc %p, rqi %p, tag %d flags 0x%llx\n",
2487 			      sc, rqi, tag, CMD_FLAGS(sc));
2488 
2489 		CMD_SP(sc) = NULL;
2490 		CMD_FLAGS(sc) |= SNIC_SCSI_CLEANUP;
2491 		spin_unlock_irqrestore(io_lock, flags);
2492 		st_time = rqi->start_time;
2493 
2494 		SNIC_HOST_INFO(snic->shost,
2495 			       "sc_clean: Releasing rqi %p : flags 0x%llx\n",
2496 			       rqi, CMD_FLAGS(sc));
2497 
2498 		snic_release_req_buf(snic, rqi, sc);
2499 
2500 cleanup:
2501 		sc->result = DID_TRANSPORT_DISRUPTED << 16;
2502 		SNIC_HOST_INFO(snic->shost,
2503 			       "sc_clean: DID_TRANSPORT_DISRUPTED for sc %p, Tag %d flags 0x%llx rqi %p duration %u msecs\n",
2504 			       sc, sc->request->tag, CMD_FLAGS(sc), rqi,
2505 			       jiffies_to_msecs(jiffies - st_time));
2506 
2507 		/* Update IO stats */
2508 		snic_stats_update_io_cmpl(&snic->s_stats);
2509 
2510 		if (sc->scsi_done) {
2511 			SNIC_TRC(snic->shost->host_no, tag, (ulong) sc,
2512 				 jiffies_to_msecs(jiffies - st_time), 0,
2513 				 SNIC_TRC_CMD(sc),
2514 				 SNIC_TRC_CMD_STATE_FLAGS(sc));
2515 
2516 			sc->scsi_done(sc);
2517 		}
2518 	}
2519 } /* end of snic_scsi_cleanup */
2520 
2521 void
2522 snic_shutdown_scsi_cleanup(struct snic *snic)
2523 {
2524 	SNIC_HOST_INFO(snic->shost, "Shutdown time SCSI Cleanup.\n");
2525 
2526 	snic_scsi_cleanup(snic, SCSI_NO_TAG);
2527 } /* end of snic_shutdown_scsi_cleanup */
2528 
2529 /*
2530  * snic_internal_abort_io
2531  * called by : snic_tgt_scsi_abort_io
2532  */
2533 static int
2534 snic_internal_abort_io(struct snic *snic, struct scsi_cmnd *sc, int tmf)
2535 {
2536 	struct snic_req_info *rqi = NULL;
2537 	spinlock_t *io_lock = NULL;
2538 	unsigned long flags;
2539 	u32 sv_state = 0;
2540 	int ret = 0;
2541 
2542 	io_lock = snic_io_lock_hash(snic, sc);
2543 	spin_lock_irqsave(io_lock, flags);
2544 	rqi = (struct snic_req_info *) CMD_SP(sc);
2545 	if (!rqi)
2546 		goto skip_internal_abts;
2547 
2548 	if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
2549 		goto skip_internal_abts;
2550 
2551 	if ((CMD_FLAGS(sc) & SNIC_DEVICE_RESET) &&
2552 		(!(CMD_FLAGS(sc) & SNIC_DEV_RST_ISSUED))) {
2553 
2554 		SNIC_SCSI_DBG(snic->shost,
2555 			      "internal_abts: dev rst not pending sc 0x%p\n",
2556 			      sc);
2557 
2558 		goto skip_internal_abts;
2559 	}
2560 
2561 
2562 	if (!(CMD_FLAGS(sc) & SNIC_IO_ISSUED)) {
2563 		SNIC_SCSI_DBG(snic->shost,
2564 			"internal_abts: IO not yet issued sc 0x%p tag 0x%x flags 0x%llx state %d\n",
2565 			sc, snic_cmd_tag(sc), CMD_FLAGS(sc), CMD_STATE(sc));
2566 
2567 		goto skip_internal_abts;
2568 	}
2569 
2570 	sv_state = CMD_STATE(sc);
2571 	CMD_STATE(sc) = SNIC_IOREQ_ABTS_PENDING;
2572 	CMD_ABTS_STATUS(sc) = SNIC_INVALID_CODE;
2573 	CMD_FLAGS(sc) |= SNIC_IO_INTERNAL_TERM_PENDING;
2574 
2575 	if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET) {
2576 		/* stats */
2577 		rqi->tm_tag = SNIC_TAG_DEV_RST;
2578 		SNIC_SCSI_DBG(snic->shost, "internal_abts:dev rst sc %p\n", sc);
2579 	}
2580 
2581 	SNIC_SCSI_DBG(snic->shost, "internal_abts: Issuing abts tag %x\n",
2582 		      snic_cmd_tag(sc));
2583 	SNIC_BUG_ON(rqi->abts_done);
2584 	spin_unlock_irqrestore(io_lock, flags);
2585 
2586 	ret = snic_queue_abort_req(snic, rqi, sc, tmf);
2587 	if (ret) {
2588 		SNIC_HOST_ERR(snic->shost,
2589 			      "internal_abts: Tag = %x , Failed w/ err = %d\n",
2590 			      snic_cmd_tag(sc), ret);
2591 
2592 		spin_lock_irqsave(io_lock, flags);
2593 
2594 		if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
2595 			CMD_STATE(sc) = sv_state;
2596 
2597 		goto skip_internal_abts;
2598 	}
2599 
2600 	spin_lock_irqsave(io_lock, flags);
2601 	if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET)
2602 		CMD_FLAGS(sc) |= SNIC_DEV_RST_TERM_ISSUED;
2603 	else
2604 		CMD_FLAGS(sc) |= SNIC_IO_INTERNAL_TERM_ISSUED;
2605 
2606 	ret = SUCCESS;
2607 
2608 skip_internal_abts:
2609 	SNIC_BUG_ON(!spin_is_locked(io_lock));
2610 	spin_unlock_irqrestore(io_lock, flags);
2611 
2612 	return ret;
2613 } /* end of snic_internal_abort_io */
2614 
2615 /*
2616  * snic_tgt_scsi_abort_io : called by snic_tgt_del
2617  */
2618 int
2619 snic_tgt_scsi_abort_io(struct snic_tgt *tgt)
2620 {
2621 	struct snic *snic = NULL;
2622 	struct scsi_cmnd *sc = NULL;
2623 	struct snic_tgt *sc_tgt = NULL;
2624 	spinlock_t *io_lock = NULL;
2625 	unsigned long flags;
2626 	int ret = 0, tag, abt_cnt = 0, tmf = 0;
2627 
2628 	if (!tgt)
2629 		return -1;
2630 
2631 	snic = shost_priv(snic_tgt_to_shost(tgt));
2632 	SNIC_SCSI_DBG(snic->shost, "tgt_abt_io: Cleaning Pending IOs.\n");
2633 
2634 	if (tgt->tdata.typ == SNIC_TGT_DAS)
2635 		tmf = SNIC_ITMF_ABTS_TASK;
2636 	else
2637 		tmf = SNIC_ITMF_ABTS_TASK_TERM;
2638 
2639 	for (tag = 0; tag < snic->max_tag_id; tag++) {
2640 		io_lock = snic_io_lock_tag(snic, tag);
2641 
2642 		spin_lock_irqsave(io_lock, flags);
2643 		sc = scsi_host_find_tag(snic->shost, tag);
2644 		if (!sc) {
2645 			spin_unlock_irqrestore(io_lock, flags);
2646 
2647 			continue;
2648 		}
2649 
2650 		sc_tgt = starget_to_tgt(scsi_target(sc->device));
2651 		if (sc_tgt != tgt) {
2652 			spin_unlock_irqrestore(io_lock, flags);
2653 
2654 			continue;
2655 		}
2656 		spin_unlock_irqrestore(io_lock, flags);
2657 
2658 		ret = snic_internal_abort_io(snic, sc, tmf);
2659 		if (ret < 0) {
2660 			SNIC_HOST_ERR(snic->shost,
2661 				      "tgt_abt_io: Tag %x, Failed w err = %d\n",
2662 				      tag, ret);
2663 
2664 			continue;
2665 		}
2666 
2667 		if (ret == SUCCESS)
2668 			abt_cnt++;
2669 	}
2670 
2671 	SNIC_SCSI_DBG(snic->shost, "tgt_abt_io: abt_cnt = %d\n", abt_cnt);
2672 
2673 	return 0;
2674 } /* end of snic_tgt_scsi_abort_io */
2675