Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1 | /* |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 2 | * Universal Flash Storage Host controller driver Core |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3 | * |
| 4 | * This code is based on drivers/scsi/ufs/ufshcd.c |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 5 | * Copyright (C) 2011-2013 Samsung India Software Operations |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 6 | * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7 | * |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8 | * Authors: |
| 9 | * Santosh Yaraganavi <santosh.sy@samsung.com> |
| 10 | * Vinayak Holikatti <h.vinayak@samsung.com> |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 16 | * See the COPYING file in the top-level directory or visit |
| 17 | * <http://www.gnu.org/licenses/gpl-2.0.html> |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 24 | * This program is provided "AS IS" and "WITH ALL FAULTS" and |
| 25 | * without warranty of any kind. You are solely responsible for |
| 26 | * determining the appropriateness of using and distributing |
| 27 | * the program and assume all risks associated with your exercise |
| 28 | * of rights with respect to the program, including but not limited |
| 29 | * to infringement of third party rights, the risks and costs of |
| 30 | * program errors, damage to or loss of data, programs or equipment, |
| 31 | * and unavailability or interruption of operations. Under no |
| 32 | * circumstances will the contributor of this Program be liable for |
| 33 | * any damages of any kind arising from your use or distribution of |
| 34 | * this program. |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 35 | * |
| 36 | * The Linux Foundation chooses to take subject only to the GPLv2 |
| 37 | * license terms, and distributes only under these terms. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 38 | */ |
| 39 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 40 | #include <linux/async.h> |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 41 | #include <linux/devfreq.h> |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 42 | #include <linux/nls.h> |
Yaniv Gardi | 54b879b | 2016-03-10 17:37:05 +0200 | [diff] [blame] | 43 | #include <linux/of.h> |
Adrian Hunter | ad44837 | 2018-03-20 15:07:38 +0200 | [diff] [blame] | 44 | #include <linux/bitfield.h> |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 45 | #include "ufshcd.h" |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 46 | #include "ufs_quirks.h" |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 47 | #include "unipro.h" |
Stanislav Nijnikov | cbb6813 | 2018-02-15 14:14:01 +0200 | [diff] [blame] | 48 | #include "ufs-sysfs.h" |
Avri Altman | df032bf | 2018-10-07 17:30:35 +0300 | [diff] [blame] | 49 | #include "ufs_bsg.h" |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 50 | |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 51 | #define CREATE_TRACE_POINTS |
| 52 | #include <trace/events/ufs.h> |
| 53 | |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 54 | #define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\ |
| 55 | UTP_TASK_REQ_COMPL |\ |
| 56 | UFSHCD_ERROR_MASK) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 57 | /* UIC command timeout, unit: ms */ |
| 58 | #define UIC_CMD_TIMEOUT 500 |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 59 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 60 | /* NOP OUT retries waiting for NOP IN response */ |
| 61 | #define NOP_OUT_RETRIES 10 |
| 62 | /* Timeout after 30 msecs if NOP OUT hangs without response */ |
| 63 | #define NOP_OUT_TIMEOUT 30 /* msecs */ |
| 64 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 65 | /* Query request retries */ |
subhashj@codeaurora.org | 10fe588 | 2016-11-23 16:31:52 -0800 | [diff] [blame] | 66 | #define QUERY_REQ_RETRIES 3 |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 67 | /* Query request timeout */ |
subhashj@codeaurora.org | 10fe588 | 2016-11-23 16:31:52 -0800 | [diff] [blame] | 68 | #define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */ |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 69 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 70 | /* Task management command timeout */ |
| 71 | #define TM_CMD_TIMEOUT 100 /* msecs */ |
| 72 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 73 | /* maximum number of retries for a general UIC command */ |
| 74 | #define UFS_UIC_COMMAND_RETRIES 3 |
| 75 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 76 | /* maximum number of link-startup retries */ |
| 77 | #define DME_LINKSTARTUP_RETRIES 3 |
| 78 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 79 | /* Maximum retries for Hibern8 enter */ |
| 80 | #define UIC_HIBERN8_ENTER_RETRIES 3 |
| 81 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 82 | /* maximum number of reset retries before giving up */ |
| 83 | #define MAX_HOST_RESET_RETRIES 5 |
| 84 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 85 | /* Expose the flag value from utp_upiu_query.value */ |
| 86 | #define MASK_QUERY_UPIU_FLAG_LOC 0xFF |
| 87 | |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 88 | /* Interrupt aggregation default timeout, unit: 40us */ |
| 89 | #define INT_AGGR_DEF_TO 0x02 |
| 90 | |
Stanley Chu | 49615ba | 2019-09-16 23:56:50 +0800 | [diff] [blame] | 91 | /* default delay of autosuspend: 2000 ms */ |
| 92 | #define RPM_AUTOSUSPEND_DELAY_MS 2000 |
| 93 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 94 | #define ufshcd_toggle_vreg(_dev, _vreg, _on) \ |
| 95 | ({ \ |
| 96 | int _ret; \ |
| 97 | if (_on) \ |
| 98 | _ret = ufshcd_enable_vreg(_dev, _vreg); \ |
| 99 | else \ |
| 100 | _ret = ufshcd_disable_vreg(_dev, _vreg); \ |
| 101 | _ret; \ |
| 102 | }) |
| 103 | |
Tomas Winkler | ba80917 | 2018-06-14 11:14:09 +0300 | [diff] [blame] | 104 | #define ufshcd_hex_dump(prefix_str, buf, len) do { \ |
| 105 | size_t __len = (len); \ |
| 106 | print_hex_dump(KERN_ERR, prefix_str, \ |
| 107 | __len > 4 ? DUMP_PREFIX_OFFSET : DUMP_PREFIX_NONE,\ |
| 108 | 16, 4, buf, __len, false); \ |
| 109 | } while (0) |
| 110 | |
| 111 | int ufshcd_dump_regs(struct ufs_hba *hba, size_t offset, size_t len, |
| 112 | const char *prefix) |
| 113 | { |
Marc Gonzalez | d672475 | 2019-01-22 18:29:22 +0100 | [diff] [blame] | 114 | u32 *regs; |
| 115 | size_t pos; |
| 116 | |
| 117 | if (offset % 4 != 0 || len % 4 != 0) /* keep readl happy */ |
| 118 | return -EINVAL; |
Tomas Winkler | ba80917 | 2018-06-14 11:14:09 +0300 | [diff] [blame] | 119 | |
| 120 | regs = kzalloc(len, GFP_KERNEL); |
| 121 | if (!regs) |
| 122 | return -ENOMEM; |
| 123 | |
Marc Gonzalez | d672475 | 2019-01-22 18:29:22 +0100 | [diff] [blame] | 124 | for (pos = 0; pos < len; pos += 4) |
| 125 | regs[pos / 4] = ufshcd_readl(hba, offset + pos); |
| 126 | |
Tomas Winkler | ba80917 | 2018-06-14 11:14:09 +0300 | [diff] [blame] | 127 | ufshcd_hex_dump(prefix, regs, len); |
| 128 | kfree(regs); |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | EXPORT_SYMBOL_GPL(ufshcd_dump_regs); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 133 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 134 | enum { |
| 135 | UFSHCD_MAX_CHANNEL = 0, |
| 136 | UFSHCD_MAX_ID = 1, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 137 | UFSHCD_CMD_PER_LUN = 32, |
| 138 | UFSHCD_CAN_QUEUE = 32, |
| 139 | }; |
| 140 | |
| 141 | /* UFSHCD states */ |
| 142 | enum { |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 143 | UFSHCD_STATE_RESET, |
| 144 | UFSHCD_STATE_ERROR, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 145 | UFSHCD_STATE_OPERATIONAL, |
Zang Leigang | 141f816 | 2016-11-16 11:29:37 +0800 | [diff] [blame] | 146 | UFSHCD_STATE_EH_SCHEDULED, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | /* UFSHCD error handling flags */ |
| 150 | enum { |
| 151 | UFSHCD_EH_IN_PROGRESS = (1 << 0), |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 152 | }; |
| 153 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 154 | /* UFSHCD UIC layer error flags */ |
| 155 | enum { |
| 156 | UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 157 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */ |
| 158 | UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */ |
| 159 | UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */ |
| 160 | UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */ |
| 161 | UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 162 | }; |
| 163 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 164 | #define ufshcd_set_eh_in_progress(h) \ |
Tomohiro Kusumi | 9c490d2 | 2017-03-28 16:49:26 +0300 | [diff] [blame] | 165 | ((h)->eh_flags |= UFSHCD_EH_IN_PROGRESS) |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 166 | #define ufshcd_eh_in_progress(h) \ |
Tomohiro Kusumi | 9c490d2 | 2017-03-28 16:49:26 +0300 | [diff] [blame] | 167 | ((h)->eh_flags & UFSHCD_EH_IN_PROGRESS) |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 168 | #define ufshcd_clear_eh_in_progress(h) \ |
Tomohiro Kusumi | 9c490d2 | 2017-03-28 16:49:26 +0300 | [diff] [blame] | 169 | ((h)->eh_flags &= ~UFSHCD_EH_IN_PROGRESS) |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 170 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 171 | #define ufshcd_set_ufs_dev_active(h) \ |
| 172 | ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE) |
| 173 | #define ufshcd_set_ufs_dev_sleep(h) \ |
| 174 | ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE) |
| 175 | #define ufshcd_set_ufs_dev_poweroff(h) \ |
| 176 | ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE) |
| 177 | #define ufshcd_is_ufs_dev_active(h) \ |
| 178 | ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE) |
| 179 | #define ufshcd_is_ufs_dev_sleep(h) \ |
| 180 | ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE) |
| 181 | #define ufshcd_is_ufs_dev_poweroff(h) \ |
| 182 | ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE) |
| 183 | |
Stanislav Nijnikov | cbb6813 | 2018-02-15 14:14:01 +0200 | [diff] [blame] | 184 | struct ufs_pm_lvl_states ufs_pm_lvl_states[] = { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 185 | {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE}, |
| 186 | {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 187 | {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE}, |
| 188 | {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 189 | {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 190 | {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE}, |
| 191 | }; |
| 192 | |
| 193 | static inline enum ufs_dev_pwr_mode |
| 194 | ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl) |
| 195 | { |
| 196 | return ufs_pm_lvl_states[lvl].dev_state; |
| 197 | } |
| 198 | |
| 199 | static inline enum uic_link_state |
| 200 | ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl) |
| 201 | { |
| 202 | return ufs_pm_lvl_states[lvl].link_state; |
| 203 | } |
| 204 | |
subhashj@codeaurora.org | 0c8f758 | 2016-12-22 18:41:11 -0800 | [diff] [blame] | 205 | static inline enum ufs_pm_level |
| 206 | ufs_get_desired_pm_lvl_for_dev_link_state(enum ufs_dev_pwr_mode dev_state, |
| 207 | enum uic_link_state link_state) |
| 208 | { |
| 209 | enum ufs_pm_level lvl; |
| 210 | |
| 211 | for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) { |
| 212 | if ((ufs_pm_lvl_states[lvl].dev_state == dev_state) && |
| 213 | (ufs_pm_lvl_states[lvl].link_state == link_state)) |
| 214 | return lvl; |
| 215 | } |
| 216 | |
| 217 | /* if no match found, return the level 0 */ |
| 218 | return UFS_PM_LVL_0; |
| 219 | } |
| 220 | |
Subhash Jadavani | 56d4a18 | 2016-12-05 19:25:32 -0800 | [diff] [blame] | 221 | static struct ufs_dev_fix ufs_fixups[] = { |
| 222 | /* UFS cards deviations table */ |
| 223 | UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, |
| 224 | UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM), |
Subhash Jadavani | 56d4a18 | 2016-12-05 19:25:32 -0800 | [diff] [blame] | 225 | UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, |
| 226 | UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS), |
| 227 | UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, |
Subhash Jadavani | 56d4a18 | 2016-12-05 19:25:32 -0800 | [diff] [blame] | 228 | UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE), |
| 229 | UFS_FIX(UFS_VENDOR_TOSHIBA, UFS_ANY_MODEL, |
| 230 | UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM), |
| 231 | UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9C8KBADG", |
| 232 | UFS_DEVICE_QUIRK_PA_TACTIVATE), |
| 233 | UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9D8KBADG", |
| 234 | UFS_DEVICE_QUIRK_PA_TACTIVATE), |
Subhash Jadavani | 56d4a18 | 2016-12-05 19:25:32 -0800 | [diff] [blame] | 235 | UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL, |
| 236 | UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME), |
Wei Li | 8e4829c | 2018-11-08 09:08:29 -0800 | [diff] [blame] | 237 | UFS_FIX(UFS_VENDOR_SKHYNIX, "hB8aL1" /*H28U62301AMR*/, |
| 238 | UFS_DEVICE_QUIRK_HOST_VS_DEBUGSAVECONFIGTIME), |
Subhash Jadavani | 56d4a18 | 2016-12-05 19:25:32 -0800 | [diff] [blame] | 239 | |
| 240 | END_FIX |
| 241 | }; |
| 242 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 243 | static void ufshcd_tmc_handler(struct ufs_hba *hba); |
| 244 | static void ufshcd_async_scan(void *data, async_cookie_t cookie); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 245 | static int ufshcd_reset_and_restore(struct ufs_hba *hba); |
Dolev Raviv | e7d3825 | 2016-12-22 18:40:07 -0800 | [diff] [blame] | 246 | static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 247 | 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] | 248 | static void ufshcd_hba_exit(struct ufs_hba *hba); |
| 249 | static int ufshcd_probe_hba(struct ufs_hba *hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 250 | static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on, |
| 251 | bool skip_ref_clk); |
| 252 | static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on); |
| 253 | static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba); |
| 254 | static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 255 | 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] | 256 | static int ufshcd_host_reset_and_restore(struct ufs_hba *hba); |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 257 | static void ufshcd_resume_clkscaling(struct ufs_hba *hba); |
| 258 | static void ufshcd_suspend_clkscaling(struct ufs_hba *hba); |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 259 | static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba); |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 260 | static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 261 | static irqreturn_t ufshcd_intr(int irq, void *__hba); |
Yaniv Gardi | 874237f | 2015-05-17 18:55:03 +0300 | [diff] [blame] | 262 | static int ufshcd_change_power_mode(struct ufs_hba *hba, |
| 263 | struct ufs_pa_layer_attr *pwr_mode); |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 264 | static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag) |
| 265 | { |
| 266 | return tag >= 0 && tag < hba->nutrs; |
| 267 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 268 | |
| 269 | static inline int ufshcd_enable_irq(struct ufs_hba *hba) |
| 270 | { |
| 271 | int ret = 0; |
| 272 | |
| 273 | if (!hba->is_irq_enabled) { |
| 274 | ret = request_irq(hba->irq, ufshcd_intr, IRQF_SHARED, UFSHCD, |
| 275 | hba); |
| 276 | if (ret) |
| 277 | dev_err(hba->dev, "%s: request_irq failed, ret=%d\n", |
| 278 | __func__, ret); |
| 279 | hba->is_irq_enabled = true; |
| 280 | } |
| 281 | |
| 282 | return ret; |
| 283 | } |
| 284 | |
| 285 | static inline void ufshcd_disable_irq(struct ufs_hba *hba) |
| 286 | { |
| 287 | if (hba->is_irq_enabled) { |
| 288 | free_irq(hba->irq, hba); |
| 289 | hba->is_irq_enabled = false; |
| 290 | } |
| 291 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 292 | |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 293 | static void ufshcd_scsi_unblock_requests(struct ufs_hba *hba) |
| 294 | { |
| 295 | if (atomic_dec_and_test(&hba->scsi_block_reqs_cnt)) |
| 296 | scsi_unblock_requests(hba->host); |
| 297 | } |
| 298 | |
| 299 | static void ufshcd_scsi_block_requests(struct ufs_hba *hba) |
| 300 | { |
| 301 | if (atomic_inc_return(&hba->scsi_block_reqs_cnt) == 1) |
| 302 | scsi_block_requests(hba->host); |
| 303 | } |
| 304 | |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 305 | static void ufshcd_add_cmd_upiu_trace(struct ufs_hba *hba, unsigned int tag, |
| 306 | const char *str) |
| 307 | { |
| 308 | struct utp_upiu_req *rq = hba->lrb[tag].ucd_req_ptr; |
| 309 | |
| 310 | trace_ufshcd_upiu(dev_name(hba->dev), str, &rq->header, &rq->sc.cdb); |
| 311 | } |
| 312 | |
| 313 | static void ufshcd_add_query_upiu_trace(struct ufs_hba *hba, unsigned int tag, |
| 314 | const char *str) |
| 315 | { |
| 316 | struct utp_upiu_req *rq = hba->lrb[tag].ucd_req_ptr; |
| 317 | |
| 318 | trace_ufshcd_upiu(dev_name(hba->dev), str, &rq->header, &rq->qr); |
| 319 | } |
| 320 | |
| 321 | static void ufshcd_add_tm_upiu_trace(struct ufs_hba *hba, unsigned int tag, |
| 322 | const char *str) |
| 323 | { |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 324 | int off = (int)tag - hba->nutrs; |
Christoph Hellwig | 391e388 | 2018-10-07 17:30:32 +0300 | [diff] [blame] | 325 | struct utp_task_req_desc *descp = &hba->utmrdl_base_addr[off]; |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 326 | |
Christoph Hellwig | 391e388 | 2018-10-07 17:30:32 +0300 | [diff] [blame] | 327 | trace_ufshcd_upiu(dev_name(hba->dev), str, &descp->req_header, |
| 328 | &descp->input_param1); |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 329 | } |
| 330 | |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 331 | static void ufshcd_add_command_trace(struct ufs_hba *hba, |
| 332 | unsigned int tag, const char *str) |
| 333 | { |
| 334 | sector_t lba = -1; |
| 335 | u8 opcode = 0; |
| 336 | u32 intr, doorbell; |
Ohad Sharabi | e7c3b37 | 2018-08-05 16:26:23 +0300 | [diff] [blame] | 337 | struct ufshcd_lrb *lrbp = &hba->lrb[tag]; |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 338 | int transfer_len = -1; |
| 339 | |
Ohad Sharabi | e7c3b37 | 2018-08-05 16:26:23 +0300 | [diff] [blame] | 340 | if (!trace_ufshcd_command_enabled()) { |
| 341 | /* trace UPIU W/O tracing command */ |
| 342 | if (lrbp->cmd) |
| 343 | ufshcd_add_cmd_upiu_trace(hba, tag, str); |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 344 | return; |
Ohad Sharabi | e7c3b37 | 2018-08-05 16:26:23 +0300 | [diff] [blame] | 345 | } |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 346 | |
| 347 | if (lrbp->cmd) { /* data phase exists */ |
Ohad Sharabi | e7c3b37 | 2018-08-05 16:26:23 +0300 | [diff] [blame] | 348 | /* trace UPIU also */ |
| 349 | ufshcd_add_cmd_upiu_trace(hba, tag, str); |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 350 | opcode = (u8)(*lrbp->cmd->cmnd); |
| 351 | if ((opcode == READ_10) || (opcode == WRITE_10)) { |
| 352 | /* |
| 353 | * Currently we only fully trace read(10) and write(10) |
| 354 | * commands |
| 355 | */ |
| 356 | if (lrbp->cmd->request && lrbp->cmd->request->bio) |
| 357 | lba = |
| 358 | lrbp->cmd->request->bio->bi_iter.bi_sector; |
| 359 | transfer_len = be32_to_cpu( |
| 360 | lrbp->ucd_req_ptr->sc.exp_data_transfer_len); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | intr = ufshcd_readl(hba, REG_INTERRUPT_STATUS); |
| 365 | doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 366 | trace_ufshcd_command(dev_name(hba->dev), str, tag, |
| 367 | doorbell, transfer_len, intr, lba, opcode); |
| 368 | } |
| 369 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 370 | static void ufshcd_print_clk_freqs(struct ufs_hba *hba) |
| 371 | { |
| 372 | struct ufs_clk_info *clki; |
| 373 | struct list_head *head = &hba->clk_list_head; |
| 374 | |
Szymon Mielczarek | 566ec9a | 2017-06-05 11:36:54 +0300 | [diff] [blame] | 375 | if (list_empty(head)) |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 376 | return; |
| 377 | |
| 378 | list_for_each_entry(clki, head, list) { |
| 379 | if (!IS_ERR_OR_NULL(clki->clk) && clki->min_freq && |
| 380 | clki->max_freq) |
| 381 | dev_err(hba->dev, "clk: %s, rate: %u\n", |
| 382 | clki->name, clki->curr_freq); |
| 383 | } |
| 384 | } |
| 385 | |
Stanley Chu | 48d5b97 | 2019-07-10 21:38:18 +0800 | [diff] [blame] | 386 | static void ufshcd_print_err_hist(struct ufs_hba *hba, |
| 387 | struct ufs_err_reg_hist *err_hist, |
| 388 | char *err_name) |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 389 | { |
| 390 | int i; |
Stanley Chu | 2775264 | 2019-01-28 22:04:26 +0800 | [diff] [blame] | 391 | bool found = false; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 392 | |
Stanley Chu | 48d5b97 | 2019-07-10 21:38:18 +0800 | [diff] [blame] | 393 | for (i = 0; i < UFS_ERR_REG_HIST_LENGTH; i++) { |
| 394 | int p = (i + err_hist->pos) % UFS_ERR_REG_HIST_LENGTH; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 395 | |
| 396 | if (err_hist->reg[p] == 0) |
| 397 | continue; |
Stanley Chu | c5397f1 | 2019-07-10 21:38:20 +0800 | [diff] [blame] | 398 | dev_err(hba->dev, "%s[%d] = 0x%x at %lld us\n", err_name, p, |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 399 | err_hist->reg[p], ktime_to_us(err_hist->tstamp[p])); |
Stanley Chu | 2775264 | 2019-01-28 22:04:26 +0800 | [diff] [blame] | 400 | found = true; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 401 | } |
Stanley Chu | 2775264 | 2019-01-28 22:04:26 +0800 | [diff] [blame] | 402 | |
| 403 | if (!found) |
Stanley Chu | 48d5b97 | 2019-07-10 21:38:18 +0800 | [diff] [blame] | 404 | dev_err(hba->dev, "No record of %s errors\n", err_name); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 405 | } |
| 406 | |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 407 | static void ufshcd_print_host_regs(struct ufs_hba *hba) |
| 408 | { |
Tomas Winkler | ba80917 | 2018-06-14 11:14:09 +0300 | [diff] [blame] | 409 | ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: "); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 410 | dev_err(hba->dev, "hba->ufs_version = 0x%x, hba->capabilities = 0x%x\n", |
| 411 | hba->ufs_version, hba->capabilities); |
| 412 | dev_err(hba->dev, |
| 413 | "hba->outstanding_reqs = 0x%x, hba->outstanding_tasks = 0x%x\n", |
| 414 | (u32)hba->outstanding_reqs, (u32)hba->outstanding_tasks); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 415 | dev_err(hba->dev, |
| 416 | "last_hibern8_exit_tstamp at %lld us, hibern8_exit_cnt = %d\n", |
| 417 | ktime_to_us(hba->ufs_stats.last_hibern8_exit_tstamp), |
| 418 | hba->ufs_stats.hibern8_exit_cnt); |
| 419 | |
Stanley Chu | 48d5b97 | 2019-07-10 21:38:18 +0800 | [diff] [blame] | 420 | ufshcd_print_err_hist(hba, &hba->ufs_stats.pa_err, "pa_err"); |
| 421 | ufshcd_print_err_hist(hba, &hba->ufs_stats.dl_err, "dl_err"); |
| 422 | ufshcd_print_err_hist(hba, &hba->ufs_stats.nl_err, "nl_err"); |
| 423 | ufshcd_print_err_hist(hba, &hba->ufs_stats.tl_err, "tl_err"); |
| 424 | ufshcd_print_err_hist(hba, &hba->ufs_stats.dme_err, "dme_err"); |
Stanley Chu | d3c615b | 2019-07-10 21:38:19 +0800 | [diff] [blame] | 425 | ufshcd_print_err_hist(hba, &hba->ufs_stats.auto_hibern8_err, |
| 426 | "auto_hibern8_err"); |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 427 | ufshcd_print_err_hist(hba, &hba->ufs_stats.fatal_err, "fatal_err"); |
| 428 | ufshcd_print_err_hist(hba, &hba->ufs_stats.link_startup_err, |
| 429 | "link_startup_fail"); |
| 430 | ufshcd_print_err_hist(hba, &hba->ufs_stats.resume_err, "resume_fail"); |
| 431 | ufshcd_print_err_hist(hba, &hba->ufs_stats.suspend_err, |
| 432 | "suspend_fail"); |
| 433 | ufshcd_print_err_hist(hba, &hba->ufs_stats.dev_reset, "dev_reset"); |
| 434 | ufshcd_print_err_hist(hba, &hba->ufs_stats.host_reset, "host_reset"); |
| 435 | ufshcd_print_err_hist(hba, &hba->ufs_stats.task_abort, "task_abort"); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 436 | |
| 437 | ufshcd_print_clk_freqs(hba); |
| 438 | |
| 439 | if (hba->vops && hba->vops->dbg_register_dump) |
| 440 | hba->vops->dbg_register_dump(hba); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | static |
| 444 | void ufshcd_print_trs(struct ufs_hba *hba, unsigned long bitmap, bool pr_prdt) |
| 445 | { |
| 446 | struct ufshcd_lrb *lrbp; |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 447 | int prdt_length; |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 448 | int tag; |
| 449 | |
| 450 | for_each_set_bit(tag, &bitmap, hba->nutrs) { |
| 451 | lrbp = &hba->lrb[tag]; |
| 452 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 453 | dev_err(hba->dev, "UPIU[%d] - issue time %lld us\n", |
| 454 | tag, ktime_to_us(lrbp->issue_time_stamp)); |
Zang Leigang | 0901718 | 2017-09-27 10:06:06 +0800 | [diff] [blame] | 455 | dev_err(hba->dev, "UPIU[%d] - complete time %lld us\n", |
| 456 | tag, ktime_to_us(lrbp->compl_time_stamp)); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 457 | dev_err(hba->dev, |
| 458 | "UPIU[%d] - Transfer Request Descriptor phys@0x%llx\n", |
| 459 | tag, (u64)lrbp->utrd_dma_addr); |
| 460 | |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 461 | ufshcd_hex_dump("UPIU TRD: ", lrbp->utr_descriptor_ptr, |
| 462 | sizeof(struct utp_transfer_req_desc)); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 463 | dev_err(hba->dev, "UPIU[%d] - Request UPIU phys@0x%llx\n", tag, |
| 464 | (u64)lrbp->ucd_req_dma_addr); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 465 | ufshcd_hex_dump("UPIU REQ: ", lrbp->ucd_req_ptr, |
| 466 | sizeof(struct utp_upiu_req)); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 467 | dev_err(hba->dev, "UPIU[%d] - Response UPIU phys@0x%llx\n", tag, |
| 468 | (u64)lrbp->ucd_rsp_dma_addr); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 469 | ufshcd_hex_dump("UPIU RSP: ", lrbp->ucd_rsp_ptr, |
| 470 | sizeof(struct utp_upiu_rsp)); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 471 | |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 472 | prdt_length = le16_to_cpu( |
| 473 | lrbp->utr_descriptor_ptr->prd_table_length); |
| 474 | dev_err(hba->dev, |
| 475 | "UPIU[%d] - PRDT - %d entries phys@0x%llx\n", |
| 476 | tag, prdt_length, |
| 477 | (u64)lrbp->ucd_prdt_dma_addr); |
| 478 | |
| 479 | if (pr_prdt) |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 480 | ufshcd_hex_dump("UPIU PRDT: ", lrbp->ucd_prdt_ptr, |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 481 | sizeof(struct ufshcd_sg_entry) * prdt_length); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
| 485 | static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap) |
| 486 | { |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 487 | int tag; |
| 488 | |
| 489 | for_each_set_bit(tag, &bitmap, hba->nutmrs) { |
Christoph Hellwig | 391e388 | 2018-10-07 17:30:32 +0300 | [diff] [blame] | 490 | struct utp_task_req_desc *tmrdp = &hba->utmrdl_base_addr[tag]; |
| 491 | |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 492 | dev_err(hba->dev, "TM[%d] - Task Management Header\n", tag); |
Christoph Hellwig | 391e388 | 2018-10-07 17:30:32 +0300 | [diff] [blame] | 493 | ufshcd_hex_dump("", tmrdp, sizeof(*tmrdp)); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
Gilad Broner | 6ba6558 | 2017-02-03 16:57:28 -0800 | [diff] [blame] | 497 | static void ufshcd_print_host_state(struct ufs_hba *hba) |
| 498 | { |
| 499 | dev_err(hba->dev, "UFS Host state=%d\n", hba->ufshcd_state); |
| 500 | dev_err(hba->dev, "lrb in use=0x%lx, outstanding reqs=0x%lx tasks=0x%lx\n", |
Zang Leigang | e002e65 | 2017-08-24 10:57:15 +0800 | [diff] [blame] | 501 | hba->lrb_in_use, hba->outstanding_reqs, hba->outstanding_tasks); |
Gilad Broner | 6ba6558 | 2017-02-03 16:57:28 -0800 | [diff] [blame] | 502 | dev_err(hba->dev, "saved_err=0x%x, saved_uic_err=0x%x\n", |
| 503 | hba->saved_err, hba->saved_uic_err); |
| 504 | dev_err(hba->dev, "Device power mode=%d, UIC link state=%d\n", |
| 505 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
| 506 | dev_err(hba->dev, "PM in progress=%d, sys. suspended=%d\n", |
| 507 | hba->pm_op_in_progress, hba->is_sys_suspended); |
| 508 | dev_err(hba->dev, "Auto BKOPS=%d, Host self-block=%d\n", |
| 509 | hba->auto_bkops_enabled, hba->host->host_self_blocked); |
| 510 | dev_err(hba->dev, "Clk gate=%d\n", hba->clk_gating.state); |
| 511 | dev_err(hba->dev, "error handling flags=0x%x, req. abort count=%d\n", |
| 512 | hba->eh_flags, hba->req_abort_count); |
| 513 | dev_err(hba->dev, "Host capabilities=0x%x, caps=0x%x\n", |
| 514 | hba->capabilities, hba->caps); |
| 515 | dev_err(hba->dev, "quirks=0x%x, dev. quirks=0x%x\n", hba->quirks, |
| 516 | hba->dev_quirks); |
| 517 | } |
| 518 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 519 | /** |
| 520 | * ufshcd_print_pwr_info - print power params as saved in hba |
| 521 | * power info |
| 522 | * @hba: per-adapter instance |
| 523 | */ |
| 524 | static void ufshcd_print_pwr_info(struct ufs_hba *hba) |
| 525 | { |
| 526 | static const char * const names[] = { |
| 527 | "INVALID MODE", |
| 528 | "FAST MODE", |
| 529 | "SLOW_MODE", |
| 530 | "INVALID MODE", |
| 531 | "FASTAUTO_MODE", |
| 532 | "SLOWAUTO_MODE", |
| 533 | "INVALID MODE", |
| 534 | }; |
| 535 | |
| 536 | dev_err(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n", |
| 537 | __func__, |
| 538 | hba->pwr_info.gear_rx, hba->pwr_info.gear_tx, |
| 539 | hba->pwr_info.lane_rx, hba->pwr_info.lane_tx, |
| 540 | names[hba->pwr_info.pwr_rx], |
| 541 | names[hba->pwr_info.pwr_tx], |
| 542 | hba->pwr_info.hs_rate); |
| 543 | } |
| 544 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 545 | /* |
| 546 | * ufshcd_wait_for_register - wait for register value to change |
| 547 | * @hba - per-adapter interface |
| 548 | * @reg - mmio register offset |
| 549 | * @mask - mask to apply to read register value |
| 550 | * @val - wait condition |
| 551 | * @interval_us - polling interval in microsecs |
| 552 | * @timeout_ms - timeout in millisecs |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 553 | * @can_sleep - perform sleep or just spin |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 554 | * |
| 555 | * Returns -ETIMEDOUT on error, zero on success |
| 556 | */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 557 | int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask, |
| 558 | u32 val, unsigned long interval_us, |
| 559 | unsigned long timeout_ms, bool can_sleep) |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 560 | { |
| 561 | int err = 0; |
| 562 | unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms); |
| 563 | |
| 564 | /* ignore bits that we don't intend to wait on */ |
| 565 | val = val & mask; |
| 566 | |
| 567 | while ((ufshcd_readl(hba, reg) & mask) != val) { |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 568 | if (can_sleep) |
| 569 | usleep_range(interval_us, interval_us + 50); |
| 570 | else |
| 571 | udelay(interval_us); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 572 | if (time_after(jiffies, timeout)) { |
| 573 | if ((ufshcd_readl(hba, reg) & mask) != val) |
| 574 | err = -ETIMEDOUT; |
| 575 | break; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | return err; |
| 580 | } |
| 581 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 582 | /** |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 583 | * ufshcd_get_intr_mask - Get the interrupt bit mask |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 584 | * @hba: Pointer to adapter instance |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 585 | * |
| 586 | * Returns interrupt bit mask per version |
| 587 | */ |
| 588 | static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba) |
| 589 | { |
Yaniv Gardi | c01848c | 2016-12-05 19:25:02 -0800 | [diff] [blame] | 590 | u32 intr_mask = 0; |
| 591 | |
| 592 | switch (hba->ufs_version) { |
| 593 | case UFSHCI_VERSION_10: |
| 594 | intr_mask = INTERRUPT_MASK_ALL_VER_10; |
| 595 | break; |
Yaniv Gardi | c01848c | 2016-12-05 19:25:02 -0800 | [diff] [blame] | 596 | case UFSHCI_VERSION_11: |
| 597 | case UFSHCI_VERSION_20: |
| 598 | intr_mask = INTERRUPT_MASK_ALL_VER_11; |
| 599 | break; |
Yaniv Gardi | c01848c | 2016-12-05 19:25:02 -0800 | [diff] [blame] | 600 | case UFSHCI_VERSION_21: |
| 601 | default: |
| 602 | intr_mask = INTERRUPT_MASK_ALL_VER_21; |
Tomohiro Kusumi | 031d1e0 | 2017-03-23 12:49:04 +0200 | [diff] [blame] | 603 | break; |
Yaniv Gardi | c01848c | 2016-12-05 19:25:02 -0800 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | return intr_mask; |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 610 | * 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] | 611 | * @hba: Pointer to adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 612 | * |
| 613 | * Returns UFSHCI version supported by the controller |
| 614 | */ |
| 615 | static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba) |
| 616 | { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 617 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION) |
| 618 | return ufshcd_vops_get_ufs_hci_version(hba); |
Yaniv Gardi | 9949e70 | 2015-05-17 18:55:05 +0300 | [diff] [blame] | 619 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 620 | return ufshcd_readl(hba, REG_UFS_VERSION); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | /** |
| 624 | * ufshcd_is_device_present - Check if any device connected to |
| 625 | * the host controller |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 626 | * @hba: pointer to adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 627 | * |
Tomohiro Kusumi | c9e6010 | 2017-03-28 16:49:24 +0300 | [diff] [blame] | 628 | * Returns true if device present, false if no device detected |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 629 | */ |
Tomohiro Kusumi | c9e6010 | 2017-03-28 16:49:24 +0300 | [diff] [blame] | 630 | static inline bool ufshcd_is_device_present(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 631 | { |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 632 | return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & |
Tomohiro Kusumi | c9e6010 | 2017-03-28 16:49:24 +0300 | [diff] [blame] | 633 | DEVICE_PRESENT) ? true : false; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | /** |
| 637 | * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 638 | * @lrbp: pointer to local command reference block |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 639 | * |
| 640 | * This function is used to get the OCS field from UTRD |
| 641 | * Returns the OCS field in the UTRD |
| 642 | */ |
| 643 | static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp) |
| 644 | { |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 645 | 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] | 646 | } |
| 647 | |
| 648 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 649 | * ufshcd_get_tm_free_slot - get a free slot for task management request |
| 650 | * @hba: per adapter instance |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 651 | * @free_slot: pointer to variable with available slot value |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 652 | * |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 653 | * Get a free tag and lock it until ufshcd_put_tm_slot() is called. |
| 654 | * Returns 0 if free slot is not available, else return 1 with tag value |
| 655 | * in @free_slot. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 656 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 657 | static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 658 | { |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 659 | int tag; |
| 660 | bool ret = false; |
| 661 | |
| 662 | if (!free_slot) |
| 663 | goto out; |
| 664 | |
| 665 | do { |
| 666 | tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs); |
| 667 | if (tag >= hba->nutmrs) |
| 668 | goto out; |
| 669 | } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use)); |
| 670 | |
| 671 | *free_slot = tag; |
| 672 | ret = true; |
| 673 | out: |
| 674 | return ret; |
| 675 | } |
| 676 | |
| 677 | static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot) |
| 678 | { |
| 679 | clear_bit_unlock(slot, &hba->tm_slots_in_use); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /** |
| 683 | * ufshcd_utrl_clear - Clear a bit in UTRLCLR register |
| 684 | * @hba: per adapter instance |
| 685 | * @pos: position of the bit to be cleared |
| 686 | */ |
| 687 | static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos) |
| 688 | { |
Alim Akhtar | 1399c5b | 2018-05-06 15:44:15 +0530 | [diff] [blame] | 689 | if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR) |
| 690 | ufshcd_writel(hba, (1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR); |
| 691 | else |
| 692 | ufshcd_writel(hba, ~(1 << pos), |
| 693 | REG_UTP_TRANSFER_REQ_LIST_CLEAR); |
| 694 | } |
| 695 | |
| 696 | /** |
| 697 | * ufshcd_utmrl_clear - Clear a bit in UTRMLCLR register |
| 698 | * @hba: per adapter instance |
| 699 | * @pos: position of the bit to be cleared |
| 700 | */ |
| 701 | static inline void ufshcd_utmrl_clear(struct ufs_hba *hba, u32 pos) |
| 702 | { |
| 703 | if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR) |
| 704 | ufshcd_writel(hba, (1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR); |
| 705 | else |
| 706 | ufshcd_writel(hba, ~(1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | /** |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 710 | * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field |
| 711 | * @hba: per adapter instance |
| 712 | * @tag: position of the bit to be cleared |
| 713 | */ |
| 714 | static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag) |
| 715 | { |
| 716 | __clear_bit(tag, &hba->outstanding_reqs); |
| 717 | } |
| 718 | |
| 719 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 720 | * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY |
| 721 | * @reg: Register value of host controller status |
| 722 | * |
| 723 | * Returns integer, 0 on Success and positive value if failed |
| 724 | */ |
| 725 | static inline int ufshcd_get_lists_status(u32 reg) |
| 726 | { |
Tomohiro Kusumi | 6cf1611 | 2017-04-26 20:28:58 +0300 | [diff] [blame] | 727 | return !((reg & UFSHCD_STATUS_READY) == UFSHCD_STATUS_READY); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | /** |
| 731 | * ufshcd_get_uic_cmd_result - Get the UIC command result |
| 732 | * @hba: Pointer to adapter instance |
| 733 | * |
| 734 | * This function gets the result of UIC command completion |
| 735 | * Returns 0 on success, non zero value on error |
| 736 | */ |
| 737 | static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba) |
| 738 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 739 | return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) & |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 740 | MASK_UIC_COMMAND_RESULT; |
| 741 | } |
| 742 | |
| 743 | /** |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 744 | * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command |
| 745 | * @hba: Pointer to adapter instance |
| 746 | * |
| 747 | * This function gets UIC command argument3 |
| 748 | * Returns 0 on success, non zero value on error |
| 749 | */ |
| 750 | static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba) |
| 751 | { |
| 752 | return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3); |
| 753 | } |
| 754 | |
| 755 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 756 | * ufshcd_get_req_rsp - returns the TR response transaction type |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 757 | * @ucd_rsp_ptr: pointer to response UPIU |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 758 | */ |
| 759 | static inline int |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 760 | ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 761 | { |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 762 | return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | /** |
| 766 | * ufshcd_get_rsp_upiu_result - Get the result from response UPIU |
| 767 | * @ucd_rsp_ptr: pointer to response UPIU |
| 768 | * |
| 769 | * This function gets the response status and scsi_status from response UPIU |
| 770 | * Returns the response result code. |
| 771 | */ |
| 772 | static inline int |
| 773 | ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 774 | { |
| 775 | return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT; |
| 776 | } |
| 777 | |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 778 | /* |
| 779 | * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length |
| 780 | * from response UPIU |
| 781 | * @ucd_rsp_ptr: pointer to response UPIU |
| 782 | * |
| 783 | * Return the data segment length. |
| 784 | */ |
| 785 | static inline unsigned int |
| 786 | ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 787 | { |
| 788 | return be32_to_cpu(ucd_rsp_ptr->header.dword_2) & |
| 789 | MASK_RSP_UPIU_DATA_SEG_LEN; |
| 790 | } |
| 791 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 792 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 793 | * ufshcd_is_exception_event - Check if the device raised an exception event |
| 794 | * @ucd_rsp_ptr: pointer to response UPIU |
| 795 | * |
| 796 | * The function checks if the device raised an exception event indicated in |
| 797 | * the Device Information field of response UPIU. |
| 798 | * |
| 799 | * Returns true if exception is raised, false otherwise. |
| 800 | */ |
| 801 | static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 802 | { |
| 803 | return be32_to_cpu(ucd_rsp_ptr->header.dword_2) & |
| 804 | MASK_RSP_EXCEPTION_EVENT ? true : false; |
| 805 | } |
| 806 | |
| 807 | /** |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 808 | * ufshcd_reset_intr_aggr - Reset interrupt aggregation values. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 809 | * @hba: per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 810 | */ |
| 811 | static inline void |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 812 | ufshcd_reset_intr_aggr(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 813 | { |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 814 | ufshcd_writel(hba, INT_AGGR_ENABLE | |
| 815 | INT_AGGR_COUNTER_AND_TIMER_RESET, |
| 816 | REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
| 817 | } |
| 818 | |
| 819 | /** |
| 820 | * ufshcd_config_intr_aggr - Configure interrupt aggregation values. |
| 821 | * @hba: per adapter instance |
| 822 | * @cnt: Interrupt aggregation counter threshold |
| 823 | * @tmout: Interrupt aggregation timeout value |
| 824 | */ |
| 825 | static inline void |
| 826 | ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout) |
| 827 | { |
| 828 | ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE | |
| 829 | INT_AGGR_COUNTER_THLD_VAL(cnt) | |
| 830 | INT_AGGR_TIMEOUT_VAL(tmout), |
| 831 | REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | /** |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 835 | * ufshcd_disable_intr_aggr - Disables interrupt aggregation. |
| 836 | * @hba: per adapter instance |
| 837 | */ |
| 838 | static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba) |
| 839 | { |
| 840 | ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
| 841 | } |
| 842 | |
| 843 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 844 | * ufshcd_enable_run_stop_reg - Enable run-stop registers, |
| 845 | * When run-stop registers are set to 1, it indicates the |
| 846 | * host controller that it can process the requests |
| 847 | * @hba: per adapter instance |
| 848 | */ |
| 849 | static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba) |
| 850 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 851 | ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT, |
| 852 | REG_UTP_TASK_REQ_LIST_RUN_STOP); |
| 853 | ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT, |
| 854 | REG_UTP_TRANSFER_REQ_LIST_RUN_STOP); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 858 | * ufshcd_hba_start - Start controller initialization sequence |
| 859 | * @hba: per adapter instance |
| 860 | */ |
| 861 | static inline void ufshcd_hba_start(struct ufs_hba *hba) |
| 862 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 863 | ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | /** |
| 867 | * ufshcd_is_hba_active - Get controller state |
| 868 | * @hba: per adapter instance |
| 869 | * |
Tomohiro Kusumi | c9e6010 | 2017-03-28 16:49:24 +0300 | [diff] [blame] | 870 | * Returns false if controller is active, true otherwise |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 871 | */ |
Tomohiro Kusumi | c9e6010 | 2017-03-28 16:49:24 +0300 | [diff] [blame] | 872 | static inline bool ufshcd_is_hba_active(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 873 | { |
Tomohiro Kusumi | 4a8eec2 | 2017-03-28 16:49:25 +0300 | [diff] [blame] | 874 | return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & CONTROLLER_ENABLE) |
| 875 | ? false : true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 876 | } |
| 877 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 878 | u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba) |
| 879 | { |
| 880 | /* HCI version 1.0 and 1.1 supports UniPro 1.41 */ |
| 881 | if ((hba->ufs_version == UFSHCI_VERSION_10) || |
| 882 | (hba->ufs_version == UFSHCI_VERSION_11)) |
| 883 | return UFS_UNIPRO_VER_1_41; |
| 884 | else |
| 885 | return UFS_UNIPRO_VER_1_6; |
| 886 | } |
| 887 | EXPORT_SYMBOL(ufshcd_get_local_unipro_ver); |
| 888 | |
| 889 | static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba) |
| 890 | { |
| 891 | /* |
| 892 | * If both host and device support UniPro ver1.6 or later, PA layer |
| 893 | * parameters tuning happens during link startup itself. |
| 894 | * |
| 895 | * We can manually tune PA layer parameters if either host or device |
| 896 | * doesn't support UniPro ver 1.6 or later. But to keep manual tuning |
| 897 | * logic simple, we will only do manual tuning if local unipro version |
| 898 | * doesn't support ver1.6 or later. |
| 899 | */ |
| 900 | if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6) |
| 901 | return true; |
| 902 | else |
| 903 | return false; |
| 904 | } |
| 905 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 906 | static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up) |
| 907 | { |
| 908 | int ret = 0; |
| 909 | struct ufs_clk_info *clki; |
| 910 | struct list_head *head = &hba->clk_list_head; |
| 911 | ktime_t start = ktime_get(); |
| 912 | bool clk_state_changed = false; |
| 913 | |
Szymon Mielczarek | 566ec9a | 2017-06-05 11:36:54 +0300 | [diff] [blame] | 914 | if (list_empty(head)) |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 915 | goto out; |
| 916 | |
| 917 | ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE); |
| 918 | if (ret) |
| 919 | return ret; |
| 920 | |
| 921 | list_for_each_entry(clki, head, list) { |
| 922 | if (!IS_ERR_OR_NULL(clki->clk)) { |
| 923 | if (scale_up && clki->max_freq) { |
| 924 | if (clki->curr_freq == clki->max_freq) |
| 925 | continue; |
| 926 | |
| 927 | clk_state_changed = true; |
| 928 | ret = clk_set_rate(clki->clk, clki->max_freq); |
| 929 | if (ret) { |
| 930 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 931 | __func__, clki->name, |
| 932 | clki->max_freq, ret); |
| 933 | break; |
| 934 | } |
| 935 | trace_ufshcd_clk_scaling(dev_name(hba->dev), |
| 936 | "scaled up", clki->name, |
| 937 | clki->curr_freq, |
| 938 | clki->max_freq); |
| 939 | |
| 940 | clki->curr_freq = clki->max_freq; |
| 941 | |
| 942 | } else if (!scale_up && clki->min_freq) { |
| 943 | if (clki->curr_freq == clki->min_freq) |
| 944 | continue; |
| 945 | |
| 946 | clk_state_changed = true; |
| 947 | ret = clk_set_rate(clki->clk, clki->min_freq); |
| 948 | if (ret) { |
| 949 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 950 | __func__, clki->name, |
| 951 | clki->min_freq, ret); |
| 952 | break; |
| 953 | } |
| 954 | trace_ufshcd_clk_scaling(dev_name(hba->dev), |
| 955 | "scaled down", clki->name, |
| 956 | clki->curr_freq, |
| 957 | clki->min_freq); |
| 958 | clki->curr_freq = clki->min_freq; |
| 959 | } |
| 960 | } |
| 961 | dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__, |
| 962 | clki->name, clk_get_rate(clki->clk)); |
| 963 | } |
| 964 | |
| 965 | ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE); |
| 966 | |
| 967 | out: |
| 968 | if (clk_state_changed) |
| 969 | trace_ufshcd_profile_clk_scaling(dev_name(hba->dev), |
| 970 | (scale_up ? "up" : "down"), |
| 971 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
| 972 | return ret; |
| 973 | } |
| 974 | |
| 975 | /** |
| 976 | * ufshcd_is_devfreq_scaling_required - check if scaling is required or not |
| 977 | * @hba: per adapter instance |
| 978 | * @scale_up: True if scaling up and false if scaling down |
| 979 | * |
| 980 | * Returns true if scaling is required, false otherwise. |
| 981 | */ |
| 982 | static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba, |
| 983 | bool scale_up) |
| 984 | { |
| 985 | struct ufs_clk_info *clki; |
| 986 | struct list_head *head = &hba->clk_list_head; |
| 987 | |
Szymon Mielczarek | 566ec9a | 2017-06-05 11:36:54 +0300 | [diff] [blame] | 988 | if (list_empty(head)) |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 989 | return false; |
| 990 | |
| 991 | list_for_each_entry(clki, head, list) { |
| 992 | if (!IS_ERR_OR_NULL(clki->clk)) { |
| 993 | if (scale_up && clki->max_freq) { |
| 994 | if (clki->curr_freq == clki->max_freq) |
| 995 | continue; |
| 996 | return true; |
| 997 | } else if (!scale_up && clki->min_freq) { |
| 998 | if (clki->curr_freq == clki->min_freq) |
| 999 | continue; |
| 1000 | return true; |
| 1001 | } |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | return false; |
| 1006 | } |
| 1007 | |
| 1008 | static int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba, |
| 1009 | u64 wait_timeout_us) |
| 1010 | { |
| 1011 | unsigned long flags; |
| 1012 | int ret = 0; |
| 1013 | u32 tm_doorbell; |
| 1014 | u32 tr_doorbell; |
| 1015 | bool timeout = false, do_last_check = false; |
| 1016 | ktime_t start; |
| 1017 | |
| 1018 | ufshcd_hold(hba, false); |
| 1019 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1020 | /* |
| 1021 | * Wait for all the outstanding tasks/transfer requests. |
| 1022 | * Verify by checking the doorbell registers are clear. |
| 1023 | */ |
| 1024 | start = ktime_get(); |
| 1025 | do { |
| 1026 | if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) { |
| 1027 | ret = -EBUSY; |
| 1028 | goto out; |
| 1029 | } |
| 1030 | |
| 1031 | tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL); |
| 1032 | tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 1033 | if (!tm_doorbell && !tr_doorbell) { |
| 1034 | timeout = false; |
| 1035 | break; |
| 1036 | } else if (do_last_check) { |
| 1037 | break; |
| 1038 | } |
| 1039 | |
| 1040 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1041 | schedule(); |
| 1042 | if (ktime_to_us(ktime_sub(ktime_get(), start)) > |
| 1043 | wait_timeout_us) { |
| 1044 | timeout = true; |
| 1045 | /* |
| 1046 | * We might have scheduled out for long time so make |
| 1047 | * sure to check if doorbells are cleared by this time |
| 1048 | * or not. |
| 1049 | */ |
| 1050 | do_last_check = true; |
| 1051 | } |
| 1052 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1053 | } while (tm_doorbell || tr_doorbell); |
| 1054 | |
| 1055 | if (timeout) { |
| 1056 | dev_err(hba->dev, |
| 1057 | "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n", |
| 1058 | __func__, tm_doorbell, tr_doorbell); |
| 1059 | ret = -EBUSY; |
| 1060 | } |
| 1061 | out: |
| 1062 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1063 | ufshcd_release(hba); |
| 1064 | return ret; |
| 1065 | } |
| 1066 | |
| 1067 | /** |
| 1068 | * ufshcd_scale_gear - scale up/down UFS gear |
| 1069 | * @hba: per adapter instance |
| 1070 | * @scale_up: True for scaling up gear and false for scaling down |
| 1071 | * |
| 1072 | * Returns 0 for success, |
| 1073 | * Returns -EBUSY if scaling can't happen at this time |
| 1074 | * Returns non-zero for any other errors |
| 1075 | */ |
| 1076 | static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up) |
| 1077 | { |
| 1078 | #define UFS_MIN_GEAR_TO_SCALE_DOWN UFS_HS_G1 |
| 1079 | int ret = 0; |
| 1080 | struct ufs_pa_layer_attr new_pwr_info; |
| 1081 | |
| 1082 | if (scale_up) { |
| 1083 | memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info.info, |
| 1084 | sizeof(struct ufs_pa_layer_attr)); |
| 1085 | } else { |
| 1086 | memcpy(&new_pwr_info, &hba->pwr_info, |
| 1087 | sizeof(struct ufs_pa_layer_attr)); |
| 1088 | |
| 1089 | if (hba->pwr_info.gear_tx > UFS_MIN_GEAR_TO_SCALE_DOWN |
| 1090 | || hba->pwr_info.gear_rx > UFS_MIN_GEAR_TO_SCALE_DOWN) { |
| 1091 | /* save the current power mode */ |
| 1092 | memcpy(&hba->clk_scaling.saved_pwr_info.info, |
| 1093 | &hba->pwr_info, |
| 1094 | sizeof(struct ufs_pa_layer_attr)); |
| 1095 | |
| 1096 | /* scale down gear */ |
| 1097 | new_pwr_info.gear_tx = UFS_MIN_GEAR_TO_SCALE_DOWN; |
| 1098 | new_pwr_info.gear_rx = UFS_MIN_GEAR_TO_SCALE_DOWN; |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | /* check if the power mode needs to be changed or not? */ |
| 1103 | ret = ufshcd_change_power_mode(hba, &new_pwr_info); |
| 1104 | |
| 1105 | if (ret) |
| 1106 | dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d)", |
| 1107 | __func__, ret, |
| 1108 | hba->pwr_info.gear_tx, hba->pwr_info.gear_rx, |
| 1109 | new_pwr_info.gear_tx, new_pwr_info.gear_rx); |
| 1110 | |
| 1111 | return ret; |
| 1112 | } |
| 1113 | |
| 1114 | static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba) |
| 1115 | { |
| 1116 | #define DOORBELL_CLR_TOUT_US (1000 * 1000) /* 1 sec */ |
| 1117 | int ret = 0; |
| 1118 | /* |
| 1119 | * make sure that there are no outstanding requests when |
| 1120 | * clock scaling is in progress |
| 1121 | */ |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 1122 | ufshcd_scsi_block_requests(hba); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1123 | down_write(&hba->clk_scaling_lock); |
| 1124 | if (ufshcd_wait_for_doorbell_clr(hba, DOORBELL_CLR_TOUT_US)) { |
| 1125 | ret = -EBUSY; |
| 1126 | up_write(&hba->clk_scaling_lock); |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 1127 | ufshcd_scsi_unblock_requests(hba); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | return ret; |
| 1131 | } |
| 1132 | |
| 1133 | static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba) |
| 1134 | { |
| 1135 | up_write(&hba->clk_scaling_lock); |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 1136 | ufshcd_scsi_unblock_requests(hba); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | /** |
| 1140 | * ufshcd_devfreq_scale - scale up/down UFS clocks and gear |
| 1141 | * @hba: per adapter instance |
| 1142 | * @scale_up: True for scaling up and false for scalin down |
| 1143 | * |
| 1144 | * Returns 0 for success, |
| 1145 | * Returns -EBUSY if scaling can't happen at this time |
| 1146 | * Returns non-zero for any other errors |
| 1147 | */ |
| 1148 | static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up) |
| 1149 | { |
| 1150 | int ret = 0; |
| 1151 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1152 | /* let's not get into low power until clock scaling is completed */ |
| 1153 | ufshcd_hold(hba, false); |
| 1154 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1155 | ret = ufshcd_clock_scaling_prepare(hba); |
| 1156 | if (ret) |
| 1157 | return ret; |
| 1158 | |
| 1159 | /* scale down the gear before scaling down clocks */ |
| 1160 | if (!scale_up) { |
| 1161 | ret = ufshcd_scale_gear(hba, false); |
| 1162 | if (ret) |
| 1163 | goto out; |
| 1164 | } |
| 1165 | |
| 1166 | ret = ufshcd_scale_clks(hba, scale_up); |
| 1167 | if (ret) { |
| 1168 | if (!scale_up) |
| 1169 | ufshcd_scale_gear(hba, true); |
| 1170 | goto out; |
| 1171 | } |
| 1172 | |
| 1173 | /* scale up the gear after scaling up clocks */ |
| 1174 | if (scale_up) { |
| 1175 | ret = ufshcd_scale_gear(hba, true); |
| 1176 | if (ret) { |
| 1177 | ufshcd_scale_clks(hba, false); |
| 1178 | goto out; |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE); |
| 1183 | |
| 1184 | out: |
| 1185 | ufshcd_clock_scaling_unprepare(hba); |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1186 | ufshcd_release(hba); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1187 | return ret; |
| 1188 | } |
| 1189 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1190 | static void ufshcd_clk_scaling_suspend_work(struct work_struct *work) |
| 1191 | { |
| 1192 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1193 | clk_scaling.suspend_work); |
| 1194 | unsigned long irq_flags; |
| 1195 | |
| 1196 | spin_lock_irqsave(hba->host->host_lock, irq_flags); |
| 1197 | if (hba->clk_scaling.active_reqs || hba->clk_scaling.is_suspended) { |
| 1198 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1199 | return; |
| 1200 | } |
| 1201 | hba->clk_scaling.is_suspended = true; |
| 1202 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1203 | |
| 1204 | __ufshcd_suspend_clkscaling(hba); |
| 1205 | } |
| 1206 | |
| 1207 | static void ufshcd_clk_scaling_resume_work(struct work_struct *work) |
| 1208 | { |
| 1209 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1210 | clk_scaling.resume_work); |
| 1211 | unsigned long irq_flags; |
| 1212 | |
| 1213 | spin_lock_irqsave(hba->host->host_lock, irq_flags); |
| 1214 | if (!hba->clk_scaling.is_suspended) { |
| 1215 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1216 | return; |
| 1217 | } |
| 1218 | hba->clk_scaling.is_suspended = false; |
| 1219 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1220 | |
| 1221 | devfreq_resume_device(hba->devfreq); |
| 1222 | } |
| 1223 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1224 | static int ufshcd_devfreq_target(struct device *dev, |
| 1225 | unsigned long *freq, u32 flags) |
| 1226 | { |
| 1227 | int ret = 0; |
| 1228 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1229 | ktime_t start; |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1230 | bool scale_up, sched_clk_scaling_suspend_work = false; |
Bjorn Andersson | 092b455 | 2018-05-17 23:26:37 -0700 | [diff] [blame] | 1231 | struct list_head *clk_list = &hba->clk_list_head; |
| 1232 | struct ufs_clk_info *clki; |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1233 | unsigned long irq_flags; |
| 1234 | |
| 1235 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 1236 | return -EINVAL; |
| 1237 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1238 | spin_lock_irqsave(hba->host->host_lock, irq_flags); |
| 1239 | if (ufshcd_eh_in_progress(hba)) { |
| 1240 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1241 | return 0; |
| 1242 | } |
| 1243 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1244 | if (!hba->clk_scaling.active_reqs) |
| 1245 | sched_clk_scaling_suspend_work = true; |
| 1246 | |
Bjorn Andersson | 092b455 | 2018-05-17 23:26:37 -0700 | [diff] [blame] | 1247 | if (list_empty(clk_list)) { |
| 1248 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1249 | goto out; |
| 1250 | } |
| 1251 | |
| 1252 | clki = list_first_entry(&hba->clk_list_head, struct ufs_clk_info, list); |
| 1253 | scale_up = (*freq == clki->max_freq) ? true : false; |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1254 | if (!ufshcd_is_devfreq_scaling_required(hba, scale_up)) { |
| 1255 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1256 | ret = 0; |
| 1257 | goto out; /* no state change required */ |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1258 | } |
| 1259 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1260 | |
| 1261 | start = ktime_get(); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1262 | ret = ufshcd_devfreq_scale(hba, scale_up); |
| 1263 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1264 | trace_ufshcd_profile_clk_scaling(dev_name(hba->dev), |
| 1265 | (scale_up ? "up" : "down"), |
| 1266 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
| 1267 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1268 | out: |
| 1269 | if (sched_clk_scaling_suspend_work) |
| 1270 | queue_work(hba->clk_scaling.workq, |
| 1271 | &hba->clk_scaling.suspend_work); |
| 1272 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1273 | return ret; |
| 1274 | } |
| 1275 | |
| 1276 | |
| 1277 | static int ufshcd_devfreq_get_dev_status(struct device *dev, |
| 1278 | struct devfreq_dev_status *stat) |
| 1279 | { |
| 1280 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1281 | struct ufs_clk_scaling *scaling = &hba->clk_scaling; |
| 1282 | unsigned long flags; |
| 1283 | |
| 1284 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 1285 | return -EINVAL; |
| 1286 | |
| 1287 | memset(stat, 0, sizeof(*stat)); |
| 1288 | |
| 1289 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1290 | if (!scaling->window_start_t) |
| 1291 | goto start_window; |
| 1292 | |
| 1293 | if (scaling->is_busy_started) |
| 1294 | scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(), |
| 1295 | scaling->busy_start_t)); |
| 1296 | |
| 1297 | stat->total_time = jiffies_to_usecs((long)jiffies - |
| 1298 | (long)scaling->window_start_t); |
| 1299 | stat->busy_time = scaling->tot_busy_t; |
| 1300 | start_window: |
| 1301 | scaling->window_start_t = jiffies; |
| 1302 | scaling->tot_busy_t = 0; |
| 1303 | |
| 1304 | if (hba->outstanding_reqs) { |
| 1305 | scaling->busy_start_t = ktime_get(); |
| 1306 | scaling->is_busy_started = true; |
| 1307 | } else { |
| 1308 | scaling->busy_start_t = 0; |
| 1309 | scaling->is_busy_started = false; |
| 1310 | } |
| 1311 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1312 | return 0; |
| 1313 | } |
| 1314 | |
| 1315 | static struct devfreq_dev_profile ufs_devfreq_profile = { |
| 1316 | .polling_ms = 100, |
| 1317 | .target = ufshcd_devfreq_target, |
| 1318 | .get_dev_status = ufshcd_devfreq_get_dev_status, |
| 1319 | }; |
| 1320 | |
Bjorn Andersson | deac444 | 2018-05-17 23:26:36 -0700 | [diff] [blame] | 1321 | static int ufshcd_devfreq_init(struct ufs_hba *hba) |
| 1322 | { |
Bjorn Andersson | 092b455 | 2018-05-17 23:26:37 -0700 | [diff] [blame] | 1323 | struct list_head *clk_list = &hba->clk_list_head; |
| 1324 | struct ufs_clk_info *clki; |
Bjorn Andersson | deac444 | 2018-05-17 23:26:36 -0700 | [diff] [blame] | 1325 | struct devfreq *devfreq; |
| 1326 | int ret; |
| 1327 | |
Bjorn Andersson | 092b455 | 2018-05-17 23:26:37 -0700 | [diff] [blame] | 1328 | /* Skip devfreq if we don't have any clocks in the list */ |
| 1329 | if (list_empty(clk_list)) |
| 1330 | return 0; |
| 1331 | |
| 1332 | clki = list_first_entry(clk_list, struct ufs_clk_info, list); |
| 1333 | dev_pm_opp_add(hba->dev, clki->min_freq, 0); |
| 1334 | dev_pm_opp_add(hba->dev, clki->max_freq, 0); |
| 1335 | |
| 1336 | devfreq = devfreq_add_device(hba->dev, |
Bjorn Andersson | deac444 | 2018-05-17 23:26:36 -0700 | [diff] [blame] | 1337 | &ufs_devfreq_profile, |
| 1338 | DEVFREQ_GOV_SIMPLE_ONDEMAND, |
| 1339 | NULL); |
| 1340 | if (IS_ERR(devfreq)) { |
| 1341 | ret = PTR_ERR(devfreq); |
| 1342 | dev_err(hba->dev, "Unable to register with devfreq %d\n", ret); |
Bjorn Andersson | 092b455 | 2018-05-17 23:26:37 -0700 | [diff] [blame] | 1343 | |
| 1344 | dev_pm_opp_remove(hba->dev, clki->min_freq); |
| 1345 | dev_pm_opp_remove(hba->dev, clki->max_freq); |
Bjorn Andersson | deac444 | 2018-05-17 23:26:36 -0700 | [diff] [blame] | 1346 | return ret; |
| 1347 | } |
| 1348 | |
| 1349 | hba->devfreq = devfreq; |
| 1350 | |
| 1351 | return 0; |
| 1352 | } |
| 1353 | |
Bjorn Andersson | 092b455 | 2018-05-17 23:26:37 -0700 | [diff] [blame] | 1354 | static void ufshcd_devfreq_remove(struct ufs_hba *hba) |
| 1355 | { |
| 1356 | struct list_head *clk_list = &hba->clk_list_head; |
| 1357 | struct ufs_clk_info *clki; |
| 1358 | |
| 1359 | if (!hba->devfreq) |
| 1360 | return; |
| 1361 | |
| 1362 | devfreq_remove_device(hba->devfreq); |
| 1363 | hba->devfreq = NULL; |
| 1364 | |
| 1365 | clki = list_first_entry(clk_list, struct ufs_clk_info, list); |
| 1366 | dev_pm_opp_remove(hba->dev, clki->min_freq); |
| 1367 | dev_pm_opp_remove(hba->dev, clki->max_freq); |
| 1368 | } |
| 1369 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1370 | static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba) |
| 1371 | { |
| 1372 | unsigned long flags; |
| 1373 | |
| 1374 | devfreq_suspend_device(hba->devfreq); |
| 1375 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1376 | hba->clk_scaling.window_start_t = 0; |
| 1377 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1378 | } |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1379 | |
Gilad Broner | a508253 | 2016-10-17 17:10:00 -0700 | [diff] [blame] | 1380 | static void ufshcd_suspend_clkscaling(struct ufs_hba *hba) |
| 1381 | { |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1382 | unsigned long flags; |
| 1383 | bool suspend = false; |
| 1384 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1385 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 1386 | return; |
| 1387 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1388 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1389 | if (!hba->clk_scaling.is_suspended) { |
| 1390 | suspend = true; |
| 1391 | hba->clk_scaling.is_suspended = true; |
| 1392 | } |
| 1393 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1394 | |
| 1395 | if (suspend) |
| 1396 | __ufshcd_suspend_clkscaling(hba); |
Gilad Broner | a508253 | 2016-10-17 17:10:00 -0700 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | static void ufshcd_resume_clkscaling(struct ufs_hba *hba) |
| 1400 | { |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1401 | unsigned long flags; |
| 1402 | bool resume = false; |
| 1403 | |
| 1404 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 1405 | return; |
| 1406 | |
| 1407 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1408 | if (hba->clk_scaling.is_suspended) { |
| 1409 | resume = true; |
| 1410 | hba->clk_scaling.is_suspended = false; |
| 1411 | } |
| 1412 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1413 | |
| 1414 | if (resume) |
| 1415 | devfreq_resume_device(hba->devfreq); |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | static ssize_t ufshcd_clkscale_enable_show(struct device *dev, |
| 1419 | struct device_attribute *attr, char *buf) |
| 1420 | { |
| 1421 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1422 | |
| 1423 | return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_scaling.is_allowed); |
| 1424 | } |
| 1425 | |
| 1426 | static ssize_t ufshcd_clkscale_enable_store(struct device *dev, |
| 1427 | struct device_attribute *attr, const char *buf, size_t count) |
| 1428 | { |
| 1429 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1430 | u32 value; |
| 1431 | int err; |
| 1432 | |
| 1433 | if (kstrtou32(buf, 0, &value)) |
| 1434 | return -EINVAL; |
| 1435 | |
| 1436 | value = !!value; |
| 1437 | if (value == hba->clk_scaling.is_allowed) |
| 1438 | goto out; |
| 1439 | |
| 1440 | pm_runtime_get_sync(hba->dev); |
| 1441 | ufshcd_hold(hba, false); |
| 1442 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1443 | cancel_work_sync(&hba->clk_scaling.suspend_work); |
| 1444 | cancel_work_sync(&hba->clk_scaling.resume_work); |
| 1445 | |
| 1446 | hba->clk_scaling.is_allowed = value; |
| 1447 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1448 | if (value) { |
| 1449 | ufshcd_resume_clkscaling(hba); |
| 1450 | } else { |
| 1451 | ufshcd_suspend_clkscaling(hba); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1452 | err = ufshcd_devfreq_scale(hba, true); |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1453 | if (err) |
| 1454 | dev_err(hba->dev, "%s: failed to scale clocks up %d\n", |
| 1455 | __func__, err); |
| 1456 | } |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1457 | |
| 1458 | ufshcd_release(hba); |
| 1459 | pm_runtime_put_sync(hba->dev); |
| 1460 | out: |
| 1461 | return count; |
Gilad Broner | a508253 | 2016-10-17 17:10:00 -0700 | [diff] [blame] | 1462 | } |
| 1463 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1464 | static void ufshcd_clkscaling_init_sysfs(struct ufs_hba *hba) |
| 1465 | { |
| 1466 | hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show; |
| 1467 | hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store; |
| 1468 | sysfs_attr_init(&hba->clk_scaling.enable_attr.attr); |
| 1469 | hba->clk_scaling.enable_attr.attr.name = "clkscale_enable"; |
| 1470 | hba->clk_scaling.enable_attr.attr.mode = 0644; |
| 1471 | if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr)) |
| 1472 | dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n"); |
| 1473 | } |
| 1474 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1475 | static void ufshcd_ungate_work(struct work_struct *work) |
| 1476 | { |
| 1477 | int ret; |
| 1478 | unsigned long flags; |
| 1479 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1480 | clk_gating.ungate_work); |
| 1481 | |
| 1482 | cancel_delayed_work_sync(&hba->clk_gating.gate_work); |
| 1483 | |
| 1484 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1485 | if (hba->clk_gating.state == CLKS_ON) { |
| 1486 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1487 | goto unblock_reqs; |
| 1488 | } |
| 1489 | |
| 1490 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1491 | ufshcd_setup_clocks(hba, true); |
| 1492 | |
| 1493 | /* Exit from hibern8 */ |
| 1494 | if (ufshcd_can_hibern8_during_gating(hba)) { |
| 1495 | /* Prevent gating in this path */ |
| 1496 | hba->clk_gating.is_suspended = true; |
| 1497 | if (ufshcd_is_link_hibern8(hba)) { |
| 1498 | ret = ufshcd_uic_hibern8_exit(hba); |
| 1499 | if (ret) |
| 1500 | dev_err(hba->dev, "%s: hibern8 exit failed %d\n", |
| 1501 | __func__, ret); |
| 1502 | else |
| 1503 | ufshcd_set_link_active(hba); |
| 1504 | } |
| 1505 | hba->clk_gating.is_suspended = false; |
| 1506 | } |
| 1507 | unblock_reqs: |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 1508 | ufshcd_scsi_unblock_requests(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | /** |
| 1512 | * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release. |
| 1513 | * Also, exit from hibern8 mode and set the link as active. |
| 1514 | * @hba: per adapter instance |
| 1515 | * @async: This indicates whether caller should ungate clocks asynchronously. |
| 1516 | */ |
| 1517 | int ufshcd_hold(struct ufs_hba *hba, bool async) |
| 1518 | { |
| 1519 | int rc = 0; |
| 1520 | unsigned long flags; |
| 1521 | |
| 1522 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1523 | goto out; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1524 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1525 | hba->clk_gating.active_reqs++; |
| 1526 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 1527 | if (ufshcd_eh_in_progress(hba)) { |
| 1528 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1529 | return 0; |
| 1530 | } |
| 1531 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1532 | start: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1533 | switch (hba->clk_gating.state) { |
| 1534 | case CLKS_ON: |
Venkat Gopalakrishnan | f2a785a | 2016-10-17 17:10:53 -0700 | [diff] [blame] | 1535 | /* |
| 1536 | * Wait for the ungate work to complete if in progress. |
| 1537 | * Though the clocks may be in ON state, the link could |
| 1538 | * still be in hibner8 state if hibern8 is allowed |
| 1539 | * during clock gating. |
| 1540 | * Make sure we exit hibern8 state also in addition to |
| 1541 | * clocks being ON. |
| 1542 | */ |
| 1543 | if (ufshcd_can_hibern8_during_gating(hba) && |
| 1544 | ufshcd_is_link_hibern8(hba)) { |
| 1545 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1546 | flush_work(&hba->clk_gating.ungate_work); |
| 1547 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1548 | goto start; |
| 1549 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1550 | break; |
| 1551 | case REQ_CLKS_OFF: |
| 1552 | if (cancel_delayed_work(&hba->clk_gating.gate_work)) { |
| 1553 | hba->clk_gating.state = CLKS_ON; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1554 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1555 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1556 | break; |
| 1557 | } |
| 1558 | /* |
Tomohiro Kusumi | 9c490d2 | 2017-03-28 16:49:26 +0300 | [diff] [blame] | 1559 | * If we are here, it means gating work is either done or |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1560 | * currently running. Hence, fall through to cancel gating |
| 1561 | * work and to enable clocks. |
| 1562 | */ |
Tomas Winkler | 30eb2e4 | 2018-11-26 10:10:34 +0200 | [diff] [blame] | 1563 | /* fallthrough */ |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1564 | case CLKS_OFF: |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 1565 | ufshcd_scsi_block_requests(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1566 | hba->clk_gating.state = REQ_CLKS_ON; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1567 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1568 | hba->clk_gating.state); |
Vijay Viswanath | 10e5e37 | 2018-05-03 16:37:22 +0530 | [diff] [blame] | 1569 | queue_work(hba->clk_gating.clk_gating_workq, |
| 1570 | &hba->clk_gating.ungate_work); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1571 | /* |
| 1572 | * fall through to check if we should wait for this |
| 1573 | * work to be done or not. |
| 1574 | */ |
Tomas Winkler | 30eb2e4 | 2018-11-26 10:10:34 +0200 | [diff] [blame] | 1575 | /* fallthrough */ |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1576 | case REQ_CLKS_ON: |
| 1577 | if (async) { |
| 1578 | rc = -EAGAIN; |
| 1579 | hba->clk_gating.active_reqs--; |
| 1580 | break; |
| 1581 | } |
| 1582 | |
| 1583 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1584 | flush_work(&hba->clk_gating.ungate_work); |
| 1585 | /* Make sure state is CLKS_ON before returning */ |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1586 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1587 | goto start; |
| 1588 | default: |
| 1589 | dev_err(hba->dev, "%s: clk gating is in invalid state %d\n", |
| 1590 | __func__, hba->clk_gating.state); |
| 1591 | break; |
| 1592 | } |
| 1593 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1594 | out: |
| 1595 | return rc; |
| 1596 | } |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 1597 | EXPORT_SYMBOL_GPL(ufshcd_hold); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1598 | |
| 1599 | static void ufshcd_gate_work(struct work_struct *work) |
| 1600 | { |
| 1601 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1602 | clk_gating.gate_work.work); |
| 1603 | unsigned long flags; |
| 1604 | |
| 1605 | spin_lock_irqsave(hba->host->host_lock, flags); |
Venkat Gopalakrishnan | 3f0c06d | 2016-10-17 17:11:07 -0700 | [diff] [blame] | 1606 | /* |
| 1607 | * In case you are here to cancel this work the gating state |
| 1608 | * would be marked as REQ_CLKS_ON. In this case save time by |
| 1609 | * skipping the gating work and exit after changing the clock |
| 1610 | * state to CLKS_ON. |
| 1611 | */ |
| 1612 | if (hba->clk_gating.is_suspended || |
| 1613 | (hba->clk_gating.state == REQ_CLKS_ON)) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1614 | hba->clk_gating.state = CLKS_ON; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1615 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1616 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1617 | goto rel_lock; |
| 1618 | } |
| 1619 | |
| 1620 | if (hba->clk_gating.active_reqs |
| 1621 | || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL |
| 1622 | || hba->lrb_in_use || hba->outstanding_tasks |
| 1623 | || hba->active_uic_cmd || hba->uic_async_done) |
| 1624 | goto rel_lock; |
| 1625 | |
| 1626 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1627 | |
| 1628 | /* put the link into hibern8 mode before turning off clocks */ |
| 1629 | if (ufshcd_can_hibern8_during_gating(hba)) { |
| 1630 | if (ufshcd_uic_hibern8_enter(hba)) { |
| 1631 | hba->clk_gating.state = CLKS_ON; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1632 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1633 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1634 | goto out; |
| 1635 | } |
| 1636 | ufshcd_set_link_hibern8(hba); |
| 1637 | } |
| 1638 | |
| 1639 | if (!ufshcd_is_link_active(hba)) |
| 1640 | ufshcd_setup_clocks(hba, false); |
| 1641 | else |
| 1642 | /* If link is active, device ref_clk can't be switched off */ |
| 1643 | __ufshcd_setup_clocks(hba, false, true); |
| 1644 | |
| 1645 | /* |
| 1646 | * In case you are here to cancel this work the gating state |
| 1647 | * would be marked as REQ_CLKS_ON. In this case keep the state |
| 1648 | * as REQ_CLKS_ON which would anyway imply that clocks are off |
| 1649 | * and a request to turn them on is pending. By doing this way, |
| 1650 | * we keep the state machine in tact and this would ultimately |
| 1651 | * prevent from doing cancel work multiple times when there are |
| 1652 | * new requests arriving before the current cancel work is done. |
| 1653 | */ |
| 1654 | spin_lock_irqsave(hba->host->host_lock, flags); |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1655 | if (hba->clk_gating.state == REQ_CLKS_OFF) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1656 | hba->clk_gating.state = CLKS_OFF; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1657 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1658 | hba->clk_gating.state); |
| 1659 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1660 | rel_lock: |
| 1661 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1662 | out: |
| 1663 | return; |
| 1664 | } |
| 1665 | |
| 1666 | /* host lock must be held before calling this variant */ |
| 1667 | static void __ufshcd_release(struct ufs_hba *hba) |
| 1668 | { |
| 1669 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1670 | return; |
| 1671 | |
| 1672 | hba->clk_gating.active_reqs--; |
| 1673 | |
| 1674 | if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended |
| 1675 | || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL |
| 1676 | || hba->lrb_in_use || hba->outstanding_tasks |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 1677 | || hba->active_uic_cmd || hba->uic_async_done |
| 1678 | || ufshcd_eh_in_progress(hba)) |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1679 | return; |
| 1680 | |
| 1681 | hba->clk_gating.state = REQ_CLKS_OFF; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1682 | trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state); |
Evan Green | f4bb770 | 2018-10-05 10:27:32 -0700 | [diff] [blame] | 1683 | queue_delayed_work(hba->clk_gating.clk_gating_workq, |
| 1684 | &hba->clk_gating.gate_work, |
| 1685 | msecs_to_jiffies(hba->clk_gating.delay_ms)); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | void ufshcd_release(struct ufs_hba *hba) |
| 1689 | { |
| 1690 | unsigned long flags; |
| 1691 | |
| 1692 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1693 | __ufshcd_release(hba); |
| 1694 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1695 | } |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 1696 | EXPORT_SYMBOL_GPL(ufshcd_release); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1697 | |
| 1698 | static ssize_t ufshcd_clkgate_delay_show(struct device *dev, |
| 1699 | struct device_attribute *attr, char *buf) |
| 1700 | { |
| 1701 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1702 | |
| 1703 | return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms); |
| 1704 | } |
| 1705 | |
| 1706 | static ssize_t ufshcd_clkgate_delay_store(struct device *dev, |
| 1707 | struct device_attribute *attr, const char *buf, size_t count) |
| 1708 | { |
| 1709 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1710 | unsigned long flags, value; |
| 1711 | |
| 1712 | if (kstrtoul(buf, 0, &value)) |
| 1713 | return -EINVAL; |
| 1714 | |
| 1715 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1716 | hba->clk_gating.delay_ms = value; |
| 1717 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1718 | return count; |
| 1719 | } |
| 1720 | |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1721 | static ssize_t ufshcd_clkgate_enable_show(struct device *dev, |
| 1722 | struct device_attribute *attr, char *buf) |
| 1723 | { |
| 1724 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1725 | |
| 1726 | return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_gating.is_enabled); |
| 1727 | } |
| 1728 | |
| 1729 | static ssize_t ufshcd_clkgate_enable_store(struct device *dev, |
| 1730 | struct device_attribute *attr, const char *buf, size_t count) |
| 1731 | { |
| 1732 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1733 | unsigned long flags; |
| 1734 | u32 value; |
| 1735 | |
| 1736 | if (kstrtou32(buf, 0, &value)) |
| 1737 | return -EINVAL; |
| 1738 | |
| 1739 | value = !!value; |
| 1740 | if (value == hba->clk_gating.is_enabled) |
| 1741 | goto out; |
| 1742 | |
| 1743 | if (value) { |
| 1744 | ufshcd_release(hba); |
| 1745 | } else { |
| 1746 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1747 | hba->clk_gating.active_reqs++; |
| 1748 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1749 | } |
| 1750 | |
| 1751 | hba->clk_gating.is_enabled = value; |
| 1752 | out: |
| 1753 | return count; |
| 1754 | } |
| 1755 | |
Vivek Gautam | eebcc19 | 2018-08-07 23:17:39 +0530 | [diff] [blame] | 1756 | static void ufshcd_init_clk_scaling(struct ufs_hba *hba) |
| 1757 | { |
| 1758 | char wq_name[sizeof("ufs_clkscaling_00")]; |
| 1759 | |
| 1760 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 1761 | return; |
| 1762 | |
| 1763 | INIT_WORK(&hba->clk_scaling.suspend_work, |
| 1764 | ufshcd_clk_scaling_suspend_work); |
| 1765 | INIT_WORK(&hba->clk_scaling.resume_work, |
| 1766 | ufshcd_clk_scaling_resume_work); |
| 1767 | |
| 1768 | snprintf(wq_name, sizeof(wq_name), "ufs_clkscaling_%d", |
| 1769 | hba->host->host_no); |
| 1770 | hba->clk_scaling.workq = create_singlethread_workqueue(wq_name); |
| 1771 | |
| 1772 | ufshcd_clkscaling_init_sysfs(hba); |
| 1773 | } |
| 1774 | |
| 1775 | static void ufshcd_exit_clk_scaling(struct ufs_hba *hba) |
| 1776 | { |
| 1777 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 1778 | return; |
| 1779 | |
| 1780 | destroy_workqueue(hba->clk_scaling.workq); |
| 1781 | ufshcd_devfreq_remove(hba); |
| 1782 | } |
| 1783 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1784 | static void ufshcd_init_clk_gating(struct ufs_hba *hba) |
| 1785 | { |
Vijay Viswanath | 10e5e37 | 2018-05-03 16:37:22 +0530 | [diff] [blame] | 1786 | char wq_name[sizeof("ufs_clk_gating_00")]; |
| 1787 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1788 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1789 | return; |
| 1790 | |
| 1791 | hba->clk_gating.delay_ms = 150; |
| 1792 | INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work); |
| 1793 | INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work); |
| 1794 | |
Vijay Viswanath | 10e5e37 | 2018-05-03 16:37:22 +0530 | [diff] [blame] | 1795 | snprintf(wq_name, ARRAY_SIZE(wq_name), "ufs_clk_gating_%d", |
| 1796 | hba->host->host_no); |
| 1797 | hba->clk_gating.clk_gating_workq = alloc_ordered_workqueue(wq_name, |
| 1798 | WQ_MEM_RECLAIM); |
| 1799 | |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1800 | hba->clk_gating.is_enabled = true; |
| 1801 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1802 | hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show; |
| 1803 | hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store; |
| 1804 | sysfs_attr_init(&hba->clk_gating.delay_attr.attr); |
| 1805 | hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms"; |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1806 | hba->clk_gating.delay_attr.attr.mode = 0644; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1807 | if (device_create_file(hba->dev, &hba->clk_gating.delay_attr)) |
| 1808 | dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n"); |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1809 | |
| 1810 | hba->clk_gating.enable_attr.show = ufshcd_clkgate_enable_show; |
| 1811 | hba->clk_gating.enable_attr.store = ufshcd_clkgate_enable_store; |
| 1812 | sysfs_attr_init(&hba->clk_gating.enable_attr.attr); |
| 1813 | hba->clk_gating.enable_attr.attr.name = "clkgate_enable"; |
| 1814 | hba->clk_gating.enable_attr.attr.mode = 0644; |
| 1815 | if (device_create_file(hba->dev, &hba->clk_gating.enable_attr)) |
| 1816 | dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n"); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | static void ufshcd_exit_clk_gating(struct ufs_hba *hba) |
| 1820 | { |
| 1821 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1822 | return; |
| 1823 | device_remove_file(hba->dev, &hba->clk_gating.delay_attr); |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1824 | device_remove_file(hba->dev, &hba->clk_gating.enable_attr); |
Akinobu Mita | 97cd680 | 2014-11-24 14:24:18 +0900 | [diff] [blame] | 1825 | cancel_work_sync(&hba->clk_gating.ungate_work); |
| 1826 | cancel_delayed_work_sync(&hba->clk_gating.gate_work); |
Vijay Viswanath | 10e5e37 | 2018-05-03 16:37:22 +0530 | [diff] [blame] | 1827 | destroy_workqueue(hba->clk_gating.clk_gating_workq); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1828 | } |
| 1829 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1830 | /* Must be called with host lock acquired */ |
| 1831 | static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba) |
| 1832 | { |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1833 | bool queue_resume_work = false; |
| 1834 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1835 | if (!ufshcd_is_clkscaling_supported(hba)) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1836 | return; |
| 1837 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1838 | if (!hba->clk_scaling.active_reqs++) |
| 1839 | queue_resume_work = true; |
| 1840 | |
| 1841 | if (!hba->clk_scaling.is_allowed || hba->pm_op_in_progress) |
| 1842 | return; |
| 1843 | |
| 1844 | if (queue_resume_work) |
| 1845 | queue_work(hba->clk_scaling.workq, |
| 1846 | &hba->clk_scaling.resume_work); |
| 1847 | |
| 1848 | if (!hba->clk_scaling.window_start_t) { |
| 1849 | hba->clk_scaling.window_start_t = jiffies; |
| 1850 | hba->clk_scaling.tot_busy_t = 0; |
| 1851 | hba->clk_scaling.is_busy_started = false; |
| 1852 | } |
| 1853 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1854 | if (!hba->clk_scaling.is_busy_started) { |
| 1855 | hba->clk_scaling.busy_start_t = ktime_get(); |
| 1856 | hba->clk_scaling.is_busy_started = true; |
| 1857 | } |
| 1858 | } |
| 1859 | |
| 1860 | static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba) |
| 1861 | { |
| 1862 | struct ufs_clk_scaling *scaling = &hba->clk_scaling; |
| 1863 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1864 | if (!ufshcd_is_clkscaling_supported(hba)) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1865 | return; |
| 1866 | |
| 1867 | if (!hba->outstanding_reqs && scaling->is_busy_started) { |
| 1868 | scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(), |
| 1869 | scaling->busy_start_t)); |
Thomas Gleixner | 8b0e195 | 2016-12-25 12:30:41 +0100 | [diff] [blame] | 1870 | scaling->busy_start_t = 0; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1871 | scaling->is_busy_started = false; |
| 1872 | } |
| 1873 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1874 | /** |
| 1875 | * ufshcd_send_command - Send SCSI or device management commands |
| 1876 | * @hba: per adapter instance |
| 1877 | * @task_tag: Task tag of the command |
| 1878 | */ |
| 1879 | static inline |
| 1880 | void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag) |
| 1881 | { |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 1882 | hba->lrb[task_tag].issue_time_stamp = ktime_get(); |
Zang Leigang | 0901718 | 2017-09-27 10:06:06 +0800 | [diff] [blame] | 1883 | hba->lrb[task_tag].compl_time_stamp = ktime_set(0, 0); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1884 | ufshcd_clk_scaling_start_busy(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1885 | __set_bit(task_tag, &hba->outstanding_reqs); |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 1886 | ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
Gilad Broner | ad1a1b9 | 2016-10-17 17:09:36 -0700 | [diff] [blame] | 1887 | /* Make sure that doorbell is committed immediately */ |
| 1888 | wmb(); |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 1889 | ufshcd_add_command_trace(hba, task_tag, "send"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | /** |
| 1893 | * 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] | 1894 | * @lrbp: pointer to local reference block |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1895 | */ |
| 1896 | static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp) |
| 1897 | { |
| 1898 | int len; |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 1899 | if (lrbp->sense_buffer && |
| 1900 | ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) { |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 1901 | int len_to_copy; |
| 1902 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1903 | len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len); |
Avri Altman | 09a5a24 | 2018-11-22 20:04:56 +0200 | [diff] [blame] | 1904 | len_to_copy = min_t(int, UFS_SENSE_SIZE, len); |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 1905 | |
Avri Altman | 09a5a24 | 2018-11-22 20:04:56 +0200 | [diff] [blame] | 1906 | memcpy(lrbp->sense_buffer, lrbp->ucd_rsp_ptr->sr.sense_data, |
| 1907 | len_to_copy); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1908 | } |
| 1909 | } |
| 1910 | |
| 1911 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1912 | * ufshcd_copy_query_response() - Copy the Query Response and the data |
| 1913 | * descriptor |
| 1914 | * @hba: per adapter instance |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 1915 | * @lrbp: pointer to local reference block |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1916 | */ |
| 1917 | static |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1918 | 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] | 1919 | { |
| 1920 | struct ufs_query_res *query_res = &hba->dev_cmd.query.response; |
| 1921 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1922 | 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] | 1923 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1924 | /* Get the descriptor */ |
Avri Altman | 1c90836 | 2019-05-21 11:24:22 +0300 | [diff] [blame] | 1925 | if (hba->dev_cmd.query.descriptor && |
| 1926 | lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 1927 | u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1928 | GENERAL_UPIU_REQUEST_SIZE; |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1929 | u16 resp_len; |
| 1930 | u16 buf_len; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1931 | |
| 1932 | /* data segment length */ |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1933 | resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) & |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1934 | MASK_QUERY_DATA_SEG_LEN; |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 1935 | buf_len = be16_to_cpu( |
| 1936 | hba->dev_cmd.query.request.upiu_req.length); |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1937 | if (likely(buf_len >= resp_len)) { |
| 1938 | memcpy(hba->dev_cmd.query.descriptor, descp, resp_len); |
| 1939 | } else { |
| 1940 | dev_warn(hba->dev, |
Bean Huo | 3d4881d | 2019-11-12 23:34:35 +0100 | [diff] [blame] | 1941 | "%s: rsp size %d is bigger than buffer size %d", |
| 1942 | __func__, resp_len, buf_len); |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1943 | return -EINVAL; |
| 1944 | } |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1945 | } |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1946 | |
| 1947 | return 0; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1951 | * ufshcd_hba_capabilities - Read controller capabilities |
| 1952 | * @hba: per adapter instance |
| 1953 | */ |
| 1954 | static inline void ufshcd_hba_capabilities(struct ufs_hba *hba) |
| 1955 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 1956 | hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1957 | |
| 1958 | /* nutrs and nutmrs are 0 based values */ |
| 1959 | hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1; |
| 1960 | hba->nutmrs = |
| 1961 | ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1; |
| 1962 | } |
| 1963 | |
| 1964 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1965 | * ufshcd_ready_for_uic_cmd - Check if controller is ready |
| 1966 | * to accept UIC commands |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1967 | * @hba: per adapter instance |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1968 | * Return true on success, else false |
| 1969 | */ |
| 1970 | static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba) |
| 1971 | { |
| 1972 | if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY) |
| 1973 | return true; |
| 1974 | else |
| 1975 | return false; |
| 1976 | } |
| 1977 | |
| 1978 | /** |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 1979 | * ufshcd_get_upmcrs - Get the power mode change request status |
| 1980 | * @hba: Pointer to adapter instance |
| 1981 | * |
| 1982 | * This function gets the UPMCRS field of HCS register |
| 1983 | * Returns value of UPMCRS field |
| 1984 | */ |
| 1985 | static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba) |
| 1986 | { |
| 1987 | return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7; |
| 1988 | } |
| 1989 | |
| 1990 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1991 | * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers |
| 1992 | * @hba: per adapter instance |
| 1993 | * @uic_cmd: UIC command |
| 1994 | * |
| 1995 | * Mutex must be held. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1996 | */ |
| 1997 | static inline void |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1998 | 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] | 1999 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2000 | WARN_ON(hba->active_uic_cmd); |
| 2001 | |
| 2002 | hba->active_uic_cmd = uic_cmd; |
| 2003 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2004 | /* Write Args */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2005 | ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1); |
| 2006 | ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2); |
| 2007 | ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2008 | |
| 2009 | /* Write UIC Cmd */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2010 | ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK, |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 2011 | REG_UIC_COMMAND); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2012 | } |
| 2013 | |
| 2014 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2015 | * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command |
| 2016 | * @hba: per adapter instance |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2017 | * @uic_cmd: UIC command |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2018 | * |
| 2019 | * Must be called with mutex held. |
| 2020 | * Returns 0 only if success. |
| 2021 | */ |
| 2022 | static int |
| 2023 | ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) |
| 2024 | { |
| 2025 | int ret; |
| 2026 | unsigned long flags; |
| 2027 | |
| 2028 | if (wait_for_completion_timeout(&uic_cmd->done, |
| 2029 | msecs_to_jiffies(UIC_CMD_TIMEOUT))) |
| 2030 | ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT; |
| 2031 | else |
| 2032 | ret = -ETIMEDOUT; |
| 2033 | |
| 2034 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2035 | hba->active_uic_cmd = NULL; |
| 2036 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2037 | |
| 2038 | return ret; |
| 2039 | } |
| 2040 | |
| 2041 | /** |
| 2042 | * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result |
| 2043 | * @hba: per adapter instance |
| 2044 | * @uic_cmd: UIC command |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2045 | * @completion: initialize the completion only if this is set to true |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2046 | * |
| 2047 | * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2048 | * with mutex held and host_lock locked. |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2049 | * Returns 0 only if success. |
| 2050 | */ |
| 2051 | static int |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2052 | __ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd, |
| 2053 | bool completion) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2054 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2055 | if (!ufshcd_ready_for_uic_cmd(hba)) { |
| 2056 | dev_err(hba->dev, |
| 2057 | "Controller not ready to accept UIC commands\n"); |
| 2058 | return -EIO; |
| 2059 | } |
| 2060 | |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2061 | if (completion) |
| 2062 | init_completion(&uic_cmd->done); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2063 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2064 | ufshcd_dispatch_uic_cmd(hba, uic_cmd); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2065 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2066 | return 0; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | /** |
| 2070 | * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result |
| 2071 | * @hba: per adapter instance |
| 2072 | * @uic_cmd: UIC command |
| 2073 | * |
| 2074 | * Returns 0 only if success. |
| 2075 | */ |
Avri Altman | e77044c5 | 2018-10-07 17:30:39 +0300 | [diff] [blame] | 2076 | 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] | 2077 | { |
| 2078 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2079 | unsigned long flags; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2080 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2081 | ufshcd_hold(hba, false); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2082 | mutex_lock(&hba->uic_cmd_mutex); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 2083 | ufshcd_add_delay_before_dme_cmd(hba); |
| 2084 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2085 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2086 | ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2087 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2088 | if (!ret) |
| 2089 | ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd); |
| 2090 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2091 | mutex_unlock(&hba->uic_cmd_mutex); |
| 2092 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2093 | ufshcd_release(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2094 | return ret; |
| 2095 | } |
| 2096 | |
| 2097 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2098 | * ufshcd_map_sg - Map scatter-gather list to prdt |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2099 | * @hba: per adapter instance |
| 2100 | * @lrbp: pointer to local reference block |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2101 | * |
| 2102 | * Returns 0 in case of success, non-zero value in case of failure |
| 2103 | */ |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 2104 | 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] | 2105 | { |
| 2106 | struct ufshcd_sg_entry *prd_table; |
| 2107 | struct scatterlist *sg; |
| 2108 | struct scsi_cmnd *cmd; |
| 2109 | int sg_segments; |
| 2110 | int i; |
| 2111 | |
| 2112 | cmd = lrbp->cmd; |
| 2113 | sg_segments = scsi_dma_map(cmd); |
| 2114 | if (sg_segments < 0) |
| 2115 | return sg_segments; |
| 2116 | |
| 2117 | if (sg_segments) { |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 2118 | if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) |
| 2119 | lrbp->utr_descriptor_ptr->prd_table_length = |
| 2120 | cpu_to_le16((u16)(sg_segments * |
| 2121 | sizeof(struct ufshcd_sg_entry))); |
| 2122 | else |
| 2123 | lrbp->utr_descriptor_ptr->prd_table_length = |
| 2124 | cpu_to_le16((u16) (sg_segments)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2125 | |
| 2126 | prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr; |
| 2127 | |
| 2128 | scsi_for_each_sg(cmd, sg, sg_segments, i) { |
| 2129 | prd_table[i].size = |
| 2130 | cpu_to_le32(((u32) sg_dma_len(sg))-1); |
| 2131 | prd_table[i].base_addr = |
| 2132 | cpu_to_le32(lower_32_bits(sg->dma_address)); |
| 2133 | prd_table[i].upper_addr = |
| 2134 | cpu_to_le32(upper_32_bits(sg->dma_address)); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2135 | prd_table[i].reserved = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2136 | } |
| 2137 | } else { |
| 2138 | lrbp->utr_descriptor_ptr->prd_table_length = 0; |
| 2139 | } |
| 2140 | |
| 2141 | return 0; |
| 2142 | } |
| 2143 | |
| 2144 | /** |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2145 | * ufshcd_enable_intr - enable interrupts |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2146 | * @hba: per adapter instance |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2147 | * @intrs: interrupt bits |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2148 | */ |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2149 | static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2150 | { |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2151 | u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
| 2152 | |
| 2153 | if (hba->ufs_version == UFSHCI_VERSION_10) { |
| 2154 | u32 rw; |
| 2155 | rw = set & INTERRUPT_MASK_RW_VER_10; |
| 2156 | set = rw | ((set ^ intrs) & intrs); |
| 2157 | } else { |
| 2158 | set |= intrs; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2159 | } |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2160 | |
| 2161 | ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE); |
| 2162 | } |
| 2163 | |
| 2164 | /** |
| 2165 | * ufshcd_disable_intr - disable interrupts |
| 2166 | * @hba: per adapter instance |
| 2167 | * @intrs: interrupt bits |
| 2168 | */ |
| 2169 | static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs) |
| 2170 | { |
| 2171 | u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
| 2172 | |
| 2173 | if (hba->ufs_version == UFSHCI_VERSION_10) { |
| 2174 | u32 rw; |
| 2175 | rw = (set & INTERRUPT_MASK_RW_VER_10) & |
| 2176 | ~(intrs & INTERRUPT_MASK_RW_VER_10); |
| 2177 | set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10); |
| 2178 | |
| 2179 | } else { |
| 2180 | set &= ~intrs; |
| 2181 | } |
| 2182 | |
| 2183 | ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2184 | } |
| 2185 | |
| 2186 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2187 | * ufshcd_prepare_req_desc_hdr() - Fills the requests header |
| 2188 | * descriptor according to request |
| 2189 | * @lrbp: pointer to local reference block |
| 2190 | * @upiu_flags: flags required in the header |
| 2191 | * @cmd_dir: requests data direction |
| 2192 | */ |
| 2193 | static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp, |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2194 | u32 *upiu_flags, enum dma_data_direction cmd_dir) |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2195 | { |
| 2196 | struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr; |
| 2197 | u32 data_direction; |
| 2198 | u32 dword_0; |
| 2199 | |
| 2200 | if (cmd_dir == DMA_FROM_DEVICE) { |
| 2201 | data_direction = UTP_DEVICE_TO_HOST; |
| 2202 | *upiu_flags = UPIU_CMD_FLAGS_READ; |
| 2203 | } else if (cmd_dir == DMA_TO_DEVICE) { |
| 2204 | data_direction = UTP_HOST_TO_DEVICE; |
| 2205 | *upiu_flags = UPIU_CMD_FLAGS_WRITE; |
| 2206 | } else { |
| 2207 | data_direction = UTP_NO_DATA_TRANSFER; |
| 2208 | *upiu_flags = UPIU_CMD_FLAGS_NONE; |
| 2209 | } |
| 2210 | |
| 2211 | dword_0 = data_direction | (lrbp->command_type |
| 2212 | << UPIU_COMMAND_TYPE_OFFSET); |
| 2213 | if (lrbp->intr_cmd) |
| 2214 | dword_0 |= UTP_REQ_DESC_INT_CMD; |
| 2215 | |
| 2216 | /* Transfer request descriptor header fields */ |
| 2217 | req_desc->header.dword_0 = cpu_to_le32(dword_0); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2218 | /* dword_1 is reserved, hence it is set to 0 */ |
| 2219 | req_desc->header.dword_1 = 0; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2220 | /* |
| 2221 | * assigning invalid value for command status. Controller |
| 2222 | * updates OCS on command completion, with the command |
| 2223 | * status |
| 2224 | */ |
| 2225 | req_desc->header.dword_2 = |
| 2226 | cpu_to_le32(OCS_INVALID_COMMAND_STATUS); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2227 | /* dword_3 is reserved, hence it is set to 0 */ |
| 2228 | req_desc->header.dword_3 = 0; |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 2229 | |
| 2230 | req_desc->prd_table_length = 0; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2231 | } |
| 2232 | |
| 2233 | /** |
| 2234 | * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc, |
| 2235 | * for scsi commands |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2236 | * @lrbp: local reference block pointer |
| 2237 | * @upiu_flags: flags |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2238 | */ |
| 2239 | static |
| 2240 | void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags) |
| 2241 | { |
| 2242 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2243 | unsigned short cdb_len; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2244 | |
| 2245 | /* command descriptor fields */ |
| 2246 | ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD( |
| 2247 | UPIU_TRANSACTION_COMMAND, upiu_flags, |
| 2248 | lrbp->lun, lrbp->task_tag); |
| 2249 | ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD( |
| 2250 | UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0); |
| 2251 | |
| 2252 | /* Total EHS length and Data segment length will be zero */ |
| 2253 | ucd_req_ptr->header.dword_2 = 0; |
| 2254 | |
| 2255 | ucd_req_ptr->sc.exp_data_transfer_len = |
| 2256 | cpu_to_be32(lrbp->cmd->sdb.length); |
| 2257 | |
Avri Altman | a851b2b | 2018-10-07 17:30:34 +0300 | [diff] [blame] | 2258 | cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, UFS_CDB_SIZE); |
| 2259 | memset(ucd_req_ptr->sc.cdb, 0, UFS_CDB_SIZE); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2260 | memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len); |
| 2261 | |
| 2262 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2263 | } |
| 2264 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2265 | /** |
| 2266 | * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc, |
| 2267 | * for query requsts |
| 2268 | * @hba: UFS hba |
| 2269 | * @lrbp: local reference block pointer |
| 2270 | * @upiu_flags: flags |
| 2271 | */ |
| 2272 | static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba, |
| 2273 | struct ufshcd_lrb *lrbp, u32 upiu_flags) |
| 2274 | { |
| 2275 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
| 2276 | struct ufs_query *query = &hba->dev_cmd.query; |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 2277 | u16 len = be16_to_cpu(query->request.upiu_req.length); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2278 | |
| 2279 | /* Query request header */ |
| 2280 | ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD( |
| 2281 | UPIU_TRANSACTION_QUERY_REQ, upiu_flags, |
| 2282 | lrbp->lun, lrbp->task_tag); |
| 2283 | ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD( |
| 2284 | 0, query->request.query_func, 0, 0); |
| 2285 | |
Zang Leigang | 6861285 | 2016-08-25 17:39:19 +0800 | [diff] [blame] | 2286 | /* Data segment length only need for WRITE_DESC */ |
| 2287 | if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) |
| 2288 | ucd_req_ptr->header.dword_2 = |
| 2289 | UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len); |
| 2290 | else |
| 2291 | ucd_req_ptr->header.dword_2 = 0; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2292 | |
| 2293 | /* Copy the Query Request buffer as is */ |
| 2294 | memcpy(&ucd_req_ptr->qr, &query->request.upiu_req, |
| 2295 | QUERY_OSF_SIZE); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2296 | |
| 2297 | /* Copy the Descriptor */ |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2298 | if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) |
Avri Altman | 220d17a6 | 2018-10-07 17:30:36 +0300 | [diff] [blame] | 2299 | memcpy(ucd_req_ptr + 1, query->descriptor, len); |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2300 | |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 2301 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2302 | } |
| 2303 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2304 | static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp) |
| 2305 | { |
| 2306 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
| 2307 | |
| 2308 | memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req)); |
| 2309 | |
| 2310 | /* command descriptor fields */ |
| 2311 | ucd_req_ptr->header.dword_0 = |
| 2312 | UPIU_HEADER_DWORD( |
| 2313 | UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag); |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 2314 | /* clear rest of the fields of basic header */ |
| 2315 | ucd_req_ptr->header.dword_1 = 0; |
| 2316 | ucd_req_ptr->header.dword_2 = 0; |
| 2317 | |
| 2318 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2319 | } |
| 2320 | |
| 2321 | /** |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2322 | * ufshcd_comp_devman_upiu - UFS Protocol Information Unit(UPIU) |
| 2323 | * for Device Management Purposes |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2324 | * @hba: per adapter instance |
| 2325 | * @lrbp: pointer to local reference block |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2326 | */ |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2327 | static int ufshcd_comp_devman_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2328 | { |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2329 | u32 upiu_flags; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2330 | int ret = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2331 | |
kehuanlin | 83dc7e3 | 2017-09-06 17:58:39 +0800 | [diff] [blame] | 2332 | if ((hba->ufs_version == UFSHCI_VERSION_10) || |
| 2333 | (hba->ufs_version == UFSHCI_VERSION_11)) |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2334 | lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE; |
kehuanlin | 83dc7e3 | 2017-09-06 17:58:39 +0800 | [diff] [blame] | 2335 | else |
| 2336 | lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE; |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2337 | |
| 2338 | ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE); |
| 2339 | if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY) |
| 2340 | ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags); |
| 2341 | else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP) |
| 2342 | ufshcd_prepare_utp_nop_upiu(lrbp); |
| 2343 | else |
| 2344 | ret = -EINVAL; |
| 2345 | |
| 2346 | return ret; |
| 2347 | } |
| 2348 | |
| 2349 | /** |
| 2350 | * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU) |
| 2351 | * for SCSI Purposes |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2352 | * @hba: per adapter instance |
| 2353 | * @lrbp: pointer to local reference block |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2354 | */ |
| 2355 | static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 2356 | { |
| 2357 | u32 upiu_flags; |
| 2358 | int ret = 0; |
| 2359 | |
kehuanlin | 83dc7e3 | 2017-09-06 17:58:39 +0800 | [diff] [blame] | 2360 | if ((hba->ufs_version == UFSHCI_VERSION_10) || |
| 2361 | (hba->ufs_version == UFSHCI_VERSION_11)) |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2362 | lrbp->command_type = UTP_CMD_TYPE_SCSI; |
kehuanlin | 83dc7e3 | 2017-09-06 17:58:39 +0800 | [diff] [blame] | 2363 | else |
| 2364 | lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE; |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2365 | |
| 2366 | if (likely(lrbp->cmd)) { |
| 2367 | ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, |
| 2368 | lrbp->cmd->sc_data_direction); |
| 2369 | ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags); |
| 2370 | } else { |
| 2371 | ret = -EINVAL; |
| 2372 | } |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2373 | |
| 2374 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2375 | } |
| 2376 | |
| 2377 | /** |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 2378 | * 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] | 2379 | * @upiu_wlun_id: UPIU W-LUN id |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 2380 | * |
| 2381 | * Returns SCSI W-LUN id |
| 2382 | */ |
| 2383 | static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id) |
| 2384 | { |
| 2385 | return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE; |
| 2386 | } |
| 2387 | |
| 2388 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2389 | * ufshcd_queuecommand - main entry point for SCSI requests |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2390 | * @host: SCSI host pointer |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2391 | * @cmd: command from SCSI Midlayer |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2392 | * |
| 2393 | * Returns 0 for success, non-zero in case of failure |
| 2394 | */ |
| 2395 | static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) |
| 2396 | { |
| 2397 | struct ufshcd_lrb *lrbp; |
| 2398 | struct ufs_hba *hba; |
| 2399 | unsigned long flags; |
| 2400 | int tag; |
| 2401 | int err = 0; |
| 2402 | |
| 2403 | hba = shost_priv(host); |
| 2404 | |
| 2405 | tag = cmd->request->tag; |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 2406 | if (!ufshcd_valid_tag(hba, tag)) { |
| 2407 | dev_err(hba->dev, |
| 2408 | "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p", |
| 2409 | __func__, tag, cmd, cmd->request); |
| 2410 | BUG(); |
| 2411 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2412 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 2413 | if (!down_read_trylock(&hba->clk_scaling_lock)) |
| 2414 | return SCSI_MLQUEUE_HOST_BUSY; |
| 2415 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2416 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2417 | switch (hba->ufshcd_state) { |
| 2418 | case UFSHCD_STATE_OPERATIONAL: |
| 2419 | break; |
Zang Leigang | 141f816 | 2016-11-16 11:29:37 +0800 | [diff] [blame] | 2420 | case UFSHCD_STATE_EH_SCHEDULED: |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2421 | case UFSHCD_STATE_RESET: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2422 | err = SCSI_MLQUEUE_HOST_BUSY; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2423 | goto out_unlock; |
| 2424 | case UFSHCD_STATE_ERROR: |
| 2425 | set_host_byte(cmd, DID_ERROR); |
| 2426 | cmd->scsi_done(cmd); |
| 2427 | goto out_unlock; |
| 2428 | default: |
| 2429 | dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n", |
| 2430 | __func__, hba->ufshcd_state); |
| 2431 | set_host_byte(cmd, DID_BAD_TARGET); |
| 2432 | cmd->scsi_done(cmd); |
| 2433 | goto out_unlock; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2434 | } |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 2435 | |
| 2436 | /* if error handling is in progress, don't issue commands */ |
| 2437 | if (ufshcd_eh_in_progress(hba)) { |
| 2438 | set_host_byte(cmd, DID_ERROR); |
| 2439 | cmd->scsi_done(cmd); |
| 2440 | goto out_unlock; |
| 2441 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2442 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2443 | |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 2444 | hba->req_abort_count = 0; |
| 2445 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2446 | /* acquire the tag to make sure device cmds don't use it */ |
| 2447 | if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) { |
| 2448 | /* |
| 2449 | * Dev manage command in progress, requeue the command. |
| 2450 | * Requeuing the command helps in cases where the request *may* |
| 2451 | * find different tag instead of waiting for dev manage command |
| 2452 | * completion. |
| 2453 | */ |
| 2454 | err = SCSI_MLQUEUE_HOST_BUSY; |
| 2455 | goto out; |
| 2456 | } |
| 2457 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2458 | err = ufshcd_hold(hba, true); |
| 2459 | if (err) { |
| 2460 | err = SCSI_MLQUEUE_HOST_BUSY; |
| 2461 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 2462 | goto out; |
| 2463 | } |
| 2464 | WARN_ON(hba->clk_gating.state != CLKS_ON); |
| 2465 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2466 | lrbp = &hba->lrb[tag]; |
| 2467 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2468 | WARN_ON(lrbp->cmd); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2469 | lrbp->cmd = cmd; |
Avri Altman | 09a5a24 | 2018-11-22 20:04:56 +0200 | [diff] [blame] | 2470 | lrbp->sense_bufflen = UFS_SENSE_SIZE; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2471 | lrbp->sense_buffer = cmd->sense_buffer; |
| 2472 | lrbp->task_tag = tag; |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 2473 | lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun); |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 2474 | lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false; |
Gilad Broner | e0b299e | 2017-02-03 16:56:40 -0800 | [diff] [blame] | 2475 | lrbp->req_abort_skip = false; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2476 | |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2477 | ufshcd_comp_scsi_upiu(hba, lrbp); |
| 2478 | |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 2479 | err = ufshcd_map_sg(hba, lrbp); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2480 | if (err) { |
| 2481 | lrbp->cmd = NULL; |
| 2482 | clear_bit_unlock(tag, &hba->lrb_in_use); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2483 | goto out; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2484 | } |
Gilad Broner | ad1a1b9 | 2016-10-17 17:09:36 -0700 | [diff] [blame] | 2485 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 2486 | wmb(); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2487 | |
| 2488 | /* issue command to the controller */ |
| 2489 | spin_lock_irqsave(hba->host->host_lock, flags); |
Kiwoong Kim | 0e675ef | 2016-11-10 21:14:36 +0900 | [diff] [blame] | 2490 | ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2491 | ufshcd_send_command(hba, tag); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2492 | out_unlock: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2493 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2494 | out: |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 2495 | up_read(&hba->clk_scaling_lock); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2496 | return err; |
| 2497 | } |
| 2498 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2499 | static int ufshcd_compose_dev_cmd(struct ufs_hba *hba, |
| 2500 | struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag) |
| 2501 | { |
| 2502 | lrbp->cmd = NULL; |
| 2503 | lrbp->sense_bufflen = 0; |
| 2504 | lrbp->sense_buffer = NULL; |
| 2505 | lrbp->task_tag = tag; |
| 2506 | 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] | 2507 | lrbp->intr_cmd = true; /* No interrupt aggregation */ |
| 2508 | hba->dev_cmd.type = cmd_type; |
| 2509 | |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2510 | return ufshcd_comp_devman_upiu(hba, lrbp); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2511 | } |
| 2512 | |
| 2513 | static int |
| 2514 | ufshcd_clear_cmd(struct ufs_hba *hba, int tag) |
| 2515 | { |
| 2516 | int err = 0; |
| 2517 | unsigned long flags; |
| 2518 | u32 mask = 1 << tag; |
| 2519 | |
| 2520 | /* clear outstanding transaction before retry */ |
| 2521 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2522 | ufshcd_utrl_clear(hba, tag); |
| 2523 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2524 | |
| 2525 | /* |
| 2526 | * wait for for h/w to clear corresponding bit in door-bell. |
| 2527 | * max. wait is 1 sec. |
| 2528 | */ |
| 2529 | err = ufshcd_wait_for_register(hba, |
| 2530 | REG_UTP_TRANSFER_REQ_DOOR_BELL, |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 2531 | mask, ~mask, 1000, 1000, true); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2532 | |
| 2533 | return err; |
| 2534 | } |
| 2535 | |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2536 | static int |
| 2537 | ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 2538 | { |
| 2539 | struct ufs_query_res *query_res = &hba->dev_cmd.query.response; |
| 2540 | |
| 2541 | /* Get the UPIU response */ |
| 2542 | query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >> |
| 2543 | UPIU_RSP_CODE_OFFSET; |
| 2544 | return query_res->response; |
| 2545 | } |
| 2546 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2547 | /** |
| 2548 | * ufshcd_dev_cmd_completion() - handles device management command responses |
| 2549 | * @hba: per adapter instance |
| 2550 | * @lrbp: pointer to local reference block |
| 2551 | */ |
| 2552 | static int |
| 2553 | ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 2554 | { |
| 2555 | int resp; |
| 2556 | int err = 0; |
| 2557 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 2558 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2559 | resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr); |
| 2560 | |
| 2561 | switch (resp) { |
| 2562 | case UPIU_TRANSACTION_NOP_IN: |
| 2563 | if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) { |
| 2564 | err = -EINVAL; |
| 2565 | dev_err(hba->dev, "%s: unexpected response %x\n", |
| 2566 | __func__, resp); |
| 2567 | } |
| 2568 | break; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2569 | case UPIU_TRANSACTION_QUERY_RSP: |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2570 | err = ufshcd_check_query_response(hba, lrbp); |
| 2571 | if (!err) |
| 2572 | err = ufshcd_copy_query_response(hba, lrbp); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2573 | break; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2574 | case UPIU_TRANSACTION_REJECT_UPIU: |
| 2575 | /* TODO: handle Reject UPIU Response */ |
| 2576 | err = -EPERM; |
| 2577 | dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n", |
| 2578 | __func__); |
| 2579 | break; |
| 2580 | default: |
| 2581 | err = -EINVAL; |
| 2582 | dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n", |
| 2583 | __func__, resp); |
| 2584 | break; |
| 2585 | } |
| 2586 | |
| 2587 | return err; |
| 2588 | } |
| 2589 | |
| 2590 | static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba, |
| 2591 | struct ufshcd_lrb *lrbp, int max_timeout) |
| 2592 | { |
| 2593 | int err = 0; |
| 2594 | unsigned long time_left; |
| 2595 | unsigned long flags; |
| 2596 | |
| 2597 | time_left = wait_for_completion_timeout(hba->dev_cmd.complete, |
| 2598 | msecs_to_jiffies(max_timeout)); |
| 2599 | |
Gilad Broner | ad1a1b9 | 2016-10-17 17:09:36 -0700 | [diff] [blame] | 2600 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 2601 | wmb(); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2602 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2603 | hba->dev_cmd.complete = NULL; |
| 2604 | if (likely(time_left)) { |
| 2605 | err = ufshcd_get_tr_ocs(lrbp); |
| 2606 | if (!err) |
| 2607 | err = ufshcd_dev_cmd_completion(hba, lrbp); |
| 2608 | } |
| 2609 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2610 | |
| 2611 | if (!time_left) { |
| 2612 | err = -ETIMEDOUT; |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 2613 | dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n", |
| 2614 | __func__, lrbp->task_tag); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2615 | if (!ufshcd_clear_cmd(hba, lrbp->task_tag)) |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 2616 | /* successfully cleared the command, retry if needed */ |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2617 | err = -EAGAIN; |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 2618 | /* |
| 2619 | * in case of an error, after clearing the doorbell, |
| 2620 | * we also need to clear the outstanding_request |
| 2621 | * field in hba |
| 2622 | */ |
| 2623 | ufshcd_outstanding_req_clear(hba, lrbp->task_tag); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2624 | } |
| 2625 | |
| 2626 | return err; |
| 2627 | } |
| 2628 | |
| 2629 | /** |
| 2630 | * ufshcd_get_dev_cmd_tag - Get device management command tag |
| 2631 | * @hba: per-adapter instance |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2632 | * @tag_out: pointer to variable with available slot value |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2633 | * |
| 2634 | * Get a free slot and lock it until device management command |
| 2635 | * completes. |
| 2636 | * |
| 2637 | * Returns false if free slot is unavailable for locking, else |
| 2638 | * return true with tag value in @tag. |
| 2639 | */ |
| 2640 | static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out) |
| 2641 | { |
| 2642 | int tag; |
| 2643 | bool ret = false; |
| 2644 | unsigned long tmp; |
| 2645 | |
| 2646 | if (!tag_out) |
| 2647 | goto out; |
| 2648 | |
| 2649 | do { |
| 2650 | tmp = ~hba->lrb_in_use; |
| 2651 | tag = find_last_bit(&tmp, hba->nutrs); |
| 2652 | if (tag >= hba->nutrs) |
| 2653 | goto out; |
| 2654 | } while (test_and_set_bit_lock(tag, &hba->lrb_in_use)); |
| 2655 | |
| 2656 | *tag_out = tag; |
| 2657 | ret = true; |
| 2658 | out: |
| 2659 | return ret; |
| 2660 | } |
| 2661 | |
| 2662 | static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag) |
| 2663 | { |
| 2664 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 2665 | } |
| 2666 | |
| 2667 | /** |
| 2668 | * ufshcd_exec_dev_cmd - API for sending device management requests |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2669 | * @hba: UFS hba |
| 2670 | * @cmd_type: specifies the type (NOP, Query...) |
| 2671 | * @timeout: time in seconds |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2672 | * |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2673 | * NOTE: Since there is only one available tag for device management commands, |
| 2674 | * it is expected you hold the hba->dev_cmd.lock mutex. |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2675 | */ |
| 2676 | static int ufshcd_exec_dev_cmd(struct ufs_hba *hba, |
| 2677 | enum dev_cmd_type cmd_type, int timeout) |
| 2678 | { |
| 2679 | struct ufshcd_lrb *lrbp; |
| 2680 | int err; |
| 2681 | int tag; |
| 2682 | struct completion wait; |
| 2683 | unsigned long flags; |
| 2684 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 2685 | down_read(&hba->clk_scaling_lock); |
| 2686 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2687 | /* |
| 2688 | * Get free slot, sleep if slots are unavailable. |
| 2689 | * Even though we use wait_event() which sleeps indefinitely, |
| 2690 | * the maximum wait time is bounded by SCSI request timeout. |
| 2691 | */ |
| 2692 | wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag)); |
| 2693 | |
| 2694 | init_completion(&wait); |
| 2695 | lrbp = &hba->lrb[tag]; |
| 2696 | WARN_ON(lrbp->cmd); |
| 2697 | err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag); |
| 2698 | if (unlikely(err)) |
| 2699 | goto out_put_tag; |
| 2700 | |
| 2701 | hba->dev_cmd.complete = &wait; |
| 2702 | |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 2703 | ufshcd_add_query_upiu_trace(hba, tag, "query_send"); |
Yaniv Gardi | e3dfdc5 | 2016-02-01 15:02:49 +0200 | [diff] [blame] | 2704 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 2705 | wmb(); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2706 | spin_lock_irqsave(hba->host->host_lock, flags); |
Kiwoong Kim | 0e675ef | 2016-11-10 21:14:36 +0900 | [diff] [blame] | 2707 | ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2708 | ufshcd_send_command(hba, tag); |
| 2709 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2710 | |
| 2711 | err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout); |
| 2712 | |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 2713 | ufshcd_add_query_upiu_trace(hba, tag, |
| 2714 | err ? "query_complete_err" : "query_complete"); |
| 2715 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2716 | out_put_tag: |
| 2717 | ufshcd_put_dev_cmd_tag(hba, tag); |
| 2718 | wake_up(&hba->dev_cmd.tag_wq); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 2719 | up_read(&hba->clk_scaling_lock); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2720 | return err; |
| 2721 | } |
| 2722 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2723 | /** |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2724 | * ufshcd_init_query() - init the query response and request parameters |
| 2725 | * @hba: per-adapter instance |
| 2726 | * @request: address of the request pointer to be initialized |
| 2727 | * @response: address of the response pointer to be initialized |
| 2728 | * @opcode: operation to perform |
| 2729 | * @idn: flag idn to access |
| 2730 | * @index: LU number to access |
| 2731 | * @selector: query/flag/descriptor further identification |
| 2732 | */ |
| 2733 | static inline void ufshcd_init_query(struct ufs_hba *hba, |
| 2734 | struct ufs_query_req **request, struct ufs_query_res **response, |
| 2735 | enum query_opcode opcode, u8 idn, u8 index, u8 selector) |
| 2736 | { |
| 2737 | *request = &hba->dev_cmd.query.request; |
| 2738 | *response = &hba->dev_cmd.query.response; |
| 2739 | memset(*request, 0, sizeof(struct ufs_query_req)); |
| 2740 | memset(*response, 0, sizeof(struct ufs_query_res)); |
| 2741 | (*request)->upiu_req.opcode = opcode; |
| 2742 | (*request)->upiu_req.idn = idn; |
| 2743 | (*request)->upiu_req.index = index; |
| 2744 | (*request)->upiu_req.selector = selector; |
| 2745 | } |
| 2746 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 2747 | static int ufshcd_query_flag_retry(struct ufs_hba *hba, |
| 2748 | enum query_opcode opcode, enum flag_idn idn, bool *flag_res) |
| 2749 | { |
| 2750 | int ret; |
| 2751 | int retries; |
| 2752 | |
| 2753 | for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) { |
| 2754 | ret = ufshcd_query_flag(hba, opcode, idn, flag_res); |
| 2755 | if (ret) |
| 2756 | dev_dbg(hba->dev, |
| 2757 | "%s: failed with error %d, retries %d\n", |
| 2758 | __func__, ret, retries); |
| 2759 | else |
| 2760 | break; |
| 2761 | } |
| 2762 | |
| 2763 | if (ret) |
| 2764 | dev_err(hba->dev, |
| 2765 | "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n", |
| 2766 | __func__, opcode, idn, ret, retries); |
| 2767 | return ret; |
| 2768 | } |
| 2769 | |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2770 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2771 | * ufshcd_query_flag() - API function for sending flag query requests |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2772 | * @hba: per-adapter instance |
| 2773 | * @opcode: flag query to perform |
| 2774 | * @idn: flag idn to access |
| 2775 | * @flag_res: the flag value after the query request completes |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2776 | * |
| 2777 | * Returns 0 for success, non-zero in case of failure |
| 2778 | */ |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 2779 | int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode, |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2780 | enum flag_idn idn, bool *flag_res) |
| 2781 | { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2782 | struct ufs_query_req *request = NULL; |
| 2783 | struct ufs_query_res *response = NULL; |
| 2784 | int err, index = 0, selector = 0; |
Yaniv Gardi | e5ad406 | 2016-02-01 15:02:41 +0200 | [diff] [blame] | 2785 | int timeout = QUERY_REQ_TIMEOUT; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2786 | |
| 2787 | BUG_ON(!hba); |
| 2788 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2789 | ufshcd_hold(hba, false); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2790 | mutex_lock(&hba->dev_cmd.lock); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2791 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 2792 | selector); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2793 | |
| 2794 | switch (opcode) { |
| 2795 | case UPIU_QUERY_OPCODE_SET_FLAG: |
| 2796 | case UPIU_QUERY_OPCODE_CLEAR_FLAG: |
| 2797 | case UPIU_QUERY_OPCODE_TOGGLE_FLAG: |
| 2798 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
| 2799 | break; |
| 2800 | case UPIU_QUERY_OPCODE_READ_FLAG: |
| 2801 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 2802 | if (!flag_res) { |
| 2803 | /* No dummy reads */ |
| 2804 | dev_err(hba->dev, "%s: Invalid argument for read request\n", |
| 2805 | __func__); |
| 2806 | err = -EINVAL; |
| 2807 | goto out_unlock; |
| 2808 | } |
| 2809 | break; |
| 2810 | default: |
| 2811 | dev_err(hba->dev, |
| 2812 | "%s: Expected query flag opcode but got = %d\n", |
| 2813 | __func__, opcode); |
| 2814 | err = -EINVAL; |
| 2815 | goto out_unlock; |
| 2816 | } |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2817 | |
Yaniv Gardi | e5ad406 | 2016-02-01 15:02:41 +0200 | [diff] [blame] | 2818 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2819 | |
| 2820 | if (err) { |
| 2821 | dev_err(hba->dev, |
| 2822 | "%s: Sending flag query for idn %d failed, err = %d\n", |
| 2823 | __func__, idn, err); |
| 2824 | goto out_unlock; |
| 2825 | } |
| 2826 | |
| 2827 | if (flag_res) |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 2828 | *flag_res = (be32_to_cpu(response->upiu_res.value) & |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2829 | MASK_QUERY_UPIU_FLAG_LOC) & 0x1; |
| 2830 | |
| 2831 | out_unlock: |
| 2832 | mutex_unlock(&hba->dev_cmd.lock); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2833 | ufshcd_release(hba); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2834 | return err; |
| 2835 | } |
| 2836 | |
| 2837 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2838 | * ufshcd_query_attr - API function for sending attribute requests |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 2839 | * @hba: per-adapter instance |
| 2840 | * @opcode: attribute opcode |
| 2841 | * @idn: attribute idn to access |
| 2842 | * @index: index field |
| 2843 | * @selector: selector field |
| 2844 | * @attr_val: the attribute value after the query request completes |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2845 | * |
| 2846 | * Returns 0 for success, non-zero in case of failure |
| 2847 | */ |
Stanislav Nijnikov | ec92b59 | 2018-02-15 14:14:11 +0200 | [diff] [blame] | 2848 | int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode, |
| 2849 | enum attr_idn idn, u8 index, u8 selector, u32 *attr_val) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2850 | { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2851 | struct ufs_query_req *request = NULL; |
| 2852 | struct ufs_query_res *response = NULL; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2853 | int err; |
| 2854 | |
| 2855 | BUG_ON(!hba); |
| 2856 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2857 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2858 | if (!attr_val) { |
| 2859 | dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n", |
| 2860 | __func__, opcode); |
| 2861 | err = -EINVAL; |
| 2862 | goto out; |
| 2863 | } |
| 2864 | |
| 2865 | mutex_lock(&hba->dev_cmd.lock); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2866 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 2867 | selector); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2868 | |
| 2869 | switch (opcode) { |
| 2870 | case UPIU_QUERY_OPCODE_WRITE_ATTR: |
| 2871 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 2872 | request->upiu_req.value = cpu_to_be32(*attr_val); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2873 | break; |
| 2874 | case UPIU_QUERY_OPCODE_READ_ATTR: |
| 2875 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 2876 | break; |
| 2877 | default: |
| 2878 | dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n", |
| 2879 | __func__, opcode); |
| 2880 | err = -EINVAL; |
| 2881 | goto out_unlock; |
| 2882 | } |
| 2883 | |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2884 | 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] | 2885 | |
| 2886 | if (err) { |
Yaniv Gardi | 4b761b5 | 2016-11-23 16:31:18 -0800 | [diff] [blame] | 2887 | dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n", |
| 2888 | __func__, opcode, idn, index, err); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2889 | goto out_unlock; |
| 2890 | } |
| 2891 | |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 2892 | *attr_val = be32_to_cpu(response->upiu_res.value); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2893 | |
| 2894 | out_unlock: |
| 2895 | mutex_unlock(&hba->dev_cmd.lock); |
| 2896 | out: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2897 | ufshcd_release(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2898 | return err; |
| 2899 | } |
| 2900 | |
| 2901 | /** |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 2902 | * ufshcd_query_attr_retry() - API function for sending query |
| 2903 | * attribute with retries |
| 2904 | * @hba: per-adapter instance |
| 2905 | * @opcode: attribute opcode |
| 2906 | * @idn: attribute idn to access |
| 2907 | * @index: index field |
| 2908 | * @selector: selector field |
| 2909 | * @attr_val: the attribute value after the query request |
| 2910 | * completes |
| 2911 | * |
| 2912 | * Returns 0 for success, non-zero in case of failure |
| 2913 | */ |
| 2914 | static int ufshcd_query_attr_retry(struct ufs_hba *hba, |
| 2915 | enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector, |
| 2916 | u32 *attr_val) |
| 2917 | { |
| 2918 | int ret = 0; |
| 2919 | u32 retries; |
| 2920 | |
| 2921 | for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { |
| 2922 | ret = ufshcd_query_attr(hba, opcode, idn, index, |
| 2923 | selector, attr_val); |
| 2924 | if (ret) |
| 2925 | dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n", |
| 2926 | __func__, ret, retries); |
| 2927 | else |
| 2928 | break; |
| 2929 | } |
| 2930 | |
| 2931 | if (ret) |
| 2932 | dev_err(hba->dev, |
| 2933 | "%s: query attribute, idn %d, failed with error %d after %d retires\n", |
| 2934 | __func__, idn, ret, QUERY_REQ_RETRIES); |
| 2935 | return ret; |
| 2936 | } |
| 2937 | |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 2938 | static int __ufshcd_query_descriptor(struct ufs_hba *hba, |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2939 | enum query_opcode opcode, enum desc_idn idn, u8 index, |
| 2940 | u8 selector, u8 *desc_buf, int *buf_len) |
| 2941 | { |
| 2942 | struct ufs_query_req *request = NULL; |
| 2943 | struct ufs_query_res *response = NULL; |
| 2944 | int err; |
| 2945 | |
| 2946 | BUG_ON(!hba); |
| 2947 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2948 | ufshcd_hold(hba, false); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2949 | if (!desc_buf) { |
| 2950 | dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n", |
| 2951 | __func__, opcode); |
| 2952 | err = -EINVAL; |
| 2953 | goto out; |
| 2954 | } |
| 2955 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 2956 | 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] | 2957 | dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n", |
| 2958 | __func__, *buf_len); |
| 2959 | err = -EINVAL; |
| 2960 | goto out; |
| 2961 | } |
| 2962 | |
| 2963 | mutex_lock(&hba->dev_cmd.lock); |
| 2964 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 2965 | selector); |
| 2966 | hba->dev_cmd.query.descriptor = desc_buf; |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 2967 | request->upiu_req.length = cpu_to_be16(*buf_len); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2968 | |
| 2969 | switch (opcode) { |
| 2970 | case UPIU_QUERY_OPCODE_WRITE_DESC: |
| 2971 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
| 2972 | break; |
| 2973 | case UPIU_QUERY_OPCODE_READ_DESC: |
| 2974 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 2975 | break; |
| 2976 | default: |
| 2977 | dev_err(hba->dev, |
| 2978 | "%s: Expected query descriptor opcode but got = 0x%.2x\n", |
| 2979 | __func__, opcode); |
| 2980 | err = -EINVAL; |
| 2981 | goto out_unlock; |
| 2982 | } |
| 2983 | |
| 2984 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT); |
| 2985 | |
| 2986 | if (err) { |
Yaniv Gardi | 4b761b5 | 2016-11-23 16:31:18 -0800 | [diff] [blame] | 2987 | dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n", |
| 2988 | __func__, opcode, idn, index, err); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2989 | goto out_unlock; |
| 2990 | } |
| 2991 | |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 2992 | *buf_len = be16_to_cpu(response->upiu_res.length); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2993 | |
| 2994 | out_unlock: |
Bean Huo | cfcbae3 | 2019-11-12 23:34:36 +0100 | [diff] [blame] | 2995 | hba->dev_cmd.query.descriptor = NULL; |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2996 | mutex_unlock(&hba->dev_cmd.lock); |
| 2997 | out: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2998 | ufshcd_release(hba); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2999 | return err; |
| 3000 | } |
| 3001 | |
| 3002 | /** |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 3003 | * ufshcd_query_descriptor_retry - API function for sending descriptor requests |
| 3004 | * @hba: per-adapter instance |
| 3005 | * @opcode: attribute opcode |
| 3006 | * @idn: attribute idn to access |
| 3007 | * @index: index field |
| 3008 | * @selector: selector field |
| 3009 | * @desc_buf: the buffer that contains the descriptor |
| 3010 | * @buf_len: length parameter passed to the device |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3011 | * |
| 3012 | * Returns 0 for success, non-zero in case of failure. |
| 3013 | * The buf_len parameter will contain, on return, the length parameter |
| 3014 | * received on the response. |
| 3015 | */ |
Stanislav Nijnikov | 2238d31 | 2018-02-15 14:14:07 +0200 | [diff] [blame] | 3016 | int ufshcd_query_descriptor_retry(struct ufs_hba *hba, |
| 3017 | enum query_opcode opcode, |
| 3018 | enum desc_idn idn, u8 index, |
| 3019 | u8 selector, |
| 3020 | u8 *desc_buf, int *buf_len) |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3021 | { |
| 3022 | int err; |
| 3023 | int retries; |
| 3024 | |
| 3025 | for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { |
| 3026 | err = __ufshcd_query_descriptor(hba, opcode, idn, index, |
| 3027 | selector, desc_buf, buf_len); |
| 3028 | if (!err || err == -EINVAL) |
| 3029 | break; |
| 3030 | } |
| 3031 | |
| 3032 | return err; |
| 3033 | } |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3034 | |
| 3035 | /** |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3036 | * ufshcd_read_desc_length - read the specified descriptor length from header |
| 3037 | * @hba: Pointer to adapter instance |
| 3038 | * @desc_id: descriptor idn value |
| 3039 | * @desc_index: descriptor index |
| 3040 | * @desc_length: pointer to variable to read the length of descriptor |
| 3041 | * |
| 3042 | * Return 0 in case of success, non-zero otherwise |
| 3043 | */ |
| 3044 | static int ufshcd_read_desc_length(struct ufs_hba *hba, |
| 3045 | enum desc_idn desc_id, |
| 3046 | int desc_index, |
| 3047 | int *desc_length) |
| 3048 | { |
| 3049 | int ret; |
| 3050 | u8 header[QUERY_DESC_HDR_SIZE]; |
| 3051 | int header_len = QUERY_DESC_HDR_SIZE; |
| 3052 | |
| 3053 | if (desc_id >= QUERY_DESC_IDN_MAX) |
| 3054 | return -EINVAL; |
| 3055 | |
| 3056 | ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC, |
| 3057 | desc_id, desc_index, 0, header, |
| 3058 | &header_len); |
| 3059 | |
| 3060 | if (ret) { |
| 3061 | dev_err(hba->dev, "%s: Failed to get descriptor header id %d", |
| 3062 | __func__, desc_id); |
| 3063 | return ret; |
| 3064 | } else if (desc_id != header[QUERY_DESC_DESC_TYPE_OFFSET]) { |
| 3065 | dev_warn(hba->dev, "%s: descriptor header id %d and desc_id %d mismatch", |
| 3066 | __func__, header[QUERY_DESC_DESC_TYPE_OFFSET], |
| 3067 | desc_id); |
| 3068 | ret = -EINVAL; |
| 3069 | } |
| 3070 | |
| 3071 | *desc_length = header[QUERY_DESC_LENGTH_OFFSET]; |
| 3072 | return ret; |
| 3073 | |
| 3074 | } |
| 3075 | |
| 3076 | /** |
| 3077 | * ufshcd_map_desc_id_to_length - map descriptor IDN to its length |
| 3078 | * @hba: Pointer to adapter instance |
| 3079 | * @desc_id: descriptor idn value |
| 3080 | * @desc_len: mapped desc length (out) |
| 3081 | * |
| 3082 | * Return 0 in case of success, non-zero otherwise |
| 3083 | */ |
| 3084 | int ufshcd_map_desc_id_to_length(struct ufs_hba *hba, |
| 3085 | enum desc_idn desc_id, int *desc_len) |
| 3086 | { |
| 3087 | switch (desc_id) { |
| 3088 | case QUERY_DESC_IDN_DEVICE: |
| 3089 | *desc_len = hba->desc_size.dev_desc; |
| 3090 | break; |
| 3091 | case QUERY_DESC_IDN_POWER: |
| 3092 | *desc_len = hba->desc_size.pwr_desc; |
| 3093 | break; |
| 3094 | case QUERY_DESC_IDN_GEOMETRY: |
| 3095 | *desc_len = hba->desc_size.geom_desc; |
| 3096 | break; |
| 3097 | case QUERY_DESC_IDN_CONFIGURATION: |
| 3098 | *desc_len = hba->desc_size.conf_desc; |
| 3099 | break; |
| 3100 | case QUERY_DESC_IDN_UNIT: |
| 3101 | *desc_len = hba->desc_size.unit_desc; |
| 3102 | break; |
| 3103 | case QUERY_DESC_IDN_INTERCONNECT: |
| 3104 | *desc_len = hba->desc_size.interc_desc; |
| 3105 | break; |
| 3106 | case QUERY_DESC_IDN_STRING: |
| 3107 | *desc_len = QUERY_DESC_MAX_SIZE; |
| 3108 | break; |
Stanislav Nijnikov | c648c2d | 2018-02-15 14:14:05 +0200 | [diff] [blame] | 3109 | case QUERY_DESC_IDN_HEALTH: |
| 3110 | *desc_len = hba->desc_size.hlth_desc; |
| 3111 | break; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3112 | case QUERY_DESC_IDN_RFU_0: |
| 3113 | case QUERY_DESC_IDN_RFU_1: |
| 3114 | *desc_len = 0; |
| 3115 | break; |
| 3116 | default: |
| 3117 | *desc_len = 0; |
| 3118 | return -EINVAL; |
| 3119 | } |
| 3120 | return 0; |
| 3121 | } |
| 3122 | EXPORT_SYMBOL(ufshcd_map_desc_id_to_length); |
| 3123 | |
| 3124 | /** |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3125 | * ufshcd_read_desc_param - read the specified descriptor parameter |
| 3126 | * @hba: Pointer to adapter instance |
| 3127 | * @desc_id: descriptor idn value |
| 3128 | * @desc_index: descriptor index |
| 3129 | * @param_offset: offset of the parameter to read |
| 3130 | * @param_read_buf: pointer to buffer where parameter would be read |
| 3131 | * @param_size: sizeof(param_read_buf) |
| 3132 | * |
| 3133 | * Return 0 in case of success, non-zero otherwise |
| 3134 | */ |
Stanislav Nijnikov | 45bced8 | 2018-02-15 14:14:02 +0200 | [diff] [blame] | 3135 | int ufshcd_read_desc_param(struct ufs_hba *hba, |
| 3136 | enum desc_idn desc_id, |
| 3137 | int desc_index, |
| 3138 | u8 param_offset, |
| 3139 | u8 *param_read_buf, |
| 3140 | u8 param_size) |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3141 | { |
| 3142 | int ret; |
| 3143 | u8 *desc_buf; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3144 | int buff_len; |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3145 | bool is_kmalloc = true; |
| 3146 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3147 | /* Safety check */ |
| 3148 | if (desc_id >= QUERY_DESC_IDN_MAX || !param_size) |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3149 | return -EINVAL; |
| 3150 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3151 | /* Get the max length of descriptor from structure filled up at probe |
| 3152 | * time. |
| 3153 | */ |
| 3154 | ret = ufshcd_map_desc_id_to_length(hba, desc_id, &buff_len); |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3155 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3156 | /* Sanity checks */ |
| 3157 | if (ret || !buff_len) { |
| 3158 | dev_err(hba->dev, "%s: Failed to get full descriptor length", |
| 3159 | __func__); |
| 3160 | return ret; |
| 3161 | } |
| 3162 | |
| 3163 | /* Check whether we need temp memory */ |
| 3164 | if (param_offset != 0 || param_size < buff_len) { |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3165 | desc_buf = kmalloc(buff_len, GFP_KERNEL); |
| 3166 | if (!desc_buf) |
| 3167 | return -ENOMEM; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3168 | } else { |
| 3169 | desc_buf = param_read_buf; |
| 3170 | is_kmalloc = false; |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3171 | } |
| 3172 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3173 | /* Request for full descriptor */ |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3174 | ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC, |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3175 | desc_id, desc_index, 0, |
| 3176 | desc_buf, &buff_len); |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3177 | |
subhashj@codeaurora.org | bde44bb | 2016-11-23 16:31:41 -0800 | [diff] [blame] | 3178 | if (ret) { |
| 3179 | dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d", |
| 3180 | __func__, desc_id, desc_index, param_offset, ret); |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3181 | goto out; |
| 3182 | } |
| 3183 | |
subhashj@codeaurora.org | bde44bb | 2016-11-23 16:31:41 -0800 | [diff] [blame] | 3184 | /* Sanity check */ |
| 3185 | if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) { |
| 3186 | dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header", |
| 3187 | __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]); |
| 3188 | ret = -EINVAL; |
| 3189 | goto out; |
| 3190 | } |
| 3191 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3192 | /* Check wherher we will not copy more data, than available */ |
| 3193 | if (is_kmalloc && param_size > buff_len) |
| 3194 | param_size = buff_len; |
subhashj@codeaurora.org | bde44bb | 2016-11-23 16:31:41 -0800 | [diff] [blame] | 3195 | |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3196 | if (is_kmalloc) |
| 3197 | memcpy(param_read_buf, &desc_buf[param_offset], param_size); |
| 3198 | out: |
| 3199 | if (is_kmalloc) |
| 3200 | kfree(desc_buf); |
| 3201 | return ret; |
| 3202 | } |
| 3203 | |
| 3204 | static inline int ufshcd_read_desc(struct ufs_hba *hba, |
| 3205 | enum desc_idn desc_id, |
| 3206 | int desc_index, |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3207 | void *buf, |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3208 | u32 size) |
| 3209 | { |
| 3210 | return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size); |
| 3211 | } |
| 3212 | |
| 3213 | static inline int ufshcd_read_power_desc(struct ufs_hba *hba, |
| 3214 | u8 *buf, |
| 3215 | u32 size) |
| 3216 | { |
Szymon Mielczarek | dbd34a6 | 2017-03-29 08:19:21 +0200 | [diff] [blame] | 3217 | return ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size); |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3218 | } |
| 3219 | |
Tomas Winkler | 8209b6d | 2017-01-05 10:45:10 +0200 | [diff] [blame] | 3220 | static int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size) |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3221 | { |
| 3222 | return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size); |
| 3223 | } |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3224 | |
| 3225 | /** |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3226 | * struct uc_string_id - unicode string |
| 3227 | * |
| 3228 | * @len: size of this descriptor inclusive |
| 3229 | * @type: descriptor type |
| 3230 | * @uc: unicode string character |
| 3231 | */ |
| 3232 | struct uc_string_id { |
| 3233 | u8 len; |
| 3234 | u8 type; |
| 3235 | wchar_t uc[0]; |
| 3236 | } __packed; |
| 3237 | |
| 3238 | /* replace non-printable or non-ASCII characters with spaces */ |
| 3239 | static inline char ufshcd_remove_non_printable(u8 ch) |
| 3240 | { |
| 3241 | return (ch >= 0x20 && ch <= 0x7e) ? ch : ' '; |
| 3242 | } |
| 3243 | |
| 3244 | /** |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3245 | * ufshcd_read_string_desc - read string descriptor |
| 3246 | * @hba: pointer to adapter instance |
| 3247 | * @desc_index: descriptor index |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3248 | * @buf: pointer to buffer where descriptor would be read, |
| 3249 | * the caller should free the memory. |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3250 | * @ascii: if true convert from unicode to ascii characters |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3251 | * null terminated string. |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3252 | * |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3253 | * Return: |
| 3254 | * * string size on success. |
| 3255 | * * -ENOMEM: on allocation failure |
| 3256 | * * -EINVAL: on a wrong parameter |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3257 | */ |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3258 | int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index, |
| 3259 | u8 **buf, bool ascii) |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3260 | { |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3261 | struct uc_string_id *uc_str; |
| 3262 | u8 *str; |
| 3263 | int ret; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3264 | |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3265 | if (!buf) |
| 3266 | return -EINVAL; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3267 | |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3268 | uc_str = kzalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL); |
| 3269 | if (!uc_str) |
| 3270 | return -ENOMEM; |
| 3271 | |
| 3272 | ret = ufshcd_read_desc(hba, QUERY_DESC_IDN_STRING, |
| 3273 | desc_index, uc_str, |
| 3274 | QUERY_DESC_MAX_SIZE); |
| 3275 | if (ret < 0) { |
| 3276 | dev_err(hba->dev, "Reading String Desc failed after %d retries. err = %d\n", |
| 3277 | QUERY_REQ_RETRIES, ret); |
| 3278 | str = NULL; |
| 3279 | goto out; |
| 3280 | } |
| 3281 | |
| 3282 | if (uc_str->len <= QUERY_DESC_HDR_SIZE) { |
| 3283 | dev_dbg(hba->dev, "String Desc is of zero length\n"); |
| 3284 | str = NULL; |
| 3285 | ret = 0; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3286 | goto out; |
| 3287 | } |
| 3288 | |
| 3289 | if (ascii) { |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3290 | ssize_t ascii_len; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3291 | int i; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3292 | /* remove header and divide by 2 to move from UTF16 to UTF8 */ |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3293 | ascii_len = (uc_str->len - QUERY_DESC_HDR_SIZE) / 2 + 1; |
| 3294 | str = kzalloc(ascii_len, GFP_KERNEL); |
| 3295 | if (!str) { |
| 3296 | ret = -ENOMEM; |
Tiezhu Yang | fcbefc3 | 2016-06-25 12:35:22 +0800 | [diff] [blame] | 3297 | goto out; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3298 | } |
| 3299 | |
| 3300 | /* |
| 3301 | * the descriptor contains string in UTF16 format |
| 3302 | * we need to convert to utf-8 so it can be displayed |
| 3303 | */ |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3304 | ret = utf16s_to_utf8s(uc_str->uc, |
| 3305 | uc_str->len - QUERY_DESC_HDR_SIZE, |
| 3306 | UTF16_BIG_ENDIAN, str, ascii_len); |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3307 | |
| 3308 | /* replace non-printable or non-ASCII characters with spaces */ |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3309 | for (i = 0; i < ret; i++) |
| 3310 | str[i] = ufshcd_remove_non_printable(str[i]); |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3311 | |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3312 | str[ret++] = '\0'; |
| 3313 | |
| 3314 | } else { |
YueHaibing | 5f57704 | 2019-08-31 12:44:24 +0000 | [diff] [blame] | 3315 | str = kmemdup(uc_str, uc_str->len, GFP_KERNEL); |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3316 | if (!str) { |
| 3317 | ret = -ENOMEM; |
| 3318 | goto out; |
| 3319 | } |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3320 | ret = uc_str->len; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3321 | } |
| 3322 | out: |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 3323 | *buf = str; |
| 3324 | kfree(uc_str); |
| 3325 | return ret; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3326 | } |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3327 | |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3328 | /** |
| 3329 | * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter |
| 3330 | * @hba: Pointer to adapter instance |
| 3331 | * @lun: lun id |
| 3332 | * @param_offset: offset of the parameter to read |
| 3333 | * @param_read_buf: pointer to buffer where parameter would be read |
| 3334 | * @param_size: sizeof(param_read_buf) |
| 3335 | * |
| 3336 | * Return 0 in case of success, non-zero otherwise |
| 3337 | */ |
| 3338 | static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba, |
| 3339 | int lun, |
| 3340 | enum unit_desc_param param_offset, |
| 3341 | u8 *param_read_buf, |
| 3342 | u32 param_size) |
| 3343 | { |
| 3344 | /* |
| 3345 | * Unit descriptors are only available for general purpose LUs (LUN id |
| 3346 | * from 0 to 7) and RPMB Well known LU. |
| 3347 | */ |
Stanislav Nijnikov | d829fc8 | 2018-02-15 14:14:09 +0200 | [diff] [blame] | 3348 | if (!ufs_is_valid_unit_desc_lun(lun)) |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3349 | return -EOPNOTSUPP; |
| 3350 | |
| 3351 | return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun, |
| 3352 | param_offset, param_read_buf, param_size); |
| 3353 | } |
| 3354 | |
| 3355 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3356 | * ufshcd_memory_alloc - allocate memory for host memory space data structures |
| 3357 | * @hba: per adapter instance |
| 3358 | * |
| 3359 | * 1. Allocate DMA memory for Command Descriptor array |
| 3360 | * Each command descriptor consist of Command UPIU, Response UPIU and PRDT |
| 3361 | * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL). |
| 3362 | * 3. Allocate DMA memory for UTP Task Management Request Descriptor List |
| 3363 | * (UTMRDL) |
| 3364 | * 4. Allocate memory for local reference block(lrb). |
| 3365 | * |
| 3366 | * Returns 0 for success, non-zero in case of failure |
| 3367 | */ |
| 3368 | static int ufshcd_memory_alloc(struct ufs_hba *hba) |
| 3369 | { |
| 3370 | size_t utmrdl_size, utrdl_size, ucdl_size; |
| 3371 | |
| 3372 | /* Allocate memory for UTP command descriptors */ |
| 3373 | ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3374 | hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 3375 | ucdl_size, |
| 3376 | &hba->ucdl_dma_addr, |
| 3377 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3378 | |
| 3379 | /* |
| 3380 | * UFSHCI requires UTP command descriptor to be 128 byte aligned. |
| 3381 | * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE |
| 3382 | * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will |
| 3383 | * be aligned to 128 bytes as well |
| 3384 | */ |
| 3385 | if (!hba->ucdl_base_addr || |
| 3386 | WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3387 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3388 | "Command Descriptor Memory allocation failed\n"); |
| 3389 | goto out; |
| 3390 | } |
| 3391 | |
| 3392 | /* |
| 3393 | * Allocate memory for UTP Transfer descriptors |
| 3394 | * UFSHCI requires 1024 byte alignment of UTRD |
| 3395 | */ |
| 3396 | utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3397 | hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 3398 | utrdl_size, |
| 3399 | &hba->utrdl_dma_addr, |
| 3400 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3401 | if (!hba->utrdl_base_addr || |
| 3402 | WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3403 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3404 | "Transfer Descriptor Memory allocation failed\n"); |
| 3405 | goto out; |
| 3406 | } |
| 3407 | |
| 3408 | /* |
| 3409 | * Allocate memory for UTP Task Management descriptors |
| 3410 | * UFSHCI requires 1024 byte alignment of UTMRD |
| 3411 | */ |
| 3412 | utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs; |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3413 | hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 3414 | utmrdl_size, |
| 3415 | &hba->utmrdl_dma_addr, |
| 3416 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3417 | if (!hba->utmrdl_base_addr || |
| 3418 | WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3419 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3420 | "Task Management Descriptor Memory allocation failed\n"); |
| 3421 | goto out; |
| 3422 | } |
| 3423 | |
| 3424 | /* Allocate memory for local reference block */ |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 3425 | hba->lrb = devm_kcalloc(hba->dev, |
| 3426 | hba->nutrs, sizeof(struct ufshcd_lrb), |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3427 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3428 | if (!hba->lrb) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3429 | dev_err(hba->dev, "LRB Memory allocation failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3430 | goto out; |
| 3431 | } |
| 3432 | return 0; |
| 3433 | out: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3434 | return -ENOMEM; |
| 3435 | } |
| 3436 | |
| 3437 | /** |
| 3438 | * ufshcd_host_memory_configure - configure local reference block with |
| 3439 | * memory offsets |
| 3440 | * @hba: per adapter instance |
| 3441 | * |
| 3442 | * Configure Host memory space |
| 3443 | * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA |
| 3444 | * address. |
| 3445 | * 2. Update each UTRD with Response UPIU offset, Response UPIU length |
| 3446 | * and PRDT offset. |
| 3447 | * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT |
| 3448 | * into local reference block. |
| 3449 | */ |
| 3450 | static void ufshcd_host_memory_configure(struct ufs_hba *hba) |
| 3451 | { |
| 3452 | struct utp_transfer_cmd_desc *cmd_descp; |
| 3453 | struct utp_transfer_req_desc *utrdlp; |
| 3454 | dma_addr_t cmd_desc_dma_addr; |
| 3455 | dma_addr_t cmd_desc_element_addr; |
| 3456 | u16 response_offset; |
| 3457 | u16 prdt_offset; |
| 3458 | int cmd_desc_size; |
| 3459 | int i; |
| 3460 | |
| 3461 | utrdlp = hba->utrdl_base_addr; |
| 3462 | cmd_descp = hba->ucdl_base_addr; |
| 3463 | |
| 3464 | response_offset = |
| 3465 | offsetof(struct utp_transfer_cmd_desc, response_upiu); |
| 3466 | prdt_offset = |
| 3467 | offsetof(struct utp_transfer_cmd_desc, prd_table); |
| 3468 | |
| 3469 | cmd_desc_size = sizeof(struct utp_transfer_cmd_desc); |
| 3470 | cmd_desc_dma_addr = hba->ucdl_dma_addr; |
| 3471 | |
| 3472 | for (i = 0; i < hba->nutrs; i++) { |
| 3473 | /* Configure UTRD with command descriptor base address */ |
| 3474 | cmd_desc_element_addr = |
| 3475 | (cmd_desc_dma_addr + (cmd_desc_size * i)); |
| 3476 | utrdlp[i].command_desc_base_addr_lo = |
| 3477 | cpu_to_le32(lower_32_bits(cmd_desc_element_addr)); |
| 3478 | utrdlp[i].command_desc_base_addr_hi = |
| 3479 | cpu_to_le32(upper_32_bits(cmd_desc_element_addr)); |
| 3480 | |
| 3481 | /* Response upiu and prdt offset should be in double words */ |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 3482 | if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) { |
| 3483 | utrdlp[i].response_upiu_offset = |
| 3484 | cpu_to_le16(response_offset); |
| 3485 | utrdlp[i].prd_table_offset = |
| 3486 | cpu_to_le16(prdt_offset); |
| 3487 | utrdlp[i].response_upiu_length = |
| 3488 | cpu_to_le16(ALIGNED_UPIU_SIZE); |
| 3489 | } else { |
| 3490 | utrdlp[i].response_upiu_offset = |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3491 | cpu_to_le16((response_offset >> 2)); |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 3492 | utrdlp[i].prd_table_offset = |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3493 | cpu_to_le16((prdt_offset >> 2)); |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 3494 | utrdlp[i].response_upiu_length = |
Sujit Reddy Thumma | 3ca316c | 2013-06-26 22:39:30 +0530 | [diff] [blame] | 3495 | cpu_to_le16(ALIGNED_UPIU_SIZE >> 2); |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 3496 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3497 | |
| 3498 | hba->lrb[i].utr_descriptor_ptr = (utrdlp + i); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 3499 | hba->lrb[i].utrd_dma_addr = hba->utrdl_dma_addr + |
| 3500 | (i * sizeof(struct utp_transfer_req_desc)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3501 | hba->lrb[i].ucd_req_ptr = |
| 3502 | (struct utp_upiu_req *)(cmd_descp + i); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 3503 | hba->lrb[i].ucd_req_dma_addr = cmd_desc_element_addr; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3504 | hba->lrb[i].ucd_rsp_ptr = |
| 3505 | (struct utp_upiu_rsp *)cmd_descp[i].response_upiu; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 3506 | hba->lrb[i].ucd_rsp_dma_addr = cmd_desc_element_addr + |
| 3507 | response_offset; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3508 | hba->lrb[i].ucd_prdt_ptr = |
| 3509 | (struct ufshcd_sg_entry *)cmd_descp[i].prd_table; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 3510 | hba->lrb[i].ucd_prdt_dma_addr = cmd_desc_element_addr + |
| 3511 | prdt_offset; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3512 | } |
| 3513 | } |
| 3514 | |
| 3515 | /** |
| 3516 | * ufshcd_dme_link_startup - Notify Unipro to perform link startup |
| 3517 | * @hba: per adapter instance |
| 3518 | * |
| 3519 | * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer, |
| 3520 | * in order to initialize the Unipro link startup procedure. |
| 3521 | * Once the Unipro links are up, the device connected to the controller |
| 3522 | * is detected. |
| 3523 | * |
| 3524 | * Returns 0 on success, non-zero value on failure |
| 3525 | */ |
| 3526 | static int ufshcd_dme_link_startup(struct ufs_hba *hba) |
| 3527 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3528 | struct uic_command uic_cmd = {0}; |
| 3529 | int ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3530 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3531 | uic_cmd.command = UIC_CMD_DME_LINK_STARTUP; |
| 3532 | |
| 3533 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3534 | if (ret) |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 3535 | dev_dbg(hba->dev, |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3536 | "dme-link-startup: error code %d\n", ret); |
| 3537 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3538 | } |
Alim Akhtar | 4404c5d | 2018-05-06 15:44:17 +0530 | [diff] [blame] | 3539 | /** |
| 3540 | * ufshcd_dme_reset - UIC command for DME_RESET |
| 3541 | * @hba: per adapter instance |
| 3542 | * |
| 3543 | * DME_RESET command is issued in order to reset UniPro stack. |
| 3544 | * This function now deal with cold reset. |
| 3545 | * |
| 3546 | * Returns 0 on success, non-zero value on failure |
| 3547 | */ |
| 3548 | static int ufshcd_dme_reset(struct ufs_hba *hba) |
| 3549 | { |
| 3550 | struct uic_command uic_cmd = {0}; |
| 3551 | int ret; |
| 3552 | |
| 3553 | uic_cmd.command = UIC_CMD_DME_RESET; |
| 3554 | |
| 3555 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3556 | if (ret) |
| 3557 | dev_err(hba->dev, |
| 3558 | "dme-reset: error code %d\n", ret); |
| 3559 | |
| 3560 | return ret; |
| 3561 | } |
| 3562 | |
| 3563 | /** |
| 3564 | * ufshcd_dme_enable - UIC command for DME_ENABLE |
| 3565 | * @hba: per adapter instance |
| 3566 | * |
| 3567 | * DME_ENABLE command is issued in order to enable UniPro stack. |
| 3568 | * |
| 3569 | * Returns 0 on success, non-zero value on failure |
| 3570 | */ |
| 3571 | static int ufshcd_dme_enable(struct ufs_hba *hba) |
| 3572 | { |
| 3573 | struct uic_command uic_cmd = {0}; |
| 3574 | int ret; |
| 3575 | |
| 3576 | uic_cmd.command = UIC_CMD_DME_ENABLE; |
| 3577 | |
| 3578 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3579 | if (ret) |
| 3580 | dev_err(hba->dev, |
| 3581 | "dme-reset: error code %d\n", ret); |
| 3582 | |
| 3583 | return ret; |
| 3584 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3585 | |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 3586 | static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba) |
| 3587 | { |
| 3588 | #define MIN_DELAY_BEFORE_DME_CMDS_US 1000 |
| 3589 | unsigned long min_sleep_time_us; |
| 3590 | |
| 3591 | if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS)) |
| 3592 | return; |
| 3593 | |
| 3594 | /* |
| 3595 | * last_dme_cmd_tstamp will be 0 only for 1st call to |
| 3596 | * this function |
| 3597 | */ |
| 3598 | if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) { |
| 3599 | min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US; |
| 3600 | } else { |
| 3601 | unsigned long delta = |
| 3602 | (unsigned long) ktime_to_us( |
| 3603 | ktime_sub(ktime_get(), |
| 3604 | hba->last_dme_cmd_tstamp)); |
| 3605 | |
| 3606 | if (delta < MIN_DELAY_BEFORE_DME_CMDS_US) |
| 3607 | min_sleep_time_us = |
| 3608 | MIN_DELAY_BEFORE_DME_CMDS_US - delta; |
| 3609 | else |
| 3610 | return; /* no more delay required */ |
| 3611 | } |
| 3612 | |
| 3613 | /* allow sleep for extra 50us if needed */ |
| 3614 | usleep_range(min_sleep_time_us, min_sleep_time_us + 50); |
| 3615 | } |
| 3616 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3617 | /** |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3618 | * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET |
| 3619 | * @hba: per adapter instance |
| 3620 | * @attr_sel: uic command argument1 |
| 3621 | * @attr_set: attribute set type as uic command argument2 |
| 3622 | * @mib_val: setting value as uic command argument3 |
| 3623 | * @peer: indicate whether peer or local |
| 3624 | * |
| 3625 | * Returns 0 on success, non-zero value on failure |
| 3626 | */ |
| 3627 | int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel, |
| 3628 | u8 attr_set, u32 mib_val, u8 peer) |
| 3629 | { |
| 3630 | struct uic_command uic_cmd = {0}; |
| 3631 | static const char *const action[] = { |
| 3632 | "dme-set", |
| 3633 | "dme-peer-set" |
| 3634 | }; |
| 3635 | const char *set = action[!!peer]; |
| 3636 | int ret; |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3637 | int retries = UFS_UIC_COMMAND_RETRIES; |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3638 | |
| 3639 | uic_cmd.command = peer ? |
| 3640 | UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET; |
| 3641 | uic_cmd.argument1 = attr_sel; |
| 3642 | uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set); |
| 3643 | uic_cmd.argument3 = mib_val; |
| 3644 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3645 | do { |
| 3646 | /* for peer attributes we retry upon failure */ |
| 3647 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3648 | if (ret) |
| 3649 | dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n", |
| 3650 | set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret); |
| 3651 | } while (ret && peer && --retries); |
| 3652 | |
Yaniv Gardi | f37e9f8 | 2016-11-23 16:32:49 -0800 | [diff] [blame] | 3653 | if (ret) |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3654 | 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] | 3655 | set, UIC_GET_ATTR_ID(attr_sel), mib_val, |
| 3656 | UFS_UIC_COMMAND_RETRIES - retries); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3657 | |
| 3658 | return ret; |
| 3659 | } |
| 3660 | EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr); |
| 3661 | |
| 3662 | /** |
| 3663 | * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET |
| 3664 | * @hba: per adapter instance |
| 3665 | * @attr_sel: uic command argument1 |
| 3666 | * @mib_val: the value of the attribute as returned by the UIC command |
| 3667 | * @peer: indicate whether peer or local |
| 3668 | * |
| 3669 | * Returns 0 on success, non-zero value on failure |
| 3670 | */ |
| 3671 | int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel, |
| 3672 | u32 *mib_val, u8 peer) |
| 3673 | { |
| 3674 | struct uic_command uic_cmd = {0}; |
| 3675 | static const char *const action[] = { |
| 3676 | "dme-get", |
| 3677 | "dme-peer-get" |
| 3678 | }; |
| 3679 | const char *get = action[!!peer]; |
| 3680 | int ret; |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3681 | int retries = UFS_UIC_COMMAND_RETRIES; |
Yaniv Gardi | 874237f | 2015-05-17 18:55:03 +0300 | [diff] [blame] | 3682 | struct ufs_pa_layer_attr orig_pwr_info; |
| 3683 | struct ufs_pa_layer_attr temp_pwr_info; |
| 3684 | bool pwr_mode_change = false; |
| 3685 | |
| 3686 | if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) { |
| 3687 | orig_pwr_info = hba->pwr_info; |
| 3688 | temp_pwr_info = orig_pwr_info; |
| 3689 | |
| 3690 | if (orig_pwr_info.pwr_tx == FAST_MODE || |
| 3691 | orig_pwr_info.pwr_rx == FAST_MODE) { |
| 3692 | temp_pwr_info.pwr_tx = FASTAUTO_MODE; |
| 3693 | temp_pwr_info.pwr_rx = FASTAUTO_MODE; |
| 3694 | pwr_mode_change = true; |
| 3695 | } else if (orig_pwr_info.pwr_tx == SLOW_MODE || |
| 3696 | orig_pwr_info.pwr_rx == SLOW_MODE) { |
| 3697 | temp_pwr_info.pwr_tx = SLOWAUTO_MODE; |
| 3698 | temp_pwr_info.pwr_rx = SLOWAUTO_MODE; |
| 3699 | pwr_mode_change = true; |
| 3700 | } |
| 3701 | if (pwr_mode_change) { |
| 3702 | ret = ufshcd_change_power_mode(hba, &temp_pwr_info); |
| 3703 | if (ret) |
| 3704 | goto out; |
| 3705 | } |
| 3706 | } |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3707 | |
| 3708 | uic_cmd.command = peer ? |
| 3709 | UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET; |
| 3710 | uic_cmd.argument1 = attr_sel; |
| 3711 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3712 | do { |
| 3713 | /* for peer attributes we retry upon failure */ |
| 3714 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3715 | if (ret) |
| 3716 | dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n", |
| 3717 | get, UIC_GET_ATTR_ID(attr_sel), ret); |
| 3718 | } while (ret && peer && --retries); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3719 | |
Yaniv Gardi | f37e9f8 | 2016-11-23 16:32:49 -0800 | [diff] [blame] | 3720 | if (ret) |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3721 | 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] | 3722 | get, UIC_GET_ATTR_ID(attr_sel), |
| 3723 | UFS_UIC_COMMAND_RETRIES - retries); |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3724 | |
| 3725 | if (mib_val && !ret) |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3726 | *mib_val = uic_cmd.argument3; |
Yaniv Gardi | 874237f | 2015-05-17 18:55:03 +0300 | [diff] [blame] | 3727 | |
| 3728 | if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE) |
| 3729 | && pwr_mode_change) |
| 3730 | ufshcd_change_power_mode(hba, &orig_pwr_info); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3731 | out: |
| 3732 | return ret; |
| 3733 | } |
| 3734 | EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr); |
| 3735 | |
| 3736 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3737 | * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power |
| 3738 | * state) and waits for it to take effect. |
| 3739 | * |
| 3740 | * @hba: per adapter instance |
| 3741 | * @cmd: UIC command to execute |
| 3742 | * |
| 3743 | * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER & |
| 3744 | * DME_HIBERNATE_EXIT commands take some time to take its effect on both host |
| 3745 | * and device UniPro link and hence it's final completion would be indicated by |
| 3746 | * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in |
| 3747 | * addition to normal UIC command completion Status (UCCS). This function only |
| 3748 | * returns after the relevant status bits indicate the completion. |
| 3749 | * |
| 3750 | * Returns 0 on success, non-zero value on failure |
| 3751 | */ |
| 3752 | static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd) |
| 3753 | { |
| 3754 | struct completion uic_async_done; |
| 3755 | unsigned long flags; |
| 3756 | u8 status; |
| 3757 | int ret; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3758 | bool reenable_intr = false; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3759 | |
| 3760 | mutex_lock(&hba->uic_cmd_mutex); |
| 3761 | init_completion(&uic_async_done); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 3762 | ufshcd_add_delay_before_dme_cmd(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3763 | |
| 3764 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 3765 | hba->uic_async_done = &uic_async_done; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3766 | if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) { |
| 3767 | ufshcd_disable_intr(hba, UIC_COMMAND_COMPL); |
| 3768 | /* |
| 3769 | * Make sure UIC command completion interrupt is disabled before |
| 3770 | * issuing UIC command. |
| 3771 | */ |
| 3772 | wmb(); |
| 3773 | reenable_intr = true; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3774 | } |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3775 | ret = __ufshcd_send_uic_cmd(hba, cmd, false); |
| 3776 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3777 | if (ret) { |
| 3778 | dev_err(hba->dev, |
| 3779 | "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n", |
| 3780 | cmd->command, cmd->argument3, ret); |
| 3781 | goto out; |
| 3782 | } |
| 3783 | |
| 3784 | if (!wait_for_completion_timeout(hba->uic_async_done, |
| 3785 | msecs_to_jiffies(UIC_CMD_TIMEOUT))) { |
| 3786 | dev_err(hba->dev, |
| 3787 | "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n", |
| 3788 | cmd->command, cmd->argument3); |
| 3789 | ret = -ETIMEDOUT; |
| 3790 | goto out; |
| 3791 | } |
| 3792 | |
| 3793 | status = ufshcd_get_upmcrs(hba); |
| 3794 | if (status != PWR_LOCAL) { |
| 3795 | dev_err(hba->dev, |
Zang Leigang | 479da36 | 2017-09-19 16:50:30 +0800 | [diff] [blame] | 3796 | "pwr ctrl cmd 0x%x failed, host upmcrs:0x%x\n", |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3797 | cmd->command, status); |
| 3798 | ret = (status != PWR_OK) ? status : -1; |
| 3799 | } |
| 3800 | out: |
Venkat Gopalakrishnan | 7942f7b | 2017-02-03 16:58:24 -0800 | [diff] [blame] | 3801 | if (ret) { |
| 3802 | ufshcd_print_host_state(hba); |
| 3803 | ufshcd_print_pwr_info(hba); |
| 3804 | ufshcd_print_host_regs(hba); |
| 3805 | } |
| 3806 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3807 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3808 | hba->active_uic_cmd = NULL; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3809 | hba->uic_async_done = NULL; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3810 | if (reenable_intr) |
| 3811 | ufshcd_enable_intr(hba, UIC_COMMAND_COMPL); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3812 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3813 | mutex_unlock(&hba->uic_cmd_mutex); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3814 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3815 | return ret; |
| 3816 | } |
| 3817 | |
| 3818 | /** |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3819 | * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage |
| 3820 | * using DME_SET primitives. |
| 3821 | * @hba: per adapter instance |
| 3822 | * @mode: powr mode value |
| 3823 | * |
| 3824 | * Returns 0 on success, non-zero value on failure |
| 3825 | */ |
Sujit Reddy Thumma | bdbe5d2 | 2014-05-26 10:59:11 +0530 | [diff] [blame] | 3826 | 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] | 3827 | { |
| 3828 | struct uic_command uic_cmd = {0}; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3829 | int ret; |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3830 | |
Yaniv Gardi | c3a2f9e | 2015-05-17 18:55:01 +0300 | [diff] [blame] | 3831 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) { |
| 3832 | ret = ufshcd_dme_set(hba, |
| 3833 | UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1); |
| 3834 | if (ret) { |
| 3835 | dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n", |
| 3836 | __func__, ret); |
| 3837 | goto out; |
| 3838 | } |
| 3839 | } |
| 3840 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3841 | uic_cmd.command = UIC_CMD_DME_SET; |
| 3842 | uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE); |
| 3843 | uic_cmd.argument3 = mode; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3844 | ufshcd_hold(hba, false); |
| 3845 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
| 3846 | ufshcd_release(hba); |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3847 | |
Yaniv Gardi | c3a2f9e | 2015-05-17 18:55:01 +0300 | [diff] [blame] | 3848 | out: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3849 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3850 | } |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3851 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 3852 | static int ufshcd_link_recovery(struct ufs_hba *hba) |
| 3853 | { |
| 3854 | int ret; |
| 3855 | unsigned long flags; |
| 3856 | |
| 3857 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 3858 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 3859 | ufshcd_set_eh_in_progress(hba); |
| 3860 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3861 | |
| 3862 | ret = ufshcd_host_reset_and_restore(hba); |
| 3863 | |
| 3864 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 3865 | if (ret) |
| 3866 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 3867 | ufshcd_clear_eh_in_progress(hba); |
| 3868 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3869 | |
| 3870 | if (ret) |
| 3871 | dev_err(hba->dev, "%s: link recovery failed, err %d", |
| 3872 | __func__, ret); |
| 3873 | |
| 3874 | return ret; |
| 3875 | } |
| 3876 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 3877 | static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3878 | { |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 3879 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3880 | struct uic_command uic_cmd = {0}; |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 3881 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3882 | |
Kiwoong Kim | ee32c90 | 2016-11-10 21:17:43 +0900 | [diff] [blame] | 3883 | ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE); |
| 3884 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3885 | uic_cmd.command = UIC_CMD_DME_HIBER_ENTER; |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 3886 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 3887 | trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter", |
| 3888 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3889 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 3890 | if (ret) { |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 3891 | dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n", |
| 3892 | __func__, ret); |
| 3893 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 3894 | /* |
| 3895 | * If link recovery fails then return error so that caller |
| 3896 | * don't retry the hibern8 enter again. |
| 3897 | */ |
| 3898 | if (ufshcd_link_recovery(hba)) |
| 3899 | ret = -ENOLINK; |
Kiwoong Kim | ee32c90 | 2016-11-10 21:17:43 +0900 | [diff] [blame] | 3900 | } else |
| 3901 | ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, |
| 3902 | POST_CHANGE); |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 3903 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 3904 | return ret; |
| 3905 | } |
| 3906 | |
| 3907 | static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba) |
| 3908 | { |
| 3909 | int ret = 0, retries; |
| 3910 | |
| 3911 | for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) { |
| 3912 | ret = __ufshcd_uic_hibern8_enter(hba); |
| 3913 | if (!ret || ret == -ENOLINK) |
| 3914 | goto out; |
| 3915 | } |
| 3916 | out: |
| 3917 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3918 | } |
| 3919 | |
| 3920 | static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba) |
| 3921 | { |
| 3922 | struct uic_command uic_cmd = {0}; |
| 3923 | int ret; |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 3924 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3925 | |
Kiwoong Kim | ee32c90 | 2016-11-10 21:17:43 +0900 | [diff] [blame] | 3926 | ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE); |
| 3927 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3928 | uic_cmd.command = UIC_CMD_DME_HIBER_EXIT; |
| 3929 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 3930 | trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit", |
| 3931 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
| 3932 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3933 | if (ret) { |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 3934 | dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n", |
| 3935 | __func__, ret); |
| 3936 | ret = ufshcd_link_recovery(hba); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 3937 | } else { |
Kiwoong Kim | ee32c90 | 2016-11-10 21:17:43 +0900 | [diff] [blame] | 3938 | ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, |
| 3939 | POST_CHANGE); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 3940 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_get(); |
| 3941 | hba->ufs_stats.hibern8_exit_cnt++; |
| 3942 | } |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3943 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3944 | return ret; |
| 3945 | } |
| 3946 | |
Adrian Hunter | ad44837 | 2018-03-20 15:07:38 +0200 | [diff] [blame] | 3947 | static void ufshcd_auto_hibern8_enable(struct ufs_hba *hba) |
| 3948 | { |
| 3949 | unsigned long flags; |
| 3950 | |
Stanley Chu | ee5f104 | 2019-05-21 14:44:52 +0800 | [diff] [blame] | 3951 | if (!ufshcd_is_auto_hibern8_supported(hba) || !hba->ahit) |
Adrian Hunter | ad44837 | 2018-03-20 15:07:38 +0200 | [diff] [blame] | 3952 | return; |
| 3953 | |
| 3954 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 3955 | ufshcd_writel(hba, hba->ahit, REG_AUTO_HIBERNATE_IDLE_TIMER); |
| 3956 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3957 | } |
| 3958 | |
Yaniv Gardi | 5064636 | 2014-10-23 13:25:13 +0300 | [diff] [blame] | 3959 | /** |
| 3960 | * ufshcd_init_pwr_info - setting the POR (power on reset) |
| 3961 | * values in hba power info |
| 3962 | * @hba: per-adapter instance |
| 3963 | */ |
| 3964 | static void ufshcd_init_pwr_info(struct ufs_hba *hba) |
| 3965 | { |
| 3966 | hba->pwr_info.gear_rx = UFS_PWM_G1; |
| 3967 | hba->pwr_info.gear_tx = UFS_PWM_G1; |
| 3968 | hba->pwr_info.lane_rx = 1; |
| 3969 | hba->pwr_info.lane_tx = 1; |
| 3970 | hba->pwr_info.pwr_rx = SLOWAUTO_MODE; |
| 3971 | hba->pwr_info.pwr_tx = SLOWAUTO_MODE; |
| 3972 | hba->pwr_info.hs_rate = 0; |
| 3973 | } |
| 3974 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3975 | /** |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3976 | * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device |
| 3977 | * @hba: per-adapter instance |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3978 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3979 | static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3980 | { |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3981 | struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info; |
| 3982 | |
| 3983 | if (hba->max_pwr_info.is_valid) |
| 3984 | return 0; |
| 3985 | |
subhashj@codeaurora.org | 2349b53 | 2016-11-23 16:33:19 -0800 | [diff] [blame] | 3986 | pwr_info->pwr_tx = FAST_MODE; |
| 3987 | pwr_info->pwr_rx = FAST_MODE; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3988 | pwr_info->hs_rate = PA_HS_MODE_B; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3989 | |
| 3990 | /* Get the connected lane count */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3991 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES), |
| 3992 | &pwr_info->lane_rx); |
| 3993 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 3994 | &pwr_info->lane_tx); |
| 3995 | |
| 3996 | if (!pwr_info->lane_rx || !pwr_info->lane_tx) { |
| 3997 | dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n", |
| 3998 | __func__, |
| 3999 | pwr_info->lane_rx, |
| 4000 | pwr_info->lane_tx); |
| 4001 | return -EINVAL; |
| 4002 | } |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4003 | |
| 4004 | /* |
| 4005 | * First, get the maximum gears of HS speed. |
| 4006 | * If a zero value, it means there is no HSGEAR capability. |
| 4007 | * Then, get the maximum gears of PWM speed. |
| 4008 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4009 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx); |
| 4010 | if (!pwr_info->gear_rx) { |
| 4011 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), |
| 4012 | &pwr_info->gear_rx); |
| 4013 | if (!pwr_info->gear_rx) { |
| 4014 | dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n", |
| 4015 | __func__, pwr_info->gear_rx); |
| 4016 | return -EINVAL; |
| 4017 | } |
subhashj@codeaurora.org | 2349b53 | 2016-11-23 16:33:19 -0800 | [diff] [blame] | 4018 | pwr_info->pwr_rx = SLOW_MODE; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4019 | } |
| 4020 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4021 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), |
| 4022 | &pwr_info->gear_tx); |
| 4023 | if (!pwr_info->gear_tx) { |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4024 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4025 | &pwr_info->gear_tx); |
| 4026 | if (!pwr_info->gear_tx) { |
| 4027 | dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n", |
| 4028 | __func__, pwr_info->gear_tx); |
| 4029 | return -EINVAL; |
| 4030 | } |
subhashj@codeaurora.org | 2349b53 | 2016-11-23 16:33:19 -0800 | [diff] [blame] | 4031 | pwr_info->pwr_tx = SLOW_MODE; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4032 | } |
| 4033 | |
| 4034 | hba->max_pwr_info.is_valid = true; |
| 4035 | return 0; |
| 4036 | } |
| 4037 | |
| 4038 | static int ufshcd_change_power_mode(struct ufs_hba *hba, |
| 4039 | struct ufs_pa_layer_attr *pwr_mode) |
| 4040 | { |
| 4041 | int ret; |
| 4042 | |
| 4043 | /* if already configured to the requested pwr_mode */ |
| 4044 | if (pwr_mode->gear_rx == hba->pwr_info.gear_rx && |
| 4045 | pwr_mode->gear_tx == hba->pwr_info.gear_tx && |
| 4046 | pwr_mode->lane_rx == hba->pwr_info.lane_rx && |
| 4047 | pwr_mode->lane_tx == hba->pwr_info.lane_tx && |
| 4048 | pwr_mode->pwr_rx == hba->pwr_info.pwr_rx && |
| 4049 | pwr_mode->pwr_tx == hba->pwr_info.pwr_tx && |
| 4050 | pwr_mode->hs_rate == hba->pwr_info.hs_rate) { |
| 4051 | dev_dbg(hba->dev, "%s: power already configured\n", __func__); |
| 4052 | return 0; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4053 | } |
| 4054 | |
| 4055 | /* |
| 4056 | * Configure attributes for power mode change with below. |
| 4057 | * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION, |
| 4058 | * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION, |
| 4059 | * - PA_HSSERIES |
| 4060 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4061 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx); |
| 4062 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES), |
| 4063 | pwr_mode->lane_rx); |
| 4064 | if (pwr_mode->pwr_rx == FASTAUTO_MODE || |
| 4065 | pwr_mode->pwr_rx == FAST_MODE) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4066 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4067 | else |
| 4068 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4069 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4070 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx); |
| 4071 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES), |
| 4072 | pwr_mode->lane_tx); |
| 4073 | if (pwr_mode->pwr_tx == FASTAUTO_MODE || |
| 4074 | pwr_mode->pwr_tx == FAST_MODE) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4075 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4076 | else |
| 4077 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4078 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4079 | if (pwr_mode->pwr_rx == FASTAUTO_MODE || |
| 4080 | pwr_mode->pwr_tx == FASTAUTO_MODE || |
| 4081 | pwr_mode->pwr_rx == FAST_MODE || |
| 4082 | pwr_mode->pwr_tx == FAST_MODE) |
| 4083 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES), |
| 4084 | pwr_mode->hs_rate); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4085 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4086 | ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4 |
| 4087 | | pwr_mode->pwr_tx); |
| 4088 | |
| 4089 | if (ret) { |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4090 | dev_err(hba->dev, |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4091 | "%s: power mode change failed %d\n", __func__, ret); |
| 4092 | } else { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4093 | ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL, |
| 4094 | pwr_mode); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4095 | |
| 4096 | memcpy(&hba->pwr_info, pwr_mode, |
| 4097 | sizeof(struct ufs_pa_layer_attr)); |
| 4098 | } |
| 4099 | |
| 4100 | return ret; |
| 4101 | } |
| 4102 | |
| 4103 | /** |
| 4104 | * ufshcd_config_pwr_mode - configure a new power mode |
| 4105 | * @hba: per-adapter instance |
| 4106 | * @desired_pwr_mode: desired power configuration |
| 4107 | */ |
Alim Akhtar | 0d846e7 | 2018-05-06 15:44:18 +0530 | [diff] [blame] | 4108 | int ufshcd_config_pwr_mode(struct ufs_hba *hba, |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4109 | struct ufs_pa_layer_attr *desired_pwr_mode) |
| 4110 | { |
| 4111 | struct ufs_pa_layer_attr final_params = { 0 }; |
| 4112 | int ret; |
| 4113 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4114 | ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE, |
| 4115 | desired_pwr_mode, &final_params); |
| 4116 | |
| 4117 | if (ret) |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4118 | memcpy(&final_params, desired_pwr_mode, sizeof(final_params)); |
| 4119 | |
| 4120 | ret = ufshcd_change_power_mode(hba, &final_params); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 4121 | if (!ret) |
| 4122 | ufshcd_print_pwr_info(hba); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4123 | |
| 4124 | return ret; |
| 4125 | } |
Alim Akhtar | 0d846e7 | 2018-05-06 15:44:18 +0530 | [diff] [blame] | 4126 | EXPORT_SYMBOL_GPL(ufshcd_config_pwr_mode); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4127 | |
| 4128 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4129 | * ufshcd_complete_dev_init() - checks device readiness |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 4130 | * @hba: per-adapter instance |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4131 | * |
| 4132 | * Set fDeviceInit flag and poll until device toggles it. |
| 4133 | */ |
| 4134 | static int ufshcd_complete_dev_init(struct ufs_hba *hba) |
| 4135 | { |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 4136 | int i; |
| 4137 | int err; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4138 | bool flag_res = 1; |
| 4139 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 4140 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG, |
| 4141 | QUERY_FLAG_IDN_FDEVICEINIT, NULL); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4142 | if (err) { |
| 4143 | dev_err(hba->dev, |
| 4144 | "%s setting fDeviceInit flag failed with error %d\n", |
| 4145 | __func__, err); |
| 4146 | goto out; |
| 4147 | } |
| 4148 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 4149 | /* poll for max. 1000 iterations for fDeviceInit flag to clear */ |
| 4150 | for (i = 0; i < 1000 && !err && flag_res; i++) |
| 4151 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG, |
| 4152 | QUERY_FLAG_IDN_FDEVICEINIT, &flag_res); |
| 4153 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4154 | if (err) |
| 4155 | dev_err(hba->dev, |
| 4156 | "%s reading fDeviceInit flag failed with error %d\n", |
| 4157 | __func__, err); |
| 4158 | else if (flag_res) |
| 4159 | dev_err(hba->dev, |
| 4160 | "%s fDeviceInit was not cleared by the device\n", |
| 4161 | __func__); |
| 4162 | |
| 4163 | out: |
| 4164 | return err; |
| 4165 | } |
| 4166 | |
| 4167 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4168 | * ufshcd_make_hba_operational - Make UFS controller operational |
| 4169 | * @hba: per adapter instance |
| 4170 | * |
| 4171 | * To bring UFS host controller to operational state, |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4172 | * 1. Enable required interrupts |
| 4173 | * 2. Configure interrupt aggregation |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 4174 | * 3. Program UTRL and UTMRL base address |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4175 | * 4. Configure run-stop-registers |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4176 | * |
| 4177 | * Returns 0 on success, non-zero value on failure |
| 4178 | */ |
| 4179 | static int ufshcd_make_hba_operational(struct ufs_hba *hba) |
| 4180 | { |
| 4181 | int err = 0; |
| 4182 | u32 reg; |
| 4183 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4184 | /* Enable required interrupts */ |
| 4185 | ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS); |
| 4186 | |
| 4187 | /* Configure interrupt aggregation */ |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 4188 | if (ufshcd_is_intr_aggr_allowed(hba)) |
| 4189 | ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO); |
| 4190 | else |
| 4191 | ufshcd_disable_intr_aggr(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4192 | |
| 4193 | /* Configure UTRL and UTMRL base address registers */ |
| 4194 | ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr), |
| 4195 | REG_UTP_TRANSFER_REQ_LIST_BASE_L); |
| 4196 | ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr), |
| 4197 | REG_UTP_TRANSFER_REQ_LIST_BASE_H); |
| 4198 | ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr), |
| 4199 | REG_UTP_TASK_REQ_LIST_BASE_L); |
| 4200 | ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr), |
| 4201 | REG_UTP_TASK_REQ_LIST_BASE_H); |
| 4202 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4203 | /* |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 4204 | * Make sure base address and interrupt setup are updated before |
| 4205 | * enabling the run/stop registers below. |
| 4206 | */ |
| 4207 | wmb(); |
| 4208 | |
| 4209 | /* |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4210 | * UCRDY, UTMRLDY and UTRLRDY bits must be 1 |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4211 | */ |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4212 | reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4213 | if (!(ufshcd_get_lists_status(reg))) { |
| 4214 | ufshcd_enable_run_stop_reg(hba); |
| 4215 | } else { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 4216 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4217 | "Host controller not ready to process requests"); |
| 4218 | err = -EIO; |
| 4219 | goto out; |
| 4220 | } |
| 4221 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4222 | out: |
| 4223 | return err; |
| 4224 | } |
| 4225 | |
| 4226 | /** |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4227 | * ufshcd_hba_stop - Send controller to reset state |
| 4228 | * @hba: per adapter instance |
| 4229 | * @can_sleep: perform sleep or just spin |
| 4230 | */ |
| 4231 | static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep) |
| 4232 | { |
| 4233 | int err; |
| 4234 | |
| 4235 | ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE); |
| 4236 | err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE, |
| 4237 | CONTROLLER_ENABLE, CONTROLLER_DISABLE, |
| 4238 | 10, 1, can_sleep); |
| 4239 | if (err) |
| 4240 | dev_err(hba->dev, "%s: Controller disable failed\n", __func__); |
| 4241 | } |
| 4242 | |
| 4243 | /** |
Alim Akhtar | 4404c5d | 2018-05-06 15:44:17 +0530 | [diff] [blame] | 4244 | * ufshcd_hba_execute_hce - initialize the controller |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4245 | * @hba: per adapter instance |
| 4246 | * |
| 4247 | * The controller resets itself and controller firmware initialization |
| 4248 | * sequence kicks off. When controller is ready it will set |
| 4249 | * the Host Controller Enable bit to 1. |
| 4250 | * |
| 4251 | * Returns 0 on success, non-zero value on failure |
| 4252 | */ |
Alim Akhtar | 4404c5d | 2018-05-06 15:44:17 +0530 | [diff] [blame] | 4253 | static int ufshcd_hba_execute_hce(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4254 | { |
| 4255 | int retry; |
| 4256 | |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4257 | if (!ufshcd_is_hba_active(hba)) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4258 | /* change controller state to "reset state" */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4259 | ufshcd_hba_stop(hba, true); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4260 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4261 | /* UniPro link is disabled at this point */ |
| 4262 | ufshcd_set_link_off(hba); |
| 4263 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4264 | ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4265 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4266 | /* start controller initialization sequence */ |
| 4267 | ufshcd_hba_start(hba); |
| 4268 | |
| 4269 | /* |
| 4270 | * To initialize a UFS host controller HCE bit must be set to 1. |
| 4271 | * During initialization the HCE bit value changes from 1->0->1. |
| 4272 | * When the host controller completes initialization sequence |
| 4273 | * it sets the value of HCE bit to 1. The same HCE bit is read back |
| 4274 | * to check if the controller has completed initialization sequence. |
| 4275 | * So without this delay the value HCE = 1, set in the previous |
| 4276 | * instruction might be read back. |
| 4277 | * This delay can be changed based on the controller. |
| 4278 | */ |
Bean Huo | 838c1ef | 2019-07-15 11:21:10 +0000 | [diff] [blame] | 4279 | usleep_range(1000, 1100); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4280 | |
| 4281 | /* wait for the host controller to complete initialization */ |
| 4282 | retry = 10; |
| 4283 | while (ufshcd_is_hba_active(hba)) { |
| 4284 | if (retry) { |
| 4285 | retry--; |
| 4286 | } else { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 4287 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4288 | "Controller enable failed\n"); |
| 4289 | return -EIO; |
| 4290 | } |
Bean Huo | 838c1ef | 2019-07-15 11:21:10 +0000 | [diff] [blame] | 4291 | usleep_range(5000, 5100); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4292 | } |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4293 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4294 | /* enable UIC related interrupts */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4295 | ufshcd_enable_intr(hba, UFSHCD_UIC_MASK); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4296 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4297 | ufshcd_vops_hce_enable_notify(hba, POST_CHANGE); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4298 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4299 | return 0; |
| 4300 | } |
| 4301 | |
Alim Akhtar | 4404c5d | 2018-05-06 15:44:17 +0530 | [diff] [blame] | 4302 | static int ufshcd_hba_enable(struct ufs_hba *hba) |
| 4303 | { |
| 4304 | int ret; |
| 4305 | |
| 4306 | if (hba->quirks & UFSHCI_QUIRK_BROKEN_HCE) { |
| 4307 | ufshcd_set_link_off(hba); |
| 4308 | ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE); |
| 4309 | |
| 4310 | /* enable UIC related interrupts */ |
| 4311 | ufshcd_enable_intr(hba, UFSHCD_UIC_MASK); |
| 4312 | ret = ufshcd_dme_reset(hba); |
| 4313 | if (!ret) { |
| 4314 | ret = ufshcd_dme_enable(hba); |
| 4315 | if (!ret) |
| 4316 | ufshcd_vops_hce_enable_notify(hba, POST_CHANGE); |
| 4317 | if (ret) |
| 4318 | dev_err(hba->dev, |
| 4319 | "Host controller enable failed with non-hce\n"); |
| 4320 | } |
| 4321 | } else { |
| 4322 | ret = ufshcd_hba_execute_hce(hba); |
| 4323 | } |
| 4324 | |
| 4325 | return ret; |
| 4326 | } |
Yaniv Gardi | 7ca38cf | 2015-05-17 18:54:59 +0300 | [diff] [blame] | 4327 | static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer) |
| 4328 | { |
| 4329 | int tx_lanes, i, err = 0; |
| 4330 | |
| 4331 | if (!peer) |
| 4332 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 4333 | &tx_lanes); |
| 4334 | else |
| 4335 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 4336 | &tx_lanes); |
| 4337 | for (i = 0; i < tx_lanes; i++) { |
| 4338 | if (!peer) |
| 4339 | err = ufshcd_dme_set(hba, |
| 4340 | UIC_ARG_MIB_SEL(TX_LCC_ENABLE, |
| 4341 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)), |
| 4342 | 0); |
| 4343 | else |
| 4344 | err = ufshcd_dme_peer_set(hba, |
| 4345 | UIC_ARG_MIB_SEL(TX_LCC_ENABLE, |
| 4346 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)), |
| 4347 | 0); |
| 4348 | if (err) { |
| 4349 | dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d", |
| 4350 | __func__, peer, i, err); |
| 4351 | break; |
| 4352 | } |
| 4353 | } |
| 4354 | |
| 4355 | return err; |
| 4356 | } |
| 4357 | |
| 4358 | static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba) |
| 4359 | { |
| 4360 | return ufshcd_disable_tx_lcc(hba, true); |
| 4361 | } |
| 4362 | |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4363 | static void ufshcd_update_reg_hist(struct ufs_err_reg_hist *reg_hist, |
| 4364 | u32 reg) |
| 4365 | { |
| 4366 | reg_hist->reg[reg_hist->pos] = reg; |
| 4367 | reg_hist->tstamp[reg_hist->pos] = ktime_get(); |
| 4368 | reg_hist->pos = (reg_hist->pos + 1) % UFS_ERR_REG_HIST_LENGTH; |
| 4369 | } |
| 4370 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4371 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4372 | * ufshcd_link_startup - Initialize unipro link startup |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4373 | * @hba: per adapter instance |
| 4374 | * |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4375 | * Returns 0 for success, non-zero in case of failure |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4376 | */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4377 | static int ufshcd_link_startup(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4378 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4379 | int ret; |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4380 | int retries = DME_LINKSTARTUP_RETRIES; |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 4381 | bool link_startup_again = false; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4382 | |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 4383 | /* |
| 4384 | * If UFS device isn't active then we will have to issue link startup |
| 4385 | * 2 times to make sure the device state move to active. |
| 4386 | */ |
| 4387 | if (!ufshcd_is_ufs_dev_active(hba)) |
| 4388 | link_startup_again = true; |
| 4389 | |
| 4390 | link_startup: |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4391 | do { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4392 | ufshcd_vops_link_startup_notify(hba, PRE_CHANGE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4393 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4394 | ret = ufshcd_dme_link_startup(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4395 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4396 | /* check if device is detected by inter-connect layer */ |
| 4397 | if (!ret && !ufshcd_is_device_present(hba)) { |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4398 | ufshcd_update_reg_hist(&hba->ufs_stats.link_startup_err, |
| 4399 | 0); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4400 | dev_err(hba->dev, "%s: Device not present\n", __func__); |
| 4401 | ret = -ENXIO; |
| 4402 | goto out; |
| 4403 | } |
| 4404 | |
| 4405 | /* |
| 4406 | * DME link lost indication is only received when link is up, |
| 4407 | * but we can't be sure if the link is up until link startup |
| 4408 | * succeeds. So reset the local Uni-Pro and try again. |
| 4409 | */ |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4410 | if (ret && ufshcd_hba_enable(hba)) { |
| 4411 | ufshcd_update_reg_hist(&hba->ufs_stats.link_startup_err, |
| 4412 | (u32)ret); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4413 | goto out; |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4414 | } |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4415 | } while (ret && retries--); |
| 4416 | |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4417 | if (ret) { |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4418 | /* failed to get the link up... retire */ |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4419 | ufshcd_update_reg_hist(&hba->ufs_stats.link_startup_err, |
| 4420 | (u32)ret); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4421 | goto out; |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 4422 | } |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4423 | |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 4424 | if (link_startup_again) { |
| 4425 | link_startup_again = false; |
| 4426 | retries = DME_LINKSTARTUP_RETRIES; |
| 4427 | goto link_startup; |
| 4428 | } |
| 4429 | |
subhashj@codeaurora.org | d2aebb9 | 2016-12-22 18:41:33 -0800 | [diff] [blame] | 4430 | /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */ |
| 4431 | ufshcd_init_pwr_info(hba); |
| 4432 | ufshcd_print_pwr_info(hba); |
| 4433 | |
Yaniv Gardi | 7ca38cf | 2015-05-17 18:54:59 +0300 | [diff] [blame] | 4434 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) { |
| 4435 | ret = ufshcd_disable_device_tx_lcc(hba); |
| 4436 | if (ret) |
| 4437 | goto out; |
| 4438 | } |
| 4439 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4440 | /* Include any host controller configuration via UIC commands */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4441 | ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE); |
| 4442 | if (ret) |
| 4443 | goto out; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4444 | |
| 4445 | ret = ufshcd_make_hba_operational(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4446 | out: |
Venkat Gopalakrishnan | 7942f7b | 2017-02-03 16:58:24 -0800 | [diff] [blame] | 4447 | if (ret) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4448 | dev_err(hba->dev, "link startup failed %d\n", ret); |
Venkat Gopalakrishnan | 7942f7b | 2017-02-03 16:58:24 -0800 | [diff] [blame] | 4449 | ufshcd_print_host_state(hba); |
| 4450 | ufshcd_print_pwr_info(hba); |
| 4451 | ufshcd_print_host_regs(hba); |
| 4452 | } |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4453 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4454 | } |
| 4455 | |
| 4456 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4457 | * ufshcd_verify_dev_init() - Verify device initialization |
| 4458 | * @hba: per-adapter instance |
| 4459 | * |
| 4460 | * Send NOP OUT UPIU and wait for NOP IN response to check whether the |
| 4461 | * device Transport Protocol (UTP) layer is ready after a reset. |
| 4462 | * If the UTP layer at the device side is not initialized, it may |
| 4463 | * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT |
| 4464 | * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations. |
| 4465 | */ |
| 4466 | static int ufshcd_verify_dev_init(struct ufs_hba *hba) |
| 4467 | { |
| 4468 | int err = 0; |
| 4469 | int retries; |
| 4470 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4471 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4472 | mutex_lock(&hba->dev_cmd.lock); |
| 4473 | for (retries = NOP_OUT_RETRIES; retries > 0; retries--) { |
| 4474 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP, |
| 4475 | NOP_OUT_TIMEOUT); |
| 4476 | |
| 4477 | if (!err || err == -ETIMEDOUT) |
| 4478 | break; |
| 4479 | |
| 4480 | dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err); |
| 4481 | } |
| 4482 | mutex_unlock(&hba->dev_cmd.lock); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4483 | ufshcd_release(hba); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4484 | |
| 4485 | if (err) |
| 4486 | dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err); |
| 4487 | return err; |
| 4488 | } |
| 4489 | |
| 4490 | /** |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4491 | * ufshcd_set_queue_depth - set lun queue depth |
| 4492 | * @sdev: pointer to SCSI device |
| 4493 | * |
| 4494 | * Read bLUQueueDepth value and activate scsi tagged command |
| 4495 | * queueing. For WLUN, queue depth is set to 1. For best-effort |
| 4496 | * cases (bLUQueueDepth = 0) the queue depth is set to a maximum |
| 4497 | * value that host can queue. |
| 4498 | */ |
| 4499 | static void ufshcd_set_queue_depth(struct scsi_device *sdev) |
| 4500 | { |
| 4501 | int ret = 0; |
| 4502 | u8 lun_qdepth; |
| 4503 | struct ufs_hba *hba; |
| 4504 | |
| 4505 | hba = shost_priv(sdev->host); |
| 4506 | |
| 4507 | lun_qdepth = hba->nutrs; |
Szymon Mielczarek | dbd34a6 | 2017-03-29 08:19:21 +0200 | [diff] [blame] | 4508 | ret = ufshcd_read_unit_desc_param(hba, |
| 4509 | ufshcd_scsi_to_upiu_lun(sdev->lun), |
| 4510 | UNIT_DESC_PARAM_LU_Q_DEPTH, |
| 4511 | &lun_qdepth, |
| 4512 | sizeof(lun_qdepth)); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4513 | |
| 4514 | /* Some WLUN doesn't support unit descriptor */ |
| 4515 | if (ret == -EOPNOTSUPP) |
| 4516 | lun_qdepth = 1; |
| 4517 | else if (!lun_qdepth) |
| 4518 | /* eventually, we can figure out the real queue depth */ |
| 4519 | lun_qdepth = hba->nutrs; |
| 4520 | else |
| 4521 | lun_qdepth = min_t(int, lun_qdepth, hba->nutrs); |
| 4522 | |
| 4523 | dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n", |
| 4524 | __func__, lun_qdepth); |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4525 | scsi_change_queue_depth(sdev, lun_qdepth); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4526 | } |
| 4527 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4528 | /* |
| 4529 | * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR |
| 4530 | * @hba: per-adapter instance |
| 4531 | * @lun: UFS device lun id |
| 4532 | * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info |
| 4533 | * |
| 4534 | * Returns 0 in case of success and b_lu_write_protect status would be returned |
| 4535 | * @b_lu_write_protect parameter. |
| 4536 | * Returns -ENOTSUPP if reading b_lu_write_protect is not supported. |
| 4537 | * Returns -EINVAL in case of invalid parameters passed to this function. |
| 4538 | */ |
| 4539 | static int ufshcd_get_lu_wp(struct ufs_hba *hba, |
| 4540 | u8 lun, |
| 4541 | u8 *b_lu_write_protect) |
| 4542 | { |
| 4543 | int ret; |
| 4544 | |
| 4545 | if (!b_lu_write_protect) |
| 4546 | ret = -EINVAL; |
| 4547 | /* |
| 4548 | * According to UFS device spec, RPMB LU can't be write |
| 4549 | * protected so skip reading bLUWriteProtect parameter for |
| 4550 | * it. For other W-LUs, UNIT DESCRIPTOR is not available. |
| 4551 | */ |
| 4552 | else if (lun >= UFS_UPIU_MAX_GENERAL_LUN) |
| 4553 | ret = -ENOTSUPP; |
| 4554 | else |
| 4555 | ret = ufshcd_read_unit_desc_param(hba, |
| 4556 | lun, |
| 4557 | UNIT_DESC_PARAM_LU_WR_PROTECT, |
| 4558 | b_lu_write_protect, |
| 4559 | sizeof(*b_lu_write_protect)); |
| 4560 | return ret; |
| 4561 | } |
| 4562 | |
| 4563 | /** |
| 4564 | * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect |
| 4565 | * status |
| 4566 | * @hba: per-adapter instance |
| 4567 | * @sdev: pointer to SCSI device |
| 4568 | * |
| 4569 | */ |
| 4570 | static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba, |
| 4571 | struct scsi_device *sdev) |
| 4572 | { |
| 4573 | if (hba->dev_info.f_power_on_wp_en && |
| 4574 | !hba->dev_info.is_lu_power_on_wp) { |
| 4575 | u8 b_lu_write_protect; |
| 4576 | |
| 4577 | if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun), |
| 4578 | &b_lu_write_protect) && |
| 4579 | (b_lu_write_protect == UFS_LU_POWER_ON_WP)) |
| 4580 | hba->dev_info.is_lu_power_on_wp = true; |
| 4581 | } |
| 4582 | } |
| 4583 | |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4584 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4585 | * ufshcd_slave_alloc - handle initial SCSI device configurations |
| 4586 | * @sdev: pointer to SCSI device |
| 4587 | * |
| 4588 | * Returns success |
| 4589 | */ |
| 4590 | static int ufshcd_slave_alloc(struct scsi_device *sdev) |
| 4591 | { |
| 4592 | struct ufs_hba *hba; |
| 4593 | |
| 4594 | hba = shost_priv(sdev->host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4595 | |
| 4596 | /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */ |
| 4597 | sdev->use_10_for_ms = 1; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4598 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4599 | /* allow SCSI layer to restart the device in case of errors */ |
| 4600 | sdev->allow_restart = 1; |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4601 | |
Sujit Reddy Thumma | b2a6c52 | 2014-07-01 12:22:38 +0300 | [diff] [blame] | 4602 | /* REPORT SUPPORTED OPERATION CODES is not supported */ |
| 4603 | sdev->no_report_opcodes = 1; |
| 4604 | |
Sujit Reddy Thumma | 84af7e8 | 2018-01-24 09:52:35 +0530 | [diff] [blame] | 4605 | /* WRITE_SAME command is not supported */ |
| 4606 | sdev->no_write_same = 1; |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4607 | |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4608 | ufshcd_set_queue_depth(sdev); |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4609 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4610 | ufshcd_get_lu_power_on_wp_status(hba, sdev); |
| 4611 | |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4612 | return 0; |
| 4613 | } |
| 4614 | |
| 4615 | /** |
| 4616 | * ufshcd_change_queue_depth - change queue depth |
| 4617 | * @sdev: pointer to SCSI device |
| 4618 | * @depth: required depth to set |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4619 | * |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4620 | * 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] | 4621 | */ |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4622 | 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] | 4623 | { |
| 4624 | struct ufs_hba *hba = shost_priv(sdev->host); |
| 4625 | |
| 4626 | if (depth > hba->nutrs) |
| 4627 | depth = hba->nutrs; |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4628 | return scsi_change_queue_depth(sdev, depth); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | /** |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 4632 | * ufshcd_slave_configure - adjust SCSI device configurations |
| 4633 | * @sdev: pointer to SCSI device |
| 4634 | */ |
| 4635 | static int ufshcd_slave_configure(struct scsi_device *sdev) |
| 4636 | { |
Stanley Chu | 49615ba | 2019-09-16 23:56:50 +0800 | [diff] [blame] | 4637 | struct ufs_hba *hba = shost_priv(sdev->host); |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 4638 | struct request_queue *q = sdev->request_queue; |
| 4639 | |
| 4640 | blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1); |
Stanley Chu | 49615ba | 2019-09-16 23:56:50 +0800 | [diff] [blame] | 4641 | |
| 4642 | if (ufshcd_is_rpm_autosuspend_allowed(hba)) |
| 4643 | sdev->rpm_autosuspend = 1; |
| 4644 | |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 4645 | return 0; |
| 4646 | } |
| 4647 | |
| 4648 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4649 | * ufshcd_slave_destroy - remove SCSI device configurations |
| 4650 | * @sdev: pointer to SCSI device |
| 4651 | */ |
| 4652 | static void ufshcd_slave_destroy(struct scsi_device *sdev) |
| 4653 | { |
| 4654 | struct ufs_hba *hba; |
| 4655 | |
| 4656 | hba = shost_priv(sdev->host); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4657 | /* Drop the reference as it won't be needed anymore */ |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 4658 | if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) { |
| 4659 | unsigned long flags; |
| 4660 | |
| 4661 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4662 | hba->sdev_ufs_device = NULL; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 4663 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4664 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4665 | } |
| 4666 | |
| 4667 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4668 | * 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] | 4669 | * @lrbp: pointer to local reference block of completed command |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4670 | * @scsi_status: SCSI command status |
| 4671 | * |
| 4672 | * Returns value base on SCSI command status |
| 4673 | */ |
| 4674 | static inline int |
| 4675 | ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status) |
| 4676 | { |
| 4677 | int result = 0; |
| 4678 | |
| 4679 | switch (scsi_status) { |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 4680 | case SAM_STAT_CHECK_CONDITION: |
| 4681 | ufshcd_copy_sense_data(lrbp); |
Tomas Winkler | 30eb2e4 | 2018-11-26 10:10:34 +0200 | [diff] [blame] | 4682 | /* fallthrough */ |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4683 | case SAM_STAT_GOOD: |
| 4684 | result |= DID_OK << 16 | |
| 4685 | COMMAND_COMPLETE << 8 | |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 4686 | scsi_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4687 | break; |
| 4688 | case SAM_STAT_TASK_SET_FULL: |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 4689 | case SAM_STAT_BUSY: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4690 | case SAM_STAT_TASK_ABORTED: |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 4691 | ufshcd_copy_sense_data(lrbp); |
| 4692 | result |= scsi_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4693 | break; |
| 4694 | default: |
| 4695 | result |= DID_ERROR << 16; |
| 4696 | break; |
| 4697 | } /* end of switch */ |
| 4698 | |
| 4699 | return result; |
| 4700 | } |
| 4701 | |
| 4702 | /** |
| 4703 | * ufshcd_transfer_rsp_status - Get overall status of the response |
| 4704 | * @hba: per adapter instance |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 4705 | * @lrbp: pointer to local reference block of completed command |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4706 | * |
| 4707 | * Returns result of the command to notify SCSI midlayer |
| 4708 | */ |
| 4709 | static inline int |
| 4710 | ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 4711 | { |
| 4712 | int result = 0; |
| 4713 | int scsi_status; |
| 4714 | int ocs; |
| 4715 | |
| 4716 | /* overall command status of utrd */ |
| 4717 | ocs = ufshcd_get_tr_ocs(lrbp); |
| 4718 | |
| 4719 | switch (ocs) { |
| 4720 | case OCS_SUCCESS: |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4721 | result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 4722 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4723 | switch (result) { |
| 4724 | case UPIU_TRANSACTION_RESPONSE: |
| 4725 | /* |
| 4726 | * get the response UPIU result to extract |
| 4727 | * the SCSI command status |
| 4728 | */ |
| 4729 | result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr); |
| 4730 | |
| 4731 | /* |
| 4732 | * get the result based on SCSI status response |
| 4733 | * to notify the SCSI midlayer of the command status |
| 4734 | */ |
| 4735 | scsi_status = result & MASK_SCSI_STATUS; |
| 4736 | result = ufshcd_scsi_cmd_status(lrbp, scsi_status); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4737 | |
Yaniv Gardi | f05ac2e | 2016-02-01 15:02:42 +0200 | [diff] [blame] | 4738 | /* |
| 4739 | * Currently we are only supporting BKOPs exception |
| 4740 | * events hence we can ignore BKOPs exception event |
| 4741 | * during power management callbacks. BKOPs exception |
| 4742 | * event is not expected to be raised in runtime suspend |
| 4743 | * callback as it allows the urgent bkops. |
| 4744 | * During system suspend, we are anyway forcefully |
| 4745 | * disabling the bkops and if urgent bkops is needed |
| 4746 | * it will be enabled on system resume. Long term |
| 4747 | * solution could be to abort the system suspend if |
| 4748 | * UFS device needs urgent BKOPs. |
| 4749 | */ |
| 4750 | if (!hba->pm_op_in_progress && |
| 4751 | ufshcd_is_exception_event(lrbp->ucd_rsp_ptr)) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4752 | schedule_work(&hba->eeh_work); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4753 | break; |
| 4754 | case UPIU_TRANSACTION_REJECT_UPIU: |
| 4755 | /* TODO: handle Reject UPIU Response */ |
| 4756 | result = DID_ERROR << 16; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 4757 | dev_err(hba->dev, |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4758 | "Reject UPIU not fully implemented\n"); |
| 4759 | break; |
| 4760 | default: |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4761 | dev_err(hba->dev, |
| 4762 | "Unexpected request response code = %x\n", |
| 4763 | result); |
Stanley Chu | e0347d8 | 2019-04-15 20:23:38 +0800 | [diff] [blame] | 4764 | result = DID_ERROR << 16; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4765 | break; |
| 4766 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4767 | break; |
| 4768 | case OCS_ABORTED: |
| 4769 | result |= DID_ABORT << 16; |
| 4770 | break; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4771 | case OCS_INVALID_COMMAND_STATUS: |
| 4772 | result |= DID_REQUEUE << 16; |
| 4773 | break; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4774 | case OCS_INVALID_CMD_TABLE_ATTR: |
| 4775 | case OCS_INVALID_PRDT_ATTR: |
| 4776 | case OCS_MISMATCH_DATA_BUF_SIZE: |
| 4777 | case OCS_MISMATCH_RESP_UPIU_SIZE: |
| 4778 | case OCS_PEER_COMM_FAILURE: |
| 4779 | case OCS_FATAL_ERROR: |
| 4780 | default: |
| 4781 | result |= DID_ERROR << 16; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 4782 | dev_err(hba->dev, |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 4783 | "OCS error from controller = %x for tag %d\n", |
| 4784 | ocs, lrbp->task_tag); |
| 4785 | ufshcd_print_host_regs(hba); |
Gilad Broner | 6ba6558 | 2017-02-03 16:57:28 -0800 | [diff] [blame] | 4786 | ufshcd_print_host_state(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4787 | break; |
| 4788 | } /* end of switch */ |
| 4789 | |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 4790 | if (host_byte(result) != DID_OK) |
| 4791 | ufshcd_print_trs(hba, 1 << lrbp->task_tag, true); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4792 | return result; |
| 4793 | } |
| 4794 | |
| 4795 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4796 | * ufshcd_uic_cmd_compl - handle completion of uic command |
| 4797 | * @hba: per adapter instance |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4798 | * @intr_status: interrupt status generated by the controller |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4799 | */ |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4800 | static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4801 | { |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4802 | if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4803 | hba->active_uic_cmd->argument2 |= |
| 4804 | ufshcd_get_uic_cmd_result(hba); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 4805 | hba->active_uic_cmd->argument3 = |
| 4806 | ufshcd_get_dme_attr_val(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4807 | complete(&hba->active_uic_cmd->done); |
| 4808 | } |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4809 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4810 | if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done) |
| 4811 | complete(hba->uic_async_done); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4812 | } |
| 4813 | |
| 4814 | /** |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4815 | * __ufshcd_transfer_req_compl - handle SCSI and query command completion |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4816 | * @hba: per adapter instance |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4817 | * @completed_reqs: requests to complete |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4818 | */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4819 | static void __ufshcd_transfer_req_compl(struct ufs_hba *hba, |
| 4820 | unsigned long completed_reqs) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4821 | { |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4822 | struct ufshcd_lrb *lrbp; |
| 4823 | struct scsi_cmnd *cmd; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4824 | int result; |
| 4825 | int index; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4826 | |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4827 | for_each_set_bit(index, &completed_reqs, hba->nutrs) { |
| 4828 | lrbp = &hba->lrb[index]; |
| 4829 | cmd = lrbp->cmd; |
| 4830 | if (cmd) { |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 4831 | ufshcd_add_command_trace(hba, index, "complete"); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4832 | result = ufshcd_transfer_rsp_status(hba, lrbp); |
| 4833 | scsi_dma_unmap(cmd); |
| 4834 | cmd->result = result; |
| 4835 | /* Mark completed command as NULL in LRB */ |
| 4836 | lrbp->cmd = NULL; |
| 4837 | clear_bit_unlock(index, &hba->lrb_in_use); |
| 4838 | /* Do not touch lrbp after scsi done */ |
| 4839 | cmd->scsi_done(cmd); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4840 | __ufshcd_release(hba); |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 4841 | } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE || |
| 4842 | lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) { |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 4843 | if (hba->dev_cmd.complete) { |
| 4844 | ufshcd_add_command_trace(hba, index, |
| 4845 | "dev_complete"); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4846 | complete(hba->dev_cmd.complete); |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 4847 | } |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4848 | } |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 4849 | if (ufshcd_is_clkscaling_supported(hba)) |
| 4850 | hba->clk_scaling.active_reqs--; |
Zang Leigang | 0901718 | 2017-09-27 10:06:06 +0800 | [diff] [blame] | 4851 | |
| 4852 | lrbp->compl_time_stamp = ktime_get(); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4853 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4854 | |
| 4855 | /* clear corresponding bits of completed commands */ |
| 4856 | hba->outstanding_reqs ^= completed_reqs; |
| 4857 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 4858 | ufshcd_clk_scaling_update_busy(hba); |
| 4859 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4860 | /* we might have free'd some tags above */ |
| 4861 | wake_up(&hba->dev_cmd.tag_wq); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4862 | } |
| 4863 | |
| 4864 | /** |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4865 | * ufshcd_transfer_req_compl - handle SCSI and query command completion |
| 4866 | * @hba: per adapter instance |
| 4867 | */ |
| 4868 | static void ufshcd_transfer_req_compl(struct ufs_hba *hba) |
| 4869 | { |
| 4870 | unsigned long completed_reqs; |
| 4871 | u32 tr_doorbell; |
| 4872 | |
| 4873 | /* Resetting interrupt aggregation counters first and reading the |
| 4874 | * DOOR_BELL afterward allows us to handle all the completed requests. |
| 4875 | * In order to prevent other interrupts starvation the DB is read once |
| 4876 | * after reset. The down side of this solution is the possibility of |
| 4877 | * false interrupt if device completes another request after resetting |
| 4878 | * aggregation and before reading the DB. |
| 4879 | */ |
Alim Akhtar | 5ac6abc | 2018-05-06 15:44:16 +0530 | [diff] [blame] | 4880 | if (ufshcd_is_intr_aggr_allowed(hba) && |
| 4881 | !(hba->quirks & UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR)) |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4882 | ufshcd_reset_intr_aggr(hba); |
| 4883 | |
| 4884 | tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 4885 | completed_reqs = tr_doorbell ^ hba->outstanding_reqs; |
| 4886 | |
| 4887 | __ufshcd_transfer_req_compl(hba, completed_reqs); |
| 4888 | } |
| 4889 | |
| 4890 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4891 | * ufshcd_disable_ee - disable exception event |
| 4892 | * @hba: per-adapter instance |
| 4893 | * @mask: exception event to disable |
| 4894 | * |
| 4895 | * Disables exception event in the device so that the EVENT_ALERT |
| 4896 | * bit is not set. |
| 4897 | * |
| 4898 | * Returns zero on success, non-zero error value on failure. |
| 4899 | */ |
| 4900 | static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask) |
| 4901 | { |
| 4902 | int err = 0; |
| 4903 | u32 val; |
| 4904 | |
| 4905 | if (!(hba->ee_ctrl_mask & mask)) |
| 4906 | goto out; |
| 4907 | |
| 4908 | val = hba->ee_ctrl_mask & ~mask; |
Tomohiro Kusumi | d7e2ddd | 2017-04-20 15:01:44 +0300 | [diff] [blame] | 4909 | val &= MASK_EE_STATUS; |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 4910 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4911 | QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val); |
| 4912 | if (!err) |
| 4913 | hba->ee_ctrl_mask &= ~mask; |
| 4914 | out: |
| 4915 | return err; |
| 4916 | } |
| 4917 | |
| 4918 | /** |
| 4919 | * ufshcd_enable_ee - enable exception event |
| 4920 | * @hba: per-adapter instance |
| 4921 | * @mask: exception event to enable |
| 4922 | * |
| 4923 | * Enable corresponding exception event in the device to allow |
| 4924 | * device to alert host in critical scenarios. |
| 4925 | * |
| 4926 | * Returns zero on success, non-zero error value on failure. |
| 4927 | */ |
| 4928 | static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask) |
| 4929 | { |
| 4930 | int err = 0; |
| 4931 | u32 val; |
| 4932 | |
| 4933 | if (hba->ee_ctrl_mask & mask) |
| 4934 | goto out; |
| 4935 | |
| 4936 | val = hba->ee_ctrl_mask | mask; |
Tomohiro Kusumi | d7e2ddd | 2017-04-20 15:01:44 +0300 | [diff] [blame] | 4937 | val &= MASK_EE_STATUS; |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 4938 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4939 | QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val); |
| 4940 | if (!err) |
| 4941 | hba->ee_ctrl_mask |= mask; |
| 4942 | out: |
| 4943 | return err; |
| 4944 | } |
| 4945 | |
| 4946 | /** |
| 4947 | * ufshcd_enable_auto_bkops - Allow device managed BKOPS |
| 4948 | * @hba: per-adapter instance |
| 4949 | * |
| 4950 | * Allow device to manage background operations on its own. Enabling |
| 4951 | * this might lead to inconsistent latencies during normal data transfers |
| 4952 | * as the device is allowed to manage its own way of handling background |
| 4953 | * operations. |
| 4954 | * |
| 4955 | * Returns zero on success, non-zero on failure. |
| 4956 | */ |
| 4957 | static int ufshcd_enable_auto_bkops(struct ufs_hba *hba) |
| 4958 | { |
| 4959 | int err = 0; |
| 4960 | |
| 4961 | if (hba->auto_bkops_enabled) |
| 4962 | goto out; |
| 4963 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 4964 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4965 | QUERY_FLAG_IDN_BKOPS_EN, NULL); |
| 4966 | if (err) { |
| 4967 | dev_err(hba->dev, "%s: failed to enable bkops %d\n", |
| 4968 | __func__, err); |
| 4969 | goto out; |
| 4970 | } |
| 4971 | |
| 4972 | hba->auto_bkops_enabled = true; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 4973 | trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Enabled"); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4974 | |
| 4975 | /* No need of URGENT_BKOPS exception from the device */ |
| 4976 | err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 4977 | if (err) |
| 4978 | dev_err(hba->dev, "%s: failed to disable exception event %d\n", |
| 4979 | __func__, err); |
| 4980 | out: |
| 4981 | return err; |
| 4982 | } |
| 4983 | |
| 4984 | /** |
| 4985 | * ufshcd_disable_auto_bkops - block device in doing background operations |
| 4986 | * @hba: per-adapter instance |
| 4987 | * |
| 4988 | * Disabling background operations improves command response latency but |
| 4989 | * has drawback of device moving into critical state where the device is |
| 4990 | * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the |
| 4991 | * host is idle so that BKOPS are managed effectively without any negative |
| 4992 | * impacts. |
| 4993 | * |
| 4994 | * Returns zero on success, non-zero on failure. |
| 4995 | */ |
| 4996 | static int ufshcd_disable_auto_bkops(struct ufs_hba *hba) |
| 4997 | { |
| 4998 | int err = 0; |
| 4999 | |
| 5000 | if (!hba->auto_bkops_enabled) |
| 5001 | goto out; |
| 5002 | |
| 5003 | /* |
| 5004 | * If host assisted BKOPs is to be enabled, make sure |
| 5005 | * urgent bkops exception is allowed. |
| 5006 | */ |
| 5007 | err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 5008 | if (err) { |
| 5009 | dev_err(hba->dev, "%s: failed to enable exception event %d\n", |
| 5010 | __func__, err); |
| 5011 | goto out; |
| 5012 | } |
| 5013 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 5014 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5015 | QUERY_FLAG_IDN_BKOPS_EN, NULL); |
| 5016 | if (err) { |
| 5017 | dev_err(hba->dev, "%s: failed to disable bkops %d\n", |
| 5018 | __func__, err); |
| 5019 | ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 5020 | goto out; |
| 5021 | } |
| 5022 | |
| 5023 | hba->auto_bkops_enabled = false; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 5024 | trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Disabled"); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5025 | out: |
| 5026 | return err; |
| 5027 | } |
| 5028 | |
| 5029 | /** |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 5030 | * ufshcd_force_reset_auto_bkops - force reset auto bkops state |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5031 | * @hba: per adapter instance |
| 5032 | * |
| 5033 | * After a device reset the device may toggle the BKOPS_EN flag |
| 5034 | * to default value. The s/w tracking variables should be updated |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 5035 | * as well. This function would change the auto-bkops state based on |
| 5036 | * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5037 | */ |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 5038 | static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5039 | { |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 5040 | if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) { |
| 5041 | hba->auto_bkops_enabled = false; |
| 5042 | hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS; |
| 5043 | ufshcd_enable_auto_bkops(hba); |
| 5044 | } else { |
| 5045 | hba->auto_bkops_enabled = true; |
| 5046 | hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS; |
| 5047 | ufshcd_disable_auto_bkops(hba); |
| 5048 | } |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5049 | } |
| 5050 | |
| 5051 | static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status) |
| 5052 | { |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 5053 | return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5054 | QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status); |
| 5055 | } |
| 5056 | |
| 5057 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5058 | * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status |
| 5059 | * @hba: per-adapter instance |
| 5060 | * @status: bkops_status value |
| 5061 | * |
| 5062 | * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn |
| 5063 | * flag in the device to permit background operations if the device |
| 5064 | * bkops_status is greater than or equal to "status" argument passed to |
| 5065 | * this function, disable otherwise. |
| 5066 | * |
| 5067 | * Returns 0 for success, non-zero in case of failure. |
| 5068 | * |
| 5069 | * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag |
| 5070 | * to know whether auto bkops is enabled or disabled after this function |
| 5071 | * returns control to it. |
| 5072 | */ |
| 5073 | static int ufshcd_bkops_ctrl(struct ufs_hba *hba, |
| 5074 | enum bkops_status status) |
| 5075 | { |
| 5076 | int err; |
| 5077 | u32 curr_status = 0; |
| 5078 | |
| 5079 | err = ufshcd_get_bkops_status(hba, &curr_status); |
| 5080 | if (err) { |
| 5081 | dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", |
| 5082 | __func__, err); |
| 5083 | goto out; |
| 5084 | } else if (curr_status > BKOPS_STATUS_MAX) { |
| 5085 | dev_err(hba->dev, "%s: invalid BKOPS status %d\n", |
| 5086 | __func__, curr_status); |
| 5087 | err = -EINVAL; |
| 5088 | goto out; |
| 5089 | } |
| 5090 | |
| 5091 | if (curr_status >= status) |
| 5092 | err = ufshcd_enable_auto_bkops(hba); |
| 5093 | else |
| 5094 | err = ufshcd_disable_auto_bkops(hba); |
| 5095 | out: |
| 5096 | return err; |
| 5097 | } |
| 5098 | |
| 5099 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5100 | * ufshcd_urgent_bkops - handle urgent bkops exception event |
| 5101 | * @hba: per-adapter instance |
| 5102 | * |
| 5103 | * Enable fBackgroundOpsEn flag in the device to permit background |
| 5104 | * operations. |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5105 | * |
| 5106 | * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled |
| 5107 | * and negative error value for any other failure. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5108 | */ |
| 5109 | static int ufshcd_urgent_bkops(struct ufs_hba *hba) |
| 5110 | { |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 5111 | return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5112 | } |
| 5113 | |
| 5114 | static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status) |
| 5115 | { |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 5116 | return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5117 | QUERY_ATTR_IDN_EE_STATUS, 0, 0, status); |
| 5118 | } |
| 5119 | |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 5120 | static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba) |
| 5121 | { |
| 5122 | int err; |
| 5123 | u32 curr_status = 0; |
| 5124 | |
| 5125 | if (hba->is_urgent_bkops_lvl_checked) |
| 5126 | goto enable_auto_bkops; |
| 5127 | |
| 5128 | err = ufshcd_get_bkops_status(hba, &curr_status); |
| 5129 | if (err) { |
| 5130 | dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", |
| 5131 | __func__, err); |
| 5132 | goto out; |
| 5133 | } |
| 5134 | |
| 5135 | /* |
| 5136 | * We are seeing that some devices are raising the urgent bkops |
| 5137 | * exception events even when BKOPS status doesn't indicate performace |
| 5138 | * impacted or critical. Handle these device by determining their urgent |
| 5139 | * bkops status at runtime. |
| 5140 | */ |
| 5141 | if (curr_status < BKOPS_STATUS_PERF_IMPACT) { |
| 5142 | dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n", |
| 5143 | __func__, curr_status); |
| 5144 | /* update the current status as the urgent bkops level */ |
| 5145 | hba->urgent_bkops_lvl = curr_status; |
| 5146 | hba->is_urgent_bkops_lvl_checked = true; |
| 5147 | } |
| 5148 | |
| 5149 | enable_auto_bkops: |
| 5150 | err = ufshcd_enable_auto_bkops(hba); |
| 5151 | out: |
| 5152 | if (err < 0) |
| 5153 | dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n", |
| 5154 | __func__, err); |
| 5155 | } |
| 5156 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5157 | /** |
| 5158 | * ufshcd_exception_event_handler - handle exceptions raised by device |
| 5159 | * @work: pointer to work data |
| 5160 | * |
| 5161 | * Read bExceptionEventStatus attribute from the device and handle the |
| 5162 | * exception event accordingly. |
| 5163 | */ |
| 5164 | static void ufshcd_exception_event_handler(struct work_struct *work) |
| 5165 | { |
| 5166 | struct ufs_hba *hba; |
| 5167 | int err; |
| 5168 | u32 status = 0; |
| 5169 | hba = container_of(work, struct ufs_hba, eeh_work); |
| 5170 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 5171 | pm_runtime_get_sync(hba->dev); |
Maya Erez | 2e3611e9 | 2018-05-03 16:37:16 +0530 | [diff] [blame] | 5172 | scsi_block_requests(hba->host); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5173 | err = ufshcd_get_ee_status(hba, &status); |
| 5174 | if (err) { |
| 5175 | dev_err(hba->dev, "%s: failed to get exception status %d\n", |
| 5176 | __func__, err); |
| 5177 | goto out; |
| 5178 | } |
| 5179 | |
| 5180 | status &= hba->ee_ctrl_mask; |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 5181 | |
| 5182 | if (status & MASK_EE_URGENT_BKOPS) |
| 5183 | ufshcd_bkops_exception_event_handler(hba); |
| 5184 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5185 | out: |
Maya Erez | 2e3611e9 | 2018-05-03 16:37:16 +0530 | [diff] [blame] | 5186 | scsi_unblock_requests(hba->host); |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 5187 | pm_runtime_put_sync(hba->dev); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5188 | return; |
| 5189 | } |
| 5190 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5191 | /* Complete requests that have door-bell cleared */ |
| 5192 | static void ufshcd_complete_requests(struct ufs_hba *hba) |
| 5193 | { |
| 5194 | ufshcd_transfer_req_compl(hba); |
| 5195 | ufshcd_tmc_handler(hba); |
| 5196 | } |
| 5197 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5198 | /** |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5199 | * ufshcd_quirk_dl_nac_errors - This function checks if error handling is |
| 5200 | * to recover from the DL NAC errors or not. |
| 5201 | * @hba: per-adapter instance |
| 5202 | * |
| 5203 | * Returns true if error handling is required, false otherwise |
| 5204 | */ |
| 5205 | static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba) |
| 5206 | { |
| 5207 | unsigned long flags; |
| 5208 | bool err_handling = true; |
| 5209 | |
| 5210 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5211 | /* |
| 5212 | * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the |
| 5213 | * device fatal error and/or DL NAC & REPLAY timeout errors. |
| 5214 | */ |
| 5215 | if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR)) |
| 5216 | goto out; |
| 5217 | |
| 5218 | if ((hba->saved_err & DEVICE_FATAL_ERROR) || |
| 5219 | ((hba->saved_err & UIC_ERROR) && |
| 5220 | (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR))) |
| 5221 | goto out; |
| 5222 | |
| 5223 | if ((hba->saved_err & UIC_ERROR) && |
| 5224 | (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) { |
| 5225 | int err; |
| 5226 | /* |
| 5227 | * wait for 50ms to see if we can get any other errors or not. |
| 5228 | */ |
| 5229 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5230 | msleep(50); |
| 5231 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5232 | |
| 5233 | /* |
| 5234 | * now check if we have got any other severe errors other than |
| 5235 | * DL NAC error? |
| 5236 | */ |
| 5237 | if ((hba->saved_err & INT_FATAL_ERRORS) || |
| 5238 | ((hba->saved_err & UIC_ERROR) && |
| 5239 | (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR))) |
| 5240 | goto out; |
| 5241 | |
| 5242 | /* |
| 5243 | * As DL NAC is the only error received so far, send out NOP |
| 5244 | * command to confirm if link is still active or not. |
| 5245 | * - If we don't get any response then do error recovery. |
| 5246 | * - If we get response then clear the DL NAC error bit. |
| 5247 | */ |
| 5248 | |
| 5249 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5250 | err = ufshcd_verify_dev_init(hba); |
| 5251 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5252 | |
| 5253 | if (err) |
| 5254 | goto out; |
| 5255 | |
| 5256 | /* Link seems to be alive hence ignore the DL NAC errors */ |
| 5257 | if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR) |
| 5258 | hba->saved_err &= ~UIC_ERROR; |
| 5259 | /* clear NAC error */ |
| 5260 | hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR; |
| 5261 | if (!hba->saved_uic_err) { |
| 5262 | err_handling = false; |
| 5263 | goto out; |
| 5264 | } |
| 5265 | } |
| 5266 | out: |
| 5267 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5268 | return err_handling; |
| 5269 | } |
| 5270 | |
| 5271 | /** |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5272 | * ufshcd_err_handler - handle UFS errors that require s/w attention |
| 5273 | * @work: pointer to work structure |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5274 | */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5275 | static void ufshcd_err_handler(struct work_struct *work) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5276 | { |
| 5277 | struct ufs_hba *hba; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5278 | unsigned long flags; |
| 5279 | u32 err_xfer = 0; |
| 5280 | u32 err_tm = 0; |
| 5281 | int err = 0; |
| 5282 | int tag; |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5283 | bool needs_reset = false; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5284 | |
| 5285 | hba = container_of(work, struct ufs_hba, eh_work); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5286 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 5287 | pm_runtime_get_sync(hba->dev); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5288 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5289 | |
| 5290 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5291 | if (hba->ufshcd_state == UFSHCD_STATE_RESET) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5292 | goto out; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5293 | |
| 5294 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 5295 | ufshcd_set_eh_in_progress(hba); |
| 5296 | |
| 5297 | /* Complete requests that have door-bell cleared by h/w */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5298 | ufshcd_complete_requests(hba); |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5299 | |
| 5300 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) { |
| 5301 | bool ret; |
| 5302 | |
| 5303 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5304 | /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */ |
| 5305 | ret = ufshcd_quirk_dl_nac_errors(hba); |
| 5306 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5307 | if (!ret) |
| 5308 | goto skip_err_handling; |
| 5309 | } |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5310 | if ((hba->saved_err & INT_FATAL_ERRORS) || |
Stanley Chu | 8217444 | 2019-05-21 14:44:54 +0800 | [diff] [blame] | 5311 | (hba->saved_err & UFSHCD_UIC_HIBERN8_MASK) || |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5312 | ((hba->saved_err & UIC_ERROR) && |
| 5313 | (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR | |
| 5314 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR | |
| 5315 | UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))) |
| 5316 | needs_reset = true; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5317 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5318 | /* |
| 5319 | * if host reset is required then skip clearing the pending |
| 5320 | * transfers forcefully because they will automatically get |
| 5321 | * cleared after link startup. |
| 5322 | */ |
| 5323 | if (needs_reset) |
| 5324 | goto skip_pending_xfer_clear; |
| 5325 | |
| 5326 | /* release lock as clear command might sleep */ |
| 5327 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5328 | /* Clear pending transfer requests */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5329 | for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) { |
| 5330 | if (ufshcd_clear_cmd(hba, tag)) { |
| 5331 | err_xfer = true; |
| 5332 | goto lock_skip_pending_xfer_clear; |
| 5333 | } |
| 5334 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5335 | |
| 5336 | /* Clear pending task management requests */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5337 | for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) { |
| 5338 | if (ufshcd_clear_tm_cmd(hba, tag)) { |
| 5339 | err_tm = true; |
| 5340 | goto lock_skip_pending_xfer_clear; |
| 5341 | } |
| 5342 | } |
| 5343 | |
| 5344 | lock_skip_pending_xfer_clear: |
| 5345 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5346 | |
| 5347 | /* Complete the requests that are cleared by s/w */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5348 | ufshcd_complete_requests(hba); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5349 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5350 | if (err_xfer || err_tm) |
| 5351 | needs_reset = true; |
| 5352 | |
| 5353 | skip_pending_xfer_clear: |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5354 | /* Fatal errors need reset */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5355 | if (needs_reset) { |
| 5356 | unsigned long max_doorbells = (1UL << hba->nutrs) - 1; |
| 5357 | |
| 5358 | /* |
| 5359 | * ufshcd_reset_and_restore() does the link reinitialization |
| 5360 | * which will need atleast one empty doorbell slot to send the |
| 5361 | * device management commands (NOP and query commands). |
| 5362 | * If there is no slot empty at this moment then free up last |
| 5363 | * slot forcefully. |
| 5364 | */ |
| 5365 | if (hba->outstanding_reqs == max_doorbells) |
| 5366 | __ufshcd_transfer_req_compl(hba, |
| 5367 | (1UL << (hba->nutrs - 1))); |
| 5368 | |
| 5369 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5370 | err = ufshcd_reset_and_restore(hba); |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5371 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5372 | if (err) { |
| 5373 | dev_err(hba->dev, "%s: reset and restore failed\n", |
| 5374 | __func__); |
| 5375 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 5376 | } |
| 5377 | /* |
| 5378 | * Inform scsi mid-layer that we did reset and allow to handle |
| 5379 | * Unit Attention properly. |
| 5380 | */ |
| 5381 | scsi_report_bus_reset(hba->host, 0); |
| 5382 | hba->saved_err = 0; |
| 5383 | hba->saved_uic_err = 0; |
| 5384 | } |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5385 | |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5386 | skip_err_handling: |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5387 | if (!needs_reset) { |
| 5388 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
| 5389 | if (hba->saved_err || hba->saved_uic_err) |
| 5390 | dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x", |
| 5391 | __func__, hba->saved_err, hba->saved_uic_err); |
| 5392 | } |
| 5393 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5394 | ufshcd_clear_eh_in_progress(hba); |
| 5395 | |
| 5396 | out: |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5397 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 5398 | ufshcd_scsi_unblock_requests(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5399 | ufshcd_release(hba); |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 5400 | pm_runtime_put_sync(hba->dev); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5401 | } |
| 5402 | |
| 5403 | /** |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5404 | * ufshcd_update_uic_error - check and set fatal UIC error flags. |
| 5405 | * @hba: per-adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5406 | */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5407 | static void ufshcd_update_uic_error(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5408 | { |
| 5409 | u32 reg; |
| 5410 | |
Dolev Raviv | fb7b45f | 2016-11-23 16:32:32 -0800 | [diff] [blame] | 5411 | /* PHY layer lane error */ |
| 5412 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER); |
| 5413 | /* Ignore LINERESET indication, as this is not an error */ |
| 5414 | if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) && |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5415 | (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK)) { |
Dolev Raviv | fb7b45f | 2016-11-23 16:32:32 -0800 | [diff] [blame] | 5416 | /* |
| 5417 | * To know whether this error is fatal or not, DB timeout |
| 5418 | * must be checked but this error is handled separately. |
| 5419 | */ |
| 5420 | dev_dbg(hba->dev, "%s: UIC Lane error reported\n", __func__); |
Stanley Chu | 48d5b97 | 2019-07-10 21:38:18 +0800 | [diff] [blame] | 5421 | ufshcd_update_reg_hist(&hba->ufs_stats.pa_err, reg); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5422 | } |
Dolev Raviv | fb7b45f | 2016-11-23 16:32:32 -0800 | [diff] [blame] | 5423 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5424 | /* PA_INIT_ERROR is fatal and needs UIC reset */ |
| 5425 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5426 | if (reg) |
Stanley Chu | 48d5b97 | 2019-07-10 21:38:18 +0800 | [diff] [blame] | 5427 | ufshcd_update_reg_hist(&hba->ufs_stats.dl_err, reg); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5428 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5429 | if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT) |
| 5430 | hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5431 | else if (hba->dev_quirks & |
| 5432 | UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) { |
| 5433 | if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED) |
| 5434 | hba->uic_error |= |
| 5435 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR; |
| 5436 | else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT) |
| 5437 | hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR; |
| 5438 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5439 | |
| 5440 | /* UIC NL/TL/DME errors needs software retry */ |
| 5441 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5442 | if (reg) { |
Stanley Chu | 48d5b97 | 2019-07-10 21:38:18 +0800 | [diff] [blame] | 5443 | ufshcd_update_reg_hist(&hba->ufs_stats.nl_err, reg); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5444 | hba->uic_error |= UFSHCD_UIC_NL_ERROR; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5445 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5446 | |
| 5447 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5448 | if (reg) { |
Stanley Chu | 48d5b97 | 2019-07-10 21:38:18 +0800 | [diff] [blame] | 5449 | ufshcd_update_reg_hist(&hba->ufs_stats.tl_err, reg); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5450 | hba->uic_error |= UFSHCD_UIC_TL_ERROR; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5451 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5452 | |
| 5453 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5454 | if (reg) { |
Stanley Chu | 48d5b97 | 2019-07-10 21:38:18 +0800 | [diff] [blame] | 5455 | ufshcd_update_reg_hist(&hba->ufs_stats.dme_err, reg); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5456 | hba->uic_error |= UFSHCD_UIC_DME_ERROR; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5457 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5458 | |
| 5459 | dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n", |
| 5460 | __func__, hba->uic_error); |
| 5461 | } |
| 5462 | |
Stanley Chu | 8217444 | 2019-05-21 14:44:54 +0800 | [diff] [blame] | 5463 | static bool ufshcd_is_auto_hibern8_error(struct ufs_hba *hba, |
| 5464 | u32 intr_mask) |
| 5465 | { |
| 5466 | if (!ufshcd_is_auto_hibern8_supported(hba)) |
| 5467 | return false; |
| 5468 | |
| 5469 | if (!(intr_mask & UFSHCD_UIC_HIBERN8_MASK)) |
| 5470 | return false; |
| 5471 | |
| 5472 | if (hba->active_uic_cmd && |
| 5473 | (hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_ENTER || |
| 5474 | hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_EXIT)) |
| 5475 | return false; |
| 5476 | |
| 5477 | return true; |
| 5478 | } |
| 5479 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5480 | /** |
| 5481 | * ufshcd_check_errors - Check for errors that need s/w attention |
| 5482 | * @hba: per-adapter instance |
| 5483 | */ |
| 5484 | static void ufshcd_check_errors(struct ufs_hba *hba) |
| 5485 | { |
| 5486 | bool queue_eh_work = false; |
| 5487 | |
Stanley Chu | d3c615b | 2019-07-10 21:38:19 +0800 | [diff] [blame] | 5488 | if (hba->errors & INT_FATAL_ERRORS) { |
| 5489 | ufshcd_update_reg_hist(&hba->ufs_stats.fatal_err, hba->errors); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5490 | queue_eh_work = true; |
Stanley Chu | d3c615b | 2019-07-10 21:38:19 +0800 | [diff] [blame] | 5491 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5492 | |
| 5493 | if (hba->errors & UIC_ERROR) { |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5494 | hba->uic_error = 0; |
| 5495 | ufshcd_update_uic_error(hba); |
| 5496 | if (hba->uic_error) |
| 5497 | queue_eh_work = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5498 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5499 | |
Stanley Chu | 8217444 | 2019-05-21 14:44:54 +0800 | [diff] [blame] | 5500 | if (hba->errors & UFSHCD_UIC_HIBERN8_MASK) { |
| 5501 | dev_err(hba->dev, |
| 5502 | "%s: Auto Hibern8 %s failed - status: 0x%08x, upmcrs: 0x%08x\n", |
| 5503 | __func__, (hba->errors & UIC_HIBERNATE_ENTER) ? |
| 5504 | "Enter" : "Exit", |
| 5505 | hba->errors, ufshcd_get_upmcrs(hba)); |
Stanley Chu | d3c615b | 2019-07-10 21:38:19 +0800 | [diff] [blame] | 5506 | ufshcd_update_reg_hist(&hba->ufs_stats.auto_hibern8_err, |
| 5507 | hba->errors); |
Stanley Chu | 8217444 | 2019-05-21 14:44:54 +0800 | [diff] [blame] | 5508 | queue_eh_work = true; |
| 5509 | } |
| 5510 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5511 | if (queue_eh_work) { |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5512 | /* |
| 5513 | * update the transfer error masks to sticky bits, let's do this |
| 5514 | * irrespective of current ufshcd_state. |
| 5515 | */ |
| 5516 | hba->saved_err |= hba->errors; |
| 5517 | hba->saved_uic_err |= hba->uic_error; |
| 5518 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5519 | /* handle fatal errors only when link is functional */ |
| 5520 | if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) { |
| 5521 | /* block commands from scsi mid-layer */ |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 5522 | ufshcd_scsi_block_requests(hba); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5523 | |
Zang Leigang | 141f816 | 2016-11-16 11:29:37 +0800 | [diff] [blame] | 5524 | hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED; |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 5525 | |
| 5526 | /* dump controller state before resetting */ |
| 5527 | if (hba->saved_err & (INT_FATAL_ERRORS | UIC_ERROR)) { |
| 5528 | bool pr_prdt = !!(hba->saved_err & |
| 5529 | SYSTEM_BUS_FATAL_ERROR); |
| 5530 | |
| 5531 | dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x\n", |
| 5532 | __func__, hba->saved_err, |
| 5533 | hba->saved_uic_err); |
| 5534 | |
| 5535 | ufshcd_print_host_regs(hba); |
| 5536 | ufshcd_print_pwr_info(hba); |
| 5537 | ufshcd_print_tmrs(hba, hba->outstanding_tasks); |
| 5538 | ufshcd_print_trs(hba, hba->outstanding_reqs, |
| 5539 | pr_prdt); |
| 5540 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5541 | schedule_work(&hba->eh_work); |
| 5542 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5543 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5544 | /* |
| 5545 | * if (!queue_eh_work) - |
| 5546 | * Other errors are either non-fatal where host recovers |
| 5547 | * itself without s/w intervention or errors that will be |
| 5548 | * handled by the SCSI core layer. |
| 5549 | */ |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5550 | } |
| 5551 | |
| 5552 | /** |
| 5553 | * ufshcd_tmc_handler - handle task management function completion |
| 5554 | * @hba: per adapter instance |
| 5555 | */ |
| 5556 | static void ufshcd_tmc_handler(struct ufs_hba *hba) |
| 5557 | { |
| 5558 | u32 tm_doorbell; |
| 5559 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 5560 | tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5561 | hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5562 | wake_up(&hba->tm_wq); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5563 | } |
| 5564 | |
| 5565 | /** |
| 5566 | * ufshcd_sl_intr - Interrupt service routine |
| 5567 | * @hba: per adapter instance |
| 5568 | * @intr_status: contains interrupts generated by the controller |
| 5569 | */ |
| 5570 | static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status) |
| 5571 | { |
| 5572 | hba->errors = UFSHCD_ERROR_MASK & intr_status; |
Stanley Chu | 8217444 | 2019-05-21 14:44:54 +0800 | [diff] [blame] | 5573 | |
| 5574 | if (ufshcd_is_auto_hibern8_error(hba, intr_status)) |
| 5575 | hba->errors |= (UFSHCD_UIC_HIBERN8_MASK & intr_status); |
| 5576 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5577 | if (hba->errors) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5578 | ufshcd_check_errors(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5579 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 5580 | if (intr_status & UFSHCD_UIC_MASK) |
| 5581 | ufshcd_uic_cmd_compl(hba, intr_status); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5582 | |
| 5583 | if (intr_status & UTP_TASK_REQ_COMPL) |
| 5584 | ufshcd_tmc_handler(hba); |
| 5585 | |
| 5586 | if (intr_status & UTP_TRANSFER_REQ_COMPL) |
| 5587 | ufshcd_transfer_req_compl(hba); |
| 5588 | } |
| 5589 | |
| 5590 | /** |
| 5591 | * ufshcd_intr - Main interrupt service routine |
| 5592 | * @irq: irq number |
| 5593 | * @__hba: pointer to adapter instance |
| 5594 | * |
| 5595 | * Returns IRQ_HANDLED - If interrupt is valid |
| 5596 | * IRQ_NONE - If invalid interrupt |
| 5597 | */ |
| 5598 | static irqreturn_t ufshcd_intr(int irq, void *__hba) |
| 5599 | { |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 5600 | u32 intr_status, enabled_intr_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5601 | irqreturn_t retval = IRQ_NONE; |
| 5602 | struct ufs_hba *hba = __hba; |
Venkat Gopalakrishnan | 7f6ba4f | 2018-05-03 16:37:20 +0530 | [diff] [blame] | 5603 | int retries = hba->nutrs; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5604 | |
| 5605 | spin_lock(hba->host->host_lock); |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 5606 | intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5607 | |
Venkat Gopalakrishnan | 7f6ba4f | 2018-05-03 16:37:20 +0530 | [diff] [blame] | 5608 | /* |
| 5609 | * There could be max of hba->nutrs reqs in flight and in worst case |
| 5610 | * if the reqs get finished 1 by 1 after the interrupt status is |
| 5611 | * read, make sure we handle them by checking the interrupt status |
| 5612 | * again in a loop until we process all of the reqs before returning. |
| 5613 | */ |
| 5614 | do { |
| 5615 | enabled_intr_status = |
| 5616 | intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
| 5617 | if (intr_status) |
| 5618 | ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS); |
| 5619 | if (enabled_intr_status) { |
| 5620 | ufshcd_sl_intr(hba, enabled_intr_status); |
| 5621 | retval = IRQ_HANDLED; |
| 5622 | } |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 5623 | |
Venkat Gopalakrishnan | 7f6ba4f | 2018-05-03 16:37:20 +0530 | [diff] [blame] | 5624 | intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS); |
| 5625 | } while (intr_status && --retries); |
| 5626 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5627 | spin_unlock(hba->host->host_lock); |
| 5628 | return retval; |
| 5629 | } |
| 5630 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5631 | static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag) |
| 5632 | { |
| 5633 | int err = 0; |
| 5634 | u32 mask = 1 << tag; |
| 5635 | unsigned long flags; |
| 5636 | |
| 5637 | if (!test_bit(tag, &hba->outstanding_tasks)) |
| 5638 | goto out; |
| 5639 | |
| 5640 | spin_lock_irqsave(hba->host->host_lock, flags); |
Alim Akhtar | 1399c5b | 2018-05-06 15:44:15 +0530 | [diff] [blame] | 5641 | ufshcd_utmrl_clear(hba, tag); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5642 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5643 | |
| 5644 | /* poll for max. 1 sec to clear door bell register by h/w */ |
| 5645 | err = ufshcd_wait_for_register(hba, |
| 5646 | REG_UTP_TASK_REQ_DOOR_BELL, |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 5647 | mask, 0, 1000, 1000, true); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5648 | out: |
| 5649 | return err; |
| 5650 | } |
| 5651 | |
Christoph Hellwig | c6049cd | 2018-10-07 17:30:33 +0300 | [diff] [blame] | 5652 | static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba, |
| 5653 | struct utp_task_req_desc *treq, u8 tm_function) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5654 | { |
Christoph Hellwig | c6049cd | 2018-10-07 17:30:33 +0300 | [diff] [blame] | 5655 | struct Scsi_Host *host = hba->host; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5656 | unsigned long flags; |
Christoph Hellwig | c6049cd | 2018-10-07 17:30:33 +0300 | [diff] [blame] | 5657 | int free_slot, task_tag, err; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5658 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5659 | /* |
| 5660 | * Get free slot, sleep if slots are unavailable. |
| 5661 | * Even though we use wait_event() which sleeps indefinitely, |
| 5662 | * the maximum wait time is bounded by %TM_CMD_TIMEOUT. |
| 5663 | */ |
| 5664 | wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot)); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5665 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5666 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5667 | spin_lock_irqsave(host->host_lock, flags); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5668 | task_tag = hba->nutrs + free_slot; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5669 | |
Christoph Hellwig | c6049cd | 2018-10-07 17:30:33 +0300 | [diff] [blame] | 5670 | treq->req_header.dword_0 |= cpu_to_be32(task_tag); |
| 5671 | |
| 5672 | memcpy(hba->utmrdl_base_addr + free_slot, treq, sizeof(*treq)); |
Kiwoong Kim | d2877be | 2016-11-10 21:16:15 +0900 | [diff] [blame] | 5673 | ufshcd_vops_setup_task_mgmt(hba, free_slot, tm_function); |
| 5674 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5675 | /* send command to the controller */ |
| 5676 | __set_bit(free_slot, &hba->outstanding_tasks); |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 5677 | |
| 5678 | /* Make sure descriptors are ready before ringing the task doorbell */ |
| 5679 | wmb(); |
| 5680 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 5681 | ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL); |
Gilad Broner | ad1a1b9 | 2016-10-17 17:09:36 -0700 | [diff] [blame] | 5682 | /* Make sure that doorbell is committed immediately */ |
| 5683 | wmb(); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5684 | |
| 5685 | spin_unlock_irqrestore(host->host_lock, flags); |
| 5686 | |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 5687 | ufshcd_add_tm_upiu_trace(hba, task_tag, "tm_send"); |
| 5688 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5689 | /* wait until the task management command is completed */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5690 | err = wait_event_timeout(hba->tm_wq, |
| 5691 | test_bit(free_slot, &hba->tm_condition), |
| 5692 | msecs_to_jiffies(TM_CMD_TIMEOUT)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5693 | if (!err) { |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 5694 | ufshcd_add_tm_upiu_trace(hba, task_tag, "tm_complete_err"); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5695 | dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n", |
| 5696 | __func__, tm_function); |
| 5697 | if (ufshcd_clear_tm_cmd(hba, free_slot)) |
| 5698 | dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n", |
| 5699 | __func__, free_slot); |
| 5700 | err = -ETIMEDOUT; |
| 5701 | } else { |
Christoph Hellwig | c6049cd | 2018-10-07 17:30:33 +0300 | [diff] [blame] | 5702 | err = 0; |
| 5703 | memcpy(treq, hba->utmrdl_base_addr + free_slot, sizeof(*treq)); |
| 5704 | |
Ohad Sharabi | 6667e6d | 2018-03-28 12:42:18 +0300 | [diff] [blame] | 5705 | ufshcd_add_tm_upiu_trace(hba, task_tag, "tm_complete"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5706 | } |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5707 | |
Stanley Chu | b557217 | 2019-08-19 21:43:28 +0800 | [diff] [blame] | 5708 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5709 | __clear_bit(free_slot, &hba->outstanding_tasks); |
| 5710 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5711 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5712 | clear_bit(free_slot, &hba->tm_condition); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5713 | ufshcd_put_tm_slot(hba, free_slot); |
| 5714 | wake_up(&hba->tm_tag_wq); |
| 5715 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5716 | ufshcd_release(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5717 | return err; |
| 5718 | } |
| 5719 | |
| 5720 | /** |
Christoph Hellwig | c6049cd | 2018-10-07 17:30:33 +0300 | [diff] [blame] | 5721 | * ufshcd_issue_tm_cmd - issues task management commands to controller |
| 5722 | * @hba: per adapter instance |
| 5723 | * @lun_id: LUN ID to which TM command is sent |
| 5724 | * @task_id: task ID to which the TM command is applicable |
| 5725 | * @tm_function: task management function opcode |
| 5726 | * @tm_response: task management service response return value |
| 5727 | * |
| 5728 | * Returns non-zero value on error, zero on success. |
| 5729 | */ |
| 5730 | static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id, |
| 5731 | u8 tm_function, u8 *tm_response) |
| 5732 | { |
| 5733 | struct utp_task_req_desc treq = { { 0 }, }; |
| 5734 | int ocs_value, err; |
| 5735 | |
| 5736 | /* Configure task request descriptor */ |
| 5737 | treq.header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD); |
| 5738 | treq.header.dword_2 = cpu_to_le32(OCS_INVALID_COMMAND_STATUS); |
| 5739 | |
| 5740 | /* Configure task request UPIU */ |
| 5741 | treq.req_header.dword_0 = cpu_to_be32(lun_id << 8) | |
| 5742 | cpu_to_be32(UPIU_TRANSACTION_TASK_REQ << 24); |
| 5743 | treq.req_header.dword_1 = cpu_to_be32(tm_function << 16); |
| 5744 | |
| 5745 | /* |
| 5746 | * The host shall provide the same value for LUN field in the basic |
| 5747 | * header and for Input Parameter. |
| 5748 | */ |
| 5749 | treq.input_param1 = cpu_to_be32(lun_id); |
| 5750 | treq.input_param2 = cpu_to_be32(task_id); |
| 5751 | |
| 5752 | err = __ufshcd_issue_tm_cmd(hba, &treq, tm_function); |
| 5753 | if (err == -ETIMEDOUT) |
| 5754 | return err; |
| 5755 | |
| 5756 | ocs_value = le32_to_cpu(treq.header.dword_2) & MASK_OCS; |
| 5757 | if (ocs_value != OCS_SUCCESS) |
| 5758 | dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", |
| 5759 | __func__, ocs_value); |
| 5760 | else if (tm_response) |
| 5761 | *tm_response = be32_to_cpu(treq.output_param1) & |
| 5762 | MASK_TM_SERVICE_RESP; |
| 5763 | return err; |
| 5764 | } |
| 5765 | |
| 5766 | /** |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 5767 | * ufshcd_issue_devman_upiu_cmd - API for sending "utrd" type requests |
| 5768 | * @hba: per-adapter instance |
| 5769 | * @req_upiu: upiu request |
| 5770 | * @rsp_upiu: upiu reply |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 5771 | * @desc_buff: pointer to descriptor buffer, NULL if NA |
| 5772 | * @buff_len: descriptor size, 0 if NA |
Bart Van Assche | d0e9760 | 2019-10-29 16:07:08 -0700 | [diff] [blame] | 5773 | * @cmd_type: specifies the type (NOP, Query...) |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 5774 | * @desc_op: descriptor operation |
| 5775 | * |
| 5776 | * Those type of requests uses UTP Transfer Request Descriptor - utrd. |
| 5777 | * Therefore, it "rides" the device management infrastructure: uses its tag and |
| 5778 | * tasks work queues. |
| 5779 | * |
| 5780 | * Since there is only one available tag for device management commands, |
| 5781 | * the caller is expected to hold the hba->dev_cmd.lock mutex. |
| 5782 | */ |
| 5783 | static int ufshcd_issue_devman_upiu_cmd(struct ufs_hba *hba, |
| 5784 | struct utp_upiu_req *req_upiu, |
| 5785 | struct utp_upiu_req *rsp_upiu, |
| 5786 | u8 *desc_buff, int *buff_len, |
Bart Van Assche | 7f674c3 | 2019-10-29 16:07:09 -0700 | [diff] [blame] | 5787 | enum dev_cmd_type cmd_type, |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 5788 | enum query_opcode desc_op) |
| 5789 | { |
| 5790 | struct ufshcd_lrb *lrbp; |
| 5791 | int err = 0; |
| 5792 | int tag; |
| 5793 | struct completion wait; |
| 5794 | unsigned long flags; |
| 5795 | u32 upiu_flags; |
| 5796 | |
| 5797 | down_read(&hba->clk_scaling_lock); |
| 5798 | |
| 5799 | wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag)); |
| 5800 | |
| 5801 | init_completion(&wait); |
| 5802 | lrbp = &hba->lrb[tag]; |
| 5803 | WARN_ON(lrbp->cmd); |
| 5804 | |
| 5805 | lrbp->cmd = NULL; |
| 5806 | lrbp->sense_bufflen = 0; |
| 5807 | lrbp->sense_buffer = NULL; |
| 5808 | lrbp->task_tag = tag; |
| 5809 | lrbp->lun = 0; |
| 5810 | lrbp->intr_cmd = true; |
| 5811 | hba->dev_cmd.type = cmd_type; |
| 5812 | |
| 5813 | switch (hba->ufs_version) { |
| 5814 | case UFSHCI_VERSION_10: |
| 5815 | case UFSHCI_VERSION_11: |
| 5816 | lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE; |
| 5817 | break; |
| 5818 | default: |
| 5819 | lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE; |
| 5820 | break; |
| 5821 | } |
| 5822 | |
| 5823 | /* update the task tag in the request upiu */ |
| 5824 | req_upiu->header.dword_0 |= cpu_to_be32(tag); |
| 5825 | |
| 5826 | ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE); |
| 5827 | |
| 5828 | /* just copy the upiu request as it is */ |
| 5829 | memcpy(lrbp->ucd_req_ptr, req_upiu, sizeof(*lrbp->ucd_req_ptr)); |
| 5830 | if (desc_buff && desc_op == UPIU_QUERY_OPCODE_WRITE_DESC) { |
| 5831 | /* The Data Segment Area is optional depending upon the query |
| 5832 | * function value. for WRITE DESCRIPTOR, the data segment |
| 5833 | * follows right after the tsf. |
| 5834 | */ |
| 5835 | memcpy(lrbp->ucd_req_ptr + 1, desc_buff, *buff_len); |
| 5836 | *buff_len = 0; |
| 5837 | } |
| 5838 | |
| 5839 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
| 5840 | |
| 5841 | hba->dev_cmd.complete = &wait; |
| 5842 | |
| 5843 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 5844 | wmb(); |
| 5845 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5846 | ufshcd_send_command(hba, tag); |
| 5847 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5848 | |
| 5849 | /* |
| 5850 | * ignore the returning value here - ufshcd_check_query_response is |
| 5851 | * bound to fail since dev_cmd.query and dev_cmd.type were left empty. |
| 5852 | * read the response directly ignoring all errors. |
| 5853 | */ |
| 5854 | ufshcd_wait_for_dev_cmd(hba, lrbp, QUERY_REQ_TIMEOUT); |
| 5855 | |
| 5856 | /* just copy the upiu response as it is */ |
| 5857 | memcpy(rsp_upiu, lrbp->ucd_rsp_ptr, sizeof(*rsp_upiu)); |
Avri Altman | 4bbbe24 | 2019-02-20 09:11:13 +0200 | [diff] [blame] | 5858 | if (desc_buff && desc_op == UPIU_QUERY_OPCODE_READ_DESC) { |
| 5859 | u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + sizeof(*rsp_upiu); |
| 5860 | u16 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) & |
| 5861 | MASK_QUERY_DATA_SEG_LEN; |
| 5862 | |
| 5863 | if (*buff_len >= resp_len) { |
| 5864 | memcpy(desc_buff, descp, resp_len); |
| 5865 | *buff_len = resp_len; |
| 5866 | } else { |
Bean Huo | 3d4881d | 2019-11-12 23:34:35 +0100 | [diff] [blame] | 5867 | dev_warn(hba->dev, |
| 5868 | "%s: rsp size %d is bigger than buffer size %d", |
| 5869 | __func__, resp_len, *buff_len); |
Avri Altman | 4bbbe24 | 2019-02-20 09:11:13 +0200 | [diff] [blame] | 5870 | *buff_len = 0; |
| 5871 | err = -EINVAL; |
| 5872 | } |
| 5873 | } |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 5874 | |
| 5875 | ufshcd_put_dev_cmd_tag(hba, tag); |
| 5876 | wake_up(&hba->dev_cmd.tag_wq); |
| 5877 | up_read(&hba->clk_scaling_lock); |
| 5878 | return err; |
| 5879 | } |
| 5880 | |
| 5881 | /** |
| 5882 | * ufshcd_exec_raw_upiu_cmd - API function for sending raw upiu commands |
| 5883 | * @hba: per-adapter instance |
| 5884 | * @req_upiu: upiu request |
| 5885 | * @rsp_upiu: upiu reply - only 8 DW as we do not support scsi commands |
| 5886 | * @msgcode: message code, one of UPIU Transaction Codes Initiator to Target |
| 5887 | * @desc_buff: pointer to descriptor buffer, NULL if NA |
| 5888 | * @buff_len: descriptor size, 0 if NA |
| 5889 | * @desc_op: descriptor operation |
| 5890 | * |
| 5891 | * Supports UTP Transfer requests (nop and query), and UTP Task |
| 5892 | * Management requests. |
| 5893 | * It is up to the caller to fill the upiu conent properly, as it will |
| 5894 | * be copied without any further input validations. |
| 5895 | */ |
| 5896 | int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba, |
| 5897 | struct utp_upiu_req *req_upiu, |
| 5898 | struct utp_upiu_req *rsp_upiu, |
| 5899 | int msgcode, |
| 5900 | u8 *desc_buff, int *buff_len, |
| 5901 | enum query_opcode desc_op) |
| 5902 | { |
| 5903 | int err; |
Bart Van Assche | 7f674c3 | 2019-10-29 16:07:09 -0700 | [diff] [blame] | 5904 | enum dev_cmd_type cmd_type = DEV_CMD_TYPE_QUERY; |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 5905 | struct utp_task_req_desc treq = { { 0 }, }; |
| 5906 | int ocs_value; |
| 5907 | u8 tm_f = be32_to_cpu(req_upiu->header.dword_1) >> 16 & MASK_TM_FUNC; |
| 5908 | |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 5909 | switch (msgcode) { |
| 5910 | case UPIU_TRANSACTION_NOP_OUT: |
| 5911 | cmd_type = DEV_CMD_TYPE_NOP; |
| 5912 | /* fall through */ |
| 5913 | case UPIU_TRANSACTION_QUERY_REQ: |
| 5914 | ufshcd_hold(hba, false); |
| 5915 | mutex_lock(&hba->dev_cmd.lock); |
| 5916 | err = ufshcd_issue_devman_upiu_cmd(hba, req_upiu, rsp_upiu, |
| 5917 | desc_buff, buff_len, |
| 5918 | cmd_type, desc_op); |
| 5919 | mutex_unlock(&hba->dev_cmd.lock); |
| 5920 | ufshcd_release(hba); |
| 5921 | |
| 5922 | break; |
| 5923 | case UPIU_TRANSACTION_TASK_REQ: |
| 5924 | treq.header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD); |
| 5925 | treq.header.dword_2 = cpu_to_le32(OCS_INVALID_COMMAND_STATUS); |
| 5926 | |
| 5927 | memcpy(&treq.req_header, req_upiu, sizeof(*req_upiu)); |
| 5928 | |
| 5929 | err = __ufshcd_issue_tm_cmd(hba, &treq, tm_f); |
| 5930 | if (err == -ETIMEDOUT) |
| 5931 | break; |
| 5932 | |
| 5933 | ocs_value = le32_to_cpu(treq.header.dword_2) & MASK_OCS; |
| 5934 | if (ocs_value != OCS_SUCCESS) { |
| 5935 | dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", __func__, |
| 5936 | ocs_value); |
| 5937 | break; |
| 5938 | } |
| 5939 | |
| 5940 | memcpy(rsp_upiu, &treq.rsp_header, sizeof(*rsp_upiu)); |
| 5941 | |
| 5942 | break; |
| 5943 | default: |
| 5944 | err = -EINVAL; |
| 5945 | |
| 5946 | break; |
| 5947 | } |
| 5948 | |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 5949 | return err; |
| 5950 | } |
| 5951 | |
| 5952 | /** |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5953 | * ufshcd_eh_device_reset_handler - device reset handler registered to |
| 5954 | * scsi layer. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5955 | * @cmd: SCSI command pointer |
| 5956 | * |
| 5957 | * Returns SUCCESS/FAILED |
| 5958 | */ |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5959 | static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5960 | { |
| 5961 | struct Scsi_Host *host; |
| 5962 | struct ufs_hba *hba; |
| 5963 | unsigned int tag; |
| 5964 | u32 pos; |
| 5965 | int err; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5966 | u8 resp = 0xF; |
| 5967 | struct ufshcd_lrb *lrbp; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5968 | unsigned long flags; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5969 | |
| 5970 | host = cmd->device->host; |
| 5971 | hba = shost_priv(host); |
| 5972 | tag = cmd->request->tag; |
| 5973 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5974 | lrbp = &hba->lrb[tag]; |
| 5975 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp); |
| 5976 | if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5977 | if (!err) |
| 5978 | err = resp; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5979 | goto out; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5980 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5981 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5982 | /* clear the commands that were pending for corresponding LUN */ |
| 5983 | for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) { |
| 5984 | if (hba->lrb[pos].lun == lrbp->lun) { |
| 5985 | err = ufshcd_clear_cmd(hba, pos); |
| 5986 | if (err) |
| 5987 | break; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5988 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5989 | } |
| 5990 | spin_lock_irqsave(host->host_lock, flags); |
| 5991 | ufshcd_transfer_req_compl(hba); |
| 5992 | spin_unlock_irqrestore(host->host_lock, flags); |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 5993 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5994 | out: |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 5995 | hba->req_abort_count = 0; |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 5996 | ufshcd_update_reg_hist(&hba->ufs_stats.dev_reset, (u32)err); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5997 | if (!err) { |
| 5998 | err = SUCCESS; |
| 5999 | } else { |
| 6000 | dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); |
| 6001 | err = FAILED; |
| 6002 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6003 | return err; |
| 6004 | } |
| 6005 | |
Gilad Broner | e0b299e | 2017-02-03 16:56:40 -0800 | [diff] [blame] | 6006 | static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap) |
| 6007 | { |
| 6008 | struct ufshcd_lrb *lrbp; |
| 6009 | int tag; |
| 6010 | |
| 6011 | for_each_set_bit(tag, &bitmap, hba->nutrs) { |
| 6012 | lrbp = &hba->lrb[tag]; |
| 6013 | lrbp->req_abort_skip = true; |
| 6014 | } |
| 6015 | } |
| 6016 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6017 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6018 | * ufshcd_abort - abort a specific command |
| 6019 | * @cmd: SCSI command pointer |
| 6020 | * |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6021 | * Abort the pending command in device by sending UFS_ABORT_TASK task management |
| 6022 | * command, and in host controller by clearing the door-bell register. There can |
| 6023 | * be race between controller sending the command to the device while abort is |
| 6024 | * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is |
| 6025 | * really issued and then try to abort it. |
| 6026 | * |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6027 | * Returns SUCCESS/FAILED |
| 6028 | */ |
| 6029 | static int ufshcd_abort(struct scsi_cmnd *cmd) |
| 6030 | { |
| 6031 | struct Scsi_Host *host; |
| 6032 | struct ufs_hba *hba; |
| 6033 | unsigned long flags; |
| 6034 | unsigned int tag; |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6035 | int err = 0; |
| 6036 | int poll_cnt; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6037 | u8 resp = 0xF; |
| 6038 | struct ufshcd_lrb *lrbp; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 6039 | u32 reg; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6040 | |
| 6041 | host = cmd->device->host; |
| 6042 | hba = shost_priv(host); |
| 6043 | tag = cmd->request->tag; |
Dolev Raviv | e7d3825 | 2016-12-22 18:40:07 -0800 | [diff] [blame] | 6044 | lrbp = &hba->lrb[tag]; |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 6045 | if (!ufshcd_valid_tag(hba, tag)) { |
| 6046 | dev_err(hba->dev, |
| 6047 | "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p", |
| 6048 | __func__, tag, cmd, cmd->request); |
| 6049 | BUG(); |
| 6050 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6051 | |
Dolev Raviv | e7d3825 | 2016-12-22 18:40:07 -0800 | [diff] [blame] | 6052 | /* |
| 6053 | * Task abort to the device W-LUN is illegal. When this command |
| 6054 | * will fail, due to spec violation, scsi err handling next step |
| 6055 | * will be to send LU reset which, again, is a spec violation. |
| 6056 | * To avoid these unnecessary/illegal step we skip to the last error |
| 6057 | * handling stage: reset and restore. |
| 6058 | */ |
| 6059 | if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN) |
| 6060 | return ufshcd_eh_host_reset_handler(cmd); |
| 6061 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6062 | ufshcd_hold(hba, false); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 6063 | reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 6064 | /* If command is already aborted/completed, return SUCCESS */ |
| 6065 | if (!(test_bit(tag, &hba->outstanding_reqs))) { |
| 6066 | dev_err(hba->dev, |
| 6067 | "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n", |
| 6068 | __func__, tag, hba->outstanding_reqs, reg); |
| 6069 | goto out; |
| 6070 | } |
| 6071 | |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 6072 | if (!(reg & (1 << tag))) { |
| 6073 | dev_err(hba->dev, |
| 6074 | "%s: cmd was completed, but without a notifying intr, tag = %d", |
| 6075 | __func__, tag); |
| 6076 | } |
| 6077 | |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 6078 | /* Print Transfer Request of aborted task */ |
| 6079 | dev_err(hba->dev, "%s: Device abort task at tag %d\n", __func__, tag); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 6080 | |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 6081 | /* |
| 6082 | * Print detailed info about aborted request. |
| 6083 | * As more than one request might get aborted at the same time, |
| 6084 | * print full information only for the first aborted request in order |
| 6085 | * to reduce repeated printouts. For other aborted requests only print |
| 6086 | * basic details. |
| 6087 | */ |
| 6088 | scsi_print_command(hba->lrb[tag].cmd); |
| 6089 | if (!hba->req_abort_count) { |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 6090 | ufshcd_update_reg_hist(&hba->ufs_stats.task_abort, 0); |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 6091 | ufshcd_print_host_regs(hba); |
Gilad Broner | 6ba6558 | 2017-02-03 16:57:28 -0800 | [diff] [blame] | 6092 | ufshcd_print_host_state(hba); |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 6093 | ufshcd_print_pwr_info(hba); |
| 6094 | ufshcd_print_trs(hba, 1 << tag, true); |
| 6095 | } else { |
| 6096 | ufshcd_print_trs(hba, 1 << tag, false); |
| 6097 | } |
| 6098 | hba->req_abort_count++; |
Gilad Broner | e0b299e | 2017-02-03 16:56:40 -0800 | [diff] [blame] | 6099 | |
| 6100 | /* Skip task abort in case previous aborts failed and report failure */ |
| 6101 | if (lrbp->req_abort_skip) { |
| 6102 | err = -EIO; |
| 6103 | goto out; |
| 6104 | } |
| 6105 | |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6106 | for (poll_cnt = 100; poll_cnt; poll_cnt--) { |
| 6107 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, |
| 6108 | UFS_QUERY_TASK, &resp); |
| 6109 | if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) { |
| 6110 | /* cmd pending in the device */ |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6111 | dev_err(hba->dev, "%s: cmd pending in the device. tag = %d\n", |
| 6112 | __func__, tag); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6113 | break; |
| 6114 | } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6115 | /* |
| 6116 | * cmd not pending in the device, check if it is |
| 6117 | * in transition. |
| 6118 | */ |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6119 | dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.\n", |
| 6120 | __func__, tag); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6121 | reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 6122 | if (reg & (1 << tag)) { |
| 6123 | /* sleep for max. 200us to stabilize */ |
| 6124 | usleep_range(100, 200); |
| 6125 | continue; |
| 6126 | } |
| 6127 | /* command completed already */ |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6128 | dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.\n", |
| 6129 | __func__, tag); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6130 | goto out; |
| 6131 | } else { |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6132 | dev_err(hba->dev, |
| 6133 | "%s: no response from device. tag = %d, err %d\n", |
| 6134 | __func__, tag, err); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6135 | if (!err) |
| 6136 | err = resp; /* service response error */ |
| 6137 | goto out; |
| 6138 | } |
| 6139 | } |
| 6140 | |
| 6141 | if (!poll_cnt) { |
| 6142 | err = -EBUSY; |
| 6143 | goto out; |
| 6144 | } |
| 6145 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6146 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, |
| 6147 | UFS_ABORT_TASK, &resp); |
| 6148 | if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6149 | if (!err) { |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6150 | err = resp; /* service response error */ |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6151 | dev_err(hba->dev, "%s: issued. tag = %d, err %d\n", |
| 6152 | __func__, tag, err); |
| 6153 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6154 | goto out; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6155 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6156 | |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6157 | err = ufshcd_clear_cmd(hba, tag); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6158 | if (err) { |
| 6159 | dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d\n", |
| 6160 | __func__, tag, err); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6161 | goto out; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6162 | } |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6163 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6164 | scsi_dma_unmap(cmd); |
| 6165 | |
| 6166 | spin_lock_irqsave(host->host_lock, flags); |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 6167 | ufshcd_outstanding_req_clear(hba, tag); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6168 | hba->lrb[tag].cmd = NULL; |
| 6169 | spin_unlock_irqrestore(host->host_lock, flags); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 6170 | |
| 6171 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 6172 | wake_up(&hba->dev_cmd.tag_wq); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6173 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6174 | out: |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6175 | if (!err) { |
| 6176 | err = SUCCESS; |
| 6177 | } else { |
| 6178 | dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); |
Gilad Broner | e0b299e | 2017-02-03 16:56:40 -0800 | [diff] [blame] | 6179 | ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6180 | err = FAILED; |
| 6181 | } |
| 6182 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6183 | /* |
| 6184 | * This ufshcd_release() corresponds to the original scsi cmd that got |
| 6185 | * aborted here (as we won't get any IRQ for it). |
| 6186 | */ |
| 6187 | ufshcd_release(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6188 | return err; |
| 6189 | } |
| 6190 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6191 | /** |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6192 | * ufshcd_host_reset_and_restore - reset and restore host controller |
| 6193 | * @hba: per-adapter instance |
| 6194 | * |
| 6195 | * Note that host controller reset may issue DME_RESET to |
| 6196 | * local and remote (device) Uni-Pro stack and the attributes |
| 6197 | * are reset to default state. |
| 6198 | * |
| 6199 | * Returns zero on success, non-zero on failure |
| 6200 | */ |
| 6201 | static int ufshcd_host_reset_and_restore(struct ufs_hba *hba) |
| 6202 | { |
| 6203 | int err; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6204 | unsigned long flags; |
| 6205 | |
| 6206 | /* Reset the host controller */ |
| 6207 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 6208 | ufshcd_hba_stop(hba, false); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6209 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6210 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 6211 | /* scale up clocks to max frequency before full reinitialization */ |
| 6212 | ufshcd_scale_clks(hba, true); |
| 6213 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6214 | err = ufshcd_hba_enable(hba); |
| 6215 | if (err) |
| 6216 | goto out; |
| 6217 | |
| 6218 | /* Establish the link again and restore the device */ |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6219 | err = ufshcd_probe_hba(hba); |
| 6220 | |
| 6221 | if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL)) |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6222 | err = -EIO; |
| 6223 | out: |
| 6224 | if (err) |
| 6225 | dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err); |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 6226 | ufshcd_update_reg_hist(&hba->ufs_stats.host_reset, (u32)err); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6227 | return err; |
| 6228 | } |
| 6229 | |
| 6230 | /** |
| 6231 | * ufshcd_reset_and_restore - reset and re-initialize host/device |
| 6232 | * @hba: per-adapter instance |
| 6233 | * |
| 6234 | * Reset and recover device, host and re-establish link. This |
| 6235 | * is helpful to recover the communication in fatal error conditions. |
| 6236 | * |
| 6237 | * Returns zero on success, non-zero on failure |
| 6238 | */ |
| 6239 | static int ufshcd_reset_and_restore(struct ufs_hba *hba) |
| 6240 | { |
| 6241 | int err = 0; |
| 6242 | unsigned long flags; |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6243 | int retries = MAX_HOST_RESET_RETRIES; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6244 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6245 | do { |
Bjorn Andersson | d8d9f79 | 2019-08-28 12:17:54 -0700 | [diff] [blame] | 6246 | /* Reset the attached device */ |
| 6247 | ufshcd_vops_device_reset(hba); |
| 6248 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6249 | err = ufshcd_host_reset_and_restore(hba); |
| 6250 | } while (err && --retries); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6251 | |
| 6252 | /* |
| 6253 | * After reset the door-bell might be cleared, complete |
| 6254 | * outstanding requests in s/w here. |
| 6255 | */ |
| 6256 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 6257 | ufshcd_transfer_req_compl(hba); |
| 6258 | ufshcd_tmc_handler(hba); |
| 6259 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6260 | |
| 6261 | return err; |
| 6262 | } |
| 6263 | |
| 6264 | /** |
| 6265 | * 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] | 6266 | * @cmd: SCSI command pointer |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6267 | * |
| 6268 | * Returns SUCCESS/FAILED |
| 6269 | */ |
| 6270 | static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd) |
| 6271 | { |
| 6272 | int err; |
| 6273 | unsigned long flags; |
| 6274 | struct ufs_hba *hba; |
| 6275 | |
| 6276 | hba = shost_priv(cmd->device->host); |
| 6277 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6278 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6279 | /* |
| 6280 | * Check if there is any race with fatal error handling. |
| 6281 | * If so, wait for it to complete. Even though fatal error |
| 6282 | * handling does reset and restore in some cases, don't assume |
| 6283 | * anything out of it. We are just avoiding race here. |
| 6284 | */ |
| 6285 | do { |
| 6286 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6287 | if (!(work_pending(&hba->eh_work) || |
Zang Leigang | 8dc0da7 | 2017-06-24 19:14:32 +0800 | [diff] [blame] | 6288 | hba->ufshcd_state == UFSHCD_STATE_RESET || |
| 6289 | hba->ufshcd_state == UFSHCD_STATE_EH_SCHEDULED)) |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6290 | break; |
| 6291 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6292 | dev_dbg(hba->dev, "%s: reset in progress\n", __func__); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6293 | flush_work(&hba->eh_work); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6294 | } while (1); |
| 6295 | |
| 6296 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 6297 | ufshcd_set_eh_in_progress(hba); |
| 6298 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6299 | |
| 6300 | err = ufshcd_reset_and_restore(hba); |
| 6301 | |
| 6302 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 6303 | if (!err) { |
| 6304 | err = SUCCESS; |
| 6305 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
| 6306 | } else { |
| 6307 | err = FAILED; |
| 6308 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 6309 | } |
| 6310 | ufshcd_clear_eh_in_progress(hba); |
| 6311 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6312 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6313 | ufshcd_release(hba); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6314 | return err; |
| 6315 | } |
| 6316 | |
| 6317 | /** |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6318 | * ufshcd_get_max_icc_level - calculate the ICC level |
| 6319 | * @sup_curr_uA: max. current supported by the regulator |
| 6320 | * @start_scan: row at the desc table to start scan from |
| 6321 | * @buff: power descriptor buffer |
| 6322 | * |
| 6323 | * Returns calculated max ICC level for specific regulator |
| 6324 | */ |
| 6325 | static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff) |
| 6326 | { |
| 6327 | int i; |
| 6328 | int curr_uA; |
| 6329 | u16 data; |
| 6330 | u16 unit; |
| 6331 | |
| 6332 | for (i = start_scan; i >= 0; i--) { |
Tomas Winkler | d79713f | 2017-01-05 10:45:11 +0200 | [diff] [blame] | 6333 | data = be16_to_cpup((__be16 *)&buff[2 * i]); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6334 | unit = (data & ATTR_ICC_LVL_UNIT_MASK) >> |
| 6335 | ATTR_ICC_LVL_UNIT_OFFSET; |
| 6336 | curr_uA = data & ATTR_ICC_LVL_VALUE_MASK; |
| 6337 | switch (unit) { |
| 6338 | case UFSHCD_NANO_AMP: |
| 6339 | curr_uA = curr_uA / 1000; |
| 6340 | break; |
| 6341 | case UFSHCD_MILI_AMP: |
| 6342 | curr_uA = curr_uA * 1000; |
| 6343 | break; |
| 6344 | case UFSHCD_AMP: |
| 6345 | curr_uA = curr_uA * 1000 * 1000; |
| 6346 | break; |
| 6347 | case UFSHCD_MICRO_AMP: |
| 6348 | default: |
| 6349 | break; |
| 6350 | } |
| 6351 | if (sup_curr_uA >= curr_uA) |
| 6352 | break; |
| 6353 | } |
| 6354 | if (i < 0) { |
| 6355 | i = 0; |
| 6356 | pr_err("%s: Couldn't find valid icc_level = %d", __func__, i); |
| 6357 | } |
| 6358 | |
| 6359 | return (u32)i; |
| 6360 | } |
| 6361 | |
| 6362 | /** |
| 6363 | * ufshcd_calc_icc_level - calculate the max ICC level |
| 6364 | * In case regulators are not initialized we'll return 0 |
| 6365 | * @hba: per-adapter instance |
| 6366 | * @desc_buf: power descriptor buffer to extract ICC levels from. |
| 6367 | * @len: length of desc_buff |
| 6368 | * |
| 6369 | * Returns calculated ICC level |
| 6370 | */ |
| 6371 | static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba, |
| 6372 | u8 *desc_buf, int len) |
| 6373 | { |
| 6374 | u32 icc_level = 0; |
| 6375 | |
| 6376 | if (!hba->vreg_info.vcc || !hba->vreg_info.vccq || |
| 6377 | !hba->vreg_info.vccq2) { |
| 6378 | dev_err(hba->dev, |
| 6379 | "%s: Regulator capability was not set, actvIccLevel=%d", |
| 6380 | __func__, icc_level); |
| 6381 | goto out; |
| 6382 | } |
| 6383 | |
Stanley Chu | 0487fff | 2019-03-28 17:16:25 +0800 | [diff] [blame] | 6384 | if (hba->vreg_info.vcc && hba->vreg_info.vcc->max_uA) |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6385 | icc_level = ufshcd_get_max_icc_level( |
| 6386 | hba->vreg_info.vcc->max_uA, |
| 6387 | POWER_DESC_MAX_ACTV_ICC_LVLS - 1, |
| 6388 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]); |
| 6389 | |
Stanley Chu | 0487fff | 2019-03-28 17:16:25 +0800 | [diff] [blame] | 6390 | if (hba->vreg_info.vccq && hba->vreg_info.vccq->max_uA) |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6391 | icc_level = ufshcd_get_max_icc_level( |
| 6392 | hba->vreg_info.vccq->max_uA, |
| 6393 | icc_level, |
| 6394 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]); |
| 6395 | |
Stanley Chu | 0487fff | 2019-03-28 17:16:25 +0800 | [diff] [blame] | 6396 | if (hba->vreg_info.vccq2 && hba->vreg_info.vccq2->max_uA) |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6397 | icc_level = ufshcd_get_max_icc_level( |
| 6398 | hba->vreg_info.vccq2->max_uA, |
| 6399 | icc_level, |
| 6400 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]); |
| 6401 | out: |
| 6402 | return icc_level; |
| 6403 | } |
| 6404 | |
| 6405 | static void ufshcd_init_icc_levels(struct ufs_hba *hba) |
| 6406 | { |
| 6407 | int ret; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 6408 | int buff_len = hba->desc_size.pwr_desc; |
Kees Cook | bbe21d7 | 2018-05-02 16:58:09 -0700 | [diff] [blame] | 6409 | u8 *desc_buf; |
| 6410 | |
| 6411 | desc_buf = kmalloc(buff_len, GFP_KERNEL); |
| 6412 | if (!desc_buf) |
| 6413 | return; |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6414 | |
| 6415 | ret = ufshcd_read_power_desc(hba, desc_buf, buff_len); |
| 6416 | if (ret) { |
| 6417 | dev_err(hba->dev, |
| 6418 | "%s: Failed reading power descriptor.len = %d ret = %d", |
| 6419 | __func__, buff_len, ret); |
Kees Cook | bbe21d7 | 2018-05-02 16:58:09 -0700 | [diff] [blame] | 6420 | goto out; |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6421 | } |
| 6422 | |
| 6423 | hba->init_prefetch_data.icc_level = |
| 6424 | ufshcd_find_max_sup_active_icc_level(hba, |
| 6425 | desc_buf, buff_len); |
| 6426 | dev_dbg(hba->dev, "%s: setting icc_level 0x%x", |
| 6427 | __func__, hba->init_prefetch_data.icc_level); |
| 6428 | |
Szymon Mielczarek | dbd34a6 | 2017-03-29 08:19:21 +0200 | [diff] [blame] | 6429 | ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
| 6430 | QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, |
| 6431 | &hba->init_prefetch_data.icc_level); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6432 | |
| 6433 | if (ret) |
| 6434 | dev_err(hba->dev, |
| 6435 | "%s: Failed configuring bActiveICCLevel = %d ret = %d", |
| 6436 | __func__, hba->init_prefetch_data.icc_level , ret); |
| 6437 | |
Kees Cook | bbe21d7 | 2018-05-02 16:58:09 -0700 | [diff] [blame] | 6438 | out: |
| 6439 | kfree(desc_buf); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6440 | } |
| 6441 | |
| 6442 | /** |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6443 | * ufshcd_scsi_add_wlus - Adds required W-LUs |
| 6444 | * @hba: per-adapter instance |
| 6445 | * |
| 6446 | * UFS device specification requires the UFS devices to support 4 well known |
| 6447 | * logical units: |
| 6448 | * "REPORT_LUNS" (address: 01h) |
| 6449 | * "UFS Device" (address: 50h) |
| 6450 | * "RPMB" (address: 44h) |
| 6451 | * "BOOT" (address: 30h) |
| 6452 | * UFS device's power management needs to be controlled by "POWER CONDITION" |
| 6453 | * field of SSU (START STOP UNIT) command. But this "power condition" field |
| 6454 | * will take effect only when its sent to "UFS device" well known logical unit |
| 6455 | * hence we require the scsi_device instance to represent this logical unit in |
| 6456 | * 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] | 6457 | * |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6458 | * We also require the scsi_device instance for "RPMB" (Replay Protected Memory |
| 6459 | * Block) LU so user space process can control this LU. User space may also |
| 6460 | * want to have access to BOOT LU. |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 6461 | * |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6462 | * This function adds scsi device instances for each of all well known LUs |
| 6463 | * (except "REPORT LUNS" LU). |
| 6464 | * |
| 6465 | * Returns zero on success (all required W-LUs are added successfully), |
| 6466 | * non-zero error value on failure (if failed to add any of the required W-LU). |
| 6467 | */ |
| 6468 | static int ufshcd_scsi_add_wlus(struct ufs_hba *hba) |
| 6469 | { |
| 6470 | int ret = 0; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6471 | struct scsi_device *sdev_rpmb; |
| 6472 | struct scsi_device *sdev_boot; |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6473 | |
| 6474 | hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0, |
| 6475 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL); |
| 6476 | if (IS_ERR(hba->sdev_ufs_device)) { |
| 6477 | ret = PTR_ERR(hba->sdev_ufs_device); |
| 6478 | hba->sdev_ufs_device = NULL; |
| 6479 | goto out; |
| 6480 | } |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6481 | scsi_device_put(hba->sdev_ufs_device); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6482 | |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6483 | sdev_rpmb = __scsi_add_device(hba->host, 0, 0, |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6484 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL); |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6485 | if (IS_ERR(sdev_rpmb)) { |
| 6486 | ret = PTR_ERR(sdev_rpmb); |
Huanlin Ke | 3d21fbd | 2017-09-22 18:31:47 +0800 | [diff] [blame] | 6487 | goto remove_sdev_ufs_device; |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6488 | } |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6489 | scsi_device_put(sdev_rpmb); |
Huanlin Ke | 3d21fbd | 2017-09-22 18:31:47 +0800 | [diff] [blame] | 6490 | |
| 6491 | sdev_boot = __scsi_add_device(hba->host, 0, 0, |
| 6492 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL); |
| 6493 | if (IS_ERR(sdev_boot)) |
| 6494 | dev_err(hba->dev, "%s: BOOT WLUN not found\n", __func__); |
| 6495 | else |
| 6496 | scsi_device_put(sdev_boot); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6497 | goto out; |
| 6498 | |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6499 | remove_sdev_ufs_device: |
| 6500 | scsi_remove_device(hba->sdev_ufs_device); |
| 6501 | out: |
| 6502 | return ret; |
| 6503 | } |
| 6504 | |
Tomas Winkler | 93fdd5a | 2017-01-05 10:45:12 +0200 | [diff] [blame] | 6505 | static int ufs_get_device_desc(struct ufs_hba *hba, |
| 6506 | struct ufs_dev_desc *dev_desc) |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6507 | { |
| 6508 | int err; |
Kees Cook | bbe21d7 | 2018-05-02 16:58:09 -0700 | [diff] [blame] | 6509 | size_t buff_len; |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6510 | u8 model_index; |
Kees Cook | bbe21d7 | 2018-05-02 16:58:09 -0700 | [diff] [blame] | 6511 | u8 *desc_buf; |
| 6512 | |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 6513 | if (!dev_desc) |
| 6514 | return -EINVAL; |
| 6515 | |
Kees Cook | bbe21d7 | 2018-05-02 16:58:09 -0700 | [diff] [blame] | 6516 | buff_len = max_t(size_t, hba->desc_size.dev_desc, |
| 6517 | QUERY_DESC_MAX_SIZE + 1); |
| 6518 | desc_buf = kmalloc(buff_len, GFP_KERNEL); |
| 6519 | if (!desc_buf) { |
| 6520 | err = -ENOMEM; |
| 6521 | goto out; |
| 6522 | } |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6523 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 6524 | err = ufshcd_read_device_desc(hba, desc_buf, hba->desc_size.dev_desc); |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6525 | if (err) { |
| 6526 | dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n", |
| 6527 | __func__, err); |
| 6528 | goto out; |
| 6529 | } |
| 6530 | |
| 6531 | /* |
| 6532 | * getting vendor (manufacturerID) and Bank Index in big endian |
| 6533 | * format |
| 6534 | */ |
Tomas Winkler | 93fdd5a | 2017-01-05 10:45:12 +0200 | [diff] [blame] | 6535 | dev_desc->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 | |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6536 | desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1]; |
| 6537 | |
| 6538 | model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME]; |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 6539 | err = ufshcd_read_string_desc(hba, model_index, |
| 6540 | &dev_desc->model, SD_ASCII_STD); |
| 6541 | if (err < 0) { |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6542 | dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n", |
| 6543 | __func__, err); |
| 6544 | goto out; |
| 6545 | } |
| 6546 | |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 6547 | /* |
| 6548 | * ufshcd_read_string_desc returns size of the string |
| 6549 | * reset the error value |
| 6550 | */ |
| 6551 | err = 0; |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6552 | |
| 6553 | out: |
Kees Cook | bbe21d7 | 2018-05-02 16:58:09 -0700 | [diff] [blame] | 6554 | kfree(desc_buf); |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6555 | return err; |
| 6556 | } |
| 6557 | |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 6558 | static void ufs_put_device_desc(struct ufs_dev_desc *dev_desc) |
| 6559 | { |
| 6560 | kfree(dev_desc->model); |
| 6561 | dev_desc->model = NULL; |
| 6562 | } |
| 6563 | |
Tomas Winkler | 93fdd5a | 2017-01-05 10:45:12 +0200 | [diff] [blame] | 6564 | static void ufs_fixup_device_setup(struct ufs_hba *hba, |
| 6565 | struct ufs_dev_desc *dev_desc) |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6566 | { |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6567 | struct ufs_dev_fix *f; |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6568 | |
| 6569 | for (f = ufs_fixups; f->quirk; f++) { |
Tomas Winkler | 93fdd5a | 2017-01-05 10:45:12 +0200 | [diff] [blame] | 6570 | if ((f->card.wmanufacturerid == dev_desc->wmanufacturerid || |
| 6571 | f->card.wmanufacturerid == UFS_ANY_VENDOR) && |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 6572 | ((dev_desc->model && |
| 6573 | STR_PRFX_EQUAL(f->card.model, dev_desc->model)) || |
| 6574 | !strcmp(f->card.model, UFS_ANY_MODEL))) |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6575 | hba->dev_quirks |= f->quirk; |
| 6576 | } |
| 6577 | } |
| 6578 | |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6579 | /** |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 6580 | * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro |
| 6581 | * @hba: per-adapter instance |
| 6582 | * |
| 6583 | * PA_TActivate parameter can be tuned manually if UniPro version is less than |
| 6584 | * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's |
| 6585 | * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce |
| 6586 | * the hibern8 exit latency. |
| 6587 | * |
| 6588 | * Returns zero on success, non-zero error value on failure. |
| 6589 | */ |
| 6590 | static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba) |
| 6591 | { |
| 6592 | int ret = 0; |
| 6593 | u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate; |
| 6594 | |
| 6595 | ret = ufshcd_dme_peer_get(hba, |
| 6596 | UIC_ARG_MIB_SEL( |
| 6597 | RX_MIN_ACTIVATETIME_CAPABILITY, |
| 6598 | UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), |
| 6599 | &peer_rx_min_activatetime); |
| 6600 | if (ret) |
| 6601 | goto out; |
| 6602 | |
| 6603 | /* make sure proper unit conversion is applied */ |
| 6604 | tuned_pa_tactivate = |
| 6605 | ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US) |
| 6606 | / PA_TACTIVATE_TIME_UNIT_US); |
| 6607 | ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 6608 | tuned_pa_tactivate); |
| 6609 | |
| 6610 | out: |
| 6611 | return ret; |
| 6612 | } |
| 6613 | |
| 6614 | /** |
| 6615 | * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro |
| 6616 | * @hba: per-adapter instance |
| 6617 | * |
| 6618 | * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than |
| 6619 | * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's |
| 6620 | * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY. |
| 6621 | * This optimal value can help reduce the hibern8 exit latency. |
| 6622 | * |
| 6623 | * Returns zero on success, non-zero error value on failure. |
| 6624 | */ |
| 6625 | static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba) |
| 6626 | { |
| 6627 | int ret = 0; |
| 6628 | u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0; |
| 6629 | u32 max_hibern8_time, tuned_pa_hibern8time; |
| 6630 | |
| 6631 | ret = ufshcd_dme_get(hba, |
| 6632 | UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY, |
| 6633 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), |
| 6634 | &local_tx_hibern8_time_cap); |
| 6635 | if (ret) |
| 6636 | goto out; |
| 6637 | |
| 6638 | ret = ufshcd_dme_peer_get(hba, |
| 6639 | UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY, |
| 6640 | UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), |
| 6641 | &peer_rx_hibern8_time_cap); |
| 6642 | if (ret) |
| 6643 | goto out; |
| 6644 | |
| 6645 | max_hibern8_time = max(local_tx_hibern8_time_cap, |
| 6646 | peer_rx_hibern8_time_cap); |
| 6647 | /* make sure proper unit conversion is applied */ |
| 6648 | tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US) |
| 6649 | / PA_HIBERN8_TIME_UNIT_US); |
| 6650 | ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME), |
| 6651 | tuned_pa_hibern8time); |
| 6652 | out: |
| 6653 | return ret; |
| 6654 | } |
| 6655 | |
subhashj@codeaurora.org | c6a6db4 | 2016-11-23 16:32:08 -0800 | [diff] [blame] | 6656 | /** |
| 6657 | * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is |
| 6658 | * less than device PA_TACTIVATE time. |
| 6659 | * @hba: per-adapter instance |
| 6660 | * |
| 6661 | * Some UFS devices require host PA_TACTIVATE to be lower than device |
| 6662 | * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk |
| 6663 | * for such devices. |
| 6664 | * |
| 6665 | * Returns zero on success, non-zero error value on failure. |
| 6666 | */ |
| 6667 | static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba) |
| 6668 | { |
| 6669 | int ret = 0; |
| 6670 | u32 granularity, peer_granularity; |
| 6671 | u32 pa_tactivate, peer_pa_tactivate; |
| 6672 | u32 pa_tactivate_us, peer_pa_tactivate_us; |
| 6673 | u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100}; |
| 6674 | |
| 6675 | ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY), |
| 6676 | &granularity); |
| 6677 | if (ret) |
| 6678 | goto out; |
| 6679 | |
| 6680 | ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY), |
| 6681 | &peer_granularity); |
| 6682 | if (ret) |
| 6683 | goto out; |
| 6684 | |
| 6685 | if ((granularity < PA_GRANULARITY_MIN_VAL) || |
| 6686 | (granularity > PA_GRANULARITY_MAX_VAL)) { |
| 6687 | dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d", |
| 6688 | __func__, granularity); |
| 6689 | return -EINVAL; |
| 6690 | } |
| 6691 | |
| 6692 | if ((peer_granularity < PA_GRANULARITY_MIN_VAL) || |
| 6693 | (peer_granularity > PA_GRANULARITY_MAX_VAL)) { |
| 6694 | dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d", |
| 6695 | __func__, peer_granularity); |
| 6696 | return -EINVAL; |
| 6697 | } |
| 6698 | |
| 6699 | ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate); |
| 6700 | if (ret) |
| 6701 | goto out; |
| 6702 | |
| 6703 | ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 6704 | &peer_pa_tactivate); |
| 6705 | if (ret) |
| 6706 | goto out; |
| 6707 | |
| 6708 | pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1]; |
| 6709 | peer_pa_tactivate_us = peer_pa_tactivate * |
| 6710 | gran_to_us_table[peer_granularity - 1]; |
| 6711 | |
| 6712 | if (pa_tactivate_us > peer_pa_tactivate_us) { |
| 6713 | u32 new_peer_pa_tactivate; |
| 6714 | |
| 6715 | new_peer_pa_tactivate = pa_tactivate_us / |
| 6716 | gran_to_us_table[peer_granularity - 1]; |
| 6717 | new_peer_pa_tactivate++; |
| 6718 | ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 6719 | new_peer_pa_tactivate); |
| 6720 | } |
| 6721 | |
| 6722 | out: |
| 6723 | return ret; |
| 6724 | } |
| 6725 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 6726 | static void ufshcd_tune_unipro_params(struct ufs_hba *hba) |
| 6727 | { |
| 6728 | if (ufshcd_is_unipro_pa_params_tuning_req(hba)) { |
| 6729 | ufshcd_tune_pa_tactivate(hba); |
| 6730 | ufshcd_tune_pa_hibern8time(hba); |
| 6731 | } |
| 6732 | |
| 6733 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE) |
| 6734 | /* set 1ms timeout for PA_TACTIVATE */ |
| 6735 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10); |
subhashj@codeaurora.org | c6a6db4 | 2016-11-23 16:32:08 -0800 | [diff] [blame] | 6736 | |
| 6737 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE) |
| 6738 | ufshcd_quirk_tune_host_pa_tactivate(hba); |
Subhash Jadavani | 56d4a18 | 2016-12-05 19:25:32 -0800 | [diff] [blame] | 6739 | |
| 6740 | ufshcd_vops_apply_dev_quirks(hba); |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 6741 | } |
| 6742 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6743 | static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba) |
| 6744 | { |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6745 | hba->ufs_stats.hibern8_exit_cnt = 0; |
| 6746 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 6747 | hba->req_abort_count = 0; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6748 | } |
| 6749 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 6750 | static void ufshcd_init_desc_sizes(struct ufs_hba *hba) |
| 6751 | { |
| 6752 | int err; |
| 6753 | |
| 6754 | err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_DEVICE, 0, |
| 6755 | &hba->desc_size.dev_desc); |
| 6756 | if (err) |
| 6757 | hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE; |
| 6758 | |
| 6759 | err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_POWER, 0, |
| 6760 | &hba->desc_size.pwr_desc); |
| 6761 | if (err) |
| 6762 | hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE; |
| 6763 | |
| 6764 | err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_INTERCONNECT, 0, |
| 6765 | &hba->desc_size.interc_desc); |
| 6766 | if (err) |
| 6767 | hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE; |
| 6768 | |
| 6769 | err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_CONFIGURATION, 0, |
| 6770 | &hba->desc_size.conf_desc); |
| 6771 | if (err) |
| 6772 | hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE; |
| 6773 | |
| 6774 | err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_UNIT, 0, |
| 6775 | &hba->desc_size.unit_desc); |
| 6776 | if (err) |
| 6777 | hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE; |
| 6778 | |
| 6779 | err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_GEOMETRY, 0, |
| 6780 | &hba->desc_size.geom_desc); |
| 6781 | if (err) |
| 6782 | hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE; |
Bean Huo | 059efd8 | 2019-10-29 14:22:45 +0000 | [diff] [blame] | 6783 | |
Stanislav Nijnikov | c648c2d | 2018-02-15 14:14:05 +0200 | [diff] [blame] | 6784 | err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_HEALTH, 0, |
| 6785 | &hba->desc_size.hlth_desc); |
| 6786 | if (err) |
| 6787 | hba->desc_size.hlth_desc = QUERY_DESC_HEALTH_DEF_SIZE; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 6788 | } |
| 6789 | |
Subhash Jadavani | 9e1e8a7 | 2018-10-16 14:29:41 +0530 | [diff] [blame] | 6790 | static struct ufs_ref_clk ufs_ref_clk_freqs[] = { |
| 6791 | {19200000, REF_CLK_FREQ_19_2_MHZ}, |
| 6792 | {26000000, REF_CLK_FREQ_26_MHZ}, |
| 6793 | {38400000, REF_CLK_FREQ_38_4_MHZ}, |
| 6794 | {52000000, REF_CLK_FREQ_52_MHZ}, |
| 6795 | {0, REF_CLK_FREQ_INVAL}, |
| 6796 | }; |
| 6797 | |
| 6798 | static enum ufs_ref_clk_freq |
| 6799 | ufs_get_bref_clk_from_hz(unsigned long freq) |
| 6800 | { |
| 6801 | int i; |
| 6802 | |
| 6803 | for (i = 0; ufs_ref_clk_freqs[i].freq_hz; i++) |
| 6804 | if (ufs_ref_clk_freqs[i].freq_hz == freq) |
| 6805 | return ufs_ref_clk_freqs[i].val; |
| 6806 | |
| 6807 | return REF_CLK_FREQ_INVAL; |
| 6808 | } |
| 6809 | |
| 6810 | void ufshcd_parse_dev_ref_clk_freq(struct ufs_hba *hba, struct clk *refclk) |
| 6811 | { |
| 6812 | unsigned long freq; |
| 6813 | |
| 6814 | freq = clk_get_rate(refclk); |
| 6815 | |
| 6816 | hba->dev_ref_clk_freq = |
| 6817 | ufs_get_bref_clk_from_hz(freq); |
| 6818 | |
| 6819 | if (hba->dev_ref_clk_freq == REF_CLK_FREQ_INVAL) |
| 6820 | dev_err(hba->dev, |
| 6821 | "invalid ref_clk setting = %ld\n", freq); |
| 6822 | } |
| 6823 | |
| 6824 | static int ufshcd_set_dev_ref_clk(struct ufs_hba *hba) |
| 6825 | { |
| 6826 | int err; |
| 6827 | u32 ref_clk; |
| 6828 | u32 freq = hba->dev_ref_clk_freq; |
| 6829 | |
| 6830 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
| 6831 | QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &ref_clk); |
| 6832 | |
| 6833 | if (err) { |
| 6834 | dev_err(hba->dev, "failed reading bRefClkFreq. err = %d\n", |
| 6835 | err); |
| 6836 | goto out; |
| 6837 | } |
| 6838 | |
| 6839 | if (ref_clk == freq) |
| 6840 | goto out; /* nothing to update */ |
| 6841 | |
| 6842 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
| 6843 | QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &freq); |
| 6844 | |
| 6845 | if (err) { |
| 6846 | dev_err(hba->dev, "bRefClkFreq setting to %lu Hz failed\n", |
| 6847 | ufs_ref_clk_freqs[freq].freq_hz); |
| 6848 | goto out; |
| 6849 | } |
| 6850 | |
| 6851 | dev_dbg(hba->dev, "bRefClkFreq setting to %lu Hz succeeded\n", |
| 6852 | ufs_ref_clk_freqs[freq].freq_hz); |
| 6853 | |
| 6854 | out: |
| 6855 | return err; |
| 6856 | } |
| 6857 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 6858 | /** |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6859 | * ufshcd_probe_hba - probe hba to detect device and initialize |
| 6860 | * @hba: per-adapter instance |
| 6861 | * |
| 6862 | * Execute link-startup and verify device initialization |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6863 | */ |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6864 | static int ufshcd_probe_hba(struct ufs_hba *hba) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6865 | { |
Tomas Winkler | 93fdd5a | 2017-01-05 10:45:12 +0200 | [diff] [blame] | 6866 | struct ufs_dev_desc card = {0}; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6867 | int ret; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 6868 | ktime_t start = ktime_get(); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6869 | |
| 6870 | ret = ufshcd_link_startup(hba); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 6871 | if (ret) |
| 6872 | goto out; |
| 6873 | |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 6874 | /* set the default level for urgent bkops */ |
| 6875 | hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT; |
| 6876 | hba->is_urgent_bkops_lvl_checked = false; |
| 6877 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6878 | /* Debug counters initialization */ |
| 6879 | ufshcd_clear_dbg_ufs_stats(hba); |
| 6880 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6881 | /* UniPro link is active now */ |
| 6882 | ufshcd_set_link_active(hba); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 6883 | |
Adrian Hunter | ad44837 | 2018-03-20 15:07:38 +0200 | [diff] [blame] | 6884 | /* Enable Auto-Hibernate if configured */ |
| 6885 | ufshcd_auto_hibern8_enable(hba); |
| 6886 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 6887 | ret = ufshcd_verify_dev_init(hba); |
| 6888 | if (ret) |
| 6889 | goto out; |
| 6890 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 6891 | ret = ufshcd_complete_dev_init(hba); |
| 6892 | if (ret) |
| 6893 | goto out; |
| 6894 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 6895 | /* Init check for device descriptor sizes */ |
| 6896 | ufshcd_init_desc_sizes(hba); |
| 6897 | |
Tomas Winkler | 93fdd5a | 2017-01-05 10:45:12 +0200 | [diff] [blame] | 6898 | ret = ufs_get_device_desc(hba, &card); |
| 6899 | if (ret) { |
| 6900 | dev_err(hba->dev, "%s: Failed getting device info. err = %d\n", |
| 6901 | __func__, ret); |
| 6902 | goto out; |
| 6903 | } |
| 6904 | |
| 6905 | ufs_fixup_device_setup(hba, &card); |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 6906 | ufs_put_device_desc(&card); |
| 6907 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 6908 | ufshcd_tune_unipro_params(hba); |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 6909 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6910 | /* UFS device is also active now */ |
| 6911 | ufshcd_set_ufs_dev_active(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 6912 | ufshcd_force_reset_auto_bkops(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6913 | hba->wlun_dev_clr_ua = true; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6914 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 6915 | if (ufshcd_get_max_pwr_mode(hba)) { |
| 6916 | dev_err(hba->dev, |
| 6917 | "%s: Failed getting max supported power mode\n", |
| 6918 | __func__); |
| 6919 | } else { |
Subhash Jadavani | 9e1e8a7 | 2018-10-16 14:29:41 +0530 | [diff] [blame] | 6920 | /* |
| 6921 | * Set the right value to bRefClkFreq before attempting to |
| 6922 | * switch to HS gears. |
| 6923 | */ |
| 6924 | if (hba->dev_ref_clk_freq != REF_CLK_FREQ_INVAL) |
| 6925 | ufshcd_set_dev_ref_clk(hba); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 6926 | ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info); |
Dov Levenglick | 8643ae6 | 2016-10-17 17:10:14 -0700 | [diff] [blame] | 6927 | if (ret) { |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 6928 | dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n", |
| 6929 | __func__, ret); |
Dov Levenglick | 8643ae6 | 2016-10-17 17:10:14 -0700 | [diff] [blame] | 6930 | goto out; |
| 6931 | } |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 6932 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6933 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 6934 | /* set the state as operational after switching to desired gear */ |
| 6935 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 6936 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6937 | /* |
| 6938 | * If we are in error handling context or in power management callbacks |
| 6939 | * context, no need to scan the host |
| 6940 | */ |
| 6941 | if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) { |
| 6942 | bool flag; |
| 6943 | |
| 6944 | /* clear any previous UFS device information */ |
| 6945 | memset(&hba->dev_info, 0, sizeof(hba->dev_info)); |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 6946 | if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG, |
| 6947 | QUERY_FLAG_IDN_PWR_ON_WPE, &flag)) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6948 | hba->dev_info.f_power_on_wp_en = flag; |
| 6949 | |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6950 | if (!hba->is_init_prefetch) |
| 6951 | ufshcd_init_icc_levels(hba); |
| 6952 | |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6953 | /* Add required well known logical units to scsi mid layer */ |
| 6954 | if (ufshcd_scsi_add_wlus(hba)) |
| 6955 | goto out; |
| 6956 | |
subhashj@codeaurora.org | 0701e49 | 2017-02-03 16:58:01 -0800 | [diff] [blame] | 6957 | /* Initialize devfreq after UFS device is detected */ |
| 6958 | if (ufshcd_is_clkscaling_supported(hba)) { |
| 6959 | memcpy(&hba->clk_scaling.saved_pwr_info.info, |
| 6960 | &hba->pwr_info, |
| 6961 | sizeof(struct ufs_pa_layer_attr)); |
| 6962 | hba->clk_scaling.saved_pwr_info.is_valid = true; |
| 6963 | if (!hba->devfreq) { |
Bjorn Andersson | deac444 | 2018-05-17 23:26:36 -0700 | [diff] [blame] | 6964 | ret = ufshcd_devfreq_init(hba); |
| 6965 | if (ret) |
subhashj@codeaurora.org | 0701e49 | 2017-02-03 16:58:01 -0800 | [diff] [blame] | 6966 | goto out; |
subhashj@codeaurora.org | 0701e49 | 2017-02-03 16:58:01 -0800 | [diff] [blame] | 6967 | } |
| 6968 | hba->clk_scaling.is_allowed = true; |
| 6969 | } |
| 6970 | |
Avri Altman | df032bf | 2018-10-07 17:30:35 +0300 | [diff] [blame] | 6971 | ufs_bsg_probe(hba); |
| 6972 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6973 | scsi_scan_host(hba->host); |
| 6974 | pm_runtime_put_sync(hba->dev); |
| 6975 | } |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6976 | |
| 6977 | if (!hba->is_init_prefetch) |
| 6978 | hba->is_init_prefetch = true; |
| 6979 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 6980 | out: |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6981 | /* |
| 6982 | * If we failed to initialize the device or the device is not |
| 6983 | * present, turn off the power/clocks etc. |
| 6984 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6985 | if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) { |
| 6986 | pm_runtime_put_sync(hba->dev); |
Vivek Gautam | eebcc19 | 2018-08-07 23:17:39 +0530 | [diff] [blame] | 6987 | ufshcd_exit_clk_scaling(hba); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6988 | ufshcd_hba_exit(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6989 | } |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6990 | |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 6991 | trace_ufshcd_init(dev_name(hba->dev), ret, |
| 6992 | ktime_to_us(ktime_sub(ktime_get(), start)), |
Subhash Jadavani | 73eba2b | 2017-01-10 16:48:25 -0800 | [diff] [blame] | 6993 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6994 | return ret; |
| 6995 | } |
| 6996 | |
| 6997 | /** |
| 6998 | * ufshcd_async_scan - asynchronous execution for probing hba |
| 6999 | * @data: data pointer to pass to this function |
| 7000 | * @cookie: cookie data |
| 7001 | */ |
| 7002 | static void ufshcd_async_scan(void *data, async_cookie_t cookie) |
| 7003 | { |
| 7004 | struct ufs_hba *hba = (struct ufs_hba *)data; |
| 7005 | |
| 7006 | ufshcd_probe_hba(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7007 | } |
| 7008 | |
Yaniv Gardi | f550c65 | 2016-03-10 17:37:07 +0200 | [diff] [blame] | 7009 | static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd) |
| 7010 | { |
| 7011 | unsigned long flags; |
| 7012 | struct Scsi_Host *host; |
| 7013 | struct ufs_hba *hba; |
| 7014 | int index; |
| 7015 | bool found = false; |
| 7016 | |
| 7017 | if (!scmd || !scmd->device || !scmd->device->host) |
Christoph Hellwig | 6600593 | 2018-05-29 15:52:29 +0200 | [diff] [blame] | 7018 | return BLK_EH_DONE; |
Yaniv Gardi | f550c65 | 2016-03-10 17:37:07 +0200 | [diff] [blame] | 7019 | |
| 7020 | host = scmd->device->host; |
| 7021 | hba = shost_priv(host); |
| 7022 | if (!hba) |
Christoph Hellwig | 6600593 | 2018-05-29 15:52:29 +0200 | [diff] [blame] | 7023 | return BLK_EH_DONE; |
Yaniv Gardi | f550c65 | 2016-03-10 17:37:07 +0200 | [diff] [blame] | 7024 | |
| 7025 | spin_lock_irqsave(host->host_lock, flags); |
| 7026 | |
| 7027 | for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) { |
| 7028 | if (hba->lrb[index].cmd == scmd) { |
| 7029 | found = true; |
| 7030 | break; |
| 7031 | } |
| 7032 | } |
| 7033 | |
| 7034 | spin_unlock_irqrestore(host->host_lock, flags); |
| 7035 | |
| 7036 | /* |
| 7037 | * Bypass SCSI error handling and reset the block layer timer if this |
| 7038 | * SCSI command was not actually dispatched to UFS driver, otherwise |
| 7039 | * let SCSI layer handle the error as usual. |
| 7040 | */ |
Christoph Hellwig | 6600593 | 2018-05-29 15:52:29 +0200 | [diff] [blame] | 7041 | return found ? BLK_EH_DONE : BLK_EH_RESET_TIMER; |
Yaniv Gardi | f550c65 | 2016-03-10 17:37:07 +0200 | [diff] [blame] | 7042 | } |
| 7043 | |
Stanislav Nijnikov | d829fc8 | 2018-02-15 14:14:09 +0200 | [diff] [blame] | 7044 | static const struct attribute_group *ufshcd_driver_groups[] = { |
| 7045 | &ufs_sysfs_unit_descriptor_group, |
Stanislav Nijnikov | ec92b59 | 2018-02-15 14:14:11 +0200 | [diff] [blame] | 7046 | &ufs_sysfs_lun_attributes_group, |
Stanislav Nijnikov | d829fc8 | 2018-02-15 14:14:09 +0200 | [diff] [blame] | 7047 | NULL, |
| 7048 | }; |
| 7049 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7050 | static struct scsi_host_template ufshcd_driver_template = { |
| 7051 | .module = THIS_MODULE, |
| 7052 | .name = UFSHCD, |
| 7053 | .proc_name = UFSHCD, |
| 7054 | .queuecommand = ufshcd_queuecommand, |
| 7055 | .slave_alloc = ufshcd_slave_alloc, |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 7056 | .slave_configure = ufshcd_slave_configure, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7057 | .slave_destroy = ufshcd_slave_destroy, |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 7058 | .change_queue_depth = ufshcd_change_queue_depth, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7059 | .eh_abort_handler = ufshcd_abort, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 7060 | .eh_device_reset_handler = ufshcd_eh_device_reset_handler, |
| 7061 | .eh_host_reset_handler = ufshcd_eh_host_reset_handler, |
Yaniv Gardi | f550c65 | 2016-03-10 17:37:07 +0200 | [diff] [blame] | 7062 | .eh_timed_out = ufshcd_eh_timed_out, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7063 | .this_id = -1, |
| 7064 | .sg_tablesize = SG_ALL, |
| 7065 | .cmd_per_lun = UFSHCD_CMD_PER_LUN, |
| 7066 | .can_queue = UFSHCD_CAN_QUEUE, |
Christoph Hellwig | 552a990 | 2019-06-17 14:19:55 +0200 | [diff] [blame] | 7067 | .max_segment_size = PRDT_DATA_BYTE_COUNT_MAX, |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7068 | .max_host_blocked = 1, |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 7069 | .track_queue_depth = 1, |
Stanislav Nijnikov | d829fc8 | 2018-02-15 14:14:09 +0200 | [diff] [blame] | 7070 | .sdev_groups = ufshcd_driver_groups, |
Christoph Hellwig | 4af14d1 | 2018-12-13 16:17:09 +0100 | [diff] [blame] | 7071 | .dma_boundary = PAGE_SIZE - 1, |
Stanley Chu | 49615ba | 2019-09-16 23:56:50 +0800 | [diff] [blame] | 7072 | .rpm_autosuspend_delay = RPM_AUTOSUSPEND_DELAY_MS, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7073 | }; |
| 7074 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7075 | static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg, |
| 7076 | int ua) |
| 7077 | { |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 7078 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7079 | |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 7080 | if (!vreg) |
| 7081 | return 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7082 | |
Stanley Chu | 0487fff | 2019-03-28 17:16:25 +0800 | [diff] [blame] | 7083 | /* |
| 7084 | * "set_load" operation shall be required on those regulators |
| 7085 | * which specifically configured current limitation. Otherwise |
| 7086 | * zero max_uA may cause unexpected behavior when regulator is |
| 7087 | * enabled or set as high power mode. |
| 7088 | */ |
| 7089 | if (!vreg->max_uA) |
| 7090 | return 0; |
| 7091 | |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 7092 | ret = regulator_set_load(vreg->reg, ua); |
| 7093 | if (ret < 0) { |
| 7094 | dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n", |
| 7095 | __func__, vreg->name, ua, ret); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7096 | } |
| 7097 | |
| 7098 | return ret; |
| 7099 | } |
| 7100 | |
| 7101 | static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba, |
| 7102 | struct ufs_vreg *vreg) |
| 7103 | { |
Marc Gonzalez | 7306798 | 2019-02-27 11:41:45 +0100 | [diff] [blame] | 7104 | 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] | 7105 | } |
| 7106 | |
| 7107 | static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba, |
| 7108 | struct ufs_vreg *vreg) |
| 7109 | { |
Adrian Hunter | 7c7cfdc | 2019-08-14 15:59:50 +0300 | [diff] [blame] | 7110 | if (!vreg) |
| 7111 | return 0; |
| 7112 | |
Marc Gonzalez | 7306798 | 2019-02-27 11:41:45 +0100 | [diff] [blame] | 7113 | return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7114 | } |
| 7115 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7116 | static int ufshcd_config_vreg(struct device *dev, |
| 7117 | struct ufs_vreg *vreg, bool on) |
| 7118 | { |
| 7119 | int ret = 0; |
Gustavo A. R. Silva | 7275359 | 2017-11-20 08:12:29 -0600 | [diff] [blame] | 7120 | struct regulator *reg; |
| 7121 | const char *name; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7122 | int min_uV, uA_load; |
| 7123 | |
| 7124 | BUG_ON(!vreg); |
| 7125 | |
Gustavo A. R. Silva | 7275359 | 2017-11-20 08:12:29 -0600 | [diff] [blame] | 7126 | reg = vreg->reg; |
| 7127 | name = vreg->name; |
| 7128 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7129 | if (regulator_count_voltages(reg) > 0) { |
Stanley Chu | 3b141e8 | 2019-03-28 17:16:24 +0800 | [diff] [blame] | 7130 | if (vreg->min_uV && vreg->max_uV) { |
| 7131 | min_uV = on ? vreg->min_uV : 0; |
| 7132 | ret = regulator_set_voltage(reg, min_uV, vreg->max_uV); |
| 7133 | if (ret) { |
| 7134 | dev_err(dev, |
| 7135 | "%s: %s set voltage failed, err=%d\n", |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7136 | __func__, name, ret); |
Stanley Chu | 3b141e8 | 2019-03-28 17:16:24 +0800 | [diff] [blame] | 7137 | goto out; |
| 7138 | } |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7139 | } |
| 7140 | |
| 7141 | uA_load = on ? vreg->max_uA : 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7142 | ret = ufshcd_config_vreg_load(dev, vreg, uA_load); |
| 7143 | if (ret) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7144 | goto out; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7145 | } |
| 7146 | out: |
| 7147 | return ret; |
| 7148 | } |
| 7149 | |
| 7150 | static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 7151 | { |
| 7152 | int ret = 0; |
| 7153 | |
Marc Gonzalez | 7306798 | 2019-02-27 11:41:45 +0100 | [diff] [blame] | 7154 | if (!vreg || vreg->enabled) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7155 | goto out; |
| 7156 | |
| 7157 | ret = ufshcd_config_vreg(dev, vreg, true); |
| 7158 | if (!ret) |
| 7159 | ret = regulator_enable(vreg->reg); |
| 7160 | |
| 7161 | if (!ret) |
| 7162 | vreg->enabled = true; |
| 7163 | else |
| 7164 | dev_err(dev, "%s: %s enable failed, err=%d\n", |
| 7165 | __func__, vreg->name, ret); |
| 7166 | out: |
| 7167 | return ret; |
| 7168 | } |
| 7169 | |
| 7170 | static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 7171 | { |
| 7172 | int ret = 0; |
| 7173 | |
Marc Gonzalez | 7306798 | 2019-02-27 11:41:45 +0100 | [diff] [blame] | 7174 | if (!vreg || !vreg->enabled) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7175 | goto out; |
| 7176 | |
| 7177 | ret = regulator_disable(vreg->reg); |
| 7178 | |
| 7179 | if (!ret) { |
| 7180 | /* ignore errors on applying disable config */ |
| 7181 | ufshcd_config_vreg(dev, vreg, false); |
| 7182 | vreg->enabled = false; |
| 7183 | } else { |
| 7184 | dev_err(dev, "%s: %s disable failed, err=%d\n", |
| 7185 | __func__, vreg->name, ret); |
| 7186 | } |
| 7187 | out: |
| 7188 | return ret; |
| 7189 | } |
| 7190 | |
| 7191 | static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on) |
| 7192 | { |
| 7193 | int ret = 0; |
| 7194 | struct device *dev = hba->dev; |
| 7195 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 7196 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7197 | ret = ufshcd_toggle_vreg(dev, info->vcc, on); |
| 7198 | if (ret) |
| 7199 | goto out; |
| 7200 | |
| 7201 | ret = ufshcd_toggle_vreg(dev, info->vccq, on); |
| 7202 | if (ret) |
| 7203 | goto out; |
| 7204 | |
| 7205 | ret = ufshcd_toggle_vreg(dev, info->vccq2, on); |
| 7206 | if (ret) |
| 7207 | goto out; |
| 7208 | |
| 7209 | out: |
| 7210 | if (ret) { |
| 7211 | ufshcd_toggle_vreg(dev, info->vccq2, false); |
| 7212 | ufshcd_toggle_vreg(dev, info->vccq, false); |
| 7213 | ufshcd_toggle_vreg(dev, info->vcc, false); |
| 7214 | } |
| 7215 | return ret; |
| 7216 | } |
| 7217 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7218 | static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on) |
| 7219 | { |
| 7220 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 7221 | |
Zeng Guangyue | 60b7b82 | 2019-03-30 17:03:13 +0800 | [diff] [blame] | 7222 | return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on); |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7223 | } |
| 7224 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7225 | static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 7226 | { |
| 7227 | int ret = 0; |
| 7228 | |
| 7229 | if (!vreg) |
| 7230 | goto out; |
| 7231 | |
| 7232 | vreg->reg = devm_regulator_get(dev, vreg->name); |
| 7233 | if (IS_ERR(vreg->reg)) { |
| 7234 | ret = PTR_ERR(vreg->reg); |
| 7235 | dev_err(dev, "%s: %s get failed, err=%d\n", |
| 7236 | __func__, vreg->name, ret); |
| 7237 | } |
| 7238 | out: |
| 7239 | return ret; |
| 7240 | } |
| 7241 | |
| 7242 | static int ufshcd_init_vreg(struct ufs_hba *hba) |
| 7243 | { |
| 7244 | int ret = 0; |
| 7245 | struct device *dev = hba->dev; |
| 7246 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 7247 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7248 | ret = ufshcd_get_vreg(dev, info->vcc); |
| 7249 | if (ret) |
| 7250 | goto out; |
| 7251 | |
| 7252 | ret = ufshcd_get_vreg(dev, info->vccq); |
| 7253 | if (ret) |
| 7254 | goto out; |
| 7255 | |
| 7256 | ret = ufshcd_get_vreg(dev, info->vccq2); |
| 7257 | out: |
| 7258 | return ret; |
| 7259 | } |
| 7260 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7261 | static int ufshcd_init_hba_vreg(struct ufs_hba *hba) |
| 7262 | { |
| 7263 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 7264 | |
| 7265 | if (info) |
| 7266 | return ufshcd_get_vreg(hba->dev, info->vdd_hba); |
| 7267 | |
| 7268 | return 0; |
| 7269 | } |
| 7270 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7271 | static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on, |
| 7272 | bool skip_ref_clk) |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7273 | { |
| 7274 | int ret = 0; |
| 7275 | struct ufs_clk_info *clki; |
| 7276 | struct list_head *head = &hba->clk_list_head; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7277 | unsigned long flags; |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 7278 | ktime_t start = ktime_get(); |
| 7279 | bool clk_state_changed = false; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7280 | |
Szymon Mielczarek | 566ec9a | 2017-06-05 11:36:54 +0300 | [diff] [blame] | 7281 | if (list_empty(head)) |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7282 | goto out; |
| 7283 | |
Subhash Jadavani | b334456 | 2018-05-03 16:37:17 +0530 | [diff] [blame] | 7284 | /* |
| 7285 | * vendor specific setup_clocks ops may depend on clocks managed by |
| 7286 | * this standard driver hence call the vendor specific setup_clocks |
| 7287 | * before disabling the clocks managed here. |
| 7288 | */ |
| 7289 | if (!on) { |
| 7290 | ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE); |
| 7291 | if (ret) |
| 7292 | return ret; |
| 7293 | } |
Subhash Jadavani | 1e879e8 | 2016-10-06 21:48:22 -0700 | [diff] [blame] | 7294 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7295 | list_for_each_entry(clki, head, list) { |
| 7296 | if (!IS_ERR_OR_NULL(clki->clk)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7297 | if (skip_ref_clk && !strcmp(clki->name, "ref_clk")) |
| 7298 | continue; |
| 7299 | |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 7300 | clk_state_changed = on ^ clki->enabled; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7301 | if (on && !clki->enabled) { |
| 7302 | ret = clk_prepare_enable(clki->clk); |
| 7303 | if (ret) { |
| 7304 | dev_err(hba->dev, "%s: %s prepare enable failed, %d\n", |
| 7305 | __func__, clki->name, ret); |
| 7306 | goto out; |
| 7307 | } |
| 7308 | } else if (!on && clki->enabled) { |
| 7309 | clk_disable_unprepare(clki->clk); |
| 7310 | } |
| 7311 | clki->enabled = on; |
| 7312 | dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__, |
| 7313 | clki->name, on ? "en" : "dis"); |
| 7314 | } |
| 7315 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7316 | |
Subhash Jadavani | b334456 | 2018-05-03 16:37:17 +0530 | [diff] [blame] | 7317 | /* |
| 7318 | * vendor specific setup_clocks ops may depend on clocks managed by |
| 7319 | * this standard driver hence call the vendor specific setup_clocks |
| 7320 | * after enabling the clocks managed here. |
| 7321 | */ |
| 7322 | if (on) { |
| 7323 | ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE); |
| 7324 | if (ret) |
| 7325 | return ret; |
| 7326 | } |
Subhash Jadavani | 1e879e8 | 2016-10-06 21:48:22 -0700 | [diff] [blame] | 7327 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7328 | out: |
| 7329 | if (ret) { |
| 7330 | list_for_each_entry(clki, head, list) { |
| 7331 | if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled) |
| 7332 | clk_disable_unprepare(clki->clk); |
| 7333 | } |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7334 | } else if (!ret && on) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7335 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 7336 | hba->clk_gating.state = CLKS_ON; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7337 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 7338 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7339 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7340 | } |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7341 | |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 7342 | if (clk_state_changed) |
| 7343 | trace_ufshcd_profile_clk_gating(dev_name(hba->dev), |
| 7344 | (on ? "on" : "off"), |
| 7345 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7346 | return ret; |
| 7347 | } |
| 7348 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7349 | static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on) |
| 7350 | { |
| 7351 | return __ufshcd_setup_clocks(hba, on, false); |
| 7352 | } |
| 7353 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7354 | static int ufshcd_init_clocks(struct ufs_hba *hba) |
| 7355 | { |
| 7356 | int ret = 0; |
| 7357 | struct ufs_clk_info *clki; |
| 7358 | struct device *dev = hba->dev; |
| 7359 | struct list_head *head = &hba->clk_list_head; |
| 7360 | |
Szymon Mielczarek | 566ec9a | 2017-06-05 11:36:54 +0300 | [diff] [blame] | 7361 | if (list_empty(head)) |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7362 | goto out; |
| 7363 | |
| 7364 | list_for_each_entry(clki, head, list) { |
| 7365 | if (!clki->name) |
| 7366 | continue; |
| 7367 | |
| 7368 | clki->clk = devm_clk_get(dev, clki->name); |
| 7369 | if (IS_ERR(clki->clk)) { |
| 7370 | ret = PTR_ERR(clki->clk); |
| 7371 | dev_err(dev, "%s: %s clk get failed, %d\n", |
| 7372 | __func__, clki->name, ret); |
| 7373 | goto out; |
| 7374 | } |
| 7375 | |
Subhash Jadavani | 9e1e8a7 | 2018-10-16 14:29:41 +0530 | [diff] [blame] | 7376 | /* |
| 7377 | * Parse device ref clk freq as per device tree "ref_clk". |
| 7378 | * Default dev_ref_clk_freq is set to REF_CLK_FREQ_INVAL |
| 7379 | * in ufshcd_alloc_host(). |
| 7380 | */ |
| 7381 | if (!strcmp(clki->name, "ref_clk")) |
| 7382 | ufshcd_parse_dev_ref_clk_freq(hba, clki->clk); |
| 7383 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7384 | if (clki->max_freq) { |
| 7385 | ret = clk_set_rate(clki->clk, clki->max_freq); |
| 7386 | if (ret) { |
| 7387 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 7388 | __func__, clki->name, |
| 7389 | clki->max_freq, ret); |
| 7390 | goto out; |
| 7391 | } |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 7392 | clki->curr_freq = clki->max_freq; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7393 | } |
| 7394 | dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__, |
| 7395 | clki->name, clk_get_rate(clki->clk)); |
| 7396 | } |
| 7397 | out: |
| 7398 | return ret; |
| 7399 | } |
| 7400 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7401 | static int ufshcd_variant_hba_init(struct ufs_hba *hba) |
| 7402 | { |
| 7403 | int err = 0; |
| 7404 | |
| 7405 | if (!hba->vops) |
| 7406 | goto out; |
| 7407 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7408 | err = ufshcd_vops_init(hba); |
| 7409 | if (err) |
| 7410 | goto out; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7411 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7412 | err = ufshcd_vops_setup_regulators(hba, true); |
| 7413 | if (err) |
| 7414 | goto out_exit; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7415 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7416 | goto out; |
| 7417 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7418 | out_exit: |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7419 | ufshcd_vops_exit(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7420 | out: |
| 7421 | if (err) |
| 7422 | dev_err(hba->dev, "%s: variant %s init failed err %d\n", |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7423 | __func__, ufshcd_get_var_name(hba), err); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7424 | return err; |
| 7425 | } |
| 7426 | |
| 7427 | static void ufshcd_variant_hba_exit(struct ufs_hba *hba) |
| 7428 | { |
| 7429 | if (!hba->vops) |
| 7430 | return; |
| 7431 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7432 | ufshcd_vops_setup_regulators(hba, false); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7433 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7434 | ufshcd_vops_exit(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7435 | } |
| 7436 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7437 | static int ufshcd_hba_init(struct ufs_hba *hba) |
| 7438 | { |
| 7439 | int err; |
| 7440 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7441 | /* |
| 7442 | * Handle host controller power separately from the UFS device power |
| 7443 | * rails as it will help controlling the UFS host controller power |
| 7444 | * collapse easily which is different than UFS device power collapse. |
| 7445 | * Also, enable the host controller power before we go ahead with rest |
| 7446 | * of the initialization here. |
| 7447 | */ |
| 7448 | err = ufshcd_init_hba_vreg(hba); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7449 | if (err) |
| 7450 | goto out; |
| 7451 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7452 | err = ufshcd_setup_hba_vreg(hba, true); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7453 | if (err) |
| 7454 | goto out; |
| 7455 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7456 | err = ufshcd_init_clocks(hba); |
| 7457 | if (err) |
| 7458 | goto out_disable_hba_vreg; |
| 7459 | |
| 7460 | err = ufshcd_setup_clocks(hba, true); |
| 7461 | if (err) |
| 7462 | goto out_disable_hba_vreg; |
| 7463 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7464 | err = ufshcd_init_vreg(hba); |
| 7465 | if (err) |
| 7466 | goto out_disable_clks; |
| 7467 | |
| 7468 | err = ufshcd_setup_vreg(hba, true); |
| 7469 | if (err) |
| 7470 | goto out_disable_clks; |
| 7471 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7472 | err = ufshcd_variant_hba_init(hba); |
| 7473 | if (err) |
| 7474 | goto out_disable_vreg; |
| 7475 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7476 | hba->is_powered = true; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7477 | goto out; |
| 7478 | |
| 7479 | out_disable_vreg: |
| 7480 | ufshcd_setup_vreg(hba, false); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7481 | out_disable_clks: |
| 7482 | ufshcd_setup_clocks(hba, false); |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7483 | out_disable_hba_vreg: |
| 7484 | ufshcd_setup_hba_vreg(hba, false); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7485 | out: |
| 7486 | return err; |
| 7487 | } |
| 7488 | |
| 7489 | static void ufshcd_hba_exit(struct ufs_hba *hba) |
| 7490 | { |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7491 | if (hba->is_powered) { |
| 7492 | ufshcd_variant_hba_exit(hba); |
| 7493 | ufshcd_setup_vreg(hba, false); |
Gilad Broner | a508253 | 2016-10-17 17:10:00 -0700 | [diff] [blame] | 7494 | ufshcd_suspend_clkscaling(hba); |
Vivek Gautam | eebcc19 | 2018-08-07 23:17:39 +0530 | [diff] [blame] | 7495 | if (ufshcd_is_clkscaling_supported(hba)) |
subhashj@codeaurora.org | 0701e49 | 2017-02-03 16:58:01 -0800 | [diff] [blame] | 7496 | if (hba->devfreq) |
| 7497 | ufshcd_suspend_clkscaling(hba); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7498 | ufshcd_setup_clocks(hba, false); |
| 7499 | ufshcd_setup_hba_vreg(hba, false); |
| 7500 | hba->is_powered = false; |
| 7501 | } |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7502 | } |
| 7503 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7504 | static int |
| 7505 | ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7506 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7507 | unsigned char cmd[6] = {REQUEST_SENSE, |
| 7508 | 0, |
| 7509 | 0, |
| 7510 | 0, |
Avri Altman | 09a5a24 | 2018-11-22 20:04:56 +0200 | [diff] [blame] | 7511 | UFS_SENSE_SIZE, |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7512 | 0}; |
| 7513 | char *buffer; |
| 7514 | int ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7515 | |
Avri Altman | 09a5a24 | 2018-11-22 20:04:56 +0200 | [diff] [blame] | 7516 | buffer = kzalloc(UFS_SENSE_SIZE, GFP_KERNEL); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7517 | if (!buffer) { |
| 7518 | ret = -ENOMEM; |
| 7519 | goto out; |
| 7520 | } |
| 7521 | |
Christoph Hellwig | fcbfffe | 2017-02-23 16:02:37 +0100 | [diff] [blame] | 7522 | ret = scsi_execute(sdp, cmd, DMA_FROM_DEVICE, buffer, |
Avri Altman | 09a5a24 | 2018-11-22 20:04:56 +0200 | [diff] [blame] | 7523 | UFS_SENSE_SIZE, NULL, NULL, |
Christoph Hellwig | fcbfffe | 2017-02-23 16:02:37 +0100 | [diff] [blame] | 7524 | msecs_to_jiffies(1000), 3, 0, RQF_PM, NULL); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7525 | if (ret) |
| 7526 | pr_err("%s: failed with err %d\n", __func__, ret); |
| 7527 | |
| 7528 | kfree(buffer); |
| 7529 | out: |
| 7530 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7531 | } |
| 7532 | |
| 7533 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7534 | * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device |
| 7535 | * power mode |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7536 | * @hba: per adapter instance |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7537 | * @pwr_mode: device power mode to set |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7538 | * |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7539 | * Returns 0 if requested power mode is set successfully |
| 7540 | * Returns non-zero if failed to set the requested power mode |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7541 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7542 | static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba, |
| 7543 | enum ufs_dev_pwr_mode pwr_mode) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7544 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7545 | unsigned char cmd[6] = { START_STOP }; |
| 7546 | struct scsi_sense_hdr sshdr; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 7547 | struct scsi_device *sdp; |
| 7548 | unsigned long flags; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7549 | int ret; |
| 7550 | |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 7551 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 7552 | sdp = hba->sdev_ufs_device; |
| 7553 | if (sdp) { |
| 7554 | ret = scsi_device_get(sdp); |
| 7555 | if (!ret && !scsi_device_online(sdp)) { |
| 7556 | ret = -ENODEV; |
| 7557 | scsi_device_put(sdp); |
| 7558 | } |
| 7559 | } else { |
| 7560 | ret = -ENODEV; |
| 7561 | } |
| 7562 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 7563 | |
| 7564 | if (ret) |
| 7565 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7566 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7567 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7568 | * If scsi commands fail, the scsi mid-layer schedules scsi error- |
| 7569 | * handling, which would wait for host to be resumed. Since we know |
| 7570 | * we are functional while we are here, skip host resume in error |
| 7571 | * handling context. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7572 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7573 | hba->host->eh_noresume = 1; |
| 7574 | if (hba->wlun_dev_clr_ua) { |
| 7575 | ret = ufshcd_send_request_sense(hba, sdp); |
| 7576 | if (ret) |
| 7577 | goto out; |
| 7578 | /* Unit attention condition is cleared now */ |
| 7579 | hba->wlun_dev_clr_ua = false; |
| 7580 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7581 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7582 | cmd[4] = pwr_mode << 4; |
| 7583 | |
| 7584 | /* |
| 7585 | * Current function would be generally called from the power management |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 7586 | * 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] | 7587 | * already suspended childs. |
| 7588 | */ |
Christoph Hellwig | fcbfffe | 2017-02-23 16:02:37 +0100 | [diff] [blame] | 7589 | ret = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr, |
| 7590 | START_STOP_TIMEOUT, 0, 0, RQF_PM, NULL); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7591 | if (ret) { |
| 7592 | sdev_printk(KERN_WARNING, sdp, |
Hannes Reinecke | ef61329 | 2014-10-24 14:27:00 +0200 | [diff] [blame] | 7593 | "START_STOP failed for power mode: %d, result %x\n", |
| 7594 | pwr_mode, ret); |
Johannes Thumshirn | c65be1a | 2018-06-25 13:20:58 +0200 | [diff] [blame] | 7595 | if (driver_byte(ret) == DRIVER_SENSE) |
Hannes Reinecke | 2104551 | 2015-01-08 07:43:46 +0100 | [diff] [blame] | 7596 | scsi_print_sense_hdr(sdp, NULL, &sshdr); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7597 | } |
| 7598 | |
| 7599 | if (!ret) |
| 7600 | hba->curr_dev_pwr_mode = pwr_mode; |
| 7601 | out: |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 7602 | scsi_device_put(sdp); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7603 | hba->host->eh_noresume = 0; |
| 7604 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7605 | } |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7606 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7607 | static int ufshcd_link_state_transition(struct ufs_hba *hba, |
| 7608 | enum uic_link_state req_link_state, |
| 7609 | int check_for_bkops) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 7610 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7611 | int ret = 0; |
| 7612 | |
| 7613 | if (req_link_state == hba->uic_link_state) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 7614 | return 0; |
| 7615 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7616 | if (req_link_state == UIC_LINK_HIBERN8_STATE) { |
| 7617 | ret = ufshcd_uic_hibern8_enter(hba); |
| 7618 | if (!ret) |
| 7619 | ufshcd_set_link_hibern8(hba); |
| 7620 | else |
| 7621 | goto out; |
| 7622 | } |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 7623 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7624 | * If autobkops is enabled, link can't be turned off because |
| 7625 | * turning off the link would also turn off the device. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 7626 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7627 | else if ((req_link_state == UIC_LINK_OFF_STATE) && |
| 7628 | (!check_for_bkops || (check_for_bkops && |
| 7629 | !hba->auto_bkops_enabled))) { |
| 7630 | /* |
Yaniv Gardi | f3099fb | 2016-03-10 17:37:17 +0200 | [diff] [blame] | 7631 | * Let's make sure that link is in low power mode, we are doing |
| 7632 | * this currently by putting the link in Hibern8. Otherway to |
| 7633 | * put the link in low power mode is to send the DME end point |
| 7634 | * to device and then send the DME reset command to local |
| 7635 | * unipro. But putting the link in hibern8 is much faster. |
| 7636 | */ |
| 7637 | ret = ufshcd_uic_hibern8_enter(hba); |
| 7638 | if (ret) |
| 7639 | goto out; |
| 7640 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7641 | * Change controller state to "reset state" which |
| 7642 | * should also put the link in off/reset state |
| 7643 | */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 7644 | ufshcd_hba_stop(hba, true); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7645 | /* |
| 7646 | * TODO: Check if we need any delay to make sure that |
| 7647 | * controller is reset |
| 7648 | */ |
| 7649 | ufshcd_set_link_off(hba); |
| 7650 | } |
| 7651 | |
| 7652 | out: |
| 7653 | return ret; |
| 7654 | } |
| 7655 | |
| 7656 | static void ufshcd_vreg_set_lpm(struct ufs_hba *hba) |
| 7657 | { |
| 7658 | /* |
Yaniv Gardi | b799fdf | 2016-03-10 17:37:18 +0200 | [diff] [blame] | 7659 | * It seems some UFS devices may keep drawing more than sleep current |
| 7660 | * (atleast for 500us) from UFS rails (especially from VCCQ rail). |
| 7661 | * To avoid this situation, add 2ms delay before putting these UFS |
| 7662 | * rails in LPM mode. |
| 7663 | */ |
| 7664 | if (!ufshcd_is_link_active(hba) && |
| 7665 | hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM) |
| 7666 | usleep_range(2000, 2100); |
| 7667 | |
| 7668 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7669 | * If UFS device is either in UFS_Sleep turn off VCC rail to save some |
| 7670 | * power. |
| 7671 | * |
| 7672 | * If UFS device and link is in OFF state, all power supplies (VCC, |
| 7673 | * VCCQ, VCCQ2) can be turned off if power on write protect is not |
| 7674 | * required. If UFS link is inactive (Hibern8 or OFF state) and device |
| 7675 | * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode. |
| 7676 | * |
| 7677 | * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway |
| 7678 | * in low power state which would save some power. |
| 7679 | */ |
| 7680 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) && |
| 7681 | !hba->dev_info.is_lu_power_on_wp) { |
| 7682 | ufshcd_setup_vreg(hba, false); |
| 7683 | } else if (!ufshcd_is_ufs_dev_active(hba)) { |
| 7684 | ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false); |
| 7685 | if (!ufshcd_is_link_active(hba)) { |
| 7686 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq); |
| 7687 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2); |
| 7688 | } |
| 7689 | } |
| 7690 | } |
| 7691 | |
| 7692 | static int ufshcd_vreg_set_hpm(struct ufs_hba *hba) |
| 7693 | { |
| 7694 | int ret = 0; |
| 7695 | |
| 7696 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) && |
| 7697 | !hba->dev_info.is_lu_power_on_wp) { |
| 7698 | ret = ufshcd_setup_vreg(hba, true); |
| 7699 | } else if (!ufshcd_is_ufs_dev_active(hba)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7700 | if (!ret && !ufshcd_is_link_active(hba)) { |
| 7701 | ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq); |
| 7702 | if (ret) |
| 7703 | goto vcc_disable; |
| 7704 | ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2); |
| 7705 | if (ret) |
| 7706 | goto vccq_lpm; |
| 7707 | } |
Subhash Jadavani | 69d72ac | 2016-10-27 17:26:24 -0700 | [diff] [blame] | 7708 | ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7709 | } |
| 7710 | goto out; |
| 7711 | |
| 7712 | vccq_lpm: |
| 7713 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq); |
| 7714 | vcc_disable: |
| 7715 | ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false); |
| 7716 | out: |
| 7717 | return ret; |
| 7718 | } |
| 7719 | |
| 7720 | static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba) |
| 7721 | { |
| 7722 | if (ufshcd_is_link_off(hba)) |
| 7723 | ufshcd_setup_hba_vreg(hba, false); |
| 7724 | } |
| 7725 | |
| 7726 | static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba) |
| 7727 | { |
| 7728 | if (ufshcd_is_link_off(hba)) |
| 7729 | ufshcd_setup_hba_vreg(hba, true); |
| 7730 | } |
| 7731 | |
| 7732 | /** |
| 7733 | * ufshcd_suspend - helper function for suspend operations |
| 7734 | * @hba: per adapter instance |
| 7735 | * @pm_op: desired low power operation type |
| 7736 | * |
| 7737 | * This function will try to put the UFS device and link into low power |
| 7738 | * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl" |
| 7739 | * (System PM level). |
| 7740 | * |
| 7741 | * If this function is called during shutdown, it will make sure that |
| 7742 | * both UFS device and UFS link is powered off. |
| 7743 | * |
| 7744 | * NOTE: UFS device & link must be active before we enter in this function. |
| 7745 | * |
| 7746 | * Returns 0 for success and non-zero for failure |
| 7747 | */ |
| 7748 | static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op) |
| 7749 | { |
| 7750 | int ret = 0; |
| 7751 | enum ufs_pm_level pm_lvl; |
| 7752 | enum ufs_dev_pwr_mode req_dev_pwr_mode; |
| 7753 | enum uic_link_state req_link_state; |
| 7754 | |
| 7755 | hba->pm_op_in_progress = 1; |
| 7756 | if (!ufshcd_is_shutdown_pm(pm_op)) { |
| 7757 | pm_lvl = ufshcd_is_runtime_pm(pm_op) ? |
| 7758 | hba->rpm_lvl : hba->spm_lvl; |
| 7759 | req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl); |
| 7760 | req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl); |
| 7761 | } else { |
| 7762 | req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE; |
| 7763 | req_link_state = UIC_LINK_OFF_STATE; |
| 7764 | } |
| 7765 | |
| 7766 | /* |
| 7767 | * If we can't transition into any of the low power modes |
| 7768 | * just gate the clocks. |
| 7769 | */ |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7770 | ufshcd_hold(hba, false); |
| 7771 | hba->clk_gating.is_suspended = true; |
| 7772 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 7773 | if (hba->clk_scaling.is_allowed) { |
| 7774 | cancel_work_sync(&hba->clk_scaling.suspend_work); |
| 7775 | cancel_work_sync(&hba->clk_scaling.resume_work); |
| 7776 | ufshcd_suspend_clkscaling(hba); |
| 7777 | } |
Subhash Jadavani | d6fcf81 | 2016-10-27 17:26:09 -0700 | [diff] [blame] | 7778 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7779 | if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE && |
| 7780 | req_link_state == UIC_LINK_ACTIVE_STATE) { |
| 7781 | goto disable_clks; |
| 7782 | } |
| 7783 | |
| 7784 | if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) && |
| 7785 | (req_link_state == hba->uic_link_state)) |
Subhash Jadavani | d6fcf81 | 2016-10-27 17:26:09 -0700 | [diff] [blame] | 7786 | goto enable_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7787 | |
| 7788 | /* UFS device & link must be active before we enter in this function */ |
| 7789 | if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) { |
| 7790 | ret = -EINVAL; |
Subhash Jadavani | d6fcf81 | 2016-10-27 17:26:09 -0700 | [diff] [blame] | 7791 | goto enable_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7792 | } |
| 7793 | |
| 7794 | if (ufshcd_is_runtime_pm(pm_op)) { |
Subhash Jadavani | 374a246 | 2014-09-25 15:32:35 +0300 | [diff] [blame] | 7795 | if (ufshcd_can_autobkops_during_suspend(hba)) { |
| 7796 | /* |
| 7797 | * The device is idle with no requests in the queue, |
| 7798 | * allow background operations if bkops status shows |
| 7799 | * that performance might be impacted. |
| 7800 | */ |
| 7801 | ret = ufshcd_urgent_bkops(hba); |
| 7802 | if (ret) |
| 7803 | goto enable_gating; |
| 7804 | } else { |
| 7805 | /* make sure that auto bkops is disabled */ |
| 7806 | ufshcd_disable_auto_bkops(hba); |
| 7807 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7808 | } |
| 7809 | |
| 7810 | if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) && |
| 7811 | ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) || |
| 7812 | !ufshcd_is_runtime_pm(pm_op))) { |
| 7813 | /* ensure that bkops is disabled */ |
| 7814 | ufshcd_disable_auto_bkops(hba); |
| 7815 | ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode); |
| 7816 | if (ret) |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7817 | goto enable_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7818 | } |
| 7819 | |
| 7820 | ret = ufshcd_link_state_transition(hba, req_link_state, 1); |
| 7821 | if (ret) |
| 7822 | goto set_dev_active; |
| 7823 | |
| 7824 | ufshcd_vreg_set_lpm(hba); |
| 7825 | |
| 7826 | disable_clks: |
| 7827 | /* |
| 7828 | * Call vendor specific suspend callback. As these callbacks may access |
| 7829 | * vendor specific host controller register space call them before the |
| 7830 | * host clocks are ON. |
| 7831 | */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7832 | ret = ufshcd_vops_suspend(hba, pm_op); |
| 7833 | if (ret) |
| 7834 | goto set_link_active; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7835 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7836 | if (!ufshcd_is_link_active(hba)) |
| 7837 | ufshcd_setup_clocks(hba, false); |
| 7838 | else |
| 7839 | /* If link is active, device ref_clk can't be switched off */ |
| 7840 | __ufshcd_setup_clocks(hba, false, true); |
| 7841 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7842 | hba->clk_gating.state = CLKS_OFF; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7843 | trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7844 | /* |
| 7845 | * Disable the host irq as host controller as there won't be any |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7846 | * host controller transaction expected till resume. |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7847 | */ |
| 7848 | ufshcd_disable_irq(hba); |
| 7849 | /* Put the host controller in low power mode if possible */ |
| 7850 | ufshcd_hba_vreg_set_lpm(hba); |
| 7851 | goto out; |
| 7852 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7853 | set_link_active: |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 7854 | if (hba->clk_scaling.is_allowed) |
| 7855 | ufshcd_resume_clkscaling(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7856 | ufshcd_vreg_set_hpm(hba); |
| 7857 | if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba)) |
| 7858 | ufshcd_set_link_active(hba); |
| 7859 | else if (ufshcd_is_link_off(hba)) |
| 7860 | ufshcd_host_reset_and_restore(hba); |
| 7861 | set_dev_active: |
| 7862 | if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE)) |
| 7863 | ufshcd_disable_auto_bkops(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7864 | enable_gating: |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 7865 | if (hba->clk_scaling.is_allowed) |
| 7866 | ufshcd_resume_clkscaling(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7867 | hba->clk_gating.is_suspended = false; |
| 7868 | ufshcd_release(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7869 | out: |
| 7870 | hba->pm_op_in_progress = 0; |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 7871 | if (ret) |
| 7872 | ufshcd_update_reg_hist(&hba->ufs_stats.suspend_err, (u32)ret); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7873 | return ret; |
| 7874 | } |
| 7875 | |
| 7876 | /** |
| 7877 | * ufshcd_resume - helper function for resume operations |
| 7878 | * @hba: per adapter instance |
| 7879 | * @pm_op: runtime PM or system PM |
| 7880 | * |
| 7881 | * This function basically brings the UFS device, UniPro link and controller |
| 7882 | * to active state. |
| 7883 | * |
| 7884 | * Returns 0 for success and non-zero for failure |
| 7885 | */ |
| 7886 | static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op) |
| 7887 | { |
| 7888 | int ret; |
| 7889 | enum uic_link_state old_link_state; |
| 7890 | |
| 7891 | hba->pm_op_in_progress = 1; |
| 7892 | old_link_state = hba->uic_link_state; |
| 7893 | |
| 7894 | ufshcd_hba_vreg_set_hpm(hba); |
| 7895 | /* Make sure clocks are enabled before accessing controller */ |
| 7896 | ret = ufshcd_setup_clocks(hba, true); |
| 7897 | if (ret) |
| 7898 | goto out; |
| 7899 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7900 | /* enable the host irq as host controller would be active soon */ |
| 7901 | ret = ufshcd_enable_irq(hba); |
| 7902 | if (ret) |
| 7903 | goto disable_irq_and_vops_clks; |
| 7904 | |
| 7905 | ret = ufshcd_vreg_set_hpm(hba); |
| 7906 | if (ret) |
| 7907 | goto disable_irq_and_vops_clks; |
| 7908 | |
| 7909 | /* |
| 7910 | * Call vendor specific resume callback. As these callbacks may access |
| 7911 | * vendor specific host controller register space call them when the |
| 7912 | * host clocks are ON. |
| 7913 | */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7914 | ret = ufshcd_vops_resume(hba, pm_op); |
| 7915 | if (ret) |
| 7916 | goto disable_vreg; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7917 | |
| 7918 | if (ufshcd_is_link_hibern8(hba)) { |
| 7919 | ret = ufshcd_uic_hibern8_exit(hba); |
| 7920 | if (!ret) |
| 7921 | ufshcd_set_link_active(hba); |
| 7922 | else |
| 7923 | goto vendor_suspend; |
| 7924 | } else if (ufshcd_is_link_off(hba)) { |
| 7925 | ret = ufshcd_host_reset_and_restore(hba); |
| 7926 | /* |
| 7927 | * ufshcd_host_reset_and_restore() should have already |
| 7928 | * set the link state as active |
| 7929 | */ |
| 7930 | if (ret || !ufshcd_is_link_active(hba)) |
| 7931 | goto vendor_suspend; |
| 7932 | } |
| 7933 | |
| 7934 | if (!ufshcd_is_ufs_dev_active(hba)) { |
| 7935 | ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE); |
| 7936 | if (ret) |
| 7937 | goto set_old_link_state; |
| 7938 | } |
| 7939 | |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 7940 | if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) |
| 7941 | ufshcd_enable_auto_bkops(hba); |
| 7942 | else |
| 7943 | /* |
| 7944 | * If BKOPs operations are urgently needed at this moment then |
| 7945 | * keep auto-bkops enabled or else disable it. |
| 7946 | */ |
| 7947 | ufshcd_urgent_bkops(hba); |
| 7948 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7949 | hba->clk_gating.is_suspended = false; |
| 7950 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 7951 | if (hba->clk_scaling.is_allowed) |
| 7952 | ufshcd_resume_clkscaling(hba); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 7953 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7954 | /* Schedule clock gating in case of no access to UFS device yet */ |
| 7955 | ufshcd_release(hba); |
Adrian Hunter | ad44837 | 2018-03-20 15:07:38 +0200 | [diff] [blame] | 7956 | |
| 7957 | /* Enable Auto-Hibernate if configured */ |
| 7958 | ufshcd_auto_hibern8_enable(hba); |
| 7959 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7960 | goto out; |
| 7961 | |
| 7962 | set_old_link_state: |
| 7963 | ufshcd_link_state_transition(hba, old_link_state, 0); |
| 7964 | vendor_suspend: |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7965 | ufshcd_vops_suspend(hba, pm_op); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7966 | disable_vreg: |
| 7967 | ufshcd_vreg_set_lpm(hba); |
| 7968 | disable_irq_and_vops_clks: |
| 7969 | ufshcd_disable_irq(hba); |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 7970 | if (hba->clk_scaling.is_allowed) |
| 7971 | ufshcd_suspend_clkscaling(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7972 | ufshcd_setup_clocks(hba, false); |
| 7973 | out: |
| 7974 | hba->pm_op_in_progress = 0; |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 7975 | if (ret) |
| 7976 | ufshcd_update_reg_hist(&hba->ufs_stats.resume_err, (u32)ret); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7977 | return ret; |
| 7978 | } |
| 7979 | |
| 7980 | /** |
| 7981 | * ufshcd_system_suspend - system suspend routine |
| 7982 | * @hba: per adapter instance |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7983 | * |
| 7984 | * Check the description of ufshcd_suspend() function for more details. |
| 7985 | * |
| 7986 | * Returns 0 for success and non-zero for failure |
| 7987 | */ |
| 7988 | int ufshcd_system_suspend(struct ufs_hba *hba) |
| 7989 | { |
| 7990 | int ret = 0; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7991 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7992 | |
| 7993 | if (!hba || !hba->is_powered) |
Dolev Raviv | 233b594 | 2014-10-23 13:25:14 +0300 | [diff] [blame] | 7994 | return 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7995 | |
subhashj@codeaurora.org | 0b25773 | 2016-11-23 16:33:08 -0800 | [diff] [blame] | 7996 | if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) == |
| 7997 | hba->curr_dev_pwr_mode) && |
| 7998 | (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) == |
| 7999 | hba->uic_link_state)) |
| 8000 | goto out; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8001 | |
subhashj@codeaurora.org | 0b25773 | 2016-11-23 16:33:08 -0800 | [diff] [blame] | 8002 | if (pm_runtime_suspended(hba->dev)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8003 | /* |
| 8004 | * UFS device and/or UFS link low power states during runtime |
| 8005 | * suspend seems to be different than what is expected during |
| 8006 | * system suspend. Hence runtime resume the devic & link and |
| 8007 | * let the system suspend low power states to take effect. |
| 8008 | * TODO: If resume takes longer time, we might have optimize |
| 8009 | * it in future by not resuming everything if possible. |
| 8010 | */ |
| 8011 | ret = ufshcd_runtime_resume(hba); |
| 8012 | if (ret) |
| 8013 | goto out; |
| 8014 | } |
| 8015 | |
| 8016 | ret = ufshcd_suspend(hba, UFS_SYSTEM_PM); |
| 8017 | out: |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8018 | trace_ufshcd_system_suspend(dev_name(hba->dev), ret, |
| 8019 | ktime_to_us(ktime_sub(ktime_get(), start)), |
Subhash Jadavani | 73eba2b | 2017-01-10 16:48:25 -0800 | [diff] [blame] | 8020 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
Dolev Raviv | e785060 | 2014-09-25 15:32:36 +0300 | [diff] [blame] | 8021 | if (!ret) |
| 8022 | hba->is_sys_suspended = true; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8023 | return ret; |
| 8024 | } |
| 8025 | EXPORT_SYMBOL(ufshcd_system_suspend); |
| 8026 | |
| 8027 | /** |
| 8028 | * ufshcd_system_resume - system resume routine |
| 8029 | * @hba: per adapter instance |
| 8030 | * |
| 8031 | * Returns 0 for success and non-zero for failure |
| 8032 | */ |
| 8033 | |
| 8034 | int ufshcd_system_resume(struct ufs_hba *hba) |
| 8035 | { |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8036 | int ret = 0; |
| 8037 | ktime_t start = ktime_get(); |
| 8038 | |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 8039 | if (!hba) |
| 8040 | return -EINVAL; |
| 8041 | |
| 8042 | if (!hba->is_powered || pm_runtime_suspended(hba->dev)) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8043 | /* |
| 8044 | * Let the runtime resume take care of resuming |
| 8045 | * if runtime suspended. |
| 8046 | */ |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8047 | goto out; |
| 8048 | else |
| 8049 | ret = ufshcd_resume(hba, UFS_SYSTEM_PM); |
| 8050 | out: |
| 8051 | trace_ufshcd_system_resume(dev_name(hba->dev), ret, |
| 8052 | ktime_to_us(ktime_sub(ktime_get(), start)), |
Subhash Jadavani | 73eba2b | 2017-01-10 16:48:25 -0800 | [diff] [blame] | 8053 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
Stanley Chu | ce9e7bc | 2019-01-07 22:19:34 +0800 | [diff] [blame] | 8054 | if (!ret) |
| 8055 | hba->is_sys_suspended = false; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8056 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8057 | } |
| 8058 | EXPORT_SYMBOL(ufshcd_system_resume); |
| 8059 | |
| 8060 | /** |
| 8061 | * ufshcd_runtime_suspend - runtime suspend routine |
| 8062 | * @hba: per adapter instance |
| 8063 | * |
| 8064 | * Check the description of ufshcd_suspend() function for more details. |
| 8065 | * |
| 8066 | * Returns 0 for success and non-zero for failure |
| 8067 | */ |
| 8068 | int ufshcd_runtime_suspend(struct ufs_hba *hba) |
| 8069 | { |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8070 | int ret = 0; |
| 8071 | ktime_t start = ktime_get(); |
| 8072 | |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 8073 | if (!hba) |
| 8074 | return -EINVAL; |
| 8075 | |
| 8076 | if (!hba->is_powered) |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8077 | goto out; |
| 8078 | else |
| 8079 | ret = ufshcd_suspend(hba, UFS_RUNTIME_PM); |
| 8080 | out: |
| 8081 | trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret, |
| 8082 | ktime_to_us(ktime_sub(ktime_get(), start)), |
Subhash Jadavani | 73eba2b | 2017-01-10 16:48:25 -0800 | [diff] [blame] | 8083 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8084 | return ret; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8085 | } |
| 8086 | EXPORT_SYMBOL(ufshcd_runtime_suspend); |
| 8087 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8088 | /** |
| 8089 | * ufshcd_runtime_resume - runtime resume routine |
| 8090 | * @hba: per adapter instance |
| 8091 | * |
| 8092 | * This function basically brings the UFS device, UniPro link and controller |
| 8093 | * to active state. Following operations are done in this function: |
| 8094 | * |
| 8095 | * 1. Turn on all the controller related clocks |
| 8096 | * 2. Bring the UniPro link out of Hibernate state |
| 8097 | * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device |
| 8098 | * to active state. |
| 8099 | * 4. If auto-bkops is enabled on the device, disable it. |
| 8100 | * |
| 8101 | * So following would be the possible power state after this function return |
| 8102 | * successfully: |
| 8103 | * S1: UFS device in Active state with VCC rail ON |
| 8104 | * UniPro link in Active state |
| 8105 | * All the UFS/UniPro controller clocks are ON |
| 8106 | * |
| 8107 | * Returns 0 for success and non-zero for failure |
| 8108 | */ |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8109 | int ufshcd_runtime_resume(struct ufs_hba *hba) |
| 8110 | { |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8111 | int ret = 0; |
| 8112 | ktime_t start = ktime_get(); |
| 8113 | |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 8114 | if (!hba) |
| 8115 | return -EINVAL; |
| 8116 | |
| 8117 | if (!hba->is_powered) |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8118 | goto out; |
| 8119 | else |
| 8120 | ret = ufshcd_resume(hba, UFS_RUNTIME_PM); |
| 8121 | out: |
| 8122 | trace_ufshcd_runtime_resume(dev_name(hba->dev), ret, |
| 8123 | ktime_to_us(ktime_sub(ktime_get(), start)), |
Subhash Jadavani | 73eba2b | 2017-01-10 16:48:25 -0800 | [diff] [blame] | 8124 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 8125 | return ret; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8126 | } |
| 8127 | EXPORT_SYMBOL(ufshcd_runtime_resume); |
| 8128 | |
| 8129 | int ufshcd_runtime_idle(struct ufs_hba *hba) |
| 8130 | { |
| 8131 | return 0; |
| 8132 | } |
| 8133 | EXPORT_SYMBOL(ufshcd_runtime_idle); |
| 8134 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8135 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8136 | * ufshcd_shutdown - shutdown routine |
| 8137 | * @hba: per adapter instance |
| 8138 | * |
| 8139 | * This function would power off both UFS device and UFS link. |
| 8140 | * |
| 8141 | * Returns 0 always to allow force shutdown even in case of errors. |
| 8142 | */ |
| 8143 | int ufshcd_shutdown(struct ufs_hba *hba) |
| 8144 | { |
| 8145 | int ret = 0; |
| 8146 | |
Stanley Chu | f51913e | 2019-09-18 12:20:38 +0800 | [diff] [blame] | 8147 | if (!hba->is_powered) |
| 8148 | goto out; |
| 8149 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8150 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba)) |
| 8151 | goto out; |
| 8152 | |
| 8153 | if (pm_runtime_suspended(hba->dev)) { |
| 8154 | ret = ufshcd_runtime_resume(hba); |
| 8155 | if (ret) |
| 8156 | goto out; |
| 8157 | } |
| 8158 | |
| 8159 | ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM); |
| 8160 | out: |
| 8161 | if (ret) |
| 8162 | dev_err(hba->dev, "%s failed, err %d\n", __func__, ret); |
| 8163 | /* allow force shutdown even in case of errors */ |
| 8164 | return 0; |
| 8165 | } |
| 8166 | EXPORT_SYMBOL(ufshcd_shutdown); |
| 8167 | |
| 8168 | /** |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8169 | * ufshcd_remove - de-allocate SCSI host and host memory space |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8170 | * data structure memory |
Bart Van Assche | 8aa29f1 | 2018-03-01 15:07:20 -0800 | [diff] [blame] | 8171 | * @hba: per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8172 | */ |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8173 | void ufshcd_remove(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8174 | { |
Avri Altman | df032bf | 2018-10-07 17:30:35 +0300 | [diff] [blame] | 8175 | ufs_bsg_remove(hba); |
Stanislav Nijnikov | cbb6813 | 2018-02-15 14:14:01 +0200 | [diff] [blame] | 8176 | ufs_sysfs_remove_nodes(hba->dev); |
Akinobu Mita | cfdf9c9 | 2013-07-30 00:36:03 +0530 | [diff] [blame] | 8177 | scsi_remove_host(hba->host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8178 | /* disable interrupts */ |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 8179 | ufshcd_disable_intr(hba, hba->intr_mask); |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 8180 | ufshcd_hba_stop(hba, true); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8181 | |
Vivek Gautam | eebcc19 | 2018-08-07 23:17:39 +0530 | [diff] [blame] | 8182 | ufshcd_exit_clk_scaling(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8183 | ufshcd_exit_clk_gating(hba); |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 8184 | if (ufshcd_is_clkscaling_supported(hba)) |
| 8185 | device_remove_file(hba->dev, &hba->clk_scaling.enable_attr); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8186 | ufshcd_hba_exit(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8187 | } |
| 8188 | EXPORT_SYMBOL_GPL(ufshcd_remove); |
| 8189 | |
| 8190 | /** |
Yaniv Gardi | 47555a5 | 2015-10-28 13:15:49 +0200 | [diff] [blame] | 8191 | * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA) |
| 8192 | * @hba: pointer to Host Bus Adapter (HBA) |
| 8193 | */ |
| 8194 | void ufshcd_dealloc_host(struct ufs_hba *hba) |
| 8195 | { |
| 8196 | scsi_host_put(hba->host); |
| 8197 | } |
| 8198 | EXPORT_SYMBOL_GPL(ufshcd_dealloc_host); |
| 8199 | |
| 8200 | /** |
Akinobu Mita | ca3d7bf | 2014-07-13 21:24:46 +0900 | [diff] [blame] | 8201 | * ufshcd_set_dma_mask - Set dma mask based on the controller |
| 8202 | * addressing capability |
| 8203 | * @hba: per adapter instance |
| 8204 | * |
| 8205 | * Returns 0 for success, non-zero for failure |
| 8206 | */ |
| 8207 | static int ufshcd_set_dma_mask(struct ufs_hba *hba) |
| 8208 | { |
| 8209 | if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) { |
| 8210 | if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64))) |
| 8211 | return 0; |
| 8212 | } |
| 8213 | return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32)); |
| 8214 | } |
| 8215 | |
| 8216 | /** |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8217 | * ufshcd_alloc_host - allocate Host Bus Adapter (HBA) |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8218 | * @dev: pointer to device handle |
| 8219 | * @hba_handle: driver private handle |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8220 | * Returns 0 on success, non-zero value on failure |
| 8221 | */ |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8222 | int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8223 | { |
| 8224 | struct Scsi_Host *host; |
| 8225 | struct ufs_hba *hba; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8226 | int err = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8227 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8228 | if (!dev) { |
| 8229 | dev_err(dev, |
| 8230 | "Invalid memory reference for dev is NULL\n"); |
| 8231 | err = -ENODEV; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8232 | goto out_error; |
| 8233 | } |
| 8234 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8235 | host = scsi_host_alloc(&ufshcd_driver_template, |
| 8236 | sizeof(struct ufs_hba)); |
| 8237 | if (!host) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8238 | dev_err(dev, "scsi_host_alloc failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8239 | err = -ENOMEM; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8240 | goto out_error; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8241 | } |
| 8242 | hba = shost_priv(host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8243 | hba->host = host; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8244 | hba->dev = dev; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8245 | *hba_handle = hba; |
Subhash Jadavani | 9e1e8a7 | 2018-10-16 14:29:41 +0530 | [diff] [blame] | 8246 | hba->dev_ref_clk_freq = REF_CLK_FREQ_INVAL; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8247 | |
Szymon Mielczarek | 566ec9a | 2017-06-05 11:36:54 +0300 | [diff] [blame] | 8248 | INIT_LIST_HEAD(&hba->clk_list_head); |
| 8249 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8250 | out_error: |
| 8251 | return err; |
| 8252 | } |
| 8253 | EXPORT_SYMBOL(ufshcd_alloc_host); |
| 8254 | |
| 8255 | /** |
| 8256 | * ufshcd_init - Driver initialization routine |
| 8257 | * @hba: per-adapter instance |
| 8258 | * @mmio_base: base register address |
| 8259 | * @irq: Interrupt line of device |
| 8260 | * Returns 0 on success, non-zero value on failure |
| 8261 | */ |
| 8262 | int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq) |
| 8263 | { |
| 8264 | int err; |
| 8265 | struct Scsi_Host *host = hba->host; |
| 8266 | struct device *dev = hba->dev; |
| 8267 | |
| 8268 | if (!mmio_base) { |
| 8269 | dev_err(hba->dev, |
| 8270 | "Invalid memory reference for mmio_base is NULL\n"); |
| 8271 | err = -ENODEV; |
| 8272 | goto out_error; |
| 8273 | } |
| 8274 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8275 | hba->mmio_base = mmio_base; |
| 8276 | hba->irq = irq; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8277 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8278 | err = ufshcd_hba_init(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8279 | if (err) |
| 8280 | goto out_error; |
| 8281 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8282 | /* Read capabilities registers */ |
| 8283 | ufshcd_hba_capabilities(hba); |
| 8284 | |
| 8285 | /* Get UFS version supported by the controller */ |
| 8286 | hba->ufs_version = ufshcd_get_ufs_version(hba); |
| 8287 | |
Yaniv Gardi | c01848c | 2016-12-05 19:25:02 -0800 | [diff] [blame] | 8288 | if ((hba->ufs_version != UFSHCI_VERSION_10) && |
| 8289 | (hba->ufs_version != UFSHCI_VERSION_11) && |
| 8290 | (hba->ufs_version != UFSHCI_VERSION_20) && |
| 8291 | (hba->ufs_version != UFSHCI_VERSION_21)) |
| 8292 | dev_err(hba->dev, "invalid UFS version 0x%x\n", |
| 8293 | hba->ufs_version); |
| 8294 | |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 8295 | /* Get Interrupt bit mask per version */ |
| 8296 | hba->intr_mask = ufshcd_get_intr_mask(hba); |
| 8297 | |
Akinobu Mita | ca3d7bf | 2014-07-13 21:24:46 +0900 | [diff] [blame] | 8298 | err = ufshcd_set_dma_mask(hba); |
| 8299 | if (err) { |
| 8300 | dev_err(hba->dev, "set dma mask failed\n"); |
| 8301 | goto out_disable; |
| 8302 | } |
| 8303 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8304 | /* Allocate memory for host memory space */ |
| 8305 | err = ufshcd_memory_alloc(hba); |
| 8306 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8307 | dev_err(hba->dev, "Memory allocation failed\n"); |
| 8308 | goto out_disable; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8309 | } |
| 8310 | |
| 8311 | /* Configure LRB */ |
| 8312 | ufshcd_host_memory_configure(hba); |
| 8313 | |
| 8314 | host->can_queue = hba->nutrs; |
| 8315 | host->cmd_per_lun = hba->nutrs; |
| 8316 | host->max_id = UFSHCD_MAX_ID; |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 8317 | host->max_lun = UFS_MAX_LUNS; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8318 | host->max_channel = UFSHCD_MAX_CHANNEL; |
| 8319 | host->unique_id = host->host_no; |
Avri Altman | a851b2b | 2018-10-07 17:30:34 +0300 | [diff] [blame] | 8320 | host->max_cmd_len = UFS_CDB_SIZE; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8321 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 8322 | hba->max_pwr_info.is_valid = false; |
| 8323 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8324 | /* Initailize wait queue for task management */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 8325 | init_waitqueue_head(&hba->tm_wq); |
| 8326 | init_waitqueue_head(&hba->tm_tag_wq); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8327 | |
| 8328 | /* Initialize work queues */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 8329 | INIT_WORK(&hba->eh_work, ufshcd_err_handler); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8330 | INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8331 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 8332 | /* Initialize UIC command mutex */ |
| 8333 | mutex_init(&hba->uic_cmd_mutex); |
| 8334 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 8335 | /* Initialize mutex for device management commands */ |
| 8336 | mutex_init(&hba->dev_cmd.lock); |
| 8337 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 8338 | init_rwsem(&hba->clk_scaling_lock); |
| 8339 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 8340 | /* Initialize device management tag acquire wait queue */ |
| 8341 | init_waitqueue_head(&hba->dev_cmd.tag_wq); |
| 8342 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8343 | ufshcd_init_clk_gating(hba); |
Yaniv Gardi | 199ef13 | 2016-03-10 17:37:06 +0200 | [diff] [blame] | 8344 | |
Vivek Gautam | eebcc19 | 2018-08-07 23:17:39 +0530 | [diff] [blame] | 8345 | ufshcd_init_clk_scaling(hba); |
| 8346 | |
Yaniv Gardi | 199ef13 | 2016-03-10 17:37:06 +0200 | [diff] [blame] | 8347 | /* |
| 8348 | * In order to avoid any spurious interrupt immediately after |
| 8349 | * registering UFS controller interrupt handler, clear any pending UFS |
| 8350 | * interrupt status and disable all the UFS interrupts. |
| 8351 | */ |
| 8352 | ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS), |
| 8353 | REG_INTERRUPT_STATUS); |
| 8354 | ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE); |
| 8355 | /* |
| 8356 | * Make sure that UFS interrupts are disabled and any pending interrupt |
| 8357 | * status is cleared before registering UFS interrupt handler. |
| 8358 | */ |
| 8359 | mb(); |
| 8360 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8361 | /* IRQ registration */ |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 8362 | err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8363 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8364 | dev_err(hba->dev, "request irq failed\n"); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8365 | goto exit_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8366 | } else { |
| 8367 | hba->is_irq_enabled = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8368 | } |
| 8369 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8370 | err = scsi_add_host(host, hba->dev); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8371 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8372 | dev_err(hba->dev, "scsi_add_host failed\n"); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8373 | goto exit_gating; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8374 | } |
| 8375 | |
Bjorn Andersson | d8d9f79 | 2019-08-28 12:17:54 -0700 | [diff] [blame] | 8376 | /* Reset the attached device */ |
| 8377 | ufshcd_vops_device_reset(hba); |
| 8378 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 8379 | /* Host controller enable */ |
| 8380 | err = ufshcd_hba_enable(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8381 | if (err) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 8382 | dev_err(hba->dev, "Host controller enable failed\n"); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 8383 | ufshcd_print_host_regs(hba); |
Gilad Broner | 6ba6558 | 2017-02-03 16:57:28 -0800 | [diff] [blame] | 8384 | ufshcd_print_host_state(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8385 | goto out_remove_scsi_host; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8386 | } |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 8387 | |
subhashj@codeaurora.org | 0c8f758 | 2016-12-22 18:41:11 -0800 | [diff] [blame] | 8388 | /* |
| 8389 | * Set the default power management level for runtime and system PM. |
| 8390 | * Default power saving mode is to keep UFS link in Hibern8 state |
| 8391 | * and UFS device in sleep state. |
| 8392 | */ |
| 8393 | hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state( |
| 8394 | UFS_SLEEP_PWR_MODE, |
| 8395 | UIC_LINK_HIBERN8_STATE); |
| 8396 | hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state( |
| 8397 | UFS_SLEEP_PWR_MODE, |
| 8398 | UIC_LINK_HIBERN8_STATE); |
| 8399 | |
Adrian Hunter | ad44837 | 2018-03-20 15:07:38 +0200 | [diff] [blame] | 8400 | /* Set the default auto-hiberate idle timer value to 150 ms */ |
Stanley Chu | f571b37 | 2019-05-21 14:44:53 +0800 | [diff] [blame] | 8401 | if (ufshcd_is_auto_hibern8_supported(hba) && !hba->ahit) { |
Adrian Hunter | ad44837 | 2018-03-20 15:07:38 +0200 | [diff] [blame] | 8402 | hba->ahit = FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, 150) | |
| 8403 | FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, 3); |
| 8404 | } |
| 8405 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 8406 | /* Hold auto suspend until async scan completes */ |
| 8407 | pm_runtime_get_sync(dev); |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 8408 | atomic_set(&hba->scsi_block_reqs_cnt, 0); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8409 | /* |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 8410 | * We are assuming that device wasn't put in sleep/power-down |
| 8411 | * state exclusively during the boot stage before kernel. |
| 8412 | * This assumption helps avoid doing link startup twice during |
| 8413 | * ufshcd_probe_hba(). |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8414 | */ |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 8415 | ufshcd_set_ufs_dev_active(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8416 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 8417 | async_schedule(ufshcd_async_scan, hba); |
Stanislav Nijnikov | cbb6813 | 2018-02-15 14:14:01 +0200 | [diff] [blame] | 8418 | ufs_sysfs_add_nodes(hba->dev); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 8419 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8420 | return 0; |
| 8421 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8422 | out_remove_scsi_host: |
| 8423 | scsi_remove_host(hba->host); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8424 | exit_gating: |
Vivek Gautam | eebcc19 | 2018-08-07 23:17:39 +0530 | [diff] [blame] | 8425 | ufshcd_exit_clk_scaling(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8426 | ufshcd_exit_clk_gating(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8427 | out_disable: |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8428 | hba->is_irq_enabled = false; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8429 | ufshcd_hba_exit(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8430 | out_error: |
| 8431 | return err; |
| 8432 | } |
| 8433 | EXPORT_SYMBOL_GPL(ufshcd_init); |
| 8434 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8435 | MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>"); |
| 8436 | MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>"); |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 8437 | MODULE_DESCRIPTION("Generic UFS host controller driver Core"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8438 | MODULE_LICENSE("GPL"); |
| 8439 | MODULE_VERSION(UFSHCD_DRIVER_VERSION); |