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> |
Can Guo | c72e79c | 2020-08-09 05:15:52 -0700 | [diff] [blame] | 18 | #include <linux/blkdev.h> |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 19 | #include "ufshcd.h" |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 20 | #include "ufs_quirks.h" |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 21 | #include "unipro.h" |
Stanislav Nijnikov | cbb6813 | 2018-02-15 14:14:01 +0200 | [diff] [blame] | 22 | #include "ufs-sysfs.h" |
Adrian Hunter | b6cacaf | 2021-01-07 09:25:38 +0200 | [diff] [blame] | 23 | #include "ufs-debugfs.h" |
Avri Altman | df032bf | 2018-10-07 17:30:35 +0300 | [diff] [blame] | 24 | #include "ufs_bsg.h" |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 25 | #include "ufshcd-crypto.h" |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 26 | #include <asm/unaligned.h> |
| 27 | #include <linux/blkdev.h> |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 28 | |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 29 | #define CREATE_TRACE_POINTS |
| 30 | #include <trace/events/ufs.h> |
| 31 | |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 32 | #define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\ |
| 33 | UTP_TASK_REQ_COMPL |\ |
| 34 | UFSHCD_ERROR_MASK) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 35 | /* UIC command timeout, unit: ms */ |
| 36 | #define UIC_CMD_TIMEOUT 500 |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 37 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 38 | /* NOP OUT retries waiting for NOP IN response */ |
| 39 | #define NOP_OUT_RETRIES 10 |
Daejun Park | 782e2ef | 2020-09-02 11:58:52 +0900 | [diff] [blame] | 40 | /* Timeout after 50 msecs if NOP OUT hangs without response */ |
| 41 | #define NOP_OUT_TIMEOUT 50 /* msecs */ |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 42 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 43 | /* Query request retries */ |
subhashj@codeaurora.org | 10fe588 | 2016-11-23 16:31:52 -0800 | [diff] [blame] | 44 | #define QUERY_REQ_RETRIES 3 |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 45 | /* Query request timeout */ |
subhashj@codeaurora.org | 10fe588 | 2016-11-23 16:31:52 -0800 | [diff] [blame] | 46 | #define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */ |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 47 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 48 | /* Task management command timeout */ |
| 49 | #define TM_CMD_TIMEOUT 100 /* msecs */ |
| 50 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 51 | /* maximum number of retries for a general UIC command */ |
| 52 | #define UFS_UIC_COMMAND_RETRIES 3 |
| 53 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 54 | /* maximum number of link-startup retries */ |
| 55 | #define DME_LINKSTARTUP_RETRIES 3 |
| 56 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 57 | /* Maximum retries for Hibern8 enter */ |
| 58 | #define UIC_HIBERN8_ENTER_RETRIES 3 |
| 59 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 60 | /* maximum number of reset retries before giving up */ |
| 61 | #define MAX_HOST_RESET_RETRIES 5 |
| 62 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 63 | /* Expose the flag value from utp_upiu_query.value */ |
| 64 | #define MASK_QUERY_UPIU_FLAG_LOC 0xFF |
| 65 | |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 66 | /* Interrupt aggregation default timeout, unit: 40us */ |
| 67 | #define INT_AGGR_DEF_TO 0x02 |
| 68 | |
Stanley Chu | 49615ba | 2019-09-16 23:56:50 +0800 | [diff] [blame] | 69 | /* default delay of autosuspend: 2000 ms */ |
| 70 | #define RPM_AUTOSUSPEND_DELAY_MS 2000 |
| 71 | |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 72 | /* Default delay of RPM device flush delayed work */ |
| 73 | #define RPM_DEV_FLUSH_RECHECK_WORK_DELAY_MS 5000 |
| 74 | |
Can Guo | 09f1779 | 2020-02-10 19:40:49 -0800 | [diff] [blame] | 75 | /* Default value of wait time before gating device ref clock */ |
| 76 | #define UFSHCD_REF_CLK_GATING_WAIT_US 0xFF /* microsecs */ |
| 77 | |
Kiwoong Kim | 29707fa | 2020-08-10 19:02:27 +0900 | [diff] [blame] | 78 | /* Polling time to wait for fDeviceInit */ |
| 79 | #define FDEVICEINIT_COMPL_TIMEOUT 1500 /* millisecs */ |
| 80 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 81 | #define ufshcd_toggle_vreg(_dev, _vreg, _on) \ |
| 82 | ({ \ |
| 83 | int _ret; \ |
| 84 | if (_on) \ |
| 85 | _ret = ufshcd_enable_vreg(_dev, _vreg); \ |
| 86 | else \ |
| 87 | _ret = ufshcd_disable_vreg(_dev, _vreg); \ |
| 88 | _ret; \ |
| 89 | }) |
| 90 | |
Tomas Winkler | ba80917 | 2018-06-14 11:14:09 +0300 | [diff] [blame] | 91 | #define ufshcd_hex_dump(prefix_str, buf, len) do { \ |
| 92 | size_t __len = (len); \ |
| 93 | print_hex_dump(KERN_ERR, prefix_str, \ |
| 94 | __len > 4 ? DUMP_PREFIX_OFFSET : DUMP_PREFIX_NONE,\ |
| 95 | 16, 4, buf, __len, false); \ |
| 96 | } while (0) |
| 97 | |
Can Guo | fb7afe2 | 2021-01-13 19:13:27 -0800 | [diff] [blame] | 98 | static bool early_suspend; |
| 99 | |
Tomas Winkler | ba80917 | 2018-06-14 11:14:09 +0300 | [diff] [blame] | 100 | int ufshcd_dump_regs(struct ufs_hba *hba, size_t offset, size_t len, |
| 101 | const char *prefix) |
| 102 | { |
Marc Gonzalez | d672475 | 2019-01-22 18:29:22 +0100 | [diff] [blame] | 103 | u32 *regs; |
| 104 | size_t pos; |
| 105 | |
| 106 | if (offset % 4 != 0 || len % 4 != 0) /* keep readl happy */ |
| 107 | return -EINVAL; |
Tomas Winkler | ba80917 | 2018-06-14 11:14:09 +0300 | [diff] [blame] | 108 | |
Can Guo | cddaeba | 2019-11-14 22:09:27 -0800 | [diff] [blame] | 109 | regs = kzalloc(len, GFP_ATOMIC); |
Tomas Winkler | ba80917 | 2018-06-14 11:14:09 +0300 | [diff] [blame] | 110 | if (!regs) |
| 111 | return -ENOMEM; |
| 112 | |
Marc Gonzalez | d672475 | 2019-01-22 18:29:22 +0100 | [diff] [blame] | 113 | for (pos = 0; pos < len; pos += 4) |
| 114 | regs[pos / 4] = ufshcd_readl(hba, offset + pos); |
| 115 | |
Tomas Winkler | ba80917 | 2018-06-14 11:14:09 +0300 | [diff] [blame] | 116 | ufshcd_hex_dump(prefix, regs, len); |
| 117 | kfree(regs); |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | EXPORT_SYMBOL_GPL(ufshcd_dump_regs); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 122 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 123 | enum { |
| 124 | UFSHCD_MAX_CHANNEL = 0, |
| 125 | UFSHCD_MAX_ID = 1, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 126 | UFSHCD_CMD_PER_LUN = 32, |
| 127 | UFSHCD_CAN_QUEUE = 32, |
| 128 | }; |
| 129 | |
| 130 | /* UFSHCD states */ |
| 131 | enum { |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 132 | UFSHCD_STATE_RESET, |
| 133 | UFSHCD_STATE_ERROR, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 134 | UFSHCD_STATE_OPERATIONAL, |
Can Guo | 5586dd8 | 2020-08-09 05:15:54 -0700 | [diff] [blame] | 135 | UFSHCD_STATE_EH_SCHEDULED_FATAL, |
| 136 | UFSHCD_STATE_EH_SCHEDULED_NON_FATAL, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | /* UFSHCD error handling flags */ |
| 140 | enum { |
| 141 | UFSHCD_EH_IN_PROGRESS = (1 << 0), |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 142 | }; |
| 143 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 144 | /* UFSHCD UIC layer error flags */ |
| 145 | enum { |
| 146 | UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 147 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */ |
| 148 | UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */ |
| 149 | UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */ |
| 150 | UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */ |
| 151 | UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */ |
Can Guo | 2355b66 | 2020-08-24 19:07:06 -0700 | [diff] [blame] | 152 | UFSHCD_UIC_PA_GENERIC_ERROR = (1 << 6), /* Generic PA error */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 153 | }; |
| 154 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 155 | #define ufshcd_set_eh_in_progress(h) \ |
Tomohiro Kusumi | 9c490d2 | 2017-03-28 16:49:26 +0300 | [diff] [blame] | 156 | ((h)->eh_flags |= UFSHCD_EH_IN_PROGRESS) |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 157 | #define ufshcd_eh_in_progress(h) \ |
Tomohiro Kusumi | 9c490d2 | 2017-03-28 16:49:26 +0300 | [diff] [blame] | 158 | ((h)->eh_flags & UFSHCD_EH_IN_PROGRESS) |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 159 | #define ufshcd_clear_eh_in_progress(h) \ |
Tomohiro Kusumi | 9c490d2 | 2017-03-28 16:49:26 +0300 | [diff] [blame] | 160 | ((h)->eh_flags &= ~UFSHCD_EH_IN_PROGRESS) |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 161 | |
Stanislav Nijnikov | cbb6813 | 2018-02-15 14:14:01 +0200 | [diff] [blame] | 162 | struct ufs_pm_lvl_states ufs_pm_lvl_states[] = { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 163 | {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE}, |
| 164 | {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 165 | {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE}, |
| 166 | {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 167 | {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 168 | {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE}, |
Adrian Hunter | fe1d4c2 | 2020-11-03 16:14:02 +0200 | [diff] [blame] | 169 | /* |
| 170 | * For DeepSleep, the link is first put in hibern8 and then off. |
| 171 | * Leaving the link in hibern8 is not supported. |
| 172 | */ |
| 173 | {UFS_DEEPSLEEP_PWR_MODE, UIC_LINK_OFF_STATE}, |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | static inline enum ufs_dev_pwr_mode |
| 177 | ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl) |
| 178 | { |
| 179 | return ufs_pm_lvl_states[lvl].dev_state; |
| 180 | } |
| 181 | |
| 182 | static inline enum uic_link_state |
| 183 | ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl) |
| 184 | { |
| 185 | return ufs_pm_lvl_states[lvl].link_state; |
| 186 | } |
| 187 | |
subhashj@codeaurora.org | 0c8f758 | 2016-12-22 18:41:11 -0800 | [diff] [blame] | 188 | static inline enum ufs_pm_level |
| 189 | ufs_get_desired_pm_lvl_for_dev_link_state(enum ufs_dev_pwr_mode dev_state, |
| 190 | enum uic_link_state link_state) |
| 191 | { |
| 192 | enum ufs_pm_level lvl; |
| 193 | |
| 194 | for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) { |
| 195 | if ((ufs_pm_lvl_states[lvl].dev_state == dev_state) && |
| 196 | (ufs_pm_lvl_states[lvl].link_state == link_state)) |
| 197 | return lvl; |
| 198 | } |
| 199 | |
| 200 | /* if no match found, return the level 0 */ |
| 201 | return UFS_PM_LVL_0; |
| 202 | } |
| 203 | |
Subhash Jadavani | 56d4a18 | 2016-12-05 19:25:32 -0800 | [diff] [blame] | 204 | static struct ufs_dev_fix ufs_fixups[] = { |
| 205 | /* UFS cards deviations table */ |
Stanley Chu | c0a18ee | 2020-06-12 09:26:24 +0800 | [diff] [blame] | 206 | UFS_FIX(UFS_VENDOR_MICRON, UFS_ANY_MODEL, |
| 207 | UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM), |
Subhash Jadavani | 56d4a18 | 2016-12-05 19:25:32 -0800 | [diff] [blame] | 208 | UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, |
Stanley Chu | ed0b40f | 2020-06-12 09:26:25 +0800 | [diff] [blame] | 209 | UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM | |
| 210 | UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE | |
Subhash Jadavani | 56d4a18 | 2016-12-05 19:25:32 -0800 | [diff] [blame] | 211 | UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS), |
Stanley Chu | ed0b40f | 2020-06-12 09:26:25 +0800 | [diff] [blame] | 212 | UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL, |
| 213 | UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME), |
| 214 | UFS_FIX(UFS_VENDOR_SKHYNIX, "hB8aL1" /*H28U62301AMR*/, |
| 215 | UFS_DEVICE_QUIRK_HOST_VS_DEBUGSAVECONFIGTIME), |
Subhash Jadavani | 56d4a18 | 2016-12-05 19:25:32 -0800 | [diff] [blame] | 216 | UFS_FIX(UFS_VENDOR_TOSHIBA, UFS_ANY_MODEL, |
| 217 | UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM), |
| 218 | UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9C8KBADG", |
| 219 | UFS_DEVICE_QUIRK_PA_TACTIVATE), |
| 220 | UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9D8KBADG", |
| 221 | UFS_DEVICE_QUIRK_PA_TACTIVATE), |
Subhash Jadavani | 56d4a18 | 2016-12-05 19:25:32 -0800 | [diff] [blame] | 222 | END_FIX |
| 223 | }; |
| 224 | |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 225 | static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 226 | static void ufshcd_async_scan(void *data, async_cookie_t cookie); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 227 | static int ufshcd_reset_and_restore(struct ufs_hba *hba); |
Dolev Raviv | e7d3825 | 2016-12-22 18:40:07 -0800 | [diff] [blame] | 228 | static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 229 | 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] | 230 | static void ufshcd_hba_exit(struct ufs_hba *hba); |
Randall Huang | 1918651 | 2020-11-30 20:14:02 -0800 | [diff] [blame] | 231 | static int ufshcd_clear_ua_wluns(struct ufs_hba *hba); |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 232 | static int ufshcd_probe_hba(struct ufs_hba *hba, bool async); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 233 | static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 234 | static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 235 | 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] | 236 | static int ufshcd_host_reset_and_restore(struct ufs_hba *hba); |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 237 | static void ufshcd_resume_clkscaling(struct ufs_hba *hba); |
| 238 | static void ufshcd_suspend_clkscaling(struct ufs_hba *hba); |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 239 | static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba); |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 240 | static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 241 | static irqreturn_t ufshcd_intr(int irq, void *__hba); |
Yaniv Gardi | 874237f | 2015-05-17 18:55:03 +0300 | [diff] [blame] | 242 | static int ufshcd_change_power_mode(struct ufs_hba *hba, |
| 243 | struct ufs_pa_layer_attr *pwr_mode); |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 244 | static void ufshcd_schedule_eh_work(struct ufs_hba *hba); |
Can Guo | c72e79c | 2020-08-09 05:15:52 -0700 | [diff] [blame] | 245 | static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on); |
| 246 | static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on); |
| 247 | static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba, |
| 248 | struct ufs_vreg *vreg); |
Can Guo | 307348f | 2020-08-24 19:07:05 -0700 | [diff] [blame] | 249 | static int ufshcd_try_to_abort_task(struct ufs_hba *hba, int tag); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 250 | static int ufshcd_wb_buf_flush_enable(struct ufs_hba *hba); |
| 251 | static int ufshcd_wb_buf_flush_disable(struct ufs_hba *hba); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 252 | static int ufshcd_wb_toggle_flush_during_h8(struct ufs_hba *hba, bool set); |
| 253 | static inline void ufshcd_wb_toggle_flush(struct ufs_hba *hba, bool enable); |
Can Guo | dd7143e | 2020-10-27 12:10:36 -0700 | [diff] [blame] | 254 | static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba); |
| 255 | static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 256 | |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 257 | static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag) |
| 258 | { |
| 259 | return tag >= 0 && tag < hba->nutrs; |
| 260 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 261 | |
Can Guo | 5231d38 | 2019-12-05 02:14:46 +0000 | [diff] [blame] | 262 | static inline void ufshcd_enable_irq(struct ufs_hba *hba) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 263 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 264 | if (!hba->is_irq_enabled) { |
Can Guo | 5231d38 | 2019-12-05 02:14:46 +0000 | [diff] [blame] | 265 | enable_irq(hba->irq); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 266 | hba->is_irq_enabled = true; |
| 267 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static inline void ufshcd_disable_irq(struct ufs_hba *hba) |
| 271 | { |
| 272 | if (hba->is_irq_enabled) { |
Can Guo | 5231d38 | 2019-12-05 02:14:46 +0000 | [diff] [blame] | 273 | disable_irq(hba->irq); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 274 | hba->is_irq_enabled = false; |
| 275 | } |
| 276 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 277 | |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 278 | static inline void ufshcd_wb_config(struct ufs_hba *hba) |
| 279 | { |
| 280 | int ret; |
| 281 | |
Stanley Chu | 79e3520 | 2020-05-08 16:01:15 +0800 | [diff] [blame] | 282 | if (!ufshcd_is_wb_allowed(hba)) |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 283 | return; |
| 284 | |
| 285 | ret = ufshcd_wb_ctrl(hba, true); |
| 286 | if (ret) |
| 287 | dev_err(hba->dev, "%s: Enable WB failed: %d\n", __func__, ret); |
| 288 | else |
| 289 | dev_info(hba->dev, "%s: Write Booster Configured\n", __func__); |
| 290 | ret = ufshcd_wb_toggle_flush_during_h8(hba, true); |
| 291 | if (ret) |
| 292 | dev_err(hba->dev, "%s: En WB flush during H8: failed: %d\n", |
| 293 | __func__, ret); |
Stanley Chu | 21acf46 | 2020-12-22 15:29:05 +0800 | [diff] [blame] | 294 | if (!(hba->quirks & UFSHCI_QUIRK_SKIP_MANUAL_WB_FLUSH_CTRL)) |
| 295 | ufshcd_wb_toggle_flush(hba, true); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 298 | static void ufshcd_scsi_unblock_requests(struct ufs_hba *hba) |
| 299 | { |
| 300 | if (atomic_dec_and_test(&hba->scsi_block_reqs_cnt)) |
| 301 | scsi_unblock_requests(hba->host); |
| 302 | } |
| 303 | |
| 304 | static void ufshcd_scsi_block_requests(struct ufs_hba *hba) |
| 305 | { |
| 306 | if (atomic_inc_return(&hba->scsi_block_reqs_cnt) == 1) |
| 307 | scsi_block_requests(hba->host); |
| 308 | } |
| 309 | |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 310 | static void ufshcd_add_cmd_upiu_trace(struct ufs_hba *hba, unsigned int tag, |
Bean Huo | 28fa68f | 2021-01-05 12:34:42 +0100 | [diff] [blame] | 311 | enum ufs_trace_str_t str_t) |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 312 | { |
| 313 | struct utp_upiu_req *rq = hba->lrb[tag].ucd_req_ptr; |
| 314 | |
Bean Huo | 9d5095e | 2021-01-05 12:34:43 +0100 | [diff] [blame] | 315 | if (!trace_ufshcd_upiu_enabled()) |
| 316 | return; |
| 317 | |
Bean Huo | 867fdc2 | 2021-01-05 12:34:46 +0100 | [diff] [blame] | 318 | trace_ufshcd_upiu(dev_name(hba->dev), str_t, &rq->header, &rq->sc.cdb, |
| 319 | UFS_TSF_CDB); |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 320 | } |
| 321 | |
Avri Altman | fb475b7 | 2021-01-10 10:46:18 +0200 | [diff] [blame] | 322 | static void ufshcd_add_query_upiu_trace(struct ufs_hba *hba, |
| 323 | enum ufs_trace_str_t str_t, |
| 324 | struct utp_upiu_req *rq_rsp) |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 325 | { |
Bean Huo | 9d5095e | 2021-01-05 12:34:43 +0100 | [diff] [blame] | 326 | if (!trace_ufshcd_upiu_enabled()) |
| 327 | return; |
| 328 | |
Bean Huo | be20b51 | 2021-01-05 12:34:44 +0100 | [diff] [blame] | 329 | trace_ufshcd_upiu(dev_name(hba->dev), str_t, &rq_rsp->header, |
Bean Huo | 867fdc2 | 2021-01-05 12:34:46 +0100 | [diff] [blame] | 330 | &rq_rsp->qr, UFS_TSF_OSF); |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | static void ufshcd_add_tm_upiu_trace(struct ufs_hba *hba, unsigned int tag, |
Bean Huo | 28fa68f | 2021-01-05 12:34:42 +0100 | [diff] [blame] | 334 | enum ufs_trace_str_t str_t) |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 335 | { |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 336 | int off = (int)tag - hba->nutrs; |
Christoph Hellwig | 391e388 | 2018-10-07 17:30:32 +0300 | [diff] [blame] | 337 | struct utp_task_req_desc *descp = &hba->utmrdl_base_addr[off]; |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 338 | |
Bean Huo | 9d5095e | 2021-01-05 12:34:43 +0100 | [diff] [blame] | 339 | if (!trace_ufshcd_upiu_enabled()) |
| 340 | return; |
| 341 | |
Bean Huo | 0ed083e | 2021-01-05 12:34:45 +0100 | [diff] [blame] | 342 | if (str_t == UFS_TM_SEND) |
| 343 | trace_ufshcd_upiu(dev_name(hba->dev), str_t, &descp->req_header, |
Bean Huo | 867fdc2 | 2021-01-05 12:34:46 +0100 | [diff] [blame] | 344 | &descp->input_param1, UFS_TSF_TM_INPUT); |
Bean Huo | 0ed083e | 2021-01-05 12:34:45 +0100 | [diff] [blame] | 345 | else |
| 346 | trace_ufshcd_upiu(dev_name(hba->dev), str_t, &descp->rsp_header, |
Bean Huo | 867fdc2 | 2021-01-05 12:34:46 +0100 | [diff] [blame] | 347 | &descp->output_param1, UFS_TSF_TM_OUTPUT); |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 348 | } |
| 349 | |
Stanley Chu | aa5c697 | 2020-06-15 15:22:35 +0800 | [diff] [blame] | 350 | static void ufshcd_add_uic_command_trace(struct ufs_hba *hba, |
| 351 | struct uic_command *ucmd, |
Bean Huo | 28fa68f | 2021-01-05 12:34:42 +0100 | [diff] [blame] | 352 | enum ufs_trace_str_t str_t) |
Stanley Chu | aa5c697 | 2020-06-15 15:22:35 +0800 | [diff] [blame] | 353 | { |
| 354 | u32 cmd; |
| 355 | |
| 356 | if (!trace_ufshcd_uic_command_enabled()) |
| 357 | return; |
| 358 | |
Bean Huo | 28fa68f | 2021-01-05 12:34:42 +0100 | [diff] [blame] | 359 | if (str_t == UFS_CMD_SEND) |
Stanley Chu | aa5c697 | 2020-06-15 15:22:35 +0800 | [diff] [blame] | 360 | cmd = ucmd->command; |
| 361 | else |
| 362 | cmd = ufshcd_readl(hba, REG_UIC_COMMAND); |
| 363 | |
Bean Huo | 28fa68f | 2021-01-05 12:34:42 +0100 | [diff] [blame] | 364 | trace_ufshcd_uic_command(dev_name(hba->dev), str_t, cmd, |
Stanley Chu | aa5c697 | 2020-06-15 15:22:35 +0800 | [diff] [blame] | 365 | ufshcd_readl(hba, REG_UIC_COMMAND_ARG_1), |
| 366 | ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2), |
| 367 | ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3)); |
| 368 | } |
| 369 | |
Bean Huo | 28fa68f | 2021-01-05 12:34:42 +0100 | [diff] [blame] | 370 | static void ufshcd_add_command_trace(struct ufs_hba *hba, unsigned int tag, |
| 371 | enum ufs_trace_str_t str_t) |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 372 | { |
| 373 | sector_t lba = -1; |
Jaegeuk Kim | 69a314d | 2020-11-17 08:58:37 -0800 | [diff] [blame] | 374 | u8 opcode = 0, group_id = 0; |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 375 | u32 intr, doorbell; |
Ohad Sharabi | e7c3b37 | 2018-08-05 16:26:23 +0300 | [diff] [blame] | 376 | struct ufshcd_lrb *lrbp = &hba->lrb[tag]; |
Bart Van Assche | e4d2add | 2019-12-24 14:02:44 -0800 | [diff] [blame] | 377 | struct scsi_cmnd *cmd = lrbp->cmd; |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 378 | int transfer_len = -1; |
| 379 | |
Ohad Sharabi | e7c3b37 | 2018-08-05 16:26:23 +0300 | [diff] [blame] | 380 | if (!trace_ufshcd_command_enabled()) { |
| 381 | /* trace UPIU W/O tracing command */ |
Bart Van Assche | e4d2add | 2019-12-24 14:02:44 -0800 | [diff] [blame] | 382 | if (cmd) |
Bean Huo | 28fa68f | 2021-01-05 12:34:42 +0100 | [diff] [blame] | 383 | ufshcd_add_cmd_upiu_trace(hba, tag, str_t); |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 384 | return; |
Ohad Sharabi | e7c3b37 | 2018-08-05 16:26:23 +0300 | [diff] [blame] | 385 | } |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 386 | |
Bart Van Assche | e4d2add | 2019-12-24 14:02:44 -0800 | [diff] [blame] | 387 | if (cmd) { /* data phase exists */ |
Ohad Sharabi | e7c3b37 | 2018-08-05 16:26:23 +0300 | [diff] [blame] | 388 | /* trace UPIU also */ |
Bean Huo | 28fa68f | 2021-01-05 12:34:42 +0100 | [diff] [blame] | 389 | ufshcd_add_cmd_upiu_trace(hba, tag, str_t); |
Bart Van Assche | e4d2add | 2019-12-24 14:02:44 -0800 | [diff] [blame] | 390 | opcode = cmd->cmnd[0]; |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 391 | if ((opcode == READ_10) || (opcode == WRITE_10)) { |
| 392 | /* |
| 393 | * Currently we only fully trace read(10) and write(10) |
| 394 | * commands |
| 395 | */ |
Bart Van Assche | e4d2add | 2019-12-24 14:02:44 -0800 | [diff] [blame] | 396 | if (cmd->request && cmd->request->bio) |
| 397 | lba = cmd->request->bio->bi_iter.bi_sector; |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 398 | transfer_len = be32_to_cpu( |
| 399 | lrbp->ucd_req_ptr->sc.exp_data_transfer_len); |
Jaegeuk Kim | 69a314d | 2020-11-17 08:58:37 -0800 | [diff] [blame] | 400 | if (opcode == WRITE_10) |
| 401 | group_id = lrbp->cmd->cmnd[6]; |
Leo Liou | 3754cde | 2020-11-17 08:58:39 -0800 | [diff] [blame] | 402 | } else if (opcode == UNMAP) { |
| 403 | if (cmd->request) { |
| 404 | lba = scsi_get_lba(cmd); |
| 405 | transfer_len = blk_rq_bytes(cmd->request); |
| 406 | } |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
| 410 | intr = ufshcd_readl(hba, REG_INTERRUPT_STATUS); |
| 411 | doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
Bean Huo | 28fa68f | 2021-01-05 12:34:42 +0100 | [diff] [blame] | 412 | trace_ufshcd_command(dev_name(hba->dev), str_t, tag, |
Jaegeuk Kim | 69a314d | 2020-11-17 08:58:37 -0800 | [diff] [blame] | 413 | doorbell, transfer_len, intr, lba, opcode, group_id); |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 414 | } |
| 415 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 416 | static void ufshcd_print_clk_freqs(struct ufs_hba *hba) |
| 417 | { |
| 418 | struct ufs_clk_info *clki; |
| 419 | struct list_head *head = &hba->clk_list_head; |
| 420 | |
Szymon Mielczarek | 566ec9a | 2017-06-05 11:36:54 +0300 | [diff] [blame] | 421 | if (list_empty(head)) |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 422 | return; |
| 423 | |
| 424 | list_for_each_entry(clki, head, list) { |
| 425 | if (!IS_ERR_OR_NULL(clki->clk) && clki->min_freq && |
| 426 | clki->max_freq) |
| 427 | dev_err(hba->dev, "clk: %s, rate: %u\n", |
| 428 | clki->name, clki->curr_freq); |
| 429 | } |
| 430 | } |
| 431 | |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 432 | static void ufshcd_print_evt(struct ufs_hba *hba, u32 id, |
| 433 | char *err_name) |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 434 | { |
| 435 | int i; |
Stanley Chu | 2775264 | 2019-01-28 22:04:26 +0800 | [diff] [blame] | 436 | bool found = false; |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 437 | struct ufs_event_hist *e; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 438 | |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 439 | if (id >= UFS_EVT_CNT) |
| 440 | return; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 441 | |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 442 | e = &hba->ufs_stats.event[id]; |
| 443 | |
| 444 | for (i = 0; i < UFS_EVENT_HIST_LENGTH; i++) { |
| 445 | int p = (i + e->pos) % UFS_EVENT_HIST_LENGTH; |
| 446 | |
| 447 | if (e->tstamp[p] == 0) |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 448 | continue; |
Stanley Chu | c5397f1 | 2019-07-10 21:38:20 +0800 | [diff] [blame] | 449 | dev_err(hba->dev, "%s[%d] = 0x%x at %lld us\n", err_name, p, |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 450 | e->val[p], ktime_to_us(e->tstamp[p])); |
Stanley Chu | 2775264 | 2019-01-28 22:04:26 +0800 | [diff] [blame] | 451 | found = true; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 452 | } |
Stanley Chu | 2775264 | 2019-01-28 22:04:26 +0800 | [diff] [blame] | 453 | |
| 454 | if (!found) |
Stanley Chu | fd1fb4d | 2020-01-04 22:26:08 +0800 | [diff] [blame] | 455 | dev_err(hba->dev, "No record of %s\n", err_name); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 456 | } |
| 457 | |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 458 | static void ufshcd_print_evt_hist(struct ufs_hba *hba) |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 459 | { |
Tomas Winkler | ba80917 | 2018-06-14 11:14:09 +0300 | [diff] [blame] | 460 | ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: "); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 461 | |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 462 | ufshcd_print_evt(hba, UFS_EVT_PA_ERR, "pa_err"); |
| 463 | ufshcd_print_evt(hba, UFS_EVT_DL_ERR, "dl_err"); |
| 464 | ufshcd_print_evt(hba, UFS_EVT_NL_ERR, "nl_err"); |
| 465 | ufshcd_print_evt(hba, UFS_EVT_TL_ERR, "tl_err"); |
| 466 | ufshcd_print_evt(hba, UFS_EVT_DME_ERR, "dme_err"); |
| 467 | ufshcd_print_evt(hba, UFS_EVT_AUTO_HIBERN8_ERR, |
| 468 | "auto_hibern8_err"); |
| 469 | ufshcd_print_evt(hba, UFS_EVT_FATAL_ERR, "fatal_err"); |
| 470 | ufshcd_print_evt(hba, UFS_EVT_LINK_STARTUP_FAIL, |
| 471 | "link_startup_fail"); |
| 472 | ufshcd_print_evt(hba, UFS_EVT_RESUME_ERR, "resume_fail"); |
| 473 | ufshcd_print_evt(hba, UFS_EVT_SUSPEND_ERR, |
| 474 | "suspend_fail"); |
| 475 | ufshcd_print_evt(hba, UFS_EVT_DEV_RESET, "dev_reset"); |
| 476 | ufshcd_print_evt(hba, UFS_EVT_HOST_RESET, "host_reset"); |
| 477 | ufshcd_print_evt(hba, UFS_EVT_ABORT, "task_abort"); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 478 | |
Stanley Chu | 7c486d91 | 2019-12-24 21:01:06 +0800 | [diff] [blame] | 479 | ufshcd_vops_dbg_register_dump(hba); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | static |
| 483 | void ufshcd_print_trs(struct ufs_hba *hba, unsigned long bitmap, bool pr_prdt) |
| 484 | { |
| 485 | struct ufshcd_lrb *lrbp; |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 486 | int prdt_length; |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 487 | int tag; |
| 488 | |
| 489 | for_each_set_bit(tag, &bitmap, hba->nutrs) { |
| 490 | lrbp = &hba->lrb[tag]; |
| 491 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 492 | dev_err(hba->dev, "UPIU[%d] - issue time %lld us\n", |
| 493 | tag, ktime_to_us(lrbp->issue_time_stamp)); |
Zang Leigang | 0901718 | 2017-09-27 10:06:06 +0800 | [diff] [blame] | 494 | dev_err(hba->dev, "UPIU[%d] - complete time %lld us\n", |
| 495 | tag, ktime_to_us(lrbp->compl_time_stamp)); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 496 | dev_err(hba->dev, |
| 497 | "UPIU[%d] - Transfer Request Descriptor phys@0x%llx\n", |
| 498 | tag, (u64)lrbp->utrd_dma_addr); |
| 499 | |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 500 | ufshcd_hex_dump("UPIU TRD: ", lrbp->utr_descriptor_ptr, |
| 501 | sizeof(struct utp_transfer_req_desc)); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 502 | dev_err(hba->dev, "UPIU[%d] - Request UPIU phys@0x%llx\n", tag, |
| 503 | (u64)lrbp->ucd_req_dma_addr); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 504 | ufshcd_hex_dump("UPIU REQ: ", lrbp->ucd_req_ptr, |
| 505 | sizeof(struct utp_upiu_req)); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 506 | dev_err(hba->dev, "UPIU[%d] - Response UPIU phys@0x%llx\n", tag, |
| 507 | (u64)lrbp->ucd_rsp_dma_addr); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 508 | ufshcd_hex_dump("UPIU RSP: ", lrbp->ucd_rsp_ptr, |
| 509 | sizeof(struct utp_upiu_rsp)); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 510 | |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 511 | prdt_length = le16_to_cpu( |
| 512 | lrbp->utr_descriptor_ptr->prd_table_length); |
Eric Biggers | cc770ce | 2020-08-25 19:10:40 -0700 | [diff] [blame] | 513 | if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) |
| 514 | prdt_length /= sizeof(struct ufshcd_sg_entry); |
| 515 | |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 516 | dev_err(hba->dev, |
| 517 | "UPIU[%d] - PRDT - %d entries phys@0x%llx\n", |
| 518 | tag, prdt_length, |
| 519 | (u64)lrbp->ucd_prdt_dma_addr); |
| 520 | |
| 521 | if (pr_prdt) |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 522 | ufshcd_hex_dump("UPIU PRDT: ", lrbp->ucd_prdt_ptr, |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 523 | sizeof(struct ufshcd_sg_entry) * prdt_length); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
| 527 | static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap) |
| 528 | { |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 529 | int tag; |
| 530 | |
| 531 | for_each_set_bit(tag, &bitmap, hba->nutmrs) { |
Christoph Hellwig | 391e388 | 2018-10-07 17:30:32 +0300 | [diff] [blame] | 532 | struct utp_task_req_desc *tmrdp = &hba->utmrdl_base_addr[tag]; |
| 533 | |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 534 | dev_err(hba->dev, "TM[%d] - Task Management Header\n", tag); |
Christoph Hellwig | 391e388 | 2018-10-07 17:30:32 +0300 | [diff] [blame] | 535 | ufshcd_hex_dump("", tmrdp, sizeof(*tmrdp)); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
Gilad Broner | 6ba6558 | 2017-02-03 16:57:28 -0800 | [diff] [blame] | 539 | static void ufshcd_print_host_state(struct ufs_hba *hba) |
| 540 | { |
Can Guo | 3f8af60 | 2020-08-09 05:15:50 -0700 | [diff] [blame] | 541 | struct scsi_device *sdev_ufs = hba->sdev_ufs_device; |
| 542 | |
Gilad Broner | 6ba6558 | 2017-02-03 16:57:28 -0800 | [diff] [blame] | 543 | 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] | 544 | dev_err(hba->dev, "outstanding reqs=0x%lx tasks=0x%lx\n", |
| 545 | hba->outstanding_reqs, hba->outstanding_tasks); |
Gilad Broner | 6ba6558 | 2017-02-03 16:57:28 -0800 | [diff] [blame] | 546 | dev_err(hba->dev, "saved_err=0x%x, saved_uic_err=0x%x\n", |
| 547 | hba->saved_err, hba->saved_uic_err); |
| 548 | dev_err(hba->dev, "Device power mode=%d, UIC link state=%d\n", |
| 549 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
| 550 | dev_err(hba->dev, "PM in progress=%d, sys. suspended=%d\n", |
| 551 | hba->pm_op_in_progress, hba->is_sys_suspended); |
| 552 | dev_err(hba->dev, "Auto BKOPS=%d, Host self-block=%d\n", |
| 553 | hba->auto_bkops_enabled, hba->host->host_self_blocked); |
| 554 | dev_err(hba->dev, "Clk gate=%d\n", hba->clk_gating.state); |
Can Guo | 3f8af60 | 2020-08-09 05:15:50 -0700 | [diff] [blame] | 555 | dev_err(hba->dev, |
| 556 | "last_hibern8_exit_tstamp at %lld us, hibern8_exit_cnt=%d\n", |
| 557 | ktime_to_us(hba->ufs_stats.last_hibern8_exit_tstamp), |
| 558 | hba->ufs_stats.hibern8_exit_cnt); |
| 559 | dev_err(hba->dev, "last intr at %lld us, last intr status=0x%x\n", |
| 560 | ktime_to_us(hba->ufs_stats.last_intr_ts), |
| 561 | hba->ufs_stats.last_intr_status); |
Gilad Broner | 6ba6558 | 2017-02-03 16:57:28 -0800 | [diff] [blame] | 562 | dev_err(hba->dev, "error handling flags=0x%x, req. abort count=%d\n", |
| 563 | hba->eh_flags, hba->req_abort_count); |
Can Guo | 3f8af60 | 2020-08-09 05:15:50 -0700 | [diff] [blame] | 564 | dev_err(hba->dev, "hba->ufs_version=0x%x, Host capabilities=0x%x, caps=0x%x\n", |
| 565 | hba->ufs_version, hba->capabilities, hba->caps); |
Gilad Broner | 6ba6558 | 2017-02-03 16:57:28 -0800 | [diff] [blame] | 566 | dev_err(hba->dev, "quirks=0x%x, dev. quirks=0x%x\n", hba->quirks, |
| 567 | hba->dev_quirks); |
Can Guo | 3f8af60 | 2020-08-09 05:15:50 -0700 | [diff] [blame] | 568 | if (sdev_ufs) |
| 569 | dev_err(hba->dev, "UFS dev info: %.8s %.16s rev %.4s\n", |
| 570 | sdev_ufs->vendor, sdev_ufs->model, sdev_ufs->rev); |
| 571 | |
| 572 | ufshcd_print_clk_freqs(hba); |
Gilad Broner | 6ba6558 | 2017-02-03 16:57:28 -0800 | [diff] [blame] | 573 | } |
| 574 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 575 | /** |
| 576 | * ufshcd_print_pwr_info - print power params as saved in hba |
| 577 | * power info |
| 578 | * @hba: per-adapter instance |
| 579 | */ |
| 580 | static void ufshcd_print_pwr_info(struct ufs_hba *hba) |
| 581 | { |
| 582 | static const char * const names[] = { |
| 583 | "INVALID MODE", |
| 584 | "FAST MODE", |
| 585 | "SLOW_MODE", |
| 586 | "INVALID MODE", |
| 587 | "FASTAUTO_MODE", |
| 588 | "SLOWAUTO_MODE", |
| 589 | "INVALID MODE", |
| 590 | }; |
| 591 | |
| 592 | dev_err(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n", |
| 593 | __func__, |
| 594 | hba->pwr_info.gear_rx, hba->pwr_info.gear_tx, |
| 595 | hba->pwr_info.lane_rx, hba->pwr_info.lane_tx, |
| 596 | names[hba->pwr_info.pwr_rx], |
| 597 | names[hba->pwr_info.pwr_tx], |
| 598 | hba->pwr_info.hs_rate); |
| 599 | } |
| 600 | |
Stanley Chu | 31a5d9c | 2020-12-08 21:56:35 +0800 | [diff] [blame] | 601 | static void ufshcd_device_reset(struct ufs_hba *hba) |
| 602 | { |
| 603 | int err; |
| 604 | |
| 605 | err = ufshcd_vops_device_reset(hba); |
| 606 | |
| 607 | if (!err) { |
| 608 | ufshcd_set_ufs_dev_active(hba); |
| 609 | if (ufshcd_is_wb_allowed(hba)) { |
Bean Huo | 4cd4899 | 2021-01-19 17:38:46 +0100 | [diff] [blame] | 610 | hba->dev_info.wb_enabled = false; |
| 611 | hba->dev_info.wb_buf_flush_enabled = false; |
Stanley Chu | 31a5d9c | 2020-12-08 21:56:35 +0800 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | if (err != -EOPNOTSUPP) |
| 615 | ufshcd_update_evt_hist(hba, UFS_EVT_DEV_RESET, err); |
| 616 | } |
| 617 | |
Stanley Chu | 5c955c1 | 2020-03-18 18:40:12 +0800 | [diff] [blame] | 618 | void ufshcd_delay_us(unsigned long us, unsigned long tolerance) |
| 619 | { |
| 620 | if (!us) |
| 621 | return; |
| 622 | |
| 623 | if (us < 10) |
| 624 | udelay(us); |
| 625 | else |
| 626 | usleep_range(us, us + tolerance); |
| 627 | } |
| 628 | EXPORT_SYMBOL_GPL(ufshcd_delay_us); |
| 629 | |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 630 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 631 | * ufshcd_wait_for_register - wait for register value to change |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 632 | * @hba: per-adapter interface |
| 633 | * @reg: mmio register offset |
| 634 | * @mask: mask to apply to the read register value |
| 635 | * @val: value to wait for |
| 636 | * @interval_us: polling interval in microseconds |
| 637 | * @timeout_ms: timeout in milliseconds |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 638 | * |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 639 | * Return: |
| 640 | * -ETIMEDOUT on error, zero on success. |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 641 | */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 642 | int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask, |
| 643 | u32 val, unsigned long interval_us, |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 644 | unsigned long timeout_ms) |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 645 | { |
| 646 | int err = 0; |
| 647 | unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms); |
| 648 | |
| 649 | /* ignore bits that we don't intend to wait on */ |
| 650 | val = val & mask; |
| 651 | |
| 652 | while ((ufshcd_readl(hba, reg) & mask) != val) { |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 653 | usleep_range(interval_us, interval_us + 50); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 654 | if (time_after(jiffies, timeout)) { |
| 655 | if ((ufshcd_readl(hba, reg) & mask) != val) |
| 656 | err = -ETIMEDOUT; |
| 657 | break; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | return err; |
| 662 | } |
| 663 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 664 | /** |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 665 | * ufshcd_get_intr_mask - Get the interrupt bit mask |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 666 | * @hba: Pointer to adapter instance |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 667 | * |
| 668 | * Returns interrupt bit mask per version |
| 669 | */ |
| 670 | static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba) |
| 671 | { |
Yaniv Gardi | c01848c | 2016-12-05 19:25:02 -0800 | [diff] [blame] | 672 | u32 intr_mask = 0; |
| 673 | |
| 674 | switch (hba->ufs_version) { |
| 675 | case UFSHCI_VERSION_10: |
| 676 | intr_mask = INTERRUPT_MASK_ALL_VER_10; |
| 677 | break; |
Yaniv Gardi | c01848c | 2016-12-05 19:25:02 -0800 | [diff] [blame] | 678 | case UFSHCI_VERSION_11: |
| 679 | case UFSHCI_VERSION_20: |
| 680 | intr_mask = INTERRUPT_MASK_ALL_VER_11; |
| 681 | break; |
Yaniv Gardi | c01848c | 2016-12-05 19:25:02 -0800 | [diff] [blame] | 682 | case UFSHCI_VERSION_21: |
| 683 | default: |
| 684 | intr_mask = INTERRUPT_MASK_ALL_VER_21; |
Tomohiro Kusumi | 031d1e0 | 2017-03-23 12:49:04 +0200 | [diff] [blame] | 685 | break; |
Yaniv Gardi | c01848c | 2016-12-05 19:25:02 -0800 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | return intr_mask; |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 692 | * 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] | 693 | * @hba: Pointer to adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 694 | * |
| 695 | * Returns UFSHCI version supported by the controller |
| 696 | */ |
| 697 | static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba) |
| 698 | { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 699 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION) |
| 700 | return ufshcd_vops_get_ufs_hci_version(hba); |
Yaniv Gardi | 9949e70 | 2015-05-17 18:55:05 +0300 | [diff] [blame] | 701 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 702 | return ufshcd_readl(hba, REG_UFS_VERSION); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | /** |
| 706 | * ufshcd_is_device_present - Check if any device connected to |
| 707 | * the host controller |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 708 | * @hba: pointer to adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 709 | * |
Tomohiro Kusumi | c9e6010 | 2017-03-28 16:49:24 +0300 | [diff] [blame] | 710 | * Returns true if device present, false if no device detected |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 711 | */ |
Tomohiro Kusumi | c9e6010 | 2017-03-28 16:49:24 +0300 | [diff] [blame] | 712 | static inline bool ufshcd_is_device_present(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 713 | { |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 714 | return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & |
Tomohiro Kusumi | c9e6010 | 2017-03-28 16:49:24 +0300 | [diff] [blame] | 715 | DEVICE_PRESENT) ? true : false; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | /** |
| 719 | * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 720 | * @lrbp: pointer to local command reference block |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 721 | * |
| 722 | * This function is used to get the OCS field from UTRD |
| 723 | * Returns the OCS field in the UTRD |
| 724 | */ |
| 725 | static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp) |
| 726 | { |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 727 | 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] | 728 | } |
| 729 | |
| 730 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 731 | * ufshcd_utrl_clear - Clear a bit in UTRLCLR register |
| 732 | * @hba: per adapter instance |
| 733 | * @pos: position of the bit to be cleared |
| 734 | */ |
| 735 | static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos) |
| 736 | { |
Alim Akhtar | 8718384 | 2020-05-28 06:46:49 +0530 | [diff] [blame] | 737 | if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR) |
| 738 | ufshcd_writel(hba, (1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR); |
| 739 | else |
| 740 | ufshcd_writel(hba, ~(1 << pos), |
| 741 | REG_UTP_TRANSFER_REQ_LIST_CLEAR); |
Alim Akhtar | 1399c5b | 2018-05-06 15:44:15 +0530 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | /** |
| 745 | * ufshcd_utmrl_clear - Clear a bit in UTRMLCLR register |
| 746 | * @hba: per adapter instance |
| 747 | * @pos: position of the bit to be cleared |
| 748 | */ |
| 749 | static inline void ufshcd_utmrl_clear(struct ufs_hba *hba, u32 pos) |
| 750 | { |
Alim Akhtar | 8718384 | 2020-05-28 06:46:49 +0530 | [diff] [blame] | 751 | if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR) |
| 752 | ufshcd_writel(hba, (1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR); |
| 753 | else |
| 754 | ufshcd_writel(hba, ~(1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | /** |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 758 | * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field |
| 759 | * @hba: per adapter instance |
| 760 | * @tag: position of the bit to be cleared |
| 761 | */ |
| 762 | static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag) |
| 763 | { |
| 764 | __clear_bit(tag, &hba->outstanding_reqs); |
| 765 | } |
| 766 | |
| 767 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 768 | * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY |
| 769 | * @reg: Register value of host controller status |
| 770 | * |
| 771 | * Returns integer, 0 on Success and positive value if failed |
| 772 | */ |
| 773 | static inline int ufshcd_get_lists_status(u32 reg) |
| 774 | { |
Tomohiro Kusumi | 6cf1611 | 2017-04-26 20:28:58 +0300 | [diff] [blame] | 775 | return !((reg & UFSHCD_STATUS_READY) == UFSHCD_STATUS_READY); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | /** |
| 779 | * ufshcd_get_uic_cmd_result - Get the UIC command result |
| 780 | * @hba: Pointer to adapter instance |
| 781 | * |
| 782 | * This function gets the result of UIC command completion |
| 783 | * Returns 0 on success, non zero value on error |
| 784 | */ |
| 785 | static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba) |
| 786 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 787 | return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) & |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 788 | MASK_UIC_COMMAND_RESULT; |
| 789 | } |
| 790 | |
| 791 | /** |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 792 | * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command |
| 793 | * @hba: Pointer to adapter instance |
| 794 | * |
| 795 | * This function gets UIC command argument3 |
| 796 | * Returns 0 on success, non zero value on error |
| 797 | */ |
| 798 | static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba) |
| 799 | { |
| 800 | return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3); |
| 801 | } |
| 802 | |
| 803 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 804 | * ufshcd_get_req_rsp - returns the TR response transaction type |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 805 | * @ucd_rsp_ptr: pointer to response UPIU |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 806 | */ |
| 807 | static inline int |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 808 | ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 809 | { |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 810 | return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | /** |
| 814 | * ufshcd_get_rsp_upiu_result - Get the result from response UPIU |
| 815 | * @ucd_rsp_ptr: pointer to response UPIU |
| 816 | * |
| 817 | * This function gets the response status and scsi_status from response UPIU |
| 818 | * Returns the response result code. |
| 819 | */ |
| 820 | static inline int |
| 821 | ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 822 | { |
| 823 | return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT; |
| 824 | } |
| 825 | |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 826 | /* |
| 827 | * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length |
| 828 | * from response UPIU |
| 829 | * @ucd_rsp_ptr: pointer to response UPIU |
| 830 | * |
| 831 | * Return the data segment length. |
| 832 | */ |
| 833 | static inline unsigned int |
| 834 | ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 835 | { |
| 836 | return be32_to_cpu(ucd_rsp_ptr->header.dword_2) & |
| 837 | MASK_RSP_UPIU_DATA_SEG_LEN; |
| 838 | } |
| 839 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 840 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 841 | * ufshcd_is_exception_event - Check if the device raised an exception event |
| 842 | * @ucd_rsp_ptr: pointer to response UPIU |
| 843 | * |
| 844 | * The function checks if the device raised an exception event indicated in |
| 845 | * the Device Information field of response UPIU. |
| 846 | * |
| 847 | * Returns true if exception is raised, false otherwise. |
| 848 | */ |
| 849 | static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 850 | { |
| 851 | return be32_to_cpu(ucd_rsp_ptr->header.dword_2) & |
| 852 | MASK_RSP_EXCEPTION_EVENT ? true : false; |
| 853 | } |
| 854 | |
| 855 | /** |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 856 | * ufshcd_reset_intr_aggr - Reset interrupt aggregation values. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 857 | * @hba: per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 858 | */ |
| 859 | static inline void |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 860 | ufshcd_reset_intr_aggr(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 861 | { |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 862 | ufshcd_writel(hba, INT_AGGR_ENABLE | |
| 863 | INT_AGGR_COUNTER_AND_TIMER_RESET, |
| 864 | REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
| 865 | } |
| 866 | |
| 867 | /** |
| 868 | * ufshcd_config_intr_aggr - Configure interrupt aggregation values. |
| 869 | * @hba: per adapter instance |
| 870 | * @cnt: Interrupt aggregation counter threshold |
| 871 | * @tmout: Interrupt aggregation timeout value |
| 872 | */ |
| 873 | static inline void |
| 874 | ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout) |
| 875 | { |
| 876 | ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE | |
| 877 | INT_AGGR_COUNTER_THLD_VAL(cnt) | |
| 878 | INT_AGGR_TIMEOUT_VAL(tmout), |
| 879 | REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | /** |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 883 | * ufshcd_disable_intr_aggr - Disables interrupt aggregation. |
| 884 | * @hba: per adapter instance |
| 885 | */ |
| 886 | static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba) |
| 887 | { |
| 888 | ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
| 889 | } |
| 890 | |
| 891 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 892 | * ufshcd_enable_run_stop_reg - Enable run-stop registers, |
| 893 | * When run-stop registers are set to 1, it indicates the |
| 894 | * host controller that it can process the requests |
| 895 | * @hba: per adapter instance |
| 896 | */ |
| 897 | static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba) |
| 898 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 899 | ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT, |
| 900 | REG_UTP_TASK_REQ_LIST_RUN_STOP); |
| 901 | ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT, |
| 902 | REG_UTP_TRANSFER_REQ_LIST_RUN_STOP); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 906 | * ufshcd_hba_start - Start controller initialization sequence |
| 907 | * @hba: per adapter instance |
| 908 | */ |
| 909 | static inline void ufshcd_hba_start(struct ufs_hba *hba) |
| 910 | { |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 911 | u32 val = CONTROLLER_ENABLE; |
| 912 | |
| 913 | if (ufshcd_crypto_enable(hba)) |
| 914 | val |= CRYPTO_GENERAL_ENABLE; |
| 915 | |
| 916 | ufshcd_writel(hba, val, REG_CONTROLLER_ENABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | /** |
| 920 | * ufshcd_is_hba_active - Get controller state |
| 921 | * @hba: per adapter instance |
| 922 | * |
Tomohiro Kusumi | c9e6010 | 2017-03-28 16:49:24 +0300 | [diff] [blame] | 923 | * Returns false if controller is active, true otherwise |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 924 | */ |
Tomohiro Kusumi | c9e6010 | 2017-03-28 16:49:24 +0300 | [diff] [blame] | 925 | static inline bool ufshcd_is_hba_active(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 926 | { |
Tomohiro Kusumi | 4a8eec2 | 2017-03-28 16:49:25 +0300 | [diff] [blame] | 927 | return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & CONTROLLER_ENABLE) |
| 928 | ? false : true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 929 | } |
| 930 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 931 | u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba) |
| 932 | { |
| 933 | /* HCI version 1.0 and 1.1 supports UniPro 1.41 */ |
| 934 | if ((hba->ufs_version == UFSHCI_VERSION_10) || |
| 935 | (hba->ufs_version == UFSHCI_VERSION_11)) |
| 936 | return UFS_UNIPRO_VER_1_41; |
| 937 | else |
| 938 | return UFS_UNIPRO_VER_1_6; |
| 939 | } |
| 940 | EXPORT_SYMBOL(ufshcd_get_local_unipro_ver); |
| 941 | |
| 942 | static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba) |
| 943 | { |
| 944 | /* |
| 945 | * If both host and device support UniPro ver1.6 or later, PA layer |
| 946 | * parameters tuning happens during link startup itself. |
| 947 | * |
| 948 | * We can manually tune PA layer parameters if either host or device |
| 949 | * doesn't support UniPro ver 1.6 or later. But to keep manual tuning |
| 950 | * logic simple, we will only do manual tuning if local unipro version |
| 951 | * doesn't support ver1.6 or later. |
| 952 | */ |
| 953 | if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6) |
| 954 | return true; |
| 955 | else |
| 956 | return false; |
| 957 | } |
| 958 | |
Subhash Jadavani | 394b949 | 2020-03-26 02:25:40 -0700 | [diff] [blame] | 959 | /** |
| 960 | * ufshcd_set_clk_freq - set UFS controller clock frequencies |
| 961 | * @hba: per adapter instance |
| 962 | * @scale_up: If True, set max possible frequency othewise set low frequency |
| 963 | * |
| 964 | * Returns 0 if successful |
| 965 | * Returns < 0 for any other errors |
| 966 | */ |
| 967 | 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] | 968 | { |
| 969 | int ret = 0; |
| 970 | struct ufs_clk_info *clki; |
| 971 | struct list_head *head = &hba->clk_list_head; |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 972 | |
Szymon Mielczarek | 566ec9a | 2017-06-05 11:36:54 +0300 | [diff] [blame] | 973 | if (list_empty(head)) |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 974 | goto out; |
| 975 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 976 | list_for_each_entry(clki, head, list) { |
| 977 | if (!IS_ERR_OR_NULL(clki->clk)) { |
| 978 | if (scale_up && clki->max_freq) { |
| 979 | if (clki->curr_freq == clki->max_freq) |
| 980 | continue; |
| 981 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 982 | ret = clk_set_rate(clki->clk, clki->max_freq); |
| 983 | if (ret) { |
| 984 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 985 | __func__, clki->name, |
| 986 | clki->max_freq, ret); |
| 987 | break; |
| 988 | } |
| 989 | trace_ufshcd_clk_scaling(dev_name(hba->dev), |
| 990 | "scaled up", clki->name, |
| 991 | clki->curr_freq, |
| 992 | clki->max_freq); |
| 993 | |
| 994 | clki->curr_freq = clki->max_freq; |
| 995 | |
| 996 | } else if (!scale_up && clki->min_freq) { |
| 997 | if (clki->curr_freq == clki->min_freq) |
| 998 | continue; |
| 999 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1000 | ret = clk_set_rate(clki->clk, clki->min_freq); |
| 1001 | if (ret) { |
| 1002 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 1003 | __func__, clki->name, |
| 1004 | clki->min_freq, ret); |
| 1005 | break; |
| 1006 | } |
| 1007 | trace_ufshcd_clk_scaling(dev_name(hba->dev), |
| 1008 | "scaled down", clki->name, |
| 1009 | clki->curr_freq, |
| 1010 | clki->min_freq); |
| 1011 | clki->curr_freq = clki->min_freq; |
| 1012 | } |
| 1013 | } |
| 1014 | dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__, |
| 1015 | clki->name, clk_get_rate(clki->clk)); |
| 1016 | } |
| 1017 | |
Subhash Jadavani | 394b949 | 2020-03-26 02:25:40 -0700 | [diff] [blame] | 1018 | out: |
| 1019 | return ret; |
| 1020 | } |
| 1021 | |
| 1022 | /** |
| 1023 | * ufshcd_scale_clks - scale up or scale down UFS controller clocks |
| 1024 | * @hba: per adapter instance |
| 1025 | * @scale_up: True if scaling up and false if scaling down |
| 1026 | * |
| 1027 | * Returns 0 if successful |
| 1028 | * Returns < 0 for any other errors |
| 1029 | */ |
| 1030 | static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up) |
| 1031 | { |
| 1032 | int ret = 0; |
| 1033 | ktime_t start = ktime_get(); |
| 1034 | |
| 1035 | ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE); |
| 1036 | if (ret) |
| 1037 | goto out; |
| 1038 | |
| 1039 | ret = ufshcd_set_clk_freq(hba, scale_up); |
| 1040 | if (ret) |
| 1041 | goto out; |
| 1042 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1043 | ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE); |
Subhash Jadavani | 394b949 | 2020-03-26 02:25:40 -0700 | [diff] [blame] | 1044 | if (ret) |
| 1045 | ufshcd_set_clk_freq(hba, !scale_up); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1046 | |
| 1047 | out: |
Subhash Jadavani | 394b949 | 2020-03-26 02:25:40 -0700 | [diff] [blame] | 1048 | trace_ufshcd_profile_clk_scaling(dev_name(hba->dev), |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1049 | (scale_up ? "up" : "down"), |
| 1050 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
| 1051 | return ret; |
| 1052 | } |
| 1053 | |
| 1054 | /** |
| 1055 | * ufshcd_is_devfreq_scaling_required - check if scaling is required or not |
| 1056 | * @hba: per adapter instance |
| 1057 | * @scale_up: True if scaling up and false if scaling down |
| 1058 | * |
| 1059 | * Returns true if scaling is required, false otherwise. |
| 1060 | */ |
| 1061 | static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba, |
| 1062 | bool scale_up) |
| 1063 | { |
| 1064 | struct ufs_clk_info *clki; |
| 1065 | struct list_head *head = &hba->clk_list_head; |
| 1066 | |
Szymon Mielczarek | 566ec9a | 2017-06-05 11:36:54 +0300 | [diff] [blame] | 1067 | if (list_empty(head)) |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1068 | return false; |
| 1069 | |
| 1070 | list_for_each_entry(clki, head, list) { |
| 1071 | if (!IS_ERR_OR_NULL(clki->clk)) { |
| 1072 | if (scale_up && clki->max_freq) { |
| 1073 | if (clki->curr_freq == clki->max_freq) |
| 1074 | continue; |
| 1075 | return true; |
| 1076 | } else if (!scale_up && clki->min_freq) { |
| 1077 | if (clki->curr_freq == clki->min_freq) |
| 1078 | continue; |
| 1079 | return true; |
| 1080 | } |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | return false; |
| 1085 | } |
| 1086 | |
| 1087 | static int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba, |
| 1088 | u64 wait_timeout_us) |
| 1089 | { |
| 1090 | unsigned long flags; |
| 1091 | int ret = 0; |
| 1092 | u32 tm_doorbell; |
| 1093 | u32 tr_doorbell; |
| 1094 | bool timeout = false, do_last_check = false; |
| 1095 | ktime_t start; |
| 1096 | |
| 1097 | ufshcd_hold(hba, false); |
| 1098 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1099 | /* |
| 1100 | * Wait for all the outstanding tasks/transfer requests. |
| 1101 | * Verify by checking the doorbell registers are clear. |
| 1102 | */ |
| 1103 | start = ktime_get(); |
| 1104 | do { |
| 1105 | if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) { |
| 1106 | ret = -EBUSY; |
| 1107 | goto out; |
| 1108 | } |
| 1109 | |
| 1110 | tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL); |
| 1111 | tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 1112 | if (!tm_doorbell && !tr_doorbell) { |
| 1113 | timeout = false; |
| 1114 | break; |
| 1115 | } else if (do_last_check) { |
| 1116 | break; |
| 1117 | } |
| 1118 | |
| 1119 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1120 | schedule(); |
| 1121 | if (ktime_to_us(ktime_sub(ktime_get(), start)) > |
| 1122 | wait_timeout_us) { |
| 1123 | timeout = true; |
| 1124 | /* |
| 1125 | * We might have scheduled out for long time so make |
| 1126 | * sure to check if doorbells are cleared by this time |
| 1127 | * or not. |
| 1128 | */ |
| 1129 | do_last_check = true; |
| 1130 | } |
| 1131 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1132 | } while (tm_doorbell || tr_doorbell); |
| 1133 | |
| 1134 | if (timeout) { |
| 1135 | dev_err(hba->dev, |
| 1136 | "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n", |
| 1137 | __func__, tm_doorbell, tr_doorbell); |
| 1138 | ret = -EBUSY; |
| 1139 | } |
| 1140 | out: |
| 1141 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1142 | ufshcd_release(hba); |
| 1143 | return ret; |
| 1144 | } |
| 1145 | |
| 1146 | /** |
| 1147 | * ufshcd_scale_gear - scale up/down UFS gear |
| 1148 | * @hba: per adapter instance |
| 1149 | * @scale_up: True for scaling up gear and false for scaling down |
| 1150 | * |
| 1151 | * Returns 0 for success, |
| 1152 | * Returns -EBUSY if scaling can't happen at this time |
| 1153 | * Returns non-zero for any other errors |
| 1154 | */ |
| 1155 | static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up) |
| 1156 | { |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1157 | int ret = 0; |
| 1158 | struct ufs_pa_layer_attr new_pwr_info; |
| 1159 | |
| 1160 | if (scale_up) { |
| 1161 | memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info.info, |
| 1162 | sizeof(struct ufs_pa_layer_attr)); |
| 1163 | } else { |
| 1164 | memcpy(&new_pwr_info, &hba->pwr_info, |
| 1165 | sizeof(struct ufs_pa_layer_attr)); |
| 1166 | |
Can Guo | 29b87e9 | 2020-11-26 17:58:48 -0800 | [diff] [blame] | 1167 | if (hba->pwr_info.gear_tx > hba->clk_scaling.min_gear || |
| 1168 | hba->pwr_info.gear_rx > hba->clk_scaling.min_gear) { |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1169 | /* save the current power mode */ |
| 1170 | memcpy(&hba->clk_scaling.saved_pwr_info.info, |
| 1171 | &hba->pwr_info, |
| 1172 | sizeof(struct ufs_pa_layer_attr)); |
| 1173 | |
| 1174 | /* scale down gear */ |
Can Guo | 29b87e9 | 2020-11-26 17:58:48 -0800 | [diff] [blame] | 1175 | new_pwr_info.gear_tx = hba->clk_scaling.min_gear; |
| 1176 | new_pwr_info.gear_rx = hba->clk_scaling.min_gear; |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | /* check if the power mode needs to be changed or not? */ |
Can Guo | 6a9df81 | 2020-02-11 21:38:28 -0800 | [diff] [blame] | 1181 | ret = ufshcd_config_pwr_mode(hba, &new_pwr_info); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1182 | if (ret) |
| 1183 | dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d)", |
| 1184 | __func__, ret, |
| 1185 | hba->pwr_info.gear_tx, hba->pwr_info.gear_rx, |
| 1186 | new_pwr_info.gear_tx, new_pwr_info.gear_rx); |
| 1187 | |
| 1188 | return ret; |
| 1189 | } |
| 1190 | |
| 1191 | static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba) |
| 1192 | { |
| 1193 | #define DOORBELL_CLR_TOUT_US (1000 * 1000) /* 1 sec */ |
| 1194 | int ret = 0; |
| 1195 | /* |
| 1196 | * make sure that there are no outstanding requests when |
| 1197 | * clock scaling is in progress |
| 1198 | */ |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 1199 | ufshcd_scsi_block_requests(hba); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1200 | down_write(&hba->clk_scaling_lock); |
Can Guo | 0e9d4ca | 2021-01-20 02:04:21 -0800 | [diff] [blame] | 1201 | |
| 1202 | if (!hba->clk_scaling.is_allowed || |
| 1203 | ufshcd_wait_for_doorbell_clr(hba, DOORBELL_CLR_TOUT_US)) { |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1204 | ret = -EBUSY; |
| 1205 | up_write(&hba->clk_scaling_lock); |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 1206 | ufshcd_scsi_unblock_requests(hba); |
Can Guo | 0e9d4ca | 2021-01-20 02:04:21 -0800 | [diff] [blame] | 1207 | goto out; |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1208 | } |
| 1209 | |
Can Guo | 0e9d4ca | 2021-01-20 02:04:21 -0800 | [diff] [blame] | 1210 | /* let's not get into low power until clock scaling is completed */ |
| 1211 | ufshcd_hold(hba, false); |
| 1212 | |
| 1213 | out: |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1214 | return ret; |
| 1215 | } |
| 1216 | |
Can Guo | 0e9d4ca | 2021-01-20 02:04:21 -0800 | [diff] [blame] | 1217 | static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba, bool writelock) |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1218 | { |
Can Guo | 0e9d4ca | 2021-01-20 02:04:21 -0800 | [diff] [blame] | 1219 | if (writelock) |
| 1220 | up_write(&hba->clk_scaling_lock); |
| 1221 | else |
| 1222 | up_read(&hba->clk_scaling_lock); |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 1223 | ufshcd_scsi_unblock_requests(hba); |
Can Guo | 0e9d4ca | 2021-01-20 02:04:21 -0800 | [diff] [blame] | 1224 | ufshcd_release(hba); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | /** |
| 1228 | * ufshcd_devfreq_scale - scale up/down UFS clocks and gear |
| 1229 | * @hba: per adapter instance |
| 1230 | * @scale_up: True for scaling up and false for scalin down |
| 1231 | * |
| 1232 | * Returns 0 for success, |
| 1233 | * Returns -EBUSY if scaling can't happen at this time |
| 1234 | * Returns non-zero for any other errors |
| 1235 | */ |
| 1236 | static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up) |
| 1237 | { |
| 1238 | int ret = 0; |
Can Guo | 0e9d4ca | 2021-01-20 02:04:21 -0800 | [diff] [blame] | 1239 | bool is_writelock = true; |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1240 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1241 | ret = ufshcd_clock_scaling_prepare(hba); |
| 1242 | if (ret) |
Can Guo | 0e9d4ca | 2021-01-20 02:04:21 -0800 | [diff] [blame] | 1243 | return ret; |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1244 | |
| 1245 | /* scale down the gear before scaling down clocks */ |
| 1246 | if (!scale_up) { |
| 1247 | ret = ufshcd_scale_gear(hba, false); |
| 1248 | if (ret) |
Subhash Jadavani | 394b949 | 2020-03-26 02:25:40 -0700 | [diff] [blame] | 1249 | goto out_unprepare; |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | ret = ufshcd_scale_clks(hba, scale_up); |
| 1253 | if (ret) { |
| 1254 | if (!scale_up) |
| 1255 | ufshcd_scale_gear(hba, true); |
Subhash Jadavani | 394b949 | 2020-03-26 02:25:40 -0700 | [diff] [blame] | 1256 | goto out_unprepare; |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | /* scale up the gear after scaling up clocks */ |
| 1260 | if (scale_up) { |
| 1261 | ret = ufshcd_scale_gear(hba, true); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 1262 | if (ret) { |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1263 | ufshcd_scale_clks(hba, false); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 1264 | goto out_unprepare; |
| 1265 | } |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1266 | } |
| 1267 | |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 1268 | /* Enable Write Booster if we have scaled up else disable it */ |
Can Guo | 0e9d4ca | 2021-01-20 02:04:21 -0800 | [diff] [blame] | 1269 | downgrade_write(&hba->clk_scaling_lock); |
| 1270 | is_writelock = false; |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 1271 | ufshcd_wb_ctrl(hba, scale_up); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 1272 | |
Subhash Jadavani | 394b949 | 2020-03-26 02:25:40 -0700 | [diff] [blame] | 1273 | out_unprepare: |
Can Guo | 0e9d4ca | 2021-01-20 02:04:21 -0800 | [diff] [blame] | 1274 | ufshcd_clock_scaling_unprepare(hba, is_writelock); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1275 | return ret; |
| 1276 | } |
| 1277 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1278 | static void ufshcd_clk_scaling_suspend_work(struct work_struct *work) |
| 1279 | { |
| 1280 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1281 | clk_scaling.suspend_work); |
| 1282 | unsigned long irq_flags; |
| 1283 | |
| 1284 | spin_lock_irqsave(hba->host->host_lock, irq_flags); |
| 1285 | if (hba->clk_scaling.active_reqs || hba->clk_scaling.is_suspended) { |
| 1286 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1287 | return; |
| 1288 | } |
| 1289 | hba->clk_scaling.is_suspended = true; |
| 1290 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1291 | |
| 1292 | __ufshcd_suspend_clkscaling(hba); |
| 1293 | } |
| 1294 | |
| 1295 | static void ufshcd_clk_scaling_resume_work(struct work_struct *work) |
| 1296 | { |
| 1297 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1298 | clk_scaling.resume_work); |
| 1299 | unsigned long irq_flags; |
| 1300 | |
| 1301 | spin_lock_irqsave(hba->host->host_lock, irq_flags); |
| 1302 | if (!hba->clk_scaling.is_suspended) { |
| 1303 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1304 | return; |
| 1305 | } |
| 1306 | hba->clk_scaling.is_suspended = false; |
| 1307 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1308 | |
| 1309 | devfreq_resume_device(hba->devfreq); |
| 1310 | } |
| 1311 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1312 | static int ufshcd_devfreq_target(struct device *dev, |
| 1313 | unsigned long *freq, u32 flags) |
| 1314 | { |
| 1315 | int ret = 0; |
| 1316 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1317 | ktime_t start; |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1318 | bool scale_up, sched_clk_scaling_suspend_work = false; |
Bjorn Andersson | 092b455 | 2018-05-17 23:26:37 -0700 | [diff] [blame] | 1319 | struct list_head *clk_list = &hba->clk_list_head; |
| 1320 | struct ufs_clk_info *clki; |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1321 | unsigned long irq_flags; |
| 1322 | |
| 1323 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 1324 | return -EINVAL; |
| 1325 | |
Asutosh Das | 91831d3 | 2020-03-25 11:29:00 -0700 | [diff] [blame] | 1326 | clki = list_first_entry(&hba->clk_list_head, struct ufs_clk_info, list); |
| 1327 | /* Override with the closest supported frequency */ |
| 1328 | *freq = (unsigned long) clk_round_rate(clki->clk, *freq); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1329 | spin_lock_irqsave(hba->host->host_lock, irq_flags); |
| 1330 | if (ufshcd_eh_in_progress(hba)) { |
| 1331 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1332 | return 0; |
| 1333 | } |
| 1334 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1335 | if (!hba->clk_scaling.active_reqs) |
| 1336 | sched_clk_scaling_suspend_work = true; |
| 1337 | |
Bjorn Andersson | 092b455 | 2018-05-17 23:26:37 -0700 | [diff] [blame] | 1338 | if (list_empty(clk_list)) { |
| 1339 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1340 | goto out; |
| 1341 | } |
| 1342 | |
Asutosh Das | 91831d3 | 2020-03-25 11:29:00 -0700 | [diff] [blame] | 1343 | /* Decide based on the rounded-off frequency and update */ |
Bjorn Andersson | 092b455 | 2018-05-17 23:26:37 -0700 | [diff] [blame] | 1344 | scale_up = (*freq == clki->max_freq) ? true : false; |
Asutosh Das | 91831d3 | 2020-03-25 11:29:00 -0700 | [diff] [blame] | 1345 | if (!scale_up) |
| 1346 | *freq = clki->min_freq; |
| 1347 | /* Update the frequency */ |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1348 | if (!ufshcd_is_devfreq_scaling_required(hba, scale_up)) { |
| 1349 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1350 | ret = 0; |
| 1351 | goto out; /* no state change required */ |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1352 | } |
| 1353 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1354 | |
| 1355 | start = ktime_get(); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1356 | ret = ufshcd_devfreq_scale(hba, scale_up); |
| 1357 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1358 | trace_ufshcd_profile_clk_scaling(dev_name(hba->dev), |
| 1359 | (scale_up ? "up" : "down"), |
| 1360 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
| 1361 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1362 | out: |
| 1363 | if (sched_clk_scaling_suspend_work) |
| 1364 | queue_work(hba->clk_scaling.workq, |
| 1365 | &hba->clk_scaling.suspend_work); |
| 1366 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1367 | return ret; |
| 1368 | } |
| 1369 | |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 1370 | static bool ufshcd_is_busy(struct request *req, void *priv, bool reserved) |
| 1371 | { |
| 1372 | int *busy = priv; |
| 1373 | |
| 1374 | WARN_ON_ONCE(reserved); |
| 1375 | (*busy)++; |
| 1376 | return false; |
| 1377 | } |
| 1378 | |
| 1379 | /* Whether or not any tag is in use by a request that is in progress. */ |
| 1380 | static bool ufshcd_any_tag_in_use(struct ufs_hba *hba) |
| 1381 | { |
| 1382 | struct request_queue *q = hba->cmd_queue; |
| 1383 | int busy = 0; |
| 1384 | |
| 1385 | blk_mq_tagset_busy_iter(q->tag_set, ufshcd_is_busy, &busy); |
| 1386 | return busy; |
| 1387 | } |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1388 | |
| 1389 | static int ufshcd_devfreq_get_dev_status(struct device *dev, |
| 1390 | struct devfreq_dev_status *stat) |
| 1391 | { |
| 1392 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1393 | struct ufs_clk_scaling *scaling = &hba->clk_scaling; |
| 1394 | unsigned long flags; |
Asutosh Das | 91831d3 | 2020-03-25 11:29:00 -0700 | [diff] [blame] | 1395 | struct list_head *clk_list = &hba->clk_list_head; |
| 1396 | struct ufs_clk_info *clki; |
Stanley Chu | b1bf66d | 2020-06-11 18:10:43 +0800 | [diff] [blame] | 1397 | ktime_t curr_t; |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1398 | |
| 1399 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 1400 | return -EINVAL; |
| 1401 | |
| 1402 | memset(stat, 0, sizeof(*stat)); |
| 1403 | |
| 1404 | spin_lock_irqsave(hba->host->host_lock, flags); |
Stanley Chu | b1bf66d | 2020-06-11 18:10:43 +0800 | [diff] [blame] | 1405 | curr_t = ktime_get(); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1406 | if (!scaling->window_start_t) |
| 1407 | goto start_window; |
| 1408 | |
Asutosh Das | 91831d3 | 2020-03-25 11:29:00 -0700 | [diff] [blame] | 1409 | clki = list_first_entry(clk_list, struct ufs_clk_info, list); |
| 1410 | /* |
| 1411 | * If current frequency is 0, then the ondemand governor considers |
| 1412 | * there's no initial frequency set. And it always requests to set |
| 1413 | * to max. frequency. |
| 1414 | */ |
| 1415 | stat->current_frequency = clki->curr_freq; |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1416 | if (scaling->is_busy_started) |
Stanley Chu | b1bf66d | 2020-06-11 18:10:43 +0800 | [diff] [blame] | 1417 | scaling->tot_busy_t += ktime_us_delta(curr_t, |
| 1418 | scaling->busy_start_t); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1419 | |
Stanley Chu | b1bf66d | 2020-06-11 18:10:43 +0800 | [diff] [blame] | 1420 | 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] | 1421 | stat->busy_time = scaling->tot_busy_t; |
| 1422 | start_window: |
Stanley Chu | b1bf66d | 2020-06-11 18:10:43 +0800 | [diff] [blame] | 1423 | scaling->window_start_t = curr_t; |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1424 | scaling->tot_busy_t = 0; |
| 1425 | |
| 1426 | if (hba->outstanding_reqs) { |
Stanley Chu | b1bf66d | 2020-06-11 18:10:43 +0800 | [diff] [blame] | 1427 | scaling->busy_start_t = curr_t; |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1428 | scaling->is_busy_started = true; |
| 1429 | } else { |
| 1430 | scaling->busy_start_t = 0; |
| 1431 | scaling->is_busy_started = false; |
| 1432 | } |
| 1433 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1434 | return 0; |
| 1435 | } |
| 1436 | |
Bjorn Andersson | deac444 | 2018-05-17 23:26:36 -0700 | [diff] [blame] | 1437 | static int ufshcd_devfreq_init(struct ufs_hba *hba) |
| 1438 | { |
Bjorn Andersson | 092b455 | 2018-05-17 23:26:37 -0700 | [diff] [blame] | 1439 | struct list_head *clk_list = &hba->clk_list_head; |
| 1440 | struct ufs_clk_info *clki; |
Bjorn Andersson | deac444 | 2018-05-17 23:26:36 -0700 | [diff] [blame] | 1441 | struct devfreq *devfreq; |
| 1442 | int ret; |
| 1443 | |
Bjorn Andersson | 092b455 | 2018-05-17 23:26:37 -0700 | [diff] [blame] | 1444 | /* Skip devfreq if we don't have any clocks in the list */ |
| 1445 | if (list_empty(clk_list)) |
| 1446 | return 0; |
| 1447 | |
| 1448 | clki = list_first_entry(clk_list, struct ufs_clk_info, list); |
| 1449 | dev_pm_opp_add(hba->dev, clki->min_freq, 0); |
| 1450 | dev_pm_opp_add(hba->dev, clki->max_freq, 0); |
| 1451 | |
Stanley Chu | 90b8491 | 2020-05-09 17:37:13 +0800 | [diff] [blame] | 1452 | ufshcd_vops_config_scaling_param(hba, &hba->vps->devfreq_profile, |
| 1453 | &hba->vps->ondemand_data); |
Bjorn Andersson | 092b455 | 2018-05-17 23:26:37 -0700 | [diff] [blame] | 1454 | devfreq = devfreq_add_device(hba->dev, |
Stanley Chu | 90b8491 | 2020-05-09 17:37:13 +0800 | [diff] [blame] | 1455 | &hba->vps->devfreq_profile, |
Bjorn Andersson | deac444 | 2018-05-17 23:26:36 -0700 | [diff] [blame] | 1456 | DEVFREQ_GOV_SIMPLE_ONDEMAND, |
Stanley Chu | 90b8491 | 2020-05-09 17:37:13 +0800 | [diff] [blame] | 1457 | &hba->vps->ondemand_data); |
Bjorn Andersson | deac444 | 2018-05-17 23:26:36 -0700 | [diff] [blame] | 1458 | if (IS_ERR(devfreq)) { |
| 1459 | ret = PTR_ERR(devfreq); |
| 1460 | dev_err(hba->dev, "Unable to register with devfreq %d\n", ret); |
Bjorn Andersson | 092b455 | 2018-05-17 23:26:37 -0700 | [diff] [blame] | 1461 | |
| 1462 | dev_pm_opp_remove(hba->dev, clki->min_freq); |
| 1463 | dev_pm_opp_remove(hba->dev, clki->max_freq); |
Bjorn Andersson | deac444 | 2018-05-17 23:26:36 -0700 | [diff] [blame] | 1464 | return ret; |
| 1465 | } |
| 1466 | |
| 1467 | hba->devfreq = devfreq; |
| 1468 | |
| 1469 | return 0; |
| 1470 | } |
| 1471 | |
Bjorn Andersson | 092b455 | 2018-05-17 23:26:37 -0700 | [diff] [blame] | 1472 | static void ufshcd_devfreq_remove(struct ufs_hba *hba) |
| 1473 | { |
| 1474 | struct list_head *clk_list = &hba->clk_list_head; |
| 1475 | struct ufs_clk_info *clki; |
| 1476 | |
| 1477 | if (!hba->devfreq) |
| 1478 | return; |
| 1479 | |
| 1480 | devfreq_remove_device(hba->devfreq); |
| 1481 | hba->devfreq = NULL; |
| 1482 | |
| 1483 | clki = list_first_entry(clk_list, struct ufs_clk_info, list); |
| 1484 | dev_pm_opp_remove(hba->dev, clki->min_freq); |
| 1485 | dev_pm_opp_remove(hba->dev, clki->max_freq); |
| 1486 | } |
| 1487 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1488 | static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba) |
| 1489 | { |
| 1490 | unsigned long flags; |
| 1491 | |
| 1492 | devfreq_suspend_device(hba->devfreq); |
| 1493 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1494 | hba->clk_scaling.window_start_t = 0; |
| 1495 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1496 | } |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1497 | |
Gilad Broner | a508253 | 2016-10-17 17:10:00 -0700 | [diff] [blame] | 1498 | static void ufshcd_suspend_clkscaling(struct ufs_hba *hba) |
| 1499 | { |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1500 | unsigned long flags; |
| 1501 | bool suspend = false; |
| 1502 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1503 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 1504 | return; |
| 1505 | |
Stanley Chu | f9a7fa3 | 2021-01-20 23:01:40 +0800 | [diff] [blame] | 1506 | cancel_work_sync(&hba->clk_scaling.suspend_work); |
| 1507 | cancel_work_sync(&hba->clk_scaling.resume_work); |
| 1508 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1509 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1510 | if (!hba->clk_scaling.is_suspended) { |
| 1511 | suspend = true; |
| 1512 | hba->clk_scaling.is_suspended = true; |
| 1513 | } |
| 1514 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1515 | |
| 1516 | if (suspend) |
| 1517 | __ufshcd_suspend_clkscaling(hba); |
Gilad Broner | a508253 | 2016-10-17 17:10:00 -0700 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | static void ufshcd_resume_clkscaling(struct ufs_hba *hba) |
| 1521 | { |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1522 | unsigned long flags; |
| 1523 | bool resume = false; |
| 1524 | |
| 1525 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 1526 | return; |
| 1527 | |
| 1528 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1529 | if (hba->clk_scaling.is_suspended) { |
| 1530 | resume = true; |
| 1531 | hba->clk_scaling.is_suspended = false; |
| 1532 | } |
| 1533 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1534 | |
| 1535 | if (resume) |
| 1536 | devfreq_resume_device(hba->devfreq); |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | static ssize_t ufshcd_clkscale_enable_show(struct device *dev, |
| 1540 | struct device_attribute *attr, char *buf) |
| 1541 | { |
| 1542 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1543 | |
Can Guo | 0e9d4ca | 2021-01-20 02:04:21 -0800 | [diff] [blame] | 1544 | return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_scaling.is_enabled); |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | static ssize_t ufshcd_clkscale_enable_store(struct device *dev, |
| 1548 | struct device_attribute *attr, const char *buf, size_t count) |
| 1549 | { |
| 1550 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1551 | u32 value; |
Can Guo | 9cd20d3 | 2021-01-13 19:13:28 -0800 | [diff] [blame] | 1552 | int err = 0; |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1553 | |
| 1554 | if (kstrtou32(buf, 0, &value)) |
| 1555 | return -EINVAL; |
| 1556 | |
Can Guo | 9cd20d3 | 2021-01-13 19:13:28 -0800 | [diff] [blame] | 1557 | down(&hba->host_sem); |
| 1558 | if (!ufshcd_is_user_access_allowed(hba)) { |
| 1559 | err = -EBUSY; |
| 1560 | goto out; |
| 1561 | } |
| 1562 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1563 | value = !!value; |
Can Guo | 0e9d4ca | 2021-01-20 02:04:21 -0800 | [diff] [blame] | 1564 | if (value == hba->clk_scaling.is_enabled) |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1565 | goto out; |
| 1566 | |
| 1567 | pm_runtime_get_sync(hba->dev); |
| 1568 | ufshcd_hold(hba, false); |
| 1569 | |
Can Guo | 0e9d4ca | 2021-01-20 02:04:21 -0800 | [diff] [blame] | 1570 | hba->clk_scaling.is_enabled = value; |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1571 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1572 | if (value) { |
| 1573 | ufshcd_resume_clkscaling(hba); |
| 1574 | } else { |
| 1575 | ufshcd_suspend_clkscaling(hba); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1576 | err = ufshcd_devfreq_scale(hba, true); |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1577 | if (err) |
| 1578 | dev_err(hba->dev, "%s: failed to scale clocks up %d\n", |
| 1579 | __func__, err); |
| 1580 | } |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1581 | |
| 1582 | ufshcd_release(hba); |
| 1583 | pm_runtime_put_sync(hba->dev); |
| 1584 | out: |
Can Guo | 9cd20d3 | 2021-01-13 19:13:28 -0800 | [diff] [blame] | 1585 | up(&hba->host_sem); |
| 1586 | return err ? err : count; |
Gilad Broner | a508253 | 2016-10-17 17:10:00 -0700 | [diff] [blame] | 1587 | } |
| 1588 | |
Can Guo | 4543d9d | 2021-01-20 02:04:22 -0800 | [diff] [blame] | 1589 | static void ufshcd_init_clk_scaling_sysfs(struct ufs_hba *hba) |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1590 | { |
| 1591 | hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show; |
| 1592 | hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store; |
| 1593 | sysfs_attr_init(&hba->clk_scaling.enable_attr.attr); |
| 1594 | hba->clk_scaling.enable_attr.attr.name = "clkscale_enable"; |
| 1595 | hba->clk_scaling.enable_attr.attr.mode = 0644; |
| 1596 | if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr)) |
| 1597 | dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n"); |
| 1598 | } |
| 1599 | |
Can Guo | 4543d9d | 2021-01-20 02:04:22 -0800 | [diff] [blame] | 1600 | static void ufshcd_remove_clk_scaling_sysfs(struct ufs_hba *hba) |
| 1601 | { |
| 1602 | if (hba->clk_scaling.enable_attr.attr.name) |
| 1603 | device_remove_file(hba->dev, &hba->clk_scaling.enable_attr); |
| 1604 | } |
| 1605 | |
| 1606 | static void ufshcd_init_clk_scaling(struct ufs_hba *hba) |
| 1607 | { |
| 1608 | char wq_name[sizeof("ufs_clkscaling_00")]; |
| 1609 | |
| 1610 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 1611 | return; |
| 1612 | |
| 1613 | INIT_WORK(&hba->clk_scaling.suspend_work, |
| 1614 | ufshcd_clk_scaling_suspend_work); |
| 1615 | INIT_WORK(&hba->clk_scaling.resume_work, |
| 1616 | ufshcd_clk_scaling_resume_work); |
| 1617 | |
| 1618 | snprintf(wq_name, sizeof(wq_name), "ufs_clkscaling_%d", |
| 1619 | hba->host->host_no); |
| 1620 | hba->clk_scaling.workq = create_singlethread_workqueue(wq_name); |
| 1621 | |
| 1622 | hba->clk_scaling.is_initialized = true; |
| 1623 | } |
| 1624 | |
| 1625 | static void ufshcd_exit_clk_scaling(struct ufs_hba *hba) |
| 1626 | { |
| 1627 | if (!hba->clk_scaling.is_initialized) |
| 1628 | return; |
| 1629 | |
| 1630 | ufshcd_remove_clk_scaling_sysfs(hba); |
| 1631 | destroy_workqueue(hba->clk_scaling.workq); |
| 1632 | ufshcd_devfreq_remove(hba); |
| 1633 | hba->clk_scaling.is_initialized = false; |
| 1634 | } |
| 1635 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1636 | static void ufshcd_ungate_work(struct work_struct *work) |
| 1637 | { |
| 1638 | int ret; |
| 1639 | unsigned long flags; |
| 1640 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1641 | clk_gating.ungate_work); |
| 1642 | |
| 1643 | cancel_delayed_work_sync(&hba->clk_gating.gate_work); |
| 1644 | |
| 1645 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1646 | if (hba->clk_gating.state == CLKS_ON) { |
| 1647 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1648 | goto unblock_reqs; |
| 1649 | } |
| 1650 | |
| 1651 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Can Guo | dd7143e | 2020-10-27 12:10:36 -0700 | [diff] [blame] | 1652 | ufshcd_hba_vreg_set_hpm(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1653 | ufshcd_setup_clocks(hba, true); |
| 1654 | |
Stanley Chu | 8b0bbf0 | 2019-12-07 20:22:01 +0800 | [diff] [blame] | 1655 | ufshcd_enable_irq(hba); |
| 1656 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1657 | /* Exit from hibern8 */ |
| 1658 | if (ufshcd_can_hibern8_during_gating(hba)) { |
| 1659 | /* Prevent gating in this path */ |
| 1660 | hba->clk_gating.is_suspended = true; |
| 1661 | if (ufshcd_is_link_hibern8(hba)) { |
| 1662 | ret = ufshcd_uic_hibern8_exit(hba); |
| 1663 | if (ret) |
| 1664 | dev_err(hba->dev, "%s: hibern8 exit failed %d\n", |
| 1665 | __func__, ret); |
| 1666 | else |
| 1667 | ufshcd_set_link_active(hba); |
| 1668 | } |
| 1669 | hba->clk_gating.is_suspended = false; |
| 1670 | } |
| 1671 | unblock_reqs: |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 1672 | ufshcd_scsi_unblock_requests(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | /** |
| 1676 | * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release. |
| 1677 | * Also, exit from hibern8 mode and set the link as active. |
| 1678 | * @hba: per adapter instance |
| 1679 | * @async: This indicates whether caller should ungate clocks asynchronously. |
| 1680 | */ |
| 1681 | int ufshcd_hold(struct ufs_hba *hba, bool async) |
| 1682 | { |
| 1683 | int rc = 0; |
Stanley Chu | 93b6c5d | 2020-08-09 13:07:34 +0800 | [diff] [blame] | 1684 | bool flush_result; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1685 | unsigned long flags; |
| 1686 | |
| 1687 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1688 | goto out; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1689 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1690 | hba->clk_gating.active_reqs++; |
| 1691 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1692 | start: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1693 | switch (hba->clk_gating.state) { |
| 1694 | case CLKS_ON: |
Venkat Gopalakrishnan | f2a785a | 2016-10-17 17:10:53 -0700 | [diff] [blame] | 1695 | /* |
| 1696 | * Wait for the ungate work to complete if in progress. |
| 1697 | * Though the clocks may be in ON state, the link could |
| 1698 | * still be in hibner8 state if hibern8 is allowed |
| 1699 | * during clock gating. |
| 1700 | * Make sure we exit hibern8 state also in addition to |
| 1701 | * clocks being ON. |
| 1702 | */ |
| 1703 | if (ufshcd_can_hibern8_during_gating(hba) && |
| 1704 | ufshcd_is_link_hibern8(hba)) { |
Can Guo | c63d609 | 2020-02-10 19:40:48 -0800 | [diff] [blame] | 1705 | if (async) { |
| 1706 | rc = -EAGAIN; |
| 1707 | hba->clk_gating.active_reqs--; |
| 1708 | break; |
| 1709 | } |
Venkat Gopalakrishnan | f2a785a | 2016-10-17 17:10:53 -0700 | [diff] [blame] | 1710 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Stanley Chu | 93b6c5d | 2020-08-09 13:07:34 +0800 | [diff] [blame] | 1711 | flush_result = flush_work(&hba->clk_gating.ungate_work); |
| 1712 | if (hba->clk_gating.is_suspended && !flush_result) |
| 1713 | goto out; |
Venkat Gopalakrishnan | f2a785a | 2016-10-17 17:10:53 -0700 | [diff] [blame] | 1714 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1715 | goto start; |
| 1716 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1717 | break; |
| 1718 | case REQ_CLKS_OFF: |
| 1719 | if (cancel_delayed_work(&hba->clk_gating.gate_work)) { |
| 1720 | hba->clk_gating.state = CLKS_ON; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1721 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1722 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1723 | break; |
| 1724 | } |
| 1725 | /* |
Tomohiro Kusumi | 9c490d2 | 2017-03-28 16:49:26 +0300 | [diff] [blame] | 1726 | * If we are here, it means gating work is either done or |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1727 | * currently running. Hence, fall through to cancel gating |
| 1728 | * work and to enable clocks. |
| 1729 | */ |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1730 | fallthrough; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1731 | case CLKS_OFF: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1732 | hba->clk_gating.state = REQ_CLKS_ON; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1733 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1734 | hba->clk_gating.state); |
Can Guo | da3fecb | 2020-11-02 22:24:39 -0800 | [diff] [blame] | 1735 | if (queue_work(hba->clk_gating.clk_gating_workq, |
| 1736 | &hba->clk_gating.ungate_work)) |
| 1737 | ufshcd_scsi_block_requests(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1738 | /* |
| 1739 | * fall through to check if we should wait for this |
| 1740 | * work to be done or not. |
| 1741 | */ |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1742 | fallthrough; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1743 | case REQ_CLKS_ON: |
| 1744 | if (async) { |
| 1745 | rc = -EAGAIN; |
| 1746 | hba->clk_gating.active_reqs--; |
| 1747 | break; |
| 1748 | } |
| 1749 | |
| 1750 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1751 | flush_work(&hba->clk_gating.ungate_work); |
| 1752 | /* Make sure state is CLKS_ON before returning */ |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1753 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1754 | goto start; |
| 1755 | default: |
| 1756 | dev_err(hba->dev, "%s: clk gating is in invalid state %d\n", |
| 1757 | __func__, hba->clk_gating.state); |
| 1758 | break; |
| 1759 | } |
| 1760 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1761 | out: |
| 1762 | return rc; |
| 1763 | } |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 1764 | EXPORT_SYMBOL_GPL(ufshcd_hold); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1765 | |
| 1766 | static void ufshcd_gate_work(struct work_struct *work) |
| 1767 | { |
| 1768 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1769 | clk_gating.gate_work.work); |
| 1770 | unsigned long flags; |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 1771 | int ret; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1772 | |
| 1773 | spin_lock_irqsave(hba->host->host_lock, flags); |
Venkat Gopalakrishnan | 3f0c06d | 2016-10-17 17:11:07 -0700 | [diff] [blame] | 1774 | /* |
| 1775 | * In case you are here to cancel this work the gating state |
| 1776 | * would be marked as REQ_CLKS_ON. In this case save time by |
| 1777 | * skipping the gating work and exit after changing the clock |
| 1778 | * state to CLKS_ON. |
| 1779 | */ |
| 1780 | if (hba->clk_gating.is_suspended || |
Asutosh Das | 18f01374 | 2019-11-14 22:09:29 -0800 | [diff] [blame] | 1781 | (hba->clk_gating.state != REQ_CLKS_OFF)) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1782 | hba->clk_gating.state = CLKS_ON; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1783 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1784 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1785 | goto rel_lock; |
| 1786 | } |
| 1787 | |
| 1788 | if (hba->clk_gating.active_reqs |
| 1789 | || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 1790 | || ufshcd_any_tag_in_use(hba) || hba->outstanding_tasks |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1791 | || hba->active_uic_cmd || hba->uic_async_done) |
| 1792 | goto rel_lock; |
| 1793 | |
| 1794 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1795 | |
| 1796 | /* put the link into hibern8 mode before turning off clocks */ |
| 1797 | if (ufshcd_can_hibern8_during_gating(hba)) { |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 1798 | ret = ufshcd_uic_hibern8_enter(hba); |
| 1799 | if (ret) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1800 | hba->clk_gating.state = CLKS_ON; |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 1801 | dev_err(hba->dev, "%s: hibern8 enter failed %d\n", |
| 1802 | __func__, ret); |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1803 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1804 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1805 | goto out; |
| 1806 | } |
| 1807 | ufshcd_set_link_hibern8(hba); |
| 1808 | } |
| 1809 | |
Stanley Chu | 8b0bbf0 | 2019-12-07 20:22:01 +0800 | [diff] [blame] | 1810 | ufshcd_disable_irq(hba); |
| 1811 | |
Can Guo | 81309c2 | 2020-11-25 18:01:00 -0800 | [diff] [blame] | 1812 | ufshcd_setup_clocks(hba, false); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1813 | |
Can Guo | dd7143e | 2020-10-27 12:10:36 -0700 | [diff] [blame] | 1814 | /* Put the host controller in low power mode if possible */ |
| 1815 | ufshcd_hba_vreg_set_lpm(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1816 | /* |
| 1817 | * In case you are here to cancel this work the gating state |
| 1818 | * would be marked as REQ_CLKS_ON. In this case keep the state |
| 1819 | * as REQ_CLKS_ON which would anyway imply that clocks are off |
| 1820 | * and a request to turn them on is pending. By doing this way, |
| 1821 | * we keep the state machine in tact and this would ultimately |
| 1822 | * prevent from doing cancel work multiple times when there are |
| 1823 | * new requests arriving before the current cancel work is done. |
| 1824 | */ |
| 1825 | spin_lock_irqsave(hba->host->host_lock, flags); |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1826 | if (hba->clk_gating.state == REQ_CLKS_OFF) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1827 | hba->clk_gating.state = CLKS_OFF; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1828 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1829 | hba->clk_gating.state); |
| 1830 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1831 | rel_lock: |
| 1832 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1833 | out: |
| 1834 | return; |
| 1835 | } |
| 1836 | |
| 1837 | /* host lock must be held before calling this variant */ |
| 1838 | static void __ufshcd_release(struct ufs_hba *hba) |
| 1839 | { |
| 1840 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1841 | return; |
| 1842 | |
| 1843 | hba->clk_gating.active_reqs--; |
| 1844 | |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 1845 | if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended || |
| 1846 | hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL || |
Jaegeuk Kim | 8eb456b | 2020-11-17 08:58:38 -0800 | [diff] [blame] | 1847 | hba->outstanding_tasks || |
Jaegeuk Kim | fd62de1 | 2020-11-17 08:58:33 -0800 | [diff] [blame] | 1848 | hba->active_uic_cmd || hba->uic_async_done || |
| 1849 | hba->clk_gating.state == CLKS_OFF) |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1850 | return; |
| 1851 | |
| 1852 | hba->clk_gating.state = REQ_CLKS_OFF; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1853 | trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state); |
Evan Green | f4bb770 | 2018-10-05 10:27:32 -0700 | [diff] [blame] | 1854 | queue_delayed_work(hba->clk_gating.clk_gating_workq, |
| 1855 | &hba->clk_gating.gate_work, |
| 1856 | msecs_to_jiffies(hba->clk_gating.delay_ms)); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | void ufshcd_release(struct ufs_hba *hba) |
| 1860 | { |
| 1861 | unsigned long flags; |
| 1862 | |
| 1863 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1864 | __ufshcd_release(hba); |
| 1865 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1866 | } |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 1867 | EXPORT_SYMBOL_GPL(ufshcd_release); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1868 | |
| 1869 | static ssize_t ufshcd_clkgate_delay_show(struct device *dev, |
| 1870 | struct device_attribute *attr, char *buf) |
| 1871 | { |
| 1872 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1873 | |
| 1874 | return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms); |
| 1875 | } |
| 1876 | |
| 1877 | static ssize_t ufshcd_clkgate_delay_store(struct device *dev, |
| 1878 | struct device_attribute *attr, const char *buf, size_t count) |
| 1879 | { |
| 1880 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1881 | unsigned long flags, value; |
| 1882 | |
| 1883 | if (kstrtoul(buf, 0, &value)) |
| 1884 | return -EINVAL; |
| 1885 | |
| 1886 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1887 | hba->clk_gating.delay_ms = value; |
| 1888 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1889 | return count; |
| 1890 | } |
| 1891 | |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1892 | static ssize_t ufshcd_clkgate_enable_show(struct device *dev, |
| 1893 | struct device_attribute *attr, char *buf) |
| 1894 | { |
| 1895 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1896 | |
| 1897 | return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_gating.is_enabled); |
| 1898 | } |
| 1899 | |
| 1900 | static ssize_t ufshcd_clkgate_enable_store(struct device *dev, |
| 1901 | struct device_attribute *attr, const char *buf, size_t count) |
| 1902 | { |
| 1903 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1904 | unsigned long flags; |
| 1905 | u32 value; |
| 1906 | |
| 1907 | if (kstrtou32(buf, 0, &value)) |
| 1908 | return -EINVAL; |
| 1909 | |
| 1910 | value = !!value; |
Jaegeuk Kim | b664511 | 2020-11-17 08:58:34 -0800 | [diff] [blame] | 1911 | |
| 1912 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1913 | if (value == hba->clk_gating.is_enabled) |
| 1914 | goto out; |
| 1915 | |
Jaegeuk Kim | b664511 | 2020-11-17 08:58:34 -0800 | [diff] [blame] | 1916 | if (value) |
| 1917 | __ufshcd_release(hba); |
| 1918 | else |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1919 | hba->clk_gating.active_reqs++; |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1920 | |
| 1921 | hba->clk_gating.is_enabled = value; |
| 1922 | out: |
Jaegeuk Kim | b664511 | 2020-11-17 08:58:34 -0800 | [diff] [blame] | 1923 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1924 | return count; |
| 1925 | } |
| 1926 | |
Can Guo | 4543d9d | 2021-01-20 02:04:22 -0800 | [diff] [blame] | 1927 | static void ufshcd_init_clk_gating_sysfs(struct ufs_hba *hba) |
Vivek Gautam | eebcc19 | 2018-08-07 23:17:39 +0530 | [diff] [blame] | 1928 | { |
Can Guo | 4543d9d | 2021-01-20 02:04:22 -0800 | [diff] [blame] | 1929 | hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show; |
| 1930 | hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store; |
| 1931 | sysfs_attr_init(&hba->clk_gating.delay_attr.attr); |
| 1932 | hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms"; |
| 1933 | hba->clk_gating.delay_attr.attr.mode = 0644; |
| 1934 | if (device_create_file(hba->dev, &hba->clk_gating.delay_attr)) |
| 1935 | dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n"); |
Vivek Gautam | eebcc19 | 2018-08-07 23:17:39 +0530 | [diff] [blame] | 1936 | |
Can Guo | 4543d9d | 2021-01-20 02:04:22 -0800 | [diff] [blame] | 1937 | hba->clk_gating.enable_attr.show = ufshcd_clkgate_enable_show; |
| 1938 | hba->clk_gating.enable_attr.store = ufshcd_clkgate_enable_store; |
| 1939 | sysfs_attr_init(&hba->clk_gating.enable_attr.attr); |
| 1940 | hba->clk_gating.enable_attr.attr.name = "clkgate_enable"; |
| 1941 | hba->clk_gating.enable_attr.attr.mode = 0644; |
| 1942 | if (device_create_file(hba->dev, &hba->clk_gating.enable_attr)) |
| 1943 | dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n"); |
Vivek Gautam | eebcc19 | 2018-08-07 23:17:39 +0530 | [diff] [blame] | 1944 | } |
| 1945 | |
Can Guo | 4543d9d | 2021-01-20 02:04:22 -0800 | [diff] [blame] | 1946 | static void ufshcd_remove_clk_gating_sysfs(struct ufs_hba *hba) |
Vivek Gautam | eebcc19 | 2018-08-07 23:17:39 +0530 | [diff] [blame] | 1947 | { |
Can Guo | 4543d9d | 2021-01-20 02:04:22 -0800 | [diff] [blame] | 1948 | if (hba->clk_gating.delay_attr.attr.name) |
| 1949 | device_remove_file(hba->dev, &hba->clk_gating.delay_attr); |
| 1950 | if (hba->clk_gating.enable_attr.attr.name) |
| 1951 | device_remove_file(hba->dev, &hba->clk_gating.enable_attr); |
Vivek Gautam | eebcc19 | 2018-08-07 23:17:39 +0530 | [diff] [blame] | 1952 | } |
| 1953 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1954 | static void ufshcd_init_clk_gating(struct ufs_hba *hba) |
| 1955 | { |
Vijay Viswanath | 10e5e37 | 2018-05-03 16:37:22 +0530 | [diff] [blame] | 1956 | char wq_name[sizeof("ufs_clk_gating_00")]; |
| 1957 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1958 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1959 | return; |
| 1960 | |
Can Guo | 2dec947 | 2020-08-09 05:15:47 -0700 | [diff] [blame] | 1961 | hba->clk_gating.state = CLKS_ON; |
| 1962 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1963 | hba->clk_gating.delay_ms = 150; |
| 1964 | INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work); |
| 1965 | INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work); |
| 1966 | |
Vijay Viswanath | 10e5e37 | 2018-05-03 16:37:22 +0530 | [diff] [blame] | 1967 | snprintf(wq_name, ARRAY_SIZE(wq_name), "ufs_clk_gating_%d", |
| 1968 | hba->host->host_no); |
| 1969 | hba->clk_gating.clk_gating_workq = alloc_ordered_workqueue(wq_name, |
Jaegeuk Kim | e93e6e4 | 2020-11-17 08:58:36 -0800 | [diff] [blame] | 1970 | WQ_MEM_RECLAIM | WQ_HIGHPRI); |
Vijay Viswanath | 10e5e37 | 2018-05-03 16:37:22 +0530 | [diff] [blame] | 1971 | |
Can Guo | 4543d9d | 2021-01-20 02:04:22 -0800 | [diff] [blame] | 1972 | ufshcd_init_clk_gating_sysfs(hba); |
| 1973 | |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1974 | hba->clk_gating.is_enabled = true; |
Can Guo | 4543d9d | 2021-01-20 02:04:22 -0800 | [diff] [blame] | 1975 | hba->clk_gating.is_initialized = true; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | static void ufshcd_exit_clk_gating(struct ufs_hba *hba) |
| 1979 | { |
Can Guo | 4543d9d | 2021-01-20 02:04:22 -0800 | [diff] [blame] | 1980 | if (!hba->clk_gating.is_initialized) |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1981 | return; |
Can Guo | 4543d9d | 2021-01-20 02:04:22 -0800 | [diff] [blame] | 1982 | ufshcd_remove_clk_gating_sysfs(hba); |
Akinobu Mita | 97cd680 | 2014-11-24 14:24:18 +0900 | [diff] [blame] | 1983 | cancel_work_sync(&hba->clk_gating.ungate_work); |
| 1984 | cancel_delayed_work_sync(&hba->clk_gating.gate_work); |
Vijay Viswanath | 10e5e37 | 2018-05-03 16:37:22 +0530 | [diff] [blame] | 1985 | destroy_workqueue(hba->clk_gating.clk_gating_workq); |
Can Guo | 4543d9d | 2021-01-20 02:04:22 -0800 | [diff] [blame] | 1986 | hba->clk_gating.is_initialized = false; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1987 | } |
| 1988 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1989 | /* Must be called with host lock acquired */ |
| 1990 | static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba) |
| 1991 | { |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1992 | bool queue_resume_work = false; |
Stanley Chu | b1bf66d | 2020-06-11 18:10:43 +0800 | [diff] [blame] | 1993 | ktime_t curr_t = ktime_get(); |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1994 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1995 | if (!ufshcd_is_clkscaling_supported(hba)) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1996 | return; |
| 1997 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1998 | if (!hba->clk_scaling.active_reqs++) |
| 1999 | queue_resume_work = true; |
| 2000 | |
Can Guo | 0e9d4ca | 2021-01-20 02:04:21 -0800 | [diff] [blame] | 2001 | if (!hba->clk_scaling.is_enabled || hba->pm_op_in_progress) |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 2002 | return; |
| 2003 | |
| 2004 | if (queue_resume_work) |
| 2005 | queue_work(hba->clk_scaling.workq, |
| 2006 | &hba->clk_scaling.resume_work); |
| 2007 | |
| 2008 | if (!hba->clk_scaling.window_start_t) { |
Stanley Chu | b1bf66d | 2020-06-11 18:10:43 +0800 | [diff] [blame] | 2009 | hba->clk_scaling.window_start_t = curr_t; |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 2010 | hba->clk_scaling.tot_busy_t = 0; |
| 2011 | hba->clk_scaling.is_busy_started = false; |
| 2012 | } |
| 2013 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 2014 | if (!hba->clk_scaling.is_busy_started) { |
Stanley Chu | b1bf66d | 2020-06-11 18:10:43 +0800 | [diff] [blame] | 2015 | hba->clk_scaling.busy_start_t = curr_t; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 2016 | hba->clk_scaling.is_busy_started = true; |
| 2017 | } |
| 2018 | } |
| 2019 | |
| 2020 | static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba) |
| 2021 | { |
| 2022 | struct ufs_clk_scaling *scaling = &hba->clk_scaling; |
| 2023 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 2024 | if (!ufshcd_is_clkscaling_supported(hba)) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 2025 | return; |
| 2026 | |
| 2027 | if (!hba->outstanding_reqs && scaling->is_busy_started) { |
| 2028 | scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(), |
| 2029 | scaling->busy_start_t)); |
Thomas Gleixner | 8b0e195 | 2016-12-25 12:30:41 +0100 | [diff] [blame] | 2030 | scaling->busy_start_t = 0; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 2031 | scaling->is_busy_started = false; |
| 2032 | } |
| 2033 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2034 | /** |
| 2035 | * ufshcd_send_command - Send SCSI or device management commands |
| 2036 | * @hba: per adapter instance |
| 2037 | * @task_tag: Task tag of the command |
| 2038 | */ |
| 2039 | static inline |
| 2040 | void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag) |
| 2041 | { |
Stanley Chu | 6edfdcf | 2020-07-06 14:07:07 +0800 | [diff] [blame] | 2042 | struct ufshcd_lrb *lrbp = &hba->lrb[task_tag]; |
| 2043 | |
| 2044 | lrbp->issue_time_stamp = ktime_get(); |
| 2045 | lrbp->compl_time_stamp = ktime_set(0, 0); |
| 2046 | ufshcd_vops_setup_xfer_req(hba, task_tag, (lrbp->cmd ? true : false)); |
Bean Huo | 28fa68f | 2021-01-05 12:34:42 +0100 | [diff] [blame] | 2047 | ufshcd_add_command_trace(hba, task_tag, UFS_CMD_SEND); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 2048 | ufshcd_clk_scaling_start_busy(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2049 | __set_bit(task_tag, &hba->outstanding_reqs); |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 2050 | ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
Gilad Broner | ad1a1b9 | 2016-10-17 17:09:36 -0700 | [diff] [blame] | 2051 | /* Make sure that doorbell is committed immediately */ |
| 2052 | wmb(); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | /** |
| 2056 | * 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] | 2057 | * @lrbp: pointer to local reference block |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2058 | */ |
| 2059 | static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp) |
| 2060 | { |
| 2061 | int len; |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 2062 | if (lrbp->sense_buffer && |
| 2063 | ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) { |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 2064 | int len_to_copy; |
| 2065 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2066 | len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len); |
Avri Altman | 09a5a24 | 2018-11-22 20:04:56 +0200 | [diff] [blame] | 2067 | len_to_copy = min_t(int, UFS_SENSE_SIZE, len); |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 2068 | |
Avri Altman | 09a5a24 | 2018-11-22 20:04:56 +0200 | [diff] [blame] | 2069 | memcpy(lrbp->sense_buffer, lrbp->ucd_rsp_ptr->sr.sense_data, |
| 2070 | len_to_copy); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2071 | } |
| 2072 | } |
| 2073 | |
| 2074 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2075 | * ufshcd_copy_query_response() - Copy the Query Response and the data |
| 2076 | * descriptor |
| 2077 | * @hba: per adapter instance |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2078 | * @lrbp: pointer to local reference block |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2079 | */ |
| 2080 | static |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2081 | 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] | 2082 | { |
| 2083 | struct ufs_query_res *query_res = &hba->dev_cmd.query.response; |
| 2084 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2085 | 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] | 2086 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2087 | /* Get the descriptor */ |
Avri Altman | 1c90836 | 2019-05-21 11:24:22 +0300 | [diff] [blame] | 2088 | if (hba->dev_cmd.query.descriptor && |
| 2089 | lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2090 | u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2091 | GENERAL_UPIU_REQUEST_SIZE; |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2092 | u16 resp_len; |
| 2093 | u16 buf_len; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2094 | |
| 2095 | /* data segment length */ |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2096 | resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) & |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2097 | MASK_QUERY_DATA_SEG_LEN; |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 2098 | buf_len = be16_to_cpu( |
| 2099 | hba->dev_cmd.query.request.upiu_req.length); |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2100 | if (likely(buf_len >= resp_len)) { |
| 2101 | memcpy(hba->dev_cmd.query.descriptor, descp, resp_len); |
| 2102 | } else { |
| 2103 | dev_warn(hba->dev, |
Bean Huo | 3d4881d | 2019-11-12 23:34:35 +0100 | [diff] [blame] | 2104 | "%s: rsp size %d is bigger than buffer size %d", |
| 2105 | __func__, resp_len, buf_len); |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2106 | return -EINVAL; |
| 2107 | } |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2108 | } |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2109 | |
| 2110 | return 0; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2111 | } |
| 2112 | |
| 2113 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2114 | * ufshcd_hba_capabilities - Read controller capabilities |
| 2115 | * @hba: per adapter instance |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 2116 | * |
| 2117 | * Return: 0 on success, negative on error. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2118 | */ |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 2119 | static inline int ufshcd_hba_capabilities(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2120 | { |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 2121 | int err; |
| 2122 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 2123 | hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2124 | |
| 2125 | /* nutrs and nutmrs are 0 based values */ |
| 2126 | hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1; |
| 2127 | hba->nutmrs = |
| 2128 | ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1; |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 2129 | |
| 2130 | /* Read crypto capabilities */ |
| 2131 | err = ufshcd_hba_init_crypto_capabilities(hba); |
| 2132 | if (err) |
| 2133 | dev_err(hba->dev, "crypto setup failed\n"); |
| 2134 | |
| 2135 | return err; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2136 | } |
| 2137 | |
| 2138 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2139 | * ufshcd_ready_for_uic_cmd - Check if controller is ready |
| 2140 | * to accept UIC commands |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2141 | * @hba: per adapter instance |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2142 | * Return true on success, else false |
| 2143 | */ |
| 2144 | static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba) |
| 2145 | { |
| 2146 | if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY) |
| 2147 | return true; |
| 2148 | else |
| 2149 | return false; |
| 2150 | } |
| 2151 | |
| 2152 | /** |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 2153 | * ufshcd_get_upmcrs - Get the power mode change request status |
| 2154 | * @hba: Pointer to adapter instance |
| 2155 | * |
| 2156 | * This function gets the UPMCRS field of HCS register |
| 2157 | * Returns value of UPMCRS field |
| 2158 | */ |
| 2159 | static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba) |
| 2160 | { |
| 2161 | return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7; |
| 2162 | } |
| 2163 | |
| 2164 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2165 | * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers |
| 2166 | * @hba: per adapter instance |
| 2167 | * @uic_cmd: UIC command |
| 2168 | * |
| 2169 | * Mutex must be held. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2170 | */ |
| 2171 | static inline void |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2172 | 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] | 2173 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2174 | WARN_ON(hba->active_uic_cmd); |
| 2175 | |
| 2176 | hba->active_uic_cmd = uic_cmd; |
| 2177 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2178 | /* Write Args */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2179 | ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1); |
| 2180 | ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2); |
| 2181 | ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2182 | |
Bean Huo | 28fa68f | 2021-01-05 12:34:42 +0100 | [diff] [blame] | 2183 | ufshcd_add_uic_command_trace(hba, uic_cmd, UFS_CMD_SEND); |
Stanley Chu | aa5c697 | 2020-06-15 15:22:35 +0800 | [diff] [blame] | 2184 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2185 | /* Write UIC Cmd */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2186 | ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK, |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 2187 | REG_UIC_COMMAND); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2188 | } |
| 2189 | |
| 2190 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2191 | * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command |
| 2192 | * @hba: per adapter instance |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2193 | * @uic_cmd: UIC command |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2194 | * |
| 2195 | * Must be called with mutex held. |
| 2196 | * Returns 0 only if success. |
| 2197 | */ |
| 2198 | static int |
| 2199 | ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) |
| 2200 | { |
| 2201 | int ret; |
| 2202 | unsigned long flags; |
| 2203 | |
| 2204 | if (wait_for_completion_timeout(&uic_cmd->done, |
Can Guo | 0f52fcb9 | 2020-11-02 22:24:40 -0800 | [diff] [blame] | 2205 | msecs_to_jiffies(UIC_CMD_TIMEOUT))) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2206 | ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT; |
Can Guo | 0f52fcb9 | 2020-11-02 22:24:40 -0800 | [diff] [blame] | 2207 | } else { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2208 | ret = -ETIMEDOUT; |
Can Guo | 0f52fcb9 | 2020-11-02 22:24:40 -0800 | [diff] [blame] | 2209 | dev_err(hba->dev, |
| 2210 | "uic cmd 0x%x with arg3 0x%x completion timeout\n", |
| 2211 | uic_cmd->command, uic_cmd->argument3); |
| 2212 | |
| 2213 | if (!uic_cmd->cmd_active) { |
| 2214 | dev_err(hba->dev, "%s: UIC cmd has been completed, return the result\n", |
| 2215 | __func__); |
| 2216 | ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT; |
| 2217 | } |
| 2218 | } |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2219 | |
| 2220 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2221 | hba->active_uic_cmd = NULL; |
| 2222 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2223 | |
| 2224 | return ret; |
| 2225 | } |
| 2226 | |
| 2227 | /** |
| 2228 | * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result |
| 2229 | * @hba: per adapter instance |
| 2230 | * @uic_cmd: UIC command |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2231 | * @completion: initialize the completion only if this is set to true |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2232 | * |
| 2233 | * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2234 | * with mutex held and host_lock locked. |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2235 | * Returns 0 only if success. |
| 2236 | */ |
| 2237 | static int |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2238 | __ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd, |
| 2239 | bool completion) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2240 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2241 | if (!ufshcd_ready_for_uic_cmd(hba)) { |
| 2242 | dev_err(hba->dev, |
| 2243 | "Controller not ready to accept UIC commands\n"); |
| 2244 | return -EIO; |
| 2245 | } |
| 2246 | |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2247 | if (completion) |
| 2248 | init_completion(&uic_cmd->done); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2249 | |
Can Guo | 0f52fcb9 | 2020-11-02 22:24:40 -0800 | [diff] [blame] | 2250 | uic_cmd->cmd_active = 1; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2251 | ufshcd_dispatch_uic_cmd(hba, uic_cmd); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2252 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2253 | return 0; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2254 | } |
| 2255 | |
| 2256 | /** |
| 2257 | * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result |
| 2258 | * @hba: per adapter instance |
| 2259 | * @uic_cmd: UIC command |
| 2260 | * |
| 2261 | * Returns 0 only if success. |
| 2262 | */ |
Avri Altman | e77044c5 | 2018-10-07 17:30:39 +0300 | [diff] [blame] | 2263 | 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] | 2264 | { |
| 2265 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2266 | unsigned long flags; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2267 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2268 | ufshcd_hold(hba, false); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2269 | mutex_lock(&hba->uic_cmd_mutex); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 2270 | ufshcd_add_delay_before_dme_cmd(hba); |
| 2271 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2272 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2273 | ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2274 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2275 | if (!ret) |
| 2276 | ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd); |
| 2277 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2278 | mutex_unlock(&hba->uic_cmd_mutex); |
| 2279 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2280 | ufshcd_release(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2281 | return ret; |
| 2282 | } |
| 2283 | |
| 2284 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2285 | * ufshcd_map_sg - Map scatter-gather list to prdt |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2286 | * @hba: per adapter instance |
| 2287 | * @lrbp: pointer to local reference block |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2288 | * |
| 2289 | * Returns 0 in case of success, non-zero value in case of failure |
| 2290 | */ |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 2291 | 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] | 2292 | { |
| 2293 | struct ufshcd_sg_entry *prd_table; |
| 2294 | struct scatterlist *sg; |
| 2295 | struct scsi_cmnd *cmd; |
| 2296 | int sg_segments; |
| 2297 | int i; |
| 2298 | |
| 2299 | cmd = lrbp->cmd; |
| 2300 | sg_segments = scsi_dma_map(cmd); |
| 2301 | if (sg_segments < 0) |
| 2302 | return sg_segments; |
| 2303 | |
| 2304 | if (sg_segments) { |
Alim Akhtar | 26f968d | 2020-05-28 06:46:52 +0530 | [diff] [blame] | 2305 | |
| 2306 | if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) |
| 2307 | lrbp->utr_descriptor_ptr->prd_table_length = |
| 2308 | cpu_to_le16((sg_segments * |
| 2309 | sizeof(struct ufshcd_sg_entry))); |
| 2310 | else |
| 2311 | lrbp->utr_descriptor_ptr->prd_table_length = |
| 2312 | cpu_to_le16((u16) (sg_segments)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2313 | |
| 2314 | prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr; |
| 2315 | |
| 2316 | scsi_for_each_sg(cmd, sg, sg_segments, i) { |
| 2317 | prd_table[i].size = |
| 2318 | cpu_to_le32(((u32) sg_dma_len(sg))-1); |
| 2319 | prd_table[i].base_addr = |
| 2320 | cpu_to_le32(lower_32_bits(sg->dma_address)); |
| 2321 | prd_table[i].upper_addr = |
| 2322 | cpu_to_le32(upper_32_bits(sg->dma_address)); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2323 | prd_table[i].reserved = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2324 | } |
| 2325 | } else { |
| 2326 | lrbp->utr_descriptor_ptr->prd_table_length = 0; |
| 2327 | } |
| 2328 | |
| 2329 | return 0; |
| 2330 | } |
| 2331 | |
| 2332 | /** |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2333 | * ufshcd_enable_intr - enable interrupts |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2334 | * @hba: per adapter instance |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2335 | * @intrs: interrupt bits |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2336 | */ |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2337 | static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2338 | { |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2339 | u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
| 2340 | |
| 2341 | if (hba->ufs_version == UFSHCI_VERSION_10) { |
| 2342 | u32 rw; |
| 2343 | rw = set & INTERRUPT_MASK_RW_VER_10; |
| 2344 | set = rw | ((set ^ intrs) & intrs); |
| 2345 | } else { |
| 2346 | set |= intrs; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2347 | } |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2348 | |
| 2349 | ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE); |
| 2350 | } |
| 2351 | |
| 2352 | /** |
| 2353 | * ufshcd_disable_intr - disable interrupts |
| 2354 | * @hba: per adapter instance |
| 2355 | * @intrs: interrupt bits |
| 2356 | */ |
| 2357 | static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs) |
| 2358 | { |
| 2359 | u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
| 2360 | |
| 2361 | if (hba->ufs_version == UFSHCI_VERSION_10) { |
| 2362 | u32 rw; |
| 2363 | rw = (set & INTERRUPT_MASK_RW_VER_10) & |
| 2364 | ~(intrs & INTERRUPT_MASK_RW_VER_10); |
| 2365 | set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10); |
| 2366 | |
| 2367 | } else { |
| 2368 | set &= ~intrs; |
| 2369 | } |
| 2370 | |
| 2371 | ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2375 | * ufshcd_prepare_req_desc_hdr() - Fills the requests header |
| 2376 | * descriptor according to request |
| 2377 | * @lrbp: pointer to local reference block |
| 2378 | * @upiu_flags: flags required in the header |
| 2379 | * @cmd_dir: requests data direction |
| 2380 | */ |
| 2381 | static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp, |
Bean Huo | a23064c | 2020-07-06 14:39:36 +0200 | [diff] [blame] | 2382 | u8 *upiu_flags, enum dma_data_direction cmd_dir) |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2383 | { |
| 2384 | struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr; |
| 2385 | u32 data_direction; |
| 2386 | u32 dword_0; |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 2387 | u32 dword_1 = 0; |
| 2388 | u32 dword_3 = 0; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2389 | |
| 2390 | if (cmd_dir == DMA_FROM_DEVICE) { |
| 2391 | data_direction = UTP_DEVICE_TO_HOST; |
| 2392 | *upiu_flags = UPIU_CMD_FLAGS_READ; |
| 2393 | } else if (cmd_dir == DMA_TO_DEVICE) { |
| 2394 | data_direction = UTP_HOST_TO_DEVICE; |
| 2395 | *upiu_flags = UPIU_CMD_FLAGS_WRITE; |
| 2396 | } else { |
| 2397 | data_direction = UTP_NO_DATA_TRANSFER; |
| 2398 | *upiu_flags = UPIU_CMD_FLAGS_NONE; |
| 2399 | } |
| 2400 | |
| 2401 | dword_0 = data_direction | (lrbp->command_type |
| 2402 | << UPIU_COMMAND_TYPE_OFFSET); |
| 2403 | if (lrbp->intr_cmd) |
| 2404 | dword_0 |= UTP_REQ_DESC_INT_CMD; |
| 2405 | |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 2406 | /* Prepare crypto related dwords */ |
| 2407 | ufshcd_prepare_req_desc_hdr_crypto(lrbp, &dword_0, &dword_1, &dword_3); |
| 2408 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2409 | /* Transfer request descriptor header fields */ |
| 2410 | req_desc->header.dword_0 = cpu_to_le32(dword_0); |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 2411 | req_desc->header.dword_1 = cpu_to_le32(dword_1); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2412 | /* |
| 2413 | * assigning invalid value for command status. Controller |
| 2414 | * updates OCS on command completion, with the command |
| 2415 | * status |
| 2416 | */ |
| 2417 | req_desc->header.dword_2 = |
| 2418 | cpu_to_le32(OCS_INVALID_COMMAND_STATUS); |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 2419 | req_desc->header.dword_3 = cpu_to_le32(dword_3); |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 2420 | |
| 2421 | req_desc->prd_table_length = 0; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | /** |
| 2425 | * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc, |
| 2426 | * for scsi commands |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2427 | * @lrbp: local reference block pointer |
| 2428 | * @upiu_flags: flags |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2429 | */ |
| 2430 | static |
Bean Huo | a23064c | 2020-07-06 14:39:36 +0200 | [diff] [blame] | 2431 | 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] | 2432 | { |
Bart Van Assche | 1b21b8f | 2019-12-24 14:02:45 -0800 | [diff] [blame] | 2433 | struct scsi_cmnd *cmd = lrbp->cmd; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2434 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2435 | unsigned short cdb_len; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2436 | |
| 2437 | /* command descriptor fields */ |
| 2438 | ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD( |
| 2439 | UPIU_TRANSACTION_COMMAND, upiu_flags, |
| 2440 | lrbp->lun, lrbp->task_tag); |
| 2441 | ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD( |
| 2442 | UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0); |
| 2443 | |
| 2444 | /* Total EHS length and Data segment length will be zero */ |
| 2445 | ucd_req_ptr->header.dword_2 = 0; |
| 2446 | |
Bart Van Assche | 1b21b8f | 2019-12-24 14:02:45 -0800 | [diff] [blame] | 2447 | 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] | 2448 | |
Bart Van Assche | 1b21b8f | 2019-12-24 14:02:45 -0800 | [diff] [blame] | 2449 | cdb_len = min_t(unsigned short, cmd->cmd_len, UFS_CDB_SIZE); |
Avri Altman | a851b2b | 2018-10-07 17:30:34 +0300 | [diff] [blame] | 2450 | memset(ucd_req_ptr->sc.cdb, 0, UFS_CDB_SIZE); |
Bart Van Assche | 1b21b8f | 2019-12-24 14:02:45 -0800 | [diff] [blame] | 2451 | memcpy(ucd_req_ptr->sc.cdb, cmd->cmnd, cdb_len); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2452 | |
| 2453 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2454 | } |
| 2455 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2456 | /** |
| 2457 | * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc, |
| 2458 | * for query requsts |
| 2459 | * @hba: UFS hba |
| 2460 | * @lrbp: local reference block pointer |
| 2461 | * @upiu_flags: flags |
| 2462 | */ |
| 2463 | static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba, |
Bean Huo | a23064c | 2020-07-06 14:39:36 +0200 | [diff] [blame] | 2464 | struct ufshcd_lrb *lrbp, u8 upiu_flags) |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2465 | { |
| 2466 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
| 2467 | struct ufs_query *query = &hba->dev_cmd.query; |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 2468 | u16 len = be16_to_cpu(query->request.upiu_req.length); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2469 | |
| 2470 | /* Query request header */ |
| 2471 | ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD( |
| 2472 | UPIU_TRANSACTION_QUERY_REQ, upiu_flags, |
| 2473 | lrbp->lun, lrbp->task_tag); |
| 2474 | ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD( |
| 2475 | 0, query->request.query_func, 0, 0); |
| 2476 | |
Zang Leigang | 6861285 | 2016-08-25 17:39:19 +0800 | [diff] [blame] | 2477 | /* Data segment length only need for WRITE_DESC */ |
| 2478 | if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) |
| 2479 | ucd_req_ptr->header.dword_2 = |
| 2480 | UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len); |
| 2481 | else |
| 2482 | ucd_req_ptr->header.dword_2 = 0; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2483 | |
| 2484 | /* Copy the Query Request buffer as is */ |
| 2485 | memcpy(&ucd_req_ptr->qr, &query->request.upiu_req, |
| 2486 | QUERY_OSF_SIZE); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2487 | |
| 2488 | /* Copy the Descriptor */ |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2489 | if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) |
Avri Altman | 220d17a6 | 2018-10-07 17:30:36 +0300 | [diff] [blame] | 2490 | memcpy(ucd_req_ptr + 1, query->descriptor, len); |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2491 | |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 2492 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2493 | } |
| 2494 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2495 | static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp) |
| 2496 | { |
| 2497 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
| 2498 | |
| 2499 | memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req)); |
| 2500 | |
| 2501 | /* command descriptor fields */ |
| 2502 | ucd_req_ptr->header.dword_0 = |
| 2503 | UPIU_HEADER_DWORD( |
| 2504 | UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag); |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 2505 | /* clear rest of the fields of basic header */ |
| 2506 | ucd_req_ptr->header.dword_1 = 0; |
| 2507 | ucd_req_ptr->header.dword_2 = 0; |
| 2508 | |
| 2509 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2510 | } |
| 2511 | |
| 2512 | /** |
Bean Huo | f273c54 | 2020-08-14 11:50:33 +0200 | [diff] [blame] | 2513 | * ufshcd_compose_devman_upiu - UFS Protocol Information Unit(UPIU) |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2514 | * for Device Management Purposes |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2515 | * @hba: per adapter instance |
| 2516 | * @lrbp: pointer to local reference block |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2517 | */ |
Bean Huo | f273c54 | 2020-08-14 11:50:33 +0200 | [diff] [blame] | 2518 | static int ufshcd_compose_devman_upiu(struct ufs_hba *hba, |
| 2519 | struct ufshcd_lrb *lrbp) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2520 | { |
Bean Huo | a23064c | 2020-07-06 14:39:36 +0200 | [diff] [blame] | 2521 | u8 upiu_flags; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2522 | int ret = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2523 | |
kehuanlin | 83dc7e3 | 2017-09-06 17:58:39 +0800 | [diff] [blame] | 2524 | if ((hba->ufs_version == UFSHCI_VERSION_10) || |
| 2525 | (hba->ufs_version == UFSHCI_VERSION_11)) |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2526 | lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE; |
kehuanlin | 83dc7e3 | 2017-09-06 17:58:39 +0800 | [diff] [blame] | 2527 | else |
| 2528 | lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE; |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2529 | |
| 2530 | ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE); |
| 2531 | if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY) |
| 2532 | ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags); |
| 2533 | else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP) |
| 2534 | ufshcd_prepare_utp_nop_upiu(lrbp); |
| 2535 | else |
| 2536 | ret = -EINVAL; |
| 2537 | |
| 2538 | return ret; |
| 2539 | } |
| 2540 | |
| 2541 | /** |
| 2542 | * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU) |
| 2543 | * for SCSI Purposes |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2544 | * @hba: per adapter instance |
| 2545 | * @lrbp: pointer to local reference block |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2546 | */ |
| 2547 | static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 2548 | { |
Bean Huo | a23064c | 2020-07-06 14:39:36 +0200 | [diff] [blame] | 2549 | u8 upiu_flags; |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2550 | int ret = 0; |
| 2551 | |
kehuanlin | 83dc7e3 | 2017-09-06 17:58:39 +0800 | [diff] [blame] | 2552 | if ((hba->ufs_version == UFSHCI_VERSION_10) || |
| 2553 | (hba->ufs_version == UFSHCI_VERSION_11)) |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2554 | lrbp->command_type = UTP_CMD_TYPE_SCSI; |
kehuanlin | 83dc7e3 | 2017-09-06 17:58:39 +0800 | [diff] [blame] | 2555 | else |
| 2556 | lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE; |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2557 | |
| 2558 | if (likely(lrbp->cmd)) { |
| 2559 | ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, |
| 2560 | lrbp->cmd->sc_data_direction); |
| 2561 | ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags); |
| 2562 | } else { |
| 2563 | ret = -EINVAL; |
| 2564 | } |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2565 | |
| 2566 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2567 | } |
| 2568 | |
| 2569 | /** |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 2570 | * 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] | 2571 | * @upiu_wlun_id: UPIU W-LUN id |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 2572 | * |
| 2573 | * Returns SCSI W-LUN id |
| 2574 | */ |
| 2575 | static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id) |
| 2576 | { |
| 2577 | return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE; |
| 2578 | } |
| 2579 | |
Bart Van Assche | 4d2b8d4 | 2020-01-22 19:56:35 -0800 | [diff] [blame] | 2580 | static void ufshcd_init_lrb(struct ufs_hba *hba, struct ufshcd_lrb *lrb, int i) |
| 2581 | { |
| 2582 | struct utp_transfer_cmd_desc *cmd_descp = hba->ucdl_base_addr; |
| 2583 | struct utp_transfer_req_desc *utrdlp = hba->utrdl_base_addr; |
| 2584 | dma_addr_t cmd_desc_element_addr = hba->ucdl_dma_addr + |
| 2585 | i * sizeof(struct utp_transfer_cmd_desc); |
| 2586 | u16 response_offset = offsetof(struct utp_transfer_cmd_desc, |
| 2587 | response_upiu); |
| 2588 | u16 prdt_offset = offsetof(struct utp_transfer_cmd_desc, prd_table); |
| 2589 | |
| 2590 | lrb->utr_descriptor_ptr = utrdlp + i; |
| 2591 | lrb->utrd_dma_addr = hba->utrdl_dma_addr + |
| 2592 | i * sizeof(struct utp_transfer_req_desc); |
| 2593 | lrb->ucd_req_ptr = (struct utp_upiu_req *)(cmd_descp + i); |
| 2594 | lrb->ucd_req_dma_addr = cmd_desc_element_addr; |
| 2595 | lrb->ucd_rsp_ptr = (struct utp_upiu_rsp *)cmd_descp[i].response_upiu; |
| 2596 | lrb->ucd_rsp_dma_addr = cmd_desc_element_addr + response_offset; |
| 2597 | lrb->ucd_prdt_ptr = (struct ufshcd_sg_entry *)cmd_descp[i].prd_table; |
| 2598 | lrb->ucd_prdt_dma_addr = cmd_desc_element_addr + prdt_offset; |
| 2599 | } |
| 2600 | |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 2601 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2602 | * ufshcd_queuecommand - main entry point for SCSI requests |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2603 | * @host: SCSI host pointer |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2604 | * @cmd: command from SCSI Midlayer |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2605 | * |
| 2606 | * Returns 0 for success, non-zero in case of failure |
| 2607 | */ |
| 2608 | static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) |
| 2609 | { |
| 2610 | struct ufshcd_lrb *lrbp; |
| 2611 | struct ufs_hba *hba; |
| 2612 | unsigned long flags; |
| 2613 | int tag; |
| 2614 | int err = 0; |
| 2615 | |
| 2616 | hba = shost_priv(host); |
| 2617 | |
| 2618 | tag = cmd->request->tag; |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 2619 | if (!ufshcd_valid_tag(hba, tag)) { |
| 2620 | dev_err(hba->dev, |
| 2621 | "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p", |
| 2622 | __func__, tag, cmd, cmd->request); |
| 2623 | BUG(); |
| 2624 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2625 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 2626 | if (!down_read_trylock(&hba->clk_scaling_lock)) |
| 2627 | return SCSI_MLQUEUE_HOST_BUSY; |
| 2628 | |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 2629 | hba->req_abort_count = 0; |
| 2630 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2631 | err = ufshcd_hold(hba, true); |
| 2632 | if (err) { |
| 2633 | err = SCSI_MLQUEUE_HOST_BUSY; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2634 | goto out; |
| 2635 | } |
Can Guo | 2dec947 | 2020-08-09 05:15:47 -0700 | [diff] [blame] | 2636 | WARN_ON(ufshcd_is_clkgating_allowed(hba) && |
| 2637 | (hba->clk_gating.state != CLKS_ON)); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2638 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2639 | lrbp = &hba->lrb[tag]; |
Can Guo | 7a7e66c | 2020-12-02 04:04:02 -0800 | [diff] [blame] | 2640 | if (unlikely(lrbp->in_use)) { |
| 2641 | if (hba->pm_op_in_progress) |
| 2642 | set_host_byte(cmd, DID_BAD_TARGET); |
| 2643 | else |
| 2644 | err = SCSI_MLQUEUE_HOST_BUSY; |
| 2645 | ufshcd_release(hba); |
| 2646 | goto out; |
| 2647 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2648 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2649 | WARN_ON(lrbp->cmd); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2650 | lrbp->cmd = cmd; |
Avri Altman | 09a5a24 | 2018-11-22 20:04:56 +0200 | [diff] [blame] | 2651 | lrbp->sense_bufflen = UFS_SENSE_SIZE; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2652 | lrbp->sense_buffer = cmd->sense_buffer; |
| 2653 | lrbp->task_tag = tag; |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 2654 | lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun); |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 2655 | lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false; |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 2656 | |
| 2657 | ufshcd_prepare_lrbp_crypto(cmd->request, lrbp); |
| 2658 | |
Gilad Broner | e0b299e | 2017-02-03 16:56:40 -0800 | [diff] [blame] | 2659 | lrbp->req_abort_skip = false; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2660 | |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2661 | ufshcd_comp_scsi_upiu(hba, lrbp); |
| 2662 | |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 2663 | err = ufshcd_map_sg(hba, lrbp); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2664 | if (err) { |
| 2665 | lrbp->cmd = NULL; |
Can Guo | 17c7d35 | 2019-12-05 02:14:33 +0000 | [diff] [blame] | 2666 | ufshcd_release(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2667 | goto out; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2668 | } |
Gilad Broner | ad1a1b9 | 2016-10-17 17:09:36 -0700 | [diff] [blame] | 2669 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 2670 | wmb(); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2671 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2672 | spin_lock_irqsave(hba->host->host_lock, flags); |
Can Guo | 5586dd8 | 2020-08-09 05:15:54 -0700 | [diff] [blame] | 2673 | switch (hba->ufshcd_state) { |
| 2674 | case UFSHCD_STATE_OPERATIONAL: |
| 2675 | case UFSHCD_STATE_EH_SCHEDULED_NON_FATAL: |
| 2676 | break; |
| 2677 | case UFSHCD_STATE_EH_SCHEDULED_FATAL: |
| 2678 | /* |
| 2679 | * pm_runtime_get_sync() is used at error handling preparation |
| 2680 | * stage. If a scsi cmd, e.g. the SSU cmd, is sent from hba's |
| 2681 | * PM ops, it can never be finished if we let SCSI layer keep |
| 2682 | * retrying it, which gets err handler stuck forever. Neither |
| 2683 | * can we let the scsi cmd pass through, because UFS is in bad |
| 2684 | * state, the scsi cmd may eventually time out, which will get |
| 2685 | * err handler blocked for too long. So, just fail the scsi cmd |
| 2686 | * sent from PM ops, err handler can recover PM error anyways. |
| 2687 | */ |
| 2688 | if (hba->pm_op_in_progress) { |
| 2689 | hba->force_reset = true; |
| 2690 | set_host_byte(cmd, DID_BAD_TARGET); |
| 2691 | goto out_compl_cmd; |
| 2692 | } |
| 2693 | fallthrough; |
| 2694 | case UFSHCD_STATE_RESET: |
| 2695 | err = SCSI_MLQUEUE_HOST_BUSY; |
| 2696 | goto out_compl_cmd; |
| 2697 | case UFSHCD_STATE_ERROR: |
| 2698 | set_host_byte(cmd, DID_ERROR); |
| 2699 | goto out_compl_cmd; |
| 2700 | default: |
| 2701 | dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n", |
| 2702 | __func__, hba->ufshcd_state); |
| 2703 | set_host_byte(cmd, DID_BAD_TARGET); |
| 2704 | goto out_compl_cmd; |
| 2705 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2706 | ufshcd_send_command(hba, tag); |
| 2707 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Can Guo | 5586dd8 | 2020-08-09 05:15:54 -0700 | [diff] [blame] | 2708 | goto out; |
| 2709 | |
| 2710 | out_compl_cmd: |
| 2711 | scsi_dma_unmap(lrbp->cmd); |
| 2712 | lrbp->cmd = NULL; |
| 2713 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2714 | ufshcd_release(hba); |
| 2715 | if (!err) |
| 2716 | cmd->scsi_done(cmd); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2717 | out: |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 2718 | up_read(&hba->clk_scaling_lock); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2719 | return err; |
| 2720 | } |
| 2721 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2722 | static int ufshcd_compose_dev_cmd(struct ufs_hba *hba, |
| 2723 | struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag) |
| 2724 | { |
| 2725 | lrbp->cmd = NULL; |
| 2726 | lrbp->sense_bufflen = 0; |
| 2727 | lrbp->sense_buffer = NULL; |
| 2728 | lrbp->task_tag = tag; |
| 2729 | 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] | 2730 | lrbp->intr_cmd = true; /* No interrupt aggregation */ |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 2731 | ufshcd_prepare_lrbp_crypto(NULL, lrbp); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2732 | hba->dev_cmd.type = cmd_type; |
| 2733 | |
Bean Huo | f273c54 | 2020-08-14 11:50:33 +0200 | [diff] [blame] | 2734 | return ufshcd_compose_devman_upiu(hba, lrbp); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2735 | } |
| 2736 | |
| 2737 | static int |
| 2738 | ufshcd_clear_cmd(struct ufs_hba *hba, int tag) |
| 2739 | { |
| 2740 | int err = 0; |
| 2741 | unsigned long flags; |
| 2742 | u32 mask = 1 << tag; |
| 2743 | |
| 2744 | /* clear outstanding transaction before retry */ |
| 2745 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2746 | ufshcd_utrl_clear(hba, tag); |
| 2747 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2748 | |
| 2749 | /* |
| 2750 | * wait for for h/w to clear corresponding bit in door-bell. |
| 2751 | * max. wait is 1 sec. |
| 2752 | */ |
| 2753 | err = ufshcd_wait_for_register(hba, |
| 2754 | REG_UTP_TRANSFER_REQ_DOOR_BELL, |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 2755 | mask, ~mask, 1000, 1000); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2756 | |
| 2757 | return err; |
| 2758 | } |
| 2759 | |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2760 | static int |
| 2761 | ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 2762 | { |
| 2763 | struct ufs_query_res *query_res = &hba->dev_cmd.query.response; |
| 2764 | |
| 2765 | /* Get the UPIU response */ |
| 2766 | query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >> |
| 2767 | UPIU_RSP_CODE_OFFSET; |
| 2768 | return query_res->response; |
| 2769 | } |
| 2770 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2771 | /** |
| 2772 | * ufshcd_dev_cmd_completion() - handles device management command responses |
| 2773 | * @hba: per adapter instance |
| 2774 | * @lrbp: pointer to local reference block |
| 2775 | */ |
| 2776 | static int |
| 2777 | ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 2778 | { |
| 2779 | int resp; |
| 2780 | int err = 0; |
| 2781 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 2782 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2783 | resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr); |
| 2784 | |
| 2785 | switch (resp) { |
| 2786 | case UPIU_TRANSACTION_NOP_IN: |
| 2787 | if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) { |
| 2788 | err = -EINVAL; |
| 2789 | dev_err(hba->dev, "%s: unexpected response %x\n", |
| 2790 | __func__, resp); |
| 2791 | } |
| 2792 | break; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2793 | case UPIU_TRANSACTION_QUERY_RSP: |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2794 | err = ufshcd_check_query_response(hba, lrbp); |
| 2795 | if (!err) |
| 2796 | err = ufshcd_copy_query_response(hba, lrbp); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2797 | break; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2798 | case UPIU_TRANSACTION_REJECT_UPIU: |
| 2799 | /* TODO: handle Reject UPIU Response */ |
| 2800 | err = -EPERM; |
| 2801 | dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n", |
| 2802 | __func__); |
| 2803 | break; |
| 2804 | default: |
| 2805 | err = -EINVAL; |
| 2806 | dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n", |
| 2807 | __func__, resp); |
| 2808 | break; |
| 2809 | } |
| 2810 | |
| 2811 | return err; |
| 2812 | } |
| 2813 | |
| 2814 | static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba, |
| 2815 | struct ufshcd_lrb *lrbp, int max_timeout) |
| 2816 | { |
| 2817 | int err = 0; |
| 2818 | unsigned long time_left; |
| 2819 | unsigned long flags; |
| 2820 | |
| 2821 | time_left = wait_for_completion_timeout(hba->dev_cmd.complete, |
| 2822 | msecs_to_jiffies(max_timeout)); |
| 2823 | |
Gilad Broner | ad1a1b9 | 2016-10-17 17:09:36 -0700 | [diff] [blame] | 2824 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 2825 | wmb(); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2826 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2827 | hba->dev_cmd.complete = NULL; |
| 2828 | if (likely(time_left)) { |
| 2829 | err = ufshcd_get_tr_ocs(lrbp); |
| 2830 | if (!err) |
| 2831 | err = ufshcd_dev_cmd_completion(hba, lrbp); |
| 2832 | } |
| 2833 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2834 | |
| 2835 | if (!time_left) { |
| 2836 | err = -ETIMEDOUT; |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 2837 | dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n", |
| 2838 | __func__, lrbp->task_tag); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2839 | if (!ufshcd_clear_cmd(hba, lrbp->task_tag)) |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 2840 | /* successfully cleared the command, retry if needed */ |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2841 | err = -EAGAIN; |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 2842 | /* |
| 2843 | * in case of an error, after clearing the doorbell, |
| 2844 | * we also need to clear the outstanding_request |
| 2845 | * field in hba |
| 2846 | */ |
| 2847 | ufshcd_outstanding_req_clear(hba, lrbp->task_tag); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2848 | } |
| 2849 | |
| 2850 | return err; |
| 2851 | } |
| 2852 | |
| 2853 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2854 | * ufshcd_exec_dev_cmd - API for sending device management requests |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2855 | * @hba: UFS hba |
| 2856 | * @cmd_type: specifies the type (NOP, Query...) |
| 2857 | * @timeout: time in seconds |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2858 | * |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2859 | * NOTE: Since there is only one available tag for device management commands, |
| 2860 | * it is expected you hold the hba->dev_cmd.lock mutex. |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2861 | */ |
| 2862 | static int ufshcd_exec_dev_cmd(struct ufs_hba *hba, |
| 2863 | enum dev_cmd_type cmd_type, int timeout) |
| 2864 | { |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 2865 | struct request_queue *q = hba->cmd_queue; |
| 2866 | struct request *req; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2867 | struct ufshcd_lrb *lrbp; |
| 2868 | int err; |
| 2869 | int tag; |
| 2870 | struct completion wait; |
| 2871 | unsigned long flags; |
| 2872 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 2873 | down_read(&hba->clk_scaling_lock); |
| 2874 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2875 | /* |
| 2876 | * Get free slot, sleep if slots are unavailable. |
| 2877 | * Even though we use wait_event() which sleeps indefinitely, |
| 2878 | * the maximum wait time is bounded by SCSI request timeout. |
| 2879 | */ |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 2880 | req = blk_get_request(q, REQ_OP_DRV_OUT, 0); |
Dan Carpenter | bb14dd1 | 2019-12-13 13:48:28 +0300 | [diff] [blame] | 2881 | if (IS_ERR(req)) { |
| 2882 | err = PTR_ERR(req); |
| 2883 | goto out_unlock; |
| 2884 | } |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 2885 | tag = req->tag; |
| 2886 | WARN_ON_ONCE(!ufshcd_valid_tag(hba, tag)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2887 | |
| 2888 | init_completion(&wait); |
| 2889 | lrbp = &hba->lrb[tag]; |
Can Guo | 7a7e66c | 2020-12-02 04:04:02 -0800 | [diff] [blame] | 2890 | if (unlikely(lrbp->in_use)) { |
| 2891 | err = -EBUSY; |
| 2892 | goto out; |
| 2893 | } |
| 2894 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2895 | WARN_ON(lrbp->cmd); |
| 2896 | err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag); |
| 2897 | if (unlikely(err)) |
| 2898 | goto out_put_tag; |
| 2899 | |
| 2900 | hba->dev_cmd.complete = &wait; |
| 2901 | |
Avri Altman | fb475b7 | 2021-01-10 10:46:18 +0200 | [diff] [blame] | 2902 | ufshcd_add_query_upiu_trace(hba, UFS_QUERY_SEND, lrbp->ucd_req_ptr); |
Yaniv Gardi | e3dfdc5 | 2016-02-01 15:02:49 +0200 | [diff] [blame] | 2903 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 2904 | wmb(); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2905 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2906 | ufshcd_send_command(hba, tag); |
| 2907 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2908 | |
| 2909 | err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout); |
| 2910 | |
Can Guo | 7a7e66c | 2020-12-02 04:04:02 -0800 | [diff] [blame] | 2911 | out: |
Avri Altman | fb475b7 | 2021-01-10 10:46:18 +0200 | [diff] [blame] | 2912 | ufshcd_add_query_upiu_trace(hba, err ? UFS_QUERY_ERR : UFS_QUERY_COMP, |
| 2913 | (struct utp_upiu_req *)lrbp->ucd_rsp_ptr); |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 2914 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2915 | out_put_tag: |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 2916 | blk_put_request(req); |
Dan Carpenter | bb14dd1 | 2019-12-13 13:48:28 +0300 | [diff] [blame] | 2917 | out_unlock: |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 2918 | up_read(&hba->clk_scaling_lock); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2919 | return err; |
| 2920 | } |
| 2921 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2922 | /** |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2923 | * ufshcd_init_query() - init the query response and request parameters |
| 2924 | * @hba: per-adapter instance |
| 2925 | * @request: address of the request pointer to be initialized |
| 2926 | * @response: address of the response pointer to be initialized |
| 2927 | * @opcode: operation to perform |
| 2928 | * @idn: flag idn to access |
| 2929 | * @index: LU number to access |
| 2930 | * @selector: query/flag/descriptor further identification |
| 2931 | */ |
| 2932 | static inline void ufshcd_init_query(struct ufs_hba *hba, |
| 2933 | struct ufs_query_req **request, struct ufs_query_res **response, |
| 2934 | enum query_opcode opcode, u8 idn, u8 index, u8 selector) |
| 2935 | { |
| 2936 | *request = &hba->dev_cmd.query.request; |
| 2937 | *response = &hba->dev_cmd.query.response; |
| 2938 | memset(*request, 0, sizeof(struct ufs_query_req)); |
| 2939 | memset(*response, 0, sizeof(struct ufs_query_res)); |
| 2940 | (*request)->upiu_req.opcode = opcode; |
| 2941 | (*request)->upiu_req.idn = idn; |
| 2942 | (*request)->upiu_req.index = index; |
| 2943 | (*request)->upiu_req.selector = selector; |
| 2944 | } |
| 2945 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 2946 | static int ufshcd_query_flag_retry(struct ufs_hba *hba, |
Stanley Chu | 1f34eed | 2020-05-08 16:01:12 +0800 | [diff] [blame] | 2947 | 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] | 2948 | { |
| 2949 | int ret; |
| 2950 | int retries; |
| 2951 | |
| 2952 | for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) { |
Stanley Chu | 1f34eed | 2020-05-08 16:01:12 +0800 | [diff] [blame] | 2953 | ret = ufshcd_query_flag(hba, opcode, idn, index, flag_res); |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 2954 | if (ret) |
| 2955 | dev_dbg(hba->dev, |
| 2956 | "%s: failed with error %d, retries %d\n", |
| 2957 | __func__, ret, retries); |
| 2958 | else |
| 2959 | break; |
| 2960 | } |
| 2961 | |
| 2962 | if (ret) |
| 2963 | dev_err(hba->dev, |
| 2964 | "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n", |
| 2965 | __func__, opcode, idn, ret, retries); |
| 2966 | return ret; |
| 2967 | } |
| 2968 | |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2969 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2970 | * ufshcd_query_flag() - API function for sending flag query requests |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2971 | * @hba: per-adapter instance |
| 2972 | * @opcode: flag query to perform |
| 2973 | * @idn: flag idn to access |
Stanley Chu | 1f34eed | 2020-05-08 16:01:12 +0800 | [diff] [blame] | 2974 | * @index: flag index to access |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2975 | * @flag_res: the flag value after the query request completes |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2976 | * |
| 2977 | * Returns 0 for success, non-zero in case of failure |
| 2978 | */ |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 2979 | int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode, |
Stanley Chu | 1f34eed | 2020-05-08 16:01:12 +0800 | [diff] [blame] | 2980 | enum flag_idn idn, u8 index, bool *flag_res) |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2981 | { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2982 | struct ufs_query_req *request = NULL; |
| 2983 | struct ufs_query_res *response = NULL; |
Stanley Chu | 1f34eed | 2020-05-08 16:01:12 +0800 | [diff] [blame] | 2984 | int err, selector = 0; |
Yaniv Gardi | e5ad406 | 2016-02-01 15:02:41 +0200 | [diff] [blame] | 2985 | int timeout = QUERY_REQ_TIMEOUT; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2986 | |
| 2987 | BUG_ON(!hba); |
| 2988 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2989 | ufshcd_hold(hba, false); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2990 | mutex_lock(&hba->dev_cmd.lock); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2991 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 2992 | selector); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2993 | |
| 2994 | switch (opcode) { |
| 2995 | case UPIU_QUERY_OPCODE_SET_FLAG: |
| 2996 | case UPIU_QUERY_OPCODE_CLEAR_FLAG: |
| 2997 | case UPIU_QUERY_OPCODE_TOGGLE_FLAG: |
| 2998 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
| 2999 | break; |
| 3000 | case UPIU_QUERY_OPCODE_READ_FLAG: |
| 3001 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 3002 | if (!flag_res) { |
| 3003 | /* No dummy reads */ |
| 3004 | dev_err(hba->dev, "%s: Invalid argument for read request\n", |
| 3005 | __func__); |
| 3006 | err = -EINVAL; |
| 3007 | goto out_unlock; |
| 3008 | } |
| 3009 | break; |
| 3010 | default: |
| 3011 | dev_err(hba->dev, |
| 3012 | "%s: Expected query flag opcode but got = %d\n", |
| 3013 | __func__, opcode); |
| 3014 | err = -EINVAL; |
| 3015 | goto out_unlock; |
| 3016 | } |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3017 | |
Yaniv Gardi | e5ad406 | 2016-02-01 15:02:41 +0200 | [diff] [blame] | 3018 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3019 | |
| 3020 | if (err) { |
| 3021 | dev_err(hba->dev, |
| 3022 | "%s: Sending flag query for idn %d failed, err = %d\n", |
| 3023 | __func__, idn, err); |
| 3024 | goto out_unlock; |
| 3025 | } |
| 3026 | |
| 3027 | if (flag_res) |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 3028 | *flag_res = (be32_to_cpu(response->upiu_res.value) & |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3029 | MASK_QUERY_UPIU_FLAG_LOC) & 0x1; |
| 3030 | |
| 3031 | out_unlock: |
| 3032 | mutex_unlock(&hba->dev_cmd.lock); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3033 | ufshcd_release(hba); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3034 | return err; |
| 3035 | } |
| 3036 | |
| 3037 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3038 | * ufshcd_query_attr - API function for sending attribute requests |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 3039 | * @hba: per-adapter instance |
| 3040 | * @opcode: attribute opcode |
| 3041 | * @idn: attribute idn to access |
| 3042 | * @index: index field |
| 3043 | * @selector: selector field |
| 3044 | * @attr_val: the attribute value after the query request completes |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3045 | * |
| 3046 | * Returns 0 for success, non-zero in case of failure |
| 3047 | */ |
Stanislav Nijnikov | ec92b59 | 2018-02-15 14:14:11 +0200 | [diff] [blame] | 3048 | int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode, |
| 3049 | enum attr_idn idn, u8 index, u8 selector, u32 *attr_val) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3050 | { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3051 | struct ufs_query_req *request = NULL; |
| 3052 | struct ufs_query_res *response = NULL; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3053 | int err; |
| 3054 | |
| 3055 | BUG_ON(!hba); |
| 3056 | |
| 3057 | if (!attr_val) { |
| 3058 | dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n", |
| 3059 | __func__, opcode); |
jintae jang | 8ca1a40 | 2020-12-03 14:25:32 +0900 | [diff] [blame] | 3060 | return -EINVAL; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3061 | } |
| 3062 | |
jintae jang | 8ca1a40 | 2020-12-03 14:25:32 +0900 | [diff] [blame] | 3063 | ufshcd_hold(hba, false); |
| 3064 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3065 | mutex_lock(&hba->dev_cmd.lock); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3066 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 3067 | selector); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3068 | |
| 3069 | switch (opcode) { |
| 3070 | case UPIU_QUERY_OPCODE_WRITE_ATTR: |
| 3071 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 3072 | request->upiu_req.value = cpu_to_be32(*attr_val); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3073 | break; |
| 3074 | case UPIU_QUERY_OPCODE_READ_ATTR: |
| 3075 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 3076 | break; |
| 3077 | default: |
| 3078 | dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n", |
| 3079 | __func__, opcode); |
| 3080 | err = -EINVAL; |
| 3081 | goto out_unlock; |
| 3082 | } |
| 3083 | |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3084 | 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] | 3085 | |
| 3086 | if (err) { |
Yaniv Gardi | 4b761b5 | 2016-11-23 16:31:18 -0800 | [diff] [blame] | 3087 | dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n", |
| 3088 | __func__, opcode, idn, index, err); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3089 | goto out_unlock; |
| 3090 | } |
| 3091 | |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 3092 | *attr_val = be32_to_cpu(response->upiu_res.value); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3093 | |
| 3094 | out_unlock: |
| 3095 | mutex_unlock(&hba->dev_cmd.lock); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3096 | ufshcd_release(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3097 | return err; |
| 3098 | } |
| 3099 | |
| 3100 | /** |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 3101 | * ufshcd_query_attr_retry() - API function for sending query |
| 3102 | * attribute with retries |
| 3103 | * @hba: per-adapter instance |
| 3104 | * @opcode: attribute opcode |
| 3105 | * @idn: attribute idn to access |
| 3106 | * @index: index field |
| 3107 | * @selector: selector field |
| 3108 | * @attr_val: the attribute value after the query request |
| 3109 | * completes |
| 3110 | * |
| 3111 | * Returns 0 for success, non-zero in case of failure |
| 3112 | */ |
| 3113 | static int ufshcd_query_attr_retry(struct ufs_hba *hba, |
| 3114 | enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector, |
| 3115 | u32 *attr_val) |
| 3116 | { |
| 3117 | int ret = 0; |
| 3118 | u32 retries; |
| 3119 | |
Bart Van Assche | 68c9fcf | 2019-12-24 14:02:43 -0800 | [diff] [blame] | 3120 | for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 3121 | ret = ufshcd_query_attr(hba, opcode, idn, index, |
| 3122 | selector, attr_val); |
| 3123 | if (ret) |
| 3124 | dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n", |
| 3125 | __func__, ret, retries); |
| 3126 | else |
| 3127 | break; |
| 3128 | } |
| 3129 | |
| 3130 | if (ret) |
| 3131 | dev_err(hba->dev, |
| 3132 | "%s: query attribute, idn %d, failed with error %d after %d retires\n", |
| 3133 | __func__, idn, ret, QUERY_REQ_RETRIES); |
| 3134 | return ret; |
| 3135 | } |
| 3136 | |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3137 | static int __ufshcd_query_descriptor(struct ufs_hba *hba, |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3138 | enum query_opcode opcode, enum desc_idn idn, u8 index, |
| 3139 | u8 selector, u8 *desc_buf, int *buf_len) |
| 3140 | { |
| 3141 | struct ufs_query_req *request = NULL; |
| 3142 | struct ufs_query_res *response = NULL; |
| 3143 | int err; |
| 3144 | |
| 3145 | BUG_ON(!hba); |
| 3146 | |
| 3147 | if (!desc_buf) { |
| 3148 | dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n", |
| 3149 | __func__, opcode); |
jintae jang | 8ca1a40 | 2020-12-03 14:25:32 +0900 | [diff] [blame] | 3150 | return -EINVAL; |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3151 | } |
| 3152 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3153 | 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] | 3154 | dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n", |
| 3155 | __func__, *buf_len); |
jintae jang | 8ca1a40 | 2020-12-03 14:25:32 +0900 | [diff] [blame] | 3156 | return -EINVAL; |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3157 | } |
| 3158 | |
jintae jang | 8ca1a40 | 2020-12-03 14:25:32 +0900 | [diff] [blame] | 3159 | ufshcd_hold(hba, false); |
| 3160 | |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3161 | mutex_lock(&hba->dev_cmd.lock); |
| 3162 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 3163 | selector); |
| 3164 | hba->dev_cmd.query.descriptor = desc_buf; |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 3165 | request->upiu_req.length = cpu_to_be16(*buf_len); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3166 | |
| 3167 | switch (opcode) { |
| 3168 | case UPIU_QUERY_OPCODE_WRITE_DESC: |
| 3169 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
| 3170 | break; |
| 3171 | case UPIU_QUERY_OPCODE_READ_DESC: |
| 3172 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 3173 | break; |
| 3174 | default: |
| 3175 | dev_err(hba->dev, |
| 3176 | "%s: Expected query descriptor opcode but got = 0x%.2x\n", |
| 3177 | __func__, opcode); |
| 3178 | err = -EINVAL; |
| 3179 | goto out_unlock; |
| 3180 | } |
| 3181 | |
| 3182 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT); |
| 3183 | |
| 3184 | if (err) { |
Yaniv Gardi | 4b761b5 | 2016-11-23 16:31:18 -0800 | [diff] [blame] | 3185 | dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n", |
| 3186 | __func__, opcode, idn, index, err); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3187 | goto out_unlock; |
| 3188 | } |
| 3189 | |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 3190 | *buf_len = be16_to_cpu(response->upiu_res.length); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3191 | |
| 3192 | out_unlock: |
Bean Huo | cfcbae3 | 2019-11-12 23:34:36 +0100 | [diff] [blame] | 3193 | hba->dev_cmd.query.descriptor = NULL; |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3194 | mutex_unlock(&hba->dev_cmd.lock); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3195 | ufshcd_release(hba); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3196 | return err; |
| 3197 | } |
| 3198 | |
| 3199 | /** |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 3200 | * ufshcd_query_descriptor_retry - API function for sending descriptor requests |
| 3201 | * @hba: per-adapter instance |
| 3202 | * @opcode: attribute opcode |
| 3203 | * @idn: attribute idn to access |
| 3204 | * @index: index field |
| 3205 | * @selector: selector field |
| 3206 | * @desc_buf: the buffer that contains the descriptor |
| 3207 | * @buf_len: length parameter passed to the device |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3208 | * |
| 3209 | * Returns 0 for success, non-zero in case of failure. |
| 3210 | * The buf_len parameter will contain, on return, the length parameter |
| 3211 | * received on the response. |
| 3212 | */ |
Stanislav Nijnikov | 2238d31 | 2018-02-15 14:14:07 +0200 | [diff] [blame] | 3213 | int ufshcd_query_descriptor_retry(struct ufs_hba *hba, |
| 3214 | enum query_opcode opcode, |
| 3215 | enum desc_idn idn, u8 index, |
| 3216 | u8 selector, |
| 3217 | u8 *desc_buf, int *buf_len) |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3218 | { |
| 3219 | int err; |
| 3220 | int retries; |
| 3221 | |
| 3222 | for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { |
| 3223 | err = __ufshcd_query_descriptor(hba, opcode, idn, index, |
| 3224 | selector, desc_buf, buf_len); |
| 3225 | if (!err || err == -EINVAL) |
| 3226 | break; |
| 3227 | } |
| 3228 | |
| 3229 | return err; |
| 3230 | } |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3231 | |
| 3232 | /** |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3233 | * ufshcd_map_desc_id_to_length - map descriptor IDN to its length |
| 3234 | * @hba: Pointer to adapter instance |
| 3235 | * @desc_id: descriptor idn value |
| 3236 | * @desc_len: mapped desc length (out) |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3237 | */ |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 3238 | void ufshcd_map_desc_id_to_length(struct ufs_hba *hba, enum desc_idn desc_id, |
| 3239 | int *desc_len) |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3240 | { |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 3241 | if (desc_id >= QUERY_DESC_IDN_MAX || desc_id == QUERY_DESC_IDN_RFU_0 || |
| 3242 | desc_id == QUERY_DESC_IDN_RFU_1) |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3243 | *desc_len = 0; |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 3244 | else |
| 3245 | *desc_len = hba->desc_size[desc_id]; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3246 | } |
| 3247 | EXPORT_SYMBOL(ufshcd_map_desc_id_to_length); |
| 3248 | |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 3249 | static void ufshcd_update_desc_length(struct ufs_hba *hba, |
Bean Huo | 72fb690 | 2020-06-03 11:19:59 +0200 | [diff] [blame] | 3250 | enum desc_idn desc_id, int desc_index, |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 3251 | unsigned char desc_len) |
| 3252 | { |
| 3253 | if (hba->desc_size[desc_id] == QUERY_DESC_MAX_SIZE && |
Bean Huo | 72fb690 | 2020-06-03 11:19:59 +0200 | [diff] [blame] | 3254 | desc_id != QUERY_DESC_IDN_STRING && desc_index != UFS_RPMB_UNIT) |
| 3255 | /* For UFS 3.1, the normal unit descriptor is 10 bytes larger |
| 3256 | * than the RPMB unit, however, both descriptors share the same |
| 3257 | * desc_idn, to cover both unit descriptors with one length, we |
| 3258 | * choose the normal unit descriptor length by desc_index. |
| 3259 | */ |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 3260 | hba->desc_size[desc_id] = desc_len; |
| 3261 | } |
| 3262 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3263 | /** |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3264 | * ufshcd_read_desc_param - read the specified descriptor parameter |
| 3265 | * @hba: Pointer to adapter instance |
| 3266 | * @desc_id: descriptor idn value |
| 3267 | * @desc_index: descriptor index |
| 3268 | * @param_offset: offset of the parameter to read |
| 3269 | * @param_read_buf: pointer to buffer where parameter would be read |
| 3270 | * @param_size: sizeof(param_read_buf) |
| 3271 | * |
| 3272 | * Return 0 in case of success, non-zero otherwise |
| 3273 | */ |
Stanislav Nijnikov | 45bced8 | 2018-02-15 14:14:02 +0200 | [diff] [blame] | 3274 | int ufshcd_read_desc_param(struct ufs_hba *hba, |
| 3275 | enum desc_idn desc_id, |
| 3276 | int desc_index, |
| 3277 | u8 param_offset, |
| 3278 | u8 *param_read_buf, |
| 3279 | u8 param_size) |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3280 | { |
| 3281 | int ret; |
| 3282 | u8 *desc_buf; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3283 | int buff_len; |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3284 | bool is_kmalloc = true; |
| 3285 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3286 | /* Safety check */ |
| 3287 | if (desc_id >= QUERY_DESC_IDN_MAX || !param_size) |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3288 | return -EINVAL; |
| 3289 | |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 3290 | /* Get the length of descriptor */ |
| 3291 | ufshcd_map_desc_id_to_length(hba, desc_id, &buff_len); |
| 3292 | if (!buff_len) { |
Can Guo | 1699f98 | 2020-10-21 22:59:00 -0700 | [diff] [blame] | 3293 | dev_err(hba->dev, "%s: Failed to get desc length\n", __func__); |
| 3294 | return -EINVAL; |
| 3295 | } |
| 3296 | |
| 3297 | if (param_offset >= buff_len) { |
| 3298 | dev_err(hba->dev, "%s: Invalid offset 0x%x in descriptor IDN 0x%x, length 0x%x\n", |
| 3299 | __func__, param_offset, desc_id, buff_len); |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 3300 | return -EINVAL; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3301 | } |
| 3302 | |
| 3303 | /* Check whether we need temp memory */ |
| 3304 | if (param_offset != 0 || param_size < buff_len) { |
Can Guo | 1699f98 | 2020-10-21 22:59:00 -0700 | [diff] [blame] | 3305 | desc_buf = kzalloc(buff_len, GFP_KERNEL); |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3306 | if (!desc_buf) |
| 3307 | return -ENOMEM; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3308 | } else { |
| 3309 | desc_buf = param_read_buf; |
| 3310 | is_kmalloc = false; |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3311 | } |
| 3312 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3313 | /* Request for full descriptor */ |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3314 | ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC, |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3315 | desc_id, desc_index, 0, |
| 3316 | desc_buf, &buff_len); |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3317 | |
subhashj@codeaurora.org | bde44bb | 2016-11-23 16:31:41 -0800 | [diff] [blame] | 3318 | if (ret) { |
Can Guo | 1699f98 | 2020-10-21 22:59:00 -0700 | [diff] [blame] | 3319 | dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d\n", |
subhashj@codeaurora.org | bde44bb | 2016-11-23 16:31:41 -0800 | [diff] [blame] | 3320 | __func__, desc_id, desc_index, param_offset, ret); |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3321 | goto out; |
| 3322 | } |
| 3323 | |
subhashj@codeaurora.org | bde44bb | 2016-11-23 16:31:41 -0800 | [diff] [blame] | 3324 | /* Sanity check */ |
| 3325 | if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) { |
Can Guo | 1699f98 | 2020-10-21 22:59:00 -0700 | [diff] [blame] | 3326 | dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header\n", |
subhashj@codeaurora.org | bde44bb | 2016-11-23 16:31:41 -0800 | [diff] [blame] | 3327 | __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]); |
| 3328 | ret = -EINVAL; |
| 3329 | goto out; |
| 3330 | } |
| 3331 | |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 3332 | /* Update descriptor length */ |
| 3333 | buff_len = desc_buf[QUERY_DESC_LENGTH_OFFSET]; |
Bean Huo | 72fb690 | 2020-06-03 11:19:59 +0200 | [diff] [blame] | 3334 | ufshcd_update_desc_length(hba, desc_id, desc_index, buff_len); |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 3335 | |
Can Guo | 1699f98 | 2020-10-21 22:59:00 -0700 | [diff] [blame] | 3336 | if (is_kmalloc) { |
| 3337 | /* Make sure we don't copy more data than available */ |
| 3338 | if (param_offset + param_size > buff_len) |
| 3339 | param_size = buff_len - param_offset; |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3340 | memcpy(param_read_buf, &desc_buf[param_offset], param_size); |
Can Guo | 1699f98 | 2020-10-21 22:59:00 -0700 | [diff] [blame] | 3341 | } |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3342 | out: |
| 3343 | if (is_kmalloc) |
| 3344 | kfree(desc_buf); |
| 3345 | return ret; |
| 3346 | } |
| 3347 | |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3348 | /** |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3349 | * struct uc_string_id - unicode string |
| 3350 | * |
| 3351 | * @len: size of this descriptor inclusive |
| 3352 | * @type: descriptor type |
| 3353 | * @uc: unicode string character |
| 3354 | */ |
| 3355 | struct uc_string_id { |
| 3356 | u8 len; |
| 3357 | u8 type; |
Gustavo A. R. Silva | ec38c0a | 2020-05-07 14:25:50 -0500 | [diff] [blame] | 3358 | wchar_t uc[]; |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3359 | } __packed; |
| 3360 | |
| 3361 | /* replace non-printable or non-ASCII characters with spaces */ |
| 3362 | static inline char ufshcd_remove_non_printable(u8 ch) |
| 3363 | { |
| 3364 | return (ch >= 0x20 && ch <= 0x7e) ? ch : ' '; |
| 3365 | } |
| 3366 | |
| 3367 | /** |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3368 | * ufshcd_read_string_desc - read string descriptor |
| 3369 | * @hba: pointer to adapter instance |
| 3370 | * @desc_index: descriptor index |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3371 | * @buf: pointer to buffer where descriptor would be read, |
| 3372 | * the caller should free the memory. |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3373 | * @ascii: if true convert from unicode to ascii characters |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3374 | * null terminated string. |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3375 | * |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3376 | * Return: |
| 3377 | * * string size on success. |
| 3378 | * * -ENOMEM: on allocation failure |
| 3379 | * * -EINVAL: on a wrong parameter |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3380 | */ |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3381 | int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index, |
| 3382 | u8 **buf, bool ascii) |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3383 | { |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3384 | struct uc_string_id *uc_str; |
| 3385 | u8 *str; |
| 3386 | int ret; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3387 | |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3388 | if (!buf) |
| 3389 | return -EINVAL; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3390 | |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3391 | uc_str = kzalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL); |
| 3392 | if (!uc_str) |
| 3393 | return -ENOMEM; |
| 3394 | |
Bean Huo | c4607a0 | 2020-06-03 11:19:56 +0200 | [diff] [blame] | 3395 | ret = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_STRING, desc_index, 0, |
| 3396 | (u8 *)uc_str, QUERY_DESC_MAX_SIZE); |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3397 | if (ret < 0) { |
| 3398 | dev_err(hba->dev, "Reading String Desc failed after %d retries. err = %d\n", |
| 3399 | QUERY_REQ_RETRIES, ret); |
| 3400 | str = NULL; |
| 3401 | goto out; |
| 3402 | } |
| 3403 | |
| 3404 | if (uc_str->len <= QUERY_DESC_HDR_SIZE) { |
| 3405 | dev_dbg(hba->dev, "String Desc is of zero length\n"); |
| 3406 | str = NULL; |
| 3407 | ret = 0; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3408 | goto out; |
| 3409 | } |
| 3410 | |
| 3411 | if (ascii) { |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3412 | ssize_t ascii_len; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3413 | int i; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3414 | /* remove header and divide by 2 to move from UTF16 to UTF8 */ |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3415 | ascii_len = (uc_str->len - QUERY_DESC_HDR_SIZE) / 2 + 1; |
| 3416 | str = kzalloc(ascii_len, GFP_KERNEL); |
| 3417 | if (!str) { |
| 3418 | ret = -ENOMEM; |
Tiezhu Yang | fcbefc3 | 2016-06-25 12:35:22 +0800 | [diff] [blame] | 3419 | goto out; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3420 | } |
| 3421 | |
| 3422 | /* |
| 3423 | * the descriptor contains string in UTF16 format |
| 3424 | * we need to convert to utf-8 so it can be displayed |
| 3425 | */ |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3426 | ret = utf16s_to_utf8s(uc_str->uc, |
| 3427 | uc_str->len - QUERY_DESC_HDR_SIZE, |
| 3428 | UTF16_BIG_ENDIAN, str, ascii_len); |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3429 | |
| 3430 | /* replace non-printable or non-ASCII characters with spaces */ |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3431 | for (i = 0; i < ret; i++) |
| 3432 | str[i] = ufshcd_remove_non_printable(str[i]); |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3433 | |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3434 | str[ret++] = '\0'; |
| 3435 | |
| 3436 | } else { |
YueHaibing | 5f57704 | 2019-08-31 12:44:24 +0000 | [diff] [blame] | 3437 | str = kmemdup(uc_str, uc_str->len, GFP_KERNEL); |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3438 | if (!str) { |
| 3439 | ret = -ENOMEM; |
| 3440 | goto out; |
| 3441 | } |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3442 | ret = uc_str->len; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3443 | } |
| 3444 | out: |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3445 | *buf = str; |
| 3446 | kfree(uc_str); |
| 3447 | return ret; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3448 | } |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3449 | |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3450 | /** |
| 3451 | * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter |
| 3452 | * @hba: Pointer to adapter instance |
| 3453 | * @lun: lun id |
| 3454 | * @param_offset: offset of the parameter to read |
| 3455 | * @param_read_buf: pointer to buffer where parameter would be read |
| 3456 | * @param_size: sizeof(param_read_buf) |
| 3457 | * |
| 3458 | * Return 0 in case of success, non-zero otherwise |
| 3459 | */ |
| 3460 | static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba, |
| 3461 | int lun, |
| 3462 | enum unit_desc_param param_offset, |
| 3463 | u8 *param_read_buf, |
| 3464 | u32 param_size) |
| 3465 | { |
| 3466 | /* |
| 3467 | * Unit descriptors are only available for general purpose LUs (LUN id |
| 3468 | * from 0 to 7) and RPMB Well known LU. |
| 3469 | */ |
Jaegeuk Kim | a2fca52 | 2021-01-11 01:59:27 -0800 | [diff] [blame] | 3470 | if (!ufs_is_valid_unit_desc_lun(&hba->dev_info, lun, param_offset)) |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3471 | return -EOPNOTSUPP; |
| 3472 | |
| 3473 | return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun, |
| 3474 | param_offset, param_read_buf, param_size); |
| 3475 | } |
| 3476 | |
Can Guo | 09f1779 | 2020-02-10 19:40:49 -0800 | [diff] [blame] | 3477 | static int ufshcd_get_ref_clk_gating_wait(struct ufs_hba *hba) |
| 3478 | { |
| 3479 | int err = 0; |
| 3480 | u32 gating_wait = UFSHCD_REF_CLK_GATING_WAIT_US; |
| 3481 | |
| 3482 | if (hba->dev_info.wspecversion >= 0x300) { |
| 3483 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
| 3484 | QUERY_ATTR_IDN_REF_CLK_GATING_WAIT_TIME, 0, 0, |
| 3485 | &gating_wait); |
| 3486 | if (err) |
| 3487 | dev_err(hba->dev, "Failed reading bRefClkGatingWait. err = %d, use default %uus\n", |
| 3488 | err, gating_wait); |
| 3489 | |
| 3490 | if (gating_wait == 0) { |
| 3491 | gating_wait = UFSHCD_REF_CLK_GATING_WAIT_US; |
| 3492 | dev_err(hba->dev, "Undefined ref clk gating wait time, use default %uus\n", |
| 3493 | gating_wait); |
| 3494 | } |
| 3495 | |
| 3496 | hba->dev_info.clk_gating_wait_us = gating_wait; |
| 3497 | } |
| 3498 | |
| 3499 | return err; |
| 3500 | } |
| 3501 | |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3502 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3503 | * ufshcd_memory_alloc - allocate memory for host memory space data structures |
| 3504 | * @hba: per adapter instance |
| 3505 | * |
| 3506 | * 1. Allocate DMA memory for Command Descriptor array |
| 3507 | * Each command descriptor consist of Command UPIU, Response UPIU and PRDT |
| 3508 | * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL). |
| 3509 | * 3. Allocate DMA memory for UTP Task Management Request Descriptor List |
| 3510 | * (UTMRDL) |
| 3511 | * 4. Allocate memory for local reference block(lrb). |
| 3512 | * |
| 3513 | * Returns 0 for success, non-zero in case of failure |
| 3514 | */ |
| 3515 | static int ufshcd_memory_alloc(struct ufs_hba *hba) |
| 3516 | { |
| 3517 | size_t utmrdl_size, utrdl_size, ucdl_size; |
| 3518 | |
| 3519 | /* Allocate memory for UTP command descriptors */ |
| 3520 | ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3521 | hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 3522 | ucdl_size, |
| 3523 | &hba->ucdl_dma_addr, |
| 3524 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3525 | |
| 3526 | /* |
| 3527 | * UFSHCI requires UTP command descriptor to be 128 byte aligned. |
| 3528 | * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE |
| 3529 | * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will |
| 3530 | * be aligned to 128 bytes as well |
| 3531 | */ |
| 3532 | if (!hba->ucdl_base_addr || |
| 3533 | WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3534 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3535 | "Command Descriptor Memory allocation failed\n"); |
| 3536 | goto out; |
| 3537 | } |
| 3538 | |
| 3539 | /* |
| 3540 | * Allocate memory for UTP Transfer descriptors |
| 3541 | * UFSHCI requires 1024 byte alignment of UTRD |
| 3542 | */ |
| 3543 | utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3544 | hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 3545 | utrdl_size, |
| 3546 | &hba->utrdl_dma_addr, |
| 3547 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3548 | if (!hba->utrdl_base_addr || |
| 3549 | WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3550 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3551 | "Transfer Descriptor Memory allocation failed\n"); |
| 3552 | goto out; |
| 3553 | } |
| 3554 | |
| 3555 | /* |
| 3556 | * Allocate memory for UTP Task Management descriptors |
| 3557 | * UFSHCI requires 1024 byte alignment of UTMRD |
| 3558 | */ |
| 3559 | utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs; |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3560 | hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 3561 | utmrdl_size, |
| 3562 | &hba->utmrdl_dma_addr, |
| 3563 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3564 | if (!hba->utmrdl_base_addr || |
| 3565 | WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3566 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3567 | "Task Management Descriptor Memory allocation failed\n"); |
| 3568 | goto out; |
| 3569 | } |
| 3570 | |
| 3571 | /* Allocate memory for local reference block */ |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 3572 | hba->lrb = devm_kcalloc(hba->dev, |
| 3573 | hba->nutrs, sizeof(struct ufshcd_lrb), |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3574 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3575 | if (!hba->lrb) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3576 | dev_err(hba->dev, "LRB Memory allocation failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3577 | goto out; |
| 3578 | } |
| 3579 | return 0; |
| 3580 | out: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3581 | return -ENOMEM; |
| 3582 | } |
| 3583 | |
| 3584 | /** |
| 3585 | * ufshcd_host_memory_configure - configure local reference block with |
| 3586 | * memory offsets |
| 3587 | * @hba: per adapter instance |
| 3588 | * |
| 3589 | * Configure Host memory space |
| 3590 | * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA |
| 3591 | * address. |
| 3592 | * 2. Update each UTRD with Response UPIU offset, Response UPIU length |
| 3593 | * and PRDT offset. |
| 3594 | * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT |
| 3595 | * into local reference block. |
| 3596 | */ |
| 3597 | static void ufshcd_host_memory_configure(struct ufs_hba *hba) |
| 3598 | { |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3599 | struct utp_transfer_req_desc *utrdlp; |
| 3600 | dma_addr_t cmd_desc_dma_addr; |
| 3601 | dma_addr_t cmd_desc_element_addr; |
| 3602 | u16 response_offset; |
| 3603 | u16 prdt_offset; |
| 3604 | int cmd_desc_size; |
| 3605 | int i; |
| 3606 | |
| 3607 | utrdlp = hba->utrdl_base_addr; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3608 | |
| 3609 | response_offset = |
| 3610 | offsetof(struct utp_transfer_cmd_desc, response_upiu); |
| 3611 | prdt_offset = |
| 3612 | offsetof(struct utp_transfer_cmd_desc, prd_table); |
| 3613 | |
| 3614 | cmd_desc_size = sizeof(struct utp_transfer_cmd_desc); |
| 3615 | cmd_desc_dma_addr = hba->ucdl_dma_addr; |
| 3616 | |
| 3617 | for (i = 0; i < hba->nutrs; i++) { |
| 3618 | /* Configure UTRD with command descriptor base address */ |
| 3619 | cmd_desc_element_addr = |
| 3620 | (cmd_desc_dma_addr + (cmd_desc_size * i)); |
| 3621 | utrdlp[i].command_desc_base_addr_lo = |
| 3622 | cpu_to_le32(lower_32_bits(cmd_desc_element_addr)); |
| 3623 | utrdlp[i].command_desc_base_addr_hi = |
| 3624 | cpu_to_le32(upper_32_bits(cmd_desc_element_addr)); |
| 3625 | |
| 3626 | /* Response upiu and prdt offset should be in double words */ |
Alim Akhtar | 26f968d | 2020-05-28 06:46:52 +0530 | [diff] [blame] | 3627 | if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) { |
| 3628 | utrdlp[i].response_upiu_offset = |
| 3629 | cpu_to_le16(response_offset); |
| 3630 | utrdlp[i].prd_table_offset = |
| 3631 | cpu_to_le16(prdt_offset); |
| 3632 | utrdlp[i].response_upiu_length = |
| 3633 | cpu_to_le16(ALIGNED_UPIU_SIZE); |
| 3634 | } else { |
| 3635 | utrdlp[i].response_upiu_offset = |
| 3636 | cpu_to_le16(response_offset >> 2); |
| 3637 | utrdlp[i].prd_table_offset = |
| 3638 | cpu_to_le16(prdt_offset >> 2); |
| 3639 | utrdlp[i].response_upiu_length = |
| 3640 | cpu_to_le16(ALIGNED_UPIU_SIZE >> 2); |
| 3641 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3642 | |
Bart Van Assche | 4d2b8d4 | 2020-01-22 19:56:35 -0800 | [diff] [blame] | 3643 | ufshcd_init_lrb(hba, &hba->lrb[i], i); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3644 | } |
| 3645 | } |
| 3646 | |
| 3647 | /** |
| 3648 | * ufshcd_dme_link_startup - Notify Unipro to perform link startup |
| 3649 | * @hba: per adapter instance |
| 3650 | * |
| 3651 | * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer, |
| 3652 | * in order to initialize the Unipro link startup procedure. |
| 3653 | * Once the Unipro links are up, the device connected to the controller |
| 3654 | * is detected. |
| 3655 | * |
| 3656 | * Returns 0 on success, non-zero value on failure |
| 3657 | */ |
| 3658 | static int ufshcd_dme_link_startup(struct ufs_hba *hba) |
| 3659 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3660 | struct uic_command uic_cmd = {0}; |
| 3661 | int ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3662 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3663 | uic_cmd.command = UIC_CMD_DME_LINK_STARTUP; |
| 3664 | |
| 3665 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3666 | if (ret) |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 3667 | dev_dbg(hba->dev, |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3668 | "dme-link-startup: error code %d\n", ret); |
| 3669 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3670 | } |
Alim Akhtar | 39bf2d8 | 2020-05-28 06:46:51 +0530 | [diff] [blame] | 3671 | /** |
| 3672 | * ufshcd_dme_reset - UIC command for DME_RESET |
| 3673 | * @hba: per adapter instance |
| 3674 | * |
| 3675 | * DME_RESET command is issued in order to reset UniPro stack. |
| 3676 | * This function now deals with cold reset. |
| 3677 | * |
| 3678 | * Returns 0 on success, non-zero value on failure |
| 3679 | */ |
| 3680 | static int ufshcd_dme_reset(struct ufs_hba *hba) |
| 3681 | { |
| 3682 | struct uic_command uic_cmd = {0}; |
| 3683 | int ret; |
| 3684 | |
| 3685 | uic_cmd.command = UIC_CMD_DME_RESET; |
| 3686 | |
| 3687 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3688 | if (ret) |
| 3689 | dev_err(hba->dev, |
| 3690 | "dme-reset: error code %d\n", ret); |
| 3691 | |
| 3692 | return ret; |
| 3693 | } |
| 3694 | |
Stanley Chu | fc85a74 | 2020-11-16 14:50:52 +0800 | [diff] [blame] | 3695 | int ufshcd_dme_configure_adapt(struct ufs_hba *hba, |
| 3696 | int agreed_gear, |
| 3697 | int adapt_val) |
| 3698 | { |
| 3699 | int ret; |
| 3700 | |
| 3701 | if (agreed_gear != UFS_HS_G4) |
Bjorn Andersson | 66df79c | 2020-11-20 20:48:10 -0800 | [diff] [blame] | 3702 | adapt_val = PA_NO_ADAPT; |
Stanley Chu | fc85a74 | 2020-11-16 14:50:52 +0800 | [diff] [blame] | 3703 | |
| 3704 | ret = ufshcd_dme_set(hba, |
| 3705 | UIC_ARG_MIB(PA_TXHSADAPTTYPE), |
| 3706 | adapt_val); |
| 3707 | return ret; |
| 3708 | } |
| 3709 | EXPORT_SYMBOL_GPL(ufshcd_dme_configure_adapt); |
| 3710 | |
Alim Akhtar | 39bf2d8 | 2020-05-28 06:46:51 +0530 | [diff] [blame] | 3711 | /** |
| 3712 | * ufshcd_dme_enable - UIC command for DME_ENABLE |
| 3713 | * @hba: per adapter instance |
| 3714 | * |
| 3715 | * DME_ENABLE command is issued in order to enable UniPro stack. |
| 3716 | * |
| 3717 | * Returns 0 on success, non-zero value on failure |
| 3718 | */ |
| 3719 | static int ufshcd_dme_enable(struct ufs_hba *hba) |
| 3720 | { |
| 3721 | struct uic_command uic_cmd = {0}; |
| 3722 | int ret; |
| 3723 | |
| 3724 | uic_cmd.command = UIC_CMD_DME_ENABLE; |
| 3725 | |
| 3726 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3727 | if (ret) |
| 3728 | dev_err(hba->dev, |
Bean Huo | 1fa0570 | 2020-12-07 20:01:37 +0100 | [diff] [blame] | 3729 | "dme-enable: error code %d\n", ret); |
Alim Akhtar | 39bf2d8 | 2020-05-28 06:46:51 +0530 | [diff] [blame] | 3730 | |
| 3731 | return ret; |
| 3732 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3733 | |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 3734 | static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba) |
| 3735 | { |
| 3736 | #define MIN_DELAY_BEFORE_DME_CMDS_US 1000 |
| 3737 | unsigned long min_sleep_time_us; |
| 3738 | |
| 3739 | if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS)) |
| 3740 | return; |
| 3741 | |
| 3742 | /* |
| 3743 | * last_dme_cmd_tstamp will be 0 only for 1st call to |
| 3744 | * this function |
| 3745 | */ |
| 3746 | if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) { |
| 3747 | min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US; |
| 3748 | } else { |
| 3749 | unsigned long delta = |
| 3750 | (unsigned long) ktime_to_us( |
| 3751 | ktime_sub(ktime_get(), |
| 3752 | hba->last_dme_cmd_tstamp)); |
| 3753 | |
| 3754 | if (delta < MIN_DELAY_BEFORE_DME_CMDS_US) |
| 3755 | min_sleep_time_us = |
| 3756 | MIN_DELAY_BEFORE_DME_CMDS_US - delta; |
| 3757 | else |
| 3758 | return; /* no more delay required */ |
| 3759 | } |
| 3760 | |
| 3761 | /* allow sleep for extra 50us if needed */ |
| 3762 | usleep_range(min_sleep_time_us, min_sleep_time_us + 50); |
| 3763 | } |
| 3764 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3765 | /** |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3766 | * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET |
| 3767 | * @hba: per adapter instance |
| 3768 | * @attr_sel: uic command argument1 |
| 3769 | * @attr_set: attribute set type as uic command argument2 |
| 3770 | * @mib_val: setting value as uic command argument3 |
| 3771 | * @peer: indicate whether peer or local |
| 3772 | * |
| 3773 | * Returns 0 on success, non-zero value on failure |
| 3774 | */ |
| 3775 | int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel, |
| 3776 | u8 attr_set, u32 mib_val, u8 peer) |
| 3777 | { |
| 3778 | struct uic_command uic_cmd = {0}; |
| 3779 | static const char *const action[] = { |
| 3780 | "dme-set", |
| 3781 | "dme-peer-set" |
| 3782 | }; |
| 3783 | const char *set = action[!!peer]; |
| 3784 | int ret; |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3785 | int retries = UFS_UIC_COMMAND_RETRIES; |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3786 | |
| 3787 | uic_cmd.command = peer ? |
| 3788 | UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET; |
| 3789 | uic_cmd.argument1 = attr_sel; |
| 3790 | uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set); |
| 3791 | uic_cmd.argument3 = mib_val; |
| 3792 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3793 | do { |
| 3794 | /* for peer attributes we retry upon failure */ |
| 3795 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3796 | if (ret) |
| 3797 | dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n", |
| 3798 | set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret); |
| 3799 | } while (ret && peer && --retries); |
| 3800 | |
Yaniv Gardi | f37e9f8 | 2016-11-23 16:32:49 -0800 | [diff] [blame] | 3801 | if (ret) |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3802 | 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] | 3803 | set, UIC_GET_ATTR_ID(attr_sel), mib_val, |
| 3804 | UFS_UIC_COMMAND_RETRIES - retries); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3805 | |
| 3806 | return ret; |
| 3807 | } |
| 3808 | EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr); |
| 3809 | |
| 3810 | /** |
| 3811 | * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET |
| 3812 | * @hba: per adapter instance |
| 3813 | * @attr_sel: uic command argument1 |
| 3814 | * @mib_val: the value of the attribute as returned by the UIC command |
| 3815 | * @peer: indicate whether peer or local |
| 3816 | * |
| 3817 | * Returns 0 on success, non-zero value on failure |
| 3818 | */ |
| 3819 | int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel, |
| 3820 | u32 *mib_val, u8 peer) |
| 3821 | { |
| 3822 | struct uic_command uic_cmd = {0}; |
| 3823 | static const char *const action[] = { |
| 3824 | "dme-get", |
| 3825 | "dme-peer-get" |
| 3826 | }; |
| 3827 | const char *get = action[!!peer]; |
| 3828 | int ret; |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3829 | int retries = UFS_UIC_COMMAND_RETRIES; |
Yaniv Gardi | 874237f | 2015-05-17 18:55:03 +0300 | [diff] [blame] | 3830 | struct ufs_pa_layer_attr orig_pwr_info; |
| 3831 | struct ufs_pa_layer_attr temp_pwr_info; |
| 3832 | bool pwr_mode_change = false; |
| 3833 | |
| 3834 | if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) { |
| 3835 | orig_pwr_info = hba->pwr_info; |
| 3836 | temp_pwr_info = orig_pwr_info; |
| 3837 | |
| 3838 | if (orig_pwr_info.pwr_tx == FAST_MODE || |
| 3839 | orig_pwr_info.pwr_rx == FAST_MODE) { |
| 3840 | temp_pwr_info.pwr_tx = FASTAUTO_MODE; |
| 3841 | temp_pwr_info.pwr_rx = FASTAUTO_MODE; |
| 3842 | pwr_mode_change = true; |
| 3843 | } else if (orig_pwr_info.pwr_tx == SLOW_MODE || |
| 3844 | orig_pwr_info.pwr_rx == SLOW_MODE) { |
| 3845 | temp_pwr_info.pwr_tx = SLOWAUTO_MODE; |
| 3846 | temp_pwr_info.pwr_rx = SLOWAUTO_MODE; |
| 3847 | pwr_mode_change = true; |
| 3848 | } |
| 3849 | if (pwr_mode_change) { |
| 3850 | ret = ufshcd_change_power_mode(hba, &temp_pwr_info); |
| 3851 | if (ret) |
| 3852 | goto out; |
| 3853 | } |
| 3854 | } |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3855 | |
| 3856 | uic_cmd.command = peer ? |
| 3857 | UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET; |
| 3858 | uic_cmd.argument1 = attr_sel; |
| 3859 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3860 | do { |
| 3861 | /* for peer attributes we retry upon failure */ |
| 3862 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3863 | if (ret) |
| 3864 | dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n", |
| 3865 | get, UIC_GET_ATTR_ID(attr_sel), ret); |
| 3866 | } while (ret && peer && --retries); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3867 | |
Yaniv Gardi | f37e9f8 | 2016-11-23 16:32:49 -0800 | [diff] [blame] | 3868 | if (ret) |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3869 | 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] | 3870 | get, UIC_GET_ATTR_ID(attr_sel), |
| 3871 | UFS_UIC_COMMAND_RETRIES - retries); |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3872 | |
| 3873 | if (mib_val && !ret) |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3874 | *mib_val = uic_cmd.argument3; |
Yaniv Gardi | 874237f | 2015-05-17 18:55:03 +0300 | [diff] [blame] | 3875 | |
| 3876 | if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE) |
| 3877 | && pwr_mode_change) |
| 3878 | ufshcd_change_power_mode(hba, &orig_pwr_info); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3879 | out: |
| 3880 | return ret; |
| 3881 | } |
| 3882 | EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr); |
| 3883 | |
| 3884 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3885 | * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power |
| 3886 | * state) and waits for it to take effect. |
| 3887 | * |
| 3888 | * @hba: per adapter instance |
| 3889 | * @cmd: UIC command to execute |
| 3890 | * |
| 3891 | * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER & |
| 3892 | * DME_HIBERNATE_EXIT commands take some time to take its effect on both host |
| 3893 | * and device UniPro link and hence it's final completion would be indicated by |
| 3894 | * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in |
| 3895 | * addition to normal UIC command completion Status (UCCS). This function only |
| 3896 | * returns after the relevant status bits indicate the completion. |
| 3897 | * |
| 3898 | * Returns 0 on success, non-zero value on failure |
| 3899 | */ |
| 3900 | static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd) |
| 3901 | { |
| 3902 | struct completion uic_async_done; |
| 3903 | unsigned long flags; |
| 3904 | u8 status; |
| 3905 | int ret; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3906 | bool reenable_intr = false; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3907 | |
| 3908 | mutex_lock(&hba->uic_cmd_mutex); |
| 3909 | init_completion(&uic_async_done); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 3910 | ufshcd_add_delay_before_dme_cmd(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3911 | |
| 3912 | spin_lock_irqsave(hba->host->host_lock, flags); |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 3913 | if (ufshcd_is_link_broken(hba)) { |
| 3914 | ret = -ENOLINK; |
| 3915 | goto out_unlock; |
| 3916 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3917 | hba->uic_async_done = &uic_async_done; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3918 | if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) { |
| 3919 | ufshcd_disable_intr(hba, UIC_COMMAND_COMPL); |
| 3920 | /* |
| 3921 | * Make sure UIC command completion interrupt is disabled before |
| 3922 | * issuing UIC command. |
| 3923 | */ |
| 3924 | wmb(); |
| 3925 | reenable_intr = true; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3926 | } |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3927 | ret = __ufshcd_send_uic_cmd(hba, cmd, false); |
| 3928 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3929 | if (ret) { |
| 3930 | dev_err(hba->dev, |
| 3931 | "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n", |
| 3932 | cmd->command, cmd->argument3, ret); |
| 3933 | goto out; |
| 3934 | } |
| 3935 | |
| 3936 | if (!wait_for_completion_timeout(hba->uic_async_done, |
| 3937 | msecs_to_jiffies(UIC_CMD_TIMEOUT))) { |
| 3938 | dev_err(hba->dev, |
| 3939 | "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n", |
| 3940 | cmd->command, cmd->argument3); |
Can Guo | 0f52fcb9 | 2020-11-02 22:24:40 -0800 | [diff] [blame] | 3941 | |
| 3942 | if (!cmd->cmd_active) { |
| 3943 | dev_err(hba->dev, "%s: Power Mode Change operation has been completed, go check UPMCRS\n", |
| 3944 | __func__); |
| 3945 | goto check_upmcrs; |
| 3946 | } |
| 3947 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3948 | ret = -ETIMEDOUT; |
| 3949 | goto out; |
| 3950 | } |
| 3951 | |
Can Guo | 0f52fcb9 | 2020-11-02 22:24:40 -0800 | [diff] [blame] | 3952 | check_upmcrs: |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3953 | status = ufshcd_get_upmcrs(hba); |
| 3954 | if (status != PWR_LOCAL) { |
| 3955 | dev_err(hba->dev, |
Zang Leigang | 479da36 | 2017-09-19 16:50:30 +0800 | [diff] [blame] | 3956 | "pwr ctrl cmd 0x%x failed, host upmcrs:0x%x\n", |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3957 | cmd->command, status); |
| 3958 | ret = (status != PWR_OK) ? status : -1; |
| 3959 | } |
| 3960 | out: |
Venkat Gopalakrishnan | 7942f7b | 2017-02-03 16:58:24 -0800 | [diff] [blame] | 3961 | if (ret) { |
| 3962 | ufshcd_print_host_state(hba); |
| 3963 | ufshcd_print_pwr_info(hba); |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 3964 | ufshcd_print_evt_hist(hba); |
Venkat Gopalakrishnan | 7942f7b | 2017-02-03 16:58:24 -0800 | [diff] [blame] | 3965 | } |
| 3966 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3967 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3968 | hba->active_uic_cmd = NULL; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3969 | hba->uic_async_done = NULL; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3970 | if (reenable_intr) |
| 3971 | ufshcd_enable_intr(hba, UIC_COMMAND_COMPL); |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 3972 | if (ret) { |
| 3973 | ufshcd_set_link_broken(hba); |
| 3974 | ufshcd_schedule_eh_work(hba); |
| 3975 | } |
| 3976 | out_unlock: |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3977 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3978 | mutex_unlock(&hba->uic_cmd_mutex); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3979 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3980 | return ret; |
| 3981 | } |
| 3982 | |
| 3983 | /** |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3984 | * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage |
| 3985 | * using DME_SET primitives. |
| 3986 | * @hba: per adapter instance |
| 3987 | * @mode: powr mode value |
| 3988 | * |
| 3989 | * Returns 0 on success, non-zero value on failure |
| 3990 | */ |
Sujit Reddy Thumma | bdbe5d2 | 2014-05-26 10:59:11 +0530 | [diff] [blame] | 3991 | 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] | 3992 | { |
| 3993 | struct uic_command uic_cmd = {0}; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3994 | int ret; |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3995 | |
Yaniv Gardi | c3a2f9e | 2015-05-17 18:55:01 +0300 | [diff] [blame] | 3996 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) { |
| 3997 | ret = ufshcd_dme_set(hba, |
| 3998 | UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1); |
| 3999 | if (ret) { |
| 4000 | dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n", |
| 4001 | __func__, ret); |
| 4002 | goto out; |
| 4003 | } |
| 4004 | } |
| 4005 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4006 | uic_cmd.command = UIC_CMD_DME_SET; |
| 4007 | uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE); |
| 4008 | uic_cmd.argument3 = mode; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4009 | ufshcd_hold(hba, false); |
| 4010 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
| 4011 | ufshcd_release(hba); |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4012 | |
Yaniv Gardi | c3a2f9e | 2015-05-17 18:55:01 +0300 | [diff] [blame] | 4013 | out: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4014 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4015 | } |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4016 | |
Stanley Chu | 087c5ef | 2020-03-27 17:53:28 +0800 | [diff] [blame] | 4017 | int ufshcd_link_recovery(struct ufs_hba *hba) |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 4018 | { |
| 4019 | int ret; |
| 4020 | unsigned long flags; |
| 4021 | |
| 4022 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4023 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 4024 | ufshcd_set_eh_in_progress(hba); |
| 4025 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4026 | |
Can Guo | ebdd1df | 2019-11-14 22:09:24 -0800 | [diff] [blame] | 4027 | /* Reset the attached device */ |
Stanley Chu | 31a5d9c | 2020-12-08 21:56:35 +0800 | [diff] [blame] | 4028 | ufshcd_device_reset(hba); |
Can Guo | ebdd1df | 2019-11-14 22:09:24 -0800 | [diff] [blame] | 4029 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 4030 | ret = ufshcd_host_reset_and_restore(hba); |
| 4031 | |
| 4032 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4033 | if (ret) |
| 4034 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 4035 | ufshcd_clear_eh_in_progress(hba); |
| 4036 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4037 | |
| 4038 | if (ret) |
| 4039 | dev_err(hba->dev, "%s: link recovery failed, err %d", |
| 4040 | __func__, ret); |
| 4041 | |
| 4042 | return ret; |
| 4043 | } |
Stanley Chu | 087c5ef | 2020-03-27 17:53:28 +0800 | [diff] [blame] | 4044 | EXPORT_SYMBOL_GPL(ufshcd_link_recovery); |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 4045 | |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 4046 | static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4047 | { |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4048 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4049 | struct uic_command uic_cmd = {0}; |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 4050 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4051 | |
Kiwoong Kim | ee32c90 | 2016-11-10 21:17:43 +0900 | [diff] [blame] | 4052 | ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE); |
| 4053 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4054 | uic_cmd.command = UIC_CMD_DME_HIBER_ENTER; |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4055 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 4056 | trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter", |
| 4057 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4058 | |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 4059 | if (ret) |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4060 | dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n", |
| 4061 | __func__, ret); |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 4062 | else |
Kiwoong Kim | ee32c90 | 2016-11-10 21:17:43 +0900 | [diff] [blame] | 4063 | ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, |
| 4064 | POST_CHANGE); |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 4065 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4066 | return ret; |
| 4067 | } |
| 4068 | |
Stanley Chu | 9d19bf7a | 2020-01-17 11:51:07 +0800 | [diff] [blame] | 4069 | int ufshcd_uic_hibern8_exit(struct ufs_hba *hba) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4070 | { |
| 4071 | struct uic_command uic_cmd = {0}; |
| 4072 | int ret; |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 4073 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4074 | |
Kiwoong Kim | ee32c90 | 2016-11-10 21:17:43 +0900 | [diff] [blame] | 4075 | ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE); |
| 4076 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4077 | uic_cmd.command = UIC_CMD_DME_HIBER_EXIT; |
| 4078 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 4079 | trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit", |
| 4080 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
| 4081 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4082 | if (ret) { |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 4083 | dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n", |
| 4084 | __func__, ret); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 4085 | } else { |
Kiwoong Kim | ee32c90 | 2016-11-10 21:17:43 +0900 | [diff] [blame] | 4086 | ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, |
| 4087 | POST_CHANGE); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 4088 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_get(); |
| 4089 | hba->ufs_stats.hibern8_exit_cnt++; |
| 4090 | } |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4091 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4092 | return ret; |
| 4093 | } |
Stanley Chu | 9d19bf7a | 2020-01-17 11:51:07 +0800 | [diff] [blame] | 4094 | EXPORT_SYMBOL_GPL(ufshcd_uic_hibern8_exit); |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4095 | |
Stanley Chu | ba7af5e | 2019-12-30 13:32:28 +0800 | [diff] [blame] | 4096 | void ufshcd_auto_hibern8_update(struct ufs_hba *hba, u32 ahit) |
| 4097 | { |
| 4098 | unsigned long flags; |
Can Guo | be7594a | 2020-03-05 00:53:07 -0800 | [diff] [blame] | 4099 | bool update = false; |
Stanley Chu | ba7af5e | 2019-12-30 13:32:28 +0800 | [diff] [blame] | 4100 | |
Can Guo | be7594a | 2020-03-05 00:53:07 -0800 | [diff] [blame] | 4101 | if (!ufshcd_is_auto_hibern8_supported(hba)) |
Stanley Chu | ba7af5e | 2019-12-30 13:32:28 +0800 | [diff] [blame] | 4102 | return; |
| 4103 | |
| 4104 | spin_lock_irqsave(hba->host->host_lock, flags); |
Can Guo | be7594a | 2020-03-05 00:53:07 -0800 | [diff] [blame] | 4105 | if (hba->ahit != ahit) { |
| 4106 | hba->ahit = ahit; |
| 4107 | update = true; |
| 4108 | } |
Stanley Chu | ba7af5e | 2019-12-30 13:32:28 +0800 | [diff] [blame] | 4109 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Can Guo | be7594a | 2020-03-05 00:53:07 -0800 | [diff] [blame] | 4110 | |
| 4111 | if (update && !pm_runtime_suspended(hba->dev)) { |
| 4112 | pm_runtime_get_sync(hba->dev); |
| 4113 | ufshcd_hold(hba, false); |
| 4114 | ufshcd_auto_hibern8_enable(hba); |
| 4115 | ufshcd_release(hba); |
| 4116 | pm_runtime_put(hba->dev); |
| 4117 | } |
Stanley Chu | ba7af5e | 2019-12-30 13:32:28 +0800 | [diff] [blame] | 4118 | } |
| 4119 | EXPORT_SYMBOL_GPL(ufshcd_auto_hibern8_update); |
| 4120 | |
Can Guo | 71d848b | 2019-11-14 22:09:26 -0800 | [diff] [blame] | 4121 | void ufshcd_auto_hibern8_enable(struct ufs_hba *hba) |
Adrian Hunter | ad44837 | 2018-03-20 15:07:38 +0200 | [diff] [blame] | 4122 | { |
| 4123 | unsigned long flags; |
| 4124 | |
Bao D. Nguyen | 499f7a9 | 2020-08-28 18:05:13 -0700 | [diff] [blame] | 4125 | if (!ufshcd_is_auto_hibern8_supported(hba)) |
Adrian Hunter | ad44837 | 2018-03-20 15:07:38 +0200 | [diff] [blame] | 4126 | return; |
| 4127 | |
| 4128 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4129 | ufshcd_writel(hba, hba->ahit, REG_AUTO_HIBERNATE_IDLE_TIMER); |
| 4130 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4131 | } |
| 4132 | |
Yaniv Gardi | 5064636 | 2014-10-23 13:25:13 +0300 | [diff] [blame] | 4133 | /** |
| 4134 | * ufshcd_init_pwr_info - setting the POR (power on reset) |
| 4135 | * values in hba power info |
| 4136 | * @hba: per-adapter instance |
| 4137 | */ |
| 4138 | static void ufshcd_init_pwr_info(struct ufs_hba *hba) |
| 4139 | { |
| 4140 | hba->pwr_info.gear_rx = UFS_PWM_G1; |
| 4141 | hba->pwr_info.gear_tx = UFS_PWM_G1; |
| 4142 | hba->pwr_info.lane_rx = 1; |
| 4143 | hba->pwr_info.lane_tx = 1; |
| 4144 | hba->pwr_info.pwr_rx = SLOWAUTO_MODE; |
| 4145 | hba->pwr_info.pwr_tx = SLOWAUTO_MODE; |
| 4146 | hba->pwr_info.hs_rate = 0; |
| 4147 | } |
| 4148 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4149 | /** |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4150 | * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device |
| 4151 | * @hba: per-adapter instance |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4152 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4153 | static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4154 | { |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4155 | struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info; |
| 4156 | |
| 4157 | if (hba->max_pwr_info.is_valid) |
| 4158 | return 0; |
| 4159 | |
subhashj@codeaurora.org | 2349b53 | 2016-11-23 16:33:19 -0800 | [diff] [blame] | 4160 | pwr_info->pwr_tx = FAST_MODE; |
| 4161 | pwr_info->pwr_rx = FAST_MODE; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4162 | pwr_info->hs_rate = PA_HS_MODE_B; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4163 | |
| 4164 | /* Get the connected lane count */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4165 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES), |
| 4166 | &pwr_info->lane_rx); |
| 4167 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 4168 | &pwr_info->lane_tx); |
| 4169 | |
| 4170 | if (!pwr_info->lane_rx || !pwr_info->lane_tx) { |
| 4171 | dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n", |
| 4172 | __func__, |
| 4173 | pwr_info->lane_rx, |
| 4174 | pwr_info->lane_tx); |
| 4175 | return -EINVAL; |
| 4176 | } |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4177 | |
| 4178 | /* |
| 4179 | * First, get the maximum gears of HS speed. |
| 4180 | * If a zero value, it means there is no HSGEAR capability. |
| 4181 | * Then, get the maximum gears of PWM speed. |
| 4182 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4183 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx); |
| 4184 | if (!pwr_info->gear_rx) { |
| 4185 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), |
| 4186 | &pwr_info->gear_rx); |
| 4187 | if (!pwr_info->gear_rx) { |
| 4188 | dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n", |
| 4189 | __func__, pwr_info->gear_rx); |
| 4190 | return -EINVAL; |
| 4191 | } |
subhashj@codeaurora.org | 2349b53 | 2016-11-23 16:33:19 -0800 | [diff] [blame] | 4192 | pwr_info->pwr_rx = SLOW_MODE; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4193 | } |
| 4194 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4195 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), |
| 4196 | &pwr_info->gear_tx); |
| 4197 | if (!pwr_info->gear_tx) { |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4198 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4199 | &pwr_info->gear_tx); |
| 4200 | if (!pwr_info->gear_tx) { |
| 4201 | dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n", |
| 4202 | __func__, pwr_info->gear_tx); |
| 4203 | return -EINVAL; |
| 4204 | } |
subhashj@codeaurora.org | 2349b53 | 2016-11-23 16:33:19 -0800 | [diff] [blame] | 4205 | pwr_info->pwr_tx = SLOW_MODE; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4206 | } |
| 4207 | |
| 4208 | hba->max_pwr_info.is_valid = true; |
| 4209 | return 0; |
| 4210 | } |
| 4211 | |
| 4212 | static int ufshcd_change_power_mode(struct ufs_hba *hba, |
| 4213 | struct ufs_pa_layer_attr *pwr_mode) |
| 4214 | { |
| 4215 | int ret; |
| 4216 | |
| 4217 | /* if already configured to the requested pwr_mode */ |
Can Guo | 2355b66 | 2020-08-24 19:07:06 -0700 | [diff] [blame] | 4218 | if (!hba->force_pmc && |
| 4219 | pwr_mode->gear_rx == hba->pwr_info.gear_rx && |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4220 | pwr_mode->gear_tx == hba->pwr_info.gear_tx && |
| 4221 | pwr_mode->lane_rx == hba->pwr_info.lane_rx && |
| 4222 | pwr_mode->lane_tx == hba->pwr_info.lane_tx && |
| 4223 | pwr_mode->pwr_rx == hba->pwr_info.pwr_rx && |
| 4224 | pwr_mode->pwr_tx == hba->pwr_info.pwr_tx && |
| 4225 | pwr_mode->hs_rate == hba->pwr_info.hs_rate) { |
| 4226 | dev_dbg(hba->dev, "%s: power already configured\n", __func__); |
| 4227 | return 0; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4228 | } |
| 4229 | |
| 4230 | /* |
| 4231 | * Configure attributes for power mode change with below. |
| 4232 | * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION, |
| 4233 | * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION, |
| 4234 | * - PA_HSSERIES |
| 4235 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4236 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx); |
| 4237 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES), |
| 4238 | pwr_mode->lane_rx); |
| 4239 | if (pwr_mode->pwr_rx == FASTAUTO_MODE || |
| 4240 | pwr_mode->pwr_rx == FAST_MODE) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4241 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4242 | else |
| 4243 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4244 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4245 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx); |
| 4246 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES), |
| 4247 | pwr_mode->lane_tx); |
| 4248 | if (pwr_mode->pwr_tx == FASTAUTO_MODE || |
| 4249 | pwr_mode->pwr_tx == FAST_MODE) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4250 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4251 | else |
| 4252 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4253 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4254 | if (pwr_mode->pwr_rx == FASTAUTO_MODE || |
| 4255 | pwr_mode->pwr_tx == FASTAUTO_MODE || |
| 4256 | pwr_mode->pwr_rx == FAST_MODE || |
| 4257 | pwr_mode->pwr_tx == FAST_MODE) |
| 4258 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES), |
| 4259 | pwr_mode->hs_rate); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4260 | |
Kiwoong Kim | b1d0d2e | 2020-12-21 10:24:40 +0900 | [diff] [blame] | 4261 | if (!(hba->quirks & UFSHCD_QUIRK_SKIP_DEF_UNIPRO_TIMEOUT_SETTING)) { |
| 4262 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0), |
| 4263 | DL_FC0ProtectionTimeOutVal_Default); |
| 4264 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1), |
| 4265 | DL_TC0ReplayTimeOutVal_Default); |
| 4266 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2), |
| 4267 | DL_AFC0ReqTimeOutVal_Default); |
| 4268 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA3), |
| 4269 | DL_FC1ProtectionTimeOutVal_Default); |
| 4270 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA4), |
| 4271 | DL_TC1ReplayTimeOutVal_Default); |
| 4272 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA5), |
| 4273 | DL_AFC1ReqTimeOutVal_Default); |
Can Guo | 0834253 | 2019-12-05 02:14:42 +0000 | [diff] [blame] | 4274 | |
Kiwoong Kim | b1d0d2e | 2020-12-21 10:24:40 +0900 | [diff] [blame] | 4275 | ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalFC0ProtectionTimeOutVal), |
| 4276 | DL_FC0ProtectionTimeOutVal_Default); |
| 4277 | ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalTC0ReplayTimeOutVal), |
| 4278 | DL_TC0ReplayTimeOutVal_Default); |
| 4279 | ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalAFC0ReqTimeOutVal), |
| 4280 | DL_AFC0ReqTimeOutVal_Default); |
| 4281 | } |
Can Guo | 0834253 | 2019-12-05 02:14:42 +0000 | [diff] [blame] | 4282 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4283 | ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4 |
| 4284 | | pwr_mode->pwr_tx); |
| 4285 | |
| 4286 | if (ret) { |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4287 | dev_err(hba->dev, |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4288 | "%s: power mode change failed %d\n", __func__, ret); |
| 4289 | } else { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4290 | ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL, |
| 4291 | pwr_mode); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4292 | |
| 4293 | memcpy(&hba->pwr_info, pwr_mode, |
| 4294 | sizeof(struct ufs_pa_layer_attr)); |
| 4295 | } |
| 4296 | |
| 4297 | return ret; |
| 4298 | } |
| 4299 | |
| 4300 | /** |
| 4301 | * ufshcd_config_pwr_mode - configure a new power mode |
| 4302 | * @hba: per-adapter instance |
| 4303 | * @desired_pwr_mode: desired power configuration |
| 4304 | */ |
Alim Akhtar | 0d846e7 | 2018-05-06 15:44:18 +0530 | [diff] [blame] | 4305 | int ufshcd_config_pwr_mode(struct ufs_hba *hba, |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4306 | struct ufs_pa_layer_attr *desired_pwr_mode) |
| 4307 | { |
| 4308 | struct ufs_pa_layer_attr final_params = { 0 }; |
| 4309 | int ret; |
| 4310 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4311 | ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE, |
| 4312 | desired_pwr_mode, &final_params); |
| 4313 | |
| 4314 | if (ret) |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4315 | memcpy(&final_params, desired_pwr_mode, sizeof(final_params)); |
| 4316 | |
| 4317 | ret = ufshcd_change_power_mode(hba, &final_params); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4318 | |
| 4319 | return ret; |
| 4320 | } |
Alim Akhtar | 0d846e7 | 2018-05-06 15:44:18 +0530 | [diff] [blame] | 4321 | EXPORT_SYMBOL_GPL(ufshcd_config_pwr_mode); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4322 | |
| 4323 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4324 | * ufshcd_complete_dev_init() - checks device readiness |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 4325 | * @hba: per-adapter instance |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4326 | * |
| 4327 | * Set fDeviceInit flag and poll until device toggles it. |
| 4328 | */ |
| 4329 | static int ufshcd_complete_dev_init(struct ufs_hba *hba) |
| 4330 | { |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 4331 | int err; |
Jason Yan | 7dfdcc3 | 2020-04-26 17:43:05 +0800 | [diff] [blame] | 4332 | bool flag_res = true; |
Kiwoong Kim | 29707fa | 2020-08-10 19:02:27 +0900 | [diff] [blame] | 4333 | ktime_t timeout; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4334 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 4335 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG, |
Stanley Chu | 1f34eed | 2020-05-08 16:01:12 +0800 | [diff] [blame] | 4336 | QUERY_FLAG_IDN_FDEVICEINIT, 0, NULL); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4337 | if (err) { |
| 4338 | dev_err(hba->dev, |
| 4339 | "%s setting fDeviceInit flag failed with error %d\n", |
| 4340 | __func__, err); |
| 4341 | goto out; |
| 4342 | } |
| 4343 | |
Kiwoong Kim | 29707fa | 2020-08-10 19:02:27 +0900 | [diff] [blame] | 4344 | /* Poll fDeviceInit flag to be cleared */ |
| 4345 | timeout = ktime_add_ms(ktime_get(), FDEVICEINIT_COMPL_TIMEOUT); |
| 4346 | do { |
| 4347 | err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_READ_FLAG, |
| 4348 | QUERY_FLAG_IDN_FDEVICEINIT, 0, &flag_res); |
| 4349 | if (!flag_res) |
| 4350 | break; |
| 4351 | usleep_range(5000, 10000); |
| 4352 | } while (ktime_before(ktime_get(), timeout)); |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 4353 | |
Kiwoong Kim | 29707fa | 2020-08-10 19:02:27 +0900 | [diff] [blame] | 4354 | if (err) { |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4355 | dev_err(hba->dev, |
Kiwoong Kim | 29707fa | 2020-08-10 19:02:27 +0900 | [diff] [blame] | 4356 | "%s reading fDeviceInit flag failed with error %d\n", |
| 4357 | __func__, err); |
| 4358 | } else if (flag_res) { |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4359 | dev_err(hba->dev, |
Kiwoong Kim | 29707fa | 2020-08-10 19:02:27 +0900 | [diff] [blame] | 4360 | "%s fDeviceInit was not cleared by the device\n", |
| 4361 | __func__); |
| 4362 | err = -EBUSY; |
| 4363 | } |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4364 | out: |
| 4365 | return err; |
| 4366 | } |
| 4367 | |
| 4368 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4369 | * ufshcd_make_hba_operational - Make UFS controller operational |
| 4370 | * @hba: per adapter instance |
| 4371 | * |
| 4372 | * To bring UFS host controller to operational state, |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4373 | * 1. Enable required interrupts |
| 4374 | * 2. Configure interrupt aggregation |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 4375 | * 3. Program UTRL and UTMRL base address |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4376 | * 4. Configure run-stop-registers |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4377 | * |
| 4378 | * Returns 0 on success, non-zero value on failure |
| 4379 | */ |
Stanley Chu | 9d19bf7a | 2020-01-17 11:51:07 +0800 | [diff] [blame] | 4380 | int ufshcd_make_hba_operational(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4381 | { |
| 4382 | int err = 0; |
| 4383 | u32 reg; |
| 4384 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4385 | /* Enable required interrupts */ |
| 4386 | ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS); |
| 4387 | |
| 4388 | /* Configure interrupt aggregation */ |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 4389 | if (ufshcd_is_intr_aggr_allowed(hba)) |
| 4390 | ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO); |
| 4391 | else |
| 4392 | ufshcd_disable_intr_aggr(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4393 | |
| 4394 | /* Configure UTRL and UTMRL base address registers */ |
| 4395 | ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr), |
| 4396 | REG_UTP_TRANSFER_REQ_LIST_BASE_L); |
| 4397 | ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr), |
| 4398 | REG_UTP_TRANSFER_REQ_LIST_BASE_H); |
| 4399 | ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr), |
| 4400 | REG_UTP_TASK_REQ_LIST_BASE_L); |
| 4401 | ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr), |
| 4402 | REG_UTP_TASK_REQ_LIST_BASE_H); |
| 4403 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4404 | /* |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 4405 | * Make sure base address and interrupt setup are updated before |
| 4406 | * enabling the run/stop registers below. |
| 4407 | */ |
| 4408 | wmb(); |
| 4409 | |
| 4410 | /* |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4411 | * UCRDY, UTMRLDY and UTRLRDY bits must be 1 |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4412 | */ |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4413 | reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4414 | if (!(ufshcd_get_lists_status(reg))) { |
| 4415 | ufshcd_enable_run_stop_reg(hba); |
| 4416 | } else { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 4417 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4418 | "Host controller not ready to process requests"); |
| 4419 | err = -EIO; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4420 | } |
| 4421 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4422 | return err; |
| 4423 | } |
Stanley Chu | 9d19bf7a | 2020-01-17 11:51:07 +0800 | [diff] [blame] | 4424 | EXPORT_SYMBOL_GPL(ufshcd_make_hba_operational); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4425 | |
| 4426 | /** |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4427 | * ufshcd_hba_stop - Send controller to reset state |
| 4428 | * @hba: per adapter instance |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4429 | */ |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 4430 | static inline void ufshcd_hba_stop(struct ufs_hba *hba) |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4431 | { |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 4432 | unsigned long flags; |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4433 | int err; |
| 4434 | |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 4435 | /* |
| 4436 | * Obtain the host lock to prevent that the controller is disabled |
| 4437 | * while the UFS interrupt handler is active on another CPU. |
| 4438 | */ |
| 4439 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4440 | ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE); |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 4441 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4442 | |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4443 | err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE, |
| 4444 | CONTROLLER_ENABLE, CONTROLLER_DISABLE, |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 4445 | 10, 1); |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4446 | if (err) |
| 4447 | dev_err(hba->dev, "%s: Controller disable failed\n", __func__); |
| 4448 | } |
| 4449 | |
| 4450 | /** |
Alim Akhtar | 39bf2d8 | 2020-05-28 06:46:51 +0530 | [diff] [blame] | 4451 | * ufshcd_hba_execute_hce - initialize the controller |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4452 | * @hba: per adapter instance |
| 4453 | * |
| 4454 | * The controller resets itself and controller firmware initialization |
| 4455 | * sequence kicks off. When controller is ready it will set |
| 4456 | * the Host Controller Enable bit to 1. |
| 4457 | * |
| 4458 | * Returns 0 on success, non-zero value on failure |
| 4459 | */ |
Alim Akhtar | 39bf2d8 | 2020-05-28 06:46:51 +0530 | [diff] [blame] | 4460 | static int ufshcd_hba_execute_hce(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4461 | { |
Stanley Chu | 6081b12 | 2020-11-12 13:45:37 +0800 | [diff] [blame] | 4462 | int retry_outer = 3; |
| 4463 | int retry_inner; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4464 | |
Stanley Chu | 6081b12 | 2020-11-12 13:45:37 +0800 | [diff] [blame] | 4465 | start: |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4466 | if (!ufshcd_is_hba_active(hba)) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4467 | /* change controller state to "reset state" */ |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 4468 | ufshcd_hba_stop(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4469 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4470 | /* UniPro link is disabled at this point */ |
| 4471 | ufshcd_set_link_off(hba); |
| 4472 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4473 | ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4474 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4475 | /* start controller initialization sequence */ |
| 4476 | ufshcd_hba_start(hba); |
| 4477 | |
| 4478 | /* |
| 4479 | * To initialize a UFS host controller HCE bit must be set to 1. |
| 4480 | * During initialization the HCE bit value changes from 1->0->1. |
| 4481 | * When the host controller completes initialization sequence |
| 4482 | * it sets the value of HCE bit to 1. The same HCE bit is read back |
| 4483 | * to check if the controller has completed initialization sequence. |
| 4484 | * So without this delay the value HCE = 1, set in the previous |
| 4485 | * instruction might be read back. |
| 4486 | * This delay can be changed based on the controller. |
| 4487 | */ |
Stanley Chu | 90b8491 | 2020-05-09 17:37:13 +0800 | [diff] [blame] | 4488 | ufshcd_delay_us(hba->vps->hba_enable_delay_us, 100); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4489 | |
| 4490 | /* wait for the host controller to complete initialization */ |
Stanley Chu | 6081b12 | 2020-11-12 13:45:37 +0800 | [diff] [blame] | 4491 | retry_inner = 50; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4492 | while (ufshcd_is_hba_active(hba)) { |
Stanley Chu | 6081b12 | 2020-11-12 13:45:37 +0800 | [diff] [blame] | 4493 | if (retry_inner) { |
| 4494 | retry_inner--; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4495 | } else { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 4496 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4497 | "Controller enable failed\n"); |
Stanley Chu | 6081b12 | 2020-11-12 13:45:37 +0800 | [diff] [blame] | 4498 | if (retry_outer) { |
| 4499 | retry_outer--; |
| 4500 | goto start; |
| 4501 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4502 | return -EIO; |
| 4503 | } |
Stanley Chu | 9fc305e | 2020-03-18 18:40:15 +0800 | [diff] [blame] | 4504 | usleep_range(1000, 1100); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4505 | } |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4506 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4507 | /* enable UIC related interrupts */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4508 | ufshcd_enable_intr(hba, UFSHCD_UIC_MASK); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4509 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4510 | ufshcd_vops_hce_enable_notify(hba, POST_CHANGE); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4511 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4512 | return 0; |
| 4513 | } |
Alim Akhtar | 39bf2d8 | 2020-05-28 06:46:51 +0530 | [diff] [blame] | 4514 | |
| 4515 | int ufshcd_hba_enable(struct ufs_hba *hba) |
| 4516 | { |
| 4517 | int ret; |
| 4518 | |
| 4519 | if (hba->quirks & UFSHCI_QUIRK_BROKEN_HCE) { |
| 4520 | ufshcd_set_link_off(hba); |
| 4521 | ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE); |
| 4522 | |
| 4523 | /* enable UIC related interrupts */ |
| 4524 | ufshcd_enable_intr(hba, UFSHCD_UIC_MASK); |
| 4525 | ret = ufshcd_dme_reset(hba); |
| 4526 | if (!ret) { |
| 4527 | ret = ufshcd_dme_enable(hba); |
| 4528 | if (!ret) |
| 4529 | ufshcd_vops_hce_enable_notify(hba, POST_CHANGE); |
| 4530 | if (ret) |
| 4531 | dev_err(hba->dev, |
| 4532 | "Host controller enable failed with non-hce\n"); |
| 4533 | } |
| 4534 | } else { |
| 4535 | ret = ufshcd_hba_execute_hce(hba); |
| 4536 | } |
| 4537 | |
| 4538 | return ret; |
| 4539 | } |
Stanley Chu | 9d19bf7a | 2020-01-17 11:51:07 +0800 | [diff] [blame] | 4540 | EXPORT_SYMBOL_GPL(ufshcd_hba_enable); |
| 4541 | |
Yaniv Gardi | 7ca38cf | 2015-05-17 18:54:59 +0300 | [diff] [blame] | 4542 | static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer) |
| 4543 | { |
Stanley Chu | ba0320f | 2020-03-18 18:40:10 +0800 | [diff] [blame] | 4544 | int tx_lanes = 0, i, err = 0; |
Yaniv Gardi | 7ca38cf | 2015-05-17 18:54:59 +0300 | [diff] [blame] | 4545 | |
| 4546 | if (!peer) |
| 4547 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 4548 | &tx_lanes); |
| 4549 | else |
| 4550 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 4551 | &tx_lanes); |
| 4552 | for (i = 0; i < tx_lanes; i++) { |
| 4553 | if (!peer) |
| 4554 | err = ufshcd_dme_set(hba, |
| 4555 | UIC_ARG_MIB_SEL(TX_LCC_ENABLE, |
| 4556 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)), |
| 4557 | 0); |
| 4558 | else |
| 4559 | err = ufshcd_dme_peer_set(hba, |
| 4560 | UIC_ARG_MIB_SEL(TX_LCC_ENABLE, |
| 4561 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)), |
| 4562 | 0); |
| 4563 | if (err) { |
| 4564 | dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d", |
| 4565 | __func__, peer, i, err); |
| 4566 | break; |
| 4567 | } |
| 4568 | } |
| 4569 | |
| 4570 | return err; |
| 4571 | } |
| 4572 | |
| 4573 | static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba) |
| 4574 | { |
| 4575 | return ufshcd_disable_tx_lcc(hba, true); |
| 4576 | } |
| 4577 | |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 4578 | void ufshcd_update_evt_hist(struct ufs_hba *hba, u32 id, u32 val) |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4579 | { |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 4580 | struct ufs_event_hist *e; |
| 4581 | |
| 4582 | if (id >= UFS_EVT_CNT) |
| 4583 | return; |
| 4584 | |
| 4585 | e = &hba->ufs_stats.event[id]; |
| 4586 | e->val[e->pos] = val; |
| 4587 | e->tstamp[e->pos] = ktime_get(); |
Adrian Hunter | b6cacaf | 2021-01-07 09:25:38 +0200 | [diff] [blame] | 4588 | e->cnt += 1; |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 4589 | e->pos = (e->pos + 1) % UFS_EVENT_HIST_LENGTH; |
Stanley Chu | 172614a | 2020-12-05 19:59:00 +0800 | [diff] [blame] | 4590 | |
| 4591 | ufshcd_vops_event_notify(hba, id, &val); |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4592 | } |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 4593 | EXPORT_SYMBOL_GPL(ufshcd_update_evt_hist); |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4594 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4595 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4596 | * ufshcd_link_startup - Initialize unipro link startup |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4597 | * @hba: per adapter instance |
| 4598 | * |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4599 | * Returns 0 for success, non-zero in case of failure |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4600 | */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4601 | static int ufshcd_link_startup(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4602 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4603 | int ret; |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4604 | int retries = DME_LINKSTARTUP_RETRIES; |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 4605 | bool link_startup_again = false; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4606 | |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 4607 | /* |
| 4608 | * If UFS device isn't active then we will have to issue link startup |
| 4609 | * 2 times to make sure the device state move to active. |
| 4610 | */ |
| 4611 | if (!ufshcd_is_ufs_dev_active(hba)) |
| 4612 | link_startup_again = true; |
| 4613 | |
| 4614 | link_startup: |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4615 | do { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4616 | ufshcd_vops_link_startup_notify(hba, PRE_CHANGE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4617 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4618 | ret = ufshcd_dme_link_startup(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4619 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4620 | /* check if device is detected by inter-connect layer */ |
| 4621 | if (!ret && !ufshcd_is_device_present(hba)) { |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 4622 | ufshcd_update_evt_hist(hba, |
| 4623 | UFS_EVT_LINK_STARTUP_FAIL, |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4624 | 0); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4625 | dev_err(hba->dev, "%s: Device not present\n", __func__); |
| 4626 | ret = -ENXIO; |
| 4627 | goto out; |
| 4628 | } |
| 4629 | |
| 4630 | /* |
| 4631 | * DME link lost indication is only received when link is up, |
| 4632 | * but we can't be sure if the link is up until link startup |
| 4633 | * succeeds. So reset the local Uni-Pro and try again. |
| 4634 | */ |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4635 | if (ret && ufshcd_hba_enable(hba)) { |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 4636 | ufshcd_update_evt_hist(hba, |
| 4637 | UFS_EVT_LINK_STARTUP_FAIL, |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4638 | (u32)ret); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4639 | goto out; |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4640 | } |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4641 | } while (ret && retries--); |
| 4642 | |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4643 | if (ret) { |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4644 | /* failed to get the link up... retire */ |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 4645 | ufshcd_update_evt_hist(hba, |
| 4646 | UFS_EVT_LINK_STARTUP_FAIL, |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4647 | (u32)ret); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4648 | goto out; |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4649 | } |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4650 | |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 4651 | if (link_startup_again) { |
| 4652 | link_startup_again = false; |
| 4653 | retries = DME_LINKSTARTUP_RETRIES; |
| 4654 | goto link_startup; |
| 4655 | } |
| 4656 | |
subhashj@codeaurora.org | d2aebb9 | 2016-12-22 18:41:33 -0800 | [diff] [blame] | 4657 | /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */ |
| 4658 | ufshcd_init_pwr_info(hba); |
| 4659 | ufshcd_print_pwr_info(hba); |
| 4660 | |
Yaniv Gardi | 7ca38cf | 2015-05-17 18:54:59 +0300 | [diff] [blame] | 4661 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) { |
| 4662 | ret = ufshcd_disable_device_tx_lcc(hba); |
| 4663 | if (ret) |
| 4664 | goto out; |
| 4665 | } |
| 4666 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4667 | /* Include any host controller configuration via UIC commands */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4668 | ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE); |
| 4669 | if (ret) |
| 4670 | goto out; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4671 | |
Can Guo | 2355b66 | 2020-08-24 19:07:06 -0700 | [diff] [blame] | 4672 | /* Clear UECPA once due to LINERESET has happened during LINK_STARTUP */ |
| 4673 | ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4674 | ret = ufshcd_make_hba_operational(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4675 | out: |
Venkat Gopalakrishnan | 7942f7b | 2017-02-03 16:58:24 -0800 | [diff] [blame] | 4676 | if (ret) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4677 | dev_err(hba->dev, "link startup failed %d\n", ret); |
Venkat Gopalakrishnan | 7942f7b | 2017-02-03 16:58:24 -0800 | [diff] [blame] | 4678 | ufshcd_print_host_state(hba); |
| 4679 | ufshcd_print_pwr_info(hba); |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 4680 | ufshcd_print_evt_hist(hba); |
Venkat Gopalakrishnan | 7942f7b | 2017-02-03 16:58:24 -0800 | [diff] [blame] | 4681 | } |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4682 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4683 | } |
| 4684 | |
| 4685 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4686 | * ufshcd_verify_dev_init() - Verify device initialization |
| 4687 | * @hba: per-adapter instance |
| 4688 | * |
| 4689 | * Send NOP OUT UPIU and wait for NOP IN response to check whether the |
| 4690 | * device Transport Protocol (UTP) layer is ready after a reset. |
| 4691 | * If the UTP layer at the device side is not initialized, it may |
| 4692 | * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT |
| 4693 | * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations. |
| 4694 | */ |
| 4695 | static int ufshcd_verify_dev_init(struct ufs_hba *hba) |
| 4696 | { |
| 4697 | int err = 0; |
| 4698 | int retries; |
| 4699 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4700 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4701 | mutex_lock(&hba->dev_cmd.lock); |
| 4702 | for (retries = NOP_OUT_RETRIES; retries > 0; retries--) { |
| 4703 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP, |
| 4704 | NOP_OUT_TIMEOUT); |
| 4705 | |
| 4706 | if (!err || err == -ETIMEDOUT) |
| 4707 | break; |
| 4708 | |
| 4709 | dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err); |
| 4710 | } |
| 4711 | mutex_unlock(&hba->dev_cmd.lock); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4712 | ufshcd_release(hba); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4713 | |
| 4714 | if (err) |
| 4715 | dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err); |
| 4716 | return err; |
| 4717 | } |
| 4718 | |
| 4719 | /** |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4720 | * ufshcd_set_queue_depth - set lun queue depth |
| 4721 | * @sdev: pointer to SCSI device |
| 4722 | * |
| 4723 | * Read bLUQueueDepth value and activate scsi tagged command |
| 4724 | * queueing. For WLUN, queue depth is set to 1. For best-effort |
| 4725 | * cases (bLUQueueDepth = 0) the queue depth is set to a maximum |
| 4726 | * value that host can queue. |
| 4727 | */ |
| 4728 | static void ufshcd_set_queue_depth(struct scsi_device *sdev) |
| 4729 | { |
| 4730 | int ret = 0; |
| 4731 | u8 lun_qdepth; |
| 4732 | struct ufs_hba *hba; |
| 4733 | |
| 4734 | hba = shost_priv(sdev->host); |
| 4735 | |
| 4736 | lun_qdepth = hba->nutrs; |
Szymon Mielczarek | dbd34a6 | 2017-03-29 08:19:21 +0200 | [diff] [blame] | 4737 | ret = ufshcd_read_unit_desc_param(hba, |
| 4738 | ufshcd_scsi_to_upiu_lun(sdev->lun), |
| 4739 | UNIT_DESC_PARAM_LU_Q_DEPTH, |
| 4740 | &lun_qdepth, |
| 4741 | sizeof(lun_qdepth)); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4742 | |
| 4743 | /* Some WLUN doesn't support unit descriptor */ |
| 4744 | if (ret == -EOPNOTSUPP) |
| 4745 | lun_qdepth = 1; |
| 4746 | else if (!lun_qdepth) |
| 4747 | /* eventually, we can figure out the real queue depth */ |
| 4748 | lun_qdepth = hba->nutrs; |
| 4749 | else |
| 4750 | lun_qdepth = min_t(int, lun_qdepth, hba->nutrs); |
| 4751 | |
| 4752 | dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n", |
| 4753 | __func__, lun_qdepth); |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4754 | scsi_change_queue_depth(sdev, lun_qdepth); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4755 | } |
| 4756 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4757 | /* |
| 4758 | * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR |
| 4759 | * @hba: per-adapter instance |
| 4760 | * @lun: UFS device lun id |
| 4761 | * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info |
| 4762 | * |
| 4763 | * Returns 0 in case of success and b_lu_write_protect status would be returned |
| 4764 | * @b_lu_write_protect parameter. |
| 4765 | * Returns -ENOTSUPP if reading b_lu_write_protect is not supported. |
| 4766 | * Returns -EINVAL in case of invalid parameters passed to this function. |
| 4767 | */ |
| 4768 | static int ufshcd_get_lu_wp(struct ufs_hba *hba, |
| 4769 | u8 lun, |
| 4770 | u8 *b_lu_write_protect) |
| 4771 | { |
| 4772 | int ret; |
| 4773 | |
| 4774 | if (!b_lu_write_protect) |
| 4775 | ret = -EINVAL; |
| 4776 | /* |
| 4777 | * According to UFS device spec, RPMB LU can't be write |
| 4778 | * protected so skip reading bLUWriteProtect parameter for |
| 4779 | * it. For other W-LUs, UNIT DESCRIPTOR is not available. |
| 4780 | */ |
Bean Huo | 1baa801 | 2020-01-20 14:08:20 +0100 | [diff] [blame] | 4781 | else if (lun >= hba->dev_info.max_lu_supported) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4782 | ret = -ENOTSUPP; |
| 4783 | else |
| 4784 | ret = ufshcd_read_unit_desc_param(hba, |
| 4785 | lun, |
| 4786 | UNIT_DESC_PARAM_LU_WR_PROTECT, |
| 4787 | b_lu_write_protect, |
| 4788 | sizeof(*b_lu_write_protect)); |
| 4789 | return ret; |
| 4790 | } |
| 4791 | |
| 4792 | /** |
| 4793 | * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect |
| 4794 | * status |
| 4795 | * @hba: per-adapter instance |
| 4796 | * @sdev: pointer to SCSI device |
| 4797 | * |
| 4798 | */ |
| 4799 | static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba, |
| 4800 | struct scsi_device *sdev) |
| 4801 | { |
| 4802 | if (hba->dev_info.f_power_on_wp_en && |
| 4803 | !hba->dev_info.is_lu_power_on_wp) { |
| 4804 | u8 b_lu_write_protect; |
| 4805 | |
| 4806 | if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun), |
| 4807 | &b_lu_write_protect) && |
| 4808 | (b_lu_write_protect == UFS_LU_POWER_ON_WP)) |
| 4809 | hba->dev_info.is_lu_power_on_wp = true; |
| 4810 | } |
| 4811 | } |
| 4812 | |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4813 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4814 | * ufshcd_slave_alloc - handle initial SCSI device configurations |
| 4815 | * @sdev: pointer to SCSI device |
| 4816 | * |
| 4817 | * Returns success |
| 4818 | */ |
| 4819 | static int ufshcd_slave_alloc(struct scsi_device *sdev) |
| 4820 | { |
| 4821 | struct ufs_hba *hba; |
| 4822 | |
| 4823 | hba = shost_priv(sdev->host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4824 | |
| 4825 | /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */ |
| 4826 | sdev->use_10_for_ms = 1; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4827 | |
Can Guo | a3a7639 | 2019-12-05 02:14:30 +0000 | [diff] [blame] | 4828 | /* DBD field should be set to 1 in mode sense(10) */ |
| 4829 | sdev->set_dbd_for_ms = 1; |
| 4830 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4831 | /* allow SCSI layer to restart the device in case of errors */ |
| 4832 | sdev->allow_restart = 1; |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4833 | |
Sujit Reddy Thumma | b2a6c52 | 2014-07-01 12:22:38 +0300 | [diff] [blame] | 4834 | /* REPORT SUPPORTED OPERATION CODES is not supported */ |
| 4835 | sdev->no_report_opcodes = 1; |
| 4836 | |
Sujit Reddy Thumma | 84af7e8 | 2018-01-24 09:52:35 +0530 | [diff] [blame] | 4837 | /* WRITE_SAME command is not supported */ |
| 4838 | sdev->no_write_same = 1; |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4839 | |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4840 | ufshcd_set_queue_depth(sdev); |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4841 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4842 | ufshcd_get_lu_power_on_wp_status(hba, sdev); |
| 4843 | |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4844 | return 0; |
| 4845 | } |
| 4846 | |
| 4847 | /** |
| 4848 | * ufshcd_change_queue_depth - change queue depth |
| 4849 | * @sdev: pointer to SCSI device |
| 4850 | * @depth: required depth to set |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4851 | * |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4852 | * 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] | 4853 | */ |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4854 | 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] | 4855 | { |
| 4856 | struct ufs_hba *hba = shost_priv(sdev->host); |
| 4857 | |
| 4858 | if (depth > hba->nutrs) |
| 4859 | depth = hba->nutrs; |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4860 | return scsi_change_queue_depth(sdev, depth); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4861 | } |
| 4862 | |
| 4863 | /** |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 4864 | * ufshcd_slave_configure - adjust SCSI device configurations |
| 4865 | * @sdev: pointer to SCSI device |
| 4866 | */ |
| 4867 | static int ufshcd_slave_configure(struct scsi_device *sdev) |
| 4868 | { |
Stanley Chu | 49615ba | 2019-09-16 23:56:50 +0800 | [diff] [blame] | 4869 | struct ufs_hba *hba = shost_priv(sdev->host); |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 4870 | struct request_queue *q = sdev->request_queue; |
| 4871 | |
| 4872 | blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1); |
Kiwoong Kim | 2b2bfc8a | 2021-01-19 12:33:41 +0900 | [diff] [blame] | 4873 | if (hba->quirks & UFSHCD_QUIRK_ALIGN_SG_WITH_PAGE_SIZE) |
| 4874 | blk_queue_update_dma_alignment(q, PAGE_SIZE - 1); |
Stanley Chu | 49615ba | 2019-09-16 23:56:50 +0800 | [diff] [blame] | 4875 | |
| 4876 | if (ufshcd_is_rpm_autosuspend_allowed(hba)) |
| 4877 | sdev->rpm_autosuspend = 1; |
| 4878 | |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 4879 | ufshcd_crypto_setup_rq_keyslot_manager(hba, q); |
| 4880 | |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 4881 | return 0; |
| 4882 | } |
| 4883 | |
| 4884 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4885 | * ufshcd_slave_destroy - remove SCSI device configurations |
| 4886 | * @sdev: pointer to SCSI device |
| 4887 | */ |
| 4888 | static void ufshcd_slave_destroy(struct scsi_device *sdev) |
| 4889 | { |
| 4890 | struct ufs_hba *hba; |
| 4891 | |
| 4892 | hba = shost_priv(sdev->host); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4893 | /* Drop the reference as it won't be needed anymore */ |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 4894 | if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) { |
| 4895 | unsigned long flags; |
| 4896 | |
| 4897 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4898 | hba->sdev_ufs_device = NULL; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 4899 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4900 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4901 | } |
| 4902 | |
| 4903 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4904 | * 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] | 4905 | * @lrbp: pointer to local reference block of completed command |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4906 | * @scsi_status: SCSI command status |
| 4907 | * |
| 4908 | * Returns value base on SCSI command status |
| 4909 | */ |
| 4910 | static inline int |
| 4911 | ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status) |
| 4912 | { |
| 4913 | int result = 0; |
| 4914 | |
| 4915 | switch (scsi_status) { |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 4916 | case SAM_STAT_CHECK_CONDITION: |
| 4917 | ufshcd_copy_sense_data(lrbp); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 4918 | fallthrough; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4919 | case SAM_STAT_GOOD: |
| 4920 | result |= DID_OK << 16 | |
| 4921 | COMMAND_COMPLETE << 8 | |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 4922 | scsi_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4923 | break; |
| 4924 | case SAM_STAT_TASK_SET_FULL: |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 4925 | case SAM_STAT_BUSY: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4926 | case SAM_STAT_TASK_ABORTED: |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 4927 | ufshcd_copy_sense_data(lrbp); |
| 4928 | result |= scsi_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4929 | break; |
| 4930 | default: |
| 4931 | result |= DID_ERROR << 16; |
| 4932 | break; |
| 4933 | } /* end of switch */ |
| 4934 | |
| 4935 | return result; |
| 4936 | } |
| 4937 | |
| 4938 | /** |
| 4939 | * ufshcd_transfer_rsp_status - Get overall status of the response |
| 4940 | * @hba: per adapter instance |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 4941 | * @lrbp: pointer to local reference block of completed command |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4942 | * |
| 4943 | * Returns result of the command to notify SCSI midlayer |
| 4944 | */ |
| 4945 | static inline int |
| 4946 | ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 4947 | { |
| 4948 | int result = 0; |
| 4949 | int scsi_status; |
| 4950 | int ocs; |
| 4951 | |
| 4952 | /* overall command status of utrd */ |
| 4953 | ocs = ufshcd_get_tr_ocs(lrbp); |
| 4954 | |
Kiwoong Kim | d779a6e | 2020-05-28 06:46:53 +0530 | [diff] [blame] | 4955 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR) { |
| 4956 | if (be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_1) & |
| 4957 | MASK_RSP_UPIU_RESULT) |
| 4958 | ocs = OCS_SUCCESS; |
| 4959 | } |
| 4960 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4961 | switch (ocs) { |
| 4962 | case OCS_SUCCESS: |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4963 | result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 4964 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4965 | switch (result) { |
| 4966 | case UPIU_TRANSACTION_RESPONSE: |
| 4967 | /* |
| 4968 | * get the response UPIU result to extract |
| 4969 | * the SCSI command status |
| 4970 | */ |
| 4971 | result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr); |
| 4972 | |
| 4973 | /* |
| 4974 | * get the result based on SCSI status response |
| 4975 | * to notify the SCSI midlayer of the command status |
| 4976 | */ |
| 4977 | scsi_status = result & MASK_SCSI_STATUS; |
| 4978 | result = ufshcd_scsi_cmd_status(lrbp, scsi_status); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4979 | |
Yaniv Gardi | f05ac2e | 2016-02-01 15:02:42 +0200 | [diff] [blame] | 4980 | /* |
| 4981 | * Currently we are only supporting BKOPs exception |
| 4982 | * events hence we can ignore BKOPs exception event |
| 4983 | * during power management callbacks. BKOPs exception |
| 4984 | * event is not expected to be raised in runtime suspend |
| 4985 | * callback as it allows the urgent bkops. |
| 4986 | * During system suspend, we are anyway forcefully |
| 4987 | * disabling the bkops and if urgent bkops is needed |
| 4988 | * it will be enabled on system resume. Long term |
| 4989 | * solution could be to abort the system suspend if |
| 4990 | * UFS device needs urgent BKOPs. |
| 4991 | */ |
| 4992 | if (!hba->pm_op_in_progress && |
Sayali Lokhande | 2824ec9 | 2020-02-10 19:40:44 -0800 | [diff] [blame] | 4993 | ufshcd_is_exception_event(lrbp->ucd_rsp_ptr) && |
| 4994 | schedule_work(&hba->eeh_work)) { |
| 4995 | /* |
| 4996 | * Prevent suspend once eeh_work is scheduled |
| 4997 | * to avoid deadlock between ufshcd_suspend |
| 4998 | * and exception event handler. |
| 4999 | */ |
| 5000 | pm_runtime_get_noresume(hba->dev); |
| 5001 | } |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 5002 | break; |
| 5003 | case UPIU_TRANSACTION_REJECT_UPIU: |
| 5004 | /* TODO: handle Reject UPIU Response */ |
| 5005 | result = DID_ERROR << 16; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 5006 | dev_err(hba->dev, |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 5007 | "Reject UPIU not fully implemented\n"); |
| 5008 | break; |
| 5009 | default: |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 5010 | dev_err(hba->dev, |
| 5011 | "Unexpected request response code = %x\n", |
| 5012 | result); |
Stanley Chu | e0347d8 | 2019-04-15 20:23:38 +0800 | [diff] [blame] | 5013 | result = DID_ERROR << 16; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5014 | break; |
| 5015 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5016 | break; |
| 5017 | case OCS_ABORTED: |
| 5018 | result |= DID_ABORT << 16; |
| 5019 | break; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5020 | case OCS_INVALID_COMMAND_STATUS: |
| 5021 | result |= DID_REQUEUE << 16; |
| 5022 | break; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5023 | case OCS_INVALID_CMD_TABLE_ATTR: |
| 5024 | case OCS_INVALID_PRDT_ATTR: |
| 5025 | case OCS_MISMATCH_DATA_BUF_SIZE: |
| 5026 | case OCS_MISMATCH_RESP_UPIU_SIZE: |
| 5027 | case OCS_PEER_COMM_FAILURE: |
| 5028 | case OCS_FATAL_ERROR: |
Satya Tangirala | 5e7341e | 2020-07-06 20:04:12 +0000 | [diff] [blame] | 5029 | case OCS_DEVICE_FATAL_ERROR: |
| 5030 | case OCS_INVALID_CRYPTO_CONFIG: |
| 5031 | case OCS_GENERAL_CRYPTO_ERROR: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5032 | default: |
| 5033 | result |= DID_ERROR << 16; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 5034 | dev_err(hba->dev, |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5035 | "OCS error from controller = %x for tag %d\n", |
| 5036 | ocs, lrbp->task_tag); |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 5037 | ufshcd_print_evt_hist(hba); |
Gilad Broner | 6ba6558 | 2017-02-03 16:57:28 -0800 | [diff] [blame] | 5038 | ufshcd_print_host_state(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5039 | break; |
| 5040 | } /* end of switch */ |
| 5041 | |
Can Guo | 2df74b6 | 2019-11-25 22:53:33 -0800 | [diff] [blame] | 5042 | if ((host_byte(result) != DID_OK) && !hba->silence_err_logs) |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 5043 | ufshcd_print_trs(hba, 1 << lrbp->task_tag, true); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5044 | return result; |
| 5045 | } |
| 5046 | |
| 5047 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5048 | * ufshcd_uic_cmd_compl - handle completion of uic command |
| 5049 | * @hba: per adapter instance |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 5050 | * @intr_status: interrupt status generated by the controller |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5051 | * |
| 5052 | * Returns |
| 5053 | * IRQ_HANDLED - If interrupt is valid |
| 5054 | * IRQ_NONE - If invalid interrupt |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5055 | */ |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5056 | 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] | 5057 | { |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5058 | irqreturn_t retval = IRQ_NONE; |
| 5059 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 5060 | if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5061 | hba->active_uic_cmd->argument2 |= |
| 5062 | ufshcd_get_uic_cmd_result(hba); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 5063 | hba->active_uic_cmd->argument3 = |
| 5064 | ufshcd_get_dme_attr_val(hba); |
Can Guo | 0f52fcb9 | 2020-11-02 22:24:40 -0800 | [diff] [blame] | 5065 | if (!hba->uic_async_done) |
| 5066 | hba->active_uic_cmd->cmd_active = 0; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5067 | complete(&hba->active_uic_cmd->done); |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5068 | retval = IRQ_HANDLED; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5069 | } |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 5070 | |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5071 | if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done) { |
Can Guo | 0f52fcb9 | 2020-11-02 22:24:40 -0800 | [diff] [blame] | 5072 | hba->active_uic_cmd->cmd_active = 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5073 | complete(hba->uic_async_done); |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5074 | retval = IRQ_HANDLED; |
| 5075 | } |
Stanley Chu | aa5c697 | 2020-06-15 15:22:35 +0800 | [diff] [blame] | 5076 | |
| 5077 | if (retval == IRQ_HANDLED) |
| 5078 | ufshcd_add_uic_command_trace(hba, hba->active_uic_cmd, |
Bean Huo | 28fa68f | 2021-01-05 12:34:42 +0100 | [diff] [blame] | 5079 | UFS_CMD_COMP); |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5080 | return retval; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5081 | } |
| 5082 | |
| 5083 | /** |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5084 | * __ufshcd_transfer_req_compl - handle SCSI and query command completion |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5085 | * @hba: per adapter instance |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5086 | * @completed_reqs: requests to complete |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5087 | */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5088 | static void __ufshcd_transfer_req_compl(struct ufs_hba *hba, |
| 5089 | unsigned long completed_reqs) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5090 | { |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 5091 | struct ufshcd_lrb *lrbp; |
| 5092 | struct scsi_cmnd *cmd; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5093 | int result; |
| 5094 | int index; |
Can Guo | 7a7e66c | 2020-12-02 04:04:02 -0800 | [diff] [blame] | 5095 | bool update_scaling = false; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5096 | |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5097 | for_each_set_bit(index, &completed_reqs, hba->nutrs) { |
| 5098 | lrbp = &hba->lrb[index]; |
Can Guo | 7a7e66c | 2020-12-02 04:04:02 -0800 | [diff] [blame] | 5099 | lrbp->in_use = false; |
Stanley Chu | a317037 | 2020-07-06 14:07:06 +0800 | [diff] [blame] | 5100 | lrbp->compl_time_stamp = ktime_get(); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5101 | cmd = lrbp->cmd; |
| 5102 | if (cmd) { |
Bean Huo | 28fa68f | 2021-01-05 12:34:42 +0100 | [diff] [blame] | 5103 | ufshcd_add_command_trace(hba, index, UFS_CMD_COMP); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5104 | result = ufshcd_transfer_rsp_status(hba, lrbp); |
| 5105 | scsi_dma_unmap(cmd); |
| 5106 | cmd->result = result; |
| 5107 | /* Mark completed command as NULL in LRB */ |
| 5108 | lrbp->cmd = NULL; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5109 | /* Do not touch lrbp after scsi done */ |
| 5110 | cmd->scsi_done(cmd); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5111 | __ufshcd_release(hba); |
Can Guo | 7a7e66c | 2020-12-02 04:04:02 -0800 | [diff] [blame] | 5112 | update_scaling = true; |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 5113 | } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE || |
| 5114 | lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) { |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 5115 | if (hba->dev_cmd.complete) { |
| 5116 | ufshcd_add_command_trace(hba, index, |
Bean Huo | 28fa68f | 2021-01-05 12:34:42 +0100 | [diff] [blame] | 5117 | UFS_DEV_COMP); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5118 | complete(hba->dev_cmd.complete); |
Can Guo | 7a7e66c | 2020-12-02 04:04:02 -0800 | [diff] [blame] | 5119 | update_scaling = true; |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 5120 | } |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5121 | } |
Can Guo | 7a7e66c | 2020-12-02 04:04:02 -0800 | [diff] [blame] | 5122 | if (ufshcd_is_clkscaling_supported(hba) && update_scaling) |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 5123 | hba->clk_scaling.active_reqs--; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5124 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5125 | |
| 5126 | /* clear corresponding bits of completed commands */ |
| 5127 | hba->outstanding_reqs ^= completed_reqs; |
| 5128 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 5129 | ufshcd_clk_scaling_update_busy(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5130 | } |
| 5131 | |
| 5132 | /** |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5133 | * ufshcd_transfer_req_compl - handle SCSI and query command completion |
| 5134 | * @hba: per adapter instance |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5135 | * |
| 5136 | * Returns |
| 5137 | * IRQ_HANDLED - If interrupt is valid |
| 5138 | * IRQ_NONE - If invalid interrupt |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5139 | */ |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5140 | static irqreturn_t ufshcd_transfer_req_compl(struct ufs_hba *hba) |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5141 | { |
| 5142 | unsigned long completed_reqs; |
| 5143 | u32 tr_doorbell; |
| 5144 | |
| 5145 | /* Resetting interrupt aggregation counters first and reading the |
| 5146 | * DOOR_BELL afterward allows us to handle all the completed requests. |
| 5147 | * In order to prevent other interrupts starvation the DB is read once |
| 5148 | * after reset. The down side of this solution is the possibility of |
| 5149 | * false interrupt if device completes another request after resetting |
| 5150 | * aggregation and before reading the DB. |
| 5151 | */ |
Alim Akhtar | b638b5e | 2020-05-28 06:46:50 +0530 | [diff] [blame] | 5152 | if (ufshcd_is_intr_aggr_allowed(hba) && |
| 5153 | !(hba->quirks & UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR)) |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5154 | ufshcd_reset_intr_aggr(hba); |
| 5155 | |
| 5156 | tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 5157 | completed_reqs = tr_doorbell ^ hba->outstanding_reqs; |
| 5158 | |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 5159 | if (completed_reqs) { |
| 5160 | __ufshcd_transfer_req_compl(hba, completed_reqs); |
| 5161 | return IRQ_HANDLED; |
| 5162 | } else { |
| 5163 | return IRQ_NONE; |
| 5164 | } |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5165 | } |
| 5166 | |
| 5167 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5168 | * ufshcd_disable_ee - disable exception event |
| 5169 | * @hba: per-adapter instance |
| 5170 | * @mask: exception event to disable |
| 5171 | * |
| 5172 | * Disables exception event in the device so that the EVENT_ALERT |
| 5173 | * bit is not set. |
| 5174 | * |
| 5175 | * Returns zero on success, non-zero error value on failure. |
| 5176 | */ |
| 5177 | static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask) |
| 5178 | { |
| 5179 | int err = 0; |
| 5180 | u32 val; |
| 5181 | |
| 5182 | if (!(hba->ee_ctrl_mask & mask)) |
| 5183 | goto out; |
| 5184 | |
| 5185 | val = hba->ee_ctrl_mask & ~mask; |
Tomohiro Kusumi | d7e2ddd | 2017-04-20 15:01:44 +0300 | [diff] [blame] | 5186 | val &= MASK_EE_STATUS; |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 5187 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5188 | QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val); |
| 5189 | if (!err) |
| 5190 | hba->ee_ctrl_mask &= ~mask; |
| 5191 | out: |
| 5192 | return err; |
| 5193 | } |
| 5194 | |
| 5195 | /** |
| 5196 | * ufshcd_enable_ee - enable exception event |
| 5197 | * @hba: per-adapter instance |
| 5198 | * @mask: exception event to enable |
| 5199 | * |
| 5200 | * Enable corresponding exception event in the device to allow |
| 5201 | * device to alert host in critical scenarios. |
| 5202 | * |
| 5203 | * Returns zero on success, non-zero error value on failure. |
| 5204 | */ |
| 5205 | static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask) |
| 5206 | { |
| 5207 | int err = 0; |
| 5208 | u32 val; |
| 5209 | |
| 5210 | if (hba->ee_ctrl_mask & mask) |
| 5211 | goto out; |
| 5212 | |
| 5213 | val = hba->ee_ctrl_mask | mask; |
Tomohiro Kusumi | d7e2ddd | 2017-04-20 15:01:44 +0300 | [diff] [blame] | 5214 | val &= MASK_EE_STATUS; |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 5215 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5216 | QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val); |
| 5217 | if (!err) |
| 5218 | hba->ee_ctrl_mask |= mask; |
| 5219 | out: |
| 5220 | return err; |
| 5221 | } |
| 5222 | |
| 5223 | /** |
| 5224 | * ufshcd_enable_auto_bkops - Allow device managed BKOPS |
| 5225 | * @hba: per-adapter instance |
| 5226 | * |
| 5227 | * Allow device to manage background operations on its own. Enabling |
| 5228 | * this might lead to inconsistent latencies during normal data transfers |
| 5229 | * as the device is allowed to manage its own way of handling background |
| 5230 | * operations. |
| 5231 | * |
| 5232 | * Returns zero on success, non-zero on failure. |
| 5233 | */ |
| 5234 | static int ufshcd_enable_auto_bkops(struct ufs_hba *hba) |
| 5235 | { |
| 5236 | int err = 0; |
| 5237 | |
| 5238 | if (hba->auto_bkops_enabled) |
| 5239 | goto out; |
| 5240 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 5241 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG, |
Stanley Chu | 1f34eed | 2020-05-08 16:01:12 +0800 | [diff] [blame] | 5242 | QUERY_FLAG_IDN_BKOPS_EN, 0, NULL); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5243 | if (err) { |
| 5244 | dev_err(hba->dev, "%s: failed to enable bkops %d\n", |
| 5245 | __func__, err); |
| 5246 | goto out; |
| 5247 | } |
| 5248 | |
| 5249 | hba->auto_bkops_enabled = true; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 5250 | trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Enabled"); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5251 | |
| 5252 | /* No need of URGENT_BKOPS exception from the device */ |
| 5253 | err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 5254 | if (err) |
| 5255 | dev_err(hba->dev, "%s: failed to disable exception event %d\n", |
| 5256 | __func__, err); |
| 5257 | out: |
| 5258 | return err; |
| 5259 | } |
| 5260 | |
| 5261 | /** |
| 5262 | * ufshcd_disable_auto_bkops - block device in doing background operations |
| 5263 | * @hba: per-adapter instance |
| 5264 | * |
| 5265 | * Disabling background operations improves command response latency but |
| 5266 | * has drawback of device moving into critical state where the device is |
| 5267 | * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the |
| 5268 | * host is idle so that BKOPS are managed effectively without any negative |
| 5269 | * impacts. |
| 5270 | * |
| 5271 | * Returns zero on success, non-zero on failure. |
| 5272 | */ |
| 5273 | static int ufshcd_disable_auto_bkops(struct ufs_hba *hba) |
| 5274 | { |
| 5275 | int err = 0; |
| 5276 | |
| 5277 | if (!hba->auto_bkops_enabled) |
| 5278 | goto out; |
| 5279 | |
| 5280 | /* |
| 5281 | * If host assisted BKOPs is to be enabled, make sure |
| 5282 | * urgent bkops exception is allowed. |
| 5283 | */ |
| 5284 | err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 5285 | if (err) { |
| 5286 | dev_err(hba->dev, "%s: failed to enable exception event %d\n", |
| 5287 | __func__, err); |
| 5288 | goto out; |
| 5289 | } |
| 5290 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 5291 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG, |
Stanley Chu | 1f34eed | 2020-05-08 16:01:12 +0800 | [diff] [blame] | 5292 | QUERY_FLAG_IDN_BKOPS_EN, 0, NULL); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5293 | if (err) { |
| 5294 | dev_err(hba->dev, "%s: failed to disable bkops %d\n", |
| 5295 | __func__, err); |
| 5296 | ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 5297 | goto out; |
| 5298 | } |
| 5299 | |
| 5300 | hba->auto_bkops_enabled = false; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 5301 | trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Disabled"); |
Asutosh Das | 24366c2a | 2019-11-25 22:53:30 -0800 | [diff] [blame] | 5302 | hba->is_urgent_bkops_lvl_checked = false; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5303 | out: |
| 5304 | return err; |
| 5305 | } |
| 5306 | |
| 5307 | /** |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 5308 | * ufshcd_force_reset_auto_bkops - force reset auto bkops state |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5309 | * @hba: per adapter instance |
| 5310 | * |
| 5311 | * After a device reset the device may toggle the BKOPS_EN flag |
| 5312 | * to default value. The s/w tracking variables should be updated |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 5313 | * as well. This function would change the auto-bkops state based on |
| 5314 | * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5315 | */ |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 5316 | static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5317 | { |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 5318 | if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) { |
| 5319 | hba->auto_bkops_enabled = false; |
| 5320 | hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS; |
| 5321 | ufshcd_enable_auto_bkops(hba); |
| 5322 | } else { |
| 5323 | hba->auto_bkops_enabled = true; |
| 5324 | hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS; |
| 5325 | ufshcd_disable_auto_bkops(hba); |
| 5326 | } |
Stanley Chu | 7b6668d | 2020-05-30 22:12:00 +0800 | [diff] [blame] | 5327 | hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT; |
Asutosh Das | 24366c2a | 2019-11-25 22:53:30 -0800 | [diff] [blame] | 5328 | hba->is_urgent_bkops_lvl_checked = false; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5329 | } |
| 5330 | |
| 5331 | static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status) |
| 5332 | { |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 5333 | return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5334 | QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status); |
| 5335 | } |
| 5336 | |
| 5337 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5338 | * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status |
| 5339 | * @hba: per-adapter instance |
| 5340 | * @status: bkops_status value |
| 5341 | * |
| 5342 | * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn |
| 5343 | * flag in the device to permit background operations if the device |
| 5344 | * bkops_status is greater than or equal to "status" argument passed to |
| 5345 | * this function, disable otherwise. |
| 5346 | * |
| 5347 | * Returns 0 for success, non-zero in case of failure. |
| 5348 | * |
| 5349 | * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag |
| 5350 | * to know whether auto bkops is enabled or disabled after this function |
| 5351 | * returns control to it. |
| 5352 | */ |
| 5353 | static int ufshcd_bkops_ctrl(struct ufs_hba *hba, |
| 5354 | enum bkops_status status) |
| 5355 | { |
| 5356 | int err; |
| 5357 | u32 curr_status = 0; |
| 5358 | |
| 5359 | err = ufshcd_get_bkops_status(hba, &curr_status); |
| 5360 | if (err) { |
| 5361 | dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", |
| 5362 | __func__, err); |
| 5363 | goto out; |
| 5364 | } else if (curr_status > BKOPS_STATUS_MAX) { |
| 5365 | dev_err(hba->dev, "%s: invalid BKOPS status %d\n", |
| 5366 | __func__, curr_status); |
| 5367 | err = -EINVAL; |
| 5368 | goto out; |
| 5369 | } |
| 5370 | |
| 5371 | if (curr_status >= status) |
| 5372 | err = ufshcd_enable_auto_bkops(hba); |
| 5373 | else |
| 5374 | err = ufshcd_disable_auto_bkops(hba); |
| 5375 | out: |
| 5376 | return err; |
| 5377 | } |
| 5378 | |
| 5379 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5380 | * ufshcd_urgent_bkops - handle urgent bkops exception event |
| 5381 | * @hba: per-adapter instance |
| 5382 | * |
| 5383 | * Enable fBackgroundOpsEn flag in the device to permit background |
| 5384 | * operations. |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5385 | * |
| 5386 | * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled |
| 5387 | * and negative error value for any other failure. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5388 | */ |
| 5389 | static int ufshcd_urgent_bkops(struct ufs_hba *hba) |
| 5390 | { |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 5391 | return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5392 | } |
| 5393 | |
| 5394 | static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status) |
| 5395 | { |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 5396 | return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5397 | QUERY_ATTR_IDN_EE_STATUS, 0, 0, status); |
| 5398 | } |
| 5399 | |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 5400 | static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba) |
| 5401 | { |
| 5402 | int err; |
| 5403 | u32 curr_status = 0; |
| 5404 | |
| 5405 | if (hba->is_urgent_bkops_lvl_checked) |
| 5406 | goto enable_auto_bkops; |
| 5407 | |
| 5408 | err = ufshcd_get_bkops_status(hba, &curr_status); |
| 5409 | if (err) { |
| 5410 | dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", |
| 5411 | __func__, err); |
| 5412 | goto out; |
| 5413 | } |
| 5414 | |
| 5415 | /* |
| 5416 | * We are seeing that some devices are raising the urgent bkops |
| 5417 | * exception events even when BKOPS status doesn't indicate performace |
| 5418 | * impacted or critical. Handle these device by determining their urgent |
| 5419 | * bkops status at runtime. |
| 5420 | */ |
| 5421 | if (curr_status < BKOPS_STATUS_PERF_IMPACT) { |
| 5422 | dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n", |
| 5423 | __func__, curr_status); |
| 5424 | /* update the current status as the urgent bkops level */ |
| 5425 | hba->urgent_bkops_lvl = curr_status; |
| 5426 | hba->is_urgent_bkops_lvl_checked = true; |
| 5427 | } |
| 5428 | |
| 5429 | enable_auto_bkops: |
| 5430 | err = ufshcd_enable_auto_bkops(hba); |
| 5431 | out: |
| 5432 | if (err < 0) |
| 5433 | dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n", |
| 5434 | __func__, err); |
| 5435 | } |
| 5436 | |
Bean Huo | 8e834ca | 2021-01-19 17:38:42 +0100 | [diff] [blame] | 5437 | int ufshcd_wb_ctrl(struct ufs_hba *hba, bool enable) |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5438 | { |
| 5439 | int ret; |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 5440 | u8 index; |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5441 | enum query_opcode opcode; |
| 5442 | |
Stanley Chu | 79e3520 | 2020-05-08 16:01:15 +0800 | [diff] [blame] | 5443 | if (!ufshcd_is_wb_allowed(hba)) |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5444 | return 0; |
| 5445 | |
Bean Huo | 4cd4899 | 2021-01-19 17:38:46 +0100 | [diff] [blame] | 5446 | if (!(enable ^ hba->dev_info.wb_enabled)) |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5447 | return 0; |
| 5448 | if (enable) |
| 5449 | opcode = UPIU_QUERY_OPCODE_SET_FLAG; |
| 5450 | else |
| 5451 | opcode = UPIU_QUERY_OPCODE_CLEAR_FLAG; |
| 5452 | |
Stanley Chu | e31011a | 2020-05-22 16:32:11 +0800 | [diff] [blame] | 5453 | index = ufshcd_wb_get_query_index(hba); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5454 | ret = ufshcd_query_flag_retry(hba, opcode, |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 5455 | QUERY_FLAG_IDN_WB_EN, index, NULL); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5456 | if (ret) { |
| 5457 | dev_err(hba->dev, "%s write booster %s failed %d\n", |
| 5458 | __func__, enable ? "enable" : "disable", ret); |
| 5459 | return ret; |
| 5460 | } |
| 5461 | |
Bean Huo | 4cd4899 | 2021-01-19 17:38:46 +0100 | [diff] [blame] | 5462 | hba->dev_info.wb_enabled = enable; |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5463 | dev_dbg(hba->dev, "%s write booster %s %d\n", |
| 5464 | __func__, enable ? "enable" : "disable", ret); |
| 5465 | |
| 5466 | return ret; |
| 5467 | } |
| 5468 | |
| 5469 | static int ufshcd_wb_toggle_flush_during_h8(struct ufs_hba *hba, bool set) |
| 5470 | { |
| 5471 | int val; |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 5472 | u8 index; |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5473 | |
| 5474 | if (set) |
| 5475 | val = UPIU_QUERY_OPCODE_SET_FLAG; |
| 5476 | else |
| 5477 | val = UPIU_QUERY_OPCODE_CLEAR_FLAG; |
| 5478 | |
Stanley Chu | e31011a | 2020-05-22 16:32:11 +0800 | [diff] [blame] | 5479 | index = ufshcd_wb_get_query_index(hba); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5480 | return ufshcd_query_flag_retry(hba, val, |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 5481 | QUERY_FLAG_IDN_WB_BUFF_FLUSH_DURING_HIBERN8, |
| 5482 | index, NULL); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5483 | } |
| 5484 | |
| 5485 | static inline void ufshcd_wb_toggle_flush(struct ufs_hba *hba, bool enable) |
| 5486 | { |
| 5487 | if (enable) |
| 5488 | ufshcd_wb_buf_flush_enable(hba); |
| 5489 | else |
| 5490 | ufshcd_wb_buf_flush_disable(hba); |
| 5491 | |
| 5492 | } |
| 5493 | |
| 5494 | static int ufshcd_wb_buf_flush_enable(struct ufs_hba *hba) |
| 5495 | { |
| 5496 | int ret; |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 5497 | u8 index; |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5498 | |
Bean Huo | 4cd4899 | 2021-01-19 17:38:46 +0100 | [diff] [blame] | 5499 | if (!ufshcd_is_wb_allowed(hba) || hba->dev_info.wb_buf_flush_enabled) |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5500 | return 0; |
| 5501 | |
Stanley Chu | e31011a | 2020-05-22 16:32:11 +0800 | [diff] [blame] | 5502 | index = ufshcd_wb_get_query_index(hba); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5503 | ret = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG, |
Stanley Chu | 1f34eed | 2020-05-08 16:01:12 +0800 | [diff] [blame] | 5504 | QUERY_FLAG_IDN_WB_BUFF_FLUSH_EN, |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 5505 | index, NULL); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5506 | if (ret) |
| 5507 | dev_err(hba->dev, "%s WB - buf flush enable failed %d\n", |
| 5508 | __func__, ret); |
| 5509 | else |
Bean Huo | 4cd4899 | 2021-01-19 17:38:46 +0100 | [diff] [blame] | 5510 | hba->dev_info.wb_buf_flush_enabled = true; |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5511 | |
| 5512 | dev_dbg(hba->dev, "WB - Flush enabled: %d\n", ret); |
| 5513 | return ret; |
| 5514 | } |
| 5515 | |
| 5516 | static int ufshcd_wb_buf_flush_disable(struct ufs_hba *hba) |
| 5517 | { |
| 5518 | int ret; |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 5519 | u8 index; |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5520 | |
Bean Huo | 4cd4899 | 2021-01-19 17:38:46 +0100 | [diff] [blame] | 5521 | if (!ufshcd_is_wb_allowed(hba) || !hba->dev_info.wb_buf_flush_enabled) |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5522 | return 0; |
| 5523 | |
Stanley Chu | e31011a | 2020-05-22 16:32:11 +0800 | [diff] [blame] | 5524 | index = ufshcd_wb_get_query_index(hba); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5525 | ret = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG, |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 5526 | QUERY_FLAG_IDN_WB_BUFF_FLUSH_EN, |
| 5527 | index, NULL); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5528 | if (ret) { |
| 5529 | dev_warn(hba->dev, "%s: WB - buf flush disable failed %d\n", |
| 5530 | __func__, ret); |
| 5531 | } else { |
Bean Huo | 4cd4899 | 2021-01-19 17:38:46 +0100 | [diff] [blame] | 5532 | hba->dev_info.wb_buf_flush_enabled = false; |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5533 | dev_dbg(hba->dev, "WB - Flush disabled: %d\n", ret); |
| 5534 | } |
| 5535 | |
| 5536 | return ret; |
| 5537 | } |
| 5538 | |
| 5539 | static bool ufshcd_wb_presrv_usrspc_keep_vcc_on(struct ufs_hba *hba, |
| 5540 | u32 avail_buf) |
| 5541 | { |
| 5542 | u32 cur_buf; |
| 5543 | int ret; |
Stanley Chu | e31011a | 2020-05-22 16:32:11 +0800 | [diff] [blame] | 5544 | u8 index; |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5545 | |
Stanley Chu | e31011a | 2020-05-22 16:32:11 +0800 | [diff] [blame] | 5546 | index = ufshcd_wb_get_query_index(hba); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5547 | ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
| 5548 | QUERY_ATTR_IDN_CURR_WB_BUFF_SIZE, |
Stanley Chu | e31011a | 2020-05-22 16:32:11 +0800 | [diff] [blame] | 5549 | index, 0, &cur_buf); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5550 | if (ret) { |
| 5551 | dev_err(hba->dev, "%s dCurWriteBoosterBufferSize read failed %d\n", |
| 5552 | __func__, ret); |
| 5553 | return false; |
| 5554 | } |
| 5555 | |
| 5556 | if (!cur_buf) { |
| 5557 | dev_info(hba->dev, "dCurWBBuf: %d WB disabled until free-space is available\n", |
| 5558 | cur_buf); |
| 5559 | return false; |
| 5560 | } |
Stanley Chu | d14734ae | 2020-05-09 17:37:15 +0800 | [diff] [blame] | 5561 | /* Let it continue to flush when available buffer exceeds threshold */ |
| 5562 | if (avail_buf < hba->vps->wb_flush_threshold) |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5563 | return true; |
| 5564 | |
| 5565 | return false; |
| 5566 | } |
| 5567 | |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 5568 | static bool ufshcd_wb_need_flush(struct ufs_hba *hba) |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5569 | { |
| 5570 | int ret; |
| 5571 | u32 avail_buf; |
Stanley Chu | e31011a | 2020-05-22 16:32:11 +0800 | [diff] [blame] | 5572 | u8 index; |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5573 | |
Stanley Chu | 79e3520 | 2020-05-08 16:01:15 +0800 | [diff] [blame] | 5574 | if (!ufshcd_is_wb_allowed(hba)) |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5575 | return false; |
| 5576 | /* |
| 5577 | * The ufs device needs the vcc to be ON to flush. |
| 5578 | * With user-space reduction enabled, it's enough to enable flush |
| 5579 | * by checking only the available buffer. The threshold |
| 5580 | * defined here is > 90% full. |
| 5581 | * With user-space preserved enabled, the current-buffer |
| 5582 | * should be checked too because the wb buffer size can reduce |
| 5583 | * when disk tends to be full. This info is provided by current |
| 5584 | * buffer (dCurrentWriteBoosterBufferSize). There's no point in |
| 5585 | * keeping vcc on when current buffer is empty. |
| 5586 | */ |
Stanley Chu | e31011a | 2020-05-22 16:32:11 +0800 | [diff] [blame] | 5587 | index = ufshcd_wb_get_query_index(hba); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5588 | ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
| 5589 | QUERY_ATTR_IDN_AVAIL_WB_BUFF_SIZE, |
Stanley Chu | e31011a | 2020-05-22 16:32:11 +0800 | [diff] [blame] | 5590 | index, 0, &avail_buf); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5591 | if (ret) { |
| 5592 | dev_warn(hba->dev, "%s dAvailableWriteBoosterBufferSize read failed %d\n", |
| 5593 | __func__, ret); |
| 5594 | return false; |
| 5595 | } |
| 5596 | |
| 5597 | if (!hba->dev_info.b_presrv_uspc_en) { |
Stanley Chu | d14734ae | 2020-05-09 17:37:15 +0800 | [diff] [blame] | 5598 | if (avail_buf <= UFS_WB_BUF_REMAIN_PERCENT(10)) |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 5599 | return true; |
| 5600 | return false; |
| 5601 | } |
| 5602 | |
| 5603 | return ufshcd_wb_presrv_usrspc_keep_vcc_on(hba, avail_buf); |
| 5604 | } |
| 5605 | |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 5606 | static void ufshcd_rpm_dev_flush_recheck_work(struct work_struct *work) |
| 5607 | { |
| 5608 | struct ufs_hba *hba = container_of(to_delayed_work(work), |
| 5609 | struct ufs_hba, |
| 5610 | rpm_dev_flush_recheck_work); |
| 5611 | /* |
| 5612 | * To prevent unnecessary VCC power drain after device finishes |
| 5613 | * WriteBooster buffer flush or Auto BKOPs, force runtime resume |
| 5614 | * after a certain delay to recheck the threshold by next runtime |
| 5615 | * suspend. |
| 5616 | */ |
| 5617 | pm_runtime_get_sync(hba->dev); |
| 5618 | pm_runtime_put_sync(hba->dev); |
| 5619 | } |
| 5620 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5621 | /** |
| 5622 | * ufshcd_exception_event_handler - handle exceptions raised by device |
| 5623 | * @work: pointer to work data |
| 5624 | * |
| 5625 | * Read bExceptionEventStatus attribute from the device and handle the |
| 5626 | * exception event accordingly. |
| 5627 | */ |
| 5628 | static void ufshcd_exception_event_handler(struct work_struct *work) |
| 5629 | { |
| 5630 | struct ufs_hba *hba; |
| 5631 | int err; |
| 5632 | u32 status = 0; |
| 5633 | hba = container_of(work, struct ufs_hba, eeh_work); |
| 5634 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 5635 | pm_runtime_get_sync(hba->dev); |
Stanley Chu | 03e1d28 | 2019-12-24 21:01:05 +0800 | [diff] [blame] | 5636 | ufshcd_scsi_block_requests(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5637 | err = ufshcd_get_ee_status(hba, &status); |
| 5638 | if (err) { |
| 5639 | dev_err(hba->dev, "%s: failed to get exception status %d\n", |
| 5640 | __func__, err); |
| 5641 | goto out; |
| 5642 | } |
| 5643 | |
| 5644 | status &= hba->ee_ctrl_mask; |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 5645 | |
| 5646 | if (status & MASK_EE_URGENT_BKOPS) |
| 5647 | ufshcd_bkops_exception_event_handler(hba); |
| 5648 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5649 | out: |
Stanley Chu | 03e1d28 | 2019-12-24 21:01:05 +0800 | [diff] [blame] | 5650 | ufshcd_scsi_unblock_requests(hba); |
Sayali Lokhande | 2824ec9 | 2020-02-10 19:40:44 -0800 | [diff] [blame] | 5651 | /* |
| 5652 | * pm_runtime_get_noresume is called while scheduling |
| 5653 | * eeh_work to avoid suspend racing with exception work. |
| 5654 | * Hence decrement usage counter using pm_runtime_put_noidle |
| 5655 | * to allow suspend on completion of exception event handler. |
| 5656 | */ |
| 5657 | pm_runtime_put_noidle(hba->dev); |
| 5658 | pm_runtime_put(hba->dev); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5659 | return; |
| 5660 | } |
| 5661 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5662 | /* Complete requests that have door-bell cleared */ |
| 5663 | static void ufshcd_complete_requests(struct ufs_hba *hba) |
| 5664 | { |
| 5665 | ufshcd_transfer_req_compl(hba); |
| 5666 | ufshcd_tmc_handler(hba); |
| 5667 | } |
| 5668 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5669 | /** |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5670 | * ufshcd_quirk_dl_nac_errors - This function checks if error handling is |
| 5671 | * to recover from the DL NAC errors or not. |
| 5672 | * @hba: per-adapter instance |
| 5673 | * |
| 5674 | * Returns true if error handling is required, false otherwise |
| 5675 | */ |
| 5676 | static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba) |
| 5677 | { |
| 5678 | unsigned long flags; |
| 5679 | bool err_handling = true; |
| 5680 | |
| 5681 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5682 | /* |
| 5683 | * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the |
| 5684 | * device fatal error and/or DL NAC & REPLAY timeout errors. |
| 5685 | */ |
| 5686 | if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR)) |
| 5687 | goto out; |
| 5688 | |
| 5689 | if ((hba->saved_err & DEVICE_FATAL_ERROR) || |
| 5690 | ((hba->saved_err & UIC_ERROR) && |
| 5691 | (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR))) |
| 5692 | goto out; |
| 5693 | |
| 5694 | if ((hba->saved_err & UIC_ERROR) && |
| 5695 | (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) { |
| 5696 | int err; |
| 5697 | /* |
| 5698 | * wait for 50ms to see if we can get any other errors or not. |
| 5699 | */ |
| 5700 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5701 | msleep(50); |
| 5702 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5703 | |
| 5704 | /* |
| 5705 | * now check if we have got any other severe errors other than |
| 5706 | * DL NAC error? |
| 5707 | */ |
| 5708 | if ((hba->saved_err & INT_FATAL_ERRORS) || |
| 5709 | ((hba->saved_err & UIC_ERROR) && |
| 5710 | (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR))) |
| 5711 | goto out; |
| 5712 | |
| 5713 | /* |
| 5714 | * As DL NAC is the only error received so far, send out NOP |
| 5715 | * command to confirm if link is still active or not. |
| 5716 | * - If we don't get any response then do error recovery. |
| 5717 | * - If we get response then clear the DL NAC error bit. |
| 5718 | */ |
| 5719 | |
| 5720 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5721 | err = ufshcd_verify_dev_init(hba); |
| 5722 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5723 | |
| 5724 | if (err) |
| 5725 | goto out; |
| 5726 | |
| 5727 | /* Link seems to be alive hence ignore the DL NAC errors */ |
| 5728 | if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR) |
| 5729 | hba->saved_err &= ~UIC_ERROR; |
| 5730 | /* clear NAC error */ |
| 5731 | hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR; |
Bean Huo | b000862 | 2020-08-14 11:50:34 +0200 | [diff] [blame] | 5732 | if (!hba->saved_uic_err) |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5733 | err_handling = false; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5734 | } |
| 5735 | out: |
| 5736 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5737 | return err_handling; |
| 5738 | } |
| 5739 | |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 5740 | /* host lock must be held before calling this func */ |
| 5741 | static inline bool ufshcd_is_saved_err_fatal(struct ufs_hba *hba) |
| 5742 | { |
| 5743 | return (hba->saved_uic_err & UFSHCD_UIC_DL_PA_INIT_ERROR) || |
| 5744 | (hba->saved_err & (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK)); |
| 5745 | } |
| 5746 | |
| 5747 | /* host lock must be held before calling this func */ |
| 5748 | static inline void ufshcd_schedule_eh_work(struct ufs_hba *hba) |
| 5749 | { |
| 5750 | /* handle fatal errors only when link is not in error state */ |
| 5751 | if (hba->ufshcd_state != UFSHCD_STATE_ERROR) { |
Can Guo | 5586dd8 | 2020-08-09 05:15:54 -0700 | [diff] [blame] | 5752 | if (hba->force_reset || ufshcd_is_link_broken(hba) || |
| 5753 | ufshcd_is_saved_err_fatal(hba)) |
| 5754 | hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED_FATAL; |
| 5755 | else |
| 5756 | hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED_NON_FATAL; |
| 5757 | queue_work(hba->eh_wq, &hba->eh_work); |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 5758 | } |
| 5759 | } |
| 5760 | |
Can Guo | c72e79c | 2020-08-09 05:15:52 -0700 | [diff] [blame] | 5761 | static void ufshcd_err_handling_prepare(struct ufs_hba *hba) |
| 5762 | { |
| 5763 | pm_runtime_get_sync(hba->dev); |
Can Guo | 88a92d6 | 2020-12-02 04:04:01 -0800 | [diff] [blame] | 5764 | if (pm_runtime_status_suspended(hba->dev) || hba->is_sys_suspended) { |
| 5765 | enum ufs_pm_op pm_op; |
| 5766 | |
Can Guo | c72e79c | 2020-08-09 05:15:52 -0700 | [diff] [blame] | 5767 | /* |
| 5768 | * Don't assume anything of pm_runtime_get_sync(), if |
| 5769 | * resume fails, irq and clocks can be OFF, and powers |
| 5770 | * can be OFF or in LPM. |
| 5771 | */ |
| 5772 | ufshcd_setup_hba_vreg(hba, true); |
| 5773 | ufshcd_enable_irq(hba); |
| 5774 | ufshcd_setup_vreg(hba, true); |
| 5775 | ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq); |
| 5776 | ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2); |
| 5777 | ufshcd_hold(hba, false); |
| 5778 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 5779 | ufshcd_setup_clocks(hba, true); |
| 5780 | ufshcd_release(hba); |
Can Guo | 88a92d6 | 2020-12-02 04:04:01 -0800 | [diff] [blame] | 5781 | pm_op = hba->is_sys_suspended ? UFS_SYSTEM_PM : UFS_RUNTIME_PM; |
| 5782 | ufshcd_vops_resume(hba, pm_op); |
Can Guo | c72e79c | 2020-08-09 05:15:52 -0700 | [diff] [blame] | 5783 | } else { |
| 5784 | ufshcd_hold(hba, false); |
Stanley Chu | f9a7fa3 | 2021-01-20 23:01:40 +0800 | [diff] [blame] | 5785 | if (hba->clk_scaling.is_enabled) |
Can Guo | c72e79c | 2020-08-09 05:15:52 -0700 | [diff] [blame] | 5786 | ufshcd_suspend_clkscaling(hba); |
Can Guo | 0e9d4ca | 2021-01-20 02:04:21 -0800 | [diff] [blame] | 5787 | down_write(&hba->clk_scaling_lock); |
| 5788 | hba->clk_scaling.is_allowed = false; |
| 5789 | up_write(&hba->clk_scaling_lock); |
Can Guo | c72e79c | 2020-08-09 05:15:52 -0700 | [diff] [blame] | 5790 | } |
| 5791 | } |
| 5792 | |
| 5793 | static void ufshcd_err_handling_unprepare(struct ufs_hba *hba) |
| 5794 | { |
| 5795 | ufshcd_release(hba); |
Can Guo | 0e9d4ca | 2021-01-20 02:04:21 -0800 | [diff] [blame] | 5796 | down_write(&hba->clk_scaling_lock); |
| 5797 | hba->clk_scaling.is_allowed = true; |
| 5798 | up_write(&hba->clk_scaling_lock); |
| 5799 | if (hba->clk_scaling.is_enabled) |
Can Guo | c72e79c | 2020-08-09 05:15:52 -0700 | [diff] [blame] | 5800 | ufshcd_resume_clkscaling(hba); |
| 5801 | pm_runtime_put(hba->dev); |
| 5802 | } |
| 5803 | |
| 5804 | static inline bool ufshcd_err_handling_should_stop(struct ufs_hba *hba) |
| 5805 | { |
Can Guo | 9cd20d3 | 2021-01-13 19:13:28 -0800 | [diff] [blame] | 5806 | return (!hba->is_powered || hba->shutting_down || |
| 5807 | hba->ufshcd_state == UFSHCD_STATE_ERROR || |
Can Guo | c72e79c | 2020-08-09 05:15:52 -0700 | [diff] [blame] | 5808 | (!(hba->saved_err || hba->saved_uic_err || hba->force_reset || |
Can Guo | 9cd20d3 | 2021-01-13 19:13:28 -0800 | [diff] [blame] | 5809 | ufshcd_is_link_broken(hba)))); |
Can Guo | c72e79c | 2020-08-09 05:15:52 -0700 | [diff] [blame] | 5810 | } |
| 5811 | |
| 5812 | #ifdef CONFIG_PM |
| 5813 | static void ufshcd_recover_pm_error(struct ufs_hba *hba) |
| 5814 | { |
| 5815 | struct Scsi_Host *shost = hba->host; |
| 5816 | struct scsi_device *sdev; |
| 5817 | struct request_queue *q; |
| 5818 | int ret; |
| 5819 | |
Can Guo | 88a92d6 | 2020-12-02 04:04:01 -0800 | [diff] [blame] | 5820 | hba->is_sys_suspended = false; |
Can Guo | c72e79c | 2020-08-09 05:15:52 -0700 | [diff] [blame] | 5821 | /* |
| 5822 | * Set RPM status of hba device to RPM_ACTIVE, |
| 5823 | * this also clears its runtime error. |
| 5824 | */ |
| 5825 | ret = pm_runtime_set_active(hba->dev); |
| 5826 | /* |
| 5827 | * If hba device had runtime error, we also need to resume those |
| 5828 | * scsi devices under hba in case any of them has failed to be |
| 5829 | * resumed due to hba runtime resume failure. This is to unblock |
| 5830 | * blk_queue_enter in case there are bios waiting inside it. |
| 5831 | */ |
| 5832 | if (!ret) { |
| 5833 | shost_for_each_device(sdev, shost) { |
| 5834 | q = sdev->request_queue; |
| 5835 | if (q->dev && (q->rpm_status == RPM_SUSPENDED || |
| 5836 | q->rpm_status == RPM_SUSPENDING)) |
| 5837 | pm_request_resume(q->dev); |
| 5838 | } |
| 5839 | } |
| 5840 | } |
| 5841 | #else |
| 5842 | static inline void ufshcd_recover_pm_error(struct ufs_hba *hba) |
| 5843 | { |
| 5844 | } |
| 5845 | #endif |
| 5846 | |
Can Guo | 2355b66 | 2020-08-24 19:07:06 -0700 | [diff] [blame] | 5847 | static bool ufshcd_is_pwr_mode_restore_needed(struct ufs_hba *hba) |
| 5848 | { |
| 5849 | struct ufs_pa_layer_attr *pwr_info = &hba->pwr_info; |
| 5850 | u32 mode; |
| 5851 | |
| 5852 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_PWRMODE), &mode); |
| 5853 | |
| 5854 | if (pwr_info->pwr_rx != ((mode >> PWRMODE_RX_OFFSET) & PWRMODE_MASK)) |
| 5855 | return true; |
| 5856 | |
| 5857 | if (pwr_info->pwr_tx != (mode & PWRMODE_MASK)) |
| 5858 | return true; |
| 5859 | |
| 5860 | return false; |
| 5861 | } |
| 5862 | |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5863 | /** |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5864 | * ufshcd_err_handler - handle UFS errors that require s/w attention |
| 5865 | * @work: pointer to work structure |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5866 | */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5867 | static void ufshcd_err_handler(struct work_struct *work) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5868 | { |
| 5869 | struct ufs_hba *hba; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5870 | unsigned long flags; |
Can Guo | 307348f | 2020-08-24 19:07:05 -0700 | [diff] [blame] | 5871 | bool err_xfer = false; |
| 5872 | bool err_tm = false; |
Can Guo | 2355b66 | 2020-08-24 19:07:06 -0700 | [diff] [blame] | 5873 | int err = 0, pmc_err; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5874 | int tag; |
Can Guo | 2355b66 | 2020-08-24 19:07:06 -0700 | [diff] [blame] | 5875 | bool needs_reset = false, needs_restore = false; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5876 | |
| 5877 | hba = container_of(work, struct ufs_hba, eh_work); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5878 | |
Can Guo | 9cd20d3 | 2021-01-13 19:13:28 -0800 | [diff] [blame] | 5879 | down(&hba->host_sem); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5880 | spin_lock_irqsave(hba->host->host_lock, flags); |
Can Guo | c72e79c | 2020-08-09 05:15:52 -0700 | [diff] [blame] | 5881 | if (ufshcd_err_handling_should_stop(hba)) { |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 5882 | if (hba->ufshcd_state != UFSHCD_STATE_ERROR) |
| 5883 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
| 5884 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Can Guo | 9cd20d3 | 2021-01-13 19:13:28 -0800 | [diff] [blame] | 5885 | up(&hba->host_sem); |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 5886 | return; |
| 5887 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5888 | ufshcd_set_eh_in_progress(hba); |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 5889 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Can Guo | c72e79c | 2020-08-09 05:15:52 -0700 | [diff] [blame] | 5890 | ufshcd_err_handling_prepare(hba); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5891 | spin_lock_irqsave(hba->host->host_lock, flags); |
Can Guo | 5586dd8 | 2020-08-09 05:15:54 -0700 | [diff] [blame] | 5892 | ufshcd_scsi_block_requests(hba); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5893 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5894 | |
| 5895 | /* Complete requests that have door-bell cleared by h/w */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5896 | ufshcd_complete_requests(hba); |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5897 | |
Can Guo | 88a92d6 | 2020-12-02 04:04:01 -0800 | [diff] [blame] | 5898 | /* |
| 5899 | * A full reset and restore might have happened after preparation |
| 5900 | * is finished, double check whether we should stop. |
| 5901 | */ |
| 5902 | if (ufshcd_err_handling_should_stop(hba)) |
| 5903 | goto skip_err_handling; |
| 5904 | |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5905 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) { |
| 5906 | bool ret; |
| 5907 | |
| 5908 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5909 | /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */ |
| 5910 | ret = ufshcd_quirk_dl_nac_errors(hba); |
| 5911 | spin_lock_irqsave(hba->host->host_lock, flags); |
Can Guo | 88a92d6 | 2020-12-02 04:04:01 -0800 | [diff] [blame] | 5912 | if (!ret && ufshcd_err_handling_should_stop(hba)) |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5913 | goto skip_err_handling; |
| 5914 | } |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 5915 | |
Can Guo | 2355b66 | 2020-08-24 19:07:06 -0700 | [diff] [blame] | 5916 | if ((hba->saved_err & (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK)) || |
| 5917 | (hba->saved_uic_err && |
| 5918 | (hba->saved_uic_err != UFSHCD_UIC_PA_GENERIC_ERROR))) { |
Can Guo | c3be8d1e | 2020-08-09 05:15:53 -0700 | [diff] [blame] | 5919 | bool pr_prdt = !!(hba->saved_err & SYSTEM_BUS_FATAL_ERROR); |
| 5920 | |
| 5921 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5922 | ufshcd_print_host_state(hba); |
| 5923 | ufshcd_print_pwr_info(hba); |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 5924 | ufshcd_print_evt_hist(hba); |
Can Guo | c3be8d1e | 2020-08-09 05:15:53 -0700 | [diff] [blame] | 5925 | ufshcd_print_tmrs(hba, hba->outstanding_tasks); |
| 5926 | ufshcd_print_trs(hba, hba->outstanding_reqs, pr_prdt); |
| 5927 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5928 | } |
| 5929 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5930 | /* |
| 5931 | * if host reset is required then skip clearing the pending |
Can Guo | 2df74b6 | 2019-11-25 22:53:33 -0800 | [diff] [blame] | 5932 | * transfers forcefully because they will get cleared during |
| 5933 | * host reset and restore |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5934 | */ |
Can Guo | 88a92d6 | 2020-12-02 04:04:01 -0800 | [diff] [blame] | 5935 | if (hba->force_reset || ufshcd_is_link_broken(hba) || |
| 5936 | ufshcd_is_saved_err_fatal(hba) || |
| 5937 | ((hba->saved_err & UIC_ERROR) && |
| 5938 | (hba->saved_uic_err & (UFSHCD_UIC_DL_NAC_RECEIVED_ERROR | |
| 5939 | UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))) { |
| 5940 | needs_reset = true; |
Can Guo | 2355b66 | 2020-08-24 19:07:06 -0700 | [diff] [blame] | 5941 | goto do_reset; |
Can Guo | 88a92d6 | 2020-12-02 04:04:01 -0800 | [diff] [blame] | 5942 | } |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5943 | |
Can Guo | 2355b66 | 2020-08-24 19:07:06 -0700 | [diff] [blame] | 5944 | /* |
| 5945 | * If LINERESET was caught, UFS might have been put to PWM mode, |
| 5946 | * check if power mode restore is needed. |
| 5947 | */ |
| 5948 | if (hba->saved_uic_err & UFSHCD_UIC_PA_GENERIC_ERROR) { |
| 5949 | hba->saved_uic_err &= ~UFSHCD_UIC_PA_GENERIC_ERROR; |
| 5950 | if (!hba->saved_uic_err) |
| 5951 | hba->saved_err &= ~UIC_ERROR; |
| 5952 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5953 | if (ufshcd_is_pwr_mode_restore_needed(hba)) |
| 5954 | needs_restore = true; |
| 5955 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5956 | if (!hba->saved_err && !needs_restore) |
| 5957 | goto skip_err_handling; |
| 5958 | } |
| 5959 | |
| 5960 | hba->silence_err_logs = true; |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5961 | /* release lock as clear command might sleep */ |
| 5962 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5963 | /* Clear pending transfer requests */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5964 | for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) { |
Can Guo | 307348f | 2020-08-24 19:07:05 -0700 | [diff] [blame] | 5965 | if (ufshcd_try_to_abort_task(hba, tag)) { |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5966 | err_xfer = true; |
| 5967 | goto lock_skip_pending_xfer_clear; |
| 5968 | } |
| 5969 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5970 | |
| 5971 | /* Clear pending task management requests */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5972 | for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) { |
| 5973 | if (ufshcd_clear_tm_cmd(hba, tag)) { |
| 5974 | err_tm = true; |
| 5975 | goto lock_skip_pending_xfer_clear; |
| 5976 | } |
| 5977 | } |
| 5978 | |
| 5979 | lock_skip_pending_xfer_clear: |
| 5980 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5981 | |
| 5982 | /* Complete the requests that are cleared by s/w */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5983 | ufshcd_complete_requests(hba); |
Can Guo | 2355b66 | 2020-08-24 19:07:06 -0700 | [diff] [blame] | 5984 | hba->silence_err_logs = false; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5985 | |
Can Guo | 2355b66 | 2020-08-24 19:07:06 -0700 | [diff] [blame] | 5986 | if (err_xfer || err_tm) { |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5987 | needs_reset = true; |
Can Guo | 2355b66 | 2020-08-24 19:07:06 -0700 | [diff] [blame] | 5988 | goto do_reset; |
| 5989 | } |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5990 | |
Can Guo | 2355b66 | 2020-08-24 19:07:06 -0700 | [diff] [blame] | 5991 | /* |
| 5992 | * After all reqs and tasks are cleared from doorbell, |
| 5993 | * now it is safe to retore power mode. |
| 5994 | */ |
| 5995 | if (needs_restore) { |
| 5996 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5997 | /* |
| 5998 | * Hold the scaling lock just in case dev cmds |
| 5999 | * are sent via bsg and/or sysfs. |
| 6000 | */ |
| 6001 | down_write(&hba->clk_scaling_lock); |
| 6002 | hba->force_pmc = true; |
| 6003 | pmc_err = ufshcd_config_pwr_mode(hba, &(hba->pwr_info)); |
| 6004 | if (pmc_err) { |
| 6005 | needs_reset = true; |
| 6006 | dev_err(hba->dev, "%s: Failed to restore power mode, err = %d\n", |
| 6007 | __func__, pmc_err); |
| 6008 | } |
| 6009 | hba->force_pmc = false; |
| 6010 | ufshcd_print_pwr_info(hba); |
| 6011 | up_write(&hba->clk_scaling_lock); |
| 6012 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 6013 | } |
| 6014 | |
| 6015 | do_reset: |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6016 | /* Fatal errors need reset */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 6017 | if (needs_reset) { |
| 6018 | unsigned long max_doorbells = (1UL << hba->nutrs) - 1; |
| 6019 | |
| 6020 | /* |
| 6021 | * ufshcd_reset_and_restore() does the link reinitialization |
| 6022 | * which will need atleast one empty doorbell slot to send the |
| 6023 | * device management commands (NOP and query commands). |
| 6024 | * If there is no slot empty at this moment then free up last |
| 6025 | * slot forcefully. |
| 6026 | */ |
| 6027 | if (hba->outstanding_reqs == max_doorbells) |
| 6028 | __ufshcd_transfer_req_compl(hba, |
| 6029 | (1UL << (hba->nutrs - 1))); |
| 6030 | |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 6031 | hba->force_reset = false; |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 6032 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6033 | err = ufshcd_reset_and_restore(hba); |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 6034 | if (err) |
| 6035 | dev_err(hba->dev, "%s: reset and restore failed with err %d\n", |
| 6036 | __func__, err); |
Can Guo | c72e79c | 2020-08-09 05:15:52 -0700 | [diff] [blame] | 6037 | else |
| 6038 | ufshcd_recover_pm_error(hba); |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 6039 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6040 | } |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 6041 | |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 6042 | skip_err_handling: |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 6043 | if (!needs_reset) { |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 6044 | if (hba->ufshcd_state == UFSHCD_STATE_RESET) |
| 6045 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 6046 | if (hba->saved_err || hba->saved_uic_err) |
| 6047 | dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x", |
| 6048 | __func__, hba->saved_err, hba->saved_uic_err); |
| 6049 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6050 | ufshcd_clear_eh_in_progress(hba); |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 6051 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 6052 | ufshcd_scsi_unblock_requests(hba); |
Can Guo | c72e79c | 2020-08-09 05:15:52 -0700 | [diff] [blame] | 6053 | ufshcd_err_handling_unprepare(hba); |
Can Guo | 9cd20d3 | 2021-01-13 19:13:28 -0800 | [diff] [blame] | 6054 | up(&hba->host_sem); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6055 | } |
| 6056 | |
| 6057 | /** |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6058 | * ufshcd_update_uic_error - check and set fatal UIC error flags. |
| 6059 | * @hba: per-adapter instance |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6060 | * |
| 6061 | * Returns |
| 6062 | * IRQ_HANDLED - If interrupt is valid |
| 6063 | * IRQ_NONE - If invalid interrupt |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6064 | */ |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6065 | static irqreturn_t ufshcd_update_uic_error(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6066 | { |
| 6067 | u32 reg; |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6068 | irqreturn_t retval = IRQ_NONE; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6069 | |
Can Guo | 2355b66 | 2020-08-24 19:07:06 -0700 | [diff] [blame] | 6070 | /* PHY layer error */ |
Dolev Raviv | fb7b45f | 2016-11-23 16:32:32 -0800 | [diff] [blame] | 6071 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER); |
Dolev Raviv | fb7b45f | 2016-11-23 16:32:32 -0800 | [diff] [blame] | 6072 | if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) && |
Can Guo | 2355b66 | 2020-08-24 19:07:06 -0700 | [diff] [blame] | 6073 | (reg & UIC_PHY_ADAPTER_LAYER_ERROR_CODE_MASK)) { |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 6074 | ufshcd_update_evt_hist(hba, UFS_EVT_PA_ERR, reg); |
Dolev Raviv | fb7b45f | 2016-11-23 16:32:32 -0800 | [diff] [blame] | 6075 | /* |
| 6076 | * To know whether this error is fatal or not, DB timeout |
| 6077 | * must be checked but this error is handled separately. |
| 6078 | */ |
Can Guo | 2355b66 | 2020-08-24 19:07:06 -0700 | [diff] [blame] | 6079 | if (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK) |
| 6080 | dev_dbg(hba->dev, "%s: UIC Lane error reported\n", |
| 6081 | __func__); |
| 6082 | |
| 6083 | /* Got a LINERESET indication. */ |
| 6084 | if (reg & UIC_PHY_ADAPTER_LAYER_GENERIC_ERROR) { |
| 6085 | struct uic_command *cmd = NULL; |
| 6086 | |
| 6087 | hba->uic_error |= UFSHCD_UIC_PA_GENERIC_ERROR; |
| 6088 | if (hba->uic_async_done && hba->active_uic_cmd) |
| 6089 | cmd = hba->active_uic_cmd; |
| 6090 | /* |
| 6091 | * Ignore the LINERESET during power mode change |
| 6092 | * operation via DME_SET command. |
| 6093 | */ |
| 6094 | if (cmd && (cmd->command == UIC_CMD_DME_SET)) |
| 6095 | hba->uic_error &= ~UFSHCD_UIC_PA_GENERIC_ERROR; |
| 6096 | } |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6097 | retval |= IRQ_HANDLED; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6098 | } |
Dolev Raviv | fb7b45f | 2016-11-23 16:32:32 -0800 | [diff] [blame] | 6099 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6100 | /* PA_INIT_ERROR is fatal and needs UIC reset */ |
| 6101 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER); |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6102 | if ((reg & UIC_DATA_LINK_LAYER_ERROR) && |
| 6103 | (reg & UIC_DATA_LINK_LAYER_ERROR_CODE_MASK)) { |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 6104 | ufshcd_update_evt_hist(hba, UFS_EVT_DL_ERR, reg); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6105 | |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6106 | if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT) |
| 6107 | hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR; |
| 6108 | else if (hba->dev_quirks & |
| 6109 | UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) { |
| 6110 | if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED) |
| 6111 | hba->uic_error |= |
| 6112 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR; |
| 6113 | else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT) |
| 6114 | hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR; |
| 6115 | } |
| 6116 | retval |= IRQ_HANDLED; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 6117 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6118 | |
| 6119 | /* UIC NL/TL/DME errors needs software retry */ |
| 6120 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER); |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6121 | if ((reg & UIC_NETWORK_LAYER_ERROR) && |
| 6122 | (reg & UIC_NETWORK_LAYER_ERROR_CODE_MASK)) { |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 6123 | ufshcd_update_evt_hist(hba, UFS_EVT_NL_ERR, reg); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6124 | hba->uic_error |= UFSHCD_UIC_NL_ERROR; |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6125 | retval |= IRQ_HANDLED; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6126 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6127 | |
| 6128 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER); |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6129 | if ((reg & UIC_TRANSPORT_LAYER_ERROR) && |
| 6130 | (reg & UIC_TRANSPORT_LAYER_ERROR_CODE_MASK)) { |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 6131 | ufshcd_update_evt_hist(hba, UFS_EVT_TL_ERR, reg); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6132 | hba->uic_error |= UFSHCD_UIC_TL_ERROR; |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6133 | retval |= IRQ_HANDLED; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6134 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6135 | |
| 6136 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME); |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6137 | if ((reg & UIC_DME_ERROR) && |
| 6138 | (reg & UIC_DME_ERROR_CODE_MASK)) { |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 6139 | ufshcd_update_evt_hist(hba, UFS_EVT_DME_ERR, reg); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6140 | hba->uic_error |= UFSHCD_UIC_DME_ERROR; |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6141 | retval |= IRQ_HANDLED; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6142 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6143 | |
| 6144 | dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n", |
| 6145 | __func__, hba->uic_error); |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6146 | return retval; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6147 | } |
| 6148 | |
Stanley Chu | 8217444 | 2019-05-21 14:44:54 +0800 | [diff] [blame] | 6149 | static bool ufshcd_is_auto_hibern8_error(struct ufs_hba *hba, |
| 6150 | u32 intr_mask) |
| 6151 | { |
Stanley Chu | 5a244e0 | 2020-01-29 18:52:50 +0800 | [diff] [blame] | 6152 | if (!ufshcd_is_auto_hibern8_supported(hba) || |
| 6153 | !ufshcd_is_auto_hibern8_enabled(hba)) |
Stanley Chu | 8217444 | 2019-05-21 14:44:54 +0800 | [diff] [blame] | 6154 | return false; |
| 6155 | |
| 6156 | if (!(intr_mask & UFSHCD_UIC_HIBERN8_MASK)) |
| 6157 | return false; |
| 6158 | |
| 6159 | if (hba->active_uic_cmd && |
| 6160 | (hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_ENTER || |
| 6161 | hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_EXIT)) |
| 6162 | return false; |
| 6163 | |
| 6164 | return true; |
| 6165 | } |
| 6166 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6167 | /** |
| 6168 | * ufshcd_check_errors - Check for errors that need s/w attention |
| 6169 | * @hba: per-adapter instance |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6170 | * |
| 6171 | * Returns |
| 6172 | * IRQ_HANDLED - If interrupt is valid |
| 6173 | * IRQ_NONE - If invalid interrupt |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6174 | */ |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6175 | static irqreturn_t ufshcd_check_errors(struct ufs_hba *hba) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6176 | { |
| 6177 | bool queue_eh_work = false; |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6178 | irqreturn_t retval = IRQ_NONE; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6179 | |
Stanley Chu | d3c615b | 2019-07-10 21:38:19 +0800 | [diff] [blame] | 6180 | if (hba->errors & INT_FATAL_ERRORS) { |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 6181 | ufshcd_update_evt_hist(hba, UFS_EVT_FATAL_ERR, |
| 6182 | hba->errors); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6183 | queue_eh_work = true; |
Stanley Chu | d3c615b | 2019-07-10 21:38:19 +0800 | [diff] [blame] | 6184 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6185 | |
| 6186 | if (hba->errors & UIC_ERROR) { |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6187 | hba->uic_error = 0; |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6188 | retval = ufshcd_update_uic_error(hba); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6189 | if (hba->uic_error) |
| 6190 | queue_eh_work = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6191 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6192 | |
Stanley Chu | 8217444 | 2019-05-21 14:44:54 +0800 | [diff] [blame] | 6193 | if (hba->errors & UFSHCD_UIC_HIBERN8_MASK) { |
| 6194 | dev_err(hba->dev, |
| 6195 | "%s: Auto Hibern8 %s failed - status: 0x%08x, upmcrs: 0x%08x\n", |
| 6196 | __func__, (hba->errors & UIC_HIBERNATE_ENTER) ? |
| 6197 | "Enter" : "Exit", |
| 6198 | hba->errors, ufshcd_get_upmcrs(hba)); |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 6199 | ufshcd_update_evt_hist(hba, UFS_EVT_AUTO_HIBERN8_ERR, |
Stanley Chu | d3c615b | 2019-07-10 21:38:19 +0800 | [diff] [blame] | 6200 | hba->errors); |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 6201 | ufshcd_set_link_broken(hba); |
Stanley Chu | 8217444 | 2019-05-21 14:44:54 +0800 | [diff] [blame] | 6202 | queue_eh_work = true; |
| 6203 | } |
| 6204 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6205 | if (queue_eh_work) { |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 6206 | /* |
| 6207 | * update the transfer error masks to sticky bits, let's do this |
| 6208 | * irrespective of current ufshcd_state. |
| 6209 | */ |
| 6210 | hba->saved_err |= hba->errors; |
| 6211 | hba->saved_uic_err |= hba->uic_error; |
| 6212 | |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 6213 | /* dump controller state before resetting */ |
Can Guo | ace3804 | 2020-12-02 04:04:03 -0800 | [diff] [blame] | 6214 | if ((hba->saved_err & |
| 6215 | (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK)) || |
Can Guo | 2355b66 | 2020-08-24 19:07:06 -0700 | [diff] [blame] | 6216 | (hba->saved_uic_err && |
| 6217 | (hba->saved_uic_err != UFSHCD_UIC_PA_GENERIC_ERROR))) { |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 6218 | dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x\n", |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 6219 | __func__, hba->saved_err, |
| 6220 | hba->saved_uic_err); |
Can Guo | c3be8d1e | 2020-08-09 05:15:53 -0700 | [diff] [blame] | 6221 | ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, |
| 6222 | "host_regs: "); |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 6223 | ufshcd_print_pwr_info(hba); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6224 | } |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 6225 | ufshcd_schedule_eh_work(hba); |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6226 | retval |= IRQ_HANDLED; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6227 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6228 | /* |
| 6229 | * if (!queue_eh_work) - |
| 6230 | * Other errors are either non-fatal where host recovers |
| 6231 | * itself without s/w intervention or errors that will be |
| 6232 | * handled by the SCSI core layer. |
| 6233 | */ |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6234 | return retval; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6235 | } |
| 6236 | |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 6237 | struct ctm_info { |
| 6238 | struct ufs_hba *hba; |
| 6239 | unsigned long pending; |
| 6240 | unsigned int ncpl; |
| 6241 | }; |
| 6242 | |
| 6243 | static bool ufshcd_compl_tm(struct request *req, void *priv, bool reserved) |
| 6244 | { |
| 6245 | struct ctm_info *const ci = priv; |
| 6246 | struct completion *c; |
| 6247 | |
| 6248 | WARN_ON_ONCE(reserved); |
| 6249 | if (test_bit(req->tag, &ci->pending)) |
| 6250 | return true; |
| 6251 | ci->ncpl++; |
| 6252 | c = req->end_io_data; |
| 6253 | if (c) |
| 6254 | complete(c); |
| 6255 | return true; |
| 6256 | } |
| 6257 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6258 | /** |
| 6259 | * ufshcd_tmc_handler - handle task management function completion |
| 6260 | * @hba: per adapter instance |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6261 | * |
| 6262 | * Returns |
| 6263 | * IRQ_HANDLED - If interrupt is valid |
| 6264 | * IRQ_NONE - If invalid interrupt |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6265 | */ |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6266 | static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6267 | { |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 6268 | struct request_queue *q = hba->tmf_queue; |
| 6269 | struct ctm_info ci = { |
| 6270 | .hba = hba, |
| 6271 | .pending = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL), |
| 6272 | }; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6273 | |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 6274 | blk_mq_tagset_busy_iter(q->tag_set, ufshcd_compl_tm, &ci); |
| 6275 | return ci.ncpl ? IRQ_HANDLED : IRQ_NONE; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6276 | } |
| 6277 | |
| 6278 | /** |
| 6279 | * ufshcd_sl_intr - Interrupt service routine |
| 6280 | * @hba: per adapter instance |
| 6281 | * @intr_status: contains interrupts generated by the controller |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6282 | * |
| 6283 | * Returns |
| 6284 | * IRQ_HANDLED - If interrupt is valid |
| 6285 | * IRQ_NONE - If invalid interrupt |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6286 | */ |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6287 | 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] | 6288 | { |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6289 | irqreturn_t retval = IRQ_NONE; |
| 6290 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6291 | hba->errors = UFSHCD_ERROR_MASK & intr_status; |
Stanley Chu | 8217444 | 2019-05-21 14:44:54 +0800 | [diff] [blame] | 6292 | |
| 6293 | if (ufshcd_is_auto_hibern8_error(hba, intr_status)) |
| 6294 | hba->errors |= (UFSHCD_UIC_HIBERN8_MASK & intr_status); |
| 6295 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6296 | if (hba->errors) |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6297 | retval |= ufshcd_check_errors(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6298 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 6299 | if (intr_status & UFSHCD_UIC_MASK) |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6300 | retval |= ufshcd_uic_cmd_compl(hba, intr_status); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6301 | |
| 6302 | if (intr_status & UTP_TASK_REQ_COMPL) |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6303 | retval |= ufshcd_tmc_handler(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6304 | |
| 6305 | if (intr_status & UTP_TRANSFER_REQ_COMPL) |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6306 | retval |= ufshcd_transfer_req_compl(hba); |
| 6307 | |
| 6308 | return retval; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6309 | } |
| 6310 | |
| 6311 | /** |
| 6312 | * ufshcd_intr - Main interrupt service routine |
| 6313 | * @irq: irq number |
| 6314 | * @__hba: pointer to adapter instance |
| 6315 | * |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6316 | * Returns |
| 6317 | * IRQ_HANDLED - If interrupt is valid |
| 6318 | * IRQ_NONE - If invalid interrupt |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6319 | */ |
| 6320 | static irqreturn_t ufshcd_intr(int irq, void *__hba) |
| 6321 | { |
Adrian Hunter | 127d5f7 | 2020-08-11 16:39:36 +0300 | [diff] [blame] | 6322 | u32 intr_status, enabled_intr_status = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6323 | irqreturn_t retval = IRQ_NONE; |
| 6324 | struct ufs_hba *hba = __hba; |
Venkat Gopalakrishnan | 7f6ba4f | 2018-05-03 16:37:20 +0530 | [diff] [blame] | 6325 | int retries = hba->nutrs; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6326 | |
| 6327 | spin_lock(hba->host->host_lock); |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 6328 | intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS); |
Can Guo | 3f8af60 | 2020-08-09 05:15:50 -0700 | [diff] [blame] | 6329 | hba->ufs_stats.last_intr_status = intr_status; |
| 6330 | hba->ufs_stats.last_intr_ts = ktime_get(); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6331 | |
Venkat Gopalakrishnan | 7f6ba4f | 2018-05-03 16:37:20 +0530 | [diff] [blame] | 6332 | /* |
| 6333 | * There could be max of hba->nutrs reqs in flight and in worst case |
| 6334 | * if the reqs get finished 1 by 1 after the interrupt status is |
| 6335 | * read, make sure we handle them by checking the interrupt status |
| 6336 | * again in a loop until we process all of the reqs before returning. |
| 6337 | */ |
Adrian Hunter | 127d5f7 | 2020-08-11 16:39:36 +0300 | [diff] [blame] | 6338 | while (intr_status && retries--) { |
Venkat Gopalakrishnan | 7f6ba4f | 2018-05-03 16:37:20 +0530 | [diff] [blame] | 6339 | enabled_intr_status = |
| 6340 | intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
Bean Huo | 60ec375 | 2021-01-18 21:12:33 +0100 | [diff] [blame] | 6341 | ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS); |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6342 | if (enabled_intr_status) |
| 6343 | retval |= ufshcd_sl_intr(hba, enabled_intr_status); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 6344 | |
Venkat Gopalakrishnan | 7f6ba4f | 2018-05-03 16:37:20 +0530 | [diff] [blame] | 6345 | intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS); |
Adrian Hunter | 127d5f7 | 2020-08-11 16:39:36 +0300 | [diff] [blame] | 6346 | } |
Venkat Gopalakrishnan | 7f6ba4f | 2018-05-03 16:37:20 +0530 | [diff] [blame] | 6347 | |
Adrian Hunter | 6337f58 | 2020-08-11 16:39:35 +0300 | [diff] [blame] | 6348 | if (enabled_intr_status && retval == IRQ_NONE) { |
Venkat Gopalakrishnan | 9333d77 | 2019-11-14 22:09:28 -0800 | [diff] [blame] | 6349 | dev_err(hba->dev, "%s: Unhandled interrupt 0x%08x\n", |
| 6350 | __func__, intr_status); |
| 6351 | ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: "); |
| 6352 | } |
| 6353 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6354 | spin_unlock(hba->host->host_lock); |
| 6355 | return retval; |
| 6356 | } |
| 6357 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6358 | static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag) |
| 6359 | { |
| 6360 | int err = 0; |
| 6361 | u32 mask = 1 << tag; |
| 6362 | unsigned long flags; |
| 6363 | |
| 6364 | if (!test_bit(tag, &hba->outstanding_tasks)) |
| 6365 | goto out; |
| 6366 | |
| 6367 | spin_lock_irqsave(hba->host->host_lock, flags); |
Alim Akhtar | 1399c5b | 2018-05-06 15:44:15 +0530 | [diff] [blame] | 6368 | ufshcd_utmrl_clear(hba, tag); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6369 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6370 | |
| 6371 | /* poll for max. 1 sec to clear door bell register by h/w */ |
| 6372 | err = ufshcd_wait_for_register(hba, |
| 6373 | REG_UTP_TASK_REQ_DOOR_BELL, |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 6374 | mask, 0, 1000, 1000); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6375 | out: |
| 6376 | return err; |
| 6377 | } |
| 6378 | |
Christoph Hellwig | c6049cd | 2018-10-07 17:30:33 +0300 | [diff] [blame] | 6379 | static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba, |
| 6380 | struct utp_task_req_desc *treq, u8 tm_function) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6381 | { |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 6382 | struct request_queue *q = hba->tmf_queue; |
Christoph Hellwig | c6049cd | 2018-10-07 17:30:33 +0300 | [diff] [blame] | 6383 | struct Scsi_Host *host = hba->host; |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 6384 | DECLARE_COMPLETION_ONSTACK(wait); |
| 6385 | struct request *req; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6386 | unsigned long flags; |
Christoph Hellwig | c6049cd | 2018-10-07 17:30:33 +0300 | [diff] [blame] | 6387 | int free_slot, task_tag, err; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6388 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6389 | /* |
| 6390 | * Get free slot, sleep if slots are unavailable. |
| 6391 | * Even though we use wait_event() which sleeps indefinitely, |
| 6392 | * the maximum wait time is bounded by %TM_CMD_TIMEOUT. |
| 6393 | */ |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 6394 | req = blk_get_request(q, REQ_OP_DRV_OUT, BLK_MQ_REQ_RESERVED); |
| 6395 | req->end_io_data = &wait; |
| 6396 | free_slot = req->tag; |
| 6397 | WARN_ON_ONCE(free_slot < 0 || free_slot >= hba->nutmrs); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6398 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6399 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6400 | spin_lock_irqsave(host->host_lock, flags); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6401 | task_tag = hba->nutrs + free_slot; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6402 | |
Christoph Hellwig | c6049cd | 2018-10-07 17:30:33 +0300 | [diff] [blame] | 6403 | treq->req_header.dword_0 |= cpu_to_be32(task_tag); |
| 6404 | |
| 6405 | memcpy(hba->utmrdl_base_addr + free_slot, treq, sizeof(*treq)); |
Kiwoong Kim | d2877be | 2016-11-10 21:16:15 +0900 | [diff] [blame] | 6406 | ufshcd_vops_setup_task_mgmt(hba, free_slot, tm_function); |
| 6407 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6408 | /* send command to the controller */ |
| 6409 | __set_bit(free_slot, &hba->outstanding_tasks); |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 6410 | |
| 6411 | /* Make sure descriptors are ready before ringing the task doorbell */ |
| 6412 | wmb(); |
| 6413 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 6414 | ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL); |
Gilad Broner | ad1a1b9 | 2016-10-17 17:09:36 -0700 | [diff] [blame] | 6415 | /* Make sure that doorbell is committed immediately */ |
| 6416 | wmb(); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6417 | |
| 6418 | spin_unlock_irqrestore(host->host_lock, flags); |
| 6419 | |
Bean Huo | 28fa68f | 2021-01-05 12:34:42 +0100 | [diff] [blame] | 6420 | ufshcd_add_tm_upiu_trace(hba, task_tag, UFS_TM_SEND); |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 6421 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6422 | /* wait until the task management command is completed */ |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 6423 | err = wait_for_completion_io_timeout(&wait, |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6424 | msecs_to_jiffies(TM_CMD_TIMEOUT)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6425 | if (!err) { |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 6426 | /* |
| 6427 | * Make sure that ufshcd_compl_tm() does not trigger a |
| 6428 | * use-after-free. |
| 6429 | */ |
| 6430 | req->end_io_data = NULL; |
Bean Huo | 28fa68f | 2021-01-05 12:34:42 +0100 | [diff] [blame] | 6431 | ufshcd_add_tm_upiu_trace(hba, task_tag, UFS_TM_ERR); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6432 | dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n", |
| 6433 | __func__, tm_function); |
| 6434 | if (ufshcd_clear_tm_cmd(hba, free_slot)) |
| 6435 | dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n", |
| 6436 | __func__, free_slot); |
| 6437 | err = -ETIMEDOUT; |
| 6438 | } else { |
Christoph Hellwig | c6049cd | 2018-10-07 17:30:33 +0300 | [diff] [blame] | 6439 | err = 0; |
| 6440 | memcpy(treq, hba->utmrdl_base_addr + free_slot, sizeof(*treq)); |
| 6441 | |
Bean Huo | 28fa68f | 2021-01-05 12:34:42 +0100 | [diff] [blame] | 6442 | ufshcd_add_tm_upiu_trace(hba, task_tag, UFS_TM_COMP); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6443 | } |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6444 | |
Stanley Chu | b557217 | 2019-08-19 21:43:28 +0800 | [diff] [blame] | 6445 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 6446 | __clear_bit(free_slot, &hba->outstanding_tasks); |
| 6447 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6448 | |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 6449 | blk_put_request(req); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6450 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6451 | ufshcd_release(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6452 | return err; |
| 6453 | } |
| 6454 | |
| 6455 | /** |
Christoph Hellwig | c6049cd | 2018-10-07 17:30:33 +0300 | [diff] [blame] | 6456 | * ufshcd_issue_tm_cmd - issues task management commands to controller |
| 6457 | * @hba: per adapter instance |
| 6458 | * @lun_id: LUN ID to which TM command is sent |
| 6459 | * @task_id: task ID to which the TM command is applicable |
| 6460 | * @tm_function: task management function opcode |
| 6461 | * @tm_response: task management service response return value |
| 6462 | * |
| 6463 | * Returns non-zero value on error, zero on success. |
| 6464 | */ |
| 6465 | static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id, |
| 6466 | u8 tm_function, u8 *tm_response) |
| 6467 | { |
| 6468 | struct utp_task_req_desc treq = { { 0 }, }; |
| 6469 | int ocs_value, err; |
| 6470 | |
| 6471 | /* Configure task request descriptor */ |
| 6472 | treq.header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD); |
| 6473 | treq.header.dword_2 = cpu_to_le32(OCS_INVALID_COMMAND_STATUS); |
| 6474 | |
| 6475 | /* Configure task request UPIU */ |
| 6476 | treq.req_header.dword_0 = cpu_to_be32(lun_id << 8) | |
| 6477 | cpu_to_be32(UPIU_TRANSACTION_TASK_REQ << 24); |
| 6478 | treq.req_header.dword_1 = cpu_to_be32(tm_function << 16); |
| 6479 | |
| 6480 | /* |
| 6481 | * The host shall provide the same value for LUN field in the basic |
| 6482 | * header and for Input Parameter. |
| 6483 | */ |
| 6484 | treq.input_param1 = cpu_to_be32(lun_id); |
| 6485 | treq.input_param2 = cpu_to_be32(task_id); |
| 6486 | |
| 6487 | err = __ufshcd_issue_tm_cmd(hba, &treq, tm_function); |
| 6488 | if (err == -ETIMEDOUT) |
| 6489 | return err; |
| 6490 | |
| 6491 | ocs_value = le32_to_cpu(treq.header.dword_2) & MASK_OCS; |
| 6492 | if (ocs_value != OCS_SUCCESS) |
| 6493 | dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", |
| 6494 | __func__, ocs_value); |
| 6495 | else if (tm_response) |
| 6496 | *tm_response = be32_to_cpu(treq.output_param1) & |
| 6497 | MASK_TM_SERVICE_RESP; |
| 6498 | return err; |
| 6499 | } |
| 6500 | |
| 6501 | /** |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6502 | * ufshcd_issue_devman_upiu_cmd - API for sending "utrd" type requests |
| 6503 | * @hba: per-adapter instance |
| 6504 | * @req_upiu: upiu request |
| 6505 | * @rsp_upiu: upiu reply |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6506 | * @desc_buff: pointer to descriptor buffer, NULL if NA |
| 6507 | * @buff_len: descriptor size, 0 if NA |
Bart Van Assche | d0e9760 | 2019-10-29 16:07:08 -0700 | [diff] [blame] | 6508 | * @cmd_type: specifies the type (NOP, Query...) |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6509 | * @desc_op: descriptor operation |
| 6510 | * |
| 6511 | * Those type of requests uses UTP Transfer Request Descriptor - utrd. |
| 6512 | * Therefore, it "rides" the device management infrastructure: uses its tag and |
| 6513 | * tasks work queues. |
| 6514 | * |
| 6515 | * Since there is only one available tag for device management commands, |
| 6516 | * the caller is expected to hold the hba->dev_cmd.lock mutex. |
| 6517 | */ |
| 6518 | static int ufshcd_issue_devman_upiu_cmd(struct ufs_hba *hba, |
| 6519 | struct utp_upiu_req *req_upiu, |
| 6520 | struct utp_upiu_req *rsp_upiu, |
| 6521 | u8 *desc_buff, int *buff_len, |
Bart Van Assche | 7f674c3 | 2019-10-29 16:07:09 -0700 | [diff] [blame] | 6522 | enum dev_cmd_type cmd_type, |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6523 | enum query_opcode desc_op) |
| 6524 | { |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 6525 | struct request_queue *q = hba->cmd_queue; |
| 6526 | struct request *req; |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6527 | struct ufshcd_lrb *lrbp; |
| 6528 | int err = 0; |
| 6529 | int tag; |
| 6530 | struct completion wait; |
| 6531 | unsigned long flags; |
Bean Huo | a23064c | 2020-07-06 14:39:36 +0200 | [diff] [blame] | 6532 | u8 upiu_flags; |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6533 | |
| 6534 | down_read(&hba->clk_scaling_lock); |
| 6535 | |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 6536 | req = blk_get_request(q, REQ_OP_DRV_OUT, 0); |
Dan Carpenter | bb14dd1 | 2019-12-13 13:48:28 +0300 | [diff] [blame] | 6537 | if (IS_ERR(req)) { |
| 6538 | err = PTR_ERR(req); |
| 6539 | goto out_unlock; |
| 6540 | } |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 6541 | tag = req->tag; |
| 6542 | WARN_ON_ONCE(!ufshcd_valid_tag(hba, tag)); |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6543 | |
| 6544 | init_completion(&wait); |
| 6545 | lrbp = &hba->lrb[tag]; |
Can Guo | 7a7e66c | 2020-12-02 04:04:02 -0800 | [diff] [blame] | 6546 | if (unlikely(lrbp->in_use)) { |
| 6547 | err = -EBUSY; |
| 6548 | goto out; |
| 6549 | } |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6550 | |
Can Guo | 7a7e66c | 2020-12-02 04:04:02 -0800 | [diff] [blame] | 6551 | WARN_ON(lrbp->cmd); |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6552 | lrbp->cmd = NULL; |
| 6553 | lrbp->sense_bufflen = 0; |
| 6554 | lrbp->sense_buffer = NULL; |
| 6555 | lrbp->task_tag = tag; |
| 6556 | lrbp->lun = 0; |
| 6557 | lrbp->intr_cmd = true; |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 6558 | ufshcd_prepare_lrbp_crypto(NULL, lrbp); |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6559 | hba->dev_cmd.type = cmd_type; |
| 6560 | |
| 6561 | switch (hba->ufs_version) { |
| 6562 | case UFSHCI_VERSION_10: |
| 6563 | case UFSHCI_VERSION_11: |
| 6564 | lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE; |
| 6565 | break; |
| 6566 | default: |
| 6567 | lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE; |
| 6568 | break; |
| 6569 | } |
| 6570 | |
| 6571 | /* update the task tag in the request upiu */ |
| 6572 | req_upiu->header.dword_0 |= cpu_to_be32(tag); |
| 6573 | |
| 6574 | ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE); |
| 6575 | |
| 6576 | /* just copy the upiu request as it is */ |
| 6577 | memcpy(lrbp->ucd_req_ptr, req_upiu, sizeof(*lrbp->ucd_req_ptr)); |
| 6578 | if (desc_buff && desc_op == UPIU_QUERY_OPCODE_WRITE_DESC) { |
| 6579 | /* The Data Segment Area is optional depending upon the query |
| 6580 | * function value. for WRITE DESCRIPTOR, the data segment |
| 6581 | * follows right after the tsf. |
| 6582 | */ |
| 6583 | memcpy(lrbp->ucd_req_ptr + 1, desc_buff, *buff_len); |
| 6584 | *buff_len = 0; |
| 6585 | } |
| 6586 | |
| 6587 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
| 6588 | |
| 6589 | hba->dev_cmd.complete = &wait; |
| 6590 | |
| 6591 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 6592 | wmb(); |
| 6593 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 6594 | ufshcd_send_command(hba, tag); |
| 6595 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6596 | |
| 6597 | /* |
| 6598 | * ignore the returning value here - ufshcd_check_query_response is |
| 6599 | * bound to fail since dev_cmd.query and dev_cmd.type were left empty. |
| 6600 | * read the response directly ignoring all errors. |
| 6601 | */ |
| 6602 | ufshcd_wait_for_dev_cmd(hba, lrbp, QUERY_REQ_TIMEOUT); |
| 6603 | |
| 6604 | /* just copy the upiu response as it is */ |
| 6605 | memcpy(rsp_upiu, lrbp->ucd_rsp_ptr, sizeof(*rsp_upiu)); |
Avri Altman | 4bbbe24 | 2019-02-20 09:11:13 +0200 | [diff] [blame] | 6606 | if (desc_buff && desc_op == UPIU_QUERY_OPCODE_READ_DESC) { |
| 6607 | u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + sizeof(*rsp_upiu); |
| 6608 | u16 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) & |
| 6609 | MASK_QUERY_DATA_SEG_LEN; |
| 6610 | |
| 6611 | if (*buff_len >= resp_len) { |
| 6612 | memcpy(desc_buff, descp, resp_len); |
| 6613 | *buff_len = resp_len; |
| 6614 | } else { |
Bean Huo | 3d4881d | 2019-11-12 23:34:35 +0100 | [diff] [blame] | 6615 | dev_warn(hba->dev, |
| 6616 | "%s: rsp size %d is bigger than buffer size %d", |
| 6617 | __func__, resp_len, *buff_len); |
Avri Altman | 4bbbe24 | 2019-02-20 09:11:13 +0200 | [diff] [blame] | 6618 | *buff_len = 0; |
| 6619 | err = -EINVAL; |
| 6620 | } |
| 6621 | } |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6622 | |
Can Guo | 7a7e66c | 2020-12-02 04:04:02 -0800 | [diff] [blame] | 6623 | out: |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 6624 | blk_put_request(req); |
Dan Carpenter | bb14dd1 | 2019-12-13 13:48:28 +0300 | [diff] [blame] | 6625 | out_unlock: |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6626 | up_read(&hba->clk_scaling_lock); |
| 6627 | return err; |
| 6628 | } |
| 6629 | |
| 6630 | /** |
| 6631 | * ufshcd_exec_raw_upiu_cmd - API function for sending raw upiu commands |
| 6632 | * @hba: per-adapter instance |
| 6633 | * @req_upiu: upiu request |
| 6634 | * @rsp_upiu: upiu reply - only 8 DW as we do not support scsi commands |
| 6635 | * @msgcode: message code, one of UPIU Transaction Codes Initiator to Target |
| 6636 | * @desc_buff: pointer to descriptor buffer, NULL if NA |
| 6637 | * @buff_len: descriptor size, 0 if NA |
| 6638 | * @desc_op: descriptor operation |
| 6639 | * |
| 6640 | * Supports UTP Transfer requests (nop and query), and UTP Task |
| 6641 | * Management requests. |
| 6642 | * It is up to the caller to fill the upiu conent properly, as it will |
| 6643 | * be copied without any further input validations. |
| 6644 | */ |
| 6645 | int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba, |
| 6646 | struct utp_upiu_req *req_upiu, |
| 6647 | struct utp_upiu_req *rsp_upiu, |
| 6648 | int msgcode, |
| 6649 | u8 *desc_buff, int *buff_len, |
| 6650 | enum query_opcode desc_op) |
| 6651 | { |
| 6652 | int err; |
Bart Van Assche | 7f674c3 | 2019-10-29 16:07:09 -0700 | [diff] [blame] | 6653 | enum dev_cmd_type cmd_type = DEV_CMD_TYPE_QUERY; |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6654 | struct utp_task_req_desc treq = { { 0 }, }; |
| 6655 | int ocs_value; |
| 6656 | u8 tm_f = be32_to_cpu(req_upiu->header.dword_1) >> 16 & MASK_TM_FUNC; |
| 6657 | |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6658 | switch (msgcode) { |
| 6659 | case UPIU_TRANSACTION_NOP_OUT: |
| 6660 | cmd_type = DEV_CMD_TYPE_NOP; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 6661 | fallthrough; |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6662 | case UPIU_TRANSACTION_QUERY_REQ: |
| 6663 | ufshcd_hold(hba, false); |
| 6664 | mutex_lock(&hba->dev_cmd.lock); |
| 6665 | err = ufshcd_issue_devman_upiu_cmd(hba, req_upiu, rsp_upiu, |
| 6666 | desc_buff, buff_len, |
| 6667 | cmd_type, desc_op); |
| 6668 | mutex_unlock(&hba->dev_cmd.lock); |
| 6669 | ufshcd_release(hba); |
| 6670 | |
| 6671 | break; |
| 6672 | case UPIU_TRANSACTION_TASK_REQ: |
| 6673 | treq.header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD); |
| 6674 | treq.header.dword_2 = cpu_to_le32(OCS_INVALID_COMMAND_STATUS); |
| 6675 | |
| 6676 | memcpy(&treq.req_header, req_upiu, sizeof(*req_upiu)); |
| 6677 | |
| 6678 | err = __ufshcd_issue_tm_cmd(hba, &treq, tm_f); |
| 6679 | if (err == -ETIMEDOUT) |
| 6680 | break; |
| 6681 | |
| 6682 | ocs_value = le32_to_cpu(treq.header.dword_2) & MASK_OCS; |
| 6683 | if (ocs_value != OCS_SUCCESS) { |
| 6684 | dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", __func__, |
| 6685 | ocs_value); |
| 6686 | break; |
| 6687 | } |
| 6688 | |
| 6689 | memcpy(rsp_upiu, &treq.rsp_header, sizeof(*rsp_upiu)); |
| 6690 | |
| 6691 | break; |
| 6692 | default: |
| 6693 | err = -EINVAL; |
| 6694 | |
| 6695 | break; |
| 6696 | } |
| 6697 | |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 6698 | return err; |
| 6699 | } |
| 6700 | |
| 6701 | /** |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6702 | * ufshcd_eh_device_reset_handler - device reset handler registered to |
| 6703 | * scsi layer. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6704 | * @cmd: SCSI command pointer |
| 6705 | * |
| 6706 | * Returns SUCCESS/FAILED |
| 6707 | */ |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6708 | static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6709 | { |
| 6710 | struct Scsi_Host *host; |
| 6711 | struct ufs_hba *hba; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6712 | u32 pos; |
| 6713 | int err; |
Can Guo | 35fc4cd | 2020-12-28 04:04:36 -0800 | [diff] [blame] | 6714 | u8 resp = 0xF, lun; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6715 | unsigned long flags; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6716 | |
| 6717 | host = cmd->device->host; |
| 6718 | hba = shost_priv(host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6719 | |
Can Guo | 35fc4cd | 2020-12-28 04:04:36 -0800 | [diff] [blame] | 6720 | lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun); |
| 6721 | err = ufshcd_issue_tm_cmd(hba, lun, 0, UFS_LOGICAL_RESET, &resp); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6722 | if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6723 | if (!err) |
| 6724 | err = resp; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6725 | goto out; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6726 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6727 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6728 | /* clear the commands that were pending for corresponding LUN */ |
| 6729 | for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) { |
Can Guo | 35fc4cd | 2020-12-28 04:04:36 -0800 | [diff] [blame] | 6730 | if (hba->lrb[pos].lun == lun) { |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6731 | err = ufshcd_clear_cmd(hba, pos); |
| 6732 | if (err) |
| 6733 | break; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6734 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6735 | } |
| 6736 | spin_lock_irqsave(host->host_lock, flags); |
| 6737 | ufshcd_transfer_req_compl(hba); |
| 6738 | spin_unlock_irqrestore(host->host_lock, flags); |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 6739 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6740 | out: |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 6741 | hba->req_abort_count = 0; |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 6742 | ufshcd_update_evt_hist(hba, UFS_EVT_DEV_RESET, (u32)err); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6743 | if (!err) { |
| 6744 | err = SUCCESS; |
| 6745 | } else { |
| 6746 | dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); |
| 6747 | err = FAILED; |
| 6748 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6749 | return err; |
| 6750 | } |
| 6751 | |
Gilad Broner | e0b299e | 2017-02-03 16:56:40 -0800 | [diff] [blame] | 6752 | static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap) |
| 6753 | { |
| 6754 | struct ufshcd_lrb *lrbp; |
| 6755 | int tag; |
| 6756 | |
| 6757 | for_each_set_bit(tag, &bitmap, hba->nutrs) { |
| 6758 | lrbp = &hba->lrb[tag]; |
| 6759 | lrbp->req_abort_skip = true; |
| 6760 | } |
| 6761 | } |
| 6762 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6763 | /** |
Can Guo | 307348f | 2020-08-24 19:07:05 -0700 | [diff] [blame] | 6764 | * ufshcd_try_to_abort_task - abort a specific task |
Lee Jones | d23ec0b | 2020-11-02 14:23:51 +0000 | [diff] [blame] | 6765 | * @hba: Pointer to adapter instance |
| 6766 | * @tag: Task tag/index to be aborted |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6767 | * |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6768 | * Abort the pending command in device by sending UFS_ABORT_TASK task management |
| 6769 | * command, and in host controller by clearing the door-bell register. There can |
| 6770 | * be race between controller sending the command to the device while abort is |
| 6771 | * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is |
| 6772 | * really issued and then try to abort it. |
| 6773 | * |
Can Guo | 307348f | 2020-08-24 19:07:05 -0700 | [diff] [blame] | 6774 | * Returns zero on success, non-zero on failure |
| 6775 | */ |
| 6776 | static int ufshcd_try_to_abort_task(struct ufs_hba *hba, int tag) |
| 6777 | { |
| 6778 | struct ufshcd_lrb *lrbp = &hba->lrb[tag]; |
| 6779 | int err = 0; |
| 6780 | int poll_cnt; |
| 6781 | u8 resp = 0xF; |
| 6782 | u32 reg; |
| 6783 | |
| 6784 | for (poll_cnt = 100; poll_cnt; poll_cnt--) { |
| 6785 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, |
| 6786 | UFS_QUERY_TASK, &resp); |
| 6787 | if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) { |
| 6788 | /* cmd pending in the device */ |
| 6789 | dev_err(hba->dev, "%s: cmd pending in the device. tag = %d\n", |
| 6790 | __func__, tag); |
| 6791 | break; |
| 6792 | } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
| 6793 | /* |
| 6794 | * cmd not pending in the device, check if it is |
| 6795 | * in transition. |
| 6796 | */ |
| 6797 | dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.\n", |
| 6798 | __func__, tag); |
| 6799 | reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 6800 | if (reg & (1 << tag)) { |
| 6801 | /* sleep for max. 200us to stabilize */ |
| 6802 | usleep_range(100, 200); |
| 6803 | continue; |
| 6804 | } |
| 6805 | /* command completed already */ |
| 6806 | dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.\n", |
| 6807 | __func__, tag); |
| 6808 | goto out; |
| 6809 | } else { |
| 6810 | dev_err(hba->dev, |
| 6811 | "%s: no response from device. tag = %d, err %d\n", |
| 6812 | __func__, tag, err); |
| 6813 | if (!err) |
| 6814 | err = resp; /* service response error */ |
| 6815 | goto out; |
| 6816 | } |
| 6817 | } |
| 6818 | |
| 6819 | if (!poll_cnt) { |
| 6820 | err = -EBUSY; |
| 6821 | goto out; |
| 6822 | } |
| 6823 | |
| 6824 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, |
| 6825 | UFS_ABORT_TASK, &resp); |
| 6826 | if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
| 6827 | if (!err) { |
| 6828 | err = resp; /* service response error */ |
| 6829 | dev_err(hba->dev, "%s: issued. tag = %d, err %d\n", |
| 6830 | __func__, tag, err); |
| 6831 | } |
| 6832 | goto out; |
| 6833 | } |
| 6834 | |
| 6835 | err = ufshcd_clear_cmd(hba, tag); |
| 6836 | if (err) |
| 6837 | dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d\n", |
| 6838 | __func__, tag, err); |
| 6839 | |
| 6840 | out: |
| 6841 | return err; |
| 6842 | } |
| 6843 | |
| 6844 | /** |
| 6845 | * ufshcd_abort - scsi host template eh_abort_handler callback |
| 6846 | * @cmd: SCSI command pointer |
| 6847 | * |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6848 | * Returns SUCCESS/FAILED |
| 6849 | */ |
| 6850 | static int ufshcd_abort(struct scsi_cmnd *cmd) |
| 6851 | { |
| 6852 | struct Scsi_Host *host; |
| 6853 | struct ufs_hba *hba; |
| 6854 | unsigned long flags; |
| 6855 | unsigned int tag; |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6856 | int err = 0; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6857 | struct ufshcd_lrb *lrbp; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 6858 | u32 reg; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6859 | |
| 6860 | host = cmd->device->host; |
| 6861 | hba = shost_priv(host); |
| 6862 | tag = cmd->request->tag; |
Dolev Raviv | e7d3825 | 2016-12-22 18:40:07 -0800 | [diff] [blame] | 6863 | lrbp = &hba->lrb[tag]; |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 6864 | if (!ufshcd_valid_tag(hba, tag)) { |
| 6865 | dev_err(hba->dev, |
| 6866 | "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p", |
| 6867 | __func__, tag, cmd, cmd->request); |
| 6868 | BUG(); |
| 6869 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6870 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6871 | ufshcd_hold(hba, false); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 6872 | reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 6873 | /* If command is already aborted/completed, return SUCCESS */ |
| 6874 | if (!(test_bit(tag, &hba->outstanding_reqs))) { |
| 6875 | dev_err(hba->dev, |
| 6876 | "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n", |
| 6877 | __func__, tag, hba->outstanding_reqs, reg); |
| 6878 | goto out; |
| 6879 | } |
| 6880 | |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 6881 | /* Print Transfer Request of aborted task */ |
Bean Huo | d87a1f6 | 2020-08-11 16:18:59 +0200 | [diff] [blame] | 6882 | dev_info(hba->dev, "%s: Device abort task at tag %d\n", __func__, tag); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 6883 | |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 6884 | /* |
| 6885 | * Print detailed info about aborted request. |
| 6886 | * As more than one request might get aborted at the same time, |
| 6887 | * print full information only for the first aborted request in order |
| 6888 | * to reduce repeated printouts. For other aborted requests only print |
| 6889 | * basic details. |
| 6890 | */ |
Can Guo | 7a7e66c | 2020-12-02 04:04:02 -0800 | [diff] [blame] | 6891 | scsi_print_command(cmd); |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 6892 | if (!hba->req_abort_count) { |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 6893 | ufshcd_update_evt_hist(hba, UFS_EVT_ABORT, tag); |
| 6894 | ufshcd_print_evt_hist(hba); |
Gilad Broner | 6ba6558 | 2017-02-03 16:57:28 -0800 | [diff] [blame] | 6895 | ufshcd_print_host_state(hba); |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 6896 | ufshcd_print_pwr_info(hba); |
| 6897 | ufshcd_print_trs(hba, 1 << tag, true); |
| 6898 | } else { |
| 6899 | ufshcd_print_trs(hba, 1 << tag, false); |
| 6900 | } |
| 6901 | hba->req_abort_count++; |
Gilad Broner | e0b299e | 2017-02-03 16:56:40 -0800 | [diff] [blame] | 6902 | |
Bean Huo | d87a1f6 | 2020-08-11 16:18:59 +0200 | [diff] [blame] | 6903 | if (!(reg & (1 << tag))) { |
| 6904 | dev_err(hba->dev, |
| 6905 | "%s: cmd was completed, but without a notifying intr, tag = %d", |
| 6906 | __func__, tag); |
| 6907 | goto cleanup; |
| 6908 | } |
| 6909 | |
Can Guo | 7a7e66c | 2020-12-02 04:04:02 -0800 | [diff] [blame] | 6910 | /* |
| 6911 | * Task abort to the device W-LUN is illegal. When this command |
| 6912 | * will fail, due to spec violation, scsi err handling next step |
| 6913 | * will be to send LU reset which, again, is a spec violation. |
| 6914 | * To avoid these unnecessary/illegal steps, first we clean up |
| 6915 | * the lrb taken by this cmd and mark the lrb as in_use, then |
| 6916 | * queue the eh_work and bail. |
| 6917 | */ |
| 6918 | if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN) { |
| 6919 | ufshcd_update_evt_hist(hba, UFS_EVT_ABORT, lrbp->lun); |
| 6920 | spin_lock_irqsave(host->host_lock, flags); |
| 6921 | if (lrbp->cmd) { |
| 6922 | __ufshcd_transfer_req_compl(hba, (1UL << tag)); |
| 6923 | __set_bit(tag, &hba->outstanding_reqs); |
| 6924 | lrbp->in_use = true; |
| 6925 | hba->force_reset = true; |
| 6926 | ufshcd_schedule_eh_work(hba); |
| 6927 | } |
| 6928 | |
| 6929 | spin_unlock_irqrestore(host->host_lock, flags); |
| 6930 | goto out; |
| 6931 | } |
| 6932 | |
Gilad Broner | e0b299e | 2017-02-03 16:56:40 -0800 | [diff] [blame] | 6933 | /* Skip task abort in case previous aborts failed and report failure */ |
Martin K. Petersen | 02f7415 | 2020-09-15 11:24:32 -0400 | [diff] [blame] | 6934 | if (lrbp->req_abort_skip) |
Gilad Broner | e0b299e | 2017-02-03 16:56:40 -0800 | [diff] [blame] | 6935 | err = -EIO; |
Martin K. Petersen | 02f7415 | 2020-09-15 11:24:32 -0400 | [diff] [blame] | 6936 | else |
| 6937 | err = ufshcd_try_to_abort_task(hba, tag); |
Gilad Broner | e0b299e | 2017-02-03 16:56:40 -0800 | [diff] [blame] | 6938 | |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6939 | if (!err) { |
Martin K. Petersen | 02f7415 | 2020-09-15 11:24:32 -0400 | [diff] [blame] | 6940 | cleanup: |
| 6941 | spin_lock_irqsave(host->host_lock, flags); |
| 6942 | __ufshcd_transfer_req_compl(hba, (1UL << tag)); |
| 6943 | spin_unlock_irqrestore(host->host_lock, flags); |
| 6944 | out: |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6945 | err = SUCCESS; |
| 6946 | } else { |
| 6947 | dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); |
Gilad Broner | e0b299e | 2017-02-03 16:56:40 -0800 | [diff] [blame] | 6948 | ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6949 | err = FAILED; |
| 6950 | } |
| 6951 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6952 | /* |
| 6953 | * This ufshcd_release() corresponds to the original scsi cmd that got |
| 6954 | * aborted here (as we won't get any IRQ for it). |
| 6955 | */ |
| 6956 | ufshcd_release(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6957 | return err; |
| 6958 | } |
| 6959 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6960 | /** |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6961 | * ufshcd_host_reset_and_restore - reset and restore host controller |
| 6962 | * @hba: per-adapter instance |
| 6963 | * |
| 6964 | * Note that host controller reset may issue DME_RESET to |
| 6965 | * local and remote (device) Uni-Pro stack and the attributes |
| 6966 | * are reset to default state. |
| 6967 | * |
| 6968 | * Returns zero on success, non-zero on failure |
| 6969 | */ |
| 6970 | static int ufshcd_host_reset_and_restore(struct ufs_hba *hba) |
| 6971 | { |
| 6972 | int err; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6973 | unsigned long flags; |
| 6974 | |
Can Guo | 2df74b6 | 2019-11-25 22:53:33 -0800 | [diff] [blame] | 6975 | /* |
| 6976 | * Stop the host controller and complete the requests |
| 6977 | * cleared by h/w |
| 6978 | */ |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 6979 | ufshcd_hba_stop(hba); |
| 6980 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6981 | spin_lock_irqsave(hba->host->host_lock, flags); |
Can Guo | 2df74b6 | 2019-11-25 22:53:33 -0800 | [diff] [blame] | 6982 | hba->silence_err_logs = true; |
| 6983 | ufshcd_complete_requests(hba); |
| 6984 | hba->silence_err_logs = false; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6985 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6986 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 6987 | /* scale up clocks to max frequency before full reinitialization */ |
Subhash Jadavani | 394b949 | 2020-03-26 02:25:40 -0700 | [diff] [blame] | 6988 | ufshcd_set_clk_freq(hba, true); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 6989 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6990 | err = ufshcd_hba_enable(hba); |
| 6991 | if (err) |
| 6992 | goto out; |
| 6993 | |
| 6994 | /* Establish the link again and restore the device */ |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 6995 | err = ufshcd_probe_hba(hba, false); |
Randall Huang | 1918651 | 2020-11-30 20:14:02 -0800 | [diff] [blame] | 6996 | if (!err) |
| 6997 | ufshcd_clear_ua_wluns(hba); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6998 | out: |
| 6999 | if (err) |
| 7000 | dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err); |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 7001 | ufshcd_update_evt_hist(hba, UFS_EVT_HOST_RESET, (u32)err); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 7002 | return err; |
| 7003 | } |
| 7004 | |
| 7005 | /** |
| 7006 | * ufshcd_reset_and_restore - reset and re-initialize host/device |
| 7007 | * @hba: per-adapter instance |
| 7008 | * |
| 7009 | * Reset and recover device, host and re-establish link. This |
| 7010 | * is helpful to recover the communication in fatal error conditions. |
| 7011 | * |
| 7012 | * Returns zero on success, non-zero on failure |
| 7013 | */ |
| 7014 | static int ufshcd_reset_and_restore(struct ufs_hba *hba) |
| 7015 | { |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 7016 | u32 saved_err; |
| 7017 | u32 saved_uic_err; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 7018 | int err = 0; |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 7019 | unsigned long flags; |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7020 | int retries = MAX_HOST_RESET_RETRIES; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 7021 | |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 7022 | /* |
| 7023 | * This is a fresh start, cache and clear saved error first, |
| 7024 | * in case new error generated during reset and restore. |
| 7025 | */ |
| 7026 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 7027 | saved_err = hba->saved_err; |
| 7028 | saved_uic_err = hba->saved_uic_err; |
| 7029 | hba->saved_err = 0; |
| 7030 | hba->saved_uic_err = 0; |
| 7031 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 7032 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7033 | do { |
Bjorn Andersson | d8d9f79 | 2019-08-28 12:17:54 -0700 | [diff] [blame] | 7034 | /* Reset the attached device */ |
Stanley Chu | 31a5d9c | 2020-12-08 21:56:35 +0800 | [diff] [blame] | 7035 | ufshcd_device_reset(hba); |
Bjorn Andersson | d8d9f79 | 2019-08-28 12:17:54 -0700 | [diff] [blame] | 7036 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7037 | err = ufshcd_host_reset_and_restore(hba); |
| 7038 | } while (err && --retries); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 7039 | |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 7040 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 7041 | /* |
| 7042 | * Inform scsi mid-layer that we did reset and allow to handle |
| 7043 | * Unit Attention properly. |
| 7044 | */ |
| 7045 | scsi_report_bus_reset(hba->host, 0); |
| 7046 | if (err) { |
Can Guo | 88a92d6 | 2020-12-02 04:04:01 -0800 | [diff] [blame] | 7047 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 7048 | hba->saved_err |= saved_err; |
| 7049 | hba->saved_uic_err |= saved_uic_err; |
| 7050 | } |
| 7051 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 7052 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 7053 | return err; |
| 7054 | } |
| 7055 | |
| 7056 | /** |
| 7057 | * 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] | 7058 | * @cmd: SCSI command pointer |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 7059 | * |
| 7060 | * Returns SUCCESS/FAILED |
| 7061 | */ |
| 7062 | static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd) |
| 7063 | { |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 7064 | int err = SUCCESS; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 7065 | unsigned long flags; |
| 7066 | struct ufs_hba *hba; |
| 7067 | |
| 7068 | hba = shost_priv(cmd->device->host); |
| 7069 | |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 7070 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 7071 | hba->force_reset = true; |
| 7072 | ufshcd_schedule_eh_work(hba); |
| 7073 | dev_err(hba->dev, "%s: reset in progress - 1\n", __func__); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 7074 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 7075 | |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 7076 | flush_work(&hba->eh_work); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 7077 | |
| 7078 | spin_lock_irqsave(hba->host->host_lock, flags); |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 7079 | if (hba->ufshcd_state == UFSHCD_STATE_ERROR) |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 7080 | err = FAILED; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 7081 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 7082 | |
| 7083 | return err; |
| 7084 | } |
| 7085 | |
| 7086 | /** |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 7087 | * ufshcd_get_max_icc_level - calculate the ICC level |
| 7088 | * @sup_curr_uA: max. current supported by the regulator |
| 7089 | * @start_scan: row at the desc table to start scan from |
| 7090 | * @buff: power descriptor buffer |
| 7091 | * |
| 7092 | * Returns calculated max ICC level for specific regulator |
| 7093 | */ |
| 7094 | static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff) |
| 7095 | { |
| 7096 | int i; |
| 7097 | int curr_uA; |
| 7098 | u16 data; |
| 7099 | u16 unit; |
| 7100 | |
| 7101 | for (i = start_scan; i >= 0; i--) { |
Tomas Winkler | d79713f | 2017-01-05 10:45:11 +0200 | [diff] [blame] | 7102 | data = be16_to_cpup((__be16 *)&buff[2 * i]); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 7103 | unit = (data & ATTR_ICC_LVL_UNIT_MASK) >> |
| 7104 | ATTR_ICC_LVL_UNIT_OFFSET; |
| 7105 | curr_uA = data & ATTR_ICC_LVL_VALUE_MASK; |
| 7106 | switch (unit) { |
| 7107 | case UFSHCD_NANO_AMP: |
| 7108 | curr_uA = curr_uA / 1000; |
| 7109 | break; |
| 7110 | case UFSHCD_MILI_AMP: |
| 7111 | curr_uA = curr_uA * 1000; |
| 7112 | break; |
| 7113 | case UFSHCD_AMP: |
| 7114 | curr_uA = curr_uA * 1000 * 1000; |
| 7115 | break; |
| 7116 | case UFSHCD_MICRO_AMP: |
| 7117 | default: |
| 7118 | break; |
| 7119 | } |
| 7120 | if (sup_curr_uA >= curr_uA) |
| 7121 | break; |
| 7122 | } |
| 7123 | if (i < 0) { |
| 7124 | i = 0; |
| 7125 | pr_err("%s: Couldn't find valid icc_level = %d", __func__, i); |
| 7126 | } |
| 7127 | |
| 7128 | return (u32)i; |
| 7129 | } |
| 7130 | |
| 7131 | /** |
| 7132 | * ufshcd_calc_icc_level - calculate the max ICC level |
| 7133 | * In case regulators are not initialized we'll return 0 |
| 7134 | * @hba: per-adapter instance |
| 7135 | * @desc_buf: power descriptor buffer to extract ICC levels from. |
| 7136 | * @len: length of desc_buff |
| 7137 | * |
| 7138 | * Returns calculated ICC level |
| 7139 | */ |
| 7140 | static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba, |
| 7141 | u8 *desc_buf, int len) |
| 7142 | { |
| 7143 | u32 icc_level = 0; |
| 7144 | |
| 7145 | if (!hba->vreg_info.vcc || !hba->vreg_info.vccq || |
| 7146 | !hba->vreg_info.vccq2) { |
| 7147 | dev_err(hba->dev, |
| 7148 | "%s: Regulator capability was not set, actvIccLevel=%d", |
| 7149 | __func__, icc_level); |
| 7150 | goto out; |
| 7151 | } |
| 7152 | |
Stanley Chu | 0487fff | 2019-03-28 17:16:25 +0800 | [diff] [blame] | 7153 | if (hba->vreg_info.vcc && hba->vreg_info.vcc->max_uA) |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 7154 | icc_level = ufshcd_get_max_icc_level( |
| 7155 | hba->vreg_info.vcc->max_uA, |
| 7156 | POWER_DESC_MAX_ACTV_ICC_LVLS - 1, |
| 7157 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]); |
| 7158 | |
Stanley Chu | 0487fff | 2019-03-28 17:16:25 +0800 | [diff] [blame] | 7159 | if (hba->vreg_info.vccq && hba->vreg_info.vccq->max_uA) |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 7160 | icc_level = ufshcd_get_max_icc_level( |
| 7161 | hba->vreg_info.vccq->max_uA, |
| 7162 | icc_level, |
| 7163 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]); |
| 7164 | |
Stanley Chu | 0487fff | 2019-03-28 17:16:25 +0800 | [diff] [blame] | 7165 | if (hba->vreg_info.vccq2 && hba->vreg_info.vccq2->max_uA) |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 7166 | icc_level = ufshcd_get_max_icc_level( |
| 7167 | hba->vreg_info.vccq2->max_uA, |
| 7168 | icc_level, |
| 7169 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]); |
| 7170 | out: |
| 7171 | return icc_level; |
| 7172 | } |
| 7173 | |
Can Guo | e89860f | 2020-03-26 02:25:41 -0700 | [diff] [blame] | 7174 | static void ufshcd_set_active_icc_lvl(struct ufs_hba *hba) |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 7175 | { |
| 7176 | int ret; |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 7177 | int buff_len = hba->desc_size[QUERY_DESC_IDN_POWER]; |
Kees Cook | bbe21d7 | 2018-05-02 16:58:09 -0700 | [diff] [blame] | 7178 | u8 *desc_buf; |
Can Guo | e89860f | 2020-03-26 02:25:41 -0700 | [diff] [blame] | 7179 | u32 icc_level; |
Kees Cook | bbe21d7 | 2018-05-02 16:58:09 -0700 | [diff] [blame] | 7180 | |
| 7181 | desc_buf = kmalloc(buff_len, GFP_KERNEL); |
| 7182 | if (!desc_buf) |
| 7183 | return; |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 7184 | |
Bean Huo | c4607a0 | 2020-06-03 11:19:56 +0200 | [diff] [blame] | 7185 | ret = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_POWER, 0, 0, |
| 7186 | desc_buf, buff_len); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 7187 | if (ret) { |
| 7188 | dev_err(hba->dev, |
| 7189 | "%s: Failed reading power descriptor.len = %d ret = %d", |
| 7190 | __func__, buff_len, ret); |
Kees Cook | bbe21d7 | 2018-05-02 16:58:09 -0700 | [diff] [blame] | 7191 | goto out; |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 7192 | } |
| 7193 | |
Can Guo | e89860f | 2020-03-26 02:25:41 -0700 | [diff] [blame] | 7194 | icc_level = ufshcd_find_max_sup_active_icc_level(hba, desc_buf, |
| 7195 | buff_len); |
| 7196 | 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] | 7197 | |
Szymon Mielczarek | dbd34a6 | 2017-03-29 08:19:21 +0200 | [diff] [blame] | 7198 | ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
Can Guo | e89860f | 2020-03-26 02:25:41 -0700 | [diff] [blame] | 7199 | QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, &icc_level); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 7200 | |
| 7201 | if (ret) |
| 7202 | dev_err(hba->dev, |
| 7203 | "%s: Failed configuring bActiveICCLevel = %d ret = %d", |
Can Guo | e89860f | 2020-03-26 02:25:41 -0700 | [diff] [blame] | 7204 | __func__, icc_level, ret); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 7205 | |
Kees Cook | bbe21d7 | 2018-05-02 16:58:09 -0700 | [diff] [blame] | 7206 | out: |
| 7207 | kfree(desc_buf); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 7208 | } |
| 7209 | |
Can Guo | fb276f7 | 2020-03-25 18:09:59 -0700 | [diff] [blame] | 7210 | static inline void ufshcd_blk_pm_runtime_init(struct scsi_device *sdev) |
| 7211 | { |
| 7212 | scsi_autopm_get_device(sdev); |
| 7213 | blk_pm_runtime_init(sdev->request_queue, &sdev->sdev_gendev); |
| 7214 | if (sdev->rpm_autosuspend) |
| 7215 | pm_runtime_set_autosuspend_delay(&sdev->sdev_gendev, |
| 7216 | RPM_AUTOSUSPEND_DELAY_MS); |
| 7217 | scsi_autopm_put_device(sdev); |
| 7218 | } |
| 7219 | |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 7220 | /** |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 7221 | * ufshcd_scsi_add_wlus - Adds required W-LUs |
| 7222 | * @hba: per-adapter instance |
| 7223 | * |
| 7224 | * UFS device specification requires the UFS devices to support 4 well known |
| 7225 | * logical units: |
| 7226 | * "REPORT_LUNS" (address: 01h) |
| 7227 | * "UFS Device" (address: 50h) |
| 7228 | * "RPMB" (address: 44h) |
| 7229 | * "BOOT" (address: 30h) |
| 7230 | * UFS device's power management needs to be controlled by "POWER CONDITION" |
| 7231 | * field of SSU (START STOP UNIT) command. But this "power condition" field |
| 7232 | * will take effect only when its sent to "UFS device" well known logical unit |
| 7233 | * hence we require the scsi_device instance to represent this logical unit in |
| 7234 | * 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] | 7235 | * |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 7236 | * We also require the scsi_device instance for "RPMB" (Replay Protected Memory |
| 7237 | * Block) LU so user space process can control this LU. User space may also |
| 7238 | * want to have access to BOOT LU. |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 7239 | * |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 7240 | * This function adds scsi device instances for each of all well known LUs |
| 7241 | * (except "REPORT LUNS" LU). |
| 7242 | * |
| 7243 | * Returns zero on success (all required W-LUs are added successfully), |
| 7244 | * non-zero error value on failure (if failed to add any of the required W-LU). |
| 7245 | */ |
| 7246 | static int ufshcd_scsi_add_wlus(struct ufs_hba *hba) |
| 7247 | { |
| 7248 | int ret = 0; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 7249 | struct scsi_device *sdev_boot; |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 7250 | |
| 7251 | hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0, |
| 7252 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL); |
| 7253 | if (IS_ERR(hba->sdev_ufs_device)) { |
| 7254 | ret = PTR_ERR(hba->sdev_ufs_device); |
| 7255 | hba->sdev_ufs_device = NULL; |
| 7256 | goto out; |
| 7257 | } |
Can Guo | fb276f7 | 2020-03-25 18:09:59 -0700 | [diff] [blame] | 7258 | ufshcd_blk_pm_runtime_init(hba->sdev_ufs_device); |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 7259 | scsi_device_put(hba->sdev_ufs_device); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 7260 | |
Jaegeuk Kim | 4f3e900 | 2020-11-17 08:58:35 -0800 | [diff] [blame] | 7261 | hba->sdev_rpmb = __scsi_add_device(hba->host, 0, 0, |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 7262 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL); |
Jaegeuk Kim | 4f3e900 | 2020-11-17 08:58:35 -0800 | [diff] [blame] | 7263 | if (IS_ERR(hba->sdev_rpmb)) { |
| 7264 | ret = PTR_ERR(hba->sdev_rpmb); |
Huanlin Ke | 3d21fbd | 2017-09-22 18:31:47 +0800 | [diff] [blame] | 7265 | goto remove_sdev_ufs_device; |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 7266 | } |
Jaegeuk Kim | 4f3e900 | 2020-11-17 08:58:35 -0800 | [diff] [blame] | 7267 | ufshcd_blk_pm_runtime_init(hba->sdev_rpmb); |
| 7268 | scsi_device_put(hba->sdev_rpmb); |
Huanlin Ke | 3d21fbd | 2017-09-22 18:31:47 +0800 | [diff] [blame] | 7269 | |
| 7270 | sdev_boot = __scsi_add_device(hba->host, 0, 0, |
| 7271 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL); |
Can Guo | fb276f7 | 2020-03-25 18:09:59 -0700 | [diff] [blame] | 7272 | if (IS_ERR(sdev_boot)) { |
Huanlin Ke | 3d21fbd | 2017-09-22 18:31:47 +0800 | [diff] [blame] | 7273 | dev_err(hba->dev, "%s: BOOT WLUN not found\n", __func__); |
Can Guo | fb276f7 | 2020-03-25 18:09:59 -0700 | [diff] [blame] | 7274 | } else { |
| 7275 | ufshcd_blk_pm_runtime_init(sdev_boot); |
Huanlin Ke | 3d21fbd | 2017-09-22 18:31:47 +0800 | [diff] [blame] | 7276 | scsi_device_put(sdev_boot); |
Can Guo | fb276f7 | 2020-03-25 18:09:59 -0700 | [diff] [blame] | 7277 | } |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 7278 | goto out; |
| 7279 | |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 7280 | remove_sdev_ufs_device: |
| 7281 | scsi_remove_device(hba->sdev_ufs_device); |
| 7282 | out: |
| 7283 | return ret; |
| 7284 | } |
| 7285 | |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 7286 | static void ufshcd_wb_probe(struct ufs_hba *hba, u8 *desc_buf) |
| 7287 | { |
Stanley Chu | a7f1e69 | 2020-06-25 11:04:30 +0800 | [diff] [blame] | 7288 | struct ufs_dev_info *dev_info = &hba->dev_info; |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 7289 | u8 lun; |
| 7290 | u32 d_lu_wb_buf_alloc; |
Bean Huo | e8d0381 | 2021-01-19 17:38:45 +0100 | [diff] [blame] | 7291 | u32 ext_ufs_feature; |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 7292 | |
Stanley Chu | 817d7e1 | 2020-05-08 16:01:08 +0800 | [diff] [blame] | 7293 | if (!ufshcd_is_wb_allowed(hba)) |
| 7294 | return; |
Stanley Chu | a7f1e69 | 2020-06-25 11:04:30 +0800 | [diff] [blame] | 7295 | /* |
| 7296 | * Probe WB only for UFS-2.2 and UFS-3.1 (and later) devices or |
| 7297 | * UFS devices with quirk UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES |
| 7298 | * enabled |
| 7299 | */ |
| 7300 | if (!(dev_info->wspecversion >= 0x310 || |
| 7301 | dev_info->wspecversion == 0x220 || |
| 7302 | (hba->dev_quirks & UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES))) |
| 7303 | goto wb_disabled; |
Stanley Chu | 817d7e1 | 2020-05-08 16:01:08 +0800 | [diff] [blame] | 7304 | |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 7305 | if (hba->desc_size[QUERY_DESC_IDN_DEVICE] < |
| 7306 | DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP + 4) |
Stanley Chu | 817d7e1 | 2020-05-08 16:01:08 +0800 | [diff] [blame] | 7307 | goto wb_disabled; |
| 7308 | |
Bean Huo | e8d0381 | 2021-01-19 17:38:45 +0100 | [diff] [blame] | 7309 | ext_ufs_feature = get_unaligned_be32(desc_buf + |
| 7310 | DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP); |
Stanley Chu | 817d7e1 | 2020-05-08 16:01:08 +0800 | [diff] [blame] | 7311 | |
Bean Huo | e8d0381 | 2021-01-19 17:38:45 +0100 | [diff] [blame] | 7312 | if (!(ext_ufs_feature & UFS_DEV_WRITE_BOOSTER_SUP)) |
Stanley Chu | 817d7e1 | 2020-05-08 16:01:08 +0800 | [diff] [blame] | 7313 | goto wb_disabled; |
| 7314 | |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 7315 | /* |
Bean Huo | ae1ce1f | 2021-01-19 17:38:44 +0100 | [diff] [blame] | 7316 | * WB may be supported but not configured while provisioning. The spec |
| 7317 | * says, in dedicated wb buffer mode, a max of 1 lun would have wb |
| 7318 | * buffer configured. |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 7319 | */ |
Bean Huo | 4cd4899 | 2021-01-19 17:38:46 +0100 | [diff] [blame] | 7320 | dev_info->wb_buffer_type = desc_buf[DEVICE_DESC_PARAM_WB_TYPE]; |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 7321 | |
Stanley Chu | a7f1e69 | 2020-06-25 11:04:30 +0800 | [diff] [blame] | 7322 | dev_info->b_presrv_uspc_en = |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 7323 | desc_buf[DEVICE_DESC_PARAM_WB_PRESRV_USRSPC_EN]; |
| 7324 | |
Bean Huo | 4cd4899 | 2021-01-19 17:38:46 +0100 | [diff] [blame] | 7325 | if (dev_info->wb_buffer_type == WB_BUF_MODE_SHARED) { |
Bean Huo | e8d0381 | 2021-01-19 17:38:45 +0100 | [diff] [blame] | 7326 | if (!get_unaligned_be32(desc_buf + |
| 7327 | DEVICE_DESC_PARAM_WB_SHARED_ALLOC_UNITS)) |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 7328 | goto wb_disabled; |
| 7329 | } else { |
| 7330 | for (lun = 0; lun < UFS_UPIU_MAX_WB_LUN_ID; lun++) { |
| 7331 | d_lu_wb_buf_alloc = 0; |
| 7332 | ufshcd_read_unit_desc_param(hba, |
| 7333 | lun, |
| 7334 | UNIT_DESC_PARAM_WB_BUF_ALLOC_UNITS, |
| 7335 | (u8 *)&d_lu_wb_buf_alloc, |
| 7336 | sizeof(d_lu_wb_buf_alloc)); |
| 7337 | if (d_lu_wb_buf_alloc) { |
Stanley Chu | a7f1e69 | 2020-06-25 11:04:30 +0800 | [diff] [blame] | 7338 | dev_info->wb_dedicated_lu = lun; |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 7339 | break; |
| 7340 | } |
| 7341 | } |
Stanley Chu | 817d7e1 | 2020-05-08 16:01:08 +0800 | [diff] [blame] | 7342 | |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 7343 | if (!d_lu_wb_buf_alloc) |
| 7344 | goto wb_disabled; |
| 7345 | } |
Stanley Chu | 817d7e1 | 2020-05-08 16:01:08 +0800 | [diff] [blame] | 7346 | return; |
| 7347 | |
| 7348 | wb_disabled: |
| 7349 | hba->caps &= ~UFSHCD_CAP_WB_EN; |
| 7350 | } |
| 7351 | |
Stanley Chu | 8db269a | 2020-05-08 16:01:10 +0800 | [diff] [blame] | 7352 | 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] | 7353 | { |
| 7354 | struct ufs_dev_fix *f; |
| 7355 | struct ufs_dev_info *dev_info = &hba->dev_info; |
| 7356 | |
Stanley Chu | 8db269a | 2020-05-08 16:01:10 +0800 | [diff] [blame] | 7357 | if (!fixups) |
| 7358 | return; |
| 7359 | |
| 7360 | for (f = fixups; f->quirk; f++) { |
Stanley Chu | 817d7e1 | 2020-05-08 16:01:08 +0800 | [diff] [blame] | 7361 | if ((f->wmanufacturerid == dev_info->wmanufacturerid || |
| 7362 | f->wmanufacturerid == UFS_ANY_VENDOR) && |
| 7363 | ((dev_info->model && |
| 7364 | STR_PRFX_EQUAL(f->model, dev_info->model)) || |
| 7365 | !strcmp(f->model, UFS_ANY_MODEL))) |
| 7366 | hba->dev_quirks |= f->quirk; |
| 7367 | } |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 7368 | } |
Stanley Chu | 8db269a | 2020-05-08 16:01:10 +0800 | [diff] [blame] | 7369 | EXPORT_SYMBOL_GPL(ufshcd_fixup_dev_quirks); |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 7370 | |
Stanley Chu | c28c00b | 2020-05-08 16:01:09 +0800 | [diff] [blame] | 7371 | static void ufs_fixup_device_setup(struct ufs_hba *hba) |
| 7372 | { |
| 7373 | /* fix by general quirk table */ |
Stanley Chu | 8db269a | 2020-05-08 16:01:10 +0800 | [diff] [blame] | 7374 | ufshcd_fixup_dev_quirks(hba, ufs_fixups); |
Stanley Chu | c28c00b | 2020-05-08 16:01:09 +0800 | [diff] [blame] | 7375 | |
| 7376 | /* allow vendors to fix quirks */ |
| 7377 | ufshcd_vops_fixup_dev_quirks(hba); |
| 7378 | } |
| 7379 | |
Bean Huo | 0975006 | 2020-01-20 14:08:14 +0100 | [diff] [blame] | 7380 | static int ufs_get_device_desc(struct ufs_hba *hba) |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 7381 | { |
| 7382 | int err; |
| 7383 | u8 model_index; |
Kees Cook | bbe21d7 | 2018-05-02 16:58:09 -0700 | [diff] [blame] | 7384 | u8 *desc_buf; |
Bean Huo | 0975006 | 2020-01-20 14:08:14 +0100 | [diff] [blame] | 7385 | struct ufs_dev_info *dev_info = &hba->dev_info; |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 7386 | |
Bean Huo | 458a45f | 2020-06-03 11:19:55 +0200 | [diff] [blame] | 7387 | desc_buf = kmalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL); |
Kees Cook | bbe21d7 | 2018-05-02 16:58:09 -0700 | [diff] [blame] | 7388 | if (!desc_buf) { |
| 7389 | err = -ENOMEM; |
| 7390 | goto out; |
| 7391 | } |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 7392 | |
Bean Huo | c4607a0 | 2020-06-03 11:19:56 +0200 | [diff] [blame] | 7393 | 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] | 7394 | hba->desc_size[QUERY_DESC_IDN_DEVICE]); |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 7395 | if (err) { |
| 7396 | dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n", |
| 7397 | __func__, err); |
| 7398 | goto out; |
| 7399 | } |
| 7400 | |
| 7401 | /* |
| 7402 | * getting vendor (manufacturerID) and Bank Index in big endian |
| 7403 | * format |
| 7404 | */ |
Bean Huo | 0975006 | 2020-01-20 14:08:14 +0100 | [diff] [blame] | 7405 | dev_info->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 | |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 7406 | desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1]; |
| 7407 | |
Can Guo | 09f1779 | 2020-02-10 19:40:49 -0800 | [diff] [blame] | 7408 | /* getting Specification Version in big endian format */ |
| 7409 | dev_info->wspecversion = desc_buf[DEVICE_DESC_PARAM_SPEC_VER] << 8 | |
| 7410 | desc_buf[DEVICE_DESC_PARAM_SPEC_VER + 1]; |
| 7411 | |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 7412 | model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME]; |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 7413 | |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 7414 | err = ufshcd_read_string_desc(hba, model_index, |
Bean Huo | 0975006 | 2020-01-20 14:08:14 +0100 | [diff] [blame] | 7415 | &dev_info->model, SD_ASCII_STD); |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 7416 | if (err < 0) { |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 7417 | dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n", |
| 7418 | __func__, err); |
| 7419 | goto out; |
| 7420 | } |
| 7421 | |
Stanley Chu | 817d7e1 | 2020-05-08 16:01:08 +0800 | [diff] [blame] | 7422 | ufs_fixup_device_setup(hba); |
| 7423 | |
Stanley Chu | a7f1e69 | 2020-06-25 11:04:30 +0800 | [diff] [blame] | 7424 | ufshcd_wb_probe(hba, desc_buf); |
Stanley Chu | 817d7e1 | 2020-05-08 16:01:08 +0800 | [diff] [blame] | 7425 | |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 7426 | /* |
| 7427 | * ufshcd_read_string_desc returns size of the string |
| 7428 | * reset the error value |
| 7429 | */ |
| 7430 | err = 0; |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 7431 | |
| 7432 | out: |
Kees Cook | bbe21d7 | 2018-05-02 16:58:09 -0700 | [diff] [blame] | 7433 | kfree(desc_buf); |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 7434 | return err; |
| 7435 | } |
| 7436 | |
Bean Huo | 0975006 | 2020-01-20 14:08:14 +0100 | [diff] [blame] | 7437 | static void ufs_put_device_desc(struct ufs_hba *hba) |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 7438 | { |
Bean Huo | 0975006 | 2020-01-20 14:08:14 +0100 | [diff] [blame] | 7439 | struct ufs_dev_info *dev_info = &hba->dev_info; |
| 7440 | |
| 7441 | kfree(dev_info->model); |
| 7442 | dev_info->model = NULL; |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 7443 | } |
| 7444 | |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 7445 | /** |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 7446 | * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro |
| 7447 | * @hba: per-adapter instance |
| 7448 | * |
| 7449 | * PA_TActivate parameter can be tuned manually if UniPro version is less than |
| 7450 | * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's |
| 7451 | * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce |
| 7452 | * the hibern8 exit latency. |
| 7453 | * |
| 7454 | * Returns zero on success, non-zero error value on failure. |
| 7455 | */ |
| 7456 | static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba) |
| 7457 | { |
| 7458 | int ret = 0; |
| 7459 | u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate; |
| 7460 | |
| 7461 | ret = ufshcd_dme_peer_get(hba, |
| 7462 | UIC_ARG_MIB_SEL( |
| 7463 | RX_MIN_ACTIVATETIME_CAPABILITY, |
| 7464 | UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), |
| 7465 | &peer_rx_min_activatetime); |
| 7466 | if (ret) |
| 7467 | goto out; |
| 7468 | |
| 7469 | /* make sure proper unit conversion is applied */ |
| 7470 | tuned_pa_tactivate = |
| 7471 | ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US) |
| 7472 | / PA_TACTIVATE_TIME_UNIT_US); |
| 7473 | ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 7474 | tuned_pa_tactivate); |
| 7475 | |
| 7476 | out: |
| 7477 | return ret; |
| 7478 | } |
| 7479 | |
| 7480 | /** |
| 7481 | * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro |
| 7482 | * @hba: per-adapter instance |
| 7483 | * |
| 7484 | * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than |
| 7485 | * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's |
| 7486 | * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY. |
| 7487 | * This optimal value can help reduce the hibern8 exit latency. |
| 7488 | * |
| 7489 | * Returns zero on success, non-zero error value on failure. |
| 7490 | */ |
| 7491 | static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba) |
| 7492 | { |
| 7493 | int ret = 0; |
| 7494 | u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0; |
| 7495 | u32 max_hibern8_time, tuned_pa_hibern8time; |
| 7496 | |
| 7497 | ret = ufshcd_dme_get(hba, |
| 7498 | UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY, |
| 7499 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), |
| 7500 | &local_tx_hibern8_time_cap); |
| 7501 | if (ret) |
| 7502 | goto out; |
| 7503 | |
| 7504 | ret = ufshcd_dme_peer_get(hba, |
| 7505 | UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY, |
| 7506 | UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), |
| 7507 | &peer_rx_hibern8_time_cap); |
| 7508 | if (ret) |
| 7509 | goto out; |
| 7510 | |
| 7511 | max_hibern8_time = max(local_tx_hibern8_time_cap, |
| 7512 | peer_rx_hibern8_time_cap); |
| 7513 | /* make sure proper unit conversion is applied */ |
| 7514 | tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US) |
| 7515 | / PA_HIBERN8_TIME_UNIT_US); |
| 7516 | ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME), |
| 7517 | tuned_pa_hibern8time); |
| 7518 | out: |
| 7519 | return ret; |
| 7520 | } |
| 7521 | |
subhashj@codeaurora.org | c6a6db4 | 2016-11-23 16:32:08 -0800 | [diff] [blame] | 7522 | /** |
| 7523 | * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is |
| 7524 | * less than device PA_TACTIVATE time. |
| 7525 | * @hba: per-adapter instance |
| 7526 | * |
| 7527 | * Some UFS devices require host PA_TACTIVATE to be lower than device |
| 7528 | * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk |
| 7529 | * for such devices. |
| 7530 | * |
| 7531 | * Returns zero on success, non-zero error value on failure. |
| 7532 | */ |
| 7533 | static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba) |
| 7534 | { |
| 7535 | int ret = 0; |
| 7536 | u32 granularity, peer_granularity; |
| 7537 | u32 pa_tactivate, peer_pa_tactivate; |
| 7538 | u32 pa_tactivate_us, peer_pa_tactivate_us; |
| 7539 | u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100}; |
| 7540 | |
| 7541 | ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY), |
| 7542 | &granularity); |
| 7543 | if (ret) |
| 7544 | goto out; |
| 7545 | |
| 7546 | ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY), |
| 7547 | &peer_granularity); |
| 7548 | if (ret) |
| 7549 | goto out; |
| 7550 | |
| 7551 | if ((granularity < PA_GRANULARITY_MIN_VAL) || |
| 7552 | (granularity > PA_GRANULARITY_MAX_VAL)) { |
| 7553 | dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d", |
| 7554 | __func__, granularity); |
| 7555 | return -EINVAL; |
| 7556 | } |
| 7557 | |
| 7558 | if ((peer_granularity < PA_GRANULARITY_MIN_VAL) || |
| 7559 | (peer_granularity > PA_GRANULARITY_MAX_VAL)) { |
| 7560 | dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d", |
| 7561 | __func__, peer_granularity); |
| 7562 | return -EINVAL; |
| 7563 | } |
| 7564 | |
| 7565 | ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate); |
| 7566 | if (ret) |
| 7567 | goto out; |
| 7568 | |
| 7569 | ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 7570 | &peer_pa_tactivate); |
| 7571 | if (ret) |
| 7572 | goto out; |
| 7573 | |
| 7574 | pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1]; |
| 7575 | peer_pa_tactivate_us = peer_pa_tactivate * |
| 7576 | gran_to_us_table[peer_granularity - 1]; |
| 7577 | |
| 7578 | if (pa_tactivate_us > peer_pa_tactivate_us) { |
| 7579 | u32 new_peer_pa_tactivate; |
| 7580 | |
| 7581 | new_peer_pa_tactivate = pa_tactivate_us / |
| 7582 | gran_to_us_table[peer_granularity - 1]; |
| 7583 | new_peer_pa_tactivate++; |
| 7584 | ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 7585 | new_peer_pa_tactivate); |
| 7586 | } |
| 7587 | |
| 7588 | out: |
| 7589 | return ret; |
| 7590 | } |
| 7591 | |
Bean Huo | 0975006 | 2020-01-20 14:08:14 +0100 | [diff] [blame] | 7592 | static void ufshcd_tune_unipro_params(struct ufs_hba *hba) |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 7593 | { |
| 7594 | if (ufshcd_is_unipro_pa_params_tuning_req(hba)) { |
| 7595 | ufshcd_tune_pa_tactivate(hba); |
| 7596 | ufshcd_tune_pa_hibern8time(hba); |
| 7597 | } |
| 7598 | |
Can Guo | e91ed9e | 2020-02-23 20:09:21 -0800 | [diff] [blame] | 7599 | ufshcd_vops_apply_dev_quirks(hba); |
| 7600 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 7601 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE) |
| 7602 | /* set 1ms timeout for PA_TACTIVATE */ |
| 7603 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10); |
subhashj@codeaurora.org | c6a6db4 | 2016-11-23 16:32:08 -0800 | [diff] [blame] | 7604 | |
| 7605 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE) |
| 7606 | ufshcd_quirk_tune_host_pa_tactivate(hba); |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 7607 | } |
| 7608 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 7609 | static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba) |
| 7610 | { |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 7611 | hba->ufs_stats.hibern8_exit_cnt = 0; |
| 7612 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 7613 | hba->req_abort_count = 0; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 7614 | } |
| 7615 | |
Bean Huo | 731f062 | 2020-01-20 14:08:19 +0100 | [diff] [blame] | 7616 | static int ufshcd_device_geo_params_init(struct ufs_hba *hba) |
| 7617 | { |
| 7618 | int err; |
| 7619 | size_t buff_len; |
| 7620 | u8 *desc_buf; |
| 7621 | |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 7622 | buff_len = hba->desc_size[QUERY_DESC_IDN_GEOMETRY]; |
Bean Huo | 731f062 | 2020-01-20 14:08:19 +0100 | [diff] [blame] | 7623 | desc_buf = kmalloc(buff_len, GFP_KERNEL); |
| 7624 | if (!desc_buf) { |
| 7625 | err = -ENOMEM; |
| 7626 | goto out; |
| 7627 | } |
| 7628 | |
Bean Huo | c4607a0 | 2020-06-03 11:19:56 +0200 | [diff] [blame] | 7629 | err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_GEOMETRY, 0, 0, |
| 7630 | desc_buf, buff_len); |
Bean Huo | 731f062 | 2020-01-20 14:08:19 +0100 | [diff] [blame] | 7631 | if (err) { |
| 7632 | dev_err(hba->dev, "%s: Failed reading Geometry Desc. err = %d\n", |
| 7633 | __func__, err); |
| 7634 | goto out; |
| 7635 | } |
| 7636 | |
| 7637 | if (desc_buf[GEOMETRY_DESC_PARAM_MAX_NUM_LUN] == 1) |
| 7638 | hba->dev_info.max_lu_supported = 32; |
| 7639 | else if (desc_buf[GEOMETRY_DESC_PARAM_MAX_NUM_LUN] == 0) |
| 7640 | hba->dev_info.max_lu_supported = 8; |
| 7641 | |
| 7642 | out: |
| 7643 | kfree(desc_buf); |
| 7644 | return err; |
| 7645 | } |
| 7646 | |
Subhash Jadavani | 9e1e8a7 | 2018-10-16 14:29:41 +0530 | [diff] [blame] | 7647 | static struct ufs_ref_clk ufs_ref_clk_freqs[] = { |
| 7648 | {19200000, REF_CLK_FREQ_19_2_MHZ}, |
| 7649 | {26000000, REF_CLK_FREQ_26_MHZ}, |
| 7650 | {38400000, REF_CLK_FREQ_38_4_MHZ}, |
| 7651 | {52000000, REF_CLK_FREQ_52_MHZ}, |
| 7652 | {0, REF_CLK_FREQ_INVAL}, |
| 7653 | }; |
| 7654 | |
| 7655 | static enum ufs_ref_clk_freq |
| 7656 | ufs_get_bref_clk_from_hz(unsigned long freq) |
| 7657 | { |
| 7658 | int i; |
| 7659 | |
| 7660 | for (i = 0; ufs_ref_clk_freqs[i].freq_hz; i++) |
| 7661 | if (ufs_ref_clk_freqs[i].freq_hz == freq) |
| 7662 | return ufs_ref_clk_freqs[i].val; |
| 7663 | |
| 7664 | return REF_CLK_FREQ_INVAL; |
| 7665 | } |
| 7666 | |
| 7667 | void ufshcd_parse_dev_ref_clk_freq(struct ufs_hba *hba, struct clk *refclk) |
| 7668 | { |
| 7669 | unsigned long freq; |
| 7670 | |
| 7671 | freq = clk_get_rate(refclk); |
| 7672 | |
| 7673 | hba->dev_ref_clk_freq = |
| 7674 | ufs_get_bref_clk_from_hz(freq); |
| 7675 | |
| 7676 | if (hba->dev_ref_clk_freq == REF_CLK_FREQ_INVAL) |
| 7677 | dev_err(hba->dev, |
| 7678 | "invalid ref_clk setting = %ld\n", freq); |
| 7679 | } |
| 7680 | |
| 7681 | static int ufshcd_set_dev_ref_clk(struct ufs_hba *hba) |
| 7682 | { |
| 7683 | int err; |
| 7684 | u32 ref_clk; |
| 7685 | u32 freq = hba->dev_ref_clk_freq; |
| 7686 | |
| 7687 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
| 7688 | QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &ref_clk); |
| 7689 | |
| 7690 | if (err) { |
| 7691 | dev_err(hba->dev, "failed reading bRefClkFreq. err = %d\n", |
| 7692 | err); |
| 7693 | goto out; |
| 7694 | } |
| 7695 | |
| 7696 | if (ref_clk == freq) |
| 7697 | goto out; /* nothing to update */ |
| 7698 | |
| 7699 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
| 7700 | QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &freq); |
| 7701 | |
| 7702 | if (err) { |
| 7703 | dev_err(hba->dev, "bRefClkFreq setting to %lu Hz failed\n", |
| 7704 | ufs_ref_clk_freqs[freq].freq_hz); |
| 7705 | goto out; |
| 7706 | } |
| 7707 | |
| 7708 | dev_dbg(hba->dev, "bRefClkFreq setting to %lu Hz succeeded\n", |
| 7709 | ufs_ref_clk_freqs[freq].freq_hz); |
| 7710 | |
| 7711 | out: |
| 7712 | return err; |
| 7713 | } |
| 7714 | |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7715 | static int ufshcd_device_params_init(struct ufs_hba *hba) |
| 7716 | { |
| 7717 | bool flag; |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 7718 | int ret, i; |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7719 | |
Bean Huo | 7a0bf85 | 2020-06-03 11:19:58 +0200 | [diff] [blame] | 7720 | /* Init device descriptor sizes */ |
| 7721 | for (i = 0; i < QUERY_DESC_IDN_MAX; i++) |
| 7722 | hba->desc_size[i] = QUERY_DESC_MAX_SIZE; |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7723 | |
Bean Huo | 731f062 | 2020-01-20 14:08:19 +0100 | [diff] [blame] | 7724 | /* Init UFS geometry descriptor related parameters */ |
| 7725 | ret = ufshcd_device_geo_params_init(hba); |
| 7726 | if (ret) |
| 7727 | goto out; |
| 7728 | |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7729 | /* Check and apply UFS device quirks */ |
| 7730 | ret = ufs_get_device_desc(hba); |
| 7731 | if (ret) { |
| 7732 | dev_err(hba->dev, "%s: Failed getting device info. err = %d\n", |
| 7733 | __func__, ret); |
| 7734 | goto out; |
| 7735 | } |
| 7736 | |
Can Guo | 09f1779 | 2020-02-10 19:40:49 -0800 | [diff] [blame] | 7737 | ufshcd_get_ref_clk_gating_wait(hba); |
| 7738 | |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7739 | if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG, |
Stanley Chu | 1f34eed | 2020-05-08 16:01:12 +0800 | [diff] [blame] | 7740 | QUERY_FLAG_IDN_PWR_ON_WPE, 0, &flag)) |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7741 | hba->dev_info.f_power_on_wp_en = flag; |
| 7742 | |
Bean Huo | 2b35b2a | 2020-01-20 14:08:16 +0100 | [diff] [blame] | 7743 | /* Probe maximum power mode co-supported by both UFS host and device */ |
| 7744 | if (ufshcd_get_max_pwr_mode(hba)) |
| 7745 | dev_err(hba->dev, |
| 7746 | "%s: Failed getting max supported power mode\n", |
| 7747 | __func__); |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7748 | out: |
| 7749 | return ret; |
| 7750 | } |
| 7751 | |
| 7752 | /** |
| 7753 | * ufshcd_add_lus - probe and add UFS logical units |
| 7754 | * @hba: per-adapter instance |
| 7755 | */ |
| 7756 | static int ufshcd_add_lus(struct ufs_hba *hba) |
| 7757 | { |
| 7758 | int ret; |
| 7759 | |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7760 | /* Add required well known logical units to scsi mid layer */ |
| 7761 | ret = ufshcd_scsi_add_wlus(hba); |
| 7762 | if (ret) |
| 7763 | goto out; |
| 7764 | |
| 7765 | /* Initialize devfreq after UFS device is detected */ |
| 7766 | if (ufshcd_is_clkscaling_supported(hba)) { |
| 7767 | memcpy(&hba->clk_scaling.saved_pwr_info.info, |
| 7768 | &hba->pwr_info, |
| 7769 | sizeof(struct ufs_pa_layer_attr)); |
| 7770 | hba->clk_scaling.saved_pwr_info.is_valid = true; |
Stanley Chu | b058fa8 | 2021-01-20 23:01:41 +0800 | [diff] [blame^] | 7771 | hba->clk_scaling.is_allowed = true; |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7772 | |
Stanley Chu | b058fa8 | 2021-01-20 23:01:41 +0800 | [diff] [blame^] | 7773 | ret = ufshcd_devfreq_init(hba); |
| 7774 | if (ret) |
| 7775 | goto out; |
| 7776 | |
| 7777 | hba->clk_scaling.is_enabled = true; |
| 7778 | ufshcd_init_clk_scaling_sysfs(hba); |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7779 | } |
| 7780 | |
| 7781 | ufs_bsg_probe(hba); |
| 7782 | scsi_scan_host(hba->host); |
| 7783 | pm_runtime_put_sync(hba->dev); |
| 7784 | |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7785 | out: |
| 7786 | return ret; |
| 7787 | } |
| 7788 | |
Jaegeuk Kim | 4f3e900 | 2020-11-17 08:58:35 -0800 | [diff] [blame] | 7789 | static int |
| 7790 | ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp); |
| 7791 | |
| 7792 | static int ufshcd_clear_ua_wlun(struct ufs_hba *hba, u8 wlun) |
| 7793 | { |
| 7794 | struct scsi_device *sdp; |
| 7795 | unsigned long flags; |
| 7796 | int ret = 0; |
| 7797 | |
| 7798 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 7799 | if (wlun == UFS_UPIU_UFS_DEVICE_WLUN) |
| 7800 | sdp = hba->sdev_ufs_device; |
| 7801 | else if (wlun == UFS_UPIU_RPMB_WLUN) |
| 7802 | sdp = hba->sdev_rpmb; |
| 7803 | else |
Arnd Bergmann | 4c60244 | 2020-12-03 23:31:26 +0100 | [diff] [blame] | 7804 | BUG(); |
Jaegeuk Kim | 4f3e900 | 2020-11-17 08:58:35 -0800 | [diff] [blame] | 7805 | if (sdp) { |
| 7806 | ret = scsi_device_get(sdp); |
| 7807 | if (!ret && !scsi_device_online(sdp)) { |
| 7808 | ret = -ENODEV; |
| 7809 | scsi_device_put(sdp); |
| 7810 | } |
| 7811 | } else { |
| 7812 | ret = -ENODEV; |
| 7813 | } |
| 7814 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 7815 | if (ret) |
| 7816 | goto out_err; |
| 7817 | |
| 7818 | ret = ufshcd_send_request_sense(hba, sdp); |
| 7819 | scsi_device_put(sdp); |
| 7820 | out_err: |
| 7821 | if (ret) |
| 7822 | dev_err(hba->dev, "%s: UAC clear LU=%x ret = %d\n", |
| 7823 | __func__, wlun, ret); |
| 7824 | return ret; |
| 7825 | } |
| 7826 | |
| 7827 | static int ufshcd_clear_ua_wluns(struct ufs_hba *hba) |
| 7828 | { |
| 7829 | int ret = 0; |
| 7830 | |
| 7831 | if (!hba->wlun_dev_clr_ua) |
| 7832 | goto out; |
| 7833 | |
| 7834 | ret = ufshcd_clear_ua_wlun(hba, UFS_UPIU_UFS_DEVICE_WLUN); |
| 7835 | if (!ret) |
| 7836 | ret = ufshcd_clear_ua_wlun(hba, UFS_UPIU_RPMB_WLUN); |
| 7837 | if (!ret) |
| 7838 | hba->wlun_dev_clr_ua = false; |
| 7839 | out: |
| 7840 | if (ret) |
| 7841 | dev_err(hba->dev, "%s: Failed to clear UAC WLUNS ret = %d\n", |
| 7842 | __func__, ret); |
| 7843 | return ret; |
| 7844 | } |
| 7845 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 7846 | /** |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7847 | * ufshcd_probe_hba - probe hba to detect device and initialize |
| 7848 | * @hba: per-adapter instance |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7849 | * @async: asynchronous execution or not |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7850 | * |
| 7851 | * Execute link-startup and verify device initialization |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7852 | */ |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7853 | static int ufshcd_probe_hba(struct ufs_hba *hba, bool async) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7854 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7855 | int ret; |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 7856 | unsigned long flags; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7857 | ktime_t start = ktime_get(); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7858 | |
| 7859 | ret = ufshcd_link_startup(hba); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 7860 | if (ret) |
| 7861 | goto out; |
| 7862 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 7863 | /* Debug counters initialization */ |
| 7864 | ufshcd_clear_dbg_ufs_stats(hba); |
| 7865 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7866 | /* UniPro link is active now */ |
| 7867 | ufshcd_set_link_active(hba); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 7868 | |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7869 | /* Verify device initialization by sending NOP OUT UPIU */ |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 7870 | ret = ufshcd_verify_dev_init(hba); |
| 7871 | if (ret) |
| 7872 | goto out; |
| 7873 | |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7874 | /* Initiate UFS initialization, and waiting until completion */ |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 7875 | ret = ufshcd_complete_dev_init(hba); |
| 7876 | if (ret) |
| 7877 | goto out; |
| 7878 | |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7879 | /* |
| 7880 | * Initialize UFS device parameters used by driver, these |
| 7881 | * parameters are associated with UFS descriptors. |
| 7882 | */ |
| 7883 | if (async) { |
| 7884 | ret = ufshcd_device_params_init(hba); |
| 7885 | if (ret) |
| 7886 | goto out; |
Tomas Winkler | 93fdd5a | 2017-01-05 10:45:12 +0200 | [diff] [blame] | 7887 | } |
| 7888 | |
Bean Huo | 0975006 | 2020-01-20 14:08:14 +0100 | [diff] [blame] | 7889 | ufshcd_tune_unipro_params(hba); |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 7890 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7891 | /* UFS device is also active now */ |
| 7892 | ufshcd_set_ufs_dev_active(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 7893 | ufshcd_force_reset_auto_bkops(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7894 | hba->wlun_dev_clr_ua = true; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 7895 | |
Bean Huo | 2b35b2a | 2020-01-20 14:08:16 +0100 | [diff] [blame] | 7896 | /* Gear up to HS gear if supported */ |
| 7897 | if (hba->max_pwr_info.is_valid) { |
Subhash Jadavani | 9e1e8a7 | 2018-10-16 14:29:41 +0530 | [diff] [blame] | 7898 | /* |
| 7899 | * Set the right value to bRefClkFreq before attempting to |
| 7900 | * switch to HS gears. |
| 7901 | */ |
| 7902 | if (hba->dev_ref_clk_freq != REF_CLK_FREQ_INVAL) |
| 7903 | ufshcd_set_dev_ref_clk(hba); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 7904 | ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info); |
Dov Levenglick | 8643ae6 | 2016-10-17 17:10:14 -0700 | [diff] [blame] | 7905 | if (ret) { |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 7906 | dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n", |
| 7907 | __func__, ret); |
Dov Levenglick | 8643ae6 | 2016-10-17 17:10:14 -0700 | [diff] [blame] | 7908 | goto out; |
| 7909 | } |
Can Guo | 6a9df81 | 2020-02-11 21:38:28 -0800 | [diff] [blame] | 7910 | ufshcd_print_pwr_info(hba); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 7911 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7912 | |
Can Guo | e89860f | 2020-03-26 02:25:41 -0700 | [diff] [blame] | 7913 | /* |
| 7914 | * bActiveICCLevel is volatile for UFS device (as per latest v2.1 spec) |
| 7915 | * and for removable UFS card as well, hence always set the parameter. |
| 7916 | * Note: Error handler may issue the device reset hence resetting |
| 7917 | * bActiveICCLevel as well so it is always safe to set this here. |
| 7918 | */ |
| 7919 | ufshcd_set_active_icc_lvl(hba); |
| 7920 | |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 7921 | ufshcd_wb_config(hba); |
Can Guo | 71d848b | 2019-11-14 22:09:26 -0800 | [diff] [blame] | 7922 | /* Enable Auto-Hibernate if configured */ |
| 7923 | ufshcd_auto_hibern8_enable(hba); |
| 7924 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 7925 | out: |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 7926 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 7927 | if (ret) |
| 7928 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 7929 | else if (hba->ufshcd_state == UFSHCD_STATE_RESET) |
| 7930 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
| 7931 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7932 | |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7933 | trace_ufshcd_init(dev_name(hba->dev), ret, |
| 7934 | ktime_to_us(ktime_sub(ktime_get(), start)), |
Subhash Jadavani | 73eba2b | 2017-01-10 16:48:25 -0800 | [diff] [blame] | 7935 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7936 | return ret; |
| 7937 | } |
| 7938 | |
| 7939 | /** |
| 7940 | * ufshcd_async_scan - asynchronous execution for probing hba |
| 7941 | * @data: data pointer to pass to this function |
| 7942 | * @cookie: cookie data |
| 7943 | */ |
| 7944 | static void ufshcd_async_scan(void *data, async_cookie_t cookie) |
| 7945 | { |
| 7946 | struct ufs_hba *hba = (struct ufs_hba *)data; |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7947 | int ret; |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7948 | |
Can Guo | 9cd20d3 | 2021-01-13 19:13:28 -0800 | [diff] [blame] | 7949 | down(&hba->host_sem); |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7950 | /* Initialize hba, detect and initialize UFS device */ |
| 7951 | ret = ufshcd_probe_hba(hba, true); |
Can Guo | 9cd20d3 | 2021-01-13 19:13:28 -0800 | [diff] [blame] | 7952 | up(&hba->host_sem); |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7953 | if (ret) |
| 7954 | goto out; |
| 7955 | |
| 7956 | /* Probe and add UFS logical units */ |
| 7957 | ret = ufshcd_add_lus(hba); |
| 7958 | out: |
| 7959 | /* |
| 7960 | * If we failed to initialize the device or the device is not |
| 7961 | * present, turn off the power/clocks etc. |
| 7962 | */ |
| 7963 | if (ret) { |
| 7964 | pm_runtime_put_sync(hba->dev); |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7965 | ufshcd_hba_exit(hba); |
Jaegeuk Kim | 4f3e900 | 2020-11-17 08:58:35 -0800 | [diff] [blame] | 7966 | } else { |
| 7967 | ufshcd_clear_ua_wluns(hba); |
Bean Huo | 1b9e214 | 2020-01-20 14:08:15 +0100 | [diff] [blame] | 7968 | } |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7969 | } |
| 7970 | |
Stanislav Nijnikov | d829fc8 | 2018-02-15 14:14:09 +0200 | [diff] [blame] | 7971 | static const struct attribute_group *ufshcd_driver_groups[] = { |
| 7972 | &ufs_sysfs_unit_descriptor_group, |
Stanislav Nijnikov | ec92b59 | 2018-02-15 14:14:11 +0200 | [diff] [blame] | 7973 | &ufs_sysfs_lun_attributes_group, |
Stanislav Nijnikov | d829fc8 | 2018-02-15 14:14:09 +0200 | [diff] [blame] | 7974 | NULL, |
| 7975 | }; |
| 7976 | |
Stanley Chu | 90b8491 | 2020-05-09 17:37:13 +0800 | [diff] [blame] | 7977 | static struct ufs_hba_variant_params ufs_hba_vps = { |
| 7978 | .hba_enable_delay_us = 1000, |
Stanley Chu | d14734ae | 2020-05-09 17:37:15 +0800 | [diff] [blame] | 7979 | .wb_flush_threshold = UFS_WB_BUF_REMAIN_PERCENT(40), |
Stanley Chu | 90b8491 | 2020-05-09 17:37:13 +0800 | [diff] [blame] | 7980 | .devfreq_profile.polling_ms = 100, |
| 7981 | .devfreq_profile.target = ufshcd_devfreq_target, |
| 7982 | .devfreq_profile.get_dev_status = ufshcd_devfreq_get_dev_status, |
| 7983 | .ondemand_data.upthreshold = 70, |
| 7984 | .ondemand_data.downdifferential = 5, |
| 7985 | }; |
| 7986 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7987 | static struct scsi_host_template ufshcd_driver_template = { |
| 7988 | .module = THIS_MODULE, |
| 7989 | .name = UFSHCD, |
| 7990 | .proc_name = UFSHCD, |
| 7991 | .queuecommand = ufshcd_queuecommand, |
| 7992 | .slave_alloc = ufshcd_slave_alloc, |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 7993 | .slave_configure = ufshcd_slave_configure, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7994 | .slave_destroy = ufshcd_slave_destroy, |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 7995 | .change_queue_depth = ufshcd_change_queue_depth, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7996 | .eh_abort_handler = ufshcd_abort, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 7997 | .eh_device_reset_handler = ufshcd_eh_device_reset_handler, |
| 7998 | .eh_host_reset_handler = ufshcd_eh_host_reset_handler, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7999 | .this_id = -1, |
| 8000 | .sg_tablesize = SG_ALL, |
| 8001 | .cmd_per_lun = UFSHCD_CMD_PER_LUN, |
| 8002 | .can_queue = UFSHCD_CAN_QUEUE, |
Christoph Hellwig | 552a990 | 2019-06-17 14:19:55 +0200 | [diff] [blame] | 8003 | .max_segment_size = PRDT_DATA_BYTE_COUNT_MAX, |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8004 | .max_host_blocked = 1, |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 8005 | .track_queue_depth = 1, |
Stanislav Nijnikov | d829fc8 | 2018-02-15 14:14:09 +0200 | [diff] [blame] | 8006 | .sdev_groups = ufshcd_driver_groups, |
Christoph Hellwig | 4af14d1 | 2018-12-13 16:17:09 +0100 | [diff] [blame] | 8007 | .dma_boundary = PAGE_SIZE - 1, |
Stanley Chu | 49615ba | 2019-09-16 23:56:50 +0800 | [diff] [blame] | 8008 | .rpm_autosuspend_delay = RPM_AUTOSUSPEND_DELAY_MS, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8009 | }; |
| 8010 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8011 | static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg, |
| 8012 | int ua) |
| 8013 | { |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 8014 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8015 | |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 8016 | if (!vreg) |
| 8017 | return 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8018 | |
Stanley Chu | 0487fff | 2019-03-28 17:16:25 +0800 | [diff] [blame] | 8019 | /* |
| 8020 | * "set_load" operation shall be required on those regulators |
| 8021 | * which specifically configured current limitation. Otherwise |
| 8022 | * zero max_uA may cause unexpected behavior when regulator is |
| 8023 | * enabled or set as high power mode. |
| 8024 | */ |
| 8025 | if (!vreg->max_uA) |
| 8026 | return 0; |
| 8027 | |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 8028 | ret = regulator_set_load(vreg->reg, ua); |
| 8029 | if (ret < 0) { |
| 8030 | dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n", |
| 8031 | __func__, vreg->name, ua, ret); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8032 | } |
| 8033 | |
| 8034 | return ret; |
| 8035 | } |
| 8036 | |
| 8037 | static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba, |
| 8038 | struct ufs_vreg *vreg) |
| 8039 | { |
Marc Gonzalez | 7306798 | 2019-02-27 11:41:45 +0100 | [diff] [blame] | 8040 | 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] | 8041 | } |
| 8042 | |
| 8043 | static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba, |
| 8044 | struct ufs_vreg *vreg) |
| 8045 | { |
Adrian Hunter | 7c7cfdc | 2019-08-14 15:59:50 +0300 | [diff] [blame] | 8046 | if (!vreg) |
| 8047 | return 0; |
| 8048 | |
Marc Gonzalez | 7306798 | 2019-02-27 11:41:45 +0100 | [diff] [blame] | 8049 | return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8050 | } |
| 8051 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8052 | static int ufshcd_config_vreg(struct device *dev, |
| 8053 | struct ufs_vreg *vreg, bool on) |
| 8054 | { |
| 8055 | int ret = 0; |
Gustavo A. R. Silva | 7275359 | 2017-11-20 08:12:29 -0600 | [diff] [blame] | 8056 | struct regulator *reg; |
| 8057 | const char *name; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8058 | int min_uV, uA_load; |
| 8059 | |
| 8060 | BUG_ON(!vreg); |
| 8061 | |
Gustavo A. R. Silva | 7275359 | 2017-11-20 08:12:29 -0600 | [diff] [blame] | 8062 | reg = vreg->reg; |
| 8063 | name = vreg->name; |
| 8064 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8065 | if (regulator_count_voltages(reg) > 0) { |
Asutosh Das | 90d88f4 | 2020-02-10 19:40:45 -0800 | [diff] [blame] | 8066 | uA_load = on ? vreg->max_uA : 0; |
| 8067 | ret = ufshcd_config_vreg_load(dev, vreg, uA_load); |
| 8068 | if (ret) |
| 8069 | goto out; |
| 8070 | |
Stanley Chu | 3b141e8 | 2019-03-28 17:16:24 +0800 | [diff] [blame] | 8071 | if (vreg->min_uV && vreg->max_uV) { |
| 8072 | min_uV = on ? vreg->min_uV : 0; |
| 8073 | ret = regulator_set_voltage(reg, min_uV, vreg->max_uV); |
Bean Huo | b000862 | 2020-08-14 11:50:34 +0200 | [diff] [blame] | 8074 | if (ret) |
Stanley Chu | 3b141e8 | 2019-03-28 17:16:24 +0800 | [diff] [blame] | 8075 | dev_err(dev, |
| 8076 | "%s: %s set voltage failed, err=%d\n", |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8077 | __func__, name, ret); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8078 | } |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8079 | } |
| 8080 | out: |
| 8081 | return ret; |
| 8082 | } |
| 8083 | |
| 8084 | static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 8085 | { |
| 8086 | int ret = 0; |
| 8087 | |
Marc Gonzalez | 7306798 | 2019-02-27 11:41:45 +0100 | [diff] [blame] | 8088 | if (!vreg || vreg->enabled) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8089 | goto out; |
| 8090 | |
| 8091 | ret = ufshcd_config_vreg(dev, vreg, true); |
| 8092 | if (!ret) |
| 8093 | ret = regulator_enable(vreg->reg); |
| 8094 | |
| 8095 | if (!ret) |
| 8096 | vreg->enabled = true; |
| 8097 | else |
| 8098 | dev_err(dev, "%s: %s enable failed, err=%d\n", |
| 8099 | __func__, vreg->name, ret); |
| 8100 | out: |
| 8101 | return ret; |
| 8102 | } |
| 8103 | |
| 8104 | static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 8105 | { |
| 8106 | int ret = 0; |
| 8107 | |
Stanley Chu | f8162ac | 2020-12-07 13:49:54 +0800 | [diff] [blame] | 8108 | if (!vreg || !vreg->enabled || vreg->always_on) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8109 | goto out; |
| 8110 | |
| 8111 | ret = regulator_disable(vreg->reg); |
| 8112 | |
| 8113 | if (!ret) { |
| 8114 | /* ignore errors on applying disable config */ |
| 8115 | ufshcd_config_vreg(dev, vreg, false); |
| 8116 | vreg->enabled = false; |
| 8117 | } else { |
| 8118 | dev_err(dev, "%s: %s disable failed, err=%d\n", |
| 8119 | __func__, vreg->name, ret); |
| 8120 | } |
| 8121 | out: |
| 8122 | return ret; |
| 8123 | } |
| 8124 | |
| 8125 | static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on) |
| 8126 | { |
| 8127 | int ret = 0; |
| 8128 | struct device *dev = hba->dev; |
| 8129 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 8130 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8131 | ret = ufshcd_toggle_vreg(dev, info->vcc, on); |
| 8132 | if (ret) |
| 8133 | goto out; |
| 8134 | |
| 8135 | ret = ufshcd_toggle_vreg(dev, info->vccq, on); |
| 8136 | if (ret) |
| 8137 | goto out; |
| 8138 | |
| 8139 | ret = ufshcd_toggle_vreg(dev, info->vccq2, on); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8140 | |
| 8141 | out: |
| 8142 | if (ret) { |
| 8143 | ufshcd_toggle_vreg(dev, info->vccq2, false); |
| 8144 | ufshcd_toggle_vreg(dev, info->vccq, false); |
| 8145 | ufshcd_toggle_vreg(dev, info->vcc, false); |
| 8146 | } |
| 8147 | return ret; |
| 8148 | } |
| 8149 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 8150 | static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on) |
| 8151 | { |
| 8152 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 8153 | |
Zeng Guangyue | 60b7b82 | 2019-03-30 17:03:13 +0800 | [diff] [blame] | 8154 | return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on); |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 8155 | } |
| 8156 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8157 | static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 8158 | { |
| 8159 | int ret = 0; |
| 8160 | |
| 8161 | if (!vreg) |
| 8162 | goto out; |
| 8163 | |
| 8164 | vreg->reg = devm_regulator_get(dev, vreg->name); |
| 8165 | if (IS_ERR(vreg->reg)) { |
| 8166 | ret = PTR_ERR(vreg->reg); |
| 8167 | dev_err(dev, "%s: %s get failed, err=%d\n", |
| 8168 | __func__, vreg->name, ret); |
| 8169 | } |
| 8170 | out: |
| 8171 | return ret; |
| 8172 | } |
| 8173 | |
| 8174 | static int ufshcd_init_vreg(struct ufs_hba *hba) |
| 8175 | { |
| 8176 | int ret = 0; |
| 8177 | struct device *dev = hba->dev; |
| 8178 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 8179 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8180 | ret = ufshcd_get_vreg(dev, info->vcc); |
| 8181 | if (ret) |
| 8182 | goto out; |
| 8183 | |
| 8184 | ret = ufshcd_get_vreg(dev, info->vccq); |
Bean Huo | b000862 | 2020-08-14 11:50:34 +0200 | [diff] [blame] | 8185 | if (!ret) |
| 8186 | ret = ufshcd_get_vreg(dev, info->vccq2); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8187 | out: |
| 8188 | return ret; |
| 8189 | } |
| 8190 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 8191 | static int ufshcd_init_hba_vreg(struct ufs_hba *hba) |
| 8192 | { |
| 8193 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 8194 | |
| 8195 | if (info) |
| 8196 | return ufshcd_get_vreg(hba->dev, info->vdd_hba); |
| 8197 | |
| 8198 | return 0; |
| 8199 | } |
| 8200 | |
Can Guo | 81309c2 | 2020-11-25 18:01:00 -0800 | [diff] [blame] | 8201 | static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on) |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8202 | { |
| 8203 | int ret = 0; |
| 8204 | struct ufs_clk_info *clki; |
| 8205 | struct list_head *head = &hba->clk_list_head; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8206 | unsigned long flags; |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 8207 | ktime_t start = ktime_get(); |
| 8208 | bool clk_state_changed = false; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8209 | |
Szymon Mielczarek | 566ec9a | 2017-06-05 11:36:54 +0300 | [diff] [blame] | 8210 | if (list_empty(head)) |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8211 | goto out; |
| 8212 | |
Can Guo | 38f3242 | 2020-02-10 19:40:47 -0800 | [diff] [blame] | 8213 | ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE); |
| 8214 | if (ret) |
| 8215 | return ret; |
Subhash Jadavani | 1e879e8 | 2016-10-06 21:48:22 -0700 | [diff] [blame] | 8216 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8217 | list_for_each_entry(clki, head, list) { |
| 8218 | if (!IS_ERR_OR_NULL(clki->clk)) { |
Can Guo | 81309c2 | 2020-11-25 18:01:00 -0800 | [diff] [blame] | 8219 | /* |
| 8220 | * Don't disable clocks which are needed |
| 8221 | * to keep the link active. |
| 8222 | */ |
| 8223 | if (ufshcd_is_link_active(hba) && |
| 8224 | clki->keep_link_active) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8225 | continue; |
| 8226 | |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 8227 | clk_state_changed = on ^ clki->enabled; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8228 | if (on && !clki->enabled) { |
| 8229 | ret = clk_prepare_enable(clki->clk); |
| 8230 | if (ret) { |
| 8231 | dev_err(hba->dev, "%s: %s prepare enable failed, %d\n", |
| 8232 | __func__, clki->name, ret); |
| 8233 | goto out; |
| 8234 | } |
| 8235 | } else if (!on && clki->enabled) { |
| 8236 | clk_disable_unprepare(clki->clk); |
| 8237 | } |
| 8238 | clki->enabled = on; |
| 8239 | dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__, |
| 8240 | clki->name, on ? "en" : "dis"); |
| 8241 | } |
| 8242 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8243 | |
Can Guo | 38f3242 | 2020-02-10 19:40:47 -0800 | [diff] [blame] | 8244 | ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE); |
| 8245 | if (ret) |
| 8246 | return ret; |
Subhash Jadavani | 1e879e8 | 2016-10-06 21:48:22 -0700 | [diff] [blame] | 8247 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8248 | out: |
| 8249 | if (ret) { |
| 8250 | list_for_each_entry(clki, head, list) { |
| 8251 | if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled) |
| 8252 | clk_disable_unprepare(clki->clk); |
| 8253 | } |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8254 | } else if (!ret && on) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8255 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 8256 | hba->clk_gating.state = CLKS_ON; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8257 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 8258 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8259 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8260 | } |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8261 | |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 8262 | if (clk_state_changed) |
| 8263 | trace_ufshcd_profile_clk_gating(dev_name(hba->dev), |
| 8264 | (on ? "on" : "off"), |
| 8265 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8266 | return ret; |
| 8267 | } |
| 8268 | |
| 8269 | static int ufshcd_init_clocks(struct ufs_hba *hba) |
| 8270 | { |
| 8271 | int ret = 0; |
| 8272 | struct ufs_clk_info *clki; |
| 8273 | struct device *dev = hba->dev; |
| 8274 | struct list_head *head = &hba->clk_list_head; |
| 8275 | |
Szymon Mielczarek | 566ec9a | 2017-06-05 11:36:54 +0300 | [diff] [blame] | 8276 | if (list_empty(head)) |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8277 | goto out; |
| 8278 | |
| 8279 | list_for_each_entry(clki, head, list) { |
| 8280 | if (!clki->name) |
| 8281 | continue; |
| 8282 | |
| 8283 | clki->clk = devm_clk_get(dev, clki->name); |
| 8284 | if (IS_ERR(clki->clk)) { |
| 8285 | ret = PTR_ERR(clki->clk); |
| 8286 | dev_err(dev, "%s: %s clk get failed, %d\n", |
| 8287 | __func__, clki->name, ret); |
| 8288 | goto out; |
| 8289 | } |
| 8290 | |
Subhash Jadavani | 9e1e8a7 | 2018-10-16 14:29:41 +0530 | [diff] [blame] | 8291 | /* |
| 8292 | * Parse device ref clk freq as per device tree "ref_clk". |
| 8293 | * Default dev_ref_clk_freq is set to REF_CLK_FREQ_INVAL |
| 8294 | * in ufshcd_alloc_host(). |
| 8295 | */ |
| 8296 | if (!strcmp(clki->name, "ref_clk")) |
| 8297 | ufshcd_parse_dev_ref_clk_freq(hba, clki->clk); |
| 8298 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8299 | if (clki->max_freq) { |
| 8300 | ret = clk_set_rate(clki->clk, clki->max_freq); |
| 8301 | if (ret) { |
| 8302 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 8303 | __func__, clki->name, |
| 8304 | clki->max_freq, ret); |
| 8305 | goto out; |
| 8306 | } |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 8307 | clki->curr_freq = clki->max_freq; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8308 | } |
| 8309 | dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__, |
| 8310 | clki->name, clk_get_rate(clki->clk)); |
| 8311 | } |
| 8312 | out: |
| 8313 | return ret; |
| 8314 | } |
| 8315 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8316 | static int ufshcd_variant_hba_init(struct ufs_hba *hba) |
| 8317 | { |
| 8318 | int err = 0; |
| 8319 | |
| 8320 | if (!hba->vops) |
| 8321 | goto out; |
| 8322 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 8323 | err = ufshcd_vops_init(hba); |
| 8324 | if (err) |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8325 | dev_err(hba->dev, "%s: variant %s init failed err %d\n", |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 8326 | __func__, ufshcd_get_var_name(hba), err); |
Stanley Chu | ade921a | 2020-12-05 20:00:38 +0800 | [diff] [blame] | 8327 | out: |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8328 | return err; |
| 8329 | } |
| 8330 | |
| 8331 | static void ufshcd_variant_hba_exit(struct ufs_hba *hba) |
| 8332 | { |
| 8333 | if (!hba->vops) |
| 8334 | return; |
| 8335 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 8336 | ufshcd_vops_exit(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8337 | } |
| 8338 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8339 | static int ufshcd_hba_init(struct ufs_hba *hba) |
| 8340 | { |
| 8341 | int err; |
| 8342 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 8343 | /* |
| 8344 | * Handle host controller power separately from the UFS device power |
| 8345 | * rails as it will help controlling the UFS host controller power |
| 8346 | * collapse easily which is different than UFS device power collapse. |
| 8347 | * Also, enable the host controller power before we go ahead with rest |
| 8348 | * of the initialization here. |
| 8349 | */ |
| 8350 | err = ufshcd_init_hba_vreg(hba); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8351 | if (err) |
| 8352 | goto out; |
| 8353 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 8354 | err = ufshcd_setup_hba_vreg(hba, true); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8355 | if (err) |
| 8356 | goto out; |
| 8357 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 8358 | err = ufshcd_init_clocks(hba); |
| 8359 | if (err) |
| 8360 | goto out_disable_hba_vreg; |
| 8361 | |
| 8362 | err = ufshcd_setup_clocks(hba, true); |
| 8363 | if (err) |
| 8364 | goto out_disable_hba_vreg; |
| 8365 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8366 | err = ufshcd_init_vreg(hba); |
| 8367 | if (err) |
| 8368 | goto out_disable_clks; |
| 8369 | |
| 8370 | err = ufshcd_setup_vreg(hba, true); |
| 8371 | if (err) |
| 8372 | goto out_disable_clks; |
| 8373 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8374 | err = ufshcd_variant_hba_init(hba); |
| 8375 | if (err) |
| 8376 | goto out_disable_vreg; |
| 8377 | |
Adrian Hunter | b6cacaf | 2021-01-07 09:25:38 +0200 | [diff] [blame] | 8378 | ufs_debugfs_hba_init(hba); |
| 8379 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 8380 | hba->is_powered = true; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8381 | goto out; |
| 8382 | |
| 8383 | out_disable_vreg: |
| 8384 | ufshcd_setup_vreg(hba, false); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8385 | out_disable_clks: |
| 8386 | ufshcd_setup_clocks(hba, false); |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 8387 | out_disable_hba_vreg: |
| 8388 | ufshcd_setup_hba_vreg(hba, false); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8389 | out: |
| 8390 | return err; |
| 8391 | } |
| 8392 | |
| 8393 | static void ufshcd_hba_exit(struct ufs_hba *hba) |
| 8394 | { |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 8395 | if (hba->is_powered) { |
Can Guo | 4543d9d | 2021-01-20 02:04:22 -0800 | [diff] [blame] | 8396 | ufshcd_exit_clk_scaling(hba); |
| 8397 | ufshcd_exit_clk_gating(hba); |
| 8398 | if (hba->eh_wq) |
| 8399 | destroy_workqueue(hba->eh_wq); |
Adrian Hunter | b6cacaf | 2021-01-07 09:25:38 +0200 | [diff] [blame] | 8400 | ufs_debugfs_hba_exit(hba); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 8401 | ufshcd_variant_hba_exit(hba); |
| 8402 | ufshcd_setup_vreg(hba, false); |
| 8403 | ufshcd_setup_clocks(hba, false); |
| 8404 | ufshcd_setup_hba_vreg(hba, false); |
| 8405 | hba->is_powered = false; |
Bean Huo | 0975006 | 2020-01-20 14:08:14 +0100 | [diff] [blame] | 8406 | ufs_put_device_desc(hba); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 8407 | } |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8408 | } |
| 8409 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8410 | static int |
| 8411 | ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8412 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8413 | unsigned char cmd[6] = {REQUEST_SENSE, |
| 8414 | 0, |
| 8415 | 0, |
| 8416 | 0, |
Avri Altman | 09a5a24 | 2018-11-22 20:04:56 +0200 | [diff] [blame] | 8417 | UFS_SENSE_SIZE, |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8418 | 0}; |
| 8419 | char *buffer; |
| 8420 | int ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8421 | |
Avri Altman | 09a5a24 | 2018-11-22 20:04:56 +0200 | [diff] [blame] | 8422 | buffer = kzalloc(UFS_SENSE_SIZE, GFP_KERNEL); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8423 | if (!buffer) { |
| 8424 | ret = -ENOMEM; |
| 8425 | goto out; |
| 8426 | } |
| 8427 | |
Christoph Hellwig | fcbfffe | 2017-02-23 16:02:37 +0100 | [diff] [blame] | 8428 | ret = scsi_execute(sdp, cmd, DMA_FROM_DEVICE, buffer, |
Avri Altman | 09a5a24 | 2018-11-22 20:04:56 +0200 | [diff] [blame] | 8429 | UFS_SENSE_SIZE, NULL, NULL, |
Christoph Hellwig | fcbfffe | 2017-02-23 16:02:37 +0100 | [diff] [blame] | 8430 | msecs_to_jiffies(1000), 3, 0, RQF_PM, NULL); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8431 | if (ret) |
| 8432 | pr_err("%s: failed with err %d\n", __func__, ret); |
| 8433 | |
| 8434 | kfree(buffer); |
| 8435 | out: |
| 8436 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8437 | } |
| 8438 | |
| 8439 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8440 | * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device |
| 8441 | * power mode |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8442 | * @hba: per adapter instance |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8443 | * @pwr_mode: device power mode to set |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8444 | * |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8445 | * Returns 0 if requested power mode is set successfully |
| 8446 | * Returns non-zero if failed to set the requested power mode |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8447 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8448 | static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba, |
| 8449 | enum ufs_dev_pwr_mode pwr_mode) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8450 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8451 | unsigned char cmd[6] = { START_STOP }; |
| 8452 | struct scsi_sense_hdr sshdr; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 8453 | struct scsi_device *sdp; |
| 8454 | unsigned long flags; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8455 | int ret; |
| 8456 | |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 8457 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 8458 | sdp = hba->sdev_ufs_device; |
| 8459 | if (sdp) { |
| 8460 | ret = scsi_device_get(sdp); |
| 8461 | if (!ret && !scsi_device_online(sdp)) { |
| 8462 | ret = -ENODEV; |
| 8463 | scsi_device_put(sdp); |
| 8464 | } |
| 8465 | } else { |
| 8466 | ret = -ENODEV; |
| 8467 | } |
| 8468 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 8469 | |
| 8470 | if (ret) |
| 8471 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8472 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8473 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8474 | * If scsi commands fail, the scsi mid-layer schedules scsi error- |
| 8475 | * handling, which would wait for host to be resumed. Since we know |
| 8476 | * we are functional while we are here, skip host resume in error |
| 8477 | * handling context. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8478 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8479 | hba->host->eh_noresume = 1; |
Randall Huang | 1918651 | 2020-11-30 20:14:02 -0800 | [diff] [blame] | 8480 | ufshcd_clear_ua_wluns(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8481 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8482 | cmd[4] = pwr_mode << 4; |
| 8483 | |
| 8484 | /* |
| 8485 | * Current function would be generally called from the power management |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 8486 | * 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] | 8487 | * already suspended childs. |
| 8488 | */ |
Christoph Hellwig | fcbfffe | 2017-02-23 16:02:37 +0100 | [diff] [blame] | 8489 | ret = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr, |
| 8490 | START_STOP_TIMEOUT, 0, 0, RQF_PM, NULL); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8491 | if (ret) { |
| 8492 | sdev_printk(KERN_WARNING, sdp, |
Hannes Reinecke | ef61329 | 2014-10-24 14:27:00 +0200 | [diff] [blame] | 8493 | "START_STOP failed for power mode: %d, result %x\n", |
| 8494 | pwr_mode, ret); |
Johannes Thumshirn | c65be1a | 2018-06-25 13:20:58 +0200 | [diff] [blame] | 8495 | if (driver_byte(ret) == DRIVER_SENSE) |
Hannes Reinecke | 2104551 | 2015-01-08 07:43:46 +0100 | [diff] [blame] | 8496 | scsi_print_sense_hdr(sdp, NULL, &sshdr); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8497 | } |
| 8498 | |
| 8499 | if (!ret) |
| 8500 | hba->curr_dev_pwr_mode = pwr_mode; |
Randall Huang | 1918651 | 2020-11-30 20:14:02 -0800 | [diff] [blame] | 8501 | |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 8502 | scsi_device_put(sdp); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8503 | hba->host->eh_noresume = 0; |
| 8504 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8505 | } |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8506 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8507 | static int ufshcd_link_state_transition(struct ufs_hba *hba, |
| 8508 | enum uic_link_state req_link_state, |
| 8509 | int check_for_bkops) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8510 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8511 | int ret = 0; |
| 8512 | |
| 8513 | if (req_link_state == hba->uic_link_state) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8514 | return 0; |
| 8515 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8516 | if (req_link_state == UIC_LINK_HIBERN8_STATE) { |
| 8517 | ret = ufshcd_uic_hibern8_enter(hba); |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 8518 | if (!ret) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8519 | ufshcd_set_link_hibern8(hba); |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 8520 | } else { |
| 8521 | dev_err(hba->dev, "%s: hibern8 enter failed %d\n", |
| 8522 | __func__, ret); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8523 | goto out; |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 8524 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8525 | } |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8526 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8527 | * If autobkops is enabled, link can't be turned off because |
Adrian Hunter | fe1d4c2 | 2020-11-03 16:14:02 +0200 | [diff] [blame] | 8528 | * turning off the link would also turn off the device, except in the |
| 8529 | * case of DeepSleep where the device is expected to remain powered. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8530 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8531 | else if ((req_link_state == UIC_LINK_OFF_STATE) && |
Dan Carpenter | dc30c9e | 2019-12-13 13:49:35 +0300 | [diff] [blame] | 8532 | (!check_for_bkops || !hba->auto_bkops_enabled)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8533 | /* |
Yaniv Gardi | f3099fb | 2016-03-10 17:37:17 +0200 | [diff] [blame] | 8534 | * Let's make sure that link is in low power mode, we are doing |
| 8535 | * this currently by putting the link in Hibern8. Otherway to |
| 8536 | * put the link in low power mode is to send the DME end point |
| 8537 | * to device and then send the DME reset command to local |
| 8538 | * unipro. But putting the link in hibern8 is much faster. |
Adrian Hunter | fe1d4c2 | 2020-11-03 16:14:02 +0200 | [diff] [blame] | 8539 | * |
| 8540 | * Note also that putting the link in Hibern8 is a requirement |
| 8541 | * for entering DeepSleep. |
Yaniv Gardi | f3099fb | 2016-03-10 17:37:17 +0200 | [diff] [blame] | 8542 | */ |
| 8543 | ret = ufshcd_uic_hibern8_enter(hba); |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 8544 | if (ret) { |
| 8545 | dev_err(hba->dev, "%s: hibern8 enter failed %d\n", |
| 8546 | __func__, ret); |
Yaniv Gardi | f3099fb | 2016-03-10 17:37:17 +0200 | [diff] [blame] | 8547 | goto out; |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 8548 | } |
Yaniv Gardi | f3099fb | 2016-03-10 17:37:17 +0200 | [diff] [blame] | 8549 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8550 | * Change controller state to "reset state" which |
| 8551 | * should also put the link in off/reset state |
| 8552 | */ |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 8553 | ufshcd_hba_stop(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8554 | /* |
| 8555 | * TODO: Check if we need any delay to make sure that |
| 8556 | * controller is reset |
| 8557 | */ |
| 8558 | ufshcd_set_link_off(hba); |
| 8559 | } |
| 8560 | |
| 8561 | out: |
| 8562 | return ret; |
| 8563 | } |
| 8564 | |
| 8565 | static void ufshcd_vreg_set_lpm(struct ufs_hba *hba) |
| 8566 | { |
Stanley Chu | c4df6ee | 2020-07-29 13:18:39 +0800 | [diff] [blame] | 8567 | bool vcc_off = false; |
| 8568 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8569 | /* |
Yaniv Gardi | b799fdf | 2016-03-10 17:37:18 +0200 | [diff] [blame] | 8570 | * It seems some UFS devices may keep drawing more than sleep current |
| 8571 | * (atleast for 500us) from UFS rails (especially from VCCQ rail). |
| 8572 | * To avoid this situation, add 2ms delay before putting these UFS |
| 8573 | * rails in LPM mode. |
| 8574 | */ |
| 8575 | if (!ufshcd_is_link_active(hba) && |
| 8576 | hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM) |
| 8577 | usleep_range(2000, 2100); |
| 8578 | |
| 8579 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8580 | * If UFS device is either in UFS_Sleep turn off VCC rail to save some |
| 8581 | * power. |
| 8582 | * |
| 8583 | * If UFS device and link is in OFF state, all power supplies (VCC, |
| 8584 | * VCCQ, VCCQ2) can be turned off if power on write protect is not |
| 8585 | * required. If UFS link is inactive (Hibern8 or OFF state) and device |
| 8586 | * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode. |
| 8587 | * |
| 8588 | * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway |
| 8589 | * in low power state which would save some power. |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 8590 | * |
| 8591 | * If Write Booster is enabled and the device needs to flush the WB |
| 8592 | * buffer OR if bkops status is urgent for WB, keep Vcc on. |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8593 | */ |
| 8594 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) && |
| 8595 | !hba->dev_info.is_lu_power_on_wp) { |
| 8596 | ufshcd_setup_vreg(hba, false); |
Stanley Chu | c4df6ee | 2020-07-29 13:18:39 +0800 | [diff] [blame] | 8597 | vcc_off = true; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8598 | } else if (!ufshcd_is_ufs_dev_active(hba)) { |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 8599 | ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false); |
Stanley Chu | c4df6ee | 2020-07-29 13:18:39 +0800 | [diff] [blame] | 8600 | vcc_off = true; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8601 | if (!ufshcd_is_link_active(hba)) { |
| 8602 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq); |
| 8603 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2); |
| 8604 | } |
| 8605 | } |
Stanley Chu | c4df6ee | 2020-07-29 13:18:39 +0800 | [diff] [blame] | 8606 | |
| 8607 | /* |
| 8608 | * Some UFS devices require delay after VCC power rail is turned-off. |
| 8609 | */ |
| 8610 | if (vcc_off && hba->vreg_info.vcc && |
| 8611 | hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_AFTER_LPM) |
| 8612 | usleep_range(5000, 5100); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8613 | } |
| 8614 | |
| 8615 | static int ufshcd_vreg_set_hpm(struct ufs_hba *hba) |
| 8616 | { |
| 8617 | int ret = 0; |
| 8618 | |
| 8619 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) && |
| 8620 | !hba->dev_info.is_lu_power_on_wp) { |
| 8621 | ret = ufshcd_setup_vreg(hba, true); |
| 8622 | } else if (!ufshcd_is_ufs_dev_active(hba)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8623 | if (!ret && !ufshcd_is_link_active(hba)) { |
| 8624 | ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq); |
| 8625 | if (ret) |
| 8626 | goto vcc_disable; |
| 8627 | ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2); |
| 8628 | if (ret) |
| 8629 | goto vccq_lpm; |
| 8630 | } |
Subhash Jadavani | 69d72ac | 2016-10-27 17:26:24 -0700 | [diff] [blame] | 8631 | ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8632 | } |
| 8633 | goto out; |
| 8634 | |
| 8635 | vccq_lpm: |
| 8636 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq); |
| 8637 | vcc_disable: |
| 8638 | ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false); |
| 8639 | out: |
| 8640 | return ret; |
| 8641 | } |
| 8642 | |
| 8643 | static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba) |
| 8644 | { |
Can Guo | dd7143e | 2020-10-27 12:10:36 -0700 | [diff] [blame] | 8645 | if (ufshcd_is_link_off(hba) || ufshcd_can_aggressive_pc(hba)) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8646 | ufshcd_setup_hba_vreg(hba, false); |
| 8647 | } |
| 8648 | |
| 8649 | static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba) |
| 8650 | { |
Can Guo | dd7143e | 2020-10-27 12:10:36 -0700 | [diff] [blame] | 8651 | if (ufshcd_is_link_off(hba) || ufshcd_can_aggressive_pc(hba)) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8652 | ufshcd_setup_hba_vreg(hba, true); |
| 8653 | } |
| 8654 | |
| 8655 | /** |
| 8656 | * ufshcd_suspend - helper function for suspend operations |
| 8657 | * @hba: per adapter instance |
| 8658 | * @pm_op: desired low power operation type |
| 8659 | * |
| 8660 | * This function will try to put the UFS device and link into low power |
| 8661 | * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl" |
| 8662 | * (System PM level). |
| 8663 | * |
| 8664 | * If this function is called during shutdown, it will make sure that |
| 8665 | * both UFS device and UFS link is powered off. |
| 8666 | * |
| 8667 | * NOTE: UFS device & link must be active before we enter in this function. |
| 8668 | * |
| 8669 | * Returns 0 for success and non-zero for failure |
| 8670 | */ |
| 8671 | static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op) |
| 8672 | { |
| 8673 | int ret = 0; |
Adrian Hunter | fe1d4c2 | 2020-11-03 16:14:02 +0200 | [diff] [blame] | 8674 | int check_for_bkops; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8675 | enum ufs_pm_level pm_lvl; |
| 8676 | enum ufs_dev_pwr_mode req_dev_pwr_mode; |
| 8677 | enum uic_link_state req_link_state; |
| 8678 | |
| 8679 | hba->pm_op_in_progress = 1; |
| 8680 | if (!ufshcd_is_shutdown_pm(pm_op)) { |
| 8681 | pm_lvl = ufshcd_is_runtime_pm(pm_op) ? |
| 8682 | hba->rpm_lvl : hba->spm_lvl; |
| 8683 | req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl); |
| 8684 | req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl); |
| 8685 | } else { |
| 8686 | req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE; |
| 8687 | req_link_state = UIC_LINK_OFF_STATE; |
| 8688 | } |
| 8689 | |
| 8690 | /* |
| 8691 | * If we can't transition into any of the low power modes |
| 8692 | * just gate the clocks. |
| 8693 | */ |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8694 | ufshcd_hold(hba, false); |
| 8695 | hba->clk_gating.is_suspended = true; |
| 8696 | |
Stanley Chu | f9a7fa3 | 2021-01-20 23:01:40 +0800 | [diff] [blame] | 8697 | if (hba->clk_scaling.is_enabled) |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 8698 | ufshcd_suspend_clkscaling(hba); |
Stanley Chu | f9a7fa3 | 2021-01-20 23:01:40 +0800 | [diff] [blame] | 8699 | |
Can Guo | 0e9d4ca | 2021-01-20 02:04:21 -0800 | [diff] [blame] | 8700 | down_write(&hba->clk_scaling_lock); |
| 8701 | hba->clk_scaling.is_allowed = false; |
| 8702 | up_write(&hba->clk_scaling_lock); |
Subhash Jadavani | d6fcf81 | 2016-10-27 17:26:09 -0700 | [diff] [blame] | 8703 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8704 | if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE && |
| 8705 | req_link_state == UIC_LINK_ACTIVE_STATE) { |
| 8706 | goto disable_clks; |
| 8707 | } |
| 8708 | |
| 8709 | if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) && |
| 8710 | (req_link_state == hba->uic_link_state)) |
Subhash Jadavani | d6fcf81 | 2016-10-27 17:26:09 -0700 | [diff] [blame] | 8711 | goto enable_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8712 | |
| 8713 | /* UFS device & link must be active before we enter in this function */ |
| 8714 | if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) { |
| 8715 | ret = -EINVAL; |
Subhash Jadavani | d6fcf81 | 2016-10-27 17:26:09 -0700 | [diff] [blame] | 8716 | goto enable_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8717 | } |
| 8718 | |
| 8719 | if (ufshcd_is_runtime_pm(pm_op)) { |
Subhash Jadavani | 374a246 | 2014-09-25 15:32:35 +0300 | [diff] [blame] | 8720 | if (ufshcd_can_autobkops_during_suspend(hba)) { |
| 8721 | /* |
| 8722 | * The device is idle with no requests in the queue, |
| 8723 | * allow background operations if bkops status shows |
| 8724 | * that performance might be impacted. |
| 8725 | */ |
| 8726 | ret = ufshcd_urgent_bkops(hba); |
| 8727 | if (ret) |
| 8728 | goto enable_gating; |
| 8729 | } else { |
| 8730 | /* make sure that auto bkops is disabled */ |
| 8731 | ufshcd_disable_auto_bkops(hba); |
| 8732 | } |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 8733 | /* |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 8734 | * If device needs to do BKOP or WB buffer flush during |
| 8735 | * Hibern8, keep device power mode as "active power mode" |
| 8736 | * and VCC supply. |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 8737 | */ |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 8738 | hba->dev_info.b_rpm_dev_flush_capable = |
| 8739 | hba->auto_bkops_enabled || |
| 8740 | (((req_link_state == UIC_LINK_HIBERN8_STATE) || |
| 8741 | ((req_link_state == UIC_LINK_ACTIVE_STATE) && |
| 8742 | ufshcd_is_auto_hibern8_enabled(hba))) && |
| 8743 | ufshcd_wb_need_flush(hba)); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8744 | } |
| 8745 | |
Kiwoong Kim | 6948a96 | 2020-12-19 15:40:39 +0900 | [diff] [blame] | 8746 | flush_work(&hba->eeh_work); |
| 8747 | |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 8748 | if (req_dev_pwr_mode != hba->curr_dev_pwr_mode) { |
Bean Huo | 939785d | 2020-11-25 19:53:00 +0100 | [diff] [blame] | 8749 | if (!ufshcd_is_runtime_pm(pm_op)) |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 8750 | /* ensure that bkops is disabled */ |
| 8751 | ufshcd_disable_auto_bkops(hba); |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 8752 | |
| 8753 | if (!hba->dev_info.b_rpm_dev_flush_capable) { |
| 8754 | ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode); |
| 8755 | if (ret) |
| 8756 | goto enable_gating; |
| 8757 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8758 | } |
| 8759 | |
Adrian Hunter | fe1d4c2 | 2020-11-03 16:14:02 +0200 | [diff] [blame] | 8760 | /* |
| 8761 | * In the case of DeepSleep, the device is expected to remain powered |
| 8762 | * with the link off, so do not check for bkops. |
| 8763 | */ |
| 8764 | check_for_bkops = !ufshcd_is_ufs_dev_deepsleep(hba); |
| 8765 | ret = ufshcd_link_state_transition(hba, req_link_state, check_for_bkops); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8766 | if (ret) |
| 8767 | goto set_dev_active; |
| 8768 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8769 | disable_clks: |
| 8770 | /* |
| 8771 | * Call vendor specific suspend callback. As these callbacks may access |
| 8772 | * vendor specific host controller register space call them before the |
| 8773 | * host clocks are ON. |
| 8774 | */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 8775 | ret = ufshcd_vops_suspend(hba, pm_op); |
| 8776 | if (ret) |
| 8777 | goto set_link_active; |
Stanley Chu | dcb6cec | 2019-12-07 20:22:00 +0800 | [diff] [blame] | 8778 | /* |
| 8779 | * Disable the host irq as host controller as there won't be any |
| 8780 | * host controller transaction expected till resume. |
| 8781 | */ |
| 8782 | ufshcd_disable_irq(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8783 | |
Can Guo | 81309c2 | 2020-11-25 18:01:00 -0800 | [diff] [blame] | 8784 | ufshcd_setup_clocks(hba, false); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8785 | |
Can Guo | 2dec947 | 2020-08-09 05:15:47 -0700 | [diff] [blame] | 8786 | if (ufshcd_is_clkgating_allowed(hba)) { |
| 8787 | hba->clk_gating.state = CLKS_OFF; |
| 8788 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 8789 | hba->clk_gating.state); |
| 8790 | } |
Stanley Chu | dcb6cec | 2019-12-07 20:22:00 +0800 | [diff] [blame] | 8791 | |
Ziqi Chen | 528db9e | 2021-01-08 18:56:24 +0800 | [diff] [blame] | 8792 | ufshcd_vreg_set_lpm(hba); |
| 8793 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8794 | /* Put the host controller in low power mode if possible */ |
| 8795 | ufshcd_hba_vreg_set_lpm(hba); |
| 8796 | goto out; |
| 8797 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8798 | set_link_active: |
| 8799 | ufshcd_vreg_set_hpm(hba); |
Adrian Hunter | fe1d4c2 | 2020-11-03 16:14:02 +0200 | [diff] [blame] | 8800 | /* |
| 8801 | * Device hardware reset is required to exit DeepSleep. Also, for |
| 8802 | * DeepSleep, the link is off so host reset and restore will be done |
| 8803 | * further below. |
| 8804 | */ |
| 8805 | if (ufshcd_is_ufs_dev_deepsleep(hba)) { |
Stanley Chu | 31a5d9c | 2020-12-08 21:56:35 +0800 | [diff] [blame] | 8806 | ufshcd_device_reset(hba); |
Adrian Hunter | fe1d4c2 | 2020-11-03 16:14:02 +0200 | [diff] [blame] | 8807 | WARN_ON(!ufshcd_is_link_off(hba)); |
| 8808 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8809 | if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba)) |
| 8810 | ufshcd_set_link_active(hba); |
| 8811 | else if (ufshcd_is_link_off(hba)) |
| 8812 | ufshcd_host_reset_and_restore(hba); |
| 8813 | set_dev_active: |
Adrian Hunter | fe1d4c2 | 2020-11-03 16:14:02 +0200 | [diff] [blame] | 8814 | /* Can also get here needing to exit DeepSleep */ |
| 8815 | if (ufshcd_is_ufs_dev_deepsleep(hba)) { |
Stanley Chu | 31a5d9c | 2020-12-08 21:56:35 +0800 | [diff] [blame] | 8816 | ufshcd_device_reset(hba); |
Adrian Hunter | fe1d4c2 | 2020-11-03 16:14:02 +0200 | [diff] [blame] | 8817 | ufshcd_host_reset_and_restore(hba); |
| 8818 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8819 | if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE)) |
| 8820 | ufshcd_disable_auto_bkops(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8821 | enable_gating: |
Can Guo | 0e9d4ca | 2021-01-20 02:04:21 -0800 | [diff] [blame] | 8822 | down_write(&hba->clk_scaling_lock); |
| 8823 | hba->clk_scaling.is_allowed = true; |
| 8824 | up_write(&hba->clk_scaling_lock); |
| 8825 | if (hba->clk_scaling.is_enabled) |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 8826 | ufshcd_resume_clkscaling(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8827 | hba->clk_gating.is_suspended = false; |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 8828 | hba->dev_info.b_rpm_dev_flush_capable = false; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8829 | ufshcd_release(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8830 | out: |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 8831 | if (hba->dev_info.b_rpm_dev_flush_capable) { |
| 8832 | schedule_delayed_work(&hba->rpm_dev_flush_recheck_work, |
| 8833 | msecs_to_jiffies(RPM_DEV_FLUSH_RECHECK_WORK_DELAY_MS)); |
| 8834 | } |
| 8835 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8836 | hba->pm_op_in_progress = 0; |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 8837 | |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 8838 | if (ret) |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 8839 | ufshcd_update_evt_hist(hba, UFS_EVT_SUSPEND_ERR, (u32)ret); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8840 | return ret; |
| 8841 | } |
| 8842 | |
| 8843 | /** |
| 8844 | * ufshcd_resume - helper function for resume operations |
| 8845 | * @hba: per adapter instance |
| 8846 | * @pm_op: runtime PM or system PM |
| 8847 | * |
| 8848 | * This function basically brings the UFS device, UniPro link and controller |
| 8849 | * to active state. |
| 8850 | * |
| 8851 | * Returns 0 for success and non-zero for failure |
| 8852 | */ |
| 8853 | static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op) |
| 8854 | { |
| 8855 | int ret; |
| 8856 | enum uic_link_state old_link_state; |
| 8857 | |
| 8858 | hba->pm_op_in_progress = 1; |
| 8859 | old_link_state = hba->uic_link_state; |
| 8860 | |
| 8861 | ufshcd_hba_vreg_set_hpm(hba); |
Ziqi Chen | 528db9e | 2021-01-08 18:56:24 +0800 | [diff] [blame] | 8862 | ret = ufshcd_vreg_set_hpm(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8863 | if (ret) |
| 8864 | goto out; |
| 8865 | |
Ziqi Chen | 528db9e | 2021-01-08 18:56:24 +0800 | [diff] [blame] | 8866 | /* Make sure clocks are enabled before accessing controller */ |
| 8867 | ret = ufshcd_setup_clocks(hba, true); |
| 8868 | if (ret) |
| 8869 | goto disable_vreg; |
| 8870 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8871 | /* enable the host irq as host controller would be active soon */ |
Can Guo | 5231d38 | 2019-12-05 02:14:46 +0000 | [diff] [blame] | 8872 | ufshcd_enable_irq(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8873 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8874 | /* |
| 8875 | * Call vendor specific resume callback. As these callbacks may access |
| 8876 | * vendor specific host controller register space call them when the |
| 8877 | * host clocks are ON. |
| 8878 | */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 8879 | ret = ufshcd_vops_resume(hba, pm_op); |
| 8880 | if (ret) |
Ziqi Chen | 528db9e | 2021-01-08 18:56:24 +0800 | [diff] [blame] | 8881 | goto disable_irq_and_vops_clks; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8882 | |
Adrian Hunter | fe1d4c2 | 2020-11-03 16:14:02 +0200 | [diff] [blame] | 8883 | /* For DeepSleep, the only supported option is to have the link off */ |
| 8884 | WARN_ON(ufshcd_is_ufs_dev_deepsleep(hba) && !ufshcd_is_link_off(hba)); |
| 8885 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8886 | if (ufshcd_is_link_hibern8(hba)) { |
| 8887 | ret = ufshcd_uic_hibern8_exit(hba); |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 8888 | if (!ret) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8889 | ufshcd_set_link_active(hba); |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 8890 | } else { |
| 8891 | dev_err(hba->dev, "%s: hibern8 exit failed %d\n", |
| 8892 | __func__, ret); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8893 | goto vendor_suspend; |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 8894 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8895 | } else if (ufshcd_is_link_off(hba)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8896 | /* |
Asutosh Das | 089f5b6 | 2020-04-13 23:14:48 -0700 | [diff] [blame] | 8897 | * A full initialization of the host and the device is |
| 8898 | * required since the link was put to off during suspend. |
Adrian Hunter | fe1d4c2 | 2020-11-03 16:14:02 +0200 | [diff] [blame] | 8899 | * Note, in the case of DeepSleep, the device will exit |
| 8900 | * DeepSleep due to device reset. |
Asutosh Das | 089f5b6 | 2020-04-13 23:14:48 -0700 | [diff] [blame] | 8901 | */ |
| 8902 | ret = ufshcd_reset_and_restore(hba); |
| 8903 | /* |
| 8904 | * ufshcd_reset_and_restore() should have already |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8905 | * set the link state as active |
| 8906 | */ |
| 8907 | if (ret || !ufshcd_is_link_active(hba)) |
| 8908 | goto vendor_suspend; |
| 8909 | } |
| 8910 | |
| 8911 | if (!ufshcd_is_ufs_dev_active(hba)) { |
| 8912 | ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE); |
| 8913 | if (ret) |
| 8914 | goto set_old_link_state; |
| 8915 | } |
| 8916 | |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 8917 | if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) |
| 8918 | ufshcd_enable_auto_bkops(hba); |
| 8919 | else |
| 8920 | /* |
| 8921 | * If BKOPs operations are urgently needed at this moment then |
| 8922 | * keep auto-bkops enabled or else disable it. |
| 8923 | */ |
| 8924 | ufshcd_urgent_bkops(hba); |
| 8925 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8926 | hba->clk_gating.is_suspended = false; |
| 8927 | |
Can Guo | 0e9d4ca | 2021-01-20 02:04:21 -0800 | [diff] [blame] | 8928 | down_write(&hba->clk_scaling_lock); |
| 8929 | hba->clk_scaling.is_allowed = true; |
| 8930 | up_write(&hba->clk_scaling_lock); |
| 8931 | if (hba->clk_scaling.is_enabled) |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 8932 | ufshcd_resume_clkscaling(hba); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 8933 | |
Adrian Hunter | ad44837 | 2018-03-20 15:07:38 +0200 | [diff] [blame] | 8934 | /* Enable Auto-Hibernate if configured */ |
| 8935 | ufshcd_auto_hibern8_enable(hba); |
| 8936 | |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 8937 | if (hba->dev_info.b_rpm_dev_flush_capable) { |
| 8938 | hba->dev_info.b_rpm_dev_flush_capable = false; |
| 8939 | cancel_delayed_work(&hba->rpm_dev_flush_recheck_work); |
| 8940 | } |
| 8941 | |
Can Guo | 71d848b | 2019-11-14 22:09:26 -0800 | [diff] [blame] | 8942 | /* Schedule clock gating in case of no access to UFS device yet */ |
| 8943 | ufshcd_release(hba); |
| 8944 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8945 | goto out; |
| 8946 | |
| 8947 | set_old_link_state: |
| 8948 | ufshcd_link_state_transition(hba, old_link_state, 0); |
| 8949 | vendor_suspend: |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 8950 | ufshcd_vops_suspend(hba, pm_op); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8951 | disable_irq_and_vops_clks: |
| 8952 | ufshcd_disable_irq(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8953 | ufshcd_setup_clocks(hba, false); |
Can Guo | 2dec947 | 2020-08-09 05:15:47 -0700 | [diff] [blame] | 8954 | if (ufshcd_is_clkgating_allowed(hba)) { |
| 8955 | hba->clk_gating.state = CLKS_OFF; |
| 8956 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 8957 | hba->clk_gating.state); |
| 8958 | } |
Ziqi Chen | 528db9e | 2021-01-08 18:56:24 +0800 | [diff] [blame] | 8959 | disable_vreg: |
| 8960 | ufshcd_vreg_set_lpm(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8961 | out: |
| 8962 | hba->pm_op_in_progress = 0; |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 8963 | if (ret) |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 8964 | ufshcd_update_evt_hist(hba, UFS_EVT_RESUME_ERR, (u32)ret); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8965 | return ret; |
| 8966 | } |
| 8967 | |
| 8968 | /** |
| 8969 | * ufshcd_system_suspend - system suspend routine |
| 8970 | * @hba: per adapter instance |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8971 | * |
| 8972 | * Check the description of ufshcd_suspend() function for more details. |
| 8973 | * |
| 8974 | * Returns 0 for success and non-zero for failure |
| 8975 | */ |
| 8976 | int ufshcd_system_suspend(struct ufs_hba *hba) |
| 8977 | { |
| 8978 | int ret = 0; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8979 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8980 | |
Can Guo | fb7afe2 | 2021-01-13 19:13:27 -0800 | [diff] [blame] | 8981 | if (!hba) { |
| 8982 | early_suspend = true; |
| 8983 | return 0; |
| 8984 | } |
| 8985 | |
Can Guo | 9cd20d3 | 2021-01-13 19:13:28 -0800 | [diff] [blame] | 8986 | down(&hba->host_sem); |
Can Guo | fb7afe2 | 2021-01-13 19:13:27 -0800 | [diff] [blame] | 8987 | |
| 8988 | if (!hba->is_powered) |
Dolev Raviv | 233b594 | 2014-10-23 13:25:14 +0300 | [diff] [blame] | 8989 | return 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8990 | |
subhashj@codeaurora.org | 0b25773 | 2016-11-23 16:33:08 -0800 | [diff] [blame] | 8991 | if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) == |
| 8992 | hba->curr_dev_pwr_mode) && |
| 8993 | (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) == |
Stanley Chu | 1d53864 | 2020-12-22 15:29:04 +0800 | [diff] [blame] | 8994 | hba->uic_link_state) && |
| 8995 | !hba->dev_info.b_rpm_dev_flush_capable) |
subhashj@codeaurora.org | 0b25773 | 2016-11-23 16:33:08 -0800 | [diff] [blame] | 8996 | goto out; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8997 | |
subhashj@codeaurora.org | 0b25773 | 2016-11-23 16:33:08 -0800 | [diff] [blame] | 8998 | if (pm_runtime_suspended(hba->dev)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8999 | /* |
| 9000 | * UFS device and/or UFS link low power states during runtime |
| 9001 | * suspend seems to be different than what is expected during |
| 9002 | * system suspend. Hence runtime resume the devic & link and |
| 9003 | * let the system suspend low power states to take effect. |
| 9004 | * TODO: If resume takes longer time, we might have optimize |
| 9005 | * it in future by not resuming everything if possible. |
| 9006 | */ |
| 9007 | ret = ufshcd_runtime_resume(hba); |
| 9008 | if (ret) |
| 9009 | goto out; |
| 9010 | } |
| 9011 | |
| 9012 | ret = ufshcd_suspend(hba, UFS_SYSTEM_PM); |
| 9013 | out: |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 9014 | trace_ufshcd_system_suspend(dev_name(hba->dev), ret, |
| 9015 | ktime_to_us(ktime_sub(ktime_get(), start)), |
Subhash Jadavani | 73eba2b | 2017-01-10 16:48:25 -0800 | [diff] [blame] | 9016 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
Dolev Raviv | e785060 | 2014-09-25 15:32:36 +0300 | [diff] [blame] | 9017 | if (!ret) |
| 9018 | hba->is_sys_suspended = true; |
Can Guo | 88a92d6 | 2020-12-02 04:04:01 -0800 | [diff] [blame] | 9019 | else |
Can Guo | 9cd20d3 | 2021-01-13 19:13:28 -0800 | [diff] [blame] | 9020 | up(&hba->host_sem); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9021 | return ret; |
| 9022 | } |
| 9023 | EXPORT_SYMBOL(ufshcd_system_suspend); |
| 9024 | |
| 9025 | /** |
| 9026 | * ufshcd_system_resume - system resume routine |
| 9027 | * @hba: per adapter instance |
| 9028 | * |
| 9029 | * Returns 0 for success and non-zero for failure |
| 9030 | */ |
| 9031 | |
| 9032 | int ufshcd_system_resume(struct ufs_hba *hba) |
| 9033 | { |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 9034 | int ret = 0; |
| 9035 | ktime_t start = ktime_get(); |
| 9036 | |
Can Guo | fb7afe2 | 2021-01-13 19:13:27 -0800 | [diff] [blame] | 9037 | if (!hba) |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 9038 | return -EINVAL; |
Can Guo | fb7afe2 | 2021-01-13 19:13:27 -0800 | [diff] [blame] | 9039 | |
| 9040 | if (unlikely(early_suspend)) { |
| 9041 | early_suspend = false; |
Can Guo | 9cd20d3 | 2021-01-13 19:13:28 -0800 | [diff] [blame] | 9042 | down(&hba->host_sem); |
Can Guo | 88a92d6 | 2020-12-02 04:04:01 -0800 | [diff] [blame] | 9043 | } |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 9044 | |
| 9045 | if (!hba->is_powered || pm_runtime_suspended(hba->dev)) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9046 | /* |
| 9047 | * Let the runtime resume take care of resuming |
| 9048 | * if runtime suspended. |
| 9049 | */ |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 9050 | goto out; |
| 9051 | else |
| 9052 | ret = ufshcd_resume(hba, UFS_SYSTEM_PM); |
| 9053 | out: |
| 9054 | trace_ufshcd_system_resume(dev_name(hba->dev), ret, |
| 9055 | ktime_to_us(ktime_sub(ktime_get(), start)), |
Subhash Jadavani | 73eba2b | 2017-01-10 16:48:25 -0800 | [diff] [blame] | 9056 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
Stanley Chu | ce9e7bc | 2019-01-07 22:19:34 +0800 | [diff] [blame] | 9057 | if (!ret) |
| 9058 | hba->is_sys_suspended = false; |
Can Guo | 9cd20d3 | 2021-01-13 19:13:28 -0800 | [diff] [blame] | 9059 | up(&hba->host_sem); |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 9060 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9061 | } |
| 9062 | EXPORT_SYMBOL(ufshcd_system_resume); |
| 9063 | |
| 9064 | /** |
| 9065 | * ufshcd_runtime_suspend - runtime suspend routine |
| 9066 | * @hba: per adapter instance |
| 9067 | * |
| 9068 | * Check the description of ufshcd_suspend() function for more details. |
| 9069 | * |
| 9070 | * Returns 0 for success and non-zero for failure |
| 9071 | */ |
| 9072 | int ufshcd_runtime_suspend(struct ufs_hba *hba) |
| 9073 | { |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 9074 | int ret = 0; |
| 9075 | ktime_t start = ktime_get(); |
| 9076 | |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 9077 | if (!hba) |
| 9078 | return -EINVAL; |
| 9079 | |
| 9080 | if (!hba->is_powered) |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 9081 | goto out; |
| 9082 | else |
| 9083 | ret = ufshcd_suspend(hba, UFS_RUNTIME_PM); |
| 9084 | out: |
| 9085 | trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret, |
| 9086 | ktime_to_us(ktime_sub(ktime_get(), start)), |
Subhash Jadavani | 73eba2b | 2017-01-10 16:48:25 -0800 | [diff] [blame] | 9087 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 9088 | return ret; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 9089 | } |
| 9090 | EXPORT_SYMBOL(ufshcd_runtime_suspend); |
| 9091 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9092 | /** |
| 9093 | * ufshcd_runtime_resume - runtime resume routine |
| 9094 | * @hba: per adapter instance |
| 9095 | * |
| 9096 | * This function basically brings the UFS device, UniPro link and controller |
| 9097 | * to active state. Following operations are done in this function: |
| 9098 | * |
| 9099 | * 1. Turn on all the controller related clocks |
| 9100 | * 2. Bring the UniPro link out of Hibernate state |
| 9101 | * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device |
| 9102 | * to active state. |
| 9103 | * 4. If auto-bkops is enabled on the device, disable it. |
| 9104 | * |
| 9105 | * So following would be the possible power state after this function return |
| 9106 | * successfully: |
| 9107 | * S1: UFS device in Active state with VCC rail ON |
| 9108 | * UniPro link in Active state |
| 9109 | * All the UFS/UniPro controller clocks are ON |
| 9110 | * |
| 9111 | * Returns 0 for success and non-zero for failure |
| 9112 | */ |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 9113 | int ufshcd_runtime_resume(struct ufs_hba *hba) |
| 9114 | { |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 9115 | int ret = 0; |
| 9116 | ktime_t start = ktime_get(); |
| 9117 | |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 9118 | if (!hba) |
| 9119 | return -EINVAL; |
| 9120 | |
| 9121 | if (!hba->is_powered) |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 9122 | goto out; |
| 9123 | else |
| 9124 | ret = ufshcd_resume(hba, UFS_RUNTIME_PM); |
| 9125 | out: |
| 9126 | trace_ufshcd_runtime_resume(dev_name(hba->dev), ret, |
| 9127 | ktime_to_us(ktime_sub(ktime_get(), start)), |
Subhash Jadavani | 73eba2b | 2017-01-10 16:48:25 -0800 | [diff] [blame] | 9128 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 9129 | return ret; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 9130 | } |
| 9131 | EXPORT_SYMBOL(ufshcd_runtime_resume); |
| 9132 | |
| 9133 | int ufshcd_runtime_idle(struct ufs_hba *hba) |
| 9134 | { |
| 9135 | return 0; |
| 9136 | } |
| 9137 | EXPORT_SYMBOL(ufshcd_runtime_idle); |
| 9138 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9139 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9140 | * ufshcd_shutdown - shutdown routine |
| 9141 | * @hba: per adapter instance |
| 9142 | * |
| 9143 | * This function would power off both UFS device and UFS link. |
| 9144 | * |
| 9145 | * Returns 0 always to allow force shutdown even in case of errors. |
| 9146 | */ |
| 9147 | int ufshcd_shutdown(struct ufs_hba *hba) |
| 9148 | { |
| 9149 | int ret = 0; |
| 9150 | |
Can Guo | 9cd20d3 | 2021-01-13 19:13:28 -0800 | [diff] [blame] | 9151 | down(&hba->host_sem); |
| 9152 | hba->shutting_down = true; |
| 9153 | up(&hba->host_sem); |
| 9154 | |
Stanley Chu | f51913e | 2019-09-18 12:20:38 +0800 | [diff] [blame] | 9155 | if (!hba->is_powered) |
| 9156 | goto out; |
| 9157 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9158 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba)) |
| 9159 | goto out; |
| 9160 | |
Stanley Chu | e92643d | 2020-11-19 14:29:16 +0800 | [diff] [blame] | 9161 | pm_runtime_get_sync(hba->dev); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9162 | |
| 9163 | ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM); |
| 9164 | out: |
| 9165 | if (ret) |
| 9166 | dev_err(hba->dev, "%s failed, err %d\n", __func__, ret); |
Can Guo | 88a92d6 | 2020-12-02 04:04:01 -0800 | [diff] [blame] | 9167 | hba->is_powered = false; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9168 | /* allow force shutdown even in case of errors */ |
| 9169 | return 0; |
| 9170 | } |
| 9171 | EXPORT_SYMBOL(ufshcd_shutdown); |
| 9172 | |
| 9173 | /** |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9174 | * ufshcd_remove - de-allocate SCSI host and host memory space |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9175 | * data structure memory |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 9176 | * @hba: per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9177 | */ |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9178 | void ufshcd_remove(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9179 | { |
Avri Altman | df032bf | 2018-10-07 17:30:35 +0300 | [diff] [blame] | 9180 | ufs_bsg_remove(hba); |
Stanislav Nijnikov | cbb6813 | 2018-02-15 14:14:01 +0200 | [diff] [blame] | 9181 | ufs_sysfs_remove_nodes(hba->dev); |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 9182 | blk_cleanup_queue(hba->tmf_queue); |
| 9183 | blk_mq_free_tag_set(&hba->tmf_tag_set); |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 9184 | blk_cleanup_queue(hba->cmd_queue); |
Akinobu Mita | cfdf9c9 | 2013-07-30 00:36:03 +0530 | [diff] [blame] | 9185 | scsi_remove_host(hba->host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9186 | /* disable interrupts */ |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 9187 | ufshcd_disable_intr(hba, hba->intr_mask); |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 9188 | ufshcd_hba_stop(hba); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 9189 | ufshcd_hba_exit(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9190 | } |
| 9191 | EXPORT_SYMBOL_GPL(ufshcd_remove); |
| 9192 | |
| 9193 | /** |
Yaniv Gardi | 47555a5 | 2015-10-28 13:15:49 +0200 | [diff] [blame] | 9194 | * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA) |
| 9195 | * @hba: pointer to Host Bus Adapter (HBA) |
| 9196 | */ |
| 9197 | void ufshcd_dealloc_host(struct ufs_hba *hba) |
| 9198 | { |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 9199 | ufshcd_crypto_destroy_keyslot_manager(hba); |
Yaniv Gardi | 47555a5 | 2015-10-28 13:15:49 +0200 | [diff] [blame] | 9200 | scsi_host_put(hba->host); |
| 9201 | } |
| 9202 | EXPORT_SYMBOL_GPL(ufshcd_dealloc_host); |
| 9203 | |
| 9204 | /** |
Akinobu Mita | ca3d7bf | 2014-07-13 21:24:46 +0900 | [diff] [blame] | 9205 | * ufshcd_set_dma_mask - Set dma mask based on the controller |
| 9206 | * addressing capability |
| 9207 | * @hba: per adapter instance |
| 9208 | * |
| 9209 | * Returns 0 for success, non-zero for failure |
| 9210 | */ |
| 9211 | static int ufshcd_set_dma_mask(struct ufs_hba *hba) |
| 9212 | { |
| 9213 | if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) { |
| 9214 | if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64))) |
| 9215 | return 0; |
| 9216 | } |
| 9217 | return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32)); |
| 9218 | } |
| 9219 | |
| 9220 | /** |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 9221 | * ufshcd_alloc_host - allocate Host Bus Adapter (HBA) |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9222 | * @dev: pointer to device handle |
| 9223 | * @hba_handle: driver private handle |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9224 | * Returns 0 on success, non-zero value on failure |
| 9225 | */ |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 9226 | int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9227 | { |
| 9228 | struct Scsi_Host *host; |
| 9229 | struct ufs_hba *hba; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 9230 | int err = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9231 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9232 | if (!dev) { |
| 9233 | dev_err(dev, |
| 9234 | "Invalid memory reference for dev is NULL\n"); |
| 9235 | err = -ENODEV; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9236 | goto out_error; |
| 9237 | } |
| 9238 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9239 | host = scsi_host_alloc(&ufshcd_driver_template, |
| 9240 | sizeof(struct ufs_hba)); |
| 9241 | if (!host) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9242 | dev_err(dev, "scsi_host_alloc failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9243 | err = -ENOMEM; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9244 | goto out_error; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9245 | } |
| 9246 | hba = shost_priv(host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9247 | hba->host = host; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9248 | hba->dev = dev; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 9249 | *hba_handle = hba; |
Subhash Jadavani | 9e1e8a7 | 2018-10-16 14:29:41 +0530 | [diff] [blame] | 9250 | hba->dev_ref_clk_freq = REF_CLK_FREQ_INVAL; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 9251 | |
Szymon Mielczarek | 566ec9a | 2017-06-05 11:36:54 +0300 | [diff] [blame] | 9252 | INIT_LIST_HEAD(&hba->clk_list_head); |
| 9253 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 9254 | out_error: |
| 9255 | return err; |
| 9256 | } |
| 9257 | EXPORT_SYMBOL(ufshcd_alloc_host); |
| 9258 | |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 9259 | /* This function exists because blk_mq_alloc_tag_set() requires this. */ |
| 9260 | static blk_status_t ufshcd_queue_tmf(struct blk_mq_hw_ctx *hctx, |
| 9261 | const struct blk_mq_queue_data *qd) |
| 9262 | { |
| 9263 | WARN_ON_ONCE(true); |
| 9264 | return BLK_STS_NOTSUPP; |
| 9265 | } |
| 9266 | |
| 9267 | static const struct blk_mq_ops ufshcd_tmf_ops = { |
| 9268 | .queue_rq = ufshcd_queue_tmf, |
| 9269 | }; |
| 9270 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 9271 | /** |
| 9272 | * ufshcd_init - Driver initialization routine |
| 9273 | * @hba: per-adapter instance |
| 9274 | * @mmio_base: base register address |
| 9275 | * @irq: Interrupt line of device |
| 9276 | * Returns 0 on success, non-zero value on failure |
| 9277 | */ |
| 9278 | int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq) |
| 9279 | { |
| 9280 | int err; |
| 9281 | struct Scsi_Host *host = hba->host; |
| 9282 | struct device *dev = hba->dev; |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 9283 | char eh_wq_name[sizeof("ufs_eh_wq_00")]; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 9284 | |
| 9285 | if (!mmio_base) { |
| 9286 | dev_err(hba->dev, |
| 9287 | "Invalid memory reference for mmio_base is NULL\n"); |
| 9288 | err = -ENODEV; |
| 9289 | goto out_error; |
| 9290 | } |
| 9291 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9292 | hba->mmio_base = mmio_base; |
| 9293 | hba->irq = irq; |
Stanley Chu | 90b8491 | 2020-05-09 17:37:13 +0800 | [diff] [blame] | 9294 | hba->vps = &ufs_hba_vps; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9295 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 9296 | err = ufshcd_hba_init(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 9297 | if (err) |
| 9298 | goto out_error; |
| 9299 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9300 | /* Read capabilities registers */ |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 9301 | err = ufshcd_hba_capabilities(hba); |
| 9302 | if (err) |
| 9303 | goto out_disable; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9304 | |
| 9305 | /* Get UFS version supported by the controller */ |
| 9306 | hba->ufs_version = ufshcd_get_ufs_version(hba); |
| 9307 | |
Yaniv Gardi | c01848c | 2016-12-05 19:25:02 -0800 | [diff] [blame] | 9308 | if ((hba->ufs_version != UFSHCI_VERSION_10) && |
| 9309 | (hba->ufs_version != UFSHCI_VERSION_11) && |
| 9310 | (hba->ufs_version != UFSHCI_VERSION_20) && |
| 9311 | (hba->ufs_version != UFSHCI_VERSION_21)) |
| 9312 | dev_err(hba->dev, "invalid UFS version 0x%x\n", |
| 9313 | hba->ufs_version); |
| 9314 | |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 9315 | /* Get Interrupt bit mask per version */ |
| 9316 | hba->intr_mask = ufshcd_get_intr_mask(hba); |
| 9317 | |
Akinobu Mita | ca3d7bf | 2014-07-13 21:24:46 +0900 | [diff] [blame] | 9318 | err = ufshcd_set_dma_mask(hba); |
| 9319 | if (err) { |
| 9320 | dev_err(hba->dev, "set dma mask failed\n"); |
| 9321 | goto out_disable; |
| 9322 | } |
| 9323 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9324 | /* Allocate memory for host memory space */ |
| 9325 | err = ufshcd_memory_alloc(hba); |
| 9326 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9327 | dev_err(hba->dev, "Memory allocation failed\n"); |
| 9328 | goto out_disable; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9329 | } |
| 9330 | |
| 9331 | /* Configure LRB */ |
| 9332 | ufshcd_host_memory_configure(hba); |
| 9333 | |
| 9334 | host->can_queue = hba->nutrs; |
| 9335 | host->cmd_per_lun = hba->nutrs; |
| 9336 | host->max_id = UFSHCD_MAX_ID; |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 9337 | host->max_lun = UFS_MAX_LUNS; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9338 | host->max_channel = UFSHCD_MAX_CHANNEL; |
| 9339 | host->unique_id = host->host_no; |
Avri Altman | a851b2b | 2018-10-07 17:30:34 +0300 | [diff] [blame] | 9340 | host->max_cmd_len = UFS_CDB_SIZE; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9341 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 9342 | hba->max_pwr_info.is_valid = false; |
| 9343 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9344 | /* Initialize work queues */ |
Can Guo | 4db7a23 | 2020-08-09 05:15:51 -0700 | [diff] [blame] | 9345 | snprintf(eh_wq_name, sizeof(eh_wq_name), "ufs_eh_wq_%d", |
| 9346 | hba->host->host_no); |
| 9347 | hba->eh_wq = create_singlethread_workqueue(eh_wq_name); |
| 9348 | if (!hba->eh_wq) { |
| 9349 | dev_err(hba->dev, "%s: failed to create eh workqueue\n", |
| 9350 | __func__); |
| 9351 | err = -ENOMEM; |
| 9352 | goto out_disable; |
| 9353 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 9354 | INIT_WORK(&hba->eh_work, ufshcd_err_handler); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 9355 | INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9356 | |
Can Guo | 9cd20d3 | 2021-01-13 19:13:28 -0800 | [diff] [blame] | 9357 | sema_init(&hba->host_sem, 1); |
Can Guo | 88a92d6 | 2020-12-02 04:04:01 -0800 | [diff] [blame] | 9358 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 9359 | /* Initialize UIC command mutex */ |
| 9360 | mutex_init(&hba->uic_cmd_mutex); |
| 9361 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 9362 | /* Initialize mutex for device management commands */ |
| 9363 | mutex_init(&hba->dev_cmd.lock); |
| 9364 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 9365 | init_rwsem(&hba->clk_scaling_lock); |
| 9366 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 9367 | ufshcd_init_clk_gating(hba); |
Yaniv Gardi | 199ef13 | 2016-03-10 17:37:06 +0200 | [diff] [blame] | 9368 | |
Vivek Gautam | eebcc19 | 2018-08-07 23:17:39 +0530 | [diff] [blame] | 9369 | ufshcd_init_clk_scaling(hba); |
| 9370 | |
Yaniv Gardi | 199ef13 | 2016-03-10 17:37:06 +0200 | [diff] [blame] | 9371 | /* |
| 9372 | * In order to avoid any spurious interrupt immediately after |
| 9373 | * registering UFS controller interrupt handler, clear any pending UFS |
| 9374 | * interrupt status and disable all the UFS interrupts. |
| 9375 | */ |
| 9376 | ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS), |
| 9377 | REG_INTERRUPT_STATUS); |
| 9378 | ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE); |
| 9379 | /* |
| 9380 | * Make sure that UFS interrupts are disabled and any pending interrupt |
| 9381 | * status is cleared before registering UFS interrupt handler. |
| 9382 | */ |
| 9383 | mb(); |
| 9384 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9385 | /* IRQ registration */ |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 9386 | err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9387 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9388 | dev_err(hba->dev, "request irq failed\n"); |
Can Guo | 4543d9d | 2021-01-20 02:04:22 -0800 | [diff] [blame] | 9389 | goto out_disable; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9390 | } else { |
| 9391 | hba->is_irq_enabled = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9392 | } |
| 9393 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9394 | err = scsi_add_host(host, hba->dev); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9395 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9396 | dev_err(hba->dev, "scsi_add_host failed\n"); |
Can Guo | 4543d9d | 2021-01-20 02:04:22 -0800 | [diff] [blame] | 9397 | goto out_disable; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9398 | } |
| 9399 | |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 9400 | hba->cmd_queue = blk_mq_init_queue(&hba->host->tag_set); |
| 9401 | if (IS_ERR(hba->cmd_queue)) { |
| 9402 | err = PTR_ERR(hba->cmd_queue); |
| 9403 | goto out_remove_scsi_host; |
| 9404 | } |
| 9405 | |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 9406 | hba->tmf_tag_set = (struct blk_mq_tag_set) { |
| 9407 | .nr_hw_queues = 1, |
| 9408 | .queue_depth = hba->nutmrs, |
| 9409 | .ops = &ufshcd_tmf_ops, |
| 9410 | .flags = BLK_MQ_F_NO_SCHED, |
| 9411 | }; |
| 9412 | err = blk_mq_alloc_tag_set(&hba->tmf_tag_set); |
| 9413 | if (err < 0) |
| 9414 | goto free_cmd_queue; |
| 9415 | hba->tmf_queue = blk_mq_init_queue(&hba->tmf_tag_set); |
| 9416 | if (IS_ERR(hba->tmf_queue)) { |
| 9417 | err = PTR_ERR(hba->tmf_queue); |
| 9418 | goto free_tmf_tag_set; |
| 9419 | } |
| 9420 | |
Bjorn Andersson | d8d9f79 | 2019-08-28 12:17:54 -0700 | [diff] [blame] | 9421 | /* Reset the attached device */ |
Stanley Chu | 31a5d9c | 2020-12-08 21:56:35 +0800 | [diff] [blame] | 9422 | ufshcd_device_reset(hba); |
Bjorn Andersson | d8d9f79 | 2019-08-28 12:17:54 -0700 | [diff] [blame] | 9423 | |
Satya Tangirala | df043c74 | 2020-07-06 20:04:14 +0000 | [diff] [blame] | 9424 | ufshcd_init_crypto(hba); |
| 9425 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 9426 | /* Host controller enable */ |
| 9427 | err = ufshcd_hba_enable(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9428 | if (err) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 9429 | dev_err(hba->dev, "Host controller enable failed\n"); |
Stanley Chu | e965e5e | 2020-12-05 19:58:59 +0800 | [diff] [blame] | 9430 | ufshcd_print_evt_hist(hba); |
Gilad Broner | 6ba6558 | 2017-02-03 16:57:28 -0800 | [diff] [blame] | 9431 | ufshcd_print_host_state(hba); |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 9432 | goto free_tmf_queue; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9433 | } |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 9434 | |
subhashj@codeaurora.org | 0c8f758 | 2016-12-22 18:41:11 -0800 | [diff] [blame] | 9435 | /* |
| 9436 | * Set the default power management level for runtime and system PM. |
| 9437 | * Default power saving mode is to keep UFS link in Hibern8 state |
| 9438 | * and UFS device in sleep state. |
| 9439 | */ |
| 9440 | hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state( |
| 9441 | UFS_SLEEP_PWR_MODE, |
| 9442 | UIC_LINK_HIBERN8_STATE); |
| 9443 | hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state( |
| 9444 | UFS_SLEEP_PWR_MODE, |
| 9445 | UIC_LINK_HIBERN8_STATE); |
| 9446 | |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 9447 | INIT_DELAYED_WORK(&hba->rpm_dev_flush_recheck_work, |
| 9448 | ufshcd_rpm_dev_flush_recheck_work); |
| 9449 | |
Adrian Hunter | ad44837 | 2018-03-20 15:07:38 +0200 | [diff] [blame] | 9450 | /* Set the default auto-hiberate idle timer value to 150 ms */ |
Stanley Chu | f571b37 | 2019-05-21 14:44:53 +0800 | [diff] [blame] | 9451 | if (ufshcd_is_auto_hibern8_supported(hba) && !hba->ahit) { |
Adrian Hunter | ad44837 | 2018-03-20 15:07:38 +0200 | [diff] [blame] | 9452 | hba->ahit = FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, 150) | |
| 9453 | FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, 3); |
| 9454 | } |
| 9455 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 9456 | /* Hold auto suspend until async scan completes */ |
| 9457 | pm_runtime_get_sync(dev); |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 9458 | atomic_set(&hba->scsi_block_reqs_cnt, 0); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9459 | /* |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 9460 | * We are assuming that device wasn't put in sleep/power-down |
| 9461 | * state exclusively during the boot stage before kernel. |
| 9462 | * This assumption helps avoid doing link startup twice during |
| 9463 | * ufshcd_probe_hba(). |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9464 | */ |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 9465 | ufshcd_set_ufs_dev_active(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9466 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 9467 | async_schedule(ufshcd_async_scan, hba); |
Stanislav Nijnikov | cbb6813 | 2018-02-15 14:14:01 +0200 | [diff] [blame] | 9468 | ufs_sysfs_add_nodes(hba->dev); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 9469 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9470 | return 0; |
| 9471 | |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 9472 | free_tmf_queue: |
| 9473 | blk_cleanup_queue(hba->tmf_queue); |
| 9474 | free_tmf_tag_set: |
| 9475 | blk_mq_free_tag_set(&hba->tmf_tag_set); |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 9476 | free_cmd_queue: |
| 9477 | blk_cleanup_queue(hba->cmd_queue); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9478 | out_remove_scsi_host: |
| 9479 | scsi_remove_host(hba->host); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9480 | out_disable: |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9481 | hba->is_irq_enabled = false; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 9482 | ufshcd_hba_exit(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9483 | out_error: |
| 9484 | return err; |
| 9485 | } |
| 9486 | EXPORT_SYMBOL_GPL(ufshcd_init); |
| 9487 | |
Adrian Hunter | b6cacaf | 2021-01-07 09:25:38 +0200 | [diff] [blame] | 9488 | static int __init ufshcd_core_init(void) |
| 9489 | { |
| 9490 | ufs_debugfs_init(); |
| 9491 | return 0; |
| 9492 | } |
| 9493 | |
| 9494 | static void __exit ufshcd_core_exit(void) |
| 9495 | { |
| 9496 | ufs_debugfs_exit(); |
| 9497 | } |
| 9498 | |
| 9499 | module_init(ufshcd_core_init); |
| 9500 | module_exit(ufshcd_core_exit); |
| 9501 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9502 | MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>"); |
| 9503 | MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>"); |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 9504 | MODULE_DESCRIPTION("Generic UFS host controller driver Core"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9505 | MODULE_LICENSE("GPL"); |
| 9506 | MODULE_VERSION(UFSHCD_DRIVER_VERSION); |