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