1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Broadcom MPI3 Storage Controllers
5 * Copyright (C) 2017-2022 Broadcom Inc.
12 /* global driver scop variables */
13 LIST_HEAD(mrioc_list);
14 DEFINE_SPINLOCK(mrioc_list_lock);
16 static int warn_non_secure_ctlr;
17 atomic64_t event_counter;
19 MODULE_AUTHOR(MPI3MR_DRIVER_AUTHOR);
20 MODULE_DESCRIPTION(MPI3MR_DRIVER_DESC);
21 MODULE_LICENSE(MPI3MR_DRIVER_LICENSE);
22 MODULE_VERSION(MPI3MR_DRIVER_VERSION);
24 /* Module parameters*/
26 module_param(prot_mask, int, 0);
27 MODULE_PARM_DESC(prot_mask, "Host protection capabilities mask, def=0x07");
29 static int prot_guard_mask = 3;
30 module_param(prot_guard_mask, int, 0);
31 MODULE_PARM_DESC(prot_guard_mask, " Host protection guard mask, def=3");
32 static int logging_level;
33 module_param(logging_level, int, 0);
34 MODULE_PARM_DESC(logging_level,
35 " bits for enabling additional logging info (default=0)");
37 /* Forward declarations*/
38 static void mpi3mr_send_event_ack(struct mpi3mr_ioc *mrioc, u8 event,
39 struct mpi3mr_drv_cmd *cmdparam, u32 event_ctx);
41 #define MPI3MR_DRIVER_EVENT_TG_QD_REDUCTION (0xFFFF)
44 * mpi3mr_host_tag_for_scmd - Get host tag for a scmd
45 * @mrioc: Adapter instance reference
46 * @scmd: SCSI command reference
48 * Calculate the host tag based on block tag for a given scmd.
50 * Return: Valid host tag or MPI3MR_HOSTTAG_INVALID.
52 static u16 mpi3mr_host_tag_for_scmd(struct mpi3mr_ioc *mrioc,
53 struct scsi_cmnd *scmd)
55 struct scmd_priv *priv = NULL;
57 u16 host_tag, hw_queue;
59 unique_tag = blk_mq_unique_tag(scsi_cmd_to_rq(scmd));
61 hw_queue = blk_mq_unique_tag_to_hwq(unique_tag);
62 if (hw_queue >= mrioc->num_op_reply_q)
63 return MPI3MR_HOSTTAG_INVALID;
64 host_tag = blk_mq_unique_tag_to_tag(unique_tag);
66 if (WARN_ON(host_tag >= mrioc->max_host_ios))
67 return MPI3MR_HOSTTAG_INVALID;
69 priv = scsi_cmd_priv(scmd);
70 /*host_tag 0 is invalid hence incrementing by 1*/
71 priv->host_tag = host_tag + 1;
73 priv->in_lld_scope = 1;
74 priv->req_q_idx = hw_queue;
75 priv->meta_chain_idx = -1;
77 priv->meta_sg_valid = 0;
78 return priv->host_tag;
82 * mpi3mr_scmd_from_host_tag - Get SCSI command from host tag
83 * @mrioc: Adapter instance reference
85 * @qidx: Operational queue index
87 * Identify the block tag from the host tag and queue index and
88 * retrieve associated scsi command using scsi_host_find_tag().
90 * Return: SCSI command reference or NULL.
92 static struct scsi_cmnd *mpi3mr_scmd_from_host_tag(
93 struct mpi3mr_ioc *mrioc, u16 host_tag, u16 qidx)
95 struct scsi_cmnd *scmd = NULL;
96 struct scmd_priv *priv = NULL;
97 u32 unique_tag = host_tag - 1;
99 if (WARN_ON(host_tag > mrioc->max_host_ios))
102 unique_tag |= (qidx << BLK_MQ_UNIQUE_TAG_BITS);
104 scmd = scsi_host_find_tag(mrioc->shost, unique_tag);
106 priv = scsi_cmd_priv(scmd);
107 if (!priv->in_lld_scope)
115 * mpi3mr_clear_scmd_priv - Cleanup SCSI command private date
116 * @mrioc: Adapter instance reference
117 * @scmd: SCSI command reference
119 * Invalidate the SCSI command private data to mark the command
120 * is not in LLD scope anymore.
124 static void mpi3mr_clear_scmd_priv(struct mpi3mr_ioc *mrioc,
125 struct scsi_cmnd *scmd)
127 struct scmd_priv *priv = NULL;
129 priv = scsi_cmd_priv(scmd);
131 if (WARN_ON(priv->in_lld_scope == 0))
133 priv->host_tag = MPI3MR_HOSTTAG_INVALID;
134 priv->req_q_idx = 0xFFFF;
136 priv->in_lld_scope = 0;
137 priv->meta_sg_valid = 0;
138 if (priv->chain_idx >= 0) {
139 clear_bit(priv->chain_idx, mrioc->chain_bitmap);
140 priv->chain_idx = -1;
142 if (priv->meta_chain_idx >= 0) {
143 clear_bit(priv->meta_chain_idx, mrioc->chain_bitmap);
144 priv->meta_chain_idx = -1;
148 static void mpi3mr_dev_rmhs_send_tm(struct mpi3mr_ioc *mrioc, u16 handle,
149 struct mpi3mr_drv_cmd *cmdparam, u8 iou_rc);
150 static void mpi3mr_fwevt_worker(struct work_struct *work);
153 * mpi3mr_fwevt_free - firmware event memory dealloctor
154 * @r: k reference pointer of the firmware event
156 * Free firmware event memory when no reference.
158 static void mpi3mr_fwevt_free(struct kref *r)
160 kfree(container_of(r, struct mpi3mr_fwevt, ref_count));
164 * mpi3mr_fwevt_get - k reference incrementor
165 * @fwevt: Firmware event reference
167 * Increment firmware event reference count.
169 static void mpi3mr_fwevt_get(struct mpi3mr_fwevt *fwevt)
171 kref_get(&fwevt->ref_count);
175 * mpi3mr_fwevt_put - k reference decrementor
176 * @fwevt: Firmware event reference
178 * decrement firmware event reference count.
180 static void mpi3mr_fwevt_put(struct mpi3mr_fwevt *fwevt)
182 kref_put(&fwevt->ref_count, mpi3mr_fwevt_free);
186 * mpi3mr_alloc_fwevt - Allocate firmware event
187 * @len: length of firmware event data to allocate
189 * Allocate firmware event with required length and initialize
190 * the reference counter.
192 * Return: firmware event reference.
194 static struct mpi3mr_fwevt *mpi3mr_alloc_fwevt(int len)
196 struct mpi3mr_fwevt *fwevt;
198 fwevt = kzalloc(sizeof(*fwevt) + len, GFP_ATOMIC);
202 kref_init(&fwevt->ref_count);
207 * mpi3mr_fwevt_add_to_list - Add firmware event to the list
208 * @mrioc: Adapter instance reference
209 * @fwevt: Firmware event reference
211 * Add the given firmware event to the firmware event list.
215 static void mpi3mr_fwevt_add_to_list(struct mpi3mr_ioc *mrioc,
216 struct mpi3mr_fwevt *fwevt)
220 if (!mrioc->fwevt_worker_thread)
223 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
224 /* get fwevt reference count while adding it to fwevt_list */
225 mpi3mr_fwevt_get(fwevt);
226 INIT_LIST_HEAD(&fwevt->list);
227 list_add_tail(&fwevt->list, &mrioc->fwevt_list);
228 INIT_WORK(&fwevt->work, mpi3mr_fwevt_worker);
229 /* get fwevt reference count while enqueueing it to worker queue */
230 mpi3mr_fwevt_get(fwevt);
231 queue_work(mrioc->fwevt_worker_thread, &fwevt->work);
232 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
236 * mpi3mr_fwevt_del_from_list - Delete firmware event from list
237 * @mrioc: Adapter instance reference
238 * @fwevt: Firmware event reference
240 * Delete the given firmware event from the firmware event list.
244 static void mpi3mr_fwevt_del_from_list(struct mpi3mr_ioc *mrioc,
245 struct mpi3mr_fwevt *fwevt)
249 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
250 if (!list_empty(&fwevt->list)) {
251 list_del_init(&fwevt->list);
253 * Put fwevt reference count after
254 * removing it from fwevt_list
256 mpi3mr_fwevt_put(fwevt);
258 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
262 * mpi3mr_dequeue_fwevt - Dequeue firmware event from the list
263 * @mrioc: Adapter instance reference
265 * Dequeue a firmware event from the firmware event list.
267 * Return: firmware event.
269 static struct mpi3mr_fwevt *mpi3mr_dequeue_fwevt(
270 struct mpi3mr_ioc *mrioc)
273 struct mpi3mr_fwevt *fwevt = NULL;
275 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
276 if (!list_empty(&mrioc->fwevt_list)) {
277 fwevt = list_first_entry(&mrioc->fwevt_list,
278 struct mpi3mr_fwevt, list);
279 list_del_init(&fwevt->list);
281 * Put fwevt reference count after
282 * removing it from fwevt_list
284 mpi3mr_fwevt_put(fwevt);
286 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
292 * mpi3mr_cancel_work - cancel firmware event
293 * @fwevt: fwevt object which needs to be canceled
297 static void mpi3mr_cancel_work(struct mpi3mr_fwevt *fwevt)
300 * Wait on the fwevt to complete. If this returns 1, then
301 * the event was never executed.
303 * If it did execute, we wait for it to finish, and the put will
304 * happen from mpi3mr_process_fwevt()
306 if (cancel_work_sync(&fwevt->work)) {
308 * Put fwevt reference count after
309 * dequeuing it from worker queue
311 mpi3mr_fwevt_put(fwevt);
313 * Put fwevt reference count to neutralize
314 * kref_init increment
316 mpi3mr_fwevt_put(fwevt);
321 * mpi3mr_cleanup_fwevt_list - Cleanup firmware event list
322 * @mrioc: Adapter instance reference
324 * Flush all pending firmware events from the firmware event
329 void mpi3mr_cleanup_fwevt_list(struct mpi3mr_ioc *mrioc)
331 struct mpi3mr_fwevt *fwevt = NULL;
333 if ((list_empty(&mrioc->fwevt_list) && !mrioc->current_event) ||
334 !mrioc->fwevt_worker_thread)
337 while ((fwevt = mpi3mr_dequeue_fwevt(mrioc)))
338 mpi3mr_cancel_work(fwevt);
340 if (mrioc->current_event) {
341 fwevt = mrioc->current_event;
343 * Don't call cancel_work_sync() API for the
344 * fwevt work if the controller reset is
345 * get called as part of processing the
346 * same fwevt work (or) when worker thread is
347 * waiting for device add/remove APIs to complete.
348 * Otherwise we will see deadlock.
350 if (current_work() == &fwevt->work || fwevt->pending_at_sml) {
355 mpi3mr_cancel_work(fwevt);
360 * mpi3mr_queue_qd_reduction_event - Queue TG QD reduction event
361 * @mrioc: Adapter instance reference
362 * @tg: Throttle group information pointer
364 * Accessor to queue on synthetically generated driver event to
365 * the event worker thread, the driver event will be used to
366 * reduce the QD of all VDs in the TG from the worker thread.
370 static void mpi3mr_queue_qd_reduction_event(struct mpi3mr_ioc *mrioc,
371 struct mpi3mr_throttle_group_info *tg)
373 struct mpi3mr_fwevt *fwevt;
374 u16 sz = sizeof(struct mpi3mr_throttle_group_info *);
377 * If the QD reduction event is already queued due to throttle and if
378 * the QD is not restored through device info change event
379 * then dont queue further reduction events
381 if (tg->fw_qd != tg->modified_qd)
384 fwevt = mpi3mr_alloc_fwevt(sz);
386 ioc_warn(mrioc, "failed to queue TG QD reduction event\n");
389 *(struct mpi3mr_throttle_group_info **)fwevt->event_data = tg;
390 fwevt->mrioc = mrioc;
391 fwevt->event_id = MPI3MR_DRIVER_EVENT_TG_QD_REDUCTION;
393 fwevt->process_evt = 1;
395 fwevt->event_data_size = sz;
396 tg->modified_qd = max_t(u16, (tg->fw_qd * tg->qd_reduction) / 10, 8);
398 dprint_event_bh(mrioc, "qd reduction event queued for tg_id(%d)\n",
400 mpi3mr_fwevt_add_to_list(mrioc, fwevt);
404 * mpi3mr_invalidate_devhandles -Invalidate device handles
405 * @mrioc: Adapter instance reference
407 * Invalidate the device handles in the target device structures
408 * . Called post reset prior to reinitializing the controller.
412 void mpi3mr_invalidate_devhandles(struct mpi3mr_ioc *mrioc)
414 struct mpi3mr_tgt_dev *tgtdev;
415 struct mpi3mr_stgt_priv_data *tgt_priv;
417 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
418 tgtdev->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
419 if (tgtdev->starget && tgtdev->starget->hostdata) {
420 tgt_priv = tgtdev->starget->hostdata;
421 tgt_priv->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
422 tgt_priv->io_throttle_enabled = 0;
423 tgt_priv->io_divert = 0;
424 tgt_priv->throttle_group = NULL;
430 * mpi3mr_print_scmd - print individual SCSI command
432 * @data: Adapter instance reference
434 * Print the SCSI command details if it is in LLD scope.
436 * Return: true always.
438 static bool mpi3mr_print_scmd(struct request *rq, void *data)
440 struct mpi3mr_ioc *mrioc = (struct mpi3mr_ioc *)data;
441 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
442 struct scmd_priv *priv = NULL;
445 priv = scsi_cmd_priv(scmd);
446 if (!priv->in_lld_scope)
449 ioc_info(mrioc, "%s :Host Tag = %d, qid = %d\n",
450 __func__, priv->host_tag, priv->req_q_idx + 1);
451 scsi_print_command(scmd);
459 * mpi3mr_flush_scmd - Flush individual SCSI command
461 * @data: Adapter instance reference
463 * Return the SCSI command to the upper layers if it is in LLD
466 * Return: true always.
469 static bool mpi3mr_flush_scmd(struct request *rq, void *data)
471 struct mpi3mr_ioc *mrioc = (struct mpi3mr_ioc *)data;
472 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
473 struct scmd_priv *priv = NULL;
476 priv = scsi_cmd_priv(scmd);
477 if (!priv->in_lld_scope)
480 if (priv->meta_sg_valid)
481 dma_unmap_sg(&mrioc->pdev->dev, scsi_prot_sglist(scmd),
482 scsi_prot_sg_count(scmd), scmd->sc_data_direction);
483 mpi3mr_clear_scmd_priv(mrioc, scmd);
484 scsi_dma_unmap(scmd);
485 scmd->result = DID_RESET << 16;
486 scsi_print_command(scmd);
488 mrioc->flush_io_count++;
496 * mpi3mr_count_dev_pending - Count commands pending for a lun
498 * @data: SCSI device reference
500 * This is an iterator function called for each SCSI command in
501 * a host and if the command is pending in the LLD for the
502 * specific device(lun) then device specific pending I/O counter
503 * is updated in the device structure.
505 * Return: true always.
508 static bool mpi3mr_count_dev_pending(struct request *rq, void *data)
510 struct scsi_device *sdev = (struct scsi_device *)data;
511 struct mpi3mr_sdev_priv_data *sdev_priv_data = sdev->hostdata;
512 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
513 struct scmd_priv *priv;
516 priv = scsi_cmd_priv(scmd);
517 if (!priv->in_lld_scope)
519 if (scmd->device == sdev)
520 sdev_priv_data->pend_count++;
528 * mpi3mr_count_tgt_pending - Count commands pending for target
530 * @data: SCSI target reference
532 * This is an iterator function called for each SCSI command in
533 * a host and if the command is pending in the LLD for the
534 * specific target then target specific pending I/O counter is
535 * updated in the target structure.
537 * Return: true always.
540 static bool mpi3mr_count_tgt_pending(struct request *rq, void *data)
542 struct scsi_target *starget = (struct scsi_target *)data;
543 struct mpi3mr_stgt_priv_data *stgt_priv_data = starget->hostdata;
544 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
545 struct scmd_priv *priv;
548 priv = scsi_cmd_priv(scmd);
549 if (!priv->in_lld_scope)
551 if (scmd->device && (scsi_target(scmd->device) == starget))
552 stgt_priv_data->pend_count++;
560 * mpi3mr_flush_host_io - Flush host I/Os
561 * @mrioc: Adapter instance reference
563 * Flush all of the pending I/Os by calling
564 * blk_mq_tagset_busy_iter() for each possible tag. This is
565 * executed post controller reset
569 void mpi3mr_flush_host_io(struct mpi3mr_ioc *mrioc)
571 struct Scsi_Host *shost = mrioc->shost;
573 mrioc->flush_io_count = 0;
574 ioc_info(mrioc, "%s :Flushing Host I/O cmds post reset\n", __func__);
575 blk_mq_tagset_busy_iter(&shost->tag_set,
576 mpi3mr_flush_scmd, (void *)mrioc);
577 ioc_info(mrioc, "%s :Flushed %d Host I/O cmds\n", __func__,
578 mrioc->flush_io_count);
582 * mpi3mr_alloc_tgtdev - target device allocator
584 * Allocate target device instance and initialize the reference
587 * Return: target device instance.
589 static struct mpi3mr_tgt_dev *mpi3mr_alloc_tgtdev(void)
591 struct mpi3mr_tgt_dev *tgtdev;
593 tgtdev = kzalloc(sizeof(*tgtdev), GFP_ATOMIC);
596 kref_init(&tgtdev->ref_count);
601 * mpi3mr_tgtdev_add_to_list -Add tgtdevice to the list
602 * @mrioc: Adapter instance reference
603 * @tgtdev: Target device
605 * Add the target device to the target device list
609 static void mpi3mr_tgtdev_add_to_list(struct mpi3mr_ioc *mrioc,
610 struct mpi3mr_tgt_dev *tgtdev)
614 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
615 mpi3mr_tgtdev_get(tgtdev);
616 INIT_LIST_HEAD(&tgtdev->list);
617 list_add_tail(&tgtdev->list, &mrioc->tgtdev_list);
618 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
622 * mpi3mr_tgtdev_del_from_list -Delete tgtdevice from the list
623 * @mrioc: Adapter instance reference
624 * @tgtdev: Target device
626 * Remove the target device from the target device list
630 static void mpi3mr_tgtdev_del_from_list(struct mpi3mr_ioc *mrioc,
631 struct mpi3mr_tgt_dev *tgtdev)
635 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
636 if (!list_empty(&tgtdev->list)) {
637 list_del_init(&tgtdev->list);
638 mpi3mr_tgtdev_put(tgtdev);
640 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
644 * __mpi3mr_get_tgtdev_by_handle -Get tgtdev from device handle
645 * @mrioc: Adapter instance reference
646 * @handle: Device handle
648 * Accessor to retrieve target device from the device handle.
651 * Return: Target device reference.
653 static struct mpi3mr_tgt_dev *__mpi3mr_get_tgtdev_by_handle(
654 struct mpi3mr_ioc *mrioc, u16 handle)
656 struct mpi3mr_tgt_dev *tgtdev;
658 assert_spin_locked(&mrioc->tgtdev_lock);
659 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list)
660 if (tgtdev->dev_handle == handle)
665 mpi3mr_tgtdev_get(tgtdev);
670 * mpi3mr_get_tgtdev_by_handle -Get tgtdev from device handle
671 * @mrioc: Adapter instance reference
672 * @handle: Device handle
674 * Accessor to retrieve target device from the device handle.
677 * Return: Target device reference.
679 struct mpi3mr_tgt_dev *mpi3mr_get_tgtdev_by_handle(
680 struct mpi3mr_ioc *mrioc, u16 handle)
682 struct mpi3mr_tgt_dev *tgtdev;
685 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
686 tgtdev = __mpi3mr_get_tgtdev_by_handle(mrioc, handle);
687 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
692 * __mpi3mr_get_tgtdev_by_perst_id -Get tgtdev from persist ID
693 * @mrioc: Adapter instance reference
694 * @persist_id: Persistent ID
696 * Accessor to retrieve target device from the Persistent ID.
699 * Return: Target device reference.
701 static struct mpi3mr_tgt_dev *__mpi3mr_get_tgtdev_by_perst_id(
702 struct mpi3mr_ioc *mrioc, u16 persist_id)
704 struct mpi3mr_tgt_dev *tgtdev;
706 assert_spin_locked(&mrioc->tgtdev_lock);
707 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list)
708 if (tgtdev->perst_id == persist_id)
713 mpi3mr_tgtdev_get(tgtdev);
718 * mpi3mr_get_tgtdev_by_perst_id -Get tgtdev from persistent ID
719 * @mrioc: Adapter instance reference
720 * @persist_id: Persistent ID
722 * Accessor to retrieve target device from the Persistent ID.
725 * Return: Target device reference.
727 static struct mpi3mr_tgt_dev *mpi3mr_get_tgtdev_by_perst_id(
728 struct mpi3mr_ioc *mrioc, u16 persist_id)
730 struct mpi3mr_tgt_dev *tgtdev;
733 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
734 tgtdev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, persist_id);
735 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
740 * __mpi3mr_get_tgtdev_from_tgtpriv -Get tgtdev from tgt private
741 * @mrioc: Adapter instance reference
742 * @tgt_priv: Target private data
744 * Accessor to return target device from the target private
745 * data. Non Lock version
747 * Return: Target device reference.
749 static struct mpi3mr_tgt_dev *__mpi3mr_get_tgtdev_from_tgtpriv(
750 struct mpi3mr_ioc *mrioc, struct mpi3mr_stgt_priv_data *tgt_priv)
752 struct mpi3mr_tgt_dev *tgtdev;
754 assert_spin_locked(&mrioc->tgtdev_lock);
755 tgtdev = tgt_priv->tgt_dev;
757 mpi3mr_tgtdev_get(tgtdev);
762 * mpi3mr_set_io_divert_for_all_vd_in_tg -set divert for TG VDs
763 * @mrioc: Adapter instance reference
764 * @tg: Throttle group information pointer
765 * @divert_value: 1 or 0
767 * Accessor to set io_divert flag for each device associated
768 * with the given throttle group with the given value.
772 static void mpi3mr_set_io_divert_for_all_vd_in_tg(struct mpi3mr_ioc *mrioc,
773 struct mpi3mr_throttle_group_info *tg, u8 divert_value)
776 struct mpi3mr_tgt_dev *tgtdev;
777 struct mpi3mr_stgt_priv_data *tgt_priv;
779 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
780 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
781 if (tgtdev->starget && tgtdev->starget->hostdata) {
782 tgt_priv = tgtdev->starget->hostdata;
783 if (tgt_priv->throttle_group == tg)
784 tgt_priv->io_divert = divert_value;
787 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
791 * mpi3mr_print_device_event_notice - print notice related to post processing of
792 * device event after controller reset.
794 * @mrioc: Adapter instance reference
795 * @device_add: true for device add event and false for device removal event
799 static void mpi3mr_print_device_event_notice(struct mpi3mr_ioc *mrioc,
802 ioc_notice(mrioc, "Device %s was in progress before the reset and\n",
803 (device_add ? "addition" : "removal"));
804 ioc_notice(mrioc, "completed after reset, verify whether the exposed devices\n");
805 ioc_notice(mrioc, "are matched with attached devices for correctness\n");
809 * mpi3mr_remove_tgtdev_from_host - Remove dev from upper layers
810 * @mrioc: Adapter instance reference
811 * @tgtdev: Target device structure
813 * Checks whether the device is exposed to upper layers and if it
814 * is then remove the device from upper layers by calling
815 * scsi_remove_target().
817 * Return: 0 on success, non zero on failure.
819 static void mpi3mr_remove_tgtdev_from_host(struct mpi3mr_ioc *mrioc,
820 struct mpi3mr_tgt_dev *tgtdev)
822 struct mpi3mr_stgt_priv_data *tgt_priv;
824 ioc_info(mrioc, "%s :Removing handle(0x%04x), wwid(0x%016llx)\n",
825 __func__, tgtdev->dev_handle, (unsigned long long)tgtdev->wwid);
826 if (tgtdev->starget && tgtdev->starget->hostdata) {
827 tgt_priv = tgtdev->starget->hostdata;
828 tgt_priv->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
831 if (tgtdev->starget) {
832 if (mrioc->current_event)
833 mrioc->current_event->pending_at_sml = 1;
834 scsi_remove_target(&tgtdev->starget->dev);
835 tgtdev->host_exposed = 0;
836 if (mrioc->current_event) {
837 mrioc->current_event->pending_at_sml = 0;
838 if (mrioc->current_event->discard) {
839 mpi3mr_print_device_event_notice(mrioc, false);
844 ioc_info(mrioc, "%s :Removed handle(0x%04x), wwid(0x%016llx)\n",
845 __func__, tgtdev->dev_handle, (unsigned long long)tgtdev->wwid);
849 * mpi3mr_report_tgtdev_to_host - Expose device to upper layers
850 * @mrioc: Adapter instance reference
851 * @perst_id: Persistent ID of the device
853 * Checks whether the device can be exposed to upper layers and
854 * if it is not then expose the device to upper layers by
855 * calling scsi_scan_target().
857 * Return: 0 on success, non zero on failure.
859 static int mpi3mr_report_tgtdev_to_host(struct mpi3mr_ioc *mrioc,
863 struct mpi3mr_tgt_dev *tgtdev;
865 tgtdev = mpi3mr_get_tgtdev_by_perst_id(mrioc, perst_id);
870 if (tgtdev->is_hidden) {
874 if (!tgtdev->host_exposed && !mrioc->reset_in_progress) {
875 tgtdev->host_exposed = 1;
876 if (mrioc->current_event)
877 mrioc->current_event->pending_at_sml = 1;
878 scsi_scan_target(&mrioc->shost->shost_gendev, 0,
880 SCAN_WILD_CARD, SCSI_SCAN_INITIAL);
881 if (!tgtdev->starget)
882 tgtdev->host_exposed = 0;
883 if (mrioc->current_event) {
884 mrioc->current_event->pending_at_sml = 0;
885 if (mrioc->current_event->discard) {
886 mpi3mr_print_device_event_notice(mrioc, true);
893 mpi3mr_tgtdev_put(tgtdev);
899 * mpi3mr_change_queue_depth- Change QD callback handler
900 * @sdev: SCSI device reference
901 * @q_depth: Queue depth
903 * Validate and limit QD and call scsi_change_queue_depth.
905 * Return: return value of scsi_change_queue_depth
907 static int mpi3mr_change_queue_depth(struct scsi_device *sdev,
910 struct scsi_target *starget = scsi_target(sdev);
911 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
914 if (!sdev->tagged_supported)
916 if (q_depth > shost->can_queue)
917 q_depth = shost->can_queue;
919 q_depth = MPI3MR_DEFAULT_SDEV_QD;
920 retval = scsi_change_queue_depth(sdev, q_depth);
921 sdev->max_queue_depth = sdev->queue_depth;
927 * mpi3mr_update_sdev - Update SCSI device information
928 * @sdev: SCSI device reference
929 * @data: target device reference
931 * This is an iterator function called for each SCSI device in a
932 * target to update the target specific information into each
938 mpi3mr_update_sdev(struct scsi_device *sdev, void *data)
940 struct mpi3mr_tgt_dev *tgtdev;
942 tgtdev = (struct mpi3mr_tgt_dev *)data;
946 mpi3mr_change_queue_depth(sdev, tgtdev->q_depth);
947 switch (tgtdev->dev_type) {
948 case MPI3_DEVICE_DEVFORM_PCIE:
949 /*The block layer hw sector size = 512*/
950 if ((tgtdev->dev_spec.pcie_inf.dev_info &
951 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) ==
952 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) {
953 blk_queue_max_hw_sectors(sdev->request_queue,
954 tgtdev->dev_spec.pcie_inf.mdts / 512);
955 if (tgtdev->dev_spec.pcie_inf.pgsz == 0)
956 blk_queue_virt_boundary(sdev->request_queue,
957 ((1 << MPI3MR_DEFAULT_PGSZEXP) - 1));
959 blk_queue_virt_boundary(sdev->request_queue,
960 ((1 << tgtdev->dev_spec.pcie_inf.pgsz) - 1));
969 * mpi3mr_rfresh_tgtdevs - Refresh target device exposure
970 * @mrioc: Adapter instance reference
972 * This is executed post controller reset to identify any
973 * missing devices during reset and remove from the upper layers
974 * or expose any newly detected device to the upper layers.
979 void mpi3mr_rfresh_tgtdevs(struct mpi3mr_ioc *mrioc)
981 struct mpi3mr_tgt_dev *tgtdev, *tgtdev_next;
983 list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
985 if (tgtdev->dev_handle == MPI3MR_INVALID_DEV_HANDLE) {
986 dprint_reset(mrioc, "removing target device with perst_id(%d)\n",
988 if (tgtdev->host_exposed)
989 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
990 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev);
991 mpi3mr_tgtdev_put(tgtdev);
996 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
997 if ((tgtdev->dev_handle != MPI3MR_INVALID_DEV_HANDLE) &&
998 !tgtdev->is_hidden && !tgtdev->host_exposed)
999 mpi3mr_report_tgtdev_to_host(mrioc, tgtdev->perst_id);
1004 * mpi3mr_update_tgtdev - DevStatusChange evt bottomhalf
1005 * @mrioc: Adapter instance reference
1006 * @tgtdev: Target device internal structure
1007 * @dev_pg0: New device page0
1008 * @is_added: Flag to indicate the device is just added
1010 * Update the information from the device page0 into the driver
1011 * cached target device structure.
1015 static void mpi3mr_update_tgtdev(struct mpi3mr_ioc *mrioc,
1016 struct mpi3mr_tgt_dev *tgtdev, struct mpi3_device_page0 *dev_pg0,
1020 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
1023 tgtdev->perst_id = le16_to_cpu(dev_pg0->persistent_id);
1024 tgtdev->dev_handle = le16_to_cpu(dev_pg0->dev_handle);
1025 tgtdev->dev_type = dev_pg0->device_form;
1026 tgtdev->encl_handle = le16_to_cpu(dev_pg0->enclosure_handle);
1027 tgtdev->parent_handle = le16_to_cpu(dev_pg0->parent_dev_handle);
1028 tgtdev->slot = le16_to_cpu(dev_pg0->slot);
1029 tgtdev->q_depth = le16_to_cpu(dev_pg0->queue_depth);
1030 tgtdev->wwid = le64_to_cpu(dev_pg0->wwid);
1032 flags = le16_to_cpu(dev_pg0->flags);
1033 tgtdev->is_hidden = (flags & MPI3_DEVICE0_FLAGS_HIDDEN);
1035 if (is_added == true)
1036 tgtdev->io_throttle_enabled =
1037 (flags & MPI3_DEVICE0_FLAGS_IO_THROTTLING_REQUIRED) ? 1 : 0;
1040 if (tgtdev->starget && tgtdev->starget->hostdata) {
1041 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
1042 tgtdev->starget->hostdata;
1043 scsi_tgt_priv_data->perst_id = tgtdev->perst_id;
1044 scsi_tgt_priv_data->dev_handle = tgtdev->dev_handle;
1045 scsi_tgt_priv_data->dev_type = tgtdev->dev_type;
1046 scsi_tgt_priv_data->io_throttle_enabled =
1047 tgtdev->io_throttle_enabled;
1050 switch (dev_pg0->access_status) {
1051 case MPI3_DEVICE0_ASTATUS_NO_ERRORS:
1052 case MPI3_DEVICE0_ASTATUS_PREPARE:
1053 case MPI3_DEVICE0_ASTATUS_NEEDS_INITIALIZATION:
1054 case MPI3_DEVICE0_ASTATUS_DEVICE_MISSING_DELAY:
1057 tgtdev->is_hidden = 1;
1061 switch (tgtdev->dev_type) {
1062 case MPI3_DEVICE_DEVFORM_SAS_SATA:
1064 struct mpi3_device0_sas_sata_format *sasinf =
1065 &dev_pg0->device_specific.sas_sata_format;
1066 u16 dev_info = le16_to_cpu(sasinf->device_info);
1068 tgtdev->dev_spec.sas_sata_inf.dev_info = dev_info;
1069 tgtdev->dev_spec.sas_sata_inf.sas_address =
1070 le64_to_cpu(sasinf->sas_address);
1071 if ((dev_info & MPI3_SAS_DEVICE_INFO_DEVICE_TYPE_MASK) !=
1072 MPI3_SAS_DEVICE_INFO_DEVICE_TYPE_END_DEVICE)
1073 tgtdev->is_hidden = 1;
1074 else if (!(dev_info & (MPI3_SAS_DEVICE_INFO_STP_SATA_TARGET |
1075 MPI3_SAS_DEVICE_INFO_SSP_TARGET)))
1076 tgtdev->is_hidden = 1;
1079 case MPI3_DEVICE_DEVFORM_PCIE:
1081 struct mpi3_device0_pcie_format *pcieinf =
1082 &dev_pg0->device_specific.pcie_format;
1083 u16 dev_info = le16_to_cpu(pcieinf->device_info);
1085 tgtdev->dev_spec.pcie_inf.dev_info = dev_info;
1086 tgtdev->dev_spec.pcie_inf.capb =
1087 le32_to_cpu(pcieinf->capabilities);
1088 tgtdev->dev_spec.pcie_inf.mdts = MPI3MR_DEFAULT_MDTS;
1090 tgtdev->dev_spec.pcie_inf.pgsz = 12;
1091 if (dev_pg0->access_status == MPI3_DEVICE0_ASTATUS_NO_ERRORS) {
1092 tgtdev->dev_spec.pcie_inf.mdts =
1093 le32_to_cpu(pcieinf->maximum_data_transfer_size);
1094 tgtdev->dev_spec.pcie_inf.pgsz = pcieinf->page_size;
1095 tgtdev->dev_spec.pcie_inf.reset_to =
1096 max_t(u8, pcieinf->controller_reset_to,
1097 MPI3MR_INTADMCMD_TIMEOUT);
1098 tgtdev->dev_spec.pcie_inf.abort_to =
1099 max_t(u8, pcieinf->nvme_abort_to,
1100 MPI3MR_INTADMCMD_TIMEOUT);
1102 if (tgtdev->dev_spec.pcie_inf.mdts > (1024 * 1024))
1103 tgtdev->dev_spec.pcie_inf.mdts = (1024 * 1024);
1104 if (((dev_info & MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) !=
1105 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) &&
1106 ((dev_info & MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) !=
1107 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_SCSI_DEVICE))
1108 tgtdev->is_hidden = 1;
1111 prot_mask = scsi_host_get_prot(mrioc->shost);
1112 if (prot_mask & SHOST_DIX_TYPE0_PROTECTION) {
1113 scsi_host_set_prot(mrioc->shost, prot_mask & 0x77);
1115 "%s : Disabling DIX0 prot capability\n", __func__);
1117 "because HBA does not support DIX0 operation on NVME drives\n");
1121 case MPI3_DEVICE_DEVFORM_VD:
1123 struct mpi3_device0_vd_format *vdinf =
1124 &dev_pg0->device_specific.vd_format;
1125 struct mpi3mr_throttle_group_info *tg = NULL;
1126 u16 vdinf_io_throttle_group =
1127 le16_to_cpu(vdinf->io_throttle_group);
1129 tgtdev->dev_spec.vd_inf.state = vdinf->vd_state;
1130 if (vdinf->vd_state == MPI3_DEVICE0_VD_STATE_OFFLINE)
1131 tgtdev->is_hidden = 1;
1132 tgtdev->dev_spec.vd_inf.tg_id = vdinf_io_throttle_group;
1133 tgtdev->dev_spec.vd_inf.tg_high =
1134 le16_to_cpu(vdinf->io_throttle_group_high) * 2048;
1135 tgtdev->dev_spec.vd_inf.tg_low =
1136 le16_to_cpu(vdinf->io_throttle_group_low) * 2048;
1137 if (vdinf_io_throttle_group < mrioc->num_io_throttle_group) {
1138 tg = mrioc->throttle_groups + vdinf_io_throttle_group;
1139 tg->id = vdinf_io_throttle_group;
1140 tg->high = tgtdev->dev_spec.vd_inf.tg_high;
1141 tg->low = tgtdev->dev_spec.vd_inf.tg_low;
1143 tgtdev->dev_spec.vd_inf.tg_qd_reduction;
1144 if (is_added == true)
1145 tg->fw_qd = tgtdev->q_depth;
1146 tg->modified_qd = tgtdev->q_depth;
1148 tgtdev->dev_spec.vd_inf.tg = tg;
1149 if (scsi_tgt_priv_data)
1150 scsi_tgt_priv_data->throttle_group = tg;
1159 * mpi3mr_devstatuschg_evt_bh - DevStatusChange evt bottomhalf
1160 * @mrioc: Adapter instance reference
1161 * @fwevt: Firmware event information.
1163 * Process Device status Change event and based on device's new
1164 * information, either expose the device to the upper layers, or
1165 * remove the device from upper layers.
1169 static void mpi3mr_devstatuschg_evt_bh(struct mpi3mr_ioc *mrioc,
1170 struct mpi3mr_fwevt *fwevt)
1173 u8 uhide = 0, delete = 0, cleanup = 0;
1174 struct mpi3mr_tgt_dev *tgtdev = NULL;
1175 struct mpi3_event_data_device_status_change *evtdata =
1176 (struct mpi3_event_data_device_status_change *)fwevt->event_data;
1178 dev_handle = le16_to_cpu(evtdata->dev_handle);
1180 "%s :device status change: handle(0x%04x): reason code(0x%x)\n",
1181 __func__, dev_handle, evtdata->reason_code);
1182 switch (evtdata->reason_code) {
1183 case MPI3_EVENT_DEV_STAT_RC_HIDDEN:
1186 case MPI3_EVENT_DEV_STAT_RC_NOT_HIDDEN:
1189 case MPI3_EVENT_DEV_STAT_RC_VD_NOT_RESPONDING:
1194 ioc_info(mrioc, "%s :Unhandled reason code(0x%x)\n", __func__,
1195 evtdata->reason_code);
1199 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
1203 tgtdev->is_hidden = 0;
1204 if (!tgtdev->host_exposed)
1205 mpi3mr_report_tgtdev_to_host(mrioc, tgtdev->perst_id);
1207 if (tgtdev->starget && tgtdev->starget->hostdata) {
1209 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1212 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev);
1213 mpi3mr_tgtdev_put(tgtdev);
1218 mpi3mr_tgtdev_put(tgtdev);
1222 * mpi3mr_devinfochg_evt_bh - DeviceInfoChange evt bottomhalf
1223 * @mrioc: Adapter instance reference
1224 * @dev_pg0: New device page0
1226 * Process Device Info Change event and based on device's new
1227 * information, either expose the device to the upper layers, or
1228 * remove the device from upper layers or update the details of
1233 static void mpi3mr_devinfochg_evt_bh(struct mpi3mr_ioc *mrioc,
1234 struct mpi3_device_page0 *dev_pg0)
1236 struct mpi3mr_tgt_dev *tgtdev = NULL;
1237 u16 dev_handle = 0, perst_id = 0;
1239 perst_id = le16_to_cpu(dev_pg0->persistent_id);
1240 dev_handle = le16_to_cpu(dev_pg0->dev_handle);
1242 "%s :Device info change: handle(0x%04x): persist_id(0x%x)\n",
1243 __func__, dev_handle, perst_id);
1244 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
1247 mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0, false);
1248 if (!tgtdev->is_hidden && !tgtdev->host_exposed)
1249 mpi3mr_report_tgtdev_to_host(mrioc, perst_id);
1250 if (tgtdev->is_hidden && tgtdev->host_exposed)
1251 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1252 if (!tgtdev->is_hidden && tgtdev->host_exposed && tgtdev->starget)
1253 starget_for_each_device(tgtdev->starget, (void *)tgtdev,
1254 mpi3mr_update_sdev);
1257 mpi3mr_tgtdev_put(tgtdev);
1261 * mpi3mr_sastopochg_evt_debug - SASTopoChange details
1262 * @mrioc: Adapter instance reference
1263 * @event_data: SAS topology change list event data
1265 * Prints information about the SAS topology change event.
1270 mpi3mr_sastopochg_evt_debug(struct mpi3mr_ioc *mrioc,
1271 struct mpi3_event_data_sas_topology_change_list *event_data)
1275 u8 reason_code, phy_number;
1276 char *status_str = NULL;
1277 u8 link_rate, prev_link_rate;
1279 switch (event_data->exp_status) {
1280 case MPI3_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
1281 status_str = "remove";
1283 case MPI3_EVENT_SAS_TOPO_ES_RESPONDING:
1284 status_str = "responding";
1286 case MPI3_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
1287 status_str = "remove delay";
1289 case MPI3_EVENT_SAS_TOPO_ES_NO_EXPANDER:
1290 status_str = "direct attached";
1293 status_str = "unknown status";
1296 ioc_info(mrioc, "%s :sas topology change: (%s)\n",
1297 __func__, status_str);
1299 "%s :\texpander_handle(0x%04x), enclosure_handle(0x%04x) start_phy(%02d), num_entries(%d)\n",
1300 __func__, le16_to_cpu(event_data->expander_dev_handle),
1301 le16_to_cpu(event_data->enclosure_handle),
1302 event_data->start_phy_num, event_data->num_entries);
1303 for (i = 0; i < event_data->num_entries; i++) {
1304 handle = le16_to_cpu(event_data->phy_entry[i].attached_dev_handle);
1307 phy_number = event_data->start_phy_num + i;
1308 reason_code = event_data->phy_entry[i].status &
1309 MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
1310 switch (reason_code) {
1311 case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
1312 status_str = "target remove";
1314 case MPI3_EVENT_SAS_TOPO_PHY_RC_DELAY_NOT_RESPONDING:
1315 status_str = "delay target remove";
1317 case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
1318 status_str = "link status change";
1320 case MPI3_EVENT_SAS_TOPO_PHY_RC_NO_CHANGE:
1321 status_str = "link status no change";
1323 case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
1324 status_str = "target responding";
1327 status_str = "unknown";
1330 link_rate = event_data->phy_entry[i].link_rate >> 4;
1331 prev_link_rate = event_data->phy_entry[i].link_rate & 0xF;
1333 "%s :\tphy(%02d), attached_handle(0x%04x): %s: link rate: new(0x%02x), old(0x%02x)\n",
1334 __func__, phy_number, handle, status_str, link_rate,
1340 * mpi3mr_sastopochg_evt_bh - SASTopologyChange evt bottomhalf
1341 * @mrioc: Adapter instance reference
1342 * @fwevt: Firmware event reference
1344 * Prints information about the SAS topology change event and
1345 * for "not responding" event code, removes the device from the
1350 static void mpi3mr_sastopochg_evt_bh(struct mpi3mr_ioc *mrioc,
1351 struct mpi3mr_fwevt *fwevt)
1353 struct mpi3_event_data_sas_topology_change_list *event_data =
1354 (struct mpi3_event_data_sas_topology_change_list *)fwevt->event_data;
1358 struct mpi3mr_tgt_dev *tgtdev = NULL;
1360 mpi3mr_sastopochg_evt_debug(mrioc, event_data);
1362 for (i = 0; i < event_data->num_entries; i++) {
1365 handle = le16_to_cpu(event_data->phy_entry[i].attached_dev_handle);
1368 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
1372 reason_code = event_data->phy_entry[i].status &
1373 MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
1375 switch (reason_code) {
1376 case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
1377 if (tgtdev->host_exposed)
1378 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1379 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev);
1380 mpi3mr_tgtdev_put(tgtdev);
1386 mpi3mr_tgtdev_put(tgtdev);
1391 * mpi3mr_pcietopochg_evt_debug - PCIeTopoChange details
1392 * @mrioc: Adapter instance reference
1393 * @event_data: PCIe topology change list event data
1395 * Prints information about the PCIe topology change event.
1400 mpi3mr_pcietopochg_evt_debug(struct mpi3mr_ioc *mrioc,
1401 struct mpi3_event_data_pcie_topology_change_list *event_data)
1407 char *status_str = NULL;
1408 u8 link_rate, prev_link_rate;
1410 switch (event_data->switch_status) {
1411 case MPI3_EVENT_PCIE_TOPO_SS_NOT_RESPONDING:
1412 status_str = "remove";
1414 case MPI3_EVENT_PCIE_TOPO_SS_RESPONDING:
1415 status_str = "responding";
1417 case MPI3_EVENT_PCIE_TOPO_SS_DELAY_NOT_RESPONDING:
1418 status_str = "remove delay";
1420 case MPI3_EVENT_PCIE_TOPO_SS_NO_PCIE_SWITCH:
1421 status_str = "direct attached";
1424 status_str = "unknown status";
1427 ioc_info(mrioc, "%s :pcie topology change: (%s)\n",
1428 __func__, status_str);
1430 "%s :\tswitch_handle(0x%04x), enclosure_handle(0x%04x) start_port(%02d), num_entries(%d)\n",
1431 __func__, le16_to_cpu(event_data->switch_dev_handle),
1432 le16_to_cpu(event_data->enclosure_handle),
1433 event_data->start_port_num, event_data->num_entries);
1434 for (i = 0; i < event_data->num_entries; i++) {
1436 le16_to_cpu(event_data->port_entry[i].attached_dev_handle);
1439 port_number = event_data->start_port_num + i;
1440 reason_code = event_data->port_entry[i].port_status;
1441 switch (reason_code) {
1442 case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
1443 status_str = "target remove";
1445 case MPI3_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
1446 status_str = "delay target remove";
1448 case MPI3_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
1449 status_str = "link status change";
1451 case MPI3_EVENT_PCIE_TOPO_PS_NO_CHANGE:
1452 status_str = "link status no change";
1454 case MPI3_EVENT_PCIE_TOPO_PS_RESPONDING:
1455 status_str = "target responding";
1458 status_str = "unknown";
1461 link_rate = event_data->port_entry[i].current_port_info &
1462 MPI3_EVENT_PCIE_TOPO_PI_RATE_MASK;
1463 prev_link_rate = event_data->port_entry[i].previous_port_info &
1464 MPI3_EVENT_PCIE_TOPO_PI_RATE_MASK;
1466 "%s :\tport(%02d), attached_handle(0x%04x): %s: link rate: new(0x%02x), old(0x%02x)\n",
1467 __func__, port_number, handle, status_str, link_rate,
1473 * mpi3mr_pcietopochg_evt_bh - PCIeTopologyChange evt bottomhalf
1474 * @mrioc: Adapter instance reference
1475 * @fwevt: Firmware event reference
1477 * Prints information about the PCIe topology change event and
1478 * for "not responding" event code, removes the device from the
1483 static void mpi3mr_pcietopochg_evt_bh(struct mpi3mr_ioc *mrioc,
1484 struct mpi3mr_fwevt *fwevt)
1486 struct mpi3_event_data_pcie_topology_change_list *event_data =
1487 (struct mpi3_event_data_pcie_topology_change_list *)fwevt->event_data;
1491 struct mpi3mr_tgt_dev *tgtdev = NULL;
1493 mpi3mr_pcietopochg_evt_debug(mrioc, event_data);
1495 for (i = 0; i < event_data->num_entries; i++) {
1499 le16_to_cpu(event_data->port_entry[i].attached_dev_handle);
1502 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
1506 reason_code = event_data->port_entry[i].port_status;
1508 switch (reason_code) {
1509 case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
1510 if (tgtdev->host_exposed)
1511 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1512 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev);
1513 mpi3mr_tgtdev_put(tgtdev);
1519 mpi3mr_tgtdev_put(tgtdev);
1524 * mpi3mr_logdata_evt_bh - Log data event bottomhalf
1525 * @mrioc: Adapter instance reference
1526 * @fwevt: Firmware event reference
1528 * Extracts the event data and calls application interfacing
1529 * function to process the event further.
1533 static void mpi3mr_logdata_evt_bh(struct mpi3mr_ioc *mrioc,
1534 struct mpi3mr_fwevt *fwevt)
1536 mpi3mr_app_save_logdata(mrioc, fwevt->event_data,
1537 fwevt->event_data_size);
1541 * mpi3mr_update_sdev_qd - Update SCSI device queue depath
1542 * @sdev: SCSI device reference
1543 * @data: Queue depth reference
1545 * This is an iterator function called for each SCSI device in a
1546 * target to update the QD of each SCSI device.
1550 static void mpi3mr_update_sdev_qd(struct scsi_device *sdev, void *data)
1552 u16 *q_depth = (u16 *)data;
1554 scsi_change_queue_depth(sdev, (int)*q_depth);
1555 sdev->max_queue_depth = sdev->queue_depth;
1559 * mpi3mr_set_qd_for_all_vd_in_tg -set QD for TG VDs
1560 * @mrioc: Adapter instance reference
1561 * @tg: Throttle group information pointer
1563 * Accessor to reduce QD for each device associated with the
1564 * given throttle group.
1568 static void mpi3mr_set_qd_for_all_vd_in_tg(struct mpi3mr_ioc *mrioc,
1569 struct mpi3mr_throttle_group_info *tg)
1571 unsigned long flags;
1572 struct mpi3mr_tgt_dev *tgtdev;
1573 struct mpi3mr_stgt_priv_data *tgt_priv;
1576 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
1577 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
1578 if (tgtdev->starget && tgtdev->starget->hostdata) {
1579 tgt_priv = tgtdev->starget->hostdata;
1580 if (tgt_priv->throttle_group == tg) {
1581 dprint_event_bh(mrioc,
1582 "updating qd due to throttling for persist_id(%d) original_qd(%d), reduced_qd (%d)\n",
1583 tgt_priv->perst_id, tgtdev->q_depth,
1585 starget_for_each_device(tgtdev->starget,
1586 (void *)&tg->modified_qd,
1587 mpi3mr_update_sdev_qd);
1591 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
1595 * mpi3mr_fwevt_bh - Firmware event bottomhalf handler
1596 * @mrioc: Adapter instance reference
1597 * @fwevt: Firmware event reference
1599 * Identifies the firmware event and calls corresponding bottomg
1600 * half handler and sends event acknowledgment if required.
1604 static void mpi3mr_fwevt_bh(struct mpi3mr_ioc *mrioc,
1605 struct mpi3mr_fwevt *fwevt)
1607 mpi3mr_fwevt_del_from_list(mrioc, fwevt);
1608 mrioc->current_event = fwevt;
1610 if (mrioc->stop_drv_processing)
1613 if (!fwevt->process_evt)
1616 switch (fwevt->event_id) {
1617 case MPI3_EVENT_DEVICE_ADDED:
1619 struct mpi3_device_page0 *dev_pg0 =
1620 (struct mpi3_device_page0 *)fwevt->event_data;
1621 mpi3mr_report_tgtdev_to_host(mrioc,
1622 le16_to_cpu(dev_pg0->persistent_id));
1625 case MPI3_EVENT_DEVICE_INFO_CHANGED:
1627 mpi3mr_devinfochg_evt_bh(mrioc,
1628 (struct mpi3_device_page0 *)fwevt->event_data);
1631 case MPI3_EVENT_DEVICE_STATUS_CHANGE:
1633 mpi3mr_devstatuschg_evt_bh(mrioc, fwevt);
1636 case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
1638 mpi3mr_sastopochg_evt_bh(mrioc, fwevt);
1641 case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
1643 mpi3mr_pcietopochg_evt_bh(mrioc, fwevt);
1646 case MPI3_EVENT_LOG_DATA:
1648 mpi3mr_logdata_evt_bh(mrioc, fwevt);
1651 case MPI3MR_DRIVER_EVENT_TG_QD_REDUCTION:
1653 struct mpi3mr_throttle_group_info *tg;
1655 tg = *(struct mpi3mr_throttle_group_info **)fwevt->event_data;
1656 dprint_event_bh(mrioc,
1657 "qd reduction event processed for tg_id(%d) reduction_needed(%d)\n",
1658 tg->id, tg->need_qd_reduction);
1659 if (tg->need_qd_reduction) {
1660 mpi3mr_set_qd_for_all_vd_in_tg(mrioc, tg);
1661 tg->need_qd_reduction = 0;
1670 if (fwevt->send_ack)
1671 mpi3mr_process_event_ack(mrioc, fwevt->event_id,
1674 /* Put fwevt reference count to neutralize kref_init increment */
1675 mpi3mr_fwevt_put(fwevt);
1676 mrioc->current_event = NULL;
1680 * mpi3mr_fwevt_worker - Firmware event worker
1681 * @work: Work struct containing firmware event
1683 * Extracts the firmware event and calls mpi3mr_fwevt_bh.
1687 static void mpi3mr_fwevt_worker(struct work_struct *work)
1689 struct mpi3mr_fwevt *fwevt = container_of(work, struct mpi3mr_fwevt,
1691 mpi3mr_fwevt_bh(fwevt->mrioc, fwevt);
1693 * Put fwevt reference count after
1694 * dequeuing it from worker queue
1696 mpi3mr_fwevt_put(fwevt);
1700 * mpi3mr_create_tgtdev - Create and add a target device
1701 * @mrioc: Adapter instance reference
1702 * @dev_pg0: Device Page 0 data
1704 * If the device specified by the device page 0 data is not
1705 * present in the driver's internal list, allocate the memory
1706 * for the device, populate the data and add to the list, else
1707 * update the device data. The key is persistent ID.
1709 * Return: 0 on success, -ENOMEM on memory allocation failure
1711 static int mpi3mr_create_tgtdev(struct mpi3mr_ioc *mrioc,
1712 struct mpi3_device_page0 *dev_pg0)
1715 struct mpi3mr_tgt_dev *tgtdev = NULL;
1718 perst_id = le16_to_cpu(dev_pg0->persistent_id);
1719 tgtdev = mpi3mr_get_tgtdev_by_perst_id(mrioc, perst_id);
1721 mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0, true);
1722 mpi3mr_tgtdev_put(tgtdev);
1724 tgtdev = mpi3mr_alloc_tgtdev();
1727 mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0, true);
1728 mpi3mr_tgtdev_add_to_list(mrioc, tgtdev);
1735 * mpi3mr_flush_delayed_cmd_lists - Flush pending commands
1736 * @mrioc: Adapter instance reference
1738 * Flush pending commands in the delayed lists due to a
1739 * controller reset or driver removal as a cleanup.
1743 void mpi3mr_flush_delayed_cmd_lists(struct mpi3mr_ioc *mrioc)
1745 struct delayed_dev_rmhs_node *_rmhs_node;
1746 struct delayed_evt_ack_node *_evtack_node;
1748 dprint_reset(mrioc, "flushing delayed dev_remove_hs commands\n");
1749 while (!list_empty(&mrioc->delayed_rmhs_list)) {
1750 _rmhs_node = list_entry(mrioc->delayed_rmhs_list.next,
1751 struct delayed_dev_rmhs_node, list);
1752 list_del(&_rmhs_node->list);
1755 dprint_reset(mrioc, "flushing delayed event ack commands\n");
1756 while (!list_empty(&mrioc->delayed_evtack_cmds_list)) {
1757 _evtack_node = list_entry(mrioc->delayed_evtack_cmds_list.next,
1758 struct delayed_evt_ack_node, list);
1759 list_del(&_evtack_node->list);
1760 kfree(_evtack_node);
1765 * mpi3mr_dev_rmhs_complete_iou - Device removal IOUC completion
1766 * @mrioc: Adapter instance reference
1767 * @drv_cmd: Internal command tracker
1769 * Issues a target reset TM to the firmware from the device
1770 * removal TM pend list or retry the removal handshake sequence
1771 * based on the IOU control request IOC status.
1775 static void mpi3mr_dev_rmhs_complete_iou(struct mpi3mr_ioc *mrioc,
1776 struct mpi3mr_drv_cmd *drv_cmd)
1778 u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
1779 struct delayed_dev_rmhs_node *delayed_dev_rmhs = NULL;
1781 if (drv_cmd->state & MPI3MR_CMD_RESET)
1785 "%s :dev_rmhs_iouctrl_complete:handle(0x%04x), ioc_status(0x%04x), loginfo(0x%08x)\n",
1786 __func__, drv_cmd->dev_handle, drv_cmd->ioc_status,
1787 drv_cmd->ioc_loginfo);
1788 if (drv_cmd->ioc_status != MPI3_IOCSTATUS_SUCCESS) {
1789 if (drv_cmd->retry_count < MPI3MR_DEV_RMHS_RETRY_COUNT) {
1790 drv_cmd->retry_count++;
1792 "%s :dev_rmhs_iouctrl_complete: handle(0x%04x)retrying handshake retry=%d\n",
1793 __func__, drv_cmd->dev_handle,
1794 drv_cmd->retry_count);
1795 mpi3mr_dev_rmhs_send_tm(mrioc, drv_cmd->dev_handle,
1796 drv_cmd, drv_cmd->iou_rc);
1800 "%s :dev removal handshake failed after all retries: handle(0x%04x)\n",
1801 __func__, drv_cmd->dev_handle);
1804 "%s :dev removal handshake completed successfully: handle(0x%04x)\n",
1805 __func__, drv_cmd->dev_handle);
1806 clear_bit(drv_cmd->dev_handle, mrioc->removepend_bitmap);
1809 if (!list_empty(&mrioc->delayed_rmhs_list)) {
1810 delayed_dev_rmhs = list_entry(mrioc->delayed_rmhs_list.next,
1811 struct delayed_dev_rmhs_node, list);
1812 drv_cmd->dev_handle = delayed_dev_rmhs->handle;
1813 drv_cmd->retry_count = 0;
1814 drv_cmd->iou_rc = delayed_dev_rmhs->iou_rc;
1816 "%s :dev_rmhs_iouctrl_complete: processing delayed TM: handle(0x%04x)\n",
1817 __func__, drv_cmd->dev_handle);
1818 mpi3mr_dev_rmhs_send_tm(mrioc, drv_cmd->dev_handle, drv_cmd,
1820 list_del(&delayed_dev_rmhs->list);
1821 kfree(delayed_dev_rmhs);
1826 drv_cmd->state = MPI3MR_CMD_NOTUSED;
1827 drv_cmd->callback = NULL;
1828 drv_cmd->retry_count = 0;
1829 drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
1830 clear_bit(cmd_idx, mrioc->devrem_bitmap);
1834 * mpi3mr_dev_rmhs_complete_tm - Device removal TM completion
1835 * @mrioc: Adapter instance reference
1836 * @drv_cmd: Internal command tracker
1838 * Issues a target reset TM to the firmware from the device
1839 * removal TM pend list or issue IO unit control request as
1840 * part of device removal or hidden acknowledgment handshake.
1844 static void mpi3mr_dev_rmhs_complete_tm(struct mpi3mr_ioc *mrioc,
1845 struct mpi3mr_drv_cmd *drv_cmd)
1847 struct mpi3_iounit_control_request iou_ctrl;
1848 u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
1849 struct mpi3_scsi_task_mgmt_reply *tm_reply = NULL;
1852 if (drv_cmd->state & MPI3MR_CMD_RESET)
1855 if (drv_cmd->state & MPI3MR_CMD_REPLY_VALID)
1856 tm_reply = (struct mpi3_scsi_task_mgmt_reply *)drv_cmd->reply;
1860 "dev_rmhs_tr_complete:handle(0x%04x), ioc_status(0x%04x), loginfo(0x%08x), term_count(%d)\n",
1861 mrioc->name, drv_cmd->dev_handle, drv_cmd->ioc_status,
1862 drv_cmd->ioc_loginfo,
1863 le32_to_cpu(tm_reply->termination_count));
1865 pr_info(IOCNAME "Issuing IOU CTL: handle(0x%04x) dev_rmhs idx(%d)\n",
1866 mrioc->name, drv_cmd->dev_handle, cmd_idx);
1868 memset(&iou_ctrl, 0, sizeof(iou_ctrl));
1870 drv_cmd->state = MPI3MR_CMD_PENDING;
1871 drv_cmd->is_waiting = 0;
1872 drv_cmd->callback = mpi3mr_dev_rmhs_complete_iou;
1873 iou_ctrl.operation = drv_cmd->iou_rc;
1874 iou_ctrl.param16[0] = cpu_to_le16(drv_cmd->dev_handle);
1875 iou_ctrl.host_tag = cpu_to_le16(drv_cmd->host_tag);
1876 iou_ctrl.function = MPI3_FUNCTION_IO_UNIT_CONTROL;
1878 retval = mpi3mr_admin_request_post(mrioc, &iou_ctrl, sizeof(iou_ctrl),
1881 pr_err(IOCNAME "Issue DevRmHsTMIOUCTL: Admin post failed\n",
1888 drv_cmd->state = MPI3MR_CMD_NOTUSED;
1889 drv_cmd->callback = NULL;
1890 drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
1891 drv_cmd->retry_count = 0;
1892 clear_bit(cmd_idx, mrioc->devrem_bitmap);
1896 * mpi3mr_dev_rmhs_send_tm - Issue TM for device removal
1897 * @mrioc: Adapter instance reference
1898 * @handle: Device handle
1899 * @cmdparam: Internal command tracker
1900 * @iou_rc: IO unit reason code
1902 * Issues a target reset TM to the firmware or add it to a pend
1903 * list as part of device removal or hidden acknowledgment
1908 static void mpi3mr_dev_rmhs_send_tm(struct mpi3mr_ioc *mrioc, u16 handle,
1909 struct mpi3mr_drv_cmd *cmdparam, u8 iou_rc)
1911 struct mpi3_scsi_task_mgmt_request tm_req;
1913 u16 cmd_idx = MPI3MR_NUM_DEVRMCMD;
1915 struct mpi3mr_drv_cmd *drv_cmd = cmdparam;
1916 struct delayed_dev_rmhs_node *delayed_dev_rmhs = NULL;
1921 cmd_idx = find_first_zero_bit(mrioc->devrem_bitmap,
1922 MPI3MR_NUM_DEVRMCMD);
1923 if (cmd_idx < MPI3MR_NUM_DEVRMCMD) {
1924 if (!test_and_set_bit(cmd_idx, mrioc->devrem_bitmap))
1926 cmd_idx = MPI3MR_NUM_DEVRMCMD;
1928 } while (retrycount--);
1930 if (cmd_idx >= MPI3MR_NUM_DEVRMCMD) {
1931 delayed_dev_rmhs = kzalloc(sizeof(*delayed_dev_rmhs),
1933 if (!delayed_dev_rmhs)
1935 INIT_LIST_HEAD(&delayed_dev_rmhs->list);
1936 delayed_dev_rmhs->handle = handle;
1937 delayed_dev_rmhs->iou_rc = iou_rc;
1938 list_add_tail(&delayed_dev_rmhs->list,
1939 &mrioc->delayed_rmhs_list);
1940 ioc_info(mrioc, "%s :DevRmHs: tr:handle(0x%04x) is postponed\n",
1944 drv_cmd = &mrioc->dev_rmhs_cmds[cmd_idx];
1947 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
1949 "%s :Issuing TR TM: for devhandle 0x%04x with dev_rmhs %d\n",
1950 __func__, handle, cmd_idx);
1952 memset(&tm_req, 0, sizeof(tm_req));
1953 if (drv_cmd->state & MPI3MR_CMD_PENDING) {
1954 ioc_err(mrioc, "%s :Issue TM: Command is in use\n", __func__);
1957 drv_cmd->state = MPI3MR_CMD_PENDING;
1958 drv_cmd->is_waiting = 0;
1959 drv_cmd->callback = mpi3mr_dev_rmhs_complete_tm;
1960 drv_cmd->dev_handle = handle;
1961 drv_cmd->iou_rc = iou_rc;
1962 tm_req.dev_handle = cpu_to_le16(handle);
1963 tm_req.task_type = MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
1964 tm_req.host_tag = cpu_to_le16(drv_cmd->host_tag);
1965 tm_req.task_host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INVALID);
1966 tm_req.function = MPI3_FUNCTION_SCSI_TASK_MGMT;
1968 set_bit(handle, mrioc->removepend_bitmap);
1969 retval = mpi3mr_admin_request_post(mrioc, &tm_req, sizeof(tm_req), 1);
1971 ioc_err(mrioc, "%s :Issue DevRmHsTM: Admin Post failed\n",
1978 drv_cmd->state = MPI3MR_CMD_NOTUSED;
1979 drv_cmd->callback = NULL;
1980 drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
1981 drv_cmd->retry_count = 0;
1982 clear_bit(cmd_idx, mrioc->devrem_bitmap);
1986 * mpi3mr_complete_evt_ack - event ack request completion
1987 * @mrioc: Adapter instance reference
1988 * @drv_cmd: Internal command tracker
1990 * This is the completion handler for non blocking event
1991 * acknowledgment sent to the firmware and this will issue any
1992 * pending event acknowledgment request.
1996 static void mpi3mr_complete_evt_ack(struct mpi3mr_ioc *mrioc,
1997 struct mpi3mr_drv_cmd *drv_cmd)
1999 u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
2000 struct delayed_evt_ack_node *delayed_evtack = NULL;
2002 if (drv_cmd->state & MPI3MR_CMD_RESET)
2005 if (drv_cmd->ioc_status != MPI3_IOCSTATUS_SUCCESS) {
2006 dprint_event_th(mrioc,
2007 "immediate event ack failed with ioc_status(0x%04x) log_info(0x%08x)\n",
2008 (drv_cmd->ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
2009 drv_cmd->ioc_loginfo);
2012 if (!list_empty(&mrioc->delayed_evtack_cmds_list)) {
2014 list_entry(mrioc->delayed_evtack_cmds_list.next,
2015 struct delayed_evt_ack_node, list);
2016 mpi3mr_send_event_ack(mrioc, delayed_evtack->event, drv_cmd,
2017 delayed_evtack->event_ctx);
2018 list_del(&delayed_evtack->list);
2019 kfree(delayed_evtack);
2023 drv_cmd->state = MPI3MR_CMD_NOTUSED;
2024 drv_cmd->callback = NULL;
2025 clear_bit(cmd_idx, mrioc->evtack_cmds_bitmap);
2029 * mpi3mr_send_event_ack - Issue event acknwoledgment request
2030 * @mrioc: Adapter instance reference
2031 * @event: MPI3 event id
2032 * @cmdparam: Internal command tracker
2033 * @event_ctx: event context
2035 * Issues event acknowledgment request to the firmware if there
2036 * is a free command to send the event ack else it to a pend
2037 * list so that it will be processed on a completion of a prior
2038 * event acknowledgment .
2042 static void mpi3mr_send_event_ack(struct mpi3mr_ioc *mrioc, u8 event,
2043 struct mpi3mr_drv_cmd *cmdparam, u32 event_ctx)
2045 struct mpi3_event_ack_request evtack_req;
2048 u16 cmd_idx = MPI3MR_NUM_EVTACKCMD;
2049 struct mpi3mr_drv_cmd *drv_cmd = cmdparam;
2050 struct delayed_evt_ack_node *delayed_evtack = NULL;
2053 dprint_event_th(mrioc,
2054 "sending delayed event ack in the top half for event(0x%02x), event_ctx(0x%08x)\n",
2058 dprint_event_th(mrioc,
2059 "sending event ack in the top half for event(0x%02x), event_ctx(0x%08x)\n",
2062 cmd_idx = find_first_zero_bit(mrioc->evtack_cmds_bitmap,
2063 MPI3MR_NUM_EVTACKCMD);
2064 if (cmd_idx < MPI3MR_NUM_EVTACKCMD) {
2065 if (!test_and_set_bit(cmd_idx,
2066 mrioc->evtack_cmds_bitmap))
2068 cmd_idx = MPI3MR_NUM_EVTACKCMD;
2070 } while (retrycount--);
2072 if (cmd_idx >= MPI3MR_NUM_EVTACKCMD) {
2073 delayed_evtack = kzalloc(sizeof(*delayed_evtack),
2075 if (!delayed_evtack)
2077 INIT_LIST_HEAD(&delayed_evtack->list);
2078 delayed_evtack->event = event;
2079 delayed_evtack->event_ctx = event_ctx;
2080 list_add_tail(&delayed_evtack->list,
2081 &mrioc->delayed_evtack_cmds_list);
2082 dprint_event_th(mrioc,
2083 "event ack in the top half for event(0x%02x), event_ctx(0x%08x) is postponed\n",
2087 drv_cmd = &mrioc->evtack_cmds[cmd_idx];
2090 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
2092 memset(&evtack_req, 0, sizeof(evtack_req));
2093 if (drv_cmd->state & MPI3MR_CMD_PENDING) {
2094 dprint_event_th(mrioc,
2095 "sending event ack failed due to command in use\n");
2098 drv_cmd->state = MPI3MR_CMD_PENDING;
2099 drv_cmd->is_waiting = 0;
2100 drv_cmd->callback = mpi3mr_complete_evt_ack;
2101 evtack_req.host_tag = cpu_to_le16(drv_cmd->host_tag);
2102 evtack_req.function = MPI3_FUNCTION_EVENT_ACK;
2103 evtack_req.event = event;
2104 evtack_req.event_context = cpu_to_le32(event_ctx);
2105 retval = mpi3mr_admin_request_post(mrioc, &evtack_req,
2106 sizeof(evtack_req), 1);
2108 dprint_event_th(mrioc,
2109 "posting event ack request is failed\n");
2113 dprint_event_th(mrioc,
2114 "event ack in the top half for event(0x%02x), event_ctx(0x%08x) is posted\n",
2119 drv_cmd->state = MPI3MR_CMD_NOTUSED;
2120 drv_cmd->callback = NULL;
2121 clear_bit(cmd_idx, mrioc->evtack_cmds_bitmap);
2125 * mpi3mr_pcietopochg_evt_th - PCIETopologyChange evt tophalf
2126 * @mrioc: Adapter instance reference
2127 * @event_reply: event data
2129 * Checks for the reason code and based on that either block I/O
2130 * to device, or unblock I/O to the device, or start the device
2131 * removal handshake with reason as remove with the firmware for
2136 static void mpi3mr_pcietopochg_evt_th(struct mpi3mr_ioc *mrioc,
2137 struct mpi3_event_notification_reply *event_reply)
2139 struct mpi3_event_data_pcie_topology_change_list *topo_evt =
2140 (struct mpi3_event_data_pcie_topology_change_list *)event_reply->event_data;
2144 struct mpi3mr_tgt_dev *tgtdev = NULL;
2145 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2147 for (i = 0; i < topo_evt->num_entries; i++) {
2148 handle = le16_to_cpu(topo_evt->port_entry[i].attached_dev_handle);
2151 reason_code = topo_evt->port_entry[i].port_status;
2152 scsi_tgt_priv_data = NULL;
2153 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
2154 if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
2155 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2156 tgtdev->starget->hostdata;
2157 switch (reason_code) {
2158 case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
2159 if (scsi_tgt_priv_data) {
2160 scsi_tgt_priv_data->dev_removed = 1;
2161 scsi_tgt_priv_data->dev_removedelay = 0;
2162 atomic_set(&scsi_tgt_priv_data->block_io, 0);
2164 mpi3mr_dev_rmhs_send_tm(mrioc, handle, NULL,
2165 MPI3_CTRL_OP_REMOVE_DEVICE);
2167 case MPI3_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
2168 if (scsi_tgt_priv_data) {
2169 scsi_tgt_priv_data->dev_removedelay = 1;
2170 atomic_inc(&scsi_tgt_priv_data->block_io);
2173 case MPI3_EVENT_PCIE_TOPO_PS_RESPONDING:
2174 if (scsi_tgt_priv_data &&
2175 scsi_tgt_priv_data->dev_removedelay) {
2176 scsi_tgt_priv_data->dev_removedelay = 0;
2177 atomic_dec_if_positive
2178 (&scsi_tgt_priv_data->block_io);
2181 case MPI3_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
2186 mpi3mr_tgtdev_put(tgtdev);
2191 * mpi3mr_sastopochg_evt_th - SASTopologyChange evt tophalf
2192 * @mrioc: Adapter instance reference
2193 * @event_reply: event data
2195 * Checks for the reason code and based on that either block I/O
2196 * to device, or unblock I/O to the device, or start the device
2197 * removal handshake with reason as remove with the firmware for
2202 static void mpi3mr_sastopochg_evt_th(struct mpi3mr_ioc *mrioc,
2203 struct mpi3_event_notification_reply *event_reply)
2205 struct mpi3_event_data_sas_topology_change_list *topo_evt =
2206 (struct mpi3_event_data_sas_topology_change_list *)event_reply->event_data;
2210 struct mpi3mr_tgt_dev *tgtdev = NULL;
2211 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2213 for (i = 0; i < topo_evt->num_entries; i++) {
2214 handle = le16_to_cpu(topo_evt->phy_entry[i].attached_dev_handle);
2217 reason_code = topo_evt->phy_entry[i].status &
2218 MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
2219 scsi_tgt_priv_data = NULL;
2220 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
2221 if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
2222 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2223 tgtdev->starget->hostdata;
2224 switch (reason_code) {
2225 case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
2226 if (scsi_tgt_priv_data) {
2227 scsi_tgt_priv_data->dev_removed = 1;
2228 scsi_tgt_priv_data->dev_removedelay = 0;
2229 atomic_set(&scsi_tgt_priv_data->block_io, 0);
2231 mpi3mr_dev_rmhs_send_tm(mrioc, handle, NULL,
2232 MPI3_CTRL_OP_REMOVE_DEVICE);
2234 case MPI3_EVENT_SAS_TOPO_PHY_RC_DELAY_NOT_RESPONDING:
2235 if (scsi_tgt_priv_data) {
2236 scsi_tgt_priv_data->dev_removedelay = 1;
2237 atomic_inc(&scsi_tgt_priv_data->block_io);
2240 case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
2241 if (scsi_tgt_priv_data &&
2242 scsi_tgt_priv_data->dev_removedelay) {
2243 scsi_tgt_priv_data->dev_removedelay = 0;
2244 atomic_dec_if_positive
2245 (&scsi_tgt_priv_data->block_io);
2248 case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
2253 mpi3mr_tgtdev_put(tgtdev);
2258 * mpi3mr_devstatuschg_evt_th - DeviceStatusChange evt tophalf
2259 * @mrioc: Adapter instance reference
2260 * @event_reply: event data
2262 * Checks for the reason code and based on that either block I/O
2263 * to device, or unblock I/O to the device, or start the device
2264 * removal handshake with reason as remove/hide acknowledgment
2265 * with the firmware.
2269 static void mpi3mr_devstatuschg_evt_th(struct mpi3mr_ioc *mrioc,
2270 struct mpi3_event_notification_reply *event_reply)
2273 u8 ublock = 0, block = 0, hide = 0, delete = 0, remove = 0;
2274 struct mpi3mr_tgt_dev *tgtdev = NULL;
2275 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2276 struct mpi3_event_data_device_status_change *evtdata =
2277 (struct mpi3_event_data_device_status_change *)event_reply->event_data;
2279 if (mrioc->stop_drv_processing)
2282 dev_handle = le16_to_cpu(evtdata->dev_handle);
2284 switch (evtdata->reason_code) {
2285 case MPI3_EVENT_DEV_STAT_RC_INT_DEVICE_RESET_STRT:
2286 case MPI3_EVENT_DEV_STAT_RC_INT_IT_NEXUS_RESET_STRT:
2289 case MPI3_EVENT_DEV_STAT_RC_HIDDEN:
2293 case MPI3_EVENT_DEV_STAT_RC_VD_NOT_RESPONDING:
2297 case MPI3_EVENT_DEV_STAT_RC_INT_DEVICE_RESET_CMP:
2298 case MPI3_EVENT_DEV_STAT_RC_INT_IT_NEXUS_RESET_CMP:
2305 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
2309 tgtdev->is_hidden = hide;
2310 if (tgtdev->starget && tgtdev->starget->hostdata) {
2311 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2312 tgtdev->starget->hostdata;
2314 atomic_inc(&scsi_tgt_priv_data->block_io);
2316 scsi_tgt_priv_data->dev_removed = 1;
2318 atomic_dec_if_positive(&scsi_tgt_priv_data->block_io);
2321 mpi3mr_dev_rmhs_send_tm(mrioc, dev_handle, NULL,
2322 MPI3_CTRL_OP_REMOVE_DEVICE);
2324 mpi3mr_dev_rmhs_send_tm(mrioc, dev_handle, NULL,
2325 MPI3_CTRL_OP_HIDDEN_ACK);
2329 mpi3mr_tgtdev_put(tgtdev);
2333 * mpi3mr_preparereset_evt_th - Prepare for reset event tophalf
2334 * @mrioc: Adapter instance reference
2335 * @event_reply: event data
2337 * Blocks and unblocks host level I/O based on the reason code
2341 static void mpi3mr_preparereset_evt_th(struct mpi3mr_ioc *mrioc,
2342 struct mpi3_event_notification_reply *event_reply)
2344 struct mpi3_event_data_prepare_for_reset *evtdata =
2345 (struct mpi3_event_data_prepare_for_reset *)event_reply->event_data;
2347 if (evtdata->reason_code == MPI3_EVENT_PREPARE_RESET_RC_START) {
2348 dprint_event_th(mrioc,
2349 "prepare for reset event top half with rc=start\n");
2350 if (mrioc->prepare_for_reset)
2352 mrioc->prepare_for_reset = 1;
2353 mrioc->prepare_for_reset_timeout_counter = 0;
2354 } else if (evtdata->reason_code == MPI3_EVENT_PREPARE_RESET_RC_ABORT) {
2355 dprint_event_th(mrioc,
2356 "prepare for reset top half with rc=abort\n");
2357 mrioc->prepare_for_reset = 0;
2358 mrioc->prepare_for_reset_timeout_counter = 0;
2360 if ((event_reply->msg_flags & MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_MASK)
2361 == MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_REQUIRED)
2362 mpi3mr_send_event_ack(mrioc, event_reply->event, NULL,
2363 le32_to_cpu(event_reply->event_context));
2367 * mpi3mr_energypackchg_evt_th - Energy pack change evt tophalf
2368 * @mrioc: Adapter instance reference
2369 * @event_reply: event data
2371 * Identifies the new shutdown timeout value and update.
2375 static void mpi3mr_energypackchg_evt_th(struct mpi3mr_ioc *mrioc,
2376 struct mpi3_event_notification_reply *event_reply)
2378 struct mpi3_event_data_energy_pack_change *evtdata =
2379 (struct mpi3_event_data_energy_pack_change *)event_reply->event_data;
2380 u16 shutdown_timeout = le16_to_cpu(evtdata->shutdown_timeout);
2382 if (shutdown_timeout <= 0) {
2384 "%s :Invalid Shutdown Timeout received = %d\n",
2385 __func__, shutdown_timeout);
2390 "%s :Previous Shutdown Timeout Value = %d New Shutdown Timeout Value = %d\n",
2391 __func__, mrioc->facts.shutdown_timeout, shutdown_timeout);
2392 mrioc->facts.shutdown_timeout = shutdown_timeout;
2396 * mpi3mr_cablemgmt_evt_th - Cable management event tophalf
2397 * @mrioc: Adapter instance reference
2398 * @event_reply: event data
2400 * Displays Cable manegemt event details.
2404 static void mpi3mr_cablemgmt_evt_th(struct mpi3mr_ioc *mrioc,
2405 struct mpi3_event_notification_reply *event_reply)
2407 struct mpi3_event_data_cable_management *evtdata =
2408 (struct mpi3_event_data_cable_management *)event_reply->event_data;
2410 switch (evtdata->status) {
2411 case MPI3_EVENT_CABLE_MGMT_STATUS_INSUFFICIENT_POWER:
2413 ioc_info(mrioc, "An active cable with receptacle_id %d cannot be powered.\n"
2414 "Devices connected to this cable are not detected.\n"
2415 "This cable requires %d mW of power.\n",
2416 evtdata->receptacle_id,
2417 le32_to_cpu(evtdata->active_cable_power_requirement));
2420 case MPI3_EVENT_CABLE_MGMT_STATUS_DEGRADED:
2422 ioc_info(mrioc, "A cable with receptacle_id %d is not running at optimal speed\n",
2423 evtdata->receptacle_id);
2432 * mpi3mr_os_handle_events - Firmware event handler
2433 * @mrioc: Adapter instance reference
2434 * @event_reply: event data
2436 * Identify whteher the event has to handled and acknowledged
2437 * and either process the event in the tophalf and/or schedule a
2438 * bottom half through mpi3mr_fwevt_worker.
2442 void mpi3mr_os_handle_events(struct mpi3mr_ioc *mrioc,
2443 struct mpi3_event_notification_reply *event_reply)
2446 struct mpi3mr_fwevt *fwevt = NULL;
2447 bool ack_req = 0, process_evt_bh = 0;
2449 if (mrioc->stop_drv_processing)
2452 if ((event_reply->msg_flags & MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_MASK)
2453 == MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_REQUIRED)
2456 evt_type = event_reply->event;
2459 case MPI3_EVENT_DEVICE_ADDED:
2461 struct mpi3_device_page0 *dev_pg0 =
2462 (struct mpi3_device_page0 *)event_reply->event_data;
2463 if (mpi3mr_create_tgtdev(mrioc, dev_pg0))
2465 "%s :Failed to add device in the device add event\n",
2471 case MPI3_EVENT_DEVICE_STATUS_CHANGE:
2474 mpi3mr_devstatuschg_evt_th(mrioc, event_reply);
2477 case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
2480 mpi3mr_sastopochg_evt_th(mrioc, event_reply);
2483 case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
2486 mpi3mr_pcietopochg_evt_th(mrioc, event_reply);
2489 case MPI3_EVENT_PREPARE_FOR_RESET:
2491 mpi3mr_preparereset_evt_th(mrioc, event_reply);
2495 case MPI3_EVENT_DEVICE_INFO_CHANGED:
2496 case MPI3_EVENT_LOG_DATA:
2501 case MPI3_EVENT_ENERGY_PACK_CHANGE:
2503 mpi3mr_energypackchg_evt_th(mrioc, event_reply);
2506 case MPI3_EVENT_CABLE_MGMT:
2508 mpi3mr_cablemgmt_evt_th(mrioc, event_reply);
2511 case MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE:
2512 case MPI3_EVENT_SAS_DISCOVERY:
2513 case MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
2514 case MPI3_EVENT_SAS_BROADCAST_PRIMITIVE:
2515 case MPI3_EVENT_PCIE_ENUMERATION:
2518 ioc_info(mrioc, "%s :event 0x%02x is not handled\n",
2519 __func__, evt_type);
2522 if (process_evt_bh || ack_req) {
2523 sz = event_reply->event_data_length * 4;
2524 fwevt = mpi3mr_alloc_fwevt(sz);
2526 ioc_info(mrioc, "%s :failure at %s:%d/%s()!\n",
2527 __func__, __FILE__, __LINE__, __func__);
2531 memcpy(fwevt->event_data, event_reply->event_data, sz);
2532 fwevt->mrioc = mrioc;
2533 fwevt->event_id = evt_type;
2534 fwevt->send_ack = ack_req;
2535 fwevt->process_evt = process_evt_bh;
2536 fwevt->evt_ctx = le32_to_cpu(event_reply->event_context);
2537 mpi3mr_fwevt_add_to_list(mrioc, fwevt);
2542 * mpi3mr_setup_eedp - Setup EEDP information in MPI3 SCSI IO
2543 * @mrioc: Adapter instance reference
2544 * @scmd: SCSI command reference
2545 * @scsiio_req: MPI3 SCSI IO request
2547 * Identifies the protection information flags from the SCSI
2548 * command and set appropriate flags in the MPI3 SCSI IO
2553 static void mpi3mr_setup_eedp(struct mpi3mr_ioc *mrioc,
2554 struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
2557 unsigned char prot_op = scsi_get_prot_op(scmd);
2560 case SCSI_PROT_NORMAL:
2562 case SCSI_PROT_READ_STRIP:
2563 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REMOVE;
2565 case SCSI_PROT_WRITE_INSERT:
2566 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_INSERT;
2568 case SCSI_PROT_READ_INSERT:
2569 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_INSERT;
2570 scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2572 case SCSI_PROT_WRITE_STRIP:
2573 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REMOVE;
2574 scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2576 case SCSI_PROT_READ_PASS:
2577 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK;
2578 scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2580 case SCSI_PROT_WRITE_PASS:
2581 if (scmd->prot_flags & SCSI_PROT_IP_CHECKSUM) {
2582 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REGEN;
2583 scsiio_req->sgl[0].eedp.application_tag_translation_mask =
2586 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK;
2588 scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2594 if (scmd->prot_flags & SCSI_PROT_GUARD_CHECK)
2595 eedp_flags |= MPI3_EEDPFLAGS_CHK_GUARD;
2597 if (scmd->prot_flags & SCSI_PROT_IP_CHECKSUM)
2598 eedp_flags |= MPI3_EEDPFLAGS_HOST_GUARD_IP_CHKSUM;
2600 if (scmd->prot_flags & SCSI_PROT_REF_CHECK) {
2601 eedp_flags |= MPI3_EEDPFLAGS_CHK_REF_TAG |
2602 MPI3_EEDPFLAGS_INCR_PRI_REF_TAG;
2603 scsiio_req->cdb.eedp32.primary_reference_tag =
2604 cpu_to_be32(scsi_prot_ref_tag(scmd));
2607 if (scmd->prot_flags & SCSI_PROT_REF_INCREMENT)
2608 eedp_flags |= MPI3_EEDPFLAGS_INCR_PRI_REF_TAG;
2610 eedp_flags |= MPI3_EEDPFLAGS_ESC_MODE_APPTAG_DISABLE;
2612 switch (scsi_prot_interval(scmd)) {
2614 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_512;
2617 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_520;
2620 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4080;
2623 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4088;
2626 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4096;
2629 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4104;
2632 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4160;
2638 scsiio_req->sgl[0].eedp.eedp_flags = cpu_to_le16(eedp_flags);
2639 scsiio_req->sgl[0].eedp.flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_EXTENDED;
2643 * mpi3mr_build_sense_buffer - Map sense information
2645 * @buf: Sense buffer to populate
2647 * @asc: Additional sense code
2648 * @ascq: Additional sense code qualifier
2650 * Maps the given sense information into either descriptor or
2651 * fixed format sense data.
2655 static inline void mpi3mr_build_sense_buffer(int desc, u8 *buf, u8 key,
2659 buf[0] = 0x72; /* descriptor, current */
2665 buf[0] = 0x70; /* fixed, current */
2674 * mpi3mr_map_eedp_error - Map EEDP errors from IOC status
2675 * @scmd: SCSI command reference
2676 * @ioc_status: status of MPI3 request
2678 * Maps the EEDP error status of the SCSI IO request to sense
2683 static void mpi3mr_map_eedp_error(struct scsi_cmnd *scmd,
2688 switch (ioc_status) {
2689 case MPI3_IOCSTATUS_EEDP_GUARD_ERROR:
2692 case MPI3_IOCSTATUS_EEDP_APP_TAG_ERROR:
2695 case MPI3_IOCSTATUS_EEDP_REF_TAG_ERROR:
2703 mpi3mr_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
2705 scmd->result = (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION;
2709 * mpi3mr_process_op_reply_desc - reply descriptor handler
2710 * @mrioc: Adapter instance reference
2711 * @reply_desc: Operational reply descriptor
2712 * @reply_dma: place holder for reply DMA address
2713 * @qidx: Operational queue index
2715 * Process the operational reply descriptor and identifies the
2716 * descriptor type. Based on the descriptor map the MPI3 request
2717 * status to a SCSI command status and calls scsi_done call
2722 void mpi3mr_process_op_reply_desc(struct mpi3mr_ioc *mrioc,
2723 struct mpi3_default_reply_descriptor *reply_desc, u64 *reply_dma, u16 qidx)
2725 u16 reply_desc_type, host_tag = 0;
2726 u16 ioc_status = MPI3_IOCSTATUS_SUCCESS;
2727 u32 ioc_loginfo = 0;
2728 struct mpi3_status_reply_descriptor *status_desc = NULL;
2729 struct mpi3_address_reply_descriptor *addr_desc = NULL;
2730 struct mpi3_success_reply_descriptor *success_desc = NULL;
2731 struct mpi3_scsi_io_reply *scsi_reply = NULL;
2732 struct scsi_cmnd *scmd = NULL;
2733 struct scmd_priv *priv = NULL;
2734 u8 *sense_buf = NULL;
2735 u8 scsi_state = 0, scsi_status = 0, sense_state = 0;
2736 u32 xfer_count = 0, sense_count = 0, resp_data = 0;
2737 u16 dev_handle = 0xFFFF;
2738 struct scsi_sense_hdr sshdr;
2739 struct mpi3mr_stgt_priv_data *stgt_priv_data = NULL;
2740 struct mpi3mr_sdev_priv_data *sdev_priv_data = NULL;
2741 u32 ioc_pend_data_len = 0, tg_pend_data_len = 0, data_len_blks = 0;
2742 struct mpi3mr_throttle_group_info *tg = NULL;
2743 u8 throttle_enabled_dev = 0;
2746 reply_desc_type = le16_to_cpu(reply_desc->reply_flags) &
2747 MPI3_REPLY_DESCRIPT_FLAGS_TYPE_MASK;
2748 switch (reply_desc_type) {
2749 case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_STATUS:
2750 status_desc = (struct mpi3_status_reply_descriptor *)reply_desc;
2751 host_tag = le16_to_cpu(status_desc->host_tag);
2752 ioc_status = le16_to_cpu(status_desc->ioc_status);
2754 MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
2755 ioc_loginfo = le32_to_cpu(status_desc->ioc_log_info);
2756 ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
2758 case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_ADDRESS_REPLY:
2759 addr_desc = (struct mpi3_address_reply_descriptor *)reply_desc;
2760 *reply_dma = le64_to_cpu(addr_desc->reply_frame_address);
2761 scsi_reply = mpi3mr_get_reply_virt_addr(mrioc,
2764 panic("%s: scsi_reply is NULL, this shouldn't happen\n",
2768 host_tag = le16_to_cpu(scsi_reply->host_tag);
2769 ioc_status = le16_to_cpu(scsi_reply->ioc_status);
2770 scsi_status = scsi_reply->scsi_status;
2771 scsi_state = scsi_reply->scsi_state;
2772 dev_handle = le16_to_cpu(scsi_reply->dev_handle);
2773 sense_state = (scsi_state & MPI3_SCSI_STATE_SENSE_MASK);
2774 xfer_count = le32_to_cpu(scsi_reply->transfer_count);
2775 sense_count = le32_to_cpu(scsi_reply->sense_count);
2776 resp_data = le32_to_cpu(scsi_reply->response_data);
2777 sense_buf = mpi3mr_get_sensebuf_virt_addr(mrioc,
2778 le64_to_cpu(scsi_reply->sense_data_buffer_address));
2780 MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
2781 ioc_loginfo = le32_to_cpu(scsi_reply->ioc_log_info);
2782 ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
2783 if (sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY)
2784 panic("%s: Ran out of sense buffers\n", mrioc->name);
2786 case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_SUCCESS:
2787 success_desc = (struct mpi3_success_reply_descriptor *)reply_desc;
2788 host_tag = le16_to_cpu(success_desc->host_tag);
2793 scmd = mpi3mr_scmd_from_host_tag(mrioc, host_tag, qidx);
2795 panic("%s: Cannot Identify scmd for host_tag 0x%x\n",
2796 mrioc->name, host_tag);
2799 priv = scsi_cmd_priv(scmd);
2801 data_len_blks = scsi_bufflen(scmd) >> 9;
2802 sdev_priv_data = scmd->device->hostdata;
2803 if (sdev_priv_data) {
2804 stgt_priv_data = sdev_priv_data->tgt_priv_data;
2805 if (stgt_priv_data) {
2806 tg = stgt_priv_data->throttle_group;
2807 throttle_enabled_dev =
2808 stgt_priv_data->io_throttle_enabled;
2811 if (unlikely((data_len_blks >= mrioc->io_throttle_data_length) &&
2812 throttle_enabled_dev)) {
2813 ioc_pend_data_len = atomic_sub_return(data_len_blks,
2814 &mrioc->pend_large_data_sz);
2816 tg_pend_data_len = atomic_sub_return(data_len_blks,
2817 &tg->pend_large_data_sz);
2818 if (tg->io_divert && ((ioc_pend_data_len <=
2819 mrioc->io_throttle_low) &&
2820 (tg_pend_data_len <= tg->low))) {
2822 mpi3mr_set_io_divert_for_all_vd_in_tg(
2826 if (ioc_pend_data_len <= mrioc->io_throttle_low)
2827 stgt_priv_data->io_divert = 0;
2829 } else if (unlikely((stgt_priv_data && stgt_priv_data->io_divert))) {
2830 ioc_pend_data_len = atomic_read(&mrioc->pend_large_data_sz);
2832 if (ioc_pend_data_len <= mrioc->io_throttle_low)
2833 stgt_priv_data->io_divert = 0;
2835 } else if (ioc_pend_data_len <= mrioc->io_throttle_low) {
2836 tg_pend_data_len = atomic_read(&tg->pend_large_data_sz);
2837 if (tg->io_divert && (tg_pend_data_len <= tg->low)) {
2839 mpi3mr_set_io_divert_for_all_vd_in_tg(
2846 scmd->result = DID_OK << 16;
2850 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_count);
2851 if (ioc_status == MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN &&
2852 xfer_count == 0 && (scsi_status == MPI3_SCSI_STATUS_BUSY ||
2853 scsi_status == MPI3_SCSI_STATUS_RESERVATION_CONFLICT ||
2854 scsi_status == MPI3_SCSI_STATUS_TASK_SET_FULL))
2855 ioc_status = MPI3_IOCSTATUS_SUCCESS;
2857 if ((sense_state == MPI3_SCSI_STATE_SENSE_VALID) && sense_count &&
2859 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE, sense_count);
2861 memcpy(scmd->sense_buffer, sense_buf, sz);
2864 switch (ioc_status) {
2865 case MPI3_IOCSTATUS_BUSY:
2866 case MPI3_IOCSTATUS_INSUFFICIENT_RESOURCES:
2867 scmd->result = SAM_STAT_BUSY;
2869 case MPI3_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
2870 scmd->result = DID_NO_CONNECT << 16;
2872 case MPI3_IOCSTATUS_SCSI_IOC_TERMINATED:
2873 scmd->result = DID_SOFT_ERROR << 16;
2875 case MPI3_IOCSTATUS_SCSI_TASK_TERMINATED:
2876 case MPI3_IOCSTATUS_SCSI_EXT_TERMINATED:
2877 scmd->result = DID_RESET << 16;
2879 case MPI3_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
2880 if ((xfer_count == 0) || (scmd->underflow > xfer_count))
2881 scmd->result = DID_SOFT_ERROR << 16;
2883 scmd->result = (DID_OK << 16) | scsi_status;
2885 case MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN:
2886 scmd->result = (DID_OK << 16) | scsi_status;
2887 if (sense_state == MPI3_SCSI_STATE_SENSE_VALID)
2889 if (xfer_count < scmd->underflow) {
2890 if (scsi_status == SAM_STAT_BUSY)
2891 scmd->result = SAM_STAT_BUSY;
2893 scmd->result = DID_SOFT_ERROR << 16;
2894 } else if ((scsi_state & (MPI3_SCSI_STATE_NO_SCSI_STATUS)) ||
2895 (sense_state != MPI3_SCSI_STATE_SENSE_NOT_AVAILABLE))
2896 scmd->result = DID_SOFT_ERROR << 16;
2897 else if (scsi_state & MPI3_SCSI_STATE_TERMINATED)
2898 scmd->result = DID_RESET << 16;
2900 case MPI3_IOCSTATUS_SCSI_DATA_OVERRUN:
2901 scsi_set_resid(scmd, 0);
2903 case MPI3_IOCSTATUS_SCSI_RECOVERED_ERROR:
2904 case MPI3_IOCSTATUS_SUCCESS:
2905 scmd->result = (DID_OK << 16) | scsi_status;
2906 if ((scsi_state & (MPI3_SCSI_STATE_NO_SCSI_STATUS)) ||
2907 (sense_state == MPI3_SCSI_STATE_SENSE_FAILED) ||
2908 (sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY))
2909 scmd->result = DID_SOFT_ERROR << 16;
2910 else if (scsi_state & MPI3_SCSI_STATE_TERMINATED)
2911 scmd->result = DID_RESET << 16;
2913 case MPI3_IOCSTATUS_EEDP_GUARD_ERROR:
2914 case MPI3_IOCSTATUS_EEDP_REF_TAG_ERROR:
2915 case MPI3_IOCSTATUS_EEDP_APP_TAG_ERROR:
2916 mpi3mr_map_eedp_error(scmd, ioc_status);
2918 case MPI3_IOCSTATUS_SCSI_PROTOCOL_ERROR:
2919 case MPI3_IOCSTATUS_INVALID_FUNCTION:
2920 case MPI3_IOCSTATUS_INVALID_SGL:
2921 case MPI3_IOCSTATUS_INTERNAL_ERROR:
2922 case MPI3_IOCSTATUS_INVALID_FIELD:
2923 case MPI3_IOCSTATUS_INVALID_STATE:
2924 case MPI3_IOCSTATUS_SCSI_IO_DATA_ERROR:
2925 case MPI3_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
2926 case MPI3_IOCSTATUS_INSUFFICIENT_POWER:
2928 scmd->result = DID_SOFT_ERROR << 16;
2932 if (scmd->result != (DID_OK << 16) && (scmd->cmnd[0] != ATA_12) &&
2933 (scmd->cmnd[0] != ATA_16)) {
2934 ioc_info(mrioc, "%s :scmd->result 0x%x\n", __func__,
2936 scsi_print_command(scmd);
2938 "%s :Command issued to handle 0x%02x returned with error 0x%04x loginfo 0x%08x, qid %d\n",
2939 __func__, dev_handle, ioc_status, ioc_loginfo,
2940 priv->req_q_idx + 1);
2942 " host_tag %d scsi_state 0x%02x scsi_status 0x%02x, xfer_cnt %d resp_data 0x%x\n",
2943 host_tag, scsi_state, scsi_status, xfer_count, resp_data);
2945 scsi_normalize_sense(sense_buf, sense_count, &sshdr);
2947 "%s :sense_count 0x%x, sense_key 0x%x ASC 0x%x, ASCQ 0x%x\n",
2948 __func__, sense_count, sshdr.sense_key,
2949 sshdr.asc, sshdr.ascq);
2953 if (priv->meta_sg_valid) {
2954 dma_unmap_sg(&mrioc->pdev->dev, scsi_prot_sglist(scmd),
2955 scsi_prot_sg_count(scmd), scmd->sc_data_direction);
2957 mpi3mr_clear_scmd_priv(mrioc, scmd);
2958 scsi_dma_unmap(scmd);
2962 mpi3mr_repost_sense_buf(mrioc,
2963 le64_to_cpu(scsi_reply->sense_data_buffer_address));
2967 * mpi3mr_get_chain_idx - get free chain buffer index
2968 * @mrioc: Adapter instance reference
2970 * Try to get a free chain buffer index from the free pool.
2972 * Return: -1 on failure or the free chain buffer index
2974 static int mpi3mr_get_chain_idx(struct mpi3mr_ioc *mrioc)
2980 spin_lock(&mrioc->chain_buf_lock);
2981 cmd_idx = find_first_zero_bit(mrioc->chain_bitmap,
2982 mrioc->chain_buf_count);
2983 if (cmd_idx < mrioc->chain_buf_count) {
2984 set_bit(cmd_idx, mrioc->chain_bitmap);
2985 spin_unlock(&mrioc->chain_buf_lock);
2988 spin_unlock(&mrioc->chain_buf_lock);
2990 } while (retry_count--);
2995 * mpi3mr_prepare_sg_scmd - build scatter gather list
2996 * @mrioc: Adapter instance reference
2997 * @scmd: SCSI command reference
2998 * @scsiio_req: MPI3 SCSI IO request
3000 * This function maps SCSI command's data and protection SGEs to
3001 * MPI request SGEs. If required additional 4K chain buffer is
3002 * used to send the SGEs.
3004 * Return: 0 on success, -ENOMEM on dma_map_sg failure
3006 static int mpi3mr_prepare_sg_scmd(struct mpi3mr_ioc *mrioc,
3007 struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
3009 dma_addr_t chain_dma;
3010 struct scatterlist *sg_scmd;
3011 void *sg_local, *chain;
3013 int sges_left, chain_idx;
3014 u32 sges_in_segment;
3015 u8 simple_sgl_flags;
3016 u8 simple_sgl_flags_last;
3017 u8 last_chain_sgl_flags;
3018 struct chain_element *chain_req;
3019 struct scmd_priv *priv = NULL;
3020 u32 meta_sg = le32_to_cpu(scsiio_req->flags) &
3021 MPI3_SCSIIO_FLAGS_DMAOPERATION_HOST_PI;
3023 priv = scsi_cmd_priv(scmd);
3025 simple_sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_SIMPLE |
3026 MPI3_SGE_FLAGS_DLAS_SYSTEM;
3027 simple_sgl_flags_last = simple_sgl_flags |
3028 MPI3_SGE_FLAGS_END_OF_LIST;
3029 last_chain_sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_LAST_CHAIN |
3030 MPI3_SGE_FLAGS_DLAS_SYSTEM;
3033 sg_local = &scsiio_req->sgl[MPI3_SCSIIO_METASGL_INDEX];
3035 sg_local = &scsiio_req->sgl;
3037 if (!scsiio_req->data_length && !meta_sg) {
3038 mpi3mr_build_zero_len_sge(sg_local);
3043 sg_scmd = scsi_prot_sglist(scmd);
3044 sges_left = dma_map_sg(&mrioc->pdev->dev,
3045 scsi_prot_sglist(scmd),
3046 scsi_prot_sg_count(scmd),
3047 scmd->sc_data_direction);
3048 priv->meta_sg_valid = 1; /* To unmap meta sg DMA */
3050 sg_scmd = scsi_sglist(scmd);
3051 sges_left = scsi_dma_map(scmd);
3054 if (sges_left < 0) {
3055 sdev_printk(KERN_ERR, scmd->device,
3056 "scsi_dma_map failed: request for %d bytes!\n",
3057 scsi_bufflen(scmd));
3060 if (sges_left > MPI3MR_SG_DEPTH) {
3061 sdev_printk(KERN_ERR, scmd->device,
3062 "scsi_dma_map returned unsupported sge count %d!\n",
3067 sges_in_segment = (mrioc->facts.op_req_sz -
3068 offsetof(struct mpi3_scsi_io_request, sgl)) / sizeof(struct mpi3_sge_common);
3070 if (scsiio_req->sgl[0].eedp.flags ==
3071 MPI3_SGE_FLAGS_ELEMENT_TYPE_EXTENDED && !meta_sg) {
3072 sg_local += sizeof(struct mpi3_sge_common);
3074 /* Reserve 1st segment (scsiio_req->sgl[0]) for eedp */
3077 if (scsiio_req->msg_flags ==
3078 MPI3_SCSIIO_MSGFLAGS_METASGL_VALID && !meta_sg) {
3080 /* Reserve last segment (scsiio_req->sgl[3]) for meta sg */
3084 sges_in_segment = 1;
3086 if (sges_left <= sges_in_segment)
3087 goto fill_in_last_segment;
3089 /* fill in main message segment when there is a chain following */
3090 while (sges_in_segment > 1) {
3091 mpi3mr_add_sg_single(sg_local, simple_sgl_flags,
3092 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
3093 sg_scmd = sg_next(sg_scmd);
3094 sg_local += sizeof(struct mpi3_sge_common);
3099 chain_idx = mpi3mr_get_chain_idx(mrioc);
3102 chain_req = &mrioc->chain_sgl_list[chain_idx];
3104 priv->meta_chain_idx = chain_idx;
3106 priv->chain_idx = chain_idx;
3108 chain = chain_req->addr;
3109 chain_dma = chain_req->dma_addr;
3110 sges_in_segment = sges_left;
3111 chain_length = sges_in_segment * sizeof(struct mpi3_sge_common);
3113 mpi3mr_add_sg_single(sg_local, last_chain_sgl_flags,
3114 chain_length, chain_dma);
3118 fill_in_last_segment:
3119 while (sges_left > 0) {
3121 mpi3mr_add_sg_single(sg_local,
3122 simple_sgl_flags_last, sg_dma_len(sg_scmd),
3123 sg_dma_address(sg_scmd));
3125 mpi3mr_add_sg_single(sg_local, simple_sgl_flags,
3126 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
3127 sg_scmd = sg_next(sg_scmd);
3128 sg_local += sizeof(struct mpi3_sge_common);
3136 * mpi3mr_build_sg_scmd - build scatter gather list for SCSI IO
3137 * @mrioc: Adapter instance reference
3138 * @scmd: SCSI command reference
3139 * @scsiio_req: MPI3 SCSI IO request
3141 * This function calls mpi3mr_prepare_sg_scmd for constructing
3142 * both data SGEs and protection information SGEs in the MPI
3143 * format from the SCSI Command as appropriate .
3145 * Return: return value of mpi3mr_prepare_sg_scmd.
3147 static int mpi3mr_build_sg_scmd(struct mpi3mr_ioc *mrioc,
3148 struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
3152 ret = mpi3mr_prepare_sg_scmd(mrioc, scmd, scsiio_req);
3156 if (scsiio_req->msg_flags == MPI3_SCSIIO_MSGFLAGS_METASGL_VALID) {
3157 /* There is a valid meta sg */
3158 scsiio_req->flags |=
3159 cpu_to_le32(MPI3_SCSIIO_FLAGS_DMAOPERATION_HOST_PI);
3160 ret = mpi3mr_prepare_sg_scmd(mrioc, scmd, scsiio_req);
3167 * mpi3mr_tm_response_name - get TM response as a string
3168 * @resp_code: TM response code
3170 * Convert known task management response code as a readable
3173 * Return: response code string.
3175 static const char *mpi3mr_tm_response_name(u8 resp_code)
3179 switch (resp_code) {
3180 case MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE:
3181 desc = "task management request completed";
3183 case MPI3_SCSITASKMGMT_RSPCODE_INVALID_FRAME:
3184 desc = "invalid frame";
3186 case MPI3_SCSITASKMGMT_RSPCODE_TM_FUNCTION_NOT_SUPPORTED:
3187 desc = "task management request not supported";
3189 case MPI3_SCSITASKMGMT_RSPCODE_TM_FAILED:
3190 desc = "task management request failed";
3192 case MPI3_SCSITASKMGMT_RSPCODE_TM_SUCCEEDED:
3193 desc = "task management request succeeded";
3195 case MPI3_SCSITASKMGMT_RSPCODE_TM_INVALID_LUN:
3196 desc = "invalid LUN";
3198 case MPI3_SCSITASKMGMT_RSPCODE_TM_OVERLAPPED_TAG:
3199 desc = "overlapped tag attempted";
3201 case MPI3_SCSITASKMGMT_RSPCODE_IO_QUEUED_ON_IOC:
3202 desc = "task queued, however not sent to target";
3204 case MPI3_SCSITASKMGMT_RSPCODE_TM_NVME_DENIED:
3205 desc = "task management request denied by NVMe device";
3215 inline void mpi3mr_poll_pend_io_completions(struct mpi3mr_ioc *mrioc)
3218 int num_of_reply_queues =
3219 mrioc->num_op_reply_q + mrioc->op_reply_q_offset;
3221 for (i = mrioc->op_reply_q_offset; i < num_of_reply_queues; i++)
3222 mpi3mr_process_op_reply_q(mrioc,
3223 mrioc->intr_info[i].op_reply_q);
3227 * mpi3mr_issue_tm - Issue Task Management request
3228 * @mrioc: Adapter instance reference
3229 * @tm_type: Task Management type
3230 * @handle: Device handle
3232 * @htag: Host tag of the TM request
3233 * @timeout: TM timeout value
3234 * @drv_cmd: Internal command tracker
3235 * @resp_code: Response code place holder
3236 * @scmd: SCSI command
3238 * Issues a Task Management Request to the controller for a
3239 * specified target, lun and command and wait for its completion
3240 * and check TM response. Recover the TM if it timed out by
3241 * issuing controller reset.
3243 * Return: 0 on success, non-zero on errors
3245 int mpi3mr_issue_tm(struct mpi3mr_ioc *mrioc, u8 tm_type,
3246 u16 handle, uint lun, u16 htag, ulong timeout,
3247 struct mpi3mr_drv_cmd *drv_cmd,
3248 u8 *resp_code, struct scsi_cmnd *scmd)
3250 struct mpi3_scsi_task_mgmt_request tm_req;
3251 struct mpi3_scsi_task_mgmt_reply *tm_reply = NULL;
3253 struct mpi3mr_tgt_dev *tgtdev = NULL;
3254 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
3255 struct scmd_priv *cmd_priv = NULL;
3256 struct scsi_device *sdev = NULL;
3257 struct mpi3mr_sdev_priv_data *sdev_priv_data = NULL;
3259 ioc_info(mrioc, "%s :Issue TM: TM type (0x%x) for devhandle 0x%04x\n",
3260 __func__, tm_type, handle);
3261 if (mrioc->unrecoverable) {
3263 ioc_err(mrioc, "%s :Issue TM: Unrecoverable controller\n",
3268 memset(&tm_req, 0, sizeof(tm_req));
3269 mutex_lock(&drv_cmd->mutex);
3270 if (drv_cmd->state & MPI3MR_CMD_PENDING) {
3272 ioc_err(mrioc, "%s :Issue TM: Command is in use\n", __func__);
3273 mutex_unlock(&drv_cmd->mutex);
3276 if (mrioc->reset_in_progress) {
3278 ioc_err(mrioc, "%s :Issue TM: Reset in progress\n", __func__);
3279 mutex_unlock(&drv_cmd->mutex);
3283 drv_cmd->state = MPI3MR_CMD_PENDING;
3284 drv_cmd->is_waiting = 1;
3285 drv_cmd->callback = NULL;
3286 tm_req.dev_handle = cpu_to_le16(handle);
3287 tm_req.task_type = tm_type;
3288 tm_req.host_tag = cpu_to_le16(htag);
3290 int_to_scsilun(lun, (struct scsi_lun *)tm_req.lun);
3291 tm_req.function = MPI3_FUNCTION_SCSI_TASK_MGMT;
3293 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
3296 sdev = scmd->device;
3297 sdev_priv_data = sdev->hostdata;
3298 scsi_tgt_priv_data = ((sdev_priv_data) ?
3299 sdev_priv_data->tgt_priv_data : NULL);
3301 if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
3302 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
3303 tgtdev->starget->hostdata;
3306 if (scsi_tgt_priv_data)
3307 atomic_inc(&scsi_tgt_priv_data->block_io);
3309 if (tgtdev && (tgtdev->dev_type == MPI3_DEVICE_DEVFORM_PCIE)) {
3310 if (cmd_priv && tgtdev->dev_spec.pcie_inf.abort_to)
3311 timeout = tgtdev->dev_spec.pcie_inf.abort_to;
3312 else if (!cmd_priv && tgtdev->dev_spec.pcie_inf.reset_to)
3313 timeout = tgtdev->dev_spec.pcie_inf.reset_to;
3316 init_completion(&drv_cmd->done);
3317 retval = mpi3mr_admin_request_post(mrioc, &tm_req, sizeof(tm_req), 1);
3319 ioc_err(mrioc, "%s :Issue TM: Admin Post failed\n", __func__);
3322 wait_for_completion_timeout(&drv_cmd->done, (timeout * HZ));
3324 if (!(drv_cmd->state & MPI3MR_CMD_COMPLETE)) {
3325 drv_cmd->is_waiting = 0;
3327 if (!(drv_cmd->state & MPI3MR_CMD_RESET)) {
3329 "task management request timed out after %ld seconds\n",
3331 if (mrioc->logging_level & MPI3_DEBUG_TM)
3332 dprint_dump_req(&tm_req, sizeof(tm_req)/4);
3333 mpi3mr_soft_reset_handler(mrioc,
3334 MPI3MR_RESET_FROM_TM_TIMEOUT, 1);
3339 if (!(drv_cmd->state & MPI3MR_CMD_REPLY_VALID)) {
3340 dprint_tm(mrioc, "invalid task management reply message\n");
3345 tm_reply = (struct mpi3_scsi_task_mgmt_reply *)drv_cmd->reply;
3347 switch (drv_cmd->ioc_status) {
3348 case MPI3_IOCSTATUS_SUCCESS:
3349 *resp_code = le32_to_cpu(tm_reply->response_data) &
3350 MPI3MR_RI_MASK_RESPCODE;
3352 case MPI3_IOCSTATUS_SCSI_IOC_TERMINATED:
3353 *resp_code = MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE;
3357 "task management request to handle(0x%04x) is failed with ioc_status(0x%04x) log_info(0x%08x)\n",
3358 handle, drv_cmd->ioc_status, drv_cmd->ioc_loginfo);
3363 switch (*resp_code) {
3364 case MPI3_SCSITASKMGMT_RSPCODE_TM_SUCCEEDED:
3365 case MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE:
3367 case MPI3_SCSITASKMGMT_RSPCODE_IO_QUEUED_ON_IOC:
3368 if (tm_type != MPI3_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
3377 "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",
3378 tm_type, handle, drv_cmd->ioc_status, drv_cmd->ioc_loginfo,
3379 le32_to_cpu(tm_reply->termination_count),
3380 mpi3mr_tm_response_name(*resp_code), *resp_code);
3383 mpi3mr_ioc_disable_intr(mrioc);
3384 mpi3mr_poll_pend_io_completions(mrioc);
3385 mpi3mr_ioc_enable_intr(mrioc);
3386 mpi3mr_poll_pend_io_completions(mrioc);
3389 case MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
3390 if (!scsi_tgt_priv_data)
3392 scsi_tgt_priv_data->pend_count = 0;
3393 blk_mq_tagset_busy_iter(&mrioc->shost->tag_set,
3394 mpi3mr_count_tgt_pending,
3395 (void *)scsi_tgt_priv_data->starget);
3397 case MPI3_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
3398 if (!sdev_priv_data)
3400 sdev_priv_data->pend_count = 0;
3401 blk_mq_tagset_busy_iter(&mrioc->shost->tag_set,
3402 mpi3mr_count_dev_pending, (void *)sdev);
3409 drv_cmd->state = MPI3MR_CMD_NOTUSED;
3410 mutex_unlock(&drv_cmd->mutex);
3411 if (scsi_tgt_priv_data)
3412 atomic_dec_if_positive(&scsi_tgt_priv_data->block_io);
3414 mpi3mr_tgtdev_put(tgtdev);
3420 * mpi3mr_bios_param - BIOS param callback
3421 * @sdev: SCSI device reference
3422 * @bdev: Block device reference
3423 * @capacity: Capacity in logical sectors
3424 * @params: Parameter array
3426 * Just the parameters with heads/secots/cylinders.
3430 static int mpi3mr_bios_param(struct scsi_device *sdev,
3431 struct block_device *bdev, sector_t capacity, int params[])
3441 dummy = heads * sectors;
3442 cylinders = capacity;
3443 sector_div(cylinders, dummy);
3445 if ((ulong)capacity >= 0x200000) {
3448 dummy = heads * sectors;
3449 cylinders = capacity;
3450 sector_div(cylinders, dummy);
3454 params[1] = sectors;
3455 params[2] = cylinders;
3460 * mpi3mr_map_queues - Map queues callback handler
3461 * @shost: SCSI host reference
3463 * Maps default and poll queues.
3465 * Return: return zero.
3467 static int mpi3mr_map_queues(struct Scsi_Host *shost)
3469 struct mpi3mr_ioc *mrioc = shost_priv(shost);
3470 int i, qoff, offset;
3471 struct blk_mq_queue_map *map = NULL;
3473 offset = mrioc->op_reply_q_offset;
3475 for (i = 0, qoff = 0; i < HCTX_MAX_TYPES; i++) {
3476 map = &shost->tag_set.map[i];
3480 if (i == HCTX_TYPE_DEFAULT)
3481 map->nr_queues = mrioc->default_qcount;
3482 else if (i == HCTX_TYPE_POLL)
3483 map->nr_queues = mrioc->active_poll_qcount;
3485 if (!map->nr_queues) {
3486 BUG_ON(i == HCTX_TYPE_DEFAULT);
3491 * The poll queue(s) doesn't have an IRQ (and hence IRQ
3492 * affinity), so use the regular blk-mq cpu mapping
3494 map->queue_offset = qoff;
3495 if (i != HCTX_TYPE_POLL)
3496 blk_mq_pci_map_queues(map, mrioc->pdev, offset);
3498 blk_mq_map_queues(map);
3500 qoff += map->nr_queues;
3501 offset += map->nr_queues;
3509 * mpi3mr_get_fw_pending_ios - Calculate pending I/O count
3510 * @mrioc: Adapter instance reference
3512 * Calculate the pending I/Os for the controller and return.
3514 * Return: Number of pending I/Os
3516 static inline int mpi3mr_get_fw_pending_ios(struct mpi3mr_ioc *mrioc)
3521 for (i = 0; i < mrioc->num_op_reply_q; i++)
3522 pend_ios += atomic_read(&mrioc->op_reply_qinfo[i].pend_ios);
3527 * mpi3mr_print_pending_host_io - print pending I/Os
3528 * @mrioc: Adapter instance reference
3530 * Print number of pending I/Os and each I/O details prior to
3531 * reset for debug purpose.
3535 static void mpi3mr_print_pending_host_io(struct mpi3mr_ioc *mrioc)
3537 struct Scsi_Host *shost = mrioc->shost;
3539 ioc_info(mrioc, "%s :Pending commands prior to reset: %d\n",
3540 __func__, mpi3mr_get_fw_pending_ios(mrioc));
3541 blk_mq_tagset_busy_iter(&shost->tag_set,
3542 mpi3mr_print_scmd, (void *)mrioc);
3546 * mpi3mr_wait_for_host_io - block for I/Os to complete
3547 * @mrioc: Adapter instance reference
3548 * @timeout: time out in seconds
3549 * Waits for pending I/Os for the given adapter to complete or
3550 * to hit the timeout.
3554 void mpi3mr_wait_for_host_io(struct mpi3mr_ioc *mrioc, u32 timeout)
3556 enum mpi3mr_iocstate iocstate;
3559 iocstate = mpi3mr_get_iocstate(mrioc);
3560 if (iocstate != MRIOC_STATE_READY)
3563 if (!mpi3mr_get_fw_pending_ios(mrioc))
3566 "%s :Waiting for %d seconds prior to reset for %d I/O\n",
3567 __func__, timeout, mpi3mr_get_fw_pending_ios(mrioc));
3569 for (i = 0; i < timeout; i++) {
3570 if (!mpi3mr_get_fw_pending_ios(mrioc))
3572 iocstate = mpi3mr_get_iocstate(mrioc);
3573 if (iocstate != MRIOC_STATE_READY)
3578 ioc_info(mrioc, "%s :Pending I/Os after wait is: %d\n", __func__,
3579 mpi3mr_get_fw_pending_ios(mrioc));
3583 * mpi3mr_eh_host_reset - Host reset error handling callback
3584 * @scmd: SCSI command reference
3586 * Issue controller reset if the scmd is for a Physical Device,
3587 * if the scmd is for RAID volume, then wait for
3588 * MPI3MR_RAID_ERRREC_RESET_TIMEOUT and checke whether any
3589 * pending I/Os prior to issuing reset to the controller.
3591 * Return: SUCCESS of successful reset else FAILED
3593 static int mpi3mr_eh_host_reset(struct scsi_cmnd *scmd)
3595 struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
3596 struct mpi3mr_stgt_priv_data *stgt_priv_data;
3597 struct mpi3mr_sdev_priv_data *sdev_priv_data;
3598 u8 dev_type = MPI3_DEVICE_DEVFORM_VD;
3599 int retval = FAILED, ret;
3601 sdev_priv_data = scmd->device->hostdata;
3602 if (sdev_priv_data && sdev_priv_data->tgt_priv_data) {
3603 stgt_priv_data = sdev_priv_data->tgt_priv_data;
3604 dev_type = stgt_priv_data->dev_type;
3607 if (dev_type == MPI3_DEVICE_DEVFORM_VD) {
3608 mpi3mr_wait_for_host_io(mrioc,
3609 MPI3MR_RAID_ERRREC_RESET_TIMEOUT);
3610 if (!mpi3mr_get_fw_pending_ios(mrioc)) {
3616 mpi3mr_print_pending_host_io(mrioc);
3617 ret = mpi3mr_soft_reset_handler(mrioc,
3618 MPI3MR_RESET_FROM_EH_HOS, 1);
3624 sdev_printk(KERN_INFO, scmd->device,
3625 "Host reset is %s for scmd(%p)\n",
3626 ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
3632 * mpi3mr_eh_target_reset - Target reset error handling callback
3633 * @scmd: SCSI command reference
3635 * Issue Target reset Task Management and verify the scmd is
3636 * terminated successfully and return status accordingly.
3638 * Return: SUCCESS of successful termination of the scmd else
3641 static int mpi3mr_eh_target_reset(struct scsi_cmnd *scmd)
3643 struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
3644 struct mpi3mr_stgt_priv_data *stgt_priv_data;
3645 struct mpi3mr_sdev_priv_data *sdev_priv_data;
3648 int retval = FAILED, ret = 0;
3650 sdev_printk(KERN_INFO, scmd->device,
3651 "Attempting Target Reset! scmd(%p)\n", scmd);
3652 scsi_print_command(scmd);
3654 sdev_priv_data = scmd->device->hostdata;
3655 if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
3656 sdev_printk(KERN_INFO, scmd->device,
3657 "SCSI device is not available\n");
3662 stgt_priv_data = sdev_priv_data->tgt_priv_data;
3663 dev_handle = stgt_priv_data->dev_handle;
3664 if (stgt_priv_data->dev_removed) {
3665 sdev_printk(KERN_INFO, scmd->device,
3666 "%s:target(handle = 0x%04x) is removed, target reset is not issued\n",
3667 mrioc->name, dev_handle);
3671 sdev_printk(KERN_INFO, scmd->device,
3672 "Target Reset is issued to handle(0x%04x)\n",
3675 ret = mpi3mr_issue_tm(mrioc,
3676 MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET, dev_handle,
3677 sdev_priv_data->lun_id, MPI3MR_HOSTTAG_BLK_TMS,
3678 MPI3MR_RESETTM_TIMEOUT, &mrioc->host_tm_cmds, &resp_code, scmd);
3683 if (stgt_priv_data->pend_count) {
3684 sdev_printk(KERN_INFO, scmd->device,
3685 "%s: target has %d pending commands, target reset is failed\n",
3686 mrioc->name, stgt_priv_data->pend_count);
3692 sdev_printk(KERN_INFO, scmd->device,
3693 "%s: target reset is %s for scmd(%p)\n", mrioc->name,
3694 ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
3700 * mpi3mr_eh_dev_reset- Device reset error handling callback
3701 * @scmd: SCSI command reference
3703 * Issue lun reset Task Management and verify the scmd is
3704 * terminated successfully and return status accordingly.
3706 * Return: SUCCESS of successful termination of the scmd else
3709 static int mpi3mr_eh_dev_reset(struct scsi_cmnd *scmd)
3711 struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
3712 struct mpi3mr_stgt_priv_data *stgt_priv_data;
3713 struct mpi3mr_sdev_priv_data *sdev_priv_data;
3716 int retval = FAILED, ret = 0;
3718 sdev_printk(KERN_INFO, scmd->device,
3719 "Attempting Device(lun) Reset! scmd(%p)\n", scmd);
3720 scsi_print_command(scmd);
3722 sdev_priv_data = scmd->device->hostdata;
3723 if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
3724 sdev_printk(KERN_INFO, scmd->device,
3725 "SCSI device is not available\n");
3730 stgt_priv_data = sdev_priv_data->tgt_priv_data;
3731 dev_handle = stgt_priv_data->dev_handle;
3732 if (stgt_priv_data->dev_removed) {
3733 sdev_printk(KERN_INFO, scmd->device,
3734 "%s: device(handle = 0x%04x) is removed, device(LUN) reset is not issued\n",
3735 mrioc->name, dev_handle);
3739 sdev_printk(KERN_INFO, scmd->device,
3740 "Device(lun) Reset is issued to handle(0x%04x)\n", dev_handle);
3742 ret = mpi3mr_issue_tm(mrioc,
3743 MPI3_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, dev_handle,
3744 sdev_priv_data->lun_id, MPI3MR_HOSTTAG_BLK_TMS,
3745 MPI3MR_RESETTM_TIMEOUT, &mrioc->host_tm_cmds, &resp_code, scmd);
3750 if (sdev_priv_data->pend_count) {
3751 sdev_printk(KERN_INFO, scmd->device,
3752 "%s: device has %d pending commands, device(LUN) reset is failed\n",
3753 mrioc->name, sdev_priv_data->pend_count);
3758 sdev_printk(KERN_INFO, scmd->device,
3759 "%s: device(LUN) reset is %s for scmd(%p)\n", mrioc->name,
3760 ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
3766 * mpi3mr_scan_start - Scan start callback handler
3767 * @shost: SCSI host reference
3769 * Issue port enable request asynchronously.
3773 static void mpi3mr_scan_start(struct Scsi_Host *shost)
3775 struct mpi3mr_ioc *mrioc = shost_priv(shost);
3777 mrioc->scan_started = 1;
3778 ioc_info(mrioc, "%s :Issuing Port Enable\n", __func__);
3779 if (mpi3mr_issue_port_enable(mrioc, 1)) {
3780 ioc_err(mrioc, "%s :Issuing port enable failed\n", __func__);
3781 mrioc->scan_started = 0;
3782 mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
3787 * mpi3mr_scan_finished - Scan finished callback handler
3788 * @shost: SCSI host reference
3789 * @time: Jiffies from the scan start
3791 * Checks whether the port enable is completed or timedout or
3792 * failed and set the scan status accordingly after taking any
3793 * recovery if required.
3795 * Return: 1 on scan finished or timed out, 0 for in progress
3797 static int mpi3mr_scan_finished(struct Scsi_Host *shost,
3800 struct mpi3mr_ioc *mrioc = shost_priv(shost);
3801 u32 pe_timeout = MPI3MR_PORTENABLE_TIMEOUT;
3802 u32 ioc_status = readl(&mrioc->sysif_regs->ioc_status);
3804 if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) ||
3805 (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)) {
3806 ioc_err(mrioc, "port enable failed due to fault or reset\n");
3807 mpi3mr_print_fault_info(mrioc);
3808 mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
3809 mrioc->scan_started = 0;
3810 mrioc->init_cmds.is_waiting = 0;
3811 mrioc->init_cmds.callback = NULL;
3812 mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
3815 if (time >= (pe_timeout * HZ)) {
3816 ioc_err(mrioc, "port enable failed due to time out\n");
3817 mpi3mr_check_rh_fault_ioc(mrioc,
3818 MPI3MR_RESET_FROM_PE_TIMEOUT);
3819 mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
3820 mrioc->scan_started = 0;
3821 mrioc->init_cmds.is_waiting = 0;
3822 mrioc->init_cmds.callback = NULL;
3823 mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
3826 if (mrioc->scan_started)
3829 if (mrioc->scan_failed) {
3831 "port enable failed with status=0x%04x\n",
3832 mrioc->scan_failed);
3834 ioc_info(mrioc, "port enable is successfully completed\n");
3836 mpi3mr_start_watchdog(mrioc);
3837 mrioc->is_driver_loading = 0;
3838 mrioc->stop_bsgs = 0;
3843 * mpi3mr_slave_destroy - Slave destroy callback handler
3844 * @sdev: SCSI device reference
3846 * Cleanup and free per device(lun) private data.
3850 static void mpi3mr_slave_destroy(struct scsi_device *sdev)
3852 struct Scsi_Host *shost;
3853 struct mpi3mr_ioc *mrioc;
3854 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
3855 struct mpi3mr_tgt_dev *tgt_dev;
3856 unsigned long flags;
3857 struct scsi_target *starget;
3859 if (!sdev->hostdata)
3862 starget = scsi_target(sdev);
3863 shost = dev_to_shost(&starget->dev);
3864 mrioc = shost_priv(shost);
3865 scsi_tgt_priv_data = starget->hostdata;
3867 scsi_tgt_priv_data->num_luns--;
3869 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
3870 tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
3871 if (tgt_dev && (!scsi_tgt_priv_data->num_luns))
3872 tgt_dev->starget = NULL;
3874 mpi3mr_tgtdev_put(tgt_dev);
3875 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
3877 kfree(sdev->hostdata);
3878 sdev->hostdata = NULL;
3882 * mpi3mr_target_destroy - Target destroy callback handler
3883 * @starget: SCSI target reference
3885 * Cleanup and free per target private data.
3889 static void mpi3mr_target_destroy(struct scsi_target *starget)
3891 struct Scsi_Host *shost;
3892 struct mpi3mr_ioc *mrioc;
3893 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
3894 struct mpi3mr_tgt_dev *tgt_dev;
3895 unsigned long flags;
3897 if (!starget->hostdata)
3900 shost = dev_to_shost(&starget->dev);
3901 mrioc = shost_priv(shost);
3902 scsi_tgt_priv_data = starget->hostdata;
3904 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
3905 tgt_dev = __mpi3mr_get_tgtdev_from_tgtpriv(mrioc, scsi_tgt_priv_data);
3906 if (tgt_dev && (tgt_dev->starget == starget) &&
3907 (tgt_dev->perst_id == starget->id))
3908 tgt_dev->starget = NULL;
3910 scsi_tgt_priv_data->tgt_dev = NULL;
3911 scsi_tgt_priv_data->perst_id = 0;
3912 mpi3mr_tgtdev_put(tgt_dev);
3913 mpi3mr_tgtdev_put(tgt_dev);
3915 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
3917 kfree(starget->hostdata);
3918 starget->hostdata = NULL;
3922 * mpi3mr_slave_configure - Slave configure callback handler
3923 * @sdev: SCSI device reference
3925 * Configure queue depth, max hardware sectors and virt boundary
3930 static int mpi3mr_slave_configure(struct scsi_device *sdev)
3932 struct scsi_target *starget;
3933 struct Scsi_Host *shost;
3934 struct mpi3mr_ioc *mrioc;
3935 struct mpi3mr_tgt_dev *tgt_dev;
3936 unsigned long flags;
3939 starget = scsi_target(sdev);
3940 shost = dev_to_shost(&starget->dev);
3941 mrioc = shost_priv(shost);
3943 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
3944 tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
3945 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
3949 mpi3mr_change_queue_depth(sdev, tgt_dev->q_depth);
3951 sdev->eh_timeout = MPI3MR_EH_SCMD_TIMEOUT;
3952 blk_queue_rq_timeout(sdev->request_queue, MPI3MR_SCMD_TIMEOUT);
3954 switch (tgt_dev->dev_type) {
3955 case MPI3_DEVICE_DEVFORM_PCIE:
3956 /*The block layer hw sector size = 512*/
3957 if ((tgt_dev->dev_spec.pcie_inf.dev_info &
3958 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) ==
3959 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) {
3960 blk_queue_max_hw_sectors(sdev->request_queue,
3961 tgt_dev->dev_spec.pcie_inf.mdts / 512);
3962 if (tgt_dev->dev_spec.pcie_inf.pgsz == 0)
3963 blk_queue_virt_boundary(sdev->request_queue,
3964 ((1 << MPI3MR_DEFAULT_PGSZEXP) - 1));
3966 blk_queue_virt_boundary(sdev->request_queue,
3967 ((1 << tgt_dev->dev_spec.pcie_inf.pgsz) - 1));
3974 mpi3mr_tgtdev_put(tgt_dev);
3980 * mpi3mr_slave_alloc -Slave alloc callback handler
3981 * @sdev: SCSI device reference
3983 * Allocate per device(lun) private data and initialize it.
3985 * Return: 0 on success -ENOMEM on memory allocation failure.
3987 static int mpi3mr_slave_alloc(struct scsi_device *sdev)
3989 struct Scsi_Host *shost;
3990 struct mpi3mr_ioc *mrioc;
3991 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
3992 struct mpi3mr_tgt_dev *tgt_dev;
3993 struct mpi3mr_sdev_priv_data *scsi_dev_priv_data;
3994 unsigned long flags;
3995 struct scsi_target *starget;
3998 starget = scsi_target(sdev);
3999 shost = dev_to_shost(&starget->dev);
4000 mrioc = shost_priv(shost);
4001 scsi_tgt_priv_data = starget->hostdata;
4003 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4004 tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4007 if (tgt_dev->starget == NULL)
4008 tgt_dev->starget = starget;
4009 mpi3mr_tgtdev_put(tgt_dev);
4012 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4016 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4018 scsi_dev_priv_data = kzalloc(sizeof(*scsi_dev_priv_data), GFP_KERNEL);
4019 if (!scsi_dev_priv_data)
4022 scsi_dev_priv_data->lun_id = sdev->lun;
4023 scsi_dev_priv_data->tgt_priv_data = scsi_tgt_priv_data;
4024 sdev->hostdata = scsi_dev_priv_data;
4026 scsi_tgt_priv_data->num_luns++;
4032 * mpi3mr_target_alloc - Target alloc callback handler
4033 * @starget: SCSI target reference
4035 * Allocate per target private data and initialize it.
4037 * Return: 0 on success -ENOMEM on memory allocation failure.
4039 static int mpi3mr_target_alloc(struct scsi_target *starget)
4041 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4042 struct mpi3mr_ioc *mrioc = shost_priv(shost);
4043 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4044 struct mpi3mr_tgt_dev *tgt_dev;
4045 unsigned long flags;
4048 scsi_tgt_priv_data = kzalloc(sizeof(*scsi_tgt_priv_data), GFP_KERNEL);
4049 if (!scsi_tgt_priv_data)
4052 starget->hostdata = scsi_tgt_priv_data;
4054 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4055 tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4056 if (tgt_dev && !tgt_dev->is_hidden) {
4057 scsi_tgt_priv_data->starget = starget;
4058 scsi_tgt_priv_data->dev_handle = tgt_dev->dev_handle;
4059 scsi_tgt_priv_data->perst_id = tgt_dev->perst_id;
4060 scsi_tgt_priv_data->dev_type = tgt_dev->dev_type;
4061 scsi_tgt_priv_data->tgt_dev = tgt_dev;
4062 tgt_dev->starget = starget;
4063 atomic_set(&scsi_tgt_priv_data->block_io, 0);
4065 scsi_tgt_priv_data->io_throttle_enabled =
4066 tgt_dev->io_throttle_enabled;
4067 if (tgt_dev->dev_type == MPI3_DEVICE_DEVFORM_VD)
4068 scsi_tgt_priv_data->throttle_group =
4069 tgt_dev->dev_spec.vd_inf.tg;
4072 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4078 * mpi3mr_check_return_unmap - Whether an unmap is allowed
4079 * @mrioc: Adapter instance reference
4080 * @scmd: SCSI Command reference
4082 * The controller hardware cannot handle certain unmap commands
4083 * for NVMe drives, this routine checks those and return true
4084 * and completes the SCSI command with proper status and sense
4087 * Return: TRUE for not allowed unmap, FALSE otherwise.
4089 static bool mpi3mr_check_return_unmap(struct mpi3mr_ioc *mrioc,
4090 struct scsi_cmnd *scmd)
4093 u16 param_len, desc_len, trunc_param_len;
4095 trunc_param_len = param_len = get_unaligned_be16(scmd->cmnd + 7);
4097 if (mrioc->pdev->revision) {
4098 if ((param_len > 24) && ((param_len - 8) & 0xF)) {
4099 trunc_param_len -= (param_len - 8) & 0xF;
4100 dprint_scsi_command(mrioc, scmd, MPI3_DEBUG_SCSI_ERROR);
4101 dprint_scsi_err(mrioc,
4102 "truncating param_len from (%d) to (%d)\n",
4103 param_len, trunc_param_len);
4104 put_unaligned_be16(trunc_param_len, scmd->cmnd + 7);
4105 dprint_scsi_command(mrioc, scmd, MPI3_DEBUG_SCSI_ERROR);
4112 "%s: cdb received with zero parameter length\n",
4114 scsi_print_command(scmd);
4115 scmd->result = DID_OK << 16;
4120 if (param_len < 24) {
4122 "%s: cdb received with invalid param_len: %d\n",
4123 __func__, param_len);
4124 scsi_print_command(scmd);
4125 scmd->result = SAM_STAT_CHECK_CONDITION;
4126 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
4131 if (param_len != scsi_bufflen(scmd)) {
4133 "%s: cdb received with param_len: %d bufflen: %d\n",
4134 __func__, param_len, scsi_bufflen(scmd));
4135 scsi_print_command(scmd);
4136 scmd->result = SAM_STAT_CHECK_CONDITION;
4137 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
4142 buf = kzalloc(scsi_bufflen(scmd), GFP_ATOMIC);
4144 scsi_print_command(scmd);
4145 scmd->result = SAM_STAT_CHECK_CONDITION;
4146 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
4151 scsi_sg_copy_to_buffer(scmd, buf, scsi_bufflen(scmd));
4152 desc_len = get_unaligned_be16(&buf[2]);
4154 if (desc_len < 16) {
4156 "%s: Invalid descriptor length in param list: %d\n",
4157 __func__, desc_len);
4158 scsi_print_command(scmd);
4159 scmd->result = SAM_STAT_CHECK_CONDITION;
4160 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
4167 if (param_len > (desc_len + 8)) {
4168 trunc_param_len = desc_len + 8;
4169 scsi_print_command(scmd);
4170 dprint_scsi_err(mrioc,
4171 "truncating param_len(%d) to desc_len+8(%d)\n",
4172 param_len, trunc_param_len);
4173 put_unaligned_be16(trunc_param_len, scmd->cmnd + 7);
4174 scsi_print_command(scmd);
4182 * mpi3mr_allow_scmd_to_fw - Command is allowed during shutdown
4183 * @scmd: SCSI Command reference
4185 * Checks whether a cdb is allowed during shutdown or not.
4187 * Return: TRUE for allowed commands, FALSE otherwise.
4190 inline bool mpi3mr_allow_scmd_to_fw(struct scsi_cmnd *scmd)
4192 switch (scmd->cmnd[0]) {
4193 case SYNCHRONIZE_CACHE:
4202 * mpi3mr_qcmd - I/O request despatcher
4203 * @shost: SCSI Host reference
4204 * @scmd: SCSI Command reference
4206 * Issues the SCSI Command as an MPI3 request.
4208 * Return: 0 on successful queueing of the request or if the
4209 * request is completed with failure.
4210 * SCSI_MLQUEUE_DEVICE_BUSY when the device is busy.
4211 * SCSI_MLQUEUE_HOST_BUSY when the host queue is full.
4213 static int mpi3mr_qcmd(struct Scsi_Host *shost,
4214 struct scsi_cmnd *scmd)
4216 struct mpi3mr_ioc *mrioc = shost_priv(shost);
4217 struct mpi3mr_stgt_priv_data *stgt_priv_data;
4218 struct mpi3mr_sdev_priv_data *sdev_priv_data;
4219 struct scmd_priv *scmd_priv_data = NULL;
4220 struct mpi3_scsi_io_request *scsiio_req = NULL;
4221 struct op_req_qinfo *op_req_q = NULL;
4225 u32 scsiio_flags = 0, data_len_blks = 0;
4226 struct request *rq = scsi_cmd_to_rq(scmd);
4229 u32 tracked_io_sz = 0;
4230 u32 ioc_pend_data_len = 0, tg_pend_data_len = 0;
4231 struct mpi3mr_throttle_group_info *tg = NULL;
4233 if (mrioc->unrecoverable) {
4234 scmd->result = DID_ERROR << 16;
4239 sdev_priv_data = scmd->device->hostdata;
4240 if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
4241 scmd->result = DID_NO_CONNECT << 16;
4246 if (mrioc->stop_drv_processing &&
4247 !(mpi3mr_allow_scmd_to_fw(scmd))) {
4248 scmd->result = DID_NO_CONNECT << 16;
4253 if (mrioc->reset_in_progress) {
4254 retval = SCSI_MLQUEUE_HOST_BUSY;
4258 stgt_priv_data = sdev_priv_data->tgt_priv_data;
4260 dev_handle = stgt_priv_data->dev_handle;
4261 if (dev_handle == MPI3MR_INVALID_DEV_HANDLE) {
4262 scmd->result = DID_NO_CONNECT << 16;
4266 if (stgt_priv_data->dev_removed) {
4267 scmd->result = DID_NO_CONNECT << 16;
4272 if (atomic_read(&stgt_priv_data->block_io)) {
4273 if (mrioc->stop_drv_processing) {
4274 scmd->result = DID_NO_CONNECT << 16;
4278 retval = SCSI_MLQUEUE_DEVICE_BUSY;
4282 if (stgt_priv_data->dev_type == MPI3_DEVICE_DEVFORM_PCIE)
4284 if ((scmd->cmnd[0] == UNMAP) && is_pcie_dev &&
4285 (mrioc->pdev->device == MPI3_MFGPAGE_DEVID_SAS4116) &&
4286 mpi3mr_check_return_unmap(mrioc, scmd))
4289 host_tag = mpi3mr_host_tag_for_scmd(mrioc, scmd);
4290 if (host_tag == MPI3MR_HOSTTAG_INVALID) {
4291 scmd->result = DID_ERROR << 16;
4296 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
4297 scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_READ;
4298 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
4299 scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_WRITE;
4301 scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_NO_DATA_TRANSFER;
4303 scsiio_flags |= MPI3_SCSIIO_FLAGS_TASKATTRIBUTE_SIMPLEQ;
4305 if (sdev_priv_data->ncq_prio_enable) {
4306 iprio_class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
4307 if (iprio_class == IOPRIO_CLASS_RT)
4308 scsiio_flags |= 1 << MPI3_SCSIIO_FLAGS_CMDPRI_SHIFT;
4311 if (scmd->cmd_len > 16)
4312 scsiio_flags |= MPI3_SCSIIO_FLAGS_CDB_GREATER_THAN_16;
4314 scmd_priv_data = scsi_cmd_priv(scmd);
4315 memset(scmd_priv_data->mpi3mr_scsiio_req, 0, MPI3MR_ADMIN_REQ_FRAME_SZ);
4316 scsiio_req = (struct mpi3_scsi_io_request *)scmd_priv_data->mpi3mr_scsiio_req;
4317 scsiio_req->function = MPI3_FUNCTION_SCSI_IO;
4318 scsiio_req->host_tag = cpu_to_le16(host_tag);
4320 mpi3mr_setup_eedp(mrioc, scmd, scsiio_req);
4322 memcpy(scsiio_req->cdb.cdb32, scmd->cmnd, scmd->cmd_len);
4323 scsiio_req->data_length = cpu_to_le32(scsi_bufflen(scmd));
4324 scsiio_req->dev_handle = cpu_to_le16(dev_handle);
4325 scsiio_req->flags = cpu_to_le32(scsiio_flags);
4326 int_to_scsilun(sdev_priv_data->lun_id,
4327 (struct scsi_lun *)scsiio_req->lun);
4329 if (mpi3mr_build_sg_scmd(mrioc, scmd, scsiio_req)) {
4330 mpi3mr_clear_scmd_priv(mrioc, scmd);
4331 retval = SCSI_MLQUEUE_HOST_BUSY;
4334 op_req_q = &mrioc->req_qinfo[scmd_priv_data->req_q_idx];
4335 data_len_blks = scsi_bufflen(scmd) >> 9;
4336 if ((data_len_blks >= mrioc->io_throttle_data_length) &&
4337 stgt_priv_data->io_throttle_enabled) {
4338 tracked_io_sz = data_len_blks;
4339 tg = stgt_priv_data->throttle_group;
4341 ioc_pend_data_len = atomic_add_return(data_len_blks,
4342 &mrioc->pend_large_data_sz);
4343 tg_pend_data_len = atomic_add_return(data_len_blks,
4344 &tg->pend_large_data_sz);
4345 if (!tg->io_divert && ((ioc_pend_data_len >=
4346 mrioc->io_throttle_high) ||
4347 (tg_pend_data_len >= tg->high))) {
4349 tg->need_qd_reduction = 1;
4350 mpi3mr_set_io_divert_for_all_vd_in_tg(mrioc,
4352 mpi3mr_queue_qd_reduction_event(mrioc, tg);
4355 ioc_pend_data_len = atomic_add_return(data_len_blks,
4356 &mrioc->pend_large_data_sz);
4357 if (ioc_pend_data_len >= mrioc->io_throttle_high)
4358 stgt_priv_data->io_divert = 1;
4362 if (stgt_priv_data->io_divert) {
4363 scsiio_req->msg_flags |=
4364 MPI3_SCSIIO_MSGFLAGS_DIVERT_TO_FIRMWARE;
4365 scsiio_flags |= MPI3_SCSIIO_FLAGS_DIVERT_REASON_IO_THROTTLING;
4367 scsiio_req->flags = cpu_to_le32(scsiio_flags);
4369 if (mpi3mr_op_request_post(mrioc, op_req_q,
4370 scmd_priv_data->mpi3mr_scsiio_req)) {
4371 mpi3mr_clear_scmd_priv(mrioc, scmd);
4372 retval = SCSI_MLQUEUE_HOST_BUSY;
4373 if (tracked_io_sz) {
4374 atomic_sub(tracked_io_sz, &mrioc->pend_large_data_sz);
4376 atomic_sub(tracked_io_sz,
4377 &tg->pend_large_data_sz);
4386 static struct scsi_host_template mpi3mr_driver_template = {
4387 .module = THIS_MODULE,
4388 .name = "MPI3 Storage Controller",
4389 .proc_name = MPI3MR_DRIVER_NAME,
4390 .queuecommand = mpi3mr_qcmd,
4391 .target_alloc = mpi3mr_target_alloc,
4392 .slave_alloc = mpi3mr_slave_alloc,
4393 .slave_configure = mpi3mr_slave_configure,
4394 .target_destroy = mpi3mr_target_destroy,
4395 .slave_destroy = mpi3mr_slave_destroy,
4396 .scan_finished = mpi3mr_scan_finished,
4397 .scan_start = mpi3mr_scan_start,
4398 .change_queue_depth = mpi3mr_change_queue_depth,
4399 .eh_device_reset_handler = mpi3mr_eh_dev_reset,
4400 .eh_target_reset_handler = mpi3mr_eh_target_reset,
4401 .eh_host_reset_handler = mpi3mr_eh_host_reset,
4402 .bios_param = mpi3mr_bios_param,
4403 .map_queues = mpi3mr_map_queues,
4404 .mq_poll = mpi3mr_blk_mq_poll,
4408 .sg_tablesize = MPI3MR_SG_DEPTH,
4409 /* max xfer supported is 1M (2K in 512 byte sized sectors)
4411 .max_sectors = 2048,
4412 .cmd_per_lun = MPI3MR_MAX_CMDS_LUN,
4413 .max_segment_size = 0xffffffff,
4414 .track_queue_depth = 1,
4415 .cmd_size = sizeof(struct scmd_priv),
4416 .shost_groups = mpi3mr_host_groups,
4417 .sdev_groups = mpi3mr_dev_groups,
4421 * mpi3mr_init_drv_cmd - Initialize internal command tracker
4422 * @cmdptr: Internal command tracker
4423 * @host_tag: Host tag used for the specific command
4425 * Initialize the internal command tracker structure with
4426 * specified host tag.
4430 static inline void mpi3mr_init_drv_cmd(struct mpi3mr_drv_cmd *cmdptr,
4433 mutex_init(&cmdptr->mutex);
4434 cmdptr->reply = NULL;
4435 cmdptr->state = MPI3MR_CMD_NOTUSED;
4436 cmdptr->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
4437 cmdptr->host_tag = host_tag;
4441 * osintfc_mrioc_security_status -Check controller secure status
4442 * @pdev: PCI device instance
4444 * Read the Device Serial Number capability from PCI config
4445 * space and decide whether the controller is secure or not.
4447 * Return: 0 on success, non-zero on failure.
4450 osintfc_mrioc_security_status(struct pci_dev *pdev)
4458 base = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DSN);
4461 "%s: PCI_EXT_CAP_ID_DSN is not supported\n", __func__);
4465 pci_read_config_dword(pdev, base + 4, &cap_data);
4467 debug_status = cap_data & MPI3MR_CTLR_SECURE_DBG_STATUS_MASK;
4468 ctlr_status = cap_data & MPI3MR_CTLR_SECURITY_STATUS_MASK;
4470 switch (ctlr_status) {
4471 case MPI3MR_INVALID_DEVICE:
4473 "%s: Non secure ctlr (Invalid) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
4474 __func__, pdev->device, pdev->subsystem_vendor,
4475 pdev->subsystem_device);
4478 case MPI3MR_CONFIG_SECURE_DEVICE:
4480 dev_info(&pdev->dev,
4481 "%s: Config secure ctlr is detected\n",
4484 case MPI3MR_HARD_SECURE_DEVICE:
4486 case MPI3MR_TAMPERED_DEVICE:
4488 "%s: Non secure ctlr (Tampered) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
4489 __func__, pdev->device, pdev->subsystem_vendor,
4490 pdev->subsystem_device);
4498 if (!retval && debug_status) {
4500 "%s: Non secure ctlr (Secure Dbg) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
4501 __func__, pdev->device, pdev->subsystem_vendor,
4502 pdev->subsystem_device);
4510 * mpi3mr_probe - PCI probe callback
4511 * @pdev: PCI device instance
4512 * @id: PCI device ID details
4514 * controller initialization routine. Checks the security status
4515 * of the controller and if it is invalid or tampered return the
4516 * probe without initializing the controller. Otherwise,
4517 * allocate per adapter instance through shost_priv and
4518 * initialize controller specific data structures, initializae
4519 * the controller hardware, add shost to the SCSI subsystem.
4521 * Return: 0 on success, non-zero on failure.
4525 mpi3mr_probe(struct pci_dev *pdev, const struct pci_device_id *id)
4527 struct mpi3mr_ioc *mrioc = NULL;
4528 struct Scsi_Host *shost = NULL;
4531 if (osintfc_mrioc_security_status(pdev)) {
4532 warn_non_secure_ctlr = 1;
4533 return 1; /* For Invalid and Tampered device */
4536 shost = scsi_host_alloc(&mpi3mr_driver_template,
4537 sizeof(struct mpi3mr_ioc));
4543 mrioc = shost_priv(shost);
4544 mrioc->id = mrioc_ids++;
4545 sprintf(mrioc->driver_name, "%s", MPI3MR_DRIVER_NAME);
4546 sprintf(mrioc->name, "%s%d", mrioc->driver_name, mrioc->id);
4547 INIT_LIST_HEAD(&mrioc->list);
4548 spin_lock(&mrioc_list_lock);
4549 list_add_tail(&mrioc->list, &mrioc_list);
4550 spin_unlock(&mrioc_list_lock);
4552 spin_lock_init(&mrioc->admin_req_lock);
4553 spin_lock_init(&mrioc->reply_free_queue_lock);
4554 spin_lock_init(&mrioc->sbq_lock);
4555 spin_lock_init(&mrioc->fwevt_lock);
4556 spin_lock_init(&mrioc->tgtdev_lock);
4557 spin_lock_init(&mrioc->watchdog_lock);
4558 spin_lock_init(&mrioc->chain_buf_lock);
4560 INIT_LIST_HEAD(&mrioc->fwevt_list);
4561 INIT_LIST_HEAD(&mrioc->tgtdev_list);
4562 INIT_LIST_HEAD(&mrioc->delayed_rmhs_list);
4563 INIT_LIST_HEAD(&mrioc->delayed_evtack_cmds_list);
4565 mutex_init(&mrioc->reset_mutex);
4566 mpi3mr_init_drv_cmd(&mrioc->init_cmds, MPI3MR_HOSTTAG_INITCMDS);
4567 mpi3mr_init_drv_cmd(&mrioc->host_tm_cmds, MPI3MR_HOSTTAG_BLK_TMS);
4568 mpi3mr_init_drv_cmd(&mrioc->bsg_cmds, MPI3MR_HOSTTAG_BSG_CMDS);
4570 for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++)
4571 mpi3mr_init_drv_cmd(&mrioc->dev_rmhs_cmds[i],
4572 MPI3MR_HOSTTAG_DEVRMCMD_MIN + i);
4575 mrioc->enable_segqueue = true;
4577 init_waitqueue_head(&mrioc->reset_waitq);
4578 mrioc->logging_level = logging_level;
4579 mrioc->shost = shost;
4581 mrioc->stop_bsgs = 1;
4583 /* init shost parameters */
4584 shost->max_cmd_len = MPI3MR_MAX_CDB_LENGTH;
4585 shost->max_lun = -1;
4586 shost->unique_id = mrioc->id;
4588 shost->max_channel = 0;
4589 shost->max_id = 0xFFFFFFFF;
4591 shost->host_tagset = 1;
4594 scsi_host_set_prot(shost, prot_mask);
4596 prot_mask = SHOST_DIF_TYPE1_PROTECTION
4597 | SHOST_DIF_TYPE2_PROTECTION
4598 | SHOST_DIF_TYPE3_PROTECTION;
4599 scsi_host_set_prot(shost, prot_mask);
4603 "%s :host protection capabilities enabled %s%s%s%s%s%s%s\n",
4605 (prot_mask & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "",
4606 (prot_mask & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "",
4607 (prot_mask & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "",
4608 (prot_mask & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "",
4609 (prot_mask & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "",
4610 (prot_mask & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "",
4611 (prot_mask & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : "");
4613 if (prot_guard_mask)
4614 scsi_host_set_guard(shost, (prot_guard_mask & 3));
4616 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
4618 snprintf(mrioc->fwevt_worker_name, sizeof(mrioc->fwevt_worker_name),
4619 "%s%d_fwevt_wrkr", mrioc->driver_name, mrioc->id);
4620 mrioc->fwevt_worker_thread = alloc_ordered_workqueue(
4621 mrioc->fwevt_worker_name, 0);
4622 if (!mrioc->fwevt_worker_thread) {
4623 ioc_err(mrioc, "failure at %s:%d/%s()!\n",
4624 __FILE__, __LINE__, __func__);
4626 goto fwevtthread_failed;
4629 mrioc->is_driver_loading = 1;
4630 mrioc->cpu_count = num_online_cpus();
4631 if (mpi3mr_setup_resources(mrioc)) {
4632 ioc_err(mrioc, "setup resources failed\n");
4634 goto resource_alloc_failed;
4636 if (mpi3mr_init_ioc(mrioc)) {
4637 ioc_err(mrioc, "initializing IOC failed\n");
4639 goto init_ioc_failed;
4642 shost->nr_hw_queues = mrioc->num_op_reply_q;
4643 if (mrioc->active_poll_qcount)
4646 shost->can_queue = mrioc->max_host_ios;
4647 shost->sg_tablesize = MPI3MR_SG_DEPTH;
4648 shost->max_id = mrioc->facts.max_perids + 1;
4650 retval = scsi_add_host(shost, &pdev->dev);
4652 ioc_err(mrioc, "failure at %s:%d/%s()!\n",
4653 __FILE__, __LINE__, __func__);
4654 goto addhost_failed;
4657 scsi_scan_host(shost);
4658 mpi3mr_bsg_init(mrioc);
4662 mpi3mr_stop_watchdog(mrioc);
4663 mpi3mr_cleanup_ioc(mrioc);
4665 mpi3mr_free_mem(mrioc);
4666 mpi3mr_cleanup_resources(mrioc);
4667 resource_alloc_failed:
4668 destroy_workqueue(mrioc->fwevt_worker_thread);
4670 spin_lock(&mrioc_list_lock);
4671 list_del(&mrioc->list);
4672 spin_unlock(&mrioc_list_lock);
4673 scsi_host_put(shost);
4679 * mpi3mr_remove - PCI remove callback
4680 * @pdev: PCI device instance
4682 * Cleanup the IOC by issuing MUR and shutdown notification.
4683 * Free up all memory and resources associated with the
4684 * controllerand target devices, unregister the shost.
4688 static void mpi3mr_remove(struct pci_dev *pdev)
4690 struct Scsi_Host *shost = pci_get_drvdata(pdev);
4691 struct mpi3mr_ioc *mrioc;
4692 struct workqueue_struct *wq;
4693 unsigned long flags;
4694 struct mpi3mr_tgt_dev *tgtdev, *tgtdev_next;
4699 mrioc = shost_priv(shost);
4700 while (mrioc->reset_in_progress || mrioc->is_driver_loading)
4703 mpi3mr_bsg_exit(mrioc);
4704 mrioc->stop_drv_processing = 1;
4705 mpi3mr_cleanup_fwevt_list(mrioc);
4706 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
4707 wq = mrioc->fwevt_worker_thread;
4708 mrioc->fwevt_worker_thread = NULL;
4709 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
4711 destroy_workqueue(wq);
4712 scsi_remove_host(shost);
4714 list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
4716 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
4717 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev);
4718 mpi3mr_tgtdev_put(tgtdev);
4720 mpi3mr_stop_watchdog(mrioc);
4721 mpi3mr_cleanup_ioc(mrioc);
4722 mpi3mr_free_mem(mrioc);
4723 mpi3mr_cleanup_resources(mrioc);
4725 spin_lock(&mrioc_list_lock);
4726 list_del(&mrioc->list);
4727 spin_unlock(&mrioc_list_lock);
4729 scsi_host_put(shost);
4733 * mpi3mr_shutdown - PCI shutdown callback
4734 * @pdev: PCI device instance
4736 * Free up all memory and resources associated with the
4741 static void mpi3mr_shutdown(struct pci_dev *pdev)
4743 struct Scsi_Host *shost = pci_get_drvdata(pdev);
4744 struct mpi3mr_ioc *mrioc;
4745 struct workqueue_struct *wq;
4746 unsigned long flags;
4751 mrioc = shost_priv(shost);
4752 while (mrioc->reset_in_progress || mrioc->is_driver_loading)
4755 mrioc->stop_drv_processing = 1;
4756 mpi3mr_cleanup_fwevt_list(mrioc);
4757 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
4758 wq = mrioc->fwevt_worker_thread;
4759 mrioc->fwevt_worker_thread = NULL;
4760 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
4762 destroy_workqueue(wq);
4764 mpi3mr_stop_watchdog(mrioc);
4765 mpi3mr_cleanup_ioc(mrioc);
4766 mpi3mr_cleanup_resources(mrioc);
4771 * mpi3mr_suspend - PCI power management suspend callback
4772 * @pdev: PCI device instance
4773 * @state: New power state
4775 * Change the power state to the given value and cleanup the IOC
4776 * by issuing MUR and shutdown notification
4780 static int mpi3mr_suspend(struct pci_dev *pdev, pm_message_t state)
4782 struct Scsi_Host *shost = pci_get_drvdata(pdev);
4783 struct mpi3mr_ioc *mrioc;
4784 pci_power_t device_state;
4789 mrioc = shost_priv(shost);
4790 while (mrioc->reset_in_progress || mrioc->is_driver_loading)
4792 mrioc->stop_drv_processing = 1;
4793 mpi3mr_cleanup_fwevt_list(mrioc);
4794 scsi_block_requests(shost);
4795 mpi3mr_stop_watchdog(mrioc);
4796 mpi3mr_cleanup_ioc(mrioc);
4798 device_state = pci_choose_state(pdev, state);
4799 ioc_info(mrioc, "pdev=0x%p, slot=%s, entering operating state [D%d]\n",
4800 pdev, pci_name(pdev), device_state);
4801 pci_save_state(pdev);
4802 mpi3mr_cleanup_resources(mrioc);
4803 pci_set_power_state(pdev, device_state);
4809 * mpi3mr_resume - PCI power management resume callback
4810 * @pdev: PCI device instance
4812 * Restore the power state to D0 and reinitialize the controller
4813 * and resume I/O operations to the target devices
4815 * Return: 0 on success, non-zero on failure
4817 static int mpi3mr_resume(struct pci_dev *pdev)
4819 struct Scsi_Host *shost = pci_get_drvdata(pdev);
4820 struct mpi3mr_ioc *mrioc;
4821 pci_power_t device_state = pdev->current_state;
4827 mrioc = shost_priv(shost);
4829 ioc_info(mrioc, "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
4830 pdev, pci_name(pdev), device_state);
4831 pci_set_power_state(pdev, PCI_D0);
4832 pci_enable_wake(pdev, PCI_D0, 0);
4833 pci_restore_state(pdev);
4835 mrioc->cpu_count = num_online_cpus();
4836 r = mpi3mr_setup_resources(mrioc);
4838 ioc_info(mrioc, "%s: Setup resources failed[%d]\n",
4843 mrioc->stop_drv_processing = 0;
4844 mpi3mr_memset_buffers(mrioc);
4845 r = mpi3mr_reinit_ioc(mrioc, 1);
4847 ioc_err(mrioc, "resuming controller failed[%d]\n", r);
4850 scsi_unblock_requests(shost);
4851 mpi3mr_start_watchdog(mrioc);
4857 static const struct pci_device_id mpi3mr_pci_id_table[] = {
4859 PCI_DEVICE_SUB(MPI3_MFGPAGE_VENDORID_BROADCOM,
4860 MPI3_MFGPAGE_DEVID_SAS4116, PCI_ANY_ID, PCI_ANY_ID)
4864 MODULE_DEVICE_TABLE(pci, mpi3mr_pci_id_table);
4866 static struct pci_driver mpi3mr_pci_driver = {
4867 .name = MPI3MR_DRIVER_NAME,
4868 .id_table = mpi3mr_pci_id_table,
4869 .probe = mpi3mr_probe,
4870 .remove = mpi3mr_remove,
4871 .shutdown = mpi3mr_shutdown,
4873 .suspend = mpi3mr_suspend,
4874 .resume = mpi3mr_resume,
4878 static ssize_t event_counter_show(struct device_driver *dd, char *buf)
4880 return sprintf(buf, "%llu\n", atomic64_read(&event_counter));
4882 static DRIVER_ATTR_RO(event_counter);
4884 static int __init mpi3mr_init(void)
4888 pr_info("Loading %s version %s\n", MPI3MR_DRIVER_NAME,
4889 MPI3MR_DRIVER_VERSION);
4891 ret_val = pci_register_driver(&mpi3mr_pci_driver);
4893 pr_err("%s failed to load due to pci register driver failure\n",
4894 MPI3MR_DRIVER_NAME);
4898 ret_val = driver_create_file(&mpi3mr_pci_driver.driver,
4899 &driver_attr_event_counter);
4901 pci_unregister_driver(&mpi3mr_pci_driver);
4906 static void __exit mpi3mr_exit(void)
4908 if (warn_non_secure_ctlr)
4910 "Unloading %s version %s while managing a non secure controller\n",
4911 MPI3MR_DRIVER_NAME, MPI3MR_DRIVER_VERSION);
4913 pr_info("Unloading %s version %s\n", MPI3MR_DRIVER_NAME,
4914 MPI3MR_DRIVER_VERSION);
4916 driver_remove_file(&mpi3mr_pci_driver.driver,
4917 &driver_attr_event_counter);
4918 pci_unregister_driver(&mpi3mr_pci_driver);
4921 module_init(mpi3mr_init);
4922 module_exit(mpi3mr_exit);