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