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> |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 44 | #include "ufshcd.h" |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 45 | #include "ufs_quirks.h" |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 46 | #include "unipro.h" |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 47 | |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 48 | #define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\ |
| 49 | UTP_TASK_REQ_COMPL |\ |
| 50 | UFSHCD_ERROR_MASK) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 51 | /* UIC command timeout, unit: ms */ |
| 52 | #define UIC_CMD_TIMEOUT 500 |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 53 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 54 | /* NOP OUT retries waiting for NOP IN response */ |
| 55 | #define NOP_OUT_RETRIES 10 |
| 56 | /* Timeout after 30 msecs if NOP OUT hangs without response */ |
| 57 | #define NOP_OUT_TIMEOUT 30 /* msecs */ |
| 58 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 59 | /* Query request retries */ |
| 60 | #define QUERY_REQ_RETRIES 10 |
| 61 | /* Query request timeout */ |
| 62 | #define QUERY_REQ_TIMEOUT 30 /* msec */ |
Yaniv Gardi | e5ad406 | 2016-02-01 15:02:41 +0200 | [diff] [blame] | 63 | /* |
| 64 | * Query request timeout for fDeviceInit flag |
| 65 | * fDeviceInit query response time for some devices is too large that default |
| 66 | * QUERY_REQ_TIMEOUT may not be enough for such devices. |
| 67 | */ |
| 68 | #define QUERY_FDEVICEINIT_REQ_TIMEOUT 600 /* msec */ |
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 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 91 | #define ufshcd_toggle_vreg(_dev, _vreg, _on) \ |
| 92 | ({ \ |
| 93 | int _ret; \ |
| 94 | if (_on) \ |
| 95 | _ret = ufshcd_enable_vreg(_dev, _vreg); \ |
| 96 | else \ |
| 97 | _ret = ufshcd_disable_vreg(_dev, _vreg); \ |
| 98 | _ret; \ |
| 99 | }) |
| 100 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 101 | enum { |
| 102 | UFSHCD_MAX_CHANNEL = 0, |
| 103 | UFSHCD_MAX_ID = 1, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 104 | UFSHCD_CMD_PER_LUN = 32, |
| 105 | UFSHCD_CAN_QUEUE = 32, |
| 106 | }; |
| 107 | |
| 108 | /* UFSHCD states */ |
| 109 | enum { |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 110 | UFSHCD_STATE_RESET, |
| 111 | UFSHCD_STATE_ERROR, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 112 | UFSHCD_STATE_OPERATIONAL, |
Zang Leigang | a17bddc | 2017-04-04 19:32:20 +0000 | [diff] [blame] | 113 | UFSHCD_STATE_EH_SCHEDULED, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | /* UFSHCD error handling flags */ |
| 117 | enum { |
| 118 | UFSHCD_EH_IN_PROGRESS = (1 << 0), |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 119 | }; |
| 120 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 121 | /* UFSHCD UIC layer error flags */ |
| 122 | enum { |
| 123 | UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 124 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */ |
| 125 | UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */ |
| 126 | UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */ |
| 127 | UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */ |
| 128 | UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 129 | }; |
| 130 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 131 | /* Interrupt configuration options */ |
| 132 | enum { |
| 133 | UFSHCD_INT_DISABLE, |
| 134 | UFSHCD_INT_ENABLE, |
| 135 | UFSHCD_INT_CLEAR, |
| 136 | }; |
| 137 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 138 | #define ufshcd_set_eh_in_progress(h) \ |
| 139 | (h->eh_flags |= UFSHCD_EH_IN_PROGRESS) |
| 140 | #define ufshcd_eh_in_progress(h) \ |
| 141 | (h->eh_flags & UFSHCD_EH_IN_PROGRESS) |
| 142 | #define ufshcd_clear_eh_in_progress(h) \ |
| 143 | (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS) |
| 144 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 145 | #define ufshcd_set_ufs_dev_active(h) \ |
| 146 | ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE) |
| 147 | #define ufshcd_set_ufs_dev_sleep(h) \ |
| 148 | ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE) |
| 149 | #define ufshcd_set_ufs_dev_poweroff(h) \ |
| 150 | ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE) |
| 151 | #define ufshcd_is_ufs_dev_active(h) \ |
| 152 | ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE) |
| 153 | #define ufshcd_is_ufs_dev_sleep(h) \ |
| 154 | ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE) |
| 155 | #define ufshcd_is_ufs_dev_poweroff(h) \ |
| 156 | ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE) |
| 157 | |
| 158 | static struct ufs_pm_lvl_states ufs_pm_lvl_states[] = { |
| 159 | {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE}, |
| 160 | {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 161 | {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE}, |
| 162 | {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 163 | {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 164 | {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE}, |
| 165 | }; |
| 166 | |
| 167 | static inline enum ufs_dev_pwr_mode |
| 168 | ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl) |
| 169 | { |
| 170 | return ufs_pm_lvl_states[lvl].dev_state; |
| 171 | } |
| 172 | |
| 173 | static inline enum uic_link_state |
| 174 | ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl) |
| 175 | { |
| 176 | return ufs_pm_lvl_states[lvl].link_state; |
| 177 | } |
| 178 | |
Subhash Jadavani | bedc629 | 2017-04-04 19:32:13 +0000 | [diff] [blame] | 179 | static struct ufs_dev_fix ufs_fixups[] = { |
| 180 | /* UFS cards deviations table */ |
| 181 | UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, |
| 182 | UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM), |
| 183 | UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, UFS_DEVICE_NO_VCCQ), |
| 184 | UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, |
| 185 | UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS), |
| 186 | UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, |
| 187 | UFS_DEVICE_NO_FASTAUTO), |
| 188 | UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, |
| 189 | UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE), |
| 190 | UFS_FIX(UFS_VENDOR_TOSHIBA, UFS_ANY_MODEL, |
| 191 | UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM), |
| 192 | UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9C8KBADG", |
| 193 | UFS_DEVICE_QUIRK_PA_TACTIVATE), |
| 194 | UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9D8KBADG", |
| 195 | UFS_DEVICE_QUIRK_PA_TACTIVATE), |
| 196 | UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL, UFS_DEVICE_NO_VCCQ), |
| 197 | UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL, |
| 198 | UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME), |
| 199 | |
| 200 | END_FIX |
| 201 | }; |
| 202 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 203 | static void ufshcd_tmc_handler(struct ufs_hba *hba); |
| 204 | static void ufshcd_async_scan(void *data, async_cookie_t cookie); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 205 | static int ufshcd_reset_and_restore(struct ufs_hba *hba); |
| 206 | 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] | 207 | static void ufshcd_hba_exit(struct ufs_hba *hba); |
| 208 | static int ufshcd_probe_hba(struct ufs_hba *hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 209 | static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on, |
| 210 | bool skip_ref_clk); |
| 211 | static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on); |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 212 | static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 213 | static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba); |
| 214 | static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 215 | 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] | 216 | static int ufshcd_host_reset_and_restore(struct ufs_hba *hba); |
| 217 | static irqreturn_t ufshcd_intr(int irq, void *__hba); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 218 | static int ufshcd_config_pwr_mode(struct ufs_hba *hba, |
| 219 | struct ufs_pa_layer_attr *desired_pwr_mode); |
Yaniv Gardi | 874237f | 2015-05-17 18:55:03 +0300 | [diff] [blame] | 220 | static int ufshcd_change_power_mode(struct ufs_hba *hba, |
| 221 | struct ufs_pa_layer_attr *pwr_mode); |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 222 | static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag) |
| 223 | { |
| 224 | return tag >= 0 && tag < hba->nutrs; |
| 225 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 226 | |
| 227 | static inline int ufshcd_enable_irq(struct ufs_hba *hba) |
| 228 | { |
| 229 | int ret = 0; |
| 230 | |
| 231 | if (!hba->is_irq_enabled) { |
| 232 | ret = request_irq(hba->irq, ufshcd_intr, IRQF_SHARED, UFSHCD, |
| 233 | hba); |
| 234 | if (ret) |
| 235 | dev_err(hba->dev, "%s: request_irq failed, ret=%d\n", |
| 236 | __func__, ret); |
| 237 | hba->is_irq_enabled = true; |
| 238 | } |
| 239 | |
| 240 | return ret; |
| 241 | } |
| 242 | |
| 243 | static inline void ufshcd_disable_irq(struct ufs_hba *hba) |
| 244 | { |
| 245 | if (hba->is_irq_enabled) { |
| 246 | free_irq(hba->irq, hba); |
| 247 | hba->is_irq_enabled = false; |
| 248 | } |
| 249 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 250 | |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 251 | /* replace non-printable or non-ASCII characters with spaces */ |
| 252 | static inline void ufshcd_remove_non_printable(char *val) |
| 253 | { |
| 254 | if (!val) |
| 255 | return; |
| 256 | |
| 257 | if (*val < 0x20 || *val > 0x7e) |
| 258 | *val = ' '; |
| 259 | } |
| 260 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 261 | /* |
| 262 | * ufshcd_wait_for_register - wait for register value to change |
| 263 | * @hba - per-adapter interface |
| 264 | * @reg - mmio register offset |
| 265 | * @mask - mask to apply to read register value |
| 266 | * @val - wait condition |
| 267 | * @interval_us - polling interval in microsecs |
| 268 | * @timeout_ms - timeout in millisecs |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 269 | * @can_sleep - perform sleep or just spin |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 270 | * |
| 271 | * Returns -ETIMEDOUT on error, zero on success |
| 272 | */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 273 | int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask, |
| 274 | u32 val, unsigned long interval_us, |
| 275 | unsigned long timeout_ms, bool can_sleep) |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 276 | { |
| 277 | int err = 0; |
| 278 | unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms); |
| 279 | |
| 280 | /* ignore bits that we don't intend to wait on */ |
| 281 | val = val & mask; |
| 282 | |
| 283 | while ((ufshcd_readl(hba, reg) & mask) != val) { |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 284 | if (can_sleep) |
| 285 | usleep_range(interval_us, interval_us + 50); |
| 286 | else |
| 287 | udelay(interval_us); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 288 | if (time_after(jiffies, timeout)) { |
| 289 | if ((ufshcd_readl(hba, reg) & mask) != val) |
| 290 | err = -ETIMEDOUT; |
| 291 | break; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | return err; |
| 296 | } |
| 297 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 298 | /** |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 299 | * ufshcd_get_intr_mask - Get the interrupt bit mask |
| 300 | * @hba - Pointer to adapter instance |
| 301 | * |
| 302 | * Returns interrupt bit mask per version |
| 303 | */ |
| 304 | static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba) |
| 305 | { |
| 306 | if (hba->ufs_version == UFSHCI_VERSION_10) |
| 307 | return INTERRUPT_MASK_ALL_VER_10; |
| 308 | else |
| 309 | return INTERRUPT_MASK_ALL_VER_11; |
| 310 | } |
| 311 | |
| 312 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 313 | * ufshcd_get_ufs_version - Get the UFS version supported by the HBA |
| 314 | * @hba - Pointer to adapter instance |
| 315 | * |
| 316 | * Returns UFSHCI version supported by the controller |
| 317 | */ |
| 318 | static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba) |
| 319 | { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 320 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION) |
| 321 | return ufshcd_vops_get_ufs_hci_version(hba); |
Yaniv Gardi | 9949e70 | 2015-05-17 18:55:05 +0300 | [diff] [blame] | 322 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 323 | return ufshcd_readl(hba, REG_UFS_VERSION); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /** |
| 327 | * ufshcd_is_device_present - Check if any device connected to |
| 328 | * the host controller |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 329 | * @hba: pointer to adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 330 | * |
Venkatraman S | 73ec513 | 2012-07-10 19:39:23 +0530 | [diff] [blame] | 331 | * Returns 1 if device present, 0 if no device detected |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 332 | */ |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 333 | static inline int ufshcd_is_device_present(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 334 | { |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 335 | return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & |
| 336 | DEVICE_PRESENT) ? 1 : 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /** |
| 340 | * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status |
| 341 | * @lrb: pointer to local command reference block |
| 342 | * |
| 343 | * This function is used to get the OCS field from UTRD |
| 344 | * Returns the OCS field in the UTRD |
| 345 | */ |
| 346 | static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp) |
| 347 | { |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 348 | 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] | 349 | } |
| 350 | |
| 351 | /** |
| 352 | * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status |
| 353 | * @task_req_descp: pointer to utp_task_req_desc structure |
| 354 | * |
| 355 | * This function is used to get the OCS field from UTMRD |
| 356 | * Returns the OCS field in the UTMRD |
| 357 | */ |
| 358 | static inline int |
| 359 | ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp) |
| 360 | { |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 361 | return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /** |
| 365 | * ufshcd_get_tm_free_slot - get a free slot for task management request |
| 366 | * @hba: per adapter instance |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 367 | * @free_slot: pointer to variable with available slot value |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 368 | * |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 369 | * Get a free tag and lock it until ufshcd_put_tm_slot() is called. |
| 370 | * Returns 0 if free slot is not available, else return 1 with tag value |
| 371 | * in @free_slot. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 372 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 373 | 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] | 374 | { |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 375 | int tag; |
| 376 | bool ret = false; |
| 377 | |
| 378 | if (!free_slot) |
| 379 | goto out; |
| 380 | |
| 381 | do { |
| 382 | tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs); |
| 383 | if (tag >= hba->nutmrs) |
| 384 | goto out; |
| 385 | } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use)); |
| 386 | |
| 387 | *free_slot = tag; |
| 388 | ret = true; |
| 389 | out: |
| 390 | return ret; |
| 391 | } |
| 392 | |
| 393 | static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot) |
| 394 | { |
| 395 | clear_bit_unlock(slot, &hba->tm_slots_in_use); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /** |
| 399 | * ufshcd_utrl_clear - Clear a bit in UTRLCLR register |
| 400 | * @hba: per adapter instance |
| 401 | * @pos: position of the bit to be cleared |
| 402 | */ |
| 403 | static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos) |
| 404 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 405 | ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /** |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 409 | * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field |
| 410 | * @hba: per adapter instance |
| 411 | * @tag: position of the bit to be cleared |
| 412 | */ |
| 413 | static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag) |
| 414 | { |
| 415 | __clear_bit(tag, &hba->outstanding_reqs); |
| 416 | } |
| 417 | |
| 418 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 419 | * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY |
| 420 | * @reg: Register value of host controller status |
| 421 | * |
| 422 | * Returns integer, 0 on Success and positive value if failed |
| 423 | */ |
| 424 | static inline int ufshcd_get_lists_status(u32 reg) |
| 425 | { |
| 426 | /* |
| 427 | * The mask 0xFF is for the following HCS register bits |
| 428 | * Bit Description |
| 429 | * 0 Device Present |
| 430 | * 1 UTRLRDY |
| 431 | * 2 UTMRLRDY |
| 432 | * 3 UCRDY |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 433 | * 4-7 reserved |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 434 | */ |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 435 | return ((reg & 0xFF) >> 1) ^ 0x07; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /** |
| 439 | * ufshcd_get_uic_cmd_result - Get the UIC command result |
| 440 | * @hba: Pointer to adapter instance |
| 441 | * |
| 442 | * This function gets the result of UIC command completion |
| 443 | * Returns 0 on success, non zero value on error |
| 444 | */ |
| 445 | static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba) |
| 446 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 447 | return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) & |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 448 | MASK_UIC_COMMAND_RESULT; |
| 449 | } |
| 450 | |
| 451 | /** |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 452 | * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command |
| 453 | * @hba: Pointer to adapter instance |
| 454 | * |
| 455 | * This function gets UIC command argument3 |
| 456 | * Returns 0 on success, non zero value on error |
| 457 | */ |
| 458 | static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba) |
| 459 | { |
| 460 | return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3); |
| 461 | } |
| 462 | |
| 463 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 464 | * ufshcd_get_req_rsp - returns the TR response transaction type |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 465 | * @ucd_rsp_ptr: pointer to response UPIU |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 466 | */ |
| 467 | static inline int |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 468 | ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 469 | { |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 470 | return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | /** |
| 474 | * ufshcd_get_rsp_upiu_result - Get the result from response UPIU |
| 475 | * @ucd_rsp_ptr: pointer to response UPIU |
| 476 | * |
| 477 | * This function gets the response status and scsi_status from response UPIU |
| 478 | * Returns the response result code. |
| 479 | */ |
| 480 | static inline int |
| 481 | ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 482 | { |
| 483 | return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT; |
| 484 | } |
| 485 | |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 486 | /* |
| 487 | * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length |
| 488 | * from response UPIU |
| 489 | * @ucd_rsp_ptr: pointer to response UPIU |
| 490 | * |
| 491 | * Return the data segment length. |
| 492 | */ |
| 493 | static inline unsigned int |
| 494 | ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 495 | { |
| 496 | return be32_to_cpu(ucd_rsp_ptr->header.dword_2) & |
| 497 | MASK_RSP_UPIU_DATA_SEG_LEN; |
| 498 | } |
| 499 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 500 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 501 | * ufshcd_is_exception_event - Check if the device raised an exception event |
| 502 | * @ucd_rsp_ptr: pointer to response UPIU |
| 503 | * |
| 504 | * The function checks if the device raised an exception event indicated in |
| 505 | * the Device Information field of response UPIU. |
| 506 | * |
| 507 | * Returns true if exception is raised, false otherwise. |
| 508 | */ |
| 509 | static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 510 | { |
| 511 | return be32_to_cpu(ucd_rsp_ptr->header.dword_2) & |
| 512 | MASK_RSP_EXCEPTION_EVENT ? true : false; |
| 513 | } |
| 514 | |
| 515 | /** |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 516 | * ufshcd_reset_intr_aggr - Reset interrupt aggregation values. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 517 | * @hba: per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 518 | */ |
| 519 | static inline void |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 520 | ufshcd_reset_intr_aggr(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 521 | { |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 522 | ufshcd_writel(hba, INT_AGGR_ENABLE | |
| 523 | INT_AGGR_COUNTER_AND_TIMER_RESET, |
| 524 | REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * ufshcd_config_intr_aggr - Configure interrupt aggregation values. |
| 529 | * @hba: per adapter instance |
| 530 | * @cnt: Interrupt aggregation counter threshold |
| 531 | * @tmout: Interrupt aggregation timeout value |
| 532 | */ |
| 533 | static inline void |
| 534 | ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout) |
| 535 | { |
| 536 | ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE | |
| 537 | INT_AGGR_COUNTER_THLD_VAL(cnt) | |
| 538 | INT_AGGR_TIMEOUT_VAL(tmout), |
| 539 | REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | /** |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 543 | * ufshcd_disable_intr_aggr - Disables interrupt aggregation. |
| 544 | * @hba: per adapter instance |
| 545 | */ |
| 546 | static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba) |
| 547 | { |
| 548 | ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
| 549 | } |
| 550 | |
| 551 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 552 | * ufshcd_enable_run_stop_reg - Enable run-stop registers, |
| 553 | * When run-stop registers are set to 1, it indicates the |
| 554 | * host controller that it can process the requests |
| 555 | * @hba: per adapter instance |
| 556 | */ |
| 557 | static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba) |
| 558 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 559 | ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT, |
| 560 | REG_UTP_TASK_REQ_LIST_RUN_STOP); |
| 561 | ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT, |
| 562 | REG_UTP_TRANSFER_REQ_LIST_RUN_STOP); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 566 | * ufshcd_hba_start - Start controller initialization sequence |
| 567 | * @hba: per adapter instance |
| 568 | */ |
| 569 | static inline void ufshcd_hba_start(struct ufs_hba *hba) |
| 570 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 571 | ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /** |
| 575 | * ufshcd_is_hba_active - Get controller state |
| 576 | * @hba: per adapter instance |
| 577 | * |
| 578 | * Returns zero if controller is active, 1 otherwise |
| 579 | */ |
| 580 | static inline int ufshcd_is_hba_active(struct ufs_hba *hba) |
| 581 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 582 | return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 583 | } |
| 584 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 585 | u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba) |
| 586 | { |
| 587 | /* HCI version 1.0 and 1.1 supports UniPro 1.41 */ |
| 588 | if ((hba->ufs_version == UFSHCI_VERSION_10) || |
| 589 | (hba->ufs_version == UFSHCI_VERSION_11)) |
| 590 | return UFS_UNIPRO_VER_1_41; |
| 591 | else |
| 592 | return UFS_UNIPRO_VER_1_6; |
| 593 | } |
| 594 | EXPORT_SYMBOL(ufshcd_get_local_unipro_ver); |
| 595 | |
| 596 | static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba) |
| 597 | { |
| 598 | /* |
| 599 | * If both host and device support UniPro ver1.6 or later, PA layer |
| 600 | * parameters tuning happens during link startup itself. |
| 601 | * |
| 602 | * We can manually tune PA layer parameters if either host or device |
| 603 | * doesn't support UniPro ver 1.6 or later. But to keep manual tuning |
| 604 | * logic simple, we will only do manual tuning if local unipro version |
| 605 | * doesn't support ver1.6 or later. |
| 606 | */ |
| 607 | if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6) |
| 608 | return true; |
| 609 | else |
| 610 | return false; |
| 611 | } |
| 612 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 613 | static void ufshcd_ungate_work(struct work_struct *work) |
| 614 | { |
| 615 | int ret; |
| 616 | unsigned long flags; |
| 617 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 618 | clk_gating.ungate_work); |
| 619 | |
| 620 | cancel_delayed_work_sync(&hba->clk_gating.gate_work); |
| 621 | |
| 622 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 623 | if (hba->clk_gating.state == CLKS_ON) { |
| 624 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 625 | goto unblock_reqs; |
| 626 | } |
| 627 | |
| 628 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 629 | ufshcd_setup_clocks(hba, true); |
| 630 | |
| 631 | /* Exit from hibern8 */ |
| 632 | if (ufshcd_can_hibern8_during_gating(hba)) { |
| 633 | /* Prevent gating in this path */ |
| 634 | hba->clk_gating.is_suspended = true; |
| 635 | if (ufshcd_is_link_hibern8(hba)) { |
| 636 | ret = ufshcd_uic_hibern8_exit(hba); |
| 637 | if (ret) |
| 638 | dev_err(hba->dev, "%s: hibern8 exit failed %d\n", |
| 639 | __func__, ret); |
| 640 | else |
| 641 | ufshcd_set_link_active(hba); |
| 642 | } |
| 643 | hba->clk_gating.is_suspended = false; |
| 644 | } |
| 645 | unblock_reqs: |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 646 | if (ufshcd_is_clkscaling_enabled(hba)) |
| 647 | devfreq_resume_device(hba->devfreq); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 648 | scsi_unblock_requests(hba->host); |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release. |
| 653 | * Also, exit from hibern8 mode and set the link as active. |
| 654 | * @hba: per adapter instance |
| 655 | * @async: This indicates whether caller should ungate clocks asynchronously. |
| 656 | */ |
| 657 | int ufshcd_hold(struct ufs_hba *hba, bool async) |
| 658 | { |
| 659 | int rc = 0; |
| 660 | unsigned long flags; |
| 661 | |
| 662 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 663 | goto out; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 664 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 665 | hba->clk_gating.active_reqs++; |
| 666 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 667 | if (ufshcd_eh_in_progress(hba)) { |
| 668 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 669 | return 0; |
| 670 | } |
| 671 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 672 | start: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 673 | switch (hba->clk_gating.state) { |
| 674 | case CLKS_ON: |
| 675 | break; |
| 676 | case REQ_CLKS_OFF: |
| 677 | if (cancel_delayed_work(&hba->clk_gating.gate_work)) { |
| 678 | hba->clk_gating.state = CLKS_ON; |
| 679 | break; |
| 680 | } |
| 681 | /* |
| 682 | * If we here, it means gating work is either done or |
| 683 | * currently running. Hence, fall through to cancel gating |
| 684 | * work and to enable clocks. |
| 685 | */ |
| 686 | case CLKS_OFF: |
| 687 | scsi_block_requests(hba->host); |
| 688 | hba->clk_gating.state = REQ_CLKS_ON; |
| 689 | schedule_work(&hba->clk_gating.ungate_work); |
| 690 | /* |
| 691 | * fall through to check if we should wait for this |
| 692 | * work to be done or not. |
| 693 | */ |
| 694 | case REQ_CLKS_ON: |
| 695 | if (async) { |
| 696 | rc = -EAGAIN; |
| 697 | hba->clk_gating.active_reqs--; |
| 698 | break; |
| 699 | } |
| 700 | |
| 701 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 702 | flush_work(&hba->clk_gating.ungate_work); |
| 703 | /* Make sure state is CLKS_ON before returning */ |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 704 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 705 | goto start; |
| 706 | default: |
| 707 | dev_err(hba->dev, "%s: clk gating is in invalid state %d\n", |
| 708 | __func__, hba->clk_gating.state); |
| 709 | break; |
| 710 | } |
| 711 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 712 | out: |
| 713 | return rc; |
| 714 | } |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 715 | EXPORT_SYMBOL_GPL(ufshcd_hold); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 716 | |
| 717 | static void ufshcd_gate_work(struct work_struct *work) |
| 718 | { |
| 719 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 720 | clk_gating.gate_work.work); |
| 721 | unsigned long flags; |
| 722 | |
| 723 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 724 | if (hba->clk_gating.is_suspended) { |
| 725 | hba->clk_gating.state = CLKS_ON; |
| 726 | goto rel_lock; |
| 727 | } |
| 728 | |
| 729 | if (hba->clk_gating.active_reqs |
| 730 | || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL |
| 731 | || hba->lrb_in_use || hba->outstanding_tasks |
| 732 | || hba->active_uic_cmd || hba->uic_async_done) |
| 733 | goto rel_lock; |
| 734 | |
| 735 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 736 | |
| 737 | /* put the link into hibern8 mode before turning off clocks */ |
| 738 | if (ufshcd_can_hibern8_during_gating(hba)) { |
| 739 | if (ufshcd_uic_hibern8_enter(hba)) { |
| 740 | hba->clk_gating.state = CLKS_ON; |
| 741 | goto out; |
| 742 | } |
| 743 | ufshcd_set_link_hibern8(hba); |
| 744 | } |
| 745 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 746 | if (ufshcd_is_clkscaling_enabled(hba)) { |
| 747 | devfreq_suspend_device(hba->devfreq); |
| 748 | hba->clk_scaling.window_start_t = 0; |
| 749 | } |
| 750 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 751 | if (!ufshcd_is_link_active(hba)) |
| 752 | ufshcd_setup_clocks(hba, false); |
| 753 | else |
| 754 | /* If link is active, device ref_clk can't be switched off */ |
| 755 | __ufshcd_setup_clocks(hba, false, true); |
| 756 | |
| 757 | /* |
| 758 | * In case you are here to cancel this work the gating state |
| 759 | * would be marked as REQ_CLKS_ON. In this case keep the state |
| 760 | * as REQ_CLKS_ON which would anyway imply that clocks are off |
| 761 | * and a request to turn them on is pending. By doing this way, |
| 762 | * we keep the state machine in tact and this would ultimately |
| 763 | * prevent from doing cancel work multiple times when there are |
| 764 | * new requests arriving before the current cancel work is done. |
| 765 | */ |
| 766 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 767 | if (hba->clk_gating.state == REQ_CLKS_OFF) |
| 768 | hba->clk_gating.state = CLKS_OFF; |
| 769 | |
| 770 | rel_lock: |
| 771 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 772 | out: |
| 773 | return; |
| 774 | } |
| 775 | |
| 776 | /* host lock must be held before calling this variant */ |
| 777 | static void __ufshcd_release(struct ufs_hba *hba) |
| 778 | { |
| 779 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 780 | return; |
| 781 | |
| 782 | hba->clk_gating.active_reqs--; |
| 783 | |
| 784 | if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended |
| 785 | || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL |
| 786 | || hba->lrb_in_use || hba->outstanding_tasks |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 787 | || hba->active_uic_cmd || hba->uic_async_done |
| 788 | || ufshcd_eh_in_progress(hba)) |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 789 | return; |
| 790 | |
| 791 | hba->clk_gating.state = REQ_CLKS_OFF; |
| 792 | schedule_delayed_work(&hba->clk_gating.gate_work, |
| 793 | msecs_to_jiffies(hba->clk_gating.delay_ms)); |
| 794 | } |
| 795 | |
| 796 | void ufshcd_release(struct ufs_hba *hba) |
| 797 | { |
| 798 | unsigned long flags; |
| 799 | |
| 800 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 801 | __ufshcd_release(hba); |
| 802 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 803 | } |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 804 | EXPORT_SYMBOL_GPL(ufshcd_release); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 805 | |
| 806 | static ssize_t ufshcd_clkgate_delay_show(struct device *dev, |
| 807 | struct device_attribute *attr, char *buf) |
| 808 | { |
| 809 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 810 | |
| 811 | return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms); |
| 812 | } |
| 813 | |
| 814 | static ssize_t ufshcd_clkgate_delay_store(struct device *dev, |
| 815 | struct device_attribute *attr, const char *buf, size_t count) |
| 816 | { |
| 817 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 818 | unsigned long flags, value; |
| 819 | |
| 820 | if (kstrtoul(buf, 0, &value)) |
| 821 | return -EINVAL; |
| 822 | |
| 823 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 824 | hba->clk_gating.delay_ms = value; |
| 825 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 826 | return count; |
| 827 | } |
| 828 | |
| 829 | static void ufshcd_init_clk_gating(struct ufs_hba *hba) |
| 830 | { |
| 831 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 832 | return; |
| 833 | |
| 834 | hba->clk_gating.delay_ms = 150; |
| 835 | INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work); |
| 836 | INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work); |
| 837 | |
| 838 | hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show; |
| 839 | hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store; |
| 840 | sysfs_attr_init(&hba->clk_gating.delay_attr.attr); |
| 841 | hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms"; |
| 842 | hba->clk_gating.delay_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 843 | if (device_create_file(hba->dev, &hba->clk_gating.delay_attr)) |
| 844 | dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n"); |
| 845 | } |
| 846 | |
| 847 | static void ufshcd_exit_clk_gating(struct ufs_hba *hba) |
| 848 | { |
| 849 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 850 | return; |
| 851 | device_remove_file(hba->dev, &hba->clk_gating.delay_attr); |
Akinobu Mita | 97cd680 | 2014-11-24 14:24:18 +0900 | [diff] [blame] | 852 | cancel_work_sync(&hba->clk_gating.ungate_work); |
| 853 | cancel_delayed_work_sync(&hba->clk_gating.gate_work); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 854 | } |
| 855 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 856 | /* Must be called with host lock acquired */ |
| 857 | static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba) |
| 858 | { |
| 859 | if (!ufshcd_is_clkscaling_enabled(hba)) |
| 860 | return; |
| 861 | |
| 862 | if (!hba->clk_scaling.is_busy_started) { |
| 863 | hba->clk_scaling.busy_start_t = ktime_get(); |
| 864 | hba->clk_scaling.is_busy_started = true; |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba) |
| 869 | { |
| 870 | struct ufs_clk_scaling *scaling = &hba->clk_scaling; |
| 871 | |
| 872 | if (!ufshcd_is_clkscaling_enabled(hba)) |
| 873 | return; |
| 874 | |
| 875 | if (!hba->outstanding_reqs && scaling->is_busy_started) { |
| 876 | scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(), |
| 877 | scaling->busy_start_t)); |
| 878 | scaling->busy_start_t = ktime_set(0, 0); |
| 879 | scaling->is_busy_started = false; |
| 880 | } |
| 881 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 882 | /** |
| 883 | * ufshcd_send_command - Send SCSI or device management commands |
| 884 | * @hba: per adapter instance |
| 885 | * @task_tag: Task tag of the command |
| 886 | */ |
| 887 | static inline |
| 888 | void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag) |
| 889 | { |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 890 | ufshcd_clk_scaling_start_busy(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 891 | __set_bit(task_tag, &hba->outstanding_reqs); |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 892 | ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | /** |
| 896 | * ufshcd_copy_sense_data - Copy sense data in case of check condition |
| 897 | * @lrb - pointer to local reference block |
| 898 | */ |
| 899 | static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp) |
| 900 | { |
| 901 | int len; |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 902 | if (lrbp->sense_buffer && |
| 903 | ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) { |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 904 | len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 905 | memcpy(lrbp->sense_buffer, |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 906 | lrbp->ucd_rsp_ptr->sr.sense_data, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 907 | min_t(int, len, SCSI_SENSE_BUFFERSIZE)); |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 912 | * ufshcd_copy_query_response() - Copy the Query Response and the data |
| 913 | * descriptor |
| 914 | * @hba: per adapter instance |
| 915 | * @lrb - pointer to local reference block |
| 916 | */ |
| 917 | static |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 918 | 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] | 919 | { |
| 920 | struct ufs_query_res *query_res = &hba->dev_cmd.query.response; |
| 921 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 922 | 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] | 923 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 924 | /* Get the descriptor */ |
| 925 | if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 926 | u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 927 | GENERAL_UPIU_REQUEST_SIZE; |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 928 | u16 resp_len; |
| 929 | u16 buf_len; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 930 | |
| 931 | /* data segment length */ |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 932 | resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) & |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 933 | MASK_QUERY_DATA_SEG_LEN; |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 934 | buf_len = be16_to_cpu( |
| 935 | hba->dev_cmd.query.request.upiu_req.length); |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 936 | if (likely(buf_len >= resp_len)) { |
| 937 | memcpy(hba->dev_cmd.query.descriptor, descp, resp_len); |
| 938 | } else { |
| 939 | dev_warn(hba->dev, |
| 940 | "%s: Response size is bigger than buffer", |
| 941 | __func__); |
| 942 | return -EINVAL; |
| 943 | } |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 944 | } |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 945 | |
| 946 | return 0; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 950 | * ufshcd_hba_capabilities - Read controller capabilities |
| 951 | * @hba: per adapter instance |
| 952 | */ |
| 953 | static inline void ufshcd_hba_capabilities(struct ufs_hba *hba) |
| 954 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 955 | hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 956 | |
| 957 | /* nutrs and nutmrs are 0 based values */ |
| 958 | hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1; |
| 959 | hba->nutmrs = |
| 960 | ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1; |
| 961 | } |
| 962 | |
| 963 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 964 | * ufshcd_ready_for_uic_cmd - Check if controller is ready |
| 965 | * to accept UIC commands |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 966 | * @hba: per adapter instance |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 967 | * Return true on success, else false |
| 968 | */ |
| 969 | static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba) |
| 970 | { |
| 971 | if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY) |
| 972 | return true; |
| 973 | else |
| 974 | return false; |
| 975 | } |
| 976 | |
| 977 | /** |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 978 | * ufshcd_get_upmcrs - Get the power mode change request status |
| 979 | * @hba: Pointer to adapter instance |
| 980 | * |
| 981 | * This function gets the UPMCRS field of HCS register |
| 982 | * Returns value of UPMCRS field |
| 983 | */ |
| 984 | static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba) |
| 985 | { |
| 986 | return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7; |
| 987 | } |
| 988 | |
| 989 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 990 | * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers |
| 991 | * @hba: per adapter instance |
| 992 | * @uic_cmd: UIC command |
| 993 | * |
| 994 | * Mutex must be held. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 995 | */ |
| 996 | static inline void |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 997 | 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] | 998 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 999 | WARN_ON(hba->active_uic_cmd); |
| 1000 | |
| 1001 | hba->active_uic_cmd = uic_cmd; |
| 1002 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1003 | /* Write Args */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1004 | ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1); |
| 1005 | ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2); |
| 1006 | ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1007 | |
| 1008 | /* Write UIC Cmd */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1009 | ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK, |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 1010 | REG_UIC_COMMAND); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1014 | * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command |
| 1015 | * @hba: per adapter instance |
| 1016 | * @uic_command: UIC command |
| 1017 | * |
| 1018 | * Must be called with mutex held. |
| 1019 | * Returns 0 only if success. |
| 1020 | */ |
| 1021 | static int |
| 1022 | ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) |
| 1023 | { |
| 1024 | int ret; |
| 1025 | unsigned long flags; |
| 1026 | |
| 1027 | if (wait_for_completion_timeout(&uic_cmd->done, |
| 1028 | msecs_to_jiffies(UIC_CMD_TIMEOUT))) |
| 1029 | ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT; |
| 1030 | else |
| 1031 | ret = -ETIMEDOUT; |
| 1032 | |
| 1033 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1034 | hba->active_uic_cmd = NULL; |
| 1035 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1036 | |
| 1037 | return ret; |
| 1038 | } |
| 1039 | |
| 1040 | /** |
| 1041 | * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result |
| 1042 | * @hba: per adapter instance |
| 1043 | * @uic_cmd: UIC command |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 1044 | * @completion: initialize the completion only if this is set to true |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1045 | * |
| 1046 | * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 1047 | * with mutex held and host_lock locked. |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1048 | * Returns 0 only if success. |
| 1049 | */ |
| 1050 | static int |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 1051 | __ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd, |
| 1052 | bool completion) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1053 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1054 | if (!ufshcd_ready_for_uic_cmd(hba)) { |
| 1055 | dev_err(hba->dev, |
| 1056 | "Controller not ready to accept UIC commands\n"); |
| 1057 | return -EIO; |
| 1058 | } |
| 1059 | |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 1060 | if (completion) |
| 1061 | init_completion(&uic_cmd->done); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1062 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1063 | ufshcd_dispatch_uic_cmd(hba, uic_cmd); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1064 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 1065 | return 0; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | /** |
| 1069 | * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result |
| 1070 | * @hba: per adapter instance |
| 1071 | * @uic_cmd: UIC command |
| 1072 | * |
| 1073 | * Returns 0 only if success. |
| 1074 | */ |
| 1075 | static int |
| 1076 | ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) |
| 1077 | { |
| 1078 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 1079 | unsigned long flags; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1080 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1081 | ufshcd_hold(hba, false); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1082 | mutex_lock(&hba->uic_cmd_mutex); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 1083 | ufshcd_add_delay_before_dme_cmd(hba); |
| 1084 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 1085 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 1086 | ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 1087 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1088 | if (!ret) |
| 1089 | ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd); |
| 1090 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1091 | mutex_unlock(&hba->uic_cmd_mutex); |
| 1092 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1093 | ufshcd_release(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1094 | return ret; |
| 1095 | } |
| 1096 | |
| 1097 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1098 | * ufshcd_map_sg - Map scatter-gather list to prdt |
| 1099 | * @lrbp - pointer to local reference block |
| 1100 | * |
| 1101 | * Returns 0 in case of success, non-zero value in case of failure |
| 1102 | */ |
Kiwoong Kim | 9b41ed7 | 2017-04-04 19:32:05 +0000 | [diff] [blame] | 1103 | 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] | 1104 | { |
| 1105 | struct ufshcd_sg_entry *prd_table; |
| 1106 | struct scatterlist *sg; |
| 1107 | struct scsi_cmnd *cmd; |
| 1108 | int sg_segments; |
| 1109 | int i; |
| 1110 | |
| 1111 | cmd = lrbp->cmd; |
| 1112 | sg_segments = scsi_dma_map(cmd); |
| 1113 | if (sg_segments < 0) |
| 1114 | return sg_segments; |
| 1115 | |
| 1116 | if (sg_segments) { |
Kiwoong Kim | 9b41ed7 | 2017-04-04 19:32:05 +0000 | [diff] [blame] | 1117 | if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) |
| 1118 | lrbp->utr_descriptor_ptr->prd_table_length = |
| 1119 | cpu_to_le16((u16)(sg_segments * |
| 1120 | sizeof(struct ufshcd_sg_entry))); |
| 1121 | else |
| 1122 | lrbp->utr_descriptor_ptr->prd_table_length = |
| 1123 | cpu_to_le16((u16) (sg_segments)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1124 | |
| 1125 | prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr; |
| 1126 | |
| 1127 | scsi_for_each_sg(cmd, sg, sg_segments, i) { |
| 1128 | prd_table[i].size = |
| 1129 | cpu_to_le32(((u32) sg_dma_len(sg))-1); |
| 1130 | prd_table[i].base_addr = |
| 1131 | cpu_to_le32(lower_32_bits(sg->dma_address)); |
| 1132 | prd_table[i].upper_addr = |
| 1133 | cpu_to_le32(upper_32_bits(sg->dma_address)); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 1134 | prd_table[i].reserved = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1135 | } |
| 1136 | } else { |
| 1137 | lrbp->utr_descriptor_ptr->prd_table_length = 0; |
| 1138 | } |
| 1139 | |
| 1140 | return 0; |
| 1141 | } |
| 1142 | |
| 1143 | /** |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 1144 | * ufshcd_enable_intr - enable interrupts |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1145 | * @hba: per adapter instance |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 1146 | * @intrs: interrupt bits |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1147 | */ |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 1148 | static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1149 | { |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 1150 | u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
| 1151 | |
| 1152 | if (hba->ufs_version == UFSHCI_VERSION_10) { |
| 1153 | u32 rw; |
| 1154 | rw = set & INTERRUPT_MASK_RW_VER_10; |
| 1155 | set = rw | ((set ^ intrs) & intrs); |
| 1156 | } else { |
| 1157 | set |= intrs; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1158 | } |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 1159 | |
| 1160 | ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE); |
| 1161 | } |
| 1162 | |
| 1163 | /** |
| 1164 | * ufshcd_disable_intr - disable interrupts |
| 1165 | * @hba: per adapter instance |
| 1166 | * @intrs: interrupt bits |
| 1167 | */ |
| 1168 | static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs) |
| 1169 | { |
| 1170 | u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
| 1171 | |
| 1172 | if (hba->ufs_version == UFSHCI_VERSION_10) { |
| 1173 | u32 rw; |
| 1174 | rw = (set & INTERRUPT_MASK_RW_VER_10) & |
| 1175 | ~(intrs & INTERRUPT_MASK_RW_VER_10); |
| 1176 | set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10); |
| 1177 | |
| 1178 | } else { |
| 1179 | set &= ~intrs; |
| 1180 | } |
| 1181 | |
| 1182 | ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1186 | * ufshcd_prepare_req_desc_hdr() - Fills the requests header |
| 1187 | * descriptor according to request |
| 1188 | * @lrbp: pointer to local reference block |
| 1189 | * @upiu_flags: flags required in the header |
| 1190 | * @cmd_dir: requests data direction |
| 1191 | */ |
| 1192 | static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp, |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 1193 | u32 *upiu_flags, enum dma_data_direction cmd_dir) |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1194 | { |
| 1195 | struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr; |
| 1196 | u32 data_direction; |
| 1197 | u32 dword_0; |
| 1198 | |
| 1199 | if (cmd_dir == DMA_FROM_DEVICE) { |
| 1200 | data_direction = UTP_DEVICE_TO_HOST; |
| 1201 | *upiu_flags = UPIU_CMD_FLAGS_READ; |
| 1202 | } else if (cmd_dir == DMA_TO_DEVICE) { |
| 1203 | data_direction = UTP_HOST_TO_DEVICE; |
| 1204 | *upiu_flags = UPIU_CMD_FLAGS_WRITE; |
| 1205 | } else { |
| 1206 | data_direction = UTP_NO_DATA_TRANSFER; |
| 1207 | *upiu_flags = UPIU_CMD_FLAGS_NONE; |
| 1208 | } |
| 1209 | |
| 1210 | dword_0 = data_direction | (lrbp->command_type |
| 1211 | << UPIU_COMMAND_TYPE_OFFSET); |
| 1212 | if (lrbp->intr_cmd) |
| 1213 | dword_0 |= UTP_REQ_DESC_INT_CMD; |
| 1214 | |
| 1215 | /* Transfer request descriptor header fields */ |
| 1216 | req_desc->header.dword_0 = cpu_to_le32(dword_0); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 1217 | /* dword_1 is reserved, hence it is set to 0 */ |
| 1218 | req_desc->header.dword_1 = 0; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1219 | /* |
| 1220 | * assigning invalid value for command status. Controller |
| 1221 | * updates OCS on command completion, with the command |
| 1222 | * status |
| 1223 | */ |
| 1224 | req_desc->header.dword_2 = |
| 1225 | cpu_to_le32(OCS_INVALID_COMMAND_STATUS); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 1226 | /* dword_3 is reserved, hence it is set to 0 */ |
| 1227 | req_desc->header.dword_3 = 0; |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 1228 | |
| 1229 | req_desc->prd_table_length = 0; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | /** |
| 1233 | * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc, |
| 1234 | * for scsi commands |
| 1235 | * @lrbp - local reference block pointer |
| 1236 | * @upiu_flags - flags |
| 1237 | */ |
| 1238 | static |
| 1239 | void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags) |
| 1240 | { |
| 1241 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 1242 | unsigned short cdb_len; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1243 | |
| 1244 | /* command descriptor fields */ |
| 1245 | ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD( |
| 1246 | UPIU_TRANSACTION_COMMAND, upiu_flags, |
| 1247 | lrbp->lun, lrbp->task_tag); |
| 1248 | ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD( |
| 1249 | UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0); |
| 1250 | |
| 1251 | /* Total EHS length and Data segment length will be zero */ |
| 1252 | ucd_req_ptr->header.dword_2 = 0; |
| 1253 | |
| 1254 | ucd_req_ptr->sc.exp_data_transfer_len = |
| 1255 | cpu_to_be32(lrbp->cmd->sdb.length); |
| 1256 | |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 1257 | cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE); |
| 1258 | memset(ucd_req_ptr->sc.cdb, 0, MAX_CDB_SIZE); |
| 1259 | memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len); |
| 1260 | |
| 1261 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1262 | } |
| 1263 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1264 | /** |
| 1265 | * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc, |
| 1266 | * for query requsts |
| 1267 | * @hba: UFS hba |
| 1268 | * @lrbp: local reference block pointer |
| 1269 | * @upiu_flags: flags |
| 1270 | */ |
| 1271 | static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba, |
| 1272 | struct ufshcd_lrb *lrbp, u32 upiu_flags) |
| 1273 | { |
| 1274 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
| 1275 | struct ufs_query *query = &hba->dev_cmd.query; |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 1276 | u16 len = be16_to_cpu(query->request.upiu_req.length); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1277 | u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE; |
| 1278 | |
| 1279 | /* Query request header */ |
| 1280 | ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD( |
| 1281 | UPIU_TRANSACTION_QUERY_REQ, upiu_flags, |
| 1282 | lrbp->lun, lrbp->task_tag); |
| 1283 | ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD( |
| 1284 | 0, query->request.query_func, 0, 0); |
| 1285 | |
Zang Leigang | 6861285 | 2016-08-25 17:39:19 +0800 | [diff] [blame] | 1286 | /* Data segment length only need for WRITE_DESC */ |
| 1287 | if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) |
| 1288 | ucd_req_ptr->header.dword_2 = |
| 1289 | UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len); |
| 1290 | else |
| 1291 | ucd_req_ptr->header.dword_2 = 0; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1292 | |
| 1293 | /* Copy the Query Request buffer as is */ |
| 1294 | memcpy(&ucd_req_ptr->qr, &query->request.upiu_req, |
| 1295 | QUERY_OSF_SIZE); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1296 | |
| 1297 | /* Copy the Descriptor */ |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1298 | if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) |
| 1299 | memcpy(descp, query->descriptor, len); |
| 1300 | |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 1301 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1302 | } |
| 1303 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1304 | static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp) |
| 1305 | { |
| 1306 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
| 1307 | |
| 1308 | memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req)); |
| 1309 | |
| 1310 | /* command descriptor fields */ |
| 1311 | ucd_req_ptr->header.dword_0 = |
| 1312 | UPIU_HEADER_DWORD( |
| 1313 | UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag); |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 1314 | /* clear rest of the fields of basic header */ |
| 1315 | ucd_req_ptr->header.dword_1 = 0; |
| 1316 | ucd_req_ptr->header.dword_2 = 0; |
| 1317 | |
| 1318 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | /** |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 1322 | * ufshcd_comp_devman_upiu - UFS Protocol Information Unit(UPIU) |
| 1323 | * for Device Management Purposes |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1324 | * @hba - per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1325 | * @lrb - pointer to local reference block |
| 1326 | */ |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 1327 | 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] | 1328 | { |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1329 | u32 upiu_flags; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1330 | int ret = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1331 | |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 1332 | if (hba->ufs_version == UFSHCI_VERSION_20) |
| 1333 | lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE; |
| 1334 | else |
| 1335 | lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE; |
| 1336 | |
| 1337 | ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE); |
| 1338 | if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY) |
| 1339 | ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags); |
| 1340 | else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP) |
| 1341 | ufshcd_prepare_utp_nop_upiu(lrbp); |
| 1342 | else |
| 1343 | ret = -EINVAL; |
| 1344 | |
| 1345 | return ret; |
| 1346 | } |
| 1347 | |
| 1348 | /** |
| 1349 | * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU) |
| 1350 | * for SCSI Purposes |
| 1351 | * @hba - per adapter instance |
| 1352 | * @lrb - pointer to local reference block |
| 1353 | */ |
| 1354 | static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 1355 | { |
| 1356 | u32 upiu_flags; |
| 1357 | int ret = 0; |
| 1358 | |
| 1359 | if (hba->ufs_version == UFSHCI_VERSION_20) |
| 1360 | lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE; |
| 1361 | else |
| 1362 | lrbp->command_type = UTP_CMD_TYPE_SCSI; |
| 1363 | |
| 1364 | if (likely(lrbp->cmd)) { |
| 1365 | ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, |
| 1366 | lrbp->cmd->sc_data_direction); |
| 1367 | ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags); |
| 1368 | } else { |
| 1369 | ret = -EINVAL; |
| 1370 | } |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1371 | |
| 1372 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1373 | } |
| 1374 | |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 1375 | /* |
| 1376 | * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN |
| 1377 | * @scsi_lun: scsi LUN id |
| 1378 | * |
| 1379 | * Returns UPIU LUN id |
| 1380 | */ |
| 1381 | static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun) |
| 1382 | { |
| 1383 | if (scsi_is_wlun(scsi_lun)) |
| 1384 | return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID) |
| 1385 | | UFS_UPIU_WLUN_ID; |
| 1386 | else |
| 1387 | return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID; |
| 1388 | } |
| 1389 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1390 | /** |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 1391 | * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID |
| 1392 | * @scsi_lun: UPIU W-LUN id |
| 1393 | * |
| 1394 | * Returns SCSI W-LUN id |
| 1395 | */ |
| 1396 | static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id) |
| 1397 | { |
| 1398 | return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE; |
| 1399 | } |
| 1400 | |
| 1401 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1402 | * ufshcd_queuecommand - main entry point for SCSI requests |
| 1403 | * @cmd: command from SCSI Midlayer |
| 1404 | * @done: call back function |
| 1405 | * |
| 1406 | * Returns 0 for success, non-zero in case of failure |
| 1407 | */ |
| 1408 | static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) |
| 1409 | { |
| 1410 | struct ufshcd_lrb *lrbp; |
| 1411 | struct ufs_hba *hba; |
| 1412 | unsigned long flags; |
| 1413 | int tag; |
| 1414 | int err = 0; |
| 1415 | |
| 1416 | hba = shost_priv(host); |
| 1417 | |
| 1418 | tag = cmd->request->tag; |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 1419 | if (!ufshcd_valid_tag(hba, tag)) { |
| 1420 | dev_err(hba->dev, |
| 1421 | "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p", |
| 1422 | __func__, tag, cmd, cmd->request); |
| 1423 | BUG(); |
| 1424 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1425 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 1426 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1427 | switch (hba->ufshcd_state) { |
| 1428 | case UFSHCD_STATE_OPERATIONAL: |
| 1429 | break; |
Zang Leigang | a17bddc | 2017-04-04 19:32:20 +0000 | [diff] [blame] | 1430 | case UFSHCD_STATE_EH_SCHEDULED: |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 1431 | case UFSHCD_STATE_RESET: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1432 | err = SCSI_MLQUEUE_HOST_BUSY; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 1433 | goto out_unlock; |
| 1434 | case UFSHCD_STATE_ERROR: |
| 1435 | set_host_byte(cmd, DID_ERROR); |
| 1436 | cmd->scsi_done(cmd); |
| 1437 | goto out_unlock; |
| 1438 | default: |
| 1439 | dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n", |
| 1440 | __func__, hba->ufshcd_state); |
| 1441 | set_host_byte(cmd, DID_BAD_TARGET); |
| 1442 | cmd->scsi_done(cmd); |
| 1443 | goto out_unlock; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1444 | } |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 1445 | |
| 1446 | /* if error handling is in progress, don't issue commands */ |
| 1447 | if (ufshcd_eh_in_progress(hba)) { |
| 1448 | set_host_byte(cmd, DID_ERROR); |
| 1449 | cmd->scsi_done(cmd); |
| 1450 | goto out_unlock; |
| 1451 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 1452 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1453 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1454 | /* acquire the tag to make sure device cmds don't use it */ |
| 1455 | if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) { |
| 1456 | /* |
| 1457 | * Dev manage command in progress, requeue the command. |
| 1458 | * Requeuing the command helps in cases where the request *may* |
| 1459 | * find different tag instead of waiting for dev manage command |
| 1460 | * completion. |
| 1461 | */ |
| 1462 | err = SCSI_MLQUEUE_HOST_BUSY; |
| 1463 | goto out; |
| 1464 | } |
| 1465 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1466 | err = ufshcd_hold(hba, true); |
| 1467 | if (err) { |
| 1468 | err = SCSI_MLQUEUE_HOST_BUSY; |
| 1469 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 1470 | goto out; |
| 1471 | } |
| 1472 | WARN_ON(hba->clk_gating.state != CLKS_ON); |
| 1473 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1474 | lrbp = &hba->lrb[tag]; |
| 1475 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1476 | WARN_ON(lrbp->cmd); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1477 | lrbp->cmd = cmd; |
| 1478 | lrbp->sense_bufflen = SCSI_SENSE_BUFFERSIZE; |
| 1479 | lrbp->sense_buffer = cmd->sense_buffer; |
| 1480 | lrbp->task_tag = tag; |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 1481 | lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun); |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 1482 | lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1483 | |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 1484 | ufshcd_comp_scsi_upiu(hba, lrbp); |
| 1485 | |
Kiwoong Kim | 9b41ed7 | 2017-04-04 19:32:05 +0000 | [diff] [blame] | 1486 | err = ufshcd_map_sg(hba, lrbp); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1487 | if (err) { |
| 1488 | lrbp->cmd = NULL; |
| 1489 | clear_bit_unlock(tag, &hba->lrb_in_use); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1490 | goto out; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1491 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1492 | |
| 1493 | /* issue command to the controller */ |
| 1494 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1495 | ufshcd_send_command(hba, tag); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 1496 | out_unlock: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1497 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1498 | out: |
| 1499 | return err; |
| 1500 | } |
| 1501 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1502 | static int ufshcd_compose_dev_cmd(struct ufs_hba *hba, |
| 1503 | struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag) |
| 1504 | { |
| 1505 | lrbp->cmd = NULL; |
| 1506 | lrbp->sense_bufflen = 0; |
| 1507 | lrbp->sense_buffer = NULL; |
| 1508 | lrbp->task_tag = tag; |
| 1509 | 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] | 1510 | lrbp->intr_cmd = true; /* No interrupt aggregation */ |
| 1511 | hba->dev_cmd.type = cmd_type; |
| 1512 | |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 1513 | return ufshcd_comp_devman_upiu(hba, lrbp); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | static int |
| 1517 | ufshcd_clear_cmd(struct ufs_hba *hba, int tag) |
| 1518 | { |
| 1519 | int err = 0; |
| 1520 | unsigned long flags; |
| 1521 | u32 mask = 1 << tag; |
| 1522 | |
| 1523 | /* clear outstanding transaction before retry */ |
| 1524 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1525 | ufshcd_utrl_clear(hba, tag); |
| 1526 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1527 | |
| 1528 | /* |
| 1529 | * wait for for h/w to clear corresponding bit in door-bell. |
| 1530 | * max. wait is 1 sec. |
| 1531 | */ |
| 1532 | err = ufshcd_wait_for_register(hba, |
| 1533 | REG_UTP_TRANSFER_REQ_DOOR_BELL, |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 1534 | mask, ~mask, 1000, 1000, true); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1535 | |
| 1536 | return err; |
| 1537 | } |
| 1538 | |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1539 | static int |
| 1540 | ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 1541 | { |
| 1542 | struct ufs_query_res *query_res = &hba->dev_cmd.query.response; |
| 1543 | |
| 1544 | /* Get the UPIU response */ |
| 1545 | query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >> |
| 1546 | UPIU_RSP_CODE_OFFSET; |
| 1547 | return query_res->response; |
| 1548 | } |
| 1549 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1550 | /** |
| 1551 | * ufshcd_dev_cmd_completion() - handles device management command responses |
| 1552 | * @hba: per adapter instance |
| 1553 | * @lrbp: pointer to local reference block |
| 1554 | */ |
| 1555 | static int |
| 1556 | ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 1557 | { |
| 1558 | int resp; |
| 1559 | int err = 0; |
| 1560 | |
| 1561 | resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr); |
| 1562 | |
| 1563 | switch (resp) { |
| 1564 | case UPIU_TRANSACTION_NOP_IN: |
| 1565 | if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) { |
| 1566 | err = -EINVAL; |
| 1567 | dev_err(hba->dev, "%s: unexpected response %x\n", |
| 1568 | __func__, resp); |
| 1569 | } |
| 1570 | break; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1571 | case UPIU_TRANSACTION_QUERY_RSP: |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1572 | err = ufshcd_check_query_response(hba, lrbp); |
| 1573 | if (!err) |
| 1574 | err = ufshcd_copy_query_response(hba, lrbp); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1575 | break; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1576 | case UPIU_TRANSACTION_REJECT_UPIU: |
| 1577 | /* TODO: handle Reject UPIU Response */ |
| 1578 | err = -EPERM; |
| 1579 | dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n", |
| 1580 | __func__); |
| 1581 | break; |
| 1582 | default: |
| 1583 | err = -EINVAL; |
| 1584 | dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n", |
| 1585 | __func__, resp); |
| 1586 | break; |
| 1587 | } |
| 1588 | |
| 1589 | return err; |
| 1590 | } |
| 1591 | |
| 1592 | static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba, |
| 1593 | struct ufshcd_lrb *lrbp, int max_timeout) |
| 1594 | { |
| 1595 | int err = 0; |
| 1596 | unsigned long time_left; |
| 1597 | unsigned long flags; |
| 1598 | |
| 1599 | time_left = wait_for_completion_timeout(hba->dev_cmd.complete, |
| 1600 | msecs_to_jiffies(max_timeout)); |
| 1601 | |
| 1602 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1603 | hba->dev_cmd.complete = NULL; |
| 1604 | if (likely(time_left)) { |
| 1605 | err = ufshcd_get_tr_ocs(lrbp); |
| 1606 | if (!err) |
| 1607 | err = ufshcd_dev_cmd_completion(hba, lrbp); |
| 1608 | } |
| 1609 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1610 | |
| 1611 | if (!time_left) { |
| 1612 | err = -ETIMEDOUT; |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 1613 | dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n", |
| 1614 | __func__, lrbp->task_tag); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1615 | if (!ufshcd_clear_cmd(hba, lrbp->task_tag)) |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 1616 | /* successfully cleared the command, retry if needed */ |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1617 | err = -EAGAIN; |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 1618 | /* |
| 1619 | * in case of an error, after clearing the doorbell, |
| 1620 | * we also need to clear the outstanding_request |
| 1621 | * field in hba |
| 1622 | */ |
| 1623 | ufshcd_outstanding_req_clear(hba, lrbp->task_tag); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | return err; |
| 1627 | } |
| 1628 | |
| 1629 | /** |
| 1630 | * ufshcd_get_dev_cmd_tag - Get device management command tag |
| 1631 | * @hba: per-adapter instance |
| 1632 | * @tag: pointer to variable with available slot value |
| 1633 | * |
| 1634 | * Get a free slot and lock it until device management command |
| 1635 | * completes. |
| 1636 | * |
| 1637 | * Returns false if free slot is unavailable for locking, else |
| 1638 | * return true with tag value in @tag. |
| 1639 | */ |
| 1640 | static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out) |
| 1641 | { |
| 1642 | int tag; |
| 1643 | bool ret = false; |
| 1644 | unsigned long tmp; |
| 1645 | |
| 1646 | if (!tag_out) |
| 1647 | goto out; |
| 1648 | |
| 1649 | do { |
| 1650 | tmp = ~hba->lrb_in_use; |
| 1651 | tag = find_last_bit(&tmp, hba->nutrs); |
| 1652 | if (tag >= hba->nutrs) |
| 1653 | goto out; |
| 1654 | } while (test_and_set_bit_lock(tag, &hba->lrb_in_use)); |
| 1655 | |
| 1656 | *tag_out = tag; |
| 1657 | ret = true; |
| 1658 | out: |
| 1659 | return ret; |
| 1660 | } |
| 1661 | |
| 1662 | static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag) |
| 1663 | { |
| 1664 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 1665 | } |
| 1666 | |
| 1667 | /** |
| 1668 | * ufshcd_exec_dev_cmd - API for sending device management requests |
| 1669 | * @hba - UFS hba |
| 1670 | * @cmd_type - specifies the type (NOP, Query...) |
| 1671 | * @timeout - time in seconds |
| 1672 | * |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1673 | * NOTE: Since there is only one available tag for device management commands, |
| 1674 | * it is expected you hold the hba->dev_cmd.lock mutex. |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1675 | */ |
| 1676 | static int ufshcd_exec_dev_cmd(struct ufs_hba *hba, |
| 1677 | enum dev_cmd_type cmd_type, int timeout) |
| 1678 | { |
| 1679 | struct ufshcd_lrb *lrbp; |
| 1680 | int err; |
| 1681 | int tag; |
| 1682 | struct completion wait; |
| 1683 | unsigned long flags; |
| 1684 | |
| 1685 | /* |
| 1686 | * Get free slot, sleep if slots are unavailable. |
| 1687 | * Even though we use wait_event() which sleeps indefinitely, |
| 1688 | * the maximum wait time is bounded by SCSI request timeout. |
| 1689 | */ |
| 1690 | wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag)); |
| 1691 | |
| 1692 | init_completion(&wait); |
| 1693 | lrbp = &hba->lrb[tag]; |
| 1694 | WARN_ON(lrbp->cmd); |
| 1695 | err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag); |
| 1696 | if (unlikely(err)) |
| 1697 | goto out_put_tag; |
| 1698 | |
| 1699 | hba->dev_cmd.complete = &wait; |
| 1700 | |
Yaniv Gardi | e3dfdc5 | 2016-02-01 15:02:49 +0200 | [diff] [blame] | 1701 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 1702 | wmb(); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1703 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1704 | ufshcd_send_command(hba, tag); |
| 1705 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1706 | |
| 1707 | err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout); |
| 1708 | |
| 1709 | out_put_tag: |
| 1710 | ufshcd_put_dev_cmd_tag(hba, tag); |
| 1711 | wake_up(&hba->dev_cmd.tag_wq); |
| 1712 | return err; |
| 1713 | } |
| 1714 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1715 | /** |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 1716 | * ufshcd_init_query() - init the query response and request parameters |
| 1717 | * @hba: per-adapter instance |
| 1718 | * @request: address of the request pointer to be initialized |
| 1719 | * @response: address of the response pointer to be initialized |
| 1720 | * @opcode: operation to perform |
| 1721 | * @idn: flag idn to access |
| 1722 | * @index: LU number to access |
| 1723 | * @selector: query/flag/descriptor further identification |
| 1724 | */ |
| 1725 | static inline void ufshcd_init_query(struct ufs_hba *hba, |
| 1726 | struct ufs_query_req **request, struct ufs_query_res **response, |
| 1727 | enum query_opcode opcode, u8 idn, u8 index, u8 selector) |
| 1728 | { |
| 1729 | *request = &hba->dev_cmd.query.request; |
| 1730 | *response = &hba->dev_cmd.query.response; |
| 1731 | memset(*request, 0, sizeof(struct ufs_query_req)); |
| 1732 | memset(*response, 0, sizeof(struct ufs_query_res)); |
| 1733 | (*request)->upiu_req.opcode = opcode; |
| 1734 | (*request)->upiu_req.idn = idn; |
| 1735 | (*request)->upiu_req.index = index; |
| 1736 | (*request)->upiu_req.selector = selector; |
| 1737 | } |
| 1738 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 1739 | static int ufshcd_query_flag_retry(struct ufs_hba *hba, |
| 1740 | enum query_opcode opcode, enum flag_idn idn, bool *flag_res) |
| 1741 | { |
| 1742 | int ret; |
| 1743 | int retries; |
| 1744 | |
| 1745 | for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) { |
| 1746 | ret = ufshcd_query_flag(hba, opcode, idn, flag_res); |
| 1747 | if (ret) |
| 1748 | dev_dbg(hba->dev, |
| 1749 | "%s: failed with error %d, retries %d\n", |
| 1750 | __func__, ret, retries); |
| 1751 | else |
| 1752 | break; |
| 1753 | } |
| 1754 | |
| 1755 | if (ret) |
| 1756 | dev_err(hba->dev, |
| 1757 | "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n", |
| 1758 | __func__, opcode, idn, ret, retries); |
| 1759 | return ret; |
| 1760 | } |
| 1761 | |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 1762 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1763 | * ufshcd_query_flag() - API function for sending flag query requests |
| 1764 | * hba: per-adapter instance |
| 1765 | * query_opcode: flag query to perform |
| 1766 | * idn: flag idn to access |
| 1767 | * flag_res: the flag value after the query request completes |
| 1768 | * |
| 1769 | * Returns 0 for success, non-zero in case of failure |
| 1770 | */ |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 1771 | int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode, |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1772 | enum flag_idn idn, bool *flag_res) |
| 1773 | { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 1774 | struct ufs_query_req *request = NULL; |
| 1775 | struct ufs_query_res *response = NULL; |
| 1776 | int err, index = 0, selector = 0; |
Yaniv Gardi | e5ad406 | 2016-02-01 15:02:41 +0200 | [diff] [blame] | 1777 | int timeout = QUERY_REQ_TIMEOUT; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1778 | |
| 1779 | BUG_ON(!hba); |
| 1780 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1781 | ufshcd_hold(hba, false); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1782 | mutex_lock(&hba->dev_cmd.lock); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 1783 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 1784 | selector); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1785 | |
| 1786 | switch (opcode) { |
| 1787 | case UPIU_QUERY_OPCODE_SET_FLAG: |
| 1788 | case UPIU_QUERY_OPCODE_CLEAR_FLAG: |
| 1789 | case UPIU_QUERY_OPCODE_TOGGLE_FLAG: |
| 1790 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
| 1791 | break; |
| 1792 | case UPIU_QUERY_OPCODE_READ_FLAG: |
| 1793 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 1794 | if (!flag_res) { |
| 1795 | /* No dummy reads */ |
| 1796 | dev_err(hba->dev, "%s: Invalid argument for read request\n", |
| 1797 | __func__); |
| 1798 | err = -EINVAL; |
| 1799 | goto out_unlock; |
| 1800 | } |
| 1801 | break; |
| 1802 | default: |
| 1803 | dev_err(hba->dev, |
| 1804 | "%s: Expected query flag opcode but got = %d\n", |
| 1805 | __func__, opcode); |
| 1806 | err = -EINVAL; |
| 1807 | goto out_unlock; |
| 1808 | } |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1809 | |
Yaniv Gardi | e5ad406 | 2016-02-01 15:02:41 +0200 | [diff] [blame] | 1810 | if (idn == QUERY_FLAG_IDN_FDEVICEINIT) |
| 1811 | timeout = QUERY_FDEVICEINIT_REQ_TIMEOUT; |
| 1812 | |
| 1813 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1814 | |
| 1815 | if (err) { |
| 1816 | dev_err(hba->dev, |
| 1817 | "%s: Sending flag query for idn %d failed, err = %d\n", |
| 1818 | __func__, idn, err); |
| 1819 | goto out_unlock; |
| 1820 | } |
| 1821 | |
| 1822 | if (flag_res) |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 1823 | *flag_res = (be32_to_cpu(response->upiu_res.value) & |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1824 | MASK_QUERY_UPIU_FLAG_LOC) & 0x1; |
| 1825 | |
| 1826 | out_unlock: |
| 1827 | mutex_unlock(&hba->dev_cmd.lock); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1828 | ufshcd_release(hba); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1829 | return err; |
| 1830 | } |
| 1831 | |
| 1832 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 1833 | * ufshcd_query_attr - API function for sending attribute requests |
| 1834 | * hba: per-adapter instance |
| 1835 | * opcode: attribute opcode |
| 1836 | * idn: attribute idn to access |
| 1837 | * index: index field |
| 1838 | * selector: selector field |
| 1839 | * attr_val: the attribute value after the query request completes |
| 1840 | * |
| 1841 | * Returns 0 for success, non-zero in case of failure |
| 1842 | */ |
Sujit Reddy Thumma | bdbe5d2 | 2014-05-26 10:59:11 +0530 | [diff] [blame] | 1843 | static int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 1844 | enum attr_idn idn, u8 index, u8 selector, u32 *attr_val) |
| 1845 | { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 1846 | struct ufs_query_req *request = NULL; |
| 1847 | struct ufs_query_res *response = NULL; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 1848 | int err; |
| 1849 | |
| 1850 | BUG_ON(!hba); |
| 1851 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1852 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 1853 | if (!attr_val) { |
| 1854 | dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n", |
| 1855 | __func__, opcode); |
| 1856 | err = -EINVAL; |
| 1857 | goto out; |
| 1858 | } |
| 1859 | |
| 1860 | mutex_lock(&hba->dev_cmd.lock); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 1861 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 1862 | selector); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 1863 | |
| 1864 | switch (opcode) { |
| 1865 | case UPIU_QUERY_OPCODE_WRITE_ATTR: |
| 1866 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 1867 | request->upiu_req.value = cpu_to_be32(*attr_val); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 1868 | break; |
| 1869 | case UPIU_QUERY_OPCODE_READ_ATTR: |
| 1870 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 1871 | break; |
| 1872 | default: |
| 1873 | dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n", |
| 1874 | __func__, opcode); |
| 1875 | err = -EINVAL; |
| 1876 | goto out_unlock; |
| 1877 | } |
| 1878 | |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 1879 | 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] | 1880 | |
| 1881 | if (err) { |
| 1882 | dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n", |
| 1883 | __func__, opcode, idn, err); |
| 1884 | goto out_unlock; |
| 1885 | } |
| 1886 | |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 1887 | *attr_val = be32_to_cpu(response->upiu_res.value); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 1888 | |
| 1889 | out_unlock: |
| 1890 | mutex_unlock(&hba->dev_cmd.lock); |
| 1891 | out: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1892 | ufshcd_release(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 1893 | return err; |
| 1894 | } |
| 1895 | |
| 1896 | /** |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 1897 | * ufshcd_query_attr_retry() - API function for sending query |
| 1898 | * attribute with retries |
| 1899 | * @hba: per-adapter instance |
| 1900 | * @opcode: attribute opcode |
| 1901 | * @idn: attribute idn to access |
| 1902 | * @index: index field |
| 1903 | * @selector: selector field |
| 1904 | * @attr_val: the attribute value after the query request |
| 1905 | * completes |
| 1906 | * |
| 1907 | * Returns 0 for success, non-zero in case of failure |
| 1908 | */ |
| 1909 | static int ufshcd_query_attr_retry(struct ufs_hba *hba, |
| 1910 | enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector, |
| 1911 | u32 *attr_val) |
| 1912 | { |
| 1913 | int ret = 0; |
| 1914 | u32 retries; |
| 1915 | |
| 1916 | for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { |
| 1917 | ret = ufshcd_query_attr(hba, opcode, idn, index, |
| 1918 | selector, attr_val); |
| 1919 | if (ret) |
| 1920 | dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n", |
| 1921 | __func__, ret, retries); |
| 1922 | else |
| 1923 | break; |
| 1924 | } |
| 1925 | |
| 1926 | if (ret) |
| 1927 | dev_err(hba->dev, |
| 1928 | "%s: query attribute, idn %d, failed with error %d after %d retires\n", |
| 1929 | __func__, idn, ret, QUERY_REQ_RETRIES); |
| 1930 | return ret; |
| 1931 | } |
| 1932 | |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 1933 | static int __ufshcd_query_descriptor(struct ufs_hba *hba, |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 1934 | enum query_opcode opcode, enum desc_idn idn, u8 index, |
| 1935 | u8 selector, u8 *desc_buf, int *buf_len) |
| 1936 | { |
| 1937 | struct ufs_query_req *request = NULL; |
| 1938 | struct ufs_query_res *response = NULL; |
| 1939 | int err; |
| 1940 | |
| 1941 | BUG_ON(!hba); |
| 1942 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1943 | ufshcd_hold(hba, false); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 1944 | if (!desc_buf) { |
| 1945 | dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n", |
| 1946 | __func__, opcode); |
| 1947 | err = -EINVAL; |
| 1948 | goto out; |
| 1949 | } |
| 1950 | |
Potomski, MichalX | e192845 | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 1951 | 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] | 1952 | dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n", |
| 1953 | __func__, *buf_len); |
| 1954 | err = -EINVAL; |
| 1955 | goto out; |
| 1956 | } |
| 1957 | |
| 1958 | mutex_lock(&hba->dev_cmd.lock); |
| 1959 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 1960 | selector); |
| 1961 | hba->dev_cmd.query.descriptor = desc_buf; |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 1962 | request->upiu_req.length = cpu_to_be16(*buf_len); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 1963 | |
| 1964 | switch (opcode) { |
| 1965 | case UPIU_QUERY_OPCODE_WRITE_DESC: |
| 1966 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
| 1967 | break; |
| 1968 | case UPIU_QUERY_OPCODE_READ_DESC: |
| 1969 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 1970 | break; |
| 1971 | default: |
| 1972 | dev_err(hba->dev, |
| 1973 | "%s: Expected query descriptor opcode but got = 0x%.2x\n", |
| 1974 | __func__, opcode); |
| 1975 | err = -EINVAL; |
| 1976 | goto out_unlock; |
| 1977 | } |
| 1978 | |
| 1979 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT); |
| 1980 | |
| 1981 | if (err) { |
| 1982 | dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n", |
| 1983 | __func__, opcode, idn, err); |
| 1984 | goto out_unlock; |
| 1985 | } |
| 1986 | |
| 1987 | hba->dev_cmd.query.descriptor = NULL; |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 1988 | *buf_len = be16_to_cpu(response->upiu_res.length); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 1989 | |
| 1990 | out_unlock: |
| 1991 | mutex_unlock(&hba->dev_cmd.lock); |
| 1992 | out: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1993 | ufshcd_release(hba); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 1994 | return err; |
| 1995 | } |
| 1996 | |
| 1997 | /** |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 1998 | * ufshcd_query_descriptor_retry - API function for sending descriptor |
| 1999 | * requests |
| 2000 | * hba: per-adapter instance |
| 2001 | * opcode: attribute opcode |
| 2002 | * idn: attribute idn to access |
| 2003 | * index: index field |
| 2004 | * selector: selector field |
| 2005 | * desc_buf: the buffer that contains the descriptor |
| 2006 | * buf_len: length parameter passed to the device |
| 2007 | * |
| 2008 | * Returns 0 for success, non-zero in case of failure. |
| 2009 | * The buf_len parameter will contain, on return, the length parameter |
| 2010 | * received on the response. |
| 2011 | */ |
| 2012 | int ufshcd_query_descriptor_retry(struct ufs_hba *hba, |
| 2013 | enum query_opcode opcode, enum desc_idn idn, u8 index, |
| 2014 | u8 selector, u8 *desc_buf, int *buf_len) |
| 2015 | { |
| 2016 | int err; |
| 2017 | int retries; |
| 2018 | |
| 2019 | for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { |
| 2020 | err = __ufshcd_query_descriptor(hba, opcode, idn, index, |
| 2021 | selector, desc_buf, buf_len); |
| 2022 | if (!err || err == -EINVAL) |
| 2023 | break; |
| 2024 | } |
| 2025 | |
| 2026 | return err; |
| 2027 | } |
| 2028 | EXPORT_SYMBOL(ufshcd_query_descriptor_retry); |
| 2029 | |
| 2030 | /** |
Potomski, MichalX | e192845 | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 2031 | * ufshcd_read_desc_length - read the specified descriptor length from header |
| 2032 | * @hba: Pointer to adapter instance |
| 2033 | * @desc_id: descriptor idn value |
| 2034 | * @desc_index: descriptor index |
| 2035 | * @desc_length: pointer to variable to read the length of descriptor |
| 2036 | * |
| 2037 | * Return 0 in case of success, non-zero otherwise |
| 2038 | */ |
| 2039 | static int ufshcd_read_desc_length(struct ufs_hba *hba, |
| 2040 | enum desc_idn desc_id, |
| 2041 | int desc_index, |
| 2042 | int *desc_length) |
| 2043 | { |
| 2044 | int ret; |
| 2045 | u8 header[QUERY_DESC_HDR_SIZE]; |
| 2046 | int header_len = QUERY_DESC_HDR_SIZE; |
| 2047 | |
| 2048 | if (desc_id >= QUERY_DESC_IDN_MAX) |
| 2049 | return -EINVAL; |
| 2050 | |
| 2051 | ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC, |
| 2052 | desc_id, desc_index, 0, header, |
| 2053 | &header_len); |
| 2054 | |
| 2055 | if (ret) { |
| 2056 | dev_err(hba->dev, "%s: Failed to get descriptor header id %d", |
| 2057 | __func__, desc_id); |
| 2058 | return ret; |
| 2059 | } else if (desc_id != header[QUERY_DESC_DESC_TYPE_OFFSET]) { |
| 2060 | dev_warn(hba->dev, "%s: descriptor header id %d and desc_id %d mismatch", |
| 2061 | __func__, header[QUERY_DESC_DESC_TYPE_OFFSET], |
| 2062 | desc_id); |
| 2063 | ret = -EINVAL; |
| 2064 | } |
| 2065 | |
| 2066 | *desc_length = header[QUERY_DESC_LENGTH_OFFSET]; |
| 2067 | return ret; |
| 2068 | |
| 2069 | } |
| 2070 | |
| 2071 | /** |
| 2072 | * ufshcd_map_desc_id_to_length - map descriptor IDN to its length |
| 2073 | * @hba: Pointer to adapter instance |
| 2074 | * @desc_id: descriptor idn value |
| 2075 | * @desc_len: mapped desc length (out) |
| 2076 | * |
| 2077 | * Return 0 in case of success, non-zero otherwise |
| 2078 | */ |
| 2079 | int ufshcd_map_desc_id_to_length(struct ufs_hba *hba, |
| 2080 | enum desc_idn desc_id, int *desc_len) |
| 2081 | { |
| 2082 | switch (desc_id) { |
| 2083 | case QUERY_DESC_IDN_DEVICE: |
| 2084 | *desc_len = hba->desc_size.dev_desc; |
| 2085 | break; |
| 2086 | case QUERY_DESC_IDN_POWER: |
| 2087 | *desc_len = hba->desc_size.pwr_desc; |
| 2088 | break; |
| 2089 | case QUERY_DESC_IDN_GEOMETRY: |
| 2090 | *desc_len = hba->desc_size.geom_desc; |
| 2091 | break; |
| 2092 | case QUERY_DESC_IDN_CONFIGURATION: |
| 2093 | *desc_len = hba->desc_size.conf_desc; |
| 2094 | break; |
| 2095 | case QUERY_DESC_IDN_UNIT: |
| 2096 | *desc_len = hba->desc_size.unit_desc; |
| 2097 | break; |
| 2098 | case QUERY_DESC_IDN_INTERCONNECT: |
| 2099 | *desc_len = hba->desc_size.interc_desc; |
| 2100 | break; |
| 2101 | case QUERY_DESC_IDN_STRING: |
| 2102 | *desc_len = QUERY_DESC_MAX_SIZE; |
| 2103 | break; |
| 2104 | case QUERY_DESC_IDN_RFU_0: |
| 2105 | case QUERY_DESC_IDN_RFU_1: |
| 2106 | *desc_len = 0; |
| 2107 | break; |
| 2108 | default: |
| 2109 | *desc_len = 0; |
| 2110 | return -EINVAL; |
| 2111 | } |
| 2112 | return 0; |
| 2113 | } |
| 2114 | EXPORT_SYMBOL(ufshcd_map_desc_id_to_length); |
| 2115 | |
| 2116 | /** |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 2117 | * ufshcd_read_desc_param - read the specified descriptor parameter |
| 2118 | * @hba: Pointer to adapter instance |
| 2119 | * @desc_id: descriptor idn value |
| 2120 | * @desc_index: descriptor index |
| 2121 | * @param_offset: offset of the parameter to read |
| 2122 | * @param_read_buf: pointer to buffer where parameter would be read |
| 2123 | * @param_size: sizeof(param_read_buf) |
| 2124 | * |
| 2125 | * Return 0 in case of success, non-zero otherwise |
| 2126 | */ |
| 2127 | static int ufshcd_read_desc_param(struct ufs_hba *hba, |
| 2128 | enum desc_idn desc_id, |
| 2129 | int desc_index, |
Potomski, MichalX | e192845 | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 2130 | u8 param_offset, |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 2131 | u8 *param_read_buf, |
Potomski, MichalX | e192845 | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 2132 | u8 param_size) |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 2133 | { |
| 2134 | int ret; |
| 2135 | u8 *desc_buf; |
Potomski, MichalX | e192845 | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 2136 | int buff_len; |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 2137 | bool is_kmalloc = true; |
| 2138 | |
Potomski, MichalX | e192845 | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 2139 | /* Safety check */ |
| 2140 | if (desc_id >= QUERY_DESC_IDN_MAX || !param_size) |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 2141 | return -EINVAL; |
| 2142 | |
Potomski, MichalX | e192845 | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 2143 | /* Get the max length of descriptor from structure filled up at probe |
| 2144 | * time. |
| 2145 | */ |
| 2146 | ret = ufshcd_map_desc_id_to_length(hba, desc_id, &buff_len); |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 2147 | |
Potomski, MichalX | e192845 | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 2148 | /* Sanity checks */ |
| 2149 | if (ret || !buff_len) { |
| 2150 | dev_err(hba->dev, "%s: Failed to get full descriptor length", |
| 2151 | __func__); |
| 2152 | return ret; |
| 2153 | } |
| 2154 | |
| 2155 | /* Check whether we need temp memory */ |
| 2156 | if (param_offset != 0 || param_size < buff_len) { |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 2157 | desc_buf = kmalloc(buff_len, GFP_KERNEL); |
| 2158 | if (!desc_buf) |
| 2159 | return -ENOMEM; |
Potomski, MichalX | e192845 | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 2160 | } else { |
| 2161 | desc_buf = param_read_buf; |
| 2162 | is_kmalloc = false; |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 2163 | } |
| 2164 | |
Potomski, MichalX | e192845 | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 2165 | /* Request for full descriptor */ |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 2166 | ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC, |
Potomski, MichalX | e192845 | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 2167 | desc_id, desc_index, 0, |
| 2168 | desc_buf, &buff_len); |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 2169 | |
Subhash Jadavani | b0a12b4 | 2016-11-23 16:31:41 -0800 | [diff] [blame] | 2170 | if (ret) { |
| 2171 | dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d", |
| 2172 | __func__, desc_id, desc_index, param_offset, ret); |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 2173 | goto out; |
| 2174 | } |
| 2175 | |
Subhash Jadavani | b0a12b4 | 2016-11-23 16:31:41 -0800 | [diff] [blame] | 2176 | /* Sanity check */ |
| 2177 | if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) { |
| 2178 | dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header", |
| 2179 | __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]); |
| 2180 | ret = -EINVAL; |
| 2181 | goto out; |
| 2182 | } |
| 2183 | |
Potomski, MichalX | e192845 | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 2184 | /* Check wherher we will not copy more data, than available */ |
| 2185 | if (is_kmalloc && param_size > buff_len) |
| 2186 | param_size = buff_len; |
Subhash Jadavani | b0a12b4 | 2016-11-23 16:31:41 -0800 | [diff] [blame] | 2187 | |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 2188 | if (is_kmalloc) |
| 2189 | memcpy(param_read_buf, &desc_buf[param_offset], param_size); |
| 2190 | out: |
| 2191 | if (is_kmalloc) |
| 2192 | kfree(desc_buf); |
| 2193 | return ret; |
| 2194 | } |
| 2195 | |
| 2196 | static inline int ufshcd_read_desc(struct ufs_hba *hba, |
| 2197 | enum desc_idn desc_id, |
| 2198 | int desc_index, |
| 2199 | u8 *buf, |
| 2200 | u32 size) |
| 2201 | { |
| 2202 | return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size); |
| 2203 | } |
| 2204 | |
| 2205 | static inline int ufshcd_read_power_desc(struct ufs_hba *hba, |
| 2206 | u8 *buf, |
| 2207 | u32 size) |
| 2208 | { |
| 2209 | return ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size); |
| 2210 | } |
| 2211 | |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 2212 | int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size) |
| 2213 | { |
| 2214 | return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size); |
| 2215 | } |
| 2216 | EXPORT_SYMBOL(ufshcd_read_device_desc); |
| 2217 | |
| 2218 | /** |
| 2219 | * ufshcd_read_string_desc - read string descriptor |
| 2220 | * @hba: pointer to adapter instance |
| 2221 | * @desc_index: descriptor index |
| 2222 | * @buf: pointer to buffer where descriptor would be read |
| 2223 | * @size: size of buf |
| 2224 | * @ascii: if true convert from unicode to ascii characters |
| 2225 | * |
| 2226 | * Return 0 in case of success, non-zero otherwise |
| 2227 | */ |
| 2228 | int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index, u8 *buf, |
| 2229 | u32 size, bool ascii) |
| 2230 | { |
| 2231 | int err = 0; |
| 2232 | |
| 2233 | err = ufshcd_read_desc(hba, |
| 2234 | QUERY_DESC_IDN_STRING, desc_index, buf, size); |
| 2235 | |
| 2236 | if (err) { |
| 2237 | dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n", |
| 2238 | __func__, QUERY_REQ_RETRIES, err); |
| 2239 | goto out; |
| 2240 | } |
| 2241 | |
| 2242 | if (ascii) { |
| 2243 | int desc_len; |
| 2244 | int ascii_len; |
| 2245 | int i; |
| 2246 | char *buff_ascii; |
| 2247 | |
| 2248 | desc_len = buf[0]; |
| 2249 | /* remove header and divide by 2 to move from UTF16 to UTF8 */ |
| 2250 | ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1; |
| 2251 | if (size < ascii_len + QUERY_DESC_HDR_SIZE) { |
| 2252 | dev_err(hba->dev, "%s: buffer allocated size is too small\n", |
| 2253 | __func__); |
| 2254 | err = -ENOMEM; |
| 2255 | goto out; |
| 2256 | } |
| 2257 | |
| 2258 | buff_ascii = kmalloc(ascii_len, GFP_KERNEL); |
| 2259 | if (!buff_ascii) { |
| 2260 | err = -ENOMEM; |
Tiezhu Yang | fcbefc3 | 2016-06-25 12:35:22 +0800 | [diff] [blame] | 2261 | goto out; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 2262 | } |
| 2263 | |
| 2264 | /* |
| 2265 | * the descriptor contains string in UTF16 format |
| 2266 | * we need to convert to utf-8 so it can be displayed |
| 2267 | */ |
| 2268 | utf16s_to_utf8s((wchar_t *)&buf[QUERY_DESC_HDR_SIZE], |
| 2269 | desc_len - QUERY_DESC_HDR_SIZE, |
| 2270 | UTF16_BIG_ENDIAN, buff_ascii, ascii_len); |
| 2271 | |
| 2272 | /* replace non-printable or non-ASCII characters with spaces */ |
| 2273 | for (i = 0; i < ascii_len; i++) |
| 2274 | ufshcd_remove_non_printable(&buff_ascii[i]); |
| 2275 | |
| 2276 | memset(buf + QUERY_DESC_HDR_SIZE, 0, |
| 2277 | size - QUERY_DESC_HDR_SIZE); |
| 2278 | memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len); |
| 2279 | buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 2280 | kfree(buff_ascii); |
| 2281 | } |
| 2282 | out: |
| 2283 | return err; |
| 2284 | } |
| 2285 | EXPORT_SYMBOL(ufshcd_read_string_desc); |
| 2286 | |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 2287 | /** |
| 2288 | * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter |
| 2289 | * @hba: Pointer to adapter instance |
| 2290 | * @lun: lun id |
| 2291 | * @param_offset: offset of the parameter to read |
| 2292 | * @param_read_buf: pointer to buffer where parameter would be read |
| 2293 | * @param_size: sizeof(param_read_buf) |
| 2294 | * |
| 2295 | * Return 0 in case of success, non-zero otherwise |
| 2296 | */ |
| 2297 | static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba, |
| 2298 | int lun, |
| 2299 | enum unit_desc_param param_offset, |
| 2300 | u8 *param_read_buf, |
| 2301 | u32 param_size) |
| 2302 | { |
| 2303 | /* |
| 2304 | * Unit descriptors are only available for general purpose LUs (LUN id |
| 2305 | * from 0 to 7) and RPMB Well known LU. |
| 2306 | */ |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 2307 | if (lun != UFS_UPIU_RPMB_WLUN && (lun >= UFS_UPIU_MAX_GENERAL_LUN)) |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 2308 | return -EOPNOTSUPP; |
| 2309 | |
| 2310 | return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun, |
| 2311 | param_offset, param_read_buf, param_size); |
| 2312 | } |
| 2313 | |
| 2314 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2315 | * ufshcd_memory_alloc - allocate memory for host memory space data structures |
| 2316 | * @hba: per adapter instance |
| 2317 | * |
| 2318 | * 1. Allocate DMA memory for Command Descriptor array |
| 2319 | * Each command descriptor consist of Command UPIU, Response UPIU and PRDT |
| 2320 | * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL). |
| 2321 | * 3. Allocate DMA memory for UTP Task Management Request Descriptor List |
| 2322 | * (UTMRDL) |
| 2323 | * 4. Allocate memory for local reference block(lrb). |
| 2324 | * |
| 2325 | * Returns 0 for success, non-zero in case of failure |
| 2326 | */ |
| 2327 | static int ufshcd_memory_alloc(struct ufs_hba *hba) |
| 2328 | { |
| 2329 | size_t utmrdl_size, utrdl_size, ucdl_size; |
| 2330 | |
| 2331 | /* Allocate memory for UTP command descriptors */ |
| 2332 | ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 2333 | hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 2334 | ucdl_size, |
| 2335 | &hba->ucdl_dma_addr, |
| 2336 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2337 | |
| 2338 | /* |
| 2339 | * UFSHCI requires UTP command descriptor to be 128 byte aligned. |
| 2340 | * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE |
| 2341 | * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will |
| 2342 | * be aligned to 128 bytes as well |
| 2343 | */ |
| 2344 | if (!hba->ucdl_base_addr || |
| 2345 | WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 2346 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2347 | "Command Descriptor Memory allocation failed\n"); |
| 2348 | goto out; |
| 2349 | } |
| 2350 | |
| 2351 | /* |
| 2352 | * Allocate memory for UTP Transfer descriptors |
| 2353 | * UFSHCI requires 1024 byte alignment of UTRD |
| 2354 | */ |
| 2355 | utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 2356 | hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 2357 | utrdl_size, |
| 2358 | &hba->utrdl_dma_addr, |
| 2359 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2360 | if (!hba->utrdl_base_addr || |
| 2361 | WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 2362 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2363 | "Transfer Descriptor Memory allocation failed\n"); |
| 2364 | goto out; |
| 2365 | } |
| 2366 | |
| 2367 | /* |
| 2368 | * Allocate memory for UTP Task Management descriptors |
| 2369 | * UFSHCI requires 1024 byte alignment of UTMRD |
| 2370 | */ |
| 2371 | utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs; |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 2372 | hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 2373 | utmrdl_size, |
| 2374 | &hba->utmrdl_dma_addr, |
| 2375 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2376 | if (!hba->utmrdl_base_addr || |
| 2377 | WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 2378 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2379 | "Task Management Descriptor Memory allocation failed\n"); |
| 2380 | goto out; |
| 2381 | } |
| 2382 | |
| 2383 | /* Allocate memory for local reference block */ |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 2384 | hba->lrb = devm_kzalloc(hba->dev, |
| 2385 | hba->nutrs * sizeof(struct ufshcd_lrb), |
| 2386 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2387 | if (!hba->lrb) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 2388 | dev_err(hba->dev, "LRB Memory allocation failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2389 | goto out; |
| 2390 | } |
| 2391 | return 0; |
| 2392 | out: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2393 | return -ENOMEM; |
| 2394 | } |
| 2395 | |
| 2396 | /** |
| 2397 | * ufshcd_host_memory_configure - configure local reference block with |
| 2398 | * memory offsets |
| 2399 | * @hba: per adapter instance |
| 2400 | * |
| 2401 | * Configure Host memory space |
| 2402 | * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA |
| 2403 | * address. |
| 2404 | * 2. Update each UTRD with Response UPIU offset, Response UPIU length |
| 2405 | * and PRDT offset. |
| 2406 | * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT |
| 2407 | * into local reference block. |
| 2408 | */ |
| 2409 | static void ufshcd_host_memory_configure(struct ufs_hba *hba) |
| 2410 | { |
| 2411 | struct utp_transfer_cmd_desc *cmd_descp; |
| 2412 | struct utp_transfer_req_desc *utrdlp; |
| 2413 | dma_addr_t cmd_desc_dma_addr; |
| 2414 | dma_addr_t cmd_desc_element_addr; |
| 2415 | u16 response_offset; |
| 2416 | u16 prdt_offset; |
| 2417 | int cmd_desc_size; |
| 2418 | int i; |
| 2419 | |
| 2420 | utrdlp = hba->utrdl_base_addr; |
| 2421 | cmd_descp = hba->ucdl_base_addr; |
| 2422 | |
| 2423 | response_offset = |
| 2424 | offsetof(struct utp_transfer_cmd_desc, response_upiu); |
| 2425 | prdt_offset = |
| 2426 | offsetof(struct utp_transfer_cmd_desc, prd_table); |
| 2427 | |
| 2428 | cmd_desc_size = sizeof(struct utp_transfer_cmd_desc); |
| 2429 | cmd_desc_dma_addr = hba->ucdl_dma_addr; |
| 2430 | |
| 2431 | for (i = 0; i < hba->nutrs; i++) { |
| 2432 | /* Configure UTRD with command descriptor base address */ |
| 2433 | cmd_desc_element_addr = |
| 2434 | (cmd_desc_dma_addr + (cmd_desc_size * i)); |
| 2435 | utrdlp[i].command_desc_base_addr_lo = |
| 2436 | cpu_to_le32(lower_32_bits(cmd_desc_element_addr)); |
| 2437 | utrdlp[i].command_desc_base_addr_hi = |
| 2438 | cpu_to_le32(upper_32_bits(cmd_desc_element_addr)); |
| 2439 | |
| 2440 | /* Response upiu and prdt offset should be in double words */ |
Kiwoong Kim | 9b41ed7 | 2017-04-04 19:32:05 +0000 | [diff] [blame] | 2441 | if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) { |
| 2442 | utrdlp[i].response_upiu_offset = |
| 2443 | cpu_to_le16(response_offset); |
| 2444 | utrdlp[i].prd_table_offset = |
| 2445 | cpu_to_le16(prdt_offset); |
| 2446 | utrdlp[i].response_upiu_length = |
| 2447 | cpu_to_le16(ALIGNED_UPIU_SIZE); |
| 2448 | } else { |
| 2449 | utrdlp[i].response_upiu_offset = |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2450 | cpu_to_le16((response_offset >> 2)); |
Kiwoong Kim | 9b41ed7 | 2017-04-04 19:32:05 +0000 | [diff] [blame] | 2451 | utrdlp[i].prd_table_offset = |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2452 | cpu_to_le16((prdt_offset >> 2)); |
Kiwoong Kim | 9b41ed7 | 2017-04-04 19:32:05 +0000 | [diff] [blame] | 2453 | utrdlp[i].response_upiu_length = |
Sujit Reddy Thumma | 3ca316c | 2013-06-26 22:39:30 +0530 | [diff] [blame] | 2454 | cpu_to_le16(ALIGNED_UPIU_SIZE >> 2); |
Kiwoong Kim | 9b41ed7 | 2017-04-04 19:32:05 +0000 | [diff] [blame] | 2455 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2456 | |
| 2457 | hba->lrb[i].utr_descriptor_ptr = (utrdlp + i); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2458 | hba->lrb[i].ucd_req_ptr = |
| 2459 | (struct utp_upiu_req *)(cmd_descp + i); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2460 | hba->lrb[i].ucd_rsp_ptr = |
| 2461 | (struct utp_upiu_rsp *)cmd_descp[i].response_upiu; |
| 2462 | hba->lrb[i].ucd_prdt_ptr = |
| 2463 | (struct ufshcd_sg_entry *)cmd_descp[i].prd_table; |
| 2464 | } |
| 2465 | } |
| 2466 | |
| 2467 | /** |
| 2468 | * ufshcd_dme_link_startup - Notify Unipro to perform link startup |
| 2469 | * @hba: per adapter instance |
| 2470 | * |
| 2471 | * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer, |
| 2472 | * in order to initialize the Unipro link startup procedure. |
| 2473 | * Once the Unipro links are up, the device connected to the controller |
| 2474 | * is detected. |
| 2475 | * |
| 2476 | * Returns 0 on success, non-zero value on failure |
| 2477 | */ |
| 2478 | static int ufshcd_dme_link_startup(struct ufs_hba *hba) |
| 2479 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2480 | struct uic_command uic_cmd = {0}; |
| 2481 | int ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2482 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2483 | uic_cmd.command = UIC_CMD_DME_LINK_STARTUP; |
| 2484 | |
| 2485 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 2486 | if (ret) |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 2487 | dev_err(hba->dev, |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2488 | "dme-link-startup: error code %d\n", ret); |
| 2489 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2490 | } |
| 2491 | |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 2492 | static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba) |
| 2493 | { |
| 2494 | #define MIN_DELAY_BEFORE_DME_CMDS_US 1000 |
| 2495 | unsigned long min_sleep_time_us; |
| 2496 | |
| 2497 | if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS)) |
| 2498 | return; |
| 2499 | |
| 2500 | /* |
| 2501 | * last_dme_cmd_tstamp will be 0 only for 1st call to |
| 2502 | * this function |
| 2503 | */ |
| 2504 | if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) { |
| 2505 | min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US; |
| 2506 | } else { |
| 2507 | unsigned long delta = |
| 2508 | (unsigned long) ktime_to_us( |
| 2509 | ktime_sub(ktime_get(), |
| 2510 | hba->last_dme_cmd_tstamp)); |
| 2511 | |
| 2512 | if (delta < MIN_DELAY_BEFORE_DME_CMDS_US) |
| 2513 | min_sleep_time_us = |
| 2514 | MIN_DELAY_BEFORE_DME_CMDS_US - delta; |
| 2515 | else |
| 2516 | return; /* no more delay required */ |
| 2517 | } |
| 2518 | |
| 2519 | /* allow sleep for extra 50us if needed */ |
| 2520 | usleep_range(min_sleep_time_us, min_sleep_time_us + 50); |
| 2521 | } |
| 2522 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2523 | /** |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 2524 | * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET |
| 2525 | * @hba: per adapter instance |
| 2526 | * @attr_sel: uic command argument1 |
| 2527 | * @attr_set: attribute set type as uic command argument2 |
| 2528 | * @mib_val: setting value as uic command argument3 |
| 2529 | * @peer: indicate whether peer or local |
| 2530 | * |
| 2531 | * Returns 0 on success, non-zero value on failure |
| 2532 | */ |
| 2533 | int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel, |
| 2534 | u8 attr_set, u32 mib_val, u8 peer) |
| 2535 | { |
| 2536 | struct uic_command uic_cmd = {0}; |
| 2537 | static const char *const action[] = { |
| 2538 | "dme-set", |
| 2539 | "dme-peer-set" |
| 2540 | }; |
| 2541 | const char *set = action[!!peer]; |
| 2542 | int ret; |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 2543 | int retries = UFS_UIC_COMMAND_RETRIES; |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 2544 | |
| 2545 | uic_cmd.command = peer ? |
| 2546 | UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET; |
| 2547 | uic_cmd.argument1 = attr_sel; |
| 2548 | uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set); |
| 2549 | uic_cmd.argument3 = mib_val; |
| 2550 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 2551 | do { |
| 2552 | /* for peer attributes we retry upon failure */ |
| 2553 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 2554 | if (ret) |
| 2555 | dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n", |
| 2556 | set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret); |
| 2557 | } while (ret && peer && --retries); |
| 2558 | |
| 2559 | if (!retries) |
| 2560 | dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n", |
| 2561 | set, UIC_GET_ATTR_ID(attr_sel), mib_val, |
| 2562 | retries); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 2563 | |
| 2564 | return ret; |
| 2565 | } |
| 2566 | EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr); |
| 2567 | |
| 2568 | /** |
| 2569 | * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET |
| 2570 | * @hba: per adapter instance |
| 2571 | * @attr_sel: uic command argument1 |
| 2572 | * @mib_val: the value of the attribute as returned by the UIC command |
| 2573 | * @peer: indicate whether peer or local |
| 2574 | * |
| 2575 | * Returns 0 on success, non-zero value on failure |
| 2576 | */ |
| 2577 | int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel, |
| 2578 | u32 *mib_val, u8 peer) |
| 2579 | { |
| 2580 | struct uic_command uic_cmd = {0}; |
| 2581 | static const char *const action[] = { |
| 2582 | "dme-get", |
| 2583 | "dme-peer-get" |
| 2584 | }; |
| 2585 | const char *get = action[!!peer]; |
| 2586 | int ret; |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 2587 | int retries = UFS_UIC_COMMAND_RETRIES; |
Yaniv Gardi | 874237f | 2015-05-17 18:55:03 +0300 | [diff] [blame] | 2588 | struct ufs_pa_layer_attr orig_pwr_info; |
| 2589 | struct ufs_pa_layer_attr temp_pwr_info; |
| 2590 | bool pwr_mode_change = false; |
| 2591 | |
| 2592 | if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) { |
| 2593 | orig_pwr_info = hba->pwr_info; |
| 2594 | temp_pwr_info = orig_pwr_info; |
| 2595 | |
| 2596 | if (orig_pwr_info.pwr_tx == FAST_MODE || |
| 2597 | orig_pwr_info.pwr_rx == FAST_MODE) { |
| 2598 | temp_pwr_info.pwr_tx = FASTAUTO_MODE; |
| 2599 | temp_pwr_info.pwr_rx = FASTAUTO_MODE; |
| 2600 | pwr_mode_change = true; |
| 2601 | } else if (orig_pwr_info.pwr_tx == SLOW_MODE || |
| 2602 | orig_pwr_info.pwr_rx == SLOW_MODE) { |
| 2603 | temp_pwr_info.pwr_tx = SLOWAUTO_MODE; |
| 2604 | temp_pwr_info.pwr_rx = SLOWAUTO_MODE; |
| 2605 | pwr_mode_change = true; |
| 2606 | } |
| 2607 | if (pwr_mode_change) { |
| 2608 | ret = ufshcd_change_power_mode(hba, &temp_pwr_info); |
| 2609 | if (ret) |
| 2610 | goto out; |
| 2611 | } |
| 2612 | } |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 2613 | |
| 2614 | uic_cmd.command = peer ? |
| 2615 | UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET; |
| 2616 | uic_cmd.argument1 = attr_sel; |
| 2617 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 2618 | do { |
| 2619 | /* for peer attributes we retry upon failure */ |
| 2620 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 2621 | if (ret) |
| 2622 | dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n", |
| 2623 | get, UIC_GET_ATTR_ID(attr_sel), ret); |
| 2624 | } while (ret && peer && --retries); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 2625 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 2626 | if (!retries) |
| 2627 | dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n", |
| 2628 | get, UIC_GET_ATTR_ID(attr_sel), retries); |
| 2629 | |
| 2630 | if (mib_val && !ret) |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 2631 | *mib_val = uic_cmd.argument3; |
Yaniv Gardi | 874237f | 2015-05-17 18:55:03 +0300 | [diff] [blame] | 2632 | |
| 2633 | if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE) |
| 2634 | && pwr_mode_change) |
| 2635 | ufshcd_change_power_mode(hba, &orig_pwr_info); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 2636 | out: |
| 2637 | return ret; |
| 2638 | } |
| 2639 | EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr); |
| 2640 | |
| 2641 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2642 | * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power |
| 2643 | * state) and waits for it to take effect. |
| 2644 | * |
| 2645 | * @hba: per adapter instance |
| 2646 | * @cmd: UIC command to execute |
| 2647 | * |
| 2648 | * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER & |
| 2649 | * DME_HIBERNATE_EXIT commands take some time to take its effect on both host |
| 2650 | * and device UniPro link and hence it's final completion would be indicated by |
| 2651 | * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in |
| 2652 | * addition to normal UIC command completion Status (UCCS). This function only |
| 2653 | * returns after the relevant status bits indicate the completion. |
| 2654 | * |
| 2655 | * Returns 0 on success, non-zero value on failure |
| 2656 | */ |
| 2657 | static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd) |
| 2658 | { |
| 2659 | struct completion uic_async_done; |
| 2660 | unsigned long flags; |
| 2661 | u8 status; |
| 2662 | int ret; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2663 | bool reenable_intr = false; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2664 | |
| 2665 | mutex_lock(&hba->uic_cmd_mutex); |
| 2666 | init_completion(&uic_async_done); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 2667 | ufshcd_add_delay_before_dme_cmd(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2668 | |
| 2669 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2670 | hba->uic_async_done = &uic_async_done; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2671 | if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) { |
| 2672 | ufshcd_disable_intr(hba, UIC_COMMAND_COMPL); |
| 2673 | /* |
| 2674 | * Make sure UIC command completion interrupt is disabled before |
| 2675 | * issuing UIC command. |
| 2676 | */ |
| 2677 | wmb(); |
| 2678 | reenable_intr = true; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2679 | } |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2680 | ret = __ufshcd_send_uic_cmd(hba, cmd, false); |
| 2681 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2682 | if (ret) { |
| 2683 | dev_err(hba->dev, |
| 2684 | "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n", |
| 2685 | cmd->command, cmd->argument3, ret); |
| 2686 | goto out; |
| 2687 | } |
| 2688 | |
| 2689 | if (!wait_for_completion_timeout(hba->uic_async_done, |
| 2690 | msecs_to_jiffies(UIC_CMD_TIMEOUT))) { |
| 2691 | dev_err(hba->dev, |
| 2692 | "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n", |
| 2693 | cmd->command, cmd->argument3); |
| 2694 | ret = -ETIMEDOUT; |
| 2695 | goto out; |
| 2696 | } |
| 2697 | |
| 2698 | status = ufshcd_get_upmcrs(hba); |
| 2699 | if (status != PWR_LOCAL) { |
| 2700 | dev_err(hba->dev, |
Kiwoong Kim | 7361542 | 2016-09-08 16:50:02 +0900 | [diff] [blame] | 2701 | "pwr ctrl cmd 0x%0x failed, host upmcrs:0x%x\n", |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2702 | cmd->command, status); |
| 2703 | ret = (status != PWR_OK) ? status : -1; |
| 2704 | } |
| 2705 | out: |
| 2706 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2707 | hba->active_uic_cmd = NULL; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2708 | hba->uic_async_done = NULL; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2709 | if (reenable_intr) |
| 2710 | ufshcd_enable_intr(hba, UIC_COMMAND_COMPL); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2711 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2712 | mutex_unlock(&hba->uic_cmd_mutex); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2713 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2714 | return ret; |
| 2715 | } |
| 2716 | |
| 2717 | /** |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 2718 | * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage |
| 2719 | * using DME_SET primitives. |
| 2720 | * @hba: per adapter instance |
| 2721 | * @mode: powr mode value |
| 2722 | * |
| 2723 | * Returns 0 on success, non-zero value on failure |
| 2724 | */ |
Sujit Reddy Thumma | bdbe5d2 | 2014-05-26 10:59:11 +0530 | [diff] [blame] | 2725 | 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] | 2726 | { |
| 2727 | struct uic_command uic_cmd = {0}; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2728 | int ret; |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 2729 | |
Yaniv Gardi | c3a2f9e | 2015-05-17 18:55:01 +0300 | [diff] [blame] | 2730 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) { |
| 2731 | ret = ufshcd_dme_set(hba, |
| 2732 | UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1); |
| 2733 | if (ret) { |
| 2734 | dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n", |
| 2735 | __func__, ret); |
| 2736 | goto out; |
| 2737 | } |
| 2738 | } |
| 2739 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 2740 | uic_cmd.command = UIC_CMD_DME_SET; |
| 2741 | uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE); |
| 2742 | uic_cmd.argument3 = mode; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2743 | ufshcd_hold(hba, false); |
| 2744 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
| 2745 | ufshcd_release(hba); |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 2746 | |
Yaniv Gardi | c3a2f9e | 2015-05-17 18:55:01 +0300 | [diff] [blame] | 2747 | out: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2748 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2749 | } |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 2750 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 2751 | static int ufshcd_link_recovery(struct ufs_hba *hba) |
| 2752 | { |
| 2753 | int ret; |
| 2754 | unsigned long flags; |
| 2755 | |
| 2756 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2757 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 2758 | ufshcd_set_eh_in_progress(hba); |
| 2759 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2760 | |
| 2761 | ret = ufshcd_host_reset_and_restore(hba); |
| 2762 | |
| 2763 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2764 | if (ret) |
| 2765 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 2766 | ufshcd_clear_eh_in_progress(hba); |
| 2767 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2768 | |
| 2769 | if (ret) |
| 2770 | dev_err(hba->dev, "%s: link recovery failed, err %d", |
| 2771 | __func__, ret); |
| 2772 | |
| 2773 | return ret; |
| 2774 | } |
| 2775 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 2776 | static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2777 | { |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 2778 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2779 | struct uic_command uic_cmd = {0}; |
| 2780 | |
| 2781 | uic_cmd.command = UIC_CMD_DME_HIBER_ENTER; |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 2782 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2783 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 2784 | if (ret) { |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 2785 | dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n", |
| 2786 | __func__, ret); |
| 2787 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 2788 | /* |
| 2789 | * If link recovery fails then return error so that caller |
| 2790 | * don't retry the hibern8 enter again. |
| 2791 | */ |
| 2792 | if (ufshcd_link_recovery(hba)) |
| 2793 | ret = -ENOLINK; |
| 2794 | } |
| 2795 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 2796 | return ret; |
| 2797 | } |
| 2798 | |
| 2799 | static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba) |
| 2800 | { |
| 2801 | int ret = 0, retries; |
| 2802 | |
| 2803 | for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) { |
| 2804 | ret = __ufshcd_uic_hibern8_enter(hba); |
| 2805 | if (!ret || ret == -ENOLINK) |
| 2806 | goto out; |
| 2807 | } |
| 2808 | out: |
| 2809 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2810 | } |
| 2811 | |
| 2812 | static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba) |
| 2813 | { |
| 2814 | struct uic_command uic_cmd = {0}; |
| 2815 | int ret; |
| 2816 | |
| 2817 | uic_cmd.command = UIC_CMD_DME_HIBER_EXIT; |
| 2818 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 2819 | if (ret) { |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 2820 | dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n", |
| 2821 | __func__, ret); |
| 2822 | ret = ufshcd_link_recovery(hba); |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 2823 | } |
| 2824 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 2825 | return ret; |
| 2826 | } |
| 2827 | |
Yaniv Gardi | 5064636 | 2014-10-23 13:25:13 +0300 | [diff] [blame] | 2828 | /** |
| 2829 | * ufshcd_init_pwr_info - setting the POR (power on reset) |
| 2830 | * values in hba power info |
| 2831 | * @hba: per-adapter instance |
| 2832 | */ |
| 2833 | static void ufshcd_init_pwr_info(struct ufs_hba *hba) |
| 2834 | { |
| 2835 | hba->pwr_info.gear_rx = UFS_PWM_G1; |
| 2836 | hba->pwr_info.gear_tx = UFS_PWM_G1; |
| 2837 | hba->pwr_info.lane_rx = 1; |
| 2838 | hba->pwr_info.lane_tx = 1; |
| 2839 | hba->pwr_info.pwr_rx = SLOWAUTO_MODE; |
| 2840 | hba->pwr_info.pwr_tx = SLOWAUTO_MODE; |
| 2841 | hba->pwr_info.hs_rate = 0; |
| 2842 | } |
| 2843 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 2844 | /** |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 2845 | * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device |
| 2846 | * @hba: per-adapter instance |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 2847 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 2848 | static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 2849 | { |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 2850 | struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info; |
| 2851 | |
| 2852 | if (hba->max_pwr_info.is_valid) |
| 2853 | return 0; |
| 2854 | |
| 2855 | pwr_info->pwr_tx = FASTAUTO_MODE; |
| 2856 | pwr_info->pwr_rx = FASTAUTO_MODE; |
| 2857 | pwr_info->hs_rate = PA_HS_MODE_B; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 2858 | |
| 2859 | /* Get the connected lane count */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 2860 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES), |
| 2861 | &pwr_info->lane_rx); |
| 2862 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 2863 | &pwr_info->lane_tx); |
| 2864 | |
| 2865 | if (!pwr_info->lane_rx || !pwr_info->lane_tx) { |
| 2866 | dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n", |
| 2867 | __func__, |
| 2868 | pwr_info->lane_rx, |
| 2869 | pwr_info->lane_tx); |
| 2870 | return -EINVAL; |
| 2871 | } |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 2872 | |
| 2873 | /* |
| 2874 | * First, get the maximum gears of HS speed. |
| 2875 | * If a zero value, it means there is no HSGEAR capability. |
| 2876 | * Then, get the maximum gears of PWM speed. |
| 2877 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 2878 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx); |
| 2879 | if (!pwr_info->gear_rx) { |
| 2880 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), |
| 2881 | &pwr_info->gear_rx); |
| 2882 | if (!pwr_info->gear_rx) { |
| 2883 | dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n", |
| 2884 | __func__, pwr_info->gear_rx); |
| 2885 | return -EINVAL; |
| 2886 | } |
| 2887 | pwr_info->pwr_rx = SLOWAUTO_MODE; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 2888 | } |
| 2889 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 2890 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), |
| 2891 | &pwr_info->gear_tx); |
| 2892 | if (!pwr_info->gear_tx) { |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 2893 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 2894 | &pwr_info->gear_tx); |
| 2895 | if (!pwr_info->gear_tx) { |
| 2896 | dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n", |
| 2897 | __func__, pwr_info->gear_tx); |
| 2898 | return -EINVAL; |
| 2899 | } |
| 2900 | pwr_info->pwr_tx = SLOWAUTO_MODE; |
| 2901 | } |
| 2902 | |
| 2903 | hba->max_pwr_info.is_valid = true; |
| 2904 | return 0; |
| 2905 | } |
| 2906 | |
| 2907 | static int ufshcd_change_power_mode(struct ufs_hba *hba, |
| 2908 | struct ufs_pa_layer_attr *pwr_mode) |
| 2909 | { |
| 2910 | int ret; |
| 2911 | |
| 2912 | /* if already configured to the requested pwr_mode */ |
| 2913 | if (pwr_mode->gear_rx == hba->pwr_info.gear_rx && |
| 2914 | pwr_mode->gear_tx == hba->pwr_info.gear_tx && |
| 2915 | pwr_mode->lane_rx == hba->pwr_info.lane_rx && |
| 2916 | pwr_mode->lane_tx == hba->pwr_info.lane_tx && |
| 2917 | pwr_mode->pwr_rx == hba->pwr_info.pwr_rx && |
| 2918 | pwr_mode->pwr_tx == hba->pwr_info.pwr_tx && |
| 2919 | pwr_mode->hs_rate == hba->pwr_info.hs_rate) { |
| 2920 | dev_dbg(hba->dev, "%s: power already configured\n", __func__); |
| 2921 | return 0; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 2922 | } |
| 2923 | |
| 2924 | /* |
| 2925 | * Configure attributes for power mode change with below. |
| 2926 | * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION, |
| 2927 | * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION, |
| 2928 | * - PA_HSSERIES |
| 2929 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 2930 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx); |
| 2931 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES), |
| 2932 | pwr_mode->lane_rx); |
| 2933 | if (pwr_mode->pwr_rx == FASTAUTO_MODE || |
| 2934 | pwr_mode->pwr_rx == FAST_MODE) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 2935 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 2936 | else |
| 2937 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 2938 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 2939 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx); |
| 2940 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES), |
| 2941 | pwr_mode->lane_tx); |
| 2942 | if (pwr_mode->pwr_tx == FASTAUTO_MODE || |
| 2943 | pwr_mode->pwr_tx == FAST_MODE) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 2944 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 2945 | else |
| 2946 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 2947 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 2948 | if (pwr_mode->pwr_rx == FASTAUTO_MODE || |
| 2949 | pwr_mode->pwr_tx == FASTAUTO_MODE || |
| 2950 | pwr_mode->pwr_rx == FAST_MODE || |
| 2951 | pwr_mode->pwr_tx == FAST_MODE) |
| 2952 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES), |
| 2953 | pwr_mode->hs_rate); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 2954 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 2955 | ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4 |
| 2956 | | pwr_mode->pwr_tx); |
| 2957 | |
| 2958 | if (ret) { |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 2959 | dev_err(hba->dev, |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 2960 | "%s: power mode change failed %d\n", __func__, ret); |
| 2961 | } else { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 2962 | ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL, |
| 2963 | pwr_mode); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 2964 | |
| 2965 | memcpy(&hba->pwr_info, pwr_mode, |
| 2966 | sizeof(struct ufs_pa_layer_attr)); |
| 2967 | } |
| 2968 | |
| 2969 | return ret; |
| 2970 | } |
| 2971 | |
| 2972 | /** |
| 2973 | * ufshcd_config_pwr_mode - configure a new power mode |
| 2974 | * @hba: per-adapter instance |
| 2975 | * @desired_pwr_mode: desired power configuration |
| 2976 | */ |
| 2977 | static int ufshcd_config_pwr_mode(struct ufs_hba *hba, |
| 2978 | struct ufs_pa_layer_attr *desired_pwr_mode) |
| 2979 | { |
| 2980 | struct ufs_pa_layer_attr final_params = { 0 }; |
| 2981 | int ret; |
| 2982 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 2983 | ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE, |
| 2984 | desired_pwr_mode, &final_params); |
| 2985 | |
| 2986 | if (ret) |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 2987 | memcpy(&final_params, desired_pwr_mode, sizeof(final_params)); |
| 2988 | |
| 2989 | ret = ufshcd_change_power_mode(hba, &final_params); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 2990 | |
| 2991 | return ret; |
| 2992 | } |
| 2993 | |
| 2994 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2995 | * ufshcd_complete_dev_init() - checks device readiness |
| 2996 | * hba: per-adapter instance |
| 2997 | * |
| 2998 | * Set fDeviceInit flag and poll until device toggles it. |
| 2999 | */ |
| 3000 | static int ufshcd_complete_dev_init(struct ufs_hba *hba) |
| 3001 | { |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 3002 | int i; |
| 3003 | int err; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3004 | bool flag_res = 1; |
| 3005 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 3006 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG, |
| 3007 | QUERY_FLAG_IDN_FDEVICEINIT, NULL); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3008 | if (err) { |
| 3009 | dev_err(hba->dev, |
| 3010 | "%s setting fDeviceInit flag failed with error %d\n", |
| 3011 | __func__, err); |
| 3012 | goto out; |
| 3013 | } |
| 3014 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 3015 | /* poll for max. 1000 iterations for fDeviceInit flag to clear */ |
| 3016 | for (i = 0; i < 1000 && !err && flag_res; i++) |
| 3017 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG, |
| 3018 | QUERY_FLAG_IDN_FDEVICEINIT, &flag_res); |
| 3019 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3020 | if (err) |
| 3021 | dev_err(hba->dev, |
| 3022 | "%s reading fDeviceInit flag failed with error %d\n", |
| 3023 | __func__, err); |
| 3024 | else if (flag_res) |
| 3025 | dev_err(hba->dev, |
| 3026 | "%s fDeviceInit was not cleared by the device\n", |
| 3027 | __func__); |
| 3028 | |
| 3029 | out: |
| 3030 | return err; |
| 3031 | } |
| 3032 | |
| 3033 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3034 | * ufshcd_make_hba_operational - Make UFS controller operational |
| 3035 | * @hba: per adapter instance |
| 3036 | * |
| 3037 | * To bring UFS host controller to operational state, |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 3038 | * 1. Enable required interrupts |
| 3039 | * 2. Configure interrupt aggregation |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 3040 | * 3. Program UTRL and UTMRL base address |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 3041 | * 4. Configure run-stop-registers |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3042 | * |
| 3043 | * Returns 0 on success, non-zero value on failure |
| 3044 | */ |
| 3045 | static int ufshcd_make_hba_operational(struct ufs_hba *hba) |
| 3046 | { |
| 3047 | int err = 0; |
| 3048 | u32 reg; |
| 3049 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3050 | /* Enable required interrupts */ |
| 3051 | ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS); |
| 3052 | |
| 3053 | /* Configure interrupt aggregation */ |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 3054 | if (ufshcd_is_intr_aggr_allowed(hba)) |
| 3055 | ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO); |
| 3056 | else |
| 3057 | ufshcd_disable_intr_aggr(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3058 | |
| 3059 | /* Configure UTRL and UTMRL base address registers */ |
| 3060 | ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr), |
| 3061 | REG_UTP_TRANSFER_REQ_LIST_BASE_L); |
| 3062 | ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr), |
| 3063 | REG_UTP_TRANSFER_REQ_LIST_BASE_H); |
| 3064 | ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr), |
| 3065 | REG_UTP_TASK_REQ_LIST_BASE_L); |
| 3066 | ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr), |
| 3067 | REG_UTP_TASK_REQ_LIST_BASE_H); |
| 3068 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3069 | /* |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 3070 | * Make sure base address and interrupt setup are updated before |
| 3071 | * enabling the run/stop registers below. |
| 3072 | */ |
| 3073 | wmb(); |
| 3074 | |
| 3075 | /* |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3076 | * UCRDY, UTMRLDY and UTRLRDY bits must be 1 |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3077 | */ |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 3078 | reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3079 | if (!(ufshcd_get_lists_status(reg))) { |
| 3080 | ufshcd_enable_run_stop_reg(hba); |
| 3081 | } else { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3082 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3083 | "Host controller not ready to process requests"); |
| 3084 | err = -EIO; |
| 3085 | goto out; |
| 3086 | } |
| 3087 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3088 | out: |
| 3089 | return err; |
| 3090 | } |
| 3091 | |
| 3092 | /** |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 3093 | * ufshcd_hba_stop - Send controller to reset state |
| 3094 | * @hba: per adapter instance |
| 3095 | * @can_sleep: perform sleep or just spin |
| 3096 | */ |
| 3097 | static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep) |
| 3098 | { |
| 3099 | int err; |
| 3100 | |
| 3101 | ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE); |
| 3102 | err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE, |
| 3103 | CONTROLLER_ENABLE, CONTROLLER_DISABLE, |
| 3104 | 10, 1, can_sleep); |
| 3105 | if (err) |
| 3106 | dev_err(hba->dev, "%s: Controller disable failed\n", __func__); |
| 3107 | } |
| 3108 | |
| 3109 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3110 | * ufshcd_hba_enable - initialize the controller |
| 3111 | * @hba: per adapter instance |
| 3112 | * |
| 3113 | * The controller resets itself and controller firmware initialization |
| 3114 | * sequence kicks off. When controller is ready it will set |
| 3115 | * the Host Controller Enable bit to 1. |
| 3116 | * |
| 3117 | * Returns 0 on success, non-zero value on failure |
| 3118 | */ |
| 3119 | static int ufshcd_hba_enable(struct ufs_hba *hba) |
| 3120 | { |
| 3121 | int retry; |
| 3122 | |
| 3123 | /* |
| 3124 | * msleep of 1 and 5 used in this function might result in msleep(20), |
| 3125 | * but it was necessary to send the UFS FPGA to reset mode during |
| 3126 | * development and testing of this driver. msleep can be changed to |
| 3127 | * mdelay and retry count can be reduced based on the controller. |
| 3128 | */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 3129 | if (!ufshcd_is_hba_active(hba)) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3130 | /* change controller state to "reset state" */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 3131 | ufshcd_hba_stop(hba, true); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3132 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3133 | /* UniPro link is disabled at this point */ |
| 3134 | ufshcd_set_link_off(hba); |
| 3135 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 3136 | ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 3137 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3138 | /* start controller initialization sequence */ |
| 3139 | ufshcd_hba_start(hba); |
| 3140 | |
| 3141 | /* |
| 3142 | * To initialize a UFS host controller HCE bit must be set to 1. |
| 3143 | * During initialization the HCE bit value changes from 1->0->1. |
| 3144 | * When the host controller completes initialization sequence |
| 3145 | * it sets the value of HCE bit to 1. The same HCE bit is read back |
| 3146 | * to check if the controller has completed initialization sequence. |
| 3147 | * So without this delay the value HCE = 1, set in the previous |
| 3148 | * instruction might be read back. |
| 3149 | * This delay can be changed based on the controller. |
| 3150 | */ |
| 3151 | msleep(1); |
| 3152 | |
| 3153 | /* wait for the host controller to complete initialization */ |
| 3154 | retry = 10; |
| 3155 | while (ufshcd_is_hba_active(hba)) { |
| 3156 | if (retry) { |
| 3157 | retry--; |
| 3158 | } else { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3159 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3160 | "Controller enable failed\n"); |
| 3161 | return -EIO; |
| 3162 | } |
| 3163 | msleep(5); |
| 3164 | } |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 3165 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 3166 | /* enable UIC related interrupts */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3167 | ufshcd_enable_intr(hba, UFSHCD_UIC_MASK); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 3168 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 3169 | ufshcd_vops_hce_enable_notify(hba, POST_CHANGE); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 3170 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3171 | return 0; |
| 3172 | } |
| 3173 | |
Yaniv Gardi | 7ca38cf | 2015-05-17 18:54:59 +0300 | [diff] [blame] | 3174 | static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer) |
| 3175 | { |
| 3176 | int tx_lanes, i, err = 0; |
| 3177 | |
| 3178 | if (!peer) |
| 3179 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 3180 | &tx_lanes); |
| 3181 | else |
| 3182 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 3183 | &tx_lanes); |
| 3184 | for (i = 0; i < tx_lanes; i++) { |
| 3185 | if (!peer) |
| 3186 | err = ufshcd_dme_set(hba, |
| 3187 | UIC_ARG_MIB_SEL(TX_LCC_ENABLE, |
| 3188 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)), |
| 3189 | 0); |
| 3190 | else |
| 3191 | err = ufshcd_dme_peer_set(hba, |
| 3192 | UIC_ARG_MIB_SEL(TX_LCC_ENABLE, |
| 3193 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)), |
| 3194 | 0); |
| 3195 | if (err) { |
| 3196 | dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d", |
| 3197 | __func__, peer, i, err); |
| 3198 | break; |
| 3199 | } |
| 3200 | } |
| 3201 | |
| 3202 | return err; |
| 3203 | } |
| 3204 | |
| 3205 | static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba) |
| 3206 | { |
| 3207 | return ufshcd_disable_tx_lcc(hba, true); |
| 3208 | } |
| 3209 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3210 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3211 | * ufshcd_link_startup - Initialize unipro link startup |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3212 | * @hba: per adapter instance |
| 3213 | * |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3214 | * Returns 0 for success, non-zero in case of failure |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3215 | */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3216 | static int ufshcd_link_startup(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3217 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3218 | int ret; |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 3219 | int retries = DME_LINKSTARTUP_RETRIES; |
subhashj@codeaurora.org | c5fc946 | 2017-04-04 19:32:20 +0000 | [diff] [blame] | 3220 | bool link_startup_again = false; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3221 | |
subhashj@codeaurora.org | c5fc946 | 2017-04-04 19:32:20 +0000 | [diff] [blame] | 3222 | /* |
| 3223 | * If UFS device isn't active then we will have to issue link startup |
| 3224 | * 2 times to make sure the device state move to active. |
| 3225 | */ |
| 3226 | if (!ufshcd_is_ufs_dev_active(hba)) |
| 3227 | link_startup_again = true; |
| 3228 | |
| 3229 | link_startup: |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 3230 | do { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 3231 | ufshcd_vops_link_startup_notify(hba, PRE_CHANGE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3232 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 3233 | ret = ufshcd_dme_link_startup(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 3234 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 3235 | /* check if device is detected by inter-connect layer */ |
| 3236 | if (!ret && !ufshcd_is_device_present(hba)) { |
| 3237 | dev_err(hba->dev, "%s: Device not present\n", __func__); |
| 3238 | ret = -ENXIO; |
| 3239 | goto out; |
| 3240 | } |
| 3241 | |
| 3242 | /* |
| 3243 | * DME link lost indication is only received when link is up, |
| 3244 | * but we can't be sure if the link is up until link startup |
| 3245 | * succeeds. So reset the local Uni-Pro and try again. |
| 3246 | */ |
| 3247 | if (ret && ufshcd_hba_enable(hba)) |
| 3248 | goto out; |
| 3249 | } while (ret && retries--); |
| 3250 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3251 | if (ret) |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 3252 | /* failed to get the link up... retire */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3253 | goto out; |
| 3254 | |
subhashj@codeaurora.org | c5fc946 | 2017-04-04 19:32:20 +0000 | [diff] [blame] | 3255 | if (link_startup_again) { |
| 3256 | link_startup_again = false; |
| 3257 | retries = DME_LINKSTARTUP_RETRIES; |
| 3258 | goto link_startup; |
| 3259 | } |
| 3260 | |
Yaniv Gardi | 7ca38cf | 2015-05-17 18:54:59 +0300 | [diff] [blame] | 3261 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) { |
| 3262 | ret = ufshcd_disable_device_tx_lcc(hba); |
| 3263 | if (ret) |
| 3264 | goto out; |
| 3265 | } |
| 3266 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 3267 | /* Include any host controller configuration via UIC commands */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 3268 | ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE); |
| 3269 | if (ret) |
| 3270 | goto out; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 3271 | |
| 3272 | ret = ufshcd_make_hba_operational(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3273 | out: |
| 3274 | if (ret) |
| 3275 | dev_err(hba->dev, "link startup failed %d\n", ret); |
| 3276 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3277 | } |
| 3278 | |
| 3279 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3280 | * ufshcd_verify_dev_init() - Verify device initialization |
| 3281 | * @hba: per-adapter instance |
| 3282 | * |
| 3283 | * Send NOP OUT UPIU and wait for NOP IN response to check whether the |
| 3284 | * device Transport Protocol (UTP) layer is ready after a reset. |
| 3285 | * If the UTP layer at the device side is not initialized, it may |
| 3286 | * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT |
| 3287 | * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations. |
| 3288 | */ |
| 3289 | static int ufshcd_verify_dev_init(struct ufs_hba *hba) |
| 3290 | { |
| 3291 | int err = 0; |
| 3292 | int retries; |
| 3293 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3294 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3295 | mutex_lock(&hba->dev_cmd.lock); |
| 3296 | for (retries = NOP_OUT_RETRIES; retries > 0; retries--) { |
| 3297 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP, |
| 3298 | NOP_OUT_TIMEOUT); |
| 3299 | |
| 3300 | if (!err || err == -ETIMEDOUT) |
| 3301 | break; |
| 3302 | |
| 3303 | dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err); |
| 3304 | } |
| 3305 | mutex_unlock(&hba->dev_cmd.lock); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3306 | ufshcd_release(hba); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3307 | |
| 3308 | if (err) |
| 3309 | dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err); |
| 3310 | return err; |
| 3311 | } |
| 3312 | |
| 3313 | /** |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 3314 | * ufshcd_set_queue_depth - set lun queue depth |
| 3315 | * @sdev: pointer to SCSI device |
| 3316 | * |
| 3317 | * Read bLUQueueDepth value and activate scsi tagged command |
| 3318 | * queueing. For WLUN, queue depth is set to 1. For best-effort |
| 3319 | * cases (bLUQueueDepth = 0) the queue depth is set to a maximum |
| 3320 | * value that host can queue. |
| 3321 | */ |
| 3322 | static void ufshcd_set_queue_depth(struct scsi_device *sdev) |
| 3323 | { |
| 3324 | int ret = 0; |
| 3325 | u8 lun_qdepth; |
| 3326 | struct ufs_hba *hba; |
| 3327 | |
| 3328 | hba = shost_priv(sdev->host); |
| 3329 | |
| 3330 | lun_qdepth = hba->nutrs; |
| 3331 | ret = ufshcd_read_unit_desc_param(hba, |
| 3332 | ufshcd_scsi_to_upiu_lun(sdev->lun), |
| 3333 | UNIT_DESC_PARAM_LU_Q_DEPTH, |
| 3334 | &lun_qdepth, |
| 3335 | sizeof(lun_qdepth)); |
| 3336 | |
| 3337 | /* Some WLUN doesn't support unit descriptor */ |
| 3338 | if (ret == -EOPNOTSUPP) |
| 3339 | lun_qdepth = 1; |
| 3340 | else if (!lun_qdepth) |
| 3341 | /* eventually, we can figure out the real queue depth */ |
| 3342 | lun_qdepth = hba->nutrs; |
| 3343 | else |
| 3344 | lun_qdepth = min_t(int, lun_qdepth, hba->nutrs); |
| 3345 | |
| 3346 | dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n", |
| 3347 | __func__, lun_qdepth); |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 3348 | scsi_change_queue_depth(sdev, lun_qdepth); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 3349 | } |
| 3350 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3351 | /* |
| 3352 | * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR |
| 3353 | * @hba: per-adapter instance |
| 3354 | * @lun: UFS device lun id |
| 3355 | * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info |
| 3356 | * |
| 3357 | * Returns 0 in case of success and b_lu_write_protect status would be returned |
| 3358 | * @b_lu_write_protect parameter. |
| 3359 | * Returns -ENOTSUPP if reading b_lu_write_protect is not supported. |
| 3360 | * Returns -EINVAL in case of invalid parameters passed to this function. |
| 3361 | */ |
| 3362 | static int ufshcd_get_lu_wp(struct ufs_hba *hba, |
| 3363 | u8 lun, |
| 3364 | u8 *b_lu_write_protect) |
| 3365 | { |
| 3366 | int ret; |
| 3367 | |
| 3368 | if (!b_lu_write_protect) |
| 3369 | ret = -EINVAL; |
| 3370 | /* |
| 3371 | * According to UFS device spec, RPMB LU can't be write |
| 3372 | * protected so skip reading bLUWriteProtect parameter for |
| 3373 | * it. For other W-LUs, UNIT DESCRIPTOR is not available. |
| 3374 | */ |
| 3375 | else if (lun >= UFS_UPIU_MAX_GENERAL_LUN) |
| 3376 | ret = -ENOTSUPP; |
| 3377 | else |
| 3378 | ret = ufshcd_read_unit_desc_param(hba, |
| 3379 | lun, |
| 3380 | UNIT_DESC_PARAM_LU_WR_PROTECT, |
| 3381 | b_lu_write_protect, |
| 3382 | sizeof(*b_lu_write_protect)); |
| 3383 | return ret; |
| 3384 | } |
| 3385 | |
| 3386 | /** |
| 3387 | * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect |
| 3388 | * status |
| 3389 | * @hba: per-adapter instance |
| 3390 | * @sdev: pointer to SCSI device |
| 3391 | * |
| 3392 | */ |
| 3393 | static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba, |
| 3394 | struct scsi_device *sdev) |
| 3395 | { |
| 3396 | if (hba->dev_info.f_power_on_wp_en && |
| 3397 | !hba->dev_info.is_lu_power_on_wp) { |
| 3398 | u8 b_lu_write_protect; |
| 3399 | |
| 3400 | if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun), |
| 3401 | &b_lu_write_protect) && |
| 3402 | (b_lu_write_protect == UFS_LU_POWER_ON_WP)) |
| 3403 | hba->dev_info.is_lu_power_on_wp = true; |
| 3404 | } |
| 3405 | } |
| 3406 | |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 3407 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3408 | * ufshcd_slave_alloc - handle initial SCSI device configurations |
| 3409 | * @sdev: pointer to SCSI device |
| 3410 | * |
| 3411 | * Returns success |
| 3412 | */ |
| 3413 | static int ufshcd_slave_alloc(struct scsi_device *sdev) |
| 3414 | { |
| 3415 | struct ufs_hba *hba; |
| 3416 | |
| 3417 | hba = shost_priv(sdev->host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3418 | |
| 3419 | /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */ |
| 3420 | sdev->use_10_for_ms = 1; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3421 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 3422 | /* allow SCSI layer to restart the device in case of errors */ |
| 3423 | sdev->allow_restart = 1; |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 3424 | |
Sujit Reddy Thumma | b2a6c52 | 2014-07-01 12:22:38 +0300 | [diff] [blame] | 3425 | /* REPORT SUPPORTED OPERATION CODES is not supported */ |
| 3426 | sdev->no_report_opcodes = 1; |
| 3427 | |
Sujit Reddy Thumma | 9760af4 | 2018-01-24 09:52:35 +0530 | [diff] [blame] | 3428 | /* WRITE_SAME command is not supported */ |
| 3429 | sdev->no_write_same = 1; |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 3430 | |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 3431 | ufshcd_set_queue_depth(sdev); |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 3432 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3433 | ufshcd_get_lu_power_on_wp_status(hba, sdev); |
| 3434 | |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 3435 | return 0; |
| 3436 | } |
| 3437 | |
| 3438 | /** |
| 3439 | * ufshcd_change_queue_depth - change queue depth |
| 3440 | * @sdev: pointer to SCSI device |
| 3441 | * @depth: required depth to set |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 3442 | * |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 3443 | * 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] | 3444 | */ |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 3445 | 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] | 3446 | { |
| 3447 | struct ufs_hba *hba = shost_priv(sdev->host); |
| 3448 | |
| 3449 | if (depth > hba->nutrs) |
| 3450 | depth = hba->nutrs; |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 3451 | return scsi_change_queue_depth(sdev, depth); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3452 | } |
| 3453 | |
| 3454 | /** |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 3455 | * ufshcd_slave_configure - adjust SCSI device configurations |
| 3456 | * @sdev: pointer to SCSI device |
| 3457 | */ |
| 3458 | static int ufshcd_slave_configure(struct scsi_device *sdev) |
| 3459 | { |
| 3460 | struct request_queue *q = sdev->request_queue; |
| 3461 | |
| 3462 | blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1); |
| 3463 | blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX); |
| 3464 | |
| 3465 | return 0; |
| 3466 | } |
| 3467 | |
| 3468 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3469 | * ufshcd_slave_destroy - remove SCSI device configurations |
| 3470 | * @sdev: pointer to SCSI device |
| 3471 | */ |
| 3472 | static void ufshcd_slave_destroy(struct scsi_device *sdev) |
| 3473 | { |
| 3474 | struct ufs_hba *hba; |
| 3475 | |
| 3476 | hba = shost_priv(sdev->host); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 3477 | /* Drop the reference as it won't be needed anymore */ |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 3478 | if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) { |
| 3479 | unsigned long flags; |
| 3480 | |
| 3481 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 3482 | hba->sdev_ufs_device = NULL; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 3483 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3484 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3485 | } |
| 3486 | |
| 3487 | /** |
| 3488 | * ufshcd_task_req_compl - handle task management request completion |
| 3489 | * @hba: per adapter instance |
| 3490 | * @index: index of the completed request |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 3491 | * @resp: task management service response |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3492 | * |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 3493 | * Returns non-zero value on error, zero on success |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3494 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 3495 | static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3496 | { |
| 3497 | struct utp_task_req_desc *task_req_descp; |
| 3498 | struct utp_upiu_task_rsp *task_rsp_upiup; |
| 3499 | unsigned long flags; |
| 3500 | int ocs_value; |
| 3501 | int task_result; |
| 3502 | |
| 3503 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 3504 | |
| 3505 | /* Clear completed tasks from outstanding_tasks */ |
| 3506 | __clear_bit(index, &hba->outstanding_tasks); |
| 3507 | |
| 3508 | task_req_descp = hba->utmrdl_base_addr; |
| 3509 | ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]); |
| 3510 | |
| 3511 | if (ocs_value == OCS_SUCCESS) { |
| 3512 | task_rsp_upiup = (struct utp_upiu_task_rsp *) |
| 3513 | task_req_descp[index].task_rsp_upiu; |
Kiwoong Kim | 8794ee0 | 2016-09-09 08:22:22 +0900 | [diff] [blame] | 3514 | task_result = be32_to_cpu(task_rsp_upiup->output_param1); |
| 3515 | task_result = task_result & MASK_TM_SERVICE_RESP; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 3516 | if (resp) |
| 3517 | *resp = (u8)task_result; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3518 | } else { |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 3519 | dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", |
| 3520 | __func__, ocs_value); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3521 | } |
| 3522 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 3523 | |
| 3524 | return ocs_value; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3525 | } |
| 3526 | |
| 3527 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3528 | * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status |
| 3529 | * @lrb: pointer to local reference block of completed command |
| 3530 | * @scsi_status: SCSI command status |
| 3531 | * |
| 3532 | * Returns value base on SCSI command status |
| 3533 | */ |
| 3534 | static inline int |
| 3535 | ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status) |
| 3536 | { |
| 3537 | int result = 0; |
| 3538 | |
| 3539 | switch (scsi_status) { |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 3540 | case SAM_STAT_CHECK_CONDITION: |
| 3541 | ufshcd_copy_sense_data(lrbp); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3542 | case SAM_STAT_GOOD: |
| 3543 | result |= DID_OK << 16 | |
| 3544 | COMMAND_COMPLETE << 8 | |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 3545 | scsi_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3546 | break; |
| 3547 | case SAM_STAT_TASK_SET_FULL: |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 3548 | case SAM_STAT_BUSY: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3549 | case SAM_STAT_TASK_ABORTED: |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 3550 | ufshcd_copy_sense_data(lrbp); |
| 3551 | result |= scsi_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3552 | break; |
| 3553 | default: |
| 3554 | result |= DID_ERROR << 16; |
| 3555 | break; |
| 3556 | } /* end of switch */ |
| 3557 | |
| 3558 | return result; |
| 3559 | } |
| 3560 | |
| 3561 | /** |
| 3562 | * ufshcd_transfer_rsp_status - Get overall status of the response |
| 3563 | * @hba: per adapter instance |
| 3564 | * @lrb: pointer to local reference block of completed command |
| 3565 | * |
| 3566 | * Returns result of the command to notify SCSI midlayer |
| 3567 | */ |
| 3568 | static inline int |
| 3569 | ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 3570 | { |
| 3571 | int result = 0; |
| 3572 | int scsi_status; |
| 3573 | int ocs; |
| 3574 | |
| 3575 | /* overall command status of utrd */ |
| 3576 | ocs = ufshcd_get_tr_ocs(lrbp); |
| 3577 | |
| 3578 | switch (ocs) { |
| 3579 | case OCS_SUCCESS: |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3580 | result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3581 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3582 | switch (result) { |
| 3583 | case UPIU_TRANSACTION_RESPONSE: |
| 3584 | /* |
| 3585 | * get the response UPIU result to extract |
| 3586 | * the SCSI command status |
| 3587 | */ |
| 3588 | result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr); |
| 3589 | |
| 3590 | /* |
| 3591 | * get the result based on SCSI status response |
| 3592 | * to notify the SCSI midlayer of the command status |
| 3593 | */ |
| 3594 | scsi_status = result & MASK_SCSI_STATUS; |
| 3595 | result = ufshcd_scsi_cmd_status(lrbp, scsi_status); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3596 | |
Yaniv Gardi | f05ac2e | 2016-02-01 15:02:42 +0200 | [diff] [blame] | 3597 | /* |
| 3598 | * Currently we are only supporting BKOPs exception |
| 3599 | * events hence we can ignore BKOPs exception event |
| 3600 | * during power management callbacks. BKOPs exception |
| 3601 | * event is not expected to be raised in runtime suspend |
| 3602 | * callback as it allows the urgent bkops. |
| 3603 | * During system suspend, we are anyway forcefully |
| 3604 | * disabling the bkops and if urgent bkops is needed |
| 3605 | * it will be enabled on system resume. Long term |
| 3606 | * solution could be to abort the system suspend if |
| 3607 | * UFS device needs urgent BKOPs. |
| 3608 | */ |
| 3609 | if (!hba->pm_op_in_progress && |
| 3610 | ufshcd_is_exception_event(lrbp->ucd_rsp_ptr)) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3611 | schedule_work(&hba->eeh_work); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3612 | break; |
| 3613 | case UPIU_TRANSACTION_REJECT_UPIU: |
| 3614 | /* TODO: handle Reject UPIU Response */ |
| 3615 | result = DID_ERROR << 16; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3616 | dev_err(hba->dev, |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3617 | "Reject UPIU not fully implemented\n"); |
| 3618 | break; |
| 3619 | default: |
| 3620 | result = DID_ERROR << 16; |
| 3621 | dev_err(hba->dev, |
| 3622 | "Unexpected request response code = %x\n", |
| 3623 | result); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3624 | break; |
| 3625 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3626 | break; |
| 3627 | case OCS_ABORTED: |
| 3628 | result |= DID_ABORT << 16; |
| 3629 | break; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 3630 | case OCS_INVALID_COMMAND_STATUS: |
| 3631 | result |= DID_REQUEUE << 16; |
| 3632 | break; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3633 | case OCS_INVALID_CMD_TABLE_ATTR: |
| 3634 | case OCS_INVALID_PRDT_ATTR: |
| 3635 | case OCS_MISMATCH_DATA_BUF_SIZE: |
| 3636 | case OCS_MISMATCH_RESP_UPIU_SIZE: |
| 3637 | case OCS_PEER_COMM_FAILURE: |
| 3638 | case OCS_FATAL_ERROR: |
| 3639 | default: |
| 3640 | result |= DID_ERROR << 16; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3641 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3642 | "OCS error from controller = %x\n", ocs); |
| 3643 | break; |
| 3644 | } /* end of switch */ |
| 3645 | |
| 3646 | return result; |
| 3647 | } |
| 3648 | |
| 3649 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3650 | * ufshcd_uic_cmd_compl - handle completion of uic command |
| 3651 | * @hba: per adapter instance |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3652 | * @intr_status: interrupt status generated by the controller |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3653 | */ |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3654 | 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] | 3655 | { |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3656 | if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3657 | hba->active_uic_cmd->argument2 |= |
| 3658 | ufshcd_get_uic_cmd_result(hba); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3659 | hba->active_uic_cmd->argument3 = |
| 3660 | ufshcd_get_dme_attr_val(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3661 | complete(&hba->active_uic_cmd->done); |
| 3662 | } |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3663 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3664 | if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done) |
| 3665 | complete(hba->uic_async_done); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3666 | } |
| 3667 | |
| 3668 | /** |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 3669 | * __ufshcd_transfer_req_compl - handle SCSI and query command completion |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3670 | * @hba: per adapter instance |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 3671 | * @completed_reqs: requests to complete |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3672 | */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 3673 | static void __ufshcd_transfer_req_compl(struct ufs_hba *hba, |
| 3674 | unsigned long completed_reqs) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3675 | { |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3676 | struct ufshcd_lrb *lrbp; |
| 3677 | struct scsi_cmnd *cmd; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3678 | int result; |
| 3679 | int index; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 3680 | |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 3681 | for_each_set_bit(index, &completed_reqs, hba->nutrs) { |
| 3682 | lrbp = &hba->lrb[index]; |
| 3683 | cmd = lrbp->cmd; |
| 3684 | if (cmd) { |
| 3685 | result = ufshcd_transfer_rsp_status(hba, lrbp); |
| 3686 | scsi_dma_unmap(cmd); |
| 3687 | cmd->result = result; |
| 3688 | /* Mark completed command as NULL in LRB */ |
| 3689 | lrbp->cmd = NULL; |
| 3690 | clear_bit_unlock(index, &hba->lrb_in_use); |
| 3691 | /* Do not touch lrbp after scsi done */ |
| 3692 | cmd->scsi_done(cmd); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3693 | __ufshcd_release(hba); |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 3694 | } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE || |
| 3695 | lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) { |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 3696 | if (hba->dev_cmd.complete) |
| 3697 | complete(hba->dev_cmd.complete); |
| 3698 | } |
| 3699 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3700 | |
| 3701 | /* clear corresponding bits of completed commands */ |
| 3702 | hba->outstanding_reqs ^= completed_reqs; |
| 3703 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 3704 | ufshcd_clk_scaling_update_busy(hba); |
| 3705 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3706 | /* we might have free'd some tags above */ |
| 3707 | wake_up(&hba->dev_cmd.tag_wq); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3708 | } |
| 3709 | |
| 3710 | /** |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 3711 | * ufshcd_transfer_req_compl - handle SCSI and query command completion |
| 3712 | * @hba: per adapter instance |
| 3713 | */ |
| 3714 | static void ufshcd_transfer_req_compl(struct ufs_hba *hba) |
| 3715 | { |
| 3716 | unsigned long completed_reqs; |
| 3717 | u32 tr_doorbell; |
| 3718 | |
| 3719 | /* Resetting interrupt aggregation counters first and reading the |
| 3720 | * DOOR_BELL afterward allows us to handle all the completed requests. |
| 3721 | * In order to prevent other interrupts starvation the DB is read once |
| 3722 | * after reset. The down side of this solution is the possibility of |
| 3723 | * false interrupt if device completes another request after resetting |
| 3724 | * aggregation and before reading the DB. |
| 3725 | */ |
| 3726 | if (ufshcd_is_intr_aggr_allowed(hba)) |
| 3727 | ufshcd_reset_intr_aggr(hba); |
| 3728 | |
| 3729 | tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 3730 | completed_reqs = tr_doorbell ^ hba->outstanding_reqs; |
| 3731 | |
| 3732 | __ufshcd_transfer_req_compl(hba, completed_reqs); |
| 3733 | } |
| 3734 | |
| 3735 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3736 | * ufshcd_disable_ee - disable exception event |
| 3737 | * @hba: per-adapter instance |
| 3738 | * @mask: exception event to disable |
| 3739 | * |
| 3740 | * Disables exception event in the device so that the EVENT_ALERT |
| 3741 | * bit is not set. |
| 3742 | * |
| 3743 | * Returns zero on success, non-zero error value on failure. |
| 3744 | */ |
| 3745 | static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask) |
| 3746 | { |
| 3747 | int err = 0; |
| 3748 | u32 val; |
| 3749 | |
| 3750 | if (!(hba->ee_ctrl_mask & mask)) |
| 3751 | goto out; |
| 3752 | |
| 3753 | val = hba->ee_ctrl_mask & ~mask; |
| 3754 | val &= 0xFFFF; /* 2 bytes */ |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 3755 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3756 | QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val); |
| 3757 | if (!err) |
| 3758 | hba->ee_ctrl_mask &= ~mask; |
| 3759 | out: |
| 3760 | return err; |
| 3761 | } |
| 3762 | |
| 3763 | /** |
| 3764 | * ufshcd_enable_ee - enable exception event |
| 3765 | * @hba: per-adapter instance |
| 3766 | * @mask: exception event to enable |
| 3767 | * |
| 3768 | * Enable corresponding exception event in the device to allow |
| 3769 | * device to alert host in critical scenarios. |
| 3770 | * |
| 3771 | * Returns zero on success, non-zero error value on failure. |
| 3772 | */ |
| 3773 | static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask) |
| 3774 | { |
| 3775 | int err = 0; |
| 3776 | u32 val; |
| 3777 | |
| 3778 | if (hba->ee_ctrl_mask & mask) |
| 3779 | goto out; |
| 3780 | |
| 3781 | val = hba->ee_ctrl_mask | mask; |
| 3782 | val &= 0xFFFF; /* 2 bytes */ |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 3783 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3784 | QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val); |
| 3785 | if (!err) |
| 3786 | hba->ee_ctrl_mask |= mask; |
| 3787 | out: |
| 3788 | return err; |
| 3789 | } |
| 3790 | |
| 3791 | /** |
| 3792 | * ufshcd_enable_auto_bkops - Allow device managed BKOPS |
| 3793 | * @hba: per-adapter instance |
| 3794 | * |
| 3795 | * Allow device to manage background operations on its own. Enabling |
| 3796 | * this might lead to inconsistent latencies during normal data transfers |
| 3797 | * as the device is allowed to manage its own way of handling background |
| 3798 | * operations. |
| 3799 | * |
| 3800 | * Returns zero on success, non-zero on failure. |
| 3801 | */ |
| 3802 | static int ufshcd_enable_auto_bkops(struct ufs_hba *hba) |
| 3803 | { |
| 3804 | int err = 0; |
| 3805 | |
| 3806 | if (hba->auto_bkops_enabled) |
| 3807 | goto out; |
| 3808 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 3809 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3810 | QUERY_FLAG_IDN_BKOPS_EN, NULL); |
| 3811 | if (err) { |
| 3812 | dev_err(hba->dev, "%s: failed to enable bkops %d\n", |
| 3813 | __func__, err); |
| 3814 | goto out; |
| 3815 | } |
| 3816 | |
| 3817 | hba->auto_bkops_enabled = true; |
| 3818 | |
| 3819 | /* No need of URGENT_BKOPS exception from the device */ |
| 3820 | err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 3821 | if (err) |
| 3822 | dev_err(hba->dev, "%s: failed to disable exception event %d\n", |
| 3823 | __func__, err); |
| 3824 | out: |
| 3825 | return err; |
| 3826 | } |
| 3827 | |
| 3828 | /** |
| 3829 | * ufshcd_disable_auto_bkops - block device in doing background operations |
| 3830 | * @hba: per-adapter instance |
| 3831 | * |
| 3832 | * Disabling background operations improves command response latency but |
| 3833 | * has drawback of device moving into critical state where the device is |
| 3834 | * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the |
| 3835 | * host is idle so that BKOPS are managed effectively without any negative |
| 3836 | * impacts. |
| 3837 | * |
| 3838 | * Returns zero on success, non-zero on failure. |
| 3839 | */ |
| 3840 | static int ufshcd_disable_auto_bkops(struct ufs_hba *hba) |
| 3841 | { |
| 3842 | int err = 0; |
| 3843 | |
| 3844 | if (!hba->auto_bkops_enabled) |
| 3845 | goto out; |
| 3846 | |
| 3847 | /* |
| 3848 | * If host assisted BKOPs is to be enabled, make sure |
| 3849 | * urgent bkops exception is allowed. |
| 3850 | */ |
| 3851 | err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 3852 | if (err) { |
| 3853 | dev_err(hba->dev, "%s: failed to enable exception event %d\n", |
| 3854 | __func__, err); |
| 3855 | goto out; |
| 3856 | } |
| 3857 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 3858 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3859 | QUERY_FLAG_IDN_BKOPS_EN, NULL); |
| 3860 | if (err) { |
| 3861 | dev_err(hba->dev, "%s: failed to disable bkops %d\n", |
| 3862 | __func__, err); |
| 3863 | ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 3864 | goto out; |
| 3865 | } |
| 3866 | |
| 3867 | hba->auto_bkops_enabled = false; |
| 3868 | out: |
| 3869 | return err; |
| 3870 | } |
| 3871 | |
| 3872 | /** |
subhashj@codeaurora.org | cfb09f0 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 3873 | * ufshcd_force_reset_auto_bkops - force reset auto bkops state |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3874 | * @hba: per adapter instance |
| 3875 | * |
| 3876 | * After a device reset the device may toggle the BKOPS_EN flag |
| 3877 | * to default value. The s/w tracking variables should be updated |
subhashj@codeaurora.org | cfb09f0 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 3878 | * as well. This function would change the auto-bkops state based on |
| 3879 | * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3880 | */ |
subhashj@codeaurora.org | cfb09f0 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 3881 | static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3882 | { |
subhashj@codeaurora.org | cfb09f0 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 3883 | if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) { |
| 3884 | hba->auto_bkops_enabled = false; |
| 3885 | hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS; |
| 3886 | ufshcd_enable_auto_bkops(hba); |
| 3887 | } else { |
| 3888 | hba->auto_bkops_enabled = true; |
| 3889 | hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS; |
| 3890 | ufshcd_disable_auto_bkops(hba); |
| 3891 | } |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3892 | } |
| 3893 | |
| 3894 | static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status) |
| 3895 | { |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 3896 | return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3897 | QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status); |
| 3898 | } |
| 3899 | |
| 3900 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3901 | * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status |
| 3902 | * @hba: per-adapter instance |
| 3903 | * @status: bkops_status value |
| 3904 | * |
| 3905 | * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn |
| 3906 | * flag in the device to permit background operations if the device |
| 3907 | * bkops_status is greater than or equal to "status" argument passed to |
| 3908 | * this function, disable otherwise. |
| 3909 | * |
| 3910 | * Returns 0 for success, non-zero in case of failure. |
| 3911 | * |
| 3912 | * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag |
| 3913 | * to know whether auto bkops is enabled or disabled after this function |
| 3914 | * returns control to it. |
| 3915 | */ |
| 3916 | static int ufshcd_bkops_ctrl(struct ufs_hba *hba, |
| 3917 | enum bkops_status status) |
| 3918 | { |
| 3919 | int err; |
| 3920 | u32 curr_status = 0; |
| 3921 | |
| 3922 | err = ufshcd_get_bkops_status(hba, &curr_status); |
| 3923 | if (err) { |
| 3924 | dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", |
| 3925 | __func__, err); |
| 3926 | goto out; |
| 3927 | } else if (curr_status > BKOPS_STATUS_MAX) { |
| 3928 | dev_err(hba->dev, "%s: invalid BKOPS status %d\n", |
| 3929 | __func__, curr_status); |
| 3930 | err = -EINVAL; |
| 3931 | goto out; |
| 3932 | } |
| 3933 | |
| 3934 | if (curr_status >= status) |
| 3935 | err = ufshcd_enable_auto_bkops(hba); |
| 3936 | else |
| 3937 | err = ufshcd_disable_auto_bkops(hba); |
| 3938 | out: |
| 3939 | return err; |
| 3940 | } |
| 3941 | |
| 3942 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3943 | * ufshcd_urgent_bkops - handle urgent bkops exception event |
| 3944 | * @hba: per-adapter instance |
| 3945 | * |
| 3946 | * Enable fBackgroundOpsEn flag in the device to permit background |
| 3947 | * operations. |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3948 | * |
| 3949 | * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled |
| 3950 | * and negative error value for any other failure. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3951 | */ |
| 3952 | static int ufshcd_urgent_bkops(struct ufs_hba *hba) |
| 3953 | { |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 3954 | return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3955 | } |
| 3956 | |
| 3957 | static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status) |
| 3958 | { |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 3959 | return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3960 | QUERY_ATTR_IDN_EE_STATUS, 0, 0, status); |
| 3961 | } |
| 3962 | |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 3963 | static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba) |
| 3964 | { |
| 3965 | int err; |
| 3966 | u32 curr_status = 0; |
| 3967 | |
| 3968 | if (hba->is_urgent_bkops_lvl_checked) |
| 3969 | goto enable_auto_bkops; |
| 3970 | |
| 3971 | err = ufshcd_get_bkops_status(hba, &curr_status); |
| 3972 | if (err) { |
| 3973 | dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", |
| 3974 | __func__, err); |
| 3975 | goto out; |
| 3976 | } |
| 3977 | |
| 3978 | /* |
| 3979 | * We are seeing that some devices are raising the urgent bkops |
| 3980 | * exception events even when BKOPS status doesn't indicate performace |
| 3981 | * impacted or critical. Handle these device by determining their urgent |
| 3982 | * bkops status at runtime. |
| 3983 | */ |
| 3984 | if (curr_status < BKOPS_STATUS_PERF_IMPACT) { |
| 3985 | dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n", |
| 3986 | __func__, curr_status); |
| 3987 | /* update the current status as the urgent bkops level */ |
| 3988 | hba->urgent_bkops_lvl = curr_status; |
| 3989 | hba->is_urgent_bkops_lvl_checked = true; |
| 3990 | } |
| 3991 | |
| 3992 | enable_auto_bkops: |
| 3993 | err = ufshcd_enable_auto_bkops(hba); |
| 3994 | out: |
| 3995 | if (err < 0) |
| 3996 | dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n", |
| 3997 | __func__, err); |
| 3998 | } |
| 3999 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4000 | /** |
| 4001 | * ufshcd_exception_event_handler - handle exceptions raised by device |
| 4002 | * @work: pointer to work data |
| 4003 | * |
| 4004 | * Read bExceptionEventStatus attribute from the device and handle the |
| 4005 | * exception event accordingly. |
| 4006 | */ |
| 4007 | static void ufshcd_exception_event_handler(struct work_struct *work) |
| 4008 | { |
| 4009 | struct ufs_hba *hba; |
| 4010 | int err; |
| 4011 | u32 status = 0; |
| 4012 | hba = container_of(work, struct ufs_hba, eeh_work); |
| 4013 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 4014 | pm_runtime_get_sync(hba->dev); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4015 | err = ufshcd_get_ee_status(hba, &status); |
| 4016 | if (err) { |
| 4017 | dev_err(hba->dev, "%s: failed to get exception status %d\n", |
| 4018 | __func__, err); |
| 4019 | goto out; |
| 4020 | } |
| 4021 | |
| 4022 | status &= hba->ee_ctrl_mask; |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 4023 | |
| 4024 | if (status & MASK_EE_URGENT_BKOPS) |
| 4025 | ufshcd_bkops_exception_event_handler(hba); |
| 4026 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4027 | out: |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 4028 | pm_runtime_put_sync(hba->dev); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4029 | return; |
| 4030 | } |
| 4031 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4032 | /* Complete requests that have door-bell cleared */ |
| 4033 | static void ufshcd_complete_requests(struct ufs_hba *hba) |
| 4034 | { |
| 4035 | ufshcd_transfer_req_compl(hba); |
| 4036 | ufshcd_tmc_handler(hba); |
| 4037 | } |
| 4038 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4039 | /** |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 4040 | * ufshcd_quirk_dl_nac_errors - This function checks if error handling is |
| 4041 | * to recover from the DL NAC errors or not. |
| 4042 | * @hba: per-adapter instance |
| 4043 | * |
| 4044 | * Returns true if error handling is required, false otherwise |
| 4045 | */ |
| 4046 | static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba) |
| 4047 | { |
| 4048 | unsigned long flags; |
| 4049 | bool err_handling = true; |
| 4050 | |
| 4051 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4052 | /* |
| 4053 | * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the |
| 4054 | * device fatal error and/or DL NAC & REPLAY timeout errors. |
| 4055 | */ |
| 4056 | if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR)) |
| 4057 | goto out; |
| 4058 | |
| 4059 | if ((hba->saved_err & DEVICE_FATAL_ERROR) || |
| 4060 | ((hba->saved_err & UIC_ERROR) && |
| 4061 | (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR))) |
| 4062 | goto out; |
| 4063 | |
| 4064 | if ((hba->saved_err & UIC_ERROR) && |
| 4065 | (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) { |
| 4066 | int err; |
| 4067 | /* |
| 4068 | * wait for 50ms to see if we can get any other errors or not. |
| 4069 | */ |
| 4070 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4071 | msleep(50); |
| 4072 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4073 | |
| 4074 | /* |
| 4075 | * now check if we have got any other severe errors other than |
| 4076 | * DL NAC error? |
| 4077 | */ |
| 4078 | if ((hba->saved_err & INT_FATAL_ERRORS) || |
| 4079 | ((hba->saved_err & UIC_ERROR) && |
| 4080 | (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR))) |
| 4081 | goto out; |
| 4082 | |
| 4083 | /* |
| 4084 | * As DL NAC is the only error received so far, send out NOP |
| 4085 | * command to confirm if link is still active or not. |
| 4086 | * - If we don't get any response then do error recovery. |
| 4087 | * - If we get response then clear the DL NAC error bit. |
| 4088 | */ |
| 4089 | |
| 4090 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4091 | err = ufshcd_verify_dev_init(hba); |
| 4092 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4093 | |
| 4094 | if (err) |
| 4095 | goto out; |
| 4096 | |
| 4097 | /* Link seems to be alive hence ignore the DL NAC errors */ |
| 4098 | if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR) |
| 4099 | hba->saved_err &= ~UIC_ERROR; |
| 4100 | /* clear NAC error */ |
| 4101 | hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR; |
| 4102 | if (!hba->saved_uic_err) { |
| 4103 | err_handling = false; |
| 4104 | goto out; |
| 4105 | } |
| 4106 | } |
| 4107 | out: |
| 4108 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4109 | return err_handling; |
| 4110 | } |
| 4111 | |
| 4112 | /** |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4113 | * ufshcd_err_handler - handle UFS errors that require s/w attention |
| 4114 | * @work: pointer to work structure |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4115 | */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4116 | static void ufshcd_err_handler(struct work_struct *work) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4117 | { |
| 4118 | struct ufs_hba *hba; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4119 | unsigned long flags; |
| 4120 | u32 err_xfer = 0; |
| 4121 | u32 err_tm = 0; |
| 4122 | int err = 0; |
| 4123 | int tag; |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4124 | bool needs_reset = false; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4125 | |
| 4126 | hba = container_of(work, struct ufs_hba, eh_work); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4127 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 4128 | pm_runtime_get_sync(hba->dev); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4129 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4130 | |
| 4131 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4132 | if (hba->ufshcd_state == UFSHCD_STATE_RESET) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4133 | goto out; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4134 | |
| 4135 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 4136 | ufshcd_set_eh_in_progress(hba); |
| 4137 | |
| 4138 | /* Complete requests that have door-bell cleared by h/w */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4139 | ufshcd_complete_requests(hba); |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 4140 | |
| 4141 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) { |
| 4142 | bool ret; |
| 4143 | |
| 4144 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4145 | /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */ |
| 4146 | ret = ufshcd_quirk_dl_nac_errors(hba); |
| 4147 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4148 | if (!ret) |
| 4149 | goto skip_err_handling; |
| 4150 | } |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4151 | if ((hba->saved_err & INT_FATAL_ERRORS) || |
| 4152 | ((hba->saved_err & UIC_ERROR) && |
| 4153 | (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR | |
| 4154 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR | |
| 4155 | UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))) |
| 4156 | needs_reset = true; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4157 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4158 | /* |
| 4159 | * if host reset is required then skip clearing the pending |
| 4160 | * transfers forcefully because they will automatically get |
| 4161 | * cleared after link startup. |
| 4162 | */ |
| 4163 | if (needs_reset) |
| 4164 | goto skip_pending_xfer_clear; |
| 4165 | |
| 4166 | /* release lock as clear command might sleep */ |
| 4167 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4168 | /* Clear pending transfer requests */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4169 | for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) { |
| 4170 | if (ufshcd_clear_cmd(hba, tag)) { |
| 4171 | err_xfer = true; |
| 4172 | goto lock_skip_pending_xfer_clear; |
| 4173 | } |
| 4174 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4175 | |
| 4176 | /* Clear pending task management requests */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4177 | for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) { |
| 4178 | if (ufshcd_clear_tm_cmd(hba, tag)) { |
| 4179 | err_tm = true; |
| 4180 | goto lock_skip_pending_xfer_clear; |
| 4181 | } |
| 4182 | } |
| 4183 | |
| 4184 | lock_skip_pending_xfer_clear: |
| 4185 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4186 | |
| 4187 | /* Complete the requests that are cleared by s/w */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4188 | ufshcd_complete_requests(hba); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4189 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4190 | if (err_xfer || err_tm) |
| 4191 | needs_reset = true; |
| 4192 | |
| 4193 | skip_pending_xfer_clear: |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4194 | /* Fatal errors need reset */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4195 | if (needs_reset) { |
| 4196 | unsigned long max_doorbells = (1UL << hba->nutrs) - 1; |
| 4197 | |
| 4198 | /* |
| 4199 | * ufshcd_reset_and_restore() does the link reinitialization |
| 4200 | * which will need atleast one empty doorbell slot to send the |
| 4201 | * device management commands (NOP and query commands). |
| 4202 | * If there is no slot empty at this moment then free up last |
| 4203 | * slot forcefully. |
| 4204 | */ |
| 4205 | if (hba->outstanding_reqs == max_doorbells) |
| 4206 | __ufshcd_transfer_req_compl(hba, |
| 4207 | (1UL << (hba->nutrs - 1))); |
| 4208 | |
| 4209 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4210 | err = ufshcd_reset_and_restore(hba); |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4211 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4212 | if (err) { |
| 4213 | dev_err(hba->dev, "%s: reset and restore failed\n", |
| 4214 | __func__); |
| 4215 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 4216 | } |
| 4217 | /* |
| 4218 | * Inform scsi mid-layer that we did reset and allow to handle |
| 4219 | * Unit Attention properly. |
| 4220 | */ |
| 4221 | scsi_report_bus_reset(hba->host, 0); |
| 4222 | hba->saved_err = 0; |
| 4223 | hba->saved_uic_err = 0; |
| 4224 | } |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4225 | |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 4226 | skip_err_handling: |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4227 | if (!needs_reset) { |
| 4228 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
| 4229 | if (hba->saved_err || hba->saved_uic_err) |
| 4230 | dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x", |
| 4231 | __func__, hba->saved_err, hba->saved_uic_err); |
| 4232 | } |
| 4233 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4234 | ufshcd_clear_eh_in_progress(hba); |
| 4235 | |
| 4236 | out: |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4237 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4238 | scsi_unblock_requests(hba->host); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4239 | ufshcd_release(hba); |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 4240 | pm_runtime_put_sync(hba->dev); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4241 | } |
| 4242 | |
| 4243 | /** |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4244 | * ufshcd_update_uic_error - check and set fatal UIC error flags. |
| 4245 | * @hba: per-adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4246 | */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4247 | static void ufshcd_update_uic_error(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4248 | { |
| 4249 | u32 reg; |
| 4250 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4251 | /* PA_INIT_ERROR is fatal and needs UIC reset */ |
| 4252 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER); |
| 4253 | if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT) |
| 4254 | hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 4255 | else if (hba->dev_quirks & |
| 4256 | UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) { |
| 4257 | if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED) |
| 4258 | hba->uic_error |= |
| 4259 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR; |
| 4260 | else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT) |
| 4261 | hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR; |
| 4262 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4263 | |
| 4264 | /* UIC NL/TL/DME errors needs software retry */ |
| 4265 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER); |
| 4266 | if (reg) |
| 4267 | hba->uic_error |= UFSHCD_UIC_NL_ERROR; |
| 4268 | |
| 4269 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER); |
| 4270 | if (reg) |
| 4271 | hba->uic_error |= UFSHCD_UIC_TL_ERROR; |
| 4272 | |
| 4273 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME); |
| 4274 | if (reg) |
| 4275 | hba->uic_error |= UFSHCD_UIC_DME_ERROR; |
| 4276 | |
| 4277 | dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n", |
| 4278 | __func__, hba->uic_error); |
| 4279 | } |
| 4280 | |
| 4281 | /** |
| 4282 | * ufshcd_check_errors - Check for errors that need s/w attention |
| 4283 | * @hba: per-adapter instance |
| 4284 | */ |
| 4285 | static void ufshcd_check_errors(struct ufs_hba *hba) |
| 4286 | { |
| 4287 | bool queue_eh_work = false; |
| 4288 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4289 | if (hba->errors & INT_FATAL_ERRORS) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4290 | queue_eh_work = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4291 | |
| 4292 | if (hba->errors & UIC_ERROR) { |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4293 | hba->uic_error = 0; |
| 4294 | ufshcd_update_uic_error(hba); |
| 4295 | if (hba->uic_error) |
| 4296 | queue_eh_work = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4297 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4298 | |
| 4299 | if (queue_eh_work) { |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4300 | /* |
| 4301 | * update the transfer error masks to sticky bits, let's do this |
| 4302 | * irrespective of current ufshcd_state. |
| 4303 | */ |
| 4304 | hba->saved_err |= hba->errors; |
| 4305 | hba->saved_uic_err |= hba->uic_error; |
| 4306 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4307 | /* handle fatal errors only when link is functional */ |
| 4308 | if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) { |
| 4309 | /* block commands from scsi mid-layer */ |
| 4310 | scsi_block_requests(hba->host); |
| 4311 | |
Zang Leigang | a17bddc | 2017-04-04 19:32:20 +0000 | [diff] [blame] | 4312 | hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4313 | schedule_work(&hba->eh_work); |
| 4314 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4315 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4316 | /* |
| 4317 | * if (!queue_eh_work) - |
| 4318 | * Other errors are either non-fatal where host recovers |
| 4319 | * itself without s/w intervention or errors that will be |
| 4320 | * handled by the SCSI core layer. |
| 4321 | */ |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4322 | } |
| 4323 | |
| 4324 | /** |
| 4325 | * ufshcd_tmc_handler - handle task management function completion |
| 4326 | * @hba: per adapter instance |
| 4327 | */ |
| 4328 | static void ufshcd_tmc_handler(struct ufs_hba *hba) |
| 4329 | { |
| 4330 | u32 tm_doorbell; |
| 4331 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 4332 | tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4333 | hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4334 | wake_up(&hba->tm_wq); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4335 | } |
| 4336 | |
| 4337 | /** |
| 4338 | * ufshcd_sl_intr - Interrupt service routine |
| 4339 | * @hba: per adapter instance |
| 4340 | * @intr_status: contains interrupts generated by the controller |
| 4341 | */ |
| 4342 | static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status) |
| 4343 | { |
| 4344 | hba->errors = UFSHCD_ERROR_MASK & intr_status; |
| 4345 | if (hba->errors) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4346 | ufshcd_check_errors(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4347 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4348 | if (intr_status & UFSHCD_UIC_MASK) |
| 4349 | ufshcd_uic_cmd_compl(hba, intr_status); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4350 | |
| 4351 | if (intr_status & UTP_TASK_REQ_COMPL) |
| 4352 | ufshcd_tmc_handler(hba); |
| 4353 | |
| 4354 | if (intr_status & UTP_TRANSFER_REQ_COMPL) |
| 4355 | ufshcd_transfer_req_compl(hba); |
| 4356 | } |
| 4357 | |
| 4358 | /** |
| 4359 | * ufshcd_intr - Main interrupt service routine |
| 4360 | * @irq: irq number |
| 4361 | * @__hba: pointer to adapter instance |
| 4362 | * |
| 4363 | * Returns IRQ_HANDLED - If interrupt is valid |
| 4364 | * IRQ_NONE - If invalid interrupt |
| 4365 | */ |
| 4366 | static irqreturn_t ufshcd_intr(int irq, void *__hba) |
| 4367 | { |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 4368 | u32 intr_status, enabled_intr_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4369 | irqreturn_t retval = IRQ_NONE; |
| 4370 | struct ufs_hba *hba = __hba; |
| 4371 | |
| 4372 | spin_lock(hba->host->host_lock); |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 4373 | intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 4374 | enabled_intr_status = |
| 4375 | intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4376 | |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 4377 | if (intr_status) |
Seungwon Jeon | 261ea45 | 2013-06-26 22:39:28 +0530 | [diff] [blame] | 4378 | ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 4379 | |
| 4380 | if (enabled_intr_status) { |
| 4381 | ufshcd_sl_intr(hba, enabled_intr_status); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4382 | retval = IRQ_HANDLED; |
| 4383 | } |
| 4384 | spin_unlock(hba->host->host_lock); |
| 4385 | return retval; |
| 4386 | } |
| 4387 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4388 | static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag) |
| 4389 | { |
| 4390 | int err = 0; |
| 4391 | u32 mask = 1 << tag; |
| 4392 | unsigned long flags; |
| 4393 | |
| 4394 | if (!test_bit(tag, &hba->outstanding_tasks)) |
| 4395 | goto out; |
| 4396 | |
| 4397 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4398 | ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR); |
| 4399 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4400 | |
| 4401 | /* poll for max. 1 sec to clear door bell register by h/w */ |
| 4402 | err = ufshcd_wait_for_register(hba, |
| 4403 | REG_UTP_TASK_REQ_DOOR_BELL, |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4404 | mask, 0, 1000, 1000, true); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4405 | out: |
| 4406 | return err; |
| 4407 | } |
| 4408 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4409 | /** |
| 4410 | * ufshcd_issue_tm_cmd - issues task management commands to controller |
| 4411 | * @hba: per adapter instance |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4412 | * @lun_id: LUN ID to which TM command is sent |
| 4413 | * @task_id: task ID to which the TM command is applicable |
| 4414 | * @tm_function: task management function opcode |
| 4415 | * @tm_response: task management service response return value |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4416 | * |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4417 | * Returns non-zero value on error, zero on success. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4418 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4419 | static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id, |
| 4420 | u8 tm_function, u8 *tm_response) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4421 | { |
| 4422 | struct utp_task_req_desc *task_req_descp; |
| 4423 | struct utp_upiu_task_req *task_req_upiup; |
| 4424 | struct Scsi_Host *host; |
| 4425 | unsigned long flags; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4426 | int free_slot; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4427 | int err; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4428 | int task_tag; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4429 | |
| 4430 | host = hba->host; |
| 4431 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4432 | /* |
| 4433 | * Get free slot, sleep if slots are unavailable. |
| 4434 | * Even though we use wait_event() which sleeps indefinitely, |
| 4435 | * the maximum wait time is bounded by %TM_CMD_TIMEOUT. |
| 4436 | */ |
| 4437 | 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] | 4438 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4439 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4440 | spin_lock_irqsave(host->host_lock, flags); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4441 | task_req_descp = hba->utmrdl_base_addr; |
| 4442 | task_req_descp += free_slot; |
| 4443 | |
| 4444 | /* Configure task request descriptor */ |
| 4445 | task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD); |
| 4446 | task_req_descp->header.dword_2 = |
| 4447 | cpu_to_le32(OCS_INVALID_COMMAND_STATUS); |
| 4448 | |
| 4449 | /* Configure task request UPIU */ |
| 4450 | task_req_upiup = |
| 4451 | (struct utp_upiu_task_req *) task_req_descp->task_req_upiu; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4452 | task_tag = hba->nutrs + free_slot; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4453 | task_req_upiup->header.dword_0 = |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4454 | UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0, |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4455 | lun_id, task_tag); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4456 | task_req_upiup->header.dword_1 = |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4457 | UPIU_HEADER_DWORD(0, tm_function, 0, 0); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4458 | /* |
| 4459 | * The host shall provide the same value for LUN field in the basic |
| 4460 | * header and for Input Parameter. |
| 4461 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4462 | task_req_upiup->input_param1 = cpu_to_be32(lun_id); |
| 4463 | task_req_upiup->input_param2 = cpu_to_be32(task_id); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4464 | |
| 4465 | /* send command to the controller */ |
| 4466 | __set_bit(free_slot, &hba->outstanding_tasks); |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 4467 | |
| 4468 | /* Make sure descriptors are ready before ringing the task doorbell */ |
| 4469 | wmb(); |
| 4470 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 4471 | ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4472 | |
| 4473 | spin_unlock_irqrestore(host->host_lock, flags); |
| 4474 | |
| 4475 | /* wait until the task management command is completed */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4476 | err = wait_event_timeout(hba->tm_wq, |
| 4477 | test_bit(free_slot, &hba->tm_condition), |
| 4478 | msecs_to_jiffies(TM_CMD_TIMEOUT)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4479 | if (!err) { |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4480 | dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n", |
| 4481 | __func__, tm_function); |
| 4482 | if (ufshcd_clear_tm_cmd(hba, free_slot)) |
| 4483 | dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n", |
| 4484 | __func__, free_slot); |
| 4485 | err = -ETIMEDOUT; |
| 4486 | } else { |
| 4487 | err = ufshcd_task_req_compl(hba, free_slot, tm_response); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4488 | } |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4489 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4490 | clear_bit(free_slot, &hba->tm_condition); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4491 | ufshcd_put_tm_slot(hba, free_slot); |
| 4492 | wake_up(&hba->tm_tag_wq); |
| 4493 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4494 | ufshcd_release(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4495 | return err; |
| 4496 | } |
| 4497 | |
| 4498 | /** |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4499 | * ufshcd_eh_device_reset_handler - device reset handler registered to |
| 4500 | * scsi layer. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4501 | * @cmd: SCSI command pointer |
| 4502 | * |
| 4503 | * Returns SUCCESS/FAILED |
| 4504 | */ |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4505 | static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4506 | { |
| 4507 | struct Scsi_Host *host; |
| 4508 | struct ufs_hba *hba; |
| 4509 | unsigned int tag; |
| 4510 | u32 pos; |
| 4511 | int err; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4512 | u8 resp = 0xF; |
| 4513 | struct ufshcd_lrb *lrbp; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4514 | unsigned long flags; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4515 | |
| 4516 | host = cmd->device->host; |
| 4517 | hba = shost_priv(host); |
| 4518 | tag = cmd->request->tag; |
| 4519 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4520 | lrbp = &hba->lrb[tag]; |
| 4521 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp); |
| 4522 | if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4523 | if (!err) |
| 4524 | err = resp; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4525 | goto out; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4526 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4527 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4528 | /* clear the commands that were pending for corresponding LUN */ |
| 4529 | for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) { |
| 4530 | if (hba->lrb[pos].lun == lrbp->lun) { |
| 4531 | err = ufshcd_clear_cmd(hba, pos); |
| 4532 | if (err) |
| 4533 | break; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4534 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4535 | } |
| 4536 | spin_lock_irqsave(host->host_lock, flags); |
| 4537 | ufshcd_transfer_req_compl(hba); |
| 4538 | spin_unlock_irqrestore(host->host_lock, flags); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4539 | out: |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4540 | if (!err) { |
| 4541 | err = SUCCESS; |
| 4542 | } else { |
| 4543 | dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); |
| 4544 | err = FAILED; |
| 4545 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4546 | return err; |
| 4547 | } |
| 4548 | |
| 4549 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4550 | * ufshcd_abort - abort a specific command |
| 4551 | * @cmd: SCSI command pointer |
| 4552 | * |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 4553 | * Abort the pending command in device by sending UFS_ABORT_TASK task management |
| 4554 | * command, and in host controller by clearing the door-bell register. There can |
| 4555 | * be race between controller sending the command to the device while abort is |
| 4556 | * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is |
| 4557 | * really issued and then try to abort it. |
| 4558 | * |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4559 | * Returns SUCCESS/FAILED |
| 4560 | */ |
| 4561 | static int ufshcd_abort(struct scsi_cmnd *cmd) |
| 4562 | { |
| 4563 | struct Scsi_Host *host; |
| 4564 | struct ufs_hba *hba; |
| 4565 | unsigned long flags; |
| 4566 | unsigned int tag; |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 4567 | int err = 0; |
| 4568 | int poll_cnt; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4569 | u8 resp = 0xF; |
| 4570 | struct ufshcd_lrb *lrbp; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4571 | u32 reg; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4572 | |
| 4573 | host = cmd->device->host; |
| 4574 | hba = shost_priv(host); |
| 4575 | tag = cmd->request->tag; |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 4576 | if (!ufshcd_valid_tag(hba, tag)) { |
| 4577 | dev_err(hba->dev, |
| 4578 | "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p", |
| 4579 | __func__, tag, cmd, cmd->request); |
| 4580 | BUG(); |
| 4581 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4582 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4583 | ufshcd_hold(hba, false); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4584 | reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 4585 | /* If command is already aborted/completed, return SUCCESS */ |
| 4586 | if (!(test_bit(tag, &hba->outstanding_reqs))) { |
| 4587 | dev_err(hba->dev, |
| 4588 | "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n", |
| 4589 | __func__, tag, hba->outstanding_reqs, reg); |
| 4590 | goto out; |
| 4591 | } |
| 4592 | |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4593 | if (!(reg & (1 << tag))) { |
| 4594 | dev_err(hba->dev, |
| 4595 | "%s: cmd was completed, but without a notifying intr, tag = %d", |
| 4596 | __func__, tag); |
| 4597 | } |
| 4598 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4599 | lrbp = &hba->lrb[tag]; |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 4600 | for (poll_cnt = 100; poll_cnt; poll_cnt--) { |
| 4601 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, |
| 4602 | UFS_QUERY_TASK, &resp); |
| 4603 | if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) { |
| 4604 | /* cmd pending in the device */ |
| 4605 | break; |
| 4606 | } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 4607 | /* |
| 4608 | * cmd not pending in the device, check if it is |
| 4609 | * in transition. |
| 4610 | */ |
| 4611 | reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 4612 | if (reg & (1 << tag)) { |
| 4613 | /* sleep for max. 200us to stabilize */ |
| 4614 | usleep_range(100, 200); |
| 4615 | continue; |
| 4616 | } |
| 4617 | /* command completed already */ |
| 4618 | goto out; |
| 4619 | } else { |
| 4620 | if (!err) |
| 4621 | err = resp; /* service response error */ |
| 4622 | goto out; |
| 4623 | } |
| 4624 | } |
| 4625 | |
| 4626 | if (!poll_cnt) { |
| 4627 | err = -EBUSY; |
| 4628 | goto out; |
| 4629 | } |
| 4630 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4631 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, |
| 4632 | UFS_ABORT_TASK, &resp); |
| 4633 | if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 4634 | if (!err) |
| 4635 | err = resp; /* service response error */ |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4636 | goto out; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4637 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4638 | |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 4639 | err = ufshcd_clear_cmd(hba, tag); |
| 4640 | if (err) |
| 4641 | goto out; |
| 4642 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4643 | scsi_dma_unmap(cmd); |
| 4644 | |
| 4645 | spin_lock_irqsave(host->host_lock, flags); |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 4646 | ufshcd_outstanding_req_clear(hba, tag); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4647 | hba->lrb[tag].cmd = NULL; |
| 4648 | spin_unlock_irqrestore(host->host_lock, flags); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4649 | |
| 4650 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 4651 | wake_up(&hba->dev_cmd.tag_wq); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4652 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4653 | out: |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 4654 | if (!err) { |
| 4655 | err = SUCCESS; |
| 4656 | } else { |
| 4657 | dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); |
| 4658 | err = FAILED; |
| 4659 | } |
| 4660 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4661 | /* |
| 4662 | * This ufshcd_release() corresponds to the original scsi cmd that got |
| 4663 | * aborted here (as we won't get any IRQ for it). |
| 4664 | */ |
| 4665 | ufshcd_release(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4666 | return err; |
| 4667 | } |
| 4668 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4669 | /** |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4670 | * ufshcd_host_reset_and_restore - reset and restore host controller |
| 4671 | * @hba: per-adapter instance |
| 4672 | * |
| 4673 | * Note that host controller reset may issue DME_RESET to |
| 4674 | * local and remote (device) Uni-Pro stack and the attributes |
| 4675 | * are reset to default state. |
| 4676 | * |
| 4677 | * Returns zero on success, non-zero on failure |
| 4678 | */ |
| 4679 | static int ufshcd_host_reset_and_restore(struct ufs_hba *hba) |
| 4680 | { |
| 4681 | int err; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4682 | unsigned long flags; |
| 4683 | |
| 4684 | /* Reset the host controller */ |
| 4685 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4686 | ufshcd_hba_stop(hba, false); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4687 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4688 | |
| 4689 | err = ufshcd_hba_enable(hba); |
| 4690 | if (err) |
| 4691 | goto out; |
| 4692 | |
| 4693 | /* Establish the link again and restore the device */ |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4694 | err = ufshcd_probe_hba(hba); |
| 4695 | |
| 4696 | if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL)) |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4697 | err = -EIO; |
| 4698 | out: |
| 4699 | if (err) |
| 4700 | dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err); |
| 4701 | |
| 4702 | return err; |
| 4703 | } |
| 4704 | |
| 4705 | /** |
| 4706 | * ufshcd_reset_and_restore - reset and re-initialize host/device |
| 4707 | * @hba: per-adapter instance |
| 4708 | * |
| 4709 | * Reset and recover device, host and re-establish link. This |
| 4710 | * is helpful to recover the communication in fatal error conditions. |
| 4711 | * |
| 4712 | * Returns zero on success, non-zero on failure |
| 4713 | */ |
| 4714 | static int ufshcd_reset_and_restore(struct ufs_hba *hba) |
| 4715 | { |
| 4716 | int err = 0; |
| 4717 | unsigned long flags; |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4718 | int retries = MAX_HOST_RESET_RETRIES; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4719 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4720 | do { |
| 4721 | err = ufshcd_host_reset_and_restore(hba); |
| 4722 | } while (err && --retries); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4723 | |
| 4724 | /* |
| 4725 | * After reset the door-bell might be cleared, complete |
| 4726 | * outstanding requests in s/w here. |
| 4727 | */ |
| 4728 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4729 | ufshcd_transfer_req_compl(hba); |
| 4730 | ufshcd_tmc_handler(hba); |
| 4731 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4732 | |
| 4733 | return err; |
| 4734 | } |
| 4735 | |
| 4736 | /** |
| 4737 | * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer |
| 4738 | * @cmd - SCSI command pointer |
| 4739 | * |
| 4740 | * Returns SUCCESS/FAILED |
| 4741 | */ |
| 4742 | static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd) |
| 4743 | { |
| 4744 | int err; |
| 4745 | unsigned long flags; |
| 4746 | struct ufs_hba *hba; |
| 4747 | |
| 4748 | hba = shost_priv(cmd->device->host); |
| 4749 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4750 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4751 | /* |
| 4752 | * Check if there is any race with fatal error handling. |
| 4753 | * If so, wait for it to complete. Even though fatal error |
| 4754 | * handling does reset and restore in some cases, don't assume |
| 4755 | * anything out of it. We are just avoiding race here. |
| 4756 | */ |
| 4757 | do { |
| 4758 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4759 | if (!(work_pending(&hba->eh_work) || |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4760 | hba->ufshcd_state == UFSHCD_STATE_RESET)) |
| 4761 | break; |
| 4762 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4763 | dev_dbg(hba->dev, "%s: reset in progress\n", __func__); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4764 | flush_work(&hba->eh_work); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4765 | } while (1); |
| 4766 | |
| 4767 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 4768 | ufshcd_set_eh_in_progress(hba); |
| 4769 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4770 | |
| 4771 | err = ufshcd_reset_and_restore(hba); |
| 4772 | |
| 4773 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4774 | if (!err) { |
| 4775 | err = SUCCESS; |
| 4776 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
| 4777 | } else { |
| 4778 | err = FAILED; |
| 4779 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 4780 | } |
| 4781 | ufshcd_clear_eh_in_progress(hba); |
| 4782 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4783 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4784 | ufshcd_release(hba); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4785 | return err; |
| 4786 | } |
| 4787 | |
| 4788 | /** |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 4789 | * ufshcd_get_max_icc_level - calculate the ICC level |
| 4790 | * @sup_curr_uA: max. current supported by the regulator |
| 4791 | * @start_scan: row at the desc table to start scan from |
| 4792 | * @buff: power descriptor buffer |
| 4793 | * |
| 4794 | * Returns calculated max ICC level for specific regulator |
| 4795 | */ |
| 4796 | static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff) |
| 4797 | { |
| 4798 | int i; |
| 4799 | int curr_uA; |
| 4800 | u16 data; |
| 4801 | u16 unit; |
| 4802 | |
| 4803 | for (i = start_scan; i >= 0; i--) { |
| 4804 | data = be16_to_cpu(*((u16 *)(buff + 2*i))); |
| 4805 | unit = (data & ATTR_ICC_LVL_UNIT_MASK) >> |
| 4806 | ATTR_ICC_LVL_UNIT_OFFSET; |
| 4807 | curr_uA = data & ATTR_ICC_LVL_VALUE_MASK; |
| 4808 | switch (unit) { |
| 4809 | case UFSHCD_NANO_AMP: |
| 4810 | curr_uA = curr_uA / 1000; |
| 4811 | break; |
| 4812 | case UFSHCD_MILI_AMP: |
| 4813 | curr_uA = curr_uA * 1000; |
| 4814 | break; |
| 4815 | case UFSHCD_AMP: |
| 4816 | curr_uA = curr_uA * 1000 * 1000; |
| 4817 | break; |
| 4818 | case UFSHCD_MICRO_AMP: |
| 4819 | default: |
| 4820 | break; |
| 4821 | } |
| 4822 | if (sup_curr_uA >= curr_uA) |
| 4823 | break; |
| 4824 | } |
| 4825 | if (i < 0) { |
| 4826 | i = 0; |
| 4827 | pr_err("%s: Couldn't find valid icc_level = %d", __func__, i); |
| 4828 | } |
| 4829 | |
| 4830 | return (u32)i; |
| 4831 | } |
| 4832 | |
| 4833 | /** |
| 4834 | * ufshcd_calc_icc_level - calculate the max ICC level |
| 4835 | * In case regulators are not initialized we'll return 0 |
| 4836 | * @hba: per-adapter instance |
| 4837 | * @desc_buf: power descriptor buffer to extract ICC levels from. |
| 4838 | * @len: length of desc_buff |
| 4839 | * |
| 4840 | * Returns calculated ICC level |
| 4841 | */ |
| 4842 | static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba, |
| 4843 | u8 *desc_buf, int len) |
| 4844 | { |
| 4845 | u32 icc_level = 0; |
| 4846 | |
| 4847 | if (!hba->vreg_info.vcc || !hba->vreg_info.vccq || |
| 4848 | !hba->vreg_info.vccq2) { |
| 4849 | dev_err(hba->dev, |
| 4850 | "%s: Regulator capability was not set, actvIccLevel=%d", |
| 4851 | __func__, icc_level); |
| 4852 | goto out; |
| 4853 | } |
| 4854 | |
| 4855 | if (hba->vreg_info.vcc) |
| 4856 | icc_level = ufshcd_get_max_icc_level( |
| 4857 | hba->vreg_info.vcc->max_uA, |
| 4858 | POWER_DESC_MAX_ACTV_ICC_LVLS - 1, |
| 4859 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]); |
| 4860 | |
| 4861 | if (hba->vreg_info.vccq) |
| 4862 | icc_level = ufshcd_get_max_icc_level( |
| 4863 | hba->vreg_info.vccq->max_uA, |
| 4864 | icc_level, |
| 4865 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]); |
| 4866 | |
| 4867 | if (hba->vreg_info.vccq2) |
| 4868 | icc_level = ufshcd_get_max_icc_level( |
| 4869 | hba->vreg_info.vccq2->max_uA, |
| 4870 | icc_level, |
| 4871 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]); |
| 4872 | out: |
| 4873 | return icc_level; |
| 4874 | } |
| 4875 | |
| 4876 | static void ufshcd_init_icc_levels(struct ufs_hba *hba) |
| 4877 | { |
| 4878 | int ret; |
Potomski, MichalX | e192845 | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 4879 | int buff_len = hba->desc_size.pwr_desc; |
| 4880 | u8 desc_buf[hba->desc_size.pwr_desc]; |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 4881 | |
| 4882 | ret = ufshcd_read_power_desc(hba, desc_buf, buff_len); |
| 4883 | if (ret) { |
| 4884 | dev_err(hba->dev, |
| 4885 | "%s: Failed reading power descriptor.len = %d ret = %d", |
| 4886 | __func__, buff_len, ret); |
| 4887 | return; |
| 4888 | } |
| 4889 | |
| 4890 | hba->init_prefetch_data.icc_level = |
| 4891 | ufshcd_find_max_sup_active_icc_level(hba, |
| 4892 | desc_buf, buff_len); |
| 4893 | dev_dbg(hba->dev, "%s: setting icc_level 0x%x", |
| 4894 | __func__, hba->init_prefetch_data.icc_level); |
| 4895 | |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 4896 | ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
| 4897 | QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, |
| 4898 | &hba->init_prefetch_data.icc_level); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 4899 | |
| 4900 | if (ret) |
| 4901 | dev_err(hba->dev, |
| 4902 | "%s: Failed configuring bActiveICCLevel = %d ret = %d", |
| 4903 | __func__, hba->init_prefetch_data.icc_level , ret); |
| 4904 | |
| 4905 | } |
| 4906 | |
| 4907 | /** |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 4908 | * ufshcd_scsi_add_wlus - Adds required W-LUs |
| 4909 | * @hba: per-adapter instance |
| 4910 | * |
| 4911 | * UFS device specification requires the UFS devices to support 4 well known |
| 4912 | * logical units: |
| 4913 | * "REPORT_LUNS" (address: 01h) |
| 4914 | * "UFS Device" (address: 50h) |
| 4915 | * "RPMB" (address: 44h) |
| 4916 | * "BOOT" (address: 30h) |
| 4917 | * UFS device's power management needs to be controlled by "POWER CONDITION" |
| 4918 | * field of SSU (START STOP UNIT) command. But this "power condition" field |
| 4919 | * will take effect only when its sent to "UFS device" well known logical unit |
| 4920 | * hence we require the scsi_device instance to represent this logical unit in |
| 4921 | * order for the UFS host driver to send the SSU command for power management. |
| 4922 | |
| 4923 | * We also require the scsi_device instance for "RPMB" (Replay Protected Memory |
| 4924 | * Block) LU so user space process can control this LU. User space may also |
| 4925 | * want to have access to BOOT LU. |
| 4926 | |
| 4927 | * This function adds scsi device instances for each of all well known LUs |
| 4928 | * (except "REPORT LUNS" LU). |
| 4929 | * |
| 4930 | * Returns zero on success (all required W-LUs are added successfully), |
| 4931 | * non-zero error value on failure (if failed to add any of the required W-LU). |
| 4932 | */ |
| 4933 | static int ufshcd_scsi_add_wlus(struct ufs_hba *hba) |
| 4934 | { |
| 4935 | int ret = 0; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 4936 | struct scsi_device *sdev_rpmb; |
| 4937 | struct scsi_device *sdev_boot; |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 4938 | |
| 4939 | hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0, |
| 4940 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL); |
| 4941 | if (IS_ERR(hba->sdev_ufs_device)) { |
| 4942 | ret = PTR_ERR(hba->sdev_ufs_device); |
| 4943 | hba->sdev_ufs_device = NULL; |
| 4944 | goto out; |
| 4945 | } |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 4946 | scsi_device_put(hba->sdev_ufs_device); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 4947 | |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 4948 | sdev_boot = __scsi_add_device(hba->host, 0, 0, |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 4949 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL); |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 4950 | if (IS_ERR(sdev_boot)) { |
| 4951 | ret = PTR_ERR(sdev_boot); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 4952 | goto remove_sdev_ufs_device; |
| 4953 | } |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 4954 | scsi_device_put(sdev_boot); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 4955 | |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 4956 | sdev_rpmb = __scsi_add_device(hba->host, 0, 0, |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 4957 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL); |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 4958 | if (IS_ERR(sdev_rpmb)) { |
| 4959 | ret = PTR_ERR(sdev_rpmb); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 4960 | goto remove_sdev_boot; |
| 4961 | } |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 4962 | scsi_device_put(sdev_rpmb); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 4963 | goto out; |
| 4964 | |
| 4965 | remove_sdev_boot: |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 4966 | scsi_remove_device(sdev_boot); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 4967 | remove_sdev_ufs_device: |
| 4968 | scsi_remove_device(hba->sdev_ufs_device); |
| 4969 | out: |
| 4970 | return ret; |
| 4971 | } |
| 4972 | |
Tomas Winkler | be4d66d | 2017-01-05 10:45:12 +0200 | [diff] [blame] | 4973 | static int ufs_get_device_desc(struct ufs_hba *hba, |
| 4974 | struct ufs_dev_desc *dev_desc) |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 4975 | { |
| 4976 | int err; |
| 4977 | u8 model_index; |
Potomski, MichalX | e192845 | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 4978 | u8 str_desc_buf[QUERY_DESC_MAX_SIZE + 1] = {0}; |
| 4979 | u8 desc_buf[hba->desc_size.dev_desc]; |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 4980 | |
Potomski, MichalX | e192845 | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 4981 | 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] | 4982 | if (err) { |
| 4983 | dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n", |
| 4984 | __func__, err); |
| 4985 | goto out; |
| 4986 | } |
| 4987 | |
| 4988 | /* |
| 4989 | * getting vendor (manufacturerID) and Bank Index in big endian |
| 4990 | * format |
| 4991 | */ |
Tomas Winkler | be4d66d | 2017-01-05 10:45:12 +0200 | [diff] [blame] | 4992 | dev_desc->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 | |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 4993 | desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1]; |
| 4994 | |
| 4995 | model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME]; |
| 4996 | |
| 4997 | err = ufshcd_read_string_desc(hba, model_index, str_desc_buf, |
Potomski, MichalX | e192845 | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 4998 | QUERY_DESC_MAX_SIZE, ASCII_STD); |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 4999 | if (err) { |
| 5000 | dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n", |
| 5001 | __func__, err); |
| 5002 | goto out; |
| 5003 | } |
| 5004 | |
Potomski, MichalX | e192845 | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 5005 | str_desc_buf[QUERY_DESC_MAX_SIZE] = '\0'; |
Tomas Winkler | be4d66d | 2017-01-05 10:45:12 +0200 | [diff] [blame] | 5006 | strlcpy(dev_desc->model, (str_desc_buf + QUERY_DESC_HDR_SIZE), |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 5007 | min_t(u8, str_desc_buf[QUERY_DESC_LENGTH_OFFSET], |
| 5008 | MAX_MODEL_LEN)); |
| 5009 | |
| 5010 | /* Null terminate the model string */ |
Tomas Winkler | be4d66d | 2017-01-05 10:45:12 +0200 | [diff] [blame] | 5011 | dev_desc->model[MAX_MODEL_LEN] = '\0'; |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 5012 | |
| 5013 | out: |
| 5014 | return err; |
| 5015 | } |
| 5016 | |
Tomas Winkler | be4d66d | 2017-01-05 10:45:12 +0200 | [diff] [blame] | 5017 | static void ufs_fixup_device_setup(struct ufs_hba *hba, |
| 5018 | struct ufs_dev_desc *dev_desc) |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 5019 | { |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 5020 | struct ufs_dev_fix *f; |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 5021 | |
| 5022 | for (f = ufs_fixups; f->quirk; f++) { |
Tomas Winkler | be4d66d | 2017-01-05 10:45:12 +0200 | [diff] [blame] | 5023 | if ((f->card.wmanufacturerid == dev_desc->wmanufacturerid || |
| 5024 | f->card.wmanufacturerid == UFS_ANY_VENDOR) && |
| 5025 | (STR_PRFX_EQUAL(f->card.model, dev_desc->model) || |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 5026 | !strcmp(f->card.model, UFS_ANY_MODEL))) |
| 5027 | hba->dev_quirks |= f->quirk; |
| 5028 | } |
| 5029 | } |
| 5030 | |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 5031 | /** |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 5032 | * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro |
| 5033 | * @hba: per-adapter instance |
| 5034 | * |
| 5035 | * PA_TActivate parameter can be tuned manually if UniPro version is less than |
| 5036 | * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's |
| 5037 | * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce |
| 5038 | * the hibern8 exit latency. |
| 5039 | * |
| 5040 | * Returns zero on success, non-zero error value on failure. |
| 5041 | */ |
| 5042 | static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba) |
| 5043 | { |
| 5044 | int ret = 0; |
| 5045 | u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate; |
| 5046 | |
| 5047 | ret = ufshcd_dme_peer_get(hba, |
| 5048 | UIC_ARG_MIB_SEL( |
| 5049 | RX_MIN_ACTIVATETIME_CAPABILITY, |
| 5050 | UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), |
| 5051 | &peer_rx_min_activatetime); |
| 5052 | if (ret) |
| 5053 | goto out; |
| 5054 | |
| 5055 | /* make sure proper unit conversion is applied */ |
| 5056 | tuned_pa_tactivate = |
| 5057 | ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US) |
| 5058 | / PA_TACTIVATE_TIME_UNIT_US); |
| 5059 | ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 5060 | tuned_pa_tactivate); |
| 5061 | |
| 5062 | out: |
| 5063 | return ret; |
| 5064 | } |
| 5065 | |
| 5066 | /** |
| 5067 | * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro |
| 5068 | * @hba: per-adapter instance |
| 5069 | * |
| 5070 | * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than |
| 5071 | * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's |
| 5072 | * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY. |
| 5073 | * This optimal value can help reduce the hibern8 exit latency. |
| 5074 | * |
| 5075 | * Returns zero on success, non-zero error value on failure. |
| 5076 | */ |
| 5077 | static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba) |
| 5078 | { |
| 5079 | int ret = 0; |
| 5080 | u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0; |
| 5081 | u32 max_hibern8_time, tuned_pa_hibern8time; |
| 5082 | |
| 5083 | ret = ufshcd_dme_get(hba, |
| 5084 | UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY, |
| 5085 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), |
| 5086 | &local_tx_hibern8_time_cap); |
| 5087 | if (ret) |
| 5088 | goto out; |
| 5089 | |
| 5090 | ret = ufshcd_dme_peer_get(hba, |
| 5091 | UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY, |
| 5092 | UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), |
| 5093 | &peer_rx_hibern8_time_cap); |
| 5094 | if (ret) |
| 5095 | goto out; |
| 5096 | |
| 5097 | max_hibern8_time = max(local_tx_hibern8_time_cap, |
| 5098 | peer_rx_hibern8_time_cap); |
| 5099 | /* make sure proper unit conversion is applied */ |
| 5100 | tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US) |
| 5101 | / PA_HIBERN8_TIME_UNIT_US); |
| 5102 | ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME), |
| 5103 | tuned_pa_hibern8time); |
| 5104 | out: |
| 5105 | return ret; |
| 5106 | } |
| 5107 | |
Subhash Jadavani | 3a87bcd | 2017-04-04 19:32:07 +0000 | [diff] [blame] | 5108 | /** |
| 5109 | * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is |
| 5110 | * less than device PA_TACTIVATE time. |
| 5111 | * @hba: per-adapter instance |
| 5112 | * |
| 5113 | * Some UFS devices require host PA_TACTIVATE to be lower than device |
| 5114 | * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk |
| 5115 | * for such devices. |
| 5116 | * |
| 5117 | * Returns zero on success, non-zero error value on failure. |
| 5118 | */ |
| 5119 | static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba) |
| 5120 | { |
| 5121 | int ret = 0; |
| 5122 | u32 granularity, peer_granularity; |
| 5123 | u32 pa_tactivate, peer_pa_tactivate; |
| 5124 | u32 pa_tactivate_us, peer_pa_tactivate_us; |
| 5125 | u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100}; |
| 5126 | |
| 5127 | ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY), |
| 5128 | &granularity); |
| 5129 | if (ret) |
| 5130 | goto out; |
| 5131 | |
| 5132 | ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY), |
| 5133 | &peer_granularity); |
| 5134 | if (ret) |
| 5135 | goto out; |
| 5136 | |
| 5137 | if ((granularity < PA_GRANULARITY_MIN_VAL) || |
| 5138 | (granularity > PA_GRANULARITY_MAX_VAL)) { |
| 5139 | dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d", |
| 5140 | __func__, granularity); |
| 5141 | return -EINVAL; |
| 5142 | } |
| 5143 | |
| 5144 | if ((peer_granularity < PA_GRANULARITY_MIN_VAL) || |
| 5145 | (peer_granularity > PA_GRANULARITY_MAX_VAL)) { |
| 5146 | dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d", |
| 5147 | __func__, peer_granularity); |
| 5148 | return -EINVAL; |
| 5149 | } |
| 5150 | |
| 5151 | ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate); |
| 5152 | if (ret) |
| 5153 | goto out; |
| 5154 | |
| 5155 | ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 5156 | &peer_pa_tactivate); |
| 5157 | if (ret) |
| 5158 | goto out; |
| 5159 | |
| 5160 | pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1]; |
| 5161 | peer_pa_tactivate_us = peer_pa_tactivate * |
| 5162 | gran_to_us_table[peer_granularity - 1]; |
| 5163 | |
| 5164 | if (pa_tactivate_us > peer_pa_tactivate_us) { |
| 5165 | u32 new_peer_pa_tactivate; |
| 5166 | |
| 5167 | new_peer_pa_tactivate = pa_tactivate_us / |
| 5168 | gran_to_us_table[peer_granularity - 1]; |
| 5169 | new_peer_pa_tactivate++; |
| 5170 | ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 5171 | new_peer_pa_tactivate); |
| 5172 | } |
| 5173 | |
| 5174 | out: |
| 5175 | return ret; |
| 5176 | } |
| 5177 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 5178 | static void ufshcd_tune_unipro_params(struct ufs_hba *hba) |
| 5179 | { |
| 5180 | if (ufshcd_is_unipro_pa_params_tuning_req(hba)) { |
| 5181 | ufshcd_tune_pa_tactivate(hba); |
| 5182 | ufshcd_tune_pa_hibern8time(hba); |
| 5183 | } |
| 5184 | |
| 5185 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE) |
| 5186 | /* set 1ms timeout for PA_TACTIVATE */ |
| 5187 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10); |
Subhash Jadavani | 3a87bcd | 2017-04-04 19:32:07 +0000 | [diff] [blame] | 5188 | |
| 5189 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE) |
| 5190 | ufshcd_quirk_tune_host_pa_tactivate(hba); |
Subhash Jadavani | bedc629 | 2017-04-04 19:32:13 +0000 | [diff] [blame] | 5191 | |
| 5192 | ufshcd_vops_apply_dev_quirks(hba); |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 5193 | } |
| 5194 | |
Potomski, MichalX | e192845 | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 5195 | static void ufshcd_init_desc_sizes(struct ufs_hba *hba) |
| 5196 | { |
| 5197 | int err; |
| 5198 | |
| 5199 | err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_DEVICE, 0, |
| 5200 | &hba->desc_size.dev_desc); |
| 5201 | if (err) |
| 5202 | hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE; |
| 5203 | |
| 5204 | err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_POWER, 0, |
| 5205 | &hba->desc_size.pwr_desc); |
| 5206 | if (err) |
| 5207 | hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE; |
| 5208 | |
| 5209 | err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_INTERCONNECT, 0, |
| 5210 | &hba->desc_size.interc_desc); |
| 5211 | if (err) |
| 5212 | hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE; |
| 5213 | |
| 5214 | err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_CONFIGURATION, 0, |
| 5215 | &hba->desc_size.conf_desc); |
| 5216 | if (err) |
| 5217 | hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE; |
| 5218 | |
| 5219 | err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_UNIT, 0, |
| 5220 | &hba->desc_size.unit_desc); |
| 5221 | if (err) |
| 5222 | hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE; |
| 5223 | |
| 5224 | err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_GEOMETRY, 0, |
| 5225 | &hba->desc_size.geom_desc); |
| 5226 | if (err) |
| 5227 | hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE; |
| 5228 | } |
| 5229 | |
| 5230 | static void ufshcd_def_desc_sizes(struct ufs_hba *hba) |
| 5231 | { |
| 5232 | hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE; |
| 5233 | hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE; |
| 5234 | hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE; |
| 5235 | hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE; |
| 5236 | hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE; |
| 5237 | hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE; |
| 5238 | } |
| 5239 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 5240 | /** |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 5241 | * ufshcd_probe_hba - probe hba to detect device and initialize |
| 5242 | * @hba: per-adapter instance |
| 5243 | * |
| 5244 | * Execute link-startup and verify device initialization |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5245 | */ |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 5246 | static int ufshcd_probe_hba(struct ufs_hba *hba) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5247 | { |
Tomas Winkler | be4d66d | 2017-01-05 10:45:12 +0200 | [diff] [blame] | 5248 | struct ufs_dev_desc card = {0}; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5249 | int ret; |
| 5250 | |
| 5251 | ret = ufshcd_link_startup(hba); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 5252 | if (ret) |
| 5253 | goto out; |
| 5254 | |
Yaniv Gardi | 5064636 | 2014-10-23 13:25:13 +0300 | [diff] [blame] | 5255 | ufshcd_init_pwr_info(hba); |
| 5256 | |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 5257 | /* set the default level for urgent bkops */ |
| 5258 | hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT; |
| 5259 | hba->is_urgent_bkops_lvl_checked = false; |
| 5260 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5261 | /* UniPro link is active now */ |
| 5262 | ufshcd_set_link_active(hba); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 5263 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 5264 | ret = ufshcd_verify_dev_init(hba); |
| 5265 | if (ret) |
| 5266 | goto out; |
| 5267 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 5268 | ret = ufshcd_complete_dev_init(hba); |
| 5269 | if (ret) |
| 5270 | goto out; |
| 5271 | |
Potomski, MichalX | e192845 | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 5272 | /* Init check for device descriptor sizes */ |
| 5273 | ufshcd_init_desc_sizes(hba); |
| 5274 | |
Tomas Winkler | be4d66d | 2017-01-05 10:45:12 +0200 | [diff] [blame] | 5275 | ret = ufs_get_device_desc(hba, &card); |
| 5276 | if (ret) { |
| 5277 | dev_err(hba->dev, "%s: Failed getting device info. err = %d\n", |
| 5278 | __func__, ret); |
| 5279 | goto out; |
| 5280 | } |
| 5281 | |
| 5282 | ufs_fixup_device_setup(hba, &card); |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 5283 | ufshcd_tune_unipro_params(hba); |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 5284 | |
| 5285 | ret = ufshcd_set_vccq_rail_unused(hba, |
| 5286 | (hba->dev_quirks & UFS_DEVICE_NO_VCCQ) ? true : false); |
| 5287 | if (ret) |
| 5288 | goto out; |
| 5289 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5290 | /* UFS device is also active now */ |
| 5291 | ufshcd_set_ufs_dev_active(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5292 | ufshcd_force_reset_auto_bkops(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5293 | hba->wlun_dev_clr_ua = true; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5294 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 5295 | if (ufshcd_get_max_pwr_mode(hba)) { |
| 5296 | dev_err(hba->dev, |
| 5297 | "%s: Failed getting max supported power mode\n", |
| 5298 | __func__); |
| 5299 | } else { |
| 5300 | ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info); |
| 5301 | if (ret) |
| 5302 | dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n", |
| 5303 | __func__, ret); |
| 5304 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5305 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 5306 | /* set the state as operational after switching to desired gear */ |
| 5307 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
Potomski, MichalX | e192845 | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 5308 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5309 | /* |
| 5310 | * If we are in error handling context or in power management callbacks |
| 5311 | * context, no need to scan the host |
| 5312 | */ |
| 5313 | if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) { |
| 5314 | bool flag; |
| 5315 | |
| 5316 | /* clear any previous UFS device information */ |
| 5317 | memset(&hba->dev_info, 0, sizeof(hba->dev_info)); |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 5318 | if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG, |
| 5319 | QUERY_FLAG_IDN_PWR_ON_WPE, &flag)) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5320 | hba->dev_info.f_power_on_wp_en = flag; |
| 5321 | |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 5322 | if (!hba->is_init_prefetch) |
| 5323 | ufshcd_init_icc_levels(hba); |
| 5324 | |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 5325 | /* Add required well known logical units to scsi mid layer */ |
| 5326 | if (ufshcd_scsi_add_wlus(hba)) |
| 5327 | goto out; |
| 5328 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5329 | scsi_scan_host(hba->host); |
| 5330 | pm_runtime_put_sync(hba->dev); |
| 5331 | } |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 5332 | |
| 5333 | if (!hba->is_init_prefetch) |
| 5334 | hba->is_init_prefetch = true; |
| 5335 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 5336 | /* Resume devfreq after UFS device is detected */ |
| 5337 | if (ufshcd_is_clkscaling_enabled(hba)) |
| 5338 | devfreq_resume_device(hba->devfreq); |
| 5339 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 5340 | out: |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 5341 | /* |
| 5342 | * If we failed to initialize the device or the device is not |
| 5343 | * present, turn off the power/clocks etc. |
| 5344 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5345 | if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) { |
| 5346 | pm_runtime_put_sync(hba->dev); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 5347 | ufshcd_hba_exit(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5348 | } |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 5349 | |
| 5350 | return ret; |
| 5351 | } |
| 5352 | |
| 5353 | /** |
| 5354 | * ufshcd_async_scan - asynchronous execution for probing hba |
| 5355 | * @data: data pointer to pass to this function |
| 5356 | * @cookie: cookie data |
| 5357 | */ |
| 5358 | static void ufshcd_async_scan(void *data, async_cookie_t cookie) |
| 5359 | { |
| 5360 | struct ufs_hba *hba = (struct ufs_hba *)data; |
| 5361 | |
| 5362 | ufshcd_probe_hba(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5363 | } |
| 5364 | |
Yaniv Gardi | f550c65 | 2016-03-10 17:37:07 +0200 | [diff] [blame] | 5365 | static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd) |
| 5366 | { |
| 5367 | unsigned long flags; |
| 5368 | struct Scsi_Host *host; |
| 5369 | struct ufs_hba *hba; |
| 5370 | int index; |
| 5371 | bool found = false; |
| 5372 | |
| 5373 | if (!scmd || !scmd->device || !scmd->device->host) |
| 5374 | return BLK_EH_NOT_HANDLED; |
| 5375 | |
| 5376 | host = scmd->device->host; |
| 5377 | hba = shost_priv(host); |
| 5378 | if (!hba) |
| 5379 | return BLK_EH_NOT_HANDLED; |
| 5380 | |
| 5381 | spin_lock_irqsave(host->host_lock, flags); |
| 5382 | |
| 5383 | for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) { |
| 5384 | if (hba->lrb[index].cmd == scmd) { |
| 5385 | found = true; |
| 5386 | break; |
| 5387 | } |
| 5388 | } |
| 5389 | |
| 5390 | spin_unlock_irqrestore(host->host_lock, flags); |
| 5391 | |
| 5392 | /* |
| 5393 | * Bypass SCSI error handling and reset the block layer timer if this |
| 5394 | * SCSI command was not actually dispatched to UFS driver, otherwise |
| 5395 | * let SCSI layer handle the error as usual. |
| 5396 | */ |
| 5397 | return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER; |
| 5398 | } |
| 5399 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5400 | static struct scsi_host_template ufshcd_driver_template = { |
| 5401 | .module = THIS_MODULE, |
| 5402 | .name = UFSHCD, |
| 5403 | .proc_name = UFSHCD, |
| 5404 | .queuecommand = ufshcd_queuecommand, |
| 5405 | .slave_alloc = ufshcd_slave_alloc, |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 5406 | .slave_configure = ufshcd_slave_configure, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5407 | .slave_destroy = ufshcd_slave_destroy, |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 5408 | .change_queue_depth = ufshcd_change_queue_depth, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5409 | .eh_abort_handler = ufshcd_abort, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5410 | .eh_device_reset_handler = ufshcd_eh_device_reset_handler, |
| 5411 | .eh_host_reset_handler = ufshcd_eh_host_reset_handler, |
Yaniv Gardi | f550c65 | 2016-03-10 17:37:07 +0200 | [diff] [blame] | 5412 | .eh_timed_out = ufshcd_eh_timed_out, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5413 | .this_id = -1, |
| 5414 | .sg_tablesize = SG_ALL, |
| 5415 | .cmd_per_lun = UFSHCD_CMD_PER_LUN, |
| 5416 | .can_queue = UFSHCD_CAN_QUEUE, |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5417 | .max_host_blocked = 1, |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 5418 | .track_queue_depth = 1, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5419 | }; |
| 5420 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5421 | static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg, |
| 5422 | int ua) |
| 5423 | { |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 5424 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5425 | |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 5426 | if (!vreg) |
| 5427 | return 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5428 | |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 5429 | ret = regulator_set_load(vreg->reg, ua); |
| 5430 | if (ret < 0) { |
| 5431 | dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n", |
| 5432 | __func__, vreg->name, ua, ret); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5433 | } |
| 5434 | |
| 5435 | return ret; |
| 5436 | } |
| 5437 | |
| 5438 | static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba, |
| 5439 | struct ufs_vreg *vreg) |
| 5440 | { |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 5441 | if (!vreg) |
| 5442 | return 0; |
| 5443 | else if (vreg->unused) |
| 5444 | return 0; |
| 5445 | else |
| 5446 | return ufshcd_config_vreg_load(hba->dev, vreg, |
| 5447 | UFS_VREG_LPM_LOAD_UA); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5448 | } |
| 5449 | |
| 5450 | static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba, |
| 5451 | struct ufs_vreg *vreg) |
| 5452 | { |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 5453 | if (!vreg) |
| 5454 | return 0; |
| 5455 | else if (vreg->unused) |
| 5456 | return 0; |
| 5457 | else |
| 5458 | return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5459 | } |
| 5460 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 5461 | static int ufshcd_config_vreg(struct device *dev, |
| 5462 | struct ufs_vreg *vreg, bool on) |
| 5463 | { |
| 5464 | int ret = 0; |
Gustavo A. R. Silva | a248dc6 | 2017-11-20 08:12:29 -0600 | [diff] [blame] | 5465 | struct regulator *reg; |
| 5466 | const char *name; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 5467 | int min_uV, uA_load; |
| 5468 | |
| 5469 | BUG_ON(!vreg); |
| 5470 | |
Gustavo A. R. Silva | a248dc6 | 2017-11-20 08:12:29 -0600 | [diff] [blame] | 5471 | reg = vreg->reg; |
| 5472 | name = vreg->name; |
| 5473 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 5474 | if (regulator_count_voltages(reg) > 0) { |
| 5475 | min_uV = on ? vreg->min_uV : 0; |
| 5476 | ret = regulator_set_voltage(reg, min_uV, vreg->max_uV); |
| 5477 | if (ret) { |
| 5478 | dev_err(dev, "%s: %s set voltage failed, err=%d\n", |
| 5479 | __func__, name, ret); |
| 5480 | goto out; |
| 5481 | } |
| 5482 | |
| 5483 | uA_load = on ? vreg->max_uA : 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5484 | ret = ufshcd_config_vreg_load(dev, vreg, uA_load); |
| 5485 | if (ret) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 5486 | goto out; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 5487 | } |
| 5488 | out: |
| 5489 | return ret; |
| 5490 | } |
| 5491 | |
| 5492 | static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 5493 | { |
| 5494 | int ret = 0; |
| 5495 | |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 5496 | if (!vreg) |
| 5497 | goto out; |
| 5498 | else if (vreg->enabled || vreg->unused) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 5499 | goto out; |
| 5500 | |
| 5501 | ret = ufshcd_config_vreg(dev, vreg, true); |
| 5502 | if (!ret) |
| 5503 | ret = regulator_enable(vreg->reg); |
| 5504 | |
| 5505 | if (!ret) |
| 5506 | vreg->enabled = true; |
| 5507 | else |
| 5508 | dev_err(dev, "%s: %s enable failed, err=%d\n", |
| 5509 | __func__, vreg->name, ret); |
| 5510 | out: |
| 5511 | return ret; |
| 5512 | } |
| 5513 | |
| 5514 | static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 5515 | { |
| 5516 | int ret = 0; |
| 5517 | |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 5518 | if (!vreg) |
| 5519 | goto out; |
| 5520 | else if (!vreg->enabled || vreg->unused) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 5521 | goto out; |
| 5522 | |
| 5523 | ret = regulator_disable(vreg->reg); |
| 5524 | |
| 5525 | if (!ret) { |
| 5526 | /* ignore errors on applying disable config */ |
| 5527 | ufshcd_config_vreg(dev, vreg, false); |
| 5528 | vreg->enabled = false; |
| 5529 | } else { |
| 5530 | dev_err(dev, "%s: %s disable failed, err=%d\n", |
| 5531 | __func__, vreg->name, ret); |
| 5532 | } |
| 5533 | out: |
| 5534 | return ret; |
| 5535 | } |
| 5536 | |
| 5537 | static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on) |
| 5538 | { |
| 5539 | int ret = 0; |
| 5540 | struct device *dev = hba->dev; |
| 5541 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 5542 | |
| 5543 | if (!info) |
| 5544 | goto out; |
| 5545 | |
| 5546 | ret = ufshcd_toggle_vreg(dev, info->vcc, on); |
| 5547 | if (ret) |
| 5548 | goto out; |
| 5549 | |
| 5550 | ret = ufshcd_toggle_vreg(dev, info->vccq, on); |
| 5551 | if (ret) |
| 5552 | goto out; |
| 5553 | |
| 5554 | ret = ufshcd_toggle_vreg(dev, info->vccq2, on); |
| 5555 | if (ret) |
| 5556 | goto out; |
| 5557 | |
| 5558 | out: |
| 5559 | if (ret) { |
| 5560 | ufshcd_toggle_vreg(dev, info->vccq2, false); |
| 5561 | ufshcd_toggle_vreg(dev, info->vccq, false); |
| 5562 | ufshcd_toggle_vreg(dev, info->vcc, false); |
| 5563 | } |
| 5564 | return ret; |
| 5565 | } |
| 5566 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 5567 | static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on) |
| 5568 | { |
| 5569 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 5570 | |
| 5571 | if (info) |
| 5572 | return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on); |
| 5573 | |
| 5574 | return 0; |
| 5575 | } |
| 5576 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 5577 | static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 5578 | { |
| 5579 | int ret = 0; |
| 5580 | |
| 5581 | if (!vreg) |
| 5582 | goto out; |
| 5583 | |
| 5584 | vreg->reg = devm_regulator_get(dev, vreg->name); |
| 5585 | if (IS_ERR(vreg->reg)) { |
| 5586 | ret = PTR_ERR(vreg->reg); |
| 5587 | dev_err(dev, "%s: %s get failed, err=%d\n", |
| 5588 | __func__, vreg->name, ret); |
| 5589 | } |
| 5590 | out: |
| 5591 | return ret; |
| 5592 | } |
| 5593 | |
| 5594 | static int ufshcd_init_vreg(struct ufs_hba *hba) |
| 5595 | { |
| 5596 | int ret = 0; |
| 5597 | struct device *dev = hba->dev; |
| 5598 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 5599 | |
| 5600 | if (!info) |
| 5601 | goto out; |
| 5602 | |
| 5603 | ret = ufshcd_get_vreg(dev, info->vcc); |
| 5604 | if (ret) |
| 5605 | goto out; |
| 5606 | |
| 5607 | ret = ufshcd_get_vreg(dev, info->vccq); |
| 5608 | if (ret) |
| 5609 | goto out; |
| 5610 | |
| 5611 | ret = ufshcd_get_vreg(dev, info->vccq2); |
| 5612 | out: |
| 5613 | return ret; |
| 5614 | } |
| 5615 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 5616 | static int ufshcd_init_hba_vreg(struct ufs_hba *hba) |
| 5617 | { |
| 5618 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 5619 | |
| 5620 | if (info) |
| 5621 | return ufshcd_get_vreg(hba->dev, info->vdd_hba); |
| 5622 | |
| 5623 | return 0; |
| 5624 | } |
| 5625 | |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 5626 | static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused) |
| 5627 | { |
| 5628 | int ret = 0; |
| 5629 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 5630 | |
| 5631 | if (!info) |
| 5632 | goto out; |
| 5633 | else if (!info->vccq) |
| 5634 | goto out; |
| 5635 | |
| 5636 | if (unused) { |
| 5637 | /* shut off the rail here */ |
| 5638 | ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false); |
| 5639 | /* |
| 5640 | * Mark this rail as no longer used, so it doesn't get enabled |
| 5641 | * later by mistake |
| 5642 | */ |
| 5643 | if (!ret) |
| 5644 | info->vccq->unused = true; |
| 5645 | } else { |
| 5646 | /* |
| 5647 | * rail should have been already enabled hence just make sure |
| 5648 | * that unused flag is cleared. |
| 5649 | */ |
| 5650 | info->vccq->unused = false; |
| 5651 | } |
| 5652 | out: |
| 5653 | return ret; |
| 5654 | } |
| 5655 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5656 | static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on, |
| 5657 | bool skip_ref_clk) |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 5658 | { |
| 5659 | int ret = 0; |
| 5660 | struct ufs_clk_info *clki; |
| 5661 | struct list_head *head = &hba->clk_list_head; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5662 | unsigned long flags; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 5663 | |
| 5664 | if (!head || list_empty(head)) |
| 5665 | goto out; |
| 5666 | |
| 5667 | list_for_each_entry(clki, head, list) { |
| 5668 | if (!IS_ERR_OR_NULL(clki->clk)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5669 | if (skip_ref_clk && !strcmp(clki->name, "ref_clk")) |
| 5670 | continue; |
| 5671 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 5672 | if (on && !clki->enabled) { |
| 5673 | ret = clk_prepare_enable(clki->clk); |
| 5674 | if (ret) { |
| 5675 | dev_err(hba->dev, "%s: %s prepare enable failed, %d\n", |
| 5676 | __func__, clki->name, ret); |
| 5677 | goto out; |
| 5678 | } |
| 5679 | } else if (!on && clki->enabled) { |
| 5680 | clk_disable_unprepare(clki->clk); |
| 5681 | } |
| 5682 | clki->enabled = on; |
| 5683 | dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__, |
| 5684 | clki->name, on ? "en" : "dis"); |
| 5685 | } |
| 5686 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5687 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 5688 | ret = ufshcd_vops_setup_clocks(hba, on); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 5689 | out: |
| 5690 | if (ret) { |
| 5691 | list_for_each_entry(clki, head, list) { |
| 5692 | if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled) |
| 5693 | clk_disable_unprepare(clki->clk); |
| 5694 | } |
Dolev Raviv | eda910e | 2014-10-23 13:25:16 +0300 | [diff] [blame] | 5695 | } else if (on) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5696 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5697 | hba->clk_gating.state = CLKS_ON; |
| 5698 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 5699 | } |
| 5700 | return ret; |
| 5701 | } |
| 5702 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5703 | static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on) |
| 5704 | { |
| 5705 | return __ufshcd_setup_clocks(hba, on, false); |
| 5706 | } |
| 5707 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 5708 | static int ufshcd_init_clocks(struct ufs_hba *hba) |
| 5709 | { |
| 5710 | int ret = 0; |
| 5711 | struct ufs_clk_info *clki; |
| 5712 | struct device *dev = hba->dev; |
| 5713 | struct list_head *head = &hba->clk_list_head; |
| 5714 | |
| 5715 | if (!head || list_empty(head)) |
| 5716 | goto out; |
| 5717 | |
| 5718 | list_for_each_entry(clki, head, list) { |
| 5719 | if (!clki->name) |
| 5720 | continue; |
| 5721 | |
| 5722 | clki->clk = devm_clk_get(dev, clki->name); |
| 5723 | if (IS_ERR(clki->clk)) { |
| 5724 | ret = PTR_ERR(clki->clk); |
| 5725 | dev_err(dev, "%s: %s clk get failed, %d\n", |
| 5726 | __func__, clki->name, ret); |
| 5727 | goto out; |
| 5728 | } |
| 5729 | |
| 5730 | if (clki->max_freq) { |
| 5731 | ret = clk_set_rate(clki->clk, clki->max_freq); |
| 5732 | if (ret) { |
| 5733 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 5734 | __func__, clki->name, |
| 5735 | clki->max_freq, ret); |
| 5736 | goto out; |
| 5737 | } |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 5738 | clki->curr_freq = clki->max_freq; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 5739 | } |
| 5740 | dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__, |
| 5741 | clki->name, clk_get_rate(clki->clk)); |
| 5742 | } |
| 5743 | out: |
| 5744 | return ret; |
| 5745 | } |
| 5746 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 5747 | static int ufshcd_variant_hba_init(struct ufs_hba *hba) |
| 5748 | { |
| 5749 | int err = 0; |
| 5750 | |
| 5751 | if (!hba->vops) |
| 5752 | goto out; |
| 5753 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 5754 | err = ufshcd_vops_init(hba); |
| 5755 | if (err) |
| 5756 | goto out; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 5757 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 5758 | err = ufshcd_vops_setup_regulators(hba, true); |
| 5759 | if (err) |
| 5760 | goto out_exit; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 5761 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 5762 | goto out; |
| 5763 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 5764 | out_exit: |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 5765 | ufshcd_vops_exit(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 5766 | out: |
| 5767 | if (err) |
| 5768 | dev_err(hba->dev, "%s: variant %s init failed err %d\n", |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 5769 | __func__, ufshcd_get_var_name(hba), err); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 5770 | return err; |
| 5771 | } |
| 5772 | |
| 5773 | static void ufshcd_variant_hba_exit(struct ufs_hba *hba) |
| 5774 | { |
| 5775 | if (!hba->vops) |
| 5776 | return; |
| 5777 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 5778 | ufshcd_vops_setup_clocks(hba, false); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 5779 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 5780 | ufshcd_vops_setup_regulators(hba, false); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 5781 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 5782 | ufshcd_vops_exit(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 5783 | } |
| 5784 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 5785 | static int ufshcd_hba_init(struct ufs_hba *hba) |
| 5786 | { |
| 5787 | int err; |
| 5788 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 5789 | /* |
| 5790 | * Handle host controller power separately from the UFS device power |
| 5791 | * rails as it will help controlling the UFS host controller power |
| 5792 | * collapse easily which is different than UFS device power collapse. |
| 5793 | * Also, enable the host controller power before we go ahead with rest |
| 5794 | * of the initialization here. |
| 5795 | */ |
| 5796 | err = ufshcd_init_hba_vreg(hba); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 5797 | if (err) |
| 5798 | goto out; |
| 5799 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 5800 | err = ufshcd_setup_hba_vreg(hba, true); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 5801 | if (err) |
| 5802 | goto out; |
| 5803 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 5804 | err = ufshcd_init_clocks(hba); |
| 5805 | if (err) |
| 5806 | goto out_disable_hba_vreg; |
| 5807 | |
| 5808 | err = ufshcd_setup_clocks(hba, true); |
| 5809 | if (err) |
| 5810 | goto out_disable_hba_vreg; |
| 5811 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 5812 | err = ufshcd_init_vreg(hba); |
| 5813 | if (err) |
| 5814 | goto out_disable_clks; |
| 5815 | |
| 5816 | err = ufshcd_setup_vreg(hba, true); |
| 5817 | if (err) |
| 5818 | goto out_disable_clks; |
| 5819 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 5820 | err = ufshcd_variant_hba_init(hba); |
| 5821 | if (err) |
| 5822 | goto out_disable_vreg; |
| 5823 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 5824 | hba->is_powered = true; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 5825 | goto out; |
| 5826 | |
| 5827 | out_disable_vreg: |
| 5828 | ufshcd_setup_vreg(hba, false); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 5829 | out_disable_clks: |
| 5830 | ufshcd_setup_clocks(hba, false); |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 5831 | out_disable_hba_vreg: |
| 5832 | ufshcd_setup_hba_vreg(hba, false); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 5833 | out: |
| 5834 | return err; |
| 5835 | } |
| 5836 | |
| 5837 | static void ufshcd_hba_exit(struct ufs_hba *hba) |
| 5838 | { |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 5839 | if (hba->is_powered) { |
| 5840 | ufshcd_variant_hba_exit(hba); |
| 5841 | ufshcd_setup_vreg(hba, false); |
| 5842 | ufshcd_setup_clocks(hba, false); |
| 5843 | ufshcd_setup_hba_vreg(hba, false); |
| 5844 | hba->is_powered = false; |
| 5845 | } |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 5846 | } |
| 5847 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5848 | static int |
| 5849 | ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5850 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5851 | unsigned char cmd[6] = {REQUEST_SENSE, |
| 5852 | 0, |
| 5853 | 0, |
| 5854 | 0, |
| 5855 | SCSI_SENSE_BUFFERSIZE, |
| 5856 | 0}; |
| 5857 | char *buffer; |
| 5858 | int ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5859 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5860 | buffer = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL); |
| 5861 | if (!buffer) { |
| 5862 | ret = -ENOMEM; |
| 5863 | goto out; |
| 5864 | } |
| 5865 | |
| 5866 | ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer, |
| 5867 | SCSI_SENSE_BUFFERSIZE, NULL, |
| 5868 | msecs_to_jiffies(1000), 3, NULL, REQ_PM); |
| 5869 | if (ret) |
| 5870 | pr_err("%s: failed with err %d\n", __func__, ret); |
| 5871 | |
| 5872 | kfree(buffer); |
| 5873 | out: |
| 5874 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5875 | } |
| 5876 | |
| 5877 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5878 | * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device |
| 5879 | * power mode |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 5880 | * @hba: per adapter instance |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5881 | * @pwr_mode: device power mode to set |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5882 | * |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5883 | * Returns 0 if requested power mode is set successfully |
| 5884 | * Returns non-zero if failed to set the requested power mode |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5885 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5886 | static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba, |
| 5887 | enum ufs_dev_pwr_mode pwr_mode) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5888 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5889 | unsigned char cmd[6] = { START_STOP }; |
| 5890 | struct scsi_sense_hdr sshdr; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 5891 | struct scsi_device *sdp; |
| 5892 | unsigned long flags; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5893 | int ret; |
| 5894 | |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 5895 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5896 | sdp = hba->sdev_ufs_device; |
| 5897 | if (sdp) { |
| 5898 | ret = scsi_device_get(sdp); |
| 5899 | if (!ret && !scsi_device_online(sdp)) { |
| 5900 | ret = -ENODEV; |
| 5901 | scsi_device_put(sdp); |
| 5902 | } |
| 5903 | } else { |
| 5904 | ret = -ENODEV; |
| 5905 | } |
| 5906 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5907 | |
| 5908 | if (ret) |
| 5909 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5910 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5911 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5912 | * If scsi commands fail, the scsi mid-layer schedules scsi error- |
| 5913 | * handling, which would wait for host to be resumed. Since we know |
| 5914 | * we are functional while we are here, skip host resume in error |
| 5915 | * handling context. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5916 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5917 | hba->host->eh_noresume = 1; |
| 5918 | if (hba->wlun_dev_clr_ua) { |
| 5919 | ret = ufshcd_send_request_sense(hba, sdp); |
| 5920 | if (ret) |
| 5921 | goto out; |
| 5922 | /* Unit attention condition is cleared now */ |
| 5923 | hba->wlun_dev_clr_ua = false; |
| 5924 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5925 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5926 | cmd[4] = pwr_mode << 4; |
| 5927 | |
| 5928 | /* |
| 5929 | * Current function would be generally called from the power management |
| 5930 | * callbacks hence set the REQ_PM flag so that it doesn't resume the |
| 5931 | * already suspended childs. |
| 5932 | */ |
| 5933 | ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr, |
| 5934 | START_STOP_TIMEOUT, 0, NULL, REQ_PM); |
| 5935 | if (ret) { |
| 5936 | sdev_printk(KERN_WARNING, sdp, |
Hannes Reinecke | ef61329 | 2014-10-24 14:27:00 +0200 | [diff] [blame] | 5937 | "START_STOP failed for power mode: %d, result %x\n", |
| 5938 | pwr_mode, ret); |
Hannes Reinecke | 2104551 | 2015-01-08 07:43:46 +0100 | [diff] [blame] | 5939 | if (driver_byte(ret) & DRIVER_SENSE) |
| 5940 | scsi_print_sense_hdr(sdp, NULL, &sshdr); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5941 | } |
| 5942 | |
| 5943 | if (!ret) |
| 5944 | hba->curr_dev_pwr_mode = pwr_mode; |
| 5945 | out: |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 5946 | scsi_device_put(sdp); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5947 | hba->host->eh_noresume = 0; |
| 5948 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5949 | } |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 5950 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5951 | static int ufshcd_link_state_transition(struct ufs_hba *hba, |
| 5952 | enum uic_link_state req_link_state, |
| 5953 | int check_for_bkops) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5954 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5955 | int ret = 0; |
| 5956 | |
| 5957 | if (req_link_state == hba->uic_link_state) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5958 | return 0; |
| 5959 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5960 | if (req_link_state == UIC_LINK_HIBERN8_STATE) { |
| 5961 | ret = ufshcd_uic_hibern8_enter(hba); |
| 5962 | if (!ret) |
| 5963 | ufshcd_set_link_hibern8(hba); |
| 5964 | else |
| 5965 | goto out; |
| 5966 | } |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5967 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5968 | * If autobkops is enabled, link can't be turned off because |
| 5969 | * turning off the link would also turn off the device. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5970 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5971 | else if ((req_link_state == UIC_LINK_OFF_STATE) && |
| 5972 | (!check_for_bkops || (check_for_bkops && |
| 5973 | !hba->auto_bkops_enabled))) { |
| 5974 | /* |
Yaniv Gardi | f3099fb | 2016-03-10 17:37:17 +0200 | [diff] [blame] | 5975 | * Let's make sure that link is in low power mode, we are doing |
| 5976 | * this currently by putting the link in Hibern8. Otherway to |
| 5977 | * put the link in low power mode is to send the DME end point |
| 5978 | * to device and then send the DME reset command to local |
| 5979 | * unipro. But putting the link in hibern8 is much faster. |
| 5980 | */ |
| 5981 | ret = ufshcd_uic_hibern8_enter(hba); |
| 5982 | if (ret) |
| 5983 | goto out; |
| 5984 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5985 | * Change controller state to "reset state" which |
| 5986 | * should also put the link in off/reset state |
| 5987 | */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 5988 | ufshcd_hba_stop(hba, true); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5989 | /* |
| 5990 | * TODO: Check if we need any delay to make sure that |
| 5991 | * controller is reset |
| 5992 | */ |
| 5993 | ufshcd_set_link_off(hba); |
| 5994 | } |
| 5995 | |
| 5996 | out: |
| 5997 | return ret; |
| 5998 | } |
| 5999 | |
| 6000 | static void ufshcd_vreg_set_lpm(struct ufs_hba *hba) |
| 6001 | { |
| 6002 | /* |
Yaniv Gardi | b799fdf | 2016-03-10 17:37:18 +0200 | [diff] [blame] | 6003 | * It seems some UFS devices may keep drawing more than sleep current |
| 6004 | * (atleast for 500us) from UFS rails (especially from VCCQ rail). |
| 6005 | * To avoid this situation, add 2ms delay before putting these UFS |
| 6006 | * rails in LPM mode. |
| 6007 | */ |
| 6008 | if (!ufshcd_is_link_active(hba) && |
| 6009 | hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM) |
| 6010 | usleep_range(2000, 2100); |
| 6011 | |
| 6012 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6013 | * If UFS device is either in UFS_Sleep turn off VCC rail to save some |
| 6014 | * power. |
| 6015 | * |
| 6016 | * If UFS device and link is in OFF state, all power supplies (VCC, |
| 6017 | * VCCQ, VCCQ2) can be turned off if power on write protect is not |
| 6018 | * required. If UFS link is inactive (Hibern8 or OFF state) and device |
| 6019 | * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode. |
| 6020 | * |
| 6021 | * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway |
| 6022 | * in low power state which would save some power. |
| 6023 | */ |
| 6024 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) && |
| 6025 | !hba->dev_info.is_lu_power_on_wp) { |
| 6026 | ufshcd_setup_vreg(hba, false); |
| 6027 | } else if (!ufshcd_is_ufs_dev_active(hba)) { |
| 6028 | ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false); |
| 6029 | if (!ufshcd_is_link_active(hba)) { |
| 6030 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq); |
| 6031 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2); |
| 6032 | } |
| 6033 | } |
| 6034 | } |
| 6035 | |
| 6036 | static int ufshcd_vreg_set_hpm(struct ufs_hba *hba) |
| 6037 | { |
| 6038 | int ret = 0; |
| 6039 | |
| 6040 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) && |
| 6041 | !hba->dev_info.is_lu_power_on_wp) { |
| 6042 | ret = ufshcd_setup_vreg(hba, true); |
| 6043 | } else if (!ufshcd_is_ufs_dev_active(hba)) { |
| 6044 | ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true); |
| 6045 | if (!ret && !ufshcd_is_link_active(hba)) { |
| 6046 | ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq); |
| 6047 | if (ret) |
| 6048 | goto vcc_disable; |
| 6049 | ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2); |
| 6050 | if (ret) |
| 6051 | goto vccq_lpm; |
| 6052 | } |
| 6053 | } |
| 6054 | goto out; |
| 6055 | |
| 6056 | vccq_lpm: |
| 6057 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq); |
| 6058 | vcc_disable: |
| 6059 | ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false); |
| 6060 | out: |
| 6061 | return ret; |
| 6062 | } |
| 6063 | |
| 6064 | static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba) |
| 6065 | { |
| 6066 | if (ufshcd_is_link_off(hba)) |
| 6067 | ufshcd_setup_hba_vreg(hba, false); |
| 6068 | } |
| 6069 | |
| 6070 | static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba) |
| 6071 | { |
| 6072 | if (ufshcd_is_link_off(hba)) |
| 6073 | ufshcd_setup_hba_vreg(hba, true); |
| 6074 | } |
| 6075 | |
| 6076 | /** |
| 6077 | * ufshcd_suspend - helper function for suspend operations |
| 6078 | * @hba: per adapter instance |
| 6079 | * @pm_op: desired low power operation type |
| 6080 | * |
| 6081 | * This function will try to put the UFS device and link into low power |
| 6082 | * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl" |
| 6083 | * (System PM level). |
| 6084 | * |
| 6085 | * If this function is called during shutdown, it will make sure that |
| 6086 | * both UFS device and UFS link is powered off. |
| 6087 | * |
| 6088 | * NOTE: UFS device & link must be active before we enter in this function. |
| 6089 | * |
| 6090 | * Returns 0 for success and non-zero for failure |
| 6091 | */ |
| 6092 | static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op) |
| 6093 | { |
| 6094 | int ret = 0; |
| 6095 | enum ufs_pm_level pm_lvl; |
| 6096 | enum ufs_dev_pwr_mode req_dev_pwr_mode; |
| 6097 | enum uic_link_state req_link_state; |
| 6098 | |
| 6099 | hba->pm_op_in_progress = 1; |
| 6100 | if (!ufshcd_is_shutdown_pm(pm_op)) { |
| 6101 | pm_lvl = ufshcd_is_runtime_pm(pm_op) ? |
| 6102 | hba->rpm_lvl : hba->spm_lvl; |
| 6103 | req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl); |
| 6104 | req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl); |
| 6105 | } else { |
| 6106 | req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE; |
| 6107 | req_link_state = UIC_LINK_OFF_STATE; |
| 6108 | } |
| 6109 | |
| 6110 | /* |
| 6111 | * If we can't transition into any of the low power modes |
| 6112 | * just gate the clocks. |
| 6113 | */ |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6114 | ufshcd_hold(hba, false); |
| 6115 | hba->clk_gating.is_suspended = true; |
| 6116 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6117 | if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE && |
| 6118 | req_link_state == UIC_LINK_ACTIVE_STATE) { |
| 6119 | goto disable_clks; |
| 6120 | } |
| 6121 | |
| 6122 | if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) && |
| 6123 | (req_link_state == hba->uic_link_state)) |
| 6124 | goto out; |
| 6125 | |
| 6126 | /* UFS device & link must be active before we enter in this function */ |
| 6127 | if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) { |
| 6128 | ret = -EINVAL; |
| 6129 | goto out; |
| 6130 | } |
| 6131 | |
| 6132 | if (ufshcd_is_runtime_pm(pm_op)) { |
Subhash Jadavani | 374a246 | 2014-09-25 15:32:35 +0300 | [diff] [blame] | 6133 | if (ufshcd_can_autobkops_during_suspend(hba)) { |
| 6134 | /* |
| 6135 | * The device is idle with no requests in the queue, |
| 6136 | * allow background operations if bkops status shows |
| 6137 | * that performance might be impacted. |
| 6138 | */ |
| 6139 | ret = ufshcd_urgent_bkops(hba); |
| 6140 | if (ret) |
| 6141 | goto enable_gating; |
| 6142 | } else { |
| 6143 | /* make sure that auto bkops is disabled */ |
| 6144 | ufshcd_disable_auto_bkops(hba); |
| 6145 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6146 | } |
| 6147 | |
| 6148 | if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) && |
| 6149 | ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) || |
| 6150 | !ufshcd_is_runtime_pm(pm_op))) { |
| 6151 | /* ensure that bkops is disabled */ |
| 6152 | ufshcd_disable_auto_bkops(hba); |
| 6153 | ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode); |
| 6154 | if (ret) |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6155 | goto enable_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6156 | } |
| 6157 | |
| 6158 | ret = ufshcd_link_state_transition(hba, req_link_state, 1); |
| 6159 | if (ret) |
| 6160 | goto set_dev_active; |
| 6161 | |
| 6162 | ufshcd_vreg_set_lpm(hba); |
| 6163 | |
| 6164 | disable_clks: |
| 6165 | /* |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 6166 | * The clock scaling needs access to controller registers. Hence, Wait |
| 6167 | * for pending clock scaling work to be done before clocks are |
| 6168 | * turned off. |
| 6169 | */ |
| 6170 | if (ufshcd_is_clkscaling_enabled(hba)) { |
| 6171 | devfreq_suspend_device(hba->devfreq); |
| 6172 | hba->clk_scaling.window_start_t = 0; |
| 6173 | } |
| 6174 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6175 | * Call vendor specific suspend callback. As these callbacks may access |
| 6176 | * vendor specific host controller register space call them before the |
| 6177 | * host clocks are ON. |
| 6178 | */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 6179 | ret = ufshcd_vops_suspend(hba, pm_op); |
| 6180 | if (ret) |
| 6181 | goto set_link_active; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6182 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 6183 | ret = ufshcd_vops_setup_clocks(hba, false); |
| 6184 | if (ret) |
| 6185 | goto vops_resume; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6186 | |
| 6187 | if (!ufshcd_is_link_active(hba)) |
| 6188 | ufshcd_setup_clocks(hba, false); |
| 6189 | else |
| 6190 | /* If link is active, device ref_clk can't be switched off */ |
| 6191 | __ufshcd_setup_clocks(hba, false, true); |
| 6192 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6193 | hba->clk_gating.state = CLKS_OFF; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6194 | /* |
| 6195 | * 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] | 6196 | * host controller transaction expected till resume. |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6197 | */ |
| 6198 | ufshcd_disable_irq(hba); |
| 6199 | /* Put the host controller in low power mode if possible */ |
| 6200 | ufshcd_hba_vreg_set_lpm(hba); |
| 6201 | goto out; |
| 6202 | |
| 6203 | vops_resume: |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 6204 | ufshcd_vops_resume(hba, pm_op); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6205 | set_link_active: |
| 6206 | ufshcd_vreg_set_hpm(hba); |
| 6207 | if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba)) |
| 6208 | ufshcd_set_link_active(hba); |
| 6209 | else if (ufshcd_is_link_off(hba)) |
| 6210 | ufshcd_host_reset_and_restore(hba); |
| 6211 | set_dev_active: |
| 6212 | if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE)) |
| 6213 | ufshcd_disable_auto_bkops(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6214 | enable_gating: |
| 6215 | hba->clk_gating.is_suspended = false; |
| 6216 | ufshcd_release(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6217 | out: |
| 6218 | hba->pm_op_in_progress = 0; |
| 6219 | return ret; |
| 6220 | } |
| 6221 | |
| 6222 | /** |
| 6223 | * ufshcd_resume - helper function for resume operations |
| 6224 | * @hba: per adapter instance |
| 6225 | * @pm_op: runtime PM or system PM |
| 6226 | * |
| 6227 | * This function basically brings the UFS device, UniPro link and controller |
| 6228 | * to active state. |
| 6229 | * |
| 6230 | * Returns 0 for success and non-zero for failure |
| 6231 | */ |
| 6232 | static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op) |
| 6233 | { |
| 6234 | int ret; |
| 6235 | enum uic_link_state old_link_state; |
| 6236 | |
| 6237 | hba->pm_op_in_progress = 1; |
| 6238 | old_link_state = hba->uic_link_state; |
| 6239 | |
| 6240 | ufshcd_hba_vreg_set_hpm(hba); |
| 6241 | /* Make sure clocks are enabled before accessing controller */ |
| 6242 | ret = ufshcd_setup_clocks(hba, true); |
| 6243 | if (ret) |
| 6244 | goto out; |
| 6245 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6246 | /* enable the host irq as host controller would be active soon */ |
| 6247 | ret = ufshcd_enable_irq(hba); |
| 6248 | if (ret) |
| 6249 | goto disable_irq_and_vops_clks; |
| 6250 | |
| 6251 | ret = ufshcd_vreg_set_hpm(hba); |
| 6252 | if (ret) |
| 6253 | goto disable_irq_and_vops_clks; |
| 6254 | |
| 6255 | /* |
| 6256 | * Call vendor specific resume callback. As these callbacks may access |
| 6257 | * vendor specific host controller register space call them when the |
| 6258 | * host clocks are ON. |
| 6259 | */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 6260 | ret = ufshcd_vops_resume(hba, pm_op); |
| 6261 | if (ret) |
| 6262 | goto disable_vreg; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6263 | |
| 6264 | if (ufshcd_is_link_hibern8(hba)) { |
| 6265 | ret = ufshcd_uic_hibern8_exit(hba); |
| 6266 | if (!ret) |
| 6267 | ufshcd_set_link_active(hba); |
| 6268 | else |
| 6269 | goto vendor_suspend; |
| 6270 | } else if (ufshcd_is_link_off(hba)) { |
| 6271 | ret = ufshcd_host_reset_and_restore(hba); |
| 6272 | /* |
| 6273 | * ufshcd_host_reset_and_restore() should have already |
| 6274 | * set the link state as active |
| 6275 | */ |
| 6276 | if (ret || !ufshcd_is_link_active(hba)) |
| 6277 | goto vendor_suspend; |
| 6278 | } |
| 6279 | |
| 6280 | if (!ufshcd_is_ufs_dev_active(hba)) { |
| 6281 | ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE); |
| 6282 | if (ret) |
| 6283 | goto set_old_link_state; |
| 6284 | } |
| 6285 | |
subhashj@codeaurora.org | cfb09f0 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 6286 | if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) |
| 6287 | ufshcd_enable_auto_bkops(hba); |
| 6288 | else |
| 6289 | /* |
| 6290 | * If BKOPs operations are urgently needed at this moment then |
| 6291 | * keep auto-bkops enabled or else disable it. |
| 6292 | */ |
| 6293 | ufshcd_urgent_bkops(hba); |
| 6294 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6295 | hba->clk_gating.is_suspended = false; |
| 6296 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 6297 | if (ufshcd_is_clkscaling_enabled(hba)) |
| 6298 | devfreq_resume_device(hba->devfreq); |
| 6299 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6300 | /* Schedule clock gating in case of no access to UFS device yet */ |
| 6301 | ufshcd_release(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6302 | goto out; |
| 6303 | |
| 6304 | set_old_link_state: |
| 6305 | ufshcd_link_state_transition(hba, old_link_state, 0); |
| 6306 | vendor_suspend: |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 6307 | ufshcd_vops_suspend(hba, pm_op); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6308 | disable_vreg: |
| 6309 | ufshcd_vreg_set_lpm(hba); |
| 6310 | disable_irq_and_vops_clks: |
| 6311 | ufshcd_disable_irq(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6312 | ufshcd_setup_clocks(hba, false); |
| 6313 | out: |
| 6314 | hba->pm_op_in_progress = 0; |
| 6315 | return ret; |
| 6316 | } |
| 6317 | |
| 6318 | /** |
| 6319 | * ufshcd_system_suspend - system suspend routine |
| 6320 | * @hba: per adapter instance |
| 6321 | * @pm_op: runtime PM or system PM |
| 6322 | * |
| 6323 | * Check the description of ufshcd_suspend() function for more details. |
| 6324 | * |
| 6325 | * Returns 0 for success and non-zero for failure |
| 6326 | */ |
| 6327 | int ufshcd_system_suspend(struct ufs_hba *hba) |
| 6328 | { |
| 6329 | int ret = 0; |
| 6330 | |
| 6331 | if (!hba || !hba->is_powered) |
Dolev Raviv | 233b594 | 2014-10-23 13:25:14 +0300 | [diff] [blame] | 6332 | return 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6333 | |
| 6334 | if (pm_runtime_suspended(hba->dev)) { |
| 6335 | if (hba->rpm_lvl == hba->spm_lvl) |
| 6336 | /* |
| 6337 | * There is possibility that device may still be in |
| 6338 | * active state during the runtime suspend. |
| 6339 | */ |
| 6340 | if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) == |
| 6341 | hba->curr_dev_pwr_mode) && !hba->auto_bkops_enabled) |
| 6342 | goto out; |
| 6343 | |
| 6344 | /* |
| 6345 | * UFS device and/or UFS link low power states during runtime |
| 6346 | * suspend seems to be different than what is expected during |
| 6347 | * system suspend. Hence runtime resume the devic & link and |
| 6348 | * let the system suspend low power states to take effect. |
| 6349 | * TODO: If resume takes longer time, we might have optimize |
| 6350 | * it in future by not resuming everything if possible. |
| 6351 | */ |
| 6352 | ret = ufshcd_runtime_resume(hba); |
| 6353 | if (ret) |
| 6354 | goto out; |
| 6355 | } |
| 6356 | |
| 6357 | ret = ufshcd_suspend(hba, UFS_SYSTEM_PM); |
| 6358 | out: |
Dolev Raviv | e785060 | 2014-09-25 15:32:36 +0300 | [diff] [blame] | 6359 | if (!ret) |
| 6360 | hba->is_sys_suspended = true; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6361 | return ret; |
| 6362 | } |
| 6363 | EXPORT_SYMBOL(ufshcd_system_suspend); |
| 6364 | |
| 6365 | /** |
| 6366 | * ufshcd_system_resume - system resume routine |
| 6367 | * @hba: per adapter instance |
| 6368 | * |
| 6369 | * Returns 0 for success and non-zero for failure |
| 6370 | */ |
| 6371 | |
| 6372 | int ufshcd_system_resume(struct ufs_hba *hba) |
| 6373 | { |
| 6374 | if (!hba || !hba->is_powered || pm_runtime_suspended(hba->dev)) |
| 6375 | /* |
| 6376 | * Let the runtime resume take care of resuming |
| 6377 | * if runtime suspended. |
| 6378 | */ |
| 6379 | return 0; |
| 6380 | |
| 6381 | return ufshcd_resume(hba, UFS_SYSTEM_PM); |
| 6382 | } |
| 6383 | EXPORT_SYMBOL(ufshcd_system_resume); |
| 6384 | |
| 6385 | /** |
| 6386 | * ufshcd_runtime_suspend - runtime suspend routine |
| 6387 | * @hba: per adapter instance |
| 6388 | * |
| 6389 | * Check the description of ufshcd_suspend() function for more details. |
| 6390 | * |
| 6391 | * Returns 0 for success and non-zero for failure |
| 6392 | */ |
| 6393 | int ufshcd_runtime_suspend(struct ufs_hba *hba) |
| 6394 | { |
| 6395 | if (!hba || !hba->is_powered) |
| 6396 | return 0; |
| 6397 | |
| 6398 | return ufshcd_suspend(hba, UFS_RUNTIME_PM); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 6399 | } |
| 6400 | EXPORT_SYMBOL(ufshcd_runtime_suspend); |
| 6401 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6402 | /** |
| 6403 | * ufshcd_runtime_resume - runtime resume routine |
| 6404 | * @hba: per adapter instance |
| 6405 | * |
| 6406 | * This function basically brings the UFS device, UniPro link and controller |
| 6407 | * to active state. Following operations are done in this function: |
| 6408 | * |
| 6409 | * 1. Turn on all the controller related clocks |
| 6410 | * 2. Bring the UniPro link out of Hibernate state |
| 6411 | * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device |
| 6412 | * to active state. |
| 6413 | * 4. If auto-bkops is enabled on the device, disable it. |
| 6414 | * |
| 6415 | * So following would be the possible power state after this function return |
| 6416 | * successfully: |
| 6417 | * S1: UFS device in Active state with VCC rail ON |
| 6418 | * UniPro link in Active state |
| 6419 | * All the UFS/UniPro controller clocks are ON |
| 6420 | * |
| 6421 | * Returns 0 for success and non-zero for failure |
| 6422 | */ |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 6423 | int ufshcd_runtime_resume(struct ufs_hba *hba) |
| 6424 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6425 | if (!hba || !hba->is_powered) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 6426 | return 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6427 | else |
| 6428 | return ufshcd_resume(hba, UFS_RUNTIME_PM); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 6429 | } |
| 6430 | EXPORT_SYMBOL(ufshcd_runtime_resume); |
| 6431 | |
| 6432 | int ufshcd_runtime_idle(struct ufs_hba *hba) |
| 6433 | { |
| 6434 | return 0; |
| 6435 | } |
| 6436 | EXPORT_SYMBOL(ufshcd_runtime_idle); |
| 6437 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6438 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6439 | * ufshcd_shutdown - shutdown routine |
| 6440 | * @hba: per adapter instance |
| 6441 | * |
| 6442 | * This function would power off both UFS device and UFS link. |
| 6443 | * |
| 6444 | * Returns 0 always to allow force shutdown even in case of errors. |
| 6445 | */ |
| 6446 | int ufshcd_shutdown(struct ufs_hba *hba) |
| 6447 | { |
| 6448 | int ret = 0; |
| 6449 | |
| 6450 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba)) |
| 6451 | goto out; |
| 6452 | |
| 6453 | if (pm_runtime_suspended(hba->dev)) { |
| 6454 | ret = ufshcd_runtime_resume(hba); |
| 6455 | if (ret) |
| 6456 | goto out; |
| 6457 | } |
| 6458 | |
| 6459 | ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM); |
| 6460 | out: |
| 6461 | if (ret) |
| 6462 | dev_err(hba->dev, "%s failed, err %d\n", __func__, ret); |
| 6463 | /* allow force shutdown even in case of errors */ |
| 6464 | return 0; |
| 6465 | } |
| 6466 | EXPORT_SYMBOL(ufshcd_shutdown); |
| 6467 | |
| 6468 | /** |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6469 | * ufshcd_remove - de-allocate SCSI host and host memory space |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6470 | * data structure memory |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6471 | * @hba - per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6472 | */ |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6473 | void ufshcd_remove(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6474 | { |
Akinobu Mita | cfdf9c9 | 2013-07-30 00:36:03 +0530 | [diff] [blame] | 6475 | scsi_remove_host(hba->host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6476 | /* disable interrupts */ |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 6477 | ufshcd_disable_intr(hba, hba->intr_mask); |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 6478 | ufshcd_hba_stop(hba, true); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6479 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6480 | scsi_host_put(hba->host); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6481 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6482 | ufshcd_exit_clk_gating(hba); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 6483 | if (ufshcd_is_clkscaling_enabled(hba)) |
| 6484 | devfreq_remove_device(hba->devfreq); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6485 | ufshcd_hba_exit(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6486 | } |
| 6487 | EXPORT_SYMBOL_GPL(ufshcd_remove); |
| 6488 | |
| 6489 | /** |
Yaniv Gardi | 47555a5 | 2015-10-28 13:15:49 +0200 | [diff] [blame] | 6490 | * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA) |
| 6491 | * @hba: pointer to Host Bus Adapter (HBA) |
| 6492 | */ |
| 6493 | void ufshcd_dealloc_host(struct ufs_hba *hba) |
| 6494 | { |
| 6495 | scsi_host_put(hba->host); |
| 6496 | } |
| 6497 | EXPORT_SYMBOL_GPL(ufshcd_dealloc_host); |
| 6498 | |
| 6499 | /** |
Akinobu Mita | ca3d7bf | 2014-07-13 21:24:46 +0900 | [diff] [blame] | 6500 | * ufshcd_set_dma_mask - Set dma mask based on the controller |
| 6501 | * addressing capability |
| 6502 | * @hba: per adapter instance |
| 6503 | * |
| 6504 | * Returns 0 for success, non-zero for failure |
| 6505 | */ |
| 6506 | static int ufshcd_set_dma_mask(struct ufs_hba *hba) |
| 6507 | { |
| 6508 | if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) { |
| 6509 | if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64))) |
| 6510 | return 0; |
| 6511 | } |
| 6512 | return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32)); |
| 6513 | } |
| 6514 | |
| 6515 | /** |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6516 | * ufshcd_alloc_host - allocate Host Bus Adapter (HBA) |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6517 | * @dev: pointer to device handle |
| 6518 | * @hba_handle: driver private handle |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6519 | * Returns 0 on success, non-zero value on failure |
| 6520 | */ |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6521 | int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6522 | { |
| 6523 | struct Scsi_Host *host; |
| 6524 | struct ufs_hba *hba; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6525 | int err = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6526 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6527 | if (!dev) { |
| 6528 | dev_err(dev, |
| 6529 | "Invalid memory reference for dev is NULL\n"); |
| 6530 | err = -ENODEV; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6531 | goto out_error; |
| 6532 | } |
| 6533 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6534 | host = scsi_host_alloc(&ufshcd_driver_template, |
| 6535 | sizeof(struct ufs_hba)); |
| 6536 | if (!host) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6537 | dev_err(dev, "scsi_host_alloc failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6538 | err = -ENOMEM; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6539 | goto out_error; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6540 | } |
| 6541 | hba = shost_priv(host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6542 | hba->host = host; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6543 | hba->dev = dev; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6544 | *hba_handle = hba; |
| 6545 | |
| 6546 | out_error: |
| 6547 | return err; |
| 6548 | } |
| 6549 | EXPORT_SYMBOL(ufshcd_alloc_host); |
| 6550 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 6551 | static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up) |
| 6552 | { |
| 6553 | int ret = 0; |
| 6554 | struct ufs_clk_info *clki; |
| 6555 | struct list_head *head = &hba->clk_list_head; |
| 6556 | |
| 6557 | if (!head || list_empty(head)) |
| 6558 | goto out; |
| 6559 | |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 6560 | ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE); |
| 6561 | if (ret) |
| 6562 | return ret; |
| 6563 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 6564 | list_for_each_entry(clki, head, list) { |
| 6565 | if (!IS_ERR_OR_NULL(clki->clk)) { |
| 6566 | if (scale_up && clki->max_freq) { |
| 6567 | if (clki->curr_freq == clki->max_freq) |
| 6568 | continue; |
| 6569 | ret = clk_set_rate(clki->clk, clki->max_freq); |
| 6570 | if (ret) { |
| 6571 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 6572 | __func__, clki->name, |
| 6573 | clki->max_freq, ret); |
| 6574 | break; |
| 6575 | } |
| 6576 | clki->curr_freq = clki->max_freq; |
| 6577 | |
| 6578 | } else if (!scale_up && clki->min_freq) { |
| 6579 | if (clki->curr_freq == clki->min_freq) |
| 6580 | continue; |
| 6581 | ret = clk_set_rate(clki->clk, clki->min_freq); |
| 6582 | if (ret) { |
| 6583 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 6584 | __func__, clki->name, |
| 6585 | clki->min_freq, ret); |
| 6586 | break; |
| 6587 | } |
| 6588 | clki->curr_freq = clki->min_freq; |
| 6589 | } |
| 6590 | } |
| 6591 | dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__, |
| 6592 | clki->name, clk_get_rate(clki->clk)); |
| 6593 | } |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 6594 | |
| 6595 | ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE); |
| 6596 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 6597 | out: |
| 6598 | return ret; |
| 6599 | } |
| 6600 | |
| 6601 | static int ufshcd_devfreq_target(struct device *dev, |
| 6602 | unsigned long *freq, u32 flags) |
| 6603 | { |
| 6604 | int err = 0; |
| 6605 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 6606 | |
| 6607 | if (!ufshcd_is_clkscaling_enabled(hba)) |
| 6608 | return -EINVAL; |
| 6609 | |
| 6610 | if (*freq == UINT_MAX) |
| 6611 | err = ufshcd_scale_clks(hba, true); |
| 6612 | else if (*freq == 0) |
| 6613 | err = ufshcd_scale_clks(hba, false); |
| 6614 | |
| 6615 | return err; |
| 6616 | } |
| 6617 | |
| 6618 | static int ufshcd_devfreq_get_dev_status(struct device *dev, |
| 6619 | struct devfreq_dev_status *stat) |
| 6620 | { |
| 6621 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 6622 | struct ufs_clk_scaling *scaling = &hba->clk_scaling; |
| 6623 | unsigned long flags; |
| 6624 | |
| 6625 | if (!ufshcd_is_clkscaling_enabled(hba)) |
| 6626 | return -EINVAL; |
| 6627 | |
| 6628 | memset(stat, 0, sizeof(*stat)); |
| 6629 | |
| 6630 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 6631 | if (!scaling->window_start_t) |
| 6632 | goto start_window; |
| 6633 | |
| 6634 | if (scaling->is_busy_started) |
| 6635 | scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(), |
| 6636 | scaling->busy_start_t)); |
| 6637 | |
| 6638 | stat->total_time = jiffies_to_usecs((long)jiffies - |
| 6639 | (long)scaling->window_start_t); |
| 6640 | stat->busy_time = scaling->tot_busy_t; |
| 6641 | start_window: |
| 6642 | scaling->window_start_t = jiffies; |
| 6643 | scaling->tot_busy_t = 0; |
| 6644 | |
| 6645 | if (hba->outstanding_reqs) { |
| 6646 | scaling->busy_start_t = ktime_get(); |
| 6647 | scaling->is_busy_started = true; |
| 6648 | } else { |
| 6649 | scaling->busy_start_t = ktime_set(0, 0); |
| 6650 | scaling->is_busy_started = false; |
| 6651 | } |
| 6652 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6653 | return 0; |
| 6654 | } |
| 6655 | |
| 6656 | static struct devfreq_dev_profile ufs_devfreq_profile = { |
| 6657 | .polling_ms = 100, |
| 6658 | .target = ufshcd_devfreq_target, |
| 6659 | .get_dev_status = ufshcd_devfreq_get_dev_status, |
| 6660 | }; |
| 6661 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6662 | /** |
| 6663 | * ufshcd_init - Driver initialization routine |
| 6664 | * @hba: per-adapter instance |
| 6665 | * @mmio_base: base register address |
| 6666 | * @irq: Interrupt line of device |
| 6667 | * Returns 0 on success, non-zero value on failure |
| 6668 | */ |
| 6669 | int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq) |
| 6670 | { |
| 6671 | int err; |
| 6672 | struct Scsi_Host *host = hba->host; |
| 6673 | struct device *dev = hba->dev; |
| 6674 | |
| 6675 | if (!mmio_base) { |
| 6676 | dev_err(hba->dev, |
| 6677 | "Invalid memory reference for mmio_base is NULL\n"); |
| 6678 | err = -ENODEV; |
| 6679 | goto out_error; |
| 6680 | } |
| 6681 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6682 | hba->mmio_base = mmio_base; |
| 6683 | hba->irq = irq; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6684 | |
Potomski, MichalX | e192845 | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 6685 | /* Set descriptor lengths to specification defaults */ |
| 6686 | ufshcd_def_desc_sizes(hba); |
| 6687 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6688 | err = ufshcd_hba_init(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6689 | if (err) |
| 6690 | goto out_error; |
| 6691 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6692 | /* Read capabilities registers */ |
| 6693 | ufshcd_hba_capabilities(hba); |
| 6694 | |
| 6695 | /* Get UFS version supported by the controller */ |
| 6696 | hba->ufs_version = ufshcd_get_ufs_version(hba); |
| 6697 | |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 6698 | /* Get Interrupt bit mask per version */ |
| 6699 | hba->intr_mask = ufshcd_get_intr_mask(hba); |
| 6700 | |
Akinobu Mita | ca3d7bf | 2014-07-13 21:24:46 +0900 | [diff] [blame] | 6701 | err = ufshcd_set_dma_mask(hba); |
| 6702 | if (err) { |
| 6703 | dev_err(hba->dev, "set dma mask failed\n"); |
| 6704 | goto out_disable; |
| 6705 | } |
| 6706 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6707 | /* Allocate memory for host memory space */ |
| 6708 | err = ufshcd_memory_alloc(hba); |
| 6709 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6710 | dev_err(hba->dev, "Memory allocation failed\n"); |
| 6711 | goto out_disable; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6712 | } |
| 6713 | |
| 6714 | /* Configure LRB */ |
| 6715 | ufshcd_host_memory_configure(hba); |
| 6716 | |
| 6717 | host->can_queue = hba->nutrs; |
| 6718 | host->cmd_per_lun = hba->nutrs; |
| 6719 | host->max_id = UFSHCD_MAX_ID; |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 6720 | host->max_lun = UFS_MAX_LUNS; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6721 | host->max_channel = UFSHCD_MAX_CHANNEL; |
| 6722 | host->unique_id = host->host_no; |
| 6723 | host->max_cmd_len = MAX_CDB_SIZE; |
| 6724 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 6725 | hba->max_pwr_info.is_valid = false; |
| 6726 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6727 | /* Initailize wait queue for task management */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6728 | init_waitqueue_head(&hba->tm_wq); |
| 6729 | init_waitqueue_head(&hba->tm_tag_wq); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6730 | |
| 6731 | /* Initialize work queues */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6732 | INIT_WORK(&hba->eh_work, ufshcd_err_handler); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 6733 | INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6734 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6735 | /* Initialize UIC command mutex */ |
| 6736 | mutex_init(&hba->uic_cmd_mutex); |
| 6737 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 6738 | /* Initialize mutex for device management commands */ |
| 6739 | mutex_init(&hba->dev_cmd.lock); |
| 6740 | |
| 6741 | /* Initialize device management tag acquire wait queue */ |
| 6742 | init_waitqueue_head(&hba->dev_cmd.tag_wq); |
| 6743 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6744 | ufshcd_init_clk_gating(hba); |
Yaniv Gardi | 199ef13 | 2016-03-10 17:37:06 +0200 | [diff] [blame] | 6745 | |
| 6746 | /* |
| 6747 | * In order to avoid any spurious interrupt immediately after |
| 6748 | * registering UFS controller interrupt handler, clear any pending UFS |
| 6749 | * interrupt status and disable all the UFS interrupts. |
| 6750 | */ |
| 6751 | ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS), |
| 6752 | REG_INTERRUPT_STATUS); |
| 6753 | ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE); |
| 6754 | /* |
| 6755 | * Make sure that UFS interrupts are disabled and any pending interrupt |
| 6756 | * status is cleared before registering UFS interrupt handler. |
| 6757 | */ |
| 6758 | mb(); |
| 6759 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6760 | /* IRQ registration */ |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 6761 | err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6762 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6763 | dev_err(hba->dev, "request irq failed\n"); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6764 | goto exit_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6765 | } else { |
| 6766 | hba->is_irq_enabled = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6767 | } |
| 6768 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6769 | err = scsi_add_host(host, hba->dev); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6770 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6771 | dev_err(hba->dev, "scsi_add_host failed\n"); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6772 | goto exit_gating; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6773 | } |
| 6774 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6775 | /* Host controller enable */ |
| 6776 | err = ufshcd_hba_enable(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6777 | if (err) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6778 | dev_err(hba->dev, "Host controller enable failed\n"); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6779 | goto out_remove_scsi_host; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6780 | } |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6781 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 6782 | if (ufshcd_is_clkscaling_enabled(hba)) { |
| 6783 | hba->devfreq = devfreq_add_device(dev, &ufs_devfreq_profile, |
| 6784 | "simple_ondemand", NULL); |
| 6785 | if (IS_ERR(hba->devfreq)) { |
| 6786 | dev_err(hba->dev, "Unable to register with devfreq %ld\n", |
| 6787 | PTR_ERR(hba->devfreq)); |
Wei Yongjun | 73811c9 | 2016-09-28 14:49:42 +0000 | [diff] [blame] | 6788 | err = PTR_ERR(hba->devfreq); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 6789 | goto out_remove_scsi_host; |
| 6790 | } |
| 6791 | /* Suspend devfreq until the UFS device is detected */ |
| 6792 | devfreq_suspend_device(hba->devfreq); |
| 6793 | hba->clk_scaling.window_start_t = 0; |
| 6794 | } |
| 6795 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 6796 | /* Hold auto suspend until async scan completes */ |
| 6797 | pm_runtime_get_sync(dev); |
| 6798 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6799 | /* |
subhashj@codeaurora.org | c5fc946 | 2017-04-04 19:32:20 +0000 | [diff] [blame] | 6800 | * We are assuming that device wasn't put in sleep/power-down |
| 6801 | * state exclusively during the boot stage before kernel. |
| 6802 | * This assumption helps avoid doing link startup twice during |
| 6803 | * ufshcd_probe_hba(). |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6804 | */ |
subhashj@codeaurora.org | c5fc946 | 2017-04-04 19:32:20 +0000 | [diff] [blame] | 6805 | ufshcd_set_ufs_dev_active(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6806 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6807 | async_schedule(ufshcd_async_scan, hba); |
| 6808 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6809 | return 0; |
| 6810 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6811 | out_remove_scsi_host: |
| 6812 | scsi_remove_host(hba->host); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6813 | exit_gating: |
| 6814 | ufshcd_exit_clk_gating(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6815 | out_disable: |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6816 | hba->is_irq_enabled = false; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6817 | scsi_host_put(host); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6818 | ufshcd_hba_exit(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6819 | out_error: |
| 6820 | return err; |
| 6821 | } |
| 6822 | EXPORT_SYMBOL_GPL(ufshcd_init); |
| 6823 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6824 | MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>"); |
| 6825 | MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>"); |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 6826 | MODULE_DESCRIPTION("Generic UFS host controller driver Core"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6827 | MODULE_LICENSE("GPL"); |
| 6828 | MODULE_VERSION(UFSHCD_DRIVER_VERSION); |