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