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