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