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