Lines Matching +full:regulator +full:- +full:booster
1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2011-2013 Samsung India Software Operations
5 * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
17 #include <linux/blk-pm.h>
23 #include <linux/regulator/consumer.h>
30 #include "ufshcd-priv.h"
33 #include "ufs-sysfs.h"
34 #include "ufs-debugfs.h"
35 #include "ufs-fault-injection.h"
37 #include "ufshcd-crypto.h"
74 /* maximum number of link-startup retries */
109 return hba->mcq_sup && use_mcq_mode; in is_mcq_supported()
113 …mode, "Control MCQ mode for controllers starting from UFSHCI 4.0. 1 - enable MCQ, 0 - disable MCQ.…
139 return -EINVAL; in ufshcd_dump_regs()
143 return -ENOMEM; in ufshcd_dump_regs()
163 UFSHCD_CMD_PER_LUN = 32 - UFSHCD_NUM_RESERVED,
164 UFSHCD_CAN_QUEUE = 32 - UFSHCD_NUM_RESERVED,
192 ((h)->eh_flags |= UFSHCD_EH_IN_PROGRESS)
194 ((h)->eh_flags & UFSHCD_EH_IN_PROGRESS)
196 ((h)->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
242 return hba->outstanding_tasks || hba->active_uic_cmd || in ufshcd_has_pending_tasks()
243 hba->uic_async_done; in ufshcd_has_pending_tasks()
248 return scsi_host_busy(hba->host) || ufshcd_has_pending_tasks(hba); in ufshcd_is_ufs_dev_busy()
307 if (!hba->is_irq_enabled) { in ufshcd_enable_irq()
308 enable_irq(hba->irq); in ufshcd_enable_irq()
309 hba->is_irq_enabled = true; in ufshcd_enable_irq()
315 if (hba->is_irq_enabled) { in ufshcd_disable_irq()
316 disable_irq(hba->irq); in ufshcd_disable_irq()
317 hba->is_irq_enabled = false; in ufshcd_disable_irq()
336 if (atomic_dec_and_test(&hba->scsi_block_reqs_cnt)) in ufshcd_scsi_unblock_requests()
337 scsi_unblock_requests(hba->host); in ufshcd_scsi_unblock_requests()
342 if (atomic_inc_return(&hba->scsi_block_reqs_cnt) == 1) in ufshcd_scsi_block_requests()
343 scsi_block_requests(hba->host); in ufshcd_scsi_block_requests()
349 struct utp_upiu_req *rq = hba->lrb[tag].ucd_req_ptr; in ufshcd_add_cmd_upiu_trace()
356 header = &rq->header; in ufshcd_add_cmd_upiu_trace()
358 header = &hba->lrb[tag].ucd_rsp_ptr->header; in ufshcd_add_cmd_upiu_trace()
360 trace_ufshcd_upiu(dev_name(hba->dev), str_t, header, &rq->sc.cdb, in ufshcd_add_cmd_upiu_trace()
371 trace_ufshcd_upiu(dev_name(hba->dev), str_t, &rq_rsp->header, in ufshcd_add_query_upiu_trace()
372 &rq_rsp->qr, UFS_TSF_OSF); in ufshcd_add_query_upiu_trace()
378 struct utp_task_req_desc *descp = &hba->utmrdl_base_addr[tag]; in ufshcd_add_tm_upiu_trace()
384 trace_ufshcd_upiu(dev_name(hba->dev), str_t, in ufshcd_add_tm_upiu_trace()
385 &descp->upiu_req.req_header, in ufshcd_add_tm_upiu_trace()
386 &descp->upiu_req.input_param1, in ufshcd_add_tm_upiu_trace()
389 trace_ufshcd_upiu(dev_name(hba->dev), str_t, in ufshcd_add_tm_upiu_trace()
390 &descp->upiu_rsp.rsp_header, in ufshcd_add_tm_upiu_trace()
391 &descp->upiu_rsp.output_param1, in ufshcd_add_tm_upiu_trace()
405 cmd = ucmd->command; in ufshcd_add_uic_command_trace()
409 trace_ufshcd_uic_command(dev_name(hba->dev), str_t, cmd, in ufshcd_add_uic_command_trace()
422 int hwq_id = -1; in ufshcd_add_command_trace()
423 struct ufshcd_lrb *lrbp = &hba->lrb[tag]; in ufshcd_add_command_trace()
424 struct scsi_cmnd *cmd = lrbp->cmd; in ufshcd_add_command_trace()
426 int transfer_len = -1; in ufshcd_add_command_trace()
436 opcode = cmd->cmnd[0]; in ufshcd_add_command_trace()
443 be32_to_cpu(lrbp->ucd_req_ptr->sc.exp_data_transfer_len); in ufshcd_add_command_trace()
446 group_id = lrbp->cmd->cmnd[6]; in ufshcd_add_command_trace()
460 hwq_id = hwq->id; in ufshcd_add_command_trace()
464 trace_ufshcd_command(dev_name(hba->dev), str_t, tag, in ufshcd_add_command_trace()
471 struct list_head *head = &hba->clk_list_head; in ufshcd_print_clk_freqs()
477 if (!IS_ERR_OR_NULL(clki->clk) && clki->min_freq && in ufshcd_print_clk_freqs()
478 clki->max_freq) in ufshcd_print_clk_freqs()
479 dev_err(hba->dev, "clk: %s, rate: %u\n", in ufshcd_print_clk_freqs()
480 clki->name, clki->curr_freq); in ufshcd_print_clk_freqs()
494 e = &hba->ufs_stats.event[id]; in ufshcd_print_evt()
497 int p = (i + e->pos) % UFS_EVENT_HIST_LENGTH; in ufshcd_print_evt()
499 if (e->tstamp[p] == 0) in ufshcd_print_evt()
501 dev_err(hba->dev, "%s[%d] = 0x%x at %lld us\n", err_name, p, in ufshcd_print_evt()
502 e->val[p], div_u64(e->tstamp[p], 1000)); in ufshcd_print_evt()
507 dev_err(hba->dev, "No record of %s\n", err_name); in ufshcd_print_evt()
509 dev_err(hba->dev, "%s: total cnt=%llu\n", err_name, e->cnt); in ufshcd_print_evt()
545 lrbp = &hba->lrb[tag]; in ufshcd_print_tr()
547 dev_err(hba->dev, "UPIU[%d] - issue time %lld us\n", in ufshcd_print_tr()
548 tag, div_u64(lrbp->issue_time_stamp_local_clock, 1000)); in ufshcd_print_tr()
549 dev_err(hba->dev, "UPIU[%d] - complete time %lld us\n", in ufshcd_print_tr()
550 tag, div_u64(lrbp->compl_time_stamp_local_clock, 1000)); in ufshcd_print_tr()
551 dev_err(hba->dev, in ufshcd_print_tr()
552 "UPIU[%d] - Transfer Request Descriptor phys@0x%llx\n", in ufshcd_print_tr()
553 tag, (u64)lrbp->utrd_dma_addr); in ufshcd_print_tr()
555 ufshcd_hex_dump("UPIU TRD: ", lrbp->utr_descriptor_ptr, in ufshcd_print_tr()
557 dev_err(hba->dev, "UPIU[%d] - Request UPIU phys@0x%llx\n", tag, in ufshcd_print_tr()
558 (u64)lrbp->ucd_req_dma_addr); in ufshcd_print_tr()
559 ufshcd_hex_dump("UPIU REQ: ", lrbp->ucd_req_ptr, in ufshcd_print_tr()
561 dev_err(hba->dev, "UPIU[%d] - Response UPIU phys@0x%llx\n", tag, in ufshcd_print_tr()
562 (u64)lrbp->ucd_rsp_dma_addr); in ufshcd_print_tr()
563 ufshcd_hex_dump("UPIU RSP: ", lrbp->ucd_rsp_ptr, in ufshcd_print_tr()
567 lrbp->utr_descriptor_ptr->prd_table_length); in ufshcd_print_tr()
568 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) in ufshcd_print_tr()
571 dev_err(hba->dev, in ufshcd_print_tr()
572 "UPIU[%d] - PRDT - %d entries phys@0x%llx\n", in ufshcd_print_tr()
574 (u64)lrbp->ucd_prdt_dma_addr); in ufshcd_print_tr()
577 ufshcd_hex_dump("UPIU PRDT: ", lrbp->ucd_prdt_ptr, in ufshcd_print_tr()
583 struct scsi_device *sdev = req->q->queuedata; in ufshcd_print_tr_iter()
584 struct Scsi_Host *shost = sdev->host; in ufshcd_print_tr_iter()
587 ufshcd_print_tr(hba, req->tag, *(bool *)priv); in ufshcd_print_tr_iter()
593 * ufshcd_print_trs_all - print trs for all started requests.
594 * @hba: per-adapter instance.
599 blk_mq_tagset_busy_iter(&hba->host->tag_set, ufshcd_print_tr_iter, &pr_prdt); in ufshcd_print_trs_all()
606 for_each_set_bit(tag, &bitmap, hba->nutmrs) { in ufshcd_print_tmrs()
607 struct utp_task_req_desc *tmrdp = &hba->utmrdl_base_addr[tag]; in ufshcd_print_tmrs()
609 dev_err(hba->dev, "TM[%d] - Task Management Header\n", tag); in ufshcd_print_tmrs()
616 const struct scsi_device *sdev_ufs = hba->ufs_device_wlun; in ufshcd_print_host_state()
618 dev_err(hba->dev, "UFS Host state=%d\n", hba->ufshcd_state); in ufshcd_print_host_state()
619 dev_err(hba->dev, "%d outstanding reqs, tasks=0x%lx\n", in ufshcd_print_host_state()
620 scsi_host_busy(hba->host), hba->outstanding_tasks); in ufshcd_print_host_state()
621 dev_err(hba->dev, "saved_err=0x%x, saved_uic_err=0x%x\n", in ufshcd_print_host_state()
622 hba->saved_err, hba->saved_uic_err); in ufshcd_print_host_state()
623 dev_err(hba->dev, "Device power mode=%d, UIC link state=%d\n", in ufshcd_print_host_state()
624 hba->curr_dev_pwr_mode, hba->uic_link_state); in ufshcd_print_host_state()
625 dev_err(hba->dev, "PM in progress=%d, sys. suspended=%d\n", in ufshcd_print_host_state()
626 hba->pm_op_in_progress, hba->is_sys_suspended); in ufshcd_print_host_state()
627 dev_err(hba->dev, "Auto BKOPS=%d, Host self-block=%d\n", in ufshcd_print_host_state()
628 hba->auto_bkops_enabled, hba->host->host_self_blocked); in ufshcd_print_host_state()
629 dev_err(hba->dev, "Clk gate=%d\n", hba->clk_gating.state); in ufshcd_print_host_state()
630 dev_err(hba->dev, in ufshcd_print_host_state()
632 div_u64(hba->ufs_stats.last_hibern8_exit_tstamp, 1000), in ufshcd_print_host_state()
633 hba->ufs_stats.hibern8_exit_cnt); in ufshcd_print_host_state()
634 dev_err(hba->dev, "last intr at %lld us, last intr status=0x%x\n", in ufshcd_print_host_state()
635 div_u64(hba->ufs_stats.last_intr_ts, 1000), in ufshcd_print_host_state()
636 hba->ufs_stats.last_intr_status); in ufshcd_print_host_state()
637 dev_err(hba->dev, "error handling flags=0x%x, req. abort count=%d\n", in ufshcd_print_host_state()
638 hba->eh_flags, hba->req_abort_count); in ufshcd_print_host_state()
639 dev_err(hba->dev, "hba->ufs_version=0x%x, Host capabilities=0x%x, caps=0x%x\n", in ufshcd_print_host_state()
640 hba->ufs_version, hba->capabilities, hba->caps); in ufshcd_print_host_state()
641 dev_err(hba->dev, "quirks=0x%x, dev. quirks=0x%x\n", hba->quirks, in ufshcd_print_host_state()
642 hba->dev_quirks); in ufshcd_print_host_state()
644 dev_err(hba->dev, "UFS dev info: %.8s %.16s rev %.4s\n", in ufshcd_print_host_state()
645 sdev_ufs->vendor, sdev_ufs->model, sdev_ufs->rev); in ufshcd_print_host_state()
651 * ufshcd_print_pwr_info - print power params as saved in hba
653 * @hba: per-adapter instance
669 * never-ending cycles of messages written back to storage by user space in ufshcd_print_pwr_info()
672 dev_dbg(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n", in ufshcd_print_pwr_info()
674 hba->pwr_info.gear_rx, hba->pwr_info.gear_tx, in ufshcd_print_pwr_info()
675 hba->pwr_info.lane_rx, hba->pwr_info.lane_tx, in ufshcd_print_pwr_info()
676 names[hba->pwr_info.pwr_rx], in ufshcd_print_pwr_info()
677 names[hba->pwr_info.pwr_tx], in ufshcd_print_pwr_info()
678 hba->pwr_info.hs_rate); in ufshcd_print_pwr_info()
690 hba->dev_info.wb_enabled = false; in ufshcd_device_reset()
691 hba->dev_info.wb_buf_flush_enabled = false; in ufshcd_device_reset()
693 if (hba->dev_info.rtc_type == UFS_RTC_RELATIVE) in ufshcd_device_reset()
694 hba->dev_info.rtc_time_baseline = 0; in ufshcd_device_reset()
696 if (err != -EOPNOTSUPP) in ufshcd_device_reset()
713 * ufshcd_wait_for_register - wait for register value to change
714 * @hba: per-adapter interface
721 * Return: -ETIMEDOUT on error, zero on success.
737 err = -ETIMEDOUT; in ufshcd_wait_for_register()
746 * ufshcd_get_intr_mask - Get the interrupt bit mask
753 if (hba->ufs_version == ufshci_version(1, 0)) in ufshcd_get_intr_mask()
755 if (hba->ufs_version <= ufshci_version(2, 0)) in ufshcd_get_intr_mask()
762 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
771 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION) in ufshcd_get_ufs_version()
788 * ufshcd_is_device_present - Check if any device connected to
800 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
812 return le32_to_cpu(cqe->status) & MASK_OCS; in ufshcd_get_tr_ocs()
814 return lrbp->utr_descriptor_ptr->header.ocs & MASK_OCS; in ufshcd_get_tr_ocs()
818 * ufshcd_utrl_clear() - Clear requests from the controller request list.
824 if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR) in ufshcd_utrl_clear()
843 * ufshcd_utmrl_clear - Clear a bit in UTMRLCLR register
849 if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR) in ufshcd_utmrl_clear()
856 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
867 * ufshcd_get_uic_cmd_result - Get the UIC command result
872 * Return: 0 on success; non-zero value on error.
881 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
886 * Return: 0 on success; non-zero value on error.
894 * ufshcd_get_req_rsp - returns the TR response transaction type
902 return ucd_rsp_ptr->header.transaction_code; in ufshcd_get_req_rsp()
906 * ufshcd_is_exception_event - Check if the device raised an exception event
916 return ucd_rsp_ptr->header.device_information & 1; in ufshcd_is_exception_event()
920 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
932 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
947 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
956 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
957 * When run-stop registers are set to 1, it indicates the
970 * ufshcd_hba_start - Start controller initialization sequence
984 * ufshcd_is_hba_active - Get controller state
998 if (hba->ufs_version <= ufshci_version(1, 1)) in ufshcd_get_local_unipro_ver()
1020 * ufshcd_set_clk_freq - set UFS controller clock frequencies
1030 struct list_head *head = &hba->clk_list_head; in ufshcd_set_clk_freq()
1036 if (!IS_ERR_OR_NULL(clki->clk)) { in ufshcd_set_clk_freq()
1037 if (scale_up && clki->max_freq) { in ufshcd_set_clk_freq()
1038 if (clki->curr_freq == clki->max_freq) in ufshcd_set_clk_freq()
1041 ret = clk_set_rate(clki->clk, clki->max_freq); in ufshcd_set_clk_freq()
1043 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", in ufshcd_set_clk_freq()
1044 __func__, clki->name, in ufshcd_set_clk_freq()
1045 clki->max_freq, ret); in ufshcd_set_clk_freq()
1048 trace_ufshcd_clk_scaling(dev_name(hba->dev), in ufshcd_set_clk_freq()
1049 "scaled up", clki->name, in ufshcd_set_clk_freq()
1050 clki->curr_freq, in ufshcd_set_clk_freq()
1051 clki->max_freq); in ufshcd_set_clk_freq()
1053 clki->curr_freq = clki->max_freq; in ufshcd_set_clk_freq()
1055 } else if (!scale_up && clki->min_freq) { in ufshcd_set_clk_freq()
1056 if (clki->curr_freq == clki->min_freq) in ufshcd_set_clk_freq()
1059 ret = clk_set_rate(clki->clk, clki->min_freq); in ufshcd_set_clk_freq()
1061 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", in ufshcd_set_clk_freq()
1062 __func__, clki->name, in ufshcd_set_clk_freq()
1063 clki->min_freq, ret); in ufshcd_set_clk_freq()
1066 trace_ufshcd_clk_scaling(dev_name(hba->dev), in ufshcd_set_clk_freq()
1067 "scaled down", clki->name, in ufshcd_set_clk_freq()
1068 clki->curr_freq, in ufshcd_set_clk_freq()
1069 clki->min_freq); in ufshcd_set_clk_freq()
1070 clki->curr_freq = clki->min_freq; in ufshcd_set_clk_freq()
1073 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__, in ufshcd_set_clk_freq()
1074 clki->name, clk_get_rate(clki->clk)); in ufshcd_set_clk_freq()
1082 * ufshcd_scale_clks - scale up or scale down UFS controller clocks
1106 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev), in ufshcd_scale_clks()
1113 * ufshcd_is_devfreq_scaling_required - check if scaling is required or not
1123 struct list_head *head = &hba->clk_list_head; in ufshcd_is_devfreq_scaling_required()
1129 if (!IS_ERR_OR_NULL(clki->clk)) { in ufshcd_is_devfreq_scaling_required()
1130 if (scale_up && clki->max_freq) { in ufshcd_is_devfreq_scaling_required()
1131 if (clki->curr_freq == clki->max_freq) in ufshcd_is_devfreq_scaling_required()
1134 } else if (!scale_up && clki->min_freq) { in ufshcd_is_devfreq_scaling_required()
1135 if (clki->curr_freq == clki->min_freq) in ufshcd_is_devfreq_scaling_required()
1157 lockdep_assert_held(hba->host->host_lock); in ufshcd_pending_cmds()
1158 __shost_for_each_device(sdev, hba->host) in ufshcd_pending_cmds()
1159 pending += sbitmap_weight(&sdev->budget_map); in ufshcd_pending_cmds()
1168 * Return: 0 upon success; -EBUSY upon timeout.
1181 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_wait_for_doorbell_clr()
1188 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) { in ufshcd_wait_for_doorbell_clr()
1189 ret = -EBUSY; in ufshcd_wait_for_doorbell_clr()
1202 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_wait_for_doorbell_clr()
1214 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_wait_for_doorbell_clr()
1218 dev_err(hba->dev, in ufshcd_wait_for_doorbell_clr()
1221 ret = -EBUSY; in ufshcd_wait_for_doorbell_clr()
1224 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_wait_for_doorbell_clr()
1230 * ufshcd_scale_gear - scale up/down UFS gear
1234 * Return: 0 for success; -EBUSY if scaling can't happen at this time;
1235 * non-zero for any other errors.
1243 memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info, in ufshcd_scale_gear()
1246 memcpy(&new_pwr_info, &hba->pwr_info, in ufshcd_scale_gear()
1249 if (hba->pwr_info.gear_tx > hba->clk_scaling.min_gear || in ufshcd_scale_gear()
1250 hba->pwr_info.gear_rx > hba->clk_scaling.min_gear) { in ufshcd_scale_gear()
1252 memcpy(&hba->clk_scaling.saved_pwr_info, in ufshcd_scale_gear()
1253 &hba->pwr_info, in ufshcd_scale_gear()
1257 new_pwr_info.gear_tx = hba->clk_scaling.min_gear; in ufshcd_scale_gear()
1258 new_pwr_info.gear_rx = hba->clk_scaling.min_gear; in ufshcd_scale_gear()
1265 dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d)", in ufshcd_scale_gear()
1267 hba->pwr_info.gear_tx, hba->pwr_info.gear_rx, in ufshcd_scale_gear()
1277 * Return: 0 upon success; -EBUSY upon timeout.
1286 blk_mq_quiesce_tagset(&hba->host->tag_set); in ufshcd_clock_scaling_prepare()
1287 mutex_lock(&hba->wb_mutex); in ufshcd_clock_scaling_prepare()
1288 down_write(&hba->clk_scaling_lock); in ufshcd_clock_scaling_prepare()
1290 if (!hba->clk_scaling.is_allowed || in ufshcd_clock_scaling_prepare()
1292 ret = -EBUSY; in ufshcd_clock_scaling_prepare()
1293 up_write(&hba->clk_scaling_lock); in ufshcd_clock_scaling_prepare()
1294 mutex_unlock(&hba->wb_mutex); in ufshcd_clock_scaling_prepare()
1295 blk_mq_unquiesce_tagset(&hba->host->tag_set); in ufshcd_clock_scaling_prepare()
1308 up_write(&hba->clk_scaling_lock); in ufshcd_clock_scaling_unprepare()
1310 /* Enable Write Booster if we have scaled up else disable it */ in ufshcd_clock_scaling_unprepare()
1314 mutex_unlock(&hba->wb_mutex); in ufshcd_clock_scaling_unprepare()
1316 blk_mq_unquiesce_tagset(&hba->host->tag_set); in ufshcd_clock_scaling_unprepare()
1321 * ufshcd_devfreq_scale - scale up/down UFS clocks and gear
1325 * Return: 0 for success; -EBUSY if scaling can't happen at this time; non-zero
1370 spin_lock_irqsave(hba->host->host_lock, irq_flags); in ufshcd_clk_scaling_suspend_work()
1371 if (hba->clk_scaling.active_reqs || hba->clk_scaling.is_suspended) { in ufshcd_clk_scaling_suspend_work()
1372 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); in ufshcd_clk_scaling_suspend_work()
1375 hba->clk_scaling.is_suspended = true; in ufshcd_clk_scaling_suspend_work()
1376 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); in ufshcd_clk_scaling_suspend_work()
1387 spin_lock_irqsave(hba->host->host_lock, irq_flags); in ufshcd_clk_scaling_resume_work()
1388 if (!hba->clk_scaling.is_suspended) { in ufshcd_clk_scaling_resume_work()
1389 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); in ufshcd_clk_scaling_resume_work()
1392 hba->clk_scaling.is_suspended = false; in ufshcd_clk_scaling_resume_work()
1393 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); in ufshcd_clk_scaling_resume_work()
1395 devfreq_resume_device(hba->devfreq); in ufshcd_clk_scaling_resume_work()
1405 struct list_head *clk_list = &hba->clk_list_head; in ufshcd_devfreq_target()
1410 return -EINVAL; in ufshcd_devfreq_target()
1412 clki = list_first_entry(&hba->clk_list_head, struct ufs_clk_info, list); in ufshcd_devfreq_target()
1414 *freq = (unsigned long) clk_round_rate(clki->clk, *freq); in ufshcd_devfreq_target()
1415 spin_lock_irqsave(hba->host->host_lock, irq_flags); in ufshcd_devfreq_target()
1417 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); in ufshcd_devfreq_target()
1421 if (!hba->clk_scaling.active_reqs) in ufshcd_devfreq_target()
1425 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); in ufshcd_devfreq_target()
1429 /* Decide based on the rounded-off frequency and update */ in ufshcd_devfreq_target()
1430 scale_up = *freq == clki->max_freq; in ufshcd_devfreq_target()
1432 *freq = clki->min_freq; in ufshcd_devfreq_target()
1435 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); in ufshcd_devfreq_target()
1439 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); in ufshcd_devfreq_target()
1444 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev), in ufshcd_devfreq_target()
1450 queue_work(hba->clk_scaling.workq, in ufshcd_devfreq_target()
1451 &hba->clk_scaling.suspend_work); in ufshcd_devfreq_target()
1460 struct ufs_clk_scaling *scaling = &hba->clk_scaling; in ufshcd_devfreq_get_dev_status()
1462 struct list_head *clk_list = &hba->clk_list_head; in ufshcd_devfreq_get_dev_status()
1467 return -EINVAL; in ufshcd_devfreq_get_dev_status()
1471 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_devfreq_get_dev_status()
1473 if (!scaling->window_start_t) in ufshcd_devfreq_get_dev_status()
1482 stat->current_frequency = clki->curr_freq; in ufshcd_devfreq_get_dev_status()
1483 if (scaling->is_busy_started) in ufshcd_devfreq_get_dev_status()
1484 scaling->tot_busy_t += ktime_us_delta(curr_t, in ufshcd_devfreq_get_dev_status()
1485 scaling->busy_start_t); in ufshcd_devfreq_get_dev_status()
1487 stat->total_time = ktime_us_delta(curr_t, scaling->window_start_t); in ufshcd_devfreq_get_dev_status()
1488 stat->busy_time = scaling->tot_busy_t; in ufshcd_devfreq_get_dev_status()
1490 scaling->window_start_t = curr_t; in ufshcd_devfreq_get_dev_status()
1491 scaling->tot_busy_t = 0; in ufshcd_devfreq_get_dev_status()
1493 if (scaling->active_reqs) { in ufshcd_devfreq_get_dev_status()
1494 scaling->busy_start_t = curr_t; in ufshcd_devfreq_get_dev_status()
1495 scaling->is_busy_started = true; in ufshcd_devfreq_get_dev_status()
1497 scaling->busy_start_t = 0; in ufshcd_devfreq_get_dev_status()
1498 scaling->is_busy_started = false; in ufshcd_devfreq_get_dev_status()
1500 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_devfreq_get_dev_status()
1506 struct list_head *clk_list = &hba->clk_list_head; in ufshcd_devfreq_init()
1516 dev_pm_opp_add(hba->dev, clki->min_freq, 0); in ufshcd_devfreq_init()
1517 dev_pm_opp_add(hba->dev, clki->max_freq, 0); in ufshcd_devfreq_init()
1519 ufshcd_vops_config_scaling_param(hba, &hba->vps->devfreq_profile, in ufshcd_devfreq_init()
1520 &hba->vps->ondemand_data); in ufshcd_devfreq_init()
1521 devfreq = devfreq_add_device(hba->dev, in ufshcd_devfreq_init()
1522 &hba->vps->devfreq_profile, in ufshcd_devfreq_init()
1524 &hba->vps->ondemand_data); in ufshcd_devfreq_init()
1527 dev_err(hba->dev, "Unable to register with devfreq %d\n", ret); in ufshcd_devfreq_init()
1529 dev_pm_opp_remove(hba->dev, clki->min_freq); in ufshcd_devfreq_init()
1530 dev_pm_opp_remove(hba->dev, clki->max_freq); in ufshcd_devfreq_init()
1534 hba->devfreq = devfreq; in ufshcd_devfreq_init()
1541 struct list_head *clk_list = &hba->clk_list_head; in ufshcd_devfreq_remove()
1544 if (!hba->devfreq) in ufshcd_devfreq_remove()
1547 devfreq_remove_device(hba->devfreq); in ufshcd_devfreq_remove()
1548 hba->devfreq = NULL; in ufshcd_devfreq_remove()
1551 dev_pm_opp_remove(hba->dev, clki->min_freq); in ufshcd_devfreq_remove()
1552 dev_pm_opp_remove(hba->dev, clki->max_freq); in ufshcd_devfreq_remove()
1559 devfreq_suspend_device(hba->devfreq); in __ufshcd_suspend_clkscaling()
1560 spin_lock_irqsave(hba->host->host_lock, flags); in __ufshcd_suspend_clkscaling()
1561 hba->clk_scaling.window_start_t = 0; in __ufshcd_suspend_clkscaling()
1562 spin_unlock_irqrestore(hba->host->host_lock, flags); in __ufshcd_suspend_clkscaling()
1570 cancel_work_sync(&hba->clk_scaling.suspend_work); in ufshcd_suspend_clkscaling()
1571 cancel_work_sync(&hba->clk_scaling.resume_work); in ufshcd_suspend_clkscaling()
1573 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_suspend_clkscaling()
1574 if (!hba->clk_scaling.is_suspended) { in ufshcd_suspend_clkscaling()
1576 hba->clk_scaling.is_suspended = true; in ufshcd_suspend_clkscaling()
1578 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_suspend_clkscaling()
1589 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_resume_clkscaling()
1590 if (hba->clk_scaling.is_suspended) { in ufshcd_resume_clkscaling()
1592 hba->clk_scaling.is_suspended = false; in ufshcd_resume_clkscaling()
1594 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_resume_clkscaling()
1597 devfreq_resume_device(hba->devfreq); in ufshcd_resume_clkscaling()
1605 return sysfs_emit(buf, "%d\n", hba->clk_scaling.is_enabled); in ufshcd_clkscale_enable_show()
1616 return -EINVAL; in ufshcd_clkscale_enable_store()
1618 down(&hba->host_sem); in ufshcd_clkscale_enable_store()
1620 err = -EBUSY; in ufshcd_clkscale_enable_store()
1625 if (value == hba->clk_scaling.is_enabled) in ufshcd_clkscale_enable_store()
1631 hba->clk_scaling.is_enabled = value; in ufshcd_clkscale_enable_store()
1639 dev_err(hba->dev, "%s: failed to scale clocks up %d\n", in ufshcd_clkscale_enable_store()
1646 up(&hba->host_sem); in ufshcd_clkscale_enable_store()
1652 hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show; in ufshcd_init_clk_scaling_sysfs()
1653 hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store; in ufshcd_init_clk_scaling_sysfs()
1654 sysfs_attr_init(&hba->clk_scaling.enable_attr.attr); in ufshcd_init_clk_scaling_sysfs()
1655 hba->clk_scaling.enable_attr.attr.name = "clkscale_enable"; in ufshcd_init_clk_scaling_sysfs()
1656 hba->clk_scaling.enable_attr.attr.mode = 0644; in ufshcd_init_clk_scaling_sysfs()
1657 if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr)) in ufshcd_init_clk_scaling_sysfs()
1658 dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n"); in ufshcd_init_clk_scaling_sysfs()
1663 if (hba->clk_scaling.enable_attr.attr.name) in ufshcd_remove_clk_scaling_sysfs()
1664 device_remove_file(hba->dev, &hba->clk_scaling.enable_attr); in ufshcd_remove_clk_scaling_sysfs()
1674 if (!hba->clk_scaling.min_gear) in ufshcd_init_clk_scaling()
1675 hba->clk_scaling.min_gear = UFS_HS_G1; in ufshcd_init_clk_scaling()
1677 INIT_WORK(&hba->clk_scaling.suspend_work, in ufshcd_init_clk_scaling()
1679 INIT_WORK(&hba->clk_scaling.resume_work, in ufshcd_init_clk_scaling()
1683 hba->host->host_no); in ufshcd_init_clk_scaling()
1684 hba->clk_scaling.workq = create_singlethread_workqueue(wq_name); in ufshcd_init_clk_scaling()
1686 hba->clk_scaling.is_initialized = true; in ufshcd_init_clk_scaling()
1691 if (!hba->clk_scaling.is_initialized) in ufshcd_exit_clk_scaling()
1695 destroy_workqueue(hba->clk_scaling.workq); in ufshcd_exit_clk_scaling()
1697 hba->clk_scaling.is_initialized = false; in ufshcd_exit_clk_scaling()
1707 cancel_delayed_work_sync(&hba->clk_gating.gate_work); in ufshcd_ungate_work()
1709 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_ungate_work()
1710 if (hba->clk_gating.state == CLKS_ON) { in ufshcd_ungate_work()
1711 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_ungate_work()
1715 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_ungate_work()
1724 hba->clk_gating.is_suspended = true; in ufshcd_ungate_work()
1728 dev_err(hba->dev, "%s: hibern8 exit failed %d\n", in ufshcd_ungate_work()
1733 hba->clk_gating.is_suspended = false; in ufshcd_ungate_work()
1738 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
1748 !hba->clk_gating.is_initialized) in ufshcd_hold()
1750 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_hold()
1751 hba->clk_gating.active_reqs++; in ufshcd_hold()
1754 switch (hba->clk_gating.state) { in ufshcd_hold()
1766 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_hold()
1767 flush_result = flush_work(&hba->clk_gating.ungate_work); in ufshcd_hold()
1768 if (hba->clk_gating.is_suspended && !flush_result) in ufshcd_hold()
1770 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_hold()
1775 if (cancel_delayed_work(&hba->clk_gating.gate_work)) { in ufshcd_hold()
1776 hba->clk_gating.state = CLKS_ON; in ufshcd_hold()
1777 trace_ufshcd_clk_gating(dev_name(hba->dev), in ufshcd_hold()
1778 hba->clk_gating.state); in ufshcd_hold()
1788 hba->clk_gating.state = REQ_CLKS_ON; in ufshcd_hold()
1789 trace_ufshcd_clk_gating(dev_name(hba->dev), in ufshcd_hold()
1790 hba->clk_gating.state); in ufshcd_hold()
1791 queue_work(hba->clk_gating.clk_gating_workq, in ufshcd_hold()
1792 &hba->clk_gating.ungate_work); in ufshcd_hold()
1799 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_hold()
1800 flush_work(&hba->clk_gating.ungate_work); in ufshcd_hold()
1802 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_hold()
1805 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n", in ufshcd_hold()
1806 __func__, hba->clk_gating.state); in ufshcd_hold()
1809 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_hold()
1820 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_gate_work()
1827 if (hba->clk_gating.is_suspended || in ufshcd_gate_work()
1828 (hba->clk_gating.state != REQ_CLKS_OFF)) { in ufshcd_gate_work()
1829 hba->clk_gating.state = CLKS_ON; in ufshcd_gate_work()
1830 trace_ufshcd_clk_gating(dev_name(hba->dev), in ufshcd_gate_work()
1831 hba->clk_gating.state); in ufshcd_gate_work()
1836 hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL || in ufshcd_gate_work()
1837 hba->clk_gating.active_reqs) in ufshcd_gate_work()
1840 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_gate_work()
1846 hba->clk_gating.state = CLKS_ON; in ufshcd_gate_work()
1847 dev_err(hba->dev, "%s: hibern8 enter failed %d\n", in ufshcd_gate_work()
1849 trace_ufshcd_clk_gating(dev_name(hba->dev), in ufshcd_gate_work()
1850 hba->clk_gating.state); in ufshcd_gate_work()
1871 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_gate_work()
1872 if (hba->clk_gating.state == REQ_CLKS_OFF) { in ufshcd_gate_work()
1873 hba->clk_gating.state = CLKS_OFF; in ufshcd_gate_work()
1874 trace_ufshcd_clk_gating(dev_name(hba->dev), in ufshcd_gate_work()
1875 hba->clk_gating.state); in ufshcd_gate_work()
1878 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_gate_work()
1889 hba->clk_gating.active_reqs--; in __ufshcd_release()
1891 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended || in __ufshcd_release()
1892 hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL || in __ufshcd_release()
1893 ufshcd_has_pending_tasks(hba) || !hba->clk_gating.is_initialized || in __ufshcd_release()
1894 hba->clk_gating.state == CLKS_OFF) in __ufshcd_release()
1897 hba->clk_gating.state = REQ_CLKS_OFF; in __ufshcd_release()
1898 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state); in __ufshcd_release()
1899 queue_delayed_work(hba->clk_gating.clk_gating_workq, in __ufshcd_release()
1900 &hba->clk_gating.gate_work, in __ufshcd_release()
1901 msecs_to_jiffies(hba->clk_gating.delay_ms)); in __ufshcd_release()
1908 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_release()
1910 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_release()
1919 return sysfs_emit(buf, "%lu\n", hba->clk_gating.delay_ms); in ufshcd_clkgate_delay_show()
1927 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_clkgate_delay_set()
1928 hba->clk_gating.delay_ms = value; in ufshcd_clkgate_delay_set()
1929 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_clkgate_delay_set()
1939 return -EINVAL; in ufshcd_clkgate_delay_store()
1950 return sysfs_emit(buf, "%d\n", hba->clk_gating.is_enabled); in ufshcd_clkgate_enable_show()
1961 return -EINVAL; in ufshcd_clkgate_enable_store()
1965 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_clkgate_enable_store()
1966 if (value == hba->clk_gating.is_enabled) in ufshcd_clkgate_enable_store()
1972 hba->clk_gating.active_reqs++; in ufshcd_clkgate_enable_store()
1974 hba->clk_gating.is_enabled = value; in ufshcd_clkgate_enable_store()
1976 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_clkgate_enable_store()
1982 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show; in ufshcd_init_clk_gating_sysfs()
1983 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store; in ufshcd_init_clk_gating_sysfs()
1984 sysfs_attr_init(&hba->clk_gating.delay_attr.attr); in ufshcd_init_clk_gating_sysfs()
1985 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms"; in ufshcd_init_clk_gating_sysfs()
1986 hba->clk_gating.delay_attr.attr.mode = 0644; in ufshcd_init_clk_gating_sysfs()
1987 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr)) in ufshcd_init_clk_gating_sysfs()
1988 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n"); in ufshcd_init_clk_gating_sysfs()
1990 hba->clk_gating.enable_attr.show = ufshcd_clkgate_enable_show; in ufshcd_init_clk_gating_sysfs()
1991 hba->clk_gating.enable_attr.store = ufshcd_clkgate_enable_store; in ufshcd_init_clk_gating_sysfs()
1992 sysfs_attr_init(&hba->clk_gating.enable_attr.attr); in ufshcd_init_clk_gating_sysfs()
1993 hba->clk_gating.enable_attr.attr.name = "clkgate_enable"; in ufshcd_init_clk_gating_sysfs()
1994 hba->clk_gating.enable_attr.attr.mode = 0644; in ufshcd_init_clk_gating_sysfs()
1995 if (device_create_file(hba->dev, &hba->clk_gating.enable_attr)) in ufshcd_init_clk_gating_sysfs()
1996 dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n"); in ufshcd_init_clk_gating_sysfs()
2001 if (hba->clk_gating.delay_attr.attr.name) in ufshcd_remove_clk_gating_sysfs()
2002 device_remove_file(hba->dev, &hba->clk_gating.delay_attr); in ufshcd_remove_clk_gating_sysfs()
2003 if (hba->clk_gating.enable_attr.attr.name) in ufshcd_remove_clk_gating_sysfs()
2004 device_remove_file(hba->dev, &hba->clk_gating.enable_attr); in ufshcd_remove_clk_gating_sysfs()
2014 hba->clk_gating.state = CLKS_ON; in ufshcd_init_clk_gating()
2016 hba->clk_gating.delay_ms = 150; in ufshcd_init_clk_gating()
2017 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work); in ufshcd_init_clk_gating()
2018 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work); in ufshcd_init_clk_gating()
2021 hba->host->host_no); in ufshcd_init_clk_gating()
2022 hba->clk_gating.clk_gating_workq = alloc_ordered_workqueue(wq_name, in ufshcd_init_clk_gating()
2027 hba->clk_gating.is_enabled = true; in ufshcd_init_clk_gating()
2028 hba->clk_gating.is_initialized = true; in ufshcd_init_clk_gating()
2033 if (!hba->clk_gating.is_initialized) in ufshcd_exit_clk_gating()
2040 hba->clk_gating.is_initialized = false; in ufshcd_exit_clk_gating()
2043 destroy_workqueue(hba->clk_gating.clk_gating_workq); in ufshcd_exit_clk_gating()
2055 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_clk_scaling_start_busy()
2056 if (!hba->clk_scaling.active_reqs++) in ufshcd_clk_scaling_start_busy()
2059 if (!hba->clk_scaling.is_enabled || hba->pm_op_in_progress) { in ufshcd_clk_scaling_start_busy()
2060 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_clk_scaling_start_busy()
2065 queue_work(hba->clk_scaling.workq, in ufshcd_clk_scaling_start_busy()
2066 &hba->clk_scaling.resume_work); in ufshcd_clk_scaling_start_busy()
2068 if (!hba->clk_scaling.window_start_t) { in ufshcd_clk_scaling_start_busy()
2069 hba->clk_scaling.window_start_t = curr_t; in ufshcd_clk_scaling_start_busy()
2070 hba->clk_scaling.tot_busy_t = 0; in ufshcd_clk_scaling_start_busy()
2071 hba->clk_scaling.is_busy_started = false; in ufshcd_clk_scaling_start_busy()
2074 if (!hba->clk_scaling.is_busy_started) { in ufshcd_clk_scaling_start_busy()
2075 hba->clk_scaling.busy_start_t = curr_t; in ufshcd_clk_scaling_start_busy()
2076 hba->clk_scaling.is_busy_started = true; in ufshcd_clk_scaling_start_busy()
2078 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_clk_scaling_start_busy()
2083 struct ufs_clk_scaling *scaling = &hba->clk_scaling; in ufshcd_clk_scaling_update_busy()
2089 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_clk_scaling_update_busy()
2090 hba->clk_scaling.active_reqs--; in ufshcd_clk_scaling_update_busy()
2091 if (!scaling->active_reqs && scaling->is_busy_started) { in ufshcd_clk_scaling_update_busy()
2092 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(), in ufshcd_clk_scaling_update_busy()
2093 scaling->busy_start_t)); in ufshcd_clk_scaling_update_busy()
2094 scaling->busy_start_t = 0; in ufshcd_clk_scaling_update_busy()
2095 scaling->is_busy_started = false; in ufshcd_clk_scaling_update_busy()
2097 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_clk_scaling_update_busy()
2107 return -EINVAL; in ufshcd_monitor_opcode2dir()
2113 const struct ufs_hba_monitor *m = &hba->monitor; in ufshcd_should_inform_monitor()
2115 return (m->enabled && lrbp && lrbp->cmd && in ufshcd_should_inform_monitor()
2116 (!m->chunk_size || m->chunk_size == lrbp->cmd->sdb.length) && in ufshcd_should_inform_monitor()
2117 ktime_before(hba->monitor.enabled_ts, lrbp->issue_time_stamp)); in ufshcd_should_inform_monitor()
2123 int dir = ufshcd_monitor_opcode2dir(*lrbp->cmd->cmnd); in ufshcd_start_monitor()
2126 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_start_monitor()
2127 if (dir >= 0 && hba->monitor.nr_queued[dir]++ == 0) in ufshcd_start_monitor()
2128 hba->monitor.busy_start_ts[dir] = ktime_get(); in ufshcd_start_monitor()
2129 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_start_monitor()
2134 int dir = ufshcd_monitor_opcode2dir(*lrbp->cmd->cmnd); in ufshcd_update_monitor()
2137 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_update_monitor()
2138 if (dir >= 0 && hba->monitor.nr_queued[dir] > 0) { in ufshcd_update_monitor()
2139 const struct request *req = scsi_cmd_to_rq(lrbp->cmd); in ufshcd_update_monitor()
2140 struct ufs_hba_monitor *m = &hba->monitor; in ufshcd_update_monitor()
2143 now = lrbp->compl_time_stamp; in ufshcd_update_monitor()
2144 inc = ktime_sub(now, m->busy_start_ts[dir]); in ufshcd_update_monitor()
2145 m->total_busy[dir] = ktime_add(m->total_busy[dir], inc); in ufshcd_update_monitor()
2146 m->nr_sec_rw[dir] += blk_rq_sectors(req); in ufshcd_update_monitor()
2149 m->nr_req[dir]++; in ufshcd_update_monitor()
2150 lat = ktime_sub(now, lrbp->issue_time_stamp); in ufshcd_update_monitor()
2151 m->lat_sum[dir] += lat; in ufshcd_update_monitor()
2152 if (m->lat_max[dir] < lat || !m->lat_max[dir]) in ufshcd_update_monitor()
2153 m->lat_max[dir] = lat; in ufshcd_update_monitor()
2154 if (m->lat_min[dir] > lat || !m->lat_min[dir]) in ufshcd_update_monitor()
2155 m->lat_min[dir] = lat; in ufshcd_update_monitor()
2157 m->nr_queued[dir]--; in ufshcd_update_monitor()
2159 m->busy_start_ts[dir] = now; in ufshcd_update_monitor()
2161 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_update_monitor()
2165 * ufshcd_send_command - Send SCSI or device management commands
2174 struct ufshcd_lrb *lrbp = &hba->lrb[task_tag]; in ufshcd_send_command()
2177 lrbp->issue_time_stamp = ktime_get(); in ufshcd_send_command()
2178 lrbp->issue_time_stamp_local_clock = local_clock(); in ufshcd_send_command()
2179 lrbp->compl_time_stamp = ktime_set(0, 0); in ufshcd_send_command()
2180 lrbp->compl_time_stamp_local_clock = 0; in ufshcd_send_command()
2188 struct utp_transfer_req_desc *src = lrbp->utr_descriptor_ptr; in ufshcd_send_command()
2191 spin_lock(&hwq->sq_lock); in ufshcd_send_command()
2192 dest = hwq->sqe_base_addr + hwq->sq_tail_slot; in ufshcd_send_command()
2195 spin_unlock(&hwq->sq_lock); in ufshcd_send_command()
2197 spin_lock_irqsave(&hba->outstanding_lock, flags); in ufshcd_send_command()
2198 if (hba->vops && hba->vops->setup_xfer_req) in ufshcd_send_command()
2199 hba->vops->setup_xfer_req(hba, lrbp->task_tag, in ufshcd_send_command()
2200 !!lrbp->cmd); in ufshcd_send_command()
2201 __set_bit(lrbp->task_tag, &hba->outstanding_reqs); in ufshcd_send_command()
2202 ufshcd_writel(hba, 1 << lrbp->task_tag, in ufshcd_send_command()
2204 spin_unlock_irqrestore(&hba->outstanding_lock, flags); in ufshcd_send_command()
2209 * ufshcd_copy_sense_data - Copy sense data in case of check condition
2214 u8 *const sense_buffer = lrbp->cmd->sense_buffer; in ufshcd_copy_sense_data()
2218 resp_len = be16_to_cpu(lrbp->ucd_rsp_ptr->header.data_segment_length); in ufshcd_copy_sense_data()
2222 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len); in ufshcd_copy_sense_data()
2225 memcpy(sense_buffer, lrbp->ucd_rsp_ptr->sr.sense_data, in ufshcd_copy_sense_data()
2231 * ufshcd_copy_query_response() - Copy the Query Response and the data
2241 struct ufs_query_res *query_res = &hba->dev_cmd.query.response; in ufshcd_copy_query_response()
2243 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE); in ufshcd_copy_query_response()
2246 if (hba->dev_cmd.query.descriptor && in ufshcd_copy_query_response()
2247 lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) { in ufshcd_copy_query_response()
2248 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + in ufshcd_copy_query_response()
2254 resp_len = be16_to_cpu(lrbp->ucd_rsp_ptr->header in ufshcd_copy_query_response()
2257 hba->dev_cmd.query.request.upiu_req.length); in ufshcd_copy_query_response()
2259 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len); in ufshcd_copy_query_response()
2261 dev_warn(hba->dev, in ufshcd_copy_query_response()
2264 return -EINVAL; in ufshcd_copy_query_response()
2272 * ufshcd_hba_capabilities - Read controller capabilities
2281 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES); in ufshcd_hba_capabilities()
2284 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1; in ufshcd_hba_capabilities()
2285 hba->nutmrs = in ufshcd_hba_capabilities()
2286 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1; in ufshcd_hba_capabilities()
2287 hba->reserved_slot = hba->nutrs - 1; in ufshcd_hba_capabilities()
2292 dev_err(hba->dev, "crypto setup failed\n"); in ufshcd_hba_capabilities()
2301 hba->mcq_sup = FIELD_GET(MASK_MCQ_SUPPORT, hba->capabilities); in ufshcd_hba_capabilities()
2306 hba->lsdb_sup = !FIELD_GET(MASK_LSDB_SUPPORT, hba->capabilities); in ufshcd_hba_capabilities()
2307 if (!hba->mcq_sup) in ufshcd_hba_capabilities()
2310 hba->mcq_capabilities = ufshcd_readl(hba, REG_MCQCAP); in ufshcd_hba_capabilities()
2311 hba->ext_iid_sup = FIELD_GET(MASK_EXT_IID_SUPPORT, in ufshcd_hba_capabilities()
2312 hba->mcq_capabilities); in ufshcd_hba_capabilities()
2318 * ufshcd_ready_for_uic_cmd - Check if controller is ready
2334 * ufshcd_get_upmcrs - Get the power mode change request status
2347 * ufshcd_dispatch_uic_cmd - Dispatch an UIC command to the Unipro layer
2354 lockdep_assert_held(&hba->uic_cmd_mutex); in ufshcd_dispatch_uic_cmd()
2356 WARN_ON(hba->active_uic_cmd); in ufshcd_dispatch_uic_cmd()
2358 hba->active_uic_cmd = uic_cmd; in ufshcd_dispatch_uic_cmd()
2361 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1); in ufshcd_dispatch_uic_cmd()
2362 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2); in ufshcd_dispatch_uic_cmd()
2363 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3); in ufshcd_dispatch_uic_cmd()
2368 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK, in ufshcd_dispatch_uic_cmd()
2373 * ufshcd_wait_for_uic_cmd - Wait for completion of an UIC command
2385 lockdep_assert_held(&hba->uic_cmd_mutex); in ufshcd_wait_for_uic_cmd()
2387 if (wait_for_completion_timeout(&uic_cmd->done, in ufshcd_wait_for_uic_cmd()
2389 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT; in ufshcd_wait_for_uic_cmd()
2391 ret = -ETIMEDOUT; in ufshcd_wait_for_uic_cmd()
2392 dev_err(hba->dev, in ufshcd_wait_for_uic_cmd()
2394 uic_cmd->command, uic_cmd->argument3); in ufshcd_wait_for_uic_cmd()
2396 if (!uic_cmd->cmd_active) { in ufshcd_wait_for_uic_cmd()
2397 dev_err(hba->dev, "%s: UIC cmd has been completed, return the result\n", in ufshcd_wait_for_uic_cmd()
2399 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT; in ufshcd_wait_for_uic_cmd()
2403 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_wait_for_uic_cmd()
2404 hba->active_uic_cmd = NULL; in ufshcd_wait_for_uic_cmd()
2405 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_wait_for_uic_cmd()
2411 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
2420 lockdep_assert_held(&hba->uic_cmd_mutex); in __ufshcd_send_uic_cmd()
2423 dev_err(hba->dev, in __ufshcd_send_uic_cmd()
2425 return -EIO; in __ufshcd_send_uic_cmd()
2428 init_completion(&uic_cmd->done); in __ufshcd_send_uic_cmd()
2430 uic_cmd->cmd_active = 1; in __ufshcd_send_uic_cmd()
2437 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
2447 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UIC_CMD) in ufshcd_send_uic_cmd()
2451 mutex_lock(&hba->uic_cmd_mutex); in ufshcd_send_uic_cmd()
2458 mutex_unlock(&hba->uic_cmd_mutex); in ufshcd_send_uic_cmd()
2465 * ufshcd_sgl_to_prdt - SG list to PRTD (Physical Region Description Table, 4DW format)
2466 * @hba: per-adapter instance
2480 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) in ufshcd_sgl_to_prdt()
2481 lrbp->utr_descriptor_ptr->prd_table_length = in ufshcd_sgl_to_prdt()
2484 lrbp->utr_descriptor_ptr->prd_table_length = cpu_to_le16(sg_entries); in ufshcd_sgl_to_prdt()
2486 prd = lrbp->ucd_prdt_ptr; in ufshcd_sgl_to_prdt()
2500 prd->size = cpu_to_le32(len - 1); in ufshcd_sgl_to_prdt()
2501 prd->addr = cpu_to_le64(sg->dma_address); in ufshcd_sgl_to_prdt()
2502 prd->reserved = 0; in ufshcd_sgl_to_prdt()
2506 lrbp->utr_descriptor_ptr->prd_table_length = 0; in ufshcd_sgl_to_prdt()
2511 * ufshcd_map_sg - Map scatter-gather list to prdt
2515 * Return: 0 in case of success, non-zero value in case of failure.
2519 struct scsi_cmnd *cmd = lrbp->cmd; in ufshcd_map_sg()
2531 * ufshcd_enable_intr - enable interrupts
2539 if (hba->ufs_version == ufshci_version(1, 0)) { in ufshcd_enable_intr()
2551 * ufshcd_disable_intr - disable interrupts
2559 if (hba->ufs_version == ufshci_version(1, 0)) { in ufshcd_disable_intr()
2573 * ufshcd_prepare_req_desc_hdr - Fill UTP Transfer request descriptor header according to request
2583 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr; in ufshcd_prepare_req_desc_hdr()
2584 struct request_desc_header *h = &req_desc->header; in ufshcd_prepare_req_desc_hdr()
2600 h->command_type = lrbp->command_type; in ufshcd_prepare_req_desc_hdr()
2601 h->data_direction = data_direction; in ufshcd_prepare_req_desc_hdr()
2602 h->ehs_length = ehs_length; in ufshcd_prepare_req_desc_hdr()
2604 if (lrbp->intr_cmd) in ufshcd_prepare_req_desc_hdr()
2605 h->interrupt = 1; in ufshcd_prepare_req_desc_hdr()
2615 h->ocs = OCS_INVALID_COMMAND_STATUS; in ufshcd_prepare_req_desc_hdr()
2617 req_desc->prd_table_length = 0; in ufshcd_prepare_req_desc_hdr()
2621 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
2629 struct scsi_cmnd *cmd = lrbp->cmd; in ufshcd_prepare_utp_scsi_cmd_upiu()
2630 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; in ufshcd_prepare_utp_scsi_cmd_upiu()
2633 ucd_req_ptr->header = (struct utp_upiu_header){ in ufshcd_prepare_utp_scsi_cmd_upiu()
2636 .lun = lrbp->lun, in ufshcd_prepare_utp_scsi_cmd_upiu()
2637 .task_tag = lrbp->task_tag, in ufshcd_prepare_utp_scsi_cmd_upiu()
2641 ucd_req_ptr->sc.exp_data_transfer_len = cpu_to_be32(cmd->sdb.length); in ufshcd_prepare_utp_scsi_cmd_upiu()
2643 cdb_len = min_t(unsigned short, cmd->cmd_len, UFS_CDB_SIZE); in ufshcd_prepare_utp_scsi_cmd_upiu()
2644 memset(ucd_req_ptr->sc.cdb, 0, UFS_CDB_SIZE); in ufshcd_prepare_utp_scsi_cmd_upiu()
2645 memcpy(ucd_req_ptr->sc.cdb, cmd->cmnd, cdb_len); in ufshcd_prepare_utp_scsi_cmd_upiu()
2647 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); in ufshcd_prepare_utp_scsi_cmd_upiu()
2651 * ufshcd_prepare_utp_query_req_upiu() - fill the utp_transfer_req_desc for query request
2659 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; in ufshcd_prepare_utp_query_req_upiu()
2660 struct ufs_query *query = &hba->dev_cmd.query; in ufshcd_prepare_utp_query_req_upiu()
2661 u16 len = be16_to_cpu(query->request.upiu_req.length); in ufshcd_prepare_utp_query_req_upiu()
2664 ucd_req_ptr->header = (struct utp_upiu_header){ in ufshcd_prepare_utp_query_req_upiu()
2667 .lun = lrbp->lun, in ufshcd_prepare_utp_query_req_upiu()
2668 .task_tag = lrbp->task_tag, in ufshcd_prepare_utp_query_req_upiu()
2669 .query_function = query->request.query_func, in ufshcd_prepare_utp_query_req_upiu()
2672 query->request.upiu_req.opcode == in ufshcd_prepare_utp_query_req_upiu()
2679 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req, in ufshcd_prepare_utp_query_req_upiu()
2683 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) in ufshcd_prepare_utp_query_req_upiu()
2684 memcpy(ucd_req_ptr + 1, query->descriptor, len); in ufshcd_prepare_utp_query_req_upiu()
2686 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); in ufshcd_prepare_utp_query_req_upiu()
2691 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; in ufshcd_prepare_utp_nop_upiu()
2695 ucd_req_ptr->header = (struct utp_upiu_header){ in ufshcd_prepare_utp_nop_upiu()
2697 .task_tag = lrbp->task_tag, in ufshcd_prepare_utp_nop_upiu()
2700 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); in ufshcd_prepare_utp_nop_upiu()
2704 * ufshcd_compose_devman_upiu - UFS Protocol Information Unit(UPIU)
2717 if (hba->ufs_version <= ufshci_version(1, 1)) in ufshcd_compose_devman_upiu()
2718 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE; in ufshcd_compose_devman_upiu()
2720 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE; in ufshcd_compose_devman_upiu()
2723 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY) in ufshcd_compose_devman_upiu()
2725 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP) in ufshcd_compose_devman_upiu()
2728 ret = -EINVAL; in ufshcd_compose_devman_upiu()
2734 * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU)
2746 if (hba->ufs_version <= ufshci_version(1, 1)) in ufshcd_comp_scsi_upiu()
2747 lrbp->command_type = UTP_CMD_TYPE_SCSI; in ufshcd_comp_scsi_upiu()
2749 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE; in ufshcd_comp_scsi_upiu()
2751 if (likely(lrbp->cmd)) { in ufshcd_comp_scsi_upiu()
2752 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, lrbp->cmd->sc_data_direction, 0); in ufshcd_comp_scsi_upiu()
2755 ret = -EINVAL; in ufshcd_comp_scsi_upiu()
2762 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
2763 * @upiu_wlun_id: UPIU W-LUN id
2765 * Return: SCSI W-LUN id.
2774 return sdev->lun == in is_device_wlun()
2788 hba->nr_queues[HCTX_TYPE_DEFAULT] = 1; in ufshcd_map_queues()
2789 hba->nr_queues[HCTX_TYPE_READ] = 0; in ufshcd_map_queues()
2790 hba->nr_queues[HCTX_TYPE_POLL] = 1; in ufshcd_map_queues()
2791 hba->nr_hw_queues = 1; in ufshcd_map_queues()
2794 for (i = 0; i < shost->nr_maps; i++) { in ufshcd_map_queues()
2795 struct blk_mq_queue_map *map = &shost->tag_set.map[i]; in ufshcd_map_queues()
2797 map->nr_queues = hba->nr_queues[i]; in ufshcd_map_queues()
2798 if (!map->nr_queues) in ufshcd_map_queues()
2800 map->queue_offset = queue_offset; in ufshcd_map_queues()
2802 map->queue_offset = 0; in ufshcd_map_queues()
2805 queue_offset += map->nr_queues; in ufshcd_map_queues()
2811 struct utp_transfer_cmd_desc *cmd_descp = (void *)hba->ucdl_base_addr + in ufshcd_init_lrb()
2813 struct utp_transfer_req_desc *utrdlp = hba->utrdl_base_addr; in ufshcd_init_lrb()
2814 dma_addr_t cmd_desc_element_addr = hba->ucdl_dma_addr + in ufshcd_init_lrb()
2819 lrb->utr_descriptor_ptr = utrdlp + i; in ufshcd_init_lrb()
2820 lrb->utrd_dma_addr = hba->utrdl_dma_addr + in ufshcd_init_lrb()
2822 lrb->ucd_req_ptr = (struct utp_upiu_req *)cmd_descp->command_upiu; in ufshcd_init_lrb()
2823 lrb->ucd_req_dma_addr = cmd_desc_element_addr; in ufshcd_init_lrb()
2824 lrb->ucd_rsp_ptr = (struct utp_upiu_rsp *)cmd_descp->response_upiu; in ufshcd_init_lrb()
2825 lrb->ucd_rsp_dma_addr = cmd_desc_element_addr + response_offset; in ufshcd_init_lrb()
2826 lrb->ucd_prdt_ptr = (struct ufshcd_sg_entry *)cmd_descp->prd_table; in ufshcd_init_lrb()
2827 lrb->ucd_prdt_dma_addr = cmd_desc_element_addr + prdt_offset; in ufshcd_init_lrb()
2831 * ufshcd_queuecommand - main entry point for SCSI requests
2835 * Return: 0 for success, non-zero in case of failure.
2840 int tag = scsi_cmd_to_rq(cmd)->tag; in ufshcd_queuecommand()
2845 WARN_ONCE(tag < 0 || tag >= hba->nutrs, "Invalid tag %d\n", tag); in ufshcd_queuecommand()
2847 switch (hba->ufshcd_state) { in ufshcd_queuecommand()
2852 * SCSI error handler can call ->queuecommand() while UFS error in ufshcd_queuecommand()
2874 if (hba->pm_op_in_progress) { in ufshcd_queuecommand()
2875 hba->force_reset = true; in ufshcd_queuecommand()
2890 hba->req_abort_count = 0; in ufshcd_queuecommand()
2894 lrbp = &hba->lrb[tag]; in ufshcd_queuecommand()
2895 lrbp->cmd = cmd; in ufshcd_queuecommand()
2896 lrbp->task_tag = tag; in ufshcd_queuecommand()
2897 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun); in ufshcd_queuecommand()
2898 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba); in ufshcd_queuecommand()
2902 lrbp->req_abort_skip = false; in ufshcd_queuecommand()
2921 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_queuecommand()
2923 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_queuecommand()
2932 lrbp->cmd = NULL; in ufshcd_compose_dev_cmd()
2933 lrbp->task_tag = tag; in ufshcd_compose_dev_cmd()
2934 lrbp->lun = 0; /* device management cmd is not specific to any LUN */ in ufshcd_compose_dev_cmd()
2935 lrbp->intr_cmd = true; /* No interrupt aggregation */ in ufshcd_compose_dev_cmd()
2937 hba->dev_cmd.type = cmd_type; in ufshcd_compose_dev_cmd()
2981 dev_err(hba->dev, "%s: failed tag=%d. err=%d\n", in ufshcd_clear_cmd()
2991 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_clear_cmd()
2993 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_clear_cmd()
2996 * wait for h/w to clear corresponding bit in door-bell. in ufshcd_clear_cmd()
3004 * ufshcd_dev_cmd_completion() - handles device management command responses
3016 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); in ufshcd_dev_cmd_completion()
3017 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr); in ufshcd_dev_cmd_completion()
3021 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) { in ufshcd_dev_cmd_completion()
3022 err = -EINVAL; in ufshcd_dev_cmd_completion()
3023 dev_err(hba->dev, "%s: unexpected response %x\n", in ufshcd_dev_cmd_completion()
3028 u8 response = lrbp->ucd_rsp_ptr->header.response; in ufshcd_dev_cmd_completion()
3033 err = -EINVAL; in ufshcd_dev_cmd_completion()
3034 dev_err(hba->dev, "%s: unexpected response in Query RSP: %x\n", in ufshcd_dev_cmd_completion()
3041 err = -EPERM; in ufshcd_dev_cmd_completion()
3042 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n", in ufshcd_dev_cmd_completion()
3046 if (hba->dev_cmd.type != DEV_CMD_TYPE_RPMB) { in ufshcd_dev_cmd_completion()
3047 err = -EINVAL; in ufshcd_dev_cmd_completion()
3048 dev_err(hba->dev, "%s: unexpected response %x\n", __func__, resp); in ufshcd_dev_cmd_completion()
3052 err = -EINVAL; in ufshcd_dev_cmd_completion()
3053 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n", in ufshcd_dev_cmd_completion()
3070 time_left = wait_for_completion_timeout(hba->dev_cmd.complete, in ufshcd_wait_for_dev_cmd()
3079 hba->dev_cmd.complete = NULL; in ufshcd_wait_for_dev_cmd()
3084 err = -ETIMEDOUT; in ufshcd_wait_for_dev_cmd()
3085 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n", in ufshcd_wait_for_dev_cmd()
3086 __func__, lrbp->task_tag); in ufshcd_wait_for_dev_cmd()
3091 if (ufshcd_clear_cmd(hba, lrbp->task_tag) == 0) in ufshcd_wait_for_dev_cmd()
3092 err = -EAGAIN; in ufshcd_wait_for_dev_cmd()
3093 hba->dev_cmd.complete = NULL; in ufshcd_wait_for_dev_cmd()
3098 if (ufshcd_clear_cmd(hba, lrbp->task_tag) == 0) { in ufshcd_wait_for_dev_cmd()
3100 err = -EAGAIN; in ufshcd_wait_for_dev_cmd()
3106 spin_lock_irqsave(&hba->outstanding_lock, flags); in ufshcd_wait_for_dev_cmd()
3107 pending = test_bit(lrbp->task_tag, in ufshcd_wait_for_dev_cmd()
3108 &hba->outstanding_reqs); in ufshcd_wait_for_dev_cmd()
3110 hba->dev_cmd.complete = NULL; in ufshcd_wait_for_dev_cmd()
3111 __clear_bit(lrbp->task_tag, in ufshcd_wait_for_dev_cmd()
3112 &hba->outstanding_reqs); in ufshcd_wait_for_dev_cmd()
3114 spin_unlock_irqrestore(&hba->outstanding_lock, flags); in ufshcd_wait_for_dev_cmd()
3125 dev_err(hba->dev, "%s: failed to clear tag %d\n", in ufshcd_wait_for_dev_cmd()
3126 __func__, lrbp->task_tag); in ufshcd_wait_for_dev_cmd()
3128 spin_lock_irqsave(&hba->outstanding_lock, flags); in ufshcd_wait_for_dev_cmd()
3129 pending = test_bit(lrbp->task_tag, in ufshcd_wait_for_dev_cmd()
3130 &hba->outstanding_reqs); in ufshcd_wait_for_dev_cmd()
3132 hba->dev_cmd.complete = NULL; in ufshcd_wait_for_dev_cmd()
3133 spin_unlock_irqrestore(&hba->outstanding_lock, flags); in ufshcd_wait_for_dev_cmd()
3150 * ufshcd_exec_dev_cmd - API for sending device management requests
3158 * it is expected you hold the hba->dev_cmd.lock mutex.
3164 const u32 tag = hba->reserved_slot; in ufshcd_exec_dev_cmd()
3168 /* Protects use of hba->reserved_slot. */ in ufshcd_exec_dev_cmd()
3169 lockdep_assert_held(&hba->dev_cmd.lock); in ufshcd_exec_dev_cmd()
3171 down_read(&hba->clk_scaling_lock); in ufshcd_exec_dev_cmd()
3173 lrbp = &hba->lrb[tag]; in ufshcd_exec_dev_cmd()
3174 lrbp->cmd = NULL; in ufshcd_exec_dev_cmd()
3179 hba->dev_cmd.complete = &wait; in ufshcd_exec_dev_cmd()
3181 ufshcd_add_query_upiu_trace(hba, UFS_QUERY_SEND, lrbp->ucd_req_ptr); in ufshcd_exec_dev_cmd()
3183 ufshcd_send_command(hba, tag, hba->dev_cmd_queue); in ufshcd_exec_dev_cmd()
3186 (struct utp_upiu_req *)lrbp->ucd_rsp_ptr); in ufshcd_exec_dev_cmd()
3189 up_read(&hba->clk_scaling_lock); in ufshcd_exec_dev_cmd()
3194 * ufshcd_init_query() - init the query response and request parameters
3195 * @hba: per-adapter instance
3207 *request = &hba->dev_cmd.query.request; in ufshcd_init_query()
3208 *response = &hba->dev_cmd.query.response; in ufshcd_init_query()
3211 (*request)->upiu_req.opcode = opcode; in ufshcd_init_query()
3212 (*request)->upiu_req.idn = idn; in ufshcd_init_query()
3213 (*request)->upiu_req.index = index; in ufshcd_init_query()
3214 (*request)->upiu_req.selector = selector; in ufshcd_init_query()
3226 dev_dbg(hba->dev, in ufshcd_query_flag_retry()
3234 dev_err(hba->dev, in ufshcd_query_flag_retry()
3241 * ufshcd_query_flag() - API function for sending flag query requests
3242 * @hba: per-adapter instance
3248 * Return: 0 for success, non-zero in case of failure.
3261 mutex_lock(&hba->dev_cmd.lock); in ufshcd_query_flag()
3269 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; in ufshcd_query_flag()
3272 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; in ufshcd_query_flag()
3275 dev_err(hba->dev, "%s: Invalid argument for read request\n", in ufshcd_query_flag()
3277 err = -EINVAL; in ufshcd_query_flag()
3282 dev_err(hba->dev, in ufshcd_query_flag()
3285 err = -EINVAL; in ufshcd_query_flag()
3292 dev_err(hba->dev, in ufshcd_query_flag()
3299 *flag_res = (be32_to_cpu(response->upiu_res.value) & in ufshcd_query_flag()
3303 mutex_unlock(&hba->dev_cmd.lock); in ufshcd_query_flag()
3309 * ufshcd_query_attr - API function for sending attribute requests
3310 * @hba: per-adapter instance
3317 * Return: 0 for success, non-zero in case of failure.
3329 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n", in ufshcd_query_attr()
3331 return -EINVAL; in ufshcd_query_attr()
3336 mutex_lock(&hba->dev_cmd.lock); in ufshcd_query_attr()
3342 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; in ufshcd_query_attr()
3343 request->upiu_req.value = cpu_to_be32(*attr_val); in ufshcd_query_attr()
3346 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; in ufshcd_query_attr()
3349 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n", in ufshcd_query_attr()
3351 err = -EINVAL; in ufshcd_query_attr()
3358 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n", in ufshcd_query_attr()
3363 *attr_val = be32_to_cpu(response->upiu_res.value); in ufshcd_query_attr()
3366 mutex_unlock(&hba->dev_cmd.lock); in ufshcd_query_attr()
3372 * ufshcd_query_attr_retry() - API function for sending query
3374 * @hba: per-adapter instance
3382 * Return: 0 for success, non-zero in case of failure.
3391 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { in ufshcd_query_attr_retry()
3395 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n", in ufshcd_query_attr_retry()
3402 dev_err(hba->dev, in ufshcd_query_attr_retry()
3419 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n", in __ufshcd_query_descriptor()
3421 return -EINVAL; in __ufshcd_query_descriptor()
3425 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n", in __ufshcd_query_descriptor()
3427 return -EINVAL; in __ufshcd_query_descriptor()
3432 mutex_lock(&hba->dev_cmd.lock); in __ufshcd_query_descriptor()
3435 hba->dev_cmd.query.descriptor = desc_buf; in __ufshcd_query_descriptor()
3436 request->upiu_req.length = cpu_to_be16(*buf_len); in __ufshcd_query_descriptor()
3440 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; in __ufshcd_query_descriptor()
3443 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; in __ufshcd_query_descriptor()
3446 dev_err(hba->dev, in __ufshcd_query_descriptor()
3449 err = -EINVAL; in __ufshcd_query_descriptor()
3456 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n", in __ufshcd_query_descriptor()
3461 *buf_len = be16_to_cpu(response->upiu_res.length); in __ufshcd_query_descriptor()
3464 hba->dev_cmd.query.descriptor = NULL; in __ufshcd_query_descriptor()
3465 mutex_unlock(&hba->dev_cmd.lock); in __ufshcd_query_descriptor()
3471 * ufshcd_query_descriptor_retry - API function for sending descriptor requests
3472 * @hba: per-adapter instance
3483 * Return: 0 for success, non-zero in case of failure.
3494 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { in ufshcd_query_descriptor_retry()
3497 if (!err || err == -EINVAL) in ufshcd_query_descriptor_retry()
3505 * ufshcd_read_desc_param - read the specified descriptor parameter
3513 * Return: 0 in case of success, non-zero otherwise.
3529 return -EINVAL; in ufshcd_read_desc_param()
3535 return -ENOMEM; in ufshcd_read_desc_param()
3546 …dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret … in ufshcd_read_desc_param()
3555 dev_err(hba->dev, "%s: Invalid offset 0x%x in descriptor IDN 0x%x, length 0x%x\n", in ufshcd_read_desc_param()
3557 ret = -EINVAL; in ufshcd_read_desc_param()
3563 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header\n", in ufshcd_read_desc_param()
3565 ret = -EINVAL; in ufshcd_read_desc_param()
3572 ret = -EINVAL; in ufshcd_read_desc_param()
3575 min_t(u32, param_size, buff_len - param_offset)); in ufshcd_read_desc_param()
3584 * struct uc_string_id - unicode string
3596 /* replace non-printable or non-ASCII characters with spaces */
3603 * ufshcd_read_string_desc - read string descriptor
3613 * * -ENOMEM: on allocation failure
3614 * * -EINVAL: on a wrong parameter
3624 return -EINVAL; in ufshcd_read_string_desc()
3628 return -ENOMEM; in ufshcd_read_string_desc()
3633 dev_err(hba->dev, "Reading String Desc failed after %d retries. err = %d\n", in ufshcd_read_string_desc()
3639 if (uc_str->len <= QUERY_DESC_HDR_SIZE) { in ufshcd_read_string_desc()
3640 dev_dbg(hba->dev, "String Desc is of zero length\n"); in ufshcd_read_string_desc()
3650 ascii_len = (uc_str->len - QUERY_DESC_HDR_SIZE) / 2 + 1; in ufshcd_read_string_desc()
3653 ret = -ENOMEM; in ufshcd_read_string_desc()
3659 * we need to convert to utf-8 so it can be displayed in ufshcd_read_string_desc()
3661 ret = utf16s_to_utf8s(uc_str->uc, in ufshcd_read_string_desc()
3662 uc_str->len - QUERY_DESC_HDR_SIZE, in ufshcd_read_string_desc()
3663 UTF16_BIG_ENDIAN, str, ascii_len - 1); in ufshcd_read_string_desc()
3665 /* replace non-printable or non-ASCII characters with spaces */ in ufshcd_read_string_desc()
3672 str = kmemdup(uc_str, uc_str->len, GFP_KERNEL); in ufshcd_read_string_desc()
3674 ret = -ENOMEM; in ufshcd_read_string_desc()
3677 ret = uc_str->len; in ufshcd_read_string_desc()
3686 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
3693 * Return: 0 in case of success, non-zero otherwise.
3705 if (!ufs_is_valid_unit_desc_lun(&hba->dev_info, lun)) in ufshcd_read_unit_desc_param()
3706 return -EOPNOTSUPP; in ufshcd_read_unit_desc_param()
3717 if (hba->dev_info.wspecversion >= 0x300) { in ufshcd_get_ref_clk_gating_wait()
3722 dev_err(hba->dev, "Failed reading bRefClkGatingWait. err = %d, use default %uus\n", in ufshcd_get_ref_clk_gating_wait()
3727 dev_err(hba->dev, "Undefined ref clk gating wait time, use default %uus\n", in ufshcd_get_ref_clk_gating_wait()
3731 hba->dev_info.clk_gating_wait_us = gating_wait; in ufshcd_get_ref_clk_gating_wait()
3738 * ufshcd_memory_alloc - allocate memory for host memory space data structures
3748 * Return: 0 for success, non-zero in case of failure.
3755 ucdl_size = ufshcd_get_ucd_size(hba) * hba->nutrs; in ufshcd_memory_alloc()
3756 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev, in ufshcd_memory_alloc()
3758 &hba->ucdl_dma_addr, in ufshcd_memory_alloc()
3764 if (!hba->ucdl_base_addr || in ufshcd_memory_alloc()
3765 WARN_ON(hba->ucdl_dma_addr & (128 - 1))) { in ufshcd_memory_alloc()
3766 dev_err(hba->dev, in ufshcd_memory_alloc()
3775 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs); in ufshcd_memory_alloc()
3776 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev, in ufshcd_memory_alloc()
3778 &hba->utrdl_dma_addr, in ufshcd_memory_alloc()
3780 if (!hba->utrdl_base_addr || in ufshcd_memory_alloc()
3781 WARN_ON(hba->utrdl_dma_addr & (SZ_1K - 1))) { in ufshcd_memory_alloc()
3782 dev_err(hba->dev, in ufshcd_memory_alloc()
3793 if (hba->utmrdl_base_addr) in ufshcd_memory_alloc()
3799 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs; in ufshcd_memory_alloc()
3800 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev, in ufshcd_memory_alloc()
3802 &hba->utmrdl_dma_addr, in ufshcd_memory_alloc()
3804 if (!hba->utmrdl_base_addr || in ufshcd_memory_alloc()
3805 WARN_ON(hba->utmrdl_dma_addr & (SZ_1K - 1))) { in ufshcd_memory_alloc()
3806 dev_err(hba->dev, in ufshcd_memory_alloc()
3813 hba->lrb = devm_kcalloc(hba->dev, in ufshcd_memory_alloc()
3814 hba->nutrs, sizeof(struct ufshcd_lrb), in ufshcd_memory_alloc()
3816 if (!hba->lrb) { in ufshcd_memory_alloc()
3817 dev_err(hba->dev, "LRB Memory allocation failed\n"); in ufshcd_memory_alloc()
3822 return -ENOMEM; in ufshcd_memory_alloc()
3826 * ufshcd_host_memory_configure - configure local reference block with
3848 utrdlp = hba->utrdl_base_addr; in ufshcd_host_memory_configure()
3856 cmd_desc_dma_addr = hba->ucdl_dma_addr; in ufshcd_host_memory_configure()
3858 for (i = 0; i < hba->nutrs; i++) { in ufshcd_host_memory_configure()
3866 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) { in ufshcd_host_memory_configure()
3882 ufshcd_init_lrb(hba, &hba->lrb[i], i); in ufshcd_host_memory_configure()
3887 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
3895 * Return: 0 on success, non-zero value on failure.
3906 dev_dbg(hba->dev, in ufshcd_dme_link_startup()
3907 "dme-link-startup: error code %d\n", ret); in ufshcd_dme_link_startup()
3911 * ufshcd_dme_reset - UIC command for DME_RESET
3917 * Return: 0 on success, non-zero value on failure.
3928 dev_err(hba->dev, in ufshcd_dme_reset()
3929 "dme-reset: error code %d\n", ret); in ufshcd_dme_reset()
3951 * ufshcd_dme_enable - UIC command for DME_ENABLE
3956 * Return: 0 on success, non-zero value on failure.
3967 dev_err(hba->dev, in ufshcd_dme_enable()
3968 "dme-enable: error code %d\n", ret); in ufshcd_dme_enable()
3978 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS)) in ufshcd_add_delay_before_dme_cmd()
3985 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) { in ufshcd_add_delay_before_dme_cmd()
3991 hba->last_dme_cmd_tstamp)); in ufshcd_add_delay_before_dme_cmd()
3995 MIN_DELAY_BEFORE_DME_CMDS_US - delta; in ufshcd_add_delay_before_dme_cmd()
4006 hba->last_dme_cmd_tstamp = ktime_get(); in ufshcd_add_delay_before_dme_cmd()
4010 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
4017 * Return: 0 on success, non-zero value on failure.
4024 "dme-set", in ufshcd_dme_set_attr()
4025 "dme-peer-set" in ufshcd_dme_set_attr()
4041 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n", in ufshcd_dme_set_attr()
4043 } while (ret && peer && --retries); in ufshcd_dme_set_attr()
4046 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n", in ufshcd_dme_set_attr()
4048 UFS_UIC_COMMAND_RETRIES - retries); in ufshcd_dme_set_attr()
4055 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
4061 * Return: 0 on success, non-zero value on failure.
4068 "dme-get", in ufshcd_dme_get_attr()
4069 "dme-peer-get" in ufshcd_dme_get_attr()
4078 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) { in ufshcd_dme_get_attr()
4079 orig_pwr_info = hba->pwr_info; in ufshcd_dme_get_attr()
4108 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n", in ufshcd_dme_get_attr()
4110 } while (ret && peer && --retries); in ufshcd_dme_get_attr()
4113 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n", in ufshcd_dme_get_attr()
4115 UFS_UIC_COMMAND_RETRIES - retries); in ufshcd_dme_get_attr()
4120 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE) in ufshcd_dme_get_attr()
4129 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
4142 * Return: 0 on success, non-zero value on failure.
4152 mutex_lock(&hba->uic_cmd_mutex); in ufshcd_uic_pwr_ctrl()
4155 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_uic_pwr_ctrl()
4157 ret = -ENOLINK; in ufshcd_uic_pwr_ctrl()
4160 hba->uic_async_done = &uic_async_done; in ufshcd_uic_pwr_ctrl()
4170 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_uic_pwr_ctrl()
4173 dev_err(hba->dev, in ufshcd_uic_pwr_ctrl()
4175 cmd->command, cmd->argument3, ret); in ufshcd_uic_pwr_ctrl()
4179 if (!wait_for_completion_timeout(hba->uic_async_done, in ufshcd_uic_pwr_ctrl()
4181 dev_err(hba->dev, in ufshcd_uic_pwr_ctrl()
4183 cmd->command, cmd->argument3); in ufshcd_uic_pwr_ctrl()
4185 if (!cmd->cmd_active) { in ufshcd_uic_pwr_ctrl()
4186 dev_err(hba->dev, "%s: Power Mode Change operation has been completed, go check UPMCRS\n", in ufshcd_uic_pwr_ctrl()
4191 ret = -ETIMEDOUT; in ufshcd_uic_pwr_ctrl()
4198 dev_err(hba->dev, in ufshcd_uic_pwr_ctrl()
4200 cmd->command, status); in ufshcd_uic_pwr_ctrl()
4201 ret = (status != PWR_OK) ? status : -1; in ufshcd_uic_pwr_ctrl()
4210 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_uic_pwr_ctrl()
4211 hba->active_uic_cmd = NULL; in ufshcd_uic_pwr_ctrl()
4212 hba->uic_async_done = NULL; in ufshcd_uic_pwr_ctrl()
4220 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_uic_pwr_ctrl()
4221 mutex_unlock(&hba->uic_cmd_mutex); in ufshcd_uic_pwr_ctrl()
4227 * ufshcd_send_bsg_uic_cmd - Send UIC commands requested via BSG layer and retrieve the result
4237 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UIC_CMD) in ufshcd_send_bsg_uic_cmd()
4242 if (uic_cmd->argument1 == UIC_ARG_MIB(PA_PWRMODE) && in ufshcd_send_bsg_uic_cmd()
4243 uic_cmd->command == UIC_CMD_DME_SET) { in ufshcd_send_bsg_uic_cmd()
4248 mutex_lock(&hba->uic_cmd_mutex); in ufshcd_send_bsg_uic_cmd()
4255 mutex_unlock(&hba->uic_cmd_mutex); in ufshcd_send_bsg_uic_cmd()
4263 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
4268 * Return: 0 on success, non-zero value on failure.
4275 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) { in ufshcd_uic_change_pwr_mode()
4279 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n", in ufshcd_uic_change_pwr_mode()
4302 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_link_recovery()
4303 hba->ufshcd_state = UFSHCD_STATE_RESET; in ufshcd_link_recovery()
4305 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_link_recovery()
4312 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_link_recovery()
4314 hba->ufshcd_state = UFSHCD_STATE_ERROR; in ufshcd_link_recovery()
4316 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_link_recovery()
4319 dev_err(hba->dev, "%s: link recovery failed, err %d", in ufshcd_link_recovery()
4336 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter", in ufshcd_uic_hibern8_enter()
4340 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n", in ufshcd_uic_hibern8_enter()
4360 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit", in ufshcd_uic_hibern8_exit()
4364 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n", in ufshcd_uic_hibern8_exit()
4369 hba->ufs_stats.last_hibern8_exit_tstamp = local_clock(); in ufshcd_uic_hibern8_exit()
4370 hba->ufs_stats.hibern8_exit_cnt++; in ufshcd_uic_hibern8_exit()
4385 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_auto_hibern8_update()
4386 if (hba->ahit != ahit) { in ufshcd_auto_hibern8_update()
4387 hba->ahit = ahit; in ufshcd_auto_hibern8_update()
4390 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_auto_hibern8_update()
4393 !pm_runtime_suspended(&hba->ufs_device_wlun->sdev_gendev)) { in ufshcd_auto_hibern8_update()
4408 ufshcd_writel(hba, hba->ahit, REG_AUTO_HIBERNATE_IDLE_TIMER); in ufshcd_auto_hibern8_enable()
4412 * ufshcd_init_pwr_info - setting the POR (power on reset)
4414 * @hba: per-adapter instance
4418 hba->pwr_info.gear_rx = UFS_PWM_G1; in ufshcd_init_pwr_info()
4419 hba->pwr_info.gear_tx = UFS_PWM_G1; in ufshcd_init_pwr_info()
4420 hba->pwr_info.lane_rx = UFS_LANE_1; in ufshcd_init_pwr_info()
4421 hba->pwr_info.lane_tx = UFS_LANE_1; in ufshcd_init_pwr_info()
4422 hba->pwr_info.pwr_rx = SLOWAUTO_MODE; in ufshcd_init_pwr_info()
4423 hba->pwr_info.pwr_tx = SLOWAUTO_MODE; in ufshcd_init_pwr_info()
4424 hba->pwr_info.hs_rate = 0; in ufshcd_init_pwr_info()
4428 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
4429 * @hba: per-adapter instance
4435 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info; in ufshcd_get_max_pwr_mode()
4437 if (hba->max_pwr_info.is_valid) in ufshcd_get_max_pwr_mode()
4440 if (hba->quirks & UFSHCD_QUIRK_HIBERN_FASTAUTO) { in ufshcd_get_max_pwr_mode()
4441 pwr_info->pwr_tx = FASTAUTO_MODE; in ufshcd_get_max_pwr_mode()
4442 pwr_info->pwr_rx = FASTAUTO_MODE; in ufshcd_get_max_pwr_mode()
4444 pwr_info->pwr_tx = FAST_MODE; in ufshcd_get_max_pwr_mode()
4445 pwr_info->pwr_rx = FAST_MODE; in ufshcd_get_max_pwr_mode()
4447 pwr_info->hs_rate = PA_HS_MODE_B; in ufshcd_get_max_pwr_mode()
4451 &pwr_info->lane_rx); in ufshcd_get_max_pwr_mode()
4453 &pwr_info->lane_tx); in ufshcd_get_max_pwr_mode()
4455 if (!pwr_info->lane_rx || !pwr_info->lane_tx) { in ufshcd_get_max_pwr_mode()
4456 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n", in ufshcd_get_max_pwr_mode()
4458 pwr_info->lane_rx, in ufshcd_get_max_pwr_mode()
4459 pwr_info->lane_tx); in ufshcd_get_max_pwr_mode()
4460 return -EINVAL; in ufshcd_get_max_pwr_mode()
4468 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx); in ufshcd_get_max_pwr_mode()
4469 if (!pwr_info->gear_rx) { in ufshcd_get_max_pwr_mode()
4471 &pwr_info->gear_rx); in ufshcd_get_max_pwr_mode()
4472 if (!pwr_info->gear_rx) { in ufshcd_get_max_pwr_mode()
4473 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n", in ufshcd_get_max_pwr_mode()
4474 __func__, pwr_info->gear_rx); in ufshcd_get_max_pwr_mode()
4475 return -EINVAL; in ufshcd_get_max_pwr_mode()
4477 pwr_info->pwr_rx = SLOW_MODE; in ufshcd_get_max_pwr_mode()
4481 &pwr_info->gear_tx); in ufshcd_get_max_pwr_mode()
4482 if (!pwr_info->gear_tx) { in ufshcd_get_max_pwr_mode()
4484 &pwr_info->gear_tx); in ufshcd_get_max_pwr_mode()
4485 if (!pwr_info->gear_tx) { in ufshcd_get_max_pwr_mode()
4486 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n", in ufshcd_get_max_pwr_mode()
4487 __func__, pwr_info->gear_tx); in ufshcd_get_max_pwr_mode()
4488 return -EINVAL; in ufshcd_get_max_pwr_mode()
4490 pwr_info->pwr_tx = SLOW_MODE; in ufshcd_get_max_pwr_mode()
4493 hba->max_pwr_info.is_valid = true; in ufshcd_get_max_pwr_mode()
4503 if (!hba->force_pmc && in ufshcd_change_power_mode()
4504 pwr_mode->gear_rx == hba->pwr_info.gear_rx && in ufshcd_change_power_mode()
4505 pwr_mode->gear_tx == hba->pwr_info.gear_tx && in ufshcd_change_power_mode()
4506 pwr_mode->lane_rx == hba->pwr_info.lane_rx && in ufshcd_change_power_mode()
4507 pwr_mode->lane_tx == hba->pwr_info.lane_tx && in ufshcd_change_power_mode()
4508 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx && in ufshcd_change_power_mode()
4509 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx && in ufshcd_change_power_mode()
4510 pwr_mode->hs_rate == hba->pwr_info.hs_rate) { in ufshcd_change_power_mode()
4511 dev_dbg(hba->dev, "%s: power already configured\n", __func__); in ufshcd_change_power_mode()
4517 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION, in ufshcd_change_power_mode()
4518 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION, in ufshcd_change_power_mode()
4519 * - PA_HSSERIES in ufshcd_change_power_mode()
4521 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx); in ufshcd_change_power_mode()
4523 pwr_mode->lane_rx); in ufshcd_change_power_mode()
4524 if (pwr_mode->pwr_rx == FASTAUTO_MODE || in ufshcd_change_power_mode()
4525 pwr_mode->pwr_rx == FAST_MODE) in ufshcd_change_power_mode()
4530 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx); in ufshcd_change_power_mode()
4532 pwr_mode->lane_tx); in ufshcd_change_power_mode()
4533 if (pwr_mode->pwr_tx == FASTAUTO_MODE || in ufshcd_change_power_mode()
4534 pwr_mode->pwr_tx == FAST_MODE) in ufshcd_change_power_mode()
4539 if (pwr_mode->pwr_rx == FASTAUTO_MODE || in ufshcd_change_power_mode()
4540 pwr_mode->pwr_tx == FASTAUTO_MODE || in ufshcd_change_power_mode()
4541 pwr_mode->pwr_rx == FAST_MODE || in ufshcd_change_power_mode()
4542 pwr_mode->pwr_tx == FAST_MODE) in ufshcd_change_power_mode()
4544 pwr_mode->hs_rate); in ufshcd_change_power_mode()
4546 if (!(hba->quirks & UFSHCD_QUIRK_SKIP_DEF_UNIPRO_TIMEOUT_SETTING)) { in ufshcd_change_power_mode()
4568 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4 in ufshcd_change_power_mode()
4569 | pwr_mode->pwr_tx); in ufshcd_change_power_mode()
4572 dev_err(hba->dev, in ufshcd_change_power_mode()
4575 memcpy(&hba->pwr_info, pwr_mode, in ufshcd_change_power_mode()
4583 * ufshcd_config_pwr_mode - configure a new power mode
4584 * @hba: per-adapter instance
4612 * ufshcd_complete_dev_init() - checks device readiness
4613 * @hba: per-adapter instance
4628 dev_err(hba->dev, in ufshcd_complete_dev_init()
4645 dev_err(hba->dev, in ufshcd_complete_dev_init()
4649 dev_err(hba->dev, in ufshcd_complete_dev_init()
4652 err = -EBUSY; in ufshcd_complete_dev_init()
4659 * ufshcd_make_hba_operational - Make UFS controller operational
4666 * 4. Configure run-stop-registers
4668 * Return: 0 on success, non-zero value on failure.
4680 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO); in ufshcd_make_hba_operational()
4685 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr), in ufshcd_make_hba_operational()
4687 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr), in ufshcd_make_hba_operational()
4689 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr), in ufshcd_make_hba_operational()
4691 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr), in ufshcd_make_hba_operational()
4707 dev_err(hba->dev, in ufshcd_make_hba_operational()
4709 err = -EIO; in ufshcd_make_hba_operational()
4717 * ufshcd_hba_stop - Send controller to reset state
4729 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_hba_stop()
4731 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_hba_stop()
4737 dev_err(hba->dev, "%s: Controller disable failed\n", __func__); in ufshcd_hba_stop()
4742 * ufshcd_hba_execute_hce - initialize the controller
4749 * Return: 0 on success, non-zero value on failure.
4771 * During initialization the HCE bit value changes from 1->0->1. in ufshcd_hba_execute_hce()
4779 ufshcd_delay_us(hba->vps->hba_enable_delay_us, 100); in ufshcd_hba_execute_hce()
4785 retry_inner--; in ufshcd_hba_execute_hce()
4787 dev_err(hba->dev, in ufshcd_hba_execute_hce()
4790 retry_outer--; in ufshcd_hba_execute_hce()
4793 return -EIO; in ufshcd_hba_execute_hce()
4810 if (hba->quirks & UFSHCI_QUIRK_BROKEN_HCE) { in ufshcd_hba_enable()
4818 dev_err(hba->dev, "DME_RESET failed\n"); in ufshcd_hba_enable()
4824 dev_err(hba->dev, "Enabling DME failed\n"); in ufshcd_hba_enable()
4859 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d", in ufshcd_disable_tx_lcc()
4880 e = &hba->ufs_stats.event[id]; in ufshcd_update_evt_hist()
4881 e->val[e->pos] = val; in ufshcd_update_evt_hist()
4882 e->tstamp[e->pos] = local_clock(); in ufshcd_update_evt_hist()
4883 e->cnt += 1; in ufshcd_update_evt_hist()
4884 e->pos = (e->pos + 1) % UFS_EVENT_HIST_LENGTH; in ufshcd_update_evt_hist()
4891 * ufshcd_link_startup - Initialize unipro link startup
4894 * Return: 0 for success, non-zero in case of failure.
4915 /* check if device is detected by inter-connect layer */ in ufshcd_link_startup()
4920 dev_err(hba->dev, "%s: Device not present\n", __func__); in ufshcd_link_startup()
4921 ret = -ENXIO; in ufshcd_link_startup()
4928 * succeeds. So reset the local Uni-Pro and try again. in ufshcd_link_startup()
4936 } while (ret && retries--); in ufshcd_link_startup()
4952 /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */ in ufshcd_link_startup()
4956 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) { in ufshcd_link_startup()
4972 dev_err(hba->dev, "link startup failed %d\n", ret); in ufshcd_link_startup()
4981 * ufshcd_verify_dev_init() - Verify device initialization
4982 * @hba: per-adapter instance
4998 mutex_lock(&hba->dev_cmd.lock); in ufshcd_verify_dev_init()
4999 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) { in ufshcd_verify_dev_init()
5001 hba->nop_out_timeout); in ufshcd_verify_dev_init()
5003 if (!err || err == -ETIMEDOUT) in ufshcd_verify_dev_init()
5006 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err); in ufshcd_verify_dev_init()
5008 mutex_unlock(&hba->dev_cmd.lock); in ufshcd_verify_dev_init()
5012 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err); in ufshcd_verify_dev_init()
5017 * ufshcd_setup_links - associate link b/w device wlun and other luns
5029 if (hba->ufs_device_wlun) { in ufshcd_setup_links()
5030 link = device_link_add(&sdev->sdev_gendev, in ufshcd_setup_links()
5031 &hba->ufs_device_wlun->sdev_gendev, in ufshcd_setup_links()
5034 dev_err(&sdev->sdev_gendev, "Failed establishing link - %s\n", in ufshcd_setup_links()
5035 dev_name(&hba->ufs_device_wlun->sdev_gendev)); in ufshcd_setup_links()
5038 hba->luns_avail--; in ufshcd_setup_links()
5040 if (hba->luns_avail == 1) { in ufshcd_setup_links()
5049 hba->luns_avail--; in ufshcd_setup_links()
5054 * ufshcd_lu_init - Initialize the relevant parameters of the LU
5055 * @hba: per-adapter instance
5061 u8 lun = ufshcd_scsi_to_upiu_lun(sdev->lun); in ufshcd_lu_init()
5062 u8 lun_qdepth = hba->nutrs; in ufshcd_lu_init()
5072 if (ret == -EOPNOTSUPP) in ufshcd_lu_init()
5081 * In per-LU queueing architecture, bLUQueueDepth will not be 0, then we will in ufshcd_lu_init()
5084 lun_qdepth = min_t(int, desc_buf[UNIT_DESC_PARAM_LU_Q_DEPTH], hba->nutrs); in ufshcd_lu_init()
5090 if (hba->dev_info.f_power_on_wp_en && lun < hba->dev_info.max_lu_supported && in ufshcd_lu_init()
5091 !hba->dev_info.is_lu_power_on_wp && in ufshcd_lu_init()
5093 hba->dev_info.is_lu_power_on_wp = true; in ufshcd_lu_init()
5098 hba->dev_info.b_advanced_rpmb_en = true; in ufshcd_lu_init()
5107 dev_dbg(hba->dev, "Set LU %x queue depth %d\n", lun, lun_qdepth); in ufshcd_lu_init()
5112 * ufshcd_slave_alloc - handle initial SCSI device configurations
5121 hba = shost_priv(sdev->host); in ufshcd_slave_alloc()
5124 sdev->use_10_for_ms = 1; in ufshcd_slave_alloc()
5127 sdev->set_dbd_for_ms = 1; in ufshcd_slave_alloc()
5130 sdev->allow_restart = 1; in ufshcd_slave_alloc()
5133 sdev->no_report_opcodes = 1; in ufshcd_slave_alloc()
5136 sdev->no_write_same = 1; in ufshcd_slave_alloc()
5146 * ufshcd_change_queue_depth - change queue depth
5156 return scsi_change_queue_depth(sdev, min(depth, sdev->host->can_queue)); in ufshcd_change_queue_depth()
5160 * ufshcd_slave_configure - adjust SCSI device configurations
5167 struct ufs_hba *hba = shost_priv(sdev->host); in ufshcd_slave_configure()
5168 struct request_queue *q = sdev->request_queue; in ufshcd_slave_configure()
5170 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1); in ufshcd_slave_configure()
5171 if (hba->quirks & UFSHCD_QUIRK_4KB_DMA_ALIGNMENT) in ufshcd_slave_configure()
5172 blk_queue_update_dma_alignment(q, SZ_4K - 1); in ufshcd_slave_configure()
5174 * Block runtime-pm until all consumers are added. in ufshcd_slave_configure()
5178 pm_runtime_get_noresume(&sdev->sdev_gendev); in ufshcd_slave_configure()
5180 sdev->rpm_autosuspend = 1; in ufshcd_slave_configure()
5182 * Do not print messages during runtime PM to avoid never-ending cycles in ufshcd_slave_configure()
5186 sdev->silence_suspend = 1; in ufshcd_slave_configure()
5194 * ufshcd_slave_destroy - remove SCSI device configurations
5202 hba = shost_priv(sdev->host); in ufshcd_slave_destroy()
5205 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) { in ufshcd_slave_destroy()
5206 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_slave_destroy()
5207 hba->ufs_device_wlun = NULL; in ufshcd_slave_destroy()
5208 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_slave_destroy()
5209 } else if (hba->ufs_device_wlun) { in ufshcd_slave_destroy()
5213 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_slave_destroy()
5214 if (hba->ufs_device_wlun) { in ufshcd_slave_destroy()
5215 supplier = &hba->ufs_device_wlun->sdev_gendev; in ufshcd_slave_destroy()
5218 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_slave_destroy()
5226 device_link_remove(&sdev->sdev_gendev, supplier); in ufshcd_slave_destroy()
5233 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
5266 * ufshcd_transfer_rsp_status - Get overall status of the response
5283 upiu_flags = lrbp->ucd_rsp_ptr->header.flags; in ufshcd_transfer_rsp_status()
5284 resid = be32_to_cpu(lrbp->ucd_rsp_ptr->sr.residual_transfer_count); in ufshcd_transfer_rsp_status()
5290 scsi_set_resid(lrbp->cmd, resid); in ufshcd_transfer_rsp_status()
5295 if (hba->quirks & UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR) { in ufshcd_transfer_rsp_status()
5296 if (lrbp->ucd_rsp_ptr->header.response || in ufshcd_transfer_rsp_status()
5297 lrbp->ucd_rsp_ptr->header.status) in ufshcd_transfer_rsp_status()
5303 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); in ufshcd_transfer_rsp_status()
5304 switch (ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr)) { in ufshcd_transfer_rsp_status()
5310 scsi_status = lrbp->ucd_rsp_ptr->header.status; in ufshcd_transfer_rsp_status()
5325 if (!hba->pm_op_in_progress && in ufshcd_transfer_rsp_status()
5327 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr)) in ufshcd_transfer_rsp_status()
5329 schedule_work(&hba->eeh_work); in ufshcd_transfer_rsp_status()
5334 dev_err(hba->dev, in ufshcd_transfer_rsp_status()
5338 dev_err(hba->dev, in ufshcd_transfer_rsp_status()
5362 dev_err(hba->dev, in ufshcd_transfer_rsp_status()
5364 ocs, lrbp->task_tag); in ufshcd_transfer_rsp_status()
5371 (host_byte(result) != DID_REQUEUE) && !hba->silence_err_logs) in ufshcd_transfer_rsp_status()
5372 ufshcd_print_tr(hba, lrbp->task_tag, true); in ufshcd_transfer_rsp_status()
5386 if (hba->active_uic_cmd && in ufshcd_is_auto_hibern8_error()
5387 (hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_ENTER || in ufshcd_is_auto_hibern8_error()
5388 hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_EXIT)) in ufshcd_is_auto_hibern8_error()
5395 * ufshcd_uic_cmd_compl - handle completion of uic command
5400 * IRQ_HANDLED - If interrupt is valid
5401 * IRQ_NONE - If invalid interrupt
5407 spin_lock(hba->host->host_lock); in ufshcd_uic_cmd_compl()
5409 hba->errors |= (UFSHCD_UIC_HIBERN8_MASK & intr_status); in ufshcd_uic_cmd_compl()
5411 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) { in ufshcd_uic_cmd_compl()
5412 hba->active_uic_cmd->argument2 |= in ufshcd_uic_cmd_compl()
5414 hba->active_uic_cmd->argument3 = in ufshcd_uic_cmd_compl()
5416 if (!hba->uic_async_done) in ufshcd_uic_cmd_compl()
5417 hba->active_uic_cmd->cmd_active = 0; in ufshcd_uic_cmd_compl()
5418 complete(&hba->active_uic_cmd->done); in ufshcd_uic_cmd_compl()
5422 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done) { in ufshcd_uic_cmd_compl()
5423 hba->active_uic_cmd->cmd_active = 0; in ufshcd_uic_cmd_compl()
5424 complete(hba->uic_async_done); in ufshcd_uic_cmd_compl()
5429 ufshcd_add_uic_command_trace(hba, hba->active_uic_cmd, in ufshcd_uic_cmd_compl()
5431 spin_unlock(hba->host->host_lock); in ufshcd_uic_cmd_compl()
5439 struct scsi_cmnd *cmd = lrbp->cmd; in ufshcd_release_scsi_cmd()
5447 * ufshcd_compl_one_cqe - handle a completion queue entry
5459 lrbp = &hba->lrb[task_tag]; in ufshcd_compl_one_cqe()
5460 lrbp->compl_time_stamp = ktime_get(); in ufshcd_compl_one_cqe()
5461 lrbp->compl_time_stamp_local_clock = local_clock(); in ufshcd_compl_one_cqe()
5462 cmd = lrbp->cmd; in ufshcd_compl_one_cqe()
5467 cmd->result = ufshcd_transfer_rsp_status(hba, lrbp, cqe); in ufshcd_compl_one_cqe()
5471 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE || in ufshcd_compl_one_cqe()
5472 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) { in ufshcd_compl_one_cqe()
5473 if (hba->dev_cmd.complete) { in ufshcd_compl_one_cqe()
5475 ocs = le32_to_cpu(cqe->status) & MASK_OCS; in ufshcd_compl_one_cqe()
5476 lrbp->utr_descriptor_ptr->header.ocs = ocs; in ufshcd_compl_one_cqe()
5478 complete(hba->dev_cmd.complete); in ufshcd_compl_one_cqe()
5485 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
5494 for_each_set_bit(tag, &completed_reqs, hba->nutrs) in __ufshcd_transfer_req_compl()
5500 UFSHCD_POLL_FROM_INTERRUPT_CONTEXT = -1
5508 for_each_set_bit(tag, completed_reqs, hba->nutrs) { in ufshcd_clear_polled()
5509 struct scsi_cmnd *cmd = hba->lrb[tag].cmd; in ufshcd_clear_polled()
5513 if (scsi_cmd_to_rq(cmd)->cmd_flags & REQ_POLLED) in ufshcd_clear_polled()
5530 hwq = &hba->uhq[queue_num]; in ufshcd_poll()
5535 spin_lock_irqsave(&hba->outstanding_lock, flags); in ufshcd_poll()
5537 completed_reqs = ~tr_doorbell & hba->outstanding_reqs; in ufshcd_poll()
5538 WARN_ONCE(completed_reqs & ~hba->outstanding_reqs, in ufshcd_poll()
5540 hba->outstanding_reqs); in ufshcd_poll()
5545 hba->outstanding_reqs &= ~completed_reqs; in ufshcd_poll()
5546 spin_unlock_irqrestore(&hba->outstanding_lock, flags); in ufshcd_poll()
5555 * ufshcd_mcq_compl_pending_transfer - MCQ mode function. It is
5575 for (tag = 0; tag < hba->nutrs; tag++) { in ufshcd_mcq_compl_pending_transfer()
5576 lrbp = &hba->lrb[tag]; in ufshcd_mcq_compl_pending_transfer()
5577 cmd = lrbp->cmd; in ufshcd_mcq_compl_pending_transfer()
5579 test_bit(SCMD_STATE_COMPLETE, &cmd->state)) in ufshcd_mcq_compl_pending_transfer()
5584 hwq = &hba->uhq[hwq_num]; in ufshcd_mcq_compl_pending_transfer()
5592 if (cmd && !test_bit(SCMD_STATE_COMPLETE, &cmd->state)) { in ufshcd_mcq_compl_pending_transfer()
5593 spin_lock_irqsave(&hwq->cq_lock, flags); in ufshcd_mcq_compl_pending_transfer()
5597 spin_unlock_irqrestore(&hwq->cq_lock, flags); in ufshcd_mcq_compl_pending_transfer()
5606 * ufshcd_transfer_req_compl - handle SCSI and query command completion
5610 * IRQ_HANDLED - If interrupt is valid
5611 * IRQ_NONE - If invalid interrupt
5623 !(hba->quirks & UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR)) in ufshcd_transfer_req_compl()
5633 ufshcd_poll(hba->host, UFSHCD_POLL_FROM_INTERRUPT_CONTEXT); in ufshcd_transfer_req_compl()
5649 mutex_lock(&hba->ee_ctrl_mutex); in ufshcd_write_ee_control()
5650 err = __ufshcd_write_ee_control(hba, hba->ee_ctrl_mask); in ufshcd_write_ee_control()
5651 mutex_unlock(&hba->ee_ctrl_mutex); in ufshcd_write_ee_control()
5653 dev_err(hba->dev, "%s: failed to write ee control %d\n", in ufshcd_write_ee_control()
5664 mutex_lock(&hba->ee_ctrl_mutex); in ufshcd_update_ee_control()
5667 if (ee_ctrl_mask != hba->ee_ctrl_mask) in ufshcd_update_ee_control()
5671 hba->ee_ctrl_mask = ee_ctrl_mask; in ufshcd_update_ee_control()
5674 mutex_unlock(&hba->ee_ctrl_mutex); in ufshcd_update_ee_control()
5679 * ufshcd_disable_ee - disable exception event
5680 * @hba: per-adapter instance
5686 * Return: zero on success, non-zero error value on failure.
5694 * ufshcd_enable_ee - enable exception event
5695 * @hba: per-adapter instance
5701 * Return: zero on success, non-zero error value on failure.
5709 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
5710 * @hba: per-adapter instance
5717 * Return: zero on success, non-zero on failure.
5723 if (hba->auto_bkops_enabled) in ufshcd_enable_auto_bkops()
5729 dev_err(hba->dev, "%s: failed to enable bkops %d\n", in ufshcd_enable_auto_bkops()
5734 hba->auto_bkops_enabled = true; in ufshcd_enable_auto_bkops()
5735 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Enabled"); in ufshcd_enable_auto_bkops()
5740 dev_err(hba->dev, "%s: failed to disable exception event %d\n", in ufshcd_enable_auto_bkops()
5747 * ufshcd_disable_auto_bkops - block device in doing background operations
5748 * @hba: per-adapter instance
5752 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
5756 * Return: zero on success, non-zero on failure.
5762 if (!hba->auto_bkops_enabled) in ufshcd_disable_auto_bkops()
5771 dev_err(hba->dev, "%s: failed to enable exception event %d\n", in ufshcd_disable_auto_bkops()
5779 dev_err(hba->dev, "%s: failed to disable bkops %d\n", in ufshcd_disable_auto_bkops()
5785 hba->auto_bkops_enabled = false; in ufshcd_disable_auto_bkops()
5786 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Disabled"); in ufshcd_disable_auto_bkops()
5787 hba->is_urgent_bkops_lvl_checked = false; in ufshcd_disable_auto_bkops()
5793 * ufshcd_force_reset_auto_bkops - force reset auto bkops state
5798 * as well. This function would change the auto-bkops state based on
5804 hba->auto_bkops_enabled = false; in ufshcd_force_reset_auto_bkops()
5805 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS; in ufshcd_force_reset_auto_bkops()
5808 hba->auto_bkops_enabled = true; in ufshcd_force_reset_auto_bkops()
5809 hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS; in ufshcd_force_reset_auto_bkops()
5812 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT; in ufshcd_force_reset_auto_bkops()
5813 hba->is_urgent_bkops_lvl_checked = false; in ufshcd_force_reset_auto_bkops()
5823 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
5824 * @hba: per-adapter instance
5832 * Return: 0 for success, non-zero in case of failure.
5834 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
5846 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", in ufshcd_bkops_ctrl()
5850 dev_err(hba->dev, "%s: invalid BKOPS status %d\n", in ufshcd_bkops_ctrl()
5852 err = -EINVAL; in ufshcd_bkops_ctrl()
5865 * ufshcd_urgent_bkops - handle urgent bkops exception event
5866 * @hba: per-adapter instance
5878 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl); in ufshcd_urgent_bkops()
5892 if (hba->is_urgent_bkops_lvl_checked) in ufshcd_bkops_exception_event_handler()
5897 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", in ufshcd_bkops_exception_event_handler()
5909 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n", in ufshcd_bkops_exception_event_handler()
5912 hba->urgent_bkops_lvl = curr_status; in ufshcd_bkops_exception_event_handler()
5913 hba->is_urgent_bkops_lvl_checked = true; in ufshcd_bkops_exception_event_handler()
5920 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n", in ufshcd_bkops_exception_event_handler()
5932 dev_info(hba->dev, "exception Tcase %d\n", value - 80); in ufshcd_temp_exception_event_handler()
5957 hba->dev_info.wb_enabled == enable) in ufshcd_wb_toggle()
5962 dev_err(hba->dev, "%s: Write Booster %s failed %d\n", in ufshcd_wb_toggle()
5967 hba->dev_info.wb_enabled = enable; in ufshcd_wb_toggle()
5968 dev_dbg(hba->dev, "%s: Write Booster %s\n", in ufshcd_wb_toggle()
5982 dev_err(hba->dev, "%s: WB-Buf Flush during H8 %s failed %d\n", in ufshcd_wb_toggle_buf_flush_during_h8()
5986 dev_dbg(hba->dev, "%s: WB-Buf Flush during H8 %s\n", in ufshcd_wb_toggle_buf_flush_during_h8()
5995 hba->dev_info.wb_buf_flush_enabled == enable) in ufshcd_wb_toggle_buf_flush()
6000 dev_err(hba->dev, "%s: WB-Buf Flush %s failed %d\n", in ufshcd_wb_toggle_buf_flush()
6005 hba->dev_info.wb_buf_flush_enabled = enable; in ufshcd_wb_toggle_buf_flush()
6006 dev_dbg(hba->dev, "%s: WB-Buf Flush %s\n", in ufshcd_wb_toggle_buf_flush()
6024 dev_err(hba->dev, "%s: dCurWriteBoosterBufferSize read failed %d\n", in ufshcd_wb_presrv_usrspc_keep_vcc_on()
6030 dev_info(hba->dev, "dCurWBBuf: %d WB disabled until free-space is available\n", in ufshcd_wb_presrv_usrspc_keep_vcc_on()
6035 return avail_buf < hba->vps->wb_flush_threshold; in ufshcd_wb_presrv_usrspc_keep_vcc_on()
6045 hba->caps &= ~UFSHCD_CAP_WB_EN; in ufshcd_wb_force_disable()
6047 dev_info(hba->dev, "%s: WB force disabled\n", __func__); in ufshcd_wb_force_disable()
6061 dev_err(hba->dev, in ufshcd_is_wb_buf_lifetime_available()
6068 dev_err(hba->dev, "%s: WB buf lifetime is exhausted 0x%02X\n", in ufshcd_is_wb_buf_lifetime_available()
6073 dev_dbg(hba->dev, "%s: WB buf lifetime is 0x%02X\n", in ufshcd_is_wb_buf_lifetime_available()
6095 * With user-space reduction enabled, it's enough to enable flush in ufshcd_wb_need_flush()
6098 * With user-space preserved enabled, the current-buffer in ufshcd_wb_need_flush()
6109 dev_warn(hba->dev, "%s: dAvailableWriteBoosterBufferSize read failed %d\n", in ufshcd_wb_need_flush()
6114 if (!hba->dev_info.b_presrv_uspc_en) in ufshcd_wb_need_flush()
6136 * ufshcd_exception_event_handler - handle exceptions raised by device
6152 dev_err(hba->dev, "%s: failed to get exception status %d\n", in ufshcd_exception_event_handler()
6157 trace_ufshcd_exception_event(dev_name(hba->dev), status); in ufshcd_exception_event_handler()
6159 if (status & hba->ee_drv_mask & MASK_EE_URGENT_BKOPS) in ufshcd_exception_event_handler()
6162 if (status & hba->ee_drv_mask & MASK_EE_URGENT_TEMP) in ufshcd_exception_event_handler()
6170 /* Complete requests that have door-bell cleared */
6182 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
6184 * @hba: per-adapter instance
6193 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_quirk_dl_nac_errors()
6198 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR)) in ufshcd_quirk_dl_nac_errors()
6201 if ((hba->saved_err & DEVICE_FATAL_ERROR) || in ufshcd_quirk_dl_nac_errors()
6202 ((hba->saved_err & UIC_ERROR) && in ufshcd_quirk_dl_nac_errors()
6203 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR))) in ufshcd_quirk_dl_nac_errors()
6206 if ((hba->saved_err & UIC_ERROR) && in ufshcd_quirk_dl_nac_errors()
6207 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) { in ufshcd_quirk_dl_nac_errors()
6212 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_quirk_dl_nac_errors()
6214 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_quirk_dl_nac_errors()
6220 if ((hba->saved_err & INT_FATAL_ERRORS) || in ufshcd_quirk_dl_nac_errors()
6221 ((hba->saved_err & UIC_ERROR) && in ufshcd_quirk_dl_nac_errors()
6222 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR))) in ufshcd_quirk_dl_nac_errors()
6228 * - If we don't get any response then do error recovery. in ufshcd_quirk_dl_nac_errors()
6229 * - If we get response then clear the DL NAC error bit. in ufshcd_quirk_dl_nac_errors()
6232 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_quirk_dl_nac_errors()
6234 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_quirk_dl_nac_errors()
6240 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR) in ufshcd_quirk_dl_nac_errors()
6241 hba->saved_err &= ~UIC_ERROR; in ufshcd_quirk_dl_nac_errors()
6243 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR; in ufshcd_quirk_dl_nac_errors()
6244 if (!hba->saved_uic_err) in ufshcd_quirk_dl_nac_errors()
6248 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_quirk_dl_nac_errors()
6255 return (hba->saved_uic_err & UFSHCD_UIC_DL_PA_INIT_ERROR) || in ufshcd_is_saved_err_fatal()
6256 (hba->saved_err & (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK)); in ufshcd_is_saved_err_fatal()
6261 lockdep_assert_held(hba->host->host_lock); in ufshcd_schedule_eh_work()
6264 if (hba->ufshcd_state != UFSHCD_STATE_ERROR) { in ufshcd_schedule_eh_work()
6265 if (hba->force_reset || ufshcd_is_link_broken(hba) || in ufshcd_schedule_eh_work()
6267 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED_FATAL; in ufshcd_schedule_eh_work()
6269 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED_NON_FATAL; in ufshcd_schedule_eh_work()
6270 queue_work(hba->eh_wq, &hba->eh_work); in ufshcd_schedule_eh_work()
6276 spin_lock_irq(hba->host->host_lock); in ufshcd_force_error_recovery()
6277 hba->force_reset = true; in ufshcd_force_error_recovery()
6279 spin_unlock_irq(hba->host->host_lock); in ufshcd_force_error_recovery()
6284 mutex_lock(&hba->wb_mutex); in ufshcd_clk_scaling_allow()
6285 down_write(&hba->clk_scaling_lock); in ufshcd_clk_scaling_allow()
6286 hba->clk_scaling.is_allowed = allow; in ufshcd_clk_scaling_allow()
6287 up_write(&hba->clk_scaling_lock); in ufshcd_clk_scaling_allow()
6288 mutex_unlock(&hba->wb_mutex); in ufshcd_clk_scaling_allow()
6294 if (hba->clk_scaling.is_enabled) in ufshcd_clk_scaling_suspend()
6299 if (hba->clk_scaling.is_enabled) in ufshcd_clk_scaling_suspend()
6307 if (pm_runtime_status_suspended(&hba->ufs_device_wlun->sdev_gendev) || in ufshcd_err_handling_prepare()
6308 hba->is_sys_suspended) { in ufshcd_err_handling_prepare()
6319 ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq); in ufshcd_err_handling_prepare()
6320 ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2); in ufshcd_err_handling_prepare()
6324 pm_op = hba->is_sys_suspended ? UFS_SYSTEM_PM : UFS_RUNTIME_PM; in ufshcd_err_handling_prepare()
6329 hba->clk_scaling.is_enabled) in ufshcd_err_handling_prepare()
6335 blk_mq_wait_quiesce_done(&hba->host->tag_set); in ufshcd_err_handling_prepare()
6336 cancel_work_sync(&hba->eeh_work); in ufshcd_err_handling_prepare()
6350 return (!hba->is_powered || hba->shutting_down || in ufshcd_err_handling_should_stop()
6351 !hba->ufs_device_wlun || in ufshcd_err_handling_should_stop()
6352 hba->ufshcd_state == UFSHCD_STATE_ERROR || in ufshcd_err_handling_should_stop()
6353 (!(hba->saved_err || hba->saved_uic_err || hba->force_reset || in ufshcd_err_handling_should_stop()
6360 struct Scsi_Host *shost = hba->host; in ufshcd_recover_pm_error()
6365 hba->is_sys_suspended = false; in ufshcd_recover_pm_error()
6370 ret = pm_runtime_set_active(&hba->ufs_device_wlun->sdev_gendev); in ufshcd_recover_pm_error()
6374 ret = pm_runtime_set_active(hba->dev); in ufshcd_recover_pm_error()
6383 q = sdev->request_queue; in ufshcd_recover_pm_error()
6384 if (q->dev && (q->rpm_status == RPM_SUSPENDED || in ufshcd_recover_pm_error()
6385 q->rpm_status == RPM_SUSPENDING)) in ufshcd_recover_pm_error()
6386 pm_request_resume(q->dev); in ufshcd_recover_pm_error()
6398 struct ufs_pa_layer_attr *pwr_info = &hba->pwr_info; in ufshcd_is_pwr_mode_restore_needed()
6403 if (pwr_info->pwr_rx != ((mode >> PWRMODE_RX_OFFSET) & PWRMODE_MASK)) in ufshcd_is_pwr_mode_restore_needed()
6406 if (pwr_info->pwr_tx != (mode & PWRMODE_MASK)) in ufshcd_is_pwr_mode_restore_needed()
6415 u32 tag = rq->tag; in ufshcd_abort_one()
6417 struct scsi_device *sdev = cmd->device; in ufshcd_abort_one()
6418 struct Scsi_Host *shost = sdev->host; in ufshcd_abort_one()
6420 struct ufshcd_lrb *lrbp = &hba->lrb[tag]; in ufshcd_abort_one()
6425 dev_err(hba->dev, "Aborting tag %d / CDB %#02x %s\n", tag, in ufshcd_abort_one()
6426 hba->lrb[tag].cmd ? hba->lrb[tag].cmd->cmnd[0] : -1, in ufshcd_abort_one()
6431 hwq = ufshcd_mcq_req_to_hwq(hba, scsi_cmd_to_rq(lrbp->cmd)); in ufshcd_abort_one()
6434 spin_lock_irqsave(&hwq->cq_lock, flags); in ufshcd_abort_one()
6435 if (ufshcd_cmd_inflight(lrbp->cmd)) in ufshcd_abort_one()
6437 spin_unlock_irqrestore(&hwq->cq_lock, flags); in ufshcd_abort_one()
6444 * ufshcd_abort_all - Abort all pending commands.
6453 blk_mq_tagset_busy_iter(&hba->host->tag_set, ufshcd_abort_one, &ret); in ufshcd_abort_all()
6458 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) { in ufshcd_abort_all()
6472 * ufshcd_err_handler - handle UFS errors that require s/w attention
6486 dev_info(hba->dev, in ufshcd_err_handler()
6488 __func__, ufshcd_state_name[hba->ufshcd_state], in ufshcd_err_handler()
6489 hba->is_powered, hba->shutting_down, hba->saved_err, in ufshcd_err_handler()
6490 hba->saved_uic_err, hba->force_reset, in ufshcd_err_handler()
6493 down(&hba->host_sem); in ufshcd_err_handler()
6494 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_err_handler()
6496 if (hba->ufshcd_state != UFSHCD_STATE_ERROR) in ufshcd_err_handler()
6497 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; in ufshcd_err_handler()
6498 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_err_handler()
6499 up(&hba->host_sem); in ufshcd_err_handler()
6503 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_err_handler()
6505 /* Complete requests that have door-bell cleared by h/w */ in ufshcd_err_handler()
6507 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_err_handler()
6512 if (hba->ufshcd_state != UFSHCD_STATE_ERROR) in ufshcd_err_handler()
6513 hba->ufshcd_state = UFSHCD_STATE_RESET; in ufshcd_err_handler()
6521 if ((hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) && in ufshcd_err_handler()
6522 !hba->force_reset) { in ufshcd_err_handler()
6525 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_err_handler()
6528 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_err_handler()
6533 if ((hba->saved_err & (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK)) || in ufshcd_err_handler()
6534 (hba->saved_uic_err && in ufshcd_err_handler()
6535 (hba->saved_uic_err != UFSHCD_UIC_PA_GENERIC_ERROR))) { in ufshcd_err_handler()
6536 bool pr_prdt = !!(hba->saved_err & SYSTEM_BUS_FATAL_ERROR); in ufshcd_err_handler()
6538 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_err_handler()
6542 ufshcd_print_tmrs(hba, hba->outstanding_tasks); in ufshcd_err_handler()
6544 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_err_handler()
6552 if (hba->force_reset || ufshcd_is_link_broken(hba) || in ufshcd_err_handler()
6554 ((hba->saved_err & UIC_ERROR) && in ufshcd_err_handler()
6555 (hba->saved_uic_err & (UFSHCD_UIC_DL_NAC_RECEIVED_ERROR | in ufshcd_err_handler()
6565 if (hba->saved_uic_err & UFSHCD_UIC_PA_GENERIC_ERROR) { in ufshcd_err_handler()
6566 hba->saved_uic_err &= ~UFSHCD_UIC_PA_GENERIC_ERROR; in ufshcd_err_handler()
6567 if (!hba->saved_uic_err) in ufshcd_err_handler()
6568 hba->saved_err &= ~UIC_ERROR; in ufshcd_err_handler()
6569 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_err_handler()
6572 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_err_handler()
6573 if (!hba->saved_err && !needs_restore) in ufshcd_err_handler()
6577 hba->silence_err_logs = true; in ufshcd_err_handler()
6579 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_err_handler()
6583 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_err_handler()
6584 hba->silence_err_logs = false; in ufshcd_err_handler()
6593 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_err_handler()
6598 down_write(&hba->clk_scaling_lock); in ufshcd_err_handler()
6599 hba->force_pmc = true; in ufshcd_err_handler()
6600 pmc_err = ufshcd_config_pwr_mode(hba, &(hba->pwr_info)); in ufshcd_err_handler()
6603 dev_err(hba->dev, "%s: Failed to restore power mode, err = %d\n", in ufshcd_err_handler()
6606 hba->force_pmc = false; in ufshcd_err_handler()
6608 up_write(&hba->clk_scaling_lock); in ufshcd_err_handler()
6609 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_err_handler()
6617 hba->force_reset = false; in ufshcd_err_handler()
6618 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_err_handler()
6621 dev_err(hba->dev, "%s: reset and restore failed with err %d\n", in ufshcd_err_handler()
6625 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_err_handler()
6630 if (hba->ufshcd_state == UFSHCD_STATE_RESET) in ufshcd_err_handler()
6631 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; in ufshcd_err_handler()
6632 if (hba->saved_err || hba->saved_uic_err) in ufshcd_err_handler()
6633 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x", in ufshcd_err_handler()
6634 __func__, hba->saved_err, hba->saved_uic_err); in ufshcd_err_handler()
6637 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL && in ufshcd_err_handler()
6638 hba->ufshcd_state != UFSHCD_STATE_ERROR) { in ufshcd_err_handler()
6639 if (--retries) in ufshcd_err_handler()
6641 hba->ufshcd_state = UFSHCD_STATE_ERROR; in ufshcd_err_handler()
6644 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_err_handler()
6646 up(&hba->host_sem); in ufshcd_err_handler()
6648 dev_info(hba->dev, "%s finished; HBA state %s\n", __func__, in ufshcd_err_handler()
6649 ufshcd_state_name[hba->ufshcd_state]); in ufshcd_err_handler()
6653 * ufshcd_update_uic_error - check and set fatal UIC error flags.
6654 * @hba: per-adapter instance
6657 * IRQ_HANDLED - If interrupt is valid
6658 * IRQ_NONE - If invalid interrupt
6675 dev_dbg(hba->dev, "%s: UIC Lane error reported\n", in ufshcd_update_uic_error()
6682 hba->uic_error |= UFSHCD_UIC_PA_GENERIC_ERROR; in ufshcd_update_uic_error()
6683 if (hba->uic_async_done && hba->active_uic_cmd) in ufshcd_update_uic_error()
6684 cmd = hba->active_uic_cmd; in ufshcd_update_uic_error()
6689 if (cmd && (cmd->command == UIC_CMD_DME_SET)) in ufshcd_update_uic_error()
6690 hba->uic_error &= ~UFSHCD_UIC_PA_GENERIC_ERROR; in ufshcd_update_uic_error()
6702 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR; in ufshcd_update_uic_error()
6703 else if (hba->dev_quirks & in ufshcd_update_uic_error()
6706 hba->uic_error |= in ufshcd_update_uic_error()
6709 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR; in ufshcd_update_uic_error()
6719 hba->uic_error |= UFSHCD_UIC_NL_ERROR; in ufshcd_update_uic_error()
6727 hba->uic_error |= UFSHCD_UIC_TL_ERROR; in ufshcd_update_uic_error()
6735 hba->uic_error |= UFSHCD_UIC_DME_ERROR; in ufshcd_update_uic_error()
6739 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n", in ufshcd_update_uic_error()
6740 __func__, hba->uic_error); in ufshcd_update_uic_error()
6745 * ufshcd_check_errors - Check for errors that need s/w attention
6746 * @hba: per-adapter instance
6750 * IRQ_HANDLED - If interrupt is valid
6751 * IRQ_NONE - If invalid interrupt
6758 spin_lock(hba->host->host_lock); in ufshcd_check_errors()
6759 hba->errors |= UFSHCD_ERROR_MASK & intr_status; in ufshcd_check_errors()
6761 if (hba->errors & INT_FATAL_ERRORS) { in ufshcd_check_errors()
6763 hba->errors); in ufshcd_check_errors()
6767 if (hba->errors & UIC_ERROR) { in ufshcd_check_errors()
6768 hba->uic_error = 0; in ufshcd_check_errors()
6770 if (hba->uic_error) in ufshcd_check_errors()
6774 if (hba->errors & UFSHCD_UIC_HIBERN8_MASK) { in ufshcd_check_errors()
6775 dev_err(hba->dev, in ufshcd_check_errors()
6776 "%s: Auto Hibern8 %s failed - status: 0x%08x, upmcrs: 0x%08x\n", in ufshcd_check_errors()
6777 __func__, (hba->errors & UIC_HIBERNATE_ENTER) ? in ufshcd_check_errors()
6779 hba->errors, ufshcd_get_upmcrs(hba)); in ufshcd_check_errors()
6781 hba->errors); in ufshcd_check_errors()
6791 hba->saved_err |= hba->errors; in ufshcd_check_errors()
6792 hba->saved_uic_err |= hba->uic_error; in ufshcd_check_errors()
6795 if ((hba->saved_err & in ufshcd_check_errors()
6797 (hba->saved_uic_err && in ufshcd_check_errors()
6798 (hba->saved_uic_err != UFSHCD_UIC_PA_GENERIC_ERROR))) { in ufshcd_check_errors()
6799 dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x\n", in ufshcd_check_errors()
6800 __func__, hba->saved_err, in ufshcd_check_errors()
6801 hba->saved_uic_err); in ufshcd_check_errors()
6810 * if (!queue_eh_work) - in ufshcd_check_errors()
6811 * Other errors are either non-fatal where host recovers in ufshcd_check_errors()
6815 hba->errors = 0; in ufshcd_check_errors()
6816 hba->uic_error = 0; in ufshcd_check_errors()
6817 spin_unlock(hba->host->host_lock); in ufshcd_check_errors()
6822 * ufshcd_tmc_handler - handle task management function completion
6826 * IRQ_HANDLED - If interrupt is valid
6827 * IRQ_NONE - If invalid interrupt
6835 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_tmc_handler()
6837 issued = hba->outstanding_tasks & ~pending; in ufshcd_tmc_handler()
6838 for_each_set_bit(tag, &issued, hba->nutmrs) { in ufshcd_tmc_handler()
6839 struct request *req = hba->tmf_rqs[tag]; in ufshcd_tmc_handler()
6840 struct completion *c = req->end_io_data; in ufshcd_tmc_handler()
6845 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_tmc_handler()
6851 * ufshcd_handle_mcq_cq_events - handle MCQ completion queue events
6866 outstanding_cqs = (1U << hba->nr_hw_queues) - 1; in ufshcd_handle_mcq_cq_events()
6869 nr_queues = hba->nr_hw_queues - hba->nr_queues[HCTX_TYPE_POLL]; in ufshcd_handle_mcq_cq_events()
6871 hwq = &hba->uhq[i]; in ufshcd_handle_mcq_cq_events()
6885 * ufshcd_sl_intr - Interrupt service routine
6890 * IRQ_HANDLED - If interrupt is valid
6891 * IRQ_NONE - If invalid interrupt
6900 if (intr_status & UFSHCD_ERROR_MASK || hba->errors) in ufshcd_sl_intr()
6916 * ufshcd_intr - Main interrupt service routine
6921 * IRQ_HANDLED - If interrupt is valid
6922 * IRQ_NONE - If invalid interrupt
6929 int retries = hba->nutrs; in ufshcd_intr()
6932 hba->ufs_stats.last_intr_status = intr_status; in ufshcd_intr()
6933 hba->ufs_stats.last_intr_ts = local_clock(); in ufshcd_intr()
6936 * There could be max of hba->nutrs reqs in flight and in worst case in ufshcd_intr()
6941 while (intr_status && retries--) { in ufshcd_intr()
6953 hba->outstanding_reqs) && !ufshcd_eh_in_progress(hba)) { in ufshcd_intr()
6954 dev_err(hba->dev, "%s: Unhandled interrupt 0x%08x (0x%08x, 0x%08x)\n", in ufshcd_intr()
6957 hba->ufs_stats.last_intr_status, in ufshcd_intr()
6971 if (!test_bit(tag, &hba->outstanding_tasks)) in ufshcd_clear_tm_cmd()
6974 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_clear_tm_cmd()
6976 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_clear_tm_cmd()
6983 dev_err(hba->dev, "Clearing task management function with tag %d %s\n", in ufshcd_clear_tm_cmd()
6993 struct request_queue *q = hba->tmf_queue; in __ufshcd_issue_tm_cmd()
6994 struct Scsi_Host *host = hba->host; in __ufshcd_issue_tm_cmd()
7007 req->end_io_data = &wait; in __ufshcd_issue_tm_cmd()
7010 spin_lock_irqsave(host->host_lock, flags); in __ufshcd_issue_tm_cmd()
7012 task_tag = req->tag; in __ufshcd_issue_tm_cmd()
7013 WARN_ONCE(task_tag < 0 || task_tag >= hba->nutmrs, "Invalid tag %d\n", in __ufshcd_issue_tm_cmd()
7015 hba->tmf_rqs[req->tag] = req; in __ufshcd_issue_tm_cmd()
7016 treq->upiu_req.req_header.task_tag = task_tag; in __ufshcd_issue_tm_cmd()
7018 memcpy(hba->utmrdl_base_addr + task_tag, treq, sizeof(*treq)); in __ufshcd_issue_tm_cmd()
7022 __set_bit(task_tag, &hba->outstanding_tasks); in __ufshcd_issue_tm_cmd()
7028 spin_unlock_irqrestore(host->host_lock, flags); in __ufshcd_issue_tm_cmd()
7037 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n", in __ufshcd_issue_tm_cmd()
7040 dev_WARN(hba->dev, "%s: unable to clear tm cmd (slot %d) after timeout\n", in __ufshcd_issue_tm_cmd()
7042 err = -ETIMEDOUT; in __ufshcd_issue_tm_cmd()
7045 memcpy(treq, hba->utmrdl_base_addr + task_tag, sizeof(*treq)); in __ufshcd_issue_tm_cmd()
7050 spin_lock_irqsave(hba->host->host_lock, flags); in __ufshcd_issue_tm_cmd()
7051 hba->tmf_rqs[req->tag] = NULL; in __ufshcd_issue_tm_cmd()
7052 __clear_bit(task_tag, &hba->outstanding_tasks); in __ufshcd_issue_tm_cmd()
7053 spin_unlock_irqrestore(hba->host->host_lock, flags); in __ufshcd_issue_tm_cmd()
7062 * ufshcd_issue_tm_cmd - issues task management commands to controller
7069 * Return: non-zero value on error, zero on success.
7095 if (err == -ETIMEDOUT) in ufshcd_issue_tm_cmd()
7100 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", in ufshcd_issue_tm_cmd()
7109 * ufshcd_issue_devman_upiu_cmd - API for sending "utrd" type requests
7110 * @hba: per-adapter instance
7118 * Those type of requests uses UTP Transfer Request Descriptor - utrd.
7123 * the caller is expected to hold the hba->dev_cmd.lock mutex.
7135 const u32 tag = hba->reserved_slot; in ufshcd_issue_devman_upiu_cmd()
7140 /* Protects use of hba->reserved_slot. */ in ufshcd_issue_devman_upiu_cmd()
7141 lockdep_assert_held(&hba->dev_cmd.lock); in ufshcd_issue_devman_upiu_cmd()
7143 down_read(&hba->clk_scaling_lock); in ufshcd_issue_devman_upiu_cmd()
7145 lrbp = &hba->lrb[tag]; in ufshcd_issue_devman_upiu_cmd()
7146 lrbp->cmd = NULL; in ufshcd_issue_devman_upiu_cmd()
7147 lrbp->task_tag = tag; in ufshcd_issue_devman_upiu_cmd()
7148 lrbp->lun = 0; in ufshcd_issue_devman_upiu_cmd()
7149 lrbp->intr_cmd = true; in ufshcd_issue_devman_upiu_cmd()
7151 hba->dev_cmd.type = cmd_type; in ufshcd_issue_devman_upiu_cmd()
7153 if (hba->ufs_version <= ufshci_version(1, 1)) in ufshcd_issue_devman_upiu_cmd()
7154 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE; in ufshcd_issue_devman_upiu_cmd()
7156 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE; in ufshcd_issue_devman_upiu_cmd()
7159 req_upiu->header.task_tag = tag; in ufshcd_issue_devman_upiu_cmd()
7164 memcpy(lrbp->ucd_req_ptr, req_upiu, sizeof(*lrbp->ucd_req_ptr)); in ufshcd_issue_devman_upiu_cmd()
7170 memcpy(lrbp->ucd_req_ptr + 1, desc_buff, *buff_len); in ufshcd_issue_devman_upiu_cmd()
7174 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); in ufshcd_issue_devman_upiu_cmd()
7176 hba->dev_cmd.complete = &wait; in ufshcd_issue_devman_upiu_cmd()
7178 ufshcd_add_query_upiu_trace(hba, UFS_QUERY_SEND, lrbp->ucd_req_ptr); in ufshcd_issue_devman_upiu_cmd()
7180 ufshcd_send_command(hba, tag, hba->dev_cmd_queue); in ufshcd_issue_devman_upiu_cmd()
7182 * ignore the returning value here - ufshcd_check_query_response is in ufshcd_issue_devman_upiu_cmd()
7189 memcpy(rsp_upiu, lrbp->ucd_rsp_ptr, sizeof(*rsp_upiu)); in ufshcd_issue_devman_upiu_cmd()
7191 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + sizeof(*rsp_upiu); in ufshcd_issue_devman_upiu_cmd()
7192 u16 resp_len = be16_to_cpu(lrbp->ucd_rsp_ptr->header in ufshcd_issue_devman_upiu_cmd()
7199 dev_warn(hba->dev, in ufshcd_issue_devman_upiu_cmd()
7203 err = -EINVAL; in ufshcd_issue_devman_upiu_cmd()
7207 (struct utp_upiu_req *)lrbp->ucd_rsp_ptr); in ufshcd_issue_devman_upiu_cmd()
7209 up_read(&hba->clk_scaling_lock); in ufshcd_issue_devman_upiu_cmd()
7214 * ufshcd_exec_raw_upiu_cmd - API function for sending raw upiu commands
7215 * @hba: per-adapter instance
7217 * @rsp_upiu: upiu reply - only 8 DW as we do not support scsi commands
7241 u8 tm_f = req_upiu->header.tm_function; in ufshcd_exec_raw_upiu_cmd()
7249 mutex_lock(&hba->dev_cmd.lock); in ufshcd_exec_raw_upiu_cmd()
7253 mutex_unlock(&hba->dev_cmd.lock); in ufshcd_exec_raw_upiu_cmd()
7264 if (err == -ETIMEDOUT) in ufshcd_exec_raw_upiu_cmd()
7269 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", __func__, in ufshcd_exec_raw_upiu_cmd()
7278 err = -EINVAL; in ufshcd_exec_raw_upiu_cmd()
7287 * ufshcd_advanced_rpmb_req_handler - handle advanced RPMB request
7297 * Return: zero on success, non-zero on failure.
7305 const u32 tag = hba->reserved_slot; in ufshcd_advanced_rpmb_req_handler()
7313 /* Protects use of hba->reserved_slot. */ in ufshcd_advanced_rpmb_req_handler()
7315 mutex_lock(&hba->dev_cmd.lock); in ufshcd_advanced_rpmb_req_handler()
7316 down_read(&hba->clk_scaling_lock); in ufshcd_advanced_rpmb_req_handler()
7318 lrbp = &hba->lrb[tag]; in ufshcd_advanced_rpmb_req_handler()
7319 lrbp->cmd = NULL; in ufshcd_advanced_rpmb_req_handler()
7320 lrbp->task_tag = tag; in ufshcd_advanced_rpmb_req_handler()
7321 lrbp->lun = UFS_UPIU_RPMB_WLUN; in ufshcd_advanced_rpmb_req_handler()
7323 lrbp->intr_cmd = true; in ufshcd_advanced_rpmb_req_handler()
7325 hba->dev_cmd.type = DEV_CMD_TYPE_RPMB; in ufshcd_advanced_rpmb_req_handler()
7328 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE; in ufshcd_advanced_rpmb_req_handler()
7335 if (hba->capabilities & MASK_EHSLUTRD_SUPPORTED) in ufshcd_advanced_rpmb_req_handler()
7341 req_upiu->header.task_tag = tag; in ufshcd_advanced_rpmb_req_handler()
7344 memcpy(lrbp->ucd_req_ptr, req_upiu, sizeof(*lrbp->ucd_req_ptr)); in ufshcd_advanced_rpmb_req_handler()
7346 memcpy(lrbp->ucd_req_ptr + 1, req_ehs, sizeof(*req_ehs)); in ufshcd_advanced_rpmb_req_handler()
7351 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); in ufshcd_advanced_rpmb_req_handler()
7353 hba->dev_cmd.complete = &wait; in ufshcd_advanced_rpmb_req_handler()
7355 ufshcd_send_command(hba, tag, hba->dev_cmd_queue); in ufshcd_advanced_rpmb_req_handler()
7361 memcpy(rsp_upiu, lrbp->ucd_rsp_ptr, sizeof(*rsp_upiu)); in ufshcd_advanced_rpmb_req_handler()
7363 result = (lrbp->ucd_rsp_ptr->header.response << 8) | in ufshcd_advanced_rpmb_req_handler()
7364 lrbp->ucd_rsp_ptr->header.status; in ufshcd_advanced_rpmb_req_handler()
7366 ehs_len = lrbp->ucd_rsp_ptr->header.ehs_length; in ufshcd_advanced_rpmb_req_handler()
7377 ehs_data = (u8 *)lrbp->ucd_rsp_ptr + EHS_OFFSET_IN_RESPONSE; in ufshcd_advanced_rpmb_req_handler()
7382 up_read(&hba->clk_scaling_lock); in ufshcd_advanced_rpmb_req_handler()
7383 mutex_unlock(&hba->dev_cmd.lock); in ufshcd_advanced_rpmb_req_handler()
7389 * ufshcd_eh_device_reset_handler() - Reset a single logical unit.
7405 host = cmd->device->host; in ufshcd_eh_device_reset_handler()
7408 lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun); in ufshcd_eh_device_reset_handler()
7417 for (pos = 0; pos < hba->nutrs; pos++) { in ufshcd_eh_device_reset_handler()
7418 lrbp = &hba->lrb[pos]; in ufshcd_eh_device_reset_handler()
7419 if (ufshcd_cmd_inflight(lrbp->cmd) && in ufshcd_eh_device_reset_handler()
7420 lrbp->lun == lun) { in ufshcd_eh_device_reset_handler()
7422 hwq = ufshcd_mcq_req_to_hwq(hba, scsi_cmd_to_rq(lrbp->cmd)); in ufshcd_eh_device_reset_handler()
7431 spin_lock_irqsave(&hba->outstanding_lock, flags); in ufshcd_eh_device_reset_handler()
7432 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) in ufshcd_eh_device_reset_handler()
7433 if (hba->lrb[pos].lun == lun) in ufshcd_eh_device_reset_handler()
7435 hba->outstanding_reqs &= ~pending_reqs; in ufshcd_eh_device_reset_handler()
7436 spin_unlock_irqrestore(&hba->outstanding_lock, flags); in ufshcd_eh_device_reset_handler()
7438 for_each_set_bit(pos, &pending_reqs, hba->nutrs) { in ufshcd_eh_device_reset_handler()
7440 spin_lock_irqsave(&hba->outstanding_lock, flags); in ufshcd_eh_device_reset_handler()
7443 hba->outstanding_reqs |= not_cleared; in ufshcd_eh_device_reset_handler()
7445 spin_unlock_irqrestore(&hba->outstanding_lock, flags); in ufshcd_eh_device_reset_handler()
7447 dev_err(hba->dev, "%s: failed to clear request %d\n", in ufshcd_eh_device_reset_handler()
7454 hba->req_abort_count = 0; in ufshcd_eh_device_reset_handler()
7459 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); in ufshcd_eh_device_reset_handler()
7470 for_each_set_bit(tag, &bitmap, hba->nutrs) { in ufshcd_set_req_abort_skip()
7471 lrbp = &hba->lrb[tag]; in ufshcd_set_req_abort_skip()
7472 lrbp->req_abort_skip = true; in ufshcd_set_req_abort_skip()
7477 * ufshcd_try_to_abort_task - abort a specific task
7482 * command, and in host controller by clearing the door-bell register. There can
7487 * Return: zero on success, non-zero on failure.
7491 struct ufshcd_lrb *lrbp = &hba->lrb[tag]; in ufshcd_try_to_abort_task()
7497 for (poll_cnt = 100; poll_cnt; poll_cnt--) { in ufshcd_try_to_abort_task()
7498 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, in ufshcd_try_to_abort_task()
7502 dev_err(hba->dev, "%s: cmd pending in the device. tag = %d\n", in ufshcd_try_to_abort_task()
7510 dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.\n", in ufshcd_try_to_abort_task()
7514 if (ufshcd_cmd_inflight(lrbp->cmd)) { in ufshcd_try_to_abort_task()
7520 dev_err(hba->dev, "%s: cmd at tag=%d is cleared.\n", in ufshcd_try_to_abort_task()
7533 dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.\n", in ufshcd_try_to_abort_task()
7537 dev_err(hba->dev, in ufshcd_try_to_abort_task()
7547 err = -EBUSY; in ufshcd_try_to_abort_task()
7551 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, in ufshcd_try_to_abort_task()
7556 dev_err(hba->dev, "%s: issued. tag = %d, err %d\n", in ufshcd_try_to_abort_task()
7564 dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d\n", in ufshcd_try_to_abort_task()
7572 * ufshcd_abort - scsi host template eh_abort_handler callback
7579 struct Scsi_Host *host = cmd->device->host; in ufshcd_abort()
7581 int tag = scsi_cmd_to_rq(cmd)->tag; in ufshcd_abort()
7582 struct ufshcd_lrb *lrbp = &hba->lrb[tag]; in ufshcd_abort()
7594 if (!test_bit(tag, &hba->outstanding_reqs)) { in ufshcd_abort()
7596 dev_err(hba->dev, in ufshcd_abort()
7598 __func__, tag, hba->outstanding_reqs, reg); in ufshcd_abort()
7604 dev_info(hba->dev, "%s: Device abort task at tag %d\n", __func__, tag); in ufshcd_abort()
7614 if (!hba->req_abort_count) { in ufshcd_abort()
7623 hba->req_abort_count++; in ufshcd_abort()
7627 dev_err(hba->dev, in ufshcd_abort()
7635 * Task abort to the device W-LUN is illegal. When this command in ufshcd_abort()
7639 * the lrb taken by this cmd and re-set it in outstanding_reqs, in ufshcd_abort()
7642 if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN) { in ufshcd_abort()
7643 ufshcd_update_evt_hist(hba, UFS_EVT_ABORT, lrbp->lun); in ufshcd_abort()
7645 spin_lock_irqsave(host->host_lock, flags); in ufshcd_abort()
7646 hba->force_reset = true; in ufshcd_abort()
7648 spin_unlock_irqrestore(host->host_lock, flags); in ufshcd_abort()
7659 if (lrbp->req_abort_skip) { in ufshcd_abort()
7660 dev_err(hba->dev, "%s: skipping abort\n", __func__); in ufshcd_abort()
7661 ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs); in ufshcd_abort()
7667 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); in ufshcd_abort()
7668 ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs); in ufshcd_abort()
7677 spin_lock_irqsave(&hba->outstanding_lock, flags); in ufshcd_abort()
7678 outstanding = __test_and_clear_bit(tag, &hba->outstanding_reqs); in ufshcd_abort()
7679 spin_unlock_irqrestore(&hba->outstanding_lock, flags); in ufshcd_abort()
7693 * ufshcd_host_reset_and_restore - reset and restore host controller
7694 * @hba: per-adapter instance
7697 * local and remote (device) Uni-Pro stack and the attributes
7700 * Return: zero on success, non-zero on failure.
7711 hba->silence_err_logs = true; in ufshcd_host_reset_and_restore()
7713 hba->silence_err_logs = false; in ufshcd_host_reset_and_restore()
7725 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err); in ufshcd_host_reset_and_restore()
7731 * ufshcd_reset_and_restore - reset and re-initialize host/device
7732 * @hba: per-adapter instance
7734 * Reset and recover device, host and re-establish link. This
7737 * Return: zero on success, non-zero on failure.
7747 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_reset_and_restore()
7753 saved_err |= hba->saved_err; in ufshcd_reset_and_restore()
7754 saved_uic_err |= hba->saved_uic_err; in ufshcd_reset_and_restore()
7755 hba->saved_err = 0; in ufshcd_reset_and_restore()
7756 hba->saved_uic_err = 0; in ufshcd_reset_and_restore()
7757 hba->force_reset = false; in ufshcd_reset_and_restore()
7758 hba->ufshcd_state = UFSHCD_STATE_RESET; in ufshcd_reset_and_restore()
7759 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_reset_and_restore()
7766 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_reset_and_restore()
7770 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL && in ufshcd_reset_and_restore()
7771 hba->ufshcd_state != UFSHCD_STATE_ERROR && in ufshcd_reset_and_restore()
7772 hba->ufshcd_state != UFSHCD_STATE_EH_SCHEDULED_NON_FATAL) in ufshcd_reset_and_restore()
7773 err = -EAGAIN; in ufshcd_reset_and_restore()
7774 } while (err && --retries); in ufshcd_reset_and_restore()
7777 * Inform scsi mid-layer that we did reset and allow to handle in ufshcd_reset_and_restore()
7780 scsi_report_bus_reset(hba->host, 0); in ufshcd_reset_and_restore()
7782 hba->ufshcd_state = UFSHCD_STATE_ERROR; in ufshcd_reset_and_restore()
7783 hba->saved_err |= saved_err; in ufshcd_reset_and_restore()
7784 hba->saved_uic_err |= saved_uic_err; in ufshcd_reset_and_restore()
7786 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_reset_and_restore()
7792 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
7803 hba = shost_priv(cmd->device->host); in ufshcd_eh_host_reset_handler()
7805 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_eh_host_reset_handler()
7806 hba->force_reset = true; in ufshcd_eh_host_reset_handler()
7808 dev_err(hba->dev, "%s: reset in progress - 1\n", __func__); in ufshcd_eh_host_reset_handler()
7809 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_eh_host_reset_handler()
7811 flush_work(&hba->eh_work); in ufshcd_eh_host_reset_handler()
7813 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_eh_host_reset_handler()
7814 if (hba->ufshcd_state == UFSHCD_STATE_ERROR) in ufshcd_eh_host_reset_handler()
7816 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_eh_host_reset_handler()
7822 * ufshcd_get_max_icc_level - calculate the ICC level
7823 * @sup_curr_uA: max. current supported by the regulator
7827 * Return: calculated max ICC level for specific regulator.
7837 for (i = start_scan; i >= 0; i--) { in ufshcd_get_max_icc_level()
7868 * ufshcd_find_max_sup_active_icc_level - calculate the max ICC level
7870 * @hba: per-adapter instance
7880 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq || in ufshcd_find_max_sup_active_icc_level()
7881 !hba->vreg_info.vccq2) { in ufshcd_find_max_sup_active_icc_level()
7884 * never-ending cycles of messages written back to storage by in ufshcd_find_max_sup_active_icc_level()
7888 dev_dbg(hba->dev, in ufshcd_find_max_sup_active_icc_level()
7889 "%s: Regulator capability was not set, actvIccLevel=%d", in ufshcd_find_max_sup_active_icc_level()
7894 if (hba->vreg_info.vcc->max_uA) in ufshcd_find_max_sup_active_icc_level()
7896 hba->vreg_info.vcc->max_uA, in ufshcd_find_max_sup_active_icc_level()
7897 POWER_DESC_MAX_ACTV_ICC_LVLS - 1, in ufshcd_find_max_sup_active_icc_level()
7900 if (hba->vreg_info.vccq->max_uA) in ufshcd_find_max_sup_active_icc_level()
7902 hba->vreg_info.vccq->max_uA, in ufshcd_find_max_sup_active_icc_level()
7906 if (hba->vreg_info.vccq2->max_uA) in ufshcd_find_max_sup_active_icc_level()
7908 hba->vreg_info.vccq2->max_uA, in ufshcd_find_max_sup_active_icc_level()
7928 dev_err(hba->dev, in ufshcd_set_active_icc_lvl()
7935 dev_dbg(hba->dev, "%s: setting icc_level 0x%x", __func__, icc_level); in ufshcd_set_active_icc_lvl()
7941 dev_err(hba->dev, in ufshcd_set_active_icc_lvl()
7952 blk_pm_runtime_init(sdev->request_queue, &sdev->sdev_gendev); in ufshcd_blk_pm_runtime_init()
7953 if (sdev->rpm_autosuspend) in ufshcd_blk_pm_runtime_init()
7954 pm_runtime_set_autosuspend_delay(&sdev->sdev_gendev, in ufshcd_blk_pm_runtime_init()
7960 * ufshcd_scsi_add_wlus - Adds required W-LUs
7961 * @hba: per-adapter instance
7982 * Return: zero on success (all required W-LUs are added successfully),
7983 * non-zero error value on failure (if failed to add any of the required W-LU).
7990 hba->ufs_device_wlun = __scsi_add_device(hba->host, 0, 0, in ufshcd_scsi_add_wlus()
7992 if (IS_ERR(hba->ufs_device_wlun)) { in ufshcd_scsi_add_wlus()
7993 ret = PTR_ERR(hba->ufs_device_wlun); in ufshcd_scsi_add_wlus()
7994 hba->ufs_device_wlun = NULL; in ufshcd_scsi_add_wlus()
7997 scsi_device_put(hba->ufs_device_wlun); in ufshcd_scsi_add_wlus()
7999 sdev_rpmb = __scsi_add_device(hba->host, 0, 0, in ufshcd_scsi_add_wlus()
8008 sdev_boot = __scsi_add_device(hba->host, 0, 0, in ufshcd_scsi_add_wlus()
8011 dev_err(hba->dev, "%s: BOOT WLUN not found\n", __func__); in ufshcd_scsi_add_wlus()
8019 scsi_remove_device(hba->ufs_device_wlun); in ufshcd_scsi_add_wlus()
8026 struct ufs_dev_info *dev_info = &hba->dev_info; in ufshcd_wb_probe()
8035 * Probe WB only for UFS-2.2 and UFS-3.1 (and later) devices or in ufshcd_wb_probe()
8039 if (!(dev_info->wspecversion >= 0x310 || in ufshcd_wb_probe()
8040 dev_info->wspecversion == 0x220 || in ufshcd_wb_probe()
8041 (hba->dev_quirks & UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES))) in ufshcd_wb_probe()
8055 dev_info->wb_buffer_type = desc_buf[DEVICE_DESC_PARAM_WB_TYPE]; in ufshcd_wb_probe()
8057 dev_info->b_presrv_uspc_en = in ufshcd_wb_probe()
8060 if (dev_info->wb_buffer_type == WB_BUF_MODE_SHARED) { in ufshcd_wb_probe()
8073 dev_info->wb_dedicated_lu = lun; in ufshcd_wb_probe()
8088 hba->caps &= ~UFSHCD_CAP_WB_EN; in ufshcd_wb_probe()
8093 struct ufs_dev_info *dev_info = &hba->dev_info; in ufshcd_temp_notif_probe()
8097 if (!(hba->caps & UFSHCD_CAP_TEMP_NOTIF) || dev_info->wspecversion < 0x300) in ufshcd_temp_notif_probe()
8116 struct ufs_dev_info *dev_info = &hba->dev_info; in ufshcd_ext_iid_probe()
8121 /* Only UFS-4.0 and above may support EXT_IID */ in ufshcd_ext_iid_probe()
8122 if (dev_info->wspecversion < 0x400) in ufshcd_ext_iid_probe()
8133 dev_err(hba->dev, "failed reading bEXTIIDEn. err = %d\n", err); in ufshcd_ext_iid_probe()
8136 dev_info->b_ext_iid_en = ext_iid_en; in ufshcd_ext_iid_probe()
8143 struct ufs_dev_info *dev_info = &hba->dev_info; in ufshcd_fixup_dev_quirks()
8148 for (f = fixups; f->quirk; f++) { in ufshcd_fixup_dev_quirks()
8149 if ((f->wmanufacturerid == dev_info->wmanufacturerid || in ufshcd_fixup_dev_quirks()
8150 f->wmanufacturerid == UFS_ANY_VENDOR) && in ufshcd_fixup_dev_quirks()
8151 ((dev_info->model && in ufshcd_fixup_dev_quirks()
8152 STR_PRFX_EQUAL(f->model, dev_info->model)) || in ufshcd_fixup_dev_quirks()
8153 !strcmp(f->model, UFS_ANY_MODEL))) in ufshcd_fixup_dev_quirks()
8154 hba->dev_quirks |= f->quirk; in ufshcd_fixup_dev_quirks()
8176 if (ts64.tv_sec < hba->dev_info.rtc_time_baseline) { in ufshcd_update_rtc()
8177 dev_warn_once(hba->dev, "%s: Current time precedes previous setting!\n", __func__); in ufshcd_update_rtc()
8182 * The Absolute RTC mode has a 136-year limit, spanning from 2010 to 2146. If a time beyond in ufshcd_update_rtc()
8185 val = ts64.tv_sec - hba->dev_info.rtc_time_baseline; in ufshcd_update_rtc()
8196 dev_err(hba->dev, "%s: Failed to update rtc %d\n", __func__, err); in ufshcd_update_rtc()
8197 else if (hba->dev_info.rtc_type == UFS_RTC_RELATIVE) in ufshcd_update_rtc()
8198 hba->dev_info.rtc_time_baseline = ts64.tv_sec; in ufshcd_update_rtc()
8209 hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL && in ufshcd_rtc_work()
8210 !hba->clk_gating.active_reqs) in ufshcd_rtc_work()
8214 schedule_delayed_work(&hba->ufs_rtc_update_work, in ufshcd_rtc_work()
8221 struct ufs_dev_info *dev_info = &hba->dev_info; in ufs_init_rtc()
8224 dev_info->rtc_type = UFS_RTC_ABSOLUTE; in ufs_init_rtc()
8231 dev_info->rtc_time_baseline = mktime64(2010, 1, 1, 0, 0, 0) - in ufs_init_rtc()
8234 dev_info->rtc_type = UFS_RTC_RELATIVE; in ufs_init_rtc()
8235 dev_info->rtc_time_baseline = 0; in ufs_init_rtc()
8244 struct ufs_dev_info *dev_info = &hba->dev_info; in ufs_get_device_desc()
8248 err = -ENOMEM; in ufs_get_device_desc()
8255 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n", in ufs_get_device_desc()
8264 dev_info->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 | in ufs_get_device_desc()
8268 dev_info->wspecversion = desc_buf[DEVICE_DESC_PARAM_SPEC_VER] << 8 | in ufs_get_device_desc()
8270 dev_info->bqueuedepth = desc_buf[DEVICE_DESC_PARAM_Q_DPTH]; in ufs_get_device_desc()
8275 &dev_info->model, SD_ASCII_STD); in ufs_get_device_desc()
8277 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n", in ufs_get_device_desc()
8282 hba->luns_avail = desc_buf[DEVICE_DESC_PARAM_NUM_LU] + in ufs_get_device_desc()
8293 if (hba->ext_iid_sup) in ufs_get_device_desc()
8309 struct ufs_dev_info *dev_info = &hba->dev_info; in ufs_put_device_desc()
8311 kfree(dev_info->model); in ufs_put_device_desc()
8312 dev_info->model = NULL; in ufs_put_device_desc()
8316 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
8317 * @hba: per-adapter instance
8320 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
8324 * Return: zero on success, non-zero error value on failure.
8351 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
8352 * @hba: per-adapter instance
8355 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
8356 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
8359 * Return: zero on success, non-zero error value on failure.
8393 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
8395 * @hba: per-adapter instance
8401 * Return: zero on success, non-zero error value on failure.
8423 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d", in ufshcd_quirk_tune_host_pa_tactivate()
8425 return -EINVAL; in ufshcd_quirk_tune_host_pa_tactivate()
8430 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d", in ufshcd_quirk_tune_host_pa_tactivate()
8432 return -EINVAL; in ufshcd_quirk_tune_host_pa_tactivate()
8444 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1]; in ufshcd_quirk_tune_host_pa_tactivate()
8446 gran_to_us_table[peer_granularity - 1]; in ufshcd_quirk_tune_host_pa_tactivate()
8452 gran_to_us_table[peer_granularity - 1]; in ufshcd_quirk_tune_host_pa_tactivate()
8471 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE) in ufshcd_tune_unipro_params()
8475 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE) in ufshcd_tune_unipro_params()
8481 hba->ufs_stats.hibern8_exit_cnt = 0; in ufshcd_clear_dbg_ufs_stats()
8482 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); in ufshcd_clear_dbg_ufs_stats()
8483 hba->req_abort_count = 0; in ufshcd_clear_dbg_ufs_stats()
8493 err = -ENOMEM; in ufshcd_device_geo_params_init()
8500 dev_err(hba->dev, "%s: Failed reading Geometry Desc. err = %d\n", in ufshcd_device_geo_params_init()
8506 hba->dev_info.max_lu_supported = 32; in ufshcd_device_geo_params_init()
8508 hba->dev_info.max_lu_supported = 8; in ufshcd_device_geo_params_init()
8546 hba->dev_ref_clk_freq = in ufshcd_parse_dev_ref_clk_freq()
8549 if (hba->dev_ref_clk_freq == REF_CLK_FREQ_INVAL) in ufshcd_parse_dev_ref_clk_freq()
8550 dev_err(hba->dev, in ufshcd_parse_dev_ref_clk_freq()
8558 u32 freq = hba->dev_ref_clk_freq; in ufshcd_set_dev_ref_clk()
8564 dev_err(hba->dev, "failed reading bRefClkFreq. err = %d\n", in ufshcd_set_dev_ref_clk()
8576 dev_err(hba->dev, "bRefClkFreq setting to %lu Hz failed\n", in ufshcd_set_dev_ref_clk()
8581 dev_dbg(hba->dev, "bRefClkFreq setting to %lu Hz succeeded\n", in ufshcd_set_dev_ref_clk()
8601 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n", in ufshcd_device_params_init()
8610 hba->dev_info.f_power_on_wp_en = flag; in ufshcd_device_params_init()
8612 /* Probe maximum power mode co-supported by both UFS host and device */ in ufshcd_device_params_init()
8614 dev_err(hba->dev, in ufshcd_device_params_init()
8626 struct ufs_dev_info *dev_info = &hba->dev_info; in ufshcd_set_timestamp_attr()
8629 if (dev_info->wspecversion < 0x400) in ufshcd_set_timestamp_attr()
8634 mutex_lock(&hba->dev_cmd.lock); in ufshcd_set_timestamp_attr()
8640 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; in ufshcd_set_timestamp_attr()
8642 upiu_data = (struct utp_upiu_query_v4_0 *)&request->upiu_req; in ufshcd_set_timestamp_attr()
8644 put_unaligned_be64(ktime_get_real_ns(), &upiu_data->osf3); in ufshcd_set_timestamp_attr()
8649 dev_err(hba->dev, "%s: failed to set timestamp %d\n", in ufshcd_set_timestamp_attr()
8652 mutex_unlock(&hba->dev_cmd.lock); in ufshcd_set_timestamp_attr()
8657 * ufshcd_add_lus - probe and add UFS logical units
8658 * @hba: per-adapter instance
8673 memcpy(&hba->clk_scaling.saved_pwr_info, in ufshcd_add_lus()
8674 &hba->pwr_info, in ufshcd_add_lus()
8676 hba->clk_scaling.is_allowed = true; in ufshcd_add_lus()
8682 hba->clk_scaling.is_enabled = true; in ufshcd_add_lus()
8687 * The RTC update code accesses the hba->ufs_device_wlun->sdev_gendev in ufshcd_add_lus()
8691 schedule_delayed_work(&hba->ufs_rtc_update_work, in ufshcd_add_lus()
8695 scsi_scan_host(hba->host); in ufshcd_add_lus()
8701 /* SDB - Single Doorbell */
8707 dmam_free_coherent(hba->dev, ucdl_size, hba->ucdl_base_addr, in ufshcd_release_sdb_queue()
8708 hba->ucdl_dma_addr); in ufshcd_release_sdb_queue()
8711 dmam_free_coherent(hba->dev, utrdl_size, hba->utrdl_base_addr, in ufshcd_release_sdb_queue()
8712 hba->utrdl_dma_addr); in ufshcd_release_sdb_queue()
8714 devm_kfree(hba->dev, hba->lrb); in ufshcd_release_sdb_queue()
8720 int old_nutrs = hba->nutrs; in ufshcd_alloc_mcq()
8726 hba->nutrs = ret; in ufshcd_alloc_mcq()
8735 if (hba->nutrs != old_nutrs) { in ufshcd_alloc_mcq()
8749 hba->nutrs = old_nutrs; in ufshcd_alloc_mcq()
8759 dev_info(hba->dev, "ESI %sconfigured\n", ret ? "is not " : ""); in ufshcd_config_mcq()
8762 if (hba->quirks & UFSHCD_QUIRK_MCQ_BROKEN_INTR) in ufshcd_config_mcq()
8766 ufshcd_mcq_config_mac(hba, hba->nutrs); in ufshcd_config_mcq()
8768 hba->host->can_queue = hba->nutrs - UFSHCD_NUM_RESERVED; in ufshcd_config_mcq()
8769 hba->reserved_slot = hba->nutrs - UFSHCD_NUM_RESERVED; in ufshcd_config_mcq()
8774 hba->mcq_enabled = true; in ufshcd_config_mcq()
8776 …dev_info(hba->dev, "MCQ configured, nr_queues=%d, io_queues=%d, read_queue=%d, poll_queues=%d, que… in ufshcd_config_mcq()
8777 hba->nr_hw_queues, hba->nr_queues[HCTX_TYPE_DEFAULT], in ufshcd_config_mcq()
8778 hba->nr_queues[HCTX_TYPE_READ], hba->nr_queues[HCTX_TYPE_POLL], in ufshcd_config_mcq()
8779 hba->nutrs); in ufshcd_config_mcq()
8785 struct Scsi_Host *host = hba->host; in ufshcd_device_init()
8787 hba->ufshcd_state = UFSHCD_STATE_RESET; in ufshcd_device_init()
8793 if (hba->quirks & UFSHCD_QUIRK_SKIP_PH_CONFIGURATION) in ufshcd_device_init()
8824 if (is_mcq_supported(hba) && !hba->scsi_host_added) { in ufshcd_device_init()
8831 dev_err(hba->dev, "MCQ mode is disabled, err=%d\n", in ufshcd_device_init()
8834 ret = scsi_add_host(host, hba->dev); in ufshcd_device_init()
8836 dev_err(hba->dev, "scsi_add_host failed\n"); in ufshcd_device_init()
8839 hba->scsi_host_added = true; in ufshcd_device_init()
8855 if (hba->max_pwr_info.is_valid) { in ufshcd_device_init()
8860 if (hba->dev_ref_clk_freq != REF_CLK_FREQ_INVAL) in ufshcd_device_init()
8862 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info); in ufshcd_device_init()
8864 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n", in ufshcd_device_init()
8874 * ufshcd_probe_hba - probe hba to detect device and initialize it
8875 * @hba: per-adapter instance
8878 * Execute link-startup and verify device initialization
8892 if (!hba->pm_op_in_progress && in ufshcd_probe_hba()
8893 (hba->quirks & UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH)) { in ufshcd_probe_hba()
8900 dev_err(hba->dev, "Host controller enable failed\n"); in ufshcd_probe_hba()
8922 /* Enable UFS Write Booster if supported */ in ufshcd_probe_hba()
8925 if (hba->ee_usr_mask) in ufshcd_probe_hba()
8927 /* Enable Auto-Hibernate if configured */ in ufshcd_probe_hba()
8931 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_probe_hba()
8933 hba->ufshcd_state = UFSHCD_STATE_ERROR; in ufshcd_probe_hba()
8934 else if (hba->ufshcd_state == UFSHCD_STATE_RESET) in ufshcd_probe_hba()
8935 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; in ufshcd_probe_hba()
8936 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_probe_hba()
8938 trace_ufshcd_init(dev_name(hba->dev), ret, in ufshcd_probe_hba()
8940 hba->curr_dev_pwr_mode, hba->uic_link_state); in ufshcd_probe_hba()
8945 * ufshcd_async_scan - asynchronous execution for probing hba
8954 down(&hba->host_sem); in ufshcd_async_scan()
8957 up(&hba->host_sem); in ufshcd_async_scan()
8965 pm_runtime_put_sync(hba->dev); in ufshcd_async_scan()
8968 dev_err(hba->dev, "%s failed: %d\n", __func__, ret); in ufshcd_async_scan()
8973 struct ufs_hba *hba = shost_priv(scmd->device->host); in ufshcd_eh_timed_out()
8975 if (!hba->system_suspending) { in ufshcd_eh_timed_out()
8987 dev_info(hba->dev, "%s() finished; outstanding_tasks = %#lx.\n", in ufshcd_eh_timed_out()
8988 __func__, hba->outstanding_tasks); in ufshcd_eh_timed_out()
8990 return scsi_host_busy(hba->host) ? SCSI_EH_RESET_TIMER : SCSI_EH_DONE; in ufshcd_eh_timed_out()
9024 .this_id = -1,
9048 * zero max_uA may cause unexpected behavior when regulator is in ufshcd_config_vreg_load()
9051 if (!vreg->max_uA) in ufshcd_config_vreg_load()
9054 ret = regulator_set_load(vreg->reg, ua); in ufshcd_config_vreg_load()
9057 __func__, vreg->name, ua, ret); in ufshcd_config_vreg_load()
9066 return ufshcd_config_vreg_load(hba->dev, vreg, UFS_VREG_LPM_LOAD_UA); in ufshcd_config_vreg_lpm()
9075 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA); in ufshcd_config_vreg_hpm()
9081 if (regulator_count_voltages(vreg->reg) <= 0) in ufshcd_config_vreg()
9084 return ufshcd_config_vreg_load(dev, vreg, on ? vreg->max_uA : 0); in ufshcd_config_vreg()
9091 if (!vreg || vreg->enabled) in ufshcd_enable_vreg()
9096 ret = regulator_enable(vreg->reg); in ufshcd_enable_vreg()
9099 vreg->enabled = true; in ufshcd_enable_vreg()
9102 __func__, vreg->name, ret); in ufshcd_enable_vreg()
9111 if (!vreg || !vreg->enabled || vreg->always_on) in ufshcd_disable_vreg()
9114 ret = regulator_disable(vreg->reg); in ufshcd_disable_vreg()
9119 vreg->enabled = false; in ufshcd_disable_vreg()
9122 __func__, vreg->name, ret); in ufshcd_disable_vreg()
9131 struct device *dev = hba->dev; in ufshcd_setup_vreg()
9132 struct ufs_vreg_info *info = &hba->vreg_info; in ufshcd_setup_vreg()
9134 ret = ufshcd_toggle_vreg(dev, info->vcc, on); in ufshcd_setup_vreg()
9138 ret = ufshcd_toggle_vreg(dev, info->vccq, on); in ufshcd_setup_vreg()
9142 ret = ufshcd_toggle_vreg(dev, info->vccq2, on); in ufshcd_setup_vreg()
9146 ufshcd_toggle_vreg(dev, info->vccq2, false); in ufshcd_setup_vreg()
9147 ufshcd_toggle_vreg(dev, info->vccq, false); in ufshcd_setup_vreg()
9148 ufshcd_toggle_vreg(dev, info->vcc, false); in ufshcd_setup_vreg()
9155 struct ufs_vreg_info *info = &hba->vreg_info; in ufshcd_setup_hba_vreg()
9157 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on); in ufshcd_setup_hba_vreg()
9167 vreg->reg = devm_regulator_get(dev, vreg->name); in ufshcd_get_vreg()
9168 if (IS_ERR(vreg->reg)) { in ufshcd_get_vreg()
9169 ret = PTR_ERR(vreg->reg); in ufshcd_get_vreg()
9171 __func__, vreg->name, ret); in ufshcd_get_vreg()
9181 struct device *dev = hba->dev; in ufshcd_init_vreg()
9182 struct ufs_vreg_info *info = &hba->vreg_info; in ufshcd_init_vreg()
9184 ret = ufshcd_get_vreg(dev, info->vcc); in ufshcd_init_vreg()
9188 ret = ufshcd_get_vreg(dev, info->vccq); in ufshcd_init_vreg()
9190 ret = ufshcd_get_vreg(dev, info->vccq2); in ufshcd_init_vreg()
9197 struct ufs_vreg_info *info = &hba->vreg_info; in ufshcd_init_hba_vreg()
9199 return ufshcd_get_vreg(hba->dev, info->vdd_hba); in ufshcd_init_hba_vreg()
9206 struct list_head *head = &hba->clk_list_head; in ufshcd_setup_clocks()
9219 if (!IS_ERR_OR_NULL(clki->clk)) { in ufshcd_setup_clocks()
9225 clki->keep_link_active) in ufshcd_setup_clocks()
9228 clk_state_changed = on ^ clki->enabled; in ufshcd_setup_clocks()
9229 if (on && !clki->enabled) { in ufshcd_setup_clocks()
9230 ret = clk_prepare_enable(clki->clk); in ufshcd_setup_clocks()
9232 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n", in ufshcd_setup_clocks()
9233 __func__, clki->name, ret); in ufshcd_setup_clocks()
9236 } else if (!on && clki->enabled) { in ufshcd_setup_clocks()
9237 clk_disable_unprepare(clki->clk); in ufshcd_setup_clocks()
9239 clki->enabled = on; in ufshcd_setup_clocks()
9240 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__, in ufshcd_setup_clocks()
9241 clki->name, on ? "en" : "dis"); in ufshcd_setup_clocks()
9252 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled) in ufshcd_setup_clocks()
9253 clk_disable_unprepare(clki->clk); in ufshcd_setup_clocks()
9256 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_setup_clocks()
9257 hba->clk_gating.state = CLKS_ON; in ufshcd_setup_clocks()
9258 trace_ufshcd_clk_gating(dev_name(hba->dev), in ufshcd_setup_clocks()
9259 hba->clk_gating.state); in ufshcd_setup_clocks()
9260 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_setup_clocks()
9264 trace_ufshcd_profile_clk_gating(dev_name(hba->dev), in ufshcd_setup_clocks()
9273 int ret = device_property_read_u32(hba->dev, "ref-clk-freq", &freq); in ufshcd_parse_ref_clk_property()
9276 dev_dbg(hba->dev, "Cannot query 'ref-clk-freq' property = %d", ret); in ufshcd_parse_ref_clk_property()
9287 struct device *dev = hba->dev; in ufshcd_init_clocks()
9288 struct list_head *head = &hba->clk_list_head; in ufshcd_init_clocks()
9294 if (!clki->name) in ufshcd_init_clocks()
9297 clki->clk = devm_clk_get(dev, clki->name); in ufshcd_init_clocks()
9298 if (IS_ERR(clki->clk)) { in ufshcd_init_clocks()
9299 ret = PTR_ERR(clki->clk); in ufshcd_init_clocks()
9301 __func__, clki->name, ret); in ufshcd_init_clocks()
9310 if (!strcmp(clki->name, "ref_clk")) in ufshcd_init_clocks()
9311 ufshcd_parse_dev_ref_clk_freq(hba, clki->clk); in ufshcd_init_clocks()
9313 if (clki->max_freq) { in ufshcd_init_clocks()
9314 ret = clk_set_rate(clki->clk, clki->max_freq); in ufshcd_init_clocks()
9316 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", in ufshcd_init_clocks()
9317 __func__, clki->name, in ufshcd_init_clocks()
9318 clki->max_freq, ret); in ufshcd_init_clocks()
9321 clki->curr_freq = clki->max_freq; in ufshcd_init_clocks()
9324 clki->name, clk_get_rate(clki->clk)); in ufshcd_init_clocks()
9334 if (!hba->vops) in ufshcd_variant_hba_init()
9339 dev_err_probe(hba->dev, err, in ufshcd_variant_hba_init()
9348 if (!hba->vops) in ufshcd_variant_hba_exit()
9377 if (hba->dev_ref_clk_freq == REF_CLK_FREQ_INVAL) in ufshcd_hba_init()
9378 hba->dev_ref_clk_freq = ufshcd_parse_ref_clk_property(hba); in ufshcd_hba_init()
9398 hba->is_powered = true; in ufshcd_hba_init()
9413 if (hba->is_powered) { in ufshcd_hba_exit()
9416 if (hba->eh_wq) in ufshcd_hba_exit()
9417 destroy_workqueue(hba->eh_wq); in ufshcd_hba_exit()
9423 hba->is_powered = false; in ufshcd_hba_exit()
9445 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
9461 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_set_dev_pwr_mode()
9462 sdp = hba->ufs_device_wlun; in ufshcd_set_dev_pwr_mode()
9466 ret = -ENODEV; in ufshcd_set_dev_pwr_mode()
9467 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_set_dev_pwr_mode()
9473 * If scsi commands fail, the scsi mid-layer schedules scsi error- in ufshcd_set_dev_pwr_mode()
9478 hba->host->eh_noresume = 1; in ufshcd_set_dev_pwr_mode()
9485 for (retries = 3; retries > 0; --retries) { in ufshcd_set_dev_pwr_mode()
9501 ret = -EIO; in ufshcd_set_dev_pwr_mode()
9504 hba->curr_dev_pwr_mode = pwr_mode; in ufshcd_set_dev_pwr_mode()
9508 hba->host->eh_noresume = 0; in ufshcd_set_dev_pwr_mode()
9518 if (req_link_state == hba->uic_link_state) in ufshcd_link_state_transition()
9526 dev_err(hba->dev, "%s: hibern8 enter failed %d\n", in ufshcd_link_state_transition()
9537 (!check_for_bkops || !hba->auto_bkops_enabled)) { in ufshcd_link_state_transition()
9550 dev_err(hba->dev, "%s: hibern8 enter failed %d\n", in ufshcd_link_state_transition()
9581 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM) in ufshcd_vreg_set_lpm()
9596 * If Write Booster is enabled and the device needs to flush the WB in ufshcd_vreg_set_lpm()
9600 !hba->dev_info.is_lu_power_on_wp) { in ufshcd_vreg_set_lpm()
9604 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false); in ufshcd_vreg_set_lpm()
9607 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq); in ufshcd_vreg_set_lpm()
9608 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2); in ufshcd_vreg_set_lpm()
9613 * Some UFS devices require delay after VCC power rail is turned-off. in ufshcd_vreg_set_lpm()
9615 if (vcc_off && hba->vreg_info.vcc && in ufshcd_vreg_set_lpm()
9616 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_AFTER_LPM) in ufshcd_vreg_set_lpm()
9626 !hba->dev_info.is_lu_power_on_wp) { in ufshcd_vreg_set_hpm()
9630 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq); in ufshcd_vreg_set_hpm()
9633 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2); in ufshcd_vreg_set_hpm()
9637 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true); in ufshcd_vreg_set_hpm()
9642 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq); in ufshcd_vreg_set_hpm()
9644 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false); in ufshcd_vreg_set_hpm()
9670 hba->pm_op_in_progress = true; in __ufshcd_wl_suspend()
9673 hba->rpm_lvl : hba->spm_lvl; in __ufshcd_wl_suspend()
9686 hba->clk_gating.is_suspended = true; in __ufshcd_wl_suspend()
9696 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) && in __ufshcd_wl_suspend()
9697 (req_link_state == hba->uic_link_state)) in __ufshcd_wl_suspend()
9705 ret = -EBUSY; in __ufshcd_wl_suspend()
9724 ret = -EBUSY; in __ufshcd_wl_suspend()
9736 hba->dev_info.b_rpm_dev_flush_capable = in __ufshcd_wl_suspend()
9737 hba->auto_bkops_enabled || in __ufshcd_wl_suspend()
9744 flush_work(&hba->eeh_work); in __ufshcd_wl_suspend()
9750 if (req_dev_pwr_mode != hba->curr_dev_pwr_mode) { in __ufshcd_wl_suspend()
9755 if (!hba->dev_info.b_rpm_dev_flush_capable) { in __ufshcd_wl_suspend()
9764 ret = -EBUSY; in __ufshcd_wl_suspend()
9784 ret = -EBUSY; in __ufshcd_wl_suspend()
9799 cancel_delayed_work_sync(&hba->ufs_rtc_update_work); in __ufshcd_wl_suspend()
9828 hba->dev_info.b_rpm_dev_flush_capable = false; in __ufshcd_wl_suspend()
9830 if (hba->dev_info.b_rpm_dev_flush_capable) { in __ufshcd_wl_suspend()
9831 schedule_delayed_work(&hba->rpm_dev_flush_recheck_work, in __ufshcd_wl_suspend()
9837 hba->clk_gating.is_suspended = false; in __ufshcd_wl_suspend()
9840 hba->pm_op_in_progress = false; in __ufshcd_wl_suspend()
9848 enum uic_link_state old_link_state = hba->uic_link_state; in __ufshcd_wl_resume()
9850 hba->pm_op_in_progress = true; in __ufshcd_wl_resume()
9869 dev_err(hba->dev, "%s: hibern8 exit failed %d\n", in __ufshcd_wl_resume()
9894 schedule_delayed_work(&hba->ufs_rtc_update_work, in __ufshcd_wl_resume()
9903 * keep auto-bkops enabled or else disable it. in __ufshcd_wl_resume()
9907 if (hba->ee_usr_mask) in __ufshcd_wl_resume()
9913 if (hba->dev_info.b_rpm_dev_flush_capable) { in __ufshcd_wl_resume()
9914 hba->dev_info.b_rpm_dev_flush_capable = false; in __ufshcd_wl_resume()
9915 cancel_delayed_work(&hba->rpm_dev_flush_recheck_work); in __ufshcd_wl_resume()
9918 /* Enable Auto-Hibernate if configured */ in __ufshcd_wl_resume()
9931 hba->clk_gating.is_suspended = false; in __ufshcd_wl_resume()
9933 hba->pm_op_in_progress = false; in __ufshcd_wl_resume()
9944 hba = shost_priv(sdev->host); in ufshcd_wl_runtime_suspend()
9948 dev_err(&sdev->sdev_gendev, "%s failed: %d\n", __func__, ret); in ufshcd_wl_runtime_suspend()
9952 hba->curr_dev_pwr_mode, hba->uic_link_state); in ufshcd_wl_runtime_suspend()
9964 hba = shost_priv(sdev->host); in ufshcd_wl_runtime_resume()
9968 dev_err(&sdev->sdev_gendev, "%s failed: %d\n", __func__, ret); in ufshcd_wl_runtime_resume()
9972 hba->curr_dev_pwr_mode, hba->uic_link_state); in ufshcd_wl_runtime_resume()
9986 hba = shost_priv(sdev->host); in ufshcd_wl_suspend()
9987 down(&hba->host_sem); in ufshcd_wl_suspend()
9988 hba->system_suspending = true; in ufshcd_wl_suspend()
9995 dev_err(&sdev->sdev_gendev, "%s failed: %d\n", __func__, ret); in ufshcd_wl_suspend()
9996 up(&hba->host_sem); in ufshcd_wl_suspend()
10001 hba->is_sys_suspended = true; in ufshcd_wl_suspend()
10004 hba->curr_dev_pwr_mode, hba->uic_link_state); in ufshcd_wl_suspend()
10016 hba = shost_priv(sdev->host); in ufshcd_wl_resume()
10023 dev_err(&sdev->sdev_gendev, "%s failed: %d\n", __func__, ret); in ufshcd_wl_resume()
10027 hba->curr_dev_pwr_mode, hba->uic_link_state); in ufshcd_wl_resume()
10029 hba->is_sys_suspended = false; in ufshcd_wl_resume()
10030 hba->system_suspending = false; in ufshcd_wl_resume()
10031 up(&hba->host_sem); in ufshcd_wl_resume()
10037 * ufshcd_suspend - helper function for suspend operations
10041 * and set vreg and hba-vreg in lpm mode.
10049 if (!hba->is_powered) in ufshcd_suspend()
10062 hba->clk_gating.state = CLKS_OFF; in ufshcd_suspend()
10063 trace_ufshcd_clk_gating(dev_name(hba->dev), in ufshcd_suspend()
10064 hba->clk_gating.state); in ufshcd_suspend()
10075 * ufshcd_resume - helper function for resume operations
10081 * Return: 0 for success and non-zero for failure.
10087 if (!hba->is_powered) in ufshcd_resume()
10116 * ufshcd_system_suspend - system suspend callback
10122 * Return: 0 for success and non-zero for failure.
10130 if (pm_runtime_suspended(hba->dev)) in ufshcd_system_suspend()
10135 trace_ufshcd_system_suspend(dev_name(hba->dev), ret, in ufshcd_system_suspend()
10137 hba->curr_dev_pwr_mode, hba->uic_link_state); in ufshcd_system_suspend()
10143 * ufshcd_system_resume - system resume callback
10149 * Return: 0 for success and non-zero for failure.
10157 if (pm_runtime_suspended(hba->dev)) in ufshcd_system_resume()
10163 trace_ufshcd_system_resume(dev_name(hba->dev), ret, in ufshcd_system_resume()
10165 hba->curr_dev_pwr_mode, hba->uic_link_state); in ufshcd_system_resume()
10174 * ufshcd_runtime_suspend - runtime suspend callback
10179 * Return: 0 for success and non-zero for failure.
10189 trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret, in ufshcd_runtime_suspend()
10191 hba->curr_dev_pwr_mode, hba->uic_link_state); in ufshcd_runtime_suspend()
10197 * ufshcd_runtime_resume - runtime resume routine
10216 trace_ufshcd_runtime_resume(dev_name(hba->dev), ret, in ufshcd_runtime_resume()
10218 hba->curr_dev_pwr_mode, hba->uic_link_state); in ufshcd_runtime_resume()
10227 struct ufs_hba *hba = shost_priv(sdev->host); in ufshcd_wl_shutdown()
10229 down(&hba->host_sem); in ufshcd_wl_shutdown()
10230 hba->shutting_down = true; in ufshcd_wl_shutdown()
10231 up(&hba->host_sem); in ufshcd_wl_shutdown()
10236 shost_for_each_device(sdev, hba->host) { in ufshcd_wl_shutdown()
10237 if (sdev == hba->ufs_device_wlun) in ufshcd_wl_shutdown()
10239 mutex_lock(&sdev->state_mutex); in ufshcd_wl_shutdown()
10241 mutex_unlock(&sdev->state_mutex); in ufshcd_wl_shutdown()
10252 hba->is_powered = false; in ufshcd_wl_shutdown()
10256 * ufshcd_remove - de-allocate SCSI host and host memory space
10262 if (hba->ufs_device_wlun) in ufshcd_remove()
10266 ufs_sysfs_remove_nodes(hba->dev); in ufshcd_remove()
10267 cancel_delayed_work_sync(&hba->ufs_rtc_update_work); in ufshcd_remove()
10268 blk_mq_destroy_queue(hba->tmf_queue); in ufshcd_remove()
10269 blk_put_queue(hba->tmf_queue); in ufshcd_remove()
10270 blk_mq_free_tag_set(&hba->tmf_tag_set); in ufshcd_remove()
10271 if (hba->scsi_host_added) in ufshcd_remove()
10272 scsi_remove_host(hba->host); in ufshcd_remove()
10274 ufshcd_disable_intr(hba, hba->intr_mask); in ufshcd_remove()
10300 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr), in ufshcd_system_restore()
10302 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr), in ufshcd_system_restore()
10304 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr), in ufshcd_system_restore()
10306 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr), in ufshcd_system_restore()
10328 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
10333 scsi_host_put(hba->host); in ufshcd_dealloc_host()
10338 * ufshcd_set_dma_mask - Set dma mask based on the controller
10342 * Return: 0 for success, non-zero for failure.
10346 if (hba->vops && hba->vops->set_dma_mask) in ufshcd_set_dma_mask()
10347 return hba->vops->set_dma_mask(hba); in ufshcd_set_dma_mask()
10348 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) { in ufshcd_set_dma_mask()
10349 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64))) in ufshcd_set_dma_mask()
10352 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32)); in ufshcd_set_dma_mask()
10356 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
10360 * Return: 0 on success, non-zero value on failure.
10371 err = -ENODEV; in ufshcd_alloc_host()
10379 err = -ENOMEM; in ufshcd_alloc_host()
10382 host->nr_maps = HCTX_TYPE_POLL + 1; in ufshcd_alloc_host()
10384 hba->host = host; in ufshcd_alloc_host()
10385 hba->dev = dev; in ufshcd_alloc_host()
10386 hba->dev_ref_clk_freq = REF_CLK_FREQ_INVAL; in ufshcd_alloc_host()
10387 hba->nop_out_timeout = NOP_OUT_TIMEOUT; in ufshcd_alloc_host()
10389 INIT_LIST_HEAD(&hba->clk_list_head); in ufshcd_alloc_host()
10390 spin_lock_init(&hba->outstanding_lock); in ufshcd_alloc_host()
10412 * ufshcd_init - Driver initialization routine
10413 * @hba: per-adapter instance
10417 * Return: 0 on success, non-zero value on failure.
10422 struct Scsi_Host *host = hba->host; in ufshcd_init()
10423 struct device *dev = hba->dev; in ufshcd_init()
10434 dev_err(hba->dev, in ufshcd_init()
10436 err = -ENODEV; in ufshcd_init()
10440 hba->mmio_base = mmio_base; in ufshcd_init()
10441 hba->irq = irq; in ufshcd_init()
10442 hba->vps = &ufs_hba_vps; in ufshcd_init()
10454 hba->ufs_version = ufshcd_get_ufs_version(hba); in ufshcd_init()
10457 hba->intr_mask = ufshcd_get_intr_mask(hba); in ufshcd_init()
10461 dev_err(hba->dev, "set dma mask failed\n"); in ufshcd_init()
10468 dev_err(hba->dev, "Memory allocation failed\n"); in ufshcd_init()
10475 host->can_queue = hba->nutrs - UFSHCD_NUM_RESERVED; in ufshcd_init()
10476 host->cmd_per_lun = hba->nutrs - UFSHCD_NUM_RESERVED; in ufshcd_init()
10477 host->max_id = UFSHCD_MAX_ID; in ufshcd_init()
10478 host->max_lun = UFS_MAX_LUNS; in ufshcd_init()
10479 host->max_channel = UFSHCD_MAX_CHANNEL; in ufshcd_init()
10480 host->unique_id = host->host_no; in ufshcd_init()
10481 host->max_cmd_len = UFS_CDB_SIZE; in ufshcd_init()
10482 host->queuecommand_may_block = !!(hba->caps & UFSHCD_CAP_CLK_GATING); in ufshcd_init()
10484 hba->max_pwr_info.is_valid = false; in ufshcd_init()
10488 hba->host->host_no); in ufshcd_init()
10489 hba->eh_wq = create_singlethread_workqueue(eh_wq_name); in ufshcd_init()
10490 if (!hba->eh_wq) { in ufshcd_init()
10491 dev_err(hba->dev, "%s: failed to create eh workqueue\n", in ufshcd_init()
10493 err = -ENOMEM; in ufshcd_init()
10496 INIT_WORK(&hba->eh_work, ufshcd_err_handler); in ufshcd_init()
10497 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler); in ufshcd_init()
10499 sema_init(&hba->host_sem, 1); in ufshcd_init()
10502 mutex_init(&hba->uic_cmd_mutex); in ufshcd_init()
10505 mutex_init(&hba->dev_cmd.lock); in ufshcd_init()
10508 mutex_init(&hba->ee_ctrl_mutex); in ufshcd_init()
10510 mutex_init(&hba->wb_mutex); in ufshcd_init()
10511 init_rwsem(&hba->clk_scaling_lock); in ufshcd_init()
10534 dev_err(hba->dev, "request irq failed\n"); in ufshcd_init()
10537 hba->is_irq_enabled = true; in ufshcd_init()
10541 if (!hba->lsdb_sup) { in ufshcd_init()
10542 dev_err(hba->dev, "%s: failed to initialize (legacy doorbell mode not supported)\n", in ufshcd_init()
10544 err = -EINVAL; in ufshcd_init()
10547 err = scsi_add_host(host, hba->dev); in ufshcd_init()
10549 dev_err(hba->dev, "scsi_add_host failed\n"); in ufshcd_init()
10552 hba->scsi_host_added = true; in ufshcd_init()
10555 hba->tmf_tag_set = (struct blk_mq_tag_set) { in ufshcd_init()
10557 .queue_depth = hba->nutmrs, in ufshcd_init()
10561 err = blk_mq_alloc_tag_set(&hba->tmf_tag_set); in ufshcd_init()
10564 hba->tmf_queue = blk_mq_init_queue(&hba->tmf_tag_set); in ufshcd_init()
10565 if (IS_ERR(hba->tmf_queue)) { in ufshcd_init()
10566 err = PTR_ERR(hba->tmf_queue); in ufshcd_init()
10569 hba->tmf_rqs = devm_kcalloc(hba->dev, hba->nutmrs, in ufshcd_init()
10570 sizeof(*hba->tmf_rqs), GFP_KERNEL); in ufshcd_init()
10571 if (!hba->tmf_rqs) { in ufshcd_init()
10572 err = -ENOMEM; in ufshcd_init()
10584 dev_err(hba->dev, "Host controller enable failed\n"); in ufshcd_init()
10596 if (!hba->rpm_lvl) in ufshcd_init()
10597 hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state( in ufshcd_init()
10600 if (!hba->spm_lvl) in ufshcd_init()
10601 hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state( in ufshcd_init()
10605 INIT_DELAYED_WORK(&hba->rpm_dev_flush_recheck_work, ufshcd_rpm_dev_flush_recheck_work); in ufshcd_init()
10606 INIT_DELAYED_WORK(&hba->ufs_rtc_update_work, ufshcd_rtc_work); in ufshcd_init()
10608 /* Set the default auto-hiberate idle timer value to 150 ms */ in ufshcd_init()
10609 if (ufshcd_is_auto_hibern8_supported(hba) && !hba->ahit) { in ufshcd_init()
10610 hba->ahit = FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, 150) | in ufshcd_init()
10616 atomic_set(&hba->scsi_block_reqs_cnt, 0); in ufshcd_init()
10618 * We are assuming that device wasn't put in sleep/power-down in ufshcd_init()
10626 ufs_sysfs_add_nodes(hba->dev); in ufshcd_init()
10632 blk_mq_destroy_queue(hba->tmf_queue); in ufshcd_init()
10633 blk_put_queue(hba->tmf_queue); in ufshcd_init()
10635 blk_mq_free_tag_set(&hba->tmf_tag_set); in ufshcd_init()
10637 if (hba->scsi_host_added) in ufshcd_init()
10638 scsi_remove_host(hba->host); in ufshcd_init()
10640 hba->is_irq_enabled = false; in ufshcd_init()
10651 if (hba->complete_put) { in ufshcd_resume_complete()
10653 hba->complete_put = false; in ufshcd_resume_complete()
10660 struct device *dev = &hba->ufs_device_wlun->sdev_gendev; in ufshcd_rpm_ok_for_spm()
10666 spin_lock_irqsave(&dev->power.lock, flags); in ufshcd_rpm_ok_for_spm()
10667 dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl); in ufshcd_rpm_ok_for_spm()
10668 link_state = ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl); in ufshcd_rpm_ok_for_spm()
10670 hba->curr_dev_pwr_mode == dev_pwr_mode && in ufshcd_rpm_ok_for_spm()
10671 hba->uic_link_state == link_state && in ufshcd_rpm_ok_for_spm()
10672 !hba->dev_info.b_rpm_dev_flush_capable; in ufshcd_rpm_ok_for_spm()
10673 spin_unlock_irqrestore(&dev->power.lock, flags); in ufshcd_rpm_ok_for_spm()
10684 * SCSI assumes that runtime-pm and system-pm for scsi drivers in __ufshcd_suspend_prepare()
10685 * are same. And it doesn't wake up the device for system-suspend in __ufshcd_suspend_prepare()
10689 if (hba->ufs_device_wlun) { in __ufshcd_suspend_prepare()
10699 if (ret < 0 && ret != -EACCES) { in __ufshcd_suspend_prepare()
10704 hba->complete_put = true; in __ufshcd_suspend_prepare()
10720 struct ufs_hba *hba = shost_priv(sdev->host); in ufshcd_wl_poweroff()
10732 return -ENODEV; in ufshcd_wl_probe()
10734 blk_pm_runtime_init(sdev->request_queue, dev); in ufshcd_wl_probe()
10762 * gcc compilers before version 10 cannot do constant-folding for in ufshcd_check_header_layout()
10763 * sub-byte bitfields. Hence skip the layout checks for gcc 9 and in ufshcd_check_header_layout()
10807 * ufs_dev_wlun_template - describes ufs device wlun
10808 * ufs-device wlun - used to send pm commands
10809 * All luns are consumers of ufs-device wlun.
10813 * With ufs design, SSU should be sent to ufs-device wlun.