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