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