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