xref: /linux/drivers/scsi/mpi3mr/mpi3mr_os.c (revision 9acb51e9617c28a92f9ce2af767db6bd660a6d4f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Driver for Broadcom MPI3 Storage Controllers
4  *
5  * Copyright (C) 2017-2023 Broadcom Inc.
6  *  (mailto: mpi3mr-linuxdrv.pdl@broadcom.com)
7  *
8  */
9 
10 #include "mpi3mr.h"
11 #include <linux/idr.h>
12 
13 /* global driver scop variables */
14 LIST_HEAD(mrioc_list);
15 DEFINE_SPINLOCK(mrioc_list_lock);
16 static DEFINE_IDA(mrioc_ida);
17 static int warn_non_secure_ctlr;
18 atomic64_t event_counter;
19 
20 MODULE_AUTHOR(MPI3MR_DRIVER_AUTHOR);
21 MODULE_DESCRIPTION(MPI3MR_DRIVER_DESC);
22 MODULE_LICENSE(MPI3MR_DRIVER_LICENSE);
23 MODULE_VERSION(MPI3MR_DRIVER_VERSION);
24 
25 /* Module parameters*/
26 int prot_mask = -1;
27 module_param(prot_mask, int, 0);
28 MODULE_PARM_DESC(prot_mask, "Host protection capabilities mask, def=0x07");
29 
30 static int prot_guard_mask = 3;
31 module_param(prot_guard_mask, int, 0);
32 MODULE_PARM_DESC(prot_guard_mask, " Host protection guard mask, def=3");
33 static int logging_level;
34 module_param(logging_level, int, 0);
35 MODULE_PARM_DESC(logging_level,
36 	" bits for enabling additional logging info (default=0)");
37 static int max_sgl_entries = MPI3MR_DEFAULT_SGL_ENTRIES;
38 module_param(max_sgl_entries, int, 0444);
39 MODULE_PARM_DESC(max_sgl_entries,
40 	"Preferred max number of SG entries to be used for a single I/O\n"
41 	"The actual value will be determined by the driver\n"
42 	"(Minimum=256, Maximum=2048, default=256)");
43 
44 /* Forward declarations*/
45 static void mpi3mr_send_event_ack(struct mpi3mr_ioc *mrioc, u8 event,
46 	struct mpi3mr_drv_cmd *cmdparam, u32 event_ctx);
47 
48 #define MPI3MR_DRIVER_EVENT_TG_QD_REDUCTION	(0xFFFF)
49 
50 #define MPI3_EVENT_WAIT_FOR_DEVICES_TO_REFRESH	(0xFFFE)
51 
52 /**
53  * mpi3mr_host_tag_for_scmd - Get host tag for a scmd
54  * @mrioc: Adapter instance reference
55  * @scmd: SCSI command reference
56  *
57  * Calculate the host tag based on block tag for a given scmd.
58  *
59  * Return: Valid host tag or MPI3MR_HOSTTAG_INVALID.
60  */
61 static u16 mpi3mr_host_tag_for_scmd(struct mpi3mr_ioc *mrioc,
62 	struct scsi_cmnd *scmd)
63 {
64 	struct scmd_priv *priv = NULL;
65 	u32 unique_tag;
66 	u16 host_tag, hw_queue;
67 
68 	unique_tag = blk_mq_unique_tag(scsi_cmd_to_rq(scmd));
69 
70 	hw_queue = blk_mq_unique_tag_to_hwq(unique_tag);
71 	if (hw_queue >= mrioc->num_op_reply_q)
72 		return MPI3MR_HOSTTAG_INVALID;
73 	host_tag = blk_mq_unique_tag_to_tag(unique_tag);
74 
75 	if (WARN_ON(host_tag >= mrioc->max_host_ios))
76 		return MPI3MR_HOSTTAG_INVALID;
77 
78 	priv = scsi_cmd_priv(scmd);
79 	/*host_tag 0 is invalid hence incrementing by 1*/
80 	priv->host_tag = host_tag + 1;
81 	priv->scmd = scmd;
82 	priv->in_lld_scope = 1;
83 	priv->req_q_idx = hw_queue;
84 	priv->meta_chain_idx = -1;
85 	priv->chain_idx = -1;
86 	priv->meta_sg_valid = 0;
87 	return priv->host_tag;
88 }
89 
90 /**
91  * mpi3mr_scmd_from_host_tag - Get SCSI command from host tag
92  * @mrioc: Adapter instance reference
93  * @host_tag: Host tag
94  * @qidx: Operational queue index
95  *
96  * Identify the block tag from the host tag and queue index and
97  * retrieve associated scsi command using scsi_host_find_tag().
98  *
99  * Return: SCSI command reference or NULL.
100  */
101 static struct scsi_cmnd *mpi3mr_scmd_from_host_tag(
102 	struct mpi3mr_ioc *mrioc, u16 host_tag, u16 qidx)
103 {
104 	struct scsi_cmnd *scmd = NULL;
105 	struct scmd_priv *priv = NULL;
106 	u32 unique_tag = host_tag - 1;
107 
108 	if (WARN_ON(host_tag > mrioc->max_host_ios))
109 		goto out;
110 
111 	unique_tag |= (qidx << BLK_MQ_UNIQUE_TAG_BITS);
112 
113 	scmd = scsi_host_find_tag(mrioc->shost, unique_tag);
114 	if (scmd) {
115 		priv = scsi_cmd_priv(scmd);
116 		if (!priv->in_lld_scope)
117 			scmd = NULL;
118 	}
119 out:
120 	return scmd;
121 }
122 
123 /**
124  * mpi3mr_clear_scmd_priv - Cleanup SCSI command private date
125  * @mrioc: Adapter instance reference
126  * @scmd: SCSI command reference
127  *
128  * Invalidate the SCSI command private data to mark the command
129  * is not in LLD scope anymore.
130  *
131  * Return: Nothing.
132  */
133 static void mpi3mr_clear_scmd_priv(struct mpi3mr_ioc *mrioc,
134 	struct scsi_cmnd *scmd)
135 {
136 	struct scmd_priv *priv = NULL;
137 
138 	priv = scsi_cmd_priv(scmd);
139 
140 	if (WARN_ON(priv->in_lld_scope == 0))
141 		return;
142 	priv->host_tag = MPI3MR_HOSTTAG_INVALID;
143 	priv->req_q_idx = 0xFFFF;
144 	priv->scmd = NULL;
145 	priv->in_lld_scope = 0;
146 	priv->meta_sg_valid = 0;
147 	if (priv->chain_idx >= 0) {
148 		clear_bit(priv->chain_idx, mrioc->chain_bitmap);
149 		priv->chain_idx = -1;
150 	}
151 	if (priv->meta_chain_idx >= 0) {
152 		clear_bit(priv->meta_chain_idx, mrioc->chain_bitmap);
153 		priv->meta_chain_idx = -1;
154 	}
155 }
156 
157 static void mpi3mr_dev_rmhs_send_tm(struct mpi3mr_ioc *mrioc, u16 handle,
158 	struct mpi3mr_drv_cmd *cmdparam, u8 iou_rc);
159 static void mpi3mr_fwevt_worker(struct work_struct *work);
160 
161 /**
162  * mpi3mr_fwevt_free - firmware event memory dealloctor
163  * @r: k reference pointer of the firmware event
164  *
165  * Free firmware event memory when no reference.
166  */
167 static void mpi3mr_fwevt_free(struct kref *r)
168 {
169 	kfree(container_of(r, struct mpi3mr_fwevt, ref_count));
170 }
171 
172 /**
173  * mpi3mr_fwevt_get - k reference incrementor
174  * @fwevt: Firmware event reference
175  *
176  * Increment firmware event reference count.
177  */
178 static void mpi3mr_fwevt_get(struct mpi3mr_fwevt *fwevt)
179 {
180 	kref_get(&fwevt->ref_count);
181 }
182 
183 /**
184  * mpi3mr_fwevt_put - k reference decrementor
185  * @fwevt: Firmware event reference
186  *
187  * decrement firmware event reference count.
188  */
189 static void mpi3mr_fwevt_put(struct mpi3mr_fwevt *fwevt)
190 {
191 	kref_put(&fwevt->ref_count, mpi3mr_fwevt_free);
192 }
193 
194 /**
195  * mpi3mr_alloc_fwevt - Allocate firmware event
196  * @len: length of firmware event data to allocate
197  *
198  * Allocate firmware event with required length and initialize
199  * the reference counter.
200  *
201  * Return: firmware event reference.
202  */
203 static struct mpi3mr_fwevt *mpi3mr_alloc_fwevt(int len)
204 {
205 	struct mpi3mr_fwevt *fwevt;
206 
207 	fwevt = kzalloc(sizeof(*fwevt) + len, GFP_ATOMIC);
208 	if (!fwevt)
209 		return NULL;
210 
211 	kref_init(&fwevt->ref_count);
212 	return fwevt;
213 }
214 
215 /**
216  * mpi3mr_fwevt_add_to_list - Add firmware event to the list
217  * @mrioc: Adapter instance reference
218  * @fwevt: Firmware event reference
219  *
220  * Add the given firmware event to the firmware event list.
221  *
222  * Return: Nothing.
223  */
224 static void mpi3mr_fwevt_add_to_list(struct mpi3mr_ioc *mrioc,
225 	struct mpi3mr_fwevt *fwevt)
226 {
227 	unsigned long flags;
228 
229 	if (!mrioc->fwevt_worker_thread)
230 		return;
231 
232 	spin_lock_irqsave(&mrioc->fwevt_lock, flags);
233 	/* get fwevt reference count while adding it to fwevt_list */
234 	mpi3mr_fwevt_get(fwevt);
235 	INIT_LIST_HEAD(&fwevt->list);
236 	list_add_tail(&fwevt->list, &mrioc->fwevt_list);
237 	INIT_WORK(&fwevt->work, mpi3mr_fwevt_worker);
238 	/* get fwevt reference count while enqueueing it to worker queue */
239 	mpi3mr_fwevt_get(fwevt);
240 	queue_work(mrioc->fwevt_worker_thread, &fwevt->work);
241 	spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
242 }
243 
244 /**
245  * mpi3mr_hdb_trigger_data_event - Add hdb trigger data event to
246  * the list
247  * @mrioc: Adapter instance reference
248  * @event_data: Event data
249  *
250  * Add the given hdb trigger data event to the firmware event
251  * list.
252  *
253  * Return: Nothing.
254  */
255 void mpi3mr_hdb_trigger_data_event(struct mpi3mr_ioc *mrioc,
256 	struct trigger_event_data *event_data)
257 {
258 	struct mpi3mr_fwevt *fwevt;
259 	u16 sz = sizeof(*event_data);
260 
261 	fwevt = mpi3mr_alloc_fwevt(sz);
262 	if (!fwevt) {
263 		ioc_warn(mrioc, "failed to queue hdb trigger data event\n");
264 		return;
265 	}
266 
267 	fwevt->mrioc = mrioc;
268 	fwevt->event_id = MPI3MR_DRIVER_EVENT_PROCESS_TRIGGER;
269 	fwevt->send_ack = 0;
270 	fwevt->process_evt = 1;
271 	fwevt->evt_ctx = 0;
272 	fwevt->event_data_size = sz;
273 	memcpy(fwevt->event_data, event_data, sz);
274 
275 	mpi3mr_fwevt_add_to_list(mrioc, fwevt);
276 }
277 
278 /**
279  * mpi3mr_fwevt_del_from_list - Delete firmware event from list
280  * @mrioc: Adapter instance reference
281  * @fwevt: Firmware event reference
282  *
283  * Delete the given firmware event from the firmware event list.
284  *
285  * Return: Nothing.
286  */
287 static void mpi3mr_fwevt_del_from_list(struct mpi3mr_ioc *mrioc,
288 	struct mpi3mr_fwevt *fwevt)
289 {
290 	unsigned long flags;
291 
292 	spin_lock_irqsave(&mrioc->fwevt_lock, flags);
293 	if (!list_empty(&fwevt->list)) {
294 		list_del_init(&fwevt->list);
295 		/*
296 		 * Put fwevt reference count after
297 		 * removing it from fwevt_list
298 		 */
299 		mpi3mr_fwevt_put(fwevt);
300 	}
301 	spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
302 }
303 
304 /**
305  * mpi3mr_dequeue_fwevt - Dequeue firmware event from the list
306  * @mrioc: Adapter instance reference
307  *
308  * Dequeue a firmware event from the firmware event list.
309  *
310  * Return: firmware event.
311  */
312 static struct mpi3mr_fwevt *mpi3mr_dequeue_fwevt(
313 	struct mpi3mr_ioc *mrioc)
314 {
315 	unsigned long flags;
316 	struct mpi3mr_fwevt *fwevt = NULL;
317 
318 	spin_lock_irqsave(&mrioc->fwevt_lock, flags);
319 	if (!list_empty(&mrioc->fwevt_list)) {
320 		fwevt = list_first_entry(&mrioc->fwevt_list,
321 		    struct mpi3mr_fwevt, list);
322 		list_del_init(&fwevt->list);
323 		/*
324 		 * Put fwevt reference count after
325 		 * removing it from fwevt_list
326 		 */
327 		mpi3mr_fwevt_put(fwevt);
328 	}
329 	spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
330 
331 	return fwevt;
332 }
333 
334 /**
335  * mpi3mr_cancel_work - cancel firmware event
336  * @fwevt: fwevt object which needs to be canceled
337  *
338  * Return: Nothing.
339  */
340 static void mpi3mr_cancel_work(struct mpi3mr_fwevt *fwevt)
341 {
342 	/*
343 	 * Wait on the fwevt to complete. If this returns 1, then
344 	 * the event was never executed.
345 	 *
346 	 * If it did execute, we wait for it to finish, and the put will
347 	 * happen from mpi3mr_process_fwevt()
348 	 */
349 	if (cancel_work_sync(&fwevt->work)) {
350 		/*
351 		 * Put fwevt reference count after
352 		 * dequeuing it from worker queue
353 		 */
354 		mpi3mr_fwevt_put(fwevt);
355 		/*
356 		 * Put fwevt reference count to neutralize
357 		 * kref_init increment
358 		 */
359 		mpi3mr_fwevt_put(fwevt);
360 	}
361 }
362 
363 /**
364  * mpi3mr_cleanup_fwevt_list - Cleanup firmware event list
365  * @mrioc: Adapter instance reference
366  *
367  * Flush all pending firmware events from the firmware event
368  * list.
369  *
370  * Return: Nothing.
371  */
372 void mpi3mr_cleanup_fwevt_list(struct mpi3mr_ioc *mrioc)
373 {
374 	struct mpi3mr_fwevt *fwevt = NULL;
375 
376 	if ((list_empty(&mrioc->fwevt_list) && !mrioc->current_event) ||
377 	    !mrioc->fwevt_worker_thread)
378 		return;
379 
380 	while ((fwevt = mpi3mr_dequeue_fwevt(mrioc)))
381 		mpi3mr_cancel_work(fwevt);
382 
383 	if (mrioc->current_event) {
384 		fwevt = mrioc->current_event;
385 		/*
386 		 * Don't call cancel_work_sync() API for the
387 		 * fwevt work if the controller reset is
388 		 * get called as part of processing the
389 		 * same fwevt work (or) when worker thread is
390 		 * waiting for device add/remove APIs to complete.
391 		 * Otherwise we will see deadlock.
392 		 */
393 		if (current_work() == &fwevt->work || fwevt->pending_at_sml) {
394 			fwevt->discard = 1;
395 			return;
396 		}
397 
398 		mpi3mr_cancel_work(fwevt);
399 	}
400 }
401 
402 /**
403  * mpi3mr_queue_qd_reduction_event - Queue TG QD reduction event
404  * @mrioc: Adapter instance reference
405  * @tg: Throttle group information pointer
406  *
407  * Accessor to queue on synthetically generated driver event to
408  * the event worker thread, the driver event will be used to
409  * reduce the QD of all VDs in the TG from the worker thread.
410  *
411  * Return: None.
412  */
413 static void mpi3mr_queue_qd_reduction_event(struct mpi3mr_ioc *mrioc,
414 	struct mpi3mr_throttle_group_info *tg)
415 {
416 	struct mpi3mr_fwevt *fwevt;
417 	u16 sz = sizeof(struct mpi3mr_throttle_group_info *);
418 
419 	/*
420 	 * If the QD reduction event is already queued due to throttle and if
421 	 * the QD is not restored through device info change event
422 	 * then dont queue further reduction events
423 	 */
424 	if (tg->fw_qd != tg->modified_qd)
425 		return;
426 
427 	fwevt = mpi3mr_alloc_fwevt(sz);
428 	if (!fwevt) {
429 		ioc_warn(mrioc, "failed to queue TG QD reduction event\n");
430 		return;
431 	}
432 	*(struct mpi3mr_throttle_group_info **)fwevt->event_data = tg;
433 	fwevt->mrioc = mrioc;
434 	fwevt->event_id = MPI3MR_DRIVER_EVENT_TG_QD_REDUCTION;
435 	fwevt->send_ack = 0;
436 	fwevt->process_evt = 1;
437 	fwevt->evt_ctx = 0;
438 	fwevt->event_data_size = sz;
439 	tg->modified_qd = max_t(u16, (tg->fw_qd * tg->qd_reduction) / 10, 8);
440 
441 	dprint_event_bh(mrioc, "qd reduction event queued for tg_id(%d)\n",
442 	    tg->id);
443 	mpi3mr_fwevt_add_to_list(mrioc, fwevt);
444 }
445 
446 /**
447  * mpi3mr_invalidate_devhandles -Invalidate device handles
448  * @mrioc: Adapter instance reference
449  *
450  * Invalidate the device handles in the target device structures
451  * . Called post reset prior to reinitializing the controller.
452  *
453  * Return: Nothing.
454  */
455 void mpi3mr_invalidate_devhandles(struct mpi3mr_ioc *mrioc)
456 {
457 	struct mpi3mr_tgt_dev *tgtdev;
458 	struct mpi3mr_stgt_priv_data *tgt_priv;
459 
460 	list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
461 		tgtdev->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
462 		if (tgtdev->starget && tgtdev->starget->hostdata) {
463 			tgt_priv = tgtdev->starget->hostdata;
464 			tgt_priv->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
465 			tgt_priv->io_throttle_enabled = 0;
466 			tgt_priv->io_divert = 0;
467 			tgt_priv->throttle_group = NULL;
468 			tgt_priv->wslen = 0;
469 			if (tgtdev->host_exposed)
470 				atomic_set(&tgt_priv->block_io, 1);
471 		}
472 	}
473 }
474 
475 /**
476  * mpi3mr_print_scmd - print individual SCSI command
477  * @rq: Block request
478  * @data: Adapter instance reference
479  *
480  * Print the SCSI command details if it is in LLD scope.
481  *
482  * Return: true always.
483  */
484 static bool mpi3mr_print_scmd(struct request *rq, void *data)
485 {
486 	struct mpi3mr_ioc *mrioc = (struct mpi3mr_ioc *)data;
487 	struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
488 	struct scmd_priv *priv = NULL;
489 
490 	if (scmd) {
491 		priv = scsi_cmd_priv(scmd);
492 		if (!priv->in_lld_scope)
493 			goto out;
494 
495 		ioc_info(mrioc, "%s :Host Tag = %d, qid = %d\n",
496 		    __func__, priv->host_tag, priv->req_q_idx + 1);
497 		scsi_print_command(scmd);
498 	}
499 
500 out:
501 	return(true);
502 }
503 
504 /**
505  * mpi3mr_flush_scmd - Flush individual SCSI command
506  * @rq: Block request
507  * @data: Adapter instance reference
508  *
509  * Return the SCSI command to the upper layers if it is in LLD
510  * scope.
511  *
512  * Return: true always.
513  */
514 
515 static bool mpi3mr_flush_scmd(struct request *rq, void *data)
516 {
517 	struct mpi3mr_ioc *mrioc = (struct mpi3mr_ioc *)data;
518 	struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
519 	struct scmd_priv *priv = NULL;
520 
521 	if (scmd) {
522 		priv = scsi_cmd_priv(scmd);
523 		if (!priv->in_lld_scope)
524 			goto out;
525 
526 		if (priv->meta_sg_valid)
527 			dma_unmap_sg(&mrioc->pdev->dev, scsi_prot_sglist(scmd),
528 			    scsi_prot_sg_count(scmd), scmd->sc_data_direction);
529 		mpi3mr_clear_scmd_priv(mrioc, scmd);
530 		scsi_dma_unmap(scmd);
531 		scmd->result = DID_RESET << 16;
532 		scsi_print_command(scmd);
533 		scsi_done(scmd);
534 		mrioc->flush_io_count++;
535 	}
536 
537 out:
538 	return(true);
539 }
540 
541 /**
542  * mpi3mr_count_dev_pending - Count commands pending for a lun
543  * @rq: Block request
544  * @data: SCSI device reference
545  *
546  * This is an iterator function called for each SCSI command in
547  * a host and if the command is pending in the LLD for the
548  * specific device(lun) then device specific pending I/O counter
549  * is updated in the device structure.
550  *
551  * Return: true always.
552  */
553 
554 static bool mpi3mr_count_dev_pending(struct request *rq, void *data)
555 {
556 	struct scsi_device *sdev = (struct scsi_device *)data;
557 	struct mpi3mr_sdev_priv_data *sdev_priv_data = sdev->hostdata;
558 	struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
559 	struct scmd_priv *priv;
560 
561 	if (scmd) {
562 		priv = scsi_cmd_priv(scmd);
563 		if (!priv->in_lld_scope)
564 			goto out;
565 		if (scmd->device == sdev)
566 			sdev_priv_data->pend_count++;
567 	}
568 
569 out:
570 	return true;
571 }
572 
573 /**
574  * mpi3mr_count_tgt_pending - Count commands pending for target
575  * @rq: Block request
576  * @data: SCSI target reference
577  *
578  * This is an iterator function called for each SCSI command in
579  * a host and if the command is pending in the LLD for the
580  * specific target then target specific pending I/O counter is
581  * updated in the target structure.
582  *
583  * Return: true always.
584  */
585 
586 static bool mpi3mr_count_tgt_pending(struct request *rq, void *data)
587 {
588 	struct scsi_target *starget = (struct scsi_target *)data;
589 	struct mpi3mr_stgt_priv_data *stgt_priv_data = starget->hostdata;
590 	struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
591 	struct scmd_priv *priv;
592 
593 	if (scmd) {
594 		priv = scsi_cmd_priv(scmd);
595 		if (!priv->in_lld_scope)
596 			goto out;
597 		if (scmd->device && (scsi_target(scmd->device) == starget))
598 			stgt_priv_data->pend_count++;
599 	}
600 
601 out:
602 	return true;
603 }
604 
605 /**
606  * mpi3mr_flush_host_io -  Flush host I/Os
607  * @mrioc: Adapter instance reference
608  *
609  * Flush all of the pending I/Os by calling
610  * blk_mq_tagset_busy_iter() for each possible tag. This is
611  * executed post controller reset
612  *
613  * Return: Nothing.
614  */
615 void mpi3mr_flush_host_io(struct mpi3mr_ioc *mrioc)
616 {
617 	struct Scsi_Host *shost = mrioc->shost;
618 
619 	mrioc->flush_io_count = 0;
620 	ioc_info(mrioc, "%s :Flushing Host I/O cmds post reset\n", __func__);
621 	blk_mq_tagset_busy_iter(&shost->tag_set,
622 	    mpi3mr_flush_scmd, (void *)mrioc);
623 	ioc_info(mrioc, "%s :Flushed %d Host I/O cmds\n", __func__,
624 	    mrioc->flush_io_count);
625 }
626 
627 /**
628  * mpi3mr_flush_cmds_for_unrecovered_controller - Flush all pending cmds
629  * @mrioc: Adapter instance reference
630  *
631  * This function waits for currently running IO poll threads to
632  * exit and then flushes all host I/Os and any internal pending
633  * cmds. This is executed after controller is marked as
634  * unrecoverable.
635  *
636  * Return: Nothing.
637  */
638 void mpi3mr_flush_cmds_for_unrecovered_controller(struct mpi3mr_ioc *mrioc)
639 {
640 	struct Scsi_Host *shost = mrioc->shost;
641 	int i;
642 
643 	if (!mrioc->unrecoverable)
644 		return;
645 
646 	if (mrioc->op_reply_qinfo) {
647 		for (i = 0; i < mrioc->num_queues; i++) {
648 			while (atomic_read(&mrioc->op_reply_qinfo[i].in_use))
649 				udelay(500);
650 			atomic_set(&mrioc->op_reply_qinfo[i].pend_ios, 0);
651 		}
652 	}
653 	mrioc->flush_io_count = 0;
654 	blk_mq_tagset_busy_iter(&shost->tag_set,
655 	    mpi3mr_flush_scmd, (void *)mrioc);
656 	mpi3mr_flush_delayed_cmd_lists(mrioc);
657 	mpi3mr_flush_drv_cmds(mrioc);
658 }
659 
660 /**
661  * mpi3mr_alloc_tgtdev - target device allocator
662  *
663  * Allocate target device instance and initialize the reference
664  * count
665  *
666  * Return: target device instance.
667  */
668 static struct mpi3mr_tgt_dev *mpi3mr_alloc_tgtdev(void)
669 {
670 	struct mpi3mr_tgt_dev *tgtdev;
671 
672 	tgtdev = kzalloc(sizeof(*tgtdev), GFP_ATOMIC);
673 	if (!tgtdev)
674 		return NULL;
675 	kref_init(&tgtdev->ref_count);
676 	return tgtdev;
677 }
678 
679 /**
680  * mpi3mr_tgtdev_add_to_list -Add tgtdevice to the list
681  * @mrioc: Adapter instance reference
682  * @tgtdev: Target device
683  *
684  * Add the target device to the target device list
685  *
686  * Return: Nothing.
687  */
688 static void mpi3mr_tgtdev_add_to_list(struct mpi3mr_ioc *mrioc,
689 	struct mpi3mr_tgt_dev *tgtdev)
690 {
691 	unsigned long flags;
692 
693 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
694 	mpi3mr_tgtdev_get(tgtdev);
695 	INIT_LIST_HEAD(&tgtdev->list);
696 	list_add_tail(&tgtdev->list, &mrioc->tgtdev_list);
697 	tgtdev->state = MPI3MR_DEV_CREATED;
698 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
699 }
700 
701 /**
702  * mpi3mr_tgtdev_del_from_list -Delete tgtdevice from the list
703  * @mrioc: Adapter instance reference
704  * @tgtdev: Target device
705  * @must_delete: Must delete the target device from the list irrespective
706  * of the device state.
707  *
708  * Remove the target device from the target device list
709  *
710  * Return: Nothing.
711  */
712 static void mpi3mr_tgtdev_del_from_list(struct mpi3mr_ioc *mrioc,
713 	struct mpi3mr_tgt_dev *tgtdev, bool must_delete)
714 {
715 	unsigned long flags;
716 
717 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
718 	if ((tgtdev->state == MPI3MR_DEV_REMOVE_HS_STARTED) || (must_delete == true)) {
719 		if (!list_empty(&tgtdev->list)) {
720 			list_del_init(&tgtdev->list);
721 			tgtdev->state = MPI3MR_DEV_DELETED;
722 			mpi3mr_tgtdev_put(tgtdev);
723 		}
724 	}
725 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
726 }
727 
728 /**
729  * __mpi3mr_get_tgtdev_by_handle -Get tgtdev from device handle
730  * @mrioc: Adapter instance reference
731  * @handle: Device handle
732  *
733  * Accessor to retrieve target device from the device handle.
734  * Non Lock version
735  *
736  * Return: Target device reference.
737  */
738 static struct mpi3mr_tgt_dev  *__mpi3mr_get_tgtdev_by_handle(
739 	struct mpi3mr_ioc *mrioc, u16 handle)
740 {
741 	struct mpi3mr_tgt_dev *tgtdev;
742 
743 	assert_spin_locked(&mrioc->tgtdev_lock);
744 	list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list)
745 		if (tgtdev->dev_handle == handle)
746 			goto found_tgtdev;
747 	return NULL;
748 
749 found_tgtdev:
750 	mpi3mr_tgtdev_get(tgtdev);
751 	return tgtdev;
752 }
753 
754 /**
755  * mpi3mr_get_tgtdev_by_handle -Get tgtdev from device handle
756  * @mrioc: Adapter instance reference
757  * @handle: Device handle
758  *
759  * Accessor to retrieve target device from the device handle.
760  * Lock version
761  *
762  * Return: Target device reference.
763  */
764 struct mpi3mr_tgt_dev *mpi3mr_get_tgtdev_by_handle(
765 	struct mpi3mr_ioc *mrioc, u16 handle)
766 {
767 	struct mpi3mr_tgt_dev *tgtdev;
768 	unsigned long flags;
769 
770 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
771 	tgtdev = __mpi3mr_get_tgtdev_by_handle(mrioc, handle);
772 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
773 	return tgtdev;
774 }
775 
776 /**
777  * __mpi3mr_get_tgtdev_by_perst_id -Get tgtdev from persist ID
778  * @mrioc: Adapter instance reference
779  * @persist_id: Persistent ID
780  *
781  * Accessor to retrieve target device from the Persistent ID.
782  * Non Lock version
783  *
784  * Return: Target device reference.
785  */
786 static struct mpi3mr_tgt_dev  *__mpi3mr_get_tgtdev_by_perst_id(
787 	struct mpi3mr_ioc *mrioc, u16 persist_id)
788 {
789 	struct mpi3mr_tgt_dev *tgtdev;
790 
791 	assert_spin_locked(&mrioc->tgtdev_lock);
792 	list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list)
793 		if (tgtdev->perst_id == persist_id)
794 			goto found_tgtdev;
795 	return NULL;
796 
797 found_tgtdev:
798 	mpi3mr_tgtdev_get(tgtdev);
799 	return tgtdev;
800 }
801 
802 /**
803  * mpi3mr_get_tgtdev_by_perst_id -Get tgtdev from persistent ID
804  * @mrioc: Adapter instance reference
805  * @persist_id: Persistent ID
806  *
807  * Accessor to retrieve target device from the Persistent ID.
808  * Lock version
809  *
810  * Return: Target device reference.
811  */
812 static struct mpi3mr_tgt_dev *mpi3mr_get_tgtdev_by_perst_id(
813 	struct mpi3mr_ioc *mrioc, u16 persist_id)
814 {
815 	struct mpi3mr_tgt_dev *tgtdev;
816 	unsigned long flags;
817 
818 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
819 	tgtdev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, persist_id);
820 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
821 	return tgtdev;
822 }
823 
824 /**
825  * __mpi3mr_get_tgtdev_from_tgtpriv -Get tgtdev from tgt private
826  * @mrioc: Adapter instance reference
827  * @tgt_priv: Target private data
828  *
829  * Accessor to return target device from the target private
830  * data. Non Lock version
831  *
832  * Return: Target device reference.
833  */
834 static struct mpi3mr_tgt_dev  *__mpi3mr_get_tgtdev_from_tgtpriv(
835 	struct mpi3mr_ioc *mrioc, struct mpi3mr_stgt_priv_data *tgt_priv)
836 {
837 	struct mpi3mr_tgt_dev *tgtdev;
838 
839 	assert_spin_locked(&mrioc->tgtdev_lock);
840 	tgtdev = tgt_priv->tgt_dev;
841 	if (tgtdev)
842 		mpi3mr_tgtdev_get(tgtdev);
843 	return tgtdev;
844 }
845 
846 /**
847  * mpi3mr_set_io_divert_for_all_vd_in_tg -set divert for TG VDs
848  * @mrioc: Adapter instance reference
849  * @tg: Throttle group information pointer
850  * @divert_value: 1 or 0
851  *
852  * Accessor to set io_divert flag for each device associated
853  * with the given throttle group with the given value.
854  *
855  * Return: None.
856  */
857 static void mpi3mr_set_io_divert_for_all_vd_in_tg(struct mpi3mr_ioc *mrioc,
858 	struct mpi3mr_throttle_group_info *tg, u8 divert_value)
859 {
860 	unsigned long flags;
861 	struct mpi3mr_tgt_dev *tgtdev;
862 	struct mpi3mr_stgt_priv_data *tgt_priv;
863 
864 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
865 	list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
866 		if (tgtdev->starget && tgtdev->starget->hostdata) {
867 			tgt_priv = tgtdev->starget->hostdata;
868 			if (tgt_priv->throttle_group == tg)
869 				tgt_priv->io_divert = divert_value;
870 		}
871 	}
872 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
873 }
874 
875 /**
876  * mpi3mr_print_device_event_notice - print notice related to post processing of
877  *					device event after controller reset.
878  *
879  * @mrioc: Adapter instance reference
880  * @device_add: true for device add event and false for device removal event
881  *
882  * Return: None.
883  */
884 void mpi3mr_print_device_event_notice(struct mpi3mr_ioc *mrioc,
885 	bool device_add)
886 {
887 	ioc_notice(mrioc, "Device %s was in progress before the reset and\n",
888 	    (device_add ? "addition" : "removal"));
889 	ioc_notice(mrioc, "completed after reset, verify whether the exposed devices\n");
890 	ioc_notice(mrioc, "are matched with attached devices for correctness\n");
891 }
892 
893 /**
894  * mpi3mr_remove_tgtdev_from_host - Remove dev from upper layers
895  * @mrioc: Adapter instance reference
896  * @tgtdev: Target device structure
897  *
898  * Checks whether the device is exposed to upper layers and if it
899  * is then remove the device from upper layers by calling
900  * scsi_remove_target().
901  *
902  * Return: 0 on success, non zero on failure.
903  */
904 void mpi3mr_remove_tgtdev_from_host(struct mpi3mr_ioc *mrioc,
905 	struct mpi3mr_tgt_dev *tgtdev)
906 {
907 	struct mpi3mr_stgt_priv_data *tgt_priv;
908 
909 	ioc_info(mrioc, "%s :Removing handle(0x%04x), wwid(0x%016llx)\n",
910 	    __func__, tgtdev->dev_handle, (unsigned long long)tgtdev->wwid);
911 	if (tgtdev->starget && tgtdev->starget->hostdata) {
912 		tgt_priv = tgtdev->starget->hostdata;
913 		atomic_set(&tgt_priv->block_io, 0);
914 		tgt_priv->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
915 	}
916 
917 	if (!mrioc->sas_transport_enabled || (tgtdev->dev_type !=
918 	    MPI3_DEVICE_DEVFORM_SAS_SATA) || tgtdev->non_stl) {
919 		if (tgtdev->starget) {
920 			if (mrioc->current_event)
921 				mrioc->current_event->pending_at_sml = 1;
922 			scsi_remove_target(&tgtdev->starget->dev);
923 			tgtdev->host_exposed = 0;
924 			if (mrioc->current_event) {
925 				mrioc->current_event->pending_at_sml = 0;
926 				if (mrioc->current_event->discard) {
927 					mpi3mr_print_device_event_notice(mrioc,
928 					    false);
929 					return;
930 				}
931 			}
932 		}
933 	} else
934 		mpi3mr_remove_tgtdev_from_sas_transport(mrioc, tgtdev);
935 	mpi3mr_global_trigger(mrioc,
936 	    MPI3_DRIVER2_GLOBALTRIGGER_DEVICE_REMOVAL_ENABLED);
937 
938 	ioc_info(mrioc, "%s :Removed handle(0x%04x), wwid(0x%016llx)\n",
939 	    __func__, tgtdev->dev_handle, (unsigned long long)tgtdev->wwid);
940 }
941 
942 /**
943  * mpi3mr_report_tgtdev_to_host - Expose device to upper layers
944  * @mrioc: Adapter instance reference
945  * @perst_id: Persistent ID of the device
946  *
947  * Checks whether the device can be exposed to upper layers and
948  * if it is not then expose the device to upper layers by
949  * calling scsi_scan_target().
950  *
951  * Return: 0 on success, non zero on failure.
952  */
953 static int mpi3mr_report_tgtdev_to_host(struct mpi3mr_ioc *mrioc,
954 	u16 perst_id)
955 {
956 	int retval = 0;
957 	struct mpi3mr_tgt_dev *tgtdev;
958 
959 	if (mrioc->reset_in_progress || mrioc->pci_err_recovery)
960 		return -1;
961 
962 	tgtdev = mpi3mr_get_tgtdev_by_perst_id(mrioc, perst_id);
963 	if (!tgtdev) {
964 		retval = -1;
965 		goto out;
966 	}
967 	if (tgtdev->is_hidden || tgtdev->host_exposed) {
968 		retval = -1;
969 		goto out;
970 	}
971 	if (!mrioc->sas_transport_enabled || (tgtdev->dev_type !=
972 	    MPI3_DEVICE_DEVFORM_SAS_SATA) || tgtdev->non_stl){
973 		tgtdev->host_exposed = 1;
974 		if (mrioc->current_event)
975 			mrioc->current_event->pending_at_sml = 1;
976 		scsi_scan_target(&mrioc->shost->shost_gendev,
977 		    mrioc->scsi_device_channel, tgtdev->perst_id,
978 		    SCAN_WILD_CARD, SCSI_SCAN_INITIAL);
979 		if (!tgtdev->starget)
980 			tgtdev->host_exposed = 0;
981 		if (mrioc->current_event) {
982 			mrioc->current_event->pending_at_sml = 0;
983 			if (mrioc->current_event->discard) {
984 				mpi3mr_print_device_event_notice(mrioc, true);
985 				goto out;
986 			}
987 		}
988 	} else
989 		mpi3mr_report_tgtdev_to_sas_transport(mrioc, tgtdev);
990 out:
991 	if (tgtdev)
992 		mpi3mr_tgtdev_put(tgtdev);
993 
994 	return retval;
995 }
996 
997 /**
998  * mpi3mr_change_queue_depth- Change QD callback handler
999  * @sdev: SCSI device reference
1000  * @q_depth: Queue depth
1001  *
1002  * Validate and limit QD and call scsi_change_queue_depth.
1003  *
1004  * Return: return value of scsi_change_queue_depth
1005  */
1006 static int mpi3mr_change_queue_depth(struct scsi_device *sdev,
1007 	int q_depth)
1008 {
1009 	struct scsi_target *starget = scsi_target(sdev);
1010 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1011 	int retval = 0;
1012 
1013 	if (!sdev->tagged_supported)
1014 		q_depth = 1;
1015 	if (q_depth > shost->can_queue)
1016 		q_depth = shost->can_queue;
1017 	else if (!q_depth)
1018 		q_depth = MPI3MR_DEFAULT_SDEV_QD;
1019 	retval = scsi_change_queue_depth(sdev, q_depth);
1020 	sdev->max_queue_depth = sdev->queue_depth;
1021 
1022 	return retval;
1023 }
1024 
1025 static void mpi3mr_configure_nvme_dev(struct mpi3mr_tgt_dev *tgt_dev,
1026 		struct queue_limits *lim)
1027 {
1028 	u8 pgsz = tgt_dev->dev_spec.pcie_inf.pgsz ? : MPI3MR_DEFAULT_PGSZEXP;
1029 
1030 	lim->max_hw_sectors = tgt_dev->dev_spec.pcie_inf.mdts / 512;
1031 	lim->virt_boundary_mask = (1 << pgsz) - 1;
1032 }
1033 
1034 static void mpi3mr_configure_tgt_dev(struct mpi3mr_tgt_dev *tgt_dev,
1035 		struct queue_limits *lim)
1036 {
1037 	if (tgt_dev->dev_type == MPI3_DEVICE_DEVFORM_PCIE &&
1038 	    (tgt_dev->dev_spec.pcie_inf.dev_info &
1039 	     MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) ==
1040 			MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE)
1041 		mpi3mr_configure_nvme_dev(tgt_dev, lim);
1042 }
1043 
1044 /**
1045  * mpi3mr_update_sdev - Update SCSI device information
1046  * @sdev: SCSI device reference
1047  * @data: target device reference
1048  *
1049  * This is an iterator function called for each SCSI device in a
1050  * target to update the target specific information into each
1051  * SCSI device.
1052  *
1053  * Return: Nothing.
1054  */
1055 static void
1056 mpi3mr_update_sdev(struct scsi_device *sdev, void *data)
1057 {
1058 	struct mpi3mr_tgt_dev *tgtdev;
1059 	struct queue_limits lim;
1060 
1061 	tgtdev = (struct mpi3mr_tgt_dev *)data;
1062 	if (!tgtdev)
1063 		return;
1064 
1065 	mpi3mr_change_queue_depth(sdev, tgtdev->q_depth);
1066 
1067 	lim = queue_limits_start_update(sdev->request_queue);
1068 	mpi3mr_configure_tgt_dev(tgtdev, &lim);
1069 	WARN_ON_ONCE(queue_limits_commit_update(sdev->request_queue, &lim));
1070 }
1071 
1072 /**
1073  * mpi3mr_refresh_tgtdevs - Refresh target device exposure
1074  * @mrioc: Adapter instance reference
1075  *
1076  * This is executed post controller reset to identify any
1077  * missing devices during reset and remove from the upper layers
1078  * or expose any newly detected device to the upper layers.
1079  *
1080  * Return: Nothing.
1081  */
1082 static void mpi3mr_refresh_tgtdevs(struct mpi3mr_ioc *mrioc)
1083 {
1084 	struct mpi3mr_tgt_dev *tgtdev, *tgtdev_next;
1085 	struct mpi3mr_stgt_priv_data *tgt_priv;
1086 
1087 	dprint_reset(mrioc, "refresh target devices: check for removals\n");
1088 	list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
1089 	    list) {
1090 		if (((tgtdev->dev_handle == MPI3MR_INVALID_DEV_HANDLE) ||
1091 		     tgtdev->is_hidden) &&
1092 		     tgtdev->host_exposed && tgtdev->starget &&
1093 		     tgtdev->starget->hostdata) {
1094 			tgt_priv = tgtdev->starget->hostdata;
1095 			tgt_priv->dev_removed = 1;
1096 			atomic_set(&tgt_priv->block_io, 0);
1097 		}
1098 	}
1099 
1100 	list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
1101 	    list) {
1102 		if (tgtdev->dev_handle == MPI3MR_INVALID_DEV_HANDLE) {
1103 			dprint_reset(mrioc, "removing target device with perst_id(%d)\n",
1104 			    tgtdev->perst_id);
1105 			if (tgtdev->host_exposed)
1106 				mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1107 			mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, true);
1108 			mpi3mr_tgtdev_put(tgtdev);
1109 		} else if (tgtdev->is_hidden & tgtdev->host_exposed) {
1110 			dprint_reset(mrioc, "hiding target device with perst_id(%d)\n",
1111 				     tgtdev->perst_id);
1112 			mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1113 		}
1114 	}
1115 
1116 	tgtdev = NULL;
1117 	list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
1118 		if ((tgtdev->dev_handle != MPI3MR_INVALID_DEV_HANDLE) &&
1119 		    !tgtdev->is_hidden) {
1120 			if (!tgtdev->host_exposed)
1121 				mpi3mr_report_tgtdev_to_host(mrioc,
1122 							     tgtdev->perst_id);
1123 			else if (tgtdev->starget)
1124 				starget_for_each_device(tgtdev->starget,
1125 							(void *)tgtdev, mpi3mr_update_sdev);
1126 	}
1127 	}
1128 }
1129 
1130 /**
1131  * mpi3mr_update_tgtdev - DevStatusChange evt bottomhalf
1132  * @mrioc: Adapter instance reference
1133  * @tgtdev: Target device internal structure
1134  * @dev_pg0: New device page0
1135  * @is_added: Flag to indicate the device is just added
1136  *
1137  * Update the information from the device page0 into the driver
1138  * cached target device structure.
1139  *
1140  * Return: Nothing.
1141  */
1142 static void mpi3mr_update_tgtdev(struct mpi3mr_ioc *mrioc,
1143 	struct mpi3mr_tgt_dev *tgtdev, struct mpi3_device_page0 *dev_pg0,
1144 	bool is_added)
1145 {
1146 	u16 flags = 0;
1147 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
1148 	struct mpi3mr_enclosure_node *enclosure_dev = NULL;
1149 	u8 prot_mask = 0;
1150 
1151 	tgtdev->perst_id = le16_to_cpu(dev_pg0->persistent_id);
1152 	tgtdev->dev_handle = le16_to_cpu(dev_pg0->dev_handle);
1153 	tgtdev->dev_type = dev_pg0->device_form;
1154 	tgtdev->io_unit_port = dev_pg0->io_unit_port;
1155 	tgtdev->encl_handle = le16_to_cpu(dev_pg0->enclosure_handle);
1156 	tgtdev->parent_handle = le16_to_cpu(dev_pg0->parent_dev_handle);
1157 	tgtdev->slot = le16_to_cpu(dev_pg0->slot);
1158 	tgtdev->q_depth = le16_to_cpu(dev_pg0->queue_depth);
1159 	tgtdev->wwid = le64_to_cpu(dev_pg0->wwid);
1160 	tgtdev->devpg0_flag = le16_to_cpu(dev_pg0->flags);
1161 
1162 	if (tgtdev->encl_handle)
1163 		enclosure_dev = mpi3mr_enclosure_find_by_handle(mrioc,
1164 		    tgtdev->encl_handle);
1165 	if (enclosure_dev)
1166 		tgtdev->enclosure_logical_id = le64_to_cpu(
1167 		    enclosure_dev->pg0.enclosure_logical_id);
1168 
1169 	flags = tgtdev->devpg0_flag;
1170 
1171 	tgtdev->is_hidden = (flags & MPI3_DEVICE0_FLAGS_HIDDEN);
1172 
1173 	if (is_added == true)
1174 		tgtdev->io_throttle_enabled =
1175 		    (flags & MPI3_DEVICE0_FLAGS_IO_THROTTLING_REQUIRED) ? 1 : 0;
1176 
1177 	switch (flags & MPI3_DEVICE0_FLAGS_MAX_WRITE_SAME_MASK) {
1178 	case MPI3_DEVICE0_FLAGS_MAX_WRITE_SAME_256_LB:
1179 		tgtdev->wslen = MPI3MR_WRITE_SAME_MAX_LEN_256_BLKS;
1180 		break;
1181 	case MPI3_DEVICE0_FLAGS_MAX_WRITE_SAME_2048_LB:
1182 		tgtdev->wslen = MPI3MR_WRITE_SAME_MAX_LEN_2048_BLKS;
1183 		break;
1184 	case MPI3_DEVICE0_FLAGS_MAX_WRITE_SAME_NO_LIMIT:
1185 	default:
1186 		tgtdev->wslen = 0;
1187 		break;
1188 	}
1189 
1190 	if (tgtdev->starget && tgtdev->starget->hostdata) {
1191 		scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
1192 		    tgtdev->starget->hostdata;
1193 		scsi_tgt_priv_data->perst_id = tgtdev->perst_id;
1194 		scsi_tgt_priv_data->dev_handle = tgtdev->dev_handle;
1195 		scsi_tgt_priv_data->dev_type = tgtdev->dev_type;
1196 		scsi_tgt_priv_data->io_throttle_enabled =
1197 		    tgtdev->io_throttle_enabled;
1198 		if (is_added == true)
1199 			atomic_set(&scsi_tgt_priv_data->block_io, 0);
1200 		scsi_tgt_priv_data->wslen = tgtdev->wslen;
1201 	}
1202 
1203 	switch (dev_pg0->access_status) {
1204 	case MPI3_DEVICE0_ASTATUS_NO_ERRORS:
1205 	case MPI3_DEVICE0_ASTATUS_PREPARE:
1206 	case MPI3_DEVICE0_ASTATUS_NEEDS_INITIALIZATION:
1207 	case MPI3_DEVICE0_ASTATUS_DEVICE_MISSING_DELAY:
1208 		break;
1209 	default:
1210 		tgtdev->is_hidden = 1;
1211 		break;
1212 	}
1213 
1214 	switch (tgtdev->dev_type) {
1215 	case MPI3_DEVICE_DEVFORM_SAS_SATA:
1216 	{
1217 		struct mpi3_device0_sas_sata_format *sasinf =
1218 		    &dev_pg0->device_specific.sas_sata_format;
1219 		u16 dev_info = le16_to_cpu(sasinf->device_info);
1220 
1221 		tgtdev->dev_spec.sas_sata_inf.dev_info = dev_info;
1222 		tgtdev->dev_spec.sas_sata_inf.sas_address =
1223 		    le64_to_cpu(sasinf->sas_address);
1224 		tgtdev->dev_spec.sas_sata_inf.phy_id = sasinf->phy_num;
1225 		tgtdev->dev_spec.sas_sata_inf.attached_phy_id =
1226 		    sasinf->attached_phy_identifier;
1227 		if ((dev_info & MPI3_SAS_DEVICE_INFO_DEVICE_TYPE_MASK) !=
1228 		    MPI3_SAS_DEVICE_INFO_DEVICE_TYPE_END_DEVICE)
1229 			tgtdev->is_hidden = 1;
1230 		else if (!(dev_info & (MPI3_SAS_DEVICE_INFO_STP_SATA_TARGET |
1231 		    MPI3_SAS_DEVICE_INFO_SSP_TARGET)))
1232 			tgtdev->is_hidden = 1;
1233 
1234 		if (((tgtdev->devpg0_flag &
1235 		    MPI3_DEVICE0_FLAGS_ATT_METHOD_DIR_ATTACHED)
1236 		    && (tgtdev->devpg0_flag &
1237 		    MPI3_DEVICE0_FLAGS_ATT_METHOD_VIRTUAL)) ||
1238 		    (tgtdev->parent_handle == 0xFFFF))
1239 			tgtdev->non_stl = 1;
1240 		if (tgtdev->dev_spec.sas_sata_inf.hba_port)
1241 			tgtdev->dev_spec.sas_sata_inf.hba_port->port_id =
1242 			    dev_pg0->io_unit_port;
1243 		break;
1244 	}
1245 	case MPI3_DEVICE_DEVFORM_PCIE:
1246 	{
1247 		struct mpi3_device0_pcie_format *pcieinf =
1248 		    &dev_pg0->device_specific.pcie_format;
1249 		u16 dev_info = le16_to_cpu(pcieinf->device_info);
1250 
1251 		tgtdev->dev_spec.pcie_inf.dev_info = dev_info;
1252 		tgtdev->dev_spec.pcie_inf.capb =
1253 		    le32_to_cpu(pcieinf->capabilities);
1254 		tgtdev->dev_spec.pcie_inf.mdts = MPI3MR_DEFAULT_MDTS;
1255 		/* 2^12 = 4096 */
1256 		tgtdev->dev_spec.pcie_inf.pgsz = 12;
1257 		if (dev_pg0->access_status == MPI3_DEVICE0_ASTATUS_NO_ERRORS) {
1258 			tgtdev->dev_spec.pcie_inf.mdts =
1259 			    le32_to_cpu(pcieinf->maximum_data_transfer_size);
1260 			tgtdev->dev_spec.pcie_inf.pgsz = pcieinf->page_size;
1261 			tgtdev->dev_spec.pcie_inf.reset_to =
1262 			    max_t(u8, pcieinf->controller_reset_to,
1263 			     MPI3MR_INTADMCMD_TIMEOUT);
1264 			tgtdev->dev_spec.pcie_inf.abort_to =
1265 			    max_t(u8, pcieinf->nvme_abort_to,
1266 			    MPI3MR_INTADMCMD_TIMEOUT);
1267 		}
1268 		if (tgtdev->dev_spec.pcie_inf.mdts > (1024 * 1024))
1269 			tgtdev->dev_spec.pcie_inf.mdts = (1024 * 1024);
1270 		if (((dev_info & MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) !=
1271 		    MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) &&
1272 		    ((dev_info & MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) !=
1273 		    MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_SCSI_DEVICE))
1274 			tgtdev->is_hidden = 1;
1275 		tgtdev->non_stl = 1;
1276 		if (!mrioc->shost)
1277 			break;
1278 		prot_mask = scsi_host_get_prot(mrioc->shost);
1279 		if (prot_mask & SHOST_DIX_TYPE0_PROTECTION) {
1280 			scsi_host_set_prot(mrioc->shost, prot_mask & 0x77);
1281 			ioc_info(mrioc,
1282 			    "%s : Disabling DIX0 prot capability\n", __func__);
1283 			ioc_info(mrioc,
1284 			    "because HBA does not support DIX0 operation on NVME drives\n");
1285 		}
1286 		break;
1287 	}
1288 	case MPI3_DEVICE_DEVFORM_VD:
1289 	{
1290 		struct mpi3_device0_vd_format *vdinf =
1291 		    &dev_pg0->device_specific.vd_format;
1292 		struct mpi3mr_throttle_group_info *tg = NULL;
1293 		u16 vdinf_io_throttle_group =
1294 		    le16_to_cpu(vdinf->io_throttle_group);
1295 
1296 		tgtdev->dev_spec.vd_inf.state = vdinf->vd_state;
1297 		if (vdinf->vd_state == MPI3_DEVICE0_VD_STATE_OFFLINE)
1298 			tgtdev->is_hidden = 1;
1299 		tgtdev->non_stl = 1;
1300 		tgtdev->dev_spec.vd_inf.tg_id = vdinf_io_throttle_group;
1301 		tgtdev->dev_spec.vd_inf.tg_high =
1302 		    le16_to_cpu(vdinf->io_throttle_group_high) * 2048;
1303 		tgtdev->dev_spec.vd_inf.tg_low =
1304 		    le16_to_cpu(vdinf->io_throttle_group_low) * 2048;
1305 		if (vdinf_io_throttle_group < mrioc->num_io_throttle_group) {
1306 			tg = mrioc->throttle_groups + vdinf_io_throttle_group;
1307 			tg->id = vdinf_io_throttle_group;
1308 			tg->high = tgtdev->dev_spec.vd_inf.tg_high;
1309 			tg->low = tgtdev->dev_spec.vd_inf.tg_low;
1310 			tg->qd_reduction =
1311 			    tgtdev->dev_spec.vd_inf.tg_qd_reduction;
1312 			if (is_added == true)
1313 				tg->fw_qd = tgtdev->q_depth;
1314 			tg->modified_qd = tgtdev->q_depth;
1315 		}
1316 		tgtdev->dev_spec.vd_inf.tg = tg;
1317 		if (scsi_tgt_priv_data)
1318 			scsi_tgt_priv_data->throttle_group = tg;
1319 		break;
1320 	}
1321 	default:
1322 		break;
1323 	}
1324 }
1325 
1326 /**
1327  * mpi3mr_devstatuschg_evt_bh - DevStatusChange evt bottomhalf
1328  * @mrioc: Adapter instance reference
1329  * @fwevt: Firmware event information.
1330  *
1331  * Process Device status Change event and based on device's new
1332  * information, either expose the device to the upper layers, or
1333  * remove the device from upper layers.
1334  *
1335  * Return: Nothing.
1336  */
1337 static void mpi3mr_devstatuschg_evt_bh(struct mpi3mr_ioc *mrioc,
1338 	struct mpi3mr_fwevt *fwevt)
1339 {
1340 	u16 dev_handle = 0;
1341 	u8 uhide = 0, delete = 0, cleanup = 0;
1342 	struct mpi3mr_tgt_dev *tgtdev = NULL;
1343 	struct mpi3_event_data_device_status_change *evtdata =
1344 	    (struct mpi3_event_data_device_status_change *)fwevt->event_data;
1345 
1346 	dev_handle = le16_to_cpu(evtdata->dev_handle);
1347 	ioc_info(mrioc,
1348 	    "%s :device status change: handle(0x%04x): reason code(0x%x)\n",
1349 	    __func__, dev_handle, evtdata->reason_code);
1350 	switch (evtdata->reason_code) {
1351 	case MPI3_EVENT_DEV_STAT_RC_HIDDEN:
1352 		delete = 1;
1353 		break;
1354 	case MPI3_EVENT_DEV_STAT_RC_NOT_HIDDEN:
1355 		uhide = 1;
1356 		break;
1357 	case MPI3_EVENT_DEV_STAT_RC_VD_NOT_RESPONDING:
1358 		delete = 1;
1359 		cleanup = 1;
1360 		break;
1361 	default:
1362 		ioc_info(mrioc, "%s :Unhandled reason code(0x%x)\n", __func__,
1363 		    evtdata->reason_code);
1364 		break;
1365 	}
1366 
1367 	tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
1368 	if (!tgtdev)
1369 		goto out;
1370 	if (uhide) {
1371 		tgtdev->is_hidden = 0;
1372 		if (!tgtdev->host_exposed)
1373 			mpi3mr_report_tgtdev_to_host(mrioc, tgtdev->perst_id);
1374 	}
1375 
1376 	if (delete)
1377 		mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1378 
1379 	if (cleanup) {
1380 		mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, false);
1381 		mpi3mr_tgtdev_put(tgtdev);
1382 	}
1383 
1384 out:
1385 	if (tgtdev)
1386 		mpi3mr_tgtdev_put(tgtdev);
1387 }
1388 
1389 /**
1390  * mpi3mr_devinfochg_evt_bh - DeviceInfoChange evt bottomhalf
1391  * @mrioc: Adapter instance reference
1392  * @dev_pg0: New device page0
1393  *
1394  * Process Device Info Change event and based on device's new
1395  * information, either expose the device to the upper layers, or
1396  * remove the device from upper layers or update the details of
1397  * the device.
1398  *
1399  * Return: Nothing.
1400  */
1401 static void mpi3mr_devinfochg_evt_bh(struct mpi3mr_ioc *mrioc,
1402 	struct mpi3_device_page0 *dev_pg0)
1403 {
1404 	struct mpi3mr_tgt_dev *tgtdev = NULL;
1405 	u16 dev_handle = 0, perst_id = 0;
1406 
1407 	perst_id = le16_to_cpu(dev_pg0->persistent_id);
1408 	dev_handle = le16_to_cpu(dev_pg0->dev_handle);
1409 	ioc_info(mrioc,
1410 	    "%s :Device info change: handle(0x%04x): persist_id(0x%x)\n",
1411 	    __func__, dev_handle, perst_id);
1412 	tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
1413 	if (!tgtdev)
1414 		goto out;
1415 	mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0, false);
1416 	if (!tgtdev->is_hidden && !tgtdev->host_exposed)
1417 		mpi3mr_report_tgtdev_to_host(mrioc, perst_id);
1418 	if (tgtdev->is_hidden && tgtdev->host_exposed)
1419 		mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1420 	if (!tgtdev->is_hidden && tgtdev->host_exposed && tgtdev->starget)
1421 		starget_for_each_device(tgtdev->starget, (void *)tgtdev,
1422 		    mpi3mr_update_sdev);
1423 out:
1424 	if (tgtdev)
1425 		mpi3mr_tgtdev_put(tgtdev);
1426 }
1427 
1428 /**
1429  * mpi3mr_free_enclosure_list - release enclosures
1430  * @mrioc: Adapter instance reference
1431  *
1432  * Free memory allocated during encloure add.
1433  *
1434  * Return nothing.
1435  */
1436 void mpi3mr_free_enclosure_list(struct mpi3mr_ioc *mrioc)
1437 {
1438 	struct mpi3mr_enclosure_node *enclosure_dev, *enclosure_dev_next;
1439 
1440 	list_for_each_entry_safe(enclosure_dev,
1441 	    enclosure_dev_next, &mrioc->enclosure_list, list) {
1442 		list_del(&enclosure_dev->list);
1443 		kfree(enclosure_dev);
1444 	}
1445 }
1446 
1447 /**
1448  * mpi3mr_enclosure_find_by_handle - enclosure search by handle
1449  * @mrioc: Adapter instance reference
1450  * @handle: Firmware device handle of the enclosure
1451  *
1452  * This searches for enclosure device based on handle, then returns the
1453  * enclosure object.
1454  *
1455  * Return: Enclosure object reference or NULL
1456  */
1457 struct mpi3mr_enclosure_node *mpi3mr_enclosure_find_by_handle(
1458 	struct mpi3mr_ioc *mrioc, u16 handle)
1459 {
1460 	struct mpi3mr_enclosure_node *enclosure_dev, *r = NULL;
1461 
1462 	list_for_each_entry(enclosure_dev, &mrioc->enclosure_list, list) {
1463 		if (le16_to_cpu(enclosure_dev->pg0.enclosure_handle) != handle)
1464 			continue;
1465 		r = enclosure_dev;
1466 		goto out;
1467 	}
1468 out:
1469 	return r;
1470 }
1471 
1472 /**
1473  * mpi3mr_process_trigger_data_event_bh - Process trigger event
1474  * data
1475  * @mrioc: Adapter instance reference
1476  * @event_data: Event data
1477  *
1478  * This function releases diage buffers or issues diag fault
1479  * based on trigger conditions
1480  *
1481  * Return: Nothing
1482  */
1483 static void mpi3mr_process_trigger_data_event_bh(struct mpi3mr_ioc *mrioc,
1484 	struct trigger_event_data *event_data)
1485 {
1486 	struct diag_buffer_desc *trace_hdb = event_data->trace_hdb;
1487 	struct diag_buffer_desc *fw_hdb = event_data->fw_hdb;
1488 	unsigned long flags;
1489 	int retval = 0;
1490 	u8 trigger_type = event_data->trigger_type;
1491 	union mpi3mr_trigger_data *trigger_data =
1492 		&event_data->trigger_specific_data;
1493 
1494 	if (event_data->snapdump)  {
1495 		if (trace_hdb)
1496 			mpi3mr_set_trigger_data_in_hdb(trace_hdb, trigger_type,
1497 			    trigger_data, 1);
1498 		if (fw_hdb)
1499 			mpi3mr_set_trigger_data_in_hdb(fw_hdb, trigger_type,
1500 			    trigger_data, 1);
1501 		mpi3mr_soft_reset_handler(mrioc,
1502 			    MPI3MR_RESET_FROM_TRIGGER, 1);
1503 		return;
1504 	}
1505 
1506 	if (trace_hdb) {
1507 		retval = mpi3mr_issue_diag_buf_release(mrioc, trace_hdb);
1508 		if (!retval) {
1509 			mpi3mr_set_trigger_data_in_hdb(trace_hdb, trigger_type,
1510 			    trigger_data, 1);
1511 		}
1512 		spin_lock_irqsave(&mrioc->trigger_lock, flags);
1513 		mrioc->trace_release_trigger_active = false;
1514 		spin_unlock_irqrestore(&mrioc->trigger_lock, flags);
1515 	}
1516 	if (fw_hdb) {
1517 		retval = mpi3mr_issue_diag_buf_release(mrioc, fw_hdb);
1518 		if (!retval) {
1519 			mpi3mr_set_trigger_data_in_hdb(fw_hdb, trigger_type,
1520 		    trigger_data, 1);
1521 		}
1522 		spin_lock_irqsave(&mrioc->trigger_lock, flags);
1523 		mrioc->fw_release_trigger_active = false;
1524 		spin_unlock_irqrestore(&mrioc->trigger_lock, flags);
1525 	}
1526 }
1527 
1528 /**
1529  * mpi3mr_encldev_add_chg_evt_debug - debug for enclosure event
1530  * @mrioc: Adapter instance reference
1531  * @encl_pg0: Enclosure page 0.
1532  * @is_added: Added event or not
1533  *
1534  * Return nothing.
1535  */
1536 static void mpi3mr_encldev_add_chg_evt_debug(struct mpi3mr_ioc *mrioc,
1537 	struct mpi3_enclosure_page0 *encl_pg0, u8 is_added)
1538 {
1539 	char *reason_str = NULL;
1540 
1541 	if (!(mrioc->logging_level & MPI3_DEBUG_EVENT_WORK_TASK))
1542 		return;
1543 
1544 	if (is_added)
1545 		reason_str = "enclosure added";
1546 	else
1547 		reason_str = "enclosure dev status changed";
1548 
1549 	ioc_info(mrioc,
1550 	    "%s: handle(0x%04x), enclosure logical id(0x%016llx)\n",
1551 	    reason_str, le16_to_cpu(encl_pg0->enclosure_handle),
1552 	    (unsigned long long)le64_to_cpu(encl_pg0->enclosure_logical_id));
1553 	ioc_info(mrioc,
1554 	    "number of slots(%d), port(%d), flags(0x%04x), present(%d)\n",
1555 	    le16_to_cpu(encl_pg0->num_slots), encl_pg0->io_unit_port,
1556 	    le16_to_cpu(encl_pg0->flags),
1557 	    ((le16_to_cpu(encl_pg0->flags) &
1558 	      MPI3_ENCLS0_FLAGS_ENCL_DEV_PRESENT_MASK) >> 4));
1559 }
1560 
1561 /**
1562  * mpi3mr_encldev_add_chg_evt_bh - Enclosure evt bottomhalf
1563  * @mrioc: Adapter instance reference
1564  * @fwevt: Firmware event reference
1565  *
1566  * Prints information about the Enclosure device status or
1567  * Enclosure add events if logging is enabled and add or remove
1568  * the enclosure from the controller's internal list of
1569  * enclosures.
1570  *
1571  * Return: Nothing.
1572  */
1573 static void mpi3mr_encldev_add_chg_evt_bh(struct mpi3mr_ioc *mrioc,
1574 	struct mpi3mr_fwevt *fwevt)
1575 {
1576 	struct mpi3mr_enclosure_node *enclosure_dev = NULL;
1577 	struct mpi3_enclosure_page0 *encl_pg0;
1578 	u16 encl_handle;
1579 	u8 added, present;
1580 
1581 	encl_pg0 = (struct mpi3_enclosure_page0 *) fwevt->event_data;
1582 	added = (fwevt->event_id == MPI3_EVENT_ENCL_DEVICE_ADDED) ? 1 : 0;
1583 	mpi3mr_encldev_add_chg_evt_debug(mrioc, encl_pg0, added);
1584 
1585 
1586 	encl_handle = le16_to_cpu(encl_pg0->enclosure_handle);
1587 	present = ((le16_to_cpu(encl_pg0->flags) &
1588 	      MPI3_ENCLS0_FLAGS_ENCL_DEV_PRESENT_MASK) >> 4);
1589 
1590 	if (encl_handle)
1591 		enclosure_dev = mpi3mr_enclosure_find_by_handle(mrioc,
1592 		    encl_handle);
1593 	if (!enclosure_dev && present) {
1594 		enclosure_dev =
1595 			kzalloc(sizeof(struct mpi3mr_enclosure_node),
1596 			    GFP_KERNEL);
1597 		if (!enclosure_dev)
1598 			return;
1599 		list_add_tail(&enclosure_dev->list,
1600 		    &mrioc->enclosure_list);
1601 	}
1602 	if (enclosure_dev) {
1603 		if (!present) {
1604 			list_del(&enclosure_dev->list);
1605 			kfree(enclosure_dev);
1606 		} else
1607 			memcpy(&enclosure_dev->pg0, encl_pg0,
1608 			    sizeof(enclosure_dev->pg0));
1609 
1610 	}
1611 }
1612 
1613 /**
1614  * mpi3mr_sastopochg_evt_debug - SASTopoChange details
1615  * @mrioc: Adapter instance reference
1616  * @event_data: SAS topology change list event data
1617  *
1618  * Prints information about the SAS topology change event.
1619  *
1620  * Return: Nothing.
1621  */
1622 static void
1623 mpi3mr_sastopochg_evt_debug(struct mpi3mr_ioc *mrioc,
1624 	struct mpi3_event_data_sas_topology_change_list *event_data)
1625 {
1626 	int i;
1627 	u16 handle;
1628 	u8 reason_code, phy_number;
1629 	char *status_str = NULL;
1630 	u8 link_rate, prev_link_rate;
1631 
1632 	switch (event_data->exp_status) {
1633 	case MPI3_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
1634 		status_str = "remove";
1635 		break;
1636 	case MPI3_EVENT_SAS_TOPO_ES_RESPONDING:
1637 		status_str =  "responding";
1638 		break;
1639 	case MPI3_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
1640 		status_str = "remove delay";
1641 		break;
1642 	case MPI3_EVENT_SAS_TOPO_ES_NO_EXPANDER:
1643 		status_str = "direct attached";
1644 		break;
1645 	default:
1646 		status_str = "unknown status";
1647 		break;
1648 	}
1649 	ioc_info(mrioc, "%s :sas topology change: (%s)\n",
1650 	    __func__, status_str);
1651 	ioc_info(mrioc,
1652 	    "%s :\texpander_handle(0x%04x), port(%d), enclosure_handle(0x%04x) start_phy(%02d), num_entries(%d)\n",
1653 	    __func__, le16_to_cpu(event_data->expander_dev_handle),
1654 	    event_data->io_unit_port,
1655 	    le16_to_cpu(event_data->enclosure_handle),
1656 	    event_data->start_phy_num, event_data->num_entries);
1657 	for (i = 0; i < event_data->num_entries; i++) {
1658 		handle = le16_to_cpu(event_data->phy_entry[i].attached_dev_handle);
1659 		if (!handle)
1660 			continue;
1661 		phy_number = event_data->start_phy_num + i;
1662 		reason_code = event_data->phy_entry[i].status &
1663 		    MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
1664 		switch (reason_code) {
1665 		case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
1666 			status_str = "target remove";
1667 			break;
1668 		case MPI3_EVENT_SAS_TOPO_PHY_RC_DELAY_NOT_RESPONDING:
1669 			status_str = "delay target remove";
1670 			break;
1671 		case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
1672 			status_str = "link status change";
1673 			break;
1674 		case MPI3_EVENT_SAS_TOPO_PHY_RC_NO_CHANGE:
1675 			status_str = "link status no change";
1676 			break;
1677 		case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
1678 			status_str = "target responding";
1679 			break;
1680 		default:
1681 			status_str = "unknown";
1682 			break;
1683 		}
1684 		link_rate = event_data->phy_entry[i].link_rate >> 4;
1685 		prev_link_rate = event_data->phy_entry[i].link_rate & 0xF;
1686 		ioc_info(mrioc,
1687 		    "%s :\tphy(%02d), attached_handle(0x%04x): %s: link rate: new(0x%02x), old(0x%02x)\n",
1688 		    __func__, phy_number, handle, status_str, link_rate,
1689 		    prev_link_rate);
1690 	}
1691 }
1692 
1693 /**
1694  * mpi3mr_sastopochg_evt_bh - SASTopologyChange evt bottomhalf
1695  * @mrioc: Adapter instance reference
1696  * @fwevt: Firmware event reference
1697  *
1698  * Prints information about the SAS topology change event and
1699  * for "not responding" event code, removes the device from the
1700  * upper layers.
1701  *
1702  * Return: Nothing.
1703  */
1704 static void mpi3mr_sastopochg_evt_bh(struct mpi3mr_ioc *mrioc,
1705 	struct mpi3mr_fwevt *fwevt)
1706 {
1707 	struct mpi3_event_data_sas_topology_change_list *event_data =
1708 	    (struct mpi3_event_data_sas_topology_change_list *)fwevt->event_data;
1709 	int i;
1710 	u16 handle;
1711 	u8 reason_code;
1712 	u64 exp_sas_address = 0, parent_sas_address = 0;
1713 	struct mpi3mr_hba_port *hba_port = NULL;
1714 	struct mpi3mr_tgt_dev *tgtdev = NULL;
1715 	struct mpi3mr_sas_node *sas_expander = NULL;
1716 	unsigned long flags;
1717 	u8 link_rate, prev_link_rate, parent_phy_number;
1718 
1719 	mpi3mr_sastopochg_evt_debug(mrioc, event_data);
1720 	if (mrioc->sas_transport_enabled) {
1721 		hba_port = mpi3mr_get_hba_port_by_id(mrioc,
1722 		    event_data->io_unit_port);
1723 		if (le16_to_cpu(event_data->expander_dev_handle)) {
1724 			spin_lock_irqsave(&mrioc->sas_node_lock, flags);
1725 			sas_expander = __mpi3mr_expander_find_by_handle(mrioc,
1726 			    le16_to_cpu(event_data->expander_dev_handle));
1727 			if (sas_expander) {
1728 				exp_sas_address = sas_expander->sas_address;
1729 				hba_port = sas_expander->hba_port;
1730 			}
1731 			spin_unlock_irqrestore(&mrioc->sas_node_lock, flags);
1732 			parent_sas_address = exp_sas_address;
1733 		} else
1734 			parent_sas_address = mrioc->sas_hba.sas_address;
1735 	}
1736 
1737 	for (i = 0; i < event_data->num_entries; i++) {
1738 		if (fwevt->discard)
1739 			return;
1740 		handle = le16_to_cpu(event_data->phy_entry[i].attached_dev_handle);
1741 		if (!handle)
1742 			continue;
1743 		tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
1744 		if (!tgtdev)
1745 			continue;
1746 
1747 		reason_code = event_data->phy_entry[i].status &
1748 		    MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
1749 
1750 		switch (reason_code) {
1751 		case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
1752 			if (tgtdev->host_exposed)
1753 				mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1754 			mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, false);
1755 			mpi3mr_tgtdev_put(tgtdev);
1756 			break;
1757 		case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
1758 		case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
1759 		case MPI3_EVENT_SAS_TOPO_PHY_RC_NO_CHANGE:
1760 		{
1761 			if (!mrioc->sas_transport_enabled || tgtdev->non_stl
1762 			    || tgtdev->is_hidden)
1763 				break;
1764 			link_rate = event_data->phy_entry[i].link_rate >> 4;
1765 			prev_link_rate = event_data->phy_entry[i].link_rate & 0xF;
1766 			if (link_rate == prev_link_rate)
1767 				break;
1768 			if (!parent_sas_address)
1769 				break;
1770 			parent_phy_number = event_data->start_phy_num + i;
1771 			mpi3mr_update_links(mrioc, parent_sas_address, handle,
1772 			    parent_phy_number, link_rate, hba_port);
1773 			break;
1774 		}
1775 		default:
1776 			break;
1777 		}
1778 		if (tgtdev)
1779 			mpi3mr_tgtdev_put(tgtdev);
1780 	}
1781 
1782 	if (mrioc->sas_transport_enabled && (event_data->exp_status ==
1783 	    MPI3_EVENT_SAS_TOPO_ES_NOT_RESPONDING)) {
1784 		if (sas_expander)
1785 			mpi3mr_expander_remove(mrioc, exp_sas_address,
1786 			    hba_port);
1787 	}
1788 }
1789 
1790 /**
1791  * mpi3mr_pcietopochg_evt_debug - PCIeTopoChange details
1792  * @mrioc: Adapter instance reference
1793  * @event_data: PCIe topology change list event data
1794  *
1795  * Prints information about the PCIe topology change event.
1796  *
1797  * Return: Nothing.
1798  */
1799 static void
1800 mpi3mr_pcietopochg_evt_debug(struct mpi3mr_ioc *mrioc,
1801 	struct mpi3_event_data_pcie_topology_change_list *event_data)
1802 {
1803 	int i;
1804 	u16 handle;
1805 	u16 reason_code;
1806 	u8 port_number;
1807 	char *status_str = NULL;
1808 	u8 link_rate, prev_link_rate;
1809 
1810 	switch (event_data->switch_status) {
1811 	case MPI3_EVENT_PCIE_TOPO_SS_NOT_RESPONDING:
1812 		status_str = "remove";
1813 		break;
1814 	case MPI3_EVENT_PCIE_TOPO_SS_RESPONDING:
1815 		status_str =  "responding";
1816 		break;
1817 	case MPI3_EVENT_PCIE_TOPO_SS_DELAY_NOT_RESPONDING:
1818 		status_str = "remove delay";
1819 		break;
1820 	case MPI3_EVENT_PCIE_TOPO_SS_NO_PCIE_SWITCH:
1821 		status_str = "direct attached";
1822 		break;
1823 	default:
1824 		status_str = "unknown status";
1825 		break;
1826 	}
1827 	ioc_info(mrioc, "%s :pcie topology change: (%s)\n",
1828 	    __func__, status_str);
1829 	ioc_info(mrioc,
1830 	    "%s :\tswitch_handle(0x%04x), enclosure_handle(0x%04x) start_port(%02d), num_entries(%d)\n",
1831 	    __func__, le16_to_cpu(event_data->switch_dev_handle),
1832 	    le16_to_cpu(event_data->enclosure_handle),
1833 	    event_data->start_port_num, event_data->num_entries);
1834 	for (i = 0; i < event_data->num_entries; i++) {
1835 		handle =
1836 		    le16_to_cpu(event_data->port_entry[i].attached_dev_handle);
1837 		if (!handle)
1838 			continue;
1839 		port_number = event_data->start_port_num + i;
1840 		reason_code = event_data->port_entry[i].port_status;
1841 		switch (reason_code) {
1842 		case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
1843 			status_str = "target remove";
1844 			break;
1845 		case MPI3_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
1846 			status_str = "delay target remove";
1847 			break;
1848 		case MPI3_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
1849 			status_str = "link status change";
1850 			break;
1851 		case MPI3_EVENT_PCIE_TOPO_PS_NO_CHANGE:
1852 			status_str = "link status no change";
1853 			break;
1854 		case MPI3_EVENT_PCIE_TOPO_PS_RESPONDING:
1855 			status_str = "target responding";
1856 			break;
1857 		default:
1858 			status_str = "unknown";
1859 			break;
1860 		}
1861 		link_rate = event_data->port_entry[i].current_port_info &
1862 		    MPI3_EVENT_PCIE_TOPO_PI_RATE_MASK;
1863 		prev_link_rate = event_data->port_entry[i].previous_port_info &
1864 		    MPI3_EVENT_PCIE_TOPO_PI_RATE_MASK;
1865 		ioc_info(mrioc,
1866 		    "%s :\tport(%02d), attached_handle(0x%04x): %s: link rate: new(0x%02x), old(0x%02x)\n",
1867 		    __func__, port_number, handle, status_str, link_rate,
1868 		    prev_link_rate);
1869 	}
1870 }
1871 
1872 /**
1873  * mpi3mr_pcietopochg_evt_bh - PCIeTopologyChange evt bottomhalf
1874  * @mrioc: Adapter instance reference
1875  * @fwevt: Firmware event reference
1876  *
1877  * Prints information about the PCIe topology change event and
1878  * for "not responding" event code, removes the device from the
1879  * upper layers.
1880  *
1881  * Return: Nothing.
1882  */
1883 static void mpi3mr_pcietopochg_evt_bh(struct mpi3mr_ioc *mrioc,
1884 	struct mpi3mr_fwevt *fwevt)
1885 {
1886 	struct mpi3_event_data_pcie_topology_change_list *event_data =
1887 	    (struct mpi3_event_data_pcie_topology_change_list *)fwevt->event_data;
1888 	int i;
1889 	u16 handle;
1890 	u8 reason_code;
1891 	struct mpi3mr_tgt_dev *tgtdev = NULL;
1892 
1893 	mpi3mr_pcietopochg_evt_debug(mrioc, event_data);
1894 
1895 	for (i = 0; i < event_data->num_entries; i++) {
1896 		if (fwevt->discard)
1897 			return;
1898 		handle =
1899 		    le16_to_cpu(event_data->port_entry[i].attached_dev_handle);
1900 		if (!handle)
1901 			continue;
1902 		tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
1903 		if (!tgtdev)
1904 			continue;
1905 
1906 		reason_code = event_data->port_entry[i].port_status;
1907 
1908 		switch (reason_code) {
1909 		case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
1910 			if (tgtdev->host_exposed)
1911 				mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1912 			mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, false);
1913 			mpi3mr_tgtdev_put(tgtdev);
1914 			break;
1915 		default:
1916 			break;
1917 		}
1918 		if (tgtdev)
1919 			mpi3mr_tgtdev_put(tgtdev);
1920 	}
1921 }
1922 
1923 /**
1924  * mpi3mr_logdata_evt_bh -  Log data event bottomhalf
1925  * @mrioc: Adapter instance reference
1926  * @fwevt: Firmware event reference
1927  *
1928  * Extracts the event data and calls application interfacing
1929  * function to process the event further.
1930  *
1931  * Return: Nothing.
1932  */
1933 static void mpi3mr_logdata_evt_bh(struct mpi3mr_ioc *mrioc,
1934 	struct mpi3mr_fwevt *fwevt)
1935 {
1936 	mpi3mr_app_save_logdata(mrioc, fwevt->event_data,
1937 	    fwevt->event_data_size);
1938 }
1939 
1940 /**
1941  * mpi3mr_update_sdev_qd - Update SCSI device queue depath
1942  * @sdev: SCSI device reference
1943  * @data: Queue depth reference
1944  *
1945  * This is an iterator function called for each SCSI device in a
1946  * target to update the QD of each SCSI device.
1947  *
1948  * Return: Nothing.
1949  */
1950 static void mpi3mr_update_sdev_qd(struct scsi_device *sdev, void *data)
1951 {
1952 	u16 *q_depth = (u16 *)data;
1953 
1954 	scsi_change_queue_depth(sdev, (int)*q_depth);
1955 	sdev->max_queue_depth = sdev->queue_depth;
1956 }
1957 
1958 /**
1959  * mpi3mr_set_qd_for_all_vd_in_tg -set QD for TG VDs
1960  * @mrioc: Adapter instance reference
1961  * @tg: Throttle group information pointer
1962  *
1963  * Accessor to reduce QD for each device associated with the
1964  * given throttle group.
1965  *
1966  * Return: None.
1967  */
1968 static void mpi3mr_set_qd_for_all_vd_in_tg(struct mpi3mr_ioc *mrioc,
1969 	struct mpi3mr_throttle_group_info *tg)
1970 {
1971 	unsigned long flags;
1972 	struct mpi3mr_tgt_dev *tgtdev;
1973 	struct mpi3mr_stgt_priv_data *tgt_priv;
1974 
1975 
1976 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
1977 	list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
1978 		if (tgtdev->starget && tgtdev->starget->hostdata) {
1979 			tgt_priv = tgtdev->starget->hostdata;
1980 			if (tgt_priv->throttle_group == tg) {
1981 				dprint_event_bh(mrioc,
1982 				    "updating qd due to throttling for persist_id(%d) original_qd(%d), reduced_qd (%d)\n",
1983 				    tgt_priv->perst_id, tgtdev->q_depth,
1984 				    tg->modified_qd);
1985 				starget_for_each_device(tgtdev->starget,
1986 				    (void *)&tg->modified_qd,
1987 				    mpi3mr_update_sdev_qd);
1988 			}
1989 		}
1990 	}
1991 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
1992 }
1993 
1994 /**
1995  * mpi3mr_fwevt_bh - Firmware event bottomhalf handler
1996  * @mrioc: Adapter instance reference
1997  * @fwevt: Firmware event reference
1998  *
1999  * Identifies the firmware event and calls corresponding bottomg
2000  * half handler and sends event acknowledgment if required.
2001  *
2002  * Return: Nothing.
2003  */
2004 static void mpi3mr_fwevt_bh(struct mpi3mr_ioc *mrioc,
2005 	struct mpi3mr_fwevt *fwevt)
2006 {
2007 	struct mpi3_device_page0 *dev_pg0 = NULL;
2008 	u16 perst_id, handle, dev_info;
2009 	struct mpi3_device0_sas_sata_format *sasinf = NULL;
2010 	unsigned int timeout;
2011 
2012 	mpi3mr_fwevt_del_from_list(mrioc, fwevt);
2013 	mrioc->current_event = fwevt;
2014 
2015 	if (mrioc->stop_drv_processing)
2016 		goto out;
2017 
2018 	if (mrioc->unrecoverable) {
2019 		dprint_event_bh(mrioc,
2020 		    "ignoring event(0x%02x) in bottom half handler due to unrecoverable controller\n",
2021 		    fwevt->event_id);
2022 		goto out;
2023 	}
2024 
2025 	if (!fwevt->process_evt)
2026 		goto evt_ack;
2027 
2028 	switch (fwevt->event_id) {
2029 	case MPI3_EVENT_DEVICE_ADDED:
2030 	{
2031 		dev_pg0 = (struct mpi3_device_page0 *)fwevt->event_data;
2032 		perst_id = le16_to_cpu(dev_pg0->persistent_id);
2033 		handle = le16_to_cpu(dev_pg0->dev_handle);
2034 		if (perst_id != MPI3_DEVICE0_PERSISTENTID_INVALID)
2035 			mpi3mr_report_tgtdev_to_host(mrioc, perst_id);
2036 		else if (mrioc->sas_transport_enabled &&
2037 		    (dev_pg0->device_form == MPI3_DEVICE_DEVFORM_SAS_SATA)) {
2038 			sasinf = &dev_pg0->device_specific.sas_sata_format;
2039 			dev_info = le16_to_cpu(sasinf->device_info);
2040 			if (!mrioc->sas_hba.num_phys)
2041 				mpi3mr_sas_host_add(mrioc);
2042 			else
2043 				mpi3mr_sas_host_refresh(mrioc);
2044 
2045 			if (mpi3mr_is_expander_device(dev_info))
2046 				mpi3mr_expander_add(mrioc, handle);
2047 		}
2048 		break;
2049 	}
2050 	case MPI3_EVENT_DEVICE_INFO_CHANGED:
2051 	{
2052 		dev_pg0 = (struct mpi3_device_page0 *)fwevt->event_data;
2053 		perst_id = le16_to_cpu(dev_pg0->persistent_id);
2054 		if (perst_id != MPI3_DEVICE0_PERSISTENTID_INVALID)
2055 			mpi3mr_devinfochg_evt_bh(mrioc, dev_pg0);
2056 		break;
2057 	}
2058 	case MPI3_EVENT_DEVICE_STATUS_CHANGE:
2059 	{
2060 		mpi3mr_devstatuschg_evt_bh(mrioc, fwevt);
2061 		break;
2062 	}
2063 	case MPI3_EVENT_ENCL_DEVICE_ADDED:
2064 	case MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE:
2065 	{
2066 		mpi3mr_encldev_add_chg_evt_bh(mrioc, fwevt);
2067 		break;
2068 	}
2069 
2070 	case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
2071 	{
2072 		mpi3mr_sastopochg_evt_bh(mrioc, fwevt);
2073 		break;
2074 	}
2075 	case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
2076 	{
2077 		mpi3mr_pcietopochg_evt_bh(mrioc, fwevt);
2078 		break;
2079 	}
2080 	case MPI3_EVENT_LOG_DATA:
2081 	{
2082 		mpi3mr_logdata_evt_bh(mrioc, fwevt);
2083 		break;
2084 	}
2085 	case MPI3MR_DRIVER_EVENT_TG_QD_REDUCTION:
2086 	{
2087 		struct mpi3mr_throttle_group_info *tg;
2088 
2089 		tg = *(struct mpi3mr_throttle_group_info **)fwevt->event_data;
2090 		dprint_event_bh(mrioc,
2091 		    "qd reduction event processed for tg_id(%d) reduction_needed(%d)\n",
2092 		    tg->id, tg->need_qd_reduction);
2093 		if (tg->need_qd_reduction) {
2094 			mpi3mr_set_qd_for_all_vd_in_tg(mrioc, tg);
2095 			tg->need_qd_reduction = 0;
2096 		}
2097 		break;
2098 	}
2099 	case MPI3_EVENT_WAIT_FOR_DEVICES_TO_REFRESH:
2100 	{
2101 		timeout = MPI3MR_RESET_TIMEOUT * 2;
2102 		while ((mrioc->device_refresh_on || mrioc->block_on_pci_err) &&
2103 		    !mrioc->unrecoverable && !mrioc->pci_err_recovery) {
2104 			msleep(500);
2105 			if (!timeout--) {
2106 				mrioc->unrecoverable = 1;
2107 				break;
2108 			}
2109 		}
2110 
2111 		if (mrioc->unrecoverable || mrioc->pci_err_recovery)
2112 			break;
2113 
2114 		dprint_event_bh(mrioc,
2115 		    "scan for non responding and newly added devices after soft reset started\n");
2116 		if (mrioc->sas_transport_enabled) {
2117 			mpi3mr_refresh_sas_ports(mrioc);
2118 			mpi3mr_refresh_expanders(mrioc);
2119 		}
2120 		mpi3mr_refresh_tgtdevs(mrioc);
2121 		ioc_info(mrioc,
2122 		    "scan for non responding and newly added devices after soft reset completed\n");
2123 		break;
2124 	}
2125 	case MPI3MR_DRIVER_EVENT_PROCESS_TRIGGER:
2126 	{
2127 		mpi3mr_process_trigger_data_event_bh(mrioc,
2128 		    (struct trigger_event_data *)fwevt->event_data);
2129 		break;
2130 	}
2131 	default:
2132 		break;
2133 	}
2134 
2135 evt_ack:
2136 	if (fwevt->send_ack)
2137 		mpi3mr_process_event_ack(mrioc, fwevt->event_id,
2138 		    fwevt->evt_ctx);
2139 out:
2140 	/* Put fwevt reference count to neutralize kref_init increment */
2141 	mpi3mr_fwevt_put(fwevt);
2142 	mrioc->current_event = NULL;
2143 }
2144 
2145 /**
2146  * mpi3mr_fwevt_worker - Firmware event worker
2147  * @work: Work struct containing firmware event
2148  *
2149  * Extracts the firmware event and calls mpi3mr_fwevt_bh.
2150  *
2151  * Return: Nothing.
2152  */
2153 static void mpi3mr_fwevt_worker(struct work_struct *work)
2154 {
2155 	struct mpi3mr_fwevt *fwevt = container_of(work, struct mpi3mr_fwevt,
2156 	    work);
2157 	mpi3mr_fwevt_bh(fwevt->mrioc, fwevt);
2158 	/*
2159 	 * Put fwevt reference count after
2160 	 * dequeuing it from worker queue
2161 	 */
2162 	mpi3mr_fwevt_put(fwevt);
2163 }
2164 
2165 /**
2166  * mpi3mr_create_tgtdev - Create and add a target device
2167  * @mrioc: Adapter instance reference
2168  * @dev_pg0: Device Page 0 data
2169  *
2170  * If the device specified by the device page 0 data is not
2171  * present in the driver's internal list, allocate the memory
2172  * for the device, populate the data and add to the list, else
2173  * update the device data.  The key is persistent ID.
2174  *
2175  * Return: 0 on success, -ENOMEM on memory allocation failure
2176  */
2177 static int mpi3mr_create_tgtdev(struct mpi3mr_ioc *mrioc,
2178 	struct mpi3_device_page0 *dev_pg0)
2179 {
2180 	int retval = 0;
2181 	struct mpi3mr_tgt_dev *tgtdev = NULL;
2182 	u16 perst_id = 0;
2183 	unsigned long flags;
2184 
2185 	perst_id = le16_to_cpu(dev_pg0->persistent_id);
2186 	if (perst_id == MPI3_DEVICE0_PERSISTENTID_INVALID)
2187 		return retval;
2188 
2189 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
2190 	tgtdev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, perst_id);
2191 	if (tgtdev)
2192 		tgtdev->state = MPI3MR_DEV_CREATED;
2193 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
2194 
2195 	if (tgtdev) {
2196 		mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0, true);
2197 		mpi3mr_tgtdev_put(tgtdev);
2198 	} else {
2199 		tgtdev = mpi3mr_alloc_tgtdev();
2200 		if (!tgtdev)
2201 			return -ENOMEM;
2202 		mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0, true);
2203 		mpi3mr_tgtdev_add_to_list(mrioc, tgtdev);
2204 	}
2205 
2206 	return retval;
2207 }
2208 
2209 /**
2210  * mpi3mr_flush_delayed_cmd_lists - Flush pending commands
2211  * @mrioc: Adapter instance reference
2212  *
2213  * Flush pending commands in the delayed lists due to a
2214  * controller reset or driver removal as a cleanup.
2215  *
2216  * Return: Nothing
2217  */
2218 void mpi3mr_flush_delayed_cmd_lists(struct mpi3mr_ioc *mrioc)
2219 {
2220 	struct delayed_dev_rmhs_node *_rmhs_node;
2221 	struct delayed_evt_ack_node *_evtack_node;
2222 
2223 	dprint_reset(mrioc, "flushing delayed dev_remove_hs commands\n");
2224 	while (!list_empty(&mrioc->delayed_rmhs_list)) {
2225 		_rmhs_node = list_entry(mrioc->delayed_rmhs_list.next,
2226 		    struct delayed_dev_rmhs_node, list);
2227 		list_del(&_rmhs_node->list);
2228 		kfree(_rmhs_node);
2229 	}
2230 	dprint_reset(mrioc, "flushing delayed event ack commands\n");
2231 	while (!list_empty(&mrioc->delayed_evtack_cmds_list)) {
2232 		_evtack_node = list_entry(mrioc->delayed_evtack_cmds_list.next,
2233 		    struct delayed_evt_ack_node, list);
2234 		list_del(&_evtack_node->list);
2235 		kfree(_evtack_node);
2236 	}
2237 }
2238 
2239 /**
2240  * mpi3mr_dev_rmhs_complete_iou - Device removal IOUC completion
2241  * @mrioc: Adapter instance reference
2242  * @drv_cmd: Internal command tracker
2243  *
2244  * Issues a target reset TM to the firmware from the device
2245  * removal TM pend list or retry the removal handshake sequence
2246  * based on the IOU control request IOC status.
2247  *
2248  * Return: Nothing
2249  */
2250 static void mpi3mr_dev_rmhs_complete_iou(struct mpi3mr_ioc *mrioc,
2251 	struct mpi3mr_drv_cmd *drv_cmd)
2252 {
2253 	u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
2254 	struct delayed_dev_rmhs_node *delayed_dev_rmhs = NULL;
2255 
2256 	if (drv_cmd->state & MPI3MR_CMD_RESET)
2257 		goto clear_drv_cmd;
2258 
2259 	ioc_info(mrioc,
2260 	    "%s :dev_rmhs_iouctrl_complete:handle(0x%04x), ioc_status(0x%04x), loginfo(0x%08x)\n",
2261 	    __func__, drv_cmd->dev_handle, drv_cmd->ioc_status,
2262 	    drv_cmd->ioc_loginfo);
2263 	if (drv_cmd->ioc_status != MPI3_IOCSTATUS_SUCCESS) {
2264 		if (drv_cmd->retry_count < MPI3MR_DEV_RMHS_RETRY_COUNT) {
2265 			drv_cmd->retry_count++;
2266 			ioc_info(mrioc,
2267 			    "%s :dev_rmhs_iouctrl_complete: handle(0x%04x)retrying handshake retry=%d\n",
2268 			    __func__, drv_cmd->dev_handle,
2269 			    drv_cmd->retry_count);
2270 			mpi3mr_dev_rmhs_send_tm(mrioc, drv_cmd->dev_handle,
2271 			    drv_cmd, drv_cmd->iou_rc);
2272 			return;
2273 		}
2274 		ioc_err(mrioc,
2275 		    "%s :dev removal handshake failed after all retries: handle(0x%04x)\n",
2276 		    __func__, drv_cmd->dev_handle);
2277 	} else {
2278 		ioc_info(mrioc,
2279 		    "%s :dev removal handshake completed successfully: handle(0x%04x)\n",
2280 		    __func__, drv_cmd->dev_handle);
2281 		clear_bit(drv_cmd->dev_handle, mrioc->removepend_bitmap);
2282 	}
2283 
2284 	if (!list_empty(&mrioc->delayed_rmhs_list)) {
2285 		delayed_dev_rmhs = list_entry(mrioc->delayed_rmhs_list.next,
2286 		    struct delayed_dev_rmhs_node, list);
2287 		drv_cmd->dev_handle = delayed_dev_rmhs->handle;
2288 		drv_cmd->retry_count = 0;
2289 		drv_cmd->iou_rc = delayed_dev_rmhs->iou_rc;
2290 		ioc_info(mrioc,
2291 		    "%s :dev_rmhs_iouctrl_complete: processing delayed TM: handle(0x%04x)\n",
2292 		    __func__, drv_cmd->dev_handle);
2293 		mpi3mr_dev_rmhs_send_tm(mrioc, drv_cmd->dev_handle, drv_cmd,
2294 		    drv_cmd->iou_rc);
2295 		list_del(&delayed_dev_rmhs->list);
2296 		kfree(delayed_dev_rmhs);
2297 		return;
2298 	}
2299 
2300 clear_drv_cmd:
2301 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
2302 	drv_cmd->callback = NULL;
2303 	drv_cmd->retry_count = 0;
2304 	drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
2305 	clear_bit(cmd_idx, mrioc->devrem_bitmap);
2306 }
2307 
2308 /**
2309  * mpi3mr_dev_rmhs_complete_tm - Device removal TM completion
2310  * @mrioc: Adapter instance reference
2311  * @drv_cmd: Internal command tracker
2312  *
2313  * Issues a target reset TM to the firmware from the device
2314  * removal TM pend list or issue IO unit control request as
2315  * part of device removal or hidden acknowledgment handshake.
2316  *
2317  * Return: Nothing
2318  */
2319 static void mpi3mr_dev_rmhs_complete_tm(struct mpi3mr_ioc *mrioc,
2320 	struct mpi3mr_drv_cmd *drv_cmd)
2321 {
2322 	struct mpi3_iounit_control_request iou_ctrl;
2323 	u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
2324 	struct mpi3_scsi_task_mgmt_reply *tm_reply = NULL;
2325 	int retval;
2326 
2327 	if (drv_cmd->state & MPI3MR_CMD_RESET)
2328 		goto clear_drv_cmd;
2329 
2330 	if (drv_cmd->state & MPI3MR_CMD_REPLY_VALID)
2331 		tm_reply = (struct mpi3_scsi_task_mgmt_reply *)drv_cmd->reply;
2332 
2333 	if (tm_reply)
2334 		pr_info(IOCNAME
2335 		    "dev_rmhs_tr_complete:handle(0x%04x), ioc_status(0x%04x), loginfo(0x%08x), term_count(%d)\n",
2336 		    mrioc->name, drv_cmd->dev_handle, drv_cmd->ioc_status,
2337 		    drv_cmd->ioc_loginfo,
2338 		    le32_to_cpu(tm_reply->termination_count));
2339 
2340 	pr_info(IOCNAME "Issuing IOU CTL: handle(0x%04x) dev_rmhs idx(%d)\n",
2341 	    mrioc->name, drv_cmd->dev_handle, cmd_idx);
2342 
2343 	memset(&iou_ctrl, 0, sizeof(iou_ctrl));
2344 
2345 	drv_cmd->state = MPI3MR_CMD_PENDING;
2346 	drv_cmd->is_waiting = 0;
2347 	drv_cmd->callback = mpi3mr_dev_rmhs_complete_iou;
2348 	iou_ctrl.operation = drv_cmd->iou_rc;
2349 	iou_ctrl.param16[0] = cpu_to_le16(drv_cmd->dev_handle);
2350 	iou_ctrl.host_tag = cpu_to_le16(drv_cmd->host_tag);
2351 	iou_ctrl.function = MPI3_FUNCTION_IO_UNIT_CONTROL;
2352 
2353 	retval = mpi3mr_admin_request_post(mrioc, &iou_ctrl, sizeof(iou_ctrl),
2354 	    1);
2355 	if (retval) {
2356 		pr_err(IOCNAME "Issue DevRmHsTMIOUCTL: Admin post failed\n",
2357 		    mrioc->name);
2358 		goto clear_drv_cmd;
2359 	}
2360 
2361 	return;
2362 clear_drv_cmd:
2363 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
2364 	drv_cmd->callback = NULL;
2365 	drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
2366 	drv_cmd->retry_count = 0;
2367 	clear_bit(cmd_idx, mrioc->devrem_bitmap);
2368 }
2369 
2370 /**
2371  * mpi3mr_dev_rmhs_send_tm - Issue TM for device removal
2372  * @mrioc: Adapter instance reference
2373  * @handle: Device handle
2374  * @cmdparam: Internal command tracker
2375  * @iou_rc: IO unit reason code
2376  *
2377  * Issues a target reset TM to the firmware or add it to a pend
2378  * list as part of device removal or hidden acknowledgment
2379  * handshake.
2380  *
2381  * Return: Nothing
2382  */
2383 static void mpi3mr_dev_rmhs_send_tm(struct mpi3mr_ioc *mrioc, u16 handle,
2384 	struct mpi3mr_drv_cmd *cmdparam, u8 iou_rc)
2385 {
2386 	struct mpi3_scsi_task_mgmt_request tm_req;
2387 	int retval = 0;
2388 	u16 cmd_idx = MPI3MR_NUM_DEVRMCMD;
2389 	u8 retrycount = 5;
2390 	struct mpi3mr_drv_cmd *drv_cmd = cmdparam;
2391 	struct delayed_dev_rmhs_node *delayed_dev_rmhs = NULL;
2392 	struct mpi3mr_tgt_dev *tgtdev = NULL;
2393 	unsigned long flags;
2394 
2395 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
2396 	tgtdev = __mpi3mr_get_tgtdev_by_handle(mrioc, handle);
2397 	if (tgtdev && (iou_rc == MPI3_CTRL_OP_REMOVE_DEVICE))
2398 		tgtdev->state = MPI3MR_DEV_REMOVE_HS_STARTED;
2399 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
2400 
2401 	if (drv_cmd)
2402 		goto issue_cmd;
2403 	do {
2404 		cmd_idx = find_first_zero_bit(mrioc->devrem_bitmap,
2405 		    MPI3MR_NUM_DEVRMCMD);
2406 		if (cmd_idx < MPI3MR_NUM_DEVRMCMD) {
2407 			if (!test_and_set_bit(cmd_idx, mrioc->devrem_bitmap))
2408 				break;
2409 			cmd_idx = MPI3MR_NUM_DEVRMCMD;
2410 		}
2411 	} while (retrycount--);
2412 
2413 	if (cmd_idx >= MPI3MR_NUM_DEVRMCMD) {
2414 		delayed_dev_rmhs = kzalloc(sizeof(*delayed_dev_rmhs),
2415 		    GFP_ATOMIC);
2416 		if (!delayed_dev_rmhs)
2417 			return;
2418 		INIT_LIST_HEAD(&delayed_dev_rmhs->list);
2419 		delayed_dev_rmhs->handle = handle;
2420 		delayed_dev_rmhs->iou_rc = iou_rc;
2421 		list_add_tail(&delayed_dev_rmhs->list,
2422 		    &mrioc->delayed_rmhs_list);
2423 		ioc_info(mrioc, "%s :DevRmHs: tr:handle(0x%04x) is postponed\n",
2424 		    __func__, handle);
2425 		return;
2426 	}
2427 	drv_cmd = &mrioc->dev_rmhs_cmds[cmd_idx];
2428 
2429 issue_cmd:
2430 	cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
2431 	ioc_info(mrioc,
2432 	    "%s :Issuing TR TM: for devhandle 0x%04x with dev_rmhs %d\n",
2433 	    __func__, handle, cmd_idx);
2434 
2435 	memset(&tm_req, 0, sizeof(tm_req));
2436 	if (drv_cmd->state & MPI3MR_CMD_PENDING) {
2437 		ioc_err(mrioc, "%s :Issue TM: Command is in use\n", __func__);
2438 		goto out;
2439 	}
2440 	drv_cmd->state = MPI3MR_CMD_PENDING;
2441 	drv_cmd->is_waiting = 0;
2442 	drv_cmd->callback = mpi3mr_dev_rmhs_complete_tm;
2443 	drv_cmd->dev_handle = handle;
2444 	drv_cmd->iou_rc = iou_rc;
2445 	tm_req.dev_handle = cpu_to_le16(handle);
2446 	tm_req.task_type = MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
2447 	tm_req.host_tag = cpu_to_le16(drv_cmd->host_tag);
2448 	tm_req.task_host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INVALID);
2449 	tm_req.function = MPI3_FUNCTION_SCSI_TASK_MGMT;
2450 
2451 	set_bit(handle, mrioc->removepend_bitmap);
2452 	retval = mpi3mr_admin_request_post(mrioc, &tm_req, sizeof(tm_req), 1);
2453 	if (retval) {
2454 		ioc_err(mrioc, "%s :Issue DevRmHsTM: Admin Post failed\n",
2455 		    __func__);
2456 		goto out_failed;
2457 	}
2458 out:
2459 	return;
2460 out_failed:
2461 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
2462 	drv_cmd->callback = NULL;
2463 	drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
2464 	drv_cmd->retry_count = 0;
2465 	clear_bit(cmd_idx, mrioc->devrem_bitmap);
2466 }
2467 
2468 /**
2469  * mpi3mr_complete_evt_ack - event ack request completion
2470  * @mrioc: Adapter instance reference
2471  * @drv_cmd: Internal command tracker
2472  *
2473  * This is the completion handler for non blocking event
2474  * acknowledgment sent to the firmware and this will issue any
2475  * pending event acknowledgment request.
2476  *
2477  * Return: Nothing
2478  */
2479 static void mpi3mr_complete_evt_ack(struct mpi3mr_ioc *mrioc,
2480 	struct mpi3mr_drv_cmd *drv_cmd)
2481 {
2482 	u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
2483 	struct delayed_evt_ack_node *delayed_evtack = NULL;
2484 
2485 	if (drv_cmd->state & MPI3MR_CMD_RESET)
2486 		goto clear_drv_cmd;
2487 
2488 	if (drv_cmd->ioc_status != MPI3_IOCSTATUS_SUCCESS) {
2489 		dprint_event_th(mrioc,
2490 		    "immediate event ack failed with ioc_status(0x%04x) log_info(0x%08x)\n",
2491 		    (drv_cmd->ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
2492 		    drv_cmd->ioc_loginfo);
2493 	}
2494 
2495 	if (!list_empty(&mrioc->delayed_evtack_cmds_list)) {
2496 		delayed_evtack =
2497 			list_entry(mrioc->delayed_evtack_cmds_list.next,
2498 			    struct delayed_evt_ack_node, list);
2499 		mpi3mr_send_event_ack(mrioc, delayed_evtack->event, drv_cmd,
2500 		    delayed_evtack->event_ctx);
2501 		list_del(&delayed_evtack->list);
2502 		kfree(delayed_evtack);
2503 		return;
2504 	}
2505 clear_drv_cmd:
2506 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
2507 	drv_cmd->callback = NULL;
2508 	clear_bit(cmd_idx, mrioc->evtack_cmds_bitmap);
2509 }
2510 
2511 /**
2512  * mpi3mr_send_event_ack - Issue event acknwoledgment request
2513  * @mrioc: Adapter instance reference
2514  * @event: MPI3 event id
2515  * @cmdparam: Internal command tracker
2516  * @event_ctx: event context
2517  *
2518  * Issues event acknowledgment request to the firmware if there
2519  * is a free command to send the event ack else it to a pend
2520  * list so that it will be processed on a completion of a prior
2521  * event acknowledgment .
2522  *
2523  * Return: Nothing
2524  */
2525 static void mpi3mr_send_event_ack(struct mpi3mr_ioc *mrioc, u8 event,
2526 	struct mpi3mr_drv_cmd *cmdparam, u32 event_ctx)
2527 {
2528 	struct mpi3_event_ack_request evtack_req;
2529 	int retval = 0;
2530 	u8 retrycount = 5;
2531 	u16 cmd_idx = MPI3MR_NUM_EVTACKCMD;
2532 	struct mpi3mr_drv_cmd *drv_cmd = cmdparam;
2533 	struct delayed_evt_ack_node *delayed_evtack = NULL;
2534 
2535 	if (drv_cmd) {
2536 		dprint_event_th(mrioc,
2537 		    "sending delayed event ack in the top half for event(0x%02x), event_ctx(0x%08x)\n",
2538 		    event, event_ctx);
2539 		goto issue_cmd;
2540 	}
2541 	dprint_event_th(mrioc,
2542 	    "sending event ack in the top half for event(0x%02x), event_ctx(0x%08x)\n",
2543 	    event, event_ctx);
2544 	do {
2545 		cmd_idx = find_first_zero_bit(mrioc->evtack_cmds_bitmap,
2546 		    MPI3MR_NUM_EVTACKCMD);
2547 		if (cmd_idx < MPI3MR_NUM_EVTACKCMD) {
2548 			if (!test_and_set_bit(cmd_idx,
2549 			    mrioc->evtack_cmds_bitmap))
2550 				break;
2551 			cmd_idx = MPI3MR_NUM_EVTACKCMD;
2552 		}
2553 	} while (retrycount--);
2554 
2555 	if (cmd_idx >= MPI3MR_NUM_EVTACKCMD) {
2556 		delayed_evtack = kzalloc(sizeof(*delayed_evtack),
2557 		    GFP_ATOMIC);
2558 		if (!delayed_evtack)
2559 			return;
2560 		INIT_LIST_HEAD(&delayed_evtack->list);
2561 		delayed_evtack->event = event;
2562 		delayed_evtack->event_ctx = event_ctx;
2563 		list_add_tail(&delayed_evtack->list,
2564 		    &mrioc->delayed_evtack_cmds_list);
2565 		dprint_event_th(mrioc,
2566 		    "event ack in the top half for event(0x%02x), event_ctx(0x%08x) is postponed\n",
2567 		    event, event_ctx);
2568 		return;
2569 	}
2570 	drv_cmd = &mrioc->evtack_cmds[cmd_idx];
2571 
2572 issue_cmd:
2573 	cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
2574 
2575 	memset(&evtack_req, 0, sizeof(evtack_req));
2576 	if (drv_cmd->state & MPI3MR_CMD_PENDING) {
2577 		dprint_event_th(mrioc,
2578 		    "sending event ack failed due to command in use\n");
2579 		goto out;
2580 	}
2581 	drv_cmd->state = MPI3MR_CMD_PENDING;
2582 	drv_cmd->is_waiting = 0;
2583 	drv_cmd->callback = mpi3mr_complete_evt_ack;
2584 	evtack_req.host_tag = cpu_to_le16(drv_cmd->host_tag);
2585 	evtack_req.function = MPI3_FUNCTION_EVENT_ACK;
2586 	evtack_req.event = event;
2587 	evtack_req.event_context = cpu_to_le32(event_ctx);
2588 	retval = mpi3mr_admin_request_post(mrioc, &evtack_req,
2589 	    sizeof(evtack_req), 1);
2590 	if (retval) {
2591 		dprint_event_th(mrioc,
2592 		    "posting event ack request is failed\n");
2593 		goto out_failed;
2594 	}
2595 
2596 	dprint_event_th(mrioc,
2597 	    "event ack in the top half for event(0x%02x), event_ctx(0x%08x) is posted\n",
2598 	    event, event_ctx);
2599 out:
2600 	return;
2601 out_failed:
2602 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
2603 	drv_cmd->callback = NULL;
2604 	clear_bit(cmd_idx, mrioc->evtack_cmds_bitmap);
2605 }
2606 
2607 /**
2608  * mpi3mr_pcietopochg_evt_th - PCIETopologyChange evt tophalf
2609  * @mrioc: Adapter instance reference
2610  * @event_reply: event data
2611  *
2612  * Checks for the reason code and based on that either block I/O
2613  * to device, or unblock I/O to the device, or start the device
2614  * removal handshake with reason as remove with the firmware for
2615  * PCIe devices.
2616  *
2617  * Return: Nothing
2618  */
2619 static void mpi3mr_pcietopochg_evt_th(struct mpi3mr_ioc *mrioc,
2620 	struct mpi3_event_notification_reply *event_reply)
2621 {
2622 	struct mpi3_event_data_pcie_topology_change_list *topo_evt =
2623 	    (struct mpi3_event_data_pcie_topology_change_list *)event_reply->event_data;
2624 	int i;
2625 	u16 handle;
2626 	u8 reason_code;
2627 	struct mpi3mr_tgt_dev *tgtdev = NULL;
2628 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2629 
2630 	for (i = 0; i < topo_evt->num_entries; i++) {
2631 		handle = le16_to_cpu(topo_evt->port_entry[i].attached_dev_handle);
2632 		if (!handle)
2633 			continue;
2634 		reason_code = topo_evt->port_entry[i].port_status;
2635 		scsi_tgt_priv_data =  NULL;
2636 		tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
2637 		if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
2638 			scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2639 			    tgtdev->starget->hostdata;
2640 		switch (reason_code) {
2641 		case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
2642 			if (scsi_tgt_priv_data) {
2643 				scsi_tgt_priv_data->dev_removed = 1;
2644 				scsi_tgt_priv_data->dev_removedelay = 0;
2645 				atomic_set(&scsi_tgt_priv_data->block_io, 0);
2646 			}
2647 			mpi3mr_dev_rmhs_send_tm(mrioc, handle, NULL,
2648 			    MPI3_CTRL_OP_REMOVE_DEVICE);
2649 			break;
2650 		case MPI3_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
2651 			if (scsi_tgt_priv_data) {
2652 				scsi_tgt_priv_data->dev_removedelay = 1;
2653 				atomic_inc(&scsi_tgt_priv_data->block_io);
2654 			}
2655 			break;
2656 		case MPI3_EVENT_PCIE_TOPO_PS_RESPONDING:
2657 			if (scsi_tgt_priv_data &&
2658 			    scsi_tgt_priv_data->dev_removedelay) {
2659 				scsi_tgt_priv_data->dev_removedelay = 0;
2660 				atomic_dec_if_positive
2661 				    (&scsi_tgt_priv_data->block_io);
2662 			}
2663 			break;
2664 		case MPI3_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
2665 		default:
2666 			break;
2667 		}
2668 		if (tgtdev)
2669 			mpi3mr_tgtdev_put(tgtdev);
2670 	}
2671 }
2672 
2673 /**
2674  * mpi3mr_sastopochg_evt_th - SASTopologyChange evt tophalf
2675  * @mrioc: Adapter instance reference
2676  * @event_reply: event data
2677  *
2678  * Checks for the reason code and based on that either block I/O
2679  * to device, or unblock I/O to the device, or start the device
2680  * removal handshake with reason as remove with the firmware for
2681  * SAS/SATA devices.
2682  *
2683  * Return: Nothing
2684  */
2685 static void mpi3mr_sastopochg_evt_th(struct mpi3mr_ioc *mrioc,
2686 	struct mpi3_event_notification_reply *event_reply)
2687 {
2688 	struct mpi3_event_data_sas_topology_change_list *topo_evt =
2689 	    (struct mpi3_event_data_sas_topology_change_list *)event_reply->event_data;
2690 	int i;
2691 	u16 handle;
2692 	u8 reason_code;
2693 	struct mpi3mr_tgt_dev *tgtdev = NULL;
2694 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2695 
2696 	for (i = 0; i < topo_evt->num_entries; i++) {
2697 		handle = le16_to_cpu(topo_evt->phy_entry[i].attached_dev_handle);
2698 		if (!handle)
2699 			continue;
2700 		reason_code = topo_evt->phy_entry[i].status &
2701 		    MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
2702 		scsi_tgt_priv_data =  NULL;
2703 		tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
2704 		if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
2705 			scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2706 			    tgtdev->starget->hostdata;
2707 		switch (reason_code) {
2708 		case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
2709 			if (scsi_tgt_priv_data) {
2710 				scsi_tgt_priv_data->dev_removed = 1;
2711 				scsi_tgt_priv_data->dev_removedelay = 0;
2712 				atomic_set(&scsi_tgt_priv_data->block_io, 0);
2713 			}
2714 			mpi3mr_dev_rmhs_send_tm(mrioc, handle, NULL,
2715 			    MPI3_CTRL_OP_REMOVE_DEVICE);
2716 			break;
2717 		case MPI3_EVENT_SAS_TOPO_PHY_RC_DELAY_NOT_RESPONDING:
2718 			if (scsi_tgt_priv_data) {
2719 				scsi_tgt_priv_data->dev_removedelay = 1;
2720 				atomic_inc(&scsi_tgt_priv_data->block_io);
2721 			}
2722 			break;
2723 		case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
2724 			if (scsi_tgt_priv_data &&
2725 			    scsi_tgt_priv_data->dev_removedelay) {
2726 				scsi_tgt_priv_data->dev_removedelay = 0;
2727 				atomic_dec_if_positive
2728 				    (&scsi_tgt_priv_data->block_io);
2729 			}
2730 			break;
2731 		case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
2732 		default:
2733 			break;
2734 		}
2735 		if (tgtdev)
2736 			mpi3mr_tgtdev_put(tgtdev);
2737 	}
2738 }
2739 
2740 /**
2741  * mpi3mr_devstatuschg_evt_th - DeviceStatusChange evt tophalf
2742  * @mrioc: Adapter instance reference
2743  * @event_reply: event data
2744  *
2745  * Checks for the reason code and based on that either block I/O
2746  * to device, or unblock I/O to the device, or start the device
2747  * removal handshake with reason as remove/hide acknowledgment
2748  * with the firmware.
2749  *
2750  * Return: Nothing
2751  */
2752 static void mpi3mr_devstatuschg_evt_th(struct mpi3mr_ioc *mrioc,
2753 	struct mpi3_event_notification_reply *event_reply)
2754 {
2755 	u16 dev_handle = 0;
2756 	u8 ublock = 0, block = 0, hide = 0, delete = 0, remove = 0;
2757 	struct mpi3mr_tgt_dev *tgtdev = NULL;
2758 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2759 	struct mpi3_event_data_device_status_change *evtdata =
2760 	    (struct mpi3_event_data_device_status_change *)event_reply->event_data;
2761 
2762 	if (mrioc->stop_drv_processing)
2763 		goto out;
2764 
2765 	dev_handle = le16_to_cpu(evtdata->dev_handle);
2766 
2767 	switch (evtdata->reason_code) {
2768 	case MPI3_EVENT_DEV_STAT_RC_INT_DEVICE_RESET_STRT:
2769 	case MPI3_EVENT_DEV_STAT_RC_INT_IT_NEXUS_RESET_STRT:
2770 		block = 1;
2771 		break;
2772 	case MPI3_EVENT_DEV_STAT_RC_HIDDEN:
2773 		delete = 1;
2774 		hide = 1;
2775 		break;
2776 	case MPI3_EVENT_DEV_STAT_RC_VD_NOT_RESPONDING:
2777 		delete = 1;
2778 		remove = 1;
2779 		break;
2780 	case MPI3_EVENT_DEV_STAT_RC_INT_DEVICE_RESET_CMP:
2781 	case MPI3_EVENT_DEV_STAT_RC_INT_IT_NEXUS_RESET_CMP:
2782 		ublock = 1;
2783 		break;
2784 	default:
2785 		break;
2786 	}
2787 
2788 	tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
2789 	if (!tgtdev)
2790 		goto out;
2791 	if (hide)
2792 		tgtdev->is_hidden = hide;
2793 	if (tgtdev->starget && tgtdev->starget->hostdata) {
2794 		scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2795 		    tgtdev->starget->hostdata;
2796 		if (block)
2797 			atomic_inc(&scsi_tgt_priv_data->block_io);
2798 		if (delete)
2799 			scsi_tgt_priv_data->dev_removed = 1;
2800 		if (ublock)
2801 			atomic_dec_if_positive(&scsi_tgt_priv_data->block_io);
2802 	}
2803 	if (remove)
2804 		mpi3mr_dev_rmhs_send_tm(mrioc, dev_handle, NULL,
2805 		    MPI3_CTRL_OP_REMOVE_DEVICE);
2806 	if (hide)
2807 		mpi3mr_dev_rmhs_send_tm(mrioc, dev_handle, NULL,
2808 		    MPI3_CTRL_OP_HIDDEN_ACK);
2809 
2810 out:
2811 	if (tgtdev)
2812 		mpi3mr_tgtdev_put(tgtdev);
2813 }
2814 
2815 /**
2816  * mpi3mr_preparereset_evt_th - Prepare for reset event tophalf
2817  * @mrioc: Adapter instance reference
2818  * @event_reply: event data
2819  *
2820  * Blocks and unblocks host level I/O based on the reason code
2821  *
2822  * Return: Nothing
2823  */
2824 static void mpi3mr_preparereset_evt_th(struct mpi3mr_ioc *mrioc,
2825 	struct mpi3_event_notification_reply *event_reply)
2826 {
2827 	struct mpi3_event_data_prepare_for_reset *evtdata =
2828 	    (struct mpi3_event_data_prepare_for_reset *)event_reply->event_data;
2829 
2830 	if (evtdata->reason_code == MPI3_EVENT_PREPARE_RESET_RC_START) {
2831 		dprint_event_th(mrioc,
2832 		    "prepare for reset event top half with rc=start\n");
2833 		if (mrioc->prepare_for_reset)
2834 			return;
2835 		mrioc->prepare_for_reset = 1;
2836 		mrioc->prepare_for_reset_timeout_counter = 0;
2837 	} else if (evtdata->reason_code == MPI3_EVENT_PREPARE_RESET_RC_ABORT) {
2838 		dprint_event_th(mrioc,
2839 		    "prepare for reset top half with rc=abort\n");
2840 		mrioc->prepare_for_reset = 0;
2841 		mrioc->prepare_for_reset_timeout_counter = 0;
2842 	}
2843 	if ((event_reply->msg_flags & MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_MASK)
2844 	    == MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_REQUIRED)
2845 		mpi3mr_send_event_ack(mrioc, event_reply->event, NULL,
2846 		    le32_to_cpu(event_reply->event_context));
2847 }
2848 
2849 /**
2850  * mpi3mr_energypackchg_evt_th - Energy pack change evt tophalf
2851  * @mrioc: Adapter instance reference
2852  * @event_reply: event data
2853  *
2854  * Identifies the new shutdown timeout value and update.
2855  *
2856  * Return: Nothing
2857  */
2858 static void mpi3mr_energypackchg_evt_th(struct mpi3mr_ioc *mrioc,
2859 	struct mpi3_event_notification_reply *event_reply)
2860 {
2861 	struct mpi3_event_data_energy_pack_change *evtdata =
2862 	    (struct mpi3_event_data_energy_pack_change *)event_reply->event_data;
2863 	u16 shutdown_timeout = le16_to_cpu(evtdata->shutdown_timeout);
2864 
2865 	if (shutdown_timeout <= 0) {
2866 		ioc_warn(mrioc,
2867 		    "%s :Invalid Shutdown Timeout received = %d\n",
2868 		    __func__, shutdown_timeout);
2869 		return;
2870 	}
2871 
2872 	ioc_info(mrioc,
2873 	    "%s :Previous Shutdown Timeout Value = %d New Shutdown Timeout Value = %d\n",
2874 	    __func__, mrioc->facts.shutdown_timeout, shutdown_timeout);
2875 	mrioc->facts.shutdown_timeout = shutdown_timeout;
2876 }
2877 
2878 /**
2879  * mpi3mr_cablemgmt_evt_th - Cable management event tophalf
2880  * @mrioc: Adapter instance reference
2881  * @event_reply: event data
2882  *
2883  * Displays Cable manegemt event details.
2884  *
2885  * Return: Nothing
2886  */
2887 static void mpi3mr_cablemgmt_evt_th(struct mpi3mr_ioc *mrioc,
2888 	struct mpi3_event_notification_reply *event_reply)
2889 {
2890 	struct mpi3_event_data_cable_management *evtdata =
2891 	    (struct mpi3_event_data_cable_management *)event_reply->event_data;
2892 
2893 	switch (evtdata->status) {
2894 	case MPI3_EVENT_CABLE_MGMT_STATUS_INSUFFICIENT_POWER:
2895 	{
2896 		ioc_info(mrioc, "An active cable with receptacle_id %d cannot be powered.\n"
2897 		    "Devices connected to this cable are not detected.\n"
2898 		    "This cable requires %d mW of power.\n",
2899 		    evtdata->receptacle_id,
2900 		    le32_to_cpu(evtdata->active_cable_power_requirement));
2901 		break;
2902 	}
2903 	case MPI3_EVENT_CABLE_MGMT_STATUS_DEGRADED:
2904 	{
2905 		ioc_info(mrioc, "A cable with receptacle_id %d is not running at optimal speed\n",
2906 		    evtdata->receptacle_id);
2907 		break;
2908 	}
2909 	default:
2910 		break;
2911 	}
2912 }
2913 
2914 /**
2915  * mpi3mr_add_event_wait_for_device_refresh - Add Wait for Device Refresh Event
2916  * @mrioc: Adapter instance reference
2917  *
2918  * Add driver specific event to make sure that the driver won't process the
2919  * events until all the devices are refreshed during soft reset.
2920  *
2921  * Return: Nothing
2922  */
2923 void mpi3mr_add_event_wait_for_device_refresh(struct mpi3mr_ioc *mrioc)
2924 {
2925 	struct mpi3mr_fwevt *fwevt = NULL;
2926 
2927 	fwevt = mpi3mr_alloc_fwevt(0);
2928 	if (!fwevt) {
2929 		dprint_event_th(mrioc,
2930 		    "failed to schedule bottom half handler for event(0x%02x)\n",
2931 		    MPI3_EVENT_WAIT_FOR_DEVICES_TO_REFRESH);
2932 		return;
2933 	}
2934 	fwevt->mrioc = mrioc;
2935 	fwevt->event_id = MPI3_EVENT_WAIT_FOR_DEVICES_TO_REFRESH;
2936 	fwevt->send_ack = 0;
2937 	fwevt->process_evt = 1;
2938 	fwevt->evt_ctx = 0;
2939 	fwevt->event_data_size = 0;
2940 	mpi3mr_fwevt_add_to_list(mrioc, fwevt);
2941 }
2942 
2943 /**
2944  * mpi3mr_os_handle_events - Firmware event handler
2945  * @mrioc: Adapter instance reference
2946  * @event_reply: event data
2947  *
2948  * Identify whteher the event has to handled and acknowledged
2949  * and either process the event in the tophalf and/or schedule a
2950  * bottom half through mpi3mr_fwevt_worker.
2951  *
2952  * Return: Nothing
2953  */
2954 void mpi3mr_os_handle_events(struct mpi3mr_ioc *mrioc,
2955 	struct mpi3_event_notification_reply *event_reply)
2956 {
2957 	u16 evt_type, sz;
2958 	struct mpi3mr_fwevt *fwevt = NULL;
2959 	bool ack_req = 0, process_evt_bh = 0;
2960 
2961 	if (mrioc->stop_drv_processing)
2962 		return;
2963 
2964 	if ((event_reply->msg_flags & MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_MASK)
2965 	    == MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_REQUIRED)
2966 		ack_req = 1;
2967 
2968 	evt_type = event_reply->event;
2969 	mpi3mr_event_trigger(mrioc, event_reply->event);
2970 
2971 	switch (evt_type) {
2972 	case MPI3_EVENT_DEVICE_ADDED:
2973 	{
2974 		struct mpi3_device_page0 *dev_pg0 =
2975 		    (struct mpi3_device_page0 *)event_reply->event_data;
2976 		if (mpi3mr_create_tgtdev(mrioc, dev_pg0))
2977 			ioc_err(mrioc,
2978 			    "%s :Failed to add device in the device add event\n",
2979 			    __func__);
2980 		else
2981 			process_evt_bh = 1;
2982 		break;
2983 	}
2984 	case MPI3_EVENT_DEVICE_STATUS_CHANGE:
2985 	{
2986 		process_evt_bh = 1;
2987 		mpi3mr_devstatuschg_evt_th(mrioc, event_reply);
2988 		break;
2989 	}
2990 	case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
2991 	{
2992 		process_evt_bh = 1;
2993 		mpi3mr_sastopochg_evt_th(mrioc, event_reply);
2994 		break;
2995 	}
2996 	case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
2997 	{
2998 		process_evt_bh = 1;
2999 		mpi3mr_pcietopochg_evt_th(mrioc, event_reply);
3000 		break;
3001 	}
3002 	case MPI3_EVENT_PREPARE_FOR_RESET:
3003 	{
3004 		mpi3mr_preparereset_evt_th(mrioc, event_reply);
3005 		ack_req = 0;
3006 		break;
3007 	}
3008 	case MPI3_EVENT_DIAGNOSTIC_BUFFER_STATUS_CHANGE:
3009 	{
3010 		mpi3mr_hdbstatuschg_evt_th(mrioc, event_reply);
3011 		break;
3012 	}
3013 	case MPI3_EVENT_DEVICE_INFO_CHANGED:
3014 	case MPI3_EVENT_LOG_DATA:
3015 	case MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE:
3016 	case MPI3_EVENT_ENCL_DEVICE_ADDED:
3017 	{
3018 		process_evt_bh = 1;
3019 		break;
3020 	}
3021 	case MPI3_EVENT_ENERGY_PACK_CHANGE:
3022 	{
3023 		mpi3mr_energypackchg_evt_th(mrioc, event_reply);
3024 		break;
3025 	}
3026 	case MPI3_EVENT_CABLE_MGMT:
3027 	{
3028 		mpi3mr_cablemgmt_evt_th(mrioc, event_reply);
3029 		break;
3030 	}
3031 	case MPI3_EVENT_SAS_DISCOVERY:
3032 	case MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
3033 	case MPI3_EVENT_SAS_BROADCAST_PRIMITIVE:
3034 	case MPI3_EVENT_PCIE_ENUMERATION:
3035 		break;
3036 	default:
3037 		ioc_info(mrioc, "%s :event 0x%02x is not handled\n",
3038 		    __func__, evt_type);
3039 		break;
3040 	}
3041 	if (process_evt_bh || ack_req) {
3042 		sz = event_reply->event_data_length * 4;
3043 		fwevt = mpi3mr_alloc_fwevt(sz);
3044 		if (!fwevt) {
3045 			ioc_info(mrioc, "%s :failure at %s:%d/%s()!\n",
3046 			    __func__, __FILE__, __LINE__, __func__);
3047 			return;
3048 		}
3049 
3050 		memcpy(fwevt->event_data, event_reply->event_data, sz);
3051 		fwevt->mrioc = mrioc;
3052 		fwevt->event_id = evt_type;
3053 		fwevt->send_ack = ack_req;
3054 		fwevt->process_evt = process_evt_bh;
3055 		fwevt->evt_ctx = le32_to_cpu(event_reply->event_context);
3056 		mpi3mr_fwevt_add_to_list(mrioc, fwevt);
3057 	}
3058 }
3059 
3060 /**
3061  * mpi3mr_setup_eedp - Setup EEDP information in MPI3 SCSI IO
3062  * @mrioc: Adapter instance reference
3063  * @scmd: SCSI command reference
3064  * @scsiio_req: MPI3 SCSI IO request
3065  *
3066  * Identifies the protection information flags from the SCSI
3067  * command and set appropriate flags in the MPI3 SCSI IO
3068  * request.
3069  *
3070  * Return: Nothing
3071  */
3072 static void mpi3mr_setup_eedp(struct mpi3mr_ioc *mrioc,
3073 	struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
3074 {
3075 	u16 eedp_flags = 0;
3076 	unsigned char prot_op = scsi_get_prot_op(scmd);
3077 
3078 	switch (prot_op) {
3079 	case SCSI_PROT_NORMAL:
3080 		return;
3081 	case SCSI_PROT_READ_STRIP:
3082 		eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REMOVE;
3083 		break;
3084 	case SCSI_PROT_WRITE_INSERT:
3085 		eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_INSERT;
3086 		break;
3087 	case SCSI_PROT_READ_INSERT:
3088 		eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_INSERT;
3089 		scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
3090 		break;
3091 	case SCSI_PROT_WRITE_STRIP:
3092 		eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REMOVE;
3093 		scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
3094 		break;
3095 	case SCSI_PROT_READ_PASS:
3096 		eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK;
3097 		scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
3098 		break;
3099 	case SCSI_PROT_WRITE_PASS:
3100 		if (scmd->prot_flags & SCSI_PROT_IP_CHECKSUM) {
3101 			eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REGEN;
3102 			scsiio_req->sgl[0].eedp.application_tag_translation_mask =
3103 			    0xffff;
3104 		} else
3105 			eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK;
3106 
3107 		scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
3108 		break;
3109 	default:
3110 		return;
3111 	}
3112 
3113 	if (scmd->prot_flags & SCSI_PROT_GUARD_CHECK)
3114 		eedp_flags |= MPI3_EEDPFLAGS_CHK_GUARD;
3115 
3116 	if (scmd->prot_flags & SCSI_PROT_IP_CHECKSUM)
3117 		eedp_flags |= MPI3_EEDPFLAGS_HOST_GUARD_IP_CHKSUM;
3118 
3119 	if (scmd->prot_flags & SCSI_PROT_REF_CHECK) {
3120 		eedp_flags |= MPI3_EEDPFLAGS_CHK_REF_TAG |
3121 			MPI3_EEDPFLAGS_INCR_PRI_REF_TAG;
3122 		scsiio_req->cdb.eedp32.primary_reference_tag =
3123 			cpu_to_be32(scsi_prot_ref_tag(scmd));
3124 	}
3125 
3126 	if (scmd->prot_flags & SCSI_PROT_REF_INCREMENT)
3127 		eedp_flags |= MPI3_EEDPFLAGS_INCR_PRI_REF_TAG;
3128 
3129 	eedp_flags |= MPI3_EEDPFLAGS_ESC_MODE_APPTAG_DISABLE;
3130 
3131 	switch (scsi_prot_interval(scmd)) {
3132 	case 512:
3133 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_512;
3134 		break;
3135 	case 520:
3136 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_520;
3137 		break;
3138 	case 4080:
3139 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4080;
3140 		break;
3141 	case 4088:
3142 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4088;
3143 		break;
3144 	case 4096:
3145 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4096;
3146 		break;
3147 	case 4104:
3148 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4104;
3149 		break;
3150 	case 4160:
3151 		scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4160;
3152 		break;
3153 	default:
3154 		break;
3155 	}
3156 
3157 	scsiio_req->sgl[0].eedp.eedp_flags = cpu_to_le16(eedp_flags);
3158 	scsiio_req->sgl[0].eedp.flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_EXTENDED;
3159 }
3160 
3161 /**
3162  * mpi3mr_build_sense_buffer - Map sense information
3163  * @desc: Sense type
3164  * @buf: Sense buffer to populate
3165  * @key: Sense key
3166  * @asc: Additional sense code
3167  * @ascq: Additional sense code qualifier
3168  *
3169  * Maps the given sense information into either descriptor or
3170  * fixed format sense data.
3171  *
3172  * Return: Nothing
3173  */
3174 static inline void mpi3mr_build_sense_buffer(int desc, u8 *buf, u8 key,
3175 	u8 asc, u8 ascq)
3176 {
3177 	if (desc) {
3178 		buf[0] = 0x72;	/* descriptor, current */
3179 		buf[1] = key;
3180 		buf[2] = asc;
3181 		buf[3] = ascq;
3182 		buf[7] = 0;
3183 	} else {
3184 		buf[0] = 0x70;	/* fixed, current */
3185 		buf[2] = key;
3186 		buf[7] = 0xa;
3187 		buf[12] = asc;
3188 		buf[13] = ascq;
3189 	}
3190 }
3191 
3192 /**
3193  * mpi3mr_map_eedp_error - Map EEDP errors from IOC status
3194  * @scmd: SCSI command reference
3195  * @ioc_status: status of MPI3 request
3196  *
3197  * Maps the EEDP error status of the SCSI IO request to sense
3198  * data.
3199  *
3200  * Return: Nothing
3201  */
3202 static void mpi3mr_map_eedp_error(struct scsi_cmnd *scmd,
3203 	u16 ioc_status)
3204 {
3205 	u8 ascq = 0;
3206 
3207 	switch (ioc_status) {
3208 	case MPI3_IOCSTATUS_EEDP_GUARD_ERROR:
3209 		ascq = 0x01;
3210 		break;
3211 	case MPI3_IOCSTATUS_EEDP_APP_TAG_ERROR:
3212 		ascq = 0x02;
3213 		break;
3214 	case MPI3_IOCSTATUS_EEDP_REF_TAG_ERROR:
3215 		ascq = 0x03;
3216 		break;
3217 	default:
3218 		ascq = 0x00;
3219 		break;
3220 	}
3221 
3222 	mpi3mr_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
3223 	    0x10, ascq);
3224 	scmd->result = (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION;
3225 }
3226 
3227 /**
3228  * mpi3mr_process_op_reply_desc - reply descriptor handler
3229  * @mrioc: Adapter instance reference
3230  * @reply_desc: Operational reply descriptor
3231  * @reply_dma: place holder for reply DMA address
3232  * @qidx: Operational queue index
3233  *
3234  * Process the operational reply descriptor and identifies the
3235  * descriptor type. Based on the descriptor map the MPI3 request
3236  * status to a SCSI command status and calls scsi_done call
3237  * back.
3238  *
3239  * Return: Nothing
3240  */
3241 void mpi3mr_process_op_reply_desc(struct mpi3mr_ioc *mrioc,
3242 	struct mpi3_default_reply_descriptor *reply_desc, u64 *reply_dma, u16 qidx)
3243 {
3244 	u16 reply_desc_type, host_tag = 0;
3245 	u16 ioc_status = MPI3_IOCSTATUS_SUCCESS;
3246 	u32 ioc_loginfo = 0;
3247 	struct mpi3_status_reply_descriptor *status_desc = NULL;
3248 	struct mpi3_address_reply_descriptor *addr_desc = NULL;
3249 	struct mpi3_success_reply_descriptor *success_desc = NULL;
3250 	struct mpi3_scsi_io_reply *scsi_reply = NULL;
3251 	struct scsi_cmnd *scmd = NULL;
3252 	struct scmd_priv *priv = NULL;
3253 	u8 *sense_buf = NULL;
3254 	u8 scsi_state = 0, scsi_status = 0, sense_state = 0;
3255 	u32 xfer_count = 0, sense_count = 0, resp_data = 0;
3256 	u16 dev_handle = 0xFFFF;
3257 	struct scsi_sense_hdr sshdr;
3258 	struct mpi3mr_stgt_priv_data *stgt_priv_data = NULL;
3259 	struct mpi3mr_sdev_priv_data *sdev_priv_data = NULL;
3260 	u32 ioc_pend_data_len = 0, tg_pend_data_len = 0, data_len_blks = 0;
3261 	struct mpi3mr_throttle_group_info *tg = NULL;
3262 	u8 throttle_enabled_dev = 0;
3263 
3264 	*reply_dma = 0;
3265 	reply_desc_type = le16_to_cpu(reply_desc->reply_flags) &
3266 	    MPI3_REPLY_DESCRIPT_FLAGS_TYPE_MASK;
3267 	switch (reply_desc_type) {
3268 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_STATUS:
3269 		status_desc = (struct mpi3_status_reply_descriptor *)reply_desc;
3270 		host_tag = le16_to_cpu(status_desc->host_tag);
3271 		ioc_status = le16_to_cpu(status_desc->ioc_status);
3272 		if (ioc_status &
3273 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
3274 			ioc_loginfo = le32_to_cpu(status_desc->ioc_log_info);
3275 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
3276 		mpi3mr_reply_trigger(mrioc, ioc_status, ioc_loginfo);
3277 		break;
3278 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_ADDRESS_REPLY:
3279 		addr_desc = (struct mpi3_address_reply_descriptor *)reply_desc;
3280 		*reply_dma = le64_to_cpu(addr_desc->reply_frame_address);
3281 		scsi_reply = mpi3mr_get_reply_virt_addr(mrioc,
3282 		    *reply_dma);
3283 		if (!scsi_reply) {
3284 			panic("%s: scsi_reply is NULL, this shouldn't happen\n",
3285 			    mrioc->name);
3286 			goto out;
3287 		}
3288 		host_tag = le16_to_cpu(scsi_reply->host_tag);
3289 		ioc_status = le16_to_cpu(scsi_reply->ioc_status);
3290 		scsi_status = scsi_reply->scsi_status;
3291 		scsi_state = scsi_reply->scsi_state;
3292 		dev_handle = le16_to_cpu(scsi_reply->dev_handle);
3293 		sense_state = (scsi_state & MPI3_SCSI_STATE_SENSE_MASK);
3294 		xfer_count = le32_to_cpu(scsi_reply->transfer_count);
3295 		sense_count = le32_to_cpu(scsi_reply->sense_count);
3296 		resp_data = le32_to_cpu(scsi_reply->response_data);
3297 		sense_buf = mpi3mr_get_sensebuf_virt_addr(mrioc,
3298 		    le64_to_cpu(scsi_reply->sense_data_buffer_address));
3299 		if (ioc_status &
3300 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
3301 			ioc_loginfo = le32_to_cpu(scsi_reply->ioc_log_info);
3302 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
3303 		if (sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY)
3304 			panic("%s: Ran out of sense buffers\n", mrioc->name);
3305 		if (sense_buf) {
3306 			scsi_normalize_sense(sense_buf, sense_count, &sshdr);
3307 			mpi3mr_scsisense_trigger(mrioc, sshdr.sense_key,
3308 			    sshdr.asc, sshdr.ascq);
3309 		}
3310 		mpi3mr_reply_trigger(mrioc, ioc_status, ioc_loginfo);
3311 		break;
3312 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_SUCCESS:
3313 		success_desc = (struct mpi3_success_reply_descriptor *)reply_desc;
3314 		host_tag = le16_to_cpu(success_desc->host_tag);
3315 		break;
3316 	default:
3317 		break;
3318 	}
3319 	scmd = mpi3mr_scmd_from_host_tag(mrioc, host_tag, qidx);
3320 	if (!scmd) {
3321 		panic("%s: Cannot Identify scmd for host_tag 0x%x\n",
3322 		    mrioc->name, host_tag);
3323 		goto out;
3324 	}
3325 	priv = scsi_cmd_priv(scmd);
3326 
3327 	data_len_blks = scsi_bufflen(scmd) >> 9;
3328 	sdev_priv_data = scmd->device->hostdata;
3329 	if (sdev_priv_data) {
3330 		stgt_priv_data = sdev_priv_data->tgt_priv_data;
3331 		if (stgt_priv_data) {
3332 			tg = stgt_priv_data->throttle_group;
3333 			throttle_enabled_dev =
3334 			    stgt_priv_data->io_throttle_enabled;
3335 			dev_handle = stgt_priv_data->dev_handle;
3336 		}
3337 	}
3338 	if (unlikely((data_len_blks >= mrioc->io_throttle_data_length) &&
3339 	    throttle_enabled_dev)) {
3340 		ioc_pend_data_len = atomic_sub_return(data_len_blks,
3341 		    &mrioc->pend_large_data_sz);
3342 		if (tg) {
3343 			tg_pend_data_len = atomic_sub_return(data_len_blks,
3344 			    &tg->pend_large_data_sz);
3345 			if (tg->io_divert  && ((ioc_pend_data_len <=
3346 			    mrioc->io_throttle_low) &&
3347 			    (tg_pend_data_len <= tg->low))) {
3348 				tg->io_divert = 0;
3349 				mpi3mr_set_io_divert_for_all_vd_in_tg(
3350 				    mrioc, tg, 0);
3351 			}
3352 		} else {
3353 			if (ioc_pend_data_len <= mrioc->io_throttle_low)
3354 				stgt_priv_data->io_divert = 0;
3355 		}
3356 	} else if (unlikely((stgt_priv_data && stgt_priv_data->io_divert))) {
3357 		ioc_pend_data_len = atomic_read(&mrioc->pend_large_data_sz);
3358 		if (!tg) {
3359 			if (ioc_pend_data_len <= mrioc->io_throttle_low)
3360 				stgt_priv_data->io_divert = 0;
3361 
3362 		} else if (ioc_pend_data_len <= mrioc->io_throttle_low) {
3363 			tg_pend_data_len = atomic_read(&tg->pend_large_data_sz);
3364 			if (tg->io_divert  && (tg_pend_data_len <= tg->low)) {
3365 				tg->io_divert = 0;
3366 				mpi3mr_set_io_divert_for_all_vd_in_tg(
3367 				    mrioc, tg, 0);
3368 			}
3369 		}
3370 	}
3371 
3372 	if (success_desc) {
3373 		scmd->result = DID_OK << 16;
3374 		goto out_success;
3375 	}
3376 
3377 	scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_count);
3378 	if (ioc_status == MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN &&
3379 	    xfer_count == 0 && (scsi_status == MPI3_SCSI_STATUS_BUSY ||
3380 	    scsi_status == MPI3_SCSI_STATUS_RESERVATION_CONFLICT ||
3381 	    scsi_status == MPI3_SCSI_STATUS_TASK_SET_FULL))
3382 		ioc_status = MPI3_IOCSTATUS_SUCCESS;
3383 
3384 	if ((sense_state == MPI3_SCSI_STATE_SENSE_VALID) && sense_count &&
3385 	    sense_buf) {
3386 		u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE, sense_count);
3387 
3388 		memcpy(scmd->sense_buffer, sense_buf, sz);
3389 	}
3390 
3391 	switch (ioc_status) {
3392 	case MPI3_IOCSTATUS_BUSY:
3393 	case MPI3_IOCSTATUS_INSUFFICIENT_RESOURCES:
3394 		scmd->result = SAM_STAT_BUSY;
3395 		break;
3396 	case MPI3_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3397 		scmd->result = DID_NO_CONNECT << 16;
3398 		break;
3399 	case MPI3_IOCSTATUS_SCSI_IOC_TERMINATED:
3400 		scmd->result = DID_SOFT_ERROR << 16;
3401 		break;
3402 	case MPI3_IOCSTATUS_SCSI_TASK_TERMINATED:
3403 	case MPI3_IOCSTATUS_SCSI_EXT_TERMINATED:
3404 		scmd->result = DID_RESET << 16;
3405 		break;
3406 	case MPI3_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3407 		if ((xfer_count == 0) || (scmd->underflow > xfer_count))
3408 			scmd->result = DID_SOFT_ERROR << 16;
3409 		else
3410 			scmd->result = (DID_OK << 16) | scsi_status;
3411 		break;
3412 	case MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN:
3413 		scmd->result = (DID_OK << 16) | scsi_status;
3414 		if (sense_state == MPI3_SCSI_STATE_SENSE_VALID)
3415 			break;
3416 		if (xfer_count < scmd->underflow) {
3417 			if (scsi_status == SAM_STAT_BUSY)
3418 				scmd->result = SAM_STAT_BUSY;
3419 			else
3420 				scmd->result = DID_SOFT_ERROR << 16;
3421 		} else if ((scsi_state & (MPI3_SCSI_STATE_NO_SCSI_STATUS)) ||
3422 		    (sense_state != MPI3_SCSI_STATE_SENSE_NOT_AVAILABLE))
3423 			scmd->result = DID_SOFT_ERROR << 16;
3424 		else if (scsi_state & MPI3_SCSI_STATE_TERMINATED)
3425 			scmd->result = DID_RESET << 16;
3426 		break;
3427 	case MPI3_IOCSTATUS_SCSI_DATA_OVERRUN:
3428 		scsi_set_resid(scmd, 0);
3429 		fallthrough;
3430 	case MPI3_IOCSTATUS_SCSI_RECOVERED_ERROR:
3431 	case MPI3_IOCSTATUS_SUCCESS:
3432 		scmd->result = (DID_OK << 16) | scsi_status;
3433 		if ((scsi_state & (MPI3_SCSI_STATE_NO_SCSI_STATUS)) ||
3434 		    (sense_state == MPI3_SCSI_STATE_SENSE_FAILED) ||
3435 			(sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY))
3436 			scmd->result = DID_SOFT_ERROR << 16;
3437 		else if (scsi_state & MPI3_SCSI_STATE_TERMINATED)
3438 			scmd->result = DID_RESET << 16;
3439 		break;
3440 	case MPI3_IOCSTATUS_EEDP_GUARD_ERROR:
3441 	case MPI3_IOCSTATUS_EEDP_REF_TAG_ERROR:
3442 	case MPI3_IOCSTATUS_EEDP_APP_TAG_ERROR:
3443 		mpi3mr_map_eedp_error(scmd, ioc_status);
3444 		break;
3445 	case MPI3_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3446 	case MPI3_IOCSTATUS_INVALID_FUNCTION:
3447 	case MPI3_IOCSTATUS_INVALID_SGL:
3448 	case MPI3_IOCSTATUS_INTERNAL_ERROR:
3449 	case MPI3_IOCSTATUS_INVALID_FIELD:
3450 	case MPI3_IOCSTATUS_INVALID_STATE:
3451 	case MPI3_IOCSTATUS_SCSI_IO_DATA_ERROR:
3452 	case MPI3_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3453 	case MPI3_IOCSTATUS_INSUFFICIENT_POWER:
3454 	default:
3455 		scmd->result = DID_SOFT_ERROR << 16;
3456 		break;
3457 	}
3458 
3459 	if (scmd->result != (DID_OK << 16) && (scmd->cmnd[0] != ATA_12) &&
3460 	    (scmd->cmnd[0] != ATA_16) &&
3461 	    mrioc->logging_level & MPI3_DEBUG_SCSI_ERROR) {
3462 		ioc_info(mrioc, "%s :scmd->result 0x%x\n", __func__,
3463 		    scmd->result);
3464 		scsi_print_command(scmd);
3465 		ioc_info(mrioc,
3466 		    "%s :Command issued to handle 0x%02x returned with error 0x%04x loginfo 0x%08x, qid %d\n",
3467 		    __func__, dev_handle, ioc_status, ioc_loginfo,
3468 		    priv->req_q_idx + 1);
3469 		ioc_info(mrioc,
3470 		    " host_tag %d scsi_state 0x%02x scsi_status 0x%02x, xfer_cnt %d resp_data 0x%x\n",
3471 		    host_tag, scsi_state, scsi_status, xfer_count, resp_data);
3472 		if (sense_buf) {
3473 			scsi_normalize_sense(sense_buf, sense_count, &sshdr);
3474 			ioc_info(mrioc,
3475 			    "%s :sense_count 0x%x, sense_key 0x%x ASC 0x%x, ASCQ 0x%x\n",
3476 			    __func__, sense_count, sshdr.sense_key,
3477 			    sshdr.asc, sshdr.ascq);
3478 		}
3479 	}
3480 out_success:
3481 	if (priv->meta_sg_valid) {
3482 		dma_unmap_sg(&mrioc->pdev->dev, scsi_prot_sglist(scmd),
3483 		    scsi_prot_sg_count(scmd), scmd->sc_data_direction);
3484 	}
3485 	mpi3mr_clear_scmd_priv(mrioc, scmd);
3486 	scsi_dma_unmap(scmd);
3487 	scsi_done(scmd);
3488 out:
3489 	if (sense_buf)
3490 		mpi3mr_repost_sense_buf(mrioc,
3491 		    le64_to_cpu(scsi_reply->sense_data_buffer_address));
3492 }
3493 
3494 /**
3495  * mpi3mr_get_chain_idx - get free chain buffer index
3496  * @mrioc: Adapter instance reference
3497  *
3498  * Try to get a free chain buffer index from the free pool.
3499  *
3500  * Return: -1 on failure or the free chain buffer index
3501  */
3502 static int mpi3mr_get_chain_idx(struct mpi3mr_ioc *mrioc)
3503 {
3504 	u8 retry_count = 5;
3505 	int cmd_idx = -1;
3506 	unsigned long flags;
3507 
3508 	spin_lock_irqsave(&mrioc->chain_buf_lock, flags);
3509 	do {
3510 		cmd_idx = find_first_zero_bit(mrioc->chain_bitmap,
3511 		    mrioc->chain_buf_count);
3512 		if (cmd_idx < mrioc->chain_buf_count) {
3513 			set_bit(cmd_idx, mrioc->chain_bitmap);
3514 			break;
3515 		}
3516 		cmd_idx = -1;
3517 	} while (retry_count--);
3518 	spin_unlock_irqrestore(&mrioc->chain_buf_lock, flags);
3519 	return cmd_idx;
3520 }
3521 
3522 /**
3523  * mpi3mr_prepare_sg_scmd - build scatter gather list
3524  * @mrioc: Adapter instance reference
3525  * @scmd: SCSI command reference
3526  * @scsiio_req: MPI3 SCSI IO request
3527  *
3528  * This function maps SCSI command's data and protection SGEs to
3529  * MPI request SGEs. If required additional 4K chain buffer is
3530  * used to send the SGEs.
3531  *
3532  * Return: 0 on success, -ENOMEM on dma_map_sg failure
3533  */
3534 static int mpi3mr_prepare_sg_scmd(struct mpi3mr_ioc *mrioc,
3535 	struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
3536 {
3537 	dma_addr_t chain_dma;
3538 	struct scatterlist *sg_scmd;
3539 	void *sg_local, *chain;
3540 	u32 chain_length;
3541 	int sges_left, chain_idx;
3542 	u32 sges_in_segment;
3543 	u8 simple_sgl_flags;
3544 	u8 simple_sgl_flags_last;
3545 	u8 last_chain_sgl_flags;
3546 	struct chain_element *chain_req;
3547 	struct scmd_priv *priv = NULL;
3548 	u32 meta_sg = le32_to_cpu(scsiio_req->flags) &
3549 	    MPI3_SCSIIO_FLAGS_DMAOPERATION_HOST_PI;
3550 
3551 	priv = scsi_cmd_priv(scmd);
3552 
3553 	simple_sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_SIMPLE |
3554 	    MPI3_SGE_FLAGS_DLAS_SYSTEM;
3555 	simple_sgl_flags_last = simple_sgl_flags |
3556 	    MPI3_SGE_FLAGS_END_OF_LIST;
3557 	last_chain_sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_LAST_CHAIN |
3558 	    MPI3_SGE_FLAGS_DLAS_SYSTEM;
3559 
3560 	if (meta_sg)
3561 		sg_local = &scsiio_req->sgl[MPI3_SCSIIO_METASGL_INDEX];
3562 	else
3563 		sg_local = &scsiio_req->sgl;
3564 
3565 	if (!scsiio_req->data_length && !meta_sg) {
3566 		mpi3mr_build_zero_len_sge(sg_local);
3567 		return 0;
3568 	}
3569 
3570 	if (meta_sg) {
3571 		sg_scmd = scsi_prot_sglist(scmd);
3572 		sges_left = dma_map_sg(&mrioc->pdev->dev,
3573 		    scsi_prot_sglist(scmd),
3574 		    scsi_prot_sg_count(scmd),
3575 		    scmd->sc_data_direction);
3576 		priv->meta_sg_valid = 1; /* To unmap meta sg DMA */
3577 	} else {
3578 		sg_scmd = scsi_sglist(scmd);
3579 		sges_left = scsi_dma_map(scmd);
3580 	}
3581 
3582 	if (sges_left < 0) {
3583 		sdev_printk(KERN_ERR, scmd->device,
3584 		    "scsi_dma_map failed: request for %d bytes!\n",
3585 		    scsi_bufflen(scmd));
3586 		return -ENOMEM;
3587 	}
3588 	if (sges_left > mrioc->max_sgl_entries) {
3589 		sdev_printk(KERN_ERR, scmd->device,
3590 		    "scsi_dma_map returned unsupported sge count %d!\n",
3591 		    sges_left);
3592 		return -ENOMEM;
3593 	}
3594 
3595 	sges_in_segment = (mrioc->facts.op_req_sz -
3596 	    offsetof(struct mpi3_scsi_io_request, sgl)) / sizeof(struct mpi3_sge_common);
3597 
3598 	if (scsiio_req->sgl[0].eedp.flags ==
3599 	    MPI3_SGE_FLAGS_ELEMENT_TYPE_EXTENDED && !meta_sg) {
3600 		sg_local += sizeof(struct mpi3_sge_common);
3601 		sges_in_segment--;
3602 		/* Reserve 1st segment (scsiio_req->sgl[0]) for eedp */
3603 	}
3604 
3605 	if (scsiio_req->msg_flags ==
3606 	    MPI3_SCSIIO_MSGFLAGS_METASGL_VALID && !meta_sg) {
3607 		sges_in_segment--;
3608 		/* Reserve last segment (scsiio_req->sgl[3]) for meta sg */
3609 	}
3610 
3611 	if (meta_sg)
3612 		sges_in_segment = 1;
3613 
3614 	if (sges_left <= sges_in_segment)
3615 		goto fill_in_last_segment;
3616 
3617 	/* fill in main message segment when there is a chain following */
3618 	while (sges_in_segment > 1) {
3619 		mpi3mr_add_sg_single(sg_local, simple_sgl_flags,
3620 		    sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
3621 		sg_scmd = sg_next(sg_scmd);
3622 		sg_local += sizeof(struct mpi3_sge_common);
3623 		sges_left--;
3624 		sges_in_segment--;
3625 	}
3626 
3627 	chain_idx = mpi3mr_get_chain_idx(mrioc);
3628 	if (chain_idx < 0)
3629 		return -1;
3630 	chain_req = &mrioc->chain_sgl_list[chain_idx];
3631 	if (meta_sg)
3632 		priv->meta_chain_idx = chain_idx;
3633 	else
3634 		priv->chain_idx = chain_idx;
3635 
3636 	chain = chain_req->addr;
3637 	chain_dma = chain_req->dma_addr;
3638 	sges_in_segment = sges_left;
3639 	chain_length = sges_in_segment * sizeof(struct mpi3_sge_common);
3640 
3641 	mpi3mr_add_sg_single(sg_local, last_chain_sgl_flags,
3642 	    chain_length, chain_dma);
3643 
3644 	sg_local = chain;
3645 
3646 fill_in_last_segment:
3647 	while (sges_left > 0) {
3648 		if (sges_left == 1)
3649 			mpi3mr_add_sg_single(sg_local,
3650 			    simple_sgl_flags_last, sg_dma_len(sg_scmd),
3651 			    sg_dma_address(sg_scmd));
3652 		else
3653 			mpi3mr_add_sg_single(sg_local, simple_sgl_flags,
3654 			    sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
3655 		sg_scmd = sg_next(sg_scmd);
3656 		sg_local += sizeof(struct mpi3_sge_common);
3657 		sges_left--;
3658 	}
3659 
3660 	return 0;
3661 }
3662 
3663 /**
3664  * mpi3mr_build_sg_scmd - build scatter gather list for SCSI IO
3665  * @mrioc: Adapter instance reference
3666  * @scmd: SCSI command reference
3667  * @scsiio_req: MPI3 SCSI IO request
3668  *
3669  * This function calls mpi3mr_prepare_sg_scmd for constructing
3670  * both data SGEs and protection information SGEs in the MPI
3671  * format from the SCSI Command as appropriate .
3672  *
3673  * Return: return value of mpi3mr_prepare_sg_scmd.
3674  */
3675 static int mpi3mr_build_sg_scmd(struct mpi3mr_ioc *mrioc,
3676 	struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
3677 {
3678 	int ret;
3679 
3680 	ret = mpi3mr_prepare_sg_scmd(mrioc, scmd, scsiio_req);
3681 	if (ret)
3682 		return ret;
3683 
3684 	if (scsiio_req->msg_flags == MPI3_SCSIIO_MSGFLAGS_METASGL_VALID) {
3685 		/* There is a valid meta sg */
3686 		scsiio_req->flags |=
3687 		    cpu_to_le32(MPI3_SCSIIO_FLAGS_DMAOPERATION_HOST_PI);
3688 		ret = mpi3mr_prepare_sg_scmd(mrioc, scmd, scsiio_req);
3689 	}
3690 
3691 	return ret;
3692 }
3693 
3694 /**
3695  * mpi3mr_tm_response_name -  get TM response as a string
3696  * @resp_code: TM response code
3697  *
3698  * Convert known task management response code as a readable
3699  * string.
3700  *
3701  * Return: response code string.
3702  */
3703 static const char *mpi3mr_tm_response_name(u8 resp_code)
3704 {
3705 	char *desc;
3706 
3707 	switch (resp_code) {
3708 	case MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE:
3709 		desc = "task management request completed";
3710 		break;
3711 	case MPI3_SCSITASKMGMT_RSPCODE_INVALID_FRAME:
3712 		desc = "invalid frame";
3713 		break;
3714 	case MPI3_SCSITASKMGMT_RSPCODE_TM_FUNCTION_NOT_SUPPORTED:
3715 		desc = "task management request not supported";
3716 		break;
3717 	case MPI3_SCSITASKMGMT_RSPCODE_TM_FAILED:
3718 		desc = "task management request failed";
3719 		break;
3720 	case MPI3_SCSITASKMGMT_RSPCODE_TM_SUCCEEDED:
3721 		desc = "task management request succeeded";
3722 		break;
3723 	case MPI3_SCSITASKMGMT_RSPCODE_TM_INVALID_LUN:
3724 		desc = "invalid LUN";
3725 		break;
3726 	case MPI3_SCSITASKMGMT_RSPCODE_TM_OVERLAPPED_TAG:
3727 		desc = "overlapped tag attempted";
3728 		break;
3729 	case MPI3_SCSITASKMGMT_RSPCODE_IO_QUEUED_ON_IOC:
3730 		desc = "task queued, however not sent to target";
3731 		break;
3732 	case MPI3_SCSITASKMGMT_RSPCODE_TM_NVME_DENIED:
3733 		desc = "task management request denied by NVMe device";
3734 		break;
3735 	default:
3736 		desc = "unknown";
3737 		break;
3738 	}
3739 
3740 	return desc;
3741 }
3742 
3743 inline void mpi3mr_poll_pend_io_completions(struct mpi3mr_ioc *mrioc)
3744 {
3745 	int i;
3746 	int num_of_reply_queues =
3747 	    mrioc->num_op_reply_q + mrioc->op_reply_q_offset;
3748 
3749 	for (i = mrioc->op_reply_q_offset; i < num_of_reply_queues; i++)
3750 		mpi3mr_process_op_reply_q(mrioc,
3751 		    mrioc->intr_info[i].op_reply_q);
3752 }
3753 
3754 /**
3755  * mpi3mr_issue_tm - Issue Task Management request
3756  * @mrioc: Adapter instance reference
3757  * @tm_type: Task Management type
3758  * @handle: Device handle
3759  * @lun: lun ID
3760  * @htag: Host tag of the TM request
3761  * @timeout: TM timeout value
3762  * @drv_cmd: Internal command tracker
3763  * @resp_code: Response code place holder
3764  * @scmd: SCSI command
3765  *
3766  * Issues a Task Management Request to the controller for a
3767  * specified target, lun and command and wait for its completion
3768  * and check TM response. Recover the TM if it timed out by
3769  * issuing controller reset.
3770  *
3771  * Return: 0 on success, non-zero on errors
3772  */
3773 int mpi3mr_issue_tm(struct mpi3mr_ioc *mrioc, u8 tm_type,
3774 	u16 handle, uint lun, u16 htag, ulong timeout,
3775 	struct mpi3mr_drv_cmd *drv_cmd,
3776 	u8 *resp_code, struct scsi_cmnd *scmd)
3777 {
3778 	struct mpi3_scsi_task_mgmt_request tm_req;
3779 	struct mpi3_scsi_task_mgmt_reply *tm_reply = NULL;
3780 	int retval = 0;
3781 	struct mpi3mr_tgt_dev *tgtdev = NULL;
3782 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
3783 	struct scmd_priv *cmd_priv = NULL;
3784 	struct scsi_device *sdev = NULL;
3785 	struct mpi3mr_sdev_priv_data *sdev_priv_data = NULL;
3786 
3787 	ioc_info(mrioc, "%s :Issue TM: TM type (0x%x) for devhandle 0x%04x\n",
3788 	     __func__, tm_type, handle);
3789 	if (mrioc->unrecoverable) {
3790 		retval = -1;
3791 		ioc_err(mrioc, "%s :Issue TM: Unrecoverable controller\n",
3792 		    __func__);
3793 		goto out;
3794 	}
3795 
3796 	memset(&tm_req, 0, sizeof(tm_req));
3797 	mutex_lock(&drv_cmd->mutex);
3798 	if (drv_cmd->state & MPI3MR_CMD_PENDING) {
3799 		retval = -1;
3800 		ioc_err(mrioc, "%s :Issue TM: Command is in use\n", __func__);
3801 		mutex_unlock(&drv_cmd->mutex);
3802 		goto out;
3803 	}
3804 	if (mrioc->reset_in_progress) {
3805 		retval = -1;
3806 		ioc_err(mrioc, "%s :Issue TM: Reset in progress\n", __func__);
3807 		mutex_unlock(&drv_cmd->mutex);
3808 		goto out;
3809 	}
3810 	if (mrioc->block_on_pci_err) {
3811 		retval = -1;
3812 		dprint_tm(mrioc, "sending task management failed due to\n"
3813 				"pci error recovery in progress\n");
3814 		mutex_unlock(&drv_cmd->mutex);
3815 		goto out;
3816 	}
3817 
3818 	drv_cmd->state = MPI3MR_CMD_PENDING;
3819 	drv_cmd->is_waiting = 1;
3820 	drv_cmd->callback = NULL;
3821 	tm_req.dev_handle = cpu_to_le16(handle);
3822 	tm_req.task_type = tm_type;
3823 	tm_req.host_tag = cpu_to_le16(htag);
3824 
3825 	int_to_scsilun(lun, (struct scsi_lun *)tm_req.lun);
3826 	tm_req.function = MPI3_FUNCTION_SCSI_TASK_MGMT;
3827 
3828 	tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
3829 
3830 	if (scmd) {
3831 		sdev = scmd->device;
3832 		sdev_priv_data = sdev->hostdata;
3833 		scsi_tgt_priv_data = ((sdev_priv_data) ?
3834 		    sdev_priv_data->tgt_priv_data : NULL);
3835 	} else {
3836 		if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
3837 			scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
3838 			    tgtdev->starget->hostdata;
3839 	}
3840 
3841 	if (scsi_tgt_priv_data)
3842 		atomic_inc(&scsi_tgt_priv_data->block_io);
3843 
3844 	if (tgtdev && (tgtdev->dev_type == MPI3_DEVICE_DEVFORM_PCIE)) {
3845 		if (cmd_priv && tgtdev->dev_spec.pcie_inf.abort_to)
3846 			timeout = tgtdev->dev_spec.pcie_inf.abort_to;
3847 		else if (!cmd_priv && tgtdev->dev_spec.pcie_inf.reset_to)
3848 			timeout = tgtdev->dev_spec.pcie_inf.reset_to;
3849 	}
3850 
3851 	init_completion(&drv_cmd->done);
3852 	retval = mpi3mr_admin_request_post(mrioc, &tm_req, sizeof(tm_req), 1);
3853 	if (retval) {
3854 		ioc_err(mrioc, "%s :Issue TM: Admin Post failed\n", __func__);
3855 		goto out_unlock;
3856 	}
3857 	wait_for_completion_timeout(&drv_cmd->done, (timeout * HZ));
3858 
3859 	if (!(drv_cmd->state & MPI3MR_CMD_COMPLETE)) {
3860 		drv_cmd->is_waiting = 0;
3861 		retval = -1;
3862 		if (!(drv_cmd->state & MPI3MR_CMD_RESET)) {
3863 			dprint_tm(mrioc,
3864 			    "task management request timed out after %ld seconds\n",
3865 			    timeout);
3866 			if (mrioc->logging_level & MPI3_DEBUG_TM)
3867 				dprint_dump_req(&tm_req, sizeof(tm_req)/4);
3868 			mpi3mr_soft_reset_handler(mrioc,
3869 			    MPI3MR_RESET_FROM_TM_TIMEOUT, 1);
3870 		}
3871 		goto out_unlock;
3872 	}
3873 
3874 	if (!(drv_cmd->state & MPI3MR_CMD_REPLY_VALID)) {
3875 		dprint_tm(mrioc, "invalid task management reply message\n");
3876 		retval = -1;
3877 		goto out_unlock;
3878 	}
3879 
3880 	tm_reply = (struct mpi3_scsi_task_mgmt_reply *)drv_cmd->reply;
3881 
3882 	switch (drv_cmd->ioc_status) {
3883 	case MPI3_IOCSTATUS_SUCCESS:
3884 		*resp_code = le32_to_cpu(tm_reply->response_data) &
3885 			MPI3MR_RI_MASK_RESPCODE;
3886 		break;
3887 	case MPI3_IOCSTATUS_SCSI_IOC_TERMINATED:
3888 		*resp_code = MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE;
3889 		break;
3890 	default:
3891 		dprint_tm(mrioc,
3892 		    "task management request to handle(0x%04x) is failed with ioc_status(0x%04x) log_info(0x%08x)\n",
3893 		    handle, drv_cmd->ioc_status, drv_cmd->ioc_loginfo);
3894 		retval = -1;
3895 		goto out_unlock;
3896 	}
3897 
3898 	switch (*resp_code) {
3899 	case MPI3_SCSITASKMGMT_RSPCODE_TM_SUCCEEDED:
3900 	case MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE:
3901 		break;
3902 	case MPI3_SCSITASKMGMT_RSPCODE_IO_QUEUED_ON_IOC:
3903 		if (tm_type != MPI3_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
3904 			retval = -1;
3905 		break;
3906 	default:
3907 		retval = -1;
3908 		break;
3909 	}
3910 
3911 	dprint_tm(mrioc,
3912 	    "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",
3913 	    tm_type, handle, drv_cmd->ioc_status, drv_cmd->ioc_loginfo,
3914 	    le32_to_cpu(tm_reply->termination_count),
3915 	    mpi3mr_tm_response_name(*resp_code), *resp_code);
3916 
3917 	if (!retval) {
3918 		mpi3mr_ioc_disable_intr(mrioc);
3919 		mpi3mr_poll_pend_io_completions(mrioc);
3920 		mpi3mr_ioc_enable_intr(mrioc);
3921 		mpi3mr_poll_pend_io_completions(mrioc);
3922 		mpi3mr_process_admin_reply_q(mrioc);
3923 	}
3924 	switch (tm_type) {
3925 	case MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
3926 		if (!scsi_tgt_priv_data)
3927 			break;
3928 		scsi_tgt_priv_data->pend_count = 0;
3929 		blk_mq_tagset_busy_iter(&mrioc->shost->tag_set,
3930 		    mpi3mr_count_tgt_pending,
3931 		    (void *)scsi_tgt_priv_data->starget);
3932 		break;
3933 	case MPI3_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
3934 		if (!sdev_priv_data)
3935 			break;
3936 		sdev_priv_data->pend_count = 0;
3937 		blk_mq_tagset_busy_iter(&mrioc->shost->tag_set,
3938 		    mpi3mr_count_dev_pending, (void *)sdev);
3939 		break;
3940 	default:
3941 		break;
3942 	}
3943 	mpi3mr_global_trigger(mrioc,
3944 	    MPI3_DRIVER2_GLOBALTRIGGER_TASK_MANAGEMENT_ENABLED);
3945 
3946 out_unlock:
3947 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
3948 	mutex_unlock(&drv_cmd->mutex);
3949 	if (scsi_tgt_priv_data)
3950 		atomic_dec_if_positive(&scsi_tgt_priv_data->block_io);
3951 	if (tgtdev)
3952 		mpi3mr_tgtdev_put(tgtdev);
3953 out:
3954 	return retval;
3955 }
3956 
3957 /**
3958  * mpi3mr_bios_param - BIOS param callback
3959  * @sdev: SCSI device reference
3960  * @bdev: Block device reference
3961  * @capacity: Capacity in logical sectors
3962  * @params: Parameter array
3963  *
3964  * Just the parameters with heads/secots/cylinders.
3965  *
3966  * Return: 0 always
3967  */
3968 static int mpi3mr_bios_param(struct scsi_device *sdev,
3969 	struct block_device *bdev, sector_t capacity, int params[])
3970 {
3971 	int heads;
3972 	int sectors;
3973 	sector_t cylinders;
3974 	ulong dummy;
3975 
3976 	heads = 64;
3977 	sectors = 32;
3978 
3979 	dummy = heads * sectors;
3980 	cylinders = capacity;
3981 	sector_div(cylinders, dummy);
3982 
3983 	if ((ulong)capacity >= 0x200000) {
3984 		heads = 255;
3985 		sectors = 63;
3986 		dummy = heads * sectors;
3987 		cylinders = capacity;
3988 		sector_div(cylinders, dummy);
3989 	}
3990 
3991 	params[0] = heads;
3992 	params[1] = sectors;
3993 	params[2] = cylinders;
3994 	return 0;
3995 }
3996 
3997 /**
3998  * mpi3mr_map_queues - Map queues callback handler
3999  * @shost: SCSI host reference
4000  *
4001  * Maps default and poll queues.
4002  *
4003  * Return: return zero.
4004  */
4005 static void mpi3mr_map_queues(struct Scsi_Host *shost)
4006 {
4007 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
4008 	int i, qoff, offset;
4009 	struct blk_mq_queue_map *map = NULL;
4010 
4011 	offset = mrioc->op_reply_q_offset;
4012 
4013 	for (i = 0, qoff = 0; i < HCTX_MAX_TYPES; i++) {
4014 		map = &shost->tag_set.map[i];
4015 
4016 		map->nr_queues  = 0;
4017 
4018 		if (i == HCTX_TYPE_DEFAULT)
4019 			map->nr_queues = mrioc->default_qcount;
4020 		else if (i == HCTX_TYPE_POLL)
4021 			map->nr_queues = mrioc->active_poll_qcount;
4022 
4023 		if (!map->nr_queues) {
4024 			BUG_ON(i == HCTX_TYPE_DEFAULT);
4025 			continue;
4026 		}
4027 
4028 		/*
4029 		 * The poll queue(s) doesn't have an IRQ (and hence IRQ
4030 		 * affinity), so use the regular blk-mq cpu mapping
4031 		 */
4032 		map->queue_offset = qoff;
4033 		if (i != HCTX_TYPE_POLL)
4034 			blk_mq_pci_map_queues(map, mrioc->pdev, offset);
4035 		else
4036 			blk_mq_map_queues(map);
4037 
4038 		qoff += map->nr_queues;
4039 		offset += map->nr_queues;
4040 	}
4041 }
4042 
4043 /**
4044  * mpi3mr_get_fw_pending_ios - Calculate pending I/O count
4045  * @mrioc: Adapter instance reference
4046  *
4047  * Calculate the pending I/Os for the controller and return.
4048  *
4049  * Return: Number of pending I/Os
4050  */
4051 static inline int mpi3mr_get_fw_pending_ios(struct mpi3mr_ioc *mrioc)
4052 {
4053 	u16 i;
4054 	uint pend_ios = 0;
4055 
4056 	for (i = 0; i < mrioc->num_op_reply_q; i++)
4057 		pend_ios += atomic_read(&mrioc->op_reply_qinfo[i].pend_ios);
4058 	return pend_ios;
4059 }
4060 
4061 /**
4062  * mpi3mr_print_pending_host_io - print pending I/Os
4063  * @mrioc: Adapter instance reference
4064  *
4065  * Print number of pending I/Os and each I/O details prior to
4066  * reset for debug purpose.
4067  *
4068  * Return: Nothing
4069  */
4070 static void mpi3mr_print_pending_host_io(struct mpi3mr_ioc *mrioc)
4071 {
4072 	struct Scsi_Host *shost = mrioc->shost;
4073 
4074 	ioc_info(mrioc, "%s :Pending commands prior to reset: %d\n",
4075 	    __func__, mpi3mr_get_fw_pending_ios(mrioc));
4076 	blk_mq_tagset_busy_iter(&shost->tag_set,
4077 	    mpi3mr_print_scmd, (void *)mrioc);
4078 }
4079 
4080 /**
4081  * mpi3mr_wait_for_host_io - block for I/Os to complete
4082  * @mrioc: Adapter instance reference
4083  * @timeout: time out in seconds
4084  * Waits for pending I/Os for the given adapter to complete or
4085  * to hit the timeout.
4086  *
4087  * Return: Nothing
4088  */
4089 void mpi3mr_wait_for_host_io(struct mpi3mr_ioc *mrioc, u32 timeout)
4090 {
4091 	enum mpi3mr_iocstate iocstate;
4092 	int i = 0;
4093 
4094 	iocstate = mpi3mr_get_iocstate(mrioc);
4095 	if (iocstate != MRIOC_STATE_READY)
4096 		return;
4097 
4098 	if (!mpi3mr_get_fw_pending_ios(mrioc))
4099 		return;
4100 	ioc_info(mrioc,
4101 	    "%s :Waiting for %d seconds prior to reset for %d I/O\n",
4102 	    __func__, timeout, mpi3mr_get_fw_pending_ios(mrioc));
4103 
4104 	for (i = 0; i < timeout; i++) {
4105 		if (!mpi3mr_get_fw_pending_ios(mrioc))
4106 			break;
4107 		iocstate = mpi3mr_get_iocstate(mrioc);
4108 		if (iocstate != MRIOC_STATE_READY)
4109 			break;
4110 		msleep(1000);
4111 	}
4112 
4113 	ioc_info(mrioc, "%s :Pending I/Os after wait is: %d\n", __func__,
4114 	    mpi3mr_get_fw_pending_ios(mrioc));
4115 }
4116 
4117 /**
4118  * mpi3mr_setup_divert_ws - Setup Divert IO flag for write same
4119  * @mrioc: Adapter instance reference
4120  * @scmd: SCSI command reference
4121  * @scsiio_req: MPI3 SCSI IO request
4122  * @scsiio_flags: Pointer to MPI3 SCSI IO Flags
4123  * @wslen: write same max length
4124  *
4125  * Gets values of unmap, ndob and number of blocks from write
4126  * same scsi io and based on these values it sets divert IO flag
4127  * and reason for diverting IO to firmware.
4128  *
4129  * Return: Nothing
4130  */
4131 static inline void mpi3mr_setup_divert_ws(struct mpi3mr_ioc *mrioc,
4132 	struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req,
4133 	u32 *scsiio_flags, u16 wslen)
4134 {
4135 	u8 unmap = 0, ndob = 0;
4136 	u8 opcode = scmd->cmnd[0];
4137 	u32 num_blocks = 0;
4138 	u16 sa = (scmd->cmnd[8] << 8) | (scmd->cmnd[9]);
4139 
4140 	if (opcode == WRITE_SAME_16) {
4141 		unmap = scmd->cmnd[1] & 0x08;
4142 		ndob = scmd->cmnd[1] & 0x01;
4143 		num_blocks = get_unaligned_be32(scmd->cmnd + 10);
4144 	} else if ((opcode == VARIABLE_LENGTH_CMD) && (sa == WRITE_SAME_32)) {
4145 		unmap = scmd->cmnd[10] & 0x08;
4146 		ndob = scmd->cmnd[10] & 0x01;
4147 		num_blocks = get_unaligned_be32(scmd->cmnd + 28);
4148 	} else
4149 		return;
4150 
4151 	if ((unmap) && (ndob) && (num_blocks > wslen)) {
4152 		scsiio_req->msg_flags |=
4153 		    MPI3_SCSIIO_MSGFLAGS_DIVERT_TO_FIRMWARE;
4154 		*scsiio_flags |=
4155 			MPI3_SCSIIO_FLAGS_DIVERT_REASON_WRITE_SAME_TOO_LARGE;
4156 	}
4157 }
4158 
4159 /**
4160  * mpi3mr_eh_host_reset - Host reset error handling callback
4161  * @scmd: SCSI command reference
4162  *
4163  * Issue controller reset
4164  *
4165  * Return: SUCCESS of successful reset else FAILED
4166  */
4167 static int mpi3mr_eh_host_reset(struct scsi_cmnd *scmd)
4168 {
4169 	struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
4170 	int retval = FAILED, ret;
4171 
4172 	ret = mpi3mr_soft_reset_handler(mrioc,
4173 	    MPI3MR_RESET_FROM_EH_HOS, 1);
4174 	if (ret)
4175 		goto out;
4176 
4177 	retval = SUCCESS;
4178 out:
4179 	sdev_printk(KERN_INFO, scmd->device,
4180 	    "Host reset is %s for scmd(%p)\n",
4181 	    ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4182 
4183 	return retval;
4184 }
4185 
4186 /**
4187  * mpi3mr_eh_bus_reset - Bus reset error handling callback
4188  * @scmd: SCSI command reference
4189  *
4190  * Checks whether pending I/Os are present for the RAID volume;
4191  * if not there's no need to reset the adapter.
4192  *
4193  * Return: SUCCESS of successful reset else FAILED
4194  */
4195 static int mpi3mr_eh_bus_reset(struct scsi_cmnd *scmd)
4196 {
4197 	struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
4198 	struct mpi3mr_stgt_priv_data *stgt_priv_data;
4199 	struct mpi3mr_sdev_priv_data *sdev_priv_data;
4200 	u8 dev_type = MPI3_DEVICE_DEVFORM_VD;
4201 	int retval = FAILED;
4202 	unsigned int timeout = MPI3MR_RESET_TIMEOUT;
4203 
4204 	sdev_priv_data = scmd->device->hostdata;
4205 	if (sdev_priv_data && sdev_priv_data->tgt_priv_data) {
4206 		stgt_priv_data = sdev_priv_data->tgt_priv_data;
4207 		dev_type = stgt_priv_data->dev_type;
4208 	}
4209 
4210 	if (dev_type == MPI3_DEVICE_DEVFORM_VD) {
4211 		mpi3mr_wait_for_host_io(mrioc,
4212 			MPI3MR_RAID_ERRREC_RESET_TIMEOUT);
4213 		if (!mpi3mr_get_fw_pending_ios(mrioc)) {
4214 			while (mrioc->reset_in_progress ||
4215 			       mrioc->prepare_for_reset ||
4216 			       mrioc->block_on_pci_err) {
4217 				ssleep(1);
4218 				if (!timeout--) {
4219 					retval = FAILED;
4220 					goto out;
4221 				}
4222 			}
4223 			retval = SUCCESS;
4224 			goto out;
4225 		}
4226 	}
4227 	if (retval == FAILED)
4228 		mpi3mr_print_pending_host_io(mrioc);
4229 
4230 out:
4231 	sdev_printk(KERN_INFO, scmd->device,
4232 		"Bus reset is %s for scmd(%p)\n",
4233 		((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4234 	return retval;
4235 }
4236 
4237 /**
4238  * mpi3mr_eh_target_reset - Target reset error handling callback
4239  * @scmd: SCSI command reference
4240  *
4241  * Issue Target reset Task Management and verify the scmd is
4242  * terminated successfully and return status accordingly.
4243  *
4244  * Return: SUCCESS of successful termination of the scmd else
4245  *         FAILED
4246  */
4247 static int mpi3mr_eh_target_reset(struct scsi_cmnd *scmd)
4248 {
4249 	struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
4250 	struct mpi3mr_stgt_priv_data *stgt_priv_data;
4251 	struct mpi3mr_sdev_priv_data *sdev_priv_data;
4252 	u16 dev_handle;
4253 	u8 resp_code = 0;
4254 	int retval = FAILED, ret = 0;
4255 
4256 	sdev_printk(KERN_INFO, scmd->device,
4257 	    "Attempting Target Reset! scmd(%p)\n", scmd);
4258 	scsi_print_command(scmd);
4259 
4260 	sdev_priv_data = scmd->device->hostdata;
4261 	if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
4262 		sdev_printk(KERN_INFO, scmd->device,
4263 		    "SCSI device is not available\n");
4264 		retval = SUCCESS;
4265 		goto out;
4266 	}
4267 
4268 	stgt_priv_data = sdev_priv_data->tgt_priv_data;
4269 	dev_handle = stgt_priv_data->dev_handle;
4270 	if (stgt_priv_data->dev_removed) {
4271 		struct scmd_priv *cmd_priv = scsi_cmd_priv(scmd);
4272 		sdev_printk(KERN_INFO, scmd->device,
4273 		    "%s:target(handle = 0x%04x) is removed, target reset is not issued\n",
4274 		    mrioc->name, dev_handle);
4275 		if (!cmd_priv->in_lld_scope || cmd_priv->host_tag == MPI3MR_HOSTTAG_INVALID)
4276 			retval = SUCCESS;
4277 		else
4278 			retval = FAILED;
4279 		goto out;
4280 	}
4281 	sdev_printk(KERN_INFO, scmd->device,
4282 	    "Target Reset is issued to handle(0x%04x)\n",
4283 	    dev_handle);
4284 
4285 	ret = mpi3mr_issue_tm(mrioc,
4286 	    MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET, dev_handle,
4287 	    sdev_priv_data->lun_id, MPI3MR_HOSTTAG_BLK_TMS,
4288 	    MPI3MR_RESETTM_TIMEOUT, &mrioc->host_tm_cmds, &resp_code, scmd);
4289 
4290 	if (ret)
4291 		goto out;
4292 
4293 	if (stgt_priv_data->pend_count) {
4294 		sdev_printk(KERN_INFO, scmd->device,
4295 		    "%s: target has %d pending commands, target reset is failed\n",
4296 		    mrioc->name, stgt_priv_data->pend_count);
4297 		goto out;
4298 	}
4299 
4300 	retval = SUCCESS;
4301 out:
4302 	sdev_printk(KERN_INFO, scmd->device,
4303 	    "%s: target reset is %s for scmd(%p)\n", mrioc->name,
4304 	    ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4305 
4306 	return retval;
4307 }
4308 
4309 /**
4310  * mpi3mr_eh_dev_reset- Device reset error handling callback
4311  * @scmd: SCSI command reference
4312  *
4313  * Issue lun reset Task Management and verify the scmd is
4314  * terminated successfully and return status accordingly.
4315  *
4316  * Return: SUCCESS of successful termination of the scmd else
4317  *         FAILED
4318  */
4319 static int mpi3mr_eh_dev_reset(struct scsi_cmnd *scmd)
4320 {
4321 	struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
4322 	struct mpi3mr_stgt_priv_data *stgt_priv_data;
4323 	struct mpi3mr_sdev_priv_data *sdev_priv_data;
4324 	u16 dev_handle;
4325 	u8 resp_code = 0;
4326 	int retval = FAILED, ret = 0;
4327 
4328 	sdev_printk(KERN_INFO, scmd->device,
4329 	    "Attempting Device(lun) Reset! scmd(%p)\n", scmd);
4330 	scsi_print_command(scmd);
4331 
4332 	sdev_priv_data = scmd->device->hostdata;
4333 	if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
4334 		sdev_printk(KERN_INFO, scmd->device,
4335 		    "SCSI device is not available\n");
4336 		retval = SUCCESS;
4337 		goto out;
4338 	}
4339 
4340 	stgt_priv_data = sdev_priv_data->tgt_priv_data;
4341 	dev_handle = stgt_priv_data->dev_handle;
4342 	if (stgt_priv_data->dev_removed) {
4343 		struct scmd_priv *cmd_priv = scsi_cmd_priv(scmd);
4344 		sdev_printk(KERN_INFO, scmd->device,
4345 		    "%s: device(handle = 0x%04x) is removed, device(LUN) reset is not issued\n",
4346 		    mrioc->name, dev_handle);
4347 		if (!cmd_priv->in_lld_scope || cmd_priv->host_tag == MPI3MR_HOSTTAG_INVALID)
4348 			retval = SUCCESS;
4349 		else
4350 			retval = FAILED;
4351 		goto out;
4352 	}
4353 	sdev_printk(KERN_INFO, scmd->device,
4354 	    "Device(lun) Reset is issued to handle(0x%04x)\n", dev_handle);
4355 
4356 	ret = mpi3mr_issue_tm(mrioc,
4357 	    MPI3_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, dev_handle,
4358 	    sdev_priv_data->lun_id, MPI3MR_HOSTTAG_BLK_TMS,
4359 	    MPI3MR_RESETTM_TIMEOUT, &mrioc->host_tm_cmds, &resp_code, scmd);
4360 
4361 	if (ret)
4362 		goto out;
4363 
4364 	if (sdev_priv_data->pend_count) {
4365 		sdev_printk(KERN_INFO, scmd->device,
4366 		    "%s: device has %d pending commands, device(LUN) reset is failed\n",
4367 		    mrioc->name, sdev_priv_data->pend_count);
4368 		goto out;
4369 	}
4370 	retval = SUCCESS;
4371 out:
4372 	sdev_printk(KERN_INFO, scmd->device,
4373 	    "%s: device(LUN) reset is %s for scmd(%p)\n", mrioc->name,
4374 	    ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4375 
4376 	return retval;
4377 }
4378 
4379 /**
4380  * mpi3mr_scan_start - Scan start callback handler
4381  * @shost: SCSI host reference
4382  *
4383  * Issue port enable request asynchronously.
4384  *
4385  * Return: Nothing
4386  */
4387 static void mpi3mr_scan_start(struct Scsi_Host *shost)
4388 {
4389 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
4390 
4391 	mrioc->scan_started = 1;
4392 	ioc_info(mrioc, "%s :Issuing Port Enable\n", __func__);
4393 	if (mpi3mr_issue_port_enable(mrioc, 1)) {
4394 		ioc_err(mrioc, "%s :Issuing port enable failed\n", __func__);
4395 		mrioc->scan_started = 0;
4396 		mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
4397 	}
4398 }
4399 
4400 /**
4401  * mpi3mr_scan_finished - Scan finished callback handler
4402  * @shost: SCSI host reference
4403  * @time: Jiffies from the scan start
4404  *
4405  * Checks whether the port enable is completed or timedout or
4406  * failed and set the scan status accordingly after taking any
4407  * recovery if required.
4408  *
4409  * Return: 1 on scan finished or timed out, 0 for in progress
4410  */
4411 static int mpi3mr_scan_finished(struct Scsi_Host *shost,
4412 	unsigned long time)
4413 {
4414 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
4415 	u32 pe_timeout = MPI3MR_PORTENABLE_TIMEOUT;
4416 	u32 ioc_status = readl(&mrioc->sysif_regs->ioc_status);
4417 
4418 	if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) ||
4419 	    (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)) {
4420 		ioc_err(mrioc, "port enable failed due to fault or reset\n");
4421 		mpi3mr_print_fault_info(mrioc);
4422 		mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
4423 		mrioc->scan_started = 0;
4424 		mrioc->init_cmds.is_waiting = 0;
4425 		mrioc->init_cmds.callback = NULL;
4426 		mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
4427 	}
4428 
4429 	if (time >= (pe_timeout * HZ)) {
4430 		ioc_err(mrioc, "port enable failed due to time out\n");
4431 		mpi3mr_check_rh_fault_ioc(mrioc,
4432 		    MPI3MR_RESET_FROM_PE_TIMEOUT);
4433 		mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
4434 		mrioc->scan_started = 0;
4435 		mrioc->init_cmds.is_waiting = 0;
4436 		mrioc->init_cmds.callback = NULL;
4437 		mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
4438 	}
4439 
4440 	if (mrioc->scan_started)
4441 		return 0;
4442 
4443 	if (mrioc->scan_failed) {
4444 		ioc_err(mrioc,
4445 		    "port enable failed with status=0x%04x\n",
4446 		    mrioc->scan_failed);
4447 	} else
4448 		ioc_info(mrioc, "port enable is successfully completed\n");
4449 
4450 	mpi3mr_start_watchdog(mrioc);
4451 	mrioc->is_driver_loading = 0;
4452 	mrioc->stop_bsgs = 0;
4453 	return 1;
4454 }
4455 
4456 /**
4457  * mpi3mr_slave_destroy - Slave destroy callback handler
4458  * @sdev: SCSI device reference
4459  *
4460  * Cleanup and free per device(lun) private data.
4461  *
4462  * Return: Nothing.
4463  */
4464 static void mpi3mr_slave_destroy(struct scsi_device *sdev)
4465 {
4466 	struct Scsi_Host *shost;
4467 	struct mpi3mr_ioc *mrioc;
4468 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4469 	struct mpi3mr_tgt_dev *tgt_dev = NULL;
4470 	unsigned long flags;
4471 	struct scsi_target *starget;
4472 	struct sas_rphy *rphy = NULL;
4473 
4474 	if (!sdev->hostdata)
4475 		return;
4476 
4477 	starget = scsi_target(sdev);
4478 	shost = dev_to_shost(&starget->dev);
4479 	mrioc = shost_priv(shost);
4480 	scsi_tgt_priv_data = starget->hostdata;
4481 
4482 	scsi_tgt_priv_data->num_luns--;
4483 
4484 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4485 	if (starget->channel == mrioc->scsi_device_channel)
4486 		tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4487 	else if (mrioc->sas_transport_enabled && !starget->channel) {
4488 		rphy = dev_to_rphy(starget->dev.parent);
4489 		tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
4490 		    rphy->identify.sas_address, rphy);
4491 	}
4492 
4493 	if (tgt_dev && (!scsi_tgt_priv_data->num_luns))
4494 		tgt_dev->starget = NULL;
4495 	if (tgt_dev)
4496 		mpi3mr_tgtdev_put(tgt_dev);
4497 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4498 
4499 	kfree(sdev->hostdata);
4500 	sdev->hostdata = NULL;
4501 }
4502 
4503 /**
4504  * mpi3mr_target_destroy - Target destroy callback handler
4505  * @starget: SCSI target reference
4506  *
4507  * Cleanup and free per target private data.
4508  *
4509  * Return: Nothing.
4510  */
4511 static void mpi3mr_target_destroy(struct scsi_target *starget)
4512 {
4513 	struct Scsi_Host *shost;
4514 	struct mpi3mr_ioc *mrioc;
4515 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4516 	struct mpi3mr_tgt_dev *tgt_dev;
4517 	unsigned long flags;
4518 
4519 	if (!starget->hostdata)
4520 		return;
4521 
4522 	shost = dev_to_shost(&starget->dev);
4523 	mrioc = shost_priv(shost);
4524 	scsi_tgt_priv_data = starget->hostdata;
4525 
4526 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4527 	tgt_dev = __mpi3mr_get_tgtdev_from_tgtpriv(mrioc, scsi_tgt_priv_data);
4528 	if (tgt_dev && (tgt_dev->starget == starget) &&
4529 	    (tgt_dev->perst_id == starget->id))
4530 		tgt_dev->starget = NULL;
4531 	if (tgt_dev) {
4532 		scsi_tgt_priv_data->tgt_dev = NULL;
4533 		scsi_tgt_priv_data->perst_id = 0;
4534 		mpi3mr_tgtdev_put(tgt_dev);
4535 		mpi3mr_tgtdev_put(tgt_dev);
4536 	}
4537 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4538 
4539 	kfree(starget->hostdata);
4540 	starget->hostdata = NULL;
4541 }
4542 
4543 /**
4544  * mpi3mr_device_configure - Slave configure callback handler
4545  * @sdev: SCSI device reference
4546  * @lim: queue limits
4547  *
4548  * Configure queue depth, max hardware sectors and virt boundary
4549  * as required
4550  *
4551  * Return: 0 always.
4552  */
4553 static int mpi3mr_device_configure(struct scsi_device *sdev,
4554 		struct queue_limits *lim)
4555 {
4556 	struct scsi_target *starget;
4557 	struct Scsi_Host *shost;
4558 	struct mpi3mr_ioc *mrioc;
4559 	struct mpi3mr_tgt_dev *tgt_dev = NULL;
4560 	unsigned long flags;
4561 	int retval = 0;
4562 	struct sas_rphy *rphy = NULL;
4563 
4564 	starget = scsi_target(sdev);
4565 	shost = dev_to_shost(&starget->dev);
4566 	mrioc = shost_priv(shost);
4567 
4568 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4569 	if (starget->channel == mrioc->scsi_device_channel)
4570 		tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4571 	else if (mrioc->sas_transport_enabled && !starget->channel) {
4572 		rphy = dev_to_rphy(starget->dev.parent);
4573 		tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
4574 		    rphy->identify.sas_address, rphy);
4575 	}
4576 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4577 	if (!tgt_dev)
4578 		return -ENXIO;
4579 
4580 	mpi3mr_change_queue_depth(sdev, tgt_dev->q_depth);
4581 
4582 	sdev->eh_timeout = MPI3MR_EH_SCMD_TIMEOUT;
4583 	blk_queue_rq_timeout(sdev->request_queue, MPI3MR_SCMD_TIMEOUT);
4584 
4585 	mpi3mr_configure_tgt_dev(tgt_dev, lim);
4586 	mpi3mr_tgtdev_put(tgt_dev);
4587 	return retval;
4588 }
4589 
4590 /**
4591  * mpi3mr_slave_alloc -Slave alloc callback handler
4592  * @sdev: SCSI device reference
4593  *
4594  * Allocate per device(lun) private data and initialize it.
4595  *
4596  * Return: 0 on success -ENOMEM on memory allocation failure.
4597  */
4598 static int mpi3mr_slave_alloc(struct scsi_device *sdev)
4599 {
4600 	struct Scsi_Host *shost;
4601 	struct mpi3mr_ioc *mrioc;
4602 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4603 	struct mpi3mr_tgt_dev *tgt_dev = NULL;
4604 	struct mpi3mr_sdev_priv_data *scsi_dev_priv_data;
4605 	unsigned long flags;
4606 	struct scsi_target *starget;
4607 	int retval = 0;
4608 	struct sas_rphy *rphy = NULL;
4609 
4610 	starget = scsi_target(sdev);
4611 	shost = dev_to_shost(&starget->dev);
4612 	mrioc = shost_priv(shost);
4613 	scsi_tgt_priv_data = starget->hostdata;
4614 
4615 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4616 
4617 	if (starget->channel == mrioc->scsi_device_channel)
4618 		tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4619 	else if (mrioc->sas_transport_enabled && !starget->channel) {
4620 		rphy = dev_to_rphy(starget->dev.parent);
4621 		tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
4622 		    rphy->identify.sas_address, rphy);
4623 	}
4624 
4625 	if (tgt_dev) {
4626 		if (tgt_dev->starget == NULL)
4627 			tgt_dev->starget = starget;
4628 		mpi3mr_tgtdev_put(tgt_dev);
4629 		retval = 0;
4630 	} else {
4631 		spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4632 		return -ENXIO;
4633 	}
4634 
4635 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4636 
4637 	scsi_dev_priv_data = kzalloc(sizeof(*scsi_dev_priv_data), GFP_KERNEL);
4638 	if (!scsi_dev_priv_data)
4639 		return -ENOMEM;
4640 
4641 	scsi_dev_priv_data->lun_id = sdev->lun;
4642 	scsi_dev_priv_data->tgt_priv_data = scsi_tgt_priv_data;
4643 	sdev->hostdata = scsi_dev_priv_data;
4644 
4645 	scsi_tgt_priv_data->num_luns++;
4646 
4647 	return retval;
4648 }
4649 
4650 /**
4651  * mpi3mr_target_alloc - Target alloc callback handler
4652  * @starget: SCSI target reference
4653  *
4654  * Allocate per target private data and initialize it.
4655  *
4656  * Return: 0 on success -ENOMEM on memory allocation failure.
4657  */
4658 static int mpi3mr_target_alloc(struct scsi_target *starget)
4659 {
4660 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4661 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
4662 	struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4663 	struct mpi3mr_tgt_dev *tgt_dev;
4664 	unsigned long flags;
4665 	int retval = 0;
4666 	struct sas_rphy *rphy = NULL;
4667 
4668 	scsi_tgt_priv_data = kzalloc(sizeof(*scsi_tgt_priv_data), GFP_KERNEL);
4669 	if (!scsi_tgt_priv_data)
4670 		return -ENOMEM;
4671 
4672 	starget->hostdata = scsi_tgt_priv_data;
4673 
4674 	spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4675 	if (starget->channel == mrioc->scsi_device_channel) {
4676 		tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4677 		if (tgt_dev && !tgt_dev->is_hidden) {
4678 			scsi_tgt_priv_data->starget = starget;
4679 			scsi_tgt_priv_data->dev_handle = tgt_dev->dev_handle;
4680 			scsi_tgt_priv_data->perst_id = tgt_dev->perst_id;
4681 			scsi_tgt_priv_data->dev_type = tgt_dev->dev_type;
4682 			scsi_tgt_priv_data->tgt_dev = tgt_dev;
4683 			tgt_dev->starget = starget;
4684 			atomic_set(&scsi_tgt_priv_data->block_io, 0);
4685 			retval = 0;
4686 			if ((tgt_dev->dev_type == MPI3_DEVICE_DEVFORM_PCIE) &&
4687 			    ((tgt_dev->dev_spec.pcie_inf.dev_info &
4688 			    MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) ==
4689 			    MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) &&
4690 			    ((tgt_dev->dev_spec.pcie_inf.dev_info &
4691 			    MPI3_DEVICE0_PCIE_DEVICE_INFO_PITYPE_MASK) !=
4692 			    MPI3_DEVICE0_PCIE_DEVICE_INFO_PITYPE_0))
4693 				scsi_tgt_priv_data->dev_nvme_dif = 1;
4694 			scsi_tgt_priv_data->io_throttle_enabled = tgt_dev->io_throttle_enabled;
4695 			scsi_tgt_priv_data->wslen = tgt_dev->wslen;
4696 			if (tgt_dev->dev_type == MPI3_DEVICE_DEVFORM_VD)
4697 				scsi_tgt_priv_data->throttle_group = tgt_dev->dev_spec.vd_inf.tg;
4698 		} else
4699 			retval = -ENXIO;
4700 	} else if (mrioc->sas_transport_enabled && !starget->channel) {
4701 		rphy = dev_to_rphy(starget->dev.parent);
4702 		tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
4703 		    rphy->identify.sas_address, rphy);
4704 		if (tgt_dev && !tgt_dev->is_hidden && !tgt_dev->non_stl &&
4705 		    (tgt_dev->dev_type == MPI3_DEVICE_DEVFORM_SAS_SATA)) {
4706 			scsi_tgt_priv_data->starget = starget;
4707 			scsi_tgt_priv_data->dev_handle = tgt_dev->dev_handle;
4708 			scsi_tgt_priv_data->perst_id = tgt_dev->perst_id;
4709 			scsi_tgt_priv_data->dev_type = tgt_dev->dev_type;
4710 			scsi_tgt_priv_data->tgt_dev = tgt_dev;
4711 			scsi_tgt_priv_data->io_throttle_enabled = tgt_dev->io_throttle_enabled;
4712 			scsi_tgt_priv_data->wslen = tgt_dev->wslen;
4713 			tgt_dev->starget = starget;
4714 			atomic_set(&scsi_tgt_priv_data->block_io, 0);
4715 			retval = 0;
4716 		} else
4717 			retval = -ENXIO;
4718 	}
4719 	spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4720 
4721 	return retval;
4722 }
4723 
4724 /**
4725  * mpi3mr_check_return_unmap - Whether an unmap is allowed
4726  * @mrioc: Adapter instance reference
4727  * @scmd: SCSI Command reference
4728  *
4729  * The controller hardware cannot handle certain unmap commands
4730  * for NVMe drives, this routine checks those and return true
4731  * and completes the SCSI command with proper status and sense
4732  * data.
4733  *
4734  * Return: TRUE for not  allowed unmap, FALSE otherwise.
4735  */
4736 static bool mpi3mr_check_return_unmap(struct mpi3mr_ioc *mrioc,
4737 	struct scsi_cmnd *scmd)
4738 {
4739 	unsigned char *buf;
4740 	u16 param_len, desc_len, trunc_param_len;
4741 
4742 	trunc_param_len = param_len = get_unaligned_be16(scmd->cmnd + 7);
4743 
4744 	if (mrioc->pdev->revision) {
4745 		if ((param_len > 24) && ((param_len - 8) & 0xF)) {
4746 			trunc_param_len -= (param_len - 8) & 0xF;
4747 			dprint_scsi_command(mrioc, scmd, MPI3_DEBUG_SCSI_ERROR);
4748 			dprint_scsi_err(mrioc,
4749 			    "truncating param_len from (%d) to (%d)\n",
4750 			    param_len, trunc_param_len);
4751 			put_unaligned_be16(trunc_param_len, scmd->cmnd + 7);
4752 			dprint_scsi_command(mrioc, scmd, MPI3_DEBUG_SCSI_ERROR);
4753 		}
4754 		return false;
4755 	}
4756 
4757 	if (!param_len) {
4758 		ioc_warn(mrioc,
4759 		    "%s: cdb received with zero parameter length\n",
4760 		    __func__);
4761 		scsi_print_command(scmd);
4762 		scmd->result = DID_OK << 16;
4763 		scsi_done(scmd);
4764 		return true;
4765 	}
4766 
4767 	if (param_len < 24) {
4768 		ioc_warn(mrioc,
4769 		    "%s: cdb received with invalid param_len: %d\n",
4770 		    __func__, param_len);
4771 		scsi_print_command(scmd);
4772 		scmd->result = SAM_STAT_CHECK_CONDITION;
4773 		scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
4774 		    0x1A, 0);
4775 		scsi_done(scmd);
4776 		return true;
4777 	}
4778 	if (param_len != scsi_bufflen(scmd)) {
4779 		ioc_warn(mrioc,
4780 		    "%s: cdb received with param_len: %d bufflen: %d\n",
4781 		    __func__, param_len, scsi_bufflen(scmd));
4782 		scsi_print_command(scmd);
4783 		scmd->result = SAM_STAT_CHECK_CONDITION;
4784 		scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
4785 		    0x1A, 0);
4786 		scsi_done(scmd);
4787 		return true;
4788 	}
4789 	buf = kzalloc(scsi_bufflen(scmd), GFP_ATOMIC);
4790 	if (!buf) {
4791 		scsi_print_command(scmd);
4792 		scmd->result = SAM_STAT_CHECK_CONDITION;
4793 		scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
4794 		    0x55, 0x03);
4795 		scsi_done(scmd);
4796 		return true;
4797 	}
4798 	scsi_sg_copy_to_buffer(scmd, buf, scsi_bufflen(scmd));
4799 	desc_len = get_unaligned_be16(&buf[2]);
4800 
4801 	if (desc_len < 16) {
4802 		ioc_warn(mrioc,
4803 		    "%s: Invalid descriptor length in param list: %d\n",
4804 		    __func__, desc_len);
4805 		scsi_print_command(scmd);
4806 		scmd->result = SAM_STAT_CHECK_CONDITION;
4807 		scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
4808 		    0x26, 0);
4809 		scsi_done(scmd);
4810 		kfree(buf);
4811 		return true;
4812 	}
4813 
4814 	if (param_len > (desc_len + 8)) {
4815 		trunc_param_len = desc_len + 8;
4816 		scsi_print_command(scmd);
4817 		dprint_scsi_err(mrioc,
4818 		    "truncating param_len(%d) to desc_len+8(%d)\n",
4819 		    param_len, trunc_param_len);
4820 		put_unaligned_be16(trunc_param_len, scmd->cmnd + 7);
4821 		scsi_print_command(scmd);
4822 	}
4823 
4824 	kfree(buf);
4825 	return false;
4826 }
4827 
4828 /**
4829  * mpi3mr_allow_scmd_to_fw - Command is allowed during shutdown
4830  * @scmd: SCSI Command reference
4831  *
4832  * Checks whether a cdb is allowed during shutdown or not.
4833  *
4834  * Return: TRUE for allowed commands, FALSE otherwise.
4835  */
4836 
4837 inline bool mpi3mr_allow_scmd_to_fw(struct scsi_cmnd *scmd)
4838 {
4839 	switch (scmd->cmnd[0]) {
4840 	case SYNCHRONIZE_CACHE:
4841 	case START_STOP:
4842 		return true;
4843 	default:
4844 		return false;
4845 	}
4846 }
4847 
4848 /**
4849  * mpi3mr_qcmd - I/O request despatcher
4850  * @shost: SCSI Host reference
4851  * @scmd: SCSI Command reference
4852  *
4853  * Issues the SCSI Command as an MPI3 request.
4854  *
4855  * Return: 0 on successful queueing of the request or if the
4856  *         request is completed with failure.
4857  *         SCSI_MLQUEUE_DEVICE_BUSY when the device is busy.
4858  *         SCSI_MLQUEUE_HOST_BUSY when the host queue is full.
4859  */
4860 static int mpi3mr_qcmd(struct Scsi_Host *shost,
4861 	struct scsi_cmnd *scmd)
4862 {
4863 	struct mpi3mr_ioc *mrioc = shost_priv(shost);
4864 	struct mpi3mr_stgt_priv_data *stgt_priv_data;
4865 	struct mpi3mr_sdev_priv_data *sdev_priv_data;
4866 	struct scmd_priv *scmd_priv_data = NULL;
4867 	struct mpi3_scsi_io_request *scsiio_req = NULL;
4868 	struct op_req_qinfo *op_req_q = NULL;
4869 	int retval = 0;
4870 	u16 dev_handle;
4871 	u16 host_tag;
4872 	u32 scsiio_flags = 0, data_len_blks = 0;
4873 	struct request *rq = scsi_cmd_to_rq(scmd);
4874 	int iprio_class;
4875 	u8 is_pcie_dev = 0;
4876 	u32 tracked_io_sz = 0;
4877 	u32 ioc_pend_data_len = 0, tg_pend_data_len = 0;
4878 	struct mpi3mr_throttle_group_info *tg = NULL;
4879 
4880 	if (mrioc->unrecoverable) {
4881 		scmd->result = DID_ERROR << 16;
4882 		scsi_done(scmd);
4883 		goto out;
4884 	}
4885 
4886 	sdev_priv_data = scmd->device->hostdata;
4887 	if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
4888 		scmd->result = DID_NO_CONNECT << 16;
4889 		scsi_done(scmd);
4890 		goto out;
4891 	}
4892 
4893 	if (mrioc->stop_drv_processing &&
4894 	    !(mpi3mr_allow_scmd_to_fw(scmd))) {
4895 		scmd->result = DID_NO_CONNECT << 16;
4896 		scsi_done(scmd);
4897 		goto out;
4898 	}
4899 
4900 	stgt_priv_data = sdev_priv_data->tgt_priv_data;
4901 	dev_handle = stgt_priv_data->dev_handle;
4902 
4903 	/* Avoid error handling escalation when device is removed or blocked */
4904 
4905 	if (scmd->device->host->shost_state == SHOST_RECOVERY &&
4906 		scmd->cmnd[0] == TEST_UNIT_READY &&
4907 		(stgt_priv_data->dev_removed || (dev_handle == MPI3MR_INVALID_DEV_HANDLE))) {
4908 		scsi_build_sense(scmd, 0, UNIT_ATTENTION, 0x29, 0x07);
4909 		scsi_done(scmd);
4910 		goto out;
4911 	}
4912 
4913 	if (mrioc->reset_in_progress || mrioc->prepare_for_reset
4914 	    || mrioc->block_on_pci_err) {
4915 		retval = SCSI_MLQUEUE_HOST_BUSY;
4916 		goto out;
4917 	}
4918 
4919 	if (atomic_read(&stgt_priv_data->block_io)) {
4920 		if (mrioc->stop_drv_processing) {
4921 			scmd->result = DID_NO_CONNECT << 16;
4922 			scsi_done(scmd);
4923 			goto out;
4924 		}
4925 		retval = SCSI_MLQUEUE_DEVICE_BUSY;
4926 		goto out;
4927 	}
4928 
4929 	if (dev_handle == MPI3MR_INVALID_DEV_HANDLE) {
4930 		scmd->result = DID_NO_CONNECT << 16;
4931 		scsi_done(scmd);
4932 		goto out;
4933 	}
4934 	if (stgt_priv_data->dev_removed) {
4935 		scmd->result = DID_NO_CONNECT << 16;
4936 		scsi_done(scmd);
4937 		goto out;
4938 	}
4939 
4940 	if (stgt_priv_data->dev_type == MPI3_DEVICE_DEVFORM_PCIE)
4941 		is_pcie_dev = 1;
4942 	if ((scmd->cmnd[0] == UNMAP) && is_pcie_dev &&
4943 	    (mrioc->pdev->device == MPI3_MFGPAGE_DEVID_SAS4116) &&
4944 	    mpi3mr_check_return_unmap(mrioc, scmd))
4945 		goto out;
4946 
4947 	host_tag = mpi3mr_host_tag_for_scmd(mrioc, scmd);
4948 	if (host_tag == MPI3MR_HOSTTAG_INVALID) {
4949 		scmd->result = DID_ERROR << 16;
4950 		scsi_done(scmd);
4951 		goto out;
4952 	}
4953 
4954 	if (scmd->sc_data_direction == DMA_FROM_DEVICE)
4955 		scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_READ;
4956 	else if (scmd->sc_data_direction == DMA_TO_DEVICE)
4957 		scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_WRITE;
4958 	else
4959 		scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_NO_DATA_TRANSFER;
4960 
4961 	scsiio_flags |= MPI3_SCSIIO_FLAGS_TASKATTRIBUTE_SIMPLEQ;
4962 
4963 	if (sdev_priv_data->ncq_prio_enable) {
4964 		iprio_class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
4965 		if (iprio_class == IOPRIO_CLASS_RT)
4966 			scsiio_flags |= 1 << MPI3_SCSIIO_FLAGS_CMDPRI_SHIFT;
4967 	}
4968 
4969 	if (scmd->cmd_len > 16)
4970 		scsiio_flags |= MPI3_SCSIIO_FLAGS_CDB_GREATER_THAN_16;
4971 
4972 	scmd_priv_data = scsi_cmd_priv(scmd);
4973 	memset(scmd_priv_data->mpi3mr_scsiio_req, 0, MPI3MR_ADMIN_REQ_FRAME_SZ);
4974 	scsiio_req = (struct mpi3_scsi_io_request *)scmd_priv_data->mpi3mr_scsiio_req;
4975 	scsiio_req->function = MPI3_FUNCTION_SCSI_IO;
4976 	scsiio_req->host_tag = cpu_to_le16(host_tag);
4977 
4978 	mpi3mr_setup_eedp(mrioc, scmd, scsiio_req);
4979 
4980 	if (stgt_priv_data->wslen)
4981 		mpi3mr_setup_divert_ws(mrioc, scmd, scsiio_req, &scsiio_flags,
4982 		    stgt_priv_data->wslen);
4983 
4984 	memcpy(scsiio_req->cdb.cdb32, scmd->cmnd, scmd->cmd_len);
4985 	scsiio_req->data_length = cpu_to_le32(scsi_bufflen(scmd));
4986 	scsiio_req->dev_handle = cpu_to_le16(dev_handle);
4987 	scsiio_req->flags = cpu_to_le32(scsiio_flags);
4988 	int_to_scsilun(sdev_priv_data->lun_id,
4989 	    (struct scsi_lun *)scsiio_req->lun);
4990 
4991 	if (mpi3mr_build_sg_scmd(mrioc, scmd, scsiio_req)) {
4992 		mpi3mr_clear_scmd_priv(mrioc, scmd);
4993 		retval = SCSI_MLQUEUE_HOST_BUSY;
4994 		goto out;
4995 	}
4996 	op_req_q = &mrioc->req_qinfo[scmd_priv_data->req_q_idx];
4997 	data_len_blks = scsi_bufflen(scmd) >> 9;
4998 	if ((data_len_blks >= mrioc->io_throttle_data_length) &&
4999 	    stgt_priv_data->io_throttle_enabled) {
5000 		tracked_io_sz = data_len_blks;
5001 		tg = stgt_priv_data->throttle_group;
5002 		if (tg) {
5003 			ioc_pend_data_len = atomic_add_return(data_len_blks,
5004 			    &mrioc->pend_large_data_sz);
5005 			tg_pend_data_len = atomic_add_return(data_len_blks,
5006 			    &tg->pend_large_data_sz);
5007 			if (!tg->io_divert  && ((ioc_pend_data_len >=
5008 			    mrioc->io_throttle_high) ||
5009 			    (tg_pend_data_len >= tg->high))) {
5010 				tg->io_divert = 1;
5011 				tg->need_qd_reduction = 1;
5012 				mpi3mr_set_io_divert_for_all_vd_in_tg(mrioc,
5013 				    tg, 1);
5014 				mpi3mr_queue_qd_reduction_event(mrioc, tg);
5015 			}
5016 		} else {
5017 			ioc_pend_data_len = atomic_add_return(data_len_blks,
5018 			    &mrioc->pend_large_data_sz);
5019 			if (ioc_pend_data_len >= mrioc->io_throttle_high)
5020 				stgt_priv_data->io_divert = 1;
5021 		}
5022 	}
5023 
5024 	if (stgt_priv_data->io_divert) {
5025 		scsiio_req->msg_flags |=
5026 		    MPI3_SCSIIO_MSGFLAGS_DIVERT_TO_FIRMWARE;
5027 		scsiio_flags |= MPI3_SCSIIO_FLAGS_DIVERT_REASON_IO_THROTTLING;
5028 	}
5029 	scsiio_req->flags |= cpu_to_le32(scsiio_flags);
5030 
5031 	if (mpi3mr_op_request_post(mrioc, op_req_q,
5032 	    scmd_priv_data->mpi3mr_scsiio_req)) {
5033 		mpi3mr_clear_scmd_priv(mrioc, scmd);
5034 		retval = SCSI_MLQUEUE_HOST_BUSY;
5035 		if (tracked_io_sz) {
5036 			atomic_sub(tracked_io_sz, &mrioc->pend_large_data_sz);
5037 			if (tg)
5038 				atomic_sub(tracked_io_sz,
5039 				    &tg->pend_large_data_sz);
5040 		}
5041 		goto out;
5042 	}
5043 
5044 out:
5045 	return retval;
5046 }
5047 
5048 static const struct scsi_host_template mpi3mr_driver_template = {
5049 	.module				= THIS_MODULE,
5050 	.name				= "MPI3 Storage Controller",
5051 	.proc_name			= MPI3MR_DRIVER_NAME,
5052 	.queuecommand			= mpi3mr_qcmd,
5053 	.target_alloc			= mpi3mr_target_alloc,
5054 	.slave_alloc			= mpi3mr_slave_alloc,
5055 	.device_configure		= mpi3mr_device_configure,
5056 	.target_destroy			= mpi3mr_target_destroy,
5057 	.slave_destroy			= mpi3mr_slave_destroy,
5058 	.scan_finished			= mpi3mr_scan_finished,
5059 	.scan_start			= mpi3mr_scan_start,
5060 	.change_queue_depth		= mpi3mr_change_queue_depth,
5061 	.eh_device_reset_handler	= mpi3mr_eh_dev_reset,
5062 	.eh_target_reset_handler	= mpi3mr_eh_target_reset,
5063 	.eh_bus_reset_handler		= mpi3mr_eh_bus_reset,
5064 	.eh_host_reset_handler		= mpi3mr_eh_host_reset,
5065 	.bios_param			= mpi3mr_bios_param,
5066 	.map_queues			= mpi3mr_map_queues,
5067 	.mq_poll                        = mpi3mr_blk_mq_poll,
5068 	.no_write_same			= 1,
5069 	.can_queue			= 1,
5070 	.this_id			= -1,
5071 	.sg_tablesize			= MPI3MR_DEFAULT_SGL_ENTRIES,
5072 	/* max xfer supported is 1M (2K in 512 byte sized sectors)
5073 	 */
5074 	.max_sectors			= (MPI3MR_DEFAULT_MAX_IO_SIZE / 512),
5075 	.cmd_per_lun			= MPI3MR_MAX_CMDS_LUN,
5076 	.max_segment_size		= 0xffffffff,
5077 	.track_queue_depth		= 1,
5078 	.cmd_size			= sizeof(struct scmd_priv),
5079 	.shost_groups			= mpi3mr_host_groups,
5080 	.sdev_groups			= mpi3mr_dev_groups,
5081 };
5082 
5083 /**
5084  * mpi3mr_init_drv_cmd - Initialize internal command tracker
5085  * @cmdptr: Internal command tracker
5086  * @host_tag: Host tag used for the specific command
5087  *
5088  * Initialize the internal command tracker structure with
5089  * specified host tag.
5090  *
5091  * Return: Nothing.
5092  */
5093 static inline void mpi3mr_init_drv_cmd(struct mpi3mr_drv_cmd *cmdptr,
5094 	u16 host_tag)
5095 {
5096 	mutex_init(&cmdptr->mutex);
5097 	cmdptr->reply = NULL;
5098 	cmdptr->state = MPI3MR_CMD_NOTUSED;
5099 	cmdptr->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
5100 	cmdptr->host_tag = host_tag;
5101 }
5102 
5103 /**
5104  * osintfc_mrioc_security_status -Check controller secure status
5105  * @pdev: PCI device instance
5106  *
5107  * Read the Device Serial Number capability from PCI config
5108  * space and decide whether the controller is secure or not.
5109  *
5110  * Return: 0 on success, non-zero on failure.
5111  */
5112 static int
5113 osintfc_mrioc_security_status(struct pci_dev *pdev)
5114 {
5115 	u32 cap_data;
5116 	int base;
5117 	u32 ctlr_status;
5118 	u32 debug_status;
5119 	int retval = 0;
5120 
5121 	base = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DSN);
5122 	if (!base) {
5123 		dev_err(&pdev->dev,
5124 		    "%s: PCI_EXT_CAP_ID_DSN is not supported\n", __func__);
5125 		return -1;
5126 	}
5127 
5128 	pci_read_config_dword(pdev, base + 4, &cap_data);
5129 
5130 	debug_status = cap_data & MPI3MR_CTLR_SECURE_DBG_STATUS_MASK;
5131 	ctlr_status = cap_data & MPI3MR_CTLR_SECURITY_STATUS_MASK;
5132 
5133 	switch (ctlr_status) {
5134 	case MPI3MR_INVALID_DEVICE:
5135 		dev_err(&pdev->dev,
5136 		    "%s: Non secure ctlr (Invalid) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
5137 		    __func__, pdev->device, pdev->subsystem_vendor,
5138 		    pdev->subsystem_device);
5139 		retval = -1;
5140 		break;
5141 	case MPI3MR_CONFIG_SECURE_DEVICE:
5142 		if (!debug_status)
5143 			dev_info(&pdev->dev,
5144 			    "%s: Config secure ctlr is detected\n",
5145 			    __func__);
5146 		break;
5147 	case MPI3MR_HARD_SECURE_DEVICE:
5148 		break;
5149 	case MPI3MR_TAMPERED_DEVICE:
5150 		dev_err(&pdev->dev,
5151 		    "%s: Non secure ctlr (Tampered) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
5152 		    __func__, pdev->device, pdev->subsystem_vendor,
5153 		    pdev->subsystem_device);
5154 		retval = -1;
5155 		break;
5156 	default:
5157 		retval = -1;
5158 			break;
5159 	}
5160 
5161 	if (!retval && debug_status) {
5162 		dev_err(&pdev->dev,
5163 		    "%s: Non secure ctlr (Secure Dbg) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
5164 		    __func__, pdev->device, pdev->subsystem_vendor,
5165 		    pdev->subsystem_device);
5166 		retval = -1;
5167 	}
5168 
5169 	return retval;
5170 }
5171 
5172 /**
5173  * mpi3mr_probe - PCI probe callback
5174  * @pdev: PCI device instance
5175  * @id: PCI device ID details
5176  *
5177  * controller initialization routine. Checks the security status
5178  * of the controller and if it is invalid or tampered return the
5179  * probe without initializing the controller. Otherwise,
5180  * allocate per adapter instance through shost_priv and
5181  * initialize controller specific data structures, initializae
5182  * the controller hardware, add shost to the SCSI subsystem.
5183  *
5184  * Return: 0 on success, non-zero on failure.
5185  */
5186 
5187 static int
5188 mpi3mr_probe(struct pci_dev *pdev, const struct pci_device_id *id)
5189 {
5190 	struct mpi3mr_ioc *mrioc = NULL;
5191 	struct Scsi_Host *shost = NULL;
5192 	int retval = 0, i;
5193 
5194 	if (osintfc_mrioc_security_status(pdev)) {
5195 		warn_non_secure_ctlr = 1;
5196 		return 1; /* For Invalid and Tampered device */
5197 	}
5198 
5199 	shost = scsi_host_alloc(&mpi3mr_driver_template,
5200 	    sizeof(struct mpi3mr_ioc));
5201 	if (!shost) {
5202 		retval = -ENODEV;
5203 		goto shost_failed;
5204 	}
5205 
5206 	mrioc = shost_priv(shost);
5207 	retval = ida_alloc_range(&mrioc_ida, 1, U8_MAX, GFP_KERNEL);
5208 	if (retval < 0)
5209 		goto id_alloc_failed;
5210 	mrioc->id = (u8)retval;
5211 	sprintf(mrioc->driver_name, "%s", MPI3MR_DRIVER_NAME);
5212 	sprintf(mrioc->name, "%s%d", mrioc->driver_name, mrioc->id);
5213 	INIT_LIST_HEAD(&mrioc->list);
5214 	spin_lock(&mrioc_list_lock);
5215 	list_add_tail(&mrioc->list, &mrioc_list);
5216 	spin_unlock(&mrioc_list_lock);
5217 
5218 	spin_lock_init(&mrioc->admin_req_lock);
5219 	spin_lock_init(&mrioc->reply_free_queue_lock);
5220 	spin_lock_init(&mrioc->sbq_lock);
5221 	spin_lock_init(&mrioc->fwevt_lock);
5222 	spin_lock_init(&mrioc->tgtdev_lock);
5223 	spin_lock_init(&mrioc->watchdog_lock);
5224 	spin_lock_init(&mrioc->chain_buf_lock);
5225 	spin_lock_init(&mrioc->sas_node_lock);
5226 
5227 	INIT_LIST_HEAD(&mrioc->fwevt_list);
5228 	INIT_LIST_HEAD(&mrioc->tgtdev_list);
5229 	INIT_LIST_HEAD(&mrioc->delayed_rmhs_list);
5230 	INIT_LIST_HEAD(&mrioc->delayed_evtack_cmds_list);
5231 	INIT_LIST_HEAD(&mrioc->sas_expander_list);
5232 	INIT_LIST_HEAD(&mrioc->hba_port_table_list);
5233 	INIT_LIST_HEAD(&mrioc->enclosure_list);
5234 
5235 	mutex_init(&mrioc->reset_mutex);
5236 	mpi3mr_init_drv_cmd(&mrioc->init_cmds, MPI3MR_HOSTTAG_INITCMDS);
5237 	mpi3mr_init_drv_cmd(&mrioc->host_tm_cmds, MPI3MR_HOSTTAG_BLK_TMS);
5238 	mpi3mr_init_drv_cmd(&mrioc->bsg_cmds, MPI3MR_HOSTTAG_BSG_CMDS);
5239 	mpi3mr_init_drv_cmd(&mrioc->cfg_cmds, MPI3MR_HOSTTAG_CFG_CMDS);
5240 	mpi3mr_init_drv_cmd(&mrioc->transport_cmds,
5241 	    MPI3MR_HOSTTAG_TRANSPORT_CMDS);
5242 
5243 	for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++)
5244 		mpi3mr_init_drv_cmd(&mrioc->dev_rmhs_cmds[i],
5245 		    MPI3MR_HOSTTAG_DEVRMCMD_MIN + i);
5246 
5247 	for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++)
5248 		mpi3mr_init_drv_cmd(&mrioc->evtack_cmds[i],
5249 				    MPI3MR_HOSTTAG_EVTACKCMD_MIN + i);
5250 
5251 	if ((pdev->device == MPI3_MFGPAGE_DEVID_SAS4116) &&
5252 		!pdev->revision)
5253 		mrioc->enable_segqueue = false;
5254 	else
5255 		mrioc->enable_segqueue = true;
5256 
5257 	init_waitqueue_head(&mrioc->reset_waitq);
5258 	mrioc->logging_level = logging_level;
5259 	mrioc->shost = shost;
5260 	mrioc->pdev = pdev;
5261 	mrioc->stop_bsgs = 1;
5262 
5263 	mrioc->max_sgl_entries = max_sgl_entries;
5264 	if (max_sgl_entries > MPI3MR_MAX_SGL_ENTRIES)
5265 		mrioc->max_sgl_entries = MPI3MR_MAX_SGL_ENTRIES;
5266 	else if (max_sgl_entries < MPI3MR_DEFAULT_SGL_ENTRIES)
5267 		mrioc->max_sgl_entries = MPI3MR_DEFAULT_SGL_ENTRIES;
5268 	else {
5269 		mrioc->max_sgl_entries /= MPI3MR_DEFAULT_SGL_ENTRIES;
5270 		mrioc->max_sgl_entries *= MPI3MR_DEFAULT_SGL_ENTRIES;
5271 	}
5272 
5273 	/* init shost parameters */
5274 	shost->max_cmd_len = MPI3MR_MAX_CDB_LENGTH;
5275 	shost->max_lun = -1;
5276 	shost->unique_id = mrioc->id;
5277 
5278 	shost->max_channel = 0;
5279 	shost->max_id = 0xFFFFFFFF;
5280 
5281 	shost->host_tagset = 1;
5282 
5283 	if (prot_mask >= 0)
5284 		scsi_host_set_prot(shost, prot_mask);
5285 	else {
5286 		prot_mask = SHOST_DIF_TYPE1_PROTECTION
5287 		    | SHOST_DIF_TYPE2_PROTECTION
5288 		    | SHOST_DIF_TYPE3_PROTECTION;
5289 		scsi_host_set_prot(shost, prot_mask);
5290 	}
5291 
5292 	ioc_info(mrioc,
5293 	    "%s :host protection capabilities enabled %s%s%s%s%s%s%s\n",
5294 	    __func__,
5295 	    (prot_mask & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "",
5296 	    (prot_mask & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "",
5297 	    (prot_mask & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "",
5298 	    (prot_mask & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "",
5299 	    (prot_mask & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "",
5300 	    (prot_mask & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "",
5301 	    (prot_mask & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : "");
5302 
5303 	if (prot_guard_mask)
5304 		scsi_host_set_guard(shost, (prot_guard_mask & 3));
5305 	else
5306 		scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
5307 
5308 	snprintf(mrioc->fwevt_worker_name, sizeof(mrioc->fwevt_worker_name),
5309 	    "%s%d_fwevt_wrkr", mrioc->driver_name, mrioc->id);
5310 	mrioc->fwevt_worker_thread = alloc_ordered_workqueue(
5311 	    mrioc->fwevt_worker_name, 0);
5312 	if (!mrioc->fwevt_worker_thread) {
5313 		ioc_err(mrioc, "failure at %s:%d/%s()!\n",
5314 		    __FILE__, __LINE__, __func__);
5315 		retval = -ENODEV;
5316 		goto fwevtthread_failed;
5317 	}
5318 
5319 	mrioc->is_driver_loading = 1;
5320 	mrioc->cpu_count = num_online_cpus();
5321 	if (mpi3mr_setup_resources(mrioc)) {
5322 		ioc_err(mrioc, "setup resources failed\n");
5323 		retval = -ENODEV;
5324 		goto resource_alloc_failed;
5325 	}
5326 	if (mpi3mr_init_ioc(mrioc)) {
5327 		ioc_err(mrioc, "initializing IOC failed\n");
5328 		retval = -ENODEV;
5329 		goto init_ioc_failed;
5330 	}
5331 
5332 	shost->nr_hw_queues = mrioc->num_op_reply_q;
5333 	if (mrioc->active_poll_qcount)
5334 		shost->nr_maps = 3;
5335 
5336 	shost->can_queue = mrioc->max_host_ios;
5337 	shost->sg_tablesize = mrioc->max_sgl_entries;
5338 	shost->max_id = mrioc->facts.max_perids + 1;
5339 
5340 	retval = scsi_add_host(shost, &pdev->dev);
5341 	if (retval) {
5342 		ioc_err(mrioc, "failure at %s:%d/%s()!\n",
5343 		    __FILE__, __LINE__, __func__);
5344 		goto addhost_failed;
5345 	}
5346 
5347 	scsi_scan_host(shost);
5348 	mpi3mr_bsg_init(mrioc);
5349 	return retval;
5350 
5351 addhost_failed:
5352 	mpi3mr_stop_watchdog(mrioc);
5353 	mpi3mr_cleanup_ioc(mrioc);
5354 init_ioc_failed:
5355 	mpi3mr_free_mem(mrioc);
5356 	mpi3mr_cleanup_resources(mrioc);
5357 resource_alloc_failed:
5358 	destroy_workqueue(mrioc->fwevt_worker_thread);
5359 fwevtthread_failed:
5360 	ida_free(&mrioc_ida, mrioc->id);
5361 	spin_lock(&mrioc_list_lock);
5362 	list_del(&mrioc->list);
5363 	spin_unlock(&mrioc_list_lock);
5364 id_alloc_failed:
5365 	scsi_host_put(shost);
5366 shost_failed:
5367 	return retval;
5368 }
5369 
5370 /**
5371  * mpi3mr_remove - PCI remove callback
5372  * @pdev: PCI device instance
5373  *
5374  * Cleanup the IOC by issuing MUR and shutdown notification.
5375  * Free up all memory and resources associated with the
5376  * controllerand target devices, unregister the shost.
5377  *
5378  * Return: Nothing.
5379  */
5380 static void mpi3mr_remove(struct pci_dev *pdev)
5381 {
5382 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
5383 	struct mpi3mr_ioc *mrioc;
5384 	struct workqueue_struct	*wq;
5385 	unsigned long flags;
5386 	struct mpi3mr_tgt_dev *tgtdev, *tgtdev_next;
5387 	struct mpi3mr_hba_port *port, *hba_port_next;
5388 	struct mpi3mr_sas_node *sas_expander, *sas_expander_next;
5389 
5390 	if (!shost)
5391 		return;
5392 
5393 	mrioc = shost_priv(shost);
5394 	while (mrioc->reset_in_progress || mrioc->is_driver_loading)
5395 		ssleep(1);
5396 
5397 	if (mrioc->block_on_pci_err) {
5398 		mrioc->block_on_pci_err = false;
5399 		scsi_unblock_requests(shost);
5400 		mrioc->unrecoverable = 1;
5401 	}
5402 
5403 	if (!pci_device_is_present(mrioc->pdev) ||
5404 	    mrioc->pci_err_recovery) {
5405 		mrioc->unrecoverable = 1;
5406 		mpi3mr_flush_cmds_for_unrecovered_controller(mrioc);
5407 	}
5408 
5409 	mpi3mr_bsg_exit(mrioc);
5410 	mrioc->stop_drv_processing = 1;
5411 	mpi3mr_cleanup_fwevt_list(mrioc);
5412 	spin_lock_irqsave(&mrioc->fwevt_lock, flags);
5413 	wq = mrioc->fwevt_worker_thread;
5414 	mrioc->fwevt_worker_thread = NULL;
5415 	spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
5416 	if (wq)
5417 		destroy_workqueue(wq);
5418 
5419 	if (mrioc->sas_transport_enabled)
5420 		sas_remove_host(shost);
5421 	else
5422 		scsi_remove_host(shost);
5423 
5424 	list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
5425 	    list) {
5426 		mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
5427 		mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, true);
5428 		mpi3mr_tgtdev_put(tgtdev);
5429 	}
5430 	mpi3mr_stop_watchdog(mrioc);
5431 	mpi3mr_cleanup_ioc(mrioc);
5432 	mpi3mr_free_mem(mrioc);
5433 	mpi3mr_cleanup_resources(mrioc);
5434 
5435 	spin_lock_irqsave(&mrioc->sas_node_lock, flags);
5436 	list_for_each_entry_safe_reverse(sas_expander, sas_expander_next,
5437 	    &mrioc->sas_expander_list, list) {
5438 		spin_unlock_irqrestore(&mrioc->sas_node_lock, flags);
5439 		mpi3mr_expander_node_remove(mrioc, sas_expander);
5440 		spin_lock_irqsave(&mrioc->sas_node_lock, flags);
5441 	}
5442 	list_for_each_entry_safe(port, hba_port_next, &mrioc->hba_port_table_list, list) {
5443 		ioc_info(mrioc,
5444 		    "removing hba_port entry: %p port: %d from hba_port list\n",
5445 		    port, port->port_id);
5446 		list_del(&port->list);
5447 		kfree(port);
5448 	}
5449 	spin_unlock_irqrestore(&mrioc->sas_node_lock, flags);
5450 
5451 	if (mrioc->sas_hba.num_phys) {
5452 		kfree(mrioc->sas_hba.phy);
5453 		mrioc->sas_hba.phy = NULL;
5454 		mrioc->sas_hba.num_phys = 0;
5455 	}
5456 
5457 	ida_free(&mrioc_ida, mrioc->id);
5458 	spin_lock(&mrioc_list_lock);
5459 	list_del(&mrioc->list);
5460 	spin_unlock(&mrioc_list_lock);
5461 
5462 	scsi_host_put(shost);
5463 }
5464 
5465 /**
5466  * mpi3mr_shutdown - PCI shutdown callback
5467  * @pdev: PCI device instance
5468  *
5469  * Free up all memory and resources associated with the
5470  * controller
5471  *
5472  * Return: Nothing.
5473  */
5474 static void mpi3mr_shutdown(struct pci_dev *pdev)
5475 {
5476 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
5477 	struct mpi3mr_ioc *mrioc;
5478 	struct workqueue_struct	*wq;
5479 	unsigned long flags;
5480 
5481 	if (!shost)
5482 		return;
5483 
5484 	mrioc = shost_priv(shost);
5485 	while (mrioc->reset_in_progress || mrioc->is_driver_loading)
5486 		ssleep(1);
5487 
5488 	mrioc->stop_drv_processing = 1;
5489 	mpi3mr_cleanup_fwevt_list(mrioc);
5490 	spin_lock_irqsave(&mrioc->fwevt_lock, flags);
5491 	wq = mrioc->fwevt_worker_thread;
5492 	mrioc->fwevt_worker_thread = NULL;
5493 	spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
5494 	if (wq)
5495 		destroy_workqueue(wq);
5496 
5497 	mpi3mr_stop_watchdog(mrioc);
5498 	mpi3mr_cleanup_ioc(mrioc);
5499 	mpi3mr_cleanup_resources(mrioc);
5500 }
5501 
5502 /**
5503  * mpi3mr_suspend - PCI power management suspend callback
5504  * @dev: Device struct
5505  *
5506  * Change the power state to the given value and cleanup the IOC
5507  * by issuing MUR and shutdown notification
5508  *
5509  * Return: 0 always.
5510  */
5511 static int __maybe_unused
5512 mpi3mr_suspend(struct device *dev)
5513 {
5514 	struct pci_dev *pdev = to_pci_dev(dev);
5515 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
5516 	struct mpi3mr_ioc *mrioc;
5517 
5518 	if (!shost)
5519 		return 0;
5520 
5521 	mrioc = shost_priv(shost);
5522 	while (mrioc->reset_in_progress || mrioc->is_driver_loading)
5523 		ssleep(1);
5524 	mrioc->stop_drv_processing = 1;
5525 	mpi3mr_cleanup_fwevt_list(mrioc);
5526 	scsi_block_requests(shost);
5527 	mpi3mr_stop_watchdog(mrioc);
5528 	mpi3mr_cleanup_ioc(mrioc);
5529 
5530 	ioc_info(mrioc, "pdev=0x%p, slot=%s, entering operating state\n",
5531 	    pdev, pci_name(pdev));
5532 	mpi3mr_cleanup_resources(mrioc);
5533 
5534 	return 0;
5535 }
5536 
5537 /**
5538  * mpi3mr_resume - PCI power management resume callback
5539  * @dev: Device struct
5540  *
5541  * Restore the power state to D0 and reinitialize the controller
5542  * and resume I/O operations to the target devices
5543  *
5544  * Return: 0 on success, non-zero on failure
5545  */
5546 static int __maybe_unused
5547 mpi3mr_resume(struct device *dev)
5548 {
5549 	struct pci_dev *pdev = to_pci_dev(dev);
5550 	struct Scsi_Host *shost = pci_get_drvdata(pdev);
5551 	struct mpi3mr_ioc *mrioc;
5552 	pci_power_t device_state = pdev->current_state;
5553 	int r;
5554 
5555 	if (!shost)
5556 		return 0;
5557 
5558 	mrioc = shost_priv(shost);
5559 
5560 	ioc_info(mrioc, "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
5561 	    pdev, pci_name(pdev), device_state);
5562 	mrioc->pdev = pdev;
5563 	mrioc->cpu_count = num_online_cpus();
5564 	r = mpi3mr_setup_resources(mrioc);
5565 	if (r) {
5566 		ioc_info(mrioc, "%s: Setup resources failed[%d]\n",
5567 		    __func__, r);
5568 		return r;
5569 	}
5570 
5571 	mrioc->stop_drv_processing = 0;
5572 	mpi3mr_invalidate_devhandles(mrioc);
5573 	mpi3mr_free_enclosure_list(mrioc);
5574 	mpi3mr_memset_buffers(mrioc);
5575 	r = mpi3mr_reinit_ioc(mrioc, 1);
5576 	if (r) {
5577 		ioc_err(mrioc, "resuming controller failed[%d]\n", r);
5578 		return r;
5579 	}
5580 	ssleep(MPI3MR_RESET_TOPOLOGY_SETTLE_TIME);
5581 	scsi_unblock_requests(shost);
5582 	mrioc->device_refresh_on = 0;
5583 	mpi3mr_start_watchdog(mrioc);
5584 
5585 	return 0;
5586 }
5587 
5588 /**
5589  * mpi3mr_pcierr_error_detected - PCI error detected callback
5590  * @pdev: PCI device instance
5591  * @state: channel state
5592  *
5593  * This function is called by the PCI error recovery driver and
5594  * based on the state passed the driver decides what actions to
5595  * be recommended back to PCI driver.
5596  *
5597  * For all of the states if there is no valid mrioc or scsi host
5598  * references in the PCI device then this function will return
5599  * the result as disconnect.
5600  *
5601  * For normal state, this function will return the result as can
5602  * recover.
5603  *
5604  * For frozen state, this function will block for any pending
5605  * controller initialization or re-initialization to complete,
5606  * stop any new interactions with the controller and return
5607  * status as reset required.
5608  *
5609  * For permanent failure state, this function will mark the
5610  * controller as unrecoverable and return status as disconnect.
5611  *
5612  * Returns: PCI_ERS_RESULT_NEED_RESET or CAN_RECOVER or
5613  * DISCONNECT based on the controller state.
5614  */
5615 static pci_ers_result_t
5616 mpi3mr_pcierr_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
5617 {
5618 	struct Scsi_Host *shost;
5619 	struct mpi3mr_ioc *mrioc;
5620 	unsigned int timeout = MPI3MR_RESET_TIMEOUT;
5621 
5622 	dev_info(&pdev->dev, "%s: callback invoked state(%d)\n", __func__,
5623 	    state);
5624 
5625 	shost = pci_get_drvdata(pdev);
5626 	mrioc = shost_priv(shost);
5627 
5628 	switch (state) {
5629 	case pci_channel_io_normal:
5630 		return PCI_ERS_RESULT_CAN_RECOVER;
5631 	case pci_channel_io_frozen:
5632 		mrioc->pci_err_recovery = true;
5633 		mrioc->block_on_pci_err = true;
5634 		do {
5635 			if (mrioc->reset_in_progress || mrioc->is_driver_loading)
5636 				ssleep(1);
5637 			else
5638 				break;
5639 		} while (--timeout);
5640 
5641 		if (!timeout) {
5642 			mrioc->pci_err_recovery = true;
5643 			mrioc->block_on_pci_err = true;
5644 			mrioc->unrecoverable = 1;
5645 			mpi3mr_stop_watchdog(mrioc);
5646 			mpi3mr_flush_cmds_for_unrecovered_controller(mrioc);
5647 			return PCI_ERS_RESULT_DISCONNECT;
5648 		}
5649 
5650 		scsi_block_requests(mrioc->shost);
5651 		mpi3mr_stop_watchdog(mrioc);
5652 		mpi3mr_cleanup_resources(mrioc);
5653 		return PCI_ERS_RESULT_NEED_RESET;
5654 	case pci_channel_io_perm_failure:
5655 		mrioc->pci_err_recovery = true;
5656 		mrioc->block_on_pci_err = true;
5657 		mrioc->unrecoverable = 1;
5658 		mpi3mr_stop_watchdog(mrioc);
5659 		mpi3mr_flush_cmds_for_unrecovered_controller(mrioc);
5660 		return PCI_ERS_RESULT_DISCONNECT;
5661 	default:
5662 		return PCI_ERS_RESULT_DISCONNECT;
5663 	}
5664 }
5665 
5666 /**
5667  * mpi3mr_pcierr_slot_reset - Post slot reset callback
5668  * @pdev: PCI device instance
5669  *
5670  * This function is called by the PCI error recovery driver
5671  * after a slot or link reset issued by it for the recovery, the
5672  * driver is expected to bring back the controller and
5673  * initialize it.
5674  *
5675  * This function restores PCI state and reinitializes controller
5676  * resources and the controller, this blocks for any pending
5677  * reset to complete.
5678  *
5679  * Returns: PCI_ERS_RESULT_DISCONNECT on failure or
5680  * PCI_ERS_RESULT_RECOVERED
5681  */
5682 static pci_ers_result_t mpi3mr_pcierr_slot_reset(struct pci_dev *pdev)
5683 {
5684 	struct Scsi_Host *shost;
5685 	struct mpi3mr_ioc *mrioc;
5686 	unsigned int timeout = MPI3MR_RESET_TIMEOUT;
5687 
5688 	dev_info(&pdev->dev, "%s: callback invoked\n", __func__);
5689 
5690 	shost = pci_get_drvdata(pdev);
5691 	mrioc = shost_priv(shost);
5692 
5693 	do {
5694 		if (mrioc->reset_in_progress)
5695 			ssleep(1);
5696 		else
5697 			break;
5698 	} while (--timeout);
5699 
5700 	if (!timeout)
5701 		goto out_failed;
5702 
5703 	pci_restore_state(pdev);
5704 
5705 	if (mpi3mr_setup_resources(mrioc)) {
5706 		ioc_err(mrioc, "setup resources failed\n");
5707 		goto out_failed;
5708 	}
5709 	mrioc->unrecoverable = 0;
5710 	mrioc->pci_err_recovery = false;
5711 
5712 	if (mpi3mr_soft_reset_handler(mrioc, MPI3MR_RESET_FROM_FIRMWARE, 0))
5713 		goto out_failed;
5714 
5715 	return PCI_ERS_RESULT_RECOVERED;
5716 
5717 out_failed:
5718 	mrioc->unrecoverable = 1;
5719 	mrioc->block_on_pci_err = false;
5720 	scsi_unblock_requests(shost);
5721 	mpi3mr_start_watchdog(mrioc);
5722 	return PCI_ERS_RESULT_DISCONNECT;
5723 }
5724 
5725 /**
5726  * mpi3mr_pcierr_resume - PCI error recovery resume
5727  * callback
5728  * @pdev: PCI device instance
5729  *
5730  * This function enables all I/O and IOCTLs post reset issued as
5731  * part of the PCI error recovery
5732  *
5733  * Return: Nothing.
5734  */
5735 static void mpi3mr_pcierr_resume(struct pci_dev *pdev)
5736 {
5737 	struct Scsi_Host *shost;
5738 	struct mpi3mr_ioc *mrioc;
5739 
5740 	dev_info(&pdev->dev, "%s: callback invoked\n", __func__);
5741 
5742 	shost = pci_get_drvdata(pdev);
5743 	mrioc = shost_priv(shost);
5744 
5745 	if (mrioc->block_on_pci_err) {
5746 		mrioc->block_on_pci_err = false;
5747 		scsi_unblock_requests(shost);
5748 		mpi3mr_start_watchdog(mrioc);
5749 	}
5750 }
5751 
5752 /**
5753  * mpi3mr_pcierr_mmio_enabled - PCI error recovery callback
5754  * @pdev: PCI device instance
5755  *
5756  * This is called only if mpi3mr_pcierr_error_detected returns
5757  * PCI_ERS_RESULT_CAN_RECOVER.
5758  *
5759  * Return: PCI_ERS_RESULT_DISCONNECT when the controller is
5760  * unrecoverable or when the shost/mrioc reference cannot be
5761  * found, else return PCI_ERS_RESULT_RECOVERED
5762  */
5763 static pci_ers_result_t mpi3mr_pcierr_mmio_enabled(struct pci_dev *pdev)
5764 {
5765 	struct Scsi_Host *shost;
5766 	struct mpi3mr_ioc *mrioc;
5767 
5768 	dev_info(&pdev->dev, "%s: callback invoked\n", __func__);
5769 
5770 	shost = pci_get_drvdata(pdev);
5771 	mrioc = shost_priv(shost);
5772 
5773 	if (mrioc->unrecoverable)
5774 		return PCI_ERS_RESULT_DISCONNECT;
5775 
5776 	return PCI_ERS_RESULT_RECOVERED;
5777 }
5778 
5779 static const struct pci_device_id mpi3mr_pci_id_table[] = {
5780 	{
5781 		PCI_DEVICE_SUB(MPI3_MFGPAGE_VENDORID_BROADCOM,
5782 		    MPI3_MFGPAGE_DEVID_SAS4116, PCI_ANY_ID, PCI_ANY_ID)
5783 	},
5784 	{
5785 		PCI_DEVICE_SUB(MPI3_MFGPAGE_VENDORID_BROADCOM,
5786 		    MPI3_MFGPAGE_DEVID_SAS5116_MPI, PCI_ANY_ID, PCI_ANY_ID)
5787 	},
5788 	{
5789 		PCI_DEVICE_SUB(MPI3_MFGPAGE_VENDORID_BROADCOM,
5790 		    MPI3_MFGPAGE_DEVID_SAS5116_MPI_MGMT, PCI_ANY_ID, PCI_ANY_ID)
5791 	},
5792 	{ 0 }
5793 };
5794 MODULE_DEVICE_TABLE(pci, mpi3mr_pci_id_table);
5795 
5796 static struct pci_error_handlers mpi3mr_err_handler = {
5797 	.error_detected = mpi3mr_pcierr_error_detected,
5798 	.mmio_enabled = mpi3mr_pcierr_mmio_enabled,
5799 	.slot_reset = mpi3mr_pcierr_slot_reset,
5800 	.resume = mpi3mr_pcierr_resume,
5801 };
5802 
5803 static SIMPLE_DEV_PM_OPS(mpi3mr_pm_ops, mpi3mr_suspend, mpi3mr_resume);
5804 
5805 static struct pci_driver mpi3mr_pci_driver = {
5806 	.name = MPI3MR_DRIVER_NAME,
5807 	.id_table = mpi3mr_pci_id_table,
5808 	.probe = mpi3mr_probe,
5809 	.remove = mpi3mr_remove,
5810 	.shutdown = mpi3mr_shutdown,
5811 	.err_handler = &mpi3mr_err_handler,
5812 	.driver.pm = &mpi3mr_pm_ops,
5813 };
5814 
5815 static ssize_t event_counter_show(struct device_driver *dd, char *buf)
5816 {
5817 	return sprintf(buf, "%llu\n", atomic64_read(&event_counter));
5818 }
5819 static DRIVER_ATTR_RO(event_counter);
5820 
5821 static int __init mpi3mr_init(void)
5822 {
5823 	int ret_val;
5824 
5825 	pr_info("Loading %s version %s\n", MPI3MR_DRIVER_NAME,
5826 	    MPI3MR_DRIVER_VERSION);
5827 
5828 	mpi3mr_transport_template =
5829 	    sas_attach_transport(&mpi3mr_transport_functions);
5830 	if (!mpi3mr_transport_template) {
5831 		pr_err("%s failed to load due to sas transport attach failure\n",
5832 		    MPI3MR_DRIVER_NAME);
5833 		return -ENODEV;
5834 	}
5835 
5836 	ret_val = pci_register_driver(&mpi3mr_pci_driver);
5837 	if (ret_val) {
5838 		pr_err("%s failed to load due to pci register driver failure\n",
5839 		    MPI3MR_DRIVER_NAME);
5840 		goto err_pci_reg_fail;
5841 	}
5842 
5843 	ret_val = driver_create_file(&mpi3mr_pci_driver.driver,
5844 				     &driver_attr_event_counter);
5845 	if (ret_val)
5846 		goto err_event_counter;
5847 
5848 	return ret_val;
5849 
5850 err_event_counter:
5851 	pci_unregister_driver(&mpi3mr_pci_driver);
5852 
5853 err_pci_reg_fail:
5854 	sas_release_transport(mpi3mr_transport_template);
5855 	return ret_val;
5856 }
5857 
5858 static void __exit mpi3mr_exit(void)
5859 {
5860 	if (warn_non_secure_ctlr)
5861 		pr_warn(
5862 		    "Unloading %s version %s while managing a non secure controller\n",
5863 		    MPI3MR_DRIVER_NAME, MPI3MR_DRIVER_VERSION);
5864 	else
5865 		pr_info("Unloading %s version %s\n", MPI3MR_DRIVER_NAME,
5866 		    MPI3MR_DRIVER_VERSION);
5867 
5868 	driver_remove_file(&mpi3mr_pci_driver.driver,
5869 			   &driver_attr_event_counter);
5870 	pci_unregister_driver(&mpi3mr_pci_driver);
5871 	sas_release_transport(mpi3mr_transport_template);
5872 	ida_destroy(&mrioc_ida);
5873 }
5874 
5875 module_init(mpi3mr_init);
5876 module_exit(mpi3mr_exit);
5877