1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Broadcom MPI3 Storage Controllers
5 * Copyright (C) 2017-2023 Broadcom Inc.
11 #include <linux/idr.h>
13 /* global driver scop variables */
14 LIST_HEAD(mrioc_list);
15 DEFINE_SPINLOCK(mrioc_list_lock);
16 static DEFINE_IDA(mrioc_ida);
17 static int warn_non_secure_ctlr;
18 atomic64_t event_counter;
20 MODULE_AUTHOR(MPI3MR_DRIVER_AUTHOR);
21 MODULE_DESCRIPTION(MPI3MR_DRIVER_DESC);
22 MODULE_LICENSE(MPI3MR_DRIVER_LICENSE);
23 MODULE_VERSION(MPI3MR_DRIVER_VERSION);
25 /* Module parameters*/
27 module_param(prot_mask, int, 0);
28 MODULE_PARM_DESC(prot_mask, "Host protection capabilities mask, def=0x07");
30 static int prot_guard_mask = 3;
31 module_param(prot_guard_mask, int, 0);
32 MODULE_PARM_DESC(prot_guard_mask, " Host protection guard mask, def=3");
33 static int logging_level;
34 module_param(logging_level, int, 0);
35 MODULE_PARM_DESC(logging_level,
36 " bits for enabling additional logging info (default=0)");
37 static int max_sgl_entries = MPI3MR_DEFAULT_SGL_ENTRIES;
38 module_param(max_sgl_entries, int, 0444);
39 MODULE_PARM_DESC(max_sgl_entries,
40 "Preferred max number of SG entries to be used for a single I/O\n"
41 "The actual value will be determined by the driver\n"
42 "(Minimum=256, Maximum=2048, default=256)");
44 /* Forward declarations*/
45 static void mpi3mr_send_event_ack(struct mpi3mr_ioc *mrioc, u8 event,
46 struct mpi3mr_drv_cmd *cmdparam, u32 event_ctx);
48 #define MPI3MR_DRIVER_EVENT_TG_QD_REDUCTION (0xFFFF)
50 #define MPI3_EVENT_WAIT_FOR_DEVICES_TO_REFRESH (0xFFFE)
53 * mpi3mr_host_tag_for_scmd - Get host tag for a scmd
54 * @mrioc: Adapter instance reference
55 * @scmd: SCSI command reference
57 * Calculate the host tag based on block tag for a given scmd.
59 * Return: Valid host tag or MPI3MR_HOSTTAG_INVALID.
61 static u16 mpi3mr_host_tag_for_scmd(struct mpi3mr_ioc *mrioc,
62 struct scsi_cmnd *scmd)
64 struct scmd_priv *priv = NULL;
66 u16 host_tag, hw_queue;
68 unique_tag = blk_mq_unique_tag(scsi_cmd_to_rq(scmd));
70 hw_queue = blk_mq_unique_tag_to_hwq(unique_tag);
71 if (hw_queue >= mrioc->num_op_reply_q)
72 return MPI3MR_HOSTTAG_INVALID;
73 host_tag = blk_mq_unique_tag_to_tag(unique_tag);
75 if (WARN_ON(host_tag >= mrioc->max_host_ios))
76 return MPI3MR_HOSTTAG_INVALID;
78 priv = scsi_cmd_priv(scmd);
79 /*host_tag 0 is invalid hence incrementing by 1*/
80 priv->host_tag = host_tag + 1;
82 priv->in_lld_scope = 1;
83 priv->req_q_idx = hw_queue;
84 priv->meta_chain_idx = -1;
86 priv->meta_sg_valid = 0;
87 return priv->host_tag;
91 * mpi3mr_scmd_from_host_tag - Get SCSI command from host tag
92 * @mrioc: Adapter instance reference
94 * @qidx: Operational queue index
96 * Identify the block tag from the host tag and queue index and
97 * retrieve associated scsi command using scsi_host_find_tag().
99 * Return: SCSI command reference or NULL.
101 static struct scsi_cmnd *mpi3mr_scmd_from_host_tag(
102 struct mpi3mr_ioc *mrioc, u16 host_tag, u16 qidx)
104 struct scsi_cmnd *scmd = NULL;
105 struct scmd_priv *priv = NULL;
106 u32 unique_tag = host_tag - 1;
108 if (WARN_ON(host_tag > mrioc->max_host_ios))
111 unique_tag |= (qidx << BLK_MQ_UNIQUE_TAG_BITS);
113 scmd = scsi_host_find_tag(mrioc->shost, unique_tag);
115 priv = scsi_cmd_priv(scmd);
116 if (!priv->in_lld_scope)
124 * mpi3mr_clear_scmd_priv - Cleanup SCSI command private date
125 * @mrioc: Adapter instance reference
126 * @scmd: SCSI command reference
128 * Invalidate the SCSI command private data to mark the command
129 * is not in LLD scope anymore.
133 static void mpi3mr_clear_scmd_priv(struct mpi3mr_ioc *mrioc,
134 struct scsi_cmnd *scmd)
136 struct scmd_priv *priv = NULL;
138 priv = scsi_cmd_priv(scmd);
140 if (WARN_ON(priv->in_lld_scope == 0))
142 priv->host_tag = MPI3MR_HOSTTAG_INVALID;
143 priv->req_q_idx = 0xFFFF;
145 priv->in_lld_scope = 0;
146 priv->meta_sg_valid = 0;
147 if (priv->chain_idx >= 0) {
148 clear_bit(priv->chain_idx, mrioc->chain_bitmap);
149 priv->chain_idx = -1;
151 if (priv->meta_chain_idx >= 0) {
152 clear_bit(priv->meta_chain_idx, mrioc->chain_bitmap);
153 priv->meta_chain_idx = -1;
157 static void mpi3mr_dev_rmhs_send_tm(struct mpi3mr_ioc *mrioc, u16 handle,
158 struct mpi3mr_drv_cmd *cmdparam, u8 iou_rc);
159 static void mpi3mr_fwevt_worker(struct work_struct *work);
162 * mpi3mr_fwevt_free - firmware event memory dealloctor
163 * @r: k reference pointer of the firmware event
165 * Free firmware event memory when no reference.
167 static void mpi3mr_fwevt_free(struct kref *r)
169 kfree(container_of(r, struct mpi3mr_fwevt, ref_count));
173 * mpi3mr_fwevt_get - k reference incrementor
174 * @fwevt: Firmware event reference
176 * Increment firmware event reference count.
178 static void mpi3mr_fwevt_get(struct mpi3mr_fwevt *fwevt)
180 kref_get(&fwevt->ref_count);
184 * mpi3mr_fwevt_put - k reference decrementor
185 * @fwevt: Firmware event reference
187 * decrement firmware event reference count.
189 static void mpi3mr_fwevt_put(struct mpi3mr_fwevt *fwevt)
191 kref_put(&fwevt->ref_count, mpi3mr_fwevt_free);
195 * mpi3mr_alloc_fwevt - Allocate firmware event
196 * @len: length of firmware event data to allocate
198 * Allocate firmware event with required length and initialize
199 * the reference counter.
201 * Return: firmware event reference.
203 static struct mpi3mr_fwevt *mpi3mr_alloc_fwevt(int len)
205 struct mpi3mr_fwevt *fwevt;
207 fwevt = kzalloc(sizeof(*fwevt) + len, GFP_ATOMIC);
211 kref_init(&fwevt->ref_count);
216 * mpi3mr_fwevt_add_to_list - Add firmware event to the list
217 * @mrioc: Adapter instance reference
218 * @fwevt: Firmware event reference
220 * Add the given firmware event to the firmware event list.
224 static void mpi3mr_fwevt_add_to_list(struct mpi3mr_ioc *mrioc,
225 struct mpi3mr_fwevt *fwevt)
229 if (!mrioc->fwevt_worker_thread)
232 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
233 /* get fwevt reference count while adding it to fwevt_list */
234 mpi3mr_fwevt_get(fwevt);
235 INIT_LIST_HEAD(&fwevt->list);
236 list_add_tail(&fwevt->list, &mrioc->fwevt_list);
237 INIT_WORK(&fwevt->work, mpi3mr_fwevt_worker);
238 /* get fwevt reference count while enqueueing it to worker queue */
239 mpi3mr_fwevt_get(fwevt);
240 queue_work(mrioc->fwevt_worker_thread, &fwevt->work);
241 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
245 * mpi3mr_hdb_trigger_data_event - Add hdb trigger data event to
247 * @mrioc: Adapter instance reference
248 * @event_data: Event data
250 * Add the given hdb trigger data event to the firmware event
255 void mpi3mr_hdb_trigger_data_event(struct mpi3mr_ioc *mrioc,
256 struct trigger_event_data *event_data)
258 struct mpi3mr_fwevt *fwevt;
259 u16 sz = sizeof(*event_data);
261 fwevt = mpi3mr_alloc_fwevt(sz);
263 ioc_warn(mrioc, "failed to queue hdb trigger data event\n");
267 fwevt->mrioc = mrioc;
268 fwevt->event_id = MPI3MR_DRIVER_EVENT_PROCESS_TRIGGER;
270 fwevt->process_evt = 1;
272 fwevt->event_data_size = sz;
273 memcpy(fwevt->event_data, event_data, sz);
275 mpi3mr_fwevt_add_to_list(mrioc, fwevt);
279 * mpi3mr_fwevt_del_from_list - Delete firmware event from list
280 * @mrioc: Adapter instance reference
281 * @fwevt: Firmware event reference
283 * Delete the given firmware event from the firmware event list.
287 static void mpi3mr_fwevt_del_from_list(struct mpi3mr_ioc *mrioc,
288 struct mpi3mr_fwevt *fwevt)
292 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
293 if (!list_empty(&fwevt->list)) {
294 list_del_init(&fwevt->list);
296 * Put fwevt reference count after
297 * removing it from fwevt_list
299 mpi3mr_fwevt_put(fwevt);
301 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
305 * mpi3mr_dequeue_fwevt - Dequeue firmware event from the list
306 * @mrioc: Adapter instance reference
308 * Dequeue a firmware event from the firmware event list.
310 * Return: firmware event.
312 static struct mpi3mr_fwevt *mpi3mr_dequeue_fwevt(
313 struct mpi3mr_ioc *mrioc)
316 struct mpi3mr_fwevt *fwevt = NULL;
318 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
319 if (!list_empty(&mrioc->fwevt_list)) {
320 fwevt = list_first_entry(&mrioc->fwevt_list,
321 struct mpi3mr_fwevt, list);
322 list_del_init(&fwevt->list);
324 * Put fwevt reference count after
325 * removing it from fwevt_list
327 mpi3mr_fwevt_put(fwevt);
329 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
335 * mpi3mr_cancel_work - cancel firmware event
336 * @fwevt: fwevt object which needs to be canceled
340 static void mpi3mr_cancel_work(struct mpi3mr_fwevt *fwevt)
343 * Wait on the fwevt to complete. If this returns 1, then
344 * the event was never executed.
346 * If it did execute, we wait for it to finish, and the put will
347 * happen from mpi3mr_process_fwevt()
349 if (cancel_work_sync(&fwevt->work)) {
351 * Put fwevt reference count after
352 * dequeuing it from worker queue
354 mpi3mr_fwevt_put(fwevt);
356 * Put fwevt reference count to neutralize
357 * kref_init increment
359 mpi3mr_fwevt_put(fwevt);
364 * mpi3mr_cleanup_fwevt_list - Cleanup firmware event list
365 * @mrioc: Adapter instance reference
367 * Flush all pending firmware events from the firmware event
372 void mpi3mr_cleanup_fwevt_list(struct mpi3mr_ioc *mrioc)
374 struct mpi3mr_fwevt *fwevt = NULL;
376 if ((list_empty(&mrioc->fwevt_list) && !mrioc->current_event) ||
377 !mrioc->fwevt_worker_thread)
380 while ((fwevt = mpi3mr_dequeue_fwevt(mrioc)))
381 mpi3mr_cancel_work(fwevt);
383 if (mrioc->current_event) {
384 fwevt = mrioc->current_event;
386 * Don't call cancel_work_sync() API for the
387 * fwevt work if the controller reset is
388 * get called as part of processing the
389 * same fwevt work (or) when worker thread is
390 * waiting for device add/remove APIs to complete.
391 * Otherwise we will see deadlock.
393 if (current_work() == &fwevt->work || fwevt->pending_at_sml) {
398 mpi3mr_cancel_work(fwevt);
403 * mpi3mr_queue_qd_reduction_event - Queue TG QD reduction event
404 * @mrioc: Adapter instance reference
405 * @tg: Throttle group information pointer
407 * Accessor to queue on synthetically generated driver event to
408 * the event worker thread, the driver event will be used to
409 * reduce the QD of all VDs in the TG from the worker thread.
413 static void mpi3mr_queue_qd_reduction_event(struct mpi3mr_ioc *mrioc,
414 struct mpi3mr_throttle_group_info *tg)
416 struct mpi3mr_fwevt *fwevt;
417 u16 sz = sizeof(struct mpi3mr_throttle_group_info *);
420 * If the QD reduction event is already queued due to throttle and if
421 * the QD is not restored through device info change event
422 * then dont queue further reduction events
424 if (tg->fw_qd != tg->modified_qd)
427 fwevt = mpi3mr_alloc_fwevt(sz);
429 ioc_warn(mrioc, "failed to queue TG QD reduction event\n");
432 *(struct mpi3mr_throttle_group_info **)fwevt->event_data = tg;
433 fwevt->mrioc = mrioc;
434 fwevt->event_id = MPI3MR_DRIVER_EVENT_TG_QD_REDUCTION;
436 fwevt->process_evt = 1;
438 fwevt->event_data_size = sz;
439 tg->modified_qd = max_t(u16, (tg->fw_qd * tg->qd_reduction) / 10, 8);
441 dprint_event_bh(mrioc, "qd reduction event queued for tg_id(%d)\n",
443 mpi3mr_fwevt_add_to_list(mrioc, fwevt);
447 * mpi3mr_invalidate_devhandles -Invalidate device handles
448 * @mrioc: Adapter instance reference
450 * Invalidate the device handles in the target device structures
451 * . Called post reset prior to reinitializing the controller.
455 void mpi3mr_invalidate_devhandles(struct mpi3mr_ioc *mrioc)
457 struct mpi3mr_tgt_dev *tgtdev;
458 struct mpi3mr_stgt_priv_data *tgt_priv;
460 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
461 tgtdev->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
462 if (tgtdev->starget && tgtdev->starget->hostdata) {
463 tgt_priv = tgtdev->starget->hostdata;
464 tgt_priv->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
465 tgt_priv->io_throttle_enabled = 0;
466 tgt_priv->io_divert = 0;
467 tgt_priv->throttle_group = NULL;
469 if (tgtdev->host_exposed)
470 atomic_set(&tgt_priv->block_io, 1);
476 * mpi3mr_print_scmd - print individual SCSI command
478 * @data: Adapter instance reference
480 * Print the SCSI command details if it is in LLD scope.
482 * Return: true always.
484 static bool mpi3mr_print_scmd(struct request *rq, void *data)
486 struct mpi3mr_ioc *mrioc = (struct mpi3mr_ioc *)data;
487 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
488 struct scmd_priv *priv = NULL;
491 priv = scsi_cmd_priv(scmd);
492 if (!priv->in_lld_scope)
495 ioc_info(mrioc, "%s :Host Tag = %d, qid = %d\n",
496 __func__, priv->host_tag, priv->req_q_idx + 1);
497 scsi_print_command(scmd);
505 * mpi3mr_flush_scmd - Flush individual SCSI command
507 * @data: Adapter instance reference
509 * Return the SCSI command to the upper layers if it is in LLD
512 * Return: true always.
515 static bool mpi3mr_flush_scmd(struct request *rq, void *data)
517 struct mpi3mr_ioc *mrioc = (struct mpi3mr_ioc *)data;
518 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
519 struct scmd_priv *priv = NULL;
522 priv = scsi_cmd_priv(scmd);
523 if (!priv->in_lld_scope)
526 if (priv->meta_sg_valid)
527 dma_unmap_sg(&mrioc->pdev->dev, scsi_prot_sglist(scmd),
528 scsi_prot_sg_count(scmd), scmd->sc_data_direction);
529 mpi3mr_clear_scmd_priv(mrioc, scmd);
530 scsi_dma_unmap(scmd);
531 scmd->result = DID_RESET << 16;
532 scsi_print_command(scmd);
534 mrioc->flush_io_count++;
542 * mpi3mr_count_dev_pending - Count commands pending for a lun
544 * @data: SCSI device reference
546 * This is an iterator function called for each SCSI command in
547 * a host and if the command is pending in the LLD for the
548 * specific device(lun) then device specific pending I/O counter
549 * is updated in the device structure.
551 * Return: true always.
554 static bool mpi3mr_count_dev_pending(struct request *rq, void *data)
556 struct scsi_device *sdev = (struct scsi_device *)data;
557 struct mpi3mr_sdev_priv_data *sdev_priv_data = sdev->hostdata;
558 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
559 struct scmd_priv *priv;
562 priv = scsi_cmd_priv(scmd);
563 if (!priv->in_lld_scope)
565 if (scmd->device == sdev)
566 sdev_priv_data->pend_count++;
574 * mpi3mr_count_tgt_pending - Count commands pending for target
576 * @data: SCSI target reference
578 * This is an iterator function called for each SCSI command in
579 * a host and if the command is pending in the LLD for the
580 * specific target then target specific pending I/O counter is
581 * updated in the target structure.
583 * Return: true always.
586 static bool mpi3mr_count_tgt_pending(struct request *rq, void *data)
588 struct scsi_target *starget = (struct scsi_target *)data;
589 struct mpi3mr_stgt_priv_data *stgt_priv_data = starget->hostdata;
590 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
591 struct scmd_priv *priv;
594 priv = scsi_cmd_priv(scmd);
595 if (!priv->in_lld_scope)
597 if (scmd->device && (scsi_target(scmd->device) == starget))
598 stgt_priv_data->pend_count++;
606 * mpi3mr_flush_host_io - Flush host I/Os
607 * @mrioc: Adapter instance reference
609 * Flush all of the pending I/Os by calling
610 * blk_mq_tagset_busy_iter() for each possible tag. This is
611 * executed post controller reset
615 void mpi3mr_flush_host_io(struct mpi3mr_ioc *mrioc)
617 struct Scsi_Host *shost = mrioc->shost;
619 mrioc->flush_io_count = 0;
620 ioc_info(mrioc, "%s :Flushing Host I/O cmds post reset\n", __func__);
621 blk_mq_tagset_busy_iter(&shost->tag_set,
622 mpi3mr_flush_scmd, (void *)mrioc);
623 ioc_info(mrioc, "%s :Flushed %d Host I/O cmds\n", __func__,
624 mrioc->flush_io_count);
628 * mpi3mr_flush_cmds_for_unrecovered_controller - Flush all pending cmds
629 * @mrioc: Adapter instance reference
631 * This function waits for currently running IO poll threads to
632 * exit and then flushes all host I/Os and any internal pending
633 * cmds. This is executed after controller is marked as
638 void mpi3mr_flush_cmds_for_unrecovered_controller(struct mpi3mr_ioc *mrioc)
640 struct Scsi_Host *shost = mrioc->shost;
643 if (!mrioc->unrecoverable)
646 if (mrioc->op_reply_qinfo) {
647 for (i = 0; i < mrioc->num_queues; i++) {
648 while (atomic_read(&mrioc->op_reply_qinfo[i].in_use))
650 atomic_set(&mrioc->op_reply_qinfo[i].pend_ios, 0);
653 mrioc->flush_io_count = 0;
654 blk_mq_tagset_busy_iter(&shost->tag_set,
655 mpi3mr_flush_scmd, (void *)mrioc);
656 mpi3mr_flush_delayed_cmd_lists(mrioc);
657 mpi3mr_flush_drv_cmds(mrioc);
661 * mpi3mr_alloc_tgtdev - target device allocator
663 * Allocate target device instance and initialize the reference
666 * Return: target device instance.
668 static struct mpi3mr_tgt_dev *mpi3mr_alloc_tgtdev(void)
670 struct mpi3mr_tgt_dev *tgtdev;
672 tgtdev = kzalloc(sizeof(*tgtdev), GFP_ATOMIC);
675 kref_init(&tgtdev->ref_count);
680 * mpi3mr_tgtdev_add_to_list -Add tgtdevice to the list
681 * @mrioc: Adapter instance reference
682 * @tgtdev: Target device
684 * Add the target device to the target device list
688 static void mpi3mr_tgtdev_add_to_list(struct mpi3mr_ioc *mrioc,
689 struct mpi3mr_tgt_dev *tgtdev)
693 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
694 mpi3mr_tgtdev_get(tgtdev);
695 INIT_LIST_HEAD(&tgtdev->list);
696 list_add_tail(&tgtdev->list, &mrioc->tgtdev_list);
697 tgtdev->state = MPI3MR_DEV_CREATED;
698 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
702 * mpi3mr_tgtdev_del_from_list -Delete tgtdevice from the list
703 * @mrioc: Adapter instance reference
704 * @tgtdev: Target device
705 * @must_delete: Must delete the target device from the list irrespective
706 * of the device state.
708 * Remove the target device from the target device list
712 static void mpi3mr_tgtdev_del_from_list(struct mpi3mr_ioc *mrioc,
713 struct mpi3mr_tgt_dev *tgtdev, bool must_delete)
717 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
718 if ((tgtdev->state == MPI3MR_DEV_REMOVE_HS_STARTED) || (must_delete == true)) {
719 if (!list_empty(&tgtdev->list)) {
720 list_del_init(&tgtdev->list);
721 tgtdev->state = MPI3MR_DEV_DELETED;
722 mpi3mr_tgtdev_put(tgtdev);
725 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
729 * __mpi3mr_get_tgtdev_by_handle -Get tgtdev from device handle
730 * @mrioc: Adapter instance reference
731 * @handle: Device handle
733 * Accessor to retrieve target device from the device handle.
736 * Return: Target device reference.
738 static struct mpi3mr_tgt_dev *__mpi3mr_get_tgtdev_by_handle(
739 struct mpi3mr_ioc *mrioc, u16 handle)
741 struct mpi3mr_tgt_dev *tgtdev;
743 assert_spin_locked(&mrioc->tgtdev_lock);
744 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list)
745 if (tgtdev->dev_handle == handle)
750 mpi3mr_tgtdev_get(tgtdev);
755 * mpi3mr_get_tgtdev_by_handle -Get tgtdev from device handle
756 * @mrioc: Adapter instance reference
757 * @handle: Device handle
759 * Accessor to retrieve target device from the device handle.
762 * Return: Target device reference.
764 struct mpi3mr_tgt_dev *mpi3mr_get_tgtdev_by_handle(
765 struct mpi3mr_ioc *mrioc, u16 handle)
767 struct mpi3mr_tgt_dev *tgtdev;
770 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
771 tgtdev = __mpi3mr_get_tgtdev_by_handle(mrioc, handle);
772 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
777 * __mpi3mr_get_tgtdev_by_perst_id -Get tgtdev from persist ID
778 * @mrioc: Adapter instance reference
779 * @persist_id: Persistent ID
781 * Accessor to retrieve target device from the Persistent ID.
784 * Return: Target device reference.
786 static struct mpi3mr_tgt_dev *__mpi3mr_get_tgtdev_by_perst_id(
787 struct mpi3mr_ioc *mrioc, u16 persist_id)
789 struct mpi3mr_tgt_dev *tgtdev;
791 assert_spin_locked(&mrioc->tgtdev_lock);
792 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list)
793 if (tgtdev->perst_id == persist_id)
798 mpi3mr_tgtdev_get(tgtdev);
803 * mpi3mr_get_tgtdev_by_perst_id -Get tgtdev from persistent ID
804 * @mrioc: Adapter instance reference
805 * @persist_id: Persistent ID
807 * Accessor to retrieve target device from the Persistent ID.
810 * Return: Target device reference.
812 static struct mpi3mr_tgt_dev *mpi3mr_get_tgtdev_by_perst_id(
813 struct mpi3mr_ioc *mrioc, u16 persist_id)
815 struct mpi3mr_tgt_dev *tgtdev;
818 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
819 tgtdev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, persist_id);
820 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
825 * __mpi3mr_get_tgtdev_from_tgtpriv -Get tgtdev from tgt private
826 * @mrioc: Adapter instance reference
827 * @tgt_priv: Target private data
829 * Accessor to return target device from the target private
830 * data. Non Lock version
832 * Return: Target device reference.
834 static struct mpi3mr_tgt_dev *__mpi3mr_get_tgtdev_from_tgtpriv(
835 struct mpi3mr_ioc *mrioc, struct mpi3mr_stgt_priv_data *tgt_priv)
837 struct mpi3mr_tgt_dev *tgtdev;
839 assert_spin_locked(&mrioc->tgtdev_lock);
840 tgtdev = tgt_priv->tgt_dev;
842 mpi3mr_tgtdev_get(tgtdev);
847 * mpi3mr_set_io_divert_for_all_vd_in_tg -set divert for TG VDs
848 * @mrioc: Adapter instance reference
849 * @tg: Throttle group information pointer
850 * @divert_value: 1 or 0
852 * Accessor to set io_divert flag for each device associated
853 * with the given throttle group with the given value.
857 static void mpi3mr_set_io_divert_for_all_vd_in_tg(struct mpi3mr_ioc *mrioc,
858 struct mpi3mr_throttle_group_info *tg, u8 divert_value)
861 struct mpi3mr_tgt_dev *tgtdev;
862 struct mpi3mr_stgt_priv_data *tgt_priv;
864 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
865 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
866 if (tgtdev->starget && tgtdev->starget->hostdata) {
867 tgt_priv = tgtdev->starget->hostdata;
868 if (tgt_priv->throttle_group == tg)
869 tgt_priv->io_divert = divert_value;
872 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
876 * mpi3mr_print_device_event_notice - print notice related to post processing of
877 * device event after controller reset.
879 * @mrioc: Adapter instance reference
880 * @device_add: true for device add event and false for device removal event
884 void mpi3mr_print_device_event_notice(struct mpi3mr_ioc *mrioc,
887 ioc_notice(mrioc, "Device %s was in progress before the reset and\n",
888 (device_add ? "addition" : "removal"));
889 ioc_notice(mrioc, "completed after reset, verify whether the exposed devices\n");
890 ioc_notice(mrioc, "are matched with attached devices for correctness\n");
894 * mpi3mr_remove_tgtdev_from_host - Remove dev from upper layers
895 * @mrioc: Adapter instance reference
896 * @tgtdev: Target device structure
898 * Checks whether the device is exposed to upper layers and if it
899 * is then remove the device from upper layers by calling
900 * scsi_remove_target().
902 * Return: 0 on success, non zero on failure.
904 void mpi3mr_remove_tgtdev_from_host(struct mpi3mr_ioc *mrioc,
905 struct mpi3mr_tgt_dev *tgtdev)
907 struct mpi3mr_stgt_priv_data *tgt_priv;
909 ioc_info(mrioc, "%s :Removing handle(0x%04x), wwid(0x%016llx)\n",
910 __func__, tgtdev->dev_handle, (unsigned long long)tgtdev->wwid);
911 if (tgtdev->starget && tgtdev->starget->hostdata) {
912 tgt_priv = tgtdev->starget->hostdata;
913 atomic_set(&tgt_priv->block_io, 0);
914 tgt_priv->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
917 if (!mrioc->sas_transport_enabled || (tgtdev->dev_type !=
918 MPI3_DEVICE_DEVFORM_SAS_SATA) || tgtdev->non_stl) {
919 if (tgtdev->starget) {
920 if (mrioc->current_event)
921 mrioc->current_event->pending_at_sml = 1;
922 scsi_remove_target(&tgtdev->starget->dev);
923 tgtdev->host_exposed = 0;
924 if (mrioc->current_event) {
925 mrioc->current_event->pending_at_sml = 0;
926 if (mrioc->current_event->discard) {
927 mpi3mr_print_device_event_notice(mrioc,
934 mpi3mr_remove_tgtdev_from_sas_transport(mrioc, tgtdev);
935 mpi3mr_global_trigger(mrioc,
936 MPI3_DRIVER2_GLOBALTRIGGER_DEVICE_REMOVAL_ENABLED);
938 ioc_info(mrioc, "%s :Removed handle(0x%04x), wwid(0x%016llx)\n",
939 __func__, tgtdev->dev_handle, (unsigned long long)tgtdev->wwid);
943 * mpi3mr_report_tgtdev_to_host - Expose device to upper layers
944 * @mrioc: Adapter instance reference
945 * @perst_id: Persistent ID of the device
947 * Checks whether the device can be exposed to upper layers and
948 * if it is not then expose the device to upper layers by
949 * calling scsi_scan_target().
951 * Return: 0 on success, non zero on failure.
953 static int mpi3mr_report_tgtdev_to_host(struct mpi3mr_ioc *mrioc,
957 struct mpi3mr_tgt_dev *tgtdev;
959 if (mrioc->reset_in_progress || mrioc->pci_err_recovery)
962 tgtdev = mpi3mr_get_tgtdev_by_perst_id(mrioc, perst_id);
967 if (tgtdev->is_hidden || tgtdev->host_exposed) {
971 if (!mrioc->sas_transport_enabled || (tgtdev->dev_type !=
972 MPI3_DEVICE_DEVFORM_SAS_SATA) || tgtdev->non_stl){
973 tgtdev->host_exposed = 1;
974 if (mrioc->current_event)
975 mrioc->current_event->pending_at_sml = 1;
976 scsi_scan_target(&mrioc->shost->shost_gendev,
977 mrioc->scsi_device_channel, tgtdev->perst_id,
978 SCAN_WILD_CARD, SCSI_SCAN_INITIAL);
979 if (!tgtdev->starget)
980 tgtdev->host_exposed = 0;
981 if (mrioc->current_event) {
982 mrioc->current_event->pending_at_sml = 0;
983 if (mrioc->current_event->discard) {
984 mpi3mr_print_device_event_notice(mrioc, true);
989 mpi3mr_report_tgtdev_to_sas_transport(mrioc, tgtdev);
992 mpi3mr_tgtdev_put(tgtdev);
998 * mpi3mr_change_queue_depth- Change QD callback handler
999 * @sdev: SCSI device reference
1000 * @q_depth: Queue depth
1002 * Validate and limit QD and call scsi_change_queue_depth.
1004 * Return: return value of scsi_change_queue_depth
1006 static int mpi3mr_change_queue_depth(struct scsi_device *sdev,
1009 struct scsi_target *starget = scsi_target(sdev);
1010 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1013 if (!sdev->tagged_supported)
1015 if (q_depth > shost->can_queue)
1016 q_depth = shost->can_queue;
1018 q_depth = MPI3MR_DEFAULT_SDEV_QD;
1019 retval = scsi_change_queue_depth(sdev, q_depth);
1020 sdev->max_queue_depth = sdev->queue_depth;
1025 static void mpi3mr_configure_nvme_dev(struct mpi3mr_tgt_dev *tgt_dev,
1026 struct queue_limits *lim)
1028 u8 pgsz = tgt_dev->dev_spec.pcie_inf.pgsz ? : MPI3MR_DEFAULT_PGSZEXP;
1030 lim->max_hw_sectors = tgt_dev->dev_spec.pcie_inf.mdts / 512;
1031 lim->virt_boundary_mask = (1 << pgsz) - 1;
1034 static void mpi3mr_configure_tgt_dev(struct mpi3mr_tgt_dev *tgt_dev,
1035 struct queue_limits *lim)
1037 if (tgt_dev->dev_type == MPI3_DEVICE_DEVFORM_PCIE &&
1038 (tgt_dev->dev_spec.pcie_inf.dev_info &
1039 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) ==
1040 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE)
1041 mpi3mr_configure_nvme_dev(tgt_dev, lim);
1045 * mpi3mr_update_sdev - Update SCSI device information
1046 * @sdev: SCSI device reference
1047 * @data: target device reference
1049 * This is an iterator function called for each SCSI device in a
1050 * target to update the target specific information into each
1056 mpi3mr_update_sdev(struct scsi_device *sdev, void *data)
1058 struct mpi3mr_tgt_dev *tgtdev;
1059 struct queue_limits lim;
1061 tgtdev = (struct mpi3mr_tgt_dev *)data;
1065 mpi3mr_change_queue_depth(sdev, tgtdev->q_depth);
1067 lim = queue_limits_start_update(sdev->request_queue);
1068 mpi3mr_configure_tgt_dev(tgtdev, &lim);
1069 WARN_ON_ONCE(queue_limits_commit_update(sdev->request_queue, &lim));
1073 * mpi3mr_refresh_tgtdevs - Refresh target device exposure
1074 * @mrioc: Adapter instance reference
1076 * This is executed post controller reset to identify any
1077 * missing devices during reset and remove from the upper layers
1078 * or expose any newly detected device to the upper layers.
1082 static void mpi3mr_refresh_tgtdevs(struct mpi3mr_ioc *mrioc)
1084 struct mpi3mr_tgt_dev *tgtdev, *tgtdev_next;
1085 struct mpi3mr_stgt_priv_data *tgt_priv;
1087 dprint_reset(mrioc, "refresh target devices: check for removals\n");
1088 list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
1090 if (((tgtdev->dev_handle == MPI3MR_INVALID_DEV_HANDLE) ||
1091 tgtdev->is_hidden) &&
1092 tgtdev->host_exposed && tgtdev->starget &&
1093 tgtdev->starget->hostdata) {
1094 tgt_priv = tgtdev->starget->hostdata;
1095 tgt_priv->dev_removed = 1;
1096 atomic_set(&tgt_priv->block_io, 0);
1100 list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
1102 if (tgtdev->dev_handle == MPI3MR_INVALID_DEV_HANDLE) {
1103 dprint_reset(mrioc, "removing target device with perst_id(%d)\n",
1105 if (tgtdev->host_exposed)
1106 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1107 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, true);
1108 mpi3mr_tgtdev_put(tgtdev);
1109 } else if (tgtdev->is_hidden & tgtdev->host_exposed) {
1110 dprint_reset(mrioc, "hiding target device with perst_id(%d)\n",
1112 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1117 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
1118 if ((tgtdev->dev_handle != MPI3MR_INVALID_DEV_HANDLE) &&
1119 !tgtdev->is_hidden) {
1120 if (!tgtdev->host_exposed)
1121 mpi3mr_report_tgtdev_to_host(mrioc,
1123 else if (tgtdev->starget)
1124 starget_for_each_device(tgtdev->starget,
1125 (void *)tgtdev, mpi3mr_update_sdev);
1131 * mpi3mr_update_tgtdev - DevStatusChange evt bottomhalf
1132 * @mrioc: Adapter instance reference
1133 * @tgtdev: Target device internal structure
1134 * @dev_pg0: New device page0
1135 * @is_added: Flag to indicate the device is just added
1137 * Update the information from the device page0 into the driver
1138 * cached target device structure.
1142 static void mpi3mr_update_tgtdev(struct mpi3mr_ioc *mrioc,
1143 struct mpi3mr_tgt_dev *tgtdev, struct mpi3_device_page0 *dev_pg0,
1147 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
1148 struct mpi3mr_enclosure_node *enclosure_dev = NULL;
1151 tgtdev->perst_id = le16_to_cpu(dev_pg0->persistent_id);
1152 tgtdev->dev_handle = le16_to_cpu(dev_pg0->dev_handle);
1153 tgtdev->dev_type = dev_pg0->device_form;
1154 tgtdev->io_unit_port = dev_pg0->io_unit_port;
1155 tgtdev->encl_handle = le16_to_cpu(dev_pg0->enclosure_handle);
1156 tgtdev->parent_handle = le16_to_cpu(dev_pg0->parent_dev_handle);
1157 tgtdev->slot = le16_to_cpu(dev_pg0->slot);
1158 tgtdev->q_depth = le16_to_cpu(dev_pg0->queue_depth);
1159 tgtdev->wwid = le64_to_cpu(dev_pg0->wwid);
1160 tgtdev->devpg0_flag = le16_to_cpu(dev_pg0->flags);
1162 if (tgtdev->encl_handle)
1163 enclosure_dev = mpi3mr_enclosure_find_by_handle(mrioc,
1164 tgtdev->encl_handle);
1166 tgtdev->enclosure_logical_id = le64_to_cpu(
1167 enclosure_dev->pg0.enclosure_logical_id);
1169 flags = tgtdev->devpg0_flag;
1171 tgtdev->is_hidden = (flags & MPI3_DEVICE0_FLAGS_HIDDEN);
1173 if (is_added == true)
1174 tgtdev->io_throttle_enabled =
1175 (flags & MPI3_DEVICE0_FLAGS_IO_THROTTLING_REQUIRED) ? 1 : 0;
1177 switch (flags & MPI3_DEVICE0_FLAGS_MAX_WRITE_SAME_MASK) {
1178 case MPI3_DEVICE0_FLAGS_MAX_WRITE_SAME_256_LB:
1179 tgtdev->wslen = MPI3MR_WRITE_SAME_MAX_LEN_256_BLKS;
1181 case MPI3_DEVICE0_FLAGS_MAX_WRITE_SAME_2048_LB:
1182 tgtdev->wslen = MPI3MR_WRITE_SAME_MAX_LEN_2048_BLKS;
1184 case MPI3_DEVICE0_FLAGS_MAX_WRITE_SAME_NO_LIMIT:
1190 if (tgtdev->starget && tgtdev->starget->hostdata) {
1191 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
1192 tgtdev->starget->hostdata;
1193 scsi_tgt_priv_data->perst_id = tgtdev->perst_id;
1194 scsi_tgt_priv_data->dev_handle = tgtdev->dev_handle;
1195 scsi_tgt_priv_data->dev_type = tgtdev->dev_type;
1196 scsi_tgt_priv_data->io_throttle_enabled =
1197 tgtdev->io_throttle_enabled;
1198 if (is_added == true)
1199 atomic_set(&scsi_tgt_priv_data->block_io, 0);
1200 scsi_tgt_priv_data->wslen = tgtdev->wslen;
1203 switch (dev_pg0->access_status) {
1204 case MPI3_DEVICE0_ASTATUS_NO_ERRORS:
1205 case MPI3_DEVICE0_ASTATUS_PREPARE:
1206 case MPI3_DEVICE0_ASTATUS_NEEDS_INITIALIZATION:
1207 case MPI3_DEVICE0_ASTATUS_DEVICE_MISSING_DELAY:
1210 tgtdev->is_hidden = 1;
1214 switch (tgtdev->dev_type) {
1215 case MPI3_DEVICE_DEVFORM_SAS_SATA:
1217 struct mpi3_device0_sas_sata_format *sasinf =
1218 &dev_pg0->device_specific.sas_sata_format;
1219 u16 dev_info = le16_to_cpu(sasinf->device_info);
1221 tgtdev->dev_spec.sas_sata_inf.dev_info = dev_info;
1222 tgtdev->dev_spec.sas_sata_inf.sas_address =
1223 le64_to_cpu(sasinf->sas_address);
1224 tgtdev->dev_spec.sas_sata_inf.phy_id = sasinf->phy_num;
1225 tgtdev->dev_spec.sas_sata_inf.attached_phy_id =
1226 sasinf->attached_phy_identifier;
1227 if ((dev_info & MPI3_SAS_DEVICE_INFO_DEVICE_TYPE_MASK) !=
1228 MPI3_SAS_DEVICE_INFO_DEVICE_TYPE_END_DEVICE)
1229 tgtdev->is_hidden = 1;
1230 else if (!(dev_info & (MPI3_SAS_DEVICE_INFO_STP_SATA_TARGET |
1231 MPI3_SAS_DEVICE_INFO_SSP_TARGET)))
1232 tgtdev->is_hidden = 1;
1234 if (((tgtdev->devpg0_flag &
1235 MPI3_DEVICE0_FLAGS_ATT_METHOD_DIR_ATTACHED)
1236 && (tgtdev->devpg0_flag &
1237 MPI3_DEVICE0_FLAGS_ATT_METHOD_VIRTUAL)) ||
1238 (tgtdev->parent_handle == 0xFFFF))
1239 tgtdev->non_stl = 1;
1240 if (tgtdev->dev_spec.sas_sata_inf.hba_port)
1241 tgtdev->dev_spec.sas_sata_inf.hba_port->port_id =
1242 dev_pg0->io_unit_port;
1245 case MPI3_DEVICE_DEVFORM_PCIE:
1247 struct mpi3_device0_pcie_format *pcieinf =
1248 &dev_pg0->device_specific.pcie_format;
1249 u16 dev_info = le16_to_cpu(pcieinf->device_info);
1251 tgtdev->dev_spec.pcie_inf.dev_info = dev_info;
1252 tgtdev->dev_spec.pcie_inf.capb =
1253 le32_to_cpu(pcieinf->capabilities);
1254 tgtdev->dev_spec.pcie_inf.mdts = MPI3MR_DEFAULT_MDTS;
1256 tgtdev->dev_spec.pcie_inf.pgsz = 12;
1257 if (dev_pg0->access_status == MPI3_DEVICE0_ASTATUS_NO_ERRORS) {
1258 tgtdev->dev_spec.pcie_inf.mdts =
1259 le32_to_cpu(pcieinf->maximum_data_transfer_size);
1260 tgtdev->dev_spec.pcie_inf.pgsz = pcieinf->page_size;
1261 tgtdev->dev_spec.pcie_inf.reset_to =
1262 max_t(u8, pcieinf->controller_reset_to,
1263 MPI3MR_INTADMCMD_TIMEOUT);
1264 tgtdev->dev_spec.pcie_inf.abort_to =
1265 max_t(u8, pcieinf->nvme_abort_to,
1266 MPI3MR_INTADMCMD_TIMEOUT);
1268 if (tgtdev->dev_spec.pcie_inf.mdts > (1024 * 1024))
1269 tgtdev->dev_spec.pcie_inf.mdts = (1024 * 1024);
1270 if (((dev_info & MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) !=
1271 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) &&
1272 ((dev_info & MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) !=
1273 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_SCSI_DEVICE))
1274 tgtdev->is_hidden = 1;
1275 tgtdev->non_stl = 1;
1278 prot_mask = scsi_host_get_prot(mrioc->shost);
1279 if (prot_mask & SHOST_DIX_TYPE0_PROTECTION) {
1280 scsi_host_set_prot(mrioc->shost, prot_mask & 0x77);
1282 "%s : Disabling DIX0 prot capability\n", __func__);
1284 "because HBA does not support DIX0 operation on NVME drives\n");
1288 case MPI3_DEVICE_DEVFORM_VD:
1290 struct mpi3_device0_vd_format *vdinf =
1291 &dev_pg0->device_specific.vd_format;
1292 struct mpi3mr_throttle_group_info *tg = NULL;
1293 u16 vdinf_io_throttle_group =
1294 le16_to_cpu(vdinf->io_throttle_group);
1296 tgtdev->dev_spec.vd_inf.state = vdinf->vd_state;
1297 if (vdinf->vd_state == MPI3_DEVICE0_VD_STATE_OFFLINE)
1298 tgtdev->is_hidden = 1;
1299 tgtdev->non_stl = 1;
1300 tgtdev->dev_spec.vd_inf.tg_id = vdinf_io_throttle_group;
1301 tgtdev->dev_spec.vd_inf.tg_high =
1302 le16_to_cpu(vdinf->io_throttle_group_high) * 2048;
1303 tgtdev->dev_spec.vd_inf.tg_low =
1304 le16_to_cpu(vdinf->io_throttle_group_low) * 2048;
1305 if (vdinf_io_throttle_group < mrioc->num_io_throttle_group) {
1306 tg = mrioc->throttle_groups + vdinf_io_throttle_group;
1307 tg->id = vdinf_io_throttle_group;
1308 tg->high = tgtdev->dev_spec.vd_inf.tg_high;
1309 tg->low = tgtdev->dev_spec.vd_inf.tg_low;
1311 tgtdev->dev_spec.vd_inf.tg_qd_reduction;
1312 if (is_added == true)
1313 tg->fw_qd = tgtdev->q_depth;
1314 tg->modified_qd = tgtdev->q_depth;
1316 tgtdev->dev_spec.vd_inf.tg = tg;
1317 if (scsi_tgt_priv_data)
1318 scsi_tgt_priv_data->throttle_group = tg;
1327 * mpi3mr_devstatuschg_evt_bh - DevStatusChange evt bottomhalf
1328 * @mrioc: Adapter instance reference
1329 * @fwevt: Firmware event information.
1331 * Process Device status Change event and based on device's new
1332 * information, either expose the device to the upper layers, or
1333 * remove the device from upper layers.
1337 static void mpi3mr_devstatuschg_evt_bh(struct mpi3mr_ioc *mrioc,
1338 struct mpi3mr_fwevt *fwevt)
1341 u8 uhide = 0, delete = 0, cleanup = 0;
1342 struct mpi3mr_tgt_dev *tgtdev = NULL;
1343 struct mpi3_event_data_device_status_change *evtdata =
1344 (struct mpi3_event_data_device_status_change *)fwevt->event_data;
1346 dev_handle = le16_to_cpu(evtdata->dev_handle);
1348 "%s :device status change: handle(0x%04x): reason code(0x%x)\n",
1349 __func__, dev_handle, evtdata->reason_code);
1350 switch (evtdata->reason_code) {
1351 case MPI3_EVENT_DEV_STAT_RC_HIDDEN:
1354 case MPI3_EVENT_DEV_STAT_RC_NOT_HIDDEN:
1357 case MPI3_EVENT_DEV_STAT_RC_VD_NOT_RESPONDING:
1362 ioc_info(mrioc, "%s :Unhandled reason code(0x%x)\n", __func__,
1363 evtdata->reason_code);
1367 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
1371 tgtdev->is_hidden = 0;
1372 if (!tgtdev->host_exposed)
1373 mpi3mr_report_tgtdev_to_host(mrioc, tgtdev->perst_id);
1377 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1380 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, false);
1381 mpi3mr_tgtdev_put(tgtdev);
1386 mpi3mr_tgtdev_put(tgtdev);
1390 * mpi3mr_devinfochg_evt_bh - DeviceInfoChange evt bottomhalf
1391 * @mrioc: Adapter instance reference
1392 * @dev_pg0: New device page0
1394 * Process Device Info Change event and based on device's new
1395 * information, either expose the device to the upper layers, or
1396 * remove the device from upper layers or update the details of
1401 static void mpi3mr_devinfochg_evt_bh(struct mpi3mr_ioc *mrioc,
1402 struct mpi3_device_page0 *dev_pg0)
1404 struct mpi3mr_tgt_dev *tgtdev = NULL;
1405 u16 dev_handle = 0, perst_id = 0;
1407 perst_id = le16_to_cpu(dev_pg0->persistent_id);
1408 dev_handle = le16_to_cpu(dev_pg0->dev_handle);
1410 "%s :Device info change: handle(0x%04x): persist_id(0x%x)\n",
1411 __func__, dev_handle, perst_id);
1412 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
1415 mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0, false);
1416 if (!tgtdev->is_hidden && !tgtdev->host_exposed)
1417 mpi3mr_report_tgtdev_to_host(mrioc, perst_id);
1418 if (tgtdev->is_hidden && tgtdev->host_exposed)
1419 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1420 if (!tgtdev->is_hidden && tgtdev->host_exposed && tgtdev->starget)
1421 starget_for_each_device(tgtdev->starget, (void *)tgtdev,
1422 mpi3mr_update_sdev);
1425 mpi3mr_tgtdev_put(tgtdev);
1429 * mpi3mr_free_enclosure_list - release enclosures
1430 * @mrioc: Adapter instance reference
1432 * Free memory allocated during encloure add.
1436 void mpi3mr_free_enclosure_list(struct mpi3mr_ioc *mrioc)
1438 struct mpi3mr_enclosure_node *enclosure_dev, *enclosure_dev_next;
1440 list_for_each_entry_safe(enclosure_dev,
1441 enclosure_dev_next, &mrioc->enclosure_list, list) {
1442 list_del(&enclosure_dev->list);
1443 kfree(enclosure_dev);
1448 * mpi3mr_enclosure_find_by_handle - enclosure search by handle
1449 * @mrioc: Adapter instance reference
1450 * @handle: Firmware device handle of the enclosure
1452 * This searches for enclosure device based on handle, then returns the
1455 * Return: Enclosure object reference or NULL
1457 struct mpi3mr_enclosure_node *mpi3mr_enclosure_find_by_handle(
1458 struct mpi3mr_ioc *mrioc, u16 handle)
1460 struct mpi3mr_enclosure_node *enclosure_dev, *r = NULL;
1462 list_for_each_entry(enclosure_dev, &mrioc->enclosure_list, list) {
1463 if (le16_to_cpu(enclosure_dev->pg0.enclosure_handle) != handle)
1473 * mpi3mr_process_trigger_data_event_bh - Process trigger event
1475 * @mrioc: Adapter instance reference
1476 * @event_data: Event data
1478 * This function releases diage buffers or issues diag fault
1479 * based on trigger conditions
1483 static void mpi3mr_process_trigger_data_event_bh(struct mpi3mr_ioc *mrioc,
1484 struct trigger_event_data *event_data)
1486 struct diag_buffer_desc *trace_hdb = event_data->trace_hdb;
1487 struct diag_buffer_desc *fw_hdb = event_data->fw_hdb;
1488 unsigned long flags;
1490 u8 trigger_type = event_data->trigger_type;
1491 union mpi3mr_trigger_data *trigger_data =
1492 &event_data->trigger_specific_data;
1494 if (event_data->snapdump) {
1496 mpi3mr_set_trigger_data_in_hdb(trace_hdb, trigger_type,
1499 mpi3mr_set_trigger_data_in_hdb(fw_hdb, trigger_type,
1501 mpi3mr_soft_reset_handler(mrioc,
1502 MPI3MR_RESET_FROM_TRIGGER, 1);
1507 retval = mpi3mr_issue_diag_buf_release(mrioc, trace_hdb);
1509 mpi3mr_set_trigger_data_in_hdb(trace_hdb, trigger_type,
1512 spin_lock_irqsave(&mrioc->trigger_lock, flags);
1513 mrioc->trace_release_trigger_active = false;
1514 spin_unlock_irqrestore(&mrioc->trigger_lock, flags);
1517 retval = mpi3mr_issue_diag_buf_release(mrioc, fw_hdb);
1519 mpi3mr_set_trigger_data_in_hdb(fw_hdb, trigger_type,
1522 spin_lock_irqsave(&mrioc->trigger_lock, flags);
1523 mrioc->fw_release_trigger_active = false;
1524 spin_unlock_irqrestore(&mrioc->trigger_lock, flags);
1529 * mpi3mr_encldev_add_chg_evt_debug - debug for enclosure event
1530 * @mrioc: Adapter instance reference
1531 * @encl_pg0: Enclosure page 0.
1532 * @is_added: Added event or not
1536 static void mpi3mr_encldev_add_chg_evt_debug(struct mpi3mr_ioc *mrioc,
1537 struct mpi3_enclosure_page0 *encl_pg0, u8 is_added)
1539 char *reason_str = NULL;
1541 if (!(mrioc->logging_level & MPI3_DEBUG_EVENT_WORK_TASK))
1545 reason_str = "enclosure added";
1547 reason_str = "enclosure dev status changed";
1550 "%s: handle(0x%04x), enclosure logical id(0x%016llx)\n",
1551 reason_str, le16_to_cpu(encl_pg0->enclosure_handle),
1552 (unsigned long long)le64_to_cpu(encl_pg0->enclosure_logical_id));
1554 "number of slots(%d), port(%d), flags(0x%04x), present(%d)\n",
1555 le16_to_cpu(encl_pg0->num_slots), encl_pg0->io_unit_port,
1556 le16_to_cpu(encl_pg0->flags),
1557 ((le16_to_cpu(encl_pg0->flags) &
1558 MPI3_ENCLS0_FLAGS_ENCL_DEV_PRESENT_MASK) >> 4));
1562 * mpi3mr_encldev_add_chg_evt_bh - Enclosure evt bottomhalf
1563 * @mrioc: Adapter instance reference
1564 * @fwevt: Firmware event reference
1566 * Prints information about the Enclosure device status or
1567 * Enclosure add events if logging is enabled and add or remove
1568 * the enclosure from the controller's internal list of
1573 static void mpi3mr_encldev_add_chg_evt_bh(struct mpi3mr_ioc *mrioc,
1574 struct mpi3mr_fwevt *fwevt)
1576 struct mpi3mr_enclosure_node *enclosure_dev = NULL;
1577 struct mpi3_enclosure_page0 *encl_pg0;
1581 encl_pg0 = (struct mpi3_enclosure_page0 *) fwevt->event_data;
1582 added = (fwevt->event_id == MPI3_EVENT_ENCL_DEVICE_ADDED) ? 1 : 0;
1583 mpi3mr_encldev_add_chg_evt_debug(mrioc, encl_pg0, added);
1586 encl_handle = le16_to_cpu(encl_pg0->enclosure_handle);
1587 present = ((le16_to_cpu(encl_pg0->flags) &
1588 MPI3_ENCLS0_FLAGS_ENCL_DEV_PRESENT_MASK) >> 4);
1591 enclosure_dev = mpi3mr_enclosure_find_by_handle(mrioc,
1593 if (!enclosure_dev && present) {
1595 kzalloc(sizeof(struct mpi3mr_enclosure_node),
1599 list_add_tail(&enclosure_dev->list,
1600 &mrioc->enclosure_list);
1602 if (enclosure_dev) {
1604 list_del(&enclosure_dev->list);
1605 kfree(enclosure_dev);
1607 memcpy(&enclosure_dev->pg0, encl_pg0,
1608 sizeof(enclosure_dev->pg0));
1614 * mpi3mr_sastopochg_evt_debug - SASTopoChange details
1615 * @mrioc: Adapter instance reference
1616 * @event_data: SAS topology change list event data
1618 * Prints information about the SAS topology change event.
1623 mpi3mr_sastopochg_evt_debug(struct mpi3mr_ioc *mrioc,
1624 struct mpi3_event_data_sas_topology_change_list *event_data)
1628 u8 reason_code, phy_number;
1629 char *status_str = NULL;
1630 u8 link_rate, prev_link_rate;
1632 switch (event_data->exp_status) {
1633 case MPI3_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
1634 status_str = "remove";
1636 case MPI3_EVENT_SAS_TOPO_ES_RESPONDING:
1637 status_str = "responding";
1639 case MPI3_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
1640 status_str = "remove delay";
1642 case MPI3_EVENT_SAS_TOPO_ES_NO_EXPANDER:
1643 status_str = "direct attached";
1646 status_str = "unknown status";
1649 ioc_info(mrioc, "%s :sas topology change: (%s)\n",
1650 __func__, status_str);
1652 "%s :\texpander_handle(0x%04x), port(%d), enclosure_handle(0x%04x) start_phy(%02d), num_entries(%d)\n",
1653 __func__, le16_to_cpu(event_data->expander_dev_handle),
1654 event_data->io_unit_port,
1655 le16_to_cpu(event_data->enclosure_handle),
1656 event_data->start_phy_num, event_data->num_entries);
1657 for (i = 0; i < event_data->num_entries; i++) {
1658 handle = le16_to_cpu(event_data->phy_entry[i].attached_dev_handle);
1661 phy_number = event_data->start_phy_num + i;
1662 reason_code = event_data->phy_entry[i].status &
1663 MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
1664 switch (reason_code) {
1665 case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
1666 status_str = "target remove";
1668 case MPI3_EVENT_SAS_TOPO_PHY_RC_DELAY_NOT_RESPONDING:
1669 status_str = "delay target remove";
1671 case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
1672 status_str = "link status change";
1674 case MPI3_EVENT_SAS_TOPO_PHY_RC_NO_CHANGE:
1675 status_str = "link status no change";
1677 case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
1678 status_str = "target responding";
1681 status_str = "unknown";
1684 link_rate = event_data->phy_entry[i].link_rate >> 4;
1685 prev_link_rate = event_data->phy_entry[i].link_rate & 0xF;
1687 "%s :\tphy(%02d), attached_handle(0x%04x): %s: link rate: new(0x%02x), old(0x%02x)\n",
1688 __func__, phy_number, handle, status_str, link_rate,
1694 * mpi3mr_sastopochg_evt_bh - SASTopologyChange evt bottomhalf
1695 * @mrioc: Adapter instance reference
1696 * @fwevt: Firmware event reference
1698 * Prints information about the SAS topology change event and
1699 * for "not responding" event code, removes the device from the
1704 static void mpi3mr_sastopochg_evt_bh(struct mpi3mr_ioc *mrioc,
1705 struct mpi3mr_fwevt *fwevt)
1707 struct mpi3_event_data_sas_topology_change_list *event_data =
1708 (struct mpi3_event_data_sas_topology_change_list *)fwevt->event_data;
1712 u64 exp_sas_address = 0, parent_sas_address = 0;
1713 struct mpi3mr_hba_port *hba_port = NULL;
1714 struct mpi3mr_tgt_dev *tgtdev = NULL;
1715 struct mpi3mr_sas_node *sas_expander = NULL;
1716 unsigned long flags;
1717 u8 link_rate, prev_link_rate, parent_phy_number;
1719 mpi3mr_sastopochg_evt_debug(mrioc, event_data);
1720 if (mrioc->sas_transport_enabled) {
1721 hba_port = mpi3mr_get_hba_port_by_id(mrioc,
1722 event_data->io_unit_port);
1723 if (le16_to_cpu(event_data->expander_dev_handle)) {
1724 spin_lock_irqsave(&mrioc->sas_node_lock, flags);
1725 sas_expander = __mpi3mr_expander_find_by_handle(mrioc,
1726 le16_to_cpu(event_data->expander_dev_handle));
1728 exp_sas_address = sas_expander->sas_address;
1729 hba_port = sas_expander->hba_port;
1731 spin_unlock_irqrestore(&mrioc->sas_node_lock, flags);
1732 parent_sas_address = exp_sas_address;
1734 parent_sas_address = mrioc->sas_hba.sas_address;
1737 for (i = 0; i < event_data->num_entries; i++) {
1740 handle = le16_to_cpu(event_data->phy_entry[i].attached_dev_handle);
1743 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
1747 reason_code = event_data->phy_entry[i].status &
1748 MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
1750 switch (reason_code) {
1751 case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
1752 if (tgtdev->host_exposed)
1753 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1754 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, false);
1755 mpi3mr_tgtdev_put(tgtdev);
1757 case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
1758 case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
1759 case MPI3_EVENT_SAS_TOPO_PHY_RC_NO_CHANGE:
1761 if (!mrioc->sas_transport_enabled || tgtdev->non_stl
1762 || tgtdev->is_hidden)
1764 link_rate = event_data->phy_entry[i].link_rate >> 4;
1765 prev_link_rate = event_data->phy_entry[i].link_rate & 0xF;
1766 if (link_rate == prev_link_rate)
1768 if (!parent_sas_address)
1770 parent_phy_number = event_data->start_phy_num + i;
1771 mpi3mr_update_links(mrioc, parent_sas_address, handle,
1772 parent_phy_number, link_rate, hba_port);
1779 mpi3mr_tgtdev_put(tgtdev);
1782 if (mrioc->sas_transport_enabled && (event_data->exp_status ==
1783 MPI3_EVENT_SAS_TOPO_ES_NOT_RESPONDING)) {
1785 mpi3mr_expander_remove(mrioc, exp_sas_address,
1791 * mpi3mr_pcietopochg_evt_debug - PCIeTopoChange details
1792 * @mrioc: Adapter instance reference
1793 * @event_data: PCIe topology change list event data
1795 * Prints information about the PCIe topology change event.
1800 mpi3mr_pcietopochg_evt_debug(struct mpi3mr_ioc *mrioc,
1801 struct mpi3_event_data_pcie_topology_change_list *event_data)
1807 char *status_str = NULL;
1808 u8 link_rate, prev_link_rate;
1810 switch (event_data->switch_status) {
1811 case MPI3_EVENT_PCIE_TOPO_SS_NOT_RESPONDING:
1812 status_str = "remove";
1814 case MPI3_EVENT_PCIE_TOPO_SS_RESPONDING:
1815 status_str = "responding";
1817 case MPI3_EVENT_PCIE_TOPO_SS_DELAY_NOT_RESPONDING:
1818 status_str = "remove delay";
1820 case MPI3_EVENT_PCIE_TOPO_SS_NO_PCIE_SWITCH:
1821 status_str = "direct attached";
1824 status_str = "unknown status";
1827 ioc_info(mrioc, "%s :pcie topology change: (%s)\n",
1828 __func__, status_str);
1830 "%s :\tswitch_handle(0x%04x), enclosure_handle(0x%04x) start_port(%02d), num_entries(%d)\n",
1831 __func__, le16_to_cpu(event_data->switch_dev_handle),
1832 le16_to_cpu(event_data->enclosure_handle),
1833 event_data->start_port_num, event_data->num_entries);
1834 for (i = 0; i < event_data->num_entries; i++) {
1836 le16_to_cpu(event_data->port_entry[i].attached_dev_handle);
1839 port_number = event_data->start_port_num + i;
1840 reason_code = event_data->port_entry[i].port_status;
1841 switch (reason_code) {
1842 case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
1843 status_str = "target remove";
1845 case MPI3_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
1846 status_str = "delay target remove";
1848 case MPI3_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
1849 status_str = "link status change";
1851 case MPI3_EVENT_PCIE_TOPO_PS_NO_CHANGE:
1852 status_str = "link status no change";
1854 case MPI3_EVENT_PCIE_TOPO_PS_RESPONDING:
1855 status_str = "target responding";
1858 status_str = "unknown";
1861 link_rate = event_data->port_entry[i].current_port_info &
1862 MPI3_EVENT_PCIE_TOPO_PI_RATE_MASK;
1863 prev_link_rate = event_data->port_entry[i].previous_port_info &
1864 MPI3_EVENT_PCIE_TOPO_PI_RATE_MASK;
1866 "%s :\tport(%02d), attached_handle(0x%04x): %s: link rate: new(0x%02x), old(0x%02x)\n",
1867 __func__, port_number, handle, status_str, link_rate,
1873 * mpi3mr_pcietopochg_evt_bh - PCIeTopologyChange evt bottomhalf
1874 * @mrioc: Adapter instance reference
1875 * @fwevt: Firmware event reference
1877 * Prints information about the PCIe topology change event and
1878 * for "not responding" event code, removes the device from the
1883 static void mpi3mr_pcietopochg_evt_bh(struct mpi3mr_ioc *mrioc,
1884 struct mpi3mr_fwevt *fwevt)
1886 struct mpi3_event_data_pcie_topology_change_list *event_data =
1887 (struct mpi3_event_data_pcie_topology_change_list *)fwevt->event_data;
1891 struct mpi3mr_tgt_dev *tgtdev = NULL;
1893 mpi3mr_pcietopochg_evt_debug(mrioc, event_data);
1895 for (i = 0; i < event_data->num_entries; i++) {
1899 le16_to_cpu(event_data->port_entry[i].attached_dev_handle);
1902 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
1906 reason_code = event_data->port_entry[i].port_status;
1908 switch (reason_code) {
1909 case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
1910 if (tgtdev->host_exposed)
1911 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1912 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, false);
1913 mpi3mr_tgtdev_put(tgtdev);
1919 mpi3mr_tgtdev_put(tgtdev);
1924 * mpi3mr_logdata_evt_bh - Log data event bottomhalf
1925 * @mrioc: Adapter instance reference
1926 * @fwevt: Firmware event reference
1928 * Extracts the event data and calls application interfacing
1929 * function to process the event further.
1933 static void mpi3mr_logdata_evt_bh(struct mpi3mr_ioc *mrioc,
1934 struct mpi3mr_fwevt *fwevt)
1936 mpi3mr_app_save_logdata(mrioc, fwevt->event_data,
1937 fwevt->event_data_size);
1941 * mpi3mr_update_sdev_qd - Update SCSI device queue depath
1942 * @sdev: SCSI device reference
1943 * @data: Queue depth reference
1945 * This is an iterator function called for each SCSI device in a
1946 * target to update the QD of each SCSI device.
1950 static void mpi3mr_update_sdev_qd(struct scsi_device *sdev, void *data)
1952 u16 *q_depth = (u16 *)data;
1954 scsi_change_queue_depth(sdev, (int)*q_depth);
1955 sdev->max_queue_depth = sdev->queue_depth;
1959 * mpi3mr_set_qd_for_all_vd_in_tg -set QD for TG VDs
1960 * @mrioc: Adapter instance reference
1961 * @tg: Throttle group information pointer
1963 * Accessor to reduce QD for each device associated with the
1964 * given throttle group.
1968 static void mpi3mr_set_qd_for_all_vd_in_tg(struct mpi3mr_ioc *mrioc,
1969 struct mpi3mr_throttle_group_info *tg)
1971 unsigned long flags;
1972 struct mpi3mr_tgt_dev *tgtdev;
1973 struct mpi3mr_stgt_priv_data *tgt_priv;
1976 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
1977 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
1978 if (tgtdev->starget && tgtdev->starget->hostdata) {
1979 tgt_priv = tgtdev->starget->hostdata;
1980 if (tgt_priv->throttle_group == tg) {
1981 dprint_event_bh(mrioc,
1982 "updating qd due to throttling for persist_id(%d) original_qd(%d), reduced_qd (%d)\n",
1983 tgt_priv->perst_id, tgtdev->q_depth,
1985 starget_for_each_device(tgtdev->starget,
1986 (void *)&tg->modified_qd,
1987 mpi3mr_update_sdev_qd);
1991 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
1995 * mpi3mr_fwevt_bh - Firmware event bottomhalf handler
1996 * @mrioc: Adapter instance reference
1997 * @fwevt: Firmware event reference
1999 * Identifies the firmware event and calls corresponding bottomg
2000 * half handler and sends event acknowledgment if required.
2004 static void mpi3mr_fwevt_bh(struct mpi3mr_ioc *mrioc,
2005 struct mpi3mr_fwevt *fwevt)
2007 struct mpi3_device_page0 *dev_pg0 = NULL;
2008 u16 perst_id, handle, dev_info;
2009 struct mpi3_device0_sas_sata_format *sasinf = NULL;
2010 unsigned int timeout;
2012 mpi3mr_fwevt_del_from_list(mrioc, fwevt);
2013 mrioc->current_event = fwevt;
2015 if (mrioc->stop_drv_processing)
2018 if (mrioc->unrecoverable) {
2019 dprint_event_bh(mrioc,
2020 "ignoring event(0x%02x) in bottom half handler due to unrecoverable controller\n",
2025 if (!fwevt->process_evt)
2028 switch (fwevt->event_id) {
2029 case MPI3_EVENT_DEVICE_ADDED:
2031 dev_pg0 = (struct mpi3_device_page0 *)fwevt->event_data;
2032 perst_id = le16_to_cpu(dev_pg0->persistent_id);
2033 handle = le16_to_cpu(dev_pg0->dev_handle);
2034 if (perst_id != MPI3_DEVICE0_PERSISTENTID_INVALID)
2035 mpi3mr_report_tgtdev_to_host(mrioc, perst_id);
2036 else if (mrioc->sas_transport_enabled &&
2037 (dev_pg0->device_form == MPI3_DEVICE_DEVFORM_SAS_SATA)) {
2038 sasinf = &dev_pg0->device_specific.sas_sata_format;
2039 dev_info = le16_to_cpu(sasinf->device_info);
2040 if (!mrioc->sas_hba.num_phys)
2041 mpi3mr_sas_host_add(mrioc);
2043 mpi3mr_sas_host_refresh(mrioc);
2045 if (mpi3mr_is_expander_device(dev_info))
2046 mpi3mr_expander_add(mrioc, handle);
2050 case MPI3_EVENT_DEVICE_INFO_CHANGED:
2052 dev_pg0 = (struct mpi3_device_page0 *)fwevt->event_data;
2053 perst_id = le16_to_cpu(dev_pg0->persistent_id);
2054 if (perst_id != MPI3_DEVICE0_PERSISTENTID_INVALID)
2055 mpi3mr_devinfochg_evt_bh(mrioc, dev_pg0);
2058 case MPI3_EVENT_DEVICE_STATUS_CHANGE:
2060 mpi3mr_devstatuschg_evt_bh(mrioc, fwevt);
2063 case MPI3_EVENT_ENCL_DEVICE_ADDED:
2064 case MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE:
2066 mpi3mr_encldev_add_chg_evt_bh(mrioc, fwevt);
2070 case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
2072 mpi3mr_sastopochg_evt_bh(mrioc, fwevt);
2075 case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
2077 mpi3mr_pcietopochg_evt_bh(mrioc, fwevt);
2080 case MPI3_EVENT_LOG_DATA:
2082 mpi3mr_logdata_evt_bh(mrioc, fwevt);
2085 case MPI3MR_DRIVER_EVENT_TG_QD_REDUCTION:
2087 struct mpi3mr_throttle_group_info *tg;
2089 tg = *(struct mpi3mr_throttle_group_info **)fwevt->event_data;
2090 dprint_event_bh(mrioc,
2091 "qd reduction event processed for tg_id(%d) reduction_needed(%d)\n",
2092 tg->id, tg->need_qd_reduction);
2093 if (tg->need_qd_reduction) {
2094 mpi3mr_set_qd_for_all_vd_in_tg(mrioc, tg);
2095 tg->need_qd_reduction = 0;
2099 case MPI3_EVENT_WAIT_FOR_DEVICES_TO_REFRESH:
2101 timeout = MPI3MR_RESET_TIMEOUT * 2;
2102 while ((mrioc->device_refresh_on || mrioc->block_on_pci_err) &&
2103 !mrioc->unrecoverable && !mrioc->pci_err_recovery) {
2106 mrioc->unrecoverable = 1;
2111 if (mrioc->unrecoverable || mrioc->pci_err_recovery)
2114 dprint_event_bh(mrioc,
2115 "scan for non responding and newly added devices after soft reset started\n");
2116 if (mrioc->sas_transport_enabled) {
2117 mpi3mr_refresh_sas_ports(mrioc);
2118 mpi3mr_refresh_expanders(mrioc);
2120 mpi3mr_refresh_tgtdevs(mrioc);
2122 "scan for non responding and newly added devices after soft reset completed\n");
2125 case MPI3MR_DRIVER_EVENT_PROCESS_TRIGGER:
2127 mpi3mr_process_trigger_data_event_bh(mrioc,
2128 (struct trigger_event_data *)fwevt->event_data);
2136 if (fwevt->send_ack)
2137 mpi3mr_process_event_ack(mrioc, fwevt->event_id,
2140 /* Put fwevt reference count to neutralize kref_init increment */
2141 mpi3mr_fwevt_put(fwevt);
2142 mrioc->current_event = NULL;
2146 * mpi3mr_fwevt_worker - Firmware event worker
2147 * @work: Work struct containing firmware event
2149 * Extracts the firmware event and calls mpi3mr_fwevt_bh.
2153 static void mpi3mr_fwevt_worker(struct work_struct *work)
2155 struct mpi3mr_fwevt *fwevt = container_of(work, struct mpi3mr_fwevt,
2157 mpi3mr_fwevt_bh(fwevt->mrioc, fwevt);
2159 * Put fwevt reference count after
2160 * dequeuing it from worker queue
2162 mpi3mr_fwevt_put(fwevt);
2166 * mpi3mr_create_tgtdev - Create and add a target device
2167 * @mrioc: Adapter instance reference
2168 * @dev_pg0: Device Page 0 data
2170 * If the device specified by the device page 0 data is not
2171 * present in the driver's internal list, allocate the memory
2172 * for the device, populate the data and add to the list, else
2173 * update the device data. The key is persistent ID.
2175 * Return: 0 on success, -ENOMEM on memory allocation failure
2177 static int mpi3mr_create_tgtdev(struct mpi3mr_ioc *mrioc,
2178 struct mpi3_device_page0 *dev_pg0)
2181 struct mpi3mr_tgt_dev *tgtdev = NULL;
2183 unsigned long flags;
2185 perst_id = le16_to_cpu(dev_pg0->persistent_id);
2186 if (perst_id == MPI3_DEVICE0_PERSISTENTID_INVALID)
2189 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
2190 tgtdev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, perst_id);
2192 tgtdev->state = MPI3MR_DEV_CREATED;
2193 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
2196 mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0, true);
2197 mpi3mr_tgtdev_put(tgtdev);
2199 tgtdev = mpi3mr_alloc_tgtdev();
2202 mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0, true);
2203 mpi3mr_tgtdev_add_to_list(mrioc, tgtdev);
2210 * mpi3mr_flush_delayed_cmd_lists - Flush pending commands
2211 * @mrioc: Adapter instance reference
2213 * Flush pending commands in the delayed lists due to a
2214 * controller reset or driver removal as a cleanup.
2218 void mpi3mr_flush_delayed_cmd_lists(struct mpi3mr_ioc *mrioc)
2220 struct delayed_dev_rmhs_node *_rmhs_node;
2221 struct delayed_evt_ack_node *_evtack_node;
2223 dprint_reset(mrioc, "flushing delayed dev_remove_hs commands\n");
2224 while (!list_empty(&mrioc->delayed_rmhs_list)) {
2225 _rmhs_node = list_entry(mrioc->delayed_rmhs_list.next,
2226 struct delayed_dev_rmhs_node, list);
2227 list_del(&_rmhs_node->list);
2230 dprint_reset(mrioc, "flushing delayed event ack commands\n");
2231 while (!list_empty(&mrioc->delayed_evtack_cmds_list)) {
2232 _evtack_node = list_entry(mrioc->delayed_evtack_cmds_list.next,
2233 struct delayed_evt_ack_node, list);
2234 list_del(&_evtack_node->list);
2235 kfree(_evtack_node);
2240 * mpi3mr_dev_rmhs_complete_iou - Device removal IOUC completion
2241 * @mrioc: Adapter instance reference
2242 * @drv_cmd: Internal command tracker
2244 * Issues a target reset TM to the firmware from the device
2245 * removal TM pend list or retry the removal handshake sequence
2246 * based on the IOU control request IOC status.
2250 static void mpi3mr_dev_rmhs_complete_iou(struct mpi3mr_ioc *mrioc,
2251 struct mpi3mr_drv_cmd *drv_cmd)
2253 u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
2254 struct delayed_dev_rmhs_node *delayed_dev_rmhs = NULL;
2256 if (drv_cmd->state & MPI3MR_CMD_RESET)
2260 "%s :dev_rmhs_iouctrl_complete:handle(0x%04x), ioc_status(0x%04x), loginfo(0x%08x)\n",
2261 __func__, drv_cmd->dev_handle, drv_cmd->ioc_status,
2262 drv_cmd->ioc_loginfo);
2263 if (drv_cmd->ioc_status != MPI3_IOCSTATUS_SUCCESS) {
2264 if (drv_cmd->retry_count < MPI3MR_DEV_RMHS_RETRY_COUNT) {
2265 drv_cmd->retry_count++;
2267 "%s :dev_rmhs_iouctrl_complete: handle(0x%04x)retrying handshake retry=%d\n",
2268 __func__, drv_cmd->dev_handle,
2269 drv_cmd->retry_count);
2270 mpi3mr_dev_rmhs_send_tm(mrioc, drv_cmd->dev_handle,
2271 drv_cmd, drv_cmd->iou_rc);
2275 "%s :dev removal handshake failed after all retries: handle(0x%04x)\n",
2276 __func__, drv_cmd->dev_handle);
2279 "%s :dev removal handshake completed successfully: handle(0x%04x)\n",
2280 __func__, drv_cmd->dev_handle);
2281 clear_bit(drv_cmd->dev_handle, mrioc->removepend_bitmap);
2284 if (!list_empty(&mrioc->delayed_rmhs_list)) {
2285 delayed_dev_rmhs = list_entry(mrioc->delayed_rmhs_list.next,
2286 struct delayed_dev_rmhs_node, list);
2287 drv_cmd->dev_handle = delayed_dev_rmhs->handle;
2288 drv_cmd->retry_count = 0;
2289 drv_cmd->iou_rc = delayed_dev_rmhs->iou_rc;
2291 "%s :dev_rmhs_iouctrl_complete: processing delayed TM: handle(0x%04x)\n",
2292 __func__, drv_cmd->dev_handle);
2293 mpi3mr_dev_rmhs_send_tm(mrioc, drv_cmd->dev_handle, drv_cmd,
2295 list_del(&delayed_dev_rmhs->list);
2296 kfree(delayed_dev_rmhs);
2301 drv_cmd->state = MPI3MR_CMD_NOTUSED;
2302 drv_cmd->callback = NULL;
2303 drv_cmd->retry_count = 0;
2304 drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
2305 clear_bit(cmd_idx, mrioc->devrem_bitmap);
2309 * mpi3mr_dev_rmhs_complete_tm - Device removal TM completion
2310 * @mrioc: Adapter instance reference
2311 * @drv_cmd: Internal command tracker
2313 * Issues a target reset TM to the firmware from the device
2314 * removal TM pend list or issue IO unit control request as
2315 * part of device removal or hidden acknowledgment handshake.
2319 static void mpi3mr_dev_rmhs_complete_tm(struct mpi3mr_ioc *mrioc,
2320 struct mpi3mr_drv_cmd *drv_cmd)
2322 struct mpi3_iounit_control_request iou_ctrl;
2323 u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
2324 struct mpi3_scsi_task_mgmt_reply *tm_reply = NULL;
2327 if (drv_cmd->state & MPI3MR_CMD_RESET)
2330 if (drv_cmd->state & MPI3MR_CMD_REPLY_VALID)
2331 tm_reply = (struct mpi3_scsi_task_mgmt_reply *)drv_cmd->reply;
2335 "dev_rmhs_tr_complete:handle(0x%04x), ioc_status(0x%04x), loginfo(0x%08x), term_count(%d)\n",
2336 mrioc->name, drv_cmd->dev_handle, drv_cmd->ioc_status,
2337 drv_cmd->ioc_loginfo,
2338 le32_to_cpu(tm_reply->termination_count));
2340 pr_info(IOCNAME "Issuing IOU CTL: handle(0x%04x) dev_rmhs idx(%d)\n",
2341 mrioc->name, drv_cmd->dev_handle, cmd_idx);
2343 memset(&iou_ctrl, 0, sizeof(iou_ctrl));
2345 drv_cmd->state = MPI3MR_CMD_PENDING;
2346 drv_cmd->is_waiting = 0;
2347 drv_cmd->callback = mpi3mr_dev_rmhs_complete_iou;
2348 iou_ctrl.operation = drv_cmd->iou_rc;
2349 iou_ctrl.param16[0] = cpu_to_le16(drv_cmd->dev_handle);
2350 iou_ctrl.host_tag = cpu_to_le16(drv_cmd->host_tag);
2351 iou_ctrl.function = MPI3_FUNCTION_IO_UNIT_CONTROL;
2353 retval = mpi3mr_admin_request_post(mrioc, &iou_ctrl, sizeof(iou_ctrl),
2356 pr_err(IOCNAME "Issue DevRmHsTMIOUCTL: Admin post failed\n",
2363 drv_cmd->state = MPI3MR_CMD_NOTUSED;
2364 drv_cmd->callback = NULL;
2365 drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
2366 drv_cmd->retry_count = 0;
2367 clear_bit(cmd_idx, mrioc->devrem_bitmap);
2371 * mpi3mr_dev_rmhs_send_tm - Issue TM for device removal
2372 * @mrioc: Adapter instance reference
2373 * @handle: Device handle
2374 * @cmdparam: Internal command tracker
2375 * @iou_rc: IO unit reason code
2377 * Issues a target reset TM to the firmware or add it to a pend
2378 * list as part of device removal or hidden acknowledgment
2383 static void mpi3mr_dev_rmhs_send_tm(struct mpi3mr_ioc *mrioc, u16 handle,
2384 struct mpi3mr_drv_cmd *cmdparam, u8 iou_rc)
2386 struct mpi3_scsi_task_mgmt_request tm_req;
2388 u16 cmd_idx = MPI3MR_NUM_DEVRMCMD;
2390 struct mpi3mr_drv_cmd *drv_cmd = cmdparam;
2391 struct delayed_dev_rmhs_node *delayed_dev_rmhs = NULL;
2392 struct mpi3mr_tgt_dev *tgtdev = NULL;
2393 unsigned long flags;
2395 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
2396 tgtdev = __mpi3mr_get_tgtdev_by_handle(mrioc, handle);
2397 if (tgtdev && (iou_rc == MPI3_CTRL_OP_REMOVE_DEVICE))
2398 tgtdev->state = MPI3MR_DEV_REMOVE_HS_STARTED;
2399 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
2404 cmd_idx = find_first_zero_bit(mrioc->devrem_bitmap,
2405 MPI3MR_NUM_DEVRMCMD);
2406 if (cmd_idx < MPI3MR_NUM_DEVRMCMD) {
2407 if (!test_and_set_bit(cmd_idx, mrioc->devrem_bitmap))
2409 cmd_idx = MPI3MR_NUM_DEVRMCMD;
2411 } while (retrycount--);
2413 if (cmd_idx >= MPI3MR_NUM_DEVRMCMD) {
2414 delayed_dev_rmhs = kzalloc(sizeof(*delayed_dev_rmhs),
2416 if (!delayed_dev_rmhs)
2418 INIT_LIST_HEAD(&delayed_dev_rmhs->list);
2419 delayed_dev_rmhs->handle = handle;
2420 delayed_dev_rmhs->iou_rc = iou_rc;
2421 list_add_tail(&delayed_dev_rmhs->list,
2422 &mrioc->delayed_rmhs_list);
2423 ioc_info(mrioc, "%s :DevRmHs: tr:handle(0x%04x) is postponed\n",
2427 drv_cmd = &mrioc->dev_rmhs_cmds[cmd_idx];
2430 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
2432 "%s :Issuing TR TM: for devhandle 0x%04x with dev_rmhs %d\n",
2433 __func__, handle, cmd_idx);
2435 memset(&tm_req, 0, sizeof(tm_req));
2436 if (drv_cmd->state & MPI3MR_CMD_PENDING) {
2437 ioc_err(mrioc, "%s :Issue TM: Command is in use\n", __func__);
2440 drv_cmd->state = MPI3MR_CMD_PENDING;
2441 drv_cmd->is_waiting = 0;
2442 drv_cmd->callback = mpi3mr_dev_rmhs_complete_tm;
2443 drv_cmd->dev_handle = handle;
2444 drv_cmd->iou_rc = iou_rc;
2445 tm_req.dev_handle = cpu_to_le16(handle);
2446 tm_req.task_type = MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
2447 tm_req.host_tag = cpu_to_le16(drv_cmd->host_tag);
2448 tm_req.task_host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INVALID);
2449 tm_req.function = MPI3_FUNCTION_SCSI_TASK_MGMT;
2451 set_bit(handle, mrioc->removepend_bitmap);
2452 retval = mpi3mr_admin_request_post(mrioc, &tm_req, sizeof(tm_req), 1);
2454 ioc_err(mrioc, "%s :Issue DevRmHsTM: Admin Post failed\n",
2461 drv_cmd->state = MPI3MR_CMD_NOTUSED;
2462 drv_cmd->callback = NULL;
2463 drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
2464 drv_cmd->retry_count = 0;
2465 clear_bit(cmd_idx, mrioc->devrem_bitmap);
2469 * mpi3mr_complete_evt_ack - event ack request completion
2470 * @mrioc: Adapter instance reference
2471 * @drv_cmd: Internal command tracker
2473 * This is the completion handler for non blocking event
2474 * acknowledgment sent to the firmware and this will issue any
2475 * pending event acknowledgment request.
2479 static void mpi3mr_complete_evt_ack(struct mpi3mr_ioc *mrioc,
2480 struct mpi3mr_drv_cmd *drv_cmd)
2482 u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
2483 struct delayed_evt_ack_node *delayed_evtack = NULL;
2485 if (drv_cmd->state & MPI3MR_CMD_RESET)
2488 if (drv_cmd->ioc_status != MPI3_IOCSTATUS_SUCCESS) {
2489 dprint_event_th(mrioc,
2490 "immediate event ack failed with ioc_status(0x%04x) log_info(0x%08x)\n",
2491 (drv_cmd->ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
2492 drv_cmd->ioc_loginfo);
2495 if (!list_empty(&mrioc->delayed_evtack_cmds_list)) {
2497 list_entry(mrioc->delayed_evtack_cmds_list.next,
2498 struct delayed_evt_ack_node, list);
2499 mpi3mr_send_event_ack(mrioc, delayed_evtack->event, drv_cmd,
2500 delayed_evtack->event_ctx);
2501 list_del(&delayed_evtack->list);
2502 kfree(delayed_evtack);
2506 drv_cmd->state = MPI3MR_CMD_NOTUSED;
2507 drv_cmd->callback = NULL;
2508 clear_bit(cmd_idx, mrioc->evtack_cmds_bitmap);
2512 * mpi3mr_send_event_ack - Issue event acknwoledgment request
2513 * @mrioc: Adapter instance reference
2514 * @event: MPI3 event id
2515 * @cmdparam: Internal command tracker
2516 * @event_ctx: event context
2518 * Issues event acknowledgment request to the firmware if there
2519 * is a free command to send the event ack else it to a pend
2520 * list so that it will be processed on a completion of a prior
2521 * event acknowledgment .
2525 static void mpi3mr_send_event_ack(struct mpi3mr_ioc *mrioc, u8 event,
2526 struct mpi3mr_drv_cmd *cmdparam, u32 event_ctx)
2528 struct mpi3_event_ack_request evtack_req;
2531 u16 cmd_idx = MPI3MR_NUM_EVTACKCMD;
2532 struct mpi3mr_drv_cmd *drv_cmd = cmdparam;
2533 struct delayed_evt_ack_node *delayed_evtack = NULL;
2536 dprint_event_th(mrioc,
2537 "sending delayed event ack in the top half for event(0x%02x), event_ctx(0x%08x)\n",
2541 dprint_event_th(mrioc,
2542 "sending event ack in the top half for event(0x%02x), event_ctx(0x%08x)\n",
2545 cmd_idx = find_first_zero_bit(mrioc->evtack_cmds_bitmap,
2546 MPI3MR_NUM_EVTACKCMD);
2547 if (cmd_idx < MPI3MR_NUM_EVTACKCMD) {
2548 if (!test_and_set_bit(cmd_idx,
2549 mrioc->evtack_cmds_bitmap))
2551 cmd_idx = MPI3MR_NUM_EVTACKCMD;
2553 } while (retrycount--);
2555 if (cmd_idx >= MPI3MR_NUM_EVTACKCMD) {
2556 delayed_evtack = kzalloc(sizeof(*delayed_evtack),
2558 if (!delayed_evtack)
2560 INIT_LIST_HEAD(&delayed_evtack->list);
2561 delayed_evtack->event = event;
2562 delayed_evtack->event_ctx = event_ctx;
2563 list_add_tail(&delayed_evtack->list,
2564 &mrioc->delayed_evtack_cmds_list);
2565 dprint_event_th(mrioc,
2566 "event ack in the top half for event(0x%02x), event_ctx(0x%08x) is postponed\n",
2570 drv_cmd = &mrioc->evtack_cmds[cmd_idx];
2573 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
2575 memset(&evtack_req, 0, sizeof(evtack_req));
2576 if (drv_cmd->state & MPI3MR_CMD_PENDING) {
2577 dprint_event_th(mrioc,
2578 "sending event ack failed due to command in use\n");
2581 drv_cmd->state = MPI3MR_CMD_PENDING;
2582 drv_cmd->is_waiting = 0;
2583 drv_cmd->callback = mpi3mr_complete_evt_ack;
2584 evtack_req.host_tag = cpu_to_le16(drv_cmd->host_tag);
2585 evtack_req.function = MPI3_FUNCTION_EVENT_ACK;
2586 evtack_req.event = event;
2587 evtack_req.event_context = cpu_to_le32(event_ctx);
2588 retval = mpi3mr_admin_request_post(mrioc, &evtack_req,
2589 sizeof(evtack_req), 1);
2591 dprint_event_th(mrioc,
2592 "posting event ack request is failed\n");
2596 dprint_event_th(mrioc,
2597 "event ack in the top half for event(0x%02x), event_ctx(0x%08x) is posted\n",
2602 drv_cmd->state = MPI3MR_CMD_NOTUSED;
2603 drv_cmd->callback = NULL;
2604 clear_bit(cmd_idx, mrioc->evtack_cmds_bitmap);
2608 * mpi3mr_pcietopochg_evt_th - PCIETopologyChange evt tophalf
2609 * @mrioc: Adapter instance reference
2610 * @event_reply: event data
2612 * Checks for the reason code and based on that either block I/O
2613 * to device, or unblock I/O to the device, or start the device
2614 * removal handshake with reason as remove with the firmware for
2619 static void mpi3mr_pcietopochg_evt_th(struct mpi3mr_ioc *mrioc,
2620 struct mpi3_event_notification_reply *event_reply)
2622 struct mpi3_event_data_pcie_topology_change_list *topo_evt =
2623 (struct mpi3_event_data_pcie_topology_change_list *)event_reply->event_data;
2627 struct mpi3mr_tgt_dev *tgtdev = NULL;
2628 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2630 for (i = 0; i < topo_evt->num_entries; i++) {
2631 handle = le16_to_cpu(topo_evt->port_entry[i].attached_dev_handle);
2634 reason_code = topo_evt->port_entry[i].port_status;
2635 scsi_tgt_priv_data = NULL;
2636 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
2637 if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
2638 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2639 tgtdev->starget->hostdata;
2640 switch (reason_code) {
2641 case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
2642 if (scsi_tgt_priv_data) {
2643 scsi_tgt_priv_data->dev_removed = 1;
2644 scsi_tgt_priv_data->dev_removedelay = 0;
2645 atomic_set(&scsi_tgt_priv_data->block_io, 0);
2647 mpi3mr_dev_rmhs_send_tm(mrioc, handle, NULL,
2648 MPI3_CTRL_OP_REMOVE_DEVICE);
2650 case MPI3_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
2651 if (scsi_tgt_priv_data) {
2652 scsi_tgt_priv_data->dev_removedelay = 1;
2653 atomic_inc(&scsi_tgt_priv_data->block_io);
2656 case MPI3_EVENT_PCIE_TOPO_PS_RESPONDING:
2657 if (scsi_tgt_priv_data &&
2658 scsi_tgt_priv_data->dev_removedelay) {
2659 scsi_tgt_priv_data->dev_removedelay = 0;
2660 atomic_dec_if_positive
2661 (&scsi_tgt_priv_data->block_io);
2664 case MPI3_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
2669 mpi3mr_tgtdev_put(tgtdev);
2674 * mpi3mr_sastopochg_evt_th - SASTopologyChange evt tophalf
2675 * @mrioc: Adapter instance reference
2676 * @event_reply: event data
2678 * Checks for the reason code and based on that either block I/O
2679 * to device, or unblock I/O to the device, or start the device
2680 * removal handshake with reason as remove with the firmware for
2685 static void mpi3mr_sastopochg_evt_th(struct mpi3mr_ioc *mrioc,
2686 struct mpi3_event_notification_reply *event_reply)
2688 struct mpi3_event_data_sas_topology_change_list *topo_evt =
2689 (struct mpi3_event_data_sas_topology_change_list *)event_reply->event_data;
2693 struct mpi3mr_tgt_dev *tgtdev = NULL;
2694 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2696 for (i = 0; i < topo_evt->num_entries; i++) {
2697 handle = le16_to_cpu(topo_evt->phy_entry[i].attached_dev_handle);
2700 reason_code = topo_evt->phy_entry[i].status &
2701 MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
2702 scsi_tgt_priv_data = NULL;
2703 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
2704 if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
2705 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2706 tgtdev->starget->hostdata;
2707 switch (reason_code) {
2708 case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
2709 if (scsi_tgt_priv_data) {
2710 scsi_tgt_priv_data->dev_removed = 1;
2711 scsi_tgt_priv_data->dev_removedelay = 0;
2712 atomic_set(&scsi_tgt_priv_data->block_io, 0);
2714 mpi3mr_dev_rmhs_send_tm(mrioc, handle, NULL,
2715 MPI3_CTRL_OP_REMOVE_DEVICE);
2717 case MPI3_EVENT_SAS_TOPO_PHY_RC_DELAY_NOT_RESPONDING:
2718 if (scsi_tgt_priv_data) {
2719 scsi_tgt_priv_data->dev_removedelay = 1;
2720 atomic_inc(&scsi_tgt_priv_data->block_io);
2723 case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
2724 if (scsi_tgt_priv_data &&
2725 scsi_tgt_priv_data->dev_removedelay) {
2726 scsi_tgt_priv_data->dev_removedelay = 0;
2727 atomic_dec_if_positive
2728 (&scsi_tgt_priv_data->block_io);
2731 case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
2736 mpi3mr_tgtdev_put(tgtdev);
2741 * mpi3mr_devstatuschg_evt_th - DeviceStatusChange evt tophalf
2742 * @mrioc: Adapter instance reference
2743 * @event_reply: event data
2745 * Checks for the reason code and based on that either block I/O
2746 * to device, or unblock I/O to the device, or start the device
2747 * removal handshake with reason as remove/hide acknowledgment
2748 * with the firmware.
2752 static void mpi3mr_devstatuschg_evt_th(struct mpi3mr_ioc *mrioc,
2753 struct mpi3_event_notification_reply *event_reply)
2756 u8 ublock = 0, block = 0, hide = 0, delete = 0, remove = 0;
2757 struct mpi3mr_tgt_dev *tgtdev = NULL;
2758 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2759 struct mpi3_event_data_device_status_change *evtdata =
2760 (struct mpi3_event_data_device_status_change *)event_reply->event_data;
2762 if (mrioc->stop_drv_processing)
2765 dev_handle = le16_to_cpu(evtdata->dev_handle);
2767 switch (evtdata->reason_code) {
2768 case MPI3_EVENT_DEV_STAT_RC_INT_DEVICE_RESET_STRT:
2769 case MPI3_EVENT_DEV_STAT_RC_INT_IT_NEXUS_RESET_STRT:
2772 case MPI3_EVENT_DEV_STAT_RC_HIDDEN:
2776 case MPI3_EVENT_DEV_STAT_RC_VD_NOT_RESPONDING:
2780 case MPI3_EVENT_DEV_STAT_RC_INT_DEVICE_RESET_CMP:
2781 case MPI3_EVENT_DEV_STAT_RC_INT_IT_NEXUS_RESET_CMP:
2788 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
2792 tgtdev->is_hidden = hide;
2793 if (tgtdev->starget && tgtdev->starget->hostdata) {
2794 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2795 tgtdev->starget->hostdata;
2797 atomic_inc(&scsi_tgt_priv_data->block_io);
2799 scsi_tgt_priv_data->dev_removed = 1;
2801 atomic_dec_if_positive(&scsi_tgt_priv_data->block_io);
2804 mpi3mr_dev_rmhs_send_tm(mrioc, dev_handle, NULL,
2805 MPI3_CTRL_OP_REMOVE_DEVICE);
2807 mpi3mr_dev_rmhs_send_tm(mrioc, dev_handle, NULL,
2808 MPI3_CTRL_OP_HIDDEN_ACK);
2812 mpi3mr_tgtdev_put(tgtdev);
2816 * mpi3mr_preparereset_evt_th - Prepare for reset event tophalf
2817 * @mrioc: Adapter instance reference
2818 * @event_reply: event data
2820 * Blocks and unblocks host level I/O based on the reason code
2824 static void mpi3mr_preparereset_evt_th(struct mpi3mr_ioc *mrioc,
2825 struct mpi3_event_notification_reply *event_reply)
2827 struct mpi3_event_data_prepare_for_reset *evtdata =
2828 (struct mpi3_event_data_prepare_for_reset *)event_reply->event_data;
2830 if (evtdata->reason_code == MPI3_EVENT_PREPARE_RESET_RC_START) {
2831 dprint_event_th(mrioc,
2832 "prepare for reset event top half with rc=start\n");
2833 if (mrioc->prepare_for_reset)
2835 mrioc->prepare_for_reset = 1;
2836 mrioc->prepare_for_reset_timeout_counter = 0;
2837 } else if (evtdata->reason_code == MPI3_EVENT_PREPARE_RESET_RC_ABORT) {
2838 dprint_event_th(mrioc,
2839 "prepare for reset top half with rc=abort\n");
2840 mrioc->prepare_for_reset = 0;
2841 mrioc->prepare_for_reset_timeout_counter = 0;
2843 if ((event_reply->msg_flags & MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_MASK)
2844 == MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_REQUIRED)
2845 mpi3mr_send_event_ack(mrioc, event_reply->event, NULL,
2846 le32_to_cpu(event_reply->event_context));
2850 * mpi3mr_energypackchg_evt_th - Energy pack change evt tophalf
2851 * @mrioc: Adapter instance reference
2852 * @event_reply: event data
2854 * Identifies the new shutdown timeout value and update.
2858 static void mpi3mr_energypackchg_evt_th(struct mpi3mr_ioc *mrioc,
2859 struct mpi3_event_notification_reply *event_reply)
2861 struct mpi3_event_data_energy_pack_change *evtdata =
2862 (struct mpi3_event_data_energy_pack_change *)event_reply->event_data;
2863 u16 shutdown_timeout = le16_to_cpu(evtdata->shutdown_timeout);
2865 if (shutdown_timeout <= 0) {
2867 "%s :Invalid Shutdown Timeout received = %d\n",
2868 __func__, shutdown_timeout);
2873 "%s :Previous Shutdown Timeout Value = %d New Shutdown Timeout Value = %d\n",
2874 __func__, mrioc->facts.shutdown_timeout, shutdown_timeout);
2875 mrioc->facts.shutdown_timeout = shutdown_timeout;
2879 * mpi3mr_cablemgmt_evt_th - Cable management event tophalf
2880 * @mrioc: Adapter instance reference
2881 * @event_reply: event data
2883 * Displays Cable manegemt event details.
2887 static void mpi3mr_cablemgmt_evt_th(struct mpi3mr_ioc *mrioc,
2888 struct mpi3_event_notification_reply *event_reply)
2890 struct mpi3_event_data_cable_management *evtdata =
2891 (struct mpi3_event_data_cable_management *)event_reply->event_data;
2893 switch (evtdata->status) {
2894 case MPI3_EVENT_CABLE_MGMT_STATUS_INSUFFICIENT_POWER:
2896 ioc_info(mrioc, "An active cable with receptacle_id %d cannot be powered.\n"
2897 "Devices connected to this cable are not detected.\n"
2898 "This cable requires %d mW of power.\n",
2899 evtdata->receptacle_id,
2900 le32_to_cpu(evtdata->active_cable_power_requirement));
2903 case MPI3_EVENT_CABLE_MGMT_STATUS_DEGRADED:
2905 ioc_info(mrioc, "A cable with receptacle_id %d is not running at optimal speed\n",
2906 evtdata->receptacle_id);
2915 * mpi3mr_add_event_wait_for_device_refresh - Add Wait for Device Refresh Event
2916 * @mrioc: Adapter instance reference
2918 * Add driver specific event to make sure that the driver won't process the
2919 * events until all the devices are refreshed during soft reset.
2923 void mpi3mr_add_event_wait_for_device_refresh(struct mpi3mr_ioc *mrioc)
2925 struct mpi3mr_fwevt *fwevt = NULL;
2927 fwevt = mpi3mr_alloc_fwevt(0);
2929 dprint_event_th(mrioc,
2930 "failed to schedule bottom half handler for event(0x%02x)\n",
2931 MPI3_EVENT_WAIT_FOR_DEVICES_TO_REFRESH);
2934 fwevt->mrioc = mrioc;
2935 fwevt->event_id = MPI3_EVENT_WAIT_FOR_DEVICES_TO_REFRESH;
2936 fwevt->send_ack = 0;
2937 fwevt->process_evt = 1;
2939 fwevt->event_data_size = 0;
2940 mpi3mr_fwevt_add_to_list(mrioc, fwevt);
2944 * mpi3mr_os_handle_events - Firmware event handler
2945 * @mrioc: Adapter instance reference
2946 * @event_reply: event data
2948 * Identify whteher the event has to handled and acknowledged
2949 * and either process the event in the tophalf and/or schedule a
2950 * bottom half through mpi3mr_fwevt_worker.
2954 void mpi3mr_os_handle_events(struct mpi3mr_ioc *mrioc,
2955 struct mpi3_event_notification_reply *event_reply)
2958 struct mpi3mr_fwevt *fwevt = NULL;
2959 bool ack_req = 0, process_evt_bh = 0;
2961 if (mrioc->stop_drv_processing)
2964 if ((event_reply->msg_flags & MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_MASK)
2965 == MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_REQUIRED)
2968 evt_type = event_reply->event;
2969 mpi3mr_event_trigger(mrioc, event_reply->event);
2972 case MPI3_EVENT_DEVICE_ADDED:
2974 struct mpi3_device_page0 *dev_pg0 =
2975 (struct mpi3_device_page0 *)event_reply->event_data;
2976 if (mpi3mr_create_tgtdev(mrioc, dev_pg0))
2978 "%s :Failed to add device in the device add event\n",
2984 case MPI3_EVENT_DEVICE_STATUS_CHANGE:
2987 mpi3mr_devstatuschg_evt_th(mrioc, event_reply);
2990 case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
2993 mpi3mr_sastopochg_evt_th(mrioc, event_reply);
2996 case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
2999 mpi3mr_pcietopochg_evt_th(mrioc, event_reply);
3002 case MPI3_EVENT_PREPARE_FOR_RESET:
3004 mpi3mr_preparereset_evt_th(mrioc, event_reply);
3008 case MPI3_EVENT_DIAGNOSTIC_BUFFER_STATUS_CHANGE:
3010 mpi3mr_hdbstatuschg_evt_th(mrioc, event_reply);
3013 case MPI3_EVENT_DEVICE_INFO_CHANGED:
3014 case MPI3_EVENT_LOG_DATA:
3015 case MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE:
3016 case MPI3_EVENT_ENCL_DEVICE_ADDED:
3021 case MPI3_EVENT_ENERGY_PACK_CHANGE:
3023 mpi3mr_energypackchg_evt_th(mrioc, event_reply);
3026 case MPI3_EVENT_CABLE_MGMT:
3028 mpi3mr_cablemgmt_evt_th(mrioc, event_reply);
3031 case MPI3_EVENT_SAS_DISCOVERY:
3032 case MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
3033 case MPI3_EVENT_SAS_BROADCAST_PRIMITIVE:
3034 case MPI3_EVENT_PCIE_ENUMERATION:
3037 ioc_info(mrioc, "%s :event 0x%02x is not handled\n",
3038 __func__, evt_type);
3041 if (process_evt_bh || ack_req) {
3042 sz = event_reply->event_data_length * 4;
3043 fwevt = mpi3mr_alloc_fwevt(sz);
3045 ioc_info(mrioc, "%s :failure at %s:%d/%s()!\n",
3046 __func__, __FILE__, __LINE__, __func__);
3050 memcpy(fwevt->event_data, event_reply->event_data, sz);
3051 fwevt->mrioc = mrioc;
3052 fwevt->event_id = evt_type;
3053 fwevt->send_ack = ack_req;
3054 fwevt->process_evt = process_evt_bh;
3055 fwevt->evt_ctx = le32_to_cpu(event_reply->event_context);
3056 mpi3mr_fwevt_add_to_list(mrioc, fwevt);
3061 * mpi3mr_setup_eedp - Setup EEDP information in MPI3 SCSI IO
3062 * @mrioc: Adapter instance reference
3063 * @scmd: SCSI command reference
3064 * @scsiio_req: MPI3 SCSI IO request
3066 * Identifies the protection information flags from the SCSI
3067 * command and set appropriate flags in the MPI3 SCSI IO
3072 static void mpi3mr_setup_eedp(struct mpi3mr_ioc *mrioc,
3073 struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
3076 unsigned char prot_op = scsi_get_prot_op(scmd);
3079 case SCSI_PROT_NORMAL:
3081 case SCSI_PROT_READ_STRIP:
3082 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REMOVE;
3084 case SCSI_PROT_WRITE_INSERT:
3085 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_INSERT;
3087 case SCSI_PROT_READ_INSERT:
3088 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_INSERT;
3089 scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
3091 case SCSI_PROT_WRITE_STRIP:
3092 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REMOVE;
3093 scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
3095 case SCSI_PROT_READ_PASS:
3096 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK;
3097 scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
3099 case SCSI_PROT_WRITE_PASS:
3100 if (scmd->prot_flags & SCSI_PROT_IP_CHECKSUM) {
3101 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REGEN;
3102 scsiio_req->sgl[0].eedp.application_tag_translation_mask =
3105 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK;
3107 scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
3113 if (scmd->prot_flags & SCSI_PROT_GUARD_CHECK)
3114 eedp_flags |= MPI3_EEDPFLAGS_CHK_GUARD;
3116 if (scmd->prot_flags & SCSI_PROT_IP_CHECKSUM)
3117 eedp_flags |= MPI3_EEDPFLAGS_HOST_GUARD_IP_CHKSUM;
3119 if (scmd->prot_flags & SCSI_PROT_REF_CHECK) {
3120 eedp_flags |= MPI3_EEDPFLAGS_CHK_REF_TAG |
3121 MPI3_EEDPFLAGS_INCR_PRI_REF_TAG;
3122 scsiio_req->cdb.eedp32.primary_reference_tag =
3123 cpu_to_be32(scsi_prot_ref_tag(scmd));
3126 if (scmd->prot_flags & SCSI_PROT_REF_INCREMENT)
3127 eedp_flags |= MPI3_EEDPFLAGS_INCR_PRI_REF_TAG;
3129 eedp_flags |= MPI3_EEDPFLAGS_ESC_MODE_APPTAG_DISABLE;
3131 switch (scsi_prot_interval(scmd)) {
3133 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_512;
3136 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_520;
3139 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4080;
3142 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4088;
3145 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4096;
3148 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4104;
3151 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4160;
3157 scsiio_req->sgl[0].eedp.eedp_flags = cpu_to_le16(eedp_flags);
3158 scsiio_req->sgl[0].eedp.flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_EXTENDED;
3162 * mpi3mr_build_sense_buffer - Map sense information
3164 * @buf: Sense buffer to populate
3166 * @asc: Additional sense code
3167 * @ascq: Additional sense code qualifier
3169 * Maps the given sense information into either descriptor or
3170 * fixed format sense data.
3174 static inline void mpi3mr_build_sense_buffer(int desc, u8 *buf, u8 key,
3178 buf[0] = 0x72; /* descriptor, current */
3184 buf[0] = 0x70; /* fixed, current */
3193 * mpi3mr_map_eedp_error - Map EEDP errors from IOC status
3194 * @scmd: SCSI command reference
3195 * @ioc_status: status of MPI3 request
3197 * Maps the EEDP error status of the SCSI IO request to sense
3202 static void mpi3mr_map_eedp_error(struct scsi_cmnd *scmd,
3207 switch (ioc_status) {
3208 case MPI3_IOCSTATUS_EEDP_GUARD_ERROR:
3211 case MPI3_IOCSTATUS_EEDP_APP_TAG_ERROR:
3214 case MPI3_IOCSTATUS_EEDP_REF_TAG_ERROR:
3222 mpi3mr_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
3224 scmd->result = (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION;
3228 * mpi3mr_process_op_reply_desc - reply descriptor handler
3229 * @mrioc: Adapter instance reference
3230 * @reply_desc: Operational reply descriptor
3231 * @reply_dma: place holder for reply DMA address
3232 * @qidx: Operational queue index
3234 * Process the operational reply descriptor and identifies the
3235 * descriptor type. Based on the descriptor map the MPI3 request
3236 * status to a SCSI command status and calls scsi_done call
3241 void mpi3mr_process_op_reply_desc(struct mpi3mr_ioc *mrioc,
3242 struct mpi3_default_reply_descriptor *reply_desc, u64 *reply_dma, u16 qidx)
3244 u16 reply_desc_type, host_tag = 0;
3245 u16 ioc_status = MPI3_IOCSTATUS_SUCCESS;
3246 u32 ioc_loginfo = 0;
3247 struct mpi3_status_reply_descriptor *status_desc = NULL;
3248 struct mpi3_address_reply_descriptor *addr_desc = NULL;
3249 struct mpi3_success_reply_descriptor *success_desc = NULL;
3250 struct mpi3_scsi_io_reply *scsi_reply = NULL;
3251 struct scsi_cmnd *scmd = NULL;
3252 struct scmd_priv *priv = NULL;
3253 u8 *sense_buf = NULL;
3254 u8 scsi_state = 0, scsi_status = 0, sense_state = 0;
3255 u32 xfer_count = 0, sense_count = 0, resp_data = 0;
3256 u16 dev_handle = 0xFFFF;
3257 struct scsi_sense_hdr sshdr;
3258 struct mpi3mr_stgt_priv_data *stgt_priv_data = NULL;
3259 struct mpi3mr_sdev_priv_data *sdev_priv_data = NULL;
3260 u32 ioc_pend_data_len = 0, tg_pend_data_len = 0, data_len_blks = 0;
3261 struct mpi3mr_throttle_group_info *tg = NULL;
3262 u8 throttle_enabled_dev = 0;
3265 reply_desc_type = le16_to_cpu(reply_desc->reply_flags) &
3266 MPI3_REPLY_DESCRIPT_FLAGS_TYPE_MASK;
3267 switch (reply_desc_type) {
3268 case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_STATUS:
3269 status_desc = (struct mpi3_status_reply_descriptor *)reply_desc;
3270 host_tag = le16_to_cpu(status_desc->host_tag);
3271 ioc_status = le16_to_cpu(status_desc->ioc_status);
3273 MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
3274 ioc_loginfo = le32_to_cpu(status_desc->ioc_log_info);
3275 ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
3276 mpi3mr_reply_trigger(mrioc, ioc_status, ioc_loginfo);
3278 case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_ADDRESS_REPLY:
3279 addr_desc = (struct mpi3_address_reply_descriptor *)reply_desc;
3280 *reply_dma = le64_to_cpu(addr_desc->reply_frame_address);
3281 scsi_reply = mpi3mr_get_reply_virt_addr(mrioc,
3284 panic("%s: scsi_reply is NULL, this shouldn't happen\n",
3288 host_tag = le16_to_cpu(scsi_reply->host_tag);
3289 ioc_status = le16_to_cpu(scsi_reply->ioc_status);
3290 scsi_status = scsi_reply->scsi_status;
3291 scsi_state = scsi_reply->scsi_state;
3292 dev_handle = le16_to_cpu(scsi_reply->dev_handle);
3293 sense_state = (scsi_state & MPI3_SCSI_STATE_SENSE_MASK);
3294 xfer_count = le32_to_cpu(scsi_reply->transfer_count);
3295 sense_count = le32_to_cpu(scsi_reply->sense_count);
3296 resp_data = le32_to_cpu(scsi_reply->response_data);
3297 sense_buf = mpi3mr_get_sensebuf_virt_addr(mrioc,
3298 le64_to_cpu(scsi_reply->sense_data_buffer_address));
3300 MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
3301 ioc_loginfo = le32_to_cpu(scsi_reply->ioc_log_info);
3302 ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
3303 if (sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY)
3304 panic("%s: Ran out of sense buffers\n", mrioc->name);
3306 scsi_normalize_sense(sense_buf, sense_count, &sshdr);
3307 mpi3mr_scsisense_trigger(mrioc, sshdr.sense_key,
3308 sshdr.asc, sshdr.ascq);
3310 mpi3mr_reply_trigger(mrioc, ioc_status, ioc_loginfo);
3312 case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_SUCCESS:
3313 success_desc = (struct mpi3_success_reply_descriptor *)reply_desc;
3314 host_tag = le16_to_cpu(success_desc->host_tag);
3319 scmd = mpi3mr_scmd_from_host_tag(mrioc, host_tag, qidx);
3321 panic("%s: Cannot Identify scmd for host_tag 0x%x\n",
3322 mrioc->name, host_tag);
3325 priv = scsi_cmd_priv(scmd);
3327 data_len_blks = scsi_bufflen(scmd) >> 9;
3328 sdev_priv_data = scmd->device->hostdata;
3329 if (sdev_priv_data) {
3330 stgt_priv_data = sdev_priv_data->tgt_priv_data;
3331 if (stgt_priv_data) {
3332 tg = stgt_priv_data->throttle_group;
3333 throttle_enabled_dev =
3334 stgt_priv_data->io_throttle_enabled;
3335 dev_handle = stgt_priv_data->dev_handle;
3338 if (unlikely((data_len_blks >= mrioc->io_throttle_data_length) &&
3339 throttle_enabled_dev)) {
3340 ioc_pend_data_len = atomic_sub_return(data_len_blks,
3341 &mrioc->pend_large_data_sz);
3343 tg_pend_data_len = atomic_sub_return(data_len_blks,
3344 &tg->pend_large_data_sz);
3345 if (tg->io_divert && ((ioc_pend_data_len <=
3346 mrioc->io_throttle_low) &&
3347 (tg_pend_data_len <= tg->low))) {
3349 mpi3mr_set_io_divert_for_all_vd_in_tg(
3353 if (ioc_pend_data_len <= mrioc->io_throttle_low)
3354 stgt_priv_data->io_divert = 0;
3356 } else if (unlikely((stgt_priv_data && stgt_priv_data->io_divert))) {
3357 ioc_pend_data_len = atomic_read(&mrioc->pend_large_data_sz);
3359 if (ioc_pend_data_len <= mrioc->io_throttle_low)
3360 stgt_priv_data->io_divert = 0;
3362 } else if (ioc_pend_data_len <= mrioc->io_throttle_low) {
3363 tg_pend_data_len = atomic_read(&tg->pend_large_data_sz);
3364 if (tg->io_divert && (tg_pend_data_len <= tg->low)) {
3366 mpi3mr_set_io_divert_for_all_vd_in_tg(
3373 scmd->result = DID_OK << 16;
3377 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_count);
3378 if (ioc_status == MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN &&
3379 xfer_count == 0 && (scsi_status == MPI3_SCSI_STATUS_BUSY ||
3380 scsi_status == MPI3_SCSI_STATUS_RESERVATION_CONFLICT ||
3381 scsi_status == MPI3_SCSI_STATUS_TASK_SET_FULL))
3382 ioc_status = MPI3_IOCSTATUS_SUCCESS;
3384 if ((sense_state == MPI3_SCSI_STATE_SENSE_VALID) && sense_count &&
3386 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE, sense_count);
3388 memcpy(scmd->sense_buffer, sense_buf, sz);
3391 switch (ioc_status) {
3392 case MPI3_IOCSTATUS_BUSY:
3393 case MPI3_IOCSTATUS_INSUFFICIENT_RESOURCES:
3394 scmd->result = SAM_STAT_BUSY;
3396 case MPI3_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3397 scmd->result = DID_NO_CONNECT << 16;
3399 case MPI3_IOCSTATUS_SCSI_IOC_TERMINATED:
3400 scmd->result = DID_SOFT_ERROR << 16;
3402 case MPI3_IOCSTATUS_SCSI_TASK_TERMINATED:
3403 case MPI3_IOCSTATUS_SCSI_EXT_TERMINATED:
3404 scmd->result = DID_RESET << 16;
3406 case MPI3_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3407 if ((xfer_count == 0) || (scmd->underflow > xfer_count))
3408 scmd->result = DID_SOFT_ERROR << 16;
3410 scmd->result = (DID_OK << 16) | scsi_status;
3412 case MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN:
3413 scmd->result = (DID_OK << 16) | scsi_status;
3414 if (sense_state == MPI3_SCSI_STATE_SENSE_VALID)
3416 if (xfer_count < scmd->underflow) {
3417 if (scsi_status == SAM_STAT_BUSY)
3418 scmd->result = SAM_STAT_BUSY;
3420 scmd->result = DID_SOFT_ERROR << 16;
3421 } else if ((scsi_state & (MPI3_SCSI_STATE_NO_SCSI_STATUS)) ||
3422 (sense_state != MPI3_SCSI_STATE_SENSE_NOT_AVAILABLE))
3423 scmd->result = DID_SOFT_ERROR << 16;
3424 else if (scsi_state & MPI3_SCSI_STATE_TERMINATED)
3425 scmd->result = DID_RESET << 16;
3427 case MPI3_IOCSTATUS_SCSI_DATA_OVERRUN:
3428 scsi_set_resid(scmd, 0);
3430 case MPI3_IOCSTATUS_SCSI_RECOVERED_ERROR:
3431 case MPI3_IOCSTATUS_SUCCESS:
3432 scmd->result = (DID_OK << 16) | scsi_status;
3433 if ((scsi_state & (MPI3_SCSI_STATE_NO_SCSI_STATUS)) ||
3434 (sense_state == MPI3_SCSI_STATE_SENSE_FAILED) ||
3435 (sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY))
3436 scmd->result = DID_SOFT_ERROR << 16;
3437 else if (scsi_state & MPI3_SCSI_STATE_TERMINATED)
3438 scmd->result = DID_RESET << 16;
3440 case MPI3_IOCSTATUS_EEDP_GUARD_ERROR:
3441 case MPI3_IOCSTATUS_EEDP_REF_TAG_ERROR:
3442 case MPI3_IOCSTATUS_EEDP_APP_TAG_ERROR:
3443 mpi3mr_map_eedp_error(scmd, ioc_status);
3445 case MPI3_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3446 case MPI3_IOCSTATUS_INVALID_FUNCTION:
3447 case MPI3_IOCSTATUS_INVALID_SGL:
3448 case MPI3_IOCSTATUS_INTERNAL_ERROR:
3449 case MPI3_IOCSTATUS_INVALID_FIELD:
3450 case MPI3_IOCSTATUS_INVALID_STATE:
3451 case MPI3_IOCSTATUS_SCSI_IO_DATA_ERROR:
3452 case MPI3_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3453 case MPI3_IOCSTATUS_INSUFFICIENT_POWER:
3455 scmd->result = DID_SOFT_ERROR << 16;
3459 if (scmd->result != (DID_OK << 16) && (scmd->cmnd[0] != ATA_12) &&
3460 (scmd->cmnd[0] != ATA_16) &&
3461 mrioc->logging_level & MPI3_DEBUG_SCSI_ERROR) {
3462 ioc_info(mrioc, "%s :scmd->result 0x%x\n", __func__,
3464 scsi_print_command(scmd);
3466 "%s :Command issued to handle 0x%02x returned with error 0x%04x loginfo 0x%08x, qid %d\n",
3467 __func__, dev_handle, ioc_status, ioc_loginfo,
3468 priv->req_q_idx + 1);
3470 " host_tag %d scsi_state 0x%02x scsi_status 0x%02x, xfer_cnt %d resp_data 0x%x\n",
3471 host_tag, scsi_state, scsi_status, xfer_count, resp_data);
3473 scsi_normalize_sense(sense_buf, sense_count, &sshdr);
3475 "%s :sense_count 0x%x, sense_key 0x%x ASC 0x%x, ASCQ 0x%x\n",
3476 __func__, sense_count, sshdr.sense_key,
3477 sshdr.asc, sshdr.ascq);
3481 if (priv->meta_sg_valid) {
3482 dma_unmap_sg(&mrioc->pdev->dev, scsi_prot_sglist(scmd),
3483 scsi_prot_sg_count(scmd), scmd->sc_data_direction);
3485 mpi3mr_clear_scmd_priv(mrioc, scmd);
3486 scsi_dma_unmap(scmd);
3490 mpi3mr_repost_sense_buf(mrioc,
3491 le64_to_cpu(scsi_reply->sense_data_buffer_address));
3495 * mpi3mr_get_chain_idx - get free chain buffer index
3496 * @mrioc: Adapter instance reference
3498 * Try to get a free chain buffer index from the free pool.
3500 * Return: -1 on failure or the free chain buffer index
3502 static int mpi3mr_get_chain_idx(struct mpi3mr_ioc *mrioc)
3506 unsigned long flags;
3508 spin_lock_irqsave(&mrioc->chain_buf_lock, flags);
3510 cmd_idx = find_first_zero_bit(mrioc->chain_bitmap,
3511 mrioc->chain_buf_count);
3512 if (cmd_idx < mrioc->chain_buf_count) {
3513 set_bit(cmd_idx, mrioc->chain_bitmap);
3517 } while (retry_count--);
3518 spin_unlock_irqrestore(&mrioc->chain_buf_lock, flags);
3523 * mpi3mr_prepare_sg_scmd - build scatter gather list
3524 * @mrioc: Adapter instance reference
3525 * @scmd: SCSI command reference
3526 * @scsiio_req: MPI3 SCSI IO request
3528 * This function maps SCSI command's data and protection SGEs to
3529 * MPI request SGEs. If required additional 4K chain buffer is
3530 * used to send the SGEs.
3532 * Return: 0 on success, -ENOMEM on dma_map_sg failure
3534 static int mpi3mr_prepare_sg_scmd(struct mpi3mr_ioc *mrioc,
3535 struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
3537 dma_addr_t chain_dma;
3538 struct scatterlist *sg_scmd;
3539 void *sg_local, *chain;
3541 int sges_left, chain_idx;
3542 u32 sges_in_segment;
3543 u8 simple_sgl_flags;
3544 u8 simple_sgl_flags_last;
3545 u8 last_chain_sgl_flags;
3546 struct chain_element *chain_req;
3547 struct scmd_priv *priv = NULL;
3548 u32 meta_sg = le32_to_cpu(scsiio_req->flags) &
3549 MPI3_SCSIIO_FLAGS_DMAOPERATION_HOST_PI;
3551 priv = scsi_cmd_priv(scmd);
3553 simple_sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_SIMPLE |
3554 MPI3_SGE_FLAGS_DLAS_SYSTEM;
3555 simple_sgl_flags_last = simple_sgl_flags |
3556 MPI3_SGE_FLAGS_END_OF_LIST;
3557 last_chain_sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_LAST_CHAIN |
3558 MPI3_SGE_FLAGS_DLAS_SYSTEM;
3561 sg_local = &scsiio_req->sgl[MPI3_SCSIIO_METASGL_INDEX];
3563 sg_local = &scsiio_req->sgl;
3565 if (!scsiio_req->data_length && !meta_sg) {
3566 mpi3mr_build_zero_len_sge(sg_local);
3571 sg_scmd = scsi_prot_sglist(scmd);
3572 sges_left = dma_map_sg(&mrioc->pdev->dev,
3573 scsi_prot_sglist(scmd),
3574 scsi_prot_sg_count(scmd),
3575 scmd->sc_data_direction);
3576 priv->meta_sg_valid = 1; /* To unmap meta sg DMA */
3579 * Some firmware versions byte-swap the REPORT ZONES command
3580 * reply from ATA-ZAC devices by directly accessing in the host
3581 * buffer. This does not respect the default command DMA
3582 * direction and causes IOMMU page faults on some architectures
3583 * with an IOMMU enforcing write mappings (e.g. AMD hosts).
3584 * Avoid such issue by making the REPORT ZONES buffer mapping
3587 if (scmd->cmnd[0] == ZBC_IN && scmd->cmnd[1] == ZI_REPORT_ZONES)
3588 scmd->sc_data_direction = DMA_BIDIRECTIONAL;
3589 sg_scmd = scsi_sglist(scmd);
3590 sges_left = scsi_dma_map(scmd);
3593 if (sges_left < 0) {
3594 sdev_printk(KERN_ERR, scmd->device,
3595 "scsi_dma_map failed: request for %d bytes!\n",
3596 scsi_bufflen(scmd));
3599 if (sges_left > mrioc->max_sgl_entries) {
3600 sdev_printk(KERN_ERR, scmd->device,
3601 "scsi_dma_map returned unsupported sge count %d!\n",
3606 sges_in_segment = (mrioc->facts.op_req_sz -
3607 offsetof(struct mpi3_scsi_io_request, sgl)) / sizeof(struct mpi3_sge_common);
3609 if (scsiio_req->sgl[0].eedp.flags ==
3610 MPI3_SGE_FLAGS_ELEMENT_TYPE_EXTENDED && !meta_sg) {
3611 sg_local += sizeof(struct mpi3_sge_common);
3613 /* Reserve 1st segment (scsiio_req->sgl[0]) for eedp */
3616 if (scsiio_req->msg_flags ==
3617 MPI3_SCSIIO_MSGFLAGS_METASGL_VALID && !meta_sg) {
3619 /* Reserve last segment (scsiio_req->sgl[3]) for meta sg */
3623 sges_in_segment = 1;
3625 if (sges_left <= sges_in_segment)
3626 goto fill_in_last_segment;
3628 /* fill in main message segment when there is a chain following */
3629 while (sges_in_segment > 1) {
3630 mpi3mr_add_sg_single(sg_local, simple_sgl_flags,
3631 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
3632 sg_scmd = sg_next(sg_scmd);
3633 sg_local += sizeof(struct mpi3_sge_common);
3638 chain_idx = mpi3mr_get_chain_idx(mrioc);
3641 chain_req = &mrioc->chain_sgl_list[chain_idx];
3643 priv->meta_chain_idx = chain_idx;
3645 priv->chain_idx = chain_idx;
3647 chain = chain_req->addr;
3648 chain_dma = chain_req->dma_addr;
3649 sges_in_segment = sges_left;
3650 chain_length = sges_in_segment * sizeof(struct mpi3_sge_common);
3652 mpi3mr_add_sg_single(sg_local, last_chain_sgl_flags,
3653 chain_length, chain_dma);
3657 fill_in_last_segment:
3658 while (sges_left > 0) {
3660 mpi3mr_add_sg_single(sg_local,
3661 simple_sgl_flags_last, sg_dma_len(sg_scmd),
3662 sg_dma_address(sg_scmd));
3664 mpi3mr_add_sg_single(sg_local, simple_sgl_flags,
3665 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
3666 sg_scmd = sg_next(sg_scmd);
3667 sg_local += sizeof(struct mpi3_sge_common);
3675 * mpi3mr_build_sg_scmd - build scatter gather list for SCSI IO
3676 * @mrioc: Adapter instance reference
3677 * @scmd: SCSI command reference
3678 * @scsiio_req: MPI3 SCSI IO request
3680 * This function calls mpi3mr_prepare_sg_scmd for constructing
3681 * both data SGEs and protection information SGEs in the MPI
3682 * format from the SCSI Command as appropriate .
3684 * Return: return value of mpi3mr_prepare_sg_scmd.
3686 static int mpi3mr_build_sg_scmd(struct mpi3mr_ioc *mrioc,
3687 struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
3691 ret = mpi3mr_prepare_sg_scmd(mrioc, scmd, scsiio_req);
3695 if (scsiio_req->msg_flags == MPI3_SCSIIO_MSGFLAGS_METASGL_VALID) {
3696 /* There is a valid meta sg */
3697 scsiio_req->flags |=
3698 cpu_to_le32(MPI3_SCSIIO_FLAGS_DMAOPERATION_HOST_PI);
3699 ret = mpi3mr_prepare_sg_scmd(mrioc, scmd, scsiio_req);
3706 * mpi3mr_tm_response_name - get TM response as a string
3707 * @resp_code: TM response code
3709 * Convert known task management response code as a readable
3712 * Return: response code string.
3714 static const char *mpi3mr_tm_response_name(u8 resp_code)
3718 switch (resp_code) {
3719 case MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE:
3720 desc = "task management request completed";
3722 case MPI3_SCSITASKMGMT_RSPCODE_INVALID_FRAME:
3723 desc = "invalid frame";
3725 case MPI3_SCSITASKMGMT_RSPCODE_TM_FUNCTION_NOT_SUPPORTED:
3726 desc = "task management request not supported";
3728 case MPI3_SCSITASKMGMT_RSPCODE_TM_FAILED:
3729 desc = "task management request failed";
3731 case MPI3_SCSITASKMGMT_RSPCODE_TM_SUCCEEDED:
3732 desc = "task management request succeeded";
3734 case MPI3_SCSITASKMGMT_RSPCODE_TM_INVALID_LUN:
3735 desc = "invalid LUN";
3737 case MPI3_SCSITASKMGMT_RSPCODE_TM_OVERLAPPED_TAG:
3738 desc = "overlapped tag attempted";
3740 case MPI3_SCSITASKMGMT_RSPCODE_IO_QUEUED_ON_IOC:
3741 desc = "task queued, however not sent to target";
3743 case MPI3_SCSITASKMGMT_RSPCODE_TM_NVME_DENIED:
3744 desc = "task management request denied by NVMe device";
3754 inline void mpi3mr_poll_pend_io_completions(struct mpi3mr_ioc *mrioc)
3757 int num_of_reply_queues =
3758 mrioc->num_op_reply_q + mrioc->op_reply_q_offset;
3760 for (i = mrioc->op_reply_q_offset; i < num_of_reply_queues; i++)
3761 mpi3mr_process_op_reply_q(mrioc,
3762 mrioc->intr_info[i].op_reply_q);
3766 * mpi3mr_issue_tm - Issue Task Management request
3767 * @mrioc: Adapter instance reference
3768 * @tm_type: Task Management type
3769 * @handle: Device handle
3771 * @htag: Host tag of the TM request
3772 * @timeout: TM timeout value
3773 * @drv_cmd: Internal command tracker
3774 * @resp_code: Response code place holder
3775 * @scmd: SCSI command
3777 * Issues a Task Management Request to the controller for a
3778 * specified target, lun and command and wait for its completion
3779 * and check TM response. Recover the TM if it timed out by
3780 * issuing controller reset.
3782 * Return: 0 on success, non-zero on errors
3784 int mpi3mr_issue_tm(struct mpi3mr_ioc *mrioc, u8 tm_type,
3785 u16 handle, uint lun, u16 htag, ulong timeout,
3786 struct mpi3mr_drv_cmd *drv_cmd,
3787 u8 *resp_code, struct scsi_cmnd *scmd)
3789 struct mpi3_scsi_task_mgmt_request tm_req;
3790 struct mpi3_scsi_task_mgmt_reply *tm_reply = NULL;
3792 struct mpi3mr_tgt_dev *tgtdev = NULL;
3793 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
3794 struct scmd_priv *cmd_priv = NULL;
3795 struct scsi_device *sdev = NULL;
3796 struct mpi3mr_sdev_priv_data *sdev_priv_data = NULL;
3798 ioc_info(mrioc, "%s :Issue TM: TM type (0x%x) for devhandle 0x%04x\n",
3799 __func__, tm_type, handle);
3800 if (mrioc->unrecoverable) {
3802 ioc_err(mrioc, "%s :Issue TM: Unrecoverable controller\n",
3807 memset(&tm_req, 0, sizeof(tm_req));
3808 mutex_lock(&drv_cmd->mutex);
3809 if (drv_cmd->state & MPI3MR_CMD_PENDING) {
3811 ioc_err(mrioc, "%s :Issue TM: Command is in use\n", __func__);
3812 mutex_unlock(&drv_cmd->mutex);
3815 if (mrioc->reset_in_progress) {
3817 ioc_err(mrioc, "%s :Issue TM: Reset in progress\n", __func__);
3818 mutex_unlock(&drv_cmd->mutex);
3821 if (mrioc->block_on_pci_err) {
3823 dprint_tm(mrioc, "sending task management failed due to\n"
3824 "pci error recovery in progress\n");
3825 mutex_unlock(&drv_cmd->mutex);
3829 drv_cmd->state = MPI3MR_CMD_PENDING;
3830 drv_cmd->is_waiting = 1;
3831 drv_cmd->callback = NULL;
3832 tm_req.dev_handle = cpu_to_le16(handle);
3833 tm_req.task_type = tm_type;
3834 tm_req.host_tag = cpu_to_le16(htag);
3836 int_to_scsilun(lun, (struct scsi_lun *)tm_req.lun);
3837 tm_req.function = MPI3_FUNCTION_SCSI_TASK_MGMT;
3839 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
3842 sdev = scmd->device;
3843 sdev_priv_data = sdev->hostdata;
3844 scsi_tgt_priv_data = ((sdev_priv_data) ?
3845 sdev_priv_data->tgt_priv_data : NULL);
3847 if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
3848 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
3849 tgtdev->starget->hostdata;
3852 if (scsi_tgt_priv_data)
3853 atomic_inc(&scsi_tgt_priv_data->block_io);
3855 if (tgtdev && (tgtdev->dev_type == MPI3_DEVICE_DEVFORM_PCIE)) {
3856 if (cmd_priv && tgtdev->dev_spec.pcie_inf.abort_to)
3857 timeout = tgtdev->dev_spec.pcie_inf.abort_to;
3858 else if (!cmd_priv && tgtdev->dev_spec.pcie_inf.reset_to)
3859 timeout = tgtdev->dev_spec.pcie_inf.reset_to;
3862 init_completion(&drv_cmd->done);
3863 retval = mpi3mr_admin_request_post(mrioc, &tm_req, sizeof(tm_req), 1);
3865 ioc_err(mrioc, "%s :Issue TM: Admin Post failed\n", __func__);
3868 wait_for_completion_timeout(&drv_cmd->done, (timeout * HZ));
3870 if (!(drv_cmd->state & MPI3MR_CMD_COMPLETE)) {
3871 drv_cmd->is_waiting = 0;
3873 if (!(drv_cmd->state & MPI3MR_CMD_RESET)) {
3875 "task management request timed out after %ld seconds\n",
3877 if (mrioc->logging_level & MPI3_DEBUG_TM)
3878 dprint_dump_req(&tm_req, sizeof(tm_req)/4);
3879 mpi3mr_soft_reset_handler(mrioc,
3880 MPI3MR_RESET_FROM_TM_TIMEOUT, 1);
3885 if (!(drv_cmd->state & MPI3MR_CMD_REPLY_VALID)) {
3886 dprint_tm(mrioc, "invalid task management reply message\n");
3891 tm_reply = (struct mpi3_scsi_task_mgmt_reply *)drv_cmd->reply;
3893 switch (drv_cmd->ioc_status) {
3894 case MPI3_IOCSTATUS_SUCCESS:
3895 *resp_code = le32_to_cpu(tm_reply->response_data) &
3896 MPI3MR_RI_MASK_RESPCODE;
3898 case MPI3_IOCSTATUS_SCSI_IOC_TERMINATED:
3899 *resp_code = MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE;
3903 "task management request to handle(0x%04x) is failed with ioc_status(0x%04x) log_info(0x%08x)\n",
3904 handle, drv_cmd->ioc_status, drv_cmd->ioc_loginfo);
3909 switch (*resp_code) {
3910 case MPI3_SCSITASKMGMT_RSPCODE_TM_SUCCEEDED:
3911 case MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE:
3913 case MPI3_SCSITASKMGMT_RSPCODE_IO_QUEUED_ON_IOC:
3914 if (tm_type != MPI3_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
3923 "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",
3924 tm_type, handle, drv_cmd->ioc_status, drv_cmd->ioc_loginfo,
3925 le32_to_cpu(tm_reply->termination_count),
3926 mpi3mr_tm_response_name(*resp_code), *resp_code);
3929 mpi3mr_ioc_disable_intr(mrioc);
3930 mpi3mr_poll_pend_io_completions(mrioc);
3931 mpi3mr_ioc_enable_intr(mrioc);
3932 mpi3mr_poll_pend_io_completions(mrioc);
3933 mpi3mr_process_admin_reply_q(mrioc);
3936 case MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
3937 if (!scsi_tgt_priv_data)
3939 scsi_tgt_priv_data->pend_count = 0;
3940 blk_mq_tagset_busy_iter(&mrioc->shost->tag_set,
3941 mpi3mr_count_tgt_pending,
3942 (void *)scsi_tgt_priv_data->starget);
3944 case MPI3_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
3945 if (!sdev_priv_data)
3947 sdev_priv_data->pend_count = 0;
3948 blk_mq_tagset_busy_iter(&mrioc->shost->tag_set,
3949 mpi3mr_count_dev_pending, (void *)sdev);
3954 mpi3mr_global_trigger(mrioc,
3955 MPI3_DRIVER2_GLOBALTRIGGER_TASK_MANAGEMENT_ENABLED);
3958 drv_cmd->state = MPI3MR_CMD_NOTUSED;
3959 mutex_unlock(&drv_cmd->mutex);
3960 if (scsi_tgt_priv_data)
3961 atomic_dec_if_positive(&scsi_tgt_priv_data->block_io);
3963 mpi3mr_tgtdev_put(tgtdev);
3969 * mpi3mr_bios_param - BIOS param callback
3970 * @sdev: SCSI device reference
3971 * @bdev: Block device reference
3972 * @capacity: Capacity in logical sectors
3973 * @params: Parameter array
3975 * Just the parameters with heads/secots/cylinders.
3979 static int mpi3mr_bios_param(struct scsi_device *sdev,
3980 struct block_device *bdev, sector_t capacity, int params[])
3990 dummy = heads * sectors;
3991 cylinders = capacity;
3992 sector_div(cylinders, dummy);
3994 if ((ulong)capacity >= 0x200000) {
3997 dummy = heads * sectors;
3998 cylinders = capacity;
3999 sector_div(cylinders, dummy);
4003 params[1] = sectors;
4004 params[2] = cylinders;
4009 * mpi3mr_map_queues - Map queues callback handler
4010 * @shost: SCSI host reference
4012 * Maps default and poll queues.
4014 * Return: return zero.
4016 static void mpi3mr_map_queues(struct Scsi_Host *shost)
4018 struct mpi3mr_ioc *mrioc = shost_priv(shost);
4019 int i, qoff, offset;
4020 struct blk_mq_queue_map *map = NULL;
4022 offset = mrioc->op_reply_q_offset;
4024 for (i = 0, qoff = 0; i < HCTX_MAX_TYPES; i++) {
4025 map = &shost->tag_set.map[i];
4029 if (i == HCTX_TYPE_DEFAULT)
4030 map->nr_queues = mrioc->default_qcount;
4031 else if (i == HCTX_TYPE_POLL)
4032 map->nr_queues = mrioc->active_poll_qcount;
4034 if (!map->nr_queues) {
4035 BUG_ON(i == HCTX_TYPE_DEFAULT);
4040 * The poll queue(s) doesn't have an IRQ (and hence IRQ
4041 * affinity), so use the regular blk-mq cpu mapping
4043 map->queue_offset = qoff;
4044 if (i != HCTX_TYPE_POLL)
4045 blk_mq_pci_map_queues(map, mrioc->pdev, offset);
4047 blk_mq_map_queues(map);
4049 qoff += map->nr_queues;
4050 offset += map->nr_queues;
4055 * mpi3mr_get_fw_pending_ios - Calculate pending I/O count
4056 * @mrioc: Adapter instance reference
4058 * Calculate the pending I/Os for the controller and return.
4060 * Return: Number of pending I/Os
4062 static inline int mpi3mr_get_fw_pending_ios(struct mpi3mr_ioc *mrioc)
4067 for (i = 0; i < mrioc->num_op_reply_q; i++)
4068 pend_ios += atomic_read(&mrioc->op_reply_qinfo[i].pend_ios);
4073 * mpi3mr_print_pending_host_io - print pending I/Os
4074 * @mrioc: Adapter instance reference
4076 * Print number of pending I/Os and each I/O details prior to
4077 * reset for debug purpose.
4081 static void mpi3mr_print_pending_host_io(struct mpi3mr_ioc *mrioc)
4083 struct Scsi_Host *shost = mrioc->shost;
4085 ioc_info(mrioc, "%s :Pending commands prior to reset: %d\n",
4086 __func__, mpi3mr_get_fw_pending_ios(mrioc));
4087 blk_mq_tagset_busy_iter(&shost->tag_set,
4088 mpi3mr_print_scmd, (void *)mrioc);
4092 * mpi3mr_wait_for_host_io - block for I/Os to complete
4093 * @mrioc: Adapter instance reference
4094 * @timeout: time out in seconds
4095 * Waits for pending I/Os for the given adapter to complete or
4096 * to hit the timeout.
4100 void mpi3mr_wait_for_host_io(struct mpi3mr_ioc *mrioc, u32 timeout)
4102 enum mpi3mr_iocstate iocstate;
4105 iocstate = mpi3mr_get_iocstate(mrioc);
4106 if (iocstate != MRIOC_STATE_READY)
4109 if (!mpi3mr_get_fw_pending_ios(mrioc))
4112 "%s :Waiting for %d seconds prior to reset for %d I/O\n",
4113 __func__, timeout, mpi3mr_get_fw_pending_ios(mrioc));
4115 for (i = 0; i < timeout; i++) {
4116 if (!mpi3mr_get_fw_pending_ios(mrioc))
4118 iocstate = mpi3mr_get_iocstate(mrioc);
4119 if (iocstate != MRIOC_STATE_READY)
4124 ioc_info(mrioc, "%s :Pending I/Os after wait is: %d\n", __func__,
4125 mpi3mr_get_fw_pending_ios(mrioc));
4129 * mpi3mr_setup_divert_ws - Setup Divert IO flag for write same
4130 * @mrioc: Adapter instance reference
4131 * @scmd: SCSI command reference
4132 * @scsiio_req: MPI3 SCSI IO request
4133 * @scsiio_flags: Pointer to MPI3 SCSI IO Flags
4134 * @wslen: write same max length
4136 * Gets values of unmap, ndob and number of blocks from write
4137 * same scsi io and based on these values it sets divert IO flag
4138 * and reason for diverting IO to firmware.
4142 static inline void mpi3mr_setup_divert_ws(struct mpi3mr_ioc *mrioc,
4143 struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req,
4144 u32 *scsiio_flags, u16 wslen)
4146 u8 unmap = 0, ndob = 0;
4147 u8 opcode = scmd->cmnd[0];
4149 u16 sa = (scmd->cmnd[8] << 8) | (scmd->cmnd[9]);
4151 if (opcode == WRITE_SAME_16) {
4152 unmap = scmd->cmnd[1] & 0x08;
4153 ndob = scmd->cmnd[1] & 0x01;
4154 num_blocks = get_unaligned_be32(scmd->cmnd + 10);
4155 } else if ((opcode == VARIABLE_LENGTH_CMD) && (sa == WRITE_SAME_32)) {
4156 unmap = scmd->cmnd[10] & 0x08;
4157 ndob = scmd->cmnd[10] & 0x01;
4158 num_blocks = get_unaligned_be32(scmd->cmnd + 28);
4162 if ((unmap) && (ndob) && (num_blocks > wslen)) {
4163 scsiio_req->msg_flags |=
4164 MPI3_SCSIIO_MSGFLAGS_DIVERT_TO_FIRMWARE;
4166 MPI3_SCSIIO_FLAGS_DIVERT_REASON_WRITE_SAME_TOO_LARGE;
4171 * mpi3mr_eh_host_reset - Host reset error handling callback
4172 * @scmd: SCSI command reference
4174 * Issue controller reset
4176 * Return: SUCCESS of successful reset else FAILED
4178 static int mpi3mr_eh_host_reset(struct scsi_cmnd *scmd)
4180 struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
4181 int retval = FAILED, ret;
4183 ret = mpi3mr_soft_reset_handler(mrioc,
4184 MPI3MR_RESET_FROM_EH_HOS, 1);
4190 sdev_printk(KERN_INFO, scmd->device,
4191 "Host reset is %s for scmd(%p)\n",
4192 ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4198 * mpi3mr_eh_bus_reset - Bus reset error handling callback
4199 * @scmd: SCSI command reference
4201 * Checks whether pending I/Os are present for the RAID volume;
4202 * if not there's no need to reset the adapter.
4204 * Return: SUCCESS of successful reset else FAILED
4206 static int mpi3mr_eh_bus_reset(struct scsi_cmnd *scmd)
4208 struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
4209 struct mpi3mr_stgt_priv_data *stgt_priv_data;
4210 struct mpi3mr_sdev_priv_data *sdev_priv_data;
4211 u8 dev_type = MPI3_DEVICE_DEVFORM_VD;
4212 int retval = FAILED;
4213 unsigned int timeout = MPI3MR_RESET_TIMEOUT;
4215 sdev_priv_data = scmd->device->hostdata;
4216 if (sdev_priv_data && sdev_priv_data->tgt_priv_data) {
4217 stgt_priv_data = sdev_priv_data->tgt_priv_data;
4218 dev_type = stgt_priv_data->dev_type;
4221 if (dev_type == MPI3_DEVICE_DEVFORM_VD) {
4222 mpi3mr_wait_for_host_io(mrioc,
4223 MPI3MR_RAID_ERRREC_RESET_TIMEOUT);
4224 if (!mpi3mr_get_fw_pending_ios(mrioc)) {
4225 while (mrioc->reset_in_progress ||
4226 mrioc->prepare_for_reset ||
4227 mrioc->block_on_pci_err) {
4238 if (retval == FAILED)
4239 mpi3mr_print_pending_host_io(mrioc);
4242 sdev_printk(KERN_INFO, scmd->device,
4243 "Bus reset is %s for scmd(%p)\n",
4244 ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4249 * mpi3mr_eh_target_reset - Target reset error handling callback
4250 * @scmd: SCSI command reference
4252 * Issue Target reset Task Management and verify the scmd is
4253 * terminated successfully and return status accordingly.
4255 * Return: SUCCESS of successful termination of the scmd else
4258 static int mpi3mr_eh_target_reset(struct scsi_cmnd *scmd)
4260 struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
4261 struct mpi3mr_stgt_priv_data *stgt_priv_data;
4262 struct mpi3mr_sdev_priv_data *sdev_priv_data;
4265 int retval = FAILED, ret = 0;
4267 sdev_printk(KERN_INFO, scmd->device,
4268 "Attempting Target Reset! scmd(%p)\n", scmd);
4269 scsi_print_command(scmd);
4271 sdev_priv_data = scmd->device->hostdata;
4272 if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
4273 sdev_printk(KERN_INFO, scmd->device,
4274 "SCSI device is not available\n");
4279 stgt_priv_data = sdev_priv_data->tgt_priv_data;
4280 dev_handle = stgt_priv_data->dev_handle;
4281 if (stgt_priv_data->dev_removed) {
4282 struct scmd_priv *cmd_priv = scsi_cmd_priv(scmd);
4283 sdev_printk(KERN_INFO, scmd->device,
4284 "%s:target(handle = 0x%04x) is removed, target reset is not issued\n",
4285 mrioc->name, dev_handle);
4286 if (!cmd_priv->in_lld_scope || cmd_priv->host_tag == MPI3MR_HOSTTAG_INVALID)
4292 sdev_printk(KERN_INFO, scmd->device,
4293 "Target Reset is issued to handle(0x%04x)\n",
4296 ret = mpi3mr_issue_tm(mrioc,
4297 MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET, dev_handle,
4298 sdev_priv_data->lun_id, MPI3MR_HOSTTAG_BLK_TMS,
4299 MPI3MR_RESETTM_TIMEOUT, &mrioc->host_tm_cmds, &resp_code, scmd);
4304 if (stgt_priv_data->pend_count) {
4305 sdev_printk(KERN_INFO, scmd->device,
4306 "%s: target has %d pending commands, target reset is failed\n",
4307 mrioc->name, stgt_priv_data->pend_count);
4313 sdev_printk(KERN_INFO, scmd->device,
4314 "%s: target reset is %s for scmd(%p)\n", mrioc->name,
4315 ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4321 * mpi3mr_eh_dev_reset- Device reset error handling callback
4322 * @scmd: SCSI command reference
4324 * Issue lun reset Task Management and verify the scmd is
4325 * terminated successfully and return status accordingly.
4327 * Return: SUCCESS of successful termination of the scmd else
4330 static int mpi3mr_eh_dev_reset(struct scsi_cmnd *scmd)
4332 struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
4333 struct mpi3mr_stgt_priv_data *stgt_priv_data;
4334 struct mpi3mr_sdev_priv_data *sdev_priv_data;
4337 int retval = FAILED, ret = 0;
4339 sdev_printk(KERN_INFO, scmd->device,
4340 "Attempting Device(lun) Reset! scmd(%p)\n", scmd);
4341 scsi_print_command(scmd);
4343 sdev_priv_data = scmd->device->hostdata;
4344 if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
4345 sdev_printk(KERN_INFO, scmd->device,
4346 "SCSI device is not available\n");
4351 stgt_priv_data = sdev_priv_data->tgt_priv_data;
4352 dev_handle = stgt_priv_data->dev_handle;
4353 if (stgt_priv_data->dev_removed) {
4354 struct scmd_priv *cmd_priv = scsi_cmd_priv(scmd);
4355 sdev_printk(KERN_INFO, scmd->device,
4356 "%s: device(handle = 0x%04x) is removed, device(LUN) reset is not issued\n",
4357 mrioc->name, dev_handle);
4358 if (!cmd_priv->in_lld_scope || cmd_priv->host_tag == MPI3MR_HOSTTAG_INVALID)
4364 sdev_printk(KERN_INFO, scmd->device,
4365 "Device(lun) Reset is issued to handle(0x%04x)\n", dev_handle);
4367 ret = mpi3mr_issue_tm(mrioc,
4368 MPI3_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, dev_handle,
4369 sdev_priv_data->lun_id, MPI3MR_HOSTTAG_BLK_TMS,
4370 MPI3MR_RESETTM_TIMEOUT, &mrioc->host_tm_cmds, &resp_code, scmd);
4375 if (sdev_priv_data->pend_count) {
4376 sdev_printk(KERN_INFO, scmd->device,
4377 "%s: device has %d pending commands, device(LUN) reset is failed\n",
4378 mrioc->name, sdev_priv_data->pend_count);
4383 sdev_printk(KERN_INFO, scmd->device,
4384 "%s: device(LUN) reset is %s for scmd(%p)\n", mrioc->name,
4385 ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4391 * mpi3mr_scan_start - Scan start callback handler
4392 * @shost: SCSI host reference
4394 * Issue port enable request asynchronously.
4398 static void mpi3mr_scan_start(struct Scsi_Host *shost)
4400 struct mpi3mr_ioc *mrioc = shost_priv(shost);
4402 mrioc->scan_started = 1;
4403 ioc_info(mrioc, "%s :Issuing Port Enable\n", __func__);
4404 if (mpi3mr_issue_port_enable(mrioc, 1)) {
4405 ioc_err(mrioc, "%s :Issuing port enable failed\n", __func__);
4406 mrioc->scan_started = 0;
4407 mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
4412 * mpi3mr_scan_finished - Scan finished callback handler
4413 * @shost: SCSI host reference
4414 * @time: Jiffies from the scan start
4416 * Checks whether the port enable is completed or timedout or
4417 * failed and set the scan status accordingly after taking any
4418 * recovery if required.
4420 * Return: 1 on scan finished or timed out, 0 for in progress
4422 static int mpi3mr_scan_finished(struct Scsi_Host *shost,
4425 struct mpi3mr_ioc *mrioc = shost_priv(shost);
4426 u32 pe_timeout = MPI3MR_PORTENABLE_TIMEOUT;
4427 u32 ioc_status = readl(&mrioc->sysif_regs->ioc_status);
4429 if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) ||
4430 (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)) {
4431 ioc_err(mrioc, "port enable failed due to fault or reset\n");
4432 mpi3mr_print_fault_info(mrioc);
4433 mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
4434 mrioc->scan_started = 0;
4435 mrioc->init_cmds.is_waiting = 0;
4436 mrioc->init_cmds.callback = NULL;
4437 mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
4440 if (time >= (pe_timeout * HZ)) {
4441 ioc_err(mrioc, "port enable failed due to time out\n");
4442 mpi3mr_check_rh_fault_ioc(mrioc,
4443 MPI3MR_RESET_FROM_PE_TIMEOUT);
4444 mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
4445 mrioc->scan_started = 0;
4446 mrioc->init_cmds.is_waiting = 0;
4447 mrioc->init_cmds.callback = NULL;
4448 mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
4451 if (mrioc->scan_started)
4454 if (mrioc->scan_failed) {
4456 "port enable failed with status=0x%04x\n",
4457 mrioc->scan_failed);
4459 ioc_info(mrioc, "port enable is successfully completed\n");
4461 mpi3mr_start_watchdog(mrioc);
4462 mrioc->is_driver_loading = 0;
4463 mrioc->stop_bsgs = 0;
4468 * mpi3mr_slave_destroy - Slave destroy callback handler
4469 * @sdev: SCSI device reference
4471 * Cleanup and free per device(lun) private data.
4475 static void mpi3mr_slave_destroy(struct scsi_device *sdev)
4477 struct Scsi_Host *shost;
4478 struct mpi3mr_ioc *mrioc;
4479 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4480 struct mpi3mr_tgt_dev *tgt_dev = NULL;
4481 unsigned long flags;
4482 struct scsi_target *starget;
4483 struct sas_rphy *rphy = NULL;
4485 if (!sdev->hostdata)
4488 starget = scsi_target(sdev);
4489 shost = dev_to_shost(&starget->dev);
4490 mrioc = shost_priv(shost);
4491 scsi_tgt_priv_data = starget->hostdata;
4493 scsi_tgt_priv_data->num_luns--;
4495 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4496 if (starget->channel == mrioc->scsi_device_channel)
4497 tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4498 else if (mrioc->sas_transport_enabled && !starget->channel) {
4499 rphy = dev_to_rphy(starget->dev.parent);
4500 tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
4501 rphy->identify.sas_address, rphy);
4504 if (tgt_dev && (!scsi_tgt_priv_data->num_luns))
4505 tgt_dev->starget = NULL;
4507 mpi3mr_tgtdev_put(tgt_dev);
4508 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4510 kfree(sdev->hostdata);
4511 sdev->hostdata = NULL;
4515 * mpi3mr_target_destroy - Target destroy callback handler
4516 * @starget: SCSI target reference
4518 * Cleanup and free per target private data.
4522 static void mpi3mr_target_destroy(struct scsi_target *starget)
4524 struct Scsi_Host *shost;
4525 struct mpi3mr_ioc *mrioc;
4526 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4527 struct mpi3mr_tgt_dev *tgt_dev;
4528 unsigned long flags;
4530 if (!starget->hostdata)
4533 shost = dev_to_shost(&starget->dev);
4534 mrioc = shost_priv(shost);
4535 scsi_tgt_priv_data = starget->hostdata;
4537 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4538 tgt_dev = __mpi3mr_get_tgtdev_from_tgtpriv(mrioc, scsi_tgt_priv_data);
4539 if (tgt_dev && (tgt_dev->starget == starget) &&
4540 (tgt_dev->perst_id == starget->id))
4541 tgt_dev->starget = NULL;
4543 scsi_tgt_priv_data->tgt_dev = NULL;
4544 scsi_tgt_priv_data->perst_id = 0;
4545 mpi3mr_tgtdev_put(tgt_dev);
4546 mpi3mr_tgtdev_put(tgt_dev);
4548 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4550 kfree(starget->hostdata);
4551 starget->hostdata = NULL;
4555 * mpi3mr_device_configure - Slave configure callback handler
4556 * @sdev: SCSI device reference
4557 * @lim: queue limits
4559 * Configure queue depth, max hardware sectors and virt boundary
4564 static int mpi3mr_device_configure(struct scsi_device *sdev,
4565 struct queue_limits *lim)
4567 struct scsi_target *starget;
4568 struct Scsi_Host *shost;
4569 struct mpi3mr_ioc *mrioc;
4570 struct mpi3mr_tgt_dev *tgt_dev = NULL;
4571 unsigned long flags;
4573 struct sas_rphy *rphy = NULL;
4575 starget = scsi_target(sdev);
4576 shost = dev_to_shost(&starget->dev);
4577 mrioc = shost_priv(shost);
4579 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4580 if (starget->channel == mrioc->scsi_device_channel)
4581 tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4582 else if (mrioc->sas_transport_enabled && !starget->channel) {
4583 rphy = dev_to_rphy(starget->dev.parent);
4584 tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
4585 rphy->identify.sas_address, rphy);
4587 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4591 mpi3mr_change_queue_depth(sdev, tgt_dev->q_depth);
4593 sdev->eh_timeout = MPI3MR_EH_SCMD_TIMEOUT;
4594 blk_queue_rq_timeout(sdev->request_queue, MPI3MR_SCMD_TIMEOUT);
4596 mpi3mr_configure_tgt_dev(tgt_dev, lim);
4597 mpi3mr_tgtdev_put(tgt_dev);
4602 * mpi3mr_slave_alloc -Slave alloc callback handler
4603 * @sdev: SCSI device reference
4605 * Allocate per device(lun) private data and initialize it.
4607 * Return: 0 on success -ENOMEM on memory allocation failure.
4609 static int mpi3mr_slave_alloc(struct scsi_device *sdev)
4611 struct Scsi_Host *shost;
4612 struct mpi3mr_ioc *mrioc;
4613 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4614 struct mpi3mr_tgt_dev *tgt_dev = NULL;
4615 struct mpi3mr_sdev_priv_data *scsi_dev_priv_data;
4616 unsigned long flags;
4617 struct scsi_target *starget;
4619 struct sas_rphy *rphy = NULL;
4621 starget = scsi_target(sdev);
4622 shost = dev_to_shost(&starget->dev);
4623 mrioc = shost_priv(shost);
4624 scsi_tgt_priv_data = starget->hostdata;
4626 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4628 if (starget->channel == mrioc->scsi_device_channel)
4629 tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4630 else if (mrioc->sas_transport_enabled && !starget->channel) {
4631 rphy = dev_to_rphy(starget->dev.parent);
4632 tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
4633 rphy->identify.sas_address, rphy);
4637 if (tgt_dev->starget == NULL)
4638 tgt_dev->starget = starget;
4639 mpi3mr_tgtdev_put(tgt_dev);
4642 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4646 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4648 scsi_dev_priv_data = kzalloc(sizeof(*scsi_dev_priv_data), GFP_KERNEL);
4649 if (!scsi_dev_priv_data)
4652 scsi_dev_priv_data->lun_id = sdev->lun;
4653 scsi_dev_priv_data->tgt_priv_data = scsi_tgt_priv_data;
4654 sdev->hostdata = scsi_dev_priv_data;
4656 scsi_tgt_priv_data->num_luns++;
4662 * mpi3mr_target_alloc - Target alloc callback handler
4663 * @starget: SCSI target reference
4665 * Allocate per target private data and initialize it.
4667 * Return: 0 on success -ENOMEM on memory allocation failure.
4669 static int mpi3mr_target_alloc(struct scsi_target *starget)
4671 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4672 struct mpi3mr_ioc *mrioc = shost_priv(shost);
4673 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4674 struct mpi3mr_tgt_dev *tgt_dev;
4675 unsigned long flags;
4677 struct sas_rphy *rphy = NULL;
4679 scsi_tgt_priv_data = kzalloc(sizeof(*scsi_tgt_priv_data), GFP_KERNEL);
4680 if (!scsi_tgt_priv_data)
4683 starget->hostdata = scsi_tgt_priv_data;
4685 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4686 if (starget->channel == mrioc->scsi_device_channel) {
4687 tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4688 if (tgt_dev && !tgt_dev->is_hidden) {
4689 scsi_tgt_priv_data->starget = starget;
4690 scsi_tgt_priv_data->dev_handle = tgt_dev->dev_handle;
4691 scsi_tgt_priv_data->perst_id = tgt_dev->perst_id;
4692 scsi_tgt_priv_data->dev_type = tgt_dev->dev_type;
4693 scsi_tgt_priv_data->tgt_dev = tgt_dev;
4694 tgt_dev->starget = starget;
4695 atomic_set(&scsi_tgt_priv_data->block_io, 0);
4697 if ((tgt_dev->dev_type == MPI3_DEVICE_DEVFORM_PCIE) &&
4698 ((tgt_dev->dev_spec.pcie_inf.dev_info &
4699 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) ==
4700 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) &&
4701 ((tgt_dev->dev_spec.pcie_inf.dev_info &
4702 MPI3_DEVICE0_PCIE_DEVICE_INFO_PITYPE_MASK) !=
4703 MPI3_DEVICE0_PCIE_DEVICE_INFO_PITYPE_0))
4704 scsi_tgt_priv_data->dev_nvme_dif = 1;
4705 scsi_tgt_priv_data->io_throttle_enabled = tgt_dev->io_throttle_enabled;
4706 scsi_tgt_priv_data->wslen = tgt_dev->wslen;
4707 if (tgt_dev->dev_type == MPI3_DEVICE_DEVFORM_VD)
4708 scsi_tgt_priv_data->throttle_group = tgt_dev->dev_spec.vd_inf.tg;
4711 } else if (mrioc->sas_transport_enabled && !starget->channel) {
4712 rphy = dev_to_rphy(starget->dev.parent);
4713 tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
4714 rphy->identify.sas_address, rphy);
4715 if (tgt_dev && !tgt_dev->is_hidden && !tgt_dev->non_stl &&
4716 (tgt_dev->dev_type == MPI3_DEVICE_DEVFORM_SAS_SATA)) {
4717 scsi_tgt_priv_data->starget = starget;
4718 scsi_tgt_priv_data->dev_handle = tgt_dev->dev_handle;
4719 scsi_tgt_priv_data->perst_id = tgt_dev->perst_id;
4720 scsi_tgt_priv_data->dev_type = tgt_dev->dev_type;
4721 scsi_tgt_priv_data->tgt_dev = tgt_dev;
4722 scsi_tgt_priv_data->io_throttle_enabled = tgt_dev->io_throttle_enabled;
4723 scsi_tgt_priv_data->wslen = tgt_dev->wslen;
4724 tgt_dev->starget = starget;
4725 atomic_set(&scsi_tgt_priv_data->block_io, 0);
4730 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4736 * mpi3mr_check_return_unmap - Whether an unmap is allowed
4737 * @mrioc: Adapter instance reference
4738 * @scmd: SCSI Command reference
4740 * The controller hardware cannot handle certain unmap commands
4741 * for NVMe drives, this routine checks those and return true
4742 * and completes the SCSI command with proper status and sense
4745 * Return: TRUE for not allowed unmap, FALSE otherwise.
4747 static bool mpi3mr_check_return_unmap(struct mpi3mr_ioc *mrioc,
4748 struct scsi_cmnd *scmd)
4751 u16 param_len, desc_len, trunc_param_len;
4753 trunc_param_len = param_len = get_unaligned_be16(scmd->cmnd + 7);
4755 if (mrioc->pdev->revision) {
4756 if ((param_len > 24) && ((param_len - 8) & 0xF)) {
4757 trunc_param_len -= (param_len - 8) & 0xF;
4758 dprint_scsi_command(mrioc, scmd, MPI3_DEBUG_SCSI_ERROR);
4759 dprint_scsi_err(mrioc,
4760 "truncating param_len from (%d) to (%d)\n",
4761 param_len, trunc_param_len);
4762 put_unaligned_be16(trunc_param_len, scmd->cmnd + 7);
4763 dprint_scsi_command(mrioc, scmd, MPI3_DEBUG_SCSI_ERROR);
4770 "%s: cdb received with zero parameter length\n",
4772 scsi_print_command(scmd);
4773 scmd->result = DID_OK << 16;
4778 if (param_len < 24) {
4780 "%s: cdb received with invalid param_len: %d\n",
4781 __func__, param_len);
4782 scsi_print_command(scmd);
4783 scmd->result = SAM_STAT_CHECK_CONDITION;
4784 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
4789 if (param_len != scsi_bufflen(scmd)) {
4791 "%s: cdb received with param_len: %d bufflen: %d\n",
4792 __func__, param_len, scsi_bufflen(scmd));
4793 scsi_print_command(scmd);
4794 scmd->result = SAM_STAT_CHECK_CONDITION;
4795 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
4800 buf = kzalloc(scsi_bufflen(scmd), GFP_ATOMIC);
4802 scsi_print_command(scmd);
4803 scmd->result = SAM_STAT_CHECK_CONDITION;
4804 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
4809 scsi_sg_copy_to_buffer(scmd, buf, scsi_bufflen(scmd));
4810 desc_len = get_unaligned_be16(&buf[2]);
4812 if (desc_len < 16) {
4814 "%s: Invalid descriptor length in param list: %d\n",
4815 __func__, desc_len);
4816 scsi_print_command(scmd);
4817 scmd->result = SAM_STAT_CHECK_CONDITION;
4818 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
4825 if (param_len > (desc_len + 8)) {
4826 trunc_param_len = desc_len + 8;
4827 scsi_print_command(scmd);
4828 dprint_scsi_err(mrioc,
4829 "truncating param_len(%d) to desc_len+8(%d)\n",
4830 param_len, trunc_param_len);
4831 put_unaligned_be16(trunc_param_len, scmd->cmnd + 7);
4832 scsi_print_command(scmd);
4840 * mpi3mr_allow_scmd_to_fw - Command is allowed during shutdown
4841 * @scmd: SCSI Command reference
4843 * Checks whether a cdb is allowed during shutdown or not.
4845 * Return: TRUE for allowed commands, FALSE otherwise.
4848 inline bool mpi3mr_allow_scmd_to_fw(struct scsi_cmnd *scmd)
4850 switch (scmd->cmnd[0]) {
4851 case SYNCHRONIZE_CACHE:
4860 * mpi3mr_qcmd - I/O request despatcher
4861 * @shost: SCSI Host reference
4862 * @scmd: SCSI Command reference
4864 * Issues the SCSI Command as an MPI3 request.
4866 * Return: 0 on successful queueing of the request or if the
4867 * request is completed with failure.
4868 * SCSI_MLQUEUE_DEVICE_BUSY when the device is busy.
4869 * SCSI_MLQUEUE_HOST_BUSY when the host queue is full.
4871 static int mpi3mr_qcmd(struct Scsi_Host *shost,
4872 struct scsi_cmnd *scmd)
4874 struct mpi3mr_ioc *mrioc = shost_priv(shost);
4875 struct mpi3mr_stgt_priv_data *stgt_priv_data;
4876 struct mpi3mr_sdev_priv_data *sdev_priv_data;
4877 struct scmd_priv *scmd_priv_data = NULL;
4878 struct mpi3_scsi_io_request *scsiio_req = NULL;
4879 struct op_req_qinfo *op_req_q = NULL;
4883 u32 scsiio_flags = 0, data_len_blks = 0;
4884 struct request *rq = scsi_cmd_to_rq(scmd);
4887 u32 tracked_io_sz = 0;
4888 u32 ioc_pend_data_len = 0, tg_pend_data_len = 0;
4889 struct mpi3mr_throttle_group_info *tg = NULL;
4891 if (mrioc->unrecoverable) {
4892 scmd->result = DID_ERROR << 16;
4897 sdev_priv_data = scmd->device->hostdata;
4898 if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
4899 scmd->result = DID_NO_CONNECT << 16;
4904 if (mrioc->stop_drv_processing &&
4905 !(mpi3mr_allow_scmd_to_fw(scmd))) {
4906 scmd->result = DID_NO_CONNECT << 16;
4911 stgt_priv_data = sdev_priv_data->tgt_priv_data;
4912 dev_handle = stgt_priv_data->dev_handle;
4914 /* Avoid error handling escalation when device is removed or blocked */
4916 if (scmd->device->host->shost_state == SHOST_RECOVERY &&
4917 scmd->cmnd[0] == TEST_UNIT_READY &&
4918 (stgt_priv_data->dev_removed || (dev_handle == MPI3MR_INVALID_DEV_HANDLE))) {
4919 scsi_build_sense(scmd, 0, UNIT_ATTENTION, 0x29, 0x07);
4924 if (mrioc->reset_in_progress || mrioc->prepare_for_reset
4925 || mrioc->block_on_pci_err) {
4926 retval = SCSI_MLQUEUE_HOST_BUSY;
4930 if (atomic_read(&stgt_priv_data->block_io)) {
4931 if (mrioc->stop_drv_processing) {
4932 scmd->result = DID_NO_CONNECT << 16;
4936 retval = SCSI_MLQUEUE_DEVICE_BUSY;
4940 if (dev_handle == MPI3MR_INVALID_DEV_HANDLE) {
4941 scmd->result = DID_NO_CONNECT << 16;
4945 if (stgt_priv_data->dev_removed) {
4946 scmd->result = DID_NO_CONNECT << 16;
4951 if (stgt_priv_data->dev_type == MPI3_DEVICE_DEVFORM_PCIE)
4953 if ((scmd->cmnd[0] == UNMAP) && is_pcie_dev &&
4954 (mrioc->pdev->device == MPI3_MFGPAGE_DEVID_SAS4116) &&
4955 mpi3mr_check_return_unmap(mrioc, scmd))
4958 host_tag = mpi3mr_host_tag_for_scmd(mrioc, scmd);
4959 if (host_tag == MPI3MR_HOSTTAG_INVALID) {
4960 scmd->result = DID_ERROR << 16;
4965 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
4966 scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_READ;
4967 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
4968 scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_WRITE;
4970 scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_NO_DATA_TRANSFER;
4972 scsiio_flags |= MPI3_SCSIIO_FLAGS_TASKATTRIBUTE_SIMPLEQ;
4974 if (sdev_priv_data->ncq_prio_enable) {
4975 iprio_class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
4976 if (iprio_class == IOPRIO_CLASS_RT)
4977 scsiio_flags |= 1 << MPI3_SCSIIO_FLAGS_CMDPRI_SHIFT;
4980 if (scmd->cmd_len > 16)
4981 scsiio_flags |= MPI3_SCSIIO_FLAGS_CDB_GREATER_THAN_16;
4983 scmd_priv_data = scsi_cmd_priv(scmd);
4984 memset(scmd_priv_data->mpi3mr_scsiio_req, 0, MPI3MR_ADMIN_REQ_FRAME_SZ);
4985 scsiio_req = (struct mpi3_scsi_io_request *)scmd_priv_data->mpi3mr_scsiio_req;
4986 scsiio_req->function = MPI3_FUNCTION_SCSI_IO;
4987 scsiio_req->host_tag = cpu_to_le16(host_tag);
4989 mpi3mr_setup_eedp(mrioc, scmd, scsiio_req);
4991 if (stgt_priv_data->wslen)
4992 mpi3mr_setup_divert_ws(mrioc, scmd, scsiio_req, &scsiio_flags,
4993 stgt_priv_data->wslen);
4995 memcpy(scsiio_req->cdb.cdb32, scmd->cmnd, scmd->cmd_len);
4996 scsiio_req->data_length = cpu_to_le32(scsi_bufflen(scmd));
4997 scsiio_req->dev_handle = cpu_to_le16(dev_handle);
4998 scsiio_req->flags = cpu_to_le32(scsiio_flags);
4999 int_to_scsilun(sdev_priv_data->lun_id,
5000 (struct scsi_lun *)scsiio_req->lun);
5002 if (mpi3mr_build_sg_scmd(mrioc, scmd, scsiio_req)) {
5003 mpi3mr_clear_scmd_priv(mrioc, scmd);
5004 retval = SCSI_MLQUEUE_HOST_BUSY;
5007 op_req_q = &mrioc->req_qinfo[scmd_priv_data->req_q_idx];
5008 data_len_blks = scsi_bufflen(scmd) >> 9;
5009 if ((data_len_blks >= mrioc->io_throttle_data_length) &&
5010 stgt_priv_data->io_throttle_enabled) {
5011 tracked_io_sz = data_len_blks;
5012 tg = stgt_priv_data->throttle_group;
5014 ioc_pend_data_len = atomic_add_return(data_len_blks,
5015 &mrioc->pend_large_data_sz);
5016 tg_pend_data_len = atomic_add_return(data_len_blks,
5017 &tg->pend_large_data_sz);
5018 if (!tg->io_divert && ((ioc_pend_data_len >=
5019 mrioc->io_throttle_high) ||
5020 (tg_pend_data_len >= tg->high))) {
5022 tg->need_qd_reduction = 1;
5023 mpi3mr_set_io_divert_for_all_vd_in_tg(mrioc,
5025 mpi3mr_queue_qd_reduction_event(mrioc, tg);
5028 ioc_pend_data_len = atomic_add_return(data_len_blks,
5029 &mrioc->pend_large_data_sz);
5030 if (ioc_pend_data_len >= mrioc->io_throttle_high)
5031 stgt_priv_data->io_divert = 1;
5035 if (stgt_priv_data->io_divert) {
5036 scsiio_req->msg_flags |=
5037 MPI3_SCSIIO_MSGFLAGS_DIVERT_TO_FIRMWARE;
5038 scsiio_flags |= MPI3_SCSIIO_FLAGS_DIVERT_REASON_IO_THROTTLING;
5040 scsiio_req->flags |= cpu_to_le32(scsiio_flags);
5042 if (mpi3mr_op_request_post(mrioc, op_req_q,
5043 scmd_priv_data->mpi3mr_scsiio_req)) {
5044 mpi3mr_clear_scmd_priv(mrioc, scmd);
5045 retval = SCSI_MLQUEUE_HOST_BUSY;
5046 if (tracked_io_sz) {
5047 atomic_sub(tracked_io_sz, &mrioc->pend_large_data_sz);
5049 atomic_sub(tracked_io_sz,
5050 &tg->pend_large_data_sz);
5059 static const struct scsi_host_template mpi3mr_driver_template = {
5060 .module = THIS_MODULE,
5061 .name = "MPI3 Storage Controller",
5062 .proc_name = MPI3MR_DRIVER_NAME,
5063 .queuecommand = mpi3mr_qcmd,
5064 .target_alloc = mpi3mr_target_alloc,
5065 .slave_alloc = mpi3mr_slave_alloc,
5066 .device_configure = mpi3mr_device_configure,
5067 .target_destroy = mpi3mr_target_destroy,
5068 .slave_destroy = mpi3mr_slave_destroy,
5069 .scan_finished = mpi3mr_scan_finished,
5070 .scan_start = mpi3mr_scan_start,
5071 .change_queue_depth = mpi3mr_change_queue_depth,
5072 .eh_device_reset_handler = mpi3mr_eh_dev_reset,
5073 .eh_target_reset_handler = mpi3mr_eh_target_reset,
5074 .eh_bus_reset_handler = mpi3mr_eh_bus_reset,
5075 .eh_host_reset_handler = mpi3mr_eh_host_reset,
5076 .bios_param = mpi3mr_bios_param,
5077 .map_queues = mpi3mr_map_queues,
5078 .mq_poll = mpi3mr_blk_mq_poll,
5082 .sg_tablesize = MPI3MR_DEFAULT_SGL_ENTRIES,
5083 /* max xfer supported is 1M (2K in 512 byte sized sectors)
5085 .max_sectors = (MPI3MR_DEFAULT_MAX_IO_SIZE / 512),
5086 .cmd_per_lun = MPI3MR_MAX_CMDS_LUN,
5087 .max_segment_size = 0xffffffff,
5088 .track_queue_depth = 1,
5089 .cmd_size = sizeof(struct scmd_priv),
5090 .shost_groups = mpi3mr_host_groups,
5091 .sdev_groups = mpi3mr_dev_groups,
5095 * mpi3mr_init_drv_cmd - Initialize internal command tracker
5096 * @cmdptr: Internal command tracker
5097 * @host_tag: Host tag used for the specific command
5099 * Initialize the internal command tracker structure with
5100 * specified host tag.
5104 static inline void mpi3mr_init_drv_cmd(struct mpi3mr_drv_cmd *cmdptr,
5107 mutex_init(&cmdptr->mutex);
5108 cmdptr->reply = NULL;
5109 cmdptr->state = MPI3MR_CMD_NOTUSED;
5110 cmdptr->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
5111 cmdptr->host_tag = host_tag;
5115 * osintfc_mrioc_security_status -Check controller secure status
5116 * @pdev: PCI device instance
5118 * Read the Device Serial Number capability from PCI config
5119 * space and decide whether the controller is secure or not.
5121 * Return: 0 on success, non-zero on failure.
5124 osintfc_mrioc_security_status(struct pci_dev *pdev)
5132 base = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DSN);
5135 "%s: PCI_EXT_CAP_ID_DSN is not supported\n", __func__);
5139 pci_read_config_dword(pdev, base + 4, &cap_data);
5141 debug_status = cap_data & MPI3MR_CTLR_SECURE_DBG_STATUS_MASK;
5142 ctlr_status = cap_data & MPI3MR_CTLR_SECURITY_STATUS_MASK;
5144 switch (ctlr_status) {
5145 case MPI3MR_INVALID_DEVICE:
5147 "%s: Non secure ctlr (Invalid) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
5148 __func__, pdev->device, pdev->subsystem_vendor,
5149 pdev->subsystem_device);
5152 case MPI3MR_CONFIG_SECURE_DEVICE:
5154 dev_info(&pdev->dev,
5155 "%s: Config secure ctlr is detected\n",
5158 case MPI3MR_HARD_SECURE_DEVICE:
5160 case MPI3MR_TAMPERED_DEVICE:
5162 "%s: Non secure ctlr (Tampered) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
5163 __func__, pdev->device, pdev->subsystem_vendor,
5164 pdev->subsystem_device);
5172 if (!retval && debug_status) {
5174 "%s: Non secure ctlr (Secure Dbg) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
5175 __func__, pdev->device, pdev->subsystem_vendor,
5176 pdev->subsystem_device);
5184 * mpi3mr_probe - PCI probe callback
5185 * @pdev: PCI device instance
5186 * @id: PCI device ID details
5188 * controller initialization routine. Checks the security status
5189 * of the controller and if it is invalid or tampered return the
5190 * probe without initializing the controller. Otherwise,
5191 * allocate per adapter instance through shost_priv and
5192 * initialize controller specific data structures, initializae
5193 * the controller hardware, add shost to the SCSI subsystem.
5195 * Return: 0 on success, non-zero on failure.
5199 mpi3mr_probe(struct pci_dev *pdev, const struct pci_device_id *id)
5201 struct mpi3mr_ioc *mrioc = NULL;
5202 struct Scsi_Host *shost = NULL;
5205 if (osintfc_mrioc_security_status(pdev)) {
5206 warn_non_secure_ctlr = 1;
5207 return 1; /* For Invalid and Tampered device */
5210 shost = scsi_host_alloc(&mpi3mr_driver_template,
5211 sizeof(struct mpi3mr_ioc));
5217 mrioc = shost_priv(shost);
5218 retval = ida_alloc_range(&mrioc_ida, 1, U8_MAX, GFP_KERNEL);
5220 goto id_alloc_failed;
5221 mrioc->id = (u8)retval;
5222 sprintf(mrioc->driver_name, "%s", MPI3MR_DRIVER_NAME);
5223 sprintf(mrioc->name, "%s%d", mrioc->driver_name, mrioc->id);
5224 INIT_LIST_HEAD(&mrioc->list);
5225 spin_lock(&mrioc_list_lock);
5226 list_add_tail(&mrioc->list, &mrioc_list);
5227 spin_unlock(&mrioc_list_lock);
5229 spin_lock_init(&mrioc->admin_req_lock);
5230 spin_lock_init(&mrioc->reply_free_queue_lock);
5231 spin_lock_init(&mrioc->sbq_lock);
5232 spin_lock_init(&mrioc->fwevt_lock);
5233 spin_lock_init(&mrioc->tgtdev_lock);
5234 spin_lock_init(&mrioc->watchdog_lock);
5235 spin_lock_init(&mrioc->chain_buf_lock);
5236 spin_lock_init(&mrioc->sas_node_lock);
5238 INIT_LIST_HEAD(&mrioc->fwevt_list);
5239 INIT_LIST_HEAD(&mrioc->tgtdev_list);
5240 INIT_LIST_HEAD(&mrioc->delayed_rmhs_list);
5241 INIT_LIST_HEAD(&mrioc->delayed_evtack_cmds_list);
5242 INIT_LIST_HEAD(&mrioc->sas_expander_list);
5243 INIT_LIST_HEAD(&mrioc->hba_port_table_list);
5244 INIT_LIST_HEAD(&mrioc->enclosure_list);
5246 mutex_init(&mrioc->reset_mutex);
5247 mpi3mr_init_drv_cmd(&mrioc->init_cmds, MPI3MR_HOSTTAG_INITCMDS);
5248 mpi3mr_init_drv_cmd(&mrioc->host_tm_cmds, MPI3MR_HOSTTAG_BLK_TMS);
5249 mpi3mr_init_drv_cmd(&mrioc->bsg_cmds, MPI3MR_HOSTTAG_BSG_CMDS);
5250 mpi3mr_init_drv_cmd(&mrioc->cfg_cmds, MPI3MR_HOSTTAG_CFG_CMDS);
5251 mpi3mr_init_drv_cmd(&mrioc->transport_cmds,
5252 MPI3MR_HOSTTAG_TRANSPORT_CMDS);
5254 for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++)
5255 mpi3mr_init_drv_cmd(&mrioc->dev_rmhs_cmds[i],
5256 MPI3MR_HOSTTAG_DEVRMCMD_MIN + i);
5258 for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++)
5259 mpi3mr_init_drv_cmd(&mrioc->evtack_cmds[i],
5260 MPI3MR_HOSTTAG_EVTACKCMD_MIN + i);
5262 if ((pdev->device == MPI3_MFGPAGE_DEVID_SAS4116) &&
5264 mrioc->enable_segqueue = false;
5266 mrioc->enable_segqueue = true;
5268 init_waitqueue_head(&mrioc->reset_waitq);
5269 mrioc->logging_level = logging_level;
5270 mrioc->shost = shost;
5272 mrioc->stop_bsgs = 1;
5274 mrioc->max_sgl_entries = max_sgl_entries;
5275 if (max_sgl_entries > MPI3MR_MAX_SGL_ENTRIES)
5276 mrioc->max_sgl_entries = MPI3MR_MAX_SGL_ENTRIES;
5277 else if (max_sgl_entries < MPI3MR_DEFAULT_SGL_ENTRIES)
5278 mrioc->max_sgl_entries = MPI3MR_DEFAULT_SGL_ENTRIES;
5280 mrioc->max_sgl_entries /= MPI3MR_DEFAULT_SGL_ENTRIES;
5281 mrioc->max_sgl_entries *= MPI3MR_DEFAULT_SGL_ENTRIES;
5284 /* init shost parameters */
5285 shost->max_cmd_len = MPI3MR_MAX_CDB_LENGTH;
5286 shost->max_lun = -1;
5287 shost->unique_id = mrioc->id;
5289 shost->max_channel = 0;
5290 shost->max_id = 0xFFFFFFFF;
5292 shost->host_tagset = 1;
5295 scsi_host_set_prot(shost, prot_mask);
5297 prot_mask = SHOST_DIF_TYPE1_PROTECTION
5298 | SHOST_DIF_TYPE2_PROTECTION
5299 | SHOST_DIF_TYPE3_PROTECTION;
5300 scsi_host_set_prot(shost, prot_mask);
5304 "%s :host protection capabilities enabled %s%s%s%s%s%s%s\n",
5306 (prot_mask & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "",
5307 (prot_mask & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "",
5308 (prot_mask & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "",
5309 (prot_mask & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "",
5310 (prot_mask & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "",
5311 (prot_mask & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "",
5312 (prot_mask & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : "");
5314 if (prot_guard_mask)
5315 scsi_host_set_guard(shost, (prot_guard_mask & 3));
5317 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
5319 snprintf(mrioc->fwevt_worker_name, sizeof(mrioc->fwevt_worker_name),
5320 "%s%d_fwevt_wrkr", mrioc->driver_name, mrioc->id);
5321 mrioc->fwevt_worker_thread = alloc_ordered_workqueue(
5322 mrioc->fwevt_worker_name, 0);
5323 if (!mrioc->fwevt_worker_thread) {
5324 ioc_err(mrioc, "failure at %s:%d/%s()!\n",
5325 __FILE__, __LINE__, __func__);
5327 goto fwevtthread_failed;
5330 mrioc->is_driver_loading = 1;
5331 mrioc->cpu_count = num_online_cpus();
5332 if (mpi3mr_setup_resources(mrioc)) {
5333 ioc_err(mrioc, "setup resources failed\n");
5335 goto resource_alloc_failed;
5337 if (mpi3mr_init_ioc(mrioc)) {
5338 ioc_err(mrioc, "initializing IOC failed\n");
5340 goto init_ioc_failed;
5343 shost->nr_hw_queues = mrioc->num_op_reply_q;
5344 if (mrioc->active_poll_qcount)
5347 shost->can_queue = mrioc->max_host_ios;
5348 shost->sg_tablesize = mrioc->max_sgl_entries;
5349 shost->max_id = mrioc->facts.max_perids + 1;
5351 retval = scsi_add_host(shost, &pdev->dev);
5353 ioc_err(mrioc, "failure at %s:%d/%s()!\n",
5354 __FILE__, __LINE__, __func__);
5355 goto addhost_failed;
5358 scsi_scan_host(shost);
5359 mpi3mr_bsg_init(mrioc);
5363 mpi3mr_stop_watchdog(mrioc);
5364 mpi3mr_cleanup_ioc(mrioc);
5366 mpi3mr_free_mem(mrioc);
5367 mpi3mr_cleanup_resources(mrioc);
5368 resource_alloc_failed:
5369 destroy_workqueue(mrioc->fwevt_worker_thread);
5371 ida_free(&mrioc_ida, mrioc->id);
5372 spin_lock(&mrioc_list_lock);
5373 list_del(&mrioc->list);
5374 spin_unlock(&mrioc_list_lock);
5376 scsi_host_put(shost);
5382 * mpi3mr_remove - PCI remove callback
5383 * @pdev: PCI device instance
5385 * Cleanup the IOC by issuing MUR and shutdown notification.
5386 * Free up all memory and resources associated with the
5387 * controllerand target devices, unregister the shost.
5391 static void mpi3mr_remove(struct pci_dev *pdev)
5393 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5394 struct mpi3mr_ioc *mrioc;
5395 struct workqueue_struct *wq;
5396 unsigned long flags;
5397 struct mpi3mr_tgt_dev *tgtdev, *tgtdev_next;
5398 struct mpi3mr_hba_port *port, *hba_port_next;
5399 struct mpi3mr_sas_node *sas_expander, *sas_expander_next;
5404 mrioc = shost_priv(shost);
5405 while (mrioc->reset_in_progress || mrioc->is_driver_loading)
5408 if (mrioc->block_on_pci_err) {
5409 mrioc->block_on_pci_err = false;
5410 scsi_unblock_requests(shost);
5411 mrioc->unrecoverable = 1;
5414 if (!pci_device_is_present(mrioc->pdev) ||
5415 mrioc->pci_err_recovery) {
5416 mrioc->unrecoverable = 1;
5417 mpi3mr_flush_cmds_for_unrecovered_controller(mrioc);
5420 mpi3mr_bsg_exit(mrioc);
5421 mrioc->stop_drv_processing = 1;
5422 mpi3mr_cleanup_fwevt_list(mrioc);
5423 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
5424 wq = mrioc->fwevt_worker_thread;
5425 mrioc->fwevt_worker_thread = NULL;
5426 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
5428 destroy_workqueue(wq);
5430 if (mrioc->sas_transport_enabled)
5431 sas_remove_host(shost);
5433 scsi_remove_host(shost);
5435 list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
5437 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
5438 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, true);
5439 mpi3mr_tgtdev_put(tgtdev);
5441 mpi3mr_stop_watchdog(mrioc);
5442 mpi3mr_cleanup_ioc(mrioc);
5443 mpi3mr_free_mem(mrioc);
5444 mpi3mr_cleanup_resources(mrioc);
5446 spin_lock_irqsave(&mrioc->sas_node_lock, flags);
5447 list_for_each_entry_safe_reverse(sas_expander, sas_expander_next,
5448 &mrioc->sas_expander_list, list) {
5449 spin_unlock_irqrestore(&mrioc->sas_node_lock, flags);
5450 mpi3mr_expander_node_remove(mrioc, sas_expander);
5451 spin_lock_irqsave(&mrioc->sas_node_lock, flags);
5453 list_for_each_entry_safe(port, hba_port_next, &mrioc->hba_port_table_list, list) {
5455 "removing hba_port entry: %p port: %d from hba_port list\n",
5456 port, port->port_id);
5457 list_del(&port->list);
5460 spin_unlock_irqrestore(&mrioc->sas_node_lock, flags);
5462 if (mrioc->sas_hba.num_phys) {
5463 kfree(mrioc->sas_hba.phy);
5464 mrioc->sas_hba.phy = NULL;
5465 mrioc->sas_hba.num_phys = 0;
5468 ida_free(&mrioc_ida, mrioc->id);
5469 spin_lock(&mrioc_list_lock);
5470 list_del(&mrioc->list);
5471 spin_unlock(&mrioc_list_lock);
5473 scsi_host_put(shost);
5477 * mpi3mr_shutdown - PCI shutdown callback
5478 * @pdev: PCI device instance
5480 * Free up all memory and resources associated with the
5485 static void mpi3mr_shutdown(struct pci_dev *pdev)
5487 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5488 struct mpi3mr_ioc *mrioc;
5489 struct workqueue_struct *wq;
5490 unsigned long flags;
5495 mrioc = shost_priv(shost);
5496 while (mrioc->reset_in_progress || mrioc->is_driver_loading)
5499 mrioc->stop_drv_processing = 1;
5500 mpi3mr_cleanup_fwevt_list(mrioc);
5501 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
5502 wq = mrioc->fwevt_worker_thread;
5503 mrioc->fwevt_worker_thread = NULL;
5504 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
5506 destroy_workqueue(wq);
5508 mpi3mr_stop_watchdog(mrioc);
5509 mpi3mr_cleanup_ioc(mrioc);
5510 mpi3mr_cleanup_resources(mrioc);
5514 * mpi3mr_suspend - PCI power management suspend callback
5515 * @dev: Device struct
5517 * Change the power state to the given value and cleanup the IOC
5518 * by issuing MUR and shutdown notification
5522 static int __maybe_unused
5523 mpi3mr_suspend(struct device *dev)
5525 struct pci_dev *pdev = to_pci_dev(dev);
5526 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5527 struct mpi3mr_ioc *mrioc;
5532 mrioc = shost_priv(shost);
5533 while (mrioc->reset_in_progress || mrioc->is_driver_loading)
5535 mrioc->stop_drv_processing = 1;
5536 mpi3mr_cleanup_fwevt_list(mrioc);
5537 scsi_block_requests(shost);
5538 mpi3mr_stop_watchdog(mrioc);
5539 mpi3mr_cleanup_ioc(mrioc);
5541 ioc_info(mrioc, "pdev=0x%p, slot=%s, entering operating state\n",
5542 pdev, pci_name(pdev));
5543 mpi3mr_cleanup_resources(mrioc);
5549 * mpi3mr_resume - PCI power management resume callback
5550 * @dev: Device struct
5552 * Restore the power state to D0 and reinitialize the controller
5553 * and resume I/O operations to the target devices
5555 * Return: 0 on success, non-zero on failure
5557 static int __maybe_unused
5558 mpi3mr_resume(struct device *dev)
5560 struct pci_dev *pdev = to_pci_dev(dev);
5561 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5562 struct mpi3mr_ioc *mrioc;
5563 pci_power_t device_state = pdev->current_state;
5569 mrioc = shost_priv(shost);
5571 ioc_info(mrioc, "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
5572 pdev, pci_name(pdev), device_state);
5574 mrioc->cpu_count = num_online_cpus();
5575 r = mpi3mr_setup_resources(mrioc);
5577 ioc_info(mrioc, "%s: Setup resources failed[%d]\n",
5582 mrioc->stop_drv_processing = 0;
5583 mpi3mr_invalidate_devhandles(mrioc);
5584 mpi3mr_free_enclosure_list(mrioc);
5585 mpi3mr_memset_buffers(mrioc);
5586 r = mpi3mr_reinit_ioc(mrioc, 1);
5588 ioc_err(mrioc, "resuming controller failed[%d]\n", r);
5591 ssleep(MPI3MR_RESET_TOPOLOGY_SETTLE_TIME);
5592 scsi_unblock_requests(shost);
5593 mrioc->device_refresh_on = 0;
5594 mpi3mr_start_watchdog(mrioc);
5600 * mpi3mr_pcierr_error_detected - PCI error detected callback
5601 * @pdev: PCI device instance
5602 * @state: channel state
5604 * This function is called by the PCI error recovery driver and
5605 * based on the state passed the driver decides what actions to
5606 * be recommended back to PCI driver.
5608 * For all of the states if there is no valid mrioc or scsi host
5609 * references in the PCI device then this function will return
5610 * the result as disconnect.
5612 * For normal state, this function will return the result as can
5615 * For frozen state, this function will block for any pending
5616 * controller initialization or re-initialization to complete,
5617 * stop any new interactions with the controller and return
5618 * status as reset required.
5620 * For permanent failure state, this function will mark the
5621 * controller as unrecoverable and return status as disconnect.
5623 * Returns: PCI_ERS_RESULT_NEED_RESET or CAN_RECOVER or
5624 * DISCONNECT based on the controller state.
5626 static pci_ers_result_t
5627 mpi3mr_pcierr_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
5629 struct Scsi_Host *shost;
5630 struct mpi3mr_ioc *mrioc;
5631 unsigned int timeout = MPI3MR_RESET_TIMEOUT;
5633 dev_info(&pdev->dev, "%s: callback invoked state(%d)\n", __func__,
5636 shost = pci_get_drvdata(pdev);
5637 mrioc = shost_priv(shost);
5640 case pci_channel_io_normal:
5641 return PCI_ERS_RESULT_CAN_RECOVER;
5642 case pci_channel_io_frozen:
5643 mrioc->pci_err_recovery = true;
5644 mrioc->block_on_pci_err = true;
5646 if (mrioc->reset_in_progress || mrioc->is_driver_loading)
5650 } while (--timeout);
5653 mrioc->pci_err_recovery = true;
5654 mrioc->block_on_pci_err = true;
5655 mrioc->unrecoverable = 1;
5656 mpi3mr_stop_watchdog(mrioc);
5657 mpi3mr_flush_cmds_for_unrecovered_controller(mrioc);
5658 return PCI_ERS_RESULT_DISCONNECT;
5661 scsi_block_requests(mrioc->shost);
5662 mpi3mr_stop_watchdog(mrioc);
5663 mpi3mr_cleanup_resources(mrioc);
5664 return PCI_ERS_RESULT_NEED_RESET;
5665 case pci_channel_io_perm_failure:
5666 mrioc->pci_err_recovery = true;
5667 mrioc->block_on_pci_err = true;
5668 mrioc->unrecoverable = 1;
5669 mpi3mr_stop_watchdog(mrioc);
5670 mpi3mr_flush_cmds_for_unrecovered_controller(mrioc);
5671 return PCI_ERS_RESULT_DISCONNECT;
5673 return PCI_ERS_RESULT_DISCONNECT;
5678 * mpi3mr_pcierr_slot_reset - Post slot reset callback
5679 * @pdev: PCI device instance
5681 * This function is called by the PCI error recovery driver
5682 * after a slot or link reset issued by it for the recovery, the
5683 * driver is expected to bring back the controller and
5686 * This function restores PCI state and reinitializes controller
5687 * resources and the controller, this blocks for any pending
5688 * reset to complete.
5690 * Returns: PCI_ERS_RESULT_DISCONNECT on failure or
5691 * PCI_ERS_RESULT_RECOVERED
5693 static pci_ers_result_t mpi3mr_pcierr_slot_reset(struct pci_dev *pdev)
5695 struct Scsi_Host *shost;
5696 struct mpi3mr_ioc *mrioc;
5697 unsigned int timeout = MPI3MR_RESET_TIMEOUT;
5699 dev_info(&pdev->dev, "%s: callback invoked\n", __func__);
5701 shost = pci_get_drvdata(pdev);
5702 mrioc = shost_priv(shost);
5705 if (mrioc->reset_in_progress)
5709 } while (--timeout);
5714 pci_restore_state(pdev);
5716 if (mpi3mr_setup_resources(mrioc)) {
5717 ioc_err(mrioc, "setup resources failed\n");
5720 mrioc->unrecoverable = 0;
5721 mrioc->pci_err_recovery = false;
5723 if (mpi3mr_soft_reset_handler(mrioc, MPI3MR_RESET_FROM_FIRMWARE, 0))
5726 return PCI_ERS_RESULT_RECOVERED;
5729 mrioc->unrecoverable = 1;
5730 mrioc->block_on_pci_err = false;
5731 scsi_unblock_requests(shost);
5732 mpi3mr_start_watchdog(mrioc);
5733 return PCI_ERS_RESULT_DISCONNECT;
5737 * mpi3mr_pcierr_resume - PCI error recovery resume
5739 * @pdev: PCI device instance
5741 * This function enables all I/O and IOCTLs post reset issued as
5742 * part of the PCI error recovery
5746 static void mpi3mr_pcierr_resume(struct pci_dev *pdev)
5748 struct Scsi_Host *shost;
5749 struct mpi3mr_ioc *mrioc;
5751 dev_info(&pdev->dev, "%s: callback invoked\n", __func__);
5753 shost = pci_get_drvdata(pdev);
5754 mrioc = shost_priv(shost);
5756 if (mrioc->block_on_pci_err) {
5757 mrioc->block_on_pci_err = false;
5758 scsi_unblock_requests(shost);
5759 mpi3mr_start_watchdog(mrioc);
5764 * mpi3mr_pcierr_mmio_enabled - PCI error recovery callback
5765 * @pdev: PCI device instance
5767 * This is called only if mpi3mr_pcierr_error_detected returns
5768 * PCI_ERS_RESULT_CAN_RECOVER.
5770 * Return: PCI_ERS_RESULT_DISCONNECT when the controller is
5771 * unrecoverable or when the shost/mrioc reference cannot be
5772 * found, else return PCI_ERS_RESULT_RECOVERED
5774 static pci_ers_result_t mpi3mr_pcierr_mmio_enabled(struct pci_dev *pdev)
5776 struct Scsi_Host *shost;
5777 struct mpi3mr_ioc *mrioc;
5779 dev_info(&pdev->dev, "%s: callback invoked\n", __func__);
5781 shost = pci_get_drvdata(pdev);
5782 mrioc = shost_priv(shost);
5784 if (mrioc->unrecoverable)
5785 return PCI_ERS_RESULT_DISCONNECT;
5787 return PCI_ERS_RESULT_RECOVERED;
5790 static const struct pci_device_id mpi3mr_pci_id_table[] = {
5792 PCI_DEVICE_SUB(MPI3_MFGPAGE_VENDORID_BROADCOM,
5793 MPI3_MFGPAGE_DEVID_SAS4116, PCI_ANY_ID, PCI_ANY_ID)
5796 PCI_DEVICE_SUB(MPI3_MFGPAGE_VENDORID_BROADCOM,
5797 MPI3_MFGPAGE_DEVID_SAS5116_MPI, PCI_ANY_ID, PCI_ANY_ID)
5800 PCI_DEVICE_SUB(MPI3_MFGPAGE_VENDORID_BROADCOM,
5801 MPI3_MFGPAGE_DEVID_SAS5116_MPI_MGMT, PCI_ANY_ID, PCI_ANY_ID)
5805 MODULE_DEVICE_TABLE(pci, mpi3mr_pci_id_table);
5807 static struct pci_error_handlers mpi3mr_err_handler = {
5808 .error_detected = mpi3mr_pcierr_error_detected,
5809 .mmio_enabled = mpi3mr_pcierr_mmio_enabled,
5810 .slot_reset = mpi3mr_pcierr_slot_reset,
5811 .resume = mpi3mr_pcierr_resume,
5814 static SIMPLE_DEV_PM_OPS(mpi3mr_pm_ops, mpi3mr_suspend, mpi3mr_resume);
5816 static struct pci_driver mpi3mr_pci_driver = {
5817 .name = MPI3MR_DRIVER_NAME,
5818 .id_table = mpi3mr_pci_id_table,
5819 .probe = mpi3mr_probe,
5820 .remove = mpi3mr_remove,
5821 .shutdown = mpi3mr_shutdown,
5822 .err_handler = &mpi3mr_err_handler,
5823 .driver.pm = &mpi3mr_pm_ops,
5826 static ssize_t event_counter_show(struct device_driver *dd, char *buf)
5828 return sprintf(buf, "%llu\n", atomic64_read(&event_counter));
5830 static DRIVER_ATTR_RO(event_counter);
5832 static int __init mpi3mr_init(void)
5836 pr_info("Loading %s version %s\n", MPI3MR_DRIVER_NAME,
5837 MPI3MR_DRIVER_VERSION);
5839 mpi3mr_transport_template =
5840 sas_attach_transport(&mpi3mr_transport_functions);
5841 if (!mpi3mr_transport_template) {
5842 pr_err("%s failed to load due to sas transport attach failure\n",
5843 MPI3MR_DRIVER_NAME);
5847 ret_val = pci_register_driver(&mpi3mr_pci_driver);
5849 pr_err("%s failed to load due to pci register driver failure\n",
5850 MPI3MR_DRIVER_NAME);
5851 goto err_pci_reg_fail;
5854 ret_val = driver_create_file(&mpi3mr_pci_driver.driver,
5855 &driver_attr_event_counter);
5857 goto err_event_counter;
5862 pci_unregister_driver(&mpi3mr_pci_driver);
5865 sas_release_transport(mpi3mr_transport_template);
5869 static void __exit mpi3mr_exit(void)
5871 if (warn_non_secure_ctlr)
5873 "Unloading %s version %s while managing a non secure controller\n",
5874 MPI3MR_DRIVER_NAME, MPI3MR_DRIVER_VERSION);
5876 pr_info("Unloading %s version %s\n", MPI3MR_DRIVER_NAME,
5877 MPI3MR_DRIVER_VERSION);
5879 driver_remove_file(&mpi3mr_pci_driver.driver,
5880 &driver_attr_event_counter);
5881 pci_unregister_driver(&mpi3mr_pci_driver);
5882 sas_release_transport(mpi3mr_transport_template);
5883 ida_destroy(&mrioc_ida);
5886 module_init(mpi3mr_init);
5887 module_exit(mpi3mr_exit);