xref: /openbmc/linux/drivers/scsi/mpi3mr/mpi3mr_os.c (revision 1aa529d4)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Driver for Broadcom MPI3 Storage Controllers
4  *
5  * Copyright (C) 2017-2022 Broadcom Inc.
6  *  (mailto: mpi3mr-linuxdrv.pdl@broadcom.com)
7  *
8  */
9 
10 #include "mpi3mr.h"
11 
12 /* global driver scop variables */
13 LIST_HEAD(mrioc_list);
14 DEFINE_SPINLOCK(mrioc_list_lock);
15 static int mrioc_ids;
16 static int warn_non_secure_ctlr;
17 atomic64_t event_counter;
18 
19 MODULE_AUTHOR(MPI3MR_DRIVER_AUTHOR);
20 MODULE_DESCRIPTION(MPI3MR_DRIVER_DESC);
21 MODULE_LICENSE(MPI3MR_DRIVER_LICENSE);
22 MODULE_VERSION(MPI3MR_DRIVER_VERSION);
23 
24 /* Module parameters*/
25 int prot_mask = -1;
26 module_param(prot_mask, int, 0);
27 MODULE_PARM_DESC(prot_mask, "Host protection capabilities mask, def=0x07");
28 
29 static int prot_guard_mask = 3;
30 module_param(prot_guard_mask, int, 0);
31 MODULE_PARM_DESC(prot_guard_mask, " Host protection guard mask, def=3");
32 static int logging_level;
33 module_param(logging_level, int, 0);
34 MODULE_PARM_DESC(logging_level,
35 	" bits for enabling additional logging info (default=0)");
36 
37 /* Forward declarations*/
38 static void mpi3mr_send_event_ack(struct mpi3mr_ioc *mrioc, u8 event,
39 	struct mpi3mr_drv_cmd *cmdparam, u32 event_ctx);
40 
41 /**
42  * mpi3mr_host_tag_for_scmd - Get host tag for a scmd
43  * @mrioc: Adapter instance reference
44  * @scmd: SCSI command reference
45  *
46  * Calculate the host tag based on block tag for a given scmd.
47  *
48  * Return: Valid host tag or MPI3MR_HOSTTAG_INVALID.
49  */
50 static u16 mpi3mr_host_tag_for_scmd(struct mpi3mr_ioc *mrioc,
51 	struct scsi_cmnd *scmd)
52 {
53 	struct scmd_priv *priv = NULL;
54 	u32 unique_tag;
55 	u16 host_tag, hw_queue;
56 
57 	unique_tag = blk_mq_unique_tag(scsi_cmd_to_rq(scmd));
58 
59 	hw_queue = blk_mq_unique_tag_to_hwq(unique_tag);
60 	if (hw_queue >= mrioc->num_op_reply_q)
61 		return MPI3MR_HOSTTAG_INVALID;
62 	host_tag = blk_mq_unique_tag_to_tag(unique_tag);
63 
64 	if (WARN_ON(host_tag >= mrioc->max_host_ios))
65 		return MPI3MR_HOSTTAG_INVALID;
66 
67 	priv = scsi_cmd_priv(scmd);
68 	/*host_tag 0 is invalid hence incrementing by 1*/
69 	priv->host_tag = host_tag + 1;
70 	priv->scmd = scmd;
71 	priv->in_lld_scope = 1;
72 	priv->req_q_idx = hw_queue;
73 	priv->meta_chain_idx = -1;
74 	priv->chain_idx = -1;
75 	priv->meta_sg_valid = 0;
76 	return priv->host_tag;
77 }
78 
79 /**
80  * mpi3mr_scmd_from_host_tag - Get SCSI command from host tag
81  * @mrioc: Adapter instance reference
82  * @host_tag: Host tag
83  * @qidx: Operational queue index
84  *
85  * Identify the block tag from the host tag and queue index and
86  * retrieve associated scsi command using scsi_host_find_tag().
87  *
88  * Return: SCSI command reference or NULL.
89  */
90 static struct scsi_cmnd *mpi3mr_scmd_from_host_tag(
91 	struct mpi3mr_ioc *mrioc, u16 host_tag, u16 qidx)
92 {
93 	struct scsi_cmnd *scmd = NULL;
94 	struct scmd_priv *priv = NULL;
95 	u32 unique_tag = host_tag - 1;
96 
97 	if (WARN_ON(host_tag > mrioc->max_host_ios))
98 		goto out;
99 
100 	unique_tag |= (qidx << BLK_MQ_UNIQUE_TAG_BITS);
101 
102 	scmd = scsi_host_find_tag(mrioc->shost, unique_tag);
103 	if (scmd) {
104 		priv = scsi_cmd_priv(scmd);
105 		if (!priv->in_lld_scope)
106 			scmd = NULL;
107 	}
108 out:
109 	return scmd;
110 }
111 
112 /**
113  * mpi3mr_clear_scmd_priv - Cleanup SCSI command private date
114  * @mrioc: Adapter instance reference
115  * @scmd: SCSI command reference
116  *
117  * Invalidate the SCSI command private data to mark the command
118  * is not in LLD scope anymore.
119  *
120  * Return: Nothing.
121  */
122 static void mpi3mr_clear_scmd_priv(struct mpi3mr_ioc *mrioc,
123 	struct scsi_cmnd *scmd)
124 {
125 	struct scmd_priv *priv = NULL;
126 
127 	priv = scsi_cmd_priv(scmd);
128 
129 	if (WARN_ON(priv->in_lld_scope == 0))
130 		return;
131 	priv->host_tag = MPI3MR_HOSTTAG_INVALID;
132 	priv->req_q_idx = 0xFFFF;
133 	priv->scmd = NULL;
134 	priv->in_lld_scope = 0;
135 	priv->meta_sg_valid = 0;
136 	if (priv->chain_idx >= 0) {
137 		clear_bit(priv->chain_idx, mrioc->chain_bitmap);
138 		priv->chain_idx = -1;
139 	}
140 	if (priv->meta_chain_idx >= 0) {
141 		clear_bit(priv->meta_chain_idx, mrioc->chain_bitmap);
142 		priv->meta_chain_idx = -1;
143 	}
144 }
145 
146 static void mpi3mr_dev_rmhs_send_tm(struct mpi3mr_ioc *mrioc, u16 handle,
147 	struct mpi3mr_drv_cmd *cmdparam, u8 iou_rc);
148 static void mpi3mr_fwevt_worker(struct work_struct *work);
149 
150 /**
151  * mpi3mr_fwevt_free - firmware event memory dealloctor
152  * @r: k reference pointer of the firmware event
153  *
154  * Free firmware event memory when no reference.
155  */
156 static void mpi3mr_fwevt_free(struct kref *r)
157 {
158 	kfree(container_of(r, struct mpi3mr_fwevt, ref_count));
159 }
160 
161 /**
162  * mpi3mr_fwevt_get - k reference incrementor
163  * @fwevt: Firmware event reference
164  *
165  * Increment firmware event reference count.
166  */
167 static void mpi3mr_fwevt_get(struct mpi3mr_fwevt *fwevt)
168 {
169 	kref_get(&fwevt->ref_count);
170 }
171 
172 /**
173  * mpi3mr_fwevt_put - k reference decrementor
174  * @fwevt: Firmware event reference
175  *
176  * decrement firmware event reference count.
177  */
178 static void mpi3mr_fwevt_put(struct mpi3mr_fwevt *fwevt)
179 {
180 	kref_put(&fwevt->ref_count, mpi3mr_fwevt_free);
181 }
182 
183 /**
184  * mpi3mr_alloc_fwevt - Allocate firmware event
185  * @len: length of firmware event data to allocate
186  *
187  * Allocate firmware event with required length and initialize
188  * the reference counter.
189  *
190  * Return: firmware event reference.
191  */
192 static struct mpi3mr_fwevt *mpi3mr_alloc_fwevt(int len)
193 {
194 	struct mpi3mr_fwevt *fwevt;
195 
196 	fwevt = kzalloc(sizeof(*fwevt) + len, GFP_ATOMIC);
197 	if (!fwevt)
198 		return NULL;
199 
200 	kref_init(&fwevt->ref_count);
201 	return fwevt;
202 }
203 
204 /**
205  * mpi3mr_fwevt_add_to_list - Add firmware event to the list
206  * @mrioc: Adapter instance reference
207  * @fwevt: Firmware event reference
208  *
209  * Add the given firmware event to the firmware event list.
210  *
211  * Return: Nothing.
212  */
213 static void mpi3mr_fwevt_add_to_list(struct mpi3mr_ioc *mrioc,
214 	struct mpi3mr_fwevt *fwevt)
215 {
216 	unsigned long flags;
217 
218 	if (!mrioc->fwevt_worker_thread)
219 		return;
220 
221 	spin_lock_irqsave(&mrioc->fwevt_lock, flags);
222 	/* get fwevt reference count while adding it to fwevt_list */
223 	mpi3mr_fwevt_get(fwevt);
224 	INIT_LIST_HEAD(&fwevt->list);
225 	list_add_tail(&fwevt->list, &mrioc->fwevt_list);
226 	INIT_WORK(&fwevt->work, mpi3mr_fwevt_worker);
227 	/* get fwevt reference count while enqueueing it to worker queue */
228 	mpi3mr_fwevt_get(fwevt);
229 	queue_work(mrioc->fwevt_worker_thread, &fwevt->work);
230 	spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
231 }
232 
233 /**
234  * mpi3mr_fwevt_del_from_list - Delete firmware event from list
235  * @mrioc: Adapter instance reference
236  * @fwevt: Firmware event reference
237  *
238  * Delete the given firmware event from the firmware event list.
239  *
240  * Return: Nothing.
241  */
242 static void mpi3mr_fwevt_del_from_list(struct mpi3mr_ioc *mrioc,
243 	struct mpi3mr_fwevt *fwevt)
244 {
245 	unsigned long flags;
246 
247 	spin_lock_irqsave(&mrioc->fwevt_lock, flags);
248 	if (!list_empty(&fwevt->list)) {
249 		list_del_init(&fwevt->list);
250 		/*
251 		 * Put fwevt reference count after
252 		 * removing it from fwevt_list
253 		 */
254 		mpi3mr_fwevt_put(fwevt);
255 	}
256 	spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
257 }
258 
259 /**
260  * mpi3mr_dequeue_fwevt - Dequeue firmware event from the list
261  * @mrioc: Adapter instance reference
262  *
263  * Dequeue a firmware event from the firmware event list.
264  *
265  * Return: firmware event.
266  */
267 static struct mpi3mr_fwevt *mpi3mr_dequeue_fwevt(
268 	struct mpi3mr_ioc *mrioc)
269 {
270 	unsigned long flags;
271 	struct mpi3mr_fwevt *fwevt = NULL;
272 
273 	spin_lock_irqsave(&mrioc->fwevt_lock, flags);
274 	if (!list_empty(&mrioc->fwevt_list)) {
275 		fwevt = list_first_entry(&mrioc->fwevt_list,
276 		    struct mpi3mr_fwevt, list);
277 		list_del_init(&fwevt->list);
278 		/*
279 		 * Put fwevt reference count after
280 		 * removing it from fwevt_list
281 		 */
282 		mpi3mr_fwevt_put(fwevt);
283 	}
284 	spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
285 
286 	return fwevt;
287 }
288 
289 /**
290  * mpi3mr_cancel_work - cancel firmware event
291  * @fwevt: fwevt object which needs to be canceled
292  *
293  * Return: Nothing.
294  */
295 static void mpi3mr_cancel_work(struct mpi3mr_fwevt *fwevt)
296 {
297 	/*
298 	 * Wait on the fwevt to complete. If this returns 1, then
299 	 * the event was never executed.
300 	 *
301 	 * If it did execute, we wait for it to finish, and the put will
302 	 * happen from mpi3mr_process_fwevt()
303 	 */
304 	if (cancel_work_sync(&fwevt->work)) {
305 		/*
306 		 * Put fwevt reference count after
307 		 * dequeuing it from worker queue
308 		 */
309 		mpi3mr_fwevt_put(fwevt);
310 		/*
311 		 * Put fwevt reference count to neutralize
312 		 * kref_init increment
313 		 */
314 		mpi3mr_fwevt_put(fwevt);
315 	}
316 }
317 
318 /**
319  * mpi3mr_cleanup_fwevt_list - Cleanup firmware event list
320  * @mrioc: Adapter instance reference
321  *
322  * Flush all pending firmware events from the firmware event
323  * list.
324  *
325  * Return: Nothing.
326  */
327 void mpi3mr_cleanup_fwevt_list(struct mpi3mr_ioc *mrioc)
328 {
329 	struct mpi3mr_fwevt *fwevt = NULL;
330 
331 	if ((list_empty(&mrioc->fwevt_list) && !mrioc->current_event) ||
332 	    !mrioc->fwevt_worker_thread)
333 		return;
334 
335 	while ((fwevt = mpi3mr_dequeue_fwevt(mrioc)))
336 		mpi3mr_cancel_work(fwevt);
337 
338 	if (mrioc->current_event) {
339 		fwevt = mrioc->current_event;
340 		/*
341 		 * Don't call cancel_work_sync() API for the
342 		 * fwevt work if the controller reset is
343 		 * get called as part of processing the
344 		 * same fwevt work (or) when worker thread is
345 		 * waiting for device add/remove APIs to complete.
346 		 * Otherwise we will see deadlock.
347 		 */
348 		if (current_work() == &fwevt->work || fwevt->pending_at_sml) {
349 			fwevt->discard = 1;
350 			return;
351 		}
352 
353 		mpi3mr_cancel_work(fwevt);
354 	}
355 }
356 
357 /**
358  * mpi3mr_invalidate_devhandles -Invalidate device handles
359  * @mrioc: Adapter instance reference
360  *
361  * Invalidate the device handles in the target device structures
362  * . Called post reset prior to reinitializing the controller.
363  *
364  * Return: Nothing.
365  */
366 void mpi3mr_invalidate_devhandles(struct mpi3mr_ioc *mrioc)
367 {
368 	struct mpi3mr_tgt_dev *tgtdev;
369 	struct mpi3mr_stgt_priv_data *tgt_priv;
370 
371 	list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
372 		tgtdev->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
373 		if (tgtdev->starget && tgtdev->starget->hostdata) {
374 			tgt_priv = tgtdev->starget->hostdata;
375 			tgt_priv->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
376 		}
377 	}
378 }
379 
380 /**
381  * mpi3mr_print_scmd - print individual SCSI command
382  * @rq: Block request
383  * @data: Adapter instance reference
384  * @reserved: N/A. Currently not used
385  *
386  * Print the SCSI command details if it is in LLD scope.
387  *
388  * Return: true always.
389  */
390 static bool mpi3mr_print_scmd(struct request *rq,
391 	void *data, bool reserved)
392 {
393 	struct mpi3mr_ioc *mrioc = (struct mpi3mr_ioc *)data;
394 	struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
395 	struct scmd_priv *priv = NULL;
396 
397 	if (scmd) {
398 		priv = scsi_cmd_priv(scmd);
399 		if (!priv->in_lld_scope)
400 			goto out;
401 
402 		ioc_info(mrioc, "%s :Host Tag = %d, qid = %d\n",
403 		    __func__, priv->host_tag, priv->req_q_idx + 1);
404 		scsi_print_command(scmd);
405 	}
406 
407 out:
408 	return(true);
409 }
410 
411 /**
412  * mpi3mr_flush_scmd - Flush individual SCSI command
413  * @rq: Block request
414  * @data: Adapter instance reference
415  * @reserved: N/A. Currently not used
416  *
417  * Return the SCSI command to the upper layers if it is in LLD
418  * scope.
419  *
420  * Return: true always.
421  */
422 
423 static bool mpi3mr_flush_scmd(struct request *rq,
424 	void *data, bool reserved)
425 {
426 	struct mpi3mr_ioc *mrioc = (struct mpi3mr_ioc *)data;
427 	struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
428 	struct scmd_priv *priv = NULL;
429 
430 	if (scmd) {
431 		priv = scsi_cmd_priv(scmd);
432 		if (!priv->in_lld_scope)
433 			goto out;
434 
435 		if (priv->meta_sg_valid)
436 			dma_unmap_sg(&mrioc->pdev->dev, scsi_prot_sglist(scmd),
437 			    scsi_prot_sg_count(scmd), scmd->sc_data_direction);
438 		mpi3mr_clear_scmd_priv(mrioc, scmd);
439 		scsi_dma_unmap(scmd);
440 		scmd->result = DID_RESET << 16;
441 		scsi_print_command(scmd);
442 		scsi_done(scmd);
443 		mrioc->flush_io_count++;
444 	}
445 
446 out:
447 	return(true);
448 }
449 
450 /**
451  * mpi3mr_count_dev_pending - Count commands pending for a lun
452  * @rq: Block request
453  * @data: SCSI device reference
454  * @reserved: Unused
455  *
456  * This is an iterator function called for each SCSI command in
457  * a host and if the command is pending in the LLD for the
458  * specific device(lun) then device specific pending I/O counter
459  * is updated in the device structure.
460  *
461  * Return: true always.
462  */
463 
464 static bool mpi3mr_count_dev_pending(struct request *rq,
465 	void *data, bool reserved)
466 {
467 	struct scsi_device *sdev = (struct scsi_device *)data;
468 	struct mpi3mr_sdev_priv_data *sdev_priv_data = sdev->hostdata;
469 	struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
470 	struct scmd_priv *priv;
471 
472 	if (scmd) {
473 		priv = scsi_cmd_priv(scmd);
474 		if (!priv->in_lld_scope)
475 			goto out;
476 		if (scmd->device == sdev)
477 			sdev_priv_data->pend_count++;
478 	}
479 
480 out:
481 	return true;
482 }
483 
484 /**
485  * mpi3mr_count_tgt_pending - Count commands pending for target
486  * @rq: Block request
487  * @data: SCSI target reference
488  * @reserved: Unused
489  *
490  * This is an iterator function called for each SCSI command in
491  * a host and if the command is pending in the LLD for the
492  * specific target then target specific pending I/O counter is
493  * updated in the target structure.
494  *
495  * Return: true always.
496  */
497 
498 static bool mpi3mr_count_tgt_pending(struct request *rq,
499 	void *data, bool reserved)
500 {
501 	struct scsi_target *starget = (struct scsi_target *)data;
502 	struct mpi3mr_stgt_priv_data *stgt_priv_data = starget->hostdata;
503 	struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
504 	struct scmd_priv *priv;
505 
506 	if (scmd) {
507 		priv = scsi_cmd_priv(scmd);
508 		if (!priv->in_lld_scope)
509 			goto out;
510 		if (scmd->device && (scsi_target(scmd->device) == starget))
511 			stgt_priv_data->pend_count++;
512 	}
513 
514 out:
515 	return true;
516 }
517 
518 /**
519  * mpi3mr_flush_host_io -  Flush host I/Os
520  * @mrioc: Adapter instance reference
521  *
522  * Flush all of the pending I/Os by calling
523  * blk_mq_tagset_busy_iter() for each possible tag. This is
524  * executed post controller reset
525  *
526  * Return: Nothing.
527  */
528 void mpi3mr_flush_host_io(struct mpi3mr_ioc *mrioc)
529 {
530 	struct Scsi_Host *shost = mrioc->shost;
531 
532 	mrioc->flush_io_count = 0;
533 	ioc_info(mrioc, "%s :Flushing Host I/O cmds post reset\n", __func__);
534 	blk_mq_tagset_busy_iter(&shost->tag_set,
535 	    mpi3mr_flush_scmd, (void *)mrioc);
536 	ioc_info(mrioc, "%s :Flushed %d Host I/O cmds\n", __func__,
537 	    mrioc->flush_io_count);
538 }
539 
540 /**
541  * mpi3mr_alloc_tgtdev - target device allocator
542  *
543  * Allocate target device instance and initialize the reference
544  * count
545  *
546  * Return: target device instance.
547  */
548 static struct mpi3mr_tgt_dev *mpi3mr_alloc_tgtdev(void)
549 {
550 	struct mpi3mr_tgt_dev *tgtdev;
551 
552 	tgtdev = kzalloc(sizeof(*tgtdev), GFP_ATOMIC);
553 	if (!tgtdev)
554 		return NULL;
555 	kref_init(&tgtdev->ref_count);
556 	return tgtdev;
557 }
558 
559 /**
560  * mpi3mr_tgtdev_add_to_list -Add tgtdevice to the list
561  * @mrioc: Adapter instance reference
562  * @tgtdev: Target device
563  *
564  * Add the target device to the target device list
565  *
566  * Return: Nothing.
567  */
568 static void mpi3mr_tgtdev_add_to_list(struct mpi3mr_ioc *mrioc,
569 	struct mpi3mr_tgt_dev *tgtdev)
570 {
571 	unsigned long flags;
572 
573 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
574 	mpi3mr_tgtdev_get(tgtdev);
575 	INIT_LIST_HEAD(&tgtdev->list);
576 	list_add_tail(&tgtdev->list, &mrioc->tgtdev_list);
577 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
578 }
579 
580 /**
581  * mpi3mr_tgtdev_del_from_list -Delete tgtdevice from the list
582  * @mrioc: Adapter instance reference
583  * @tgtdev: Target device
584  *
585  * Remove the target device from the target device list
586  *
587  * Return: Nothing.
588  */
589 static void mpi3mr_tgtdev_del_from_list(struct mpi3mr_ioc *mrioc,
590 	struct mpi3mr_tgt_dev *tgtdev)
591 {
592 	unsigned long flags;
593 
594 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
595 	if (!list_empty(&tgtdev->list)) {
596 		list_del_init(&tgtdev->list);
597 		mpi3mr_tgtdev_put(tgtdev);
598 	}
599 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
600 }
601 
602 /**
603  * __mpi3mr_get_tgtdev_by_handle -Get tgtdev from device handle
604  * @mrioc: Adapter instance reference
605  * @handle: Device handle
606  *
607  * Accessor to retrieve target device from the device handle.
608  * Non Lock version
609  *
610  * Return: Target device reference.
611  */
612 static struct mpi3mr_tgt_dev  *__mpi3mr_get_tgtdev_by_handle(
613 	struct mpi3mr_ioc *mrioc, u16 handle)
614 {
615 	struct mpi3mr_tgt_dev *tgtdev;
616 
617 	assert_spin_locked(&mrioc->tgtdev_lock);
618 	list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list)
619 		if (tgtdev->dev_handle == handle)
620 			goto found_tgtdev;
621 	return NULL;
622 
623 found_tgtdev:
624 	mpi3mr_tgtdev_get(tgtdev);
625 	return tgtdev;
626 }
627 
628 /**
629  * mpi3mr_get_tgtdev_by_handle -Get tgtdev from device handle
630  * @mrioc: Adapter instance reference
631  * @handle: Device handle
632  *
633  * Accessor to retrieve target device from the device handle.
634  * Lock version
635  *
636  * Return: Target device reference.
637  */
638 struct mpi3mr_tgt_dev *mpi3mr_get_tgtdev_by_handle(
639 	struct mpi3mr_ioc *mrioc, u16 handle)
640 {
641 	struct mpi3mr_tgt_dev *tgtdev;
642 	unsigned long flags;
643 
644 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
645 	tgtdev = __mpi3mr_get_tgtdev_by_handle(mrioc, handle);
646 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
647 	return tgtdev;
648 }
649 
650 /**
651  * __mpi3mr_get_tgtdev_by_perst_id -Get tgtdev from persist ID
652  * @mrioc: Adapter instance reference
653  * @persist_id: Persistent ID
654  *
655  * Accessor to retrieve target device from the Persistent ID.
656  * Non Lock version
657  *
658  * Return: Target device reference.
659  */
660 static struct mpi3mr_tgt_dev  *__mpi3mr_get_tgtdev_by_perst_id(
661 	struct mpi3mr_ioc *mrioc, u16 persist_id)
662 {
663 	struct mpi3mr_tgt_dev *tgtdev;
664 
665 	assert_spin_locked(&mrioc->tgtdev_lock);
666 	list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list)
667 		if (tgtdev->perst_id == persist_id)
668 			goto found_tgtdev;
669 	return NULL;
670 
671 found_tgtdev:
672 	mpi3mr_tgtdev_get(tgtdev);
673 	return tgtdev;
674 }
675 
676 /**
677  * mpi3mr_get_tgtdev_by_perst_id -Get tgtdev from persistent ID
678  * @mrioc: Adapter instance reference
679  * @persist_id: Persistent ID
680  *
681  * Accessor to retrieve target device from the Persistent ID.
682  * Lock version
683  *
684  * Return: Target device reference.
685  */
686 static struct mpi3mr_tgt_dev *mpi3mr_get_tgtdev_by_perst_id(
687 	struct mpi3mr_ioc *mrioc, u16 persist_id)
688 {
689 	struct mpi3mr_tgt_dev *tgtdev;
690 	unsigned long flags;
691 
692 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
693 	tgtdev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, persist_id);
694 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
695 	return tgtdev;
696 }
697 
698 /**
699  * __mpi3mr_get_tgtdev_from_tgtpriv -Get tgtdev from tgt private
700  * @mrioc: Adapter instance reference
701  * @tgt_priv: Target private data
702  *
703  * Accessor to return target device from the target private
704  * data. Non Lock version
705  *
706  * Return: Target device reference.
707  */
708 static struct mpi3mr_tgt_dev  *__mpi3mr_get_tgtdev_from_tgtpriv(
709 	struct mpi3mr_ioc *mrioc, struct mpi3mr_stgt_priv_data *tgt_priv)
710 {
711 	struct mpi3mr_tgt_dev *tgtdev;
712 
713 	assert_spin_locked(&mrioc->tgtdev_lock);
714 	tgtdev = tgt_priv->tgt_dev;
715 	if (tgtdev)
716 		mpi3mr_tgtdev_get(tgtdev);
717 	return tgtdev;
718 }
719 
720 /**
721  * mpi3mr_print_device_event_notice - print notice related to post processing of
722  *					device event after controller reset.
723  *
724  * @mrioc: Adapter instance reference
725  * @device_add: true for device add event and false for device removal event
726  *
727  * Return: None.
728  */
729 static void mpi3mr_print_device_event_notice(struct mpi3mr_ioc *mrioc,
730 	bool device_add)
731 {
732 	ioc_notice(mrioc, "Device %s was in progress before the reset and\n",
733 	    (device_add ? "addition" : "removal"));
734 	ioc_notice(mrioc, "completed after reset, verify whether the exposed devices\n");
735 	ioc_notice(mrioc, "are matched with attached devices for correctness\n");
736 }
737 
738 /**
739  * mpi3mr_remove_tgtdev_from_host - Remove dev from upper layers
740  * @mrioc: Adapter instance reference
741  * @tgtdev: Target device structure
742  *
743  * Checks whether the device is exposed to upper layers and if it
744  * is then remove the device from upper layers by calling
745  * scsi_remove_target().
746  *
747  * Return: 0 on success, non zero on failure.
748  */
749 static void mpi3mr_remove_tgtdev_from_host(struct mpi3mr_ioc *mrioc,
750 	struct mpi3mr_tgt_dev *tgtdev)
751 {
752 	struct mpi3mr_stgt_priv_data *tgt_priv;
753 
754 	ioc_info(mrioc, "%s :Removing handle(0x%04x), wwid(0x%016llx)\n",
755 	    __func__, tgtdev->dev_handle, (unsigned long long)tgtdev->wwid);
756 	if (tgtdev->starget && tgtdev->starget->hostdata) {
757 		tgt_priv = tgtdev->starget->hostdata;
758 		tgt_priv->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
759 	}
760 
761 	if (tgtdev->starget) {
762 		if (mrioc->current_event)
763 			mrioc->current_event->pending_at_sml = 1;
764 		scsi_remove_target(&tgtdev->starget->dev);
765 		tgtdev->host_exposed = 0;
766 		if (mrioc->current_event) {
767 			mrioc->current_event->pending_at_sml = 0;
768 			if (mrioc->current_event->discard) {
769 				mpi3mr_print_device_event_notice(mrioc, false);
770 				return;
771 			}
772 		}
773 	}
774 	ioc_info(mrioc, "%s :Removed handle(0x%04x), wwid(0x%016llx)\n",
775 	    __func__, tgtdev->dev_handle, (unsigned long long)tgtdev->wwid);
776 }
777 
778 /**
779  * mpi3mr_report_tgtdev_to_host - Expose device to upper layers
780  * @mrioc: Adapter instance reference
781  * @perst_id: Persistent ID of the device
782  *
783  * Checks whether the device can be exposed to upper layers and
784  * if it is not then expose the device to upper layers by
785  * calling scsi_scan_target().
786  *
787  * Return: 0 on success, non zero on failure.
788  */
789 static int mpi3mr_report_tgtdev_to_host(struct mpi3mr_ioc *mrioc,
790 	u16 perst_id)
791 {
792 	int retval = 0;
793 	struct mpi3mr_tgt_dev *tgtdev;
794 
795 	tgtdev = mpi3mr_get_tgtdev_by_perst_id(mrioc, perst_id);
796 	if (!tgtdev) {
797 		retval = -1;
798 		goto out;
799 	}
800 	if (tgtdev->is_hidden) {
801 		retval = -1;
802 		goto out;
803 	}
804 	if (!tgtdev->host_exposed && !mrioc->reset_in_progress) {
805 		tgtdev->host_exposed = 1;
806 		if (mrioc->current_event)
807 			mrioc->current_event->pending_at_sml = 1;
808 		scsi_scan_target(&mrioc->shost->shost_gendev, 0,
809 		    tgtdev->perst_id,
810 		    SCAN_WILD_CARD, SCSI_SCAN_INITIAL);
811 		if (!tgtdev->starget)
812 			tgtdev->host_exposed = 0;
813 		if (mrioc->current_event) {
814 			mrioc->current_event->pending_at_sml = 0;
815 			if (mrioc->current_event->discard) {
816 				mpi3mr_print_device_event_notice(mrioc, true);
817 				goto out;
818 			}
819 		}
820 	}
821 out:
822 	if (tgtdev)
823 		mpi3mr_tgtdev_put(tgtdev);
824 
825 	return retval;
826 }
827 
828 /**
829  * mpi3mr_change_queue_depth- Change QD callback handler
830  * @sdev: SCSI device reference
831  * @q_depth: Queue depth
832  *
833  * Validate and limit QD and call scsi_change_queue_depth.
834  *
835  * Return: return value of scsi_change_queue_depth
836  */
837 static int mpi3mr_change_queue_depth(struct scsi_device *sdev,
838 	int q_depth)
839 {
840 	struct scsi_target *starget = scsi_target(sdev);
841 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
842 	int retval = 0;
843 
844 	if (!sdev->tagged_supported)
845 		q_depth = 1;
846 	if (q_depth > shost->can_queue)
847 		q_depth = shost->can_queue;
848 	else if (!q_depth)
849 		q_depth = MPI3MR_DEFAULT_SDEV_QD;
850 	retval = scsi_change_queue_depth(sdev, q_depth);
851 
852 	return retval;
853 }
854 
855 /**
856  * mpi3mr_update_sdev - Update SCSI device information
857  * @sdev: SCSI device reference
858  * @data: target device reference
859  *
860  * This is an iterator function called for each SCSI device in a
861  * target to update the target specific information into each
862  * SCSI device.
863  *
864  * Return: Nothing.
865  */
866 static void
867 mpi3mr_update_sdev(struct scsi_device *sdev, void *data)
868 {
869 	struct mpi3mr_tgt_dev *tgtdev;
870 
871 	tgtdev = (struct mpi3mr_tgt_dev *)data;
872 	if (!tgtdev)
873 		return;
874 
875 	mpi3mr_change_queue_depth(sdev, tgtdev->q_depth);
876 	switch (tgtdev->dev_type) {
877 	case MPI3_DEVICE_DEVFORM_PCIE:
878 		/*The block layer hw sector size = 512*/
879 		if ((tgtdev->dev_spec.pcie_inf.dev_info &
880 		    MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) ==
881 		    MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) {
882 			blk_queue_max_hw_sectors(sdev->request_queue,
883 			    tgtdev->dev_spec.pcie_inf.mdts / 512);
884 			if (tgtdev->dev_spec.pcie_inf.pgsz == 0)
885 				blk_queue_virt_boundary(sdev->request_queue,
886 				    ((1 << MPI3MR_DEFAULT_PGSZEXP) - 1));
887 			else
888 				blk_queue_virt_boundary(sdev->request_queue,
889 				    ((1 << tgtdev->dev_spec.pcie_inf.pgsz) - 1));
890 		}
891 		break;
892 	default:
893 		break;
894 	}
895 }
896 
897 /**
898  * mpi3mr_rfresh_tgtdevs - Refresh target device exposure
899  * @mrioc: Adapter instance reference
900  *
901  * This is executed post controller reset to identify any
902  * missing devices during reset and remove from the upper layers
903  * or expose any newly detected device to the upper layers.
904  *
905  * Return: Nothing.
906  */
907 
908 void mpi3mr_rfresh_tgtdevs(struct mpi3mr_ioc *mrioc)
909 {
910 	struct mpi3mr_tgt_dev *tgtdev, *tgtdev_next;
911 
912 	list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
913 	    list) {
914 		if ((tgtdev->dev_handle == MPI3MR_INVALID_DEV_HANDLE) &&
915 		    tgtdev->host_exposed) {
916 			mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
917 			mpi3mr_tgtdev_del_from_list(mrioc, tgtdev);
918 			mpi3mr_tgtdev_put(tgtdev);
919 		}
920 	}
921 
922 	tgtdev = NULL;
923 	list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
924 		if ((tgtdev->dev_handle != MPI3MR_INVALID_DEV_HANDLE) &&
925 		    !tgtdev->is_hidden && !tgtdev->host_exposed)
926 			mpi3mr_report_tgtdev_to_host(mrioc, tgtdev->perst_id);
927 	}
928 }
929 
930 /**
931  * mpi3mr_update_tgtdev - DevStatusChange evt bottomhalf
932  * @mrioc: Adapter instance reference
933  * @tgtdev: Target device internal structure
934  * @dev_pg0: New device page0
935  *
936  * Update the information from the device page0 into the driver
937  * cached target device structure.
938  *
939  * Return: Nothing.
940  */
941 static void mpi3mr_update_tgtdev(struct mpi3mr_ioc *mrioc,
942 	struct mpi3mr_tgt_dev *tgtdev, struct mpi3_device_page0 *dev_pg0)
943 {
944 	u16 flags = 0;
945 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
946 	u8 prot_mask = 0;
947 
948 	tgtdev->perst_id = le16_to_cpu(dev_pg0->persistent_id);
949 	tgtdev->dev_handle = le16_to_cpu(dev_pg0->dev_handle);
950 	tgtdev->dev_type = dev_pg0->device_form;
951 	tgtdev->encl_handle = le16_to_cpu(dev_pg0->enclosure_handle);
952 	tgtdev->parent_handle = le16_to_cpu(dev_pg0->parent_dev_handle);
953 	tgtdev->slot = le16_to_cpu(dev_pg0->slot);
954 	tgtdev->q_depth = le16_to_cpu(dev_pg0->queue_depth);
955 	tgtdev->wwid = le64_to_cpu(dev_pg0->wwid);
956 
957 	flags = le16_to_cpu(dev_pg0->flags);
958 	tgtdev->is_hidden = (flags & MPI3_DEVICE0_FLAGS_HIDDEN);
959 
960 	if (tgtdev->starget && tgtdev->starget->hostdata) {
961 		scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
962 		    tgtdev->starget->hostdata;
963 		scsi_tgt_priv_data->perst_id = tgtdev->perst_id;
964 		scsi_tgt_priv_data->dev_handle = tgtdev->dev_handle;
965 		scsi_tgt_priv_data->dev_type = tgtdev->dev_type;
966 	}
967 
968 	switch (dev_pg0->access_status) {
969 	case MPI3_DEVICE0_ASTATUS_NO_ERRORS:
970 	case MPI3_DEVICE0_ASTATUS_PREPARE:
971 	case MPI3_DEVICE0_ASTATUS_NEEDS_INITIALIZATION:
972 	case MPI3_DEVICE0_ASTATUS_DEVICE_MISSING_DELAY:
973 		break;
974 	default:
975 		tgtdev->is_hidden = 1;
976 		break;
977 	}
978 
979 	switch (tgtdev->dev_type) {
980 	case MPI3_DEVICE_DEVFORM_SAS_SATA:
981 	{
982 		struct mpi3_device0_sas_sata_format *sasinf =
983 		    &dev_pg0->device_specific.sas_sata_format;
984 		u16 dev_info = le16_to_cpu(sasinf->device_info);
985 
986 		tgtdev->dev_spec.sas_sata_inf.dev_info = dev_info;
987 		tgtdev->dev_spec.sas_sata_inf.sas_address =
988 		    le64_to_cpu(sasinf->sas_address);
989 		if ((dev_info & MPI3_SAS_DEVICE_INFO_DEVICE_TYPE_MASK) !=
990 		    MPI3_SAS_DEVICE_INFO_DEVICE_TYPE_END_DEVICE)
991 			tgtdev->is_hidden = 1;
992 		else if (!(dev_info & (MPI3_SAS_DEVICE_INFO_STP_SATA_TARGET |
993 		    MPI3_SAS_DEVICE_INFO_SSP_TARGET)))
994 			tgtdev->is_hidden = 1;
995 		break;
996 	}
997 	case MPI3_DEVICE_DEVFORM_PCIE:
998 	{
999 		struct mpi3_device0_pcie_format *pcieinf =
1000 		    &dev_pg0->device_specific.pcie_format;
1001 		u16 dev_info = le16_to_cpu(pcieinf->device_info);
1002 
1003 		tgtdev->dev_spec.pcie_inf.dev_info = dev_info;
1004 		tgtdev->dev_spec.pcie_inf.capb =
1005 		    le32_to_cpu(pcieinf->capabilities);
1006 		tgtdev->dev_spec.pcie_inf.mdts = MPI3MR_DEFAULT_MDTS;
1007 		/* 2^12 = 4096 */
1008 		tgtdev->dev_spec.pcie_inf.pgsz = 12;
1009 		if (dev_pg0->access_status == MPI3_DEVICE0_ASTATUS_NO_ERRORS) {
1010 			tgtdev->dev_spec.pcie_inf.mdts =
1011 			    le32_to_cpu(pcieinf->maximum_data_transfer_size);
1012 			tgtdev->dev_spec.pcie_inf.pgsz = pcieinf->page_size;
1013 			tgtdev->dev_spec.pcie_inf.reset_to =
1014 			    max_t(u8, pcieinf->controller_reset_to,
1015 			     MPI3MR_INTADMCMD_TIMEOUT);
1016 			tgtdev->dev_spec.pcie_inf.abort_to =
1017 			    max_t(u8, pcieinf->nvme_abort_to,
1018 			    MPI3MR_INTADMCMD_TIMEOUT);
1019 		}
1020 		if (tgtdev->dev_spec.pcie_inf.mdts > (1024 * 1024))
1021 			tgtdev->dev_spec.pcie_inf.mdts = (1024 * 1024);
1022 		if (((dev_info & MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) !=
1023 		    MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) &&
1024 		    ((dev_info & MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) !=
1025 		    MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_SCSI_DEVICE))
1026 			tgtdev->is_hidden = 1;
1027 		if (!mrioc->shost)
1028 			break;
1029 		prot_mask = scsi_host_get_prot(mrioc->shost);
1030 		if (prot_mask & SHOST_DIX_TYPE0_PROTECTION) {
1031 			scsi_host_set_prot(mrioc->shost, prot_mask & 0x77);
1032 			ioc_info(mrioc,
1033 			    "%s : Disabling DIX0 prot capability\n", __func__);
1034 			ioc_info(mrioc,
1035 			    "because HBA does not support DIX0 operation on NVME drives\n");
1036 		}
1037 		break;
1038 	}
1039 	case MPI3_DEVICE_DEVFORM_VD:
1040 	{
1041 		struct mpi3_device0_vd_format *vdinf =
1042 		    &dev_pg0->device_specific.vd_format;
1043 
1044 		tgtdev->dev_spec.vol_inf.state = vdinf->vd_state;
1045 		if (vdinf->vd_state == MPI3_DEVICE0_VD_STATE_OFFLINE)
1046 			tgtdev->is_hidden = 1;
1047 		break;
1048 	}
1049 	default:
1050 		break;
1051 	}
1052 }
1053 
1054 /**
1055  * mpi3mr_devstatuschg_evt_bh - DevStatusChange evt bottomhalf
1056  * @mrioc: Adapter instance reference
1057  * @fwevt: Firmware event information.
1058  *
1059  * Process Device status Change event and based on device's new
1060  * information, either expose the device to the upper layers, or
1061  * remove the device from upper layers.
1062  *
1063  * Return: Nothing.
1064  */
1065 static void mpi3mr_devstatuschg_evt_bh(struct mpi3mr_ioc *mrioc,
1066 	struct mpi3mr_fwevt *fwevt)
1067 {
1068 	u16 dev_handle = 0;
1069 	u8 uhide = 0, delete = 0, cleanup = 0;
1070 	struct mpi3mr_tgt_dev *tgtdev = NULL;
1071 	struct mpi3_event_data_device_status_change *evtdata =
1072 	    (struct mpi3_event_data_device_status_change *)fwevt->event_data;
1073 
1074 	dev_handle = le16_to_cpu(evtdata->dev_handle);
1075 	ioc_info(mrioc,
1076 	    "%s :device status change: handle(0x%04x): reason code(0x%x)\n",
1077 	    __func__, dev_handle, evtdata->reason_code);
1078 	switch (evtdata->reason_code) {
1079 	case MPI3_EVENT_DEV_STAT_RC_HIDDEN:
1080 		delete = 1;
1081 		break;
1082 	case MPI3_EVENT_DEV_STAT_RC_NOT_HIDDEN:
1083 		uhide = 1;
1084 		break;
1085 	case MPI3_EVENT_DEV_STAT_RC_VD_NOT_RESPONDING:
1086 		delete = 1;
1087 		cleanup = 1;
1088 		break;
1089 	default:
1090 		ioc_info(mrioc, "%s :Unhandled reason code(0x%x)\n", __func__,
1091 		    evtdata->reason_code);
1092 		break;
1093 	}
1094 
1095 	tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
1096 	if (!tgtdev)
1097 		goto out;
1098 	if (uhide) {
1099 		tgtdev->is_hidden = 0;
1100 		if (!tgtdev->host_exposed)
1101 			mpi3mr_report_tgtdev_to_host(mrioc, tgtdev->perst_id);
1102 	}
1103 	if (tgtdev->starget && tgtdev->starget->hostdata) {
1104 		if (delete)
1105 			mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1106 	}
1107 	if (cleanup) {
1108 		mpi3mr_tgtdev_del_from_list(mrioc, tgtdev);
1109 		mpi3mr_tgtdev_put(tgtdev);
1110 	}
1111 
1112 out:
1113 	if (tgtdev)
1114 		mpi3mr_tgtdev_put(tgtdev);
1115 }
1116 
1117 /**
1118  * mpi3mr_devinfochg_evt_bh - DeviceInfoChange evt bottomhalf
1119  * @mrioc: Adapter instance reference
1120  * @dev_pg0: New device page0
1121  *
1122  * Process Device Info Change event and based on device's new
1123  * information, either expose the device to the upper layers, or
1124  * remove the device from upper layers or update the details of
1125  * the device.
1126  *
1127  * Return: Nothing.
1128  */
1129 static void mpi3mr_devinfochg_evt_bh(struct mpi3mr_ioc *mrioc,
1130 	struct mpi3_device_page0 *dev_pg0)
1131 {
1132 	struct mpi3mr_tgt_dev *tgtdev = NULL;
1133 	u16 dev_handle = 0, perst_id = 0;
1134 
1135 	perst_id = le16_to_cpu(dev_pg0->persistent_id);
1136 	dev_handle = le16_to_cpu(dev_pg0->dev_handle);
1137 	ioc_info(mrioc,
1138 	    "%s :Device info change: handle(0x%04x): persist_id(0x%x)\n",
1139 	    __func__, dev_handle, perst_id);
1140 	tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
1141 	if (!tgtdev)
1142 		goto out;
1143 	mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0);
1144 	if (!tgtdev->is_hidden && !tgtdev->host_exposed)
1145 		mpi3mr_report_tgtdev_to_host(mrioc, perst_id);
1146 	if (tgtdev->is_hidden && tgtdev->host_exposed)
1147 		mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1148 	if (!tgtdev->is_hidden && tgtdev->host_exposed && tgtdev->starget)
1149 		starget_for_each_device(tgtdev->starget, (void *)tgtdev,
1150 		    mpi3mr_update_sdev);
1151 out:
1152 	if (tgtdev)
1153 		mpi3mr_tgtdev_put(tgtdev);
1154 }
1155 
1156 /**
1157  * mpi3mr_sastopochg_evt_debug - SASTopoChange details
1158  * @mrioc: Adapter instance reference
1159  * @event_data: SAS topology change list event data
1160  *
1161  * Prints information about the SAS topology change event.
1162  *
1163  * Return: Nothing.
1164  */
1165 static void
1166 mpi3mr_sastopochg_evt_debug(struct mpi3mr_ioc *mrioc,
1167 	struct mpi3_event_data_sas_topology_change_list *event_data)
1168 {
1169 	int i;
1170 	u16 handle;
1171 	u8 reason_code, phy_number;
1172 	char *status_str = NULL;
1173 	u8 link_rate, prev_link_rate;
1174 
1175 	switch (event_data->exp_status) {
1176 	case MPI3_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
1177 		status_str = "remove";
1178 		break;
1179 	case MPI3_EVENT_SAS_TOPO_ES_RESPONDING:
1180 		status_str =  "responding";
1181 		break;
1182 	case MPI3_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
1183 		status_str = "remove delay";
1184 		break;
1185 	case MPI3_EVENT_SAS_TOPO_ES_NO_EXPANDER:
1186 		status_str = "direct attached";
1187 		break;
1188 	default:
1189 		status_str = "unknown status";
1190 		break;
1191 	}
1192 	ioc_info(mrioc, "%s :sas topology change: (%s)\n",
1193 	    __func__, status_str);
1194 	ioc_info(mrioc,
1195 	    "%s :\texpander_handle(0x%04x), enclosure_handle(0x%04x) start_phy(%02d), num_entries(%d)\n",
1196 	    __func__, le16_to_cpu(event_data->expander_dev_handle),
1197 	    le16_to_cpu(event_data->enclosure_handle),
1198 	    event_data->start_phy_num, event_data->num_entries);
1199 	for (i = 0; i < event_data->num_entries; i++) {
1200 		handle = le16_to_cpu(event_data->phy_entry[i].attached_dev_handle);
1201 		if (!handle)
1202 			continue;
1203 		phy_number = event_data->start_phy_num + i;
1204 		reason_code = event_data->phy_entry[i].status &
1205 		    MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
1206 		switch (reason_code) {
1207 		case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
1208 			status_str = "target remove";
1209 			break;
1210 		case MPI3_EVENT_SAS_TOPO_PHY_RC_DELAY_NOT_RESPONDING:
1211 			status_str = "delay target remove";
1212 			break;
1213 		case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
1214 			status_str = "link status change";
1215 			break;
1216 		case MPI3_EVENT_SAS_TOPO_PHY_RC_NO_CHANGE:
1217 			status_str = "link status no change";
1218 			break;
1219 		case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
1220 			status_str = "target responding";
1221 			break;
1222 		default:
1223 			status_str = "unknown";
1224 			break;
1225 		}
1226 		link_rate = event_data->phy_entry[i].link_rate >> 4;
1227 		prev_link_rate = event_data->phy_entry[i].link_rate & 0xF;
1228 		ioc_info(mrioc,
1229 		    "%s :\tphy(%02d), attached_handle(0x%04x): %s: link rate: new(0x%02x), old(0x%02x)\n",
1230 		    __func__, phy_number, handle, status_str, link_rate,
1231 		    prev_link_rate);
1232 	}
1233 }
1234 
1235 /**
1236  * mpi3mr_sastopochg_evt_bh - SASTopologyChange evt bottomhalf
1237  * @mrioc: Adapter instance reference
1238  * @fwevt: Firmware event reference
1239  *
1240  * Prints information about the SAS topology change event and
1241  * for "not responding" event code, removes the device from the
1242  * upper layers.
1243  *
1244  * Return: Nothing.
1245  */
1246 static void mpi3mr_sastopochg_evt_bh(struct mpi3mr_ioc *mrioc,
1247 	struct mpi3mr_fwevt *fwevt)
1248 {
1249 	struct mpi3_event_data_sas_topology_change_list *event_data =
1250 	    (struct mpi3_event_data_sas_topology_change_list *)fwevt->event_data;
1251 	int i;
1252 	u16 handle;
1253 	u8 reason_code;
1254 	struct mpi3mr_tgt_dev *tgtdev = NULL;
1255 
1256 	mpi3mr_sastopochg_evt_debug(mrioc, event_data);
1257 
1258 	for (i = 0; i < event_data->num_entries; i++) {
1259 		if (fwevt->discard)
1260 			return;
1261 		handle = le16_to_cpu(event_data->phy_entry[i].attached_dev_handle);
1262 		if (!handle)
1263 			continue;
1264 		tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
1265 		if (!tgtdev)
1266 			continue;
1267 
1268 		reason_code = event_data->phy_entry[i].status &
1269 		    MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
1270 
1271 		switch (reason_code) {
1272 		case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
1273 			if (tgtdev->host_exposed)
1274 				mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1275 			mpi3mr_tgtdev_del_from_list(mrioc, tgtdev);
1276 			mpi3mr_tgtdev_put(tgtdev);
1277 			break;
1278 		default:
1279 			break;
1280 		}
1281 		if (tgtdev)
1282 			mpi3mr_tgtdev_put(tgtdev);
1283 	}
1284 }
1285 
1286 /**
1287  * mpi3mr_pcietopochg_evt_debug - PCIeTopoChange details
1288  * @mrioc: Adapter instance reference
1289  * @event_data: PCIe topology change list event data
1290  *
1291  * Prints information about the PCIe topology change event.
1292  *
1293  * Return: Nothing.
1294  */
1295 static void
1296 mpi3mr_pcietopochg_evt_debug(struct mpi3mr_ioc *mrioc,
1297 	struct mpi3_event_data_pcie_topology_change_list *event_data)
1298 {
1299 	int i;
1300 	u16 handle;
1301 	u16 reason_code;
1302 	u8 port_number;
1303 	char *status_str = NULL;
1304 	u8 link_rate, prev_link_rate;
1305 
1306 	switch (event_data->switch_status) {
1307 	case MPI3_EVENT_PCIE_TOPO_SS_NOT_RESPONDING:
1308 		status_str = "remove";
1309 		break;
1310 	case MPI3_EVENT_PCIE_TOPO_SS_RESPONDING:
1311 		status_str =  "responding";
1312 		break;
1313 	case MPI3_EVENT_PCIE_TOPO_SS_DELAY_NOT_RESPONDING:
1314 		status_str = "remove delay";
1315 		break;
1316 	case MPI3_EVENT_PCIE_TOPO_SS_NO_PCIE_SWITCH:
1317 		status_str = "direct attached";
1318 		break;
1319 	default:
1320 		status_str = "unknown status";
1321 		break;
1322 	}
1323 	ioc_info(mrioc, "%s :pcie topology change: (%s)\n",
1324 	    __func__, status_str);
1325 	ioc_info(mrioc,
1326 	    "%s :\tswitch_handle(0x%04x), enclosure_handle(0x%04x) start_port(%02d), num_entries(%d)\n",
1327 	    __func__, le16_to_cpu(event_data->switch_dev_handle),
1328 	    le16_to_cpu(event_data->enclosure_handle),
1329 	    event_data->start_port_num, event_data->num_entries);
1330 	for (i = 0; i < event_data->num_entries; i++) {
1331 		handle =
1332 		    le16_to_cpu(event_data->port_entry[i].attached_dev_handle);
1333 		if (!handle)
1334 			continue;
1335 		port_number = event_data->start_port_num + i;
1336 		reason_code = event_data->port_entry[i].port_status;
1337 		switch (reason_code) {
1338 		case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
1339 			status_str = "target remove";
1340 			break;
1341 		case MPI3_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
1342 			status_str = "delay target remove";
1343 			break;
1344 		case MPI3_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
1345 			status_str = "link status change";
1346 			break;
1347 		case MPI3_EVENT_PCIE_TOPO_PS_NO_CHANGE:
1348 			status_str = "link status no change";
1349 			break;
1350 		case MPI3_EVENT_PCIE_TOPO_PS_RESPONDING:
1351 			status_str = "target responding";
1352 			break;
1353 		default:
1354 			status_str = "unknown";
1355 			break;
1356 		}
1357 		link_rate = event_data->port_entry[i].current_port_info &
1358 		    MPI3_EVENT_PCIE_TOPO_PI_RATE_MASK;
1359 		prev_link_rate = event_data->port_entry[i].previous_port_info &
1360 		    MPI3_EVENT_PCIE_TOPO_PI_RATE_MASK;
1361 		ioc_info(mrioc,
1362 		    "%s :\tport(%02d), attached_handle(0x%04x): %s: link rate: new(0x%02x), old(0x%02x)\n",
1363 		    __func__, port_number, handle, status_str, link_rate,
1364 		    prev_link_rate);
1365 	}
1366 }
1367 
1368 /**
1369  * mpi3mr_pcietopochg_evt_bh - PCIeTopologyChange evt bottomhalf
1370  * @mrioc: Adapter instance reference
1371  * @fwevt: Firmware event reference
1372  *
1373  * Prints information about the PCIe topology change event and
1374  * for "not responding" event code, removes the device from the
1375  * upper layers.
1376  *
1377  * Return: Nothing.
1378  */
1379 static void mpi3mr_pcietopochg_evt_bh(struct mpi3mr_ioc *mrioc,
1380 	struct mpi3mr_fwevt *fwevt)
1381 {
1382 	struct mpi3_event_data_pcie_topology_change_list *event_data =
1383 	    (struct mpi3_event_data_pcie_topology_change_list *)fwevt->event_data;
1384 	int i;
1385 	u16 handle;
1386 	u8 reason_code;
1387 	struct mpi3mr_tgt_dev *tgtdev = NULL;
1388 
1389 	mpi3mr_pcietopochg_evt_debug(mrioc, event_data);
1390 
1391 	for (i = 0; i < event_data->num_entries; i++) {
1392 		if (fwevt->discard)
1393 			return;
1394 		handle =
1395 		    le16_to_cpu(event_data->port_entry[i].attached_dev_handle);
1396 		if (!handle)
1397 			continue;
1398 		tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
1399 		if (!tgtdev)
1400 			continue;
1401 
1402 		reason_code = event_data->port_entry[i].port_status;
1403 
1404 		switch (reason_code) {
1405 		case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
1406 			if (tgtdev->host_exposed)
1407 				mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1408 			mpi3mr_tgtdev_del_from_list(mrioc, tgtdev);
1409 			mpi3mr_tgtdev_put(tgtdev);
1410 			break;
1411 		default:
1412 			break;
1413 		}
1414 		if (tgtdev)
1415 			mpi3mr_tgtdev_put(tgtdev);
1416 	}
1417 }
1418 
1419 /**
1420  * mpi3mr_logdata_evt_bh -  Log data event bottomhalf
1421  * @mrioc: Adapter instance reference
1422  * @fwevt: Firmware event reference
1423  *
1424  * Extracts the event data and calls application interfacing
1425  * function to process the event further.
1426  *
1427  * Return: Nothing.
1428  */
1429 static void mpi3mr_logdata_evt_bh(struct mpi3mr_ioc *mrioc,
1430 	struct mpi3mr_fwevt *fwevt)
1431 {
1432 	mpi3mr_app_save_logdata(mrioc, fwevt->event_data,
1433 	    fwevt->event_data_size);
1434 }
1435 
1436 /**
1437  * mpi3mr_fwevt_bh - Firmware event bottomhalf handler
1438  * @mrioc: Adapter instance reference
1439  * @fwevt: Firmware event reference
1440  *
1441  * Identifies the firmware event and calls corresponding bottomg
1442  * half handler and sends event acknowledgment if required.
1443  *
1444  * Return: Nothing.
1445  */
1446 static void mpi3mr_fwevt_bh(struct mpi3mr_ioc *mrioc,
1447 	struct mpi3mr_fwevt *fwevt)
1448 {
1449 	mpi3mr_fwevt_del_from_list(mrioc, fwevt);
1450 	mrioc->current_event = fwevt;
1451 
1452 	if (mrioc->stop_drv_processing)
1453 		goto out;
1454 
1455 	if (!fwevt->process_evt)
1456 		goto evt_ack;
1457 
1458 	switch (fwevt->event_id) {
1459 	case MPI3_EVENT_DEVICE_ADDED:
1460 	{
1461 		struct mpi3_device_page0 *dev_pg0 =
1462 		    (struct mpi3_device_page0 *)fwevt->event_data;
1463 		mpi3mr_report_tgtdev_to_host(mrioc,
1464 		    le16_to_cpu(dev_pg0->persistent_id));
1465 		break;
1466 	}
1467 	case MPI3_EVENT_DEVICE_INFO_CHANGED:
1468 	{
1469 		mpi3mr_devinfochg_evt_bh(mrioc,
1470 		    (struct mpi3_device_page0 *)fwevt->event_data);
1471 		break;
1472 	}
1473 	case MPI3_EVENT_DEVICE_STATUS_CHANGE:
1474 	{
1475 		mpi3mr_devstatuschg_evt_bh(mrioc, fwevt);
1476 		break;
1477 	}
1478 	case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
1479 	{
1480 		mpi3mr_sastopochg_evt_bh(mrioc, fwevt);
1481 		break;
1482 	}
1483 	case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
1484 	{
1485 		mpi3mr_pcietopochg_evt_bh(mrioc, fwevt);
1486 		break;
1487 	}
1488 	case MPI3_EVENT_LOG_DATA:
1489 	{
1490 		mpi3mr_logdata_evt_bh(mrioc, fwevt);
1491 		break;
1492 	}
1493 	default:
1494 		break;
1495 	}
1496 
1497 evt_ack:
1498 	if (fwevt->send_ack)
1499 		mpi3mr_process_event_ack(mrioc, fwevt->event_id,
1500 		    fwevt->evt_ctx);
1501 out:
1502 	/* Put fwevt reference count to neutralize kref_init increment */
1503 	mpi3mr_fwevt_put(fwevt);
1504 	mrioc->current_event = NULL;
1505 }
1506 
1507 /**
1508  * mpi3mr_fwevt_worker - Firmware event worker
1509  * @work: Work struct containing firmware event
1510  *
1511  * Extracts the firmware event and calls mpi3mr_fwevt_bh.
1512  *
1513  * Return: Nothing.
1514  */
1515 static void mpi3mr_fwevt_worker(struct work_struct *work)
1516 {
1517 	struct mpi3mr_fwevt *fwevt = container_of(work, struct mpi3mr_fwevt,
1518 	    work);
1519 	mpi3mr_fwevt_bh(fwevt->mrioc, fwevt);
1520 	/*
1521 	 * Put fwevt reference count after
1522 	 * dequeuing it from worker queue
1523 	 */
1524 	mpi3mr_fwevt_put(fwevt);
1525 }
1526 
1527 /**
1528  * mpi3mr_create_tgtdev - Create and add a target device
1529  * @mrioc: Adapter instance reference
1530  * @dev_pg0: Device Page 0 data
1531  *
1532  * If the device specified by the device page 0 data is not
1533  * present in the driver's internal list, allocate the memory
1534  * for the device, populate the data and add to the list, else
1535  * update the device data.  The key is persistent ID.
1536  *
1537  * Return: 0 on success, -ENOMEM on memory allocation failure
1538  */
1539 static int mpi3mr_create_tgtdev(struct mpi3mr_ioc *mrioc,
1540 	struct mpi3_device_page0 *dev_pg0)
1541 {
1542 	int retval = 0;
1543 	struct mpi3mr_tgt_dev *tgtdev = NULL;
1544 	u16 perst_id = 0;
1545 
1546 	perst_id = le16_to_cpu(dev_pg0->persistent_id);
1547 	tgtdev = mpi3mr_get_tgtdev_by_perst_id(mrioc, perst_id);
1548 	if (tgtdev) {
1549 		mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0);
1550 		mpi3mr_tgtdev_put(tgtdev);
1551 	} else {
1552 		tgtdev = mpi3mr_alloc_tgtdev();
1553 		if (!tgtdev)
1554 			return -ENOMEM;
1555 		mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0);
1556 		mpi3mr_tgtdev_add_to_list(mrioc, tgtdev);
1557 	}
1558 
1559 	return retval;
1560 }
1561 
1562 /**
1563  * mpi3mr_flush_delayed_cmd_lists - Flush pending commands
1564  * @mrioc: Adapter instance reference
1565  *
1566  * Flush pending commands in the delayed lists due to a
1567  * controller reset or driver removal as a cleanup.
1568  *
1569  * Return: Nothing
1570  */
1571 void mpi3mr_flush_delayed_cmd_lists(struct mpi3mr_ioc *mrioc)
1572 {
1573 	struct delayed_dev_rmhs_node *_rmhs_node;
1574 	struct delayed_evt_ack_node *_evtack_node;
1575 
1576 	dprint_reset(mrioc, "flushing delayed dev_remove_hs commands\n");
1577 	while (!list_empty(&mrioc->delayed_rmhs_list)) {
1578 		_rmhs_node = list_entry(mrioc->delayed_rmhs_list.next,
1579 		    struct delayed_dev_rmhs_node, list);
1580 		list_del(&_rmhs_node->list);
1581 		kfree(_rmhs_node);
1582 	}
1583 	dprint_reset(mrioc, "flushing delayed event ack commands\n");
1584 	while (!list_empty(&mrioc->delayed_evtack_cmds_list)) {
1585 		_evtack_node = list_entry(mrioc->delayed_evtack_cmds_list.next,
1586 		    struct delayed_evt_ack_node, list);
1587 		list_del(&_evtack_node->list);
1588 		kfree(_evtack_node);
1589 	}
1590 }
1591 
1592 /**
1593  * mpi3mr_dev_rmhs_complete_iou - Device removal IOUC completion
1594  * @mrioc: Adapter instance reference
1595  * @drv_cmd: Internal command tracker
1596  *
1597  * Issues a target reset TM to the firmware from the device
1598  * removal TM pend list or retry the removal handshake sequence
1599  * based on the IOU control request IOC status.
1600  *
1601  * Return: Nothing
1602  */
1603 static void mpi3mr_dev_rmhs_complete_iou(struct mpi3mr_ioc *mrioc,
1604 	struct mpi3mr_drv_cmd *drv_cmd)
1605 {
1606 	u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
1607 	struct delayed_dev_rmhs_node *delayed_dev_rmhs = NULL;
1608 
1609 	if (drv_cmd->state & MPI3MR_CMD_RESET)
1610 		goto clear_drv_cmd;
1611 
1612 	ioc_info(mrioc,
1613 	    "%s :dev_rmhs_iouctrl_complete:handle(0x%04x), ioc_status(0x%04x), loginfo(0x%08x)\n",
1614 	    __func__, drv_cmd->dev_handle, drv_cmd->ioc_status,
1615 	    drv_cmd->ioc_loginfo);
1616 	if (drv_cmd->ioc_status != MPI3_IOCSTATUS_SUCCESS) {
1617 		if (drv_cmd->retry_count < MPI3MR_DEV_RMHS_RETRY_COUNT) {
1618 			drv_cmd->retry_count++;
1619 			ioc_info(mrioc,
1620 			    "%s :dev_rmhs_iouctrl_complete: handle(0x%04x)retrying handshake retry=%d\n",
1621 			    __func__, drv_cmd->dev_handle,
1622 			    drv_cmd->retry_count);
1623 			mpi3mr_dev_rmhs_send_tm(mrioc, drv_cmd->dev_handle,
1624 			    drv_cmd, drv_cmd->iou_rc);
1625 			return;
1626 		}
1627 		ioc_err(mrioc,
1628 		    "%s :dev removal handshake failed after all retries: handle(0x%04x)\n",
1629 		    __func__, drv_cmd->dev_handle);
1630 	} else {
1631 		ioc_info(mrioc,
1632 		    "%s :dev removal handshake completed successfully: handle(0x%04x)\n",
1633 		    __func__, drv_cmd->dev_handle);
1634 		clear_bit(drv_cmd->dev_handle, mrioc->removepend_bitmap);
1635 	}
1636 
1637 	if (!list_empty(&mrioc->delayed_rmhs_list)) {
1638 		delayed_dev_rmhs = list_entry(mrioc->delayed_rmhs_list.next,
1639 		    struct delayed_dev_rmhs_node, list);
1640 		drv_cmd->dev_handle = delayed_dev_rmhs->handle;
1641 		drv_cmd->retry_count = 0;
1642 		drv_cmd->iou_rc = delayed_dev_rmhs->iou_rc;
1643 		ioc_info(mrioc,
1644 		    "%s :dev_rmhs_iouctrl_complete: processing delayed TM: handle(0x%04x)\n",
1645 		    __func__, drv_cmd->dev_handle);
1646 		mpi3mr_dev_rmhs_send_tm(mrioc, drv_cmd->dev_handle, drv_cmd,
1647 		    drv_cmd->iou_rc);
1648 		list_del(&delayed_dev_rmhs->list);
1649 		kfree(delayed_dev_rmhs);
1650 		return;
1651 	}
1652 
1653 clear_drv_cmd:
1654 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
1655 	drv_cmd->callback = NULL;
1656 	drv_cmd->retry_count = 0;
1657 	drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
1658 	clear_bit(cmd_idx, mrioc->devrem_bitmap);
1659 }
1660 
1661 /**
1662  * mpi3mr_dev_rmhs_complete_tm - Device removal TM completion
1663  * @mrioc: Adapter instance reference
1664  * @drv_cmd: Internal command tracker
1665  *
1666  * Issues a target reset TM to the firmware from the device
1667  * removal TM pend list or issue IO unit control request as
1668  * part of device removal or hidden acknowledgment handshake.
1669  *
1670  * Return: Nothing
1671  */
1672 static void mpi3mr_dev_rmhs_complete_tm(struct mpi3mr_ioc *mrioc,
1673 	struct mpi3mr_drv_cmd *drv_cmd)
1674 {
1675 	struct mpi3_iounit_control_request iou_ctrl;
1676 	u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
1677 	struct mpi3_scsi_task_mgmt_reply *tm_reply = NULL;
1678 	int retval;
1679 
1680 	if (drv_cmd->state & MPI3MR_CMD_RESET)
1681 		goto clear_drv_cmd;
1682 
1683 	if (drv_cmd->state & MPI3MR_CMD_REPLY_VALID)
1684 		tm_reply = (struct mpi3_scsi_task_mgmt_reply *)drv_cmd->reply;
1685 
1686 	if (tm_reply)
1687 		pr_info(IOCNAME
1688 		    "dev_rmhs_tr_complete:handle(0x%04x), ioc_status(0x%04x), loginfo(0x%08x), term_count(%d)\n",
1689 		    mrioc->name, drv_cmd->dev_handle, drv_cmd->ioc_status,
1690 		    drv_cmd->ioc_loginfo,
1691 		    le32_to_cpu(tm_reply->termination_count));
1692 
1693 	pr_info(IOCNAME "Issuing IOU CTL: handle(0x%04x) dev_rmhs idx(%d)\n",
1694 	    mrioc->name, drv_cmd->dev_handle, cmd_idx);
1695 
1696 	memset(&iou_ctrl, 0, sizeof(iou_ctrl));
1697 
1698 	drv_cmd->state = MPI3MR_CMD_PENDING;
1699 	drv_cmd->is_waiting = 0;
1700 	drv_cmd->callback = mpi3mr_dev_rmhs_complete_iou;
1701 	iou_ctrl.operation = drv_cmd->iou_rc;
1702 	iou_ctrl.param16[0] = cpu_to_le16(drv_cmd->dev_handle);
1703 	iou_ctrl.host_tag = cpu_to_le16(drv_cmd->host_tag);
1704 	iou_ctrl.function = MPI3_FUNCTION_IO_UNIT_CONTROL;
1705 
1706 	retval = mpi3mr_admin_request_post(mrioc, &iou_ctrl, sizeof(iou_ctrl),
1707 	    1);
1708 	if (retval) {
1709 		pr_err(IOCNAME "Issue DevRmHsTMIOUCTL: Admin post failed\n",
1710 		    mrioc->name);
1711 		goto clear_drv_cmd;
1712 	}
1713 
1714 	return;
1715 clear_drv_cmd:
1716 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
1717 	drv_cmd->callback = NULL;
1718 	drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
1719 	drv_cmd->retry_count = 0;
1720 	clear_bit(cmd_idx, mrioc->devrem_bitmap);
1721 }
1722 
1723 /**
1724  * mpi3mr_dev_rmhs_send_tm - Issue TM for device removal
1725  * @mrioc: Adapter instance reference
1726  * @handle: Device handle
1727  * @cmdparam: Internal command tracker
1728  * @iou_rc: IO unit reason code
1729  *
1730  * Issues a target reset TM to the firmware or add it to a pend
1731  * list as part of device removal or hidden acknowledgment
1732  * handshake.
1733  *
1734  * Return: Nothing
1735  */
1736 static void mpi3mr_dev_rmhs_send_tm(struct mpi3mr_ioc *mrioc, u16 handle,
1737 	struct mpi3mr_drv_cmd *cmdparam, u8 iou_rc)
1738 {
1739 	struct mpi3_scsi_task_mgmt_request tm_req;
1740 	int retval = 0;
1741 	u16 cmd_idx = MPI3MR_NUM_DEVRMCMD;
1742 	u8 retrycount = 5;
1743 	struct mpi3mr_drv_cmd *drv_cmd = cmdparam;
1744 	struct delayed_dev_rmhs_node *delayed_dev_rmhs = NULL;
1745 
1746 	if (drv_cmd)
1747 		goto issue_cmd;
1748 	do {
1749 		cmd_idx = find_first_zero_bit(mrioc->devrem_bitmap,
1750 		    MPI3MR_NUM_DEVRMCMD);
1751 		if (cmd_idx < MPI3MR_NUM_DEVRMCMD) {
1752 			if (!test_and_set_bit(cmd_idx, mrioc->devrem_bitmap))
1753 				break;
1754 			cmd_idx = MPI3MR_NUM_DEVRMCMD;
1755 		}
1756 	} while (retrycount--);
1757 
1758 	if (cmd_idx >= MPI3MR_NUM_DEVRMCMD) {
1759 		delayed_dev_rmhs = kzalloc(sizeof(*delayed_dev_rmhs),
1760 		    GFP_ATOMIC);
1761 		if (!delayed_dev_rmhs)
1762 			return;
1763 		INIT_LIST_HEAD(&delayed_dev_rmhs->list);
1764 		delayed_dev_rmhs->handle = handle;
1765 		delayed_dev_rmhs->iou_rc = iou_rc;
1766 		list_add_tail(&delayed_dev_rmhs->list,
1767 		    &mrioc->delayed_rmhs_list);
1768 		ioc_info(mrioc, "%s :DevRmHs: tr:handle(0x%04x) is postponed\n",
1769 		    __func__, handle);
1770 		return;
1771 	}
1772 	drv_cmd = &mrioc->dev_rmhs_cmds[cmd_idx];
1773 
1774 issue_cmd:
1775 	cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
1776 	ioc_info(mrioc,
1777 	    "%s :Issuing TR TM: for devhandle 0x%04x with dev_rmhs %d\n",
1778 	    __func__, handle, cmd_idx);
1779 
1780 	memset(&tm_req, 0, sizeof(tm_req));
1781 	if (drv_cmd->state & MPI3MR_CMD_PENDING) {
1782 		ioc_err(mrioc, "%s :Issue TM: Command is in use\n", __func__);
1783 		goto out;
1784 	}
1785 	drv_cmd->state = MPI3MR_CMD_PENDING;
1786 	drv_cmd->is_waiting = 0;
1787 	drv_cmd->callback = mpi3mr_dev_rmhs_complete_tm;
1788 	drv_cmd->dev_handle = handle;
1789 	drv_cmd->iou_rc = iou_rc;
1790 	tm_req.dev_handle = cpu_to_le16(handle);
1791 	tm_req.task_type = MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
1792 	tm_req.host_tag = cpu_to_le16(drv_cmd->host_tag);
1793 	tm_req.task_host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INVALID);
1794 	tm_req.function = MPI3_FUNCTION_SCSI_TASK_MGMT;
1795 
1796 	set_bit(handle, mrioc->removepend_bitmap);
1797 	retval = mpi3mr_admin_request_post(mrioc, &tm_req, sizeof(tm_req), 1);
1798 	if (retval) {
1799 		ioc_err(mrioc, "%s :Issue DevRmHsTM: Admin Post failed\n",
1800 		    __func__);
1801 		goto out_failed;
1802 	}
1803 out:
1804 	return;
1805 out_failed:
1806 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
1807 	drv_cmd->callback = NULL;
1808 	drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
1809 	drv_cmd->retry_count = 0;
1810 	clear_bit(cmd_idx, mrioc->devrem_bitmap);
1811 }
1812 
1813 /**
1814  * mpi3mr_complete_evt_ack - event ack request completion
1815  * @mrioc: Adapter instance reference
1816  * @drv_cmd: Internal command tracker
1817  *
1818  * This is the completion handler for non blocking event
1819  * acknowledgment sent to the firmware and this will issue any
1820  * pending event acknowledgment request.
1821  *
1822  * Return: Nothing
1823  */
1824 static void mpi3mr_complete_evt_ack(struct mpi3mr_ioc *mrioc,
1825 	struct mpi3mr_drv_cmd *drv_cmd)
1826 {
1827 	u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
1828 	struct delayed_evt_ack_node *delayed_evtack = NULL;
1829 
1830 	if (drv_cmd->state & MPI3MR_CMD_RESET)
1831 		goto clear_drv_cmd;
1832 
1833 	if (drv_cmd->ioc_status != MPI3_IOCSTATUS_SUCCESS) {
1834 		dprint_event_th(mrioc,
1835 		    "immediate event ack failed with ioc_status(0x%04x) log_info(0x%08x)\n",
1836 		    (drv_cmd->ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
1837 		    drv_cmd->ioc_loginfo);
1838 	}
1839 
1840 	if (!list_empty(&mrioc->delayed_evtack_cmds_list)) {
1841 		delayed_evtack =
1842 			list_entry(mrioc->delayed_evtack_cmds_list.next,
1843 			    struct delayed_evt_ack_node, list);
1844 		mpi3mr_send_event_ack(mrioc, delayed_evtack->event, drv_cmd,
1845 		    delayed_evtack->event_ctx);
1846 		list_del(&delayed_evtack->list);
1847 		kfree(delayed_evtack);
1848 		return;
1849 	}
1850 clear_drv_cmd:
1851 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
1852 	drv_cmd->callback = NULL;
1853 	clear_bit(cmd_idx, mrioc->evtack_cmds_bitmap);
1854 }
1855 
1856 /**
1857  * mpi3mr_send_event_ack - Issue event acknwoledgment request
1858  * @mrioc: Adapter instance reference
1859  * @event: MPI3 event id
1860  * @cmdparam: Internal command tracker
1861  * @event_ctx: event context
1862  *
1863  * Issues event acknowledgment request to the firmware if there
1864  * is a free command to send the event ack else it to a pend
1865  * list so that it will be processed on a completion of a prior
1866  * event acknowledgment .
1867  *
1868  * Return: Nothing
1869  */
1870 static void mpi3mr_send_event_ack(struct mpi3mr_ioc *mrioc, u8 event,
1871 	struct mpi3mr_drv_cmd *cmdparam, u32 event_ctx)
1872 {
1873 	struct mpi3_event_ack_request evtack_req;
1874 	int retval = 0;
1875 	u8 retrycount = 5;
1876 	u16 cmd_idx = MPI3MR_NUM_EVTACKCMD;
1877 	struct mpi3mr_drv_cmd *drv_cmd = cmdparam;
1878 	struct delayed_evt_ack_node *delayed_evtack = NULL;
1879 
1880 	if (drv_cmd) {
1881 		dprint_event_th(mrioc,
1882 		    "sending delayed event ack in the top half for event(0x%02x), event_ctx(0x%08x)\n",
1883 		    event, event_ctx);
1884 		goto issue_cmd;
1885 	}
1886 	dprint_event_th(mrioc,
1887 	    "sending event ack in the top half for event(0x%02x), event_ctx(0x%08x)\n",
1888 	    event, event_ctx);
1889 	do {
1890 		cmd_idx = find_first_zero_bit(mrioc->evtack_cmds_bitmap,
1891 		    MPI3MR_NUM_EVTACKCMD);
1892 		if (cmd_idx < MPI3MR_NUM_EVTACKCMD) {
1893 			if (!test_and_set_bit(cmd_idx,
1894 			    mrioc->evtack_cmds_bitmap))
1895 				break;
1896 			cmd_idx = MPI3MR_NUM_EVTACKCMD;
1897 		}
1898 	} while (retrycount--);
1899 
1900 	if (cmd_idx >= MPI3MR_NUM_EVTACKCMD) {
1901 		delayed_evtack = kzalloc(sizeof(*delayed_evtack),
1902 		    GFP_ATOMIC);
1903 		if (!delayed_evtack)
1904 			return;
1905 		INIT_LIST_HEAD(&delayed_evtack->list);
1906 		delayed_evtack->event = event;
1907 		delayed_evtack->event_ctx = event_ctx;
1908 		list_add_tail(&delayed_evtack->list,
1909 		    &mrioc->delayed_evtack_cmds_list);
1910 		dprint_event_th(mrioc,
1911 		    "event ack in the top half for event(0x%02x), event_ctx(0x%08x) is postponed\n",
1912 		    event, event_ctx);
1913 		return;
1914 	}
1915 	drv_cmd = &mrioc->evtack_cmds[cmd_idx];
1916 
1917 issue_cmd:
1918 	cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
1919 
1920 	memset(&evtack_req, 0, sizeof(evtack_req));
1921 	if (drv_cmd->state & MPI3MR_CMD_PENDING) {
1922 		dprint_event_th(mrioc,
1923 		    "sending event ack failed due to command in use\n");
1924 		goto out;
1925 	}
1926 	drv_cmd->state = MPI3MR_CMD_PENDING;
1927 	drv_cmd->is_waiting = 0;
1928 	drv_cmd->callback = mpi3mr_complete_evt_ack;
1929 	evtack_req.host_tag = cpu_to_le16(drv_cmd->host_tag);
1930 	evtack_req.function = MPI3_FUNCTION_EVENT_ACK;
1931 	evtack_req.event = event;
1932 	evtack_req.event_context = cpu_to_le32(event_ctx);
1933 	retval = mpi3mr_admin_request_post(mrioc, &evtack_req,
1934 	    sizeof(evtack_req), 1);
1935 	if (retval) {
1936 		dprint_event_th(mrioc,
1937 		    "posting event ack request is failed\n");
1938 		goto out_failed;
1939 	}
1940 
1941 	dprint_event_th(mrioc,
1942 	    "event ack in the top half for event(0x%02x), event_ctx(0x%08x) is posted\n",
1943 	    event, event_ctx);
1944 out:
1945 	return;
1946 out_failed:
1947 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
1948 	drv_cmd->callback = NULL;
1949 	clear_bit(cmd_idx, mrioc->evtack_cmds_bitmap);
1950 }
1951 
1952 /**
1953  * mpi3mr_pcietopochg_evt_th - PCIETopologyChange evt tophalf
1954  * @mrioc: Adapter instance reference
1955  * @event_reply: event data
1956  *
1957  * Checks for the reason code and based on that either block I/O
1958  * to device, or unblock I/O to the device, or start the device
1959  * removal handshake with reason as remove with the firmware for
1960  * PCIe devices.
1961  *
1962  * Return: Nothing
1963  */
1964 static void mpi3mr_pcietopochg_evt_th(struct mpi3mr_ioc *mrioc,
1965 	struct mpi3_event_notification_reply *event_reply)
1966 {
1967 	struct mpi3_event_data_pcie_topology_change_list *topo_evt =
1968 	    (struct mpi3_event_data_pcie_topology_change_list *)event_reply->event_data;
1969 	int i;
1970 	u16 handle;
1971 	u8 reason_code;
1972 	struct mpi3mr_tgt_dev *tgtdev = NULL;
1973 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
1974 
1975 	for (i = 0; i < topo_evt->num_entries; i++) {
1976 		handle = le16_to_cpu(topo_evt->port_entry[i].attached_dev_handle);
1977 		if (!handle)
1978 			continue;
1979 		reason_code = topo_evt->port_entry[i].port_status;
1980 		scsi_tgt_priv_data =  NULL;
1981 		tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
1982 		if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
1983 			scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
1984 			    tgtdev->starget->hostdata;
1985 		switch (reason_code) {
1986 		case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
1987 			if (scsi_tgt_priv_data) {
1988 				scsi_tgt_priv_data->dev_removed = 1;
1989 				scsi_tgt_priv_data->dev_removedelay = 0;
1990 				atomic_set(&scsi_tgt_priv_data->block_io, 0);
1991 			}
1992 			mpi3mr_dev_rmhs_send_tm(mrioc, handle, NULL,
1993 			    MPI3_CTRL_OP_REMOVE_DEVICE);
1994 			break;
1995 		case MPI3_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
1996 			if (scsi_tgt_priv_data) {
1997 				scsi_tgt_priv_data->dev_removedelay = 1;
1998 				atomic_inc(&scsi_tgt_priv_data->block_io);
1999 			}
2000 			break;
2001 		case MPI3_EVENT_PCIE_TOPO_PS_RESPONDING:
2002 			if (scsi_tgt_priv_data &&
2003 			    scsi_tgt_priv_data->dev_removedelay) {
2004 				scsi_tgt_priv_data->dev_removedelay = 0;
2005 				atomic_dec_if_positive
2006 				    (&scsi_tgt_priv_data->block_io);
2007 			}
2008 			break;
2009 		case MPI3_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
2010 		default:
2011 			break;
2012 		}
2013 		if (tgtdev)
2014 			mpi3mr_tgtdev_put(tgtdev);
2015 	}
2016 }
2017 
2018 /**
2019  * mpi3mr_sastopochg_evt_th - SASTopologyChange evt tophalf
2020  * @mrioc: Adapter instance reference
2021  * @event_reply: event data
2022  *
2023  * Checks for the reason code and based on that either block I/O
2024  * to device, or unblock I/O to the device, or start the device
2025  * removal handshake with reason as remove with the firmware for
2026  * SAS/SATA devices.
2027  *
2028  * Return: Nothing
2029  */
2030 static void mpi3mr_sastopochg_evt_th(struct mpi3mr_ioc *mrioc,
2031 	struct mpi3_event_notification_reply *event_reply)
2032 {
2033 	struct mpi3_event_data_sas_topology_change_list *topo_evt =
2034 	    (struct mpi3_event_data_sas_topology_change_list *)event_reply->event_data;
2035 	int i;
2036 	u16 handle;
2037 	u8 reason_code;
2038 	struct mpi3mr_tgt_dev *tgtdev = NULL;
2039 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2040 
2041 	for (i = 0; i < topo_evt->num_entries; i++) {
2042 		handle = le16_to_cpu(topo_evt->phy_entry[i].attached_dev_handle);
2043 		if (!handle)
2044 			continue;
2045 		reason_code = topo_evt->phy_entry[i].status &
2046 		    MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
2047 		scsi_tgt_priv_data =  NULL;
2048 		tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
2049 		if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
2050 			scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2051 			    tgtdev->starget->hostdata;
2052 		switch (reason_code) {
2053 		case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
2054 			if (scsi_tgt_priv_data) {
2055 				scsi_tgt_priv_data->dev_removed = 1;
2056 				scsi_tgt_priv_data->dev_removedelay = 0;
2057 				atomic_set(&scsi_tgt_priv_data->block_io, 0);
2058 			}
2059 			mpi3mr_dev_rmhs_send_tm(mrioc, handle, NULL,
2060 			    MPI3_CTRL_OP_REMOVE_DEVICE);
2061 			break;
2062 		case MPI3_EVENT_SAS_TOPO_PHY_RC_DELAY_NOT_RESPONDING:
2063 			if (scsi_tgt_priv_data) {
2064 				scsi_tgt_priv_data->dev_removedelay = 1;
2065 				atomic_inc(&scsi_tgt_priv_data->block_io);
2066 			}
2067 			break;
2068 		case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
2069 			if (scsi_tgt_priv_data &&
2070 			    scsi_tgt_priv_data->dev_removedelay) {
2071 				scsi_tgt_priv_data->dev_removedelay = 0;
2072 				atomic_dec_if_positive
2073 				    (&scsi_tgt_priv_data->block_io);
2074 			}
2075 			break;
2076 		case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
2077 		default:
2078 			break;
2079 		}
2080 		if (tgtdev)
2081 			mpi3mr_tgtdev_put(tgtdev);
2082 	}
2083 }
2084 
2085 /**
2086  * mpi3mr_devstatuschg_evt_th - DeviceStatusChange evt tophalf
2087  * @mrioc: Adapter instance reference
2088  * @event_reply: event data
2089  *
2090  * Checks for the reason code and based on that either block I/O
2091  * to device, or unblock I/O to the device, or start the device
2092  * removal handshake with reason as remove/hide acknowledgment
2093  * with the firmware.
2094  *
2095  * Return: Nothing
2096  */
2097 static void mpi3mr_devstatuschg_evt_th(struct mpi3mr_ioc *mrioc,
2098 	struct mpi3_event_notification_reply *event_reply)
2099 {
2100 	u16 dev_handle = 0;
2101 	u8 ublock = 0, block = 0, hide = 0, delete = 0, remove = 0;
2102 	struct mpi3mr_tgt_dev *tgtdev = NULL;
2103 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2104 	struct mpi3_event_data_device_status_change *evtdata =
2105 	    (struct mpi3_event_data_device_status_change *)event_reply->event_data;
2106 
2107 	if (mrioc->stop_drv_processing)
2108 		goto out;
2109 
2110 	dev_handle = le16_to_cpu(evtdata->dev_handle);
2111 
2112 	switch (evtdata->reason_code) {
2113 	case MPI3_EVENT_DEV_STAT_RC_INT_DEVICE_RESET_STRT:
2114 	case MPI3_EVENT_DEV_STAT_RC_INT_IT_NEXUS_RESET_STRT:
2115 		block = 1;
2116 		break;
2117 	case MPI3_EVENT_DEV_STAT_RC_HIDDEN:
2118 		delete = 1;
2119 		hide = 1;
2120 		break;
2121 	case MPI3_EVENT_DEV_STAT_RC_VD_NOT_RESPONDING:
2122 		delete = 1;
2123 		remove = 1;
2124 		break;
2125 	case MPI3_EVENT_DEV_STAT_RC_INT_DEVICE_RESET_CMP:
2126 	case MPI3_EVENT_DEV_STAT_RC_INT_IT_NEXUS_RESET_CMP:
2127 		ublock = 1;
2128 		break;
2129 	default:
2130 		break;
2131 	}
2132 
2133 	tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
2134 	if (!tgtdev)
2135 		goto out;
2136 	if (hide)
2137 		tgtdev->is_hidden = hide;
2138 	if (tgtdev->starget && tgtdev->starget->hostdata) {
2139 		scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2140 		    tgtdev->starget->hostdata;
2141 		if (block)
2142 			atomic_inc(&scsi_tgt_priv_data->block_io);
2143 		if (delete)
2144 			scsi_tgt_priv_data->dev_removed = 1;
2145 		if (ublock)
2146 			atomic_dec_if_positive(&scsi_tgt_priv_data->block_io);
2147 	}
2148 	if (remove)
2149 		mpi3mr_dev_rmhs_send_tm(mrioc, dev_handle, NULL,
2150 		    MPI3_CTRL_OP_REMOVE_DEVICE);
2151 	if (hide)
2152 		mpi3mr_dev_rmhs_send_tm(mrioc, dev_handle, NULL,
2153 		    MPI3_CTRL_OP_HIDDEN_ACK);
2154 
2155 out:
2156 	if (tgtdev)
2157 		mpi3mr_tgtdev_put(tgtdev);
2158 }
2159 
2160 /**
2161  * mpi3mr_preparereset_evt_th - Prepare for reset event tophalf
2162  * @mrioc: Adapter instance reference
2163  * @event_reply: event data
2164  *
2165  * Blocks and unblocks host level I/O based on the reason code
2166  *
2167  * Return: Nothing
2168  */
2169 static void mpi3mr_preparereset_evt_th(struct mpi3mr_ioc *mrioc,
2170 	struct mpi3_event_notification_reply *event_reply)
2171 {
2172 	struct mpi3_event_data_prepare_for_reset *evtdata =
2173 	    (struct mpi3_event_data_prepare_for_reset *)event_reply->event_data;
2174 
2175 	if (evtdata->reason_code == MPI3_EVENT_PREPARE_RESET_RC_START) {
2176 		dprint_event_th(mrioc,
2177 		    "prepare for reset event top half with rc=start\n");
2178 		if (mrioc->prepare_for_reset)
2179 			return;
2180 		mrioc->prepare_for_reset = 1;
2181 		mrioc->prepare_for_reset_timeout_counter = 0;
2182 	} else if (evtdata->reason_code == MPI3_EVENT_PREPARE_RESET_RC_ABORT) {
2183 		dprint_event_th(mrioc,
2184 		    "prepare for reset top half with rc=abort\n");
2185 		mrioc->prepare_for_reset = 0;
2186 		mrioc->prepare_for_reset_timeout_counter = 0;
2187 	}
2188 	if ((event_reply->msg_flags & MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_MASK)
2189 	    == MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_REQUIRED)
2190 		mpi3mr_send_event_ack(mrioc, event_reply->event, NULL,
2191 		    le32_to_cpu(event_reply->event_context));
2192 }
2193 
2194 /**
2195  * mpi3mr_energypackchg_evt_th - Energy pack change evt tophalf
2196  * @mrioc: Adapter instance reference
2197  * @event_reply: event data
2198  *
2199  * Identifies the new shutdown timeout value and update.
2200  *
2201  * Return: Nothing
2202  */
2203 static void mpi3mr_energypackchg_evt_th(struct mpi3mr_ioc *mrioc,
2204 	struct mpi3_event_notification_reply *event_reply)
2205 {
2206 	struct mpi3_event_data_energy_pack_change *evtdata =
2207 	    (struct mpi3_event_data_energy_pack_change *)event_reply->event_data;
2208 	u16 shutdown_timeout = le16_to_cpu(evtdata->shutdown_timeout);
2209 
2210 	if (shutdown_timeout <= 0) {
2211 		ioc_warn(mrioc,
2212 		    "%s :Invalid Shutdown Timeout received = %d\n",
2213 		    __func__, shutdown_timeout);
2214 		return;
2215 	}
2216 
2217 	ioc_info(mrioc,
2218 	    "%s :Previous Shutdown Timeout Value = %d New Shutdown Timeout Value = %d\n",
2219 	    __func__, mrioc->facts.shutdown_timeout, shutdown_timeout);
2220 	mrioc->facts.shutdown_timeout = shutdown_timeout;
2221 }
2222 
2223 /**
2224  * mpi3mr_cablemgmt_evt_th - Cable management event tophalf
2225  * @mrioc: Adapter instance reference
2226  * @event_reply: event data
2227  *
2228  * Displays Cable manegemt event details.
2229  *
2230  * Return: Nothing
2231  */
2232 static void mpi3mr_cablemgmt_evt_th(struct mpi3mr_ioc *mrioc,
2233 	struct mpi3_event_notification_reply *event_reply)
2234 {
2235 	struct mpi3_event_data_cable_management *evtdata =
2236 	    (struct mpi3_event_data_cable_management *)event_reply->event_data;
2237 
2238 	switch (evtdata->status) {
2239 	case MPI3_EVENT_CABLE_MGMT_STATUS_INSUFFICIENT_POWER:
2240 	{
2241 		ioc_info(mrioc, "An active cable with receptacle_id %d cannot be powered.\n"
2242 		    "Devices connected to this cable are not detected.\n"
2243 		    "This cable requires %d mW of power.\n",
2244 		    evtdata->receptacle_id,
2245 		    le32_to_cpu(evtdata->active_cable_power_requirement));
2246 		break;
2247 	}
2248 	case MPI3_EVENT_CABLE_MGMT_STATUS_DEGRADED:
2249 	{
2250 		ioc_info(mrioc, "A cable with receptacle_id %d is not running at optimal speed\n",
2251 		    evtdata->receptacle_id);
2252 		break;
2253 	}
2254 	default:
2255 		break;
2256 	}
2257 }
2258 
2259 /**
2260  * mpi3mr_os_handle_events - Firmware event handler
2261  * @mrioc: Adapter instance reference
2262  * @event_reply: event data
2263  *
2264  * Identify whteher the event has to handled and acknowledged
2265  * and either process the event in the tophalf and/or schedule a
2266  * bottom half through mpi3mr_fwevt_worker.
2267  *
2268  * Return: Nothing
2269  */
2270 void mpi3mr_os_handle_events(struct mpi3mr_ioc *mrioc,
2271 	struct mpi3_event_notification_reply *event_reply)
2272 {
2273 	u16 evt_type, sz;
2274 	struct mpi3mr_fwevt *fwevt = NULL;
2275 	bool ack_req = 0, process_evt_bh = 0;
2276 
2277 	if (mrioc->stop_drv_processing)
2278 		return;
2279 
2280 	if ((event_reply->msg_flags & MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_MASK)
2281 	    == MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_REQUIRED)
2282 		ack_req = 1;
2283 
2284 	evt_type = event_reply->event;
2285 
2286 	switch (evt_type) {
2287 	case MPI3_EVENT_DEVICE_ADDED:
2288 	{
2289 		struct mpi3_device_page0 *dev_pg0 =
2290 		    (struct mpi3_device_page0 *)event_reply->event_data;
2291 		if (mpi3mr_create_tgtdev(mrioc, dev_pg0))
2292 			ioc_err(mrioc,
2293 			    "%s :Failed to add device in the device add event\n",
2294 			    __func__);
2295 		else
2296 			process_evt_bh = 1;
2297 		break;
2298 	}
2299 	case MPI3_EVENT_DEVICE_STATUS_CHANGE:
2300 	{
2301 		process_evt_bh = 1;
2302 		mpi3mr_devstatuschg_evt_th(mrioc, event_reply);
2303 		break;
2304 	}
2305 	case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
2306 	{
2307 		process_evt_bh = 1;
2308 		mpi3mr_sastopochg_evt_th(mrioc, event_reply);
2309 		break;
2310 	}
2311 	case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
2312 	{
2313 		process_evt_bh = 1;
2314 		mpi3mr_pcietopochg_evt_th(mrioc, event_reply);
2315 		break;
2316 	}
2317 	case MPI3_EVENT_PREPARE_FOR_RESET:
2318 	{
2319 		mpi3mr_preparereset_evt_th(mrioc, event_reply);
2320 		ack_req = 0;
2321 		break;
2322 	}
2323 	case MPI3_EVENT_DEVICE_INFO_CHANGED:
2324 	case MPI3_EVENT_LOG_DATA:
2325 	{
2326 		process_evt_bh = 1;
2327 		break;
2328 	}
2329 	case MPI3_EVENT_ENERGY_PACK_CHANGE:
2330 	{
2331 		mpi3mr_energypackchg_evt_th(mrioc, event_reply);
2332 		break;
2333 	}
2334 	case MPI3_EVENT_CABLE_MGMT:
2335 	{
2336 		mpi3mr_cablemgmt_evt_th(mrioc, event_reply);
2337 		break;
2338 	}
2339 	case MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE:
2340 	case MPI3_EVENT_SAS_DISCOVERY:
2341 	case MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
2342 	case MPI3_EVENT_SAS_BROADCAST_PRIMITIVE:
2343 	case MPI3_EVENT_PCIE_ENUMERATION:
2344 		break;
2345 	default:
2346 		ioc_info(mrioc, "%s :event 0x%02x is not handled\n",
2347 		    __func__, evt_type);
2348 		break;
2349 	}
2350 	if (process_evt_bh || ack_req) {
2351 		sz = event_reply->event_data_length * 4;
2352 		fwevt = mpi3mr_alloc_fwevt(sz);
2353 		if (!fwevt) {
2354 			ioc_info(mrioc, "%s :failure at %s:%d/%s()!\n",
2355 			    __func__, __FILE__, __LINE__, __func__);
2356 			return;
2357 		}
2358 
2359 		memcpy(fwevt->event_data, event_reply->event_data, sz);
2360 		fwevt->mrioc = mrioc;
2361 		fwevt->event_id = evt_type;
2362 		fwevt->send_ack = ack_req;
2363 		fwevt->process_evt = process_evt_bh;
2364 		fwevt->evt_ctx = le32_to_cpu(event_reply->event_context);
2365 		mpi3mr_fwevt_add_to_list(mrioc, fwevt);
2366 	}
2367 }
2368 
2369 /**
2370  * mpi3mr_setup_eedp - Setup EEDP information in MPI3 SCSI IO
2371  * @mrioc: Adapter instance reference
2372  * @scmd: SCSI command reference
2373  * @scsiio_req: MPI3 SCSI IO request
2374  *
2375  * Identifies the protection information flags from the SCSI
2376  * command and set appropriate flags in the MPI3 SCSI IO
2377  * request.
2378  *
2379  * Return: Nothing
2380  */
2381 static void mpi3mr_setup_eedp(struct mpi3mr_ioc *mrioc,
2382 	struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
2383 {
2384 	u16 eedp_flags = 0;
2385 	unsigned char prot_op = scsi_get_prot_op(scmd);
2386 
2387 	switch (prot_op) {
2388 	case SCSI_PROT_NORMAL:
2389 		return;
2390 	case SCSI_PROT_READ_STRIP:
2391 		eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REMOVE;
2392 		break;
2393 	case SCSI_PROT_WRITE_INSERT:
2394 		eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_INSERT;
2395 		break;
2396 	case SCSI_PROT_READ_INSERT:
2397 		eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_INSERT;
2398 		scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2399 		break;
2400 	case SCSI_PROT_WRITE_STRIP:
2401 		eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REMOVE;
2402 		scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2403 		break;
2404 	case SCSI_PROT_READ_PASS:
2405 		eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK;
2406 		scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2407 		break;
2408 	case SCSI_PROT_WRITE_PASS:
2409 		if (scmd->prot_flags & SCSI_PROT_IP_CHECKSUM) {
2410 			eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REGEN;
2411 			scsiio_req->sgl[0].eedp.application_tag_translation_mask =
2412 			    0xffff;
2413 		} else
2414 			eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK;
2415 
2416 		scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2417 		break;
2418 	default:
2419 		return;
2420 	}
2421 
2422 	if (scmd->prot_flags & SCSI_PROT_GUARD_CHECK)
2423 		eedp_flags |= MPI3_EEDPFLAGS_CHK_GUARD;
2424 
2425 	if (scmd->prot_flags & SCSI_PROT_IP_CHECKSUM)
2426 		eedp_flags |= MPI3_EEDPFLAGS_HOST_GUARD_IP_CHKSUM;
2427 
2428 	if (scmd->prot_flags & SCSI_PROT_REF_CHECK) {
2429 		eedp_flags |= MPI3_EEDPFLAGS_CHK_REF_TAG |
2430 			MPI3_EEDPFLAGS_INCR_PRI_REF_TAG;
2431 		scsiio_req->cdb.eedp32.primary_reference_tag =
2432 			cpu_to_be32(scsi_prot_ref_tag(scmd));
2433 	}
2434 
2435 	if (scmd->prot_flags & SCSI_PROT_REF_INCREMENT)
2436 		eedp_flags |= MPI3_EEDPFLAGS_INCR_PRI_REF_TAG;
2437 
2438 	eedp_flags |= MPI3_EEDPFLAGS_ESC_MODE_APPTAG_DISABLE;
2439 
2440 	switch (scsi_prot_interval(scmd)) {
2441 	case 512:
2442 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_512;
2443 		break;
2444 	case 520:
2445 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_520;
2446 		break;
2447 	case 4080:
2448 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4080;
2449 		break;
2450 	case 4088:
2451 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4088;
2452 		break;
2453 	case 4096:
2454 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4096;
2455 		break;
2456 	case 4104:
2457 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4104;
2458 		break;
2459 	case 4160:
2460 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4160;
2461 		break;
2462 	default:
2463 		break;
2464 	}
2465 
2466 	scsiio_req->sgl[0].eedp.eedp_flags = cpu_to_le16(eedp_flags);
2467 	scsiio_req->sgl[0].eedp.flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_EXTENDED;
2468 }
2469 
2470 /**
2471  * mpi3mr_build_sense_buffer - Map sense information
2472  * @desc: Sense type
2473  * @buf: Sense buffer to populate
2474  * @key: Sense key
2475  * @asc: Additional sense code
2476  * @ascq: Additional sense code qualifier
2477  *
2478  * Maps the given sense information into either descriptor or
2479  * fixed format sense data.
2480  *
2481  * Return: Nothing
2482  */
2483 static inline void mpi3mr_build_sense_buffer(int desc, u8 *buf, u8 key,
2484 	u8 asc, u8 ascq)
2485 {
2486 	if (desc) {
2487 		buf[0] = 0x72;	/* descriptor, current */
2488 		buf[1] = key;
2489 		buf[2] = asc;
2490 		buf[3] = ascq;
2491 		buf[7] = 0;
2492 	} else {
2493 		buf[0] = 0x70;	/* fixed, current */
2494 		buf[2] = key;
2495 		buf[7] = 0xa;
2496 		buf[12] = asc;
2497 		buf[13] = ascq;
2498 	}
2499 }
2500 
2501 /**
2502  * mpi3mr_map_eedp_error - Map EEDP errors from IOC status
2503  * @scmd: SCSI command reference
2504  * @ioc_status: status of MPI3 request
2505  *
2506  * Maps the EEDP error status of the SCSI IO request to sense
2507  * data.
2508  *
2509  * Return: Nothing
2510  */
2511 static void mpi3mr_map_eedp_error(struct scsi_cmnd *scmd,
2512 	u16 ioc_status)
2513 {
2514 	u8 ascq = 0;
2515 
2516 	switch (ioc_status) {
2517 	case MPI3_IOCSTATUS_EEDP_GUARD_ERROR:
2518 		ascq = 0x01;
2519 		break;
2520 	case MPI3_IOCSTATUS_EEDP_APP_TAG_ERROR:
2521 		ascq = 0x02;
2522 		break;
2523 	case MPI3_IOCSTATUS_EEDP_REF_TAG_ERROR:
2524 		ascq = 0x03;
2525 		break;
2526 	default:
2527 		ascq = 0x00;
2528 		break;
2529 	}
2530 
2531 	mpi3mr_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
2532 	    0x10, ascq);
2533 	scmd->result = (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION;
2534 }
2535 
2536 /**
2537  * mpi3mr_process_op_reply_desc - reply descriptor handler
2538  * @mrioc: Adapter instance reference
2539  * @reply_desc: Operational reply descriptor
2540  * @reply_dma: place holder for reply DMA address
2541  * @qidx: Operational queue index
2542  *
2543  * Process the operational reply descriptor and identifies the
2544  * descriptor type. Based on the descriptor map the MPI3 request
2545  * status to a SCSI command status and calls scsi_done call
2546  * back.
2547  *
2548  * Return: Nothing
2549  */
2550 void mpi3mr_process_op_reply_desc(struct mpi3mr_ioc *mrioc,
2551 	struct mpi3_default_reply_descriptor *reply_desc, u64 *reply_dma, u16 qidx)
2552 {
2553 	u16 reply_desc_type, host_tag = 0;
2554 	u16 ioc_status = MPI3_IOCSTATUS_SUCCESS;
2555 	u32 ioc_loginfo = 0;
2556 	struct mpi3_status_reply_descriptor *status_desc = NULL;
2557 	struct mpi3_address_reply_descriptor *addr_desc = NULL;
2558 	struct mpi3_success_reply_descriptor *success_desc = NULL;
2559 	struct mpi3_scsi_io_reply *scsi_reply = NULL;
2560 	struct scsi_cmnd *scmd = NULL;
2561 	struct scmd_priv *priv = NULL;
2562 	u8 *sense_buf = NULL;
2563 	u8 scsi_state = 0, scsi_status = 0, sense_state = 0;
2564 	u32 xfer_count = 0, sense_count = 0, resp_data = 0;
2565 	u16 dev_handle = 0xFFFF;
2566 	struct scsi_sense_hdr sshdr;
2567 
2568 	*reply_dma = 0;
2569 	reply_desc_type = le16_to_cpu(reply_desc->reply_flags) &
2570 	    MPI3_REPLY_DESCRIPT_FLAGS_TYPE_MASK;
2571 	switch (reply_desc_type) {
2572 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_STATUS:
2573 		status_desc = (struct mpi3_status_reply_descriptor *)reply_desc;
2574 		host_tag = le16_to_cpu(status_desc->host_tag);
2575 		ioc_status = le16_to_cpu(status_desc->ioc_status);
2576 		if (ioc_status &
2577 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
2578 			ioc_loginfo = le32_to_cpu(status_desc->ioc_log_info);
2579 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
2580 		break;
2581 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_ADDRESS_REPLY:
2582 		addr_desc = (struct mpi3_address_reply_descriptor *)reply_desc;
2583 		*reply_dma = le64_to_cpu(addr_desc->reply_frame_address);
2584 		scsi_reply = mpi3mr_get_reply_virt_addr(mrioc,
2585 		    *reply_dma);
2586 		if (!scsi_reply) {
2587 			panic("%s: scsi_reply is NULL, this shouldn't happen\n",
2588 			    mrioc->name);
2589 			goto out;
2590 		}
2591 		host_tag = le16_to_cpu(scsi_reply->host_tag);
2592 		ioc_status = le16_to_cpu(scsi_reply->ioc_status);
2593 		scsi_status = scsi_reply->scsi_status;
2594 		scsi_state = scsi_reply->scsi_state;
2595 		dev_handle = le16_to_cpu(scsi_reply->dev_handle);
2596 		sense_state = (scsi_state & MPI3_SCSI_STATE_SENSE_MASK);
2597 		xfer_count = le32_to_cpu(scsi_reply->transfer_count);
2598 		sense_count = le32_to_cpu(scsi_reply->sense_count);
2599 		resp_data = le32_to_cpu(scsi_reply->response_data);
2600 		sense_buf = mpi3mr_get_sensebuf_virt_addr(mrioc,
2601 		    le64_to_cpu(scsi_reply->sense_data_buffer_address));
2602 		if (ioc_status &
2603 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
2604 			ioc_loginfo = le32_to_cpu(scsi_reply->ioc_log_info);
2605 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
2606 		if (sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY)
2607 			panic("%s: Ran out of sense buffers\n", mrioc->name);
2608 		break;
2609 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_SUCCESS:
2610 		success_desc = (struct mpi3_success_reply_descriptor *)reply_desc;
2611 		host_tag = le16_to_cpu(success_desc->host_tag);
2612 		break;
2613 	default:
2614 		break;
2615 	}
2616 	scmd = mpi3mr_scmd_from_host_tag(mrioc, host_tag, qidx);
2617 	if (!scmd) {
2618 		panic("%s: Cannot Identify scmd for host_tag 0x%x\n",
2619 		    mrioc->name, host_tag);
2620 		goto out;
2621 	}
2622 	priv = scsi_cmd_priv(scmd);
2623 	if (success_desc) {
2624 		scmd->result = DID_OK << 16;
2625 		goto out_success;
2626 	}
2627 
2628 	scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_count);
2629 	if (ioc_status == MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN &&
2630 	    xfer_count == 0 && (scsi_status == MPI3_SCSI_STATUS_BUSY ||
2631 	    scsi_status == MPI3_SCSI_STATUS_RESERVATION_CONFLICT ||
2632 	    scsi_status == MPI3_SCSI_STATUS_TASK_SET_FULL))
2633 		ioc_status = MPI3_IOCSTATUS_SUCCESS;
2634 
2635 	if ((sense_state == MPI3_SCSI_STATE_SENSE_VALID) && sense_count &&
2636 	    sense_buf) {
2637 		u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE, sense_count);
2638 
2639 		memcpy(scmd->sense_buffer, sense_buf, sz);
2640 	}
2641 
2642 	switch (ioc_status) {
2643 	case MPI3_IOCSTATUS_BUSY:
2644 	case MPI3_IOCSTATUS_INSUFFICIENT_RESOURCES:
2645 		scmd->result = SAM_STAT_BUSY;
2646 		break;
2647 	case MPI3_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
2648 		scmd->result = DID_NO_CONNECT << 16;
2649 		break;
2650 	case MPI3_IOCSTATUS_SCSI_IOC_TERMINATED:
2651 		scmd->result = DID_SOFT_ERROR << 16;
2652 		break;
2653 	case MPI3_IOCSTATUS_SCSI_TASK_TERMINATED:
2654 	case MPI3_IOCSTATUS_SCSI_EXT_TERMINATED:
2655 		scmd->result = DID_RESET << 16;
2656 		break;
2657 	case MPI3_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
2658 		if ((xfer_count == 0) || (scmd->underflow > xfer_count))
2659 			scmd->result = DID_SOFT_ERROR << 16;
2660 		else
2661 			scmd->result = (DID_OK << 16) | scsi_status;
2662 		break;
2663 	case MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN:
2664 		scmd->result = (DID_OK << 16) | scsi_status;
2665 		if (sense_state == MPI3_SCSI_STATE_SENSE_VALID)
2666 			break;
2667 		if (xfer_count < scmd->underflow) {
2668 			if (scsi_status == SAM_STAT_BUSY)
2669 				scmd->result = SAM_STAT_BUSY;
2670 			else
2671 				scmd->result = DID_SOFT_ERROR << 16;
2672 		} else if ((scsi_state & (MPI3_SCSI_STATE_NO_SCSI_STATUS)) ||
2673 		    (sense_state != MPI3_SCSI_STATE_SENSE_NOT_AVAILABLE))
2674 			scmd->result = DID_SOFT_ERROR << 16;
2675 		else if (scsi_state & MPI3_SCSI_STATE_TERMINATED)
2676 			scmd->result = DID_RESET << 16;
2677 		break;
2678 	case MPI3_IOCSTATUS_SCSI_DATA_OVERRUN:
2679 		scsi_set_resid(scmd, 0);
2680 		fallthrough;
2681 	case MPI3_IOCSTATUS_SCSI_RECOVERED_ERROR:
2682 	case MPI3_IOCSTATUS_SUCCESS:
2683 		scmd->result = (DID_OK << 16) | scsi_status;
2684 		if ((scsi_state & (MPI3_SCSI_STATE_NO_SCSI_STATUS)) ||
2685 		    (sense_state == MPI3_SCSI_STATE_SENSE_FAILED) ||
2686 			(sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY))
2687 			scmd->result = DID_SOFT_ERROR << 16;
2688 		else if (scsi_state & MPI3_SCSI_STATE_TERMINATED)
2689 			scmd->result = DID_RESET << 16;
2690 		break;
2691 	case MPI3_IOCSTATUS_EEDP_GUARD_ERROR:
2692 	case MPI3_IOCSTATUS_EEDP_REF_TAG_ERROR:
2693 	case MPI3_IOCSTATUS_EEDP_APP_TAG_ERROR:
2694 		mpi3mr_map_eedp_error(scmd, ioc_status);
2695 		break;
2696 	case MPI3_IOCSTATUS_SCSI_PROTOCOL_ERROR:
2697 	case MPI3_IOCSTATUS_INVALID_FUNCTION:
2698 	case MPI3_IOCSTATUS_INVALID_SGL:
2699 	case MPI3_IOCSTATUS_INTERNAL_ERROR:
2700 	case MPI3_IOCSTATUS_INVALID_FIELD:
2701 	case MPI3_IOCSTATUS_INVALID_STATE:
2702 	case MPI3_IOCSTATUS_SCSI_IO_DATA_ERROR:
2703 	case MPI3_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
2704 	case MPI3_IOCSTATUS_INSUFFICIENT_POWER:
2705 	default:
2706 		scmd->result = DID_SOFT_ERROR << 16;
2707 		break;
2708 	}
2709 
2710 	if (scmd->result != (DID_OK << 16) && (scmd->cmnd[0] != ATA_12) &&
2711 	    (scmd->cmnd[0] != ATA_16)) {
2712 		ioc_info(mrioc, "%s :scmd->result 0x%x\n", __func__,
2713 		    scmd->result);
2714 		scsi_print_command(scmd);
2715 		ioc_info(mrioc,
2716 		    "%s :Command issued to handle 0x%02x returned with error 0x%04x loginfo 0x%08x, qid %d\n",
2717 		    __func__, dev_handle, ioc_status, ioc_loginfo,
2718 		    priv->req_q_idx + 1);
2719 		ioc_info(mrioc,
2720 		    " host_tag %d scsi_state 0x%02x scsi_status 0x%02x, xfer_cnt %d resp_data 0x%x\n",
2721 		    host_tag, scsi_state, scsi_status, xfer_count, resp_data);
2722 		if (sense_buf) {
2723 			scsi_normalize_sense(sense_buf, sense_count, &sshdr);
2724 			ioc_info(mrioc,
2725 			    "%s :sense_count 0x%x, sense_key 0x%x ASC 0x%x, ASCQ 0x%x\n",
2726 			    __func__, sense_count, sshdr.sense_key,
2727 			    sshdr.asc, sshdr.ascq);
2728 		}
2729 	}
2730 out_success:
2731 	if (priv->meta_sg_valid) {
2732 		dma_unmap_sg(&mrioc->pdev->dev, scsi_prot_sglist(scmd),
2733 		    scsi_prot_sg_count(scmd), scmd->sc_data_direction);
2734 	}
2735 	mpi3mr_clear_scmd_priv(mrioc, scmd);
2736 	scsi_dma_unmap(scmd);
2737 	scsi_done(scmd);
2738 out:
2739 	if (sense_buf)
2740 		mpi3mr_repost_sense_buf(mrioc,
2741 		    le64_to_cpu(scsi_reply->sense_data_buffer_address));
2742 }
2743 
2744 /**
2745  * mpi3mr_get_chain_idx - get free chain buffer index
2746  * @mrioc: Adapter instance reference
2747  *
2748  * Try to get a free chain buffer index from the free pool.
2749  *
2750  * Return: -1 on failure or the free chain buffer index
2751  */
2752 static int mpi3mr_get_chain_idx(struct mpi3mr_ioc *mrioc)
2753 {
2754 	u8 retry_count = 5;
2755 	int cmd_idx = -1;
2756 
2757 	do {
2758 		spin_lock(&mrioc->chain_buf_lock);
2759 		cmd_idx = find_first_zero_bit(mrioc->chain_bitmap,
2760 		    mrioc->chain_buf_count);
2761 		if (cmd_idx < mrioc->chain_buf_count) {
2762 			set_bit(cmd_idx, mrioc->chain_bitmap);
2763 			spin_unlock(&mrioc->chain_buf_lock);
2764 			break;
2765 		}
2766 		spin_unlock(&mrioc->chain_buf_lock);
2767 		cmd_idx = -1;
2768 	} while (retry_count--);
2769 	return cmd_idx;
2770 }
2771 
2772 /**
2773  * mpi3mr_prepare_sg_scmd - build scatter gather list
2774  * @mrioc: Adapter instance reference
2775  * @scmd: SCSI command reference
2776  * @scsiio_req: MPI3 SCSI IO request
2777  *
2778  * This function maps SCSI command's data and protection SGEs to
2779  * MPI request SGEs. If required additional 4K chain buffer is
2780  * used to send the SGEs.
2781  *
2782  * Return: 0 on success, -ENOMEM on dma_map_sg failure
2783  */
2784 static int mpi3mr_prepare_sg_scmd(struct mpi3mr_ioc *mrioc,
2785 	struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
2786 {
2787 	dma_addr_t chain_dma;
2788 	struct scatterlist *sg_scmd;
2789 	void *sg_local, *chain;
2790 	u32 chain_length;
2791 	int sges_left, chain_idx;
2792 	u32 sges_in_segment;
2793 	u8 simple_sgl_flags;
2794 	u8 simple_sgl_flags_last;
2795 	u8 last_chain_sgl_flags;
2796 	struct chain_element *chain_req;
2797 	struct scmd_priv *priv = NULL;
2798 	u32 meta_sg = le32_to_cpu(scsiio_req->flags) &
2799 	    MPI3_SCSIIO_FLAGS_DMAOPERATION_HOST_PI;
2800 
2801 	priv = scsi_cmd_priv(scmd);
2802 
2803 	simple_sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_SIMPLE |
2804 	    MPI3_SGE_FLAGS_DLAS_SYSTEM;
2805 	simple_sgl_flags_last = simple_sgl_flags |
2806 	    MPI3_SGE_FLAGS_END_OF_LIST;
2807 	last_chain_sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_LAST_CHAIN |
2808 	    MPI3_SGE_FLAGS_DLAS_SYSTEM;
2809 
2810 	if (meta_sg)
2811 		sg_local = &scsiio_req->sgl[MPI3_SCSIIO_METASGL_INDEX];
2812 	else
2813 		sg_local = &scsiio_req->sgl;
2814 
2815 	if (!scsiio_req->data_length && !meta_sg) {
2816 		mpi3mr_build_zero_len_sge(sg_local);
2817 		return 0;
2818 	}
2819 
2820 	if (meta_sg) {
2821 		sg_scmd = scsi_prot_sglist(scmd);
2822 		sges_left = dma_map_sg(&mrioc->pdev->dev,
2823 		    scsi_prot_sglist(scmd),
2824 		    scsi_prot_sg_count(scmd),
2825 		    scmd->sc_data_direction);
2826 		priv->meta_sg_valid = 1; /* To unmap meta sg DMA */
2827 	} else {
2828 		sg_scmd = scsi_sglist(scmd);
2829 		sges_left = scsi_dma_map(scmd);
2830 	}
2831 
2832 	if (sges_left < 0) {
2833 		sdev_printk(KERN_ERR, scmd->device,
2834 		    "scsi_dma_map failed: request for %d bytes!\n",
2835 		    scsi_bufflen(scmd));
2836 		return -ENOMEM;
2837 	}
2838 	if (sges_left > MPI3MR_SG_DEPTH) {
2839 		sdev_printk(KERN_ERR, scmd->device,
2840 		    "scsi_dma_map returned unsupported sge count %d!\n",
2841 		    sges_left);
2842 		return -ENOMEM;
2843 	}
2844 
2845 	sges_in_segment = (mrioc->facts.op_req_sz -
2846 	    offsetof(struct mpi3_scsi_io_request, sgl)) / sizeof(struct mpi3_sge_common);
2847 
2848 	if (scsiio_req->sgl[0].eedp.flags ==
2849 	    MPI3_SGE_FLAGS_ELEMENT_TYPE_EXTENDED && !meta_sg) {
2850 		sg_local += sizeof(struct mpi3_sge_common);
2851 		sges_in_segment--;
2852 		/* Reserve 1st segment (scsiio_req->sgl[0]) for eedp */
2853 	}
2854 
2855 	if (scsiio_req->msg_flags ==
2856 	    MPI3_SCSIIO_MSGFLAGS_METASGL_VALID && !meta_sg) {
2857 		sges_in_segment--;
2858 		/* Reserve last segment (scsiio_req->sgl[3]) for meta sg */
2859 	}
2860 
2861 	if (meta_sg)
2862 		sges_in_segment = 1;
2863 
2864 	if (sges_left <= sges_in_segment)
2865 		goto fill_in_last_segment;
2866 
2867 	/* fill in main message segment when there is a chain following */
2868 	while (sges_in_segment > 1) {
2869 		mpi3mr_add_sg_single(sg_local, simple_sgl_flags,
2870 		    sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2871 		sg_scmd = sg_next(sg_scmd);
2872 		sg_local += sizeof(struct mpi3_sge_common);
2873 		sges_left--;
2874 		sges_in_segment--;
2875 	}
2876 
2877 	chain_idx = mpi3mr_get_chain_idx(mrioc);
2878 	if (chain_idx < 0)
2879 		return -1;
2880 	chain_req = &mrioc->chain_sgl_list[chain_idx];
2881 	if (meta_sg)
2882 		priv->meta_chain_idx = chain_idx;
2883 	else
2884 		priv->chain_idx = chain_idx;
2885 
2886 	chain = chain_req->addr;
2887 	chain_dma = chain_req->dma_addr;
2888 	sges_in_segment = sges_left;
2889 	chain_length = sges_in_segment * sizeof(struct mpi3_sge_common);
2890 
2891 	mpi3mr_add_sg_single(sg_local, last_chain_sgl_flags,
2892 	    chain_length, chain_dma);
2893 
2894 	sg_local = chain;
2895 
2896 fill_in_last_segment:
2897 	while (sges_left > 0) {
2898 		if (sges_left == 1)
2899 			mpi3mr_add_sg_single(sg_local,
2900 			    simple_sgl_flags_last, sg_dma_len(sg_scmd),
2901 			    sg_dma_address(sg_scmd));
2902 		else
2903 			mpi3mr_add_sg_single(sg_local, simple_sgl_flags,
2904 			    sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2905 		sg_scmd = sg_next(sg_scmd);
2906 		sg_local += sizeof(struct mpi3_sge_common);
2907 		sges_left--;
2908 	}
2909 
2910 	return 0;
2911 }
2912 
2913 /**
2914  * mpi3mr_build_sg_scmd - build scatter gather list for SCSI IO
2915  * @mrioc: Adapter instance reference
2916  * @scmd: SCSI command reference
2917  * @scsiio_req: MPI3 SCSI IO request
2918  *
2919  * This function calls mpi3mr_prepare_sg_scmd for constructing
2920  * both data SGEs and protection information SGEs in the MPI
2921  * format from the SCSI Command as appropriate .
2922  *
2923  * Return: return value of mpi3mr_prepare_sg_scmd.
2924  */
2925 static int mpi3mr_build_sg_scmd(struct mpi3mr_ioc *mrioc,
2926 	struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
2927 {
2928 	int ret;
2929 
2930 	ret = mpi3mr_prepare_sg_scmd(mrioc, scmd, scsiio_req);
2931 	if (ret)
2932 		return ret;
2933 
2934 	if (scsiio_req->msg_flags == MPI3_SCSIIO_MSGFLAGS_METASGL_VALID) {
2935 		/* There is a valid meta sg */
2936 		scsiio_req->flags |=
2937 		    cpu_to_le32(MPI3_SCSIIO_FLAGS_DMAOPERATION_HOST_PI);
2938 		ret = mpi3mr_prepare_sg_scmd(mrioc, scmd, scsiio_req);
2939 	}
2940 
2941 	return ret;
2942 }
2943 
2944 /**
2945  * mpi3mr_tm_response_name -  get TM response as a string
2946  * @resp_code: TM response code
2947  *
2948  * Convert known task management response code as a readable
2949  * string.
2950  *
2951  * Return: response code string.
2952  */
2953 static const char *mpi3mr_tm_response_name(u8 resp_code)
2954 {
2955 	char *desc;
2956 
2957 	switch (resp_code) {
2958 	case MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE:
2959 		desc = "task management request completed";
2960 		break;
2961 	case MPI3_SCSITASKMGMT_RSPCODE_INVALID_FRAME:
2962 		desc = "invalid frame";
2963 		break;
2964 	case MPI3_SCSITASKMGMT_RSPCODE_TM_FUNCTION_NOT_SUPPORTED:
2965 		desc = "task management request not supported";
2966 		break;
2967 	case MPI3_SCSITASKMGMT_RSPCODE_TM_FAILED:
2968 		desc = "task management request failed";
2969 		break;
2970 	case MPI3_SCSITASKMGMT_RSPCODE_TM_SUCCEEDED:
2971 		desc = "task management request succeeded";
2972 		break;
2973 	case MPI3_SCSITASKMGMT_RSPCODE_TM_INVALID_LUN:
2974 		desc = "invalid LUN";
2975 		break;
2976 	case MPI3_SCSITASKMGMT_RSPCODE_TM_OVERLAPPED_TAG:
2977 		desc = "overlapped tag attempted";
2978 		break;
2979 	case MPI3_SCSITASKMGMT_RSPCODE_IO_QUEUED_ON_IOC:
2980 		desc = "task queued, however not sent to target";
2981 		break;
2982 	case MPI3_SCSITASKMGMT_RSPCODE_TM_NVME_DENIED:
2983 		desc = "task management request denied by NVMe device";
2984 		break;
2985 	default:
2986 		desc = "unknown";
2987 		break;
2988 	}
2989 
2990 	return desc;
2991 }
2992 
2993 inline void mpi3mr_poll_pend_io_completions(struct mpi3mr_ioc *mrioc)
2994 {
2995 	int i;
2996 	int num_of_reply_queues =
2997 	    mrioc->num_op_reply_q + mrioc->op_reply_q_offset;
2998 
2999 	for (i = mrioc->op_reply_q_offset; i < num_of_reply_queues; i++)
3000 		mpi3mr_process_op_reply_q(mrioc,
3001 		    mrioc->intr_info[i].op_reply_q);
3002 }
3003 
3004 /**
3005  * mpi3mr_issue_tm - Issue Task Management request
3006  * @mrioc: Adapter instance reference
3007  * @tm_type: Task Management type
3008  * @handle: Device handle
3009  * @lun: lun ID
3010  * @htag: Host tag of the TM request
3011  * @timeout: TM timeout value
3012  * @drv_cmd: Internal command tracker
3013  * @resp_code: Response code place holder
3014  * @scmd: SCSI command
3015  *
3016  * Issues a Task Management Request to the controller for a
3017  * specified target, lun and command and wait for its completion
3018  * and check TM response. Recover the TM if it timed out by
3019  * issuing controller reset.
3020  *
3021  * Return: 0 on success, non-zero on errors
3022  */
3023 int mpi3mr_issue_tm(struct mpi3mr_ioc *mrioc, u8 tm_type,
3024 	u16 handle, uint lun, u16 htag, ulong timeout,
3025 	struct mpi3mr_drv_cmd *drv_cmd,
3026 	u8 *resp_code, struct scsi_cmnd *scmd)
3027 {
3028 	struct mpi3_scsi_task_mgmt_request tm_req;
3029 	struct mpi3_scsi_task_mgmt_reply *tm_reply = NULL;
3030 	int retval = 0;
3031 	struct mpi3mr_tgt_dev *tgtdev = NULL;
3032 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
3033 	struct scmd_priv *cmd_priv = NULL;
3034 	struct scsi_device *sdev = NULL;
3035 	struct mpi3mr_sdev_priv_data *sdev_priv_data = NULL;
3036 
3037 	ioc_info(mrioc, "%s :Issue TM: TM type (0x%x) for devhandle 0x%04x\n",
3038 	     __func__, tm_type, handle);
3039 	if (mrioc->unrecoverable) {
3040 		retval = -1;
3041 		ioc_err(mrioc, "%s :Issue TM: Unrecoverable controller\n",
3042 		    __func__);
3043 		goto out;
3044 	}
3045 
3046 	memset(&tm_req, 0, sizeof(tm_req));
3047 	mutex_lock(&drv_cmd->mutex);
3048 	if (drv_cmd->state & MPI3MR_CMD_PENDING) {
3049 		retval = -1;
3050 		ioc_err(mrioc, "%s :Issue TM: Command is in use\n", __func__);
3051 		mutex_unlock(&drv_cmd->mutex);
3052 		goto out;
3053 	}
3054 	if (mrioc->reset_in_progress) {
3055 		retval = -1;
3056 		ioc_err(mrioc, "%s :Issue TM: Reset in progress\n", __func__);
3057 		mutex_unlock(&drv_cmd->mutex);
3058 		goto out;
3059 	}
3060 
3061 	drv_cmd->state = MPI3MR_CMD_PENDING;
3062 	drv_cmd->is_waiting = 1;
3063 	drv_cmd->callback = NULL;
3064 	tm_req.dev_handle = cpu_to_le16(handle);
3065 	tm_req.task_type = tm_type;
3066 	tm_req.host_tag = cpu_to_le16(htag);
3067 
3068 	int_to_scsilun(lun, (struct scsi_lun *)tm_req.lun);
3069 	tm_req.function = MPI3_FUNCTION_SCSI_TASK_MGMT;
3070 
3071 	tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
3072 
3073 	if (scmd) {
3074 		sdev = scmd->device;
3075 		sdev_priv_data = sdev->hostdata;
3076 		scsi_tgt_priv_data = ((sdev_priv_data) ?
3077 		    sdev_priv_data->tgt_priv_data : NULL);
3078 	} else {
3079 		if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
3080 			scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
3081 			    tgtdev->starget->hostdata;
3082 	}
3083 
3084 	if (scsi_tgt_priv_data)
3085 		atomic_inc(&scsi_tgt_priv_data->block_io);
3086 
3087 	if (tgtdev && (tgtdev->dev_type == MPI3_DEVICE_DEVFORM_PCIE)) {
3088 		if (cmd_priv && tgtdev->dev_spec.pcie_inf.abort_to)
3089 			timeout = tgtdev->dev_spec.pcie_inf.abort_to;
3090 		else if (!cmd_priv && tgtdev->dev_spec.pcie_inf.reset_to)
3091 			timeout = tgtdev->dev_spec.pcie_inf.reset_to;
3092 	}
3093 
3094 	init_completion(&drv_cmd->done);
3095 	retval = mpi3mr_admin_request_post(mrioc, &tm_req, sizeof(tm_req), 1);
3096 	if (retval) {
3097 		ioc_err(mrioc, "%s :Issue TM: Admin Post failed\n", __func__);
3098 		goto out_unlock;
3099 	}
3100 	wait_for_completion_timeout(&drv_cmd->done, (timeout * HZ));
3101 
3102 	if (!(drv_cmd->state & MPI3MR_CMD_COMPLETE)) {
3103 		drv_cmd->is_waiting = 0;
3104 		retval = -1;
3105 		if (!(drv_cmd->state & MPI3MR_CMD_RESET)) {
3106 			dprint_tm(mrioc,
3107 			    "task management request timed out after %ld seconds\n",
3108 			    timeout);
3109 			if (mrioc->logging_level & MPI3_DEBUG_TM)
3110 				dprint_dump_req(&tm_req, sizeof(tm_req)/4);
3111 			mpi3mr_soft_reset_handler(mrioc,
3112 			    MPI3MR_RESET_FROM_TM_TIMEOUT, 1);
3113 		}
3114 		goto out_unlock;
3115 	}
3116 
3117 	if (!(drv_cmd->state & MPI3MR_CMD_REPLY_VALID)) {
3118 		dprint_tm(mrioc, "invalid task management reply message\n");
3119 		retval = -1;
3120 		goto out_unlock;
3121 	}
3122 
3123 	tm_reply = (struct mpi3_scsi_task_mgmt_reply *)drv_cmd->reply;
3124 
3125 	switch (drv_cmd->ioc_status) {
3126 	case MPI3_IOCSTATUS_SUCCESS:
3127 		*resp_code = le32_to_cpu(tm_reply->response_data) &
3128 			MPI3MR_RI_MASK_RESPCODE;
3129 		break;
3130 	case MPI3_IOCSTATUS_SCSI_IOC_TERMINATED:
3131 		*resp_code = MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE;
3132 		break;
3133 	default:
3134 		dprint_tm(mrioc,
3135 		    "task management request to handle(0x%04x) is failed with ioc_status(0x%04x) log_info(0x%08x)\n",
3136 		    handle, drv_cmd->ioc_status, drv_cmd->ioc_loginfo);
3137 		retval = -1;
3138 		goto out_unlock;
3139 	}
3140 
3141 	switch (*resp_code) {
3142 	case MPI3_SCSITASKMGMT_RSPCODE_TM_SUCCEEDED:
3143 	case MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE:
3144 		break;
3145 	case MPI3_SCSITASKMGMT_RSPCODE_IO_QUEUED_ON_IOC:
3146 		if (tm_type != MPI3_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
3147 			retval = -1;
3148 		break;
3149 	default:
3150 		retval = -1;
3151 		break;
3152 	}
3153 
3154 	dprint_tm(mrioc,
3155 	    "task management request type(%d) completed for handle(0x%04x) with ioc_status(0x%04x), log_info(0x%08x), termination_count(%d), response:%s(0x%x)\n",
3156 	    tm_type, handle, drv_cmd->ioc_status, drv_cmd->ioc_loginfo,
3157 	    le32_to_cpu(tm_reply->termination_count),
3158 	    mpi3mr_tm_response_name(*resp_code), *resp_code);
3159 
3160 	if (!retval) {
3161 		mpi3mr_ioc_disable_intr(mrioc);
3162 		mpi3mr_poll_pend_io_completions(mrioc);
3163 		mpi3mr_ioc_enable_intr(mrioc);
3164 		mpi3mr_poll_pend_io_completions(mrioc);
3165 	}
3166 	switch (tm_type) {
3167 	case MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
3168 		if (!scsi_tgt_priv_data)
3169 			break;
3170 		scsi_tgt_priv_data->pend_count = 0;
3171 		blk_mq_tagset_busy_iter(&mrioc->shost->tag_set,
3172 		    mpi3mr_count_tgt_pending,
3173 		    (void *)scsi_tgt_priv_data->starget);
3174 		break;
3175 	case MPI3_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
3176 		if (!sdev_priv_data)
3177 			break;
3178 		sdev_priv_data->pend_count = 0;
3179 		blk_mq_tagset_busy_iter(&mrioc->shost->tag_set,
3180 		    mpi3mr_count_dev_pending, (void *)sdev);
3181 		break;
3182 	default:
3183 		break;
3184 	}
3185 
3186 out_unlock:
3187 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
3188 	mutex_unlock(&drv_cmd->mutex);
3189 	if (scsi_tgt_priv_data)
3190 		atomic_dec_if_positive(&scsi_tgt_priv_data->block_io);
3191 	if (tgtdev)
3192 		mpi3mr_tgtdev_put(tgtdev);
3193 out:
3194 	return retval;
3195 }
3196 
3197 /**
3198  * mpi3mr_bios_param - BIOS param callback
3199  * @sdev: SCSI device reference
3200  * @bdev: Block device reference
3201  * @capacity: Capacity in logical sectors
3202  * @params: Parameter array
3203  *
3204  * Just the parameters with heads/secots/cylinders.
3205  *
3206  * Return: 0 always
3207  */
3208 static int mpi3mr_bios_param(struct scsi_device *sdev,
3209 	struct block_device *bdev, sector_t capacity, int params[])
3210 {
3211 	int heads;
3212 	int sectors;
3213 	sector_t cylinders;
3214 	ulong dummy;
3215 
3216 	heads = 64;
3217 	sectors = 32;
3218 
3219 	dummy = heads * sectors;
3220 	cylinders = capacity;
3221 	sector_div(cylinders, dummy);
3222 
3223 	if ((ulong)capacity >= 0x200000) {
3224 		heads = 255;
3225 		sectors = 63;
3226 		dummy = heads * sectors;
3227 		cylinders = capacity;
3228 		sector_div(cylinders, dummy);
3229 	}
3230 
3231 	params[0] = heads;
3232 	params[1] = sectors;
3233 	params[2] = cylinders;
3234 	return 0;
3235 }
3236 
3237 /**
3238  * mpi3mr_map_queues - Map queues callback handler
3239  * @shost: SCSI host reference
3240  *
3241  * Maps default and poll queues.
3242  *
3243  * Return: return zero.
3244  */
3245 static int mpi3mr_map_queues(struct Scsi_Host *shost)
3246 {
3247 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
3248 	int i, qoff, offset;
3249 	struct blk_mq_queue_map *map = NULL;
3250 
3251 	offset = mrioc->op_reply_q_offset;
3252 
3253 	for (i = 0, qoff = 0; i < HCTX_MAX_TYPES; i++) {
3254 		map = &shost->tag_set.map[i];
3255 
3256 		map->nr_queues  = 0;
3257 
3258 		if (i == HCTX_TYPE_DEFAULT)
3259 			map->nr_queues = mrioc->default_qcount;
3260 		else if (i == HCTX_TYPE_POLL)
3261 			map->nr_queues = mrioc->active_poll_qcount;
3262 
3263 		if (!map->nr_queues) {
3264 			BUG_ON(i == HCTX_TYPE_DEFAULT);
3265 			continue;
3266 		}
3267 
3268 		/*
3269 		 * The poll queue(s) doesn't have an IRQ (and hence IRQ
3270 		 * affinity), so use the regular blk-mq cpu mapping
3271 		 */
3272 		map->queue_offset = qoff;
3273 		if (i != HCTX_TYPE_POLL)
3274 			blk_mq_pci_map_queues(map, mrioc->pdev, offset);
3275 		else
3276 			blk_mq_map_queues(map);
3277 
3278 		qoff += map->nr_queues;
3279 		offset += map->nr_queues;
3280 	}
3281 
3282 	return 0;
3283 
3284 }
3285 
3286 /**
3287  * mpi3mr_get_fw_pending_ios - Calculate pending I/O count
3288  * @mrioc: Adapter instance reference
3289  *
3290  * Calculate the pending I/Os for the controller and return.
3291  *
3292  * Return: Number of pending I/Os
3293  */
3294 static inline int mpi3mr_get_fw_pending_ios(struct mpi3mr_ioc *mrioc)
3295 {
3296 	u16 i;
3297 	uint pend_ios = 0;
3298 
3299 	for (i = 0; i < mrioc->num_op_reply_q; i++)
3300 		pend_ios += atomic_read(&mrioc->op_reply_qinfo[i].pend_ios);
3301 	return pend_ios;
3302 }
3303 
3304 /**
3305  * mpi3mr_print_pending_host_io - print pending I/Os
3306  * @mrioc: Adapter instance reference
3307  *
3308  * Print number of pending I/Os and each I/O details prior to
3309  * reset for debug purpose.
3310  *
3311  * Return: Nothing
3312  */
3313 static void mpi3mr_print_pending_host_io(struct mpi3mr_ioc *mrioc)
3314 {
3315 	struct Scsi_Host *shost = mrioc->shost;
3316 
3317 	ioc_info(mrioc, "%s :Pending commands prior to reset: %d\n",
3318 	    __func__, mpi3mr_get_fw_pending_ios(mrioc));
3319 	blk_mq_tagset_busy_iter(&shost->tag_set,
3320 	    mpi3mr_print_scmd, (void *)mrioc);
3321 }
3322 
3323 /**
3324  * mpi3mr_wait_for_host_io - block for I/Os to complete
3325  * @mrioc: Adapter instance reference
3326  * @timeout: time out in seconds
3327  * Waits for pending I/Os for the given adapter to complete or
3328  * to hit the timeout.
3329  *
3330  * Return: Nothing
3331  */
3332 void mpi3mr_wait_for_host_io(struct mpi3mr_ioc *mrioc, u32 timeout)
3333 {
3334 	enum mpi3mr_iocstate iocstate;
3335 	int i = 0;
3336 
3337 	iocstate = mpi3mr_get_iocstate(mrioc);
3338 	if (iocstate != MRIOC_STATE_READY)
3339 		return;
3340 
3341 	if (!mpi3mr_get_fw_pending_ios(mrioc))
3342 		return;
3343 	ioc_info(mrioc,
3344 	    "%s :Waiting for %d seconds prior to reset for %d I/O\n",
3345 	    __func__, timeout, mpi3mr_get_fw_pending_ios(mrioc));
3346 
3347 	for (i = 0; i < timeout; i++) {
3348 		if (!mpi3mr_get_fw_pending_ios(mrioc))
3349 			break;
3350 		iocstate = mpi3mr_get_iocstate(mrioc);
3351 		if (iocstate != MRIOC_STATE_READY)
3352 			break;
3353 		msleep(1000);
3354 	}
3355 
3356 	ioc_info(mrioc, "%s :Pending I/Os after wait is: %d\n", __func__,
3357 	    mpi3mr_get_fw_pending_ios(mrioc));
3358 }
3359 
3360 /**
3361  * mpi3mr_eh_host_reset - Host reset error handling callback
3362  * @scmd: SCSI command reference
3363  *
3364  * Issue controller reset if the scmd is for a Physical Device,
3365  * if the scmd is for RAID volume, then wait for
3366  * MPI3MR_RAID_ERRREC_RESET_TIMEOUT and checke whether any
3367  * pending I/Os prior to issuing reset to the controller.
3368  *
3369  * Return: SUCCESS of successful reset else FAILED
3370  */
3371 static int mpi3mr_eh_host_reset(struct scsi_cmnd *scmd)
3372 {
3373 	struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
3374 	struct mpi3mr_stgt_priv_data *stgt_priv_data;
3375 	struct mpi3mr_sdev_priv_data *sdev_priv_data;
3376 	u8 dev_type = MPI3_DEVICE_DEVFORM_VD;
3377 	int retval = FAILED, ret;
3378 
3379 	sdev_priv_data = scmd->device->hostdata;
3380 	if (sdev_priv_data && sdev_priv_data->tgt_priv_data) {
3381 		stgt_priv_data = sdev_priv_data->tgt_priv_data;
3382 		dev_type = stgt_priv_data->dev_type;
3383 	}
3384 
3385 	if (dev_type == MPI3_DEVICE_DEVFORM_VD) {
3386 		mpi3mr_wait_for_host_io(mrioc,
3387 		    MPI3MR_RAID_ERRREC_RESET_TIMEOUT);
3388 		if (!mpi3mr_get_fw_pending_ios(mrioc)) {
3389 			retval = SUCCESS;
3390 			goto out;
3391 		}
3392 	}
3393 
3394 	mpi3mr_print_pending_host_io(mrioc);
3395 	ret = mpi3mr_soft_reset_handler(mrioc,
3396 	    MPI3MR_RESET_FROM_EH_HOS, 1);
3397 	if (ret)
3398 		goto out;
3399 
3400 	retval = SUCCESS;
3401 out:
3402 	sdev_printk(KERN_INFO, scmd->device,
3403 	    "Host reset is %s for scmd(%p)\n",
3404 	    ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
3405 
3406 	return retval;
3407 }
3408 
3409 /**
3410  * mpi3mr_eh_target_reset - Target reset error handling callback
3411  * @scmd: SCSI command reference
3412  *
3413  * Issue Target reset Task Management and verify the scmd is
3414  * terminated successfully and return status accordingly.
3415  *
3416  * Return: SUCCESS of successful termination of the scmd else
3417  *         FAILED
3418  */
3419 static int mpi3mr_eh_target_reset(struct scsi_cmnd *scmd)
3420 {
3421 	struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
3422 	struct mpi3mr_stgt_priv_data *stgt_priv_data;
3423 	struct mpi3mr_sdev_priv_data *sdev_priv_data;
3424 	u16 dev_handle;
3425 	u8 resp_code = 0;
3426 	int retval = FAILED, ret = 0;
3427 
3428 	sdev_printk(KERN_INFO, scmd->device,
3429 	    "Attempting Target Reset! scmd(%p)\n", scmd);
3430 	scsi_print_command(scmd);
3431 
3432 	sdev_priv_data = scmd->device->hostdata;
3433 	if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
3434 		sdev_printk(KERN_INFO, scmd->device,
3435 		    "SCSI device is not available\n");
3436 		retval = SUCCESS;
3437 		goto out;
3438 	}
3439 
3440 	stgt_priv_data = sdev_priv_data->tgt_priv_data;
3441 	dev_handle = stgt_priv_data->dev_handle;
3442 	if (stgt_priv_data->dev_removed) {
3443 		sdev_printk(KERN_INFO, scmd->device,
3444 		    "%s:target(handle = 0x%04x) is removed, target reset is not issued\n",
3445 		    mrioc->name, dev_handle);
3446 		retval = FAILED;
3447 		goto out;
3448 	}
3449 	sdev_printk(KERN_INFO, scmd->device,
3450 	    "Target Reset is issued to handle(0x%04x)\n",
3451 	    dev_handle);
3452 
3453 	ret = mpi3mr_issue_tm(mrioc,
3454 	    MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET, dev_handle,
3455 	    sdev_priv_data->lun_id, MPI3MR_HOSTTAG_BLK_TMS,
3456 	    MPI3MR_RESETTM_TIMEOUT, &mrioc->host_tm_cmds, &resp_code, scmd);
3457 
3458 	if (ret)
3459 		goto out;
3460 
3461 	if (stgt_priv_data->pend_count) {
3462 		sdev_printk(KERN_INFO, scmd->device,
3463 		    "%s: target has %d pending commands, target reset is failed\n",
3464 		    mrioc->name, stgt_priv_data->pend_count);
3465 		goto out;
3466 	}
3467 
3468 	retval = SUCCESS;
3469 out:
3470 	sdev_printk(KERN_INFO, scmd->device,
3471 	    "%s: target reset is %s for scmd(%p)\n", mrioc->name,
3472 	    ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
3473 
3474 	return retval;
3475 }
3476 
3477 /**
3478  * mpi3mr_eh_dev_reset- Device reset error handling callback
3479  * @scmd: SCSI command reference
3480  *
3481  * Issue lun reset Task Management and verify the scmd is
3482  * terminated successfully and return status accordingly.
3483  *
3484  * Return: SUCCESS of successful termination of the scmd else
3485  *         FAILED
3486  */
3487 static int mpi3mr_eh_dev_reset(struct scsi_cmnd *scmd)
3488 {
3489 	struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
3490 	struct mpi3mr_stgt_priv_data *stgt_priv_data;
3491 	struct mpi3mr_sdev_priv_data *sdev_priv_data;
3492 	u16 dev_handle;
3493 	u8 resp_code = 0;
3494 	int retval = FAILED, ret = 0;
3495 
3496 	sdev_printk(KERN_INFO, scmd->device,
3497 	    "Attempting Device(lun) Reset! scmd(%p)\n", scmd);
3498 	scsi_print_command(scmd);
3499 
3500 	sdev_priv_data = scmd->device->hostdata;
3501 	if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
3502 		sdev_printk(KERN_INFO, scmd->device,
3503 		    "SCSI device is not available\n");
3504 		retval = SUCCESS;
3505 		goto out;
3506 	}
3507 
3508 	stgt_priv_data = sdev_priv_data->tgt_priv_data;
3509 	dev_handle = stgt_priv_data->dev_handle;
3510 	if (stgt_priv_data->dev_removed) {
3511 		sdev_printk(KERN_INFO, scmd->device,
3512 		    "%s: device(handle = 0x%04x) is removed, device(LUN) reset is not issued\n",
3513 		    mrioc->name, dev_handle);
3514 		retval = FAILED;
3515 		goto out;
3516 	}
3517 	sdev_printk(KERN_INFO, scmd->device,
3518 	    "Device(lun) Reset is issued to handle(0x%04x)\n", dev_handle);
3519 
3520 	ret = mpi3mr_issue_tm(mrioc,
3521 	    MPI3_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, dev_handle,
3522 	    sdev_priv_data->lun_id, MPI3MR_HOSTTAG_BLK_TMS,
3523 	    MPI3MR_RESETTM_TIMEOUT, &mrioc->host_tm_cmds, &resp_code, scmd);
3524 
3525 	if (ret)
3526 		goto out;
3527 
3528 	if (sdev_priv_data->pend_count) {
3529 		sdev_printk(KERN_INFO, scmd->device,
3530 		    "%s: device has %d pending commands, device(LUN) reset is failed\n",
3531 		    mrioc->name, sdev_priv_data->pend_count);
3532 		goto out;
3533 	}
3534 	retval = SUCCESS;
3535 out:
3536 	sdev_printk(KERN_INFO, scmd->device,
3537 	    "%s: device(LUN) reset is %s for scmd(%p)\n", mrioc->name,
3538 	    ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
3539 
3540 	return retval;
3541 }
3542 
3543 /**
3544  * mpi3mr_scan_start - Scan start callback handler
3545  * @shost: SCSI host reference
3546  *
3547  * Issue port enable request asynchronously.
3548  *
3549  * Return: Nothing
3550  */
3551 static void mpi3mr_scan_start(struct Scsi_Host *shost)
3552 {
3553 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
3554 
3555 	mrioc->scan_started = 1;
3556 	ioc_info(mrioc, "%s :Issuing Port Enable\n", __func__);
3557 	if (mpi3mr_issue_port_enable(mrioc, 1)) {
3558 		ioc_err(mrioc, "%s :Issuing port enable failed\n", __func__);
3559 		mrioc->scan_started = 0;
3560 		mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
3561 	}
3562 }
3563 
3564 /**
3565  * mpi3mr_scan_finished - Scan finished callback handler
3566  * @shost: SCSI host reference
3567  * @time: Jiffies from the scan start
3568  *
3569  * Checks whether the port enable is completed or timedout or
3570  * failed and set the scan status accordingly after taking any
3571  * recovery if required.
3572  *
3573  * Return: 1 on scan finished or timed out, 0 for in progress
3574  */
3575 static int mpi3mr_scan_finished(struct Scsi_Host *shost,
3576 	unsigned long time)
3577 {
3578 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
3579 	u32 pe_timeout = MPI3MR_PORTENABLE_TIMEOUT;
3580 	u32 ioc_status = readl(&mrioc->sysif_regs->ioc_status);
3581 
3582 	if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) ||
3583 	    (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)) {
3584 		ioc_err(mrioc, "port enable failed due to fault or reset\n");
3585 		mpi3mr_print_fault_info(mrioc);
3586 		mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
3587 		mrioc->scan_started = 0;
3588 		mrioc->init_cmds.is_waiting = 0;
3589 		mrioc->init_cmds.callback = NULL;
3590 		mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
3591 	}
3592 
3593 	if (time >= (pe_timeout * HZ)) {
3594 		ioc_err(mrioc, "port enable failed due to time out\n");
3595 		mpi3mr_check_rh_fault_ioc(mrioc,
3596 		    MPI3MR_RESET_FROM_PE_TIMEOUT);
3597 		mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
3598 		mrioc->scan_started = 0;
3599 		mrioc->init_cmds.is_waiting = 0;
3600 		mrioc->init_cmds.callback = NULL;
3601 		mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
3602 	}
3603 
3604 	if (mrioc->scan_started)
3605 		return 0;
3606 
3607 	if (mrioc->scan_failed) {
3608 		ioc_err(mrioc,
3609 		    "port enable failed with status=0x%04x\n",
3610 		    mrioc->scan_failed);
3611 	} else
3612 		ioc_info(mrioc, "port enable is successfully completed\n");
3613 
3614 	mpi3mr_start_watchdog(mrioc);
3615 	mrioc->is_driver_loading = 0;
3616 	mrioc->stop_bsgs = 0;
3617 	return 1;
3618 }
3619 
3620 /**
3621  * mpi3mr_slave_destroy - Slave destroy callback handler
3622  * @sdev: SCSI device reference
3623  *
3624  * Cleanup and free per device(lun) private data.
3625  *
3626  * Return: Nothing.
3627  */
3628 static void mpi3mr_slave_destroy(struct scsi_device *sdev)
3629 {
3630 	struct Scsi_Host *shost;
3631 	struct mpi3mr_ioc *mrioc;
3632 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
3633 	struct mpi3mr_tgt_dev *tgt_dev;
3634 	unsigned long flags;
3635 	struct scsi_target *starget;
3636 
3637 	if (!sdev->hostdata)
3638 		return;
3639 
3640 	starget = scsi_target(sdev);
3641 	shost = dev_to_shost(&starget->dev);
3642 	mrioc = shost_priv(shost);
3643 	scsi_tgt_priv_data = starget->hostdata;
3644 
3645 	scsi_tgt_priv_data->num_luns--;
3646 
3647 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
3648 	tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
3649 	if (tgt_dev && (!scsi_tgt_priv_data->num_luns))
3650 		tgt_dev->starget = NULL;
3651 	if (tgt_dev)
3652 		mpi3mr_tgtdev_put(tgt_dev);
3653 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
3654 
3655 	kfree(sdev->hostdata);
3656 	sdev->hostdata = NULL;
3657 }
3658 
3659 /**
3660  * mpi3mr_target_destroy - Target destroy callback handler
3661  * @starget: SCSI target reference
3662  *
3663  * Cleanup and free per target private data.
3664  *
3665  * Return: Nothing.
3666  */
3667 static void mpi3mr_target_destroy(struct scsi_target *starget)
3668 {
3669 	struct Scsi_Host *shost;
3670 	struct mpi3mr_ioc *mrioc;
3671 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
3672 	struct mpi3mr_tgt_dev *tgt_dev;
3673 	unsigned long flags;
3674 
3675 	if (!starget->hostdata)
3676 		return;
3677 
3678 	shost = dev_to_shost(&starget->dev);
3679 	mrioc = shost_priv(shost);
3680 	scsi_tgt_priv_data = starget->hostdata;
3681 
3682 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
3683 	tgt_dev = __mpi3mr_get_tgtdev_from_tgtpriv(mrioc, scsi_tgt_priv_data);
3684 	if (tgt_dev && (tgt_dev->starget == starget) &&
3685 	    (tgt_dev->perst_id == starget->id))
3686 		tgt_dev->starget = NULL;
3687 	if (tgt_dev) {
3688 		scsi_tgt_priv_data->tgt_dev = NULL;
3689 		scsi_tgt_priv_data->perst_id = 0;
3690 		mpi3mr_tgtdev_put(tgt_dev);
3691 		mpi3mr_tgtdev_put(tgt_dev);
3692 	}
3693 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
3694 
3695 	kfree(starget->hostdata);
3696 	starget->hostdata = NULL;
3697 }
3698 
3699 /**
3700  * mpi3mr_slave_configure - Slave configure callback handler
3701  * @sdev: SCSI device reference
3702  *
3703  * Configure queue depth, max hardware sectors and virt boundary
3704  * as required
3705  *
3706  * Return: 0 always.
3707  */
3708 static int mpi3mr_slave_configure(struct scsi_device *sdev)
3709 {
3710 	struct scsi_target *starget;
3711 	struct Scsi_Host *shost;
3712 	struct mpi3mr_ioc *mrioc;
3713 	struct mpi3mr_tgt_dev *tgt_dev;
3714 	unsigned long flags;
3715 	int retval = 0;
3716 
3717 	starget = scsi_target(sdev);
3718 	shost = dev_to_shost(&starget->dev);
3719 	mrioc = shost_priv(shost);
3720 
3721 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
3722 	tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
3723 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
3724 	if (!tgt_dev)
3725 		return -ENXIO;
3726 
3727 	mpi3mr_change_queue_depth(sdev, tgt_dev->q_depth);
3728 
3729 	sdev->eh_timeout = MPI3MR_EH_SCMD_TIMEOUT;
3730 	blk_queue_rq_timeout(sdev->request_queue, MPI3MR_SCMD_TIMEOUT);
3731 
3732 	switch (tgt_dev->dev_type) {
3733 	case MPI3_DEVICE_DEVFORM_PCIE:
3734 		/*The block layer hw sector size = 512*/
3735 		if ((tgt_dev->dev_spec.pcie_inf.dev_info &
3736 		    MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) ==
3737 		    MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) {
3738 			blk_queue_max_hw_sectors(sdev->request_queue,
3739 			    tgt_dev->dev_spec.pcie_inf.mdts / 512);
3740 			if (tgt_dev->dev_spec.pcie_inf.pgsz == 0)
3741 				blk_queue_virt_boundary(sdev->request_queue,
3742 				    ((1 << MPI3MR_DEFAULT_PGSZEXP) - 1));
3743 			else
3744 				blk_queue_virt_boundary(sdev->request_queue,
3745 				    ((1 << tgt_dev->dev_spec.pcie_inf.pgsz) - 1));
3746 		}
3747 		break;
3748 	default:
3749 		break;
3750 	}
3751 
3752 	mpi3mr_tgtdev_put(tgt_dev);
3753 
3754 	return retval;
3755 }
3756 
3757 /**
3758  * mpi3mr_slave_alloc -Slave alloc callback handler
3759  * @sdev: SCSI device reference
3760  *
3761  * Allocate per device(lun) private data and initialize it.
3762  *
3763  * Return: 0 on success -ENOMEM on memory allocation failure.
3764  */
3765 static int mpi3mr_slave_alloc(struct scsi_device *sdev)
3766 {
3767 	struct Scsi_Host *shost;
3768 	struct mpi3mr_ioc *mrioc;
3769 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
3770 	struct mpi3mr_tgt_dev *tgt_dev;
3771 	struct mpi3mr_sdev_priv_data *scsi_dev_priv_data;
3772 	unsigned long flags;
3773 	struct scsi_target *starget;
3774 	int retval = 0;
3775 
3776 	starget = scsi_target(sdev);
3777 	shost = dev_to_shost(&starget->dev);
3778 	mrioc = shost_priv(shost);
3779 	scsi_tgt_priv_data = starget->hostdata;
3780 
3781 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
3782 	tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
3783 
3784 	if (tgt_dev) {
3785 		if (tgt_dev->starget == NULL)
3786 			tgt_dev->starget = starget;
3787 		mpi3mr_tgtdev_put(tgt_dev);
3788 		retval = 0;
3789 	} else {
3790 		spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
3791 		return -ENXIO;
3792 	}
3793 
3794 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
3795 
3796 	scsi_dev_priv_data = kzalloc(sizeof(*scsi_dev_priv_data), GFP_KERNEL);
3797 	if (!scsi_dev_priv_data)
3798 		return -ENOMEM;
3799 
3800 	scsi_dev_priv_data->lun_id = sdev->lun;
3801 	scsi_dev_priv_data->tgt_priv_data = scsi_tgt_priv_data;
3802 	sdev->hostdata = scsi_dev_priv_data;
3803 
3804 	scsi_tgt_priv_data->num_luns++;
3805 
3806 	return retval;
3807 }
3808 
3809 /**
3810  * mpi3mr_target_alloc - Target alloc callback handler
3811  * @starget: SCSI target reference
3812  *
3813  * Allocate per target private data and initialize it.
3814  *
3815  * Return: 0 on success -ENOMEM on memory allocation failure.
3816  */
3817 static int mpi3mr_target_alloc(struct scsi_target *starget)
3818 {
3819 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
3820 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
3821 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
3822 	struct mpi3mr_tgt_dev *tgt_dev;
3823 	unsigned long flags;
3824 	int retval = 0;
3825 
3826 	scsi_tgt_priv_data = kzalloc(sizeof(*scsi_tgt_priv_data), GFP_KERNEL);
3827 	if (!scsi_tgt_priv_data)
3828 		return -ENOMEM;
3829 
3830 	starget->hostdata = scsi_tgt_priv_data;
3831 
3832 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
3833 	tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
3834 	if (tgt_dev && !tgt_dev->is_hidden) {
3835 		scsi_tgt_priv_data->starget = starget;
3836 		scsi_tgt_priv_data->dev_handle = tgt_dev->dev_handle;
3837 		scsi_tgt_priv_data->perst_id = tgt_dev->perst_id;
3838 		scsi_tgt_priv_data->dev_type = tgt_dev->dev_type;
3839 		scsi_tgt_priv_data->tgt_dev = tgt_dev;
3840 		tgt_dev->starget = starget;
3841 		atomic_set(&scsi_tgt_priv_data->block_io, 0);
3842 		retval = 0;
3843 	} else
3844 		retval = -ENXIO;
3845 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
3846 
3847 	return retval;
3848 }
3849 
3850 /**
3851  * mpi3mr_check_return_unmap - Whether an unmap is allowed
3852  * @mrioc: Adapter instance reference
3853  * @scmd: SCSI Command reference
3854  *
3855  * The controller hardware cannot handle certain unmap commands
3856  * for NVMe drives, this routine checks those and return true
3857  * and completes the SCSI command with proper status and sense
3858  * data.
3859  *
3860  * Return: TRUE for not  allowed unmap, FALSE otherwise.
3861  */
3862 static bool mpi3mr_check_return_unmap(struct mpi3mr_ioc *mrioc,
3863 	struct scsi_cmnd *scmd)
3864 {
3865 	unsigned char *buf;
3866 	u16 param_len, desc_len, trunc_param_len;
3867 
3868 	trunc_param_len = param_len = get_unaligned_be16(scmd->cmnd + 7);
3869 
3870 	if (mrioc->pdev->revision) {
3871 		if ((param_len > 24) && ((param_len - 8) & 0xF)) {
3872 			trunc_param_len -= (param_len - 8) & 0xF;
3873 			dprint_scsi_command(mrioc, scmd, MPI3_DEBUG_SCSI_ERROR);
3874 			dprint_scsi_err(mrioc,
3875 			    "truncating param_len from (%d) to (%d)\n",
3876 			    param_len, trunc_param_len);
3877 			put_unaligned_be16(trunc_param_len, scmd->cmnd + 7);
3878 			dprint_scsi_command(mrioc, scmd, MPI3_DEBUG_SCSI_ERROR);
3879 		}
3880 		return false;
3881 	}
3882 
3883 	if (!param_len) {
3884 		ioc_warn(mrioc,
3885 		    "%s: cdb received with zero parameter length\n",
3886 		    __func__);
3887 		scsi_print_command(scmd);
3888 		scmd->result = DID_OK << 16;
3889 		scsi_done(scmd);
3890 		return true;
3891 	}
3892 
3893 	if (param_len < 24) {
3894 		ioc_warn(mrioc,
3895 		    "%s: cdb received with invalid param_len: %d\n",
3896 		    __func__, param_len);
3897 		scsi_print_command(scmd);
3898 		scmd->result = SAM_STAT_CHECK_CONDITION;
3899 		scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
3900 		    0x1A, 0);
3901 		scsi_done(scmd);
3902 		return true;
3903 	}
3904 	if (param_len != scsi_bufflen(scmd)) {
3905 		ioc_warn(mrioc,
3906 		    "%s: cdb received with param_len: %d bufflen: %d\n",
3907 		    __func__, param_len, scsi_bufflen(scmd));
3908 		scsi_print_command(scmd);
3909 		scmd->result = SAM_STAT_CHECK_CONDITION;
3910 		scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
3911 		    0x1A, 0);
3912 		scsi_done(scmd);
3913 		return true;
3914 	}
3915 	buf = kzalloc(scsi_bufflen(scmd), GFP_ATOMIC);
3916 	if (!buf) {
3917 		scsi_print_command(scmd);
3918 		scmd->result = SAM_STAT_CHECK_CONDITION;
3919 		scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
3920 		    0x55, 0x03);
3921 		scsi_done(scmd);
3922 		return true;
3923 	}
3924 	scsi_sg_copy_to_buffer(scmd, buf, scsi_bufflen(scmd));
3925 	desc_len = get_unaligned_be16(&buf[2]);
3926 
3927 	if (desc_len < 16) {
3928 		ioc_warn(mrioc,
3929 		    "%s: Invalid descriptor length in param list: %d\n",
3930 		    __func__, desc_len);
3931 		scsi_print_command(scmd);
3932 		scmd->result = SAM_STAT_CHECK_CONDITION;
3933 		scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
3934 		    0x26, 0);
3935 		scsi_done(scmd);
3936 		kfree(buf);
3937 		return true;
3938 	}
3939 
3940 	if (param_len > (desc_len + 8)) {
3941 		trunc_param_len = desc_len + 8;
3942 		scsi_print_command(scmd);
3943 		dprint_scsi_err(mrioc,
3944 		    "truncating param_len(%d) to desc_len+8(%d)\n",
3945 		    param_len, trunc_param_len);
3946 		put_unaligned_be16(trunc_param_len, scmd->cmnd + 7);
3947 		scsi_print_command(scmd);
3948 	}
3949 
3950 	kfree(buf);
3951 	return false;
3952 }
3953 
3954 /**
3955  * mpi3mr_allow_scmd_to_fw - Command is allowed during shutdown
3956  * @scmd: SCSI Command reference
3957  *
3958  * Checks whether a cdb is allowed during shutdown or not.
3959  *
3960  * Return: TRUE for allowed commands, FALSE otherwise.
3961  */
3962 
3963 inline bool mpi3mr_allow_scmd_to_fw(struct scsi_cmnd *scmd)
3964 {
3965 	switch (scmd->cmnd[0]) {
3966 	case SYNCHRONIZE_CACHE:
3967 	case START_STOP:
3968 		return true;
3969 	default:
3970 		return false;
3971 	}
3972 }
3973 
3974 /**
3975  * mpi3mr_qcmd - I/O request despatcher
3976  * @shost: SCSI Host reference
3977  * @scmd: SCSI Command reference
3978  *
3979  * Issues the SCSI Command as an MPI3 request.
3980  *
3981  * Return: 0 on successful queueing of the request or if the
3982  *         request is completed with failure.
3983  *         SCSI_MLQUEUE_DEVICE_BUSY when the device is busy.
3984  *         SCSI_MLQUEUE_HOST_BUSY when the host queue is full.
3985  */
3986 static int mpi3mr_qcmd(struct Scsi_Host *shost,
3987 	struct scsi_cmnd *scmd)
3988 {
3989 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
3990 	struct mpi3mr_stgt_priv_data *stgt_priv_data;
3991 	struct mpi3mr_sdev_priv_data *sdev_priv_data;
3992 	struct scmd_priv *scmd_priv_data = NULL;
3993 	struct mpi3_scsi_io_request *scsiio_req = NULL;
3994 	struct op_req_qinfo *op_req_q = NULL;
3995 	int retval = 0;
3996 	u16 dev_handle;
3997 	u16 host_tag;
3998 	u32 scsiio_flags = 0;
3999 	struct request *rq = scsi_cmd_to_rq(scmd);
4000 	int iprio_class;
4001 	u8 is_pcie_dev = 0;
4002 
4003 	sdev_priv_data = scmd->device->hostdata;
4004 	if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
4005 		scmd->result = DID_NO_CONNECT << 16;
4006 		scsi_done(scmd);
4007 		goto out;
4008 	}
4009 
4010 	if (mrioc->stop_drv_processing &&
4011 	    !(mpi3mr_allow_scmd_to_fw(scmd))) {
4012 		scmd->result = DID_NO_CONNECT << 16;
4013 		scsi_done(scmd);
4014 		goto out;
4015 	}
4016 
4017 	if (mrioc->reset_in_progress) {
4018 		retval = SCSI_MLQUEUE_HOST_BUSY;
4019 		goto out;
4020 	}
4021 
4022 	stgt_priv_data = sdev_priv_data->tgt_priv_data;
4023 
4024 	dev_handle = stgt_priv_data->dev_handle;
4025 	if (dev_handle == MPI3MR_INVALID_DEV_HANDLE) {
4026 		scmd->result = DID_NO_CONNECT << 16;
4027 		scsi_done(scmd);
4028 		goto out;
4029 	}
4030 	if (stgt_priv_data->dev_removed) {
4031 		scmd->result = DID_NO_CONNECT << 16;
4032 		scsi_done(scmd);
4033 		goto out;
4034 	}
4035 
4036 	if (atomic_read(&stgt_priv_data->block_io)) {
4037 		if (mrioc->stop_drv_processing) {
4038 			scmd->result = DID_NO_CONNECT << 16;
4039 			scsi_done(scmd);
4040 			goto out;
4041 		}
4042 		retval = SCSI_MLQUEUE_DEVICE_BUSY;
4043 		goto out;
4044 	}
4045 
4046 	if (stgt_priv_data->dev_type == MPI3_DEVICE_DEVFORM_PCIE)
4047 		is_pcie_dev = 1;
4048 	if ((scmd->cmnd[0] == UNMAP) && is_pcie_dev &&
4049 	    (mrioc->pdev->device == MPI3_MFGPAGE_DEVID_SAS4116) &&
4050 	    mpi3mr_check_return_unmap(mrioc, scmd))
4051 		goto out;
4052 
4053 	host_tag = mpi3mr_host_tag_for_scmd(mrioc, scmd);
4054 	if (host_tag == MPI3MR_HOSTTAG_INVALID) {
4055 		scmd->result = DID_ERROR << 16;
4056 		scsi_done(scmd);
4057 		goto out;
4058 	}
4059 
4060 	if (scmd->sc_data_direction == DMA_FROM_DEVICE)
4061 		scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_READ;
4062 	else if (scmd->sc_data_direction == DMA_TO_DEVICE)
4063 		scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_WRITE;
4064 	else
4065 		scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_NO_DATA_TRANSFER;
4066 
4067 	scsiio_flags |= MPI3_SCSIIO_FLAGS_TASKATTRIBUTE_SIMPLEQ;
4068 
4069 	if (sdev_priv_data->ncq_prio_enable) {
4070 		iprio_class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
4071 		if (iprio_class == IOPRIO_CLASS_RT)
4072 			scsiio_flags |= 1 << MPI3_SCSIIO_FLAGS_CMDPRI_SHIFT;
4073 	}
4074 
4075 	if (scmd->cmd_len > 16)
4076 		scsiio_flags |= MPI3_SCSIIO_FLAGS_CDB_GREATER_THAN_16;
4077 
4078 	scmd_priv_data = scsi_cmd_priv(scmd);
4079 	memset(scmd_priv_data->mpi3mr_scsiio_req, 0, MPI3MR_ADMIN_REQ_FRAME_SZ);
4080 	scsiio_req = (struct mpi3_scsi_io_request *)scmd_priv_data->mpi3mr_scsiio_req;
4081 	scsiio_req->function = MPI3_FUNCTION_SCSI_IO;
4082 	scsiio_req->host_tag = cpu_to_le16(host_tag);
4083 
4084 	mpi3mr_setup_eedp(mrioc, scmd, scsiio_req);
4085 
4086 	memcpy(scsiio_req->cdb.cdb32, scmd->cmnd, scmd->cmd_len);
4087 	scsiio_req->data_length = cpu_to_le32(scsi_bufflen(scmd));
4088 	scsiio_req->dev_handle = cpu_to_le16(dev_handle);
4089 	scsiio_req->flags = cpu_to_le32(scsiio_flags);
4090 	int_to_scsilun(sdev_priv_data->lun_id,
4091 	    (struct scsi_lun *)scsiio_req->lun);
4092 
4093 	if (mpi3mr_build_sg_scmd(mrioc, scmd, scsiio_req)) {
4094 		mpi3mr_clear_scmd_priv(mrioc, scmd);
4095 		retval = SCSI_MLQUEUE_HOST_BUSY;
4096 		goto out;
4097 	}
4098 	op_req_q = &mrioc->req_qinfo[scmd_priv_data->req_q_idx];
4099 
4100 	if (mpi3mr_op_request_post(mrioc, op_req_q,
4101 	    scmd_priv_data->mpi3mr_scsiio_req)) {
4102 		mpi3mr_clear_scmd_priv(mrioc, scmd);
4103 		retval = SCSI_MLQUEUE_HOST_BUSY;
4104 		goto out;
4105 	}
4106 
4107 out:
4108 	return retval;
4109 }
4110 
4111 static struct scsi_host_template mpi3mr_driver_template = {
4112 	.module				= THIS_MODULE,
4113 	.name				= "MPI3 Storage Controller",
4114 	.proc_name			= MPI3MR_DRIVER_NAME,
4115 	.queuecommand			= mpi3mr_qcmd,
4116 	.target_alloc			= mpi3mr_target_alloc,
4117 	.slave_alloc			= mpi3mr_slave_alloc,
4118 	.slave_configure		= mpi3mr_slave_configure,
4119 	.target_destroy			= mpi3mr_target_destroy,
4120 	.slave_destroy			= mpi3mr_slave_destroy,
4121 	.scan_finished			= mpi3mr_scan_finished,
4122 	.scan_start			= mpi3mr_scan_start,
4123 	.change_queue_depth		= mpi3mr_change_queue_depth,
4124 	.eh_device_reset_handler	= mpi3mr_eh_dev_reset,
4125 	.eh_target_reset_handler	= mpi3mr_eh_target_reset,
4126 	.eh_host_reset_handler		= mpi3mr_eh_host_reset,
4127 	.bios_param			= mpi3mr_bios_param,
4128 	.map_queues			= mpi3mr_map_queues,
4129 	.mq_poll                        = mpi3mr_blk_mq_poll,
4130 	.no_write_same			= 1,
4131 	.can_queue			= 1,
4132 	.this_id			= -1,
4133 	.sg_tablesize			= MPI3MR_SG_DEPTH,
4134 	/* max xfer supported is 1M (2K in 512 byte sized sectors)
4135 	 */
4136 	.max_sectors			= 2048,
4137 	.cmd_per_lun			= MPI3MR_MAX_CMDS_LUN,
4138 	.max_segment_size		= 0xffffffff,
4139 	.track_queue_depth		= 1,
4140 	.cmd_size			= sizeof(struct scmd_priv),
4141 	.shost_groups			= mpi3mr_host_groups,
4142 };
4143 
4144 /**
4145  * mpi3mr_init_drv_cmd - Initialize internal command tracker
4146  * @cmdptr: Internal command tracker
4147  * @host_tag: Host tag used for the specific command
4148  *
4149  * Initialize the internal command tracker structure with
4150  * specified host tag.
4151  *
4152  * Return: Nothing.
4153  */
4154 static inline void mpi3mr_init_drv_cmd(struct mpi3mr_drv_cmd *cmdptr,
4155 	u16 host_tag)
4156 {
4157 	mutex_init(&cmdptr->mutex);
4158 	cmdptr->reply = NULL;
4159 	cmdptr->state = MPI3MR_CMD_NOTUSED;
4160 	cmdptr->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
4161 	cmdptr->host_tag = host_tag;
4162 }
4163 
4164 /**
4165  * osintfc_mrioc_security_status -Check controller secure status
4166  * @pdev: PCI device instance
4167  *
4168  * Read the Device Serial Number capability from PCI config
4169  * space and decide whether the controller is secure or not.
4170  *
4171  * Return: 0 on success, non-zero on failure.
4172  */
4173 static int
4174 osintfc_mrioc_security_status(struct pci_dev *pdev)
4175 {
4176 	u32 cap_data;
4177 	int base;
4178 	u32 ctlr_status;
4179 	u32 debug_status;
4180 	int retval = 0;
4181 
4182 	base = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DSN);
4183 	if (!base) {
4184 		dev_err(&pdev->dev,
4185 		    "%s: PCI_EXT_CAP_ID_DSN is not supported\n", __func__);
4186 		return -1;
4187 	}
4188 
4189 	pci_read_config_dword(pdev, base + 4, &cap_data);
4190 
4191 	debug_status = cap_data & MPI3MR_CTLR_SECURE_DBG_STATUS_MASK;
4192 	ctlr_status = cap_data & MPI3MR_CTLR_SECURITY_STATUS_MASK;
4193 
4194 	switch (ctlr_status) {
4195 	case MPI3MR_INVALID_DEVICE:
4196 		dev_err(&pdev->dev,
4197 		    "%s: Non secure ctlr (Invalid) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
4198 		    __func__, pdev->device, pdev->subsystem_vendor,
4199 		    pdev->subsystem_device);
4200 		retval = -1;
4201 		break;
4202 	case MPI3MR_CONFIG_SECURE_DEVICE:
4203 		if (!debug_status)
4204 			dev_info(&pdev->dev,
4205 			    "%s: Config secure ctlr is detected\n",
4206 			    __func__);
4207 		break;
4208 	case MPI3MR_HARD_SECURE_DEVICE:
4209 		break;
4210 	case MPI3MR_TAMPERED_DEVICE:
4211 		dev_err(&pdev->dev,
4212 		    "%s: Non secure ctlr (Tampered) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
4213 		    __func__, pdev->device, pdev->subsystem_vendor,
4214 		    pdev->subsystem_device);
4215 		retval = -1;
4216 		break;
4217 	default:
4218 		retval = -1;
4219 			break;
4220 	}
4221 
4222 	if (!retval && debug_status) {
4223 		dev_err(&pdev->dev,
4224 		    "%s: Non secure ctlr (Secure Dbg) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
4225 		    __func__, pdev->device, pdev->subsystem_vendor,
4226 		    pdev->subsystem_device);
4227 		retval = -1;
4228 	}
4229 
4230 	return retval;
4231 }
4232 
4233 /**
4234  * mpi3mr_probe - PCI probe callback
4235  * @pdev: PCI device instance
4236  * @id: PCI device ID details
4237  *
4238  * controller initialization routine. Checks the security status
4239  * of the controller and if it is invalid or tampered return the
4240  * probe without initializing the controller. Otherwise,
4241  * allocate per adapter instance through shost_priv and
4242  * initialize controller specific data structures, initializae
4243  * the controller hardware, add shost to the SCSI subsystem.
4244  *
4245  * Return: 0 on success, non-zero on failure.
4246  */
4247 
4248 static int
4249 mpi3mr_probe(struct pci_dev *pdev, const struct pci_device_id *id)
4250 {
4251 	struct mpi3mr_ioc *mrioc = NULL;
4252 	struct Scsi_Host *shost = NULL;
4253 	int retval = 0, i;
4254 
4255 	if (osintfc_mrioc_security_status(pdev)) {
4256 		warn_non_secure_ctlr = 1;
4257 		return 1; /* For Invalid and Tampered device */
4258 	}
4259 
4260 	shost = scsi_host_alloc(&mpi3mr_driver_template,
4261 	    sizeof(struct mpi3mr_ioc));
4262 	if (!shost) {
4263 		retval = -ENODEV;
4264 		goto shost_failed;
4265 	}
4266 
4267 	mrioc = shost_priv(shost);
4268 	mrioc->id = mrioc_ids++;
4269 	sprintf(mrioc->driver_name, "%s", MPI3MR_DRIVER_NAME);
4270 	sprintf(mrioc->name, "%s%d", mrioc->driver_name, mrioc->id);
4271 	INIT_LIST_HEAD(&mrioc->list);
4272 	spin_lock(&mrioc_list_lock);
4273 	list_add_tail(&mrioc->list, &mrioc_list);
4274 	spin_unlock(&mrioc_list_lock);
4275 
4276 	spin_lock_init(&mrioc->admin_req_lock);
4277 	spin_lock_init(&mrioc->reply_free_queue_lock);
4278 	spin_lock_init(&mrioc->sbq_lock);
4279 	spin_lock_init(&mrioc->fwevt_lock);
4280 	spin_lock_init(&mrioc->tgtdev_lock);
4281 	spin_lock_init(&mrioc->watchdog_lock);
4282 	spin_lock_init(&mrioc->chain_buf_lock);
4283 
4284 	INIT_LIST_HEAD(&mrioc->fwevt_list);
4285 	INIT_LIST_HEAD(&mrioc->tgtdev_list);
4286 	INIT_LIST_HEAD(&mrioc->delayed_rmhs_list);
4287 	INIT_LIST_HEAD(&mrioc->delayed_evtack_cmds_list);
4288 
4289 	mutex_init(&mrioc->reset_mutex);
4290 	mpi3mr_init_drv_cmd(&mrioc->init_cmds, MPI3MR_HOSTTAG_INITCMDS);
4291 	mpi3mr_init_drv_cmd(&mrioc->host_tm_cmds, MPI3MR_HOSTTAG_BLK_TMS);
4292 	mpi3mr_init_drv_cmd(&mrioc->bsg_cmds, MPI3MR_HOSTTAG_BSG_CMDS);
4293 
4294 	for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++)
4295 		mpi3mr_init_drv_cmd(&mrioc->dev_rmhs_cmds[i],
4296 		    MPI3MR_HOSTTAG_DEVRMCMD_MIN + i);
4297 
4298 	if (pdev->revision)
4299 		mrioc->enable_segqueue = true;
4300 
4301 	init_waitqueue_head(&mrioc->reset_waitq);
4302 	mrioc->logging_level = logging_level;
4303 	mrioc->shost = shost;
4304 	mrioc->pdev = pdev;
4305 	mrioc->stop_bsgs = 1;
4306 
4307 	/* init shost parameters */
4308 	shost->max_cmd_len = MPI3MR_MAX_CDB_LENGTH;
4309 	shost->max_lun = -1;
4310 	shost->unique_id = mrioc->id;
4311 
4312 	shost->max_channel = 0;
4313 	shost->max_id = 0xFFFFFFFF;
4314 
4315 	if (prot_mask >= 0)
4316 		scsi_host_set_prot(shost, prot_mask);
4317 	else {
4318 		prot_mask = SHOST_DIF_TYPE1_PROTECTION
4319 		    | SHOST_DIF_TYPE2_PROTECTION
4320 		    | SHOST_DIF_TYPE3_PROTECTION;
4321 		scsi_host_set_prot(shost, prot_mask);
4322 	}
4323 
4324 	ioc_info(mrioc,
4325 	    "%s :host protection capabilities enabled %s%s%s%s%s%s%s\n",
4326 	    __func__,
4327 	    (prot_mask & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "",
4328 	    (prot_mask & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "",
4329 	    (prot_mask & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "",
4330 	    (prot_mask & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "",
4331 	    (prot_mask & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "",
4332 	    (prot_mask & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "",
4333 	    (prot_mask & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : "");
4334 
4335 	if (prot_guard_mask)
4336 		scsi_host_set_guard(shost, (prot_guard_mask & 3));
4337 	else
4338 		scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
4339 
4340 	snprintf(mrioc->fwevt_worker_name, sizeof(mrioc->fwevt_worker_name),
4341 	    "%s%d_fwevt_wrkr", mrioc->driver_name, mrioc->id);
4342 	mrioc->fwevt_worker_thread = alloc_ordered_workqueue(
4343 	    mrioc->fwevt_worker_name, 0);
4344 	if (!mrioc->fwevt_worker_thread) {
4345 		ioc_err(mrioc, "failure at %s:%d/%s()!\n",
4346 		    __FILE__, __LINE__, __func__);
4347 		retval = -ENODEV;
4348 		goto fwevtthread_failed;
4349 	}
4350 
4351 	mrioc->is_driver_loading = 1;
4352 	mrioc->cpu_count = num_online_cpus();
4353 	if (mpi3mr_setup_resources(mrioc)) {
4354 		ioc_err(mrioc, "setup resources failed\n");
4355 		retval = -ENODEV;
4356 		goto resource_alloc_failed;
4357 	}
4358 	if (mpi3mr_init_ioc(mrioc)) {
4359 		ioc_err(mrioc, "initializing IOC failed\n");
4360 		retval = -ENODEV;
4361 		goto init_ioc_failed;
4362 	}
4363 
4364 	shost->nr_hw_queues = mrioc->num_op_reply_q;
4365 	if (mrioc->active_poll_qcount)
4366 		shost->nr_maps = 3;
4367 
4368 	shost->can_queue = mrioc->max_host_ios;
4369 	shost->sg_tablesize = MPI3MR_SG_DEPTH;
4370 	shost->max_id = mrioc->facts.max_perids + 1;
4371 
4372 	retval = scsi_add_host(shost, &pdev->dev);
4373 	if (retval) {
4374 		ioc_err(mrioc, "failure at %s:%d/%s()!\n",
4375 		    __FILE__, __LINE__, __func__);
4376 		goto addhost_failed;
4377 	}
4378 
4379 	scsi_scan_host(shost);
4380 	mpi3mr_bsg_init(mrioc);
4381 	return retval;
4382 
4383 addhost_failed:
4384 	mpi3mr_stop_watchdog(mrioc);
4385 	mpi3mr_cleanup_ioc(mrioc);
4386 init_ioc_failed:
4387 	mpi3mr_free_mem(mrioc);
4388 	mpi3mr_cleanup_resources(mrioc);
4389 resource_alloc_failed:
4390 	destroy_workqueue(mrioc->fwevt_worker_thread);
4391 fwevtthread_failed:
4392 	spin_lock(&mrioc_list_lock);
4393 	list_del(&mrioc->list);
4394 	spin_unlock(&mrioc_list_lock);
4395 	scsi_host_put(shost);
4396 shost_failed:
4397 	return retval;
4398 }
4399 
4400 /**
4401  * mpi3mr_remove - PCI remove callback
4402  * @pdev: PCI device instance
4403  *
4404  * Cleanup the IOC by issuing MUR and shutdown notification.
4405  * Free up all memory and resources associated with the
4406  * controllerand target devices, unregister the shost.
4407  *
4408  * Return: Nothing.
4409  */
4410 static void mpi3mr_remove(struct pci_dev *pdev)
4411 {
4412 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
4413 	struct mpi3mr_ioc *mrioc;
4414 	struct workqueue_struct	*wq;
4415 	unsigned long flags;
4416 	struct mpi3mr_tgt_dev *tgtdev, *tgtdev_next;
4417 
4418 	if (!shost)
4419 		return;
4420 
4421 	mrioc = shost_priv(shost);
4422 	while (mrioc->reset_in_progress || mrioc->is_driver_loading)
4423 		ssleep(1);
4424 
4425 	mpi3mr_bsg_exit(mrioc);
4426 	mrioc->stop_drv_processing = 1;
4427 	mpi3mr_cleanup_fwevt_list(mrioc);
4428 	spin_lock_irqsave(&mrioc->fwevt_lock, flags);
4429 	wq = mrioc->fwevt_worker_thread;
4430 	mrioc->fwevt_worker_thread = NULL;
4431 	spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
4432 	if (wq)
4433 		destroy_workqueue(wq);
4434 	scsi_remove_host(shost);
4435 
4436 	list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
4437 	    list) {
4438 		mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
4439 		mpi3mr_tgtdev_del_from_list(mrioc, tgtdev);
4440 		mpi3mr_tgtdev_put(tgtdev);
4441 	}
4442 	mpi3mr_stop_watchdog(mrioc);
4443 	mpi3mr_cleanup_ioc(mrioc);
4444 	mpi3mr_free_mem(mrioc);
4445 	mpi3mr_cleanup_resources(mrioc);
4446 
4447 	spin_lock(&mrioc_list_lock);
4448 	list_del(&mrioc->list);
4449 	spin_unlock(&mrioc_list_lock);
4450 
4451 	scsi_host_put(shost);
4452 }
4453 
4454 /**
4455  * mpi3mr_shutdown - PCI shutdown callback
4456  * @pdev: PCI device instance
4457  *
4458  * Free up all memory and resources associated with the
4459  * controller
4460  *
4461  * Return: Nothing.
4462  */
4463 static void mpi3mr_shutdown(struct pci_dev *pdev)
4464 {
4465 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
4466 	struct mpi3mr_ioc *mrioc;
4467 	struct workqueue_struct	*wq;
4468 	unsigned long flags;
4469 
4470 	if (!shost)
4471 		return;
4472 
4473 	mrioc = shost_priv(shost);
4474 	while (mrioc->reset_in_progress || mrioc->is_driver_loading)
4475 		ssleep(1);
4476 
4477 	mrioc->stop_drv_processing = 1;
4478 	mpi3mr_cleanup_fwevt_list(mrioc);
4479 	spin_lock_irqsave(&mrioc->fwevt_lock, flags);
4480 	wq = mrioc->fwevt_worker_thread;
4481 	mrioc->fwevt_worker_thread = NULL;
4482 	spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
4483 	if (wq)
4484 		destroy_workqueue(wq);
4485 
4486 	mpi3mr_stop_watchdog(mrioc);
4487 	mpi3mr_cleanup_ioc(mrioc);
4488 	mpi3mr_cleanup_resources(mrioc);
4489 }
4490 
4491 #ifdef CONFIG_PM
4492 /**
4493  * mpi3mr_suspend - PCI power management suspend callback
4494  * @pdev: PCI device instance
4495  * @state: New power state
4496  *
4497  * Change the power state to the given value and cleanup the IOC
4498  * by issuing MUR and shutdown notification
4499  *
4500  * Return: 0 always.
4501  */
4502 static int mpi3mr_suspend(struct pci_dev *pdev, pm_message_t state)
4503 {
4504 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
4505 	struct mpi3mr_ioc *mrioc;
4506 	pci_power_t device_state;
4507 
4508 	if (!shost)
4509 		return 0;
4510 
4511 	mrioc = shost_priv(shost);
4512 	while (mrioc->reset_in_progress || mrioc->is_driver_loading)
4513 		ssleep(1);
4514 	mrioc->stop_drv_processing = 1;
4515 	mpi3mr_cleanup_fwevt_list(mrioc);
4516 	scsi_block_requests(shost);
4517 	mpi3mr_stop_watchdog(mrioc);
4518 	mpi3mr_cleanup_ioc(mrioc);
4519 
4520 	device_state = pci_choose_state(pdev, state);
4521 	ioc_info(mrioc, "pdev=0x%p, slot=%s, entering operating state [D%d]\n",
4522 	    pdev, pci_name(pdev), device_state);
4523 	pci_save_state(pdev);
4524 	mpi3mr_cleanup_resources(mrioc);
4525 	pci_set_power_state(pdev, device_state);
4526 
4527 	return 0;
4528 }
4529 
4530 /**
4531  * mpi3mr_resume - PCI power management resume callback
4532  * @pdev: PCI device instance
4533  *
4534  * Restore the power state to D0 and reinitialize the controller
4535  * and resume I/O operations to the target devices
4536  *
4537  * Return: 0 on success, non-zero on failure
4538  */
4539 static int mpi3mr_resume(struct pci_dev *pdev)
4540 {
4541 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
4542 	struct mpi3mr_ioc *mrioc;
4543 	pci_power_t device_state = pdev->current_state;
4544 	int r;
4545 
4546 	if (!shost)
4547 		return 0;
4548 
4549 	mrioc = shost_priv(shost);
4550 
4551 	ioc_info(mrioc, "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
4552 	    pdev, pci_name(pdev), device_state);
4553 	pci_set_power_state(pdev, PCI_D0);
4554 	pci_enable_wake(pdev, PCI_D0, 0);
4555 	pci_restore_state(pdev);
4556 	mrioc->pdev = pdev;
4557 	mrioc->cpu_count = num_online_cpus();
4558 	r = mpi3mr_setup_resources(mrioc);
4559 	if (r) {
4560 		ioc_info(mrioc, "%s: Setup resources failed[%d]\n",
4561 		    __func__, r);
4562 		return r;
4563 	}
4564 
4565 	mrioc->stop_drv_processing = 0;
4566 	mpi3mr_memset_buffers(mrioc);
4567 	r = mpi3mr_reinit_ioc(mrioc, 1);
4568 	if (r) {
4569 		ioc_err(mrioc, "resuming controller failed[%d]\n", r);
4570 		return r;
4571 	}
4572 	scsi_unblock_requests(shost);
4573 	mpi3mr_start_watchdog(mrioc);
4574 
4575 	return 0;
4576 }
4577 #endif
4578 
4579 static const struct pci_device_id mpi3mr_pci_id_table[] = {
4580 	{
4581 		PCI_DEVICE_SUB(MPI3_MFGPAGE_VENDORID_BROADCOM,
4582 		    MPI3_MFGPAGE_DEVID_SAS4116, PCI_ANY_ID, PCI_ANY_ID)
4583 	},
4584 	{ 0 }
4585 };
4586 MODULE_DEVICE_TABLE(pci, mpi3mr_pci_id_table);
4587 
4588 static struct pci_driver mpi3mr_pci_driver = {
4589 	.name = MPI3MR_DRIVER_NAME,
4590 	.id_table = mpi3mr_pci_id_table,
4591 	.probe = mpi3mr_probe,
4592 	.remove = mpi3mr_remove,
4593 	.shutdown = mpi3mr_shutdown,
4594 #ifdef CONFIG_PM
4595 	.suspend = mpi3mr_suspend,
4596 	.resume = mpi3mr_resume,
4597 #endif
4598 };
4599 
4600 static ssize_t event_counter_show(struct device_driver *dd, char *buf)
4601 {
4602 	return sprintf(buf, "%llu\n", atomic64_read(&event_counter));
4603 }
4604 static DRIVER_ATTR_RO(event_counter);
4605 
4606 static int __init mpi3mr_init(void)
4607 {
4608 	int ret_val;
4609 
4610 	pr_info("Loading %s version %s\n", MPI3MR_DRIVER_NAME,
4611 	    MPI3MR_DRIVER_VERSION);
4612 
4613 	ret_val = pci_register_driver(&mpi3mr_pci_driver);
4614 	if (ret_val) {
4615 		pr_err("%s failed to load due to pci register driver failure\n",
4616 		    MPI3MR_DRIVER_NAME);
4617 		return ret_val;
4618 	}
4619 
4620 	ret_val = driver_create_file(&mpi3mr_pci_driver.driver,
4621 				     &driver_attr_event_counter);
4622 	if (ret_val)
4623 		pci_unregister_driver(&mpi3mr_pci_driver);
4624 
4625 	return ret_val;
4626 }
4627 
4628 static void __exit mpi3mr_exit(void)
4629 {
4630 	if (warn_non_secure_ctlr)
4631 		pr_warn(
4632 		    "Unloading %s version %s while managing a non secure controller\n",
4633 		    MPI3MR_DRIVER_NAME, MPI3MR_DRIVER_VERSION);
4634 	else
4635 		pr_info("Unloading %s version %s\n", MPI3MR_DRIVER_NAME,
4636 		    MPI3MR_DRIVER_VERSION);
4637 
4638 	driver_remove_file(&mpi3mr_pci_driver.driver,
4639 			   &driver_attr_event_counter);
4640 	pci_unregister_driver(&mpi3mr_pci_driver);
4641 }
4642 
4643 module_init(mpi3mr_init);
4644 module_exit(mpi3mr_exit);
4645