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