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