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