Bean Huo | 6735111 | 2020-06-05 22:05:19 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2 | /* |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 3 | * Universal Flash Storage Host controller driver Core |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 4 | * Copyright (C) 2011-2013 Samsung India Software Operations |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 5 | * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6 | * |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7 | * Authors: |
| 8 | * Santosh Yaraganavi <santosh.sy@samsung.com> |
| 9 | * Vinayak Holikatti <h.vinayak@samsung.com> |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 10 | */ |
| 11 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 12 | #include <linux/async.h> |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 13 | #include <linux/devfreq.h> |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 14 | #include <linux/nls.h> |
Yaniv Gardi | 54b879b | 2016-03-10 17:37:05 +0200 | [diff] [blame] | 15 | #include <linux/of.h> |
Adrian Hunter | ad44837 | 2018-03-20 15:07:38 +0200 | [diff] [blame] | 16 | #include <linux/bitfield.h> |
Can Guo | fb276f7 | 2020-03-25 18:09:59 -0700 | [diff] [blame] | 17 | #include <linux/blk-pm.h> |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 18 | #include "ufshcd.h" |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 19 | #include "ufs_quirks.h" |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 20 | #include "unipro.h" |
Stanislav Nijnikov | cbb6813 | 2018-02-15 14:14:01 +0200 | [diff] [blame] | 21 | #include "ufs-sysfs.h" |
Avri Altman | df032bf | 2018-10-07 17:30:35 +0300 | [diff] [blame] | 22 | #include "ufs_bsg.h" |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 23 | #include "ufshcd-crypto.h" |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 24 | #include <asm/unaligned.h> |
| 25 | #include <linux/blkdev.h> |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 26 | |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 27 | #define CREATE_TRACE_POINTS |
| 28 | #include <trace/events/ufs.h> |
| 29 | |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 30 | #define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\ |
| 31 | UTP_TASK_REQ_COMPL |\ |
| 32 | UFSHCD_ERROR_MASK) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 33 | /* UIC command timeout, unit: ms */ |
| 34 | #define UIC_CMD_TIMEOUT 500 |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 35 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 36 | /* NOP OUT retries waiting for NOP IN response */ |
| 37 | #define NOP_OUT_RETRIES 10 |
| 38 | /* Timeout after 30 msecs if NOP OUT hangs without response */ |
| 39 | #define NOP_OUT_TIMEOUT 30 /* msecs */ |
| 40 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 41 | /* Query request retries */ |
subhashj@codeaurora.org | 10fe588 | 2016-11-23 16:31:52 -0800 | [diff] [blame] | 42 | #define QUERY_REQ_RETRIES 3 |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 43 | /* Query request timeout */ |
subhashj@codeaurora.org | 10fe588 | 2016-11-23 16:31:52 -0800 | [diff] [blame] | 44 | #define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */ |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 45 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 46 | /* Task management command timeout */ |
| 47 | #define TM_CMD_TIMEOUT 100 /* msecs */ |
| 48 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 49 | /* maximum number of retries for a general UIC command */ |
| 50 | #define UFS_UIC_COMMAND_RETRIES 3 |
| 51 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 52 | /* maximum number of link-startup retries */ |
| 53 | #define DME_LINKSTARTUP_RETRIES 3 |
| 54 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 55 | /* Maximum retries for Hibern8 enter */ |
| 56 | #define UIC_HIBERN8_ENTER_RETRIES 3 |
| 57 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 58 | /* maximum number of reset retries before giving up */ |
| 59 | #define MAX_HOST_RESET_RETRIES 5 |
| 60 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 61 | /* Expose the flag value from utp_upiu_query.value */ |
| 62 | #define MASK_QUERY_UPIU_FLAG_LOC 0xFF |
| 63 | |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 64 | /* Interrupt aggregation default timeout, unit: 40us */ |
| 65 | #define INT_AGGR_DEF_TO 0x02 |
| 66 | |
Stanley Chu | 49615ba | 2019-09-16 23:56:50 +0800 | [diff] [blame] | 67 | /* default delay of autosuspend: 2000 ms */ |
| 68 | #define RPM_AUTOSUSPEND_DELAY_MS 2000 |
| 69 | |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 70 | /* Default delay of RPM device flush delayed work */ |
| 71 | #define RPM_DEV_FLUSH_RECHECK_WORK_DELAY_MS 5000 |
| 72 | |
Can Guo | 09f1779 | 2020-02-10 19:40:49 -0800 | [diff] [blame] | 73 | /* Default value of wait time before gating device ref clock */ |
| 74 | #define UFSHCD_REF_CLK_GATING_WAIT_US 0xFF /* microsecs */ |
| 75 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 76 | #define ufshcd_toggle_vreg(_dev, _vreg, _on) \ |
| 77 | ({ \ |
| 78 | int _ret; \ |
| 79 | if (_on) \ |
| 80 | _ret = ufshcd_enable_vreg(_dev, _vreg); \ |
| 81 | else \ |
| 82 | _ret = ufshcd_disable_vreg(_dev, _vreg); \ |
| 83 | _ret; \ |
| 84 | }) |
| 85 | |
Tomas Winkler | ba80917 | 2018-06-14 11:14:09 +0300 | [diff] [blame] | 86 | #define ufshcd_hex_dump(prefix_str, buf, len) do { \ |
| 87 | size_t __len = (len); \ |
| 88 | print_hex_dump(KERN_ERR, prefix_str, \ |
| 89 | __len > 4 ? DUMP_PREFIX_OFFSET : DUMP_PREFIX_NONE,\ |
| 90 | 16, 4, buf, __len, false); \ |
| 91 | } while (0) |
| 92 | |
| 93 | int ufshcd_dump_regs(struct ufs_hba *hba, size_t offset, size_t len, |
| 94 | const char *prefix) |
| 95 | { |
Marc Gonzalez | d672475 | 2019-01-22 18:29:22 +0100 | [diff] [blame] | 96 | u32 *regs; |
| 97 | size_t pos; |
| 98 | |
| 99 | if (offset % 4 != 0 || len % 4 != 0) /* keep readl happy */ |
| 100 | return -EINVAL; |
Tomas Winkler | ba80917 | 2018-06-14 11:14:09 +0300 | [diff] [blame] | 101 | |
Can Guo | cddaeba | 2019-11-14 22:09:27 -0800 | [diff] [blame] | 102 | regs = kzalloc(len, GFP_ATOMIC); |
Tomas Winkler | ba80917 | 2018-06-14 11:14:09 +0300 | [diff] [blame] | 103 | if (!regs) |
| 104 | return -ENOMEM; |
| 105 | |
Marc Gonzalez | d672475 | 2019-01-22 18:29:22 +0100 | [diff] [blame] | 106 | for (pos = 0; pos < len; pos += 4) |
| 107 | regs[pos / 4] = ufshcd_readl(hba, offset + pos); |
| 108 | |
Tomas Winkler | ba80917 | 2018-06-14 11:14:09 +0300 | [diff] [blame] | 109 | ufshcd_hex_dump(prefix, regs, len); |
| 110 | kfree(regs); |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | EXPORT_SYMBOL_GPL(ufshcd_dump_regs); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 115 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 116 | enum { |
| 117 | UFSHCD_MAX_CHANNEL = 0, |
| 118 | UFSHCD_MAX_ID = 1, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 119 | UFSHCD_CMD_PER_LUN = 32, |
| 120 | UFSHCD_CAN_QUEUE = 32, |
| 121 | }; |
| 122 | |
| 123 | /* UFSHCD states */ |
| 124 | enum { |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 125 | UFSHCD_STATE_RESET, |
| 126 | UFSHCD_STATE_ERROR, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 127 | UFSHCD_STATE_OPERATIONAL, |
Zang Leigang | 141f816 | 2016-11-16 11:29:37 +0800 | [diff] [blame] | 128 | UFSHCD_STATE_EH_SCHEDULED, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | /* UFSHCD error handling flags */ |
| 132 | enum { |
| 133 | UFSHCD_EH_IN_PROGRESS = (1 << 0), |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 134 | }; |
| 135 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 136 | /* UFSHCD UIC layer error flags */ |
| 137 | enum { |
| 138 | UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 139 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */ |
| 140 | UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */ |
| 141 | UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */ |
| 142 | UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */ |
| 143 | UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 144 | }; |
| 145 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 146 | #define ufshcd_set_eh_in_progress(h) \ |
Tomohiro Kusumi | 9c490d2 | 2017-03-28 16:49:26 +0300 | [diff] [blame] | 147 | ((h)->eh_flags |= UFSHCD_EH_IN_PROGRESS) |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 148 | #define ufshcd_eh_in_progress(h) \ |
Tomohiro Kusumi | 9c490d2 | 2017-03-28 16:49:26 +0300 | [diff] [blame] | 149 | ((h)->eh_flags & UFSHCD_EH_IN_PROGRESS) |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 150 | #define ufshcd_clear_eh_in_progress(h) \ |
Tomohiro Kusumi | 9c490d2 | 2017-03-28 16:49:26 +0300 | [diff] [blame] | 151 | ((h)->eh_flags &= ~UFSHCD_EH_IN_PROGRESS) |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 152 | |
Stanislav Nijnikov | cbb6813 | 2018-02-15 14:14:01 +0200 | [diff] [blame] | 153 | struct ufs_pm_lvl_states ufs_pm_lvl_states[] = { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 154 | {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE}, |
| 155 | {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 156 | {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE}, |
| 157 | {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 158 | {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 159 | {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE}, |
| 160 | }; |
| 161 | |
| 162 | static inline enum ufs_dev_pwr_mode |
| 163 | ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl) |
| 164 | { |
| 165 | return ufs_pm_lvl_states[lvl].dev_state; |
| 166 | } |
| 167 | |
| 168 | static inline enum uic_link_state |
| 169 | ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl) |
| 170 | { |
| 171 | return ufs_pm_lvl_states[lvl].link_state; |
| 172 | } |
| 173 | |
subhashj@codeaurora.org | 0c8f758 | 2016-12-22 18:41:11 -0800 | [diff] [blame] | 174 | static inline enum ufs_pm_level |
| 175 | ufs_get_desired_pm_lvl_for_dev_link_state(enum ufs_dev_pwr_mode dev_state, |
| 176 | enum uic_link_state link_state) |
| 177 | { |
| 178 | enum ufs_pm_level lvl; |
| 179 | |
| 180 | for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) { |
| 181 | if ((ufs_pm_lvl_states[lvl].dev_state == dev_state) && |
| 182 | (ufs_pm_lvl_states[lvl].link_state == link_state)) |
| 183 | return lvl; |
| 184 | } |
| 185 | |
| 186 | /* if no match found, return the level 0 */ |
| 187 | return UFS_PM_LVL_0; |
| 188 | } |
| 189 | |
Subhash Jadavani | 56d4a18 | 2016-12-05 19:25:32 -0800 | [diff] [blame] | 190 | static struct ufs_dev_fix ufs_fixups[] = { |
| 191 | /* UFS cards deviations table */ |
Stanley Chu | c0a18ee | 2020-06-12 09:26:24 +0800 | [diff] [blame] | 192 | UFS_FIX(UFS_VENDOR_MICRON, UFS_ANY_MODEL, |
| 193 | UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM), |
Subhash Jadavani | 56d4a18 | 2016-12-05 19:25:32 -0800 | [diff] [blame] | 194 | UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, |
Stanley Chu | ed0b40f | 2020-06-12 09:26:25 +0800 | [diff] [blame] | 195 | UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM | |
| 196 | UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE | |
Subhash Jadavani | 56d4a18 | 2016-12-05 19:25:32 -0800 | [diff] [blame] | 197 | UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS), |
Stanley Chu | ed0b40f | 2020-06-12 09:26:25 +0800 | [diff] [blame] | 198 | UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL, |
| 199 | UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME), |
| 200 | UFS_FIX(UFS_VENDOR_SKHYNIX, "hB8aL1" /*H28U62301AMR*/, |
| 201 | UFS_DEVICE_QUIRK_HOST_VS_DEBUGSAVECONFIGTIME), |
Subhash Jadavani | 56d4a18 | 2016-12-05 19:25:32 -0800 | [diff] [blame] | 202 | UFS_FIX(UFS_VENDOR_TOSHIBA, UFS_ANY_MODEL, |
| 203 | UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM), |
| 204 | UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9C8KBADG", |
| 205 | UFS_DEVICE_QUIRK_PA_TACTIVATE), |
| 206 | UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9D8KBADG", |
| 207 | UFS_DEVICE_QUIRK_PA_TACTIVATE), |
Subhash Jadavani | 56d4a18 | 2016-12-05 19:25:32 -0800 | [diff] [blame] | 208 | END_FIX |
| 209 | }; |
| 210 | |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 211 | static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 212 | static void ufshcd_async_scan(void *data, async_cookie_t cookie); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 213 | static int ufshcd_reset_and_restore(struct ufs_hba *hba); |
Dolev Raviv | e7d3825 | 2016-12-22 18:40:07 -0800 | [diff] [blame] | 214 | static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 215 | static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 216 | static void ufshcd_hba_exit(struct ufs_hba *hba); |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 217 | static int ufshcd_probe_hba(struct ufs_hba *hba, bool async); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 218 | static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on, |
| 219 | bool skip_ref_clk); |
| 220 | static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 221 | static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 222 | static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 223 | static int ufshcd_host_reset_and_restore(struct ufs_hba *hba); |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 224 | static void ufshcd_resume_clkscaling(struct ufs_hba *hba); |
| 225 | static void ufshcd_suspend_clkscaling(struct ufs_hba *hba); |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 226 | static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba); |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 227 | static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 228 | static irqreturn_t ufshcd_intr(int irq, void *__hba); |
Yaniv Gardi | 874237f | 2015-05-17 18:55:03 +0300 | [diff] [blame] | 229 | static int ufshcd_change_power_mode(struct ufs_hba *hba, |
| 230 | struct ufs_pa_layer_attr *pwr_mode); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 231 | static int ufshcd_wb_buf_flush_enable(struct ufs_hba *hba); |
| 232 | static int ufshcd_wb_buf_flush_disable(struct ufs_hba *hba); |
| 233 | static int ufshcd_wb_ctrl(struct ufs_hba *hba, bool enable); |
| 234 | static int ufshcd_wb_toggle_flush_during_h8(struct ufs_hba *hba, bool set); |
| 235 | static inline void ufshcd_wb_toggle_flush(struct ufs_hba *hba, bool enable); |
| 236 | |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 237 | static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag) |
| 238 | { |
| 239 | return tag >= 0 && tag < hba->nutrs; |
| 240 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 241 | |
Can Guo | 5231d38 | 2019-12-05 02:14:46 +0000 | [diff] [blame] | 242 | static inline void ufshcd_enable_irq(struct ufs_hba *hba) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 243 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 244 | if (!hba->is_irq_enabled) { |
Can Guo | 5231d38 | 2019-12-05 02:14:46 +0000 | [diff] [blame] | 245 | enable_irq(hba->irq); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 246 | hba->is_irq_enabled = true; |
| 247 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | static inline void ufshcd_disable_irq(struct ufs_hba *hba) |
| 251 | { |
| 252 | if (hba->is_irq_enabled) { |
Can Guo | 5231d38 | 2019-12-05 02:14:46 +0000 | [diff] [blame] | 253 | disable_irq(hba->irq); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 254 | hba->is_irq_enabled = false; |
| 255 | } |
| 256 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 257 | |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 258 | static inline void ufshcd_wb_config(struct ufs_hba *hba) |
| 259 | { |
| 260 | int ret; |
| 261 | |
Stanley Chu | 79e3520 | 2020-05-08 16:01:15 +0800 | [diff] [blame] | 262 | if (!ufshcd_is_wb_allowed(hba)) |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 263 | return; |
| 264 | |
| 265 | ret = ufshcd_wb_ctrl(hba, true); |
| 266 | if (ret) |
| 267 | dev_err(hba->dev, "%s: Enable WB failed: %d\n", __func__, ret); |
| 268 | else |
| 269 | dev_info(hba->dev, "%s: Write Booster Configured\n", __func__); |
| 270 | ret = ufshcd_wb_toggle_flush_during_h8(hba, true); |
| 271 | if (ret) |
| 272 | dev_err(hba->dev, "%s: En WB flush during H8: failed: %d\n", |
| 273 | __func__, ret); |
| 274 | ufshcd_wb_toggle_flush(hba, true); |
| 275 | } |
| 276 | |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 277 | static void ufshcd_scsi_unblock_requests(struct ufs_hba *hba) |
| 278 | { |
| 279 | if (atomic_dec_and_test(&hba->scsi_block_reqs_cnt)) |
| 280 | scsi_unblock_requests(hba->host); |
| 281 | } |
| 282 | |
| 283 | static void ufshcd_scsi_block_requests(struct ufs_hba *hba) |
| 284 | { |
| 285 | if (atomic_inc_return(&hba->scsi_block_reqs_cnt) == 1) |
| 286 | scsi_block_requests(hba->host); |
| 287 | } |
| 288 | |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 289 | static void ufshcd_add_cmd_upiu_trace(struct ufs_hba *hba, unsigned int tag, |
| 290 | const char *str) |
| 291 | { |
| 292 | struct utp_upiu_req *rq = hba->lrb[tag].ucd_req_ptr; |
| 293 | |
| 294 | trace_ufshcd_upiu(dev_name(hba->dev), str, &rq->header, &rq->sc.cdb); |
| 295 | } |
| 296 | |
| 297 | static void ufshcd_add_query_upiu_trace(struct ufs_hba *hba, unsigned int tag, |
| 298 | const char *str) |
| 299 | { |
| 300 | struct utp_upiu_req *rq = hba->lrb[tag].ucd_req_ptr; |
| 301 | |
| 302 | trace_ufshcd_upiu(dev_name(hba->dev), str, &rq->header, &rq->qr); |
| 303 | } |
| 304 | |
| 305 | static void ufshcd_add_tm_upiu_trace(struct ufs_hba *hba, unsigned int tag, |
| 306 | const char *str) |
| 307 | { |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 308 | int off = (int)tag - hba->nutrs; |
Christoph Hellwig | 391e388 | 2018-10-07 17:30:32 +0300 | [diff] [blame] | 309 | struct utp_task_req_desc *descp = &hba->utmrdl_base_addr[off]; |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 310 | |
Christoph Hellwig | 391e388 | 2018-10-07 17:30:32 +0300 | [diff] [blame] | 311 | trace_ufshcd_upiu(dev_name(hba->dev), str, &descp->req_header, |
| 312 | &descp->input_param1); |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 313 | } |
| 314 | |
Stanley Chu | aa5c697 | 2020-06-15 15:22:35 +0800 | [diff] [blame] | 315 | static void ufshcd_add_uic_command_trace(struct ufs_hba *hba, |
| 316 | struct uic_command *ucmd, |
| 317 | const char *str) |
| 318 | { |
| 319 | u32 cmd; |
| 320 | |
| 321 | if (!trace_ufshcd_uic_command_enabled()) |
| 322 | return; |
| 323 | |
| 324 | if (!strcmp(str, "send")) |
| 325 | cmd = ucmd->command; |
| 326 | else |
| 327 | cmd = ufshcd_readl(hba, REG_UIC_COMMAND); |
| 328 | |
| 329 | trace_ufshcd_uic_command(dev_name(hba->dev), str, cmd, |
| 330 | ufshcd_readl(hba, REG_UIC_COMMAND_ARG_1), |
| 331 | ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2), |
| 332 | ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3)); |
| 333 | } |
| 334 | |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 335 | static void ufshcd_add_command_trace(struct ufs_hba *hba, |
| 336 | unsigned int tag, const char *str) |
| 337 | { |
| 338 | sector_t lba = -1; |
| 339 | u8 opcode = 0; |
| 340 | u32 intr, doorbell; |
Ohad Sharabi | e7c3b37 | 2018-08-05 16:26:23 +0300 | [diff] [blame] | 341 | struct ufshcd_lrb *lrbp = &hba->lrb[tag]; |
Bart Van Assche | e4d2add | 2019-12-24 14:02:44 -0800 | [diff] [blame] | 342 | struct scsi_cmnd *cmd = lrbp->cmd; |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 343 | int transfer_len = -1; |
| 344 | |
Ohad Sharabi | e7c3b37 | 2018-08-05 16:26:23 +0300 | [diff] [blame] | 345 | if (!trace_ufshcd_command_enabled()) { |
| 346 | /* trace UPIU W/O tracing command */ |
Bart Van Assche | e4d2add | 2019-12-24 14:02:44 -0800 | [diff] [blame] | 347 | if (cmd) |
Ohad Sharabi | e7c3b37 | 2018-08-05 16:26:23 +0300 | [diff] [blame] | 348 | ufshcd_add_cmd_upiu_trace(hba, tag, str); |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 349 | return; |
Ohad Sharabi | e7c3b37 | 2018-08-05 16:26:23 +0300 | [diff] [blame] | 350 | } |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 351 | |
Bart Van Assche | e4d2add | 2019-12-24 14:02:44 -0800 | [diff] [blame] | 352 | if (cmd) { /* data phase exists */ |
Ohad Sharabi | e7c3b37 | 2018-08-05 16:26:23 +0300 | [diff] [blame] | 353 | /* trace UPIU also */ |
| 354 | ufshcd_add_cmd_upiu_trace(hba, tag, str); |
Bart Van Assche | e4d2add | 2019-12-24 14:02:44 -0800 | [diff] [blame] | 355 | opcode = cmd->cmnd[0]; |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 356 | if ((opcode == READ_10) || (opcode == WRITE_10)) { |
| 357 | /* |
| 358 | * Currently we only fully trace read(10) and write(10) |
| 359 | * commands |
| 360 | */ |
Bart Van Assche | e4d2add | 2019-12-24 14:02:44 -0800 | [diff] [blame] | 361 | if (cmd->request && cmd->request->bio) |
| 362 | lba = cmd->request->bio->bi_iter.bi_sector; |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 363 | transfer_len = be32_to_cpu( |
| 364 | lrbp->ucd_req_ptr->sc.exp_data_transfer_len); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | intr = ufshcd_readl(hba, REG_INTERRUPT_STATUS); |
| 369 | doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 370 | trace_ufshcd_command(dev_name(hba->dev), str, tag, |
| 371 | doorbell, transfer_len, intr, lba, opcode); |
| 372 | } |
| 373 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 374 | static void ufshcd_print_clk_freqs(struct ufs_hba *hba) |
| 375 | { |
| 376 | struct ufs_clk_info *clki; |
| 377 | struct list_head *head = &hba->clk_list_head; |
| 378 | |
Szymon Mielczarek | 566ec9a | 2017-06-05 11:36:54 +0300 | [diff] [blame] | 379 | if (list_empty(head)) |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 380 | return; |
| 381 | |
| 382 | list_for_each_entry(clki, head, list) { |
| 383 | if (!IS_ERR_OR_NULL(clki->clk) && clki->min_freq && |
| 384 | clki->max_freq) |
| 385 | dev_err(hba->dev, "clk: %s, rate: %u\n", |
| 386 | clki->name, clki->curr_freq); |
| 387 | } |
| 388 | } |
| 389 | |
Stanley Chu | 48d5b97 | 2019-07-10 21:38:18 +0800 | [diff] [blame] | 390 | static void ufshcd_print_err_hist(struct ufs_hba *hba, |
| 391 | struct ufs_err_reg_hist *err_hist, |
| 392 | char *err_name) |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 393 | { |
| 394 | int i; |
Stanley Chu | 2775264 | 2019-01-28 22:04:26 +0800 | [diff] [blame] | 395 | bool found = false; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 396 | |
Stanley Chu | 48d5b97 | 2019-07-10 21:38:18 +0800 | [diff] [blame] | 397 | for (i = 0; i < UFS_ERR_REG_HIST_LENGTH; i++) { |
| 398 | int p = (i + err_hist->pos) % UFS_ERR_REG_HIST_LENGTH; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 399 | |
Stanley Chu | 645728a | 2020-01-04 22:26:06 +0800 | [diff] [blame] | 400 | if (err_hist->tstamp[p] == 0) |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 401 | continue; |
Stanley Chu | c5397f1 | 2019-07-10 21:38:20 +0800 | [diff] [blame] | 402 | dev_err(hba->dev, "%s[%d] = 0x%x at %lld us\n", err_name, p, |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 403 | err_hist->reg[p], ktime_to_us(err_hist->tstamp[p])); |
Stanley Chu | 2775264 | 2019-01-28 22:04:26 +0800 | [diff] [blame] | 404 | found = true; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 405 | } |
Stanley Chu | 2775264 | 2019-01-28 22:04:26 +0800 | [diff] [blame] | 406 | |
| 407 | if (!found) |
Stanley Chu | fd1fb4d | 2020-01-04 22:26:08 +0800 | [diff] [blame] | 408 | dev_err(hba->dev, "No record of %s\n", err_name); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 409 | } |
| 410 | |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 411 | static void ufshcd_print_host_regs(struct ufs_hba *hba) |
| 412 | { |
Tomas Winkler | ba80917 | 2018-06-14 11:14:09 +0300 | [diff] [blame] | 413 | ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: "); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 414 | dev_err(hba->dev, "hba->ufs_version = 0x%x, hba->capabilities = 0x%x\n", |
| 415 | hba->ufs_version, hba->capabilities); |
| 416 | dev_err(hba->dev, |
| 417 | "hba->outstanding_reqs = 0x%x, hba->outstanding_tasks = 0x%x\n", |
| 418 | (u32)hba->outstanding_reqs, (u32)hba->outstanding_tasks); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 419 | dev_err(hba->dev, |
| 420 | "last_hibern8_exit_tstamp at %lld us, hibern8_exit_cnt = %d\n", |
| 421 | ktime_to_us(hba->ufs_stats.last_hibern8_exit_tstamp), |
| 422 | hba->ufs_stats.hibern8_exit_cnt); |
| 423 | |
Stanley Chu | 48d5b97 | 2019-07-10 21:38:18 +0800 | [diff] [blame] | 424 | ufshcd_print_err_hist(hba, &hba->ufs_stats.pa_err, "pa_err"); |
| 425 | ufshcd_print_err_hist(hba, &hba->ufs_stats.dl_err, "dl_err"); |
| 426 | ufshcd_print_err_hist(hba, &hba->ufs_stats.nl_err, "nl_err"); |
| 427 | ufshcd_print_err_hist(hba, &hba->ufs_stats.tl_err, "tl_err"); |
| 428 | ufshcd_print_err_hist(hba, &hba->ufs_stats.dme_err, "dme_err"); |
Stanley Chu | d3c615b | 2019-07-10 21:38:19 +0800 | [diff] [blame] | 429 | ufshcd_print_err_hist(hba, &hba->ufs_stats.auto_hibern8_err, |
| 430 | "auto_hibern8_err"); |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 431 | ufshcd_print_err_hist(hba, &hba->ufs_stats.fatal_err, "fatal_err"); |
| 432 | ufshcd_print_err_hist(hba, &hba->ufs_stats.link_startup_err, |
| 433 | "link_startup_fail"); |
| 434 | ufshcd_print_err_hist(hba, &hba->ufs_stats.resume_err, "resume_fail"); |
| 435 | ufshcd_print_err_hist(hba, &hba->ufs_stats.suspend_err, |
| 436 | "suspend_fail"); |
| 437 | ufshcd_print_err_hist(hba, &hba->ufs_stats.dev_reset, "dev_reset"); |
| 438 | ufshcd_print_err_hist(hba, &hba->ufs_stats.host_reset, "host_reset"); |
| 439 | ufshcd_print_err_hist(hba, &hba->ufs_stats.task_abort, "task_abort"); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 440 | |
| 441 | ufshcd_print_clk_freqs(hba); |
| 442 | |
Stanley Chu | 7c486d91 | 2019-12-24 21:01:06 +0800 | [diff] [blame] | 443 | ufshcd_vops_dbg_register_dump(hba); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | static |
| 447 | void ufshcd_print_trs(struct ufs_hba *hba, unsigned long bitmap, bool pr_prdt) |
| 448 | { |
| 449 | struct ufshcd_lrb *lrbp; |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 450 | int prdt_length; |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 451 | int tag; |
| 452 | |
| 453 | for_each_set_bit(tag, &bitmap, hba->nutrs) { |
| 454 | lrbp = &hba->lrb[tag]; |
| 455 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 456 | dev_err(hba->dev, "UPIU[%d] - issue time %lld us\n", |
| 457 | tag, ktime_to_us(lrbp->issue_time_stamp)); |
Zang Leigang | 0901718 | 2017-09-27 10:06:06 +0800 | [diff] [blame] | 458 | dev_err(hba->dev, "UPIU[%d] - complete time %lld us\n", |
| 459 | tag, ktime_to_us(lrbp->compl_time_stamp)); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 460 | dev_err(hba->dev, |
| 461 | "UPIU[%d] - Transfer Request Descriptor phys@0x%llx\n", |
| 462 | tag, (u64)lrbp->utrd_dma_addr); |
| 463 | |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 464 | ufshcd_hex_dump("UPIU TRD: ", lrbp->utr_descriptor_ptr, |
| 465 | sizeof(struct utp_transfer_req_desc)); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 466 | dev_err(hba->dev, "UPIU[%d] - Request UPIU phys@0x%llx\n", tag, |
| 467 | (u64)lrbp->ucd_req_dma_addr); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 468 | ufshcd_hex_dump("UPIU REQ: ", lrbp->ucd_req_ptr, |
| 469 | sizeof(struct utp_upiu_req)); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 470 | dev_err(hba->dev, "UPIU[%d] - Response UPIU phys@0x%llx\n", tag, |
| 471 | (u64)lrbp->ucd_rsp_dma_addr); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 472 | ufshcd_hex_dump("UPIU RSP: ", lrbp->ucd_rsp_ptr, |
| 473 | sizeof(struct utp_upiu_rsp)); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 474 | |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 475 | prdt_length = le16_to_cpu( |
| 476 | lrbp->utr_descriptor_ptr->prd_table_length); |
| 477 | dev_err(hba->dev, |
| 478 | "UPIU[%d] - PRDT - %d entries phys@0x%llx\n", |
| 479 | tag, prdt_length, |
| 480 | (u64)lrbp->ucd_prdt_dma_addr); |
| 481 | |
| 482 | if (pr_prdt) |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 483 | ufshcd_hex_dump("UPIU PRDT: ", lrbp->ucd_prdt_ptr, |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 484 | sizeof(struct ufshcd_sg_entry) * prdt_length); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
| 488 | static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap) |
| 489 | { |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 490 | int tag; |
| 491 | |
| 492 | for_each_set_bit(tag, &bitmap, hba->nutmrs) { |
Christoph Hellwig | 391e388 | 2018-10-07 17:30:32 +0300 | [diff] [blame] | 493 | struct utp_task_req_desc *tmrdp = &hba->utmrdl_base_addr[tag]; |
| 494 | |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 495 | dev_err(hba->dev, "TM[%d] - Task Management Header\n", tag); |
Christoph Hellwig | 391e388 | 2018-10-07 17:30:32 +0300 | [diff] [blame] | 496 | ufshcd_hex_dump("", tmrdp, sizeof(*tmrdp)); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
Gilad Broner | 6ba6558 | 2017-02-03 16:57:28 -0800 | [diff] [blame] | 500 | static void ufshcd_print_host_state(struct ufs_hba *hba) |
| 501 | { |
| 502 | dev_err(hba->dev, "UFS Host state=%d\n", hba->ufshcd_state); |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 503 | dev_err(hba->dev, "outstanding reqs=0x%lx tasks=0x%lx\n", |
| 504 | hba->outstanding_reqs, hba->outstanding_tasks); |
Gilad Broner | 6ba6558 | 2017-02-03 16:57:28 -0800 | [diff] [blame] | 505 | dev_err(hba->dev, "saved_err=0x%x, saved_uic_err=0x%x\n", |
| 506 | hba->saved_err, hba->saved_uic_err); |
| 507 | dev_err(hba->dev, "Device power mode=%d, UIC link state=%d\n", |
| 508 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
| 509 | dev_err(hba->dev, "PM in progress=%d, sys. suspended=%d\n", |
| 510 | hba->pm_op_in_progress, hba->is_sys_suspended); |
| 511 | dev_err(hba->dev, "Auto BKOPS=%d, Host self-block=%d\n", |
| 512 | hba->auto_bkops_enabled, hba->host->host_self_blocked); |
| 513 | dev_err(hba->dev, "Clk gate=%d\n", hba->clk_gating.state); |
| 514 | dev_err(hba->dev, "error handling flags=0x%x, req. abort count=%d\n", |
| 515 | hba->eh_flags, hba->req_abort_count); |
| 516 | dev_err(hba->dev, "Host capabilities=0x%x, caps=0x%x\n", |
| 517 | hba->capabilities, hba->caps); |
| 518 | dev_err(hba->dev, "quirks=0x%x, dev. quirks=0x%x\n", hba->quirks, |
| 519 | hba->dev_quirks); |
| 520 | } |
| 521 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 522 | /** |
| 523 | * ufshcd_print_pwr_info - print power params as saved in hba |
| 524 | * power info |
| 525 | * @hba: per-adapter instance |
| 526 | */ |
| 527 | static void ufshcd_print_pwr_info(struct ufs_hba *hba) |
| 528 | { |
| 529 | static const char * const names[] = { |
| 530 | "INVALID MODE", |
| 531 | "FAST MODE", |
| 532 | "SLOW_MODE", |
| 533 | "INVALID MODE", |
| 534 | "FASTAUTO_MODE", |
| 535 | "SLOWAUTO_MODE", |
| 536 | "INVALID MODE", |
| 537 | }; |
| 538 | |
| 539 | dev_err(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n", |
| 540 | __func__, |
| 541 | hba->pwr_info.gear_rx, hba->pwr_info.gear_tx, |
| 542 | hba->pwr_info.lane_rx, hba->pwr_info.lane_tx, |
| 543 | names[hba->pwr_info.pwr_rx], |
| 544 | names[hba->pwr_info.pwr_tx], |
| 545 | hba->pwr_info.hs_rate); |
| 546 | } |
| 547 | |
Stanley Chu | 5c955c1 | 2020-03-18 18:40:12 +0800 | [diff] [blame] | 548 | void ufshcd_delay_us(unsigned long us, unsigned long tolerance) |
| 549 | { |
| 550 | if (!us) |
| 551 | return; |
| 552 | |
| 553 | if (us < 10) |
| 554 | udelay(us); |
| 555 | else |
| 556 | usleep_range(us, us + tolerance); |
| 557 | } |
| 558 | EXPORT_SYMBOL_GPL(ufshcd_delay_us); |
| 559 | |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 560 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 561 | * ufshcd_wait_for_register - wait for register value to change |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 562 | * @hba: per-adapter interface |
| 563 | * @reg: mmio register offset |
| 564 | * @mask: mask to apply to the read register value |
| 565 | * @val: value to wait for |
| 566 | * @interval_us: polling interval in microseconds |
| 567 | * @timeout_ms: timeout in milliseconds |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 568 | * |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 569 | * Return: |
| 570 | * -ETIMEDOUT on error, zero on success. |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 571 | */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 572 | int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask, |
| 573 | u32 val, unsigned long interval_us, |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 574 | unsigned long timeout_ms) |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 575 | { |
| 576 | int err = 0; |
| 577 | unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms); |
| 578 | |
| 579 | /* ignore bits that we don't intend to wait on */ |
| 580 | val = val & mask; |
| 581 | |
| 582 | while ((ufshcd_readl(hba, reg) & mask) != val) { |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 583 | usleep_range(interval_us, interval_us + 50); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 584 | if (time_after(jiffies, timeout)) { |
| 585 | if ((ufshcd_readl(hba, reg) & mask) != val) |
| 586 | err = -ETIMEDOUT; |
| 587 | break; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | return err; |
| 592 | } |
| 593 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 594 | /** |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 595 | * ufshcd_get_intr_mask - Get the interrupt bit mask |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 596 | * @hba: Pointer to adapter instance |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 597 | * |
| 598 | * Returns interrupt bit mask per version |
| 599 | */ |
| 600 | static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba) |
| 601 | { |
Yaniv Gardi | c01848c | 2016-12-05 19:25:02 -0800 | [diff] [blame] | 602 | u32 intr_mask = 0; |
| 603 | |
| 604 | switch (hba->ufs_version) { |
| 605 | case UFSHCI_VERSION_10: |
| 606 | intr_mask = INTERRUPT_MASK_ALL_VER_10; |
| 607 | break; |
Yaniv Gardi | c01848c | 2016-12-05 19:25:02 -0800 | [diff] [blame] | 608 | case UFSHCI_VERSION_11: |
| 609 | case UFSHCI_VERSION_20: |
| 610 | intr_mask = INTERRUPT_MASK_ALL_VER_11; |
| 611 | break; |
Yaniv Gardi | c01848c | 2016-12-05 19:25:02 -0800 | [diff] [blame] | 612 | case UFSHCI_VERSION_21: |
| 613 | default: |
| 614 | intr_mask = INTERRUPT_MASK_ALL_VER_21; |
Tomohiro Kusumi | 031d1e0 | 2017-03-23 12:49:04 +0200 | [diff] [blame] | 615 | break; |
Yaniv Gardi | c01848c | 2016-12-05 19:25:02 -0800 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | return intr_mask; |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 622 | * ufshcd_get_ufs_version - Get the UFS version supported by the HBA |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 623 | * @hba: Pointer to adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 624 | * |
| 625 | * Returns UFSHCI version supported by the controller |
| 626 | */ |
| 627 | static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba) |
| 628 | { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 629 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION) |
| 630 | return ufshcd_vops_get_ufs_hci_version(hba); |
Yaniv Gardi | 9949e70 | 2015-05-17 18:55:05 +0300 | [diff] [blame] | 631 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 632 | return ufshcd_readl(hba, REG_UFS_VERSION); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /** |
| 636 | * ufshcd_is_device_present - Check if any device connected to |
| 637 | * the host controller |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 638 | * @hba: pointer to adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 639 | * |
Tomohiro Kusumi | c9e6010 | 2017-03-28 16:49:24 +0300 | [diff] [blame] | 640 | * Returns true if device present, false if no device detected |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 641 | */ |
Tomohiro Kusumi | c9e6010 | 2017-03-28 16:49:24 +0300 | [diff] [blame] | 642 | static inline bool ufshcd_is_device_present(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 643 | { |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 644 | return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & |
Tomohiro Kusumi | c9e6010 | 2017-03-28 16:49:24 +0300 | [diff] [blame] | 645 | DEVICE_PRESENT) ? true : false; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | /** |
| 649 | * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 650 | * @lrbp: pointer to local command reference block |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 651 | * |
| 652 | * This function is used to get the OCS field from UTRD |
| 653 | * Returns the OCS field in the UTRD |
| 654 | */ |
| 655 | static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp) |
| 656 | { |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 657 | return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 661 | * ufshcd_utrl_clear - Clear a bit in UTRLCLR register |
| 662 | * @hba: per adapter instance |
| 663 | * @pos: position of the bit to be cleared |
| 664 | */ |
| 665 | static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos) |
| 666 | { |
Alim Akhtar | 8718384 | 2020-05-28 06:46:49 +0530 | [diff] [blame] | 667 | if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR) |
| 668 | ufshcd_writel(hba, (1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR); |
| 669 | else |
| 670 | ufshcd_writel(hba, ~(1 << pos), |
| 671 | REG_UTP_TRANSFER_REQ_LIST_CLEAR); |
Alim Akhtar | 1399c5b | 2018-05-06 15:44:15 +0530 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | /** |
| 675 | * ufshcd_utmrl_clear - Clear a bit in UTRMLCLR register |
| 676 | * @hba: per adapter instance |
| 677 | * @pos: position of the bit to be cleared |
| 678 | */ |
| 679 | static inline void ufshcd_utmrl_clear(struct ufs_hba *hba, u32 pos) |
| 680 | { |
Alim Akhtar | 8718384 | 2020-05-28 06:46:49 +0530 | [diff] [blame] | 681 | if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR) |
| 682 | ufshcd_writel(hba, (1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR); |
| 683 | else |
| 684 | ufshcd_writel(hba, ~(1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | /** |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 688 | * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field |
| 689 | * @hba: per adapter instance |
| 690 | * @tag: position of the bit to be cleared |
| 691 | */ |
| 692 | static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag) |
| 693 | { |
| 694 | __clear_bit(tag, &hba->outstanding_reqs); |
| 695 | } |
| 696 | |
| 697 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 698 | * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY |
| 699 | * @reg: Register value of host controller status |
| 700 | * |
| 701 | * Returns integer, 0 on Success and positive value if failed |
| 702 | */ |
| 703 | static inline int ufshcd_get_lists_status(u32 reg) |
| 704 | { |
Tomohiro Kusumi | 6cf1611 | 2017-04-26 20:28:58 +0300 | [diff] [blame] | 705 | return !((reg & UFSHCD_STATUS_READY) == UFSHCD_STATUS_READY); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | /** |
| 709 | * ufshcd_get_uic_cmd_result - Get the UIC command result |
| 710 | * @hba: Pointer to adapter instance |
| 711 | * |
| 712 | * This function gets the result of UIC command completion |
| 713 | * Returns 0 on success, non zero value on error |
| 714 | */ |
| 715 | static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba) |
| 716 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 717 | return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) & |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 718 | MASK_UIC_COMMAND_RESULT; |
| 719 | } |
| 720 | |
| 721 | /** |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 722 | * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command |
| 723 | * @hba: Pointer to adapter instance |
| 724 | * |
| 725 | * This function gets UIC command argument3 |
| 726 | * Returns 0 on success, non zero value on error |
| 727 | */ |
| 728 | static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba) |
| 729 | { |
| 730 | return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3); |
| 731 | } |
| 732 | |
| 733 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 734 | * ufshcd_get_req_rsp - returns the TR response transaction type |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 735 | * @ucd_rsp_ptr: pointer to response UPIU |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 736 | */ |
| 737 | static inline int |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 738 | ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 739 | { |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 740 | return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | /** |
| 744 | * ufshcd_get_rsp_upiu_result - Get the result from response UPIU |
| 745 | * @ucd_rsp_ptr: pointer to response UPIU |
| 746 | * |
| 747 | * This function gets the response status and scsi_status from response UPIU |
| 748 | * Returns the response result code. |
| 749 | */ |
| 750 | static inline int |
| 751 | ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 752 | { |
| 753 | return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT; |
| 754 | } |
| 755 | |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 756 | /* |
| 757 | * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length |
| 758 | * from response UPIU |
| 759 | * @ucd_rsp_ptr: pointer to response UPIU |
| 760 | * |
| 761 | * Return the data segment length. |
| 762 | */ |
| 763 | static inline unsigned int |
| 764 | ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 765 | { |
| 766 | return be32_to_cpu(ucd_rsp_ptr->header.dword_2) & |
| 767 | MASK_RSP_UPIU_DATA_SEG_LEN; |
| 768 | } |
| 769 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 770 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 771 | * ufshcd_is_exception_event - Check if the device raised an exception event |
| 772 | * @ucd_rsp_ptr: pointer to response UPIU |
| 773 | * |
| 774 | * The function checks if the device raised an exception event indicated in |
| 775 | * the Device Information field of response UPIU. |
| 776 | * |
| 777 | * Returns true if exception is raised, false otherwise. |
| 778 | */ |
| 779 | static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 780 | { |
| 781 | return be32_to_cpu(ucd_rsp_ptr->header.dword_2) & |
| 782 | MASK_RSP_EXCEPTION_EVENT ? true : false; |
| 783 | } |
| 784 | |
| 785 | /** |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 786 | * ufshcd_reset_intr_aggr - Reset interrupt aggregation values. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 787 | * @hba: per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 788 | */ |
| 789 | static inline void |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 790 | ufshcd_reset_intr_aggr(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 791 | { |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 792 | ufshcd_writel(hba, INT_AGGR_ENABLE | |
| 793 | INT_AGGR_COUNTER_AND_TIMER_RESET, |
| 794 | REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
| 795 | } |
| 796 | |
| 797 | /** |
| 798 | * ufshcd_config_intr_aggr - Configure interrupt aggregation values. |
| 799 | * @hba: per adapter instance |
| 800 | * @cnt: Interrupt aggregation counter threshold |
| 801 | * @tmout: Interrupt aggregation timeout value |
| 802 | */ |
| 803 | static inline void |
| 804 | ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout) |
| 805 | { |
| 806 | ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE | |
| 807 | INT_AGGR_COUNTER_THLD_VAL(cnt) | |
| 808 | INT_AGGR_TIMEOUT_VAL(tmout), |
| 809 | REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | /** |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 813 | * ufshcd_disable_intr_aggr - Disables interrupt aggregation. |
| 814 | * @hba: per adapter instance |
| 815 | */ |
| 816 | static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba) |
| 817 | { |
| 818 | ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
| 819 | } |
| 820 | |
| 821 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 822 | * ufshcd_enable_run_stop_reg - Enable run-stop registers, |
| 823 | * When run-stop registers are set to 1, it indicates the |
| 824 | * host controller that it can process the requests |
| 825 | * @hba: per adapter instance |
| 826 | */ |
| 827 | static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba) |
| 828 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 829 | ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT, |
| 830 | REG_UTP_TASK_REQ_LIST_RUN_STOP); |
| 831 | ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT, |
| 832 | REG_UTP_TRANSFER_REQ_LIST_RUN_STOP); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 836 | * ufshcd_hba_start - Start controller initialization sequence |
| 837 | * @hba: per adapter instance |
| 838 | */ |
| 839 | static inline void ufshcd_hba_start(struct ufs_hba *hba) |
| 840 | { |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 841 | u32 val = CONTROLLER_ENABLE; |
| 842 | |
| 843 | if (ufshcd_crypto_enable(hba)) |
| 844 | val |= CRYPTO_GENERAL_ENABLE; |
| 845 | |
| 846 | ufshcd_writel(hba, val, REG_CONTROLLER_ENABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | /** |
| 850 | * ufshcd_is_hba_active - Get controller state |
| 851 | * @hba: per adapter instance |
| 852 | * |
Tomohiro Kusumi | c9e6010 | 2017-03-28 16:49:24 +0300 | [diff] [blame] | 853 | * Returns false if controller is active, true otherwise |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 854 | */ |
Tomohiro Kusumi | c9e6010 | 2017-03-28 16:49:24 +0300 | [diff] [blame] | 855 | static inline bool ufshcd_is_hba_active(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 856 | { |
Tomohiro Kusumi | 4a8eec2 | 2017-03-28 16:49:25 +0300 | [diff] [blame] | 857 | return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & CONTROLLER_ENABLE) |
| 858 | ? false : true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 859 | } |
| 860 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 861 | u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba) |
| 862 | { |
| 863 | /* HCI version 1.0 and 1.1 supports UniPro 1.41 */ |
| 864 | if ((hba->ufs_version == UFSHCI_VERSION_10) || |
| 865 | (hba->ufs_version == UFSHCI_VERSION_11)) |
| 866 | return UFS_UNIPRO_VER_1_41; |
| 867 | else |
| 868 | return UFS_UNIPRO_VER_1_6; |
| 869 | } |
| 870 | EXPORT_SYMBOL(ufshcd_get_local_unipro_ver); |
| 871 | |
| 872 | static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba) |
| 873 | { |
| 874 | /* |
| 875 | * If both host and device support UniPro ver1.6 or later, PA layer |
| 876 | * parameters tuning happens during link startup itself. |
| 877 | * |
| 878 | * We can manually tune PA layer parameters if either host or device |
| 879 | * doesn't support UniPro ver 1.6 or later. But to keep manual tuning |
| 880 | * logic simple, we will only do manual tuning if local unipro version |
| 881 | * doesn't support ver1.6 or later. |
| 882 | */ |
| 883 | if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6) |
| 884 | return true; |
| 885 | else |
| 886 | return false; |
| 887 | } |
| 888 | |
Subhash Jadavani | 394b949 | 2020-03-26 02:25:40 -0700 | [diff] [blame] | 889 | /** |
| 890 | * ufshcd_set_clk_freq - set UFS controller clock frequencies |
| 891 | * @hba: per adapter instance |
| 892 | * @scale_up: If True, set max possible frequency othewise set low frequency |
| 893 | * |
| 894 | * Returns 0 if successful |
| 895 | * Returns < 0 for any other errors |
| 896 | */ |
| 897 | static int ufshcd_set_clk_freq(struct ufs_hba *hba, bool scale_up) |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 898 | { |
| 899 | int ret = 0; |
| 900 | struct ufs_clk_info *clki; |
| 901 | struct list_head *head = &hba->clk_list_head; |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 902 | |
Szymon Mielczarek | 566ec9a | 2017-06-05 11:36:54 +0300 | [diff] [blame] | 903 | if (list_empty(head)) |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 904 | goto out; |
| 905 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 906 | list_for_each_entry(clki, head, list) { |
| 907 | if (!IS_ERR_OR_NULL(clki->clk)) { |
| 908 | if (scale_up && clki->max_freq) { |
| 909 | if (clki->curr_freq == clki->max_freq) |
| 910 | continue; |
| 911 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 912 | ret = clk_set_rate(clki->clk, clki->max_freq); |
| 913 | if (ret) { |
| 914 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 915 | __func__, clki->name, |
| 916 | clki->max_freq, ret); |
| 917 | break; |
| 918 | } |
| 919 | trace_ufshcd_clk_scaling(dev_name(hba->dev), |
| 920 | "scaled up", clki->name, |
| 921 | clki->curr_freq, |
| 922 | clki->max_freq); |
| 923 | |
| 924 | clki->curr_freq = clki->max_freq; |
| 925 | |
| 926 | } else if (!scale_up && clki->min_freq) { |
| 927 | if (clki->curr_freq == clki->min_freq) |
| 928 | continue; |
| 929 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 930 | ret = clk_set_rate(clki->clk, clki->min_freq); |
| 931 | if (ret) { |
| 932 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 933 | __func__, clki->name, |
| 934 | clki->min_freq, ret); |
| 935 | break; |
| 936 | } |
| 937 | trace_ufshcd_clk_scaling(dev_name(hba->dev), |
| 938 | "scaled down", clki->name, |
| 939 | clki->curr_freq, |
| 940 | clki->min_freq); |
| 941 | clki->curr_freq = clki->min_freq; |
| 942 | } |
| 943 | } |
| 944 | dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__, |
| 945 | clki->name, clk_get_rate(clki->clk)); |
| 946 | } |
| 947 | |
Subhash Jadavani | 394b949 | 2020-03-26 02:25:40 -0700 | [diff] [blame] | 948 | out: |
| 949 | return ret; |
| 950 | } |
| 951 | |
| 952 | /** |
| 953 | * ufshcd_scale_clks - scale up or scale down UFS controller clocks |
| 954 | * @hba: per adapter instance |
| 955 | * @scale_up: True if scaling up and false if scaling down |
| 956 | * |
| 957 | * Returns 0 if successful |
| 958 | * Returns < 0 for any other errors |
| 959 | */ |
| 960 | static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up) |
| 961 | { |
| 962 | int ret = 0; |
| 963 | ktime_t start = ktime_get(); |
| 964 | |
| 965 | ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE); |
| 966 | if (ret) |
| 967 | goto out; |
| 968 | |
| 969 | ret = ufshcd_set_clk_freq(hba, scale_up); |
| 970 | if (ret) |
| 971 | goto out; |
| 972 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 973 | ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE); |
Subhash Jadavani | 394b949 | 2020-03-26 02:25:40 -0700 | [diff] [blame] | 974 | if (ret) |
| 975 | ufshcd_set_clk_freq(hba, !scale_up); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 976 | |
| 977 | out: |
Subhash Jadavani | 394b949 | 2020-03-26 02:25:40 -0700 | [diff] [blame] | 978 | trace_ufshcd_profile_clk_scaling(dev_name(hba->dev), |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 979 | (scale_up ? "up" : "down"), |
| 980 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
| 981 | return ret; |
| 982 | } |
| 983 | |
| 984 | /** |
| 985 | * ufshcd_is_devfreq_scaling_required - check if scaling is required or not |
| 986 | * @hba: per adapter instance |
| 987 | * @scale_up: True if scaling up and false if scaling down |
| 988 | * |
| 989 | * Returns true if scaling is required, false otherwise. |
| 990 | */ |
| 991 | static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba, |
| 992 | bool scale_up) |
| 993 | { |
| 994 | struct ufs_clk_info *clki; |
| 995 | struct list_head *head = &hba->clk_list_head; |
| 996 | |
Szymon Mielczarek | 566ec9a | 2017-06-05 11:36:54 +0300 | [diff] [blame] | 997 | if (list_empty(head)) |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 998 | return false; |
| 999 | |
| 1000 | list_for_each_entry(clki, head, list) { |
| 1001 | if (!IS_ERR_OR_NULL(clki->clk)) { |
| 1002 | if (scale_up && clki->max_freq) { |
| 1003 | if (clki->curr_freq == clki->max_freq) |
| 1004 | continue; |
| 1005 | return true; |
| 1006 | } else if (!scale_up && clki->min_freq) { |
| 1007 | if (clki->curr_freq == clki->min_freq) |
| 1008 | continue; |
| 1009 | return true; |
| 1010 | } |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | return false; |
| 1015 | } |
| 1016 | |
| 1017 | static int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba, |
| 1018 | u64 wait_timeout_us) |
| 1019 | { |
| 1020 | unsigned long flags; |
| 1021 | int ret = 0; |
| 1022 | u32 tm_doorbell; |
| 1023 | u32 tr_doorbell; |
| 1024 | bool timeout = false, do_last_check = false; |
| 1025 | ktime_t start; |
| 1026 | |
| 1027 | ufshcd_hold(hba, false); |
| 1028 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1029 | /* |
| 1030 | * Wait for all the outstanding tasks/transfer requests. |
| 1031 | * Verify by checking the doorbell registers are clear. |
| 1032 | */ |
| 1033 | start = ktime_get(); |
| 1034 | do { |
| 1035 | if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) { |
| 1036 | ret = -EBUSY; |
| 1037 | goto out; |
| 1038 | } |
| 1039 | |
| 1040 | tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL); |
| 1041 | tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 1042 | if (!tm_doorbell && !tr_doorbell) { |
| 1043 | timeout = false; |
| 1044 | break; |
| 1045 | } else if (do_last_check) { |
| 1046 | break; |
| 1047 | } |
| 1048 | |
| 1049 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1050 | schedule(); |
| 1051 | if (ktime_to_us(ktime_sub(ktime_get(), start)) > |
| 1052 | wait_timeout_us) { |
| 1053 | timeout = true; |
| 1054 | /* |
| 1055 | * We might have scheduled out for long time so make |
| 1056 | * sure to check if doorbells are cleared by this time |
| 1057 | * or not. |
| 1058 | */ |
| 1059 | do_last_check = true; |
| 1060 | } |
| 1061 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1062 | } while (tm_doorbell || tr_doorbell); |
| 1063 | |
| 1064 | if (timeout) { |
| 1065 | dev_err(hba->dev, |
| 1066 | "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n", |
| 1067 | __func__, tm_doorbell, tr_doorbell); |
| 1068 | ret = -EBUSY; |
| 1069 | } |
| 1070 | out: |
| 1071 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1072 | ufshcd_release(hba); |
| 1073 | return ret; |
| 1074 | } |
| 1075 | |
| 1076 | /** |
| 1077 | * ufshcd_scale_gear - scale up/down UFS gear |
| 1078 | * @hba: per adapter instance |
| 1079 | * @scale_up: True for scaling up gear and false for scaling down |
| 1080 | * |
| 1081 | * Returns 0 for success, |
| 1082 | * Returns -EBUSY if scaling can't happen at this time |
| 1083 | * Returns non-zero for any other errors |
| 1084 | */ |
| 1085 | static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up) |
| 1086 | { |
| 1087 | #define UFS_MIN_GEAR_TO_SCALE_DOWN UFS_HS_G1 |
| 1088 | int ret = 0; |
| 1089 | struct ufs_pa_layer_attr new_pwr_info; |
| 1090 | |
| 1091 | if (scale_up) { |
| 1092 | memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info.info, |
| 1093 | sizeof(struct ufs_pa_layer_attr)); |
| 1094 | } else { |
| 1095 | memcpy(&new_pwr_info, &hba->pwr_info, |
| 1096 | sizeof(struct ufs_pa_layer_attr)); |
| 1097 | |
| 1098 | if (hba->pwr_info.gear_tx > UFS_MIN_GEAR_TO_SCALE_DOWN |
| 1099 | || hba->pwr_info.gear_rx > UFS_MIN_GEAR_TO_SCALE_DOWN) { |
| 1100 | /* save the current power mode */ |
| 1101 | memcpy(&hba->clk_scaling.saved_pwr_info.info, |
| 1102 | &hba->pwr_info, |
| 1103 | sizeof(struct ufs_pa_layer_attr)); |
| 1104 | |
| 1105 | /* scale down gear */ |
| 1106 | new_pwr_info.gear_tx = UFS_MIN_GEAR_TO_SCALE_DOWN; |
| 1107 | new_pwr_info.gear_rx = UFS_MIN_GEAR_TO_SCALE_DOWN; |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | /* check if the power mode needs to be changed or not? */ |
Can Guo | 6a9df81 | 2020-02-11 21:38:28 -0800 | [diff] [blame] | 1112 | ret = ufshcd_config_pwr_mode(hba, &new_pwr_info); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1113 | if (ret) |
| 1114 | dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d)", |
| 1115 | __func__, ret, |
| 1116 | hba->pwr_info.gear_tx, hba->pwr_info.gear_rx, |
| 1117 | new_pwr_info.gear_tx, new_pwr_info.gear_rx); |
| 1118 | |
| 1119 | return ret; |
| 1120 | } |
| 1121 | |
| 1122 | static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba) |
| 1123 | { |
| 1124 | #define DOORBELL_CLR_TOUT_US (1000 * 1000) /* 1 sec */ |
| 1125 | int ret = 0; |
| 1126 | /* |
| 1127 | * make sure that there are no outstanding requests when |
| 1128 | * clock scaling is in progress |
| 1129 | */ |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 1130 | ufshcd_scsi_block_requests(hba); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1131 | down_write(&hba->clk_scaling_lock); |
| 1132 | if (ufshcd_wait_for_doorbell_clr(hba, DOORBELL_CLR_TOUT_US)) { |
| 1133 | ret = -EBUSY; |
| 1134 | up_write(&hba->clk_scaling_lock); |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 1135 | ufshcd_scsi_unblock_requests(hba); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | return ret; |
| 1139 | } |
| 1140 | |
| 1141 | static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba) |
| 1142 | { |
| 1143 | up_write(&hba->clk_scaling_lock); |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 1144 | ufshcd_scsi_unblock_requests(hba); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | /** |
| 1148 | * ufshcd_devfreq_scale - scale up/down UFS clocks and gear |
| 1149 | * @hba: per adapter instance |
| 1150 | * @scale_up: True for scaling up and false for scalin down |
| 1151 | * |
| 1152 | * Returns 0 for success, |
| 1153 | * Returns -EBUSY if scaling can't happen at this time |
| 1154 | * Returns non-zero for any other errors |
| 1155 | */ |
| 1156 | static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up) |
| 1157 | { |
| 1158 | int ret = 0; |
| 1159 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1160 | /* let's not get into low power until clock scaling is completed */ |
| 1161 | ufshcd_hold(hba, false); |
| 1162 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1163 | ret = ufshcd_clock_scaling_prepare(hba); |
| 1164 | if (ret) |
Subhash Jadavani | 394b949 | 2020-03-26 02:25:40 -0700 | [diff] [blame] | 1165 | goto out; |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1166 | |
| 1167 | /* scale down the gear before scaling down clocks */ |
| 1168 | if (!scale_up) { |
| 1169 | ret = ufshcd_scale_gear(hba, false); |
| 1170 | if (ret) |
Subhash Jadavani | 394b949 | 2020-03-26 02:25:40 -0700 | [diff] [blame] | 1171 | goto out_unprepare; |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | ret = ufshcd_scale_clks(hba, scale_up); |
| 1175 | if (ret) { |
| 1176 | if (!scale_up) |
| 1177 | ufshcd_scale_gear(hba, true); |
Subhash Jadavani | 394b949 | 2020-03-26 02:25:40 -0700 | [diff] [blame] | 1178 | goto out_unprepare; |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | /* scale up the gear after scaling up clocks */ |
| 1182 | if (scale_up) { |
| 1183 | ret = ufshcd_scale_gear(hba, true); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 1184 | if (ret) { |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1185 | ufshcd_scale_clks(hba, false); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 1186 | goto out_unprepare; |
| 1187 | } |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1188 | } |
| 1189 | |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 1190 | /* Enable Write Booster if we have scaled up else disable it */ |
| 1191 | up_write(&hba->clk_scaling_lock); |
| 1192 | ufshcd_wb_ctrl(hba, scale_up); |
| 1193 | down_write(&hba->clk_scaling_lock); |
| 1194 | |
Subhash Jadavani | 394b949 | 2020-03-26 02:25:40 -0700 | [diff] [blame] | 1195 | out_unprepare: |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1196 | ufshcd_clock_scaling_unprepare(hba); |
Subhash Jadavani | 394b949 | 2020-03-26 02:25:40 -0700 | [diff] [blame] | 1197 | out: |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1198 | ufshcd_release(hba); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1199 | return ret; |
| 1200 | } |
| 1201 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1202 | static void ufshcd_clk_scaling_suspend_work(struct work_struct *work) |
| 1203 | { |
| 1204 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1205 | clk_scaling.suspend_work); |
| 1206 | unsigned long irq_flags; |
| 1207 | |
| 1208 | spin_lock_irqsave(hba->host->host_lock, irq_flags); |
| 1209 | if (hba->clk_scaling.active_reqs || hba->clk_scaling.is_suspended) { |
| 1210 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1211 | return; |
| 1212 | } |
| 1213 | hba->clk_scaling.is_suspended = true; |
| 1214 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1215 | |
| 1216 | __ufshcd_suspend_clkscaling(hba); |
| 1217 | } |
| 1218 | |
| 1219 | static void ufshcd_clk_scaling_resume_work(struct work_struct *work) |
| 1220 | { |
| 1221 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1222 | clk_scaling.resume_work); |
| 1223 | unsigned long irq_flags; |
| 1224 | |
| 1225 | spin_lock_irqsave(hba->host->host_lock, irq_flags); |
| 1226 | if (!hba->clk_scaling.is_suspended) { |
| 1227 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1228 | return; |
| 1229 | } |
| 1230 | hba->clk_scaling.is_suspended = false; |
| 1231 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1232 | |
| 1233 | devfreq_resume_device(hba->devfreq); |
| 1234 | } |
| 1235 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1236 | static int ufshcd_devfreq_target(struct device *dev, |
| 1237 | unsigned long *freq, u32 flags) |
| 1238 | { |
| 1239 | int ret = 0; |
| 1240 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1241 | ktime_t start; |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1242 | bool scale_up, sched_clk_scaling_suspend_work = false; |
Bjorn Andersson | 092b455 | 2018-05-17 23:26:37 -0700 | [diff] [blame] | 1243 | struct list_head *clk_list = &hba->clk_list_head; |
| 1244 | struct ufs_clk_info *clki; |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1245 | unsigned long irq_flags; |
| 1246 | |
| 1247 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 1248 | return -EINVAL; |
| 1249 | |
Asutosh Das | 91831d3 | 2020-03-25 11:29:00 -0700 | [diff] [blame] | 1250 | clki = list_first_entry(&hba->clk_list_head, struct ufs_clk_info, list); |
| 1251 | /* Override with the closest supported frequency */ |
| 1252 | *freq = (unsigned long) clk_round_rate(clki->clk, *freq); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1253 | spin_lock_irqsave(hba->host->host_lock, irq_flags); |
| 1254 | if (ufshcd_eh_in_progress(hba)) { |
| 1255 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1256 | return 0; |
| 1257 | } |
| 1258 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1259 | if (!hba->clk_scaling.active_reqs) |
| 1260 | sched_clk_scaling_suspend_work = true; |
| 1261 | |
Bjorn Andersson | 092b455 | 2018-05-17 23:26:37 -0700 | [diff] [blame] | 1262 | if (list_empty(clk_list)) { |
| 1263 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1264 | goto out; |
| 1265 | } |
| 1266 | |
Asutosh Das | 91831d3 | 2020-03-25 11:29:00 -0700 | [diff] [blame] | 1267 | /* Decide based on the rounded-off frequency and update */ |
Bjorn Andersson | 092b455 | 2018-05-17 23:26:37 -0700 | [diff] [blame] | 1268 | scale_up = (*freq == clki->max_freq) ? true : false; |
Asutosh Das | 91831d3 | 2020-03-25 11:29:00 -0700 | [diff] [blame] | 1269 | if (!scale_up) |
| 1270 | *freq = clki->min_freq; |
| 1271 | /* Update the frequency */ |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1272 | if (!ufshcd_is_devfreq_scaling_required(hba, scale_up)) { |
| 1273 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1274 | ret = 0; |
| 1275 | goto out; /* no state change required */ |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1276 | } |
| 1277 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1278 | |
| 1279 | start = ktime_get(); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1280 | ret = ufshcd_devfreq_scale(hba, scale_up); |
| 1281 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1282 | trace_ufshcd_profile_clk_scaling(dev_name(hba->dev), |
| 1283 | (scale_up ? "up" : "down"), |
| 1284 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
| 1285 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1286 | out: |
| 1287 | if (sched_clk_scaling_suspend_work) |
| 1288 | queue_work(hba->clk_scaling.workq, |
| 1289 | &hba->clk_scaling.suspend_work); |
| 1290 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1291 | return ret; |
| 1292 | } |
| 1293 | |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 1294 | static bool ufshcd_is_busy(struct request *req, void *priv, bool reserved) |
| 1295 | { |
| 1296 | int *busy = priv; |
| 1297 | |
| 1298 | WARN_ON_ONCE(reserved); |
| 1299 | (*busy)++; |
| 1300 | return false; |
| 1301 | } |
| 1302 | |
| 1303 | /* Whether or not any tag is in use by a request that is in progress. */ |
| 1304 | static bool ufshcd_any_tag_in_use(struct ufs_hba *hba) |
| 1305 | { |
| 1306 | struct request_queue *q = hba->cmd_queue; |
| 1307 | int busy = 0; |
| 1308 | |
| 1309 | blk_mq_tagset_busy_iter(q->tag_set, ufshcd_is_busy, &busy); |
| 1310 | return busy; |
| 1311 | } |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1312 | |
| 1313 | static int ufshcd_devfreq_get_dev_status(struct device *dev, |
| 1314 | struct devfreq_dev_status *stat) |
| 1315 | { |
| 1316 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1317 | struct ufs_clk_scaling *scaling = &hba->clk_scaling; |
| 1318 | unsigned long flags; |
Asutosh Das | 91831d3 | 2020-03-25 11:29:00 -0700 | [diff] [blame] | 1319 | struct list_head *clk_list = &hba->clk_list_head; |
| 1320 | struct ufs_clk_info *clki; |
Stanley Chu | b1bf66d | 2020-06-11 18:10:43 +0800 | [diff] [blame] | 1321 | ktime_t curr_t; |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1322 | |
| 1323 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 1324 | return -EINVAL; |
| 1325 | |
| 1326 | memset(stat, 0, sizeof(*stat)); |
| 1327 | |
| 1328 | spin_lock_irqsave(hba->host->host_lock, flags); |
Stanley Chu | b1bf66d | 2020-06-11 18:10:43 +0800 | [diff] [blame] | 1329 | curr_t = ktime_get(); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1330 | if (!scaling->window_start_t) |
| 1331 | goto start_window; |
| 1332 | |
Asutosh Das | 91831d3 | 2020-03-25 11:29:00 -0700 | [diff] [blame] | 1333 | clki = list_first_entry(clk_list, struct ufs_clk_info, list); |
| 1334 | /* |
| 1335 | * If current frequency is 0, then the ondemand governor considers |
| 1336 | * there's no initial frequency set. And it always requests to set |
| 1337 | * to max. frequency. |
| 1338 | */ |
| 1339 | stat->current_frequency = clki->curr_freq; |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1340 | if (scaling->is_busy_started) |
Stanley Chu | b1bf66d | 2020-06-11 18:10:43 +0800 | [diff] [blame] | 1341 | scaling->tot_busy_t += ktime_us_delta(curr_t, |
| 1342 | scaling->busy_start_t); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1343 | |
Stanley Chu | b1bf66d | 2020-06-11 18:10:43 +0800 | [diff] [blame] | 1344 | stat->total_time = ktime_us_delta(curr_t, scaling->window_start_t); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1345 | stat->busy_time = scaling->tot_busy_t; |
| 1346 | start_window: |
Stanley Chu | b1bf66d | 2020-06-11 18:10:43 +0800 | [diff] [blame] | 1347 | scaling->window_start_t = curr_t; |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1348 | scaling->tot_busy_t = 0; |
| 1349 | |
| 1350 | if (hba->outstanding_reqs) { |
Stanley Chu | b1bf66d | 2020-06-11 18:10:43 +0800 | [diff] [blame] | 1351 | scaling->busy_start_t = curr_t; |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1352 | scaling->is_busy_started = true; |
| 1353 | } else { |
| 1354 | scaling->busy_start_t = 0; |
| 1355 | scaling->is_busy_started = false; |
| 1356 | } |
| 1357 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1358 | return 0; |
| 1359 | } |
| 1360 | |
Bjorn Andersson | deac444 | 2018-05-17 23:26:36 -0700 | [diff] [blame] | 1361 | static int ufshcd_devfreq_init(struct ufs_hba *hba) |
| 1362 | { |
Bjorn Andersson | 092b455 | 2018-05-17 23:26:37 -0700 | [diff] [blame] | 1363 | struct list_head *clk_list = &hba->clk_list_head; |
| 1364 | struct ufs_clk_info *clki; |
Bjorn Andersson | deac444 | 2018-05-17 23:26:36 -0700 | [diff] [blame] | 1365 | struct devfreq *devfreq; |
| 1366 | int ret; |
| 1367 | |
Bjorn Andersson | 092b455 | 2018-05-17 23:26:37 -0700 | [diff] [blame] | 1368 | /* Skip devfreq if we don't have any clocks in the list */ |
| 1369 | if (list_empty(clk_list)) |
| 1370 | return 0; |
| 1371 | |
| 1372 | clki = list_first_entry(clk_list, struct ufs_clk_info, list); |
| 1373 | dev_pm_opp_add(hba->dev, clki->min_freq, 0); |
| 1374 | dev_pm_opp_add(hba->dev, clki->max_freq, 0); |
| 1375 | |
Stanley Chu | 90b8491 | 2020-05-09 17:37:13 +0800 | [diff] [blame] | 1376 | ufshcd_vops_config_scaling_param(hba, &hba->vps->devfreq_profile, |
| 1377 | &hba->vps->ondemand_data); |
Bjorn Andersson | 092b455 | 2018-05-17 23:26:37 -0700 | [diff] [blame] | 1378 | devfreq = devfreq_add_device(hba->dev, |
Stanley Chu | 90b8491 | 2020-05-09 17:37:13 +0800 | [diff] [blame] | 1379 | &hba->vps->devfreq_profile, |
Bjorn Andersson | deac444 | 2018-05-17 23:26:36 -0700 | [diff] [blame] | 1380 | DEVFREQ_GOV_SIMPLE_ONDEMAND, |
Stanley Chu | 90b8491 | 2020-05-09 17:37:13 +0800 | [diff] [blame] | 1381 | &hba->vps->ondemand_data); |
Bjorn Andersson | deac444 | 2018-05-17 23:26:36 -0700 | [diff] [blame] | 1382 | if (IS_ERR(devfreq)) { |
| 1383 | ret = PTR_ERR(devfreq); |
| 1384 | dev_err(hba->dev, "Unable to register with devfreq %d\n", ret); |
Bjorn Andersson | 092b455 | 2018-05-17 23:26:37 -0700 | [diff] [blame] | 1385 | |
| 1386 | dev_pm_opp_remove(hba->dev, clki->min_freq); |
| 1387 | dev_pm_opp_remove(hba->dev, clki->max_freq); |
Bjorn Andersson | deac444 | 2018-05-17 23:26:36 -0700 | [diff] [blame] | 1388 | return ret; |
| 1389 | } |
| 1390 | |
| 1391 | hba->devfreq = devfreq; |
| 1392 | |
| 1393 | return 0; |
| 1394 | } |
| 1395 | |
Bjorn Andersson | 092b455 | 2018-05-17 23:26:37 -0700 | [diff] [blame] | 1396 | static void ufshcd_devfreq_remove(struct ufs_hba *hba) |
| 1397 | { |
| 1398 | struct list_head *clk_list = &hba->clk_list_head; |
| 1399 | struct ufs_clk_info *clki; |
| 1400 | |
| 1401 | if (!hba->devfreq) |
| 1402 | return; |
| 1403 | |
| 1404 | devfreq_remove_device(hba->devfreq); |
| 1405 | hba->devfreq = NULL; |
| 1406 | |
| 1407 | clki = list_first_entry(clk_list, struct ufs_clk_info, list); |
| 1408 | dev_pm_opp_remove(hba->dev, clki->min_freq); |
| 1409 | dev_pm_opp_remove(hba->dev, clki->max_freq); |
| 1410 | } |
| 1411 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1412 | static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba) |
| 1413 | { |
| 1414 | unsigned long flags; |
| 1415 | |
| 1416 | devfreq_suspend_device(hba->devfreq); |
| 1417 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1418 | hba->clk_scaling.window_start_t = 0; |
| 1419 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1420 | } |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1421 | |
Gilad Broner | a508253 | 2016-10-17 17:10:00 -0700 | [diff] [blame] | 1422 | static void ufshcd_suspend_clkscaling(struct ufs_hba *hba) |
| 1423 | { |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1424 | unsigned long flags; |
| 1425 | bool suspend = false; |
| 1426 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1427 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 1428 | return; |
| 1429 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1430 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1431 | if (!hba->clk_scaling.is_suspended) { |
| 1432 | suspend = true; |
| 1433 | hba->clk_scaling.is_suspended = true; |
| 1434 | } |
| 1435 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1436 | |
| 1437 | if (suspend) |
| 1438 | __ufshcd_suspend_clkscaling(hba); |
Gilad Broner | a508253 | 2016-10-17 17:10:00 -0700 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | static void ufshcd_resume_clkscaling(struct ufs_hba *hba) |
| 1442 | { |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1443 | unsigned long flags; |
| 1444 | bool resume = false; |
| 1445 | |
| 1446 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 1447 | return; |
| 1448 | |
| 1449 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1450 | if (hba->clk_scaling.is_suspended) { |
| 1451 | resume = true; |
| 1452 | hba->clk_scaling.is_suspended = false; |
| 1453 | } |
| 1454 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1455 | |
| 1456 | if (resume) |
| 1457 | devfreq_resume_device(hba->devfreq); |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | static ssize_t ufshcd_clkscale_enable_show(struct device *dev, |
| 1461 | struct device_attribute *attr, char *buf) |
| 1462 | { |
| 1463 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1464 | |
| 1465 | return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_scaling.is_allowed); |
| 1466 | } |
| 1467 | |
| 1468 | static ssize_t ufshcd_clkscale_enable_store(struct device *dev, |
| 1469 | struct device_attribute *attr, const char *buf, size_t count) |
| 1470 | { |
| 1471 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1472 | u32 value; |
| 1473 | int err; |
| 1474 | |
| 1475 | if (kstrtou32(buf, 0, &value)) |
| 1476 | return -EINVAL; |
| 1477 | |
| 1478 | value = !!value; |
| 1479 | if (value == hba->clk_scaling.is_allowed) |
| 1480 | goto out; |
| 1481 | |
| 1482 | pm_runtime_get_sync(hba->dev); |
| 1483 | ufshcd_hold(hba, false); |
| 1484 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1485 | cancel_work_sync(&hba->clk_scaling.suspend_work); |
| 1486 | cancel_work_sync(&hba->clk_scaling.resume_work); |
| 1487 | |
| 1488 | hba->clk_scaling.is_allowed = value; |
| 1489 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1490 | if (value) { |
| 1491 | ufshcd_resume_clkscaling(hba); |
| 1492 | } else { |
| 1493 | ufshcd_suspend_clkscaling(hba); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1494 | err = ufshcd_devfreq_scale(hba, true); |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1495 | if (err) |
| 1496 | dev_err(hba->dev, "%s: failed to scale clocks up %d\n", |
| 1497 | __func__, err); |
| 1498 | } |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1499 | |
| 1500 | ufshcd_release(hba); |
| 1501 | pm_runtime_put_sync(hba->dev); |
| 1502 | out: |
| 1503 | return count; |
Gilad Broner | a508253 | 2016-10-17 17:10:00 -0700 | [diff] [blame] | 1504 | } |
| 1505 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1506 | static void ufshcd_clkscaling_init_sysfs(struct ufs_hba *hba) |
| 1507 | { |
| 1508 | hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show; |
| 1509 | hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store; |
| 1510 | sysfs_attr_init(&hba->clk_scaling.enable_attr.attr); |
| 1511 | hba->clk_scaling.enable_attr.attr.name = "clkscale_enable"; |
| 1512 | hba->clk_scaling.enable_attr.attr.mode = 0644; |
| 1513 | if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr)) |
| 1514 | dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n"); |
| 1515 | } |
| 1516 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1517 | static void ufshcd_ungate_work(struct work_struct *work) |
| 1518 | { |
| 1519 | int ret; |
| 1520 | unsigned long flags; |
| 1521 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1522 | clk_gating.ungate_work); |
| 1523 | |
| 1524 | cancel_delayed_work_sync(&hba->clk_gating.gate_work); |
| 1525 | |
| 1526 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1527 | if (hba->clk_gating.state == CLKS_ON) { |
| 1528 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1529 | goto unblock_reqs; |
| 1530 | } |
| 1531 | |
| 1532 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1533 | ufshcd_setup_clocks(hba, true); |
| 1534 | |
Stanley Chu | 8b0bbf0 | 2019-12-07 20:22:01 +0800 | [diff] [blame] | 1535 | ufshcd_enable_irq(hba); |
| 1536 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1537 | /* Exit from hibern8 */ |
| 1538 | if (ufshcd_can_hibern8_during_gating(hba)) { |
| 1539 | /* Prevent gating in this path */ |
| 1540 | hba->clk_gating.is_suspended = true; |
| 1541 | if (ufshcd_is_link_hibern8(hba)) { |
| 1542 | ret = ufshcd_uic_hibern8_exit(hba); |
| 1543 | if (ret) |
| 1544 | dev_err(hba->dev, "%s: hibern8 exit failed %d\n", |
| 1545 | __func__, ret); |
| 1546 | else |
| 1547 | ufshcd_set_link_active(hba); |
| 1548 | } |
| 1549 | hba->clk_gating.is_suspended = false; |
| 1550 | } |
| 1551 | unblock_reqs: |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 1552 | ufshcd_scsi_unblock_requests(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | /** |
| 1556 | * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release. |
| 1557 | * Also, exit from hibern8 mode and set the link as active. |
| 1558 | * @hba: per adapter instance |
| 1559 | * @async: This indicates whether caller should ungate clocks asynchronously. |
| 1560 | */ |
| 1561 | int ufshcd_hold(struct ufs_hba *hba, bool async) |
| 1562 | { |
| 1563 | int rc = 0; |
Stanley Chu | 93b6c5d | 2020-08-09 13:07:34 +0800 | [diff] [blame] | 1564 | bool flush_result; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1565 | unsigned long flags; |
| 1566 | |
| 1567 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1568 | goto out; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1569 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1570 | hba->clk_gating.active_reqs++; |
| 1571 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 1572 | if (ufshcd_eh_in_progress(hba)) { |
| 1573 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1574 | return 0; |
| 1575 | } |
| 1576 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1577 | start: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1578 | switch (hba->clk_gating.state) { |
| 1579 | case CLKS_ON: |
Venkat Gopalakrishnan | f2a785a | 2016-10-17 17:10:53 -0700 | [diff] [blame] | 1580 | /* |
| 1581 | * Wait for the ungate work to complete if in progress. |
| 1582 | * Though the clocks may be in ON state, the link could |
| 1583 | * still be in hibner8 state if hibern8 is allowed |
| 1584 | * during clock gating. |
| 1585 | * Make sure we exit hibern8 state also in addition to |
| 1586 | * clocks being ON. |
| 1587 | */ |
| 1588 | if (ufshcd_can_hibern8_during_gating(hba) && |
| 1589 | ufshcd_is_link_hibern8(hba)) { |
Can Guo | c63d609 | 2020-02-10 19:40:48 -0800 | [diff] [blame] | 1590 | if (async) { |
| 1591 | rc = -EAGAIN; |
| 1592 | hba->clk_gating.active_reqs--; |
| 1593 | break; |
| 1594 | } |
Venkat Gopalakrishnan | f2a785a | 2016-10-17 17:10:53 -0700 | [diff] [blame] | 1595 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Stanley Chu | 93b6c5d | 2020-08-09 13:07:34 +0800 | [diff] [blame] | 1596 | flush_result = flush_work(&hba->clk_gating.ungate_work); |
| 1597 | if (hba->clk_gating.is_suspended && !flush_result) |
| 1598 | goto out; |
Venkat Gopalakrishnan | f2a785a | 2016-10-17 17:10:53 -0700 | [diff] [blame] | 1599 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1600 | goto start; |
| 1601 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1602 | break; |
| 1603 | case REQ_CLKS_OFF: |
| 1604 | if (cancel_delayed_work(&hba->clk_gating.gate_work)) { |
| 1605 | hba->clk_gating.state = CLKS_ON; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1606 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1607 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1608 | break; |
| 1609 | } |
| 1610 | /* |
Tomohiro Kusumi | 9c490d2 | 2017-03-28 16:49:26 +0300 | [diff] [blame] | 1611 | * If we are here, it means gating work is either done or |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1612 | * currently running. Hence, fall through to cancel gating |
| 1613 | * work and to enable clocks. |
| 1614 | */ |
Tomas Winkler | 30eb2e4 | 2018-11-26 10:10:34 +0200 | [diff] [blame] | 1615 | /* fallthrough */ |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1616 | case CLKS_OFF: |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 1617 | ufshcd_scsi_block_requests(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1618 | hba->clk_gating.state = REQ_CLKS_ON; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1619 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1620 | hba->clk_gating.state); |
Vijay Viswanath | 10e5e37 | 2018-05-03 16:37:22 +0530 | [diff] [blame] | 1621 | queue_work(hba->clk_gating.clk_gating_workq, |
| 1622 | &hba->clk_gating.ungate_work); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1623 | /* |
| 1624 | * fall through to check if we should wait for this |
| 1625 | * work to be done or not. |
| 1626 | */ |
Tomas Winkler | 30eb2e4 | 2018-11-26 10:10:34 +0200 | [diff] [blame] | 1627 | /* fallthrough */ |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1628 | case REQ_CLKS_ON: |
| 1629 | if (async) { |
| 1630 | rc = -EAGAIN; |
| 1631 | hba->clk_gating.active_reqs--; |
| 1632 | break; |
| 1633 | } |
| 1634 | |
| 1635 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1636 | flush_work(&hba->clk_gating.ungate_work); |
| 1637 | /* Make sure state is CLKS_ON before returning */ |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1638 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1639 | goto start; |
| 1640 | default: |
| 1641 | dev_err(hba->dev, "%s: clk gating is in invalid state %d\n", |
| 1642 | __func__, hba->clk_gating.state); |
| 1643 | break; |
| 1644 | } |
| 1645 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1646 | out: |
| 1647 | return rc; |
| 1648 | } |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 1649 | EXPORT_SYMBOL_GPL(ufshcd_hold); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1650 | |
| 1651 | static void ufshcd_gate_work(struct work_struct *work) |
| 1652 | { |
| 1653 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1654 | clk_gating.gate_work.work); |
| 1655 | unsigned long flags; |
| 1656 | |
| 1657 | spin_lock_irqsave(hba->host->host_lock, flags); |
Venkat Gopalakrishnan | 3f0c06d | 2016-10-17 17:11:07 -0700 | [diff] [blame] | 1658 | /* |
| 1659 | * In case you are here to cancel this work the gating state |
| 1660 | * would be marked as REQ_CLKS_ON. In this case save time by |
| 1661 | * skipping the gating work and exit after changing the clock |
| 1662 | * state to CLKS_ON. |
| 1663 | */ |
| 1664 | if (hba->clk_gating.is_suspended || |
Asutosh Das | 18f01374 | 2019-11-14 22:09:29 -0800 | [diff] [blame] | 1665 | (hba->clk_gating.state != REQ_CLKS_OFF)) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1666 | hba->clk_gating.state = CLKS_ON; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1667 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1668 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1669 | goto rel_lock; |
| 1670 | } |
| 1671 | |
| 1672 | if (hba->clk_gating.active_reqs |
| 1673 | || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 1674 | || ufshcd_any_tag_in_use(hba) || hba->outstanding_tasks |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1675 | || hba->active_uic_cmd || hba->uic_async_done) |
| 1676 | goto rel_lock; |
| 1677 | |
| 1678 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1679 | |
| 1680 | /* put the link into hibern8 mode before turning off clocks */ |
| 1681 | if (ufshcd_can_hibern8_during_gating(hba)) { |
| 1682 | if (ufshcd_uic_hibern8_enter(hba)) { |
| 1683 | hba->clk_gating.state = CLKS_ON; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1684 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1685 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1686 | goto out; |
| 1687 | } |
| 1688 | ufshcd_set_link_hibern8(hba); |
| 1689 | } |
| 1690 | |
Stanley Chu | 8b0bbf0 | 2019-12-07 20:22:01 +0800 | [diff] [blame] | 1691 | ufshcd_disable_irq(hba); |
| 1692 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1693 | if (!ufshcd_is_link_active(hba)) |
| 1694 | ufshcd_setup_clocks(hba, false); |
| 1695 | else |
| 1696 | /* If link is active, device ref_clk can't be switched off */ |
| 1697 | __ufshcd_setup_clocks(hba, false, true); |
| 1698 | |
| 1699 | /* |
| 1700 | * In case you are here to cancel this work the gating state |
| 1701 | * would be marked as REQ_CLKS_ON. In this case keep the state |
| 1702 | * as REQ_CLKS_ON which would anyway imply that clocks are off |
| 1703 | * and a request to turn them on is pending. By doing this way, |
| 1704 | * we keep the state machine in tact and this would ultimately |
| 1705 | * prevent from doing cancel work multiple times when there are |
| 1706 | * new requests arriving before the current cancel work is done. |
| 1707 | */ |
| 1708 | spin_lock_irqsave(hba->host->host_lock, flags); |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1709 | if (hba->clk_gating.state == REQ_CLKS_OFF) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1710 | hba->clk_gating.state = CLKS_OFF; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1711 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1712 | hba->clk_gating.state); |
| 1713 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1714 | rel_lock: |
| 1715 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1716 | out: |
| 1717 | return; |
| 1718 | } |
| 1719 | |
| 1720 | /* host lock must be held before calling this variant */ |
| 1721 | static void __ufshcd_release(struct ufs_hba *hba) |
| 1722 | { |
| 1723 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1724 | return; |
| 1725 | |
| 1726 | hba->clk_gating.active_reqs--; |
| 1727 | |
| 1728 | if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended |
| 1729 | || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 1730 | || ufshcd_any_tag_in_use(hba) || hba->outstanding_tasks |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 1731 | || hba->active_uic_cmd || hba->uic_async_done |
| 1732 | || ufshcd_eh_in_progress(hba)) |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1733 | return; |
| 1734 | |
| 1735 | hba->clk_gating.state = REQ_CLKS_OFF; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1736 | trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state); |
Evan Green | f4bb770 | 2018-10-05 10:27:32 -0700 | [diff] [blame] | 1737 | queue_delayed_work(hba->clk_gating.clk_gating_workq, |
| 1738 | &hba->clk_gating.gate_work, |
| 1739 | msecs_to_jiffies(hba->clk_gating.delay_ms)); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | void ufshcd_release(struct ufs_hba *hba) |
| 1743 | { |
| 1744 | unsigned long flags; |
| 1745 | |
| 1746 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1747 | __ufshcd_release(hba); |
| 1748 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1749 | } |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 1750 | EXPORT_SYMBOL_GPL(ufshcd_release); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1751 | |
| 1752 | static ssize_t ufshcd_clkgate_delay_show(struct device *dev, |
| 1753 | struct device_attribute *attr, char *buf) |
| 1754 | { |
| 1755 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1756 | |
| 1757 | return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms); |
| 1758 | } |
| 1759 | |
| 1760 | static ssize_t ufshcd_clkgate_delay_store(struct device *dev, |
| 1761 | struct device_attribute *attr, const char *buf, size_t count) |
| 1762 | { |
| 1763 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1764 | unsigned long flags, value; |
| 1765 | |
| 1766 | if (kstrtoul(buf, 0, &value)) |
| 1767 | return -EINVAL; |
| 1768 | |
| 1769 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1770 | hba->clk_gating.delay_ms = value; |
| 1771 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1772 | return count; |
| 1773 | } |
| 1774 | |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1775 | static ssize_t ufshcd_clkgate_enable_show(struct device *dev, |
| 1776 | struct device_attribute *attr, char *buf) |
| 1777 | { |
| 1778 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1779 | |
| 1780 | return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_gating.is_enabled); |
| 1781 | } |
| 1782 | |
| 1783 | static ssize_t ufshcd_clkgate_enable_store(struct device *dev, |
| 1784 | struct device_attribute *attr, const char *buf, size_t count) |
| 1785 | { |
| 1786 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1787 | unsigned long flags; |
| 1788 | u32 value; |
| 1789 | |
| 1790 | if (kstrtou32(buf, 0, &value)) |
| 1791 | return -EINVAL; |
| 1792 | |
| 1793 | value = !!value; |
| 1794 | if (value == hba->clk_gating.is_enabled) |
| 1795 | goto out; |
| 1796 | |
| 1797 | if (value) { |
| 1798 | ufshcd_release(hba); |
| 1799 | } else { |
| 1800 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1801 | hba->clk_gating.active_reqs++; |
| 1802 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1803 | } |
| 1804 | |
| 1805 | hba->clk_gating.is_enabled = value; |
| 1806 | out: |
| 1807 | return count; |
| 1808 | } |
| 1809 | |
Vivek Gautam | eebcc19 | 2018-08-07 23:17:39 +0530 | [diff] [blame] | 1810 | static void ufshcd_init_clk_scaling(struct ufs_hba *hba) |
| 1811 | { |
| 1812 | char wq_name[sizeof("ufs_clkscaling_00")]; |
| 1813 | |
| 1814 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 1815 | return; |
| 1816 | |
| 1817 | INIT_WORK(&hba->clk_scaling.suspend_work, |
| 1818 | ufshcd_clk_scaling_suspend_work); |
| 1819 | INIT_WORK(&hba->clk_scaling.resume_work, |
| 1820 | ufshcd_clk_scaling_resume_work); |
| 1821 | |
| 1822 | snprintf(wq_name, sizeof(wq_name), "ufs_clkscaling_%d", |
| 1823 | hba->host->host_no); |
| 1824 | hba->clk_scaling.workq = create_singlethread_workqueue(wq_name); |
| 1825 | |
| 1826 | ufshcd_clkscaling_init_sysfs(hba); |
| 1827 | } |
| 1828 | |
| 1829 | static void ufshcd_exit_clk_scaling(struct ufs_hba *hba) |
| 1830 | { |
| 1831 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 1832 | return; |
| 1833 | |
| 1834 | destroy_workqueue(hba->clk_scaling.workq); |
| 1835 | ufshcd_devfreq_remove(hba); |
| 1836 | } |
| 1837 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1838 | static void ufshcd_init_clk_gating(struct ufs_hba *hba) |
| 1839 | { |
Vijay Viswanath | 10e5e37 | 2018-05-03 16:37:22 +0530 | [diff] [blame] | 1840 | char wq_name[sizeof("ufs_clk_gating_00")]; |
| 1841 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1842 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1843 | return; |
| 1844 | |
| 1845 | hba->clk_gating.delay_ms = 150; |
| 1846 | INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work); |
| 1847 | INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work); |
| 1848 | |
Vijay Viswanath | 10e5e37 | 2018-05-03 16:37:22 +0530 | [diff] [blame] | 1849 | snprintf(wq_name, ARRAY_SIZE(wq_name), "ufs_clk_gating_%d", |
| 1850 | hba->host->host_no); |
| 1851 | hba->clk_gating.clk_gating_workq = alloc_ordered_workqueue(wq_name, |
| 1852 | WQ_MEM_RECLAIM); |
| 1853 | |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1854 | hba->clk_gating.is_enabled = true; |
| 1855 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1856 | hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show; |
| 1857 | hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store; |
| 1858 | sysfs_attr_init(&hba->clk_gating.delay_attr.attr); |
| 1859 | hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms"; |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1860 | hba->clk_gating.delay_attr.attr.mode = 0644; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1861 | if (device_create_file(hba->dev, &hba->clk_gating.delay_attr)) |
| 1862 | dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n"); |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1863 | |
| 1864 | hba->clk_gating.enable_attr.show = ufshcd_clkgate_enable_show; |
| 1865 | hba->clk_gating.enable_attr.store = ufshcd_clkgate_enable_store; |
| 1866 | sysfs_attr_init(&hba->clk_gating.enable_attr.attr); |
| 1867 | hba->clk_gating.enable_attr.attr.name = "clkgate_enable"; |
| 1868 | hba->clk_gating.enable_attr.attr.mode = 0644; |
| 1869 | if (device_create_file(hba->dev, &hba->clk_gating.enable_attr)) |
| 1870 | dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n"); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | static void ufshcd_exit_clk_gating(struct ufs_hba *hba) |
| 1874 | { |
| 1875 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1876 | return; |
| 1877 | device_remove_file(hba->dev, &hba->clk_gating.delay_attr); |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1878 | device_remove_file(hba->dev, &hba->clk_gating.enable_attr); |
Akinobu Mita | 97cd680 | 2014-11-24 14:24:18 +0900 | [diff] [blame] | 1879 | cancel_work_sync(&hba->clk_gating.ungate_work); |
| 1880 | cancel_delayed_work_sync(&hba->clk_gating.gate_work); |
Vijay Viswanath | 10e5e37 | 2018-05-03 16:37:22 +0530 | [diff] [blame] | 1881 | destroy_workqueue(hba->clk_gating.clk_gating_workq); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1882 | } |
| 1883 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1884 | /* Must be called with host lock acquired */ |
| 1885 | static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba) |
| 1886 | { |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1887 | bool queue_resume_work = false; |
Stanley Chu | b1bf66d | 2020-06-11 18:10:43 +0800 | [diff] [blame] | 1888 | ktime_t curr_t = ktime_get(); |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1889 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1890 | if (!ufshcd_is_clkscaling_supported(hba)) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1891 | return; |
| 1892 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1893 | if (!hba->clk_scaling.active_reqs++) |
| 1894 | queue_resume_work = true; |
| 1895 | |
| 1896 | if (!hba->clk_scaling.is_allowed || hba->pm_op_in_progress) |
| 1897 | return; |
| 1898 | |
| 1899 | if (queue_resume_work) |
| 1900 | queue_work(hba->clk_scaling.workq, |
| 1901 | &hba->clk_scaling.resume_work); |
| 1902 | |
| 1903 | if (!hba->clk_scaling.window_start_t) { |
Stanley Chu | b1bf66d | 2020-06-11 18:10:43 +0800 | [diff] [blame] | 1904 | hba->clk_scaling.window_start_t = curr_t; |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1905 | hba->clk_scaling.tot_busy_t = 0; |
| 1906 | hba->clk_scaling.is_busy_started = false; |
| 1907 | } |
| 1908 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1909 | if (!hba->clk_scaling.is_busy_started) { |
Stanley Chu | b1bf66d | 2020-06-11 18:10:43 +0800 | [diff] [blame] | 1910 | hba->clk_scaling.busy_start_t = curr_t; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1911 | hba->clk_scaling.is_busy_started = true; |
| 1912 | } |
| 1913 | } |
| 1914 | |
| 1915 | static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba) |
| 1916 | { |
| 1917 | struct ufs_clk_scaling *scaling = &hba->clk_scaling; |
| 1918 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1919 | if (!ufshcd_is_clkscaling_supported(hba)) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1920 | return; |
| 1921 | |
| 1922 | if (!hba->outstanding_reqs && scaling->is_busy_started) { |
| 1923 | scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(), |
| 1924 | scaling->busy_start_t)); |
Thomas Gleixner | 8b0e195 | 2016-12-25 12:30:41 +0100 | [diff] [blame] | 1925 | scaling->busy_start_t = 0; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1926 | scaling->is_busy_started = false; |
| 1927 | } |
| 1928 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1929 | /** |
| 1930 | * ufshcd_send_command - Send SCSI or device management commands |
| 1931 | * @hba: per adapter instance |
| 1932 | * @task_tag: Task tag of the command |
| 1933 | */ |
| 1934 | static inline |
| 1935 | void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag) |
| 1936 | { |
Stanley Chu | 6edfdcf | 2020-07-06 14:07:07 +0800 | [diff] [blame] | 1937 | struct ufshcd_lrb *lrbp = &hba->lrb[task_tag]; |
| 1938 | |
| 1939 | lrbp->issue_time_stamp = ktime_get(); |
| 1940 | lrbp->compl_time_stamp = ktime_set(0, 0); |
| 1941 | ufshcd_vops_setup_xfer_req(hba, task_tag, (lrbp->cmd ? true : false)); |
Bart Van Assche | eacf36f | 2019-12-24 14:02:46 -0800 | [diff] [blame] | 1942 | ufshcd_add_command_trace(hba, task_tag, "send"); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1943 | ufshcd_clk_scaling_start_busy(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1944 | __set_bit(task_tag, &hba->outstanding_reqs); |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 1945 | ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
Gilad Broner | ad1a1b9 | 2016-10-17 17:09:36 -0700 | [diff] [blame] | 1946 | /* Make sure that doorbell is committed immediately */ |
| 1947 | wmb(); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | /** |
| 1951 | * ufshcd_copy_sense_data - Copy sense data in case of check condition |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 1952 | * @lrbp: pointer to local reference block |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1953 | */ |
| 1954 | static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp) |
| 1955 | { |
| 1956 | int len; |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 1957 | if (lrbp->sense_buffer && |
| 1958 | ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) { |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 1959 | int len_to_copy; |
| 1960 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1961 | len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len); |
Avri Altman | 09a5a24 | 2018-11-22 20:04:56 +0200 | [diff] [blame] | 1962 | len_to_copy = min_t(int, UFS_SENSE_SIZE, len); |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 1963 | |
Avri Altman | 09a5a24 | 2018-11-22 20:04:56 +0200 | [diff] [blame] | 1964 | memcpy(lrbp->sense_buffer, lrbp->ucd_rsp_ptr->sr.sense_data, |
| 1965 | len_to_copy); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1966 | } |
| 1967 | } |
| 1968 | |
| 1969 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1970 | * ufshcd_copy_query_response() - Copy the Query Response and the data |
| 1971 | * descriptor |
| 1972 | * @hba: per adapter instance |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 1973 | * @lrbp: pointer to local reference block |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1974 | */ |
| 1975 | static |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1976 | int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1977 | { |
| 1978 | struct ufs_query_res *query_res = &hba->dev_cmd.query.response; |
| 1979 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1980 | memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1981 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1982 | /* Get the descriptor */ |
Avri Altman | 1c90836 | 2019-05-21 11:24:22 +0300 | [diff] [blame] | 1983 | if (hba->dev_cmd.query.descriptor && |
| 1984 | lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 1985 | u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1986 | GENERAL_UPIU_REQUEST_SIZE; |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1987 | u16 resp_len; |
| 1988 | u16 buf_len; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1989 | |
| 1990 | /* data segment length */ |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1991 | resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) & |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1992 | MASK_QUERY_DATA_SEG_LEN; |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 1993 | buf_len = be16_to_cpu( |
| 1994 | hba->dev_cmd.query.request.upiu_req.length); |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1995 | if (likely(buf_len >= resp_len)) { |
| 1996 | memcpy(hba->dev_cmd.query.descriptor, descp, resp_len); |
| 1997 | } else { |
| 1998 | dev_warn(hba->dev, |
Bean Huo | 3d4881d | 2019-11-12 23:34:35 +0100 | [diff] [blame] | 1999 | "%s: rsp size %d is bigger than buffer size %d", |
| 2000 | __func__, resp_len, buf_len); |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2001 | return -EINVAL; |
| 2002 | } |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2003 | } |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2004 | |
| 2005 | return 0; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2009 | * ufshcd_hba_capabilities - Read controller capabilities |
| 2010 | * @hba: per adapter instance |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 2011 | * |
| 2012 | * Return: 0 on success, negative on error. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2013 | */ |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 2014 | static inline int ufshcd_hba_capabilities(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2015 | { |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 2016 | int err; |
| 2017 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 2018 | hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2019 | |
| 2020 | /* nutrs and nutmrs are 0 based values */ |
| 2021 | hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1; |
| 2022 | hba->nutmrs = |
| 2023 | ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1; |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 2024 | |
| 2025 | /* Read crypto capabilities */ |
| 2026 | err = ufshcd_hba_init_crypto_capabilities(hba); |
| 2027 | if (err) |
| 2028 | dev_err(hba->dev, "crypto setup failed\n"); |
| 2029 | |
| 2030 | return err; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2034 | * ufshcd_ready_for_uic_cmd - Check if controller is ready |
| 2035 | * to accept UIC commands |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2036 | * @hba: per adapter instance |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2037 | * Return true on success, else false |
| 2038 | */ |
| 2039 | static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba) |
| 2040 | { |
| 2041 | if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY) |
| 2042 | return true; |
| 2043 | else |
| 2044 | return false; |
| 2045 | } |
| 2046 | |
| 2047 | /** |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 2048 | * ufshcd_get_upmcrs - Get the power mode change request status |
| 2049 | * @hba: Pointer to adapter instance |
| 2050 | * |
| 2051 | * This function gets the UPMCRS field of HCS register |
| 2052 | * Returns value of UPMCRS field |
| 2053 | */ |
| 2054 | static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba) |
| 2055 | { |
| 2056 | return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7; |
| 2057 | } |
| 2058 | |
| 2059 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2060 | * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers |
| 2061 | * @hba: per adapter instance |
| 2062 | * @uic_cmd: UIC command |
| 2063 | * |
| 2064 | * Mutex must be held. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2065 | */ |
| 2066 | static inline void |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2067 | ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2068 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2069 | WARN_ON(hba->active_uic_cmd); |
| 2070 | |
| 2071 | hba->active_uic_cmd = uic_cmd; |
| 2072 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2073 | /* Write Args */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2074 | ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1); |
| 2075 | ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2); |
| 2076 | ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2077 | |
Stanley Chu | aa5c697 | 2020-06-15 15:22:35 +0800 | [diff] [blame] | 2078 | ufshcd_add_uic_command_trace(hba, uic_cmd, "send"); |
| 2079 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2080 | /* Write UIC Cmd */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2081 | ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK, |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 2082 | REG_UIC_COMMAND); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2083 | } |
| 2084 | |
| 2085 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2086 | * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command |
| 2087 | * @hba: per adapter instance |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2088 | * @uic_cmd: UIC command |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2089 | * |
| 2090 | * Must be called with mutex held. |
| 2091 | * Returns 0 only if success. |
| 2092 | */ |
| 2093 | static int |
| 2094 | ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) |
| 2095 | { |
| 2096 | int ret; |
| 2097 | unsigned long flags; |
| 2098 | |
| 2099 | if (wait_for_completion_timeout(&uic_cmd->done, |
| 2100 | msecs_to_jiffies(UIC_CMD_TIMEOUT))) |
| 2101 | ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT; |
| 2102 | else |
| 2103 | ret = -ETIMEDOUT; |
| 2104 | |
| 2105 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2106 | hba->active_uic_cmd = NULL; |
| 2107 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2108 | |
| 2109 | return ret; |
| 2110 | } |
| 2111 | |
| 2112 | /** |
| 2113 | * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result |
| 2114 | * @hba: per adapter instance |
| 2115 | * @uic_cmd: UIC command |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2116 | * @completion: initialize the completion only if this is set to true |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2117 | * |
| 2118 | * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2119 | * with mutex held and host_lock locked. |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2120 | * Returns 0 only if success. |
| 2121 | */ |
| 2122 | static int |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2123 | __ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd, |
| 2124 | bool completion) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2125 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2126 | if (!ufshcd_ready_for_uic_cmd(hba)) { |
| 2127 | dev_err(hba->dev, |
| 2128 | "Controller not ready to accept UIC commands\n"); |
| 2129 | return -EIO; |
| 2130 | } |
| 2131 | |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2132 | if (completion) |
| 2133 | init_completion(&uic_cmd->done); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2134 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2135 | ufshcd_dispatch_uic_cmd(hba, uic_cmd); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2136 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2137 | return 0; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2138 | } |
| 2139 | |
| 2140 | /** |
| 2141 | * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result |
| 2142 | * @hba: per adapter instance |
| 2143 | * @uic_cmd: UIC command |
| 2144 | * |
| 2145 | * Returns 0 only if success. |
| 2146 | */ |
Avri Altman | e77044c5 | 2018-10-07 17:30:39 +0300 | [diff] [blame] | 2147 | int ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2148 | { |
| 2149 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2150 | unsigned long flags; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2151 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2152 | ufshcd_hold(hba, false); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2153 | mutex_lock(&hba->uic_cmd_mutex); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 2154 | ufshcd_add_delay_before_dme_cmd(hba); |
| 2155 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2156 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2157 | ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2158 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2159 | if (!ret) |
| 2160 | ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd); |
| 2161 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2162 | mutex_unlock(&hba->uic_cmd_mutex); |
| 2163 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2164 | ufshcd_release(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2165 | return ret; |
| 2166 | } |
| 2167 | |
| 2168 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2169 | * ufshcd_map_sg - Map scatter-gather list to prdt |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2170 | * @hba: per adapter instance |
| 2171 | * @lrbp: pointer to local reference block |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2172 | * |
| 2173 | * Returns 0 in case of success, non-zero value in case of failure |
| 2174 | */ |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 2175 | static int ufshcd_map_sg(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2176 | { |
| 2177 | struct ufshcd_sg_entry *prd_table; |
| 2178 | struct scatterlist *sg; |
| 2179 | struct scsi_cmnd *cmd; |
| 2180 | int sg_segments; |
| 2181 | int i; |
| 2182 | |
| 2183 | cmd = lrbp->cmd; |
| 2184 | sg_segments = scsi_dma_map(cmd); |
| 2185 | if (sg_segments < 0) |
| 2186 | return sg_segments; |
| 2187 | |
| 2188 | if (sg_segments) { |
Alim Akhtar | 26f968d | 2020-05-28 06:46:52 +0530 | [diff] [blame] | 2189 | |
| 2190 | if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) |
| 2191 | lrbp->utr_descriptor_ptr->prd_table_length = |
| 2192 | cpu_to_le16((sg_segments * |
| 2193 | sizeof(struct ufshcd_sg_entry))); |
| 2194 | else |
| 2195 | lrbp->utr_descriptor_ptr->prd_table_length = |
| 2196 | cpu_to_le16((u16) (sg_segments)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2197 | |
| 2198 | prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr; |
| 2199 | |
| 2200 | scsi_for_each_sg(cmd, sg, sg_segments, i) { |
| 2201 | prd_table[i].size = |
| 2202 | cpu_to_le32(((u32) sg_dma_len(sg))-1); |
| 2203 | prd_table[i].base_addr = |
| 2204 | cpu_to_le32(lower_32_bits(sg->dma_address)); |
| 2205 | prd_table[i].upper_addr = |
| 2206 | cpu_to_le32(upper_32_bits(sg->dma_address)); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2207 | prd_table[i].reserved = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2208 | } |
| 2209 | } else { |
| 2210 | lrbp->utr_descriptor_ptr->prd_table_length = 0; |
| 2211 | } |
| 2212 | |
| 2213 | return 0; |
| 2214 | } |
| 2215 | |
| 2216 | /** |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2217 | * ufshcd_enable_intr - enable interrupts |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2218 | * @hba: per adapter instance |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2219 | * @intrs: interrupt bits |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2220 | */ |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2221 | static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2222 | { |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2223 | u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
| 2224 | |
| 2225 | if (hba->ufs_version == UFSHCI_VERSION_10) { |
| 2226 | u32 rw; |
| 2227 | rw = set & INTERRUPT_MASK_RW_VER_10; |
| 2228 | set = rw | ((set ^ intrs) & intrs); |
| 2229 | } else { |
| 2230 | set |= intrs; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2231 | } |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2232 | |
| 2233 | ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE); |
| 2234 | } |
| 2235 | |
| 2236 | /** |
| 2237 | * ufshcd_disable_intr - disable interrupts |
| 2238 | * @hba: per adapter instance |
| 2239 | * @intrs: interrupt bits |
| 2240 | */ |
| 2241 | static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs) |
| 2242 | { |
| 2243 | u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
| 2244 | |
| 2245 | if (hba->ufs_version == UFSHCI_VERSION_10) { |
| 2246 | u32 rw; |
| 2247 | rw = (set & INTERRUPT_MASK_RW_VER_10) & |
| 2248 | ~(intrs & INTERRUPT_MASK_RW_VER_10); |
| 2249 | set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10); |
| 2250 | |
| 2251 | } else { |
| 2252 | set &= ~intrs; |
| 2253 | } |
| 2254 | |
| 2255 | ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2256 | } |
| 2257 | |
| 2258 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2259 | * ufshcd_prepare_req_desc_hdr() - Fills the requests header |
| 2260 | * descriptor according to request |
| 2261 | * @lrbp: pointer to local reference block |
| 2262 | * @upiu_flags: flags required in the header |
| 2263 | * @cmd_dir: requests data direction |
| 2264 | */ |
| 2265 | static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp, |
Bean Huo | a23064c | 2020-07-06 14:39:36 +0200 | [diff] [blame] | 2266 | u8 *upiu_flags, enum dma_data_direction cmd_dir) |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2267 | { |
| 2268 | struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr; |
| 2269 | u32 data_direction; |
| 2270 | u32 dword_0; |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 2271 | u32 dword_1 = 0; |
| 2272 | u32 dword_3 = 0; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2273 | |
| 2274 | if (cmd_dir == DMA_FROM_DEVICE) { |
| 2275 | data_direction = UTP_DEVICE_TO_HOST; |
| 2276 | *upiu_flags = UPIU_CMD_FLAGS_READ; |
| 2277 | } else if (cmd_dir == DMA_TO_DEVICE) { |
| 2278 | data_direction = UTP_HOST_TO_DEVICE; |
| 2279 | *upiu_flags = UPIU_CMD_FLAGS_WRITE; |
| 2280 | } else { |
| 2281 | data_direction = UTP_NO_DATA_TRANSFER; |
| 2282 | *upiu_flags = UPIU_CMD_FLAGS_NONE; |
| 2283 | } |
| 2284 | |
| 2285 | dword_0 = data_direction | (lrbp->command_type |
| 2286 | << UPIU_COMMAND_TYPE_OFFSET); |
| 2287 | if (lrbp->intr_cmd) |
| 2288 | dword_0 |= UTP_REQ_DESC_INT_CMD; |
| 2289 | |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 2290 | /* Prepare crypto related dwords */ |
| 2291 | ufshcd_prepare_req_desc_hdr_crypto(lrbp, &dword_0, &dword_1, &dword_3); |
| 2292 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2293 | /* Transfer request descriptor header fields */ |
| 2294 | req_desc->header.dword_0 = cpu_to_le32(dword_0); |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 2295 | req_desc->header.dword_1 = cpu_to_le32(dword_1); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2296 | /* |
| 2297 | * assigning invalid value for command status. Controller |
| 2298 | * updates OCS on command completion, with the command |
| 2299 | * status |
| 2300 | */ |
| 2301 | req_desc->header.dword_2 = |
| 2302 | cpu_to_le32(OCS_INVALID_COMMAND_STATUS); |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 2303 | req_desc->header.dword_3 = cpu_to_le32(dword_3); |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 2304 | |
| 2305 | req_desc->prd_table_length = 0; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2306 | } |
| 2307 | |
| 2308 | /** |
| 2309 | * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc, |
| 2310 | * for scsi commands |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2311 | * @lrbp: local reference block pointer |
| 2312 | * @upiu_flags: flags |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2313 | */ |
| 2314 | static |
Bean Huo | a23064c | 2020-07-06 14:39:36 +0200 | [diff] [blame] | 2315 | void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u8 upiu_flags) |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2316 | { |
Bart Van Assche | 1b21b8f | 2019-12-24 14:02:45 -0800 | [diff] [blame] | 2317 | struct scsi_cmnd *cmd = lrbp->cmd; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2318 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2319 | unsigned short cdb_len; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2320 | |
| 2321 | /* command descriptor fields */ |
| 2322 | ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD( |
| 2323 | UPIU_TRANSACTION_COMMAND, upiu_flags, |
| 2324 | lrbp->lun, lrbp->task_tag); |
| 2325 | ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD( |
| 2326 | UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0); |
| 2327 | |
| 2328 | /* Total EHS length and Data segment length will be zero */ |
| 2329 | ucd_req_ptr->header.dword_2 = 0; |
| 2330 | |
Bart Van Assche | 1b21b8f | 2019-12-24 14:02:45 -0800 | [diff] [blame] | 2331 | ucd_req_ptr->sc.exp_data_transfer_len = cpu_to_be32(cmd->sdb.length); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2332 | |
Bart Van Assche | 1b21b8f | 2019-12-24 14:02:45 -0800 | [diff] [blame] | 2333 | cdb_len = min_t(unsigned short, cmd->cmd_len, UFS_CDB_SIZE); |
Avri Altman | a851b2b | 2018-10-07 17:30:34 +0300 | [diff] [blame] | 2334 | memset(ucd_req_ptr->sc.cdb, 0, UFS_CDB_SIZE); |
Bart Van Assche | 1b21b8f | 2019-12-24 14:02:45 -0800 | [diff] [blame] | 2335 | memcpy(ucd_req_ptr->sc.cdb, cmd->cmnd, cdb_len); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2336 | |
| 2337 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2338 | } |
| 2339 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2340 | /** |
| 2341 | * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc, |
| 2342 | * for query requsts |
| 2343 | * @hba: UFS hba |
| 2344 | * @lrbp: local reference block pointer |
| 2345 | * @upiu_flags: flags |
| 2346 | */ |
| 2347 | static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba, |
Bean Huo | a23064c | 2020-07-06 14:39:36 +0200 | [diff] [blame] | 2348 | struct ufshcd_lrb *lrbp, u8 upiu_flags) |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2349 | { |
| 2350 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
| 2351 | struct ufs_query *query = &hba->dev_cmd.query; |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 2352 | u16 len = be16_to_cpu(query->request.upiu_req.length); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2353 | |
| 2354 | /* Query request header */ |
| 2355 | ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD( |
| 2356 | UPIU_TRANSACTION_QUERY_REQ, upiu_flags, |
| 2357 | lrbp->lun, lrbp->task_tag); |
| 2358 | ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD( |
| 2359 | 0, query->request.query_func, 0, 0); |
| 2360 | |
Zang Leigang | 6861285 | 2016-08-25 17:39:19 +0800 | [diff] [blame] | 2361 | /* Data segment length only need for WRITE_DESC */ |
| 2362 | if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) |
| 2363 | ucd_req_ptr->header.dword_2 = |
| 2364 | UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len); |
| 2365 | else |
| 2366 | ucd_req_ptr->header.dword_2 = 0; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2367 | |
| 2368 | /* Copy the Query Request buffer as is */ |
| 2369 | memcpy(&ucd_req_ptr->qr, &query->request.upiu_req, |
| 2370 | QUERY_OSF_SIZE); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2371 | |
| 2372 | /* Copy the Descriptor */ |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2373 | if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) |
Avri Altman | 220d17a6 | 2018-10-07 17:30:36 +0300 | [diff] [blame] | 2374 | memcpy(ucd_req_ptr + 1, query->descriptor, len); |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2375 | |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 2376 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2377 | } |
| 2378 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2379 | static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp) |
| 2380 | { |
| 2381 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
| 2382 | |
| 2383 | memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req)); |
| 2384 | |
| 2385 | /* command descriptor fields */ |
| 2386 | ucd_req_ptr->header.dword_0 = |
| 2387 | UPIU_HEADER_DWORD( |
| 2388 | UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag); |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 2389 | /* clear rest of the fields of basic header */ |
| 2390 | ucd_req_ptr->header.dword_1 = 0; |
| 2391 | ucd_req_ptr->header.dword_2 = 0; |
| 2392 | |
| 2393 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2394 | } |
| 2395 | |
| 2396 | /** |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2397 | * ufshcd_comp_devman_upiu - UFS Protocol Information Unit(UPIU) |
| 2398 | * for Device Management Purposes |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2399 | * @hba: per adapter instance |
| 2400 | * @lrbp: pointer to local reference block |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2401 | */ |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2402 | static int ufshcd_comp_devman_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2403 | { |
Bean Huo | a23064c | 2020-07-06 14:39:36 +0200 | [diff] [blame] | 2404 | u8 upiu_flags; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2405 | int ret = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2406 | |
kehuanlin | 83dc7e3 | 2017-09-06 17:58:39 +0800 | [diff] [blame] | 2407 | if ((hba->ufs_version == UFSHCI_VERSION_10) || |
| 2408 | (hba->ufs_version == UFSHCI_VERSION_11)) |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2409 | lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE; |
kehuanlin | 83dc7e3 | 2017-09-06 17:58:39 +0800 | [diff] [blame] | 2410 | else |
| 2411 | lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE; |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2412 | |
| 2413 | ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE); |
| 2414 | if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY) |
| 2415 | ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags); |
| 2416 | else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP) |
| 2417 | ufshcd_prepare_utp_nop_upiu(lrbp); |
| 2418 | else |
| 2419 | ret = -EINVAL; |
| 2420 | |
| 2421 | return ret; |
| 2422 | } |
| 2423 | |
| 2424 | /** |
| 2425 | * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU) |
| 2426 | * for SCSI Purposes |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2427 | * @hba: per adapter instance |
| 2428 | * @lrbp: pointer to local reference block |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2429 | */ |
| 2430 | static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 2431 | { |
Bean Huo | a23064c | 2020-07-06 14:39:36 +0200 | [diff] [blame] | 2432 | u8 upiu_flags; |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2433 | int ret = 0; |
| 2434 | |
kehuanlin | 83dc7e3 | 2017-09-06 17:58:39 +0800 | [diff] [blame] | 2435 | if ((hba->ufs_version == UFSHCI_VERSION_10) || |
| 2436 | (hba->ufs_version == UFSHCI_VERSION_11)) |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2437 | lrbp->command_type = UTP_CMD_TYPE_SCSI; |
kehuanlin | 83dc7e3 | 2017-09-06 17:58:39 +0800 | [diff] [blame] | 2438 | else |
| 2439 | lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE; |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2440 | |
| 2441 | if (likely(lrbp->cmd)) { |
| 2442 | ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, |
| 2443 | lrbp->cmd->sc_data_direction); |
| 2444 | ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags); |
| 2445 | } else { |
| 2446 | ret = -EINVAL; |
| 2447 | } |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2448 | |
| 2449 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2450 | } |
| 2451 | |
| 2452 | /** |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 2453 | * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2454 | * @upiu_wlun_id: UPIU W-LUN id |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 2455 | * |
| 2456 | * Returns SCSI W-LUN id |
| 2457 | */ |
| 2458 | static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id) |
| 2459 | { |
| 2460 | return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE; |
| 2461 | } |
| 2462 | |
Bart Van Assche | 4d2b8d4 | 2020-01-22 19:56:35 -0800 | [diff] [blame] | 2463 | static void ufshcd_init_lrb(struct ufs_hba *hba, struct ufshcd_lrb *lrb, int i) |
| 2464 | { |
| 2465 | struct utp_transfer_cmd_desc *cmd_descp = hba->ucdl_base_addr; |
| 2466 | struct utp_transfer_req_desc *utrdlp = hba->utrdl_base_addr; |
| 2467 | dma_addr_t cmd_desc_element_addr = hba->ucdl_dma_addr + |
| 2468 | i * sizeof(struct utp_transfer_cmd_desc); |
| 2469 | u16 response_offset = offsetof(struct utp_transfer_cmd_desc, |
| 2470 | response_upiu); |
| 2471 | u16 prdt_offset = offsetof(struct utp_transfer_cmd_desc, prd_table); |
| 2472 | |
| 2473 | lrb->utr_descriptor_ptr = utrdlp + i; |
| 2474 | lrb->utrd_dma_addr = hba->utrdl_dma_addr + |
| 2475 | i * sizeof(struct utp_transfer_req_desc); |
| 2476 | lrb->ucd_req_ptr = (struct utp_upiu_req *)(cmd_descp + i); |
| 2477 | lrb->ucd_req_dma_addr = cmd_desc_element_addr; |
| 2478 | lrb->ucd_rsp_ptr = (struct utp_upiu_rsp *)cmd_descp[i].response_upiu; |
| 2479 | lrb->ucd_rsp_dma_addr = cmd_desc_element_addr + response_offset; |
| 2480 | lrb->ucd_prdt_ptr = (struct ufshcd_sg_entry *)cmd_descp[i].prd_table; |
| 2481 | lrb->ucd_prdt_dma_addr = cmd_desc_element_addr + prdt_offset; |
| 2482 | } |
| 2483 | |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 2484 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2485 | * ufshcd_queuecommand - main entry point for SCSI requests |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2486 | * @host: SCSI host pointer |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2487 | * @cmd: command from SCSI Midlayer |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2488 | * |
| 2489 | * Returns 0 for success, non-zero in case of failure |
| 2490 | */ |
| 2491 | static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) |
| 2492 | { |
| 2493 | struct ufshcd_lrb *lrbp; |
| 2494 | struct ufs_hba *hba; |
| 2495 | unsigned long flags; |
| 2496 | int tag; |
| 2497 | int err = 0; |
| 2498 | |
| 2499 | hba = shost_priv(host); |
| 2500 | |
| 2501 | tag = cmd->request->tag; |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 2502 | if (!ufshcd_valid_tag(hba, tag)) { |
| 2503 | dev_err(hba->dev, |
| 2504 | "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p", |
| 2505 | __func__, tag, cmd, cmd->request); |
| 2506 | BUG(); |
| 2507 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2508 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 2509 | if (!down_read_trylock(&hba->clk_scaling_lock)) |
| 2510 | return SCSI_MLQUEUE_HOST_BUSY; |
| 2511 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2512 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2513 | switch (hba->ufshcd_state) { |
| 2514 | case UFSHCD_STATE_OPERATIONAL: |
| 2515 | break; |
Zang Leigang | 141f816 | 2016-11-16 11:29:37 +0800 | [diff] [blame] | 2516 | case UFSHCD_STATE_EH_SCHEDULED: |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2517 | case UFSHCD_STATE_RESET: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2518 | err = SCSI_MLQUEUE_HOST_BUSY; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2519 | goto out_unlock; |
| 2520 | case UFSHCD_STATE_ERROR: |
| 2521 | set_host_byte(cmd, DID_ERROR); |
| 2522 | cmd->scsi_done(cmd); |
| 2523 | goto out_unlock; |
| 2524 | default: |
| 2525 | dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n", |
| 2526 | __func__, hba->ufshcd_state); |
| 2527 | set_host_byte(cmd, DID_BAD_TARGET); |
| 2528 | cmd->scsi_done(cmd); |
| 2529 | goto out_unlock; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2530 | } |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 2531 | |
| 2532 | /* if error handling is in progress, don't issue commands */ |
| 2533 | if (ufshcd_eh_in_progress(hba)) { |
| 2534 | set_host_byte(cmd, DID_ERROR); |
| 2535 | cmd->scsi_done(cmd); |
| 2536 | goto out_unlock; |
| 2537 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2538 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2539 | |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 2540 | hba->req_abort_count = 0; |
| 2541 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2542 | err = ufshcd_hold(hba, true); |
| 2543 | if (err) { |
| 2544 | err = SCSI_MLQUEUE_HOST_BUSY; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2545 | goto out; |
| 2546 | } |
| 2547 | WARN_ON(hba->clk_gating.state != CLKS_ON); |
| 2548 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2549 | lrbp = &hba->lrb[tag]; |
| 2550 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2551 | WARN_ON(lrbp->cmd); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2552 | lrbp->cmd = cmd; |
Avri Altman | 09a5a24 | 2018-11-22 20:04:56 +0200 | [diff] [blame] | 2553 | lrbp->sense_bufflen = UFS_SENSE_SIZE; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2554 | lrbp->sense_buffer = cmd->sense_buffer; |
| 2555 | lrbp->task_tag = tag; |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 2556 | lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun); |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 2557 | lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false; |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 2558 | |
| 2559 | ufshcd_prepare_lrbp_crypto(cmd->request, lrbp); |
| 2560 | |
Gilad Broner | e0b299e | 2017-02-03 16:56:40 -0800 | [diff] [blame] | 2561 | lrbp->req_abort_skip = false; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2562 | |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2563 | ufshcd_comp_scsi_upiu(hba, lrbp); |
| 2564 | |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 2565 | err = ufshcd_map_sg(hba, lrbp); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2566 | if (err) { |
| 2567 | lrbp->cmd = NULL; |
Can Guo | 17c7d35 | 2019-12-05 02:14:33 +0000 | [diff] [blame] | 2568 | ufshcd_release(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2569 | goto out; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2570 | } |
Gilad Broner | ad1a1b9 | 2016-10-17 17:09:36 -0700 | [diff] [blame] | 2571 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 2572 | wmb(); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2573 | |
| 2574 | /* issue command to the controller */ |
| 2575 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2576 | ufshcd_send_command(hba, tag); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2577 | out_unlock: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2578 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2579 | out: |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 2580 | up_read(&hba->clk_scaling_lock); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2581 | return err; |
| 2582 | } |
| 2583 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2584 | static int ufshcd_compose_dev_cmd(struct ufs_hba *hba, |
| 2585 | struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag) |
| 2586 | { |
| 2587 | lrbp->cmd = NULL; |
| 2588 | lrbp->sense_bufflen = 0; |
| 2589 | lrbp->sense_buffer = NULL; |
| 2590 | lrbp->task_tag = tag; |
| 2591 | lrbp->lun = 0; /* device management cmd is not specific to any LUN */ |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2592 | lrbp->intr_cmd = true; /* No interrupt aggregation */ |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 2593 | ufshcd_prepare_lrbp_crypto(NULL, lrbp); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2594 | hba->dev_cmd.type = cmd_type; |
| 2595 | |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2596 | return ufshcd_comp_devman_upiu(hba, lrbp); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2597 | } |
| 2598 | |
| 2599 | static int |
| 2600 | ufshcd_clear_cmd(struct ufs_hba *hba, int tag) |
| 2601 | { |
| 2602 | int err = 0; |
| 2603 | unsigned long flags; |
| 2604 | u32 mask = 1 << tag; |
| 2605 | |
| 2606 | /* clear outstanding transaction before retry */ |
| 2607 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2608 | ufshcd_utrl_clear(hba, tag); |
| 2609 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2610 | |
| 2611 | /* |
| 2612 | * wait for for h/w to clear corresponding bit in door-bell. |
| 2613 | * max. wait is 1 sec. |
| 2614 | */ |
| 2615 | err = ufshcd_wait_for_register(hba, |
| 2616 | REG_UTP_TRANSFER_REQ_DOOR_BELL, |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 2617 | mask, ~mask, 1000, 1000); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2618 | |
| 2619 | return err; |
| 2620 | } |
| 2621 | |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2622 | static int |
| 2623 | ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 2624 | { |
| 2625 | struct ufs_query_res *query_res = &hba->dev_cmd.query.response; |
| 2626 | |
| 2627 | /* Get the UPIU response */ |
| 2628 | query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >> |
| 2629 | UPIU_RSP_CODE_OFFSET; |
| 2630 | return query_res->response; |
| 2631 | } |
| 2632 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2633 | /** |
| 2634 | * ufshcd_dev_cmd_completion() - handles device management command responses |
| 2635 | * @hba: per adapter instance |
| 2636 | * @lrbp: pointer to local reference block |
| 2637 | */ |
| 2638 | static int |
| 2639 | ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 2640 | { |
| 2641 | int resp; |
| 2642 | int err = 0; |
| 2643 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 2644 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2645 | resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr); |
| 2646 | |
| 2647 | switch (resp) { |
| 2648 | case UPIU_TRANSACTION_NOP_IN: |
| 2649 | if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) { |
| 2650 | err = -EINVAL; |
| 2651 | dev_err(hba->dev, "%s: unexpected response %x\n", |
| 2652 | __func__, resp); |
| 2653 | } |
| 2654 | break; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2655 | case UPIU_TRANSACTION_QUERY_RSP: |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2656 | err = ufshcd_check_query_response(hba, lrbp); |
| 2657 | if (!err) |
| 2658 | err = ufshcd_copy_query_response(hba, lrbp); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2659 | break; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2660 | case UPIU_TRANSACTION_REJECT_UPIU: |
| 2661 | /* TODO: handle Reject UPIU Response */ |
| 2662 | err = -EPERM; |
| 2663 | dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n", |
| 2664 | __func__); |
| 2665 | break; |
| 2666 | default: |
| 2667 | err = -EINVAL; |
| 2668 | dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n", |
| 2669 | __func__, resp); |
| 2670 | break; |
| 2671 | } |
| 2672 | |
| 2673 | return err; |
| 2674 | } |
| 2675 | |
| 2676 | static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba, |
| 2677 | struct ufshcd_lrb *lrbp, int max_timeout) |
| 2678 | { |
| 2679 | int err = 0; |
| 2680 | unsigned long time_left; |
| 2681 | unsigned long flags; |
| 2682 | |
| 2683 | time_left = wait_for_completion_timeout(hba->dev_cmd.complete, |
| 2684 | msecs_to_jiffies(max_timeout)); |
| 2685 | |
Gilad Broner | ad1a1b9 | 2016-10-17 17:09:36 -0700 | [diff] [blame] | 2686 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 2687 | wmb(); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2688 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2689 | hba->dev_cmd.complete = NULL; |
| 2690 | if (likely(time_left)) { |
| 2691 | err = ufshcd_get_tr_ocs(lrbp); |
| 2692 | if (!err) |
| 2693 | err = ufshcd_dev_cmd_completion(hba, lrbp); |
| 2694 | } |
| 2695 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2696 | |
| 2697 | if (!time_left) { |
| 2698 | err = -ETIMEDOUT; |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 2699 | dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n", |
| 2700 | __func__, lrbp->task_tag); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2701 | if (!ufshcd_clear_cmd(hba, lrbp->task_tag)) |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 2702 | /* successfully cleared the command, retry if needed */ |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2703 | err = -EAGAIN; |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 2704 | /* |
| 2705 | * in case of an error, after clearing the doorbell, |
| 2706 | * we also need to clear the outstanding_request |
| 2707 | * field in hba |
| 2708 | */ |
| 2709 | ufshcd_outstanding_req_clear(hba, lrbp->task_tag); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | return err; |
| 2713 | } |
| 2714 | |
| 2715 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2716 | * ufshcd_exec_dev_cmd - API for sending device management requests |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2717 | * @hba: UFS hba |
| 2718 | * @cmd_type: specifies the type (NOP, Query...) |
| 2719 | * @timeout: time in seconds |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2720 | * |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2721 | * NOTE: Since there is only one available tag for device management commands, |
| 2722 | * it is expected you hold the hba->dev_cmd.lock mutex. |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2723 | */ |
| 2724 | static int ufshcd_exec_dev_cmd(struct ufs_hba *hba, |
| 2725 | enum dev_cmd_type cmd_type, int timeout) |
| 2726 | { |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 2727 | struct request_queue *q = hba->cmd_queue; |
| 2728 | struct request *req; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2729 | struct ufshcd_lrb *lrbp; |
| 2730 | int err; |
| 2731 | int tag; |
| 2732 | struct completion wait; |
| 2733 | unsigned long flags; |
| 2734 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 2735 | down_read(&hba->clk_scaling_lock); |
| 2736 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2737 | /* |
| 2738 | * Get free slot, sleep if slots are unavailable. |
| 2739 | * Even though we use wait_event() which sleeps indefinitely, |
| 2740 | * the maximum wait time is bounded by SCSI request timeout. |
| 2741 | */ |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 2742 | req = blk_get_request(q, REQ_OP_DRV_OUT, 0); |
Dan Carpenter | bb14dd1 | 2019-12-13 13:48:28 +0300 | [diff] [blame] | 2743 | if (IS_ERR(req)) { |
| 2744 | err = PTR_ERR(req); |
| 2745 | goto out_unlock; |
| 2746 | } |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 2747 | tag = req->tag; |
| 2748 | WARN_ON_ONCE(!ufshcd_valid_tag(hba, tag)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2749 | |
| 2750 | init_completion(&wait); |
| 2751 | lrbp = &hba->lrb[tag]; |
| 2752 | WARN_ON(lrbp->cmd); |
| 2753 | err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag); |
| 2754 | if (unlikely(err)) |
| 2755 | goto out_put_tag; |
| 2756 | |
| 2757 | hba->dev_cmd.complete = &wait; |
| 2758 | |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 2759 | ufshcd_add_query_upiu_trace(hba, tag, "query_send"); |
Yaniv Gardi | e3dfdc5 | 2016-02-01 15:02:49 +0200 | [diff] [blame] | 2760 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 2761 | wmb(); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2762 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2763 | ufshcd_send_command(hba, tag); |
| 2764 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2765 | |
| 2766 | err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout); |
| 2767 | |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 2768 | ufshcd_add_query_upiu_trace(hba, tag, |
| 2769 | err ? "query_complete_err" : "query_complete"); |
| 2770 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2771 | out_put_tag: |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 2772 | blk_put_request(req); |
Dan Carpenter | bb14dd1 | 2019-12-13 13:48:28 +0300 | [diff] [blame] | 2773 | out_unlock: |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 2774 | up_read(&hba->clk_scaling_lock); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2775 | return err; |
| 2776 | } |
| 2777 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2778 | /** |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2779 | * ufshcd_init_query() - init the query response and request parameters |
| 2780 | * @hba: per-adapter instance |
| 2781 | * @request: address of the request pointer to be initialized |
| 2782 | * @response: address of the response pointer to be initialized |
| 2783 | * @opcode: operation to perform |
| 2784 | * @idn: flag idn to access |
| 2785 | * @index: LU number to access |
| 2786 | * @selector: query/flag/descriptor further identification |
| 2787 | */ |
| 2788 | static inline void ufshcd_init_query(struct ufs_hba *hba, |
| 2789 | struct ufs_query_req **request, struct ufs_query_res **response, |
| 2790 | enum query_opcode opcode, u8 idn, u8 index, u8 selector) |
| 2791 | { |
| 2792 | *request = &hba->dev_cmd.query.request; |
| 2793 | *response = &hba->dev_cmd.query.response; |
| 2794 | memset(*request, 0, sizeof(struct ufs_query_req)); |
| 2795 | memset(*response, 0, sizeof(struct ufs_query_res)); |
| 2796 | (*request)->upiu_req.opcode = opcode; |
| 2797 | (*request)->upiu_req.idn = idn; |
| 2798 | (*request)->upiu_req.index = index; |
| 2799 | (*request)->upiu_req.selector = selector; |
| 2800 | } |
| 2801 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 2802 | static int ufshcd_query_flag_retry(struct ufs_hba *hba, |
Stanley Chu | 1f34eed | 2020-05-08 16:01:12 +0800 | [diff] [blame] | 2803 | enum query_opcode opcode, enum flag_idn idn, u8 index, bool *flag_res) |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 2804 | { |
| 2805 | int ret; |
| 2806 | int retries; |
| 2807 | |
| 2808 | for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) { |
Stanley Chu | 1f34eed | 2020-05-08 16:01:12 +0800 | [diff] [blame] | 2809 | ret = ufshcd_query_flag(hba, opcode, idn, index, flag_res); |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 2810 | if (ret) |
| 2811 | dev_dbg(hba->dev, |
| 2812 | "%s: failed with error %d, retries %d\n", |
| 2813 | __func__, ret, retries); |
| 2814 | else |
| 2815 | break; |
| 2816 | } |
| 2817 | |
| 2818 | if (ret) |
| 2819 | dev_err(hba->dev, |
| 2820 | "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n", |
| 2821 | __func__, opcode, idn, ret, retries); |
| 2822 | return ret; |
| 2823 | } |
| 2824 | |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2825 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2826 | * ufshcd_query_flag() - API function for sending flag query requests |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2827 | * @hba: per-adapter instance |
| 2828 | * @opcode: flag query to perform |
| 2829 | * @idn: flag idn to access |
Stanley Chu | 1f34eed | 2020-05-08 16:01:12 +0800 | [diff] [blame] | 2830 | * @index: flag index to access |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2831 | * @flag_res: the flag value after the query request completes |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2832 | * |
| 2833 | * Returns 0 for success, non-zero in case of failure |
| 2834 | */ |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 2835 | int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode, |
Stanley Chu | 1f34eed | 2020-05-08 16:01:12 +0800 | [diff] [blame] | 2836 | enum flag_idn idn, u8 index, bool *flag_res) |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2837 | { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2838 | struct ufs_query_req *request = NULL; |
| 2839 | struct ufs_query_res *response = NULL; |
Stanley Chu | 1f34eed | 2020-05-08 16:01:12 +0800 | [diff] [blame] | 2840 | int err, selector = 0; |
Yaniv Gardi | e5ad406 | 2016-02-01 15:02:41 +0200 | [diff] [blame] | 2841 | int timeout = QUERY_REQ_TIMEOUT; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2842 | |
| 2843 | BUG_ON(!hba); |
| 2844 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2845 | ufshcd_hold(hba, false); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2846 | mutex_lock(&hba->dev_cmd.lock); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2847 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 2848 | selector); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2849 | |
| 2850 | switch (opcode) { |
| 2851 | case UPIU_QUERY_OPCODE_SET_FLAG: |
| 2852 | case UPIU_QUERY_OPCODE_CLEAR_FLAG: |
| 2853 | case UPIU_QUERY_OPCODE_TOGGLE_FLAG: |
| 2854 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
| 2855 | break; |
| 2856 | case UPIU_QUERY_OPCODE_READ_FLAG: |
| 2857 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 2858 | if (!flag_res) { |
| 2859 | /* No dummy reads */ |
| 2860 | dev_err(hba->dev, "%s: Invalid argument for read request\n", |
| 2861 | __func__); |
| 2862 | err = -EINVAL; |
| 2863 | goto out_unlock; |
| 2864 | } |
| 2865 | break; |
| 2866 | default: |
| 2867 | dev_err(hba->dev, |
| 2868 | "%s: Expected query flag opcode but got = %d\n", |
| 2869 | __func__, opcode); |
| 2870 | err = -EINVAL; |
| 2871 | goto out_unlock; |
| 2872 | } |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2873 | |
Yaniv Gardi | e5ad406 | 2016-02-01 15:02:41 +0200 | [diff] [blame] | 2874 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2875 | |
| 2876 | if (err) { |
| 2877 | dev_err(hba->dev, |
| 2878 | "%s: Sending flag query for idn %d failed, err = %d\n", |
| 2879 | __func__, idn, err); |
| 2880 | goto out_unlock; |
| 2881 | } |
| 2882 | |
| 2883 | if (flag_res) |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 2884 | *flag_res = (be32_to_cpu(response->upiu_res.value) & |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2885 | MASK_QUERY_UPIU_FLAG_LOC) & 0x1; |
| 2886 | |
| 2887 | out_unlock: |
| 2888 | mutex_unlock(&hba->dev_cmd.lock); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2889 | ufshcd_release(hba); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2890 | return err; |
| 2891 | } |
| 2892 | |
| 2893 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2894 | * ufshcd_query_attr - API function for sending attribute requests |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2895 | * @hba: per-adapter instance |
| 2896 | * @opcode: attribute opcode |
| 2897 | * @idn: attribute idn to access |
| 2898 | * @index: index field |
| 2899 | * @selector: selector field |
| 2900 | * @attr_val: the attribute value after the query request completes |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2901 | * |
| 2902 | * Returns 0 for success, non-zero in case of failure |
| 2903 | */ |
Stanislav Nijnikov | ec92b59 | 2018-02-15 14:14:11 +0200 | [diff] [blame] | 2904 | int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode, |
| 2905 | enum attr_idn idn, u8 index, u8 selector, u32 *attr_val) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2906 | { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2907 | struct ufs_query_req *request = NULL; |
| 2908 | struct ufs_query_res *response = NULL; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2909 | int err; |
| 2910 | |
| 2911 | BUG_ON(!hba); |
| 2912 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2913 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2914 | if (!attr_val) { |
| 2915 | dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n", |
| 2916 | __func__, opcode); |
| 2917 | err = -EINVAL; |
| 2918 | goto out; |
| 2919 | } |
| 2920 | |
| 2921 | mutex_lock(&hba->dev_cmd.lock); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2922 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 2923 | selector); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2924 | |
| 2925 | switch (opcode) { |
| 2926 | case UPIU_QUERY_OPCODE_WRITE_ATTR: |
| 2927 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 2928 | request->upiu_req.value = cpu_to_be32(*attr_val); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2929 | break; |
| 2930 | case UPIU_QUERY_OPCODE_READ_ATTR: |
| 2931 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 2932 | break; |
| 2933 | default: |
| 2934 | dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n", |
| 2935 | __func__, opcode); |
| 2936 | err = -EINVAL; |
| 2937 | goto out_unlock; |
| 2938 | } |
| 2939 | |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2940 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2941 | |
| 2942 | if (err) { |
Yaniv Gardi | 4b761b5 | 2016-11-23 16:31:18 -0800 | [diff] [blame] | 2943 | dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n", |
| 2944 | __func__, opcode, idn, index, err); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2945 | goto out_unlock; |
| 2946 | } |
| 2947 | |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 2948 | *attr_val = be32_to_cpu(response->upiu_res.value); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2949 | |
| 2950 | out_unlock: |
| 2951 | mutex_unlock(&hba->dev_cmd.lock); |
| 2952 | out: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2953 | ufshcd_release(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2954 | return err; |
| 2955 | } |
| 2956 | |
| 2957 | /** |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 2958 | * ufshcd_query_attr_retry() - API function for sending query |
| 2959 | * attribute with retries |
| 2960 | * @hba: per-adapter instance |
| 2961 | * @opcode: attribute opcode |
| 2962 | * @idn: attribute idn to access |
| 2963 | * @index: index field |
| 2964 | * @selector: selector field |
| 2965 | * @attr_val: the attribute value after the query request |
| 2966 | * completes |
| 2967 | * |
| 2968 | * Returns 0 for success, non-zero in case of failure |
| 2969 | */ |
| 2970 | static int ufshcd_query_attr_retry(struct ufs_hba *hba, |
| 2971 | enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector, |
| 2972 | u32 *attr_val) |
| 2973 | { |
| 2974 | int ret = 0; |
| 2975 | u32 retries; |
| 2976 | |
Bart Van Assche | 68c9fcf | 2019-12-24 14:02:43 -0800 | [diff] [blame] | 2977 | for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 2978 | ret = ufshcd_query_attr(hba, opcode, idn, index, |
| 2979 | selector, attr_val); |
| 2980 | if (ret) |
| 2981 | dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n", |
| 2982 | __func__, ret, retries); |
| 2983 | else |
| 2984 | break; |
| 2985 | } |
| 2986 | |
| 2987 | if (ret) |
| 2988 | dev_err(hba->dev, |
| 2989 | "%s: query attribute, idn %d, failed with error %d after %d retires\n", |
| 2990 | __func__, idn, ret, QUERY_REQ_RETRIES); |
| 2991 | return ret; |
| 2992 | } |
| 2993 | |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 2994 | static int __ufshcd_query_descriptor(struct ufs_hba *hba, |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2995 | enum query_opcode opcode, enum desc_idn idn, u8 index, |
| 2996 | u8 selector, u8 *desc_buf, int *buf_len) |
| 2997 | { |
| 2998 | struct ufs_query_req *request = NULL; |
| 2999 | struct ufs_query_res *response = NULL; |
| 3000 | int err; |
| 3001 | |
| 3002 | BUG_ON(!hba); |
| 3003 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3004 | ufshcd_hold(hba, false); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3005 | if (!desc_buf) { |
| 3006 | dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n", |
| 3007 | __func__, opcode); |
| 3008 | err = -EINVAL; |
| 3009 | goto out; |
| 3010 | } |
| 3011 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3012 | if (*buf_len < QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3013 | dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n", |
| 3014 | __func__, *buf_len); |
| 3015 | err = -EINVAL; |
| 3016 | goto out; |
| 3017 | } |
| 3018 | |
| 3019 | mutex_lock(&hba->dev_cmd.lock); |
| 3020 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 3021 | selector); |
| 3022 | hba->dev_cmd.query.descriptor = desc_buf; |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 3023 | request->upiu_req.length = cpu_to_be16(*buf_len); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3024 | |
| 3025 | switch (opcode) { |
| 3026 | case UPIU_QUERY_OPCODE_WRITE_DESC: |
| 3027 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
| 3028 | break; |
| 3029 | case UPIU_QUERY_OPCODE_READ_DESC: |
| 3030 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 3031 | break; |
| 3032 | default: |
| 3033 | dev_err(hba->dev, |
| 3034 | "%s: Expected query descriptor opcode but got = 0x%.2x\n", |
| 3035 | __func__, opcode); |
| 3036 | err = -EINVAL; |
| 3037 | goto out_unlock; |
| 3038 | } |
| 3039 | |
| 3040 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT); |
| 3041 | |
| 3042 | if (err) { |
Yaniv Gardi | 4b761b5 | 2016-11-23 16:31:18 -0800 | [diff] [blame] | 3043 | dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n", |
| 3044 | __func__, opcode, idn, index, err); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3045 | goto out_unlock; |
| 3046 | } |
| 3047 | |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 3048 | *buf_len = be16_to_cpu(response->upiu_res.length); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3049 | |
| 3050 | out_unlock: |
Bean Huo | cfcbae3 | 2019-11-12 23:34:36 +0100 | [diff] [blame] | 3051 | hba->dev_cmd.query.descriptor = NULL; |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3052 | mutex_unlock(&hba->dev_cmd.lock); |
| 3053 | out: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3054 | ufshcd_release(hba); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3055 | return err; |
| 3056 | } |
| 3057 | |
| 3058 | /** |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 3059 | * ufshcd_query_descriptor_retry - API function for sending descriptor requests |
| 3060 | * @hba: per-adapter instance |
| 3061 | * @opcode: attribute opcode |
| 3062 | * @idn: attribute idn to access |
| 3063 | * @index: index field |
| 3064 | * @selector: selector field |
| 3065 | * @desc_buf: the buffer that contains the descriptor |
| 3066 | * @buf_len: length parameter passed to the device |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3067 | * |
| 3068 | * Returns 0 for success, non-zero in case of failure. |
| 3069 | * The buf_len parameter will contain, on return, the length parameter |
| 3070 | * received on the response. |
| 3071 | */ |
Stanislav Nijnikov | 2238d31 | 2018-02-15 14:14:07 +0200 | [diff] [blame] | 3072 | int ufshcd_query_descriptor_retry(struct ufs_hba *hba, |
| 3073 | enum query_opcode opcode, |
| 3074 | enum desc_idn idn, u8 index, |
| 3075 | u8 selector, |
| 3076 | u8 *desc_buf, int *buf_len) |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3077 | { |
| 3078 | int err; |
| 3079 | int retries; |
| 3080 | |
| 3081 | for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { |
| 3082 | err = __ufshcd_query_descriptor(hba, opcode, idn, index, |
| 3083 | selector, desc_buf, buf_len); |
| 3084 | if (!err || err == -EINVAL) |
| 3085 | break; |
| 3086 | } |
| 3087 | |
| 3088 | return err; |
| 3089 | } |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3090 | |
| 3091 | /** |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3092 | * ufshcd_map_desc_id_to_length - map descriptor IDN to its length |
| 3093 | * @hba: Pointer to adapter instance |
| 3094 | * @desc_id: descriptor idn value |
| 3095 | * @desc_len: mapped desc length (out) |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3096 | */ |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 3097 | void ufshcd_map_desc_id_to_length(struct ufs_hba *hba, enum desc_idn desc_id, |
| 3098 | int *desc_len) |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3099 | { |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 3100 | if (desc_id >= QUERY_DESC_IDN_MAX || desc_id == QUERY_DESC_IDN_RFU_0 || |
| 3101 | desc_id == QUERY_DESC_IDN_RFU_1) |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3102 | *desc_len = 0; |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 3103 | else |
| 3104 | *desc_len = hba->desc_size[desc_id]; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3105 | } |
| 3106 | EXPORT_SYMBOL(ufshcd_map_desc_id_to_length); |
| 3107 | |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 3108 | static void ufshcd_update_desc_length(struct ufs_hba *hba, |
Bean Huo | 72fb690 | 2020-06-03 11:19:59 +0200 | [diff] [blame] | 3109 | enum desc_idn desc_id, int desc_index, |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 3110 | unsigned char desc_len) |
| 3111 | { |
| 3112 | if (hba->desc_size[desc_id] == QUERY_DESC_MAX_SIZE && |
Bean Huo | 72fb690 | 2020-06-03 11:19:59 +0200 | [diff] [blame] | 3113 | desc_id != QUERY_DESC_IDN_STRING && desc_index != UFS_RPMB_UNIT) |
| 3114 | /* For UFS 3.1, the normal unit descriptor is 10 bytes larger |
| 3115 | * than the RPMB unit, however, both descriptors share the same |
| 3116 | * desc_idn, to cover both unit descriptors with one length, we |
| 3117 | * choose the normal unit descriptor length by desc_index. |
| 3118 | */ |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 3119 | hba->desc_size[desc_id] = desc_len; |
| 3120 | } |
| 3121 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3122 | /** |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3123 | * ufshcd_read_desc_param - read the specified descriptor parameter |
| 3124 | * @hba: Pointer to adapter instance |
| 3125 | * @desc_id: descriptor idn value |
| 3126 | * @desc_index: descriptor index |
| 3127 | * @param_offset: offset of the parameter to read |
| 3128 | * @param_read_buf: pointer to buffer where parameter would be read |
| 3129 | * @param_size: sizeof(param_read_buf) |
| 3130 | * |
| 3131 | * Return 0 in case of success, non-zero otherwise |
| 3132 | */ |
Stanislav Nijnikov | 45bced8 | 2018-02-15 14:14:02 +0200 | [diff] [blame] | 3133 | int ufshcd_read_desc_param(struct ufs_hba *hba, |
| 3134 | enum desc_idn desc_id, |
| 3135 | int desc_index, |
| 3136 | u8 param_offset, |
| 3137 | u8 *param_read_buf, |
| 3138 | u8 param_size) |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3139 | { |
| 3140 | int ret; |
| 3141 | u8 *desc_buf; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3142 | int buff_len; |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3143 | bool is_kmalloc = true; |
| 3144 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3145 | /* Safety check */ |
| 3146 | if (desc_id >= QUERY_DESC_IDN_MAX || !param_size) |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3147 | return -EINVAL; |
| 3148 | |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 3149 | /* Get the length of descriptor */ |
| 3150 | ufshcd_map_desc_id_to_length(hba, desc_id, &buff_len); |
| 3151 | if (!buff_len) { |
| 3152 | dev_err(hba->dev, "%s: Failed to get desc length", __func__); |
| 3153 | return -EINVAL; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
| 3156 | /* Check whether we need temp memory */ |
| 3157 | if (param_offset != 0 || param_size < buff_len) { |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3158 | desc_buf = kmalloc(buff_len, GFP_KERNEL); |
| 3159 | if (!desc_buf) |
| 3160 | return -ENOMEM; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3161 | } else { |
| 3162 | desc_buf = param_read_buf; |
| 3163 | is_kmalloc = false; |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3164 | } |
| 3165 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3166 | /* Request for full descriptor */ |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3167 | ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC, |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3168 | desc_id, desc_index, 0, |
| 3169 | desc_buf, &buff_len); |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3170 | |
subhashj@codeaurora.org | bde44bb | 2016-11-23 16:31:41 -0800 | [diff] [blame] | 3171 | if (ret) { |
| 3172 | dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d", |
| 3173 | __func__, desc_id, desc_index, param_offset, ret); |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3174 | goto out; |
| 3175 | } |
| 3176 | |
subhashj@codeaurora.org | bde44bb | 2016-11-23 16:31:41 -0800 | [diff] [blame] | 3177 | /* Sanity check */ |
| 3178 | if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) { |
| 3179 | dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header", |
| 3180 | __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]); |
| 3181 | ret = -EINVAL; |
| 3182 | goto out; |
| 3183 | } |
| 3184 | |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 3185 | /* Update descriptor length */ |
| 3186 | buff_len = desc_buf[QUERY_DESC_LENGTH_OFFSET]; |
Bean Huo | 72fb690 | 2020-06-03 11:19:59 +0200 | [diff] [blame] | 3187 | ufshcd_update_desc_length(hba, desc_id, desc_index, buff_len); |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 3188 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3189 | /* Check wherher we will not copy more data, than available */ |
Bean Huo | cbe193f | 2020-06-03 11:19:57 +0200 | [diff] [blame] | 3190 | if (is_kmalloc && (param_offset + param_size) > buff_len) |
| 3191 | param_size = buff_len - param_offset; |
subhashj@codeaurora.org | bde44bb | 2016-11-23 16:31:41 -0800 | [diff] [blame] | 3192 | |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3193 | if (is_kmalloc) |
| 3194 | memcpy(param_read_buf, &desc_buf[param_offset], param_size); |
| 3195 | out: |
| 3196 | if (is_kmalloc) |
| 3197 | kfree(desc_buf); |
| 3198 | return ret; |
| 3199 | } |
| 3200 | |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3201 | /** |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3202 | * struct uc_string_id - unicode string |
| 3203 | * |
| 3204 | * @len: size of this descriptor inclusive |
| 3205 | * @type: descriptor type |
| 3206 | * @uc: unicode string character |
| 3207 | */ |
| 3208 | struct uc_string_id { |
| 3209 | u8 len; |
| 3210 | u8 type; |
Gustavo A. R. Silva | ec38c0a | 2020-05-07 14:25:50 -0500 | [diff] [blame] | 3211 | wchar_t uc[]; |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3212 | } __packed; |
| 3213 | |
| 3214 | /* replace non-printable or non-ASCII characters with spaces */ |
| 3215 | static inline char ufshcd_remove_non_printable(u8 ch) |
| 3216 | { |
| 3217 | return (ch >= 0x20 && ch <= 0x7e) ? ch : ' '; |
| 3218 | } |
| 3219 | |
| 3220 | /** |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3221 | * ufshcd_read_string_desc - read string descriptor |
| 3222 | * @hba: pointer to adapter instance |
| 3223 | * @desc_index: descriptor index |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3224 | * @buf: pointer to buffer where descriptor would be read, |
| 3225 | * the caller should free the memory. |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3226 | * @ascii: if true convert from unicode to ascii characters |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3227 | * null terminated string. |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3228 | * |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3229 | * Return: |
| 3230 | * * string size on success. |
| 3231 | * * -ENOMEM: on allocation failure |
| 3232 | * * -EINVAL: on a wrong parameter |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3233 | */ |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3234 | int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index, |
| 3235 | u8 **buf, bool ascii) |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3236 | { |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3237 | struct uc_string_id *uc_str; |
| 3238 | u8 *str; |
| 3239 | int ret; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3240 | |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3241 | if (!buf) |
| 3242 | return -EINVAL; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3243 | |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3244 | uc_str = kzalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL); |
| 3245 | if (!uc_str) |
| 3246 | return -ENOMEM; |
| 3247 | |
Bean Huo | c4607a0 | 2020-06-03 11:19:56 +0200 | [diff] [blame] | 3248 | ret = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_STRING, desc_index, 0, |
| 3249 | (u8 *)uc_str, QUERY_DESC_MAX_SIZE); |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3250 | if (ret < 0) { |
| 3251 | dev_err(hba->dev, "Reading String Desc failed after %d retries. err = %d\n", |
| 3252 | QUERY_REQ_RETRIES, ret); |
| 3253 | str = NULL; |
| 3254 | goto out; |
| 3255 | } |
| 3256 | |
| 3257 | if (uc_str->len <= QUERY_DESC_HDR_SIZE) { |
| 3258 | dev_dbg(hba->dev, "String Desc is of zero length\n"); |
| 3259 | str = NULL; |
| 3260 | ret = 0; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3261 | goto out; |
| 3262 | } |
| 3263 | |
| 3264 | if (ascii) { |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3265 | ssize_t ascii_len; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3266 | int i; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3267 | /* remove header and divide by 2 to move from UTF16 to UTF8 */ |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3268 | ascii_len = (uc_str->len - QUERY_DESC_HDR_SIZE) / 2 + 1; |
| 3269 | str = kzalloc(ascii_len, GFP_KERNEL); |
| 3270 | if (!str) { |
| 3271 | ret = -ENOMEM; |
Tiezhu Yang | fcbefc3 | 2016-06-25 12:35:22 +0800 | [diff] [blame] | 3272 | goto out; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | /* |
| 3276 | * the descriptor contains string in UTF16 format |
| 3277 | * we need to convert to utf-8 so it can be displayed |
| 3278 | */ |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3279 | ret = utf16s_to_utf8s(uc_str->uc, |
| 3280 | uc_str->len - QUERY_DESC_HDR_SIZE, |
| 3281 | UTF16_BIG_ENDIAN, str, ascii_len); |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3282 | |
| 3283 | /* replace non-printable or non-ASCII characters with spaces */ |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3284 | for (i = 0; i < ret; i++) |
| 3285 | str[i] = ufshcd_remove_non_printable(str[i]); |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3286 | |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3287 | str[ret++] = '\0'; |
| 3288 | |
| 3289 | } else { |
YueHaibing | 5f57704 | 2019-08-31 12:44:24 +0000 | [diff] [blame] | 3290 | str = kmemdup(uc_str, uc_str->len, GFP_KERNEL); |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3291 | if (!str) { |
| 3292 | ret = -ENOMEM; |
| 3293 | goto out; |
| 3294 | } |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3295 | ret = uc_str->len; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3296 | } |
| 3297 | out: |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3298 | *buf = str; |
| 3299 | kfree(uc_str); |
| 3300 | return ret; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3301 | } |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3302 | |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3303 | /** |
| 3304 | * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter |
| 3305 | * @hba: Pointer to adapter instance |
| 3306 | * @lun: lun id |
| 3307 | * @param_offset: offset of the parameter to read |
| 3308 | * @param_read_buf: pointer to buffer where parameter would be read |
| 3309 | * @param_size: sizeof(param_read_buf) |
| 3310 | * |
| 3311 | * Return 0 in case of success, non-zero otherwise |
| 3312 | */ |
| 3313 | static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba, |
| 3314 | int lun, |
| 3315 | enum unit_desc_param param_offset, |
| 3316 | u8 *param_read_buf, |
| 3317 | u32 param_size) |
| 3318 | { |
| 3319 | /* |
| 3320 | * Unit descriptors are only available for general purpose LUs (LUN id |
| 3321 | * from 0 to 7) and RPMB Well known LU. |
| 3322 | */ |
Bean Huo | 1baa801 | 2020-01-20 14:08:20 +0100 | [diff] [blame] | 3323 | if (!ufs_is_valid_unit_desc_lun(&hba->dev_info, lun)) |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3324 | return -EOPNOTSUPP; |
| 3325 | |
| 3326 | return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun, |
| 3327 | param_offset, param_read_buf, param_size); |
| 3328 | } |
| 3329 | |
Can Guo | 09f1779 | 2020-02-10 19:40:49 -0800 | [diff] [blame] | 3330 | static int ufshcd_get_ref_clk_gating_wait(struct ufs_hba *hba) |
| 3331 | { |
| 3332 | int err = 0; |
| 3333 | u32 gating_wait = UFSHCD_REF_CLK_GATING_WAIT_US; |
| 3334 | |
| 3335 | if (hba->dev_info.wspecversion >= 0x300) { |
| 3336 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
| 3337 | QUERY_ATTR_IDN_REF_CLK_GATING_WAIT_TIME, 0, 0, |
| 3338 | &gating_wait); |
| 3339 | if (err) |
| 3340 | dev_err(hba->dev, "Failed reading bRefClkGatingWait. err = %d, use default %uus\n", |
| 3341 | err, gating_wait); |
| 3342 | |
| 3343 | if (gating_wait == 0) { |
| 3344 | gating_wait = UFSHCD_REF_CLK_GATING_WAIT_US; |
| 3345 | dev_err(hba->dev, "Undefined ref clk gating wait time, use default %uus\n", |
| 3346 | gating_wait); |
| 3347 | } |
| 3348 | |
| 3349 | hba->dev_info.clk_gating_wait_us = gating_wait; |
| 3350 | } |
| 3351 | |
| 3352 | return err; |
| 3353 | } |
| 3354 | |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3355 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3356 | * ufshcd_memory_alloc - allocate memory for host memory space data structures |
| 3357 | * @hba: per adapter instance |
| 3358 | * |
| 3359 | * 1. Allocate DMA memory for Command Descriptor array |
| 3360 | * Each command descriptor consist of Command UPIU, Response UPIU and PRDT |
| 3361 | * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL). |
| 3362 | * 3. Allocate DMA memory for UTP Task Management Request Descriptor List |
| 3363 | * (UTMRDL) |
| 3364 | * 4. Allocate memory for local reference block(lrb). |
| 3365 | * |
| 3366 | * Returns 0 for success, non-zero in case of failure |
| 3367 | */ |
| 3368 | static int ufshcd_memory_alloc(struct ufs_hba *hba) |
| 3369 | { |
| 3370 | size_t utmrdl_size, utrdl_size, ucdl_size; |
| 3371 | |
| 3372 | /* Allocate memory for UTP command descriptors */ |
| 3373 | ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3374 | hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 3375 | ucdl_size, |
| 3376 | &hba->ucdl_dma_addr, |
| 3377 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3378 | |
| 3379 | /* |
| 3380 | * UFSHCI requires UTP command descriptor to be 128 byte aligned. |
| 3381 | * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE |
| 3382 | * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will |
| 3383 | * be aligned to 128 bytes as well |
| 3384 | */ |
| 3385 | if (!hba->ucdl_base_addr || |
| 3386 | WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3387 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3388 | "Command Descriptor Memory allocation failed\n"); |
| 3389 | goto out; |
| 3390 | } |
| 3391 | |
| 3392 | /* |
| 3393 | * Allocate memory for UTP Transfer descriptors |
| 3394 | * UFSHCI requires 1024 byte alignment of UTRD |
| 3395 | */ |
| 3396 | utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3397 | hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 3398 | utrdl_size, |
| 3399 | &hba->utrdl_dma_addr, |
| 3400 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3401 | if (!hba->utrdl_base_addr || |
| 3402 | WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3403 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3404 | "Transfer Descriptor Memory allocation failed\n"); |
| 3405 | goto out; |
| 3406 | } |
| 3407 | |
| 3408 | /* |
| 3409 | * Allocate memory for UTP Task Management descriptors |
| 3410 | * UFSHCI requires 1024 byte alignment of UTMRD |
| 3411 | */ |
| 3412 | utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs; |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3413 | hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 3414 | utmrdl_size, |
| 3415 | &hba->utmrdl_dma_addr, |
| 3416 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3417 | if (!hba->utmrdl_base_addr || |
| 3418 | WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3419 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3420 | "Task Management Descriptor Memory allocation failed\n"); |
| 3421 | goto out; |
| 3422 | } |
| 3423 | |
| 3424 | /* Allocate memory for local reference block */ |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 3425 | hba->lrb = devm_kcalloc(hba->dev, |
| 3426 | hba->nutrs, sizeof(struct ufshcd_lrb), |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3427 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3428 | if (!hba->lrb) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3429 | dev_err(hba->dev, "LRB Memory allocation failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3430 | goto out; |
| 3431 | } |
| 3432 | return 0; |
| 3433 | out: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3434 | return -ENOMEM; |
| 3435 | } |
| 3436 | |
| 3437 | /** |
| 3438 | * ufshcd_host_memory_configure - configure local reference block with |
| 3439 | * memory offsets |
| 3440 | * @hba: per adapter instance |
| 3441 | * |
| 3442 | * Configure Host memory space |
| 3443 | * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA |
| 3444 | * address. |
| 3445 | * 2. Update each UTRD with Response UPIU offset, Response UPIU length |
| 3446 | * and PRDT offset. |
| 3447 | * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT |
| 3448 | * into local reference block. |
| 3449 | */ |
| 3450 | static void ufshcd_host_memory_configure(struct ufs_hba *hba) |
| 3451 | { |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3452 | struct utp_transfer_req_desc *utrdlp; |
| 3453 | dma_addr_t cmd_desc_dma_addr; |
| 3454 | dma_addr_t cmd_desc_element_addr; |
| 3455 | u16 response_offset; |
| 3456 | u16 prdt_offset; |
| 3457 | int cmd_desc_size; |
| 3458 | int i; |
| 3459 | |
| 3460 | utrdlp = hba->utrdl_base_addr; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3461 | |
| 3462 | response_offset = |
| 3463 | offsetof(struct utp_transfer_cmd_desc, response_upiu); |
| 3464 | prdt_offset = |
| 3465 | offsetof(struct utp_transfer_cmd_desc, prd_table); |
| 3466 | |
| 3467 | cmd_desc_size = sizeof(struct utp_transfer_cmd_desc); |
| 3468 | cmd_desc_dma_addr = hba->ucdl_dma_addr; |
| 3469 | |
| 3470 | for (i = 0; i < hba->nutrs; i++) { |
| 3471 | /* Configure UTRD with command descriptor base address */ |
| 3472 | cmd_desc_element_addr = |
| 3473 | (cmd_desc_dma_addr + (cmd_desc_size * i)); |
| 3474 | utrdlp[i].command_desc_base_addr_lo = |
| 3475 | cpu_to_le32(lower_32_bits(cmd_desc_element_addr)); |
| 3476 | utrdlp[i].command_desc_base_addr_hi = |
| 3477 | cpu_to_le32(upper_32_bits(cmd_desc_element_addr)); |
| 3478 | |
| 3479 | /* Response upiu and prdt offset should be in double words */ |
Alim Akhtar | 26f968d | 2020-05-28 06:46:52 +0530 | [diff] [blame] | 3480 | if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) { |
| 3481 | utrdlp[i].response_upiu_offset = |
| 3482 | cpu_to_le16(response_offset); |
| 3483 | utrdlp[i].prd_table_offset = |
| 3484 | cpu_to_le16(prdt_offset); |
| 3485 | utrdlp[i].response_upiu_length = |
| 3486 | cpu_to_le16(ALIGNED_UPIU_SIZE); |
| 3487 | } else { |
| 3488 | utrdlp[i].response_upiu_offset = |
| 3489 | cpu_to_le16(response_offset >> 2); |
| 3490 | utrdlp[i].prd_table_offset = |
| 3491 | cpu_to_le16(prdt_offset >> 2); |
| 3492 | utrdlp[i].response_upiu_length = |
| 3493 | cpu_to_le16(ALIGNED_UPIU_SIZE >> 2); |
| 3494 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3495 | |
Bart Van Assche | 4d2b8d4 | 2020-01-22 19:56:35 -0800 | [diff] [blame] | 3496 | ufshcd_init_lrb(hba, &hba->lrb[i], i); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3497 | } |
| 3498 | } |
| 3499 | |
| 3500 | /** |
| 3501 | * ufshcd_dme_link_startup - Notify Unipro to perform link startup |
| 3502 | * @hba: per adapter instance |
| 3503 | * |
| 3504 | * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer, |
| 3505 | * in order to initialize the Unipro link startup procedure. |
| 3506 | * Once the Unipro links are up, the device connected to the controller |
| 3507 | * is detected. |
| 3508 | * |
| 3509 | * Returns 0 on success, non-zero value on failure |
| 3510 | */ |
| 3511 | static int ufshcd_dme_link_startup(struct ufs_hba *hba) |
| 3512 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3513 | struct uic_command uic_cmd = {0}; |
| 3514 | int ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3515 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3516 | uic_cmd.command = UIC_CMD_DME_LINK_STARTUP; |
| 3517 | |
| 3518 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3519 | if (ret) |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 3520 | dev_dbg(hba->dev, |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3521 | "dme-link-startup: error code %d\n", ret); |
| 3522 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3523 | } |
Alim Akhtar | 39bf2d8 | 2020-05-28 06:46:51 +0530 | [diff] [blame] | 3524 | /** |
| 3525 | * ufshcd_dme_reset - UIC command for DME_RESET |
| 3526 | * @hba: per adapter instance |
| 3527 | * |
| 3528 | * DME_RESET command is issued in order to reset UniPro stack. |
| 3529 | * This function now deals with cold reset. |
| 3530 | * |
| 3531 | * Returns 0 on success, non-zero value on failure |
| 3532 | */ |
| 3533 | static int ufshcd_dme_reset(struct ufs_hba *hba) |
| 3534 | { |
| 3535 | struct uic_command uic_cmd = {0}; |
| 3536 | int ret; |
| 3537 | |
| 3538 | uic_cmd.command = UIC_CMD_DME_RESET; |
| 3539 | |
| 3540 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3541 | if (ret) |
| 3542 | dev_err(hba->dev, |
| 3543 | "dme-reset: error code %d\n", ret); |
| 3544 | |
| 3545 | return ret; |
| 3546 | } |
| 3547 | |
| 3548 | /** |
| 3549 | * ufshcd_dme_enable - UIC command for DME_ENABLE |
| 3550 | * @hba: per adapter instance |
| 3551 | * |
| 3552 | * DME_ENABLE command is issued in order to enable UniPro stack. |
| 3553 | * |
| 3554 | * Returns 0 on success, non-zero value on failure |
| 3555 | */ |
| 3556 | static int ufshcd_dme_enable(struct ufs_hba *hba) |
| 3557 | { |
| 3558 | struct uic_command uic_cmd = {0}; |
| 3559 | int ret; |
| 3560 | |
| 3561 | uic_cmd.command = UIC_CMD_DME_ENABLE; |
| 3562 | |
| 3563 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3564 | if (ret) |
| 3565 | dev_err(hba->dev, |
| 3566 | "dme-reset: error code %d\n", ret); |
| 3567 | |
| 3568 | return ret; |
| 3569 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3570 | |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 3571 | static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba) |
| 3572 | { |
| 3573 | #define MIN_DELAY_BEFORE_DME_CMDS_US 1000 |
| 3574 | unsigned long min_sleep_time_us; |
| 3575 | |
| 3576 | if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS)) |
| 3577 | return; |
| 3578 | |
| 3579 | /* |
| 3580 | * last_dme_cmd_tstamp will be 0 only for 1st call to |
| 3581 | * this function |
| 3582 | */ |
| 3583 | if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) { |
| 3584 | min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US; |
| 3585 | } else { |
| 3586 | unsigned long delta = |
| 3587 | (unsigned long) ktime_to_us( |
| 3588 | ktime_sub(ktime_get(), |
| 3589 | hba->last_dme_cmd_tstamp)); |
| 3590 | |
| 3591 | if (delta < MIN_DELAY_BEFORE_DME_CMDS_US) |
| 3592 | min_sleep_time_us = |
| 3593 | MIN_DELAY_BEFORE_DME_CMDS_US - delta; |
| 3594 | else |
| 3595 | return; /* no more delay required */ |
| 3596 | } |
| 3597 | |
| 3598 | /* allow sleep for extra 50us if needed */ |
| 3599 | usleep_range(min_sleep_time_us, min_sleep_time_us + 50); |
| 3600 | } |
| 3601 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3602 | /** |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3603 | * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET |
| 3604 | * @hba: per adapter instance |
| 3605 | * @attr_sel: uic command argument1 |
| 3606 | * @attr_set: attribute set type as uic command argument2 |
| 3607 | * @mib_val: setting value as uic command argument3 |
| 3608 | * @peer: indicate whether peer or local |
| 3609 | * |
| 3610 | * Returns 0 on success, non-zero value on failure |
| 3611 | */ |
| 3612 | int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel, |
| 3613 | u8 attr_set, u32 mib_val, u8 peer) |
| 3614 | { |
| 3615 | struct uic_command uic_cmd = {0}; |
| 3616 | static const char *const action[] = { |
| 3617 | "dme-set", |
| 3618 | "dme-peer-set" |
| 3619 | }; |
| 3620 | const char *set = action[!!peer]; |
| 3621 | int ret; |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3622 | int retries = UFS_UIC_COMMAND_RETRIES; |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3623 | |
| 3624 | uic_cmd.command = peer ? |
| 3625 | UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET; |
| 3626 | uic_cmd.argument1 = attr_sel; |
| 3627 | uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set); |
| 3628 | uic_cmd.argument3 = mib_val; |
| 3629 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3630 | do { |
| 3631 | /* for peer attributes we retry upon failure */ |
| 3632 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3633 | if (ret) |
| 3634 | dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n", |
| 3635 | set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret); |
| 3636 | } while (ret && peer && --retries); |
| 3637 | |
Yaniv Gardi | f37e9f8 | 2016-11-23 16:32:49 -0800 | [diff] [blame] | 3638 | if (ret) |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3639 | dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n", |
Yaniv Gardi | f37e9f8 | 2016-11-23 16:32:49 -0800 | [diff] [blame] | 3640 | set, UIC_GET_ATTR_ID(attr_sel), mib_val, |
| 3641 | UFS_UIC_COMMAND_RETRIES - retries); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3642 | |
| 3643 | return ret; |
| 3644 | } |
| 3645 | EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr); |
| 3646 | |
| 3647 | /** |
| 3648 | * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET |
| 3649 | * @hba: per adapter instance |
| 3650 | * @attr_sel: uic command argument1 |
| 3651 | * @mib_val: the value of the attribute as returned by the UIC command |
| 3652 | * @peer: indicate whether peer or local |
| 3653 | * |
| 3654 | * Returns 0 on success, non-zero value on failure |
| 3655 | */ |
| 3656 | int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel, |
| 3657 | u32 *mib_val, u8 peer) |
| 3658 | { |
| 3659 | struct uic_command uic_cmd = {0}; |
| 3660 | static const char *const action[] = { |
| 3661 | "dme-get", |
| 3662 | "dme-peer-get" |
| 3663 | }; |
| 3664 | const char *get = action[!!peer]; |
| 3665 | int ret; |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3666 | int retries = UFS_UIC_COMMAND_RETRIES; |
Yaniv Gardi | 874237f | 2015-05-17 18:55:03 +0300 | [diff] [blame] | 3667 | struct ufs_pa_layer_attr orig_pwr_info; |
| 3668 | struct ufs_pa_layer_attr temp_pwr_info; |
| 3669 | bool pwr_mode_change = false; |
| 3670 | |
| 3671 | if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) { |
| 3672 | orig_pwr_info = hba->pwr_info; |
| 3673 | temp_pwr_info = orig_pwr_info; |
| 3674 | |
| 3675 | if (orig_pwr_info.pwr_tx == FAST_MODE || |
| 3676 | orig_pwr_info.pwr_rx == FAST_MODE) { |
| 3677 | temp_pwr_info.pwr_tx = FASTAUTO_MODE; |
| 3678 | temp_pwr_info.pwr_rx = FASTAUTO_MODE; |
| 3679 | pwr_mode_change = true; |
| 3680 | } else if (orig_pwr_info.pwr_tx == SLOW_MODE || |
| 3681 | orig_pwr_info.pwr_rx == SLOW_MODE) { |
| 3682 | temp_pwr_info.pwr_tx = SLOWAUTO_MODE; |
| 3683 | temp_pwr_info.pwr_rx = SLOWAUTO_MODE; |
| 3684 | pwr_mode_change = true; |
| 3685 | } |
| 3686 | if (pwr_mode_change) { |
| 3687 | ret = ufshcd_change_power_mode(hba, &temp_pwr_info); |
| 3688 | if (ret) |
| 3689 | goto out; |
| 3690 | } |
| 3691 | } |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3692 | |
| 3693 | uic_cmd.command = peer ? |
| 3694 | UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET; |
| 3695 | uic_cmd.argument1 = attr_sel; |
| 3696 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3697 | do { |
| 3698 | /* for peer attributes we retry upon failure */ |
| 3699 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3700 | if (ret) |
| 3701 | dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n", |
| 3702 | get, UIC_GET_ATTR_ID(attr_sel), ret); |
| 3703 | } while (ret && peer && --retries); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3704 | |
Yaniv Gardi | f37e9f8 | 2016-11-23 16:32:49 -0800 | [diff] [blame] | 3705 | if (ret) |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3706 | dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n", |
Yaniv Gardi | f37e9f8 | 2016-11-23 16:32:49 -0800 | [diff] [blame] | 3707 | get, UIC_GET_ATTR_ID(attr_sel), |
| 3708 | UFS_UIC_COMMAND_RETRIES - retries); |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3709 | |
| 3710 | if (mib_val && !ret) |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3711 | *mib_val = uic_cmd.argument3; |
Yaniv Gardi | 874237f | 2015-05-17 18:55:03 +0300 | [diff] [blame] | 3712 | |
| 3713 | if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE) |
| 3714 | && pwr_mode_change) |
| 3715 | ufshcd_change_power_mode(hba, &orig_pwr_info); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3716 | out: |
| 3717 | return ret; |
| 3718 | } |
| 3719 | EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr); |
| 3720 | |
| 3721 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3722 | * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power |
| 3723 | * state) and waits for it to take effect. |
| 3724 | * |
| 3725 | * @hba: per adapter instance |
| 3726 | * @cmd: UIC command to execute |
| 3727 | * |
| 3728 | * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER & |
| 3729 | * DME_HIBERNATE_EXIT commands take some time to take its effect on both host |
| 3730 | * and device UniPro link and hence it's final completion would be indicated by |
| 3731 | * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in |
| 3732 | * addition to normal UIC command completion Status (UCCS). This function only |
| 3733 | * returns after the relevant status bits indicate the completion. |
| 3734 | * |
| 3735 | * Returns 0 on success, non-zero value on failure |
| 3736 | */ |
| 3737 | static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd) |
| 3738 | { |
| 3739 | struct completion uic_async_done; |
| 3740 | unsigned long flags; |
| 3741 | u8 status; |
| 3742 | int ret; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3743 | bool reenable_intr = false; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3744 | |
| 3745 | mutex_lock(&hba->uic_cmd_mutex); |
| 3746 | init_completion(&uic_async_done); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 3747 | ufshcd_add_delay_before_dme_cmd(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3748 | |
| 3749 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 3750 | hba->uic_async_done = &uic_async_done; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3751 | if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) { |
| 3752 | ufshcd_disable_intr(hba, UIC_COMMAND_COMPL); |
| 3753 | /* |
| 3754 | * Make sure UIC command completion interrupt is disabled before |
| 3755 | * issuing UIC command. |
| 3756 | */ |
| 3757 | wmb(); |
| 3758 | reenable_intr = true; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3759 | } |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3760 | ret = __ufshcd_send_uic_cmd(hba, cmd, false); |
| 3761 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3762 | if (ret) { |
| 3763 | dev_err(hba->dev, |
| 3764 | "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n", |
| 3765 | cmd->command, cmd->argument3, ret); |
| 3766 | goto out; |
| 3767 | } |
| 3768 | |
| 3769 | if (!wait_for_completion_timeout(hba->uic_async_done, |
| 3770 | msecs_to_jiffies(UIC_CMD_TIMEOUT))) { |
| 3771 | dev_err(hba->dev, |
| 3772 | "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n", |
| 3773 | cmd->command, cmd->argument3); |
| 3774 | ret = -ETIMEDOUT; |
| 3775 | goto out; |
| 3776 | } |
| 3777 | |
| 3778 | status = ufshcd_get_upmcrs(hba); |
| 3779 | if (status != PWR_LOCAL) { |
| 3780 | dev_err(hba->dev, |
Zang Leigang | 479da36 | 2017-09-19 16:50:30 +0800 | [diff] [blame] | 3781 | "pwr ctrl cmd 0x%x failed, host upmcrs:0x%x\n", |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3782 | cmd->command, status); |
| 3783 | ret = (status != PWR_OK) ? status : -1; |
| 3784 | } |
| 3785 | out: |
Venkat Gopalakrishnan | 7942f7b | 2017-02-03 16:58:24 -0800 | [diff] [blame] | 3786 | if (ret) { |
| 3787 | ufshcd_print_host_state(hba); |
| 3788 | ufshcd_print_pwr_info(hba); |
| 3789 | ufshcd_print_host_regs(hba); |
| 3790 | } |
| 3791 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3792 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3793 | hba->active_uic_cmd = NULL; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3794 | hba->uic_async_done = NULL; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3795 | if (reenable_intr) |
| 3796 | ufshcd_enable_intr(hba, UIC_COMMAND_COMPL); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3797 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3798 | mutex_unlock(&hba->uic_cmd_mutex); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3799 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3800 | return ret; |
| 3801 | } |
| 3802 | |
| 3803 | /** |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3804 | * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage |
| 3805 | * using DME_SET primitives. |
| 3806 | * @hba: per adapter instance |
| 3807 | * @mode: powr mode value |
| 3808 | * |
| 3809 | * Returns 0 on success, non-zero value on failure |
| 3810 | */ |
Sujit Reddy Thumma | bdbe5d2 | 2014-05-26 10:59:11 +0530 | [diff] [blame] | 3811 | static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode) |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3812 | { |
| 3813 | struct uic_command uic_cmd = {0}; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3814 | int ret; |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3815 | |
Yaniv Gardi | c3a2f9e | 2015-05-17 18:55:01 +0300 | [diff] [blame] | 3816 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) { |
| 3817 | ret = ufshcd_dme_set(hba, |
| 3818 | UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1); |
| 3819 | if (ret) { |
| 3820 | dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n", |
| 3821 | __func__, ret); |
| 3822 | goto out; |
| 3823 | } |
| 3824 | } |
| 3825 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3826 | uic_cmd.command = UIC_CMD_DME_SET; |
| 3827 | uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE); |
| 3828 | uic_cmd.argument3 = mode; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3829 | ufshcd_hold(hba, false); |
| 3830 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
| 3831 | ufshcd_release(hba); |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3832 | |
Yaniv Gardi | c3a2f9e | 2015-05-17 18:55:01 +0300 | [diff] [blame] | 3833 | out: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3834 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3835 | } |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3836 | |
Stanley Chu | 087c5ef | 2020-03-27 17:53:28 +0800 | [diff] [blame] | 3837 | int ufshcd_link_recovery(struct ufs_hba *hba) |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 3838 | { |
| 3839 | int ret; |
| 3840 | unsigned long flags; |
| 3841 | |
| 3842 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 3843 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 3844 | ufshcd_set_eh_in_progress(hba); |
| 3845 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3846 | |
Can Guo | ebdd1df | 2019-11-14 22:09:24 -0800 | [diff] [blame] | 3847 | /* Reset the attached device */ |
| 3848 | ufshcd_vops_device_reset(hba); |
| 3849 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 3850 | ret = ufshcd_host_reset_and_restore(hba); |
| 3851 | |
| 3852 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 3853 | if (ret) |
| 3854 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 3855 | ufshcd_clear_eh_in_progress(hba); |
| 3856 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3857 | |
| 3858 | if (ret) |
| 3859 | dev_err(hba->dev, "%s: link recovery failed, err %d", |
| 3860 | __func__, ret); |
| 3861 | |
| 3862 | return ret; |
| 3863 | } |
Stanley Chu | 087c5ef | 2020-03-27 17:53:28 +0800 | [diff] [blame] | 3864 | EXPORT_SYMBOL_GPL(ufshcd_link_recovery); |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 3865 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 3866 | static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3867 | { |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 3868 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3869 | struct uic_command uic_cmd = {0}; |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 3870 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3871 | |
Kiwoong Kim | ee32c90 | 2016-11-10 21:17:43 +0900 | [diff] [blame] | 3872 | ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE); |
| 3873 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3874 | uic_cmd.command = UIC_CMD_DME_HIBER_ENTER; |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 3875 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 3876 | trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter", |
| 3877 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3878 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 3879 | if (ret) { |
Subhash Jadavani | 6d303e4 | 2019-11-14 22:09:30 -0800 | [diff] [blame] | 3880 | int err; |
| 3881 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 3882 | dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n", |
| 3883 | __func__, ret); |
| 3884 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 3885 | /* |
Subhash Jadavani | 6d303e4 | 2019-11-14 22:09:30 -0800 | [diff] [blame] | 3886 | * If link recovery fails then return error code returned from |
| 3887 | * ufshcd_link_recovery(). |
| 3888 | * If link recovery succeeds then return -EAGAIN to attempt |
| 3889 | * hibern8 enter retry again. |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 3890 | */ |
Subhash Jadavani | 6d303e4 | 2019-11-14 22:09:30 -0800 | [diff] [blame] | 3891 | err = ufshcd_link_recovery(hba); |
| 3892 | if (err) { |
| 3893 | dev_err(hba->dev, "%s: link recovery failed", __func__); |
| 3894 | ret = err; |
| 3895 | } else { |
| 3896 | ret = -EAGAIN; |
| 3897 | } |
Kiwoong Kim | ee32c90 | 2016-11-10 21:17:43 +0900 | [diff] [blame] | 3898 | } else |
| 3899 | ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, |
| 3900 | POST_CHANGE); |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 3901 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 3902 | return ret; |
| 3903 | } |
| 3904 | |
| 3905 | static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba) |
| 3906 | { |
| 3907 | int ret = 0, retries; |
| 3908 | |
| 3909 | for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) { |
| 3910 | ret = __ufshcd_uic_hibern8_enter(hba); |
Subhash Jadavani | 6d303e4 | 2019-11-14 22:09:30 -0800 | [diff] [blame] | 3911 | if (!ret) |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 3912 | goto out; |
| 3913 | } |
| 3914 | out: |
| 3915 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3916 | } |
| 3917 | |
Stanley Chu | 9d19bf7a | 2020-01-17 11:51:07 +0800 | [diff] [blame] | 3918 | int ufshcd_uic_hibern8_exit(struct ufs_hba *hba) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3919 | { |
| 3920 | struct uic_command uic_cmd = {0}; |
| 3921 | int ret; |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 3922 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3923 | |
Kiwoong Kim | ee32c90 | 2016-11-10 21:17:43 +0900 | [diff] [blame] | 3924 | ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE); |
| 3925 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3926 | uic_cmd.command = UIC_CMD_DME_HIBER_EXIT; |
| 3927 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 3928 | trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit", |
| 3929 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
| 3930 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3931 | if (ret) { |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 3932 | dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n", |
| 3933 | __func__, ret); |
| 3934 | ret = ufshcd_link_recovery(hba); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 3935 | } else { |
Kiwoong Kim | ee32c90 | 2016-11-10 21:17:43 +0900 | [diff] [blame] | 3936 | ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, |
| 3937 | POST_CHANGE); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 3938 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_get(); |
| 3939 | hba->ufs_stats.hibern8_exit_cnt++; |
| 3940 | } |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3941 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3942 | return ret; |
| 3943 | } |
Stanley Chu | 9d19bf7a | 2020-01-17 11:51:07 +0800 | [diff] [blame] | 3944 | EXPORT_SYMBOL_GPL(ufshcd_uic_hibern8_exit); |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3945 | |
Stanley Chu | ba7af5e | 2019-12-30 13:32:28 +0800 | [diff] [blame] | 3946 | void ufshcd_auto_hibern8_update(struct ufs_hba *hba, u32 ahit) |
| 3947 | { |
| 3948 | unsigned long flags; |
Can Guo | be7594a | 2020-03-05 00:53:07 -0800 | [diff] [blame] | 3949 | bool update = false; |
Stanley Chu | ba7af5e | 2019-12-30 13:32:28 +0800 | [diff] [blame] | 3950 | |
Can Guo | be7594a | 2020-03-05 00:53:07 -0800 | [diff] [blame] | 3951 | if (!ufshcd_is_auto_hibern8_supported(hba)) |
Stanley Chu | ba7af5e | 2019-12-30 13:32:28 +0800 | [diff] [blame] | 3952 | return; |
| 3953 | |
| 3954 | spin_lock_irqsave(hba->host->host_lock, flags); |
Can Guo | be7594a | 2020-03-05 00:53:07 -0800 | [diff] [blame] | 3955 | if (hba->ahit != ahit) { |
| 3956 | hba->ahit = ahit; |
| 3957 | update = true; |
| 3958 | } |
Stanley Chu | ba7af5e | 2019-12-30 13:32:28 +0800 | [diff] [blame] | 3959 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Can Guo | be7594a | 2020-03-05 00:53:07 -0800 | [diff] [blame] | 3960 | |
| 3961 | if (update && !pm_runtime_suspended(hba->dev)) { |
| 3962 | pm_runtime_get_sync(hba->dev); |
| 3963 | ufshcd_hold(hba, false); |
| 3964 | ufshcd_auto_hibern8_enable(hba); |
| 3965 | ufshcd_release(hba); |
| 3966 | pm_runtime_put(hba->dev); |
| 3967 | } |
Stanley Chu | ba7af5e | 2019-12-30 13:32:28 +0800 | [diff] [blame] | 3968 | } |
| 3969 | EXPORT_SYMBOL_GPL(ufshcd_auto_hibern8_update); |
| 3970 | |
Can Guo | 71d848b | 2019-11-14 22:09:26 -0800 | [diff] [blame] | 3971 | void ufshcd_auto_hibern8_enable(struct ufs_hba *hba) |
Adrian Hunter | ad44837 | 2018-03-20 15:07:38 +0200 | [diff] [blame] | 3972 | { |
| 3973 | unsigned long flags; |
| 3974 | |
Stanley Chu | ee5f104 | 2019-05-21 14:44:52 +0800 | [diff] [blame] | 3975 | if (!ufshcd_is_auto_hibern8_supported(hba) || !hba->ahit) |
Adrian Hunter | ad44837 | 2018-03-20 15:07:38 +0200 | [diff] [blame] | 3976 | return; |
| 3977 | |
| 3978 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 3979 | ufshcd_writel(hba, hba->ahit, REG_AUTO_HIBERNATE_IDLE_TIMER); |
| 3980 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3981 | } |
| 3982 | |
Yaniv Gardi | 5064636 | 2014-10-23 13:25:13 +0300 | [diff] [blame] | 3983 | /** |
| 3984 | * ufshcd_init_pwr_info - setting the POR (power on reset) |
| 3985 | * values in hba power info |
| 3986 | * @hba: per-adapter instance |
| 3987 | */ |
| 3988 | static void ufshcd_init_pwr_info(struct ufs_hba *hba) |
| 3989 | { |
| 3990 | hba->pwr_info.gear_rx = UFS_PWM_G1; |
| 3991 | hba->pwr_info.gear_tx = UFS_PWM_G1; |
| 3992 | hba->pwr_info.lane_rx = 1; |
| 3993 | hba->pwr_info.lane_tx = 1; |
| 3994 | hba->pwr_info.pwr_rx = SLOWAUTO_MODE; |
| 3995 | hba->pwr_info.pwr_tx = SLOWAUTO_MODE; |
| 3996 | hba->pwr_info.hs_rate = 0; |
| 3997 | } |
| 3998 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3999 | /** |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4000 | * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device |
| 4001 | * @hba: per-adapter instance |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4002 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4003 | static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4004 | { |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4005 | struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info; |
| 4006 | |
| 4007 | if (hba->max_pwr_info.is_valid) |
| 4008 | return 0; |
| 4009 | |
subhashj@codeaurora.org | 2349b53 | 2016-11-23 16:33:19 -0800 | [diff] [blame] | 4010 | pwr_info->pwr_tx = FAST_MODE; |
| 4011 | pwr_info->pwr_rx = FAST_MODE; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4012 | pwr_info->hs_rate = PA_HS_MODE_B; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4013 | |
| 4014 | /* Get the connected lane count */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4015 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES), |
| 4016 | &pwr_info->lane_rx); |
| 4017 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 4018 | &pwr_info->lane_tx); |
| 4019 | |
| 4020 | if (!pwr_info->lane_rx || !pwr_info->lane_tx) { |
| 4021 | dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n", |
| 4022 | __func__, |
| 4023 | pwr_info->lane_rx, |
| 4024 | pwr_info->lane_tx); |
| 4025 | return -EINVAL; |
| 4026 | } |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4027 | |
| 4028 | /* |
| 4029 | * First, get the maximum gears of HS speed. |
| 4030 | * If a zero value, it means there is no HSGEAR capability. |
| 4031 | * Then, get the maximum gears of PWM speed. |
| 4032 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4033 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx); |
| 4034 | if (!pwr_info->gear_rx) { |
| 4035 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), |
| 4036 | &pwr_info->gear_rx); |
| 4037 | if (!pwr_info->gear_rx) { |
| 4038 | dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n", |
| 4039 | __func__, pwr_info->gear_rx); |
| 4040 | return -EINVAL; |
| 4041 | } |
subhashj@codeaurora.org | 2349b53 | 2016-11-23 16:33:19 -0800 | [diff] [blame] | 4042 | pwr_info->pwr_rx = SLOW_MODE; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4043 | } |
| 4044 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4045 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), |
| 4046 | &pwr_info->gear_tx); |
| 4047 | if (!pwr_info->gear_tx) { |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4048 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4049 | &pwr_info->gear_tx); |
| 4050 | if (!pwr_info->gear_tx) { |
| 4051 | dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n", |
| 4052 | __func__, pwr_info->gear_tx); |
| 4053 | return -EINVAL; |
| 4054 | } |
subhashj@codeaurora.org | 2349b53 | 2016-11-23 16:33:19 -0800 | [diff] [blame] | 4055 | pwr_info->pwr_tx = SLOW_MODE; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4056 | } |
| 4057 | |
| 4058 | hba->max_pwr_info.is_valid = true; |
| 4059 | return 0; |
| 4060 | } |
| 4061 | |
| 4062 | static int ufshcd_change_power_mode(struct ufs_hba *hba, |
| 4063 | struct ufs_pa_layer_attr *pwr_mode) |
| 4064 | { |
| 4065 | int ret; |
| 4066 | |
| 4067 | /* if already configured to the requested pwr_mode */ |
| 4068 | if (pwr_mode->gear_rx == hba->pwr_info.gear_rx && |
| 4069 | pwr_mode->gear_tx == hba->pwr_info.gear_tx && |
| 4070 | pwr_mode->lane_rx == hba->pwr_info.lane_rx && |
| 4071 | pwr_mode->lane_tx == hba->pwr_info.lane_tx && |
| 4072 | pwr_mode->pwr_rx == hba->pwr_info.pwr_rx && |
| 4073 | pwr_mode->pwr_tx == hba->pwr_info.pwr_tx && |
| 4074 | pwr_mode->hs_rate == hba->pwr_info.hs_rate) { |
| 4075 | dev_dbg(hba->dev, "%s: power already configured\n", __func__); |
| 4076 | return 0; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4077 | } |
| 4078 | |
| 4079 | /* |
| 4080 | * Configure attributes for power mode change with below. |
| 4081 | * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION, |
| 4082 | * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION, |
| 4083 | * - PA_HSSERIES |
| 4084 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4085 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx); |
| 4086 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES), |
| 4087 | pwr_mode->lane_rx); |
| 4088 | if (pwr_mode->pwr_rx == FASTAUTO_MODE || |
| 4089 | pwr_mode->pwr_rx == FAST_MODE) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4090 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4091 | else |
| 4092 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4093 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4094 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx); |
| 4095 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES), |
| 4096 | pwr_mode->lane_tx); |
| 4097 | if (pwr_mode->pwr_tx == FASTAUTO_MODE || |
| 4098 | pwr_mode->pwr_tx == FAST_MODE) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4099 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4100 | else |
| 4101 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4102 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4103 | if (pwr_mode->pwr_rx == FASTAUTO_MODE || |
| 4104 | pwr_mode->pwr_tx == FASTAUTO_MODE || |
| 4105 | pwr_mode->pwr_rx == FAST_MODE || |
| 4106 | pwr_mode->pwr_tx == FAST_MODE) |
| 4107 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES), |
| 4108 | pwr_mode->hs_rate); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4109 | |
Can Guo | 0834253 | 2019-12-05 02:14:42 +0000 | [diff] [blame] | 4110 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0), |
| 4111 | DL_FC0ProtectionTimeOutVal_Default); |
| 4112 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1), |
| 4113 | DL_TC0ReplayTimeOutVal_Default); |
| 4114 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2), |
| 4115 | DL_AFC0ReqTimeOutVal_Default); |
| 4116 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA3), |
| 4117 | DL_FC1ProtectionTimeOutVal_Default); |
| 4118 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA4), |
| 4119 | DL_TC1ReplayTimeOutVal_Default); |
| 4120 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA5), |
| 4121 | DL_AFC1ReqTimeOutVal_Default); |
| 4122 | |
| 4123 | ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalFC0ProtectionTimeOutVal), |
| 4124 | DL_FC0ProtectionTimeOutVal_Default); |
| 4125 | ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalTC0ReplayTimeOutVal), |
| 4126 | DL_TC0ReplayTimeOutVal_Default); |
| 4127 | ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalAFC0ReqTimeOutVal), |
| 4128 | DL_AFC0ReqTimeOutVal_Default); |
| 4129 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4130 | ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4 |
| 4131 | | pwr_mode->pwr_tx); |
| 4132 | |
| 4133 | if (ret) { |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4134 | dev_err(hba->dev, |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4135 | "%s: power mode change failed %d\n", __func__, ret); |
| 4136 | } else { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4137 | ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL, |
| 4138 | pwr_mode); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4139 | |
| 4140 | memcpy(&hba->pwr_info, pwr_mode, |
| 4141 | sizeof(struct ufs_pa_layer_attr)); |
| 4142 | } |
| 4143 | |
| 4144 | return ret; |
| 4145 | } |
| 4146 | |
| 4147 | /** |
| 4148 | * ufshcd_config_pwr_mode - configure a new power mode |
| 4149 | * @hba: per-adapter instance |
| 4150 | * @desired_pwr_mode: desired power configuration |
| 4151 | */ |
Alim Akhtar | 0d846e7 | 2018-05-06 15:44:18 +0530 | [diff] [blame] | 4152 | int ufshcd_config_pwr_mode(struct ufs_hba *hba, |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4153 | struct ufs_pa_layer_attr *desired_pwr_mode) |
| 4154 | { |
| 4155 | struct ufs_pa_layer_attr final_params = { 0 }; |
| 4156 | int ret; |
| 4157 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4158 | ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE, |
| 4159 | desired_pwr_mode, &final_params); |
| 4160 | |
| 4161 | if (ret) |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4162 | memcpy(&final_params, desired_pwr_mode, sizeof(final_params)); |
| 4163 | |
| 4164 | ret = ufshcd_change_power_mode(hba, &final_params); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4165 | |
| 4166 | return ret; |
| 4167 | } |
Alim Akhtar | 0d846e7 | 2018-05-06 15:44:18 +0530 | [diff] [blame] | 4168 | EXPORT_SYMBOL_GPL(ufshcd_config_pwr_mode); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4169 | |
| 4170 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4171 | * ufshcd_complete_dev_init() - checks device readiness |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 4172 | * @hba: per-adapter instance |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4173 | * |
| 4174 | * Set fDeviceInit flag and poll until device toggles it. |
| 4175 | */ |
| 4176 | static int ufshcd_complete_dev_init(struct ufs_hba *hba) |
| 4177 | { |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 4178 | int i; |
| 4179 | int err; |
Jason Yan | 7dfdcc3 | 2020-04-26 17:43:05 +0800 | [diff] [blame] | 4180 | bool flag_res = true; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4181 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 4182 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG, |
Stanley Chu | 1f34eed | 2020-05-08 16:01:12 +0800 | [diff] [blame] | 4183 | QUERY_FLAG_IDN_FDEVICEINIT, 0, NULL); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4184 | if (err) { |
| 4185 | dev_err(hba->dev, |
| 4186 | "%s setting fDeviceInit flag failed with error %d\n", |
| 4187 | __func__, err); |
| 4188 | goto out; |
| 4189 | } |
| 4190 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 4191 | /* poll for max. 1000 iterations for fDeviceInit flag to clear */ |
| 4192 | for (i = 0; i < 1000 && !err && flag_res; i++) |
| 4193 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG, |
Stanley Chu | 1f34eed | 2020-05-08 16:01:12 +0800 | [diff] [blame] | 4194 | QUERY_FLAG_IDN_FDEVICEINIT, 0, &flag_res); |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 4195 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4196 | if (err) |
| 4197 | dev_err(hba->dev, |
| 4198 | "%s reading fDeviceInit flag failed with error %d\n", |
| 4199 | __func__, err); |
| 4200 | else if (flag_res) |
| 4201 | dev_err(hba->dev, |
| 4202 | "%s fDeviceInit was not cleared by the device\n", |
| 4203 | __func__); |
| 4204 | |
| 4205 | out: |
| 4206 | return err; |
| 4207 | } |
| 4208 | |
| 4209 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4210 | * ufshcd_make_hba_operational - Make UFS controller operational |
| 4211 | * @hba: per adapter instance |
| 4212 | * |
| 4213 | * To bring UFS host controller to operational state, |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4214 | * 1. Enable required interrupts |
| 4215 | * 2. Configure interrupt aggregation |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 4216 | * 3. Program UTRL and UTMRL base address |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4217 | * 4. Configure run-stop-registers |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4218 | * |
| 4219 | * Returns 0 on success, non-zero value on failure |
| 4220 | */ |
Stanley Chu | 9d19bf7a | 2020-01-17 11:51:07 +0800 | [diff] [blame] | 4221 | int ufshcd_make_hba_operational(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4222 | { |
| 4223 | int err = 0; |
| 4224 | u32 reg; |
| 4225 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4226 | /* Enable required interrupts */ |
| 4227 | ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS); |
| 4228 | |
| 4229 | /* Configure interrupt aggregation */ |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 4230 | if (ufshcd_is_intr_aggr_allowed(hba)) |
| 4231 | ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO); |
| 4232 | else |
| 4233 | ufshcd_disable_intr_aggr(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4234 | |
| 4235 | /* Configure UTRL and UTMRL base address registers */ |
| 4236 | ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr), |
| 4237 | REG_UTP_TRANSFER_REQ_LIST_BASE_L); |
| 4238 | ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr), |
| 4239 | REG_UTP_TRANSFER_REQ_LIST_BASE_H); |
| 4240 | ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr), |
| 4241 | REG_UTP_TASK_REQ_LIST_BASE_L); |
| 4242 | ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr), |
| 4243 | REG_UTP_TASK_REQ_LIST_BASE_H); |
| 4244 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4245 | /* |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 4246 | * Make sure base address and interrupt setup are updated before |
| 4247 | * enabling the run/stop registers below. |
| 4248 | */ |
| 4249 | wmb(); |
| 4250 | |
| 4251 | /* |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4252 | * UCRDY, UTMRLDY and UTRLRDY bits must be 1 |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4253 | */ |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4254 | reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4255 | if (!(ufshcd_get_lists_status(reg))) { |
| 4256 | ufshcd_enable_run_stop_reg(hba); |
| 4257 | } else { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 4258 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4259 | "Host controller not ready to process requests"); |
| 4260 | err = -EIO; |
| 4261 | goto out; |
| 4262 | } |
| 4263 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4264 | out: |
| 4265 | return err; |
| 4266 | } |
Stanley Chu | 9d19bf7a | 2020-01-17 11:51:07 +0800 | [diff] [blame] | 4267 | EXPORT_SYMBOL_GPL(ufshcd_make_hba_operational); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4268 | |
| 4269 | /** |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4270 | * ufshcd_hba_stop - Send controller to reset state |
| 4271 | * @hba: per adapter instance |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4272 | */ |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 4273 | static inline void ufshcd_hba_stop(struct ufs_hba *hba) |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4274 | { |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 4275 | unsigned long flags; |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4276 | int err; |
| 4277 | |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 4278 | /* |
| 4279 | * Obtain the host lock to prevent that the controller is disabled |
| 4280 | * while the UFS interrupt handler is active on another CPU. |
| 4281 | */ |
| 4282 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4283 | ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE); |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 4284 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4285 | |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4286 | err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE, |
| 4287 | CONTROLLER_ENABLE, CONTROLLER_DISABLE, |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 4288 | 10, 1); |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4289 | if (err) |
| 4290 | dev_err(hba->dev, "%s: Controller disable failed\n", __func__); |
| 4291 | } |
| 4292 | |
| 4293 | /** |
Alim Akhtar | 39bf2d8 | 2020-05-28 06:46:51 +0530 | [diff] [blame] | 4294 | * ufshcd_hba_execute_hce - initialize the controller |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4295 | * @hba: per adapter instance |
| 4296 | * |
| 4297 | * The controller resets itself and controller firmware initialization |
| 4298 | * sequence kicks off. When controller is ready it will set |
| 4299 | * the Host Controller Enable bit to 1. |
| 4300 | * |
| 4301 | * Returns 0 on success, non-zero value on failure |
| 4302 | */ |
Alim Akhtar | 39bf2d8 | 2020-05-28 06:46:51 +0530 | [diff] [blame] | 4303 | static int ufshcd_hba_execute_hce(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4304 | { |
| 4305 | int retry; |
| 4306 | |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4307 | if (!ufshcd_is_hba_active(hba)) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4308 | /* change controller state to "reset state" */ |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 4309 | ufshcd_hba_stop(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4310 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4311 | /* UniPro link is disabled at this point */ |
| 4312 | ufshcd_set_link_off(hba); |
| 4313 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4314 | ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4315 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4316 | /* start controller initialization sequence */ |
| 4317 | ufshcd_hba_start(hba); |
| 4318 | |
| 4319 | /* |
| 4320 | * To initialize a UFS host controller HCE bit must be set to 1. |
| 4321 | * During initialization the HCE bit value changes from 1->0->1. |
| 4322 | * When the host controller completes initialization sequence |
| 4323 | * it sets the value of HCE bit to 1. The same HCE bit is read back |
| 4324 | * to check if the controller has completed initialization sequence. |
| 4325 | * So without this delay the value HCE = 1, set in the previous |
| 4326 | * instruction might be read back. |
| 4327 | * This delay can be changed based on the controller. |
| 4328 | */ |
Stanley Chu | 90b8491 | 2020-05-09 17:37:13 +0800 | [diff] [blame] | 4329 | ufshcd_delay_us(hba->vps->hba_enable_delay_us, 100); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4330 | |
| 4331 | /* wait for the host controller to complete initialization */ |
Stanley Chu | 9fc305e | 2020-03-18 18:40:15 +0800 | [diff] [blame] | 4332 | retry = 50; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4333 | while (ufshcd_is_hba_active(hba)) { |
| 4334 | if (retry) { |
| 4335 | retry--; |
| 4336 | } else { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 4337 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4338 | "Controller enable failed\n"); |
| 4339 | return -EIO; |
| 4340 | } |
Stanley Chu | 9fc305e | 2020-03-18 18:40:15 +0800 | [diff] [blame] | 4341 | usleep_range(1000, 1100); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4342 | } |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4343 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4344 | /* enable UIC related interrupts */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4345 | ufshcd_enable_intr(hba, UFSHCD_UIC_MASK); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4346 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4347 | ufshcd_vops_hce_enable_notify(hba, POST_CHANGE); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4348 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4349 | return 0; |
| 4350 | } |
Alim Akhtar | 39bf2d8 | 2020-05-28 06:46:51 +0530 | [diff] [blame] | 4351 | |
| 4352 | int ufshcd_hba_enable(struct ufs_hba *hba) |
| 4353 | { |
| 4354 | int ret; |
| 4355 | |
| 4356 | if (hba->quirks & UFSHCI_QUIRK_BROKEN_HCE) { |
| 4357 | ufshcd_set_link_off(hba); |
| 4358 | ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE); |
| 4359 | |
| 4360 | /* enable UIC related interrupts */ |
| 4361 | ufshcd_enable_intr(hba, UFSHCD_UIC_MASK); |
| 4362 | ret = ufshcd_dme_reset(hba); |
| 4363 | if (!ret) { |
| 4364 | ret = ufshcd_dme_enable(hba); |
| 4365 | if (!ret) |
| 4366 | ufshcd_vops_hce_enable_notify(hba, POST_CHANGE); |
| 4367 | if (ret) |
| 4368 | dev_err(hba->dev, |
| 4369 | "Host controller enable failed with non-hce\n"); |
| 4370 | } |
| 4371 | } else { |
| 4372 | ret = ufshcd_hba_execute_hce(hba); |
| 4373 | } |
| 4374 | |
| 4375 | return ret; |
| 4376 | } |
Stanley Chu | 9d19bf7a | 2020-01-17 11:51:07 +0800 | [diff] [blame] | 4377 | EXPORT_SYMBOL_GPL(ufshcd_hba_enable); |
| 4378 | |
Yaniv Gardi | 7ca38cf | 2015-05-17 18:54:59 +0300 | [diff] [blame] | 4379 | static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer) |
| 4380 | { |
Stanley Chu | ba0320f | 2020-03-18 18:40:10 +0800 | [diff] [blame] | 4381 | int tx_lanes = 0, i, err = 0; |
Yaniv Gardi | 7ca38cf | 2015-05-17 18:54:59 +0300 | [diff] [blame] | 4382 | |
| 4383 | if (!peer) |
| 4384 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 4385 | &tx_lanes); |
| 4386 | else |
| 4387 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 4388 | &tx_lanes); |
| 4389 | for (i = 0; i < tx_lanes; i++) { |
| 4390 | if (!peer) |
| 4391 | err = ufshcd_dme_set(hba, |
| 4392 | UIC_ARG_MIB_SEL(TX_LCC_ENABLE, |
| 4393 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)), |
| 4394 | 0); |
| 4395 | else |
| 4396 | err = ufshcd_dme_peer_set(hba, |
| 4397 | UIC_ARG_MIB_SEL(TX_LCC_ENABLE, |
| 4398 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)), |
| 4399 | 0); |
| 4400 | if (err) { |
| 4401 | dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d", |
| 4402 | __func__, peer, i, err); |
| 4403 | break; |
| 4404 | } |
| 4405 | } |
| 4406 | |
| 4407 | return err; |
| 4408 | } |
| 4409 | |
| 4410 | static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba) |
| 4411 | { |
| 4412 | return ufshcd_disable_tx_lcc(hba, true); |
| 4413 | } |
| 4414 | |
Stanley Chu | a5fe372d | 2020-01-04 22:26:07 +0800 | [diff] [blame] | 4415 | void ufshcd_update_reg_hist(struct ufs_err_reg_hist *reg_hist, |
| 4416 | u32 reg) |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4417 | { |
| 4418 | reg_hist->reg[reg_hist->pos] = reg; |
| 4419 | reg_hist->tstamp[reg_hist->pos] = ktime_get(); |
| 4420 | reg_hist->pos = (reg_hist->pos + 1) % UFS_ERR_REG_HIST_LENGTH; |
| 4421 | } |
Stanley Chu | a5fe372d | 2020-01-04 22:26:07 +0800 | [diff] [blame] | 4422 | EXPORT_SYMBOL_GPL(ufshcd_update_reg_hist); |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4423 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4424 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4425 | * ufshcd_link_startup - Initialize unipro link startup |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4426 | * @hba: per adapter instance |
| 4427 | * |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4428 | * Returns 0 for success, non-zero in case of failure |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4429 | */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4430 | static int ufshcd_link_startup(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4431 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4432 | int ret; |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4433 | int retries = DME_LINKSTARTUP_RETRIES; |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 4434 | bool link_startup_again = false; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4435 | |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 4436 | /* |
| 4437 | * If UFS device isn't active then we will have to issue link startup |
| 4438 | * 2 times to make sure the device state move to active. |
| 4439 | */ |
| 4440 | if (!ufshcd_is_ufs_dev_active(hba)) |
| 4441 | link_startup_again = true; |
| 4442 | |
| 4443 | link_startup: |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4444 | do { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4445 | ufshcd_vops_link_startup_notify(hba, PRE_CHANGE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4446 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4447 | ret = ufshcd_dme_link_startup(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4448 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4449 | /* check if device is detected by inter-connect layer */ |
| 4450 | if (!ret && !ufshcd_is_device_present(hba)) { |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4451 | ufshcd_update_reg_hist(&hba->ufs_stats.link_startup_err, |
| 4452 | 0); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4453 | dev_err(hba->dev, "%s: Device not present\n", __func__); |
| 4454 | ret = -ENXIO; |
| 4455 | goto out; |
| 4456 | } |
| 4457 | |
| 4458 | /* |
| 4459 | * DME link lost indication is only received when link is up, |
| 4460 | * but we can't be sure if the link is up until link startup |
| 4461 | * succeeds. So reset the local Uni-Pro and try again. |
| 4462 | */ |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4463 | if (ret && ufshcd_hba_enable(hba)) { |
| 4464 | ufshcd_update_reg_hist(&hba->ufs_stats.link_startup_err, |
| 4465 | (u32)ret); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4466 | goto out; |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4467 | } |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4468 | } while (ret && retries--); |
| 4469 | |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4470 | if (ret) { |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4471 | /* failed to get the link up... retire */ |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4472 | ufshcd_update_reg_hist(&hba->ufs_stats.link_startup_err, |
| 4473 | (u32)ret); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4474 | goto out; |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4475 | } |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4476 | |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 4477 | if (link_startup_again) { |
| 4478 | link_startup_again = false; |
| 4479 | retries = DME_LINKSTARTUP_RETRIES; |
| 4480 | goto link_startup; |
| 4481 | } |
| 4482 | |
subhashj@codeaurora.org | d2aebb9 | 2016-12-22 18:41:33 -0800 | [diff] [blame] | 4483 | /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */ |
| 4484 | ufshcd_init_pwr_info(hba); |
| 4485 | ufshcd_print_pwr_info(hba); |
| 4486 | |
Yaniv Gardi | 7ca38cf | 2015-05-17 18:54:59 +0300 | [diff] [blame] | 4487 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) { |
| 4488 | ret = ufshcd_disable_device_tx_lcc(hba); |
| 4489 | if (ret) |
| 4490 | goto out; |
| 4491 | } |
| 4492 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4493 | /* Include any host controller configuration via UIC commands */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4494 | ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE); |
| 4495 | if (ret) |
| 4496 | goto out; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4497 | |
| 4498 | ret = ufshcd_make_hba_operational(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4499 | out: |
Venkat Gopalakrishnan | 7942f7b | 2017-02-03 16:58:24 -0800 | [diff] [blame] | 4500 | if (ret) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4501 | dev_err(hba->dev, "link startup failed %d\n", ret); |
Venkat Gopalakrishnan | 7942f7b | 2017-02-03 16:58:24 -0800 | [diff] [blame] | 4502 | ufshcd_print_host_state(hba); |
| 4503 | ufshcd_print_pwr_info(hba); |
| 4504 | ufshcd_print_host_regs(hba); |
| 4505 | } |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4506 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4507 | } |
| 4508 | |
| 4509 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4510 | * ufshcd_verify_dev_init() - Verify device initialization |
| 4511 | * @hba: per-adapter instance |
| 4512 | * |
| 4513 | * Send NOP OUT UPIU and wait for NOP IN response to check whether the |
| 4514 | * device Transport Protocol (UTP) layer is ready after a reset. |
| 4515 | * If the UTP layer at the device side is not initialized, it may |
| 4516 | * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT |
| 4517 | * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations. |
| 4518 | */ |
| 4519 | static int ufshcd_verify_dev_init(struct ufs_hba *hba) |
| 4520 | { |
| 4521 | int err = 0; |
| 4522 | int retries; |
| 4523 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4524 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4525 | mutex_lock(&hba->dev_cmd.lock); |
| 4526 | for (retries = NOP_OUT_RETRIES; retries > 0; retries--) { |
| 4527 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP, |
| 4528 | NOP_OUT_TIMEOUT); |
| 4529 | |
| 4530 | if (!err || err == -ETIMEDOUT) |
| 4531 | break; |
| 4532 | |
| 4533 | dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err); |
| 4534 | } |
| 4535 | mutex_unlock(&hba->dev_cmd.lock); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4536 | ufshcd_release(hba); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4537 | |
| 4538 | if (err) |
| 4539 | dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err); |
| 4540 | return err; |
| 4541 | } |
| 4542 | |
| 4543 | /** |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4544 | * ufshcd_set_queue_depth - set lun queue depth |
| 4545 | * @sdev: pointer to SCSI device |
| 4546 | * |
| 4547 | * Read bLUQueueDepth value and activate scsi tagged command |
| 4548 | * queueing. For WLUN, queue depth is set to 1. For best-effort |
| 4549 | * cases (bLUQueueDepth = 0) the queue depth is set to a maximum |
| 4550 | * value that host can queue. |
| 4551 | */ |
| 4552 | static void ufshcd_set_queue_depth(struct scsi_device *sdev) |
| 4553 | { |
| 4554 | int ret = 0; |
| 4555 | u8 lun_qdepth; |
| 4556 | struct ufs_hba *hba; |
| 4557 | |
| 4558 | hba = shost_priv(sdev->host); |
| 4559 | |
| 4560 | lun_qdepth = hba->nutrs; |
Szymon Mielczarek | dbd34a6 | 2017-03-29 08:19:21 +0200 | [diff] [blame] | 4561 | ret = ufshcd_read_unit_desc_param(hba, |
| 4562 | ufshcd_scsi_to_upiu_lun(sdev->lun), |
| 4563 | UNIT_DESC_PARAM_LU_Q_DEPTH, |
| 4564 | &lun_qdepth, |
| 4565 | sizeof(lun_qdepth)); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4566 | |
| 4567 | /* Some WLUN doesn't support unit descriptor */ |
| 4568 | if (ret == -EOPNOTSUPP) |
| 4569 | lun_qdepth = 1; |
| 4570 | else if (!lun_qdepth) |
| 4571 | /* eventually, we can figure out the real queue depth */ |
| 4572 | lun_qdepth = hba->nutrs; |
| 4573 | else |
| 4574 | lun_qdepth = min_t(int, lun_qdepth, hba->nutrs); |
| 4575 | |
| 4576 | dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n", |
| 4577 | __func__, lun_qdepth); |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4578 | scsi_change_queue_depth(sdev, lun_qdepth); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4579 | } |
| 4580 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4581 | /* |
| 4582 | * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR |
| 4583 | * @hba: per-adapter instance |
| 4584 | * @lun: UFS device lun id |
| 4585 | * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info |
| 4586 | * |
| 4587 | * Returns 0 in case of success and b_lu_write_protect status would be returned |
| 4588 | * @b_lu_write_protect parameter. |
| 4589 | * Returns -ENOTSUPP if reading b_lu_write_protect is not supported. |
| 4590 | * Returns -EINVAL in case of invalid parameters passed to this function. |
| 4591 | */ |
| 4592 | static int ufshcd_get_lu_wp(struct ufs_hba *hba, |
| 4593 | u8 lun, |
| 4594 | u8 *b_lu_write_protect) |
| 4595 | { |
| 4596 | int ret; |
| 4597 | |
| 4598 | if (!b_lu_write_protect) |
| 4599 | ret = -EINVAL; |
| 4600 | /* |
| 4601 | * According to UFS device spec, RPMB LU can't be write |
| 4602 | * protected so skip reading bLUWriteProtect parameter for |
| 4603 | * it. For other W-LUs, UNIT DESCRIPTOR is not available. |
| 4604 | */ |
Bean Huo | 1baa801 | 2020-01-20 14:08:20 +0100 | [diff] [blame] | 4605 | else if (lun >= hba->dev_info.max_lu_supported) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4606 | ret = -ENOTSUPP; |
| 4607 | else |
| 4608 | ret = ufshcd_read_unit_desc_param(hba, |
| 4609 | lun, |
| 4610 | UNIT_DESC_PARAM_LU_WR_PROTECT, |
| 4611 | b_lu_write_protect, |
| 4612 | sizeof(*b_lu_write_protect)); |
| 4613 | return ret; |
| 4614 | } |
| 4615 | |
| 4616 | /** |
| 4617 | * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect |
| 4618 | * status |
| 4619 | * @hba: per-adapter instance |
| 4620 | * @sdev: pointer to SCSI device |
| 4621 | * |
| 4622 | */ |
| 4623 | static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba, |
| 4624 | struct scsi_device *sdev) |
| 4625 | { |
| 4626 | if (hba->dev_info.f_power_on_wp_en && |
| 4627 | !hba->dev_info.is_lu_power_on_wp) { |
| 4628 | u8 b_lu_write_protect; |
| 4629 | |
| 4630 | if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun), |
| 4631 | &b_lu_write_protect) && |
| 4632 | (b_lu_write_protect == UFS_LU_POWER_ON_WP)) |
| 4633 | hba->dev_info.is_lu_power_on_wp = true; |
| 4634 | } |
| 4635 | } |
| 4636 | |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4637 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4638 | * ufshcd_slave_alloc - handle initial SCSI device configurations |
| 4639 | * @sdev: pointer to SCSI device |
| 4640 | * |
| 4641 | * Returns success |
| 4642 | */ |
| 4643 | static int ufshcd_slave_alloc(struct scsi_device *sdev) |
| 4644 | { |
| 4645 | struct ufs_hba *hba; |
| 4646 | |
| 4647 | hba = shost_priv(sdev->host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4648 | |
| 4649 | /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */ |
| 4650 | sdev->use_10_for_ms = 1; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4651 | |
Can Guo | a3a7639 | 2019-12-05 02:14:30 +0000 | [diff] [blame] | 4652 | /* DBD field should be set to 1 in mode sense(10) */ |
| 4653 | sdev->set_dbd_for_ms = 1; |
| 4654 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4655 | /* allow SCSI layer to restart the device in case of errors */ |
| 4656 | sdev->allow_restart = 1; |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4657 | |
Sujit Reddy Thumma | b2a6c52 | 2014-07-01 12:22:38 +0300 | [diff] [blame] | 4658 | /* REPORT SUPPORTED OPERATION CODES is not supported */ |
| 4659 | sdev->no_report_opcodes = 1; |
| 4660 | |
Sujit Reddy Thumma | 84af7e8 | 2018-01-24 09:52:35 +0530 | [diff] [blame] | 4661 | /* WRITE_SAME command is not supported */ |
| 4662 | sdev->no_write_same = 1; |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4663 | |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4664 | ufshcd_set_queue_depth(sdev); |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4665 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4666 | ufshcd_get_lu_power_on_wp_status(hba, sdev); |
| 4667 | |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4668 | return 0; |
| 4669 | } |
| 4670 | |
| 4671 | /** |
| 4672 | * ufshcd_change_queue_depth - change queue depth |
| 4673 | * @sdev: pointer to SCSI device |
| 4674 | * @depth: required depth to set |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4675 | * |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4676 | * Change queue depth and make sure the max. limits are not crossed. |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4677 | */ |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4678 | static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth) |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4679 | { |
| 4680 | struct ufs_hba *hba = shost_priv(sdev->host); |
| 4681 | |
| 4682 | if (depth > hba->nutrs) |
| 4683 | depth = hba->nutrs; |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4684 | return scsi_change_queue_depth(sdev, depth); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4685 | } |
| 4686 | |
| 4687 | /** |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 4688 | * ufshcd_slave_configure - adjust SCSI device configurations |
| 4689 | * @sdev: pointer to SCSI device |
| 4690 | */ |
| 4691 | static int ufshcd_slave_configure(struct scsi_device *sdev) |
| 4692 | { |
Stanley Chu | 49615ba | 2019-09-16 23:56:50 +0800 | [diff] [blame] | 4693 | struct ufs_hba *hba = shost_priv(sdev->host); |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 4694 | struct request_queue *q = sdev->request_queue; |
| 4695 | |
| 4696 | blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1); |
Stanley Chu | 49615ba | 2019-09-16 23:56:50 +0800 | [diff] [blame] | 4697 | |
| 4698 | if (ufshcd_is_rpm_autosuspend_allowed(hba)) |
| 4699 | sdev->rpm_autosuspend = 1; |
| 4700 | |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 4701 | ufshcd_crypto_setup_rq_keyslot_manager(hba, q); |
| 4702 | |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 4703 | return 0; |
| 4704 | } |
| 4705 | |
| 4706 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4707 | * ufshcd_slave_destroy - remove SCSI device configurations |
| 4708 | * @sdev: pointer to SCSI device |
| 4709 | */ |
| 4710 | static void ufshcd_slave_destroy(struct scsi_device *sdev) |
| 4711 | { |
| 4712 | struct ufs_hba *hba; |
| 4713 | |
| 4714 | hba = shost_priv(sdev->host); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4715 | /* Drop the reference as it won't be needed anymore */ |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 4716 | if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) { |
| 4717 | unsigned long flags; |
| 4718 | |
| 4719 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4720 | hba->sdev_ufs_device = NULL; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 4721 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4722 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4723 | } |
| 4724 | |
| 4725 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4726 | * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 4727 | * @lrbp: pointer to local reference block of completed command |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4728 | * @scsi_status: SCSI command status |
| 4729 | * |
| 4730 | * Returns value base on SCSI command status |
| 4731 | */ |
| 4732 | static inline int |
| 4733 | ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status) |
| 4734 | { |
| 4735 | int result = 0; |
| 4736 | |
| 4737 | switch (scsi_status) { |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 4738 | case SAM_STAT_CHECK_CONDITION: |
| 4739 | ufshcd_copy_sense_data(lrbp); |
Tomas Winkler | 30eb2e4 | 2018-11-26 10:10:34 +0200 | [diff] [blame] | 4740 | /* fallthrough */ |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4741 | case SAM_STAT_GOOD: |
| 4742 | result |= DID_OK << 16 | |
| 4743 | COMMAND_COMPLETE << 8 | |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 4744 | scsi_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4745 | break; |
| 4746 | case SAM_STAT_TASK_SET_FULL: |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 4747 | case SAM_STAT_BUSY: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4748 | case SAM_STAT_TASK_ABORTED: |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 4749 | ufshcd_copy_sense_data(lrbp); |
| 4750 | result |= scsi_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4751 | break; |
| 4752 | default: |
| 4753 | result |= DID_ERROR << 16; |
| 4754 | break; |
| 4755 | } /* end of switch */ |
| 4756 | |
| 4757 | return result; |
| 4758 | } |
| 4759 | |
| 4760 | /** |
| 4761 | * ufshcd_transfer_rsp_status - Get overall status of the response |
| 4762 | * @hba: per adapter instance |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 4763 | * @lrbp: pointer to local reference block of completed command |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4764 | * |
| 4765 | * Returns result of the command to notify SCSI midlayer |
| 4766 | */ |
| 4767 | static inline int |
| 4768 | ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 4769 | { |
| 4770 | int result = 0; |
| 4771 | int scsi_status; |
| 4772 | int ocs; |
| 4773 | |
| 4774 | /* overall command status of utrd */ |
| 4775 | ocs = ufshcd_get_tr_ocs(lrbp); |
| 4776 | |
Kiwoong Kim | d779a6e | 2020-05-28 06:46:53 +0530 | [diff] [blame] | 4777 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR) { |
| 4778 | if (be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_1) & |
| 4779 | MASK_RSP_UPIU_RESULT) |
| 4780 | ocs = OCS_SUCCESS; |
| 4781 | } |
| 4782 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4783 | switch (ocs) { |
| 4784 | case OCS_SUCCESS: |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4785 | result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 4786 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4787 | switch (result) { |
| 4788 | case UPIU_TRANSACTION_RESPONSE: |
| 4789 | /* |
| 4790 | * get the response UPIU result to extract |
| 4791 | * the SCSI command status |
| 4792 | */ |
| 4793 | result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr); |
| 4794 | |
| 4795 | /* |
| 4796 | * get the result based on SCSI status response |
| 4797 | * to notify the SCSI midlayer of the command status |
| 4798 | */ |
| 4799 | scsi_status = result & MASK_SCSI_STATUS; |
| 4800 | result = ufshcd_scsi_cmd_status(lrbp, scsi_status); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4801 | |
Yaniv Gardi | f05ac2e | 2016-02-01 15:02:42 +0200 | [diff] [blame] | 4802 | /* |
| 4803 | * Currently we are only supporting BKOPs exception |
| 4804 | * events hence we can ignore BKOPs exception event |
| 4805 | * during power management callbacks. BKOPs exception |
| 4806 | * event is not expected to be raised in runtime suspend |
| 4807 | * callback as it allows the urgent bkops. |
| 4808 | * During system suspend, we are anyway forcefully |
| 4809 | * disabling the bkops and if urgent bkops is needed |
| 4810 | * it will be enabled on system resume. Long term |
| 4811 | * solution could be to abort the system suspend if |
| 4812 | * UFS device needs urgent BKOPs. |
| 4813 | */ |
| 4814 | if (!hba->pm_op_in_progress && |
Sayali Lokhande | 2824ec9 | 2020-02-10 19:40:44 -0800 | [diff] [blame] | 4815 | ufshcd_is_exception_event(lrbp->ucd_rsp_ptr) && |
| 4816 | schedule_work(&hba->eeh_work)) { |
| 4817 | /* |
| 4818 | * Prevent suspend once eeh_work is scheduled |
| 4819 | * to avoid deadlock between ufshcd_suspend |
| 4820 | * and exception event handler. |
| 4821 | */ |
| 4822 | pm_runtime_get_noresume(hba->dev); |
| 4823 | } |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4824 | break; |
| 4825 | case UPIU_TRANSACTION_REJECT_UPIU: |
| 4826 | /* TODO: handle Reject UPIU Response */ |
| 4827 | result = DID_ERROR << 16; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 4828 | dev_err(hba->dev, |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4829 | "Reject UPIU not fully implemented\n"); |
| 4830 | break; |
| 4831 | default: |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4832 | dev_err(hba->dev, |
| 4833 | "Unexpected request response code = %x\n", |
| 4834 | result); |
Stanley Chu | e0347d8 | 2019-04-15 20:23:38 +0800 | [diff] [blame] | 4835 | result = DID_ERROR << 16; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4836 | break; |
| 4837 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4838 | break; |
| 4839 | case OCS_ABORTED: |
| 4840 | result |= DID_ABORT << 16; |
| 4841 | break; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4842 | case OCS_INVALID_COMMAND_STATUS: |
| 4843 | result |= DID_REQUEUE << 16; |
| 4844 | break; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4845 | case OCS_INVALID_CMD_TABLE_ATTR: |
| 4846 | case OCS_INVALID_PRDT_ATTR: |
| 4847 | case OCS_MISMATCH_DATA_BUF_SIZE: |
| 4848 | case OCS_MISMATCH_RESP_UPIU_SIZE: |
| 4849 | case OCS_PEER_COMM_FAILURE: |
| 4850 | case OCS_FATAL_ERROR: |
Satya Tangirala | 5e7341e | 2020-07-06 20:04:12 +0000 | [diff] [blame] | 4851 | case OCS_DEVICE_FATAL_ERROR: |
| 4852 | case OCS_INVALID_CRYPTO_CONFIG: |
| 4853 | case OCS_GENERAL_CRYPTO_ERROR: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4854 | default: |
| 4855 | result |= DID_ERROR << 16; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 4856 | dev_err(hba->dev, |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 4857 | "OCS error from controller = %x for tag %d\n", |
| 4858 | ocs, lrbp->task_tag); |
| 4859 | ufshcd_print_host_regs(hba); |
Gilad Broner | 6ba6558 | 2017-02-03 16:57:28 -0800 | [diff] [blame] | 4860 | ufshcd_print_host_state(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4861 | break; |
| 4862 | } /* end of switch */ |
| 4863 | |
Can Guo | 2df74b6 | 2019-11-25 22:53:33 -0800 | [diff] [blame] | 4864 | if ((host_byte(result) != DID_OK) && !hba->silence_err_logs) |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 4865 | ufshcd_print_trs(hba, 1 << lrbp->task_tag, true); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4866 | return result; |
| 4867 | } |
| 4868 | |
| 4869 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4870 | * ufshcd_uic_cmd_compl - handle completion of uic command |
| 4871 | * @hba: per adapter instance |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4872 | * @intr_status: interrupt status generated by the controller |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 4873 | * |
| 4874 | * Returns |
| 4875 | * IRQ_HANDLED - If interrupt is valid |
| 4876 | * IRQ_NONE - If invalid interrupt |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4877 | */ |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 4878 | static irqreturn_t ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4879 | { |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 4880 | irqreturn_t retval = IRQ_NONE; |
| 4881 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4882 | if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4883 | hba->active_uic_cmd->argument2 |= |
| 4884 | ufshcd_get_uic_cmd_result(hba); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 4885 | hba->active_uic_cmd->argument3 = |
| 4886 | ufshcd_get_dme_attr_val(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4887 | complete(&hba->active_uic_cmd->done); |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 4888 | retval = IRQ_HANDLED; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4889 | } |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4890 | |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 4891 | if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4892 | complete(hba->uic_async_done); |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 4893 | retval = IRQ_HANDLED; |
| 4894 | } |
Stanley Chu | aa5c697 | 2020-06-15 15:22:35 +0800 | [diff] [blame] | 4895 | |
| 4896 | if (retval == IRQ_HANDLED) |
| 4897 | ufshcd_add_uic_command_trace(hba, hba->active_uic_cmd, |
| 4898 | "complete"); |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 4899 | return retval; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4900 | } |
| 4901 | |
| 4902 | /** |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4903 | * __ufshcd_transfer_req_compl - handle SCSI and query command completion |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4904 | * @hba: per adapter instance |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4905 | * @completed_reqs: requests to complete |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4906 | */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4907 | static void __ufshcd_transfer_req_compl(struct ufs_hba *hba, |
| 4908 | unsigned long completed_reqs) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4909 | { |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4910 | struct ufshcd_lrb *lrbp; |
| 4911 | struct scsi_cmnd *cmd; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4912 | int result; |
| 4913 | int index; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4914 | |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4915 | for_each_set_bit(index, &completed_reqs, hba->nutrs) { |
| 4916 | lrbp = &hba->lrb[index]; |
Stanley Chu | a317037 | 2020-07-06 14:07:06 +0800 | [diff] [blame] | 4917 | lrbp->compl_time_stamp = ktime_get(); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4918 | cmd = lrbp->cmd; |
| 4919 | if (cmd) { |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 4920 | ufshcd_add_command_trace(hba, index, "complete"); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4921 | result = ufshcd_transfer_rsp_status(hba, lrbp); |
| 4922 | scsi_dma_unmap(cmd); |
| 4923 | cmd->result = result; |
| 4924 | /* Mark completed command as NULL in LRB */ |
| 4925 | lrbp->cmd = NULL; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4926 | /* Do not touch lrbp after scsi done */ |
| 4927 | cmd->scsi_done(cmd); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4928 | __ufshcd_release(hba); |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 4929 | } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE || |
| 4930 | lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) { |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 4931 | if (hba->dev_cmd.complete) { |
| 4932 | ufshcd_add_command_trace(hba, index, |
| 4933 | "dev_complete"); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4934 | complete(hba->dev_cmd.complete); |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 4935 | } |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4936 | } |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 4937 | if (ufshcd_is_clkscaling_supported(hba)) |
| 4938 | hba->clk_scaling.active_reqs--; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4939 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4940 | |
| 4941 | /* clear corresponding bits of completed commands */ |
| 4942 | hba->outstanding_reqs ^= completed_reqs; |
| 4943 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 4944 | ufshcd_clk_scaling_update_busy(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4945 | } |
| 4946 | |
| 4947 | /** |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4948 | * ufshcd_transfer_req_compl - handle SCSI and query command completion |
| 4949 | * @hba: per adapter instance |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 4950 | * |
| 4951 | * Returns |
| 4952 | * IRQ_HANDLED - If interrupt is valid |
| 4953 | * IRQ_NONE - If invalid interrupt |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4954 | */ |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 4955 | static irqreturn_t ufshcd_transfer_req_compl(struct ufs_hba *hba) |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4956 | { |
| 4957 | unsigned long completed_reqs; |
| 4958 | u32 tr_doorbell; |
| 4959 | |
| 4960 | /* Resetting interrupt aggregation counters first and reading the |
| 4961 | * DOOR_BELL afterward allows us to handle all the completed requests. |
| 4962 | * In order to prevent other interrupts starvation the DB is read once |
| 4963 | * after reset. The down side of this solution is the possibility of |
| 4964 | * false interrupt if device completes another request after resetting |
| 4965 | * aggregation and before reading the DB. |
| 4966 | */ |
Alim Akhtar | b638b5e | 2020-05-28 06:46:50 +0530 | [diff] [blame] | 4967 | if (ufshcd_is_intr_aggr_allowed(hba) && |
| 4968 | !(hba->quirks & UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR)) |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4969 | ufshcd_reset_intr_aggr(hba); |
| 4970 | |
| 4971 | tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 4972 | completed_reqs = tr_doorbell ^ hba->outstanding_reqs; |
| 4973 | |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 4974 | if (completed_reqs) { |
| 4975 | __ufshcd_transfer_req_compl(hba, completed_reqs); |
| 4976 | return IRQ_HANDLED; |
| 4977 | } else { |
| 4978 | return IRQ_NONE; |
| 4979 | } |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4980 | } |
| 4981 | |
| 4982 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4983 | * ufshcd_disable_ee - disable exception event |
| 4984 | * @hba: per-adapter instance |
| 4985 | * @mask: exception event to disable |
| 4986 | * |
| 4987 | * Disables exception event in the device so that the EVENT_ALERT |
| 4988 | * bit is not set. |
| 4989 | * |
| 4990 | * Returns zero on success, non-zero error value on failure. |
| 4991 | */ |
| 4992 | static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask) |
| 4993 | { |
| 4994 | int err = 0; |
| 4995 | u32 val; |
| 4996 | |
| 4997 | if (!(hba->ee_ctrl_mask & mask)) |
| 4998 | goto out; |
| 4999 | |
| 5000 | val = hba->ee_ctrl_mask & ~mask; |
Tomohiro Kusumi | d7e2ddd | 2017-04-20 15:01:44 +0300 | [diff] [blame] | 5001 | val &= MASK_EE_STATUS; |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 5002 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5003 | QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val); |
| 5004 | if (!err) |
| 5005 | hba->ee_ctrl_mask &= ~mask; |
| 5006 | out: |
| 5007 | return err; |
| 5008 | } |
| 5009 | |
| 5010 | /** |
| 5011 | * ufshcd_enable_ee - enable exception event |
| 5012 | * @hba: per-adapter instance |
| 5013 | * @mask: exception event to enable |
| 5014 | * |
| 5015 | * Enable corresponding exception event in the device to allow |
| 5016 | * device to alert host in critical scenarios. |
| 5017 | * |
| 5018 | * Returns zero on success, non-zero error value on failure. |
| 5019 | */ |
| 5020 | static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask) |
| 5021 | { |
| 5022 | int err = 0; |
| 5023 | u32 val; |
| 5024 | |
| 5025 | if (hba->ee_ctrl_mask & mask) |
| 5026 | goto out; |
| 5027 | |
| 5028 | val = hba->ee_ctrl_mask | mask; |
Tomohiro Kusumi | d7e2ddd | 2017-04-20 15:01:44 +0300 | [diff] [blame] | 5029 | val &= MASK_EE_STATUS; |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 5030 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5031 | QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val); |
| 5032 | if (!err) |
| 5033 | hba->ee_ctrl_mask |= mask; |
| 5034 | out: |
| 5035 | return err; |
| 5036 | } |
| 5037 | |
| 5038 | /** |
| 5039 | * ufshcd_enable_auto_bkops - Allow device managed BKOPS |
| 5040 | * @hba: per-adapter instance |
| 5041 | * |
| 5042 | * Allow device to manage background operations on its own. Enabling |
| 5043 | * this might lead to inconsistent latencies during normal data transfers |
| 5044 | * as the device is allowed to manage its own way of handling background |
| 5045 | * operations. |
| 5046 | * |
| 5047 | * Returns zero on success, non-zero on failure. |
| 5048 | */ |
| 5049 | static int ufshcd_enable_auto_bkops(struct ufs_hba *hba) |
| 5050 | { |
| 5051 | int err = 0; |
| 5052 | |
| 5053 | if (hba->auto_bkops_enabled) |
| 5054 | goto out; |
| 5055 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 5056 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG, |
Stanley Chu | 1f34eed | 2020-05-08 16:01:12 +0800 | [diff] [blame] | 5057 | QUERY_FLAG_IDN_BKOPS_EN, 0, NULL); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5058 | if (err) { |
| 5059 | dev_err(hba->dev, "%s: failed to enable bkops %d\n", |
| 5060 | __func__, err); |
| 5061 | goto out; |
| 5062 | } |
| 5063 | |
| 5064 | hba->auto_bkops_enabled = true; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 5065 | trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Enabled"); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5066 | |
| 5067 | /* No need of URGENT_BKOPS exception from the device */ |
| 5068 | err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 5069 | if (err) |
| 5070 | dev_err(hba->dev, "%s: failed to disable exception event %d\n", |
| 5071 | __func__, err); |
| 5072 | out: |
| 5073 | return err; |
| 5074 | } |
| 5075 | |
| 5076 | /** |
| 5077 | * ufshcd_disable_auto_bkops - block device in doing background operations |
| 5078 | * @hba: per-adapter instance |
| 5079 | * |
| 5080 | * Disabling background operations improves command response latency but |
| 5081 | * has drawback of device moving into critical state where the device is |
| 5082 | * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the |
| 5083 | * host is idle so that BKOPS are managed effectively without any negative |
| 5084 | * impacts. |
| 5085 | * |
| 5086 | * Returns zero on success, non-zero on failure. |
| 5087 | */ |
| 5088 | static int ufshcd_disable_auto_bkops(struct ufs_hba *hba) |
| 5089 | { |
| 5090 | int err = 0; |
| 5091 | |
| 5092 | if (!hba->auto_bkops_enabled) |
| 5093 | goto out; |
| 5094 | |
| 5095 | /* |
| 5096 | * If host assisted BKOPs is to be enabled, make sure |
| 5097 | * urgent bkops exception is allowed. |
| 5098 | */ |
| 5099 | err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 5100 | if (err) { |
| 5101 | dev_err(hba->dev, "%s: failed to enable exception event %d\n", |
| 5102 | __func__, err); |
| 5103 | goto out; |
| 5104 | } |
| 5105 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 5106 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG, |
Stanley Chu | 1f34eed | 2020-05-08 16:01:12 +0800 | [diff] [blame] | 5107 | QUERY_FLAG_IDN_BKOPS_EN, 0, NULL); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5108 | if (err) { |
| 5109 | dev_err(hba->dev, "%s: failed to disable bkops %d\n", |
| 5110 | __func__, err); |
| 5111 | ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 5112 | goto out; |
| 5113 | } |
| 5114 | |
| 5115 | hba->auto_bkops_enabled = false; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 5116 | trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Disabled"); |
Asutosh Das | 24366c2a | 2019-11-25 22:53:30 -0800 | [diff] [blame] | 5117 | hba->is_urgent_bkops_lvl_checked = false; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5118 | out: |
| 5119 | return err; |
| 5120 | } |
| 5121 | |
| 5122 | /** |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 5123 | * ufshcd_force_reset_auto_bkops - force reset auto bkops state |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5124 | * @hba: per adapter instance |
| 5125 | * |
| 5126 | * After a device reset the device may toggle the BKOPS_EN flag |
| 5127 | * to default value. The s/w tracking variables should be updated |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 5128 | * as well. This function would change the auto-bkops state based on |
| 5129 | * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5130 | */ |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 5131 | static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5132 | { |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 5133 | if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) { |
| 5134 | hba->auto_bkops_enabled = false; |
| 5135 | hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS; |
| 5136 | ufshcd_enable_auto_bkops(hba); |
| 5137 | } else { |
| 5138 | hba->auto_bkops_enabled = true; |
| 5139 | hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS; |
| 5140 | ufshcd_disable_auto_bkops(hba); |
| 5141 | } |
Stanley Chu | 7b6668d | 2020-05-30 22:12:00 +0800 | [diff] [blame] | 5142 | hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT; |
Asutosh Das | 24366c2a | 2019-11-25 22:53:30 -0800 | [diff] [blame] | 5143 | hba->is_urgent_bkops_lvl_checked = false; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5144 | } |
| 5145 | |
| 5146 | static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status) |
| 5147 | { |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 5148 | return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5149 | QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status); |
| 5150 | } |
| 5151 | |
| 5152 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5153 | * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status |
| 5154 | * @hba: per-adapter instance |
| 5155 | * @status: bkops_status value |
| 5156 | * |
| 5157 | * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn |
| 5158 | * flag in the device to permit background operations if the device |
| 5159 | * bkops_status is greater than or equal to "status" argument passed to |
| 5160 | * this function, disable otherwise. |
| 5161 | * |
| 5162 | * Returns 0 for success, non-zero in case of failure. |
| 5163 | * |
| 5164 | * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag |
| 5165 | * to know whether auto bkops is enabled or disabled after this function |
| 5166 | * returns control to it. |
| 5167 | */ |
| 5168 | static int ufshcd_bkops_ctrl(struct ufs_hba *hba, |
| 5169 | enum bkops_status status) |
| 5170 | { |
| 5171 | int err; |
| 5172 | u32 curr_status = 0; |
| 5173 | |
| 5174 | err = ufshcd_get_bkops_status(hba, &curr_status); |
| 5175 | if (err) { |
| 5176 | dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", |
| 5177 | __func__, err); |
| 5178 | goto out; |
| 5179 | } else if (curr_status > BKOPS_STATUS_MAX) { |
| 5180 | dev_err(hba->dev, "%s: invalid BKOPS status %d\n", |
| 5181 | __func__, curr_status); |
| 5182 | err = -EINVAL; |
| 5183 | goto out; |
| 5184 | } |
| 5185 | |
| 5186 | if (curr_status >= status) |
| 5187 | err = ufshcd_enable_auto_bkops(hba); |
| 5188 | else |
| 5189 | err = ufshcd_disable_auto_bkops(hba); |
| 5190 | out: |
| 5191 | return err; |
| 5192 | } |
| 5193 | |
| 5194 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5195 | * ufshcd_urgent_bkops - handle urgent bkops exception event |
| 5196 | * @hba: per-adapter instance |
| 5197 | * |
| 5198 | * Enable fBackgroundOpsEn flag in the device to permit background |
| 5199 | * operations. |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5200 | * |
| 5201 | * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled |
| 5202 | * and negative error value for any other failure. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5203 | */ |
| 5204 | static int ufshcd_urgent_bkops(struct ufs_hba *hba) |
| 5205 | { |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 5206 | return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5207 | } |
| 5208 | |
| 5209 | static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status) |
| 5210 | { |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 5211 | return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5212 | QUERY_ATTR_IDN_EE_STATUS, 0, 0, status); |
| 5213 | } |
| 5214 | |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 5215 | static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba) |
| 5216 | { |
| 5217 | int err; |
| 5218 | u32 curr_status = 0; |
| 5219 | |
| 5220 | if (hba->is_urgent_bkops_lvl_checked) |
| 5221 | goto enable_auto_bkops; |
| 5222 | |
| 5223 | err = ufshcd_get_bkops_status(hba, &curr_status); |
| 5224 | if (err) { |
| 5225 | dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", |
| 5226 | __func__, err); |
| 5227 | goto out; |
| 5228 | } |
| 5229 | |
| 5230 | /* |
| 5231 | * We are seeing that some devices are raising the urgent bkops |
| 5232 | * exception events even when BKOPS status doesn't indicate performace |
| 5233 | * impacted or critical. Handle these device by determining their urgent |
| 5234 | * bkops status at runtime. |
| 5235 | */ |
| 5236 | if (curr_status < BKOPS_STATUS_PERF_IMPACT) { |
| 5237 | dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n", |
| 5238 | __func__, curr_status); |
| 5239 | /* update the current status as the urgent bkops level */ |
| 5240 | hba->urgent_bkops_lvl = curr_status; |
| 5241 | hba->is_urgent_bkops_lvl_checked = true; |
| 5242 | } |
| 5243 | |
| 5244 | enable_auto_bkops: |
| 5245 | err = ufshcd_enable_auto_bkops(hba); |
| 5246 | out: |
| 5247 | if (err < 0) |
| 5248 | dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n", |
| 5249 | __func__, err); |
| 5250 | } |
| 5251 | |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5252 | static int ufshcd_wb_ctrl(struct ufs_hba *hba, bool enable) |
| 5253 | { |
| 5254 | int ret; |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 5255 | u8 index; |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5256 | enum query_opcode opcode; |
| 5257 | |
Stanley Chu | 79e3520 | 2020-05-08 16:01:15 +0800 | [diff] [blame] | 5258 | if (!ufshcd_is_wb_allowed(hba)) |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5259 | return 0; |
| 5260 | |
| 5261 | if (!(enable ^ hba->wb_enabled)) |
| 5262 | return 0; |
| 5263 | if (enable) |
| 5264 | opcode = UPIU_QUERY_OPCODE_SET_FLAG; |
| 5265 | else |
| 5266 | opcode = UPIU_QUERY_OPCODE_CLEAR_FLAG; |
| 5267 | |
Stanley Chu | e31011a | 2020-05-22 16:32:11 +0800 | [diff] [blame] | 5268 | index = ufshcd_wb_get_query_index(hba); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5269 | ret = ufshcd_query_flag_retry(hba, opcode, |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 5270 | QUERY_FLAG_IDN_WB_EN, index, NULL); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5271 | if (ret) { |
| 5272 | dev_err(hba->dev, "%s write booster %s failed %d\n", |
| 5273 | __func__, enable ? "enable" : "disable", ret); |
| 5274 | return ret; |
| 5275 | } |
| 5276 | |
| 5277 | hba->wb_enabled = enable; |
| 5278 | dev_dbg(hba->dev, "%s write booster %s %d\n", |
| 5279 | __func__, enable ? "enable" : "disable", ret); |
| 5280 | |
| 5281 | return ret; |
| 5282 | } |
| 5283 | |
| 5284 | static int ufshcd_wb_toggle_flush_during_h8(struct ufs_hba *hba, bool set) |
| 5285 | { |
| 5286 | int val; |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 5287 | u8 index; |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5288 | |
| 5289 | if (set) |
| 5290 | val = UPIU_QUERY_OPCODE_SET_FLAG; |
| 5291 | else |
| 5292 | val = UPIU_QUERY_OPCODE_CLEAR_FLAG; |
| 5293 | |
Stanley Chu | e31011a | 2020-05-22 16:32:11 +0800 | [diff] [blame] | 5294 | index = ufshcd_wb_get_query_index(hba); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5295 | return ufshcd_query_flag_retry(hba, val, |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 5296 | QUERY_FLAG_IDN_WB_BUFF_FLUSH_DURING_HIBERN8, |
| 5297 | index, NULL); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5298 | } |
| 5299 | |
| 5300 | static inline void ufshcd_wb_toggle_flush(struct ufs_hba *hba, bool enable) |
| 5301 | { |
| 5302 | if (enable) |
| 5303 | ufshcd_wb_buf_flush_enable(hba); |
| 5304 | else |
| 5305 | ufshcd_wb_buf_flush_disable(hba); |
| 5306 | |
| 5307 | } |
| 5308 | |
| 5309 | static int ufshcd_wb_buf_flush_enable(struct ufs_hba *hba) |
| 5310 | { |
| 5311 | int ret; |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 5312 | u8 index; |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5313 | |
Stanley Chu | 79e3520 | 2020-05-08 16:01:15 +0800 | [diff] [blame] | 5314 | if (!ufshcd_is_wb_allowed(hba) || hba->wb_buf_flush_enabled) |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5315 | return 0; |
| 5316 | |
Stanley Chu | e31011a | 2020-05-22 16:32:11 +0800 | [diff] [blame] | 5317 | index = ufshcd_wb_get_query_index(hba); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5318 | ret = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG, |
Stanley Chu | 1f34eed | 2020-05-08 16:01:12 +0800 | [diff] [blame] | 5319 | QUERY_FLAG_IDN_WB_BUFF_FLUSH_EN, |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 5320 | index, NULL); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5321 | if (ret) |
| 5322 | dev_err(hba->dev, "%s WB - buf flush enable failed %d\n", |
| 5323 | __func__, ret); |
| 5324 | else |
| 5325 | hba->wb_buf_flush_enabled = true; |
| 5326 | |
| 5327 | dev_dbg(hba->dev, "WB - Flush enabled: %d\n", ret); |
| 5328 | return ret; |
| 5329 | } |
| 5330 | |
| 5331 | static int ufshcd_wb_buf_flush_disable(struct ufs_hba *hba) |
| 5332 | { |
| 5333 | int ret; |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 5334 | u8 index; |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5335 | |
Stanley Chu | 79e3520 | 2020-05-08 16:01:15 +0800 | [diff] [blame] | 5336 | if (!ufshcd_is_wb_allowed(hba) || !hba->wb_buf_flush_enabled) |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5337 | return 0; |
| 5338 | |
Stanley Chu | e31011a | 2020-05-22 16:32:11 +0800 | [diff] [blame] | 5339 | index = ufshcd_wb_get_query_index(hba); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5340 | ret = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG, |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 5341 | QUERY_FLAG_IDN_WB_BUFF_FLUSH_EN, |
| 5342 | index, NULL); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5343 | if (ret) { |
| 5344 | dev_warn(hba->dev, "%s: WB - buf flush disable failed %d\n", |
| 5345 | __func__, ret); |
| 5346 | } else { |
| 5347 | hba->wb_buf_flush_enabled = false; |
| 5348 | dev_dbg(hba->dev, "WB - Flush disabled: %d\n", ret); |
| 5349 | } |
| 5350 | |
| 5351 | return ret; |
| 5352 | } |
| 5353 | |
| 5354 | static bool ufshcd_wb_presrv_usrspc_keep_vcc_on(struct ufs_hba *hba, |
| 5355 | u32 avail_buf) |
| 5356 | { |
| 5357 | u32 cur_buf; |
| 5358 | int ret; |
Stanley Chu | e31011a | 2020-05-22 16:32:11 +0800 | [diff] [blame] | 5359 | u8 index; |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5360 | |
Stanley Chu | e31011a | 2020-05-22 16:32:11 +0800 | [diff] [blame] | 5361 | index = ufshcd_wb_get_query_index(hba); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5362 | ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
| 5363 | QUERY_ATTR_IDN_CURR_WB_BUFF_SIZE, |
Stanley Chu | e31011a | 2020-05-22 16:32:11 +0800 | [diff] [blame] | 5364 | index, 0, &cur_buf); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5365 | if (ret) { |
| 5366 | dev_err(hba->dev, "%s dCurWriteBoosterBufferSize read failed %d\n", |
| 5367 | __func__, ret); |
| 5368 | return false; |
| 5369 | } |
| 5370 | |
| 5371 | if (!cur_buf) { |
| 5372 | dev_info(hba->dev, "dCurWBBuf: %d WB disabled until free-space is available\n", |
| 5373 | cur_buf); |
| 5374 | return false; |
| 5375 | } |
Stanley Chu | d14734ae | 2020-05-09 17:37:15 +0800 | [diff] [blame] | 5376 | /* Let it continue to flush when available buffer exceeds threshold */ |
| 5377 | if (avail_buf < hba->vps->wb_flush_threshold) |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5378 | return true; |
| 5379 | |
| 5380 | return false; |
| 5381 | } |
| 5382 | |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 5383 | static bool ufshcd_wb_need_flush(struct ufs_hba *hba) |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5384 | { |
| 5385 | int ret; |
| 5386 | u32 avail_buf; |
Stanley Chu | e31011a | 2020-05-22 16:32:11 +0800 | [diff] [blame] | 5387 | u8 index; |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5388 | |
Stanley Chu | 79e3520 | 2020-05-08 16:01:15 +0800 | [diff] [blame] | 5389 | if (!ufshcd_is_wb_allowed(hba)) |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5390 | return false; |
| 5391 | /* |
| 5392 | * The ufs device needs the vcc to be ON to flush. |
| 5393 | * With user-space reduction enabled, it's enough to enable flush |
| 5394 | * by checking only the available buffer. The threshold |
| 5395 | * defined here is > 90% full. |
| 5396 | * With user-space preserved enabled, the current-buffer |
| 5397 | * should be checked too because the wb buffer size can reduce |
| 5398 | * when disk tends to be full. This info is provided by current |
| 5399 | * buffer (dCurrentWriteBoosterBufferSize). There's no point in |
| 5400 | * keeping vcc on when current buffer is empty. |
| 5401 | */ |
Stanley Chu | e31011a | 2020-05-22 16:32:11 +0800 | [diff] [blame] | 5402 | index = ufshcd_wb_get_query_index(hba); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5403 | ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
| 5404 | QUERY_ATTR_IDN_AVAIL_WB_BUFF_SIZE, |
Stanley Chu | e31011a | 2020-05-22 16:32:11 +0800 | [diff] [blame] | 5405 | index, 0, &avail_buf); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5406 | if (ret) { |
| 5407 | dev_warn(hba->dev, "%s dAvailableWriteBoosterBufferSize read failed %d\n", |
| 5408 | __func__, ret); |
| 5409 | return false; |
| 5410 | } |
| 5411 | |
| 5412 | if (!hba->dev_info.b_presrv_uspc_en) { |
Stanley Chu | d14734ae | 2020-05-09 17:37:15 +0800 | [diff] [blame] | 5413 | if (avail_buf <= UFS_WB_BUF_REMAIN_PERCENT(10)) |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5414 | return true; |
| 5415 | return false; |
| 5416 | } |
| 5417 | |
| 5418 | return ufshcd_wb_presrv_usrspc_keep_vcc_on(hba, avail_buf); |
| 5419 | } |
| 5420 | |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 5421 | static void ufshcd_rpm_dev_flush_recheck_work(struct work_struct *work) |
| 5422 | { |
| 5423 | struct ufs_hba *hba = container_of(to_delayed_work(work), |
| 5424 | struct ufs_hba, |
| 5425 | rpm_dev_flush_recheck_work); |
| 5426 | /* |
| 5427 | * To prevent unnecessary VCC power drain after device finishes |
| 5428 | * WriteBooster buffer flush or Auto BKOPs, force runtime resume |
| 5429 | * after a certain delay to recheck the threshold by next runtime |
| 5430 | * suspend. |
| 5431 | */ |
| 5432 | pm_runtime_get_sync(hba->dev); |
| 5433 | pm_runtime_put_sync(hba->dev); |
| 5434 | } |
| 5435 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5436 | /** |
| 5437 | * ufshcd_exception_event_handler - handle exceptions raised by device |
| 5438 | * @work: pointer to work data |
| 5439 | * |
| 5440 | * Read bExceptionEventStatus attribute from the device and handle the |
| 5441 | * exception event accordingly. |
| 5442 | */ |
| 5443 | static void ufshcd_exception_event_handler(struct work_struct *work) |
| 5444 | { |
| 5445 | struct ufs_hba *hba; |
| 5446 | int err; |
| 5447 | u32 status = 0; |
| 5448 | hba = container_of(work, struct ufs_hba, eeh_work); |
| 5449 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 5450 | pm_runtime_get_sync(hba->dev); |
Stanley Chu | 03e1d28 | 2019-12-24 21:01:05 +0800 | [diff] [blame] | 5451 | ufshcd_scsi_block_requests(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5452 | err = ufshcd_get_ee_status(hba, &status); |
| 5453 | if (err) { |
| 5454 | dev_err(hba->dev, "%s: failed to get exception status %d\n", |
| 5455 | __func__, err); |
| 5456 | goto out; |
| 5457 | } |
| 5458 | |
| 5459 | status &= hba->ee_ctrl_mask; |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 5460 | |
| 5461 | if (status & MASK_EE_URGENT_BKOPS) |
| 5462 | ufshcd_bkops_exception_event_handler(hba); |
| 5463 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5464 | out: |
Stanley Chu | 03e1d28 | 2019-12-24 21:01:05 +0800 | [diff] [blame] | 5465 | ufshcd_scsi_unblock_requests(hba); |
Sayali Lokhande | 2824ec9 | 2020-02-10 19:40:44 -0800 | [diff] [blame] | 5466 | /* |
| 5467 | * pm_runtime_get_noresume is called while scheduling |
| 5468 | * eeh_work to avoid suspend racing with exception work. |
| 5469 | * Hence decrement usage counter using pm_runtime_put_noidle |
| 5470 | * to allow suspend on completion of exception event handler. |
| 5471 | */ |
| 5472 | pm_runtime_put_noidle(hba->dev); |
| 5473 | pm_runtime_put(hba->dev); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5474 | return; |
| 5475 | } |
| 5476 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5477 | /* Complete requests that have door-bell cleared */ |
| 5478 | static void ufshcd_complete_requests(struct ufs_hba *hba) |
| 5479 | { |
| 5480 | ufshcd_transfer_req_compl(hba); |
| 5481 | ufshcd_tmc_handler(hba); |
| 5482 | } |
| 5483 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5484 | /** |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5485 | * ufshcd_quirk_dl_nac_errors - This function checks if error handling is |
| 5486 | * to recover from the DL NAC errors or not. |
| 5487 | * @hba: per-adapter instance |
| 5488 | * |
| 5489 | * Returns true if error handling is required, false otherwise |
| 5490 | */ |
| 5491 | static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba) |
| 5492 | { |
| 5493 | unsigned long flags; |
| 5494 | bool err_handling = true; |
| 5495 | |
| 5496 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5497 | /* |
| 5498 | * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the |
| 5499 | * device fatal error and/or DL NAC & REPLAY timeout errors. |
| 5500 | */ |
| 5501 | if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR)) |
| 5502 | goto out; |
| 5503 | |
| 5504 | if ((hba->saved_err & DEVICE_FATAL_ERROR) || |
| 5505 | ((hba->saved_err & UIC_ERROR) && |
| 5506 | (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR))) |
| 5507 | goto out; |
| 5508 | |
| 5509 | if ((hba->saved_err & UIC_ERROR) && |
| 5510 | (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) { |
| 5511 | int err; |
| 5512 | /* |
| 5513 | * wait for 50ms to see if we can get any other errors or not. |
| 5514 | */ |
| 5515 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5516 | msleep(50); |
| 5517 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5518 | |
| 5519 | /* |
| 5520 | * now check if we have got any other severe errors other than |
| 5521 | * DL NAC error? |
| 5522 | */ |
| 5523 | if ((hba->saved_err & INT_FATAL_ERRORS) || |
| 5524 | ((hba->saved_err & UIC_ERROR) && |
| 5525 | (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR))) |
| 5526 | goto out; |
| 5527 | |
| 5528 | /* |
| 5529 | * As DL NAC is the only error received so far, send out NOP |
| 5530 | * command to confirm if link is still active or not. |
| 5531 | * - If we don't get any response then do error recovery. |
| 5532 | * - If we get response then clear the DL NAC error bit. |
| 5533 | */ |
| 5534 | |
| 5535 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5536 | err = ufshcd_verify_dev_init(hba); |
| 5537 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5538 | |
| 5539 | if (err) |
| 5540 | goto out; |
| 5541 | |
| 5542 | /* Link seems to be alive hence ignore the DL NAC errors */ |
| 5543 | if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR) |
| 5544 | hba->saved_err &= ~UIC_ERROR; |
| 5545 | /* clear NAC error */ |
| 5546 | hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR; |
| 5547 | if (!hba->saved_uic_err) { |
| 5548 | err_handling = false; |
| 5549 | goto out; |
| 5550 | } |
| 5551 | } |
| 5552 | out: |
| 5553 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5554 | return err_handling; |
| 5555 | } |
| 5556 | |
| 5557 | /** |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5558 | * ufshcd_err_handler - handle UFS errors that require s/w attention |
| 5559 | * @work: pointer to work structure |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5560 | */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5561 | static void ufshcd_err_handler(struct work_struct *work) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5562 | { |
| 5563 | struct ufs_hba *hba; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5564 | unsigned long flags; |
| 5565 | u32 err_xfer = 0; |
| 5566 | u32 err_tm = 0; |
| 5567 | int err = 0; |
| 5568 | int tag; |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5569 | bool needs_reset = false; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5570 | |
| 5571 | hba = container_of(work, struct ufs_hba, eh_work); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5572 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 5573 | pm_runtime_get_sync(hba->dev); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5574 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5575 | |
| 5576 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5577 | if (hba->ufshcd_state == UFSHCD_STATE_RESET) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5578 | goto out; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5579 | |
| 5580 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 5581 | ufshcd_set_eh_in_progress(hba); |
| 5582 | |
| 5583 | /* Complete requests that have door-bell cleared by h/w */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5584 | ufshcd_complete_requests(hba); |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5585 | |
| 5586 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) { |
| 5587 | bool ret; |
| 5588 | |
| 5589 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5590 | /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */ |
| 5591 | ret = ufshcd_quirk_dl_nac_errors(hba); |
| 5592 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5593 | if (!ret) |
| 5594 | goto skip_err_handling; |
| 5595 | } |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5596 | if ((hba->saved_err & INT_FATAL_ERRORS) || |
Stanley Chu | 8217444 | 2019-05-21 14:44:54 +0800 | [diff] [blame] | 5597 | (hba->saved_err & UFSHCD_UIC_HIBERN8_MASK) || |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5598 | ((hba->saved_err & UIC_ERROR) && |
| 5599 | (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR | |
| 5600 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR | |
| 5601 | UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))) |
| 5602 | needs_reset = true; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5603 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5604 | /* |
| 5605 | * if host reset is required then skip clearing the pending |
Can Guo | 2df74b6 | 2019-11-25 22:53:33 -0800 | [diff] [blame] | 5606 | * transfers forcefully because they will get cleared during |
| 5607 | * host reset and restore |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5608 | */ |
| 5609 | if (needs_reset) |
| 5610 | goto skip_pending_xfer_clear; |
| 5611 | |
| 5612 | /* release lock as clear command might sleep */ |
| 5613 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5614 | /* Clear pending transfer requests */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5615 | for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) { |
| 5616 | if (ufshcd_clear_cmd(hba, tag)) { |
| 5617 | err_xfer = true; |
| 5618 | goto lock_skip_pending_xfer_clear; |
| 5619 | } |
| 5620 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5621 | |
| 5622 | /* Clear pending task management requests */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5623 | for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) { |
| 5624 | if (ufshcd_clear_tm_cmd(hba, tag)) { |
| 5625 | err_tm = true; |
| 5626 | goto lock_skip_pending_xfer_clear; |
| 5627 | } |
| 5628 | } |
| 5629 | |
| 5630 | lock_skip_pending_xfer_clear: |
| 5631 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5632 | |
| 5633 | /* Complete the requests that are cleared by s/w */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5634 | ufshcd_complete_requests(hba); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5635 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5636 | if (err_xfer || err_tm) |
| 5637 | needs_reset = true; |
| 5638 | |
| 5639 | skip_pending_xfer_clear: |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5640 | /* Fatal errors need reset */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5641 | if (needs_reset) { |
| 5642 | unsigned long max_doorbells = (1UL << hba->nutrs) - 1; |
| 5643 | |
| 5644 | /* |
| 5645 | * ufshcd_reset_and_restore() does the link reinitialization |
| 5646 | * which will need atleast one empty doorbell slot to send the |
| 5647 | * device management commands (NOP and query commands). |
| 5648 | * If there is no slot empty at this moment then free up last |
| 5649 | * slot forcefully. |
| 5650 | */ |
| 5651 | if (hba->outstanding_reqs == max_doorbells) |
| 5652 | __ufshcd_transfer_req_compl(hba, |
| 5653 | (1UL << (hba->nutrs - 1))); |
| 5654 | |
| 5655 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5656 | err = ufshcd_reset_and_restore(hba); |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5657 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5658 | if (err) { |
| 5659 | dev_err(hba->dev, "%s: reset and restore failed\n", |
| 5660 | __func__); |
| 5661 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 5662 | } |
| 5663 | /* |
| 5664 | * Inform scsi mid-layer that we did reset and allow to handle |
| 5665 | * Unit Attention properly. |
| 5666 | */ |
| 5667 | scsi_report_bus_reset(hba->host, 0); |
| 5668 | hba->saved_err = 0; |
| 5669 | hba->saved_uic_err = 0; |
| 5670 | } |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5671 | |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5672 | skip_err_handling: |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5673 | if (!needs_reset) { |
| 5674 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
| 5675 | if (hba->saved_err || hba->saved_uic_err) |
| 5676 | dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x", |
| 5677 | __func__, hba->saved_err, hba->saved_uic_err); |
| 5678 | } |
| 5679 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5680 | ufshcd_clear_eh_in_progress(hba); |
| 5681 | |
| 5682 | out: |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5683 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 5684 | ufshcd_scsi_unblock_requests(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5685 | ufshcd_release(hba); |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 5686 | pm_runtime_put_sync(hba->dev); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5687 | } |
| 5688 | |
| 5689 | /** |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5690 | * ufshcd_update_uic_error - check and set fatal UIC error flags. |
| 5691 | * @hba: per-adapter instance |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5692 | * |
| 5693 | * Returns |
| 5694 | * IRQ_HANDLED - If interrupt is valid |
| 5695 | * IRQ_NONE - If invalid interrupt |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5696 | */ |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5697 | static irqreturn_t ufshcd_update_uic_error(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5698 | { |
| 5699 | u32 reg; |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5700 | irqreturn_t retval = IRQ_NONE; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5701 | |
Dolev Raviv | fb7b45f | 2016-11-23 16:32:32 -0800 | [diff] [blame] | 5702 | /* PHY layer lane error */ |
| 5703 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER); |
| 5704 | /* Ignore LINERESET indication, as this is not an error */ |
| 5705 | if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) && |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5706 | (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK)) { |
Dolev Raviv | fb7b45f | 2016-11-23 16:32:32 -0800 | [diff] [blame] | 5707 | /* |
| 5708 | * To know whether this error is fatal or not, DB timeout |
| 5709 | * must be checked but this error is handled separately. |
| 5710 | */ |
| 5711 | dev_dbg(hba->dev, "%s: UIC Lane error reported\n", __func__); |
Stanley Chu | 48d5b97 | 2019-07-10 21:38:18 +0800 | [diff] [blame] | 5712 | ufshcd_update_reg_hist(&hba->ufs_stats.pa_err, reg); |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5713 | retval |= IRQ_HANDLED; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5714 | } |
Dolev Raviv | fb7b45f | 2016-11-23 16:32:32 -0800 | [diff] [blame] | 5715 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5716 | /* PA_INIT_ERROR is fatal and needs UIC reset */ |
| 5717 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER); |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5718 | if ((reg & UIC_DATA_LINK_LAYER_ERROR) && |
| 5719 | (reg & UIC_DATA_LINK_LAYER_ERROR_CODE_MASK)) { |
Stanley Chu | 48d5b97 | 2019-07-10 21:38:18 +0800 | [diff] [blame] | 5720 | ufshcd_update_reg_hist(&hba->ufs_stats.dl_err, reg); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5721 | |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5722 | if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT) |
| 5723 | hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR; |
| 5724 | else if (hba->dev_quirks & |
| 5725 | UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) { |
| 5726 | if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED) |
| 5727 | hba->uic_error |= |
| 5728 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR; |
| 5729 | else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT) |
| 5730 | hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR; |
| 5731 | } |
| 5732 | retval |= IRQ_HANDLED; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5733 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5734 | |
| 5735 | /* UIC NL/TL/DME errors needs software retry */ |
| 5736 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER); |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5737 | if ((reg & UIC_NETWORK_LAYER_ERROR) && |
| 5738 | (reg & UIC_NETWORK_LAYER_ERROR_CODE_MASK)) { |
Stanley Chu | 48d5b97 | 2019-07-10 21:38:18 +0800 | [diff] [blame] | 5739 | ufshcd_update_reg_hist(&hba->ufs_stats.nl_err, reg); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5740 | hba->uic_error |= UFSHCD_UIC_NL_ERROR; |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5741 | retval |= IRQ_HANDLED; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5742 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5743 | |
| 5744 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER); |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5745 | if ((reg & UIC_TRANSPORT_LAYER_ERROR) && |
| 5746 | (reg & UIC_TRANSPORT_LAYER_ERROR_CODE_MASK)) { |
Stanley Chu | 48d5b97 | 2019-07-10 21:38:18 +0800 | [diff] [blame] | 5747 | ufshcd_update_reg_hist(&hba->ufs_stats.tl_err, reg); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5748 | hba->uic_error |= UFSHCD_UIC_TL_ERROR; |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5749 | retval |= IRQ_HANDLED; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5750 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5751 | |
| 5752 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME); |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5753 | if ((reg & UIC_DME_ERROR) && |
| 5754 | (reg & UIC_DME_ERROR_CODE_MASK)) { |
Stanley Chu | 48d5b97 | 2019-07-10 21:38:18 +0800 | [diff] [blame] | 5755 | ufshcd_update_reg_hist(&hba->ufs_stats.dme_err, reg); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5756 | hba->uic_error |= UFSHCD_UIC_DME_ERROR; |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5757 | retval |= IRQ_HANDLED; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5758 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5759 | |
| 5760 | dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n", |
| 5761 | __func__, hba->uic_error); |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5762 | return retval; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5763 | } |
| 5764 | |
Stanley Chu | 8217444 | 2019-05-21 14:44:54 +0800 | [diff] [blame] | 5765 | static bool ufshcd_is_auto_hibern8_error(struct ufs_hba *hba, |
| 5766 | u32 intr_mask) |
| 5767 | { |
Stanley Chu | 5a244e0 | 2020-01-29 18:52:50 +0800 | [diff] [blame] | 5768 | if (!ufshcd_is_auto_hibern8_supported(hba) || |
| 5769 | !ufshcd_is_auto_hibern8_enabled(hba)) |
Stanley Chu | 8217444 | 2019-05-21 14:44:54 +0800 | [diff] [blame] | 5770 | return false; |
| 5771 | |
| 5772 | if (!(intr_mask & UFSHCD_UIC_HIBERN8_MASK)) |
| 5773 | return false; |
| 5774 | |
| 5775 | if (hba->active_uic_cmd && |
| 5776 | (hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_ENTER || |
| 5777 | hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_EXIT)) |
| 5778 | return false; |
| 5779 | |
| 5780 | return true; |
| 5781 | } |
| 5782 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5783 | /** |
| 5784 | * ufshcd_check_errors - Check for errors that need s/w attention |
| 5785 | * @hba: per-adapter instance |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5786 | * |
| 5787 | * Returns |
| 5788 | * IRQ_HANDLED - If interrupt is valid |
| 5789 | * IRQ_NONE - If invalid interrupt |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5790 | */ |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5791 | static irqreturn_t ufshcd_check_errors(struct ufs_hba *hba) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5792 | { |
| 5793 | bool queue_eh_work = false; |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5794 | irqreturn_t retval = IRQ_NONE; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5795 | |
Stanley Chu | d3c615b | 2019-07-10 21:38:19 +0800 | [diff] [blame] | 5796 | if (hba->errors & INT_FATAL_ERRORS) { |
| 5797 | ufshcd_update_reg_hist(&hba->ufs_stats.fatal_err, hba->errors); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5798 | queue_eh_work = true; |
Stanley Chu | d3c615b | 2019-07-10 21:38:19 +0800 | [diff] [blame] | 5799 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5800 | |
| 5801 | if (hba->errors & UIC_ERROR) { |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5802 | hba->uic_error = 0; |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5803 | retval = ufshcd_update_uic_error(hba); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5804 | if (hba->uic_error) |
| 5805 | queue_eh_work = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5806 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5807 | |
Stanley Chu | 8217444 | 2019-05-21 14:44:54 +0800 | [diff] [blame] | 5808 | if (hba->errors & UFSHCD_UIC_HIBERN8_MASK) { |
| 5809 | dev_err(hba->dev, |
| 5810 | "%s: Auto Hibern8 %s failed - status: 0x%08x, upmcrs: 0x%08x\n", |
| 5811 | __func__, (hba->errors & UIC_HIBERNATE_ENTER) ? |
| 5812 | "Enter" : "Exit", |
| 5813 | hba->errors, ufshcd_get_upmcrs(hba)); |
Stanley Chu | d3c615b | 2019-07-10 21:38:19 +0800 | [diff] [blame] | 5814 | ufshcd_update_reg_hist(&hba->ufs_stats.auto_hibern8_err, |
| 5815 | hba->errors); |
Stanley Chu | 8217444 | 2019-05-21 14:44:54 +0800 | [diff] [blame] | 5816 | queue_eh_work = true; |
| 5817 | } |
| 5818 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5819 | if (queue_eh_work) { |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5820 | /* |
| 5821 | * update the transfer error masks to sticky bits, let's do this |
| 5822 | * irrespective of current ufshcd_state. |
| 5823 | */ |
| 5824 | hba->saved_err |= hba->errors; |
| 5825 | hba->saved_uic_err |= hba->uic_error; |
| 5826 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5827 | /* handle fatal errors only when link is functional */ |
| 5828 | if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) { |
| 5829 | /* block commands from scsi mid-layer */ |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 5830 | ufshcd_scsi_block_requests(hba); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5831 | |
Zang Leigang | 141f816 | 2016-11-16 11:29:37 +0800 | [diff] [blame] | 5832 | hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED; |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 5833 | |
| 5834 | /* dump controller state before resetting */ |
| 5835 | if (hba->saved_err & (INT_FATAL_ERRORS | UIC_ERROR)) { |
| 5836 | bool pr_prdt = !!(hba->saved_err & |
| 5837 | SYSTEM_BUS_FATAL_ERROR); |
| 5838 | |
| 5839 | dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x\n", |
| 5840 | __func__, hba->saved_err, |
| 5841 | hba->saved_uic_err); |
| 5842 | |
| 5843 | ufshcd_print_host_regs(hba); |
| 5844 | ufshcd_print_pwr_info(hba); |
| 5845 | ufshcd_print_tmrs(hba, hba->outstanding_tasks); |
| 5846 | ufshcd_print_trs(hba, hba->outstanding_reqs, |
| 5847 | pr_prdt); |
| 5848 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5849 | schedule_work(&hba->eh_work); |
| 5850 | } |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5851 | retval |= IRQ_HANDLED; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5852 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5853 | /* |
| 5854 | * if (!queue_eh_work) - |
| 5855 | * Other errors are either non-fatal where host recovers |
| 5856 | * itself without s/w intervention or errors that will be |
| 5857 | * handled by the SCSI core layer. |
| 5858 | */ |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5859 | return retval; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5860 | } |
| 5861 | |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 5862 | struct ctm_info { |
| 5863 | struct ufs_hba *hba; |
| 5864 | unsigned long pending; |
| 5865 | unsigned int ncpl; |
| 5866 | }; |
| 5867 | |
| 5868 | static bool ufshcd_compl_tm(struct request *req, void *priv, bool reserved) |
| 5869 | { |
| 5870 | struct ctm_info *const ci = priv; |
| 5871 | struct completion *c; |
| 5872 | |
| 5873 | WARN_ON_ONCE(reserved); |
| 5874 | if (test_bit(req->tag, &ci->pending)) |
| 5875 | return true; |
| 5876 | ci->ncpl++; |
| 5877 | c = req->end_io_data; |
| 5878 | if (c) |
| 5879 | complete(c); |
| 5880 | return true; |
| 5881 | } |
| 5882 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5883 | /** |
| 5884 | * ufshcd_tmc_handler - handle task management function completion |
| 5885 | * @hba: per adapter instance |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5886 | * |
| 5887 | * Returns |
| 5888 | * IRQ_HANDLED - If interrupt is valid |
| 5889 | * IRQ_NONE - If invalid interrupt |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5890 | */ |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5891 | static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5892 | { |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 5893 | struct request_queue *q = hba->tmf_queue; |
| 5894 | struct ctm_info ci = { |
| 5895 | .hba = hba, |
| 5896 | .pending = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL), |
| 5897 | }; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5898 | |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 5899 | blk_mq_tagset_busy_iter(q->tag_set, ufshcd_compl_tm, &ci); |
| 5900 | return ci.ncpl ? IRQ_HANDLED : IRQ_NONE; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5901 | } |
| 5902 | |
| 5903 | /** |
| 5904 | * ufshcd_sl_intr - Interrupt service routine |
| 5905 | * @hba: per adapter instance |
| 5906 | * @intr_status: contains interrupts generated by the controller |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5907 | * |
| 5908 | * Returns |
| 5909 | * IRQ_HANDLED - If interrupt is valid |
| 5910 | * IRQ_NONE - If invalid interrupt |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5911 | */ |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5912 | static irqreturn_t ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5913 | { |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5914 | irqreturn_t retval = IRQ_NONE; |
| 5915 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5916 | hba->errors = UFSHCD_ERROR_MASK & intr_status; |
Stanley Chu | 8217444 | 2019-05-21 14:44:54 +0800 | [diff] [blame] | 5917 | |
| 5918 | if (ufshcd_is_auto_hibern8_error(hba, intr_status)) |
| 5919 | hba->errors |= (UFSHCD_UIC_HIBERN8_MASK & intr_status); |
| 5920 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5921 | if (hba->errors) |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5922 | retval |= ufshcd_check_errors(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5923 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 5924 | if (intr_status & UFSHCD_UIC_MASK) |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5925 | retval |= ufshcd_uic_cmd_compl(hba, intr_status); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5926 | |
| 5927 | if (intr_status & UTP_TASK_REQ_COMPL) |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5928 | retval |= ufshcd_tmc_handler(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5929 | |
| 5930 | if (intr_status & UTP_TRANSFER_REQ_COMPL) |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5931 | retval |= ufshcd_transfer_req_compl(hba); |
| 5932 | |
| 5933 | return retval; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5934 | } |
| 5935 | |
| 5936 | /** |
| 5937 | * ufshcd_intr - Main interrupt service routine |
| 5938 | * @irq: irq number |
| 5939 | * @__hba: pointer to adapter instance |
| 5940 | * |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5941 | * Returns |
| 5942 | * IRQ_HANDLED - If interrupt is valid |
| 5943 | * IRQ_NONE - If invalid interrupt |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5944 | */ |
| 5945 | static irqreturn_t ufshcd_intr(int irq, void *__hba) |
| 5946 | { |
Adrian Hunter | 127d5f7 | 2020-08-11 16:39:36 +0300 | [diff] [blame^] | 5947 | u32 intr_status, enabled_intr_status = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5948 | irqreturn_t retval = IRQ_NONE; |
| 5949 | struct ufs_hba *hba = __hba; |
Venkat Gopalakrishnan | 7f6ba4f | 2018-05-03 16:37:20 +0530 | [diff] [blame] | 5950 | int retries = hba->nutrs; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5951 | |
| 5952 | spin_lock(hba->host->host_lock); |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 5953 | intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5954 | |
Venkat Gopalakrishnan | 7f6ba4f | 2018-05-03 16:37:20 +0530 | [diff] [blame] | 5955 | /* |
| 5956 | * There could be max of hba->nutrs reqs in flight and in worst case |
| 5957 | * if the reqs get finished 1 by 1 after the interrupt status is |
| 5958 | * read, make sure we handle them by checking the interrupt status |
| 5959 | * again in a loop until we process all of the reqs before returning. |
| 5960 | */ |
Adrian Hunter | 127d5f7 | 2020-08-11 16:39:36 +0300 | [diff] [blame^] | 5961 | while (intr_status && retries--) { |
Venkat Gopalakrishnan | 7f6ba4f | 2018-05-03 16:37:20 +0530 | [diff] [blame] | 5962 | enabled_intr_status = |
| 5963 | intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
| 5964 | if (intr_status) |
| 5965 | ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS); |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5966 | if (enabled_intr_status) |
| 5967 | retval |= ufshcd_sl_intr(hba, enabled_intr_status); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 5968 | |
Venkat Gopalakrishnan | 7f6ba4f | 2018-05-03 16:37:20 +0530 | [diff] [blame] | 5969 | intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS); |
Adrian Hunter | 127d5f7 | 2020-08-11 16:39:36 +0300 | [diff] [blame^] | 5970 | } |
Venkat Gopalakrishnan | 7f6ba4f | 2018-05-03 16:37:20 +0530 | [diff] [blame] | 5971 | |
Adrian Hunter | 6337f58 | 2020-08-11 16:39:35 +0300 | [diff] [blame] | 5972 | if (enabled_intr_status && retval == IRQ_NONE) { |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5973 | dev_err(hba->dev, "%s: Unhandled interrupt 0x%08x\n", |
| 5974 | __func__, intr_status); |
| 5975 | ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: "); |
| 5976 | } |
| 5977 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5978 | spin_unlock(hba->host->host_lock); |
| 5979 | return retval; |
| 5980 | } |
| 5981 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5982 | static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag) |
| 5983 | { |
| 5984 | int err = 0; |
| 5985 | u32 mask = 1 << tag; |
| 5986 | unsigned long flags; |
| 5987 | |
| 5988 | if (!test_bit(tag, &hba->outstanding_tasks)) |
| 5989 | goto out; |
| 5990 | |
| 5991 | spin_lock_irqsave(hba->host->host_lock, flags); |
Alim Akhtar | 1399c5b | 2018-05-06 15:44:15 +0530 | [diff] [blame] | 5992 | ufshcd_utmrl_clear(hba, tag); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5993 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5994 | |
| 5995 | /* poll for max. 1 sec to clear door bell register by h/w */ |
| 5996 | err = ufshcd_wait_for_register(hba, |
| 5997 | REG_UTP_TASK_REQ_DOOR_BELL, |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 5998 | mask, 0, 1000, 1000); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5999 | out: |
| 6000 | return err; |
| 6001 | } |
| 6002 | |
Christoph Hellwig | c6049cd | 2018-10-07 17:30:33 +0300 | [diff] [blame] | 6003 | static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba, |
| 6004 | struct utp_task_req_desc *treq, u8 tm_function) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6005 | { |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 6006 | struct request_queue *q = hba->tmf_queue; |
Christoph Hellwig | c6049cd | 2018-10-07 17:30:33 +0300 | [diff] [blame] | 6007 | struct Scsi_Host *host = hba->host; |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 6008 | DECLARE_COMPLETION_ONSTACK(wait); |
| 6009 | struct request *req; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6010 | unsigned long flags; |
Christoph Hellwig | c6049cd | 2018-10-07 17:30:33 +0300 | [diff] [blame] | 6011 | int free_slot, task_tag, err; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6012 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6013 | /* |
| 6014 | * Get free slot, sleep if slots are unavailable. |
| 6015 | * Even though we use wait_event() which sleeps indefinitely, |
| 6016 | * the maximum wait time is bounded by %TM_CMD_TIMEOUT. |
| 6017 | */ |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 6018 | req = blk_get_request(q, REQ_OP_DRV_OUT, BLK_MQ_REQ_RESERVED); |
| 6019 | req->end_io_data = &wait; |
| 6020 | free_slot = req->tag; |
| 6021 | WARN_ON_ONCE(free_slot < 0 || free_slot >= hba->nutmrs); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6022 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6023 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6024 | spin_lock_irqsave(host->host_lock, flags); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6025 | task_tag = hba->nutrs + free_slot; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6026 | |
Christoph Hellwig | c6049cd | 2018-10-07 17:30:33 +0300 | [diff] [blame] | 6027 | treq->req_header.dword_0 |= cpu_to_be32(task_tag); |
| 6028 | |
| 6029 | memcpy(hba->utmrdl_base_addr + free_slot, treq, sizeof(*treq)); |
Kiwoong Kim | d2877be | 2016-11-10 21:16:15 +0900 | [diff] [blame] | 6030 | ufshcd_vops_setup_task_mgmt(hba, free_slot, tm_function); |
| 6031 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6032 | /* send command to the controller */ |
| 6033 | __set_bit(free_slot, &hba->outstanding_tasks); |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 6034 | |
| 6035 | /* Make sure descriptors are ready before ringing the task doorbell */ |
| 6036 | wmb(); |
| 6037 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 6038 | ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL); |
Gilad Broner | ad1a1b9 | 2016-10-17 17:09:36 -0700 | [diff] [blame] | 6039 | /* Make sure that doorbell is committed immediately */ |
| 6040 | wmb(); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6041 | |
| 6042 | spin_unlock_irqrestore(host->host_lock, flags); |
| 6043 | |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 6044 | ufshcd_add_tm_upiu_trace(hba, task_tag, "tm_send"); |
| 6045 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6046 | /* wait until the task management command is completed */ |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 6047 | err = wait_for_completion_io_timeout(&wait, |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6048 | msecs_to_jiffies(TM_CMD_TIMEOUT)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6049 | if (!err) { |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 6050 | /* |
| 6051 | * Make sure that ufshcd_compl_tm() does not trigger a |
| 6052 | * use-after-free. |
| 6053 | */ |
| 6054 | req->end_io_data = NULL; |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 6055 | ufshcd_add_tm_upiu_trace(hba, task_tag, "tm_complete_err"); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6056 | dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n", |
| 6057 | __func__, tm_function); |
| 6058 | if (ufshcd_clear_tm_cmd(hba, free_slot)) |
| 6059 | dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n", |
| 6060 | __func__, free_slot); |
| 6061 | err = -ETIMEDOUT; |
| 6062 | } else { |
Christoph Hellwig | c6049cd | 2018-10-07 17:30:33 +0300 | [diff] [blame] | 6063 | err = 0; |
| 6064 | memcpy(treq, hba->utmrdl_base_addr + free_slot, sizeof(*treq)); |
| 6065 | |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 6066 | ufshcd_add_tm_upiu_trace(hba, task_tag, "tm_complete"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6067 | } |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6068 | |
Stanley Chu | b557217 | 2019-08-19 21:43:28 +0800 | [diff] [blame] | 6069 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 6070 | __clear_bit(free_slot, &hba->outstanding_tasks); |
| 6071 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6072 | |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 6073 | blk_put_request(req); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6074 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6075 | ufshcd_release(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6076 | return err; |
| 6077 | } |
| 6078 | |
| 6079 | /** |
Christoph Hellwig | c6049cd | 2018-10-07 17:30:33 +0300 | [diff] [blame] | 6080 | * ufshcd_issue_tm_cmd - issues task management commands to controller |
| 6081 | * @hba: per adapter instance |
| 6082 | * @lun_id: LUN ID to which TM command is sent |
| 6083 | * @task_id: task ID to which the TM command is applicable |
| 6084 | * @tm_function: task management function opcode |
| 6085 | * @tm_response: task management service response return value |
| 6086 | * |
| 6087 | * Returns non-zero value on error, zero on success. |
| 6088 | */ |
| 6089 | static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id, |
| 6090 | u8 tm_function, u8 *tm_response) |
| 6091 | { |
| 6092 | struct utp_task_req_desc treq = { { 0 }, }; |
| 6093 | int ocs_value, err; |
| 6094 | |
| 6095 | /* Configure task request descriptor */ |
| 6096 | treq.header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD); |
| 6097 | treq.header.dword_2 = cpu_to_le32(OCS_INVALID_COMMAND_STATUS); |
| 6098 | |
| 6099 | /* Configure task request UPIU */ |
| 6100 | treq.req_header.dword_0 = cpu_to_be32(lun_id << 8) | |
| 6101 | cpu_to_be32(UPIU_TRANSACTION_TASK_REQ << 24); |
| 6102 | treq.req_header.dword_1 = cpu_to_be32(tm_function << 16); |
| 6103 | |
| 6104 | /* |
| 6105 | * The host shall provide the same value for LUN field in the basic |
| 6106 | * header and for Input Parameter. |
| 6107 | */ |
| 6108 | treq.input_param1 = cpu_to_be32(lun_id); |
| 6109 | treq.input_param2 = cpu_to_be32(task_id); |
| 6110 | |
| 6111 | err = __ufshcd_issue_tm_cmd(hba, &treq, tm_function); |
| 6112 | if (err == -ETIMEDOUT) |
| 6113 | return err; |
| 6114 | |
| 6115 | ocs_value = le32_to_cpu(treq.header.dword_2) & MASK_OCS; |
| 6116 | if (ocs_value != OCS_SUCCESS) |
| 6117 | dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", |
| 6118 | __func__, ocs_value); |
| 6119 | else if (tm_response) |
| 6120 | *tm_response = be32_to_cpu(treq.output_param1) & |
| 6121 | MASK_TM_SERVICE_RESP; |
| 6122 | return err; |
| 6123 | } |
| 6124 | |
| 6125 | /** |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6126 | * ufshcd_issue_devman_upiu_cmd - API for sending "utrd" type requests |
| 6127 | * @hba: per-adapter instance |
| 6128 | * @req_upiu: upiu request |
| 6129 | * @rsp_upiu: upiu reply |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6130 | * @desc_buff: pointer to descriptor buffer, NULL if NA |
| 6131 | * @buff_len: descriptor size, 0 if NA |
Bart Van Assche | d0e9760 | 2019-10-29 16:07:08 -0700 | [diff] [blame] | 6132 | * @cmd_type: specifies the type (NOP, Query...) |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6133 | * @desc_op: descriptor operation |
| 6134 | * |
| 6135 | * Those type of requests uses UTP Transfer Request Descriptor - utrd. |
| 6136 | * Therefore, it "rides" the device management infrastructure: uses its tag and |
| 6137 | * tasks work queues. |
| 6138 | * |
| 6139 | * Since there is only one available tag for device management commands, |
| 6140 | * the caller is expected to hold the hba->dev_cmd.lock mutex. |
| 6141 | */ |
| 6142 | static int ufshcd_issue_devman_upiu_cmd(struct ufs_hba *hba, |
| 6143 | struct utp_upiu_req *req_upiu, |
| 6144 | struct utp_upiu_req *rsp_upiu, |
| 6145 | u8 *desc_buff, int *buff_len, |
Bart Van Assche | 7f674c3 | 2019-10-29 16:07:09 -0700 | [diff] [blame] | 6146 | enum dev_cmd_type cmd_type, |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6147 | enum query_opcode desc_op) |
| 6148 | { |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 6149 | struct request_queue *q = hba->cmd_queue; |
| 6150 | struct request *req; |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6151 | struct ufshcd_lrb *lrbp; |
| 6152 | int err = 0; |
| 6153 | int tag; |
| 6154 | struct completion wait; |
| 6155 | unsigned long flags; |
Bean Huo | a23064c | 2020-07-06 14:39:36 +0200 | [diff] [blame] | 6156 | u8 upiu_flags; |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6157 | |
| 6158 | down_read(&hba->clk_scaling_lock); |
| 6159 | |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 6160 | req = blk_get_request(q, REQ_OP_DRV_OUT, 0); |
Dan Carpenter | bb14dd1 | 2019-12-13 13:48:28 +0300 | [diff] [blame] | 6161 | if (IS_ERR(req)) { |
| 6162 | err = PTR_ERR(req); |
| 6163 | goto out_unlock; |
| 6164 | } |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 6165 | tag = req->tag; |
| 6166 | WARN_ON_ONCE(!ufshcd_valid_tag(hba, tag)); |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6167 | |
| 6168 | init_completion(&wait); |
| 6169 | lrbp = &hba->lrb[tag]; |
| 6170 | WARN_ON(lrbp->cmd); |
| 6171 | |
| 6172 | lrbp->cmd = NULL; |
| 6173 | lrbp->sense_bufflen = 0; |
| 6174 | lrbp->sense_buffer = NULL; |
| 6175 | lrbp->task_tag = tag; |
| 6176 | lrbp->lun = 0; |
| 6177 | lrbp->intr_cmd = true; |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 6178 | ufshcd_prepare_lrbp_crypto(NULL, lrbp); |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6179 | hba->dev_cmd.type = cmd_type; |
| 6180 | |
| 6181 | switch (hba->ufs_version) { |
| 6182 | case UFSHCI_VERSION_10: |
| 6183 | case UFSHCI_VERSION_11: |
| 6184 | lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE; |
| 6185 | break; |
| 6186 | default: |
| 6187 | lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE; |
| 6188 | break; |
| 6189 | } |
| 6190 | |
| 6191 | /* update the task tag in the request upiu */ |
| 6192 | req_upiu->header.dword_0 |= cpu_to_be32(tag); |
| 6193 | |
| 6194 | ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE); |
| 6195 | |
| 6196 | /* just copy the upiu request as it is */ |
| 6197 | memcpy(lrbp->ucd_req_ptr, req_upiu, sizeof(*lrbp->ucd_req_ptr)); |
| 6198 | if (desc_buff && desc_op == UPIU_QUERY_OPCODE_WRITE_DESC) { |
| 6199 | /* The Data Segment Area is optional depending upon the query |
| 6200 | * function value. for WRITE DESCRIPTOR, the data segment |
| 6201 | * follows right after the tsf. |
| 6202 | */ |
| 6203 | memcpy(lrbp->ucd_req_ptr + 1, desc_buff, *buff_len); |
| 6204 | *buff_len = 0; |
| 6205 | } |
| 6206 | |
| 6207 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
| 6208 | |
| 6209 | hba->dev_cmd.complete = &wait; |
| 6210 | |
| 6211 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 6212 | wmb(); |
| 6213 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 6214 | ufshcd_send_command(hba, tag); |
| 6215 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6216 | |
| 6217 | /* |
| 6218 | * ignore the returning value here - ufshcd_check_query_response is |
| 6219 | * bound to fail since dev_cmd.query and dev_cmd.type were left empty. |
| 6220 | * read the response directly ignoring all errors. |
| 6221 | */ |
| 6222 | ufshcd_wait_for_dev_cmd(hba, lrbp, QUERY_REQ_TIMEOUT); |
| 6223 | |
| 6224 | /* just copy the upiu response as it is */ |
| 6225 | memcpy(rsp_upiu, lrbp->ucd_rsp_ptr, sizeof(*rsp_upiu)); |
Avri Altman | 4bbbe24 | 2019-02-20 09:11:13 +0200 | [diff] [blame] | 6226 | if (desc_buff && desc_op == UPIU_QUERY_OPCODE_READ_DESC) { |
| 6227 | u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + sizeof(*rsp_upiu); |
| 6228 | u16 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) & |
| 6229 | MASK_QUERY_DATA_SEG_LEN; |
| 6230 | |
| 6231 | if (*buff_len >= resp_len) { |
| 6232 | memcpy(desc_buff, descp, resp_len); |
| 6233 | *buff_len = resp_len; |
| 6234 | } else { |
Bean Huo | 3d4881d | 2019-11-12 23:34:35 +0100 | [diff] [blame] | 6235 | dev_warn(hba->dev, |
| 6236 | "%s: rsp size %d is bigger than buffer size %d", |
| 6237 | __func__, resp_len, *buff_len); |
Avri Altman | 4bbbe24 | 2019-02-20 09:11:13 +0200 | [diff] [blame] | 6238 | *buff_len = 0; |
| 6239 | err = -EINVAL; |
| 6240 | } |
| 6241 | } |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6242 | |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 6243 | blk_put_request(req); |
Dan Carpenter | bb14dd1 | 2019-12-13 13:48:28 +0300 | [diff] [blame] | 6244 | out_unlock: |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6245 | up_read(&hba->clk_scaling_lock); |
| 6246 | return err; |
| 6247 | } |
| 6248 | |
| 6249 | /** |
| 6250 | * ufshcd_exec_raw_upiu_cmd - API function for sending raw upiu commands |
| 6251 | * @hba: per-adapter instance |
| 6252 | * @req_upiu: upiu request |
| 6253 | * @rsp_upiu: upiu reply - only 8 DW as we do not support scsi commands |
| 6254 | * @msgcode: message code, one of UPIU Transaction Codes Initiator to Target |
| 6255 | * @desc_buff: pointer to descriptor buffer, NULL if NA |
| 6256 | * @buff_len: descriptor size, 0 if NA |
| 6257 | * @desc_op: descriptor operation |
| 6258 | * |
| 6259 | * Supports UTP Transfer requests (nop and query), and UTP Task |
| 6260 | * Management requests. |
| 6261 | * It is up to the caller to fill the upiu conent properly, as it will |
| 6262 | * be copied without any further input validations. |
| 6263 | */ |
| 6264 | int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba, |
| 6265 | struct utp_upiu_req *req_upiu, |
| 6266 | struct utp_upiu_req *rsp_upiu, |
| 6267 | int msgcode, |
| 6268 | u8 *desc_buff, int *buff_len, |
| 6269 | enum query_opcode desc_op) |
| 6270 | { |
| 6271 | int err; |
Bart Van Assche | 7f674c3 | 2019-10-29 16:07:09 -0700 | [diff] [blame] | 6272 | enum dev_cmd_type cmd_type = DEV_CMD_TYPE_QUERY; |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6273 | struct utp_task_req_desc treq = { { 0 }, }; |
| 6274 | int ocs_value; |
| 6275 | u8 tm_f = be32_to_cpu(req_upiu->header.dword_1) >> 16 & MASK_TM_FUNC; |
| 6276 | |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6277 | switch (msgcode) { |
| 6278 | case UPIU_TRANSACTION_NOP_OUT: |
| 6279 | cmd_type = DEV_CMD_TYPE_NOP; |
| 6280 | /* fall through */ |
| 6281 | case UPIU_TRANSACTION_QUERY_REQ: |
| 6282 | ufshcd_hold(hba, false); |
| 6283 | mutex_lock(&hba->dev_cmd.lock); |
| 6284 | err = ufshcd_issue_devman_upiu_cmd(hba, req_upiu, rsp_upiu, |
| 6285 | desc_buff, buff_len, |
| 6286 | cmd_type, desc_op); |
| 6287 | mutex_unlock(&hba->dev_cmd.lock); |
| 6288 | ufshcd_release(hba); |
| 6289 | |
| 6290 | break; |
| 6291 | case UPIU_TRANSACTION_TASK_REQ: |
| 6292 | treq.header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD); |
| 6293 | treq.header.dword_2 = cpu_to_le32(OCS_INVALID_COMMAND_STATUS); |
| 6294 | |
| 6295 | memcpy(&treq.req_header, req_upiu, sizeof(*req_upiu)); |
| 6296 | |
| 6297 | err = __ufshcd_issue_tm_cmd(hba, &treq, tm_f); |
| 6298 | if (err == -ETIMEDOUT) |
| 6299 | break; |
| 6300 | |
| 6301 | ocs_value = le32_to_cpu(treq.header.dword_2) & MASK_OCS; |
| 6302 | if (ocs_value != OCS_SUCCESS) { |
| 6303 | dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", __func__, |
| 6304 | ocs_value); |
| 6305 | break; |
| 6306 | } |
| 6307 | |
| 6308 | memcpy(rsp_upiu, &treq.rsp_header, sizeof(*rsp_upiu)); |
| 6309 | |
| 6310 | break; |
| 6311 | default: |
| 6312 | err = -EINVAL; |
| 6313 | |
| 6314 | break; |
| 6315 | } |
| 6316 | |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6317 | return err; |
| 6318 | } |
| 6319 | |
| 6320 | /** |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6321 | * ufshcd_eh_device_reset_handler - device reset handler registered to |
| 6322 | * scsi layer. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6323 | * @cmd: SCSI command pointer |
| 6324 | * |
| 6325 | * Returns SUCCESS/FAILED |
| 6326 | */ |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6327 | static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6328 | { |
| 6329 | struct Scsi_Host *host; |
| 6330 | struct ufs_hba *hba; |
| 6331 | unsigned int tag; |
| 6332 | u32 pos; |
| 6333 | int err; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6334 | u8 resp = 0xF; |
| 6335 | struct ufshcd_lrb *lrbp; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6336 | unsigned long flags; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6337 | |
| 6338 | host = cmd->device->host; |
| 6339 | hba = shost_priv(host); |
| 6340 | tag = cmd->request->tag; |
| 6341 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6342 | lrbp = &hba->lrb[tag]; |
| 6343 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp); |
| 6344 | if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6345 | if (!err) |
| 6346 | err = resp; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6347 | goto out; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6348 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6349 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6350 | /* clear the commands that were pending for corresponding LUN */ |
| 6351 | for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) { |
| 6352 | if (hba->lrb[pos].lun == lrbp->lun) { |
| 6353 | err = ufshcd_clear_cmd(hba, pos); |
| 6354 | if (err) |
| 6355 | break; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6356 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6357 | } |
| 6358 | spin_lock_irqsave(host->host_lock, flags); |
| 6359 | ufshcd_transfer_req_compl(hba); |
| 6360 | spin_unlock_irqrestore(host->host_lock, flags); |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 6361 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6362 | out: |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 6363 | hba->req_abort_count = 0; |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 6364 | ufshcd_update_reg_hist(&hba->ufs_stats.dev_reset, (u32)err); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6365 | if (!err) { |
| 6366 | err = SUCCESS; |
| 6367 | } else { |
| 6368 | dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); |
| 6369 | err = FAILED; |
| 6370 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6371 | return err; |
| 6372 | } |
| 6373 | |
Gilad Broner | e0b299e | 2017-02-03 16:56:40 -0800 | [diff] [blame] | 6374 | static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap) |
| 6375 | { |
| 6376 | struct ufshcd_lrb *lrbp; |
| 6377 | int tag; |
| 6378 | |
| 6379 | for_each_set_bit(tag, &bitmap, hba->nutrs) { |
| 6380 | lrbp = &hba->lrb[tag]; |
| 6381 | lrbp->req_abort_skip = true; |
| 6382 | } |
| 6383 | } |
| 6384 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6385 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6386 | * ufshcd_abort - abort a specific command |
| 6387 | * @cmd: SCSI command pointer |
| 6388 | * |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6389 | * Abort the pending command in device by sending UFS_ABORT_TASK task management |
| 6390 | * command, and in host controller by clearing the door-bell register. There can |
| 6391 | * be race between controller sending the command to the device while abort is |
| 6392 | * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is |
| 6393 | * really issued and then try to abort it. |
| 6394 | * |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6395 | * Returns SUCCESS/FAILED |
| 6396 | */ |
| 6397 | static int ufshcd_abort(struct scsi_cmnd *cmd) |
| 6398 | { |
| 6399 | struct Scsi_Host *host; |
| 6400 | struct ufs_hba *hba; |
| 6401 | unsigned long flags; |
| 6402 | unsigned int tag; |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6403 | int err = 0; |
| 6404 | int poll_cnt; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6405 | u8 resp = 0xF; |
| 6406 | struct ufshcd_lrb *lrbp; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 6407 | u32 reg; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6408 | |
| 6409 | host = cmd->device->host; |
| 6410 | hba = shost_priv(host); |
| 6411 | tag = cmd->request->tag; |
Dolev Raviv | e7d3825 | 2016-12-22 18:40:07 -0800 | [diff] [blame] | 6412 | lrbp = &hba->lrb[tag]; |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 6413 | if (!ufshcd_valid_tag(hba, tag)) { |
| 6414 | dev_err(hba->dev, |
| 6415 | "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p", |
| 6416 | __func__, tag, cmd, cmd->request); |
| 6417 | BUG(); |
| 6418 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6419 | |
Dolev Raviv | e7d3825 | 2016-12-22 18:40:07 -0800 | [diff] [blame] | 6420 | /* |
| 6421 | * Task abort to the device W-LUN is illegal. When this command |
| 6422 | * will fail, due to spec violation, scsi err handling next step |
| 6423 | * will be to send LU reset which, again, is a spec violation. |
| 6424 | * To avoid these unnecessary/illegal step we skip to the last error |
| 6425 | * handling stage: reset and restore. |
| 6426 | */ |
| 6427 | if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN) |
| 6428 | return ufshcd_eh_host_reset_handler(cmd); |
| 6429 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6430 | ufshcd_hold(hba, false); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 6431 | reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 6432 | /* If command is already aborted/completed, return SUCCESS */ |
| 6433 | if (!(test_bit(tag, &hba->outstanding_reqs))) { |
| 6434 | dev_err(hba->dev, |
| 6435 | "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n", |
| 6436 | __func__, tag, hba->outstanding_reqs, reg); |
| 6437 | goto out; |
| 6438 | } |
| 6439 | |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 6440 | if (!(reg & (1 << tag))) { |
| 6441 | dev_err(hba->dev, |
| 6442 | "%s: cmd was completed, but without a notifying intr, tag = %d", |
| 6443 | __func__, tag); |
| 6444 | } |
| 6445 | |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 6446 | /* Print Transfer Request of aborted task */ |
| 6447 | dev_err(hba->dev, "%s: Device abort task at tag %d\n", __func__, tag); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 6448 | |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 6449 | /* |
| 6450 | * Print detailed info about aborted request. |
| 6451 | * As more than one request might get aborted at the same time, |
| 6452 | * print full information only for the first aborted request in order |
| 6453 | * to reduce repeated printouts. For other aborted requests only print |
| 6454 | * basic details. |
| 6455 | */ |
| 6456 | scsi_print_command(hba->lrb[tag].cmd); |
| 6457 | if (!hba->req_abort_count) { |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 6458 | ufshcd_update_reg_hist(&hba->ufs_stats.task_abort, 0); |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 6459 | ufshcd_print_host_regs(hba); |
Gilad Broner | 6ba6558 | 2017-02-03 16:57:28 -0800 | [diff] [blame] | 6460 | ufshcd_print_host_state(hba); |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 6461 | ufshcd_print_pwr_info(hba); |
| 6462 | ufshcd_print_trs(hba, 1 << tag, true); |
| 6463 | } else { |
| 6464 | ufshcd_print_trs(hba, 1 << tag, false); |
| 6465 | } |
| 6466 | hba->req_abort_count++; |
Gilad Broner | e0b299e | 2017-02-03 16:56:40 -0800 | [diff] [blame] | 6467 | |
| 6468 | /* Skip task abort in case previous aborts failed and report failure */ |
| 6469 | if (lrbp->req_abort_skip) { |
| 6470 | err = -EIO; |
| 6471 | goto out; |
| 6472 | } |
| 6473 | |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6474 | for (poll_cnt = 100; poll_cnt; poll_cnt--) { |
| 6475 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, |
| 6476 | UFS_QUERY_TASK, &resp); |
| 6477 | if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) { |
| 6478 | /* cmd pending in the device */ |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6479 | dev_err(hba->dev, "%s: cmd pending in the device. tag = %d\n", |
| 6480 | __func__, tag); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6481 | break; |
| 6482 | } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6483 | /* |
| 6484 | * cmd not pending in the device, check if it is |
| 6485 | * in transition. |
| 6486 | */ |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6487 | dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.\n", |
| 6488 | __func__, tag); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6489 | reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 6490 | if (reg & (1 << tag)) { |
| 6491 | /* sleep for max. 200us to stabilize */ |
| 6492 | usleep_range(100, 200); |
| 6493 | continue; |
| 6494 | } |
| 6495 | /* command completed already */ |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6496 | dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.\n", |
| 6497 | __func__, tag); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6498 | goto out; |
| 6499 | } else { |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6500 | dev_err(hba->dev, |
| 6501 | "%s: no response from device. tag = %d, err %d\n", |
| 6502 | __func__, tag, err); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6503 | if (!err) |
| 6504 | err = resp; /* service response error */ |
| 6505 | goto out; |
| 6506 | } |
| 6507 | } |
| 6508 | |
| 6509 | if (!poll_cnt) { |
| 6510 | err = -EBUSY; |
| 6511 | goto out; |
| 6512 | } |
| 6513 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6514 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, |
| 6515 | UFS_ABORT_TASK, &resp); |
| 6516 | if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6517 | if (!err) { |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6518 | err = resp; /* service response error */ |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6519 | dev_err(hba->dev, "%s: issued. tag = %d, err %d\n", |
| 6520 | __func__, tag, err); |
| 6521 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6522 | goto out; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6523 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6524 | |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6525 | err = ufshcd_clear_cmd(hba, tag); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6526 | if (err) { |
| 6527 | dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d\n", |
| 6528 | __func__, tag, err); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6529 | goto out; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6530 | } |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6531 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6532 | scsi_dma_unmap(cmd); |
| 6533 | |
| 6534 | spin_lock_irqsave(host->host_lock, flags); |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 6535 | ufshcd_outstanding_req_clear(hba, tag); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6536 | hba->lrb[tag].cmd = NULL; |
| 6537 | spin_unlock_irqrestore(host->host_lock, flags); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 6538 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6539 | out: |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6540 | if (!err) { |
| 6541 | err = SUCCESS; |
| 6542 | } else { |
| 6543 | dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); |
Gilad Broner | e0b299e | 2017-02-03 16:56:40 -0800 | [diff] [blame] | 6544 | ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6545 | err = FAILED; |
| 6546 | } |
| 6547 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6548 | /* |
| 6549 | * This ufshcd_release() corresponds to the original scsi cmd that got |
| 6550 | * aborted here (as we won't get any IRQ for it). |
| 6551 | */ |
| 6552 | ufshcd_release(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6553 | return err; |
| 6554 | } |
| 6555 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6556 | /** |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6557 | * ufshcd_host_reset_and_restore - reset and restore host controller |
| 6558 | * @hba: per-adapter instance |
| 6559 | * |
| 6560 | * Note that host controller reset may issue DME_RESET to |
| 6561 | * local and remote (device) Uni-Pro stack and the attributes |
| 6562 | * are reset to default state. |
| 6563 | * |
| 6564 | * Returns zero on success, non-zero on failure |
| 6565 | */ |
| 6566 | static int ufshcd_host_reset_and_restore(struct ufs_hba *hba) |
| 6567 | { |
| 6568 | int err; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6569 | unsigned long flags; |
| 6570 | |
Can Guo | 2df74b6 | 2019-11-25 22:53:33 -0800 | [diff] [blame] | 6571 | /* |
| 6572 | * Stop the host controller and complete the requests |
| 6573 | * cleared by h/w |
| 6574 | */ |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 6575 | ufshcd_hba_stop(hba); |
| 6576 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6577 | spin_lock_irqsave(hba->host->host_lock, flags); |
Can Guo | 2df74b6 | 2019-11-25 22:53:33 -0800 | [diff] [blame] | 6578 | hba->silence_err_logs = true; |
| 6579 | ufshcd_complete_requests(hba); |
| 6580 | hba->silence_err_logs = false; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6581 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6582 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 6583 | /* scale up clocks to max frequency before full reinitialization */ |
Subhash Jadavani | 394b949 | 2020-03-26 02:25:40 -0700 | [diff] [blame] | 6584 | ufshcd_set_clk_freq(hba, true); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 6585 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6586 | err = ufshcd_hba_enable(hba); |
| 6587 | if (err) |
| 6588 | goto out; |
| 6589 | |
| 6590 | /* Establish the link again and restore the device */ |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 6591 | err = ufshcd_probe_hba(hba, false); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6592 | |
| 6593 | if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL)) |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6594 | err = -EIO; |
| 6595 | out: |
| 6596 | if (err) |
| 6597 | dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err); |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 6598 | ufshcd_update_reg_hist(&hba->ufs_stats.host_reset, (u32)err); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6599 | return err; |
| 6600 | } |
| 6601 | |
| 6602 | /** |
| 6603 | * ufshcd_reset_and_restore - reset and re-initialize host/device |
| 6604 | * @hba: per-adapter instance |
| 6605 | * |
| 6606 | * Reset and recover device, host and re-establish link. This |
| 6607 | * is helpful to recover the communication in fatal error conditions. |
| 6608 | * |
| 6609 | * Returns zero on success, non-zero on failure |
| 6610 | */ |
| 6611 | static int ufshcd_reset_and_restore(struct ufs_hba *hba) |
| 6612 | { |
| 6613 | int err = 0; |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6614 | int retries = MAX_HOST_RESET_RETRIES; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6615 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6616 | do { |
Bjorn Andersson | d8d9f79 | 2019-08-28 12:17:54 -0700 | [diff] [blame] | 6617 | /* Reset the attached device */ |
| 6618 | ufshcd_vops_device_reset(hba); |
| 6619 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6620 | err = ufshcd_host_reset_and_restore(hba); |
| 6621 | } while (err && --retries); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6622 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6623 | return err; |
| 6624 | } |
| 6625 | |
| 6626 | /** |
| 6627 | * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 6628 | * @cmd: SCSI command pointer |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6629 | * |
| 6630 | * Returns SUCCESS/FAILED |
| 6631 | */ |
| 6632 | static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd) |
| 6633 | { |
| 6634 | int err; |
| 6635 | unsigned long flags; |
| 6636 | struct ufs_hba *hba; |
| 6637 | |
| 6638 | hba = shost_priv(cmd->device->host); |
| 6639 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6640 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6641 | /* |
| 6642 | * Check if there is any race with fatal error handling. |
| 6643 | * If so, wait for it to complete. Even though fatal error |
| 6644 | * handling does reset and restore in some cases, don't assume |
| 6645 | * anything out of it. We are just avoiding race here. |
| 6646 | */ |
| 6647 | do { |
| 6648 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6649 | if (!(work_pending(&hba->eh_work) || |
Zang Leigang | 8dc0da7 | 2017-06-24 19:14:32 +0800 | [diff] [blame] | 6650 | hba->ufshcd_state == UFSHCD_STATE_RESET || |
| 6651 | hba->ufshcd_state == UFSHCD_STATE_EH_SCHEDULED)) |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6652 | break; |
| 6653 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6654 | dev_dbg(hba->dev, "%s: reset in progress\n", __func__); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6655 | flush_work(&hba->eh_work); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6656 | } while (1); |
| 6657 | |
| 6658 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 6659 | ufshcd_set_eh_in_progress(hba); |
| 6660 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6661 | |
| 6662 | err = ufshcd_reset_and_restore(hba); |
| 6663 | |
| 6664 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 6665 | if (!err) { |
| 6666 | err = SUCCESS; |
| 6667 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
| 6668 | } else { |
| 6669 | err = FAILED; |
| 6670 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 6671 | } |
| 6672 | ufshcd_clear_eh_in_progress(hba); |
| 6673 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6674 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6675 | ufshcd_release(hba); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6676 | return err; |
| 6677 | } |
| 6678 | |
| 6679 | /** |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6680 | * ufshcd_get_max_icc_level - calculate the ICC level |
| 6681 | * @sup_curr_uA: max. current supported by the regulator |
| 6682 | * @start_scan: row at the desc table to start scan from |
| 6683 | * @buff: power descriptor buffer |
| 6684 | * |
| 6685 | * Returns calculated max ICC level for specific regulator |
| 6686 | */ |
| 6687 | static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff) |
| 6688 | { |
| 6689 | int i; |
| 6690 | int curr_uA; |
| 6691 | u16 data; |
| 6692 | u16 unit; |
| 6693 | |
| 6694 | for (i = start_scan; i >= 0; i--) { |
Tomas Winkler | d79713f | 2017-01-05 10:45:11 +0200 | [diff] [blame] | 6695 | data = be16_to_cpup((__be16 *)&buff[2 * i]); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6696 | unit = (data & ATTR_ICC_LVL_UNIT_MASK) >> |
| 6697 | ATTR_ICC_LVL_UNIT_OFFSET; |
| 6698 | curr_uA = data & ATTR_ICC_LVL_VALUE_MASK; |
| 6699 | switch (unit) { |
| 6700 | case UFSHCD_NANO_AMP: |
| 6701 | curr_uA = curr_uA / 1000; |
| 6702 | break; |
| 6703 | case UFSHCD_MILI_AMP: |
| 6704 | curr_uA = curr_uA * 1000; |
| 6705 | break; |
| 6706 | case UFSHCD_AMP: |
| 6707 | curr_uA = curr_uA * 1000 * 1000; |
| 6708 | break; |
| 6709 | case UFSHCD_MICRO_AMP: |
| 6710 | default: |
| 6711 | break; |
| 6712 | } |
| 6713 | if (sup_curr_uA >= curr_uA) |
| 6714 | break; |
| 6715 | } |
| 6716 | if (i < 0) { |
| 6717 | i = 0; |
| 6718 | pr_err("%s: Couldn't find valid icc_level = %d", __func__, i); |
| 6719 | } |
| 6720 | |
| 6721 | return (u32)i; |
| 6722 | } |
| 6723 | |
| 6724 | /** |
| 6725 | * ufshcd_calc_icc_level - calculate the max ICC level |
| 6726 | * In case regulators are not initialized we'll return 0 |
| 6727 | * @hba: per-adapter instance |
| 6728 | * @desc_buf: power descriptor buffer to extract ICC levels from. |
| 6729 | * @len: length of desc_buff |
| 6730 | * |
| 6731 | * Returns calculated ICC level |
| 6732 | */ |
| 6733 | static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba, |
| 6734 | u8 *desc_buf, int len) |
| 6735 | { |
| 6736 | u32 icc_level = 0; |
| 6737 | |
| 6738 | if (!hba->vreg_info.vcc || !hba->vreg_info.vccq || |
| 6739 | !hba->vreg_info.vccq2) { |
| 6740 | dev_err(hba->dev, |
| 6741 | "%s: Regulator capability was not set, actvIccLevel=%d", |
| 6742 | __func__, icc_level); |
| 6743 | goto out; |
| 6744 | } |
| 6745 | |
Stanley Chu | 0487fff | 2019-03-28 17:16:25 +0800 | [diff] [blame] | 6746 | if (hba->vreg_info.vcc && hba->vreg_info.vcc->max_uA) |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6747 | icc_level = ufshcd_get_max_icc_level( |
| 6748 | hba->vreg_info.vcc->max_uA, |
| 6749 | POWER_DESC_MAX_ACTV_ICC_LVLS - 1, |
| 6750 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]); |
| 6751 | |
Stanley Chu | 0487fff | 2019-03-28 17:16:25 +0800 | [diff] [blame] | 6752 | if (hba->vreg_info.vccq && hba->vreg_info.vccq->max_uA) |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6753 | icc_level = ufshcd_get_max_icc_level( |
| 6754 | hba->vreg_info.vccq->max_uA, |
| 6755 | icc_level, |
| 6756 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]); |
| 6757 | |
Stanley Chu | 0487fff | 2019-03-28 17:16:25 +0800 | [diff] [blame] | 6758 | if (hba->vreg_info.vccq2 && hba->vreg_info.vccq2->max_uA) |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6759 | icc_level = ufshcd_get_max_icc_level( |
| 6760 | hba->vreg_info.vccq2->max_uA, |
| 6761 | icc_level, |
| 6762 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]); |
| 6763 | out: |
| 6764 | return icc_level; |
| 6765 | } |
| 6766 | |
Can Guo | e89860f | 2020-03-26 02:25:41 -0700 | [diff] [blame] | 6767 | static void ufshcd_set_active_icc_lvl(struct ufs_hba *hba) |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6768 | { |
| 6769 | int ret; |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 6770 | int buff_len = hba->desc_size[QUERY_DESC_IDN_POWER]; |
Kees Cook | bbe21d7 | 2018-05-02 16:58:09 -0700 | [diff] [blame] | 6771 | u8 *desc_buf; |
Can Guo | e89860f | 2020-03-26 02:25:41 -0700 | [diff] [blame] | 6772 | u32 icc_level; |
Kees Cook | bbe21d7 | 2018-05-02 16:58:09 -0700 | [diff] [blame] | 6773 | |
| 6774 | desc_buf = kmalloc(buff_len, GFP_KERNEL); |
| 6775 | if (!desc_buf) |
| 6776 | return; |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6777 | |
Bean Huo | c4607a0 | 2020-06-03 11:19:56 +0200 | [diff] [blame] | 6778 | ret = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_POWER, 0, 0, |
| 6779 | desc_buf, buff_len); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6780 | if (ret) { |
| 6781 | dev_err(hba->dev, |
| 6782 | "%s: Failed reading power descriptor.len = %d ret = %d", |
| 6783 | __func__, buff_len, ret); |
Kees Cook | bbe21d7 | 2018-05-02 16:58:09 -0700 | [diff] [blame] | 6784 | goto out; |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6785 | } |
| 6786 | |
Can Guo | e89860f | 2020-03-26 02:25:41 -0700 | [diff] [blame] | 6787 | icc_level = ufshcd_find_max_sup_active_icc_level(hba, desc_buf, |
| 6788 | buff_len); |
| 6789 | dev_dbg(hba->dev, "%s: setting icc_level 0x%x", __func__, icc_level); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6790 | |
Szymon Mielczarek | dbd34a6 | 2017-03-29 08:19:21 +0200 | [diff] [blame] | 6791 | ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
Can Guo | e89860f | 2020-03-26 02:25:41 -0700 | [diff] [blame] | 6792 | QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, &icc_level); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6793 | |
| 6794 | if (ret) |
| 6795 | dev_err(hba->dev, |
| 6796 | "%s: Failed configuring bActiveICCLevel = %d ret = %d", |
Can Guo | e89860f | 2020-03-26 02:25:41 -0700 | [diff] [blame] | 6797 | __func__, icc_level, ret); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6798 | |
Kees Cook | bbe21d7 | 2018-05-02 16:58:09 -0700 | [diff] [blame] | 6799 | out: |
| 6800 | kfree(desc_buf); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6801 | } |
| 6802 | |
Can Guo | fb276f7 | 2020-03-25 18:09:59 -0700 | [diff] [blame] | 6803 | static inline void ufshcd_blk_pm_runtime_init(struct scsi_device *sdev) |
| 6804 | { |
| 6805 | scsi_autopm_get_device(sdev); |
| 6806 | blk_pm_runtime_init(sdev->request_queue, &sdev->sdev_gendev); |
| 6807 | if (sdev->rpm_autosuspend) |
| 6808 | pm_runtime_set_autosuspend_delay(&sdev->sdev_gendev, |
| 6809 | RPM_AUTOSUSPEND_DELAY_MS); |
| 6810 | scsi_autopm_put_device(sdev); |
| 6811 | } |
| 6812 | |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6813 | /** |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6814 | * ufshcd_scsi_add_wlus - Adds required W-LUs |
| 6815 | * @hba: per-adapter instance |
| 6816 | * |
| 6817 | * UFS device specification requires the UFS devices to support 4 well known |
| 6818 | * logical units: |
| 6819 | * "REPORT_LUNS" (address: 01h) |
| 6820 | * "UFS Device" (address: 50h) |
| 6821 | * "RPMB" (address: 44h) |
| 6822 | * "BOOT" (address: 30h) |
| 6823 | * UFS device's power management needs to be controlled by "POWER CONDITION" |
| 6824 | * field of SSU (START STOP UNIT) command. But this "power condition" field |
| 6825 | * will take effect only when its sent to "UFS device" well known logical unit |
| 6826 | * hence we require the scsi_device instance to represent this logical unit in |
| 6827 | * order for the UFS host driver to send the SSU command for power management. |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 6828 | * |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6829 | * We also require the scsi_device instance for "RPMB" (Replay Protected Memory |
| 6830 | * Block) LU so user space process can control this LU. User space may also |
| 6831 | * want to have access to BOOT LU. |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 6832 | * |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6833 | * This function adds scsi device instances for each of all well known LUs |
| 6834 | * (except "REPORT LUNS" LU). |
| 6835 | * |
| 6836 | * Returns zero on success (all required W-LUs are added successfully), |
| 6837 | * non-zero error value on failure (if failed to add any of the required W-LU). |
| 6838 | */ |
| 6839 | static int ufshcd_scsi_add_wlus(struct ufs_hba *hba) |
| 6840 | { |
| 6841 | int ret = 0; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6842 | struct scsi_device *sdev_rpmb; |
| 6843 | struct scsi_device *sdev_boot; |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6844 | |
| 6845 | hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0, |
| 6846 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL); |
| 6847 | if (IS_ERR(hba->sdev_ufs_device)) { |
| 6848 | ret = PTR_ERR(hba->sdev_ufs_device); |
| 6849 | hba->sdev_ufs_device = NULL; |
| 6850 | goto out; |
| 6851 | } |
Can Guo | fb276f7 | 2020-03-25 18:09:59 -0700 | [diff] [blame] | 6852 | ufshcd_blk_pm_runtime_init(hba->sdev_ufs_device); |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6853 | scsi_device_put(hba->sdev_ufs_device); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6854 | |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6855 | sdev_rpmb = __scsi_add_device(hba->host, 0, 0, |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6856 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL); |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6857 | if (IS_ERR(sdev_rpmb)) { |
| 6858 | ret = PTR_ERR(sdev_rpmb); |
Huanlin Ke | 3d21fbd | 2017-09-22 18:31:47 +0800 | [diff] [blame] | 6859 | goto remove_sdev_ufs_device; |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6860 | } |
Can Guo | fb276f7 | 2020-03-25 18:09:59 -0700 | [diff] [blame] | 6861 | ufshcd_blk_pm_runtime_init(sdev_rpmb); |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6862 | scsi_device_put(sdev_rpmb); |
Huanlin Ke | 3d21fbd | 2017-09-22 18:31:47 +0800 | [diff] [blame] | 6863 | |
| 6864 | sdev_boot = __scsi_add_device(hba->host, 0, 0, |
| 6865 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL); |
Can Guo | fb276f7 | 2020-03-25 18:09:59 -0700 | [diff] [blame] | 6866 | if (IS_ERR(sdev_boot)) { |
Huanlin Ke | 3d21fbd | 2017-09-22 18:31:47 +0800 | [diff] [blame] | 6867 | dev_err(hba->dev, "%s: BOOT WLUN not found\n", __func__); |
Can Guo | fb276f7 | 2020-03-25 18:09:59 -0700 | [diff] [blame] | 6868 | } else { |
| 6869 | ufshcd_blk_pm_runtime_init(sdev_boot); |
Huanlin Ke | 3d21fbd | 2017-09-22 18:31:47 +0800 | [diff] [blame] | 6870 | scsi_device_put(sdev_boot); |
Can Guo | fb276f7 | 2020-03-25 18:09:59 -0700 | [diff] [blame] | 6871 | } |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6872 | goto out; |
| 6873 | |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6874 | remove_sdev_ufs_device: |
| 6875 | scsi_remove_device(hba->sdev_ufs_device); |
| 6876 | out: |
| 6877 | return ret; |
| 6878 | } |
| 6879 | |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 6880 | static void ufshcd_wb_probe(struct ufs_hba *hba, u8 *desc_buf) |
| 6881 | { |
Stanley Chu | a7f1e69 | 2020-06-25 11:04:30 +0800 | [diff] [blame] | 6882 | struct ufs_dev_info *dev_info = &hba->dev_info; |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 6883 | u8 lun; |
| 6884 | u32 d_lu_wb_buf_alloc; |
| 6885 | |
Stanley Chu | 817d7e1 | 2020-05-08 16:01:08 +0800 | [diff] [blame] | 6886 | if (!ufshcd_is_wb_allowed(hba)) |
| 6887 | return; |
Stanley Chu | a7f1e69 | 2020-06-25 11:04:30 +0800 | [diff] [blame] | 6888 | /* |
| 6889 | * Probe WB only for UFS-2.2 and UFS-3.1 (and later) devices or |
| 6890 | * UFS devices with quirk UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES |
| 6891 | * enabled |
| 6892 | */ |
| 6893 | if (!(dev_info->wspecversion >= 0x310 || |
| 6894 | dev_info->wspecversion == 0x220 || |
| 6895 | (hba->dev_quirks & UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES))) |
| 6896 | goto wb_disabled; |
Stanley Chu | 817d7e1 | 2020-05-08 16:01:08 +0800 | [diff] [blame] | 6897 | |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 6898 | if (hba->desc_size[QUERY_DESC_IDN_DEVICE] < |
| 6899 | DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP + 4) |
Stanley Chu | 817d7e1 | 2020-05-08 16:01:08 +0800 | [diff] [blame] | 6900 | goto wb_disabled; |
| 6901 | |
Stanley Chu | a7f1e69 | 2020-06-25 11:04:30 +0800 | [diff] [blame] | 6902 | dev_info->d_ext_ufs_feature_sup = |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 6903 | get_unaligned_be32(desc_buf + |
| 6904 | DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP); |
Stanley Chu | 817d7e1 | 2020-05-08 16:01:08 +0800 | [diff] [blame] | 6905 | |
Stanley Chu | a7f1e69 | 2020-06-25 11:04:30 +0800 | [diff] [blame] | 6906 | if (!(dev_info->d_ext_ufs_feature_sup & UFS_DEV_WRITE_BOOSTER_SUP)) |
Stanley Chu | 817d7e1 | 2020-05-08 16:01:08 +0800 | [diff] [blame] | 6907 | goto wb_disabled; |
| 6908 | |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 6909 | /* |
| 6910 | * WB may be supported but not configured while provisioning. |
| 6911 | * The spec says, in dedicated wb buffer mode, |
| 6912 | * a max of 1 lun would have wb buffer configured. |
| 6913 | * Now only shared buffer mode is supported. |
| 6914 | */ |
Stanley Chu | a7f1e69 | 2020-06-25 11:04:30 +0800 | [diff] [blame] | 6915 | dev_info->b_wb_buffer_type = |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 6916 | desc_buf[DEVICE_DESC_PARAM_WB_TYPE]; |
| 6917 | |
Stanley Chu | a7f1e69 | 2020-06-25 11:04:30 +0800 | [diff] [blame] | 6918 | dev_info->b_presrv_uspc_en = |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 6919 | desc_buf[DEVICE_DESC_PARAM_WB_PRESRV_USRSPC_EN]; |
| 6920 | |
Stanley Chu | a7f1e69 | 2020-06-25 11:04:30 +0800 | [diff] [blame] | 6921 | if (dev_info->b_wb_buffer_type == WB_BUF_MODE_SHARED) { |
| 6922 | dev_info->d_wb_alloc_units = |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 6923 | get_unaligned_be32(desc_buf + |
| 6924 | DEVICE_DESC_PARAM_WB_SHARED_ALLOC_UNITS); |
Stanley Chu | a7f1e69 | 2020-06-25 11:04:30 +0800 | [diff] [blame] | 6925 | if (!dev_info->d_wb_alloc_units) |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 6926 | goto wb_disabled; |
| 6927 | } else { |
| 6928 | for (lun = 0; lun < UFS_UPIU_MAX_WB_LUN_ID; lun++) { |
| 6929 | d_lu_wb_buf_alloc = 0; |
| 6930 | ufshcd_read_unit_desc_param(hba, |
| 6931 | lun, |
| 6932 | UNIT_DESC_PARAM_WB_BUF_ALLOC_UNITS, |
| 6933 | (u8 *)&d_lu_wb_buf_alloc, |
| 6934 | sizeof(d_lu_wb_buf_alloc)); |
| 6935 | if (d_lu_wb_buf_alloc) { |
Stanley Chu | a7f1e69 | 2020-06-25 11:04:30 +0800 | [diff] [blame] | 6936 | dev_info->wb_dedicated_lu = lun; |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 6937 | break; |
| 6938 | } |
| 6939 | } |
Stanley Chu | 817d7e1 | 2020-05-08 16:01:08 +0800 | [diff] [blame] | 6940 | |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 6941 | if (!d_lu_wb_buf_alloc) |
| 6942 | goto wb_disabled; |
| 6943 | } |
Stanley Chu | 817d7e1 | 2020-05-08 16:01:08 +0800 | [diff] [blame] | 6944 | return; |
| 6945 | |
| 6946 | wb_disabled: |
| 6947 | hba->caps &= ~UFSHCD_CAP_WB_EN; |
| 6948 | } |
| 6949 | |
Stanley Chu | 8db269a | 2020-05-08 16:01:10 +0800 | [diff] [blame] | 6950 | void ufshcd_fixup_dev_quirks(struct ufs_hba *hba, struct ufs_dev_fix *fixups) |
Stanley Chu | 817d7e1 | 2020-05-08 16:01:08 +0800 | [diff] [blame] | 6951 | { |
| 6952 | struct ufs_dev_fix *f; |
| 6953 | struct ufs_dev_info *dev_info = &hba->dev_info; |
| 6954 | |
Stanley Chu | 8db269a | 2020-05-08 16:01:10 +0800 | [diff] [blame] | 6955 | if (!fixups) |
| 6956 | return; |
| 6957 | |
| 6958 | for (f = fixups; f->quirk; f++) { |
Stanley Chu | 817d7e1 | 2020-05-08 16:01:08 +0800 | [diff] [blame] | 6959 | if ((f->wmanufacturerid == dev_info->wmanufacturerid || |
| 6960 | f->wmanufacturerid == UFS_ANY_VENDOR) && |
| 6961 | ((dev_info->model && |
| 6962 | STR_PRFX_EQUAL(f->model, dev_info->model)) || |
| 6963 | !strcmp(f->model, UFS_ANY_MODEL))) |
| 6964 | hba->dev_quirks |= f->quirk; |
| 6965 | } |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 6966 | } |
Stanley Chu | 8db269a | 2020-05-08 16:01:10 +0800 | [diff] [blame] | 6967 | EXPORT_SYMBOL_GPL(ufshcd_fixup_dev_quirks); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 6968 | |
Stanley Chu | c28c00b | 2020-05-08 16:01:09 +0800 | [diff] [blame] | 6969 | static void ufs_fixup_device_setup(struct ufs_hba *hba) |
| 6970 | { |
| 6971 | /* fix by general quirk table */ |
Stanley Chu | 8db269a | 2020-05-08 16:01:10 +0800 | [diff] [blame] | 6972 | ufshcd_fixup_dev_quirks(hba, ufs_fixups); |
Stanley Chu | c28c00b | 2020-05-08 16:01:09 +0800 | [diff] [blame] | 6973 | |
| 6974 | /* allow vendors to fix quirks */ |
| 6975 | ufshcd_vops_fixup_dev_quirks(hba); |
| 6976 | } |
| 6977 | |
Bean Huo | 0975006 | 2020-01-20 14:08:14 +0100 | [diff] [blame] | 6978 | static int ufs_get_device_desc(struct ufs_hba *hba) |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6979 | { |
| 6980 | int err; |
| 6981 | u8 model_index; |
Kees Cook | bbe21d7 | 2018-05-02 16:58:09 -0700 | [diff] [blame] | 6982 | u8 *desc_buf; |
Bean Huo | 0975006 | 2020-01-20 14:08:14 +0100 | [diff] [blame] | 6983 | struct ufs_dev_info *dev_info = &hba->dev_info; |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 6984 | |
Bean Huo | 458a45f | 2020-06-03 11:19:55 +0200 | [diff] [blame] | 6985 | desc_buf = kmalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL); |
Kees Cook | bbe21d7 | 2018-05-02 16:58:09 -0700 | [diff] [blame] | 6986 | if (!desc_buf) { |
| 6987 | err = -ENOMEM; |
| 6988 | goto out; |
| 6989 | } |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6990 | |
Bean Huo | c4607a0 | 2020-06-03 11:19:56 +0200 | [diff] [blame] | 6991 | err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_DEVICE, 0, 0, desc_buf, |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 6992 | hba->desc_size[QUERY_DESC_IDN_DEVICE]); |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6993 | if (err) { |
| 6994 | dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n", |
| 6995 | __func__, err); |
| 6996 | goto out; |
| 6997 | } |
| 6998 | |
| 6999 | /* |
| 7000 | * getting vendor (manufacturerID) and Bank Index in big endian |
| 7001 | * format |
| 7002 | */ |
Bean Huo | 0975006 | 2020-01-20 14:08:14 +0100 | [diff] [blame] | 7003 | dev_info->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 | |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 7004 | desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1]; |
| 7005 | |
Can Guo | 09f1779 | 2020-02-10 19:40:49 -0800 | [diff] [blame] | 7006 | /* getting Specification Version in big endian format */ |
| 7007 | dev_info->wspecversion = desc_buf[DEVICE_DESC_PARAM_SPEC_VER] << 8 | |
| 7008 | desc_buf[DEVICE_DESC_PARAM_SPEC_VER + 1]; |
| 7009 | |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 7010 | model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME]; |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 7011 | |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 7012 | err = ufshcd_read_string_desc(hba, model_index, |
Bean Huo | 0975006 | 2020-01-20 14:08:14 +0100 | [diff] [blame] | 7013 | &dev_info->model, SD_ASCII_STD); |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 7014 | if (err < 0) { |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 7015 | dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n", |
| 7016 | __func__, err); |
| 7017 | goto out; |
| 7018 | } |
| 7019 | |
Stanley Chu | 817d7e1 | 2020-05-08 16:01:08 +0800 | [diff] [blame] | 7020 | ufs_fixup_device_setup(hba); |
| 7021 | |
Stanley Chu | a7f1e69 | 2020-06-25 11:04:30 +0800 | [diff] [blame] | 7022 | ufshcd_wb_probe(hba, desc_buf); |
Stanley Chu | 817d7e1 | 2020-05-08 16:01:08 +0800 | [diff] [blame] | 7023 | |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 7024 | /* |
| 7025 | * ufshcd_read_string_desc returns size of the string |
| 7026 | * reset the error value |
| 7027 | */ |
| 7028 | err = 0; |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 7029 | |
| 7030 | out: |
Kees Cook | bbe21d7 | 2018-05-02 16:58:09 -0700 | [diff] [blame] | 7031 | kfree(desc_buf); |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 7032 | return err; |
| 7033 | } |
| 7034 | |
Bean Huo | 0975006 | 2020-01-20 14:08:14 +0100 | [diff] [blame] | 7035 | static void ufs_put_device_desc(struct ufs_hba *hba) |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 7036 | { |
Bean Huo | 0975006 | 2020-01-20 14:08:14 +0100 | [diff] [blame] | 7037 | struct ufs_dev_info *dev_info = &hba->dev_info; |
| 7038 | |
| 7039 | kfree(dev_info->model); |
| 7040 | dev_info->model = NULL; |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 7041 | } |
| 7042 | |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 7043 | /** |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 7044 | * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro |
| 7045 | * @hba: per-adapter instance |
| 7046 | * |
| 7047 | * PA_TActivate parameter can be tuned manually if UniPro version is less than |
| 7048 | * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's |
| 7049 | * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce |
| 7050 | * the hibern8 exit latency. |
| 7051 | * |
| 7052 | * Returns zero on success, non-zero error value on failure. |
| 7053 | */ |
| 7054 | static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba) |
| 7055 | { |
| 7056 | int ret = 0; |
| 7057 | u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate; |
| 7058 | |
| 7059 | ret = ufshcd_dme_peer_get(hba, |
| 7060 | UIC_ARG_MIB_SEL( |
| 7061 | RX_MIN_ACTIVATETIME_CAPABILITY, |
| 7062 | UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), |
| 7063 | &peer_rx_min_activatetime); |
| 7064 | if (ret) |
| 7065 | goto out; |
| 7066 | |
| 7067 | /* make sure proper unit conversion is applied */ |
| 7068 | tuned_pa_tactivate = |
| 7069 | ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US) |
| 7070 | / PA_TACTIVATE_TIME_UNIT_US); |
| 7071 | ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 7072 | tuned_pa_tactivate); |
| 7073 | |
| 7074 | out: |
| 7075 | return ret; |
| 7076 | } |
| 7077 | |
| 7078 | /** |
| 7079 | * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro |
| 7080 | * @hba: per-adapter instance |
| 7081 | * |
| 7082 | * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than |
| 7083 | * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's |
| 7084 | * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY. |
| 7085 | * This optimal value can help reduce the hibern8 exit latency. |
| 7086 | * |
| 7087 | * Returns zero on success, non-zero error value on failure. |
| 7088 | */ |
| 7089 | static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba) |
| 7090 | { |
| 7091 | int ret = 0; |
| 7092 | u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0; |
| 7093 | u32 max_hibern8_time, tuned_pa_hibern8time; |
| 7094 | |
| 7095 | ret = ufshcd_dme_get(hba, |
| 7096 | UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY, |
| 7097 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), |
| 7098 | &local_tx_hibern8_time_cap); |
| 7099 | if (ret) |
| 7100 | goto out; |
| 7101 | |
| 7102 | ret = ufshcd_dme_peer_get(hba, |
| 7103 | UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY, |
| 7104 | UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), |
| 7105 | &peer_rx_hibern8_time_cap); |
| 7106 | if (ret) |
| 7107 | goto out; |
| 7108 | |
| 7109 | max_hibern8_time = max(local_tx_hibern8_time_cap, |
| 7110 | peer_rx_hibern8_time_cap); |
| 7111 | /* make sure proper unit conversion is applied */ |
| 7112 | tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US) |
| 7113 | / PA_HIBERN8_TIME_UNIT_US); |
| 7114 | ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME), |
| 7115 | tuned_pa_hibern8time); |
| 7116 | out: |
| 7117 | return ret; |
| 7118 | } |
| 7119 | |
subhashj@codeaurora.org | c6a6db4 | 2016-11-23 16:32:08 -0800 | [diff] [blame] | 7120 | /** |
| 7121 | * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is |
| 7122 | * less than device PA_TACTIVATE time. |
| 7123 | * @hba: per-adapter instance |
| 7124 | * |
| 7125 | * Some UFS devices require host PA_TACTIVATE to be lower than device |
| 7126 | * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk |
| 7127 | * for such devices. |
| 7128 | * |
| 7129 | * Returns zero on success, non-zero error value on failure. |
| 7130 | */ |
| 7131 | static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba) |
| 7132 | { |
| 7133 | int ret = 0; |
| 7134 | u32 granularity, peer_granularity; |
| 7135 | u32 pa_tactivate, peer_pa_tactivate; |
| 7136 | u32 pa_tactivate_us, peer_pa_tactivate_us; |
| 7137 | u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100}; |
| 7138 | |
| 7139 | ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY), |
| 7140 | &granularity); |
| 7141 | if (ret) |
| 7142 | goto out; |
| 7143 | |
| 7144 | ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY), |
| 7145 | &peer_granularity); |
| 7146 | if (ret) |
| 7147 | goto out; |
| 7148 | |
| 7149 | if ((granularity < PA_GRANULARITY_MIN_VAL) || |
| 7150 | (granularity > PA_GRANULARITY_MAX_VAL)) { |
| 7151 | dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d", |
| 7152 | __func__, granularity); |
| 7153 | return -EINVAL; |
| 7154 | } |
| 7155 | |
| 7156 | if ((peer_granularity < PA_GRANULARITY_MIN_VAL) || |
| 7157 | (peer_granularity > PA_GRANULARITY_MAX_VAL)) { |
| 7158 | dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d", |
| 7159 | __func__, peer_granularity); |
| 7160 | return -EINVAL; |
| 7161 | } |
| 7162 | |
| 7163 | ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate); |
| 7164 | if (ret) |
| 7165 | goto out; |
| 7166 | |
| 7167 | ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 7168 | &peer_pa_tactivate); |
| 7169 | if (ret) |
| 7170 | goto out; |
| 7171 | |
| 7172 | pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1]; |
| 7173 | peer_pa_tactivate_us = peer_pa_tactivate * |
| 7174 | gran_to_us_table[peer_granularity - 1]; |
| 7175 | |
| 7176 | if (pa_tactivate_us > peer_pa_tactivate_us) { |
| 7177 | u32 new_peer_pa_tactivate; |
| 7178 | |
| 7179 | new_peer_pa_tactivate = pa_tactivate_us / |
| 7180 | gran_to_us_table[peer_granularity - 1]; |
| 7181 | new_peer_pa_tactivate++; |
| 7182 | ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 7183 | new_peer_pa_tactivate); |
| 7184 | } |
| 7185 | |
| 7186 | out: |
| 7187 | return ret; |
| 7188 | } |
| 7189 | |
Bean Huo | 0975006 | 2020-01-20 14:08:14 +0100 | [diff] [blame] | 7190 | static void ufshcd_tune_unipro_params(struct ufs_hba *hba) |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 7191 | { |
| 7192 | if (ufshcd_is_unipro_pa_params_tuning_req(hba)) { |
| 7193 | ufshcd_tune_pa_tactivate(hba); |
| 7194 | ufshcd_tune_pa_hibern8time(hba); |
| 7195 | } |
| 7196 | |
Can Guo | e91ed9e | 2020-02-23 20:09:21 -0800 | [diff] [blame] | 7197 | ufshcd_vops_apply_dev_quirks(hba); |
| 7198 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 7199 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE) |
| 7200 | /* set 1ms timeout for PA_TACTIVATE */ |
| 7201 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10); |
subhashj@codeaurora.org | c6a6db4 | 2016-11-23 16:32:08 -0800 | [diff] [blame] | 7202 | |
| 7203 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE) |
| 7204 | ufshcd_quirk_tune_host_pa_tactivate(hba); |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 7205 | } |
| 7206 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 7207 | static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba) |
| 7208 | { |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 7209 | hba->ufs_stats.hibern8_exit_cnt = 0; |
| 7210 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 7211 | hba->req_abort_count = 0; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 7212 | } |
| 7213 | |
Bean Huo | 731f062 | 2020-01-20 14:08:19 +0100 | [diff] [blame] | 7214 | static int ufshcd_device_geo_params_init(struct ufs_hba *hba) |
| 7215 | { |
| 7216 | int err; |
| 7217 | size_t buff_len; |
| 7218 | u8 *desc_buf; |
| 7219 | |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 7220 | buff_len = hba->desc_size[QUERY_DESC_IDN_GEOMETRY]; |
Bean Huo | 731f062 | 2020-01-20 14:08:19 +0100 | [diff] [blame] | 7221 | desc_buf = kmalloc(buff_len, GFP_KERNEL); |
| 7222 | if (!desc_buf) { |
| 7223 | err = -ENOMEM; |
| 7224 | goto out; |
| 7225 | } |
| 7226 | |
Bean Huo | c4607a0 | 2020-06-03 11:19:56 +0200 | [diff] [blame] | 7227 | err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_GEOMETRY, 0, 0, |
| 7228 | desc_buf, buff_len); |
Bean Huo | 731f062 | 2020-01-20 14:08:19 +0100 | [diff] [blame] | 7229 | if (err) { |
| 7230 | dev_err(hba->dev, "%s: Failed reading Geometry Desc. err = %d\n", |
| 7231 | __func__, err); |
| 7232 | goto out; |
| 7233 | } |
| 7234 | |
| 7235 | if (desc_buf[GEOMETRY_DESC_PARAM_MAX_NUM_LUN] == 1) |
| 7236 | hba->dev_info.max_lu_supported = 32; |
| 7237 | else if (desc_buf[GEOMETRY_DESC_PARAM_MAX_NUM_LUN] == 0) |
| 7238 | hba->dev_info.max_lu_supported = 8; |
| 7239 | |
| 7240 | out: |
| 7241 | kfree(desc_buf); |
| 7242 | return err; |
| 7243 | } |
| 7244 | |
Subhash Jadavani | 9e1e8a7 | 2018-10-16 14:29:41 +0530 | [diff] [blame] | 7245 | static struct ufs_ref_clk ufs_ref_clk_freqs[] = { |
| 7246 | {19200000, REF_CLK_FREQ_19_2_MHZ}, |
| 7247 | {26000000, REF_CLK_FREQ_26_MHZ}, |
| 7248 | {38400000, REF_CLK_FREQ_38_4_MHZ}, |
| 7249 | {52000000, REF_CLK_FREQ_52_MHZ}, |
| 7250 | {0, REF_CLK_FREQ_INVAL}, |
| 7251 | }; |
| 7252 | |
| 7253 | static enum ufs_ref_clk_freq |
| 7254 | ufs_get_bref_clk_from_hz(unsigned long freq) |
| 7255 | { |
| 7256 | int i; |
| 7257 | |
| 7258 | for (i = 0; ufs_ref_clk_freqs[i].freq_hz; i++) |
| 7259 | if (ufs_ref_clk_freqs[i].freq_hz == freq) |
| 7260 | return ufs_ref_clk_freqs[i].val; |
| 7261 | |
| 7262 | return REF_CLK_FREQ_INVAL; |
| 7263 | } |
| 7264 | |
| 7265 | void ufshcd_parse_dev_ref_clk_freq(struct ufs_hba *hba, struct clk *refclk) |
| 7266 | { |
| 7267 | unsigned long freq; |
| 7268 | |
| 7269 | freq = clk_get_rate(refclk); |
| 7270 | |
| 7271 | hba->dev_ref_clk_freq = |
| 7272 | ufs_get_bref_clk_from_hz(freq); |
| 7273 | |
| 7274 | if (hba->dev_ref_clk_freq == REF_CLK_FREQ_INVAL) |
| 7275 | dev_err(hba->dev, |
| 7276 | "invalid ref_clk setting = %ld\n", freq); |
| 7277 | } |
| 7278 | |
| 7279 | static int ufshcd_set_dev_ref_clk(struct ufs_hba *hba) |
| 7280 | { |
| 7281 | int err; |
| 7282 | u32 ref_clk; |
| 7283 | u32 freq = hba->dev_ref_clk_freq; |
| 7284 | |
| 7285 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
| 7286 | QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &ref_clk); |
| 7287 | |
| 7288 | if (err) { |
| 7289 | dev_err(hba->dev, "failed reading bRefClkFreq. err = %d\n", |
| 7290 | err); |
| 7291 | goto out; |
| 7292 | } |
| 7293 | |
| 7294 | if (ref_clk == freq) |
| 7295 | goto out; /* nothing to update */ |
| 7296 | |
| 7297 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
| 7298 | QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &freq); |
| 7299 | |
| 7300 | if (err) { |
| 7301 | dev_err(hba->dev, "bRefClkFreq setting to %lu Hz failed\n", |
| 7302 | ufs_ref_clk_freqs[freq].freq_hz); |
| 7303 | goto out; |
| 7304 | } |
| 7305 | |
| 7306 | dev_dbg(hba->dev, "bRefClkFreq setting to %lu Hz succeeded\n", |
| 7307 | ufs_ref_clk_freqs[freq].freq_hz); |
| 7308 | |
| 7309 | out: |
| 7310 | return err; |
| 7311 | } |
| 7312 | |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7313 | static int ufshcd_device_params_init(struct ufs_hba *hba) |
| 7314 | { |
| 7315 | bool flag; |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 7316 | int ret, i; |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7317 | |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 7318 | /* Init device descriptor sizes */ |
| 7319 | for (i = 0; i < QUERY_DESC_IDN_MAX; i++) |
| 7320 | hba->desc_size[i] = QUERY_DESC_MAX_SIZE; |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7321 | |
Bean Huo | 731f062 | 2020-01-20 14:08:19 +0100 | [diff] [blame] | 7322 | /* Init UFS geometry descriptor related parameters */ |
| 7323 | ret = ufshcd_device_geo_params_init(hba); |
| 7324 | if (ret) |
| 7325 | goto out; |
| 7326 | |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7327 | /* Check and apply UFS device quirks */ |
| 7328 | ret = ufs_get_device_desc(hba); |
| 7329 | if (ret) { |
| 7330 | dev_err(hba->dev, "%s: Failed getting device info. err = %d\n", |
| 7331 | __func__, ret); |
| 7332 | goto out; |
| 7333 | } |
| 7334 | |
Can Guo | 09f1779 | 2020-02-10 19:40:49 -0800 | [diff] [blame] | 7335 | ufshcd_get_ref_clk_gating_wait(hba); |
| 7336 | |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7337 | if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG, |
Stanley Chu | 1f34eed | 2020-05-08 16:01:12 +0800 | [diff] [blame] | 7338 | QUERY_FLAG_IDN_PWR_ON_WPE, 0, &flag)) |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7339 | hba->dev_info.f_power_on_wp_en = flag; |
| 7340 | |
Bean Huo | 2b35b2a | 2020-01-20 14:08:16 +0100 | [diff] [blame] | 7341 | /* Probe maximum power mode co-supported by both UFS host and device */ |
| 7342 | if (ufshcd_get_max_pwr_mode(hba)) |
| 7343 | dev_err(hba->dev, |
| 7344 | "%s: Failed getting max supported power mode\n", |
| 7345 | __func__); |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7346 | out: |
| 7347 | return ret; |
| 7348 | } |
| 7349 | |
| 7350 | /** |
| 7351 | * ufshcd_add_lus - probe and add UFS logical units |
| 7352 | * @hba: per-adapter instance |
| 7353 | */ |
| 7354 | static int ufshcd_add_lus(struct ufs_hba *hba) |
| 7355 | { |
| 7356 | int ret; |
| 7357 | |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7358 | /* Add required well known logical units to scsi mid layer */ |
| 7359 | ret = ufshcd_scsi_add_wlus(hba); |
| 7360 | if (ret) |
| 7361 | goto out; |
| 7362 | |
| 7363 | /* Initialize devfreq after UFS device is detected */ |
| 7364 | if (ufshcd_is_clkscaling_supported(hba)) { |
| 7365 | memcpy(&hba->clk_scaling.saved_pwr_info.info, |
| 7366 | &hba->pwr_info, |
| 7367 | sizeof(struct ufs_pa_layer_attr)); |
| 7368 | hba->clk_scaling.saved_pwr_info.is_valid = true; |
| 7369 | if (!hba->devfreq) { |
| 7370 | ret = ufshcd_devfreq_init(hba); |
| 7371 | if (ret) |
| 7372 | goto out; |
| 7373 | } |
| 7374 | |
| 7375 | hba->clk_scaling.is_allowed = true; |
| 7376 | } |
| 7377 | |
| 7378 | ufs_bsg_probe(hba); |
| 7379 | scsi_scan_host(hba->host); |
| 7380 | pm_runtime_put_sync(hba->dev); |
| 7381 | |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7382 | out: |
| 7383 | return ret; |
| 7384 | } |
| 7385 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 7386 | /** |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7387 | * ufshcd_probe_hba - probe hba to detect device and initialize |
| 7388 | * @hba: per-adapter instance |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7389 | * @async: asynchronous execution or not |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7390 | * |
| 7391 | * Execute link-startup and verify device initialization |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7392 | */ |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7393 | static int ufshcd_probe_hba(struct ufs_hba *hba, bool async) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7394 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7395 | int ret; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7396 | ktime_t start = ktime_get(); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7397 | |
| 7398 | ret = ufshcd_link_startup(hba); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 7399 | if (ret) |
| 7400 | goto out; |
| 7401 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 7402 | /* Debug counters initialization */ |
| 7403 | ufshcd_clear_dbg_ufs_stats(hba); |
| 7404 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7405 | /* UniPro link is active now */ |
| 7406 | ufshcd_set_link_active(hba); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 7407 | |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7408 | /* Verify device initialization by sending NOP OUT UPIU */ |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 7409 | ret = ufshcd_verify_dev_init(hba); |
| 7410 | if (ret) |
| 7411 | goto out; |
| 7412 | |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7413 | /* Initiate UFS initialization, and waiting until completion */ |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 7414 | ret = ufshcd_complete_dev_init(hba); |
| 7415 | if (ret) |
| 7416 | goto out; |
| 7417 | |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7418 | /* |
| 7419 | * Initialize UFS device parameters used by driver, these |
| 7420 | * parameters are associated with UFS descriptors. |
| 7421 | */ |
| 7422 | if (async) { |
| 7423 | ret = ufshcd_device_params_init(hba); |
| 7424 | if (ret) |
| 7425 | goto out; |
Tomas Winkler | 93fdd5a | 2017-01-05 10:45:12 +0200 | [diff] [blame] | 7426 | } |
| 7427 | |
Bean Huo | 0975006 | 2020-01-20 14:08:14 +0100 | [diff] [blame] | 7428 | ufshcd_tune_unipro_params(hba); |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 7429 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7430 | /* UFS device is also active now */ |
| 7431 | ufshcd_set_ufs_dev_active(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 7432 | ufshcd_force_reset_auto_bkops(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7433 | hba->wlun_dev_clr_ua = true; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 7434 | |
Bean Huo | 2b35b2a | 2020-01-20 14:08:16 +0100 | [diff] [blame] | 7435 | /* Gear up to HS gear if supported */ |
| 7436 | if (hba->max_pwr_info.is_valid) { |
Subhash Jadavani | 9e1e8a7 | 2018-10-16 14:29:41 +0530 | [diff] [blame] | 7437 | /* |
| 7438 | * Set the right value to bRefClkFreq before attempting to |
| 7439 | * switch to HS gears. |
| 7440 | */ |
| 7441 | if (hba->dev_ref_clk_freq != REF_CLK_FREQ_INVAL) |
| 7442 | ufshcd_set_dev_ref_clk(hba); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 7443 | ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info); |
Dov Levenglick | 8643ae6 | 2016-10-17 17:10:14 -0700 | [diff] [blame] | 7444 | if (ret) { |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 7445 | dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n", |
| 7446 | __func__, ret); |
Dov Levenglick | 8643ae6 | 2016-10-17 17:10:14 -0700 | [diff] [blame] | 7447 | goto out; |
| 7448 | } |
Can Guo | 6a9df81 | 2020-02-11 21:38:28 -0800 | [diff] [blame] | 7449 | ufshcd_print_pwr_info(hba); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 7450 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7451 | |
Can Guo | e89860f | 2020-03-26 02:25:41 -0700 | [diff] [blame] | 7452 | /* |
| 7453 | * bActiveICCLevel is volatile for UFS device (as per latest v2.1 spec) |
| 7454 | * and for removable UFS card as well, hence always set the parameter. |
| 7455 | * Note: Error handler may issue the device reset hence resetting |
| 7456 | * bActiveICCLevel as well so it is always safe to set this here. |
| 7457 | */ |
| 7458 | ufshcd_set_active_icc_lvl(hba); |
| 7459 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 7460 | /* set the state as operational after switching to desired gear */ |
| 7461 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 7462 | |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 7463 | ufshcd_wb_config(hba); |
Can Guo | 71d848b | 2019-11-14 22:09:26 -0800 | [diff] [blame] | 7464 | /* Enable Auto-Hibernate if configured */ |
| 7465 | ufshcd_auto_hibern8_enable(hba); |
| 7466 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 7467 | out: |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7468 | |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7469 | trace_ufshcd_init(dev_name(hba->dev), ret, |
| 7470 | ktime_to_us(ktime_sub(ktime_get(), start)), |
Subhash Jadavani | 73eba2b | 2017-01-10 16:48:25 -0800 | [diff] [blame] | 7471 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7472 | return ret; |
| 7473 | } |
| 7474 | |
| 7475 | /** |
| 7476 | * ufshcd_async_scan - asynchronous execution for probing hba |
| 7477 | * @data: data pointer to pass to this function |
| 7478 | * @cookie: cookie data |
| 7479 | */ |
| 7480 | static void ufshcd_async_scan(void *data, async_cookie_t cookie) |
| 7481 | { |
| 7482 | struct ufs_hba *hba = (struct ufs_hba *)data; |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7483 | int ret; |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7484 | |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7485 | /* Initialize hba, detect and initialize UFS device */ |
| 7486 | ret = ufshcd_probe_hba(hba, true); |
| 7487 | if (ret) |
| 7488 | goto out; |
| 7489 | |
| 7490 | /* Probe and add UFS logical units */ |
| 7491 | ret = ufshcd_add_lus(hba); |
| 7492 | out: |
| 7493 | /* |
| 7494 | * If we failed to initialize the device or the device is not |
| 7495 | * present, turn off the power/clocks etc. |
| 7496 | */ |
| 7497 | if (ret) { |
| 7498 | pm_runtime_put_sync(hba->dev); |
| 7499 | ufshcd_exit_clk_scaling(hba); |
| 7500 | ufshcd_hba_exit(hba); |
| 7501 | } |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7502 | } |
| 7503 | |
Stanislav Nijnikov | d829fc8 | 2018-02-15 14:14:09 +0200 | [diff] [blame] | 7504 | static const struct attribute_group *ufshcd_driver_groups[] = { |
| 7505 | &ufs_sysfs_unit_descriptor_group, |
Stanislav Nijnikov | ec92b59 | 2018-02-15 14:14:11 +0200 | [diff] [blame] | 7506 | &ufs_sysfs_lun_attributes_group, |
Stanislav Nijnikov | d829fc8 | 2018-02-15 14:14:09 +0200 | [diff] [blame] | 7507 | NULL, |
| 7508 | }; |
| 7509 | |
Stanley Chu | 90b8491 | 2020-05-09 17:37:13 +0800 | [diff] [blame] | 7510 | static struct ufs_hba_variant_params ufs_hba_vps = { |
| 7511 | .hba_enable_delay_us = 1000, |
Stanley Chu | d14734ae | 2020-05-09 17:37:15 +0800 | [diff] [blame] | 7512 | .wb_flush_threshold = UFS_WB_BUF_REMAIN_PERCENT(40), |
Stanley Chu | 90b8491 | 2020-05-09 17:37:13 +0800 | [diff] [blame] | 7513 | .devfreq_profile.polling_ms = 100, |
| 7514 | .devfreq_profile.target = ufshcd_devfreq_target, |
| 7515 | .devfreq_profile.get_dev_status = ufshcd_devfreq_get_dev_status, |
| 7516 | .ondemand_data.upthreshold = 70, |
| 7517 | .ondemand_data.downdifferential = 5, |
| 7518 | }; |
| 7519 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7520 | static struct scsi_host_template ufshcd_driver_template = { |
| 7521 | .module = THIS_MODULE, |
| 7522 | .name = UFSHCD, |
| 7523 | .proc_name = UFSHCD, |
| 7524 | .queuecommand = ufshcd_queuecommand, |
| 7525 | .slave_alloc = ufshcd_slave_alloc, |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 7526 | .slave_configure = ufshcd_slave_configure, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7527 | .slave_destroy = ufshcd_slave_destroy, |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 7528 | .change_queue_depth = ufshcd_change_queue_depth, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7529 | .eh_abort_handler = ufshcd_abort, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 7530 | .eh_device_reset_handler = ufshcd_eh_device_reset_handler, |
| 7531 | .eh_host_reset_handler = ufshcd_eh_host_reset_handler, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7532 | .this_id = -1, |
| 7533 | .sg_tablesize = SG_ALL, |
| 7534 | .cmd_per_lun = UFSHCD_CMD_PER_LUN, |
| 7535 | .can_queue = UFSHCD_CAN_QUEUE, |
Christoph Hellwig | 552a990 | 2019-06-17 14:19:55 +0200 | [diff] [blame] | 7536 | .max_segment_size = PRDT_DATA_BYTE_COUNT_MAX, |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7537 | .max_host_blocked = 1, |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 7538 | .track_queue_depth = 1, |
Stanislav Nijnikov | d829fc8 | 2018-02-15 14:14:09 +0200 | [diff] [blame] | 7539 | .sdev_groups = ufshcd_driver_groups, |
Christoph Hellwig | 4af14d1 | 2018-12-13 16:17:09 +0100 | [diff] [blame] | 7540 | .dma_boundary = PAGE_SIZE - 1, |
Stanley Chu | 49615ba | 2019-09-16 23:56:50 +0800 | [diff] [blame] | 7541 | .rpm_autosuspend_delay = RPM_AUTOSUSPEND_DELAY_MS, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7542 | }; |
| 7543 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7544 | static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg, |
| 7545 | int ua) |
| 7546 | { |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 7547 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7548 | |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 7549 | if (!vreg) |
| 7550 | return 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7551 | |
Stanley Chu | 0487fff | 2019-03-28 17:16:25 +0800 | [diff] [blame] | 7552 | /* |
| 7553 | * "set_load" operation shall be required on those regulators |
| 7554 | * which specifically configured current limitation. Otherwise |
| 7555 | * zero max_uA may cause unexpected behavior when regulator is |
| 7556 | * enabled or set as high power mode. |
| 7557 | */ |
| 7558 | if (!vreg->max_uA) |
| 7559 | return 0; |
| 7560 | |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 7561 | ret = regulator_set_load(vreg->reg, ua); |
| 7562 | if (ret < 0) { |
| 7563 | dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n", |
| 7564 | __func__, vreg->name, ua, ret); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7565 | } |
| 7566 | |
| 7567 | return ret; |
| 7568 | } |
| 7569 | |
| 7570 | static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba, |
| 7571 | struct ufs_vreg *vreg) |
| 7572 | { |
Marc Gonzalez | 7306798 | 2019-02-27 11:41:45 +0100 | [diff] [blame] | 7573 | return ufshcd_config_vreg_load(hba->dev, vreg, UFS_VREG_LPM_LOAD_UA); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7574 | } |
| 7575 | |
| 7576 | static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba, |
| 7577 | struct ufs_vreg *vreg) |
| 7578 | { |
Adrian Hunter | 7c7cfdc | 2019-08-14 15:59:50 +0300 | [diff] [blame] | 7579 | if (!vreg) |
| 7580 | return 0; |
| 7581 | |
Marc Gonzalez | 7306798 | 2019-02-27 11:41:45 +0100 | [diff] [blame] | 7582 | return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7583 | } |
| 7584 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7585 | static int ufshcd_config_vreg(struct device *dev, |
| 7586 | struct ufs_vreg *vreg, bool on) |
| 7587 | { |
| 7588 | int ret = 0; |
Gustavo A. R. Silva | 7275359 | 2017-11-20 08:12:29 -0600 | [diff] [blame] | 7589 | struct regulator *reg; |
| 7590 | const char *name; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7591 | int min_uV, uA_load; |
| 7592 | |
| 7593 | BUG_ON(!vreg); |
| 7594 | |
Gustavo A. R. Silva | 7275359 | 2017-11-20 08:12:29 -0600 | [diff] [blame] | 7595 | reg = vreg->reg; |
| 7596 | name = vreg->name; |
| 7597 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7598 | if (regulator_count_voltages(reg) > 0) { |
Asutosh Das | 90d88f4 | 2020-02-10 19:40:45 -0800 | [diff] [blame] | 7599 | uA_load = on ? vreg->max_uA : 0; |
| 7600 | ret = ufshcd_config_vreg_load(dev, vreg, uA_load); |
| 7601 | if (ret) |
| 7602 | goto out; |
| 7603 | |
Stanley Chu | 3b141e8 | 2019-03-28 17:16:24 +0800 | [diff] [blame] | 7604 | if (vreg->min_uV && vreg->max_uV) { |
| 7605 | min_uV = on ? vreg->min_uV : 0; |
| 7606 | ret = regulator_set_voltage(reg, min_uV, vreg->max_uV); |
| 7607 | if (ret) { |
| 7608 | dev_err(dev, |
| 7609 | "%s: %s set voltage failed, err=%d\n", |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7610 | __func__, name, ret); |
Stanley Chu | 3b141e8 | 2019-03-28 17:16:24 +0800 | [diff] [blame] | 7611 | goto out; |
| 7612 | } |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7613 | } |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7614 | } |
| 7615 | out: |
| 7616 | return ret; |
| 7617 | } |
| 7618 | |
| 7619 | static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 7620 | { |
| 7621 | int ret = 0; |
| 7622 | |
Marc Gonzalez | 7306798 | 2019-02-27 11:41:45 +0100 | [diff] [blame] | 7623 | if (!vreg || vreg->enabled) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7624 | goto out; |
| 7625 | |
| 7626 | ret = ufshcd_config_vreg(dev, vreg, true); |
| 7627 | if (!ret) |
| 7628 | ret = regulator_enable(vreg->reg); |
| 7629 | |
| 7630 | if (!ret) |
| 7631 | vreg->enabled = true; |
| 7632 | else |
| 7633 | dev_err(dev, "%s: %s enable failed, err=%d\n", |
| 7634 | __func__, vreg->name, ret); |
| 7635 | out: |
| 7636 | return ret; |
| 7637 | } |
| 7638 | |
| 7639 | static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 7640 | { |
| 7641 | int ret = 0; |
| 7642 | |
Marc Gonzalez | 7306798 | 2019-02-27 11:41:45 +0100 | [diff] [blame] | 7643 | if (!vreg || !vreg->enabled) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7644 | goto out; |
| 7645 | |
| 7646 | ret = regulator_disable(vreg->reg); |
| 7647 | |
| 7648 | if (!ret) { |
| 7649 | /* ignore errors on applying disable config */ |
| 7650 | ufshcd_config_vreg(dev, vreg, false); |
| 7651 | vreg->enabled = false; |
| 7652 | } else { |
| 7653 | dev_err(dev, "%s: %s disable failed, err=%d\n", |
| 7654 | __func__, vreg->name, ret); |
| 7655 | } |
| 7656 | out: |
| 7657 | return ret; |
| 7658 | } |
| 7659 | |
| 7660 | static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on) |
| 7661 | { |
| 7662 | int ret = 0; |
| 7663 | struct device *dev = hba->dev; |
| 7664 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 7665 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7666 | ret = ufshcd_toggle_vreg(dev, info->vcc, on); |
| 7667 | if (ret) |
| 7668 | goto out; |
| 7669 | |
| 7670 | ret = ufshcd_toggle_vreg(dev, info->vccq, on); |
| 7671 | if (ret) |
| 7672 | goto out; |
| 7673 | |
| 7674 | ret = ufshcd_toggle_vreg(dev, info->vccq2, on); |
| 7675 | if (ret) |
| 7676 | goto out; |
| 7677 | |
| 7678 | out: |
| 7679 | if (ret) { |
| 7680 | ufshcd_toggle_vreg(dev, info->vccq2, false); |
| 7681 | ufshcd_toggle_vreg(dev, info->vccq, false); |
| 7682 | ufshcd_toggle_vreg(dev, info->vcc, false); |
| 7683 | } |
| 7684 | return ret; |
| 7685 | } |
| 7686 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7687 | static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on) |
| 7688 | { |
| 7689 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 7690 | |
Zeng Guangyue | 60b7b82 | 2019-03-30 17:03:13 +0800 | [diff] [blame] | 7691 | return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on); |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7692 | } |
| 7693 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7694 | static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 7695 | { |
| 7696 | int ret = 0; |
| 7697 | |
| 7698 | if (!vreg) |
| 7699 | goto out; |
| 7700 | |
| 7701 | vreg->reg = devm_regulator_get(dev, vreg->name); |
| 7702 | if (IS_ERR(vreg->reg)) { |
| 7703 | ret = PTR_ERR(vreg->reg); |
| 7704 | dev_err(dev, "%s: %s get failed, err=%d\n", |
| 7705 | __func__, vreg->name, ret); |
| 7706 | } |
| 7707 | out: |
| 7708 | return ret; |
| 7709 | } |
| 7710 | |
| 7711 | static int ufshcd_init_vreg(struct ufs_hba *hba) |
| 7712 | { |
| 7713 | int ret = 0; |
| 7714 | struct device *dev = hba->dev; |
| 7715 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 7716 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7717 | ret = ufshcd_get_vreg(dev, info->vcc); |
| 7718 | if (ret) |
| 7719 | goto out; |
| 7720 | |
| 7721 | ret = ufshcd_get_vreg(dev, info->vccq); |
| 7722 | if (ret) |
| 7723 | goto out; |
| 7724 | |
| 7725 | ret = ufshcd_get_vreg(dev, info->vccq2); |
| 7726 | out: |
| 7727 | return ret; |
| 7728 | } |
| 7729 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7730 | static int ufshcd_init_hba_vreg(struct ufs_hba *hba) |
| 7731 | { |
| 7732 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 7733 | |
| 7734 | if (info) |
| 7735 | return ufshcd_get_vreg(hba->dev, info->vdd_hba); |
| 7736 | |
| 7737 | return 0; |
| 7738 | } |
| 7739 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7740 | static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on, |
| 7741 | bool skip_ref_clk) |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7742 | { |
| 7743 | int ret = 0; |
| 7744 | struct ufs_clk_info *clki; |
| 7745 | struct list_head *head = &hba->clk_list_head; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7746 | unsigned long flags; |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 7747 | ktime_t start = ktime_get(); |
| 7748 | bool clk_state_changed = false; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7749 | |
Szymon Mielczarek | 566ec9a | 2017-06-05 11:36:54 +0300 | [diff] [blame] | 7750 | if (list_empty(head)) |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7751 | goto out; |
| 7752 | |
Can Guo | 38f3242 | 2020-02-10 19:40:47 -0800 | [diff] [blame] | 7753 | ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE); |
| 7754 | if (ret) |
| 7755 | return ret; |
Subhash Jadavani | 1e879e8 | 2016-10-06 21:48:22 -0700 | [diff] [blame] | 7756 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7757 | list_for_each_entry(clki, head, list) { |
| 7758 | if (!IS_ERR_OR_NULL(clki->clk)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7759 | if (skip_ref_clk && !strcmp(clki->name, "ref_clk")) |
| 7760 | continue; |
| 7761 | |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 7762 | clk_state_changed = on ^ clki->enabled; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7763 | if (on && !clki->enabled) { |
| 7764 | ret = clk_prepare_enable(clki->clk); |
| 7765 | if (ret) { |
| 7766 | dev_err(hba->dev, "%s: %s prepare enable failed, %d\n", |
| 7767 | __func__, clki->name, ret); |
| 7768 | goto out; |
| 7769 | } |
| 7770 | } else if (!on && clki->enabled) { |
| 7771 | clk_disable_unprepare(clki->clk); |
| 7772 | } |
| 7773 | clki->enabled = on; |
| 7774 | dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__, |
| 7775 | clki->name, on ? "en" : "dis"); |
| 7776 | } |
| 7777 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7778 | |
Can Guo | 38f3242 | 2020-02-10 19:40:47 -0800 | [diff] [blame] | 7779 | ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE); |
| 7780 | if (ret) |
| 7781 | return ret; |
Subhash Jadavani | 1e879e8 | 2016-10-06 21:48:22 -0700 | [diff] [blame] | 7782 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7783 | out: |
| 7784 | if (ret) { |
| 7785 | list_for_each_entry(clki, head, list) { |
| 7786 | if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled) |
| 7787 | clk_disable_unprepare(clki->clk); |
| 7788 | } |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7789 | } else if (!ret && on) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7790 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 7791 | hba->clk_gating.state = CLKS_ON; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7792 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 7793 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7794 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7795 | } |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7796 | |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 7797 | if (clk_state_changed) |
| 7798 | trace_ufshcd_profile_clk_gating(dev_name(hba->dev), |
| 7799 | (on ? "on" : "off"), |
| 7800 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7801 | return ret; |
| 7802 | } |
| 7803 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7804 | static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on) |
| 7805 | { |
| 7806 | return __ufshcd_setup_clocks(hba, on, false); |
| 7807 | } |
| 7808 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7809 | static int ufshcd_init_clocks(struct ufs_hba *hba) |
| 7810 | { |
| 7811 | int ret = 0; |
| 7812 | struct ufs_clk_info *clki; |
| 7813 | struct device *dev = hba->dev; |
| 7814 | struct list_head *head = &hba->clk_list_head; |
| 7815 | |
Szymon Mielczarek | 566ec9a | 2017-06-05 11:36:54 +0300 | [diff] [blame] | 7816 | if (list_empty(head)) |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7817 | goto out; |
| 7818 | |
| 7819 | list_for_each_entry(clki, head, list) { |
| 7820 | if (!clki->name) |
| 7821 | continue; |
| 7822 | |
| 7823 | clki->clk = devm_clk_get(dev, clki->name); |
| 7824 | if (IS_ERR(clki->clk)) { |
| 7825 | ret = PTR_ERR(clki->clk); |
| 7826 | dev_err(dev, "%s: %s clk get failed, %d\n", |
| 7827 | __func__, clki->name, ret); |
| 7828 | goto out; |
| 7829 | } |
| 7830 | |
Subhash Jadavani | 9e1e8a7 | 2018-10-16 14:29:41 +0530 | [diff] [blame] | 7831 | /* |
| 7832 | * Parse device ref clk freq as per device tree "ref_clk". |
| 7833 | * Default dev_ref_clk_freq is set to REF_CLK_FREQ_INVAL |
| 7834 | * in ufshcd_alloc_host(). |
| 7835 | */ |
| 7836 | if (!strcmp(clki->name, "ref_clk")) |
| 7837 | ufshcd_parse_dev_ref_clk_freq(hba, clki->clk); |
| 7838 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7839 | if (clki->max_freq) { |
| 7840 | ret = clk_set_rate(clki->clk, clki->max_freq); |
| 7841 | if (ret) { |
| 7842 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 7843 | __func__, clki->name, |
| 7844 | clki->max_freq, ret); |
| 7845 | goto out; |
| 7846 | } |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 7847 | clki->curr_freq = clki->max_freq; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7848 | } |
| 7849 | dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__, |
| 7850 | clki->name, clk_get_rate(clki->clk)); |
| 7851 | } |
| 7852 | out: |
| 7853 | return ret; |
| 7854 | } |
| 7855 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7856 | static int ufshcd_variant_hba_init(struct ufs_hba *hba) |
| 7857 | { |
| 7858 | int err = 0; |
| 7859 | |
| 7860 | if (!hba->vops) |
| 7861 | goto out; |
| 7862 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7863 | err = ufshcd_vops_init(hba); |
| 7864 | if (err) |
| 7865 | goto out; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7866 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7867 | err = ufshcd_vops_setup_regulators(hba, true); |
| 7868 | if (err) |
| 7869 | goto out_exit; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7870 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7871 | goto out; |
| 7872 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7873 | out_exit: |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7874 | ufshcd_vops_exit(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7875 | out: |
| 7876 | if (err) |
| 7877 | dev_err(hba->dev, "%s: variant %s init failed err %d\n", |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7878 | __func__, ufshcd_get_var_name(hba), err); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7879 | return err; |
| 7880 | } |
| 7881 | |
| 7882 | static void ufshcd_variant_hba_exit(struct ufs_hba *hba) |
| 7883 | { |
| 7884 | if (!hba->vops) |
| 7885 | return; |
| 7886 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7887 | ufshcd_vops_setup_regulators(hba, false); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7888 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7889 | ufshcd_vops_exit(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7890 | } |
| 7891 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7892 | static int ufshcd_hba_init(struct ufs_hba *hba) |
| 7893 | { |
| 7894 | int err; |
| 7895 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7896 | /* |
| 7897 | * Handle host controller power separately from the UFS device power |
| 7898 | * rails as it will help controlling the UFS host controller power |
| 7899 | * collapse easily which is different than UFS device power collapse. |
| 7900 | * Also, enable the host controller power before we go ahead with rest |
| 7901 | * of the initialization here. |
| 7902 | */ |
| 7903 | err = ufshcd_init_hba_vreg(hba); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7904 | if (err) |
| 7905 | goto out; |
| 7906 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7907 | err = ufshcd_setup_hba_vreg(hba, true); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7908 | if (err) |
| 7909 | goto out; |
| 7910 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7911 | err = ufshcd_init_clocks(hba); |
| 7912 | if (err) |
| 7913 | goto out_disable_hba_vreg; |
| 7914 | |
| 7915 | err = ufshcd_setup_clocks(hba, true); |
| 7916 | if (err) |
| 7917 | goto out_disable_hba_vreg; |
| 7918 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7919 | err = ufshcd_init_vreg(hba); |
| 7920 | if (err) |
| 7921 | goto out_disable_clks; |
| 7922 | |
| 7923 | err = ufshcd_setup_vreg(hba, true); |
| 7924 | if (err) |
| 7925 | goto out_disable_clks; |
| 7926 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7927 | err = ufshcd_variant_hba_init(hba); |
| 7928 | if (err) |
| 7929 | goto out_disable_vreg; |
| 7930 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7931 | hba->is_powered = true; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7932 | goto out; |
| 7933 | |
| 7934 | out_disable_vreg: |
| 7935 | ufshcd_setup_vreg(hba, false); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7936 | out_disable_clks: |
| 7937 | ufshcd_setup_clocks(hba, false); |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7938 | out_disable_hba_vreg: |
| 7939 | ufshcd_setup_hba_vreg(hba, false); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7940 | out: |
| 7941 | return err; |
| 7942 | } |
| 7943 | |
| 7944 | static void ufshcd_hba_exit(struct ufs_hba *hba) |
| 7945 | { |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7946 | if (hba->is_powered) { |
| 7947 | ufshcd_variant_hba_exit(hba); |
| 7948 | ufshcd_setup_vreg(hba, false); |
Gilad Broner | a508253 | 2016-10-17 17:10:00 -0700 | [diff] [blame] | 7949 | ufshcd_suspend_clkscaling(hba); |
Vivek Gautam | eebcc19 | 2018-08-07 23:17:39 +0530 | [diff] [blame] | 7950 | if (ufshcd_is_clkscaling_supported(hba)) |
subhashj@codeaurora.org | 0701e49 | 2017-02-03 16:58:01 -0800 | [diff] [blame] | 7951 | if (hba->devfreq) |
| 7952 | ufshcd_suspend_clkscaling(hba); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7953 | ufshcd_setup_clocks(hba, false); |
| 7954 | ufshcd_setup_hba_vreg(hba, false); |
| 7955 | hba->is_powered = false; |
Bean Huo | 0975006 | 2020-01-20 14:08:14 +0100 | [diff] [blame] | 7956 | ufs_put_device_desc(hba); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7957 | } |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7958 | } |
| 7959 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7960 | static int |
| 7961 | ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7962 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7963 | unsigned char cmd[6] = {REQUEST_SENSE, |
| 7964 | 0, |
| 7965 | 0, |
| 7966 | 0, |
Avri Altman | 09a5a24 | 2018-11-22 20:04:56 +0200 | [diff] [blame] | 7967 | UFS_SENSE_SIZE, |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7968 | 0}; |
| 7969 | char *buffer; |
| 7970 | int ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7971 | |
Avri Altman | 09a5a24 | 2018-11-22 20:04:56 +0200 | [diff] [blame] | 7972 | buffer = kzalloc(UFS_SENSE_SIZE, GFP_KERNEL); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7973 | if (!buffer) { |
| 7974 | ret = -ENOMEM; |
| 7975 | goto out; |
| 7976 | } |
| 7977 | |
Christoph Hellwig | fcbfffe | 2017-02-23 16:02:37 +0100 | [diff] [blame] | 7978 | ret = scsi_execute(sdp, cmd, DMA_FROM_DEVICE, buffer, |
Avri Altman | 09a5a24 | 2018-11-22 20:04:56 +0200 | [diff] [blame] | 7979 | UFS_SENSE_SIZE, NULL, NULL, |
Christoph Hellwig | fcbfffe | 2017-02-23 16:02:37 +0100 | [diff] [blame] | 7980 | msecs_to_jiffies(1000), 3, 0, RQF_PM, NULL); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7981 | if (ret) |
| 7982 | pr_err("%s: failed with err %d\n", __func__, ret); |
| 7983 | |
| 7984 | kfree(buffer); |
| 7985 | out: |
| 7986 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7987 | } |
| 7988 | |
| 7989 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7990 | * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device |
| 7991 | * power mode |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7992 | * @hba: per adapter instance |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7993 | * @pwr_mode: device power mode to set |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7994 | * |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7995 | * Returns 0 if requested power mode is set successfully |
| 7996 | * Returns non-zero if failed to set the requested power mode |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7997 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7998 | static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba, |
| 7999 | enum ufs_dev_pwr_mode pwr_mode) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8000 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8001 | unsigned char cmd[6] = { START_STOP }; |
| 8002 | struct scsi_sense_hdr sshdr; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 8003 | struct scsi_device *sdp; |
| 8004 | unsigned long flags; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8005 | int ret; |
| 8006 | |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 8007 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 8008 | sdp = hba->sdev_ufs_device; |
| 8009 | if (sdp) { |
| 8010 | ret = scsi_device_get(sdp); |
| 8011 | if (!ret && !scsi_device_online(sdp)) { |
| 8012 | ret = -ENODEV; |
| 8013 | scsi_device_put(sdp); |
| 8014 | } |
| 8015 | } else { |
| 8016 | ret = -ENODEV; |
| 8017 | } |
| 8018 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 8019 | |
| 8020 | if (ret) |
| 8021 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8022 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8023 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8024 | * If scsi commands fail, the scsi mid-layer schedules scsi error- |
| 8025 | * handling, which would wait for host to be resumed. Since we know |
| 8026 | * we are functional while we are here, skip host resume in error |
| 8027 | * handling context. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8028 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8029 | hba->host->eh_noresume = 1; |
| 8030 | if (hba->wlun_dev_clr_ua) { |
| 8031 | ret = ufshcd_send_request_sense(hba, sdp); |
| 8032 | if (ret) |
| 8033 | goto out; |
| 8034 | /* Unit attention condition is cleared now */ |
| 8035 | hba->wlun_dev_clr_ua = false; |
| 8036 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8037 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8038 | cmd[4] = pwr_mode << 4; |
| 8039 | |
| 8040 | /* |
| 8041 | * Current function would be generally called from the power management |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 8042 | * callbacks hence set the RQF_PM flag so that it doesn't resume the |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8043 | * already suspended childs. |
| 8044 | */ |
Christoph Hellwig | fcbfffe | 2017-02-23 16:02:37 +0100 | [diff] [blame] | 8045 | ret = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr, |
| 8046 | START_STOP_TIMEOUT, 0, 0, RQF_PM, NULL); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8047 | if (ret) { |
| 8048 | sdev_printk(KERN_WARNING, sdp, |
Hannes Reinecke | ef61329 | 2014-10-24 14:27:00 +0200 | [diff] [blame] | 8049 | "START_STOP failed for power mode: %d, result %x\n", |
| 8050 | pwr_mode, ret); |
Johannes Thumshirn | c65be1a | 2018-06-25 13:20:58 +0200 | [diff] [blame] | 8051 | if (driver_byte(ret) == DRIVER_SENSE) |
Hannes Reinecke | 2104551 | 2015-01-08 07:43:46 +0100 | [diff] [blame] | 8052 | scsi_print_sense_hdr(sdp, NULL, &sshdr); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8053 | } |
| 8054 | |
| 8055 | if (!ret) |
| 8056 | hba->curr_dev_pwr_mode = pwr_mode; |
| 8057 | out: |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 8058 | scsi_device_put(sdp); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8059 | hba->host->eh_noresume = 0; |
| 8060 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8061 | } |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8062 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8063 | static int ufshcd_link_state_transition(struct ufs_hba *hba, |
| 8064 | enum uic_link_state req_link_state, |
| 8065 | int check_for_bkops) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8066 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8067 | int ret = 0; |
| 8068 | |
| 8069 | if (req_link_state == hba->uic_link_state) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8070 | return 0; |
| 8071 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8072 | if (req_link_state == UIC_LINK_HIBERN8_STATE) { |
| 8073 | ret = ufshcd_uic_hibern8_enter(hba); |
| 8074 | if (!ret) |
| 8075 | ufshcd_set_link_hibern8(hba); |
| 8076 | else |
| 8077 | goto out; |
| 8078 | } |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8079 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8080 | * If autobkops is enabled, link can't be turned off because |
| 8081 | * turning off the link would also turn off the device. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8082 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8083 | else if ((req_link_state == UIC_LINK_OFF_STATE) && |
Dan Carpenter | dc30c9e | 2019-12-13 13:49:35 +0300 | [diff] [blame] | 8084 | (!check_for_bkops || !hba->auto_bkops_enabled)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8085 | /* |
Yaniv Gardi | f3099fb | 2016-03-10 17:37:17 +0200 | [diff] [blame] | 8086 | * Let's make sure that link is in low power mode, we are doing |
| 8087 | * this currently by putting the link in Hibern8. Otherway to |
| 8088 | * put the link in low power mode is to send the DME end point |
| 8089 | * to device and then send the DME reset command to local |
| 8090 | * unipro. But putting the link in hibern8 is much faster. |
| 8091 | */ |
| 8092 | ret = ufshcd_uic_hibern8_enter(hba); |
| 8093 | if (ret) |
| 8094 | goto out; |
| 8095 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8096 | * Change controller state to "reset state" which |
| 8097 | * should also put the link in off/reset state |
| 8098 | */ |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 8099 | ufshcd_hba_stop(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8100 | /* |
| 8101 | * TODO: Check if we need any delay to make sure that |
| 8102 | * controller is reset |
| 8103 | */ |
| 8104 | ufshcd_set_link_off(hba); |
| 8105 | } |
| 8106 | |
| 8107 | out: |
| 8108 | return ret; |
| 8109 | } |
| 8110 | |
| 8111 | static void ufshcd_vreg_set_lpm(struct ufs_hba *hba) |
| 8112 | { |
Stanley Chu | c4df6ee | 2020-07-29 13:18:39 +0800 | [diff] [blame] | 8113 | bool vcc_off = false; |
| 8114 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8115 | /* |
Yaniv Gardi | b799fdf | 2016-03-10 17:37:18 +0200 | [diff] [blame] | 8116 | * It seems some UFS devices may keep drawing more than sleep current |
| 8117 | * (atleast for 500us) from UFS rails (especially from VCCQ rail). |
| 8118 | * To avoid this situation, add 2ms delay before putting these UFS |
| 8119 | * rails in LPM mode. |
| 8120 | */ |
| 8121 | if (!ufshcd_is_link_active(hba) && |
| 8122 | hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM) |
| 8123 | usleep_range(2000, 2100); |
| 8124 | |
| 8125 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8126 | * If UFS device is either in UFS_Sleep turn off VCC rail to save some |
| 8127 | * power. |
| 8128 | * |
| 8129 | * If UFS device and link is in OFF state, all power supplies (VCC, |
| 8130 | * VCCQ, VCCQ2) can be turned off if power on write protect is not |
| 8131 | * required. If UFS link is inactive (Hibern8 or OFF state) and device |
| 8132 | * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode. |
| 8133 | * |
| 8134 | * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway |
| 8135 | * in low power state which would save some power. |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 8136 | * |
| 8137 | * If Write Booster is enabled and the device needs to flush the WB |
| 8138 | * buffer OR if bkops status is urgent for WB, keep Vcc on. |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8139 | */ |
| 8140 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) && |
| 8141 | !hba->dev_info.is_lu_power_on_wp) { |
| 8142 | ufshcd_setup_vreg(hba, false); |
Stanley Chu | c4df6ee | 2020-07-29 13:18:39 +0800 | [diff] [blame] | 8143 | vcc_off = true; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8144 | } else if (!ufshcd_is_ufs_dev_active(hba)) { |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 8145 | ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false); |
Stanley Chu | c4df6ee | 2020-07-29 13:18:39 +0800 | [diff] [blame] | 8146 | vcc_off = true; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8147 | if (!ufshcd_is_link_active(hba)) { |
| 8148 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq); |
| 8149 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2); |
| 8150 | } |
| 8151 | } |
Stanley Chu | c4df6ee | 2020-07-29 13:18:39 +0800 | [diff] [blame] | 8152 | |
| 8153 | /* |
| 8154 | * Some UFS devices require delay after VCC power rail is turned-off. |
| 8155 | */ |
| 8156 | if (vcc_off && hba->vreg_info.vcc && |
| 8157 | hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_AFTER_LPM) |
| 8158 | usleep_range(5000, 5100); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8159 | } |
| 8160 | |
| 8161 | static int ufshcd_vreg_set_hpm(struct ufs_hba *hba) |
| 8162 | { |
| 8163 | int ret = 0; |
| 8164 | |
| 8165 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) && |
| 8166 | !hba->dev_info.is_lu_power_on_wp) { |
| 8167 | ret = ufshcd_setup_vreg(hba, true); |
| 8168 | } else if (!ufshcd_is_ufs_dev_active(hba)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8169 | if (!ret && !ufshcd_is_link_active(hba)) { |
| 8170 | ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq); |
| 8171 | if (ret) |
| 8172 | goto vcc_disable; |
| 8173 | ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2); |
| 8174 | if (ret) |
| 8175 | goto vccq_lpm; |
| 8176 | } |
Subhash Jadavani | 69d72ac | 2016-10-27 17:26:24 -0700 | [diff] [blame] | 8177 | ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8178 | } |
| 8179 | goto out; |
| 8180 | |
| 8181 | vccq_lpm: |
| 8182 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq); |
| 8183 | vcc_disable: |
| 8184 | ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false); |
| 8185 | out: |
| 8186 | return ret; |
| 8187 | } |
| 8188 | |
| 8189 | static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba) |
| 8190 | { |
| 8191 | if (ufshcd_is_link_off(hba)) |
| 8192 | ufshcd_setup_hba_vreg(hba, false); |
| 8193 | } |
| 8194 | |
| 8195 | static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba) |
| 8196 | { |
| 8197 | if (ufshcd_is_link_off(hba)) |
| 8198 | ufshcd_setup_hba_vreg(hba, true); |
| 8199 | } |
| 8200 | |
| 8201 | /** |
| 8202 | * ufshcd_suspend - helper function for suspend operations |
| 8203 | * @hba: per adapter instance |
| 8204 | * @pm_op: desired low power operation type |
| 8205 | * |
| 8206 | * This function will try to put the UFS device and link into low power |
| 8207 | * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl" |
| 8208 | * (System PM level). |
| 8209 | * |
| 8210 | * If this function is called during shutdown, it will make sure that |
| 8211 | * both UFS device and UFS link is powered off. |
| 8212 | * |
| 8213 | * NOTE: UFS device & link must be active before we enter in this function. |
| 8214 | * |
| 8215 | * Returns 0 for success and non-zero for failure |
| 8216 | */ |
| 8217 | static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op) |
| 8218 | { |
| 8219 | int ret = 0; |
| 8220 | enum ufs_pm_level pm_lvl; |
| 8221 | enum ufs_dev_pwr_mode req_dev_pwr_mode; |
| 8222 | enum uic_link_state req_link_state; |
| 8223 | |
| 8224 | hba->pm_op_in_progress = 1; |
| 8225 | if (!ufshcd_is_shutdown_pm(pm_op)) { |
| 8226 | pm_lvl = ufshcd_is_runtime_pm(pm_op) ? |
| 8227 | hba->rpm_lvl : hba->spm_lvl; |
| 8228 | req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl); |
| 8229 | req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl); |
| 8230 | } else { |
| 8231 | req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE; |
| 8232 | req_link_state = UIC_LINK_OFF_STATE; |
| 8233 | } |
| 8234 | |
| 8235 | /* |
| 8236 | * If we can't transition into any of the low power modes |
| 8237 | * just gate the clocks. |
| 8238 | */ |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8239 | ufshcd_hold(hba, false); |
| 8240 | hba->clk_gating.is_suspended = true; |
| 8241 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 8242 | if (hba->clk_scaling.is_allowed) { |
| 8243 | cancel_work_sync(&hba->clk_scaling.suspend_work); |
| 8244 | cancel_work_sync(&hba->clk_scaling.resume_work); |
| 8245 | ufshcd_suspend_clkscaling(hba); |
| 8246 | } |
Subhash Jadavani | d6fcf81 | 2016-10-27 17:26:09 -0700 | [diff] [blame] | 8247 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8248 | if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE && |
| 8249 | req_link_state == UIC_LINK_ACTIVE_STATE) { |
| 8250 | goto disable_clks; |
| 8251 | } |
| 8252 | |
| 8253 | if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) && |
| 8254 | (req_link_state == hba->uic_link_state)) |
Subhash Jadavani | d6fcf81 | 2016-10-27 17:26:09 -0700 | [diff] [blame] | 8255 | goto enable_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8256 | |
| 8257 | /* UFS device & link must be active before we enter in this function */ |
| 8258 | if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) { |
| 8259 | ret = -EINVAL; |
Subhash Jadavani | d6fcf81 | 2016-10-27 17:26:09 -0700 | [diff] [blame] | 8260 | goto enable_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8261 | } |
| 8262 | |
| 8263 | if (ufshcd_is_runtime_pm(pm_op)) { |
Subhash Jadavani | 374a246 | 2014-09-25 15:32:35 +0300 | [diff] [blame] | 8264 | if (ufshcd_can_autobkops_during_suspend(hba)) { |
| 8265 | /* |
| 8266 | * The device is idle with no requests in the queue, |
| 8267 | * allow background operations if bkops status shows |
| 8268 | * that performance might be impacted. |
| 8269 | */ |
| 8270 | ret = ufshcd_urgent_bkops(hba); |
| 8271 | if (ret) |
| 8272 | goto enable_gating; |
| 8273 | } else { |
| 8274 | /* make sure that auto bkops is disabled */ |
| 8275 | ufshcd_disable_auto_bkops(hba); |
| 8276 | } |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 8277 | /* |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 8278 | * If device needs to do BKOP or WB buffer flush during |
| 8279 | * Hibern8, keep device power mode as "active power mode" |
| 8280 | * and VCC supply. |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 8281 | */ |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 8282 | hba->dev_info.b_rpm_dev_flush_capable = |
| 8283 | hba->auto_bkops_enabled || |
| 8284 | (((req_link_state == UIC_LINK_HIBERN8_STATE) || |
| 8285 | ((req_link_state == UIC_LINK_ACTIVE_STATE) && |
| 8286 | ufshcd_is_auto_hibern8_enabled(hba))) && |
| 8287 | ufshcd_wb_need_flush(hba)); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8288 | } |
| 8289 | |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 8290 | if (req_dev_pwr_mode != hba->curr_dev_pwr_mode) { |
| 8291 | if ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) || |
| 8292 | !ufshcd_is_runtime_pm(pm_op)) { |
| 8293 | /* ensure that bkops is disabled */ |
| 8294 | ufshcd_disable_auto_bkops(hba); |
| 8295 | } |
| 8296 | |
| 8297 | if (!hba->dev_info.b_rpm_dev_flush_capable) { |
| 8298 | ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode); |
| 8299 | if (ret) |
| 8300 | goto enable_gating; |
| 8301 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8302 | } |
| 8303 | |
Sayali Lokhande | 2824ec9 | 2020-02-10 19:40:44 -0800 | [diff] [blame] | 8304 | flush_work(&hba->eeh_work); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8305 | ret = ufshcd_link_state_transition(hba, req_link_state, 1); |
| 8306 | if (ret) |
| 8307 | goto set_dev_active; |
| 8308 | |
| 8309 | ufshcd_vreg_set_lpm(hba); |
| 8310 | |
| 8311 | disable_clks: |
| 8312 | /* |
| 8313 | * Call vendor specific suspend callback. As these callbacks may access |
| 8314 | * vendor specific host controller register space call them before the |
| 8315 | * host clocks are ON. |
| 8316 | */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 8317 | ret = ufshcd_vops_suspend(hba, pm_op); |
| 8318 | if (ret) |
| 8319 | goto set_link_active; |
Stanley Chu | dcb6cec | 2019-12-07 20:22:00 +0800 | [diff] [blame] | 8320 | /* |
| 8321 | * Disable the host irq as host controller as there won't be any |
| 8322 | * host controller transaction expected till resume. |
| 8323 | */ |
| 8324 | ufshcd_disable_irq(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8325 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8326 | if (!ufshcd_is_link_active(hba)) |
| 8327 | ufshcd_setup_clocks(hba, false); |
| 8328 | else |
| 8329 | /* If link is active, device ref_clk can't be switched off */ |
| 8330 | __ufshcd_setup_clocks(hba, false, true); |
| 8331 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8332 | hba->clk_gating.state = CLKS_OFF; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8333 | trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state); |
Stanley Chu | dcb6cec | 2019-12-07 20:22:00 +0800 | [diff] [blame] | 8334 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8335 | /* Put the host controller in low power mode if possible */ |
| 8336 | ufshcd_hba_vreg_set_lpm(hba); |
| 8337 | goto out; |
| 8338 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8339 | set_link_active: |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 8340 | if (hba->clk_scaling.is_allowed) |
| 8341 | ufshcd_resume_clkscaling(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8342 | ufshcd_vreg_set_hpm(hba); |
| 8343 | if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba)) |
| 8344 | ufshcd_set_link_active(hba); |
| 8345 | else if (ufshcd_is_link_off(hba)) |
| 8346 | ufshcd_host_reset_and_restore(hba); |
| 8347 | set_dev_active: |
| 8348 | if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE)) |
| 8349 | ufshcd_disable_auto_bkops(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8350 | enable_gating: |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 8351 | if (hba->clk_scaling.is_allowed) |
| 8352 | ufshcd_resume_clkscaling(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8353 | hba->clk_gating.is_suspended = false; |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 8354 | hba->dev_info.b_rpm_dev_flush_capable = false; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8355 | ufshcd_release(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8356 | out: |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 8357 | if (hba->dev_info.b_rpm_dev_flush_capable) { |
| 8358 | schedule_delayed_work(&hba->rpm_dev_flush_recheck_work, |
| 8359 | msecs_to_jiffies(RPM_DEV_FLUSH_RECHECK_WORK_DELAY_MS)); |
| 8360 | } |
| 8361 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8362 | hba->pm_op_in_progress = 0; |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 8363 | |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 8364 | if (ret) |
| 8365 | ufshcd_update_reg_hist(&hba->ufs_stats.suspend_err, (u32)ret); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8366 | return ret; |
| 8367 | } |
| 8368 | |
| 8369 | /** |
| 8370 | * ufshcd_resume - helper function for resume operations |
| 8371 | * @hba: per adapter instance |
| 8372 | * @pm_op: runtime PM or system PM |
| 8373 | * |
| 8374 | * This function basically brings the UFS device, UniPro link and controller |
| 8375 | * to active state. |
| 8376 | * |
| 8377 | * Returns 0 for success and non-zero for failure |
| 8378 | */ |
| 8379 | static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op) |
| 8380 | { |
| 8381 | int ret; |
| 8382 | enum uic_link_state old_link_state; |
| 8383 | |
| 8384 | hba->pm_op_in_progress = 1; |
| 8385 | old_link_state = hba->uic_link_state; |
| 8386 | |
| 8387 | ufshcd_hba_vreg_set_hpm(hba); |
| 8388 | /* Make sure clocks are enabled before accessing controller */ |
| 8389 | ret = ufshcd_setup_clocks(hba, true); |
| 8390 | if (ret) |
| 8391 | goto out; |
| 8392 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8393 | /* enable the host irq as host controller would be active soon */ |
Can Guo | 5231d38 | 2019-12-05 02:14:46 +0000 | [diff] [blame] | 8394 | ufshcd_enable_irq(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8395 | |
| 8396 | ret = ufshcd_vreg_set_hpm(hba); |
| 8397 | if (ret) |
| 8398 | goto disable_irq_and_vops_clks; |
| 8399 | |
| 8400 | /* |
| 8401 | * Call vendor specific resume callback. As these callbacks may access |
| 8402 | * vendor specific host controller register space call them when the |
| 8403 | * host clocks are ON. |
| 8404 | */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 8405 | ret = ufshcd_vops_resume(hba, pm_op); |
| 8406 | if (ret) |
| 8407 | goto disable_vreg; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8408 | |
| 8409 | if (ufshcd_is_link_hibern8(hba)) { |
| 8410 | ret = ufshcd_uic_hibern8_exit(hba); |
| 8411 | if (!ret) |
| 8412 | ufshcd_set_link_active(hba); |
| 8413 | else |
| 8414 | goto vendor_suspend; |
| 8415 | } else if (ufshcd_is_link_off(hba)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8416 | /* |
Asutosh Das | 089f5b6 | 2020-04-13 23:14:48 -0700 | [diff] [blame] | 8417 | * A full initialization of the host and the device is |
| 8418 | * required since the link was put to off during suspend. |
| 8419 | */ |
| 8420 | ret = ufshcd_reset_and_restore(hba); |
| 8421 | /* |
| 8422 | * ufshcd_reset_and_restore() should have already |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8423 | * set the link state as active |
| 8424 | */ |
| 8425 | if (ret || !ufshcd_is_link_active(hba)) |
| 8426 | goto vendor_suspend; |
| 8427 | } |
| 8428 | |
| 8429 | if (!ufshcd_is_ufs_dev_active(hba)) { |
| 8430 | ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE); |
| 8431 | if (ret) |
| 8432 | goto set_old_link_state; |
| 8433 | } |
| 8434 | |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 8435 | if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) |
| 8436 | ufshcd_enable_auto_bkops(hba); |
| 8437 | else |
| 8438 | /* |
| 8439 | * If BKOPs operations are urgently needed at this moment then |
| 8440 | * keep auto-bkops enabled or else disable it. |
| 8441 | */ |
| 8442 | ufshcd_urgent_bkops(hba); |
| 8443 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8444 | hba->clk_gating.is_suspended = false; |
| 8445 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 8446 | if (hba->clk_scaling.is_allowed) |
| 8447 | ufshcd_resume_clkscaling(hba); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 8448 | |
Adrian Hunter | ad44837 | 2018-03-20 15:07:38 +0200 | [diff] [blame] | 8449 | /* Enable Auto-Hibernate if configured */ |
| 8450 | ufshcd_auto_hibern8_enable(hba); |
| 8451 | |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 8452 | if (hba->dev_info.b_rpm_dev_flush_capable) { |
| 8453 | hba->dev_info.b_rpm_dev_flush_capable = false; |
| 8454 | cancel_delayed_work(&hba->rpm_dev_flush_recheck_work); |
| 8455 | } |
| 8456 | |
Can Guo | 71d848b | 2019-11-14 22:09:26 -0800 | [diff] [blame] | 8457 | /* Schedule clock gating in case of no access to UFS device yet */ |
| 8458 | ufshcd_release(hba); |
| 8459 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8460 | goto out; |
| 8461 | |
| 8462 | set_old_link_state: |
| 8463 | ufshcd_link_state_transition(hba, old_link_state, 0); |
| 8464 | vendor_suspend: |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 8465 | ufshcd_vops_suspend(hba, pm_op); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8466 | disable_vreg: |
| 8467 | ufshcd_vreg_set_lpm(hba); |
| 8468 | disable_irq_and_vops_clks: |
| 8469 | ufshcd_disable_irq(hba); |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 8470 | if (hba->clk_scaling.is_allowed) |
| 8471 | ufshcd_suspend_clkscaling(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8472 | ufshcd_setup_clocks(hba, false); |
| 8473 | out: |
| 8474 | hba->pm_op_in_progress = 0; |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 8475 | if (ret) |
| 8476 | ufshcd_update_reg_hist(&hba->ufs_stats.resume_err, (u32)ret); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8477 | return ret; |
| 8478 | } |
| 8479 | |
| 8480 | /** |
| 8481 | * ufshcd_system_suspend - system suspend routine |
| 8482 | * @hba: per adapter instance |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8483 | * |
| 8484 | * Check the description of ufshcd_suspend() function for more details. |
| 8485 | * |
| 8486 | * Returns 0 for success and non-zero for failure |
| 8487 | */ |
| 8488 | int ufshcd_system_suspend(struct ufs_hba *hba) |
| 8489 | { |
| 8490 | int ret = 0; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8491 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8492 | |
| 8493 | if (!hba || !hba->is_powered) |
Dolev Raviv | 233b594 | 2014-10-23 13:25:14 +0300 | [diff] [blame] | 8494 | return 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8495 | |
subhashj@codeaurora.org | 0b25773 | 2016-11-23 16:33:08 -0800 | [diff] [blame] | 8496 | if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) == |
| 8497 | hba->curr_dev_pwr_mode) && |
| 8498 | (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) == |
| 8499 | hba->uic_link_state)) |
| 8500 | goto out; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8501 | |
subhashj@codeaurora.org | 0b25773 | 2016-11-23 16:33:08 -0800 | [diff] [blame] | 8502 | if (pm_runtime_suspended(hba->dev)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8503 | /* |
| 8504 | * UFS device and/or UFS link low power states during runtime |
| 8505 | * suspend seems to be different than what is expected during |
| 8506 | * system suspend. Hence runtime resume the devic & link and |
| 8507 | * let the system suspend low power states to take effect. |
| 8508 | * TODO: If resume takes longer time, we might have optimize |
| 8509 | * it in future by not resuming everything if possible. |
| 8510 | */ |
| 8511 | ret = ufshcd_runtime_resume(hba); |
| 8512 | if (ret) |
| 8513 | goto out; |
| 8514 | } |
| 8515 | |
| 8516 | ret = ufshcd_suspend(hba, UFS_SYSTEM_PM); |
| 8517 | out: |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8518 | trace_ufshcd_system_suspend(dev_name(hba->dev), ret, |
| 8519 | ktime_to_us(ktime_sub(ktime_get(), start)), |
Subhash Jadavani | 73eba2b | 2017-01-10 16:48:25 -0800 | [diff] [blame] | 8520 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
Dolev Raviv | e785060 | 2014-09-25 15:32:36 +0300 | [diff] [blame] | 8521 | if (!ret) |
| 8522 | hba->is_sys_suspended = true; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8523 | return ret; |
| 8524 | } |
| 8525 | EXPORT_SYMBOL(ufshcd_system_suspend); |
| 8526 | |
| 8527 | /** |
| 8528 | * ufshcd_system_resume - system resume routine |
| 8529 | * @hba: per adapter instance |
| 8530 | * |
| 8531 | * Returns 0 for success and non-zero for failure |
| 8532 | */ |
| 8533 | |
| 8534 | int ufshcd_system_resume(struct ufs_hba *hba) |
| 8535 | { |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8536 | int ret = 0; |
| 8537 | ktime_t start = ktime_get(); |
| 8538 | |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 8539 | if (!hba) |
| 8540 | return -EINVAL; |
| 8541 | |
| 8542 | if (!hba->is_powered || pm_runtime_suspended(hba->dev)) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8543 | /* |
| 8544 | * Let the runtime resume take care of resuming |
| 8545 | * if runtime suspended. |
| 8546 | */ |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8547 | goto out; |
| 8548 | else |
| 8549 | ret = ufshcd_resume(hba, UFS_SYSTEM_PM); |
| 8550 | out: |
| 8551 | trace_ufshcd_system_resume(dev_name(hba->dev), ret, |
| 8552 | ktime_to_us(ktime_sub(ktime_get(), start)), |
Subhash Jadavani | 73eba2b | 2017-01-10 16:48:25 -0800 | [diff] [blame] | 8553 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
Stanley Chu | ce9e7bc | 2019-01-07 22:19:34 +0800 | [diff] [blame] | 8554 | if (!ret) |
| 8555 | hba->is_sys_suspended = false; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8556 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8557 | } |
| 8558 | EXPORT_SYMBOL(ufshcd_system_resume); |
| 8559 | |
| 8560 | /** |
| 8561 | * ufshcd_runtime_suspend - runtime suspend routine |
| 8562 | * @hba: per adapter instance |
| 8563 | * |
| 8564 | * Check the description of ufshcd_suspend() function for more details. |
| 8565 | * |
| 8566 | * Returns 0 for success and non-zero for failure |
| 8567 | */ |
| 8568 | int ufshcd_runtime_suspend(struct ufs_hba *hba) |
| 8569 | { |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8570 | int ret = 0; |
| 8571 | ktime_t start = ktime_get(); |
| 8572 | |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 8573 | if (!hba) |
| 8574 | return -EINVAL; |
| 8575 | |
| 8576 | if (!hba->is_powered) |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8577 | goto out; |
| 8578 | else |
| 8579 | ret = ufshcd_suspend(hba, UFS_RUNTIME_PM); |
| 8580 | out: |
| 8581 | trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret, |
| 8582 | ktime_to_us(ktime_sub(ktime_get(), start)), |
Subhash Jadavani | 73eba2b | 2017-01-10 16:48:25 -0800 | [diff] [blame] | 8583 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8584 | return ret; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8585 | } |
| 8586 | EXPORT_SYMBOL(ufshcd_runtime_suspend); |
| 8587 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8588 | /** |
| 8589 | * ufshcd_runtime_resume - runtime resume routine |
| 8590 | * @hba: per adapter instance |
| 8591 | * |
| 8592 | * This function basically brings the UFS device, UniPro link and controller |
| 8593 | * to active state. Following operations are done in this function: |
| 8594 | * |
| 8595 | * 1. Turn on all the controller related clocks |
| 8596 | * 2. Bring the UniPro link out of Hibernate state |
| 8597 | * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device |
| 8598 | * to active state. |
| 8599 | * 4. If auto-bkops is enabled on the device, disable it. |
| 8600 | * |
| 8601 | * So following would be the possible power state after this function return |
| 8602 | * successfully: |
| 8603 | * S1: UFS device in Active state with VCC rail ON |
| 8604 | * UniPro link in Active state |
| 8605 | * All the UFS/UniPro controller clocks are ON |
| 8606 | * |
| 8607 | * Returns 0 for success and non-zero for failure |
| 8608 | */ |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8609 | int ufshcd_runtime_resume(struct ufs_hba *hba) |
| 8610 | { |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8611 | int ret = 0; |
| 8612 | ktime_t start = ktime_get(); |
| 8613 | |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 8614 | if (!hba) |
| 8615 | return -EINVAL; |
| 8616 | |
| 8617 | if (!hba->is_powered) |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8618 | goto out; |
| 8619 | else |
| 8620 | ret = ufshcd_resume(hba, UFS_RUNTIME_PM); |
| 8621 | out: |
| 8622 | trace_ufshcd_runtime_resume(dev_name(hba->dev), ret, |
| 8623 | ktime_to_us(ktime_sub(ktime_get(), start)), |
Subhash Jadavani | 73eba2b | 2017-01-10 16:48:25 -0800 | [diff] [blame] | 8624 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8625 | return ret; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8626 | } |
| 8627 | EXPORT_SYMBOL(ufshcd_runtime_resume); |
| 8628 | |
| 8629 | int ufshcd_runtime_idle(struct ufs_hba *hba) |
| 8630 | { |
| 8631 | return 0; |
| 8632 | } |
| 8633 | EXPORT_SYMBOL(ufshcd_runtime_idle); |
| 8634 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8635 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8636 | * ufshcd_shutdown - shutdown routine |
| 8637 | * @hba: per adapter instance |
| 8638 | * |
| 8639 | * This function would power off both UFS device and UFS link. |
| 8640 | * |
| 8641 | * Returns 0 always to allow force shutdown even in case of errors. |
| 8642 | */ |
| 8643 | int ufshcd_shutdown(struct ufs_hba *hba) |
| 8644 | { |
| 8645 | int ret = 0; |
| 8646 | |
Stanley Chu | f51913e | 2019-09-18 12:20:38 +0800 | [diff] [blame] | 8647 | if (!hba->is_powered) |
| 8648 | goto out; |
| 8649 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8650 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba)) |
| 8651 | goto out; |
| 8652 | |
| 8653 | if (pm_runtime_suspended(hba->dev)) { |
| 8654 | ret = ufshcd_runtime_resume(hba); |
| 8655 | if (ret) |
| 8656 | goto out; |
| 8657 | } |
| 8658 | |
| 8659 | ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM); |
| 8660 | out: |
| 8661 | if (ret) |
| 8662 | dev_err(hba->dev, "%s failed, err %d\n", __func__, ret); |
| 8663 | /* allow force shutdown even in case of errors */ |
| 8664 | return 0; |
| 8665 | } |
| 8666 | EXPORT_SYMBOL(ufshcd_shutdown); |
| 8667 | |
| 8668 | /** |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8669 | * ufshcd_remove - de-allocate SCSI host and host memory space |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8670 | * data structure memory |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 8671 | * @hba: per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8672 | */ |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8673 | void ufshcd_remove(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8674 | { |
Avri Altman | df032bf | 2018-10-07 17:30:35 +0300 | [diff] [blame] | 8675 | ufs_bsg_remove(hba); |
Stanislav Nijnikov | cbb6813 | 2018-02-15 14:14:01 +0200 | [diff] [blame] | 8676 | ufs_sysfs_remove_nodes(hba->dev); |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 8677 | blk_cleanup_queue(hba->tmf_queue); |
| 8678 | blk_mq_free_tag_set(&hba->tmf_tag_set); |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 8679 | blk_cleanup_queue(hba->cmd_queue); |
Akinobu Mita | cfdf9c9 | 2013-07-30 00:36:03 +0530 | [diff] [blame] | 8680 | scsi_remove_host(hba->host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8681 | /* disable interrupts */ |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 8682 | ufshcd_disable_intr(hba, hba->intr_mask); |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 8683 | ufshcd_hba_stop(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8684 | |
Vivek Gautam | eebcc19 | 2018-08-07 23:17:39 +0530 | [diff] [blame] | 8685 | ufshcd_exit_clk_scaling(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8686 | ufshcd_exit_clk_gating(hba); |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 8687 | if (ufshcd_is_clkscaling_supported(hba)) |
| 8688 | device_remove_file(hba->dev, &hba->clk_scaling.enable_attr); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8689 | ufshcd_hba_exit(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8690 | } |
| 8691 | EXPORT_SYMBOL_GPL(ufshcd_remove); |
| 8692 | |
| 8693 | /** |
Yaniv Gardi | 47555a5 | 2015-10-28 13:15:49 +0200 | [diff] [blame] | 8694 | * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA) |
| 8695 | * @hba: pointer to Host Bus Adapter (HBA) |
| 8696 | */ |
| 8697 | void ufshcd_dealloc_host(struct ufs_hba *hba) |
| 8698 | { |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 8699 | ufshcd_crypto_destroy_keyslot_manager(hba); |
Yaniv Gardi | 47555a5 | 2015-10-28 13:15:49 +0200 | [diff] [blame] | 8700 | scsi_host_put(hba->host); |
| 8701 | } |
| 8702 | EXPORT_SYMBOL_GPL(ufshcd_dealloc_host); |
| 8703 | |
| 8704 | /** |
Akinobu Mita | ca3d7bf | 2014-07-13 21:24:46 +0900 | [diff] [blame] | 8705 | * ufshcd_set_dma_mask - Set dma mask based on the controller |
| 8706 | * addressing capability |
| 8707 | * @hba: per adapter instance |
| 8708 | * |
| 8709 | * Returns 0 for success, non-zero for failure |
| 8710 | */ |
| 8711 | static int ufshcd_set_dma_mask(struct ufs_hba *hba) |
| 8712 | { |
| 8713 | if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) { |
| 8714 | if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64))) |
| 8715 | return 0; |
| 8716 | } |
| 8717 | return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32)); |
| 8718 | } |
| 8719 | |
| 8720 | /** |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8721 | * ufshcd_alloc_host - allocate Host Bus Adapter (HBA) |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8722 | * @dev: pointer to device handle |
| 8723 | * @hba_handle: driver private handle |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8724 | * Returns 0 on success, non-zero value on failure |
| 8725 | */ |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8726 | int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8727 | { |
| 8728 | struct Scsi_Host *host; |
| 8729 | struct ufs_hba *hba; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8730 | int err = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8731 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8732 | if (!dev) { |
| 8733 | dev_err(dev, |
| 8734 | "Invalid memory reference for dev is NULL\n"); |
| 8735 | err = -ENODEV; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8736 | goto out_error; |
| 8737 | } |
| 8738 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8739 | host = scsi_host_alloc(&ufshcd_driver_template, |
| 8740 | sizeof(struct ufs_hba)); |
| 8741 | if (!host) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8742 | dev_err(dev, "scsi_host_alloc failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8743 | err = -ENOMEM; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8744 | goto out_error; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8745 | } |
| 8746 | hba = shost_priv(host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8747 | hba->host = host; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8748 | hba->dev = dev; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8749 | *hba_handle = hba; |
Subhash Jadavani | 9e1e8a7 | 2018-10-16 14:29:41 +0530 | [diff] [blame] | 8750 | hba->dev_ref_clk_freq = REF_CLK_FREQ_INVAL; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8751 | |
Szymon Mielczarek | 566ec9a | 2017-06-05 11:36:54 +0300 | [diff] [blame] | 8752 | INIT_LIST_HEAD(&hba->clk_list_head); |
| 8753 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8754 | out_error: |
| 8755 | return err; |
| 8756 | } |
| 8757 | EXPORT_SYMBOL(ufshcd_alloc_host); |
| 8758 | |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 8759 | /* This function exists because blk_mq_alloc_tag_set() requires this. */ |
| 8760 | static blk_status_t ufshcd_queue_tmf(struct blk_mq_hw_ctx *hctx, |
| 8761 | const struct blk_mq_queue_data *qd) |
| 8762 | { |
| 8763 | WARN_ON_ONCE(true); |
| 8764 | return BLK_STS_NOTSUPP; |
| 8765 | } |
| 8766 | |
| 8767 | static const struct blk_mq_ops ufshcd_tmf_ops = { |
| 8768 | .queue_rq = ufshcd_queue_tmf, |
| 8769 | }; |
| 8770 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8771 | /** |
| 8772 | * ufshcd_init - Driver initialization routine |
| 8773 | * @hba: per-adapter instance |
| 8774 | * @mmio_base: base register address |
| 8775 | * @irq: Interrupt line of device |
| 8776 | * Returns 0 on success, non-zero value on failure |
| 8777 | */ |
| 8778 | int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq) |
| 8779 | { |
| 8780 | int err; |
| 8781 | struct Scsi_Host *host = hba->host; |
| 8782 | struct device *dev = hba->dev; |
| 8783 | |
| 8784 | if (!mmio_base) { |
| 8785 | dev_err(hba->dev, |
| 8786 | "Invalid memory reference for mmio_base is NULL\n"); |
| 8787 | err = -ENODEV; |
| 8788 | goto out_error; |
| 8789 | } |
| 8790 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8791 | hba->mmio_base = mmio_base; |
| 8792 | hba->irq = irq; |
Stanley Chu | 90b8491 | 2020-05-09 17:37:13 +0800 | [diff] [blame] | 8793 | hba->vps = &ufs_hba_vps; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8794 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8795 | err = ufshcd_hba_init(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8796 | if (err) |
| 8797 | goto out_error; |
| 8798 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8799 | /* Read capabilities registers */ |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 8800 | err = ufshcd_hba_capabilities(hba); |
| 8801 | if (err) |
| 8802 | goto out_disable; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8803 | |
| 8804 | /* Get UFS version supported by the controller */ |
| 8805 | hba->ufs_version = ufshcd_get_ufs_version(hba); |
| 8806 | |
Yaniv Gardi | c01848c | 2016-12-05 19:25:02 -0800 | [diff] [blame] | 8807 | if ((hba->ufs_version != UFSHCI_VERSION_10) && |
| 8808 | (hba->ufs_version != UFSHCI_VERSION_11) && |
| 8809 | (hba->ufs_version != UFSHCI_VERSION_20) && |
| 8810 | (hba->ufs_version != UFSHCI_VERSION_21)) |
| 8811 | dev_err(hba->dev, "invalid UFS version 0x%x\n", |
| 8812 | hba->ufs_version); |
| 8813 | |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 8814 | /* Get Interrupt bit mask per version */ |
| 8815 | hba->intr_mask = ufshcd_get_intr_mask(hba); |
| 8816 | |
Akinobu Mita | ca3d7bf | 2014-07-13 21:24:46 +0900 | [diff] [blame] | 8817 | err = ufshcd_set_dma_mask(hba); |
| 8818 | if (err) { |
| 8819 | dev_err(hba->dev, "set dma mask failed\n"); |
| 8820 | goto out_disable; |
| 8821 | } |
| 8822 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8823 | /* Allocate memory for host memory space */ |
| 8824 | err = ufshcd_memory_alloc(hba); |
| 8825 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8826 | dev_err(hba->dev, "Memory allocation failed\n"); |
| 8827 | goto out_disable; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8828 | } |
| 8829 | |
| 8830 | /* Configure LRB */ |
| 8831 | ufshcd_host_memory_configure(hba); |
| 8832 | |
| 8833 | host->can_queue = hba->nutrs; |
| 8834 | host->cmd_per_lun = hba->nutrs; |
| 8835 | host->max_id = UFSHCD_MAX_ID; |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 8836 | host->max_lun = UFS_MAX_LUNS; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8837 | host->max_channel = UFSHCD_MAX_CHANNEL; |
| 8838 | host->unique_id = host->host_no; |
Avri Altman | a851b2b | 2018-10-07 17:30:34 +0300 | [diff] [blame] | 8839 | host->max_cmd_len = UFS_CDB_SIZE; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8840 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 8841 | hba->max_pwr_info.is_valid = false; |
| 8842 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8843 | /* Initialize work queues */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 8844 | INIT_WORK(&hba->eh_work, ufshcd_err_handler); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8845 | INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8846 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 8847 | /* Initialize UIC command mutex */ |
| 8848 | mutex_init(&hba->uic_cmd_mutex); |
| 8849 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 8850 | /* Initialize mutex for device management commands */ |
| 8851 | mutex_init(&hba->dev_cmd.lock); |
| 8852 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 8853 | init_rwsem(&hba->clk_scaling_lock); |
| 8854 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8855 | ufshcd_init_clk_gating(hba); |
Yaniv Gardi | 199ef13 | 2016-03-10 17:37:06 +0200 | [diff] [blame] | 8856 | |
Vivek Gautam | eebcc19 | 2018-08-07 23:17:39 +0530 | [diff] [blame] | 8857 | ufshcd_init_clk_scaling(hba); |
| 8858 | |
Yaniv Gardi | 199ef13 | 2016-03-10 17:37:06 +0200 | [diff] [blame] | 8859 | /* |
| 8860 | * In order to avoid any spurious interrupt immediately after |
| 8861 | * registering UFS controller interrupt handler, clear any pending UFS |
| 8862 | * interrupt status and disable all the UFS interrupts. |
| 8863 | */ |
| 8864 | ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS), |
| 8865 | REG_INTERRUPT_STATUS); |
| 8866 | ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE); |
| 8867 | /* |
| 8868 | * Make sure that UFS interrupts are disabled and any pending interrupt |
| 8869 | * status is cleared before registering UFS interrupt handler. |
| 8870 | */ |
| 8871 | mb(); |
| 8872 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8873 | /* IRQ registration */ |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 8874 | err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8875 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8876 | dev_err(hba->dev, "request irq failed\n"); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8877 | goto exit_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8878 | } else { |
| 8879 | hba->is_irq_enabled = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8880 | } |
| 8881 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8882 | err = scsi_add_host(host, hba->dev); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8883 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8884 | dev_err(hba->dev, "scsi_add_host failed\n"); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8885 | goto exit_gating; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8886 | } |
| 8887 | |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 8888 | hba->cmd_queue = blk_mq_init_queue(&hba->host->tag_set); |
| 8889 | if (IS_ERR(hba->cmd_queue)) { |
| 8890 | err = PTR_ERR(hba->cmd_queue); |
| 8891 | goto out_remove_scsi_host; |
| 8892 | } |
| 8893 | |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 8894 | hba->tmf_tag_set = (struct blk_mq_tag_set) { |
| 8895 | .nr_hw_queues = 1, |
| 8896 | .queue_depth = hba->nutmrs, |
| 8897 | .ops = &ufshcd_tmf_ops, |
| 8898 | .flags = BLK_MQ_F_NO_SCHED, |
| 8899 | }; |
| 8900 | err = blk_mq_alloc_tag_set(&hba->tmf_tag_set); |
| 8901 | if (err < 0) |
| 8902 | goto free_cmd_queue; |
| 8903 | hba->tmf_queue = blk_mq_init_queue(&hba->tmf_tag_set); |
| 8904 | if (IS_ERR(hba->tmf_queue)) { |
| 8905 | err = PTR_ERR(hba->tmf_queue); |
| 8906 | goto free_tmf_tag_set; |
| 8907 | } |
| 8908 | |
Bjorn Andersson | d8d9f79 | 2019-08-28 12:17:54 -0700 | [diff] [blame] | 8909 | /* Reset the attached device */ |
| 8910 | ufshcd_vops_device_reset(hba); |
| 8911 | |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 8912 | ufshcd_init_crypto(hba); |
| 8913 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 8914 | /* Host controller enable */ |
| 8915 | err = ufshcd_hba_enable(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8916 | if (err) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 8917 | dev_err(hba->dev, "Host controller enable failed\n"); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 8918 | ufshcd_print_host_regs(hba); |
Gilad Broner | 6ba6558 | 2017-02-03 16:57:28 -0800 | [diff] [blame] | 8919 | ufshcd_print_host_state(hba); |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 8920 | goto free_tmf_queue; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8921 | } |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 8922 | |
subhashj@codeaurora.org | 0c8f758 | 2016-12-22 18:41:11 -0800 | [diff] [blame] | 8923 | /* |
| 8924 | * Set the default power management level for runtime and system PM. |
| 8925 | * Default power saving mode is to keep UFS link in Hibern8 state |
| 8926 | * and UFS device in sleep state. |
| 8927 | */ |
| 8928 | hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state( |
| 8929 | UFS_SLEEP_PWR_MODE, |
| 8930 | UIC_LINK_HIBERN8_STATE); |
| 8931 | hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state( |
| 8932 | UFS_SLEEP_PWR_MODE, |
| 8933 | UIC_LINK_HIBERN8_STATE); |
| 8934 | |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 8935 | INIT_DELAYED_WORK(&hba->rpm_dev_flush_recheck_work, |
| 8936 | ufshcd_rpm_dev_flush_recheck_work); |
| 8937 | |
Adrian Hunter | ad44837 | 2018-03-20 15:07:38 +0200 | [diff] [blame] | 8938 | /* Set the default auto-hiberate idle timer value to 150 ms */ |
Stanley Chu | f571b37 | 2019-05-21 14:44:53 +0800 | [diff] [blame] | 8939 | if (ufshcd_is_auto_hibern8_supported(hba) && !hba->ahit) { |
Adrian Hunter | ad44837 | 2018-03-20 15:07:38 +0200 | [diff] [blame] | 8940 | hba->ahit = FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, 150) | |
| 8941 | FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, 3); |
| 8942 | } |
| 8943 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 8944 | /* Hold auto suspend until async scan completes */ |
| 8945 | pm_runtime_get_sync(dev); |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 8946 | atomic_set(&hba->scsi_block_reqs_cnt, 0); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8947 | /* |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 8948 | * We are assuming that device wasn't put in sleep/power-down |
| 8949 | * state exclusively during the boot stage before kernel. |
| 8950 | * This assumption helps avoid doing link startup twice during |
| 8951 | * ufshcd_probe_hba(). |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8952 | */ |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 8953 | ufshcd_set_ufs_dev_active(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8954 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 8955 | async_schedule(ufshcd_async_scan, hba); |
Stanislav Nijnikov | cbb6813 | 2018-02-15 14:14:01 +0200 | [diff] [blame] | 8956 | ufs_sysfs_add_nodes(hba->dev); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 8957 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8958 | return 0; |
| 8959 | |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 8960 | free_tmf_queue: |
| 8961 | blk_cleanup_queue(hba->tmf_queue); |
| 8962 | free_tmf_tag_set: |
| 8963 | blk_mq_free_tag_set(&hba->tmf_tag_set); |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 8964 | free_cmd_queue: |
| 8965 | blk_cleanup_queue(hba->cmd_queue); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8966 | out_remove_scsi_host: |
| 8967 | scsi_remove_host(hba->host); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8968 | exit_gating: |
Vivek Gautam | eebcc19 | 2018-08-07 23:17:39 +0530 | [diff] [blame] | 8969 | ufshcd_exit_clk_scaling(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8970 | ufshcd_exit_clk_gating(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8971 | out_disable: |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8972 | hba->is_irq_enabled = false; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8973 | ufshcd_hba_exit(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8974 | out_error: |
| 8975 | return err; |
| 8976 | } |
| 8977 | EXPORT_SYMBOL_GPL(ufshcd_init); |
| 8978 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8979 | MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>"); |
| 8980 | MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>"); |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 8981 | MODULE_DESCRIPTION("Generic UFS host controller driver Core"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8982 | MODULE_LICENSE("GPL"); |
| 8983 | MODULE_VERSION(UFSHCD_DRIVER_VERSION); |