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