module_param_cb(uic_cmd_timeout, &uic_cmd_timeout_ops, &uic_cmd_timeout, 0644);
MODULE_PARM_DESC(uic_cmd_timeout, "UFS UIC command timeout in milliseconds. Defaults to 500ms. Supported values range from 500ms to 5 seconds inclusively");
module_param_cb(dev_cmd_timeout, &dev_cmd_timeout_ops, &dev_cmd_timeout, 0644);
MODULE_PARM_DESC(dev_cmd_timeout, "UFS Device command timeout in milliseconds. Defaults to 1.5s. Supported values range from 1ms to 30 seconds inclusively");
conststruct ufs_pm_lvl_states ufs_pm_lvl_states[] = {
[UFS_PM_LVL_0] = {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE},
[UFS_PM_LVL_1] = {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE},
[UFS_PM_LVL_2] = {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE},
[UFS_PM_LVL_3] = {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE},
[UFS_PM_LVL_4] = {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE},
[UFS_PM_LVL_5] = {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE}, /* * For DeepSleep, the link is first put in hibern8 and then off. * Leaving the link in hibern8 is not supported.
*/
[UFS_PM_LVL_6] = {UFS_DEEPSLEEP_PWR_MODE, UIC_LINK_OFF_STATE},
};
/* trace UPIU also */
ufshcd_add_cmd_upiu_trace(hba, tag, str_t); if (!trace_ufshcd_command_enabled()) return;
opcode = cmd->cmnd[0];
if (opcode == READ_10 || opcode == WRITE_10) { /* * Currently we only fully trace read(10) and write(10) commands
*/
transfer_len =
be32_to_cpu(lrbp->ucd_req_ptr->sc.exp_data_transfer_len);
lba = scsi_get_lba(cmd); if (opcode == WRITE_10)
group_id = lrbp->cmd->cmnd[6];
} elseif (opcode == UNMAP) { /* * The number of Bytes to be unmapped beginning with the lba.
*/
transfer_len = blk_rq_bytes(rq);
lba = scsi_get_lba(cmd);
}
intr = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
if (hba->mcq_enabled) { struct ufs_hw_queue *hwq = ufshcd_mcq_req_to_hwq(hba, rq);
/** * ufshcd_print_pwr_info - print power params as saved in hba * power info * @hba: per-adapter instance
*/ staticvoid ufshcd_print_pwr_info(struct ufs_hba *hba)
{ staticconstchar * const names[] = { "INVALID MODE", "FAST MODE", "SLOW_MODE", "INVALID MODE", "FASTAUTO_MODE", "SLOWAUTO_MODE", "INVALID MODE",
};
/* * Using dev_dbg to avoid messages during runtime PM to avoid * never-ending cycles of messages written back to storage by user space * causing runtime resume, causing more messages and so on.
*/
dev_dbg(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n",
__func__,
hba->pwr_info.gear_rx, hba->pwr_info.gear_tx,
hba->pwr_info.lane_rx, hba->pwr_info.lane_tx,
names[hba->pwr_info.pwr_rx],
names[hba->pwr_info.pwr_tx],
hba->pwr_info.hs_rate);
}
staticvoid ufshcd_device_reset(struct ufs_hba *hba)
{ int err;
err = ufshcd_vops_device_reset(hba);
if (!err) {
ufshcd_set_ufs_dev_active(hba); if (ufshcd_is_wb_allowed(hba)) {
hba->dev_info.wb_enabled = false;
hba->dev_info.wb_buf_flush_enabled = false;
} if (hba->dev_info.rtc_type == UFS_RTC_RELATIVE)
hba->dev_info.rtc_time_baseline = 0;
} if (err != -EOPNOTSUPP)
ufshcd_update_evt_hist(hba, UFS_EVT_DEV_RESET, err);
}
void ufshcd_delay_us(unsignedlong us, unsignedlong tolerance)
{ if (!us) return;
if (us < 10)
udelay(us); else
usleep_range(us, us + tolerance);
}
EXPORT_SYMBOL_GPL(ufshcd_delay_us);
/** * ufshcd_wait_for_register - wait for register value to change * @hba: per-adapter interface * @reg: mmio register offset * @mask: mask to apply to the read register value * @val: value to wait for * @interval_us: polling interval in microseconds * @timeout_ms: timeout in milliseconds * * Return: -ETIMEDOUT on error, zero on success.
*/ staticint ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
u32 val, unsignedlong interval_us, unsignedlong timeout_ms)
{
u32 v;
val &= mask; /* ignore bits that we don't intend to wait on */
/** * ufshcd_get_intr_mask - Get the interrupt bit mask * @hba: Pointer to adapter instance * * Return: interrupt bit mask per version
*/ staticinline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
{ if (hba->ufs_version <= ufshci_version(2, 0)) return INTERRUPT_MASK_ALL_VER_11;
return INTERRUPT_MASK_ALL_VER_21;
}
/** * ufshcd_get_ufs_version - Get the UFS version supported by the HBA * @hba: Pointer to adapter instance * * Return: UFSHCI version supported by the controller
*/ staticinline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
{
u32 ufshci_ver;
/* * UFSHCI v1.x uses a different version scheme, in order * to allow the use of comparisons with the ufshci_version * function, we convert it to the same scheme as ufs 2.0+.
*/ if (ufshci_ver & 0x00010000) return ufshci_version(1, ufshci_ver & 0x00000100);
return ufshci_ver;
}
/** * ufshcd_is_device_present - Check if any device connected to * the host controller * @hba: pointer to adapter instance * * Return: true if device present, false if no device detected
*/ staticinlinebool ufshcd_is_device_present(struct ufs_hba *hba)
{ return ufshcd_readl(hba, REG_CONTROLLER_STATUS) & DEVICE_PRESENT;
}
/** * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status * @lrbp: pointer to local command reference block * @cqe: pointer to the completion queue entry * * This function is used to get the OCS field from UTRD * * Return: the OCS field in the UTRD.
*/ staticenum utp_ocs ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp, struct cq_entry *cqe)
{ if (cqe) return le32_to_cpu(cqe->status) & MASK_OCS;
/** * ufshcd_utrl_clear() - Clear requests from the controller request list. * @hba: per adapter instance * @mask: mask with one bit set for each request to be cleared
*/ staticinlinevoid ufshcd_utrl_clear(struct ufs_hba *hba, u32 mask)
{ if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR)
mask = ~mask; /* * From the UFSHCI specification: "UTP Transfer Request List CLear * Register (UTRLCLR): This field is bit significant. Each bit * corresponds to a slot in the UTP Transfer Request List, where bit 0 * corresponds to request slot 0. A bit in this field is set to ‘0’ * by host software to indicate to the host controller that a transfer * request slot is cleared. The host controller * shall free up any resources associated to the request slot * immediately, and shall set the associated bit in UTRLDBR to ‘0’. The * host software indicates no change to request slots by setting the * associated bits in this field to ‘1’. Bits in this field shall only * be set ‘1’ or ‘0’ by host software when UTRLRSR is set to ‘1’."
*/
ufshcd_writel(hba, ~mask, REG_UTP_TRANSFER_REQ_LIST_CLEAR);
}
/** * ufshcd_utmrl_clear - Clear a bit in UTMRLCLR register * @hba: per adapter instance * @pos: position of the bit to be cleared
*/ staticinlinevoid ufshcd_utmrl_clear(struct ufs_hba *hba, u32 pos)
{ if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR)
ufshcd_writel(hba, (1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR); else
ufshcd_writel(hba, ~(1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR);
}
/** * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY * @reg: Register value of host controller status * * Return: 0 on success; a positive value if failed.
*/ staticinlineint ufshcd_get_lists_status(u32 reg)
{ return !((reg & UFSHCD_STATUS_READY) == UFSHCD_STATUS_READY);
}
/** * ufshcd_get_uic_cmd_result - Get the UIC command result * @hba: Pointer to adapter instance * * This function gets the result of UIC command completion * * Return: 0 on success; non-zero value on error.
*/ staticinlineint ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
{ return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
MASK_UIC_COMMAND_RESULT;
}
/** * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command * @hba: Pointer to adapter instance * * This function gets UIC command argument3 * * Return: 0 on success; non-zero value on error.
*/ staticinline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
{ return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
}
/** * ufshcd_is_exception_event - Check if the device raised an exception event * @ucd_rsp_ptr: pointer to response UPIU * * The function checks if the device raised an exception event indicated in * the Device Information field of response UPIU. * * Return: true if exception is raised, false otherwise.
*/ staticinlinebool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
{ return ucd_rsp_ptr->header.device_information & 1;
}
/** * ufshcd_enable_run_stop_reg - Enable run-stop registers, * When run-stop registers are set to 1, it indicates the * host controller that it can process the requests * @hba: per adapter instance
*/ staticvoid ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
{
ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
REG_UTP_TASK_REQ_LIST_RUN_STOP);
ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
}
if (ufshcd_crypto_enable(hba))
val |= CRYPTO_GENERAL_ENABLE;
ufshcd_writel(hba, val, REG_CONTROLLER_ENABLE);
}
/** * ufshcd_is_hba_active - Get controller state * @hba: per adapter instance * * Return: true if and only if the controller is active.
*/ bool ufshcd_is_hba_active(struct ufs_hba *hba)
{ return ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & CONTROLLER_ENABLE;
}
EXPORT_SYMBOL_GPL(ufshcd_is_hba_active);
/** * ufshcd_pm_qos_update - update PM QoS request * @hba: per adapter instance * @on: If True, vote for perf PM QoS mode otherwise power save mode
*/ staticvoid ufshcd_pm_qos_update(struct ufs_hba *hba, bool on)
{
guard(mutex)(&hba->pm_qos_mutex);
if (!hba->pm_qos_enabled) return;
cpu_latency_qos_update_request(&hba->pm_qos_req, on ? 0 : PM_QOS_DEFAULT_VALUE);
}
/** * ufshcd_set_clk_freq - set UFS controller clock frequencies * @hba: per adapter instance * @scale_up: If True, set max possible frequency othewise set low frequency * * Return: 0 if successful; < 0 upon failure.
*/ staticint ufshcd_set_clk_freq(struct ufs_hba *hba, bool scale_up)
{ int ret = 0; struct ufs_clk_info *clki; struct list_head *head = &hba->clk_list_head;
if (list_empty(head)) goto out;
list_for_each_entry(clki, head, list) { if (!IS_ERR_OR_NULL(clki->clk)) { if (scale_up && clki->max_freq) { if (clki->curr_freq == clki->max_freq) continue;
ret = clk_set_rate(clki->clk, clki->max_freq); if (ret) {
dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
__func__, clki->name,
clki->max_freq, ret); break;
}
trace_ufshcd_clk_scaling(hba, "scaled up", clki->name,
clki->curr_freq,
clki->max_freq);
opp = dev_pm_opp_find_freq_floor_indexed(hba->dev,
&freq, 0); if (IS_ERR(opp)) return PTR_ERR(opp);
ret = dev_pm_opp_set_opp(hba->dev, opp);
dev_pm_opp_put(opp);
return ret;
}
/** * ufshcd_scale_clks - scale up or scale down UFS controller clocks * @hba: per adapter instance * @freq: frequency to scale * @scale_up: True if scaling up and false if scaling down * * Return: 0 if successful; < 0 upon failure.
*/ staticint ufshcd_scale_clks(struct ufs_hba *hba, unsignedlong freq, bool scale_up)
{ int ret = 0;
ktime_t start = ktime_get();
ret = ufshcd_vops_clk_scale_notify(hba, scale_up, freq, PRE_CHANGE); if (ret) goto out;
if (hba->use_pm_opp)
ret = ufshcd_opp_set_rate(hba, freq); else
ret = ufshcd_set_clk_freq(hba, scale_up); if (ret) goto out;
ret = ufshcd_vops_clk_scale_notify(hba, scale_up, freq, POST_CHANGE); if (ret) { if (hba->use_pm_opp)
ufshcd_opp_set_rate(hba,
hba->devfreq->previous_freq); else
ufshcd_set_clk_freq(hba, !scale_up); goto out;
}
/** * ufshcd_is_devfreq_scaling_required - check if scaling is required or not * @hba: per adapter instance * @freq: frequency to scale * @scale_up: True if scaling up and false if scaling down * * Return: true if scaling is required, false otherwise.
*/ staticbool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba, unsignedlong freq, bool scale_up)
{ struct ufs_clk_info *clki; struct list_head *head = &hba->clk_list_head;
if (list_empty(head)) returnfalse;
if (hba->use_pm_opp) return freq != hba->clk_scaling.target_freq;
list_for_each_entry(clki, head, list) { if (!IS_ERR_OR_NULL(clki->clk)) { if (scale_up && clki->max_freq) { if (clki->curr_freq == clki->max_freq) continue; returntrue;
} elseif (!scale_up && clki->min_freq) { if (clki->curr_freq == clki->min_freq) continue; returntrue;
}
}
}
returnfalse;
}
/* * Determine the number of pending commands by counting the bits in the SCSI * device budget maps. This approach has been selected because a bit is set in * the budget map before scsi_host_queue_ready() checks the host_self_blocked * flag. The host_self_blocked flag can be modified by calling * scsi_block_requests() or scsi_unblock_requests().
*/ static u32 ufshcd_pending_cmds(struct ufs_hba *hba)
{ conststruct scsi_device *sdev; unsignedlong flags;
u32 pending = 0;
/* * Wait until all pending SCSI commands and TMFs have finished or the timeout * has expired. * * Return: 0 upon success; -EBUSY upon timeout.
*/ staticint ufshcd_wait_for_pending_cmds(struct ufs_hba *hba,
u64 wait_timeout_us)
{ int ret = 0;
u32 tm_doorbell;
u32 tr_pending; bool timeout = false, do_last_check = false;
ktime_t start;
ufshcd_hold(hba); /* * Wait for all the outstanding tasks/transfer requests. * Verify by checking the doorbell registers are clear.
*/
start = ktime_get(); do { if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) {
ret = -EBUSY; goto out;
}
io_schedule_timeout(msecs_to_jiffies(20)); if (ktime_to_us(ktime_sub(ktime_get(), start)) >
wait_timeout_us) {
timeout = true; /* * We might have scheduled out for long time so make * sure to check if doorbells are cleared by this time * or not.
*/
do_last_check = true;
}
} while (tm_doorbell || tr_pending);
if (timeout) {
dev_err(hba->dev, "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n",
__func__, tm_doorbell, tr_pending);
ret = -EBUSY;
}
out:
ufshcd_release(hba); return ret;
}
/** * ufshcd_scale_gear - scale up/down UFS gear * @hba: per adapter instance * @target_gear: target gear to scale to * @scale_up: True for scaling up gear and false for scaling down * * Return: 0 for success; -EBUSY if scaling can't happen at this time; * non-zero for any other errors.
*/ staticint ufshcd_scale_gear(struct ufs_hba *hba, u32 target_gear, bool scale_up)
{ int ret = 0; struct ufs_pa_layer_attr new_pwr_info;
/* Legacy gear scaling, in case vops_freq_to_gear_speed() is not implemented */ if (scale_up) {
memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info, sizeof(struct ufs_pa_layer_attr));
} else {
memcpy(&new_pwr_info, &hba->pwr_info, sizeof(struct ufs_pa_layer_attr));
if (hba->pwr_info.gear_tx > hba->clk_scaling.min_gear ||
hba->pwr_info.gear_rx > hba->clk_scaling.min_gear) { /* save the current power mode */
memcpy(&hba->clk_scaling.saved_pwr_info,
&hba->pwr_info, sizeof(struct ufs_pa_layer_attr));
config_pwr_mode: /* check if the power mode needs to be changed or not? */
ret = ufshcd_config_pwr_mode(hba, &new_pwr_info); if (ret)
dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d)",
__func__, ret,
hba->pwr_info.gear_tx, hba->pwr_info.gear_rx,
new_pwr_info.gear_tx, new_pwr_info.gear_rx);
return ret;
}
/* * Wait until all pending SCSI commands and TMFs have finished or the timeout * has expired. * * Return: 0 upon success; -EBUSY upon timeout.
*/ staticint ufshcd_clock_scaling_prepare(struct ufs_hba *hba, u64 timeout_us)
{ int ret = 0; /* * make sure that there are no outstanding requests when * clock scaling is in progress
*/
mutex_lock(&hba->host->scan_mutex);
blk_mq_quiesce_tagset(&hba->host->tag_set);
mutex_lock(&hba->wb_mutex);
down_write(&hba->clk_scaling_lock);
if (!hba->clk_scaling.is_allowed ||
ufshcd_wait_for_pending_cmds(hba, timeout_us)) {
ret = -EBUSY;
up_write(&hba->clk_scaling_lock);
mutex_unlock(&hba->wb_mutex);
blk_mq_unquiesce_tagset(&hba->host->tag_set);
mutex_unlock(&hba->host->scan_mutex); goto out;
}
/* let's not get into low power until clock scaling is completed */
ufshcd_hold(hba);
out: return ret;
}
staticvoid ufshcd_clock_scaling_unprepare(struct ufs_hba *hba, int err)
{
up_write(&hba->clk_scaling_lock);
/* Enable Write Booster if current gear requires it else disable it */ if (ufshcd_enable_wb_if_scaling_up(hba) && !err)
ufshcd_wb_toggle(hba, hba->pwr_info.gear_rx >= hba->clk_scaling.wb_gear);
/** * ufshcd_devfreq_scale - scale up/down UFS clocks and gear * @hba: per adapter instance * @freq: frequency to scale * @scale_up: True for scaling up and false for scalin down * * Return: 0 for success; -EBUSY if scaling can't happen at this time; non-zero * for any other errors.
*/ staticint ufshcd_devfreq_scale(struct ufs_hba *hba, unsignedlong freq, bool scale_up)
{
u32 old_gear = hba->pwr_info.gear_rx;
u32 new_gear = 0; int ret = 0;
ret = ufshcd_clock_scaling_prepare(hba, 1 * USEC_PER_SEC); if (ret) return ret;
/* scale down the gear before scaling down clocks */ if (!scale_up) {
ret = ufshcd_scale_gear(hba, new_gear, false); if (ret) goto out_unprepare;
}
ret = ufshcd_scale_clks(hba, freq, scale_up); if (ret) { if (!scale_up)
ufshcd_scale_gear(hba, old_gear, true); goto out_unprepare;
}
/* scale up the gear after scaling up clocks */ if (scale_up) {
ret = ufshcd_scale_gear(hba, new_gear, true); if (ret) {
ufshcd_scale_clks(hba, hba->devfreq->previous_freq, false); goto out_unprepare;
}
}
if (!ufshcd_is_clkscaling_supported(hba)) return -EINVAL;
if (hba->use_pm_opp) { struct dev_pm_opp *opp;
/* Get the recommended frequency from OPP framework */
opp = devfreq_recommended_opp(dev, freq, flags); if (IS_ERR(opp)) return PTR_ERR(opp);
dev_pm_opp_put(opp);
} else { /* Override with the closest supported frequency */
clki = list_first_entry(&hba->clk_list_head, struct ufs_clk_info,
list);
*freq = (unsignedlong) clk_round_rate(clki->clk, *freq);
}
scoped_guard(spinlock_irqsave, &hba->clk_scaling.lock)
{ if (ufshcd_eh_in_progress(hba)) return 0;
/* Skip scaling clock when clock scaling is suspended */ if (hba->clk_scaling.is_suspended) {
dev_warn(hba->dev, "clock scaling is suspended, skip"); return 0;
}
if (!hba->clk_scaling.active_reqs)
sched_clk_scaling_suspend_work = true;
if (list_empty(clk_list)) goto out;
/* Decide based on the target or rounded-off frequency and update */ if (hba->use_pm_opp)
scale_up = *freq > hba->clk_scaling.target_freq; else
scale_up = *freq == clki->max_freq;
if (!hba->use_pm_opp && !scale_up)
*freq = clki->min_freq;
/* Update the frequency */ if (!ufshcd_is_devfreq_scaling_required(hba, *freq, scale_up)) {
ret = 0; goto out; /* no state change required */
}
}
start = ktime_get();
ret = ufshcd_devfreq_scale(hba, *freq, scale_up); if (!ret)
hba->clk_scaling.target_freq = *freq;
if (!ufshcd_is_clkscaling_supported(hba)) return -EINVAL;
memset(stat, 0, sizeof(*stat));
guard(spinlock_irqsave)(&hba->clk_scaling.lock);
curr_t = ktime_get(); if (!scaling->window_start_t) goto start_window;
/* * If current frequency is 0, then the ondemand governor considers * there's no initial frequency set. And it always requests to set * to max. frequency.
*/ if (hba->use_pm_opp) {
stat->current_frequency = hba->clk_scaling.target_freq;
} else { struct list_head *clk_list = &hba->clk_list_head; struct ufs_clk_info *clki;
/* Exit from hibern8 */ if (ufshcd_can_hibern8_during_gating(hba)) { /* Prevent gating in this path */
hba->clk_gating.is_suspended = true; if (ufshcd_is_link_hibern8(hba)) {
ret = ufshcd_uic_hibern8_exit(hba); if (ret)
dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
__func__, ret); else
ufshcd_set_link_active(hba);
}
hba->clk_gating.is_suspended = false;
}
}
/** * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release. * Also, exit from hibern8 mode and set the link as active. * @hba: per adapter instance
*/ void ufshcd_hold(struct ufs_hba *hba)
{ bool flush_result; unsignedlong flags;
if (!ufshcd_is_clkgating_allowed(hba) ||
!hba->clk_gating.is_initialized) return;
spin_lock_irqsave(&hba->clk_gating.lock, flags);
hba->clk_gating.active_reqs++;
start: switch (hba->clk_gating.state) { case CLKS_ON: /* * Wait for the ungate work to complete if in progress. * Though the clocks may be in ON state, the link could * still be in hibner8 state if hibern8 is allowed * during clock gating. * Make sure we exit hibern8 state also in addition to * clocks being ON.
*/ if (ufshcd_can_hibern8_during_gating(hba) &&
ufshcd_is_link_hibern8(hba)) {
spin_unlock_irqrestore(&hba->clk_gating.lock, flags);
flush_result = flush_work(&hba->clk_gating.ungate_work); if (hba->clk_gating.is_suspended && !flush_result) return;
spin_lock_irqsave(&hba->clk_gating.lock, flags); goto start;
} break; case REQ_CLKS_OFF: if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
hba->clk_gating.state = CLKS_ON;
trace_ufshcd_clk_gating(hba,
hba->clk_gating.state); break;
} /* * If we are here, it means gating work is either done or * currently running. Hence, fall through to cancel gating * work and to enable clocks.
*/
fallthrough; case CLKS_OFF:
hba->clk_gating.state = REQ_CLKS_ON;
trace_ufshcd_clk_gating(hba,
hba->clk_gating.state);
queue_work(hba->clk_gating.clk_gating_workq,
&hba->clk_gating.ungate_work); /* * fall through to check if we should wait for this * work to be done or not.
*/
fallthrough; case REQ_CLKS_ON:
spin_unlock_irqrestore(&hba->clk_gating.lock, flags);
flush_work(&hba->clk_gating.ungate_work); /* Make sure state is CLKS_ON before returning */
spin_lock_irqsave(&hba->clk_gating.lock, flags); goto start; default:
dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
__func__, hba->clk_gating.state); break;
}
spin_unlock_irqrestore(&hba->clk_gating.lock, flags);
}
EXPORT_SYMBOL_GPL(ufshcd_hold);
scoped_guard(spinlock_irqsave, &hba->clk_gating.lock) { /* * In case you are here to cancel this work the gating state * would be marked as REQ_CLKS_ON. In this case save time by * skipping the gating work and exit after changing the clock * state to CLKS_ON.
*/ if (hba->clk_gating.is_suspended ||
hba->clk_gating.state != REQ_CLKS_OFF) {
hba->clk_gating.state = CLKS_ON;
trace_ufshcd_clk_gating(hba,
hba->clk_gating.state); return;
}
/* put the link into hibern8 mode before turning off clocks */ if (ufshcd_can_hibern8_during_gating(hba)) {
ret = ufshcd_uic_hibern8_enter(hba); if (ret) {
hba->clk_gating.state = CLKS_ON;
dev_err(hba->dev, "%s: hibern8 enter failed %d\n",
__func__, ret);
trace_ufshcd_clk_gating(hba,
hba->clk_gating.state); return;
}
ufshcd_set_link_hibern8(hba);
}
ufshcd_disable_irq(hba);
ufshcd_setup_clocks(hba, false);
/* Put the host controller in low power mode if possible */
ufshcd_hba_vreg_set_lpm(hba); /* * In case you are here to cancel this work the gating state * would be marked as REQ_CLKS_ON. In this case keep the state * as REQ_CLKS_ON which would anyway imply that clocks are off * and a request to turn them on is pending. By doing this way, * we keep the state machine in tact and this would ultimately * prevent from doing cancel work multiple times when there are * new requests arriving before the current cancel work is done.
*/
guard(spinlock_irqsave)(&hba->clk_gating.lock); if (hba->clk_gating.state == REQ_CLKS_OFF) {
hba->clk_gating.state = CLKS_OFF;
trace_ufshcd_clk_gating(hba,
hba->clk_gating.state);
}
}
/** * ufshcd_copy_sense_data - Copy sense data in case of check condition * @lrbp: pointer to local reference block
*/ staticinlinevoid ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
{
u8 *const sense_buffer = lrbp->cmd->sense_buffer;
u16 resp_len; int len;
resp_len = be16_to_cpu(lrbp->ucd_rsp_ptr->header.data_segment_length); if (sense_buffer && resp_len) { int len_to_copy;
len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
len_to_copy = min_t(int, UFS_SENSE_SIZE, len);
/** * ufshcd_copy_query_response() - Copy the Query Response and the data * descriptor * @hba: per adapter instance * @lrbp: pointer to local reference block * * Return: 0 upon success; < 0 upon failure.
*/ static int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
{ struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
/* * The UFSHCI 3.0 specification does not define MCQ_SUPPORT and * LSDB_SUPPORT, but [31:29] as reserved bits with reset value 0s, which * means we can simply read values regardless of version.
*/
hba->mcq_sup = FIELD_GET(MASK_MCQ_SUPPORT, hba->capabilities); /* * 0h: legacy single doorbell support is available * 1h: indicate that legacy single doorbell support has been removed
*/ if (!(hba->quirks & UFSHCD_QUIRK_BROKEN_LSDBS_CAP))
hba->lsdb_sup = !FIELD_GET(MASK_LSDB_SUPPORT, hba->capabilities); else
hba->lsdb_sup = true;
/** * ufshcd_ready_for_uic_cmd - Check if controller is ready * to accept UIC commands * @hba: per adapter instance * * Return: true on success, else false.
*/ staticinlinebool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
{
u32 val; int ret = read_poll_timeout(ufshcd_readl, val, val & UIC_COMMAND_READY,
500, uic_cmd_timeout * 1000, false, hba,
REG_CONTROLLER_STATUS); return ret == 0;
}
/** * ufshcd_get_upmcrs - Get the power mode change request status * @hba: Pointer to adapter instance * * This function gets the UPMCRS field of HCS register * * Return: value of UPMCRS field.
*/ staticinline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
{ return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
}
/** * ufshcd_dispatch_uic_cmd - Dispatch an UIC command to the Unipro layer * @hba: per adapter instance * @uic_cmd: UIC command
*/ staticinlinevoid
ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
{
lockdep_assert_held(&hba->uic_cmd_mutex);
/** * ufshcd_wait_for_uic_cmd - Wait for completion of an UIC command * @hba: per adapter instance * @uic_cmd: UIC command * * Return: 0 only if success.
*/ staticint
ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
{ int ret; unsignedlong flags;
lockdep_assert_held(&hba->uic_cmd_mutex);
if (wait_for_completion_timeout(&uic_cmd->done,
msecs_to_jiffies(uic_cmd_timeout))) {
ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
} else {
ret = -ETIMEDOUT;
dev_err(hba->dev, "uic cmd 0x%x with arg3 0x%x completion timeout\n",
uic_cmd->command, uic_cmd->argument3);
if (!uic_cmd->cmd_active) {
dev_err(hba->dev, "%s: UIC cmd has been completed, return the result\n",
__func__);
ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
}
}
/** * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result * @hba: per adapter instance * @uic_cmd: UIC command * * Return: 0 only if success.
*/ int ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
{ unsignedlong flags; int ret;
if (hba->quirks & UFSHCD_QUIRK_BROKEN_UIC_CMD) return 0;
ret = __ufshcd_send_uic_cmd(hba, uic_cmd); if (!ret)
ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
mutex_unlock(&hba->uic_cmd_mutex);
ufshcd_release(hba); return ret;
}
/** * ufshcd_sgl_to_prdt - SG list to PRTD (Physical Region Description Table, 4DW format) * @hba: per-adapter instance * @lrbp: pointer to local reference block * @sg_entries: The number of sg lists actually used * @sg_list: Pointer to SG list
*/ staticvoid ufshcd_sgl_to_prdt(struct ufs_hba *hba, struct ufshcd_lrb *lrbp, int sg_entries, struct scatterlist *sg_list)
{ struct ufshcd_sg_entry *prd; struct scatterlist *sg; int i;
for_each_sg(sg_list, sg, sg_entries, i) { constunsignedint len = sg_dma_len(sg);
/* * From the UFSHCI spec: "Data Byte Count (DBC): A '0' * based value that indicates the length, in bytes, of * the data block. A maximum of length of 256KB may * exist for any entry. Bits 1:0 of this field shall be * 11b to indicate Dword granularity. A value of '3' * indicates 4 bytes, '7' indicates 8 bytes, etc."
*/
WARN_ONCE(len > SZ_256K, "len = %#x\n", len);
prd->size = cpu_to_le32(len - 1);
prd->addr = cpu_to_le64(sg->dma_address);
prd->reserved = 0;
prd = (void *)prd + ufshcd_sg_entry_size(hba);
}
} else {
lrbp->utr_descriptor_ptr->prd_table_length = 0;
}
}
/** * ufshcd_map_sg - Map scatter-gather list to prdt * @hba: per adapter instance * @lrbp: pointer to local reference block * * Return: 0 in case of success, non-zero value in case of failure.
*/ staticint ufshcd_map_sg(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
{ struct scsi_cmnd *cmd = lrbp->cmd; int sg_segments = scsi_dma_map(cmd);
/** * ufshcd_prepare_req_desc_hdr - Fill UTP Transfer request descriptor header according to request * descriptor according to request * @hba: per adapter instance * @lrbp: pointer to local reference block * @upiu_flags: flags required in the header * @cmd_dir: requests data direction * @ehs_length: Total EHS Length (in 32‐bytes units of all Extra Header Segments)
*/ staticvoid
ufshcd_prepare_req_desc_hdr(struct ufs_hba *hba, struct ufshcd_lrb *lrbp,
u8 *upiu_flags, enum dma_data_direction cmd_dir, int ehs_length)
{ struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr; struct request_desc_header *h = &req_desc->header; enum utp_data_direction data_direction;
/* Prepare crypto related dwords */
ufshcd_prepare_req_desc_hdr_crypto(lrbp, h);
/* * assigning invalid value for command status. Controller * updates OCS on command completion, with the command * status
*/
h->ocs = OCS_INVALID_COMMAND_STATUS;
/* * Associate the UFS controller queue with the default and poll HCTX types. * Initialize the mq_map[] arrays.
*/ staticvoid ufshcd_map_queues(struct Scsi_Host *shost)
{ struct ufs_hba *hba = shost_priv(shost); int i, queue_offset = 0;
/** * ufshcd_queuecommand - main entry point for SCSI requests * @host: SCSI host pointer * @cmd: command from SCSI Midlayer * * Return: 0 for success, non-zero in case of failure.
*/ staticint ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
{ struct ufs_hba *hba = shost_priv(host); int tag = scsi_cmd_to_rq(cmd)->tag; struct ufshcd_lrb *lrbp; int err = 0; struct ufs_hw_queue *hwq = NULL;
switch (hba->ufshcd_state) { case UFSHCD_STATE_OPERATIONAL: break; case UFSHCD_STATE_EH_SCHEDULED_NON_FATAL: /* * SCSI error handler can call ->queuecommand() while UFS error * handler is in progress. Error interrupts could change the * state from UFSHCD_STATE_RESET to * UFSHCD_STATE_EH_SCHEDULED_NON_FATAL. Prevent requests * being issued in that case.
*/ if (ufshcd_eh_in_progress(hba)) {
err = SCSI_MLQUEUE_HOST_BUSY; goto out;
} break; case UFSHCD_STATE_EH_SCHEDULED_FATAL: /* * pm_runtime_get_sync() is used at error handling preparation * stage. If a scsi cmd, e.g. the SSU cmd, is sent from hba's * PM ops, it can never be finished if we let SCSI layer keep * retrying it, which gets err handler stuck forever. Neither * can we let the scsi cmd pass through, because UFS is in bad * state, the scsi cmd may eventually time out, which will get * err handler blocked for too long. So, just fail the scsi cmd * sent from PM ops, err handler can recover PM error anyways.
*/ if (hba->pm_op_in_progress) {
hba->force_reset = true;
set_host_byte(cmd, DID_BAD_TARGET);
scsi_done(cmd); goto out;
}
fallthrough; case UFSHCD_STATE_RESET:
err = SCSI_MLQUEUE_HOST_BUSY; goto out; case UFSHCD_STATE_ERROR:
set_host_byte(cmd, DID_ERROR);
scsi_done(cmd); goto out;
}
/* * Return: 0 upon success; < 0 upon failure.
*/ staticint ufshcd_compose_dev_cmd(struct ufs_hba *hba, struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
{
ufshcd_setup_dev_cmd(hba, lrbp, cmd_type, 0, tag);
return ufshcd_compose_devman_upiu(hba, lrbp);
}
/* * Check with the block layer if the command is inflight * @cmd: command to check. * * Return: true if command is inflight; false if not.
*/ bool ufshcd_cmd_inflight(struct scsi_cmnd *cmd)
{ return cmd && blk_mq_rq_state(scsi_cmd_to_rq(cmd)) == MQ_RQ_IN_FLIGHT;
}
/* * Clear the pending command in the controller and wait until * the controller confirms that the command has been cleared. * @hba: per adapter instance * @task_tag: The tag number of the command to be cleared.
*/ staticint ufshcd_clear_cmd(struct ufs_hba *hba, u32 task_tag)
{
u32 mask; int err;
if (hba->mcq_enabled) { /* * MCQ mode. Clean up the MCQ resources similar to * what the ufshcd_utrl_clear() does for SDB mode.
*/
err = ufshcd_mcq_sq_cleanup(hba, task_tag); if (err) {
dev_err(hba->dev, "%s: failed tag=%d. err=%d\n",
__func__, task_tag, err); return err;
} return 0;
}
mask = 1U << task_tag;
/* clear outstanding transaction before retry */
ufshcd_utrl_clear(hba, mask);
/* * wait for h/w to clear corresponding bit in door-bell. * max. wait is 1 sec.
*/ return ufshcd_wait_for_register(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL,
mask, ~mask, 1000, 1000);
}
/** * ufshcd_dev_cmd_completion() - handles device management command responses * @hba: per adapter instance * @lrbp: pointer to local reference block * * Return: 0 upon success; < 0 upon failure.
*/ staticint
ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
{ enum upiu_response_transaction resp; int err = 0;
/* * Return: 0 upon success; > 0 in case the UFS device reported an OCS error; * < 0 if another error occurred.
*/ staticint ufshcd_wait_for_dev_cmd(struct ufs_hba *hba, struct ufshcd_lrb *lrbp, int max_timeout)
{ unsignedlong time_left = msecs_to_jiffies(max_timeout); unsignedlong flags; bool pending; int err;
if (likely(time_left)) {
err = ufshcd_get_tr_ocs(lrbp, NULL); if (!err)
err = ufshcd_dev_cmd_completion(hba, lrbp);
} else {
err = -ETIMEDOUT;
dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
__func__, lrbp->task_tag);
/* MCQ mode */ if (hba->mcq_enabled) { /* successfully cleared the command, retry if needed */ if (ufshcd_clear_cmd(hba, lrbp->task_tag) == 0)
err = -EAGAIN; return err;
}
/* SDB mode */ if (ufshcd_clear_cmd(hba, lrbp->task_tag) == 0) { /* successfully cleared the command, retry if needed */
err = -EAGAIN; /* * Since clearing the command succeeded we also need to * clear the task tag bit from the outstanding_reqs * variable.
*/
spin_lock_irqsave(&hba->outstanding_lock, flags);
pending = test_bit(lrbp->task_tag,
&hba->outstanding_reqs); if (pending)
__clear_bit(lrbp->task_tag,
&hba->outstanding_reqs);
spin_unlock_irqrestore(&hba->outstanding_lock, flags);
if (!pending) { /* * The completion handler ran while we tried to * clear the command.
*/
time_left = 1; goto retry;
}
} else {
dev_err(hba->dev, "%s: failed to clear tag %d\n",
__func__, lrbp->task_tag);
/* * Return: 0 upon success; > 0 in case the UFS device reported an OCS error; * < 0 if another error occurred.
*/ staticint ufshcd_issue_dev_cmd(struct ufs_hba *hba, struct ufshcd_lrb *lrbp, const u32 tag, int timeout)
{ int err;
/** * ufshcd_exec_dev_cmd - API for sending device management requests * @hba: UFS hba * @cmd_type: specifies the type (NOP, Query...) * @timeout: timeout in milliseconds * * Return: 0 upon success; > 0 in case the UFS device reported an OCS error; * < 0 if another error occurred. * * NOTE: Since there is only one available tag for device management commands, * it is expected you hold the hba->dev_cmd.lock mutex.
*/ staticint ufshcd_exec_dev_cmd(struct ufs_hba *hba, enum dev_cmd_type cmd_type, int timeout)
{ const u32 tag = hba->reserved_slot; struct ufshcd_lrb *lrbp = &hba->lrb[tag]; int err;
/* Protects use of hba->reserved_slot. */
lockdep_assert_held(&hba->dev_cmd.lock);
err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag); if (unlikely(err)) return err;
/** * ufshcd_init_query() - init the query response and request parameters * @hba: per-adapter instance * @request: address of the request pointer to be initialized * @response: address of the response pointer to be initialized * @opcode: operation to perform * @idn: flag idn to access * @index: LU number to access * @selector: query/flag/descriptor further identification
*/ staticinlinevoid ufshcd_init_query(struct ufs_hba *hba, struct ufs_query_req **request, struct ufs_query_res **response, enum query_opcode opcode, u8 idn, u8 index, u8 selector)
{
*request = &hba->dev_cmd.query.request;
*response = &hba->dev_cmd.query.response;
memset(*request, 0, sizeof(struct ufs_query_req));
memset(*response, 0, sizeof(struct ufs_query_res));
(*request)->upiu_req.opcode = opcode;
(*request)->upiu_req.idn = idn;
(*request)->upiu_req.index = index;
(*request)->upiu_req.selector = selector;
}
/* * Return: 0 upon success; > 0 in case the UFS device reported an OCS error; * < 0 if another error occurred.
*/ staticint ufshcd_query_flag_retry(struct ufs_hba *hba, enum query_opcode opcode, enum flag_idn idn, u8 index, bool *flag_res)
{ int ret; int retries;
for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
ret = ufshcd_query_flag(hba, opcode, idn, index, flag_res); if (ret)
dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
__func__, ret, retries); else break;
}
if (ret)
dev_err(hba->dev, "%s: query flag, opcode %d, idn %d, failed with error %d after %d retries\n",
__func__, opcode, idn, ret, retries); return ret;
}
/** * ufshcd_query_flag() - API function for sending flag query requests * @hba: per-adapter instance * @opcode: flag query to perform * @idn: flag idn to access * @index: flag index to access * @flag_res: the flag value after the query request completes * * Return: 0 upon success; > 0 in case the UFS device reported an OCS error; * < 0 if another error occurred.
*/ int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode, enum flag_idn idn, u8 index, bool *flag_res)
{ struct ufs_query_req *request = NULL; struct ufs_query_res *response = NULL; int err, selector = 0; int timeout = dev_cmd_timeout;
/** * ufshcd_query_attr - API function for sending attribute requests * @hba: per-adapter instance * @opcode: attribute opcode * @idn: attribute idn to access * @index: index field * @selector: selector field * @attr_val: the attribute value after the query request completes * * Return: 0 upon success; > 0 in case the UFS device reported an OCS error; * < 0 if another error occurred.
*/ int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
{ struct ufs_query_req *request = NULL; struct ufs_query_res *response = NULL; int err;
BUG_ON(!hba);
if (!attr_val) {
dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
__func__, opcode); return -EINVAL;
}
/** * ufshcd_query_attr_retry() - API function for sending query * attribute with retries * @hba: per-adapter instance * @opcode: attribute opcode * @idn: attribute idn to access * @index: index field * @selector: selector field * @attr_val: the attribute value after the query request * completes * * Return: 0 upon success; > 0 in case the UFS device reported an OCS error; * < 0 if another error occurred.
*/ int ufshcd_query_attr_retry(struct ufs_hba *hba, enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
u32 *attr_val)
{ int ret = 0;
u32 retries;
for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
ret = ufshcd_query_attr(hba, opcode, idn, index,
selector, attr_val); if (ret)
dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
__func__, ret, retries); else break;
}
if (ret)
dev_err(hba->dev, "%s: query attribute, idn %d, failed with error %d after %d retries\n",
__func__, idn, ret, QUERY_REQ_RETRIES); return ret;
}
/* * Return: 0 upon success; > 0 in case the UFS device reported an OCS error; * < 0 if another error occurred.
*/ staticint __ufshcd_query_descriptor(struct ufs_hba *hba, enum query_opcode opcode, enum desc_idn idn, u8 index,
u8 selector, u8 *desc_buf, int *buf_len)
{ struct ufs_query_req *request = NULL; struct ufs_query_res *response = NULL; int err;
BUG_ON(!hba);
if (!desc_buf) {
dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
__func__, opcode); return -EINVAL;
}
if (*buf_len < QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
__func__, *buf_len); return -EINVAL;
}
/** * ufshcd_query_descriptor_retry - API function for sending descriptor requests * @hba: per-adapter instance * @opcode: attribute opcode * @idn: attribute idn to access * @index: index field * @selector: selector field * @desc_buf: the buffer that contains the descriptor * @buf_len: length parameter passed to the device * * The buf_len parameter will contain, on return, the length parameter * received on the response. * * Return: 0 upon success; > 0 in case the UFS device reported an OCS error; * < 0 if another error occurred.
*/ int ufshcd_query_descriptor_retry(struct ufs_hba *hba, enum query_opcode opcode, enum desc_idn idn, u8 index,
u8 selector,
u8 *desc_buf, int *buf_len)
{ int err; int retries;
/** * ufshcd_read_desc_param - read the specified descriptor parameter * @hba: Pointer to adapter instance * @desc_id: descriptor idn value * @desc_index: descriptor index * @param_offset: offset of the parameter to read * @param_read_buf: pointer to buffer where parameter would be read * @param_size: sizeof(param_read_buf) * * Return: 0 upon success; > 0 in case the UFS device reported an OCS error; * < 0 if another error occurred.
*/ int ufshcd_read_desc_param(struct ufs_hba *hba, enum desc_idn desc_id, int desc_index,
u8 param_offset,
u8 *param_read_buf,
u8 param_size)
{ int ret;
u8 *desc_buf; int buff_len = QUERY_DESC_MAX_SIZE; bool is_kmalloc = true;
if (param_offset >= buff_len) {
dev_err(hba->dev, "%s: Invalid offset 0x%x in descriptor IDN 0x%x, length 0x%x\n",
__func__, param_offset, desc_id, buff_len);
ret = -EINVAL; goto out;
}
/* Sanity check */ if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header\n",
__func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
ret = -EINVAL; goto out;
}
if (is_kmalloc) { /* Make sure we don't copy more data than available */ if (param_offset >= buff_len)
ret = -EINVAL; else
memcpy(param_read_buf, &desc_buf[param_offset],
min_t(u32, param_size, buff_len - param_offset));
}
out: if (is_kmalloc)
kfree(desc_buf); return ret;
}
/** * struct uc_string_id - unicode string * * @len: size of this descriptor inclusive * @type: descriptor type * @uc: unicode string character
*/ struct uc_string_id {
u8 len;
u8 type; wchar_t uc[];
} __packed;
/** * ufshcd_read_string_desc - read string descriptor * @hba: pointer to adapter instance * @desc_index: descriptor index * @buf: pointer to buffer where descriptor would be read, * the caller should free the memory. * @ascii: if true convert from unicode to ascii characters * null terminated string. * * Return: * * string size on success. * * -ENOMEM: on allocation failure * * -EINVAL: on a wrong parameter
*/ int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index,
u8 **buf, bool ascii)
{ struct uc_string_id *uc_str;
u8 *str; int ret;
if (!buf) return -EINVAL;
uc_str = kzalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL); if (!uc_str) return -ENOMEM;
if (uc_str->len <= QUERY_DESC_HDR_SIZE) {
dev_dbg(hba->dev, "String Desc is of zero length\n");
str = NULL;
ret = 0; goto out;
}
if (ascii) {
ssize_t ascii_len; int i; /* remove header and divide by 2 to move from UTF16 to UTF8 */
ascii_len = (uc_str->len - QUERY_DESC_HDR_SIZE) / 2 + 1;
str = kzalloc(ascii_len, GFP_KERNEL); if (!str) {
ret = -ENOMEM; goto out;
}
/* * the descriptor contains string in UTF16 format * we need to convert to utf-8 so it can be displayed
*/
ret = utf16s_to_utf8s(uc_str->uc,
uc_str->len - QUERY_DESC_HDR_SIZE,
UTF16_BIG_ENDIAN, str, ascii_len - 1);
/* replace non-printable or non-ASCII characters with spaces */ for (i = 0; i < ret; i++)
str[i] = ufshcd_remove_non_printable(str[i]);
str[ret++] = '\0';
} else {
str = kmemdup(uc_str, uc_str->len, GFP_KERNEL); if (!str) {
ret = -ENOMEM; goto out;
}
ret = uc_str->len;
}
out:
*buf = str;
kfree(uc_str); return ret;
}
/** * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter * @hba: Pointer to adapter instance * @lun: lun id * @param_offset: offset of the parameter to read * @param_read_buf: pointer to buffer where parameter would be read * @param_size: sizeof(param_read_buf) * * Return: 0 in case of success; < 0 upon failure.
*/ staticinlineint ufshcd_read_unit_desc_param(struct ufs_hba *hba, int lun, enum unit_desc_param param_offset,
u8 *param_read_buf,
u32 param_size)
{ /* * Unit descriptors are only available for general purpose LUs (LUN id * from 0 to 7) and RPMB Well known LU.
*/ if (!ufs_is_valid_unit_desc_lun(&hba->dev_info, lun)) return -EOPNOTSUPP;
if (hba->dev_info.wspecversion >= 0x300) {
err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
QUERY_ATTR_IDN_REF_CLK_GATING_WAIT_TIME, 0, 0,
&gating_wait); if (err)
dev_err(hba->dev, "Failed reading bRefClkGatingWait. err = %d, use default %uus\n",
err, gating_wait);
if (gating_wait == 0) {
gating_wait = UFSHCD_REF_CLK_GATING_WAIT_US;
dev_err(hba->dev, "Undefined ref clk gating wait time, use default %uus\n",
gating_wait);
}
hba->dev_info.clk_gating_wait_us = gating_wait;
}
return err;
}
/** * ufshcd_memory_alloc - allocate memory for host memory space data structures * @hba: per adapter instance * * 1. Allocate DMA memory for Command Descriptor array * Each command descriptor consist of Command UPIU, Response UPIU and PRDT * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL). * 3. Allocate DMA memory for UTP Task Management Request Descriptor List * (UTMRDL) * 4. Allocate memory for local reference block(lrb). * * Return: 0 for success, non-zero in case of failure.
*/ staticint ufshcd_memory_alloc(struct ufs_hba *hba)
{
size_t utmrdl_size, utrdl_size, ucdl_size;
/* * Skip utmrdl allocation; it may have been * allocated during first pass and not released during * MCQ memory allocation. * See ufshcd_release_sdb_queue() and ufshcd_config_mcq()
*/ if (hba->utmrdl_base_addr) goto skip_utmrdl; /* * Allocate memory for UTP Task Management descriptors * UFSHCI requires 1KB alignment of UTMRD
*/
utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
utmrdl_size,
&hba->utmrdl_dma_addr,
GFP_KERNEL); if (!hba->utmrdl_base_addr ||
WARN_ON(hba->utmrdl_dma_addr & (SZ_1K - 1))) {
dev_err(hba->dev, "Task Management Descriptor Memory allocation failed\n"); goto out;
}
for (i = 0; i < hba->nutrs; i++) { /* Configure UTRD with command descriptor base address */
cmd_desc_element_addr =
(cmd_desc_dma_addr + (cmd_desc_size * i));
utrdlp[i].command_desc_base_addr =
cpu_to_le64(cmd_desc_element_addr);
/* Response upiu and prdt offset should be in double words */ if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) {
utrdlp[i].response_upiu_offset =
cpu_to_le16(response_offset);
utrdlp[i].prd_table_offset =
cpu_to_le16(prdt_offset);
utrdlp[i].response_upiu_length =
cpu_to_le16(ALIGNED_UPIU_SIZE);
} else {
utrdlp[i].response_upiu_offset =
cpu_to_le16(response_offset >> 2);
utrdlp[i].prd_table_offset =
cpu_to_le16(prdt_offset >> 2);
utrdlp[i].response_upiu_length =
cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
}
ufshcd_init_lrb(hba, &hba->lrb[i], i);
}
}
/** * ufshcd_dme_link_startup - Notify Unipro to perform link startup * @hba: per adapter instance * * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer, * in order to initialize the Unipro link startup procedure. * Once the Unipro links are up, the device connected to the controller * is detected. * * Return: 0 on success, non-zero value on failure.
*/ staticint ufshcd_dme_link_startup(struct ufs_hba *hba)
{ struct uic_command uic_cmd = {
.command = UIC_CMD_DME_LINK_STARTUP,
}; int ret;
ret = ufshcd_send_uic_cmd(hba, &uic_cmd); if (ret)
dev_dbg(hba->dev, "dme-link-startup: error code %d\n", ret); return ret;
} /** * ufshcd_dme_reset - UIC command for DME_RESET * @hba: per adapter instance * * DME_RESET command is issued in order to reset UniPro stack. * This function now deals with cold reset. * * Return: 0 on success, non-zero value on failure.
*/ int ufshcd_dme_reset(struct ufs_hba *hba)
{ struct uic_command uic_cmd = {
.command = UIC_CMD_DME_RESET,
}; int ret;
ret = ufshcd_send_uic_cmd(hba, &uic_cmd); if (ret)
dev_err(hba->dev, "dme-reset: error code %d\n", ret);
int ufshcd_dme_configure_adapt(struct ufs_hba *hba, int agreed_gear, int adapt_val)
{ int ret;
if (agreed_gear < UFS_HS_G4)
adapt_val = PA_NO_ADAPT;
ret = ufshcd_dme_set(hba,
UIC_ARG_MIB(PA_TXHSADAPTTYPE),
adapt_val); return ret;
}
EXPORT_SYMBOL_GPL(ufshcd_dme_configure_adapt);
/** * ufshcd_dme_enable - UIC command for DME_ENABLE * @hba: per adapter instance * * DME_ENABLE command is issued in order to enable UniPro stack. * * Return: 0 on success, non-zero value on failure.
*/ int ufshcd_dme_enable(struct ufs_hba *hba)
{ struct uic_command uic_cmd = {
.command = UIC_CMD_DME_ENABLE,
}; int ret;
ret = ufshcd_send_uic_cmd(hba, &uic_cmd); if (ret)
dev_err(hba->dev, "dme-enable: error code %d\n", ret);
if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS)) return;
/* * last_dme_cmd_tstamp will be 0 only for 1st call to * this function
*/ if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
} else { unsignedlong delta =
(unsignedlong) ktime_to_us(
ktime_sub(ktime_get(),
hba->last_dme_cmd_tstamp));
if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
min_sleep_time_us =
MIN_DELAY_BEFORE_DME_CMDS_US - delta; else
min_sleep_time_us = 0; /* no more delay required */
}
if (min_sleep_time_us > 0) { /* allow sleep for extra 50us if needed */
usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
}
/* update the last_dme_cmd_tstamp */
hba->last_dme_cmd_tstamp = ktime_get();
}
/** * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET * @hba: per adapter instance * @attr_sel: uic command argument1 * @attr_set: attribute set type as uic command argument2 * @mib_val: setting value as uic command argument3 * @peer: indicate whether peer or local * * Return: 0 on success, non-zero value on failure.
*/ int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
u8 attr_set, u32 mib_val, u8 peer)
{ struct uic_command uic_cmd = {
.command = peer ? UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET,
.argument1 = attr_sel,
.argument2 = UIC_ARG_ATTR_TYPE(attr_set),
.argument3 = mib_val,
}; staticconstchar *const action[] = { "dme-set", "dme-peer-set"
}; constchar *set = action[!!peer]; int ret; int retries = UFS_UIC_COMMAND_RETRIES;
do { /* for peer attributes we retry upon failure */
ret = ufshcd_send_uic_cmd(hba, &uic_cmd); if (ret)
dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
} while (ret && peer && --retries);
if (ret)
dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
set, UIC_GET_ATTR_ID(attr_sel), mib_val,
UFS_UIC_COMMAND_RETRIES - retries);
do { /* for peer attributes we retry upon failure */
ret = ufshcd_send_uic_cmd(hba, &uic_cmd); if (ret)
dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
get, UIC_GET_ATTR_ID(attr_sel), ret);
} while (ret && peer && --retries);
/** * ufshcd_dme_rmw - get modify set a DME attribute * @hba: per adapter instance * @mask: indicates which bits to clear from the value that has been read * @val: actual value to write * @attr: dme attribute
*/ int ufshcd_dme_rmw(struct ufs_hba *hba, u32 mask,
u32 val, u32 attr)
{
u32 cfg = 0; int err;
err = ufshcd_dme_get(hba, UIC_ARG_MIB(attr), &cfg); if (err) return err;
/** * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power * state) and waits for it to take effect. * * @hba: per adapter instance * @cmd: UIC command to execute * * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER & * DME_HIBERNATE_EXIT commands take some time to take its effect on both host * and device UniPro link and hence it's final completion would be indicated by * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in * addition to normal UIC command completion Status (UCCS). This function only * returns after the relevant status bits indicate the completion. * * Return: 0 on success, non-zero value on failure.
*/ staticint ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
{
DECLARE_COMPLETION_ONSTACK(uic_async_done); unsignedlong flags;
u8 status; int ret;
/* * If the h8 exit fails during the runtime resume process, it becomes * stuck and cannot be recovered through the error handler. To fix * this, use link recovery instead of the error handler.
*/ if (ret && hba->pm_op_in_progress)
ret = ufshcd_link_recovery(hba);
return ret;
}
/** * ufshcd_send_bsg_uic_cmd - Send UIC commands requested via BSG layer and retrieve the result * @hba: per adapter instance * @uic_cmd: UIC command * * Return: 0 only if success.
*/ int ufshcd_send_bsg_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
{ int ret;
/* Get the connected lane count */
ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
&pwr_info->lane_rx);
ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
&pwr_info->lane_tx);
/* * First, get the maximum gears of HS speed. * If a zero value, it means there is no HSGEAR capability. * Then, get the maximum gears of PWM speed.
*/
ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx); if (!pwr_info->gear_rx) {
ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
&pwr_info->gear_rx); if (!pwr_info->gear_rx) {
dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
__func__, pwr_info->gear_rx); return -EINVAL;
}
pwr_info->pwr_rx = SLOW_MODE;
}
/** * ufshcd_complete_dev_init() - checks device readiness * @hba: per-adapter instance * * Set fDeviceInit flag and poll until device toggles it. * * Return: 0 upon success; > 0 in case the UFS device reported an OCS error; * < 0 if another error occurred.
*/ staticint ufshcd_complete_dev_init(struct ufs_hba *hba)
{ int err; bool flag_res = true;
ktime_t timeout;
err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
QUERY_FLAG_IDN_FDEVICEINIT, 0, NULL); if (err) {
dev_err(hba->dev, "%s: setting fDeviceInit flag failed with error %d\n",
__func__, err); goto out;
}
/* Poll fDeviceInit flag to be cleared */
timeout = ktime_add_ms(ktime_get(), FDEVICEINIT_COMPL_TIMEOUT); do {
err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_READ_FLAG,
QUERY_FLAG_IDN_FDEVICEINIT, 0, &flag_res); if (!flag_res) break;
usleep_range(500, 1000);
} while (ktime_before(ktime_get(), timeout));
if (err) {
dev_err(hba->dev, "%s: reading fDeviceInit flag failed with error %d\n",
__func__, err);
} elseif (flag_res) {
dev_err(hba->dev, "%s: fDeviceInit was not cleared by the device\n",
__func__);
err = -EBUSY;
}
out: return err;
}
/** * ufshcd_make_hba_operational - Make UFS controller operational * @hba: per adapter instance * * To bring UFS host controller to operational state, * 1. Enable required interrupts * 2. Configure interrupt aggregation * 3. Program UTRL and UTMRL base address * 4. Configure run-stop-registers * * Return: 0 if successful; < 0 upon failure.
*/ int ufshcd_make_hba_operational(struct ufs_hba *hba)
{ int err = 0;
u32 reg;
/* * UCRDY, UTMRLDY and UTRLRDY bits must be 1
*/
reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS); if (!(ufshcd_get_lists_status(reg))) {
ufshcd_enable_run_stop_reg(hba);
} else {
dev_err(hba->dev, "Host controller not ready to process requests");
err = -EIO;
}
/** * ufshcd_hba_execute_hce - initialize the controller * @hba: per adapter instance * * The controller resets itself and controller firmware initialization * sequence kicks off. When controller is ready it will set * the Host Controller Enable bit to 1. * * Return: 0 on success, non-zero value on failure.
*/ staticint ufshcd_hba_execute_hce(struct ufs_hba *hba)
{ int retry;
for (retry = 3; retry > 0; retry--) { if (ufshcd_is_hba_active(hba)) /* change controller state to "reset state" */
ufshcd_hba_stop(hba);
/* UniPro link is disabled at this point */
ufshcd_set_link_off(hba);
/* * To initialize a UFS host controller HCE bit must be set to 1. * During initialization the HCE bit value changes from 1->0->1. * When the host controller completes initialization sequence * it sets the value of HCE bit to 1. The same HCE bit is read back * to check if the controller has completed initialization sequence. * So without this delay the value HCE = 1, set in the previous * instruction might be read back. * This delay can be changed based on the controller.
*/
ufshcd_delay_us(hba->vps->hba_enable_delay_us, 100);
/* wait for the host controller to complete initialization */ if (!ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE, CONTROLLER_ENABLE,
CONTROLLER_ENABLE, 1000, 50)) break;
dev_err(hba->dev, "Enabling the controller failed\n");
}
if (!retry) return -EIO;
/* enable UIC related interrupts */
ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
return 0;
}
int ufshcd_hba_enable(struct ufs_hba *hba)
{ int ret;
if (hba->quirks & UFSHCI_QUIRK_BROKEN_HCE) {
ufshcd_set_link_off(hba);
ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
/* enable UIC related interrupts */
ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
ret = ufshcd_dme_reset(hba); if (ret) {
dev_err(hba->dev, "DME_RESET failed\n"); return ret;
}
ret = ufshcd_dme_enable(hba); if (ret) {
dev_err(hba->dev, "Enabling DME failed\n"); return ret;
}
ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
} else {
ret = ufshcd_hba_execute_hce(hba);
}
/** * ufshcd_link_startup - Initialize unipro link startup * @hba: per adapter instance * * Return: 0 for success, non-zero in case of failure.
*/ staticint ufshcd_link_startup(struct ufs_hba *hba)
{ int ret; int retries = DME_LINKSTARTUP_RETRIES; bool link_startup_again = false;
/* * If UFS device isn't active then we will have to issue link startup * 2 times to make sure the device state move to active.
*/ if (!(hba->quirks & UFSHCD_QUIRK_PERFORM_LINK_STARTUP_ONCE) &&
!ufshcd_is_ufs_dev_active(hba))
link_startup_again = true;
link_startup: do {
ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
ret = ufshcd_dme_link_startup(hba);
/* check if device is detected by inter-connect layer */ if (!ret && !ufshcd_is_device_present(hba)) {
ufshcd_update_evt_hist(hba,
UFS_EVT_LINK_STARTUP_FAIL,
0);
dev_err(hba->dev, "%s: Device not present\n", __func__);
ret = -ENXIO; goto out;
}
/* * DME link lost indication is only received when link is up, * but we can't be sure if the link is up until link startup * succeeds. So reset the local Uni-Pro and try again.
*/ if (ret && retries && ufshcd_hba_enable(hba)) {
ufshcd_update_evt_hist(hba,
UFS_EVT_LINK_STARTUP_FAIL,
(u32)ret); goto out;
}
} while (ret && retries--);
if (ret) { /* failed to get the link up... retire */
ufshcd_update_evt_hist(hba,
UFS_EVT_LINK_STARTUP_FAIL,
(u32)ret); goto out;
}
/* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */
ufshcd_init_pwr_info(hba);
ufshcd_print_pwr_info(hba);
if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
ret = ufshcd_disable_device_tx_lcc(hba); if (ret) goto out;
}
/* Include any host controller configuration via UIC commands */
ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE); if (ret) goto out;
/* Clear UECPA once due to LINERESET has happened during LINK_STARTUP */
ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
ret = ufshcd_make_hba_operational(hba);
out: if (ret) {
dev_err(hba->dev, "link startup failed %d\n", ret);
ufshcd_print_host_state(hba);
ufshcd_print_pwr_info(hba);
ufshcd_print_evt_hist(hba);
} return ret;
}
/** * ufshcd_verify_dev_init() - Verify device initialization * @hba: per-adapter instance * * Send NOP OUT UPIU and wait for NOP IN response to check whether the * device Transport Protocol (UTP) layer is ready after a reset. * If the UTP layer at the device side is not initialized, it may * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations. * * Return: 0 upon success; > 0 in case the UFS device reported an OCS error; * < 0 if another error occurred.
*/ staticint ufshcd_verify_dev_init(struct ufs_hba *hba)
{ int err = 0; int retries;
if (err)
dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err); return err;
}
/** * ufshcd_setup_links - associate link b/w device wlun and other luns * @sdev: pointer to SCSI device * @hba: pointer to ufs hba
*/ staticvoid ufshcd_setup_links(struct ufs_hba *hba, struct scsi_device *sdev)
{ struct device_link *link;
/* * Device wlun is the supplier & rest of the luns are consumers. * This ensures that device wlun suspends after all other luns.
*/ if (hba->ufs_device_wlun) {
link = device_link_add(&sdev->sdev_gendev,
&hba->ufs_device_wlun->sdev_gendev,
DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE); if (!link) {
dev_err(&sdev->sdev_gendev, "Failed establishing link - %s\n",
dev_name(&hba->ufs_device_wlun->sdev_gendev)); return;
}
hba->luns_avail--; /* Ignore REPORT_LUN wlun probing */ if (hba->luns_avail == 1) {
ufshcd_rpm_put(hba); return;
}
} else { /* * Device wlun is probed. The assumption is that WLUNs are * scanned before other LUNs.
*/
hba->luns_avail--;
}
}
/** * ufshcd_lu_init - Initialize the relevant parameters of the LU * @hba: per-adapter instance * @sdev: pointer to SCSI device
*/ staticvoid ufshcd_lu_init(struct ufs_hba *hba, struct scsi_device *sdev)
{ int len = QUERY_DESC_MAX_SIZE;
u8 lun = ufshcd_scsi_to_upiu_lun(sdev->lun);
u8 lun_qdepth = hba->nutrs;
u8 *desc_buf; int ret;
desc_buf = kzalloc(len, GFP_KERNEL); if (!desc_buf) goto set_qdepth;
ret = ufshcd_read_unit_desc_param(hba, lun, 0, desc_buf, len); if (ret < 0) { if (ret == -EOPNOTSUPP) /* If LU doesn't support unit descriptor, its queue depth is set to 1 */
lun_qdepth = 1;
kfree(desc_buf); goto set_qdepth;
}
if (desc_buf[UNIT_DESC_PARAM_LU_Q_DEPTH]) { /* * In per-LU queueing architecture, bLUQueueDepth will not be 0, then we will * use the smaller between UFSHCI CAP.NUTRS and UFS LU bLUQueueDepth
*/
lun_qdepth = min_t(int, desc_buf[UNIT_DESC_PARAM_LU_Q_DEPTH], hba->nutrs);
} /* * According to UFS device specification, the write protection mode is only supported by * normal LU, not supported by WLUN.
*/ if (hba->dev_info.f_power_on_wp_en && lun < hba->dev_info.max_lu_supported &&
!hba->dev_info.is_lu_power_on_wp &&
desc_buf[UNIT_DESC_PARAM_LU_WR_PROTECT] == UFS_LU_POWER_ON_WP)
hba->dev_info.is_lu_power_on_wp = true;
/* In case of RPMB LU, check if advanced RPMB mode is enabled */ if (desc_buf[UNIT_DESC_PARAM_UNIT_INDEX] == UFS_UPIU_RPMB_WLUN &&
desc_buf[RPMB_UNIT_DESC_PARAM_REGION_EN] & BIT(4))
hba->dev_info.b_advanced_rpmb_en = true;
kfree(desc_buf);
set_qdepth: /* * For WLUNs that don't support unit descriptor, queue depth is set to 1. For LUs whose * bLUQueueDepth == 0, the queue depth is set to a maximum value that host can queue.
*/
dev_dbg(hba->dev, "Set LU %x queue depth %d\n", lun, lun_qdepth);
scsi_change_queue_depth(sdev, lun_qdepth);
}
/* * Block runtime-pm until all consumers are added. * Refer ufshcd_setup_links().
*/ if (is_device_wlun(sdev))
pm_runtime_get_noresume(&sdev->sdev_gendev); elseif (ufshcd_is_rpm_autosuspend_allowed(hba))
sdev->rpm_autosuspend = 1; /* * Do not print messages during runtime PM to avoid never-ending cycles * of messages written back to storage by user space causing runtime * resume, causing more messages and so on.
*/
sdev->silence_suspend = 1;
if (hba->vops && hba->vops->config_scsi_dev)
hba->vops->config_scsi_dev(sdev);
/* Drop the reference as it won't be needed anymore */ if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
spin_lock_irqsave(hba->host->host_lock, flags);
hba->ufs_device_wlun = NULL;
spin_unlock_irqrestore(hba->host->host_lock, flags);
} elseif (hba->ufs_device_wlun) { struct device *supplier = NULL;
/* Ensure UFS Device WLUN exists and does not disappear */
spin_lock_irqsave(hba->host->host_lock, flags); if (hba->ufs_device_wlun) {
supplier = &hba->ufs_device_wlun->sdev_gendev;
get_device(supplier);
}
spin_unlock_irqrestore(hba->host->host_lock, flags);
if (supplier) { /* * If a LUN fails to probe (e.g. absent BOOT WLUN), the * device will not have been registered but can still * have a device link holding a reference to the device.
*/
device_link_remove(&sdev->sdev_gendev, supplier);
put_device(supplier);
}
}
}
/** * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status * @lrbp: pointer to local reference block of completed command * @scsi_status: SCSI command status * * Return: value base on SCSI command status.
*/ staticinlineint
ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
{ int result = 0;
switch (scsi_status) { case SAM_STAT_CHECK_CONDITION:
ufshcd_copy_sense_data(lrbp);
fallthrough; case SAM_STAT_GOOD:
result |= DID_OK << 16 | scsi_status; break; case SAM_STAT_TASK_SET_FULL: case SAM_STAT_BUSY: case SAM_STAT_TASK_ABORTED:
ufshcd_copy_sense_data(lrbp);
result |= scsi_status; break; default:
result |= DID_ERROR << 16; break;
} /* end of switch */
return result;
}
/** * ufshcd_transfer_rsp_status - Get overall status of the response * @hba: per adapter instance * @lrbp: pointer to local reference block of completed command * @cqe: pointer to the completion queue entry * * Return: result of the command to notify SCSI midlayer.
*/ staticinlineint
ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp, struct cq_entry *cqe)
{ int result = 0; int scsi_status; enum utp_ocs ocs;
u8 upiu_flags;
u32 resid;
upiu_flags = lrbp->ucd_rsp_ptr->header.flags;
resid = be32_to_cpu(lrbp->ucd_rsp_ptr->sr.residual_transfer_count); /* * Test !overflow instead of underflow to support UFS devices that do * not set either flag.
*/ if (resid && !(upiu_flags & UPIU_RSP_FLAG_OVERFLOW))
scsi_set_resid(lrbp->cmd, resid);
/* overall command status of utrd */
ocs = ufshcd_get_tr_ocs(lrbp, cqe);
if (hba->quirks & UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR) { if (lrbp->ucd_rsp_ptr->header.response ||
lrbp->ucd_rsp_ptr->header.status)
ocs = OCS_SUCCESS;
}
switch (ocs) { case OCS_SUCCESS:
hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); switch (ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr)) { case UPIU_TRANSACTION_RESPONSE: /* * get the result based on SCSI status response * to notify the SCSI midlayer of the command status
*/
scsi_status = lrbp->ucd_rsp_ptr->header.status;
result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
/* * Currently we are only supporting BKOPs exception * events hence we can ignore BKOPs exception event * during power management callbacks. BKOPs exception * event is not expected to be raised in runtime suspend * callback as it allows the urgent bkops. * During system suspend, we are anyway forcefully * disabling the bkops and if urgent bkops is needed * it will be enabled on system resume. Long term * solution could be to abort the system suspend if * UFS device needs urgent BKOPs.
*/ if (!hba->pm_op_in_progress &&
!ufshcd_eh_in_progress(hba) &&
ufshcd_is_exception_event(lrbp->ucd_rsp_ptr)) /* Flushed in suspend */
schedule_work(&hba->eeh_work); break; case UPIU_TRANSACTION_REJECT_UPIU: /* TODO: handle Reject UPIU Response */
result = DID_ERROR << 16;
dev_err(hba->dev, "Reject UPIU not fully implemented\n"); break; default:
dev_err(hba->dev, "Unexpected request response code = %x\n",
result);
result = DID_ERROR << 16; break;
} break; case OCS_ABORTED: case OCS_INVALID_COMMAND_STATUS:
result |= DID_REQUEUE << 16;
dev_warn(hba->dev, "OCS %s from controller for tag %d\n",
(ocs == OCS_ABORTED ? "aborted" : "invalid"),
lrbp->task_tag); break; case OCS_INVALID_CMD_TABLE_ATTR: case OCS_INVALID_PRDT_ATTR: case OCS_MISMATCH_DATA_BUF_SIZE: case OCS_MISMATCH_RESP_UPIU_SIZE: case OCS_PEER_COMM_FAILURE: case OCS_FATAL_ERROR: case OCS_DEVICE_FATAL_ERROR: case OCS_INVALID_CRYPTO_CONFIG: case OCS_GENERAL_CRYPTO_ERROR: default:
result |= DID_ERROR << 16;
dev_err(hba->dev, "OCS error from controller = %x for tag %d\n",
ocs, lrbp->task_tag);
ufshcd_print_evt_hist(hba);
ufshcd_print_host_state(hba); break;
} /* end of switch */
/** * ufshcd_compl_one_cqe - handle a completion queue entry * @hba: per adapter instance * @task_tag: the task tag of the request to be completed * @cqe: pointer to the completion queue entry
*/ void ufshcd_compl_one_cqe(struct ufs_hba *hba, int task_tag, struct cq_entry *cqe)
{ struct ufshcd_lrb *lrbp; struct scsi_cmnd *cmd; enum utp_ocs ocs;
lrbp = &hba->lrb[task_tag];
lrbp->compl_time_stamp = ktime_get();
lrbp->compl_time_stamp_local_clock = local_clock();
cmd = lrbp->cmd; if (cmd) { if (unlikely(ufshcd_should_inform_monitor(hba, lrbp)))
ufshcd_update_monitor(hba, lrbp);
ufshcd_add_command_trace(hba, task_tag, UFS_CMD_COMP);
cmd->result = ufshcd_transfer_rsp_status(hba, lrbp, cqe);
ufshcd_release_scsi_cmd(hba, lrbp); /* Do not touch lrbp after scsi done */
scsi_done(cmd);
} else { if (cqe) {
ocs = le32_to_cpu(cqe->status) & MASK_OCS;
lrbp->utr_descriptor_ptr->header.ocs = ocs;
}
complete(&hba->dev_cmd.complete);
}
}
/** * __ufshcd_transfer_req_compl - handle SCSI and query command completion * @hba: per adapter instance * @completed_reqs: bitmask that indicates which requests to complete
*/ staticvoid __ufshcd_transfer_req_compl(struct ufs_hba *hba, unsignedlong completed_reqs)
{ int tag;
if (!cmd) continue; if (scsi_cmd_to_rq(cmd)->cmd_flags & REQ_POLLED)
__clear_bit(tag, completed_reqs);
}
}
/* * Return: > 0 if one or more commands have been completed or 0 if no * requests have been completed.
*/ staticint ufshcd_poll(struct Scsi_Host *shost, unsignedint queue_num)
{ struct ufs_hba *hba = shost_priv(shost); unsignedlong completed_reqs, flags;
u32 tr_doorbell; struct ufs_hw_queue *hwq;
if (hba->mcq_enabled) {
hwq = &hba->uhq[queue_num];
if (completed_reqs)
__ufshcd_transfer_req_compl(hba, completed_reqs);
return completed_reqs != 0;
}
/** * ufshcd_mcq_compl_pending_transfer - MCQ mode function. It is * invoked from the error handler context or ufshcd_host_reset_and_restore() * to complete the pending transfers and free the resources associated with * the scsi command. * * @hba: per adapter instance * @force_compl: This flag is set to true when invoked * from ufshcd_host_reset_and_restore() in which case it requires special * handling because the host controller has been reset by ufshcd_hba_stop().
*/ staticvoid ufshcd_mcq_compl_pending_transfer(struct ufs_hba *hba, bool force_compl)
{ struct ufs_hw_queue *hwq; struct ufshcd_lrb *lrbp; struct scsi_cmnd *cmd; unsignedlong flags; int tag;
for (tag = 0; tag < hba->nutrs; tag++) {
lrbp = &hba->lrb[tag];
cmd = lrbp->cmd; if (!ufshcd_cmd_inflight(cmd) ||
test_bit(SCMD_STATE_COMPLETE, &cmd->state)) continue;
hwq = ufshcd_mcq_req_to_hwq(hba, scsi_cmd_to_rq(cmd)); if (!hwq) continue;
if (force_compl) {
ufshcd_mcq_compl_all_cqes_lock(hba, hwq); /* * For those cmds of which the cqes are not present * in the cq, complete them explicitly.
*/
spin_lock_irqsave(&hwq->cq_lock, flags); if (cmd && !test_bit(SCMD_STATE_COMPLETE, &cmd->state)) {
set_host_byte(cmd, DID_REQUEUE);
ufshcd_release_scsi_cmd(hba, lrbp);
scsi_done(cmd);
}
spin_unlock_irqrestore(&hwq->cq_lock, flags);
} else {
ufshcd_mcq_poll_cqe_lock(hba, hwq);
}
}
}
/** * ufshcd_transfer_req_compl - handle SCSI and query command completion * @hba: per adapter instance * * Return: * IRQ_HANDLED - If interrupt is valid * IRQ_NONE - If invalid interrupt
*/ static irqreturn_t ufshcd_transfer_req_compl(struct ufs_hba *hba)
{ /* Resetting interrupt aggregation counters first and reading the * DOOR_BELL afterward allows us to handle all the completed requests. * In order to prevent other interrupts starvation the DB is read once * after reset. The down side of this solution is the possibility of * false interrupt if device completes another request after resetting * aggregation and before reading the DB.
*/ if (ufshcd_is_intr_aggr_allowed(hba) &&
!(hba->quirks & UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR))
ufshcd_reset_intr_aggr(hba);
if (ufs_fail_completion(hba)) return IRQ_HANDLED;
/* * Ignore the ufshcd_poll() return value and return IRQ_HANDLED since we * do not want polling to trigger spurious interrupt complaints.
*/
ufshcd_poll(hba->host, UFSHCD_POLL_FROM_INTERRUPT_CONTEXT);
mutex_lock(&hba->ee_ctrl_mutex);
new_mask = (*mask & ~clr) | set;
ee_ctrl_mask = new_mask | *other_mask; if (ee_ctrl_mask != hba->ee_ctrl_mask)
err = __ufshcd_write_ee_control(hba, ee_ctrl_mask); /* Still need to update 'mask' even if 'ee_ctrl_mask' was unchanged */ if (!err) {
hba->ee_ctrl_mask = ee_ctrl_mask;
*mask = new_mask;
}
mutex_unlock(&hba->ee_ctrl_mutex); return err;
}
/** * ufshcd_disable_ee - disable exception event * @hba: per-adapter instance * @mask: exception event to disable * * Disables exception event in the device so that the EVENT_ALERT * bit is not set. * * Return: zero on success, non-zero error value on failure.
*/ staticinlineint ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
{ return ufshcd_update_ee_drv_mask(hba, 0, mask);
}
/** * ufshcd_enable_ee - enable exception event * @hba: per-adapter instance * @mask: exception event to enable * * Enable corresponding exception event in the device to allow * device to alert host in critical scenarios. * * Return: zero on success, non-zero error value on failure.
*/ staticinlineint ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
{ return ufshcd_update_ee_drv_mask(hba, mask, 0);
}
/** * ufshcd_enable_auto_bkops - Allow device managed BKOPS * @hba: per-adapter instance * * Allow device to manage background operations on its own. Enabling * this might lead to inconsistent latencies during normal data transfers * as the device is allowed to manage its own way of handling background * operations. * * Return: 0 upon success; > 0 in case the UFS device reported an OCS error; * < 0 if another error occurred.
*/ staticint ufshcd_enable_auto_bkops(struct ufs_hba *hba)
{ int err = 0;
/* No need of URGENT_BKOPS exception from the device */
err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); if (err)
dev_err(hba->dev, "%s: failed to disable exception event %d\n",
__func__, err);
out: return err;
}
/** * ufshcd_disable_auto_bkops - block device in doing background operations * @hba: per-adapter instance * * Disabling background operations improves command response latency but * has drawback of device moving into critical state where the device is * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the * host is idle so that BKOPS are managed effectively without any negative * impacts. * * Return: 0 upon success; > 0 in case the UFS device reported an OCS error; * < 0 if another error occurred.
*/ staticint ufshcd_disable_auto_bkops(struct ufs_hba *hba)
{ int err = 0;
if (!hba->auto_bkops_enabled) goto out;
/* * If host assisted BKOPs is to be enabled, make sure * urgent bkops exception is allowed.
*/
err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS); if (err) {
dev_err(hba->dev, "%s: failed to enable exception event %d\n",
__func__, err); goto out;
}
/** * ufshcd_force_reset_auto_bkops - force reset auto bkops state * @hba: per adapter instance * * After a device reset the device may toggle the BKOPS_EN flag * to default value. The s/w tracking variables should be updated * as well. This function would change the auto-bkops state based on * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND.
*/ staticvoid ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
{ if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) {
hba->auto_bkops_enabled = false;
hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
ufshcd_enable_auto_bkops(hba);
} else {
hba->auto_bkops_enabled = true;
hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS;
ufshcd_disable_auto_bkops(hba);
}
hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
hba->is_urgent_bkops_lvl_checked = false;
}
/** * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status * @hba: per-adapter instance * * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn * flag in the device to permit background operations if the device * bkops_status is greater than or equal to the "hba->urgent_bkops_lvl", * disable otherwise. * * Return: 0 for success, non-zero in case of failure. * * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag * to know whether auto bkops is enabled or disabled after this function * returns control to it.
*/ staticint ufshcd_bkops_ctrl(struct ufs_hba *hba)
{ enum bkops_status status = hba->urgent_bkops_lvl;
u32 curr_status = 0; int err;
err = ufshcd_get_bkops_status(hba, &curr_status); if (err) {
dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
__func__, err); goto out;
} elseif (curr_status > BKOPS_STATUS_MAX) {
dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
__func__, curr_status);
err = -EINVAL; goto out;
}
if (hba->is_urgent_bkops_lvl_checked) goto enable_auto_bkops;
err = ufshcd_get_bkops_status(hba, &curr_status); if (err) {
dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
__func__, err); goto out;
}
/* * We are seeing that some devices are raising the urgent bkops * exception events even when BKOPS status doesn't indicate performace * impacted or critical. Handle these device by determining their urgent * bkops status at runtime.
*/ if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
__func__, curr_status); /* update the current status as the urgent bkops level */
hba->urgent_bkops_lvl = curr_status;
hba->is_urgent_bkops_lvl_checked = true;
}
enable_auto_bkops:
err = ufshcd_enable_auto_bkops(hba);
out: if (err < 0)
dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
__func__, err);
}
/* * Return: 0 upon success; > 0 in case the UFS device reported an OCS error; * < 0 if another error occurred.
*/ int ufshcd_read_device_lvl_exception_id(struct ufs_hba *hba, u64 *exception_id)
{ struct utp_upiu_query_v4_0 *upiu_resp; struct ufs_query_req *request = NULL; struct ufs_query_res *response = NULL; int err;
if (hba->dev_info.wspecversion < 0x410) return -EOPNOTSUPP;
index = ufshcd_wb_get_query_index(hba);
ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
QUERY_ATTR_IDN_CURR_WB_BUFF_SIZE,
index, 0, &cur_buf); if (ret) {
dev_err(hba->dev, "%s: dCurWriteBoosterBufferSize read failed %d\n",
__func__, ret); returnfalse;
}
if (!cur_buf) {
dev_info(hba->dev, "dCurWBBuf: %d WB disabled until free-space is available\n",
cur_buf); returnfalse;
} /* Let it continue to flush when available buffer exceeds threshold */ return avail_buf < hba->vps->wb_flush_threshold;
}
staticvoid ufshcd_wb_force_disable(struct ufs_hba *hba)
{ if (ufshcd_is_wb_buf_flush_allowed(hba))
ufshcd_wb_toggle_buf_flush(hba, false);
if (!ufshcd_is_wb_buf_lifetime_available(hba)) {
ufshcd_wb_force_disable(hba); returnfalse;
}
/* * With user-space reduction enabled, it's enough to enable flush * by checking only the available buffer. The threshold * defined here is > 90% full. * With user-space preserved enabled, the current-buffer * should be checked too because the wb buffer size can reduce * when disk tends to be full. This info is provided by current * buffer (dCurrentWriteBoosterBufferSize).
*/
index = ufshcd_wb_get_query_index(hba);
ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
QUERY_ATTR_IDN_AVAIL_WB_BUFF_SIZE,
index, 0, &avail_buf); if (ret) {
dev_warn(hba->dev, "%s: dAvailableWriteBoosterBufferSize read failed %d\n",
__func__, ret); returnfalse;
}
if (!hba->dev_info.b_presrv_uspc_en) return avail_buf <= UFS_WB_BUF_REMAIN_PERCENT(10);
staticvoid ufshcd_rpm_dev_flush_recheck_work(struct work_struct *work)
{ struct ufs_hba *hba = container_of(to_delayed_work(work), struct ufs_hba,
rpm_dev_flush_recheck_work); /* * To prevent unnecessary VCC power drain after device finishes * WriteBooster buffer flush or Auto BKOPs, force runtime resume * after a certain delay to recheck the threshold by next runtime * suspend.
*/
ufshcd_rpm_get_sync(hba);
ufshcd_rpm_put_sync(hba);
}
/** * ufshcd_exception_event_handler - handle exceptions raised by device * @work: pointer to work data * * Read bExceptionEventStatus attribute from the device and handle the * exception event accordingly.
*/ staticvoid ufshcd_exception_event_handler(struct work_struct *work)
{ struct ufs_hba *hba; int err;
u32 status = 0;
hba = container_of(work, struct ufs_hba, eeh_work);
err = ufshcd_get_ee_status(hba, &status); if (err) {
dev_err(hba->dev, "%s: failed to get exception status %d\n",
__func__, err); return;
}
trace_ufshcd_exception_event(hba, status);
if (status & hba->ee_drv_mask & MASK_EE_URGENT_BKOPS)
ufshcd_bkops_exception_event_handler(hba);
if (status & hba->ee_drv_mask & MASK_EE_URGENT_TEMP)
ufs_hwmon_notify_event(hba, status & MASK_EE_URGENT_TEMP);
/* Complete requests that have door-bell cleared */ staticvoid ufshcd_complete_requests(struct ufs_hba *hba, bool force_compl)
{ if (hba->mcq_enabled)
ufshcd_mcq_compl_pending_transfer(hba, force_compl); else
ufshcd_transfer_req_compl(hba);
ufshcd_tmc_handler(hba);
}
/** * ufshcd_quirk_dl_nac_errors - This function checks if error handling is * to recover from the DL NAC errors or not. * @hba: per-adapter instance * * Return: true if error handling is required, false otherwise.
*/ staticbool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
{ unsignedlong flags; bool err_handling = true;
spin_lock_irqsave(hba->host->host_lock, flags); /* * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the * device fatal error and/or DL NAC & REPLAY timeout errors.
*/ if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR)) goto out;
if ((hba->saved_err & UIC_ERROR) &&
(hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) { int err; /* * wait for 50ms to see if we can get any other errors or not.
*/
spin_unlock_irqrestore(hba->host->host_lock, flags);
msleep(50);
spin_lock_irqsave(hba->host->host_lock, flags);
/* * now check if we have got any other severe errors other than * DL NAC error?
*/ if ((hba->saved_err & INT_FATAL_ERRORS) ||
((hba->saved_err & UIC_ERROR) &&
(hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR))) goto out;
/* * As DL NAC is the only error received so far, send out NOP * command to confirm if link is still active or not. * - If we don't get any response then do error recovery. * - If we get response then clear the DL NAC error bit.
*/
/* handle fatal errors only when link is not in error state */ if (hba->ufshcd_state != UFSHCD_STATE_ERROR) { if (hba->force_reset || ufshcd_is_link_broken(hba) ||
ufshcd_is_saved_err_fatal(hba))
hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED_FATAL; else
hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED_NON_FATAL;
queue_work(hba->eh_wq, &hba->eh_work);
}
}
/* * Don't assume anything of resume, if * resume fails, irq and clocks can be OFF, and powers * can be OFF or in LPM.
*/
ufshcd_setup_hba_vreg(hba, true);
ufshcd_enable_irq(hba);
ufshcd_setup_vreg(hba, true);
ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
ufshcd_hold(hba); if (!ufshcd_is_clkgating_allowed(hba))
ufshcd_setup_clocks(hba, true);
pm_op = hba->is_sys_suspended ? UFS_SYSTEM_PM : UFS_RUNTIME_PM;
ufshcd_vops_resume(hba, pm_op);
} else {
ufshcd_hold(hba); if (ufshcd_is_clkscaling_supported(hba) &&
hba->clk_scaling.is_enabled)
ufshcd_suspend_clkscaling(hba);
ufshcd_clk_scaling_allow(hba, false);
} /* Wait for ongoing ufshcd_queuecommand() calls to finish. */
blk_mq_quiesce_tagset(&hba->host->tag_set);
cancel_work_sync(&hba->eeh_work);
}
hba->is_sys_suspended = false; /* * Set RPM status of wlun device to RPM_ACTIVE, * this also clears its runtime error.
*/
ret = pm_runtime_set_active(&hba->ufs_device_wlun->sdev_gendev);
/* hba device might have a runtime error otherwise */ if (ret)
ret = pm_runtime_set_active(hba->dev); /* * If wlun device had runtime error, we also need to resume those * consumer scsi devices in case any of them has failed to be * resumed due to supplier runtime resume failure. This is to unblock * blk_queue_enter in case there are bios waiting inside it.
*/ if (!ret) {
shost_for_each_device(sdev, shost) {
q = sdev->request_queue; if (q->dev && (q->rpm_status == RPM_SUSPENDED ||
q->rpm_status == RPM_SUSPENDING))
pm_request_resume(q->dev);
}
}
} #else staticinlinevoid ufshcd_recover_pm_error(struct ufs_hba *hba)
{
} #endif
/** * ufshcd_abort_all - Abort all pending commands. * @hba: Host bus adapter pointer. * * Return: true if and only if the host controller needs to be reset.
*/ staticbool ufshcd_abort_all(struct ufs_hba *hba)
{ int tag, ret = 0;
blk_mq_tagset_busy_iter(&hba->host->tag_set, ufshcd_abort_one, &ret); if (ret) goto out;
/* Clear pending task management requests */
for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
ret = ufshcd_clear_tm_cmd(hba, tag); if (ret) goto out;
}
out: /* Complete the requests that are cleared by s/w */
ufshcd_complete_requests(hba, false);
return ret != 0;
}
/** * ufshcd_err_handler - handle UFS errors that require s/w attention * @work: pointer to work structure
*/ staticvoid ufshcd_err_handler(struct work_struct *work)
{ int retries = MAX_ERR_HANDLER_RETRIES; struct ufs_hba *hba; unsignedlong flags; bool needs_restore; bool needs_reset; int pmc_err;
/* Complete requests that have door-bell cleared by h/w */
ufshcd_complete_requests(hba, false);
spin_lock_irqsave(hba->host->host_lock, flags);
again:
needs_restore = false;
needs_reset = false;
if (hba->ufshcd_state != UFSHCD_STATE_ERROR)
hba->ufshcd_state = UFSHCD_STATE_RESET; /* * A full reset and restore might have happened after preparation * is finished, double check whether we should stop.
*/ if (ufshcd_err_handling_should_stop(hba)) goto skip_err_handling;
if ((hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) &&
!hba->force_reset) { bool ret;
spin_unlock_irqrestore(hba->host->host_lock, flags); /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
ret = ufshcd_quirk_dl_nac_errors(hba);
spin_lock_irqsave(hba->host->host_lock, flags); if (!ret && ufshcd_err_handling_should_stop(hba)) goto skip_err_handling;
}
/* * if host reset is required then skip clearing the pending * transfers forcefully because they will get cleared during * host reset and restore
*/ if (hba->force_reset || ufshcd_is_link_broken(hba) ||
ufshcd_is_saved_err_fatal(hba) ||
((hba->saved_err & UIC_ERROR) &&
(hba->saved_uic_err & (UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))) {
needs_reset = true; goto do_reset;
}
/* * If LINERESET was caught, UFS might have been put to PWM mode, * check if power mode restore is needed.
*/ if (hba->saved_uic_err & UFSHCD_UIC_PA_GENERIC_ERROR) {
hba->saved_uic_err &= ~UFSHCD_UIC_PA_GENERIC_ERROR; if (!hba->saved_uic_err)
hba->saved_err &= ~UIC_ERROR;
spin_unlock_irqrestore(hba->host->host_lock, flags); if (ufshcd_is_pwr_mode_restore_needed(hba))
needs_restore = true;
spin_lock_irqsave(hba->host->host_lock, flags); if (!hba->saved_err && !needs_restore) goto skip_err_handling;
}
spin_lock_irqsave(hba->host->host_lock, flags);
hba->silence_err_logs = false; if (needs_reset) goto do_reset;
/* * After all reqs and tasks are cleared from doorbell, * now it is safe to retore power mode.
*/ if (needs_restore) {
spin_unlock_irqrestore(hba->host->host_lock, flags); /* * Hold the scaling lock just in case dev cmds * are sent via bsg and/or sysfs.
*/
down_write(&hba->clk_scaling_lock);
hba->force_pmc = true;
pmc_err = ufshcd_config_pwr_mode(hba, &(hba->pwr_info)); if (pmc_err) {
needs_reset = true;
dev_err(hba->dev, "%s: Failed to restore power mode, err = %d\n",
__func__, pmc_err);
}
hba->force_pmc = false;
ufshcd_print_pwr_info(hba);
up_write(&hba->clk_scaling_lock);
spin_lock_irqsave(hba->host->host_lock, flags);
}
do_reset: /* Fatal errors need reset */ if (needs_reset) { int err;
hba->force_reset = false;
spin_unlock_irqrestore(hba->host->host_lock, flags);
err = ufshcd_reset_and_restore(hba); if (err)
dev_err(hba->dev, "%s: reset and restore failed with err %d\n",
__func__, err); else
ufshcd_recover_pm_error(hba);
spin_lock_irqsave(hba->host->host_lock, flags);
}
skip_err_handling: if (!needs_reset) { if (hba->ufshcd_state == UFSHCD_STATE_RESET)
hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; if (hba->saved_err || hba->saved_uic_err)
dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
__func__, hba->saved_err, hba->saved_uic_err);
} /* Exit in an operational state or dead */ if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL &&
hba->ufshcd_state != UFSHCD_STATE_ERROR) { if (--retries) goto again;
hba->ufshcd_state = UFSHCD_STATE_ERROR;
}
ufshcd_clear_eh_in_progress(hba);
spin_unlock_irqrestore(hba->host->host_lock, flags);
ufshcd_err_handling_unprepare(hba);
up(&hba->host_sem);
dev_info(hba->dev, "%s finished; HBA state %s\n", __func__,
ufshcd_state_name[hba->ufshcd_state]);
}
/** * ufshcd_update_uic_error - check and set fatal UIC error flags. * @hba: per-adapter instance * * Return: * IRQ_HANDLED - If interrupt is valid * IRQ_NONE - If invalid interrupt
*/ static irqreturn_t ufshcd_update_uic_error(struct ufs_hba *hba)
{
u32 reg;
irqreturn_t retval = IRQ_NONE;
/* PHY layer error */
reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER); if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) &&
(reg & UIC_PHY_ADAPTER_LAYER_ERROR_CODE_MASK)) {
ufshcd_update_evt_hist(hba, UFS_EVT_PA_ERR, reg); /* * To know whether this error is fatal or not, DB timeout * must be checked but this error is handled separately.
*/ if (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK)
dev_dbg(hba->dev, "%s: UIC Lane error reported\n",
__func__);
/* Got a LINERESET indication. */ if (reg & UIC_PHY_ADAPTER_LAYER_GENERIC_ERROR) { struct uic_command *cmd = NULL;
hba->uic_error |= UFSHCD_UIC_PA_GENERIC_ERROR; if (hba->uic_async_done && hba->active_uic_cmd)
cmd = hba->active_uic_cmd; /* * Ignore the LINERESET during power mode change * operation via DME_SET command.
*/ if (cmd && (cmd->command == UIC_CMD_DME_SET))
hba->uic_error &= ~UFSHCD_UIC_PA_GENERIC_ERROR;
}
retval |= IRQ_HANDLED;
}
/* PA_INIT_ERROR is fatal and needs UIC reset */
reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER); if ((reg & UIC_DATA_LINK_LAYER_ERROR) &&
(reg & UIC_DATA_LINK_LAYER_ERROR_CODE_MASK)) {
ufshcd_update_evt_hist(hba, UFS_EVT_DL_ERR, reg);
if (queue_eh_work) { /* * update the transfer error masks to sticky bits, let's do this * irrespective of current ufshcd_state.
*/
hba->saved_err |= hba->errors;
hba->saved_uic_err |= hba->uic_error;
/* dump controller state before resetting */ if ((hba->saved_err &
(INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK)) ||
(hba->saved_uic_err &&
(hba->saved_uic_err != UFSHCD_UIC_PA_GENERIC_ERROR))) {
dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x\n",
__func__, hba->saved_err,
hba->saved_uic_err);
ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: ");
ufshcd_print_pwr_info(hba);
}
ufshcd_schedule_eh_work(hba);
retval |= IRQ_HANDLED;
} /* * if (!queue_eh_work) - * Other errors are either non-fatal where host recovers * itself without s/w intervention or errors that will be * handled by the SCSI core layer.
*/
hba->errors = 0;
hba->uic_error = 0;
return retval;
}
/** * ufshcd_tmc_handler - handle task management function completion * @hba: per adapter instance * * Return: * IRQ_HANDLED - If interrupt is valid * IRQ_NONE - If invalid interrupt
*/ static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba)
{ unsignedlong flags, pending, issued;
irqreturn_t ret = IRQ_NONE; int tag;
/* * There could be max of hba->nutrs reqs in flight and in worst case * if the reqs get finished 1 by 1 after the interrupt status is * read, make sure we handle them by checking the interrupt status * again in a loop until we process all of the reqs before returning.
*/ while (intr_status && retries--) {
enabled_intr_status =
intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS); if (enabled_intr_status)
retval |= ufshcd_sl_intr(hba, enabled_intr_status);
/* * blk_mq_alloc_request() is used here only to get a free tag.
*/
req = blk_mq_alloc_request(q, REQ_OP_DRV_OUT, 0); if (IS_ERR(req)) return PTR_ERR(req);
/** * ufshcd_issue_tm_cmd - issues task management commands to controller * @hba: per adapter instance * @lun_id: LUN ID to which TM command is sent * @task_id: task ID to which the TM command is applicable * @tm_function: task management function opcode * @tm_response: task management service response return value * * Return: non-zero value on error, zero on success.
*/ staticint ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
u8 tm_function, u8 *tm_response)
{ struct utp_task_req_desc treq = { }; enum utp_ocs ocs_value; int err;
/* * The host shall provide the same value for LUN field in the basic * header and for Input Parameter.
*/
treq.upiu_req.input_param1 = cpu_to_be32(lun_id);
treq.upiu_req.input_param2 = cpu_to_be32(task_id);
/** * ufshcd_issue_devman_upiu_cmd - API for sending "utrd" type requests * @hba: per-adapter instance * @req_upiu: upiu request * @rsp_upiu: upiu reply * @desc_buff: pointer to descriptor buffer, NULL if NA * @buff_len: descriptor size, 0 if NA * @cmd_type: specifies the type (NOP, Query...) * @desc_op: descriptor operation * * Those type of requests uses UTP Transfer Request Descriptor - utrd. * Therefore, it "rides" the device management infrastructure: uses its tag and * tasks work queues. * * Since there is only one available tag for device management commands, * the caller is expected to hold the hba->dev_cmd.lock mutex. * * Return: 0 upon success; < 0 upon failure.
*/ staticint ufshcd_issue_devman_upiu_cmd(struct ufs_hba *hba, struct utp_upiu_req *req_upiu, struct utp_upiu_req *rsp_upiu,
u8 *desc_buff, int *buff_len, enum dev_cmd_type cmd_type, enum query_opcode desc_op)
{ const u32 tag = hba->reserved_slot; struct ufshcd_lrb *lrbp = &hba->lrb[tag]; int err = 0;
u8 upiu_flags;
/* Protects use of hba->reserved_slot. */
lockdep_assert_held(&hba->dev_cmd.lock);
/* update the task tag in the request upiu */
req_upiu->header.task_tag = tag;
/* just copy the upiu request as it is */
memcpy(lrbp->ucd_req_ptr, req_upiu, sizeof(*lrbp->ucd_req_ptr)); if (desc_buff && desc_op == UPIU_QUERY_OPCODE_WRITE_DESC) { /* The Data Segment Area is optional depending upon the query * function value. for WRITE DESCRIPTOR, the data segment * follows right after the tsf.
*/
memcpy(lrbp->ucd_req_ptr + 1, desc_buff, *buff_len);
*buff_len = 0;
}
/* * ignore the returning value here - ufshcd_check_query_response is * bound to fail since dev_cmd.query and dev_cmd.type were left empty. * read the response directly ignoring all errors.
*/
ufshcd_issue_dev_cmd(hba, lrbp, tag, dev_cmd_timeout);
/* just copy the upiu response as it is */
memcpy(rsp_upiu, lrbp->ucd_rsp_ptr, sizeof(*rsp_upiu)); if (desc_buff && desc_op == UPIU_QUERY_OPCODE_READ_DESC) {
u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + sizeof(*rsp_upiu);
u16 resp_len = be16_to_cpu(lrbp->ucd_rsp_ptr->header
.data_segment_length);
/** * ufshcd_exec_raw_upiu_cmd - API function for sending raw upiu commands * @hba: per-adapter instance * @req_upiu: upiu request * @rsp_upiu: upiu reply - only 8 DW as we do not support scsi commands * @msgcode: message code, one of UPIU Transaction Codes Initiator to Target * @desc_buff: pointer to descriptor buffer, NULL if NA * @buff_len: descriptor size, 0 if NA * @desc_op: descriptor operation * * Supports UTP Transfer requests (nop and query), and UTP Task * Management requests. * It is up to the caller to fill the upiu conent properly, as it will * be copied without any further input validations. * * Return: 0 upon success; < 0 upon failure.
*/ int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba, struct utp_upiu_req *req_upiu, struct utp_upiu_req *rsp_upiu, enum upiu_request_transaction msgcode,
u8 *desc_buff, int *buff_len, enum query_opcode desc_op)
{ int err; enum dev_cmd_type cmd_type = DEV_CMD_TYPE_QUERY; struct utp_task_req_desc treq = { }; enum utp_ocs ocs_value;
u8 tm_f = req_upiu->header.tm_function;
/* update the task tag */
req_upiu->header.task_tag = tag;
/* copy the UPIU(contains CDB) request as it is */
memcpy(lrbp->ucd_req_ptr, req_upiu, sizeof(*lrbp->ucd_req_ptr)); /* Copy EHS, starting with byte32, immediately after the CDB package */
memcpy(lrbp->ucd_req_ptr + 1, req_ehs, sizeof(*req_ehs));
if (dir != DMA_NONE && sg_list)
ufshcd_sgl_to_prdt(hba, lrbp, sg_cnt, sg_list);
if (!err) { /* Just copy the upiu response as it is */
memcpy(rsp_upiu, lrbp->ucd_rsp_ptr, sizeof(*rsp_upiu)); /* Get the response UPIU result */
result = (lrbp->ucd_rsp_ptr->header.response << 8) |
lrbp->ucd_rsp_ptr->header.status;
ehs_len = lrbp->ucd_rsp_ptr->header.ehs_length; /* * Since the bLength in EHS indicates the total size of the EHS Header and EHS Data * in 32 Byte units, the value of the bLength Request/Response for Advanced RPMB * Message is 02h
*/ if (ehs_len == 2 && rsp_ehs) { /* * ucd_rsp_ptr points to a buffer with a length of 512 bytes * (ALIGNED_UPIU_SIZE = 512), and the EHS data just starts from byte32
*/
ehs_data = (u8 *)lrbp->ucd_rsp_ptr + EHS_OFFSET_IN_RESPONSE;
memcpy(rsp_ehs, ehs_data, ehs_len * 32);
}
}
/** * ufshcd_try_to_abort_task - abort a specific task * @hba: Pointer to adapter instance * @tag: Task tag/index to be aborted * * Abort the pending command in device by sending UFS_ABORT_TASK task management * command, and in host controller by clearing the door-bell register. There can * be race between controller sending the command to the device while abort is * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is * really issued and then try to abort it. * * Return: zero on success, non-zero on failure.
*/ int ufshcd_try_to_abort_task(struct ufs_hba *hba, int tag)
{ struct ufshcd_lrb *lrbp = &hba->lrb[tag]; int err; int poll_cnt;
u8 resp = 0xF;
for (poll_cnt = 100; poll_cnt; poll_cnt--) {
err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
UFS_QUERY_TASK, &resp); if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) { /* cmd pending in the device */
dev_err(hba->dev, "%s: cmd pending in the device. tag = %d\n",
__func__, tag); break;
} elseif (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) { /* * cmd not pending in the device, check if it is * in transition.
*/
dev_info(
hba->dev, "%s: cmd with tag %d not pending in the device.\n",
__func__, tag); if (!ufshcd_cmd_inflight(lrbp->cmd)) {
dev_info(hba->dev, "%s: cmd with tag=%d completed.\n",
__func__, tag); return 0;
}
usleep_range(100, 200);
} else {
dev_err(hba->dev, "%s: no response from device. tag = %d, err %d\n",
__func__, tag, err); return err ? : resp;
}
}
if (!poll_cnt) return -EBUSY;
err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
UFS_ABORT_TASK, &resp); if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) { if (!err) {
err = resp; /* service response error */
dev_err(hba->dev, "%s: issued. tag = %d, err %d\n",
__func__, tag, err);
} return err;
}
err = ufshcd_clear_cmd(hba, tag); if (err)
dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d\n",
__func__, tag, err);
if (!hba->mcq_enabled) {
reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); if (!test_bit(tag, &hba->outstanding_reqs)) { /* If command is already aborted/completed, return FAILED. */
dev_err(hba->dev, "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
__func__, tag, hba->outstanding_reqs, reg); goto release;
}
}
/* Print Transfer Request of aborted task */
dev_info(hba->dev, "%s: Device abort task at tag %d\n", __func__, tag);
/* * Print detailed info about aborted request. * As more than one request might get aborted at the same time, * print full information only for the first aborted request in order * to reduce repeated printouts. For other aborted requests only print * basic details.
*/
scsi_print_command(cmd); if (!hba->req_abort_count) {
ufshcd_update_evt_hist(hba, UFS_EVT_ABORT, tag);
ufshcd_print_evt_hist(hba);
ufshcd_print_host_state(hba);
ufshcd_print_pwr_info(hba);
ufshcd_print_tr(hba, tag, true);
} else {
ufshcd_print_tr(hba, tag, false);
}
hba->req_abort_count++;
if (!hba->mcq_enabled && !(reg & (1 << tag))) { /* only execute this code in single doorbell mode */
dev_err(hba->dev, "%s: cmd was completed, but without a notifying intr, tag = %d",
__func__, tag);
__ufshcd_transfer_req_compl(hba, 1UL << tag); goto release;
}
/* * Task abort to the device W-LUN is illegal. When this command * will fail, due to spec violation, scsi err handling next step * will be to send LU reset which, again, is a spec violation. * To avoid these unnecessary/illegal steps, first we clean up * the lrb taken by this cmd and re-set it in outstanding_reqs, * then queue the eh_work and bail.
*/ if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN) {
ufshcd_update_evt_hist(hba, UFS_EVT_ABORT, lrbp->lun);
/* * Clear the corresponding bit from outstanding_reqs since the command * has been aborted successfully.
*/
spin_lock_irqsave(&hba->outstanding_lock, flags);
outstanding = __test_and_clear_bit(tag, &hba->outstanding_reqs);
spin_unlock_irqrestore(&hba->outstanding_lock, flags);
if (outstanding)
ufshcd_release_scsi_cmd(hba, lrbp);
err = SUCCESS;
release: /* Matches the ufshcd_hold() call at the start of this function. */
ufshcd_release(hba); return err;
}
/** * ufshcd_process_probe_result - Process the ufshcd_probe_hba() result. * @hba: UFS host controller instance. * @probe_start: time when the ufshcd_probe_hba() call started. * @ret: ufshcd_probe_hba() return value.
*/ staticvoid ufshcd_process_probe_result(struct ufs_hba *hba,
ktime_t probe_start, int ret)
{ unsignedlong flags;
/** * ufshcd_host_reset_and_restore - reset and restore host controller * @hba: per-adapter instance * * Note that host controller reset may issue DME_RESET to * local and remote (device) Uni-Pro stack and the attributes * are reset to default state. * * Return: zero on success, non-zero on failure.
*/ staticint ufshcd_host_reset_and_restore(struct ufs_hba *hba)
{ int err;
/* * Stop the host controller and complete the requests * cleared by h/w
*/
ufshcd_hba_stop(hba);
hba->silence_err_logs = true;
ufshcd_complete_requests(hba, true);
hba->silence_err_logs = false;
/* scale up clocks to max frequency before full reinitialization */ if (ufshcd_is_clkscaling_supported(hba))
ufshcd_scale_clks(hba, ULONG_MAX, true);
err = ufshcd_hba_enable(hba);
/* Establish the link again and restore the device */ if (!err) {
ktime_t probe_start = ktime_get();
/** * ufshcd_reset_and_restore - reset and re-initialize host/device * @hba: per-adapter instance * * Reset and recover device, host and re-establish link. This * is helpful to recover the communication in fatal error conditions. * * Return: zero on success, non-zero on failure.
*/ staticint ufshcd_reset_and_restore(struct ufs_hba *hba)
{
u32 saved_err = 0;
u32 saved_uic_err = 0; int err = 0; unsignedlong flags; int retries = MAX_HOST_RESET_RETRIES;
spin_lock_irqsave(hba->host->host_lock, flags); do { /* * This is a fresh start, cache and clear saved error first, * in case new error generated during reset and restore.
*/
saved_err |= hba->saved_err;
saved_uic_err |= hba->saved_uic_err;
hba->saved_err = 0;
hba->saved_uic_err = 0;
hba->force_reset = false;
hba->ufshcd_state = UFSHCD_STATE_RESET;
spin_unlock_irqrestore(hba->host->host_lock, flags);
/* Reset the attached device */
ufshcd_device_reset(hba);
err = ufshcd_host_reset_and_restore(hba);
spin_lock_irqsave(hba->host->host_lock, flags); if (err) continue; /* Do not exit unless operational or dead */ if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL &&
hba->ufshcd_state != UFSHCD_STATE_ERROR &&
hba->ufshcd_state != UFSHCD_STATE_EH_SCHEDULED_NON_FATAL)
err = -EAGAIN;
} while (err && --retries);
/* * Inform scsi mid-layer that we did reset and allow to handle * Unit Attention properly.
*/
scsi_report_bus_reset(hba->host, 0); if (err) {
hba->ufshcd_state = UFSHCD_STATE_ERROR;
hba->saved_err |= saved_err;
hba->saved_uic_err |= saved_uic_err;
}
spin_unlock_irqrestore(hba->host->host_lock, flags);
/* * If runtime PM sent SSU and got a timeout, scsi_error_handler is * stuck in this function waiting for flush_work(&hba->eh_work). And * ufshcd_err_handler(eh_work) is stuck waiting for runtime PM. Do * ufshcd_link_recovery instead of eh_work to prevent deadlock.
*/ if (hba->pm_op_in_progress) { if (ufshcd_link_recovery(hba))
err = FAILED;
/** * ufshcd_get_max_icc_level - calculate the ICC level * @sup_curr_uA: max. current supported by the regulator * @start_scan: row at the desc table to start scan from * @buff: power descriptor buffer * * Return: calculated max ICC level for specific regulator.
*/ static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, constchar *buff)
{ int i; int curr_uA;
u16 data;
u16 unit;
for (i = start_scan; i >= 0; i--) {
data = get_unaligned_be16(&buff[2 * i]);
unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
ATTR_ICC_LVL_UNIT_OFFSET;
curr_uA = data & ATTR_ICC_LVL_VALUE_MASK; switch (unit) { case UFSHCD_NANO_AMP:
curr_uA = curr_uA / 1000; break; case UFSHCD_MILI_AMP:
curr_uA = curr_uA * 1000; break; case UFSHCD_AMP:
curr_uA = curr_uA * 1000 * 1000; break; case UFSHCD_MICRO_AMP: default: break;
} if (sup_curr_uA >= curr_uA) break;
} if (i < 0) {
i = 0;
pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
}
return (u32)i;
}
/** * ufshcd_find_max_sup_active_icc_level - calculate the max ICC level * In case regulators are not initialized we'll return 0 * @hba: per-adapter instance * @desc_buf: power descriptor buffer to extract ICC levels from. * * Return: calculated ICC level.
*/ static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba, const u8 *desc_buf)
{
u32 icc_level = 0;
if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
!hba->vreg_info.vccq2) { /* * Using dev_dbg to avoid messages during runtime PM to avoid * never-ending cycles of messages written back to storage by * user space causing runtime resume, causing more messages and * so on.
*/
dev_dbg(hba->dev, "%s: Regulator capability was not set, actvIccLevel=%d",
__func__, icc_level); goto out;
}
if (hba->vreg_info.vcc->max_uA)
icc_level = ufshcd_get_max_icc_level(
hba->vreg_info.vcc->max_uA,
POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
&desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
if (hba->vreg_info.vccq->max_uA)
icc_level = ufshcd_get_max_icc_level(
hba->vreg_info.vccq->max_uA,
icc_level,
&desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
scsi_autopm_get_device(sdev);
blk_pm_runtime_init(sdev->request_queue, &sdev->sdev_gendev); if (sdev->rpm_autosuspend)
pm_runtime_set_autosuspend_delay(&sdev->sdev_gendev,
shost->rpm_autosuspend_delay);
scsi_autopm_put_device(sdev);
}
/** * ufshcd_scsi_add_wlus - Adds required W-LUs * @hba: per-adapter instance * * UFS device specification requires the UFS devices to support 4 well known * logical units: * "REPORT_LUNS" (address: 01h) * "UFS Device" (address: 50h) * "RPMB" (address: 44h) * "BOOT" (address: 30h) * UFS device's power management needs to be controlled by "POWER CONDITION" * field of SSU (START STOP UNIT) command. But this "power condition" field * will take effect only when its sent to "UFS device" well known logical unit * hence we require the scsi_device instance to represent this logical unit in * order for the UFS host driver to send the SSU command for power management. * * We also require the scsi_device instance for "RPMB" (Replay Protected Memory * Block) LU so user space process can control this LU. User space may also * want to have access to BOOT LU. * * This function adds scsi device instances for each of all well known LUs * (except "REPORT LUNS" LU). * * Return: zero on success (all required W-LUs are added successfully), * non-zero error value on failure (if failed to add any of the required W-LU).
*/ staticint ufshcd_scsi_add_wlus(struct ufs_hba *hba)
{ int ret = 0; struct scsi_device *sdev_boot, *sdev_rpmb;
hba->ufs_device_wlun = __scsi_add_device(hba->host, 0, 0,
ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL); if (IS_ERR(hba->ufs_device_wlun)) {
ret = PTR_ERR(hba->ufs_device_wlun);
hba->ufs_device_wlun = NULL; goto out;
}
scsi_device_put(hba->ufs_device_wlun);
sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL); if (IS_ERR(sdev_rpmb)) {
ret = PTR_ERR(sdev_rpmb); goto remove_ufs_device_wlun;
}
ufshcd_blk_pm_runtime_init(sdev_rpmb);
scsi_device_put(sdev_rpmb);
if (!(ext_ufs_feature & UFS_DEV_WRITE_BOOSTER_SUP)) goto wb_disabled;
/* * WB may be supported but not configured while provisioning. The spec * says, in dedicated wb buffer mode, a max of 1 lun would have wb * buffer configured.
*/
dev_info->wb_buffer_type = desc_buf[DEVICE_DESC_PARAM_WB_TYPE];
if (ts64.tv_sec < hba->dev_info.rtc_time_baseline) {
dev_warn_once(hba->dev, "%s: Current time precedes previous setting!\n", __func__); return;
}
/* * The Absolute RTC mode has a 136-year limit, spanning from 2010 to 2146. If a time beyond * 2146 is required, it is recommended to choose the relative RTC mode.
*/
val = ts64.tv_sec - hba->dev_info.rtc_time_baseline;
/* Skip update RTC if RPM state is not RPM_ACTIVE */ if (ufshcd_rpm_get_if_active(hba) <= 0) return;
/* Update RTC only when there are no requests in progress and UFSHCI is operational */ if (!ufshcd_is_ufs_dev_busy(hba) &&
hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL &&
!hba->clk_gating.active_reqs)
ufshcd_update_rtc(hba);
if (ufshcd_is_ufs_dev_active(hba) && hba->dev_info.rtc_update_period)
schedule_delayed_work(&hba->ufs_rtc_update_work,
msecs_to_jiffies(hba->dev_info.rtc_update_period));
}
if (periodic_rtc_update & UFS_RTC_TIME_BASELINE) {
dev_info->rtc_type = UFS_RTC_ABSOLUTE;
/* * The concept of measuring time in Linux as the number of seconds elapsed since * 00:00:00 UTC on January 1, 1970, and UFS ABS RTC is elapsed from January 1st * 2010 00:00, here we need to adjust ABS baseline.
*/
dev_info->rtc_time_baseline = mktime64(2010, 1, 1, 0, 0, 0) -
mktime64(1970, 1, 1, 0, 0, 0);
} else {
dev_info->rtc_type = UFS_RTC_RELATIVE;
dev_info->rtc_time_baseline = 0;
}
/* * We ignore TIME_PERIOD defined in wPeriodicRTCUpdate because Spec does not clearly state * how to calculate the specific update period for each time unit. And we disable periodic * RTC update work, let user configure by sysfs node according to specific circumstance.
*/
dev_info->rtc_update_period = 0;
}
/* * getting vendor (manufacturerID) and Bank Index in big endian * format
*/
dev_info->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
/* getting Specification Version in big endian format */
dev_info->wspecversion = desc_buf[DEVICE_DESC_PARAM_SPEC_VER] << 8 |
desc_buf[DEVICE_DESC_PARAM_SPEC_VER + 1];
dev_info->bqueuedepth = desc_buf[DEVICE_DESC_PARAM_Q_DPTH];
/** * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is * less than device PA_TACTIVATE time. * @hba: per-adapter instance * * Some UFS devices require host PA_TACTIVATE to be lower than device * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk * for such devices. * * Return: zero on success, non-zero error value on failure.
*/ staticint ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
{ int ret = 0;
u32 granularity, peer_granularity;
u32 pa_tactivate, peer_pa_tactivate;
u32 pa_tactivate_us, peer_pa_tactivate_us; staticconst u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
&granularity); if (ret) goto out;
ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
&peer_granularity); if (ret) goto out;
/** * ufshcd_quirk_override_pa_h8time - Ensures proper adjustment of PA_HIBERN8TIME. * @hba: per-adapter instance * * Some UFS devices require specific adjustments to the PA_HIBERN8TIME parameter * to ensure proper hibernation timing. This function retrieves the current * PA_HIBERN8TIME value and increments it by 100us.
*/ staticvoid ufshcd_quirk_override_pa_h8time(struct ufs_hba *hba)
{
u32 pa_h8time; int ret;
ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_HIBERN8TIME), &pa_h8time); if (ret) {
dev_err(hba->dev, "Failed to get PA_HIBERN8TIME: %d\n", ret); return;
}
/* Increment by 1 to increase hibernation time by 100 µs */
ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME), pa_h8time + 1); if (ret)
dev_err(hba->dev, "Failed updating PA_HIBERN8TIME: %d\n", ret);
}
if (err) {
dev_err(hba->dev, "bRefClkFreq setting to %lu Hz failed\n",
ufs_ref_clk_freqs[freq].freq_hz); goto out;
}
dev_dbg(hba->dev, "bRefClkFreq setting to %lu Hz succeeded\n",
ufs_ref_clk_freqs[freq].freq_hz);
out: return err;
}
staticint ufshcd_device_params_init(struct ufs_hba *hba)
{ bool flag; int ret;
/* Init UFS geometry descriptor related parameters */
ret = ufshcd_device_geo_params_init(hba); if (ret) goto out;
/* Check and apply UFS device quirks */
ret = ufs_get_device_desc(hba); if (ret) {
dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
__func__, ret); goto out;
}
ufshcd_set_rtt(hba);
ufshcd_get_ref_clk_gating_wait(hba);
if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
QUERY_FLAG_IDN_PWR_ON_WPE, 0, &flag))
hba->dev_info.f_power_on_wp_en = flag;
/* Probe maximum power mode co-supported by both UFS host and device */ if (ufshcd_get_max_pwr_mode(hba))
dev_err(hba->dev, "%s: Failed getting max supported power mode\n",
__func__);
out: return ret;
}
/* * The RTC update code accesses the hba->ufs_device_wlun->sdev_gendev * pointer and hence must only be started after the WLUN pointer has * been initialized by ufshcd_scsi_add_wlus().
*/
schedule_delayed_work(&hba->ufs_rtc_update_work,
msecs_to_jiffies(UFS_RTC_UPDATE_INTERVAL_MS));
ufs_bsg_probe(hba);
scsi_scan_host(hba->host);
out: return ret;
}
/* SDB - Single Doorbell */ staticvoid ufshcd_release_sdb_queue(struct ufs_hba *hba, int nutrs)
{
size_t ucdl_size, utrdl_size;
staticint ufshcd_alloc_mcq(struct ufs_hba *hba)
{ int ret; int old_nutrs = hba->nutrs;
ret = ufshcd_mcq_decide_queue_depth(hba); if (ret < 0) return ret;
hba->nutrs = ret;
ret = ufshcd_mcq_init(hba); if (ret) goto err;
/* * Previously allocated memory for nutrs may not be enough in MCQ mode. * Number of supported tags in MCQ mode may be larger than SDB mode.
*/ if (hba->nutrs != old_nutrs) {
ufshcd_release_sdb_queue(hba, old_nutrs);
ret = ufshcd_memory_alloc(hba); if (ret) goto err;
ufshcd_host_memory_configure(hba);
}
ret = ufshcd_mcq_memory_alloc(hba); if (ret) goto err;
staticint ufshcd_post_device_init(struct ufs_hba *hba)
{ int ret;
ufshcd_tune_unipro_params(hba);
/* UFS device is also active now */
ufshcd_set_ufs_dev_active(hba);
ufshcd_force_reset_auto_bkops(hba);
ufshcd_set_timestamp_attr(hba);
if (!hba->max_pwr_info.is_valid) return 0;
/* * Set the right value to bRefClkFreq before attempting to * switch to HS gears.
*/ if (hba->dev_ref_clk_freq != REF_CLK_FREQ_INVAL)
ufshcd_set_dev_ref_clk(hba); /* Gear up to HS gear. */
ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info); if (ret) {
dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
__func__, ret); return ret;
}
return 0;
}
staticint ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params)
{ int ret;
WARN_ON_ONCE(!hba->scsi_host_added);
hba->ufshcd_state = UFSHCD_STATE_RESET;
ret = ufshcd_link_startup(hba); if (ret) return ret;
if (hba->quirks & UFSHCD_QUIRK_SKIP_PH_CONFIGURATION) return ret;
/* UniPro link is active now */
ufshcd_set_link_active(hba);
/* Reconfigure MCQ upon reset */ if (hba->mcq_enabled && !init_dev_params) {
ufshcd_config_mcq(hba);
ufshcd_mcq_enable(hba);
}
/* Verify device initialization by sending NOP OUT UPIU */
ret = ufshcd_verify_dev_init(hba); if (ret) return ret;
/* Initiate UFS initialization, and waiting until completion */
ret = ufshcd_complete_dev_init(hba); if (ret) return ret;
/* * Initialize UFS device parameters used by driver, these * parameters are associated with UFS descriptors.
*/ if (init_dev_params) {
ret = ufshcd_device_params_init(hba); if (ret) return ret; if (is_mcq_supported(hba) &&
hba->quirks & UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH) {
ufshcd_config_mcq(hba);
ufshcd_mcq_enable(hba);
}
}
return ufshcd_post_device_init(hba);
}
/** * ufshcd_probe_hba - probe hba to detect device and initialize it * @hba: per-adapter instance * @init_dev_params: whether or not to call ufshcd_device_params_init(). * * Execute link-startup and verify device initialization * * Return: 0 upon success; < 0 upon failure.
*/ staticint ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params)
{ int ret;
if (!hba->pm_op_in_progress &&
(hba->quirks & UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH)) { /* Reset the device and controller before doing reinit */
ufshcd_device_reset(hba);
ufs_put_device_desc(hba);
ufshcd_hba_stop(hba);
ret = ufshcd_hba_enable(hba); if (ret) {
dev_err(hba->dev, "Host controller enable failed\n");
ufshcd_print_evt_hist(hba);
ufshcd_print_host_state(hba); return ret;
}
/* Reinit the device */
ret = ufshcd_device_init(hba, init_dev_params); if (ret) return ret;
}
ufshcd_print_pwr_info(hba);
/* * bActiveICCLevel is volatile for UFS device (as per latest v2.1 spec) * and for removable UFS card as well, hence always set the parameter. * Note: Error handler may issue the device reset hence resetting * bActiveICCLevel as well so it is always safe to set this here.
*/
ufshcd_set_active_icc_lvl(hba);
/* Enable UFS Write Booster if supported */
ufshcd_configure_wb(hba);
if (hba->ee_usr_mask)
ufshcd_write_ee_control(hba);
ufshcd_configure_auto_hibern8(hba);
return 0;
}
/** * ufshcd_async_scan - asynchronous execution for probing hba * @data: data pointer to pass to this function * @cookie: cookie data
*/ staticvoid ufshcd_async_scan(void *data, async_cookie_t cookie)
{ struct ufs_hba *hba = (struct ufs_hba *)data;
ktime_t probe_start; int ret;
down(&hba->host_sem); /* Initialize hba, detect and initialize UFS device */
probe_start = ktime_get();
ret = ufshcd_probe_hba(hba, true);
ufshcd_process_probe_result(hba, probe_start, ret);
up(&hba->host_sem); if (ret) goto out;
/* Probe and add UFS logical units */
ret = ufshcd_add_lus(hba);
out:
pm_runtime_put_sync(hba->dev);
if (ret)
dev_err(hba->dev, "%s failed: %d\n", __func__, ret);
}
if (!hba->system_suspending) { /* Activate the error handler in the SCSI core. */ return SCSI_EH_NOT_HANDLED;
}
/* * If we get here we know that no TMFs are outstanding and also that * the only pending command is a START STOP UNIT command. Handle the * timeout of that command directly to prevent a deadlock between * ufshcd_set_dev_pwr_mode() and ufshcd_err_handler().
*/
ufshcd_link_recovery(hba);
dev_info(hba->dev, "%s() finished; outstanding_tasks = %#lx.\n",
__func__, hba->outstanding_tasks);
staticint ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg, int ua)
{ int ret;
if (!vreg) return 0;
/* * "set_load" operation shall be required on those regulators * which specifically configured current limitation. Otherwise * zero max_uA may cause unexpected behavior when regulator is * enabled or set as high power mode.
*/ if (!vreg->max_uA) return 0;
ret = regulator_set_load(vreg->reg, ua); if (ret < 0) {
dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
__func__, vreg->name, ua, ret);
}
ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE); if (ret) return ret;
list_for_each_entry(clki, head, list) { if (!IS_ERR_OR_NULL(clki->clk)) { /* * Don't disable clocks which are needed * to keep the link active.
*/ if (ufshcd_is_link_active(hba) &&
clki->keep_link_active) continue;
clk_state_changed = on ^ clki->enabled; if (on && !clki->enabled) {
ret = clk_prepare_enable(clki->clk); if (ret) {
dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
__func__, clki->name, ret); goto out;
}
} elseif (!on && clki->enabled) {
clk_disable_unprepare(clki->clk);
}
clki->enabled = on;
dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
clki->name, on ? "en" : "dis");
}
}
ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE); if (ret) return ret;
if (!ufshcd_is_clkscaling_supported(hba))
ufshcd_pm_qos_update(hba, on);
out: if (ret) {
list_for_each_entry(clki, head, list) { if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
clk_disable_unprepare(clki->clk);
}
} elseif (!ret && on && hba->clk_gating.is_initialized) {
scoped_guard(spinlock_irqsave, &hba->clk_gating.lock)
hba->clk_gating.state = CLKS_ON;
trace_ufshcd_clk_gating(hba,
hba->clk_gating.state);
}
if (clk_state_changed)
trace_ufshcd_profile_clk_gating(hba,
(on ? "on" : "off"),
ktime_to_us(ktime_sub(ktime_get(), start)), ret); return ret;
}
staticenum ufs_ref_clk_freq ufshcd_parse_ref_clk_property(struct ufs_hba *hba)
{
u32 freq; int ret = device_property_read_u32(hba->dev, "ref-clk-freq", &freq);
list_for_each_entry(clki, head, list) { if (!clki->name) continue;
clki->clk = devm_clk_get(dev, clki->name); if (IS_ERR(clki->clk)) {
ret = PTR_ERR(clki->clk);
dev_err(dev, "%s: %s clk get failed, %d\n",
__func__, clki->name, ret); goto out;
}
/* * Parse device ref clk freq as per device tree "ref_clk". * Default dev_ref_clk_freq is set to REF_CLK_FREQ_INVAL * in ufshcd_alloc_host().
*/ if (!strcmp(clki->name, "ref_clk"))
ufshcd_parse_dev_ref_clk_freq(hba, clki->clk);
if (clki->max_freq) {
ret = clk_set_rate(clki->clk, clki->max_freq); if (ret) {
dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
__func__, clki->name,
clki->max_freq, ret); goto out;
}
clki->curr_freq = clki->max_freq;
}
dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
clki->name, clk_get_rate(clki->clk));
}
/* Set Max. frequency for all clocks */ if (hba->use_pm_opp) {
ret = ufshcd_opp_set_rate(hba, ULONG_MAX); if (ret) {
dev_err(hba->dev, "%s: failed to set OPP: %d", __func__,
ret); goto out;
}
}
out: return ret;
}
staticint ufshcd_variant_hba_init(struct ufs_hba *hba)
{ int err = 0;
staticvoid ufshcd_variant_hba_exit(struct ufs_hba *hba)
{ if (!hba->vops) return;
ufshcd_vops_exit(hba);
}
staticint ufshcd_hba_init(struct ufs_hba *hba)
{ int err;
/* * Handle host controller power separately from the UFS device power * rails as it will help controlling the UFS host controller power * collapse easily which is different than UFS device power collapse. * Also, enable the host controller power before we go ahead with rest * of the initialization here.
*/
err = ufshcd_init_hba_vreg(hba); if (err) goto out;
err = ufshcd_setup_hba_vreg(hba, true); if (err) goto out;
err = ufshcd_init_clocks(hba); if (err) goto out_disable_hba_vreg;
if (hba->dev_ref_clk_freq == REF_CLK_FREQ_INVAL)
hba->dev_ref_clk_freq = ufshcd_parse_ref_clk_property(hba);
err = ufshcd_setup_clocks(hba, true); if (err) goto out_disable_hba_vreg;
err = ufshcd_init_vreg(hba); if (err) goto out_disable_clks;
err = ufshcd_setup_vreg(hba, true); if (err) goto out_disable_clks;
err = ufshcd_variant_hba_init(hba); if (err) goto out_disable_vreg;
/** * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device * power mode * @hba: per adapter instance * @pwr_mode: device power mode to set * * Return: 0 if requested power mode is set successfully; * < 0 if failed to set the requested power mode.
*/ staticint ufshcd_set_dev_pwr_mode(struct ufs_hba *hba, enum ufs_dev_pwr_mode pwr_mode)
{ struct scsi_sense_hdr sshdr; struct scsi_device *sdp; unsignedlong flags; int ret;
spin_lock_irqsave(hba->host->host_lock, flags);
sdp = hba->ufs_device_wlun; if (sdp && scsi_device_online(sdp))
ret = scsi_device_get(sdp); else
ret = -ENODEV;
spin_unlock_irqrestore(hba->host->host_lock, flags);
if (ret) return ret;
/* * If scsi commands fail, the scsi mid-layer schedules scsi error- * handling, which would wait for host to be resumed. Since we know * we are functional while we are here, skip host resume in error * handling context.
*/
hba->host->eh_noresume = 1;
/* * Current function would be generally called from the power management * callbacks hence set the RQF_PM flag so that it doesn't resume the * already suspended childs.
*/
ret = ufshcd_execute_start_stop(sdp, pwr_mode, &sshdr); if (ret) {
sdev_printk(KERN_WARNING, sdp, "START_STOP failed for power mode: %d, result %x\n",
pwr_mode, ret); if (ret > 0) { if (scsi_sense_valid(&sshdr))
scsi_print_sense_hdr(sdp, NULL, &sshdr);
ret = -EIO;
}
} else {
hba->curr_dev_pwr_mode = pwr_mode;
}
staticint ufshcd_link_state_transition(struct ufs_hba *hba, enum uic_link_state req_link_state, bool check_for_bkops)
{ int ret = 0;
if (req_link_state == hba->uic_link_state) return 0;
if (req_link_state == UIC_LINK_HIBERN8_STATE) {
ret = ufshcd_uic_hibern8_enter(hba); if (!ret) {
ufshcd_set_link_hibern8(hba);
} else {
dev_err(hba->dev, "%s: hibern8 enter failed %d\n",
__func__, ret); goto out;
}
} /* * If autobkops is enabled, link can't be turned off because * turning off the link would also turn off the device, except in the * case of DeepSleep where the device is expected to remain powered.
*/ elseif ((req_link_state == UIC_LINK_OFF_STATE) &&
(!check_for_bkops || !hba->auto_bkops_enabled)) { /* * Let's make sure that link is in low power mode, we are doing * this currently by putting the link in Hibern8. Otherway to * put the link in low power mode is to send the DME end point * to device and then send the DME reset command to local * unipro. But putting the link in hibern8 is much faster. * * Note also that putting the link in Hibern8 is a requirement * for entering DeepSleep.
*/
ret = ufshcd_uic_hibern8_enter(hba); if (ret) {
dev_err(hba->dev, "%s: hibern8 enter failed %d\n",
__func__, ret); goto out;
} /* * Change controller state to "reset state" which * should also put the link in off/reset state
*/
ufshcd_hba_stop(hba); /* * TODO: Check if we need any delay to make sure that * controller is reset
*/
ufshcd_set_link_off(hba);
}
/* * It seems some UFS devices may keep drawing more than sleep current * (atleast for 500us) from UFS rails (especially from VCCQ rail). * To avoid this situation, add 2ms delay before putting these UFS * rails in LPM mode.
*/ if (!ufshcd_is_link_active(hba) &&
hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
usleep_range(2000, 2100);
/* * If UFS device is either in UFS_Sleep turn off VCC rail to save some * power. * * If UFS device and link is in OFF state, all power supplies (VCC, * VCCQ, VCCQ2) can be turned off if power on write protect is not * required. If UFS link is inactive (Hibern8 or OFF state) and device * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode. * * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway * in low power state which would save some power. * * If Write Booster is enabled and the device needs to flush the WB * buffer OR if bkops status is urgent for WB, keep Vcc on.
*/ if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
!hba->dev_info.is_lu_power_on_wp) {
ufshcd_setup_vreg(hba, false);
vcc_off = true;
} elseif (!ufshcd_is_ufs_dev_active(hba)) {
ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
vcc_off = true; if (ufshcd_is_link_hibern8(hba) || ufshcd_is_link_off(hba)) {
ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
}
}
/* * Some UFS devices require delay after VCC power rail is turned-off.
*/ if (vcc_off && hba->vreg_info.vcc &&
hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_AFTER_LPM)
usleep_range(5000, 5100);
}
#ifdef CONFIG_PM staticint ufshcd_vreg_set_hpm(struct ufs_hba *hba)
{ int ret = 0;
if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
!hba->dev_info.is_lu_power_on_wp) {
ret = ufshcd_setup_vreg(hba, true);
} elseif (!ufshcd_is_ufs_dev_active(hba)) { if (!ufshcd_is_link_active(hba)) {
ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq); if (ret) goto vcc_disable;
ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2); if (ret) goto vccq_lpm;
}
ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
} goto out;
if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
(req_link_state == hba->uic_link_state)) goto enable_scaling;
/* UFS device & link must be active before we enter in this function */ if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) { /* Wait err handler finish or trigger err recovery */ if (!ufshcd_eh_in_progress(hba))
ufshcd_force_error_recovery(hba);
ret = -EBUSY; goto enable_scaling;
}
if (pm_op == UFS_RUNTIME_PM) { if (ufshcd_can_autobkops_during_suspend(hba)) { /* * The device is idle with no requests in the queue, * allow background operations if bkops status shows * that performance might be impacted.
*/
ret = ufshcd_bkops_ctrl(hba); if (ret) { /* * If return err in suspend flow, IO will hang. * Trigger error handler and break suspend for * error recovery.
*/
ufshcd_force_error_recovery(hba);
ret = -EBUSY; goto enable_scaling;
}
} else { /* make sure that auto bkops is disabled */
ufshcd_disable_auto_bkops(hba);
} /* * If device needs to do BKOP or WB buffer flush during * Hibern8, keep device power mode as "active power mode" * and VCC supply.
*/
hba->dev_info.b_rpm_dev_flush_capable =
hba->auto_bkops_enabled ||
(((req_link_state == UIC_LINK_HIBERN8_STATE) ||
((req_link_state == UIC_LINK_ACTIVE_STATE) &&
ufshcd_is_auto_hibern8_enabled(hba))) &&
ufshcd_wb_need_flush(hba));
}
flush_work(&hba->eeh_work);
ret = ufshcd_vops_suspend(hba, pm_op, PRE_CHANGE); if (ret) goto enable_scaling;
if (req_dev_pwr_mode != hba->curr_dev_pwr_mode) { if (pm_op != UFS_RUNTIME_PM) /* ensure that bkops is disabled */
ufshcd_disable_auto_bkops(hba);
if (!hba->dev_info.b_rpm_dev_flush_capable) {
ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode); if (ret && pm_op != UFS_SHUTDOWN_PM) { /* * If return err in suspend flow, IO will hang. * Trigger error handler and break suspend for * error recovery.
*/
ufshcd_force_error_recovery(hba);
ret = -EBUSY;
} if (ret) goto enable_scaling;
}
}
/* * In the case of DeepSleep, the device is expected to remain powered * with the link off, so do not check for bkops.
*/
check_for_bkops = !ufshcd_is_ufs_dev_deepsleep(hba);
ret = ufshcd_link_state_transition(hba, req_link_state, check_for_bkops); if (ret && pm_op != UFS_SHUTDOWN_PM) { /* * If return err in suspend flow, IO will hang. * Trigger error handler and break suspend for * error recovery.
*/
ufshcd_force_error_recovery(hba);
ret = -EBUSY;
} if (ret) goto set_dev_active;
vops_suspend: /* * Call vendor specific suspend callback. As these callbacks may access * vendor specific host controller register space call them before the * host clocks are ON.
*/
ret = ufshcd_vops_suspend(hba, pm_op, POST_CHANGE); if (ret) goto set_link_active;
set_link_active: /* * Device hardware reset is required to exit DeepSleep. Also, for * DeepSleep, the link is off so host reset and restore will be done * further below.
*/ if (ufshcd_is_ufs_dev_deepsleep(hba)) {
ufshcd_device_reset(hba);
WARN_ON(!ufshcd_is_link_off(hba));
} if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
ufshcd_set_link_active(hba); elseif (ufshcd_is_link_off(hba))
ufshcd_host_reset_and_restore(hba);
set_dev_active: /* Can also get here needing to exit DeepSleep */ if (ufshcd_is_ufs_dev_deepsleep(hba)) {
ufshcd_device_reset(hba);
ufshcd_host_reset_and_restore(hba);
} if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
ufshcd_disable_auto_bkops(hba);
enable_scaling: if (ufshcd_is_clkscaling_supported(hba))
ufshcd_clk_scaling_suspend(hba, false);
hba->dev_info.b_rpm_dev_flush_capable = false;
out: if (hba->dev_info.b_rpm_dev_flush_capable) {
schedule_delayed_work(&hba->rpm_dev_flush_recheck_work,
msecs_to_jiffies(RPM_DEV_FLUSH_RECHECK_WORK_DELAY_MS));
}
/* * Call vendor specific resume callback. As these callbacks may access * vendor specific host controller register space call them when the * host clocks are ON.
*/
ret = ufshcd_vops_resume(hba, pm_op); if (ret) goto out;
/* For DeepSleep, the only supported option is to have the link off */
WARN_ON(ufshcd_is_ufs_dev_deepsleep(hba) && !ufshcd_is_link_off(hba));
if (ufshcd_is_link_hibern8(hba)) {
ret = ufshcd_uic_hibern8_exit(hba); if (!ret) {
ufshcd_set_link_active(hba);
} else {
dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
__func__, ret); goto vendor_suspend;
}
} elseif (ufshcd_is_link_off(hba)) { /* * A full initialization of the host and the device is * required since the link was put to off during suspend. * Note, in the case of DeepSleep, the device will exit * DeepSleep due to device reset.
*/
ret = ufshcd_reset_and_restore(hba); /* * ufshcd_reset_and_restore() should have already * set the link state as active
*/ if (ret || !ufshcd_is_link_active(hba)) goto vendor_suspend;
}
if (!ufshcd_is_ufs_dev_active(hba)) {
ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE); if (ret) goto set_old_link_state;
ufshcd_set_timestamp_attr(hba);
schedule_delayed_work(&hba->ufs_rtc_update_work,
msecs_to_jiffies(UFS_RTC_UPDATE_INTERVAL_MS));
}
if (ufshcd_keep_autobkops_enabled_except_suspend(hba))
ufshcd_enable_auto_bkops(hba); else /* * If BKOPs operations are urgently needed at this moment then * keep auto-bkops enabled or else disable it.
*/
ufshcd_bkops_ctrl(hba);
if (hba->ee_usr_mask)
ufshcd_write_ee_control(hba);
if (ufshcd_is_clkscaling_supported(hba))
ufshcd_clk_scaling_suspend(hba, false);
if (hba->dev_info.b_rpm_dev_flush_capable) {
hba->dev_info.b_rpm_dev_flush_capable = false;
cancel_delayed_work(&hba->rpm_dev_flush_recheck_work);
}
/** * ufshcd_suspend - helper function for suspend operations * @hba: per adapter instance * * This function will put disable irqs, turn off clocks * and set vreg and hba-vreg in lpm mode. * * Return: 0 upon success; < 0 upon failure.
*/ staticint ufshcd_suspend(struct ufs_hba *hba)
{ int ret;
if (!hba->is_powered) return 0; /* * Disable the host irq as host controller as there won't be any * host controller transaction expected till resume.
*/
ufshcd_disable_irq(hba);
ret = ufshcd_setup_clocks(hba, false); if (ret) {
ufshcd_enable_irq(hba); return ret;
} if (ufshcd_is_clkgating_allowed(hba)) {
hba->clk_gating.state = CLKS_OFF;
trace_ufshcd_clk_gating(hba,
hba->clk_gating.state);
}
ufshcd_vreg_set_lpm(hba); /* Put the host controller in low power mode if possible */
ufshcd_hba_vreg_set_lpm(hba);
ufshcd_pm_qos_update(hba, false); return ret;
}
#ifdef CONFIG_PM /** * ufshcd_resume - helper function for resume operations * @hba: per adapter instance * * This function basically turns on the regulators, clocks and * irqs of the hba. * * Return: 0 for success and non-zero for failure.
*/ staticint ufshcd_resume(struct ufs_hba *hba)
{ int ret;
if (!hba->is_powered) return 0;
ufshcd_hba_vreg_set_hpm(hba);
ret = ufshcd_vreg_set_hpm(hba); if (ret) goto out;
/* Make sure clocks are enabled before accessing controller */
ret = ufshcd_setup_clocks(hba, true); if (ret) goto disable_vreg;
/* enable the host irq as host controller would be active soon */
ufshcd_enable_irq(hba);
#ifdef CONFIG_PM_SLEEP /** * ufshcd_system_suspend - system suspend callback * @dev: Device associated with the UFS controller. * * Executed before putting the system into a sleep state in which the contents * of main memory are preserved. * * Return: 0 for success and non-zero for failure.
*/ int ufshcd_system_suspend(struct device *dev)
{ struct ufs_hba *hba = dev_get_drvdata(dev); int ret = 0;
ktime_t start = ktime_get();
/** * ufshcd_system_resume - system resume callback * @dev: Device associated with the UFS controller. * * Executed after waking the system up from a sleep state in which the contents * of main memory were preserved. * * Return: 0 for success and non-zero for failure.
*/ int ufshcd_system_resume(struct device *dev)
{ struct ufs_hba *hba = dev_get_drvdata(dev);
ktime_t start = ktime_get(); int ret = 0;
#ifdef CONFIG_PM /** * ufshcd_runtime_suspend - runtime suspend callback * @dev: Device associated with the UFS controller. * * Check the description of ufshcd_suspend() function for more details. * * Return: 0 for success and non-zero for failure.
*/ int ufshcd_runtime_suspend(struct device *dev)
{ struct ufs_hba *hba = dev_get_drvdata(dev); int ret;
ktime_t start = ktime_get();
/** * ufshcd_runtime_resume - runtime resume routine * @dev: Device associated with the UFS controller. * * This function basically brings controller * to active state. Following operations are done in this function: * * 1. Turn on all the controller related clocks * 2. Turn ON VCC rail * * Return: 0 upon success; < 0 upon failure.
*/ int ufshcd_runtime_resume(struct device *dev)
{ struct ufs_hba *hba = dev_get_drvdata(dev); int ret;
ktime_t start = ktime_get();
/* Turn on everything while shutting down */
ufshcd_rpm_get_sync(hba);
scsi_device_quiesce(sdev);
shost_for_each_device(sdev, hba->host) { if (sdev == hba->ufs_device_wlun) continue;
mutex_lock(&sdev->state_mutex);
scsi_device_set_state(sdev, SDEV_OFFLINE);
mutex_unlock(&sdev->state_mutex);
}
__ufshcd_wl_suspend(hba, UFS_SHUTDOWN_PM);
/* * Next, turn off the UFS controller and the UFS regulators. Disable * clocks.
*/ if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
ufshcd_suspend(hba);
hba->is_powered = false;
}
/** * ufshcd_remove - de-allocate SCSI host and host memory space * data structure memory * @hba: per adapter instance
*/ void ufshcd_remove(struct ufs_hba *hba)
{ if (hba->ufs_device_wlun)
ufshcd_rpm_get_sync(hba);
ufs_hwmon_remove(hba);
ufs_bsg_remove(hba);
ufs_sysfs_remove_nodes(hba->dev);
cancel_delayed_work_sync(&hba->ufs_rtc_update_work);
blk_mq_destroy_queue(hba->tmf_queue);
blk_put_queue(hba->tmf_queue);
blk_mq_free_tag_set(&hba->tmf_tag_set); if (hba->scsi_host_added)
scsi_remove_host(hba->host); /* disable interrupts */
ufshcd_disable_intr(hba, hba->intr_mask);
ufshcd_hba_stop(hba);
ufshcd_hba_exit(hba);
}
EXPORT_SYMBOL_GPL(ufshcd_remove);
#ifdef CONFIG_PM_SLEEP int ufshcd_system_freeze(struct device *dev)
{
return ufshcd_system_suspend(dev);
}
EXPORT_SYMBOL_GPL(ufshcd_system_freeze);
int ufshcd_system_restore(struct device *dev)
{
struct ufs_hba *hba = dev_get_drvdata(dev); int ret;
ret = ufshcd_system_resume(dev); if (ret) return ret;
/* Configure UTRL and UTMRL base address registers */
ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
REG_UTP_TRANSFER_REQ_LIST_BASE_L);
ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
REG_UTP_TRANSFER_REQ_LIST_BASE_H);
ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
REG_UTP_TASK_REQ_LIST_BASE_L);
ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
REG_UTP_TASK_REQ_LIST_BASE_H); /* * Make sure that UTRL and UTMRL base address registers * are updated with the latest queue addresses. Only after * updating these addresses, we can queue the new commands.
*/
ufshcd_readl(hba, REG_UTP_TASK_REQ_LIST_BASE_H);
/** * ufshcd_set_dma_mask - Set dma mask based on the controller * addressing capability * @hba: per adapter instance * * Return: 0 for success, non-zero for failure.
*/ staticint ufshcd_set_dma_mask(struct ufs_hba *hba)
{ if (hba->vops && hba->vops->set_dma_mask) return hba->vops->set_dma_mask(hba); if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) { if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64))) return 0;
} return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
}
/** * ufshcd_devres_release - devres cleanup handler, invoked during release of * hba->dev * @host: pointer to SCSI host
*/ staticvoid ufshcd_devres_release(void *host)
{
scsi_host_put(host);
}
/** * ufshcd_alloc_host - allocate Host Bus Adapter (HBA) * @dev: pointer to device handle * @hba_handle: driver private handle * * Return: 0 on success, non-zero value on failure. * * NOTE: There is no corresponding ufshcd_dealloc_host() because this function * keeps track of its allocations using devres and deallocates everything on * device removal automatically.
*/ int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
{ struct Scsi_Host *host; struct ufs_hba *hba; int err = 0;
if (!dev) {
dev_err(dev, "Invalid memory reference for dev is NULL\n");
err = -ENODEV; goto out_error;
}
remove_scsi_host: if (hba->scsi_host_added)
scsi_remove_host(hba->host);
return err;
}
/** * ufshcd_init - Driver initialization routine * @hba: per-adapter instance * @mmio_base: base register address * @irq: Interrupt line of device * * Return: 0 on success; < 0 on failure.
*/ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsignedint irq)
{ int err; struct Scsi_Host *host = hba->host; struct device *dev = hba->dev;
/* * dev_set_drvdata() must be called before any callbacks are registered * that use dev_get_drvdata() (frequency scaling, clock scaling, hwmon, * sysfs).
*/
dev_set_drvdata(dev, hba);
if (!mmio_base) {
dev_err(hba->dev, "Invalid memory reference for mmio_base is NULL\n");
err = -ENODEV; goto out_error;
}
/* * Initialize clk_gating.lock early since it is being used in * ufshcd_setup_clocks()
*/
spin_lock_init(&hba->clk_gating.lock);
/* Initialize mutex for PM QoS request synchronization */
mutex_init(&hba->pm_qos_mutex);
/* * Set the default power management level for runtime and system PM. * Host controller drivers can override them in their * 'ufs_hba_variant_ops::init' callback. * * Default power saving mode is to keep UFS link in Hibern8 state * and UFS device in sleep state.
*/
hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
UFS_SLEEP_PWR_MODE,
UIC_LINK_HIBERN8_STATE);
hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
UFS_SLEEP_PWR_MODE,
UIC_LINK_HIBERN8_STATE);
init_completion(&hba->dev_cmd.complete);
err = ufshcd_hba_init(hba); if (err) goto out_error;
/* Initialize mutex for device management commands */
mutex_init(&hba->dev_cmd.lock);
/* Initialize mutex for exception event control */
mutex_init(&hba->ee_ctrl_mutex);
mutex_init(&hba->wb_mutex);
init_rwsem(&hba->clk_scaling_lock);
ufshcd_init_clk_gating(hba);
ufshcd_init_clk_scaling(hba);
/* * In order to avoid any spurious interrupt immediately after * registering UFS controller interrupt handler, clear any pending UFS * interrupt status and disable all the UFS interrupts.
*/
ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
REG_INTERRUPT_STATUS);
ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE); /* * Make sure that UFS interrupts are disabled and any pending interrupt * status is cleared before registering UFS interrupt handler.
*/
ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
/* Set the default auto-hiberate idle timer value to 150 ms */ if (ufshcd_is_auto_hibern8_supported(hba) && !hba->ahit) {
hba->ahit = FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, 150) |
FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, 3);
}
/* Hold auto suspend until async scan completes */
pm_runtime_get_sync(dev);
/* * We are assuming that device wasn't put in sleep/power-down * state exclusively during the boot stage before kernel. * This assumption helps avoid doing link startup twice during * ufshcd_probe_hba().
*/
ufshcd_set_ufs_dev_active(hba);
int __ufshcd_suspend_prepare(struct device *dev, bool rpm_ok_for_spm)
{ struct ufs_hba *hba = dev_get_drvdata(dev); int ret;
/* * SCSI assumes that runtime-pm and system-pm for scsi drivers * are same. And it doesn't wake up the device for system-suspend * if it's runtime suspended. But ufs doesn't follow that. * Refer ufshcd_resume_complete()
*/ if (hba->ufs_device_wlun) { /* Prevent runtime suspend */
ufshcd_rpm_get_noresume(hba); /* * Check if already runtime suspended in same state as system * suspend would be.
*/ if (!rpm_ok_for_spm || !ufshcd_rpm_ok_for_spm(hba)) { /* RPM state is not ok for SPM, so runtime resume */
ret = ufshcd_rpm_resume(hba); if (ret < 0 && ret != -EACCES) {
ufshcd_rpm_put(hba); return ret;
}
}
hba->complete_put = true;
} return 0;
}
EXPORT_SYMBOL_GPL(__ufshcd_suspend_prepare);
int ufshcd_suspend_prepare(struct device *dev)
{ return __ufshcd_suspend_prepare(dev, true);
}
EXPORT_SYMBOL_GPL(ufshcd_suspend_prepare);
staticvoid ufshcd_check_header_layout(void)
{ /* * gcc compilers before version 10 cannot do constant-folding for * sub-byte bitfields. Hence skip the layout checks for gcc 9 and * before.
*/ if (IS_ENABLED(CONFIG_CC_IS_GCC) && CONFIG_GCC_VERSION < 100000) return;
/* * ufs_dev_wlun_template - describes ufs device wlun * ufs-device wlun - used to send pm commands * All luns are consumers of ufs-device wlun. * * Currently, no sd driver is present for wluns. * Hence the no specific pm operations are performed. * With ufs design, SSU should be sent to ufs-device wlun. * Hence register a scsi driver for ufs wluns only.
*/ staticstruct scsi_driver ufs_dev_wlun_template = {
.gendrv = {
.name = "ufs_device_wlun",
.probe = ufshcd_wl_probe,
.remove = ufshcd_wl_remove,
.pm = &ufshcd_wl_pm_ops,
.shutdown = ufshcd_wl_shutdown,
},
};
staticint __init ufshcd_core_init(void)
{ int ret;
ufshcd_check_header_layout();
ufs_debugfs_init();
ret = scsi_register_driver(&ufs_dev_wlun_template.gendrv); if (ret)
ufs_debugfs_exit(); return ret;
}
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.312Bemerkung:
(vorverarbeitet am 2026-04-29)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.