Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1 | /* |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 2 | * Universal Flash Storage Host controller driver Core |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3 | * |
| 4 | * This code is based on drivers/scsi/ufs/ufshcd.c |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 5 | * Copyright (C) 2011-2013 Samsung India Software Operations |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 6 | * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7 | * |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8 | * Authors: |
| 9 | * Santosh Yaraganavi <santosh.sy@samsung.com> |
| 10 | * Vinayak Holikatti <h.vinayak@samsung.com> |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 16 | * See the COPYING file in the top-level directory or visit |
| 17 | * <http://www.gnu.org/licenses/gpl-2.0.html> |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 24 | * This program is provided "AS IS" and "WITH ALL FAULTS" and |
| 25 | * without warranty of any kind. You are solely responsible for |
| 26 | * determining the appropriateness of using and distributing |
| 27 | * the program and assume all risks associated with your exercise |
| 28 | * of rights with respect to the program, including but not limited |
| 29 | * to infringement of third party rights, the risks and costs of |
| 30 | * program errors, damage to or loss of data, programs or equipment, |
| 31 | * and unavailability or interruption of operations. Under no |
| 32 | * circumstances will the contributor of this Program be liable for |
| 33 | * any damages of any kind arising from your use or distribution of |
| 34 | * this program. |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 35 | * |
| 36 | * The Linux Foundation chooses to take subject only to the GPLv2 |
| 37 | * license terms, and distributes only under these terms. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 38 | */ |
| 39 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 40 | #include <linux/async.h> |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 41 | #include <linux/devfreq.h> |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 42 | #include <linux/nls.h> |
Yaniv Gardi | 54b879b | 2016-03-10 17:37:05 +0200 | [diff] [blame] | 43 | #include <linux/of.h> |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 44 | #include "ufshcd.h" |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 45 | #include "ufs_quirks.h" |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 46 | #include "unipro.h" |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 47 | |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 48 | #define CREATE_TRACE_POINTS |
| 49 | #include <trace/events/ufs.h> |
| 50 | |
Gilad Broner | dcea0bf | 2016-10-17 17:09:48 -0700 | [diff] [blame] | 51 | #define UFSHCD_REQ_SENSE_SIZE 18 |
| 52 | |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 53 | #define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\ |
| 54 | UTP_TASK_REQ_COMPL |\ |
| 55 | UFSHCD_ERROR_MASK) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 56 | /* UIC command timeout, unit: ms */ |
| 57 | #define UIC_CMD_TIMEOUT 500 |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 58 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 59 | /* NOP OUT retries waiting for NOP IN response */ |
| 60 | #define NOP_OUT_RETRIES 10 |
| 61 | /* Timeout after 30 msecs if NOP OUT hangs without response */ |
| 62 | #define NOP_OUT_TIMEOUT 30 /* msecs */ |
| 63 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 64 | /* Query request retries */ |
subhashj@codeaurora.org | 10fe588 | 2016-11-23 16:31:52 -0800 | [diff] [blame] | 65 | #define QUERY_REQ_RETRIES 3 |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 66 | /* Query request timeout */ |
subhashj@codeaurora.org | 10fe588 | 2016-11-23 16:31:52 -0800 | [diff] [blame] | 67 | #define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */ |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 68 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 69 | /* Task management command timeout */ |
| 70 | #define TM_CMD_TIMEOUT 100 /* msecs */ |
| 71 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 72 | /* maximum number of retries for a general UIC command */ |
| 73 | #define UFS_UIC_COMMAND_RETRIES 3 |
| 74 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 75 | /* maximum number of link-startup retries */ |
| 76 | #define DME_LINKSTARTUP_RETRIES 3 |
| 77 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 78 | /* Maximum retries for Hibern8 enter */ |
| 79 | #define UIC_HIBERN8_ENTER_RETRIES 3 |
| 80 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 81 | /* maximum number of reset retries before giving up */ |
| 82 | #define MAX_HOST_RESET_RETRIES 5 |
| 83 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 84 | /* Expose the flag value from utp_upiu_query.value */ |
| 85 | #define MASK_QUERY_UPIU_FLAG_LOC 0xFF |
| 86 | |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 87 | /* Interrupt aggregation default timeout, unit: 40us */ |
| 88 | #define INT_AGGR_DEF_TO 0x02 |
| 89 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 90 | #define ufshcd_toggle_vreg(_dev, _vreg, _on) \ |
| 91 | ({ \ |
| 92 | int _ret; \ |
| 93 | if (_on) \ |
| 94 | _ret = ufshcd_enable_vreg(_dev, _vreg); \ |
| 95 | else \ |
| 96 | _ret = ufshcd_disable_vreg(_dev, _vreg); \ |
| 97 | _ret; \ |
| 98 | }) |
| 99 | |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 100 | #define ufshcd_hex_dump(prefix_str, buf, len) \ |
| 101 | print_hex_dump(KERN_ERR, prefix_str, DUMP_PREFIX_OFFSET, 16, 4, buf, len, false) |
| 102 | |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 103 | static u32 ufs_query_desc_max_size[] = { |
| 104 | QUERY_DESC_DEVICE_MAX_SIZE, |
| 105 | QUERY_DESC_CONFIGURAION_MAX_SIZE, |
| 106 | QUERY_DESC_UNIT_MAX_SIZE, |
| 107 | QUERY_DESC_RFU_MAX_SIZE, |
| 108 | QUERY_DESC_INTERCONNECT_MAX_SIZE, |
| 109 | QUERY_DESC_STRING_MAX_SIZE, |
| 110 | QUERY_DESC_RFU_MAX_SIZE, |
Tomas Winkler | 1ce2179 | 2016-02-09 10:25:40 +0200 | [diff] [blame] | 111 | QUERY_DESC_GEOMETRY_MAX_SIZE, |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 112 | QUERY_DESC_POWER_MAX_SIZE, |
| 113 | QUERY_DESC_RFU_MAX_SIZE, |
| 114 | }; |
| 115 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 116 | enum { |
| 117 | UFSHCD_MAX_CHANNEL = 0, |
| 118 | UFSHCD_MAX_ID = 1, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 119 | UFSHCD_CMD_PER_LUN = 32, |
| 120 | UFSHCD_CAN_QUEUE = 32, |
| 121 | }; |
| 122 | |
| 123 | /* UFSHCD states */ |
| 124 | enum { |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 125 | UFSHCD_STATE_RESET, |
| 126 | UFSHCD_STATE_ERROR, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 127 | UFSHCD_STATE_OPERATIONAL, |
Zang Leigang | 141f816 | 2016-11-16 11:29:37 +0800 | [diff] [blame] | 128 | UFSHCD_STATE_EH_SCHEDULED, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | /* UFSHCD error handling flags */ |
| 132 | enum { |
| 133 | UFSHCD_EH_IN_PROGRESS = (1 << 0), |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 134 | }; |
| 135 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 136 | /* UFSHCD UIC layer error flags */ |
| 137 | enum { |
| 138 | UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 139 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */ |
| 140 | UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */ |
| 141 | UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */ |
| 142 | UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */ |
| 143 | UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 144 | }; |
| 145 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 146 | /* Interrupt configuration options */ |
| 147 | enum { |
| 148 | UFSHCD_INT_DISABLE, |
| 149 | UFSHCD_INT_ENABLE, |
| 150 | UFSHCD_INT_CLEAR, |
| 151 | }; |
| 152 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 153 | #define ufshcd_set_eh_in_progress(h) \ |
| 154 | (h->eh_flags |= UFSHCD_EH_IN_PROGRESS) |
| 155 | #define ufshcd_eh_in_progress(h) \ |
| 156 | (h->eh_flags & UFSHCD_EH_IN_PROGRESS) |
| 157 | #define ufshcd_clear_eh_in_progress(h) \ |
| 158 | (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS) |
| 159 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 160 | #define ufshcd_set_ufs_dev_active(h) \ |
| 161 | ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE) |
| 162 | #define ufshcd_set_ufs_dev_sleep(h) \ |
| 163 | ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE) |
| 164 | #define ufshcd_set_ufs_dev_poweroff(h) \ |
| 165 | ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE) |
| 166 | #define ufshcd_is_ufs_dev_active(h) \ |
| 167 | ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE) |
| 168 | #define ufshcd_is_ufs_dev_sleep(h) \ |
| 169 | ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE) |
| 170 | #define ufshcd_is_ufs_dev_poweroff(h) \ |
| 171 | ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE) |
| 172 | |
| 173 | static struct ufs_pm_lvl_states ufs_pm_lvl_states[] = { |
| 174 | {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE}, |
| 175 | {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 176 | {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE}, |
| 177 | {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 178 | {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 179 | {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE}, |
| 180 | }; |
| 181 | |
| 182 | static inline enum ufs_dev_pwr_mode |
| 183 | ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl) |
| 184 | { |
| 185 | return ufs_pm_lvl_states[lvl].dev_state; |
| 186 | } |
| 187 | |
| 188 | static inline enum uic_link_state |
| 189 | ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl) |
| 190 | { |
| 191 | return ufs_pm_lvl_states[lvl].link_state; |
| 192 | } |
| 193 | |
subhashj@codeaurora.org | 0c8f758 | 2016-12-22 18:41:11 -0800 | [diff] [blame] | 194 | static inline enum ufs_pm_level |
| 195 | ufs_get_desired_pm_lvl_for_dev_link_state(enum ufs_dev_pwr_mode dev_state, |
| 196 | enum uic_link_state link_state) |
| 197 | { |
| 198 | enum ufs_pm_level lvl; |
| 199 | |
| 200 | for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) { |
| 201 | if ((ufs_pm_lvl_states[lvl].dev_state == dev_state) && |
| 202 | (ufs_pm_lvl_states[lvl].link_state == link_state)) |
| 203 | return lvl; |
| 204 | } |
| 205 | |
| 206 | /* if no match found, return the level 0 */ |
| 207 | return UFS_PM_LVL_0; |
| 208 | } |
| 209 | |
Subhash Jadavani | 56d4a18 | 2016-12-05 19:25:32 -0800 | [diff] [blame] | 210 | static struct ufs_dev_fix ufs_fixups[] = { |
| 211 | /* UFS cards deviations table */ |
| 212 | UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, |
| 213 | UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM), |
| 214 | UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, UFS_DEVICE_NO_VCCQ), |
| 215 | UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, |
| 216 | UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS), |
| 217 | UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, |
| 218 | UFS_DEVICE_NO_FASTAUTO), |
| 219 | UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, |
| 220 | UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE), |
| 221 | UFS_FIX(UFS_VENDOR_TOSHIBA, UFS_ANY_MODEL, |
| 222 | UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM), |
| 223 | UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9C8KBADG", |
| 224 | UFS_DEVICE_QUIRK_PA_TACTIVATE), |
| 225 | UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9D8KBADG", |
| 226 | UFS_DEVICE_QUIRK_PA_TACTIVATE), |
| 227 | UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL, UFS_DEVICE_NO_VCCQ), |
| 228 | UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL, |
| 229 | UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME), |
| 230 | |
| 231 | END_FIX |
| 232 | }; |
| 233 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 234 | static void ufshcd_tmc_handler(struct ufs_hba *hba); |
| 235 | static void ufshcd_async_scan(void *data, async_cookie_t cookie); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 236 | static int ufshcd_reset_and_restore(struct ufs_hba *hba); |
Dolev Raviv | e7d3825 | 2016-12-22 18:40:07 -0800 | [diff] [blame] | 237 | static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 238 | 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] | 239 | static void ufshcd_hba_exit(struct ufs_hba *hba); |
| 240 | static int ufshcd_probe_hba(struct ufs_hba *hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 241 | static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on, |
| 242 | bool skip_ref_clk); |
| 243 | static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on); |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 244 | static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 245 | static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba); |
| 246 | static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 247 | static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 248 | static int ufshcd_host_reset_and_restore(struct ufs_hba *hba); |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 249 | static void ufshcd_resume_clkscaling(struct ufs_hba *hba); |
| 250 | static void ufshcd_suspend_clkscaling(struct ufs_hba *hba); |
| 251 | static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 252 | static irqreturn_t ufshcd_intr(int irq, void *__hba); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 253 | static int ufshcd_config_pwr_mode(struct ufs_hba *hba, |
| 254 | struct ufs_pa_layer_attr *desired_pwr_mode); |
Yaniv Gardi | 874237f | 2015-05-17 18:55:03 +0300 | [diff] [blame] | 255 | static int ufshcd_change_power_mode(struct ufs_hba *hba, |
| 256 | struct ufs_pa_layer_attr *pwr_mode); |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 257 | static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag) |
| 258 | { |
| 259 | return tag >= 0 && tag < hba->nutrs; |
| 260 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 261 | |
| 262 | static inline int ufshcd_enable_irq(struct ufs_hba *hba) |
| 263 | { |
| 264 | int ret = 0; |
| 265 | |
| 266 | if (!hba->is_irq_enabled) { |
| 267 | ret = request_irq(hba->irq, ufshcd_intr, IRQF_SHARED, UFSHCD, |
| 268 | hba); |
| 269 | if (ret) |
| 270 | dev_err(hba->dev, "%s: request_irq failed, ret=%d\n", |
| 271 | __func__, ret); |
| 272 | hba->is_irq_enabled = true; |
| 273 | } |
| 274 | |
| 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | static inline void ufshcd_disable_irq(struct ufs_hba *hba) |
| 279 | { |
| 280 | if (hba->is_irq_enabled) { |
| 281 | free_irq(hba->irq, hba); |
| 282 | hba->is_irq_enabled = false; |
| 283 | } |
| 284 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 285 | |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 286 | /* replace non-printable or non-ASCII characters with spaces */ |
| 287 | static inline void ufshcd_remove_non_printable(char *val) |
| 288 | { |
| 289 | if (!val) |
| 290 | return; |
| 291 | |
| 292 | if (*val < 0x20 || *val > 0x7e) |
| 293 | *val = ' '; |
| 294 | } |
| 295 | |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 296 | static void ufshcd_print_host_regs(struct ufs_hba *hba) |
| 297 | { |
| 298 | /* |
| 299 | * hex_dump reads its data without the readl macro. This might |
| 300 | * cause inconsistency issues on some platform, as the printed |
| 301 | * values may be from cache and not the most recent value. |
| 302 | * To know whether you are looking at an un-cached version verify |
| 303 | * that IORESOURCE_MEM flag is on when xxx_get_resource() is invoked |
| 304 | * during platform/pci probe function. |
| 305 | */ |
| 306 | ufshcd_hex_dump("host regs: ", hba->mmio_base, UFSHCI_REG_SPACE_SIZE); |
| 307 | dev_err(hba->dev, "hba->ufs_version = 0x%x, hba->capabilities = 0x%x\n", |
| 308 | hba->ufs_version, hba->capabilities); |
| 309 | dev_err(hba->dev, |
| 310 | "hba->outstanding_reqs = 0x%x, hba->outstanding_tasks = 0x%x\n", |
| 311 | (u32)hba->outstanding_reqs, (u32)hba->outstanding_tasks); |
| 312 | } |
| 313 | |
| 314 | static |
| 315 | void ufshcd_print_trs(struct ufs_hba *hba, unsigned long bitmap, bool pr_prdt) |
| 316 | { |
| 317 | struct ufshcd_lrb *lrbp; |
| 318 | int tag; |
| 319 | |
| 320 | for_each_set_bit(tag, &bitmap, hba->nutrs) { |
| 321 | lrbp = &hba->lrb[tag]; |
| 322 | |
| 323 | dev_err(hba->dev, "UPIU[%d] - Transfer Request Descriptor\n", |
| 324 | tag); |
| 325 | ufshcd_hex_dump("UPIU TRD: ", lrbp->utr_descriptor_ptr, |
| 326 | sizeof(struct utp_transfer_req_desc)); |
| 327 | dev_err(hba->dev, "UPIU[%d] - Request UPIU\n", tag); |
| 328 | ufshcd_hex_dump("UPIU REQ: ", lrbp->ucd_req_ptr, |
| 329 | sizeof(struct utp_upiu_req)); |
| 330 | dev_err(hba->dev, "UPIU[%d] - Response UPIU\n", tag); |
| 331 | ufshcd_hex_dump("UPIU RSP: ", lrbp->ucd_rsp_ptr, |
| 332 | sizeof(struct utp_upiu_rsp)); |
| 333 | if (pr_prdt) { |
| 334 | int prdt_length = le16_to_cpu( |
| 335 | lrbp->utr_descriptor_ptr->prd_table_length); |
| 336 | |
| 337 | dev_err(hba->dev, "UPIU[%d] - PRDT - %d entries\n", tag, |
| 338 | prdt_length); |
| 339 | ufshcd_hex_dump("UPIU PRDT: ", lrbp->ucd_prdt_ptr, |
| 340 | sizeof(struct ufshcd_sg_entry) * |
| 341 | prdt_length); |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap) |
| 347 | { |
| 348 | struct utp_task_req_desc *tmrdp; |
| 349 | int tag; |
| 350 | |
| 351 | for_each_set_bit(tag, &bitmap, hba->nutmrs) { |
| 352 | tmrdp = &hba->utmrdl_base_addr[tag]; |
| 353 | dev_err(hba->dev, "TM[%d] - Task Management Header\n", tag); |
| 354 | ufshcd_hex_dump("TM TRD: ", &tmrdp->header, |
| 355 | sizeof(struct request_desc_header)); |
| 356 | dev_err(hba->dev, "TM[%d] - Task Management Request UPIU\n", |
| 357 | tag); |
| 358 | ufshcd_hex_dump("TM REQ: ", tmrdp->task_req_upiu, |
| 359 | sizeof(struct utp_upiu_req)); |
| 360 | dev_err(hba->dev, "TM[%d] - Task Management Response UPIU\n", |
| 361 | tag); |
| 362 | ufshcd_hex_dump("TM RSP: ", tmrdp->task_rsp_upiu, |
| 363 | sizeof(struct utp_task_req_desc)); |
| 364 | } |
| 365 | } |
| 366 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 367 | /* |
| 368 | * ufshcd_wait_for_register - wait for register value to change |
| 369 | * @hba - per-adapter interface |
| 370 | * @reg - mmio register offset |
| 371 | * @mask - mask to apply to read register value |
| 372 | * @val - wait condition |
| 373 | * @interval_us - polling interval in microsecs |
| 374 | * @timeout_ms - timeout in millisecs |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 375 | * @can_sleep - perform sleep or just spin |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 376 | * |
| 377 | * Returns -ETIMEDOUT on error, zero on success |
| 378 | */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 379 | int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask, |
| 380 | u32 val, unsigned long interval_us, |
| 381 | unsigned long timeout_ms, bool can_sleep) |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 382 | { |
| 383 | int err = 0; |
| 384 | unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms); |
| 385 | |
| 386 | /* ignore bits that we don't intend to wait on */ |
| 387 | val = val & mask; |
| 388 | |
| 389 | while ((ufshcd_readl(hba, reg) & mask) != val) { |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 390 | if (can_sleep) |
| 391 | usleep_range(interval_us, interval_us + 50); |
| 392 | else |
| 393 | udelay(interval_us); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 394 | if (time_after(jiffies, timeout)) { |
| 395 | if ((ufshcd_readl(hba, reg) & mask) != val) |
| 396 | err = -ETIMEDOUT; |
| 397 | break; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | return err; |
| 402 | } |
| 403 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 404 | /** |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 405 | * ufshcd_get_intr_mask - Get the interrupt bit mask |
| 406 | * @hba - Pointer to adapter instance |
| 407 | * |
| 408 | * Returns interrupt bit mask per version |
| 409 | */ |
| 410 | static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba) |
| 411 | { |
Yaniv Gardi | c01848c | 2016-12-05 19:25:02 -0800 | [diff] [blame] | 412 | u32 intr_mask = 0; |
| 413 | |
| 414 | switch (hba->ufs_version) { |
| 415 | case UFSHCI_VERSION_10: |
| 416 | intr_mask = INTERRUPT_MASK_ALL_VER_10; |
| 417 | break; |
| 418 | /* allow fall through */ |
| 419 | case UFSHCI_VERSION_11: |
| 420 | case UFSHCI_VERSION_20: |
| 421 | intr_mask = INTERRUPT_MASK_ALL_VER_11; |
| 422 | break; |
| 423 | /* allow fall through */ |
| 424 | case UFSHCI_VERSION_21: |
| 425 | default: |
| 426 | intr_mask = INTERRUPT_MASK_ALL_VER_21; |
| 427 | } |
| 428 | |
| 429 | return intr_mask; |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 433 | * ufshcd_get_ufs_version - Get the UFS version supported by the HBA |
| 434 | * @hba - Pointer to adapter instance |
| 435 | * |
| 436 | * Returns UFSHCI version supported by the controller |
| 437 | */ |
| 438 | static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba) |
| 439 | { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 440 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION) |
| 441 | return ufshcd_vops_get_ufs_hci_version(hba); |
Yaniv Gardi | 9949e70 | 2015-05-17 18:55:05 +0300 | [diff] [blame] | 442 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 443 | return ufshcd_readl(hba, REG_UFS_VERSION); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /** |
| 447 | * ufshcd_is_device_present - Check if any device connected to |
| 448 | * the host controller |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 449 | * @hba: pointer to adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 450 | * |
Venkatraman S | 73ec513 | 2012-07-10 19:39:23 +0530 | [diff] [blame] | 451 | * Returns 1 if device present, 0 if no device detected |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 452 | */ |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 453 | static inline int ufshcd_is_device_present(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 454 | { |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 455 | return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & |
| 456 | DEVICE_PRESENT) ? 1 : 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | /** |
| 460 | * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status |
| 461 | * @lrb: pointer to local command reference block |
| 462 | * |
| 463 | * This function is used to get the OCS field from UTRD |
| 464 | * Returns the OCS field in the UTRD |
| 465 | */ |
| 466 | static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp) |
| 467 | { |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 468 | 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] | 469 | } |
| 470 | |
| 471 | /** |
| 472 | * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status |
| 473 | * @task_req_descp: pointer to utp_task_req_desc structure |
| 474 | * |
| 475 | * This function is used to get the OCS field from UTMRD |
| 476 | * Returns the OCS field in the UTMRD |
| 477 | */ |
| 478 | static inline int |
| 479 | ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp) |
| 480 | { |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 481 | return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | /** |
| 485 | * ufshcd_get_tm_free_slot - get a free slot for task management request |
| 486 | * @hba: per adapter instance |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 487 | * @free_slot: pointer to variable with available slot value |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 488 | * |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 489 | * Get a free tag and lock it until ufshcd_put_tm_slot() is called. |
| 490 | * Returns 0 if free slot is not available, else return 1 with tag value |
| 491 | * in @free_slot. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 492 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 493 | 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] | 494 | { |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 495 | int tag; |
| 496 | bool ret = false; |
| 497 | |
| 498 | if (!free_slot) |
| 499 | goto out; |
| 500 | |
| 501 | do { |
| 502 | tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs); |
| 503 | if (tag >= hba->nutmrs) |
| 504 | goto out; |
| 505 | } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use)); |
| 506 | |
| 507 | *free_slot = tag; |
| 508 | ret = true; |
| 509 | out: |
| 510 | return ret; |
| 511 | } |
| 512 | |
| 513 | static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot) |
| 514 | { |
| 515 | clear_bit_unlock(slot, &hba->tm_slots_in_use); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /** |
| 519 | * ufshcd_utrl_clear - Clear a bit in UTRLCLR register |
| 520 | * @hba: per adapter instance |
| 521 | * @pos: position of the bit to be cleared |
| 522 | */ |
| 523 | static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos) |
| 524 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 525 | ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | /** |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 529 | * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field |
| 530 | * @hba: per adapter instance |
| 531 | * @tag: position of the bit to be cleared |
| 532 | */ |
| 533 | static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag) |
| 534 | { |
| 535 | __clear_bit(tag, &hba->outstanding_reqs); |
| 536 | } |
| 537 | |
| 538 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 539 | * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY |
| 540 | * @reg: Register value of host controller status |
| 541 | * |
| 542 | * Returns integer, 0 on Success and positive value if failed |
| 543 | */ |
| 544 | static inline int ufshcd_get_lists_status(u32 reg) |
| 545 | { |
| 546 | /* |
| 547 | * The mask 0xFF is for the following HCS register bits |
| 548 | * Bit Description |
| 549 | * 0 Device Present |
| 550 | * 1 UTRLRDY |
| 551 | * 2 UTMRLRDY |
| 552 | * 3 UCRDY |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 553 | * 4-7 reserved |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 554 | */ |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 555 | return ((reg & 0xFF) >> 1) ^ 0x07; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /** |
| 559 | * ufshcd_get_uic_cmd_result - Get the UIC command result |
| 560 | * @hba: Pointer to adapter instance |
| 561 | * |
| 562 | * This function gets the result of UIC command completion |
| 563 | * Returns 0 on success, non zero value on error |
| 564 | */ |
| 565 | static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba) |
| 566 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 567 | return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) & |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 568 | MASK_UIC_COMMAND_RESULT; |
| 569 | } |
| 570 | |
| 571 | /** |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 572 | * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command |
| 573 | * @hba: Pointer to adapter instance |
| 574 | * |
| 575 | * This function gets UIC command argument3 |
| 576 | * Returns 0 on success, non zero value on error |
| 577 | */ |
| 578 | static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba) |
| 579 | { |
| 580 | return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3); |
| 581 | } |
| 582 | |
| 583 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 584 | * ufshcd_get_req_rsp - returns the TR response transaction type |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 585 | * @ucd_rsp_ptr: pointer to response UPIU |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 586 | */ |
| 587 | static inline int |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 588 | ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 589 | { |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 590 | return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | /** |
| 594 | * ufshcd_get_rsp_upiu_result - Get the result from response UPIU |
| 595 | * @ucd_rsp_ptr: pointer to response UPIU |
| 596 | * |
| 597 | * This function gets the response status and scsi_status from response UPIU |
| 598 | * Returns the response result code. |
| 599 | */ |
| 600 | static inline int |
| 601 | ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 602 | { |
| 603 | return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT; |
| 604 | } |
| 605 | |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 606 | /* |
| 607 | * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length |
| 608 | * from response UPIU |
| 609 | * @ucd_rsp_ptr: pointer to response UPIU |
| 610 | * |
| 611 | * Return the data segment length. |
| 612 | */ |
| 613 | static inline unsigned int |
| 614 | ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 615 | { |
| 616 | return be32_to_cpu(ucd_rsp_ptr->header.dword_2) & |
| 617 | MASK_RSP_UPIU_DATA_SEG_LEN; |
| 618 | } |
| 619 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 620 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 621 | * ufshcd_is_exception_event - Check if the device raised an exception event |
| 622 | * @ucd_rsp_ptr: pointer to response UPIU |
| 623 | * |
| 624 | * The function checks if the device raised an exception event indicated in |
| 625 | * the Device Information field of response UPIU. |
| 626 | * |
| 627 | * Returns true if exception is raised, false otherwise. |
| 628 | */ |
| 629 | static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 630 | { |
| 631 | return be32_to_cpu(ucd_rsp_ptr->header.dword_2) & |
| 632 | MASK_RSP_EXCEPTION_EVENT ? true : false; |
| 633 | } |
| 634 | |
| 635 | /** |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 636 | * ufshcd_reset_intr_aggr - Reset interrupt aggregation values. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 637 | * @hba: per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 638 | */ |
| 639 | static inline void |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 640 | ufshcd_reset_intr_aggr(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 641 | { |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 642 | ufshcd_writel(hba, INT_AGGR_ENABLE | |
| 643 | INT_AGGR_COUNTER_AND_TIMER_RESET, |
| 644 | REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * ufshcd_config_intr_aggr - Configure interrupt aggregation values. |
| 649 | * @hba: per adapter instance |
| 650 | * @cnt: Interrupt aggregation counter threshold |
| 651 | * @tmout: Interrupt aggregation timeout value |
| 652 | */ |
| 653 | static inline void |
| 654 | ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout) |
| 655 | { |
| 656 | ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE | |
| 657 | INT_AGGR_COUNTER_THLD_VAL(cnt) | |
| 658 | INT_AGGR_TIMEOUT_VAL(tmout), |
| 659 | REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | /** |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 663 | * ufshcd_disable_intr_aggr - Disables interrupt aggregation. |
| 664 | * @hba: per adapter instance |
| 665 | */ |
| 666 | static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba) |
| 667 | { |
| 668 | ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
| 669 | } |
| 670 | |
| 671 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 672 | * ufshcd_enable_run_stop_reg - Enable run-stop registers, |
| 673 | * When run-stop registers are set to 1, it indicates the |
| 674 | * host controller that it can process the requests |
| 675 | * @hba: per adapter instance |
| 676 | */ |
| 677 | static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba) |
| 678 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 679 | ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT, |
| 680 | REG_UTP_TASK_REQ_LIST_RUN_STOP); |
| 681 | ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT, |
| 682 | REG_UTP_TRANSFER_REQ_LIST_RUN_STOP); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 686 | * ufshcd_hba_start - Start controller initialization sequence |
| 687 | * @hba: per adapter instance |
| 688 | */ |
| 689 | static inline void ufshcd_hba_start(struct ufs_hba *hba) |
| 690 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 691 | ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | /** |
| 695 | * ufshcd_is_hba_active - Get controller state |
| 696 | * @hba: per adapter instance |
| 697 | * |
| 698 | * Returns zero if controller is active, 1 otherwise |
| 699 | */ |
| 700 | static inline int ufshcd_is_hba_active(struct ufs_hba *hba) |
| 701 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 702 | return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 703 | } |
| 704 | |
subhashj@codeaurora.org | 09690d5 | 2016-12-22 18:41:00 -0800 | [diff] [blame] | 705 | static const char *ufschd_uic_link_state_to_string( |
| 706 | enum uic_link_state state) |
| 707 | { |
| 708 | switch (state) { |
| 709 | case UIC_LINK_OFF_STATE: return "OFF"; |
| 710 | case UIC_LINK_ACTIVE_STATE: return "ACTIVE"; |
| 711 | case UIC_LINK_HIBERN8_STATE: return "HIBERN8"; |
| 712 | default: return "UNKNOWN"; |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | static const char *ufschd_ufs_dev_pwr_mode_to_string( |
| 717 | enum ufs_dev_pwr_mode state) |
| 718 | { |
| 719 | switch (state) { |
| 720 | case UFS_ACTIVE_PWR_MODE: return "ACTIVE"; |
| 721 | case UFS_SLEEP_PWR_MODE: return "SLEEP"; |
| 722 | case UFS_POWERDOWN_PWR_MODE: return "POWERDOWN"; |
| 723 | default: return "UNKNOWN"; |
| 724 | } |
| 725 | } |
| 726 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 727 | u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba) |
| 728 | { |
| 729 | /* HCI version 1.0 and 1.1 supports UniPro 1.41 */ |
| 730 | if ((hba->ufs_version == UFSHCI_VERSION_10) || |
| 731 | (hba->ufs_version == UFSHCI_VERSION_11)) |
| 732 | return UFS_UNIPRO_VER_1_41; |
| 733 | else |
| 734 | return UFS_UNIPRO_VER_1_6; |
| 735 | } |
| 736 | EXPORT_SYMBOL(ufshcd_get_local_unipro_ver); |
| 737 | |
| 738 | static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba) |
| 739 | { |
| 740 | /* |
| 741 | * If both host and device support UniPro ver1.6 or later, PA layer |
| 742 | * parameters tuning happens during link startup itself. |
| 743 | * |
| 744 | * We can manually tune PA layer parameters if either host or device |
| 745 | * doesn't support UniPro ver 1.6 or later. But to keep manual tuning |
| 746 | * logic simple, we will only do manual tuning if local unipro version |
| 747 | * doesn't support ver1.6 or later. |
| 748 | */ |
| 749 | if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6) |
| 750 | return true; |
| 751 | else |
| 752 | return false; |
| 753 | } |
| 754 | |
Gilad Broner | a508253 | 2016-10-17 17:10:00 -0700 | [diff] [blame] | 755 | static void ufshcd_suspend_clkscaling(struct ufs_hba *hba) |
| 756 | { |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 757 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 758 | return; |
| 759 | |
| 760 | devfreq_suspend_device(hba->devfreq); |
| 761 | hba->clk_scaling.window_start_t = 0; |
Gilad Broner | a508253 | 2016-10-17 17:10:00 -0700 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | static void ufshcd_resume_clkscaling(struct ufs_hba *hba) |
| 765 | { |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 766 | devfreq_resume_device(hba->devfreq); |
| 767 | } |
| 768 | |
| 769 | static ssize_t ufshcd_clkscale_enable_show(struct device *dev, |
| 770 | struct device_attribute *attr, char *buf) |
| 771 | { |
| 772 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 773 | |
| 774 | return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_scaling.is_allowed); |
| 775 | } |
| 776 | |
| 777 | static ssize_t ufshcd_clkscale_enable_store(struct device *dev, |
| 778 | struct device_attribute *attr, const char *buf, size_t count) |
| 779 | { |
| 780 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 781 | u32 value; |
| 782 | int err; |
| 783 | |
| 784 | if (kstrtou32(buf, 0, &value)) |
| 785 | return -EINVAL; |
| 786 | |
| 787 | value = !!value; |
| 788 | if (value == hba->clk_scaling.is_allowed) |
| 789 | goto out; |
| 790 | |
| 791 | pm_runtime_get_sync(hba->dev); |
| 792 | ufshcd_hold(hba, false); |
| 793 | |
| 794 | if (value) { |
| 795 | ufshcd_resume_clkscaling(hba); |
| 796 | } else { |
| 797 | ufshcd_suspend_clkscaling(hba); |
| 798 | err = ufshcd_scale_clks(hba, true); |
| 799 | if (err) |
| 800 | dev_err(hba->dev, "%s: failed to scale clocks up %d\n", |
| 801 | __func__, err); |
| 802 | } |
| 803 | hba->clk_scaling.is_allowed = value; |
| 804 | |
| 805 | ufshcd_release(hba); |
| 806 | pm_runtime_put_sync(hba->dev); |
| 807 | out: |
| 808 | return count; |
Gilad Broner | a508253 | 2016-10-17 17:10:00 -0700 | [diff] [blame] | 809 | } |
| 810 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 811 | static void ufshcd_ungate_work(struct work_struct *work) |
| 812 | { |
| 813 | int ret; |
| 814 | unsigned long flags; |
| 815 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 816 | clk_gating.ungate_work); |
| 817 | |
| 818 | cancel_delayed_work_sync(&hba->clk_gating.gate_work); |
| 819 | |
| 820 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 821 | if (hba->clk_gating.state == CLKS_ON) { |
| 822 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 823 | goto unblock_reqs; |
| 824 | } |
| 825 | |
| 826 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 827 | ufshcd_setup_clocks(hba, true); |
| 828 | |
| 829 | /* Exit from hibern8 */ |
| 830 | if (ufshcd_can_hibern8_during_gating(hba)) { |
| 831 | /* Prevent gating in this path */ |
| 832 | hba->clk_gating.is_suspended = true; |
| 833 | if (ufshcd_is_link_hibern8(hba)) { |
| 834 | ret = ufshcd_uic_hibern8_exit(hba); |
| 835 | if (ret) |
| 836 | dev_err(hba->dev, "%s: hibern8 exit failed %d\n", |
| 837 | __func__, ret); |
| 838 | else |
| 839 | ufshcd_set_link_active(hba); |
| 840 | } |
| 841 | hba->clk_gating.is_suspended = false; |
| 842 | } |
| 843 | unblock_reqs: |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 844 | if (hba->clk_scaling.is_allowed) |
| 845 | ufshcd_resume_clkscaling(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 846 | scsi_unblock_requests(hba->host); |
| 847 | } |
| 848 | |
| 849 | /** |
| 850 | * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release. |
| 851 | * Also, exit from hibern8 mode and set the link as active. |
| 852 | * @hba: per adapter instance |
| 853 | * @async: This indicates whether caller should ungate clocks asynchronously. |
| 854 | */ |
| 855 | int ufshcd_hold(struct ufs_hba *hba, bool async) |
| 856 | { |
| 857 | int rc = 0; |
| 858 | unsigned long flags; |
| 859 | |
| 860 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 861 | goto out; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 862 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 863 | hba->clk_gating.active_reqs++; |
| 864 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 865 | if (ufshcd_eh_in_progress(hba)) { |
| 866 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 867 | return 0; |
| 868 | } |
| 869 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 870 | start: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 871 | switch (hba->clk_gating.state) { |
| 872 | case CLKS_ON: |
Venkat Gopalakrishnan | f2a785a | 2016-10-17 17:10:53 -0700 | [diff] [blame] | 873 | /* |
| 874 | * Wait for the ungate work to complete if in progress. |
| 875 | * Though the clocks may be in ON state, the link could |
| 876 | * still be in hibner8 state if hibern8 is allowed |
| 877 | * during clock gating. |
| 878 | * Make sure we exit hibern8 state also in addition to |
| 879 | * clocks being ON. |
| 880 | */ |
| 881 | if (ufshcd_can_hibern8_during_gating(hba) && |
| 882 | ufshcd_is_link_hibern8(hba)) { |
| 883 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 884 | flush_work(&hba->clk_gating.ungate_work); |
| 885 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 886 | goto start; |
| 887 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 888 | break; |
| 889 | case REQ_CLKS_OFF: |
| 890 | if (cancel_delayed_work(&hba->clk_gating.gate_work)) { |
| 891 | hba->clk_gating.state = CLKS_ON; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 892 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 893 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 894 | break; |
| 895 | } |
| 896 | /* |
| 897 | * If we here, it means gating work is either done or |
| 898 | * currently running. Hence, fall through to cancel gating |
| 899 | * work and to enable clocks. |
| 900 | */ |
| 901 | case CLKS_OFF: |
| 902 | scsi_block_requests(hba->host); |
| 903 | hba->clk_gating.state = REQ_CLKS_ON; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 904 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 905 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 906 | schedule_work(&hba->clk_gating.ungate_work); |
| 907 | /* |
| 908 | * fall through to check if we should wait for this |
| 909 | * work to be done or not. |
| 910 | */ |
| 911 | case REQ_CLKS_ON: |
| 912 | if (async) { |
| 913 | rc = -EAGAIN; |
| 914 | hba->clk_gating.active_reqs--; |
| 915 | break; |
| 916 | } |
| 917 | |
| 918 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 919 | flush_work(&hba->clk_gating.ungate_work); |
| 920 | /* Make sure state is CLKS_ON before returning */ |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 921 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 922 | goto start; |
| 923 | default: |
| 924 | dev_err(hba->dev, "%s: clk gating is in invalid state %d\n", |
| 925 | __func__, hba->clk_gating.state); |
| 926 | break; |
| 927 | } |
| 928 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 929 | out: |
| 930 | return rc; |
| 931 | } |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 932 | EXPORT_SYMBOL_GPL(ufshcd_hold); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 933 | |
| 934 | static void ufshcd_gate_work(struct work_struct *work) |
| 935 | { |
| 936 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 937 | clk_gating.gate_work.work); |
| 938 | unsigned long flags; |
| 939 | |
| 940 | spin_lock_irqsave(hba->host->host_lock, flags); |
Venkat Gopalakrishnan | 3f0c06d | 2016-10-17 17:11:07 -0700 | [diff] [blame] | 941 | /* |
| 942 | * In case you are here to cancel this work the gating state |
| 943 | * would be marked as REQ_CLKS_ON. In this case save time by |
| 944 | * skipping the gating work and exit after changing the clock |
| 945 | * state to CLKS_ON. |
| 946 | */ |
| 947 | if (hba->clk_gating.is_suspended || |
| 948 | (hba->clk_gating.state == REQ_CLKS_ON)) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 949 | hba->clk_gating.state = CLKS_ON; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 950 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 951 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 952 | goto rel_lock; |
| 953 | } |
| 954 | |
| 955 | if (hba->clk_gating.active_reqs |
| 956 | || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL |
| 957 | || hba->lrb_in_use || hba->outstanding_tasks |
| 958 | || hba->active_uic_cmd || hba->uic_async_done) |
| 959 | goto rel_lock; |
| 960 | |
| 961 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 962 | |
| 963 | /* put the link into hibern8 mode before turning off clocks */ |
| 964 | if (ufshcd_can_hibern8_during_gating(hba)) { |
| 965 | if (ufshcd_uic_hibern8_enter(hba)) { |
| 966 | hba->clk_gating.state = CLKS_ON; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 967 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 968 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 969 | goto out; |
| 970 | } |
| 971 | ufshcd_set_link_hibern8(hba); |
| 972 | } |
| 973 | |
Gilad Broner | a508253 | 2016-10-17 17:10:00 -0700 | [diff] [blame] | 974 | ufshcd_suspend_clkscaling(hba); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 975 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 976 | if (!ufshcd_is_link_active(hba)) |
| 977 | ufshcd_setup_clocks(hba, false); |
| 978 | else |
| 979 | /* If link is active, device ref_clk can't be switched off */ |
| 980 | __ufshcd_setup_clocks(hba, false, true); |
| 981 | |
| 982 | /* |
| 983 | * In case you are here to cancel this work the gating state |
| 984 | * would be marked as REQ_CLKS_ON. In this case keep the state |
| 985 | * as REQ_CLKS_ON which would anyway imply that clocks are off |
| 986 | * and a request to turn them on is pending. By doing this way, |
| 987 | * we keep the state machine in tact and this would ultimately |
| 988 | * prevent from doing cancel work multiple times when there are |
| 989 | * new requests arriving before the current cancel work is done. |
| 990 | */ |
| 991 | spin_lock_irqsave(hba->host->host_lock, flags); |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 992 | if (hba->clk_gating.state == REQ_CLKS_OFF) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 993 | hba->clk_gating.state = CLKS_OFF; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 994 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 995 | hba->clk_gating.state); |
| 996 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 997 | rel_lock: |
| 998 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 999 | out: |
| 1000 | return; |
| 1001 | } |
| 1002 | |
| 1003 | /* host lock must be held before calling this variant */ |
| 1004 | static void __ufshcd_release(struct ufs_hba *hba) |
| 1005 | { |
| 1006 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1007 | return; |
| 1008 | |
| 1009 | hba->clk_gating.active_reqs--; |
| 1010 | |
| 1011 | if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended |
| 1012 | || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL |
| 1013 | || hba->lrb_in_use || hba->outstanding_tasks |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 1014 | || hba->active_uic_cmd || hba->uic_async_done |
| 1015 | || ufshcd_eh_in_progress(hba)) |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1016 | return; |
| 1017 | |
| 1018 | hba->clk_gating.state = REQ_CLKS_OFF; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1019 | trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1020 | schedule_delayed_work(&hba->clk_gating.gate_work, |
| 1021 | msecs_to_jiffies(hba->clk_gating.delay_ms)); |
| 1022 | } |
| 1023 | |
| 1024 | void ufshcd_release(struct ufs_hba *hba) |
| 1025 | { |
| 1026 | unsigned long flags; |
| 1027 | |
| 1028 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1029 | __ufshcd_release(hba); |
| 1030 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1031 | } |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 1032 | EXPORT_SYMBOL_GPL(ufshcd_release); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1033 | |
| 1034 | static ssize_t ufshcd_clkgate_delay_show(struct device *dev, |
| 1035 | struct device_attribute *attr, char *buf) |
| 1036 | { |
| 1037 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1038 | |
| 1039 | return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms); |
| 1040 | } |
| 1041 | |
| 1042 | static ssize_t ufshcd_clkgate_delay_store(struct device *dev, |
| 1043 | struct device_attribute *attr, const char *buf, size_t count) |
| 1044 | { |
| 1045 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1046 | unsigned long flags, value; |
| 1047 | |
| 1048 | if (kstrtoul(buf, 0, &value)) |
| 1049 | return -EINVAL; |
| 1050 | |
| 1051 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1052 | hba->clk_gating.delay_ms = value; |
| 1053 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1054 | return count; |
| 1055 | } |
| 1056 | |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1057 | static ssize_t ufshcd_clkgate_enable_show(struct device *dev, |
| 1058 | struct device_attribute *attr, char *buf) |
| 1059 | { |
| 1060 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1061 | |
| 1062 | return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_gating.is_enabled); |
| 1063 | } |
| 1064 | |
| 1065 | static ssize_t ufshcd_clkgate_enable_store(struct device *dev, |
| 1066 | struct device_attribute *attr, const char *buf, size_t count) |
| 1067 | { |
| 1068 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1069 | unsigned long flags; |
| 1070 | u32 value; |
| 1071 | |
| 1072 | if (kstrtou32(buf, 0, &value)) |
| 1073 | return -EINVAL; |
| 1074 | |
| 1075 | value = !!value; |
| 1076 | if (value == hba->clk_gating.is_enabled) |
| 1077 | goto out; |
| 1078 | |
| 1079 | if (value) { |
| 1080 | ufshcd_release(hba); |
| 1081 | } else { |
| 1082 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1083 | hba->clk_gating.active_reqs++; |
| 1084 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1085 | } |
| 1086 | |
| 1087 | hba->clk_gating.is_enabled = value; |
| 1088 | out: |
| 1089 | return count; |
| 1090 | } |
| 1091 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1092 | static void ufshcd_init_clk_gating(struct ufs_hba *hba) |
| 1093 | { |
| 1094 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1095 | return; |
| 1096 | |
| 1097 | hba->clk_gating.delay_ms = 150; |
| 1098 | INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work); |
| 1099 | INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work); |
| 1100 | |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1101 | hba->clk_gating.is_enabled = true; |
| 1102 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1103 | hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show; |
| 1104 | hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store; |
| 1105 | sysfs_attr_init(&hba->clk_gating.delay_attr.attr); |
| 1106 | hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms"; |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1107 | hba->clk_gating.delay_attr.attr.mode = 0644; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1108 | if (device_create_file(hba->dev, &hba->clk_gating.delay_attr)) |
| 1109 | dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n"); |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1110 | |
| 1111 | hba->clk_gating.enable_attr.show = ufshcd_clkgate_enable_show; |
| 1112 | hba->clk_gating.enable_attr.store = ufshcd_clkgate_enable_store; |
| 1113 | sysfs_attr_init(&hba->clk_gating.enable_attr.attr); |
| 1114 | hba->clk_gating.enable_attr.attr.name = "clkgate_enable"; |
| 1115 | hba->clk_gating.enable_attr.attr.mode = 0644; |
| 1116 | if (device_create_file(hba->dev, &hba->clk_gating.enable_attr)) |
| 1117 | dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n"); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | static void ufshcd_exit_clk_gating(struct ufs_hba *hba) |
| 1121 | { |
| 1122 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1123 | return; |
| 1124 | device_remove_file(hba->dev, &hba->clk_gating.delay_attr); |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1125 | device_remove_file(hba->dev, &hba->clk_gating.enable_attr); |
Akinobu Mita | 97cd680 | 2014-11-24 14:24:18 +0900 | [diff] [blame] | 1126 | cancel_work_sync(&hba->clk_gating.ungate_work); |
| 1127 | cancel_delayed_work_sync(&hba->clk_gating.gate_work); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1128 | } |
| 1129 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1130 | /* Must be called with host lock acquired */ |
| 1131 | static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba) |
| 1132 | { |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1133 | if (!ufshcd_is_clkscaling_supported(hba)) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1134 | return; |
| 1135 | |
| 1136 | if (!hba->clk_scaling.is_busy_started) { |
| 1137 | hba->clk_scaling.busy_start_t = ktime_get(); |
| 1138 | hba->clk_scaling.is_busy_started = true; |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba) |
| 1143 | { |
| 1144 | struct ufs_clk_scaling *scaling = &hba->clk_scaling; |
| 1145 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1146 | if (!ufshcd_is_clkscaling_supported(hba)) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1147 | return; |
| 1148 | |
| 1149 | if (!hba->outstanding_reqs && scaling->is_busy_started) { |
| 1150 | scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(), |
| 1151 | scaling->busy_start_t)); |
Thomas Gleixner | 8b0e195 | 2016-12-25 12:30:41 +0100 | [diff] [blame] | 1152 | scaling->busy_start_t = 0; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1153 | scaling->is_busy_started = false; |
| 1154 | } |
| 1155 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1156 | /** |
| 1157 | * ufshcd_send_command - Send SCSI or device management commands |
| 1158 | * @hba: per adapter instance |
| 1159 | * @task_tag: Task tag of the command |
| 1160 | */ |
| 1161 | static inline |
| 1162 | void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag) |
| 1163 | { |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1164 | ufshcd_clk_scaling_start_busy(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1165 | __set_bit(task_tag, &hba->outstanding_reqs); |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 1166 | ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
Gilad Broner | ad1a1b9 | 2016-10-17 17:09:36 -0700 | [diff] [blame] | 1167 | /* Make sure that doorbell is committed immediately */ |
| 1168 | wmb(); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | /** |
| 1172 | * ufshcd_copy_sense_data - Copy sense data in case of check condition |
| 1173 | * @lrb - pointer to local reference block |
| 1174 | */ |
| 1175 | static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp) |
| 1176 | { |
| 1177 | int len; |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 1178 | if (lrbp->sense_buffer && |
| 1179 | ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) { |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 1180 | int len_to_copy; |
| 1181 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1182 | len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len); |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 1183 | len_to_copy = min_t(int, RESPONSE_UPIU_SENSE_DATA_LENGTH, len); |
| 1184 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1185 | memcpy(lrbp->sense_buffer, |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1186 | lrbp->ucd_rsp_ptr->sr.sense_data, |
Gilad Broner | dcea0bf | 2016-10-17 17:09:48 -0700 | [diff] [blame] | 1187 | min_t(int, len_to_copy, UFSHCD_REQ_SENSE_SIZE)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1192 | * ufshcd_copy_query_response() - Copy the Query Response and the data |
| 1193 | * descriptor |
| 1194 | * @hba: per adapter instance |
| 1195 | * @lrb - pointer to local reference block |
| 1196 | */ |
| 1197 | static |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1198 | 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] | 1199 | { |
| 1200 | struct ufs_query_res *query_res = &hba->dev_cmd.query.response; |
| 1201 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1202 | 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] | 1203 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1204 | /* Get the descriptor */ |
| 1205 | if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 1206 | u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1207 | GENERAL_UPIU_REQUEST_SIZE; |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1208 | u16 resp_len; |
| 1209 | u16 buf_len; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1210 | |
| 1211 | /* data segment length */ |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1212 | resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) & |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1213 | MASK_QUERY_DATA_SEG_LEN; |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 1214 | buf_len = be16_to_cpu( |
| 1215 | hba->dev_cmd.query.request.upiu_req.length); |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1216 | if (likely(buf_len >= resp_len)) { |
| 1217 | memcpy(hba->dev_cmd.query.descriptor, descp, resp_len); |
| 1218 | } else { |
| 1219 | dev_warn(hba->dev, |
| 1220 | "%s: Response size is bigger than buffer", |
| 1221 | __func__); |
| 1222 | return -EINVAL; |
| 1223 | } |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1224 | } |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1225 | |
| 1226 | return 0; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1230 | * ufshcd_hba_capabilities - Read controller capabilities |
| 1231 | * @hba: per adapter instance |
| 1232 | */ |
| 1233 | static inline void ufshcd_hba_capabilities(struct ufs_hba *hba) |
| 1234 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 1235 | hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1236 | |
| 1237 | /* nutrs and nutmrs are 0 based values */ |
| 1238 | hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1; |
| 1239 | hba->nutmrs = |
| 1240 | ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1; |
| 1241 | } |
| 1242 | |
| 1243 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1244 | * ufshcd_ready_for_uic_cmd - Check if controller is ready |
| 1245 | * to accept UIC commands |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1246 | * @hba: per adapter instance |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1247 | * Return true on success, else false |
| 1248 | */ |
| 1249 | static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba) |
| 1250 | { |
| 1251 | if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY) |
| 1252 | return true; |
| 1253 | else |
| 1254 | return false; |
| 1255 | } |
| 1256 | |
| 1257 | /** |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 1258 | * ufshcd_get_upmcrs - Get the power mode change request status |
| 1259 | * @hba: Pointer to adapter instance |
| 1260 | * |
| 1261 | * This function gets the UPMCRS field of HCS register |
| 1262 | * Returns value of UPMCRS field |
| 1263 | */ |
| 1264 | static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba) |
| 1265 | { |
| 1266 | return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7; |
| 1267 | } |
| 1268 | |
| 1269 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1270 | * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers |
| 1271 | * @hba: per adapter instance |
| 1272 | * @uic_cmd: UIC command |
| 1273 | * |
| 1274 | * Mutex must be held. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1275 | */ |
| 1276 | static inline void |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1277 | 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] | 1278 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1279 | WARN_ON(hba->active_uic_cmd); |
| 1280 | |
| 1281 | hba->active_uic_cmd = uic_cmd; |
| 1282 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1283 | /* Write Args */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1284 | ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1); |
| 1285 | ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2); |
| 1286 | ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1287 | |
| 1288 | /* Write UIC Cmd */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1289 | ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK, |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 1290 | REG_UIC_COMMAND); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1294 | * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command |
| 1295 | * @hba: per adapter instance |
| 1296 | * @uic_command: UIC command |
| 1297 | * |
| 1298 | * Must be called with mutex held. |
| 1299 | * Returns 0 only if success. |
| 1300 | */ |
| 1301 | static int |
| 1302 | ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) |
| 1303 | { |
| 1304 | int ret; |
| 1305 | unsigned long flags; |
| 1306 | |
| 1307 | if (wait_for_completion_timeout(&uic_cmd->done, |
| 1308 | msecs_to_jiffies(UIC_CMD_TIMEOUT))) |
| 1309 | ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT; |
| 1310 | else |
| 1311 | ret = -ETIMEDOUT; |
| 1312 | |
| 1313 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1314 | hba->active_uic_cmd = NULL; |
| 1315 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1316 | |
| 1317 | return ret; |
| 1318 | } |
| 1319 | |
| 1320 | /** |
| 1321 | * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result |
| 1322 | * @hba: per adapter instance |
| 1323 | * @uic_cmd: UIC command |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 1324 | * @completion: initialize the completion only if this is set to true |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1325 | * |
| 1326 | * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 1327 | * with mutex held and host_lock locked. |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1328 | * Returns 0 only if success. |
| 1329 | */ |
| 1330 | static int |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 1331 | __ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd, |
| 1332 | bool completion) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1333 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1334 | if (!ufshcd_ready_for_uic_cmd(hba)) { |
| 1335 | dev_err(hba->dev, |
| 1336 | "Controller not ready to accept UIC commands\n"); |
| 1337 | return -EIO; |
| 1338 | } |
| 1339 | |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 1340 | if (completion) |
| 1341 | init_completion(&uic_cmd->done); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1342 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1343 | ufshcd_dispatch_uic_cmd(hba, uic_cmd); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1344 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 1345 | return 0; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | /** |
| 1349 | * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result |
| 1350 | * @hba: per adapter instance |
| 1351 | * @uic_cmd: UIC command |
| 1352 | * |
| 1353 | * Returns 0 only if success. |
| 1354 | */ |
| 1355 | static int |
| 1356 | ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) |
| 1357 | { |
| 1358 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 1359 | unsigned long flags; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1360 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1361 | ufshcd_hold(hba, false); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1362 | mutex_lock(&hba->uic_cmd_mutex); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 1363 | ufshcd_add_delay_before_dme_cmd(hba); |
| 1364 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 1365 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 1366 | ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 1367 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1368 | if (!ret) |
| 1369 | ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd); |
| 1370 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1371 | mutex_unlock(&hba->uic_cmd_mutex); |
| 1372 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1373 | ufshcd_release(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1374 | return ret; |
| 1375 | } |
| 1376 | |
| 1377 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1378 | * ufshcd_map_sg - Map scatter-gather list to prdt |
| 1379 | * @lrbp - pointer to local reference block |
| 1380 | * |
| 1381 | * Returns 0 in case of success, non-zero value in case of failure |
| 1382 | */ |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 1383 | static int ufshcd_map_sg(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1384 | { |
| 1385 | struct ufshcd_sg_entry *prd_table; |
| 1386 | struct scatterlist *sg; |
| 1387 | struct scsi_cmnd *cmd; |
| 1388 | int sg_segments; |
| 1389 | int i; |
| 1390 | |
| 1391 | cmd = lrbp->cmd; |
| 1392 | sg_segments = scsi_dma_map(cmd); |
| 1393 | if (sg_segments < 0) |
| 1394 | return sg_segments; |
| 1395 | |
| 1396 | if (sg_segments) { |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 1397 | if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) |
| 1398 | lrbp->utr_descriptor_ptr->prd_table_length = |
| 1399 | cpu_to_le16((u16)(sg_segments * |
| 1400 | sizeof(struct ufshcd_sg_entry))); |
| 1401 | else |
| 1402 | lrbp->utr_descriptor_ptr->prd_table_length = |
| 1403 | cpu_to_le16((u16) (sg_segments)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1404 | |
| 1405 | prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr; |
| 1406 | |
| 1407 | scsi_for_each_sg(cmd, sg, sg_segments, i) { |
| 1408 | prd_table[i].size = |
| 1409 | cpu_to_le32(((u32) sg_dma_len(sg))-1); |
| 1410 | prd_table[i].base_addr = |
| 1411 | cpu_to_le32(lower_32_bits(sg->dma_address)); |
| 1412 | prd_table[i].upper_addr = |
| 1413 | cpu_to_le32(upper_32_bits(sg->dma_address)); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 1414 | prd_table[i].reserved = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1415 | } |
| 1416 | } else { |
| 1417 | lrbp->utr_descriptor_ptr->prd_table_length = 0; |
| 1418 | } |
| 1419 | |
| 1420 | return 0; |
| 1421 | } |
| 1422 | |
| 1423 | /** |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 1424 | * ufshcd_enable_intr - enable interrupts |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1425 | * @hba: per adapter instance |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 1426 | * @intrs: interrupt bits |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1427 | */ |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 1428 | static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1429 | { |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 1430 | u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
| 1431 | |
| 1432 | if (hba->ufs_version == UFSHCI_VERSION_10) { |
| 1433 | u32 rw; |
| 1434 | rw = set & INTERRUPT_MASK_RW_VER_10; |
| 1435 | set = rw | ((set ^ intrs) & intrs); |
| 1436 | } else { |
| 1437 | set |= intrs; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1438 | } |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 1439 | |
| 1440 | ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE); |
| 1441 | } |
| 1442 | |
| 1443 | /** |
| 1444 | * ufshcd_disable_intr - disable interrupts |
| 1445 | * @hba: per adapter instance |
| 1446 | * @intrs: interrupt bits |
| 1447 | */ |
| 1448 | static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs) |
| 1449 | { |
| 1450 | u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
| 1451 | |
| 1452 | if (hba->ufs_version == UFSHCI_VERSION_10) { |
| 1453 | u32 rw; |
| 1454 | rw = (set & INTERRUPT_MASK_RW_VER_10) & |
| 1455 | ~(intrs & INTERRUPT_MASK_RW_VER_10); |
| 1456 | set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10); |
| 1457 | |
| 1458 | } else { |
| 1459 | set &= ~intrs; |
| 1460 | } |
| 1461 | |
| 1462 | ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1466 | * ufshcd_prepare_req_desc_hdr() - Fills the requests header |
| 1467 | * descriptor according to request |
| 1468 | * @lrbp: pointer to local reference block |
| 1469 | * @upiu_flags: flags required in the header |
| 1470 | * @cmd_dir: requests data direction |
| 1471 | */ |
| 1472 | static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp, |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 1473 | u32 *upiu_flags, enum dma_data_direction cmd_dir) |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1474 | { |
| 1475 | struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr; |
| 1476 | u32 data_direction; |
| 1477 | u32 dword_0; |
| 1478 | |
| 1479 | if (cmd_dir == DMA_FROM_DEVICE) { |
| 1480 | data_direction = UTP_DEVICE_TO_HOST; |
| 1481 | *upiu_flags = UPIU_CMD_FLAGS_READ; |
| 1482 | } else if (cmd_dir == DMA_TO_DEVICE) { |
| 1483 | data_direction = UTP_HOST_TO_DEVICE; |
| 1484 | *upiu_flags = UPIU_CMD_FLAGS_WRITE; |
| 1485 | } else { |
| 1486 | data_direction = UTP_NO_DATA_TRANSFER; |
| 1487 | *upiu_flags = UPIU_CMD_FLAGS_NONE; |
| 1488 | } |
| 1489 | |
| 1490 | dword_0 = data_direction | (lrbp->command_type |
| 1491 | << UPIU_COMMAND_TYPE_OFFSET); |
| 1492 | if (lrbp->intr_cmd) |
| 1493 | dword_0 |= UTP_REQ_DESC_INT_CMD; |
| 1494 | |
| 1495 | /* Transfer request descriptor header fields */ |
| 1496 | req_desc->header.dword_0 = cpu_to_le32(dword_0); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 1497 | /* dword_1 is reserved, hence it is set to 0 */ |
| 1498 | req_desc->header.dword_1 = 0; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1499 | /* |
| 1500 | * assigning invalid value for command status. Controller |
| 1501 | * updates OCS on command completion, with the command |
| 1502 | * status |
| 1503 | */ |
| 1504 | req_desc->header.dword_2 = |
| 1505 | cpu_to_le32(OCS_INVALID_COMMAND_STATUS); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 1506 | /* dword_3 is reserved, hence it is set to 0 */ |
| 1507 | req_desc->header.dword_3 = 0; |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 1508 | |
| 1509 | req_desc->prd_table_length = 0; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | /** |
| 1513 | * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc, |
| 1514 | * for scsi commands |
| 1515 | * @lrbp - local reference block pointer |
| 1516 | * @upiu_flags - flags |
| 1517 | */ |
| 1518 | static |
| 1519 | void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags) |
| 1520 | { |
| 1521 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 1522 | unsigned short cdb_len; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1523 | |
| 1524 | /* command descriptor fields */ |
| 1525 | ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD( |
| 1526 | UPIU_TRANSACTION_COMMAND, upiu_flags, |
| 1527 | lrbp->lun, lrbp->task_tag); |
| 1528 | ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD( |
| 1529 | UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0); |
| 1530 | |
| 1531 | /* Total EHS length and Data segment length will be zero */ |
| 1532 | ucd_req_ptr->header.dword_2 = 0; |
| 1533 | |
| 1534 | ucd_req_ptr->sc.exp_data_transfer_len = |
| 1535 | cpu_to_be32(lrbp->cmd->sdb.length); |
| 1536 | |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 1537 | cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE); |
| 1538 | memset(ucd_req_ptr->sc.cdb, 0, MAX_CDB_SIZE); |
| 1539 | memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len); |
| 1540 | |
| 1541 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1542 | } |
| 1543 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1544 | /** |
| 1545 | * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc, |
| 1546 | * for query requsts |
| 1547 | * @hba: UFS hba |
| 1548 | * @lrbp: local reference block pointer |
| 1549 | * @upiu_flags: flags |
| 1550 | */ |
| 1551 | static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba, |
| 1552 | struct ufshcd_lrb *lrbp, u32 upiu_flags) |
| 1553 | { |
| 1554 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
| 1555 | struct ufs_query *query = &hba->dev_cmd.query; |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 1556 | u16 len = be16_to_cpu(query->request.upiu_req.length); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1557 | u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE; |
| 1558 | |
| 1559 | /* Query request header */ |
| 1560 | ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD( |
| 1561 | UPIU_TRANSACTION_QUERY_REQ, upiu_flags, |
| 1562 | lrbp->lun, lrbp->task_tag); |
| 1563 | ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD( |
| 1564 | 0, query->request.query_func, 0, 0); |
| 1565 | |
Zang Leigang | 6861285 | 2016-08-25 17:39:19 +0800 | [diff] [blame] | 1566 | /* Data segment length only need for WRITE_DESC */ |
| 1567 | if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) |
| 1568 | ucd_req_ptr->header.dword_2 = |
| 1569 | UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len); |
| 1570 | else |
| 1571 | ucd_req_ptr->header.dword_2 = 0; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1572 | |
| 1573 | /* Copy the Query Request buffer as is */ |
| 1574 | memcpy(&ucd_req_ptr->qr, &query->request.upiu_req, |
| 1575 | QUERY_OSF_SIZE); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1576 | |
| 1577 | /* Copy the Descriptor */ |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1578 | if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) |
| 1579 | memcpy(descp, query->descriptor, len); |
| 1580 | |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 1581 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1582 | } |
| 1583 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1584 | static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp) |
| 1585 | { |
| 1586 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
| 1587 | |
| 1588 | memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req)); |
| 1589 | |
| 1590 | /* command descriptor fields */ |
| 1591 | ucd_req_ptr->header.dword_0 = |
| 1592 | UPIU_HEADER_DWORD( |
| 1593 | UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag); |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 1594 | /* clear rest of the fields of basic header */ |
| 1595 | ucd_req_ptr->header.dword_1 = 0; |
| 1596 | ucd_req_ptr->header.dword_2 = 0; |
| 1597 | |
| 1598 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | /** |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 1602 | * ufshcd_comp_devman_upiu - UFS Protocol Information Unit(UPIU) |
| 1603 | * for Device Management Purposes |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1604 | * @hba - per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1605 | * @lrb - pointer to local reference block |
| 1606 | */ |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 1607 | static int ufshcd_comp_devman_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1608 | { |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1609 | u32 upiu_flags; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1610 | int ret = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1611 | |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 1612 | if (hba->ufs_version == UFSHCI_VERSION_20) |
| 1613 | lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE; |
| 1614 | else |
| 1615 | lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE; |
| 1616 | |
| 1617 | ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE); |
| 1618 | if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY) |
| 1619 | ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags); |
| 1620 | else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP) |
| 1621 | ufshcd_prepare_utp_nop_upiu(lrbp); |
| 1622 | else |
| 1623 | ret = -EINVAL; |
| 1624 | |
| 1625 | return ret; |
| 1626 | } |
| 1627 | |
| 1628 | /** |
| 1629 | * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU) |
| 1630 | * for SCSI Purposes |
| 1631 | * @hba - per adapter instance |
| 1632 | * @lrb - pointer to local reference block |
| 1633 | */ |
| 1634 | static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 1635 | { |
| 1636 | u32 upiu_flags; |
| 1637 | int ret = 0; |
| 1638 | |
| 1639 | if (hba->ufs_version == UFSHCI_VERSION_20) |
| 1640 | lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE; |
| 1641 | else |
| 1642 | lrbp->command_type = UTP_CMD_TYPE_SCSI; |
| 1643 | |
| 1644 | if (likely(lrbp->cmd)) { |
| 1645 | ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, |
| 1646 | lrbp->cmd->sc_data_direction); |
| 1647 | ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags); |
| 1648 | } else { |
| 1649 | ret = -EINVAL; |
| 1650 | } |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1651 | |
| 1652 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1653 | } |
| 1654 | |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 1655 | /* |
| 1656 | * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN |
| 1657 | * @scsi_lun: scsi LUN id |
| 1658 | * |
| 1659 | * Returns UPIU LUN id |
| 1660 | */ |
| 1661 | static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun) |
| 1662 | { |
| 1663 | if (scsi_is_wlun(scsi_lun)) |
| 1664 | return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID) |
| 1665 | | UFS_UPIU_WLUN_ID; |
| 1666 | else |
| 1667 | return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID; |
| 1668 | } |
| 1669 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1670 | /** |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 1671 | * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID |
| 1672 | * @scsi_lun: UPIU W-LUN id |
| 1673 | * |
| 1674 | * Returns SCSI W-LUN id |
| 1675 | */ |
| 1676 | static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id) |
| 1677 | { |
| 1678 | return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE; |
| 1679 | } |
| 1680 | |
| 1681 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1682 | * ufshcd_queuecommand - main entry point for SCSI requests |
| 1683 | * @cmd: command from SCSI Midlayer |
| 1684 | * @done: call back function |
| 1685 | * |
| 1686 | * Returns 0 for success, non-zero in case of failure |
| 1687 | */ |
| 1688 | static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) |
| 1689 | { |
| 1690 | struct ufshcd_lrb *lrbp; |
| 1691 | struct ufs_hba *hba; |
| 1692 | unsigned long flags; |
| 1693 | int tag; |
| 1694 | int err = 0; |
| 1695 | |
| 1696 | hba = shost_priv(host); |
| 1697 | |
| 1698 | tag = cmd->request->tag; |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 1699 | if (!ufshcd_valid_tag(hba, tag)) { |
| 1700 | dev_err(hba->dev, |
| 1701 | "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p", |
| 1702 | __func__, tag, cmd, cmd->request); |
| 1703 | BUG(); |
| 1704 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1705 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 1706 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1707 | switch (hba->ufshcd_state) { |
| 1708 | case UFSHCD_STATE_OPERATIONAL: |
| 1709 | break; |
Zang Leigang | 141f816 | 2016-11-16 11:29:37 +0800 | [diff] [blame] | 1710 | case UFSHCD_STATE_EH_SCHEDULED: |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 1711 | case UFSHCD_STATE_RESET: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1712 | err = SCSI_MLQUEUE_HOST_BUSY; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 1713 | goto out_unlock; |
| 1714 | case UFSHCD_STATE_ERROR: |
| 1715 | set_host_byte(cmd, DID_ERROR); |
| 1716 | cmd->scsi_done(cmd); |
| 1717 | goto out_unlock; |
| 1718 | default: |
| 1719 | dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n", |
| 1720 | __func__, hba->ufshcd_state); |
| 1721 | set_host_byte(cmd, DID_BAD_TARGET); |
| 1722 | cmd->scsi_done(cmd); |
| 1723 | goto out_unlock; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1724 | } |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 1725 | |
| 1726 | /* if error handling is in progress, don't issue commands */ |
| 1727 | if (ufshcd_eh_in_progress(hba)) { |
| 1728 | set_host_byte(cmd, DID_ERROR); |
| 1729 | cmd->scsi_done(cmd); |
| 1730 | goto out_unlock; |
| 1731 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 1732 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1733 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1734 | /* acquire the tag to make sure device cmds don't use it */ |
| 1735 | if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) { |
| 1736 | /* |
| 1737 | * Dev manage command in progress, requeue the command. |
| 1738 | * Requeuing the command helps in cases where the request *may* |
| 1739 | * find different tag instead of waiting for dev manage command |
| 1740 | * completion. |
| 1741 | */ |
| 1742 | err = SCSI_MLQUEUE_HOST_BUSY; |
| 1743 | goto out; |
| 1744 | } |
| 1745 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1746 | err = ufshcd_hold(hba, true); |
| 1747 | if (err) { |
| 1748 | err = SCSI_MLQUEUE_HOST_BUSY; |
| 1749 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 1750 | goto out; |
| 1751 | } |
| 1752 | WARN_ON(hba->clk_gating.state != CLKS_ON); |
| 1753 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1754 | lrbp = &hba->lrb[tag]; |
| 1755 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1756 | WARN_ON(lrbp->cmd); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1757 | lrbp->cmd = cmd; |
Gilad Broner | dcea0bf | 2016-10-17 17:09:48 -0700 | [diff] [blame] | 1758 | lrbp->sense_bufflen = UFSHCD_REQ_SENSE_SIZE; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1759 | lrbp->sense_buffer = cmd->sense_buffer; |
| 1760 | lrbp->task_tag = tag; |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 1761 | lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun); |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 1762 | lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1763 | |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 1764 | ufshcd_comp_scsi_upiu(hba, lrbp); |
| 1765 | |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 1766 | err = ufshcd_map_sg(hba, lrbp); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1767 | if (err) { |
| 1768 | lrbp->cmd = NULL; |
| 1769 | clear_bit_unlock(tag, &hba->lrb_in_use); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1770 | goto out; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1771 | } |
Gilad Broner | ad1a1b9 | 2016-10-17 17:09:36 -0700 | [diff] [blame] | 1772 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 1773 | wmb(); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1774 | |
| 1775 | /* issue command to the controller */ |
| 1776 | spin_lock_irqsave(hba->host->host_lock, flags); |
Kiwoong Kim | 0e675ef | 2016-11-10 21:14:36 +0900 | [diff] [blame] | 1777 | ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1778 | ufshcd_send_command(hba, tag); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 1779 | out_unlock: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1780 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1781 | out: |
| 1782 | return err; |
| 1783 | } |
| 1784 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1785 | static int ufshcd_compose_dev_cmd(struct ufs_hba *hba, |
| 1786 | struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag) |
| 1787 | { |
| 1788 | lrbp->cmd = NULL; |
| 1789 | lrbp->sense_bufflen = 0; |
| 1790 | lrbp->sense_buffer = NULL; |
| 1791 | lrbp->task_tag = tag; |
| 1792 | lrbp->lun = 0; /* device management cmd is not specific to any LUN */ |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1793 | lrbp->intr_cmd = true; /* No interrupt aggregation */ |
| 1794 | hba->dev_cmd.type = cmd_type; |
| 1795 | |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 1796 | return ufshcd_comp_devman_upiu(hba, lrbp); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1797 | } |
| 1798 | |
| 1799 | static int |
| 1800 | ufshcd_clear_cmd(struct ufs_hba *hba, int tag) |
| 1801 | { |
| 1802 | int err = 0; |
| 1803 | unsigned long flags; |
| 1804 | u32 mask = 1 << tag; |
| 1805 | |
| 1806 | /* clear outstanding transaction before retry */ |
| 1807 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1808 | ufshcd_utrl_clear(hba, tag); |
| 1809 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1810 | |
| 1811 | /* |
| 1812 | * wait for for h/w to clear corresponding bit in door-bell. |
| 1813 | * max. wait is 1 sec. |
| 1814 | */ |
| 1815 | err = ufshcd_wait_for_register(hba, |
| 1816 | REG_UTP_TRANSFER_REQ_DOOR_BELL, |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 1817 | mask, ~mask, 1000, 1000, true); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1818 | |
| 1819 | return err; |
| 1820 | } |
| 1821 | |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1822 | static int |
| 1823 | ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 1824 | { |
| 1825 | struct ufs_query_res *query_res = &hba->dev_cmd.query.response; |
| 1826 | |
| 1827 | /* Get the UPIU response */ |
| 1828 | query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >> |
| 1829 | UPIU_RSP_CODE_OFFSET; |
| 1830 | return query_res->response; |
| 1831 | } |
| 1832 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1833 | /** |
| 1834 | * ufshcd_dev_cmd_completion() - handles device management command responses |
| 1835 | * @hba: per adapter instance |
| 1836 | * @lrbp: pointer to local reference block |
| 1837 | */ |
| 1838 | static int |
| 1839 | ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 1840 | { |
| 1841 | int resp; |
| 1842 | int err = 0; |
| 1843 | |
| 1844 | resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr); |
| 1845 | |
| 1846 | switch (resp) { |
| 1847 | case UPIU_TRANSACTION_NOP_IN: |
| 1848 | if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) { |
| 1849 | err = -EINVAL; |
| 1850 | dev_err(hba->dev, "%s: unexpected response %x\n", |
| 1851 | __func__, resp); |
| 1852 | } |
| 1853 | break; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1854 | case UPIU_TRANSACTION_QUERY_RSP: |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1855 | err = ufshcd_check_query_response(hba, lrbp); |
| 1856 | if (!err) |
| 1857 | err = ufshcd_copy_query_response(hba, lrbp); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1858 | break; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1859 | case UPIU_TRANSACTION_REJECT_UPIU: |
| 1860 | /* TODO: handle Reject UPIU Response */ |
| 1861 | err = -EPERM; |
| 1862 | dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n", |
| 1863 | __func__); |
| 1864 | break; |
| 1865 | default: |
| 1866 | err = -EINVAL; |
| 1867 | dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n", |
| 1868 | __func__, resp); |
| 1869 | break; |
| 1870 | } |
| 1871 | |
| 1872 | return err; |
| 1873 | } |
| 1874 | |
| 1875 | static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba, |
| 1876 | struct ufshcd_lrb *lrbp, int max_timeout) |
| 1877 | { |
| 1878 | int err = 0; |
| 1879 | unsigned long time_left; |
| 1880 | unsigned long flags; |
| 1881 | |
| 1882 | time_left = wait_for_completion_timeout(hba->dev_cmd.complete, |
| 1883 | msecs_to_jiffies(max_timeout)); |
| 1884 | |
Gilad Broner | ad1a1b9 | 2016-10-17 17:09:36 -0700 | [diff] [blame] | 1885 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 1886 | wmb(); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1887 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1888 | hba->dev_cmd.complete = NULL; |
| 1889 | if (likely(time_left)) { |
| 1890 | err = ufshcd_get_tr_ocs(lrbp); |
| 1891 | if (!err) |
| 1892 | err = ufshcd_dev_cmd_completion(hba, lrbp); |
| 1893 | } |
| 1894 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1895 | |
| 1896 | if (!time_left) { |
| 1897 | err = -ETIMEDOUT; |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 1898 | dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n", |
| 1899 | __func__, lrbp->task_tag); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1900 | if (!ufshcd_clear_cmd(hba, lrbp->task_tag)) |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 1901 | /* successfully cleared the command, retry if needed */ |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1902 | err = -EAGAIN; |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 1903 | /* |
| 1904 | * in case of an error, after clearing the doorbell, |
| 1905 | * we also need to clear the outstanding_request |
| 1906 | * field in hba |
| 1907 | */ |
| 1908 | ufshcd_outstanding_req_clear(hba, lrbp->task_tag); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | return err; |
| 1912 | } |
| 1913 | |
| 1914 | /** |
| 1915 | * ufshcd_get_dev_cmd_tag - Get device management command tag |
| 1916 | * @hba: per-adapter instance |
| 1917 | * @tag: pointer to variable with available slot value |
| 1918 | * |
| 1919 | * Get a free slot and lock it until device management command |
| 1920 | * completes. |
| 1921 | * |
| 1922 | * Returns false if free slot is unavailable for locking, else |
| 1923 | * return true with tag value in @tag. |
| 1924 | */ |
| 1925 | static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out) |
| 1926 | { |
| 1927 | int tag; |
| 1928 | bool ret = false; |
| 1929 | unsigned long tmp; |
| 1930 | |
| 1931 | if (!tag_out) |
| 1932 | goto out; |
| 1933 | |
| 1934 | do { |
| 1935 | tmp = ~hba->lrb_in_use; |
| 1936 | tag = find_last_bit(&tmp, hba->nutrs); |
| 1937 | if (tag >= hba->nutrs) |
| 1938 | goto out; |
| 1939 | } while (test_and_set_bit_lock(tag, &hba->lrb_in_use)); |
| 1940 | |
| 1941 | *tag_out = tag; |
| 1942 | ret = true; |
| 1943 | out: |
| 1944 | return ret; |
| 1945 | } |
| 1946 | |
| 1947 | static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag) |
| 1948 | { |
| 1949 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 1950 | } |
| 1951 | |
| 1952 | /** |
| 1953 | * ufshcd_exec_dev_cmd - API for sending device management requests |
| 1954 | * @hba - UFS hba |
| 1955 | * @cmd_type - specifies the type (NOP, Query...) |
| 1956 | * @timeout - time in seconds |
| 1957 | * |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1958 | * NOTE: Since there is only one available tag for device management commands, |
| 1959 | * it is expected you hold the hba->dev_cmd.lock mutex. |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1960 | */ |
| 1961 | static int ufshcd_exec_dev_cmd(struct ufs_hba *hba, |
| 1962 | enum dev_cmd_type cmd_type, int timeout) |
| 1963 | { |
| 1964 | struct ufshcd_lrb *lrbp; |
| 1965 | int err; |
| 1966 | int tag; |
| 1967 | struct completion wait; |
| 1968 | unsigned long flags; |
| 1969 | |
| 1970 | /* |
| 1971 | * Get free slot, sleep if slots are unavailable. |
| 1972 | * Even though we use wait_event() which sleeps indefinitely, |
| 1973 | * the maximum wait time is bounded by SCSI request timeout. |
| 1974 | */ |
| 1975 | wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag)); |
| 1976 | |
| 1977 | init_completion(&wait); |
| 1978 | lrbp = &hba->lrb[tag]; |
| 1979 | WARN_ON(lrbp->cmd); |
| 1980 | err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag); |
| 1981 | if (unlikely(err)) |
| 1982 | goto out_put_tag; |
| 1983 | |
| 1984 | hba->dev_cmd.complete = &wait; |
| 1985 | |
Yaniv Gardi | e3dfdc5 | 2016-02-01 15:02:49 +0200 | [diff] [blame] | 1986 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 1987 | wmb(); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1988 | spin_lock_irqsave(hba->host->host_lock, flags); |
Kiwoong Kim | 0e675ef | 2016-11-10 21:14:36 +0900 | [diff] [blame] | 1989 | ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1990 | ufshcd_send_command(hba, tag); |
| 1991 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1992 | |
| 1993 | err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout); |
| 1994 | |
| 1995 | out_put_tag: |
| 1996 | ufshcd_put_dev_cmd_tag(hba, tag); |
| 1997 | wake_up(&hba->dev_cmd.tag_wq); |
| 1998 | return err; |
| 1999 | } |
| 2000 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2001 | /** |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2002 | * ufshcd_init_query() - init the query response and request parameters |
| 2003 | * @hba: per-adapter instance |
| 2004 | * @request: address of the request pointer to be initialized |
| 2005 | * @response: address of the response pointer to be initialized |
| 2006 | * @opcode: operation to perform |
| 2007 | * @idn: flag idn to access |
| 2008 | * @index: LU number to access |
| 2009 | * @selector: query/flag/descriptor further identification |
| 2010 | */ |
| 2011 | static inline void ufshcd_init_query(struct ufs_hba *hba, |
| 2012 | struct ufs_query_req **request, struct ufs_query_res **response, |
| 2013 | enum query_opcode opcode, u8 idn, u8 index, u8 selector) |
| 2014 | { |
| 2015 | *request = &hba->dev_cmd.query.request; |
| 2016 | *response = &hba->dev_cmd.query.response; |
| 2017 | memset(*request, 0, sizeof(struct ufs_query_req)); |
| 2018 | memset(*response, 0, sizeof(struct ufs_query_res)); |
| 2019 | (*request)->upiu_req.opcode = opcode; |
| 2020 | (*request)->upiu_req.idn = idn; |
| 2021 | (*request)->upiu_req.index = index; |
| 2022 | (*request)->upiu_req.selector = selector; |
| 2023 | } |
| 2024 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 2025 | static int ufshcd_query_flag_retry(struct ufs_hba *hba, |
| 2026 | enum query_opcode opcode, enum flag_idn idn, bool *flag_res) |
| 2027 | { |
| 2028 | int ret; |
| 2029 | int retries; |
| 2030 | |
| 2031 | for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) { |
| 2032 | ret = ufshcd_query_flag(hba, opcode, idn, flag_res); |
| 2033 | if (ret) |
| 2034 | dev_dbg(hba->dev, |
| 2035 | "%s: failed with error %d, retries %d\n", |
| 2036 | __func__, ret, retries); |
| 2037 | else |
| 2038 | break; |
| 2039 | } |
| 2040 | |
| 2041 | if (ret) |
| 2042 | dev_err(hba->dev, |
| 2043 | "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n", |
| 2044 | __func__, opcode, idn, ret, retries); |
| 2045 | return ret; |
| 2046 | } |
| 2047 | |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2048 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2049 | * ufshcd_query_flag() - API function for sending flag query requests |
| 2050 | * hba: per-adapter instance |
| 2051 | * query_opcode: flag query to perform |
| 2052 | * idn: flag idn to access |
| 2053 | * flag_res: the flag value after the query request completes |
| 2054 | * |
| 2055 | * Returns 0 for success, non-zero in case of failure |
| 2056 | */ |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 2057 | int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode, |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2058 | enum flag_idn idn, bool *flag_res) |
| 2059 | { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2060 | struct ufs_query_req *request = NULL; |
| 2061 | struct ufs_query_res *response = NULL; |
| 2062 | int err, index = 0, selector = 0; |
Yaniv Gardi | e5ad406 | 2016-02-01 15:02:41 +0200 | [diff] [blame] | 2063 | int timeout = QUERY_REQ_TIMEOUT; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2064 | |
| 2065 | BUG_ON(!hba); |
| 2066 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2067 | ufshcd_hold(hba, false); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2068 | mutex_lock(&hba->dev_cmd.lock); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2069 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 2070 | selector); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2071 | |
| 2072 | switch (opcode) { |
| 2073 | case UPIU_QUERY_OPCODE_SET_FLAG: |
| 2074 | case UPIU_QUERY_OPCODE_CLEAR_FLAG: |
| 2075 | case UPIU_QUERY_OPCODE_TOGGLE_FLAG: |
| 2076 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
| 2077 | break; |
| 2078 | case UPIU_QUERY_OPCODE_READ_FLAG: |
| 2079 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 2080 | if (!flag_res) { |
| 2081 | /* No dummy reads */ |
| 2082 | dev_err(hba->dev, "%s: Invalid argument for read request\n", |
| 2083 | __func__); |
| 2084 | err = -EINVAL; |
| 2085 | goto out_unlock; |
| 2086 | } |
| 2087 | break; |
| 2088 | default: |
| 2089 | dev_err(hba->dev, |
| 2090 | "%s: Expected query flag opcode but got = %d\n", |
| 2091 | __func__, opcode); |
| 2092 | err = -EINVAL; |
| 2093 | goto out_unlock; |
| 2094 | } |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2095 | |
Yaniv Gardi | e5ad406 | 2016-02-01 15:02:41 +0200 | [diff] [blame] | 2096 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2097 | |
| 2098 | if (err) { |
| 2099 | dev_err(hba->dev, |
| 2100 | "%s: Sending flag query for idn %d failed, err = %d\n", |
| 2101 | __func__, idn, err); |
| 2102 | goto out_unlock; |
| 2103 | } |
| 2104 | |
| 2105 | if (flag_res) |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 2106 | *flag_res = (be32_to_cpu(response->upiu_res.value) & |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2107 | MASK_QUERY_UPIU_FLAG_LOC) & 0x1; |
| 2108 | |
| 2109 | out_unlock: |
| 2110 | mutex_unlock(&hba->dev_cmd.lock); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2111 | ufshcd_release(hba); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2112 | return err; |
| 2113 | } |
| 2114 | |
| 2115 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2116 | * ufshcd_query_attr - API function for sending attribute requests |
| 2117 | * hba: per-adapter instance |
| 2118 | * opcode: attribute opcode |
| 2119 | * idn: attribute idn to access |
| 2120 | * index: index field |
| 2121 | * selector: selector field |
| 2122 | * attr_val: the attribute value after the query request completes |
| 2123 | * |
| 2124 | * Returns 0 for success, non-zero in case of failure |
| 2125 | */ |
Sujit Reddy Thumma | bdbe5d2 | 2014-05-26 10:59:11 +0530 | [diff] [blame] | 2126 | 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] | 2127 | enum attr_idn idn, u8 index, u8 selector, u32 *attr_val) |
| 2128 | { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2129 | struct ufs_query_req *request = NULL; |
| 2130 | struct ufs_query_res *response = NULL; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2131 | int err; |
| 2132 | |
| 2133 | BUG_ON(!hba); |
| 2134 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2135 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2136 | if (!attr_val) { |
| 2137 | dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n", |
| 2138 | __func__, opcode); |
| 2139 | err = -EINVAL; |
| 2140 | goto out; |
| 2141 | } |
| 2142 | |
| 2143 | mutex_lock(&hba->dev_cmd.lock); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2144 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 2145 | selector); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2146 | |
| 2147 | switch (opcode) { |
| 2148 | case UPIU_QUERY_OPCODE_WRITE_ATTR: |
| 2149 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 2150 | request->upiu_req.value = cpu_to_be32(*attr_val); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2151 | break; |
| 2152 | case UPIU_QUERY_OPCODE_READ_ATTR: |
| 2153 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 2154 | break; |
| 2155 | default: |
| 2156 | dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n", |
| 2157 | __func__, opcode); |
| 2158 | err = -EINVAL; |
| 2159 | goto out_unlock; |
| 2160 | } |
| 2161 | |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2162 | 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] | 2163 | |
| 2164 | if (err) { |
Yaniv Gardi | 4b761b5 | 2016-11-23 16:31:18 -0800 | [diff] [blame] | 2165 | dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n", |
| 2166 | __func__, opcode, idn, index, err); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2167 | goto out_unlock; |
| 2168 | } |
| 2169 | |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 2170 | *attr_val = be32_to_cpu(response->upiu_res.value); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2171 | |
| 2172 | out_unlock: |
| 2173 | mutex_unlock(&hba->dev_cmd.lock); |
| 2174 | out: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2175 | ufshcd_release(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2176 | return err; |
| 2177 | } |
| 2178 | |
| 2179 | /** |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 2180 | * ufshcd_query_attr_retry() - API function for sending query |
| 2181 | * attribute with retries |
| 2182 | * @hba: per-adapter instance |
| 2183 | * @opcode: attribute opcode |
| 2184 | * @idn: attribute idn to access |
| 2185 | * @index: index field |
| 2186 | * @selector: selector field |
| 2187 | * @attr_val: the attribute value after the query request |
| 2188 | * completes |
| 2189 | * |
| 2190 | * Returns 0 for success, non-zero in case of failure |
| 2191 | */ |
| 2192 | static int ufshcd_query_attr_retry(struct ufs_hba *hba, |
| 2193 | enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector, |
| 2194 | u32 *attr_val) |
| 2195 | { |
| 2196 | int ret = 0; |
| 2197 | u32 retries; |
| 2198 | |
| 2199 | for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { |
| 2200 | ret = ufshcd_query_attr(hba, opcode, idn, index, |
| 2201 | selector, attr_val); |
| 2202 | if (ret) |
| 2203 | dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n", |
| 2204 | __func__, ret, retries); |
| 2205 | else |
| 2206 | break; |
| 2207 | } |
| 2208 | |
| 2209 | if (ret) |
| 2210 | dev_err(hba->dev, |
| 2211 | "%s: query attribute, idn %d, failed with error %d after %d retires\n", |
| 2212 | __func__, idn, ret, QUERY_REQ_RETRIES); |
| 2213 | return ret; |
| 2214 | } |
| 2215 | |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 2216 | static int __ufshcd_query_descriptor(struct ufs_hba *hba, |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2217 | enum query_opcode opcode, enum desc_idn idn, u8 index, |
| 2218 | u8 selector, u8 *desc_buf, int *buf_len) |
| 2219 | { |
| 2220 | struct ufs_query_req *request = NULL; |
| 2221 | struct ufs_query_res *response = NULL; |
| 2222 | int err; |
| 2223 | |
| 2224 | BUG_ON(!hba); |
| 2225 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2226 | ufshcd_hold(hba, false); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2227 | if (!desc_buf) { |
| 2228 | dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n", |
| 2229 | __func__, opcode); |
| 2230 | err = -EINVAL; |
| 2231 | goto out; |
| 2232 | } |
| 2233 | |
| 2234 | if (*buf_len <= QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) { |
| 2235 | dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n", |
| 2236 | __func__, *buf_len); |
| 2237 | err = -EINVAL; |
| 2238 | goto out; |
| 2239 | } |
| 2240 | |
| 2241 | mutex_lock(&hba->dev_cmd.lock); |
| 2242 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 2243 | selector); |
| 2244 | hba->dev_cmd.query.descriptor = desc_buf; |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 2245 | request->upiu_req.length = cpu_to_be16(*buf_len); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2246 | |
| 2247 | switch (opcode) { |
| 2248 | case UPIU_QUERY_OPCODE_WRITE_DESC: |
| 2249 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
| 2250 | break; |
| 2251 | case UPIU_QUERY_OPCODE_READ_DESC: |
| 2252 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 2253 | break; |
| 2254 | default: |
| 2255 | dev_err(hba->dev, |
| 2256 | "%s: Expected query descriptor opcode but got = 0x%.2x\n", |
| 2257 | __func__, opcode); |
| 2258 | err = -EINVAL; |
| 2259 | goto out_unlock; |
| 2260 | } |
| 2261 | |
| 2262 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT); |
| 2263 | |
| 2264 | if (err) { |
Yaniv Gardi | 4b761b5 | 2016-11-23 16:31:18 -0800 | [diff] [blame] | 2265 | dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n", |
| 2266 | __func__, opcode, idn, index, err); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2267 | goto out_unlock; |
| 2268 | } |
| 2269 | |
| 2270 | hba->dev_cmd.query.descriptor = NULL; |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 2271 | *buf_len = be16_to_cpu(response->upiu_res.length); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2272 | |
| 2273 | out_unlock: |
| 2274 | mutex_unlock(&hba->dev_cmd.lock); |
| 2275 | out: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2276 | ufshcd_release(hba); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2277 | return err; |
| 2278 | } |
| 2279 | |
| 2280 | /** |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 2281 | * ufshcd_query_descriptor_retry - API function for sending descriptor |
| 2282 | * requests |
| 2283 | * hba: per-adapter instance |
| 2284 | * opcode: attribute opcode |
| 2285 | * idn: attribute idn to access |
| 2286 | * index: index field |
| 2287 | * selector: selector field |
| 2288 | * desc_buf: the buffer that contains the descriptor |
| 2289 | * buf_len: length parameter passed to the device |
| 2290 | * |
| 2291 | * Returns 0 for success, non-zero in case of failure. |
| 2292 | * The buf_len parameter will contain, on return, the length parameter |
| 2293 | * received on the response. |
| 2294 | */ |
| 2295 | int ufshcd_query_descriptor_retry(struct ufs_hba *hba, |
| 2296 | enum query_opcode opcode, enum desc_idn idn, u8 index, |
| 2297 | u8 selector, u8 *desc_buf, int *buf_len) |
| 2298 | { |
| 2299 | int err; |
| 2300 | int retries; |
| 2301 | |
| 2302 | for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { |
| 2303 | err = __ufshcd_query_descriptor(hba, opcode, idn, index, |
| 2304 | selector, desc_buf, buf_len); |
| 2305 | if (!err || err == -EINVAL) |
| 2306 | break; |
| 2307 | } |
| 2308 | |
| 2309 | return err; |
| 2310 | } |
| 2311 | EXPORT_SYMBOL(ufshcd_query_descriptor_retry); |
| 2312 | |
| 2313 | /** |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 2314 | * ufshcd_read_desc_param - read the specified descriptor parameter |
| 2315 | * @hba: Pointer to adapter instance |
| 2316 | * @desc_id: descriptor idn value |
| 2317 | * @desc_index: descriptor index |
| 2318 | * @param_offset: offset of the parameter to read |
| 2319 | * @param_read_buf: pointer to buffer where parameter would be read |
| 2320 | * @param_size: sizeof(param_read_buf) |
| 2321 | * |
| 2322 | * Return 0 in case of success, non-zero otherwise |
| 2323 | */ |
| 2324 | static int ufshcd_read_desc_param(struct ufs_hba *hba, |
| 2325 | enum desc_idn desc_id, |
| 2326 | int desc_index, |
| 2327 | u32 param_offset, |
| 2328 | u8 *param_read_buf, |
| 2329 | u32 param_size) |
| 2330 | { |
| 2331 | int ret; |
| 2332 | u8 *desc_buf; |
| 2333 | u32 buff_len; |
| 2334 | bool is_kmalloc = true; |
| 2335 | |
| 2336 | /* safety checks */ |
| 2337 | if (desc_id >= QUERY_DESC_IDN_MAX) |
| 2338 | return -EINVAL; |
| 2339 | |
| 2340 | buff_len = ufs_query_desc_max_size[desc_id]; |
| 2341 | if ((param_offset + param_size) > buff_len) |
| 2342 | return -EINVAL; |
| 2343 | |
| 2344 | if (!param_offset && (param_size == buff_len)) { |
| 2345 | /* memory space already available to hold full descriptor */ |
| 2346 | desc_buf = param_read_buf; |
| 2347 | is_kmalloc = false; |
| 2348 | } else { |
| 2349 | /* allocate memory to hold full descriptor */ |
| 2350 | desc_buf = kmalloc(buff_len, GFP_KERNEL); |
| 2351 | if (!desc_buf) |
| 2352 | return -ENOMEM; |
| 2353 | } |
| 2354 | |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 2355 | ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC, |
| 2356 | desc_id, desc_index, 0, desc_buf, |
| 2357 | &buff_len); |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 2358 | |
subhashj@codeaurora.org | bde44bb | 2016-11-23 16:31:41 -0800 | [diff] [blame] | 2359 | if (ret) { |
| 2360 | dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d", |
| 2361 | __func__, desc_id, desc_index, param_offset, ret); |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 2362 | |
| 2363 | goto out; |
| 2364 | } |
| 2365 | |
subhashj@codeaurora.org | bde44bb | 2016-11-23 16:31:41 -0800 | [diff] [blame] | 2366 | /* Sanity check */ |
| 2367 | if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) { |
| 2368 | dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header", |
| 2369 | __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]); |
| 2370 | ret = -EINVAL; |
| 2371 | goto out; |
| 2372 | } |
| 2373 | |
| 2374 | /* |
| 2375 | * While reading variable size descriptors (like string descriptor), |
| 2376 | * some UFS devices may report the "LENGTH" (field in "Transaction |
| 2377 | * Specific fields" of Query Response UPIU) same as what was requested |
| 2378 | * in Query Request UPIU instead of reporting the actual size of the |
| 2379 | * variable size descriptor. |
| 2380 | * Although it's safe to ignore the "LENGTH" field for variable size |
| 2381 | * descriptors as we can always derive the length of the descriptor from |
| 2382 | * the descriptor header fields. Hence this change impose the length |
| 2383 | * match check only for fixed size descriptors (for which we always |
| 2384 | * request the correct size as part of Query Request UPIU). |
| 2385 | */ |
| 2386 | if ((desc_id != QUERY_DESC_IDN_STRING) && |
| 2387 | (buff_len != desc_buf[QUERY_DESC_LENGTH_OFFSET])) { |
| 2388 | dev_err(hba->dev, "%s: desc_buf length mismatch: buff_len %d, buff_len(desc_header) %d", |
| 2389 | __func__, buff_len, desc_buf[QUERY_DESC_LENGTH_OFFSET]); |
| 2390 | ret = -EINVAL; |
| 2391 | goto out; |
| 2392 | } |
| 2393 | |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 2394 | if (is_kmalloc) |
| 2395 | memcpy(param_read_buf, &desc_buf[param_offset], param_size); |
| 2396 | out: |
| 2397 | if (is_kmalloc) |
| 2398 | kfree(desc_buf); |
| 2399 | return ret; |
| 2400 | } |
| 2401 | |
| 2402 | static inline int ufshcd_read_desc(struct ufs_hba *hba, |
| 2403 | enum desc_idn desc_id, |
| 2404 | int desc_index, |
| 2405 | u8 *buf, |
| 2406 | u32 size) |
| 2407 | { |
| 2408 | return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size); |
| 2409 | } |
| 2410 | |
| 2411 | static inline int ufshcd_read_power_desc(struct ufs_hba *hba, |
| 2412 | u8 *buf, |
| 2413 | u32 size) |
| 2414 | { |
Dolev Raviv | 61e0735 | 2016-11-23 16:30:49 -0800 | [diff] [blame] | 2415 | int err = 0; |
| 2416 | int retries; |
| 2417 | |
| 2418 | for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { |
| 2419 | /* Read descriptor*/ |
| 2420 | err = ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size); |
| 2421 | if (!err) |
| 2422 | break; |
| 2423 | dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err); |
| 2424 | } |
| 2425 | |
| 2426 | return err; |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 2427 | } |
| 2428 | |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 2429 | int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size) |
| 2430 | { |
| 2431 | return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size); |
| 2432 | } |
| 2433 | EXPORT_SYMBOL(ufshcd_read_device_desc); |
| 2434 | |
| 2435 | /** |
| 2436 | * ufshcd_read_string_desc - read string descriptor |
| 2437 | * @hba: pointer to adapter instance |
| 2438 | * @desc_index: descriptor index |
| 2439 | * @buf: pointer to buffer where descriptor would be read |
| 2440 | * @size: size of buf |
| 2441 | * @ascii: if true convert from unicode to ascii characters |
| 2442 | * |
| 2443 | * Return 0 in case of success, non-zero otherwise |
| 2444 | */ |
| 2445 | int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index, u8 *buf, |
| 2446 | u32 size, bool ascii) |
| 2447 | { |
| 2448 | int err = 0; |
| 2449 | |
| 2450 | err = ufshcd_read_desc(hba, |
| 2451 | QUERY_DESC_IDN_STRING, desc_index, buf, size); |
| 2452 | |
| 2453 | if (err) { |
| 2454 | dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n", |
| 2455 | __func__, QUERY_REQ_RETRIES, err); |
| 2456 | goto out; |
| 2457 | } |
| 2458 | |
| 2459 | if (ascii) { |
| 2460 | int desc_len; |
| 2461 | int ascii_len; |
| 2462 | int i; |
| 2463 | char *buff_ascii; |
| 2464 | |
| 2465 | desc_len = buf[0]; |
| 2466 | /* remove header and divide by 2 to move from UTF16 to UTF8 */ |
| 2467 | ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1; |
| 2468 | if (size < ascii_len + QUERY_DESC_HDR_SIZE) { |
| 2469 | dev_err(hba->dev, "%s: buffer allocated size is too small\n", |
| 2470 | __func__); |
| 2471 | err = -ENOMEM; |
| 2472 | goto out; |
| 2473 | } |
| 2474 | |
| 2475 | buff_ascii = kmalloc(ascii_len, GFP_KERNEL); |
| 2476 | if (!buff_ascii) { |
| 2477 | err = -ENOMEM; |
Tiezhu Yang | fcbefc3 | 2016-06-25 12:35:22 +0800 | [diff] [blame] | 2478 | goto out; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 2479 | } |
| 2480 | |
| 2481 | /* |
| 2482 | * the descriptor contains string in UTF16 format |
| 2483 | * we need to convert to utf-8 so it can be displayed |
| 2484 | */ |
| 2485 | utf16s_to_utf8s((wchar_t *)&buf[QUERY_DESC_HDR_SIZE], |
| 2486 | desc_len - QUERY_DESC_HDR_SIZE, |
| 2487 | UTF16_BIG_ENDIAN, buff_ascii, ascii_len); |
| 2488 | |
| 2489 | /* replace non-printable or non-ASCII characters with spaces */ |
| 2490 | for (i = 0; i < ascii_len; i++) |
| 2491 | ufshcd_remove_non_printable(&buff_ascii[i]); |
| 2492 | |
| 2493 | memset(buf + QUERY_DESC_HDR_SIZE, 0, |
| 2494 | size - QUERY_DESC_HDR_SIZE); |
| 2495 | memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len); |
| 2496 | buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 2497 | kfree(buff_ascii); |
| 2498 | } |
| 2499 | out: |
| 2500 | return err; |
| 2501 | } |
| 2502 | EXPORT_SYMBOL(ufshcd_read_string_desc); |
| 2503 | |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 2504 | /** |
| 2505 | * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter |
| 2506 | * @hba: Pointer to adapter instance |
| 2507 | * @lun: lun id |
| 2508 | * @param_offset: offset of the parameter to read |
| 2509 | * @param_read_buf: pointer to buffer where parameter would be read |
| 2510 | * @param_size: sizeof(param_read_buf) |
| 2511 | * |
| 2512 | * Return 0 in case of success, non-zero otherwise |
| 2513 | */ |
| 2514 | static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba, |
| 2515 | int lun, |
| 2516 | enum unit_desc_param param_offset, |
| 2517 | u8 *param_read_buf, |
| 2518 | u32 param_size) |
| 2519 | { |
| 2520 | /* |
| 2521 | * Unit descriptors are only available for general purpose LUs (LUN id |
| 2522 | * from 0 to 7) and RPMB Well known LU. |
| 2523 | */ |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 2524 | if (lun != UFS_UPIU_RPMB_WLUN && (lun >= UFS_UPIU_MAX_GENERAL_LUN)) |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 2525 | return -EOPNOTSUPP; |
| 2526 | |
| 2527 | return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun, |
| 2528 | param_offset, param_read_buf, param_size); |
| 2529 | } |
| 2530 | |
| 2531 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2532 | * ufshcd_memory_alloc - allocate memory for host memory space data structures |
| 2533 | * @hba: per adapter instance |
| 2534 | * |
| 2535 | * 1. Allocate DMA memory for Command Descriptor array |
| 2536 | * Each command descriptor consist of Command UPIU, Response UPIU and PRDT |
| 2537 | * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL). |
| 2538 | * 3. Allocate DMA memory for UTP Task Management Request Descriptor List |
| 2539 | * (UTMRDL) |
| 2540 | * 4. Allocate memory for local reference block(lrb). |
| 2541 | * |
| 2542 | * Returns 0 for success, non-zero in case of failure |
| 2543 | */ |
| 2544 | static int ufshcd_memory_alloc(struct ufs_hba *hba) |
| 2545 | { |
| 2546 | size_t utmrdl_size, utrdl_size, ucdl_size; |
| 2547 | |
| 2548 | /* Allocate memory for UTP command descriptors */ |
| 2549 | ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 2550 | hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 2551 | ucdl_size, |
| 2552 | &hba->ucdl_dma_addr, |
| 2553 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2554 | |
| 2555 | /* |
| 2556 | * UFSHCI requires UTP command descriptor to be 128 byte aligned. |
| 2557 | * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE |
| 2558 | * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will |
| 2559 | * be aligned to 128 bytes as well |
| 2560 | */ |
| 2561 | if (!hba->ucdl_base_addr || |
| 2562 | WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 2563 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2564 | "Command Descriptor Memory allocation failed\n"); |
| 2565 | goto out; |
| 2566 | } |
| 2567 | |
| 2568 | /* |
| 2569 | * Allocate memory for UTP Transfer descriptors |
| 2570 | * UFSHCI requires 1024 byte alignment of UTRD |
| 2571 | */ |
| 2572 | utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 2573 | hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 2574 | utrdl_size, |
| 2575 | &hba->utrdl_dma_addr, |
| 2576 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2577 | if (!hba->utrdl_base_addr || |
| 2578 | WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 2579 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2580 | "Transfer Descriptor Memory allocation failed\n"); |
| 2581 | goto out; |
| 2582 | } |
| 2583 | |
| 2584 | /* |
| 2585 | * Allocate memory for UTP Task Management descriptors |
| 2586 | * UFSHCI requires 1024 byte alignment of UTMRD |
| 2587 | */ |
| 2588 | utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs; |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 2589 | hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 2590 | utmrdl_size, |
| 2591 | &hba->utmrdl_dma_addr, |
| 2592 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2593 | if (!hba->utmrdl_base_addr || |
| 2594 | WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 2595 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2596 | "Task Management Descriptor Memory allocation failed\n"); |
| 2597 | goto out; |
| 2598 | } |
| 2599 | |
| 2600 | /* Allocate memory for local reference block */ |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 2601 | hba->lrb = devm_kzalloc(hba->dev, |
| 2602 | hba->nutrs * sizeof(struct ufshcd_lrb), |
| 2603 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2604 | if (!hba->lrb) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 2605 | dev_err(hba->dev, "LRB Memory allocation failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2606 | goto out; |
| 2607 | } |
| 2608 | return 0; |
| 2609 | out: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2610 | return -ENOMEM; |
| 2611 | } |
| 2612 | |
| 2613 | /** |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 2614 | * ufshcd_print_pwr_info - print power params as saved in hba |
| 2615 | * power info |
| 2616 | * @hba: per-adapter instance |
| 2617 | */ |
| 2618 | static void ufshcd_print_pwr_info(struct ufs_hba *hba) |
| 2619 | { |
| 2620 | static const char * const names[] = { |
| 2621 | "INVALID MODE", |
| 2622 | "FAST MODE", |
| 2623 | "SLOW_MODE", |
| 2624 | "INVALID MODE", |
| 2625 | "FASTAUTO_MODE", |
| 2626 | "SLOWAUTO_MODE", |
| 2627 | "INVALID MODE", |
| 2628 | }; |
| 2629 | |
| 2630 | dev_info(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n", |
| 2631 | __func__, |
| 2632 | hba->pwr_info.gear_rx, hba->pwr_info.gear_tx, |
| 2633 | hba->pwr_info.lane_rx, hba->pwr_info.lane_tx, |
| 2634 | names[hba->pwr_info.pwr_rx], |
| 2635 | names[hba->pwr_info.pwr_tx], |
| 2636 | hba->pwr_info.hs_rate); |
| 2637 | } |
| 2638 | |
| 2639 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2640 | * ufshcd_host_memory_configure - configure local reference block with |
| 2641 | * memory offsets |
| 2642 | * @hba: per adapter instance |
| 2643 | * |
| 2644 | * Configure Host memory space |
| 2645 | * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA |
| 2646 | * address. |
| 2647 | * 2. Update each UTRD with Response UPIU offset, Response UPIU length |
| 2648 | * and PRDT offset. |
| 2649 | * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT |
| 2650 | * into local reference block. |
| 2651 | */ |
| 2652 | static void ufshcd_host_memory_configure(struct ufs_hba *hba) |
| 2653 | { |
| 2654 | struct utp_transfer_cmd_desc *cmd_descp; |
| 2655 | struct utp_transfer_req_desc *utrdlp; |
| 2656 | dma_addr_t cmd_desc_dma_addr; |
| 2657 | dma_addr_t cmd_desc_element_addr; |
| 2658 | u16 response_offset; |
| 2659 | u16 prdt_offset; |
| 2660 | int cmd_desc_size; |
| 2661 | int i; |
| 2662 | |
| 2663 | utrdlp = hba->utrdl_base_addr; |
| 2664 | cmd_descp = hba->ucdl_base_addr; |
| 2665 | |
| 2666 | response_offset = |
| 2667 | offsetof(struct utp_transfer_cmd_desc, response_upiu); |
| 2668 | prdt_offset = |
| 2669 | offsetof(struct utp_transfer_cmd_desc, prd_table); |
| 2670 | |
| 2671 | cmd_desc_size = sizeof(struct utp_transfer_cmd_desc); |
| 2672 | cmd_desc_dma_addr = hba->ucdl_dma_addr; |
| 2673 | |
| 2674 | for (i = 0; i < hba->nutrs; i++) { |
| 2675 | /* Configure UTRD with command descriptor base address */ |
| 2676 | cmd_desc_element_addr = |
| 2677 | (cmd_desc_dma_addr + (cmd_desc_size * i)); |
| 2678 | utrdlp[i].command_desc_base_addr_lo = |
| 2679 | cpu_to_le32(lower_32_bits(cmd_desc_element_addr)); |
| 2680 | utrdlp[i].command_desc_base_addr_hi = |
| 2681 | cpu_to_le32(upper_32_bits(cmd_desc_element_addr)); |
| 2682 | |
| 2683 | /* Response upiu and prdt offset should be in double words */ |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 2684 | if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) { |
| 2685 | utrdlp[i].response_upiu_offset = |
| 2686 | cpu_to_le16(response_offset); |
| 2687 | utrdlp[i].prd_table_offset = |
| 2688 | cpu_to_le16(prdt_offset); |
| 2689 | utrdlp[i].response_upiu_length = |
| 2690 | cpu_to_le16(ALIGNED_UPIU_SIZE); |
| 2691 | } else { |
| 2692 | utrdlp[i].response_upiu_offset = |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2693 | cpu_to_le16((response_offset >> 2)); |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 2694 | utrdlp[i].prd_table_offset = |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2695 | cpu_to_le16((prdt_offset >> 2)); |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 2696 | utrdlp[i].response_upiu_length = |
Sujit Reddy Thumma | 3ca316c | 2013-06-26 22:39:30 +0530 | [diff] [blame] | 2697 | cpu_to_le16(ALIGNED_UPIU_SIZE >> 2); |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 2698 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2699 | |
| 2700 | hba->lrb[i].utr_descriptor_ptr = (utrdlp + i); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2701 | hba->lrb[i].ucd_req_ptr = |
| 2702 | (struct utp_upiu_req *)(cmd_descp + i); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2703 | hba->lrb[i].ucd_rsp_ptr = |
| 2704 | (struct utp_upiu_rsp *)cmd_descp[i].response_upiu; |
| 2705 | hba->lrb[i].ucd_prdt_ptr = |
| 2706 | (struct ufshcd_sg_entry *)cmd_descp[i].prd_table; |
| 2707 | } |
| 2708 | } |
| 2709 | |
| 2710 | /** |
| 2711 | * ufshcd_dme_link_startup - Notify Unipro to perform link startup |
| 2712 | * @hba: per adapter instance |
| 2713 | * |
| 2714 | * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer, |
| 2715 | * in order to initialize the Unipro link startup procedure. |
| 2716 | * Once the Unipro links are up, the device connected to the controller |
| 2717 | * is detected. |
| 2718 | * |
| 2719 | * Returns 0 on success, non-zero value on failure |
| 2720 | */ |
| 2721 | static int ufshcd_dme_link_startup(struct ufs_hba *hba) |
| 2722 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2723 | struct uic_command uic_cmd = {0}; |
| 2724 | int ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2725 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2726 | uic_cmd.command = UIC_CMD_DME_LINK_STARTUP; |
| 2727 | |
| 2728 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 2729 | if (ret) |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 2730 | dev_err(hba->dev, |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2731 | "dme-link-startup: error code %d\n", ret); |
| 2732 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2733 | } |
| 2734 | |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 2735 | static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba) |
| 2736 | { |
| 2737 | #define MIN_DELAY_BEFORE_DME_CMDS_US 1000 |
| 2738 | unsigned long min_sleep_time_us; |
| 2739 | |
| 2740 | if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS)) |
| 2741 | return; |
| 2742 | |
| 2743 | /* |
| 2744 | * last_dme_cmd_tstamp will be 0 only for 1st call to |
| 2745 | * this function |
| 2746 | */ |
| 2747 | if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) { |
| 2748 | min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US; |
| 2749 | } else { |
| 2750 | unsigned long delta = |
| 2751 | (unsigned long) ktime_to_us( |
| 2752 | ktime_sub(ktime_get(), |
| 2753 | hba->last_dme_cmd_tstamp)); |
| 2754 | |
| 2755 | if (delta < MIN_DELAY_BEFORE_DME_CMDS_US) |
| 2756 | min_sleep_time_us = |
| 2757 | MIN_DELAY_BEFORE_DME_CMDS_US - delta; |
| 2758 | else |
| 2759 | return; /* no more delay required */ |
| 2760 | } |
| 2761 | |
| 2762 | /* allow sleep for extra 50us if needed */ |
| 2763 | usleep_range(min_sleep_time_us, min_sleep_time_us + 50); |
| 2764 | } |
| 2765 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2766 | /** |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 2767 | * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET |
| 2768 | * @hba: per adapter instance |
| 2769 | * @attr_sel: uic command argument1 |
| 2770 | * @attr_set: attribute set type as uic command argument2 |
| 2771 | * @mib_val: setting value as uic command argument3 |
| 2772 | * @peer: indicate whether peer or local |
| 2773 | * |
| 2774 | * Returns 0 on success, non-zero value on failure |
| 2775 | */ |
| 2776 | int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel, |
| 2777 | u8 attr_set, u32 mib_val, u8 peer) |
| 2778 | { |
| 2779 | struct uic_command uic_cmd = {0}; |
| 2780 | static const char *const action[] = { |
| 2781 | "dme-set", |
| 2782 | "dme-peer-set" |
| 2783 | }; |
| 2784 | const char *set = action[!!peer]; |
| 2785 | int ret; |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 2786 | int retries = UFS_UIC_COMMAND_RETRIES; |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 2787 | |
| 2788 | uic_cmd.command = peer ? |
| 2789 | UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET; |
| 2790 | uic_cmd.argument1 = attr_sel; |
| 2791 | uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set); |
| 2792 | uic_cmd.argument3 = mib_val; |
| 2793 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 2794 | do { |
| 2795 | /* for peer attributes we retry upon failure */ |
| 2796 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 2797 | if (ret) |
| 2798 | dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n", |
| 2799 | set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret); |
| 2800 | } while (ret && peer && --retries); |
| 2801 | |
Yaniv Gardi | f37e9f8 | 2016-11-23 16:32:49 -0800 | [diff] [blame] | 2802 | if (ret) |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 2803 | dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n", |
Yaniv Gardi | f37e9f8 | 2016-11-23 16:32:49 -0800 | [diff] [blame] | 2804 | set, UIC_GET_ATTR_ID(attr_sel), mib_val, |
| 2805 | UFS_UIC_COMMAND_RETRIES - retries); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 2806 | |
| 2807 | return ret; |
| 2808 | } |
| 2809 | EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr); |
| 2810 | |
| 2811 | /** |
| 2812 | * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET |
| 2813 | * @hba: per adapter instance |
| 2814 | * @attr_sel: uic command argument1 |
| 2815 | * @mib_val: the value of the attribute as returned by the UIC command |
| 2816 | * @peer: indicate whether peer or local |
| 2817 | * |
| 2818 | * Returns 0 on success, non-zero value on failure |
| 2819 | */ |
| 2820 | int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel, |
| 2821 | u32 *mib_val, u8 peer) |
| 2822 | { |
| 2823 | struct uic_command uic_cmd = {0}; |
| 2824 | static const char *const action[] = { |
| 2825 | "dme-get", |
| 2826 | "dme-peer-get" |
| 2827 | }; |
| 2828 | const char *get = action[!!peer]; |
| 2829 | int ret; |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 2830 | int retries = UFS_UIC_COMMAND_RETRIES; |
Yaniv Gardi | 874237f | 2015-05-17 18:55:03 +0300 | [diff] [blame] | 2831 | struct ufs_pa_layer_attr orig_pwr_info; |
| 2832 | struct ufs_pa_layer_attr temp_pwr_info; |
| 2833 | bool pwr_mode_change = false; |
| 2834 | |
| 2835 | if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) { |
| 2836 | orig_pwr_info = hba->pwr_info; |
| 2837 | temp_pwr_info = orig_pwr_info; |
| 2838 | |
| 2839 | if (orig_pwr_info.pwr_tx == FAST_MODE || |
| 2840 | orig_pwr_info.pwr_rx == FAST_MODE) { |
| 2841 | temp_pwr_info.pwr_tx = FASTAUTO_MODE; |
| 2842 | temp_pwr_info.pwr_rx = FASTAUTO_MODE; |
| 2843 | pwr_mode_change = true; |
| 2844 | } else if (orig_pwr_info.pwr_tx == SLOW_MODE || |
| 2845 | orig_pwr_info.pwr_rx == SLOW_MODE) { |
| 2846 | temp_pwr_info.pwr_tx = SLOWAUTO_MODE; |
| 2847 | temp_pwr_info.pwr_rx = SLOWAUTO_MODE; |
| 2848 | pwr_mode_change = true; |
| 2849 | } |
| 2850 | if (pwr_mode_change) { |
| 2851 | ret = ufshcd_change_power_mode(hba, &temp_pwr_info); |
| 2852 | if (ret) |
| 2853 | goto out; |
| 2854 | } |
| 2855 | } |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 2856 | |
| 2857 | uic_cmd.command = peer ? |
| 2858 | UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET; |
| 2859 | uic_cmd.argument1 = attr_sel; |
| 2860 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 2861 | do { |
| 2862 | /* for peer attributes we retry upon failure */ |
| 2863 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 2864 | if (ret) |
| 2865 | dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n", |
| 2866 | get, UIC_GET_ATTR_ID(attr_sel), ret); |
| 2867 | } while (ret && peer && --retries); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 2868 | |
Yaniv Gardi | f37e9f8 | 2016-11-23 16:32:49 -0800 | [diff] [blame] | 2869 | if (ret) |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 2870 | dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n", |
Yaniv Gardi | f37e9f8 | 2016-11-23 16:32:49 -0800 | [diff] [blame] | 2871 | get, UIC_GET_ATTR_ID(attr_sel), |
| 2872 | UFS_UIC_COMMAND_RETRIES - retries); |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 2873 | |
| 2874 | if (mib_val && !ret) |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 2875 | *mib_val = uic_cmd.argument3; |
Yaniv Gardi | 874237f | 2015-05-17 18:55:03 +0300 | [diff] [blame] | 2876 | |
| 2877 | if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE) |
| 2878 | && pwr_mode_change) |
| 2879 | ufshcd_change_power_mode(hba, &orig_pwr_info); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 2880 | out: |
| 2881 | return ret; |
| 2882 | } |
| 2883 | EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr); |
| 2884 | |
| 2885 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2886 | * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power |
| 2887 | * state) and waits for it to take effect. |
| 2888 | * |
| 2889 | * @hba: per adapter instance |
| 2890 | * @cmd: UIC command to execute |
| 2891 | * |
| 2892 | * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER & |
| 2893 | * DME_HIBERNATE_EXIT commands take some time to take its effect on both host |
| 2894 | * and device UniPro link and hence it's final completion would be indicated by |
| 2895 | * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in |
| 2896 | * addition to normal UIC command completion Status (UCCS). This function only |
| 2897 | * returns after the relevant status bits indicate the completion. |
| 2898 | * |
| 2899 | * Returns 0 on success, non-zero value on failure |
| 2900 | */ |
| 2901 | static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd) |
| 2902 | { |
| 2903 | struct completion uic_async_done; |
| 2904 | unsigned long flags; |
| 2905 | u8 status; |
| 2906 | int ret; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2907 | bool reenable_intr = false; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2908 | |
| 2909 | mutex_lock(&hba->uic_cmd_mutex); |
| 2910 | init_completion(&uic_async_done); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 2911 | ufshcd_add_delay_before_dme_cmd(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2912 | |
| 2913 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2914 | hba->uic_async_done = &uic_async_done; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2915 | if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) { |
| 2916 | ufshcd_disable_intr(hba, UIC_COMMAND_COMPL); |
| 2917 | /* |
| 2918 | * Make sure UIC command completion interrupt is disabled before |
| 2919 | * issuing UIC command. |
| 2920 | */ |
| 2921 | wmb(); |
| 2922 | reenable_intr = true; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2923 | } |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2924 | ret = __ufshcd_send_uic_cmd(hba, cmd, false); |
| 2925 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2926 | if (ret) { |
| 2927 | dev_err(hba->dev, |
| 2928 | "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n", |
| 2929 | cmd->command, cmd->argument3, ret); |
| 2930 | goto out; |
| 2931 | } |
| 2932 | |
| 2933 | if (!wait_for_completion_timeout(hba->uic_async_done, |
| 2934 | msecs_to_jiffies(UIC_CMD_TIMEOUT))) { |
| 2935 | dev_err(hba->dev, |
| 2936 | "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n", |
| 2937 | cmd->command, cmd->argument3); |
| 2938 | ret = -ETIMEDOUT; |
| 2939 | goto out; |
| 2940 | } |
| 2941 | |
| 2942 | status = ufshcd_get_upmcrs(hba); |
| 2943 | if (status != PWR_LOCAL) { |
| 2944 | dev_err(hba->dev, |
Kiwoong Kim | 7361542 | 2016-09-08 16:50:02 +0900 | [diff] [blame] | 2945 | "pwr ctrl cmd 0x%0x failed, host upmcrs:0x%x\n", |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2946 | cmd->command, status); |
| 2947 | ret = (status != PWR_OK) ? status : -1; |
| 2948 | } |
| 2949 | out: |
| 2950 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2951 | hba->active_uic_cmd = NULL; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2952 | hba->uic_async_done = NULL; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2953 | if (reenable_intr) |
| 2954 | ufshcd_enable_intr(hba, UIC_COMMAND_COMPL); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2955 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2956 | mutex_unlock(&hba->uic_cmd_mutex); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2957 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2958 | return ret; |
| 2959 | } |
| 2960 | |
| 2961 | /** |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 2962 | * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage |
| 2963 | * using DME_SET primitives. |
| 2964 | * @hba: per adapter instance |
| 2965 | * @mode: powr mode value |
| 2966 | * |
| 2967 | * Returns 0 on success, non-zero value on failure |
| 2968 | */ |
Sujit Reddy Thumma | bdbe5d2 | 2014-05-26 10:59:11 +0530 | [diff] [blame] | 2969 | 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] | 2970 | { |
| 2971 | struct uic_command uic_cmd = {0}; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2972 | int ret; |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 2973 | |
Yaniv Gardi | c3a2f9e | 2015-05-17 18:55:01 +0300 | [diff] [blame] | 2974 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) { |
| 2975 | ret = ufshcd_dme_set(hba, |
| 2976 | UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1); |
| 2977 | if (ret) { |
| 2978 | dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n", |
| 2979 | __func__, ret); |
| 2980 | goto out; |
| 2981 | } |
| 2982 | } |
| 2983 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 2984 | uic_cmd.command = UIC_CMD_DME_SET; |
| 2985 | uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE); |
| 2986 | uic_cmd.argument3 = mode; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2987 | ufshcd_hold(hba, false); |
| 2988 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
| 2989 | ufshcd_release(hba); |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 2990 | |
Yaniv Gardi | c3a2f9e | 2015-05-17 18:55:01 +0300 | [diff] [blame] | 2991 | out: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2992 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2993 | } |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 2994 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 2995 | static int ufshcd_link_recovery(struct ufs_hba *hba) |
| 2996 | { |
| 2997 | int ret; |
| 2998 | unsigned long flags; |
| 2999 | |
| 3000 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 3001 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 3002 | ufshcd_set_eh_in_progress(hba); |
| 3003 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3004 | |
| 3005 | ret = ufshcd_host_reset_and_restore(hba); |
| 3006 | |
| 3007 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 3008 | if (ret) |
| 3009 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 3010 | ufshcd_clear_eh_in_progress(hba); |
| 3011 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3012 | |
| 3013 | if (ret) |
| 3014 | dev_err(hba->dev, "%s: link recovery failed, err %d", |
| 3015 | __func__, ret); |
| 3016 | |
| 3017 | return ret; |
| 3018 | } |
| 3019 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 3020 | static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3021 | { |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 3022 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3023 | struct uic_command uic_cmd = {0}; |
| 3024 | |
Kiwoong Kim | ee32c90 | 2016-11-10 21:17:43 +0900 | [diff] [blame] | 3025 | ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE); |
| 3026 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3027 | uic_cmd.command = UIC_CMD_DME_HIBER_ENTER; |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 3028 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3029 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 3030 | if (ret) { |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 3031 | dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n", |
| 3032 | __func__, ret); |
| 3033 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 3034 | /* |
| 3035 | * If link recovery fails then return error so that caller |
| 3036 | * don't retry the hibern8 enter again. |
| 3037 | */ |
| 3038 | if (ufshcd_link_recovery(hba)) |
| 3039 | ret = -ENOLINK; |
Kiwoong Kim | ee32c90 | 2016-11-10 21:17:43 +0900 | [diff] [blame] | 3040 | } else |
| 3041 | ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, |
| 3042 | POST_CHANGE); |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 3043 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 3044 | return ret; |
| 3045 | } |
| 3046 | |
| 3047 | static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba) |
| 3048 | { |
| 3049 | int ret = 0, retries; |
| 3050 | |
| 3051 | for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) { |
| 3052 | ret = __ufshcd_uic_hibern8_enter(hba); |
| 3053 | if (!ret || ret == -ENOLINK) |
| 3054 | goto out; |
| 3055 | } |
| 3056 | out: |
| 3057 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3058 | } |
| 3059 | |
| 3060 | static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba) |
| 3061 | { |
| 3062 | struct uic_command uic_cmd = {0}; |
| 3063 | int ret; |
| 3064 | |
Kiwoong Kim | ee32c90 | 2016-11-10 21:17:43 +0900 | [diff] [blame] | 3065 | ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE); |
| 3066 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3067 | uic_cmd.command = UIC_CMD_DME_HIBER_EXIT; |
| 3068 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3069 | if (ret) { |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 3070 | dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n", |
| 3071 | __func__, ret); |
| 3072 | ret = ufshcd_link_recovery(hba); |
Kiwoong Kim | ee32c90 | 2016-11-10 21:17:43 +0900 | [diff] [blame] | 3073 | } else |
| 3074 | ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, |
| 3075 | POST_CHANGE); |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3076 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3077 | return ret; |
| 3078 | } |
| 3079 | |
Yaniv Gardi | 5064636 | 2014-10-23 13:25:13 +0300 | [diff] [blame] | 3080 | /** |
| 3081 | * ufshcd_init_pwr_info - setting the POR (power on reset) |
| 3082 | * values in hba power info |
| 3083 | * @hba: per-adapter instance |
| 3084 | */ |
| 3085 | static void ufshcd_init_pwr_info(struct ufs_hba *hba) |
| 3086 | { |
| 3087 | hba->pwr_info.gear_rx = UFS_PWM_G1; |
| 3088 | hba->pwr_info.gear_tx = UFS_PWM_G1; |
| 3089 | hba->pwr_info.lane_rx = 1; |
| 3090 | hba->pwr_info.lane_tx = 1; |
| 3091 | hba->pwr_info.pwr_rx = SLOWAUTO_MODE; |
| 3092 | hba->pwr_info.pwr_tx = SLOWAUTO_MODE; |
| 3093 | hba->pwr_info.hs_rate = 0; |
| 3094 | } |
| 3095 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3096 | /** |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3097 | * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device |
| 3098 | * @hba: per-adapter instance |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3099 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3100 | static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3101 | { |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3102 | struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info; |
| 3103 | |
| 3104 | if (hba->max_pwr_info.is_valid) |
| 3105 | return 0; |
| 3106 | |
subhashj@codeaurora.org | 2349b53 | 2016-11-23 16:33:19 -0800 | [diff] [blame] | 3107 | pwr_info->pwr_tx = FAST_MODE; |
| 3108 | pwr_info->pwr_rx = FAST_MODE; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3109 | pwr_info->hs_rate = PA_HS_MODE_B; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3110 | |
| 3111 | /* Get the connected lane count */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3112 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES), |
| 3113 | &pwr_info->lane_rx); |
| 3114 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 3115 | &pwr_info->lane_tx); |
| 3116 | |
| 3117 | if (!pwr_info->lane_rx || !pwr_info->lane_tx) { |
| 3118 | dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n", |
| 3119 | __func__, |
| 3120 | pwr_info->lane_rx, |
| 3121 | pwr_info->lane_tx); |
| 3122 | return -EINVAL; |
| 3123 | } |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3124 | |
| 3125 | /* |
| 3126 | * First, get the maximum gears of HS speed. |
| 3127 | * If a zero value, it means there is no HSGEAR capability. |
| 3128 | * Then, get the maximum gears of PWM speed. |
| 3129 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3130 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx); |
| 3131 | if (!pwr_info->gear_rx) { |
| 3132 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), |
| 3133 | &pwr_info->gear_rx); |
| 3134 | if (!pwr_info->gear_rx) { |
| 3135 | dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n", |
| 3136 | __func__, pwr_info->gear_rx); |
| 3137 | return -EINVAL; |
| 3138 | } |
subhashj@codeaurora.org | 2349b53 | 2016-11-23 16:33:19 -0800 | [diff] [blame] | 3139 | pwr_info->pwr_rx = SLOW_MODE; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3140 | } |
| 3141 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3142 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), |
| 3143 | &pwr_info->gear_tx); |
| 3144 | if (!pwr_info->gear_tx) { |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3145 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3146 | &pwr_info->gear_tx); |
| 3147 | if (!pwr_info->gear_tx) { |
| 3148 | dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n", |
| 3149 | __func__, pwr_info->gear_tx); |
| 3150 | return -EINVAL; |
| 3151 | } |
subhashj@codeaurora.org | 2349b53 | 2016-11-23 16:33:19 -0800 | [diff] [blame] | 3152 | pwr_info->pwr_tx = SLOW_MODE; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3153 | } |
| 3154 | |
| 3155 | hba->max_pwr_info.is_valid = true; |
| 3156 | return 0; |
| 3157 | } |
| 3158 | |
| 3159 | static int ufshcd_change_power_mode(struct ufs_hba *hba, |
| 3160 | struct ufs_pa_layer_attr *pwr_mode) |
| 3161 | { |
| 3162 | int ret; |
| 3163 | |
| 3164 | /* if already configured to the requested pwr_mode */ |
| 3165 | if (pwr_mode->gear_rx == hba->pwr_info.gear_rx && |
| 3166 | pwr_mode->gear_tx == hba->pwr_info.gear_tx && |
| 3167 | pwr_mode->lane_rx == hba->pwr_info.lane_rx && |
| 3168 | pwr_mode->lane_tx == hba->pwr_info.lane_tx && |
| 3169 | pwr_mode->pwr_rx == hba->pwr_info.pwr_rx && |
| 3170 | pwr_mode->pwr_tx == hba->pwr_info.pwr_tx && |
| 3171 | pwr_mode->hs_rate == hba->pwr_info.hs_rate) { |
| 3172 | dev_dbg(hba->dev, "%s: power already configured\n", __func__); |
| 3173 | return 0; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3174 | } |
| 3175 | |
| 3176 | /* |
| 3177 | * Configure attributes for power mode change with below. |
| 3178 | * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION, |
| 3179 | * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION, |
| 3180 | * - PA_HSSERIES |
| 3181 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3182 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx); |
| 3183 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES), |
| 3184 | pwr_mode->lane_rx); |
| 3185 | if (pwr_mode->pwr_rx == FASTAUTO_MODE || |
| 3186 | pwr_mode->pwr_rx == FAST_MODE) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3187 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3188 | else |
| 3189 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3190 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3191 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx); |
| 3192 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES), |
| 3193 | pwr_mode->lane_tx); |
| 3194 | if (pwr_mode->pwr_tx == FASTAUTO_MODE || |
| 3195 | pwr_mode->pwr_tx == FAST_MODE) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3196 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3197 | else |
| 3198 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3199 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3200 | if (pwr_mode->pwr_rx == FASTAUTO_MODE || |
| 3201 | pwr_mode->pwr_tx == FASTAUTO_MODE || |
| 3202 | pwr_mode->pwr_rx == FAST_MODE || |
| 3203 | pwr_mode->pwr_tx == FAST_MODE) |
| 3204 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES), |
| 3205 | pwr_mode->hs_rate); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3206 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3207 | ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4 |
| 3208 | | pwr_mode->pwr_tx); |
| 3209 | |
| 3210 | if (ret) { |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3211 | dev_err(hba->dev, |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3212 | "%s: power mode change failed %d\n", __func__, ret); |
| 3213 | } else { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 3214 | ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL, |
| 3215 | pwr_mode); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3216 | |
| 3217 | memcpy(&hba->pwr_info, pwr_mode, |
| 3218 | sizeof(struct ufs_pa_layer_attr)); |
| 3219 | } |
| 3220 | |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 3221 | ufshcd_print_pwr_info(hba); |
| 3222 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3223 | return ret; |
| 3224 | } |
| 3225 | |
| 3226 | /** |
| 3227 | * ufshcd_config_pwr_mode - configure a new power mode |
| 3228 | * @hba: per-adapter instance |
| 3229 | * @desired_pwr_mode: desired power configuration |
| 3230 | */ |
| 3231 | static int ufshcd_config_pwr_mode(struct ufs_hba *hba, |
| 3232 | struct ufs_pa_layer_attr *desired_pwr_mode) |
| 3233 | { |
| 3234 | struct ufs_pa_layer_attr final_params = { 0 }; |
| 3235 | int ret; |
| 3236 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 3237 | ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE, |
| 3238 | desired_pwr_mode, &final_params); |
| 3239 | |
| 3240 | if (ret) |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3241 | memcpy(&final_params, desired_pwr_mode, sizeof(final_params)); |
| 3242 | |
| 3243 | ret = ufshcd_change_power_mode(hba, &final_params); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3244 | |
| 3245 | return ret; |
| 3246 | } |
| 3247 | |
| 3248 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3249 | * ufshcd_complete_dev_init() - checks device readiness |
| 3250 | * hba: per-adapter instance |
| 3251 | * |
| 3252 | * Set fDeviceInit flag and poll until device toggles it. |
| 3253 | */ |
| 3254 | static int ufshcd_complete_dev_init(struct ufs_hba *hba) |
| 3255 | { |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 3256 | int i; |
| 3257 | int err; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3258 | bool flag_res = 1; |
| 3259 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 3260 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG, |
| 3261 | QUERY_FLAG_IDN_FDEVICEINIT, NULL); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3262 | if (err) { |
| 3263 | dev_err(hba->dev, |
| 3264 | "%s setting fDeviceInit flag failed with error %d\n", |
| 3265 | __func__, err); |
| 3266 | goto out; |
| 3267 | } |
| 3268 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 3269 | /* poll for max. 1000 iterations for fDeviceInit flag to clear */ |
| 3270 | for (i = 0; i < 1000 && !err && flag_res; i++) |
| 3271 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG, |
| 3272 | QUERY_FLAG_IDN_FDEVICEINIT, &flag_res); |
| 3273 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3274 | if (err) |
| 3275 | dev_err(hba->dev, |
| 3276 | "%s reading fDeviceInit flag failed with error %d\n", |
| 3277 | __func__, err); |
| 3278 | else if (flag_res) |
| 3279 | dev_err(hba->dev, |
| 3280 | "%s fDeviceInit was not cleared by the device\n", |
| 3281 | __func__); |
| 3282 | |
| 3283 | out: |
| 3284 | return err; |
| 3285 | } |
| 3286 | |
| 3287 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3288 | * ufshcd_make_hba_operational - Make UFS controller operational |
| 3289 | * @hba: per adapter instance |
| 3290 | * |
| 3291 | * To bring UFS host controller to operational state, |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 3292 | * 1. Enable required interrupts |
| 3293 | * 2. Configure interrupt aggregation |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 3294 | * 3. Program UTRL and UTMRL base address |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 3295 | * 4. Configure run-stop-registers |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3296 | * |
| 3297 | * Returns 0 on success, non-zero value on failure |
| 3298 | */ |
| 3299 | static int ufshcd_make_hba_operational(struct ufs_hba *hba) |
| 3300 | { |
| 3301 | int err = 0; |
| 3302 | u32 reg; |
| 3303 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3304 | /* Enable required interrupts */ |
| 3305 | ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS); |
| 3306 | |
| 3307 | /* Configure interrupt aggregation */ |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 3308 | if (ufshcd_is_intr_aggr_allowed(hba)) |
| 3309 | ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO); |
| 3310 | else |
| 3311 | ufshcd_disable_intr_aggr(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3312 | |
| 3313 | /* Configure UTRL and UTMRL base address registers */ |
| 3314 | ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr), |
| 3315 | REG_UTP_TRANSFER_REQ_LIST_BASE_L); |
| 3316 | ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr), |
| 3317 | REG_UTP_TRANSFER_REQ_LIST_BASE_H); |
| 3318 | ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr), |
| 3319 | REG_UTP_TASK_REQ_LIST_BASE_L); |
| 3320 | ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr), |
| 3321 | REG_UTP_TASK_REQ_LIST_BASE_H); |
| 3322 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3323 | /* |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 3324 | * Make sure base address and interrupt setup are updated before |
| 3325 | * enabling the run/stop registers below. |
| 3326 | */ |
| 3327 | wmb(); |
| 3328 | |
| 3329 | /* |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3330 | * UCRDY, UTMRLDY and UTRLRDY bits must be 1 |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3331 | */ |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 3332 | reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3333 | if (!(ufshcd_get_lists_status(reg))) { |
| 3334 | ufshcd_enable_run_stop_reg(hba); |
| 3335 | } else { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3336 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3337 | "Host controller not ready to process requests"); |
| 3338 | err = -EIO; |
| 3339 | goto out; |
| 3340 | } |
| 3341 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3342 | out: |
| 3343 | return err; |
| 3344 | } |
| 3345 | |
| 3346 | /** |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 3347 | * ufshcd_hba_stop - Send controller to reset state |
| 3348 | * @hba: per adapter instance |
| 3349 | * @can_sleep: perform sleep or just spin |
| 3350 | */ |
| 3351 | static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep) |
| 3352 | { |
| 3353 | int err; |
| 3354 | |
| 3355 | ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE); |
| 3356 | err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE, |
| 3357 | CONTROLLER_ENABLE, CONTROLLER_DISABLE, |
| 3358 | 10, 1, can_sleep); |
| 3359 | if (err) |
| 3360 | dev_err(hba->dev, "%s: Controller disable failed\n", __func__); |
| 3361 | } |
| 3362 | |
| 3363 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3364 | * ufshcd_hba_enable - initialize the controller |
| 3365 | * @hba: per adapter instance |
| 3366 | * |
| 3367 | * The controller resets itself and controller firmware initialization |
| 3368 | * sequence kicks off. When controller is ready it will set |
| 3369 | * the Host Controller Enable bit to 1. |
| 3370 | * |
| 3371 | * Returns 0 on success, non-zero value on failure |
| 3372 | */ |
| 3373 | static int ufshcd_hba_enable(struct ufs_hba *hba) |
| 3374 | { |
| 3375 | int retry; |
| 3376 | |
| 3377 | /* |
| 3378 | * msleep of 1 and 5 used in this function might result in msleep(20), |
| 3379 | * but it was necessary to send the UFS FPGA to reset mode during |
| 3380 | * development and testing of this driver. msleep can be changed to |
| 3381 | * mdelay and retry count can be reduced based on the controller. |
| 3382 | */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 3383 | if (!ufshcd_is_hba_active(hba)) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3384 | /* change controller state to "reset state" */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 3385 | ufshcd_hba_stop(hba, true); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3386 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3387 | /* UniPro link is disabled at this point */ |
| 3388 | ufshcd_set_link_off(hba); |
| 3389 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 3390 | ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 3391 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3392 | /* start controller initialization sequence */ |
| 3393 | ufshcd_hba_start(hba); |
| 3394 | |
| 3395 | /* |
| 3396 | * To initialize a UFS host controller HCE bit must be set to 1. |
| 3397 | * During initialization the HCE bit value changes from 1->0->1. |
| 3398 | * When the host controller completes initialization sequence |
| 3399 | * it sets the value of HCE bit to 1. The same HCE bit is read back |
| 3400 | * to check if the controller has completed initialization sequence. |
| 3401 | * So without this delay the value HCE = 1, set in the previous |
| 3402 | * instruction might be read back. |
| 3403 | * This delay can be changed based on the controller. |
| 3404 | */ |
| 3405 | msleep(1); |
| 3406 | |
| 3407 | /* wait for the host controller to complete initialization */ |
| 3408 | retry = 10; |
| 3409 | while (ufshcd_is_hba_active(hba)) { |
| 3410 | if (retry) { |
| 3411 | retry--; |
| 3412 | } else { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3413 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3414 | "Controller enable failed\n"); |
| 3415 | return -EIO; |
| 3416 | } |
| 3417 | msleep(5); |
| 3418 | } |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 3419 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 3420 | /* enable UIC related interrupts */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3421 | ufshcd_enable_intr(hba, UFSHCD_UIC_MASK); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 3422 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 3423 | ufshcd_vops_hce_enable_notify(hba, POST_CHANGE); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 3424 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3425 | return 0; |
| 3426 | } |
| 3427 | |
Yaniv Gardi | 7ca38cf | 2015-05-17 18:54:59 +0300 | [diff] [blame] | 3428 | static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer) |
| 3429 | { |
| 3430 | int tx_lanes, i, err = 0; |
| 3431 | |
| 3432 | if (!peer) |
| 3433 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 3434 | &tx_lanes); |
| 3435 | else |
| 3436 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 3437 | &tx_lanes); |
| 3438 | for (i = 0; i < tx_lanes; i++) { |
| 3439 | if (!peer) |
| 3440 | err = ufshcd_dme_set(hba, |
| 3441 | UIC_ARG_MIB_SEL(TX_LCC_ENABLE, |
| 3442 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)), |
| 3443 | 0); |
| 3444 | else |
| 3445 | err = ufshcd_dme_peer_set(hba, |
| 3446 | UIC_ARG_MIB_SEL(TX_LCC_ENABLE, |
| 3447 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)), |
| 3448 | 0); |
| 3449 | if (err) { |
| 3450 | dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d", |
| 3451 | __func__, peer, i, err); |
| 3452 | break; |
| 3453 | } |
| 3454 | } |
| 3455 | |
| 3456 | return err; |
| 3457 | } |
| 3458 | |
| 3459 | static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba) |
| 3460 | { |
| 3461 | return ufshcd_disable_tx_lcc(hba, true); |
| 3462 | } |
| 3463 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3464 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3465 | * ufshcd_link_startup - Initialize unipro link startup |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3466 | * @hba: per adapter instance |
| 3467 | * |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3468 | * Returns 0 for success, non-zero in case of failure |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3469 | */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3470 | static int ufshcd_link_startup(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3471 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3472 | int ret; |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 3473 | int retries = DME_LINKSTARTUP_RETRIES; |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 3474 | bool link_startup_again = false; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3475 | |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 3476 | /* |
| 3477 | * If UFS device isn't active then we will have to issue link startup |
| 3478 | * 2 times to make sure the device state move to active. |
| 3479 | */ |
| 3480 | if (!ufshcd_is_ufs_dev_active(hba)) |
| 3481 | link_startup_again = true; |
| 3482 | |
| 3483 | link_startup: |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 3484 | do { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 3485 | ufshcd_vops_link_startup_notify(hba, PRE_CHANGE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3486 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 3487 | ret = ufshcd_dme_link_startup(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 3488 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 3489 | /* check if device is detected by inter-connect layer */ |
| 3490 | if (!ret && !ufshcd_is_device_present(hba)) { |
| 3491 | dev_err(hba->dev, "%s: Device not present\n", __func__); |
| 3492 | ret = -ENXIO; |
| 3493 | goto out; |
| 3494 | } |
| 3495 | |
| 3496 | /* |
| 3497 | * DME link lost indication is only received when link is up, |
| 3498 | * but we can't be sure if the link is up until link startup |
| 3499 | * succeeds. So reset the local Uni-Pro and try again. |
| 3500 | */ |
| 3501 | if (ret && ufshcd_hba_enable(hba)) |
| 3502 | goto out; |
| 3503 | } while (ret && retries--); |
| 3504 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3505 | if (ret) |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 3506 | /* failed to get the link up... retire */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3507 | goto out; |
| 3508 | |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 3509 | if (link_startup_again) { |
| 3510 | link_startup_again = false; |
| 3511 | retries = DME_LINKSTARTUP_RETRIES; |
| 3512 | goto link_startup; |
| 3513 | } |
| 3514 | |
subhashj@codeaurora.org | d2aebb9 | 2016-12-22 18:41:33 -0800 | [diff] [blame^] | 3515 | /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */ |
| 3516 | ufshcd_init_pwr_info(hba); |
| 3517 | ufshcd_print_pwr_info(hba); |
| 3518 | |
Yaniv Gardi | 7ca38cf | 2015-05-17 18:54:59 +0300 | [diff] [blame] | 3519 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) { |
| 3520 | ret = ufshcd_disable_device_tx_lcc(hba); |
| 3521 | if (ret) |
| 3522 | goto out; |
| 3523 | } |
| 3524 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 3525 | /* Include any host controller configuration via UIC commands */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 3526 | ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE); |
| 3527 | if (ret) |
| 3528 | goto out; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 3529 | |
| 3530 | ret = ufshcd_make_hba_operational(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3531 | out: |
| 3532 | if (ret) |
| 3533 | dev_err(hba->dev, "link startup failed %d\n", ret); |
| 3534 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3535 | } |
| 3536 | |
| 3537 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3538 | * ufshcd_verify_dev_init() - Verify device initialization |
| 3539 | * @hba: per-adapter instance |
| 3540 | * |
| 3541 | * Send NOP OUT UPIU and wait for NOP IN response to check whether the |
| 3542 | * device Transport Protocol (UTP) layer is ready after a reset. |
| 3543 | * If the UTP layer at the device side is not initialized, it may |
| 3544 | * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT |
| 3545 | * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations. |
| 3546 | */ |
| 3547 | static int ufshcd_verify_dev_init(struct ufs_hba *hba) |
| 3548 | { |
| 3549 | int err = 0; |
| 3550 | int retries; |
| 3551 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3552 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3553 | mutex_lock(&hba->dev_cmd.lock); |
| 3554 | for (retries = NOP_OUT_RETRIES; retries > 0; retries--) { |
| 3555 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP, |
| 3556 | NOP_OUT_TIMEOUT); |
| 3557 | |
| 3558 | if (!err || err == -ETIMEDOUT) |
| 3559 | break; |
| 3560 | |
| 3561 | dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err); |
| 3562 | } |
| 3563 | mutex_unlock(&hba->dev_cmd.lock); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3564 | ufshcd_release(hba); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3565 | |
| 3566 | if (err) |
| 3567 | dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err); |
| 3568 | return err; |
| 3569 | } |
| 3570 | |
| 3571 | /** |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 3572 | * ufshcd_set_queue_depth - set lun queue depth |
| 3573 | * @sdev: pointer to SCSI device |
| 3574 | * |
| 3575 | * Read bLUQueueDepth value and activate scsi tagged command |
| 3576 | * queueing. For WLUN, queue depth is set to 1. For best-effort |
| 3577 | * cases (bLUQueueDepth = 0) the queue depth is set to a maximum |
| 3578 | * value that host can queue. |
| 3579 | */ |
| 3580 | static void ufshcd_set_queue_depth(struct scsi_device *sdev) |
| 3581 | { |
| 3582 | int ret = 0; |
| 3583 | u8 lun_qdepth; |
Dolev Raviv | 61e0735 | 2016-11-23 16:30:49 -0800 | [diff] [blame] | 3584 | int retries; |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 3585 | struct ufs_hba *hba; |
| 3586 | |
| 3587 | hba = shost_priv(sdev->host); |
| 3588 | |
| 3589 | lun_qdepth = hba->nutrs; |
Dolev Raviv | 61e0735 | 2016-11-23 16:30:49 -0800 | [diff] [blame] | 3590 | for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { |
| 3591 | /* Read descriptor*/ |
| 3592 | ret = ufshcd_read_unit_desc_param(hba, |
| 3593 | ufshcd_scsi_to_upiu_lun(sdev->lun), |
| 3594 | UNIT_DESC_PARAM_LU_Q_DEPTH, |
| 3595 | &lun_qdepth, |
| 3596 | sizeof(lun_qdepth)); |
| 3597 | if (!ret || ret == -ENOTSUPP) |
| 3598 | break; |
| 3599 | |
| 3600 | dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, ret); |
| 3601 | } |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 3602 | |
| 3603 | /* Some WLUN doesn't support unit descriptor */ |
| 3604 | if (ret == -EOPNOTSUPP) |
| 3605 | lun_qdepth = 1; |
| 3606 | else if (!lun_qdepth) |
| 3607 | /* eventually, we can figure out the real queue depth */ |
| 3608 | lun_qdepth = hba->nutrs; |
| 3609 | else |
| 3610 | lun_qdepth = min_t(int, lun_qdepth, hba->nutrs); |
| 3611 | |
| 3612 | dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n", |
| 3613 | __func__, lun_qdepth); |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 3614 | scsi_change_queue_depth(sdev, lun_qdepth); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 3615 | } |
| 3616 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3617 | /* |
| 3618 | * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR |
| 3619 | * @hba: per-adapter instance |
| 3620 | * @lun: UFS device lun id |
| 3621 | * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info |
| 3622 | * |
| 3623 | * Returns 0 in case of success and b_lu_write_protect status would be returned |
| 3624 | * @b_lu_write_protect parameter. |
| 3625 | * Returns -ENOTSUPP if reading b_lu_write_protect is not supported. |
| 3626 | * Returns -EINVAL in case of invalid parameters passed to this function. |
| 3627 | */ |
| 3628 | static int ufshcd_get_lu_wp(struct ufs_hba *hba, |
| 3629 | u8 lun, |
| 3630 | u8 *b_lu_write_protect) |
| 3631 | { |
| 3632 | int ret; |
| 3633 | |
| 3634 | if (!b_lu_write_protect) |
| 3635 | ret = -EINVAL; |
| 3636 | /* |
| 3637 | * According to UFS device spec, RPMB LU can't be write |
| 3638 | * protected so skip reading bLUWriteProtect parameter for |
| 3639 | * it. For other W-LUs, UNIT DESCRIPTOR is not available. |
| 3640 | */ |
| 3641 | else if (lun >= UFS_UPIU_MAX_GENERAL_LUN) |
| 3642 | ret = -ENOTSUPP; |
| 3643 | else |
| 3644 | ret = ufshcd_read_unit_desc_param(hba, |
| 3645 | lun, |
| 3646 | UNIT_DESC_PARAM_LU_WR_PROTECT, |
| 3647 | b_lu_write_protect, |
| 3648 | sizeof(*b_lu_write_protect)); |
| 3649 | return ret; |
| 3650 | } |
| 3651 | |
| 3652 | /** |
| 3653 | * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect |
| 3654 | * status |
| 3655 | * @hba: per-adapter instance |
| 3656 | * @sdev: pointer to SCSI device |
| 3657 | * |
| 3658 | */ |
| 3659 | static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba, |
| 3660 | struct scsi_device *sdev) |
| 3661 | { |
| 3662 | if (hba->dev_info.f_power_on_wp_en && |
| 3663 | !hba->dev_info.is_lu_power_on_wp) { |
| 3664 | u8 b_lu_write_protect; |
| 3665 | |
| 3666 | if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun), |
| 3667 | &b_lu_write_protect) && |
| 3668 | (b_lu_write_protect == UFS_LU_POWER_ON_WP)) |
| 3669 | hba->dev_info.is_lu_power_on_wp = true; |
| 3670 | } |
| 3671 | } |
| 3672 | |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 3673 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3674 | * ufshcd_slave_alloc - handle initial SCSI device configurations |
| 3675 | * @sdev: pointer to SCSI device |
| 3676 | * |
| 3677 | * Returns success |
| 3678 | */ |
| 3679 | static int ufshcd_slave_alloc(struct scsi_device *sdev) |
| 3680 | { |
| 3681 | struct ufs_hba *hba; |
| 3682 | |
| 3683 | hba = shost_priv(sdev->host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3684 | |
| 3685 | /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */ |
| 3686 | sdev->use_10_for_ms = 1; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3687 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 3688 | /* allow SCSI layer to restart the device in case of errors */ |
| 3689 | sdev->allow_restart = 1; |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 3690 | |
Sujit Reddy Thumma | b2a6c52 | 2014-07-01 12:22:38 +0300 | [diff] [blame] | 3691 | /* REPORT SUPPORTED OPERATION CODES is not supported */ |
| 3692 | sdev->no_report_opcodes = 1; |
| 3693 | |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 3694 | |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 3695 | ufshcd_set_queue_depth(sdev); |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 3696 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3697 | ufshcd_get_lu_power_on_wp_status(hba, sdev); |
| 3698 | |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 3699 | return 0; |
| 3700 | } |
| 3701 | |
| 3702 | /** |
| 3703 | * ufshcd_change_queue_depth - change queue depth |
| 3704 | * @sdev: pointer to SCSI device |
| 3705 | * @depth: required depth to set |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 3706 | * |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 3707 | * Change queue depth and make sure the max. limits are not crossed. |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 3708 | */ |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 3709 | static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth) |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 3710 | { |
| 3711 | struct ufs_hba *hba = shost_priv(sdev->host); |
| 3712 | |
| 3713 | if (depth > hba->nutrs) |
| 3714 | depth = hba->nutrs; |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 3715 | return scsi_change_queue_depth(sdev, depth); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3716 | } |
| 3717 | |
| 3718 | /** |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 3719 | * ufshcd_slave_configure - adjust SCSI device configurations |
| 3720 | * @sdev: pointer to SCSI device |
| 3721 | */ |
| 3722 | static int ufshcd_slave_configure(struct scsi_device *sdev) |
| 3723 | { |
| 3724 | struct request_queue *q = sdev->request_queue; |
| 3725 | |
| 3726 | blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1); |
| 3727 | blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX); |
| 3728 | |
| 3729 | return 0; |
| 3730 | } |
| 3731 | |
| 3732 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3733 | * ufshcd_slave_destroy - remove SCSI device configurations |
| 3734 | * @sdev: pointer to SCSI device |
| 3735 | */ |
| 3736 | static void ufshcd_slave_destroy(struct scsi_device *sdev) |
| 3737 | { |
| 3738 | struct ufs_hba *hba; |
| 3739 | |
| 3740 | hba = shost_priv(sdev->host); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 3741 | /* Drop the reference as it won't be needed anymore */ |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 3742 | if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) { |
| 3743 | unsigned long flags; |
| 3744 | |
| 3745 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 3746 | hba->sdev_ufs_device = NULL; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 3747 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3748 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3749 | } |
| 3750 | |
| 3751 | /** |
| 3752 | * ufshcd_task_req_compl - handle task management request completion |
| 3753 | * @hba: per adapter instance |
| 3754 | * @index: index of the completed request |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 3755 | * @resp: task management service response |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3756 | * |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 3757 | * Returns non-zero value on error, zero on success |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3758 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 3759 | 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] | 3760 | { |
| 3761 | struct utp_task_req_desc *task_req_descp; |
| 3762 | struct utp_upiu_task_rsp *task_rsp_upiup; |
| 3763 | unsigned long flags; |
| 3764 | int ocs_value; |
| 3765 | int task_result; |
| 3766 | |
| 3767 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 3768 | |
| 3769 | /* Clear completed tasks from outstanding_tasks */ |
| 3770 | __clear_bit(index, &hba->outstanding_tasks); |
| 3771 | |
| 3772 | task_req_descp = hba->utmrdl_base_addr; |
| 3773 | ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]); |
| 3774 | |
| 3775 | if (ocs_value == OCS_SUCCESS) { |
| 3776 | task_rsp_upiup = (struct utp_upiu_task_rsp *) |
| 3777 | task_req_descp[index].task_rsp_upiu; |
Kiwoong Kim | 8794ee0 | 2016-09-09 08:22:22 +0900 | [diff] [blame] | 3778 | task_result = be32_to_cpu(task_rsp_upiup->output_param1); |
| 3779 | task_result = task_result & MASK_TM_SERVICE_RESP; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 3780 | if (resp) |
| 3781 | *resp = (u8)task_result; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3782 | } else { |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 3783 | dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", |
| 3784 | __func__, ocs_value); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3785 | } |
| 3786 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 3787 | |
| 3788 | return ocs_value; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3789 | } |
| 3790 | |
| 3791 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3792 | * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status |
| 3793 | * @lrb: pointer to local reference block of completed command |
| 3794 | * @scsi_status: SCSI command status |
| 3795 | * |
| 3796 | * Returns value base on SCSI command status |
| 3797 | */ |
| 3798 | static inline int |
| 3799 | ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status) |
| 3800 | { |
| 3801 | int result = 0; |
| 3802 | |
| 3803 | switch (scsi_status) { |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 3804 | case SAM_STAT_CHECK_CONDITION: |
| 3805 | ufshcd_copy_sense_data(lrbp); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3806 | case SAM_STAT_GOOD: |
| 3807 | result |= DID_OK << 16 | |
| 3808 | COMMAND_COMPLETE << 8 | |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 3809 | scsi_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3810 | break; |
| 3811 | case SAM_STAT_TASK_SET_FULL: |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 3812 | case SAM_STAT_BUSY: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3813 | case SAM_STAT_TASK_ABORTED: |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 3814 | ufshcd_copy_sense_data(lrbp); |
| 3815 | result |= scsi_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3816 | break; |
| 3817 | default: |
| 3818 | result |= DID_ERROR << 16; |
| 3819 | break; |
| 3820 | } /* end of switch */ |
| 3821 | |
| 3822 | return result; |
| 3823 | } |
| 3824 | |
| 3825 | /** |
| 3826 | * ufshcd_transfer_rsp_status - Get overall status of the response |
| 3827 | * @hba: per adapter instance |
| 3828 | * @lrb: pointer to local reference block of completed command |
| 3829 | * |
| 3830 | * Returns result of the command to notify SCSI midlayer |
| 3831 | */ |
| 3832 | static inline int |
| 3833 | ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 3834 | { |
| 3835 | int result = 0; |
| 3836 | int scsi_status; |
| 3837 | int ocs; |
| 3838 | |
| 3839 | /* overall command status of utrd */ |
| 3840 | ocs = ufshcd_get_tr_ocs(lrbp); |
| 3841 | |
| 3842 | switch (ocs) { |
| 3843 | case OCS_SUCCESS: |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3844 | result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3845 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3846 | switch (result) { |
| 3847 | case UPIU_TRANSACTION_RESPONSE: |
| 3848 | /* |
| 3849 | * get the response UPIU result to extract |
| 3850 | * the SCSI command status |
| 3851 | */ |
| 3852 | result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr); |
| 3853 | |
| 3854 | /* |
| 3855 | * get the result based on SCSI status response |
| 3856 | * to notify the SCSI midlayer of the command status |
| 3857 | */ |
| 3858 | scsi_status = result & MASK_SCSI_STATUS; |
| 3859 | result = ufshcd_scsi_cmd_status(lrbp, scsi_status); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3860 | |
Yaniv Gardi | f05ac2e | 2016-02-01 15:02:42 +0200 | [diff] [blame] | 3861 | /* |
| 3862 | * Currently we are only supporting BKOPs exception |
| 3863 | * events hence we can ignore BKOPs exception event |
| 3864 | * during power management callbacks. BKOPs exception |
| 3865 | * event is not expected to be raised in runtime suspend |
| 3866 | * callback as it allows the urgent bkops. |
| 3867 | * During system suspend, we are anyway forcefully |
| 3868 | * disabling the bkops and if urgent bkops is needed |
| 3869 | * it will be enabled on system resume. Long term |
| 3870 | * solution could be to abort the system suspend if |
| 3871 | * UFS device needs urgent BKOPs. |
| 3872 | */ |
| 3873 | if (!hba->pm_op_in_progress && |
| 3874 | ufshcd_is_exception_event(lrbp->ucd_rsp_ptr)) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3875 | schedule_work(&hba->eeh_work); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3876 | break; |
| 3877 | case UPIU_TRANSACTION_REJECT_UPIU: |
| 3878 | /* TODO: handle Reject UPIU Response */ |
| 3879 | result = DID_ERROR << 16; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3880 | dev_err(hba->dev, |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3881 | "Reject UPIU not fully implemented\n"); |
| 3882 | break; |
| 3883 | default: |
| 3884 | result = DID_ERROR << 16; |
| 3885 | dev_err(hba->dev, |
| 3886 | "Unexpected request response code = %x\n", |
| 3887 | result); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3888 | break; |
| 3889 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3890 | break; |
| 3891 | case OCS_ABORTED: |
| 3892 | result |= DID_ABORT << 16; |
| 3893 | break; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 3894 | case OCS_INVALID_COMMAND_STATUS: |
| 3895 | result |= DID_REQUEUE << 16; |
| 3896 | break; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3897 | case OCS_INVALID_CMD_TABLE_ATTR: |
| 3898 | case OCS_INVALID_PRDT_ATTR: |
| 3899 | case OCS_MISMATCH_DATA_BUF_SIZE: |
| 3900 | case OCS_MISMATCH_RESP_UPIU_SIZE: |
| 3901 | case OCS_PEER_COMM_FAILURE: |
| 3902 | case OCS_FATAL_ERROR: |
| 3903 | default: |
| 3904 | result |= DID_ERROR << 16; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3905 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3906 | "OCS error from controller = %x\n", ocs); |
| 3907 | break; |
| 3908 | } /* end of switch */ |
| 3909 | |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 3910 | if (host_byte(result) != DID_OK) |
| 3911 | ufshcd_print_trs(hba, 1 << lrbp->task_tag, true); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3912 | return result; |
| 3913 | } |
| 3914 | |
| 3915 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3916 | * ufshcd_uic_cmd_compl - handle completion of uic command |
| 3917 | * @hba: per adapter instance |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3918 | * @intr_status: interrupt status generated by the controller |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3919 | */ |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3920 | 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] | 3921 | { |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3922 | if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3923 | hba->active_uic_cmd->argument2 |= |
| 3924 | ufshcd_get_uic_cmd_result(hba); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3925 | hba->active_uic_cmd->argument3 = |
| 3926 | ufshcd_get_dme_attr_val(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3927 | complete(&hba->active_uic_cmd->done); |
| 3928 | } |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3929 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3930 | if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done) |
| 3931 | complete(hba->uic_async_done); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3932 | } |
| 3933 | |
| 3934 | /** |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 3935 | * __ufshcd_transfer_req_compl - handle SCSI and query command completion |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3936 | * @hba: per adapter instance |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 3937 | * @completed_reqs: requests to complete |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3938 | */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 3939 | static void __ufshcd_transfer_req_compl(struct ufs_hba *hba, |
| 3940 | unsigned long completed_reqs) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3941 | { |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3942 | struct ufshcd_lrb *lrbp; |
| 3943 | struct scsi_cmnd *cmd; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3944 | int result; |
| 3945 | int index; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 3946 | |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 3947 | for_each_set_bit(index, &completed_reqs, hba->nutrs) { |
| 3948 | lrbp = &hba->lrb[index]; |
| 3949 | cmd = lrbp->cmd; |
| 3950 | if (cmd) { |
| 3951 | result = ufshcd_transfer_rsp_status(hba, lrbp); |
| 3952 | scsi_dma_unmap(cmd); |
| 3953 | cmd->result = result; |
| 3954 | /* Mark completed command as NULL in LRB */ |
| 3955 | lrbp->cmd = NULL; |
| 3956 | clear_bit_unlock(index, &hba->lrb_in_use); |
| 3957 | /* Do not touch lrbp after scsi done */ |
| 3958 | cmd->scsi_done(cmd); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3959 | __ufshcd_release(hba); |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 3960 | } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE || |
| 3961 | lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) { |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 3962 | if (hba->dev_cmd.complete) |
| 3963 | complete(hba->dev_cmd.complete); |
| 3964 | } |
| 3965 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3966 | |
| 3967 | /* clear corresponding bits of completed commands */ |
| 3968 | hba->outstanding_reqs ^= completed_reqs; |
| 3969 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 3970 | ufshcd_clk_scaling_update_busy(hba); |
| 3971 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3972 | /* we might have free'd some tags above */ |
| 3973 | wake_up(&hba->dev_cmd.tag_wq); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3974 | } |
| 3975 | |
| 3976 | /** |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 3977 | * ufshcd_transfer_req_compl - handle SCSI and query command completion |
| 3978 | * @hba: per adapter instance |
| 3979 | */ |
| 3980 | static void ufshcd_transfer_req_compl(struct ufs_hba *hba) |
| 3981 | { |
| 3982 | unsigned long completed_reqs; |
| 3983 | u32 tr_doorbell; |
| 3984 | |
| 3985 | /* Resetting interrupt aggregation counters first and reading the |
| 3986 | * DOOR_BELL afterward allows us to handle all the completed requests. |
| 3987 | * In order to prevent other interrupts starvation the DB is read once |
| 3988 | * after reset. The down side of this solution is the possibility of |
| 3989 | * false interrupt if device completes another request after resetting |
| 3990 | * aggregation and before reading the DB. |
| 3991 | */ |
| 3992 | if (ufshcd_is_intr_aggr_allowed(hba)) |
| 3993 | ufshcd_reset_intr_aggr(hba); |
| 3994 | |
| 3995 | tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 3996 | completed_reqs = tr_doorbell ^ hba->outstanding_reqs; |
| 3997 | |
| 3998 | __ufshcd_transfer_req_compl(hba, completed_reqs); |
| 3999 | } |
| 4000 | |
| 4001 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4002 | * ufshcd_disable_ee - disable exception event |
| 4003 | * @hba: per-adapter instance |
| 4004 | * @mask: exception event to disable |
| 4005 | * |
| 4006 | * Disables exception event in the device so that the EVENT_ALERT |
| 4007 | * bit is not set. |
| 4008 | * |
| 4009 | * Returns zero on success, non-zero error value on failure. |
| 4010 | */ |
| 4011 | static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask) |
| 4012 | { |
| 4013 | int err = 0; |
| 4014 | u32 val; |
| 4015 | |
| 4016 | if (!(hba->ee_ctrl_mask & mask)) |
| 4017 | goto out; |
| 4018 | |
| 4019 | val = hba->ee_ctrl_mask & ~mask; |
| 4020 | val &= 0xFFFF; /* 2 bytes */ |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 4021 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4022 | QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val); |
| 4023 | if (!err) |
| 4024 | hba->ee_ctrl_mask &= ~mask; |
| 4025 | out: |
| 4026 | return err; |
| 4027 | } |
| 4028 | |
| 4029 | /** |
| 4030 | * ufshcd_enable_ee - enable exception event |
| 4031 | * @hba: per-adapter instance |
| 4032 | * @mask: exception event to enable |
| 4033 | * |
| 4034 | * Enable corresponding exception event in the device to allow |
| 4035 | * device to alert host in critical scenarios. |
| 4036 | * |
| 4037 | * Returns zero on success, non-zero error value on failure. |
| 4038 | */ |
| 4039 | static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask) |
| 4040 | { |
| 4041 | int err = 0; |
| 4042 | u32 val; |
| 4043 | |
| 4044 | if (hba->ee_ctrl_mask & mask) |
| 4045 | goto out; |
| 4046 | |
| 4047 | val = hba->ee_ctrl_mask | mask; |
| 4048 | val &= 0xFFFF; /* 2 bytes */ |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 4049 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4050 | QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val); |
| 4051 | if (!err) |
| 4052 | hba->ee_ctrl_mask |= mask; |
| 4053 | out: |
| 4054 | return err; |
| 4055 | } |
| 4056 | |
| 4057 | /** |
| 4058 | * ufshcd_enable_auto_bkops - Allow device managed BKOPS |
| 4059 | * @hba: per-adapter instance |
| 4060 | * |
| 4061 | * Allow device to manage background operations on its own. Enabling |
| 4062 | * this might lead to inconsistent latencies during normal data transfers |
| 4063 | * as the device is allowed to manage its own way of handling background |
| 4064 | * operations. |
| 4065 | * |
| 4066 | * Returns zero on success, non-zero on failure. |
| 4067 | */ |
| 4068 | static int ufshcd_enable_auto_bkops(struct ufs_hba *hba) |
| 4069 | { |
| 4070 | int err = 0; |
| 4071 | |
| 4072 | if (hba->auto_bkops_enabled) |
| 4073 | goto out; |
| 4074 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 4075 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4076 | QUERY_FLAG_IDN_BKOPS_EN, NULL); |
| 4077 | if (err) { |
| 4078 | dev_err(hba->dev, "%s: failed to enable bkops %d\n", |
| 4079 | __func__, err); |
| 4080 | goto out; |
| 4081 | } |
| 4082 | |
| 4083 | hba->auto_bkops_enabled = true; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 4084 | trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Enabled"); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4085 | |
| 4086 | /* No need of URGENT_BKOPS exception from the device */ |
| 4087 | err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 4088 | if (err) |
| 4089 | dev_err(hba->dev, "%s: failed to disable exception event %d\n", |
| 4090 | __func__, err); |
| 4091 | out: |
| 4092 | return err; |
| 4093 | } |
| 4094 | |
| 4095 | /** |
| 4096 | * ufshcd_disable_auto_bkops - block device in doing background operations |
| 4097 | * @hba: per-adapter instance |
| 4098 | * |
| 4099 | * Disabling background operations improves command response latency but |
| 4100 | * has drawback of device moving into critical state where the device is |
| 4101 | * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the |
| 4102 | * host is idle so that BKOPS are managed effectively without any negative |
| 4103 | * impacts. |
| 4104 | * |
| 4105 | * Returns zero on success, non-zero on failure. |
| 4106 | */ |
| 4107 | static int ufshcd_disable_auto_bkops(struct ufs_hba *hba) |
| 4108 | { |
| 4109 | int err = 0; |
| 4110 | |
| 4111 | if (!hba->auto_bkops_enabled) |
| 4112 | goto out; |
| 4113 | |
| 4114 | /* |
| 4115 | * If host assisted BKOPs is to be enabled, make sure |
| 4116 | * urgent bkops exception is allowed. |
| 4117 | */ |
| 4118 | err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 4119 | if (err) { |
| 4120 | dev_err(hba->dev, "%s: failed to enable exception event %d\n", |
| 4121 | __func__, err); |
| 4122 | goto out; |
| 4123 | } |
| 4124 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 4125 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4126 | QUERY_FLAG_IDN_BKOPS_EN, NULL); |
| 4127 | if (err) { |
| 4128 | dev_err(hba->dev, "%s: failed to disable bkops %d\n", |
| 4129 | __func__, err); |
| 4130 | ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 4131 | goto out; |
| 4132 | } |
| 4133 | |
| 4134 | hba->auto_bkops_enabled = false; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 4135 | trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Disabled"); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4136 | out: |
| 4137 | return err; |
| 4138 | } |
| 4139 | |
| 4140 | /** |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 4141 | * ufshcd_force_reset_auto_bkops - force reset auto bkops state |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4142 | * @hba: per adapter instance |
| 4143 | * |
| 4144 | * After a device reset the device may toggle the BKOPS_EN flag |
| 4145 | * to default value. The s/w tracking variables should be updated |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 4146 | * as well. This function would change the auto-bkops state based on |
| 4147 | * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4148 | */ |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 4149 | static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4150 | { |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 4151 | if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) { |
| 4152 | hba->auto_bkops_enabled = false; |
| 4153 | hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS; |
| 4154 | ufshcd_enable_auto_bkops(hba); |
| 4155 | } else { |
| 4156 | hba->auto_bkops_enabled = true; |
| 4157 | hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS; |
| 4158 | ufshcd_disable_auto_bkops(hba); |
| 4159 | } |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4160 | } |
| 4161 | |
| 4162 | static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status) |
| 4163 | { |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 4164 | return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4165 | QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status); |
| 4166 | } |
| 4167 | |
| 4168 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4169 | * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status |
| 4170 | * @hba: per-adapter instance |
| 4171 | * @status: bkops_status value |
| 4172 | * |
| 4173 | * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn |
| 4174 | * flag in the device to permit background operations if the device |
| 4175 | * bkops_status is greater than or equal to "status" argument passed to |
| 4176 | * this function, disable otherwise. |
| 4177 | * |
| 4178 | * Returns 0 for success, non-zero in case of failure. |
| 4179 | * |
| 4180 | * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag |
| 4181 | * to know whether auto bkops is enabled or disabled after this function |
| 4182 | * returns control to it. |
| 4183 | */ |
| 4184 | static int ufshcd_bkops_ctrl(struct ufs_hba *hba, |
| 4185 | enum bkops_status status) |
| 4186 | { |
| 4187 | int err; |
| 4188 | u32 curr_status = 0; |
| 4189 | |
| 4190 | err = ufshcd_get_bkops_status(hba, &curr_status); |
| 4191 | if (err) { |
| 4192 | dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", |
| 4193 | __func__, err); |
| 4194 | goto out; |
| 4195 | } else if (curr_status > BKOPS_STATUS_MAX) { |
| 4196 | dev_err(hba->dev, "%s: invalid BKOPS status %d\n", |
| 4197 | __func__, curr_status); |
| 4198 | err = -EINVAL; |
| 4199 | goto out; |
| 4200 | } |
| 4201 | |
| 4202 | if (curr_status >= status) |
| 4203 | err = ufshcd_enable_auto_bkops(hba); |
| 4204 | else |
| 4205 | err = ufshcd_disable_auto_bkops(hba); |
| 4206 | out: |
| 4207 | return err; |
| 4208 | } |
| 4209 | |
| 4210 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4211 | * ufshcd_urgent_bkops - handle urgent bkops exception event |
| 4212 | * @hba: per-adapter instance |
| 4213 | * |
| 4214 | * Enable fBackgroundOpsEn flag in the device to permit background |
| 4215 | * operations. |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4216 | * |
| 4217 | * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled |
| 4218 | * and negative error value for any other failure. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4219 | */ |
| 4220 | static int ufshcd_urgent_bkops(struct ufs_hba *hba) |
| 4221 | { |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 4222 | return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4223 | } |
| 4224 | |
| 4225 | static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status) |
| 4226 | { |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 4227 | return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4228 | QUERY_ATTR_IDN_EE_STATUS, 0, 0, status); |
| 4229 | } |
| 4230 | |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 4231 | static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba) |
| 4232 | { |
| 4233 | int err; |
| 4234 | u32 curr_status = 0; |
| 4235 | |
| 4236 | if (hba->is_urgent_bkops_lvl_checked) |
| 4237 | goto enable_auto_bkops; |
| 4238 | |
| 4239 | err = ufshcd_get_bkops_status(hba, &curr_status); |
| 4240 | if (err) { |
| 4241 | dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", |
| 4242 | __func__, err); |
| 4243 | goto out; |
| 4244 | } |
| 4245 | |
| 4246 | /* |
| 4247 | * We are seeing that some devices are raising the urgent bkops |
| 4248 | * exception events even when BKOPS status doesn't indicate performace |
| 4249 | * impacted or critical. Handle these device by determining their urgent |
| 4250 | * bkops status at runtime. |
| 4251 | */ |
| 4252 | if (curr_status < BKOPS_STATUS_PERF_IMPACT) { |
| 4253 | dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n", |
| 4254 | __func__, curr_status); |
| 4255 | /* update the current status as the urgent bkops level */ |
| 4256 | hba->urgent_bkops_lvl = curr_status; |
| 4257 | hba->is_urgent_bkops_lvl_checked = true; |
| 4258 | } |
| 4259 | |
| 4260 | enable_auto_bkops: |
| 4261 | err = ufshcd_enable_auto_bkops(hba); |
| 4262 | out: |
| 4263 | if (err < 0) |
| 4264 | dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n", |
| 4265 | __func__, err); |
| 4266 | } |
| 4267 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4268 | /** |
| 4269 | * ufshcd_exception_event_handler - handle exceptions raised by device |
| 4270 | * @work: pointer to work data |
| 4271 | * |
| 4272 | * Read bExceptionEventStatus attribute from the device and handle the |
| 4273 | * exception event accordingly. |
| 4274 | */ |
| 4275 | static void ufshcd_exception_event_handler(struct work_struct *work) |
| 4276 | { |
| 4277 | struct ufs_hba *hba; |
| 4278 | int err; |
| 4279 | u32 status = 0; |
| 4280 | hba = container_of(work, struct ufs_hba, eeh_work); |
| 4281 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 4282 | pm_runtime_get_sync(hba->dev); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4283 | err = ufshcd_get_ee_status(hba, &status); |
| 4284 | if (err) { |
| 4285 | dev_err(hba->dev, "%s: failed to get exception status %d\n", |
| 4286 | __func__, err); |
| 4287 | goto out; |
| 4288 | } |
| 4289 | |
| 4290 | status &= hba->ee_ctrl_mask; |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 4291 | |
| 4292 | if (status & MASK_EE_URGENT_BKOPS) |
| 4293 | ufshcd_bkops_exception_event_handler(hba); |
| 4294 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4295 | out: |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 4296 | pm_runtime_put_sync(hba->dev); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4297 | return; |
| 4298 | } |
| 4299 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4300 | /* Complete requests that have door-bell cleared */ |
| 4301 | static void ufshcd_complete_requests(struct ufs_hba *hba) |
| 4302 | { |
| 4303 | ufshcd_transfer_req_compl(hba); |
| 4304 | ufshcd_tmc_handler(hba); |
| 4305 | } |
| 4306 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4307 | /** |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 4308 | * ufshcd_quirk_dl_nac_errors - This function checks if error handling is |
| 4309 | * to recover from the DL NAC errors or not. |
| 4310 | * @hba: per-adapter instance |
| 4311 | * |
| 4312 | * Returns true if error handling is required, false otherwise |
| 4313 | */ |
| 4314 | static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba) |
| 4315 | { |
| 4316 | unsigned long flags; |
| 4317 | bool err_handling = true; |
| 4318 | |
| 4319 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4320 | /* |
| 4321 | * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the |
| 4322 | * device fatal error and/or DL NAC & REPLAY timeout errors. |
| 4323 | */ |
| 4324 | if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR)) |
| 4325 | goto out; |
| 4326 | |
| 4327 | if ((hba->saved_err & DEVICE_FATAL_ERROR) || |
| 4328 | ((hba->saved_err & UIC_ERROR) && |
| 4329 | (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR))) |
| 4330 | goto out; |
| 4331 | |
| 4332 | if ((hba->saved_err & UIC_ERROR) && |
| 4333 | (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) { |
| 4334 | int err; |
| 4335 | /* |
| 4336 | * wait for 50ms to see if we can get any other errors or not. |
| 4337 | */ |
| 4338 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4339 | msleep(50); |
| 4340 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4341 | |
| 4342 | /* |
| 4343 | * now check if we have got any other severe errors other than |
| 4344 | * DL NAC error? |
| 4345 | */ |
| 4346 | if ((hba->saved_err & INT_FATAL_ERRORS) || |
| 4347 | ((hba->saved_err & UIC_ERROR) && |
| 4348 | (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR))) |
| 4349 | goto out; |
| 4350 | |
| 4351 | /* |
| 4352 | * As DL NAC is the only error received so far, send out NOP |
| 4353 | * command to confirm if link is still active or not. |
| 4354 | * - If we don't get any response then do error recovery. |
| 4355 | * - If we get response then clear the DL NAC error bit. |
| 4356 | */ |
| 4357 | |
| 4358 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4359 | err = ufshcd_verify_dev_init(hba); |
| 4360 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4361 | |
| 4362 | if (err) |
| 4363 | goto out; |
| 4364 | |
| 4365 | /* Link seems to be alive hence ignore the DL NAC errors */ |
| 4366 | if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR) |
| 4367 | hba->saved_err &= ~UIC_ERROR; |
| 4368 | /* clear NAC error */ |
| 4369 | hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR; |
| 4370 | if (!hba->saved_uic_err) { |
| 4371 | err_handling = false; |
| 4372 | goto out; |
| 4373 | } |
| 4374 | } |
| 4375 | out: |
| 4376 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4377 | return err_handling; |
| 4378 | } |
| 4379 | |
| 4380 | /** |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4381 | * ufshcd_err_handler - handle UFS errors that require s/w attention |
| 4382 | * @work: pointer to work structure |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4383 | */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4384 | static void ufshcd_err_handler(struct work_struct *work) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4385 | { |
| 4386 | struct ufs_hba *hba; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4387 | unsigned long flags; |
| 4388 | u32 err_xfer = 0; |
| 4389 | u32 err_tm = 0; |
| 4390 | int err = 0; |
| 4391 | int tag; |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4392 | bool needs_reset = false; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4393 | |
| 4394 | hba = container_of(work, struct ufs_hba, eh_work); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4395 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 4396 | pm_runtime_get_sync(hba->dev); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4397 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4398 | |
| 4399 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4400 | if (hba->ufshcd_state == UFSHCD_STATE_RESET) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4401 | goto out; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4402 | |
| 4403 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 4404 | ufshcd_set_eh_in_progress(hba); |
| 4405 | |
| 4406 | /* Complete requests that have door-bell cleared by h/w */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4407 | ufshcd_complete_requests(hba); |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 4408 | |
| 4409 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) { |
| 4410 | bool ret; |
| 4411 | |
| 4412 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4413 | /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */ |
| 4414 | ret = ufshcd_quirk_dl_nac_errors(hba); |
| 4415 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4416 | if (!ret) |
| 4417 | goto skip_err_handling; |
| 4418 | } |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4419 | if ((hba->saved_err & INT_FATAL_ERRORS) || |
| 4420 | ((hba->saved_err & UIC_ERROR) && |
| 4421 | (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR | |
| 4422 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR | |
| 4423 | UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))) |
| 4424 | needs_reset = true; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4425 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4426 | /* |
| 4427 | * if host reset is required then skip clearing the pending |
| 4428 | * transfers forcefully because they will automatically get |
| 4429 | * cleared after link startup. |
| 4430 | */ |
| 4431 | if (needs_reset) |
| 4432 | goto skip_pending_xfer_clear; |
| 4433 | |
| 4434 | /* release lock as clear command might sleep */ |
| 4435 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4436 | /* Clear pending transfer requests */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4437 | for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) { |
| 4438 | if (ufshcd_clear_cmd(hba, tag)) { |
| 4439 | err_xfer = true; |
| 4440 | goto lock_skip_pending_xfer_clear; |
| 4441 | } |
| 4442 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4443 | |
| 4444 | /* Clear pending task management requests */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4445 | for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) { |
| 4446 | if (ufshcd_clear_tm_cmd(hba, tag)) { |
| 4447 | err_tm = true; |
| 4448 | goto lock_skip_pending_xfer_clear; |
| 4449 | } |
| 4450 | } |
| 4451 | |
| 4452 | lock_skip_pending_xfer_clear: |
| 4453 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4454 | |
| 4455 | /* Complete the requests that are cleared by s/w */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4456 | ufshcd_complete_requests(hba); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4457 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4458 | if (err_xfer || err_tm) |
| 4459 | needs_reset = true; |
| 4460 | |
| 4461 | skip_pending_xfer_clear: |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4462 | /* Fatal errors need reset */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4463 | if (needs_reset) { |
| 4464 | unsigned long max_doorbells = (1UL << hba->nutrs) - 1; |
| 4465 | |
| 4466 | /* |
| 4467 | * ufshcd_reset_and_restore() does the link reinitialization |
| 4468 | * which will need atleast one empty doorbell slot to send the |
| 4469 | * device management commands (NOP and query commands). |
| 4470 | * If there is no slot empty at this moment then free up last |
| 4471 | * slot forcefully. |
| 4472 | */ |
| 4473 | if (hba->outstanding_reqs == max_doorbells) |
| 4474 | __ufshcd_transfer_req_compl(hba, |
| 4475 | (1UL << (hba->nutrs - 1))); |
| 4476 | |
| 4477 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4478 | err = ufshcd_reset_and_restore(hba); |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4479 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4480 | if (err) { |
| 4481 | dev_err(hba->dev, "%s: reset and restore failed\n", |
| 4482 | __func__); |
| 4483 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 4484 | } |
| 4485 | /* |
| 4486 | * Inform scsi mid-layer that we did reset and allow to handle |
| 4487 | * Unit Attention properly. |
| 4488 | */ |
| 4489 | scsi_report_bus_reset(hba->host, 0); |
| 4490 | hba->saved_err = 0; |
| 4491 | hba->saved_uic_err = 0; |
| 4492 | } |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4493 | |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 4494 | skip_err_handling: |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4495 | if (!needs_reset) { |
| 4496 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
| 4497 | if (hba->saved_err || hba->saved_uic_err) |
| 4498 | dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x", |
| 4499 | __func__, hba->saved_err, hba->saved_uic_err); |
| 4500 | } |
| 4501 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4502 | ufshcd_clear_eh_in_progress(hba); |
| 4503 | |
| 4504 | out: |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4505 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4506 | scsi_unblock_requests(hba->host); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4507 | ufshcd_release(hba); |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 4508 | pm_runtime_put_sync(hba->dev); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4509 | } |
| 4510 | |
| 4511 | /** |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4512 | * ufshcd_update_uic_error - check and set fatal UIC error flags. |
| 4513 | * @hba: per-adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4514 | */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4515 | static void ufshcd_update_uic_error(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4516 | { |
| 4517 | u32 reg; |
| 4518 | |
Dolev Raviv | fb7b45f | 2016-11-23 16:32:32 -0800 | [diff] [blame] | 4519 | /* PHY layer lane error */ |
| 4520 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER); |
| 4521 | /* Ignore LINERESET indication, as this is not an error */ |
| 4522 | if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) && |
| 4523 | (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK)) |
| 4524 | /* |
| 4525 | * To know whether this error is fatal or not, DB timeout |
| 4526 | * must be checked but this error is handled separately. |
| 4527 | */ |
| 4528 | dev_dbg(hba->dev, "%s: UIC Lane error reported\n", __func__); |
| 4529 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4530 | /* PA_INIT_ERROR is fatal and needs UIC reset */ |
| 4531 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER); |
| 4532 | if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT) |
| 4533 | hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 4534 | else if (hba->dev_quirks & |
| 4535 | UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) { |
| 4536 | if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED) |
| 4537 | hba->uic_error |= |
| 4538 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR; |
| 4539 | else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT) |
| 4540 | hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR; |
| 4541 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4542 | |
| 4543 | /* UIC NL/TL/DME errors needs software retry */ |
| 4544 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER); |
| 4545 | if (reg) |
| 4546 | hba->uic_error |= UFSHCD_UIC_NL_ERROR; |
| 4547 | |
| 4548 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER); |
| 4549 | if (reg) |
| 4550 | hba->uic_error |= UFSHCD_UIC_TL_ERROR; |
| 4551 | |
| 4552 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME); |
| 4553 | if (reg) |
| 4554 | hba->uic_error |= UFSHCD_UIC_DME_ERROR; |
| 4555 | |
| 4556 | dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n", |
| 4557 | __func__, hba->uic_error); |
| 4558 | } |
| 4559 | |
| 4560 | /** |
| 4561 | * ufshcd_check_errors - Check for errors that need s/w attention |
| 4562 | * @hba: per-adapter instance |
| 4563 | */ |
| 4564 | static void ufshcd_check_errors(struct ufs_hba *hba) |
| 4565 | { |
| 4566 | bool queue_eh_work = false; |
| 4567 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4568 | if (hba->errors & INT_FATAL_ERRORS) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4569 | queue_eh_work = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4570 | |
| 4571 | if (hba->errors & UIC_ERROR) { |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4572 | hba->uic_error = 0; |
| 4573 | ufshcd_update_uic_error(hba); |
| 4574 | if (hba->uic_error) |
| 4575 | queue_eh_work = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4576 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4577 | |
| 4578 | if (queue_eh_work) { |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4579 | /* |
| 4580 | * update the transfer error masks to sticky bits, let's do this |
| 4581 | * irrespective of current ufshcd_state. |
| 4582 | */ |
| 4583 | hba->saved_err |= hba->errors; |
| 4584 | hba->saved_uic_err |= hba->uic_error; |
| 4585 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4586 | /* handle fatal errors only when link is functional */ |
| 4587 | if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) { |
| 4588 | /* block commands from scsi mid-layer */ |
| 4589 | scsi_block_requests(hba->host); |
| 4590 | |
Zang Leigang | 141f816 | 2016-11-16 11:29:37 +0800 | [diff] [blame] | 4591 | hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED; |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 4592 | |
| 4593 | /* dump controller state before resetting */ |
| 4594 | if (hba->saved_err & (INT_FATAL_ERRORS | UIC_ERROR)) { |
| 4595 | bool pr_prdt = !!(hba->saved_err & |
| 4596 | SYSTEM_BUS_FATAL_ERROR); |
| 4597 | |
| 4598 | dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x\n", |
| 4599 | __func__, hba->saved_err, |
| 4600 | hba->saved_uic_err); |
| 4601 | |
| 4602 | ufshcd_print_host_regs(hba); |
| 4603 | ufshcd_print_pwr_info(hba); |
| 4604 | ufshcd_print_tmrs(hba, hba->outstanding_tasks); |
| 4605 | ufshcd_print_trs(hba, hba->outstanding_reqs, |
| 4606 | pr_prdt); |
| 4607 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4608 | schedule_work(&hba->eh_work); |
| 4609 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4610 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4611 | /* |
| 4612 | * if (!queue_eh_work) - |
| 4613 | * Other errors are either non-fatal where host recovers |
| 4614 | * itself without s/w intervention or errors that will be |
| 4615 | * handled by the SCSI core layer. |
| 4616 | */ |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4617 | } |
| 4618 | |
| 4619 | /** |
| 4620 | * ufshcd_tmc_handler - handle task management function completion |
| 4621 | * @hba: per adapter instance |
| 4622 | */ |
| 4623 | static void ufshcd_tmc_handler(struct ufs_hba *hba) |
| 4624 | { |
| 4625 | u32 tm_doorbell; |
| 4626 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 4627 | tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4628 | hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4629 | wake_up(&hba->tm_wq); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4630 | } |
| 4631 | |
| 4632 | /** |
| 4633 | * ufshcd_sl_intr - Interrupt service routine |
| 4634 | * @hba: per adapter instance |
| 4635 | * @intr_status: contains interrupts generated by the controller |
| 4636 | */ |
| 4637 | static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status) |
| 4638 | { |
| 4639 | hba->errors = UFSHCD_ERROR_MASK & intr_status; |
| 4640 | if (hba->errors) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4641 | ufshcd_check_errors(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4642 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4643 | if (intr_status & UFSHCD_UIC_MASK) |
| 4644 | ufshcd_uic_cmd_compl(hba, intr_status); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4645 | |
| 4646 | if (intr_status & UTP_TASK_REQ_COMPL) |
| 4647 | ufshcd_tmc_handler(hba); |
| 4648 | |
| 4649 | if (intr_status & UTP_TRANSFER_REQ_COMPL) |
| 4650 | ufshcd_transfer_req_compl(hba); |
| 4651 | } |
| 4652 | |
| 4653 | /** |
| 4654 | * ufshcd_intr - Main interrupt service routine |
| 4655 | * @irq: irq number |
| 4656 | * @__hba: pointer to adapter instance |
| 4657 | * |
| 4658 | * Returns IRQ_HANDLED - If interrupt is valid |
| 4659 | * IRQ_NONE - If invalid interrupt |
| 4660 | */ |
| 4661 | static irqreturn_t ufshcd_intr(int irq, void *__hba) |
| 4662 | { |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 4663 | u32 intr_status, enabled_intr_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4664 | irqreturn_t retval = IRQ_NONE; |
| 4665 | struct ufs_hba *hba = __hba; |
| 4666 | |
| 4667 | spin_lock(hba->host->host_lock); |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 4668 | intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 4669 | enabled_intr_status = |
| 4670 | intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4671 | |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 4672 | if (intr_status) |
Seungwon Jeon | 261ea45 | 2013-06-26 22:39:28 +0530 | [diff] [blame] | 4673 | ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 4674 | |
| 4675 | if (enabled_intr_status) { |
| 4676 | ufshcd_sl_intr(hba, enabled_intr_status); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4677 | retval = IRQ_HANDLED; |
| 4678 | } |
| 4679 | spin_unlock(hba->host->host_lock); |
| 4680 | return retval; |
| 4681 | } |
| 4682 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4683 | static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag) |
| 4684 | { |
| 4685 | int err = 0; |
| 4686 | u32 mask = 1 << tag; |
| 4687 | unsigned long flags; |
| 4688 | |
| 4689 | if (!test_bit(tag, &hba->outstanding_tasks)) |
| 4690 | goto out; |
| 4691 | |
| 4692 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4693 | ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR); |
| 4694 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4695 | |
| 4696 | /* poll for max. 1 sec to clear door bell register by h/w */ |
| 4697 | err = ufshcd_wait_for_register(hba, |
| 4698 | REG_UTP_TASK_REQ_DOOR_BELL, |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4699 | mask, 0, 1000, 1000, true); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4700 | out: |
| 4701 | return err; |
| 4702 | } |
| 4703 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4704 | /** |
| 4705 | * ufshcd_issue_tm_cmd - issues task management commands to controller |
| 4706 | * @hba: per adapter instance |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4707 | * @lun_id: LUN ID to which TM command is sent |
| 4708 | * @task_id: task ID to which the TM command is applicable |
| 4709 | * @tm_function: task management function opcode |
| 4710 | * @tm_response: task management service response return value |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4711 | * |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4712 | * Returns non-zero value on error, zero on success. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4713 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4714 | static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id, |
| 4715 | u8 tm_function, u8 *tm_response) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4716 | { |
| 4717 | struct utp_task_req_desc *task_req_descp; |
| 4718 | struct utp_upiu_task_req *task_req_upiup; |
| 4719 | struct Scsi_Host *host; |
| 4720 | unsigned long flags; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4721 | int free_slot; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4722 | int err; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4723 | int task_tag; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4724 | |
| 4725 | host = hba->host; |
| 4726 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4727 | /* |
| 4728 | * Get free slot, sleep if slots are unavailable. |
| 4729 | * Even though we use wait_event() which sleeps indefinitely, |
| 4730 | * the maximum wait time is bounded by %TM_CMD_TIMEOUT. |
| 4731 | */ |
| 4732 | 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] | 4733 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4734 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4735 | spin_lock_irqsave(host->host_lock, flags); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4736 | task_req_descp = hba->utmrdl_base_addr; |
| 4737 | task_req_descp += free_slot; |
| 4738 | |
| 4739 | /* Configure task request descriptor */ |
| 4740 | task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD); |
| 4741 | task_req_descp->header.dword_2 = |
| 4742 | cpu_to_le32(OCS_INVALID_COMMAND_STATUS); |
| 4743 | |
| 4744 | /* Configure task request UPIU */ |
| 4745 | task_req_upiup = |
| 4746 | (struct utp_upiu_task_req *) task_req_descp->task_req_upiu; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4747 | task_tag = hba->nutrs + free_slot; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4748 | task_req_upiup->header.dword_0 = |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4749 | UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0, |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4750 | lun_id, task_tag); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4751 | task_req_upiup->header.dword_1 = |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4752 | UPIU_HEADER_DWORD(0, tm_function, 0, 0); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4753 | /* |
| 4754 | * The host shall provide the same value for LUN field in the basic |
| 4755 | * header and for Input Parameter. |
| 4756 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4757 | task_req_upiup->input_param1 = cpu_to_be32(lun_id); |
| 4758 | task_req_upiup->input_param2 = cpu_to_be32(task_id); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4759 | |
Kiwoong Kim | d2877be | 2016-11-10 21:16:15 +0900 | [diff] [blame] | 4760 | ufshcd_vops_setup_task_mgmt(hba, free_slot, tm_function); |
| 4761 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4762 | /* send command to the controller */ |
| 4763 | __set_bit(free_slot, &hba->outstanding_tasks); |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 4764 | |
| 4765 | /* Make sure descriptors are ready before ringing the task doorbell */ |
| 4766 | wmb(); |
| 4767 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 4768 | ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL); |
Gilad Broner | ad1a1b9 | 2016-10-17 17:09:36 -0700 | [diff] [blame] | 4769 | /* Make sure that doorbell is committed immediately */ |
| 4770 | wmb(); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4771 | |
| 4772 | spin_unlock_irqrestore(host->host_lock, flags); |
| 4773 | |
| 4774 | /* wait until the task management command is completed */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4775 | err = wait_event_timeout(hba->tm_wq, |
| 4776 | test_bit(free_slot, &hba->tm_condition), |
| 4777 | msecs_to_jiffies(TM_CMD_TIMEOUT)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4778 | if (!err) { |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4779 | dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n", |
| 4780 | __func__, tm_function); |
| 4781 | if (ufshcd_clear_tm_cmd(hba, free_slot)) |
| 4782 | dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n", |
| 4783 | __func__, free_slot); |
| 4784 | err = -ETIMEDOUT; |
| 4785 | } else { |
| 4786 | err = ufshcd_task_req_compl(hba, free_slot, tm_response); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4787 | } |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4788 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4789 | clear_bit(free_slot, &hba->tm_condition); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4790 | ufshcd_put_tm_slot(hba, free_slot); |
| 4791 | wake_up(&hba->tm_tag_wq); |
| 4792 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4793 | ufshcd_release(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4794 | return err; |
| 4795 | } |
| 4796 | |
| 4797 | /** |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4798 | * ufshcd_eh_device_reset_handler - device reset handler registered to |
| 4799 | * scsi layer. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4800 | * @cmd: SCSI command pointer |
| 4801 | * |
| 4802 | * Returns SUCCESS/FAILED |
| 4803 | */ |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4804 | static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4805 | { |
| 4806 | struct Scsi_Host *host; |
| 4807 | struct ufs_hba *hba; |
| 4808 | unsigned int tag; |
| 4809 | u32 pos; |
| 4810 | int err; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4811 | u8 resp = 0xF; |
| 4812 | struct ufshcd_lrb *lrbp; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4813 | unsigned long flags; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4814 | |
| 4815 | host = cmd->device->host; |
| 4816 | hba = shost_priv(host); |
| 4817 | tag = cmd->request->tag; |
| 4818 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4819 | lrbp = &hba->lrb[tag]; |
| 4820 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp); |
| 4821 | if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4822 | if (!err) |
| 4823 | err = resp; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4824 | goto out; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4825 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4826 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4827 | /* clear the commands that were pending for corresponding LUN */ |
| 4828 | for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) { |
| 4829 | if (hba->lrb[pos].lun == lrbp->lun) { |
| 4830 | err = ufshcd_clear_cmd(hba, pos); |
| 4831 | if (err) |
| 4832 | break; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4833 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4834 | } |
| 4835 | spin_lock_irqsave(host->host_lock, flags); |
| 4836 | ufshcd_transfer_req_compl(hba); |
| 4837 | spin_unlock_irqrestore(host->host_lock, flags); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4838 | out: |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4839 | if (!err) { |
| 4840 | err = SUCCESS; |
| 4841 | } else { |
| 4842 | dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); |
| 4843 | err = FAILED; |
| 4844 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4845 | return err; |
| 4846 | } |
| 4847 | |
| 4848 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4849 | * ufshcd_abort - abort a specific command |
| 4850 | * @cmd: SCSI command pointer |
| 4851 | * |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 4852 | * Abort the pending command in device by sending UFS_ABORT_TASK task management |
| 4853 | * command, and in host controller by clearing the door-bell register. There can |
| 4854 | * be race between controller sending the command to the device while abort is |
| 4855 | * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is |
| 4856 | * really issued and then try to abort it. |
| 4857 | * |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4858 | * Returns SUCCESS/FAILED |
| 4859 | */ |
| 4860 | static int ufshcd_abort(struct scsi_cmnd *cmd) |
| 4861 | { |
| 4862 | struct Scsi_Host *host; |
| 4863 | struct ufs_hba *hba; |
| 4864 | unsigned long flags; |
| 4865 | unsigned int tag; |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 4866 | int err = 0; |
| 4867 | int poll_cnt; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4868 | u8 resp = 0xF; |
| 4869 | struct ufshcd_lrb *lrbp; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4870 | u32 reg; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4871 | |
| 4872 | host = cmd->device->host; |
| 4873 | hba = shost_priv(host); |
| 4874 | tag = cmd->request->tag; |
Dolev Raviv | e7d3825 | 2016-12-22 18:40:07 -0800 | [diff] [blame] | 4875 | lrbp = &hba->lrb[tag]; |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 4876 | if (!ufshcd_valid_tag(hba, tag)) { |
| 4877 | dev_err(hba->dev, |
| 4878 | "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p", |
| 4879 | __func__, tag, cmd, cmd->request); |
| 4880 | BUG(); |
| 4881 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4882 | |
Dolev Raviv | e7d3825 | 2016-12-22 18:40:07 -0800 | [diff] [blame] | 4883 | /* |
| 4884 | * Task abort to the device W-LUN is illegal. When this command |
| 4885 | * will fail, due to spec violation, scsi err handling next step |
| 4886 | * will be to send LU reset which, again, is a spec violation. |
| 4887 | * To avoid these unnecessary/illegal step we skip to the last error |
| 4888 | * handling stage: reset and restore. |
| 4889 | */ |
| 4890 | if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN) |
| 4891 | return ufshcd_eh_host_reset_handler(cmd); |
| 4892 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4893 | ufshcd_hold(hba, false); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4894 | reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 4895 | /* If command is already aborted/completed, return SUCCESS */ |
| 4896 | if (!(test_bit(tag, &hba->outstanding_reqs))) { |
| 4897 | dev_err(hba->dev, |
| 4898 | "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n", |
| 4899 | __func__, tag, hba->outstanding_reqs, reg); |
| 4900 | goto out; |
| 4901 | } |
| 4902 | |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4903 | if (!(reg & (1 << tag))) { |
| 4904 | dev_err(hba->dev, |
| 4905 | "%s: cmd was completed, but without a notifying intr, tag = %d", |
| 4906 | __func__, tag); |
| 4907 | } |
| 4908 | |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 4909 | /* Print Transfer Request of aborted task */ |
| 4910 | dev_err(hba->dev, "%s: Device abort task at tag %d\n", __func__, tag); |
| 4911 | scsi_print_command(hba->lrb[tag].cmd); |
| 4912 | ufshcd_print_host_regs(hba); |
| 4913 | ufshcd_print_pwr_info(hba); |
| 4914 | ufshcd_print_trs(hba, 1 << tag, true); |
| 4915 | |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 4916 | for (poll_cnt = 100; poll_cnt; poll_cnt--) { |
| 4917 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, |
| 4918 | UFS_QUERY_TASK, &resp); |
| 4919 | if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) { |
| 4920 | /* cmd pending in the device */ |
| 4921 | break; |
| 4922 | } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 4923 | /* |
| 4924 | * cmd not pending in the device, check if it is |
| 4925 | * in transition. |
| 4926 | */ |
| 4927 | reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 4928 | if (reg & (1 << tag)) { |
| 4929 | /* sleep for max. 200us to stabilize */ |
| 4930 | usleep_range(100, 200); |
| 4931 | continue; |
| 4932 | } |
| 4933 | /* command completed already */ |
| 4934 | goto out; |
| 4935 | } else { |
| 4936 | if (!err) |
| 4937 | err = resp; /* service response error */ |
| 4938 | goto out; |
| 4939 | } |
| 4940 | } |
| 4941 | |
| 4942 | if (!poll_cnt) { |
| 4943 | err = -EBUSY; |
| 4944 | goto out; |
| 4945 | } |
| 4946 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4947 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, |
| 4948 | UFS_ABORT_TASK, &resp); |
| 4949 | if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 4950 | if (!err) |
| 4951 | err = resp; /* service response error */ |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4952 | goto out; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4953 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4954 | |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 4955 | err = ufshcd_clear_cmd(hba, tag); |
| 4956 | if (err) |
| 4957 | goto out; |
| 4958 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4959 | scsi_dma_unmap(cmd); |
| 4960 | |
| 4961 | spin_lock_irqsave(host->host_lock, flags); |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 4962 | ufshcd_outstanding_req_clear(hba, tag); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4963 | hba->lrb[tag].cmd = NULL; |
| 4964 | spin_unlock_irqrestore(host->host_lock, flags); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4965 | |
| 4966 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 4967 | wake_up(&hba->dev_cmd.tag_wq); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4968 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4969 | out: |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 4970 | if (!err) { |
| 4971 | err = SUCCESS; |
| 4972 | } else { |
| 4973 | dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); |
| 4974 | err = FAILED; |
| 4975 | } |
| 4976 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4977 | /* |
| 4978 | * This ufshcd_release() corresponds to the original scsi cmd that got |
| 4979 | * aborted here (as we won't get any IRQ for it). |
| 4980 | */ |
| 4981 | ufshcd_release(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4982 | return err; |
| 4983 | } |
| 4984 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4985 | /** |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4986 | * ufshcd_host_reset_and_restore - reset and restore host controller |
| 4987 | * @hba: per-adapter instance |
| 4988 | * |
| 4989 | * Note that host controller reset may issue DME_RESET to |
| 4990 | * local and remote (device) Uni-Pro stack and the attributes |
| 4991 | * are reset to default state. |
| 4992 | * |
| 4993 | * Returns zero on success, non-zero on failure |
| 4994 | */ |
| 4995 | static int ufshcd_host_reset_and_restore(struct ufs_hba *hba) |
| 4996 | { |
| 4997 | int err; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 4998 | unsigned long flags; |
| 4999 | |
| 5000 | /* Reset the host controller */ |
| 5001 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 5002 | ufshcd_hba_stop(hba, false); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5003 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5004 | |
| 5005 | err = ufshcd_hba_enable(hba); |
| 5006 | if (err) |
| 5007 | goto out; |
| 5008 | |
| 5009 | /* Establish the link again and restore the device */ |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 5010 | err = ufshcd_probe_hba(hba); |
| 5011 | |
| 5012 | if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL)) |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5013 | err = -EIO; |
| 5014 | out: |
| 5015 | if (err) |
| 5016 | dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err); |
| 5017 | |
| 5018 | return err; |
| 5019 | } |
| 5020 | |
| 5021 | /** |
| 5022 | * ufshcd_reset_and_restore - reset and re-initialize host/device |
| 5023 | * @hba: per-adapter instance |
| 5024 | * |
| 5025 | * Reset and recover device, host and re-establish link. This |
| 5026 | * is helpful to recover the communication in fatal error conditions. |
| 5027 | * |
| 5028 | * Returns zero on success, non-zero on failure |
| 5029 | */ |
| 5030 | static int ufshcd_reset_and_restore(struct ufs_hba *hba) |
| 5031 | { |
| 5032 | int err = 0; |
| 5033 | unsigned long flags; |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 5034 | int retries = MAX_HOST_RESET_RETRIES; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5035 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 5036 | do { |
| 5037 | err = ufshcd_host_reset_and_restore(hba); |
| 5038 | } while (err && --retries); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5039 | |
| 5040 | /* |
| 5041 | * After reset the door-bell might be cleared, complete |
| 5042 | * outstanding requests in s/w here. |
| 5043 | */ |
| 5044 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5045 | ufshcd_transfer_req_compl(hba); |
| 5046 | ufshcd_tmc_handler(hba); |
| 5047 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5048 | |
| 5049 | return err; |
| 5050 | } |
| 5051 | |
| 5052 | /** |
| 5053 | * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer |
| 5054 | * @cmd - SCSI command pointer |
| 5055 | * |
| 5056 | * Returns SUCCESS/FAILED |
| 5057 | */ |
| 5058 | static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd) |
| 5059 | { |
| 5060 | int err; |
| 5061 | unsigned long flags; |
| 5062 | struct ufs_hba *hba; |
| 5063 | |
| 5064 | hba = shost_priv(cmd->device->host); |
| 5065 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5066 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5067 | /* |
| 5068 | * Check if there is any race with fatal error handling. |
| 5069 | * If so, wait for it to complete. Even though fatal error |
| 5070 | * handling does reset and restore in some cases, don't assume |
| 5071 | * anything out of it. We are just avoiding race here. |
| 5072 | */ |
| 5073 | do { |
| 5074 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5075 | if (!(work_pending(&hba->eh_work) || |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5076 | hba->ufshcd_state == UFSHCD_STATE_RESET)) |
| 5077 | break; |
| 5078 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5079 | dev_dbg(hba->dev, "%s: reset in progress\n", __func__); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5080 | flush_work(&hba->eh_work); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5081 | } while (1); |
| 5082 | |
| 5083 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 5084 | ufshcd_set_eh_in_progress(hba); |
| 5085 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5086 | |
| 5087 | err = ufshcd_reset_and_restore(hba); |
| 5088 | |
| 5089 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5090 | if (!err) { |
| 5091 | err = SUCCESS; |
| 5092 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
| 5093 | } else { |
| 5094 | err = FAILED; |
| 5095 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 5096 | } |
| 5097 | ufshcd_clear_eh_in_progress(hba); |
| 5098 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5099 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5100 | ufshcd_release(hba); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5101 | return err; |
| 5102 | } |
| 5103 | |
| 5104 | /** |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 5105 | * ufshcd_get_max_icc_level - calculate the ICC level |
| 5106 | * @sup_curr_uA: max. current supported by the regulator |
| 5107 | * @start_scan: row at the desc table to start scan from |
| 5108 | * @buff: power descriptor buffer |
| 5109 | * |
| 5110 | * Returns calculated max ICC level for specific regulator |
| 5111 | */ |
| 5112 | static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff) |
| 5113 | { |
| 5114 | int i; |
| 5115 | int curr_uA; |
| 5116 | u16 data; |
| 5117 | u16 unit; |
| 5118 | |
| 5119 | for (i = start_scan; i >= 0; i--) { |
| 5120 | data = be16_to_cpu(*((u16 *)(buff + 2*i))); |
| 5121 | unit = (data & ATTR_ICC_LVL_UNIT_MASK) >> |
| 5122 | ATTR_ICC_LVL_UNIT_OFFSET; |
| 5123 | curr_uA = data & ATTR_ICC_LVL_VALUE_MASK; |
| 5124 | switch (unit) { |
| 5125 | case UFSHCD_NANO_AMP: |
| 5126 | curr_uA = curr_uA / 1000; |
| 5127 | break; |
| 5128 | case UFSHCD_MILI_AMP: |
| 5129 | curr_uA = curr_uA * 1000; |
| 5130 | break; |
| 5131 | case UFSHCD_AMP: |
| 5132 | curr_uA = curr_uA * 1000 * 1000; |
| 5133 | break; |
| 5134 | case UFSHCD_MICRO_AMP: |
| 5135 | default: |
| 5136 | break; |
| 5137 | } |
| 5138 | if (sup_curr_uA >= curr_uA) |
| 5139 | break; |
| 5140 | } |
| 5141 | if (i < 0) { |
| 5142 | i = 0; |
| 5143 | pr_err("%s: Couldn't find valid icc_level = %d", __func__, i); |
| 5144 | } |
| 5145 | |
| 5146 | return (u32)i; |
| 5147 | } |
| 5148 | |
| 5149 | /** |
| 5150 | * ufshcd_calc_icc_level - calculate the max ICC level |
| 5151 | * In case regulators are not initialized we'll return 0 |
| 5152 | * @hba: per-adapter instance |
| 5153 | * @desc_buf: power descriptor buffer to extract ICC levels from. |
| 5154 | * @len: length of desc_buff |
| 5155 | * |
| 5156 | * Returns calculated ICC level |
| 5157 | */ |
| 5158 | static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba, |
| 5159 | u8 *desc_buf, int len) |
| 5160 | { |
| 5161 | u32 icc_level = 0; |
| 5162 | |
| 5163 | if (!hba->vreg_info.vcc || !hba->vreg_info.vccq || |
| 5164 | !hba->vreg_info.vccq2) { |
| 5165 | dev_err(hba->dev, |
| 5166 | "%s: Regulator capability was not set, actvIccLevel=%d", |
| 5167 | __func__, icc_level); |
| 5168 | goto out; |
| 5169 | } |
| 5170 | |
| 5171 | if (hba->vreg_info.vcc) |
| 5172 | icc_level = ufshcd_get_max_icc_level( |
| 5173 | hba->vreg_info.vcc->max_uA, |
| 5174 | POWER_DESC_MAX_ACTV_ICC_LVLS - 1, |
| 5175 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]); |
| 5176 | |
| 5177 | if (hba->vreg_info.vccq) |
| 5178 | icc_level = ufshcd_get_max_icc_level( |
| 5179 | hba->vreg_info.vccq->max_uA, |
| 5180 | icc_level, |
| 5181 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]); |
| 5182 | |
| 5183 | if (hba->vreg_info.vccq2) |
| 5184 | icc_level = ufshcd_get_max_icc_level( |
| 5185 | hba->vreg_info.vccq2->max_uA, |
| 5186 | icc_level, |
| 5187 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]); |
| 5188 | out: |
| 5189 | return icc_level; |
| 5190 | } |
| 5191 | |
Dolev Raviv | 61e0735 | 2016-11-23 16:30:49 -0800 | [diff] [blame] | 5192 | static int ufshcd_set_icc_levels_attr(struct ufs_hba *hba, u32 icc_level) |
| 5193 | { |
| 5194 | int ret = 0; |
| 5195 | int retries; |
| 5196 | |
| 5197 | for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { |
| 5198 | /* write attribute */ |
| 5199 | ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
| 5200 | QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, &icc_level); |
| 5201 | if (!ret) |
| 5202 | break; |
| 5203 | |
| 5204 | dev_dbg(hba->dev, "%s: failed with error %d\n", __func__, ret); |
| 5205 | } |
| 5206 | |
| 5207 | return ret; |
| 5208 | } |
| 5209 | |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 5210 | static void ufshcd_init_icc_levels(struct ufs_hba *hba) |
| 5211 | { |
| 5212 | int ret; |
| 5213 | int buff_len = QUERY_DESC_POWER_MAX_SIZE; |
| 5214 | u8 desc_buf[QUERY_DESC_POWER_MAX_SIZE]; |
| 5215 | |
| 5216 | ret = ufshcd_read_power_desc(hba, desc_buf, buff_len); |
| 5217 | if (ret) { |
| 5218 | dev_err(hba->dev, |
| 5219 | "%s: Failed reading power descriptor.len = %d ret = %d", |
| 5220 | __func__, buff_len, ret); |
| 5221 | return; |
| 5222 | } |
| 5223 | |
| 5224 | hba->init_prefetch_data.icc_level = |
| 5225 | ufshcd_find_max_sup_active_icc_level(hba, |
| 5226 | desc_buf, buff_len); |
| 5227 | dev_dbg(hba->dev, "%s: setting icc_level 0x%x", |
| 5228 | __func__, hba->init_prefetch_data.icc_level); |
| 5229 | |
Dolev Raviv | 61e0735 | 2016-11-23 16:30:49 -0800 | [diff] [blame] | 5230 | ret = ufshcd_set_icc_levels_attr(hba, |
| 5231 | hba->init_prefetch_data.icc_level); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 5232 | |
| 5233 | if (ret) |
| 5234 | dev_err(hba->dev, |
| 5235 | "%s: Failed configuring bActiveICCLevel = %d ret = %d", |
| 5236 | __func__, hba->init_prefetch_data.icc_level , ret); |
| 5237 | |
| 5238 | } |
| 5239 | |
| 5240 | /** |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 5241 | * ufshcd_scsi_add_wlus - Adds required W-LUs |
| 5242 | * @hba: per-adapter instance |
| 5243 | * |
| 5244 | * UFS device specification requires the UFS devices to support 4 well known |
| 5245 | * logical units: |
| 5246 | * "REPORT_LUNS" (address: 01h) |
| 5247 | * "UFS Device" (address: 50h) |
| 5248 | * "RPMB" (address: 44h) |
| 5249 | * "BOOT" (address: 30h) |
| 5250 | * UFS device's power management needs to be controlled by "POWER CONDITION" |
| 5251 | * field of SSU (START STOP UNIT) command. But this "power condition" field |
| 5252 | * will take effect only when its sent to "UFS device" well known logical unit |
| 5253 | * hence we require the scsi_device instance to represent this logical unit in |
| 5254 | * order for the UFS host driver to send the SSU command for power management. |
| 5255 | |
| 5256 | * We also require the scsi_device instance for "RPMB" (Replay Protected Memory |
| 5257 | * Block) LU so user space process can control this LU. User space may also |
| 5258 | * want to have access to BOOT LU. |
| 5259 | |
| 5260 | * This function adds scsi device instances for each of all well known LUs |
| 5261 | * (except "REPORT LUNS" LU). |
| 5262 | * |
| 5263 | * Returns zero on success (all required W-LUs are added successfully), |
| 5264 | * non-zero error value on failure (if failed to add any of the required W-LU). |
| 5265 | */ |
| 5266 | static int ufshcd_scsi_add_wlus(struct ufs_hba *hba) |
| 5267 | { |
| 5268 | int ret = 0; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 5269 | struct scsi_device *sdev_rpmb; |
| 5270 | struct scsi_device *sdev_boot; |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 5271 | |
| 5272 | hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0, |
| 5273 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL); |
| 5274 | if (IS_ERR(hba->sdev_ufs_device)) { |
| 5275 | ret = PTR_ERR(hba->sdev_ufs_device); |
| 5276 | hba->sdev_ufs_device = NULL; |
| 5277 | goto out; |
| 5278 | } |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 5279 | scsi_device_put(hba->sdev_ufs_device); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 5280 | |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 5281 | sdev_boot = __scsi_add_device(hba->host, 0, 0, |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 5282 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL); |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 5283 | if (IS_ERR(sdev_boot)) { |
| 5284 | ret = PTR_ERR(sdev_boot); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 5285 | goto remove_sdev_ufs_device; |
| 5286 | } |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 5287 | scsi_device_put(sdev_boot); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 5288 | |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 5289 | sdev_rpmb = __scsi_add_device(hba->host, 0, 0, |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 5290 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL); |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 5291 | if (IS_ERR(sdev_rpmb)) { |
| 5292 | ret = PTR_ERR(sdev_rpmb); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 5293 | goto remove_sdev_boot; |
| 5294 | } |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 5295 | scsi_device_put(sdev_rpmb); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 5296 | goto out; |
| 5297 | |
| 5298 | remove_sdev_boot: |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 5299 | scsi_remove_device(sdev_boot); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 5300 | remove_sdev_ufs_device: |
| 5301 | scsi_remove_device(hba->sdev_ufs_device); |
| 5302 | out: |
| 5303 | return ret; |
| 5304 | } |
| 5305 | |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 5306 | static int ufs_get_device_info(struct ufs_hba *hba, |
| 5307 | struct ufs_device_info *card_data) |
| 5308 | { |
| 5309 | int err; |
| 5310 | u8 model_index; |
| 5311 | u8 str_desc_buf[QUERY_DESC_STRING_MAX_SIZE + 1] = {0}; |
| 5312 | u8 desc_buf[QUERY_DESC_DEVICE_MAX_SIZE]; |
| 5313 | |
| 5314 | err = ufshcd_read_device_desc(hba, desc_buf, |
| 5315 | QUERY_DESC_DEVICE_MAX_SIZE); |
| 5316 | if (err) { |
| 5317 | dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n", |
| 5318 | __func__, err); |
| 5319 | goto out; |
| 5320 | } |
| 5321 | |
| 5322 | /* |
| 5323 | * getting vendor (manufacturerID) and Bank Index in big endian |
| 5324 | * format |
| 5325 | */ |
| 5326 | card_data->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 | |
| 5327 | desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1]; |
| 5328 | |
| 5329 | model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME]; |
| 5330 | |
| 5331 | err = ufshcd_read_string_desc(hba, model_index, str_desc_buf, |
| 5332 | QUERY_DESC_STRING_MAX_SIZE, ASCII_STD); |
| 5333 | if (err) { |
| 5334 | dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n", |
| 5335 | __func__, err); |
| 5336 | goto out; |
| 5337 | } |
| 5338 | |
| 5339 | str_desc_buf[QUERY_DESC_STRING_MAX_SIZE] = '\0'; |
| 5340 | strlcpy(card_data->model, (str_desc_buf + QUERY_DESC_HDR_SIZE), |
| 5341 | min_t(u8, str_desc_buf[QUERY_DESC_LENGTH_OFFSET], |
| 5342 | MAX_MODEL_LEN)); |
| 5343 | |
| 5344 | /* Null terminate the model string */ |
| 5345 | card_data->model[MAX_MODEL_LEN] = '\0'; |
| 5346 | |
| 5347 | out: |
| 5348 | return err; |
| 5349 | } |
| 5350 | |
| 5351 | void ufs_advertise_fixup_device(struct ufs_hba *hba) |
| 5352 | { |
| 5353 | int err; |
| 5354 | struct ufs_dev_fix *f; |
| 5355 | struct ufs_device_info card_data; |
| 5356 | |
| 5357 | card_data.wmanufacturerid = 0; |
| 5358 | |
| 5359 | err = ufs_get_device_info(hba, &card_data); |
| 5360 | if (err) { |
| 5361 | dev_err(hba->dev, "%s: Failed getting device info. err = %d\n", |
| 5362 | __func__, err); |
| 5363 | return; |
| 5364 | } |
| 5365 | |
| 5366 | for (f = ufs_fixups; f->quirk; f++) { |
| 5367 | if (((f->card.wmanufacturerid == card_data.wmanufacturerid) || |
| 5368 | (f->card.wmanufacturerid == UFS_ANY_VENDOR)) && |
| 5369 | (STR_PRFX_EQUAL(f->card.model, card_data.model) || |
| 5370 | !strcmp(f->card.model, UFS_ANY_MODEL))) |
| 5371 | hba->dev_quirks |= f->quirk; |
| 5372 | } |
| 5373 | } |
| 5374 | |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 5375 | /** |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 5376 | * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro |
| 5377 | * @hba: per-adapter instance |
| 5378 | * |
| 5379 | * PA_TActivate parameter can be tuned manually if UniPro version is less than |
| 5380 | * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's |
| 5381 | * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce |
| 5382 | * the hibern8 exit latency. |
| 5383 | * |
| 5384 | * Returns zero on success, non-zero error value on failure. |
| 5385 | */ |
| 5386 | static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba) |
| 5387 | { |
| 5388 | int ret = 0; |
| 5389 | u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate; |
| 5390 | |
| 5391 | ret = ufshcd_dme_peer_get(hba, |
| 5392 | UIC_ARG_MIB_SEL( |
| 5393 | RX_MIN_ACTIVATETIME_CAPABILITY, |
| 5394 | UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), |
| 5395 | &peer_rx_min_activatetime); |
| 5396 | if (ret) |
| 5397 | goto out; |
| 5398 | |
| 5399 | /* make sure proper unit conversion is applied */ |
| 5400 | tuned_pa_tactivate = |
| 5401 | ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US) |
| 5402 | / PA_TACTIVATE_TIME_UNIT_US); |
| 5403 | ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 5404 | tuned_pa_tactivate); |
| 5405 | |
| 5406 | out: |
| 5407 | return ret; |
| 5408 | } |
| 5409 | |
| 5410 | /** |
| 5411 | * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro |
| 5412 | * @hba: per-adapter instance |
| 5413 | * |
| 5414 | * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than |
| 5415 | * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's |
| 5416 | * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY. |
| 5417 | * This optimal value can help reduce the hibern8 exit latency. |
| 5418 | * |
| 5419 | * Returns zero on success, non-zero error value on failure. |
| 5420 | */ |
| 5421 | static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba) |
| 5422 | { |
| 5423 | int ret = 0; |
| 5424 | u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0; |
| 5425 | u32 max_hibern8_time, tuned_pa_hibern8time; |
| 5426 | |
| 5427 | ret = ufshcd_dme_get(hba, |
| 5428 | UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY, |
| 5429 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), |
| 5430 | &local_tx_hibern8_time_cap); |
| 5431 | if (ret) |
| 5432 | goto out; |
| 5433 | |
| 5434 | ret = ufshcd_dme_peer_get(hba, |
| 5435 | UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY, |
| 5436 | UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), |
| 5437 | &peer_rx_hibern8_time_cap); |
| 5438 | if (ret) |
| 5439 | goto out; |
| 5440 | |
| 5441 | max_hibern8_time = max(local_tx_hibern8_time_cap, |
| 5442 | peer_rx_hibern8_time_cap); |
| 5443 | /* make sure proper unit conversion is applied */ |
| 5444 | tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US) |
| 5445 | / PA_HIBERN8_TIME_UNIT_US); |
| 5446 | ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME), |
| 5447 | tuned_pa_hibern8time); |
| 5448 | out: |
| 5449 | return ret; |
| 5450 | } |
| 5451 | |
subhashj@codeaurora.org | c6a6db4 | 2016-11-23 16:32:08 -0800 | [diff] [blame] | 5452 | /** |
| 5453 | * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is |
| 5454 | * less than device PA_TACTIVATE time. |
| 5455 | * @hba: per-adapter instance |
| 5456 | * |
| 5457 | * Some UFS devices require host PA_TACTIVATE to be lower than device |
| 5458 | * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk |
| 5459 | * for such devices. |
| 5460 | * |
| 5461 | * Returns zero on success, non-zero error value on failure. |
| 5462 | */ |
| 5463 | static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba) |
| 5464 | { |
| 5465 | int ret = 0; |
| 5466 | u32 granularity, peer_granularity; |
| 5467 | u32 pa_tactivate, peer_pa_tactivate; |
| 5468 | u32 pa_tactivate_us, peer_pa_tactivate_us; |
| 5469 | u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100}; |
| 5470 | |
| 5471 | ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY), |
| 5472 | &granularity); |
| 5473 | if (ret) |
| 5474 | goto out; |
| 5475 | |
| 5476 | ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY), |
| 5477 | &peer_granularity); |
| 5478 | if (ret) |
| 5479 | goto out; |
| 5480 | |
| 5481 | if ((granularity < PA_GRANULARITY_MIN_VAL) || |
| 5482 | (granularity > PA_GRANULARITY_MAX_VAL)) { |
| 5483 | dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d", |
| 5484 | __func__, granularity); |
| 5485 | return -EINVAL; |
| 5486 | } |
| 5487 | |
| 5488 | if ((peer_granularity < PA_GRANULARITY_MIN_VAL) || |
| 5489 | (peer_granularity > PA_GRANULARITY_MAX_VAL)) { |
| 5490 | dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d", |
| 5491 | __func__, peer_granularity); |
| 5492 | return -EINVAL; |
| 5493 | } |
| 5494 | |
| 5495 | ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate); |
| 5496 | if (ret) |
| 5497 | goto out; |
| 5498 | |
| 5499 | ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 5500 | &peer_pa_tactivate); |
| 5501 | if (ret) |
| 5502 | goto out; |
| 5503 | |
| 5504 | pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1]; |
| 5505 | peer_pa_tactivate_us = peer_pa_tactivate * |
| 5506 | gran_to_us_table[peer_granularity - 1]; |
| 5507 | |
| 5508 | if (pa_tactivate_us > peer_pa_tactivate_us) { |
| 5509 | u32 new_peer_pa_tactivate; |
| 5510 | |
| 5511 | new_peer_pa_tactivate = pa_tactivate_us / |
| 5512 | gran_to_us_table[peer_granularity - 1]; |
| 5513 | new_peer_pa_tactivate++; |
| 5514 | ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 5515 | new_peer_pa_tactivate); |
| 5516 | } |
| 5517 | |
| 5518 | out: |
| 5519 | return ret; |
| 5520 | } |
| 5521 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 5522 | static void ufshcd_tune_unipro_params(struct ufs_hba *hba) |
| 5523 | { |
| 5524 | if (ufshcd_is_unipro_pa_params_tuning_req(hba)) { |
| 5525 | ufshcd_tune_pa_tactivate(hba); |
| 5526 | ufshcd_tune_pa_hibern8time(hba); |
| 5527 | } |
| 5528 | |
| 5529 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE) |
| 5530 | /* set 1ms timeout for PA_TACTIVATE */ |
| 5531 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10); |
subhashj@codeaurora.org | c6a6db4 | 2016-11-23 16:32:08 -0800 | [diff] [blame] | 5532 | |
| 5533 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE) |
| 5534 | ufshcd_quirk_tune_host_pa_tactivate(hba); |
Subhash Jadavani | 56d4a18 | 2016-12-05 19:25:32 -0800 | [diff] [blame] | 5535 | |
| 5536 | ufshcd_vops_apply_dev_quirks(hba); |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 5537 | } |
| 5538 | |
| 5539 | /** |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 5540 | * ufshcd_probe_hba - probe hba to detect device and initialize |
| 5541 | * @hba: per-adapter instance |
| 5542 | * |
| 5543 | * Execute link-startup and verify device initialization |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5544 | */ |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 5545 | static int ufshcd_probe_hba(struct ufs_hba *hba) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5546 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5547 | int ret; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 5548 | ktime_t start = ktime_get(); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5549 | |
| 5550 | ret = ufshcd_link_startup(hba); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 5551 | if (ret) |
| 5552 | goto out; |
| 5553 | |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 5554 | /* set the default level for urgent bkops */ |
| 5555 | hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT; |
| 5556 | hba->is_urgent_bkops_lvl_checked = false; |
| 5557 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5558 | /* UniPro link is active now */ |
| 5559 | ufshcd_set_link_active(hba); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 5560 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 5561 | ret = ufshcd_verify_dev_init(hba); |
| 5562 | if (ret) |
| 5563 | goto out; |
| 5564 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 5565 | ret = ufshcd_complete_dev_init(hba); |
| 5566 | if (ret) |
| 5567 | goto out; |
| 5568 | |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 5569 | ufs_advertise_fixup_device(hba); |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 5570 | ufshcd_tune_unipro_params(hba); |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 5571 | |
| 5572 | ret = ufshcd_set_vccq_rail_unused(hba, |
| 5573 | (hba->dev_quirks & UFS_DEVICE_NO_VCCQ) ? true : false); |
| 5574 | if (ret) |
| 5575 | goto out; |
| 5576 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5577 | /* UFS device is also active now */ |
| 5578 | ufshcd_set_ufs_dev_active(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5579 | ufshcd_force_reset_auto_bkops(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5580 | hba->wlun_dev_clr_ua = true; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5581 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 5582 | if (ufshcd_get_max_pwr_mode(hba)) { |
| 5583 | dev_err(hba->dev, |
| 5584 | "%s: Failed getting max supported power mode\n", |
| 5585 | __func__); |
| 5586 | } else { |
| 5587 | ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info); |
Dov Levenglick | 8643ae6 | 2016-10-17 17:10:14 -0700 | [diff] [blame] | 5588 | if (ret) { |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 5589 | dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n", |
| 5590 | __func__, ret); |
Dov Levenglick | 8643ae6 | 2016-10-17 17:10:14 -0700 | [diff] [blame] | 5591 | goto out; |
| 5592 | } |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 5593 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5594 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 5595 | /* set the state as operational after switching to desired gear */ |
| 5596 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5597 | /* |
| 5598 | * If we are in error handling context or in power management callbacks |
| 5599 | * context, no need to scan the host |
| 5600 | */ |
| 5601 | if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) { |
| 5602 | bool flag; |
| 5603 | |
| 5604 | /* clear any previous UFS device information */ |
| 5605 | memset(&hba->dev_info, 0, sizeof(hba->dev_info)); |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 5606 | if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG, |
| 5607 | QUERY_FLAG_IDN_PWR_ON_WPE, &flag)) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5608 | hba->dev_info.f_power_on_wp_en = flag; |
| 5609 | |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 5610 | if (!hba->is_init_prefetch) |
| 5611 | ufshcd_init_icc_levels(hba); |
| 5612 | |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 5613 | /* Add required well known logical units to scsi mid layer */ |
| 5614 | if (ufshcd_scsi_add_wlus(hba)) |
| 5615 | goto out; |
| 5616 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5617 | scsi_scan_host(hba->host); |
| 5618 | pm_runtime_put_sync(hba->dev); |
| 5619 | } |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 5620 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 5621 | /* Resume devfreq after UFS device is detected */ |
| 5622 | if (ufshcd_is_clkscaling_supported(hba)) { |
| 5623 | ufshcd_resume_clkscaling(hba); |
| 5624 | hba->clk_scaling.is_allowed = true; |
| 5625 | } |
| 5626 | |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 5627 | if (!hba->is_init_prefetch) |
| 5628 | hba->is_init_prefetch = true; |
| 5629 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 5630 | out: |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 5631 | /* |
| 5632 | * If we failed to initialize the device or the device is not |
| 5633 | * present, turn off the power/clocks etc. |
| 5634 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5635 | if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) { |
| 5636 | pm_runtime_put_sync(hba->dev); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 5637 | ufshcd_hba_exit(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5638 | } |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 5639 | |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 5640 | trace_ufshcd_init(dev_name(hba->dev), ret, |
| 5641 | ktime_to_us(ktime_sub(ktime_get(), start)), |
| 5642 | hba->uic_link_state, hba->curr_dev_pwr_mode); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 5643 | return ret; |
| 5644 | } |
| 5645 | |
| 5646 | /** |
| 5647 | * ufshcd_async_scan - asynchronous execution for probing hba |
| 5648 | * @data: data pointer to pass to this function |
| 5649 | * @cookie: cookie data |
| 5650 | */ |
| 5651 | static void ufshcd_async_scan(void *data, async_cookie_t cookie) |
| 5652 | { |
| 5653 | struct ufs_hba *hba = (struct ufs_hba *)data; |
| 5654 | |
| 5655 | ufshcd_probe_hba(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5656 | } |
| 5657 | |
Yaniv Gardi | f550c65 | 2016-03-10 17:37:07 +0200 | [diff] [blame] | 5658 | static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd) |
| 5659 | { |
| 5660 | unsigned long flags; |
| 5661 | struct Scsi_Host *host; |
| 5662 | struct ufs_hba *hba; |
| 5663 | int index; |
| 5664 | bool found = false; |
| 5665 | |
| 5666 | if (!scmd || !scmd->device || !scmd->device->host) |
| 5667 | return BLK_EH_NOT_HANDLED; |
| 5668 | |
| 5669 | host = scmd->device->host; |
| 5670 | hba = shost_priv(host); |
| 5671 | if (!hba) |
| 5672 | return BLK_EH_NOT_HANDLED; |
| 5673 | |
| 5674 | spin_lock_irqsave(host->host_lock, flags); |
| 5675 | |
| 5676 | for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) { |
| 5677 | if (hba->lrb[index].cmd == scmd) { |
| 5678 | found = true; |
| 5679 | break; |
| 5680 | } |
| 5681 | } |
| 5682 | |
| 5683 | spin_unlock_irqrestore(host->host_lock, flags); |
| 5684 | |
| 5685 | /* |
| 5686 | * Bypass SCSI error handling and reset the block layer timer if this |
| 5687 | * SCSI command was not actually dispatched to UFS driver, otherwise |
| 5688 | * let SCSI layer handle the error as usual. |
| 5689 | */ |
| 5690 | return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER; |
| 5691 | } |
| 5692 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5693 | static struct scsi_host_template ufshcd_driver_template = { |
| 5694 | .module = THIS_MODULE, |
| 5695 | .name = UFSHCD, |
| 5696 | .proc_name = UFSHCD, |
| 5697 | .queuecommand = ufshcd_queuecommand, |
| 5698 | .slave_alloc = ufshcd_slave_alloc, |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 5699 | .slave_configure = ufshcd_slave_configure, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5700 | .slave_destroy = ufshcd_slave_destroy, |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 5701 | .change_queue_depth = ufshcd_change_queue_depth, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5702 | .eh_abort_handler = ufshcd_abort, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5703 | .eh_device_reset_handler = ufshcd_eh_device_reset_handler, |
| 5704 | .eh_host_reset_handler = ufshcd_eh_host_reset_handler, |
Yaniv Gardi | f550c65 | 2016-03-10 17:37:07 +0200 | [diff] [blame] | 5705 | .eh_timed_out = ufshcd_eh_timed_out, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5706 | .this_id = -1, |
| 5707 | .sg_tablesize = SG_ALL, |
| 5708 | .cmd_per_lun = UFSHCD_CMD_PER_LUN, |
| 5709 | .can_queue = UFSHCD_CAN_QUEUE, |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5710 | .max_host_blocked = 1, |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 5711 | .track_queue_depth = 1, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5712 | }; |
| 5713 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5714 | static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg, |
| 5715 | int ua) |
| 5716 | { |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 5717 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5718 | |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 5719 | if (!vreg) |
| 5720 | return 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5721 | |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 5722 | ret = regulator_set_load(vreg->reg, ua); |
| 5723 | if (ret < 0) { |
| 5724 | dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n", |
| 5725 | __func__, vreg->name, ua, ret); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5726 | } |
| 5727 | |
| 5728 | return ret; |
| 5729 | } |
| 5730 | |
| 5731 | static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba, |
| 5732 | struct ufs_vreg *vreg) |
| 5733 | { |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 5734 | if (!vreg) |
| 5735 | return 0; |
| 5736 | else if (vreg->unused) |
| 5737 | return 0; |
| 5738 | else |
| 5739 | return ufshcd_config_vreg_load(hba->dev, vreg, |
| 5740 | UFS_VREG_LPM_LOAD_UA); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5741 | } |
| 5742 | |
| 5743 | static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba, |
| 5744 | struct ufs_vreg *vreg) |
| 5745 | { |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 5746 | if (!vreg) |
| 5747 | return 0; |
| 5748 | else if (vreg->unused) |
| 5749 | return 0; |
| 5750 | else |
| 5751 | return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5752 | } |
| 5753 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 5754 | static int ufshcd_config_vreg(struct device *dev, |
| 5755 | struct ufs_vreg *vreg, bool on) |
| 5756 | { |
| 5757 | int ret = 0; |
| 5758 | struct regulator *reg = vreg->reg; |
| 5759 | const char *name = vreg->name; |
| 5760 | int min_uV, uA_load; |
| 5761 | |
| 5762 | BUG_ON(!vreg); |
| 5763 | |
| 5764 | if (regulator_count_voltages(reg) > 0) { |
| 5765 | min_uV = on ? vreg->min_uV : 0; |
| 5766 | ret = regulator_set_voltage(reg, min_uV, vreg->max_uV); |
| 5767 | if (ret) { |
| 5768 | dev_err(dev, "%s: %s set voltage failed, err=%d\n", |
| 5769 | __func__, name, ret); |
| 5770 | goto out; |
| 5771 | } |
| 5772 | |
| 5773 | uA_load = on ? vreg->max_uA : 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5774 | ret = ufshcd_config_vreg_load(dev, vreg, uA_load); |
| 5775 | if (ret) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 5776 | goto out; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 5777 | } |
| 5778 | out: |
| 5779 | return ret; |
| 5780 | } |
| 5781 | |
| 5782 | static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 5783 | { |
| 5784 | int ret = 0; |
| 5785 | |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 5786 | if (!vreg) |
| 5787 | goto out; |
| 5788 | else if (vreg->enabled || vreg->unused) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 5789 | goto out; |
| 5790 | |
| 5791 | ret = ufshcd_config_vreg(dev, vreg, true); |
| 5792 | if (!ret) |
| 5793 | ret = regulator_enable(vreg->reg); |
| 5794 | |
| 5795 | if (!ret) |
| 5796 | vreg->enabled = true; |
| 5797 | else |
| 5798 | dev_err(dev, "%s: %s enable failed, err=%d\n", |
| 5799 | __func__, vreg->name, ret); |
| 5800 | out: |
| 5801 | return ret; |
| 5802 | } |
| 5803 | |
| 5804 | static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 5805 | { |
| 5806 | int ret = 0; |
| 5807 | |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 5808 | if (!vreg) |
| 5809 | goto out; |
| 5810 | else if (!vreg->enabled || vreg->unused) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 5811 | goto out; |
| 5812 | |
| 5813 | ret = regulator_disable(vreg->reg); |
| 5814 | |
| 5815 | if (!ret) { |
| 5816 | /* ignore errors on applying disable config */ |
| 5817 | ufshcd_config_vreg(dev, vreg, false); |
| 5818 | vreg->enabled = false; |
| 5819 | } else { |
| 5820 | dev_err(dev, "%s: %s disable failed, err=%d\n", |
| 5821 | __func__, vreg->name, ret); |
| 5822 | } |
| 5823 | out: |
| 5824 | return ret; |
| 5825 | } |
| 5826 | |
| 5827 | static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on) |
| 5828 | { |
| 5829 | int ret = 0; |
| 5830 | struct device *dev = hba->dev; |
| 5831 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 5832 | |
| 5833 | if (!info) |
| 5834 | goto out; |
| 5835 | |
| 5836 | ret = ufshcd_toggle_vreg(dev, info->vcc, on); |
| 5837 | if (ret) |
| 5838 | goto out; |
| 5839 | |
| 5840 | ret = ufshcd_toggle_vreg(dev, info->vccq, on); |
| 5841 | if (ret) |
| 5842 | goto out; |
| 5843 | |
| 5844 | ret = ufshcd_toggle_vreg(dev, info->vccq2, on); |
| 5845 | if (ret) |
| 5846 | goto out; |
| 5847 | |
| 5848 | out: |
| 5849 | if (ret) { |
| 5850 | ufshcd_toggle_vreg(dev, info->vccq2, false); |
| 5851 | ufshcd_toggle_vreg(dev, info->vccq, false); |
| 5852 | ufshcd_toggle_vreg(dev, info->vcc, false); |
| 5853 | } |
| 5854 | return ret; |
| 5855 | } |
| 5856 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 5857 | static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on) |
| 5858 | { |
| 5859 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 5860 | |
| 5861 | if (info) |
| 5862 | return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on); |
| 5863 | |
| 5864 | return 0; |
| 5865 | } |
| 5866 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 5867 | static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 5868 | { |
| 5869 | int ret = 0; |
| 5870 | |
| 5871 | if (!vreg) |
| 5872 | goto out; |
| 5873 | |
| 5874 | vreg->reg = devm_regulator_get(dev, vreg->name); |
| 5875 | if (IS_ERR(vreg->reg)) { |
| 5876 | ret = PTR_ERR(vreg->reg); |
| 5877 | dev_err(dev, "%s: %s get failed, err=%d\n", |
| 5878 | __func__, vreg->name, ret); |
| 5879 | } |
| 5880 | out: |
| 5881 | return ret; |
| 5882 | } |
| 5883 | |
| 5884 | static int ufshcd_init_vreg(struct ufs_hba *hba) |
| 5885 | { |
| 5886 | int ret = 0; |
| 5887 | struct device *dev = hba->dev; |
| 5888 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 5889 | |
| 5890 | if (!info) |
| 5891 | goto out; |
| 5892 | |
| 5893 | ret = ufshcd_get_vreg(dev, info->vcc); |
| 5894 | if (ret) |
| 5895 | goto out; |
| 5896 | |
| 5897 | ret = ufshcd_get_vreg(dev, info->vccq); |
| 5898 | if (ret) |
| 5899 | goto out; |
| 5900 | |
| 5901 | ret = ufshcd_get_vreg(dev, info->vccq2); |
| 5902 | out: |
| 5903 | return ret; |
| 5904 | } |
| 5905 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 5906 | static int ufshcd_init_hba_vreg(struct ufs_hba *hba) |
| 5907 | { |
| 5908 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 5909 | |
| 5910 | if (info) |
| 5911 | return ufshcd_get_vreg(hba->dev, info->vdd_hba); |
| 5912 | |
| 5913 | return 0; |
| 5914 | } |
| 5915 | |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 5916 | static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused) |
| 5917 | { |
| 5918 | int ret = 0; |
| 5919 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 5920 | |
| 5921 | if (!info) |
| 5922 | goto out; |
| 5923 | else if (!info->vccq) |
| 5924 | goto out; |
| 5925 | |
| 5926 | if (unused) { |
| 5927 | /* shut off the rail here */ |
| 5928 | ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false); |
| 5929 | /* |
| 5930 | * Mark this rail as no longer used, so it doesn't get enabled |
| 5931 | * later by mistake |
| 5932 | */ |
| 5933 | if (!ret) |
| 5934 | info->vccq->unused = true; |
| 5935 | } else { |
| 5936 | /* |
| 5937 | * rail should have been already enabled hence just make sure |
| 5938 | * that unused flag is cleared. |
| 5939 | */ |
| 5940 | info->vccq->unused = false; |
| 5941 | } |
| 5942 | out: |
| 5943 | return ret; |
| 5944 | } |
| 5945 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5946 | static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on, |
| 5947 | bool skip_ref_clk) |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 5948 | { |
| 5949 | int ret = 0; |
| 5950 | struct ufs_clk_info *clki; |
| 5951 | struct list_head *head = &hba->clk_list_head; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5952 | unsigned long flags; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 5953 | |
| 5954 | if (!head || list_empty(head)) |
| 5955 | goto out; |
| 5956 | |
Subhash Jadavani | 1e879e8 | 2016-10-06 21:48:22 -0700 | [diff] [blame] | 5957 | ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE); |
| 5958 | if (ret) |
| 5959 | return ret; |
| 5960 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 5961 | list_for_each_entry(clki, head, list) { |
| 5962 | if (!IS_ERR_OR_NULL(clki->clk)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5963 | if (skip_ref_clk && !strcmp(clki->name, "ref_clk")) |
| 5964 | continue; |
| 5965 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 5966 | if (on && !clki->enabled) { |
| 5967 | ret = clk_prepare_enable(clki->clk); |
| 5968 | if (ret) { |
| 5969 | dev_err(hba->dev, "%s: %s prepare enable failed, %d\n", |
| 5970 | __func__, clki->name, ret); |
| 5971 | goto out; |
| 5972 | } |
| 5973 | } else if (!on && clki->enabled) { |
| 5974 | clk_disable_unprepare(clki->clk); |
| 5975 | } |
| 5976 | clki->enabled = on; |
| 5977 | dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__, |
| 5978 | clki->name, on ? "en" : "dis"); |
| 5979 | } |
| 5980 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5981 | |
Subhash Jadavani | 1e879e8 | 2016-10-06 21:48:22 -0700 | [diff] [blame] | 5982 | ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE); |
| 5983 | if (ret) |
| 5984 | return ret; |
| 5985 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 5986 | out: |
| 5987 | if (ret) { |
| 5988 | list_for_each_entry(clki, head, list) { |
| 5989 | if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled) |
| 5990 | clk_disable_unprepare(clki->clk); |
| 5991 | } |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 5992 | } else if (!ret && on) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5993 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5994 | hba->clk_gating.state = CLKS_ON; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 5995 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 5996 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5997 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 5998 | } |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 5999 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 6000 | return ret; |
| 6001 | } |
| 6002 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6003 | static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on) |
| 6004 | { |
| 6005 | return __ufshcd_setup_clocks(hba, on, false); |
| 6006 | } |
| 6007 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 6008 | static int ufshcd_init_clocks(struct ufs_hba *hba) |
| 6009 | { |
| 6010 | int ret = 0; |
| 6011 | struct ufs_clk_info *clki; |
| 6012 | struct device *dev = hba->dev; |
| 6013 | struct list_head *head = &hba->clk_list_head; |
| 6014 | |
| 6015 | if (!head || list_empty(head)) |
| 6016 | goto out; |
| 6017 | |
| 6018 | list_for_each_entry(clki, head, list) { |
| 6019 | if (!clki->name) |
| 6020 | continue; |
| 6021 | |
| 6022 | clki->clk = devm_clk_get(dev, clki->name); |
| 6023 | if (IS_ERR(clki->clk)) { |
| 6024 | ret = PTR_ERR(clki->clk); |
| 6025 | dev_err(dev, "%s: %s clk get failed, %d\n", |
| 6026 | __func__, clki->name, ret); |
| 6027 | goto out; |
| 6028 | } |
| 6029 | |
| 6030 | if (clki->max_freq) { |
| 6031 | ret = clk_set_rate(clki->clk, clki->max_freq); |
| 6032 | if (ret) { |
| 6033 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 6034 | __func__, clki->name, |
| 6035 | clki->max_freq, ret); |
| 6036 | goto out; |
| 6037 | } |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 6038 | clki->curr_freq = clki->max_freq; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 6039 | } |
| 6040 | dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__, |
| 6041 | clki->name, clk_get_rate(clki->clk)); |
| 6042 | } |
| 6043 | out: |
| 6044 | return ret; |
| 6045 | } |
| 6046 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6047 | static int ufshcd_variant_hba_init(struct ufs_hba *hba) |
| 6048 | { |
| 6049 | int err = 0; |
| 6050 | |
| 6051 | if (!hba->vops) |
| 6052 | goto out; |
| 6053 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 6054 | err = ufshcd_vops_init(hba); |
| 6055 | if (err) |
| 6056 | goto out; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6057 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 6058 | err = ufshcd_vops_setup_regulators(hba, true); |
| 6059 | if (err) |
| 6060 | goto out_exit; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6061 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6062 | goto out; |
| 6063 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6064 | out_exit: |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 6065 | ufshcd_vops_exit(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6066 | out: |
| 6067 | if (err) |
| 6068 | dev_err(hba->dev, "%s: variant %s init failed err %d\n", |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 6069 | __func__, ufshcd_get_var_name(hba), err); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6070 | return err; |
| 6071 | } |
| 6072 | |
| 6073 | static void ufshcd_variant_hba_exit(struct ufs_hba *hba) |
| 6074 | { |
| 6075 | if (!hba->vops) |
| 6076 | return; |
| 6077 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 6078 | ufshcd_vops_setup_regulators(hba, false); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6079 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 6080 | ufshcd_vops_exit(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6081 | } |
| 6082 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6083 | static int ufshcd_hba_init(struct ufs_hba *hba) |
| 6084 | { |
| 6085 | int err; |
| 6086 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 6087 | /* |
| 6088 | * Handle host controller power separately from the UFS device power |
| 6089 | * rails as it will help controlling the UFS host controller power |
| 6090 | * collapse easily which is different than UFS device power collapse. |
| 6091 | * Also, enable the host controller power before we go ahead with rest |
| 6092 | * of the initialization here. |
| 6093 | */ |
| 6094 | err = ufshcd_init_hba_vreg(hba); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6095 | if (err) |
| 6096 | goto out; |
| 6097 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 6098 | err = ufshcd_setup_hba_vreg(hba, true); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6099 | if (err) |
| 6100 | goto out; |
| 6101 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 6102 | err = ufshcd_init_clocks(hba); |
| 6103 | if (err) |
| 6104 | goto out_disable_hba_vreg; |
| 6105 | |
| 6106 | err = ufshcd_setup_clocks(hba, true); |
| 6107 | if (err) |
| 6108 | goto out_disable_hba_vreg; |
| 6109 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 6110 | err = ufshcd_init_vreg(hba); |
| 6111 | if (err) |
| 6112 | goto out_disable_clks; |
| 6113 | |
| 6114 | err = ufshcd_setup_vreg(hba, true); |
| 6115 | if (err) |
| 6116 | goto out_disable_clks; |
| 6117 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6118 | err = ufshcd_variant_hba_init(hba); |
| 6119 | if (err) |
| 6120 | goto out_disable_vreg; |
| 6121 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6122 | hba->is_powered = true; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6123 | goto out; |
| 6124 | |
| 6125 | out_disable_vreg: |
| 6126 | ufshcd_setup_vreg(hba, false); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 6127 | out_disable_clks: |
| 6128 | ufshcd_setup_clocks(hba, false); |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 6129 | out_disable_hba_vreg: |
| 6130 | ufshcd_setup_hba_vreg(hba, false); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6131 | out: |
| 6132 | return err; |
| 6133 | } |
| 6134 | |
| 6135 | static void ufshcd_hba_exit(struct ufs_hba *hba) |
| 6136 | { |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6137 | if (hba->is_powered) { |
| 6138 | ufshcd_variant_hba_exit(hba); |
| 6139 | ufshcd_setup_vreg(hba, false); |
Gilad Broner | a508253 | 2016-10-17 17:10:00 -0700 | [diff] [blame] | 6140 | ufshcd_suspend_clkscaling(hba); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6141 | ufshcd_setup_clocks(hba, false); |
| 6142 | ufshcd_setup_hba_vreg(hba, false); |
| 6143 | hba->is_powered = false; |
| 6144 | } |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6145 | } |
| 6146 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6147 | static int |
| 6148 | ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6149 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6150 | unsigned char cmd[6] = {REQUEST_SENSE, |
| 6151 | 0, |
| 6152 | 0, |
| 6153 | 0, |
Gilad Broner | dcea0bf | 2016-10-17 17:09:48 -0700 | [diff] [blame] | 6154 | UFSHCD_REQ_SENSE_SIZE, |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6155 | 0}; |
| 6156 | char *buffer; |
| 6157 | int ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6158 | |
Gilad Broner | dcea0bf | 2016-10-17 17:09:48 -0700 | [diff] [blame] | 6159 | buffer = kzalloc(UFSHCD_REQ_SENSE_SIZE, GFP_KERNEL); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6160 | if (!buffer) { |
| 6161 | ret = -ENOMEM; |
| 6162 | goto out; |
| 6163 | } |
| 6164 | |
| 6165 | ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer, |
Gilad Broner | dcea0bf | 2016-10-17 17:09:48 -0700 | [diff] [blame] | 6166 | UFSHCD_REQ_SENSE_SIZE, NULL, |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 6167 | msecs_to_jiffies(1000), 3, NULL, 0, RQF_PM); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6168 | if (ret) |
| 6169 | pr_err("%s: failed with err %d\n", __func__, ret); |
| 6170 | |
| 6171 | kfree(buffer); |
| 6172 | out: |
| 6173 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6174 | } |
| 6175 | |
| 6176 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6177 | * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device |
| 6178 | * power mode |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6179 | * @hba: per adapter instance |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6180 | * @pwr_mode: device power mode to set |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6181 | * |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6182 | * Returns 0 if requested power mode is set successfully |
| 6183 | * Returns non-zero if failed to set the requested power mode |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6184 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6185 | static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba, |
| 6186 | enum ufs_dev_pwr_mode pwr_mode) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6187 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6188 | unsigned char cmd[6] = { START_STOP }; |
| 6189 | struct scsi_sense_hdr sshdr; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6190 | struct scsi_device *sdp; |
| 6191 | unsigned long flags; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6192 | int ret; |
| 6193 | |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6194 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 6195 | sdp = hba->sdev_ufs_device; |
| 6196 | if (sdp) { |
| 6197 | ret = scsi_device_get(sdp); |
| 6198 | if (!ret && !scsi_device_online(sdp)) { |
| 6199 | ret = -ENODEV; |
| 6200 | scsi_device_put(sdp); |
| 6201 | } |
| 6202 | } else { |
| 6203 | ret = -ENODEV; |
| 6204 | } |
| 6205 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6206 | |
| 6207 | if (ret) |
| 6208 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6209 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6210 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6211 | * If scsi commands fail, the scsi mid-layer schedules scsi error- |
| 6212 | * handling, which would wait for host to be resumed. Since we know |
| 6213 | * we are functional while we are here, skip host resume in error |
| 6214 | * handling context. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6215 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6216 | hba->host->eh_noresume = 1; |
| 6217 | if (hba->wlun_dev_clr_ua) { |
| 6218 | ret = ufshcd_send_request_sense(hba, sdp); |
| 6219 | if (ret) |
| 6220 | goto out; |
| 6221 | /* Unit attention condition is cleared now */ |
| 6222 | hba->wlun_dev_clr_ua = false; |
| 6223 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6224 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6225 | cmd[4] = pwr_mode << 4; |
| 6226 | |
| 6227 | /* |
| 6228 | * Current function would be generally called from the power management |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 6229 | * callbacks hence set the RQF_PM flag so that it doesn't resume the |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6230 | * already suspended childs. |
| 6231 | */ |
| 6232 | ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr, |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 6233 | START_STOP_TIMEOUT, 0, NULL, 0, RQF_PM); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6234 | if (ret) { |
| 6235 | sdev_printk(KERN_WARNING, sdp, |
Hannes Reinecke | ef61329 | 2014-10-24 14:27:00 +0200 | [diff] [blame] | 6236 | "START_STOP failed for power mode: %d, result %x\n", |
| 6237 | pwr_mode, ret); |
Hannes Reinecke | 2104551 | 2015-01-08 07:43:46 +0100 | [diff] [blame] | 6238 | if (driver_byte(ret) & DRIVER_SENSE) |
| 6239 | scsi_print_sense_hdr(sdp, NULL, &sshdr); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6240 | } |
| 6241 | |
| 6242 | if (!ret) |
| 6243 | hba->curr_dev_pwr_mode = pwr_mode; |
| 6244 | out: |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6245 | scsi_device_put(sdp); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6246 | hba->host->eh_noresume = 0; |
| 6247 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6248 | } |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6249 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6250 | static int ufshcd_link_state_transition(struct ufs_hba *hba, |
| 6251 | enum uic_link_state req_link_state, |
| 6252 | int check_for_bkops) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 6253 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6254 | int ret = 0; |
| 6255 | |
| 6256 | if (req_link_state == hba->uic_link_state) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 6257 | return 0; |
| 6258 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6259 | if (req_link_state == UIC_LINK_HIBERN8_STATE) { |
| 6260 | ret = ufshcd_uic_hibern8_enter(hba); |
| 6261 | if (!ret) |
| 6262 | ufshcd_set_link_hibern8(hba); |
| 6263 | else |
| 6264 | goto out; |
| 6265 | } |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 6266 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6267 | * If autobkops is enabled, link can't be turned off because |
| 6268 | * turning off the link would also turn off the device. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 6269 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6270 | else if ((req_link_state == UIC_LINK_OFF_STATE) && |
| 6271 | (!check_for_bkops || (check_for_bkops && |
| 6272 | !hba->auto_bkops_enabled))) { |
| 6273 | /* |
Yaniv Gardi | f3099fb | 2016-03-10 17:37:17 +0200 | [diff] [blame] | 6274 | * Let's make sure that link is in low power mode, we are doing |
| 6275 | * this currently by putting the link in Hibern8. Otherway to |
| 6276 | * put the link in low power mode is to send the DME end point |
| 6277 | * to device and then send the DME reset command to local |
| 6278 | * unipro. But putting the link in hibern8 is much faster. |
| 6279 | */ |
| 6280 | ret = ufshcd_uic_hibern8_enter(hba); |
| 6281 | if (ret) |
| 6282 | goto out; |
| 6283 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6284 | * Change controller state to "reset state" which |
| 6285 | * should also put the link in off/reset state |
| 6286 | */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 6287 | ufshcd_hba_stop(hba, true); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6288 | /* |
| 6289 | * TODO: Check if we need any delay to make sure that |
| 6290 | * controller is reset |
| 6291 | */ |
| 6292 | ufshcd_set_link_off(hba); |
| 6293 | } |
| 6294 | |
| 6295 | out: |
| 6296 | return ret; |
| 6297 | } |
| 6298 | |
| 6299 | static void ufshcd_vreg_set_lpm(struct ufs_hba *hba) |
| 6300 | { |
| 6301 | /* |
Yaniv Gardi | b799fdf | 2016-03-10 17:37:18 +0200 | [diff] [blame] | 6302 | * It seems some UFS devices may keep drawing more than sleep current |
| 6303 | * (atleast for 500us) from UFS rails (especially from VCCQ rail). |
| 6304 | * To avoid this situation, add 2ms delay before putting these UFS |
| 6305 | * rails in LPM mode. |
| 6306 | */ |
| 6307 | if (!ufshcd_is_link_active(hba) && |
| 6308 | hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM) |
| 6309 | usleep_range(2000, 2100); |
| 6310 | |
| 6311 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6312 | * If UFS device is either in UFS_Sleep turn off VCC rail to save some |
| 6313 | * power. |
| 6314 | * |
| 6315 | * If UFS device and link is in OFF state, all power supplies (VCC, |
| 6316 | * VCCQ, VCCQ2) can be turned off if power on write protect is not |
| 6317 | * required. If UFS link is inactive (Hibern8 or OFF state) and device |
| 6318 | * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode. |
| 6319 | * |
| 6320 | * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway |
| 6321 | * in low power state which would save some power. |
| 6322 | */ |
| 6323 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) && |
| 6324 | !hba->dev_info.is_lu_power_on_wp) { |
| 6325 | ufshcd_setup_vreg(hba, false); |
| 6326 | } else if (!ufshcd_is_ufs_dev_active(hba)) { |
| 6327 | ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false); |
| 6328 | if (!ufshcd_is_link_active(hba)) { |
| 6329 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq); |
| 6330 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2); |
| 6331 | } |
| 6332 | } |
| 6333 | } |
| 6334 | |
| 6335 | static int ufshcd_vreg_set_hpm(struct ufs_hba *hba) |
| 6336 | { |
| 6337 | int ret = 0; |
| 6338 | |
| 6339 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) && |
| 6340 | !hba->dev_info.is_lu_power_on_wp) { |
| 6341 | ret = ufshcd_setup_vreg(hba, true); |
| 6342 | } else if (!ufshcd_is_ufs_dev_active(hba)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6343 | if (!ret && !ufshcd_is_link_active(hba)) { |
| 6344 | ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq); |
| 6345 | if (ret) |
| 6346 | goto vcc_disable; |
| 6347 | ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2); |
| 6348 | if (ret) |
| 6349 | goto vccq_lpm; |
| 6350 | } |
Subhash Jadavani | 69d72ac | 2016-10-27 17:26:24 -0700 | [diff] [blame] | 6351 | ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6352 | } |
| 6353 | goto out; |
| 6354 | |
| 6355 | vccq_lpm: |
| 6356 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq); |
| 6357 | vcc_disable: |
| 6358 | ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false); |
| 6359 | out: |
| 6360 | return ret; |
| 6361 | } |
| 6362 | |
| 6363 | static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba) |
| 6364 | { |
| 6365 | if (ufshcd_is_link_off(hba)) |
| 6366 | ufshcd_setup_hba_vreg(hba, false); |
| 6367 | } |
| 6368 | |
| 6369 | static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba) |
| 6370 | { |
| 6371 | if (ufshcd_is_link_off(hba)) |
| 6372 | ufshcd_setup_hba_vreg(hba, true); |
| 6373 | } |
| 6374 | |
| 6375 | /** |
| 6376 | * ufshcd_suspend - helper function for suspend operations |
| 6377 | * @hba: per adapter instance |
| 6378 | * @pm_op: desired low power operation type |
| 6379 | * |
| 6380 | * This function will try to put the UFS device and link into low power |
| 6381 | * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl" |
| 6382 | * (System PM level). |
| 6383 | * |
| 6384 | * If this function is called during shutdown, it will make sure that |
| 6385 | * both UFS device and UFS link is powered off. |
| 6386 | * |
| 6387 | * NOTE: UFS device & link must be active before we enter in this function. |
| 6388 | * |
| 6389 | * Returns 0 for success and non-zero for failure |
| 6390 | */ |
| 6391 | static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op) |
| 6392 | { |
| 6393 | int ret = 0; |
| 6394 | enum ufs_pm_level pm_lvl; |
| 6395 | enum ufs_dev_pwr_mode req_dev_pwr_mode; |
| 6396 | enum uic_link_state req_link_state; |
| 6397 | |
| 6398 | hba->pm_op_in_progress = 1; |
| 6399 | if (!ufshcd_is_shutdown_pm(pm_op)) { |
| 6400 | pm_lvl = ufshcd_is_runtime_pm(pm_op) ? |
| 6401 | hba->rpm_lvl : hba->spm_lvl; |
| 6402 | req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl); |
| 6403 | req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl); |
| 6404 | } else { |
| 6405 | req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE; |
| 6406 | req_link_state = UIC_LINK_OFF_STATE; |
| 6407 | } |
| 6408 | |
| 6409 | /* |
| 6410 | * If we can't transition into any of the low power modes |
| 6411 | * just gate the clocks. |
| 6412 | */ |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6413 | ufshcd_hold(hba, false); |
| 6414 | hba->clk_gating.is_suspended = true; |
| 6415 | |
Subhash Jadavani | d6fcf81 | 2016-10-27 17:26:09 -0700 | [diff] [blame] | 6416 | ufshcd_suspend_clkscaling(hba); |
| 6417 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6418 | if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE && |
| 6419 | req_link_state == UIC_LINK_ACTIVE_STATE) { |
| 6420 | goto disable_clks; |
| 6421 | } |
| 6422 | |
| 6423 | if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) && |
| 6424 | (req_link_state == hba->uic_link_state)) |
Subhash Jadavani | d6fcf81 | 2016-10-27 17:26:09 -0700 | [diff] [blame] | 6425 | goto enable_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6426 | |
| 6427 | /* UFS device & link must be active before we enter in this function */ |
| 6428 | if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) { |
| 6429 | ret = -EINVAL; |
Subhash Jadavani | d6fcf81 | 2016-10-27 17:26:09 -0700 | [diff] [blame] | 6430 | goto enable_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6431 | } |
| 6432 | |
| 6433 | if (ufshcd_is_runtime_pm(pm_op)) { |
Subhash Jadavani | 374a246 | 2014-09-25 15:32:35 +0300 | [diff] [blame] | 6434 | if (ufshcd_can_autobkops_during_suspend(hba)) { |
| 6435 | /* |
| 6436 | * The device is idle with no requests in the queue, |
| 6437 | * allow background operations if bkops status shows |
| 6438 | * that performance might be impacted. |
| 6439 | */ |
| 6440 | ret = ufshcd_urgent_bkops(hba); |
| 6441 | if (ret) |
| 6442 | goto enable_gating; |
| 6443 | } else { |
| 6444 | /* make sure that auto bkops is disabled */ |
| 6445 | ufshcd_disable_auto_bkops(hba); |
| 6446 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6447 | } |
| 6448 | |
| 6449 | if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) && |
| 6450 | ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) || |
| 6451 | !ufshcd_is_runtime_pm(pm_op))) { |
| 6452 | /* ensure that bkops is disabled */ |
| 6453 | ufshcd_disable_auto_bkops(hba); |
| 6454 | ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode); |
| 6455 | if (ret) |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6456 | goto enable_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6457 | } |
| 6458 | |
| 6459 | ret = ufshcd_link_state_transition(hba, req_link_state, 1); |
| 6460 | if (ret) |
| 6461 | goto set_dev_active; |
| 6462 | |
| 6463 | ufshcd_vreg_set_lpm(hba); |
| 6464 | |
| 6465 | disable_clks: |
| 6466 | /* |
| 6467 | * Call vendor specific suspend callback. As these callbacks may access |
| 6468 | * vendor specific host controller register space call them before the |
| 6469 | * host clocks are ON. |
| 6470 | */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 6471 | ret = ufshcd_vops_suspend(hba, pm_op); |
| 6472 | if (ret) |
| 6473 | goto set_link_active; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6474 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6475 | if (!ufshcd_is_link_active(hba)) |
| 6476 | ufshcd_setup_clocks(hba, false); |
| 6477 | else |
| 6478 | /* If link is active, device ref_clk can't be switched off */ |
| 6479 | __ufshcd_setup_clocks(hba, false, true); |
| 6480 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6481 | hba->clk_gating.state = CLKS_OFF; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 6482 | trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6483 | /* |
| 6484 | * Disable the host irq as host controller as there won't be any |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 6485 | * host controller transaction expected till resume. |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6486 | */ |
| 6487 | ufshcd_disable_irq(hba); |
| 6488 | /* Put the host controller in low power mode if possible */ |
| 6489 | ufshcd_hba_vreg_set_lpm(hba); |
| 6490 | goto out; |
| 6491 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6492 | set_link_active: |
Gilad Broner | a508253 | 2016-10-17 17:10:00 -0700 | [diff] [blame] | 6493 | ufshcd_resume_clkscaling(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6494 | ufshcd_vreg_set_hpm(hba); |
| 6495 | if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba)) |
| 6496 | ufshcd_set_link_active(hba); |
| 6497 | else if (ufshcd_is_link_off(hba)) |
| 6498 | ufshcd_host_reset_and_restore(hba); |
| 6499 | set_dev_active: |
| 6500 | if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE)) |
| 6501 | ufshcd_disable_auto_bkops(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6502 | enable_gating: |
Subhash Jadavani | d6fcf81 | 2016-10-27 17:26:09 -0700 | [diff] [blame] | 6503 | ufshcd_resume_clkscaling(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6504 | hba->clk_gating.is_suspended = false; |
| 6505 | ufshcd_release(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6506 | out: |
| 6507 | hba->pm_op_in_progress = 0; |
| 6508 | return ret; |
| 6509 | } |
| 6510 | |
| 6511 | /** |
| 6512 | * ufshcd_resume - helper function for resume operations |
| 6513 | * @hba: per adapter instance |
| 6514 | * @pm_op: runtime PM or system PM |
| 6515 | * |
| 6516 | * This function basically brings the UFS device, UniPro link and controller |
| 6517 | * to active state. |
| 6518 | * |
| 6519 | * Returns 0 for success and non-zero for failure |
| 6520 | */ |
| 6521 | static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op) |
| 6522 | { |
| 6523 | int ret; |
| 6524 | enum uic_link_state old_link_state; |
| 6525 | |
| 6526 | hba->pm_op_in_progress = 1; |
| 6527 | old_link_state = hba->uic_link_state; |
| 6528 | |
| 6529 | ufshcd_hba_vreg_set_hpm(hba); |
| 6530 | /* Make sure clocks are enabled before accessing controller */ |
| 6531 | ret = ufshcd_setup_clocks(hba, true); |
| 6532 | if (ret) |
| 6533 | goto out; |
| 6534 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6535 | /* enable the host irq as host controller would be active soon */ |
| 6536 | ret = ufshcd_enable_irq(hba); |
| 6537 | if (ret) |
| 6538 | goto disable_irq_and_vops_clks; |
| 6539 | |
| 6540 | ret = ufshcd_vreg_set_hpm(hba); |
| 6541 | if (ret) |
| 6542 | goto disable_irq_and_vops_clks; |
| 6543 | |
| 6544 | /* |
| 6545 | * Call vendor specific resume callback. As these callbacks may access |
| 6546 | * vendor specific host controller register space call them when the |
| 6547 | * host clocks are ON. |
| 6548 | */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 6549 | ret = ufshcd_vops_resume(hba, pm_op); |
| 6550 | if (ret) |
| 6551 | goto disable_vreg; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6552 | |
| 6553 | if (ufshcd_is_link_hibern8(hba)) { |
| 6554 | ret = ufshcd_uic_hibern8_exit(hba); |
| 6555 | if (!ret) |
| 6556 | ufshcd_set_link_active(hba); |
| 6557 | else |
| 6558 | goto vendor_suspend; |
| 6559 | } else if (ufshcd_is_link_off(hba)) { |
| 6560 | ret = ufshcd_host_reset_and_restore(hba); |
| 6561 | /* |
| 6562 | * ufshcd_host_reset_and_restore() should have already |
| 6563 | * set the link state as active |
| 6564 | */ |
| 6565 | if (ret || !ufshcd_is_link_active(hba)) |
| 6566 | goto vendor_suspend; |
| 6567 | } |
| 6568 | |
| 6569 | if (!ufshcd_is_ufs_dev_active(hba)) { |
| 6570 | ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE); |
| 6571 | if (ret) |
| 6572 | goto set_old_link_state; |
| 6573 | } |
| 6574 | |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 6575 | if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) |
| 6576 | ufshcd_enable_auto_bkops(hba); |
| 6577 | else |
| 6578 | /* |
| 6579 | * If BKOPs operations are urgently needed at this moment then |
| 6580 | * keep auto-bkops enabled or else disable it. |
| 6581 | */ |
| 6582 | ufshcd_urgent_bkops(hba); |
| 6583 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6584 | hba->clk_gating.is_suspended = false; |
| 6585 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 6586 | if (hba->clk_scaling.is_allowed) |
| 6587 | ufshcd_resume_clkscaling(hba); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 6588 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6589 | /* Schedule clock gating in case of no access to UFS device yet */ |
| 6590 | ufshcd_release(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6591 | goto out; |
| 6592 | |
| 6593 | set_old_link_state: |
| 6594 | ufshcd_link_state_transition(hba, old_link_state, 0); |
| 6595 | vendor_suspend: |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 6596 | ufshcd_vops_suspend(hba, pm_op); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6597 | disable_vreg: |
| 6598 | ufshcd_vreg_set_lpm(hba); |
| 6599 | disable_irq_and_vops_clks: |
| 6600 | ufshcd_disable_irq(hba); |
Gilad Broner | a508253 | 2016-10-17 17:10:00 -0700 | [diff] [blame] | 6601 | ufshcd_suspend_clkscaling(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6602 | ufshcd_setup_clocks(hba, false); |
| 6603 | out: |
| 6604 | hba->pm_op_in_progress = 0; |
| 6605 | return ret; |
| 6606 | } |
| 6607 | |
| 6608 | /** |
| 6609 | * ufshcd_system_suspend - system suspend routine |
| 6610 | * @hba: per adapter instance |
| 6611 | * @pm_op: runtime PM or system PM |
| 6612 | * |
| 6613 | * Check the description of ufshcd_suspend() function for more details. |
| 6614 | * |
| 6615 | * Returns 0 for success and non-zero for failure |
| 6616 | */ |
| 6617 | int ufshcd_system_suspend(struct ufs_hba *hba) |
| 6618 | { |
| 6619 | int ret = 0; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 6620 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6621 | |
| 6622 | if (!hba || !hba->is_powered) |
Dolev Raviv | 233b594 | 2014-10-23 13:25:14 +0300 | [diff] [blame] | 6623 | return 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6624 | |
subhashj@codeaurora.org | 0b25773 | 2016-11-23 16:33:08 -0800 | [diff] [blame] | 6625 | if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) == |
| 6626 | hba->curr_dev_pwr_mode) && |
| 6627 | (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) == |
| 6628 | hba->uic_link_state)) |
| 6629 | goto out; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6630 | |
subhashj@codeaurora.org | 0b25773 | 2016-11-23 16:33:08 -0800 | [diff] [blame] | 6631 | if (pm_runtime_suspended(hba->dev)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6632 | /* |
| 6633 | * UFS device and/or UFS link low power states during runtime |
| 6634 | * suspend seems to be different than what is expected during |
| 6635 | * system suspend. Hence runtime resume the devic & link and |
| 6636 | * let the system suspend low power states to take effect. |
| 6637 | * TODO: If resume takes longer time, we might have optimize |
| 6638 | * it in future by not resuming everything if possible. |
| 6639 | */ |
| 6640 | ret = ufshcd_runtime_resume(hba); |
| 6641 | if (ret) |
| 6642 | goto out; |
| 6643 | } |
| 6644 | |
| 6645 | ret = ufshcd_suspend(hba, UFS_SYSTEM_PM); |
| 6646 | out: |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 6647 | trace_ufshcd_system_suspend(dev_name(hba->dev), ret, |
| 6648 | ktime_to_us(ktime_sub(ktime_get(), start)), |
| 6649 | hba->uic_link_state, hba->curr_dev_pwr_mode); |
Dolev Raviv | e785060 | 2014-09-25 15:32:36 +0300 | [diff] [blame] | 6650 | if (!ret) |
| 6651 | hba->is_sys_suspended = true; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6652 | return ret; |
| 6653 | } |
| 6654 | EXPORT_SYMBOL(ufshcd_system_suspend); |
| 6655 | |
| 6656 | /** |
| 6657 | * ufshcd_system_resume - system resume routine |
| 6658 | * @hba: per adapter instance |
| 6659 | * |
| 6660 | * Returns 0 for success and non-zero for failure |
| 6661 | */ |
| 6662 | |
| 6663 | int ufshcd_system_resume(struct ufs_hba *hba) |
| 6664 | { |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 6665 | int ret = 0; |
| 6666 | ktime_t start = ktime_get(); |
| 6667 | |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 6668 | if (!hba) |
| 6669 | return -EINVAL; |
| 6670 | |
| 6671 | if (!hba->is_powered || pm_runtime_suspended(hba->dev)) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6672 | /* |
| 6673 | * Let the runtime resume take care of resuming |
| 6674 | * if runtime suspended. |
| 6675 | */ |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 6676 | goto out; |
| 6677 | else |
| 6678 | ret = ufshcd_resume(hba, UFS_SYSTEM_PM); |
| 6679 | out: |
| 6680 | trace_ufshcd_system_resume(dev_name(hba->dev), ret, |
| 6681 | ktime_to_us(ktime_sub(ktime_get(), start)), |
| 6682 | hba->uic_link_state, hba->curr_dev_pwr_mode); |
| 6683 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6684 | } |
| 6685 | EXPORT_SYMBOL(ufshcd_system_resume); |
| 6686 | |
| 6687 | /** |
| 6688 | * ufshcd_runtime_suspend - runtime suspend routine |
| 6689 | * @hba: per adapter instance |
| 6690 | * |
| 6691 | * Check the description of ufshcd_suspend() function for more details. |
| 6692 | * |
| 6693 | * Returns 0 for success and non-zero for failure |
| 6694 | */ |
| 6695 | int ufshcd_runtime_suspend(struct ufs_hba *hba) |
| 6696 | { |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 6697 | int ret = 0; |
| 6698 | ktime_t start = ktime_get(); |
| 6699 | |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 6700 | if (!hba) |
| 6701 | return -EINVAL; |
| 6702 | |
| 6703 | if (!hba->is_powered) |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 6704 | goto out; |
| 6705 | else |
| 6706 | ret = ufshcd_suspend(hba, UFS_RUNTIME_PM); |
| 6707 | out: |
| 6708 | trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret, |
| 6709 | ktime_to_us(ktime_sub(ktime_get(), start)), |
| 6710 | hba->uic_link_state, hba->curr_dev_pwr_mode); |
| 6711 | return ret; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 6712 | } |
| 6713 | EXPORT_SYMBOL(ufshcd_runtime_suspend); |
| 6714 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6715 | /** |
| 6716 | * ufshcd_runtime_resume - runtime resume routine |
| 6717 | * @hba: per adapter instance |
| 6718 | * |
| 6719 | * This function basically brings the UFS device, UniPro link and controller |
| 6720 | * to active state. Following operations are done in this function: |
| 6721 | * |
| 6722 | * 1. Turn on all the controller related clocks |
| 6723 | * 2. Bring the UniPro link out of Hibernate state |
| 6724 | * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device |
| 6725 | * to active state. |
| 6726 | * 4. If auto-bkops is enabled on the device, disable it. |
| 6727 | * |
| 6728 | * So following would be the possible power state after this function return |
| 6729 | * successfully: |
| 6730 | * S1: UFS device in Active state with VCC rail ON |
| 6731 | * UniPro link in Active state |
| 6732 | * All the UFS/UniPro controller clocks are ON |
| 6733 | * |
| 6734 | * Returns 0 for success and non-zero for failure |
| 6735 | */ |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 6736 | int ufshcd_runtime_resume(struct ufs_hba *hba) |
| 6737 | { |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 6738 | int ret = 0; |
| 6739 | ktime_t start = ktime_get(); |
| 6740 | |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 6741 | if (!hba) |
| 6742 | return -EINVAL; |
| 6743 | |
| 6744 | if (!hba->is_powered) |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 6745 | goto out; |
| 6746 | else |
| 6747 | ret = ufshcd_resume(hba, UFS_RUNTIME_PM); |
| 6748 | out: |
| 6749 | trace_ufshcd_runtime_resume(dev_name(hba->dev), ret, |
| 6750 | ktime_to_us(ktime_sub(ktime_get(), start)), |
| 6751 | hba->uic_link_state, hba->curr_dev_pwr_mode); |
| 6752 | return ret; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 6753 | } |
| 6754 | EXPORT_SYMBOL(ufshcd_runtime_resume); |
| 6755 | |
| 6756 | int ufshcd_runtime_idle(struct ufs_hba *hba) |
| 6757 | { |
| 6758 | return 0; |
| 6759 | } |
| 6760 | EXPORT_SYMBOL(ufshcd_runtime_idle); |
| 6761 | |
subhashj@codeaurora.org | 09690d5 | 2016-12-22 18:41:00 -0800 | [diff] [blame] | 6762 | static inline ssize_t ufshcd_pm_lvl_store(struct device *dev, |
| 6763 | struct device_attribute *attr, |
| 6764 | const char *buf, size_t count, |
| 6765 | bool rpm) |
| 6766 | { |
| 6767 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 6768 | unsigned long flags, value; |
| 6769 | |
| 6770 | if (kstrtoul(buf, 0, &value)) |
| 6771 | return -EINVAL; |
| 6772 | |
| 6773 | if ((value < UFS_PM_LVL_0) || (value >= UFS_PM_LVL_MAX)) |
| 6774 | return -EINVAL; |
| 6775 | |
| 6776 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 6777 | if (rpm) |
| 6778 | hba->rpm_lvl = value; |
| 6779 | else |
| 6780 | hba->spm_lvl = value; |
| 6781 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6782 | return count; |
| 6783 | } |
| 6784 | |
| 6785 | static ssize_t ufshcd_rpm_lvl_show(struct device *dev, |
| 6786 | struct device_attribute *attr, char *buf) |
| 6787 | { |
| 6788 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 6789 | int curr_len; |
| 6790 | u8 lvl; |
| 6791 | |
| 6792 | curr_len = snprintf(buf, PAGE_SIZE, |
| 6793 | "\nCurrent Runtime PM level [%d] => dev_state [%s] link_state [%s]\n", |
| 6794 | hba->rpm_lvl, |
| 6795 | ufschd_ufs_dev_pwr_mode_to_string( |
| 6796 | ufs_pm_lvl_states[hba->rpm_lvl].dev_state), |
| 6797 | ufschd_uic_link_state_to_string( |
| 6798 | ufs_pm_lvl_states[hba->rpm_lvl].link_state)); |
| 6799 | |
| 6800 | curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len), |
| 6801 | "\nAll available Runtime PM levels info:\n"); |
| 6802 | for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) |
| 6803 | curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len), |
| 6804 | "\tRuntime PM level [%d] => dev_state [%s] link_state [%s]\n", |
| 6805 | lvl, |
| 6806 | ufschd_ufs_dev_pwr_mode_to_string( |
| 6807 | ufs_pm_lvl_states[lvl].dev_state), |
| 6808 | ufschd_uic_link_state_to_string( |
| 6809 | ufs_pm_lvl_states[lvl].link_state)); |
| 6810 | |
| 6811 | return curr_len; |
| 6812 | } |
| 6813 | |
| 6814 | static ssize_t ufshcd_rpm_lvl_store(struct device *dev, |
| 6815 | struct device_attribute *attr, const char *buf, size_t count) |
| 6816 | { |
| 6817 | return ufshcd_pm_lvl_store(dev, attr, buf, count, true); |
| 6818 | } |
| 6819 | |
| 6820 | static void ufshcd_add_rpm_lvl_sysfs_nodes(struct ufs_hba *hba) |
| 6821 | { |
| 6822 | hba->rpm_lvl_attr.show = ufshcd_rpm_lvl_show; |
| 6823 | hba->rpm_lvl_attr.store = ufshcd_rpm_lvl_store; |
| 6824 | sysfs_attr_init(&hba->rpm_lvl_attr.attr); |
| 6825 | hba->rpm_lvl_attr.attr.name = "rpm_lvl"; |
| 6826 | hba->rpm_lvl_attr.attr.mode = 0644; |
| 6827 | if (device_create_file(hba->dev, &hba->rpm_lvl_attr)) |
| 6828 | dev_err(hba->dev, "Failed to create sysfs for rpm_lvl\n"); |
| 6829 | } |
| 6830 | |
| 6831 | static ssize_t ufshcd_spm_lvl_show(struct device *dev, |
| 6832 | struct device_attribute *attr, char *buf) |
| 6833 | { |
| 6834 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 6835 | int curr_len; |
| 6836 | u8 lvl; |
| 6837 | |
| 6838 | curr_len = snprintf(buf, PAGE_SIZE, |
| 6839 | "\nCurrent System PM level [%d] => dev_state [%s] link_state [%s]\n", |
| 6840 | hba->spm_lvl, |
| 6841 | ufschd_ufs_dev_pwr_mode_to_string( |
| 6842 | ufs_pm_lvl_states[hba->spm_lvl].dev_state), |
| 6843 | ufschd_uic_link_state_to_string( |
| 6844 | ufs_pm_lvl_states[hba->spm_lvl].link_state)); |
| 6845 | |
| 6846 | curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len), |
| 6847 | "\nAll available System PM levels info:\n"); |
| 6848 | for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) |
| 6849 | curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len), |
| 6850 | "\tSystem PM level [%d] => dev_state [%s] link_state [%s]\n", |
| 6851 | lvl, |
| 6852 | ufschd_ufs_dev_pwr_mode_to_string( |
| 6853 | ufs_pm_lvl_states[lvl].dev_state), |
| 6854 | ufschd_uic_link_state_to_string( |
| 6855 | ufs_pm_lvl_states[lvl].link_state)); |
| 6856 | |
| 6857 | return curr_len; |
| 6858 | } |
| 6859 | |
| 6860 | static ssize_t ufshcd_spm_lvl_store(struct device *dev, |
| 6861 | struct device_attribute *attr, const char *buf, size_t count) |
| 6862 | { |
| 6863 | return ufshcd_pm_lvl_store(dev, attr, buf, count, false); |
| 6864 | } |
| 6865 | |
| 6866 | static void ufshcd_add_spm_lvl_sysfs_nodes(struct ufs_hba *hba) |
| 6867 | { |
| 6868 | hba->spm_lvl_attr.show = ufshcd_spm_lvl_show; |
| 6869 | hba->spm_lvl_attr.store = ufshcd_spm_lvl_store; |
| 6870 | sysfs_attr_init(&hba->spm_lvl_attr.attr); |
| 6871 | hba->spm_lvl_attr.attr.name = "spm_lvl"; |
| 6872 | hba->spm_lvl_attr.attr.mode = 0644; |
| 6873 | if (device_create_file(hba->dev, &hba->spm_lvl_attr)) |
| 6874 | dev_err(hba->dev, "Failed to create sysfs for spm_lvl\n"); |
| 6875 | } |
| 6876 | |
| 6877 | static inline void ufshcd_add_sysfs_nodes(struct ufs_hba *hba) |
| 6878 | { |
| 6879 | ufshcd_add_rpm_lvl_sysfs_nodes(hba); |
| 6880 | ufshcd_add_spm_lvl_sysfs_nodes(hba); |
| 6881 | } |
| 6882 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6883 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6884 | * ufshcd_shutdown - shutdown routine |
| 6885 | * @hba: per adapter instance |
| 6886 | * |
| 6887 | * This function would power off both UFS device and UFS link. |
| 6888 | * |
| 6889 | * Returns 0 always to allow force shutdown even in case of errors. |
| 6890 | */ |
| 6891 | int ufshcd_shutdown(struct ufs_hba *hba) |
| 6892 | { |
| 6893 | int ret = 0; |
| 6894 | |
| 6895 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba)) |
| 6896 | goto out; |
| 6897 | |
| 6898 | if (pm_runtime_suspended(hba->dev)) { |
| 6899 | ret = ufshcd_runtime_resume(hba); |
| 6900 | if (ret) |
| 6901 | goto out; |
| 6902 | } |
| 6903 | |
| 6904 | ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM); |
| 6905 | out: |
| 6906 | if (ret) |
| 6907 | dev_err(hba->dev, "%s failed, err %d\n", __func__, ret); |
| 6908 | /* allow force shutdown even in case of errors */ |
| 6909 | return 0; |
| 6910 | } |
| 6911 | EXPORT_SYMBOL(ufshcd_shutdown); |
| 6912 | |
| 6913 | /** |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6914 | * ufshcd_remove - de-allocate SCSI host and host memory space |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6915 | * data structure memory |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6916 | * @hba - per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6917 | */ |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6918 | void ufshcd_remove(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6919 | { |
Akinobu Mita | cfdf9c9 | 2013-07-30 00:36:03 +0530 | [diff] [blame] | 6920 | scsi_remove_host(hba->host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6921 | /* disable interrupts */ |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 6922 | ufshcd_disable_intr(hba, hba->intr_mask); |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 6923 | ufshcd_hba_stop(hba, true); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6924 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6925 | ufshcd_exit_clk_gating(hba); |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 6926 | if (ufshcd_is_clkscaling_supported(hba)) |
| 6927 | device_remove_file(hba->dev, &hba->clk_scaling.enable_attr); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6928 | ufshcd_hba_exit(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6929 | } |
| 6930 | EXPORT_SYMBOL_GPL(ufshcd_remove); |
| 6931 | |
| 6932 | /** |
Yaniv Gardi | 47555a5 | 2015-10-28 13:15:49 +0200 | [diff] [blame] | 6933 | * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA) |
| 6934 | * @hba: pointer to Host Bus Adapter (HBA) |
| 6935 | */ |
| 6936 | void ufshcd_dealloc_host(struct ufs_hba *hba) |
| 6937 | { |
| 6938 | scsi_host_put(hba->host); |
| 6939 | } |
| 6940 | EXPORT_SYMBOL_GPL(ufshcd_dealloc_host); |
| 6941 | |
| 6942 | /** |
Akinobu Mita | ca3d7bf | 2014-07-13 21:24:46 +0900 | [diff] [blame] | 6943 | * ufshcd_set_dma_mask - Set dma mask based on the controller |
| 6944 | * addressing capability |
| 6945 | * @hba: per adapter instance |
| 6946 | * |
| 6947 | * Returns 0 for success, non-zero for failure |
| 6948 | */ |
| 6949 | static int ufshcd_set_dma_mask(struct ufs_hba *hba) |
| 6950 | { |
| 6951 | if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) { |
| 6952 | if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64))) |
| 6953 | return 0; |
| 6954 | } |
| 6955 | return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32)); |
| 6956 | } |
| 6957 | |
| 6958 | /** |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6959 | * ufshcd_alloc_host - allocate Host Bus Adapter (HBA) |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6960 | * @dev: pointer to device handle |
| 6961 | * @hba_handle: driver private handle |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6962 | * Returns 0 on success, non-zero value on failure |
| 6963 | */ |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6964 | int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6965 | { |
| 6966 | struct Scsi_Host *host; |
| 6967 | struct ufs_hba *hba; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6968 | int err = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6969 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6970 | if (!dev) { |
| 6971 | dev_err(dev, |
| 6972 | "Invalid memory reference for dev is NULL\n"); |
| 6973 | err = -ENODEV; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6974 | goto out_error; |
| 6975 | } |
| 6976 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6977 | host = scsi_host_alloc(&ufshcd_driver_template, |
| 6978 | sizeof(struct ufs_hba)); |
| 6979 | if (!host) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6980 | dev_err(dev, "scsi_host_alloc failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6981 | err = -ENOMEM; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6982 | goto out_error; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6983 | } |
| 6984 | hba = shost_priv(host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6985 | hba->host = host; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6986 | hba->dev = dev; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6987 | *hba_handle = hba; |
| 6988 | |
| 6989 | out_error: |
| 6990 | return err; |
| 6991 | } |
| 6992 | EXPORT_SYMBOL(ufshcd_alloc_host); |
| 6993 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 6994 | static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up) |
| 6995 | { |
| 6996 | int ret = 0; |
| 6997 | struct ufs_clk_info *clki; |
| 6998 | struct list_head *head = &hba->clk_list_head; |
| 6999 | |
| 7000 | if (!head || list_empty(head)) |
| 7001 | goto out; |
| 7002 | |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 7003 | ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE); |
| 7004 | if (ret) |
| 7005 | return ret; |
| 7006 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 7007 | list_for_each_entry(clki, head, list) { |
| 7008 | if (!IS_ERR_OR_NULL(clki->clk)) { |
| 7009 | if (scale_up && clki->max_freq) { |
| 7010 | if (clki->curr_freq == clki->max_freq) |
| 7011 | continue; |
| 7012 | ret = clk_set_rate(clki->clk, clki->max_freq); |
| 7013 | if (ret) { |
| 7014 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 7015 | __func__, clki->name, |
| 7016 | clki->max_freq, ret); |
| 7017 | break; |
| 7018 | } |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7019 | trace_ufshcd_clk_scaling(dev_name(hba->dev), |
| 7020 | "scaled up", clki->name, |
| 7021 | clki->curr_freq, |
| 7022 | clki->max_freq); |
| 7023 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 7024 | clki->curr_freq = clki->max_freq; |
| 7025 | |
| 7026 | } else if (!scale_up && clki->min_freq) { |
| 7027 | if (clki->curr_freq == clki->min_freq) |
| 7028 | continue; |
| 7029 | ret = clk_set_rate(clki->clk, clki->min_freq); |
| 7030 | if (ret) { |
| 7031 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 7032 | __func__, clki->name, |
| 7033 | clki->min_freq, ret); |
| 7034 | break; |
| 7035 | } |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7036 | trace_ufshcd_clk_scaling(dev_name(hba->dev), |
| 7037 | "scaled down", clki->name, |
| 7038 | clki->curr_freq, |
| 7039 | clki->min_freq); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 7040 | clki->curr_freq = clki->min_freq; |
| 7041 | } |
| 7042 | } |
| 7043 | dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__, |
| 7044 | clki->name, clk_get_rate(clki->clk)); |
| 7045 | } |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 7046 | |
| 7047 | ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE); |
| 7048 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 7049 | out: |
| 7050 | return ret; |
| 7051 | } |
| 7052 | |
| 7053 | static int ufshcd_devfreq_target(struct device *dev, |
| 7054 | unsigned long *freq, u32 flags) |
| 7055 | { |
| 7056 | int err = 0; |
| 7057 | struct ufs_hba *hba = dev_get_drvdata(dev); |
Subhash Jadavani | 30fc33f | 2016-10-27 17:25:47 -0700 | [diff] [blame] | 7058 | bool release_clk_hold = false; |
| 7059 | unsigned long irq_flags; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 7060 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 7061 | if (!ufshcd_is_clkscaling_supported(hba)) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 7062 | return -EINVAL; |
| 7063 | |
Subhash Jadavani | 30fc33f | 2016-10-27 17:25:47 -0700 | [diff] [blame] | 7064 | spin_lock_irqsave(hba->host->host_lock, irq_flags); |
| 7065 | if (ufshcd_eh_in_progress(hba)) { |
| 7066 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 7067 | return 0; |
| 7068 | } |
| 7069 | |
| 7070 | if (ufshcd_is_clkgating_allowed(hba) && |
| 7071 | (hba->clk_gating.state != CLKS_ON)) { |
| 7072 | if (cancel_delayed_work(&hba->clk_gating.gate_work)) { |
| 7073 | /* hold the vote until the scaling work is completed */ |
| 7074 | hba->clk_gating.active_reqs++; |
| 7075 | release_clk_hold = true; |
| 7076 | hba->clk_gating.state = CLKS_ON; |
| 7077 | } else { |
| 7078 | /* |
| 7079 | * Clock gating work seems to be running in parallel |
| 7080 | * hence skip scaling work to avoid deadlock between |
| 7081 | * current scaling work and gating work. |
| 7082 | */ |
| 7083 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 7084 | return 0; |
| 7085 | } |
| 7086 | } |
| 7087 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 7088 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 7089 | if (*freq == UINT_MAX) |
| 7090 | err = ufshcd_scale_clks(hba, true); |
| 7091 | else if (*freq == 0) |
| 7092 | err = ufshcd_scale_clks(hba, false); |
| 7093 | |
Subhash Jadavani | 30fc33f | 2016-10-27 17:25:47 -0700 | [diff] [blame] | 7094 | spin_lock_irqsave(hba->host->host_lock, irq_flags); |
| 7095 | if (release_clk_hold) |
| 7096 | __ufshcd_release(hba); |
| 7097 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 7098 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 7099 | return err; |
| 7100 | } |
| 7101 | |
| 7102 | static int ufshcd_devfreq_get_dev_status(struct device *dev, |
| 7103 | struct devfreq_dev_status *stat) |
| 7104 | { |
| 7105 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 7106 | struct ufs_clk_scaling *scaling = &hba->clk_scaling; |
| 7107 | unsigned long flags; |
| 7108 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 7109 | if (!ufshcd_is_clkscaling_supported(hba)) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 7110 | return -EINVAL; |
| 7111 | |
| 7112 | memset(stat, 0, sizeof(*stat)); |
| 7113 | |
| 7114 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 7115 | if (!scaling->window_start_t) |
| 7116 | goto start_window; |
| 7117 | |
| 7118 | if (scaling->is_busy_started) |
| 7119 | scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(), |
| 7120 | scaling->busy_start_t)); |
| 7121 | |
| 7122 | stat->total_time = jiffies_to_usecs((long)jiffies - |
| 7123 | (long)scaling->window_start_t); |
| 7124 | stat->busy_time = scaling->tot_busy_t; |
| 7125 | start_window: |
| 7126 | scaling->window_start_t = jiffies; |
| 7127 | scaling->tot_busy_t = 0; |
| 7128 | |
| 7129 | if (hba->outstanding_reqs) { |
| 7130 | scaling->busy_start_t = ktime_get(); |
| 7131 | scaling->is_busy_started = true; |
| 7132 | } else { |
Thomas Gleixner | 8b0e195 | 2016-12-25 12:30:41 +0100 | [diff] [blame] | 7133 | scaling->busy_start_t = 0; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 7134 | scaling->is_busy_started = false; |
| 7135 | } |
| 7136 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 7137 | return 0; |
| 7138 | } |
| 7139 | |
| 7140 | static struct devfreq_dev_profile ufs_devfreq_profile = { |
| 7141 | .polling_ms = 100, |
| 7142 | .target = ufshcd_devfreq_target, |
| 7143 | .get_dev_status = ufshcd_devfreq_get_dev_status, |
| 7144 | }; |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 7145 | static void ufshcd_clkscaling_init_sysfs(struct ufs_hba *hba) |
| 7146 | { |
| 7147 | hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show; |
| 7148 | hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store; |
| 7149 | sysfs_attr_init(&hba->clk_scaling.enable_attr.attr); |
| 7150 | hba->clk_scaling.enable_attr.attr.name = "clkscale_enable"; |
| 7151 | hba->clk_scaling.enable_attr.attr.mode = 0644; |
| 7152 | if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr)) |
| 7153 | dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n"); |
| 7154 | } |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 7155 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7156 | /** |
| 7157 | * ufshcd_init - Driver initialization routine |
| 7158 | * @hba: per-adapter instance |
| 7159 | * @mmio_base: base register address |
| 7160 | * @irq: Interrupt line of device |
| 7161 | * Returns 0 on success, non-zero value on failure |
| 7162 | */ |
| 7163 | int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq) |
| 7164 | { |
| 7165 | int err; |
| 7166 | struct Scsi_Host *host = hba->host; |
| 7167 | struct device *dev = hba->dev; |
| 7168 | |
| 7169 | if (!mmio_base) { |
| 7170 | dev_err(hba->dev, |
| 7171 | "Invalid memory reference for mmio_base is NULL\n"); |
| 7172 | err = -ENODEV; |
| 7173 | goto out_error; |
| 7174 | } |
| 7175 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7176 | hba->mmio_base = mmio_base; |
| 7177 | hba->irq = irq; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7178 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7179 | err = ufshcd_hba_init(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7180 | if (err) |
| 7181 | goto out_error; |
| 7182 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7183 | /* Read capabilities registers */ |
| 7184 | ufshcd_hba_capabilities(hba); |
| 7185 | |
| 7186 | /* Get UFS version supported by the controller */ |
| 7187 | hba->ufs_version = ufshcd_get_ufs_version(hba); |
| 7188 | |
Yaniv Gardi | c01848c | 2016-12-05 19:25:02 -0800 | [diff] [blame] | 7189 | if ((hba->ufs_version != UFSHCI_VERSION_10) && |
| 7190 | (hba->ufs_version != UFSHCI_VERSION_11) && |
| 7191 | (hba->ufs_version != UFSHCI_VERSION_20) && |
| 7192 | (hba->ufs_version != UFSHCI_VERSION_21)) |
| 7193 | dev_err(hba->dev, "invalid UFS version 0x%x\n", |
| 7194 | hba->ufs_version); |
| 7195 | |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 7196 | /* Get Interrupt bit mask per version */ |
| 7197 | hba->intr_mask = ufshcd_get_intr_mask(hba); |
| 7198 | |
Akinobu Mita | ca3d7bf | 2014-07-13 21:24:46 +0900 | [diff] [blame] | 7199 | err = ufshcd_set_dma_mask(hba); |
| 7200 | if (err) { |
| 7201 | dev_err(hba->dev, "set dma mask failed\n"); |
| 7202 | goto out_disable; |
| 7203 | } |
| 7204 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7205 | /* Allocate memory for host memory space */ |
| 7206 | err = ufshcd_memory_alloc(hba); |
| 7207 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7208 | dev_err(hba->dev, "Memory allocation failed\n"); |
| 7209 | goto out_disable; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7210 | } |
| 7211 | |
| 7212 | /* Configure LRB */ |
| 7213 | ufshcd_host_memory_configure(hba); |
| 7214 | |
| 7215 | host->can_queue = hba->nutrs; |
| 7216 | host->cmd_per_lun = hba->nutrs; |
| 7217 | host->max_id = UFSHCD_MAX_ID; |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 7218 | host->max_lun = UFS_MAX_LUNS; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7219 | host->max_channel = UFSHCD_MAX_CHANNEL; |
| 7220 | host->unique_id = host->host_no; |
| 7221 | host->max_cmd_len = MAX_CDB_SIZE; |
| 7222 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 7223 | hba->max_pwr_info.is_valid = false; |
| 7224 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7225 | /* Initailize wait queue for task management */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 7226 | init_waitqueue_head(&hba->tm_wq); |
| 7227 | init_waitqueue_head(&hba->tm_tag_wq); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7228 | |
| 7229 | /* Initialize work queues */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 7230 | INIT_WORK(&hba->eh_work, ufshcd_err_handler); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 7231 | INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7232 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7233 | /* Initialize UIC command mutex */ |
| 7234 | mutex_init(&hba->uic_cmd_mutex); |
| 7235 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 7236 | /* Initialize mutex for device management commands */ |
| 7237 | mutex_init(&hba->dev_cmd.lock); |
| 7238 | |
| 7239 | /* Initialize device management tag acquire wait queue */ |
| 7240 | init_waitqueue_head(&hba->dev_cmd.tag_wq); |
| 7241 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7242 | ufshcd_init_clk_gating(hba); |
Yaniv Gardi | 199ef13 | 2016-03-10 17:37:06 +0200 | [diff] [blame] | 7243 | |
| 7244 | /* |
| 7245 | * In order to avoid any spurious interrupt immediately after |
| 7246 | * registering UFS controller interrupt handler, clear any pending UFS |
| 7247 | * interrupt status and disable all the UFS interrupts. |
| 7248 | */ |
| 7249 | ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS), |
| 7250 | REG_INTERRUPT_STATUS); |
| 7251 | ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE); |
| 7252 | /* |
| 7253 | * Make sure that UFS interrupts are disabled and any pending interrupt |
| 7254 | * status is cleared before registering UFS interrupt handler. |
| 7255 | */ |
| 7256 | mb(); |
| 7257 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7258 | /* IRQ registration */ |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 7259 | err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7260 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7261 | dev_err(hba->dev, "request irq failed\n"); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7262 | goto exit_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7263 | } else { |
| 7264 | hba->is_irq_enabled = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7265 | } |
| 7266 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7267 | err = scsi_add_host(host, hba->dev); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7268 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7269 | dev_err(hba->dev, "scsi_add_host failed\n"); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7270 | goto exit_gating; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7271 | } |
| 7272 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7273 | /* Host controller enable */ |
| 7274 | err = ufshcd_hba_enable(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7275 | if (err) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7276 | dev_err(hba->dev, "Host controller enable failed\n"); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 7277 | ufshcd_print_host_regs(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7278 | goto out_remove_scsi_host; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7279 | } |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7280 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 7281 | if (ufshcd_is_clkscaling_supported(hba)) { |
Chanwoo Choi | 4861ee1 | 2016-11-08 18:13:28 +0900 | [diff] [blame] | 7282 | hba->devfreq = devm_devfreq_add_device(dev, &ufs_devfreq_profile, |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 7283 | "simple_ondemand", NULL); |
| 7284 | if (IS_ERR(hba->devfreq)) { |
| 7285 | dev_err(hba->dev, "Unable to register with devfreq %ld\n", |
| 7286 | PTR_ERR(hba->devfreq)); |
Wei Yongjun | 73811c9 | 2016-09-28 14:49:42 +0000 | [diff] [blame] | 7287 | err = PTR_ERR(hba->devfreq); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 7288 | goto out_remove_scsi_host; |
| 7289 | } |
| 7290 | /* Suspend devfreq until the UFS device is detected */ |
Gilad Broner | a508253 | 2016-10-17 17:10:00 -0700 | [diff] [blame] | 7291 | ufshcd_suspend_clkscaling(hba); |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 7292 | ufshcd_clkscaling_init_sysfs(hba); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 7293 | } |
| 7294 | |
subhashj@codeaurora.org | 0c8f758 | 2016-12-22 18:41:11 -0800 | [diff] [blame] | 7295 | /* |
| 7296 | * Set the default power management level for runtime and system PM. |
| 7297 | * Default power saving mode is to keep UFS link in Hibern8 state |
| 7298 | * and UFS device in sleep state. |
| 7299 | */ |
| 7300 | hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state( |
| 7301 | UFS_SLEEP_PWR_MODE, |
| 7302 | UIC_LINK_HIBERN8_STATE); |
| 7303 | hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state( |
| 7304 | UFS_SLEEP_PWR_MODE, |
| 7305 | UIC_LINK_HIBERN8_STATE); |
| 7306 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 7307 | /* Hold auto suspend until async scan completes */ |
| 7308 | pm_runtime_get_sync(dev); |
| 7309 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7310 | /* |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 7311 | * We are assuming that device wasn't put in sleep/power-down |
| 7312 | * state exclusively during the boot stage before kernel. |
| 7313 | * This assumption helps avoid doing link startup twice during |
| 7314 | * ufshcd_probe_hba(). |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7315 | */ |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 7316 | ufshcd_set_ufs_dev_active(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7317 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7318 | async_schedule(ufshcd_async_scan, hba); |
subhashj@codeaurora.org | 09690d5 | 2016-12-22 18:41:00 -0800 | [diff] [blame] | 7319 | ufshcd_add_sysfs_nodes(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7320 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7321 | return 0; |
| 7322 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7323 | out_remove_scsi_host: |
| 7324 | scsi_remove_host(hba->host); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7325 | exit_gating: |
| 7326 | ufshcd_exit_clk_gating(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7327 | out_disable: |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7328 | hba->is_irq_enabled = false; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7329 | ufshcd_hba_exit(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7330 | out_error: |
| 7331 | return err; |
| 7332 | } |
| 7333 | EXPORT_SYMBOL_GPL(ufshcd_init); |
| 7334 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7335 | MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>"); |
| 7336 | MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>"); |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 7337 | MODULE_DESCRIPTION("Generic UFS host controller driver Core"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7338 | MODULE_LICENSE("GPL"); |
| 7339 | MODULE_VERSION(UFSHCD_DRIVER_VERSION); |