xref: /openbmc/linux/drivers/scsi/snic/snic_scsi.c (revision 3f5c11a4)
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 		break;
1469 
1470 	default:
1471 		/* Firmware completed abort with error */
1472 		ret = FAILED;
1473 		break;
1474 	}
1475 
1476 	CMD_SP(sc) = NULL;
1477 	SNIC_HOST_INFO(snic->shost,
1478 		       "abt_fini: Tag %x, Cmpl Status %s flags 0x%llx\n",
1479 		       tag, snic_io_status_to_str(CMD_ABTS_STATUS(sc)),
1480 		       CMD_FLAGS(sc));
1481 
1482 abort_fail:
1483 	spin_unlock_irqrestore(io_lock, flags);
1484 	if (rqi)
1485 		snic_release_req_buf(snic, rqi, sc);
1486 
1487 	return ret;
1488 } /* end of snic_abort_finish */
1489 
1490 /*
1491  * snic_send_abort_and_wait : Issues Abort, and Waits
1492  */
1493 static int
1494 snic_send_abort_and_wait(struct snic *snic, struct scsi_cmnd *sc)
1495 {
1496 	struct snic_req_info *rqi = NULL;
1497 	enum snic_ioreq_state sv_state;
1498 	struct snic_tgt *tgt = NULL;
1499 	spinlock_t *io_lock = NULL;
1500 	DECLARE_COMPLETION_ONSTACK(tm_done);
1501 	unsigned long flags;
1502 	int ret = 0, tmf = 0, tag = snic_cmd_tag(sc);
1503 
1504 	tgt = starget_to_tgt(scsi_target(sc->device));
1505 	if ((snic_tgt_chkready(tgt) != 0) && (tgt->tdata.typ == SNIC_TGT_SAN))
1506 		tmf = SNIC_ITMF_ABTS_TASK_TERM;
1507 	else
1508 		tmf = SNIC_ITMF_ABTS_TASK;
1509 
1510 	/* stats */
1511 
1512 	io_lock = snic_io_lock_hash(snic, sc);
1513 
1514 	/*
1515 	 * Avoid a race between SCSI issuing the abort and the device
1516 	 * completing the command.
1517 	 *
1518 	 * If the command is already completed by fw_cmpl code,
1519 	 * we just return SUCCESS from here. This means that the abort
1520 	 * succeeded. In the SCSI ML, since the timeout for command has
1521 	 * happend, the completion wont actually complete the command
1522 	 * and it will be considered as an aborted command
1523 	 *
1524 	 * The CMD_SP will not be cleared except while holding io_lock
1525 	 */
1526 	spin_lock_irqsave(io_lock, flags);
1527 	rqi = (struct snic_req_info *) CMD_SP(sc);
1528 	if (!rqi) {
1529 		spin_unlock_irqrestore(io_lock, flags);
1530 
1531 		SNIC_HOST_ERR(snic->shost,
1532 			      "abt_cmd: rqi is null. Tag %d flags 0x%llx\n",
1533 			      tag, CMD_FLAGS(sc));
1534 
1535 		ret = SUCCESS;
1536 
1537 		goto send_abts_end;
1538 	}
1539 
1540 	rqi->abts_done = &tm_done;
1541 	if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING) {
1542 		spin_unlock_irqrestore(io_lock, flags);
1543 
1544 		ret = 0;
1545 		goto abts_pending;
1546 	}
1547 	SNIC_BUG_ON(!rqi->abts_done);
1548 
1549 	/* Save Command State, should be restored on failed to Queue. */
1550 	sv_state = CMD_STATE(sc);
1551 
1552 	/*
1553 	 * Command is still pending, need to abort it
1554 	 * If the fw completes the command after this point,
1555 	 * the completion won't be done till mid-layer, since abot
1556 	 * has already started.
1557 	 */
1558 	CMD_STATE(sc) = SNIC_IOREQ_ABTS_PENDING;
1559 	CMD_ABTS_STATUS(sc) = SNIC_INVALID_CODE;
1560 
1561 	SNIC_SCSI_DBG(snic->shost, "send_abt_cmd: TAG 0x%x\n", tag);
1562 
1563 	spin_unlock_irqrestore(io_lock, flags);
1564 
1565 	/* Now Queue the abort command to firmware */
1566 	ret = snic_queue_abort_req(snic, rqi, sc, tmf);
1567 	if (ret) {
1568 		atomic64_inc(&snic->s_stats.abts.q_fail);
1569 		SNIC_HOST_ERR(snic->shost,
1570 			      "send_abt_cmd: IO w/ Tag 0x%x fail w/ err %d flags 0x%llx\n",
1571 			      tag, ret, CMD_FLAGS(sc));
1572 
1573 		spin_lock_irqsave(io_lock, flags);
1574 		/* Restore Command's previous state */
1575 		CMD_STATE(sc) = sv_state;
1576 		rqi = (struct snic_req_info *) CMD_SP(sc);
1577 		if (rqi)
1578 			rqi->abts_done = NULL;
1579 		spin_unlock_irqrestore(io_lock, flags);
1580 		ret = FAILED;
1581 
1582 		goto send_abts_end;
1583 	}
1584 
1585 	spin_lock_irqsave(io_lock, flags);
1586 	if (tmf == SNIC_ITMF_ABTS_TASK) {
1587 		CMD_FLAGS(sc) |= SNIC_IO_ABTS_ISSUED;
1588 		atomic64_inc(&snic->s_stats.abts.num);
1589 	} else {
1590 		/* term stats */
1591 		CMD_FLAGS(sc) |= SNIC_IO_TERM_ISSUED;
1592 	}
1593 	spin_unlock_irqrestore(io_lock, flags);
1594 
1595 	SNIC_SCSI_DBG(snic->shost,
1596 		      "send_abt_cmd: sc %p Tag %x flags 0x%llx\n",
1597 		      sc, tag, CMD_FLAGS(sc));
1598 
1599 
1600 	ret = 0;
1601 
1602 abts_pending:
1603 	/*
1604 	 * Queued an abort IO, wait for its completion.
1605 	 * Once the fw completes the abort command, it will
1606 	 * wakeup this thread.
1607 	 */
1608 	wait_for_completion_timeout(&tm_done, SNIC_ABTS_TIMEOUT);
1609 
1610 send_abts_end:
1611 	return ret;
1612 } /* end of snic_send_abort_and_wait */
1613 
1614 /*
1615  * This function is exported to SCSI for sending abort cmnds.
1616  * A SCSI IO is represent by snic_ioreq in the driver.
1617  * The snic_ioreq is linked to the SCSI Cmd, thus a link with the ULP'S IO
1618  */
1619 int
1620 snic_abort_cmd(struct scsi_cmnd *sc)
1621 {
1622 	struct snic *snic = shost_priv(sc->device->host);
1623 	int ret = SUCCESS, tag = snic_cmd_tag(sc);
1624 	u32 start_time = jiffies;
1625 
1626 	SNIC_SCSI_DBG(snic->shost, "abt_cmd:sc %p :0x%x :req = %p :tag = %d\n",
1627 		       sc, sc->cmnd[0], sc->request, tag);
1628 
1629 	if (unlikely(snic_get_state(snic) != SNIC_ONLINE)) {
1630 		SNIC_HOST_ERR(snic->shost,
1631 			      "abt_cmd: tag %x Parent Devs are not rdy\n",
1632 			      tag);
1633 		ret = FAST_IO_FAIL;
1634 
1635 		goto abort_end;
1636 	}
1637 
1638 
1639 	ret = snic_send_abort_and_wait(snic, sc);
1640 	if (ret)
1641 		goto abort_end;
1642 
1643 	ret = snic_abort_finish(snic, sc);
1644 
1645 abort_end:
1646 	SNIC_TRC(snic->shost->host_no, tag, (ulong) sc,
1647 		 jiffies_to_msecs(jiffies - start_time), 0,
1648 		 SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
1649 
1650 	SNIC_SCSI_DBG(snic->shost,
1651 		      "abts: Abort Req Status = %s\n",
1652 		      (ret == SUCCESS) ? "SUCCESS" :
1653 		       ((ret == FAST_IO_FAIL) ? "FAST_IO_FAIL" : "FAILED"));
1654 
1655 	return ret;
1656 }
1657 
1658 
1659 
1660 static int
1661 snic_is_abts_pending(struct snic *snic, struct scsi_cmnd *lr_sc)
1662 {
1663 	struct snic_req_info *rqi = NULL;
1664 	struct scsi_cmnd *sc = NULL;
1665 	struct scsi_device *lr_sdev = NULL;
1666 	spinlock_t *io_lock = NULL;
1667 	u32 tag;
1668 	unsigned long flags;
1669 
1670 	if (lr_sc)
1671 		lr_sdev = lr_sc->device;
1672 
1673 	/* walk through the tag map, an dcheck if IOs are still pending in fw*/
1674 	for (tag = 0; tag < snic->max_tag_id; tag++) {
1675 		io_lock = snic_io_lock_tag(snic, tag);
1676 
1677 		spin_lock_irqsave(io_lock, flags);
1678 		sc = scsi_host_find_tag(snic->shost, tag);
1679 
1680 		if (!sc || (lr_sc && (sc->device != lr_sdev || sc == lr_sc))) {
1681 			spin_unlock_irqrestore(io_lock, flags);
1682 
1683 			continue;
1684 		}
1685 
1686 		rqi = (struct snic_req_info *) CMD_SP(sc);
1687 		if (!rqi) {
1688 			spin_unlock_irqrestore(io_lock, flags);
1689 
1690 			continue;
1691 		}
1692 
1693 		/*
1694 		 * Found IO that is still pending w/ firmware and belongs to
1695 		 * the LUN that is under reset, if lr_sc != NULL
1696 		 */
1697 		SNIC_SCSI_DBG(snic->shost, "Found IO in %s on LUN\n",
1698 			      snic_ioreq_state_to_str(CMD_STATE(sc)));
1699 
1700 		if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING) {
1701 			spin_unlock_irqrestore(io_lock, flags);
1702 
1703 			return 1;
1704 		}
1705 
1706 		spin_unlock_irqrestore(io_lock, flags);
1707 	}
1708 
1709 	return 0;
1710 } /* end of snic_is_abts_pending */
1711 
1712 static int
1713 snic_dr_clean_single_req(struct snic *snic,
1714 			 u32 tag,
1715 			 struct scsi_device *lr_sdev)
1716 {
1717 	struct snic_req_info *rqi = NULL;
1718 	struct snic_tgt *tgt = NULL;
1719 	struct scsi_cmnd *sc = NULL;
1720 	spinlock_t *io_lock = NULL;
1721 	u32 sv_state = 0, tmf = 0;
1722 	DECLARE_COMPLETION_ONSTACK(tm_done);
1723 	unsigned long flags;
1724 	int ret = 0;
1725 
1726 	io_lock = snic_io_lock_tag(snic, tag);
1727 	spin_lock_irqsave(io_lock, flags);
1728 	sc = scsi_host_find_tag(snic->shost, tag);
1729 
1730 	/* Ignore Cmd that don't belong to Lun Reset device */
1731 	if (!sc || sc->device != lr_sdev)
1732 		goto skip_clean;
1733 
1734 	rqi = (struct snic_req_info *) CMD_SP(sc);
1735 
1736 	if (!rqi)
1737 		goto skip_clean;
1738 
1739 
1740 	if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
1741 		goto skip_clean;
1742 
1743 
1744 	if ((CMD_FLAGS(sc) & SNIC_DEVICE_RESET) &&
1745 			(!(CMD_FLAGS(sc) & SNIC_DEV_RST_ISSUED))) {
1746 
1747 		SNIC_SCSI_DBG(snic->shost,
1748 			      "clean_single_req: devrst is not pending sc 0x%p\n",
1749 			      sc);
1750 
1751 		goto skip_clean;
1752 	}
1753 
1754 	SNIC_SCSI_DBG(snic->shost,
1755 		"clean_single_req: Found IO in %s on lun\n",
1756 		snic_ioreq_state_to_str(CMD_STATE(sc)));
1757 
1758 	/* Save Command State */
1759 	sv_state = CMD_STATE(sc);
1760 
1761 	/*
1762 	 * Any pending IO issued prior to reset is expected to be
1763 	 * in abts pending state, if not we need to set SNIC_IOREQ_ABTS_PENDING
1764 	 * to indicate the IO is abort pending.
1765 	 * When IO is completed, the IO will be handed over and handled
1766 	 * in this function.
1767 	 */
1768 
1769 	CMD_STATE(sc) = SNIC_IOREQ_ABTS_PENDING;
1770 	SNIC_BUG_ON(rqi->abts_done);
1771 
1772 	if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET) {
1773 		rqi->tm_tag = SNIC_TAG_DEV_RST;
1774 
1775 		SNIC_SCSI_DBG(snic->shost,
1776 			      "clean_single_req:devrst sc 0x%p\n", sc);
1777 	}
1778 
1779 	CMD_ABTS_STATUS(sc) = SNIC_INVALID_CODE;
1780 	rqi->abts_done = &tm_done;
1781 	spin_unlock_irqrestore(io_lock, flags);
1782 
1783 	tgt = starget_to_tgt(scsi_target(sc->device));
1784 	if ((snic_tgt_chkready(tgt) != 0) && (tgt->tdata.typ == SNIC_TGT_SAN))
1785 		tmf = SNIC_ITMF_ABTS_TASK_TERM;
1786 	else
1787 		tmf = SNIC_ITMF_ABTS_TASK;
1788 
1789 	/* Now queue the abort command to firmware */
1790 	ret = snic_queue_abort_req(snic, rqi, sc, tmf);
1791 	if (ret) {
1792 		SNIC_HOST_ERR(snic->shost,
1793 			      "clean_single_req_err:sc %p, tag %d abt failed. tm_tag %d flags 0x%llx\n",
1794 			      sc, tag, rqi->tm_tag, CMD_FLAGS(sc));
1795 
1796 		spin_lock_irqsave(io_lock, flags);
1797 		rqi = (struct snic_req_info *) CMD_SP(sc);
1798 		if (rqi)
1799 			rqi->abts_done = NULL;
1800 
1801 		/* Restore Command State */
1802 		if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
1803 			CMD_STATE(sc) = sv_state;
1804 
1805 		ret = 1;
1806 		goto skip_clean;
1807 	}
1808 
1809 	spin_lock_irqsave(io_lock, flags);
1810 	if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET)
1811 		CMD_FLAGS(sc) |= SNIC_DEV_RST_TERM_ISSUED;
1812 
1813 	CMD_FLAGS(sc) |= SNIC_IO_INTERNAL_TERM_ISSUED;
1814 	spin_unlock_irqrestore(io_lock, flags);
1815 
1816 	wait_for_completion_timeout(&tm_done, SNIC_ABTS_TIMEOUT);
1817 
1818 	/* Recheck cmd state to check if it now aborted. */
1819 	spin_lock_irqsave(io_lock, flags);
1820 	rqi = (struct snic_req_info *) CMD_SP(sc);
1821 	if (!rqi) {
1822 		CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_REQ_NULL;
1823 		goto skip_clean;
1824 	}
1825 	rqi->abts_done = NULL;
1826 
1827 	/* if abort is still pending w/ fw, fail */
1828 	if (CMD_ABTS_STATUS(sc) == SNIC_INVALID_CODE) {
1829 		SNIC_HOST_ERR(snic->shost,
1830 			      "clean_single_req_err:sc %p tag %d abt still pending w/ fw, tm_tag %d flags 0x%llx\n",
1831 			      sc, tag, rqi->tm_tag, CMD_FLAGS(sc));
1832 
1833 		CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_DONE;
1834 		ret = 1;
1835 
1836 		goto skip_clean;
1837 	}
1838 
1839 	CMD_STATE(sc) = SNIC_IOREQ_ABTS_COMPLETE;
1840 	CMD_SP(sc) = NULL;
1841 	spin_unlock_irqrestore(io_lock, flags);
1842 
1843 	snic_release_req_buf(snic, rqi, sc);
1844 
1845 	ret = 0;
1846 
1847 	return ret;
1848 
1849 skip_clean:
1850 	spin_unlock_irqrestore(io_lock, flags);
1851 
1852 	return ret;
1853 } /* end of snic_dr_clean_single_req */
1854 
1855 static int
1856 snic_dr_clean_pending_req(struct snic *snic, struct scsi_cmnd *lr_sc)
1857 {
1858 	struct scsi_device *lr_sdev = lr_sc->device;
1859 	u32 tag = 0;
1860 	int ret = FAILED;
1861 
1862 	for (tag = 0; tag < snic->max_tag_id; tag++) {
1863 		if (tag == snic_cmd_tag(lr_sc))
1864 			continue;
1865 
1866 		ret = snic_dr_clean_single_req(snic, tag, lr_sdev);
1867 		if (ret) {
1868 			SNIC_HOST_ERR(snic->shost, "clean_err:tag = %d\n", tag);
1869 
1870 			goto clean_err;
1871 		}
1872 	}
1873 
1874 	schedule_timeout(msecs_to_jiffies(100));
1875 
1876 	/* Walk through all the cmds and check abts status. */
1877 	if (snic_is_abts_pending(snic, lr_sc)) {
1878 		ret = FAILED;
1879 
1880 		goto clean_err;
1881 	}
1882 
1883 	ret = 0;
1884 	SNIC_SCSI_DBG(snic->shost, "clean_pending_req: Success.\n");
1885 
1886 	return ret;
1887 
1888 clean_err:
1889 	ret = FAILED;
1890 	SNIC_HOST_ERR(snic->shost,
1891 		      "Failed to Clean Pending IOs on %s device.\n",
1892 		      dev_name(&lr_sdev->sdev_gendev));
1893 
1894 	return ret;
1895 
1896 } /* end of snic_dr_clean_pending_req */
1897 
1898 /*
1899  * snic_dr_finish : Called by snic_device_reset
1900  */
1901 static int
1902 snic_dr_finish(struct snic *snic, struct scsi_cmnd *sc)
1903 {
1904 	struct snic_req_info *rqi = NULL;
1905 	spinlock_t *io_lock = NULL;
1906 	unsigned long flags;
1907 	int lr_res = 0;
1908 	int ret = FAILED;
1909 
1910 	io_lock = snic_io_lock_hash(snic, sc);
1911 	spin_lock_irqsave(io_lock, flags);
1912 	rqi = (struct snic_req_info *) CMD_SP(sc);
1913 	if (!rqi) {
1914 		spin_unlock_irqrestore(io_lock, flags);
1915 		SNIC_SCSI_DBG(snic->shost,
1916 			      "dr_fini: rqi is null tag 0x%x sc 0x%p flags 0x%llx\n",
1917 			      snic_cmd_tag(sc), sc, CMD_FLAGS(sc));
1918 
1919 		ret = FAILED;
1920 		goto dr_fini_end;
1921 	}
1922 
1923 	rqi->dr_done = NULL;
1924 
1925 	lr_res = CMD_LR_STATUS(sc);
1926 
1927 	switch (lr_res) {
1928 	case SNIC_INVALID_CODE:
1929 		/* stats */
1930 		SNIC_SCSI_DBG(snic->shost,
1931 			      "dr_fini: Tag %x Dev Reset Timedout. flags 0x%llx\n",
1932 			      snic_cmd_tag(sc), CMD_FLAGS(sc));
1933 
1934 		CMD_FLAGS(sc) |= SNIC_DEV_RST_TIMEDOUT;
1935 		ret = FAILED;
1936 
1937 		goto dr_failed;
1938 
1939 	case SNIC_STAT_IO_SUCCESS:
1940 		SNIC_SCSI_DBG(snic->shost,
1941 			      "dr_fini: Tag %x Dev Reset cmpl\n",
1942 			      snic_cmd_tag(sc));
1943 		ret = 0;
1944 		break;
1945 
1946 	default:
1947 		SNIC_HOST_ERR(snic->shost,
1948 			      "dr_fini:Device Reset completed& failed.Tag = %x lr_status %s flags 0x%llx\n",
1949 			      snic_cmd_tag(sc),
1950 			      snic_io_status_to_str(lr_res), CMD_FLAGS(sc));
1951 		ret = FAILED;
1952 		goto dr_failed;
1953 	}
1954 	spin_unlock_irqrestore(io_lock, flags);
1955 
1956 	/*
1957 	 * Cleanup any IOs on this LUN that have still not completed.
1958 	 * If any of these fail, then LUN Reset fails.
1959 	 * Cleanup cleans all commands on this LUN except
1960 	 * the lun reset command. If all cmds get cleaned, the LUN Reset
1961 	 * succeeds.
1962 	 */
1963 
1964 	ret = snic_dr_clean_pending_req(snic, sc);
1965 	if (ret) {
1966 		spin_lock_irqsave(io_lock, flags);
1967 		SNIC_SCSI_DBG(snic->shost,
1968 			      "dr_fini: Device Reset Failed since could not abort all IOs. Tag = %x.\n",
1969 			      snic_cmd_tag(sc));
1970 		rqi = (struct snic_req_info *) CMD_SP(sc);
1971 
1972 		goto dr_failed;
1973 	} else {
1974 		/* Cleanup LUN Reset Command */
1975 		spin_lock_irqsave(io_lock, flags);
1976 		rqi = (struct snic_req_info *) CMD_SP(sc);
1977 		if (rqi)
1978 			ret = SUCCESS; /* Completed Successfully */
1979 		else
1980 			ret = FAILED;
1981 	}
1982 
1983 dr_failed:
1984 	SNIC_BUG_ON(!spin_is_locked(io_lock));
1985 	if (rqi)
1986 		CMD_SP(sc) = NULL;
1987 	spin_unlock_irqrestore(io_lock, flags);
1988 
1989 	if (rqi)
1990 		snic_release_req_buf(snic, rqi, sc);
1991 
1992 dr_fini_end:
1993 	return ret;
1994 } /* end of snic_dr_finish */
1995 
1996 static int
1997 snic_queue_dr_req(struct snic *snic,
1998 		  struct snic_req_info *rqi,
1999 		  struct scsi_cmnd *sc)
2000 {
2001 	/* Add special tag for device reset */
2002 	rqi->tm_tag |= SNIC_TAG_DEV_RST;
2003 
2004 	return snic_issue_tm_req(snic, rqi, sc, SNIC_ITMF_LUN_RESET);
2005 }
2006 
2007 static int
2008 snic_send_dr_and_wait(struct snic *snic, struct scsi_cmnd *sc)
2009 {
2010 	struct snic_req_info *rqi = NULL;
2011 	enum snic_ioreq_state sv_state;
2012 	spinlock_t *io_lock = NULL;
2013 	unsigned long flags;
2014 	DECLARE_COMPLETION_ONSTACK(tm_done);
2015 	int ret = FAILED, tag = snic_cmd_tag(sc);
2016 
2017 	io_lock = snic_io_lock_hash(snic, sc);
2018 	spin_lock_irqsave(io_lock, flags);
2019 	CMD_FLAGS(sc) |= SNIC_DEVICE_RESET;
2020 	rqi = (struct snic_req_info *) CMD_SP(sc);
2021 	if (!rqi) {
2022 		SNIC_HOST_ERR(snic->shost,
2023 			      "send_dr: rqi is null, Tag 0x%x flags 0x%llx\n",
2024 			      tag, CMD_FLAGS(sc));
2025 		spin_unlock_irqrestore(io_lock, flags);
2026 
2027 		ret = FAILED;
2028 		goto send_dr_end;
2029 	}
2030 
2031 	/* Save Command state to restore in case Queuing failed. */
2032 	sv_state = CMD_STATE(sc);
2033 
2034 	CMD_STATE(sc) = SNIC_IOREQ_LR_PENDING;
2035 	CMD_LR_STATUS(sc) = SNIC_INVALID_CODE;
2036 
2037 	SNIC_SCSI_DBG(snic->shost, "dr: TAG = %x\n", tag);
2038 
2039 	rqi->dr_done = &tm_done;
2040 	SNIC_BUG_ON(!rqi->dr_done);
2041 
2042 	spin_unlock_irqrestore(io_lock, flags);
2043 	/*
2044 	 * The Command state is changed to IOREQ_PENDING,
2045 	 * in this case, if the command is completed, the icmnd_cmpl will
2046 	 * mark the cmd as completed.
2047 	 * This logic still makes LUN Reset is inevitable.
2048 	 */
2049 
2050 	ret = snic_queue_dr_req(snic, rqi, sc);
2051 	if (ret) {
2052 		SNIC_HOST_ERR(snic->shost,
2053 			      "send_dr: IO w/ Tag 0x%x Failed err = %d. flags 0x%llx\n",
2054 			      tag, ret, CMD_FLAGS(sc));
2055 
2056 		spin_lock_irqsave(io_lock, flags);
2057 		/* Restore State */
2058 		CMD_STATE(sc) = sv_state;
2059 		rqi = (struct snic_req_info *) CMD_SP(sc);
2060 		if (rqi)
2061 			rqi->dr_done = NULL;
2062 		/* rqi is freed in caller. */
2063 		spin_unlock_irqrestore(io_lock, flags);
2064 		ret = FAILED;
2065 
2066 		goto send_dr_end;
2067 	}
2068 
2069 	spin_lock_irqsave(io_lock, flags);
2070 	CMD_FLAGS(sc) |= SNIC_DEV_RST_ISSUED;
2071 	spin_unlock_irqrestore(io_lock, flags);
2072 
2073 	ret = 0;
2074 
2075 	wait_for_completion_timeout(&tm_done, SNIC_LUN_RESET_TIMEOUT);
2076 
2077 send_dr_end:
2078 	return ret;
2079 }
2080 
2081 /*
2082  * auxillary funciton to check lun reset op is supported or not
2083  * Not supported if returns 0
2084  */
2085 static int
2086 snic_dev_reset_supported(struct scsi_device *sdev)
2087 {
2088 	struct snic_tgt *tgt = starget_to_tgt(scsi_target(sdev));
2089 
2090 	if (tgt->tdata.typ == SNIC_TGT_DAS)
2091 		return 0;
2092 
2093 	return 1;
2094 }
2095 
2096 static void
2097 snic_unlink_and_release_req(struct snic *snic, struct scsi_cmnd *sc, int flag)
2098 {
2099 	struct snic_req_info *rqi = NULL;
2100 	spinlock_t *io_lock = NULL;
2101 	unsigned long flags;
2102 	u32 start_time = jiffies;
2103 
2104 	io_lock = snic_io_lock_hash(snic, sc);
2105 	spin_lock_irqsave(io_lock, flags);
2106 	rqi = (struct snic_req_info *) CMD_SP(sc);
2107 	if (rqi) {
2108 		start_time = rqi->start_time;
2109 		CMD_SP(sc) = NULL;
2110 	}
2111 
2112 	CMD_FLAGS(sc) |= flag;
2113 	spin_unlock_irqrestore(io_lock, flags);
2114 
2115 	if (rqi)
2116 		snic_release_req_buf(snic, rqi, sc);
2117 
2118 	SNIC_TRC(snic->shost->host_no, snic_cmd_tag(sc), (ulong) sc,
2119 		 jiffies_to_msecs(jiffies - start_time), (ulong) rqi,
2120 		 SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
2121 }
2122 
2123 /*
2124  * SCSI Eh thread issues a LUN Reset when one or more commands on a LUN
2125  * fail to get aborted. It calls driver's eh_device_reset with a SCSI
2126  * command on the LUN.
2127  */
2128 int
2129 snic_device_reset(struct scsi_cmnd *sc)
2130 {
2131 	struct Scsi_Host *shost = sc->device->host;
2132 	struct snic *snic = shost_priv(shost);
2133 	struct snic_req_info *rqi = NULL;
2134 	int tag = snic_cmd_tag(sc);
2135 	int start_time = jiffies;
2136 	int ret = FAILED;
2137 	int dr_supp = 0;
2138 
2139 	SNIC_SCSI_DBG(shost, "dev_reset:sc %p :0x%x :req = %p :tag = %d\n",
2140 		      sc, sc->cmnd[0], sc->request,
2141 		      snic_cmd_tag(sc));
2142 	dr_supp = snic_dev_reset_supported(sc->device);
2143 	if (!dr_supp) {
2144 		/* device reset op is not supported */
2145 		SNIC_HOST_INFO(shost, "LUN Reset Op not supported.\n");
2146 		snic_unlink_and_release_req(snic, sc, SNIC_DEV_RST_NOTSUP);
2147 
2148 		goto dev_rst_end;
2149 	}
2150 
2151 	if (unlikely(snic_get_state(snic) != SNIC_ONLINE)) {
2152 		snic_unlink_and_release_req(snic, sc, 0);
2153 		SNIC_HOST_ERR(shost, "Devrst: Parent Devs are not online.\n");
2154 
2155 		goto dev_rst_end;
2156 	}
2157 
2158 	/* There is no tag when lun reset is issue through ioctl. */
2159 	if (unlikely(tag <= SNIC_NO_TAG)) {
2160 		SNIC_HOST_INFO(snic->shost,
2161 			       "Devrst: LUN Reset Recvd thru IOCTL.\n");
2162 
2163 		rqi = snic_req_init(snic, 0);
2164 		if (!rqi)
2165 			goto dev_rst_end;
2166 
2167 		memset(scsi_cmd_priv(sc), 0,
2168 			sizeof(struct snic_internal_io_state));
2169 		CMD_SP(sc) = (char *)rqi;
2170 		CMD_FLAGS(sc) = SNIC_NO_FLAGS;
2171 
2172 		/* Add special tag for dr coming from user spc */
2173 		rqi->tm_tag = SNIC_TAG_IOCTL_DEV_RST;
2174 		rqi->sc = sc;
2175 	}
2176 
2177 	ret = snic_send_dr_and_wait(snic, sc);
2178 	if (ret) {
2179 		SNIC_HOST_ERR(snic->shost,
2180 			      "Devrst: IO w/ Tag %x Failed w/ err = %d\n",
2181 			      tag, ret);
2182 
2183 		snic_unlink_and_release_req(snic, sc, 0);
2184 
2185 		goto dev_rst_end;
2186 	}
2187 
2188 	ret = snic_dr_finish(snic, sc);
2189 
2190 dev_rst_end:
2191 	SNIC_TRC(snic->shost->host_no, tag, (ulong) sc,
2192 		 jiffies_to_msecs(jiffies - start_time),
2193 		 0, SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
2194 
2195 	SNIC_SCSI_DBG(snic->shost,
2196 		      "Devrst: Returning from Device Reset : %s\n",
2197 		      (ret == SUCCESS) ? "SUCCESS" : "FAILED");
2198 
2199 	return ret;
2200 } /* end of snic_device_reset */
2201 
2202 /*
2203  * SCSI Error handling calls driver's eh_host_reset if all prior
2204  * error handling levels return FAILED.
2205  *
2206  * Host Reset is the highest level of error recovery. If this fails, then
2207  * host is offlined by SCSI.
2208  */
2209 /*
2210  * snic_issue_hba_reset : Queues FW Reset Request.
2211  */
2212 static int
2213 snic_issue_hba_reset(struct snic *snic, struct scsi_cmnd *sc)
2214 {
2215 	struct snic_req_info *rqi = NULL;
2216 	struct snic_host_req *req = NULL;
2217 	spinlock_t *io_lock = NULL;
2218 	DECLARE_COMPLETION_ONSTACK(wait);
2219 	unsigned long flags;
2220 	int ret = -ENOMEM;
2221 
2222 	rqi = snic_req_init(snic, 0);
2223 	if (!rqi) {
2224 		ret = -ENOMEM;
2225 
2226 		goto hba_rst_end;
2227 	}
2228 
2229 	if (snic_cmd_tag(sc) == SCSI_NO_TAG) {
2230 		memset(scsi_cmd_priv(sc), 0,
2231 			sizeof(struct snic_internal_io_state));
2232 		SNIC_HOST_INFO(snic->shost, "issu_hr:Host reset thru ioctl.\n");
2233 		rqi->sc = sc;
2234 	}
2235 
2236 	req = rqi_to_req(rqi);
2237 
2238 	io_lock = snic_io_lock_hash(snic, sc);
2239 	spin_lock_irqsave(io_lock, flags);
2240 	SNIC_BUG_ON(CMD_SP(sc) != NULL);
2241 	CMD_STATE(sc) = SNIC_IOREQ_PENDING;
2242 	CMD_SP(sc) = (char *) rqi;
2243 	CMD_FLAGS(sc) |= SNIC_IO_INITIALIZED;
2244 	snic->remove_wait = &wait;
2245 	spin_unlock_irqrestore(io_lock, flags);
2246 
2247 	/* Initialize Request */
2248 	snic_io_hdr_enc(&req->hdr, SNIC_REQ_HBA_RESET, 0, snic_cmd_tag(sc),
2249 			snic->config.hid, 0, (ulong) rqi);
2250 
2251 	req->u.reset.flags = 0;
2252 
2253 	ret = snic_queue_wq_desc(snic, req, sizeof(*req));
2254 	if (ret) {
2255 		SNIC_HOST_ERR(snic->shost,
2256 			      "issu_hr:Queuing HBA Reset Failed. w err %d\n",
2257 			      ret);
2258 
2259 		goto hba_rst_err;
2260 	}
2261 
2262 	spin_lock_irqsave(io_lock, flags);
2263 	CMD_FLAGS(sc) |= SNIC_HOST_RESET_ISSUED;
2264 	spin_unlock_irqrestore(io_lock, flags);
2265 	atomic64_inc(&snic->s_stats.reset.hba_resets);
2266 	SNIC_HOST_INFO(snic->shost, "Queued HBA Reset Successfully.\n");
2267 
2268 	wait_for_completion_timeout(snic->remove_wait,
2269 				    SNIC_HOST_RESET_TIMEOUT);
2270 
2271 	if (snic_get_state(snic) == SNIC_FWRESET) {
2272 		SNIC_HOST_ERR(snic->shost, "reset_cmpl: Reset Timedout.\n");
2273 		ret = -ETIMEDOUT;
2274 
2275 		goto hba_rst_err;
2276 	}
2277 
2278 	spin_lock_irqsave(io_lock, flags);
2279 	snic->remove_wait = NULL;
2280 	rqi = (struct snic_req_info *) CMD_SP(sc);
2281 	CMD_SP(sc) = NULL;
2282 	spin_unlock_irqrestore(io_lock, flags);
2283 
2284 	if (rqi)
2285 		snic_req_free(snic, rqi);
2286 
2287 	ret = 0;
2288 
2289 	return ret;
2290 
2291 hba_rst_err:
2292 	spin_lock_irqsave(io_lock, flags);
2293 	snic->remove_wait = NULL;
2294 	rqi = (struct snic_req_info *) CMD_SP(sc);
2295 	CMD_SP(sc) = NULL;
2296 	spin_unlock_irqrestore(io_lock, flags);
2297 
2298 	if (rqi)
2299 		snic_req_free(snic, rqi);
2300 
2301 hba_rst_end:
2302 	SNIC_HOST_ERR(snic->shost,
2303 		      "reset:HBA Reset Failed w/ err = %d.\n",
2304 		      ret);
2305 
2306 	return ret;
2307 } /* end of snic_issue_hba_reset */
2308 
2309 int
2310 snic_reset(struct Scsi_Host *shost, struct scsi_cmnd *sc)
2311 {
2312 	struct snic *snic = shost_priv(shost);
2313 	enum snic_state sv_state;
2314 	unsigned long flags;
2315 	int ret = FAILED;
2316 
2317 	/* Set snic state as SNIC_FWRESET*/
2318 	sv_state = snic_get_state(snic);
2319 
2320 	spin_lock_irqsave(&snic->snic_lock, flags);
2321 	if (snic_get_state(snic) == SNIC_FWRESET) {
2322 		spin_unlock_irqrestore(&snic->snic_lock, flags);
2323 		SNIC_HOST_INFO(shost, "reset:prev reset is in progres\n");
2324 
2325 		msleep(SNIC_HOST_RESET_TIMEOUT);
2326 		ret = SUCCESS;
2327 
2328 		goto reset_end;
2329 	}
2330 
2331 	snic_set_state(snic, SNIC_FWRESET);
2332 	spin_unlock_irqrestore(&snic->snic_lock, flags);
2333 
2334 
2335 	/* Wait for all the IOs that are entered in Qcmd */
2336 	while (atomic_read(&snic->ios_inflight))
2337 		schedule_timeout(msecs_to_jiffies(1));
2338 
2339 	ret = snic_issue_hba_reset(snic, sc);
2340 	if (ret) {
2341 		SNIC_HOST_ERR(shost,
2342 			      "reset:Host Reset Failed w/ err %d.\n",
2343 			      ret);
2344 		spin_lock_irqsave(&snic->snic_lock, flags);
2345 		snic_set_state(snic, sv_state);
2346 		spin_unlock_irqrestore(&snic->snic_lock, flags);
2347 		atomic64_inc(&snic->s_stats.reset.hba_reset_fail);
2348 		ret = FAILED;
2349 
2350 		goto reset_end;
2351 	}
2352 
2353 	ret = SUCCESS;
2354 
2355 reset_end:
2356 	return ret;
2357 } /* end of snic_reset */
2358 
2359 /*
2360  * SCSI Error handling calls driver's eh_host_reset if all prior
2361  * error handling levels return FAILED.
2362  *
2363  * Host Reset is the highest level of error recovery. If this fails, then
2364  * host is offlined by SCSI.
2365  */
2366 int
2367 snic_host_reset(struct scsi_cmnd *sc)
2368 {
2369 	struct Scsi_Host *shost = sc->device->host;
2370 	u32 start_time  = jiffies;
2371 	int ret = FAILED;
2372 
2373 	SNIC_SCSI_DBG(shost,
2374 		      "host reset:sc %p sc_cmd 0x%x req %p tag %d flags 0x%llx\n",
2375 		      sc, sc->cmnd[0], sc->request,
2376 		      snic_cmd_tag(sc), CMD_FLAGS(sc));
2377 
2378 	ret = snic_reset(shost, sc);
2379 
2380 	SNIC_TRC(shost->host_no, snic_cmd_tag(sc), (ulong) sc,
2381 		 jiffies_to_msecs(jiffies - start_time),
2382 		 0, SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
2383 
2384 	return ret;
2385 } /* end of snic_host_reset */
2386 
2387 /*
2388  * snic_cmpl_pending_tmreq : Caller should hold io_lock
2389  */
2390 static void
2391 snic_cmpl_pending_tmreq(struct snic *snic, struct scsi_cmnd *sc)
2392 {
2393 	struct snic_req_info *rqi = NULL;
2394 
2395 	SNIC_SCSI_DBG(snic->shost,
2396 		      "Completing Pending TM Req sc %p, state %s flags 0x%llx\n",
2397 		      sc, snic_io_status_to_str(CMD_STATE(sc)), CMD_FLAGS(sc));
2398 
2399 	rqi = (struct snic_req_info *) CMD_SP(sc);
2400 	if (!rqi)
2401 		return;
2402 
2403 	if (rqi->dr_done)
2404 		complete(rqi->dr_done);
2405 	else if (rqi->abts_done)
2406 		complete(rqi->abts_done);
2407 }
2408 
2409 /*
2410  * snic_scsi_cleanup: Walks through tag map and releases the reqs
2411  */
2412 static void
2413 snic_scsi_cleanup(struct snic *snic, int ex_tag)
2414 {
2415 	struct snic_req_info *rqi = NULL;
2416 	struct scsi_cmnd *sc = NULL;
2417 	spinlock_t *io_lock = NULL;
2418 	unsigned long flags;
2419 	int tag;
2420 	u64 st_time = 0;
2421 
2422 	SNIC_SCSI_DBG(snic->shost, "sc_clean: scsi cleanup.\n");
2423 
2424 	for (tag = 0; tag < snic->max_tag_id; tag++) {
2425 		/* Skip ex_tag */
2426 		if (tag == ex_tag)
2427 			continue;
2428 
2429 		io_lock = snic_io_lock_tag(snic, tag);
2430 		spin_lock_irqsave(io_lock, flags);
2431 		sc = scsi_host_find_tag(snic->shost, tag);
2432 		if (!sc) {
2433 			spin_unlock_irqrestore(io_lock, flags);
2434 
2435 			continue;
2436 		}
2437 
2438 		if (unlikely(snic_tmreq_pending(sc))) {
2439 			/*
2440 			 * When FW Completes reset w/o sending completions
2441 			 * for outstanding ios.
2442 			 */
2443 			snic_cmpl_pending_tmreq(snic, sc);
2444 			spin_unlock_irqrestore(io_lock, flags);
2445 
2446 			continue;
2447 		}
2448 
2449 		rqi = (struct snic_req_info *) CMD_SP(sc);
2450 		if (!rqi) {
2451 			spin_unlock_irqrestore(io_lock, flags);
2452 
2453 			goto cleanup;
2454 		}
2455 
2456 		SNIC_SCSI_DBG(snic->shost,
2457 			      "sc_clean: sc %p, rqi %p, tag %d flags 0x%llx\n",
2458 			      sc, rqi, tag, CMD_FLAGS(sc));
2459 
2460 		CMD_SP(sc) = NULL;
2461 		CMD_FLAGS(sc) |= SNIC_SCSI_CLEANUP;
2462 		spin_unlock_irqrestore(io_lock, flags);
2463 		st_time = rqi->start_time;
2464 
2465 		SNIC_HOST_INFO(snic->shost,
2466 			       "sc_clean: Releasing rqi %p : flags 0x%llx\n",
2467 			       rqi, CMD_FLAGS(sc));
2468 
2469 		snic_release_req_buf(snic, rqi, sc);
2470 
2471 cleanup:
2472 		sc->result = DID_TRANSPORT_DISRUPTED << 16;
2473 		SNIC_HOST_INFO(snic->shost,
2474 			       "sc_clean: DID_TRANSPORT_DISRUPTED for sc %p, Tag %d flags 0x%llx rqi %p duration %u msecs\n",
2475 			       sc, sc->request->tag, CMD_FLAGS(sc), rqi,
2476 			       jiffies_to_msecs(jiffies - st_time));
2477 
2478 		/* Update IO stats */
2479 		snic_stats_update_io_cmpl(&snic->s_stats);
2480 
2481 		if (sc->scsi_done) {
2482 			SNIC_TRC(snic->shost->host_no, tag, (ulong) sc,
2483 				 jiffies_to_msecs(jiffies - st_time), 0,
2484 				 SNIC_TRC_CMD(sc),
2485 				 SNIC_TRC_CMD_STATE_FLAGS(sc));
2486 
2487 			sc->scsi_done(sc);
2488 		}
2489 	}
2490 } /* end of snic_scsi_cleanup */
2491 
2492 void
2493 snic_shutdown_scsi_cleanup(struct snic *snic)
2494 {
2495 	SNIC_HOST_INFO(snic->shost, "Shutdown time SCSI Cleanup.\n");
2496 
2497 	snic_scsi_cleanup(snic, SCSI_NO_TAG);
2498 } /* end of snic_shutdown_scsi_cleanup */
2499 
2500 /*
2501  * snic_internal_abort_io
2502  * called by : snic_tgt_scsi_abort_io
2503  */
2504 static int
2505 snic_internal_abort_io(struct snic *snic, struct scsi_cmnd *sc, int tmf)
2506 {
2507 	struct snic_req_info *rqi = NULL;
2508 	spinlock_t *io_lock = NULL;
2509 	unsigned long flags;
2510 	u32 sv_state = 0;
2511 	int ret = 0;
2512 
2513 	io_lock = snic_io_lock_hash(snic, sc);
2514 	spin_lock_irqsave(io_lock, flags);
2515 	rqi = (struct snic_req_info *) CMD_SP(sc);
2516 	if (!rqi)
2517 		goto skip_internal_abts;
2518 
2519 	if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
2520 		goto skip_internal_abts;
2521 
2522 	if ((CMD_FLAGS(sc) & SNIC_DEVICE_RESET) &&
2523 		(!(CMD_FLAGS(sc) & SNIC_DEV_RST_ISSUED))) {
2524 
2525 		SNIC_SCSI_DBG(snic->shost,
2526 			      "internal_abts: dev rst not pending sc 0x%p\n",
2527 			      sc);
2528 
2529 		goto skip_internal_abts;
2530 	}
2531 
2532 
2533 	if (!(CMD_FLAGS(sc) & SNIC_IO_ISSUED)) {
2534 		SNIC_SCSI_DBG(snic->shost,
2535 			"internal_abts: IO not yet issued sc 0x%p tag 0x%x flags 0x%llx state %d\n",
2536 			sc, snic_cmd_tag(sc), CMD_FLAGS(sc), CMD_STATE(sc));
2537 
2538 		goto skip_internal_abts;
2539 	}
2540 
2541 	sv_state = CMD_STATE(sc);
2542 	CMD_STATE(sc) = SNIC_IOREQ_ABTS_PENDING;
2543 	CMD_ABTS_STATUS(sc) = SNIC_INVALID_CODE;
2544 	CMD_FLAGS(sc) |= SNIC_IO_INTERNAL_TERM_PENDING;
2545 
2546 	if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET) {
2547 		/* stats */
2548 		rqi->tm_tag = SNIC_TAG_DEV_RST;
2549 		SNIC_SCSI_DBG(snic->shost, "internal_abts:dev rst sc %p\n", sc);
2550 	}
2551 
2552 	SNIC_SCSI_DBG(snic->shost, "internal_abts: Issuing abts tag %x\n",
2553 		      snic_cmd_tag(sc));
2554 	SNIC_BUG_ON(rqi->abts_done);
2555 	spin_unlock_irqrestore(io_lock, flags);
2556 
2557 	ret = snic_queue_abort_req(snic, rqi, sc, tmf);
2558 	if (ret) {
2559 		SNIC_HOST_ERR(snic->shost,
2560 			      "internal_abts: Tag = %x , Failed w/ err = %d\n",
2561 			      snic_cmd_tag(sc), ret);
2562 
2563 		spin_lock_irqsave(io_lock, flags);
2564 
2565 		if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
2566 			CMD_STATE(sc) = sv_state;
2567 
2568 		goto skip_internal_abts;
2569 	}
2570 
2571 	spin_lock_irqsave(io_lock, flags);
2572 	if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET)
2573 		CMD_FLAGS(sc) |= SNIC_DEV_RST_TERM_ISSUED;
2574 	else
2575 		CMD_FLAGS(sc) |= SNIC_IO_INTERNAL_TERM_ISSUED;
2576 
2577 	ret = SUCCESS;
2578 
2579 skip_internal_abts:
2580 	SNIC_BUG_ON(!spin_is_locked(io_lock));
2581 	spin_unlock_irqrestore(io_lock, flags);
2582 
2583 	return ret;
2584 } /* end of snic_internal_abort_io */
2585 
2586 /*
2587  * snic_tgt_scsi_abort_io : called by snic_tgt_del
2588  */
2589 int
2590 snic_tgt_scsi_abort_io(struct snic_tgt *tgt)
2591 {
2592 	struct snic *snic = NULL;
2593 	struct scsi_cmnd *sc = NULL;
2594 	struct snic_tgt *sc_tgt = NULL;
2595 	spinlock_t *io_lock = NULL;
2596 	unsigned long flags;
2597 	int ret = 0, tag, abt_cnt = 0, tmf = 0;
2598 
2599 	if (!tgt)
2600 		return -1;
2601 
2602 	snic = shost_priv(snic_tgt_to_shost(tgt));
2603 	SNIC_SCSI_DBG(snic->shost, "tgt_abt_io: Cleaning Pending IOs.\n");
2604 
2605 	if (tgt->tdata.typ == SNIC_TGT_DAS)
2606 		tmf = SNIC_ITMF_ABTS_TASK;
2607 	else
2608 		tmf = SNIC_ITMF_ABTS_TASK_TERM;
2609 
2610 	for (tag = 0; tag < snic->max_tag_id; tag++) {
2611 		io_lock = snic_io_lock_tag(snic, tag);
2612 
2613 		spin_lock_irqsave(io_lock, flags);
2614 		sc = scsi_host_find_tag(snic->shost, tag);
2615 		if (!sc) {
2616 			spin_unlock_irqrestore(io_lock, flags);
2617 
2618 			continue;
2619 		}
2620 
2621 		sc_tgt = starget_to_tgt(scsi_target(sc->device));
2622 		if (sc_tgt != tgt) {
2623 			spin_unlock_irqrestore(io_lock, flags);
2624 
2625 			continue;
2626 		}
2627 		spin_unlock_irqrestore(io_lock, flags);
2628 
2629 		ret = snic_internal_abort_io(snic, sc, tmf);
2630 		if (ret < 0) {
2631 			SNIC_HOST_ERR(snic->shost,
2632 				      "tgt_abt_io: Tag %x, Failed w err = %d\n",
2633 				      tag, ret);
2634 
2635 			continue;
2636 		}
2637 
2638 		if (ret == SUCCESS)
2639 			abt_cnt++;
2640 	}
2641 
2642 	SNIC_SCSI_DBG(snic->shost, "tgt_abt_io: abt_cnt = %d\n", abt_cnt);
2643 
2644 	return 0;
2645 } /* end of snic_tgt_scsi_abort_io */
2646