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