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