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->host_exposed && tgtdev->starget &&
1051 tgtdev->starget->hostdata) {
1052 tgt_priv = tgtdev->starget->hostdata;
1053 tgt_priv->dev_removed = 1;
1054 atomic_set(&tgt_priv->block_io, 0);
1058 list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
1060 if (tgtdev->dev_handle == MPI3MR_INVALID_DEV_HANDLE) {
1061 dprint_reset(mrioc, "removing target device with perst_id(%d)\n",
1063 if (tgtdev->host_exposed)
1064 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1065 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, true);
1066 mpi3mr_tgtdev_put(tgtdev);
1071 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
1072 if ((tgtdev->dev_handle != MPI3MR_INVALID_DEV_HANDLE) &&
1073 !tgtdev->is_hidden && !tgtdev->host_exposed)
1074 mpi3mr_report_tgtdev_to_host(mrioc, tgtdev->perst_id);
1079 * mpi3mr_update_tgtdev - DevStatusChange evt bottomhalf
1080 * @mrioc: Adapter instance reference
1081 * @tgtdev: Target device internal structure
1082 * @dev_pg0: New device page0
1083 * @is_added: Flag to indicate the device is just added
1085 * Update the information from the device page0 into the driver
1086 * cached target device structure.
1090 static void mpi3mr_update_tgtdev(struct mpi3mr_ioc *mrioc,
1091 struct mpi3mr_tgt_dev *tgtdev, struct mpi3_device_page0 *dev_pg0,
1095 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
1096 struct mpi3mr_enclosure_node *enclosure_dev = NULL;
1099 tgtdev->perst_id = le16_to_cpu(dev_pg0->persistent_id);
1100 tgtdev->dev_handle = le16_to_cpu(dev_pg0->dev_handle);
1101 tgtdev->dev_type = dev_pg0->device_form;
1102 tgtdev->io_unit_port = dev_pg0->io_unit_port;
1103 tgtdev->encl_handle = le16_to_cpu(dev_pg0->enclosure_handle);
1104 tgtdev->parent_handle = le16_to_cpu(dev_pg0->parent_dev_handle);
1105 tgtdev->slot = le16_to_cpu(dev_pg0->slot);
1106 tgtdev->q_depth = le16_to_cpu(dev_pg0->queue_depth);
1107 tgtdev->wwid = le64_to_cpu(dev_pg0->wwid);
1108 tgtdev->devpg0_flag = le16_to_cpu(dev_pg0->flags);
1110 if (tgtdev->encl_handle)
1111 enclosure_dev = mpi3mr_enclosure_find_by_handle(mrioc,
1112 tgtdev->encl_handle);
1114 tgtdev->enclosure_logical_id = le64_to_cpu(
1115 enclosure_dev->pg0.enclosure_logical_id);
1117 flags = tgtdev->devpg0_flag;
1119 tgtdev->is_hidden = (flags & MPI3_DEVICE0_FLAGS_HIDDEN);
1121 if (is_added == true)
1122 tgtdev->io_throttle_enabled =
1123 (flags & MPI3_DEVICE0_FLAGS_IO_THROTTLING_REQUIRED) ? 1 : 0;
1125 switch (flags & MPI3_DEVICE0_FLAGS_MAX_WRITE_SAME_MASK) {
1126 case MPI3_DEVICE0_FLAGS_MAX_WRITE_SAME_256_LB:
1127 tgtdev->wslen = MPI3MR_WRITE_SAME_MAX_LEN_256_BLKS;
1129 case MPI3_DEVICE0_FLAGS_MAX_WRITE_SAME_2048_LB:
1130 tgtdev->wslen = MPI3MR_WRITE_SAME_MAX_LEN_2048_BLKS;
1132 case MPI3_DEVICE0_FLAGS_MAX_WRITE_SAME_NO_LIMIT:
1138 if (tgtdev->starget && tgtdev->starget->hostdata) {
1139 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
1140 tgtdev->starget->hostdata;
1141 scsi_tgt_priv_data->perst_id = tgtdev->perst_id;
1142 scsi_tgt_priv_data->dev_handle = tgtdev->dev_handle;
1143 scsi_tgt_priv_data->dev_type = tgtdev->dev_type;
1144 scsi_tgt_priv_data->io_throttle_enabled =
1145 tgtdev->io_throttle_enabled;
1146 if (is_added == true)
1147 atomic_set(&scsi_tgt_priv_data->block_io, 0);
1148 scsi_tgt_priv_data->wslen = tgtdev->wslen;
1151 switch (dev_pg0->access_status) {
1152 case MPI3_DEVICE0_ASTATUS_NO_ERRORS:
1153 case MPI3_DEVICE0_ASTATUS_PREPARE:
1154 case MPI3_DEVICE0_ASTATUS_NEEDS_INITIALIZATION:
1155 case MPI3_DEVICE0_ASTATUS_DEVICE_MISSING_DELAY:
1158 tgtdev->is_hidden = 1;
1162 switch (tgtdev->dev_type) {
1163 case MPI3_DEVICE_DEVFORM_SAS_SATA:
1165 struct mpi3_device0_sas_sata_format *sasinf =
1166 &dev_pg0->device_specific.sas_sata_format;
1167 u16 dev_info = le16_to_cpu(sasinf->device_info);
1169 tgtdev->dev_spec.sas_sata_inf.dev_info = dev_info;
1170 tgtdev->dev_spec.sas_sata_inf.sas_address =
1171 le64_to_cpu(sasinf->sas_address);
1172 tgtdev->dev_spec.sas_sata_inf.phy_id = sasinf->phy_num;
1173 tgtdev->dev_spec.sas_sata_inf.attached_phy_id =
1174 sasinf->attached_phy_identifier;
1175 if ((dev_info & MPI3_SAS_DEVICE_INFO_DEVICE_TYPE_MASK) !=
1176 MPI3_SAS_DEVICE_INFO_DEVICE_TYPE_END_DEVICE)
1177 tgtdev->is_hidden = 1;
1178 else if (!(dev_info & (MPI3_SAS_DEVICE_INFO_STP_SATA_TARGET |
1179 MPI3_SAS_DEVICE_INFO_SSP_TARGET)))
1180 tgtdev->is_hidden = 1;
1182 if (((tgtdev->devpg0_flag &
1183 MPI3_DEVICE0_FLAGS_ATT_METHOD_DIR_ATTACHED)
1184 && (tgtdev->devpg0_flag &
1185 MPI3_DEVICE0_FLAGS_ATT_METHOD_VIRTUAL)) ||
1186 (tgtdev->parent_handle == 0xFFFF))
1187 tgtdev->non_stl = 1;
1188 if (tgtdev->dev_spec.sas_sata_inf.hba_port)
1189 tgtdev->dev_spec.sas_sata_inf.hba_port->port_id =
1190 dev_pg0->io_unit_port;
1193 case MPI3_DEVICE_DEVFORM_PCIE:
1195 struct mpi3_device0_pcie_format *pcieinf =
1196 &dev_pg0->device_specific.pcie_format;
1197 u16 dev_info = le16_to_cpu(pcieinf->device_info);
1199 tgtdev->dev_spec.pcie_inf.dev_info = dev_info;
1200 tgtdev->dev_spec.pcie_inf.capb =
1201 le32_to_cpu(pcieinf->capabilities);
1202 tgtdev->dev_spec.pcie_inf.mdts = MPI3MR_DEFAULT_MDTS;
1204 tgtdev->dev_spec.pcie_inf.pgsz = 12;
1205 if (dev_pg0->access_status == MPI3_DEVICE0_ASTATUS_NO_ERRORS) {
1206 tgtdev->dev_spec.pcie_inf.mdts =
1207 le32_to_cpu(pcieinf->maximum_data_transfer_size);
1208 tgtdev->dev_spec.pcie_inf.pgsz = pcieinf->page_size;
1209 tgtdev->dev_spec.pcie_inf.reset_to =
1210 max_t(u8, pcieinf->controller_reset_to,
1211 MPI3MR_INTADMCMD_TIMEOUT);
1212 tgtdev->dev_spec.pcie_inf.abort_to =
1213 max_t(u8, pcieinf->nvme_abort_to,
1214 MPI3MR_INTADMCMD_TIMEOUT);
1216 if (tgtdev->dev_spec.pcie_inf.mdts > (1024 * 1024))
1217 tgtdev->dev_spec.pcie_inf.mdts = (1024 * 1024);
1218 if (((dev_info & MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) !=
1219 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) &&
1220 ((dev_info & MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) !=
1221 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_SCSI_DEVICE))
1222 tgtdev->is_hidden = 1;
1223 tgtdev->non_stl = 1;
1226 prot_mask = scsi_host_get_prot(mrioc->shost);
1227 if (prot_mask & SHOST_DIX_TYPE0_PROTECTION) {
1228 scsi_host_set_prot(mrioc->shost, prot_mask & 0x77);
1230 "%s : Disabling DIX0 prot capability\n", __func__);
1232 "because HBA does not support DIX0 operation on NVME drives\n");
1236 case MPI3_DEVICE_DEVFORM_VD:
1238 struct mpi3_device0_vd_format *vdinf =
1239 &dev_pg0->device_specific.vd_format;
1240 struct mpi3mr_throttle_group_info *tg = NULL;
1241 u16 vdinf_io_throttle_group =
1242 le16_to_cpu(vdinf->io_throttle_group);
1244 tgtdev->dev_spec.vd_inf.state = vdinf->vd_state;
1245 if (vdinf->vd_state == MPI3_DEVICE0_VD_STATE_OFFLINE)
1246 tgtdev->is_hidden = 1;
1247 tgtdev->non_stl = 1;
1248 tgtdev->dev_spec.vd_inf.tg_id = vdinf_io_throttle_group;
1249 tgtdev->dev_spec.vd_inf.tg_high =
1250 le16_to_cpu(vdinf->io_throttle_group_high) * 2048;
1251 tgtdev->dev_spec.vd_inf.tg_low =
1252 le16_to_cpu(vdinf->io_throttle_group_low) * 2048;
1253 if (vdinf_io_throttle_group < mrioc->num_io_throttle_group) {
1254 tg = mrioc->throttle_groups + vdinf_io_throttle_group;
1255 tg->id = vdinf_io_throttle_group;
1256 tg->high = tgtdev->dev_spec.vd_inf.tg_high;
1257 tg->low = tgtdev->dev_spec.vd_inf.tg_low;
1259 tgtdev->dev_spec.vd_inf.tg_qd_reduction;
1260 if (is_added == true)
1261 tg->fw_qd = tgtdev->q_depth;
1262 tg->modified_qd = tgtdev->q_depth;
1264 tgtdev->dev_spec.vd_inf.tg = tg;
1265 if (scsi_tgt_priv_data)
1266 scsi_tgt_priv_data->throttle_group = tg;
1275 * mpi3mr_devstatuschg_evt_bh - DevStatusChange evt bottomhalf
1276 * @mrioc: Adapter instance reference
1277 * @fwevt: Firmware event information.
1279 * Process Device status Change event and based on device's new
1280 * information, either expose the device to the upper layers, or
1281 * remove the device from upper layers.
1285 static void mpi3mr_devstatuschg_evt_bh(struct mpi3mr_ioc *mrioc,
1286 struct mpi3mr_fwevt *fwevt)
1289 u8 uhide = 0, delete = 0, cleanup = 0;
1290 struct mpi3mr_tgt_dev *tgtdev = NULL;
1291 struct mpi3_event_data_device_status_change *evtdata =
1292 (struct mpi3_event_data_device_status_change *)fwevt->event_data;
1294 dev_handle = le16_to_cpu(evtdata->dev_handle);
1296 "%s :device status change: handle(0x%04x): reason code(0x%x)\n",
1297 __func__, dev_handle, evtdata->reason_code);
1298 switch (evtdata->reason_code) {
1299 case MPI3_EVENT_DEV_STAT_RC_HIDDEN:
1302 case MPI3_EVENT_DEV_STAT_RC_NOT_HIDDEN:
1305 case MPI3_EVENT_DEV_STAT_RC_VD_NOT_RESPONDING:
1310 ioc_info(mrioc, "%s :Unhandled reason code(0x%x)\n", __func__,
1311 evtdata->reason_code);
1315 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
1319 tgtdev->is_hidden = 0;
1320 if (!tgtdev->host_exposed)
1321 mpi3mr_report_tgtdev_to_host(mrioc, tgtdev->perst_id);
1325 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1328 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, false);
1329 mpi3mr_tgtdev_put(tgtdev);
1334 mpi3mr_tgtdev_put(tgtdev);
1338 * mpi3mr_devinfochg_evt_bh - DeviceInfoChange evt bottomhalf
1339 * @mrioc: Adapter instance reference
1340 * @dev_pg0: New device page0
1342 * Process Device Info Change event and based on device's new
1343 * information, either expose the device to the upper layers, or
1344 * remove the device from upper layers or update the details of
1349 static void mpi3mr_devinfochg_evt_bh(struct mpi3mr_ioc *mrioc,
1350 struct mpi3_device_page0 *dev_pg0)
1352 struct mpi3mr_tgt_dev *tgtdev = NULL;
1353 u16 dev_handle = 0, perst_id = 0;
1355 perst_id = le16_to_cpu(dev_pg0->persistent_id);
1356 dev_handle = le16_to_cpu(dev_pg0->dev_handle);
1358 "%s :Device info change: handle(0x%04x): persist_id(0x%x)\n",
1359 __func__, dev_handle, perst_id);
1360 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
1363 mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0, false);
1364 if (!tgtdev->is_hidden && !tgtdev->host_exposed)
1365 mpi3mr_report_tgtdev_to_host(mrioc, perst_id);
1366 if (tgtdev->is_hidden && tgtdev->host_exposed)
1367 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1368 if (!tgtdev->is_hidden && tgtdev->host_exposed && tgtdev->starget)
1369 starget_for_each_device(tgtdev->starget, (void *)tgtdev,
1370 mpi3mr_update_sdev);
1373 mpi3mr_tgtdev_put(tgtdev);
1377 * mpi3mr_free_enclosure_list - release enclosures
1378 * @mrioc: Adapter instance reference
1380 * Free memory allocated during encloure add.
1384 void mpi3mr_free_enclosure_list(struct mpi3mr_ioc *mrioc)
1386 struct mpi3mr_enclosure_node *enclosure_dev, *enclosure_dev_next;
1388 list_for_each_entry_safe(enclosure_dev,
1389 enclosure_dev_next, &mrioc->enclosure_list, list) {
1390 list_del(&enclosure_dev->list);
1391 kfree(enclosure_dev);
1396 * mpi3mr_enclosure_find_by_handle - enclosure search by handle
1397 * @mrioc: Adapter instance reference
1398 * @handle: Firmware device handle of the enclosure
1400 * This searches for enclosure device based on handle, then returns the
1403 * Return: Enclosure object reference or NULL
1405 struct mpi3mr_enclosure_node *mpi3mr_enclosure_find_by_handle(
1406 struct mpi3mr_ioc *mrioc, u16 handle)
1408 struct mpi3mr_enclosure_node *enclosure_dev, *r = NULL;
1410 list_for_each_entry(enclosure_dev, &mrioc->enclosure_list, list) {
1411 if (le16_to_cpu(enclosure_dev->pg0.enclosure_handle) != handle)
1421 * mpi3mr_encldev_add_chg_evt_debug - debug for enclosure event
1422 * @mrioc: Adapter instance reference
1423 * @encl_pg0: Enclosure page 0.
1424 * @is_added: Added event or not
1428 static void mpi3mr_encldev_add_chg_evt_debug(struct mpi3mr_ioc *mrioc,
1429 struct mpi3_enclosure_page0 *encl_pg0, u8 is_added)
1431 char *reason_str = NULL;
1433 if (!(mrioc->logging_level & MPI3_DEBUG_EVENT_WORK_TASK))
1437 reason_str = "enclosure added";
1439 reason_str = "enclosure dev status changed";
1442 "%s: handle(0x%04x), enclosure logical id(0x%016llx)\n",
1443 reason_str, le16_to_cpu(encl_pg0->enclosure_handle),
1444 (unsigned long long)le64_to_cpu(encl_pg0->enclosure_logical_id));
1446 "number of slots(%d), port(%d), flags(0x%04x), present(%d)\n",
1447 le16_to_cpu(encl_pg0->num_slots), encl_pg0->io_unit_port,
1448 le16_to_cpu(encl_pg0->flags),
1449 ((le16_to_cpu(encl_pg0->flags) &
1450 MPI3_ENCLS0_FLAGS_ENCL_DEV_PRESENT_MASK) >> 4));
1454 * mpi3mr_encldev_add_chg_evt_bh - Enclosure evt bottomhalf
1455 * @mrioc: Adapter instance reference
1456 * @fwevt: Firmware event reference
1458 * Prints information about the Enclosure device status or
1459 * Enclosure add events if logging is enabled and add or remove
1460 * the enclosure from the controller's internal list of
1465 static void mpi3mr_encldev_add_chg_evt_bh(struct mpi3mr_ioc *mrioc,
1466 struct mpi3mr_fwevt *fwevt)
1468 struct mpi3mr_enclosure_node *enclosure_dev = NULL;
1469 struct mpi3_enclosure_page0 *encl_pg0;
1473 encl_pg0 = (struct mpi3_enclosure_page0 *) fwevt->event_data;
1474 added = (fwevt->event_id == MPI3_EVENT_ENCL_DEVICE_ADDED) ? 1 : 0;
1475 mpi3mr_encldev_add_chg_evt_debug(mrioc, encl_pg0, added);
1478 encl_handle = le16_to_cpu(encl_pg0->enclosure_handle);
1479 present = ((le16_to_cpu(encl_pg0->flags) &
1480 MPI3_ENCLS0_FLAGS_ENCL_DEV_PRESENT_MASK) >> 4);
1483 enclosure_dev = mpi3mr_enclosure_find_by_handle(mrioc,
1485 if (!enclosure_dev && present) {
1487 kzalloc(sizeof(struct mpi3mr_enclosure_node),
1491 list_add_tail(&enclosure_dev->list,
1492 &mrioc->enclosure_list);
1494 if (enclosure_dev) {
1496 list_del(&enclosure_dev->list);
1497 kfree(enclosure_dev);
1499 memcpy(&enclosure_dev->pg0, encl_pg0,
1500 sizeof(enclosure_dev->pg0));
1506 * mpi3mr_sastopochg_evt_debug - SASTopoChange details
1507 * @mrioc: Adapter instance reference
1508 * @event_data: SAS topology change list event data
1510 * Prints information about the SAS topology change event.
1515 mpi3mr_sastopochg_evt_debug(struct mpi3mr_ioc *mrioc,
1516 struct mpi3_event_data_sas_topology_change_list *event_data)
1520 u8 reason_code, phy_number;
1521 char *status_str = NULL;
1522 u8 link_rate, prev_link_rate;
1524 switch (event_data->exp_status) {
1525 case MPI3_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
1526 status_str = "remove";
1528 case MPI3_EVENT_SAS_TOPO_ES_RESPONDING:
1529 status_str = "responding";
1531 case MPI3_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
1532 status_str = "remove delay";
1534 case MPI3_EVENT_SAS_TOPO_ES_NO_EXPANDER:
1535 status_str = "direct attached";
1538 status_str = "unknown status";
1541 ioc_info(mrioc, "%s :sas topology change: (%s)\n",
1542 __func__, status_str);
1544 "%s :\texpander_handle(0x%04x), port(%d), enclosure_handle(0x%04x) start_phy(%02d), num_entries(%d)\n",
1545 __func__, le16_to_cpu(event_data->expander_dev_handle),
1546 event_data->io_unit_port,
1547 le16_to_cpu(event_data->enclosure_handle),
1548 event_data->start_phy_num, event_data->num_entries);
1549 for (i = 0; i < event_data->num_entries; i++) {
1550 handle = le16_to_cpu(event_data->phy_entry[i].attached_dev_handle);
1553 phy_number = event_data->start_phy_num + i;
1554 reason_code = event_data->phy_entry[i].status &
1555 MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
1556 switch (reason_code) {
1557 case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
1558 status_str = "target remove";
1560 case MPI3_EVENT_SAS_TOPO_PHY_RC_DELAY_NOT_RESPONDING:
1561 status_str = "delay target remove";
1563 case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
1564 status_str = "link status change";
1566 case MPI3_EVENT_SAS_TOPO_PHY_RC_NO_CHANGE:
1567 status_str = "link status no change";
1569 case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
1570 status_str = "target responding";
1573 status_str = "unknown";
1576 link_rate = event_data->phy_entry[i].link_rate >> 4;
1577 prev_link_rate = event_data->phy_entry[i].link_rate & 0xF;
1579 "%s :\tphy(%02d), attached_handle(0x%04x): %s: link rate: new(0x%02x), old(0x%02x)\n",
1580 __func__, phy_number, handle, status_str, link_rate,
1586 * mpi3mr_sastopochg_evt_bh - SASTopologyChange evt bottomhalf
1587 * @mrioc: Adapter instance reference
1588 * @fwevt: Firmware event reference
1590 * Prints information about the SAS topology change event and
1591 * for "not responding" event code, removes the device from the
1596 static void mpi3mr_sastopochg_evt_bh(struct mpi3mr_ioc *mrioc,
1597 struct mpi3mr_fwevt *fwevt)
1599 struct mpi3_event_data_sas_topology_change_list *event_data =
1600 (struct mpi3_event_data_sas_topology_change_list *)fwevt->event_data;
1604 u64 exp_sas_address = 0, parent_sas_address = 0;
1605 struct mpi3mr_hba_port *hba_port = NULL;
1606 struct mpi3mr_tgt_dev *tgtdev = NULL;
1607 struct mpi3mr_sas_node *sas_expander = NULL;
1608 unsigned long flags;
1609 u8 link_rate, prev_link_rate, parent_phy_number;
1611 mpi3mr_sastopochg_evt_debug(mrioc, event_data);
1612 if (mrioc->sas_transport_enabled) {
1613 hba_port = mpi3mr_get_hba_port_by_id(mrioc,
1614 event_data->io_unit_port);
1615 if (le16_to_cpu(event_data->expander_dev_handle)) {
1616 spin_lock_irqsave(&mrioc->sas_node_lock, flags);
1617 sas_expander = __mpi3mr_expander_find_by_handle(mrioc,
1618 le16_to_cpu(event_data->expander_dev_handle));
1620 exp_sas_address = sas_expander->sas_address;
1621 hba_port = sas_expander->hba_port;
1623 spin_unlock_irqrestore(&mrioc->sas_node_lock, flags);
1624 parent_sas_address = exp_sas_address;
1626 parent_sas_address = mrioc->sas_hba.sas_address;
1629 for (i = 0; i < event_data->num_entries; i++) {
1632 handle = le16_to_cpu(event_data->phy_entry[i].attached_dev_handle);
1635 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
1639 reason_code = event_data->phy_entry[i].status &
1640 MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
1642 switch (reason_code) {
1643 case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
1644 if (tgtdev->host_exposed)
1645 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1646 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, false);
1647 mpi3mr_tgtdev_put(tgtdev);
1649 case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
1650 case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
1651 case MPI3_EVENT_SAS_TOPO_PHY_RC_NO_CHANGE:
1653 if (!mrioc->sas_transport_enabled || tgtdev->non_stl
1654 || tgtdev->is_hidden)
1656 link_rate = event_data->phy_entry[i].link_rate >> 4;
1657 prev_link_rate = event_data->phy_entry[i].link_rate & 0xF;
1658 if (link_rate == prev_link_rate)
1660 if (!parent_sas_address)
1662 parent_phy_number = event_data->start_phy_num + i;
1663 mpi3mr_update_links(mrioc, parent_sas_address, handle,
1664 parent_phy_number, link_rate, hba_port);
1671 mpi3mr_tgtdev_put(tgtdev);
1674 if (mrioc->sas_transport_enabled && (event_data->exp_status ==
1675 MPI3_EVENT_SAS_TOPO_ES_NOT_RESPONDING)) {
1677 mpi3mr_expander_remove(mrioc, exp_sas_address,
1683 * mpi3mr_pcietopochg_evt_debug - PCIeTopoChange details
1684 * @mrioc: Adapter instance reference
1685 * @event_data: PCIe topology change list event data
1687 * Prints information about the PCIe topology change event.
1692 mpi3mr_pcietopochg_evt_debug(struct mpi3mr_ioc *mrioc,
1693 struct mpi3_event_data_pcie_topology_change_list *event_data)
1699 char *status_str = NULL;
1700 u8 link_rate, prev_link_rate;
1702 switch (event_data->switch_status) {
1703 case MPI3_EVENT_PCIE_TOPO_SS_NOT_RESPONDING:
1704 status_str = "remove";
1706 case MPI3_EVENT_PCIE_TOPO_SS_RESPONDING:
1707 status_str = "responding";
1709 case MPI3_EVENT_PCIE_TOPO_SS_DELAY_NOT_RESPONDING:
1710 status_str = "remove delay";
1712 case MPI3_EVENT_PCIE_TOPO_SS_NO_PCIE_SWITCH:
1713 status_str = "direct attached";
1716 status_str = "unknown status";
1719 ioc_info(mrioc, "%s :pcie topology change: (%s)\n",
1720 __func__, status_str);
1722 "%s :\tswitch_handle(0x%04x), enclosure_handle(0x%04x) start_port(%02d), num_entries(%d)\n",
1723 __func__, le16_to_cpu(event_data->switch_dev_handle),
1724 le16_to_cpu(event_data->enclosure_handle),
1725 event_data->start_port_num, event_data->num_entries);
1726 for (i = 0; i < event_data->num_entries; i++) {
1728 le16_to_cpu(event_data->port_entry[i].attached_dev_handle);
1731 port_number = event_data->start_port_num + i;
1732 reason_code = event_data->port_entry[i].port_status;
1733 switch (reason_code) {
1734 case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
1735 status_str = "target remove";
1737 case MPI3_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
1738 status_str = "delay target remove";
1740 case MPI3_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
1741 status_str = "link status change";
1743 case MPI3_EVENT_PCIE_TOPO_PS_NO_CHANGE:
1744 status_str = "link status no change";
1746 case MPI3_EVENT_PCIE_TOPO_PS_RESPONDING:
1747 status_str = "target responding";
1750 status_str = "unknown";
1753 link_rate = event_data->port_entry[i].current_port_info &
1754 MPI3_EVENT_PCIE_TOPO_PI_RATE_MASK;
1755 prev_link_rate = event_data->port_entry[i].previous_port_info &
1756 MPI3_EVENT_PCIE_TOPO_PI_RATE_MASK;
1758 "%s :\tport(%02d), attached_handle(0x%04x): %s: link rate: new(0x%02x), old(0x%02x)\n",
1759 __func__, port_number, handle, status_str, link_rate,
1765 * mpi3mr_pcietopochg_evt_bh - PCIeTopologyChange evt bottomhalf
1766 * @mrioc: Adapter instance reference
1767 * @fwevt: Firmware event reference
1769 * Prints information about the PCIe topology change event and
1770 * for "not responding" event code, removes the device from the
1775 static void mpi3mr_pcietopochg_evt_bh(struct mpi3mr_ioc *mrioc,
1776 struct mpi3mr_fwevt *fwevt)
1778 struct mpi3_event_data_pcie_topology_change_list *event_data =
1779 (struct mpi3_event_data_pcie_topology_change_list *)fwevt->event_data;
1783 struct mpi3mr_tgt_dev *tgtdev = NULL;
1785 mpi3mr_pcietopochg_evt_debug(mrioc, event_data);
1787 for (i = 0; i < event_data->num_entries; i++) {
1791 le16_to_cpu(event_data->port_entry[i].attached_dev_handle);
1794 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
1798 reason_code = event_data->port_entry[i].port_status;
1800 switch (reason_code) {
1801 case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
1802 if (tgtdev->host_exposed)
1803 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1804 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, false);
1805 mpi3mr_tgtdev_put(tgtdev);
1811 mpi3mr_tgtdev_put(tgtdev);
1816 * mpi3mr_logdata_evt_bh - Log data event bottomhalf
1817 * @mrioc: Adapter instance reference
1818 * @fwevt: Firmware event reference
1820 * Extracts the event data and calls application interfacing
1821 * function to process the event further.
1825 static void mpi3mr_logdata_evt_bh(struct mpi3mr_ioc *mrioc,
1826 struct mpi3mr_fwevt *fwevt)
1828 mpi3mr_app_save_logdata(mrioc, fwevt->event_data,
1829 fwevt->event_data_size);
1833 * mpi3mr_update_sdev_qd - Update SCSI device queue depath
1834 * @sdev: SCSI device reference
1835 * @data: Queue depth reference
1837 * This is an iterator function called for each SCSI device in a
1838 * target to update the QD of each SCSI device.
1842 static void mpi3mr_update_sdev_qd(struct scsi_device *sdev, void *data)
1844 u16 *q_depth = (u16 *)data;
1846 scsi_change_queue_depth(sdev, (int)*q_depth);
1847 sdev->max_queue_depth = sdev->queue_depth;
1851 * mpi3mr_set_qd_for_all_vd_in_tg -set QD for TG VDs
1852 * @mrioc: Adapter instance reference
1853 * @tg: Throttle group information pointer
1855 * Accessor to reduce QD for each device associated with the
1856 * given throttle group.
1860 static void mpi3mr_set_qd_for_all_vd_in_tg(struct mpi3mr_ioc *mrioc,
1861 struct mpi3mr_throttle_group_info *tg)
1863 unsigned long flags;
1864 struct mpi3mr_tgt_dev *tgtdev;
1865 struct mpi3mr_stgt_priv_data *tgt_priv;
1868 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
1869 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
1870 if (tgtdev->starget && tgtdev->starget->hostdata) {
1871 tgt_priv = tgtdev->starget->hostdata;
1872 if (tgt_priv->throttle_group == tg) {
1873 dprint_event_bh(mrioc,
1874 "updating qd due to throttling for persist_id(%d) original_qd(%d), reduced_qd (%d)\n",
1875 tgt_priv->perst_id, tgtdev->q_depth,
1877 starget_for_each_device(tgtdev->starget,
1878 (void *)&tg->modified_qd,
1879 mpi3mr_update_sdev_qd);
1883 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
1887 * mpi3mr_fwevt_bh - Firmware event bottomhalf handler
1888 * @mrioc: Adapter instance reference
1889 * @fwevt: Firmware event reference
1891 * Identifies the firmware event and calls corresponding bottomg
1892 * half handler and sends event acknowledgment if required.
1896 static void mpi3mr_fwevt_bh(struct mpi3mr_ioc *mrioc,
1897 struct mpi3mr_fwevt *fwevt)
1899 struct mpi3_device_page0 *dev_pg0 = NULL;
1900 u16 perst_id, handle, dev_info;
1901 struct mpi3_device0_sas_sata_format *sasinf = NULL;
1903 mpi3mr_fwevt_del_from_list(mrioc, fwevt);
1904 mrioc->current_event = fwevt;
1906 if (mrioc->stop_drv_processing)
1909 if (mrioc->unrecoverable) {
1910 dprint_event_bh(mrioc,
1911 "ignoring event(0x%02x) in bottom half handler due to unrecoverable controller\n",
1916 if (!fwevt->process_evt)
1919 switch (fwevt->event_id) {
1920 case MPI3_EVENT_DEVICE_ADDED:
1922 dev_pg0 = (struct mpi3_device_page0 *)fwevt->event_data;
1923 perst_id = le16_to_cpu(dev_pg0->persistent_id);
1924 handle = le16_to_cpu(dev_pg0->dev_handle);
1925 if (perst_id != MPI3_DEVICE0_PERSISTENTID_INVALID)
1926 mpi3mr_report_tgtdev_to_host(mrioc, perst_id);
1927 else if (mrioc->sas_transport_enabled &&
1928 (dev_pg0->device_form == MPI3_DEVICE_DEVFORM_SAS_SATA)) {
1929 sasinf = &dev_pg0->device_specific.sas_sata_format;
1930 dev_info = le16_to_cpu(sasinf->device_info);
1931 if (!mrioc->sas_hba.num_phys)
1932 mpi3mr_sas_host_add(mrioc);
1934 mpi3mr_sas_host_refresh(mrioc);
1936 if (mpi3mr_is_expander_device(dev_info))
1937 mpi3mr_expander_add(mrioc, handle);
1941 case MPI3_EVENT_DEVICE_INFO_CHANGED:
1943 dev_pg0 = (struct mpi3_device_page0 *)fwevt->event_data;
1944 perst_id = le16_to_cpu(dev_pg0->persistent_id);
1945 if (perst_id != MPI3_DEVICE0_PERSISTENTID_INVALID)
1946 mpi3mr_devinfochg_evt_bh(mrioc, dev_pg0);
1949 case MPI3_EVENT_DEVICE_STATUS_CHANGE:
1951 mpi3mr_devstatuschg_evt_bh(mrioc, fwevt);
1954 case MPI3_EVENT_ENCL_DEVICE_ADDED:
1955 case MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE:
1957 mpi3mr_encldev_add_chg_evt_bh(mrioc, fwevt);
1961 case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
1963 mpi3mr_sastopochg_evt_bh(mrioc, fwevt);
1966 case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
1968 mpi3mr_pcietopochg_evt_bh(mrioc, fwevt);
1971 case MPI3_EVENT_LOG_DATA:
1973 mpi3mr_logdata_evt_bh(mrioc, fwevt);
1976 case MPI3MR_DRIVER_EVENT_TG_QD_REDUCTION:
1978 struct mpi3mr_throttle_group_info *tg;
1980 tg = *(struct mpi3mr_throttle_group_info **)fwevt->event_data;
1981 dprint_event_bh(mrioc,
1982 "qd reduction event processed for tg_id(%d) reduction_needed(%d)\n",
1983 tg->id, tg->need_qd_reduction);
1984 if (tg->need_qd_reduction) {
1985 mpi3mr_set_qd_for_all_vd_in_tg(mrioc, tg);
1986 tg->need_qd_reduction = 0;
1990 case MPI3_EVENT_WAIT_FOR_DEVICES_TO_REFRESH:
1992 while (mrioc->device_refresh_on)
1995 dprint_event_bh(mrioc,
1996 "scan for non responding and newly added devices after soft reset started\n");
1997 if (mrioc->sas_transport_enabled) {
1998 mpi3mr_refresh_sas_ports(mrioc);
1999 mpi3mr_refresh_expanders(mrioc);
2001 mpi3mr_rfresh_tgtdevs(mrioc);
2003 "scan for non responding and newly added devices after soft reset completed\n");
2011 if (fwevt->send_ack)
2012 mpi3mr_process_event_ack(mrioc, fwevt->event_id,
2015 /* Put fwevt reference count to neutralize kref_init increment */
2016 mpi3mr_fwevt_put(fwevt);
2017 mrioc->current_event = NULL;
2021 * mpi3mr_fwevt_worker - Firmware event worker
2022 * @work: Work struct containing firmware event
2024 * Extracts the firmware event and calls mpi3mr_fwevt_bh.
2028 static void mpi3mr_fwevt_worker(struct work_struct *work)
2030 struct mpi3mr_fwevt *fwevt = container_of(work, struct mpi3mr_fwevt,
2032 mpi3mr_fwevt_bh(fwevt->mrioc, fwevt);
2034 * Put fwevt reference count after
2035 * dequeuing it from worker queue
2037 mpi3mr_fwevt_put(fwevt);
2041 * mpi3mr_create_tgtdev - Create and add a target device
2042 * @mrioc: Adapter instance reference
2043 * @dev_pg0: Device Page 0 data
2045 * If the device specified by the device page 0 data is not
2046 * present in the driver's internal list, allocate the memory
2047 * for the device, populate the data and add to the list, else
2048 * update the device data. The key is persistent ID.
2050 * Return: 0 on success, -ENOMEM on memory allocation failure
2052 static int mpi3mr_create_tgtdev(struct mpi3mr_ioc *mrioc,
2053 struct mpi3_device_page0 *dev_pg0)
2056 struct mpi3mr_tgt_dev *tgtdev = NULL;
2058 unsigned long flags;
2060 perst_id = le16_to_cpu(dev_pg0->persistent_id);
2061 if (perst_id == MPI3_DEVICE0_PERSISTENTID_INVALID)
2064 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
2065 tgtdev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, perst_id);
2067 tgtdev->state = MPI3MR_DEV_CREATED;
2068 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
2071 mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0, true);
2072 mpi3mr_tgtdev_put(tgtdev);
2074 tgtdev = mpi3mr_alloc_tgtdev();
2077 mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0, true);
2078 mpi3mr_tgtdev_add_to_list(mrioc, tgtdev);
2085 * mpi3mr_flush_delayed_cmd_lists - Flush pending commands
2086 * @mrioc: Adapter instance reference
2088 * Flush pending commands in the delayed lists due to a
2089 * controller reset or driver removal as a cleanup.
2093 void mpi3mr_flush_delayed_cmd_lists(struct mpi3mr_ioc *mrioc)
2095 struct delayed_dev_rmhs_node *_rmhs_node;
2096 struct delayed_evt_ack_node *_evtack_node;
2098 dprint_reset(mrioc, "flushing delayed dev_remove_hs commands\n");
2099 while (!list_empty(&mrioc->delayed_rmhs_list)) {
2100 _rmhs_node = list_entry(mrioc->delayed_rmhs_list.next,
2101 struct delayed_dev_rmhs_node, list);
2102 list_del(&_rmhs_node->list);
2105 dprint_reset(mrioc, "flushing delayed event ack commands\n");
2106 while (!list_empty(&mrioc->delayed_evtack_cmds_list)) {
2107 _evtack_node = list_entry(mrioc->delayed_evtack_cmds_list.next,
2108 struct delayed_evt_ack_node, list);
2109 list_del(&_evtack_node->list);
2110 kfree(_evtack_node);
2115 * mpi3mr_dev_rmhs_complete_iou - Device removal IOUC completion
2116 * @mrioc: Adapter instance reference
2117 * @drv_cmd: Internal command tracker
2119 * Issues a target reset TM to the firmware from the device
2120 * removal TM pend list or retry the removal handshake sequence
2121 * based on the IOU control request IOC status.
2125 static void mpi3mr_dev_rmhs_complete_iou(struct mpi3mr_ioc *mrioc,
2126 struct mpi3mr_drv_cmd *drv_cmd)
2128 u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
2129 struct delayed_dev_rmhs_node *delayed_dev_rmhs = NULL;
2131 if (drv_cmd->state & MPI3MR_CMD_RESET)
2135 "%s :dev_rmhs_iouctrl_complete:handle(0x%04x), ioc_status(0x%04x), loginfo(0x%08x)\n",
2136 __func__, drv_cmd->dev_handle, drv_cmd->ioc_status,
2137 drv_cmd->ioc_loginfo);
2138 if (drv_cmd->ioc_status != MPI3_IOCSTATUS_SUCCESS) {
2139 if (drv_cmd->retry_count < MPI3MR_DEV_RMHS_RETRY_COUNT) {
2140 drv_cmd->retry_count++;
2142 "%s :dev_rmhs_iouctrl_complete: handle(0x%04x)retrying handshake retry=%d\n",
2143 __func__, drv_cmd->dev_handle,
2144 drv_cmd->retry_count);
2145 mpi3mr_dev_rmhs_send_tm(mrioc, drv_cmd->dev_handle,
2146 drv_cmd, drv_cmd->iou_rc);
2150 "%s :dev removal handshake failed after all retries: handle(0x%04x)\n",
2151 __func__, drv_cmd->dev_handle);
2154 "%s :dev removal handshake completed successfully: handle(0x%04x)\n",
2155 __func__, drv_cmd->dev_handle);
2156 clear_bit(drv_cmd->dev_handle, mrioc->removepend_bitmap);
2159 if (!list_empty(&mrioc->delayed_rmhs_list)) {
2160 delayed_dev_rmhs = list_entry(mrioc->delayed_rmhs_list.next,
2161 struct delayed_dev_rmhs_node, list);
2162 drv_cmd->dev_handle = delayed_dev_rmhs->handle;
2163 drv_cmd->retry_count = 0;
2164 drv_cmd->iou_rc = delayed_dev_rmhs->iou_rc;
2166 "%s :dev_rmhs_iouctrl_complete: processing delayed TM: handle(0x%04x)\n",
2167 __func__, drv_cmd->dev_handle);
2168 mpi3mr_dev_rmhs_send_tm(mrioc, drv_cmd->dev_handle, drv_cmd,
2170 list_del(&delayed_dev_rmhs->list);
2171 kfree(delayed_dev_rmhs);
2176 drv_cmd->state = MPI3MR_CMD_NOTUSED;
2177 drv_cmd->callback = NULL;
2178 drv_cmd->retry_count = 0;
2179 drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
2180 clear_bit(cmd_idx, mrioc->devrem_bitmap);
2184 * mpi3mr_dev_rmhs_complete_tm - Device removal TM completion
2185 * @mrioc: Adapter instance reference
2186 * @drv_cmd: Internal command tracker
2188 * Issues a target reset TM to the firmware from the device
2189 * removal TM pend list or issue IO unit control request as
2190 * part of device removal or hidden acknowledgment handshake.
2194 static void mpi3mr_dev_rmhs_complete_tm(struct mpi3mr_ioc *mrioc,
2195 struct mpi3mr_drv_cmd *drv_cmd)
2197 struct mpi3_iounit_control_request iou_ctrl;
2198 u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
2199 struct mpi3_scsi_task_mgmt_reply *tm_reply = NULL;
2202 if (drv_cmd->state & MPI3MR_CMD_RESET)
2205 if (drv_cmd->state & MPI3MR_CMD_REPLY_VALID)
2206 tm_reply = (struct mpi3_scsi_task_mgmt_reply *)drv_cmd->reply;
2210 "dev_rmhs_tr_complete:handle(0x%04x), ioc_status(0x%04x), loginfo(0x%08x), term_count(%d)\n",
2211 mrioc->name, drv_cmd->dev_handle, drv_cmd->ioc_status,
2212 drv_cmd->ioc_loginfo,
2213 le32_to_cpu(tm_reply->termination_count));
2215 pr_info(IOCNAME "Issuing IOU CTL: handle(0x%04x) dev_rmhs idx(%d)\n",
2216 mrioc->name, drv_cmd->dev_handle, cmd_idx);
2218 memset(&iou_ctrl, 0, sizeof(iou_ctrl));
2220 drv_cmd->state = MPI3MR_CMD_PENDING;
2221 drv_cmd->is_waiting = 0;
2222 drv_cmd->callback = mpi3mr_dev_rmhs_complete_iou;
2223 iou_ctrl.operation = drv_cmd->iou_rc;
2224 iou_ctrl.param16[0] = cpu_to_le16(drv_cmd->dev_handle);
2225 iou_ctrl.host_tag = cpu_to_le16(drv_cmd->host_tag);
2226 iou_ctrl.function = MPI3_FUNCTION_IO_UNIT_CONTROL;
2228 retval = mpi3mr_admin_request_post(mrioc, &iou_ctrl, sizeof(iou_ctrl),
2231 pr_err(IOCNAME "Issue DevRmHsTMIOUCTL: Admin post failed\n",
2238 drv_cmd->state = MPI3MR_CMD_NOTUSED;
2239 drv_cmd->callback = NULL;
2240 drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
2241 drv_cmd->retry_count = 0;
2242 clear_bit(cmd_idx, mrioc->devrem_bitmap);
2246 * mpi3mr_dev_rmhs_send_tm - Issue TM for device removal
2247 * @mrioc: Adapter instance reference
2248 * @handle: Device handle
2249 * @cmdparam: Internal command tracker
2250 * @iou_rc: IO unit reason code
2252 * Issues a target reset TM to the firmware or add it to a pend
2253 * list as part of device removal or hidden acknowledgment
2258 static void mpi3mr_dev_rmhs_send_tm(struct mpi3mr_ioc *mrioc, u16 handle,
2259 struct mpi3mr_drv_cmd *cmdparam, u8 iou_rc)
2261 struct mpi3_scsi_task_mgmt_request tm_req;
2263 u16 cmd_idx = MPI3MR_NUM_DEVRMCMD;
2265 struct mpi3mr_drv_cmd *drv_cmd = cmdparam;
2266 struct delayed_dev_rmhs_node *delayed_dev_rmhs = NULL;
2267 struct mpi3mr_tgt_dev *tgtdev = NULL;
2268 unsigned long flags;
2270 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
2271 tgtdev = __mpi3mr_get_tgtdev_by_handle(mrioc, handle);
2272 if (tgtdev && (iou_rc == MPI3_CTRL_OP_REMOVE_DEVICE))
2273 tgtdev->state = MPI3MR_DEV_REMOVE_HS_STARTED;
2274 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
2279 cmd_idx = find_first_zero_bit(mrioc->devrem_bitmap,
2280 MPI3MR_NUM_DEVRMCMD);
2281 if (cmd_idx < MPI3MR_NUM_DEVRMCMD) {
2282 if (!test_and_set_bit(cmd_idx, mrioc->devrem_bitmap))
2284 cmd_idx = MPI3MR_NUM_DEVRMCMD;
2286 } while (retrycount--);
2288 if (cmd_idx >= MPI3MR_NUM_DEVRMCMD) {
2289 delayed_dev_rmhs = kzalloc(sizeof(*delayed_dev_rmhs),
2291 if (!delayed_dev_rmhs)
2293 INIT_LIST_HEAD(&delayed_dev_rmhs->list);
2294 delayed_dev_rmhs->handle = handle;
2295 delayed_dev_rmhs->iou_rc = iou_rc;
2296 list_add_tail(&delayed_dev_rmhs->list,
2297 &mrioc->delayed_rmhs_list);
2298 ioc_info(mrioc, "%s :DevRmHs: tr:handle(0x%04x) is postponed\n",
2302 drv_cmd = &mrioc->dev_rmhs_cmds[cmd_idx];
2305 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
2307 "%s :Issuing TR TM: for devhandle 0x%04x with dev_rmhs %d\n",
2308 __func__, handle, cmd_idx);
2310 memset(&tm_req, 0, sizeof(tm_req));
2311 if (drv_cmd->state & MPI3MR_CMD_PENDING) {
2312 ioc_err(mrioc, "%s :Issue TM: Command is in use\n", __func__);
2315 drv_cmd->state = MPI3MR_CMD_PENDING;
2316 drv_cmd->is_waiting = 0;
2317 drv_cmd->callback = mpi3mr_dev_rmhs_complete_tm;
2318 drv_cmd->dev_handle = handle;
2319 drv_cmd->iou_rc = iou_rc;
2320 tm_req.dev_handle = cpu_to_le16(handle);
2321 tm_req.task_type = MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
2322 tm_req.host_tag = cpu_to_le16(drv_cmd->host_tag);
2323 tm_req.task_host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INVALID);
2324 tm_req.function = MPI3_FUNCTION_SCSI_TASK_MGMT;
2326 set_bit(handle, mrioc->removepend_bitmap);
2327 retval = mpi3mr_admin_request_post(mrioc, &tm_req, sizeof(tm_req), 1);
2329 ioc_err(mrioc, "%s :Issue DevRmHsTM: Admin Post failed\n",
2336 drv_cmd->state = MPI3MR_CMD_NOTUSED;
2337 drv_cmd->callback = NULL;
2338 drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
2339 drv_cmd->retry_count = 0;
2340 clear_bit(cmd_idx, mrioc->devrem_bitmap);
2344 * mpi3mr_complete_evt_ack - event ack request completion
2345 * @mrioc: Adapter instance reference
2346 * @drv_cmd: Internal command tracker
2348 * This is the completion handler for non blocking event
2349 * acknowledgment sent to the firmware and this will issue any
2350 * pending event acknowledgment request.
2354 static void mpi3mr_complete_evt_ack(struct mpi3mr_ioc *mrioc,
2355 struct mpi3mr_drv_cmd *drv_cmd)
2357 u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
2358 struct delayed_evt_ack_node *delayed_evtack = NULL;
2360 if (drv_cmd->state & MPI3MR_CMD_RESET)
2363 if (drv_cmd->ioc_status != MPI3_IOCSTATUS_SUCCESS) {
2364 dprint_event_th(mrioc,
2365 "immediate event ack failed with ioc_status(0x%04x) log_info(0x%08x)\n",
2366 (drv_cmd->ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
2367 drv_cmd->ioc_loginfo);
2370 if (!list_empty(&mrioc->delayed_evtack_cmds_list)) {
2372 list_entry(mrioc->delayed_evtack_cmds_list.next,
2373 struct delayed_evt_ack_node, list);
2374 mpi3mr_send_event_ack(mrioc, delayed_evtack->event, drv_cmd,
2375 delayed_evtack->event_ctx);
2376 list_del(&delayed_evtack->list);
2377 kfree(delayed_evtack);
2381 drv_cmd->state = MPI3MR_CMD_NOTUSED;
2382 drv_cmd->callback = NULL;
2383 clear_bit(cmd_idx, mrioc->evtack_cmds_bitmap);
2387 * mpi3mr_send_event_ack - Issue event acknwoledgment request
2388 * @mrioc: Adapter instance reference
2389 * @event: MPI3 event id
2390 * @cmdparam: Internal command tracker
2391 * @event_ctx: event context
2393 * Issues event acknowledgment request to the firmware if there
2394 * is a free command to send the event ack else it to a pend
2395 * list so that it will be processed on a completion of a prior
2396 * event acknowledgment .
2400 static void mpi3mr_send_event_ack(struct mpi3mr_ioc *mrioc, u8 event,
2401 struct mpi3mr_drv_cmd *cmdparam, u32 event_ctx)
2403 struct mpi3_event_ack_request evtack_req;
2406 u16 cmd_idx = MPI3MR_NUM_EVTACKCMD;
2407 struct mpi3mr_drv_cmd *drv_cmd = cmdparam;
2408 struct delayed_evt_ack_node *delayed_evtack = NULL;
2411 dprint_event_th(mrioc,
2412 "sending delayed event ack in the top half for event(0x%02x), event_ctx(0x%08x)\n",
2416 dprint_event_th(mrioc,
2417 "sending event ack in the top half for event(0x%02x), event_ctx(0x%08x)\n",
2420 cmd_idx = find_first_zero_bit(mrioc->evtack_cmds_bitmap,
2421 MPI3MR_NUM_EVTACKCMD);
2422 if (cmd_idx < MPI3MR_NUM_EVTACKCMD) {
2423 if (!test_and_set_bit(cmd_idx,
2424 mrioc->evtack_cmds_bitmap))
2426 cmd_idx = MPI3MR_NUM_EVTACKCMD;
2428 } while (retrycount--);
2430 if (cmd_idx >= MPI3MR_NUM_EVTACKCMD) {
2431 delayed_evtack = kzalloc(sizeof(*delayed_evtack),
2433 if (!delayed_evtack)
2435 INIT_LIST_HEAD(&delayed_evtack->list);
2436 delayed_evtack->event = event;
2437 delayed_evtack->event_ctx = event_ctx;
2438 list_add_tail(&delayed_evtack->list,
2439 &mrioc->delayed_evtack_cmds_list);
2440 dprint_event_th(mrioc,
2441 "event ack in the top half for event(0x%02x), event_ctx(0x%08x) is postponed\n",
2445 drv_cmd = &mrioc->evtack_cmds[cmd_idx];
2448 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
2450 memset(&evtack_req, 0, sizeof(evtack_req));
2451 if (drv_cmd->state & MPI3MR_CMD_PENDING) {
2452 dprint_event_th(mrioc,
2453 "sending event ack failed due to command in use\n");
2456 drv_cmd->state = MPI3MR_CMD_PENDING;
2457 drv_cmd->is_waiting = 0;
2458 drv_cmd->callback = mpi3mr_complete_evt_ack;
2459 evtack_req.host_tag = cpu_to_le16(drv_cmd->host_tag);
2460 evtack_req.function = MPI3_FUNCTION_EVENT_ACK;
2461 evtack_req.event = event;
2462 evtack_req.event_context = cpu_to_le32(event_ctx);
2463 retval = mpi3mr_admin_request_post(mrioc, &evtack_req,
2464 sizeof(evtack_req), 1);
2466 dprint_event_th(mrioc,
2467 "posting event ack request is failed\n");
2471 dprint_event_th(mrioc,
2472 "event ack in the top half for event(0x%02x), event_ctx(0x%08x) is posted\n",
2477 drv_cmd->state = MPI3MR_CMD_NOTUSED;
2478 drv_cmd->callback = NULL;
2479 clear_bit(cmd_idx, mrioc->evtack_cmds_bitmap);
2483 * mpi3mr_pcietopochg_evt_th - PCIETopologyChange evt tophalf
2484 * @mrioc: Adapter instance reference
2485 * @event_reply: event data
2487 * Checks for the reason code and based on that either block I/O
2488 * to device, or unblock I/O to the device, or start the device
2489 * removal handshake with reason as remove with the firmware for
2494 static void mpi3mr_pcietopochg_evt_th(struct mpi3mr_ioc *mrioc,
2495 struct mpi3_event_notification_reply *event_reply)
2497 struct mpi3_event_data_pcie_topology_change_list *topo_evt =
2498 (struct mpi3_event_data_pcie_topology_change_list *)event_reply->event_data;
2502 struct mpi3mr_tgt_dev *tgtdev = NULL;
2503 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2505 for (i = 0; i < topo_evt->num_entries; i++) {
2506 handle = le16_to_cpu(topo_evt->port_entry[i].attached_dev_handle);
2509 reason_code = topo_evt->port_entry[i].port_status;
2510 scsi_tgt_priv_data = NULL;
2511 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
2512 if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
2513 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2514 tgtdev->starget->hostdata;
2515 switch (reason_code) {
2516 case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
2517 if (scsi_tgt_priv_data) {
2518 scsi_tgt_priv_data->dev_removed = 1;
2519 scsi_tgt_priv_data->dev_removedelay = 0;
2520 atomic_set(&scsi_tgt_priv_data->block_io, 0);
2522 mpi3mr_dev_rmhs_send_tm(mrioc, handle, NULL,
2523 MPI3_CTRL_OP_REMOVE_DEVICE);
2525 case MPI3_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
2526 if (scsi_tgt_priv_data) {
2527 scsi_tgt_priv_data->dev_removedelay = 1;
2528 atomic_inc(&scsi_tgt_priv_data->block_io);
2531 case MPI3_EVENT_PCIE_TOPO_PS_RESPONDING:
2532 if (scsi_tgt_priv_data &&
2533 scsi_tgt_priv_data->dev_removedelay) {
2534 scsi_tgt_priv_data->dev_removedelay = 0;
2535 atomic_dec_if_positive
2536 (&scsi_tgt_priv_data->block_io);
2539 case MPI3_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
2544 mpi3mr_tgtdev_put(tgtdev);
2549 * mpi3mr_sastopochg_evt_th - SASTopologyChange evt tophalf
2550 * @mrioc: Adapter instance reference
2551 * @event_reply: event data
2553 * Checks for the reason code and based on that either block I/O
2554 * to device, or unblock I/O to the device, or start the device
2555 * removal handshake with reason as remove with the firmware for
2560 static void mpi3mr_sastopochg_evt_th(struct mpi3mr_ioc *mrioc,
2561 struct mpi3_event_notification_reply *event_reply)
2563 struct mpi3_event_data_sas_topology_change_list *topo_evt =
2564 (struct mpi3_event_data_sas_topology_change_list *)event_reply->event_data;
2568 struct mpi3mr_tgt_dev *tgtdev = NULL;
2569 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2571 for (i = 0; i < topo_evt->num_entries; i++) {
2572 handle = le16_to_cpu(topo_evt->phy_entry[i].attached_dev_handle);
2575 reason_code = topo_evt->phy_entry[i].status &
2576 MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
2577 scsi_tgt_priv_data = NULL;
2578 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
2579 if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
2580 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2581 tgtdev->starget->hostdata;
2582 switch (reason_code) {
2583 case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
2584 if (scsi_tgt_priv_data) {
2585 scsi_tgt_priv_data->dev_removed = 1;
2586 scsi_tgt_priv_data->dev_removedelay = 0;
2587 atomic_set(&scsi_tgt_priv_data->block_io, 0);
2589 mpi3mr_dev_rmhs_send_tm(mrioc, handle, NULL,
2590 MPI3_CTRL_OP_REMOVE_DEVICE);
2592 case MPI3_EVENT_SAS_TOPO_PHY_RC_DELAY_NOT_RESPONDING:
2593 if (scsi_tgt_priv_data) {
2594 scsi_tgt_priv_data->dev_removedelay = 1;
2595 atomic_inc(&scsi_tgt_priv_data->block_io);
2598 case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
2599 if (scsi_tgt_priv_data &&
2600 scsi_tgt_priv_data->dev_removedelay) {
2601 scsi_tgt_priv_data->dev_removedelay = 0;
2602 atomic_dec_if_positive
2603 (&scsi_tgt_priv_data->block_io);
2606 case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
2611 mpi3mr_tgtdev_put(tgtdev);
2616 * mpi3mr_devstatuschg_evt_th - DeviceStatusChange evt tophalf
2617 * @mrioc: Adapter instance reference
2618 * @event_reply: event data
2620 * Checks for the reason code and based on that either block I/O
2621 * to device, or unblock I/O to the device, or start the device
2622 * removal handshake with reason as remove/hide acknowledgment
2623 * with the firmware.
2627 static void mpi3mr_devstatuschg_evt_th(struct mpi3mr_ioc *mrioc,
2628 struct mpi3_event_notification_reply *event_reply)
2631 u8 ublock = 0, block = 0, hide = 0, delete = 0, remove = 0;
2632 struct mpi3mr_tgt_dev *tgtdev = NULL;
2633 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2634 struct mpi3_event_data_device_status_change *evtdata =
2635 (struct mpi3_event_data_device_status_change *)event_reply->event_data;
2637 if (mrioc->stop_drv_processing)
2640 dev_handle = le16_to_cpu(evtdata->dev_handle);
2642 switch (evtdata->reason_code) {
2643 case MPI3_EVENT_DEV_STAT_RC_INT_DEVICE_RESET_STRT:
2644 case MPI3_EVENT_DEV_STAT_RC_INT_IT_NEXUS_RESET_STRT:
2647 case MPI3_EVENT_DEV_STAT_RC_HIDDEN:
2651 case MPI3_EVENT_DEV_STAT_RC_VD_NOT_RESPONDING:
2655 case MPI3_EVENT_DEV_STAT_RC_INT_DEVICE_RESET_CMP:
2656 case MPI3_EVENT_DEV_STAT_RC_INT_IT_NEXUS_RESET_CMP:
2663 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
2667 tgtdev->is_hidden = hide;
2668 if (tgtdev->starget && tgtdev->starget->hostdata) {
2669 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2670 tgtdev->starget->hostdata;
2672 atomic_inc(&scsi_tgt_priv_data->block_io);
2674 scsi_tgt_priv_data->dev_removed = 1;
2676 atomic_dec_if_positive(&scsi_tgt_priv_data->block_io);
2679 mpi3mr_dev_rmhs_send_tm(mrioc, dev_handle, NULL,
2680 MPI3_CTRL_OP_REMOVE_DEVICE);
2682 mpi3mr_dev_rmhs_send_tm(mrioc, dev_handle, NULL,
2683 MPI3_CTRL_OP_HIDDEN_ACK);
2687 mpi3mr_tgtdev_put(tgtdev);
2691 * mpi3mr_preparereset_evt_th - Prepare for reset event tophalf
2692 * @mrioc: Adapter instance reference
2693 * @event_reply: event data
2695 * Blocks and unblocks host level I/O based on the reason code
2699 static void mpi3mr_preparereset_evt_th(struct mpi3mr_ioc *mrioc,
2700 struct mpi3_event_notification_reply *event_reply)
2702 struct mpi3_event_data_prepare_for_reset *evtdata =
2703 (struct mpi3_event_data_prepare_for_reset *)event_reply->event_data;
2705 if (evtdata->reason_code == MPI3_EVENT_PREPARE_RESET_RC_START) {
2706 dprint_event_th(mrioc,
2707 "prepare for reset event top half with rc=start\n");
2708 if (mrioc->prepare_for_reset)
2710 mrioc->prepare_for_reset = 1;
2711 mrioc->prepare_for_reset_timeout_counter = 0;
2712 } else if (evtdata->reason_code == MPI3_EVENT_PREPARE_RESET_RC_ABORT) {
2713 dprint_event_th(mrioc,
2714 "prepare for reset top half with rc=abort\n");
2715 mrioc->prepare_for_reset = 0;
2716 mrioc->prepare_for_reset_timeout_counter = 0;
2718 if ((event_reply->msg_flags & MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_MASK)
2719 == MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_REQUIRED)
2720 mpi3mr_send_event_ack(mrioc, event_reply->event, NULL,
2721 le32_to_cpu(event_reply->event_context));
2725 * mpi3mr_energypackchg_evt_th - Energy pack change evt tophalf
2726 * @mrioc: Adapter instance reference
2727 * @event_reply: event data
2729 * Identifies the new shutdown timeout value and update.
2733 static void mpi3mr_energypackchg_evt_th(struct mpi3mr_ioc *mrioc,
2734 struct mpi3_event_notification_reply *event_reply)
2736 struct mpi3_event_data_energy_pack_change *evtdata =
2737 (struct mpi3_event_data_energy_pack_change *)event_reply->event_data;
2738 u16 shutdown_timeout = le16_to_cpu(evtdata->shutdown_timeout);
2740 if (shutdown_timeout <= 0) {
2742 "%s :Invalid Shutdown Timeout received = %d\n",
2743 __func__, shutdown_timeout);
2748 "%s :Previous Shutdown Timeout Value = %d New Shutdown Timeout Value = %d\n",
2749 __func__, mrioc->facts.shutdown_timeout, shutdown_timeout);
2750 mrioc->facts.shutdown_timeout = shutdown_timeout;
2754 * mpi3mr_cablemgmt_evt_th - Cable management event tophalf
2755 * @mrioc: Adapter instance reference
2756 * @event_reply: event data
2758 * Displays Cable manegemt event details.
2762 static void mpi3mr_cablemgmt_evt_th(struct mpi3mr_ioc *mrioc,
2763 struct mpi3_event_notification_reply *event_reply)
2765 struct mpi3_event_data_cable_management *evtdata =
2766 (struct mpi3_event_data_cable_management *)event_reply->event_data;
2768 switch (evtdata->status) {
2769 case MPI3_EVENT_CABLE_MGMT_STATUS_INSUFFICIENT_POWER:
2771 ioc_info(mrioc, "An active cable with receptacle_id %d cannot be powered.\n"
2772 "Devices connected to this cable are not detected.\n"
2773 "This cable requires %d mW of power.\n",
2774 evtdata->receptacle_id,
2775 le32_to_cpu(evtdata->active_cable_power_requirement));
2778 case MPI3_EVENT_CABLE_MGMT_STATUS_DEGRADED:
2780 ioc_info(mrioc, "A cable with receptacle_id %d is not running at optimal speed\n",
2781 evtdata->receptacle_id);
2790 * mpi3mr_add_event_wait_for_device_refresh - Add Wait for Device Refresh Event
2791 * @mrioc: Adapter instance reference
2793 * Add driver specific event to make sure that the driver won't process the
2794 * events until all the devices are refreshed during soft reset.
2798 void mpi3mr_add_event_wait_for_device_refresh(struct mpi3mr_ioc *mrioc)
2800 struct mpi3mr_fwevt *fwevt = NULL;
2802 fwevt = mpi3mr_alloc_fwevt(0);
2804 dprint_event_th(mrioc,
2805 "failed to schedule bottom half handler for event(0x%02x)\n",
2806 MPI3_EVENT_WAIT_FOR_DEVICES_TO_REFRESH);
2809 fwevt->mrioc = mrioc;
2810 fwevt->event_id = MPI3_EVENT_WAIT_FOR_DEVICES_TO_REFRESH;
2811 fwevt->send_ack = 0;
2812 fwevt->process_evt = 1;
2814 fwevt->event_data_size = 0;
2815 mpi3mr_fwevt_add_to_list(mrioc, fwevt);
2819 * mpi3mr_os_handle_events - Firmware event handler
2820 * @mrioc: Adapter instance reference
2821 * @event_reply: event data
2823 * Identify whteher the event has to handled and acknowledged
2824 * and either process the event in the tophalf and/or schedule a
2825 * bottom half through mpi3mr_fwevt_worker.
2829 void mpi3mr_os_handle_events(struct mpi3mr_ioc *mrioc,
2830 struct mpi3_event_notification_reply *event_reply)
2833 struct mpi3mr_fwevt *fwevt = NULL;
2834 bool ack_req = 0, process_evt_bh = 0;
2836 if (mrioc->stop_drv_processing)
2839 if ((event_reply->msg_flags & MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_MASK)
2840 == MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_REQUIRED)
2843 evt_type = event_reply->event;
2846 case MPI3_EVENT_DEVICE_ADDED:
2848 struct mpi3_device_page0 *dev_pg0 =
2849 (struct mpi3_device_page0 *)event_reply->event_data;
2850 if (mpi3mr_create_tgtdev(mrioc, dev_pg0))
2852 "%s :Failed to add device in the device add event\n",
2858 case MPI3_EVENT_DEVICE_STATUS_CHANGE:
2861 mpi3mr_devstatuschg_evt_th(mrioc, event_reply);
2864 case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
2867 mpi3mr_sastopochg_evt_th(mrioc, event_reply);
2870 case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
2873 mpi3mr_pcietopochg_evt_th(mrioc, event_reply);
2876 case MPI3_EVENT_PREPARE_FOR_RESET:
2878 mpi3mr_preparereset_evt_th(mrioc, event_reply);
2882 case MPI3_EVENT_DEVICE_INFO_CHANGED:
2883 case MPI3_EVENT_LOG_DATA:
2884 case MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE:
2885 case MPI3_EVENT_ENCL_DEVICE_ADDED:
2890 case MPI3_EVENT_ENERGY_PACK_CHANGE:
2892 mpi3mr_energypackchg_evt_th(mrioc, event_reply);
2895 case MPI3_EVENT_CABLE_MGMT:
2897 mpi3mr_cablemgmt_evt_th(mrioc, event_reply);
2900 case MPI3_EVENT_SAS_DISCOVERY:
2901 case MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
2902 case MPI3_EVENT_SAS_BROADCAST_PRIMITIVE:
2903 case MPI3_EVENT_PCIE_ENUMERATION:
2906 ioc_info(mrioc, "%s :event 0x%02x is not handled\n",
2907 __func__, evt_type);
2910 if (process_evt_bh || ack_req) {
2911 sz = event_reply->event_data_length * 4;
2912 fwevt = mpi3mr_alloc_fwevt(sz);
2914 ioc_info(mrioc, "%s :failure at %s:%d/%s()!\n",
2915 __func__, __FILE__, __LINE__, __func__);
2919 memcpy(fwevt->event_data, event_reply->event_data, sz);
2920 fwevt->mrioc = mrioc;
2921 fwevt->event_id = evt_type;
2922 fwevt->send_ack = ack_req;
2923 fwevt->process_evt = process_evt_bh;
2924 fwevt->evt_ctx = le32_to_cpu(event_reply->event_context);
2925 mpi3mr_fwevt_add_to_list(mrioc, fwevt);
2930 * mpi3mr_setup_eedp - Setup EEDP information in MPI3 SCSI IO
2931 * @mrioc: Adapter instance reference
2932 * @scmd: SCSI command reference
2933 * @scsiio_req: MPI3 SCSI IO request
2935 * Identifies the protection information flags from the SCSI
2936 * command and set appropriate flags in the MPI3 SCSI IO
2941 static void mpi3mr_setup_eedp(struct mpi3mr_ioc *mrioc,
2942 struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
2945 unsigned char prot_op = scsi_get_prot_op(scmd);
2948 case SCSI_PROT_NORMAL:
2950 case SCSI_PROT_READ_STRIP:
2951 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REMOVE;
2953 case SCSI_PROT_WRITE_INSERT:
2954 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_INSERT;
2956 case SCSI_PROT_READ_INSERT:
2957 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_INSERT;
2958 scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2960 case SCSI_PROT_WRITE_STRIP:
2961 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REMOVE;
2962 scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2964 case SCSI_PROT_READ_PASS:
2965 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK;
2966 scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2968 case SCSI_PROT_WRITE_PASS:
2969 if (scmd->prot_flags & SCSI_PROT_IP_CHECKSUM) {
2970 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REGEN;
2971 scsiio_req->sgl[0].eedp.application_tag_translation_mask =
2974 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK;
2976 scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2982 if (scmd->prot_flags & SCSI_PROT_GUARD_CHECK)
2983 eedp_flags |= MPI3_EEDPFLAGS_CHK_GUARD;
2985 if (scmd->prot_flags & SCSI_PROT_IP_CHECKSUM)
2986 eedp_flags |= MPI3_EEDPFLAGS_HOST_GUARD_IP_CHKSUM;
2988 if (scmd->prot_flags & SCSI_PROT_REF_CHECK) {
2989 eedp_flags |= MPI3_EEDPFLAGS_CHK_REF_TAG |
2990 MPI3_EEDPFLAGS_INCR_PRI_REF_TAG;
2991 scsiio_req->cdb.eedp32.primary_reference_tag =
2992 cpu_to_be32(scsi_prot_ref_tag(scmd));
2995 if (scmd->prot_flags & SCSI_PROT_REF_INCREMENT)
2996 eedp_flags |= MPI3_EEDPFLAGS_INCR_PRI_REF_TAG;
2998 eedp_flags |= MPI3_EEDPFLAGS_ESC_MODE_APPTAG_DISABLE;
3000 switch (scsi_prot_interval(scmd)) {
3002 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_512;
3005 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_520;
3008 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4080;
3011 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4088;
3014 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4096;
3017 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4104;
3020 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4160;
3026 scsiio_req->sgl[0].eedp.eedp_flags = cpu_to_le16(eedp_flags);
3027 scsiio_req->sgl[0].eedp.flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_EXTENDED;
3031 * mpi3mr_build_sense_buffer - Map sense information
3033 * @buf: Sense buffer to populate
3035 * @asc: Additional sense code
3036 * @ascq: Additional sense code qualifier
3038 * Maps the given sense information into either descriptor or
3039 * fixed format sense data.
3043 static inline void mpi3mr_build_sense_buffer(int desc, u8 *buf, u8 key,
3047 buf[0] = 0x72; /* descriptor, current */
3053 buf[0] = 0x70; /* fixed, current */
3062 * mpi3mr_map_eedp_error - Map EEDP errors from IOC status
3063 * @scmd: SCSI command reference
3064 * @ioc_status: status of MPI3 request
3066 * Maps the EEDP error status of the SCSI IO request to sense
3071 static void mpi3mr_map_eedp_error(struct scsi_cmnd *scmd,
3076 switch (ioc_status) {
3077 case MPI3_IOCSTATUS_EEDP_GUARD_ERROR:
3080 case MPI3_IOCSTATUS_EEDP_APP_TAG_ERROR:
3083 case MPI3_IOCSTATUS_EEDP_REF_TAG_ERROR:
3091 mpi3mr_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
3093 scmd->result = (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION;
3097 * mpi3mr_process_op_reply_desc - reply descriptor handler
3098 * @mrioc: Adapter instance reference
3099 * @reply_desc: Operational reply descriptor
3100 * @reply_dma: place holder for reply DMA address
3101 * @qidx: Operational queue index
3103 * Process the operational reply descriptor and identifies the
3104 * descriptor type. Based on the descriptor map the MPI3 request
3105 * status to a SCSI command status and calls scsi_done call
3110 void mpi3mr_process_op_reply_desc(struct mpi3mr_ioc *mrioc,
3111 struct mpi3_default_reply_descriptor *reply_desc, u64 *reply_dma, u16 qidx)
3113 u16 reply_desc_type, host_tag = 0;
3114 u16 ioc_status = MPI3_IOCSTATUS_SUCCESS;
3115 u32 ioc_loginfo = 0;
3116 struct mpi3_status_reply_descriptor *status_desc = NULL;
3117 struct mpi3_address_reply_descriptor *addr_desc = NULL;
3118 struct mpi3_success_reply_descriptor *success_desc = NULL;
3119 struct mpi3_scsi_io_reply *scsi_reply = NULL;
3120 struct scsi_cmnd *scmd = NULL;
3121 struct scmd_priv *priv = NULL;
3122 u8 *sense_buf = NULL;
3123 u8 scsi_state = 0, scsi_status = 0, sense_state = 0;
3124 u32 xfer_count = 0, sense_count = 0, resp_data = 0;
3125 u16 dev_handle = 0xFFFF;
3126 struct scsi_sense_hdr sshdr;
3127 struct mpi3mr_stgt_priv_data *stgt_priv_data = NULL;
3128 struct mpi3mr_sdev_priv_data *sdev_priv_data = NULL;
3129 u32 ioc_pend_data_len = 0, tg_pend_data_len = 0, data_len_blks = 0;
3130 struct mpi3mr_throttle_group_info *tg = NULL;
3131 u8 throttle_enabled_dev = 0;
3134 reply_desc_type = le16_to_cpu(reply_desc->reply_flags) &
3135 MPI3_REPLY_DESCRIPT_FLAGS_TYPE_MASK;
3136 switch (reply_desc_type) {
3137 case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_STATUS:
3138 status_desc = (struct mpi3_status_reply_descriptor *)reply_desc;
3139 host_tag = le16_to_cpu(status_desc->host_tag);
3140 ioc_status = le16_to_cpu(status_desc->ioc_status);
3142 MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
3143 ioc_loginfo = le32_to_cpu(status_desc->ioc_log_info);
3144 ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
3146 case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_ADDRESS_REPLY:
3147 addr_desc = (struct mpi3_address_reply_descriptor *)reply_desc;
3148 *reply_dma = le64_to_cpu(addr_desc->reply_frame_address);
3149 scsi_reply = mpi3mr_get_reply_virt_addr(mrioc,
3152 panic("%s: scsi_reply is NULL, this shouldn't happen\n",
3156 host_tag = le16_to_cpu(scsi_reply->host_tag);
3157 ioc_status = le16_to_cpu(scsi_reply->ioc_status);
3158 scsi_status = scsi_reply->scsi_status;
3159 scsi_state = scsi_reply->scsi_state;
3160 dev_handle = le16_to_cpu(scsi_reply->dev_handle);
3161 sense_state = (scsi_state & MPI3_SCSI_STATE_SENSE_MASK);
3162 xfer_count = le32_to_cpu(scsi_reply->transfer_count);
3163 sense_count = le32_to_cpu(scsi_reply->sense_count);
3164 resp_data = le32_to_cpu(scsi_reply->response_data);
3165 sense_buf = mpi3mr_get_sensebuf_virt_addr(mrioc,
3166 le64_to_cpu(scsi_reply->sense_data_buffer_address));
3168 MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
3169 ioc_loginfo = le32_to_cpu(scsi_reply->ioc_log_info);
3170 ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
3171 if (sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY)
3172 panic("%s: Ran out of sense buffers\n", mrioc->name);
3174 case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_SUCCESS:
3175 success_desc = (struct mpi3_success_reply_descriptor *)reply_desc;
3176 host_tag = le16_to_cpu(success_desc->host_tag);
3181 scmd = mpi3mr_scmd_from_host_tag(mrioc, host_tag, qidx);
3183 panic("%s: Cannot Identify scmd for host_tag 0x%x\n",
3184 mrioc->name, host_tag);
3187 priv = scsi_cmd_priv(scmd);
3189 data_len_blks = scsi_bufflen(scmd) >> 9;
3190 sdev_priv_data = scmd->device->hostdata;
3191 if (sdev_priv_data) {
3192 stgt_priv_data = sdev_priv_data->tgt_priv_data;
3193 if (stgt_priv_data) {
3194 tg = stgt_priv_data->throttle_group;
3195 throttle_enabled_dev =
3196 stgt_priv_data->io_throttle_enabled;
3199 if (unlikely((data_len_blks >= mrioc->io_throttle_data_length) &&
3200 throttle_enabled_dev)) {
3201 ioc_pend_data_len = atomic_sub_return(data_len_blks,
3202 &mrioc->pend_large_data_sz);
3204 tg_pend_data_len = atomic_sub_return(data_len_blks,
3205 &tg->pend_large_data_sz);
3206 if (tg->io_divert && ((ioc_pend_data_len <=
3207 mrioc->io_throttle_low) &&
3208 (tg_pend_data_len <= tg->low))) {
3210 mpi3mr_set_io_divert_for_all_vd_in_tg(
3214 if (ioc_pend_data_len <= mrioc->io_throttle_low)
3215 stgt_priv_data->io_divert = 0;
3217 } else if (unlikely((stgt_priv_data && stgt_priv_data->io_divert))) {
3218 ioc_pend_data_len = atomic_read(&mrioc->pend_large_data_sz);
3220 if (ioc_pend_data_len <= mrioc->io_throttle_low)
3221 stgt_priv_data->io_divert = 0;
3223 } else if (ioc_pend_data_len <= mrioc->io_throttle_low) {
3224 tg_pend_data_len = atomic_read(&tg->pend_large_data_sz);
3225 if (tg->io_divert && (tg_pend_data_len <= tg->low)) {
3227 mpi3mr_set_io_divert_for_all_vd_in_tg(
3234 scmd->result = DID_OK << 16;
3238 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_count);
3239 if (ioc_status == MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN &&
3240 xfer_count == 0 && (scsi_status == MPI3_SCSI_STATUS_BUSY ||
3241 scsi_status == MPI3_SCSI_STATUS_RESERVATION_CONFLICT ||
3242 scsi_status == MPI3_SCSI_STATUS_TASK_SET_FULL))
3243 ioc_status = MPI3_IOCSTATUS_SUCCESS;
3245 if ((sense_state == MPI3_SCSI_STATE_SENSE_VALID) && sense_count &&
3247 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE, sense_count);
3249 memcpy(scmd->sense_buffer, sense_buf, sz);
3252 switch (ioc_status) {
3253 case MPI3_IOCSTATUS_BUSY:
3254 case MPI3_IOCSTATUS_INSUFFICIENT_RESOURCES:
3255 scmd->result = SAM_STAT_BUSY;
3257 case MPI3_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3258 scmd->result = DID_NO_CONNECT << 16;
3260 case MPI3_IOCSTATUS_SCSI_IOC_TERMINATED:
3261 scmd->result = DID_SOFT_ERROR << 16;
3263 case MPI3_IOCSTATUS_SCSI_TASK_TERMINATED:
3264 case MPI3_IOCSTATUS_SCSI_EXT_TERMINATED:
3265 scmd->result = DID_RESET << 16;
3267 case MPI3_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3268 if ((xfer_count == 0) || (scmd->underflow > xfer_count))
3269 scmd->result = DID_SOFT_ERROR << 16;
3271 scmd->result = (DID_OK << 16) | scsi_status;
3273 case MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN:
3274 scmd->result = (DID_OK << 16) | scsi_status;
3275 if (sense_state == MPI3_SCSI_STATE_SENSE_VALID)
3277 if (xfer_count < scmd->underflow) {
3278 if (scsi_status == SAM_STAT_BUSY)
3279 scmd->result = SAM_STAT_BUSY;
3281 scmd->result = DID_SOFT_ERROR << 16;
3282 } else if ((scsi_state & (MPI3_SCSI_STATE_NO_SCSI_STATUS)) ||
3283 (sense_state != MPI3_SCSI_STATE_SENSE_NOT_AVAILABLE))
3284 scmd->result = DID_SOFT_ERROR << 16;
3285 else if (scsi_state & MPI3_SCSI_STATE_TERMINATED)
3286 scmd->result = DID_RESET << 16;
3288 case MPI3_IOCSTATUS_SCSI_DATA_OVERRUN:
3289 scsi_set_resid(scmd, 0);
3291 case MPI3_IOCSTATUS_SCSI_RECOVERED_ERROR:
3292 case MPI3_IOCSTATUS_SUCCESS:
3293 scmd->result = (DID_OK << 16) | scsi_status;
3294 if ((scsi_state & (MPI3_SCSI_STATE_NO_SCSI_STATUS)) ||
3295 (sense_state == MPI3_SCSI_STATE_SENSE_FAILED) ||
3296 (sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY))
3297 scmd->result = DID_SOFT_ERROR << 16;
3298 else if (scsi_state & MPI3_SCSI_STATE_TERMINATED)
3299 scmd->result = DID_RESET << 16;
3301 case MPI3_IOCSTATUS_EEDP_GUARD_ERROR:
3302 case MPI3_IOCSTATUS_EEDP_REF_TAG_ERROR:
3303 case MPI3_IOCSTATUS_EEDP_APP_TAG_ERROR:
3304 mpi3mr_map_eedp_error(scmd, ioc_status);
3306 case MPI3_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3307 case MPI3_IOCSTATUS_INVALID_FUNCTION:
3308 case MPI3_IOCSTATUS_INVALID_SGL:
3309 case MPI3_IOCSTATUS_INTERNAL_ERROR:
3310 case MPI3_IOCSTATUS_INVALID_FIELD:
3311 case MPI3_IOCSTATUS_INVALID_STATE:
3312 case MPI3_IOCSTATUS_SCSI_IO_DATA_ERROR:
3313 case MPI3_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3314 case MPI3_IOCSTATUS_INSUFFICIENT_POWER:
3316 scmd->result = DID_SOFT_ERROR << 16;
3320 if (scmd->result != (DID_OK << 16) && (scmd->cmnd[0] != ATA_12) &&
3321 (scmd->cmnd[0] != ATA_16) &&
3322 mrioc->logging_level & MPI3_DEBUG_SCSI_ERROR) {
3323 ioc_info(mrioc, "%s :scmd->result 0x%x\n", __func__,
3325 scsi_print_command(scmd);
3327 "%s :Command issued to handle 0x%02x returned with error 0x%04x loginfo 0x%08x, qid %d\n",
3328 __func__, dev_handle, ioc_status, ioc_loginfo,
3329 priv->req_q_idx + 1);
3331 " host_tag %d scsi_state 0x%02x scsi_status 0x%02x, xfer_cnt %d resp_data 0x%x\n",
3332 host_tag, scsi_state, scsi_status, xfer_count, resp_data);
3334 scsi_normalize_sense(sense_buf, sense_count, &sshdr);
3336 "%s :sense_count 0x%x, sense_key 0x%x ASC 0x%x, ASCQ 0x%x\n",
3337 __func__, sense_count, sshdr.sense_key,
3338 sshdr.asc, sshdr.ascq);
3342 if (priv->meta_sg_valid) {
3343 dma_unmap_sg(&mrioc->pdev->dev, scsi_prot_sglist(scmd),
3344 scsi_prot_sg_count(scmd), scmd->sc_data_direction);
3346 mpi3mr_clear_scmd_priv(mrioc, scmd);
3347 scsi_dma_unmap(scmd);
3351 mpi3mr_repost_sense_buf(mrioc,
3352 le64_to_cpu(scsi_reply->sense_data_buffer_address));
3356 * mpi3mr_get_chain_idx - get free chain buffer index
3357 * @mrioc: Adapter instance reference
3359 * Try to get a free chain buffer index from the free pool.
3361 * Return: -1 on failure or the free chain buffer index
3363 static int mpi3mr_get_chain_idx(struct mpi3mr_ioc *mrioc)
3367 unsigned long flags;
3369 spin_lock_irqsave(&mrioc->chain_buf_lock, flags);
3371 cmd_idx = find_first_zero_bit(mrioc->chain_bitmap,
3372 mrioc->chain_buf_count);
3373 if (cmd_idx < mrioc->chain_buf_count) {
3374 set_bit(cmd_idx, mrioc->chain_bitmap);
3378 } while (retry_count--);
3379 spin_unlock_irqrestore(&mrioc->chain_buf_lock, flags);
3384 * mpi3mr_prepare_sg_scmd - build scatter gather list
3385 * @mrioc: Adapter instance reference
3386 * @scmd: SCSI command reference
3387 * @scsiio_req: MPI3 SCSI IO request
3389 * This function maps SCSI command's data and protection SGEs to
3390 * MPI request SGEs. If required additional 4K chain buffer is
3391 * used to send the SGEs.
3393 * Return: 0 on success, -ENOMEM on dma_map_sg failure
3395 static int mpi3mr_prepare_sg_scmd(struct mpi3mr_ioc *mrioc,
3396 struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
3398 dma_addr_t chain_dma;
3399 struct scatterlist *sg_scmd;
3400 void *sg_local, *chain;
3402 int sges_left, chain_idx;
3403 u32 sges_in_segment;
3404 u8 simple_sgl_flags;
3405 u8 simple_sgl_flags_last;
3406 u8 last_chain_sgl_flags;
3407 struct chain_element *chain_req;
3408 struct scmd_priv *priv = NULL;
3409 u32 meta_sg = le32_to_cpu(scsiio_req->flags) &
3410 MPI3_SCSIIO_FLAGS_DMAOPERATION_HOST_PI;
3412 priv = scsi_cmd_priv(scmd);
3414 simple_sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_SIMPLE |
3415 MPI3_SGE_FLAGS_DLAS_SYSTEM;
3416 simple_sgl_flags_last = simple_sgl_flags |
3417 MPI3_SGE_FLAGS_END_OF_LIST;
3418 last_chain_sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_LAST_CHAIN |
3419 MPI3_SGE_FLAGS_DLAS_SYSTEM;
3422 sg_local = &scsiio_req->sgl[MPI3_SCSIIO_METASGL_INDEX];
3424 sg_local = &scsiio_req->sgl;
3426 if (!scsiio_req->data_length && !meta_sg) {
3427 mpi3mr_build_zero_len_sge(sg_local);
3432 sg_scmd = scsi_prot_sglist(scmd);
3433 sges_left = dma_map_sg(&mrioc->pdev->dev,
3434 scsi_prot_sglist(scmd),
3435 scsi_prot_sg_count(scmd),
3436 scmd->sc_data_direction);
3437 priv->meta_sg_valid = 1; /* To unmap meta sg DMA */
3439 sg_scmd = scsi_sglist(scmd);
3440 sges_left = scsi_dma_map(scmd);
3443 if (sges_left < 0) {
3444 sdev_printk(KERN_ERR, scmd->device,
3445 "scsi_dma_map failed: request for %d bytes!\n",
3446 scsi_bufflen(scmd));
3449 if (sges_left > mrioc->max_sgl_entries) {
3450 sdev_printk(KERN_ERR, scmd->device,
3451 "scsi_dma_map returned unsupported sge count %d!\n",
3456 sges_in_segment = (mrioc->facts.op_req_sz -
3457 offsetof(struct mpi3_scsi_io_request, sgl)) / sizeof(struct mpi3_sge_common);
3459 if (scsiio_req->sgl[0].eedp.flags ==
3460 MPI3_SGE_FLAGS_ELEMENT_TYPE_EXTENDED && !meta_sg) {
3461 sg_local += sizeof(struct mpi3_sge_common);
3463 /* Reserve 1st segment (scsiio_req->sgl[0]) for eedp */
3466 if (scsiio_req->msg_flags ==
3467 MPI3_SCSIIO_MSGFLAGS_METASGL_VALID && !meta_sg) {
3469 /* Reserve last segment (scsiio_req->sgl[3]) for meta sg */
3473 sges_in_segment = 1;
3475 if (sges_left <= sges_in_segment)
3476 goto fill_in_last_segment;
3478 /* fill in main message segment when there is a chain following */
3479 while (sges_in_segment > 1) {
3480 mpi3mr_add_sg_single(sg_local, simple_sgl_flags,
3481 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
3482 sg_scmd = sg_next(sg_scmd);
3483 sg_local += sizeof(struct mpi3_sge_common);
3488 chain_idx = mpi3mr_get_chain_idx(mrioc);
3491 chain_req = &mrioc->chain_sgl_list[chain_idx];
3493 priv->meta_chain_idx = chain_idx;
3495 priv->chain_idx = chain_idx;
3497 chain = chain_req->addr;
3498 chain_dma = chain_req->dma_addr;
3499 sges_in_segment = sges_left;
3500 chain_length = sges_in_segment * sizeof(struct mpi3_sge_common);
3502 mpi3mr_add_sg_single(sg_local, last_chain_sgl_flags,
3503 chain_length, chain_dma);
3507 fill_in_last_segment:
3508 while (sges_left > 0) {
3510 mpi3mr_add_sg_single(sg_local,
3511 simple_sgl_flags_last, sg_dma_len(sg_scmd),
3512 sg_dma_address(sg_scmd));
3514 mpi3mr_add_sg_single(sg_local, simple_sgl_flags,
3515 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
3516 sg_scmd = sg_next(sg_scmd);
3517 sg_local += sizeof(struct mpi3_sge_common);
3525 * mpi3mr_build_sg_scmd - build scatter gather list for SCSI IO
3526 * @mrioc: Adapter instance reference
3527 * @scmd: SCSI command reference
3528 * @scsiio_req: MPI3 SCSI IO request
3530 * This function calls mpi3mr_prepare_sg_scmd for constructing
3531 * both data SGEs and protection information SGEs in the MPI
3532 * format from the SCSI Command as appropriate .
3534 * Return: return value of mpi3mr_prepare_sg_scmd.
3536 static int mpi3mr_build_sg_scmd(struct mpi3mr_ioc *mrioc,
3537 struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
3541 ret = mpi3mr_prepare_sg_scmd(mrioc, scmd, scsiio_req);
3545 if (scsiio_req->msg_flags == MPI3_SCSIIO_MSGFLAGS_METASGL_VALID) {
3546 /* There is a valid meta sg */
3547 scsiio_req->flags |=
3548 cpu_to_le32(MPI3_SCSIIO_FLAGS_DMAOPERATION_HOST_PI);
3549 ret = mpi3mr_prepare_sg_scmd(mrioc, scmd, scsiio_req);
3556 * mpi3mr_tm_response_name - get TM response as a string
3557 * @resp_code: TM response code
3559 * Convert known task management response code as a readable
3562 * Return: response code string.
3564 static const char *mpi3mr_tm_response_name(u8 resp_code)
3568 switch (resp_code) {
3569 case MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE:
3570 desc = "task management request completed";
3572 case MPI3_SCSITASKMGMT_RSPCODE_INVALID_FRAME:
3573 desc = "invalid frame";
3575 case MPI3_SCSITASKMGMT_RSPCODE_TM_FUNCTION_NOT_SUPPORTED:
3576 desc = "task management request not supported";
3578 case MPI3_SCSITASKMGMT_RSPCODE_TM_FAILED:
3579 desc = "task management request failed";
3581 case MPI3_SCSITASKMGMT_RSPCODE_TM_SUCCEEDED:
3582 desc = "task management request succeeded";
3584 case MPI3_SCSITASKMGMT_RSPCODE_TM_INVALID_LUN:
3585 desc = "invalid LUN";
3587 case MPI3_SCSITASKMGMT_RSPCODE_TM_OVERLAPPED_TAG:
3588 desc = "overlapped tag attempted";
3590 case MPI3_SCSITASKMGMT_RSPCODE_IO_QUEUED_ON_IOC:
3591 desc = "task queued, however not sent to target";
3593 case MPI3_SCSITASKMGMT_RSPCODE_TM_NVME_DENIED:
3594 desc = "task management request denied by NVMe device";
3604 inline void mpi3mr_poll_pend_io_completions(struct mpi3mr_ioc *mrioc)
3607 int num_of_reply_queues =
3608 mrioc->num_op_reply_q + mrioc->op_reply_q_offset;
3610 for (i = mrioc->op_reply_q_offset; i < num_of_reply_queues; i++)
3611 mpi3mr_process_op_reply_q(mrioc,
3612 mrioc->intr_info[i].op_reply_q);
3616 * mpi3mr_issue_tm - Issue Task Management request
3617 * @mrioc: Adapter instance reference
3618 * @tm_type: Task Management type
3619 * @handle: Device handle
3621 * @htag: Host tag of the TM request
3622 * @timeout: TM timeout value
3623 * @drv_cmd: Internal command tracker
3624 * @resp_code: Response code place holder
3625 * @scmd: SCSI command
3627 * Issues a Task Management Request to the controller for a
3628 * specified target, lun and command and wait for its completion
3629 * and check TM response. Recover the TM if it timed out by
3630 * issuing controller reset.
3632 * Return: 0 on success, non-zero on errors
3634 int mpi3mr_issue_tm(struct mpi3mr_ioc *mrioc, u8 tm_type,
3635 u16 handle, uint lun, u16 htag, ulong timeout,
3636 struct mpi3mr_drv_cmd *drv_cmd,
3637 u8 *resp_code, struct scsi_cmnd *scmd)
3639 struct mpi3_scsi_task_mgmt_request tm_req;
3640 struct mpi3_scsi_task_mgmt_reply *tm_reply = NULL;
3642 struct mpi3mr_tgt_dev *tgtdev = NULL;
3643 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
3644 struct scmd_priv *cmd_priv = NULL;
3645 struct scsi_device *sdev = NULL;
3646 struct mpi3mr_sdev_priv_data *sdev_priv_data = NULL;
3648 ioc_info(mrioc, "%s :Issue TM: TM type (0x%x) for devhandle 0x%04x\n",
3649 __func__, tm_type, handle);
3650 if (mrioc->unrecoverable) {
3652 ioc_err(mrioc, "%s :Issue TM: Unrecoverable controller\n",
3657 memset(&tm_req, 0, sizeof(tm_req));
3658 mutex_lock(&drv_cmd->mutex);
3659 if (drv_cmd->state & MPI3MR_CMD_PENDING) {
3661 ioc_err(mrioc, "%s :Issue TM: Command is in use\n", __func__);
3662 mutex_unlock(&drv_cmd->mutex);
3665 if (mrioc->reset_in_progress) {
3667 ioc_err(mrioc, "%s :Issue TM: Reset in progress\n", __func__);
3668 mutex_unlock(&drv_cmd->mutex);
3672 drv_cmd->state = MPI3MR_CMD_PENDING;
3673 drv_cmd->is_waiting = 1;
3674 drv_cmd->callback = NULL;
3675 tm_req.dev_handle = cpu_to_le16(handle);
3676 tm_req.task_type = tm_type;
3677 tm_req.host_tag = cpu_to_le16(htag);
3679 int_to_scsilun(lun, (struct scsi_lun *)tm_req.lun);
3680 tm_req.function = MPI3_FUNCTION_SCSI_TASK_MGMT;
3682 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
3685 sdev = scmd->device;
3686 sdev_priv_data = sdev->hostdata;
3687 scsi_tgt_priv_data = ((sdev_priv_data) ?
3688 sdev_priv_data->tgt_priv_data : NULL);
3690 if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
3691 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
3692 tgtdev->starget->hostdata;
3695 if (scsi_tgt_priv_data)
3696 atomic_inc(&scsi_tgt_priv_data->block_io);
3698 if (tgtdev && (tgtdev->dev_type == MPI3_DEVICE_DEVFORM_PCIE)) {
3699 if (cmd_priv && tgtdev->dev_spec.pcie_inf.abort_to)
3700 timeout = tgtdev->dev_spec.pcie_inf.abort_to;
3701 else if (!cmd_priv && tgtdev->dev_spec.pcie_inf.reset_to)
3702 timeout = tgtdev->dev_spec.pcie_inf.reset_to;
3705 init_completion(&drv_cmd->done);
3706 retval = mpi3mr_admin_request_post(mrioc, &tm_req, sizeof(tm_req), 1);
3708 ioc_err(mrioc, "%s :Issue TM: Admin Post failed\n", __func__);
3711 wait_for_completion_timeout(&drv_cmd->done, (timeout * HZ));
3713 if (!(drv_cmd->state & MPI3MR_CMD_COMPLETE)) {
3714 drv_cmd->is_waiting = 0;
3716 if (!(drv_cmd->state & MPI3MR_CMD_RESET)) {
3718 "task management request timed out after %ld seconds\n",
3720 if (mrioc->logging_level & MPI3_DEBUG_TM)
3721 dprint_dump_req(&tm_req, sizeof(tm_req)/4);
3722 mpi3mr_soft_reset_handler(mrioc,
3723 MPI3MR_RESET_FROM_TM_TIMEOUT, 1);
3728 if (!(drv_cmd->state & MPI3MR_CMD_REPLY_VALID)) {
3729 dprint_tm(mrioc, "invalid task management reply message\n");
3734 tm_reply = (struct mpi3_scsi_task_mgmt_reply *)drv_cmd->reply;
3736 switch (drv_cmd->ioc_status) {
3737 case MPI3_IOCSTATUS_SUCCESS:
3738 *resp_code = le32_to_cpu(tm_reply->response_data) &
3739 MPI3MR_RI_MASK_RESPCODE;
3741 case MPI3_IOCSTATUS_SCSI_IOC_TERMINATED:
3742 *resp_code = MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE;
3746 "task management request to handle(0x%04x) is failed with ioc_status(0x%04x) log_info(0x%08x)\n",
3747 handle, drv_cmd->ioc_status, drv_cmd->ioc_loginfo);
3752 switch (*resp_code) {
3753 case MPI3_SCSITASKMGMT_RSPCODE_TM_SUCCEEDED:
3754 case MPI3_SCSITASKMGMT_RSPCODE_TM_COMPLETE:
3756 case MPI3_SCSITASKMGMT_RSPCODE_IO_QUEUED_ON_IOC:
3757 if (tm_type != MPI3_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
3766 "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",
3767 tm_type, handle, drv_cmd->ioc_status, drv_cmd->ioc_loginfo,
3768 le32_to_cpu(tm_reply->termination_count),
3769 mpi3mr_tm_response_name(*resp_code), *resp_code);
3772 mpi3mr_ioc_disable_intr(mrioc);
3773 mpi3mr_poll_pend_io_completions(mrioc);
3774 mpi3mr_ioc_enable_intr(mrioc);
3775 mpi3mr_poll_pend_io_completions(mrioc);
3776 mpi3mr_process_admin_reply_q(mrioc);
3779 case MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
3780 if (!scsi_tgt_priv_data)
3782 scsi_tgt_priv_data->pend_count = 0;
3783 blk_mq_tagset_busy_iter(&mrioc->shost->tag_set,
3784 mpi3mr_count_tgt_pending,
3785 (void *)scsi_tgt_priv_data->starget);
3787 case MPI3_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
3788 if (!sdev_priv_data)
3790 sdev_priv_data->pend_count = 0;
3791 blk_mq_tagset_busy_iter(&mrioc->shost->tag_set,
3792 mpi3mr_count_dev_pending, (void *)sdev);
3799 drv_cmd->state = MPI3MR_CMD_NOTUSED;
3800 mutex_unlock(&drv_cmd->mutex);
3801 if (scsi_tgt_priv_data)
3802 atomic_dec_if_positive(&scsi_tgt_priv_data->block_io);
3804 mpi3mr_tgtdev_put(tgtdev);
3810 * mpi3mr_bios_param - BIOS param callback
3811 * @sdev: SCSI device reference
3812 * @bdev: Block device reference
3813 * @capacity: Capacity in logical sectors
3814 * @params: Parameter array
3816 * Just the parameters with heads/secots/cylinders.
3820 static int mpi3mr_bios_param(struct scsi_device *sdev,
3821 struct block_device *bdev, sector_t capacity, int params[])
3831 dummy = heads * sectors;
3832 cylinders = capacity;
3833 sector_div(cylinders, dummy);
3835 if ((ulong)capacity >= 0x200000) {
3838 dummy = heads * sectors;
3839 cylinders = capacity;
3840 sector_div(cylinders, dummy);
3844 params[1] = sectors;
3845 params[2] = cylinders;
3850 * mpi3mr_map_queues - Map queues callback handler
3851 * @shost: SCSI host reference
3853 * Maps default and poll queues.
3855 * Return: return zero.
3857 static void mpi3mr_map_queues(struct Scsi_Host *shost)
3859 struct mpi3mr_ioc *mrioc = shost_priv(shost);
3860 int i, qoff, offset;
3861 struct blk_mq_queue_map *map = NULL;
3863 offset = mrioc->op_reply_q_offset;
3865 for (i = 0, qoff = 0; i < HCTX_MAX_TYPES; i++) {
3866 map = &shost->tag_set.map[i];
3870 if (i == HCTX_TYPE_DEFAULT)
3871 map->nr_queues = mrioc->default_qcount;
3872 else if (i == HCTX_TYPE_POLL)
3873 map->nr_queues = mrioc->active_poll_qcount;
3875 if (!map->nr_queues) {
3876 BUG_ON(i == HCTX_TYPE_DEFAULT);
3881 * The poll queue(s) doesn't have an IRQ (and hence IRQ
3882 * affinity), so use the regular blk-mq cpu mapping
3884 map->queue_offset = qoff;
3885 if (i != HCTX_TYPE_POLL)
3886 blk_mq_pci_map_queues(map, mrioc->pdev, offset);
3888 blk_mq_map_queues(map);
3890 qoff += map->nr_queues;
3891 offset += map->nr_queues;
3896 * mpi3mr_get_fw_pending_ios - Calculate pending I/O count
3897 * @mrioc: Adapter instance reference
3899 * Calculate the pending I/Os for the controller and return.
3901 * Return: Number of pending I/Os
3903 static inline int mpi3mr_get_fw_pending_ios(struct mpi3mr_ioc *mrioc)
3908 for (i = 0; i < mrioc->num_op_reply_q; i++)
3909 pend_ios += atomic_read(&mrioc->op_reply_qinfo[i].pend_ios);
3914 * mpi3mr_print_pending_host_io - print pending I/Os
3915 * @mrioc: Adapter instance reference
3917 * Print number of pending I/Os and each I/O details prior to
3918 * reset for debug purpose.
3922 static void mpi3mr_print_pending_host_io(struct mpi3mr_ioc *mrioc)
3924 struct Scsi_Host *shost = mrioc->shost;
3926 ioc_info(mrioc, "%s :Pending commands prior to reset: %d\n",
3927 __func__, mpi3mr_get_fw_pending_ios(mrioc));
3928 blk_mq_tagset_busy_iter(&shost->tag_set,
3929 mpi3mr_print_scmd, (void *)mrioc);
3933 * mpi3mr_wait_for_host_io - block for I/Os to complete
3934 * @mrioc: Adapter instance reference
3935 * @timeout: time out in seconds
3936 * Waits for pending I/Os for the given adapter to complete or
3937 * to hit the timeout.
3941 void mpi3mr_wait_for_host_io(struct mpi3mr_ioc *mrioc, u32 timeout)
3943 enum mpi3mr_iocstate iocstate;
3946 iocstate = mpi3mr_get_iocstate(mrioc);
3947 if (iocstate != MRIOC_STATE_READY)
3950 if (!mpi3mr_get_fw_pending_ios(mrioc))
3953 "%s :Waiting for %d seconds prior to reset for %d I/O\n",
3954 __func__, timeout, mpi3mr_get_fw_pending_ios(mrioc));
3956 for (i = 0; i < timeout; i++) {
3957 if (!mpi3mr_get_fw_pending_ios(mrioc))
3959 iocstate = mpi3mr_get_iocstate(mrioc);
3960 if (iocstate != MRIOC_STATE_READY)
3965 ioc_info(mrioc, "%s :Pending I/Os after wait is: %d\n", __func__,
3966 mpi3mr_get_fw_pending_ios(mrioc));
3970 * mpi3mr_setup_divert_ws - Setup Divert IO flag for write same
3971 * @mrioc: Adapter instance reference
3972 * @scmd: SCSI command reference
3973 * @scsiio_req: MPI3 SCSI IO request
3974 * @scsiio_flags: Pointer to MPI3 SCSI IO Flags
3975 * @wslen: write same max length
3977 * Gets values of unmap, ndob and number of blocks from write
3978 * same scsi io and based on these values it sets divert IO flag
3979 * and reason for diverting IO to firmware.
3983 static inline void mpi3mr_setup_divert_ws(struct mpi3mr_ioc *mrioc,
3984 struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req,
3985 u32 *scsiio_flags, u16 wslen)
3987 u8 unmap = 0, ndob = 0;
3988 u8 opcode = scmd->cmnd[0];
3990 u16 sa = (scmd->cmnd[8] << 8) | (scmd->cmnd[9]);
3992 if (opcode == WRITE_SAME_16) {
3993 unmap = scmd->cmnd[1] & 0x08;
3994 ndob = scmd->cmnd[1] & 0x01;
3995 num_blocks = get_unaligned_be32(scmd->cmnd + 10);
3996 } else if ((opcode == VARIABLE_LENGTH_CMD) && (sa == WRITE_SAME_32)) {
3997 unmap = scmd->cmnd[10] & 0x08;
3998 ndob = scmd->cmnd[10] & 0x01;
3999 num_blocks = get_unaligned_be32(scmd->cmnd + 28);
4003 if ((unmap) && (ndob) && (num_blocks > wslen)) {
4004 scsiio_req->msg_flags |=
4005 MPI3_SCSIIO_MSGFLAGS_DIVERT_TO_FIRMWARE;
4007 MPI3_SCSIIO_FLAGS_DIVERT_REASON_WRITE_SAME_TOO_LARGE;
4012 * mpi3mr_eh_host_reset - Host reset error handling callback
4013 * @scmd: SCSI command reference
4015 * Issue controller reset
4017 * Return: SUCCESS of successful reset else FAILED
4019 static int mpi3mr_eh_host_reset(struct scsi_cmnd *scmd)
4021 struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
4022 int retval = FAILED, ret;
4024 ret = mpi3mr_soft_reset_handler(mrioc,
4025 MPI3MR_RESET_FROM_EH_HOS, 1);
4031 sdev_printk(KERN_INFO, scmd->device,
4032 "Host reset is %s for scmd(%p)\n",
4033 ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4039 * mpi3mr_eh_bus_reset - Bus reset error handling callback
4040 * @scmd: SCSI command reference
4042 * Checks whether pending I/Os are present for the RAID volume;
4043 * if not there's no need to reset the adapter.
4045 * Return: SUCCESS of successful reset else FAILED
4047 static int mpi3mr_eh_bus_reset(struct scsi_cmnd *scmd)
4049 struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
4050 struct mpi3mr_stgt_priv_data *stgt_priv_data;
4051 struct mpi3mr_sdev_priv_data *sdev_priv_data;
4052 u8 dev_type = MPI3_DEVICE_DEVFORM_VD;
4053 int retval = FAILED;
4055 sdev_priv_data = scmd->device->hostdata;
4056 if (sdev_priv_data && sdev_priv_data->tgt_priv_data) {
4057 stgt_priv_data = sdev_priv_data->tgt_priv_data;
4058 dev_type = stgt_priv_data->dev_type;
4061 if (dev_type == MPI3_DEVICE_DEVFORM_VD) {
4062 mpi3mr_wait_for_host_io(mrioc,
4063 MPI3MR_RAID_ERRREC_RESET_TIMEOUT);
4064 if (!mpi3mr_get_fw_pending_ios(mrioc))
4067 if (retval == FAILED)
4068 mpi3mr_print_pending_host_io(mrioc);
4070 sdev_printk(KERN_INFO, scmd->device,
4071 "Bus reset is %s for scmd(%p)\n",
4072 ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4077 * mpi3mr_eh_target_reset - Target reset error handling callback
4078 * @scmd: SCSI command reference
4080 * Issue Target reset Task Management and verify the scmd is
4081 * terminated successfully and return status accordingly.
4083 * Return: SUCCESS of successful termination of the scmd else
4086 static int mpi3mr_eh_target_reset(struct scsi_cmnd *scmd)
4088 struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
4089 struct mpi3mr_stgt_priv_data *stgt_priv_data;
4090 struct mpi3mr_sdev_priv_data *sdev_priv_data;
4093 int retval = FAILED, ret = 0;
4095 sdev_printk(KERN_INFO, scmd->device,
4096 "Attempting Target Reset! scmd(%p)\n", scmd);
4097 scsi_print_command(scmd);
4099 sdev_priv_data = scmd->device->hostdata;
4100 if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
4101 sdev_printk(KERN_INFO, scmd->device,
4102 "SCSI device is not available\n");
4107 stgt_priv_data = sdev_priv_data->tgt_priv_data;
4108 dev_handle = stgt_priv_data->dev_handle;
4109 if (stgt_priv_data->dev_removed) {
4110 struct scmd_priv *cmd_priv = scsi_cmd_priv(scmd);
4111 sdev_printk(KERN_INFO, scmd->device,
4112 "%s:target(handle = 0x%04x) is removed, target reset is not issued\n",
4113 mrioc->name, dev_handle);
4114 if (!cmd_priv->in_lld_scope || cmd_priv->host_tag == MPI3MR_HOSTTAG_INVALID)
4120 sdev_printk(KERN_INFO, scmd->device,
4121 "Target Reset is issued to handle(0x%04x)\n",
4124 ret = mpi3mr_issue_tm(mrioc,
4125 MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET, dev_handle,
4126 sdev_priv_data->lun_id, MPI3MR_HOSTTAG_BLK_TMS,
4127 MPI3MR_RESETTM_TIMEOUT, &mrioc->host_tm_cmds, &resp_code, scmd);
4132 if (stgt_priv_data->pend_count) {
4133 sdev_printk(KERN_INFO, scmd->device,
4134 "%s: target has %d pending commands, target reset is failed\n",
4135 mrioc->name, stgt_priv_data->pend_count);
4141 sdev_printk(KERN_INFO, scmd->device,
4142 "%s: target reset is %s for scmd(%p)\n", mrioc->name,
4143 ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4149 * mpi3mr_eh_dev_reset- Device reset error handling callback
4150 * @scmd: SCSI command reference
4152 * Issue lun reset Task Management and verify the scmd is
4153 * terminated successfully and return status accordingly.
4155 * Return: SUCCESS of successful termination of the scmd else
4158 static int mpi3mr_eh_dev_reset(struct scsi_cmnd *scmd)
4160 struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
4161 struct mpi3mr_stgt_priv_data *stgt_priv_data;
4162 struct mpi3mr_sdev_priv_data *sdev_priv_data;
4165 int retval = FAILED, ret = 0;
4167 sdev_printk(KERN_INFO, scmd->device,
4168 "Attempting Device(lun) Reset! scmd(%p)\n", scmd);
4169 scsi_print_command(scmd);
4171 sdev_priv_data = scmd->device->hostdata;
4172 if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
4173 sdev_printk(KERN_INFO, scmd->device,
4174 "SCSI device is not available\n");
4179 stgt_priv_data = sdev_priv_data->tgt_priv_data;
4180 dev_handle = stgt_priv_data->dev_handle;
4181 if (stgt_priv_data->dev_removed) {
4182 struct scmd_priv *cmd_priv = scsi_cmd_priv(scmd);
4183 sdev_printk(KERN_INFO, scmd->device,
4184 "%s: device(handle = 0x%04x) is removed, device(LUN) reset is not issued\n",
4185 mrioc->name, dev_handle);
4186 if (!cmd_priv->in_lld_scope || cmd_priv->host_tag == MPI3MR_HOSTTAG_INVALID)
4192 sdev_printk(KERN_INFO, scmd->device,
4193 "Device(lun) Reset is issued to handle(0x%04x)\n", dev_handle);
4195 ret = mpi3mr_issue_tm(mrioc,
4196 MPI3_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, dev_handle,
4197 sdev_priv_data->lun_id, MPI3MR_HOSTTAG_BLK_TMS,
4198 MPI3MR_RESETTM_TIMEOUT, &mrioc->host_tm_cmds, &resp_code, scmd);
4203 if (sdev_priv_data->pend_count) {
4204 sdev_printk(KERN_INFO, scmd->device,
4205 "%s: device has %d pending commands, device(LUN) reset is failed\n",
4206 mrioc->name, sdev_priv_data->pend_count);
4211 sdev_printk(KERN_INFO, scmd->device,
4212 "%s: device(LUN) reset is %s for scmd(%p)\n", mrioc->name,
4213 ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4219 * mpi3mr_scan_start - Scan start callback handler
4220 * @shost: SCSI host reference
4222 * Issue port enable request asynchronously.
4226 static void mpi3mr_scan_start(struct Scsi_Host *shost)
4228 struct mpi3mr_ioc *mrioc = shost_priv(shost);
4230 mrioc->scan_started = 1;
4231 ioc_info(mrioc, "%s :Issuing Port Enable\n", __func__);
4232 if (mpi3mr_issue_port_enable(mrioc, 1)) {
4233 ioc_err(mrioc, "%s :Issuing port enable failed\n", __func__);
4234 mrioc->scan_started = 0;
4235 mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
4240 * mpi3mr_scan_finished - Scan finished callback handler
4241 * @shost: SCSI host reference
4242 * @time: Jiffies from the scan start
4244 * Checks whether the port enable is completed or timedout or
4245 * failed and set the scan status accordingly after taking any
4246 * recovery if required.
4248 * Return: 1 on scan finished or timed out, 0 for in progress
4250 static int mpi3mr_scan_finished(struct Scsi_Host *shost,
4253 struct mpi3mr_ioc *mrioc = shost_priv(shost);
4254 u32 pe_timeout = MPI3MR_PORTENABLE_TIMEOUT;
4255 u32 ioc_status = readl(&mrioc->sysif_regs->ioc_status);
4257 if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) ||
4258 (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)) {
4259 ioc_err(mrioc, "port enable failed due to fault or reset\n");
4260 mpi3mr_print_fault_info(mrioc);
4261 mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
4262 mrioc->scan_started = 0;
4263 mrioc->init_cmds.is_waiting = 0;
4264 mrioc->init_cmds.callback = NULL;
4265 mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
4268 if (time >= (pe_timeout * HZ)) {
4269 ioc_err(mrioc, "port enable failed due to time out\n");
4270 mpi3mr_check_rh_fault_ioc(mrioc,
4271 MPI3MR_RESET_FROM_PE_TIMEOUT);
4272 mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
4273 mrioc->scan_started = 0;
4274 mrioc->init_cmds.is_waiting = 0;
4275 mrioc->init_cmds.callback = NULL;
4276 mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
4279 if (mrioc->scan_started)
4282 if (mrioc->scan_failed) {
4284 "port enable failed with status=0x%04x\n",
4285 mrioc->scan_failed);
4287 ioc_info(mrioc, "port enable is successfully completed\n");
4289 mpi3mr_start_watchdog(mrioc);
4290 mrioc->is_driver_loading = 0;
4291 mrioc->stop_bsgs = 0;
4296 * mpi3mr_slave_destroy - Slave destroy callback handler
4297 * @sdev: SCSI device reference
4299 * Cleanup and free per device(lun) private data.
4303 static void mpi3mr_slave_destroy(struct scsi_device *sdev)
4305 struct Scsi_Host *shost;
4306 struct mpi3mr_ioc *mrioc;
4307 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4308 struct mpi3mr_tgt_dev *tgt_dev = NULL;
4309 unsigned long flags;
4310 struct scsi_target *starget;
4311 struct sas_rphy *rphy = NULL;
4313 if (!sdev->hostdata)
4316 starget = scsi_target(sdev);
4317 shost = dev_to_shost(&starget->dev);
4318 mrioc = shost_priv(shost);
4319 scsi_tgt_priv_data = starget->hostdata;
4321 scsi_tgt_priv_data->num_luns--;
4323 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4324 if (starget->channel == mrioc->scsi_device_channel)
4325 tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4326 else if (mrioc->sas_transport_enabled && !starget->channel) {
4327 rphy = dev_to_rphy(starget->dev.parent);
4328 tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
4329 rphy->identify.sas_address, rphy);
4332 if (tgt_dev && (!scsi_tgt_priv_data->num_luns))
4333 tgt_dev->starget = NULL;
4335 mpi3mr_tgtdev_put(tgt_dev);
4336 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4338 kfree(sdev->hostdata);
4339 sdev->hostdata = NULL;
4343 * mpi3mr_target_destroy - Target destroy callback handler
4344 * @starget: SCSI target reference
4346 * Cleanup and free per target private data.
4350 static void mpi3mr_target_destroy(struct scsi_target *starget)
4352 struct Scsi_Host *shost;
4353 struct mpi3mr_ioc *mrioc;
4354 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4355 struct mpi3mr_tgt_dev *tgt_dev;
4356 unsigned long flags;
4358 if (!starget->hostdata)
4361 shost = dev_to_shost(&starget->dev);
4362 mrioc = shost_priv(shost);
4363 scsi_tgt_priv_data = starget->hostdata;
4365 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4366 tgt_dev = __mpi3mr_get_tgtdev_from_tgtpriv(mrioc, scsi_tgt_priv_data);
4367 if (tgt_dev && (tgt_dev->starget == starget) &&
4368 (tgt_dev->perst_id == starget->id))
4369 tgt_dev->starget = NULL;
4371 scsi_tgt_priv_data->tgt_dev = NULL;
4372 scsi_tgt_priv_data->perst_id = 0;
4373 mpi3mr_tgtdev_put(tgt_dev);
4374 mpi3mr_tgtdev_put(tgt_dev);
4376 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4378 kfree(starget->hostdata);
4379 starget->hostdata = NULL;
4383 * mpi3mr_slave_configure - Slave configure callback handler
4384 * @sdev: SCSI device reference
4386 * Configure queue depth, max hardware sectors and virt boundary
4391 static int mpi3mr_slave_configure(struct scsi_device *sdev)
4393 struct scsi_target *starget;
4394 struct Scsi_Host *shost;
4395 struct mpi3mr_ioc *mrioc;
4396 struct mpi3mr_tgt_dev *tgt_dev = NULL;
4397 unsigned long flags;
4399 struct sas_rphy *rphy = NULL;
4401 starget = scsi_target(sdev);
4402 shost = dev_to_shost(&starget->dev);
4403 mrioc = shost_priv(shost);
4405 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4406 if (starget->channel == mrioc->scsi_device_channel)
4407 tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4408 else if (mrioc->sas_transport_enabled && !starget->channel) {
4409 rphy = dev_to_rphy(starget->dev.parent);
4410 tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
4411 rphy->identify.sas_address, rphy);
4413 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4417 mpi3mr_change_queue_depth(sdev, tgt_dev->q_depth);
4419 sdev->eh_timeout = MPI3MR_EH_SCMD_TIMEOUT;
4420 blk_queue_rq_timeout(sdev->request_queue, MPI3MR_SCMD_TIMEOUT);
4422 switch (tgt_dev->dev_type) {
4423 case MPI3_DEVICE_DEVFORM_PCIE:
4424 /*The block layer hw sector size = 512*/
4425 if ((tgt_dev->dev_spec.pcie_inf.dev_info &
4426 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) ==
4427 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) {
4428 blk_queue_max_hw_sectors(sdev->request_queue,
4429 tgt_dev->dev_spec.pcie_inf.mdts / 512);
4430 if (tgt_dev->dev_spec.pcie_inf.pgsz == 0)
4431 blk_queue_virt_boundary(sdev->request_queue,
4432 ((1 << MPI3MR_DEFAULT_PGSZEXP) - 1));
4434 blk_queue_virt_boundary(sdev->request_queue,
4435 ((1 << tgt_dev->dev_spec.pcie_inf.pgsz) - 1));
4442 mpi3mr_tgtdev_put(tgt_dev);
4448 * mpi3mr_slave_alloc -Slave alloc callback handler
4449 * @sdev: SCSI device reference
4451 * Allocate per device(lun) private data and initialize it.
4453 * Return: 0 on success -ENOMEM on memory allocation failure.
4455 static int mpi3mr_slave_alloc(struct scsi_device *sdev)
4457 struct Scsi_Host *shost;
4458 struct mpi3mr_ioc *mrioc;
4459 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4460 struct mpi3mr_tgt_dev *tgt_dev = NULL;
4461 struct mpi3mr_sdev_priv_data *scsi_dev_priv_data;
4462 unsigned long flags;
4463 struct scsi_target *starget;
4465 struct sas_rphy *rphy = NULL;
4467 starget = scsi_target(sdev);
4468 shost = dev_to_shost(&starget->dev);
4469 mrioc = shost_priv(shost);
4470 scsi_tgt_priv_data = starget->hostdata;
4472 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4474 if (starget->channel == mrioc->scsi_device_channel)
4475 tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4476 else if (mrioc->sas_transport_enabled && !starget->channel) {
4477 rphy = dev_to_rphy(starget->dev.parent);
4478 tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
4479 rphy->identify.sas_address, rphy);
4483 if (tgt_dev->starget == NULL)
4484 tgt_dev->starget = starget;
4485 mpi3mr_tgtdev_put(tgt_dev);
4488 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4492 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4494 scsi_dev_priv_data = kzalloc(sizeof(*scsi_dev_priv_data), GFP_KERNEL);
4495 if (!scsi_dev_priv_data)
4498 scsi_dev_priv_data->lun_id = sdev->lun;
4499 scsi_dev_priv_data->tgt_priv_data = scsi_tgt_priv_data;
4500 sdev->hostdata = scsi_dev_priv_data;
4502 scsi_tgt_priv_data->num_luns++;
4508 * mpi3mr_target_alloc - Target alloc callback handler
4509 * @starget: SCSI target reference
4511 * Allocate per target private data and initialize it.
4513 * Return: 0 on success -ENOMEM on memory allocation failure.
4515 static int mpi3mr_target_alloc(struct scsi_target *starget)
4517 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
4518 struct mpi3mr_ioc *mrioc = shost_priv(shost);
4519 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
4520 struct mpi3mr_tgt_dev *tgt_dev;
4521 unsigned long flags;
4523 struct sas_rphy *rphy = NULL;
4525 scsi_tgt_priv_data = kzalloc(sizeof(*scsi_tgt_priv_data), GFP_KERNEL);
4526 if (!scsi_tgt_priv_data)
4529 starget->hostdata = scsi_tgt_priv_data;
4531 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
4532 if (starget->channel == mrioc->scsi_device_channel) {
4533 tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
4534 if (tgt_dev && !tgt_dev->is_hidden) {
4535 scsi_tgt_priv_data->starget = starget;
4536 scsi_tgt_priv_data->dev_handle = tgt_dev->dev_handle;
4537 scsi_tgt_priv_data->perst_id = tgt_dev->perst_id;
4538 scsi_tgt_priv_data->dev_type = tgt_dev->dev_type;
4539 scsi_tgt_priv_data->tgt_dev = tgt_dev;
4540 tgt_dev->starget = starget;
4541 atomic_set(&scsi_tgt_priv_data->block_io, 0);
4543 if ((tgt_dev->dev_type == MPI3_DEVICE_DEVFORM_PCIE) &&
4544 ((tgt_dev->dev_spec.pcie_inf.dev_info &
4545 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) ==
4546 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE) &&
4547 ((tgt_dev->dev_spec.pcie_inf.dev_info &
4548 MPI3_DEVICE0_PCIE_DEVICE_INFO_PITYPE_MASK) !=
4549 MPI3_DEVICE0_PCIE_DEVICE_INFO_PITYPE_0))
4550 scsi_tgt_priv_data->dev_nvme_dif = 1;
4551 scsi_tgt_priv_data->io_throttle_enabled = tgt_dev->io_throttle_enabled;
4552 scsi_tgt_priv_data->wslen = tgt_dev->wslen;
4553 if (tgt_dev->dev_type == MPI3_DEVICE_DEVFORM_VD)
4554 scsi_tgt_priv_data->throttle_group = tgt_dev->dev_spec.vd_inf.tg;
4557 } else if (mrioc->sas_transport_enabled && !starget->channel) {
4558 rphy = dev_to_rphy(starget->dev.parent);
4559 tgt_dev = __mpi3mr_get_tgtdev_by_addr_and_rphy(mrioc,
4560 rphy->identify.sas_address, rphy);
4561 if (tgt_dev && !tgt_dev->is_hidden && !tgt_dev->non_stl &&
4562 (tgt_dev->dev_type == MPI3_DEVICE_DEVFORM_SAS_SATA)) {
4563 scsi_tgt_priv_data->starget = starget;
4564 scsi_tgt_priv_data->dev_handle = tgt_dev->dev_handle;
4565 scsi_tgt_priv_data->perst_id = tgt_dev->perst_id;
4566 scsi_tgt_priv_data->dev_type = tgt_dev->dev_type;
4567 scsi_tgt_priv_data->tgt_dev = tgt_dev;
4568 scsi_tgt_priv_data->io_throttle_enabled = tgt_dev->io_throttle_enabled;
4569 scsi_tgt_priv_data->wslen = tgt_dev->wslen;
4570 tgt_dev->starget = starget;
4571 atomic_set(&scsi_tgt_priv_data->block_io, 0);
4576 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
4582 * mpi3mr_check_return_unmap - Whether an unmap is allowed
4583 * @mrioc: Adapter instance reference
4584 * @scmd: SCSI Command reference
4586 * The controller hardware cannot handle certain unmap commands
4587 * for NVMe drives, this routine checks those and return true
4588 * and completes the SCSI command with proper status and sense
4591 * Return: TRUE for not allowed unmap, FALSE otherwise.
4593 static bool mpi3mr_check_return_unmap(struct mpi3mr_ioc *mrioc,
4594 struct scsi_cmnd *scmd)
4597 u16 param_len, desc_len, trunc_param_len;
4599 trunc_param_len = param_len = get_unaligned_be16(scmd->cmnd + 7);
4601 if (mrioc->pdev->revision) {
4602 if ((param_len > 24) && ((param_len - 8) & 0xF)) {
4603 trunc_param_len -= (param_len - 8) & 0xF;
4604 dprint_scsi_command(mrioc, scmd, MPI3_DEBUG_SCSI_ERROR);
4605 dprint_scsi_err(mrioc,
4606 "truncating param_len from (%d) to (%d)\n",
4607 param_len, trunc_param_len);
4608 put_unaligned_be16(trunc_param_len, scmd->cmnd + 7);
4609 dprint_scsi_command(mrioc, scmd, MPI3_DEBUG_SCSI_ERROR);
4616 "%s: cdb received with zero parameter length\n",
4618 scsi_print_command(scmd);
4619 scmd->result = DID_OK << 16;
4624 if (param_len < 24) {
4626 "%s: cdb received with invalid param_len: %d\n",
4627 __func__, param_len);
4628 scsi_print_command(scmd);
4629 scmd->result = SAM_STAT_CHECK_CONDITION;
4630 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
4635 if (param_len != scsi_bufflen(scmd)) {
4637 "%s: cdb received with param_len: %d bufflen: %d\n",
4638 __func__, param_len, scsi_bufflen(scmd));
4639 scsi_print_command(scmd);
4640 scmd->result = SAM_STAT_CHECK_CONDITION;
4641 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
4646 buf = kzalloc(scsi_bufflen(scmd), GFP_ATOMIC);
4648 scsi_print_command(scmd);
4649 scmd->result = SAM_STAT_CHECK_CONDITION;
4650 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
4655 scsi_sg_copy_to_buffer(scmd, buf, scsi_bufflen(scmd));
4656 desc_len = get_unaligned_be16(&buf[2]);
4658 if (desc_len < 16) {
4660 "%s: Invalid descriptor length in param list: %d\n",
4661 __func__, desc_len);
4662 scsi_print_command(scmd);
4663 scmd->result = SAM_STAT_CHECK_CONDITION;
4664 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
4671 if (param_len > (desc_len + 8)) {
4672 trunc_param_len = desc_len + 8;
4673 scsi_print_command(scmd);
4674 dprint_scsi_err(mrioc,
4675 "truncating param_len(%d) to desc_len+8(%d)\n",
4676 param_len, trunc_param_len);
4677 put_unaligned_be16(trunc_param_len, scmd->cmnd + 7);
4678 scsi_print_command(scmd);
4686 * mpi3mr_allow_scmd_to_fw - Command is allowed during shutdown
4687 * @scmd: SCSI Command reference
4689 * Checks whether a cdb is allowed during shutdown or not.
4691 * Return: TRUE for allowed commands, FALSE otherwise.
4694 inline bool mpi3mr_allow_scmd_to_fw(struct scsi_cmnd *scmd)
4696 switch (scmd->cmnd[0]) {
4697 case SYNCHRONIZE_CACHE:
4706 * mpi3mr_qcmd - I/O request despatcher
4707 * @shost: SCSI Host reference
4708 * @scmd: SCSI Command reference
4710 * Issues the SCSI Command as an MPI3 request.
4712 * Return: 0 on successful queueing of the request or if the
4713 * request is completed with failure.
4714 * SCSI_MLQUEUE_DEVICE_BUSY when the device is busy.
4715 * SCSI_MLQUEUE_HOST_BUSY when the host queue is full.
4717 static int mpi3mr_qcmd(struct Scsi_Host *shost,
4718 struct scsi_cmnd *scmd)
4720 struct mpi3mr_ioc *mrioc = shost_priv(shost);
4721 struct mpi3mr_stgt_priv_data *stgt_priv_data;
4722 struct mpi3mr_sdev_priv_data *sdev_priv_data;
4723 struct scmd_priv *scmd_priv_data = NULL;
4724 struct mpi3_scsi_io_request *scsiio_req = NULL;
4725 struct op_req_qinfo *op_req_q = NULL;
4729 u32 scsiio_flags = 0, data_len_blks = 0;
4730 struct request *rq = scsi_cmd_to_rq(scmd);
4733 u32 tracked_io_sz = 0;
4734 u32 ioc_pend_data_len = 0, tg_pend_data_len = 0;
4735 struct mpi3mr_throttle_group_info *tg = NULL;
4737 if (mrioc->unrecoverable) {
4738 scmd->result = DID_ERROR << 16;
4743 sdev_priv_data = scmd->device->hostdata;
4744 if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
4745 scmd->result = DID_NO_CONNECT << 16;
4750 if (mrioc->stop_drv_processing &&
4751 !(mpi3mr_allow_scmd_to_fw(scmd))) {
4752 scmd->result = DID_NO_CONNECT << 16;
4757 stgt_priv_data = sdev_priv_data->tgt_priv_data;
4758 dev_handle = stgt_priv_data->dev_handle;
4760 /* Avoid error handling escalation when device is removed or blocked */
4762 if (scmd->device->host->shost_state == SHOST_RECOVERY &&
4763 scmd->cmnd[0] == TEST_UNIT_READY &&
4764 (stgt_priv_data->dev_removed || (dev_handle == MPI3MR_INVALID_DEV_HANDLE))) {
4765 scsi_build_sense(scmd, 0, UNIT_ATTENTION, 0x29, 0x07);
4770 if (mrioc->reset_in_progress) {
4771 retval = SCSI_MLQUEUE_HOST_BUSY;
4775 if (atomic_read(&stgt_priv_data->block_io)) {
4776 if (mrioc->stop_drv_processing) {
4777 scmd->result = DID_NO_CONNECT << 16;
4781 retval = SCSI_MLQUEUE_DEVICE_BUSY;
4785 if (dev_handle == MPI3MR_INVALID_DEV_HANDLE) {
4786 scmd->result = DID_NO_CONNECT << 16;
4790 if (stgt_priv_data->dev_removed) {
4791 scmd->result = DID_NO_CONNECT << 16;
4796 if (stgt_priv_data->dev_type == MPI3_DEVICE_DEVFORM_PCIE)
4798 if ((scmd->cmnd[0] == UNMAP) && is_pcie_dev &&
4799 (mrioc->pdev->device == MPI3_MFGPAGE_DEVID_SAS4116) &&
4800 mpi3mr_check_return_unmap(mrioc, scmd))
4803 host_tag = mpi3mr_host_tag_for_scmd(mrioc, scmd);
4804 if (host_tag == MPI3MR_HOSTTAG_INVALID) {
4805 scmd->result = DID_ERROR << 16;
4810 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
4811 scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_READ;
4812 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
4813 scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_WRITE;
4815 scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_NO_DATA_TRANSFER;
4817 scsiio_flags |= MPI3_SCSIIO_FLAGS_TASKATTRIBUTE_SIMPLEQ;
4819 if (sdev_priv_data->ncq_prio_enable) {
4820 iprio_class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
4821 if (iprio_class == IOPRIO_CLASS_RT)
4822 scsiio_flags |= 1 << MPI3_SCSIIO_FLAGS_CMDPRI_SHIFT;
4825 if (scmd->cmd_len > 16)
4826 scsiio_flags |= MPI3_SCSIIO_FLAGS_CDB_GREATER_THAN_16;
4828 scmd_priv_data = scsi_cmd_priv(scmd);
4829 memset(scmd_priv_data->mpi3mr_scsiio_req, 0, MPI3MR_ADMIN_REQ_FRAME_SZ);
4830 scsiio_req = (struct mpi3_scsi_io_request *)scmd_priv_data->mpi3mr_scsiio_req;
4831 scsiio_req->function = MPI3_FUNCTION_SCSI_IO;
4832 scsiio_req->host_tag = cpu_to_le16(host_tag);
4834 mpi3mr_setup_eedp(mrioc, scmd, scsiio_req);
4836 if (stgt_priv_data->wslen)
4837 mpi3mr_setup_divert_ws(mrioc, scmd, scsiio_req, &scsiio_flags,
4838 stgt_priv_data->wslen);
4840 memcpy(scsiio_req->cdb.cdb32, scmd->cmnd, scmd->cmd_len);
4841 scsiio_req->data_length = cpu_to_le32(scsi_bufflen(scmd));
4842 scsiio_req->dev_handle = cpu_to_le16(dev_handle);
4843 scsiio_req->flags = cpu_to_le32(scsiio_flags);
4844 int_to_scsilun(sdev_priv_data->lun_id,
4845 (struct scsi_lun *)scsiio_req->lun);
4847 if (mpi3mr_build_sg_scmd(mrioc, scmd, scsiio_req)) {
4848 mpi3mr_clear_scmd_priv(mrioc, scmd);
4849 retval = SCSI_MLQUEUE_HOST_BUSY;
4852 op_req_q = &mrioc->req_qinfo[scmd_priv_data->req_q_idx];
4853 data_len_blks = scsi_bufflen(scmd) >> 9;
4854 if ((data_len_blks >= mrioc->io_throttle_data_length) &&
4855 stgt_priv_data->io_throttle_enabled) {
4856 tracked_io_sz = data_len_blks;
4857 tg = stgt_priv_data->throttle_group;
4859 ioc_pend_data_len = atomic_add_return(data_len_blks,
4860 &mrioc->pend_large_data_sz);
4861 tg_pend_data_len = atomic_add_return(data_len_blks,
4862 &tg->pend_large_data_sz);
4863 if (!tg->io_divert && ((ioc_pend_data_len >=
4864 mrioc->io_throttle_high) ||
4865 (tg_pend_data_len >= tg->high))) {
4867 tg->need_qd_reduction = 1;
4868 mpi3mr_set_io_divert_for_all_vd_in_tg(mrioc,
4870 mpi3mr_queue_qd_reduction_event(mrioc, tg);
4873 ioc_pend_data_len = atomic_add_return(data_len_blks,
4874 &mrioc->pend_large_data_sz);
4875 if (ioc_pend_data_len >= mrioc->io_throttle_high)
4876 stgt_priv_data->io_divert = 1;
4880 if (stgt_priv_data->io_divert) {
4881 scsiio_req->msg_flags |=
4882 MPI3_SCSIIO_MSGFLAGS_DIVERT_TO_FIRMWARE;
4883 scsiio_flags |= MPI3_SCSIIO_FLAGS_DIVERT_REASON_IO_THROTTLING;
4885 scsiio_req->flags = cpu_to_le32(scsiio_flags);
4887 if (mpi3mr_op_request_post(mrioc, op_req_q,
4888 scmd_priv_data->mpi3mr_scsiio_req)) {
4889 mpi3mr_clear_scmd_priv(mrioc, scmd);
4890 retval = SCSI_MLQUEUE_HOST_BUSY;
4891 if (tracked_io_sz) {
4892 atomic_sub(tracked_io_sz, &mrioc->pend_large_data_sz);
4894 atomic_sub(tracked_io_sz,
4895 &tg->pend_large_data_sz);
4904 static const struct scsi_host_template mpi3mr_driver_template = {
4905 .module = THIS_MODULE,
4906 .name = "MPI3 Storage Controller",
4907 .proc_name = MPI3MR_DRIVER_NAME,
4908 .queuecommand = mpi3mr_qcmd,
4909 .target_alloc = mpi3mr_target_alloc,
4910 .slave_alloc = mpi3mr_slave_alloc,
4911 .slave_configure = mpi3mr_slave_configure,
4912 .target_destroy = mpi3mr_target_destroy,
4913 .slave_destroy = mpi3mr_slave_destroy,
4914 .scan_finished = mpi3mr_scan_finished,
4915 .scan_start = mpi3mr_scan_start,
4916 .change_queue_depth = mpi3mr_change_queue_depth,
4917 .eh_device_reset_handler = mpi3mr_eh_dev_reset,
4918 .eh_target_reset_handler = mpi3mr_eh_target_reset,
4919 .eh_bus_reset_handler = mpi3mr_eh_bus_reset,
4920 .eh_host_reset_handler = mpi3mr_eh_host_reset,
4921 .bios_param = mpi3mr_bios_param,
4922 .map_queues = mpi3mr_map_queues,
4923 .mq_poll = mpi3mr_blk_mq_poll,
4927 .sg_tablesize = MPI3MR_DEFAULT_SGL_ENTRIES,
4928 /* max xfer supported is 1M (2K in 512 byte sized sectors)
4930 .max_sectors = (MPI3MR_DEFAULT_MAX_IO_SIZE / 512),
4931 .cmd_per_lun = MPI3MR_MAX_CMDS_LUN,
4932 .max_segment_size = 0xffffffff,
4933 .track_queue_depth = 1,
4934 .cmd_size = sizeof(struct scmd_priv),
4935 .shost_groups = mpi3mr_host_groups,
4936 .sdev_groups = mpi3mr_dev_groups,
4940 * mpi3mr_init_drv_cmd - Initialize internal command tracker
4941 * @cmdptr: Internal command tracker
4942 * @host_tag: Host tag used for the specific command
4944 * Initialize the internal command tracker structure with
4945 * specified host tag.
4949 static inline void mpi3mr_init_drv_cmd(struct mpi3mr_drv_cmd *cmdptr,
4952 mutex_init(&cmdptr->mutex);
4953 cmdptr->reply = NULL;
4954 cmdptr->state = MPI3MR_CMD_NOTUSED;
4955 cmdptr->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
4956 cmdptr->host_tag = host_tag;
4960 * osintfc_mrioc_security_status -Check controller secure status
4961 * @pdev: PCI device instance
4963 * Read the Device Serial Number capability from PCI config
4964 * space and decide whether the controller is secure or not.
4966 * Return: 0 on success, non-zero on failure.
4969 osintfc_mrioc_security_status(struct pci_dev *pdev)
4977 base = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DSN);
4980 "%s: PCI_EXT_CAP_ID_DSN is not supported\n", __func__);
4984 pci_read_config_dword(pdev, base + 4, &cap_data);
4986 debug_status = cap_data & MPI3MR_CTLR_SECURE_DBG_STATUS_MASK;
4987 ctlr_status = cap_data & MPI3MR_CTLR_SECURITY_STATUS_MASK;
4989 switch (ctlr_status) {
4990 case MPI3MR_INVALID_DEVICE:
4992 "%s: Non secure ctlr (Invalid) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
4993 __func__, pdev->device, pdev->subsystem_vendor,
4994 pdev->subsystem_device);
4997 case MPI3MR_CONFIG_SECURE_DEVICE:
4999 dev_info(&pdev->dev,
5000 "%s: Config secure ctlr is detected\n",
5003 case MPI3MR_HARD_SECURE_DEVICE:
5005 case MPI3MR_TAMPERED_DEVICE:
5007 "%s: Non secure ctlr (Tampered) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
5008 __func__, pdev->device, pdev->subsystem_vendor,
5009 pdev->subsystem_device);
5017 if (!retval && debug_status) {
5019 "%s: Non secure ctlr (Secure Dbg) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
5020 __func__, pdev->device, pdev->subsystem_vendor,
5021 pdev->subsystem_device);
5029 * mpi3mr_probe - PCI probe callback
5030 * @pdev: PCI device instance
5031 * @id: PCI device ID details
5033 * controller initialization routine. Checks the security status
5034 * of the controller and if it is invalid or tampered return the
5035 * probe without initializing the controller. Otherwise,
5036 * allocate per adapter instance through shost_priv and
5037 * initialize controller specific data structures, initializae
5038 * the controller hardware, add shost to the SCSI subsystem.
5040 * Return: 0 on success, non-zero on failure.
5044 mpi3mr_probe(struct pci_dev *pdev, const struct pci_device_id *id)
5046 struct mpi3mr_ioc *mrioc = NULL;
5047 struct Scsi_Host *shost = NULL;
5050 if (osintfc_mrioc_security_status(pdev)) {
5051 warn_non_secure_ctlr = 1;
5052 return 1; /* For Invalid and Tampered device */
5055 shost = scsi_host_alloc(&mpi3mr_driver_template,
5056 sizeof(struct mpi3mr_ioc));
5062 mrioc = shost_priv(shost);
5063 mrioc->id = mrioc_ids++;
5064 sprintf(mrioc->driver_name, "%s", MPI3MR_DRIVER_NAME);
5065 sprintf(mrioc->name, "%s%d", mrioc->driver_name, mrioc->id);
5066 INIT_LIST_HEAD(&mrioc->list);
5067 spin_lock(&mrioc_list_lock);
5068 list_add_tail(&mrioc->list, &mrioc_list);
5069 spin_unlock(&mrioc_list_lock);
5071 spin_lock_init(&mrioc->admin_req_lock);
5072 spin_lock_init(&mrioc->reply_free_queue_lock);
5073 spin_lock_init(&mrioc->sbq_lock);
5074 spin_lock_init(&mrioc->fwevt_lock);
5075 spin_lock_init(&mrioc->tgtdev_lock);
5076 spin_lock_init(&mrioc->watchdog_lock);
5077 spin_lock_init(&mrioc->chain_buf_lock);
5078 spin_lock_init(&mrioc->sas_node_lock);
5080 INIT_LIST_HEAD(&mrioc->fwevt_list);
5081 INIT_LIST_HEAD(&mrioc->tgtdev_list);
5082 INIT_LIST_HEAD(&mrioc->delayed_rmhs_list);
5083 INIT_LIST_HEAD(&mrioc->delayed_evtack_cmds_list);
5084 INIT_LIST_HEAD(&mrioc->sas_expander_list);
5085 INIT_LIST_HEAD(&mrioc->hba_port_table_list);
5086 INIT_LIST_HEAD(&mrioc->enclosure_list);
5088 mutex_init(&mrioc->reset_mutex);
5089 mpi3mr_init_drv_cmd(&mrioc->init_cmds, MPI3MR_HOSTTAG_INITCMDS);
5090 mpi3mr_init_drv_cmd(&mrioc->host_tm_cmds, MPI3MR_HOSTTAG_BLK_TMS);
5091 mpi3mr_init_drv_cmd(&mrioc->bsg_cmds, MPI3MR_HOSTTAG_BSG_CMDS);
5092 mpi3mr_init_drv_cmd(&mrioc->cfg_cmds, MPI3MR_HOSTTAG_CFG_CMDS);
5093 mpi3mr_init_drv_cmd(&mrioc->transport_cmds,
5094 MPI3MR_HOSTTAG_TRANSPORT_CMDS);
5096 for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++)
5097 mpi3mr_init_drv_cmd(&mrioc->dev_rmhs_cmds[i],
5098 MPI3MR_HOSTTAG_DEVRMCMD_MIN + i);
5100 for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++)
5101 mpi3mr_init_drv_cmd(&mrioc->evtack_cmds[i],
5102 MPI3MR_HOSTTAG_EVTACKCMD_MIN + i);
5105 mrioc->enable_segqueue = true;
5107 init_waitqueue_head(&mrioc->reset_waitq);
5108 mrioc->logging_level = logging_level;
5109 mrioc->shost = shost;
5111 mrioc->stop_bsgs = 1;
5113 mrioc->max_sgl_entries = max_sgl_entries;
5114 if (max_sgl_entries > MPI3MR_MAX_SGL_ENTRIES)
5115 mrioc->max_sgl_entries = MPI3MR_MAX_SGL_ENTRIES;
5116 else if (max_sgl_entries < MPI3MR_DEFAULT_SGL_ENTRIES)
5117 mrioc->max_sgl_entries = MPI3MR_DEFAULT_SGL_ENTRIES;
5119 mrioc->max_sgl_entries /= MPI3MR_DEFAULT_SGL_ENTRIES;
5120 mrioc->max_sgl_entries *= MPI3MR_DEFAULT_SGL_ENTRIES;
5123 /* init shost parameters */
5124 shost->max_cmd_len = MPI3MR_MAX_CDB_LENGTH;
5125 shost->max_lun = -1;
5126 shost->unique_id = mrioc->id;
5128 shost->max_channel = 0;
5129 shost->max_id = 0xFFFFFFFF;
5131 shost->host_tagset = 1;
5134 scsi_host_set_prot(shost, prot_mask);
5136 prot_mask = SHOST_DIF_TYPE1_PROTECTION
5137 | SHOST_DIF_TYPE2_PROTECTION
5138 | SHOST_DIF_TYPE3_PROTECTION;
5139 scsi_host_set_prot(shost, prot_mask);
5143 "%s :host protection capabilities enabled %s%s%s%s%s%s%s\n",
5145 (prot_mask & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "",
5146 (prot_mask & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "",
5147 (prot_mask & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "",
5148 (prot_mask & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "",
5149 (prot_mask & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "",
5150 (prot_mask & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "",
5151 (prot_mask & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : "");
5153 if (prot_guard_mask)
5154 scsi_host_set_guard(shost, (prot_guard_mask & 3));
5156 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
5158 snprintf(mrioc->fwevt_worker_name, sizeof(mrioc->fwevt_worker_name),
5159 "%s%d_fwevt_wrkr", mrioc->driver_name, mrioc->id);
5160 mrioc->fwevt_worker_thread = alloc_ordered_workqueue(
5161 mrioc->fwevt_worker_name, 0);
5162 if (!mrioc->fwevt_worker_thread) {
5163 ioc_err(mrioc, "failure at %s:%d/%s()!\n",
5164 __FILE__, __LINE__, __func__);
5166 goto fwevtthread_failed;
5169 mrioc->is_driver_loading = 1;
5170 mrioc->cpu_count = num_online_cpus();
5171 if (mpi3mr_setup_resources(mrioc)) {
5172 ioc_err(mrioc, "setup resources failed\n");
5174 goto resource_alloc_failed;
5176 if (mpi3mr_init_ioc(mrioc)) {
5177 ioc_err(mrioc, "initializing IOC failed\n");
5179 goto init_ioc_failed;
5182 shost->nr_hw_queues = mrioc->num_op_reply_q;
5183 if (mrioc->active_poll_qcount)
5186 shost->can_queue = mrioc->max_host_ios;
5187 shost->sg_tablesize = mrioc->max_sgl_entries;
5188 shost->max_id = mrioc->facts.max_perids + 1;
5190 retval = scsi_add_host(shost, &pdev->dev);
5192 ioc_err(mrioc, "failure at %s:%d/%s()!\n",
5193 __FILE__, __LINE__, __func__);
5194 goto addhost_failed;
5197 scsi_scan_host(shost);
5198 mpi3mr_bsg_init(mrioc);
5202 mpi3mr_stop_watchdog(mrioc);
5203 mpi3mr_cleanup_ioc(mrioc);
5205 mpi3mr_free_mem(mrioc);
5206 mpi3mr_cleanup_resources(mrioc);
5207 resource_alloc_failed:
5208 destroy_workqueue(mrioc->fwevt_worker_thread);
5210 spin_lock(&mrioc_list_lock);
5211 list_del(&mrioc->list);
5212 spin_unlock(&mrioc_list_lock);
5213 scsi_host_put(shost);
5219 * mpi3mr_remove - PCI remove callback
5220 * @pdev: PCI device instance
5222 * Cleanup the IOC by issuing MUR and shutdown notification.
5223 * Free up all memory and resources associated with the
5224 * controllerand target devices, unregister the shost.
5228 static void mpi3mr_remove(struct pci_dev *pdev)
5230 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5231 struct mpi3mr_ioc *mrioc;
5232 struct workqueue_struct *wq;
5233 unsigned long flags;
5234 struct mpi3mr_tgt_dev *tgtdev, *tgtdev_next;
5235 struct mpi3mr_hba_port *port, *hba_port_next;
5236 struct mpi3mr_sas_node *sas_expander, *sas_expander_next;
5241 mrioc = shost_priv(shost);
5242 while (mrioc->reset_in_progress || mrioc->is_driver_loading)
5245 if (!pci_device_is_present(mrioc->pdev)) {
5246 mrioc->unrecoverable = 1;
5247 mpi3mr_flush_cmds_for_unrecovered_controller(mrioc);
5250 mpi3mr_bsg_exit(mrioc);
5251 mrioc->stop_drv_processing = 1;
5252 mpi3mr_cleanup_fwevt_list(mrioc);
5253 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
5254 wq = mrioc->fwevt_worker_thread;
5255 mrioc->fwevt_worker_thread = NULL;
5256 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
5258 destroy_workqueue(wq);
5260 if (mrioc->sas_transport_enabled)
5261 sas_remove_host(shost);
5263 scsi_remove_host(shost);
5265 list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
5267 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
5268 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev, true);
5269 mpi3mr_tgtdev_put(tgtdev);
5271 mpi3mr_stop_watchdog(mrioc);
5272 mpi3mr_cleanup_ioc(mrioc);
5273 mpi3mr_free_mem(mrioc);
5274 mpi3mr_cleanup_resources(mrioc);
5276 spin_lock_irqsave(&mrioc->sas_node_lock, flags);
5277 list_for_each_entry_safe_reverse(sas_expander, sas_expander_next,
5278 &mrioc->sas_expander_list, list) {
5279 spin_unlock_irqrestore(&mrioc->sas_node_lock, flags);
5280 mpi3mr_expander_node_remove(mrioc, sas_expander);
5281 spin_lock_irqsave(&mrioc->sas_node_lock, flags);
5283 list_for_each_entry_safe(port, hba_port_next, &mrioc->hba_port_table_list, list) {
5285 "removing hba_port entry: %p port: %d from hba_port list\n",
5286 port, port->port_id);
5287 list_del(&port->list);
5290 spin_unlock_irqrestore(&mrioc->sas_node_lock, flags);
5292 if (mrioc->sas_hba.num_phys) {
5293 kfree(mrioc->sas_hba.phy);
5294 mrioc->sas_hba.phy = NULL;
5295 mrioc->sas_hba.num_phys = 0;
5298 spin_lock(&mrioc_list_lock);
5299 list_del(&mrioc->list);
5300 spin_unlock(&mrioc_list_lock);
5302 scsi_host_put(shost);
5306 * mpi3mr_shutdown - PCI shutdown callback
5307 * @pdev: PCI device instance
5309 * Free up all memory and resources associated with the
5314 static void mpi3mr_shutdown(struct pci_dev *pdev)
5316 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5317 struct mpi3mr_ioc *mrioc;
5318 struct workqueue_struct *wq;
5319 unsigned long flags;
5324 mrioc = shost_priv(shost);
5325 while (mrioc->reset_in_progress || mrioc->is_driver_loading)
5328 mrioc->stop_drv_processing = 1;
5329 mpi3mr_cleanup_fwevt_list(mrioc);
5330 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
5331 wq = mrioc->fwevt_worker_thread;
5332 mrioc->fwevt_worker_thread = NULL;
5333 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
5335 destroy_workqueue(wq);
5337 mpi3mr_stop_watchdog(mrioc);
5338 mpi3mr_cleanup_ioc(mrioc);
5339 mpi3mr_cleanup_resources(mrioc);
5343 * mpi3mr_suspend - PCI power management suspend callback
5344 * @dev: Device struct
5346 * Change the power state to the given value and cleanup the IOC
5347 * by issuing MUR and shutdown notification
5351 static int __maybe_unused
5352 mpi3mr_suspend(struct device *dev)
5354 struct pci_dev *pdev = to_pci_dev(dev);
5355 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5356 struct mpi3mr_ioc *mrioc;
5361 mrioc = shost_priv(shost);
5362 while (mrioc->reset_in_progress || mrioc->is_driver_loading)
5364 mrioc->stop_drv_processing = 1;
5365 mpi3mr_cleanup_fwevt_list(mrioc);
5366 scsi_block_requests(shost);
5367 mpi3mr_stop_watchdog(mrioc);
5368 mpi3mr_cleanup_ioc(mrioc);
5370 ioc_info(mrioc, "pdev=0x%p, slot=%s, entering operating state\n",
5371 pdev, pci_name(pdev));
5372 mpi3mr_cleanup_resources(mrioc);
5378 * mpi3mr_resume - PCI power management resume callback
5379 * @dev: Device struct
5381 * Restore the power state to D0 and reinitialize the controller
5382 * and resume I/O operations to the target devices
5384 * Return: 0 on success, non-zero on failure
5386 static int __maybe_unused
5387 mpi3mr_resume(struct device *dev)
5389 struct pci_dev *pdev = to_pci_dev(dev);
5390 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5391 struct mpi3mr_ioc *mrioc;
5392 pci_power_t device_state = pdev->current_state;
5398 mrioc = shost_priv(shost);
5400 ioc_info(mrioc, "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
5401 pdev, pci_name(pdev), device_state);
5403 mrioc->cpu_count = num_online_cpus();
5404 r = mpi3mr_setup_resources(mrioc);
5406 ioc_info(mrioc, "%s: Setup resources failed[%d]\n",
5411 mrioc->stop_drv_processing = 0;
5412 mpi3mr_invalidate_devhandles(mrioc);
5413 mpi3mr_free_enclosure_list(mrioc);
5414 mpi3mr_memset_buffers(mrioc);
5415 r = mpi3mr_reinit_ioc(mrioc, 1);
5417 ioc_err(mrioc, "resuming controller failed[%d]\n", r);
5420 ssleep(MPI3MR_RESET_TOPOLOGY_SETTLE_TIME);
5421 scsi_unblock_requests(shost);
5422 mrioc->device_refresh_on = 0;
5423 mpi3mr_start_watchdog(mrioc);
5428 static const struct pci_device_id mpi3mr_pci_id_table[] = {
5430 PCI_DEVICE_SUB(MPI3_MFGPAGE_VENDORID_BROADCOM,
5431 MPI3_MFGPAGE_DEVID_SAS4116, PCI_ANY_ID, PCI_ANY_ID)
5435 MODULE_DEVICE_TABLE(pci, mpi3mr_pci_id_table);
5437 static SIMPLE_DEV_PM_OPS(mpi3mr_pm_ops, mpi3mr_suspend, mpi3mr_resume);
5439 static struct pci_driver mpi3mr_pci_driver = {
5440 .name = MPI3MR_DRIVER_NAME,
5441 .id_table = mpi3mr_pci_id_table,
5442 .probe = mpi3mr_probe,
5443 .remove = mpi3mr_remove,
5444 .shutdown = mpi3mr_shutdown,
5445 .driver.pm = &mpi3mr_pm_ops,
5448 static ssize_t event_counter_show(struct device_driver *dd, char *buf)
5450 return sprintf(buf, "%llu\n", atomic64_read(&event_counter));
5452 static DRIVER_ATTR_RO(event_counter);
5454 static int __init mpi3mr_init(void)
5458 pr_info("Loading %s version %s\n", MPI3MR_DRIVER_NAME,
5459 MPI3MR_DRIVER_VERSION);
5461 mpi3mr_transport_template =
5462 sas_attach_transport(&mpi3mr_transport_functions);
5463 if (!mpi3mr_transport_template) {
5464 pr_err("%s failed to load due to sas transport attach failure\n",
5465 MPI3MR_DRIVER_NAME);
5469 ret_val = pci_register_driver(&mpi3mr_pci_driver);
5471 pr_err("%s failed to load due to pci register driver failure\n",
5472 MPI3MR_DRIVER_NAME);
5473 goto err_pci_reg_fail;
5476 ret_val = driver_create_file(&mpi3mr_pci_driver.driver,
5477 &driver_attr_event_counter);
5479 goto err_event_counter;
5484 pci_unregister_driver(&mpi3mr_pci_driver);
5487 sas_release_transport(mpi3mr_transport_template);
5491 static void __exit mpi3mr_exit(void)
5493 if (warn_non_secure_ctlr)
5495 "Unloading %s version %s while managing a non secure controller\n",
5496 MPI3MR_DRIVER_NAME, MPI3MR_DRIVER_VERSION);
5498 pr_info("Unloading %s version %s\n", MPI3MR_DRIVER_NAME,
5499 MPI3MR_DRIVER_VERSION);
5501 driver_remove_file(&mpi3mr_pci_driver.driver,
5502 &driver_attr_event_counter);
5503 pci_unregister_driver(&mpi3mr_pci_driver);
5504 sas_release_transport(mpi3mr_transport_template);
5507 module_init(mpi3mr_init);
5508 module_exit(mpi3mr_exit);