1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Broadcom MPI3 Storage Controllers
5 * Copyright (C) 2017-2021 Broadcom Inc.
6 * (mailto: mpi3mr-linuxdrv.pdl@broadcom.com)
12 /* global driver scop variables */
13 LIST_HEAD(mrioc_list);
14 DEFINE_SPINLOCK(mrioc_list_lock);
16 static int warn_non_secure_ctlr;
18 MODULE_AUTHOR(MPI3MR_DRIVER_AUTHOR);
19 MODULE_DESCRIPTION(MPI3MR_DRIVER_DESC);
20 MODULE_LICENSE(MPI3MR_DRIVER_LICENSE);
21 MODULE_VERSION(MPI3MR_DRIVER_VERSION);
23 /* Module parameters*/
25 module_param(prot_mask, int, 0);
26 MODULE_PARM_DESC(prot_mask, "Host protection capabilities mask, def=0x07");
28 static int prot_guard_mask = 3;
29 module_param(prot_guard_mask, int, 0);
30 MODULE_PARM_DESC(prot_guard_mask, " Host protection guard mask, def=3");
31 static int logging_level;
32 module_param(logging_level, int, 0);
33 MODULE_PARM_DESC(logging_level,
34 " bits for enabling additional logging info (default=0)");
36 /* Forward declarations*/
38 * mpi3mr_host_tag_for_scmd - Get host tag for a scmd
39 * @mrioc: Adapter instance reference
40 * @scmd: SCSI command reference
42 * Calculate the host tag based on block tag for a given scmd.
44 * Return: Valid host tag or MPI3MR_HOSTTAG_INVALID.
46 static u16 mpi3mr_host_tag_for_scmd(struct mpi3mr_ioc *mrioc,
47 struct scsi_cmnd *scmd)
49 struct scmd_priv *priv = NULL;
51 u16 host_tag, hw_queue;
53 unique_tag = blk_mq_unique_tag(scsi_cmd_to_rq(scmd));
55 hw_queue = blk_mq_unique_tag_to_hwq(unique_tag);
56 if (hw_queue >= mrioc->num_op_reply_q)
57 return MPI3MR_HOSTTAG_INVALID;
58 host_tag = blk_mq_unique_tag_to_tag(unique_tag);
60 if (WARN_ON(host_tag >= mrioc->max_host_ios))
61 return MPI3MR_HOSTTAG_INVALID;
63 priv = scsi_cmd_priv(scmd);
64 /*host_tag 0 is invalid hence incrementing by 1*/
65 priv->host_tag = host_tag + 1;
67 priv->in_lld_scope = 1;
68 priv->req_q_idx = hw_queue;
69 priv->meta_chain_idx = -1;
71 priv->meta_sg_valid = 0;
72 return priv->host_tag;
76 * mpi3mr_scmd_from_host_tag - Get SCSI command from host tag
77 * @mrioc: Adapter instance reference
79 * @qidx: Operational queue index
81 * Identify the block tag from the host tag and queue index and
82 * retrieve associated scsi command using scsi_host_find_tag().
84 * Return: SCSI command reference or NULL.
86 static struct scsi_cmnd *mpi3mr_scmd_from_host_tag(
87 struct mpi3mr_ioc *mrioc, u16 host_tag, u16 qidx)
89 struct scsi_cmnd *scmd = NULL;
90 struct scmd_priv *priv = NULL;
91 u32 unique_tag = host_tag - 1;
93 if (WARN_ON(host_tag > mrioc->max_host_ios))
96 unique_tag |= (qidx << BLK_MQ_UNIQUE_TAG_BITS);
98 scmd = scsi_host_find_tag(mrioc->shost, unique_tag);
100 priv = scsi_cmd_priv(scmd);
101 if (!priv->in_lld_scope)
109 * mpi3mr_clear_scmd_priv - Cleanup SCSI command private date
110 * @mrioc: Adapter instance reference
111 * @scmd: SCSI command reference
113 * Invalidate the SCSI command private data to mark the command
114 * is not in LLD scope anymore.
118 static void mpi3mr_clear_scmd_priv(struct mpi3mr_ioc *mrioc,
119 struct scsi_cmnd *scmd)
121 struct scmd_priv *priv = NULL;
123 priv = scsi_cmd_priv(scmd);
125 if (WARN_ON(priv->in_lld_scope == 0))
127 priv->host_tag = MPI3MR_HOSTTAG_INVALID;
128 priv->req_q_idx = 0xFFFF;
130 priv->in_lld_scope = 0;
131 priv->meta_sg_valid = 0;
132 if (priv->chain_idx >= 0) {
133 clear_bit(priv->chain_idx, mrioc->chain_bitmap);
134 priv->chain_idx = -1;
136 if (priv->meta_chain_idx >= 0) {
137 clear_bit(priv->meta_chain_idx, mrioc->chain_bitmap);
138 priv->meta_chain_idx = -1;
142 static void mpi3mr_dev_rmhs_send_tm(struct mpi3mr_ioc *mrioc, u16 handle,
143 struct mpi3mr_drv_cmd *cmdparam, u8 iou_rc);
144 static void mpi3mr_fwevt_worker(struct work_struct *work);
147 * mpi3mr_fwevt_free - firmware event memory dealloctor
148 * @r: k reference pointer of the firmware event
150 * Free firmware event memory when no reference.
152 static void mpi3mr_fwevt_free(struct kref *r)
154 kfree(container_of(r, struct mpi3mr_fwevt, ref_count));
158 * mpi3mr_fwevt_get - k reference incrementor
159 * @fwevt: Firmware event reference
161 * Increment firmware event reference count.
163 static void mpi3mr_fwevt_get(struct mpi3mr_fwevt *fwevt)
165 kref_get(&fwevt->ref_count);
169 * mpi3mr_fwevt_put - k reference decrementor
170 * @fwevt: Firmware event reference
172 * decrement firmware event reference count.
174 static void mpi3mr_fwevt_put(struct mpi3mr_fwevt *fwevt)
176 kref_put(&fwevt->ref_count, mpi3mr_fwevt_free);
180 * mpi3mr_alloc_fwevt - Allocate firmware event
181 * @len: length of firmware event data to allocate
183 * Allocate firmware event with required length and initialize
184 * the reference counter.
186 * Return: firmware event reference.
188 static struct mpi3mr_fwevt *mpi3mr_alloc_fwevt(int len)
190 struct mpi3mr_fwevt *fwevt;
192 fwevt = kzalloc(sizeof(*fwevt) + len, GFP_ATOMIC);
196 kref_init(&fwevt->ref_count);
201 * mpi3mr_fwevt_add_to_list - Add firmware event to the list
202 * @mrioc: Adapter instance reference
203 * @fwevt: Firmware event reference
205 * Add the given firmware event to the firmware event list.
209 static void mpi3mr_fwevt_add_to_list(struct mpi3mr_ioc *mrioc,
210 struct mpi3mr_fwevt *fwevt)
214 if (!mrioc->fwevt_worker_thread)
217 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
218 /* get fwevt reference count while adding it to fwevt_list */
219 mpi3mr_fwevt_get(fwevt);
220 INIT_LIST_HEAD(&fwevt->list);
221 list_add_tail(&fwevt->list, &mrioc->fwevt_list);
222 INIT_WORK(&fwevt->work, mpi3mr_fwevt_worker);
223 /* get fwevt reference count while enqueueing it to worker queue */
224 mpi3mr_fwevt_get(fwevt);
225 queue_work(mrioc->fwevt_worker_thread, &fwevt->work);
226 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
230 * mpi3mr_fwevt_del_from_list - Delete firmware event from list
231 * @mrioc: Adapter instance reference
232 * @fwevt: Firmware event reference
234 * Delete the given firmware event from the firmware event list.
238 static void mpi3mr_fwevt_del_from_list(struct mpi3mr_ioc *mrioc,
239 struct mpi3mr_fwevt *fwevt)
243 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
244 if (!list_empty(&fwevt->list)) {
245 list_del_init(&fwevt->list);
247 * Put fwevt reference count after
248 * removing it from fwevt_list
250 mpi3mr_fwevt_put(fwevt);
252 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
256 * mpi3mr_dequeue_fwevt - Dequeue firmware event from the list
257 * @mrioc: Adapter instance reference
259 * Dequeue a firmware event from the firmware event list.
261 * Return: firmware event.
263 static struct mpi3mr_fwevt *mpi3mr_dequeue_fwevt(
264 struct mpi3mr_ioc *mrioc)
267 struct mpi3mr_fwevt *fwevt = NULL;
269 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
270 if (!list_empty(&mrioc->fwevt_list)) {
271 fwevt = list_first_entry(&mrioc->fwevt_list,
272 struct mpi3mr_fwevt, list);
273 list_del_init(&fwevt->list);
275 * Put fwevt reference count after
276 * removing it from fwevt_list
278 mpi3mr_fwevt_put(fwevt);
280 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
286 * mpi3mr_cleanup_fwevt_list - Cleanup firmware event list
287 * @mrioc: Adapter instance reference
289 * Flush all pending firmware events from the firmware event
294 void mpi3mr_cleanup_fwevt_list(struct mpi3mr_ioc *mrioc)
296 struct mpi3mr_fwevt *fwevt = NULL;
298 if ((list_empty(&mrioc->fwevt_list) && !mrioc->current_event) ||
299 !mrioc->fwevt_worker_thread)
302 while ((fwevt = mpi3mr_dequeue_fwevt(mrioc)) ||
303 (fwevt = mrioc->current_event)) {
305 * Wait on the fwevt to complete. If this returns 1, then
306 * the event was never executed, and we need a put for the
307 * reference the work had on the fwevt.
309 * If it did execute, we wait for it to finish, and the put will
310 * happen from mpi3mr_process_fwevt()
312 if (cancel_work_sync(&fwevt->work)) {
314 * Put fwevt reference count after
315 * dequeuing it from worker queue
317 mpi3mr_fwevt_put(fwevt);
319 * Put fwevt reference count to neutralize
320 * kref_init increment
322 mpi3mr_fwevt_put(fwevt);
328 * mpi3mr_invalidate_devhandles -Invalidate device handles
329 * @mrioc: Adapter instance reference
331 * Invalidate the device handles in the target device structures
332 * . Called post reset prior to reinitializing the controller.
336 void mpi3mr_invalidate_devhandles(struct mpi3mr_ioc *mrioc)
338 struct mpi3mr_tgt_dev *tgtdev;
339 struct mpi3mr_stgt_priv_data *tgt_priv;
341 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
342 tgtdev->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
343 if (tgtdev->starget && tgtdev->starget->hostdata) {
344 tgt_priv = tgtdev->starget->hostdata;
345 tgt_priv->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
351 * mpi3mr_print_scmd - print individual SCSI command
353 * @data: Adapter instance reference
354 * @reserved: N/A. Currently not used
356 * Print the SCSI command details if it is in LLD scope.
358 * Return: true always.
360 static bool mpi3mr_print_scmd(struct request *rq,
361 void *data, bool reserved)
363 struct mpi3mr_ioc *mrioc = (struct mpi3mr_ioc *)data;
364 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
365 struct scmd_priv *priv = NULL;
368 priv = scsi_cmd_priv(scmd);
369 if (!priv->in_lld_scope)
372 ioc_info(mrioc, "%s :Host Tag = %d, qid = %d\n",
373 __func__, priv->host_tag, priv->req_q_idx + 1);
374 scsi_print_command(scmd);
382 * mpi3mr_flush_scmd - Flush individual SCSI command
384 * @data: Adapter instance reference
385 * @reserved: N/A. Currently not used
387 * Return the SCSI command to the upper layers if it is in LLD
390 * Return: true always.
393 static bool mpi3mr_flush_scmd(struct request *rq,
394 void *data, bool reserved)
396 struct mpi3mr_ioc *mrioc = (struct mpi3mr_ioc *)data;
397 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
398 struct scmd_priv *priv = NULL;
401 priv = scsi_cmd_priv(scmd);
402 if (!priv->in_lld_scope)
405 if (priv->meta_sg_valid)
406 dma_unmap_sg(&mrioc->pdev->dev, scsi_prot_sglist(scmd),
407 scsi_prot_sg_count(scmd), scmd->sc_data_direction);
408 mpi3mr_clear_scmd_priv(mrioc, scmd);
409 scsi_dma_unmap(scmd);
410 scmd->result = DID_RESET << 16;
411 scsi_print_command(scmd);
412 scmd->scsi_done(scmd);
413 mrioc->flush_io_count++;
421 * mpi3mr_flush_host_io - Flush host I/Os
422 * @mrioc: Adapter instance reference
424 * Flush all of the pending I/Os by calling
425 * blk_mq_tagset_busy_iter() for each possible tag. This is
426 * executed post controller reset
430 void mpi3mr_flush_host_io(struct mpi3mr_ioc *mrioc)
432 struct Scsi_Host *shost = mrioc->shost;
434 mrioc->flush_io_count = 0;
435 ioc_info(mrioc, "%s :Flushing Host I/O cmds post reset\n", __func__);
436 blk_mq_tagset_busy_iter(&shost->tag_set,
437 mpi3mr_flush_scmd, (void *)mrioc);
438 ioc_info(mrioc, "%s :Flushed %d Host I/O cmds\n", __func__,
439 mrioc->flush_io_count);
443 * mpi3mr_alloc_tgtdev - target device allocator
445 * Allocate target device instance and initialize the reference
448 * Return: target device instance.
450 static struct mpi3mr_tgt_dev *mpi3mr_alloc_tgtdev(void)
452 struct mpi3mr_tgt_dev *tgtdev;
454 tgtdev = kzalloc(sizeof(*tgtdev), GFP_ATOMIC);
457 kref_init(&tgtdev->ref_count);
462 * mpi3mr_tgtdev_add_to_list -Add tgtdevice to the list
463 * @mrioc: Adapter instance reference
464 * @tgtdev: Target device
466 * Add the target device to the target device list
470 static void mpi3mr_tgtdev_add_to_list(struct mpi3mr_ioc *mrioc,
471 struct mpi3mr_tgt_dev *tgtdev)
475 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
476 mpi3mr_tgtdev_get(tgtdev);
477 INIT_LIST_HEAD(&tgtdev->list);
478 list_add_tail(&tgtdev->list, &mrioc->tgtdev_list);
479 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
483 * mpi3mr_tgtdev_del_from_list -Delete tgtdevice from the list
484 * @mrioc: Adapter instance reference
485 * @tgtdev: Target device
487 * Remove the target device from the target device list
491 static void mpi3mr_tgtdev_del_from_list(struct mpi3mr_ioc *mrioc,
492 struct mpi3mr_tgt_dev *tgtdev)
496 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
497 if (!list_empty(&tgtdev->list)) {
498 list_del_init(&tgtdev->list);
499 mpi3mr_tgtdev_put(tgtdev);
501 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
505 * __mpi3mr_get_tgtdev_by_handle -Get tgtdev from device handle
506 * @mrioc: Adapter instance reference
507 * @handle: Device handle
509 * Accessor to retrieve target device from the device handle.
512 * Return: Target device reference.
514 static struct mpi3mr_tgt_dev *__mpi3mr_get_tgtdev_by_handle(
515 struct mpi3mr_ioc *mrioc, u16 handle)
517 struct mpi3mr_tgt_dev *tgtdev;
519 assert_spin_locked(&mrioc->tgtdev_lock);
520 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list)
521 if (tgtdev->dev_handle == handle)
526 mpi3mr_tgtdev_get(tgtdev);
531 * mpi3mr_get_tgtdev_by_handle -Get tgtdev from device handle
532 * @mrioc: Adapter instance reference
533 * @handle: Device handle
535 * Accessor to retrieve target device from the device handle.
538 * Return: Target device reference.
540 static struct mpi3mr_tgt_dev *mpi3mr_get_tgtdev_by_handle(
541 struct mpi3mr_ioc *mrioc, u16 handle)
543 struct mpi3mr_tgt_dev *tgtdev;
546 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
547 tgtdev = __mpi3mr_get_tgtdev_by_handle(mrioc, handle);
548 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
553 * __mpi3mr_get_tgtdev_by_perst_id -Get tgtdev from persist ID
554 * @mrioc: Adapter instance reference
555 * @persist_id: Persistent ID
557 * Accessor to retrieve target device from the Persistent ID.
560 * Return: Target device reference.
562 static struct mpi3mr_tgt_dev *__mpi3mr_get_tgtdev_by_perst_id(
563 struct mpi3mr_ioc *mrioc, u16 persist_id)
565 struct mpi3mr_tgt_dev *tgtdev;
567 assert_spin_locked(&mrioc->tgtdev_lock);
568 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list)
569 if (tgtdev->perst_id == persist_id)
574 mpi3mr_tgtdev_get(tgtdev);
579 * mpi3mr_get_tgtdev_by_perst_id -Get tgtdev from persistent ID
580 * @mrioc: Adapter instance reference
581 * @persist_id: Persistent ID
583 * Accessor to retrieve target device from the Persistent ID.
586 * Return: Target device reference.
588 static struct mpi3mr_tgt_dev *mpi3mr_get_tgtdev_by_perst_id(
589 struct mpi3mr_ioc *mrioc, u16 persist_id)
591 struct mpi3mr_tgt_dev *tgtdev;
594 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
595 tgtdev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, persist_id);
596 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
601 * __mpi3mr_get_tgtdev_from_tgtpriv -Get tgtdev from tgt private
602 * @mrioc: Adapter instance reference
603 * @tgt_priv: Target private data
605 * Accessor to return target device from the target private
606 * data. Non Lock version
608 * Return: Target device reference.
610 static struct mpi3mr_tgt_dev *__mpi3mr_get_tgtdev_from_tgtpriv(
611 struct mpi3mr_ioc *mrioc, struct mpi3mr_stgt_priv_data *tgt_priv)
613 struct mpi3mr_tgt_dev *tgtdev;
615 assert_spin_locked(&mrioc->tgtdev_lock);
616 tgtdev = tgt_priv->tgt_dev;
618 mpi3mr_tgtdev_get(tgtdev);
623 * mpi3mr_remove_tgtdev_from_host - Remove dev from upper layers
624 * @mrioc: Adapter instance reference
625 * @tgtdev: Target device structure
627 * Checks whether the device is exposed to upper layers and if it
628 * is then remove the device from upper layers by calling
629 * scsi_remove_target().
631 * Return: 0 on success, non zero on failure.
633 static void mpi3mr_remove_tgtdev_from_host(struct mpi3mr_ioc *mrioc,
634 struct mpi3mr_tgt_dev *tgtdev)
636 struct mpi3mr_stgt_priv_data *tgt_priv;
638 ioc_info(mrioc, "%s :Removing handle(0x%04x), wwid(0x%016llx)\n",
639 __func__, tgtdev->dev_handle, (unsigned long long)tgtdev->wwid);
640 if (tgtdev->starget && tgtdev->starget->hostdata) {
641 tgt_priv = tgtdev->starget->hostdata;
642 tgt_priv->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
645 if (tgtdev->starget) {
646 scsi_remove_target(&tgtdev->starget->dev);
647 tgtdev->host_exposed = 0;
649 ioc_info(mrioc, "%s :Removed handle(0x%04x), wwid(0x%016llx)\n",
650 __func__, tgtdev->dev_handle, (unsigned long long)tgtdev->wwid);
654 * mpi3mr_report_tgtdev_to_host - Expose device to upper layers
655 * @mrioc: Adapter instance reference
656 * @perst_id: Persistent ID of the device
658 * Checks whether the device can be exposed to upper layers and
659 * if it is not then expose the device to upper layers by
660 * calling scsi_scan_target().
662 * Return: 0 on success, non zero on failure.
664 static int mpi3mr_report_tgtdev_to_host(struct mpi3mr_ioc *mrioc,
668 struct mpi3mr_tgt_dev *tgtdev;
670 tgtdev = mpi3mr_get_tgtdev_by_perst_id(mrioc, perst_id);
675 if (tgtdev->is_hidden) {
679 if (!tgtdev->host_exposed && !mrioc->reset_in_progress) {
680 tgtdev->host_exposed = 1;
681 scsi_scan_target(&mrioc->shost->shost_gendev, 0,
683 SCAN_WILD_CARD, SCSI_SCAN_INITIAL);
684 if (!tgtdev->starget)
685 tgtdev->host_exposed = 0;
689 mpi3mr_tgtdev_put(tgtdev);
695 * mpi3mr_change_queue_depth- Change QD callback handler
696 * @sdev: SCSI device reference
697 * @q_depth: Queue depth
699 * Validate and limit QD and call scsi_change_queue_depth.
701 * Return: return value of scsi_change_queue_depth
703 static int mpi3mr_change_queue_depth(struct scsi_device *sdev,
706 struct scsi_target *starget = scsi_target(sdev);
707 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
710 if (!sdev->tagged_supported)
712 if (q_depth > shost->can_queue)
713 q_depth = shost->can_queue;
715 q_depth = MPI3MR_DEFAULT_SDEV_QD;
716 retval = scsi_change_queue_depth(sdev, q_depth);
722 * mpi3mr_update_sdev - Update SCSI device information
723 * @sdev: SCSI device reference
724 * @data: target device reference
726 * This is an iterator function called for each SCSI device in a
727 * target to update the target specific information into each
733 mpi3mr_update_sdev(struct scsi_device *sdev, void *data)
735 struct mpi3mr_tgt_dev *tgtdev;
737 tgtdev = (struct mpi3mr_tgt_dev *)data;
741 mpi3mr_change_queue_depth(sdev, tgtdev->q_depth);
742 switch (tgtdev->dev_type) {
743 case MPI3_DEVICE_DEVFORM_PCIE:
744 /*The block layer hw sector size = 512*/
745 blk_queue_max_hw_sectors(sdev->request_queue,
746 tgtdev->dev_spec.pcie_inf.mdts / 512);
747 blk_queue_virt_boundary(sdev->request_queue,
748 ((1 << tgtdev->dev_spec.pcie_inf.pgsz) - 1));
757 * mpi3mr_rfresh_tgtdevs - Refresh target device exposure
758 * @mrioc: Adapter instance reference
760 * This is executed post controller reset to identify any
761 * missing devices during reset and remove from the upper layers
762 * or expose any newly detected device to the upper layers.
767 void mpi3mr_rfresh_tgtdevs(struct mpi3mr_ioc *mrioc)
769 struct mpi3mr_tgt_dev *tgtdev, *tgtdev_next;
771 list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
773 if ((tgtdev->dev_handle == MPI3MR_INVALID_DEV_HANDLE) &&
774 tgtdev->host_exposed) {
775 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
776 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev);
777 mpi3mr_tgtdev_put(tgtdev);
782 list_for_each_entry(tgtdev, &mrioc->tgtdev_list, list) {
783 if ((tgtdev->dev_handle != MPI3MR_INVALID_DEV_HANDLE) &&
784 !tgtdev->is_hidden && !tgtdev->host_exposed)
785 mpi3mr_report_tgtdev_to_host(mrioc, tgtdev->perst_id);
790 * mpi3mr_update_tgtdev - DevStatusChange evt bottomhalf
791 * @mrioc: Adapter instance reference
792 * @tgtdev: Target device internal structure
793 * @dev_pg0: New device page0
795 * Update the information from the device page0 into the driver
796 * cached target device structure.
800 static void mpi3mr_update_tgtdev(struct mpi3mr_ioc *mrioc,
801 struct mpi3mr_tgt_dev *tgtdev, struct mpi3_device_page0 *dev_pg0)
804 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
807 tgtdev->perst_id = le16_to_cpu(dev_pg0->persistent_id);
808 tgtdev->dev_handle = le16_to_cpu(dev_pg0->dev_handle);
809 tgtdev->dev_type = dev_pg0->device_form;
810 tgtdev->encl_handle = le16_to_cpu(dev_pg0->enclosure_handle);
811 tgtdev->parent_handle = le16_to_cpu(dev_pg0->parent_dev_handle);
812 tgtdev->slot = le16_to_cpu(dev_pg0->slot);
813 tgtdev->q_depth = le16_to_cpu(dev_pg0->queue_depth);
814 tgtdev->wwid = le64_to_cpu(dev_pg0->wwid);
816 flags = le16_to_cpu(dev_pg0->flags);
817 tgtdev->is_hidden = (flags & MPI3_DEVICE0_FLAGS_HIDDEN);
819 if (tgtdev->starget && tgtdev->starget->hostdata) {
820 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
821 tgtdev->starget->hostdata;
822 scsi_tgt_priv_data->perst_id = tgtdev->perst_id;
823 scsi_tgt_priv_data->dev_handle = tgtdev->dev_handle;
824 scsi_tgt_priv_data->dev_type = tgtdev->dev_type;
827 switch (tgtdev->dev_type) {
828 case MPI3_DEVICE_DEVFORM_SAS_SATA:
830 struct mpi3_device0_sas_sata_format *sasinf =
831 &dev_pg0->device_specific.sas_sata_format;
832 u16 dev_info = le16_to_cpu(sasinf->device_info);
834 tgtdev->dev_spec.sas_sata_inf.dev_info = dev_info;
835 tgtdev->dev_spec.sas_sata_inf.sas_address =
836 le64_to_cpu(sasinf->sas_address);
837 if ((dev_info & MPI3_SAS_DEVICE_INFO_DEVICE_TYPE_MASK) !=
838 MPI3_SAS_DEVICE_INFO_DEVICE_TYPE_END_DEVICE)
839 tgtdev->is_hidden = 1;
840 else if (!(dev_info & (MPI3_SAS_DEVICE_INFO_STP_SATA_TARGET |
841 MPI3_SAS_DEVICE_INFO_SSP_TARGET)))
842 tgtdev->is_hidden = 1;
845 case MPI3_DEVICE_DEVFORM_PCIE:
847 struct mpi3_device0_pcie_format *pcieinf =
848 &dev_pg0->device_specific.pcie_format;
849 u16 dev_info = le16_to_cpu(pcieinf->device_info);
851 tgtdev->dev_spec.pcie_inf.capb =
852 le32_to_cpu(pcieinf->capabilities);
853 tgtdev->dev_spec.pcie_inf.mdts = MPI3MR_DEFAULT_MDTS;
855 tgtdev->dev_spec.pcie_inf.pgsz = 12;
856 if (dev_pg0->access_status == MPI3_DEVICE0_ASTATUS_NO_ERRORS) {
857 tgtdev->dev_spec.pcie_inf.mdts =
858 le32_to_cpu(pcieinf->maximum_data_transfer_size);
859 tgtdev->dev_spec.pcie_inf.pgsz = pcieinf->page_size;
860 tgtdev->dev_spec.pcie_inf.reset_to =
861 pcieinf->controller_reset_to;
862 tgtdev->dev_spec.pcie_inf.abort_to =
863 pcieinf->nv_me_abort_to;
865 if (tgtdev->dev_spec.pcie_inf.mdts > (1024 * 1024))
866 tgtdev->dev_spec.pcie_inf.mdts = (1024 * 1024);
867 if ((dev_info & MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_MASK) !=
868 MPI3_DEVICE0_PCIE_DEVICE_INFO_TYPE_NVME_DEVICE)
869 tgtdev->is_hidden = 1;
872 prot_mask = scsi_host_get_prot(mrioc->shost);
873 if (prot_mask & SHOST_DIX_TYPE0_PROTECTION) {
874 scsi_host_set_prot(mrioc->shost, prot_mask & 0x77);
876 "%s : Disabling DIX0 prot capability\n", __func__);
878 "because HBA does not support DIX0 operation on NVME drives\n");
882 case MPI3_DEVICE_DEVFORM_VD:
884 struct mpi3_device0_vd_format *vdinf =
885 &dev_pg0->device_specific.vd_format;
887 tgtdev->dev_spec.vol_inf.state = vdinf->vd_state;
888 if (vdinf->vd_state == MPI3_DEVICE0_VD_STATE_OFFLINE)
889 tgtdev->is_hidden = 1;
898 * mpi3mr_devstatuschg_evt_bh - DevStatusChange evt bottomhalf
899 * @mrioc: Adapter instance reference
900 * @fwevt: Firmware event information.
902 * Process Device status Change event and based on device's new
903 * information, either expose the device to the upper layers, or
904 * remove the device from upper layers.
908 static void mpi3mr_devstatuschg_evt_bh(struct mpi3mr_ioc *mrioc,
909 struct mpi3mr_fwevt *fwevt)
912 u8 uhide = 0, delete = 0, cleanup = 0;
913 struct mpi3mr_tgt_dev *tgtdev = NULL;
914 struct mpi3_event_data_device_status_change *evtdata =
915 (struct mpi3_event_data_device_status_change *)fwevt->event_data;
917 dev_handle = le16_to_cpu(evtdata->dev_handle);
919 "%s :device status change: handle(0x%04x): reason code(0x%x)\n",
920 __func__, dev_handle, evtdata->reason_code);
921 switch (evtdata->reason_code) {
922 case MPI3_EVENT_DEV_STAT_RC_HIDDEN:
925 case MPI3_EVENT_DEV_STAT_RC_NOT_HIDDEN:
928 case MPI3_EVENT_DEV_STAT_RC_VD_NOT_RESPONDING:
933 ioc_info(mrioc, "%s :Unhandled reason code(0x%x)\n", __func__,
934 evtdata->reason_code);
938 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
942 tgtdev->is_hidden = 0;
943 if (!tgtdev->host_exposed)
944 mpi3mr_report_tgtdev_to_host(mrioc, tgtdev->perst_id);
946 if (tgtdev->starget && tgtdev->starget->hostdata) {
948 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
951 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev);
952 mpi3mr_tgtdev_put(tgtdev);
957 mpi3mr_tgtdev_put(tgtdev);
961 * mpi3mr_devinfochg_evt_bh - DeviceInfoChange evt bottomhalf
962 * @mrioc: Adapter instance reference
963 * @dev_pg0: New device page0
965 * Process Device Info Change event and based on device's new
966 * information, either expose the device to the upper layers, or
967 * remove the device from upper layers or update the details of
972 static void mpi3mr_devinfochg_evt_bh(struct mpi3mr_ioc *mrioc,
973 struct mpi3_device_page0 *dev_pg0)
975 struct mpi3mr_tgt_dev *tgtdev = NULL;
976 u16 dev_handle = 0, perst_id = 0;
978 perst_id = le16_to_cpu(dev_pg0->persistent_id);
979 dev_handle = le16_to_cpu(dev_pg0->dev_handle);
981 "%s :Device info change: handle(0x%04x): persist_id(0x%x)\n",
982 __func__, dev_handle, perst_id);
983 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
986 mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0);
987 if (!tgtdev->is_hidden && !tgtdev->host_exposed)
988 mpi3mr_report_tgtdev_to_host(mrioc, perst_id);
989 if (tgtdev->is_hidden && tgtdev->host_exposed)
990 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
991 if (!tgtdev->is_hidden && tgtdev->host_exposed && tgtdev->starget)
992 starget_for_each_device(tgtdev->starget, (void *)tgtdev,
996 mpi3mr_tgtdev_put(tgtdev);
1000 * mpi3mr_sastopochg_evt_debug - SASTopoChange details
1001 * @mrioc: Adapter instance reference
1002 * @event_data: SAS topology change list event data
1004 * Prints information about the SAS topology change event.
1009 mpi3mr_sastopochg_evt_debug(struct mpi3mr_ioc *mrioc,
1010 struct mpi3_event_data_sas_topology_change_list *event_data)
1014 u8 reason_code, phy_number;
1015 char *status_str = NULL;
1016 u8 link_rate, prev_link_rate;
1018 switch (event_data->exp_status) {
1019 case MPI3_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
1020 status_str = "remove";
1022 case MPI3_EVENT_SAS_TOPO_ES_RESPONDING:
1023 status_str = "responding";
1025 case MPI3_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
1026 status_str = "remove delay";
1028 case MPI3_EVENT_SAS_TOPO_ES_NO_EXPANDER:
1029 status_str = "direct attached";
1032 status_str = "unknown status";
1035 ioc_info(mrioc, "%s :sas topology change: (%s)\n",
1036 __func__, status_str);
1038 "%s :\texpander_handle(0x%04x), enclosure_handle(0x%04x) start_phy(%02d), num_entries(%d)\n",
1039 __func__, le16_to_cpu(event_data->expander_dev_handle),
1040 le16_to_cpu(event_data->enclosure_handle),
1041 event_data->start_phy_num, event_data->num_entries);
1042 for (i = 0; i < event_data->num_entries; i++) {
1043 handle = le16_to_cpu(event_data->phy_entry[i].attached_dev_handle);
1046 phy_number = event_data->start_phy_num + i;
1047 reason_code = event_data->phy_entry[i].status &
1048 MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
1049 switch (reason_code) {
1050 case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
1051 status_str = "target remove";
1053 case MPI3_EVENT_SAS_TOPO_PHY_RC_DELAY_NOT_RESPONDING:
1054 status_str = "delay target remove";
1056 case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
1057 status_str = "link status change";
1059 case MPI3_EVENT_SAS_TOPO_PHY_RC_NO_CHANGE:
1060 status_str = "link status no change";
1062 case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
1063 status_str = "target responding";
1066 status_str = "unknown";
1069 link_rate = event_data->phy_entry[i].link_rate >> 4;
1070 prev_link_rate = event_data->phy_entry[i].link_rate & 0xF;
1072 "%s :\tphy(%02d), attached_handle(0x%04x): %s: link rate: new(0x%02x), old(0x%02x)\n",
1073 __func__, phy_number, handle, status_str, link_rate,
1079 * mpi3mr_sastopochg_evt_bh - SASTopologyChange evt bottomhalf
1080 * @mrioc: Adapter instance reference
1081 * @fwevt: Firmware event reference
1083 * Prints information about the SAS topology change event and
1084 * for "not responding" event code, removes the device from the
1089 static void mpi3mr_sastopochg_evt_bh(struct mpi3mr_ioc *mrioc,
1090 struct mpi3mr_fwevt *fwevt)
1092 struct mpi3_event_data_sas_topology_change_list *event_data =
1093 (struct mpi3_event_data_sas_topology_change_list *)fwevt->event_data;
1097 struct mpi3mr_tgt_dev *tgtdev = NULL;
1099 mpi3mr_sastopochg_evt_debug(mrioc, event_data);
1101 for (i = 0; i < event_data->num_entries; i++) {
1102 handle = le16_to_cpu(event_data->phy_entry[i].attached_dev_handle);
1105 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
1109 reason_code = event_data->phy_entry[i].status &
1110 MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
1112 switch (reason_code) {
1113 case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
1114 if (tgtdev->host_exposed)
1115 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1116 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev);
1117 mpi3mr_tgtdev_put(tgtdev);
1123 mpi3mr_tgtdev_put(tgtdev);
1128 * mpi3mr_pcietopochg_evt_debug - PCIeTopoChange details
1129 * @mrioc: Adapter instance reference
1130 * @event_data: PCIe topology change list event data
1132 * Prints information about the PCIe topology change event.
1137 mpi3mr_pcietopochg_evt_debug(struct mpi3mr_ioc *mrioc,
1138 struct mpi3_event_data_pcie_topology_change_list *event_data)
1144 char *status_str = NULL;
1145 u8 link_rate, prev_link_rate;
1147 switch (event_data->switch_status) {
1148 case MPI3_EVENT_PCIE_TOPO_SS_NOT_RESPONDING:
1149 status_str = "remove";
1151 case MPI3_EVENT_PCIE_TOPO_SS_RESPONDING:
1152 status_str = "responding";
1154 case MPI3_EVENT_PCIE_TOPO_SS_DELAY_NOT_RESPONDING:
1155 status_str = "remove delay";
1157 case MPI3_EVENT_PCIE_TOPO_SS_NO_PCIE_SWITCH:
1158 status_str = "direct attached";
1161 status_str = "unknown status";
1164 ioc_info(mrioc, "%s :pcie topology change: (%s)\n",
1165 __func__, status_str);
1167 "%s :\tswitch_handle(0x%04x), enclosure_handle(0x%04x) start_port(%02d), num_entries(%d)\n",
1168 __func__, le16_to_cpu(event_data->switch_dev_handle),
1169 le16_to_cpu(event_data->enclosure_handle),
1170 event_data->start_port_num, event_data->num_entries);
1171 for (i = 0; i < event_data->num_entries; i++) {
1173 le16_to_cpu(event_data->port_entry[i].attached_dev_handle);
1176 port_number = event_data->start_port_num + i;
1177 reason_code = event_data->port_entry[i].port_status;
1178 switch (reason_code) {
1179 case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
1180 status_str = "target remove";
1182 case MPI3_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
1183 status_str = "delay target remove";
1185 case MPI3_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
1186 status_str = "link status change";
1188 case MPI3_EVENT_PCIE_TOPO_PS_NO_CHANGE:
1189 status_str = "link status no change";
1191 case MPI3_EVENT_PCIE_TOPO_PS_RESPONDING:
1192 status_str = "target responding";
1195 status_str = "unknown";
1198 link_rate = event_data->port_entry[i].current_port_info &
1199 MPI3_EVENT_PCIE_TOPO_PI_RATE_MASK;
1200 prev_link_rate = event_data->port_entry[i].previous_port_info &
1201 MPI3_EVENT_PCIE_TOPO_PI_RATE_MASK;
1203 "%s :\tport(%02d), attached_handle(0x%04x): %s: link rate: new(0x%02x), old(0x%02x)\n",
1204 __func__, port_number, handle, status_str, link_rate,
1210 * mpi3mr_pcietopochg_evt_bh - PCIeTopologyChange evt bottomhalf
1211 * @mrioc: Adapter instance reference
1212 * @fwevt: Firmware event reference
1214 * Prints information about the PCIe topology change event and
1215 * for "not responding" event code, removes the device from the
1220 static void mpi3mr_pcietopochg_evt_bh(struct mpi3mr_ioc *mrioc,
1221 struct mpi3mr_fwevt *fwevt)
1223 struct mpi3_event_data_pcie_topology_change_list *event_data =
1224 (struct mpi3_event_data_pcie_topology_change_list *)fwevt->event_data;
1228 struct mpi3mr_tgt_dev *tgtdev = NULL;
1230 mpi3mr_pcietopochg_evt_debug(mrioc, event_data);
1232 for (i = 0; i < event_data->num_entries; i++) {
1234 le16_to_cpu(event_data->port_entry[i].attached_dev_handle);
1237 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
1241 reason_code = event_data->port_entry[i].port_status;
1243 switch (reason_code) {
1244 case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
1245 if (tgtdev->host_exposed)
1246 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
1247 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev);
1248 mpi3mr_tgtdev_put(tgtdev);
1254 mpi3mr_tgtdev_put(tgtdev);
1259 * mpi3mr_fwevt_bh - Firmware event bottomhalf handler
1260 * @mrioc: Adapter instance reference
1261 * @fwevt: Firmware event reference
1263 * Identifies the firmware event and calls corresponding bottomg
1264 * half handler and sends event acknowledgment if required.
1268 static void mpi3mr_fwevt_bh(struct mpi3mr_ioc *mrioc,
1269 struct mpi3mr_fwevt *fwevt)
1271 mrioc->current_event = fwevt;
1272 mpi3mr_fwevt_del_from_list(mrioc, fwevt);
1274 if (mrioc->stop_drv_processing)
1277 if (!fwevt->process_evt)
1280 switch (fwevt->event_id) {
1281 case MPI3_EVENT_DEVICE_ADDED:
1283 struct mpi3_device_page0 *dev_pg0 =
1284 (struct mpi3_device_page0 *)fwevt->event_data;
1285 mpi3mr_report_tgtdev_to_host(mrioc,
1286 le16_to_cpu(dev_pg0->persistent_id));
1289 case MPI3_EVENT_DEVICE_INFO_CHANGED:
1291 mpi3mr_devinfochg_evt_bh(mrioc,
1292 (struct mpi3_device_page0 *)fwevt->event_data);
1295 case MPI3_EVENT_DEVICE_STATUS_CHANGE:
1297 mpi3mr_devstatuschg_evt_bh(mrioc, fwevt);
1300 case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
1302 mpi3mr_sastopochg_evt_bh(mrioc, fwevt);
1305 case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
1307 mpi3mr_pcietopochg_evt_bh(mrioc, fwevt);
1315 if (fwevt->send_ack)
1316 mpi3mr_send_event_ack(mrioc, fwevt->event_id,
1319 /* Put fwevt reference count to neutralize kref_init increment */
1320 mpi3mr_fwevt_put(fwevt);
1321 mrioc->current_event = NULL;
1325 * mpi3mr_fwevt_worker - Firmware event worker
1326 * @work: Work struct containing firmware event
1328 * Extracts the firmware event and calls mpi3mr_fwevt_bh.
1332 static void mpi3mr_fwevt_worker(struct work_struct *work)
1334 struct mpi3mr_fwevt *fwevt = container_of(work, struct mpi3mr_fwevt,
1336 mpi3mr_fwevt_bh(fwevt->mrioc, fwevt);
1338 * Put fwevt reference count after
1339 * dequeuing it from worker queue
1341 mpi3mr_fwevt_put(fwevt);
1345 * mpi3mr_create_tgtdev - Create and add a target device
1346 * @mrioc: Adapter instance reference
1347 * @dev_pg0: Device Page 0 data
1349 * If the device specified by the device page 0 data is not
1350 * present in the driver's internal list, allocate the memory
1351 * for the device, populate the data and add to the list, else
1352 * update the device data. The key is persistent ID.
1354 * Return: 0 on success, -ENOMEM on memory allocation failure
1356 static int mpi3mr_create_tgtdev(struct mpi3mr_ioc *mrioc,
1357 struct mpi3_device_page0 *dev_pg0)
1360 struct mpi3mr_tgt_dev *tgtdev = NULL;
1363 perst_id = le16_to_cpu(dev_pg0->persistent_id);
1364 tgtdev = mpi3mr_get_tgtdev_by_perst_id(mrioc, perst_id);
1366 mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0);
1367 mpi3mr_tgtdev_put(tgtdev);
1369 tgtdev = mpi3mr_alloc_tgtdev();
1372 mpi3mr_update_tgtdev(mrioc, tgtdev, dev_pg0);
1373 mpi3mr_tgtdev_add_to_list(mrioc, tgtdev);
1380 * mpi3mr_flush_delayed_rmhs_list - Flush pending commands
1381 * @mrioc: Adapter instance reference
1383 * Flush pending commands in the delayed removal handshake list
1384 * due to a controller reset or driver removal as a cleanup.
1388 void mpi3mr_flush_delayed_rmhs_list(struct mpi3mr_ioc *mrioc)
1390 struct delayed_dev_rmhs_node *_rmhs_node;
1392 while (!list_empty(&mrioc->delayed_rmhs_list)) {
1393 _rmhs_node = list_entry(mrioc->delayed_rmhs_list.next,
1394 struct delayed_dev_rmhs_node, list);
1395 list_del(&_rmhs_node->list);
1401 * mpi3mr_dev_rmhs_complete_iou - Device removal IOUC completion
1402 * @mrioc: Adapter instance reference
1403 * @drv_cmd: Internal command tracker
1405 * Issues a target reset TM to the firmware from the device
1406 * removal TM pend list or retry the removal handshake sequence
1407 * based on the IOU control request IOC status.
1411 static void mpi3mr_dev_rmhs_complete_iou(struct mpi3mr_ioc *mrioc,
1412 struct mpi3mr_drv_cmd *drv_cmd)
1414 u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
1415 struct delayed_dev_rmhs_node *delayed_dev_rmhs = NULL;
1418 "%s :dev_rmhs_iouctrl_complete:handle(0x%04x), ioc_status(0x%04x), loginfo(0x%08x)\n",
1419 __func__, drv_cmd->dev_handle, drv_cmd->ioc_status,
1420 drv_cmd->ioc_loginfo);
1421 if (drv_cmd->ioc_status != MPI3_IOCSTATUS_SUCCESS) {
1422 if (drv_cmd->retry_count < MPI3MR_DEV_RMHS_RETRY_COUNT) {
1423 drv_cmd->retry_count++;
1425 "%s :dev_rmhs_iouctrl_complete: handle(0x%04x)retrying handshake retry=%d\n",
1426 __func__, drv_cmd->dev_handle,
1427 drv_cmd->retry_count);
1428 mpi3mr_dev_rmhs_send_tm(mrioc, drv_cmd->dev_handle,
1429 drv_cmd, drv_cmd->iou_rc);
1433 "%s :dev removal handshake failed after all retries: handle(0x%04x)\n",
1434 __func__, drv_cmd->dev_handle);
1437 "%s :dev removal handshake completed successfully: handle(0x%04x)\n",
1438 __func__, drv_cmd->dev_handle);
1439 clear_bit(drv_cmd->dev_handle, mrioc->removepend_bitmap);
1442 if (!list_empty(&mrioc->delayed_rmhs_list)) {
1443 delayed_dev_rmhs = list_entry(mrioc->delayed_rmhs_list.next,
1444 struct delayed_dev_rmhs_node, list);
1445 drv_cmd->dev_handle = delayed_dev_rmhs->handle;
1446 drv_cmd->retry_count = 0;
1447 drv_cmd->iou_rc = delayed_dev_rmhs->iou_rc;
1449 "%s :dev_rmhs_iouctrl_complete: processing delayed TM: handle(0x%04x)\n",
1450 __func__, drv_cmd->dev_handle);
1451 mpi3mr_dev_rmhs_send_tm(mrioc, drv_cmd->dev_handle, drv_cmd,
1453 list_del(&delayed_dev_rmhs->list);
1454 kfree(delayed_dev_rmhs);
1457 drv_cmd->state = MPI3MR_CMD_NOTUSED;
1458 drv_cmd->callback = NULL;
1459 drv_cmd->retry_count = 0;
1460 drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
1461 clear_bit(cmd_idx, mrioc->devrem_bitmap);
1465 * mpi3mr_dev_rmhs_complete_tm - Device removal TM completion
1466 * @mrioc: Adapter instance reference
1467 * @drv_cmd: Internal command tracker
1469 * Issues a target reset TM to the firmware from the device
1470 * removal TM pend list or issue IO unit control request as
1471 * part of device removal or hidden acknowledgment handshake.
1475 static void mpi3mr_dev_rmhs_complete_tm(struct mpi3mr_ioc *mrioc,
1476 struct mpi3mr_drv_cmd *drv_cmd)
1478 struct mpi3_iounit_control_request iou_ctrl;
1479 u16 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
1480 struct mpi3_scsi_task_mgmt_reply *tm_reply = NULL;
1483 if (drv_cmd->state & MPI3MR_CMD_REPLY_VALID)
1484 tm_reply = (struct mpi3_scsi_task_mgmt_reply *)drv_cmd->reply;
1488 "dev_rmhs_tr_complete:handle(0x%04x), ioc_status(0x%04x), loginfo(0x%08x), term_count(%d)\n",
1489 mrioc->name, drv_cmd->dev_handle, drv_cmd->ioc_status,
1490 drv_cmd->ioc_loginfo,
1491 le32_to_cpu(tm_reply->termination_count));
1493 pr_info(IOCNAME "Issuing IOU CTL: handle(0x%04x) dev_rmhs idx(%d)\n",
1494 mrioc->name, drv_cmd->dev_handle, cmd_idx);
1496 memset(&iou_ctrl, 0, sizeof(iou_ctrl));
1498 drv_cmd->state = MPI3MR_CMD_PENDING;
1499 drv_cmd->is_waiting = 0;
1500 drv_cmd->callback = mpi3mr_dev_rmhs_complete_iou;
1501 iou_ctrl.operation = drv_cmd->iou_rc;
1502 iou_ctrl.param16[0] = cpu_to_le16(drv_cmd->dev_handle);
1503 iou_ctrl.host_tag = cpu_to_le16(drv_cmd->host_tag);
1504 iou_ctrl.function = MPI3_FUNCTION_IO_UNIT_CONTROL;
1506 retval = mpi3mr_admin_request_post(mrioc, &iou_ctrl, sizeof(iou_ctrl),
1509 pr_err(IOCNAME "Issue DevRmHsTMIOUCTL: Admin post failed\n",
1516 drv_cmd->state = MPI3MR_CMD_NOTUSED;
1517 drv_cmd->callback = NULL;
1518 drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
1519 drv_cmd->retry_count = 0;
1520 clear_bit(cmd_idx, mrioc->devrem_bitmap);
1524 * mpi3mr_dev_rmhs_send_tm - Issue TM for device removal
1525 * @mrioc: Adapter instance reference
1526 * @handle: Device handle
1527 * @cmdparam: Internal command tracker
1528 * @iou_rc: IO unit reason code
1530 * Issues a target reset TM to the firmware or add it to a pend
1531 * list as part of device removal or hidden acknowledgment
1536 static void mpi3mr_dev_rmhs_send_tm(struct mpi3mr_ioc *mrioc, u16 handle,
1537 struct mpi3mr_drv_cmd *cmdparam, u8 iou_rc)
1539 struct mpi3_scsi_task_mgmt_request tm_req;
1541 u16 cmd_idx = MPI3MR_NUM_DEVRMCMD;
1543 struct mpi3mr_drv_cmd *drv_cmd = cmdparam;
1544 struct delayed_dev_rmhs_node *delayed_dev_rmhs = NULL;
1549 cmd_idx = find_first_zero_bit(mrioc->devrem_bitmap,
1550 MPI3MR_NUM_DEVRMCMD);
1551 if (cmd_idx < MPI3MR_NUM_DEVRMCMD) {
1552 if (!test_and_set_bit(cmd_idx, mrioc->devrem_bitmap))
1554 cmd_idx = MPI3MR_NUM_DEVRMCMD;
1556 } while (retrycount--);
1558 if (cmd_idx >= MPI3MR_NUM_DEVRMCMD) {
1559 delayed_dev_rmhs = kzalloc(sizeof(*delayed_dev_rmhs),
1561 if (!delayed_dev_rmhs)
1563 INIT_LIST_HEAD(&delayed_dev_rmhs->list);
1564 delayed_dev_rmhs->handle = handle;
1565 delayed_dev_rmhs->iou_rc = iou_rc;
1566 list_add_tail(&delayed_dev_rmhs->list,
1567 &mrioc->delayed_rmhs_list);
1568 ioc_info(mrioc, "%s :DevRmHs: tr:handle(0x%04x) is postponed\n",
1572 drv_cmd = &mrioc->dev_rmhs_cmds[cmd_idx];
1575 cmd_idx = drv_cmd->host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
1577 "%s :Issuing TR TM: for devhandle 0x%04x with dev_rmhs %d\n",
1578 __func__, handle, cmd_idx);
1580 memset(&tm_req, 0, sizeof(tm_req));
1581 if (drv_cmd->state & MPI3MR_CMD_PENDING) {
1582 ioc_err(mrioc, "%s :Issue TM: Command is in use\n", __func__);
1585 drv_cmd->state = MPI3MR_CMD_PENDING;
1586 drv_cmd->is_waiting = 0;
1587 drv_cmd->callback = mpi3mr_dev_rmhs_complete_tm;
1588 drv_cmd->dev_handle = handle;
1589 drv_cmd->iou_rc = iou_rc;
1590 tm_req.dev_handle = cpu_to_le16(handle);
1591 tm_req.task_type = MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
1592 tm_req.host_tag = cpu_to_le16(drv_cmd->host_tag);
1593 tm_req.task_host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INVALID);
1594 tm_req.function = MPI3_FUNCTION_SCSI_TASK_MGMT;
1596 set_bit(handle, mrioc->removepend_bitmap);
1597 retval = mpi3mr_admin_request_post(mrioc, &tm_req, sizeof(tm_req), 1);
1599 ioc_err(mrioc, "%s :Issue DevRmHsTM: Admin Post failed\n",
1606 drv_cmd->state = MPI3MR_CMD_NOTUSED;
1607 drv_cmd->callback = NULL;
1608 drv_cmd->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
1609 drv_cmd->retry_count = 0;
1610 clear_bit(cmd_idx, mrioc->devrem_bitmap);
1614 * mpi3mr_pcietopochg_evt_th - PCIETopologyChange evt tophalf
1615 * @mrioc: Adapter instance reference
1616 * @event_reply: event data
1618 * Checks for the reason code and based on that either block I/O
1619 * to device, or unblock I/O to the device, or start the device
1620 * removal handshake with reason as remove with the firmware for
1625 static void mpi3mr_pcietopochg_evt_th(struct mpi3mr_ioc *mrioc,
1626 struct mpi3_event_notification_reply *event_reply)
1628 struct mpi3_event_data_pcie_topology_change_list *topo_evt =
1629 (struct mpi3_event_data_pcie_topology_change_list *)event_reply->event_data;
1633 struct mpi3mr_tgt_dev *tgtdev = NULL;
1634 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
1636 for (i = 0; i < topo_evt->num_entries; i++) {
1637 handle = le16_to_cpu(topo_evt->port_entry[i].attached_dev_handle);
1640 reason_code = topo_evt->port_entry[i].port_status;
1641 scsi_tgt_priv_data = NULL;
1642 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
1643 if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
1644 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
1645 tgtdev->starget->hostdata;
1646 switch (reason_code) {
1647 case MPI3_EVENT_PCIE_TOPO_PS_NOT_RESPONDING:
1648 if (scsi_tgt_priv_data) {
1649 scsi_tgt_priv_data->dev_removed = 1;
1650 scsi_tgt_priv_data->dev_removedelay = 0;
1651 atomic_set(&scsi_tgt_priv_data->block_io, 0);
1653 mpi3mr_dev_rmhs_send_tm(mrioc, handle, NULL,
1654 MPI3_CTRL_OP_REMOVE_DEVICE);
1656 case MPI3_EVENT_PCIE_TOPO_PS_DELAY_NOT_RESPONDING:
1657 if (scsi_tgt_priv_data) {
1658 scsi_tgt_priv_data->dev_removedelay = 1;
1659 atomic_inc(&scsi_tgt_priv_data->block_io);
1662 case MPI3_EVENT_PCIE_TOPO_PS_RESPONDING:
1663 if (scsi_tgt_priv_data &&
1664 scsi_tgt_priv_data->dev_removedelay) {
1665 scsi_tgt_priv_data->dev_removedelay = 0;
1666 atomic_dec_if_positive
1667 (&scsi_tgt_priv_data->block_io);
1670 case MPI3_EVENT_PCIE_TOPO_PS_PORT_CHANGED:
1675 mpi3mr_tgtdev_put(tgtdev);
1680 * mpi3mr_sastopochg_evt_th - SASTopologyChange evt tophalf
1681 * @mrioc: Adapter instance reference
1682 * @event_reply: event data
1684 * Checks for the reason code and based on that either block I/O
1685 * to device, or unblock I/O to the device, or start the device
1686 * removal handshake with reason as remove with the firmware for
1691 static void mpi3mr_sastopochg_evt_th(struct mpi3mr_ioc *mrioc,
1692 struct mpi3_event_notification_reply *event_reply)
1694 struct mpi3_event_data_sas_topology_change_list *topo_evt =
1695 (struct mpi3_event_data_sas_topology_change_list *)event_reply->event_data;
1699 struct mpi3mr_tgt_dev *tgtdev = NULL;
1700 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
1702 for (i = 0; i < topo_evt->num_entries; i++) {
1703 handle = le16_to_cpu(topo_evt->phy_entry[i].attached_dev_handle);
1706 reason_code = topo_evt->phy_entry[i].status &
1707 MPI3_EVENT_SAS_TOPO_PHY_RC_MASK;
1708 scsi_tgt_priv_data = NULL;
1709 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
1710 if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata)
1711 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
1712 tgtdev->starget->hostdata;
1713 switch (reason_code) {
1714 case MPI3_EVENT_SAS_TOPO_PHY_RC_TARG_NOT_RESPONDING:
1715 if (scsi_tgt_priv_data) {
1716 scsi_tgt_priv_data->dev_removed = 1;
1717 scsi_tgt_priv_data->dev_removedelay = 0;
1718 atomic_set(&scsi_tgt_priv_data->block_io, 0);
1720 mpi3mr_dev_rmhs_send_tm(mrioc, handle, NULL,
1721 MPI3_CTRL_OP_REMOVE_DEVICE);
1723 case MPI3_EVENT_SAS_TOPO_PHY_RC_DELAY_NOT_RESPONDING:
1724 if (scsi_tgt_priv_data) {
1725 scsi_tgt_priv_data->dev_removedelay = 1;
1726 atomic_inc(&scsi_tgt_priv_data->block_io);
1729 case MPI3_EVENT_SAS_TOPO_PHY_RC_RESPONDING:
1730 if (scsi_tgt_priv_data &&
1731 scsi_tgt_priv_data->dev_removedelay) {
1732 scsi_tgt_priv_data->dev_removedelay = 0;
1733 atomic_dec_if_positive
1734 (&scsi_tgt_priv_data->block_io);
1737 case MPI3_EVENT_SAS_TOPO_PHY_RC_PHY_CHANGED:
1742 mpi3mr_tgtdev_put(tgtdev);
1747 * mpi3mr_devstatuschg_evt_th - DeviceStatusChange evt tophalf
1748 * @mrioc: Adapter instance reference
1749 * @event_reply: event data
1751 * Checks for the reason code and based on that either block I/O
1752 * to device, or unblock I/O to the device, or start the device
1753 * removal handshake with reason as remove/hide acknowledgment
1754 * with the firmware.
1758 static void mpi3mr_devstatuschg_evt_th(struct mpi3mr_ioc *mrioc,
1759 struct mpi3_event_notification_reply *event_reply)
1762 u8 ublock = 0, block = 0, hide = 0, delete = 0, remove = 0;
1763 struct mpi3mr_tgt_dev *tgtdev = NULL;
1764 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
1765 struct mpi3_event_data_device_status_change *evtdata =
1766 (struct mpi3_event_data_device_status_change *)event_reply->event_data;
1768 if (mrioc->stop_drv_processing)
1771 dev_handle = le16_to_cpu(evtdata->dev_handle);
1773 switch (evtdata->reason_code) {
1774 case MPI3_EVENT_DEV_STAT_RC_INT_DEVICE_RESET_STRT:
1775 case MPI3_EVENT_DEV_STAT_RC_INT_IT_NEXUS_RESET_STRT:
1778 case MPI3_EVENT_DEV_STAT_RC_HIDDEN:
1782 case MPI3_EVENT_DEV_STAT_RC_VD_NOT_RESPONDING:
1786 case MPI3_EVENT_DEV_STAT_RC_INT_DEVICE_RESET_CMP:
1787 case MPI3_EVENT_DEV_STAT_RC_INT_IT_NEXUS_RESET_CMP:
1794 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, dev_handle);
1798 tgtdev->is_hidden = hide;
1799 if (tgtdev->starget && tgtdev->starget->hostdata) {
1800 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
1801 tgtdev->starget->hostdata;
1803 atomic_inc(&scsi_tgt_priv_data->block_io);
1805 scsi_tgt_priv_data->dev_removed = 1;
1807 atomic_dec_if_positive(&scsi_tgt_priv_data->block_io);
1810 mpi3mr_dev_rmhs_send_tm(mrioc, dev_handle, NULL,
1811 MPI3_CTRL_OP_REMOVE_DEVICE);
1813 mpi3mr_dev_rmhs_send_tm(mrioc, dev_handle, NULL,
1814 MPI3_CTRL_OP_HIDDEN_ACK);
1818 mpi3mr_tgtdev_put(tgtdev);
1822 * mpi3mr_energypackchg_evt_th - Energy pack change evt tophalf
1823 * @mrioc: Adapter instance reference
1824 * @event_reply: event data
1826 * Identifies the new shutdown timeout value and update.
1830 static void mpi3mr_energypackchg_evt_th(struct mpi3mr_ioc *mrioc,
1831 struct mpi3_event_notification_reply *event_reply)
1833 struct mpi3_event_data_energy_pack_change *evtdata =
1834 (struct mpi3_event_data_energy_pack_change *)event_reply->event_data;
1835 u16 shutdown_timeout = le16_to_cpu(evtdata->shutdown_timeout);
1837 if (shutdown_timeout <= 0) {
1839 "%s :Invalid Shutdown Timeout received = %d\n",
1840 __func__, shutdown_timeout);
1845 "%s :Previous Shutdown Timeout Value = %d New Shutdown Timeout Value = %d\n",
1846 __func__, mrioc->facts.shutdown_timeout, shutdown_timeout);
1847 mrioc->facts.shutdown_timeout = shutdown_timeout;
1851 * mpi3mr_os_handle_events - Firmware event handler
1852 * @mrioc: Adapter instance reference
1853 * @event_reply: event data
1855 * Identify whteher the event has to handled and acknowledged
1856 * and either process the event in the tophalf and/or schedule a
1857 * bottom half through mpi3mr_fwevt_worker.
1861 void mpi3mr_os_handle_events(struct mpi3mr_ioc *mrioc,
1862 struct mpi3_event_notification_reply *event_reply)
1865 struct mpi3mr_fwevt *fwevt = NULL;
1866 bool ack_req = 0, process_evt_bh = 0;
1868 if (mrioc->stop_drv_processing)
1871 if ((event_reply->msg_flags & MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_MASK)
1872 == MPI3_EVENT_NOTIFY_MSGFLAGS_ACK_REQUIRED)
1875 evt_type = event_reply->event;
1878 case MPI3_EVENT_DEVICE_ADDED:
1880 struct mpi3_device_page0 *dev_pg0 =
1881 (struct mpi3_device_page0 *)event_reply->event_data;
1882 if (mpi3mr_create_tgtdev(mrioc, dev_pg0))
1884 "%s :Failed to add device in the device add event\n",
1890 case MPI3_EVENT_DEVICE_STATUS_CHANGE:
1893 mpi3mr_devstatuschg_evt_th(mrioc, event_reply);
1896 case MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
1899 mpi3mr_sastopochg_evt_th(mrioc, event_reply);
1902 case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
1905 mpi3mr_pcietopochg_evt_th(mrioc, event_reply);
1908 case MPI3_EVENT_DEVICE_INFO_CHANGED:
1913 case MPI3_EVENT_ENERGY_PACK_CHANGE:
1915 mpi3mr_energypackchg_evt_th(mrioc, event_reply);
1918 case MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE:
1919 case MPI3_EVENT_SAS_DISCOVERY:
1920 case MPI3_EVENT_CABLE_MGMT:
1921 case MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
1922 case MPI3_EVENT_SAS_BROADCAST_PRIMITIVE:
1923 case MPI3_EVENT_PCIE_ENUMERATION:
1926 ioc_info(mrioc, "%s :event 0x%02x is not handled\n",
1927 __func__, evt_type);
1930 if (process_evt_bh || ack_req) {
1931 sz = event_reply->event_data_length * 4;
1932 fwevt = mpi3mr_alloc_fwevt(sz);
1934 ioc_info(mrioc, "%s :failure at %s:%d/%s()!\n",
1935 __func__, __FILE__, __LINE__, __func__);
1939 memcpy(fwevt->event_data, event_reply->event_data, sz);
1940 fwevt->mrioc = mrioc;
1941 fwevt->event_id = evt_type;
1942 fwevt->send_ack = ack_req;
1943 fwevt->process_evt = process_evt_bh;
1944 fwevt->evt_ctx = le32_to_cpu(event_reply->event_context);
1945 mpi3mr_fwevt_add_to_list(mrioc, fwevt);
1950 * mpi3mr_setup_eedp - Setup EEDP information in MPI3 SCSI IO
1951 * @mrioc: Adapter instance reference
1952 * @scmd: SCSI command reference
1953 * @scsiio_req: MPI3 SCSI IO request
1955 * Identifies the protection information flags from the SCSI
1956 * command and set appropriate flags in the MPI3 SCSI IO
1961 static void mpi3mr_setup_eedp(struct mpi3mr_ioc *mrioc,
1962 struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
1965 unsigned char prot_op = scsi_get_prot_op(scmd);
1968 case SCSI_PROT_NORMAL:
1970 case SCSI_PROT_READ_STRIP:
1971 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REMOVE;
1973 case SCSI_PROT_WRITE_INSERT:
1974 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_INSERT;
1976 case SCSI_PROT_READ_INSERT:
1977 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_INSERT;
1978 scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
1980 case SCSI_PROT_WRITE_STRIP:
1981 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REMOVE;
1982 scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
1984 case SCSI_PROT_READ_PASS:
1985 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK;
1986 scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
1988 case SCSI_PROT_WRITE_PASS:
1989 if (scmd->prot_flags & SCSI_PROT_IP_CHECKSUM) {
1990 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK_REGEN;
1991 scsiio_req->sgl[0].eedp.application_tag_translation_mask =
1994 eedp_flags = MPI3_EEDPFLAGS_EEDP_OP_CHECK;
1996 scsiio_req->msg_flags |= MPI3_SCSIIO_MSGFLAGS_METASGL_VALID;
2002 if (scmd->prot_flags & SCSI_PROT_GUARD_CHECK)
2003 eedp_flags |= MPI3_EEDPFLAGS_CHK_GUARD;
2005 if (scmd->prot_flags & SCSI_PROT_IP_CHECKSUM)
2006 eedp_flags |= MPI3_EEDPFLAGS_HOST_GUARD_IP_CHKSUM;
2008 if (scmd->prot_flags & SCSI_PROT_REF_CHECK) {
2009 eedp_flags |= MPI3_EEDPFLAGS_CHK_REF_TAG |
2010 MPI3_EEDPFLAGS_INCR_PRI_REF_TAG;
2011 scsiio_req->cdb.eedp32.primary_reference_tag =
2012 cpu_to_be32(scsi_prot_ref_tag(scmd));
2015 if (scmd->prot_flags & SCSI_PROT_REF_INCREMENT)
2016 eedp_flags |= MPI3_EEDPFLAGS_INCR_PRI_REF_TAG;
2018 eedp_flags |= MPI3_EEDPFLAGS_ESC_MODE_APPTAG_DISABLE;
2020 switch (scsi_prot_interval(scmd)) {
2022 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_512;
2025 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_520;
2028 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4080;
2031 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4088;
2034 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4096;
2037 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4104;
2040 scsiio_req->sgl[0].eedp.user_data_size = MPI3_EEDP_UDS_4160;
2046 scsiio_req->sgl[0].eedp.eedp_flags = cpu_to_le16(eedp_flags);
2047 scsiio_req->sgl[0].eedp.flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_EXTENDED;
2051 * mpi3mr_build_sense_buffer - Map sense information
2053 * @buf: Sense buffer to populate
2055 * @asc: Additional sense code
2056 * @ascq: Additional sense code qualifier
2058 * Maps the given sense information into either descriptor or
2059 * fixed format sense data.
2063 static inline void mpi3mr_build_sense_buffer(int desc, u8 *buf, u8 key,
2067 buf[0] = 0x72; /* descriptor, current */
2073 buf[0] = 0x70; /* fixed, current */
2082 * mpi3mr_map_eedp_error - Map EEDP errors from IOC status
2083 * @scmd: SCSI command reference
2084 * @ioc_status: status of MPI3 request
2086 * Maps the EEDP error status of the SCSI IO request to sense
2091 static void mpi3mr_map_eedp_error(struct scsi_cmnd *scmd,
2096 switch (ioc_status) {
2097 case MPI3_IOCSTATUS_EEDP_GUARD_ERROR:
2100 case MPI3_IOCSTATUS_EEDP_APP_TAG_ERROR:
2103 case MPI3_IOCSTATUS_EEDP_REF_TAG_ERROR:
2111 mpi3mr_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
2113 scmd->result = (DID_ABORT << 16) | SAM_STAT_CHECK_CONDITION;
2117 * mpi3mr_process_op_reply_desc - reply descriptor handler
2118 * @mrioc: Adapter instance reference
2119 * @reply_desc: Operational reply descriptor
2120 * @reply_dma: place holder for reply DMA address
2121 * @qidx: Operational queue index
2123 * Process the operational reply descriptor and identifies the
2124 * descriptor type. Based on the descriptor map the MPI3 request
2125 * status to a SCSI command status and calls scsi_done call
2130 void mpi3mr_process_op_reply_desc(struct mpi3mr_ioc *mrioc,
2131 struct mpi3_default_reply_descriptor *reply_desc, u64 *reply_dma, u16 qidx)
2133 u16 reply_desc_type, host_tag = 0;
2134 u16 ioc_status = MPI3_IOCSTATUS_SUCCESS;
2135 u32 ioc_loginfo = 0;
2136 struct mpi3_status_reply_descriptor *status_desc = NULL;
2137 struct mpi3_address_reply_descriptor *addr_desc = NULL;
2138 struct mpi3_success_reply_descriptor *success_desc = NULL;
2139 struct mpi3_scsi_io_reply *scsi_reply = NULL;
2140 struct scsi_cmnd *scmd = NULL;
2141 struct scmd_priv *priv = NULL;
2142 u8 *sense_buf = NULL;
2143 u8 scsi_state = 0, scsi_status = 0, sense_state = 0;
2144 u32 xfer_count = 0, sense_count = 0, resp_data = 0;
2145 u16 dev_handle = 0xFFFF;
2146 struct scsi_sense_hdr sshdr;
2149 reply_desc_type = le16_to_cpu(reply_desc->reply_flags) &
2150 MPI3_REPLY_DESCRIPT_FLAGS_TYPE_MASK;
2151 switch (reply_desc_type) {
2152 case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_STATUS:
2153 status_desc = (struct mpi3_status_reply_descriptor *)reply_desc;
2154 host_tag = le16_to_cpu(status_desc->host_tag);
2155 ioc_status = le16_to_cpu(status_desc->ioc_status);
2157 MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
2158 ioc_loginfo = le32_to_cpu(status_desc->ioc_log_info);
2159 ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
2161 case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_ADDRESS_REPLY:
2162 addr_desc = (struct mpi3_address_reply_descriptor *)reply_desc;
2163 *reply_dma = le64_to_cpu(addr_desc->reply_frame_address);
2164 scsi_reply = mpi3mr_get_reply_virt_addr(mrioc,
2167 panic("%s: scsi_reply is NULL, this shouldn't happen\n",
2171 host_tag = le16_to_cpu(scsi_reply->host_tag);
2172 ioc_status = le16_to_cpu(scsi_reply->ioc_status);
2173 scsi_status = scsi_reply->scsi_status;
2174 scsi_state = scsi_reply->scsi_state;
2175 dev_handle = le16_to_cpu(scsi_reply->dev_handle);
2176 sense_state = (scsi_state & MPI3_SCSI_STATE_SENSE_MASK);
2177 xfer_count = le32_to_cpu(scsi_reply->transfer_count);
2178 sense_count = le32_to_cpu(scsi_reply->sense_count);
2179 resp_data = le32_to_cpu(scsi_reply->response_data);
2180 sense_buf = mpi3mr_get_sensebuf_virt_addr(mrioc,
2181 le64_to_cpu(scsi_reply->sense_data_buffer_address));
2183 MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
2184 ioc_loginfo = le32_to_cpu(scsi_reply->ioc_log_info);
2185 ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
2186 if (sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY)
2187 panic("%s: Ran out of sense buffers\n", mrioc->name);
2189 case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_SUCCESS:
2190 success_desc = (struct mpi3_success_reply_descriptor *)reply_desc;
2191 host_tag = le16_to_cpu(success_desc->host_tag);
2196 scmd = mpi3mr_scmd_from_host_tag(mrioc, host_tag, qidx);
2198 panic("%s: Cannot Identify scmd for host_tag 0x%x\n",
2199 mrioc->name, host_tag);
2202 priv = scsi_cmd_priv(scmd);
2204 scmd->result = DID_OK << 16;
2207 if (ioc_status == MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN &&
2208 xfer_count == 0 && (scsi_status == MPI3_SCSI_STATUS_BUSY ||
2209 scsi_status == MPI3_SCSI_STATUS_RESERVATION_CONFLICT ||
2210 scsi_status == MPI3_SCSI_STATUS_TASK_SET_FULL))
2211 ioc_status = MPI3_IOCSTATUS_SUCCESS;
2213 if ((sense_state == MPI3_SCSI_STATE_SENSE_VALID) && sense_count &&
2215 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE, sense_count);
2217 memcpy(scmd->sense_buffer, sense_buf, sz);
2220 switch (ioc_status) {
2221 case MPI3_IOCSTATUS_BUSY:
2222 case MPI3_IOCSTATUS_INSUFFICIENT_RESOURCES:
2223 scmd->result = SAM_STAT_BUSY;
2225 case MPI3_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
2226 scmd->result = DID_NO_CONNECT << 16;
2228 case MPI3_IOCSTATUS_SCSI_IOC_TERMINATED:
2229 scmd->result = DID_SOFT_ERROR << 16;
2231 case MPI3_IOCSTATUS_SCSI_TASK_TERMINATED:
2232 case MPI3_IOCSTATUS_SCSI_EXT_TERMINATED:
2233 scmd->result = DID_RESET << 16;
2235 case MPI3_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
2236 if ((xfer_count == 0) || (scmd->underflow > xfer_count))
2237 scmd->result = DID_SOFT_ERROR << 16;
2239 scmd->result = (DID_OK << 16) | scsi_status;
2241 case MPI3_IOCSTATUS_SCSI_DATA_UNDERRUN:
2242 scmd->result = (DID_OK << 16) | scsi_status;
2243 if (sense_state == MPI3_SCSI_STATE_SENSE_VALID)
2245 if (xfer_count < scmd->underflow) {
2246 if (scsi_status == SAM_STAT_BUSY)
2247 scmd->result = SAM_STAT_BUSY;
2249 scmd->result = DID_SOFT_ERROR << 16;
2250 } else if ((scsi_state & (MPI3_SCSI_STATE_NO_SCSI_STATUS)) ||
2251 (sense_state != MPI3_SCSI_STATE_SENSE_NOT_AVAILABLE))
2252 scmd->result = DID_SOFT_ERROR << 16;
2253 else if (scsi_state & MPI3_SCSI_STATE_TERMINATED)
2254 scmd->result = DID_RESET << 16;
2256 case MPI3_IOCSTATUS_SCSI_DATA_OVERRUN:
2257 scsi_set_resid(scmd, 0);
2259 case MPI3_IOCSTATUS_SCSI_RECOVERED_ERROR:
2260 case MPI3_IOCSTATUS_SUCCESS:
2261 scmd->result = (DID_OK << 16) | scsi_status;
2262 if ((scsi_state & (MPI3_SCSI_STATE_NO_SCSI_STATUS)) ||
2263 (sense_state == MPI3_SCSI_STATE_SENSE_FAILED) ||
2264 (sense_state == MPI3_SCSI_STATE_SENSE_BUFF_Q_EMPTY))
2265 scmd->result = DID_SOFT_ERROR << 16;
2266 else if (scsi_state & MPI3_SCSI_STATE_TERMINATED)
2267 scmd->result = DID_RESET << 16;
2269 case MPI3_IOCSTATUS_EEDP_GUARD_ERROR:
2270 case MPI3_IOCSTATUS_EEDP_REF_TAG_ERROR:
2271 case MPI3_IOCSTATUS_EEDP_APP_TAG_ERROR:
2272 mpi3mr_map_eedp_error(scmd, ioc_status);
2274 case MPI3_IOCSTATUS_SCSI_PROTOCOL_ERROR:
2275 case MPI3_IOCSTATUS_INVALID_FUNCTION:
2276 case MPI3_IOCSTATUS_INVALID_SGL:
2277 case MPI3_IOCSTATUS_INTERNAL_ERROR:
2278 case MPI3_IOCSTATUS_INVALID_FIELD:
2279 case MPI3_IOCSTATUS_INVALID_STATE:
2280 case MPI3_IOCSTATUS_SCSI_IO_DATA_ERROR:
2281 case MPI3_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
2282 case MPI3_IOCSTATUS_INSUFFICIENT_POWER:
2284 scmd->result = DID_SOFT_ERROR << 16;
2288 if (scmd->result != (DID_OK << 16) && (scmd->cmnd[0] != ATA_12) &&
2289 (scmd->cmnd[0] != ATA_16)) {
2290 ioc_info(mrioc, "%s :scmd->result 0x%x\n", __func__,
2292 scsi_print_command(scmd);
2294 "%s :Command issued to handle 0x%02x returned with error 0x%04x loginfo 0x%08x, qid %d\n",
2295 __func__, dev_handle, ioc_status, ioc_loginfo,
2296 priv->req_q_idx + 1);
2298 " host_tag %d scsi_state 0x%02x scsi_status 0x%02x, xfer_cnt %d resp_data 0x%x\n",
2299 host_tag, scsi_state, scsi_status, xfer_count, resp_data);
2301 scsi_normalize_sense(sense_buf, sense_count, &sshdr);
2303 "%s :sense_count 0x%x, sense_key 0x%x ASC 0x%x, ASCQ 0x%x\n",
2304 __func__, sense_count, sshdr.sense_key,
2305 sshdr.asc, sshdr.ascq);
2309 if (priv->meta_sg_valid) {
2310 dma_unmap_sg(&mrioc->pdev->dev, scsi_prot_sglist(scmd),
2311 scsi_prot_sg_count(scmd), scmd->sc_data_direction);
2313 mpi3mr_clear_scmd_priv(mrioc, scmd);
2314 scsi_dma_unmap(scmd);
2315 scmd->scsi_done(scmd);
2318 mpi3mr_repost_sense_buf(mrioc,
2319 le64_to_cpu(scsi_reply->sense_data_buffer_address));
2323 * mpi3mr_get_chain_idx - get free chain buffer index
2324 * @mrioc: Adapter instance reference
2326 * Try to get a free chain buffer index from the free pool.
2328 * Return: -1 on failure or the free chain buffer index
2330 static int mpi3mr_get_chain_idx(struct mpi3mr_ioc *mrioc)
2336 spin_lock(&mrioc->chain_buf_lock);
2337 cmd_idx = find_first_zero_bit(mrioc->chain_bitmap,
2338 mrioc->chain_buf_count);
2339 if (cmd_idx < mrioc->chain_buf_count) {
2340 set_bit(cmd_idx, mrioc->chain_bitmap);
2341 spin_unlock(&mrioc->chain_buf_lock);
2344 spin_unlock(&mrioc->chain_buf_lock);
2346 } while (retry_count--);
2351 * mpi3mr_prepare_sg_scmd - build scatter gather list
2352 * @mrioc: Adapter instance reference
2353 * @scmd: SCSI command reference
2354 * @scsiio_req: MPI3 SCSI IO request
2356 * This function maps SCSI command's data and protection SGEs to
2357 * MPI request SGEs. If required additional 4K chain buffer is
2358 * used to send the SGEs.
2360 * Return: 0 on success, -ENOMEM on dma_map_sg failure
2362 static int mpi3mr_prepare_sg_scmd(struct mpi3mr_ioc *mrioc,
2363 struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
2365 dma_addr_t chain_dma;
2366 struct scatterlist *sg_scmd;
2367 void *sg_local, *chain;
2369 int sges_left, chain_idx;
2370 u32 sges_in_segment;
2371 u8 simple_sgl_flags;
2372 u8 simple_sgl_flags_last;
2373 u8 last_chain_sgl_flags;
2374 struct chain_element *chain_req;
2375 struct scmd_priv *priv = NULL;
2376 u32 meta_sg = le32_to_cpu(scsiio_req->flags) &
2377 MPI3_SCSIIO_FLAGS_DMAOPERATION_HOST_PI;
2379 priv = scsi_cmd_priv(scmd);
2381 simple_sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_SIMPLE |
2382 MPI3_SGE_FLAGS_DLAS_SYSTEM;
2383 simple_sgl_flags_last = simple_sgl_flags |
2384 MPI3_SGE_FLAGS_END_OF_LIST;
2385 last_chain_sgl_flags = MPI3_SGE_FLAGS_ELEMENT_TYPE_LAST_CHAIN |
2386 MPI3_SGE_FLAGS_DLAS_SYSTEM;
2389 sg_local = &scsiio_req->sgl[MPI3_SCSIIO_METASGL_INDEX];
2391 sg_local = &scsiio_req->sgl;
2393 if (!scsiio_req->data_length && !meta_sg) {
2394 mpi3mr_build_zero_len_sge(sg_local);
2399 sg_scmd = scsi_prot_sglist(scmd);
2400 sges_left = dma_map_sg(&mrioc->pdev->dev,
2401 scsi_prot_sglist(scmd),
2402 scsi_prot_sg_count(scmd),
2403 scmd->sc_data_direction);
2404 priv->meta_sg_valid = 1; /* To unmap meta sg DMA */
2406 sg_scmd = scsi_sglist(scmd);
2407 sges_left = scsi_dma_map(scmd);
2410 if (sges_left < 0) {
2411 sdev_printk(KERN_ERR, scmd->device,
2412 "scsi_dma_map failed: request for %d bytes!\n",
2413 scsi_bufflen(scmd));
2416 if (sges_left > MPI3MR_SG_DEPTH) {
2417 sdev_printk(KERN_ERR, scmd->device,
2418 "scsi_dma_map returned unsupported sge count %d!\n",
2423 sges_in_segment = (mrioc->facts.op_req_sz -
2424 offsetof(struct mpi3_scsi_io_request, sgl)) / sizeof(struct mpi3_sge_common);
2426 if (scsiio_req->sgl[0].eedp.flags ==
2427 MPI3_SGE_FLAGS_ELEMENT_TYPE_EXTENDED && !meta_sg) {
2428 sg_local += sizeof(struct mpi3_sge_common);
2430 /* Reserve 1st segment (scsiio_req->sgl[0]) for eedp */
2433 if (scsiio_req->msg_flags ==
2434 MPI3_SCSIIO_MSGFLAGS_METASGL_VALID && !meta_sg) {
2436 /* Reserve last segment (scsiio_req->sgl[3]) for meta sg */
2440 sges_in_segment = 1;
2442 if (sges_left <= sges_in_segment)
2443 goto fill_in_last_segment;
2445 /* fill in main message segment when there is a chain following */
2446 while (sges_in_segment > 1) {
2447 mpi3mr_add_sg_single(sg_local, simple_sgl_flags,
2448 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2449 sg_scmd = sg_next(sg_scmd);
2450 sg_local += sizeof(struct mpi3_sge_common);
2455 chain_idx = mpi3mr_get_chain_idx(mrioc);
2458 chain_req = &mrioc->chain_sgl_list[chain_idx];
2460 priv->meta_chain_idx = chain_idx;
2462 priv->chain_idx = chain_idx;
2464 chain = chain_req->addr;
2465 chain_dma = chain_req->dma_addr;
2466 sges_in_segment = sges_left;
2467 chain_length = sges_in_segment * sizeof(struct mpi3_sge_common);
2469 mpi3mr_add_sg_single(sg_local, last_chain_sgl_flags,
2470 chain_length, chain_dma);
2474 fill_in_last_segment:
2475 while (sges_left > 0) {
2477 mpi3mr_add_sg_single(sg_local,
2478 simple_sgl_flags_last, sg_dma_len(sg_scmd),
2479 sg_dma_address(sg_scmd));
2481 mpi3mr_add_sg_single(sg_local, simple_sgl_flags,
2482 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2483 sg_scmd = sg_next(sg_scmd);
2484 sg_local += sizeof(struct mpi3_sge_common);
2492 * mpi3mr_build_sg_scmd - build scatter gather list for SCSI IO
2493 * @mrioc: Adapter instance reference
2494 * @scmd: SCSI command reference
2495 * @scsiio_req: MPI3 SCSI IO request
2497 * This function calls mpi3mr_prepare_sg_scmd for constructing
2498 * both data SGEs and protection information SGEs in the MPI
2499 * format from the SCSI Command as appropriate .
2501 * Return: return value of mpi3mr_prepare_sg_scmd.
2503 static int mpi3mr_build_sg_scmd(struct mpi3mr_ioc *mrioc,
2504 struct scsi_cmnd *scmd, struct mpi3_scsi_io_request *scsiio_req)
2508 ret = mpi3mr_prepare_sg_scmd(mrioc, scmd, scsiio_req);
2512 if (scsiio_req->msg_flags == MPI3_SCSIIO_MSGFLAGS_METASGL_VALID) {
2513 /* There is a valid meta sg */
2514 scsiio_req->flags |=
2515 cpu_to_le32(MPI3_SCSIIO_FLAGS_DMAOPERATION_HOST_PI);
2516 ret = mpi3mr_prepare_sg_scmd(mrioc, scmd, scsiio_req);
2523 * mpi3mr_print_response_code - print TM response as a string
2524 * @mrioc: Adapter instance reference
2525 * @resp_code: TM response code
2527 * Print TM response code as a readable string.
2531 static void mpi3mr_print_response_code(struct mpi3mr_ioc *mrioc, u8 resp_code)
2535 switch (resp_code) {
2536 case MPI3MR_RSP_TM_COMPLETE:
2537 desc = "task management request completed";
2539 case MPI3MR_RSP_INVALID_FRAME:
2540 desc = "invalid frame";
2542 case MPI3MR_RSP_TM_NOT_SUPPORTED:
2543 desc = "task management request not supported";
2545 case MPI3MR_RSP_TM_FAILED:
2546 desc = "task management request failed";
2548 case MPI3MR_RSP_TM_SUCCEEDED:
2549 desc = "task management request succeeded";
2551 case MPI3MR_RSP_TM_INVALID_LUN:
2552 desc = "invalid lun";
2554 case MPI3MR_RSP_TM_OVERLAPPED_TAG:
2555 desc = "overlapped tag attempted";
2557 case MPI3MR_RSP_IO_QUEUED_ON_IOC:
2558 desc = "task queued, however not sent to target";
2564 ioc_info(mrioc, "%s :response_code(0x%01x): %s\n", __func__,
2569 * mpi3mr_issue_tm - Issue Task Management request
2570 * @mrioc: Adapter instance reference
2571 * @tm_type: Task Management type
2572 * @handle: Device handle
2574 * @htag: Host tag of the TM request
2575 * @drv_cmd: Internal command tracker
2576 * @resp_code: Response code place holder
2577 * @cmd_priv: SCSI command private data
2579 * Issues a Task Management Request to the controller for a
2580 * specified target, lun and command and wait for its completion
2581 * and check TM response. Recover the TM if it timed out by
2582 * issuing controller reset.
2584 * Return: 0 on success, non-zero on errors
2586 static int mpi3mr_issue_tm(struct mpi3mr_ioc *mrioc, u8 tm_type,
2587 u16 handle, uint lun, u16 htag, ulong timeout,
2588 struct mpi3mr_drv_cmd *drv_cmd,
2589 u8 *resp_code, struct scmd_priv *cmd_priv)
2591 struct mpi3_scsi_task_mgmt_request tm_req;
2592 struct mpi3_scsi_task_mgmt_reply *tm_reply = NULL;
2594 struct mpi3mr_tgt_dev *tgtdev = NULL;
2595 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data = NULL;
2596 struct op_req_qinfo *op_req_q = NULL;
2598 ioc_info(mrioc, "%s :Issue TM: TM type (0x%x) for devhandle 0x%04x\n",
2599 __func__, tm_type, handle);
2600 if (mrioc->unrecoverable) {
2602 ioc_err(mrioc, "%s :Issue TM: Unrecoverable controller\n",
2607 memset(&tm_req, 0, sizeof(tm_req));
2608 mutex_lock(&drv_cmd->mutex);
2609 if (drv_cmd->state & MPI3MR_CMD_PENDING) {
2611 ioc_err(mrioc, "%s :Issue TM: Command is in use\n", __func__);
2612 mutex_unlock(&drv_cmd->mutex);
2615 if (mrioc->reset_in_progress) {
2617 ioc_err(mrioc, "%s :Issue TM: Reset in progress\n", __func__);
2618 mutex_unlock(&drv_cmd->mutex);
2622 drv_cmd->state = MPI3MR_CMD_PENDING;
2623 drv_cmd->is_waiting = 1;
2624 drv_cmd->callback = NULL;
2625 tm_req.dev_handle = cpu_to_le16(handle);
2626 tm_req.task_type = tm_type;
2627 tm_req.host_tag = cpu_to_le16(htag);
2629 int_to_scsilun(lun, (struct scsi_lun *)tm_req.lun);
2630 tm_req.function = MPI3_FUNCTION_SCSI_TASK_MGMT;
2632 tgtdev = mpi3mr_get_tgtdev_by_handle(mrioc, handle);
2633 if (tgtdev && tgtdev->starget && tgtdev->starget->hostdata) {
2634 scsi_tgt_priv_data = (struct mpi3mr_stgt_priv_data *)
2635 tgtdev->starget->hostdata;
2636 atomic_inc(&scsi_tgt_priv_data->block_io);
2639 op_req_q = &mrioc->req_qinfo[cmd_priv->req_q_idx];
2640 tm_req.task_host_tag = cpu_to_le16(cmd_priv->host_tag);
2641 tm_req.task_request_queue_id = cpu_to_le16(op_req_q->qid);
2643 if (tgtdev && (tgtdev->dev_type == MPI3_DEVICE_DEVFORM_PCIE)) {
2644 if (cmd_priv && tgtdev->dev_spec.pcie_inf.abort_to)
2645 timeout = tgtdev->dev_spec.pcie_inf.abort_to;
2646 else if (!cmd_priv && tgtdev->dev_spec.pcie_inf.reset_to)
2647 timeout = tgtdev->dev_spec.pcie_inf.reset_to;
2650 init_completion(&drv_cmd->done);
2651 retval = mpi3mr_admin_request_post(mrioc, &tm_req, sizeof(tm_req), 1);
2653 ioc_err(mrioc, "%s :Issue TM: Admin Post failed\n", __func__);
2656 wait_for_completion_timeout(&drv_cmd->done, (timeout * HZ));
2658 if (!(drv_cmd->state & MPI3MR_CMD_COMPLETE)) {
2659 ioc_err(mrioc, "%s :Issue TM: command timed out\n", __func__);
2660 drv_cmd->is_waiting = 0;
2662 mpi3mr_soft_reset_handler(mrioc,
2663 MPI3MR_RESET_FROM_TM_TIMEOUT, 1);
2667 if (drv_cmd->state & MPI3MR_CMD_REPLY_VALID)
2668 tm_reply = (struct mpi3_scsi_task_mgmt_reply *)drv_cmd->reply;
2670 if (drv_cmd->ioc_status != MPI3_IOCSTATUS_SUCCESS) {
2672 "%s :Issue TM: handle(0x%04x) Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
2673 __func__, handle, drv_cmd->ioc_status,
2674 drv_cmd->ioc_loginfo);
2680 ioc_err(mrioc, "%s :Issue TM: No TM Reply message\n", __func__);
2685 *resp_code = le32_to_cpu(tm_reply->response_data) &
2686 MPI3MR_RI_MASK_RESPCODE;
2687 switch (*resp_code) {
2688 case MPI3MR_RSP_TM_SUCCEEDED:
2689 case MPI3MR_RSP_TM_COMPLETE:
2691 case MPI3MR_RSP_IO_QUEUED_ON_IOC:
2692 if (tm_type != MPI3_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
2701 "%s :Issue TM: Completed TM type (0x%x) handle(0x%04x) ",
2702 __func__, tm_type, handle);
2704 "with ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2705 drv_cmd->ioc_status, drv_cmd->ioc_loginfo,
2706 le32_to_cpu(tm_reply->termination_count));
2707 mpi3mr_print_response_code(mrioc, *resp_code);
2710 drv_cmd->state = MPI3MR_CMD_NOTUSED;
2711 mutex_unlock(&drv_cmd->mutex);
2712 if (scsi_tgt_priv_data)
2713 atomic_dec_if_positive(&scsi_tgt_priv_data->block_io);
2715 mpi3mr_tgtdev_put(tgtdev);
2718 * Flush all IRQ handlers by calling synchronize_irq().
2719 * mpi3mr_ioc_disable_intr() takes care of it.
2721 mpi3mr_ioc_disable_intr(mrioc);
2722 mpi3mr_ioc_enable_intr(mrioc);
2729 * mpi3mr_bios_param - BIOS param callback
2730 * @sdev: SCSI device reference
2731 * @bdev: Block device reference
2732 * @capacity: Capacity in logical sectors
2733 * @params: Parameter array
2735 * Just the parameters with heads/secots/cylinders.
2739 static int mpi3mr_bios_param(struct scsi_device *sdev,
2740 struct block_device *bdev, sector_t capacity, int params[])
2750 dummy = heads * sectors;
2751 cylinders = capacity;
2752 sector_div(cylinders, dummy);
2754 if ((ulong)capacity >= 0x200000) {
2757 dummy = heads * sectors;
2758 cylinders = capacity;
2759 sector_div(cylinders, dummy);
2763 params[1] = sectors;
2764 params[2] = cylinders;
2769 * mpi3mr_map_queues - Map queues callback handler
2770 * @shost: SCSI host reference
2772 * Call the blk_mq_pci_map_queues with from which operational
2773 * queue the mapping has to be done
2775 * Return: return of blk_mq_pci_map_queues
2777 static int mpi3mr_map_queues(struct Scsi_Host *shost)
2779 struct mpi3mr_ioc *mrioc = shost_priv(shost);
2781 return blk_mq_pci_map_queues(&shost->tag_set.map[HCTX_TYPE_DEFAULT],
2782 mrioc->pdev, mrioc->op_reply_q_offset);
2786 * mpi3mr_get_fw_pending_ios - Calculate pending I/O count
2787 * @mrioc: Adapter instance reference
2789 * Calculate the pending I/Os for the controller and return.
2791 * Return: Number of pending I/Os
2793 static inline int mpi3mr_get_fw_pending_ios(struct mpi3mr_ioc *mrioc)
2798 for (i = 0; i < mrioc->num_op_reply_q; i++)
2799 pend_ios += atomic_read(&mrioc->op_reply_qinfo[i].pend_ios);
2804 * mpi3mr_print_pending_host_io - print pending I/Os
2805 * @mrioc: Adapter instance reference
2807 * Print number of pending I/Os and each I/O details prior to
2808 * reset for debug purpose.
2812 static void mpi3mr_print_pending_host_io(struct mpi3mr_ioc *mrioc)
2814 struct Scsi_Host *shost = mrioc->shost;
2816 ioc_info(mrioc, "%s :Pending commands prior to reset: %d\n",
2817 __func__, mpi3mr_get_fw_pending_ios(mrioc));
2818 blk_mq_tagset_busy_iter(&shost->tag_set,
2819 mpi3mr_print_scmd, (void *)mrioc);
2823 * mpi3mr_wait_for_host_io - block for I/Os to complete
2824 * @mrioc: Adapter instance reference
2825 * @timeout: time out in seconds
2826 * Waits for pending I/Os for the given adapter to complete or
2827 * to hit the timeout.
2831 void mpi3mr_wait_for_host_io(struct mpi3mr_ioc *mrioc, u32 timeout)
2833 enum mpi3mr_iocstate iocstate;
2836 iocstate = mpi3mr_get_iocstate(mrioc);
2837 if (iocstate != MRIOC_STATE_READY)
2840 if (!mpi3mr_get_fw_pending_ios(mrioc))
2843 "%s :Waiting for %d seconds prior to reset for %d I/O\n",
2844 __func__, timeout, mpi3mr_get_fw_pending_ios(mrioc));
2846 for (i = 0; i < timeout; i++) {
2847 if (!mpi3mr_get_fw_pending_ios(mrioc))
2849 iocstate = mpi3mr_get_iocstate(mrioc);
2850 if (iocstate != MRIOC_STATE_READY)
2855 ioc_info(mrioc, "%s :Pending I/Os after wait is: %d\n", __func__,
2856 mpi3mr_get_fw_pending_ios(mrioc));
2860 * mpi3mr_eh_host_reset - Host reset error handling callback
2861 * @scmd: SCSI command reference
2863 * Issue controller reset if the scmd is for a Physical Device,
2864 * if the scmd is for RAID volume, then wait for
2865 * MPI3MR_RAID_ERRREC_RESET_TIMEOUT and checke whether any
2866 * pending I/Os prior to issuing reset to the controller.
2868 * Return: SUCCESS of successful reset else FAILED
2870 static int mpi3mr_eh_host_reset(struct scsi_cmnd *scmd)
2872 struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
2873 struct mpi3mr_stgt_priv_data *stgt_priv_data;
2874 struct mpi3mr_sdev_priv_data *sdev_priv_data;
2875 u8 dev_type = MPI3_DEVICE_DEVFORM_VD;
2876 int retval = FAILED, ret;
2878 sdev_priv_data = scmd->device->hostdata;
2879 if (sdev_priv_data && sdev_priv_data->tgt_priv_data) {
2880 stgt_priv_data = sdev_priv_data->tgt_priv_data;
2881 dev_type = stgt_priv_data->dev_type;
2884 if (dev_type == MPI3_DEVICE_DEVFORM_VD) {
2885 mpi3mr_wait_for_host_io(mrioc,
2886 MPI3MR_RAID_ERRREC_RESET_TIMEOUT);
2887 if (!mpi3mr_get_fw_pending_ios(mrioc)) {
2893 mpi3mr_print_pending_host_io(mrioc);
2894 ret = mpi3mr_soft_reset_handler(mrioc,
2895 MPI3MR_RESET_FROM_EH_HOS, 1);
2901 sdev_printk(KERN_INFO, scmd->device,
2902 "Host reset is %s for scmd(%p)\n",
2903 ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2909 * mpi3mr_eh_target_reset - Target reset error handling callback
2910 * @scmd: SCSI command reference
2912 * Issue Target reset Task Management and verify the scmd is
2913 * terminated successfully and return status accordingly.
2915 * Return: SUCCESS of successful termination of the scmd else
2918 static int mpi3mr_eh_target_reset(struct scsi_cmnd *scmd)
2920 struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
2921 struct mpi3mr_stgt_priv_data *stgt_priv_data;
2922 struct mpi3mr_sdev_priv_data *sdev_priv_data;
2925 int retval = FAILED, ret = 0;
2927 sdev_printk(KERN_INFO, scmd->device,
2928 "Attempting Target Reset! scmd(%p)\n", scmd);
2929 scsi_print_command(scmd);
2931 sdev_priv_data = scmd->device->hostdata;
2932 if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
2933 sdev_printk(KERN_INFO, scmd->device,
2934 "SCSI device is not available\n");
2939 stgt_priv_data = sdev_priv_data->tgt_priv_data;
2940 dev_handle = stgt_priv_data->dev_handle;
2941 sdev_printk(KERN_INFO, scmd->device,
2942 "Target Reset is issued to handle(0x%04x)\n",
2945 ret = mpi3mr_issue_tm(mrioc,
2946 MPI3_SCSITASKMGMT_TASKTYPE_TARGET_RESET, dev_handle,
2947 sdev_priv_data->lun_id, MPI3MR_HOSTTAG_BLK_TMS,
2948 MPI3MR_RESETTM_TIMEOUT, &mrioc->host_tm_cmds, &resp_code, NULL);
2955 sdev_printk(KERN_INFO, scmd->device,
2956 "Target reset is %s for scmd(%p)\n",
2957 ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2963 * mpi3mr_eh_dev_reset- Device reset error handling callback
2964 * @scmd: SCSI command reference
2966 * Issue lun reset Task Management and verify the scmd is
2967 * terminated successfully and return status accordingly.
2969 * Return: SUCCESS of successful termination of the scmd else
2972 static int mpi3mr_eh_dev_reset(struct scsi_cmnd *scmd)
2974 struct mpi3mr_ioc *mrioc = shost_priv(scmd->device->host);
2975 struct mpi3mr_stgt_priv_data *stgt_priv_data;
2976 struct mpi3mr_sdev_priv_data *sdev_priv_data;
2979 int retval = FAILED, ret = 0;
2981 sdev_printk(KERN_INFO, scmd->device,
2982 "Attempting Device(lun) Reset! scmd(%p)\n", scmd);
2983 scsi_print_command(scmd);
2985 sdev_priv_data = scmd->device->hostdata;
2986 if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
2987 sdev_printk(KERN_INFO, scmd->device,
2988 "SCSI device is not available\n");
2993 stgt_priv_data = sdev_priv_data->tgt_priv_data;
2994 dev_handle = stgt_priv_data->dev_handle;
2995 sdev_printk(KERN_INFO, scmd->device,
2996 "Device(lun) Reset is issued to handle(0x%04x)\n", dev_handle);
2998 ret = mpi3mr_issue_tm(mrioc,
2999 MPI3_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, dev_handle,
3000 sdev_priv_data->lun_id, MPI3MR_HOSTTAG_BLK_TMS,
3001 MPI3MR_RESETTM_TIMEOUT, &mrioc->host_tm_cmds, &resp_code, NULL);
3008 sdev_printk(KERN_INFO, scmd->device,
3009 "Device(lun) reset is %s for scmd(%p)\n",
3010 ((retval == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
3016 * mpi3mr_scan_start - Scan start callback handler
3017 * @shost: SCSI host reference
3019 * Issue port enable request asynchronously.
3023 static void mpi3mr_scan_start(struct Scsi_Host *shost)
3025 struct mpi3mr_ioc *mrioc = shost_priv(shost);
3027 mrioc->scan_started = 1;
3028 ioc_info(mrioc, "%s :Issuing Port Enable\n", __func__);
3029 if (mpi3mr_issue_port_enable(mrioc, 1)) {
3030 ioc_err(mrioc, "%s :Issuing port enable failed\n", __func__);
3031 mrioc->scan_started = 0;
3032 mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
3037 * mpi3mr_scan_finished - Scan finished callback handler
3038 * @shost: SCSI host reference
3039 * @time: Jiffies from the scan start
3041 * Checks whether the port enable is completed or timedout or
3042 * failed and set the scan status accordingly after taking any
3043 * recovery if required.
3045 * Return: 1 on scan finished or timed out, 0 for in progress
3047 static int mpi3mr_scan_finished(struct Scsi_Host *shost,
3050 struct mpi3mr_ioc *mrioc = shost_priv(shost);
3051 u32 pe_timeout = MPI3MR_PORTENABLE_TIMEOUT;
3053 if (time >= (pe_timeout * HZ)) {
3054 mrioc->init_cmds.is_waiting = 0;
3055 mrioc->init_cmds.callback = NULL;
3056 mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
3057 ioc_err(mrioc, "%s :port enable request timed out\n", __func__);
3058 mrioc->is_driver_loading = 0;
3059 mpi3mr_soft_reset_handler(mrioc,
3060 MPI3MR_RESET_FROM_PE_TIMEOUT, 1);
3063 if (mrioc->scan_failed) {
3065 "%s :port enable failed with (ioc_status=0x%08x)\n",
3066 __func__, mrioc->scan_failed);
3067 mrioc->is_driver_loading = 0;
3068 mrioc->stop_drv_processing = 1;
3072 if (mrioc->scan_started)
3074 ioc_info(mrioc, "%s :port enable: SUCCESS\n", __func__);
3075 mpi3mr_start_watchdog(mrioc);
3076 mrioc->is_driver_loading = 0;
3082 * mpi3mr_slave_destroy - Slave destroy callback handler
3083 * @sdev: SCSI device reference
3085 * Cleanup and free per device(lun) private data.
3089 static void mpi3mr_slave_destroy(struct scsi_device *sdev)
3091 struct Scsi_Host *shost;
3092 struct mpi3mr_ioc *mrioc;
3093 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
3094 struct mpi3mr_tgt_dev *tgt_dev;
3095 unsigned long flags;
3096 struct scsi_target *starget;
3098 if (!sdev->hostdata)
3101 starget = scsi_target(sdev);
3102 shost = dev_to_shost(&starget->dev);
3103 mrioc = shost_priv(shost);
3104 scsi_tgt_priv_data = starget->hostdata;
3106 scsi_tgt_priv_data->num_luns--;
3108 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
3109 tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
3110 if (tgt_dev && (!scsi_tgt_priv_data->num_luns))
3111 tgt_dev->starget = NULL;
3113 mpi3mr_tgtdev_put(tgt_dev);
3114 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
3116 kfree(sdev->hostdata);
3117 sdev->hostdata = NULL;
3121 * mpi3mr_target_destroy - Target destroy callback handler
3122 * @starget: SCSI target reference
3124 * Cleanup and free per target private data.
3128 static void mpi3mr_target_destroy(struct scsi_target *starget)
3130 struct Scsi_Host *shost;
3131 struct mpi3mr_ioc *mrioc;
3132 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
3133 struct mpi3mr_tgt_dev *tgt_dev;
3134 unsigned long flags;
3136 if (!starget->hostdata)
3139 shost = dev_to_shost(&starget->dev);
3140 mrioc = shost_priv(shost);
3141 scsi_tgt_priv_data = starget->hostdata;
3143 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
3144 tgt_dev = __mpi3mr_get_tgtdev_from_tgtpriv(mrioc, scsi_tgt_priv_data);
3145 if (tgt_dev && (tgt_dev->starget == starget) &&
3146 (tgt_dev->perst_id == starget->id))
3147 tgt_dev->starget = NULL;
3149 scsi_tgt_priv_data->tgt_dev = NULL;
3150 scsi_tgt_priv_data->perst_id = 0;
3151 mpi3mr_tgtdev_put(tgt_dev);
3152 mpi3mr_tgtdev_put(tgt_dev);
3154 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
3156 kfree(starget->hostdata);
3157 starget->hostdata = NULL;
3161 * mpi3mr_slave_configure - Slave configure callback handler
3162 * @sdev: SCSI device reference
3164 * Configure queue depth, max hardware sectors and virt boundary
3169 static int mpi3mr_slave_configure(struct scsi_device *sdev)
3171 struct scsi_target *starget;
3172 struct Scsi_Host *shost;
3173 struct mpi3mr_ioc *mrioc;
3174 struct mpi3mr_tgt_dev *tgt_dev;
3175 unsigned long flags;
3178 starget = scsi_target(sdev);
3179 shost = dev_to_shost(&starget->dev);
3180 mrioc = shost_priv(shost);
3182 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
3183 tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
3184 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
3188 mpi3mr_change_queue_depth(sdev, tgt_dev->q_depth);
3189 switch (tgt_dev->dev_type) {
3190 case MPI3_DEVICE_DEVFORM_PCIE:
3191 /*The block layer hw sector size = 512*/
3192 blk_queue_max_hw_sectors(sdev->request_queue,
3193 tgt_dev->dev_spec.pcie_inf.mdts / 512);
3194 blk_queue_virt_boundary(sdev->request_queue,
3195 ((1 << tgt_dev->dev_spec.pcie_inf.pgsz) - 1));
3201 mpi3mr_tgtdev_put(tgt_dev);
3207 * mpi3mr_slave_alloc -Slave alloc callback handler
3208 * @sdev: SCSI device reference
3210 * Allocate per device(lun) private data and initialize it.
3212 * Return: 0 on success -ENOMEM on memory allocation failure.
3214 static int mpi3mr_slave_alloc(struct scsi_device *sdev)
3216 struct Scsi_Host *shost;
3217 struct mpi3mr_ioc *mrioc;
3218 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
3219 struct mpi3mr_tgt_dev *tgt_dev;
3220 struct mpi3mr_sdev_priv_data *scsi_dev_priv_data;
3221 unsigned long flags;
3222 struct scsi_target *starget;
3225 starget = scsi_target(sdev);
3226 shost = dev_to_shost(&starget->dev);
3227 mrioc = shost_priv(shost);
3228 scsi_tgt_priv_data = starget->hostdata;
3230 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
3231 tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
3234 if (tgt_dev->starget == NULL)
3235 tgt_dev->starget = starget;
3236 mpi3mr_tgtdev_put(tgt_dev);
3239 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
3243 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
3245 scsi_dev_priv_data = kzalloc(sizeof(*scsi_dev_priv_data), GFP_KERNEL);
3246 if (!scsi_dev_priv_data)
3249 scsi_dev_priv_data->lun_id = sdev->lun;
3250 scsi_dev_priv_data->tgt_priv_data = scsi_tgt_priv_data;
3251 sdev->hostdata = scsi_dev_priv_data;
3253 scsi_tgt_priv_data->num_luns++;
3259 * mpi3mr_target_alloc - Target alloc callback handler
3260 * @starget: SCSI target reference
3262 * Allocate per target private data and initialize it.
3264 * Return: 0 on success -ENOMEM on memory allocation failure.
3266 static int mpi3mr_target_alloc(struct scsi_target *starget)
3268 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
3269 struct mpi3mr_ioc *mrioc = shost_priv(shost);
3270 struct mpi3mr_stgt_priv_data *scsi_tgt_priv_data;
3271 struct mpi3mr_tgt_dev *tgt_dev;
3272 unsigned long flags;
3275 scsi_tgt_priv_data = kzalloc(sizeof(*scsi_tgt_priv_data), GFP_KERNEL);
3276 if (!scsi_tgt_priv_data)
3279 starget->hostdata = scsi_tgt_priv_data;
3281 spin_lock_irqsave(&mrioc->tgtdev_lock, flags);
3282 tgt_dev = __mpi3mr_get_tgtdev_by_perst_id(mrioc, starget->id);
3283 if (tgt_dev && !tgt_dev->is_hidden) {
3284 scsi_tgt_priv_data->starget = starget;
3285 scsi_tgt_priv_data->dev_handle = tgt_dev->dev_handle;
3286 scsi_tgt_priv_data->perst_id = tgt_dev->perst_id;
3287 scsi_tgt_priv_data->dev_type = tgt_dev->dev_type;
3288 scsi_tgt_priv_data->tgt_dev = tgt_dev;
3289 tgt_dev->starget = starget;
3290 atomic_set(&scsi_tgt_priv_data->block_io, 0);
3294 spin_unlock_irqrestore(&mrioc->tgtdev_lock, flags);
3300 * mpi3mr_check_return_unmap - Whether an unmap is allowed
3301 * @mrioc: Adapter instance reference
3302 * @scmd: SCSI Command reference
3304 * The controller hardware cannot handle certain unmap commands
3305 * for NVMe drives, this routine checks those and return true
3306 * and completes the SCSI command with proper status and sense
3309 * Return: TRUE for not allowed unmap, FALSE otherwise.
3311 static bool mpi3mr_check_return_unmap(struct mpi3mr_ioc *mrioc,
3312 struct scsi_cmnd *scmd)
3315 u16 param_len, desc_len;
3317 param_len = get_unaligned_be16(scmd->cmnd + 7);
3321 "%s: cdb received with zero parameter length\n",
3323 scsi_print_command(scmd);
3324 scmd->result = DID_OK << 16;
3325 scmd->scsi_done(scmd);
3329 if (param_len < 24) {
3331 "%s: cdb received with invalid param_len: %d\n",
3332 __func__, param_len);
3333 scsi_print_command(scmd);
3334 scmd->result = SAM_STAT_CHECK_CONDITION;
3335 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
3337 scmd->scsi_done(scmd);
3340 if (param_len != scsi_bufflen(scmd)) {
3342 "%s: cdb received with param_len: %d bufflen: %d\n",
3343 __func__, param_len, scsi_bufflen(scmd));
3344 scsi_print_command(scmd);
3345 scmd->result = SAM_STAT_CHECK_CONDITION;
3346 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
3348 scmd->scsi_done(scmd);
3351 buf = kzalloc(scsi_bufflen(scmd), GFP_ATOMIC);
3353 scsi_print_command(scmd);
3354 scmd->result = SAM_STAT_CHECK_CONDITION;
3355 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
3357 scmd->scsi_done(scmd);
3360 scsi_sg_copy_to_buffer(scmd, buf, scsi_bufflen(scmd));
3361 desc_len = get_unaligned_be16(&buf[2]);
3363 if (desc_len < 16) {
3365 "%s: Invalid descriptor length in param list: %d\n",
3366 __func__, desc_len);
3367 scsi_print_command(scmd);
3368 scmd->result = SAM_STAT_CHECK_CONDITION;
3369 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST,
3371 scmd->scsi_done(scmd);
3376 if (param_len > (desc_len + 8)) {
3377 scsi_print_command(scmd);
3379 "%s: Truncating param_len(%d) to desc_len+8(%d)\n",
3380 __func__, param_len, (desc_len + 8));
3381 param_len = desc_len + 8;
3382 put_unaligned_be16(param_len, scmd->cmnd + 7);
3383 scsi_print_command(scmd);
3391 * mpi3mr_allow_scmd_to_fw - Command is allowed during shutdown
3392 * @scmd: SCSI Command reference
3394 * Checks whether a cdb is allowed during shutdown or not.
3396 * Return: TRUE for allowed commands, FALSE otherwise.
3399 inline bool mpi3mr_allow_scmd_to_fw(struct scsi_cmnd *scmd)
3401 switch (scmd->cmnd[0]) {
3402 case SYNCHRONIZE_CACHE:
3411 * mpi3mr_qcmd - I/O request despatcher
3412 * @shost: SCSI Host reference
3413 * @scmd: SCSI Command reference
3415 * Issues the SCSI Command as an MPI3 request.
3417 * Return: 0 on successful queueing of the request or if the
3418 * request is completed with failure.
3419 * SCSI_MLQUEUE_DEVICE_BUSY when the device is busy.
3420 * SCSI_MLQUEUE_HOST_BUSY when the host queue is full.
3422 static int mpi3mr_qcmd(struct Scsi_Host *shost,
3423 struct scsi_cmnd *scmd)
3425 struct mpi3mr_ioc *mrioc = shost_priv(shost);
3426 struct mpi3mr_stgt_priv_data *stgt_priv_data;
3427 struct mpi3mr_sdev_priv_data *sdev_priv_data;
3428 struct scmd_priv *scmd_priv_data = NULL;
3429 struct mpi3_scsi_io_request *scsiio_req = NULL;
3430 struct op_req_qinfo *op_req_q = NULL;
3434 u32 scsiio_flags = 0;
3435 struct request *rq = scsi_cmd_to_rq(scmd);
3438 sdev_priv_data = scmd->device->hostdata;
3439 if (!sdev_priv_data || !sdev_priv_data->tgt_priv_data) {
3440 scmd->result = DID_NO_CONNECT << 16;
3441 scmd->scsi_done(scmd);
3445 if (mrioc->stop_drv_processing &&
3446 !(mpi3mr_allow_scmd_to_fw(scmd))) {
3447 scmd->result = DID_NO_CONNECT << 16;
3448 scmd->scsi_done(scmd);
3452 if (mrioc->reset_in_progress) {
3453 retval = SCSI_MLQUEUE_HOST_BUSY;
3457 stgt_priv_data = sdev_priv_data->tgt_priv_data;
3459 dev_handle = stgt_priv_data->dev_handle;
3460 if (dev_handle == MPI3MR_INVALID_DEV_HANDLE) {
3461 scmd->result = DID_NO_CONNECT << 16;
3462 scmd->scsi_done(scmd);
3465 if (stgt_priv_data->dev_removed) {
3466 scmd->result = DID_NO_CONNECT << 16;
3467 scmd->scsi_done(scmd);
3471 if (atomic_read(&stgt_priv_data->block_io)) {
3472 if (mrioc->stop_drv_processing) {
3473 scmd->result = DID_NO_CONNECT << 16;
3474 scmd->scsi_done(scmd);
3477 retval = SCSI_MLQUEUE_DEVICE_BUSY;
3481 if ((scmd->cmnd[0] == UNMAP) &&
3482 (stgt_priv_data->dev_type == MPI3_DEVICE_DEVFORM_PCIE) &&
3483 mpi3mr_check_return_unmap(mrioc, scmd))
3486 host_tag = mpi3mr_host_tag_for_scmd(mrioc, scmd);
3487 if (host_tag == MPI3MR_HOSTTAG_INVALID) {
3488 scmd->result = DID_ERROR << 16;
3489 scmd->scsi_done(scmd);
3493 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3494 scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_READ;
3495 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3496 scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_WRITE;
3498 scsiio_flags = MPI3_SCSIIO_FLAGS_DATADIRECTION_NO_DATA_TRANSFER;
3500 scsiio_flags |= MPI3_SCSIIO_FLAGS_TASKATTRIBUTE_SIMPLEQ;
3502 if (sdev_priv_data->ncq_prio_enable) {
3503 iprio_class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
3504 if (iprio_class == IOPRIO_CLASS_RT)
3505 scsiio_flags |= 1 << MPI3_SCSIIO_FLAGS_CMDPRI_SHIFT;
3508 if (scmd->cmd_len > 16)
3509 scsiio_flags |= MPI3_SCSIIO_FLAGS_CDB_GREATER_THAN_16;
3511 scmd_priv_data = scsi_cmd_priv(scmd);
3512 memset(scmd_priv_data->mpi3mr_scsiio_req, 0, MPI3MR_ADMIN_REQ_FRAME_SZ);
3513 scsiio_req = (struct mpi3_scsi_io_request *)scmd_priv_data->mpi3mr_scsiio_req;
3514 scsiio_req->function = MPI3_FUNCTION_SCSI_IO;
3515 scsiio_req->host_tag = cpu_to_le16(host_tag);
3517 mpi3mr_setup_eedp(mrioc, scmd, scsiio_req);
3519 memcpy(scsiio_req->cdb.cdb32, scmd->cmnd, scmd->cmd_len);
3520 scsiio_req->data_length = cpu_to_le32(scsi_bufflen(scmd));
3521 scsiio_req->dev_handle = cpu_to_le16(dev_handle);
3522 scsiio_req->flags = cpu_to_le32(scsiio_flags);
3523 int_to_scsilun(sdev_priv_data->lun_id,
3524 (struct scsi_lun *)scsiio_req->lun);
3526 if (mpi3mr_build_sg_scmd(mrioc, scmd, scsiio_req)) {
3527 mpi3mr_clear_scmd_priv(mrioc, scmd);
3528 retval = SCSI_MLQUEUE_HOST_BUSY;
3531 op_req_q = &mrioc->req_qinfo[scmd_priv_data->req_q_idx];
3533 if (mpi3mr_op_request_post(mrioc, op_req_q,
3534 scmd_priv_data->mpi3mr_scsiio_req)) {
3535 mpi3mr_clear_scmd_priv(mrioc, scmd);
3536 retval = SCSI_MLQUEUE_HOST_BUSY;
3544 static struct scsi_host_template mpi3mr_driver_template = {
3545 .module = THIS_MODULE,
3546 .name = "MPI3 Storage Controller",
3547 .proc_name = MPI3MR_DRIVER_NAME,
3548 .queuecommand = mpi3mr_qcmd,
3549 .target_alloc = mpi3mr_target_alloc,
3550 .slave_alloc = mpi3mr_slave_alloc,
3551 .slave_configure = mpi3mr_slave_configure,
3552 .target_destroy = mpi3mr_target_destroy,
3553 .slave_destroy = mpi3mr_slave_destroy,
3554 .scan_finished = mpi3mr_scan_finished,
3555 .scan_start = mpi3mr_scan_start,
3556 .change_queue_depth = mpi3mr_change_queue_depth,
3557 .eh_device_reset_handler = mpi3mr_eh_dev_reset,
3558 .eh_target_reset_handler = mpi3mr_eh_target_reset,
3559 .eh_host_reset_handler = mpi3mr_eh_host_reset,
3560 .bios_param = mpi3mr_bios_param,
3561 .map_queues = mpi3mr_map_queues,
3565 .sg_tablesize = MPI3MR_SG_DEPTH,
3566 /* max xfer supported is 1M (2K in 512 byte sized sectors)
3568 .max_sectors = 2048,
3569 .cmd_per_lun = MPI3MR_MAX_CMDS_LUN,
3570 .track_queue_depth = 1,
3571 .cmd_size = sizeof(struct scmd_priv),
3575 * mpi3mr_init_drv_cmd - Initialize internal command tracker
3576 * @cmdptr: Internal command tracker
3577 * @host_tag: Host tag used for the specific command
3579 * Initialize the internal command tracker structure with
3580 * specified host tag.
3584 static inline void mpi3mr_init_drv_cmd(struct mpi3mr_drv_cmd *cmdptr,
3587 mutex_init(&cmdptr->mutex);
3588 cmdptr->reply = NULL;
3589 cmdptr->state = MPI3MR_CMD_NOTUSED;
3590 cmdptr->dev_handle = MPI3MR_INVALID_DEV_HANDLE;
3591 cmdptr->host_tag = host_tag;
3595 * osintfc_mrioc_security_status -Check controller secure status
3596 * @pdev: PCI device instance
3598 * Read the Device Serial Number capability from PCI config
3599 * space and decide whether the controller is secure or not.
3601 * Return: 0 on success, non-zero on failure.
3604 osintfc_mrioc_security_status(struct pci_dev *pdev)
3612 base = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DSN);
3615 "%s: PCI_EXT_CAP_ID_DSN is not supported\n", __func__);
3619 pci_read_config_dword(pdev, base + 4, &cap_data);
3621 debug_status = cap_data & MPI3MR_CTLR_SECURE_DBG_STATUS_MASK;
3622 ctlr_status = cap_data & MPI3MR_CTLR_SECURITY_STATUS_MASK;
3624 switch (ctlr_status) {
3625 case MPI3MR_INVALID_DEVICE:
3627 "%s: Non secure ctlr (Invalid) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
3628 __func__, pdev->device, pdev->subsystem_vendor,
3629 pdev->subsystem_device);
3632 case MPI3MR_CONFIG_SECURE_DEVICE:
3634 dev_info(&pdev->dev,
3635 "%s: Config secure ctlr is detected\n",
3638 case MPI3MR_HARD_SECURE_DEVICE:
3640 case MPI3MR_TAMPERED_DEVICE:
3642 "%s: Non secure ctlr (Tampered) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
3643 __func__, pdev->device, pdev->subsystem_vendor,
3644 pdev->subsystem_device);
3652 if (!retval && debug_status) {
3654 "%s: Non secure ctlr (Secure Dbg) is detected: DID: 0x%x: SVID: 0x%x: SDID: 0x%x\n",
3655 __func__, pdev->device, pdev->subsystem_vendor,
3656 pdev->subsystem_device);
3664 * mpi3mr_probe - PCI probe callback
3665 * @pdev: PCI device instance
3666 * @id: PCI device ID details
3668 * controller initialization routine. Checks the security status
3669 * of the controller and if it is invalid or tampered return the
3670 * probe without initializing the controller. Otherwise,
3671 * allocate per adapter instance through shost_priv and
3672 * initialize controller specific data structures, initializae
3673 * the controller hardware, add shost to the SCSI subsystem.
3675 * Return: 0 on success, non-zero on failure.
3679 mpi3mr_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3681 struct mpi3mr_ioc *mrioc = NULL;
3682 struct Scsi_Host *shost = NULL;
3685 if (osintfc_mrioc_security_status(pdev)) {
3686 warn_non_secure_ctlr = 1;
3687 return 1; /* For Invalid and Tampered device */
3690 shost = scsi_host_alloc(&mpi3mr_driver_template,
3691 sizeof(struct mpi3mr_ioc));
3697 mrioc = shost_priv(shost);
3698 mrioc->id = mrioc_ids++;
3699 sprintf(mrioc->driver_name, "%s", MPI3MR_DRIVER_NAME);
3700 sprintf(mrioc->name, "%s%d", mrioc->driver_name, mrioc->id);
3701 INIT_LIST_HEAD(&mrioc->list);
3702 spin_lock(&mrioc_list_lock);
3703 list_add_tail(&mrioc->list, &mrioc_list);
3704 spin_unlock(&mrioc_list_lock);
3706 spin_lock_init(&mrioc->admin_req_lock);
3707 spin_lock_init(&mrioc->reply_free_queue_lock);
3708 spin_lock_init(&mrioc->sbq_lock);
3709 spin_lock_init(&mrioc->fwevt_lock);
3710 spin_lock_init(&mrioc->tgtdev_lock);
3711 spin_lock_init(&mrioc->watchdog_lock);
3712 spin_lock_init(&mrioc->chain_buf_lock);
3714 INIT_LIST_HEAD(&mrioc->fwevt_list);
3715 INIT_LIST_HEAD(&mrioc->tgtdev_list);
3716 INIT_LIST_HEAD(&mrioc->delayed_rmhs_list);
3718 mutex_init(&mrioc->reset_mutex);
3719 mpi3mr_init_drv_cmd(&mrioc->init_cmds, MPI3MR_HOSTTAG_INITCMDS);
3720 mpi3mr_init_drv_cmd(&mrioc->host_tm_cmds, MPI3MR_HOSTTAG_BLK_TMS);
3722 for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++)
3723 mpi3mr_init_drv_cmd(&mrioc->dev_rmhs_cmds[i],
3724 MPI3MR_HOSTTAG_DEVRMCMD_MIN + i);
3727 mrioc->enable_segqueue = true;
3729 init_waitqueue_head(&mrioc->reset_waitq);
3730 mrioc->logging_level = logging_level;
3731 mrioc->shost = shost;
3734 /* init shost parameters */
3735 shost->max_cmd_len = MPI3MR_MAX_CDB_LENGTH;
3736 shost->max_lun = -1;
3737 shost->unique_id = mrioc->id;
3739 shost->max_channel = 1;
3740 shost->max_id = 0xFFFFFFFF;
3743 scsi_host_set_prot(shost, prot_mask);
3745 prot_mask = SHOST_DIF_TYPE1_PROTECTION
3746 | SHOST_DIF_TYPE2_PROTECTION
3747 | SHOST_DIF_TYPE3_PROTECTION;
3748 scsi_host_set_prot(shost, prot_mask);
3752 "%s :host protection capabilities enabled %s%s%s%s%s%s%s\n",
3754 (prot_mask & SHOST_DIF_TYPE1_PROTECTION) ? " DIF1" : "",
3755 (prot_mask & SHOST_DIF_TYPE2_PROTECTION) ? " DIF2" : "",
3756 (prot_mask & SHOST_DIF_TYPE3_PROTECTION) ? " DIF3" : "",
3757 (prot_mask & SHOST_DIX_TYPE0_PROTECTION) ? " DIX0" : "",
3758 (prot_mask & SHOST_DIX_TYPE1_PROTECTION) ? " DIX1" : "",
3759 (prot_mask & SHOST_DIX_TYPE2_PROTECTION) ? " DIX2" : "",
3760 (prot_mask & SHOST_DIX_TYPE3_PROTECTION) ? " DIX3" : "");
3762 if (prot_guard_mask)
3763 scsi_host_set_guard(shost, (prot_guard_mask & 3));
3765 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
3767 snprintf(mrioc->fwevt_worker_name, sizeof(mrioc->fwevt_worker_name),
3768 "%s%d_fwevt_wrkr", mrioc->driver_name, mrioc->id);
3769 mrioc->fwevt_worker_thread = alloc_ordered_workqueue(
3770 mrioc->fwevt_worker_name, WQ_MEM_RECLAIM);
3771 if (!mrioc->fwevt_worker_thread) {
3772 ioc_err(mrioc, "failure at %s:%d/%s()!\n",
3773 __FILE__, __LINE__, __func__);
3775 goto out_fwevtthread_failed;
3778 mrioc->is_driver_loading = 1;
3779 if (mpi3mr_init_ioc(mrioc, MPI3MR_IT_INIT)) {
3780 ioc_err(mrioc, "failure at %s:%d/%s()!\n",
3781 __FILE__, __LINE__, __func__);
3783 goto out_iocinit_failed;
3786 shost->nr_hw_queues = mrioc->num_op_reply_q;
3787 shost->can_queue = mrioc->max_host_ios;
3788 shost->sg_tablesize = MPI3MR_SG_DEPTH;
3789 shost->max_id = mrioc->facts.max_perids;
3791 retval = scsi_add_host(shost, &pdev->dev);
3793 ioc_err(mrioc, "failure at %s:%d/%s()!\n",
3794 __FILE__, __LINE__, __func__);
3795 goto addhost_failed;
3798 scsi_scan_host(shost);
3802 mpi3mr_cleanup_ioc(mrioc, MPI3MR_COMPLETE_CLEANUP);
3804 destroy_workqueue(mrioc->fwevt_worker_thread);
3805 out_fwevtthread_failed:
3806 spin_lock(&mrioc_list_lock);
3807 list_del(&mrioc->list);
3808 spin_unlock(&mrioc_list_lock);
3809 scsi_host_put(shost);
3815 * mpi3mr_remove - PCI remove callback
3816 * @pdev: PCI device instance
3818 * Free up all memory and resources associated with the
3819 * controllerand target devices, unregister the shost.
3823 static void mpi3mr_remove(struct pci_dev *pdev)
3825 struct Scsi_Host *shost = pci_get_drvdata(pdev);
3826 struct mpi3mr_ioc *mrioc;
3827 struct workqueue_struct *wq;
3828 unsigned long flags;
3829 struct mpi3mr_tgt_dev *tgtdev, *tgtdev_next;
3834 mrioc = shost_priv(shost);
3835 while (mrioc->reset_in_progress || mrioc->is_driver_loading)
3838 mrioc->stop_drv_processing = 1;
3839 mpi3mr_cleanup_fwevt_list(mrioc);
3840 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
3841 wq = mrioc->fwevt_worker_thread;
3842 mrioc->fwevt_worker_thread = NULL;
3843 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
3845 destroy_workqueue(wq);
3846 scsi_remove_host(shost);
3848 list_for_each_entry_safe(tgtdev, tgtdev_next, &mrioc->tgtdev_list,
3850 mpi3mr_remove_tgtdev_from_host(mrioc, tgtdev);
3851 mpi3mr_tgtdev_del_from_list(mrioc, tgtdev);
3852 mpi3mr_tgtdev_put(tgtdev);
3854 mpi3mr_cleanup_ioc(mrioc, MPI3MR_COMPLETE_CLEANUP);
3856 spin_lock(&mrioc_list_lock);
3857 list_del(&mrioc->list);
3858 spin_unlock(&mrioc_list_lock);
3860 scsi_host_put(shost);
3864 * mpi3mr_shutdown - PCI shutdown callback
3865 * @pdev: PCI device instance
3867 * Free up all memory and resources associated with the
3872 static void mpi3mr_shutdown(struct pci_dev *pdev)
3874 struct Scsi_Host *shost = pci_get_drvdata(pdev);
3875 struct mpi3mr_ioc *mrioc;
3876 struct workqueue_struct *wq;
3877 unsigned long flags;
3882 mrioc = shost_priv(shost);
3883 while (mrioc->reset_in_progress || mrioc->is_driver_loading)
3886 mrioc->stop_drv_processing = 1;
3887 mpi3mr_cleanup_fwevt_list(mrioc);
3888 spin_lock_irqsave(&mrioc->fwevt_lock, flags);
3889 wq = mrioc->fwevt_worker_thread;
3890 mrioc->fwevt_worker_thread = NULL;
3891 spin_unlock_irqrestore(&mrioc->fwevt_lock, flags);
3893 destroy_workqueue(wq);
3894 mpi3mr_cleanup_ioc(mrioc, MPI3MR_COMPLETE_CLEANUP);
3899 * mpi3mr_suspend - PCI power management suspend callback
3900 * @pdev: PCI device instance
3901 * @state: New power state
3903 * Change the power state to the given value and cleanup the IOC
3904 * by issuing MUR and shutdown notification
3908 static int mpi3mr_suspend(struct pci_dev *pdev, pm_message_t state)
3910 struct Scsi_Host *shost = pci_get_drvdata(pdev);
3911 struct mpi3mr_ioc *mrioc;
3912 pci_power_t device_state;
3917 mrioc = shost_priv(shost);
3918 while (mrioc->reset_in_progress || mrioc->is_driver_loading)
3920 mrioc->stop_drv_processing = 1;
3921 mpi3mr_cleanup_fwevt_list(mrioc);
3922 scsi_block_requests(shost);
3923 mpi3mr_stop_watchdog(mrioc);
3924 mpi3mr_cleanup_ioc(mrioc, MPI3MR_SUSPEND);
3926 device_state = pci_choose_state(pdev, state);
3927 ioc_info(mrioc, "pdev=0x%p, slot=%s, entering operating state [D%d]\n",
3928 pdev, pci_name(pdev), device_state);
3929 pci_save_state(pdev);
3930 pci_set_power_state(pdev, device_state);
3931 mpi3mr_cleanup_resources(mrioc);
3937 * mpi3mr_resume - PCI power management resume callback
3938 * @pdev: PCI device instance
3940 * Restore the power state to D0 and reinitialize the controller
3941 * and resume I/O operations to the target devices
3943 * Return: 0 on success, non-zero on failure
3945 static int mpi3mr_resume(struct pci_dev *pdev)
3947 struct Scsi_Host *shost = pci_get_drvdata(pdev);
3948 struct mpi3mr_ioc *mrioc;
3949 pci_power_t device_state = pdev->current_state;
3955 mrioc = shost_priv(shost);
3957 ioc_info(mrioc, "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
3958 pdev, pci_name(pdev), device_state);
3959 pci_set_power_state(pdev, PCI_D0);
3960 pci_enable_wake(pdev, PCI_D0, 0);
3961 pci_restore_state(pdev);
3963 mrioc->cpu_count = num_online_cpus();
3964 r = mpi3mr_setup_resources(mrioc);
3966 ioc_info(mrioc, "%s: Setup resources failed[%d]\n",
3971 mrioc->stop_drv_processing = 0;
3972 mpi3mr_memset_buffers(mrioc);
3973 mpi3mr_init_ioc(mrioc, MPI3MR_IT_RESUME);
3974 scsi_unblock_requests(shost);
3975 mpi3mr_start_watchdog(mrioc);
3981 static const struct pci_device_id mpi3mr_pci_id_table[] = {
3983 PCI_DEVICE_SUB(PCI_VENDOR_ID_LSI_LOGIC, 0x00A5,
3984 PCI_ANY_ID, PCI_ANY_ID)
3988 MODULE_DEVICE_TABLE(pci, mpi3mr_pci_id_table);
3990 static struct pci_driver mpi3mr_pci_driver = {
3991 .name = MPI3MR_DRIVER_NAME,
3992 .id_table = mpi3mr_pci_id_table,
3993 .probe = mpi3mr_probe,
3994 .remove = mpi3mr_remove,
3995 .shutdown = mpi3mr_shutdown,
3997 .suspend = mpi3mr_suspend,
3998 .resume = mpi3mr_resume,
4002 static int __init mpi3mr_init(void)
4006 pr_info("Loading %s version %s\n", MPI3MR_DRIVER_NAME,
4007 MPI3MR_DRIVER_VERSION);
4009 ret_val = pci_register_driver(&mpi3mr_pci_driver);
4014 static void __exit mpi3mr_exit(void)
4016 if (warn_non_secure_ctlr)
4018 "Unloading %s version %s while managing a non secure controller\n",
4019 MPI3MR_DRIVER_NAME, MPI3MR_DRIVER_VERSION);
4021 pr_info("Unloading %s version %s\n", MPI3MR_DRIVER_NAME,
4022 MPI3MR_DRIVER_VERSION);
4024 pci_unregister_driver(&mpi3mr_pci_driver);
4027 module_init(mpi3mr_init);
4028 module_exit(mpi3mr_exit);