1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Broadcom MPI3 Storage Controllers
5 * Copyright (C) 2017-2023 Broadcom Inc.
6 * (mailto: mpi3mr-linuxdrv.pdl@broadcom.com)
12 /* global driver scop variables */
13 LIST_HEAD(mrioc_list);
14 DEFINE_SPINLOCK(mrioc_list_lock);
16 static int warn_non_secure_ctlr;
17 atomic64_t event_counter;
19 MODULE_AUTHOR(MPI3MR_DRIVER_AUTHOR);
20 MODULE_DESCRIPTION(MPI3MR_DRIVER_DESC);
21 MODULE_LICENSE(MPI3MR_DRIVER_LICENSE);
22 MODULE_VERSION(MPI3MR_DRIVER_VERSION);
24 /* Module parameters*/
26 module_param(prot_mask, int, 0);
27 MODULE_PARM_DESC(prot_mask, "Host protection capabilities mask, def=0x07");
29 static int prot_guard_mask = 3;
30 module_param(prot_guard_mask, int, 0);
31 MODULE_PARM_DESC(prot_guard_mask, " Host protection guard mask, def=3");
32 static int logging_level;
33 module_param(logging_level, int, 0);
34 MODULE_PARM_DESC(logging_level,
35 " bits for enabling additional logging info (default=0)");
36 static int max_sgl_entries = MPI3MR_DEFAULT_SGL_ENTRIES;
37 module_param(max_sgl_entries, int, 0444);
38 MODULE_PARM_DESC(max_sgl_entries,
39 "Preferred max number of SG entries to be used for a single I/O\n"
40 "The actual value will be determined by the driver\n"
41 "(Minimum=256, Maximum=2048, default=256)");
43 /* Forward declarations*/
44 static void mpi3mr_send_event_ack(struct mpi3mr_ioc *mrioc, u8 event,
45 struct mpi3mr_drv_cmd *cmdparam, u32 event_ctx);
47 #define MPI3MR_DRIVER_EVENT_TG_QD_REDUCTION (0xFFFF)
49 #define MPI3_EVENT_WAIT_FOR_DEVICES_TO_REFRESH (0xFFFE)
52 * mpi3mr_host_tag_for_scmd - Get host tag for a scmd
53 * @mrioc: Adapter instance reference
54 * @scmd: SCSI command reference
56 * Calculate the host tag based on block tag for a given scmd.
58 * Return: Valid host tag or MPI3MR_HOSTTAG_INVALID.
60 static u16 mpi3mr_host_tag_for_scmd(struct mpi3mr_ioc *mrioc,
61 struct scsi_cmnd *scmd)
63 struct scmd_priv *priv = NULL;
65 u16 host_tag, hw_queue;
67 unique_tag = blk_mq_unique_tag(scsi_cmd_to_rq(scmd));
69 hw_queue = blk_mq_unique_tag_to_hwq(unique_tag);
70 if (hw_queue >= mrioc->num_op_reply_q)
71 return MPI3MR_HOSTTAG_INVALID;
72 host_tag = blk_mq_unique_tag_to_tag(unique_tag);
74 if (WARN_ON(host_tag >= mrioc->max_host_ios))
75 return MPI3MR_HOSTTAG_INVALID;
77 priv = scsi_cmd_priv(scmd);
78 /*host_tag 0 is invalid hence incrementing by 1*/
79 priv->host_tag = host_tag + 1;
81 priv->in_lld_scope = 1;
82 priv->req_q_idx = hw_queue;
83 priv->meta_chain_idx = -1;
85 priv->meta_sg_valid = 0;
86 return priv->host_tag;
90 * mpi3mr_scmd_from_host_tag - Get SCSI command from host tag
91 * @mrioc: Adapter instance reference
93 * @qidx: Operational queue index
95 * Identify the block tag from the host tag and queue index and
96 * retrieve associated scsi command using scsi_host_find_tag().
98 * Return: SCSI command reference or NULL.
100 static struct scsi_cmnd *mpi3mr_scmd_from_host_tag(
101 struct mpi3mr_ioc *mrioc, u16 host_tag, u16 qidx)
103 struct scsi_cmnd *scmd = NULL;
104 struct scmd_priv *priv = NULL;
105 u32 unique_tag = host_tag - 1;
107 if (WARN_ON(host_tag > mrioc->max_host_ios))
110 unique_tag |= (qidx << BLK_MQ_UNIQUE_TAG_BITS);
112 scmd = scsi_host_find_tag(mrioc->shost, unique_tag);
114 priv = scsi_cmd_priv(scmd);
115 if (!priv->in_lld_scope)
123 * mpi3mr_clear_scmd_priv - Cleanup SCSI command private date
124 * @mrioc: Adapter instance reference
125 * @scmd: SCSI command reference
127 * Invalidate the SCSI command private data to mark the command
128 * is not in LLD scope anymore.
132 static void mpi3mr_clear_scmd_priv(struct mpi3mr_ioc *mrioc,
133 struct scsi_cmnd *scmd)
135 struct scmd_priv *priv = NULL;
137 priv = scsi_cmd_priv(scmd);
139 if (WARN_ON(priv->in_lld_scope == 0))
141 priv->host_tag = MPI3MR_HOSTTAG_INVALID;
142 priv->req_q_idx = 0xFFFF;
144 priv->in_lld_scope = 0;
145 priv->meta_sg_valid = 0;
146 if (priv->chain_idx >= 0) {
147 clear_bit(priv->chain_idx, mrioc->chain_bitmap);
148 priv->chain_idx = -1;
150 if (priv->meta_chain_idx >= 0) {
151 clear_bit(priv->meta_chain_idx, mrioc->chain_bitmap);
152 priv->meta_chain_idx = -1;
156 static void mpi3mr_dev_rmhs_send_tm(struct mpi3mr_ioc *mrioc, u16 handle,
157 struct mpi3mr_drv_cmd *cmdparam, u8 iou_rc);
158 static void mpi3mr_fwevt_worker(struct work_struct *work);
161 * mpi3mr_fwevt_free - firmware event memory dealloctor
162 * @r: k reference pointer of the firmware event
164 * Free firmware event memory when no reference.
166 static void mpi3mr_fwevt_free(struct kref *r)
168 kfree(container_of(r, struct mpi3mr_fwevt, ref_count));
172 * mpi3mr_fwevt_get - k reference incrementor
173 * @fwevt: Firmware event reference
175 * Increment firmware event reference count.
177 static void mpi3mr_fwevt_get(struct mpi3mr_fwevt *fwevt)
179 kref_get(&fwevt->ref_count);
183 * mpi3mr_fwevt_put - k reference decrementor
184 * @fwevt: Firmware event reference
186 * decrement firmware event reference count.
188 static void mpi3mr_fwevt_put(struct mpi3mr_fwevt *fwevt)
190 kref_put(&fwevt->ref_count, mpi3mr_fwevt_free);
194 * mpi3mr_alloc_fwevt - Allocate firmware event
195 * @len: length of firmware event data to allocate
197 * Allocate firmware event with required length and initialize
198 * the reference counter.
200 * Return: firmware event reference.
202 static struct mpi3mr_fwevt *mpi3mr_alloc_fwevt(int len)
204 struct mpi3mr_fwevt *fwevt;
206 fwevt = kzalloc(sizeof(*fwevt) + len, GFP_ATOMIC);
210 kref_init(&fwevt->ref_count);
215 * mpi3mr_fwevt_add_to_list - Add firmware event to the list
216 * @mrioc: Adapter instance reference
217 * @fwevt: Firmware event reference
219 * Add the given firmware event to the firmware event list.
223 static void mpi3mr_fwevt_add_to_list(struct mpi3mr_ioc *mrioc,
224 struct mpi3mr_fwevt *fwevt)
228 if (!mrioc->fwevt_worker_thread)
231 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
232 /* get fwevt reference count while adding it to fwevt_list */
233 mpi3mr_fwevt_get(fwevt);
234 INIT_LIST_HEAD(&fwevt->list);
235 list_add_tail(&fwevt->list, &mrioc->fwevt_list);
236 INIT_WORK(&fwevt->work, mpi3mr_fwevt_worker);
237 /* get fwevt reference count while enqueueing it to worker queue */
238 mpi3mr_fwevt_get(fwevt);
239 queue_work(mrioc->fwevt_worker_thread, &fwevt->work);
240 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
244 * mpi3mr_fwevt_del_from_list - Delete firmware event from list
245 * @mrioc: Adapter instance reference
246 * @fwevt: Firmware event reference
248 * Delete the given firmware event from the firmware event list.
252 static void mpi3mr_fwevt_del_from_list(struct mpi3mr_ioc *mrioc,
253 struct mpi3mr_fwevt *fwevt)
257 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
258 if (!list_empty(&fwevt->list)) {
259 list_del_init(&fwevt->list);
261 * Put fwevt reference count after
262 * removing it from fwevt_list
264 mpi3mr_fwevt_put(fwevt);
266 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
270 * mpi3mr_dequeue_fwevt - Dequeue firmware event from the list
271 * @mrioc: Adapter instance reference
273 * Dequeue a firmware event from the firmware event list.
275 * Return: firmware event.
277 static struct mpi3mr_fwevt *mpi3mr_dequeue_fwevt(
278 struct mpi3mr_ioc *mrioc)
281 struct mpi3mr_fwevt *fwevt = NULL;
283 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
284 if (!list_empty(&mrioc->fwevt_list)) {
285 fwevt = list_first_entry(&mrioc->fwevt_list,
286 struct mpi3mr_fwevt, list);
287 list_del_init(&fwevt->list);
289 * Put fwevt reference count after
290 * removing it from fwevt_list
292 mpi3mr_fwevt_put(fwevt);
294 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
300 * mpi3mr_cancel_work - cancel firmware event
301 * @fwevt: fwevt object which needs to be canceled
305 static void mpi3mr_cancel_work(struct mpi3mr_fwevt *fwevt)
308 * Wait on the fwevt to complete. If this returns 1, then
309 * the event was never executed.
311 * If it did execute, we wait for it to finish, and the put will
312 * happen from mpi3mr_process_fwevt()
314 if (cancel_work_sync(&fwevt->work)) {
316 * Put fwevt reference count after
317 * dequeuing it from worker queue
319 mpi3mr_fwevt_put(fwevt);
321 * Put fwevt reference count to neutralize
322 * kref_init increment
324 mpi3mr_fwevt_put(fwevt);
329 * mpi3mr_cleanup_fwevt_list - Cleanup firmware event list
330 * @mrioc: Adapter instance reference
332 * Flush all pending firmware events from the firmware event
337 void mpi3mr_cleanup_fwevt_list(struct mpi3mr_ioc *mrioc)
339 struct mpi3mr_fwevt *fwevt = NULL;
341 if ((list_empty(&mrioc->fwevt_list) && !mrioc->current_event) ||
342 !mrioc->fwevt_worker_thread)
345 while ((fwevt = mpi3mr_dequeue_fwevt(mrioc)))
346 mpi3mr_cancel_work(fwevt);
348 if (mrioc->current_event) {
349 fwevt = mrioc->current_event;
351 * Don't call cancel_work_sync() API for the
352 * fwevt work if the controller reset is
353 * get called as part of processing the
354 * same fwevt work (or) when worker thread is
355 * waiting for device add/remove APIs to complete.
356 * Otherwise we will see deadlock.
358 if (current_work() == &fwevt->work || fwevt->pending_at_sml) {
363 mpi3mr_cancel_work(fwevt);
368 * mpi3mr_queue_qd_reduction_event - Queue TG QD reduction event
369 * @mrioc: Adapter instance reference
370 * @tg: Throttle group information pointer
372 * Accessor to queue on synthetically generated driver event to
373 * the event worker thread, the driver event will be used to
374 * reduce the QD of all VDs in the TG from the worker thread.
378 static void mpi3mr_queue_qd_reduction_event(struct mpi3mr_ioc *mrioc,
379 struct mpi3mr_throttle_group_info *tg)
381 struct mpi3mr_fwevt *fwevt;
382 u16 sz = sizeof(struct mpi3mr_throttle_group_info *);
385 * If the QD reduction event is already queued due to throttle and if
386 * the QD is not restored through device info change event
387 * then dont queue further reduction events
389 if (tg->fw_qd != tg->modified_qd)
392 fwevt = mpi3mr_alloc_fwevt(sz);
394 ioc_warn(mrioc, "failed to queue TG QD reduction event\n");
397 *(struct mpi3mr_throttle_group_info **)fwevt->event_data = tg;
398 fwevt->mrioc = mrioc;
399 fwevt->event_id = MPI3MR_DRIVER_EVENT_TG_QD_REDUCTION;
401 fwevt->process_evt = 1;
403 fwevt->event_data_size = sz;
404 tg->modified_qd = max_t(u16, (tg->fw_qd * tg->qd_reduction) / 10, 8);
406 dprint_event_bh(mrioc, "qd reduction event queued for tg_id(%d)\n",
408 mpi3mr_fwevt_add_to_list(mrioc, fwevt);
412 * mpi3mr_invalidate_devhandles -Invalidate device handles
413 * @mrioc: Adapter instance reference
415 * Invalidate the device handles in the target device structures
416 * . Called post reset prior to reinitializing the controller.
420 void mpi3mr_invalidate_devhandles(struct mpi3mr_ioc *mrioc)
422 struct mpi3mr_tgt_dev *tgtdev;
423 struct mpi3mr_stgt_priv_data *tgt_priv;
425 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
426 tgtdev->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
427 if (tgtdev->starget && tgtdev->starget->hostdata) {
428 tgt_priv = tgtdev->starget->hostdata;
429 tgt_priv->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
430 tgt_priv->io_throttle_enabled = 0;
431 tgt_priv->io_divert = 0;
432 tgt_priv->throttle_group = NULL;
434 if (tgtdev->host_exposed)
435 atomic_set(&tgt_priv->block_io, 1);
441 * mpi3mr_print_scmd - print individual SCSI command
443 * @data: Adapter instance reference
445 * Print the SCSI command details if it is in LLD scope.
447 * Return: true always.
449 static bool mpi3mr_print_scmd(struct request *rq, void *data)
451 struct mpi3mr_ioc *mrioc = (struct mpi3mr_ioc *)data;
452 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
453 struct scmd_priv *priv = NULL;
456 priv = scsi_cmd_priv(scmd);
457 if (!priv->in_lld_scope)
460 ioc_info(mrioc, "%s :Host Tag = %d, qid = %d\n",
461 __func__, priv->host_tag, priv->req_q_idx + 1);
462 scsi_print_command(scmd);
470 * mpi3mr_flush_scmd - Flush individual SCSI command
472 * @data: Adapter instance reference
474 * Return the SCSI command to the upper layers if it is in LLD
477 * Return: true always.
480 static bool mpi3mr_flush_scmd(struct request *rq, void *data)
482 struct mpi3mr_ioc *mrioc = (struct mpi3mr_ioc *)data;
483 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
484 struct scmd_priv *priv = NULL;
487 priv = scsi_cmd_priv(scmd);
488 if (!priv->in_lld_scope)
491 if (priv->meta_sg_valid)
492 dma_unmap_sg(&mrioc->pdev->dev, scsi_prot_sglist(scmd),
493 scsi_prot_sg_count(scmd), scmd->sc_data_direction);
494 mpi3mr_clear_scmd_priv(mrioc, scmd);
495 scsi_dma_unmap(scmd);
496 scmd->result = DID_RESET << 16;
497 scsi_print_command(scmd);
499 mrioc->flush_io_count++;
507 * mpi3mr_count_dev_pending - Count commands pending for a lun
509 * @data: SCSI device reference
511 * This is an iterator function called for each SCSI command in
512 * a host and if the command is pending in the LLD for the
513 * specific device(lun) then device specific pending I/O counter
514 * is updated in the device structure.
516 * Return: true always.
519 static bool mpi3mr_count_dev_pending(struct request *rq, void *data)
521 struct scsi_device *sdev = (struct scsi_device *)data;
522 struct mpi3mr_sdev_priv_data *sdev_priv_data = sdev->hostdata;
523 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
524 struct scmd_priv *priv;
527 priv = scsi_cmd_priv(scmd);
528 if (!priv->in_lld_scope)
530 if (scmd->device == sdev)
531 sdev_priv_data->pend_count++;
539 * mpi3mr_count_tgt_pending - Count commands pending for target
541 * @data: SCSI target reference
543 * This is an iterator function called for each SCSI command in
544 * a host and if the command is pending in the LLD for the
545 * specific target then target specific pending I/O counter is
546 * updated in the target structure.
548 * Return: true always.
551 static bool mpi3mr_count_tgt_pending(struct request *rq, void *data)
553 struct scsi_target *starget = (struct scsi_target *)data;
554 struct mpi3mr_stgt_priv_data *stgt_priv_data = starget->hostdata;
555 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
556 struct scmd_priv *priv;
559 priv = scsi_cmd_priv(scmd);
560 if (!priv->in_lld_scope)
562 if (scmd->device && (scsi_target(scmd->device) == starget))
563 stgt_priv_data->pend_count++;
571 * mpi3mr_flush_host_io - Flush host I/Os
572 * @mrioc: Adapter instance reference
574 * Flush all of the pending I/Os by calling
575 * blk_mq_tagset_busy_iter() for each possible tag. This is
576 * executed post controller reset
580 void mpi3mr_flush_host_io(struct mpi3mr_ioc *mrioc)
582 struct Scsi_Host *shost = mrioc->shost;
584 mrioc->flush_io_count = 0;
585 ioc_info(mrioc, "%s :Flushing Host I/O cmds post reset\n", __func__);
586 blk_mq_tagset_busy_iter(&shost->tag_set,
587 mpi3mr_flush_scmd, (void *)mrioc);
588 ioc_info(mrioc, "%s :Flushed %d Host I/O cmds\n", __func__,
589 mrioc->flush_io_count);
593 * mpi3mr_flush_cmds_for_unrecovered_controller - Flush all pending cmds
594 * @mrioc: Adapter instance reference
596 * This function waits for currently running IO poll threads to
597 * exit and then flushes all host I/Os and any internal pending
598 * cmds. This is executed after controller is marked as
603 void mpi3mr_flush_cmds_for_unrecovered_controller(struct mpi3mr_ioc *mrioc)
605 struct Scsi_Host *shost = mrioc->shost;
608 if (!mrioc->unrecoverable)
611 if (mrioc->op_reply_qinfo) {
612 for (i = 0; i < mrioc->num_queues; i++) {
613 while (atomic_read(&mrioc->op_reply_qinfo[i].in_use))
615 atomic_set(&mrioc->op_reply_qinfo[i].pend_ios, 0);
618 mrioc->flush_io_count = 0;
619 blk_mq_tagset_busy_iter(&shost->tag_set,
620 mpi3mr_flush_scmd, (void *)mrioc);
621 mpi3mr_flush_delayed_cmd_lists(mrioc);
622 mpi3mr_flush_drv_cmds(mrioc);
626 * mpi3mr_alloc_tgtdev - target device allocator
628 * Allocate target device instance and initialize the reference
631 * Return: target device instance.
633 static struct mpi3mr_tgt_dev *mpi3mr_alloc_tgtdev(void)
635 struct mpi3mr_tgt_dev *tgtdev;
637 tgtdev = kzalloc(sizeof(*tgtdev), GFP_ATOMIC);
640 kref_init(&tgtdev->ref_count);
645 * mpi3mr_tgtdev_add_to_list -Add tgtdevice to the list
646 * @mrioc: Adapter instance reference
647 * @tgtdev: Target device
649 * Add the target device to the target device list
653 static void mpi3mr_tgtdev_add_to_list(struct mpi3mr_ioc *mrioc,
654 struct mpi3mr_tgt_dev *tgtdev)
658 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
659 mpi3mr_tgtdev_get(tgtdev);
660 INIT_LIST_HEAD(&tgtdev->list);
661 list_add_tail(&tgtdev->list, &mrioc->tgtdev_list);
662 tgtdev->state = MPI3MR_DEV_CREATED;
663 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
667 * mpi3mr_tgtdev_del_from_list -Delete tgtdevice from the list
668 * @mrioc: Adapter instance reference
669 * @tgtdev: Target device
670 * @must_delete: Must delete the target device from the list irrespective
671 * of the device state.
673 * Remove the target device from the target device list
677 static void mpi3mr_tgtdev_del_from_list(struct mpi3mr_ioc *mrioc,
678 struct mpi3mr_tgt_dev *tgtdev, bool must_delete)
682 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
683 if ((tgtdev->state == MPI3MR_DEV_REMOVE_HS_STARTED) || (must_delete == true)) {
684 if (!list_empty(&tgtdev->list)) {
685 list_del_init(&tgtdev->list);
686 tgtdev->state = MPI3MR_DEV_DELETED;
687 mpi3mr_tgtdev_put(tgtdev);
690 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
694 * __mpi3mr_get_tgtdev_by_handle -Get tgtdev from device handle
695 * @mrioc: Adapter instance reference
696 * @handle: Device handle
698 * Accessor to retrieve target device from the device handle.
701 * Return: Target device reference.
703 static struct mpi3mr_tgt_dev *__mpi3mr_get_tgtdev_by_handle(
704 struct mpi3mr_ioc *mrioc, u16 handle)
706 struct mpi3mr_tgt_dev *tgtdev;
708 assert_spin_locked(&mrioc->tgtdev_lock);
709 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list)
710 if (tgtdev->dev_handle == handle)
715 mpi3mr_tgtdev_get(tgtdev);
720 * mpi3mr_get_tgtdev_by_handle -Get tgtdev from device handle
721 * @mrioc: Adapter instance reference
722 * @handle: Device handle
724 * Accessor to retrieve target device from the device handle.
727 * Return: Target device reference.
729 struct mpi3mr_tgt_dev *mpi3mr_get_tgtdev_by_handle(
730 struct mpi3mr_ioc *mrioc, u16 handle)
732 struct mpi3mr_tgt_dev *tgtdev;
735 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
736 tgtdev = __mpi3mr_get_tgtdev_by_handle(mrioc, handle);
737 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
742 * __mpi3mr_get_tgtdev_by_perst_id -Get tgtdev from persist ID
743 * @mrioc: Adapter instance reference
744 * @persist_id: Persistent ID
746 * Accessor to retrieve target device from the Persistent ID.
749 * Return: Target device reference.
751 static struct mpi3mr_tgt_dev *__mpi3mr_get_tgtdev_by_perst_id(
752 struct mpi3mr_ioc *mrioc, u16 persist_id)
754 struct mpi3mr_tgt_dev *tgtdev;
756 assert_spin_locked(&mrioc->tgtdev_lock);
757 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list)
758 if (tgtdev->perst_id == persist_id)
763 mpi3mr_tgtdev_get(tgtdev);
768 * mpi3mr_get_tgtdev_by_perst_id -Get tgtdev from persistent ID
769 * @mrioc: Adapter instance reference
770 * @persist_id: Persistent ID
772 * Accessor to retrieve target device from the Persistent ID.
775 * Return: Target device reference.
777 static struct mpi3mr_tgt_dev *mpi3mr_get_tgtdev_by_perst_id(
778 struct mpi3mr_ioc *mrioc, u16 persist_id)
780 struct mpi3mr_tgt_dev *tgtdev;
783 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
784 tgtdev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, persist_id);
785 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
790 * __mpi3mr_get_tgtdev_from_tgtpriv -Get tgtdev from tgt private
791 * @mrioc: Adapter instance reference
792 * @tgt_priv: Target private data
794 * Accessor to return target device from the target private
795 * data. Non Lock version
797 * Return: Target device reference.
799 static struct mpi3mr_tgt_dev *__mpi3mr_get_tgtdev_from_tgtpriv(
800 struct mpi3mr_ioc *mrioc, struct mpi3mr_stgt_priv_data *tgt_priv)
802 struct mpi3mr_tgt_dev *tgtdev;
804 assert_spin_locked(&mrioc->tgtdev_lock);
805 tgtdev = tgt_priv->tgt_dev;
807 mpi3mr_tgtdev_get(tgtdev);
812 * mpi3mr_set_io_divert_for_all_vd_in_tg -set divert for TG VDs
813 * @mrioc: Adapter instance reference
814 * @tg: Throttle group information pointer
815 * @divert_value: 1 or 0
817 * Accessor to set io_divert flag for each device associated
818 * with the given throttle group with the given value.
822 static void mpi3mr_set_io_divert_for_all_vd_in_tg(struct mpi3mr_ioc *mrioc,
823 struct mpi3mr_throttle_group_info *tg, u8 divert_value)
826 struct mpi3mr_tgt_dev *tgtdev;
827 struct mpi3mr_stgt_priv_data *tgt_priv;
829 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
830 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
831 if (tgtdev->starget && tgtdev->starget->hostdata) {
832 tgt_priv = tgtdev->starget->hostdata;
833 if (tgt_priv->throttle_group == tg)
834 tgt_priv->io_divert = divert_value;
837 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
841 * mpi3mr_print_device_event_notice - print notice related to post processing of
842 * device event after controller reset.
844 * @mrioc: Adapter instance reference
845 * @device_add: true for device add event and false for device removal event
849 void mpi3mr_print_device_event_notice(struct mpi3mr_ioc *mrioc,
852 ioc_notice(mrioc, "Device %s was in progress before the reset and\n",
853 (device_add ? "addition" : "removal"));
854 ioc_notice(mrioc, "completed after reset, verify whether the exposed devices\n");
855 ioc_notice(mrioc, "are matched with attached devices for correctness\n");
859 * mpi3mr_remove_tgtdev_from_host - Remove dev from upper layers
860 * @mrioc: Adapter instance reference
861 * @tgtdev: Target device structure
863 * Checks whether the device is exposed to upper layers and if it
864 * is then remove the device from upper layers by calling
865 * scsi_remove_target().
867 * Return: 0 on success, non zero on failure.
869 void mpi3mr_remove_tgtdev_from_host(struct mpi3mr_ioc *mrioc,
870 struct mpi3mr_tgt_dev *tgtdev)
872 struct mpi3mr_stgt_priv_data *tgt_priv;
874 ioc_info(mrioc, "%s :Removing handle(0x%04x), wwid(0x%016llx)\n",
875 __func__, tgtdev->dev_handle, (unsigned long long)tgtdev->wwid);
876 if (tgtdev->starget && tgtdev->starget->hostdata) {
877 tgt_priv = tgtdev->starget->hostdata;
878 atomic_set(&tgt_priv->block_io, 0);
879 tgt_priv->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
882 if (!mrioc->sas_transport_enabled || (tgtdev->dev_type !=
883 MPI3_DEVICE_DEVFORM_SAS_SATA) || tgtdev->non_stl) {
884 if (tgtdev->starget) {
885 if (mrioc->current_event)
886 mrioc->current_event->pending_at_sml = 1;
887 scsi_remove_target(&tgtdev->starget->dev);
888 tgtdev->host_exposed = 0;
889 if (mrioc->current_event) {
890 mrioc->current_event->pending_at_sml = 0;
891 if (mrioc->current_event->discard) {
892 mpi3mr_print_device_event_notice(mrioc,
899 mpi3mr_remove_tgtdev_from_sas_transport(mrioc, tgtdev);
901 ioc_info(mrioc, "%s :Removed handle(0x%04x), wwid(0x%016llx)\n",
902 __func__, tgtdev->dev_handle, (unsigned long long)tgtdev->wwid);
906 * mpi3mr_report_tgtdev_to_host - Expose device to upper layers
907 * @mrioc: Adapter instance reference
908 * @perst_id: Persistent ID of the device
910 * Checks whether the device can be exposed to upper layers and
911 * if it is not then expose the device to upper layers by
912 * calling scsi_scan_target().
914 * Return: 0 on success, non zero on failure.
916 static int mpi3mr_report_tgtdev_to_host(struct mpi3mr_ioc *mrioc,
920 struct mpi3mr_tgt_dev *tgtdev;
922 if (mrioc->reset_in_progress)
925 tgtdev = mpi3mr_get_tgtdev_by_perst_id(mrioc, perst_id);
930 if (tgtdev->is_hidden || tgtdev->host_exposed) {
934 if (!mrioc->sas_transport_enabled || (tgtdev->dev_type !=
935 MPI3_DEVICE_DEVFORM_SAS_SATA) || tgtdev->non_stl){
936 tgtdev->host_exposed = 1;
937 if (mrioc->current_event)
938 mrioc->current_event->pending_at_sml = 1;
939 scsi_scan_target(&mrioc->shost->shost_gendev,
940 mrioc->scsi_device_channel, tgtdev->perst_id,
941 SCAN_WILD_CARD, SCSI_SCAN_INITIAL);
942 if (!tgtdev->starget)
943 tgtdev->host_exposed = 0;
944 if (mrioc->current_event) {
945 mrioc->current_event->pending_at_sml = 0;
946 if (mrioc->current_event->discard) {
947 mpi3mr_print_device_event_notice(mrioc, true);
952 mpi3mr_report_tgtdev_to_sas_transport(mrioc, tgtdev);
955 mpi3mr_tgtdev_put(tgtdev);
961 * mpi3mr_change_queue_depth- Change QD callback handler
962 * @sdev: SCSI device reference
963 * @q_depth: Queue depth
965 * Validate and limit QD and call scsi_change_queue_depth.
967 * Return: return value of scsi_change_queue_depth
969 static int mpi3mr_change_queue_depth(struct scsi_device *sdev,
972 struct scsi_target *starget = scsi_target(sdev);
973 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
976 if (!sdev->tagged_supported)
978 if (q_depth > shost->can_queue)
979 q_depth = shost->can_queue;
981 q_depth = MPI3MR_DEFAULT_SDEV_QD;
982 retval = scsi_change_queue_depth(sdev, q_depth);
983 sdev->max_queue_depth = sdev->queue_depth;
989 * mpi3mr_update_sdev - Update SCSI device information
990 * @sdev: SCSI device reference
991 * @data: target device reference
993 * This is an iterator function called for each SCSI device in a
994 * target to update the target specific information into each
1000 mpi3mr_update_sdev(struct scsi_device *sdev, void *data)
1002 struct mpi3mr_tgt_dev *tgtdev;
1004 tgtdev = (struct mpi3mr_tgt_dev *)data;
1008 mpi3mr_change_queue_depth(sdev, tgtdev->q_depth);
1009 switch (tgtdev->dev_type) {
1010 case MPI3_DEVICE_DEVFORM_PCIE:
1011 /*The block layer hw sector size = 512*/
1012 if ((tgtdev->dev_spec.pcie_inf.dev_info &
1013 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) ==
1014 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) {
1015 blk_queue_max_hw_sectors(sdev->request_queue,
1016 tgtdev->dev_spec.pcie_inf.mdts / 512);
1017 if (tgtdev->dev_spec.pcie_inf.pgsz == 0)
1018 blk_queue_virt_boundary(sdev->request_queue,
1019 ((1 << MPI3MR_DEFAULT_PGSZEXP) - 1));
1021 blk_queue_virt_boundary(sdev->request_queue,
1022 ((1 << tgtdev->dev_spec.pcie_inf.pgsz) - 1));
1031 * mpi3mr_rfresh_tgtdevs - Refresh target device exposure
1032 * @mrioc: Adapter instance reference
1034 * This is executed post controller reset to identify any
1035 * missing devices during reset and remove from the upper layers
1036 * or expose any newly detected device to the upper layers.
1041 void mpi3mr_rfresh_tgtdevs(struct mpi3mr_ioc *mrioc)
1043 struct mpi3mr_tgt_dev *tgtdev, *tgtdev_next;
1044 struct mpi3mr_stgt_priv_data *tgt_priv;
1046 dprint_reset(mrioc, "refresh target devices: check for removals\n");
1047 list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
1049 if ((tgtdev->dev_handle == MPI3MR_INVALID_DEV_HANDLE) &&
1050 tgtdev->is_hidden &&
1051 tgtdev->host_exposed && tgtdev->starget &&
1052 tgtdev->starget->hostdata) {
1053 tgt_priv = tgtdev->starget->hostdata;
1054 tgt_priv->dev_removed = 1;
1055 atomic_set(&tgt_priv->block_io, 0);
1059 list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
1061 if (tgtdev->dev_handle == MPI3MR_INVALID_DEV_HANDLE) {
1062 dprint_reset(mrioc, "removing target device with perst_id(%d)\n",
1064 if (tgtdev->host_exposed)
1065 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1066 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, true);
1067 mpi3mr_tgtdev_put(tgtdev);
1068 } else if (tgtdev->is_hidden & tgtdev->host_exposed) {
1069 dprint_reset(mrioc, "hiding target device with perst_id(%d)\n",
1071 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1076 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
1077 if ((tgtdev->dev_handle != MPI3MR_INVALID_DEV_HANDLE) &&
1078 !tgtdev->is_hidden) {
1079 if (!tgtdev->host_exposed)
1080 mpi3mr_report_tgtdev_to_host(mrioc,
1082 else if (tgtdev->starget)
1083 starget_for_each_device(tgtdev->starget,
1084 (void *)tgtdev, mpi3mr_update_sdev);
1090 * mpi3mr_update_tgtdev - DevStatusChange evt bottomhalf
1091 * @mrioc: Adapter instance reference
1092 * @tgtdev: Target device internal structure
1093 * @dev_pg0: New device page0
1094 * @is_added: Flag to indicate the device is just added
1096 * Update the information from the device page0 into the driver
1097 * cached target device structure.
1101 static void mpi3mr_update_tgtdev(struct mpi3mr_ioc *mrioc,
1102 struct mpi3mr_tgt_dev *tgtdev, struct mpi3_device_page0 *dev_pg0,
1106 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
1107 struct mpi3mr_enclosure_node *enclosure_dev = NULL;
1110 tgtdev->perst_id = le16_to_cpu(dev_pg0->persistent_id);
1111 tgtdev->dev_handle = le16_to_cpu(dev_pg0->dev_handle);
1112 tgtdev->dev_type = dev_pg0->device_form;
1113 tgtdev->io_unit_port = dev_pg0->io_unit_port;
1114 tgtdev->encl_handle = le16_to_cpu(dev_pg0->enclosure_handle);
1115 tgtdev->parent_handle = le16_to_cpu(dev_pg0->parent_dev_handle);
1116 tgtdev->slot = le16_to_cpu(dev_pg0->slot);
1117 tgtdev->q_depth = le16_to_cpu(dev_pg0->queue_depth);
1118 tgtdev->wwid = le64_to_cpu(dev_pg0->wwid);
1119 tgtdev->devpg0_flag = le16_to_cpu(dev_pg0->flags);
1121 if (tgtdev->encl_handle)
1122 enclosure_dev = mpi3mr_enclosure_find_by_handle(mrioc,
1123 tgtdev->encl_handle);
1125 tgtdev->enclosure_logical_id = le64_to_cpu(
1126 enclosure_dev->pg0.enclosure_logical_id);
1128 flags = tgtdev->devpg0_flag;
1130 tgtdev->is_hidden = (flags & MPI3_DEVICE0_FLAGS_HIDDEN);
1132 if (is_added == true)
1133 tgtdev->io_throttle_enabled =
1134 (flags & MPI3_DEVICE0_FLAGS_IO_THROTTLING_REQUIRED) ? 1 : 0;
1136 switch (flags & MPI3_DEVICE0_FLAGS_MAX_WRITE_SAME_MASK) {
1137 case MPI3_DEVICE0_FLAGS_MAX_WRITE_SAME_256_LB:
1138 tgtdev->wslen = MPI3MR_WRITE_SAME_MAX_LEN_256_BLKS;
1140 case MPI3_DEVICE0_FLAGS_MAX_WRITE_SAME_2048_LB:
1141 tgtdev->wslen = MPI3MR_WRITE_SAME_MAX_LEN_2048_BLKS;
1143 case MPI3_DEVICE0_FLAGS_MAX_WRITE_SAME_NO_LIMIT:
1149 if (tgtdev->starget && tgtdev->starget->hostdata) {
1150 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
1151 tgtdev->starget->hostdata;
1152 scsi_tgt_priv_data->perst_id = tgtdev->perst_id;
1153 scsi_tgt_priv_data->dev_handle = tgtdev->dev_handle;
1154 scsi_tgt_priv_data->dev_type = tgtdev->dev_type;
1155 scsi_tgt_priv_data->io_throttle_enabled =
1156 tgtdev->io_throttle_enabled;
1157 if (is_added == true)
1158 atomic_set(&scsi_tgt_priv_data->block_io, 0);
1159 scsi_tgt_priv_data->wslen = tgtdev->wslen;
1162 switch (dev_pg0->access_status) {
1163 case MPI3_DEVICE0_ASTATUS_NO_ERRORS:
1164 case MPI3_DEVICE0_ASTATUS_PREPARE:
1165 case MPI3_DEVICE0_ASTATUS_NEEDS_INITIALIZATION:
1166 case MPI3_DEVICE0_ASTATUS_DEVICE_MISSING_DELAY:
1169 tgtdev->is_hidden = 1;
1173 switch (tgtdev->dev_type) {
1174 case MPI3_DEVICE_DEVFORM_SAS_SATA:
1176 struct mpi3_device0_sas_sata_format *sasinf =
1177 &dev_pg0->device_specific.sas_sata_format;
1178 u16 dev_info = le16_to_cpu(sasinf->device_info);
1180 tgtdev->dev_spec.sas_sata_inf.dev_info = dev_info;
1181 tgtdev->dev_spec.sas_sata_inf.sas_address =
1182 le64_to_cpu(sasinf->sas_address);
1183 tgtdev->dev_spec.sas_sata_inf.phy_id = sasinf->phy_num;
1184 tgtdev->dev_spec.sas_sata_inf.attached_phy_id =
1185 sasinf->attached_phy_identifier;
1186 if ((dev_info & MPI3_SAS_DEVICE_INFO_DEVICE_TYPE_MASK) !=
1187 MPI3_SAS_DEVICE_INFO_DEVICE_TYPE_END_DEVICE)
1188 tgtdev->is_hidden = 1;
1189 else if (!(dev_info & (MPI3_SAS_DEVICE_INFO_STP_SATA_TARGET |
1190 MPI3_SAS_DEVICE_INFO_SSP_TARGET)))
1191 tgtdev->is_hidden = 1;
1193 if (((tgtdev->devpg0_flag &
1194 MPI3_DEVICE0_FLAGS_ATT_METHOD_DIR_ATTACHED)
1195 && (tgtdev->devpg0_flag &
1196 MPI3_DEVICE0_FLAGS_ATT_METHOD_VIRTUAL)) ||
1197 (tgtdev->parent_handle == 0xFFFF))
1198 tgtdev->non_stl = 1;
1199 if (tgtdev->dev_spec.sas_sata_inf.hba_port)
1200 tgtdev->dev_spec.sas_sata_inf.hba_port->port_id =
1201 dev_pg0->io_unit_port;
1204 case MPI3_DEVICE_DEVFORM_PCIE:
1206 struct mpi3_device0_pcie_format *pcieinf =
1207 &dev_pg0->device_specific.pcie_format;
1208 u16 dev_info = le16_to_cpu(pcieinf->device_info);
1210 tgtdev->dev_spec.pcie_inf.dev_info = dev_info;
1211 tgtdev->dev_spec.pcie_inf.capb =
1212 le32_to_cpu(pcieinf->capabilities);
1213 tgtdev->dev_spec.pcie_inf.mdts = MPI3MR_DEFAULT_MDTS;
1215 tgtdev->dev_spec.pcie_inf.pgsz = 12;
1216 if (dev_pg0->access_status == MPI3_DEVICE0_ASTATUS_NO_ERRORS) {
1217 tgtdev->dev_spec.pcie_inf.mdts =
1218 le32_to_cpu(pcieinf->maximum_data_transfer_size);
1219 tgtdev->dev_spec.pcie_inf.pgsz = pcieinf->page_size;
1220 tgtdev->dev_spec.pcie_inf.reset_to =
1221 max_t(u8, pcieinf->controller_reset_to,
1222 MPI3MR_INTADMCMD_TIMEOUT);
1223 tgtdev->dev_spec.pcie_inf.abort_to =
1224 max_t(u8, pcieinf->nvme_abort_to,
1225 MPI3MR_INTADMCMD_TIMEOUT);
1227 if (tgtdev->dev_spec.pcie_inf.mdts > (1024 * 1024))
1228 tgtdev->dev_spec.pcie_inf.mdts = (1024 * 1024);
1229 if (((dev_info & MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) !=
1230 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) &&
1231 ((dev_info & MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) !=
1232 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_SCSI_DEVICE))
1233 tgtdev->is_hidden = 1;
1234 tgtdev->non_stl = 1;
1237 prot_mask = scsi_host_get_prot(mrioc->shost);
1238 if (prot_mask & SHOST_DIX_TYPE0_PROTECTION) {
1239 scsi_host_set_prot(mrioc->shost, prot_mask & 0x77);
1241 "%s : Disabling DIX0 prot capability\n", __func__);
1243 "because HBA does not support DIX0 operation on NVME drives\n");
1247 case MPI3_DEVICE_DEVFORM_VD:
1249 struct mpi3_device0_vd_format *vdinf =
1250 &dev_pg0->device_specific.vd_format;
1251 struct mpi3mr_throttle_group_info *tg = NULL;
1252 u16 vdinf_io_throttle_group =
1253 le16_to_cpu(vdinf->io_throttle_group);
1255 tgtdev->dev_spec.vd_inf.state = vdinf->vd_state;
1256 if (vdinf->vd_state == MPI3_DEVICE0_VD_STATE_OFFLINE)
1257 tgtdev->is_hidden = 1;
1258 tgtdev->non_stl = 1;
1259 tgtdev->dev_spec.vd_inf.tg_id = vdinf_io_throttle_group;
1260 tgtdev->dev_spec.vd_inf.tg_high =
1261 le16_to_cpu(vdinf->io_throttle_group_high) * 2048;
1262 tgtdev->dev_spec.vd_inf.tg_low =
1263 le16_to_cpu(vdinf->io_throttle_group_low) * 2048;
1264 if (vdinf_io_throttle_group < mrioc->num_io_throttle_group) {
1265 tg = mrioc->throttle_groups + vdinf_io_throttle_group;
1266 tg->id = vdinf_io_throttle_group;
1267 tg->high = tgtdev->dev_spec.vd_inf.tg_high;
1268 tg->low = tgtdev->dev_spec.vd_inf.tg_low;
1270 tgtdev->dev_spec.vd_inf.tg_qd_reduction;
1271 if (is_added == true)
1272 tg->fw_qd = tgtdev->q_depth;
1273 tg->modified_qd = tgtdev->q_depth;
1275 tgtdev->dev_spec.vd_inf.tg = tg;
1276 if (scsi_tgt_priv_data)
1277 scsi_tgt_priv_data->throttle_group = tg;
1286 * mpi3mr_devstatuschg_evt_bh - DevStatusChange evt bottomhalf
1287 * @mrioc: Adapter instance reference
1288 * @fwevt: Firmware event information.
1290 * Process Device status Change event and based on device's new
1291 * information, either expose the device to the upper layers, or
1292 * remove the device from upper layers.
1296 static void mpi3mr_devstatuschg_evt_bh(struct mpi3mr_ioc *mrioc,
1297 struct mpi3mr_fwevt *fwevt)
1300 u8 uhide = 0, delete = 0, cleanup = 0;
1301 struct mpi3mr_tgt_dev *tgtdev = NULL;
1302 struct mpi3_event_data_device_status_change *evtdata =
1303 (struct mpi3_event_data_device_status_change *)fwevt->event_data;
1305 dev_handle = le16_to_cpu(evtdata->dev_handle);
1307 "%s :device status change: handle(0x%04x): reason code(0x%x)\n",
1308 __func__, dev_handle, evtdata->reason_code);
1309 switch (evtdata->reason_code) {
1310 case MPI3_EVENT_DEV_STAT_RC_HIDDEN:
1313 case MPI3_EVENT_DEV_STAT_RC_NOT_HIDDEN:
1316 case MPI3_EVENT_DEV_STAT_RC_VD_NOT_RESPONDING:
1321 ioc_info(mrioc, "%s :Unhandled reason code(0x%x)\n", __func__,
1322 evtdata->reason_code);
1326 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
1330 tgtdev->is_hidden = 0;
1331 if (!tgtdev->host_exposed)
1332 mpi3mr_report_tgtdev_to_host(mrioc, tgtdev->perst_id);
1336 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1339 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, false);
1340 mpi3mr_tgtdev_put(tgtdev);
1345 mpi3mr_tgtdev_put(tgtdev);
1349 * mpi3mr_devinfochg_evt_bh - DeviceInfoChange evt bottomhalf
1350 * @mrioc: Adapter instance reference
1351 * @dev_pg0: New device page0
1353 * Process Device Info Change event and based on device's new
1354 * information, either expose the device to the upper layers, or
1355 * remove the device from upper layers or update the details of
1360 static void mpi3mr_devinfochg_evt_bh(struct mpi3mr_ioc *mrioc,
1361 struct mpi3_device_page0 *dev_pg0)
1363 struct mpi3mr_tgt_dev *tgtdev = NULL;
1364 u16 dev_handle = 0, perst_id = 0;
1366 perst_id = le16_to_cpu(dev_pg0->persistent_id);
1367 dev_handle = le16_to_cpu(dev_pg0->dev_handle);
1369 "%s :Device info change: handle(0x%04x): persist_id(0x%x)\n",
1370 __func__, dev_handle, perst_id);
1371 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
1374 mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0, false);
1375 if (!tgtdev->is_hidden && !tgtdev->host_exposed)
1376 mpi3mr_report_tgtdev_to_host(mrioc, perst_id);
1377 if (tgtdev->is_hidden && tgtdev->host_exposed)
1378 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1379 if (!tgtdev->is_hidden && tgtdev->host_exposed && tgtdev->starget)
1380 starget_for_each_device(tgtdev->starget, (void *)tgtdev,
1381 mpi3mr_update_sdev);
1384 mpi3mr_tgtdev_put(tgtdev);
1388 * mpi3mr_free_enclosure_list - release enclosures
1389 * @mrioc: Adapter instance reference
1391 * Free memory allocated during encloure add.
1395 void mpi3mr_free_enclosure_list(struct mpi3mr_ioc *mrioc)
1397 struct mpi3mr_enclosure_node *enclosure_dev, *enclosure_dev_next;
1399 list_for_each_entry_safe(enclosure_dev,
1400 enclosure_dev_next, &mrioc->enclosure_list, list) {
1401 list_del(&enclosure_dev->list);
1402 kfree(enclosure_dev);
1407 * mpi3mr_enclosure_find_by_handle - enclosure search by handle
1408 * @mrioc: Adapter instance reference
1409 * @handle: Firmware device handle of the enclosure
1411 * This searches for enclosure device based on handle, then returns the
1414 * Return: Enclosure object reference or NULL
1416 struct mpi3mr_enclosure_node *mpi3mr_enclosure_find_by_handle(
1417 struct mpi3mr_ioc *mrioc, u16 handle)
1419 struct mpi3mr_enclosure_node *enclosure_dev, *r = NULL;
1421 list_for_each_entry(enclosure_dev, &mrioc->enclosure_list, list) {
1422 if (le16_to_cpu(enclosure_dev->pg0.enclosure_handle) != handle)
1432 * mpi3mr_encldev_add_chg_evt_debug - debug for enclosure event
1433 * @mrioc: Adapter instance reference
1434 * @encl_pg0: Enclosure page 0.
1435 * @is_added: Added event or not
1439 static void mpi3mr_encldev_add_chg_evt_debug(struct mpi3mr_ioc *mrioc,
1440 struct mpi3_enclosure_page0 *encl_pg0, u8 is_added)
1442 char *reason_str = NULL;
1444 if (!(mrioc->logging_level & MPI3_DEBUG_EVENT_WORK_TASK))
1448 reason_str = "enclosure added";
1450 reason_str = "enclosure dev status changed";
1453 "%s: handle(0x%04x), enclosure logical id(0x%016llx)\n",
1454 reason_str, le16_to_cpu(encl_pg0->enclosure_handle),
1455 (unsigned long long)le64_to_cpu(encl_pg0->enclosure_logical_id));
1457 "number of slots(%d), port(%d), flags(0x%04x), present(%d)\n",
1458 le16_to_cpu(encl_pg0->num_slots), encl_pg0->io_unit_port,
1459 le16_to_cpu(encl_pg0->flags),
1460 ((le16_to_cpu(encl_pg0->flags) &
1461 MPI3_ENCLS0_FLAGS_ENCL_DEV_PRESENT_MASK) >> 4));
1465 * mpi3mr_encldev_add_chg_evt_bh - Enclosure evt bottomhalf
1466 * @mrioc: Adapter instance reference
1467 * @fwevt: Firmware event reference
1469 * Prints information about the Enclosure device status or
1470 * Enclosure add events if logging is enabled and add or remove
1471 * the enclosure from the controller's internal list of
1476 static void mpi3mr_encldev_add_chg_evt_bh(struct mpi3mr_ioc *mrioc,
1477 struct mpi3mr_fwevt *fwevt)
1479 struct mpi3mr_enclosure_node *enclosure_dev = NULL;
1480 struct mpi3_enclosure_page0 *encl_pg0;
1484 encl_pg0 = (struct mpi3_enclosure_page0 *) fwevt->event_data;
1485 added = (fwevt->event_id == MPI3_EVENT_ENCL_DEVICE_ADDED) ? 1 : 0;
1486 mpi3mr_encldev_add_chg_evt_debug(mrioc, encl_pg0, added);
1489 encl_handle = le16_to_cpu(encl_pg0->enclosure_handle);
1490 present = ((le16_to_cpu(encl_pg0->flags) &
1491 MPI3_ENCLS0_FLAGS_ENCL_DEV_PRESENT_MASK) >> 4);
1494 enclosure_dev = mpi3mr_enclosure_find_by_handle(mrioc,
1496 if (!enclosure_dev && present) {
1498 kzalloc(sizeof(struct mpi3mr_enclosure_node),
1502 list_add_tail(&enclosure_dev->list,
1503 &mrioc->enclosure_list);
1505 if (enclosure_dev) {
1507 list_del(&enclosure_dev->list);
1508 kfree(enclosure_dev);
1510 memcpy(&enclosure_dev->pg0, encl_pg0,
1511 sizeof(enclosure_dev->pg0));
1517 * mpi3mr_sastopochg_evt_debug - SASTopoChange details
1518 * @mrioc: Adapter instance reference
1519 * @event_data: SAS topology change list event data
1521 * Prints information about the SAS topology change event.
1526 mpi3mr_sastopochg_evt_debug(struct mpi3mr_ioc *mrioc,
1527 struct mpi3_event_data_sas_topology_change_list *event_data)
1531 u8 reason_code, phy_number;
1532 char *status_str = NULL;
1533 u8 link_rate, prev_link_rate;
1535 switch (event_data->exp_status) {
1536 case MPI3_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
1537 status_str = "remove";
1539 case MPI3_EVENT_SAS_TOPO_ES_RESPONDING:
1540 status_str = "responding";
1542 case MPI3_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
1543 status_str = "remove delay";
1545 case MPI3_EVENT_SAS_TOPO_ES_NO_EXPANDER:
1546 status_str = "direct attached";
1549 status_str = "unknown status";
1552 ioc_info(mrioc, "%s :sas topology change: (%s)\n",
1553 __func__, status_str);
1555 "%s :\texpander_handle(0x%04x), port(%d), enclosure_handle(0x%04x) start_phy(%02d), num_entries(%d)\n",
1556 __func__, le16_to_cpu(event_data->expander_dev_handle),
1557 event_data->io_unit_port,
1558 le16_to_cpu(event_data->enclosure_handle),
1559 event_data->start_phy_num, event_data->num_entries);
1560 for (i = 0; i < event_data->num_entries; i++) {
1561 handle = le16_to_cpu(event_data->phy_entry[i].attached_dev_handle);
1564 phy_number = event_data->start_phy_num + i;
1565 reason_code = event_data->phy_entry[i].status &
1566 MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
1567 switch (reason_code) {
1568 case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
1569 status_str = "target remove";
1571 case MPI3_EVENT_SAS_TOPO_PHY_RC_DELAY_NOT_RESPONDING:
1572 status_str = "delay target remove";
1574 case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
1575 status_str = "link status change";
1577 case MPI3_EVENT_SAS_TOPO_PHY_RC_NO_CHANGE:
1578 status_str = "link status no change";
1580 case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
1581 status_str = "target responding";
1584 status_str = "unknown";
1587 link_rate = event_data->phy_entry[i].link_rate >> 4;
1588 prev_link_rate = event_data->phy_entry[i].link_rate & 0xF;
1590 "%s :\tphy(%02d), attached_handle(0x%04x): %s: link rate: new(0x%02x), old(0x%02x)\n",
1591 __func__, phy_number, handle, status_str, link_rate,
1597 * mpi3mr_sastopochg_evt_bh - SASTopologyChange evt bottomhalf
1598 * @mrioc: Adapter instance reference
1599 * @fwevt: Firmware event reference
1601 * Prints information about the SAS topology change event and
1602 * for "not responding" event code, removes the device from the
1607 static void mpi3mr_sastopochg_evt_bh(struct mpi3mr_ioc *mrioc,
1608 struct mpi3mr_fwevt *fwevt)
1610 struct mpi3_event_data_sas_topology_change_list *event_data =
1611 (struct mpi3_event_data_sas_topology_change_list *)fwevt->event_data;
1615 u64 exp_sas_address = 0, parent_sas_address = 0;
1616 struct mpi3mr_hba_port *hba_port = NULL;
1617 struct mpi3mr_tgt_dev *tgtdev = NULL;
1618 struct mpi3mr_sas_node *sas_expander = NULL;
1619 unsigned long flags;
1620 u8 link_rate, prev_link_rate, parent_phy_number;
1622 mpi3mr_sastopochg_evt_debug(mrioc, event_data);
1623 if (mrioc->sas_transport_enabled) {
1624 hba_port = mpi3mr_get_hba_port_by_id(mrioc,
1625 event_data->io_unit_port);
1626 if (le16_to_cpu(event_data->expander_dev_handle)) {
1627 spin_lock_irqsave(&mrioc->sas_node_lock, flags);
1628 sas_expander = __mpi3mr_expander_find_by_handle(mrioc,
1629 le16_to_cpu(event_data->expander_dev_handle));
1631 exp_sas_address = sas_expander->sas_address;
1632 hba_port = sas_expander->hba_port;
1634 spin_unlock_irqrestore(&mrioc->sas_node_lock, flags);
1635 parent_sas_address = exp_sas_address;
1637 parent_sas_address = mrioc->sas_hba.sas_address;
1640 for (i = 0; i < event_data->num_entries; i++) {
1643 handle = le16_to_cpu(event_data->phy_entry[i].attached_dev_handle);
1646 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
1650 reason_code = event_data->phy_entry[i].status &
1651 MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
1653 switch (reason_code) {
1654 case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
1655 if (tgtdev->host_exposed)
1656 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1657 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, false);
1658 mpi3mr_tgtdev_put(tgtdev);
1660 case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
1661 case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
1662 case MPI3_EVENT_SAS_TOPO_PHY_RC_NO_CHANGE:
1664 if (!mrioc->sas_transport_enabled || tgtdev->non_stl
1665 || tgtdev->is_hidden)
1667 link_rate = event_data->phy_entry[i].link_rate >> 4;
1668 prev_link_rate = event_data->phy_entry[i].link_rate & 0xF;
1669 if (link_rate == prev_link_rate)
1671 if (!parent_sas_address)
1673 parent_phy_number = event_data->start_phy_num + i;
1674 mpi3mr_update_links(mrioc, parent_sas_address, handle,
1675 parent_phy_number, link_rate, hba_port);
1682 mpi3mr_tgtdev_put(tgtdev);
1685 if (mrioc->sas_transport_enabled && (event_data->exp_status ==
1686 MPI3_EVENT_SAS_TOPO_ES_NOT_RESPONDING)) {
1688 mpi3mr_expander_remove(mrioc, exp_sas_address,
1694 * mpi3mr_pcietopochg_evt_debug - PCIeTopoChange details
1695 * @mrioc: Adapter instance reference
1696 * @event_data: PCIe topology change list event data
1698 * Prints information about the PCIe topology change event.
1703 mpi3mr_pcietopochg_evt_debug(struct mpi3mr_ioc *mrioc,
1704 struct mpi3_event_data_pcie_topology_change_list *event_data)
1710 char *status_str = NULL;
1711 u8 link_rate, prev_link_rate;
1713 switch (event_data->switch_status) {
1714 case MPI3_EVENT_PCIE_TOPO_SS_NOT_RESPONDING:
1715 status_str = "remove";
1717 case MPI3_EVENT_PCIE_TOPO_SS_RESPONDING:
1718 status_str = "responding";
1720 case MPI3_EVENT_PCIE_TOPO_SS_DELAY_NOT_RESPONDING:
1721 status_str = "remove delay";
1723 case MPI3_EVENT_PCIE_TOPO_SS_NO_PCIE_SWITCH:
1724 status_str = "direct attached";
1727 status_str = "unknown status";
1730 ioc_info(mrioc, "%s :pcie topology change: (%s)\n",
1731 __func__, status_str);
1733 "%s :\tswitch_handle(0x%04x), enclosure_handle(0x%04x) start_port(%02d), num_entries(%d)\n",
1734 __func__, le16_to_cpu(event_data->switch_dev_handle),
1735 le16_to_cpu(event_data->enclosure_handle),
1736 event_data->start_port_num, event_data->num_entries);
1737 for (i = 0; i < event_data->num_entries; i++) {
1739 le16_to_cpu(event_data->port_entry[i].attached_dev_handle);
1742 port_number = event_data->start_port_num + i;
1743 reason_code = event_data->port_entry[i].port_status;
1744 switch (reason_code) {
1745 case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
1746 status_str = "target remove";
1748 case MPI3_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
1749 status_str = "delay target remove";
1751 case MPI3_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
1752 status_str = "link status change";
1754 case MPI3_EVENT_PCIE_TOPO_PS_NO_CHANGE:
1755 status_str = "link status no change";
1757 case MPI3_EVENT_PCIE_TOPO_PS_RESPONDING:
1758 status_str = "target responding";
1761 status_str = "unknown";
1764 link_rate = event_data->port_entry[i].current_port_info &
1765 MPI3_EVENT_PCIE_TOPO_PI_RATE_MASK;
1766 prev_link_rate = event_data->port_entry[i].previous_port_info &
1767 MPI3_EVENT_PCIE_TOPO_PI_RATE_MASK;
1769 "%s :\tport(%02d), attached_handle(0x%04x): %s: link rate: new(0x%02x), old(0x%02x)\n",
1770 __func__, port_number, handle, status_str, link_rate,
1776 * mpi3mr_pcietopochg_evt_bh - PCIeTopologyChange evt bottomhalf
1777 * @mrioc: Adapter instance reference
1778 * @fwevt: Firmware event reference
1780 * Prints information about the PCIe topology change event and
1781 * for "not responding" event code, removes the device from the
1786 static void mpi3mr_pcietopochg_evt_bh(struct mpi3mr_ioc *mrioc,
1787 struct mpi3mr_fwevt *fwevt)
1789 struct mpi3_event_data_pcie_topology_change_list *event_data =
1790 (struct mpi3_event_data_pcie_topology_change_list *)fwevt->event_data;
1794 struct mpi3mr_tgt_dev *tgtdev = NULL;
1796 mpi3mr_pcietopochg_evt_debug(mrioc, event_data);
1798 for (i = 0; i < event_data->num_entries; i++) {
1802 le16_to_cpu(event_data->port_entry[i].attached_dev_handle);
1805 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
1809 reason_code = event_data->port_entry[i].port_status;
1811 switch (reason_code) {
1812 case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
1813 if (tgtdev->host_exposed)
1814 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1815 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, false);
1816 mpi3mr_tgtdev_put(tgtdev);
1822 mpi3mr_tgtdev_put(tgtdev);
1827 * mpi3mr_logdata_evt_bh - Log data event bottomhalf
1828 * @mrioc: Adapter instance reference
1829 * @fwevt: Firmware event reference
1831 * Extracts the event data and calls application interfacing
1832 * function to process the event further.
1836 static void mpi3mr_logdata_evt_bh(struct mpi3mr_ioc *mrioc,
1837 struct mpi3mr_fwevt *fwevt)
1839 mpi3mr_app_save_logdata(mrioc, fwevt->event_data,
1840 fwevt->event_data_size);
1844 * mpi3mr_update_sdev_qd - Update SCSI device queue depath
1845 * @sdev: SCSI device reference
1846 * @data: Queue depth reference
1848 * This is an iterator function called for each SCSI device in a
1849 * target to update the QD of each SCSI device.
1853 static void mpi3mr_update_sdev_qd(struct scsi_device *sdev, void *data)
1855 u16 *q_depth = (u16 *)data;
1857 scsi_change_queue_depth(sdev, (int)*q_depth);
1858 sdev->max_queue_depth = sdev->queue_depth;
1862 * mpi3mr_set_qd_for_all_vd_in_tg -set QD for TG VDs
1863 * @mrioc: Adapter instance reference
1864 * @tg: Throttle group information pointer
1866 * Accessor to reduce QD for each device associated with the
1867 * given throttle group.
1871 static void mpi3mr_set_qd_for_all_vd_in_tg(struct mpi3mr_ioc *mrioc,
1872 struct mpi3mr_throttle_group_info *tg)
1874 unsigned long flags;
1875 struct mpi3mr_tgt_dev *tgtdev;
1876 struct mpi3mr_stgt_priv_data *tgt_priv;
1879 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
1880 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
1881 if (tgtdev->starget && tgtdev->starget->hostdata) {
1882 tgt_priv = tgtdev->starget->hostdata;
1883 if (tgt_priv->throttle_group == tg) {
1884 dprint_event_bh(mrioc,
1885 "updating qd due to throttling for persist_id(%d) original_qd(%d), reduced_qd (%d)\n",
1886 tgt_priv->perst_id, tgtdev->q_depth,
1888 starget_for_each_device(tgtdev->starget,
1889 (void *)&tg->modified_qd,
1890 mpi3mr_update_sdev_qd);
1894 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
1898 * mpi3mr_fwevt_bh - Firmware event bottomhalf handler
1899 * @mrioc: Adapter instance reference
1900 * @fwevt: Firmware event reference
1902 * Identifies the firmware event and calls corresponding bottomg
1903 * half handler and sends event acknowledgment if required.
1907 static void mpi3mr_fwevt_bh(struct mpi3mr_ioc *mrioc,
1908 struct mpi3mr_fwevt *fwevt)
1910 struct mpi3_device_page0 *dev_pg0 = NULL;
1911 u16 perst_id, handle, dev_info;
1912 struct mpi3_device0_sas_sata_format *sasinf = NULL;
1914 mpi3mr_fwevt_del_from_list(mrioc, fwevt);
1915 mrioc->current_event = fwevt;
1917 if (mrioc->stop_drv_processing)
1920 if (mrioc->unrecoverable) {
1921 dprint_event_bh(mrioc,
1922 "ignoring event(0x%02x) in bottom half handler due to unrecoverable controller\n",
1927 if (!fwevt->process_evt)
1930 switch (fwevt->event_id) {
1931 case MPI3_EVENT_DEVICE_ADDED:
1933 dev_pg0 = (struct mpi3_device_page0 *)fwevt->event_data;
1934 perst_id = le16_to_cpu(dev_pg0->persistent_id);
1935 handle = le16_to_cpu(dev_pg0->dev_handle);
1936 if (perst_id != MPI3_DEVICE0_PERSISTENTID_INVALID)
1937 mpi3mr_report_tgtdev_to_host(mrioc, perst_id);
1938 else if (mrioc->sas_transport_enabled &&
1939 (dev_pg0->device_form == MPI3_DEVICE_DEVFORM_SAS_SATA)) {
1940 sasinf = &dev_pg0->device_specific.sas_sata_format;
1941 dev_info = le16_to_cpu(sasinf->device_info);
1942 if (!mrioc->sas_hba.num_phys)
1943 mpi3mr_sas_host_add(mrioc);
1945 mpi3mr_sas_host_refresh(mrioc);
1947 if (mpi3mr_is_expander_device(dev_info))
1948 mpi3mr_expander_add(mrioc, handle);
1952 case MPI3_EVENT_DEVICE_INFO_CHANGED:
1954 dev_pg0 = (struct mpi3_device_page0 *)fwevt->event_data;
1955 perst_id = le16_to_cpu(dev_pg0->persistent_id);
1956 if (perst_id != MPI3_DEVICE0_PERSISTENTID_INVALID)
1957 mpi3mr_devinfochg_evt_bh(mrioc, dev_pg0);
1960 case MPI3_EVENT_DEVICE_STATUS_CHANGE:
1962 mpi3mr_devstatuschg_evt_bh(mrioc, fwevt);
1965 case MPI3_EVENT_ENCL_DEVICE_ADDED:
1966 case MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE:
1968 mpi3mr_encldev_add_chg_evt_bh(mrioc, fwevt);
1972 case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
1974 mpi3mr_sastopochg_evt_bh(mrioc, fwevt);
1977 case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
1979 mpi3mr_pcietopochg_evt_bh(mrioc, fwevt);
1982 case MPI3_EVENT_LOG_DATA:
1984 mpi3mr_logdata_evt_bh(mrioc, fwevt);
1987 case MPI3MR_DRIVER_EVENT_TG_QD_REDUCTION:
1989 struct mpi3mr_throttle_group_info *tg;
1991 tg = *(struct mpi3mr_throttle_group_info **)fwevt->event_data;
1992 dprint_event_bh(mrioc,
1993 "qd reduction event processed for tg_id(%d) reduction_needed(%d)\n",
1994 tg->id, tg->need_qd_reduction);
1995 if (tg->need_qd_reduction) {
1996 mpi3mr_set_qd_for_all_vd_in_tg(mrioc, tg);
1997 tg->need_qd_reduction = 0;
2001 case MPI3_EVENT_WAIT_FOR_DEVICES_TO_REFRESH:
2003 while (mrioc->device_refresh_on)
2006 dprint_event_bh(mrioc,
2007 "scan for non responding and newly added devices after soft reset started\n");
2008 if (mrioc->sas_transport_enabled) {
2009 mpi3mr_refresh_sas_ports(mrioc);
2010 mpi3mr_refresh_expanders(mrioc);
2012 mpi3mr_rfresh_tgtdevs(mrioc);
2014 "scan for non responding and newly added devices after soft reset completed\n");
2022 if (fwevt->send_ack)
2023 mpi3mr_process_event_ack(mrioc, fwevt->event_id,
2026 /* Put fwevt reference count to neutralize kref_init increment */
2027 mpi3mr_fwevt_put(fwevt);
2028 mrioc->current_event = NULL;
2032 * mpi3mr_fwevt_worker - Firmware event worker
2033 * @work: Work struct containing firmware event
2035 * Extracts the firmware event and calls mpi3mr_fwevt_bh.
2039 static void mpi3mr_fwevt_worker(struct work_struct *work)
2041 struct mpi3mr_fwevt *fwevt = container_of(work, struct mpi3mr_fwevt,
2043 mpi3mr_fwevt_bh(fwevt->mrioc, fwevt);
2045 * Put fwevt reference count after
2046 * dequeuing it from worker queue
2048 mpi3mr_fwevt_put(fwevt);
2052 * mpi3mr_create_tgtdev - Create and add a target device
2053 * @mrioc: Adapter instance reference
2054 * @dev_pg0: Device Page 0 data
2056 * If the device specified by the device page 0 data is not
2057 * present in the driver's internal list, allocate the memory
2058 * for the device, populate the data and add to the list, else
2059 * update the device data. The key is persistent ID.
2061 * Return: 0 on success, -ENOMEM on memory allocation failure
2063 static int mpi3mr_create_tgtdev(struct mpi3mr_ioc *mrioc,
2064 struct mpi3_device_page0 *dev_pg0)
2067 struct mpi3mr_tgt_dev *tgtdev = NULL;
2069 unsigned long flags;
2071 perst_id = le16_to_cpu(dev_pg0->persistent_id);
2072 if (perst_id == MPI3_DEVICE0_PERSISTENTID_INVALID)
2075 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
2076 tgtdev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, perst_id);
2078 tgtdev->state = MPI3MR_DEV_CREATED;
2079 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
2082 mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0, true);
2083 mpi3mr_tgtdev_put(tgtdev);
2085 tgtdev = mpi3mr_alloc_tgtdev();
2088 mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0, true);
2089 mpi3mr_tgtdev_add_to_list(mrioc, tgtdev);
2096 * mpi3mr_flush_delayed_cmd_lists - Flush pending commands
2097 * @mrioc: Adapter instance reference
2099 * Flush pending commands in the delayed lists due to a
2100 * controller reset or driver removal as a cleanup.
2104 void mpi3mr_flush_delayed_cmd_lists(struct mpi3mr_ioc *mrioc)
2106 struct delayed_dev_rmhs_node *_rmhs_node;
2107 struct delayed_evt_ack_node *_evtack_node;
2109 dprint_reset(mrioc, "flushing delayed dev_remove_hs commands\n");
2110 while (!list_empty(&mrioc->delayed_rmhs_list)) {
2111 _rmhs_node = list_entry(mrioc->delayed_rmhs_list.next,
2112 struct delayed_dev_rmhs_node, list);
2113 list_del(&_rmhs_node->list);
2116 dprint_reset(mrioc, "flushing delayed event ack commands\n");
2117 while (!list_empty(&mrioc->delayed_evtack_cmds_list)) {
2118 _evtack_node = list_entry(mrioc->delayed_evtack_cmds_list.next,
2119 struct delayed_evt_ack_node, list);
2120 list_del(&_evtack_node->list);
2121 kfree(_evtack_node);
2126 * mpi3mr_dev_rmhs_complete_iou - Device removal IOUC completion
2127 * @mrioc: Adapter instance reference
2128 * @drv_cmd: Internal command tracker
2130 * Issues a target reset TM to the firmware from the device
2131 * removal TM pend list or retry the removal handshake sequence
2132 * based on the IOU control request IOC status.
2136 static void mpi3mr_dev_rmhs_complete_iou(struct mpi3mr_ioc *mrioc,
2137 struct mpi3mr_drv_cmd *drv_cmd)
2139 u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
2140 struct delayed_dev_rmhs_node *delayed_dev_rmhs = NULL;
2142 if (drv_cmd->state & MPI3MR_CMD_RESET)
2146 "%s :dev_rmhs_iouctrl_complete:handle(0x%04x), ioc_status(0x%04x), loginfo(0x%08x)\n",
2147 __func__, drv_cmd->dev_handle, drv_cmd->ioc_status,
2148 drv_cmd->ioc_loginfo);
2149 if (drv_cmd->ioc_status != MPI3_IOCSTATUS_SUCCESS) {
2150 if (drv_cmd->retry_count < MPI3MR_DEV_RMHS_RETRY_COUNT) {
2151 drv_cmd->retry_count++;
2153 "%s :dev_rmhs_iouctrl_complete: handle(0x%04x)retrying handshake retry=%d\n",
2154 __func__, drv_cmd->dev_handle,
2155 drv_cmd->retry_count);
2156 mpi3mr_dev_rmhs_send_tm(mrioc, drv_cmd->dev_handle,
2157 drv_cmd, drv_cmd->iou_rc);
2161 "%s :dev removal handshake failed after all retries: handle(0x%04x)\n",
2162 __func__, drv_cmd->dev_handle);
2165 "%s :dev removal handshake completed successfully: handle(0x%04x)\n",
2166 __func__, drv_cmd->dev_handle);
2167 clear_bit(drv_cmd->dev_handle, mrioc->removepend_bitmap);
2170 if (!list_empty(&mrioc->delayed_rmhs_list)) {
2171 delayed_dev_rmhs = list_entry(mrioc->delayed_rmhs_list.next,
2172 struct delayed_dev_rmhs_node, list);
2173 drv_cmd->dev_handle = delayed_dev_rmhs->handle;
2174 drv_cmd->retry_count = 0;
2175 drv_cmd->iou_rc = delayed_dev_rmhs->iou_rc;
2177 "%s :dev_rmhs_iouctrl_complete: processing delayed TM: handle(0x%04x)\n",
2178 __func__, drv_cmd->dev_handle);
2179 mpi3mr_dev_rmhs_send_tm(mrioc, drv_cmd->dev_handle, drv_cmd,
2181 list_del(&delayed_dev_rmhs->list);
2182 kfree(delayed_dev_rmhs);
2187 drv_cmd->state = MPI3MR_CMD_NOTUSED;
2188 drv_cmd->callback = NULL;
2189 drv_cmd->retry_count = 0;
2190 drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
2191 clear_bit(cmd_idx, mrioc->devrem_bitmap);
2195 * mpi3mr_dev_rmhs_complete_tm - Device removal TM completion
2196 * @mrioc: Adapter instance reference
2197 * @drv_cmd: Internal command tracker
2199 * Issues a target reset TM to the firmware from the device
2200 * removal TM pend list or issue IO unit control request as
2201 * part of device removal or hidden acknowledgment handshake.
2205 static void mpi3mr_dev_rmhs_complete_tm(struct mpi3mr_ioc *mrioc,
2206 struct mpi3mr_drv_cmd *drv_cmd)
2208 struct mpi3_iounit_control_request iou_ctrl;
2209 u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
2210 struct mpi3_scsi_task_mgmt_reply *tm_reply = NULL;
2213 if (drv_cmd->state & MPI3MR_CMD_RESET)
2216 if (drv_cmd->state & MPI3MR_CMD_REPLY_VALID)
2217 tm_reply = (struct mpi3_scsi_task_mgmt_reply *)drv_cmd->reply;
2221 "dev_rmhs_tr_complete:handle(0x%04x), ioc_status(0x%04x), loginfo(0x%08x), term_count(%d)\n",
2222 mrioc->name, drv_cmd->dev_handle, drv_cmd->ioc_status,
2223 drv_cmd->ioc_loginfo,
2224 le32_to_cpu(tm_reply->termination_count));
2226 pr_info(IOCNAME "Issuing IOU CTL: handle(0x%04x) dev_rmhs idx(%d)\n",
2227 mrioc->name, drv_cmd->dev_handle, cmd_idx);
2229 memset(&iou_ctrl, 0, sizeof(iou_ctrl));
2231 drv_cmd->state = MPI3MR_CMD_PENDING;
2232 drv_cmd->is_waiting = 0;
2233 drv_cmd->callback = mpi3mr_dev_rmhs_complete_iou;
2234 iou_ctrl.operation = drv_cmd->iou_rc;
2235 iou_ctrl.param16[0] = cpu_to_le16(drv_cmd->dev_handle);
2236 iou_ctrl.host_tag = cpu_to_le16(drv_cmd->host_tag);
2237 iou_ctrl.function = MPI3_FUNCTION_IO_UNIT_CONTROL;
2239 retval = mpi3mr_admin_request_post(mrioc, &iou_ctrl, sizeof(iou_ctrl),
2242 pr_err(IOCNAME "Issue DevRmHsTMIOUCTL: Admin post failed\n",
2249 drv_cmd->state = MPI3MR_CMD_NOTUSED;
2250 drv_cmd->callback = NULL;
2251 drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
2252 drv_cmd->retry_count = 0;
2253 clear_bit(cmd_idx, mrioc->devrem_bitmap);
2257 * mpi3mr_dev_rmhs_send_tm - Issue TM for device removal
2258 * @mrioc: Adapter instance reference
2259 * @handle: Device handle
2260 * @cmdparam: Internal command tracker
2261 * @iou_rc: IO unit reason code
2263 * Issues a target reset TM to the firmware or add it to a pend
2264 * list as part of device removal or hidden acknowledgment
2269 static void mpi3mr_dev_rmhs_send_tm(struct mpi3mr_ioc *mrioc, u16 handle,
2270 struct mpi3mr_drv_cmd *cmdparam, u8 iou_rc)
2272 struct mpi3_scsi_task_mgmt_request tm_req;
2274 u16 cmd_idx = MPI3MR_NUM_DEVRMCMD;
2276 struct mpi3mr_drv_cmd *drv_cmd = cmdparam;
2277 struct delayed_dev_rmhs_node *delayed_dev_rmhs = NULL;
2278 struct mpi3mr_tgt_dev *tgtdev = NULL;
2279 unsigned long flags;
2281 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
2282 tgtdev = __mpi3mr_get_tgtdev_by_handle(mrioc, handle);
2283 if (tgtdev && (iou_rc == MPI3_CTRL_OP_REMOVE_DEVICE))
2284 tgtdev->state = MPI3MR_DEV_REMOVE_HS_STARTED;
2285 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
2290 cmd_idx = find_first_zero_bit(mrioc->devrem_bitmap,
2291 MPI3MR_NUM_DEVRMCMD);
2292 if (cmd_idx < MPI3MR_NUM_DEVRMCMD) {
2293 if (!test_and_set_bit(cmd_idx, mrioc->devrem_bitmap))
2295 cmd_idx = MPI3MR_NUM_DEVRMCMD;
2297 } while (retrycount--);
2299 if (cmd_idx >= MPI3MR_NUM_DEVRMCMD) {
2300 delayed_dev_rmhs = kzalloc(sizeof(*delayed_dev_rmhs),
2302 if (!delayed_dev_rmhs)
2304 INIT_LIST_HEAD(&delayed_dev_rmhs->list);
2305 delayed_dev_rmhs->handle = handle;
2306 delayed_dev_rmhs->iou_rc = iou_rc;
2307 list_add_tail(&delayed_dev_rmhs->list,
2308 &mrioc->delayed_rmhs_list);
2309 ioc_info(mrioc, "%s :DevRmHs: tr:handle(0x%04x) is postponed\n",
2313 drv_cmd = &mrioc->dev_rmhs_cmds[cmd_idx];
2316 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
2318 "%s :Issuing TR TM: for devhandle 0x%04x with dev_rmhs %d\n",
2319 __func__, handle, cmd_idx);
2321 memset(&tm_req, 0, sizeof(tm_req));
2322 if (drv_cmd->state & MPI3MR_CMD_PENDING) {
2323 ioc_err(mrioc, "%s :Issue TM: Command is in use\n", __func__);
2326 drv_cmd->state = MPI3MR_CMD_PENDING;
2327 drv_cmd->is_waiting = 0;
2328 drv_cmd->callback = mpi3mr_dev_rmhs_complete_tm;
2329 drv_cmd->dev_handle = handle;
2330 drv_cmd->iou_rc = iou_rc;
2331 tm_req.dev_handle = cpu_to_le16(handle);
2332 tm_req.task_type = MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
2333 tm_req.host_tag = cpu_to_le16(drv_cmd->host_tag);
2334 tm_req.task_host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INVALID);
2335 tm_req.function = MPI3_FUNCTION_SCSI_TASK_MGMT;
2337 set_bit(handle, mrioc->removepend_bitmap);
2338 retval = mpi3mr_admin_request_post(mrioc, &tm_req, sizeof(tm_req), 1);
2340 ioc_err(mrioc, "%s :Issue DevRmHsTM: Admin Post failed\n",
2347 drv_cmd->state = MPI3MR_CMD_NOTUSED;
2348 drv_cmd->callback = NULL;
2349 drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
2350 drv_cmd->retry_count = 0;
2351 clear_bit(cmd_idx, mrioc->devrem_bitmap);
2355 * mpi3mr_complete_evt_ack - event ack request completion
2356 * @mrioc: Adapter instance reference
2357 * @drv_cmd: Internal command tracker
2359 * This is the completion handler for non blocking event
2360 * acknowledgment sent to the firmware and this will issue any
2361 * pending event acknowledgment request.
2365 static void mpi3mr_complete_evt_ack(struct mpi3mr_ioc *mrioc,
2366 struct mpi3mr_drv_cmd *drv_cmd)
2368 u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
2369 struct delayed_evt_ack_node *delayed_evtack = NULL;
2371 if (drv_cmd->state & MPI3MR_CMD_RESET)
2374 if (drv_cmd->ioc_status != MPI3_IOCSTATUS_SUCCESS) {
2375 dprint_event_th(mrioc,
2376 "immediate event ack failed with ioc_status(0x%04x) log_info(0x%08x)\n",
2377 (drv_cmd->ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
2378 drv_cmd->ioc_loginfo);
2381 if (!list_empty(&mrioc->delayed_evtack_cmds_list)) {
2383 list_entry(mrioc->delayed_evtack_cmds_list.next,
2384 struct delayed_evt_ack_node, list);
2385 mpi3mr_send_event_ack(mrioc, delayed_evtack->event, drv_cmd,
2386 delayed_evtack->event_ctx);
2387 list_del(&delayed_evtack->list);
2388 kfree(delayed_evtack);
2392 drv_cmd->state = MPI3MR_CMD_NOTUSED;
2393 drv_cmd->callback = NULL;
2394 clear_bit(cmd_idx, mrioc->evtack_cmds_bitmap);
2398 * mpi3mr_send_event_ack - Issue event acknwoledgment request
2399 * @mrioc: Adapter instance reference
2400 * @event: MPI3 event id
2401 * @cmdparam: Internal command tracker
2402 * @event_ctx: event context
2404 * Issues event acknowledgment request to the firmware if there
2405 * is a free command to send the event ack else it to a pend
2406 * list so that it will be processed on a completion of a prior
2407 * event acknowledgment .
2411 static void mpi3mr_send_event_ack(struct mpi3mr_ioc *mrioc, u8 event,
2412 struct mpi3mr_drv_cmd *cmdparam, u32 event_ctx)
2414 struct mpi3_event_ack_request evtack_req;
2417 u16 cmd_idx = MPI3MR_NUM_EVTACKCMD;
2418 struct mpi3mr_drv_cmd *drv_cmd = cmdparam;
2419 struct delayed_evt_ack_node *delayed_evtack = NULL;
2422 dprint_event_th(mrioc,
2423 "sending delayed event ack in the top half for event(0x%02x), event_ctx(0x%08x)\n",
2427 dprint_event_th(mrioc,
2428 "sending event ack in the top half for event(0x%02x), event_ctx(0x%08x)\n",
2431 cmd_idx = find_first_zero_bit(mrioc->evtack_cmds_bitmap,
2432 MPI3MR_NUM_EVTACKCMD);
2433 if (cmd_idx < MPI3MR_NUM_EVTACKCMD) {
2434 if (!test_and_set_bit(cmd_idx,
2435 mrioc->evtack_cmds_bitmap))
2437 cmd_idx = MPI3MR_NUM_EVTACKCMD;
2439 } while (retrycount--);
2441 if (cmd_idx >= MPI3MR_NUM_EVTACKCMD) {
2442 delayed_evtack = kzalloc(sizeof(*delayed_evtack),
2444 if (!delayed_evtack)
2446 INIT_LIST_HEAD(&delayed_evtack->list);
2447 delayed_evtack->event = event;
2448 delayed_evtack->event_ctx = event_ctx;
2449 list_add_tail(&delayed_evtack->list,
2450 &mrioc->delayed_evtack_cmds_list);
2451 dprint_event_th(mrioc,
2452 "event ack in the top half for event(0x%02x), event_ctx(0x%08x) is postponed\n",
2456 drv_cmd = &mrioc->evtack_cmds[cmd_idx];
2459 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
2461 memset(&evtack_req, 0, sizeof(evtack_req));
2462 if (drv_cmd->state & MPI3MR_CMD_PENDING) {
2463 dprint_event_th(mrioc,
2464 "sending event ack failed due to command in use\n");
2467 drv_cmd->state = MPI3MR_CMD_PENDING;
2468 drv_cmd->is_waiting = 0;
2469 drv_cmd->callback = mpi3mr_complete_evt_ack;
2470 evtack_req.host_tag = cpu_to_le16(drv_cmd->host_tag);
2471 evtack_req.function = MPI3_FUNCTION_EVENT_ACK;
2472 evtack_req.event = event;
2473 evtack_req.event_context = cpu_to_le32(event_ctx);
2474 retval = mpi3mr_admin_request_post(mrioc, &evtack_req,
2475 sizeof(evtack_req), 1);
2477 dprint_event_th(mrioc,
2478 "posting event ack request is failed\n");
2482 dprint_event_th(mrioc,
2483 "event ack in the top half for event(0x%02x), event_ctx(0x%08x) is posted\n",
2488 drv_cmd->state = MPI3MR_CMD_NOTUSED;
2489 drv_cmd->callback = NULL;
2490 clear_bit(cmd_idx, mrioc->evtack_cmds_bitmap);
2494 * mpi3mr_pcietopochg_evt_th - PCIETopologyChange evt tophalf
2495 * @mrioc: Adapter instance reference
2496 * @event_reply: event data
2498 * Checks for the reason code and based on that either block I/O
2499 * to device, or unblock I/O to the device, or start the device
2500 * removal handshake with reason as remove with the firmware for
2505 static void mpi3mr_pcietopochg_evt_th(struct mpi3mr_ioc *mrioc,
2506 struct mpi3_event_notification_reply *event_reply)
2508 struct mpi3_event_data_pcie_topology_change_list *topo_evt =
2509 (struct mpi3_event_data_pcie_topology_change_list *)event_reply->event_data;
2513 struct mpi3mr_tgt_dev *tgtdev = NULL;
2514 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2516 for (i = 0; i < topo_evt->num_entries; i++) {
2517 handle = le16_to_cpu(topo_evt->port_entry[i].attached_dev_handle);
2520 reason_code = topo_evt->port_entry[i].port_status;
2521 scsi_tgt_priv_data = NULL;
2522 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
2523 if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
2524 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2525 tgtdev->starget->hostdata;
2526 switch (reason_code) {
2527 case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
2528 if (scsi_tgt_priv_data) {
2529 scsi_tgt_priv_data->dev_removed = 1;
2530 scsi_tgt_priv_data->dev_removedelay = 0;
2531 atomic_set(&scsi_tgt_priv_data->block_io, 0);
2533 mpi3mr_dev_rmhs_send_tm(mrioc, handle, NULL,
2534 MPI3_CTRL_OP_REMOVE_DEVICE);
2536 case MPI3_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
2537 if (scsi_tgt_priv_data) {
2538 scsi_tgt_priv_data->dev_removedelay = 1;
2539 atomic_inc(&scsi_tgt_priv_data->block_io);
2542 case MPI3_EVENT_PCIE_TOPO_PS_RESPONDING:
2543 if (scsi_tgt_priv_data &&
2544 scsi_tgt_priv_data->dev_removedelay) {
2545 scsi_tgt_priv_data->dev_removedelay = 0;
2546 atomic_dec_if_positive
2547 (&scsi_tgt_priv_data->block_io);
2550 case MPI3_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
2555 mpi3mr_tgtdev_put(tgtdev);
2560 * mpi3mr_sastopochg_evt_th - SASTopologyChange evt tophalf
2561 * @mrioc: Adapter instance reference
2562 * @event_reply: event data
2564 * Checks for the reason code and based on that either block I/O
2565 * to device, or unblock I/O to the device, or start the device
2566 * removal handshake with reason as remove with the firmware for
2571 static void mpi3mr_sastopochg_evt_th(struct mpi3mr_ioc *mrioc,
2572 struct mpi3_event_notification_reply *event_reply)
2574 struct mpi3_event_data_sas_topology_change_list *topo_evt =
2575 (struct mpi3_event_data_sas_topology_change_list *)event_reply->event_data;
2579 struct mpi3mr_tgt_dev *tgtdev = NULL;
2580 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2582 for (i = 0; i < topo_evt->num_entries; i++) {
2583 handle = le16_to_cpu(topo_evt->phy_entry[i].attached_dev_handle);
2586 reason_code = topo_evt->phy_entry[i].status &
2587 MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
2588 scsi_tgt_priv_data = NULL;
2589 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
2590 if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
2591 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2592 tgtdev->starget->hostdata;
2593 switch (reason_code) {
2594 case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
2595 if (scsi_tgt_priv_data) {
2596 scsi_tgt_priv_data->dev_removed = 1;
2597 scsi_tgt_priv_data->dev_removedelay = 0;
2598 atomic_set(&scsi_tgt_priv_data->block_io, 0);
2600 mpi3mr_dev_rmhs_send_tm(mrioc, handle, NULL,
2601 MPI3_CTRL_OP_REMOVE_DEVICE);
2603 case MPI3_EVENT_SAS_TOPO_PHY_RC_DELAY_NOT_RESPONDING:
2604 if (scsi_tgt_priv_data) {
2605 scsi_tgt_priv_data->dev_removedelay = 1;
2606 atomic_inc(&scsi_tgt_priv_data->block_io);
2609 case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
2610 if (scsi_tgt_priv_data &&
2611 scsi_tgt_priv_data->dev_removedelay) {
2612 scsi_tgt_priv_data->dev_removedelay = 0;
2613 atomic_dec_if_positive
2614 (&scsi_tgt_priv_data->block_io);
2617 case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
2622 mpi3mr_tgtdev_put(tgtdev);
2627 * mpi3mr_devstatuschg_evt_th - DeviceStatusChange evt tophalf
2628 * @mrioc: Adapter instance reference
2629 * @event_reply: event data
2631 * Checks for the reason code and based on that either block I/O
2632 * to device, or unblock I/O to the device, or start the device
2633 * removal handshake with reason as remove/hide acknowledgment
2634 * with the firmware.
2638 static void mpi3mr_devstatuschg_evt_th(struct mpi3mr_ioc *mrioc,
2639 struct mpi3_event_notification_reply *event_reply)
2642 u8 ublock = 0, block = 0, hide = 0, delete = 0, remove = 0;
2643 struct mpi3mr_tgt_dev *tgtdev = NULL;
2644 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2645 struct mpi3_event_data_device_status_change *evtdata =
2646 (struct mpi3_event_data_device_status_change *)event_reply->event_data;
2648 if (mrioc->stop_drv_processing)
2651 dev_handle = le16_to_cpu(evtdata->dev_handle);
2653 switch (evtdata->reason_code) {
2654 case MPI3_EVENT_DEV_STAT_RC_INT_DEVICE_RESET_STRT:
2655 case MPI3_EVENT_DEV_STAT_RC_INT_IT_NEXUS_RESET_STRT:
2658 case MPI3_EVENT_DEV_STAT_RC_HIDDEN:
2662 case MPI3_EVENT_DEV_STAT_RC_VD_NOT_RESPONDING:
2666 case MPI3_EVENT_DEV_STAT_RC_INT_DEVICE_RESET_CMP:
2667 case MPI3_EVENT_DEV_STAT_RC_INT_IT_NEXUS_RESET_CMP:
2674 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
2678 tgtdev->is_hidden = hide;
2679 if (tgtdev->starget && tgtdev->starget->hostdata) {
2680 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2681 tgtdev->starget->hostdata;
2683 atomic_inc(&scsi_tgt_priv_data->block_io);
2685 scsi_tgt_priv_data->dev_removed = 1;
2687 atomic_dec_if_positive(&scsi_tgt_priv_data->block_io);
2690 mpi3mr_dev_rmhs_send_tm(mrioc, dev_handle, NULL,
2691 MPI3_CTRL_OP_REMOVE_DEVICE);
2693 mpi3mr_dev_rmhs_send_tm(mrioc, dev_handle, NULL,
2694 MPI3_CTRL_OP_HIDDEN_ACK);
2698 mpi3mr_tgtdev_put(tgtdev);
2702 * mpi3mr_preparereset_evt_th - Prepare for reset event tophalf
2703 * @mrioc: Adapter instance reference
2704 * @event_reply: event data
2706 * Blocks and unblocks host level I/O based on the reason code
2710 static void mpi3mr_preparereset_evt_th(struct mpi3mr_ioc *mrioc,
2711 struct mpi3_event_notification_reply *event_reply)
2713 struct mpi3_event_data_prepare_for_reset *evtdata =
2714 (struct mpi3_event_data_prepare_for_reset *)event_reply->event_data;
2716 if (evtdata->reason_code == MPI3_EVENT_PREPARE_RESET_RC_START) {
2717 dprint_event_th(mrioc,
2718 "prepare for reset event top half with rc=start\n");
2719 if (mrioc->prepare_for_reset)
2721 mrioc->prepare_for_reset = 1;
2722 mrioc->prepare_for_reset_timeout_counter = 0;
2723 } else if (evtdata->reason_code == MPI3_EVENT_PREPARE_RESET_RC_ABORT) {
2724 dprint_event_th(mrioc,
2725 "prepare for reset top half with rc=abort\n");
2726 mrioc->prepare_for_reset = 0;
2727 mrioc->prepare_for_reset_timeout_counter = 0;
2729 if ((event_reply->msg_flags & MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_MASK)
2730 == MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_REQUIRED)
2731 mpi3mr_send_event_ack(mrioc, event_reply->event, NULL,
2732 le32_to_cpu(event_reply->event_context));
2736 * mpi3mr_energypackchg_evt_th - Energy pack change evt tophalf
2737 * @mrioc: Adapter instance reference
2738 * @event_reply: event data
2740 * Identifies the new shutdown timeout value and update.
2744 static void mpi3mr_energypackchg_evt_th(struct mpi3mr_ioc *mrioc,
2745 struct mpi3_event_notification_reply *event_reply)
2747 struct mpi3_event_data_energy_pack_change *evtdata =
2748 (struct mpi3_event_data_energy_pack_change *)event_reply->event_data;
2749 u16 shutdown_timeout = le16_to_cpu(evtdata->shutdown_timeout);
2751 if (shutdown_timeout <= 0) {
2753 "%s :Invalid Shutdown Timeout received = %d\n",
2754 __func__, shutdown_timeout);
2759 "%s :Previous Shutdown Timeout Value = %d New Shutdown Timeout Value = %d\n",
2760 __func__, mrioc->facts.shutdown_timeout, shutdown_timeout);
2761 mrioc->facts.shutdown_timeout = shutdown_timeout;
2765 * mpi3mr_cablemgmt_evt_th - Cable management event tophalf
2766 * @mrioc: Adapter instance reference
2767 * @event_reply: event data
2769 * Displays Cable manegemt event details.
2773 static void mpi3mr_cablemgmt_evt_th(struct mpi3mr_ioc *mrioc,
2774 struct mpi3_event_notification_reply *event_reply)
2776 struct mpi3_event_data_cable_management *evtdata =
2777 (struct mpi3_event_data_cable_management *)event_reply->event_data;
2779 switch (evtdata->status) {
2780 case MPI3_EVENT_CABLE_MGMT_STATUS_INSUFFICIENT_POWER:
2782 ioc_info(mrioc, "An active cable with receptacle_id %d cannot be powered.\n"
2783 "Devices connected to this cable are not detected.\n"
2784 "This cable requires %d mW of power.\n",
2785 evtdata->receptacle_id,
2786 le32_to_cpu(evtdata->active_cable_power_requirement));
2789 case MPI3_EVENT_CABLE_MGMT_STATUS_DEGRADED:
2791 ioc_info(mrioc, "A cable with receptacle_id %d is not running at optimal speed\n",
2792 evtdata->receptacle_id);
2801 * mpi3mr_add_event_wait_for_device_refresh - Add Wait for Device Refresh Event
2802 * @mrioc: Adapter instance reference
2804 * Add driver specific event to make sure that the driver won't process the
2805 * events until all the devices are refreshed during soft reset.
2809 void mpi3mr_add_event_wait_for_device_refresh(struct mpi3mr_ioc *mrioc)
2811 struct mpi3mr_fwevt *fwevt = NULL;
2813 fwevt = mpi3mr_alloc_fwevt(0);
2815 dprint_event_th(mrioc,
2816 "failed to schedule bottom half handler for event(0x%02x)\n",
2817 MPI3_EVENT_WAIT_FOR_DEVICES_TO_REFRESH);
2820 fwevt->mrioc = mrioc;
2821 fwevt->event_id = MPI3_EVENT_WAIT_FOR_DEVICES_TO_REFRESH;
2822 fwevt->send_ack = 0;
2823 fwevt->process_evt = 1;
2825 fwevt->event_data_size = 0;
2826 mpi3mr_fwevt_add_to_list(mrioc, fwevt);
2830 * mpi3mr_os_handle_events - Firmware event handler
2831 * @mrioc: Adapter instance reference
2832 * @event_reply: event data
2834 * Identify whteher the event has to handled and acknowledged
2835 * and either process the event in the tophalf and/or schedule a
2836 * bottom half through mpi3mr_fwevt_worker.
2840 void mpi3mr_os_handle_events(struct mpi3mr_ioc *mrioc,
2841 struct mpi3_event_notification_reply *event_reply)
2844 struct mpi3mr_fwevt *fwevt = NULL;
2845 bool ack_req = 0, process_evt_bh = 0;
2847 if (mrioc->stop_drv_processing)
2850 if ((event_reply->msg_flags & MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_MASK)
2851 == MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_REQUIRED)
2854 evt_type = event_reply->event;
2857 case MPI3_EVENT_DEVICE_ADDED:
2859 struct mpi3_device_page0 *dev_pg0 =
2860 (struct mpi3_device_page0 *)event_reply->event_data;
2861 if (mpi3mr_create_tgtdev(mrioc, dev_pg0))
2863 "%s :Failed to add device in the device add event\n",
2869 case MPI3_EVENT_DEVICE_STATUS_CHANGE:
2872 mpi3mr_devstatuschg_evt_th(mrioc, event_reply);
2875 case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
2878 mpi3mr_sastopochg_evt_th(mrioc, event_reply);
2881 case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
2884 mpi3mr_pcietopochg_evt_th(mrioc, event_reply);
2887 case MPI3_EVENT_PREPARE_FOR_RESET:
2889 mpi3mr_preparereset_evt_th(mrioc, event_reply);
2893 case MPI3_EVENT_DEVICE_INFO_CHANGED:
2894 case MPI3_EVENT_LOG_DATA:
2895 case MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE:
2896 case MPI3_EVENT_ENCL_DEVICE_ADDED:
2901 case MPI3_EVENT_ENERGY_PACK_CHANGE:
2903 mpi3mr_energypackchg_evt_th(mrioc, event_reply);
2906 case MPI3_EVENT_CABLE_MGMT:
2908 mpi3mr_cablemgmt_evt_th(mrioc, event_reply);
2911 case MPI3_EVENT_SAS_DISCOVERY:
2912 case MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
2913 case MPI3_EVENT_SAS_BROADCAST_PRIMITIVE:
2914 case MPI3_EVENT_PCIE_ENUMERATION:
2917 ioc_info(mrioc, "%s :event 0x%02x is not handled\n",
2918 __func__, evt_type);
2921 if (process_evt_bh || ack_req) {
2922 sz = event_reply->event_data_length * 4;
2923 fwevt = mpi3mr_alloc_fwevt(sz);
2925 ioc_info(mrioc, "%s :failure at %s:%d/%s()!\n",
2926 __func__, __FILE__, __LINE__, __func__);
2930 memcpy(fwevt->event_data, event_reply->event_data, sz);
2931 fwevt->mrioc = mrioc;
2932 fwevt->event_id = evt_type;
2933 fwevt->send_ack = ack_req;
2934 fwevt->process_evt = process_evt_bh;
2935 fwevt->evt_ctx = le32_to_cpu(event_reply->event_context);
2936 mpi3mr_fwevt_add_to_list(mrioc, fwevt);
2941 * mpi3mr_setup_eedp - Setup EEDP information in MPI3 SCSI IO
2942 * @mrioc: Adapter instance reference
2943 * @scmd: SCSI command reference
2944 * @scsiio_req: MPI3 SCSI IO request
2946 * Identifies the protection information flags from the SCSI
2947 * command and set appropriate flags in the MPI3 SCSI IO
2952 static void mpi3mr_setup_eedp(struct mpi3mr_ioc *mrioc,
2953 struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
2956 unsigned char prot_op = scsi_get_prot_op(scmd);
2959 case SCSI_PROT_NORMAL:
2961 case SCSI_PROT_READ_STRIP:
2962 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REMOVE;
2964 case SCSI_PROT_WRITE_INSERT:
2965 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_INSERT;
2967 case SCSI_PROT_READ_INSERT:
2968 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_INSERT;
2969 scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2971 case SCSI_PROT_WRITE_STRIP:
2972 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REMOVE;
2973 scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2975 case SCSI_PROT_READ_PASS:
2976 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK;
2977 scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2979 case SCSI_PROT_WRITE_PASS:
2980 if (scmd->prot_flags & SCSI_PROT_IP_CHECKSUM) {
2981 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REGEN;
2982 scsiio_req->sgl[0].eedp.application_tag_translation_mask =
2985 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK;
2987 scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2993 if (scmd->prot_flags & SCSI_PROT_GUARD_CHECK)
2994 eedp_flags |= MPI3_EEDPFLAGS_CHK_GUARD;
2996 if (scmd->prot_flags & SCSI_PROT_IP_CHECKSUM)
2997 eedp_flags |= MPI3_EEDPFLAGS_HOST_GUARD_IP_CHKSUM;
2999 if (scmd->prot_flags & SCSI_PROT_REF_CHECK) {
3000 eedp_flags |= MPI3_EEDPFLAGS_CHK_REF_TAG |
3001 MPI3_EEDPFLAGS_INCR_PRI_REF_TAG;
3002 scsiio_req->cdb.eedp32.primary_reference_tag =
3003 cpu_to_be32(scsi_prot_ref_tag(scmd));
3006 if (scmd->prot_flags & SCSI_PROT_REF_INCREMENT)
3007 eedp_flags |= MPI3_EEDPFLAGS_INCR_PRI_REF_TAG;
3009 eedp_flags |= MPI3_EEDPFLAGS_ESC_MODE_APPTAG_DISABLE;
3011 switch (scsi_prot_interval(scmd)) {
3013 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_512;
3016 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_520;
3019 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4080;
3022 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4088;
3025 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4096;
3028 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4104;
3031 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4160;
3037 scsiio_req->sgl[0].eedp.eedp_flags = cpu_to_le16(eedp_flags);
3038 scsiio_req->sgl[0].eedp.flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_EXTENDED;
3042 * mpi3mr_build_sense_buffer - Map sense information
3044 * @buf: Sense buffer to populate
3046 * @asc: Additional sense code
3047 * @ascq: Additional sense code qualifier
3049 * Maps the given sense information into either descriptor or
3050 * fixed format sense data.
3054 static inline void mpi3mr_build_sense_buffer(int desc, u8 *buf, u8 key,
3058 buf[0] = 0x72; /* descriptor, current */
3064 buf[0] = 0x70; /* fixed, current */
3073 * mpi3mr_map_eedp_error - Map EEDP errors from IOC status
3074 * @scmd: SCSI command reference
3075 * @ioc_status: status of MPI3 request
3077 * Maps the EEDP error status of the SCSI IO request to sense
3082 static void mpi3mr_map_eedp_error(struct scsi_cmnd *scmd,
3087 switch (ioc_status) {
3088 case MPI3_IOCSTATUS_EEDP_GUARD_ERROR:
3091 case MPI3_IOCSTATUS_EEDP_APP_TAG_ERROR:
3094 case MPI3_IOCSTATUS_EEDP_REF_TAG_ERROR:
3102 mpi3mr_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
3104 scmd->result = (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION;
3108 * mpi3mr_process_op_reply_desc - reply descriptor handler
3109 * @mrioc: Adapter instance reference
3110 * @reply_desc: Operational reply descriptor
3111 * @reply_dma: place holder for reply DMA address
3112 * @qidx: Operational queue index
3114 * Process the operational reply descriptor and identifies the
3115 * descriptor type. Based on the descriptor map the MPI3 request
3116 * status to a SCSI command status and calls scsi_done call
3121 void mpi3mr_process_op_reply_desc(struct mpi3mr_ioc *mrioc,
3122 struct mpi3_default_reply_descriptor *reply_desc, u64 *reply_dma, u16 qidx)
3124 u16 reply_desc_type, host_tag = 0;
3125 u16 ioc_status = MPI3_IOCSTATUS_SUCCESS;
3126 u32 ioc_loginfo = 0;
3127 struct mpi3_status_reply_descriptor *status_desc = NULL;
3128 struct mpi3_address_reply_descriptor *addr_desc = NULL;
3129 struct mpi3_success_reply_descriptor *success_desc = NULL;
3130 struct mpi3_scsi_io_reply *scsi_reply = NULL;
3131 struct scsi_cmnd *scmd = NULL;
3132 struct scmd_priv *priv = NULL;
3133 u8 *sense_buf = NULL;
3134 u8 scsi_state = 0, scsi_status = 0, sense_state = 0;
3135 u32 xfer_count = 0, sense_count = 0, resp_data = 0;
3136 u16 dev_handle = 0xFFFF;
3137 struct scsi_sense_hdr sshdr;
3138 struct mpi3mr_stgt_priv_data *stgt_priv_data = NULL;
3139 struct mpi3mr_sdev_priv_data *sdev_priv_data = NULL;
3140 u32 ioc_pend_data_len = 0, tg_pend_data_len = 0, data_len_blks = 0;
3141 struct mpi3mr_throttle_group_info *tg = NULL;
3142 u8 throttle_enabled_dev = 0;
3145 reply_desc_type = le16_to_cpu(reply_desc->reply_flags) &
3146 MPI3_REPLY_DESCRIPT_FLAGS_TYPE_MASK;
3147 switch (reply_desc_type) {
3148 case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_STATUS:
3149 status_desc = (struct mpi3_status_reply_descriptor *)reply_desc;
3150 host_tag = le16_to_cpu(status_desc->host_tag);
3151 ioc_status = le16_to_cpu(status_desc->ioc_status);
3153 MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
3154 ioc_loginfo = le32_to_cpu(status_desc->ioc_log_info);
3155 ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
3157 case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_ADDRESS_REPLY:
3158 addr_desc = (struct mpi3_address_reply_descriptor *)reply_desc;
3159 *reply_dma = le64_to_cpu(addr_desc->reply_frame_address);
3160 scsi_reply = mpi3mr_get_reply_virt_addr(mrioc,
3163 panic("%s: scsi_reply is NULL, this shouldn't happen\n",
3167 host_tag = le16_to_cpu(scsi_reply->host_tag);
3168 ioc_status = le16_to_cpu(scsi_reply->ioc_status);
3169 scsi_status = scsi_reply->scsi_status;
3170 scsi_state = scsi_reply->scsi_state;
3171 dev_handle = le16_to_cpu(scsi_reply->dev_handle);
3172 sense_state = (scsi_state & MPI3_SCSI_STATE_SENSE_MASK);
3173 xfer_count = le32_to_cpu(scsi_reply->transfer_count);
3174 sense_count = le32_to_cpu(scsi_reply->sense_count);
3175 resp_data = le32_to_cpu(scsi_reply->response_data);
3176 sense_buf = mpi3mr_get_sensebuf_virt_addr(mrioc,
3177 le64_to_cpu(scsi_reply->sense_data_buffer_address));
3179 MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
3180 ioc_loginfo = le32_to_cpu(scsi_reply->ioc_log_info);
3181 ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
3182 if (sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY)
3183 panic("%s: Ran out of sense buffers\n", mrioc->name);
3185 case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_SUCCESS:
3186 success_desc = (struct mpi3_success_reply_descriptor *)reply_desc;
3187 host_tag = le16_to_cpu(success_desc->host_tag);
3192 scmd = mpi3mr_scmd_from_host_tag(mrioc, host_tag, qidx);
3194 panic("%s: Cannot Identify scmd for host_tag 0x%x\n",
3195 mrioc->name, host_tag);
3198 priv = scsi_cmd_priv(scmd);
3200 data_len_blks = scsi_bufflen(scmd) >> 9;
3201 sdev_priv_data = scmd->device->hostdata;
3202 if (sdev_priv_data) {
3203 stgt_priv_data = sdev_priv_data->tgt_priv_data;
3204 if (stgt_priv_data) {
3205 tg = stgt_priv_data->throttle_group;
3206 throttle_enabled_dev =
3207 stgt_priv_data->io_throttle_enabled;
3208 dev_handle = stgt_priv_data->dev_handle;
3211 if (unlikely((data_len_blks >= mrioc->io_throttle_data_length) &&
3212 throttle_enabled_dev)) {
3213 ioc_pend_data_len = atomic_sub_return(data_len_blks,
3214 &mrioc->pend_large_data_sz);
3216 tg_pend_data_len = atomic_sub_return(data_len_blks,
3217 &tg->pend_large_data_sz);
3218 if (tg->io_divert && ((ioc_pend_data_len <=
3219 mrioc->io_throttle_low) &&
3220 (tg_pend_data_len <= tg->low))) {
3222 mpi3mr_set_io_divert_for_all_vd_in_tg(
3226 if (ioc_pend_data_len <= mrioc->io_throttle_low)
3227 stgt_priv_data->io_divert = 0;
3229 } else if (unlikely((stgt_priv_data && stgt_priv_data->io_divert))) {
3230 ioc_pend_data_len = atomic_read(&mrioc->pend_large_data_sz);
3232 if (ioc_pend_data_len <= mrioc->io_throttle_low)
3233 stgt_priv_data->io_divert = 0;
3235 } else if (ioc_pend_data_len <= mrioc->io_throttle_low) {
3236 tg_pend_data_len = atomic_read(&tg->pend_large_data_sz);
3237 if (tg->io_divert && (tg_pend_data_len <= tg->low)) {
3239 mpi3mr_set_io_divert_for_all_vd_in_tg(
3246 scmd->result = DID_OK << 16;
3250 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_count);
3251 if (ioc_status == MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN &&
3252 xfer_count == 0 && (scsi_status == MPI3_SCSI_STATUS_BUSY ||
3253 scsi_status == MPI3_SCSI_STATUS_RESERVATION_CONFLICT ||
3254 scsi_status == MPI3_SCSI_STATUS_TASK_SET_FULL))
3255 ioc_status = MPI3_IOCSTATUS_SUCCESS;
3257 if ((sense_state == MPI3_SCSI_STATE_SENSE_VALID) && sense_count &&
3259 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE, sense_count);
3261 memcpy(scmd->sense_buffer, sense_buf, sz);
3264 switch (ioc_status) {
3265 case MPI3_IOCSTATUS_BUSY:
3266 case MPI3_IOCSTATUS_INSUFFICIENT_RESOURCES:
3267 scmd->result = SAM_STAT_BUSY;
3269 case MPI3_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3270 scmd->result = DID_NO_CONNECT << 16;
3272 case MPI3_IOCSTATUS_SCSI_IOC_TERMINATED:
3273 scmd->result = DID_SOFT_ERROR << 16;
3275 case MPI3_IOCSTATUS_SCSI_TASK_TERMINATED:
3276 case MPI3_IOCSTATUS_SCSI_EXT_TERMINATED:
3277 scmd->result = DID_RESET << 16;
3279 case MPI3_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3280 if ((xfer_count == 0) || (scmd->underflow > xfer_count))
3281 scmd->result = DID_SOFT_ERROR << 16;
3283 scmd->result = (DID_OK << 16) | scsi_status;
3285 case MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN:
3286 scmd->result = (DID_OK << 16) | scsi_status;
3287 if (sense_state == MPI3_SCSI_STATE_SENSE_VALID)
3289 if (xfer_count < scmd->underflow) {
3290 if (scsi_status == SAM_STAT_BUSY)
3291 scmd->result = SAM_STAT_BUSY;
3293 scmd->result = DID_SOFT_ERROR << 16;
3294 } else if ((scsi_state & (MPI3_SCSI_STATE_NO_SCSI_STATUS)) ||
3295 (sense_state != MPI3_SCSI_STATE_SENSE_NOT_AVAILABLE))
3296 scmd->result = DID_SOFT_ERROR << 16;
3297 else if (scsi_state & MPI3_SCSI_STATE_TERMINATED)
3298 scmd->result = DID_RESET << 16;
3300 case MPI3_IOCSTATUS_SCSI_DATA_OVERRUN:
3301 scsi_set_resid(scmd, 0);
3303 case MPI3_IOCSTATUS_SCSI_RECOVERED_ERROR:
3304 case MPI3_IOCSTATUS_SUCCESS:
3305 scmd->result = (DID_OK << 16) | scsi_status;
3306 if ((scsi_state & (MPI3_SCSI_STATE_NO_SCSI_STATUS)) ||
3307 (sense_state == MPI3_SCSI_STATE_SENSE_FAILED) ||
3308 (sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY))
3309 scmd->result = DID_SOFT_ERROR << 16;
3310 else if (scsi_state & MPI3_SCSI_STATE_TERMINATED)
3311 scmd->result = DID_RESET << 16;
3313 case MPI3_IOCSTATUS_EEDP_GUARD_ERROR:
3314 case MPI3_IOCSTATUS_EEDP_REF_TAG_ERROR:
3315 case MPI3_IOCSTATUS_EEDP_APP_TAG_ERROR:
3316 mpi3mr_map_eedp_error(scmd, ioc_status);
3318 case MPI3_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3319 case MPI3_IOCSTATUS_INVALID_FUNCTION:
3320 case MPI3_IOCSTATUS_INVALID_SGL:
3321 case MPI3_IOCSTATUS_INTERNAL_ERROR:
3322 case MPI3_IOCSTATUS_INVALID_FIELD:
3323 case MPI3_IOCSTATUS_INVALID_STATE:
3324 case MPI3_IOCSTATUS_SCSI_IO_DATA_ERROR:
3325 case MPI3_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3326 case MPI3_IOCSTATUS_INSUFFICIENT_POWER:
3328 scmd->result = DID_SOFT_ERROR << 16;
3332 if (scmd->result != (DID_OK << 16) && (scmd->cmnd[0] != ATA_12) &&
3333 (scmd->cmnd[0] != ATA_16) &&
3334 mrioc->logging_level & MPI3_DEBUG_SCSI_ERROR) {
3335 ioc_info(mrioc, "%s :scmd->result 0x%x\n", __func__,
3337 scsi_print_command(scmd);
3339 "%s :Command issued to handle 0x%02x returned with error 0x%04x loginfo 0x%08x, qid %d\n",
3340 __func__, dev_handle, ioc_status, ioc_loginfo,
3341 priv->req_q_idx + 1);
3343 " host_tag %d scsi_state 0x%02x scsi_status 0x%02x, xfer_cnt %d resp_data 0x%x\n",
3344 host_tag, scsi_state, scsi_status, xfer_count, resp_data);
3346 scsi_normalize_sense(sense_buf, sense_count, &sshdr);
3348 "%s :sense_count 0x%x, sense_key 0x%x ASC 0x%x, ASCQ 0x%x\n",
3349 __func__, sense_count, sshdr.sense_key,
3350 sshdr.asc, sshdr.ascq);
3354 if (priv->meta_sg_valid) {
3355 dma_unmap_sg(&mrioc->pdev->dev, scsi_prot_sglist(scmd),
3356 scsi_prot_sg_count(scmd), scmd->sc_data_direction);
3358 mpi3mr_clear_scmd_priv(mrioc, scmd);
3359 scsi_dma_unmap(scmd);
3363 mpi3mr_repost_sense_buf(mrioc,
3364 le64_to_cpu(scsi_reply->sense_data_buffer_address));
3368 * mpi3mr_get_chain_idx - get free chain buffer index
3369 * @mrioc: Adapter instance reference
3371 * Try to get a free chain buffer index from the free pool.
3373 * Return: -1 on failure or the free chain buffer index
3375 static int mpi3mr_get_chain_idx(struct mpi3mr_ioc *mrioc)
3379 unsigned long flags;
3381 spin_lock_irqsave(&mrioc->chain_buf_lock, flags);
3383 cmd_idx = find_first_zero_bit(mrioc->chain_bitmap,
3384 mrioc->chain_buf_count);
3385 if (cmd_idx < mrioc->chain_buf_count) {
3386 set_bit(cmd_idx, mrioc->chain_bitmap);
3390 } while (retry_count--);
3391 spin_unlock_irqrestore(&mrioc->chain_buf_lock, flags);
3396 * mpi3mr_prepare_sg_scmd - build scatter gather list
3397 * @mrioc: Adapter instance reference
3398 * @scmd: SCSI command reference
3399 * @scsiio_req: MPI3 SCSI IO request
3401 * This function maps SCSI command's data and protection SGEs to
3402 * MPI request SGEs. If required additional 4K chain buffer is
3403 * used to send the SGEs.
3405 * Return: 0 on success, -ENOMEM on dma_map_sg failure
3407 static int mpi3mr_prepare_sg_scmd(struct mpi3mr_ioc *mrioc,
3408 struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
3410 dma_addr_t chain_dma;
3411 struct scatterlist *sg_scmd;
3412 void *sg_local, *chain;
3414 int sges_left, chain_idx;
3415 u32 sges_in_segment;
3416 u8 simple_sgl_flags;
3417 u8 simple_sgl_flags_last;
3418 u8 last_chain_sgl_flags;
3419 struct chain_element *chain_req;
3420 struct scmd_priv *priv = NULL;
3421 u32 meta_sg = le32_to_cpu(scsiio_req->flags) &
3422 MPI3_SCSIIO_FLAGS_DMAOPERATION_HOST_PI;
3424 priv = scsi_cmd_priv(scmd);
3426 simple_sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_SIMPLE |
3427 MPI3_SGE_FLAGS_DLAS_SYSTEM;
3428 simple_sgl_flags_last = simple_sgl_flags |
3429 MPI3_SGE_FLAGS_END_OF_LIST;
3430 last_chain_sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_LAST_CHAIN |
3431 MPI3_SGE_FLAGS_DLAS_SYSTEM;
3434 sg_local = &scsiio_req->sgl[MPI3_SCSIIO_METASGL_INDEX];
3436 sg_local = &scsiio_req->sgl;
3438 if (!scsiio_req->data_length && !meta_sg) {
3439 mpi3mr_build_zero_len_sge(sg_local);
3444 sg_scmd = scsi_prot_sglist(scmd);
3445 sges_left = dma_map_sg(&mrioc->pdev->dev,
3446 scsi_prot_sglist(scmd),
3447 scsi_prot_sg_count(scmd),
3448 scmd->sc_data_direction);
3449 priv->meta_sg_valid = 1; /* To unmap meta sg DMA */
3451 sg_scmd = scsi_sglist(scmd);
3452 sges_left = scsi_dma_map(scmd);
3455 if (sges_left < 0) {
3456 sdev_printk(KERN_ERR, scmd->device,
3457 "scsi_dma_map failed: request for %d bytes!\n",
3458 scsi_bufflen(scmd));
3461 if (sges_left > mrioc->max_sgl_entries) {
3462 sdev_printk(KERN_ERR, scmd->device,
3463 "scsi_dma_map returned unsupported sge count %d!\n",
3468 sges_in_segment = (mrioc->facts.op_req_sz -
3469 offsetof(struct mpi3_scsi_io_request, sgl)) / sizeof(struct mpi3_sge_common);
3471 if (scsiio_req->sgl[0].eedp.flags ==
3472 MPI3_SGE_FLAGS_ELEMENT_TYPE_EXTENDED && !meta_sg) {
3473 sg_local += sizeof(struct mpi3_sge_common);
3475 /* Reserve 1st segment (scsiio_req->sgl[0]) for eedp */
3478 if (scsiio_req->msg_flags ==
3479 MPI3_SCSIIO_MSGFLAGS_METASGL_VALID && !meta_sg) {
3481 /* Reserve last segment (scsiio_req->sgl[3]) for meta sg */
3485 sges_in_segment = 1;
3487 if (sges_left <= sges_in_segment)
3488 goto fill_in_last_segment;
3490 /* fill in main message segment when there is a chain following */
3491 while (sges_in_segment > 1) {
3492 mpi3mr_add_sg_single(sg_local, simple_sgl_flags,
3493 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
3494 sg_scmd = sg_next(sg_scmd);
3495 sg_local += sizeof(struct mpi3_sge_common);
3500 chain_idx = mpi3mr_get_chain_idx(mrioc);
3503 chain_req = &mrioc->chain_sgl_list[chain_idx];
3505 priv->meta_chain_idx = chain_idx;
3507 priv->chain_idx = chain_idx;
3509 chain = chain_req->addr;
3510 chain_dma = chain_req->dma_addr;
3511 sges_in_segment = sges_left;
3512 chain_length = sges_in_segment * sizeof(struct mpi3_sge_common);
3514 mpi3mr_add_sg_single(sg_local, last_chain_sgl_flags,
3515 chain_length, chain_dma);
3519 fill_in_last_segment:
3520 while (sges_left > 0) {
3522 mpi3mr_add_sg_single(sg_local,
3523 simple_sgl_flags_last, sg_dma_len(sg_scmd),
3524 sg_dma_address(sg_scmd));
3526 mpi3mr_add_sg_single(sg_local, simple_sgl_flags,
3527 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
3528 sg_scmd = sg_next(sg_scmd);
3529 sg_local += sizeof(struct mpi3_sge_common);
3537 * mpi3mr_build_sg_scmd - build scatter gather list for SCSI IO
3538 * @mrioc: Adapter instance reference
3539 * @scmd: SCSI command reference
3540 * @scsiio_req: MPI3 SCSI IO request
3542 * This function calls mpi3mr_prepare_sg_scmd for constructing
3543 * both data SGEs and protection information SGEs in the MPI
3544 * format from the SCSI Command as appropriate .
3546 * Return: return value of mpi3mr_prepare_sg_scmd.
3548 static int mpi3mr_build_sg_scmd(struct mpi3mr_ioc *mrioc,
3549 struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
3553 ret = mpi3mr_prepare_sg_scmd(mrioc, scmd, scsiio_req);
3557 if (scsiio_req->msg_flags == MPI3_SCSIIO_MSGFLAGS_METASGL_VALID) {
3558 /* There is a valid meta sg */
3559 scsiio_req->flags |=
3560 cpu_to_le32(MPI3_SCSIIO_FLAGS_DMAOPERATION_HOST_PI);
3561 ret = mpi3mr_prepare_sg_scmd(mrioc, scmd, scsiio_req);
3568 * mpi3mr_tm_response_name - get TM response as a string
3569 * @resp_code: TM response code
3571 * Convert known task management response code as a readable
3574 * Return: response code string.
3576 static const char *mpi3mr_tm_response_name(u8 resp_code)
3580 switch (resp_code) {
3581 case MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE:
3582 desc = "task management request completed";
3584 case MPI3_SCSITASKMGMT_RSPCODE_INVALID_FRAME:
3585 desc = "invalid frame";
3587 case MPI3_SCSITASKMGMT_RSPCODE_TM_FUNCTION_NOT_SUPPORTED:
3588 desc = "task management request not supported";
3590 case MPI3_SCSITASKMGMT_RSPCODE_TM_FAILED:
3591 desc = "task management request failed";
3593 case MPI3_SCSITASKMGMT_RSPCODE_TM_SUCCEEDED:
3594 desc = "task management request succeeded";
3596 case MPI3_SCSITASKMGMT_RSPCODE_TM_INVALID_LUN:
3597 desc = "invalid LUN";
3599 case MPI3_SCSITASKMGMT_RSPCODE_TM_OVERLAPPED_TAG:
3600 desc = "overlapped tag attempted";
3602 case MPI3_SCSITASKMGMT_RSPCODE_IO_QUEUED_ON_IOC:
3603 desc = "task queued, however not sent to target";
3605 case MPI3_SCSITASKMGMT_RSPCODE_TM_NVME_DENIED:
3606 desc = "task management request denied by NVMe device";
3616 inline void mpi3mr_poll_pend_io_completions(struct mpi3mr_ioc *mrioc)
3619 int num_of_reply_queues =
3620 mrioc->num_op_reply_q + mrioc->op_reply_q_offset;
3622 for (i = mrioc->op_reply_q_offset; i < num_of_reply_queues; i++)
3623 mpi3mr_process_op_reply_q(mrioc,
3624 mrioc->intr_info[i].op_reply_q);
3628 * mpi3mr_issue_tm - Issue Task Management request
3629 * @mrioc: Adapter instance reference
3630 * @tm_type: Task Management type
3631 * @handle: Device handle
3633 * @htag: Host tag of the TM request
3634 * @timeout: TM timeout value
3635 * @drv_cmd: Internal command tracker
3636 * @resp_code: Response code place holder
3637 * @scmd: SCSI command
3639 * Issues a Task Management Request to the controller for a
3640 * specified target, lun and command and wait for its completion
3641 * and check TM response. Recover the TM if it timed out by
3642 * issuing controller reset.
3644 * Return: 0 on success, non-zero on errors
3646 int mpi3mr_issue_tm(struct mpi3mr_ioc *mrioc, u8 tm_type,
3647 u16 handle, uint lun, u16 htag, ulong timeout,
3648 struct mpi3mr_drv_cmd *drv_cmd,
3649 u8 *resp_code, struct scsi_cmnd *scmd)
3651 struct mpi3_scsi_task_mgmt_request tm_req;
3652 struct mpi3_scsi_task_mgmt_reply *tm_reply = NULL;
3654 struct mpi3mr_tgt_dev *tgtdev = NULL;
3655 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
3656 struct scmd_priv *cmd_priv = NULL;
3657 struct scsi_device *sdev = NULL;
3658 struct mpi3mr_sdev_priv_data *sdev_priv_data = NULL;
3660 ioc_info(mrioc, "%s :Issue TM: TM type (0x%x) for devhandle 0x%04x\n",
3661 __func__, tm_type, handle);
3662 if (mrioc->unrecoverable) {
3664 ioc_err(mrioc, "%s :Issue TM: Unrecoverable controller\n",
3669 memset(&tm_req, 0, sizeof(tm_req));
3670 mutex_lock(&drv_cmd->mutex);
3671 if (drv_cmd->state & MPI3MR_CMD_PENDING) {
3673 ioc_err(mrioc, "%s :Issue TM: Command is in use\n", __func__);
3674 mutex_unlock(&drv_cmd->mutex);
3677 if (mrioc->reset_in_progress) {
3679 ioc_err(mrioc, "%s :Issue TM: Reset in progress\n", __func__);
3680 mutex_unlock(&drv_cmd->mutex);
3684 drv_cmd->state = MPI3MR_CMD_PENDING;
3685 drv_cmd->is_waiting = 1;
3686 drv_cmd->callback = NULL;
3687 tm_req.dev_handle = cpu_to_le16(handle);
3688 tm_req.task_type = tm_type;
3689 tm_req.host_tag = cpu_to_le16(htag);
3691 int_to_scsilun(lun, (struct scsi_lun *)tm_req.lun);
3692 tm_req.function = MPI3_FUNCTION_SCSI_TASK_MGMT;
3694 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
3697 sdev = scmd->device;
3698 sdev_priv_data = sdev->hostdata;
3699 scsi_tgt_priv_data = ((sdev_priv_data) ?
3700 sdev_priv_data->tgt_priv_data : NULL);
3702 if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
3703 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
3704 tgtdev->starget->hostdata;
3707 if (scsi_tgt_priv_data)
3708 atomic_inc(&scsi_tgt_priv_data->block_io);
3710 if (tgtdev && (tgtdev->dev_type == MPI3_DEVICE_DEVFORM_PCIE)) {
3711 if (cmd_priv && tgtdev->dev_spec.pcie_inf.abort_to)
3712 timeout = tgtdev->dev_spec.pcie_inf.abort_to;
3713 else if (!cmd_priv && tgtdev->dev_spec.pcie_inf.reset_to)
3714 timeout = tgtdev->dev_spec.pcie_inf.reset_to;
3717 init_completion(&drv_cmd->done);
3718 retval = mpi3mr_admin_request_post(mrioc, &tm_req, sizeof(tm_req), 1);
3720 ioc_err(mrioc, "%s :Issue TM: Admin Post failed\n", __func__);
3723 wait_for_completion_timeout(&drv_cmd->done, (timeout * HZ));
3725 if (!(drv_cmd->state & MPI3MR_CMD_COMPLETE)) {
3726 drv_cmd->is_waiting = 0;
3728 if (!(drv_cmd->state & MPI3MR_CMD_RESET)) {
3730 "task management request timed out after %ld seconds\n",
3732 if (mrioc->logging_level & MPI3_DEBUG_TM)
3733 dprint_dump_req(&tm_req, sizeof(tm_req)/4);
3734 mpi3mr_soft_reset_handler(mrioc,
3735 MPI3MR_RESET_FROM_TM_TIMEOUT, 1);
3740 if (!(drv_cmd->state & MPI3MR_CMD_REPLY_VALID)) {
3741 dprint_tm(mrioc, "invalid task management reply message\n");
3746 tm_reply = (struct mpi3_scsi_task_mgmt_reply *)drv_cmd->reply;
3748 switch (drv_cmd->ioc_status) {
3749 case MPI3_IOCSTATUS_SUCCESS:
3750 *resp_code = le32_to_cpu(tm_reply->response_data) &
3751 MPI3MR_RI_MASK_RESPCODE;
3753 case MPI3_IOCSTATUS_SCSI_IOC_TERMINATED:
3754 *resp_code = MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE;
3758 "task management request to handle(0x%04x) is failed with ioc_status(0x%04x) log_info(0x%08x)\n",
3759 handle, drv_cmd->ioc_status, drv_cmd->ioc_loginfo);
3764 switch (*resp_code) {
3765 case MPI3_SCSITASKMGMT_RSPCODE_TM_SUCCEEDED:
3766 case MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE:
3768 case MPI3_SCSITASKMGMT_RSPCODE_IO_QUEUED_ON_IOC:
3769 if (tm_type != MPI3_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
3778 "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",
3779 tm_type, handle, drv_cmd->ioc_status, drv_cmd->ioc_loginfo,
3780 le32_to_cpu(tm_reply->termination_count),
3781 mpi3mr_tm_response_name(*resp_code), *resp_code);
3784 mpi3mr_ioc_disable_intr(mrioc);
3785 mpi3mr_poll_pend_io_completions(mrioc);
3786 mpi3mr_ioc_enable_intr(mrioc);
3787 mpi3mr_poll_pend_io_completions(mrioc);
3788 mpi3mr_process_admin_reply_q(mrioc);
3791 case MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
3792 if (!scsi_tgt_priv_data)
3794 scsi_tgt_priv_data->pend_count = 0;
3795 blk_mq_tagset_busy_iter(&mrioc->shost->tag_set,
3796 mpi3mr_count_tgt_pending,
3797 (void *)scsi_tgt_priv_data->starget);
3799 case MPI3_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
3800 if (!sdev_priv_data)
3802 sdev_priv_data->pend_count = 0;
3803 blk_mq_tagset_busy_iter(&mrioc->shost->tag_set,
3804 mpi3mr_count_dev_pending, (void *)sdev);
3811 drv_cmd->state = MPI3MR_CMD_NOTUSED;
3812 mutex_unlock(&drv_cmd->mutex);
3813 if (scsi_tgt_priv_data)
3814 atomic_dec_if_positive(&scsi_tgt_priv_data->block_io);
3816 mpi3mr_tgtdev_put(tgtdev);
3822 * mpi3mr_bios_param - BIOS param callback
3823 * @sdev: SCSI device reference
3824 * @bdev: Block device reference
3825 * @capacity: Capacity in logical sectors
3826 * @params: Parameter array
3828 * Just the parameters with heads/secots/cylinders.
3832 static int mpi3mr_bios_param(struct scsi_device *sdev,
3833 struct block_device *bdev, sector_t capacity, int params[])
3843 dummy = heads * sectors;
3844 cylinders = capacity;
3845 sector_div(cylinders, dummy);
3847 if ((ulong)capacity >= 0x200000) {
3850 dummy = heads * sectors;
3851 cylinders = capacity;
3852 sector_div(cylinders, dummy);
3856 params[1] = sectors;
3857 params[2] = cylinders;
3862 * mpi3mr_map_queues - Map queues callback handler
3863 * @shost: SCSI host reference
3865 * Maps default and poll queues.
3867 * Return: return zero.
3869 static void mpi3mr_map_queues(struct Scsi_Host *shost)
3871 struct mpi3mr_ioc *mrioc = shost_priv(shost);
3872 int i, qoff, offset;
3873 struct blk_mq_queue_map *map = NULL;
3875 offset = mrioc->op_reply_q_offset;
3877 for (i = 0, qoff = 0; i < HCTX_MAX_TYPES; i++) {
3878 map = &shost->tag_set.map[i];
3882 if (i == HCTX_TYPE_DEFAULT)
3883 map->nr_queues = mrioc->default_qcount;
3884 else if (i == HCTX_TYPE_POLL)
3885 map->nr_queues = mrioc->active_poll_qcount;
3887 if (!map->nr_queues) {
3888 BUG_ON(i == HCTX_TYPE_DEFAULT);
3893 * The poll queue(s) doesn't have an IRQ (and hence IRQ
3894 * affinity), so use the regular blk-mq cpu mapping
3896 map->queue_offset = qoff;
3897 if (i != HCTX_TYPE_POLL)
3898 blk_mq_pci_map_queues(map, mrioc->pdev, offset);
3900 blk_mq_map_queues(map);
3902 qoff += map->nr_queues;
3903 offset += map->nr_queues;
3908 * mpi3mr_get_fw_pending_ios - Calculate pending I/O count
3909 * @mrioc: Adapter instance reference
3911 * Calculate the pending I/Os for the controller and return.
3913 * Return: Number of pending I/Os
3915 static inline int mpi3mr_get_fw_pending_ios(struct mpi3mr_ioc *mrioc)
3920 for (i = 0; i < mrioc->num_op_reply_q; i++)
3921 pend_ios += atomic_read(&mrioc->op_reply_qinfo[i].pend_ios);
3926 * mpi3mr_print_pending_host_io - print pending I/Os
3927 * @mrioc: Adapter instance reference
3929 * Print number of pending I/Os and each I/O details prior to
3930 * reset for debug purpose.
3934 static void mpi3mr_print_pending_host_io(struct mpi3mr_ioc *mrioc)
3936 struct Scsi_Host *shost = mrioc->shost;
3938 ioc_info(mrioc, "%s :Pending commands prior to reset: %d\n",
3939 __func__, mpi3mr_get_fw_pending_ios(mrioc));
3940 blk_mq_tagset_busy_iter(&shost->tag_set,
3941 mpi3mr_print_scmd, (void *)mrioc);
3945 * mpi3mr_wait_for_host_io - block for I/Os to complete
3946 * @mrioc: Adapter instance reference
3947 * @timeout: time out in seconds
3948 * Waits for pending I/Os for the given adapter to complete or
3949 * to hit the timeout.
3953 void mpi3mr_wait_for_host_io(struct mpi3mr_ioc *mrioc, u32 timeout)
3955 enum mpi3mr_iocstate iocstate;
3958 iocstate = mpi3mr_get_iocstate(mrioc);
3959 if (iocstate != MRIOC_STATE_READY)
3962 if (!mpi3mr_get_fw_pending_ios(mrioc))
3965 "%s :Waiting for %d seconds prior to reset for %d I/O\n",
3966 __func__, timeout, mpi3mr_get_fw_pending_ios(mrioc));
3968 for (i = 0; i < timeout; i++) {
3969 if (!mpi3mr_get_fw_pending_ios(mrioc))
3971 iocstate = mpi3mr_get_iocstate(mrioc);
3972 if (iocstate != MRIOC_STATE_READY)
3977 ioc_info(mrioc, "%s :Pending I/Os after wait is: %d\n", __func__,
3978 mpi3mr_get_fw_pending_ios(mrioc));
3982 * mpi3mr_setup_divert_ws - Setup Divert IO flag for write same
3983 * @mrioc: Adapter instance reference
3984 * @scmd: SCSI command reference
3985 * @scsiio_req: MPI3 SCSI IO request
3986 * @scsiio_flags: Pointer to MPI3 SCSI IO Flags
3987 * @wslen: write same max length
3989 * Gets values of unmap, ndob and number of blocks from write
3990 * same scsi io and based on these values it sets divert IO flag
3991 * and reason for diverting IO to firmware.
3995 static inline void mpi3mr_setup_divert_ws(struct mpi3mr_ioc *mrioc,
3996 struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req,
3997 u32 *scsiio_flags, u16 wslen)
3999 u8 unmap = 0, ndob = 0;
4000 u8 opcode = scmd->cmnd[0];
4002 u16 sa = (scmd->cmnd[8] << 8) | (scmd->cmnd[9]);
4004 if (opcode == WRITE_SAME_16) {
4005 unmap = scmd->cmnd[1] & 0x08;
4006 ndob = scmd->cmnd[1] & 0x01;
4007 num_blocks = get_unaligned_be32(scmd->cmnd + 10);
4008 } else if ((opcode == VARIABLE_LENGTH_CMD) && (sa == WRITE_SAME_32)) {
4009 unmap = scmd->cmnd[10] & 0x08;
4010 ndob = scmd->cmnd[10] & 0x01;
4011 num_blocks = get_unaligned_be32(scmd->cmnd + 28);
4015 if ((unmap) && (ndob) && (num_blocks > wslen)) {
4016 scsiio_req->msg_flags |=
4017 MPI3_SCSIIO_MSGFLAGS_DIVERT_TO_FIRMWARE;
4019 MPI3_SCSIIO_FLAGS_DIVERT_REASON_WRITE_SAME_TOO_LARGE;
4024 * mpi3mr_eh_host_reset - Host reset error handling callback
4025 * @scmd: SCSI command reference
4027 * Issue controller reset
4029 * Return: SUCCESS of successful reset else FAILED
4031 static int mpi3mr_eh_host_reset(struct scsi_cmnd *scmd)
4033 struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
4034 int retval = FAILED, ret;
4036 ret = mpi3mr_soft_reset_handler(mrioc,
4037 MPI3MR_RESET_FROM_EH_HOS, 1);
4043 sdev_printk(KERN_INFO, scmd->device,
4044 "Host reset is %s for scmd(%p)\n",
4045 ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4051 * mpi3mr_eh_bus_reset - Bus reset error handling callback
4052 * @scmd: SCSI command reference
4054 * Checks whether pending I/Os are present for the RAID volume;
4055 * if not there's no need to reset the adapter.
4057 * Return: SUCCESS of successful reset else FAILED
4059 static int mpi3mr_eh_bus_reset(struct scsi_cmnd *scmd)
4061 struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
4062 struct mpi3mr_stgt_priv_data *stgt_priv_data;
4063 struct mpi3mr_sdev_priv_data *sdev_priv_data;
4064 u8 dev_type = MPI3_DEVICE_DEVFORM_VD;
4065 int retval = FAILED;
4067 sdev_priv_data = scmd->device->hostdata;
4068 if (sdev_priv_data && sdev_priv_data->tgt_priv_data) {
4069 stgt_priv_data = sdev_priv_data->tgt_priv_data;
4070 dev_type = stgt_priv_data->dev_type;
4073 if (dev_type == MPI3_DEVICE_DEVFORM_VD) {
4074 mpi3mr_wait_for_host_io(mrioc,
4075 MPI3MR_RAID_ERRREC_RESET_TIMEOUT);
4076 if (!mpi3mr_get_fw_pending_ios(mrioc))
4079 if (retval == FAILED)
4080 mpi3mr_print_pending_host_io(mrioc);
4082 sdev_printk(KERN_INFO, scmd->device,
4083 "Bus reset is %s for scmd(%p)\n",
4084 ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4089 * mpi3mr_eh_target_reset - Target reset error handling callback
4090 * @scmd: SCSI command reference
4092 * Issue Target reset Task Management and verify the scmd is
4093 * terminated successfully and return status accordingly.
4095 * Return: SUCCESS of successful termination of the scmd else
4098 static int mpi3mr_eh_target_reset(struct scsi_cmnd *scmd)
4100 struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
4101 struct mpi3mr_stgt_priv_data *stgt_priv_data;
4102 struct mpi3mr_sdev_priv_data *sdev_priv_data;
4105 int retval = FAILED, ret = 0;
4107 sdev_printk(KERN_INFO, scmd->device,
4108 "Attempting Target Reset! scmd(%p)\n", scmd);
4109 scsi_print_command(scmd);
4111 sdev_priv_data = scmd->device->hostdata;
4112 if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
4113 sdev_printk(KERN_INFO, scmd->device,
4114 "SCSI device is not available\n");
4119 stgt_priv_data = sdev_priv_data->tgt_priv_data;
4120 dev_handle = stgt_priv_data->dev_handle;
4121 if (stgt_priv_data->dev_removed) {
4122 struct scmd_priv *cmd_priv = scsi_cmd_priv(scmd);
4123 sdev_printk(KERN_INFO, scmd->device,
4124 "%s:target(handle = 0x%04x) is removed, target reset is not issued\n",
4125 mrioc->name, dev_handle);
4126 if (!cmd_priv->in_lld_scope || cmd_priv->host_tag == MPI3MR_HOSTTAG_INVALID)
4132 sdev_printk(KERN_INFO, scmd->device,
4133 "Target Reset is issued to handle(0x%04x)\n",
4136 ret = mpi3mr_issue_tm(mrioc,
4137 MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET, dev_handle,
4138 sdev_priv_data->lun_id, MPI3MR_HOSTTAG_BLK_TMS,
4139 MPI3MR_RESETTM_TIMEOUT, &mrioc->host_tm_cmds, &resp_code, scmd);
4144 if (stgt_priv_data->pend_count) {
4145 sdev_printk(KERN_INFO, scmd->device,
4146 "%s: target has %d pending commands, target reset is failed\n",
4147 mrioc->name, stgt_priv_data->pend_count);
4153 sdev_printk(KERN_INFO, scmd->device,
4154 "%s: target reset is %s for scmd(%p)\n", mrioc->name,
4155 ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4161 * mpi3mr_eh_dev_reset- Device reset error handling callback
4162 * @scmd: SCSI command reference
4164 * Issue lun reset Task Management and verify the scmd is
4165 * terminated successfully and return status accordingly.
4167 * Return: SUCCESS of successful termination of the scmd else
4170 static int mpi3mr_eh_dev_reset(struct scsi_cmnd *scmd)
4172 struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
4173 struct mpi3mr_stgt_priv_data *stgt_priv_data;
4174 struct mpi3mr_sdev_priv_data *sdev_priv_data;
4177 int retval = FAILED, ret = 0;
4179 sdev_printk(KERN_INFO, scmd->device,
4180 "Attempting Device(lun) Reset! scmd(%p)\n", scmd);
4181 scsi_print_command(scmd);
4183 sdev_priv_data = scmd->device->hostdata;
4184 if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
4185 sdev_printk(KERN_INFO, scmd->device,
4186 "SCSI device is not available\n");
4191 stgt_priv_data = sdev_priv_data->tgt_priv_data;
4192 dev_handle = stgt_priv_data->dev_handle;
4193 if (stgt_priv_data->dev_removed) {
4194 struct scmd_priv *cmd_priv = scsi_cmd_priv(scmd);
4195 sdev_printk(KERN_INFO, scmd->device,
4196 "%s: device(handle = 0x%04x) is removed, device(LUN) reset is not issued\n",
4197 mrioc->name, dev_handle);
4198 if (!cmd_priv->in_lld_scope || cmd_priv->host_tag == MPI3MR_HOSTTAG_INVALID)
4204 sdev_printk(KERN_INFO, scmd->device,
4205 "Device(lun) Reset is issued to handle(0x%04x)\n", dev_handle);
4207 ret = mpi3mr_issue_tm(mrioc,
4208 MPI3_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, dev_handle,
4209 sdev_priv_data->lun_id, MPI3MR_HOSTTAG_BLK_TMS,
4210 MPI3MR_RESETTM_TIMEOUT, &mrioc->host_tm_cmds, &resp_code, scmd);
4215 if (sdev_priv_data->pend_count) {
4216 sdev_printk(KERN_INFO, scmd->device,
4217 "%s: device has %d pending commands, device(LUN) reset is failed\n",
4218 mrioc->name, sdev_priv_data->pend_count);
4223 sdev_printk(KERN_INFO, scmd->device,
4224 "%s: device(LUN) reset is %s for scmd(%p)\n", mrioc->name,
4225 ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4231 * mpi3mr_scan_start - Scan start callback handler
4232 * @shost: SCSI host reference
4234 * Issue port enable request asynchronously.
4238 static void mpi3mr_scan_start(struct Scsi_Host *shost)
4240 struct mpi3mr_ioc *mrioc = shost_priv(shost);
4242 mrioc->scan_started = 1;
4243 ioc_info(mrioc, "%s :Issuing Port Enable\n", __func__);
4244 if (mpi3mr_issue_port_enable(mrioc, 1)) {
4245 ioc_err(mrioc, "%s :Issuing port enable failed\n", __func__);
4246 mrioc->scan_started = 0;
4247 mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
4252 * mpi3mr_scan_finished - Scan finished callback handler
4253 * @shost: SCSI host reference
4254 * @time: Jiffies from the scan start
4256 * Checks whether the port enable is completed or timedout or
4257 * failed and set the scan status accordingly after taking any
4258 * recovery if required.
4260 * Return: 1 on scan finished or timed out, 0 for in progress
4262 static int mpi3mr_scan_finished(struct Scsi_Host *shost,
4265 struct mpi3mr_ioc *mrioc = shost_priv(shost);
4266 u32 pe_timeout = MPI3MR_PORTENABLE_TIMEOUT;
4267 u32 ioc_status = readl(&mrioc->sysif_regs->ioc_status);
4269 if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) ||
4270 (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)) {
4271 ioc_err(mrioc, "port enable failed due to fault or reset\n");
4272 mpi3mr_print_fault_info(mrioc);
4273 mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
4274 mrioc->scan_started = 0;
4275 mrioc->init_cmds.is_waiting = 0;
4276 mrioc->init_cmds.callback = NULL;
4277 mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
4280 if (time >= (pe_timeout * HZ)) {
4281 ioc_err(mrioc, "port enable failed due to time out\n");
4282 mpi3mr_check_rh_fault_ioc(mrioc,
4283 MPI3MR_RESET_FROM_PE_TIMEOUT);
4284 mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
4285 mrioc->scan_started = 0;
4286 mrioc->init_cmds.is_waiting = 0;
4287 mrioc->init_cmds.callback = NULL;
4288 mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
4291 if (mrioc->scan_started)
4294 if (mrioc->scan_failed) {
4296 "port enable failed with status=0x%04x\n",
4297 mrioc->scan_failed);
4299 ioc_info(mrioc, "port enable is successfully completed\n");
4301 mpi3mr_start_watchdog(mrioc);
4302 mrioc->is_driver_loading = 0;
4303 mrioc->stop_bsgs = 0;
4308 * mpi3mr_slave_destroy - Slave destroy callback handler
4309 * @sdev: SCSI device reference
4311 * Cleanup and free per device(lun) private data.
4315 static void mpi3mr_slave_destroy(struct scsi_device *sdev)
4317 struct Scsi_Host *shost;
4318 struct mpi3mr_ioc *mrioc;
4319 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4320 struct mpi3mr_tgt_dev *tgt_dev = NULL;
4321 unsigned long flags;
4322 struct scsi_target *starget;
4323 struct sas_rphy *rphy = NULL;
4325 if (!sdev->hostdata)
4328 starget = scsi_target(sdev);
4329 shost = dev_to_shost(&starget->dev);
4330 mrioc = shost_priv(shost);
4331 scsi_tgt_priv_data = starget->hostdata;
4333 scsi_tgt_priv_data->num_luns--;
4335 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4336 if (starget->channel == mrioc->scsi_device_channel)
4337 tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4338 else if (mrioc->sas_transport_enabled && !starget->channel) {
4339 rphy = dev_to_rphy(starget->dev.parent);
4340 tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
4341 rphy->identify.sas_address, rphy);
4344 if (tgt_dev && (!scsi_tgt_priv_data->num_luns))
4345 tgt_dev->starget = NULL;
4347 mpi3mr_tgtdev_put(tgt_dev);
4348 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4350 kfree(sdev->hostdata);
4351 sdev->hostdata = NULL;
4355 * mpi3mr_target_destroy - Target destroy callback handler
4356 * @starget: SCSI target reference
4358 * Cleanup and free per target private data.
4362 static void mpi3mr_target_destroy(struct scsi_target *starget)
4364 struct Scsi_Host *shost;
4365 struct mpi3mr_ioc *mrioc;
4366 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4367 struct mpi3mr_tgt_dev *tgt_dev;
4368 unsigned long flags;
4370 if (!starget->hostdata)
4373 shost = dev_to_shost(&starget->dev);
4374 mrioc = shost_priv(shost);
4375 scsi_tgt_priv_data = starget->hostdata;
4377 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4378 tgt_dev = __mpi3mr_get_tgtdev_from_tgtpriv(mrioc, scsi_tgt_priv_data);
4379 if (tgt_dev && (tgt_dev->starget == starget) &&
4380 (tgt_dev->perst_id == starget->id))
4381 tgt_dev->starget = NULL;
4383 scsi_tgt_priv_data->tgt_dev = NULL;
4384 scsi_tgt_priv_data->perst_id = 0;
4385 mpi3mr_tgtdev_put(tgt_dev);
4386 mpi3mr_tgtdev_put(tgt_dev);
4388 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4390 kfree(starget->hostdata);
4391 starget->hostdata = NULL;
4395 * mpi3mr_slave_configure - Slave configure callback handler
4396 * @sdev: SCSI device reference
4398 * Configure queue depth, max hardware sectors and virt boundary
4403 static int mpi3mr_slave_configure(struct scsi_device *sdev)
4405 struct scsi_target *starget;
4406 struct Scsi_Host *shost;
4407 struct mpi3mr_ioc *mrioc;
4408 struct mpi3mr_tgt_dev *tgt_dev = NULL;
4409 unsigned long flags;
4411 struct sas_rphy *rphy = NULL;
4413 starget = scsi_target(sdev);
4414 shost = dev_to_shost(&starget->dev);
4415 mrioc = shost_priv(shost);
4417 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4418 if (starget->channel == mrioc->scsi_device_channel)
4419 tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4420 else if (mrioc->sas_transport_enabled && !starget->channel) {
4421 rphy = dev_to_rphy(starget->dev.parent);
4422 tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
4423 rphy->identify.sas_address, rphy);
4425 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4429 mpi3mr_change_queue_depth(sdev, tgt_dev->q_depth);
4431 sdev->eh_timeout = MPI3MR_EH_SCMD_TIMEOUT;
4432 blk_queue_rq_timeout(sdev->request_queue, MPI3MR_SCMD_TIMEOUT);
4434 switch (tgt_dev->dev_type) {
4435 case MPI3_DEVICE_DEVFORM_PCIE:
4436 /*The block layer hw sector size = 512*/
4437 if ((tgt_dev->dev_spec.pcie_inf.dev_info &
4438 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) ==
4439 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) {
4440 blk_queue_max_hw_sectors(sdev->request_queue,
4441 tgt_dev->dev_spec.pcie_inf.mdts / 512);
4442 if (tgt_dev->dev_spec.pcie_inf.pgsz == 0)
4443 blk_queue_virt_boundary(sdev->request_queue,
4444 ((1 << MPI3MR_DEFAULT_PGSZEXP) - 1));
4446 blk_queue_virt_boundary(sdev->request_queue,
4447 ((1 << tgt_dev->dev_spec.pcie_inf.pgsz) - 1));
4454 mpi3mr_tgtdev_put(tgt_dev);
4460 * mpi3mr_slave_alloc -Slave alloc callback handler
4461 * @sdev: SCSI device reference
4463 * Allocate per device(lun) private data and initialize it.
4465 * Return: 0 on success -ENOMEM on memory allocation failure.
4467 static int mpi3mr_slave_alloc(struct scsi_device *sdev)
4469 struct Scsi_Host *shost;
4470 struct mpi3mr_ioc *mrioc;
4471 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4472 struct mpi3mr_tgt_dev *tgt_dev = NULL;
4473 struct mpi3mr_sdev_priv_data *scsi_dev_priv_data;
4474 unsigned long flags;
4475 struct scsi_target *starget;
4477 struct sas_rphy *rphy = NULL;
4479 starget = scsi_target(sdev);
4480 shost = dev_to_shost(&starget->dev);
4481 mrioc = shost_priv(shost);
4482 scsi_tgt_priv_data = starget->hostdata;
4484 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4486 if (starget->channel == mrioc->scsi_device_channel)
4487 tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4488 else if (mrioc->sas_transport_enabled && !starget->channel) {
4489 rphy = dev_to_rphy(starget->dev.parent);
4490 tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
4491 rphy->identify.sas_address, rphy);
4495 if (tgt_dev->starget == NULL)
4496 tgt_dev->starget = starget;
4497 mpi3mr_tgtdev_put(tgt_dev);
4500 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4504 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4506 scsi_dev_priv_data = kzalloc(sizeof(*scsi_dev_priv_data), GFP_KERNEL);
4507 if (!scsi_dev_priv_data)
4510 scsi_dev_priv_data->lun_id = sdev->lun;
4511 scsi_dev_priv_data->tgt_priv_data = scsi_tgt_priv_data;
4512 sdev->hostdata = scsi_dev_priv_data;
4514 scsi_tgt_priv_data->num_luns++;
4520 * mpi3mr_target_alloc - Target alloc callback handler
4521 * @starget: SCSI target reference
4523 * Allocate per target private data and initialize it.
4525 * Return: 0 on success -ENOMEM on memory allocation failure.
4527 static int mpi3mr_target_alloc(struct scsi_target *starget)
4529 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4530 struct mpi3mr_ioc *mrioc = shost_priv(shost);
4531 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4532 struct mpi3mr_tgt_dev *tgt_dev;
4533 unsigned long flags;
4535 struct sas_rphy *rphy = NULL;
4537 scsi_tgt_priv_data = kzalloc(sizeof(*scsi_tgt_priv_data), GFP_KERNEL);
4538 if (!scsi_tgt_priv_data)
4541 starget->hostdata = scsi_tgt_priv_data;
4543 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4544 if (starget->channel == mrioc->scsi_device_channel) {
4545 tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4546 if (tgt_dev && !tgt_dev->is_hidden) {
4547 scsi_tgt_priv_data->starget = starget;
4548 scsi_tgt_priv_data->dev_handle = tgt_dev->dev_handle;
4549 scsi_tgt_priv_data->perst_id = tgt_dev->perst_id;
4550 scsi_tgt_priv_data->dev_type = tgt_dev->dev_type;
4551 scsi_tgt_priv_data->tgt_dev = tgt_dev;
4552 tgt_dev->starget = starget;
4553 atomic_set(&scsi_tgt_priv_data->block_io, 0);
4555 if ((tgt_dev->dev_type == MPI3_DEVICE_DEVFORM_PCIE) &&
4556 ((tgt_dev->dev_spec.pcie_inf.dev_info &
4557 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) ==
4558 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) &&
4559 ((tgt_dev->dev_spec.pcie_inf.dev_info &
4560 MPI3_DEVICE0_PCIE_DEVICE_INFO_PITYPE_MASK) !=
4561 MPI3_DEVICE0_PCIE_DEVICE_INFO_PITYPE_0))
4562 scsi_tgt_priv_data->dev_nvme_dif = 1;
4563 scsi_tgt_priv_data->io_throttle_enabled = tgt_dev->io_throttle_enabled;
4564 scsi_tgt_priv_data->wslen = tgt_dev->wslen;
4565 if (tgt_dev->dev_type == MPI3_DEVICE_DEVFORM_VD)
4566 scsi_tgt_priv_data->throttle_group = tgt_dev->dev_spec.vd_inf.tg;
4569 } else if (mrioc->sas_transport_enabled && !starget->channel) {
4570 rphy = dev_to_rphy(starget->dev.parent);
4571 tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
4572 rphy->identify.sas_address, rphy);
4573 if (tgt_dev && !tgt_dev->is_hidden && !tgt_dev->non_stl &&
4574 (tgt_dev->dev_type == MPI3_DEVICE_DEVFORM_SAS_SATA)) {
4575 scsi_tgt_priv_data->starget = starget;
4576 scsi_tgt_priv_data->dev_handle = tgt_dev->dev_handle;
4577 scsi_tgt_priv_data->perst_id = tgt_dev->perst_id;
4578 scsi_tgt_priv_data->dev_type = tgt_dev->dev_type;
4579 scsi_tgt_priv_data->tgt_dev = tgt_dev;
4580 scsi_tgt_priv_data->io_throttle_enabled = tgt_dev->io_throttle_enabled;
4581 scsi_tgt_priv_data->wslen = tgt_dev->wslen;
4582 tgt_dev->starget = starget;
4583 atomic_set(&scsi_tgt_priv_data->block_io, 0);
4588 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4594 * mpi3mr_check_return_unmap - Whether an unmap is allowed
4595 * @mrioc: Adapter instance reference
4596 * @scmd: SCSI Command reference
4598 * The controller hardware cannot handle certain unmap commands
4599 * for NVMe drives, this routine checks those and return true
4600 * and completes the SCSI command with proper status and sense
4603 * Return: TRUE for not allowed unmap, FALSE otherwise.
4605 static bool mpi3mr_check_return_unmap(struct mpi3mr_ioc *mrioc,
4606 struct scsi_cmnd *scmd)
4609 u16 param_len, desc_len, trunc_param_len;
4611 trunc_param_len = param_len = get_unaligned_be16(scmd->cmnd + 7);
4613 if (mrioc->pdev->revision) {
4614 if ((param_len > 24) && ((param_len - 8) & 0xF)) {
4615 trunc_param_len -= (param_len - 8) & 0xF;
4616 dprint_scsi_command(mrioc, scmd, MPI3_DEBUG_SCSI_ERROR);
4617 dprint_scsi_err(mrioc,
4618 "truncating param_len from (%d) to (%d)\n",
4619 param_len, trunc_param_len);
4620 put_unaligned_be16(trunc_param_len, scmd->cmnd + 7);
4621 dprint_scsi_command(mrioc, scmd, MPI3_DEBUG_SCSI_ERROR);
4628 "%s: cdb received with zero parameter length\n",
4630 scsi_print_command(scmd);
4631 scmd->result = DID_OK << 16;
4636 if (param_len < 24) {
4638 "%s: cdb received with invalid param_len: %d\n",
4639 __func__, param_len);
4640 scsi_print_command(scmd);
4641 scmd->result = SAM_STAT_CHECK_CONDITION;
4642 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
4647 if (param_len != scsi_bufflen(scmd)) {
4649 "%s: cdb received with param_len: %d bufflen: %d\n",
4650 __func__, param_len, scsi_bufflen(scmd));
4651 scsi_print_command(scmd);
4652 scmd->result = SAM_STAT_CHECK_CONDITION;
4653 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
4658 buf = kzalloc(scsi_bufflen(scmd), GFP_ATOMIC);
4660 scsi_print_command(scmd);
4661 scmd->result = SAM_STAT_CHECK_CONDITION;
4662 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
4667 scsi_sg_copy_to_buffer(scmd, buf, scsi_bufflen(scmd));
4668 desc_len = get_unaligned_be16(&buf[2]);
4670 if (desc_len < 16) {
4672 "%s: Invalid descriptor length in param list: %d\n",
4673 __func__, desc_len);
4674 scsi_print_command(scmd);
4675 scmd->result = SAM_STAT_CHECK_CONDITION;
4676 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
4683 if (param_len > (desc_len + 8)) {
4684 trunc_param_len = desc_len + 8;
4685 scsi_print_command(scmd);
4686 dprint_scsi_err(mrioc,
4687 "truncating param_len(%d) to desc_len+8(%d)\n",
4688 param_len, trunc_param_len);
4689 put_unaligned_be16(trunc_param_len, scmd->cmnd + 7);
4690 scsi_print_command(scmd);
4698 * mpi3mr_allow_scmd_to_fw - Command is allowed during shutdown
4699 * @scmd: SCSI Command reference
4701 * Checks whether a cdb is allowed during shutdown or not.
4703 * Return: TRUE for allowed commands, FALSE otherwise.
4706 inline bool mpi3mr_allow_scmd_to_fw(struct scsi_cmnd *scmd)
4708 switch (scmd->cmnd[0]) {
4709 case SYNCHRONIZE_CACHE:
4718 * mpi3mr_qcmd - I/O request despatcher
4719 * @shost: SCSI Host reference
4720 * @scmd: SCSI Command reference
4722 * Issues the SCSI Command as an MPI3 request.
4724 * Return: 0 on successful queueing of the request or if the
4725 * request is completed with failure.
4726 * SCSI_MLQUEUE_DEVICE_BUSY when the device is busy.
4727 * SCSI_MLQUEUE_HOST_BUSY when the host queue is full.
4729 static int mpi3mr_qcmd(struct Scsi_Host *shost,
4730 struct scsi_cmnd *scmd)
4732 struct mpi3mr_ioc *mrioc = shost_priv(shost);
4733 struct mpi3mr_stgt_priv_data *stgt_priv_data;
4734 struct mpi3mr_sdev_priv_data *sdev_priv_data;
4735 struct scmd_priv *scmd_priv_data = NULL;
4736 struct mpi3_scsi_io_request *scsiio_req = NULL;
4737 struct op_req_qinfo *op_req_q = NULL;
4741 u32 scsiio_flags = 0, data_len_blks = 0;
4742 struct request *rq = scsi_cmd_to_rq(scmd);
4745 u32 tracked_io_sz = 0;
4746 u32 ioc_pend_data_len = 0, tg_pend_data_len = 0;
4747 struct mpi3mr_throttle_group_info *tg = NULL;
4749 if (mrioc->unrecoverable) {
4750 scmd->result = DID_ERROR << 16;
4755 sdev_priv_data = scmd->device->hostdata;
4756 if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
4757 scmd->result = DID_NO_CONNECT << 16;
4762 if (mrioc->stop_drv_processing &&
4763 !(mpi3mr_allow_scmd_to_fw(scmd))) {
4764 scmd->result = DID_NO_CONNECT << 16;
4769 stgt_priv_data = sdev_priv_data->tgt_priv_data;
4770 dev_handle = stgt_priv_data->dev_handle;
4772 /* Avoid error handling escalation when device is removed or blocked */
4774 if (scmd->device->host->shost_state == SHOST_RECOVERY &&
4775 scmd->cmnd[0] == TEST_UNIT_READY &&
4776 (stgt_priv_data->dev_removed || (dev_handle == MPI3MR_INVALID_DEV_HANDLE))) {
4777 scsi_build_sense(scmd, 0, UNIT_ATTENTION, 0x29, 0x07);
4782 if (mrioc->reset_in_progress) {
4783 retval = SCSI_MLQUEUE_HOST_BUSY;
4787 if (atomic_read(&stgt_priv_data->block_io)) {
4788 if (mrioc->stop_drv_processing) {
4789 scmd->result = DID_NO_CONNECT << 16;
4793 retval = SCSI_MLQUEUE_DEVICE_BUSY;
4797 if (dev_handle == MPI3MR_INVALID_DEV_HANDLE) {
4798 scmd->result = DID_NO_CONNECT << 16;
4802 if (stgt_priv_data->dev_removed) {
4803 scmd->result = DID_NO_CONNECT << 16;
4808 if (stgt_priv_data->dev_type == MPI3_DEVICE_DEVFORM_PCIE)
4810 if ((scmd->cmnd[0] == UNMAP) && is_pcie_dev &&
4811 (mrioc->pdev->device == MPI3_MFGPAGE_DEVID_SAS4116) &&
4812 mpi3mr_check_return_unmap(mrioc, scmd))
4815 host_tag = mpi3mr_host_tag_for_scmd(mrioc, scmd);
4816 if (host_tag == MPI3MR_HOSTTAG_INVALID) {
4817 scmd->result = DID_ERROR << 16;
4822 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
4823 scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_READ;
4824 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
4825 scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_WRITE;
4827 scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_NO_DATA_TRANSFER;
4829 scsiio_flags |= MPI3_SCSIIO_FLAGS_TASKATTRIBUTE_SIMPLEQ;
4831 if (sdev_priv_data->ncq_prio_enable) {
4832 iprio_class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
4833 if (iprio_class == IOPRIO_CLASS_RT)
4834 scsiio_flags |= 1 << MPI3_SCSIIO_FLAGS_CMDPRI_SHIFT;
4837 if (scmd->cmd_len > 16)
4838 scsiio_flags |= MPI3_SCSIIO_FLAGS_CDB_GREATER_THAN_16;
4840 scmd_priv_data = scsi_cmd_priv(scmd);
4841 memset(scmd_priv_data->mpi3mr_scsiio_req, 0, MPI3MR_ADMIN_REQ_FRAME_SZ);
4842 scsiio_req = (struct mpi3_scsi_io_request *)scmd_priv_data->mpi3mr_scsiio_req;
4843 scsiio_req->function = MPI3_FUNCTION_SCSI_IO;
4844 scsiio_req->host_tag = cpu_to_le16(host_tag);
4846 mpi3mr_setup_eedp(mrioc, scmd, scsiio_req);
4848 if (stgt_priv_data->wslen)
4849 mpi3mr_setup_divert_ws(mrioc, scmd, scsiio_req, &scsiio_flags,
4850 stgt_priv_data->wslen);
4852 memcpy(scsiio_req->cdb.cdb32, scmd->cmnd, scmd->cmd_len);
4853 scsiio_req->data_length = cpu_to_le32(scsi_bufflen(scmd));
4854 scsiio_req->dev_handle = cpu_to_le16(dev_handle);
4855 scsiio_req->flags = cpu_to_le32(scsiio_flags);
4856 int_to_scsilun(sdev_priv_data->lun_id,
4857 (struct scsi_lun *)scsiio_req->lun);
4859 if (mpi3mr_build_sg_scmd(mrioc, scmd, scsiio_req)) {
4860 mpi3mr_clear_scmd_priv(mrioc, scmd);
4861 retval = SCSI_MLQUEUE_HOST_BUSY;
4864 op_req_q = &mrioc->req_qinfo[scmd_priv_data->req_q_idx];
4865 data_len_blks = scsi_bufflen(scmd) >> 9;
4866 if ((data_len_blks >= mrioc->io_throttle_data_length) &&
4867 stgt_priv_data->io_throttle_enabled) {
4868 tracked_io_sz = data_len_blks;
4869 tg = stgt_priv_data->throttle_group;
4871 ioc_pend_data_len = atomic_add_return(data_len_blks,
4872 &mrioc->pend_large_data_sz);
4873 tg_pend_data_len = atomic_add_return(data_len_blks,
4874 &tg->pend_large_data_sz);
4875 if (!tg->io_divert && ((ioc_pend_data_len >=
4876 mrioc->io_throttle_high) ||
4877 (tg_pend_data_len >= tg->high))) {
4879 tg->need_qd_reduction = 1;
4880 mpi3mr_set_io_divert_for_all_vd_in_tg(mrioc,
4882 mpi3mr_queue_qd_reduction_event(mrioc, tg);
4885 ioc_pend_data_len = atomic_add_return(data_len_blks,
4886 &mrioc->pend_large_data_sz);
4887 if (ioc_pend_data_len >= mrioc->io_throttle_high)
4888 stgt_priv_data->io_divert = 1;
4892 if (stgt_priv_data->io_divert) {
4893 scsiio_req->msg_flags |=
4894 MPI3_SCSIIO_MSGFLAGS_DIVERT_TO_FIRMWARE;
4895 scsiio_flags |= MPI3_SCSIIO_FLAGS_DIVERT_REASON_IO_THROTTLING;
4897 scsiio_req->flags = cpu_to_le32(scsiio_flags);
4899 if (mpi3mr_op_request_post(mrioc, op_req_q,
4900 scmd_priv_data->mpi3mr_scsiio_req)) {
4901 mpi3mr_clear_scmd_priv(mrioc, scmd);
4902 retval = SCSI_MLQUEUE_HOST_BUSY;
4903 if (tracked_io_sz) {
4904 atomic_sub(tracked_io_sz, &mrioc->pend_large_data_sz);
4906 atomic_sub(tracked_io_sz,
4907 &tg->pend_large_data_sz);
4916 static const struct scsi_host_template mpi3mr_driver_template = {
4917 .module = THIS_MODULE,
4918 .name = "MPI3 Storage Controller",
4919 .proc_name = MPI3MR_DRIVER_NAME,
4920 .queuecommand = mpi3mr_qcmd,
4921 .target_alloc = mpi3mr_target_alloc,
4922 .slave_alloc = mpi3mr_slave_alloc,
4923 .slave_configure = mpi3mr_slave_configure,
4924 .target_destroy = mpi3mr_target_destroy,
4925 .slave_destroy = mpi3mr_slave_destroy,
4926 .scan_finished = mpi3mr_scan_finished,
4927 .scan_start = mpi3mr_scan_start,
4928 .change_queue_depth = mpi3mr_change_queue_depth,
4929 .eh_device_reset_handler = mpi3mr_eh_dev_reset,
4930 .eh_target_reset_handler = mpi3mr_eh_target_reset,
4931 .eh_bus_reset_handler = mpi3mr_eh_bus_reset,
4932 .eh_host_reset_handler = mpi3mr_eh_host_reset,
4933 .bios_param = mpi3mr_bios_param,
4934 .map_queues = mpi3mr_map_queues,
4935 .mq_poll = mpi3mr_blk_mq_poll,
4939 .sg_tablesize = MPI3MR_DEFAULT_SGL_ENTRIES,
4940 /* max xfer supported is 1M (2K in 512 byte sized sectors)
4942 .max_sectors = (MPI3MR_DEFAULT_MAX_IO_SIZE / 512),
4943 .cmd_per_lun = MPI3MR_MAX_CMDS_LUN,
4944 .max_segment_size = 0xffffffff,
4945 .track_queue_depth = 1,
4946 .cmd_size = sizeof(struct scmd_priv),
4947 .shost_groups = mpi3mr_host_groups,
4948 .sdev_groups = mpi3mr_dev_groups,
4952 * mpi3mr_init_drv_cmd - Initialize internal command tracker
4953 * @cmdptr: Internal command tracker
4954 * @host_tag: Host tag used for the specific command
4956 * Initialize the internal command tracker structure with
4957 * specified host tag.
4961 static inline void mpi3mr_init_drv_cmd(struct mpi3mr_drv_cmd *cmdptr,
4964 mutex_init(&cmdptr->mutex);
4965 cmdptr->reply = NULL;
4966 cmdptr->state = MPI3MR_CMD_NOTUSED;
4967 cmdptr->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
4968 cmdptr->host_tag = host_tag;
4972 * osintfc_mrioc_security_status -Check controller secure status
4973 * @pdev: PCI device instance
4975 * Read the Device Serial Number capability from PCI config
4976 * space and decide whether the controller is secure or not.
4978 * Return: 0 on success, non-zero on failure.
4981 osintfc_mrioc_security_status(struct pci_dev *pdev)
4989 base = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DSN);
4992 "%s: PCI_EXT_CAP_ID_DSN is not supported\n", __func__);
4996 pci_read_config_dword(pdev, base + 4, &cap_data);
4998 debug_status = cap_data & MPI3MR_CTLR_SECURE_DBG_STATUS_MASK;
4999 ctlr_status = cap_data & MPI3MR_CTLR_SECURITY_STATUS_MASK;
5001 switch (ctlr_status) {
5002 case MPI3MR_INVALID_DEVICE:
5004 "%s: Non secure ctlr (Invalid) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
5005 __func__, pdev->device, pdev->subsystem_vendor,
5006 pdev->subsystem_device);
5009 case MPI3MR_CONFIG_SECURE_DEVICE:
5011 dev_info(&pdev->dev,
5012 "%s: Config secure ctlr is detected\n",
5015 case MPI3MR_HARD_SECURE_DEVICE:
5017 case MPI3MR_TAMPERED_DEVICE:
5019 "%s: Non secure ctlr (Tampered) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
5020 __func__, pdev->device, pdev->subsystem_vendor,
5021 pdev->subsystem_device);
5029 if (!retval && debug_status) {
5031 "%s: Non secure ctlr (Secure Dbg) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
5032 __func__, pdev->device, pdev->subsystem_vendor,
5033 pdev->subsystem_device);
5041 * mpi3mr_probe - PCI probe callback
5042 * @pdev: PCI device instance
5043 * @id: PCI device ID details
5045 * controller initialization routine. Checks the security status
5046 * of the controller and if it is invalid or tampered return the
5047 * probe without initializing the controller. Otherwise,
5048 * allocate per adapter instance through shost_priv and
5049 * initialize controller specific data structures, initializae
5050 * the controller hardware, add shost to the SCSI subsystem.
5052 * Return: 0 on success, non-zero on failure.
5056 mpi3mr_probe(struct pci_dev *pdev, const struct pci_device_id *id)
5058 struct mpi3mr_ioc *mrioc = NULL;
5059 struct Scsi_Host *shost = NULL;
5062 if (osintfc_mrioc_security_status(pdev)) {
5063 warn_non_secure_ctlr = 1;
5064 return 1; /* For Invalid and Tampered device */
5067 shost = scsi_host_alloc(&mpi3mr_driver_template,
5068 sizeof(struct mpi3mr_ioc));
5074 mrioc = shost_priv(shost);
5075 mrioc->id = mrioc_ids++;
5076 sprintf(mrioc->driver_name, "%s", MPI3MR_DRIVER_NAME);
5077 sprintf(mrioc->name, "%s%d", mrioc->driver_name, mrioc->id);
5078 INIT_LIST_HEAD(&mrioc->list);
5079 spin_lock(&mrioc_list_lock);
5080 list_add_tail(&mrioc->list, &mrioc_list);
5081 spin_unlock(&mrioc_list_lock);
5083 spin_lock_init(&mrioc->admin_req_lock);
5084 spin_lock_init(&mrioc->reply_free_queue_lock);
5085 spin_lock_init(&mrioc->sbq_lock);
5086 spin_lock_init(&mrioc->fwevt_lock);
5087 spin_lock_init(&mrioc->tgtdev_lock);
5088 spin_lock_init(&mrioc->watchdog_lock);
5089 spin_lock_init(&mrioc->chain_buf_lock);
5090 spin_lock_init(&mrioc->sas_node_lock);
5092 INIT_LIST_HEAD(&mrioc->fwevt_list);
5093 INIT_LIST_HEAD(&mrioc->tgtdev_list);
5094 INIT_LIST_HEAD(&mrioc->delayed_rmhs_list);
5095 INIT_LIST_HEAD(&mrioc->delayed_evtack_cmds_list);
5096 INIT_LIST_HEAD(&mrioc->sas_expander_list);
5097 INIT_LIST_HEAD(&mrioc->hba_port_table_list);
5098 INIT_LIST_HEAD(&mrioc->enclosure_list);
5100 mutex_init(&mrioc->reset_mutex);
5101 mpi3mr_init_drv_cmd(&mrioc->init_cmds, MPI3MR_HOSTTAG_INITCMDS);
5102 mpi3mr_init_drv_cmd(&mrioc->host_tm_cmds, MPI3MR_HOSTTAG_BLK_TMS);
5103 mpi3mr_init_drv_cmd(&mrioc->bsg_cmds, MPI3MR_HOSTTAG_BSG_CMDS);
5104 mpi3mr_init_drv_cmd(&mrioc->cfg_cmds, MPI3MR_HOSTTAG_CFG_CMDS);
5105 mpi3mr_init_drv_cmd(&mrioc->transport_cmds,
5106 MPI3MR_HOSTTAG_TRANSPORT_CMDS);
5108 for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++)
5109 mpi3mr_init_drv_cmd(&mrioc->dev_rmhs_cmds[i],
5110 MPI3MR_HOSTTAG_DEVRMCMD_MIN + i);
5112 for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++)
5113 mpi3mr_init_drv_cmd(&mrioc->evtack_cmds[i],
5114 MPI3MR_HOSTTAG_EVTACKCMD_MIN + i);
5116 if ((pdev->device == MPI3_MFGPAGE_DEVID_SAS4116) &&
5118 mrioc->enable_segqueue = false;
5120 mrioc->enable_segqueue = true;
5122 init_waitqueue_head(&mrioc->reset_waitq);
5123 mrioc->logging_level = logging_level;
5124 mrioc->shost = shost;
5126 mrioc->stop_bsgs = 1;
5128 mrioc->max_sgl_entries = max_sgl_entries;
5129 if (max_sgl_entries > MPI3MR_MAX_SGL_ENTRIES)
5130 mrioc->max_sgl_entries = MPI3MR_MAX_SGL_ENTRIES;
5131 else if (max_sgl_entries < MPI3MR_DEFAULT_SGL_ENTRIES)
5132 mrioc->max_sgl_entries = MPI3MR_DEFAULT_SGL_ENTRIES;
5134 mrioc->max_sgl_entries /= MPI3MR_DEFAULT_SGL_ENTRIES;
5135 mrioc->max_sgl_entries *= MPI3MR_DEFAULT_SGL_ENTRIES;
5138 /* init shost parameters */
5139 shost->max_cmd_len = MPI3MR_MAX_CDB_LENGTH;
5140 shost->max_lun = -1;
5141 shost->unique_id = mrioc->id;
5143 shost->max_channel = 0;
5144 shost->max_id = 0xFFFFFFFF;
5146 shost->host_tagset = 1;
5149 scsi_host_set_prot(shost, prot_mask);
5151 prot_mask = SHOST_DIF_TYPE1_PROTECTION
5152 | SHOST_DIF_TYPE2_PROTECTION
5153 | SHOST_DIF_TYPE3_PROTECTION;
5154 scsi_host_set_prot(shost, prot_mask);
5158 "%s :host protection capabilities enabled %s%s%s%s%s%s%s\n",
5160 (prot_mask & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "",
5161 (prot_mask & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "",
5162 (prot_mask & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "",
5163 (prot_mask & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "",
5164 (prot_mask & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "",
5165 (prot_mask & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "",
5166 (prot_mask & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : "");
5168 if (prot_guard_mask)
5169 scsi_host_set_guard(shost, (prot_guard_mask & 3));
5171 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
5173 snprintf(mrioc->fwevt_worker_name, sizeof(mrioc->fwevt_worker_name),
5174 "%s%d_fwevt_wrkr", mrioc->driver_name, mrioc->id);
5175 mrioc->fwevt_worker_thread = alloc_ordered_workqueue(
5176 mrioc->fwevt_worker_name, 0);
5177 if (!mrioc->fwevt_worker_thread) {
5178 ioc_err(mrioc, "failure at %s:%d/%s()!\n",
5179 __FILE__, __LINE__, __func__);
5181 goto fwevtthread_failed;
5184 mrioc->is_driver_loading = 1;
5185 mrioc->cpu_count = num_online_cpus();
5186 if (mpi3mr_setup_resources(mrioc)) {
5187 ioc_err(mrioc, "setup resources failed\n");
5189 goto resource_alloc_failed;
5191 if (mpi3mr_init_ioc(mrioc)) {
5192 ioc_err(mrioc, "initializing IOC failed\n");
5194 goto init_ioc_failed;
5197 shost->nr_hw_queues = mrioc->num_op_reply_q;
5198 if (mrioc->active_poll_qcount)
5201 shost->can_queue = mrioc->max_host_ios;
5202 shost->sg_tablesize = mrioc->max_sgl_entries;
5203 shost->max_id = mrioc->facts.max_perids + 1;
5205 retval = scsi_add_host(shost, &pdev->dev);
5207 ioc_err(mrioc, "failure at %s:%d/%s()!\n",
5208 __FILE__, __LINE__, __func__);
5209 goto addhost_failed;
5212 scsi_scan_host(shost);
5213 mpi3mr_bsg_init(mrioc);
5217 mpi3mr_stop_watchdog(mrioc);
5218 mpi3mr_cleanup_ioc(mrioc);
5220 mpi3mr_free_mem(mrioc);
5221 mpi3mr_cleanup_resources(mrioc);
5222 resource_alloc_failed:
5223 destroy_workqueue(mrioc->fwevt_worker_thread);
5225 spin_lock(&mrioc_list_lock);
5226 list_del(&mrioc->list);
5227 spin_unlock(&mrioc_list_lock);
5228 scsi_host_put(shost);
5234 * mpi3mr_remove - PCI remove callback
5235 * @pdev: PCI device instance
5237 * Cleanup the IOC by issuing MUR and shutdown notification.
5238 * Free up all memory and resources associated with the
5239 * controllerand target devices, unregister the shost.
5243 static void mpi3mr_remove(struct pci_dev *pdev)
5245 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5246 struct mpi3mr_ioc *mrioc;
5247 struct workqueue_struct *wq;
5248 unsigned long flags;
5249 struct mpi3mr_tgt_dev *tgtdev, *tgtdev_next;
5250 struct mpi3mr_hba_port *port, *hba_port_next;
5251 struct mpi3mr_sas_node *sas_expander, *sas_expander_next;
5256 mrioc = shost_priv(shost);
5257 while (mrioc->reset_in_progress || mrioc->is_driver_loading)
5260 if (!pci_device_is_present(mrioc->pdev)) {
5261 mrioc->unrecoverable = 1;
5262 mpi3mr_flush_cmds_for_unrecovered_controller(mrioc);
5265 mpi3mr_bsg_exit(mrioc);
5266 mrioc->stop_drv_processing = 1;
5267 mpi3mr_cleanup_fwevt_list(mrioc);
5268 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
5269 wq = mrioc->fwevt_worker_thread;
5270 mrioc->fwevt_worker_thread = NULL;
5271 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
5273 destroy_workqueue(wq);
5275 if (mrioc->sas_transport_enabled)
5276 sas_remove_host(shost);
5278 scsi_remove_host(shost);
5280 list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
5282 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
5283 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, true);
5284 mpi3mr_tgtdev_put(tgtdev);
5286 mpi3mr_stop_watchdog(mrioc);
5287 mpi3mr_cleanup_ioc(mrioc);
5288 mpi3mr_free_mem(mrioc);
5289 mpi3mr_cleanup_resources(mrioc);
5291 spin_lock_irqsave(&mrioc->sas_node_lock, flags);
5292 list_for_each_entry_safe_reverse(sas_expander, sas_expander_next,
5293 &mrioc->sas_expander_list, list) {
5294 spin_unlock_irqrestore(&mrioc->sas_node_lock, flags);
5295 mpi3mr_expander_node_remove(mrioc, sas_expander);
5296 spin_lock_irqsave(&mrioc->sas_node_lock, flags);
5298 list_for_each_entry_safe(port, hba_port_next, &mrioc->hba_port_table_list, list) {
5300 "removing hba_port entry: %p port: %d from hba_port list\n",
5301 port, port->port_id);
5302 list_del(&port->list);
5305 spin_unlock_irqrestore(&mrioc->sas_node_lock, flags);
5307 if (mrioc->sas_hba.num_phys) {
5308 kfree(mrioc->sas_hba.phy);
5309 mrioc->sas_hba.phy = NULL;
5310 mrioc->sas_hba.num_phys = 0;
5313 spin_lock(&mrioc_list_lock);
5314 list_del(&mrioc->list);
5315 spin_unlock(&mrioc_list_lock);
5317 scsi_host_put(shost);
5321 * mpi3mr_shutdown - PCI shutdown callback
5322 * @pdev: PCI device instance
5324 * Free up all memory and resources associated with the
5329 static void mpi3mr_shutdown(struct pci_dev *pdev)
5331 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5332 struct mpi3mr_ioc *mrioc;
5333 struct workqueue_struct *wq;
5334 unsigned long flags;
5339 mrioc = shost_priv(shost);
5340 while (mrioc->reset_in_progress || mrioc->is_driver_loading)
5343 mrioc->stop_drv_processing = 1;
5344 mpi3mr_cleanup_fwevt_list(mrioc);
5345 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
5346 wq = mrioc->fwevt_worker_thread;
5347 mrioc->fwevt_worker_thread = NULL;
5348 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
5350 destroy_workqueue(wq);
5352 mpi3mr_stop_watchdog(mrioc);
5353 mpi3mr_cleanup_ioc(mrioc);
5354 mpi3mr_cleanup_resources(mrioc);
5358 * mpi3mr_suspend - PCI power management suspend callback
5359 * @dev: Device struct
5361 * Change the power state to the given value and cleanup the IOC
5362 * by issuing MUR and shutdown notification
5366 static int __maybe_unused
5367 mpi3mr_suspend(struct device *dev)
5369 struct pci_dev *pdev = to_pci_dev(dev);
5370 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5371 struct mpi3mr_ioc *mrioc;
5376 mrioc = shost_priv(shost);
5377 while (mrioc->reset_in_progress || mrioc->is_driver_loading)
5379 mrioc->stop_drv_processing = 1;
5380 mpi3mr_cleanup_fwevt_list(mrioc);
5381 scsi_block_requests(shost);
5382 mpi3mr_stop_watchdog(mrioc);
5383 mpi3mr_cleanup_ioc(mrioc);
5385 ioc_info(mrioc, "pdev=0x%p, slot=%s, entering operating state\n",
5386 pdev, pci_name(pdev));
5387 mpi3mr_cleanup_resources(mrioc);
5393 * mpi3mr_resume - PCI power management resume callback
5394 * @dev: Device struct
5396 * Restore the power state to D0 and reinitialize the controller
5397 * and resume I/O operations to the target devices
5399 * Return: 0 on success, non-zero on failure
5401 static int __maybe_unused
5402 mpi3mr_resume(struct device *dev)
5404 struct pci_dev *pdev = to_pci_dev(dev);
5405 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5406 struct mpi3mr_ioc *mrioc;
5407 pci_power_t device_state = pdev->current_state;
5413 mrioc = shost_priv(shost);
5415 ioc_info(mrioc, "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
5416 pdev, pci_name(pdev), device_state);
5418 mrioc->cpu_count = num_online_cpus();
5419 r = mpi3mr_setup_resources(mrioc);
5421 ioc_info(mrioc, "%s: Setup resources failed[%d]\n",
5426 mrioc->stop_drv_processing = 0;
5427 mpi3mr_invalidate_devhandles(mrioc);
5428 mpi3mr_free_enclosure_list(mrioc);
5429 mpi3mr_memset_buffers(mrioc);
5430 r = mpi3mr_reinit_ioc(mrioc, 1);
5432 ioc_err(mrioc, "resuming controller failed[%d]\n", r);
5435 ssleep(MPI3MR_RESET_TOPOLOGY_SETTLE_TIME);
5436 scsi_unblock_requests(shost);
5437 mrioc->device_refresh_on = 0;
5438 mpi3mr_start_watchdog(mrioc);
5443 static const struct pci_device_id mpi3mr_pci_id_table[] = {
5445 PCI_DEVICE_SUB(MPI3_MFGPAGE_VENDORID_BROADCOM,
5446 MPI3_MFGPAGE_DEVID_SAS4116, PCI_ANY_ID, PCI_ANY_ID)
5449 PCI_DEVICE_SUB(MPI3_MFGPAGE_VENDORID_BROADCOM,
5450 MPI3_MFGPAGE_DEVID_SAS5116_MPI, PCI_ANY_ID, PCI_ANY_ID)
5453 PCI_DEVICE_SUB(MPI3_MFGPAGE_VENDORID_BROADCOM,
5454 MPI3_MFGPAGE_DEVID_SAS5116_MPI_MGMT, PCI_ANY_ID, PCI_ANY_ID)
5458 MODULE_DEVICE_TABLE(pci, mpi3mr_pci_id_table);
5460 static SIMPLE_DEV_PM_OPS(mpi3mr_pm_ops, mpi3mr_suspend, mpi3mr_resume);
5462 static struct pci_driver mpi3mr_pci_driver = {
5463 .name = MPI3MR_DRIVER_NAME,
5464 .id_table = mpi3mr_pci_id_table,
5465 .probe = mpi3mr_probe,
5466 .remove = mpi3mr_remove,
5467 .shutdown = mpi3mr_shutdown,
5468 .driver.pm = &mpi3mr_pm_ops,
5471 static ssize_t event_counter_show(struct device_driver *dd, char *buf)
5473 return sprintf(buf, "%llu\n", atomic64_read(&event_counter));
5475 static DRIVER_ATTR_RO(event_counter);
5477 static int __init mpi3mr_init(void)
5481 pr_info("Loading %s version %s\n", MPI3MR_DRIVER_NAME,
5482 MPI3MR_DRIVER_VERSION);
5484 mpi3mr_transport_template =
5485 sas_attach_transport(&mpi3mr_transport_functions);
5486 if (!mpi3mr_transport_template) {
5487 pr_err("%s failed to load due to sas transport attach failure\n",
5488 MPI3MR_DRIVER_NAME);
5492 ret_val = pci_register_driver(&mpi3mr_pci_driver);
5494 pr_err("%s failed to load due to pci register driver failure\n",
5495 MPI3MR_DRIVER_NAME);
5496 goto err_pci_reg_fail;
5499 ret_val = driver_create_file(&mpi3mr_pci_driver.driver,
5500 &driver_attr_event_counter);
5502 goto err_event_counter;
5507 pci_unregister_driver(&mpi3mr_pci_driver);
5510 sas_release_transport(mpi3mr_transport_template);
5514 static void __exit mpi3mr_exit(void)
5516 if (warn_non_secure_ctlr)
5518 "Unloading %s version %s while managing a non secure controller\n",
5519 MPI3MR_DRIVER_NAME, MPI3MR_DRIVER_VERSION);
5521 pr_info("Unloading %s version %s\n", MPI3MR_DRIVER_NAME,
5522 MPI3MR_DRIVER_VERSION);
5524 driver_remove_file(&mpi3mr_pci_driver.driver,
5525 &driver_attr_event_counter);
5526 pci_unregister_driver(&mpi3mr_pci_driver);
5527 sas_release_transport(mpi3mr_transport_template);
5530 module_init(mpi3mr_init);
5531 module_exit(mpi3mr_exit);