1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Broadcom MPI3 Storage Controllers
5 * Copyright (C) 2017-2021 Broadcom Inc.
6 * (mailto: mpi3mr-linuxdrv.pdl@broadcom.com)
12 /* global driver scop variables */
13 LIST_HEAD(mrioc_list);
14 DEFINE_SPINLOCK(mrioc_list_lock);
16 static int warn_non_secure_ctlr;
18 MODULE_AUTHOR(MPI3MR_DRIVER_AUTHOR);
19 MODULE_DESCRIPTION(MPI3MR_DRIVER_DESC);
20 MODULE_LICENSE(MPI3MR_DRIVER_LICENSE);
21 MODULE_VERSION(MPI3MR_DRIVER_VERSION);
23 /* Module parameters*/
25 module_param(prot_mask, int, 0);
26 MODULE_PARM_DESC(prot_mask, "Host protection capabilities mask, def=0x07");
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)");
36 /* Forward declarations*/
37 static void mpi3mr_send_event_ack(struct mpi3mr_ioc *mrioc, u8 event,
38 struct mpi3mr_drv_cmd *cmdparam, u32 event_ctx);
41 * mpi3mr_host_tag_for_scmd - Get host tag for a scmd
42 * @mrioc: Adapter instance reference
43 * @scmd: SCSI command reference
45 * Calculate the host tag based on block tag for a given scmd.
47 * Return: Valid host tag or MPI3MR_HOSTTAG_INVALID.
49 static u16 mpi3mr_host_tag_for_scmd(struct mpi3mr_ioc *mrioc,
50 struct scsi_cmnd *scmd)
52 struct scmd_priv *priv = NULL;
54 u16 host_tag, hw_queue;
56 unique_tag = blk_mq_unique_tag(scsi_cmd_to_rq(scmd));
58 hw_queue = blk_mq_unique_tag_to_hwq(unique_tag);
59 if (hw_queue >= mrioc->num_op_reply_q)
60 return MPI3MR_HOSTTAG_INVALID;
61 host_tag = blk_mq_unique_tag_to_tag(unique_tag);
63 if (WARN_ON(host_tag >= mrioc->max_host_ios))
64 return MPI3MR_HOSTTAG_INVALID;
66 priv = scsi_cmd_priv(scmd);
67 /*host_tag 0 is invalid hence incrementing by 1*/
68 priv->host_tag = host_tag + 1;
70 priv->in_lld_scope = 1;
71 priv->req_q_idx = hw_queue;
72 priv->meta_chain_idx = -1;
74 priv->meta_sg_valid = 0;
75 return priv->host_tag;
79 * mpi3mr_scmd_from_host_tag - Get SCSI command from host tag
80 * @mrioc: Adapter instance reference
82 * @qidx: Operational queue index
84 * Identify the block tag from the host tag and queue index and
85 * retrieve associated scsi command using scsi_host_find_tag().
87 * Return: SCSI command reference or NULL.
89 static struct scsi_cmnd *mpi3mr_scmd_from_host_tag(
90 struct mpi3mr_ioc *mrioc, u16 host_tag, u16 qidx)
92 struct scsi_cmnd *scmd = NULL;
93 struct scmd_priv *priv = NULL;
94 u32 unique_tag = host_tag - 1;
96 if (WARN_ON(host_tag > mrioc->max_host_ios))
99 unique_tag |= (qidx << BLK_MQ_UNIQUE_TAG_BITS);
101 scmd = scsi_host_find_tag(mrioc->shost, unique_tag);
103 priv = scsi_cmd_priv(scmd);
104 if (!priv->in_lld_scope)
112 * mpi3mr_clear_scmd_priv - Cleanup SCSI command private date
113 * @mrioc: Adapter instance reference
114 * @scmd: SCSI command reference
116 * Invalidate the SCSI command private data to mark the command
117 * is not in LLD scope anymore.
121 static void mpi3mr_clear_scmd_priv(struct mpi3mr_ioc *mrioc,
122 struct scsi_cmnd *scmd)
124 struct scmd_priv *priv = NULL;
126 priv = scsi_cmd_priv(scmd);
128 if (WARN_ON(priv->in_lld_scope == 0))
130 priv->host_tag = MPI3MR_HOSTTAG_INVALID;
131 priv->req_q_idx = 0xFFFF;
133 priv->in_lld_scope = 0;
134 priv->meta_sg_valid = 0;
135 if (priv->chain_idx >= 0) {
136 clear_bit(priv->chain_idx, mrioc->chain_bitmap);
137 priv->chain_idx = -1;
139 if (priv->meta_chain_idx >= 0) {
140 clear_bit(priv->meta_chain_idx, mrioc->chain_bitmap);
141 priv->meta_chain_idx = -1;
145 static void mpi3mr_dev_rmhs_send_tm(struct mpi3mr_ioc *mrioc, u16 handle,
146 struct mpi3mr_drv_cmd *cmdparam, u8 iou_rc);
147 static void mpi3mr_fwevt_worker(struct work_struct *work);
150 * mpi3mr_fwevt_free - firmware event memory dealloctor
151 * @r: k reference pointer of the firmware event
153 * Free firmware event memory when no reference.
155 static void mpi3mr_fwevt_free(struct kref *r)
157 kfree(container_of(r, struct mpi3mr_fwevt, ref_count));
161 * mpi3mr_fwevt_get - k reference incrementor
162 * @fwevt: Firmware event reference
164 * Increment firmware event reference count.
166 static void mpi3mr_fwevt_get(struct mpi3mr_fwevt *fwevt)
168 kref_get(&fwevt->ref_count);
172 * mpi3mr_fwevt_put - k reference decrementor
173 * @fwevt: Firmware event reference
175 * decrement firmware event reference count.
177 static void mpi3mr_fwevt_put(struct mpi3mr_fwevt *fwevt)
179 kref_put(&fwevt->ref_count, mpi3mr_fwevt_free);
183 * mpi3mr_alloc_fwevt - Allocate firmware event
184 * @len: length of firmware event data to allocate
186 * Allocate firmware event with required length and initialize
187 * the reference counter.
189 * Return: firmware event reference.
191 static struct mpi3mr_fwevt *mpi3mr_alloc_fwevt(int len)
193 struct mpi3mr_fwevt *fwevt;
195 fwevt = kzalloc(sizeof(*fwevt) + len, GFP_ATOMIC);
199 kref_init(&fwevt->ref_count);
204 * mpi3mr_fwevt_add_to_list - Add firmware event to the list
205 * @mrioc: Adapter instance reference
206 * @fwevt: Firmware event reference
208 * Add the given firmware event to the firmware event list.
212 static void mpi3mr_fwevt_add_to_list(struct mpi3mr_ioc *mrioc,
213 struct mpi3mr_fwevt *fwevt)
217 if (!mrioc->fwevt_worker_thread)
220 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
221 /* get fwevt reference count while adding it to fwevt_list */
222 mpi3mr_fwevt_get(fwevt);
223 INIT_LIST_HEAD(&fwevt->list);
224 list_add_tail(&fwevt->list, &mrioc->fwevt_list);
225 INIT_WORK(&fwevt->work, mpi3mr_fwevt_worker);
226 /* get fwevt reference count while enqueueing it to worker queue */
227 mpi3mr_fwevt_get(fwevt);
228 queue_work(mrioc->fwevt_worker_thread, &fwevt->work);
229 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
233 * mpi3mr_fwevt_del_from_list - Delete firmware event from list
234 * @mrioc: Adapter instance reference
235 * @fwevt: Firmware event reference
237 * Delete the given firmware event from the firmware event list.
241 static void mpi3mr_fwevt_del_from_list(struct mpi3mr_ioc *mrioc,
242 struct mpi3mr_fwevt *fwevt)
246 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
247 if (!list_empty(&fwevt->list)) {
248 list_del_init(&fwevt->list);
250 * Put fwevt reference count after
251 * removing it from fwevt_list
253 mpi3mr_fwevt_put(fwevt);
255 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
259 * mpi3mr_dequeue_fwevt - Dequeue firmware event from the list
260 * @mrioc: Adapter instance reference
262 * Dequeue a firmware event from the firmware event list.
264 * Return: firmware event.
266 static struct mpi3mr_fwevt *mpi3mr_dequeue_fwevt(
267 struct mpi3mr_ioc *mrioc)
270 struct mpi3mr_fwevt *fwevt = NULL;
272 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
273 if (!list_empty(&mrioc->fwevt_list)) {
274 fwevt = list_first_entry(&mrioc->fwevt_list,
275 struct mpi3mr_fwevt, list);
276 list_del_init(&fwevt->list);
278 * Put fwevt reference count after
279 * removing it from fwevt_list
281 mpi3mr_fwevt_put(fwevt);
283 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
289 * mpi3mr_cleanup_fwevt_list - Cleanup firmware event list
290 * @mrioc: Adapter instance reference
292 * Flush all pending firmware events from the firmware event
297 void mpi3mr_cleanup_fwevt_list(struct mpi3mr_ioc *mrioc)
299 struct mpi3mr_fwevt *fwevt = NULL;
301 if ((list_empty(&mrioc->fwevt_list) && !mrioc->current_event) ||
302 !mrioc->fwevt_worker_thread)
305 while ((fwevt = mpi3mr_dequeue_fwevt(mrioc)) ||
306 (fwevt = mrioc->current_event)) {
308 * Wait on the fwevt to complete. If this returns 1, then
309 * the event was never executed, and we need a put for the
310 * reference the work had on the fwevt.
312 * If it did execute, we wait for it to finish, and the put will
313 * happen from mpi3mr_process_fwevt()
315 if (cancel_work_sync(&fwevt->work)) {
317 * Put fwevt reference count after
318 * dequeuing it from worker queue
320 mpi3mr_fwevt_put(fwevt);
322 * Put fwevt reference count to neutralize
323 * kref_init increment
325 mpi3mr_fwevt_put(fwevt);
331 * mpi3mr_invalidate_devhandles -Invalidate device handles
332 * @mrioc: Adapter instance reference
334 * Invalidate the device handles in the target device structures
335 * . Called post reset prior to reinitializing the controller.
339 void mpi3mr_invalidate_devhandles(struct mpi3mr_ioc *mrioc)
341 struct mpi3mr_tgt_dev *tgtdev;
342 struct mpi3mr_stgt_priv_data *tgt_priv;
344 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
345 tgtdev->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
346 if (tgtdev->starget && tgtdev->starget->hostdata) {
347 tgt_priv = tgtdev->starget->hostdata;
348 tgt_priv->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
354 * mpi3mr_print_scmd - print individual SCSI command
356 * @data: Adapter instance reference
357 * @reserved: N/A. Currently not used
359 * Print the SCSI command details if it is in LLD scope.
361 * Return: true always.
363 static bool mpi3mr_print_scmd(struct request *rq,
364 void *data, bool reserved)
366 struct mpi3mr_ioc *mrioc = (struct mpi3mr_ioc *)data;
367 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
368 struct scmd_priv *priv = NULL;
371 priv = scsi_cmd_priv(scmd);
372 if (!priv->in_lld_scope)
375 ioc_info(mrioc, "%s :Host Tag = %d, qid = %d\n",
376 __func__, priv->host_tag, priv->req_q_idx + 1);
377 scsi_print_command(scmd);
385 * mpi3mr_flush_scmd - Flush individual SCSI command
387 * @data: Adapter instance reference
388 * @reserved: N/A. Currently not used
390 * Return the SCSI command to the upper layers if it is in LLD
393 * Return: true always.
396 static bool mpi3mr_flush_scmd(struct request *rq,
397 void *data, bool reserved)
399 struct mpi3mr_ioc *mrioc = (struct mpi3mr_ioc *)data;
400 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
401 struct scmd_priv *priv = NULL;
404 priv = scsi_cmd_priv(scmd);
405 if (!priv->in_lld_scope)
408 if (priv->meta_sg_valid)
409 dma_unmap_sg(&mrioc->pdev->dev, scsi_prot_sglist(scmd),
410 scsi_prot_sg_count(scmd), scmd->sc_data_direction);
411 mpi3mr_clear_scmd_priv(mrioc, scmd);
412 scsi_dma_unmap(scmd);
413 scmd->result = DID_RESET << 16;
414 scsi_print_command(scmd);
416 mrioc->flush_io_count++;
424 * mpi3mr_flush_host_io - Flush host I/Os
425 * @mrioc: Adapter instance reference
427 * Flush all of the pending I/Os by calling
428 * blk_mq_tagset_busy_iter() for each possible tag. This is
429 * executed post controller reset
433 void mpi3mr_flush_host_io(struct mpi3mr_ioc *mrioc)
435 struct Scsi_Host *shost = mrioc->shost;
437 mrioc->flush_io_count = 0;
438 ioc_info(mrioc, "%s :Flushing Host I/O cmds post reset\n", __func__);
439 blk_mq_tagset_busy_iter(&shost->tag_set,
440 mpi3mr_flush_scmd, (void *)mrioc);
441 ioc_info(mrioc, "%s :Flushed %d Host I/O cmds\n", __func__,
442 mrioc->flush_io_count);
446 * mpi3mr_alloc_tgtdev - target device allocator
448 * Allocate target device instance and initialize the reference
451 * Return: target device instance.
453 static struct mpi3mr_tgt_dev *mpi3mr_alloc_tgtdev(void)
455 struct mpi3mr_tgt_dev *tgtdev;
457 tgtdev = kzalloc(sizeof(*tgtdev), GFP_ATOMIC);
460 kref_init(&tgtdev->ref_count);
465 * mpi3mr_tgtdev_add_to_list -Add tgtdevice to the list
466 * @mrioc: Adapter instance reference
467 * @tgtdev: Target device
469 * Add the target device to the target device list
473 static void mpi3mr_tgtdev_add_to_list(struct mpi3mr_ioc *mrioc,
474 struct mpi3mr_tgt_dev *tgtdev)
478 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
479 mpi3mr_tgtdev_get(tgtdev);
480 INIT_LIST_HEAD(&tgtdev->list);
481 list_add_tail(&tgtdev->list, &mrioc->tgtdev_list);
482 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
486 * mpi3mr_tgtdev_del_from_list -Delete tgtdevice from the list
487 * @mrioc: Adapter instance reference
488 * @tgtdev: Target device
490 * Remove the target device from the target device list
494 static void mpi3mr_tgtdev_del_from_list(struct mpi3mr_ioc *mrioc,
495 struct mpi3mr_tgt_dev *tgtdev)
499 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
500 if (!list_empty(&tgtdev->list)) {
501 list_del_init(&tgtdev->list);
502 mpi3mr_tgtdev_put(tgtdev);
504 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
508 * __mpi3mr_get_tgtdev_by_handle -Get tgtdev from device handle
509 * @mrioc: Adapter instance reference
510 * @handle: Device handle
512 * Accessor to retrieve target device from the device handle.
515 * Return: Target device reference.
517 static struct mpi3mr_tgt_dev *__mpi3mr_get_tgtdev_by_handle(
518 struct mpi3mr_ioc *mrioc, u16 handle)
520 struct mpi3mr_tgt_dev *tgtdev;
522 assert_spin_locked(&mrioc->tgtdev_lock);
523 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list)
524 if (tgtdev->dev_handle == handle)
529 mpi3mr_tgtdev_get(tgtdev);
534 * mpi3mr_get_tgtdev_by_handle -Get tgtdev from device handle
535 * @mrioc: Adapter instance reference
536 * @handle: Device handle
538 * Accessor to retrieve target device from the device handle.
541 * Return: Target device reference.
543 static struct mpi3mr_tgt_dev *mpi3mr_get_tgtdev_by_handle(
544 struct mpi3mr_ioc *mrioc, u16 handle)
546 struct mpi3mr_tgt_dev *tgtdev;
549 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
550 tgtdev = __mpi3mr_get_tgtdev_by_handle(mrioc, handle);
551 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
556 * __mpi3mr_get_tgtdev_by_perst_id -Get tgtdev from persist ID
557 * @mrioc: Adapter instance reference
558 * @persist_id: Persistent ID
560 * Accessor to retrieve target device from the Persistent ID.
563 * Return: Target device reference.
565 static struct mpi3mr_tgt_dev *__mpi3mr_get_tgtdev_by_perst_id(
566 struct mpi3mr_ioc *mrioc, u16 persist_id)
568 struct mpi3mr_tgt_dev *tgtdev;
570 assert_spin_locked(&mrioc->tgtdev_lock);
571 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list)
572 if (tgtdev->perst_id == persist_id)
577 mpi3mr_tgtdev_get(tgtdev);
582 * mpi3mr_get_tgtdev_by_perst_id -Get tgtdev from persistent ID
583 * @mrioc: Adapter instance reference
584 * @persist_id: Persistent ID
586 * Accessor to retrieve target device from the Persistent ID.
589 * Return: Target device reference.
591 static struct mpi3mr_tgt_dev *mpi3mr_get_tgtdev_by_perst_id(
592 struct mpi3mr_ioc *mrioc, u16 persist_id)
594 struct mpi3mr_tgt_dev *tgtdev;
597 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
598 tgtdev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, persist_id);
599 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
604 * __mpi3mr_get_tgtdev_from_tgtpriv -Get tgtdev from tgt private
605 * @mrioc: Adapter instance reference
606 * @tgt_priv: Target private data
608 * Accessor to return target device from the target private
609 * data. Non Lock version
611 * Return: Target device reference.
613 static struct mpi3mr_tgt_dev *__mpi3mr_get_tgtdev_from_tgtpriv(
614 struct mpi3mr_ioc *mrioc, struct mpi3mr_stgt_priv_data *tgt_priv)
616 struct mpi3mr_tgt_dev *tgtdev;
618 assert_spin_locked(&mrioc->tgtdev_lock);
619 tgtdev = tgt_priv->tgt_dev;
621 mpi3mr_tgtdev_get(tgtdev);
626 * mpi3mr_remove_tgtdev_from_host - Remove dev from upper layers
627 * @mrioc: Adapter instance reference
628 * @tgtdev: Target device structure
630 * Checks whether the device is exposed to upper layers and if it
631 * is then remove the device from upper layers by calling
632 * scsi_remove_target().
634 * Return: 0 on success, non zero on failure.
636 static void mpi3mr_remove_tgtdev_from_host(struct mpi3mr_ioc *mrioc,
637 struct mpi3mr_tgt_dev *tgtdev)
639 struct mpi3mr_stgt_priv_data *tgt_priv;
641 ioc_info(mrioc, "%s :Removing handle(0x%04x), wwid(0x%016llx)\n",
642 __func__, tgtdev->dev_handle, (unsigned long long)tgtdev->wwid);
643 if (tgtdev->starget && tgtdev->starget->hostdata) {
644 tgt_priv = tgtdev->starget->hostdata;
645 tgt_priv->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
648 if (tgtdev->starget) {
649 scsi_remove_target(&tgtdev->starget->dev);
650 tgtdev->host_exposed = 0;
652 ioc_info(mrioc, "%s :Removed handle(0x%04x), wwid(0x%016llx)\n",
653 __func__, tgtdev->dev_handle, (unsigned long long)tgtdev->wwid);
657 * mpi3mr_report_tgtdev_to_host - Expose device to upper layers
658 * @mrioc: Adapter instance reference
659 * @perst_id: Persistent ID of the device
661 * Checks whether the device can be exposed to upper layers and
662 * if it is not then expose the device to upper layers by
663 * calling scsi_scan_target().
665 * Return: 0 on success, non zero on failure.
667 static int mpi3mr_report_tgtdev_to_host(struct mpi3mr_ioc *mrioc,
671 struct mpi3mr_tgt_dev *tgtdev;
673 tgtdev = mpi3mr_get_tgtdev_by_perst_id(mrioc, perst_id);
678 if (tgtdev->is_hidden) {
682 if (!tgtdev->host_exposed && !mrioc->reset_in_progress) {
683 tgtdev->host_exposed = 1;
684 scsi_scan_target(&mrioc->shost->shost_gendev, 0,
686 SCAN_WILD_CARD, SCSI_SCAN_INITIAL);
687 if (!tgtdev->starget)
688 tgtdev->host_exposed = 0;
692 mpi3mr_tgtdev_put(tgtdev);
698 * mpi3mr_change_queue_depth- Change QD callback handler
699 * @sdev: SCSI device reference
700 * @q_depth: Queue depth
702 * Validate and limit QD and call scsi_change_queue_depth.
704 * Return: return value of scsi_change_queue_depth
706 static int mpi3mr_change_queue_depth(struct scsi_device *sdev,
709 struct scsi_target *starget = scsi_target(sdev);
710 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
713 if (!sdev->tagged_supported)
715 if (q_depth > shost->can_queue)
716 q_depth = shost->can_queue;
718 q_depth = MPI3MR_DEFAULT_SDEV_QD;
719 retval = scsi_change_queue_depth(sdev, q_depth);
725 * mpi3mr_update_sdev - Update SCSI device information
726 * @sdev: SCSI device reference
727 * @data: target device reference
729 * This is an iterator function called for each SCSI device in a
730 * target to update the target specific information into each
736 mpi3mr_update_sdev(struct scsi_device *sdev, void *data)
738 struct mpi3mr_tgt_dev *tgtdev;
740 tgtdev = (struct mpi3mr_tgt_dev *)data;
744 mpi3mr_change_queue_depth(sdev, tgtdev->q_depth);
745 switch (tgtdev->dev_type) {
746 case MPI3_DEVICE_DEVFORM_PCIE:
747 /*The block layer hw sector size = 512*/
748 if ((tgtdev->dev_spec.pcie_inf.dev_info &
749 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) ==
750 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) {
751 blk_queue_max_hw_sectors(sdev->request_queue,
752 tgtdev->dev_spec.pcie_inf.mdts / 512);
753 if (tgtdev->dev_spec.pcie_inf.pgsz == 0)
754 blk_queue_virt_boundary(sdev->request_queue,
755 ((1 << MPI3MR_DEFAULT_PGSZEXP) - 1));
757 blk_queue_virt_boundary(sdev->request_queue,
758 ((1 << tgtdev->dev_spec.pcie_inf.pgsz) - 1));
767 * mpi3mr_rfresh_tgtdevs - Refresh target device exposure
768 * @mrioc: Adapter instance reference
770 * This is executed post controller reset to identify any
771 * missing devices during reset and remove from the upper layers
772 * or expose any newly detected device to the upper layers.
777 void mpi3mr_rfresh_tgtdevs(struct mpi3mr_ioc *mrioc)
779 struct mpi3mr_tgt_dev *tgtdev, *tgtdev_next;
781 list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
783 if ((tgtdev->dev_handle == MPI3MR_INVALID_DEV_HANDLE) &&
784 tgtdev->host_exposed) {
785 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
786 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev);
787 mpi3mr_tgtdev_put(tgtdev);
792 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
793 if ((tgtdev->dev_handle != MPI3MR_INVALID_DEV_HANDLE) &&
794 !tgtdev->is_hidden && !tgtdev->host_exposed)
795 mpi3mr_report_tgtdev_to_host(mrioc, tgtdev->perst_id);
800 * mpi3mr_update_tgtdev - DevStatusChange evt bottomhalf
801 * @mrioc: Adapter instance reference
802 * @tgtdev: Target device internal structure
803 * @dev_pg0: New device page0
805 * Update the information from the device page0 into the driver
806 * cached target device structure.
810 static void mpi3mr_update_tgtdev(struct mpi3mr_ioc *mrioc,
811 struct mpi3mr_tgt_dev *tgtdev, struct mpi3_device_page0 *dev_pg0)
814 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
817 tgtdev->perst_id = le16_to_cpu(dev_pg0->persistent_id);
818 tgtdev->dev_handle = le16_to_cpu(dev_pg0->dev_handle);
819 tgtdev->dev_type = dev_pg0->device_form;
820 tgtdev->encl_handle = le16_to_cpu(dev_pg0->enclosure_handle);
821 tgtdev->parent_handle = le16_to_cpu(dev_pg0->parent_dev_handle);
822 tgtdev->slot = le16_to_cpu(dev_pg0->slot);
823 tgtdev->q_depth = le16_to_cpu(dev_pg0->queue_depth);
824 tgtdev->wwid = le64_to_cpu(dev_pg0->wwid);
826 flags = le16_to_cpu(dev_pg0->flags);
827 tgtdev->is_hidden = (flags & MPI3_DEVICE0_FLAGS_HIDDEN);
829 if (tgtdev->starget && tgtdev->starget->hostdata) {
830 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
831 tgtdev->starget->hostdata;
832 scsi_tgt_priv_data->perst_id = tgtdev->perst_id;
833 scsi_tgt_priv_data->dev_handle = tgtdev->dev_handle;
834 scsi_tgt_priv_data->dev_type = tgtdev->dev_type;
837 switch (dev_pg0->access_status) {
838 case MPI3_DEVICE0_ASTATUS_NO_ERRORS:
839 case MPI3_DEVICE0_ASTATUS_PREPARE:
840 case MPI3_DEVICE0_ASTATUS_NEEDS_INITIALIZATION:
841 case MPI3_DEVICE0_ASTATUS_DEVICE_MISSING_DELAY:
844 tgtdev->is_hidden = 1;
848 switch (tgtdev->dev_type) {
849 case MPI3_DEVICE_DEVFORM_SAS_SATA:
851 struct mpi3_device0_sas_sata_format *sasinf =
852 &dev_pg0->device_specific.sas_sata_format;
853 u16 dev_info = le16_to_cpu(sasinf->device_info);
855 tgtdev->dev_spec.sas_sata_inf.dev_info = dev_info;
856 tgtdev->dev_spec.sas_sata_inf.sas_address =
857 le64_to_cpu(sasinf->sas_address);
858 if ((dev_info & MPI3_SAS_DEVICE_INFO_DEVICE_TYPE_MASK) !=
859 MPI3_SAS_DEVICE_INFO_DEVICE_TYPE_END_DEVICE)
860 tgtdev->is_hidden = 1;
861 else if (!(dev_info & (MPI3_SAS_DEVICE_INFO_STP_SATA_TARGET |
862 MPI3_SAS_DEVICE_INFO_SSP_TARGET)))
863 tgtdev->is_hidden = 1;
866 case MPI3_DEVICE_DEVFORM_PCIE:
868 struct mpi3_device0_pcie_format *pcieinf =
869 &dev_pg0->device_specific.pcie_format;
870 u16 dev_info = le16_to_cpu(pcieinf->device_info);
872 tgtdev->dev_spec.pcie_inf.dev_info = dev_info;
873 tgtdev->dev_spec.pcie_inf.capb =
874 le32_to_cpu(pcieinf->capabilities);
875 tgtdev->dev_spec.pcie_inf.mdts = MPI3MR_DEFAULT_MDTS;
877 tgtdev->dev_spec.pcie_inf.pgsz = 12;
878 if (dev_pg0->access_status == MPI3_DEVICE0_ASTATUS_NO_ERRORS) {
879 tgtdev->dev_spec.pcie_inf.mdts =
880 le32_to_cpu(pcieinf->maximum_data_transfer_size);
881 tgtdev->dev_spec.pcie_inf.pgsz = pcieinf->page_size;
882 tgtdev->dev_spec.pcie_inf.reset_to =
883 max_t(u8, pcieinf->controller_reset_to,
884 MPI3MR_INTADMCMD_TIMEOUT);
885 tgtdev->dev_spec.pcie_inf.abort_to =
886 max_t(u8, pcieinf->nvme_abort_to,
887 MPI3MR_INTADMCMD_TIMEOUT);
889 if (tgtdev->dev_spec.pcie_inf.mdts > (1024 * 1024))
890 tgtdev->dev_spec.pcie_inf.mdts = (1024 * 1024);
891 if (((dev_info & MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) !=
892 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) &&
893 ((dev_info & MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) !=
894 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_SCSI_DEVICE))
895 tgtdev->is_hidden = 1;
898 prot_mask = scsi_host_get_prot(mrioc->shost);
899 if (prot_mask & SHOST_DIX_TYPE0_PROTECTION) {
900 scsi_host_set_prot(mrioc->shost, prot_mask & 0x77);
902 "%s : Disabling DIX0 prot capability\n", __func__);
904 "because HBA does not support DIX0 operation on NVME drives\n");
908 case MPI3_DEVICE_DEVFORM_VD:
910 struct mpi3_device0_vd_format *vdinf =
911 &dev_pg0->device_specific.vd_format;
913 tgtdev->dev_spec.vol_inf.state = vdinf->vd_state;
914 if (vdinf->vd_state == MPI3_DEVICE0_VD_STATE_OFFLINE)
915 tgtdev->is_hidden = 1;
924 * mpi3mr_devstatuschg_evt_bh - DevStatusChange evt bottomhalf
925 * @mrioc: Adapter instance reference
926 * @fwevt: Firmware event information.
928 * Process Device status Change event and based on device's new
929 * information, either expose the device to the upper layers, or
930 * remove the device from upper layers.
934 static void mpi3mr_devstatuschg_evt_bh(struct mpi3mr_ioc *mrioc,
935 struct mpi3mr_fwevt *fwevt)
938 u8 uhide = 0, delete = 0, cleanup = 0;
939 struct mpi3mr_tgt_dev *tgtdev = NULL;
940 struct mpi3_event_data_device_status_change *evtdata =
941 (struct mpi3_event_data_device_status_change *)fwevt->event_data;
943 dev_handle = le16_to_cpu(evtdata->dev_handle);
945 "%s :device status change: handle(0x%04x): reason code(0x%x)\n",
946 __func__, dev_handle, evtdata->reason_code);
947 switch (evtdata->reason_code) {
948 case MPI3_EVENT_DEV_STAT_RC_HIDDEN:
951 case MPI3_EVENT_DEV_STAT_RC_NOT_HIDDEN:
954 case MPI3_EVENT_DEV_STAT_RC_VD_NOT_RESPONDING:
959 ioc_info(mrioc, "%s :Unhandled reason code(0x%x)\n", __func__,
960 evtdata->reason_code);
964 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
968 tgtdev->is_hidden = 0;
969 if (!tgtdev->host_exposed)
970 mpi3mr_report_tgtdev_to_host(mrioc, tgtdev->perst_id);
972 if (tgtdev->starget && tgtdev->starget->hostdata) {
974 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
977 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev);
978 mpi3mr_tgtdev_put(tgtdev);
983 mpi3mr_tgtdev_put(tgtdev);
987 * mpi3mr_devinfochg_evt_bh - DeviceInfoChange evt bottomhalf
988 * @mrioc: Adapter instance reference
989 * @dev_pg0: New device page0
991 * Process Device Info Change event and based on device's new
992 * information, either expose the device to the upper layers, or
993 * remove the device from upper layers or update the details of
998 static void mpi3mr_devinfochg_evt_bh(struct mpi3mr_ioc *mrioc,
999 struct mpi3_device_page0 *dev_pg0)
1001 struct mpi3mr_tgt_dev *tgtdev = NULL;
1002 u16 dev_handle = 0, perst_id = 0;
1004 perst_id = le16_to_cpu(dev_pg0->persistent_id);
1005 dev_handle = le16_to_cpu(dev_pg0->dev_handle);
1007 "%s :Device info change: handle(0x%04x): persist_id(0x%x)\n",
1008 __func__, dev_handle, perst_id);
1009 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
1012 mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0);
1013 if (!tgtdev->is_hidden && !tgtdev->host_exposed)
1014 mpi3mr_report_tgtdev_to_host(mrioc, perst_id);
1015 if (tgtdev->is_hidden && tgtdev->host_exposed)
1016 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1017 if (!tgtdev->is_hidden && tgtdev->host_exposed && tgtdev->starget)
1018 starget_for_each_device(tgtdev->starget, (void *)tgtdev,
1019 mpi3mr_update_sdev);
1022 mpi3mr_tgtdev_put(tgtdev);
1026 * mpi3mr_sastopochg_evt_debug - SASTopoChange details
1027 * @mrioc: Adapter instance reference
1028 * @event_data: SAS topology change list event data
1030 * Prints information about the SAS topology change event.
1035 mpi3mr_sastopochg_evt_debug(struct mpi3mr_ioc *mrioc,
1036 struct mpi3_event_data_sas_topology_change_list *event_data)
1040 u8 reason_code, phy_number;
1041 char *status_str = NULL;
1042 u8 link_rate, prev_link_rate;
1044 switch (event_data->exp_status) {
1045 case MPI3_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
1046 status_str = "remove";
1048 case MPI3_EVENT_SAS_TOPO_ES_RESPONDING:
1049 status_str = "responding";
1051 case MPI3_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
1052 status_str = "remove delay";
1054 case MPI3_EVENT_SAS_TOPO_ES_NO_EXPANDER:
1055 status_str = "direct attached";
1058 status_str = "unknown status";
1061 ioc_info(mrioc, "%s :sas topology change: (%s)\n",
1062 __func__, status_str);
1064 "%s :\texpander_handle(0x%04x), enclosure_handle(0x%04x) start_phy(%02d), num_entries(%d)\n",
1065 __func__, le16_to_cpu(event_data->expander_dev_handle),
1066 le16_to_cpu(event_data->enclosure_handle),
1067 event_data->start_phy_num, event_data->num_entries);
1068 for (i = 0; i < event_data->num_entries; i++) {
1069 handle = le16_to_cpu(event_data->phy_entry[i].attached_dev_handle);
1072 phy_number = event_data->start_phy_num + i;
1073 reason_code = event_data->phy_entry[i].status &
1074 MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
1075 switch (reason_code) {
1076 case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
1077 status_str = "target remove";
1079 case MPI3_EVENT_SAS_TOPO_PHY_RC_DELAY_NOT_RESPONDING:
1080 status_str = "delay target remove";
1082 case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
1083 status_str = "link status change";
1085 case MPI3_EVENT_SAS_TOPO_PHY_RC_NO_CHANGE:
1086 status_str = "link status no change";
1088 case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
1089 status_str = "target responding";
1092 status_str = "unknown";
1095 link_rate = event_data->phy_entry[i].link_rate >> 4;
1096 prev_link_rate = event_data->phy_entry[i].link_rate & 0xF;
1098 "%s :\tphy(%02d), attached_handle(0x%04x): %s: link rate: new(0x%02x), old(0x%02x)\n",
1099 __func__, phy_number, handle, status_str, link_rate,
1105 * mpi3mr_sastopochg_evt_bh - SASTopologyChange evt bottomhalf
1106 * @mrioc: Adapter instance reference
1107 * @fwevt: Firmware event reference
1109 * Prints information about the SAS topology change event and
1110 * for "not responding" event code, removes the device from the
1115 static void mpi3mr_sastopochg_evt_bh(struct mpi3mr_ioc *mrioc,
1116 struct mpi3mr_fwevt *fwevt)
1118 struct mpi3_event_data_sas_topology_change_list *event_data =
1119 (struct mpi3_event_data_sas_topology_change_list *)fwevt->event_data;
1123 struct mpi3mr_tgt_dev *tgtdev = NULL;
1125 mpi3mr_sastopochg_evt_debug(mrioc, event_data);
1127 for (i = 0; i < event_data->num_entries; i++) {
1128 handle = le16_to_cpu(event_data->phy_entry[i].attached_dev_handle);
1131 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
1135 reason_code = event_data->phy_entry[i].status &
1136 MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
1138 switch (reason_code) {
1139 case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
1140 if (tgtdev->host_exposed)
1141 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1142 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev);
1143 mpi3mr_tgtdev_put(tgtdev);
1149 mpi3mr_tgtdev_put(tgtdev);
1154 * mpi3mr_pcietopochg_evt_debug - PCIeTopoChange details
1155 * @mrioc: Adapter instance reference
1156 * @event_data: PCIe topology change list event data
1158 * Prints information about the PCIe topology change event.
1163 mpi3mr_pcietopochg_evt_debug(struct mpi3mr_ioc *mrioc,
1164 struct mpi3_event_data_pcie_topology_change_list *event_data)
1170 char *status_str = NULL;
1171 u8 link_rate, prev_link_rate;
1173 switch (event_data->switch_status) {
1174 case MPI3_EVENT_PCIE_TOPO_SS_NOT_RESPONDING:
1175 status_str = "remove";
1177 case MPI3_EVENT_PCIE_TOPO_SS_RESPONDING:
1178 status_str = "responding";
1180 case MPI3_EVENT_PCIE_TOPO_SS_DELAY_NOT_RESPONDING:
1181 status_str = "remove delay";
1183 case MPI3_EVENT_PCIE_TOPO_SS_NO_PCIE_SWITCH:
1184 status_str = "direct attached";
1187 status_str = "unknown status";
1190 ioc_info(mrioc, "%s :pcie topology change: (%s)\n",
1191 __func__, status_str);
1193 "%s :\tswitch_handle(0x%04x), enclosure_handle(0x%04x) start_port(%02d), num_entries(%d)\n",
1194 __func__, le16_to_cpu(event_data->switch_dev_handle),
1195 le16_to_cpu(event_data->enclosure_handle),
1196 event_data->start_port_num, event_data->num_entries);
1197 for (i = 0; i < event_data->num_entries; i++) {
1199 le16_to_cpu(event_data->port_entry[i].attached_dev_handle);
1202 port_number = event_data->start_port_num + i;
1203 reason_code = event_data->port_entry[i].port_status;
1204 switch (reason_code) {
1205 case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
1206 status_str = "target remove";
1208 case MPI3_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
1209 status_str = "delay target remove";
1211 case MPI3_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
1212 status_str = "link status change";
1214 case MPI3_EVENT_PCIE_TOPO_PS_NO_CHANGE:
1215 status_str = "link status no change";
1217 case MPI3_EVENT_PCIE_TOPO_PS_RESPONDING:
1218 status_str = "target responding";
1221 status_str = "unknown";
1224 link_rate = event_data->port_entry[i].current_port_info &
1225 MPI3_EVENT_PCIE_TOPO_PI_RATE_MASK;
1226 prev_link_rate = event_data->port_entry[i].previous_port_info &
1227 MPI3_EVENT_PCIE_TOPO_PI_RATE_MASK;
1229 "%s :\tport(%02d), attached_handle(0x%04x): %s: link rate: new(0x%02x), old(0x%02x)\n",
1230 __func__, port_number, handle, status_str, link_rate,
1236 * mpi3mr_pcietopochg_evt_bh - PCIeTopologyChange evt bottomhalf
1237 * @mrioc: Adapter instance reference
1238 * @fwevt: Firmware event reference
1240 * Prints information about the PCIe topology change event and
1241 * for "not responding" event code, removes the device from the
1246 static void mpi3mr_pcietopochg_evt_bh(struct mpi3mr_ioc *mrioc,
1247 struct mpi3mr_fwevt *fwevt)
1249 struct mpi3_event_data_pcie_topology_change_list *event_data =
1250 (struct mpi3_event_data_pcie_topology_change_list *)fwevt->event_data;
1254 struct mpi3mr_tgt_dev *tgtdev = NULL;
1256 mpi3mr_pcietopochg_evt_debug(mrioc, event_data);
1258 for (i = 0; i < event_data->num_entries; i++) {
1260 le16_to_cpu(event_data->port_entry[i].attached_dev_handle);
1263 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
1267 reason_code = event_data->port_entry[i].port_status;
1269 switch (reason_code) {
1270 case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
1271 if (tgtdev->host_exposed)
1272 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1273 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev);
1274 mpi3mr_tgtdev_put(tgtdev);
1280 mpi3mr_tgtdev_put(tgtdev);
1285 * mpi3mr_fwevt_bh - Firmware event bottomhalf handler
1286 * @mrioc: Adapter instance reference
1287 * @fwevt: Firmware event reference
1289 * Identifies the firmware event and calls corresponding bottomg
1290 * half handler and sends event acknowledgment if required.
1294 static void mpi3mr_fwevt_bh(struct mpi3mr_ioc *mrioc,
1295 struct mpi3mr_fwevt *fwevt)
1297 mrioc->current_event = fwevt;
1298 mpi3mr_fwevt_del_from_list(mrioc, fwevt);
1300 if (mrioc->stop_drv_processing)
1303 if (!fwevt->process_evt)
1306 switch (fwevt->event_id) {
1307 case MPI3_EVENT_DEVICE_ADDED:
1309 struct mpi3_device_page0 *dev_pg0 =
1310 (struct mpi3_device_page0 *)fwevt->event_data;
1311 mpi3mr_report_tgtdev_to_host(mrioc,
1312 le16_to_cpu(dev_pg0->persistent_id));
1315 case MPI3_EVENT_DEVICE_INFO_CHANGED:
1317 mpi3mr_devinfochg_evt_bh(mrioc,
1318 (struct mpi3_device_page0 *)fwevt->event_data);
1321 case MPI3_EVENT_DEVICE_STATUS_CHANGE:
1323 mpi3mr_devstatuschg_evt_bh(mrioc, fwevt);
1326 case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
1328 mpi3mr_sastopochg_evt_bh(mrioc, fwevt);
1331 case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
1333 mpi3mr_pcietopochg_evt_bh(mrioc, fwevt);
1341 if (fwevt->send_ack)
1342 mpi3mr_process_event_ack(mrioc, fwevt->event_id,
1345 /* Put fwevt reference count to neutralize kref_init increment */
1346 mpi3mr_fwevt_put(fwevt);
1347 mrioc->current_event = NULL;
1351 * mpi3mr_fwevt_worker - Firmware event worker
1352 * @work: Work struct containing firmware event
1354 * Extracts the firmware event and calls mpi3mr_fwevt_bh.
1358 static void mpi3mr_fwevt_worker(struct work_struct *work)
1360 struct mpi3mr_fwevt *fwevt = container_of(work, struct mpi3mr_fwevt,
1362 mpi3mr_fwevt_bh(fwevt->mrioc, fwevt);
1364 * Put fwevt reference count after
1365 * dequeuing it from worker queue
1367 mpi3mr_fwevt_put(fwevt);
1371 * mpi3mr_create_tgtdev - Create and add a target device
1372 * @mrioc: Adapter instance reference
1373 * @dev_pg0: Device Page 0 data
1375 * If the device specified by the device page 0 data is not
1376 * present in the driver's internal list, allocate the memory
1377 * for the device, populate the data and add to the list, else
1378 * update the device data. The key is persistent ID.
1380 * Return: 0 on success, -ENOMEM on memory allocation failure
1382 static int mpi3mr_create_tgtdev(struct mpi3mr_ioc *mrioc,
1383 struct mpi3_device_page0 *dev_pg0)
1386 struct mpi3mr_tgt_dev *tgtdev = NULL;
1389 perst_id = le16_to_cpu(dev_pg0->persistent_id);
1390 tgtdev = mpi3mr_get_tgtdev_by_perst_id(mrioc, perst_id);
1392 mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0);
1393 mpi3mr_tgtdev_put(tgtdev);
1395 tgtdev = mpi3mr_alloc_tgtdev();
1398 mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0);
1399 mpi3mr_tgtdev_add_to_list(mrioc, tgtdev);
1406 * mpi3mr_flush_delayed_cmd_lists - Flush pending commands
1407 * @mrioc: Adapter instance reference
1409 * Flush pending commands in the delayed lists due to a
1410 * controller reset or driver removal as a cleanup.
1414 void mpi3mr_flush_delayed_cmd_lists(struct mpi3mr_ioc *mrioc)
1416 struct delayed_dev_rmhs_node *_rmhs_node;
1417 struct delayed_evt_ack_node *_evtack_node;
1419 dprint_reset(mrioc, "flushing delayed dev_remove_hs commands\n");
1420 while (!list_empty(&mrioc->delayed_rmhs_list)) {
1421 _rmhs_node = list_entry(mrioc->delayed_rmhs_list.next,
1422 struct delayed_dev_rmhs_node, list);
1423 list_del(&_rmhs_node->list);
1426 dprint_reset(mrioc, "flushing delayed event ack commands\n");
1427 while (!list_empty(&mrioc->delayed_evtack_cmds_list)) {
1428 _evtack_node = list_entry(mrioc->delayed_evtack_cmds_list.next,
1429 struct delayed_evt_ack_node, list);
1430 list_del(&_evtack_node->list);
1431 kfree(_evtack_node);
1436 * mpi3mr_dev_rmhs_complete_iou - Device removal IOUC completion
1437 * @mrioc: Adapter instance reference
1438 * @drv_cmd: Internal command tracker
1440 * Issues a target reset TM to the firmware from the device
1441 * removal TM pend list or retry the removal handshake sequence
1442 * based on the IOU control request IOC status.
1446 static void mpi3mr_dev_rmhs_complete_iou(struct mpi3mr_ioc *mrioc,
1447 struct mpi3mr_drv_cmd *drv_cmd)
1449 u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
1450 struct delayed_dev_rmhs_node *delayed_dev_rmhs = NULL;
1453 "%s :dev_rmhs_iouctrl_complete:handle(0x%04x), ioc_status(0x%04x), loginfo(0x%08x)\n",
1454 __func__, drv_cmd->dev_handle, drv_cmd->ioc_status,
1455 drv_cmd->ioc_loginfo);
1456 if (drv_cmd->ioc_status != MPI3_IOCSTATUS_SUCCESS) {
1457 if (drv_cmd->retry_count < MPI3MR_DEV_RMHS_RETRY_COUNT) {
1458 drv_cmd->retry_count++;
1460 "%s :dev_rmhs_iouctrl_complete: handle(0x%04x)retrying handshake retry=%d\n",
1461 __func__, drv_cmd->dev_handle,
1462 drv_cmd->retry_count);
1463 mpi3mr_dev_rmhs_send_tm(mrioc, drv_cmd->dev_handle,
1464 drv_cmd, drv_cmd->iou_rc);
1468 "%s :dev removal handshake failed after all retries: handle(0x%04x)\n",
1469 __func__, drv_cmd->dev_handle);
1472 "%s :dev removal handshake completed successfully: handle(0x%04x)\n",
1473 __func__, drv_cmd->dev_handle);
1474 clear_bit(drv_cmd->dev_handle, mrioc->removepend_bitmap);
1477 if (!list_empty(&mrioc->delayed_rmhs_list)) {
1478 delayed_dev_rmhs = list_entry(mrioc->delayed_rmhs_list.next,
1479 struct delayed_dev_rmhs_node, list);
1480 drv_cmd->dev_handle = delayed_dev_rmhs->handle;
1481 drv_cmd->retry_count = 0;
1482 drv_cmd->iou_rc = delayed_dev_rmhs->iou_rc;
1484 "%s :dev_rmhs_iouctrl_complete: processing delayed TM: handle(0x%04x)\n",
1485 __func__, drv_cmd->dev_handle);
1486 mpi3mr_dev_rmhs_send_tm(mrioc, drv_cmd->dev_handle, drv_cmd,
1488 list_del(&delayed_dev_rmhs->list);
1489 kfree(delayed_dev_rmhs);
1492 drv_cmd->state = MPI3MR_CMD_NOTUSED;
1493 drv_cmd->callback = NULL;
1494 drv_cmd->retry_count = 0;
1495 drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
1496 clear_bit(cmd_idx, mrioc->devrem_bitmap);
1500 * mpi3mr_dev_rmhs_complete_tm - Device removal TM completion
1501 * @mrioc: Adapter instance reference
1502 * @drv_cmd: Internal command tracker
1504 * Issues a target reset TM to the firmware from the device
1505 * removal TM pend list or issue IO unit control request as
1506 * part of device removal or hidden acknowledgment handshake.
1510 static void mpi3mr_dev_rmhs_complete_tm(struct mpi3mr_ioc *mrioc,
1511 struct mpi3mr_drv_cmd *drv_cmd)
1513 struct mpi3_iounit_control_request iou_ctrl;
1514 u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
1515 struct mpi3_scsi_task_mgmt_reply *tm_reply = NULL;
1518 if (drv_cmd->state & MPI3MR_CMD_REPLY_VALID)
1519 tm_reply = (struct mpi3_scsi_task_mgmt_reply *)drv_cmd->reply;
1523 "dev_rmhs_tr_complete:handle(0x%04x), ioc_status(0x%04x), loginfo(0x%08x), term_count(%d)\n",
1524 mrioc->name, drv_cmd->dev_handle, drv_cmd->ioc_status,
1525 drv_cmd->ioc_loginfo,
1526 le32_to_cpu(tm_reply->termination_count));
1528 pr_info(IOCNAME "Issuing IOU CTL: handle(0x%04x) dev_rmhs idx(%d)\n",
1529 mrioc->name, drv_cmd->dev_handle, cmd_idx);
1531 memset(&iou_ctrl, 0, sizeof(iou_ctrl));
1533 drv_cmd->state = MPI3MR_CMD_PENDING;
1534 drv_cmd->is_waiting = 0;
1535 drv_cmd->callback = mpi3mr_dev_rmhs_complete_iou;
1536 iou_ctrl.operation = drv_cmd->iou_rc;
1537 iou_ctrl.param16[0] = cpu_to_le16(drv_cmd->dev_handle);
1538 iou_ctrl.host_tag = cpu_to_le16(drv_cmd->host_tag);
1539 iou_ctrl.function = MPI3_FUNCTION_IO_UNIT_CONTROL;
1541 retval = mpi3mr_admin_request_post(mrioc, &iou_ctrl, sizeof(iou_ctrl),
1544 pr_err(IOCNAME "Issue DevRmHsTMIOUCTL: Admin post failed\n",
1551 drv_cmd->state = MPI3MR_CMD_NOTUSED;
1552 drv_cmd->callback = NULL;
1553 drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
1554 drv_cmd->retry_count = 0;
1555 clear_bit(cmd_idx, mrioc->devrem_bitmap);
1559 * mpi3mr_dev_rmhs_send_tm - Issue TM for device removal
1560 * @mrioc: Adapter instance reference
1561 * @handle: Device handle
1562 * @cmdparam: Internal command tracker
1563 * @iou_rc: IO unit reason code
1565 * Issues a target reset TM to the firmware or add it to a pend
1566 * list as part of device removal or hidden acknowledgment
1571 static void mpi3mr_dev_rmhs_send_tm(struct mpi3mr_ioc *mrioc, u16 handle,
1572 struct mpi3mr_drv_cmd *cmdparam, u8 iou_rc)
1574 struct mpi3_scsi_task_mgmt_request tm_req;
1576 u16 cmd_idx = MPI3MR_NUM_DEVRMCMD;
1578 struct mpi3mr_drv_cmd *drv_cmd = cmdparam;
1579 struct delayed_dev_rmhs_node *delayed_dev_rmhs = NULL;
1584 cmd_idx = find_first_zero_bit(mrioc->devrem_bitmap,
1585 MPI3MR_NUM_DEVRMCMD);
1586 if (cmd_idx < MPI3MR_NUM_DEVRMCMD) {
1587 if (!test_and_set_bit(cmd_idx, mrioc->devrem_bitmap))
1589 cmd_idx = MPI3MR_NUM_DEVRMCMD;
1591 } while (retrycount--);
1593 if (cmd_idx >= MPI3MR_NUM_DEVRMCMD) {
1594 delayed_dev_rmhs = kzalloc(sizeof(*delayed_dev_rmhs),
1596 if (!delayed_dev_rmhs)
1598 INIT_LIST_HEAD(&delayed_dev_rmhs->list);
1599 delayed_dev_rmhs->handle = handle;
1600 delayed_dev_rmhs->iou_rc = iou_rc;
1601 list_add_tail(&delayed_dev_rmhs->list,
1602 &mrioc->delayed_rmhs_list);
1603 ioc_info(mrioc, "%s :DevRmHs: tr:handle(0x%04x) is postponed\n",
1607 drv_cmd = &mrioc->dev_rmhs_cmds[cmd_idx];
1610 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
1612 "%s :Issuing TR TM: for devhandle 0x%04x with dev_rmhs %d\n",
1613 __func__, handle, cmd_idx);
1615 memset(&tm_req, 0, sizeof(tm_req));
1616 if (drv_cmd->state & MPI3MR_CMD_PENDING) {
1617 ioc_err(mrioc, "%s :Issue TM: Command is in use\n", __func__);
1620 drv_cmd->state = MPI3MR_CMD_PENDING;
1621 drv_cmd->is_waiting = 0;
1622 drv_cmd->callback = mpi3mr_dev_rmhs_complete_tm;
1623 drv_cmd->dev_handle = handle;
1624 drv_cmd->iou_rc = iou_rc;
1625 tm_req.dev_handle = cpu_to_le16(handle);
1626 tm_req.task_type = MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
1627 tm_req.host_tag = cpu_to_le16(drv_cmd->host_tag);
1628 tm_req.task_host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INVALID);
1629 tm_req.function = MPI3_FUNCTION_SCSI_TASK_MGMT;
1631 set_bit(handle, mrioc->removepend_bitmap);
1632 retval = mpi3mr_admin_request_post(mrioc, &tm_req, sizeof(tm_req), 1);
1634 ioc_err(mrioc, "%s :Issue DevRmHsTM: Admin Post failed\n",
1641 drv_cmd->state = MPI3MR_CMD_NOTUSED;
1642 drv_cmd->callback = NULL;
1643 drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
1644 drv_cmd->retry_count = 0;
1645 clear_bit(cmd_idx, mrioc->devrem_bitmap);
1649 * mpi3mr_complete_evt_ack - event ack request completion
1650 * @mrioc: Adapter instance reference
1651 * @drv_cmd: Internal command tracker
1653 * This is the completion handler for non blocking event
1654 * acknowledgment sent to the firmware and this will issue any
1655 * pending event acknowledgment request.
1659 static void mpi3mr_complete_evt_ack(struct mpi3mr_ioc *mrioc,
1660 struct mpi3mr_drv_cmd *drv_cmd)
1662 u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
1663 struct delayed_evt_ack_node *delayed_evtack = NULL;
1665 if (drv_cmd->ioc_status != MPI3_IOCSTATUS_SUCCESS) {
1666 dprint_event_th(mrioc,
1667 "immediate event ack failed with ioc_status(0x%04x) log_info(0x%08x)\n",
1668 (drv_cmd->ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
1669 drv_cmd->ioc_loginfo);
1672 if (!list_empty(&mrioc->delayed_evtack_cmds_list)) {
1674 list_entry(mrioc->delayed_evtack_cmds_list.next,
1675 struct delayed_evt_ack_node, list);
1676 mpi3mr_send_event_ack(mrioc, delayed_evtack->event, drv_cmd,
1677 delayed_evtack->event_ctx);
1678 list_del(&delayed_evtack->list);
1679 kfree(delayed_evtack);
1682 drv_cmd->state = MPI3MR_CMD_NOTUSED;
1683 drv_cmd->callback = NULL;
1684 clear_bit(cmd_idx, mrioc->evtack_cmds_bitmap);
1688 * mpi3mr_send_event_ack - Issue event acknwoledgment request
1689 * @mrioc: Adapter instance reference
1690 * @event: MPI3 event id
1691 * @cmdparam: Internal command tracker
1692 * @event_ctx: event context
1694 * Issues event acknowledgment request to the firmware if there
1695 * is a free command to send the event ack else it to a pend
1696 * list so that it will be processed on a completion of a prior
1697 * event acknowledgment .
1701 static void mpi3mr_send_event_ack(struct mpi3mr_ioc *mrioc, u8 event,
1702 struct mpi3mr_drv_cmd *cmdparam, u32 event_ctx)
1704 struct mpi3_event_ack_request evtack_req;
1707 u16 cmd_idx = MPI3MR_NUM_EVTACKCMD;
1708 struct mpi3mr_drv_cmd *drv_cmd = cmdparam;
1709 struct delayed_evt_ack_node *delayed_evtack = NULL;
1712 dprint_event_th(mrioc,
1713 "sending delayed event ack in the top half for event(0x%02x), event_ctx(0x%08x)\n",
1717 dprint_event_th(mrioc,
1718 "sending event ack in the top half for event(0x%02x), event_ctx(0x%08x)\n",
1721 cmd_idx = find_first_zero_bit(mrioc->evtack_cmds_bitmap,
1722 MPI3MR_NUM_EVTACKCMD);
1723 if (cmd_idx < MPI3MR_NUM_EVTACKCMD) {
1724 if (!test_and_set_bit(cmd_idx,
1725 mrioc->evtack_cmds_bitmap))
1727 cmd_idx = MPI3MR_NUM_EVTACKCMD;
1729 } while (retrycount--);
1731 if (cmd_idx >= MPI3MR_NUM_EVTACKCMD) {
1732 delayed_evtack = kzalloc(sizeof(*delayed_evtack),
1734 if (!delayed_evtack)
1736 INIT_LIST_HEAD(&delayed_evtack->list);
1737 delayed_evtack->event = event;
1738 delayed_evtack->event_ctx = event_ctx;
1739 list_add_tail(&delayed_evtack->list,
1740 &mrioc->delayed_evtack_cmds_list);
1741 dprint_event_th(mrioc,
1742 "event ack in the top half for event(0x%02x), event_ctx(0x%08x) is postponed\n",
1746 drv_cmd = &mrioc->evtack_cmds[cmd_idx];
1749 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
1751 memset(&evtack_req, 0, sizeof(evtack_req));
1752 if (drv_cmd->state & MPI3MR_CMD_PENDING) {
1753 dprint_event_th(mrioc,
1754 "sending event ack failed due to command in use\n");
1757 drv_cmd->state = MPI3MR_CMD_PENDING;
1758 drv_cmd->is_waiting = 0;
1759 drv_cmd->callback = mpi3mr_complete_evt_ack;
1760 evtack_req.host_tag = cpu_to_le16(drv_cmd->host_tag);
1761 evtack_req.function = MPI3_FUNCTION_EVENT_ACK;
1762 evtack_req.event = event;
1763 evtack_req.event_context = cpu_to_le32(event_ctx);
1764 retval = mpi3mr_admin_request_post(mrioc, &evtack_req,
1765 sizeof(evtack_req), 1);
1767 dprint_event_th(mrioc,
1768 "posting event ack request is failed\n");
1772 dprint_event_th(mrioc,
1773 "event ack in the top half for event(0x%02x), event_ctx(0x%08x) is posted\n",
1778 drv_cmd->state = MPI3MR_CMD_NOTUSED;
1779 drv_cmd->callback = NULL;
1780 clear_bit(cmd_idx, mrioc->evtack_cmds_bitmap);
1784 * mpi3mr_pcietopochg_evt_th - PCIETopologyChange evt tophalf
1785 * @mrioc: Adapter instance reference
1786 * @event_reply: event data
1788 * Checks for the reason code and based on that either block I/O
1789 * to device, or unblock I/O to the device, or start the device
1790 * removal handshake with reason as remove with the firmware for
1795 static void mpi3mr_pcietopochg_evt_th(struct mpi3mr_ioc *mrioc,
1796 struct mpi3_event_notification_reply *event_reply)
1798 struct mpi3_event_data_pcie_topology_change_list *topo_evt =
1799 (struct mpi3_event_data_pcie_topology_change_list *)event_reply->event_data;
1803 struct mpi3mr_tgt_dev *tgtdev = NULL;
1804 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
1806 for (i = 0; i < topo_evt->num_entries; i++) {
1807 handle = le16_to_cpu(topo_evt->port_entry[i].attached_dev_handle);
1810 reason_code = topo_evt->port_entry[i].port_status;
1811 scsi_tgt_priv_data = NULL;
1812 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
1813 if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
1814 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
1815 tgtdev->starget->hostdata;
1816 switch (reason_code) {
1817 case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
1818 if (scsi_tgt_priv_data) {
1819 scsi_tgt_priv_data->dev_removed = 1;
1820 scsi_tgt_priv_data->dev_removedelay = 0;
1821 atomic_set(&scsi_tgt_priv_data->block_io, 0);
1823 mpi3mr_dev_rmhs_send_tm(mrioc, handle, NULL,
1824 MPI3_CTRL_OP_REMOVE_DEVICE);
1826 case MPI3_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
1827 if (scsi_tgt_priv_data) {
1828 scsi_tgt_priv_data->dev_removedelay = 1;
1829 atomic_inc(&scsi_tgt_priv_data->block_io);
1832 case MPI3_EVENT_PCIE_TOPO_PS_RESPONDING:
1833 if (scsi_tgt_priv_data &&
1834 scsi_tgt_priv_data->dev_removedelay) {
1835 scsi_tgt_priv_data->dev_removedelay = 0;
1836 atomic_dec_if_positive
1837 (&scsi_tgt_priv_data->block_io);
1840 case MPI3_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
1845 mpi3mr_tgtdev_put(tgtdev);
1850 * mpi3mr_sastopochg_evt_th - SASTopologyChange evt tophalf
1851 * @mrioc: Adapter instance reference
1852 * @event_reply: event data
1854 * Checks for the reason code and based on that either block I/O
1855 * to device, or unblock I/O to the device, or start the device
1856 * removal handshake with reason as remove with the firmware for
1861 static void mpi3mr_sastopochg_evt_th(struct mpi3mr_ioc *mrioc,
1862 struct mpi3_event_notification_reply *event_reply)
1864 struct mpi3_event_data_sas_topology_change_list *topo_evt =
1865 (struct mpi3_event_data_sas_topology_change_list *)event_reply->event_data;
1869 struct mpi3mr_tgt_dev *tgtdev = NULL;
1870 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
1872 for (i = 0; i < topo_evt->num_entries; i++) {
1873 handle = le16_to_cpu(topo_evt->phy_entry[i].attached_dev_handle);
1876 reason_code = topo_evt->phy_entry[i].status &
1877 MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
1878 scsi_tgt_priv_data = NULL;
1879 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
1880 if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
1881 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
1882 tgtdev->starget->hostdata;
1883 switch (reason_code) {
1884 case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
1885 if (scsi_tgt_priv_data) {
1886 scsi_tgt_priv_data->dev_removed = 1;
1887 scsi_tgt_priv_data->dev_removedelay = 0;
1888 atomic_set(&scsi_tgt_priv_data->block_io, 0);
1890 mpi3mr_dev_rmhs_send_tm(mrioc, handle, NULL,
1891 MPI3_CTRL_OP_REMOVE_DEVICE);
1893 case MPI3_EVENT_SAS_TOPO_PHY_RC_DELAY_NOT_RESPONDING:
1894 if (scsi_tgt_priv_data) {
1895 scsi_tgt_priv_data->dev_removedelay = 1;
1896 atomic_inc(&scsi_tgt_priv_data->block_io);
1899 case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
1900 if (scsi_tgt_priv_data &&
1901 scsi_tgt_priv_data->dev_removedelay) {
1902 scsi_tgt_priv_data->dev_removedelay = 0;
1903 atomic_dec_if_positive
1904 (&scsi_tgt_priv_data->block_io);
1907 case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
1912 mpi3mr_tgtdev_put(tgtdev);
1917 * mpi3mr_devstatuschg_evt_th - DeviceStatusChange evt tophalf
1918 * @mrioc: Adapter instance reference
1919 * @event_reply: event data
1921 * Checks for the reason code and based on that either block I/O
1922 * to device, or unblock I/O to the device, or start the device
1923 * removal handshake with reason as remove/hide acknowledgment
1924 * with the firmware.
1928 static void mpi3mr_devstatuschg_evt_th(struct mpi3mr_ioc *mrioc,
1929 struct mpi3_event_notification_reply *event_reply)
1932 u8 ublock = 0, block = 0, hide = 0, delete = 0, remove = 0;
1933 struct mpi3mr_tgt_dev *tgtdev = NULL;
1934 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
1935 struct mpi3_event_data_device_status_change *evtdata =
1936 (struct mpi3_event_data_device_status_change *)event_reply->event_data;
1938 if (mrioc->stop_drv_processing)
1941 dev_handle = le16_to_cpu(evtdata->dev_handle);
1943 switch (evtdata->reason_code) {
1944 case MPI3_EVENT_DEV_STAT_RC_INT_DEVICE_RESET_STRT:
1945 case MPI3_EVENT_DEV_STAT_RC_INT_IT_NEXUS_RESET_STRT:
1948 case MPI3_EVENT_DEV_STAT_RC_HIDDEN:
1952 case MPI3_EVENT_DEV_STAT_RC_VD_NOT_RESPONDING:
1956 case MPI3_EVENT_DEV_STAT_RC_INT_DEVICE_RESET_CMP:
1957 case MPI3_EVENT_DEV_STAT_RC_INT_IT_NEXUS_RESET_CMP:
1964 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
1968 tgtdev->is_hidden = hide;
1969 if (tgtdev->starget && tgtdev->starget->hostdata) {
1970 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
1971 tgtdev->starget->hostdata;
1973 atomic_inc(&scsi_tgt_priv_data->block_io);
1975 scsi_tgt_priv_data->dev_removed = 1;
1977 atomic_dec_if_positive(&scsi_tgt_priv_data->block_io);
1980 mpi3mr_dev_rmhs_send_tm(mrioc, dev_handle, NULL,
1981 MPI3_CTRL_OP_REMOVE_DEVICE);
1983 mpi3mr_dev_rmhs_send_tm(mrioc, dev_handle, NULL,
1984 MPI3_CTRL_OP_HIDDEN_ACK);
1988 mpi3mr_tgtdev_put(tgtdev);
1992 * mpi3mr_preparereset_evt_th - Prepare for reset event tophalf
1993 * @mrioc: Adapter instance reference
1994 * @event_reply: event data
1996 * Blocks and unblocks host level I/O based on the reason code
2000 static void mpi3mr_preparereset_evt_th(struct mpi3mr_ioc *mrioc,
2001 struct mpi3_event_notification_reply *event_reply)
2003 struct mpi3_event_data_prepare_for_reset *evtdata =
2004 (struct mpi3_event_data_prepare_for_reset *)event_reply->event_data;
2006 if (evtdata->reason_code == MPI3_EVENT_PREPARE_RESET_RC_START) {
2007 dprint_event_th(mrioc,
2008 "prepare for reset event top half with rc=start\n");
2009 if (mrioc->prepare_for_reset)
2011 mrioc->prepare_for_reset = 1;
2012 mrioc->prepare_for_reset_timeout_counter = 0;
2013 } else if (evtdata->reason_code == MPI3_EVENT_PREPARE_RESET_RC_ABORT) {
2014 dprint_event_th(mrioc,
2015 "prepare for reset top half with rc=abort\n");
2016 mrioc->prepare_for_reset = 0;
2017 mrioc->prepare_for_reset_timeout_counter = 0;
2019 if ((event_reply->msg_flags & MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_MASK)
2020 == MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_REQUIRED)
2021 mpi3mr_send_event_ack(mrioc, event_reply->event, NULL,
2022 le32_to_cpu(event_reply->event_context));
2026 * mpi3mr_energypackchg_evt_th - Energy pack change evt tophalf
2027 * @mrioc: Adapter instance reference
2028 * @event_reply: event data
2030 * Identifies the new shutdown timeout value and update.
2034 static void mpi3mr_energypackchg_evt_th(struct mpi3mr_ioc *mrioc,
2035 struct mpi3_event_notification_reply *event_reply)
2037 struct mpi3_event_data_energy_pack_change *evtdata =
2038 (struct mpi3_event_data_energy_pack_change *)event_reply->event_data;
2039 u16 shutdown_timeout = le16_to_cpu(evtdata->shutdown_timeout);
2041 if (shutdown_timeout <= 0) {
2043 "%s :Invalid Shutdown Timeout received = %d\n",
2044 __func__, shutdown_timeout);
2049 "%s :Previous Shutdown Timeout Value = %d New Shutdown Timeout Value = %d\n",
2050 __func__, mrioc->facts.shutdown_timeout, shutdown_timeout);
2051 mrioc->facts.shutdown_timeout = shutdown_timeout;
2055 * mpi3mr_tempthreshold_evt_th - Temp threshold event tophalf
2056 * @mrioc: Adapter instance reference
2057 * @event_reply: event data
2059 * Displays temperature threshold event details and fault code
2060 * if any is hit due to temperature exceeding threshold.
2064 static void mpi3mr_tempthreshold_evt_th(struct mpi3mr_ioc *mrioc,
2065 struct mpi3_event_notification_reply *event_reply)
2067 struct mpi3_event_data_temp_threshold *evtdata =
2068 (struct mpi3_event_data_temp_threshold *)event_reply->event_data;
2070 ioc_err(mrioc, "Temperature threshold levels %s%s%s exceeded for sensor: %d !!! Current temperature in Celsius: %d\n",
2071 (le16_to_cpu(evtdata->status) & 0x1) ? "Warning " : " ",
2072 (le16_to_cpu(evtdata->status) & 0x2) ? "Critical " : " ",
2073 (le16_to_cpu(evtdata->status) & 0x4) ? "Fatal " : " ", evtdata->sensor_num,
2074 le16_to_cpu(evtdata->current_temperature));
2075 mpi3mr_print_fault_info(mrioc);
2079 * mpi3mr_cablemgmt_evt_th - Cable management event tophalf
2080 * @mrioc: Adapter instance reference
2081 * @event_reply: event data
2083 * Displays Cable manegemt event details.
2087 static void mpi3mr_cablemgmt_evt_th(struct mpi3mr_ioc *mrioc,
2088 struct mpi3_event_notification_reply *event_reply)
2090 struct mpi3_event_data_cable_management *evtdata =
2091 (struct mpi3_event_data_cable_management *)event_reply->event_data;
2093 switch (evtdata->status) {
2094 case MPI3_EVENT_CABLE_MGMT_STATUS_INSUFFICIENT_POWER:
2096 ioc_info(mrioc, "An active cable with receptacle_id %d cannot be powered.\n"
2097 "Devices connected to this cable are not detected.\n"
2098 "This cable requires %d mW of power.\n",
2099 evtdata->receptacle_id,
2100 le32_to_cpu(evtdata->active_cable_power_requirement));
2103 case MPI3_EVENT_CABLE_MGMT_STATUS_DEGRADED:
2105 ioc_info(mrioc, "A cable with receptacle_id %d is not running at optimal speed\n",
2106 evtdata->receptacle_id);
2115 * mpi3mr_os_handle_events - Firmware event handler
2116 * @mrioc: Adapter instance reference
2117 * @event_reply: event data
2119 * Identify whteher the event has to handled and acknowledged
2120 * and either process the event in the tophalf and/or schedule a
2121 * bottom half through mpi3mr_fwevt_worker.
2125 void mpi3mr_os_handle_events(struct mpi3mr_ioc *mrioc,
2126 struct mpi3_event_notification_reply *event_reply)
2129 struct mpi3mr_fwevt *fwevt = NULL;
2130 bool ack_req = 0, process_evt_bh = 0;
2132 if (mrioc->stop_drv_processing)
2135 if ((event_reply->msg_flags & MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_MASK)
2136 == MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_REQUIRED)
2139 evt_type = event_reply->event;
2142 case MPI3_EVENT_DEVICE_ADDED:
2144 struct mpi3_device_page0 *dev_pg0 =
2145 (struct mpi3_device_page0 *)event_reply->event_data;
2146 if (mpi3mr_create_tgtdev(mrioc, dev_pg0))
2148 "%s :Failed to add device in the device add event\n",
2154 case MPI3_EVENT_DEVICE_STATUS_CHANGE:
2157 mpi3mr_devstatuschg_evt_th(mrioc, event_reply);
2160 case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
2163 mpi3mr_sastopochg_evt_th(mrioc, event_reply);
2166 case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
2169 mpi3mr_pcietopochg_evt_th(mrioc, event_reply);
2172 case MPI3_EVENT_PREPARE_FOR_RESET:
2174 mpi3mr_preparereset_evt_th(mrioc, event_reply);
2178 case MPI3_EVENT_DEVICE_INFO_CHANGED:
2183 case MPI3_EVENT_ENERGY_PACK_CHANGE:
2185 mpi3mr_energypackchg_evt_th(mrioc, event_reply);
2188 case MPI3_EVENT_TEMP_THRESHOLD:
2190 mpi3mr_tempthreshold_evt_th(mrioc, event_reply);
2193 case MPI3_EVENT_CABLE_MGMT:
2195 mpi3mr_cablemgmt_evt_th(mrioc, event_reply);
2198 case MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE:
2199 case MPI3_EVENT_SAS_DISCOVERY:
2200 case MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
2201 case MPI3_EVENT_SAS_BROADCAST_PRIMITIVE:
2202 case MPI3_EVENT_PCIE_ENUMERATION:
2205 ioc_info(mrioc, "%s :event 0x%02x is not handled\n",
2206 __func__, evt_type);
2209 if (process_evt_bh || ack_req) {
2210 sz = event_reply->event_data_length * 4;
2211 fwevt = mpi3mr_alloc_fwevt(sz);
2213 ioc_info(mrioc, "%s :failure at %s:%d/%s()!\n",
2214 __func__, __FILE__, __LINE__, __func__);
2218 memcpy(fwevt->event_data, event_reply->event_data, sz);
2219 fwevt->mrioc = mrioc;
2220 fwevt->event_id = evt_type;
2221 fwevt->send_ack = ack_req;
2222 fwevt->process_evt = process_evt_bh;
2223 fwevt->evt_ctx = le32_to_cpu(event_reply->event_context);
2224 mpi3mr_fwevt_add_to_list(mrioc, fwevt);
2229 * mpi3mr_setup_eedp - Setup EEDP information in MPI3 SCSI IO
2230 * @mrioc: Adapter instance reference
2231 * @scmd: SCSI command reference
2232 * @scsiio_req: MPI3 SCSI IO request
2234 * Identifies the protection information flags from the SCSI
2235 * command and set appropriate flags in the MPI3 SCSI IO
2240 static void mpi3mr_setup_eedp(struct mpi3mr_ioc *mrioc,
2241 struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
2244 unsigned char prot_op = scsi_get_prot_op(scmd);
2247 case SCSI_PROT_NORMAL:
2249 case SCSI_PROT_READ_STRIP:
2250 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REMOVE;
2252 case SCSI_PROT_WRITE_INSERT:
2253 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_INSERT;
2255 case SCSI_PROT_READ_INSERT:
2256 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_INSERT;
2257 scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2259 case SCSI_PROT_WRITE_STRIP:
2260 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REMOVE;
2261 scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2263 case SCSI_PROT_READ_PASS:
2264 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK;
2265 scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2267 case SCSI_PROT_WRITE_PASS:
2268 if (scmd->prot_flags & SCSI_PROT_IP_CHECKSUM) {
2269 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REGEN;
2270 scsiio_req->sgl[0].eedp.application_tag_translation_mask =
2273 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK;
2275 scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2281 if (scmd->prot_flags & SCSI_PROT_GUARD_CHECK)
2282 eedp_flags |= MPI3_EEDPFLAGS_CHK_GUARD;
2284 if (scmd->prot_flags & SCSI_PROT_IP_CHECKSUM)
2285 eedp_flags |= MPI3_EEDPFLAGS_HOST_GUARD_IP_CHKSUM;
2287 if (scmd->prot_flags & SCSI_PROT_REF_CHECK) {
2288 eedp_flags |= MPI3_EEDPFLAGS_CHK_REF_TAG |
2289 MPI3_EEDPFLAGS_INCR_PRI_REF_TAG;
2290 scsiio_req->cdb.eedp32.primary_reference_tag =
2291 cpu_to_be32(scsi_prot_ref_tag(scmd));
2294 if (scmd->prot_flags & SCSI_PROT_REF_INCREMENT)
2295 eedp_flags |= MPI3_EEDPFLAGS_INCR_PRI_REF_TAG;
2297 eedp_flags |= MPI3_EEDPFLAGS_ESC_MODE_APPTAG_DISABLE;
2299 switch (scsi_prot_interval(scmd)) {
2301 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_512;
2304 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_520;
2307 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4080;
2310 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4088;
2313 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4096;
2316 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4104;
2319 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4160;
2325 scsiio_req->sgl[0].eedp.eedp_flags = cpu_to_le16(eedp_flags);
2326 scsiio_req->sgl[0].eedp.flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_EXTENDED;
2330 * mpi3mr_build_sense_buffer - Map sense information
2332 * @buf: Sense buffer to populate
2334 * @asc: Additional sense code
2335 * @ascq: Additional sense code qualifier
2337 * Maps the given sense information into either descriptor or
2338 * fixed format sense data.
2342 static inline void mpi3mr_build_sense_buffer(int desc, u8 *buf, u8 key,
2346 buf[0] = 0x72; /* descriptor, current */
2352 buf[0] = 0x70; /* fixed, current */
2361 * mpi3mr_map_eedp_error - Map EEDP errors from IOC status
2362 * @scmd: SCSI command reference
2363 * @ioc_status: status of MPI3 request
2365 * Maps the EEDP error status of the SCSI IO request to sense
2370 static void mpi3mr_map_eedp_error(struct scsi_cmnd *scmd,
2375 switch (ioc_status) {
2376 case MPI3_IOCSTATUS_EEDP_GUARD_ERROR:
2379 case MPI3_IOCSTATUS_EEDP_APP_TAG_ERROR:
2382 case MPI3_IOCSTATUS_EEDP_REF_TAG_ERROR:
2390 mpi3mr_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
2392 scmd->result = (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION;
2396 * mpi3mr_process_op_reply_desc - reply descriptor handler
2397 * @mrioc: Adapter instance reference
2398 * @reply_desc: Operational reply descriptor
2399 * @reply_dma: place holder for reply DMA address
2400 * @qidx: Operational queue index
2402 * Process the operational reply descriptor and identifies the
2403 * descriptor type. Based on the descriptor map the MPI3 request
2404 * status to a SCSI command status and calls scsi_done call
2409 void mpi3mr_process_op_reply_desc(struct mpi3mr_ioc *mrioc,
2410 struct mpi3_default_reply_descriptor *reply_desc, u64 *reply_dma, u16 qidx)
2412 u16 reply_desc_type, host_tag = 0;
2413 u16 ioc_status = MPI3_IOCSTATUS_SUCCESS;
2414 u32 ioc_loginfo = 0;
2415 struct mpi3_status_reply_descriptor *status_desc = NULL;
2416 struct mpi3_address_reply_descriptor *addr_desc = NULL;
2417 struct mpi3_success_reply_descriptor *success_desc = NULL;
2418 struct mpi3_scsi_io_reply *scsi_reply = NULL;
2419 struct scsi_cmnd *scmd = NULL;
2420 struct scmd_priv *priv = NULL;
2421 u8 *sense_buf = NULL;
2422 u8 scsi_state = 0, scsi_status = 0, sense_state = 0;
2423 u32 xfer_count = 0, sense_count = 0, resp_data = 0;
2424 u16 dev_handle = 0xFFFF;
2425 struct scsi_sense_hdr sshdr;
2428 reply_desc_type = le16_to_cpu(reply_desc->reply_flags) &
2429 MPI3_REPLY_DESCRIPT_FLAGS_TYPE_MASK;
2430 switch (reply_desc_type) {
2431 case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_STATUS:
2432 status_desc = (struct mpi3_status_reply_descriptor *)reply_desc;
2433 host_tag = le16_to_cpu(status_desc->host_tag);
2434 ioc_status = le16_to_cpu(status_desc->ioc_status);
2436 MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
2437 ioc_loginfo = le32_to_cpu(status_desc->ioc_log_info);
2438 ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
2440 case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_ADDRESS_REPLY:
2441 addr_desc = (struct mpi3_address_reply_descriptor *)reply_desc;
2442 *reply_dma = le64_to_cpu(addr_desc->reply_frame_address);
2443 scsi_reply = mpi3mr_get_reply_virt_addr(mrioc,
2446 panic("%s: scsi_reply is NULL, this shouldn't happen\n",
2450 host_tag = le16_to_cpu(scsi_reply->host_tag);
2451 ioc_status = le16_to_cpu(scsi_reply->ioc_status);
2452 scsi_status = scsi_reply->scsi_status;
2453 scsi_state = scsi_reply->scsi_state;
2454 dev_handle = le16_to_cpu(scsi_reply->dev_handle);
2455 sense_state = (scsi_state & MPI3_SCSI_STATE_SENSE_MASK);
2456 xfer_count = le32_to_cpu(scsi_reply->transfer_count);
2457 sense_count = le32_to_cpu(scsi_reply->sense_count);
2458 resp_data = le32_to_cpu(scsi_reply->response_data);
2459 sense_buf = mpi3mr_get_sensebuf_virt_addr(mrioc,
2460 le64_to_cpu(scsi_reply->sense_data_buffer_address));
2462 MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
2463 ioc_loginfo = le32_to_cpu(scsi_reply->ioc_log_info);
2464 ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
2465 if (sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY)
2466 panic("%s: Ran out of sense buffers\n", mrioc->name);
2468 case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_SUCCESS:
2469 success_desc = (struct mpi3_success_reply_descriptor *)reply_desc;
2470 host_tag = le16_to_cpu(success_desc->host_tag);
2475 scmd = mpi3mr_scmd_from_host_tag(mrioc, host_tag, qidx);
2477 panic("%s: Cannot Identify scmd for host_tag 0x%x\n",
2478 mrioc->name, host_tag);
2481 priv = scsi_cmd_priv(scmd);
2483 scmd->result = DID_OK << 16;
2486 if (ioc_status == MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN &&
2487 xfer_count == 0 && (scsi_status == MPI3_SCSI_STATUS_BUSY ||
2488 scsi_status == MPI3_SCSI_STATUS_RESERVATION_CONFLICT ||
2489 scsi_status == MPI3_SCSI_STATUS_TASK_SET_FULL))
2490 ioc_status = MPI3_IOCSTATUS_SUCCESS;
2492 if ((sense_state == MPI3_SCSI_STATE_SENSE_VALID) && sense_count &&
2494 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE, sense_count);
2496 memcpy(scmd->sense_buffer, sense_buf, sz);
2499 switch (ioc_status) {
2500 case MPI3_IOCSTATUS_BUSY:
2501 case MPI3_IOCSTATUS_INSUFFICIENT_RESOURCES:
2502 scmd->result = SAM_STAT_BUSY;
2504 case MPI3_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
2505 scmd->result = DID_NO_CONNECT << 16;
2507 case MPI3_IOCSTATUS_SCSI_IOC_TERMINATED:
2508 scmd->result = DID_SOFT_ERROR << 16;
2510 case MPI3_IOCSTATUS_SCSI_TASK_TERMINATED:
2511 case MPI3_IOCSTATUS_SCSI_EXT_TERMINATED:
2512 scmd->result = DID_RESET << 16;
2514 case MPI3_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
2515 if ((xfer_count == 0) || (scmd->underflow > xfer_count))
2516 scmd->result = DID_SOFT_ERROR << 16;
2518 scmd->result = (DID_OK << 16) | scsi_status;
2520 case MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN:
2521 scmd->result = (DID_OK << 16) | scsi_status;
2522 if (sense_state == MPI3_SCSI_STATE_SENSE_VALID)
2524 if (xfer_count < scmd->underflow) {
2525 if (scsi_status == SAM_STAT_BUSY)
2526 scmd->result = SAM_STAT_BUSY;
2528 scmd->result = DID_SOFT_ERROR << 16;
2529 } else if ((scsi_state & (MPI3_SCSI_STATE_NO_SCSI_STATUS)) ||
2530 (sense_state != MPI3_SCSI_STATE_SENSE_NOT_AVAILABLE))
2531 scmd->result = DID_SOFT_ERROR << 16;
2532 else if (scsi_state & MPI3_SCSI_STATE_TERMINATED)
2533 scmd->result = DID_RESET << 16;
2535 case MPI3_IOCSTATUS_SCSI_DATA_OVERRUN:
2536 scsi_set_resid(scmd, 0);
2538 case MPI3_IOCSTATUS_SCSI_RECOVERED_ERROR:
2539 case MPI3_IOCSTATUS_SUCCESS:
2540 scmd->result = (DID_OK << 16) | scsi_status;
2541 if ((scsi_state & (MPI3_SCSI_STATE_NO_SCSI_STATUS)) ||
2542 (sense_state == MPI3_SCSI_STATE_SENSE_FAILED) ||
2543 (sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY))
2544 scmd->result = DID_SOFT_ERROR << 16;
2545 else if (scsi_state & MPI3_SCSI_STATE_TERMINATED)
2546 scmd->result = DID_RESET << 16;
2548 case MPI3_IOCSTATUS_EEDP_GUARD_ERROR:
2549 case MPI3_IOCSTATUS_EEDP_REF_TAG_ERROR:
2550 case MPI3_IOCSTATUS_EEDP_APP_TAG_ERROR:
2551 mpi3mr_map_eedp_error(scmd, ioc_status);
2553 case MPI3_IOCSTATUS_SCSI_PROTOCOL_ERROR:
2554 case MPI3_IOCSTATUS_INVALID_FUNCTION:
2555 case MPI3_IOCSTATUS_INVALID_SGL:
2556 case MPI3_IOCSTATUS_INTERNAL_ERROR:
2557 case MPI3_IOCSTATUS_INVALID_FIELD:
2558 case MPI3_IOCSTATUS_INVALID_STATE:
2559 case MPI3_IOCSTATUS_SCSI_IO_DATA_ERROR:
2560 case MPI3_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
2561 case MPI3_IOCSTATUS_INSUFFICIENT_POWER:
2563 scmd->result = DID_SOFT_ERROR << 16;
2567 if (scmd->result != (DID_OK << 16) && (scmd->cmnd[0] != ATA_12) &&
2568 (scmd->cmnd[0] != ATA_16)) {
2569 ioc_info(mrioc, "%s :scmd->result 0x%x\n", __func__,
2571 scsi_print_command(scmd);
2573 "%s :Command issued to handle 0x%02x returned with error 0x%04x loginfo 0x%08x, qid %d\n",
2574 __func__, dev_handle, ioc_status, ioc_loginfo,
2575 priv->req_q_idx + 1);
2577 " host_tag %d scsi_state 0x%02x scsi_status 0x%02x, xfer_cnt %d resp_data 0x%x\n",
2578 host_tag, scsi_state, scsi_status, xfer_count, resp_data);
2580 scsi_normalize_sense(sense_buf, sense_count, &sshdr);
2582 "%s :sense_count 0x%x, sense_key 0x%x ASC 0x%x, ASCQ 0x%x\n",
2583 __func__, sense_count, sshdr.sense_key,
2584 sshdr.asc, sshdr.ascq);
2588 if (priv->meta_sg_valid) {
2589 dma_unmap_sg(&mrioc->pdev->dev, scsi_prot_sglist(scmd),
2590 scsi_prot_sg_count(scmd), scmd->sc_data_direction);
2592 mpi3mr_clear_scmd_priv(mrioc, scmd);
2593 scsi_dma_unmap(scmd);
2597 mpi3mr_repost_sense_buf(mrioc,
2598 le64_to_cpu(scsi_reply->sense_data_buffer_address));
2602 * mpi3mr_get_chain_idx - get free chain buffer index
2603 * @mrioc: Adapter instance reference
2605 * Try to get a free chain buffer index from the free pool.
2607 * Return: -1 on failure or the free chain buffer index
2609 static int mpi3mr_get_chain_idx(struct mpi3mr_ioc *mrioc)
2615 spin_lock(&mrioc->chain_buf_lock);
2616 cmd_idx = find_first_zero_bit(mrioc->chain_bitmap,
2617 mrioc->chain_buf_count);
2618 if (cmd_idx < mrioc->chain_buf_count) {
2619 set_bit(cmd_idx, mrioc->chain_bitmap);
2620 spin_unlock(&mrioc->chain_buf_lock);
2623 spin_unlock(&mrioc->chain_buf_lock);
2625 } while (retry_count--);
2630 * mpi3mr_prepare_sg_scmd - build scatter gather list
2631 * @mrioc: Adapter instance reference
2632 * @scmd: SCSI command reference
2633 * @scsiio_req: MPI3 SCSI IO request
2635 * This function maps SCSI command's data and protection SGEs to
2636 * MPI request SGEs. If required additional 4K chain buffer is
2637 * used to send the SGEs.
2639 * Return: 0 on success, -ENOMEM on dma_map_sg failure
2641 static int mpi3mr_prepare_sg_scmd(struct mpi3mr_ioc *mrioc,
2642 struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
2644 dma_addr_t chain_dma;
2645 struct scatterlist *sg_scmd;
2646 void *sg_local, *chain;
2648 int sges_left, chain_idx;
2649 u32 sges_in_segment;
2650 u8 simple_sgl_flags;
2651 u8 simple_sgl_flags_last;
2652 u8 last_chain_sgl_flags;
2653 struct chain_element *chain_req;
2654 struct scmd_priv *priv = NULL;
2655 u32 meta_sg = le32_to_cpu(scsiio_req->flags) &
2656 MPI3_SCSIIO_FLAGS_DMAOPERATION_HOST_PI;
2658 priv = scsi_cmd_priv(scmd);
2660 simple_sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_SIMPLE |
2661 MPI3_SGE_FLAGS_DLAS_SYSTEM;
2662 simple_sgl_flags_last = simple_sgl_flags |
2663 MPI3_SGE_FLAGS_END_OF_LIST;
2664 last_chain_sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_LAST_CHAIN |
2665 MPI3_SGE_FLAGS_DLAS_SYSTEM;
2668 sg_local = &scsiio_req->sgl[MPI3_SCSIIO_METASGL_INDEX];
2670 sg_local = &scsiio_req->sgl;
2672 if (!scsiio_req->data_length && !meta_sg) {
2673 mpi3mr_build_zero_len_sge(sg_local);
2678 sg_scmd = scsi_prot_sglist(scmd);
2679 sges_left = dma_map_sg(&mrioc->pdev->dev,
2680 scsi_prot_sglist(scmd),
2681 scsi_prot_sg_count(scmd),
2682 scmd->sc_data_direction);
2683 priv->meta_sg_valid = 1; /* To unmap meta sg DMA */
2685 sg_scmd = scsi_sglist(scmd);
2686 sges_left = scsi_dma_map(scmd);
2689 if (sges_left < 0) {
2690 sdev_printk(KERN_ERR, scmd->device,
2691 "scsi_dma_map failed: request for %d bytes!\n",
2692 scsi_bufflen(scmd));
2695 if (sges_left > MPI3MR_SG_DEPTH) {
2696 sdev_printk(KERN_ERR, scmd->device,
2697 "scsi_dma_map returned unsupported sge count %d!\n",
2702 sges_in_segment = (mrioc->facts.op_req_sz -
2703 offsetof(struct mpi3_scsi_io_request, sgl)) / sizeof(struct mpi3_sge_common);
2705 if (scsiio_req->sgl[0].eedp.flags ==
2706 MPI3_SGE_FLAGS_ELEMENT_TYPE_EXTENDED && !meta_sg) {
2707 sg_local += sizeof(struct mpi3_sge_common);
2709 /* Reserve 1st segment (scsiio_req->sgl[0]) for eedp */
2712 if (scsiio_req->msg_flags ==
2713 MPI3_SCSIIO_MSGFLAGS_METASGL_VALID && !meta_sg) {
2715 /* Reserve last segment (scsiio_req->sgl[3]) for meta sg */
2719 sges_in_segment = 1;
2721 if (sges_left <= sges_in_segment)
2722 goto fill_in_last_segment;
2724 /* fill in main message segment when there is a chain following */
2725 while (sges_in_segment > 1) {
2726 mpi3mr_add_sg_single(sg_local, simple_sgl_flags,
2727 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2728 sg_scmd = sg_next(sg_scmd);
2729 sg_local += sizeof(struct mpi3_sge_common);
2734 chain_idx = mpi3mr_get_chain_idx(mrioc);
2737 chain_req = &mrioc->chain_sgl_list[chain_idx];
2739 priv->meta_chain_idx = chain_idx;
2741 priv->chain_idx = chain_idx;
2743 chain = chain_req->addr;
2744 chain_dma = chain_req->dma_addr;
2745 sges_in_segment = sges_left;
2746 chain_length = sges_in_segment * sizeof(struct mpi3_sge_common);
2748 mpi3mr_add_sg_single(sg_local, last_chain_sgl_flags,
2749 chain_length, chain_dma);
2753 fill_in_last_segment:
2754 while (sges_left > 0) {
2756 mpi3mr_add_sg_single(sg_local,
2757 simple_sgl_flags_last, sg_dma_len(sg_scmd),
2758 sg_dma_address(sg_scmd));
2760 mpi3mr_add_sg_single(sg_local, simple_sgl_flags,
2761 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2762 sg_scmd = sg_next(sg_scmd);
2763 sg_local += sizeof(struct mpi3_sge_common);
2771 * mpi3mr_build_sg_scmd - build scatter gather list for SCSI IO
2772 * @mrioc: Adapter instance reference
2773 * @scmd: SCSI command reference
2774 * @scsiio_req: MPI3 SCSI IO request
2776 * This function calls mpi3mr_prepare_sg_scmd for constructing
2777 * both data SGEs and protection information SGEs in the MPI
2778 * format from the SCSI Command as appropriate .
2780 * Return: return value of mpi3mr_prepare_sg_scmd.
2782 static int mpi3mr_build_sg_scmd(struct mpi3mr_ioc *mrioc,
2783 struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
2787 ret = mpi3mr_prepare_sg_scmd(mrioc, scmd, scsiio_req);
2791 if (scsiio_req->msg_flags == MPI3_SCSIIO_MSGFLAGS_METASGL_VALID) {
2792 /* There is a valid meta sg */
2793 scsiio_req->flags |=
2794 cpu_to_le32(MPI3_SCSIIO_FLAGS_DMAOPERATION_HOST_PI);
2795 ret = mpi3mr_prepare_sg_scmd(mrioc, scmd, scsiio_req);
2802 * mpi3mr_tm_response_name - get TM response as a string
2803 * @resp_code: TM response code
2805 * Convert known task management response code as a readable
2808 * Return: response code string.
2810 static const char *mpi3mr_tm_response_name(u8 resp_code)
2814 switch (resp_code) {
2815 case MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE:
2816 desc = "task management request completed";
2818 case MPI3_SCSITASKMGMT_RSPCODE_INVALID_FRAME:
2819 desc = "invalid frame";
2821 case MPI3_SCSITASKMGMT_RSPCODE_TM_FUNCTION_NOT_SUPPORTED:
2822 desc = "task management request not supported";
2824 case MPI3_SCSITASKMGMT_RSPCODE_TM_FAILED:
2825 desc = "task management request failed";
2827 case MPI3_SCSITASKMGMT_RSPCODE_TM_SUCCEEDED:
2828 desc = "task management request succeeded";
2830 case MPI3_SCSITASKMGMT_RSPCODE_TM_INVALID_LUN:
2831 desc = "invalid LUN";
2833 case MPI3_SCSITASKMGMT_RSPCODE_TM_OVERLAPPED_TAG:
2834 desc = "overlapped tag attempted";
2836 case MPI3_SCSITASKMGMT_RSPCODE_IO_QUEUED_ON_IOC:
2837 desc = "task queued, however not sent to target";
2839 case MPI3_SCSITASKMGMT_RSPCODE_TM_NVME_DENIED:
2840 desc = "task management request denied by NVMe device";
2851 * mpi3mr_issue_tm - Issue Task Management request
2852 * @mrioc: Adapter instance reference
2853 * @tm_type: Task Management type
2854 * @handle: Device handle
2856 * @htag: Host tag of the TM request
2857 * @drv_cmd: Internal command tracker
2858 * @resp_code: Response code place holder
2859 * @cmd_priv: SCSI command private data
2861 * Issues a Task Management Request to the controller for a
2862 * specified target, lun and command and wait for its completion
2863 * and check TM response. Recover the TM if it timed out by
2864 * issuing controller reset.
2866 * Return: 0 on success, non-zero on errors
2868 static int mpi3mr_issue_tm(struct mpi3mr_ioc *mrioc, u8 tm_type,
2869 u16 handle, uint lun, u16 htag, ulong timeout,
2870 struct mpi3mr_drv_cmd *drv_cmd,
2871 u8 *resp_code, struct scmd_priv *cmd_priv)
2873 struct mpi3_scsi_task_mgmt_request tm_req;
2874 struct mpi3_scsi_task_mgmt_reply *tm_reply = NULL;
2876 struct mpi3mr_tgt_dev *tgtdev = NULL;
2877 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2878 struct op_req_qinfo *op_req_q = NULL;
2880 ioc_info(mrioc, "%s :Issue TM: TM type (0x%x) for devhandle 0x%04x\n",
2881 __func__, tm_type, handle);
2882 if (mrioc->unrecoverable) {
2884 ioc_err(mrioc, "%s :Issue TM: Unrecoverable controller\n",
2889 memset(&tm_req, 0, sizeof(tm_req));
2890 mutex_lock(&drv_cmd->mutex);
2891 if (drv_cmd->state & MPI3MR_CMD_PENDING) {
2893 ioc_err(mrioc, "%s :Issue TM: Command is in use\n", __func__);
2894 mutex_unlock(&drv_cmd->mutex);
2897 if (mrioc->reset_in_progress) {
2899 ioc_err(mrioc, "%s :Issue TM: Reset in progress\n", __func__);
2900 mutex_unlock(&drv_cmd->mutex);
2904 drv_cmd->state = MPI3MR_CMD_PENDING;
2905 drv_cmd->is_waiting = 1;
2906 drv_cmd->callback = NULL;
2907 tm_req.dev_handle = cpu_to_le16(handle);
2908 tm_req.task_type = tm_type;
2909 tm_req.host_tag = cpu_to_le16(htag);
2911 int_to_scsilun(lun, (struct scsi_lun *)tm_req.lun);
2912 tm_req.function = MPI3_FUNCTION_SCSI_TASK_MGMT;
2914 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
2915 if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata) {
2916 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2917 tgtdev->starget->hostdata;
2918 atomic_inc(&scsi_tgt_priv_data->block_io);
2921 op_req_q = &mrioc->req_qinfo[cmd_priv->req_q_idx];
2922 tm_req.task_host_tag = cpu_to_le16(cmd_priv->host_tag);
2923 tm_req.task_request_queue_id = cpu_to_le16(op_req_q->qid);
2925 if (tgtdev && (tgtdev->dev_type == MPI3_DEVICE_DEVFORM_PCIE)) {
2926 if (cmd_priv && tgtdev->dev_spec.pcie_inf.abort_to)
2927 timeout = tgtdev->dev_spec.pcie_inf.abort_to;
2928 else if (!cmd_priv && tgtdev->dev_spec.pcie_inf.reset_to)
2929 timeout = tgtdev->dev_spec.pcie_inf.reset_to;
2932 init_completion(&drv_cmd->done);
2933 retval = mpi3mr_admin_request_post(mrioc, &tm_req, sizeof(tm_req), 1);
2935 ioc_err(mrioc, "%s :Issue TM: Admin Post failed\n", __func__);
2938 wait_for_completion_timeout(&drv_cmd->done, (timeout * HZ));
2940 if (!(drv_cmd->state & MPI3MR_CMD_COMPLETE)) {
2941 ioc_err(mrioc, "%s :Issue TM: command timed out\n", __func__);
2942 drv_cmd->is_waiting = 0;
2944 if (!(drv_cmd->state & MPI3MR_CMD_RESET))
2945 mpi3mr_soft_reset_handler(mrioc,
2946 MPI3MR_RESET_FROM_TM_TIMEOUT, 1);
2950 if (drv_cmd->state & MPI3MR_CMD_REPLY_VALID)
2951 tm_reply = (struct mpi3_scsi_task_mgmt_reply *)drv_cmd->reply;
2953 if (drv_cmd->ioc_status != MPI3_IOCSTATUS_SUCCESS) {
2955 "%s :Issue TM: handle(0x%04x) Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
2956 __func__, handle, drv_cmd->ioc_status,
2957 drv_cmd->ioc_loginfo);
2963 ioc_err(mrioc, "%s :Issue TM: No TM Reply message\n", __func__);
2968 *resp_code = le32_to_cpu(tm_reply->response_data) &
2969 MPI3MR_RI_MASK_RESPCODE;
2970 switch (*resp_code) {
2971 case MPI3_SCSITASKMGMT_RSPCODE_TM_SUCCEEDED:
2972 case MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE:
2974 case MPI3_SCSITASKMGMT_RSPCODE_IO_QUEUED_ON_IOC:
2975 if (tm_type != MPI3_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
2984 "task management request type(%d) completed for handle(0x%04x) with ioc_status(0x%04x), log_info(0x%08x), termination_count(%d), response:%s(0x%x)\n",
2985 tm_type, handle, drv_cmd->ioc_status, drv_cmd->ioc_loginfo,
2986 le32_to_cpu(tm_reply->termination_count),
2987 mpi3mr_tm_response_name(*resp_code), *resp_code);
2990 drv_cmd->state = MPI3MR_CMD_NOTUSED;
2991 mutex_unlock(&drv_cmd->mutex);
2992 if (scsi_tgt_priv_data)
2993 atomic_dec_if_positive(&scsi_tgt_priv_data->block_io);
2995 mpi3mr_tgtdev_put(tgtdev);
2998 * Flush all IRQ handlers by calling synchronize_irq().
2999 * mpi3mr_ioc_disable_intr() takes care of it.
3001 mpi3mr_ioc_disable_intr(mrioc);
3002 mpi3mr_ioc_enable_intr(mrioc);
3009 * mpi3mr_bios_param - BIOS param callback
3010 * @sdev: SCSI device reference
3011 * @bdev: Block device reference
3012 * @capacity: Capacity in logical sectors
3013 * @params: Parameter array
3015 * Just the parameters with heads/secots/cylinders.
3019 static int mpi3mr_bios_param(struct scsi_device *sdev,
3020 struct block_device *bdev, sector_t capacity, int params[])
3030 dummy = heads * sectors;
3031 cylinders = capacity;
3032 sector_div(cylinders, dummy);
3034 if ((ulong)capacity >= 0x200000) {
3037 dummy = heads * sectors;
3038 cylinders = capacity;
3039 sector_div(cylinders, dummy);
3043 params[1] = sectors;
3044 params[2] = cylinders;
3049 * mpi3mr_map_queues - Map queues callback handler
3050 * @shost: SCSI host reference
3052 * Maps default and poll queues.
3054 * Return: return zero.
3056 static int mpi3mr_map_queues(struct Scsi_Host *shost)
3058 struct mpi3mr_ioc *mrioc = shost_priv(shost);
3059 int i, qoff, offset;
3060 struct blk_mq_queue_map *map = NULL;
3062 offset = mrioc->op_reply_q_offset;
3064 for (i = 0, qoff = 0; i < HCTX_MAX_TYPES; i++) {
3065 map = &shost->tag_set.map[i];
3069 if (i == HCTX_TYPE_DEFAULT)
3070 map->nr_queues = mrioc->default_qcount;
3071 else if (i == HCTX_TYPE_POLL)
3072 map->nr_queues = mrioc->active_poll_qcount;
3074 if (!map->nr_queues) {
3075 BUG_ON(i == HCTX_TYPE_DEFAULT);
3080 * The poll queue(s) doesn't have an IRQ (and hence IRQ
3081 * affinity), so use the regular blk-mq cpu mapping
3083 map->queue_offset = qoff;
3084 if (i != HCTX_TYPE_POLL)
3085 blk_mq_pci_map_queues(map, mrioc->pdev, offset);
3087 blk_mq_map_queues(map);
3089 qoff += map->nr_queues;
3090 offset += map->nr_queues;
3098 * mpi3mr_get_fw_pending_ios - Calculate pending I/O count
3099 * @mrioc: Adapter instance reference
3101 * Calculate the pending I/Os for the controller and return.
3103 * Return: Number of pending I/Os
3105 static inline int mpi3mr_get_fw_pending_ios(struct mpi3mr_ioc *mrioc)
3110 for (i = 0; i < mrioc->num_op_reply_q; i++)
3111 pend_ios += atomic_read(&mrioc->op_reply_qinfo[i].pend_ios);
3116 * mpi3mr_print_pending_host_io - print pending I/Os
3117 * @mrioc: Adapter instance reference
3119 * Print number of pending I/Os and each I/O details prior to
3120 * reset for debug purpose.
3124 static void mpi3mr_print_pending_host_io(struct mpi3mr_ioc *mrioc)
3126 struct Scsi_Host *shost = mrioc->shost;
3128 ioc_info(mrioc, "%s :Pending commands prior to reset: %d\n",
3129 __func__, mpi3mr_get_fw_pending_ios(mrioc));
3130 blk_mq_tagset_busy_iter(&shost->tag_set,
3131 mpi3mr_print_scmd, (void *)mrioc);
3135 * mpi3mr_wait_for_host_io - block for I/Os to complete
3136 * @mrioc: Adapter instance reference
3137 * @timeout: time out in seconds
3138 * Waits for pending I/Os for the given adapter to complete or
3139 * to hit the timeout.
3143 void mpi3mr_wait_for_host_io(struct mpi3mr_ioc *mrioc, u32 timeout)
3145 enum mpi3mr_iocstate iocstate;
3148 iocstate = mpi3mr_get_iocstate(mrioc);
3149 if (iocstate != MRIOC_STATE_READY)
3152 if (!mpi3mr_get_fw_pending_ios(mrioc))
3155 "%s :Waiting for %d seconds prior to reset for %d I/O\n",
3156 __func__, timeout, mpi3mr_get_fw_pending_ios(mrioc));
3158 for (i = 0; i < timeout; i++) {
3159 if (!mpi3mr_get_fw_pending_ios(mrioc))
3161 iocstate = mpi3mr_get_iocstate(mrioc);
3162 if (iocstate != MRIOC_STATE_READY)
3167 ioc_info(mrioc, "%s :Pending I/Os after wait is: %d\n", __func__,
3168 mpi3mr_get_fw_pending_ios(mrioc));
3172 * mpi3mr_eh_host_reset - Host reset error handling callback
3173 * @scmd: SCSI command reference
3175 * Issue controller reset if the scmd is for a Physical Device,
3176 * if the scmd is for RAID volume, then wait for
3177 * MPI3MR_RAID_ERRREC_RESET_TIMEOUT and checke whether any
3178 * pending I/Os prior to issuing reset to the controller.
3180 * Return: SUCCESS of successful reset else FAILED
3182 static int mpi3mr_eh_host_reset(struct scsi_cmnd *scmd)
3184 struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
3185 struct mpi3mr_stgt_priv_data *stgt_priv_data;
3186 struct mpi3mr_sdev_priv_data *sdev_priv_data;
3187 u8 dev_type = MPI3_DEVICE_DEVFORM_VD;
3188 int retval = FAILED, ret;
3190 sdev_priv_data = scmd->device->hostdata;
3191 if (sdev_priv_data && sdev_priv_data->tgt_priv_data) {
3192 stgt_priv_data = sdev_priv_data->tgt_priv_data;
3193 dev_type = stgt_priv_data->dev_type;
3196 if (dev_type == MPI3_DEVICE_DEVFORM_VD) {
3197 mpi3mr_wait_for_host_io(mrioc,
3198 MPI3MR_RAID_ERRREC_RESET_TIMEOUT);
3199 if (!mpi3mr_get_fw_pending_ios(mrioc)) {
3205 mpi3mr_print_pending_host_io(mrioc);
3206 ret = mpi3mr_soft_reset_handler(mrioc,
3207 MPI3MR_RESET_FROM_EH_HOS, 1);
3213 sdev_printk(KERN_INFO, scmd->device,
3214 "Host reset is %s for scmd(%p)\n",
3215 ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
3221 * mpi3mr_eh_target_reset - Target reset error handling callback
3222 * @scmd: SCSI command reference
3224 * Issue Target reset Task Management and verify the scmd is
3225 * terminated successfully and return status accordingly.
3227 * Return: SUCCESS of successful termination of the scmd else
3230 static int mpi3mr_eh_target_reset(struct scsi_cmnd *scmd)
3232 struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
3233 struct mpi3mr_stgt_priv_data *stgt_priv_data;
3234 struct mpi3mr_sdev_priv_data *sdev_priv_data;
3237 int retval = FAILED, ret = 0;
3239 sdev_printk(KERN_INFO, scmd->device,
3240 "Attempting Target Reset! scmd(%p)\n", scmd);
3241 scsi_print_command(scmd);
3243 sdev_priv_data = scmd->device->hostdata;
3244 if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
3245 sdev_printk(KERN_INFO, scmd->device,
3246 "SCSI device is not available\n");
3251 stgt_priv_data = sdev_priv_data->tgt_priv_data;
3252 dev_handle = stgt_priv_data->dev_handle;
3253 sdev_printk(KERN_INFO, scmd->device,
3254 "Target Reset is issued to handle(0x%04x)\n",
3257 ret = mpi3mr_issue_tm(mrioc,
3258 MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET, dev_handle,
3259 sdev_priv_data->lun_id, MPI3MR_HOSTTAG_BLK_TMS,
3260 MPI3MR_RESETTM_TIMEOUT, &mrioc->host_tm_cmds, &resp_code, NULL);
3267 sdev_printk(KERN_INFO, scmd->device,
3268 "Target reset is %s for scmd(%p)\n",
3269 ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
3275 * mpi3mr_eh_dev_reset- Device reset error handling callback
3276 * @scmd: SCSI command reference
3278 * Issue lun reset Task Management and verify the scmd is
3279 * terminated successfully and return status accordingly.
3281 * Return: SUCCESS of successful termination of the scmd else
3284 static int mpi3mr_eh_dev_reset(struct scsi_cmnd *scmd)
3286 struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
3287 struct mpi3mr_stgt_priv_data *stgt_priv_data;
3288 struct mpi3mr_sdev_priv_data *sdev_priv_data;
3291 int retval = FAILED, ret = 0;
3293 sdev_printk(KERN_INFO, scmd->device,
3294 "Attempting Device(lun) Reset! scmd(%p)\n", scmd);
3295 scsi_print_command(scmd);
3297 sdev_priv_data = scmd->device->hostdata;
3298 if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
3299 sdev_printk(KERN_INFO, scmd->device,
3300 "SCSI device is not available\n");
3305 stgt_priv_data = sdev_priv_data->tgt_priv_data;
3306 dev_handle = stgt_priv_data->dev_handle;
3307 sdev_printk(KERN_INFO, scmd->device,
3308 "Device(lun) Reset is issued to handle(0x%04x)\n", dev_handle);
3310 ret = mpi3mr_issue_tm(mrioc,
3311 MPI3_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, dev_handle,
3312 sdev_priv_data->lun_id, MPI3MR_HOSTTAG_BLK_TMS,
3313 MPI3MR_RESETTM_TIMEOUT, &mrioc->host_tm_cmds, &resp_code, NULL);
3320 sdev_printk(KERN_INFO, scmd->device,
3321 "Device(lun) reset is %s for scmd(%p)\n",
3322 ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
3328 * mpi3mr_scan_start - Scan start callback handler
3329 * @shost: SCSI host reference
3331 * Issue port enable request asynchronously.
3335 static void mpi3mr_scan_start(struct Scsi_Host *shost)
3337 struct mpi3mr_ioc *mrioc = shost_priv(shost);
3339 mrioc->scan_started = 1;
3340 ioc_info(mrioc, "%s :Issuing Port Enable\n", __func__);
3341 if (mpi3mr_issue_port_enable(mrioc, 1)) {
3342 ioc_err(mrioc, "%s :Issuing port enable failed\n", __func__);
3343 mrioc->scan_started = 0;
3344 mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
3349 * mpi3mr_scan_finished - Scan finished callback handler
3350 * @shost: SCSI host reference
3351 * @time: Jiffies from the scan start
3353 * Checks whether the port enable is completed or timedout or
3354 * failed and set the scan status accordingly after taking any
3355 * recovery if required.
3357 * Return: 1 on scan finished or timed out, 0 for in progress
3359 static int mpi3mr_scan_finished(struct Scsi_Host *shost,
3362 struct mpi3mr_ioc *mrioc = shost_priv(shost);
3363 u32 pe_timeout = MPI3MR_PORTENABLE_TIMEOUT;
3364 u32 ioc_status = readl(&mrioc->sysif_regs->ioc_status);
3366 if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) ||
3367 (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)) {
3368 ioc_err(mrioc, "port enable failed due to fault or reset\n");
3369 mpi3mr_print_fault_info(mrioc);
3370 mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
3371 mrioc->scan_started = 0;
3372 mrioc->init_cmds.is_waiting = 0;
3373 mrioc->init_cmds.callback = NULL;
3374 mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
3377 if (time >= (pe_timeout * HZ)) {
3378 ioc_err(mrioc, "port enable failed due to time out\n");
3379 mpi3mr_check_rh_fault_ioc(mrioc,
3380 MPI3MR_RESET_FROM_PE_TIMEOUT);
3381 mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
3382 mrioc->scan_started = 0;
3383 mrioc->init_cmds.is_waiting = 0;
3384 mrioc->init_cmds.callback = NULL;
3385 mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
3388 if (mrioc->scan_started)
3391 if (mrioc->scan_failed) {
3393 "port enable failed with status=0x%04x\n",
3394 mrioc->scan_failed);
3396 ioc_info(mrioc, "port enable is successfully completed\n");
3398 mpi3mr_start_watchdog(mrioc);
3399 mrioc->is_driver_loading = 0;
3404 * mpi3mr_slave_destroy - Slave destroy callback handler
3405 * @sdev: SCSI device reference
3407 * Cleanup and free per device(lun) private data.
3411 static void mpi3mr_slave_destroy(struct scsi_device *sdev)
3413 struct Scsi_Host *shost;
3414 struct mpi3mr_ioc *mrioc;
3415 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
3416 struct mpi3mr_tgt_dev *tgt_dev;
3417 unsigned long flags;
3418 struct scsi_target *starget;
3420 if (!sdev->hostdata)
3423 starget = scsi_target(sdev);
3424 shost = dev_to_shost(&starget->dev);
3425 mrioc = shost_priv(shost);
3426 scsi_tgt_priv_data = starget->hostdata;
3428 scsi_tgt_priv_data->num_luns--;
3430 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
3431 tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
3432 if (tgt_dev && (!scsi_tgt_priv_data->num_luns))
3433 tgt_dev->starget = NULL;
3435 mpi3mr_tgtdev_put(tgt_dev);
3436 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
3438 kfree(sdev->hostdata);
3439 sdev->hostdata = NULL;
3443 * mpi3mr_target_destroy - Target destroy callback handler
3444 * @starget: SCSI target reference
3446 * Cleanup and free per target private data.
3450 static void mpi3mr_target_destroy(struct scsi_target *starget)
3452 struct Scsi_Host *shost;
3453 struct mpi3mr_ioc *mrioc;
3454 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
3455 struct mpi3mr_tgt_dev *tgt_dev;
3456 unsigned long flags;
3458 if (!starget->hostdata)
3461 shost = dev_to_shost(&starget->dev);
3462 mrioc = shost_priv(shost);
3463 scsi_tgt_priv_data = starget->hostdata;
3465 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
3466 tgt_dev = __mpi3mr_get_tgtdev_from_tgtpriv(mrioc, scsi_tgt_priv_data);
3467 if (tgt_dev && (tgt_dev->starget == starget) &&
3468 (tgt_dev->perst_id == starget->id))
3469 tgt_dev->starget = NULL;
3471 scsi_tgt_priv_data->tgt_dev = NULL;
3472 scsi_tgt_priv_data->perst_id = 0;
3473 mpi3mr_tgtdev_put(tgt_dev);
3474 mpi3mr_tgtdev_put(tgt_dev);
3476 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
3478 kfree(starget->hostdata);
3479 starget->hostdata = NULL;
3483 * mpi3mr_slave_configure - Slave configure callback handler
3484 * @sdev: SCSI device reference
3486 * Configure queue depth, max hardware sectors and virt boundary
3491 static int mpi3mr_slave_configure(struct scsi_device *sdev)
3493 struct scsi_target *starget;
3494 struct Scsi_Host *shost;
3495 struct mpi3mr_ioc *mrioc;
3496 struct mpi3mr_tgt_dev *tgt_dev;
3497 unsigned long flags;
3500 starget = scsi_target(sdev);
3501 shost = dev_to_shost(&starget->dev);
3502 mrioc = shost_priv(shost);
3504 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
3505 tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
3506 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
3510 mpi3mr_change_queue_depth(sdev, tgt_dev->q_depth);
3511 switch (tgt_dev->dev_type) {
3512 case MPI3_DEVICE_DEVFORM_PCIE:
3513 /*The block layer hw sector size = 512*/
3514 if ((tgt_dev->dev_spec.pcie_inf.dev_info &
3515 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) ==
3516 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) {
3517 blk_queue_max_hw_sectors(sdev->request_queue,
3518 tgt_dev->dev_spec.pcie_inf.mdts / 512);
3519 if (tgt_dev->dev_spec.pcie_inf.pgsz == 0)
3520 blk_queue_virt_boundary(sdev->request_queue,
3521 ((1 << MPI3MR_DEFAULT_PGSZEXP) - 1));
3523 blk_queue_virt_boundary(sdev->request_queue,
3524 ((1 << tgt_dev->dev_spec.pcie_inf.pgsz) - 1));
3531 mpi3mr_tgtdev_put(tgt_dev);
3537 * mpi3mr_slave_alloc -Slave alloc callback handler
3538 * @sdev: SCSI device reference
3540 * Allocate per device(lun) private data and initialize it.
3542 * Return: 0 on success -ENOMEM on memory allocation failure.
3544 static int mpi3mr_slave_alloc(struct scsi_device *sdev)
3546 struct Scsi_Host *shost;
3547 struct mpi3mr_ioc *mrioc;
3548 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
3549 struct mpi3mr_tgt_dev *tgt_dev;
3550 struct mpi3mr_sdev_priv_data *scsi_dev_priv_data;
3551 unsigned long flags;
3552 struct scsi_target *starget;
3555 starget = scsi_target(sdev);
3556 shost = dev_to_shost(&starget->dev);
3557 mrioc = shost_priv(shost);
3558 scsi_tgt_priv_data = starget->hostdata;
3560 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
3561 tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
3564 if (tgt_dev->starget == NULL)
3565 tgt_dev->starget = starget;
3566 mpi3mr_tgtdev_put(tgt_dev);
3569 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
3573 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
3575 scsi_dev_priv_data = kzalloc(sizeof(*scsi_dev_priv_data), GFP_KERNEL);
3576 if (!scsi_dev_priv_data)
3579 scsi_dev_priv_data->lun_id = sdev->lun;
3580 scsi_dev_priv_data->tgt_priv_data = scsi_tgt_priv_data;
3581 sdev->hostdata = scsi_dev_priv_data;
3583 scsi_tgt_priv_data->num_luns++;
3589 * mpi3mr_target_alloc - Target alloc callback handler
3590 * @starget: SCSI target reference
3592 * Allocate per target private data and initialize it.
3594 * Return: 0 on success -ENOMEM on memory allocation failure.
3596 static int mpi3mr_target_alloc(struct scsi_target *starget)
3598 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
3599 struct mpi3mr_ioc *mrioc = shost_priv(shost);
3600 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
3601 struct mpi3mr_tgt_dev *tgt_dev;
3602 unsigned long flags;
3605 scsi_tgt_priv_data = kzalloc(sizeof(*scsi_tgt_priv_data), GFP_KERNEL);
3606 if (!scsi_tgt_priv_data)
3609 starget->hostdata = scsi_tgt_priv_data;
3611 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
3612 tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
3613 if (tgt_dev && !tgt_dev->is_hidden) {
3614 scsi_tgt_priv_data->starget = starget;
3615 scsi_tgt_priv_data->dev_handle = tgt_dev->dev_handle;
3616 scsi_tgt_priv_data->perst_id = tgt_dev->perst_id;
3617 scsi_tgt_priv_data->dev_type = tgt_dev->dev_type;
3618 scsi_tgt_priv_data->tgt_dev = tgt_dev;
3619 tgt_dev->starget = starget;
3620 atomic_set(&scsi_tgt_priv_data->block_io, 0);
3624 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
3630 * mpi3mr_check_return_unmap - Whether an unmap is allowed
3631 * @mrioc: Adapter instance reference
3632 * @scmd: SCSI Command reference
3634 * The controller hardware cannot handle certain unmap commands
3635 * for NVMe drives, this routine checks those and return true
3636 * and completes the SCSI command with proper status and sense
3639 * Return: TRUE for not allowed unmap, FALSE otherwise.
3641 static bool mpi3mr_check_return_unmap(struct mpi3mr_ioc *mrioc,
3642 struct scsi_cmnd *scmd)
3645 u16 param_len, desc_len, trunc_param_len;
3647 trunc_param_len = param_len = get_unaligned_be16(scmd->cmnd + 7);
3649 if (mrioc->pdev->revision) {
3650 if ((param_len > 24) && ((param_len - 8) & 0xF)) {
3651 trunc_param_len -= (param_len - 8) & 0xF;
3652 dprint_scsi_command(mrioc, scmd, MPI3_DEBUG_SCSI_ERROR);
3653 dprint_scsi_err(mrioc,
3654 "truncating param_len from (%d) to (%d)\n",
3655 param_len, trunc_param_len);
3656 put_unaligned_be16(trunc_param_len, scmd->cmnd + 7);
3657 dprint_scsi_command(mrioc, scmd, MPI3_DEBUG_SCSI_ERROR);
3664 "%s: cdb received with zero parameter length\n",
3666 scsi_print_command(scmd);
3667 scmd->result = DID_OK << 16;
3672 if (param_len < 24) {
3674 "%s: cdb received with invalid param_len: %d\n",
3675 __func__, param_len);
3676 scsi_print_command(scmd);
3677 scmd->result = SAM_STAT_CHECK_CONDITION;
3678 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
3683 if (param_len != scsi_bufflen(scmd)) {
3685 "%s: cdb received with param_len: %d bufflen: %d\n",
3686 __func__, param_len, scsi_bufflen(scmd));
3687 scsi_print_command(scmd);
3688 scmd->result = SAM_STAT_CHECK_CONDITION;
3689 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
3694 buf = kzalloc(scsi_bufflen(scmd), GFP_ATOMIC);
3696 scsi_print_command(scmd);
3697 scmd->result = SAM_STAT_CHECK_CONDITION;
3698 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
3703 scsi_sg_copy_to_buffer(scmd, buf, scsi_bufflen(scmd));
3704 desc_len = get_unaligned_be16(&buf[2]);
3706 if (desc_len < 16) {
3708 "%s: Invalid descriptor length in param list: %d\n",
3709 __func__, desc_len);
3710 scsi_print_command(scmd);
3711 scmd->result = SAM_STAT_CHECK_CONDITION;
3712 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
3719 if (param_len > (desc_len + 8)) {
3720 trunc_param_len = desc_len + 8;
3721 scsi_print_command(scmd);
3722 dprint_scsi_err(mrioc,
3723 "truncating param_len(%d) to desc_len+8(%d)\n",
3724 param_len, trunc_param_len);
3725 put_unaligned_be16(trunc_param_len, scmd->cmnd + 7);
3726 scsi_print_command(scmd);
3734 * mpi3mr_allow_scmd_to_fw - Command is allowed during shutdown
3735 * @scmd: SCSI Command reference
3737 * Checks whether a cdb is allowed during shutdown or not.
3739 * Return: TRUE for allowed commands, FALSE otherwise.
3742 inline bool mpi3mr_allow_scmd_to_fw(struct scsi_cmnd *scmd)
3744 switch (scmd->cmnd[0]) {
3745 case SYNCHRONIZE_CACHE:
3754 * mpi3mr_qcmd - I/O request despatcher
3755 * @shost: SCSI Host reference
3756 * @scmd: SCSI Command reference
3758 * Issues the SCSI Command as an MPI3 request.
3760 * Return: 0 on successful queueing of the request or if the
3761 * request is completed with failure.
3762 * SCSI_MLQUEUE_DEVICE_BUSY when the device is busy.
3763 * SCSI_MLQUEUE_HOST_BUSY when the host queue is full.
3765 static int mpi3mr_qcmd(struct Scsi_Host *shost,
3766 struct scsi_cmnd *scmd)
3768 struct mpi3mr_ioc *mrioc = shost_priv(shost);
3769 struct mpi3mr_stgt_priv_data *stgt_priv_data;
3770 struct mpi3mr_sdev_priv_data *sdev_priv_data;
3771 struct scmd_priv *scmd_priv_data = NULL;
3772 struct mpi3_scsi_io_request *scsiio_req = NULL;
3773 struct op_req_qinfo *op_req_q = NULL;
3777 u32 scsiio_flags = 0;
3778 struct request *rq = scsi_cmd_to_rq(scmd);
3782 sdev_priv_data = scmd->device->hostdata;
3783 if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
3784 scmd->result = DID_NO_CONNECT << 16;
3789 if (mrioc->stop_drv_processing &&
3790 !(mpi3mr_allow_scmd_to_fw(scmd))) {
3791 scmd->result = DID_NO_CONNECT << 16;
3796 if (mrioc->reset_in_progress) {
3797 retval = SCSI_MLQUEUE_HOST_BUSY;
3801 stgt_priv_data = sdev_priv_data->tgt_priv_data;
3803 dev_handle = stgt_priv_data->dev_handle;
3804 if (dev_handle == MPI3MR_INVALID_DEV_HANDLE) {
3805 scmd->result = DID_NO_CONNECT << 16;
3809 if (stgt_priv_data->dev_removed) {
3810 scmd->result = DID_NO_CONNECT << 16;
3815 if (atomic_read(&stgt_priv_data->block_io)) {
3816 if (mrioc->stop_drv_processing) {
3817 scmd->result = DID_NO_CONNECT << 16;
3821 retval = SCSI_MLQUEUE_DEVICE_BUSY;
3825 if (stgt_priv_data->dev_type == MPI3_DEVICE_DEVFORM_PCIE)
3827 if ((scmd->cmnd[0] == UNMAP) && is_pcie_dev &&
3828 (mrioc->pdev->device == MPI3_MFGPAGE_DEVID_SAS4116) &&
3829 mpi3mr_check_return_unmap(mrioc, scmd))
3832 host_tag = mpi3mr_host_tag_for_scmd(mrioc, scmd);
3833 if (host_tag == MPI3MR_HOSTTAG_INVALID) {
3834 scmd->result = DID_ERROR << 16;
3839 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3840 scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_READ;
3841 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3842 scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_WRITE;
3844 scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_NO_DATA_TRANSFER;
3846 scsiio_flags |= MPI3_SCSIIO_FLAGS_TASKATTRIBUTE_SIMPLEQ;
3848 if (sdev_priv_data->ncq_prio_enable) {
3849 iprio_class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
3850 if (iprio_class == IOPRIO_CLASS_RT)
3851 scsiio_flags |= 1 << MPI3_SCSIIO_FLAGS_CMDPRI_SHIFT;
3854 if (scmd->cmd_len > 16)
3855 scsiio_flags |= MPI3_SCSIIO_FLAGS_CDB_GREATER_THAN_16;
3857 scmd_priv_data = scsi_cmd_priv(scmd);
3858 memset(scmd_priv_data->mpi3mr_scsiio_req, 0, MPI3MR_ADMIN_REQ_FRAME_SZ);
3859 scsiio_req = (struct mpi3_scsi_io_request *)scmd_priv_data->mpi3mr_scsiio_req;
3860 scsiio_req->function = MPI3_FUNCTION_SCSI_IO;
3861 scsiio_req->host_tag = cpu_to_le16(host_tag);
3863 mpi3mr_setup_eedp(mrioc, scmd, scsiio_req);
3865 memcpy(scsiio_req->cdb.cdb32, scmd->cmnd, scmd->cmd_len);
3866 scsiio_req->data_length = cpu_to_le32(scsi_bufflen(scmd));
3867 scsiio_req->dev_handle = cpu_to_le16(dev_handle);
3868 scsiio_req->flags = cpu_to_le32(scsiio_flags);
3869 int_to_scsilun(sdev_priv_data->lun_id,
3870 (struct scsi_lun *)scsiio_req->lun);
3872 if (mpi3mr_build_sg_scmd(mrioc, scmd, scsiio_req)) {
3873 mpi3mr_clear_scmd_priv(mrioc, scmd);
3874 retval = SCSI_MLQUEUE_HOST_BUSY;
3877 op_req_q = &mrioc->req_qinfo[scmd_priv_data->req_q_idx];
3879 if (mpi3mr_op_request_post(mrioc, op_req_q,
3880 scmd_priv_data->mpi3mr_scsiio_req)) {
3881 mpi3mr_clear_scmd_priv(mrioc, scmd);
3882 retval = SCSI_MLQUEUE_HOST_BUSY;
3890 static struct scsi_host_template mpi3mr_driver_template = {
3891 .module = THIS_MODULE,
3892 .name = "MPI3 Storage Controller",
3893 .proc_name = MPI3MR_DRIVER_NAME,
3894 .queuecommand = mpi3mr_qcmd,
3895 .target_alloc = mpi3mr_target_alloc,
3896 .slave_alloc = mpi3mr_slave_alloc,
3897 .slave_configure = mpi3mr_slave_configure,
3898 .target_destroy = mpi3mr_target_destroy,
3899 .slave_destroy = mpi3mr_slave_destroy,
3900 .scan_finished = mpi3mr_scan_finished,
3901 .scan_start = mpi3mr_scan_start,
3902 .change_queue_depth = mpi3mr_change_queue_depth,
3903 .eh_device_reset_handler = mpi3mr_eh_dev_reset,
3904 .eh_target_reset_handler = mpi3mr_eh_target_reset,
3905 .eh_host_reset_handler = mpi3mr_eh_host_reset,
3906 .bios_param = mpi3mr_bios_param,
3907 .map_queues = mpi3mr_map_queues,
3908 .mq_poll = mpi3mr_blk_mq_poll,
3912 .sg_tablesize = MPI3MR_SG_DEPTH,
3913 /* max xfer supported is 1M (2K in 512 byte sized sectors)
3915 .max_sectors = 2048,
3916 .cmd_per_lun = MPI3MR_MAX_CMDS_LUN,
3917 .max_segment_size = 0xffffffff,
3918 .track_queue_depth = 1,
3919 .cmd_size = sizeof(struct scmd_priv),
3923 * mpi3mr_init_drv_cmd - Initialize internal command tracker
3924 * @cmdptr: Internal command tracker
3925 * @host_tag: Host tag used for the specific command
3927 * Initialize the internal command tracker structure with
3928 * specified host tag.
3932 static inline void mpi3mr_init_drv_cmd(struct mpi3mr_drv_cmd *cmdptr,
3935 mutex_init(&cmdptr->mutex);
3936 cmdptr->reply = NULL;
3937 cmdptr->state = MPI3MR_CMD_NOTUSED;
3938 cmdptr->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
3939 cmdptr->host_tag = host_tag;
3943 * osintfc_mrioc_security_status -Check controller secure status
3944 * @pdev: PCI device instance
3946 * Read the Device Serial Number capability from PCI config
3947 * space and decide whether the controller is secure or not.
3949 * Return: 0 on success, non-zero on failure.
3952 osintfc_mrioc_security_status(struct pci_dev *pdev)
3960 base = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DSN);
3963 "%s: PCI_EXT_CAP_ID_DSN is not supported\n", __func__);
3967 pci_read_config_dword(pdev, base + 4, &cap_data);
3969 debug_status = cap_data & MPI3MR_CTLR_SECURE_DBG_STATUS_MASK;
3970 ctlr_status = cap_data & MPI3MR_CTLR_SECURITY_STATUS_MASK;
3972 switch (ctlr_status) {
3973 case MPI3MR_INVALID_DEVICE:
3975 "%s: Non secure ctlr (Invalid) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
3976 __func__, pdev->device, pdev->subsystem_vendor,
3977 pdev->subsystem_device);
3980 case MPI3MR_CONFIG_SECURE_DEVICE:
3982 dev_info(&pdev->dev,
3983 "%s: Config secure ctlr is detected\n",
3986 case MPI3MR_HARD_SECURE_DEVICE:
3988 case MPI3MR_TAMPERED_DEVICE:
3990 "%s: Non secure ctlr (Tampered) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
3991 __func__, pdev->device, pdev->subsystem_vendor,
3992 pdev->subsystem_device);
4000 if (!retval && debug_status) {
4002 "%s: Non secure ctlr (Secure Dbg) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
4003 __func__, pdev->device, pdev->subsystem_vendor,
4004 pdev->subsystem_device);
4012 * mpi3mr_probe - PCI probe callback
4013 * @pdev: PCI device instance
4014 * @id: PCI device ID details
4016 * controller initialization routine. Checks the security status
4017 * of the controller and if it is invalid or tampered return the
4018 * probe without initializing the controller. Otherwise,
4019 * allocate per adapter instance through shost_priv and
4020 * initialize controller specific data structures, initializae
4021 * the controller hardware, add shost to the SCSI subsystem.
4023 * Return: 0 on success, non-zero on failure.
4027 mpi3mr_probe(struct pci_dev *pdev, const struct pci_device_id *id)
4029 struct mpi3mr_ioc *mrioc = NULL;
4030 struct Scsi_Host *shost = NULL;
4033 if (osintfc_mrioc_security_status(pdev)) {
4034 warn_non_secure_ctlr = 1;
4035 return 1; /* For Invalid and Tampered device */
4038 shost = scsi_host_alloc(&mpi3mr_driver_template,
4039 sizeof(struct mpi3mr_ioc));
4045 mrioc = shost_priv(shost);
4046 mrioc->id = mrioc_ids++;
4047 sprintf(mrioc->driver_name, "%s", MPI3MR_DRIVER_NAME);
4048 sprintf(mrioc->name, "%s%d", mrioc->driver_name, mrioc->id);
4049 INIT_LIST_HEAD(&mrioc->list);
4050 spin_lock(&mrioc_list_lock);
4051 list_add_tail(&mrioc->list, &mrioc_list);
4052 spin_unlock(&mrioc_list_lock);
4054 spin_lock_init(&mrioc->admin_req_lock);
4055 spin_lock_init(&mrioc->reply_free_queue_lock);
4056 spin_lock_init(&mrioc->sbq_lock);
4057 spin_lock_init(&mrioc->fwevt_lock);
4058 spin_lock_init(&mrioc->tgtdev_lock);
4059 spin_lock_init(&mrioc->watchdog_lock);
4060 spin_lock_init(&mrioc->chain_buf_lock);
4062 INIT_LIST_HEAD(&mrioc->fwevt_list);
4063 INIT_LIST_HEAD(&mrioc->tgtdev_list);
4064 INIT_LIST_HEAD(&mrioc->delayed_rmhs_list);
4065 INIT_LIST_HEAD(&mrioc->delayed_evtack_cmds_list);
4067 mutex_init(&mrioc->reset_mutex);
4068 mpi3mr_init_drv_cmd(&mrioc->init_cmds, MPI3MR_HOSTTAG_INITCMDS);
4069 mpi3mr_init_drv_cmd(&mrioc->host_tm_cmds, MPI3MR_HOSTTAG_BLK_TMS);
4071 for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++)
4072 mpi3mr_init_drv_cmd(&mrioc->dev_rmhs_cmds[i],
4073 MPI3MR_HOSTTAG_DEVRMCMD_MIN + i);
4076 mrioc->enable_segqueue = true;
4078 init_waitqueue_head(&mrioc->reset_waitq);
4079 mrioc->logging_level = logging_level;
4080 mrioc->shost = shost;
4083 /* init shost parameters */
4084 shost->max_cmd_len = MPI3MR_MAX_CDB_LENGTH;
4085 shost->max_lun = -1;
4086 shost->unique_id = mrioc->id;
4088 shost->max_channel = 0;
4089 shost->max_id = 0xFFFFFFFF;
4092 scsi_host_set_prot(shost, prot_mask);
4094 prot_mask = SHOST_DIF_TYPE1_PROTECTION
4095 | SHOST_DIF_TYPE2_PROTECTION
4096 | SHOST_DIF_TYPE3_PROTECTION;
4097 scsi_host_set_prot(shost, prot_mask);
4101 "%s :host protection capabilities enabled %s%s%s%s%s%s%s\n",
4103 (prot_mask & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "",
4104 (prot_mask & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "",
4105 (prot_mask & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "",
4106 (prot_mask & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "",
4107 (prot_mask & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "",
4108 (prot_mask & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "",
4109 (prot_mask & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : "");
4111 if (prot_guard_mask)
4112 scsi_host_set_guard(shost, (prot_guard_mask & 3));
4114 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
4116 snprintf(mrioc->fwevt_worker_name, sizeof(mrioc->fwevt_worker_name),
4117 "%s%d_fwevt_wrkr", mrioc->driver_name, mrioc->id);
4118 mrioc->fwevt_worker_thread = alloc_ordered_workqueue(
4119 mrioc->fwevt_worker_name, WQ_MEM_RECLAIM);
4120 if (!mrioc->fwevt_worker_thread) {
4121 ioc_err(mrioc, "failure at %s:%d/%s()!\n",
4122 __FILE__, __LINE__, __func__);
4124 goto fwevtthread_failed;
4127 mrioc->is_driver_loading = 1;
4128 mrioc->cpu_count = num_online_cpus();
4129 if (mpi3mr_setup_resources(mrioc)) {
4130 ioc_err(mrioc, "setup resources failed\n");
4132 goto resource_alloc_failed;
4134 if (mpi3mr_init_ioc(mrioc)) {
4135 ioc_err(mrioc, "initializing IOC failed\n");
4137 goto init_ioc_failed;
4140 shost->nr_hw_queues = mrioc->num_op_reply_q;
4141 if (mrioc->active_poll_qcount)
4144 shost->can_queue = mrioc->max_host_ios;
4145 shost->sg_tablesize = MPI3MR_SG_DEPTH;
4146 shost->max_id = mrioc->facts.max_perids + 1;
4148 retval = scsi_add_host(shost, &pdev->dev);
4150 ioc_err(mrioc, "failure at %s:%d/%s()!\n",
4151 __FILE__, __LINE__, __func__);
4152 goto addhost_failed;
4155 scsi_scan_host(shost);
4159 mpi3mr_stop_watchdog(mrioc);
4160 mpi3mr_cleanup_ioc(mrioc);
4162 mpi3mr_free_mem(mrioc);
4163 mpi3mr_cleanup_resources(mrioc);
4164 resource_alloc_failed:
4165 destroy_workqueue(mrioc->fwevt_worker_thread);
4167 spin_lock(&mrioc_list_lock);
4168 list_del(&mrioc->list);
4169 spin_unlock(&mrioc_list_lock);
4170 scsi_host_put(shost);
4176 * mpi3mr_remove - PCI remove callback
4177 * @pdev: PCI device instance
4179 * Cleanup the IOC by issuing MUR and shutdown notification.
4180 * Free up all memory and resources associated with the
4181 * controllerand target devices, unregister the shost.
4185 static void mpi3mr_remove(struct pci_dev *pdev)
4187 struct Scsi_Host *shost = pci_get_drvdata(pdev);
4188 struct mpi3mr_ioc *mrioc;
4189 struct workqueue_struct *wq;
4190 unsigned long flags;
4191 struct mpi3mr_tgt_dev *tgtdev, *tgtdev_next;
4196 mrioc = shost_priv(shost);
4197 while (mrioc->reset_in_progress || mrioc->is_driver_loading)
4200 mrioc->stop_drv_processing = 1;
4201 mpi3mr_cleanup_fwevt_list(mrioc);
4202 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
4203 wq = mrioc->fwevt_worker_thread;
4204 mrioc->fwevt_worker_thread = NULL;
4205 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
4207 destroy_workqueue(wq);
4208 scsi_remove_host(shost);
4210 list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
4212 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
4213 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev);
4214 mpi3mr_tgtdev_put(tgtdev);
4216 mpi3mr_stop_watchdog(mrioc);
4217 mpi3mr_cleanup_ioc(mrioc);
4218 mpi3mr_free_mem(mrioc);
4219 mpi3mr_cleanup_resources(mrioc);
4221 spin_lock(&mrioc_list_lock);
4222 list_del(&mrioc->list);
4223 spin_unlock(&mrioc_list_lock);
4225 scsi_host_put(shost);
4229 * mpi3mr_shutdown - PCI shutdown callback
4230 * @pdev: PCI device instance
4232 * Free up all memory and resources associated with the
4237 static void mpi3mr_shutdown(struct pci_dev *pdev)
4239 struct Scsi_Host *shost = pci_get_drvdata(pdev);
4240 struct mpi3mr_ioc *mrioc;
4241 struct workqueue_struct *wq;
4242 unsigned long flags;
4247 mrioc = shost_priv(shost);
4248 while (mrioc->reset_in_progress || mrioc->is_driver_loading)
4251 mrioc->stop_drv_processing = 1;
4252 mpi3mr_cleanup_fwevt_list(mrioc);
4253 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
4254 wq = mrioc->fwevt_worker_thread;
4255 mrioc->fwevt_worker_thread = NULL;
4256 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
4258 destroy_workqueue(wq);
4260 mpi3mr_stop_watchdog(mrioc);
4261 mpi3mr_cleanup_ioc(mrioc);
4262 mpi3mr_cleanup_resources(mrioc);
4267 * mpi3mr_suspend - PCI power management suspend callback
4268 * @pdev: PCI device instance
4269 * @state: New power state
4271 * Change the power state to the given value and cleanup the IOC
4272 * by issuing MUR and shutdown notification
4276 static int mpi3mr_suspend(struct pci_dev *pdev, pm_message_t state)
4278 struct Scsi_Host *shost = pci_get_drvdata(pdev);
4279 struct mpi3mr_ioc *mrioc;
4280 pci_power_t device_state;
4285 mrioc = shost_priv(shost);
4286 while (mrioc->reset_in_progress || mrioc->is_driver_loading)
4288 mrioc->stop_drv_processing = 1;
4289 mpi3mr_cleanup_fwevt_list(mrioc);
4290 scsi_block_requests(shost);
4291 mpi3mr_stop_watchdog(mrioc);
4292 mpi3mr_cleanup_ioc(mrioc);
4294 device_state = pci_choose_state(pdev, state);
4295 ioc_info(mrioc, "pdev=0x%p, slot=%s, entering operating state [D%d]\n",
4296 pdev, pci_name(pdev), device_state);
4297 pci_save_state(pdev);
4298 pci_set_power_state(pdev, device_state);
4299 mpi3mr_cleanup_resources(mrioc);
4305 * mpi3mr_resume - PCI power management resume callback
4306 * @pdev: PCI device instance
4308 * Restore the power state to D0 and reinitialize the controller
4309 * and resume I/O operations to the target devices
4311 * Return: 0 on success, non-zero on failure
4313 static int mpi3mr_resume(struct pci_dev *pdev)
4315 struct Scsi_Host *shost = pci_get_drvdata(pdev);
4316 struct mpi3mr_ioc *mrioc;
4317 pci_power_t device_state = pdev->current_state;
4323 mrioc = shost_priv(shost);
4325 ioc_info(mrioc, "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
4326 pdev, pci_name(pdev), device_state);
4327 pci_set_power_state(pdev, PCI_D0);
4328 pci_enable_wake(pdev, PCI_D0, 0);
4329 pci_restore_state(pdev);
4331 mrioc->cpu_count = num_online_cpus();
4332 r = mpi3mr_setup_resources(mrioc);
4334 ioc_info(mrioc, "%s: Setup resources failed[%d]\n",
4339 mrioc->stop_drv_processing = 0;
4340 mpi3mr_memset_buffers(mrioc);
4341 r = mpi3mr_reinit_ioc(mrioc, 1);
4343 ioc_err(mrioc, "resuming controller failed[%d]\n", r);
4346 scsi_unblock_requests(shost);
4347 mpi3mr_start_watchdog(mrioc);
4353 static const struct pci_device_id mpi3mr_pci_id_table[] = {
4355 PCI_DEVICE_SUB(MPI3_MFGPAGE_VENDORID_BROADCOM,
4356 MPI3_MFGPAGE_DEVID_SAS4116, PCI_ANY_ID, PCI_ANY_ID)
4360 MODULE_DEVICE_TABLE(pci, mpi3mr_pci_id_table);
4362 static struct pci_driver mpi3mr_pci_driver = {
4363 .name = MPI3MR_DRIVER_NAME,
4364 .id_table = mpi3mr_pci_id_table,
4365 .probe = mpi3mr_probe,
4366 .remove = mpi3mr_remove,
4367 .shutdown = mpi3mr_shutdown,
4369 .suspend = mpi3mr_suspend,
4370 .resume = mpi3mr_resume,
4374 static int __init mpi3mr_init(void)
4378 pr_info("Loading %s version %s\n", MPI3MR_DRIVER_NAME,
4379 MPI3MR_DRIVER_VERSION);
4381 ret_val = pci_register_driver(&mpi3mr_pci_driver);
4386 static void __exit mpi3mr_exit(void)
4388 if (warn_non_secure_ctlr)
4390 "Unloading %s version %s while managing a non secure controller\n",
4391 MPI3MR_DRIVER_NAME, MPI3MR_DRIVER_VERSION);
4393 pr_info("Unloading %s version %s\n", MPI3MR_DRIVER_NAME,
4394 MPI3MR_DRIVER_VERSION);
4396 pci_unregister_driver(&mpi3mr_pci_driver);
4399 module_init(mpi3mr_init);
4400 module_exit(mpi3mr_exit);