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