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" |
Stanislav Nijnikov | cbb6813 | 2018-02-15 14:14:01 +0200 | [diff] [blame] | 47 | #include "ufs-sysfs.h" |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 48 | |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 49 | #define CREATE_TRACE_POINTS |
| 50 | #include <trace/events/ufs.h> |
| 51 | |
Gilad Broner | dcea0bf | 2016-10-17 17:09:48 -0700 | [diff] [blame] | 52 | #define UFSHCD_REQ_SENSE_SIZE 18 |
| 53 | |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 54 | #define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\ |
| 55 | UTP_TASK_REQ_COMPL |\ |
| 56 | UFSHCD_ERROR_MASK) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 57 | /* UIC command timeout, unit: ms */ |
| 58 | #define UIC_CMD_TIMEOUT 500 |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 59 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 60 | /* NOP OUT retries waiting for NOP IN response */ |
| 61 | #define NOP_OUT_RETRIES 10 |
| 62 | /* Timeout after 30 msecs if NOP OUT hangs without response */ |
| 63 | #define NOP_OUT_TIMEOUT 30 /* msecs */ |
| 64 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 65 | /* Query request retries */ |
subhashj@codeaurora.org | 10fe588 | 2016-11-23 16:31:52 -0800 | [diff] [blame] | 66 | #define QUERY_REQ_RETRIES 3 |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 67 | /* Query request timeout */ |
subhashj@codeaurora.org | 10fe588 | 2016-11-23 16:31:52 -0800 | [diff] [blame] | 68 | #define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */ |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 69 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 70 | /* Task management command timeout */ |
| 71 | #define TM_CMD_TIMEOUT 100 /* msecs */ |
| 72 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 73 | /* maximum number of retries for a general UIC command */ |
| 74 | #define UFS_UIC_COMMAND_RETRIES 3 |
| 75 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 76 | /* maximum number of link-startup retries */ |
| 77 | #define DME_LINKSTARTUP_RETRIES 3 |
| 78 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 79 | /* Maximum retries for Hibern8 enter */ |
| 80 | #define UIC_HIBERN8_ENTER_RETRIES 3 |
| 81 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 82 | /* maximum number of reset retries before giving up */ |
| 83 | #define MAX_HOST_RESET_RETRIES 5 |
| 84 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 85 | /* Expose the flag value from utp_upiu_query.value */ |
| 86 | #define MASK_QUERY_UPIU_FLAG_LOC 0xFF |
| 87 | |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 88 | /* Interrupt aggregation default timeout, unit: 40us */ |
| 89 | #define INT_AGGR_DEF_TO 0x02 |
| 90 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 91 | #define ufshcd_toggle_vreg(_dev, _vreg, _on) \ |
| 92 | ({ \ |
| 93 | int _ret; \ |
| 94 | if (_on) \ |
| 95 | _ret = ufshcd_enable_vreg(_dev, _vreg); \ |
| 96 | else \ |
| 97 | _ret = ufshcd_disable_vreg(_dev, _vreg); \ |
| 98 | _ret; \ |
| 99 | }) |
| 100 | |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 101 | #define ufshcd_hex_dump(prefix_str, buf, len) \ |
| 102 | print_hex_dump(KERN_ERR, prefix_str, DUMP_PREFIX_OFFSET, 16, 4, buf, len, false) |
| 103 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 104 | enum { |
| 105 | UFSHCD_MAX_CHANNEL = 0, |
| 106 | UFSHCD_MAX_ID = 1, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 107 | UFSHCD_CMD_PER_LUN = 32, |
| 108 | UFSHCD_CAN_QUEUE = 32, |
| 109 | }; |
| 110 | |
| 111 | /* UFSHCD states */ |
| 112 | enum { |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 113 | UFSHCD_STATE_RESET, |
| 114 | UFSHCD_STATE_ERROR, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 115 | UFSHCD_STATE_OPERATIONAL, |
Zang Leigang | 141f816 | 2016-11-16 11:29:37 +0800 | [diff] [blame] | 116 | UFSHCD_STATE_EH_SCHEDULED, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | /* UFSHCD error handling flags */ |
| 120 | enum { |
| 121 | UFSHCD_EH_IN_PROGRESS = (1 << 0), |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 122 | }; |
| 123 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 124 | /* UFSHCD UIC layer error flags */ |
| 125 | enum { |
| 126 | UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 127 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */ |
| 128 | UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */ |
| 129 | UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */ |
| 130 | UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */ |
| 131 | UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 132 | }; |
| 133 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 134 | #define ufshcd_set_eh_in_progress(h) \ |
Tomohiro Kusumi | 9c490d2 | 2017-03-28 16:49:26 +0300 | [diff] [blame] | 135 | ((h)->eh_flags |= UFSHCD_EH_IN_PROGRESS) |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 136 | #define ufshcd_eh_in_progress(h) \ |
Tomohiro Kusumi | 9c490d2 | 2017-03-28 16:49:26 +0300 | [diff] [blame] | 137 | ((h)->eh_flags & UFSHCD_EH_IN_PROGRESS) |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 138 | #define ufshcd_clear_eh_in_progress(h) \ |
Tomohiro Kusumi | 9c490d2 | 2017-03-28 16:49:26 +0300 | [diff] [blame] | 139 | ((h)->eh_flags &= ~UFSHCD_EH_IN_PROGRESS) |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 140 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 141 | #define ufshcd_set_ufs_dev_active(h) \ |
| 142 | ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE) |
| 143 | #define ufshcd_set_ufs_dev_sleep(h) \ |
| 144 | ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE) |
| 145 | #define ufshcd_set_ufs_dev_poweroff(h) \ |
| 146 | ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE) |
| 147 | #define ufshcd_is_ufs_dev_active(h) \ |
| 148 | ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE) |
| 149 | #define ufshcd_is_ufs_dev_sleep(h) \ |
| 150 | ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE) |
| 151 | #define ufshcd_is_ufs_dev_poweroff(h) \ |
| 152 | ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE) |
| 153 | |
Stanislav Nijnikov | cbb6813 | 2018-02-15 14:14:01 +0200 | [diff] [blame] | 154 | struct ufs_pm_lvl_states ufs_pm_lvl_states[] = { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 155 | {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE}, |
| 156 | {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 157 | {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE}, |
| 158 | {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 159 | {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 160 | {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE}, |
| 161 | }; |
| 162 | |
| 163 | static inline enum ufs_dev_pwr_mode |
| 164 | ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl) |
| 165 | { |
| 166 | return ufs_pm_lvl_states[lvl].dev_state; |
| 167 | } |
| 168 | |
| 169 | static inline enum uic_link_state |
| 170 | ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl) |
| 171 | { |
| 172 | return ufs_pm_lvl_states[lvl].link_state; |
| 173 | } |
| 174 | |
subhashj@codeaurora.org | 0c8f758 | 2016-12-22 18:41:11 -0800 | [diff] [blame] | 175 | static inline enum ufs_pm_level |
| 176 | ufs_get_desired_pm_lvl_for_dev_link_state(enum ufs_dev_pwr_mode dev_state, |
| 177 | enum uic_link_state link_state) |
| 178 | { |
| 179 | enum ufs_pm_level lvl; |
| 180 | |
| 181 | for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) { |
| 182 | if ((ufs_pm_lvl_states[lvl].dev_state == dev_state) && |
| 183 | (ufs_pm_lvl_states[lvl].link_state == link_state)) |
| 184 | return lvl; |
| 185 | } |
| 186 | |
| 187 | /* if no match found, return the level 0 */ |
| 188 | return UFS_PM_LVL_0; |
| 189 | } |
| 190 | |
Subhash Jadavani | 56d4a18 | 2016-12-05 19:25:32 -0800 | [diff] [blame] | 191 | static struct ufs_dev_fix ufs_fixups[] = { |
| 192 | /* UFS cards deviations table */ |
| 193 | UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, |
| 194 | UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM), |
| 195 | UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, UFS_DEVICE_NO_VCCQ), |
| 196 | UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, |
| 197 | UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS), |
| 198 | UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, |
| 199 | UFS_DEVICE_NO_FASTAUTO), |
| 200 | UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, |
| 201 | UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE), |
| 202 | UFS_FIX(UFS_VENDOR_TOSHIBA, UFS_ANY_MODEL, |
| 203 | UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM), |
| 204 | UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9C8KBADG", |
| 205 | UFS_DEVICE_QUIRK_PA_TACTIVATE), |
| 206 | UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9D8KBADG", |
| 207 | UFS_DEVICE_QUIRK_PA_TACTIVATE), |
| 208 | UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL, UFS_DEVICE_NO_VCCQ), |
| 209 | UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL, |
| 210 | UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME), |
| 211 | |
| 212 | END_FIX |
| 213 | }; |
| 214 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 215 | static void ufshcd_tmc_handler(struct ufs_hba *hba); |
| 216 | static void ufshcd_async_scan(void *data, async_cookie_t cookie); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 217 | static int ufshcd_reset_and_restore(struct ufs_hba *hba); |
Dolev Raviv | e7d3825 | 2016-12-22 18:40:07 -0800 | [diff] [blame] | 218 | static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 219 | 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] | 220 | static void ufshcd_hba_exit(struct ufs_hba *hba); |
| 221 | static int ufshcd_probe_hba(struct ufs_hba *hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 222 | static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on, |
| 223 | bool skip_ref_clk); |
| 224 | static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on); |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 225 | 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] | 226 | static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba); |
| 227 | static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 228 | 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] | 229 | static int ufshcd_host_reset_and_restore(struct ufs_hba *hba); |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 230 | static void ufshcd_resume_clkscaling(struct ufs_hba *hba); |
| 231 | static void ufshcd_suspend_clkscaling(struct ufs_hba *hba); |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 232 | static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba); |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 233 | static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 234 | static irqreturn_t ufshcd_intr(int irq, void *__hba); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 235 | static int ufshcd_config_pwr_mode(struct ufs_hba *hba, |
| 236 | struct ufs_pa_layer_attr *desired_pwr_mode); |
Yaniv Gardi | 874237f | 2015-05-17 18:55:03 +0300 | [diff] [blame] | 237 | static int ufshcd_change_power_mode(struct ufs_hba *hba, |
| 238 | struct ufs_pa_layer_attr *pwr_mode); |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 239 | static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag) |
| 240 | { |
| 241 | return tag >= 0 && tag < hba->nutrs; |
| 242 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 243 | |
| 244 | static inline int ufshcd_enable_irq(struct ufs_hba *hba) |
| 245 | { |
| 246 | int ret = 0; |
| 247 | |
| 248 | if (!hba->is_irq_enabled) { |
| 249 | ret = request_irq(hba->irq, ufshcd_intr, IRQF_SHARED, UFSHCD, |
| 250 | hba); |
| 251 | if (ret) |
| 252 | dev_err(hba->dev, "%s: request_irq failed, ret=%d\n", |
| 253 | __func__, ret); |
| 254 | hba->is_irq_enabled = true; |
| 255 | } |
| 256 | |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | static inline void ufshcd_disable_irq(struct ufs_hba *hba) |
| 261 | { |
| 262 | if (hba->is_irq_enabled) { |
| 263 | free_irq(hba->irq, hba); |
| 264 | hba->is_irq_enabled = false; |
| 265 | } |
| 266 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 267 | |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 268 | /* replace non-printable or non-ASCII characters with spaces */ |
| 269 | static inline void ufshcd_remove_non_printable(char *val) |
| 270 | { |
| 271 | if (!val) |
| 272 | return; |
| 273 | |
| 274 | if (*val < 0x20 || *val > 0x7e) |
| 275 | *val = ' '; |
| 276 | } |
| 277 | |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 278 | static void ufshcd_add_command_trace(struct ufs_hba *hba, |
| 279 | unsigned int tag, const char *str) |
| 280 | { |
| 281 | sector_t lba = -1; |
| 282 | u8 opcode = 0; |
| 283 | u32 intr, doorbell; |
| 284 | struct ufshcd_lrb *lrbp; |
| 285 | int transfer_len = -1; |
| 286 | |
| 287 | if (!trace_ufshcd_command_enabled()) |
| 288 | return; |
| 289 | |
| 290 | lrbp = &hba->lrb[tag]; |
| 291 | |
| 292 | if (lrbp->cmd) { /* data phase exists */ |
| 293 | opcode = (u8)(*lrbp->cmd->cmnd); |
| 294 | if ((opcode == READ_10) || (opcode == WRITE_10)) { |
| 295 | /* |
| 296 | * Currently we only fully trace read(10) and write(10) |
| 297 | * commands |
| 298 | */ |
| 299 | if (lrbp->cmd->request && lrbp->cmd->request->bio) |
| 300 | lba = |
| 301 | lrbp->cmd->request->bio->bi_iter.bi_sector; |
| 302 | transfer_len = be32_to_cpu( |
| 303 | lrbp->ucd_req_ptr->sc.exp_data_transfer_len); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | intr = ufshcd_readl(hba, REG_INTERRUPT_STATUS); |
| 308 | doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 309 | trace_ufshcd_command(dev_name(hba->dev), str, tag, |
| 310 | doorbell, transfer_len, intr, lba, opcode); |
| 311 | } |
| 312 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 313 | static void ufshcd_print_clk_freqs(struct ufs_hba *hba) |
| 314 | { |
| 315 | struct ufs_clk_info *clki; |
| 316 | struct list_head *head = &hba->clk_list_head; |
| 317 | |
Szymon Mielczarek | 566ec9a | 2017-06-05 11:36:54 +0300 | [diff] [blame] | 318 | if (list_empty(head)) |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 319 | return; |
| 320 | |
| 321 | list_for_each_entry(clki, head, list) { |
| 322 | if (!IS_ERR_OR_NULL(clki->clk) && clki->min_freq && |
| 323 | clki->max_freq) |
| 324 | dev_err(hba->dev, "clk: %s, rate: %u\n", |
| 325 | clki->name, clki->curr_freq); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | static void ufshcd_print_uic_err_hist(struct ufs_hba *hba, |
| 330 | struct ufs_uic_err_reg_hist *err_hist, char *err_name) |
| 331 | { |
| 332 | int i; |
| 333 | |
| 334 | for (i = 0; i < UIC_ERR_REG_HIST_LENGTH; i++) { |
| 335 | int p = (i + err_hist->pos - 1) % UIC_ERR_REG_HIST_LENGTH; |
| 336 | |
| 337 | if (err_hist->reg[p] == 0) |
| 338 | continue; |
| 339 | dev_err(hba->dev, "%s[%d] = 0x%x at %lld us\n", err_name, i, |
| 340 | err_hist->reg[p], ktime_to_us(err_hist->tstamp[p])); |
| 341 | } |
| 342 | } |
| 343 | |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 344 | static void ufshcd_print_host_regs(struct ufs_hba *hba) |
| 345 | { |
| 346 | /* |
| 347 | * hex_dump reads its data without the readl macro. This might |
| 348 | * cause inconsistency issues on some platform, as the printed |
| 349 | * values may be from cache and not the most recent value. |
| 350 | * To know whether you are looking at an un-cached version verify |
| 351 | * that IORESOURCE_MEM flag is on when xxx_get_resource() is invoked |
| 352 | * during platform/pci probe function. |
| 353 | */ |
| 354 | ufshcd_hex_dump("host regs: ", hba->mmio_base, UFSHCI_REG_SPACE_SIZE); |
| 355 | dev_err(hba->dev, "hba->ufs_version = 0x%x, hba->capabilities = 0x%x\n", |
| 356 | hba->ufs_version, hba->capabilities); |
| 357 | dev_err(hba->dev, |
| 358 | "hba->outstanding_reqs = 0x%x, hba->outstanding_tasks = 0x%x\n", |
| 359 | (u32)hba->outstanding_reqs, (u32)hba->outstanding_tasks); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 360 | dev_err(hba->dev, |
| 361 | "last_hibern8_exit_tstamp at %lld us, hibern8_exit_cnt = %d\n", |
| 362 | ktime_to_us(hba->ufs_stats.last_hibern8_exit_tstamp), |
| 363 | hba->ufs_stats.hibern8_exit_cnt); |
| 364 | |
| 365 | ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.pa_err, "pa_err"); |
| 366 | ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.dl_err, "dl_err"); |
| 367 | ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.nl_err, "nl_err"); |
| 368 | ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.tl_err, "tl_err"); |
| 369 | ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.dme_err, "dme_err"); |
| 370 | |
| 371 | ufshcd_print_clk_freqs(hba); |
| 372 | |
| 373 | if (hba->vops && hba->vops->dbg_register_dump) |
| 374 | hba->vops->dbg_register_dump(hba); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | static |
| 378 | void ufshcd_print_trs(struct ufs_hba *hba, unsigned long bitmap, bool pr_prdt) |
| 379 | { |
| 380 | struct ufshcd_lrb *lrbp; |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 381 | int prdt_length; |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 382 | int tag; |
| 383 | |
| 384 | for_each_set_bit(tag, &bitmap, hba->nutrs) { |
| 385 | lrbp = &hba->lrb[tag]; |
| 386 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 387 | dev_err(hba->dev, "UPIU[%d] - issue time %lld us\n", |
| 388 | tag, ktime_to_us(lrbp->issue_time_stamp)); |
Zang Leigang | 0901718 | 2017-09-27 10:06:06 +0800 | [diff] [blame] | 389 | dev_err(hba->dev, "UPIU[%d] - complete time %lld us\n", |
| 390 | tag, ktime_to_us(lrbp->compl_time_stamp)); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 391 | dev_err(hba->dev, |
| 392 | "UPIU[%d] - Transfer Request Descriptor phys@0x%llx\n", |
| 393 | tag, (u64)lrbp->utrd_dma_addr); |
| 394 | |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 395 | ufshcd_hex_dump("UPIU TRD: ", lrbp->utr_descriptor_ptr, |
| 396 | sizeof(struct utp_transfer_req_desc)); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 397 | dev_err(hba->dev, "UPIU[%d] - Request UPIU phys@0x%llx\n", tag, |
| 398 | (u64)lrbp->ucd_req_dma_addr); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 399 | ufshcd_hex_dump("UPIU REQ: ", lrbp->ucd_req_ptr, |
| 400 | sizeof(struct utp_upiu_req)); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 401 | dev_err(hba->dev, "UPIU[%d] - Response UPIU phys@0x%llx\n", tag, |
| 402 | (u64)lrbp->ucd_rsp_dma_addr); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 403 | ufshcd_hex_dump("UPIU RSP: ", lrbp->ucd_rsp_ptr, |
| 404 | sizeof(struct utp_upiu_rsp)); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 405 | |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 406 | prdt_length = le16_to_cpu( |
| 407 | lrbp->utr_descriptor_ptr->prd_table_length); |
| 408 | dev_err(hba->dev, |
| 409 | "UPIU[%d] - PRDT - %d entries phys@0x%llx\n", |
| 410 | tag, prdt_length, |
| 411 | (u64)lrbp->ucd_prdt_dma_addr); |
| 412 | |
| 413 | if (pr_prdt) |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 414 | ufshcd_hex_dump("UPIU PRDT: ", lrbp->ucd_prdt_ptr, |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 415 | sizeof(struct ufshcd_sg_entry) * prdt_length); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
| 419 | static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap) |
| 420 | { |
| 421 | struct utp_task_req_desc *tmrdp; |
| 422 | int tag; |
| 423 | |
| 424 | for_each_set_bit(tag, &bitmap, hba->nutmrs) { |
| 425 | tmrdp = &hba->utmrdl_base_addr[tag]; |
| 426 | dev_err(hba->dev, "TM[%d] - Task Management Header\n", tag); |
| 427 | ufshcd_hex_dump("TM TRD: ", &tmrdp->header, |
| 428 | sizeof(struct request_desc_header)); |
| 429 | dev_err(hba->dev, "TM[%d] - Task Management Request UPIU\n", |
| 430 | tag); |
| 431 | ufshcd_hex_dump("TM REQ: ", tmrdp->task_req_upiu, |
| 432 | sizeof(struct utp_upiu_req)); |
| 433 | dev_err(hba->dev, "TM[%d] - Task Management Response UPIU\n", |
| 434 | tag); |
| 435 | ufshcd_hex_dump("TM RSP: ", tmrdp->task_rsp_upiu, |
| 436 | sizeof(struct utp_task_req_desc)); |
| 437 | } |
| 438 | } |
| 439 | |
Gilad Broner | 6ba6558 | 2017-02-03 16:57:28 -0800 | [diff] [blame] | 440 | static void ufshcd_print_host_state(struct ufs_hba *hba) |
| 441 | { |
| 442 | dev_err(hba->dev, "UFS Host state=%d\n", hba->ufshcd_state); |
| 443 | dev_err(hba->dev, "lrb in use=0x%lx, outstanding reqs=0x%lx tasks=0x%lx\n", |
Zang Leigang | e002e65 | 2017-08-24 10:57:15 +0800 | [diff] [blame] | 444 | hba->lrb_in_use, hba->outstanding_reqs, hba->outstanding_tasks); |
Gilad Broner | 6ba6558 | 2017-02-03 16:57:28 -0800 | [diff] [blame] | 445 | dev_err(hba->dev, "saved_err=0x%x, saved_uic_err=0x%x\n", |
| 446 | hba->saved_err, hba->saved_uic_err); |
| 447 | dev_err(hba->dev, "Device power mode=%d, UIC link state=%d\n", |
| 448 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
| 449 | dev_err(hba->dev, "PM in progress=%d, sys. suspended=%d\n", |
| 450 | hba->pm_op_in_progress, hba->is_sys_suspended); |
| 451 | dev_err(hba->dev, "Auto BKOPS=%d, Host self-block=%d\n", |
| 452 | hba->auto_bkops_enabled, hba->host->host_self_blocked); |
| 453 | dev_err(hba->dev, "Clk gate=%d\n", hba->clk_gating.state); |
| 454 | dev_err(hba->dev, "error handling flags=0x%x, req. abort count=%d\n", |
| 455 | hba->eh_flags, hba->req_abort_count); |
| 456 | dev_err(hba->dev, "Host capabilities=0x%x, caps=0x%x\n", |
| 457 | hba->capabilities, hba->caps); |
| 458 | dev_err(hba->dev, "quirks=0x%x, dev. quirks=0x%x\n", hba->quirks, |
| 459 | hba->dev_quirks); |
| 460 | } |
| 461 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 462 | /** |
| 463 | * ufshcd_print_pwr_info - print power params as saved in hba |
| 464 | * power info |
| 465 | * @hba: per-adapter instance |
| 466 | */ |
| 467 | static void ufshcd_print_pwr_info(struct ufs_hba *hba) |
| 468 | { |
| 469 | static const char * const names[] = { |
| 470 | "INVALID MODE", |
| 471 | "FAST MODE", |
| 472 | "SLOW_MODE", |
| 473 | "INVALID MODE", |
| 474 | "FASTAUTO_MODE", |
| 475 | "SLOWAUTO_MODE", |
| 476 | "INVALID MODE", |
| 477 | }; |
| 478 | |
| 479 | dev_err(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n", |
| 480 | __func__, |
| 481 | hba->pwr_info.gear_rx, hba->pwr_info.gear_tx, |
| 482 | hba->pwr_info.lane_rx, hba->pwr_info.lane_tx, |
| 483 | names[hba->pwr_info.pwr_rx], |
| 484 | names[hba->pwr_info.pwr_tx], |
| 485 | hba->pwr_info.hs_rate); |
| 486 | } |
| 487 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 488 | /* |
| 489 | * ufshcd_wait_for_register - wait for register value to change |
| 490 | * @hba - per-adapter interface |
| 491 | * @reg - mmio register offset |
| 492 | * @mask - mask to apply to read register value |
| 493 | * @val - wait condition |
| 494 | * @interval_us - polling interval in microsecs |
| 495 | * @timeout_ms - timeout in millisecs |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 496 | * @can_sleep - perform sleep or just spin |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 497 | * |
| 498 | * Returns -ETIMEDOUT on error, zero on success |
| 499 | */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 500 | int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask, |
| 501 | u32 val, unsigned long interval_us, |
| 502 | unsigned long timeout_ms, bool can_sleep) |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 503 | { |
| 504 | int err = 0; |
| 505 | unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms); |
| 506 | |
| 507 | /* ignore bits that we don't intend to wait on */ |
| 508 | val = val & mask; |
| 509 | |
| 510 | while ((ufshcd_readl(hba, reg) & mask) != val) { |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 511 | if (can_sleep) |
| 512 | usleep_range(interval_us, interval_us + 50); |
| 513 | else |
| 514 | udelay(interval_us); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 515 | if (time_after(jiffies, timeout)) { |
| 516 | if ((ufshcd_readl(hba, reg) & mask) != val) |
| 517 | err = -ETIMEDOUT; |
| 518 | break; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | return err; |
| 523 | } |
| 524 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 525 | /** |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 526 | * ufshcd_get_intr_mask - Get the interrupt bit mask |
| 527 | * @hba - Pointer to adapter instance |
| 528 | * |
| 529 | * Returns interrupt bit mask per version |
| 530 | */ |
| 531 | static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba) |
| 532 | { |
Yaniv Gardi | c01848c | 2016-12-05 19:25:02 -0800 | [diff] [blame] | 533 | u32 intr_mask = 0; |
| 534 | |
| 535 | switch (hba->ufs_version) { |
| 536 | case UFSHCI_VERSION_10: |
| 537 | intr_mask = INTERRUPT_MASK_ALL_VER_10; |
| 538 | break; |
Yaniv Gardi | c01848c | 2016-12-05 19:25:02 -0800 | [diff] [blame] | 539 | case UFSHCI_VERSION_11: |
| 540 | case UFSHCI_VERSION_20: |
| 541 | intr_mask = INTERRUPT_MASK_ALL_VER_11; |
| 542 | break; |
Yaniv Gardi | c01848c | 2016-12-05 19:25:02 -0800 | [diff] [blame] | 543 | case UFSHCI_VERSION_21: |
| 544 | default: |
| 545 | intr_mask = INTERRUPT_MASK_ALL_VER_21; |
Tomohiro Kusumi | 031d1e0 | 2017-03-23 12:49:04 +0200 | [diff] [blame] | 546 | break; |
Yaniv Gardi | c01848c | 2016-12-05 19:25:02 -0800 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | return intr_mask; |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 553 | * ufshcd_get_ufs_version - Get the UFS version supported by the HBA |
| 554 | * @hba - Pointer to adapter instance |
| 555 | * |
| 556 | * Returns UFSHCI version supported by the controller |
| 557 | */ |
| 558 | static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba) |
| 559 | { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 560 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION) |
| 561 | return ufshcd_vops_get_ufs_hci_version(hba); |
Yaniv Gardi | 9949e70 | 2015-05-17 18:55:05 +0300 | [diff] [blame] | 562 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 563 | return ufshcd_readl(hba, REG_UFS_VERSION); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | /** |
| 567 | * ufshcd_is_device_present - Check if any device connected to |
| 568 | * the host controller |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 569 | * @hba: pointer to adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 570 | * |
Tomohiro Kusumi | c9e6010 | 2017-03-28 16:49:24 +0300 | [diff] [blame] | 571 | * Returns true if device present, false if no device detected |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 572 | */ |
Tomohiro Kusumi | c9e6010 | 2017-03-28 16:49:24 +0300 | [diff] [blame] | 573 | static inline bool ufshcd_is_device_present(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 574 | { |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 575 | return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & |
Tomohiro Kusumi | c9e6010 | 2017-03-28 16:49:24 +0300 | [diff] [blame] | 576 | DEVICE_PRESENT) ? true : false; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | /** |
| 580 | * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status |
| 581 | * @lrb: pointer to local command reference block |
| 582 | * |
| 583 | * This function is used to get the OCS field from UTRD |
| 584 | * Returns the OCS field in the UTRD |
| 585 | */ |
| 586 | static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp) |
| 587 | { |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 588 | 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] | 589 | } |
| 590 | |
| 591 | /** |
| 592 | * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status |
| 593 | * @task_req_descp: pointer to utp_task_req_desc structure |
| 594 | * |
| 595 | * This function is used to get the OCS field from UTMRD |
| 596 | * Returns the OCS field in the UTMRD |
| 597 | */ |
| 598 | static inline int |
| 599 | ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp) |
| 600 | { |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 601 | return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | /** |
| 605 | * ufshcd_get_tm_free_slot - get a free slot for task management request |
| 606 | * @hba: per adapter instance |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 607 | * @free_slot: pointer to variable with available slot value |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 608 | * |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 609 | * Get a free tag and lock it until ufshcd_put_tm_slot() is called. |
| 610 | * Returns 0 if free slot is not available, else return 1 with tag value |
| 611 | * in @free_slot. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 612 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 613 | 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] | 614 | { |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 615 | int tag; |
| 616 | bool ret = false; |
| 617 | |
| 618 | if (!free_slot) |
| 619 | goto out; |
| 620 | |
| 621 | do { |
| 622 | tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs); |
| 623 | if (tag >= hba->nutmrs) |
| 624 | goto out; |
| 625 | } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use)); |
| 626 | |
| 627 | *free_slot = tag; |
| 628 | ret = true; |
| 629 | out: |
| 630 | return ret; |
| 631 | } |
| 632 | |
| 633 | static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot) |
| 634 | { |
| 635 | clear_bit_unlock(slot, &hba->tm_slots_in_use); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | /** |
| 639 | * ufshcd_utrl_clear - Clear a bit in UTRLCLR register |
| 640 | * @hba: per adapter instance |
| 641 | * @pos: position of the bit to be cleared |
| 642 | */ |
| 643 | static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos) |
| 644 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 645 | ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | /** |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 649 | * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field |
| 650 | * @hba: per adapter instance |
| 651 | * @tag: position of the bit to be cleared |
| 652 | */ |
| 653 | static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag) |
| 654 | { |
| 655 | __clear_bit(tag, &hba->outstanding_reqs); |
| 656 | } |
| 657 | |
| 658 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 659 | * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY |
| 660 | * @reg: Register value of host controller status |
| 661 | * |
| 662 | * Returns integer, 0 on Success and positive value if failed |
| 663 | */ |
| 664 | static inline int ufshcd_get_lists_status(u32 reg) |
| 665 | { |
Tomohiro Kusumi | 6cf1611 | 2017-04-26 20:28:58 +0300 | [diff] [blame] | 666 | return !((reg & UFSHCD_STATUS_READY) == UFSHCD_STATUS_READY); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /** |
| 670 | * ufshcd_get_uic_cmd_result - Get the UIC command result |
| 671 | * @hba: Pointer to adapter instance |
| 672 | * |
| 673 | * This function gets the result of UIC command completion |
| 674 | * Returns 0 on success, non zero value on error |
| 675 | */ |
| 676 | static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba) |
| 677 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 678 | return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) & |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 679 | MASK_UIC_COMMAND_RESULT; |
| 680 | } |
| 681 | |
| 682 | /** |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 683 | * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command |
| 684 | * @hba: Pointer to adapter instance |
| 685 | * |
| 686 | * This function gets UIC command argument3 |
| 687 | * Returns 0 on success, non zero value on error |
| 688 | */ |
| 689 | static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba) |
| 690 | { |
| 691 | return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3); |
| 692 | } |
| 693 | |
| 694 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 695 | * ufshcd_get_req_rsp - returns the TR response transaction type |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 696 | * @ucd_rsp_ptr: pointer to response UPIU |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 697 | */ |
| 698 | static inline int |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 699 | ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 700 | { |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 701 | return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | /** |
| 705 | * ufshcd_get_rsp_upiu_result - Get the result from response UPIU |
| 706 | * @ucd_rsp_ptr: pointer to response UPIU |
| 707 | * |
| 708 | * This function gets the response status and scsi_status from response UPIU |
| 709 | * Returns the response result code. |
| 710 | */ |
| 711 | static inline int |
| 712 | ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 713 | { |
| 714 | return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT; |
| 715 | } |
| 716 | |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 717 | /* |
| 718 | * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length |
| 719 | * from response UPIU |
| 720 | * @ucd_rsp_ptr: pointer to response UPIU |
| 721 | * |
| 722 | * Return the data segment length. |
| 723 | */ |
| 724 | static inline unsigned int |
| 725 | ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 726 | { |
| 727 | return be32_to_cpu(ucd_rsp_ptr->header.dword_2) & |
| 728 | MASK_RSP_UPIU_DATA_SEG_LEN; |
| 729 | } |
| 730 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 731 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 732 | * ufshcd_is_exception_event - Check if the device raised an exception event |
| 733 | * @ucd_rsp_ptr: pointer to response UPIU |
| 734 | * |
| 735 | * The function checks if the device raised an exception event indicated in |
| 736 | * the Device Information field of response UPIU. |
| 737 | * |
| 738 | * Returns true if exception is raised, false otherwise. |
| 739 | */ |
| 740 | static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 741 | { |
| 742 | return be32_to_cpu(ucd_rsp_ptr->header.dword_2) & |
| 743 | MASK_RSP_EXCEPTION_EVENT ? true : false; |
| 744 | } |
| 745 | |
| 746 | /** |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 747 | * ufshcd_reset_intr_aggr - Reset interrupt aggregation values. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 748 | * @hba: per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 749 | */ |
| 750 | static inline void |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 751 | ufshcd_reset_intr_aggr(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 752 | { |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 753 | ufshcd_writel(hba, INT_AGGR_ENABLE | |
| 754 | INT_AGGR_COUNTER_AND_TIMER_RESET, |
| 755 | REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
| 756 | } |
| 757 | |
| 758 | /** |
| 759 | * ufshcd_config_intr_aggr - Configure interrupt aggregation values. |
| 760 | * @hba: per adapter instance |
| 761 | * @cnt: Interrupt aggregation counter threshold |
| 762 | * @tmout: Interrupt aggregation timeout value |
| 763 | */ |
| 764 | static inline void |
| 765 | ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout) |
| 766 | { |
| 767 | ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE | |
| 768 | INT_AGGR_COUNTER_THLD_VAL(cnt) | |
| 769 | INT_AGGR_TIMEOUT_VAL(tmout), |
| 770 | REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | /** |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 774 | * ufshcd_disable_intr_aggr - Disables interrupt aggregation. |
| 775 | * @hba: per adapter instance |
| 776 | */ |
| 777 | static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba) |
| 778 | { |
| 779 | ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
| 780 | } |
| 781 | |
| 782 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 783 | * ufshcd_enable_run_stop_reg - Enable run-stop registers, |
| 784 | * When run-stop registers are set to 1, it indicates the |
| 785 | * host controller that it can process the requests |
| 786 | * @hba: per adapter instance |
| 787 | */ |
| 788 | static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba) |
| 789 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 790 | ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT, |
| 791 | REG_UTP_TASK_REQ_LIST_RUN_STOP); |
| 792 | ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT, |
| 793 | REG_UTP_TRANSFER_REQ_LIST_RUN_STOP); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 797 | * ufshcd_hba_start - Start controller initialization sequence |
| 798 | * @hba: per adapter instance |
| 799 | */ |
| 800 | static inline void ufshcd_hba_start(struct ufs_hba *hba) |
| 801 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 802 | ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | /** |
| 806 | * ufshcd_is_hba_active - Get controller state |
| 807 | * @hba: per adapter instance |
| 808 | * |
Tomohiro Kusumi | c9e6010 | 2017-03-28 16:49:24 +0300 | [diff] [blame] | 809 | * Returns false if controller is active, true otherwise |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 810 | */ |
Tomohiro Kusumi | c9e6010 | 2017-03-28 16:49:24 +0300 | [diff] [blame] | 811 | static inline bool ufshcd_is_hba_active(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 812 | { |
Tomohiro Kusumi | 4a8eec2 | 2017-03-28 16:49:25 +0300 | [diff] [blame] | 813 | return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & CONTROLLER_ENABLE) |
| 814 | ? false : true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 815 | } |
| 816 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 817 | u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba) |
| 818 | { |
| 819 | /* HCI version 1.0 and 1.1 supports UniPro 1.41 */ |
| 820 | if ((hba->ufs_version == UFSHCI_VERSION_10) || |
| 821 | (hba->ufs_version == UFSHCI_VERSION_11)) |
| 822 | return UFS_UNIPRO_VER_1_41; |
| 823 | else |
| 824 | return UFS_UNIPRO_VER_1_6; |
| 825 | } |
| 826 | EXPORT_SYMBOL(ufshcd_get_local_unipro_ver); |
| 827 | |
| 828 | static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba) |
| 829 | { |
| 830 | /* |
| 831 | * If both host and device support UniPro ver1.6 or later, PA layer |
| 832 | * parameters tuning happens during link startup itself. |
| 833 | * |
| 834 | * We can manually tune PA layer parameters if either host or device |
| 835 | * doesn't support UniPro ver 1.6 or later. But to keep manual tuning |
| 836 | * logic simple, we will only do manual tuning if local unipro version |
| 837 | * doesn't support ver1.6 or later. |
| 838 | */ |
| 839 | if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6) |
| 840 | return true; |
| 841 | else |
| 842 | return false; |
| 843 | } |
| 844 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 845 | static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up) |
| 846 | { |
| 847 | int ret = 0; |
| 848 | struct ufs_clk_info *clki; |
| 849 | struct list_head *head = &hba->clk_list_head; |
| 850 | ktime_t start = ktime_get(); |
| 851 | bool clk_state_changed = false; |
| 852 | |
Szymon Mielczarek | 566ec9a | 2017-06-05 11:36:54 +0300 | [diff] [blame] | 853 | if (list_empty(head)) |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 854 | goto out; |
| 855 | |
| 856 | ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE); |
| 857 | if (ret) |
| 858 | return ret; |
| 859 | |
| 860 | list_for_each_entry(clki, head, list) { |
| 861 | if (!IS_ERR_OR_NULL(clki->clk)) { |
| 862 | if (scale_up && clki->max_freq) { |
| 863 | if (clki->curr_freq == clki->max_freq) |
| 864 | continue; |
| 865 | |
| 866 | clk_state_changed = true; |
| 867 | ret = clk_set_rate(clki->clk, clki->max_freq); |
| 868 | if (ret) { |
| 869 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 870 | __func__, clki->name, |
| 871 | clki->max_freq, ret); |
| 872 | break; |
| 873 | } |
| 874 | trace_ufshcd_clk_scaling(dev_name(hba->dev), |
| 875 | "scaled up", clki->name, |
| 876 | clki->curr_freq, |
| 877 | clki->max_freq); |
| 878 | |
| 879 | clki->curr_freq = clki->max_freq; |
| 880 | |
| 881 | } else if (!scale_up && clki->min_freq) { |
| 882 | if (clki->curr_freq == clki->min_freq) |
| 883 | continue; |
| 884 | |
| 885 | clk_state_changed = true; |
| 886 | ret = clk_set_rate(clki->clk, clki->min_freq); |
| 887 | if (ret) { |
| 888 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 889 | __func__, clki->name, |
| 890 | clki->min_freq, ret); |
| 891 | break; |
| 892 | } |
| 893 | trace_ufshcd_clk_scaling(dev_name(hba->dev), |
| 894 | "scaled down", clki->name, |
| 895 | clki->curr_freq, |
| 896 | clki->min_freq); |
| 897 | clki->curr_freq = clki->min_freq; |
| 898 | } |
| 899 | } |
| 900 | dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__, |
| 901 | clki->name, clk_get_rate(clki->clk)); |
| 902 | } |
| 903 | |
| 904 | ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE); |
| 905 | |
| 906 | out: |
| 907 | if (clk_state_changed) |
| 908 | trace_ufshcd_profile_clk_scaling(dev_name(hba->dev), |
| 909 | (scale_up ? "up" : "down"), |
| 910 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
| 911 | return ret; |
| 912 | } |
| 913 | |
| 914 | /** |
| 915 | * ufshcd_is_devfreq_scaling_required - check if scaling is required or not |
| 916 | * @hba: per adapter instance |
| 917 | * @scale_up: True if scaling up and false if scaling down |
| 918 | * |
| 919 | * Returns true if scaling is required, false otherwise. |
| 920 | */ |
| 921 | static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba, |
| 922 | bool scale_up) |
| 923 | { |
| 924 | struct ufs_clk_info *clki; |
| 925 | struct list_head *head = &hba->clk_list_head; |
| 926 | |
Szymon Mielczarek | 566ec9a | 2017-06-05 11:36:54 +0300 | [diff] [blame] | 927 | if (list_empty(head)) |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 928 | return false; |
| 929 | |
| 930 | list_for_each_entry(clki, head, list) { |
| 931 | if (!IS_ERR_OR_NULL(clki->clk)) { |
| 932 | if (scale_up && clki->max_freq) { |
| 933 | if (clki->curr_freq == clki->max_freq) |
| 934 | continue; |
| 935 | return true; |
| 936 | } else if (!scale_up && clki->min_freq) { |
| 937 | if (clki->curr_freq == clki->min_freq) |
| 938 | continue; |
| 939 | return true; |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | return false; |
| 945 | } |
| 946 | |
| 947 | static int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba, |
| 948 | u64 wait_timeout_us) |
| 949 | { |
| 950 | unsigned long flags; |
| 951 | int ret = 0; |
| 952 | u32 tm_doorbell; |
| 953 | u32 tr_doorbell; |
| 954 | bool timeout = false, do_last_check = false; |
| 955 | ktime_t start; |
| 956 | |
| 957 | ufshcd_hold(hba, false); |
| 958 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 959 | /* |
| 960 | * Wait for all the outstanding tasks/transfer requests. |
| 961 | * Verify by checking the doorbell registers are clear. |
| 962 | */ |
| 963 | start = ktime_get(); |
| 964 | do { |
| 965 | if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) { |
| 966 | ret = -EBUSY; |
| 967 | goto out; |
| 968 | } |
| 969 | |
| 970 | tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL); |
| 971 | tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 972 | if (!tm_doorbell && !tr_doorbell) { |
| 973 | timeout = false; |
| 974 | break; |
| 975 | } else if (do_last_check) { |
| 976 | break; |
| 977 | } |
| 978 | |
| 979 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 980 | schedule(); |
| 981 | if (ktime_to_us(ktime_sub(ktime_get(), start)) > |
| 982 | wait_timeout_us) { |
| 983 | timeout = true; |
| 984 | /* |
| 985 | * We might have scheduled out for long time so make |
| 986 | * sure to check if doorbells are cleared by this time |
| 987 | * or not. |
| 988 | */ |
| 989 | do_last_check = true; |
| 990 | } |
| 991 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 992 | } while (tm_doorbell || tr_doorbell); |
| 993 | |
| 994 | if (timeout) { |
| 995 | dev_err(hba->dev, |
| 996 | "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n", |
| 997 | __func__, tm_doorbell, tr_doorbell); |
| 998 | ret = -EBUSY; |
| 999 | } |
| 1000 | out: |
| 1001 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1002 | ufshcd_release(hba); |
| 1003 | return ret; |
| 1004 | } |
| 1005 | |
| 1006 | /** |
| 1007 | * ufshcd_scale_gear - scale up/down UFS gear |
| 1008 | * @hba: per adapter instance |
| 1009 | * @scale_up: True for scaling up gear and false for scaling down |
| 1010 | * |
| 1011 | * Returns 0 for success, |
| 1012 | * Returns -EBUSY if scaling can't happen at this time |
| 1013 | * Returns non-zero for any other errors |
| 1014 | */ |
| 1015 | static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up) |
| 1016 | { |
| 1017 | #define UFS_MIN_GEAR_TO_SCALE_DOWN UFS_HS_G1 |
| 1018 | int ret = 0; |
| 1019 | struct ufs_pa_layer_attr new_pwr_info; |
| 1020 | |
| 1021 | if (scale_up) { |
| 1022 | memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info.info, |
| 1023 | sizeof(struct ufs_pa_layer_attr)); |
| 1024 | } else { |
| 1025 | memcpy(&new_pwr_info, &hba->pwr_info, |
| 1026 | sizeof(struct ufs_pa_layer_attr)); |
| 1027 | |
| 1028 | if (hba->pwr_info.gear_tx > UFS_MIN_GEAR_TO_SCALE_DOWN |
| 1029 | || hba->pwr_info.gear_rx > UFS_MIN_GEAR_TO_SCALE_DOWN) { |
| 1030 | /* save the current power mode */ |
| 1031 | memcpy(&hba->clk_scaling.saved_pwr_info.info, |
| 1032 | &hba->pwr_info, |
| 1033 | sizeof(struct ufs_pa_layer_attr)); |
| 1034 | |
| 1035 | /* scale down gear */ |
| 1036 | new_pwr_info.gear_tx = UFS_MIN_GEAR_TO_SCALE_DOWN; |
| 1037 | new_pwr_info.gear_rx = UFS_MIN_GEAR_TO_SCALE_DOWN; |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | /* check if the power mode needs to be changed or not? */ |
| 1042 | ret = ufshcd_change_power_mode(hba, &new_pwr_info); |
| 1043 | |
| 1044 | if (ret) |
| 1045 | dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d)", |
| 1046 | __func__, ret, |
| 1047 | hba->pwr_info.gear_tx, hba->pwr_info.gear_rx, |
| 1048 | new_pwr_info.gear_tx, new_pwr_info.gear_rx); |
| 1049 | |
| 1050 | return ret; |
| 1051 | } |
| 1052 | |
| 1053 | static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba) |
| 1054 | { |
| 1055 | #define DOORBELL_CLR_TOUT_US (1000 * 1000) /* 1 sec */ |
| 1056 | int ret = 0; |
| 1057 | /* |
| 1058 | * make sure that there are no outstanding requests when |
| 1059 | * clock scaling is in progress |
| 1060 | */ |
| 1061 | scsi_block_requests(hba->host); |
| 1062 | down_write(&hba->clk_scaling_lock); |
| 1063 | if (ufshcd_wait_for_doorbell_clr(hba, DOORBELL_CLR_TOUT_US)) { |
| 1064 | ret = -EBUSY; |
| 1065 | up_write(&hba->clk_scaling_lock); |
| 1066 | scsi_unblock_requests(hba->host); |
| 1067 | } |
| 1068 | |
| 1069 | return ret; |
| 1070 | } |
| 1071 | |
| 1072 | static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba) |
| 1073 | { |
| 1074 | up_write(&hba->clk_scaling_lock); |
| 1075 | scsi_unblock_requests(hba->host); |
| 1076 | } |
| 1077 | |
| 1078 | /** |
| 1079 | * ufshcd_devfreq_scale - scale up/down UFS clocks and gear |
| 1080 | * @hba: per adapter instance |
| 1081 | * @scale_up: True for scaling up and false for scalin down |
| 1082 | * |
| 1083 | * Returns 0 for success, |
| 1084 | * Returns -EBUSY if scaling can't happen at this time |
| 1085 | * Returns non-zero for any other errors |
| 1086 | */ |
| 1087 | static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up) |
| 1088 | { |
| 1089 | int ret = 0; |
| 1090 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1091 | /* let's not get into low power until clock scaling is completed */ |
| 1092 | ufshcd_hold(hba, false); |
| 1093 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1094 | ret = ufshcd_clock_scaling_prepare(hba); |
| 1095 | if (ret) |
| 1096 | return ret; |
| 1097 | |
| 1098 | /* scale down the gear before scaling down clocks */ |
| 1099 | if (!scale_up) { |
| 1100 | ret = ufshcd_scale_gear(hba, false); |
| 1101 | if (ret) |
| 1102 | goto out; |
| 1103 | } |
| 1104 | |
| 1105 | ret = ufshcd_scale_clks(hba, scale_up); |
| 1106 | if (ret) { |
| 1107 | if (!scale_up) |
| 1108 | ufshcd_scale_gear(hba, true); |
| 1109 | goto out; |
| 1110 | } |
| 1111 | |
| 1112 | /* scale up the gear after scaling up clocks */ |
| 1113 | if (scale_up) { |
| 1114 | ret = ufshcd_scale_gear(hba, true); |
| 1115 | if (ret) { |
| 1116 | ufshcd_scale_clks(hba, false); |
| 1117 | goto out; |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE); |
| 1122 | |
| 1123 | out: |
| 1124 | ufshcd_clock_scaling_unprepare(hba); |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1125 | ufshcd_release(hba); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1126 | return ret; |
| 1127 | } |
| 1128 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1129 | static void ufshcd_clk_scaling_suspend_work(struct work_struct *work) |
| 1130 | { |
| 1131 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1132 | clk_scaling.suspend_work); |
| 1133 | unsigned long irq_flags; |
| 1134 | |
| 1135 | spin_lock_irqsave(hba->host->host_lock, irq_flags); |
| 1136 | if (hba->clk_scaling.active_reqs || hba->clk_scaling.is_suspended) { |
| 1137 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1138 | return; |
| 1139 | } |
| 1140 | hba->clk_scaling.is_suspended = true; |
| 1141 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1142 | |
| 1143 | __ufshcd_suspend_clkscaling(hba); |
| 1144 | } |
| 1145 | |
| 1146 | static void ufshcd_clk_scaling_resume_work(struct work_struct *work) |
| 1147 | { |
| 1148 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1149 | clk_scaling.resume_work); |
| 1150 | unsigned long irq_flags; |
| 1151 | |
| 1152 | spin_lock_irqsave(hba->host->host_lock, irq_flags); |
| 1153 | if (!hba->clk_scaling.is_suspended) { |
| 1154 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1155 | return; |
| 1156 | } |
| 1157 | hba->clk_scaling.is_suspended = false; |
| 1158 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1159 | |
| 1160 | devfreq_resume_device(hba->devfreq); |
| 1161 | } |
| 1162 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1163 | static int ufshcd_devfreq_target(struct device *dev, |
| 1164 | unsigned long *freq, u32 flags) |
| 1165 | { |
| 1166 | int ret = 0; |
| 1167 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1168 | ktime_t start; |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1169 | bool scale_up, sched_clk_scaling_suspend_work = false; |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1170 | unsigned long irq_flags; |
| 1171 | |
| 1172 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 1173 | return -EINVAL; |
| 1174 | |
| 1175 | if ((*freq > 0) && (*freq < UINT_MAX)) { |
| 1176 | dev_err(hba->dev, "%s: invalid freq = %lu\n", __func__, *freq); |
| 1177 | return -EINVAL; |
| 1178 | } |
| 1179 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1180 | spin_lock_irqsave(hba->host->host_lock, irq_flags); |
| 1181 | if (ufshcd_eh_in_progress(hba)) { |
| 1182 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1186 | if (!hba->clk_scaling.active_reqs) |
| 1187 | sched_clk_scaling_suspend_work = true; |
| 1188 | |
| 1189 | scale_up = (*freq == UINT_MAX) ? true : false; |
| 1190 | if (!ufshcd_is_devfreq_scaling_required(hba, scale_up)) { |
| 1191 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1192 | ret = 0; |
| 1193 | goto out; /* no state change required */ |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1194 | } |
| 1195 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 1196 | |
| 1197 | start = ktime_get(); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1198 | ret = ufshcd_devfreq_scale(hba, scale_up); |
| 1199 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1200 | trace_ufshcd_profile_clk_scaling(dev_name(hba->dev), |
| 1201 | (scale_up ? "up" : "down"), |
| 1202 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
| 1203 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1204 | out: |
| 1205 | if (sched_clk_scaling_suspend_work) |
| 1206 | queue_work(hba->clk_scaling.workq, |
| 1207 | &hba->clk_scaling.suspend_work); |
| 1208 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1209 | return ret; |
| 1210 | } |
| 1211 | |
| 1212 | |
| 1213 | static int ufshcd_devfreq_get_dev_status(struct device *dev, |
| 1214 | struct devfreq_dev_status *stat) |
| 1215 | { |
| 1216 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1217 | struct ufs_clk_scaling *scaling = &hba->clk_scaling; |
| 1218 | unsigned long flags; |
| 1219 | |
| 1220 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 1221 | return -EINVAL; |
| 1222 | |
| 1223 | memset(stat, 0, sizeof(*stat)); |
| 1224 | |
| 1225 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1226 | if (!scaling->window_start_t) |
| 1227 | goto start_window; |
| 1228 | |
| 1229 | if (scaling->is_busy_started) |
| 1230 | scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(), |
| 1231 | scaling->busy_start_t)); |
| 1232 | |
| 1233 | stat->total_time = jiffies_to_usecs((long)jiffies - |
| 1234 | (long)scaling->window_start_t); |
| 1235 | stat->busy_time = scaling->tot_busy_t; |
| 1236 | start_window: |
| 1237 | scaling->window_start_t = jiffies; |
| 1238 | scaling->tot_busy_t = 0; |
| 1239 | |
| 1240 | if (hba->outstanding_reqs) { |
| 1241 | scaling->busy_start_t = ktime_get(); |
| 1242 | scaling->is_busy_started = true; |
| 1243 | } else { |
| 1244 | scaling->busy_start_t = 0; |
| 1245 | scaling->is_busy_started = false; |
| 1246 | } |
| 1247 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1248 | return 0; |
| 1249 | } |
| 1250 | |
| 1251 | static struct devfreq_dev_profile ufs_devfreq_profile = { |
| 1252 | .polling_ms = 100, |
| 1253 | .target = ufshcd_devfreq_target, |
| 1254 | .get_dev_status = ufshcd_devfreq_get_dev_status, |
| 1255 | }; |
| 1256 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1257 | static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba) |
| 1258 | { |
| 1259 | unsigned long flags; |
| 1260 | |
| 1261 | devfreq_suspend_device(hba->devfreq); |
| 1262 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1263 | hba->clk_scaling.window_start_t = 0; |
| 1264 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1265 | } |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1266 | |
Gilad Broner | a508253 | 2016-10-17 17:10:00 -0700 | [diff] [blame] | 1267 | static void ufshcd_suspend_clkscaling(struct ufs_hba *hba) |
| 1268 | { |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1269 | unsigned long flags; |
| 1270 | bool suspend = false; |
| 1271 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1272 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 1273 | return; |
| 1274 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1275 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1276 | if (!hba->clk_scaling.is_suspended) { |
| 1277 | suspend = true; |
| 1278 | hba->clk_scaling.is_suspended = true; |
| 1279 | } |
| 1280 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1281 | |
| 1282 | if (suspend) |
| 1283 | __ufshcd_suspend_clkscaling(hba); |
Gilad Broner | a508253 | 2016-10-17 17:10:00 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | static void ufshcd_resume_clkscaling(struct ufs_hba *hba) |
| 1287 | { |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1288 | unsigned long flags; |
| 1289 | bool resume = false; |
| 1290 | |
| 1291 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 1292 | return; |
| 1293 | |
| 1294 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1295 | if (hba->clk_scaling.is_suspended) { |
| 1296 | resume = true; |
| 1297 | hba->clk_scaling.is_suspended = false; |
| 1298 | } |
| 1299 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1300 | |
| 1301 | if (resume) |
| 1302 | devfreq_resume_device(hba->devfreq); |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | static ssize_t ufshcd_clkscale_enable_show(struct device *dev, |
| 1306 | struct device_attribute *attr, char *buf) |
| 1307 | { |
| 1308 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1309 | |
| 1310 | return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_scaling.is_allowed); |
| 1311 | } |
| 1312 | |
| 1313 | static ssize_t ufshcd_clkscale_enable_store(struct device *dev, |
| 1314 | struct device_attribute *attr, const char *buf, size_t count) |
| 1315 | { |
| 1316 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1317 | u32 value; |
| 1318 | int err; |
| 1319 | |
| 1320 | if (kstrtou32(buf, 0, &value)) |
| 1321 | return -EINVAL; |
| 1322 | |
| 1323 | value = !!value; |
| 1324 | if (value == hba->clk_scaling.is_allowed) |
| 1325 | goto out; |
| 1326 | |
| 1327 | pm_runtime_get_sync(hba->dev); |
| 1328 | ufshcd_hold(hba, false); |
| 1329 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1330 | cancel_work_sync(&hba->clk_scaling.suspend_work); |
| 1331 | cancel_work_sync(&hba->clk_scaling.resume_work); |
| 1332 | |
| 1333 | hba->clk_scaling.is_allowed = value; |
| 1334 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1335 | if (value) { |
| 1336 | ufshcd_resume_clkscaling(hba); |
| 1337 | } else { |
| 1338 | ufshcd_suspend_clkscaling(hba); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1339 | err = ufshcd_devfreq_scale(hba, true); |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1340 | if (err) |
| 1341 | dev_err(hba->dev, "%s: failed to scale clocks up %d\n", |
| 1342 | __func__, err); |
| 1343 | } |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1344 | |
| 1345 | ufshcd_release(hba); |
| 1346 | pm_runtime_put_sync(hba->dev); |
| 1347 | out: |
| 1348 | return count; |
Gilad Broner | a508253 | 2016-10-17 17:10:00 -0700 | [diff] [blame] | 1349 | } |
| 1350 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 1351 | static void ufshcd_clkscaling_init_sysfs(struct ufs_hba *hba) |
| 1352 | { |
| 1353 | hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show; |
| 1354 | hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store; |
| 1355 | sysfs_attr_init(&hba->clk_scaling.enable_attr.attr); |
| 1356 | hba->clk_scaling.enable_attr.attr.name = "clkscale_enable"; |
| 1357 | hba->clk_scaling.enable_attr.attr.mode = 0644; |
| 1358 | if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr)) |
| 1359 | dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n"); |
| 1360 | } |
| 1361 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1362 | static void ufshcd_ungate_work(struct work_struct *work) |
| 1363 | { |
| 1364 | int ret; |
| 1365 | unsigned long flags; |
| 1366 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1367 | clk_gating.ungate_work); |
| 1368 | |
| 1369 | cancel_delayed_work_sync(&hba->clk_gating.gate_work); |
| 1370 | |
| 1371 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1372 | if (hba->clk_gating.state == CLKS_ON) { |
| 1373 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1374 | goto unblock_reqs; |
| 1375 | } |
| 1376 | |
| 1377 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1378 | ufshcd_setup_clocks(hba, true); |
| 1379 | |
| 1380 | /* Exit from hibern8 */ |
| 1381 | if (ufshcd_can_hibern8_during_gating(hba)) { |
| 1382 | /* Prevent gating in this path */ |
| 1383 | hba->clk_gating.is_suspended = true; |
| 1384 | if (ufshcd_is_link_hibern8(hba)) { |
| 1385 | ret = ufshcd_uic_hibern8_exit(hba); |
| 1386 | if (ret) |
| 1387 | dev_err(hba->dev, "%s: hibern8 exit failed %d\n", |
| 1388 | __func__, ret); |
| 1389 | else |
| 1390 | ufshcd_set_link_active(hba); |
| 1391 | } |
| 1392 | hba->clk_gating.is_suspended = false; |
| 1393 | } |
| 1394 | unblock_reqs: |
| 1395 | scsi_unblock_requests(hba->host); |
| 1396 | } |
| 1397 | |
| 1398 | /** |
| 1399 | * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release. |
| 1400 | * Also, exit from hibern8 mode and set the link as active. |
| 1401 | * @hba: per adapter instance |
| 1402 | * @async: This indicates whether caller should ungate clocks asynchronously. |
| 1403 | */ |
| 1404 | int ufshcd_hold(struct ufs_hba *hba, bool async) |
| 1405 | { |
| 1406 | int rc = 0; |
| 1407 | unsigned long flags; |
| 1408 | |
| 1409 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1410 | goto out; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1411 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1412 | hba->clk_gating.active_reqs++; |
| 1413 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 1414 | if (ufshcd_eh_in_progress(hba)) { |
| 1415 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1416 | return 0; |
| 1417 | } |
| 1418 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1419 | start: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1420 | switch (hba->clk_gating.state) { |
| 1421 | case CLKS_ON: |
Venkat Gopalakrishnan | f2a785a | 2016-10-17 17:10:53 -0700 | [diff] [blame] | 1422 | /* |
| 1423 | * Wait for the ungate work to complete if in progress. |
| 1424 | * Though the clocks may be in ON state, the link could |
| 1425 | * still be in hibner8 state if hibern8 is allowed |
| 1426 | * during clock gating. |
| 1427 | * Make sure we exit hibern8 state also in addition to |
| 1428 | * clocks being ON. |
| 1429 | */ |
| 1430 | if (ufshcd_can_hibern8_during_gating(hba) && |
| 1431 | ufshcd_is_link_hibern8(hba)) { |
| 1432 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1433 | flush_work(&hba->clk_gating.ungate_work); |
| 1434 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1435 | goto start; |
| 1436 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1437 | break; |
| 1438 | case REQ_CLKS_OFF: |
| 1439 | if (cancel_delayed_work(&hba->clk_gating.gate_work)) { |
| 1440 | hba->clk_gating.state = CLKS_ON; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1441 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1442 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1443 | break; |
| 1444 | } |
| 1445 | /* |
Tomohiro Kusumi | 9c490d2 | 2017-03-28 16:49:26 +0300 | [diff] [blame] | 1446 | * If we are here, it means gating work is either done or |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1447 | * currently running. Hence, fall through to cancel gating |
| 1448 | * work and to enable clocks. |
| 1449 | */ |
| 1450 | case CLKS_OFF: |
| 1451 | scsi_block_requests(hba->host); |
| 1452 | hba->clk_gating.state = REQ_CLKS_ON; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1453 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1454 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1455 | schedule_work(&hba->clk_gating.ungate_work); |
| 1456 | /* |
| 1457 | * fall through to check if we should wait for this |
| 1458 | * work to be done or not. |
| 1459 | */ |
| 1460 | case REQ_CLKS_ON: |
| 1461 | if (async) { |
| 1462 | rc = -EAGAIN; |
| 1463 | hba->clk_gating.active_reqs--; |
| 1464 | break; |
| 1465 | } |
| 1466 | |
| 1467 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1468 | flush_work(&hba->clk_gating.ungate_work); |
| 1469 | /* Make sure state is CLKS_ON before returning */ |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1470 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1471 | goto start; |
| 1472 | default: |
| 1473 | dev_err(hba->dev, "%s: clk gating is in invalid state %d\n", |
| 1474 | __func__, hba->clk_gating.state); |
| 1475 | break; |
| 1476 | } |
| 1477 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1478 | out: |
| 1479 | return rc; |
| 1480 | } |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 1481 | EXPORT_SYMBOL_GPL(ufshcd_hold); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1482 | |
| 1483 | static void ufshcd_gate_work(struct work_struct *work) |
| 1484 | { |
| 1485 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1486 | clk_gating.gate_work.work); |
| 1487 | unsigned long flags; |
| 1488 | |
| 1489 | spin_lock_irqsave(hba->host->host_lock, flags); |
Venkat Gopalakrishnan | 3f0c06d | 2016-10-17 17:11:07 -0700 | [diff] [blame] | 1490 | /* |
| 1491 | * In case you are here to cancel this work the gating state |
| 1492 | * would be marked as REQ_CLKS_ON. In this case save time by |
| 1493 | * skipping the gating work and exit after changing the clock |
| 1494 | * state to CLKS_ON. |
| 1495 | */ |
| 1496 | if (hba->clk_gating.is_suspended || |
| 1497 | (hba->clk_gating.state == REQ_CLKS_ON)) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1498 | hba->clk_gating.state = CLKS_ON; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1499 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1500 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1501 | goto rel_lock; |
| 1502 | } |
| 1503 | |
| 1504 | if (hba->clk_gating.active_reqs |
| 1505 | || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL |
| 1506 | || hba->lrb_in_use || hba->outstanding_tasks |
| 1507 | || hba->active_uic_cmd || hba->uic_async_done) |
| 1508 | goto rel_lock; |
| 1509 | |
| 1510 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1511 | |
| 1512 | /* put the link into hibern8 mode before turning off clocks */ |
| 1513 | if (ufshcd_can_hibern8_during_gating(hba)) { |
| 1514 | if (ufshcd_uic_hibern8_enter(hba)) { |
| 1515 | hba->clk_gating.state = CLKS_ON; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1516 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1517 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1518 | goto out; |
| 1519 | } |
| 1520 | ufshcd_set_link_hibern8(hba); |
| 1521 | } |
| 1522 | |
| 1523 | if (!ufshcd_is_link_active(hba)) |
| 1524 | ufshcd_setup_clocks(hba, false); |
| 1525 | else |
| 1526 | /* If link is active, device ref_clk can't be switched off */ |
| 1527 | __ufshcd_setup_clocks(hba, false, true); |
| 1528 | |
| 1529 | /* |
| 1530 | * In case you are here to cancel this work the gating state |
| 1531 | * would be marked as REQ_CLKS_ON. In this case keep the state |
| 1532 | * as REQ_CLKS_ON which would anyway imply that clocks are off |
| 1533 | * and a request to turn them on is pending. By doing this way, |
| 1534 | * we keep the state machine in tact and this would ultimately |
| 1535 | * prevent from doing cancel work multiple times when there are |
| 1536 | * new requests arriving before the current cancel work is done. |
| 1537 | */ |
| 1538 | spin_lock_irqsave(hba->host->host_lock, flags); |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1539 | if (hba->clk_gating.state == REQ_CLKS_OFF) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1540 | hba->clk_gating.state = CLKS_OFF; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1541 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1542 | hba->clk_gating.state); |
| 1543 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1544 | rel_lock: |
| 1545 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1546 | out: |
| 1547 | return; |
| 1548 | } |
| 1549 | |
| 1550 | /* host lock must be held before calling this variant */ |
| 1551 | static void __ufshcd_release(struct ufs_hba *hba) |
| 1552 | { |
| 1553 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1554 | return; |
| 1555 | |
| 1556 | hba->clk_gating.active_reqs--; |
| 1557 | |
| 1558 | if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended |
| 1559 | || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL |
| 1560 | || hba->lrb_in_use || hba->outstanding_tasks |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 1561 | || hba->active_uic_cmd || hba->uic_async_done |
| 1562 | || ufshcd_eh_in_progress(hba)) |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1563 | return; |
| 1564 | |
| 1565 | hba->clk_gating.state = REQ_CLKS_OFF; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 1566 | trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1567 | schedule_delayed_work(&hba->clk_gating.gate_work, |
| 1568 | msecs_to_jiffies(hba->clk_gating.delay_ms)); |
| 1569 | } |
| 1570 | |
| 1571 | void ufshcd_release(struct ufs_hba *hba) |
| 1572 | { |
| 1573 | unsigned long flags; |
| 1574 | |
| 1575 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1576 | __ufshcd_release(hba); |
| 1577 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1578 | } |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 1579 | EXPORT_SYMBOL_GPL(ufshcd_release); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1580 | |
| 1581 | static ssize_t ufshcd_clkgate_delay_show(struct device *dev, |
| 1582 | struct device_attribute *attr, char *buf) |
| 1583 | { |
| 1584 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1585 | |
| 1586 | return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms); |
| 1587 | } |
| 1588 | |
| 1589 | static ssize_t ufshcd_clkgate_delay_store(struct device *dev, |
| 1590 | struct device_attribute *attr, const char *buf, size_t count) |
| 1591 | { |
| 1592 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1593 | unsigned long flags, value; |
| 1594 | |
| 1595 | if (kstrtoul(buf, 0, &value)) |
| 1596 | return -EINVAL; |
| 1597 | |
| 1598 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1599 | hba->clk_gating.delay_ms = value; |
| 1600 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1601 | return count; |
| 1602 | } |
| 1603 | |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1604 | static ssize_t ufshcd_clkgate_enable_show(struct device *dev, |
| 1605 | struct device_attribute *attr, char *buf) |
| 1606 | { |
| 1607 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1608 | |
| 1609 | return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_gating.is_enabled); |
| 1610 | } |
| 1611 | |
| 1612 | static ssize_t ufshcd_clkgate_enable_store(struct device *dev, |
| 1613 | struct device_attribute *attr, const char *buf, size_t count) |
| 1614 | { |
| 1615 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1616 | unsigned long flags; |
| 1617 | u32 value; |
| 1618 | |
| 1619 | if (kstrtou32(buf, 0, &value)) |
| 1620 | return -EINVAL; |
| 1621 | |
| 1622 | value = !!value; |
| 1623 | if (value == hba->clk_gating.is_enabled) |
| 1624 | goto out; |
| 1625 | |
| 1626 | if (value) { |
| 1627 | ufshcd_release(hba); |
| 1628 | } else { |
| 1629 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1630 | hba->clk_gating.active_reqs++; |
| 1631 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1632 | } |
| 1633 | |
| 1634 | hba->clk_gating.is_enabled = value; |
| 1635 | out: |
| 1636 | return count; |
| 1637 | } |
| 1638 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1639 | static void ufshcd_init_clk_gating(struct ufs_hba *hba) |
| 1640 | { |
| 1641 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1642 | return; |
| 1643 | |
| 1644 | hba->clk_gating.delay_ms = 150; |
| 1645 | INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work); |
| 1646 | INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work); |
| 1647 | |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1648 | hba->clk_gating.is_enabled = true; |
| 1649 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1650 | hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show; |
| 1651 | hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store; |
| 1652 | sysfs_attr_init(&hba->clk_gating.delay_attr.attr); |
| 1653 | hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms"; |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1654 | hba->clk_gating.delay_attr.attr.mode = 0644; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1655 | if (device_create_file(hba->dev, &hba->clk_gating.delay_attr)) |
| 1656 | dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n"); |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1657 | |
| 1658 | hba->clk_gating.enable_attr.show = ufshcd_clkgate_enable_show; |
| 1659 | hba->clk_gating.enable_attr.store = ufshcd_clkgate_enable_store; |
| 1660 | sysfs_attr_init(&hba->clk_gating.enable_attr.attr); |
| 1661 | hba->clk_gating.enable_attr.attr.name = "clkgate_enable"; |
| 1662 | hba->clk_gating.enable_attr.attr.mode = 0644; |
| 1663 | if (device_create_file(hba->dev, &hba->clk_gating.enable_attr)) |
| 1664 | dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n"); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | static void ufshcd_exit_clk_gating(struct ufs_hba *hba) |
| 1668 | { |
| 1669 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1670 | return; |
| 1671 | device_remove_file(hba->dev, &hba->clk_gating.delay_attr); |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 1672 | device_remove_file(hba->dev, &hba->clk_gating.enable_attr); |
Akinobu Mita | 97cd680 | 2014-11-24 14:24:18 +0900 | [diff] [blame] | 1673 | cancel_work_sync(&hba->clk_gating.ungate_work); |
| 1674 | cancel_delayed_work_sync(&hba->clk_gating.gate_work); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1675 | } |
| 1676 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1677 | /* Must be called with host lock acquired */ |
| 1678 | static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba) |
| 1679 | { |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1680 | bool queue_resume_work = false; |
| 1681 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1682 | if (!ufshcd_is_clkscaling_supported(hba)) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1683 | return; |
| 1684 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 1685 | if (!hba->clk_scaling.active_reqs++) |
| 1686 | queue_resume_work = true; |
| 1687 | |
| 1688 | if (!hba->clk_scaling.is_allowed || hba->pm_op_in_progress) |
| 1689 | return; |
| 1690 | |
| 1691 | if (queue_resume_work) |
| 1692 | queue_work(hba->clk_scaling.workq, |
| 1693 | &hba->clk_scaling.resume_work); |
| 1694 | |
| 1695 | if (!hba->clk_scaling.window_start_t) { |
| 1696 | hba->clk_scaling.window_start_t = jiffies; |
| 1697 | hba->clk_scaling.tot_busy_t = 0; |
| 1698 | hba->clk_scaling.is_busy_started = false; |
| 1699 | } |
| 1700 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1701 | if (!hba->clk_scaling.is_busy_started) { |
| 1702 | hba->clk_scaling.busy_start_t = ktime_get(); |
| 1703 | hba->clk_scaling.is_busy_started = true; |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba) |
| 1708 | { |
| 1709 | struct ufs_clk_scaling *scaling = &hba->clk_scaling; |
| 1710 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 1711 | if (!ufshcd_is_clkscaling_supported(hba)) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1712 | return; |
| 1713 | |
| 1714 | if (!hba->outstanding_reqs && scaling->is_busy_started) { |
| 1715 | scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(), |
| 1716 | scaling->busy_start_t)); |
Thomas Gleixner | 8b0e195 | 2016-12-25 12:30:41 +0100 | [diff] [blame] | 1717 | scaling->busy_start_t = 0; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1718 | scaling->is_busy_started = false; |
| 1719 | } |
| 1720 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1721 | /** |
| 1722 | * ufshcd_send_command - Send SCSI or device management commands |
| 1723 | * @hba: per adapter instance |
| 1724 | * @task_tag: Task tag of the command |
| 1725 | */ |
| 1726 | static inline |
| 1727 | void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag) |
| 1728 | { |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 1729 | hba->lrb[task_tag].issue_time_stamp = ktime_get(); |
Zang Leigang | 0901718 | 2017-09-27 10:06:06 +0800 | [diff] [blame] | 1730 | hba->lrb[task_tag].compl_time_stamp = ktime_set(0, 0); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1731 | ufshcd_clk_scaling_start_busy(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1732 | __set_bit(task_tag, &hba->outstanding_reqs); |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 1733 | ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
Gilad Broner | ad1a1b9 | 2016-10-17 17:09:36 -0700 | [diff] [blame] | 1734 | /* Make sure that doorbell is committed immediately */ |
| 1735 | wmb(); |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 1736 | ufshcd_add_command_trace(hba, task_tag, "send"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | /** |
| 1740 | * ufshcd_copy_sense_data - Copy sense data in case of check condition |
| 1741 | * @lrb - pointer to local reference block |
| 1742 | */ |
| 1743 | static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp) |
| 1744 | { |
| 1745 | int len; |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 1746 | if (lrbp->sense_buffer && |
| 1747 | ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) { |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 1748 | int len_to_copy; |
| 1749 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1750 | len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len); |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 1751 | len_to_copy = min_t(int, RESPONSE_UPIU_SENSE_DATA_LENGTH, len); |
| 1752 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1753 | memcpy(lrbp->sense_buffer, |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1754 | lrbp->ucd_rsp_ptr->sr.sense_data, |
Gilad Broner | dcea0bf | 2016-10-17 17:09:48 -0700 | [diff] [blame] | 1755 | min_t(int, len_to_copy, UFSHCD_REQ_SENSE_SIZE)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1760 | * ufshcd_copy_query_response() - Copy the Query Response and the data |
| 1761 | * descriptor |
| 1762 | * @hba: per adapter instance |
| 1763 | * @lrb - pointer to local reference block |
| 1764 | */ |
| 1765 | static |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1766 | 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] | 1767 | { |
| 1768 | struct ufs_query_res *query_res = &hba->dev_cmd.query.response; |
| 1769 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1770 | 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] | 1771 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1772 | /* Get the descriptor */ |
| 1773 | if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 1774 | u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1775 | GENERAL_UPIU_REQUEST_SIZE; |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1776 | u16 resp_len; |
| 1777 | u16 buf_len; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1778 | |
| 1779 | /* data segment length */ |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1780 | resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) & |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1781 | MASK_QUERY_DATA_SEG_LEN; |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 1782 | buf_len = be16_to_cpu( |
| 1783 | hba->dev_cmd.query.request.upiu_req.length); |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1784 | if (likely(buf_len >= resp_len)) { |
| 1785 | memcpy(hba->dev_cmd.query.descriptor, descp, resp_len); |
| 1786 | } else { |
| 1787 | dev_warn(hba->dev, |
| 1788 | "%s: Response size is bigger than buffer", |
| 1789 | __func__); |
| 1790 | return -EINVAL; |
| 1791 | } |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1792 | } |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1793 | |
| 1794 | return 0; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1798 | * ufshcd_hba_capabilities - Read controller capabilities |
| 1799 | * @hba: per adapter instance |
| 1800 | */ |
| 1801 | static inline void ufshcd_hba_capabilities(struct ufs_hba *hba) |
| 1802 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 1803 | hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1804 | |
| 1805 | /* nutrs and nutmrs are 0 based values */ |
| 1806 | hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1; |
| 1807 | hba->nutmrs = |
| 1808 | ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1; |
| 1809 | } |
| 1810 | |
| 1811 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1812 | * ufshcd_ready_for_uic_cmd - Check if controller is ready |
| 1813 | * to accept UIC commands |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1814 | * @hba: per adapter instance |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1815 | * Return true on success, else false |
| 1816 | */ |
| 1817 | static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba) |
| 1818 | { |
| 1819 | if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY) |
| 1820 | return true; |
| 1821 | else |
| 1822 | return false; |
| 1823 | } |
| 1824 | |
| 1825 | /** |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 1826 | * ufshcd_get_upmcrs - Get the power mode change request status |
| 1827 | * @hba: Pointer to adapter instance |
| 1828 | * |
| 1829 | * This function gets the UPMCRS field of HCS register |
| 1830 | * Returns value of UPMCRS field |
| 1831 | */ |
| 1832 | static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba) |
| 1833 | { |
| 1834 | return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7; |
| 1835 | } |
| 1836 | |
| 1837 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1838 | * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers |
| 1839 | * @hba: per adapter instance |
| 1840 | * @uic_cmd: UIC command |
| 1841 | * |
| 1842 | * Mutex must be held. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1843 | */ |
| 1844 | static inline void |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1845 | 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] | 1846 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1847 | WARN_ON(hba->active_uic_cmd); |
| 1848 | |
| 1849 | hba->active_uic_cmd = uic_cmd; |
| 1850 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1851 | /* Write Args */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1852 | ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1); |
| 1853 | ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2); |
| 1854 | ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1855 | |
| 1856 | /* Write UIC Cmd */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1857 | ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK, |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 1858 | REG_UIC_COMMAND); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1862 | * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command |
| 1863 | * @hba: per adapter instance |
| 1864 | * @uic_command: UIC command |
| 1865 | * |
| 1866 | * Must be called with mutex held. |
| 1867 | * Returns 0 only if success. |
| 1868 | */ |
| 1869 | static int |
| 1870 | ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) |
| 1871 | { |
| 1872 | int ret; |
| 1873 | unsigned long flags; |
| 1874 | |
| 1875 | if (wait_for_completion_timeout(&uic_cmd->done, |
| 1876 | msecs_to_jiffies(UIC_CMD_TIMEOUT))) |
| 1877 | ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT; |
| 1878 | else |
| 1879 | ret = -ETIMEDOUT; |
| 1880 | |
| 1881 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1882 | hba->active_uic_cmd = NULL; |
| 1883 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1884 | |
| 1885 | return ret; |
| 1886 | } |
| 1887 | |
| 1888 | /** |
| 1889 | * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result |
| 1890 | * @hba: per adapter instance |
| 1891 | * @uic_cmd: UIC command |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 1892 | * @completion: initialize the completion only if this is set to true |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1893 | * |
| 1894 | * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 1895 | * with mutex held and host_lock locked. |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1896 | * Returns 0 only if success. |
| 1897 | */ |
| 1898 | static int |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 1899 | __ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd, |
| 1900 | bool completion) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1901 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1902 | if (!ufshcd_ready_for_uic_cmd(hba)) { |
| 1903 | dev_err(hba->dev, |
| 1904 | "Controller not ready to accept UIC commands\n"); |
| 1905 | return -EIO; |
| 1906 | } |
| 1907 | |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 1908 | if (completion) |
| 1909 | init_completion(&uic_cmd->done); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1910 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1911 | ufshcd_dispatch_uic_cmd(hba, uic_cmd); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1912 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 1913 | return 0; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | /** |
| 1917 | * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result |
| 1918 | * @hba: per adapter instance |
| 1919 | * @uic_cmd: UIC command |
| 1920 | * |
| 1921 | * Returns 0 only if success. |
| 1922 | */ |
| 1923 | static int |
| 1924 | ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) |
| 1925 | { |
| 1926 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 1927 | unsigned long flags; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1928 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1929 | ufshcd_hold(hba, false); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1930 | mutex_lock(&hba->uic_cmd_mutex); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 1931 | ufshcd_add_delay_before_dme_cmd(hba); |
| 1932 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 1933 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 1934 | ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 1935 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1936 | if (!ret) |
| 1937 | ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd); |
| 1938 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1939 | mutex_unlock(&hba->uic_cmd_mutex); |
| 1940 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1941 | ufshcd_release(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1942 | return ret; |
| 1943 | } |
| 1944 | |
| 1945 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1946 | * ufshcd_map_sg - Map scatter-gather list to prdt |
| 1947 | * @lrbp - pointer to local reference block |
| 1948 | * |
| 1949 | * Returns 0 in case of success, non-zero value in case of failure |
| 1950 | */ |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 1951 | 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] | 1952 | { |
| 1953 | struct ufshcd_sg_entry *prd_table; |
| 1954 | struct scatterlist *sg; |
| 1955 | struct scsi_cmnd *cmd; |
| 1956 | int sg_segments; |
| 1957 | int i; |
| 1958 | |
| 1959 | cmd = lrbp->cmd; |
| 1960 | sg_segments = scsi_dma_map(cmd); |
| 1961 | if (sg_segments < 0) |
| 1962 | return sg_segments; |
| 1963 | |
| 1964 | if (sg_segments) { |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 1965 | if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) |
| 1966 | lrbp->utr_descriptor_ptr->prd_table_length = |
| 1967 | cpu_to_le16((u16)(sg_segments * |
| 1968 | sizeof(struct ufshcd_sg_entry))); |
| 1969 | else |
| 1970 | lrbp->utr_descriptor_ptr->prd_table_length = |
| 1971 | cpu_to_le16((u16) (sg_segments)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1972 | |
| 1973 | prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr; |
| 1974 | |
| 1975 | scsi_for_each_sg(cmd, sg, sg_segments, i) { |
| 1976 | prd_table[i].size = |
| 1977 | cpu_to_le32(((u32) sg_dma_len(sg))-1); |
| 1978 | prd_table[i].base_addr = |
| 1979 | cpu_to_le32(lower_32_bits(sg->dma_address)); |
| 1980 | prd_table[i].upper_addr = |
| 1981 | cpu_to_le32(upper_32_bits(sg->dma_address)); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 1982 | prd_table[i].reserved = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1983 | } |
| 1984 | } else { |
| 1985 | lrbp->utr_descriptor_ptr->prd_table_length = 0; |
| 1986 | } |
| 1987 | |
| 1988 | return 0; |
| 1989 | } |
| 1990 | |
| 1991 | /** |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 1992 | * ufshcd_enable_intr - enable interrupts |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1993 | * @hba: per adapter instance |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 1994 | * @intrs: interrupt bits |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1995 | */ |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 1996 | static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1997 | { |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 1998 | u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
| 1999 | |
| 2000 | if (hba->ufs_version == UFSHCI_VERSION_10) { |
| 2001 | u32 rw; |
| 2002 | rw = set & INTERRUPT_MASK_RW_VER_10; |
| 2003 | set = rw | ((set ^ intrs) & intrs); |
| 2004 | } else { |
| 2005 | set |= intrs; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2006 | } |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2007 | |
| 2008 | ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE); |
| 2009 | } |
| 2010 | |
| 2011 | /** |
| 2012 | * ufshcd_disable_intr - disable interrupts |
| 2013 | * @hba: per adapter instance |
| 2014 | * @intrs: interrupt bits |
| 2015 | */ |
| 2016 | static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs) |
| 2017 | { |
| 2018 | u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
| 2019 | |
| 2020 | if (hba->ufs_version == UFSHCI_VERSION_10) { |
| 2021 | u32 rw; |
| 2022 | rw = (set & INTERRUPT_MASK_RW_VER_10) & |
| 2023 | ~(intrs & INTERRUPT_MASK_RW_VER_10); |
| 2024 | set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10); |
| 2025 | |
| 2026 | } else { |
| 2027 | set &= ~intrs; |
| 2028 | } |
| 2029 | |
| 2030 | ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2034 | * ufshcd_prepare_req_desc_hdr() - Fills the requests header |
| 2035 | * descriptor according to request |
| 2036 | * @lrbp: pointer to local reference block |
| 2037 | * @upiu_flags: flags required in the header |
| 2038 | * @cmd_dir: requests data direction |
| 2039 | */ |
| 2040 | static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp, |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2041 | u32 *upiu_flags, enum dma_data_direction cmd_dir) |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2042 | { |
| 2043 | struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr; |
| 2044 | u32 data_direction; |
| 2045 | u32 dword_0; |
| 2046 | |
| 2047 | if (cmd_dir == DMA_FROM_DEVICE) { |
| 2048 | data_direction = UTP_DEVICE_TO_HOST; |
| 2049 | *upiu_flags = UPIU_CMD_FLAGS_READ; |
| 2050 | } else if (cmd_dir == DMA_TO_DEVICE) { |
| 2051 | data_direction = UTP_HOST_TO_DEVICE; |
| 2052 | *upiu_flags = UPIU_CMD_FLAGS_WRITE; |
| 2053 | } else { |
| 2054 | data_direction = UTP_NO_DATA_TRANSFER; |
| 2055 | *upiu_flags = UPIU_CMD_FLAGS_NONE; |
| 2056 | } |
| 2057 | |
| 2058 | dword_0 = data_direction | (lrbp->command_type |
| 2059 | << UPIU_COMMAND_TYPE_OFFSET); |
| 2060 | if (lrbp->intr_cmd) |
| 2061 | dword_0 |= UTP_REQ_DESC_INT_CMD; |
| 2062 | |
| 2063 | /* Transfer request descriptor header fields */ |
| 2064 | req_desc->header.dword_0 = cpu_to_le32(dword_0); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2065 | /* dword_1 is reserved, hence it is set to 0 */ |
| 2066 | req_desc->header.dword_1 = 0; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2067 | /* |
| 2068 | * assigning invalid value for command status. Controller |
| 2069 | * updates OCS on command completion, with the command |
| 2070 | * status |
| 2071 | */ |
| 2072 | req_desc->header.dword_2 = |
| 2073 | cpu_to_le32(OCS_INVALID_COMMAND_STATUS); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2074 | /* dword_3 is reserved, hence it is set to 0 */ |
| 2075 | req_desc->header.dword_3 = 0; |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 2076 | |
| 2077 | req_desc->prd_table_length = 0; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2078 | } |
| 2079 | |
| 2080 | /** |
| 2081 | * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc, |
| 2082 | * for scsi commands |
| 2083 | * @lrbp - local reference block pointer |
| 2084 | * @upiu_flags - flags |
| 2085 | */ |
| 2086 | static |
| 2087 | void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags) |
| 2088 | { |
| 2089 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2090 | unsigned short cdb_len; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2091 | |
| 2092 | /* command descriptor fields */ |
| 2093 | ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD( |
| 2094 | UPIU_TRANSACTION_COMMAND, upiu_flags, |
| 2095 | lrbp->lun, lrbp->task_tag); |
| 2096 | ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD( |
| 2097 | UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0); |
| 2098 | |
| 2099 | /* Total EHS length and Data segment length will be zero */ |
| 2100 | ucd_req_ptr->header.dword_2 = 0; |
| 2101 | |
| 2102 | ucd_req_ptr->sc.exp_data_transfer_len = |
| 2103 | cpu_to_be32(lrbp->cmd->sdb.length); |
| 2104 | |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2105 | cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE); |
| 2106 | memset(ucd_req_ptr->sc.cdb, 0, MAX_CDB_SIZE); |
| 2107 | memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len); |
| 2108 | |
| 2109 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2110 | } |
| 2111 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2112 | /** |
| 2113 | * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc, |
| 2114 | * for query requsts |
| 2115 | * @hba: UFS hba |
| 2116 | * @lrbp: local reference block pointer |
| 2117 | * @upiu_flags: flags |
| 2118 | */ |
| 2119 | static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba, |
| 2120 | struct ufshcd_lrb *lrbp, u32 upiu_flags) |
| 2121 | { |
| 2122 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
| 2123 | struct ufs_query *query = &hba->dev_cmd.query; |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 2124 | u16 len = be16_to_cpu(query->request.upiu_req.length); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2125 | u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE; |
| 2126 | |
| 2127 | /* Query request header */ |
| 2128 | ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD( |
| 2129 | UPIU_TRANSACTION_QUERY_REQ, upiu_flags, |
| 2130 | lrbp->lun, lrbp->task_tag); |
| 2131 | ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD( |
| 2132 | 0, query->request.query_func, 0, 0); |
| 2133 | |
Zang Leigang | 6861285 | 2016-08-25 17:39:19 +0800 | [diff] [blame] | 2134 | /* Data segment length only need for WRITE_DESC */ |
| 2135 | if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) |
| 2136 | ucd_req_ptr->header.dword_2 = |
| 2137 | UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len); |
| 2138 | else |
| 2139 | ucd_req_ptr->header.dword_2 = 0; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2140 | |
| 2141 | /* Copy the Query Request buffer as is */ |
| 2142 | memcpy(&ucd_req_ptr->qr, &query->request.upiu_req, |
| 2143 | QUERY_OSF_SIZE); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2144 | |
| 2145 | /* Copy the Descriptor */ |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2146 | if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) |
| 2147 | memcpy(descp, query->descriptor, len); |
| 2148 | |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 2149 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2150 | } |
| 2151 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2152 | static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp) |
| 2153 | { |
| 2154 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
| 2155 | |
| 2156 | memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req)); |
| 2157 | |
| 2158 | /* command descriptor fields */ |
| 2159 | ucd_req_ptr->header.dword_0 = |
| 2160 | UPIU_HEADER_DWORD( |
| 2161 | UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag); |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 2162 | /* clear rest of the fields of basic header */ |
| 2163 | ucd_req_ptr->header.dword_1 = 0; |
| 2164 | ucd_req_ptr->header.dword_2 = 0; |
| 2165 | |
| 2166 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | /** |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2170 | * ufshcd_comp_devman_upiu - UFS Protocol Information Unit(UPIU) |
| 2171 | * for Device Management Purposes |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2172 | * @hba - per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2173 | * @lrb - pointer to local reference block |
| 2174 | */ |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2175 | 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] | 2176 | { |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2177 | u32 upiu_flags; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2178 | int ret = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2179 | |
kehuanlin | 83dc7e3 | 2017-09-06 17:58:39 +0800 | [diff] [blame] | 2180 | if ((hba->ufs_version == UFSHCI_VERSION_10) || |
| 2181 | (hba->ufs_version == UFSHCI_VERSION_11)) |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2182 | lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE; |
kehuanlin | 83dc7e3 | 2017-09-06 17:58:39 +0800 | [diff] [blame] | 2183 | else |
| 2184 | lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE; |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2185 | |
| 2186 | ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE); |
| 2187 | if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY) |
| 2188 | ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags); |
| 2189 | else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP) |
| 2190 | ufshcd_prepare_utp_nop_upiu(lrbp); |
| 2191 | else |
| 2192 | ret = -EINVAL; |
| 2193 | |
| 2194 | return ret; |
| 2195 | } |
| 2196 | |
| 2197 | /** |
| 2198 | * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU) |
| 2199 | * for SCSI Purposes |
| 2200 | * @hba - per adapter instance |
| 2201 | * @lrb - pointer to local reference block |
| 2202 | */ |
| 2203 | static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 2204 | { |
| 2205 | u32 upiu_flags; |
| 2206 | int ret = 0; |
| 2207 | |
kehuanlin | 83dc7e3 | 2017-09-06 17:58:39 +0800 | [diff] [blame] | 2208 | if ((hba->ufs_version == UFSHCI_VERSION_10) || |
| 2209 | (hba->ufs_version == UFSHCI_VERSION_11)) |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2210 | lrbp->command_type = UTP_CMD_TYPE_SCSI; |
kehuanlin | 83dc7e3 | 2017-09-06 17:58:39 +0800 | [diff] [blame] | 2211 | else |
| 2212 | lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE; |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2213 | |
| 2214 | if (likely(lrbp->cmd)) { |
| 2215 | ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, |
| 2216 | lrbp->cmd->sc_data_direction); |
| 2217 | ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags); |
| 2218 | } else { |
| 2219 | ret = -EINVAL; |
| 2220 | } |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2221 | |
| 2222 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2223 | } |
| 2224 | |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 2225 | /* |
| 2226 | * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN |
| 2227 | * @scsi_lun: scsi LUN id |
| 2228 | * |
| 2229 | * Returns UPIU LUN id |
| 2230 | */ |
| 2231 | static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun) |
| 2232 | { |
| 2233 | if (scsi_is_wlun(scsi_lun)) |
| 2234 | return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID) |
| 2235 | | UFS_UPIU_WLUN_ID; |
| 2236 | else |
| 2237 | return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID; |
| 2238 | } |
| 2239 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2240 | /** |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 2241 | * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID |
| 2242 | * @scsi_lun: UPIU W-LUN id |
| 2243 | * |
| 2244 | * Returns SCSI W-LUN id |
| 2245 | */ |
| 2246 | static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id) |
| 2247 | { |
| 2248 | return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE; |
| 2249 | } |
| 2250 | |
| 2251 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2252 | * ufshcd_queuecommand - main entry point for SCSI requests |
| 2253 | * @cmd: command from SCSI Midlayer |
| 2254 | * @done: call back function |
| 2255 | * |
| 2256 | * Returns 0 for success, non-zero in case of failure |
| 2257 | */ |
| 2258 | static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) |
| 2259 | { |
| 2260 | struct ufshcd_lrb *lrbp; |
| 2261 | struct ufs_hba *hba; |
| 2262 | unsigned long flags; |
| 2263 | int tag; |
| 2264 | int err = 0; |
| 2265 | |
| 2266 | hba = shost_priv(host); |
| 2267 | |
| 2268 | tag = cmd->request->tag; |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 2269 | if (!ufshcd_valid_tag(hba, tag)) { |
| 2270 | dev_err(hba->dev, |
| 2271 | "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p", |
| 2272 | __func__, tag, cmd, cmd->request); |
| 2273 | BUG(); |
| 2274 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2275 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 2276 | if (!down_read_trylock(&hba->clk_scaling_lock)) |
| 2277 | return SCSI_MLQUEUE_HOST_BUSY; |
| 2278 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2279 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2280 | switch (hba->ufshcd_state) { |
| 2281 | case UFSHCD_STATE_OPERATIONAL: |
| 2282 | break; |
Zang Leigang | 141f816 | 2016-11-16 11:29:37 +0800 | [diff] [blame] | 2283 | case UFSHCD_STATE_EH_SCHEDULED: |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2284 | case UFSHCD_STATE_RESET: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2285 | err = SCSI_MLQUEUE_HOST_BUSY; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2286 | goto out_unlock; |
| 2287 | case UFSHCD_STATE_ERROR: |
| 2288 | set_host_byte(cmd, DID_ERROR); |
| 2289 | cmd->scsi_done(cmd); |
| 2290 | goto out_unlock; |
| 2291 | default: |
| 2292 | dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n", |
| 2293 | __func__, hba->ufshcd_state); |
| 2294 | set_host_byte(cmd, DID_BAD_TARGET); |
| 2295 | cmd->scsi_done(cmd); |
| 2296 | goto out_unlock; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2297 | } |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 2298 | |
| 2299 | /* if error handling is in progress, don't issue commands */ |
| 2300 | if (ufshcd_eh_in_progress(hba)) { |
| 2301 | set_host_byte(cmd, DID_ERROR); |
| 2302 | cmd->scsi_done(cmd); |
| 2303 | goto out_unlock; |
| 2304 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2305 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2306 | |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 2307 | hba->req_abort_count = 0; |
| 2308 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2309 | /* acquire the tag to make sure device cmds don't use it */ |
| 2310 | if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) { |
| 2311 | /* |
| 2312 | * Dev manage command in progress, requeue the command. |
| 2313 | * Requeuing the command helps in cases where the request *may* |
| 2314 | * find different tag instead of waiting for dev manage command |
| 2315 | * completion. |
| 2316 | */ |
| 2317 | err = SCSI_MLQUEUE_HOST_BUSY; |
| 2318 | goto out; |
| 2319 | } |
| 2320 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2321 | err = ufshcd_hold(hba, true); |
| 2322 | if (err) { |
| 2323 | err = SCSI_MLQUEUE_HOST_BUSY; |
| 2324 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 2325 | goto out; |
| 2326 | } |
| 2327 | WARN_ON(hba->clk_gating.state != CLKS_ON); |
| 2328 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2329 | lrbp = &hba->lrb[tag]; |
| 2330 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2331 | WARN_ON(lrbp->cmd); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2332 | lrbp->cmd = cmd; |
Gilad Broner | dcea0bf | 2016-10-17 17:09:48 -0700 | [diff] [blame] | 2333 | lrbp->sense_bufflen = UFSHCD_REQ_SENSE_SIZE; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2334 | lrbp->sense_buffer = cmd->sense_buffer; |
| 2335 | lrbp->task_tag = tag; |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 2336 | lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun); |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 2337 | lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false; |
Gilad Broner | e0b299e | 2017-02-03 16:56:40 -0800 | [diff] [blame] | 2338 | lrbp->req_abort_skip = false; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2339 | |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2340 | ufshcd_comp_scsi_upiu(hba, lrbp); |
| 2341 | |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 2342 | err = ufshcd_map_sg(hba, lrbp); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2343 | if (err) { |
| 2344 | lrbp->cmd = NULL; |
| 2345 | clear_bit_unlock(tag, &hba->lrb_in_use); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2346 | goto out; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2347 | } |
Gilad Broner | ad1a1b9 | 2016-10-17 17:09:36 -0700 | [diff] [blame] | 2348 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 2349 | wmb(); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2350 | |
| 2351 | /* issue command to the controller */ |
| 2352 | spin_lock_irqsave(hba->host->host_lock, flags); |
Kiwoong Kim | 0e675ef | 2016-11-10 21:14:36 +0900 | [diff] [blame] | 2353 | ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2354 | ufshcd_send_command(hba, tag); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2355 | out_unlock: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2356 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2357 | out: |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 2358 | up_read(&hba->clk_scaling_lock); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2359 | return err; |
| 2360 | } |
| 2361 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2362 | static int ufshcd_compose_dev_cmd(struct ufs_hba *hba, |
| 2363 | struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag) |
| 2364 | { |
| 2365 | lrbp->cmd = NULL; |
| 2366 | lrbp->sense_bufflen = 0; |
| 2367 | lrbp->sense_buffer = NULL; |
| 2368 | lrbp->task_tag = tag; |
| 2369 | 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] | 2370 | lrbp->intr_cmd = true; /* No interrupt aggregation */ |
| 2371 | hba->dev_cmd.type = cmd_type; |
| 2372 | |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2373 | return ufshcd_comp_devman_upiu(hba, lrbp); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2374 | } |
| 2375 | |
| 2376 | static int |
| 2377 | ufshcd_clear_cmd(struct ufs_hba *hba, int tag) |
| 2378 | { |
| 2379 | int err = 0; |
| 2380 | unsigned long flags; |
| 2381 | u32 mask = 1 << tag; |
| 2382 | |
| 2383 | /* clear outstanding transaction before retry */ |
| 2384 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2385 | ufshcd_utrl_clear(hba, tag); |
| 2386 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2387 | |
| 2388 | /* |
| 2389 | * wait for for h/w to clear corresponding bit in door-bell. |
| 2390 | * max. wait is 1 sec. |
| 2391 | */ |
| 2392 | err = ufshcd_wait_for_register(hba, |
| 2393 | REG_UTP_TRANSFER_REQ_DOOR_BELL, |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 2394 | mask, ~mask, 1000, 1000, true); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2395 | |
| 2396 | return err; |
| 2397 | } |
| 2398 | |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2399 | static int |
| 2400 | ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 2401 | { |
| 2402 | struct ufs_query_res *query_res = &hba->dev_cmd.query.response; |
| 2403 | |
| 2404 | /* Get the UPIU response */ |
| 2405 | query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >> |
| 2406 | UPIU_RSP_CODE_OFFSET; |
| 2407 | return query_res->response; |
| 2408 | } |
| 2409 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2410 | /** |
| 2411 | * ufshcd_dev_cmd_completion() - handles device management command responses |
| 2412 | * @hba: per adapter instance |
| 2413 | * @lrbp: pointer to local reference block |
| 2414 | */ |
| 2415 | static int |
| 2416 | ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 2417 | { |
| 2418 | int resp; |
| 2419 | int err = 0; |
| 2420 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 2421 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2422 | resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr); |
| 2423 | |
| 2424 | switch (resp) { |
| 2425 | case UPIU_TRANSACTION_NOP_IN: |
| 2426 | if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) { |
| 2427 | err = -EINVAL; |
| 2428 | dev_err(hba->dev, "%s: unexpected response %x\n", |
| 2429 | __func__, resp); |
| 2430 | } |
| 2431 | break; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2432 | case UPIU_TRANSACTION_QUERY_RSP: |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2433 | err = ufshcd_check_query_response(hba, lrbp); |
| 2434 | if (!err) |
| 2435 | err = ufshcd_copy_query_response(hba, lrbp); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2436 | break; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2437 | case UPIU_TRANSACTION_REJECT_UPIU: |
| 2438 | /* TODO: handle Reject UPIU Response */ |
| 2439 | err = -EPERM; |
| 2440 | dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n", |
| 2441 | __func__); |
| 2442 | break; |
| 2443 | default: |
| 2444 | err = -EINVAL; |
| 2445 | dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n", |
| 2446 | __func__, resp); |
| 2447 | break; |
| 2448 | } |
| 2449 | |
| 2450 | return err; |
| 2451 | } |
| 2452 | |
| 2453 | static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba, |
| 2454 | struct ufshcd_lrb *lrbp, int max_timeout) |
| 2455 | { |
| 2456 | int err = 0; |
| 2457 | unsigned long time_left; |
| 2458 | unsigned long flags; |
| 2459 | |
| 2460 | time_left = wait_for_completion_timeout(hba->dev_cmd.complete, |
| 2461 | msecs_to_jiffies(max_timeout)); |
| 2462 | |
Gilad Broner | ad1a1b9 | 2016-10-17 17:09:36 -0700 | [diff] [blame] | 2463 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 2464 | wmb(); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2465 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2466 | hba->dev_cmd.complete = NULL; |
| 2467 | if (likely(time_left)) { |
| 2468 | err = ufshcd_get_tr_ocs(lrbp); |
| 2469 | if (!err) |
| 2470 | err = ufshcd_dev_cmd_completion(hba, lrbp); |
| 2471 | } |
| 2472 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2473 | |
| 2474 | if (!time_left) { |
| 2475 | err = -ETIMEDOUT; |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 2476 | dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n", |
| 2477 | __func__, lrbp->task_tag); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2478 | if (!ufshcd_clear_cmd(hba, lrbp->task_tag)) |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 2479 | /* successfully cleared the command, retry if needed */ |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2480 | err = -EAGAIN; |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 2481 | /* |
| 2482 | * in case of an error, after clearing the doorbell, |
| 2483 | * we also need to clear the outstanding_request |
| 2484 | * field in hba |
| 2485 | */ |
| 2486 | ufshcd_outstanding_req_clear(hba, lrbp->task_tag); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2487 | } |
| 2488 | |
| 2489 | return err; |
| 2490 | } |
| 2491 | |
| 2492 | /** |
| 2493 | * ufshcd_get_dev_cmd_tag - Get device management command tag |
| 2494 | * @hba: per-adapter instance |
| 2495 | * @tag: pointer to variable with available slot value |
| 2496 | * |
| 2497 | * Get a free slot and lock it until device management command |
| 2498 | * completes. |
| 2499 | * |
| 2500 | * Returns false if free slot is unavailable for locking, else |
| 2501 | * return true with tag value in @tag. |
| 2502 | */ |
| 2503 | static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out) |
| 2504 | { |
| 2505 | int tag; |
| 2506 | bool ret = false; |
| 2507 | unsigned long tmp; |
| 2508 | |
| 2509 | if (!tag_out) |
| 2510 | goto out; |
| 2511 | |
| 2512 | do { |
| 2513 | tmp = ~hba->lrb_in_use; |
| 2514 | tag = find_last_bit(&tmp, hba->nutrs); |
| 2515 | if (tag >= hba->nutrs) |
| 2516 | goto out; |
| 2517 | } while (test_and_set_bit_lock(tag, &hba->lrb_in_use)); |
| 2518 | |
| 2519 | *tag_out = tag; |
| 2520 | ret = true; |
| 2521 | out: |
| 2522 | return ret; |
| 2523 | } |
| 2524 | |
| 2525 | static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag) |
| 2526 | { |
| 2527 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 2528 | } |
| 2529 | |
| 2530 | /** |
| 2531 | * ufshcd_exec_dev_cmd - API for sending device management requests |
| 2532 | * @hba - UFS hba |
| 2533 | * @cmd_type - specifies the type (NOP, Query...) |
| 2534 | * @timeout - time in seconds |
| 2535 | * |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2536 | * NOTE: Since there is only one available tag for device management commands, |
| 2537 | * it is expected you hold the hba->dev_cmd.lock mutex. |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2538 | */ |
| 2539 | static int ufshcd_exec_dev_cmd(struct ufs_hba *hba, |
| 2540 | enum dev_cmd_type cmd_type, int timeout) |
| 2541 | { |
| 2542 | struct ufshcd_lrb *lrbp; |
| 2543 | int err; |
| 2544 | int tag; |
| 2545 | struct completion wait; |
| 2546 | unsigned long flags; |
| 2547 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 2548 | down_read(&hba->clk_scaling_lock); |
| 2549 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2550 | /* |
| 2551 | * Get free slot, sleep if slots are unavailable. |
| 2552 | * Even though we use wait_event() which sleeps indefinitely, |
| 2553 | * the maximum wait time is bounded by SCSI request timeout. |
| 2554 | */ |
| 2555 | wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag)); |
| 2556 | |
| 2557 | init_completion(&wait); |
| 2558 | lrbp = &hba->lrb[tag]; |
| 2559 | WARN_ON(lrbp->cmd); |
| 2560 | err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag); |
| 2561 | if (unlikely(err)) |
| 2562 | goto out_put_tag; |
| 2563 | |
| 2564 | hba->dev_cmd.complete = &wait; |
| 2565 | |
Yaniv Gardi | e3dfdc5 | 2016-02-01 15:02:49 +0200 | [diff] [blame] | 2566 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 2567 | wmb(); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2568 | spin_lock_irqsave(hba->host->host_lock, flags); |
Kiwoong Kim | 0e675ef | 2016-11-10 21:14:36 +0900 | [diff] [blame] | 2569 | ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2570 | ufshcd_send_command(hba, tag); |
| 2571 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2572 | |
| 2573 | err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout); |
| 2574 | |
| 2575 | out_put_tag: |
| 2576 | ufshcd_put_dev_cmd_tag(hba, tag); |
| 2577 | wake_up(&hba->dev_cmd.tag_wq); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 2578 | up_read(&hba->clk_scaling_lock); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2579 | return err; |
| 2580 | } |
| 2581 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2582 | /** |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2583 | * ufshcd_init_query() - init the query response and request parameters |
| 2584 | * @hba: per-adapter instance |
| 2585 | * @request: address of the request pointer to be initialized |
| 2586 | * @response: address of the response pointer to be initialized |
| 2587 | * @opcode: operation to perform |
| 2588 | * @idn: flag idn to access |
| 2589 | * @index: LU number to access |
| 2590 | * @selector: query/flag/descriptor further identification |
| 2591 | */ |
| 2592 | static inline void ufshcd_init_query(struct ufs_hba *hba, |
| 2593 | struct ufs_query_req **request, struct ufs_query_res **response, |
| 2594 | enum query_opcode opcode, u8 idn, u8 index, u8 selector) |
| 2595 | { |
| 2596 | *request = &hba->dev_cmd.query.request; |
| 2597 | *response = &hba->dev_cmd.query.response; |
| 2598 | memset(*request, 0, sizeof(struct ufs_query_req)); |
| 2599 | memset(*response, 0, sizeof(struct ufs_query_res)); |
| 2600 | (*request)->upiu_req.opcode = opcode; |
| 2601 | (*request)->upiu_req.idn = idn; |
| 2602 | (*request)->upiu_req.index = index; |
| 2603 | (*request)->upiu_req.selector = selector; |
| 2604 | } |
| 2605 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 2606 | static int ufshcd_query_flag_retry(struct ufs_hba *hba, |
| 2607 | enum query_opcode opcode, enum flag_idn idn, bool *flag_res) |
| 2608 | { |
| 2609 | int ret; |
| 2610 | int retries; |
| 2611 | |
| 2612 | for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) { |
| 2613 | ret = ufshcd_query_flag(hba, opcode, idn, flag_res); |
| 2614 | if (ret) |
| 2615 | dev_dbg(hba->dev, |
| 2616 | "%s: failed with error %d, retries %d\n", |
| 2617 | __func__, ret, retries); |
| 2618 | else |
| 2619 | break; |
| 2620 | } |
| 2621 | |
| 2622 | if (ret) |
| 2623 | dev_err(hba->dev, |
| 2624 | "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n", |
| 2625 | __func__, opcode, idn, ret, retries); |
| 2626 | return ret; |
| 2627 | } |
| 2628 | |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2629 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2630 | * ufshcd_query_flag() - API function for sending flag query requests |
| 2631 | * hba: per-adapter instance |
| 2632 | * query_opcode: flag query to perform |
| 2633 | * idn: flag idn to access |
| 2634 | * flag_res: the flag value after the query request completes |
| 2635 | * |
| 2636 | * Returns 0 for success, non-zero in case of failure |
| 2637 | */ |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 2638 | int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode, |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2639 | enum flag_idn idn, bool *flag_res) |
| 2640 | { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2641 | struct ufs_query_req *request = NULL; |
| 2642 | struct ufs_query_res *response = NULL; |
| 2643 | int err, index = 0, selector = 0; |
Yaniv Gardi | e5ad406 | 2016-02-01 15:02:41 +0200 | [diff] [blame] | 2644 | int timeout = QUERY_REQ_TIMEOUT; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2645 | |
| 2646 | BUG_ON(!hba); |
| 2647 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2648 | ufshcd_hold(hba, false); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2649 | mutex_lock(&hba->dev_cmd.lock); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2650 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 2651 | selector); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2652 | |
| 2653 | switch (opcode) { |
| 2654 | case UPIU_QUERY_OPCODE_SET_FLAG: |
| 2655 | case UPIU_QUERY_OPCODE_CLEAR_FLAG: |
| 2656 | case UPIU_QUERY_OPCODE_TOGGLE_FLAG: |
| 2657 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
| 2658 | break; |
| 2659 | case UPIU_QUERY_OPCODE_READ_FLAG: |
| 2660 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 2661 | if (!flag_res) { |
| 2662 | /* No dummy reads */ |
| 2663 | dev_err(hba->dev, "%s: Invalid argument for read request\n", |
| 2664 | __func__); |
| 2665 | err = -EINVAL; |
| 2666 | goto out_unlock; |
| 2667 | } |
| 2668 | break; |
| 2669 | default: |
| 2670 | dev_err(hba->dev, |
| 2671 | "%s: Expected query flag opcode but got = %d\n", |
| 2672 | __func__, opcode); |
| 2673 | err = -EINVAL; |
| 2674 | goto out_unlock; |
| 2675 | } |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2676 | |
Yaniv Gardi | e5ad406 | 2016-02-01 15:02:41 +0200 | [diff] [blame] | 2677 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2678 | |
| 2679 | if (err) { |
| 2680 | dev_err(hba->dev, |
| 2681 | "%s: Sending flag query for idn %d failed, err = %d\n", |
| 2682 | __func__, idn, err); |
| 2683 | goto out_unlock; |
| 2684 | } |
| 2685 | |
| 2686 | if (flag_res) |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 2687 | *flag_res = (be32_to_cpu(response->upiu_res.value) & |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2688 | MASK_QUERY_UPIU_FLAG_LOC) & 0x1; |
| 2689 | |
| 2690 | out_unlock: |
| 2691 | mutex_unlock(&hba->dev_cmd.lock); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2692 | ufshcd_release(hba); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2693 | return err; |
| 2694 | } |
| 2695 | |
| 2696 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2697 | * ufshcd_query_attr - API function for sending attribute requests |
| 2698 | * hba: per-adapter instance |
| 2699 | * opcode: attribute opcode |
| 2700 | * idn: attribute idn to access |
| 2701 | * index: index field |
| 2702 | * selector: selector field |
| 2703 | * attr_val: the attribute value after the query request completes |
| 2704 | * |
| 2705 | * Returns 0 for success, non-zero in case of failure |
| 2706 | */ |
Sujit Reddy Thumma | bdbe5d2 | 2014-05-26 10:59:11 +0530 | [diff] [blame] | 2707 | 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] | 2708 | enum attr_idn idn, u8 index, u8 selector, u32 *attr_val) |
| 2709 | { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2710 | struct ufs_query_req *request = NULL; |
| 2711 | struct ufs_query_res *response = NULL; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2712 | int err; |
| 2713 | |
| 2714 | BUG_ON(!hba); |
| 2715 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2716 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2717 | if (!attr_val) { |
| 2718 | dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n", |
| 2719 | __func__, opcode); |
| 2720 | err = -EINVAL; |
| 2721 | goto out; |
| 2722 | } |
| 2723 | |
| 2724 | mutex_lock(&hba->dev_cmd.lock); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2725 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 2726 | selector); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2727 | |
| 2728 | switch (opcode) { |
| 2729 | case UPIU_QUERY_OPCODE_WRITE_ATTR: |
| 2730 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 2731 | request->upiu_req.value = cpu_to_be32(*attr_val); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2732 | break; |
| 2733 | case UPIU_QUERY_OPCODE_READ_ATTR: |
| 2734 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 2735 | break; |
| 2736 | default: |
| 2737 | dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n", |
| 2738 | __func__, opcode); |
| 2739 | err = -EINVAL; |
| 2740 | goto out_unlock; |
| 2741 | } |
| 2742 | |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2743 | 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] | 2744 | |
| 2745 | if (err) { |
Yaniv Gardi | 4b761b5 | 2016-11-23 16:31:18 -0800 | [diff] [blame] | 2746 | dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n", |
| 2747 | __func__, opcode, idn, index, err); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2748 | goto out_unlock; |
| 2749 | } |
| 2750 | |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 2751 | *attr_val = be32_to_cpu(response->upiu_res.value); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2752 | |
| 2753 | out_unlock: |
| 2754 | mutex_unlock(&hba->dev_cmd.lock); |
| 2755 | out: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2756 | ufshcd_release(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2757 | return err; |
| 2758 | } |
| 2759 | |
| 2760 | /** |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 2761 | * ufshcd_query_attr_retry() - API function for sending query |
| 2762 | * attribute with retries |
| 2763 | * @hba: per-adapter instance |
| 2764 | * @opcode: attribute opcode |
| 2765 | * @idn: attribute idn to access |
| 2766 | * @index: index field |
| 2767 | * @selector: selector field |
| 2768 | * @attr_val: the attribute value after the query request |
| 2769 | * completes |
| 2770 | * |
| 2771 | * Returns 0 for success, non-zero in case of failure |
| 2772 | */ |
| 2773 | static int ufshcd_query_attr_retry(struct ufs_hba *hba, |
| 2774 | enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector, |
| 2775 | u32 *attr_val) |
| 2776 | { |
| 2777 | int ret = 0; |
| 2778 | u32 retries; |
| 2779 | |
| 2780 | for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { |
| 2781 | ret = ufshcd_query_attr(hba, opcode, idn, index, |
| 2782 | selector, attr_val); |
| 2783 | if (ret) |
| 2784 | dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n", |
| 2785 | __func__, ret, retries); |
| 2786 | else |
| 2787 | break; |
| 2788 | } |
| 2789 | |
| 2790 | if (ret) |
| 2791 | dev_err(hba->dev, |
| 2792 | "%s: query attribute, idn %d, failed with error %d after %d retires\n", |
| 2793 | __func__, idn, ret, QUERY_REQ_RETRIES); |
| 2794 | return ret; |
| 2795 | } |
| 2796 | |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 2797 | static int __ufshcd_query_descriptor(struct ufs_hba *hba, |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2798 | enum query_opcode opcode, enum desc_idn idn, u8 index, |
| 2799 | u8 selector, u8 *desc_buf, int *buf_len) |
| 2800 | { |
| 2801 | struct ufs_query_req *request = NULL; |
| 2802 | struct ufs_query_res *response = NULL; |
| 2803 | int err; |
| 2804 | |
| 2805 | BUG_ON(!hba); |
| 2806 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2807 | ufshcd_hold(hba, false); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2808 | if (!desc_buf) { |
| 2809 | dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n", |
| 2810 | __func__, opcode); |
| 2811 | err = -EINVAL; |
| 2812 | goto out; |
| 2813 | } |
| 2814 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 2815 | if (*buf_len < QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2816 | dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n", |
| 2817 | __func__, *buf_len); |
| 2818 | err = -EINVAL; |
| 2819 | goto out; |
| 2820 | } |
| 2821 | |
| 2822 | mutex_lock(&hba->dev_cmd.lock); |
| 2823 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 2824 | selector); |
| 2825 | hba->dev_cmd.query.descriptor = desc_buf; |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 2826 | request->upiu_req.length = cpu_to_be16(*buf_len); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2827 | |
| 2828 | switch (opcode) { |
| 2829 | case UPIU_QUERY_OPCODE_WRITE_DESC: |
| 2830 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
| 2831 | break; |
| 2832 | case UPIU_QUERY_OPCODE_READ_DESC: |
| 2833 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 2834 | break; |
| 2835 | default: |
| 2836 | dev_err(hba->dev, |
| 2837 | "%s: Expected query descriptor opcode but got = 0x%.2x\n", |
| 2838 | __func__, opcode); |
| 2839 | err = -EINVAL; |
| 2840 | goto out_unlock; |
| 2841 | } |
| 2842 | |
| 2843 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT); |
| 2844 | |
| 2845 | if (err) { |
Yaniv Gardi | 4b761b5 | 2016-11-23 16:31:18 -0800 | [diff] [blame] | 2846 | dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n", |
| 2847 | __func__, opcode, idn, index, err); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2848 | goto out_unlock; |
| 2849 | } |
| 2850 | |
| 2851 | hba->dev_cmd.query.descriptor = NULL; |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 2852 | *buf_len = be16_to_cpu(response->upiu_res.length); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2853 | |
| 2854 | out_unlock: |
| 2855 | mutex_unlock(&hba->dev_cmd.lock); |
| 2856 | out: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2857 | ufshcd_release(hba); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2858 | return err; |
| 2859 | } |
| 2860 | |
| 2861 | /** |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 2862 | * ufshcd_query_descriptor_retry - API function for sending descriptor |
| 2863 | * requests |
| 2864 | * hba: per-adapter instance |
| 2865 | * opcode: attribute opcode |
| 2866 | * idn: attribute idn to access |
| 2867 | * index: index field |
| 2868 | * selector: selector field |
| 2869 | * desc_buf: the buffer that contains the descriptor |
| 2870 | * buf_len: length parameter passed to the device |
| 2871 | * |
| 2872 | * Returns 0 for success, non-zero in case of failure. |
| 2873 | * The buf_len parameter will contain, on return, the length parameter |
| 2874 | * received on the response. |
| 2875 | */ |
Stanislav Nijnikov | 2238d31 | 2018-02-15 14:14:07 +0200 | [diff] [blame^] | 2876 | int ufshcd_query_descriptor_retry(struct ufs_hba *hba, |
| 2877 | enum query_opcode opcode, |
| 2878 | enum desc_idn idn, u8 index, |
| 2879 | u8 selector, |
| 2880 | u8 *desc_buf, int *buf_len) |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 2881 | { |
| 2882 | int err; |
| 2883 | int retries; |
| 2884 | |
| 2885 | for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { |
| 2886 | err = __ufshcd_query_descriptor(hba, opcode, idn, index, |
| 2887 | selector, desc_buf, buf_len); |
| 2888 | if (!err || err == -EINVAL) |
| 2889 | break; |
| 2890 | } |
| 2891 | |
| 2892 | return err; |
| 2893 | } |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 2894 | |
| 2895 | /** |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 2896 | * ufshcd_read_desc_length - read the specified descriptor length from header |
| 2897 | * @hba: Pointer to adapter instance |
| 2898 | * @desc_id: descriptor idn value |
| 2899 | * @desc_index: descriptor index |
| 2900 | * @desc_length: pointer to variable to read the length of descriptor |
| 2901 | * |
| 2902 | * Return 0 in case of success, non-zero otherwise |
| 2903 | */ |
| 2904 | static int ufshcd_read_desc_length(struct ufs_hba *hba, |
| 2905 | enum desc_idn desc_id, |
| 2906 | int desc_index, |
| 2907 | int *desc_length) |
| 2908 | { |
| 2909 | int ret; |
| 2910 | u8 header[QUERY_DESC_HDR_SIZE]; |
| 2911 | int header_len = QUERY_DESC_HDR_SIZE; |
| 2912 | |
| 2913 | if (desc_id >= QUERY_DESC_IDN_MAX) |
| 2914 | return -EINVAL; |
| 2915 | |
| 2916 | ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC, |
| 2917 | desc_id, desc_index, 0, header, |
| 2918 | &header_len); |
| 2919 | |
| 2920 | if (ret) { |
| 2921 | dev_err(hba->dev, "%s: Failed to get descriptor header id %d", |
| 2922 | __func__, desc_id); |
| 2923 | return ret; |
| 2924 | } else if (desc_id != header[QUERY_DESC_DESC_TYPE_OFFSET]) { |
| 2925 | dev_warn(hba->dev, "%s: descriptor header id %d and desc_id %d mismatch", |
| 2926 | __func__, header[QUERY_DESC_DESC_TYPE_OFFSET], |
| 2927 | desc_id); |
| 2928 | ret = -EINVAL; |
| 2929 | } |
| 2930 | |
| 2931 | *desc_length = header[QUERY_DESC_LENGTH_OFFSET]; |
| 2932 | return ret; |
| 2933 | |
| 2934 | } |
| 2935 | |
| 2936 | /** |
| 2937 | * ufshcd_map_desc_id_to_length - map descriptor IDN to its length |
| 2938 | * @hba: Pointer to adapter instance |
| 2939 | * @desc_id: descriptor idn value |
| 2940 | * @desc_len: mapped desc length (out) |
| 2941 | * |
| 2942 | * Return 0 in case of success, non-zero otherwise |
| 2943 | */ |
| 2944 | int ufshcd_map_desc_id_to_length(struct ufs_hba *hba, |
| 2945 | enum desc_idn desc_id, int *desc_len) |
| 2946 | { |
| 2947 | switch (desc_id) { |
| 2948 | case QUERY_DESC_IDN_DEVICE: |
| 2949 | *desc_len = hba->desc_size.dev_desc; |
| 2950 | break; |
| 2951 | case QUERY_DESC_IDN_POWER: |
| 2952 | *desc_len = hba->desc_size.pwr_desc; |
| 2953 | break; |
| 2954 | case QUERY_DESC_IDN_GEOMETRY: |
| 2955 | *desc_len = hba->desc_size.geom_desc; |
| 2956 | break; |
| 2957 | case QUERY_DESC_IDN_CONFIGURATION: |
| 2958 | *desc_len = hba->desc_size.conf_desc; |
| 2959 | break; |
| 2960 | case QUERY_DESC_IDN_UNIT: |
| 2961 | *desc_len = hba->desc_size.unit_desc; |
| 2962 | break; |
| 2963 | case QUERY_DESC_IDN_INTERCONNECT: |
| 2964 | *desc_len = hba->desc_size.interc_desc; |
| 2965 | break; |
| 2966 | case QUERY_DESC_IDN_STRING: |
| 2967 | *desc_len = QUERY_DESC_MAX_SIZE; |
| 2968 | break; |
Stanislav Nijnikov | c648c2d | 2018-02-15 14:14:05 +0200 | [diff] [blame] | 2969 | case QUERY_DESC_IDN_HEALTH: |
| 2970 | *desc_len = hba->desc_size.hlth_desc; |
| 2971 | break; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 2972 | case QUERY_DESC_IDN_RFU_0: |
| 2973 | case QUERY_DESC_IDN_RFU_1: |
| 2974 | *desc_len = 0; |
| 2975 | break; |
| 2976 | default: |
| 2977 | *desc_len = 0; |
| 2978 | return -EINVAL; |
| 2979 | } |
| 2980 | return 0; |
| 2981 | } |
| 2982 | EXPORT_SYMBOL(ufshcd_map_desc_id_to_length); |
| 2983 | |
| 2984 | /** |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 2985 | * ufshcd_read_desc_param - read the specified descriptor parameter |
| 2986 | * @hba: Pointer to adapter instance |
| 2987 | * @desc_id: descriptor idn value |
| 2988 | * @desc_index: descriptor index |
| 2989 | * @param_offset: offset of the parameter to read |
| 2990 | * @param_read_buf: pointer to buffer where parameter would be read |
| 2991 | * @param_size: sizeof(param_read_buf) |
| 2992 | * |
| 2993 | * Return 0 in case of success, non-zero otherwise |
| 2994 | */ |
Stanislav Nijnikov | 45bced8 | 2018-02-15 14:14:02 +0200 | [diff] [blame] | 2995 | int ufshcd_read_desc_param(struct ufs_hba *hba, |
| 2996 | enum desc_idn desc_id, |
| 2997 | int desc_index, |
| 2998 | u8 param_offset, |
| 2999 | u8 *param_read_buf, |
| 3000 | u8 param_size) |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3001 | { |
| 3002 | int ret; |
| 3003 | u8 *desc_buf; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3004 | int buff_len; |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3005 | bool is_kmalloc = true; |
| 3006 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3007 | /* Safety check */ |
| 3008 | if (desc_id >= QUERY_DESC_IDN_MAX || !param_size) |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3009 | return -EINVAL; |
| 3010 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3011 | /* Get the max length of descriptor from structure filled up at probe |
| 3012 | * time. |
| 3013 | */ |
| 3014 | ret = ufshcd_map_desc_id_to_length(hba, desc_id, &buff_len); |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3015 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3016 | /* Sanity checks */ |
| 3017 | if (ret || !buff_len) { |
| 3018 | dev_err(hba->dev, "%s: Failed to get full descriptor length", |
| 3019 | __func__); |
| 3020 | return ret; |
| 3021 | } |
| 3022 | |
| 3023 | /* Check whether we need temp memory */ |
| 3024 | if (param_offset != 0 || param_size < buff_len) { |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3025 | desc_buf = kmalloc(buff_len, GFP_KERNEL); |
| 3026 | if (!desc_buf) |
| 3027 | return -ENOMEM; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3028 | } else { |
| 3029 | desc_buf = param_read_buf; |
| 3030 | is_kmalloc = false; |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3031 | } |
| 3032 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3033 | /* Request for full descriptor */ |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3034 | ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC, |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3035 | desc_id, desc_index, 0, |
| 3036 | desc_buf, &buff_len); |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3037 | |
subhashj@codeaurora.org | bde44bb | 2016-11-23 16:31:41 -0800 | [diff] [blame] | 3038 | if (ret) { |
| 3039 | dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d", |
| 3040 | __func__, desc_id, desc_index, param_offset, ret); |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3041 | goto out; |
| 3042 | } |
| 3043 | |
subhashj@codeaurora.org | bde44bb | 2016-11-23 16:31:41 -0800 | [diff] [blame] | 3044 | /* Sanity check */ |
| 3045 | if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) { |
| 3046 | dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header", |
| 3047 | __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]); |
| 3048 | ret = -EINVAL; |
| 3049 | goto out; |
| 3050 | } |
| 3051 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 3052 | /* Check wherher we will not copy more data, than available */ |
| 3053 | if (is_kmalloc && param_size > buff_len) |
| 3054 | param_size = buff_len; |
subhashj@codeaurora.org | bde44bb | 2016-11-23 16:31:41 -0800 | [diff] [blame] | 3055 | |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3056 | if (is_kmalloc) |
| 3057 | memcpy(param_read_buf, &desc_buf[param_offset], param_size); |
| 3058 | out: |
| 3059 | if (is_kmalloc) |
| 3060 | kfree(desc_buf); |
| 3061 | return ret; |
| 3062 | } |
| 3063 | |
| 3064 | static inline int ufshcd_read_desc(struct ufs_hba *hba, |
| 3065 | enum desc_idn desc_id, |
| 3066 | int desc_index, |
| 3067 | u8 *buf, |
| 3068 | u32 size) |
| 3069 | { |
| 3070 | return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size); |
| 3071 | } |
| 3072 | |
| 3073 | static inline int ufshcd_read_power_desc(struct ufs_hba *hba, |
| 3074 | u8 *buf, |
| 3075 | u32 size) |
| 3076 | { |
Szymon Mielczarek | dbd34a6 | 2017-03-29 08:19:21 +0200 | [diff] [blame] | 3077 | return ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size); |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3078 | } |
| 3079 | |
Tomas Winkler | 8209b6d | 2017-01-05 10:45:10 +0200 | [diff] [blame] | 3080 | static int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size) |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3081 | { |
| 3082 | return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size); |
| 3083 | } |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3084 | |
| 3085 | /** |
| 3086 | * ufshcd_read_string_desc - read string descriptor |
| 3087 | * @hba: pointer to adapter instance |
| 3088 | * @desc_index: descriptor index |
| 3089 | * @buf: pointer to buffer where descriptor would be read |
| 3090 | * @size: size of buf |
| 3091 | * @ascii: if true convert from unicode to ascii characters |
| 3092 | * |
| 3093 | * Return 0 in case of success, non-zero otherwise |
| 3094 | */ |
Tomas Winkler | 8209b6d | 2017-01-05 10:45:10 +0200 | [diff] [blame] | 3095 | #define ASCII_STD true |
Stanislav Nijnikov | 2238d31 | 2018-02-15 14:14:07 +0200 | [diff] [blame^] | 3096 | int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index, |
| 3097 | u8 *buf, u32 size, bool ascii) |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3098 | { |
| 3099 | int err = 0; |
| 3100 | |
| 3101 | err = ufshcd_read_desc(hba, |
| 3102 | QUERY_DESC_IDN_STRING, desc_index, buf, size); |
| 3103 | |
| 3104 | if (err) { |
| 3105 | dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n", |
| 3106 | __func__, QUERY_REQ_RETRIES, err); |
| 3107 | goto out; |
| 3108 | } |
| 3109 | |
| 3110 | if (ascii) { |
| 3111 | int desc_len; |
| 3112 | int ascii_len; |
| 3113 | int i; |
| 3114 | char *buff_ascii; |
| 3115 | |
| 3116 | desc_len = buf[0]; |
| 3117 | /* remove header and divide by 2 to move from UTF16 to UTF8 */ |
| 3118 | ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1; |
| 3119 | if (size < ascii_len + QUERY_DESC_HDR_SIZE) { |
| 3120 | dev_err(hba->dev, "%s: buffer allocated size is too small\n", |
| 3121 | __func__); |
| 3122 | err = -ENOMEM; |
| 3123 | goto out; |
| 3124 | } |
| 3125 | |
| 3126 | buff_ascii = kmalloc(ascii_len, GFP_KERNEL); |
| 3127 | if (!buff_ascii) { |
| 3128 | err = -ENOMEM; |
Tiezhu Yang | fcbefc3 | 2016-06-25 12:35:22 +0800 | [diff] [blame] | 3129 | goto out; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3130 | } |
| 3131 | |
| 3132 | /* |
| 3133 | * the descriptor contains string in UTF16 format |
| 3134 | * we need to convert to utf-8 so it can be displayed |
| 3135 | */ |
| 3136 | utf16s_to_utf8s((wchar_t *)&buf[QUERY_DESC_HDR_SIZE], |
| 3137 | desc_len - QUERY_DESC_HDR_SIZE, |
| 3138 | UTF16_BIG_ENDIAN, buff_ascii, ascii_len); |
| 3139 | |
| 3140 | /* replace non-printable or non-ASCII characters with spaces */ |
| 3141 | for (i = 0; i < ascii_len; i++) |
| 3142 | ufshcd_remove_non_printable(&buff_ascii[i]); |
| 3143 | |
| 3144 | memset(buf + QUERY_DESC_HDR_SIZE, 0, |
| 3145 | size - QUERY_DESC_HDR_SIZE); |
| 3146 | memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len); |
| 3147 | buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3148 | kfree(buff_ascii); |
| 3149 | } |
| 3150 | out: |
| 3151 | return err; |
| 3152 | } |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3153 | |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3154 | /** |
| 3155 | * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter |
| 3156 | * @hba: Pointer to adapter instance |
| 3157 | * @lun: lun id |
| 3158 | * @param_offset: offset of the parameter to read |
| 3159 | * @param_read_buf: pointer to buffer where parameter would be read |
| 3160 | * @param_size: sizeof(param_read_buf) |
| 3161 | * |
| 3162 | * Return 0 in case of success, non-zero otherwise |
| 3163 | */ |
| 3164 | static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba, |
| 3165 | int lun, |
| 3166 | enum unit_desc_param param_offset, |
| 3167 | u8 *param_read_buf, |
| 3168 | u32 param_size) |
| 3169 | { |
| 3170 | /* |
| 3171 | * Unit descriptors are only available for general purpose LUs (LUN id |
| 3172 | * from 0 to 7) and RPMB Well known LU. |
| 3173 | */ |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 3174 | if (lun != UFS_UPIU_RPMB_WLUN && (lun >= UFS_UPIU_MAX_GENERAL_LUN)) |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3175 | return -EOPNOTSUPP; |
| 3176 | |
| 3177 | return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun, |
| 3178 | param_offset, param_read_buf, param_size); |
| 3179 | } |
| 3180 | |
| 3181 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3182 | * ufshcd_memory_alloc - allocate memory for host memory space data structures |
| 3183 | * @hba: per adapter instance |
| 3184 | * |
| 3185 | * 1. Allocate DMA memory for Command Descriptor array |
| 3186 | * Each command descriptor consist of Command UPIU, Response UPIU and PRDT |
| 3187 | * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL). |
| 3188 | * 3. Allocate DMA memory for UTP Task Management Request Descriptor List |
| 3189 | * (UTMRDL) |
| 3190 | * 4. Allocate memory for local reference block(lrb). |
| 3191 | * |
| 3192 | * Returns 0 for success, non-zero in case of failure |
| 3193 | */ |
| 3194 | static int ufshcd_memory_alloc(struct ufs_hba *hba) |
| 3195 | { |
| 3196 | size_t utmrdl_size, utrdl_size, ucdl_size; |
| 3197 | |
| 3198 | /* Allocate memory for UTP command descriptors */ |
| 3199 | ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3200 | hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 3201 | ucdl_size, |
| 3202 | &hba->ucdl_dma_addr, |
| 3203 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3204 | |
| 3205 | /* |
| 3206 | * UFSHCI requires UTP command descriptor to be 128 byte aligned. |
| 3207 | * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE |
| 3208 | * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will |
| 3209 | * be aligned to 128 bytes as well |
| 3210 | */ |
| 3211 | if (!hba->ucdl_base_addr || |
| 3212 | WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3213 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3214 | "Command Descriptor Memory allocation failed\n"); |
| 3215 | goto out; |
| 3216 | } |
| 3217 | |
| 3218 | /* |
| 3219 | * Allocate memory for UTP Transfer descriptors |
| 3220 | * UFSHCI requires 1024 byte alignment of UTRD |
| 3221 | */ |
| 3222 | utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3223 | hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 3224 | utrdl_size, |
| 3225 | &hba->utrdl_dma_addr, |
| 3226 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3227 | if (!hba->utrdl_base_addr || |
| 3228 | WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3229 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3230 | "Transfer Descriptor Memory allocation failed\n"); |
| 3231 | goto out; |
| 3232 | } |
| 3233 | |
| 3234 | /* |
| 3235 | * Allocate memory for UTP Task Management descriptors |
| 3236 | * UFSHCI requires 1024 byte alignment of UTMRD |
| 3237 | */ |
| 3238 | utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs; |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3239 | hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 3240 | utmrdl_size, |
| 3241 | &hba->utmrdl_dma_addr, |
| 3242 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3243 | if (!hba->utmrdl_base_addr || |
| 3244 | WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3245 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3246 | "Task Management Descriptor Memory allocation failed\n"); |
| 3247 | goto out; |
| 3248 | } |
| 3249 | |
| 3250 | /* Allocate memory for local reference block */ |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3251 | hba->lrb = devm_kzalloc(hba->dev, |
| 3252 | hba->nutrs * sizeof(struct ufshcd_lrb), |
| 3253 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3254 | if (!hba->lrb) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3255 | dev_err(hba->dev, "LRB Memory allocation failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3256 | goto out; |
| 3257 | } |
| 3258 | return 0; |
| 3259 | out: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3260 | return -ENOMEM; |
| 3261 | } |
| 3262 | |
| 3263 | /** |
| 3264 | * ufshcd_host_memory_configure - configure local reference block with |
| 3265 | * memory offsets |
| 3266 | * @hba: per adapter instance |
| 3267 | * |
| 3268 | * Configure Host memory space |
| 3269 | * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA |
| 3270 | * address. |
| 3271 | * 2. Update each UTRD with Response UPIU offset, Response UPIU length |
| 3272 | * and PRDT offset. |
| 3273 | * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT |
| 3274 | * into local reference block. |
| 3275 | */ |
| 3276 | static void ufshcd_host_memory_configure(struct ufs_hba *hba) |
| 3277 | { |
| 3278 | struct utp_transfer_cmd_desc *cmd_descp; |
| 3279 | struct utp_transfer_req_desc *utrdlp; |
| 3280 | dma_addr_t cmd_desc_dma_addr; |
| 3281 | dma_addr_t cmd_desc_element_addr; |
| 3282 | u16 response_offset; |
| 3283 | u16 prdt_offset; |
| 3284 | int cmd_desc_size; |
| 3285 | int i; |
| 3286 | |
| 3287 | utrdlp = hba->utrdl_base_addr; |
| 3288 | cmd_descp = hba->ucdl_base_addr; |
| 3289 | |
| 3290 | response_offset = |
| 3291 | offsetof(struct utp_transfer_cmd_desc, response_upiu); |
| 3292 | prdt_offset = |
| 3293 | offsetof(struct utp_transfer_cmd_desc, prd_table); |
| 3294 | |
| 3295 | cmd_desc_size = sizeof(struct utp_transfer_cmd_desc); |
| 3296 | cmd_desc_dma_addr = hba->ucdl_dma_addr; |
| 3297 | |
| 3298 | for (i = 0; i < hba->nutrs; i++) { |
| 3299 | /* Configure UTRD with command descriptor base address */ |
| 3300 | cmd_desc_element_addr = |
| 3301 | (cmd_desc_dma_addr + (cmd_desc_size * i)); |
| 3302 | utrdlp[i].command_desc_base_addr_lo = |
| 3303 | cpu_to_le32(lower_32_bits(cmd_desc_element_addr)); |
| 3304 | utrdlp[i].command_desc_base_addr_hi = |
| 3305 | cpu_to_le32(upper_32_bits(cmd_desc_element_addr)); |
| 3306 | |
| 3307 | /* Response upiu and prdt offset should be in double words */ |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 3308 | if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) { |
| 3309 | utrdlp[i].response_upiu_offset = |
| 3310 | cpu_to_le16(response_offset); |
| 3311 | utrdlp[i].prd_table_offset = |
| 3312 | cpu_to_le16(prdt_offset); |
| 3313 | utrdlp[i].response_upiu_length = |
| 3314 | cpu_to_le16(ALIGNED_UPIU_SIZE); |
| 3315 | } else { |
| 3316 | utrdlp[i].response_upiu_offset = |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3317 | cpu_to_le16((response_offset >> 2)); |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 3318 | utrdlp[i].prd_table_offset = |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3319 | cpu_to_le16((prdt_offset >> 2)); |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 3320 | utrdlp[i].response_upiu_length = |
Sujit Reddy Thumma | 3ca316c | 2013-06-26 22:39:30 +0530 | [diff] [blame] | 3321 | cpu_to_le16(ALIGNED_UPIU_SIZE >> 2); |
Kiwoong Kim | 75b1cc4 | 2016-11-22 17:06:59 +0900 | [diff] [blame] | 3322 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3323 | |
| 3324 | hba->lrb[i].utr_descriptor_ptr = (utrdlp + i); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 3325 | hba->lrb[i].utrd_dma_addr = hba->utrdl_dma_addr + |
| 3326 | (i * sizeof(struct utp_transfer_req_desc)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3327 | hba->lrb[i].ucd_req_ptr = |
| 3328 | (struct utp_upiu_req *)(cmd_descp + i); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 3329 | hba->lrb[i].ucd_req_dma_addr = cmd_desc_element_addr; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3330 | hba->lrb[i].ucd_rsp_ptr = |
| 3331 | (struct utp_upiu_rsp *)cmd_descp[i].response_upiu; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 3332 | hba->lrb[i].ucd_rsp_dma_addr = cmd_desc_element_addr + |
| 3333 | response_offset; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3334 | hba->lrb[i].ucd_prdt_ptr = |
| 3335 | (struct ufshcd_sg_entry *)cmd_descp[i].prd_table; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 3336 | hba->lrb[i].ucd_prdt_dma_addr = cmd_desc_element_addr + |
| 3337 | prdt_offset; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3338 | } |
| 3339 | } |
| 3340 | |
| 3341 | /** |
| 3342 | * ufshcd_dme_link_startup - Notify Unipro to perform link startup |
| 3343 | * @hba: per adapter instance |
| 3344 | * |
| 3345 | * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer, |
| 3346 | * in order to initialize the Unipro link startup procedure. |
| 3347 | * Once the Unipro links are up, the device connected to the controller |
| 3348 | * is detected. |
| 3349 | * |
| 3350 | * Returns 0 on success, non-zero value on failure |
| 3351 | */ |
| 3352 | static int ufshcd_dme_link_startup(struct ufs_hba *hba) |
| 3353 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3354 | struct uic_command uic_cmd = {0}; |
| 3355 | int ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3356 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3357 | uic_cmd.command = UIC_CMD_DME_LINK_STARTUP; |
| 3358 | |
| 3359 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3360 | if (ret) |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 3361 | dev_dbg(hba->dev, |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3362 | "dme-link-startup: error code %d\n", ret); |
| 3363 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3364 | } |
| 3365 | |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 3366 | static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba) |
| 3367 | { |
| 3368 | #define MIN_DELAY_BEFORE_DME_CMDS_US 1000 |
| 3369 | unsigned long min_sleep_time_us; |
| 3370 | |
| 3371 | if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS)) |
| 3372 | return; |
| 3373 | |
| 3374 | /* |
| 3375 | * last_dme_cmd_tstamp will be 0 only for 1st call to |
| 3376 | * this function |
| 3377 | */ |
| 3378 | if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) { |
| 3379 | min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US; |
| 3380 | } else { |
| 3381 | unsigned long delta = |
| 3382 | (unsigned long) ktime_to_us( |
| 3383 | ktime_sub(ktime_get(), |
| 3384 | hba->last_dme_cmd_tstamp)); |
| 3385 | |
| 3386 | if (delta < MIN_DELAY_BEFORE_DME_CMDS_US) |
| 3387 | min_sleep_time_us = |
| 3388 | MIN_DELAY_BEFORE_DME_CMDS_US - delta; |
| 3389 | else |
| 3390 | return; /* no more delay required */ |
| 3391 | } |
| 3392 | |
| 3393 | /* allow sleep for extra 50us if needed */ |
| 3394 | usleep_range(min_sleep_time_us, min_sleep_time_us + 50); |
| 3395 | } |
| 3396 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3397 | /** |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3398 | * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET |
| 3399 | * @hba: per adapter instance |
| 3400 | * @attr_sel: uic command argument1 |
| 3401 | * @attr_set: attribute set type as uic command argument2 |
| 3402 | * @mib_val: setting value as uic command argument3 |
| 3403 | * @peer: indicate whether peer or local |
| 3404 | * |
| 3405 | * Returns 0 on success, non-zero value on failure |
| 3406 | */ |
| 3407 | int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel, |
| 3408 | u8 attr_set, u32 mib_val, u8 peer) |
| 3409 | { |
| 3410 | struct uic_command uic_cmd = {0}; |
| 3411 | static const char *const action[] = { |
| 3412 | "dme-set", |
| 3413 | "dme-peer-set" |
| 3414 | }; |
| 3415 | const char *set = action[!!peer]; |
| 3416 | int ret; |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3417 | int retries = UFS_UIC_COMMAND_RETRIES; |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3418 | |
| 3419 | uic_cmd.command = peer ? |
| 3420 | UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET; |
| 3421 | uic_cmd.argument1 = attr_sel; |
| 3422 | uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set); |
| 3423 | uic_cmd.argument3 = mib_val; |
| 3424 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3425 | do { |
| 3426 | /* for peer attributes we retry upon failure */ |
| 3427 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3428 | if (ret) |
| 3429 | dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n", |
| 3430 | set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret); |
| 3431 | } while (ret && peer && --retries); |
| 3432 | |
Yaniv Gardi | f37e9f8 | 2016-11-23 16:32:49 -0800 | [diff] [blame] | 3433 | if (ret) |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3434 | 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] | 3435 | set, UIC_GET_ATTR_ID(attr_sel), mib_val, |
| 3436 | UFS_UIC_COMMAND_RETRIES - retries); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3437 | |
| 3438 | return ret; |
| 3439 | } |
| 3440 | EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr); |
| 3441 | |
| 3442 | /** |
| 3443 | * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET |
| 3444 | * @hba: per adapter instance |
| 3445 | * @attr_sel: uic command argument1 |
| 3446 | * @mib_val: the value of the attribute as returned by the UIC command |
| 3447 | * @peer: indicate whether peer or local |
| 3448 | * |
| 3449 | * Returns 0 on success, non-zero value on failure |
| 3450 | */ |
| 3451 | int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel, |
| 3452 | u32 *mib_val, u8 peer) |
| 3453 | { |
| 3454 | struct uic_command uic_cmd = {0}; |
| 3455 | static const char *const action[] = { |
| 3456 | "dme-get", |
| 3457 | "dme-peer-get" |
| 3458 | }; |
| 3459 | const char *get = action[!!peer]; |
| 3460 | int ret; |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3461 | int retries = UFS_UIC_COMMAND_RETRIES; |
Yaniv Gardi | 874237f | 2015-05-17 18:55:03 +0300 | [diff] [blame] | 3462 | struct ufs_pa_layer_attr orig_pwr_info; |
| 3463 | struct ufs_pa_layer_attr temp_pwr_info; |
| 3464 | bool pwr_mode_change = false; |
| 3465 | |
| 3466 | if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) { |
| 3467 | orig_pwr_info = hba->pwr_info; |
| 3468 | temp_pwr_info = orig_pwr_info; |
| 3469 | |
| 3470 | if (orig_pwr_info.pwr_tx == FAST_MODE || |
| 3471 | orig_pwr_info.pwr_rx == FAST_MODE) { |
| 3472 | temp_pwr_info.pwr_tx = FASTAUTO_MODE; |
| 3473 | temp_pwr_info.pwr_rx = FASTAUTO_MODE; |
| 3474 | pwr_mode_change = true; |
| 3475 | } else if (orig_pwr_info.pwr_tx == SLOW_MODE || |
| 3476 | orig_pwr_info.pwr_rx == SLOW_MODE) { |
| 3477 | temp_pwr_info.pwr_tx = SLOWAUTO_MODE; |
| 3478 | temp_pwr_info.pwr_rx = SLOWAUTO_MODE; |
| 3479 | pwr_mode_change = true; |
| 3480 | } |
| 3481 | if (pwr_mode_change) { |
| 3482 | ret = ufshcd_change_power_mode(hba, &temp_pwr_info); |
| 3483 | if (ret) |
| 3484 | goto out; |
| 3485 | } |
| 3486 | } |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3487 | |
| 3488 | uic_cmd.command = peer ? |
| 3489 | UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET; |
| 3490 | uic_cmd.argument1 = attr_sel; |
| 3491 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3492 | do { |
| 3493 | /* for peer attributes we retry upon failure */ |
| 3494 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3495 | if (ret) |
| 3496 | dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n", |
| 3497 | get, UIC_GET_ATTR_ID(attr_sel), ret); |
| 3498 | } while (ret && peer && --retries); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3499 | |
Yaniv Gardi | f37e9f8 | 2016-11-23 16:32:49 -0800 | [diff] [blame] | 3500 | if (ret) |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3501 | 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] | 3502 | get, UIC_GET_ATTR_ID(attr_sel), |
| 3503 | UFS_UIC_COMMAND_RETRIES - retries); |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3504 | |
| 3505 | if (mib_val && !ret) |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3506 | *mib_val = uic_cmd.argument3; |
Yaniv Gardi | 874237f | 2015-05-17 18:55:03 +0300 | [diff] [blame] | 3507 | |
| 3508 | if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE) |
| 3509 | && pwr_mode_change) |
| 3510 | ufshcd_change_power_mode(hba, &orig_pwr_info); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3511 | out: |
| 3512 | return ret; |
| 3513 | } |
| 3514 | EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr); |
| 3515 | |
| 3516 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3517 | * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power |
| 3518 | * state) and waits for it to take effect. |
| 3519 | * |
| 3520 | * @hba: per adapter instance |
| 3521 | * @cmd: UIC command to execute |
| 3522 | * |
| 3523 | * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER & |
| 3524 | * DME_HIBERNATE_EXIT commands take some time to take its effect on both host |
| 3525 | * and device UniPro link and hence it's final completion would be indicated by |
| 3526 | * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in |
| 3527 | * addition to normal UIC command completion Status (UCCS). This function only |
| 3528 | * returns after the relevant status bits indicate the completion. |
| 3529 | * |
| 3530 | * Returns 0 on success, non-zero value on failure |
| 3531 | */ |
| 3532 | static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd) |
| 3533 | { |
| 3534 | struct completion uic_async_done; |
| 3535 | unsigned long flags; |
| 3536 | u8 status; |
| 3537 | int ret; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3538 | bool reenable_intr = false; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3539 | |
| 3540 | mutex_lock(&hba->uic_cmd_mutex); |
| 3541 | init_completion(&uic_async_done); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 3542 | ufshcd_add_delay_before_dme_cmd(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3543 | |
| 3544 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 3545 | hba->uic_async_done = &uic_async_done; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3546 | if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) { |
| 3547 | ufshcd_disable_intr(hba, UIC_COMMAND_COMPL); |
| 3548 | /* |
| 3549 | * Make sure UIC command completion interrupt is disabled before |
| 3550 | * issuing UIC command. |
| 3551 | */ |
| 3552 | wmb(); |
| 3553 | reenable_intr = true; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3554 | } |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3555 | ret = __ufshcd_send_uic_cmd(hba, cmd, false); |
| 3556 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3557 | if (ret) { |
| 3558 | dev_err(hba->dev, |
| 3559 | "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n", |
| 3560 | cmd->command, cmd->argument3, ret); |
| 3561 | goto out; |
| 3562 | } |
| 3563 | |
| 3564 | if (!wait_for_completion_timeout(hba->uic_async_done, |
| 3565 | msecs_to_jiffies(UIC_CMD_TIMEOUT))) { |
| 3566 | dev_err(hba->dev, |
| 3567 | "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n", |
| 3568 | cmd->command, cmd->argument3); |
| 3569 | ret = -ETIMEDOUT; |
| 3570 | goto out; |
| 3571 | } |
| 3572 | |
| 3573 | status = ufshcd_get_upmcrs(hba); |
| 3574 | if (status != PWR_LOCAL) { |
| 3575 | dev_err(hba->dev, |
Zang Leigang | 479da36 | 2017-09-19 16:50:30 +0800 | [diff] [blame] | 3576 | "pwr ctrl cmd 0x%x failed, host upmcrs:0x%x\n", |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3577 | cmd->command, status); |
| 3578 | ret = (status != PWR_OK) ? status : -1; |
| 3579 | } |
| 3580 | out: |
Venkat Gopalakrishnan | 7942f7b | 2017-02-03 16:58:24 -0800 | [diff] [blame] | 3581 | if (ret) { |
| 3582 | ufshcd_print_host_state(hba); |
| 3583 | ufshcd_print_pwr_info(hba); |
| 3584 | ufshcd_print_host_regs(hba); |
| 3585 | } |
| 3586 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3587 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3588 | hba->active_uic_cmd = NULL; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3589 | hba->uic_async_done = NULL; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3590 | if (reenable_intr) |
| 3591 | ufshcd_enable_intr(hba, UIC_COMMAND_COMPL); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3592 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3593 | mutex_unlock(&hba->uic_cmd_mutex); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3594 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3595 | return ret; |
| 3596 | } |
| 3597 | |
| 3598 | /** |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3599 | * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage |
| 3600 | * using DME_SET primitives. |
| 3601 | * @hba: per adapter instance |
| 3602 | * @mode: powr mode value |
| 3603 | * |
| 3604 | * Returns 0 on success, non-zero value on failure |
| 3605 | */ |
Sujit Reddy Thumma | bdbe5d2 | 2014-05-26 10:59:11 +0530 | [diff] [blame] | 3606 | 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] | 3607 | { |
| 3608 | struct uic_command uic_cmd = {0}; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3609 | int ret; |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3610 | |
Yaniv Gardi | c3a2f9e | 2015-05-17 18:55:01 +0300 | [diff] [blame] | 3611 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) { |
| 3612 | ret = ufshcd_dme_set(hba, |
| 3613 | UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1); |
| 3614 | if (ret) { |
| 3615 | dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n", |
| 3616 | __func__, ret); |
| 3617 | goto out; |
| 3618 | } |
| 3619 | } |
| 3620 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3621 | uic_cmd.command = UIC_CMD_DME_SET; |
| 3622 | uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE); |
| 3623 | uic_cmd.argument3 = mode; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3624 | ufshcd_hold(hba, false); |
| 3625 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
| 3626 | ufshcd_release(hba); |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3627 | |
Yaniv Gardi | c3a2f9e | 2015-05-17 18:55:01 +0300 | [diff] [blame] | 3628 | out: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3629 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3630 | } |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3631 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 3632 | static int ufshcd_link_recovery(struct ufs_hba *hba) |
| 3633 | { |
| 3634 | int ret; |
| 3635 | unsigned long flags; |
| 3636 | |
| 3637 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 3638 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 3639 | ufshcd_set_eh_in_progress(hba); |
| 3640 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3641 | |
| 3642 | ret = ufshcd_host_reset_and_restore(hba); |
| 3643 | |
| 3644 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 3645 | if (ret) |
| 3646 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 3647 | ufshcd_clear_eh_in_progress(hba); |
| 3648 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3649 | |
| 3650 | if (ret) |
| 3651 | dev_err(hba->dev, "%s: link recovery failed, err %d", |
| 3652 | __func__, ret); |
| 3653 | |
| 3654 | return ret; |
| 3655 | } |
| 3656 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 3657 | static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3658 | { |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 3659 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3660 | struct uic_command uic_cmd = {0}; |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 3661 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3662 | |
Kiwoong Kim | ee32c90 | 2016-11-10 21:17:43 +0900 | [diff] [blame] | 3663 | ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE); |
| 3664 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3665 | uic_cmd.command = UIC_CMD_DME_HIBER_ENTER; |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 3666 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 3667 | trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter", |
| 3668 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3669 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 3670 | if (ret) { |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 3671 | dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n", |
| 3672 | __func__, ret); |
| 3673 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 3674 | /* |
| 3675 | * If link recovery fails then return error so that caller |
| 3676 | * don't retry the hibern8 enter again. |
| 3677 | */ |
| 3678 | if (ufshcd_link_recovery(hba)) |
| 3679 | ret = -ENOLINK; |
Kiwoong Kim | ee32c90 | 2016-11-10 21:17:43 +0900 | [diff] [blame] | 3680 | } else |
| 3681 | ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, |
| 3682 | POST_CHANGE); |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 3683 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 3684 | return ret; |
| 3685 | } |
| 3686 | |
| 3687 | static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba) |
| 3688 | { |
| 3689 | int ret = 0, retries; |
| 3690 | |
| 3691 | for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) { |
| 3692 | ret = __ufshcd_uic_hibern8_enter(hba); |
| 3693 | if (!ret || ret == -ENOLINK) |
| 3694 | goto out; |
| 3695 | } |
| 3696 | out: |
| 3697 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3698 | } |
| 3699 | |
| 3700 | static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba) |
| 3701 | { |
| 3702 | struct uic_command uic_cmd = {0}; |
| 3703 | int ret; |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 3704 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3705 | |
Kiwoong Kim | ee32c90 | 2016-11-10 21:17:43 +0900 | [diff] [blame] | 3706 | ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE); |
| 3707 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3708 | uic_cmd.command = UIC_CMD_DME_HIBER_EXIT; |
| 3709 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 3710 | trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit", |
| 3711 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
| 3712 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3713 | if (ret) { |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 3714 | dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n", |
| 3715 | __func__, ret); |
| 3716 | ret = ufshcd_link_recovery(hba); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 3717 | } else { |
Kiwoong Kim | ee32c90 | 2016-11-10 21:17:43 +0900 | [diff] [blame] | 3718 | ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, |
| 3719 | POST_CHANGE); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 3720 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_get(); |
| 3721 | hba->ufs_stats.hibern8_exit_cnt++; |
| 3722 | } |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3723 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3724 | return ret; |
| 3725 | } |
| 3726 | |
Yaniv Gardi | 5064636 | 2014-10-23 13:25:13 +0300 | [diff] [blame] | 3727 | /** |
| 3728 | * ufshcd_init_pwr_info - setting the POR (power on reset) |
| 3729 | * values in hba power info |
| 3730 | * @hba: per-adapter instance |
| 3731 | */ |
| 3732 | static void ufshcd_init_pwr_info(struct ufs_hba *hba) |
| 3733 | { |
| 3734 | hba->pwr_info.gear_rx = UFS_PWM_G1; |
| 3735 | hba->pwr_info.gear_tx = UFS_PWM_G1; |
| 3736 | hba->pwr_info.lane_rx = 1; |
| 3737 | hba->pwr_info.lane_tx = 1; |
| 3738 | hba->pwr_info.pwr_rx = SLOWAUTO_MODE; |
| 3739 | hba->pwr_info.pwr_tx = SLOWAUTO_MODE; |
| 3740 | hba->pwr_info.hs_rate = 0; |
| 3741 | } |
| 3742 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3743 | /** |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3744 | * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device |
| 3745 | * @hba: per-adapter instance |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3746 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3747 | static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3748 | { |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3749 | struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info; |
| 3750 | |
| 3751 | if (hba->max_pwr_info.is_valid) |
| 3752 | return 0; |
| 3753 | |
subhashj@codeaurora.org | 2349b53 | 2016-11-23 16:33:19 -0800 | [diff] [blame] | 3754 | pwr_info->pwr_tx = FAST_MODE; |
| 3755 | pwr_info->pwr_rx = FAST_MODE; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3756 | pwr_info->hs_rate = PA_HS_MODE_B; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3757 | |
| 3758 | /* Get the connected lane count */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3759 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES), |
| 3760 | &pwr_info->lane_rx); |
| 3761 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 3762 | &pwr_info->lane_tx); |
| 3763 | |
| 3764 | if (!pwr_info->lane_rx || !pwr_info->lane_tx) { |
| 3765 | dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n", |
| 3766 | __func__, |
| 3767 | pwr_info->lane_rx, |
| 3768 | pwr_info->lane_tx); |
| 3769 | return -EINVAL; |
| 3770 | } |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3771 | |
| 3772 | /* |
| 3773 | * First, get the maximum gears of HS speed. |
| 3774 | * If a zero value, it means there is no HSGEAR capability. |
| 3775 | * Then, get the maximum gears of PWM speed. |
| 3776 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3777 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx); |
| 3778 | if (!pwr_info->gear_rx) { |
| 3779 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), |
| 3780 | &pwr_info->gear_rx); |
| 3781 | if (!pwr_info->gear_rx) { |
| 3782 | dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n", |
| 3783 | __func__, pwr_info->gear_rx); |
| 3784 | return -EINVAL; |
| 3785 | } |
subhashj@codeaurora.org | 2349b53 | 2016-11-23 16:33:19 -0800 | [diff] [blame] | 3786 | pwr_info->pwr_rx = SLOW_MODE; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3787 | } |
| 3788 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3789 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), |
| 3790 | &pwr_info->gear_tx); |
| 3791 | if (!pwr_info->gear_tx) { |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3792 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3793 | &pwr_info->gear_tx); |
| 3794 | if (!pwr_info->gear_tx) { |
| 3795 | dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n", |
| 3796 | __func__, pwr_info->gear_tx); |
| 3797 | return -EINVAL; |
| 3798 | } |
subhashj@codeaurora.org | 2349b53 | 2016-11-23 16:33:19 -0800 | [diff] [blame] | 3799 | pwr_info->pwr_tx = SLOW_MODE; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3800 | } |
| 3801 | |
| 3802 | hba->max_pwr_info.is_valid = true; |
| 3803 | return 0; |
| 3804 | } |
| 3805 | |
| 3806 | static int ufshcd_change_power_mode(struct ufs_hba *hba, |
| 3807 | struct ufs_pa_layer_attr *pwr_mode) |
| 3808 | { |
| 3809 | int ret; |
| 3810 | |
| 3811 | /* if already configured to the requested pwr_mode */ |
| 3812 | if (pwr_mode->gear_rx == hba->pwr_info.gear_rx && |
| 3813 | pwr_mode->gear_tx == hba->pwr_info.gear_tx && |
| 3814 | pwr_mode->lane_rx == hba->pwr_info.lane_rx && |
| 3815 | pwr_mode->lane_tx == hba->pwr_info.lane_tx && |
| 3816 | pwr_mode->pwr_rx == hba->pwr_info.pwr_rx && |
| 3817 | pwr_mode->pwr_tx == hba->pwr_info.pwr_tx && |
| 3818 | pwr_mode->hs_rate == hba->pwr_info.hs_rate) { |
| 3819 | dev_dbg(hba->dev, "%s: power already configured\n", __func__); |
| 3820 | return 0; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3821 | } |
| 3822 | |
| 3823 | /* |
| 3824 | * Configure attributes for power mode change with below. |
| 3825 | * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION, |
| 3826 | * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION, |
| 3827 | * - PA_HSSERIES |
| 3828 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3829 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx); |
| 3830 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES), |
| 3831 | pwr_mode->lane_rx); |
| 3832 | if (pwr_mode->pwr_rx == FASTAUTO_MODE || |
| 3833 | pwr_mode->pwr_rx == FAST_MODE) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3834 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3835 | else |
| 3836 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3837 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3838 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx); |
| 3839 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES), |
| 3840 | pwr_mode->lane_tx); |
| 3841 | if (pwr_mode->pwr_tx == FASTAUTO_MODE || |
| 3842 | pwr_mode->pwr_tx == FAST_MODE) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3843 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3844 | else |
| 3845 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3846 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3847 | if (pwr_mode->pwr_rx == FASTAUTO_MODE || |
| 3848 | pwr_mode->pwr_tx == FASTAUTO_MODE || |
| 3849 | pwr_mode->pwr_rx == FAST_MODE || |
| 3850 | pwr_mode->pwr_tx == FAST_MODE) |
| 3851 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES), |
| 3852 | pwr_mode->hs_rate); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3853 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3854 | ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4 |
| 3855 | | pwr_mode->pwr_tx); |
| 3856 | |
| 3857 | if (ret) { |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3858 | dev_err(hba->dev, |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3859 | "%s: power mode change failed %d\n", __func__, ret); |
| 3860 | } else { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 3861 | ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL, |
| 3862 | pwr_mode); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3863 | |
| 3864 | memcpy(&hba->pwr_info, pwr_mode, |
| 3865 | sizeof(struct ufs_pa_layer_attr)); |
| 3866 | } |
| 3867 | |
| 3868 | return ret; |
| 3869 | } |
| 3870 | |
| 3871 | /** |
| 3872 | * ufshcd_config_pwr_mode - configure a new power mode |
| 3873 | * @hba: per-adapter instance |
| 3874 | * @desired_pwr_mode: desired power configuration |
| 3875 | */ |
| 3876 | static int ufshcd_config_pwr_mode(struct ufs_hba *hba, |
| 3877 | struct ufs_pa_layer_attr *desired_pwr_mode) |
| 3878 | { |
| 3879 | struct ufs_pa_layer_attr final_params = { 0 }; |
| 3880 | int ret; |
| 3881 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 3882 | ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE, |
| 3883 | desired_pwr_mode, &final_params); |
| 3884 | |
| 3885 | if (ret) |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 3886 | memcpy(&final_params, desired_pwr_mode, sizeof(final_params)); |
| 3887 | |
| 3888 | ret = ufshcd_change_power_mode(hba, &final_params); |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 3889 | if (!ret) |
| 3890 | ufshcd_print_pwr_info(hba); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3891 | |
| 3892 | return ret; |
| 3893 | } |
| 3894 | |
| 3895 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3896 | * ufshcd_complete_dev_init() - checks device readiness |
| 3897 | * hba: per-adapter instance |
| 3898 | * |
| 3899 | * Set fDeviceInit flag and poll until device toggles it. |
| 3900 | */ |
| 3901 | static int ufshcd_complete_dev_init(struct ufs_hba *hba) |
| 3902 | { |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 3903 | int i; |
| 3904 | int err; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3905 | bool flag_res = 1; |
| 3906 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 3907 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG, |
| 3908 | QUERY_FLAG_IDN_FDEVICEINIT, NULL); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3909 | if (err) { |
| 3910 | dev_err(hba->dev, |
| 3911 | "%s setting fDeviceInit flag failed with error %d\n", |
| 3912 | __func__, err); |
| 3913 | goto out; |
| 3914 | } |
| 3915 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 3916 | /* poll for max. 1000 iterations for fDeviceInit flag to clear */ |
| 3917 | for (i = 0; i < 1000 && !err && flag_res; i++) |
| 3918 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG, |
| 3919 | QUERY_FLAG_IDN_FDEVICEINIT, &flag_res); |
| 3920 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3921 | if (err) |
| 3922 | dev_err(hba->dev, |
| 3923 | "%s reading fDeviceInit flag failed with error %d\n", |
| 3924 | __func__, err); |
| 3925 | else if (flag_res) |
| 3926 | dev_err(hba->dev, |
| 3927 | "%s fDeviceInit was not cleared by the device\n", |
| 3928 | __func__); |
| 3929 | |
| 3930 | out: |
| 3931 | return err; |
| 3932 | } |
| 3933 | |
| 3934 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3935 | * ufshcd_make_hba_operational - Make UFS controller operational |
| 3936 | * @hba: per adapter instance |
| 3937 | * |
| 3938 | * To bring UFS host controller to operational state, |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 3939 | * 1. Enable required interrupts |
| 3940 | * 2. Configure interrupt aggregation |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 3941 | * 3. Program UTRL and UTMRL base address |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 3942 | * 4. Configure run-stop-registers |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3943 | * |
| 3944 | * Returns 0 on success, non-zero value on failure |
| 3945 | */ |
| 3946 | static int ufshcd_make_hba_operational(struct ufs_hba *hba) |
| 3947 | { |
| 3948 | int err = 0; |
| 3949 | u32 reg; |
| 3950 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3951 | /* Enable required interrupts */ |
| 3952 | ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS); |
| 3953 | |
| 3954 | /* Configure interrupt aggregation */ |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 3955 | if (ufshcd_is_intr_aggr_allowed(hba)) |
| 3956 | ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO); |
| 3957 | else |
| 3958 | ufshcd_disable_intr_aggr(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3959 | |
| 3960 | /* Configure UTRL and UTMRL base address registers */ |
| 3961 | ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr), |
| 3962 | REG_UTP_TRANSFER_REQ_LIST_BASE_L); |
| 3963 | ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr), |
| 3964 | REG_UTP_TRANSFER_REQ_LIST_BASE_H); |
| 3965 | ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr), |
| 3966 | REG_UTP_TASK_REQ_LIST_BASE_L); |
| 3967 | ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr), |
| 3968 | REG_UTP_TASK_REQ_LIST_BASE_H); |
| 3969 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3970 | /* |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 3971 | * Make sure base address and interrupt setup are updated before |
| 3972 | * enabling the run/stop registers below. |
| 3973 | */ |
| 3974 | wmb(); |
| 3975 | |
| 3976 | /* |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3977 | * UCRDY, UTMRLDY and UTRLRDY bits must be 1 |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3978 | */ |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 3979 | reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3980 | if (!(ufshcd_get_lists_status(reg))) { |
| 3981 | ufshcd_enable_run_stop_reg(hba); |
| 3982 | } else { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3983 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3984 | "Host controller not ready to process requests"); |
| 3985 | err = -EIO; |
| 3986 | goto out; |
| 3987 | } |
| 3988 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3989 | out: |
| 3990 | return err; |
| 3991 | } |
| 3992 | |
| 3993 | /** |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 3994 | * ufshcd_hba_stop - Send controller to reset state |
| 3995 | * @hba: per adapter instance |
| 3996 | * @can_sleep: perform sleep or just spin |
| 3997 | */ |
| 3998 | static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep) |
| 3999 | { |
| 4000 | int err; |
| 4001 | |
| 4002 | ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE); |
| 4003 | err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE, |
| 4004 | CONTROLLER_ENABLE, CONTROLLER_DISABLE, |
| 4005 | 10, 1, can_sleep); |
| 4006 | if (err) |
| 4007 | dev_err(hba->dev, "%s: Controller disable failed\n", __func__); |
| 4008 | } |
| 4009 | |
| 4010 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4011 | * ufshcd_hba_enable - initialize the controller |
| 4012 | * @hba: per adapter instance |
| 4013 | * |
| 4014 | * The controller resets itself and controller firmware initialization |
| 4015 | * sequence kicks off. When controller is ready it will set |
| 4016 | * the Host Controller Enable bit to 1. |
| 4017 | * |
| 4018 | * Returns 0 on success, non-zero value on failure |
| 4019 | */ |
| 4020 | static int ufshcd_hba_enable(struct ufs_hba *hba) |
| 4021 | { |
| 4022 | int retry; |
| 4023 | |
| 4024 | /* |
| 4025 | * msleep of 1 and 5 used in this function might result in msleep(20), |
| 4026 | * but it was necessary to send the UFS FPGA to reset mode during |
| 4027 | * development and testing of this driver. msleep can be changed to |
| 4028 | * mdelay and retry count can be reduced based on the controller. |
| 4029 | */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4030 | if (!ufshcd_is_hba_active(hba)) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4031 | /* change controller state to "reset state" */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4032 | ufshcd_hba_stop(hba, true); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4033 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4034 | /* UniPro link is disabled at this point */ |
| 4035 | ufshcd_set_link_off(hba); |
| 4036 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4037 | ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4038 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4039 | /* start controller initialization sequence */ |
| 4040 | ufshcd_hba_start(hba); |
| 4041 | |
| 4042 | /* |
| 4043 | * To initialize a UFS host controller HCE bit must be set to 1. |
| 4044 | * During initialization the HCE bit value changes from 1->0->1. |
| 4045 | * When the host controller completes initialization sequence |
| 4046 | * it sets the value of HCE bit to 1. The same HCE bit is read back |
| 4047 | * to check if the controller has completed initialization sequence. |
| 4048 | * So without this delay the value HCE = 1, set in the previous |
| 4049 | * instruction might be read back. |
| 4050 | * This delay can be changed based on the controller. |
| 4051 | */ |
| 4052 | msleep(1); |
| 4053 | |
| 4054 | /* wait for the host controller to complete initialization */ |
| 4055 | retry = 10; |
| 4056 | while (ufshcd_is_hba_active(hba)) { |
| 4057 | if (retry) { |
| 4058 | retry--; |
| 4059 | } else { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 4060 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4061 | "Controller enable failed\n"); |
| 4062 | return -EIO; |
| 4063 | } |
| 4064 | msleep(5); |
| 4065 | } |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4066 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4067 | /* enable UIC related interrupts */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4068 | ufshcd_enable_intr(hba, UFSHCD_UIC_MASK); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4069 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4070 | ufshcd_vops_hce_enable_notify(hba, POST_CHANGE); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4071 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4072 | return 0; |
| 4073 | } |
| 4074 | |
Yaniv Gardi | 7ca38cf | 2015-05-17 18:54:59 +0300 | [diff] [blame] | 4075 | static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer) |
| 4076 | { |
| 4077 | int tx_lanes, i, err = 0; |
| 4078 | |
| 4079 | if (!peer) |
| 4080 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 4081 | &tx_lanes); |
| 4082 | else |
| 4083 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 4084 | &tx_lanes); |
| 4085 | for (i = 0; i < tx_lanes; i++) { |
| 4086 | if (!peer) |
| 4087 | err = ufshcd_dme_set(hba, |
| 4088 | UIC_ARG_MIB_SEL(TX_LCC_ENABLE, |
| 4089 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)), |
| 4090 | 0); |
| 4091 | else |
| 4092 | err = ufshcd_dme_peer_set(hba, |
| 4093 | UIC_ARG_MIB_SEL(TX_LCC_ENABLE, |
| 4094 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)), |
| 4095 | 0); |
| 4096 | if (err) { |
| 4097 | dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d", |
| 4098 | __func__, peer, i, err); |
| 4099 | break; |
| 4100 | } |
| 4101 | } |
| 4102 | |
| 4103 | return err; |
| 4104 | } |
| 4105 | |
| 4106 | static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba) |
| 4107 | { |
| 4108 | return ufshcd_disable_tx_lcc(hba, true); |
| 4109 | } |
| 4110 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4111 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4112 | * ufshcd_link_startup - Initialize unipro link startup |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4113 | * @hba: per adapter instance |
| 4114 | * |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4115 | * Returns 0 for success, non-zero in case of failure |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4116 | */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4117 | static int ufshcd_link_startup(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4118 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4119 | int ret; |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4120 | int retries = DME_LINKSTARTUP_RETRIES; |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 4121 | bool link_startup_again = false; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4122 | |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 4123 | /* |
| 4124 | * If UFS device isn't active then we will have to issue link startup |
| 4125 | * 2 times to make sure the device state move to active. |
| 4126 | */ |
| 4127 | if (!ufshcd_is_ufs_dev_active(hba)) |
| 4128 | link_startup_again = true; |
| 4129 | |
| 4130 | link_startup: |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4131 | do { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4132 | ufshcd_vops_link_startup_notify(hba, PRE_CHANGE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4133 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4134 | ret = ufshcd_dme_link_startup(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4135 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4136 | /* check if device is detected by inter-connect layer */ |
| 4137 | if (!ret && !ufshcd_is_device_present(hba)) { |
| 4138 | dev_err(hba->dev, "%s: Device not present\n", __func__); |
| 4139 | ret = -ENXIO; |
| 4140 | goto out; |
| 4141 | } |
| 4142 | |
| 4143 | /* |
| 4144 | * DME link lost indication is only received when link is up, |
| 4145 | * but we can't be sure if the link is up until link startup |
| 4146 | * succeeds. So reset the local Uni-Pro and try again. |
| 4147 | */ |
| 4148 | if (ret && ufshcd_hba_enable(hba)) |
| 4149 | goto out; |
| 4150 | } while (ret && retries--); |
| 4151 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4152 | if (ret) |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4153 | /* failed to get the link up... retire */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4154 | goto out; |
| 4155 | |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 4156 | if (link_startup_again) { |
| 4157 | link_startup_again = false; |
| 4158 | retries = DME_LINKSTARTUP_RETRIES; |
| 4159 | goto link_startup; |
| 4160 | } |
| 4161 | |
subhashj@codeaurora.org | d2aebb9 | 2016-12-22 18:41:33 -0800 | [diff] [blame] | 4162 | /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */ |
| 4163 | ufshcd_init_pwr_info(hba); |
| 4164 | ufshcd_print_pwr_info(hba); |
| 4165 | |
Yaniv Gardi | 7ca38cf | 2015-05-17 18:54:59 +0300 | [diff] [blame] | 4166 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) { |
| 4167 | ret = ufshcd_disable_device_tx_lcc(hba); |
| 4168 | if (ret) |
| 4169 | goto out; |
| 4170 | } |
| 4171 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4172 | /* Include any host controller configuration via UIC commands */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4173 | ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE); |
| 4174 | if (ret) |
| 4175 | goto out; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4176 | |
| 4177 | ret = ufshcd_make_hba_operational(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4178 | out: |
Venkat Gopalakrishnan | 7942f7b | 2017-02-03 16:58:24 -0800 | [diff] [blame] | 4179 | if (ret) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4180 | dev_err(hba->dev, "link startup failed %d\n", ret); |
Venkat Gopalakrishnan | 7942f7b | 2017-02-03 16:58:24 -0800 | [diff] [blame] | 4181 | ufshcd_print_host_state(hba); |
| 4182 | ufshcd_print_pwr_info(hba); |
| 4183 | ufshcd_print_host_regs(hba); |
| 4184 | } |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4185 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4186 | } |
| 4187 | |
| 4188 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4189 | * ufshcd_verify_dev_init() - Verify device initialization |
| 4190 | * @hba: per-adapter instance |
| 4191 | * |
| 4192 | * Send NOP OUT UPIU and wait for NOP IN response to check whether the |
| 4193 | * device Transport Protocol (UTP) layer is ready after a reset. |
| 4194 | * If the UTP layer at the device side is not initialized, it may |
| 4195 | * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT |
| 4196 | * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations. |
| 4197 | */ |
| 4198 | static int ufshcd_verify_dev_init(struct ufs_hba *hba) |
| 4199 | { |
| 4200 | int err = 0; |
| 4201 | int retries; |
| 4202 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4203 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4204 | mutex_lock(&hba->dev_cmd.lock); |
| 4205 | for (retries = NOP_OUT_RETRIES; retries > 0; retries--) { |
| 4206 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP, |
| 4207 | NOP_OUT_TIMEOUT); |
| 4208 | |
| 4209 | if (!err || err == -ETIMEDOUT) |
| 4210 | break; |
| 4211 | |
| 4212 | dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err); |
| 4213 | } |
| 4214 | mutex_unlock(&hba->dev_cmd.lock); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4215 | ufshcd_release(hba); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4216 | |
| 4217 | if (err) |
| 4218 | dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err); |
| 4219 | return err; |
| 4220 | } |
| 4221 | |
| 4222 | /** |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4223 | * ufshcd_set_queue_depth - set lun queue depth |
| 4224 | * @sdev: pointer to SCSI device |
| 4225 | * |
| 4226 | * Read bLUQueueDepth value and activate scsi tagged command |
| 4227 | * queueing. For WLUN, queue depth is set to 1. For best-effort |
| 4228 | * cases (bLUQueueDepth = 0) the queue depth is set to a maximum |
| 4229 | * value that host can queue. |
| 4230 | */ |
| 4231 | static void ufshcd_set_queue_depth(struct scsi_device *sdev) |
| 4232 | { |
| 4233 | int ret = 0; |
| 4234 | u8 lun_qdepth; |
| 4235 | struct ufs_hba *hba; |
| 4236 | |
| 4237 | hba = shost_priv(sdev->host); |
| 4238 | |
| 4239 | lun_qdepth = hba->nutrs; |
Szymon Mielczarek | dbd34a6 | 2017-03-29 08:19:21 +0200 | [diff] [blame] | 4240 | ret = ufshcd_read_unit_desc_param(hba, |
| 4241 | ufshcd_scsi_to_upiu_lun(sdev->lun), |
| 4242 | UNIT_DESC_PARAM_LU_Q_DEPTH, |
| 4243 | &lun_qdepth, |
| 4244 | sizeof(lun_qdepth)); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4245 | |
| 4246 | /* Some WLUN doesn't support unit descriptor */ |
| 4247 | if (ret == -EOPNOTSUPP) |
| 4248 | lun_qdepth = 1; |
| 4249 | else if (!lun_qdepth) |
| 4250 | /* eventually, we can figure out the real queue depth */ |
| 4251 | lun_qdepth = hba->nutrs; |
| 4252 | else |
| 4253 | lun_qdepth = min_t(int, lun_qdepth, hba->nutrs); |
| 4254 | |
| 4255 | dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n", |
| 4256 | __func__, lun_qdepth); |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4257 | scsi_change_queue_depth(sdev, lun_qdepth); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4258 | } |
| 4259 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4260 | /* |
| 4261 | * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR |
| 4262 | * @hba: per-adapter instance |
| 4263 | * @lun: UFS device lun id |
| 4264 | * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info |
| 4265 | * |
| 4266 | * Returns 0 in case of success and b_lu_write_protect status would be returned |
| 4267 | * @b_lu_write_protect parameter. |
| 4268 | * Returns -ENOTSUPP if reading b_lu_write_protect is not supported. |
| 4269 | * Returns -EINVAL in case of invalid parameters passed to this function. |
| 4270 | */ |
| 4271 | static int ufshcd_get_lu_wp(struct ufs_hba *hba, |
| 4272 | u8 lun, |
| 4273 | u8 *b_lu_write_protect) |
| 4274 | { |
| 4275 | int ret; |
| 4276 | |
| 4277 | if (!b_lu_write_protect) |
| 4278 | ret = -EINVAL; |
| 4279 | /* |
| 4280 | * According to UFS device spec, RPMB LU can't be write |
| 4281 | * protected so skip reading bLUWriteProtect parameter for |
| 4282 | * it. For other W-LUs, UNIT DESCRIPTOR is not available. |
| 4283 | */ |
| 4284 | else if (lun >= UFS_UPIU_MAX_GENERAL_LUN) |
| 4285 | ret = -ENOTSUPP; |
| 4286 | else |
| 4287 | ret = ufshcd_read_unit_desc_param(hba, |
| 4288 | lun, |
| 4289 | UNIT_DESC_PARAM_LU_WR_PROTECT, |
| 4290 | b_lu_write_protect, |
| 4291 | sizeof(*b_lu_write_protect)); |
| 4292 | return ret; |
| 4293 | } |
| 4294 | |
| 4295 | /** |
| 4296 | * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect |
| 4297 | * status |
| 4298 | * @hba: per-adapter instance |
| 4299 | * @sdev: pointer to SCSI device |
| 4300 | * |
| 4301 | */ |
| 4302 | static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba, |
| 4303 | struct scsi_device *sdev) |
| 4304 | { |
| 4305 | if (hba->dev_info.f_power_on_wp_en && |
| 4306 | !hba->dev_info.is_lu_power_on_wp) { |
| 4307 | u8 b_lu_write_protect; |
| 4308 | |
| 4309 | if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun), |
| 4310 | &b_lu_write_protect) && |
| 4311 | (b_lu_write_protect == UFS_LU_POWER_ON_WP)) |
| 4312 | hba->dev_info.is_lu_power_on_wp = true; |
| 4313 | } |
| 4314 | } |
| 4315 | |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4316 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4317 | * ufshcd_slave_alloc - handle initial SCSI device configurations |
| 4318 | * @sdev: pointer to SCSI device |
| 4319 | * |
| 4320 | * Returns success |
| 4321 | */ |
| 4322 | static int ufshcd_slave_alloc(struct scsi_device *sdev) |
| 4323 | { |
| 4324 | struct ufs_hba *hba; |
| 4325 | |
| 4326 | hba = shost_priv(sdev->host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4327 | |
| 4328 | /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */ |
| 4329 | sdev->use_10_for_ms = 1; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4330 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4331 | /* allow SCSI layer to restart the device in case of errors */ |
| 4332 | sdev->allow_restart = 1; |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4333 | |
Sujit Reddy Thumma | b2a6c52 | 2014-07-01 12:22:38 +0300 | [diff] [blame] | 4334 | /* REPORT SUPPORTED OPERATION CODES is not supported */ |
| 4335 | sdev->no_report_opcodes = 1; |
| 4336 | |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4337 | |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4338 | ufshcd_set_queue_depth(sdev); |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4339 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4340 | ufshcd_get_lu_power_on_wp_status(hba, sdev); |
| 4341 | |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4342 | return 0; |
| 4343 | } |
| 4344 | |
| 4345 | /** |
| 4346 | * ufshcd_change_queue_depth - change queue depth |
| 4347 | * @sdev: pointer to SCSI device |
| 4348 | * @depth: required depth to set |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4349 | * |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4350 | * 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] | 4351 | */ |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4352 | 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] | 4353 | { |
| 4354 | struct ufs_hba *hba = shost_priv(sdev->host); |
| 4355 | |
| 4356 | if (depth > hba->nutrs) |
| 4357 | depth = hba->nutrs; |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4358 | return scsi_change_queue_depth(sdev, depth); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4359 | } |
| 4360 | |
| 4361 | /** |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 4362 | * ufshcd_slave_configure - adjust SCSI device configurations |
| 4363 | * @sdev: pointer to SCSI device |
| 4364 | */ |
| 4365 | static int ufshcd_slave_configure(struct scsi_device *sdev) |
| 4366 | { |
| 4367 | struct request_queue *q = sdev->request_queue; |
| 4368 | |
| 4369 | blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1); |
| 4370 | blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX); |
| 4371 | |
| 4372 | return 0; |
| 4373 | } |
| 4374 | |
| 4375 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4376 | * ufshcd_slave_destroy - remove SCSI device configurations |
| 4377 | * @sdev: pointer to SCSI device |
| 4378 | */ |
| 4379 | static void ufshcd_slave_destroy(struct scsi_device *sdev) |
| 4380 | { |
| 4381 | struct ufs_hba *hba; |
| 4382 | |
| 4383 | hba = shost_priv(sdev->host); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4384 | /* Drop the reference as it won't be needed anymore */ |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 4385 | if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) { |
| 4386 | unsigned long flags; |
| 4387 | |
| 4388 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4389 | hba->sdev_ufs_device = NULL; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 4390 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4391 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4392 | } |
| 4393 | |
| 4394 | /** |
| 4395 | * ufshcd_task_req_compl - handle task management request completion |
| 4396 | * @hba: per adapter instance |
| 4397 | * @index: index of the completed request |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4398 | * @resp: task management service response |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4399 | * |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4400 | * Returns non-zero value on error, zero on success |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4401 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4402 | 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] | 4403 | { |
| 4404 | struct utp_task_req_desc *task_req_descp; |
| 4405 | struct utp_upiu_task_rsp *task_rsp_upiup; |
| 4406 | unsigned long flags; |
| 4407 | int ocs_value; |
| 4408 | int task_result; |
| 4409 | |
| 4410 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4411 | |
| 4412 | /* Clear completed tasks from outstanding_tasks */ |
| 4413 | __clear_bit(index, &hba->outstanding_tasks); |
| 4414 | |
| 4415 | task_req_descp = hba->utmrdl_base_addr; |
| 4416 | ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]); |
| 4417 | |
| 4418 | if (ocs_value == OCS_SUCCESS) { |
| 4419 | task_rsp_upiup = (struct utp_upiu_task_rsp *) |
| 4420 | task_req_descp[index].task_rsp_upiu; |
Kiwoong Kim | 8794ee0 | 2016-09-09 08:22:22 +0900 | [diff] [blame] | 4421 | task_result = be32_to_cpu(task_rsp_upiup->output_param1); |
| 4422 | task_result = task_result & MASK_TM_SERVICE_RESP; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4423 | if (resp) |
| 4424 | *resp = (u8)task_result; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4425 | } else { |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4426 | dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", |
| 4427 | __func__, ocs_value); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4428 | } |
| 4429 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4430 | |
| 4431 | return ocs_value; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4432 | } |
| 4433 | |
| 4434 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4435 | * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status |
| 4436 | * @lrb: pointer to local reference block of completed command |
| 4437 | * @scsi_status: SCSI command status |
| 4438 | * |
| 4439 | * Returns value base on SCSI command status |
| 4440 | */ |
| 4441 | static inline int |
| 4442 | ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status) |
| 4443 | { |
| 4444 | int result = 0; |
| 4445 | |
| 4446 | switch (scsi_status) { |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 4447 | case SAM_STAT_CHECK_CONDITION: |
| 4448 | ufshcd_copy_sense_data(lrbp); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4449 | case SAM_STAT_GOOD: |
| 4450 | result |= DID_OK << 16 | |
| 4451 | COMMAND_COMPLETE << 8 | |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 4452 | scsi_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4453 | break; |
| 4454 | case SAM_STAT_TASK_SET_FULL: |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 4455 | case SAM_STAT_BUSY: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4456 | case SAM_STAT_TASK_ABORTED: |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 4457 | ufshcd_copy_sense_data(lrbp); |
| 4458 | result |= scsi_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4459 | break; |
| 4460 | default: |
| 4461 | result |= DID_ERROR << 16; |
| 4462 | break; |
| 4463 | } /* end of switch */ |
| 4464 | |
| 4465 | return result; |
| 4466 | } |
| 4467 | |
| 4468 | /** |
| 4469 | * ufshcd_transfer_rsp_status - Get overall status of the response |
| 4470 | * @hba: per adapter instance |
| 4471 | * @lrb: pointer to local reference block of completed command |
| 4472 | * |
| 4473 | * Returns result of the command to notify SCSI midlayer |
| 4474 | */ |
| 4475 | static inline int |
| 4476 | ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 4477 | { |
| 4478 | int result = 0; |
| 4479 | int scsi_status; |
| 4480 | int ocs; |
| 4481 | |
| 4482 | /* overall command status of utrd */ |
| 4483 | ocs = ufshcd_get_tr_ocs(lrbp); |
| 4484 | |
| 4485 | switch (ocs) { |
| 4486 | case OCS_SUCCESS: |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4487 | result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 4488 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4489 | switch (result) { |
| 4490 | case UPIU_TRANSACTION_RESPONSE: |
| 4491 | /* |
| 4492 | * get the response UPIU result to extract |
| 4493 | * the SCSI command status |
| 4494 | */ |
| 4495 | result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr); |
| 4496 | |
| 4497 | /* |
| 4498 | * get the result based on SCSI status response |
| 4499 | * to notify the SCSI midlayer of the command status |
| 4500 | */ |
| 4501 | scsi_status = result & MASK_SCSI_STATUS; |
| 4502 | result = ufshcd_scsi_cmd_status(lrbp, scsi_status); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4503 | |
Yaniv Gardi | f05ac2e | 2016-02-01 15:02:42 +0200 | [diff] [blame] | 4504 | /* |
| 4505 | * Currently we are only supporting BKOPs exception |
| 4506 | * events hence we can ignore BKOPs exception event |
| 4507 | * during power management callbacks. BKOPs exception |
| 4508 | * event is not expected to be raised in runtime suspend |
| 4509 | * callback as it allows the urgent bkops. |
| 4510 | * During system suspend, we are anyway forcefully |
| 4511 | * disabling the bkops and if urgent bkops is needed |
| 4512 | * it will be enabled on system resume. Long term |
| 4513 | * solution could be to abort the system suspend if |
| 4514 | * UFS device needs urgent BKOPs. |
| 4515 | */ |
| 4516 | if (!hba->pm_op_in_progress && |
| 4517 | ufshcd_is_exception_event(lrbp->ucd_rsp_ptr)) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4518 | schedule_work(&hba->eeh_work); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4519 | break; |
| 4520 | case UPIU_TRANSACTION_REJECT_UPIU: |
| 4521 | /* TODO: handle Reject UPIU Response */ |
| 4522 | result = DID_ERROR << 16; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 4523 | dev_err(hba->dev, |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4524 | "Reject UPIU not fully implemented\n"); |
| 4525 | break; |
| 4526 | default: |
| 4527 | result = DID_ERROR << 16; |
| 4528 | dev_err(hba->dev, |
| 4529 | "Unexpected request response code = %x\n", |
| 4530 | result); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4531 | break; |
| 4532 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4533 | break; |
| 4534 | case OCS_ABORTED: |
| 4535 | result |= DID_ABORT << 16; |
| 4536 | break; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4537 | case OCS_INVALID_COMMAND_STATUS: |
| 4538 | result |= DID_REQUEUE << 16; |
| 4539 | break; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4540 | case OCS_INVALID_CMD_TABLE_ATTR: |
| 4541 | case OCS_INVALID_PRDT_ATTR: |
| 4542 | case OCS_MISMATCH_DATA_BUF_SIZE: |
| 4543 | case OCS_MISMATCH_RESP_UPIU_SIZE: |
| 4544 | case OCS_PEER_COMM_FAILURE: |
| 4545 | case OCS_FATAL_ERROR: |
| 4546 | default: |
| 4547 | result |= DID_ERROR << 16; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 4548 | dev_err(hba->dev, |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 4549 | "OCS error from controller = %x for tag %d\n", |
| 4550 | ocs, lrbp->task_tag); |
| 4551 | ufshcd_print_host_regs(hba); |
Gilad Broner | 6ba6558 | 2017-02-03 16:57:28 -0800 | [diff] [blame] | 4552 | ufshcd_print_host_state(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4553 | break; |
| 4554 | } /* end of switch */ |
| 4555 | |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 4556 | if (host_byte(result) != DID_OK) |
| 4557 | ufshcd_print_trs(hba, 1 << lrbp->task_tag, true); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4558 | return result; |
| 4559 | } |
| 4560 | |
| 4561 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4562 | * ufshcd_uic_cmd_compl - handle completion of uic command |
| 4563 | * @hba: per adapter instance |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4564 | * @intr_status: interrupt status generated by the controller |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4565 | */ |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4566 | 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] | 4567 | { |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4568 | if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4569 | hba->active_uic_cmd->argument2 |= |
| 4570 | ufshcd_get_uic_cmd_result(hba); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 4571 | hba->active_uic_cmd->argument3 = |
| 4572 | ufshcd_get_dme_attr_val(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4573 | complete(&hba->active_uic_cmd->done); |
| 4574 | } |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4575 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4576 | if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done) |
| 4577 | complete(hba->uic_async_done); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4578 | } |
| 4579 | |
| 4580 | /** |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4581 | * __ufshcd_transfer_req_compl - handle SCSI and query command completion |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4582 | * @hba: per adapter instance |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4583 | * @completed_reqs: requests to complete |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4584 | */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4585 | static void __ufshcd_transfer_req_compl(struct ufs_hba *hba, |
| 4586 | unsigned long completed_reqs) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4587 | { |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4588 | struct ufshcd_lrb *lrbp; |
| 4589 | struct scsi_cmnd *cmd; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4590 | int result; |
| 4591 | int index; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4592 | |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4593 | for_each_set_bit(index, &completed_reqs, hba->nutrs) { |
| 4594 | lrbp = &hba->lrb[index]; |
| 4595 | cmd = lrbp->cmd; |
| 4596 | if (cmd) { |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 4597 | ufshcd_add_command_trace(hba, index, "complete"); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4598 | result = ufshcd_transfer_rsp_status(hba, lrbp); |
| 4599 | scsi_dma_unmap(cmd); |
| 4600 | cmd->result = result; |
| 4601 | /* Mark completed command as NULL in LRB */ |
| 4602 | lrbp->cmd = NULL; |
| 4603 | clear_bit_unlock(index, &hba->lrb_in_use); |
| 4604 | /* Do not touch lrbp after scsi done */ |
| 4605 | cmd->scsi_done(cmd); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4606 | __ufshcd_release(hba); |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 4607 | } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE || |
| 4608 | lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) { |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 4609 | if (hba->dev_cmd.complete) { |
| 4610 | ufshcd_add_command_trace(hba, index, |
| 4611 | "dev_complete"); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4612 | complete(hba->dev_cmd.complete); |
Lee Susman | 1a07f2d | 2016-12-22 18:42:03 -0800 | [diff] [blame] | 4613 | } |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4614 | } |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 4615 | if (ufshcd_is_clkscaling_supported(hba)) |
| 4616 | hba->clk_scaling.active_reqs--; |
Zang Leigang | 0901718 | 2017-09-27 10:06:06 +0800 | [diff] [blame] | 4617 | |
| 4618 | lrbp->compl_time_stamp = ktime_get(); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 4619 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4620 | |
| 4621 | /* clear corresponding bits of completed commands */ |
| 4622 | hba->outstanding_reqs ^= completed_reqs; |
| 4623 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 4624 | ufshcd_clk_scaling_update_busy(hba); |
| 4625 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4626 | /* we might have free'd some tags above */ |
| 4627 | wake_up(&hba->dev_cmd.tag_wq); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4628 | } |
| 4629 | |
| 4630 | /** |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4631 | * ufshcd_transfer_req_compl - handle SCSI and query command completion |
| 4632 | * @hba: per adapter instance |
| 4633 | */ |
| 4634 | static void ufshcd_transfer_req_compl(struct ufs_hba *hba) |
| 4635 | { |
| 4636 | unsigned long completed_reqs; |
| 4637 | u32 tr_doorbell; |
| 4638 | |
| 4639 | /* Resetting interrupt aggregation counters first and reading the |
| 4640 | * DOOR_BELL afterward allows us to handle all the completed requests. |
| 4641 | * In order to prevent other interrupts starvation the DB is read once |
| 4642 | * after reset. The down side of this solution is the possibility of |
| 4643 | * false interrupt if device completes another request after resetting |
| 4644 | * aggregation and before reading the DB. |
| 4645 | */ |
| 4646 | if (ufshcd_is_intr_aggr_allowed(hba)) |
| 4647 | ufshcd_reset_intr_aggr(hba); |
| 4648 | |
| 4649 | tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 4650 | completed_reqs = tr_doorbell ^ hba->outstanding_reqs; |
| 4651 | |
| 4652 | __ufshcd_transfer_req_compl(hba, completed_reqs); |
| 4653 | } |
| 4654 | |
| 4655 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4656 | * ufshcd_disable_ee - disable exception event |
| 4657 | * @hba: per-adapter instance |
| 4658 | * @mask: exception event to disable |
| 4659 | * |
| 4660 | * Disables exception event in the device so that the EVENT_ALERT |
| 4661 | * bit is not set. |
| 4662 | * |
| 4663 | * Returns zero on success, non-zero error value on failure. |
| 4664 | */ |
| 4665 | static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask) |
| 4666 | { |
| 4667 | int err = 0; |
| 4668 | u32 val; |
| 4669 | |
| 4670 | if (!(hba->ee_ctrl_mask & mask)) |
| 4671 | goto out; |
| 4672 | |
| 4673 | val = hba->ee_ctrl_mask & ~mask; |
Tomohiro Kusumi | d7e2ddd | 2017-04-20 15:01:44 +0300 | [diff] [blame] | 4674 | val &= MASK_EE_STATUS; |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 4675 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4676 | QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val); |
| 4677 | if (!err) |
| 4678 | hba->ee_ctrl_mask &= ~mask; |
| 4679 | out: |
| 4680 | return err; |
| 4681 | } |
| 4682 | |
| 4683 | /** |
| 4684 | * ufshcd_enable_ee - enable exception event |
| 4685 | * @hba: per-adapter instance |
| 4686 | * @mask: exception event to enable |
| 4687 | * |
| 4688 | * Enable corresponding exception event in the device to allow |
| 4689 | * device to alert host in critical scenarios. |
| 4690 | * |
| 4691 | * Returns zero on success, non-zero error value on failure. |
| 4692 | */ |
| 4693 | static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask) |
| 4694 | { |
| 4695 | int err = 0; |
| 4696 | u32 val; |
| 4697 | |
| 4698 | if (hba->ee_ctrl_mask & mask) |
| 4699 | goto out; |
| 4700 | |
| 4701 | val = hba->ee_ctrl_mask | mask; |
Tomohiro Kusumi | d7e2ddd | 2017-04-20 15:01:44 +0300 | [diff] [blame] | 4702 | val &= MASK_EE_STATUS; |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 4703 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4704 | QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val); |
| 4705 | if (!err) |
| 4706 | hba->ee_ctrl_mask |= mask; |
| 4707 | out: |
| 4708 | return err; |
| 4709 | } |
| 4710 | |
| 4711 | /** |
| 4712 | * ufshcd_enable_auto_bkops - Allow device managed BKOPS |
| 4713 | * @hba: per-adapter instance |
| 4714 | * |
| 4715 | * Allow device to manage background operations on its own. Enabling |
| 4716 | * this might lead to inconsistent latencies during normal data transfers |
| 4717 | * as the device is allowed to manage its own way of handling background |
| 4718 | * operations. |
| 4719 | * |
| 4720 | * Returns zero on success, non-zero on failure. |
| 4721 | */ |
| 4722 | static int ufshcd_enable_auto_bkops(struct ufs_hba *hba) |
| 4723 | { |
| 4724 | int err = 0; |
| 4725 | |
| 4726 | if (hba->auto_bkops_enabled) |
| 4727 | goto out; |
| 4728 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 4729 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4730 | QUERY_FLAG_IDN_BKOPS_EN, NULL); |
| 4731 | if (err) { |
| 4732 | dev_err(hba->dev, "%s: failed to enable bkops %d\n", |
| 4733 | __func__, err); |
| 4734 | goto out; |
| 4735 | } |
| 4736 | |
| 4737 | hba->auto_bkops_enabled = true; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 4738 | trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Enabled"); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4739 | |
| 4740 | /* No need of URGENT_BKOPS exception from the device */ |
| 4741 | err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 4742 | if (err) |
| 4743 | dev_err(hba->dev, "%s: failed to disable exception event %d\n", |
| 4744 | __func__, err); |
| 4745 | out: |
| 4746 | return err; |
| 4747 | } |
| 4748 | |
| 4749 | /** |
| 4750 | * ufshcd_disable_auto_bkops - block device in doing background operations |
| 4751 | * @hba: per-adapter instance |
| 4752 | * |
| 4753 | * Disabling background operations improves command response latency but |
| 4754 | * has drawback of device moving into critical state where the device is |
| 4755 | * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the |
| 4756 | * host is idle so that BKOPS are managed effectively without any negative |
| 4757 | * impacts. |
| 4758 | * |
| 4759 | * Returns zero on success, non-zero on failure. |
| 4760 | */ |
| 4761 | static int ufshcd_disable_auto_bkops(struct ufs_hba *hba) |
| 4762 | { |
| 4763 | int err = 0; |
| 4764 | |
| 4765 | if (!hba->auto_bkops_enabled) |
| 4766 | goto out; |
| 4767 | |
| 4768 | /* |
| 4769 | * If host assisted BKOPs is to be enabled, make sure |
| 4770 | * urgent bkops exception is allowed. |
| 4771 | */ |
| 4772 | err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 4773 | if (err) { |
| 4774 | dev_err(hba->dev, "%s: failed to enable exception event %d\n", |
| 4775 | __func__, err); |
| 4776 | goto out; |
| 4777 | } |
| 4778 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 4779 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4780 | QUERY_FLAG_IDN_BKOPS_EN, NULL); |
| 4781 | if (err) { |
| 4782 | dev_err(hba->dev, "%s: failed to disable bkops %d\n", |
| 4783 | __func__, err); |
| 4784 | ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 4785 | goto out; |
| 4786 | } |
| 4787 | |
| 4788 | hba->auto_bkops_enabled = false; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 4789 | trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Disabled"); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4790 | out: |
| 4791 | return err; |
| 4792 | } |
| 4793 | |
| 4794 | /** |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 4795 | * ufshcd_force_reset_auto_bkops - force reset auto bkops state |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4796 | * @hba: per adapter instance |
| 4797 | * |
| 4798 | * After a device reset the device may toggle the BKOPS_EN flag |
| 4799 | * to default value. The s/w tracking variables should be updated |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 4800 | * as well. This function would change the auto-bkops state based on |
| 4801 | * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4802 | */ |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 4803 | static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4804 | { |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 4805 | if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) { |
| 4806 | hba->auto_bkops_enabled = false; |
| 4807 | hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS; |
| 4808 | ufshcd_enable_auto_bkops(hba); |
| 4809 | } else { |
| 4810 | hba->auto_bkops_enabled = true; |
| 4811 | hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS; |
| 4812 | ufshcd_disable_auto_bkops(hba); |
| 4813 | } |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4814 | } |
| 4815 | |
| 4816 | static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status) |
| 4817 | { |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 4818 | return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4819 | QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status); |
| 4820 | } |
| 4821 | |
| 4822 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4823 | * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status |
| 4824 | * @hba: per-adapter instance |
| 4825 | * @status: bkops_status value |
| 4826 | * |
| 4827 | * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn |
| 4828 | * flag in the device to permit background operations if the device |
| 4829 | * bkops_status is greater than or equal to "status" argument passed to |
| 4830 | * this function, disable otherwise. |
| 4831 | * |
| 4832 | * Returns 0 for success, non-zero in case of failure. |
| 4833 | * |
| 4834 | * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag |
| 4835 | * to know whether auto bkops is enabled or disabled after this function |
| 4836 | * returns control to it. |
| 4837 | */ |
| 4838 | static int ufshcd_bkops_ctrl(struct ufs_hba *hba, |
| 4839 | enum bkops_status status) |
| 4840 | { |
| 4841 | int err; |
| 4842 | u32 curr_status = 0; |
| 4843 | |
| 4844 | err = ufshcd_get_bkops_status(hba, &curr_status); |
| 4845 | if (err) { |
| 4846 | dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", |
| 4847 | __func__, err); |
| 4848 | goto out; |
| 4849 | } else if (curr_status > BKOPS_STATUS_MAX) { |
| 4850 | dev_err(hba->dev, "%s: invalid BKOPS status %d\n", |
| 4851 | __func__, curr_status); |
| 4852 | err = -EINVAL; |
| 4853 | goto out; |
| 4854 | } |
| 4855 | |
| 4856 | if (curr_status >= status) |
| 4857 | err = ufshcd_enable_auto_bkops(hba); |
| 4858 | else |
| 4859 | err = ufshcd_disable_auto_bkops(hba); |
| 4860 | out: |
| 4861 | return err; |
| 4862 | } |
| 4863 | |
| 4864 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4865 | * ufshcd_urgent_bkops - handle urgent bkops exception event |
| 4866 | * @hba: per-adapter instance |
| 4867 | * |
| 4868 | * Enable fBackgroundOpsEn flag in the device to permit background |
| 4869 | * operations. |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4870 | * |
| 4871 | * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled |
| 4872 | * and negative error value for any other failure. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4873 | */ |
| 4874 | static int ufshcd_urgent_bkops(struct ufs_hba *hba) |
| 4875 | { |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 4876 | return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status) |
| 4880 | { |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 4881 | return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4882 | QUERY_ATTR_IDN_EE_STATUS, 0, 0, status); |
| 4883 | } |
| 4884 | |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 4885 | static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba) |
| 4886 | { |
| 4887 | int err; |
| 4888 | u32 curr_status = 0; |
| 4889 | |
| 4890 | if (hba->is_urgent_bkops_lvl_checked) |
| 4891 | goto enable_auto_bkops; |
| 4892 | |
| 4893 | err = ufshcd_get_bkops_status(hba, &curr_status); |
| 4894 | if (err) { |
| 4895 | dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", |
| 4896 | __func__, err); |
| 4897 | goto out; |
| 4898 | } |
| 4899 | |
| 4900 | /* |
| 4901 | * We are seeing that some devices are raising the urgent bkops |
| 4902 | * exception events even when BKOPS status doesn't indicate performace |
| 4903 | * impacted or critical. Handle these device by determining their urgent |
| 4904 | * bkops status at runtime. |
| 4905 | */ |
| 4906 | if (curr_status < BKOPS_STATUS_PERF_IMPACT) { |
| 4907 | dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n", |
| 4908 | __func__, curr_status); |
| 4909 | /* update the current status as the urgent bkops level */ |
| 4910 | hba->urgent_bkops_lvl = curr_status; |
| 4911 | hba->is_urgent_bkops_lvl_checked = true; |
| 4912 | } |
| 4913 | |
| 4914 | enable_auto_bkops: |
| 4915 | err = ufshcd_enable_auto_bkops(hba); |
| 4916 | out: |
| 4917 | if (err < 0) |
| 4918 | dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n", |
| 4919 | __func__, err); |
| 4920 | } |
| 4921 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4922 | /** |
| 4923 | * ufshcd_exception_event_handler - handle exceptions raised by device |
| 4924 | * @work: pointer to work data |
| 4925 | * |
| 4926 | * Read bExceptionEventStatus attribute from the device and handle the |
| 4927 | * exception event accordingly. |
| 4928 | */ |
| 4929 | static void ufshcd_exception_event_handler(struct work_struct *work) |
| 4930 | { |
| 4931 | struct ufs_hba *hba; |
| 4932 | int err; |
| 4933 | u32 status = 0; |
| 4934 | hba = container_of(work, struct ufs_hba, eeh_work); |
| 4935 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 4936 | pm_runtime_get_sync(hba->dev); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4937 | err = ufshcd_get_ee_status(hba, &status); |
| 4938 | if (err) { |
| 4939 | dev_err(hba->dev, "%s: failed to get exception status %d\n", |
| 4940 | __func__, err); |
| 4941 | goto out; |
| 4942 | } |
| 4943 | |
| 4944 | status &= hba->ee_ctrl_mask; |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 4945 | |
| 4946 | if (status & MASK_EE_URGENT_BKOPS) |
| 4947 | ufshcd_bkops_exception_event_handler(hba); |
| 4948 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4949 | out: |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 4950 | pm_runtime_put_sync(hba->dev); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4951 | return; |
| 4952 | } |
| 4953 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 4954 | /* Complete requests that have door-bell cleared */ |
| 4955 | static void ufshcd_complete_requests(struct ufs_hba *hba) |
| 4956 | { |
| 4957 | ufshcd_transfer_req_compl(hba); |
| 4958 | ufshcd_tmc_handler(hba); |
| 4959 | } |
| 4960 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4961 | /** |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 4962 | * ufshcd_quirk_dl_nac_errors - This function checks if error handling is |
| 4963 | * to recover from the DL NAC errors or not. |
| 4964 | * @hba: per-adapter instance |
| 4965 | * |
| 4966 | * Returns true if error handling is required, false otherwise |
| 4967 | */ |
| 4968 | static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba) |
| 4969 | { |
| 4970 | unsigned long flags; |
| 4971 | bool err_handling = true; |
| 4972 | |
| 4973 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4974 | /* |
| 4975 | * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the |
| 4976 | * device fatal error and/or DL NAC & REPLAY timeout errors. |
| 4977 | */ |
| 4978 | if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR)) |
| 4979 | goto out; |
| 4980 | |
| 4981 | if ((hba->saved_err & DEVICE_FATAL_ERROR) || |
| 4982 | ((hba->saved_err & UIC_ERROR) && |
| 4983 | (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR))) |
| 4984 | goto out; |
| 4985 | |
| 4986 | if ((hba->saved_err & UIC_ERROR) && |
| 4987 | (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) { |
| 4988 | int err; |
| 4989 | /* |
| 4990 | * wait for 50ms to see if we can get any other errors or not. |
| 4991 | */ |
| 4992 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4993 | msleep(50); |
| 4994 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4995 | |
| 4996 | /* |
| 4997 | * now check if we have got any other severe errors other than |
| 4998 | * DL NAC error? |
| 4999 | */ |
| 5000 | if ((hba->saved_err & INT_FATAL_ERRORS) || |
| 5001 | ((hba->saved_err & UIC_ERROR) && |
| 5002 | (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR))) |
| 5003 | goto out; |
| 5004 | |
| 5005 | /* |
| 5006 | * As DL NAC is the only error received so far, send out NOP |
| 5007 | * command to confirm if link is still active or not. |
| 5008 | * - If we don't get any response then do error recovery. |
| 5009 | * - If we get response then clear the DL NAC error bit. |
| 5010 | */ |
| 5011 | |
| 5012 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5013 | err = ufshcd_verify_dev_init(hba); |
| 5014 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5015 | |
| 5016 | if (err) |
| 5017 | goto out; |
| 5018 | |
| 5019 | /* Link seems to be alive hence ignore the DL NAC errors */ |
| 5020 | if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR) |
| 5021 | hba->saved_err &= ~UIC_ERROR; |
| 5022 | /* clear NAC error */ |
| 5023 | hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR; |
| 5024 | if (!hba->saved_uic_err) { |
| 5025 | err_handling = false; |
| 5026 | goto out; |
| 5027 | } |
| 5028 | } |
| 5029 | out: |
| 5030 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5031 | return err_handling; |
| 5032 | } |
| 5033 | |
| 5034 | /** |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5035 | * ufshcd_err_handler - handle UFS errors that require s/w attention |
| 5036 | * @work: pointer to work structure |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5037 | */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5038 | static void ufshcd_err_handler(struct work_struct *work) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5039 | { |
| 5040 | struct ufs_hba *hba; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5041 | unsigned long flags; |
| 5042 | u32 err_xfer = 0; |
| 5043 | u32 err_tm = 0; |
| 5044 | int err = 0; |
| 5045 | int tag; |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5046 | bool needs_reset = false; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5047 | |
| 5048 | hba = container_of(work, struct ufs_hba, eh_work); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5049 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 5050 | pm_runtime_get_sync(hba->dev); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5051 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5052 | |
| 5053 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5054 | if (hba->ufshcd_state == UFSHCD_STATE_RESET) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5055 | goto out; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5056 | |
| 5057 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 5058 | ufshcd_set_eh_in_progress(hba); |
| 5059 | |
| 5060 | /* Complete requests that have door-bell cleared by h/w */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5061 | ufshcd_complete_requests(hba); |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5062 | |
| 5063 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) { |
| 5064 | bool ret; |
| 5065 | |
| 5066 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5067 | /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */ |
| 5068 | ret = ufshcd_quirk_dl_nac_errors(hba); |
| 5069 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5070 | if (!ret) |
| 5071 | goto skip_err_handling; |
| 5072 | } |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5073 | if ((hba->saved_err & INT_FATAL_ERRORS) || |
| 5074 | ((hba->saved_err & UIC_ERROR) && |
| 5075 | (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR | |
| 5076 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR | |
| 5077 | UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))) |
| 5078 | needs_reset = true; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5079 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5080 | /* |
| 5081 | * if host reset is required then skip clearing the pending |
| 5082 | * transfers forcefully because they will automatically get |
| 5083 | * cleared after link startup. |
| 5084 | */ |
| 5085 | if (needs_reset) |
| 5086 | goto skip_pending_xfer_clear; |
| 5087 | |
| 5088 | /* release lock as clear command might sleep */ |
| 5089 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5090 | /* Clear pending transfer requests */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5091 | for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) { |
| 5092 | if (ufshcd_clear_cmd(hba, tag)) { |
| 5093 | err_xfer = true; |
| 5094 | goto lock_skip_pending_xfer_clear; |
| 5095 | } |
| 5096 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5097 | |
| 5098 | /* Clear pending task management requests */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5099 | for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) { |
| 5100 | if (ufshcd_clear_tm_cmd(hba, tag)) { |
| 5101 | err_tm = true; |
| 5102 | goto lock_skip_pending_xfer_clear; |
| 5103 | } |
| 5104 | } |
| 5105 | |
| 5106 | lock_skip_pending_xfer_clear: |
| 5107 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5108 | |
| 5109 | /* Complete the requests that are cleared by s/w */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5110 | ufshcd_complete_requests(hba); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5111 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5112 | if (err_xfer || err_tm) |
| 5113 | needs_reset = true; |
| 5114 | |
| 5115 | skip_pending_xfer_clear: |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5116 | /* Fatal errors need reset */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5117 | if (needs_reset) { |
| 5118 | unsigned long max_doorbells = (1UL << hba->nutrs) - 1; |
| 5119 | |
| 5120 | /* |
| 5121 | * ufshcd_reset_and_restore() does the link reinitialization |
| 5122 | * which will need atleast one empty doorbell slot to send the |
| 5123 | * device management commands (NOP and query commands). |
| 5124 | * If there is no slot empty at this moment then free up last |
| 5125 | * slot forcefully. |
| 5126 | */ |
| 5127 | if (hba->outstanding_reqs == max_doorbells) |
| 5128 | __ufshcd_transfer_req_compl(hba, |
| 5129 | (1UL << (hba->nutrs - 1))); |
| 5130 | |
| 5131 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5132 | err = ufshcd_reset_and_restore(hba); |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5133 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5134 | if (err) { |
| 5135 | dev_err(hba->dev, "%s: reset and restore failed\n", |
| 5136 | __func__); |
| 5137 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 5138 | } |
| 5139 | /* |
| 5140 | * Inform scsi mid-layer that we did reset and allow to handle |
| 5141 | * Unit Attention properly. |
| 5142 | */ |
| 5143 | scsi_report_bus_reset(hba->host, 0); |
| 5144 | hba->saved_err = 0; |
| 5145 | hba->saved_uic_err = 0; |
| 5146 | } |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5147 | |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5148 | skip_err_handling: |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5149 | if (!needs_reset) { |
| 5150 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
| 5151 | if (hba->saved_err || hba->saved_uic_err) |
| 5152 | dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x", |
| 5153 | __func__, hba->saved_err, hba->saved_uic_err); |
| 5154 | } |
| 5155 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5156 | ufshcd_clear_eh_in_progress(hba); |
| 5157 | |
| 5158 | out: |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5159 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5160 | scsi_unblock_requests(hba->host); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5161 | ufshcd_release(hba); |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 5162 | pm_runtime_put_sync(hba->dev); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5163 | } |
| 5164 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5165 | static void ufshcd_update_uic_reg_hist(struct ufs_uic_err_reg_hist *reg_hist, |
| 5166 | u32 reg) |
| 5167 | { |
| 5168 | reg_hist->reg[reg_hist->pos] = reg; |
| 5169 | reg_hist->tstamp[reg_hist->pos] = ktime_get(); |
| 5170 | reg_hist->pos = (reg_hist->pos + 1) % UIC_ERR_REG_HIST_LENGTH; |
| 5171 | } |
| 5172 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5173 | /** |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5174 | * ufshcd_update_uic_error - check and set fatal UIC error flags. |
| 5175 | * @hba: per-adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5176 | */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5177 | static void ufshcd_update_uic_error(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5178 | { |
| 5179 | u32 reg; |
| 5180 | |
Dolev Raviv | fb7b45f | 2016-11-23 16:32:32 -0800 | [diff] [blame] | 5181 | /* PHY layer lane error */ |
| 5182 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER); |
| 5183 | /* Ignore LINERESET indication, as this is not an error */ |
| 5184 | if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) && |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5185 | (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK)) { |
Dolev Raviv | fb7b45f | 2016-11-23 16:32:32 -0800 | [diff] [blame] | 5186 | /* |
| 5187 | * To know whether this error is fatal or not, DB timeout |
| 5188 | * must be checked but this error is handled separately. |
| 5189 | */ |
| 5190 | dev_dbg(hba->dev, "%s: UIC Lane error reported\n", __func__); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5191 | ufshcd_update_uic_reg_hist(&hba->ufs_stats.pa_err, reg); |
| 5192 | } |
Dolev Raviv | fb7b45f | 2016-11-23 16:32:32 -0800 | [diff] [blame] | 5193 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5194 | /* PA_INIT_ERROR is fatal and needs UIC reset */ |
| 5195 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5196 | if (reg) |
| 5197 | ufshcd_update_uic_reg_hist(&hba->ufs_stats.dl_err, reg); |
| 5198 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5199 | if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT) |
| 5200 | hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5201 | else if (hba->dev_quirks & |
| 5202 | UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) { |
| 5203 | if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED) |
| 5204 | hba->uic_error |= |
| 5205 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR; |
| 5206 | else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT) |
| 5207 | hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR; |
| 5208 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5209 | |
| 5210 | /* UIC NL/TL/DME errors needs software retry */ |
| 5211 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5212 | if (reg) { |
| 5213 | ufshcd_update_uic_reg_hist(&hba->ufs_stats.nl_err, reg); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5214 | hba->uic_error |= UFSHCD_UIC_NL_ERROR; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5215 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5216 | |
| 5217 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5218 | if (reg) { |
| 5219 | ufshcd_update_uic_reg_hist(&hba->ufs_stats.tl_err, reg); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5220 | hba->uic_error |= UFSHCD_UIC_TL_ERROR; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5221 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5222 | |
| 5223 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5224 | if (reg) { |
| 5225 | ufshcd_update_uic_reg_hist(&hba->ufs_stats.dme_err, reg); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5226 | hba->uic_error |= UFSHCD_UIC_DME_ERROR; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5227 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5228 | |
| 5229 | dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n", |
| 5230 | __func__, hba->uic_error); |
| 5231 | } |
| 5232 | |
| 5233 | /** |
| 5234 | * ufshcd_check_errors - Check for errors that need s/w attention |
| 5235 | * @hba: per-adapter instance |
| 5236 | */ |
| 5237 | static void ufshcd_check_errors(struct ufs_hba *hba) |
| 5238 | { |
| 5239 | bool queue_eh_work = false; |
| 5240 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5241 | if (hba->errors & INT_FATAL_ERRORS) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5242 | queue_eh_work = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5243 | |
| 5244 | if (hba->errors & UIC_ERROR) { |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5245 | hba->uic_error = 0; |
| 5246 | ufshcd_update_uic_error(hba); |
| 5247 | if (hba->uic_error) |
| 5248 | queue_eh_work = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5249 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5250 | |
| 5251 | if (queue_eh_work) { |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5252 | /* |
| 5253 | * update the transfer error masks to sticky bits, let's do this |
| 5254 | * irrespective of current ufshcd_state. |
| 5255 | */ |
| 5256 | hba->saved_err |= hba->errors; |
| 5257 | hba->saved_uic_err |= hba->uic_error; |
| 5258 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5259 | /* handle fatal errors only when link is functional */ |
| 5260 | if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) { |
| 5261 | /* block commands from scsi mid-layer */ |
| 5262 | scsi_block_requests(hba->host); |
| 5263 | |
Zang Leigang | 141f816 | 2016-11-16 11:29:37 +0800 | [diff] [blame] | 5264 | hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED; |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 5265 | |
| 5266 | /* dump controller state before resetting */ |
| 5267 | if (hba->saved_err & (INT_FATAL_ERRORS | UIC_ERROR)) { |
| 5268 | bool pr_prdt = !!(hba->saved_err & |
| 5269 | SYSTEM_BUS_FATAL_ERROR); |
| 5270 | |
| 5271 | dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x\n", |
| 5272 | __func__, hba->saved_err, |
| 5273 | hba->saved_uic_err); |
| 5274 | |
| 5275 | ufshcd_print_host_regs(hba); |
| 5276 | ufshcd_print_pwr_info(hba); |
| 5277 | ufshcd_print_tmrs(hba, hba->outstanding_tasks); |
| 5278 | ufshcd_print_trs(hba, hba->outstanding_reqs, |
| 5279 | pr_prdt); |
| 5280 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5281 | schedule_work(&hba->eh_work); |
| 5282 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5283 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5284 | /* |
| 5285 | * if (!queue_eh_work) - |
| 5286 | * Other errors are either non-fatal where host recovers |
| 5287 | * itself without s/w intervention or errors that will be |
| 5288 | * handled by the SCSI core layer. |
| 5289 | */ |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5290 | } |
| 5291 | |
| 5292 | /** |
| 5293 | * ufshcd_tmc_handler - handle task management function completion |
| 5294 | * @hba: per adapter instance |
| 5295 | */ |
| 5296 | static void ufshcd_tmc_handler(struct ufs_hba *hba) |
| 5297 | { |
| 5298 | u32 tm_doorbell; |
| 5299 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 5300 | tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5301 | hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5302 | wake_up(&hba->tm_wq); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5303 | } |
| 5304 | |
| 5305 | /** |
| 5306 | * ufshcd_sl_intr - Interrupt service routine |
| 5307 | * @hba: per adapter instance |
| 5308 | * @intr_status: contains interrupts generated by the controller |
| 5309 | */ |
| 5310 | static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status) |
| 5311 | { |
| 5312 | hba->errors = UFSHCD_ERROR_MASK & intr_status; |
| 5313 | if (hba->errors) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5314 | ufshcd_check_errors(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5315 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 5316 | if (intr_status & UFSHCD_UIC_MASK) |
| 5317 | ufshcd_uic_cmd_compl(hba, intr_status); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5318 | |
| 5319 | if (intr_status & UTP_TASK_REQ_COMPL) |
| 5320 | ufshcd_tmc_handler(hba); |
| 5321 | |
| 5322 | if (intr_status & UTP_TRANSFER_REQ_COMPL) |
| 5323 | ufshcd_transfer_req_compl(hba); |
| 5324 | } |
| 5325 | |
| 5326 | /** |
| 5327 | * ufshcd_intr - Main interrupt service routine |
| 5328 | * @irq: irq number |
| 5329 | * @__hba: pointer to adapter instance |
| 5330 | * |
| 5331 | * Returns IRQ_HANDLED - If interrupt is valid |
| 5332 | * IRQ_NONE - If invalid interrupt |
| 5333 | */ |
| 5334 | static irqreturn_t ufshcd_intr(int irq, void *__hba) |
| 5335 | { |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 5336 | u32 intr_status, enabled_intr_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5337 | irqreturn_t retval = IRQ_NONE; |
| 5338 | struct ufs_hba *hba = __hba; |
| 5339 | |
| 5340 | spin_lock(hba->host->host_lock); |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 5341 | intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 5342 | enabled_intr_status = |
| 5343 | intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5344 | |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 5345 | if (intr_status) |
Seungwon Jeon | 261ea45 | 2013-06-26 22:39:28 +0530 | [diff] [blame] | 5346 | ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 5347 | |
| 5348 | if (enabled_intr_status) { |
| 5349 | ufshcd_sl_intr(hba, enabled_intr_status); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5350 | retval = IRQ_HANDLED; |
| 5351 | } |
| 5352 | spin_unlock(hba->host->host_lock); |
| 5353 | return retval; |
| 5354 | } |
| 5355 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5356 | static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag) |
| 5357 | { |
| 5358 | int err = 0; |
| 5359 | u32 mask = 1 << tag; |
| 5360 | unsigned long flags; |
| 5361 | |
| 5362 | if (!test_bit(tag, &hba->outstanding_tasks)) |
| 5363 | goto out; |
| 5364 | |
| 5365 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5366 | ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR); |
| 5367 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5368 | |
| 5369 | /* poll for max. 1 sec to clear door bell register by h/w */ |
| 5370 | err = ufshcd_wait_for_register(hba, |
| 5371 | REG_UTP_TASK_REQ_DOOR_BELL, |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 5372 | mask, 0, 1000, 1000, true); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5373 | out: |
| 5374 | return err; |
| 5375 | } |
| 5376 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5377 | /** |
| 5378 | * ufshcd_issue_tm_cmd - issues task management commands to controller |
| 5379 | * @hba: per adapter instance |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5380 | * @lun_id: LUN ID to which TM command is sent |
| 5381 | * @task_id: task ID to which the TM command is applicable |
| 5382 | * @tm_function: task management function opcode |
| 5383 | * @tm_response: task management service response return value |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5384 | * |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5385 | * Returns non-zero value on error, zero on success. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5386 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5387 | static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id, |
| 5388 | u8 tm_function, u8 *tm_response) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5389 | { |
| 5390 | struct utp_task_req_desc *task_req_descp; |
| 5391 | struct utp_upiu_task_req *task_req_upiup; |
| 5392 | struct Scsi_Host *host; |
| 5393 | unsigned long flags; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5394 | int free_slot; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5395 | int err; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5396 | int task_tag; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5397 | |
| 5398 | host = hba->host; |
| 5399 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5400 | /* |
| 5401 | * Get free slot, sleep if slots are unavailable. |
| 5402 | * Even though we use wait_event() which sleeps indefinitely, |
| 5403 | * the maximum wait time is bounded by %TM_CMD_TIMEOUT. |
| 5404 | */ |
| 5405 | 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] | 5406 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5407 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5408 | spin_lock_irqsave(host->host_lock, flags); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5409 | task_req_descp = hba->utmrdl_base_addr; |
| 5410 | task_req_descp += free_slot; |
| 5411 | |
| 5412 | /* Configure task request descriptor */ |
| 5413 | task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD); |
| 5414 | task_req_descp->header.dword_2 = |
| 5415 | cpu_to_le32(OCS_INVALID_COMMAND_STATUS); |
| 5416 | |
| 5417 | /* Configure task request UPIU */ |
| 5418 | task_req_upiup = |
| 5419 | (struct utp_upiu_task_req *) task_req_descp->task_req_upiu; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5420 | task_tag = hba->nutrs + free_slot; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5421 | task_req_upiup->header.dword_0 = |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 5422 | UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0, |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5423 | lun_id, task_tag); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5424 | task_req_upiup->header.dword_1 = |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 5425 | UPIU_HEADER_DWORD(0, tm_function, 0, 0); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 5426 | /* |
| 5427 | * The host shall provide the same value for LUN field in the basic |
| 5428 | * header and for Input Parameter. |
| 5429 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5430 | task_req_upiup->input_param1 = cpu_to_be32(lun_id); |
| 5431 | task_req_upiup->input_param2 = cpu_to_be32(task_id); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5432 | |
Kiwoong Kim | d2877be | 2016-11-10 21:16:15 +0900 | [diff] [blame] | 5433 | ufshcd_vops_setup_task_mgmt(hba, free_slot, tm_function); |
| 5434 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5435 | /* send command to the controller */ |
| 5436 | __set_bit(free_slot, &hba->outstanding_tasks); |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 5437 | |
| 5438 | /* Make sure descriptors are ready before ringing the task doorbell */ |
| 5439 | wmb(); |
| 5440 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 5441 | ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL); |
Gilad Broner | ad1a1b9 | 2016-10-17 17:09:36 -0700 | [diff] [blame] | 5442 | /* Make sure that doorbell is committed immediately */ |
| 5443 | wmb(); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5444 | |
| 5445 | spin_unlock_irqrestore(host->host_lock, flags); |
| 5446 | |
| 5447 | /* wait until the task management command is completed */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5448 | err = wait_event_timeout(hba->tm_wq, |
| 5449 | test_bit(free_slot, &hba->tm_condition), |
| 5450 | msecs_to_jiffies(TM_CMD_TIMEOUT)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5451 | if (!err) { |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5452 | dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n", |
| 5453 | __func__, tm_function); |
| 5454 | if (ufshcd_clear_tm_cmd(hba, free_slot)) |
| 5455 | dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n", |
| 5456 | __func__, free_slot); |
| 5457 | err = -ETIMEDOUT; |
| 5458 | } else { |
| 5459 | err = ufshcd_task_req_compl(hba, free_slot, tm_response); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5460 | } |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5461 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5462 | clear_bit(free_slot, &hba->tm_condition); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5463 | ufshcd_put_tm_slot(hba, free_slot); |
| 5464 | wake_up(&hba->tm_tag_wq); |
| 5465 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5466 | ufshcd_release(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5467 | return err; |
| 5468 | } |
| 5469 | |
| 5470 | /** |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5471 | * ufshcd_eh_device_reset_handler - device reset handler registered to |
| 5472 | * scsi layer. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5473 | * @cmd: SCSI command pointer |
| 5474 | * |
| 5475 | * Returns SUCCESS/FAILED |
| 5476 | */ |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5477 | static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5478 | { |
| 5479 | struct Scsi_Host *host; |
| 5480 | struct ufs_hba *hba; |
| 5481 | unsigned int tag; |
| 5482 | u32 pos; |
| 5483 | int err; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5484 | u8 resp = 0xF; |
| 5485 | struct ufshcd_lrb *lrbp; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5486 | unsigned long flags; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5487 | |
| 5488 | host = cmd->device->host; |
| 5489 | hba = shost_priv(host); |
| 5490 | tag = cmd->request->tag; |
| 5491 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5492 | lrbp = &hba->lrb[tag]; |
| 5493 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp); |
| 5494 | if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5495 | if (!err) |
| 5496 | err = resp; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5497 | goto out; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5498 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5499 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5500 | /* clear the commands that were pending for corresponding LUN */ |
| 5501 | for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) { |
| 5502 | if (hba->lrb[pos].lun == lrbp->lun) { |
| 5503 | err = ufshcd_clear_cmd(hba, pos); |
| 5504 | if (err) |
| 5505 | break; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5506 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5507 | } |
| 5508 | spin_lock_irqsave(host->host_lock, flags); |
| 5509 | ufshcd_transfer_req_compl(hba); |
| 5510 | spin_unlock_irqrestore(host->host_lock, flags); |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 5511 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5512 | out: |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 5513 | hba->req_abort_count = 0; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5514 | if (!err) { |
| 5515 | err = SUCCESS; |
| 5516 | } else { |
| 5517 | dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); |
| 5518 | err = FAILED; |
| 5519 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5520 | return err; |
| 5521 | } |
| 5522 | |
Gilad Broner | e0b299e | 2017-02-03 16:56:40 -0800 | [diff] [blame] | 5523 | static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap) |
| 5524 | { |
| 5525 | struct ufshcd_lrb *lrbp; |
| 5526 | int tag; |
| 5527 | |
| 5528 | for_each_set_bit(tag, &bitmap, hba->nutrs) { |
| 5529 | lrbp = &hba->lrb[tag]; |
| 5530 | lrbp->req_abort_skip = true; |
| 5531 | } |
| 5532 | } |
| 5533 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5534 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5535 | * ufshcd_abort - abort a specific command |
| 5536 | * @cmd: SCSI command pointer |
| 5537 | * |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 5538 | * Abort the pending command in device by sending UFS_ABORT_TASK task management |
| 5539 | * command, and in host controller by clearing the door-bell register. There can |
| 5540 | * be race between controller sending the command to the device while abort is |
| 5541 | * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is |
| 5542 | * really issued and then try to abort it. |
| 5543 | * |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5544 | * Returns SUCCESS/FAILED |
| 5545 | */ |
| 5546 | static int ufshcd_abort(struct scsi_cmnd *cmd) |
| 5547 | { |
| 5548 | struct Scsi_Host *host; |
| 5549 | struct ufs_hba *hba; |
| 5550 | unsigned long flags; |
| 5551 | unsigned int tag; |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 5552 | int err = 0; |
| 5553 | int poll_cnt; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5554 | u8 resp = 0xF; |
| 5555 | struct ufshcd_lrb *lrbp; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5556 | u32 reg; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5557 | |
| 5558 | host = cmd->device->host; |
| 5559 | hba = shost_priv(host); |
| 5560 | tag = cmd->request->tag; |
Dolev Raviv | e7d3825 | 2016-12-22 18:40:07 -0800 | [diff] [blame] | 5561 | lrbp = &hba->lrb[tag]; |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 5562 | if (!ufshcd_valid_tag(hba, tag)) { |
| 5563 | dev_err(hba->dev, |
| 5564 | "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p", |
| 5565 | __func__, tag, cmd, cmd->request); |
| 5566 | BUG(); |
| 5567 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5568 | |
Dolev Raviv | e7d3825 | 2016-12-22 18:40:07 -0800 | [diff] [blame] | 5569 | /* |
| 5570 | * Task abort to the device W-LUN is illegal. When this command |
| 5571 | * will fail, due to spec violation, scsi err handling next step |
| 5572 | * will be to send LU reset which, again, is a spec violation. |
| 5573 | * To avoid these unnecessary/illegal step we skip to the last error |
| 5574 | * handling stage: reset and restore. |
| 5575 | */ |
| 5576 | if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN) |
| 5577 | return ufshcd_eh_host_reset_handler(cmd); |
| 5578 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5579 | ufshcd_hold(hba, false); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5580 | reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 5581 | /* If command is already aborted/completed, return SUCCESS */ |
| 5582 | if (!(test_bit(tag, &hba->outstanding_reqs))) { |
| 5583 | dev_err(hba->dev, |
| 5584 | "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n", |
| 5585 | __func__, tag, hba->outstanding_reqs, reg); |
| 5586 | goto out; |
| 5587 | } |
| 5588 | |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5589 | if (!(reg & (1 << tag))) { |
| 5590 | dev_err(hba->dev, |
| 5591 | "%s: cmd was completed, but without a notifying intr, tag = %d", |
| 5592 | __func__, tag); |
| 5593 | } |
| 5594 | |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 5595 | /* Print Transfer Request of aborted task */ |
| 5596 | dev_err(hba->dev, "%s: Device abort task at tag %d\n", __func__, tag); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 5597 | |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 5598 | /* |
| 5599 | * Print detailed info about aborted request. |
| 5600 | * As more than one request might get aborted at the same time, |
| 5601 | * print full information only for the first aborted request in order |
| 5602 | * to reduce repeated printouts. For other aborted requests only print |
| 5603 | * basic details. |
| 5604 | */ |
| 5605 | scsi_print_command(hba->lrb[tag].cmd); |
| 5606 | if (!hba->req_abort_count) { |
| 5607 | ufshcd_print_host_regs(hba); |
Gilad Broner | 6ba6558 | 2017-02-03 16:57:28 -0800 | [diff] [blame] | 5608 | ufshcd_print_host_state(hba); |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 5609 | ufshcd_print_pwr_info(hba); |
| 5610 | ufshcd_print_trs(hba, 1 << tag, true); |
| 5611 | } else { |
| 5612 | ufshcd_print_trs(hba, 1 << tag, false); |
| 5613 | } |
| 5614 | hba->req_abort_count++; |
Gilad Broner | e0b299e | 2017-02-03 16:56:40 -0800 | [diff] [blame] | 5615 | |
| 5616 | /* Skip task abort in case previous aborts failed and report failure */ |
| 5617 | if (lrbp->req_abort_skip) { |
| 5618 | err = -EIO; |
| 5619 | goto out; |
| 5620 | } |
| 5621 | |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 5622 | for (poll_cnt = 100; poll_cnt; poll_cnt--) { |
| 5623 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, |
| 5624 | UFS_QUERY_TASK, &resp); |
| 5625 | if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) { |
| 5626 | /* cmd pending in the device */ |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5627 | dev_err(hba->dev, "%s: cmd pending in the device. tag = %d\n", |
| 5628 | __func__, tag); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 5629 | break; |
| 5630 | } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 5631 | /* |
| 5632 | * cmd not pending in the device, check if it is |
| 5633 | * in transition. |
| 5634 | */ |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5635 | dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.\n", |
| 5636 | __func__, tag); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 5637 | reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 5638 | if (reg & (1 << tag)) { |
| 5639 | /* sleep for max. 200us to stabilize */ |
| 5640 | usleep_range(100, 200); |
| 5641 | continue; |
| 5642 | } |
| 5643 | /* command completed already */ |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5644 | dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.\n", |
| 5645 | __func__, tag); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 5646 | goto out; |
| 5647 | } else { |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5648 | dev_err(hba->dev, |
| 5649 | "%s: no response from device. tag = %d, err %d\n", |
| 5650 | __func__, tag, err); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 5651 | if (!err) |
| 5652 | err = resp; /* service response error */ |
| 5653 | goto out; |
| 5654 | } |
| 5655 | } |
| 5656 | |
| 5657 | if (!poll_cnt) { |
| 5658 | err = -EBUSY; |
| 5659 | goto out; |
| 5660 | } |
| 5661 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5662 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, |
| 5663 | UFS_ABORT_TASK, &resp); |
| 5664 | if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5665 | if (!err) { |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 5666 | err = resp; /* service response error */ |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5667 | dev_err(hba->dev, "%s: issued. tag = %d, err %d\n", |
| 5668 | __func__, tag, err); |
| 5669 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5670 | goto out; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5671 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5672 | |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 5673 | err = ufshcd_clear_cmd(hba, tag); |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5674 | if (err) { |
| 5675 | dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d\n", |
| 5676 | __func__, tag, err); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 5677 | goto out; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 5678 | } |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 5679 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5680 | scsi_dma_unmap(cmd); |
| 5681 | |
| 5682 | spin_lock_irqsave(host->host_lock, flags); |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 5683 | ufshcd_outstanding_req_clear(hba, tag); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5684 | hba->lrb[tag].cmd = NULL; |
| 5685 | spin_unlock_irqrestore(host->host_lock, flags); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 5686 | |
| 5687 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 5688 | wake_up(&hba->dev_cmd.tag_wq); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5689 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5690 | out: |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 5691 | if (!err) { |
| 5692 | err = SUCCESS; |
| 5693 | } else { |
| 5694 | dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); |
Gilad Broner | e0b299e | 2017-02-03 16:56:40 -0800 | [diff] [blame] | 5695 | ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 5696 | err = FAILED; |
| 5697 | } |
| 5698 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5699 | /* |
| 5700 | * This ufshcd_release() corresponds to the original scsi cmd that got |
| 5701 | * aborted here (as we won't get any IRQ for it). |
| 5702 | */ |
| 5703 | ufshcd_release(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5704 | return err; |
| 5705 | } |
| 5706 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5707 | /** |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5708 | * ufshcd_host_reset_and_restore - reset and restore host controller |
| 5709 | * @hba: per-adapter instance |
| 5710 | * |
| 5711 | * Note that host controller reset may issue DME_RESET to |
| 5712 | * local and remote (device) Uni-Pro stack and the attributes |
| 5713 | * are reset to default state. |
| 5714 | * |
| 5715 | * Returns zero on success, non-zero on failure |
| 5716 | */ |
| 5717 | static int ufshcd_host_reset_and_restore(struct ufs_hba *hba) |
| 5718 | { |
| 5719 | int err; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5720 | unsigned long flags; |
| 5721 | |
| 5722 | /* Reset the host controller */ |
| 5723 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 5724 | ufshcd_hba_stop(hba, false); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5725 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5726 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 5727 | /* scale up clocks to max frequency before full reinitialization */ |
| 5728 | ufshcd_scale_clks(hba, true); |
| 5729 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5730 | err = ufshcd_hba_enable(hba); |
| 5731 | if (err) |
| 5732 | goto out; |
| 5733 | |
| 5734 | /* Establish the link again and restore the device */ |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 5735 | err = ufshcd_probe_hba(hba); |
| 5736 | |
| 5737 | if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL)) |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5738 | err = -EIO; |
| 5739 | out: |
| 5740 | if (err) |
| 5741 | dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err); |
| 5742 | |
| 5743 | return err; |
| 5744 | } |
| 5745 | |
| 5746 | /** |
| 5747 | * ufshcd_reset_and_restore - reset and re-initialize host/device |
| 5748 | * @hba: per-adapter instance |
| 5749 | * |
| 5750 | * Reset and recover device, host and re-establish link. This |
| 5751 | * is helpful to recover the communication in fatal error conditions. |
| 5752 | * |
| 5753 | * Returns zero on success, non-zero on failure |
| 5754 | */ |
| 5755 | static int ufshcd_reset_and_restore(struct ufs_hba *hba) |
| 5756 | { |
| 5757 | int err = 0; |
| 5758 | unsigned long flags; |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 5759 | int retries = MAX_HOST_RESET_RETRIES; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5760 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 5761 | do { |
| 5762 | err = ufshcd_host_reset_and_restore(hba); |
| 5763 | } while (err && --retries); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5764 | |
| 5765 | /* |
| 5766 | * After reset the door-bell might be cleared, complete |
| 5767 | * outstanding requests in s/w here. |
| 5768 | */ |
| 5769 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5770 | ufshcd_transfer_req_compl(hba); |
| 5771 | ufshcd_tmc_handler(hba); |
| 5772 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5773 | |
| 5774 | return err; |
| 5775 | } |
| 5776 | |
| 5777 | /** |
| 5778 | * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer |
| 5779 | * @cmd - SCSI command pointer |
| 5780 | * |
| 5781 | * Returns SUCCESS/FAILED |
| 5782 | */ |
| 5783 | static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd) |
| 5784 | { |
| 5785 | int err; |
| 5786 | unsigned long flags; |
| 5787 | struct ufs_hba *hba; |
| 5788 | |
| 5789 | hba = shost_priv(cmd->device->host); |
| 5790 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5791 | ufshcd_hold(hba, false); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5792 | /* |
| 5793 | * Check if there is any race with fatal error handling. |
| 5794 | * If so, wait for it to complete. Even though fatal error |
| 5795 | * handling does reset and restore in some cases, don't assume |
| 5796 | * anything out of it. We are just avoiding race here. |
| 5797 | */ |
| 5798 | do { |
| 5799 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5800 | if (!(work_pending(&hba->eh_work) || |
Zang Leigang | 8dc0da7 | 2017-06-24 19:14:32 +0800 | [diff] [blame] | 5801 | hba->ufshcd_state == UFSHCD_STATE_RESET || |
| 5802 | hba->ufshcd_state == UFSHCD_STATE_EH_SCHEDULED)) |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5803 | break; |
| 5804 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5805 | dev_dbg(hba->dev, "%s: reset in progress\n", __func__); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5806 | flush_work(&hba->eh_work); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5807 | } while (1); |
| 5808 | |
| 5809 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 5810 | ufshcd_set_eh_in_progress(hba); |
| 5811 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5812 | |
| 5813 | err = ufshcd_reset_and_restore(hba); |
| 5814 | |
| 5815 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5816 | if (!err) { |
| 5817 | err = SUCCESS; |
| 5818 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
| 5819 | } else { |
| 5820 | err = FAILED; |
| 5821 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 5822 | } |
| 5823 | ufshcd_clear_eh_in_progress(hba); |
| 5824 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5825 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 5826 | ufshcd_release(hba); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5827 | return err; |
| 5828 | } |
| 5829 | |
| 5830 | /** |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 5831 | * ufshcd_get_max_icc_level - calculate the ICC level |
| 5832 | * @sup_curr_uA: max. current supported by the regulator |
| 5833 | * @start_scan: row at the desc table to start scan from |
| 5834 | * @buff: power descriptor buffer |
| 5835 | * |
| 5836 | * Returns calculated max ICC level for specific regulator |
| 5837 | */ |
| 5838 | static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff) |
| 5839 | { |
| 5840 | int i; |
| 5841 | int curr_uA; |
| 5842 | u16 data; |
| 5843 | u16 unit; |
| 5844 | |
| 5845 | for (i = start_scan; i >= 0; i--) { |
Tomas Winkler | d79713f | 2017-01-05 10:45:11 +0200 | [diff] [blame] | 5846 | data = be16_to_cpup((__be16 *)&buff[2 * i]); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 5847 | unit = (data & ATTR_ICC_LVL_UNIT_MASK) >> |
| 5848 | ATTR_ICC_LVL_UNIT_OFFSET; |
| 5849 | curr_uA = data & ATTR_ICC_LVL_VALUE_MASK; |
| 5850 | switch (unit) { |
| 5851 | case UFSHCD_NANO_AMP: |
| 5852 | curr_uA = curr_uA / 1000; |
| 5853 | break; |
| 5854 | case UFSHCD_MILI_AMP: |
| 5855 | curr_uA = curr_uA * 1000; |
| 5856 | break; |
| 5857 | case UFSHCD_AMP: |
| 5858 | curr_uA = curr_uA * 1000 * 1000; |
| 5859 | break; |
| 5860 | case UFSHCD_MICRO_AMP: |
| 5861 | default: |
| 5862 | break; |
| 5863 | } |
| 5864 | if (sup_curr_uA >= curr_uA) |
| 5865 | break; |
| 5866 | } |
| 5867 | if (i < 0) { |
| 5868 | i = 0; |
| 5869 | pr_err("%s: Couldn't find valid icc_level = %d", __func__, i); |
| 5870 | } |
| 5871 | |
| 5872 | return (u32)i; |
| 5873 | } |
| 5874 | |
| 5875 | /** |
| 5876 | * ufshcd_calc_icc_level - calculate the max ICC level |
| 5877 | * In case regulators are not initialized we'll return 0 |
| 5878 | * @hba: per-adapter instance |
| 5879 | * @desc_buf: power descriptor buffer to extract ICC levels from. |
| 5880 | * @len: length of desc_buff |
| 5881 | * |
| 5882 | * Returns calculated ICC level |
| 5883 | */ |
| 5884 | static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba, |
| 5885 | u8 *desc_buf, int len) |
| 5886 | { |
| 5887 | u32 icc_level = 0; |
| 5888 | |
| 5889 | if (!hba->vreg_info.vcc || !hba->vreg_info.vccq || |
| 5890 | !hba->vreg_info.vccq2) { |
| 5891 | dev_err(hba->dev, |
| 5892 | "%s: Regulator capability was not set, actvIccLevel=%d", |
| 5893 | __func__, icc_level); |
| 5894 | goto out; |
| 5895 | } |
| 5896 | |
| 5897 | if (hba->vreg_info.vcc) |
| 5898 | icc_level = ufshcd_get_max_icc_level( |
| 5899 | hba->vreg_info.vcc->max_uA, |
| 5900 | POWER_DESC_MAX_ACTV_ICC_LVLS - 1, |
| 5901 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]); |
| 5902 | |
| 5903 | if (hba->vreg_info.vccq) |
| 5904 | icc_level = ufshcd_get_max_icc_level( |
| 5905 | hba->vreg_info.vccq->max_uA, |
| 5906 | icc_level, |
| 5907 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]); |
| 5908 | |
| 5909 | if (hba->vreg_info.vccq2) |
| 5910 | icc_level = ufshcd_get_max_icc_level( |
| 5911 | hba->vreg_info.vccq2->max_uA, |
| 5912 | icc_level, |
| 5913 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]); |
| 5914 | out: |
| 5915 | return icc_level; |
| 5916 | } |
| 5917 | |
| 5918 | static void ufshcd_init_icc_levels(struct ufs_hba *hba) |
| 5919 | { |
| 5920 | int ret; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 5921 | int buff_len = hba->desc_size.pwr_desc; |
| 5922 | u8 desc_buf[hba->desc_size.pwr_desc]; |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 5923 | |
| 5924 | ret = ufshcd_read_power_desc(hba, desc_buf, buff_len); |
| 5925 | if (ret) { |
| 5926 | dev_err(hba->dev, |
| 5927 | "%s: Failed reading power descriptor.len = %d ret = %d", |
| 5928 | __func__, buff_len, ret); |
| 5929 | return; |
| 5930 | } |
| 5931 | |
| 5932 | hba->init_prefetch_data.icc_level = |
| 5933 | ufshcd_find_max_sup_active_icc_level(hba, |
| 5934 | desc_buf, buff_len); |
| 5935 | dev_dbg(hba->dev, "%s: setting icc_level 0x%x", |
| 5936 | __func__, hba->init_prefetch_data.icc_level); |
| 5937 | |
Szymon Mielczarek | dbd34a6 | 2017-03-29 08:19:21 +0200 | [diff] [blame] | 5938 | ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
| 5939 | QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, |
| 5940 | &hba->init_prefetch_data.icc_level); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 5941 | |
| 5942 | if (ret) |
| 5943 | dev_err(hba->dev, |
| 5944 | "%s: Failed configuring bActiveICCLevel = %d ret = %d", |
| 5945 | __func__, hba->init_prefetch_data.icc_level , ret); |
| 5946 | |
| 5947 | } |
| 5948 | |
| 5949 | /** |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 5950 | * ufshcd_scsi_add_wlus - Adds required W-LUs |
| 5951 | * @hba: per-adapter instance |
| 5952 | * |
| 5953 | * UFS device specification requires the UFS devices to support 4 well known |
| 5954 | * logical units: |
| 5955 | * "REPORT_LUNS" (address: 01h) |
| 5956 | * "UFS Device" (address: 50h) |
| 5957 | * "RPMB" (address: 44h) |
| 5958 | * "BOOT" (address: 30h) |
| 5959 | * UFS device's power management needs to be controlled by "POWER CONDITION" |
| 5960 | * field of SSU (START STOP UNIT) command. But this "power condition" field |
| 5961 | * will take effect only when its sent to "UFS device" well known logical unit |
| 5962 | * hence we require the scsi_device instance to represent this logical unit in |
| 5963 | * order for the UFS host driver to send the SSU command for power management. |
| 5964 | |
| 5965 | * We also require the scsi_device instance for "RPMB" (Replay Protected Memory |
| 5966 | * Block) LU so user space process can control this LU. User space may also |
| 5967 | * want to have access to BOOT LU. |
| 5968 | |
| 5969 | * This function adds scsi device instances for each of all well known LUs |
| 5970 | * (except "REPORT LUNS" LU). |
| 5971 | * |
| 5972 | * Returns zero on success (all required W-LUs are added successfully), |
| 5973 | * non-zero error value on failure (if failed to add any of the required W-LU). |
| 5974 | */ |
| 5975 | static int ufshcd_scsi_add_wlus(struct ufs_hba *hba) |
| 5976 | { |
| 5977 | int ret = 0; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 5978 | struct scsi_device *sdev_rpmb; |
| 5979 | struct scsi_device *sdev_boot; |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 5980 | |
| 5981 | hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0, |
| 5982 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL); |
| 5983 | if (IS_ERR(hba->sdev_ufs_device)) { |
| 5984 | ret = PTR_ERR(hba->sdev_ufs_device); |
| 5985 | hba->sdev_ufs_device = NULL; |
| 5986 | goto out; |
| 5987 | } |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 5988 | scsi_device_put(hba->sdev_ufs_device); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 5989 | |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 5990 | sdev_rpmb = __scsi_add_device(hba->host, 0, 0, |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 5991 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL); |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 5992 | if (IS_ERR(sdev_rpmb)) { |
| 5993 | ret = PTR_ERR(sdev_rpmb); |
Huanlin Ke | 3d21fbd | 2017-09-22 18:31:47 +0800 | [diff] [blame] | 5994 | goto remove_sdev_ufs_device; |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 5995 | } |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 5996 | scsi_device_put(sdev_rpmb); |
Huanlin Ke | 3d21fbd | 2017-09-22 18:31:47 +0800 | [diff] [blame] | 5997 | |
| 5998 | sdev_boot = __scsi_add_device(hba->host, 0, 0, |
| 5999 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL); |
| 6000 | if (IS_ERR(sdev_boot)) |
| 6001 | dev_err(hba->dev, "%s: BOOT WLUN not found\n", __func__); |
| 6002 | else |
| 6003 | scsi_device_put(sdev_boot); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6004 | goto out; |
| 6005 | |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6006 | remove_sdev_ufs_device: |
| 6007 | scsi_remove_device(hba->sdev_ufs_device); |
| 6008 | out: |
| 6009 | return ret; |
| 6010 | } |
| 6011 | |
Tomas Winkler | 93fdd5a | 2017-01-05 10:45:12 +0200 | [diff] [blame] | 6012 | static int ufs_get_device_desc(struct ufs_hba *hba, |
| 6013 | struct ufs_dev_desc *dev_desc) |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6014 | { |
| 6015 | int err; |
| 6016 | u8 model_index; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 6017 | u8 str_desc_buf[QUERY_DESC_MAX_SIZE + 1] = {0}; |
| 6018 | u8 desc_buf[hba->desc_size.dev_desc]; |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6019 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 6020 | err = ufshcd_read_device_desc(hba, desc_buf, hba->desc_size.dev_desc); |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6021 | if (err) { |
| 6022 | dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n", |
| 6023 | __func__, err); |
| 6024 | goto out; |
| 6025 | } |
| 6026 | |
| 6027 | /* |
| 6028 | * getting vendor (manufacturerID) and Bank Index in big endian |
| 6029 | * format |
| 6030 | */ |
Tomas Winkler | 93fdd5a | 2017-01-05 10:45:12 +0200 | [diff] [blame] | 6031 | dev_desc->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 | |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6032 | desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1]; |
| 6033 | |
| 6034 | model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME]; |
| 6035 | |
| 6036 | err = ufshcd_read_string_desc(hba, model_index, str_desc_buf, |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 6037 | QUERY_DESC_MAX_SIZE, ASCII_STD); |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6038 | if (err) { |
| 6039 | dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n", |
| 6040 | __func__, err); |
| 6041 | goto out; |
| 6042 | } |
| 6043 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 6044 | str_desc_buf[QUERY_DESC_MAX_SIZE] = '\0'; |
Tomas Winkler | 93fdd5a | 2017-01-05 10:45:12 +0200 | [diff] [blame] | 6045 | strlcpy(dev_desc->model, (str_desc_buf + QUERY_DESC_HDR_SIZE), |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6046 | min_t(u8, str_desc_buf[QUERY_DESC_LENGTH_OFFSET], |
| 6047 | MAX_MODEL_LEN)); |
| 6048 | |
| 6049 | /* Null terminate the model string */ |
Tomas Winkler | 93fdd5a | 2017-01-05 10:45:12 +0200 | [diff] [blame] | 6050 | dev_desc->model[MAX_MODEL_LEN] = '\0'; |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6051 | |
| 6052 | out: |
| 6053 | return err; |
| 6054 | } |
| 6055 | |
Tomas Winkler | 93fdd5a | 2017-01-05 10:45:12 +0200 | [diff] [blame] | 6056 | static void ufs_fixup_device_setup(struct ufs_hba *hba, |
| 6057 | struct ufs_dev_desc *dev_desc) |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6058 | { |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6059 | struct ufs_dev_fix *f; |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6060 | |
| 6061 | for (f = ufs_fixups; f->quirk; f++) { |
Tomas Winkler | 93fdd5a | 2017-01-05 10:45:12 +0200 | [diff] [blame] | 6062 | if ((f->card.wmanufacturerid == dev_desc->wmanufacturerid || |
| 6063 | f->card.wmanufacturerid == UFS_ANY_VENDOR) && |
| 6064 | (STR_PRFX_EQUAL(f->card.model, dev_desc->model) || |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6065 | !strcmp(f->card.model, UFS_ANY_MODEL))) |
| 6066 | hba->dev_quirks |= f->quirk; |
| 6067 | } |
| 6068 | } |
| 6069 | |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6070 | /** |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 6071 | * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro |
| 6072 | * @hba: per-adapter instance |
| 6073 | * |
| 6074 | * PA_TActivate parameter can be tuned manually if UniPro version is less than |
| 6075 | * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's |
| 6076 | * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce |
| 6077 | * the hibern8 exit latency. |
| 6078 | * |
| 6079 | * Returns zero on success, non-zero error value on failure. |
| 6080 | */ |
| 6081 | static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba) |
| 6082 | { |
| 6083 | int ret = 0; |
| 6084 | u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate; |
| 6085 | |
| 6086 | ret = ufshcd_dme_peer_get(hba, |
| 6087 | UIC_ARG_MIB_SEL( |
| 6088 | RX_MIN_ACTIVATETIME_CAPABILITY, |
| 6089 | UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), |
| 6090 | &peer_rx_min_activatetime); |
| 6091 | if (ret) |
| 6092 | goto out; |
| 6093 | |
| 6094 | /* make sure proper unit conversion is applied */ |
| 6095 | tuned_pa_tactivate = |
| 6096 | ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US) |
| 6097 | / PA_TACTIVATE_TIME_UNIT_US); |
| 6098 | ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 6099 | tuned_pa_tactivate); |
| 6100 | |
| 6101 | out: |
| 6102 | return ret; |
| 6103 | } |
| 6104 | |
| 6105 | /** |
| 6106 | * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro |
| 6107 | * @hba: per-adapter instance |
| 6108 | * |
| 6109 | * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than |
| 6110 | * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's |
| 6111 | * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY. |
| 6112 | * This optimal value can help reduce the hibern8 exit latency. |
| 6113 | * |
| 6114 | * Returns zero on success, non-zero error value on failure. |
| 6115 | */ |
| 6116 | static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba) |
| 6117 | { |
| 6118 | int ret = 0; |
| 6119 | u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0; |
| 6120 | u32 max_hibern8_time, tuned_pa_hibern8time; |
| 6121 | |
| 6122 | ret = ufshcd_dme_get(hba, |
| 6123 | UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY, |
| 6124 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), |
| 6125 | &local_tx_hibern8_time_cap); |
| 6126 | if (ret) |
| 6127 | goto out; |
| 6128 | |
| 6129 | ret = ufshcd_dme_peer_get(hba, |
| 6130 | UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY, |
| 6131 | UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), |
| 6132 | &peer_rx_hibern8_time_cap); |
| 6133 | if (ret) |
| 6134 | goto out; |
| 6135 | |
| 6136 | max_hibern8_time = max(local_tx_hibern8_time_cap, |
| 6137 | peer_rx_hibern8_time_cap); |
| 6138 | /* make sure proper unit conversion is applied */ |
| 6139 | tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US) |
| 6140 | / PA_HIBERN8_TIME_UNIT_US); |
| 6141 | ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME), |
| 6142 | tuned_pa_hibern8time); |
| 6143 | out: |
| 6144 | return ret; |
| 6145 | } |
| 6146 | |
subhashj@codeaurora.org | c6a6db4 | 2016-11-23 16:32:08 -0800 | [diff] [blame] | 6147 | /** |
| 6148 | * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is |
| 6149 | * less than device PA_TACTIVATE time. |
| 6150 | * @hba: per-adapter instance |
| 6151 | * |
| 6152 | * Some UFS devices require host PA_TACTIVATE to be lower than device |
| 6153 | * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk |
| 6154 | * for such devices. |
| 6155 | * |
| 6156 | * Returns zero on success, non-zero error value on failure. |
| 6157 | */ |
| 6158 | static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba) |
| 6159 | { |
| 6160 | int ret = 0; |
| 6161 | u32 granularity, peer_granularity; |
| 6162 | u32 pa_tactivate, peer_pa_tactivate; |
| 6163 | u32 pa_tactivate_us, peer_pa_tactivate_us; |
| 6164 | u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100}; |
| 6165 | |
| 6166 | ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY), |
| 6167 | &granularity); |
| 6168 | if (ret) |
| 6169 | goto out; |
| 6170 | |
| 6171 | ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY), |
| 6172 | &peer_granularity); |
| 6173 | if (ret) |
| 6174 | goto out; |
| 6175 | |
| 6176 | if ((granularity < PA_GRANULARITY_MIN_VAL) || |
| 6177 | (granularity > PA_GRANULARITY_MAX_VAL)) { |
| 6178 | dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d", |
| 6179 | __func__, granularity); |
| 6180 | return -EINVAL; |
| 6181 | } |
| 6182 | |
| 6183 | if ((peer_granularity < PA_GRANULARITY_MIN_VAL) || |
| 6184 | (peer_granularity > PA_GRANULARITY_MAX_VAL)) { |
| 6185 | dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d", |
| 6186 | __func__, peer_granularity); |
| 6187 | return -EINVAL; |
| 6188 | } |
| 6189 | |
| 6190 | ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate); |
| 6191 | if (ret) |
| 6192 | goto out; |
| 6193 | |
| 6194 | ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 6195 | &peer_pa_tactivate); |
| 6196 | if (ret) |
| 6197 | goto out; |
| 6198 | |
| 6199 | pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1]; |
| 6200 | peer_pa_tactivate_us = peer_pa_tactivate * |
| 6201 | gran_to_us_table[peer_granularity - 1]; |
| 6202 | |
| 6203 | if (pa_tactivate_us > peer_pa_tactivate_us) { |
| 6204 | u32 new_peer_pa_tactivate; |
| 6205 | |
| 6206 | new_peer_pa_tactivate = pa_tactivate_us / |
| 6207 | gran_to_us_table[peer_granularity - 1]; |
| 6208 | new_peer_pa_tactivate++; |
| 6209 | ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 6210 | new_peer_pa_tactivate); |
| 6211 | } |
| 6212 | |
| 6213 | out: |
| 6214 | return ret; |
| 6215 | } |
| 6216 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 6217 | static void ufshcd_tune_unipro_params(struct ufs_hba *hba) |
| 6218 | { |
| 6219 | if (ufshcd_is_unipro_pa_params_tuning_req(hba)) { |
| 6220 | ufshcd_tune_pa_tactivate(hba); |
| 6221 | ufshcd_tune_pa_hibern8time(hba); |
| 6222 | } |
| 6223 | |
| 6224 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE) |
| 6225 | /* set 1ms timeout for PA_TACTIVATE */ |
| 6226 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10); |
subhashj@codeaurora.org | c6a6db4 | 2016-11-23 16:32:08 -0800 | [diff] [blame] | 6227 | |
| 6228 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE) |
| 6229 | ufshcd_quirk_tune_host_pa_tactivate(hba); |
Subhash Jadavani | 56d4a18 | 2016-12-05 19:25:32 -0800 | [diff] [blame] | 6230 | |
| 6231 | ufshcd_vops_apply_dev_quirks(hba); |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 6232 | } |
| 6233 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6234 | static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba) |
| 6235 | { |
| 6236 | int err_reg_hist_size = sizeof(struct ufs_uic_err_reg_hist); |
| 6237 | |
| 6238 | hba->ufs_stats.hibern8_exit_cnt = 0; |
| 6239 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); |
| 6240 | |
| 6241 | memset(&hba->ufs_stats.pa_err, 0, err_reg_hist_size); |
| 6242 | memset(&hba->ufs_stats.dl_err, 0, err_reg_hist_size); |
| 6243 | memset(&hba->ufs_stats.nl_err, 0, err_reg_hist_size); |
| 6244 | memset(&hba->ufs_stats.tl_err, 0, err_reg_hist_size); |
| 6245 | memset(&hba->ufs_stats.dme_err, 0, err_reg_hist_size); |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 6246 | |
| 6247 | hba->req_abort_count = 0; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6248 | } |
| 6249 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 6250 | static void ufshcd_init_desc_sizes(struct ufs_hba *hba) |
| 6251 | { |
| 6252 | int err; |
| 6253 | |
| 6254 | err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_DEVICE, 0, |
| 6255 | &hba->desc_size.dev_desc); |
| 6256 | if (err) |
| 6257 | hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE; |
| 6258 | |
| 6259 | err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_POWER, 0, |
| 6260 | &hba->desc_size.pwr_desc); |
| 6261 | if (err) |
| 6262 | hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE; |
| 6263 | |
| 6264 | err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_INTERCONNECT, 0, |
| 6265 | &hba->desc_size.interc_desc); |
| 6266 | if (err) |
| 6267 | hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE; |
| 6268 | |
| 6269 | err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_CONFIGURATION, 0, |
| 6270 | &hba->desc_size.conf_desc); |
| 6271 | if (err) |
| 6272 | hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE; |
| 6273 | |
| 6274 | err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_UNIT, 0, |
| 6275 | &hba->desc_size.unit_desc); |
| 6276 | if (err) |
| 6277 | hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE; |
| 6278 | |
| 6279 | err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_GEOMETRY, 0, |
| 6280 | &hba->desc_size.geom_desc); |
| 6281 | if (err) |
| 6282 | hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE; |
Stanislav Nijnikov | c648c2d | 2018-02-15 14:14:05 +0200 | [diff] [blame] | 6283 | err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_HEALTH, 0, |
| 6284 | &hba->desc_size.hlth_desc); |
| 6285 | if (err) |
| 6286 | hba->desc_size.hlth_desc = QUERY_DESC_HEALTH_DEF_SIZE; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 6287 | } |
| 6288 | |
| 6289 | static void ufshcd_def_desc_sizes(struct ufs_hba *hba) |
| 6290 | { |
| 6291 | hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE; |
| 6292 | hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE; |
| 6293 | hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE; |
| 6294 | hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE; |
| 6295 | hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE; |
| 6296 | hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE; |
Stanislav Nijnikov | c648c2d | 2018-02-15 14:14:05 +0200 | [diff] [blame] | 6297 | hba->desc_size.hlth_desc = QUERY_DESC_HEALTH_DEF_SIZE; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 6298 | } |
| 6299 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 6300 | /** |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6301 | * ufshcd_probe_hba - probe hba to detect device and initialize |
| 6302 | * @hba: per-adapter instance |
| 6303 | * |
| 6304 | * Execute link-startup and verify device initialization |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6305 | */ |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6306 | static int ufshcd_probe_hba(struct ufs_hba *hba) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6307 | { |
Tomas Winkler | 93fdd5a | 2017-01-05 10:45:12 +0200 | [diff] [blame] | 6308 | struct ufs_dev_desc card = {0}; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6309 | int ret; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 6310 | ktime_t start = ktime_get(); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6311 | |
| 6312 | ret = ufshcd_link_startup(hba); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 6313 | if (ret) |
| 6314 | goto out; |
| 6315 | |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 6316 | /* set the default level for urgent bkops */ |
| 6317 | hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT; |
| 6318 | hba->is_urgent_bkops_lvl_checked = false; |
| 6319 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 6320 | /* Debug counters initialization */ |
| 6321 | ufshcd_clear_dbg_ufs_stats(hba); |
| 6322 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6323 | /* UniPro link is active now */ |
| 6324 | ufshcd_set_link_active(hba); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 6325 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 6326 | ret = ufshcd_verify_dev_init(hba); |
| 6327 | if (ret) |
| 6328 | goto out; |
| 6329 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 6330 | ret = ufshcd_complete_dev_init(hba); |
| 6331 | if (ret) |
| 6332 | goto out; |
| 6333 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 6334 | /* Init check for device descriptor sizes */ |
| 6335 | ufshcd_init_desc_sizes(hba); |
| 6336 | |
Tomas Winkler | 93fdd5a | 2017-01-05 10:45:12 +0200 | [diff] [blame] | 6337 | ret = ufs_get_device_desc(hba, &card); |
| 6338 | if (ret) { |
| 6339 | dev_err(hba->dev, "%s: Failed getting device info. err = %d\n", |
| 6340 | __func__, ret); |
| 6341 | goto out; |
| 6342 | } |
| 6343 | |
| 6344 | ufs_fixup_device_setup(hba, &card); |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 6345 | ufshcd_tune_unipro_params(hba); |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 6346 | |
| 6347 | ret = ufshcd_set_vccq_rail_unused(hba, |
| 6348 | (hba->dev_quirks & UFS_DEVICE_NO_VCCQ) ? true : false); |
| 6349 | if (ret) |
| 6350 | goto out; |
| 6351 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6352 | /* UFS device is also active now */ |
| 6353 | ufshcd_set_ufs_dev_active(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 6354 | ufshcd_force_reset_auto_bkops(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6355 | hba->wlun_dev_clr_ua = true; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6356 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 6357 | if (ufshcd_get_max_pwr_mode(hba)) { |
| 6358 | dev_err(hba->dev, |
| 6359 | "%s: Failed getting max supported power mode\n", |
| 6360 | __func__); |
| 6361 | } else { |
| 6362 | ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info); |
Dov Levenglick | 8643ae6 | 2016-10-17 17:10:14 -0700 | [diff] [blame] | 6363 | if (ret) { |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 6364 | dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n", |
| 6365 | __func__, ret); |
Dov Levenglick | 8643ae6 | 2016-10-17 17:10:14 -0700 | [diff] [blame] | 6366 | goto out; |
| 6367 | } |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 6368 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6369 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 6370 | /* set the state as operational after switching to desired gear */ |
| 6371 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 6372 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6373 | /* |
| 6374 | * If we are in error handling context or in power management callbacks |
| 6375 | * context, no need to scan the host |
| 6376 | */ |
| 6377 | if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) { |
| 6378 | bool flag; |
| 6379 | |
| 6380 | /* clear any previous UFS device information */ |
| 6381 | memset(&hba->dev_info, 0, sizeof(hba->dev_info)); |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 6382 | if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG, |
| 6383 | QUERY_FLAG_IDN_PWR_ON_WPE, &flag)) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6384 | hba->dev_info.f_power_on_wp_en = flag; |
| 6385 | |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6386 | if (!hba->is_init_prefetch) |
| 6387 | ufshcd_init_icc_levels(hba); |
| 6388 | |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6389 | /* Add required well known logical units to scsi mid layer */ |
| 6390 | if (ufshcd_scsi_add_wlus(hba)) |
| 6391 | goto out; |
| 6392 | |
subhashj@codeaurora.org | 0701e49 | 2017-02-03 16:58:01 -0800 | [diff] [blame] | 6393 | /* Initialize devfreq after UFS device is detected */ |
| 6394 | if (ufshcd_is_clkscaling_supported(hba)) { |
| 6395 | memcpy(&hba->clk_scaling.saved_pwr_info.info, |
| 6396 | &hba->pwr_info, |
| 6397 | sizeof(struct ufs_pa_layer_attr)); |
| 6398 | hba->clk_scaling.saved_pwr_info.is_valid = true; |
| 6399 | if (!hba->devfreq) { |
| 6400 | hba->devfreq = devm_devfreq_add_device(hba->dev, |
| 6401 | &ufs_devfreq_profile, |
| 6402 | "simple_ondemand", |
| 6403 | NULL); |
| 6404 | if (IS_ERR(hba->devfreq)) { |
| 6405 | ret = PTR_ERR(hba->devfreq); |
| 6406 | dev_err(hba->dev, "Unable to register with devfreq %d\n", |
| 6407 | ret); |
| 6408 | goto out; |
| 6409 | } |
| 6410 | } |
| 6411 | hba->clk_scaling.is_allowed = true; |
| 6412 | } |
| 6413 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6414 | scsi_scan_host(hba->host); |
| 6415 | pm_runtime_put_sync(hba->dev); |
| 6416 | } |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6417 | |
| 6418 | if (!hba->is_init_prefetch) |
| 6419 | hba->is_init_prefetch = true; |
| 6420 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 6421 | out: |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6422 | /* |
| 6423 | * If we failed to initialize the device or the device is not |
| 6424 | * present, turn off the power/clocks etc. |
| 6425 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6426 | if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) { |
| 6427 | pm_runtime_put_sync(hba->dev); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6428 | ufshcd_hba_exit(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6429 | } |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6430 | |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 6431 | trace_ufshcd_init(dev_name(hba->dev), ret, |
| 6432 | ktime_to_us(ktime_sub(ktime_get(), start)), |
Subhash Jadavani | 73eba2b | 2017-01-10 16:48:25 -0800 | [diff] [blame] | 6433 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6434 | return ret; |
| 6435 | } |
| 6436 | |
| 6437 | /** |
| 6438 | * ufshcd_async_scan - asynchronous execution for probing hba |
| 6439 | * @data: data pointer to pass to this function |
| 6440 | * @cookie: cookie data |
| 6441 | */ |
| 6442 | static void ufshcd_async_scan(void *data, async_cookie_t cookie) |
| 6443 | { |
| 6444 | struct ufs_hba *hba = (struct ufs_hba *)data; |
| 6445 | |
| 6446 | ufshcd_probe_hba(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6447 | } |
| 6448 | |
Yaniv Gardi | f550c65 | 2016-03-10 17:37:07 +0200 | [diff] [blame] | 6449 | static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd) |
| 6450 | { |
| 6451 | unsigned long flags; |
| 6452 | struct Scsi_Host *host; |
| 6453 | struct ufs_hba *hba; |
| 6454 | int index; |
| 6455 | bool found = false; |
| 6456 | |
| 6457 | if (!scmd || !scmd->device || !scmd->device->host) |
| 6458 | return BLK_EH_NOT_HANDLED; |
| 6459 | |
| 6460 | host = scmd->device->host; |
| 6461 | hba = shost_priv(host); |
| 6462 | if (!hba) |
| 6463 | return BLK_EH_NOT_HANDLED; |
| 6464 | |
| 6465 | spin_lock_irqsave(host->host_lock, flags); |
| 6466 | |
| 6467 | for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) { |
| 6468 | if (hba->lrb[index].cmd == scmd) { |
| 6469 | found = true; |
| 6470 | break; |
| 6471 | } |
| 6472 | } |
| 6473 | |
| 6474 | spin_unlock_irqrestore(host->host_lock, flags); |
| 6475 | |
| 6476 | /* |
| 6477 | * Bypass SCSI error handling and reset the block layer timer if this |
| 6478 | * SCSI command was not actually dispatched to UFS driver, otherwise |
| 6479 | * let SCSI layer handle the error as usual. |
| 6480 | */ |
| 6481 | return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER; |
| 6482 | } |
| 6483 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6484 | static struct scsi_host_template ufshcd_driver_template = { |
| 6485 | .module = THIS_MODULE, |
| 6486 | .name = UFSHCD, |
| 6487 | .proc_name = UFSHCD, |
| 6488 | .queuecommand = ufshcd_queuecommand, |
| 6489 | .slave_alloc = ufshcd_slave_alloc, |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 6490 | .slave_configure = ufshcd_slave_configure, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6491 | .slave_destroy = ufshcd_slave_destroy, |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 6492 | .change_queue_depth = ufshcd_change_queue_depth, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6493 | .eh_abort_handler = ufshcd_abort, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6494 | .eh_device_reset_handler = ufshcd_eh_device_reset_handler, |
| 6495 | .eh_host_reset_handler = ufshcd_eh_host_reset_handler, |
Yaniv Gardi | f550c65 | 2016-03-10 17:37:07 +0200 | [diff] [blame] | 6496 | .eh_timed_out = ufshcd_eh_timed_out, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6497 | .this_id = -1, |
| 6498 | .sg_tablesize = SG_ALL, |
| 6499 | .cmd_per_lun = UFSHCD_CMD_PER_LUN, |
| 6500 | .can_queue = UFSHCD_CAN_QUEUE, |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6501 | .max_host_blocked = 1, |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 6502 | .track_queue_depth = 1, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6503 | }; |
| 6504 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6505 | static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg, |
| 6506 | int ua) |
| 6507 | { |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 6508 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6509 | |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 6510 | if (!vreg) |
| 6511 | return 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6512 | |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 6513 | ret = regulator_set_load(vreg->reg, ua); |
| 6514 | if (ret < 0) { |
| 6515 | dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n", |
| 6516 | __func__, vreg->name, ua, ret); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6517 | } |
| 6518 | |
| 6519 | return ret; |
| 6520 | } |
| 6521 | |
| 6522 | static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba, |
| 6523 | struct ufs_vreg *vreg) |
| 6524 | { |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 6525 | if (!vreg) |
| 6526 | return 0; |
| 6527 | else if (vreg->unused) |
| 6528 | return 0; |
| 6529 | else |
| 6530 | return ufshcd_config_vreg_load(hba->dev, vreg, |
| 6531 | UFS_VREG_LPM_LOAD_UA); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6532 | } |
| 6533 | |
| 6534 | static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba, |
| 6535 | struct ufs_vreg *vreg) |
| 6536 | { |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 6537 | if (!vreg) |
| 6538 | return 0; |
| 6539 | else if (vreg->unused) |
| 6540 | return 0; |
| 6541 | else |
| 6542 | return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6543 | } |
| 6544 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6545 | static int ufshcd_config_vreg(struct device *dev, |
| 6546 | struct ufs_vreg *vreg, bool on) |
| 6547 | { |
| 6548 | int ret = 0; |
Gustavo A. R. Silva | 7275359 | 2017-11-20 08:12:29 -0600 | [diff] [blame] | 6549 | struct regulator *reg; |
| 6550 | const char *name; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6551 | int min_uV, uA_load; |
| 6552 | |
| 6553 | BUG_ON(!vreg); |
| 6554 | |
Gustavo A. R. Silva | 7275359 | 2017-11-20 08:12:29 -0600 | [diff] [blame] | 6555 | reg = vreg->reg; |
| 6556 | name = vreg->name; |
| 6557 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6558 | if (regulator_count_voltages(reg) > 0) { |
| 6559 | min_uV = on ? vreg->min_uV : 0; |
| 6560 | ret = regulator_set_voltage(reg, min_uV, vreg->max_uV); |
| 6561 | if (ret) { |
| 6562 | dev_err(dev, "%s: %s set voltage failed, err=%d\n", |
| 6563 | __func__, name, ret); |
| 6564 | goto out; |
| 6565 | } |
| 6566 | |
| 6567 | uA_load = on ? vreg->max_uA : 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6568 | ret = ufshcd_config_vreg_load(dev, vreg, uA_load); |
| 6569 | if (ret) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6570 | goto out; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6571 | } |
| 6572 | out: |
| 6573 | return ret; |
| 6574 | } |
| 6575 | |
| 6576 | static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 6577 | { |
| 6578 | int ret = 0; |
| 6579 | |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 6580 | if (!vreg) |
| 6581 | goto out; |
| 6582 | else if (vreg->enabled || vreg->unused) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6583 | goto out; |
| 6584 | |
| 6585 | ret = ufshcd_config_vreg(dev, vreg, true); |
| 6586 | if (!ret) |
| 6587 | ret = regulator_enable(vreg->reg); |
| 6588 | |
| 6589 | if (!ret) |
| 6590 | vreg->enabled = true; |
| 6591 | else |
| 6592 | dev_err(dev, "%s: %s enable failed, err=%d\n", |
| 6593 | __func__, vreg->name, ret); |
| 6594 | out: |
| 6595 | return ret; |
| 6596 | } |
| 6597 | |
| 6598 | static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 6599 | { |
| 6600 | int ret = 0; |
| 6601 | |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 6602 | if (!vreg) |
| 6603 | goto out; |
| 6604 | else if (!vreg->enabled || vreg->unused) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6605 | goto out; |
| 6606 | |
| 6607 | ret = regulator_disable(vreg->reg); |
| 6608 | |
| 6609 | if (!ret) { |
| 6610 | /* ignore errors on applying disable config */ |
| 6611 | ufshcd_config_vreg(dev, vreg, false); |
| 6612 | vreg->enabled = false; |
| 6613 | } else { |
| 6614 | dev_err(dev, "%s: %s disable failed, err=%d\n", |
| 6615 | __func__, vreg->name, ret); |
| 6616 | } |
| 6617 | out: |
| 6618 | return ret; |
| 6619 | } |
| 6620 | |
| 6621 | static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on) |
| 6622 | { |
| 6623 | int ret = 0; |
| 6624 | struct device *dev = hba->dev; |
| 6625 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 6626 | |
| 6627 | if (!info) |
| 6628 | goto out; |
| 6629 | |
| 6630 | ret = ufshcd_toggle_vreg(dev, info->vcc, on); |
| 6631 | if (ret) |
| 6632 | goto out; |
| 6633 | |
| 6634 | ret = ufshcd_toggle_vreg(dev, info->vccq, on); |
| 6635 | if (ret) |
| 6636 | goto out; |
| 6637 | |
| 6638 | ret = ufshcd_toggle_vreg(dev, info->vccq2, on); |
| 6639 | if (ret) |
| 6640 | goto out; |
| 6641 | |
| 6642 | out: |
| 6643 | if (ret) { |
| 6644 | ufshcd_toggle_vreg(dev, info->vccq2, false); |
| 6645 | ufshcd_toggle_vreg(dev, info->vccq, false); |
| 6646 | ufshcd_toggle_vreg(dev, info->vcc, false); |
| 6647 | } |
| 6648 | return ret; |
| 6649 | } |
| 6650 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 6651 | static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on) |
| 6652 | { |
| 6653 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 6654 | |
| 6655 | if (info) |
| 6656 | return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on); |
| 6657 | |
| 6658 | return 0; |
| 6659 | } |
| 6660 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6661 | static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 6662 | { |
| 6663 | int ret = 0; |
| 6664 | |
| 6665 | if (!vreg) |
| 6666 | goto out; |
| 6667 | |
| 6668 | vreg->reg = devm_regulator_get(dev, vreg->name); |
| 6669 | if (IS_ERR(vreg->reg)) { |
| 6670 | ret = PTR_ERR(vreg->reg); |
| 6671 | dev_err(dev, "%s: %s get failed, err=%d\n", |
| 6672 | __func__, vreg->name, ret); |
| 6673 | } |
| 6674 | out: |
| 6675 | return ret; |
| 6676 | } |
| 6677 | |
| 6678 | static int ufshcd_init_vreg(struct ufs_hba *hba) |
| 6679 | { |
| 6680 | int ret = 0; |
| 6681 | struct device *dev = hba->dev; |
| 6682 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 6683 | |
| 6684 | if (!info) |
| 6685 | goto out; |
| 6686 | |
| 6687 | ret = ufshcd_get_vreg(dev, info->vcc); |
| 6688 | if (ret) |
| 6689 | goto out; |
| 6690 | |
| 6691 | ret = ufshcd_get_vreg(dev, info->vccq); |
| 6692 | if (ret) |
| 6693 | goto out; |
| 6694 | |
| 6695 | ret = ufshcd_get_vreg(dev, info->vccq2); |
| 6696 | out: |
| 6697 | return ret; |
| 6698 | } |
| 6699 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 6700 | static int ufshcd_init_hba_vreg(struct ufs_hba *hba) |
| 6701 | { |
| 6702 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 6703 | |
| 6704 | if (info) |
| 6705 | return ufshcd_get_vreg(hba->dev, info->vdd_hba); |
| 6706 | |
| 6707 | return 0; |
| 6708 | } |
| 6709 | |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 6710 | static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused) |
| 6711 | { |
| 6712 | int ret = 0; |
| 6713 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 6714 | |
| 6715 | if (!info) |
| 6716 | goto out; |
| 6717 | else if (!info->vccq) |
| 6718 | goto out; |
| 6719 | |
| 6720 | if (unused) { |
| 6721 | /* shut off the rail here */ |
| 6722 | ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false); |
| 6723 | /* |
| 6724 | * Mark this rail as no longer used, so it doesn't get enabled |
| 6725 | * later by mistake |
| 6726 | */ |
| 6727 | if (!ret) |
| 6728 | info->vccq->unused = true; |
| 6729 | } else { |
| 6730 | /* |
| 6731 | * rail should have been already enabled hence just make sure |
| 6732 | * that unused flag is cleared. |
| 6733 | */ |
| 6734 | info->vccq->unused = false; |
| 6735 | } |
| 6736 | out: |
| 6737 | return ret; |
| 6738 | } |
| 6739 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6740 | static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on, |
| 6741 | bool skip_ref_clk) |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 6742 | { |
| 6743 | int ret = 0; |
| 6744 | struct ufs_clk_info *clki; |
| 6745 | struct list_head *head = &hba->clk_list_head; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6746 | unsigned long flags; |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 6747 | ktime_t start = ktime_get(); |
| 6748 | bool clk_state_changed = false; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 6749 | |
Szymon Mielczarek | 566ec9a | 2017-06-05 11:36:54 +0300 | [diff] [blame] | 6750 | if (list_empty(head)) |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 6751 | goto out; |
| 6752 | |
Subhash Jadavani | 1e879e8 | 2016-10-06 21:48:22 -0700 | [diff] [blame] | 6753 | ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE); |
| 6754 | if (ret) |
| 6755 | return ret; |
| 6756 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 6757 | list_for_each_entry(clki, head, list) { |
| 6758 | if (!IS_ERR_OR_NULL(clki->clk)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6759 | if (skip_ref_clk && !strcmp(clki->name, "ref_clk")) |
| 6760 | continue; |
| 6761 | |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 6762 | clk_state_changed = on ^ clki->enabled; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 6763 | if (on && !clki->enabled) { |
| 6764 | ret = clk_prepare_enable(clki->clk); |
| 6765 | if (ret) { |
| 6766 | dev_err(hba->dev, "%s: %s prepare enable failed, %d\n", |
| 6767 | __func__, clki->name, ret); |
| 6768 | goto out; |
| 6769 | } |
| 6770 | } else if (!on && clki->enabled) { |
| 6771 | clk_disable_unprepare(clki->clk); |
| 6772 | } |
| 6773 | clki->enabled = on; |
| 6774 | dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__, |
| 6775 | clki->name, on ? "en" : "dis"); |
| 6776 | } |
| 6777 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6778 | |
Subhash Jadavani | 1e879e8 | 2016-10-06 21:48:22 -0700 | [diff] [blame] | 6779 | ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE); |
| 6780 | if (ret) |
| 6781 | return ret; |
| 6782 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 6783 | out: |
| 6784 | if (ret) { |
| 6785 | list_for_each_entry(clki, head, list) { |
| 6786 | if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled) |
| 6787 | clk_disable_unprepare(clki->clk); |
| 6788 | } |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 6789 | } else if (!ret && on) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6790 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 6791 | hba->clk_gating.state = CLKS_ON; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 6792 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 6793 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6794 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 6795 | } |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 6796 | |
subhashj@codeaurora.org | 911a077 | 2016-12-22 18:41:48 -0800 | [diff] [blame] | 6797 | if (clk_state_changed) |
| 6798 | trace_ufshcd_profile_clk_gating(dev_name(hba->dev), |
| 6799 | (on ? "on" : "off"), |
| 6800 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 6801 | return ret; |
| 6802 | } |
| 6803 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6804 | static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on) |
| 6805 | { |
| 6806 | return __ufshcd_setup_clocks(hba, on, false); |
| 6807 | } |
| 6808 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 6809 | static int ufshcd_init_clocks(struct ufs_hba *hba) |
| 6810 | { |
| 6811 | int ret = 0; |
| 6812 | struct ufs_clk_info *clki; |
| 6813 | struct device *dev = hba->dev; |
| 6814 | struct list_head *head = &hba->clk_list_head; |
| 6815 | |
Szymon Mielczarek | 566ec9a | 2017-06-05 11:36:54 +0300 | [diff] [blame] | 6816 | if (list_empty(head)) |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 6817 | goto out; |
| 6818 | |
| 6819 | list_for_each_entry(clki, head, list) { |
| 6820 | if (!clki->name) |
| 6821 | continue; |
| 6822 | |
| 6823 | clki->clk = devm_clk_get(dev, clki->name); |
| 6824 | if (IS_ERR(clki->clk)) { |
| 6825 | ret = PTR_ERR(clki->clk); |
| 6826 | dev_err(dev, "%s: %s clk get failed, %d\n", |
| 6827 | __func__, clki->name, ret); |
| 6828 | goto out; |
| 6829 | } |
| 6830 | |
| 6831 | if (clki->max_freq) { |
| 6832 | ret = clk_set_rate(clki->clk, clki->max_freq); |
| 6833 | if (ret) { |
| 6834 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 6835 | __func__, clki->name, |
| 6836 | clki->max_freq, ret); |
| 6837 | goto out; |
| 6838 | } |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 6839 | clki->curr_freq = clki->max_freq; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 6840 | } |
| 6841 | dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__, |
| 6842 | clki->name, clk_get_rate(clki->clk)); |
| 6843 | } |
| 6844 | out: |
| 6845 | return ret; |
| 6846 | } |
| 6847 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6848 | static int ufshcd_variant_hba_init(struct ufs_hba *hba) |
| 6849 | { |
| 6850 | int err = 0; |
| 6851 | |
| 6852 | if (!hba->vops) |
| 6853 | goto out; |
| 6854 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 6855 | err = ufshcd_vops_init(hba); |
| 6856 | if (err) |
| 6857 | goto out; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6858 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 6859 | err = ufshcd_vops_setup_regulators(hba, true); |
| 6860 | if (err) |
| 6861 | goto out_exit; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6862 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6863 | goto out; |
| 6864 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6865 | out_exit: |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 6866 | ufshcd_vops_exit(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6867 | out: |
| 6868 | if (err) |
| 6869 | dev_err(hba->dev, "%s: variant %s init failed err %d\n", |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 6870 | __func__, ufshcd_get_var_name(hba), err); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6871 | return err; |
| 6872 | } |
| 6873 | |
| 6874 | static void ufshcd_variant_hba_exit(struct ufs_hba *hba) |
| 6875 | { |
| 6876 | if (!hba->vops) |
| 6877 | return; |
| 6878 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 6879 | ufshcd_vops_setup_regulators(hba, false); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6880 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 6881 | ufshcd_vops_exit(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 6882 | } |
| 6883 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6884 | static int ufshcd_hba_init(struct ufs_hba *hba) |
| 6885 | { |
| 6886 | int err; |
| 6887 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 6888 | /* |
| 6889 | * Handle host controller power separately from the UFS device power |
| 6890 | * rails as it will help controlling the UFS host controller power |
| 6891 | * collapse easily which is different than UFS device power collapse. |
| 6892 | * Also, enable the host controller power before we go ahead with rest |
| 6893 | * of the initialization here. |
| 6894 | */ |
| 6895 | err = ufshcd_init_hba_vreg(hba); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6896 | if (err) |
| 6897 | goto out; |
| 6898 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 6899 | err = ufshcd_setup_hba_vreg(hba, true); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6900 | if (err) |
| 6901 | goto out; |
| 6902 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 6903 | err = ufshcd_init_clocks(hba); |
| 6904 | if (err) |
| 6905 | goto out_disable_hba_vreg; |
| 6906 | |
| 6907 | err = ufshcd_setup_clocks(hba, true); |
| 6908 | if (err) |
| 6909 | goto out_disable_hba_vreg; |
| 6910 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 6911 | err = ufshcd_init_vreg(hba); |
| 6912 | if (err) |
| 6913 | goto out_disable_clks; |
| 6914 | |
| 6915 | err = ufshcd_setup_vreg(hba, true); |
| 6916 | if (err) |
| 6917 | goto out_disable_clks; |
| 6918 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6919 | err = ufshcd_variant_hba_init(hba); |
| 6920 | if (err) |
| 6921 | goto out_disable_vreg; |
| 6922 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6923 | hba->is_powered = true; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6924 | goto out; |
| 6925 | |
| 6926 | out_disable_vreg: |
| 6927 | ufshcd_setup_vreg(hba, false); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 6928 | out_disable_clks: |
| 6929 | ufshcd_setup_clocks(hba, false); |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 6930 | out_disable_hba_vreg: |
| 6931 | ufshcd_setup_hba_vreg(hba, false); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6932 | out: |
| 6933 | return err; |
| 6934 | } |
| 6935 | |
| 6936 | static void ufshcd_hba_exit(struct ufs_hba *hba) |
| 6937 | { |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6938 | if (hba->is_powered) { |
| 6939 | ufshcd_variant_hba_exit(hba); |
| 6940 | ufshcd_setup_vreg(hba, false); |
Gilad Broner | a508253 | 2016-10-17 17:10:00 -0700 | [diff] [blame] | 6941 | ufshcd_suspend_clkscaling(hba); |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 6942 | if (ufshcd_is_clkscaling_supported(hba)) { |
subhashj@codeaurora.org | 0701e49 | 2017-02-03 16:58:01 -0800 | [diff] [blame] | 6943 | if (hba->devfreq) |
| 6944 | ufshcd_suspend_clkscaling(hba); |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 6945 | destroy_workqueue(hba->clk_scaling.workq); |
| 6946 | } |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6947 | ufshcd_setup_clocks(hba, false); |
| 6948 | ufshcd_setup_hba_vreg(hba, false); |
| 6949 | hba->is_powered = false; |
| 6950 | } |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 6951 | } |
| 6952 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6953 | static int |
| 6954 | ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6955 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6956 | unsigned char cmd[6] = {REQUEST_SENSE, |
| 6957 | 0, |
| 6958 | 0, |
| 6959 | 0, |
Gilad Broner | dcea0bf | 2016-10-17 17:09:48 -0700 | [diff] [blame] | 6960 | UFSHCD_REQ_SENSE_SIZE, |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6961 | 0}; |
| 6962 | char *buffer; |
| 6963 | int ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6964 | |
Gilad Broner | dcea0bf | 2016-10-17 17:09:48 -0700 | [diff] [blame] | 6965 | buffer = kzalloc(UFSHCD_REQ_SENSE_SIZE, GFP_KERNEL); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6966 | if (!buffer) { |
| 6967 | ret = -ENOMEM; |
| 6968 | goto out; |
| 6969 | } |
| 6970 | |
Christoph Hellwig | fcbfffe | 2017-02-23 16:02:37 +0100 | [diff] [blame] | 6971 | ret = scsi_execute(sdp, cmd, DMA_FROM_DEVICE, buffer, |
| 6972 | UFSHCD_REQ_SENSE_SIZE, NULL, NULL, |
| 6973 | msecs_to_jiffies(1000), 3, 0, RQF_PM, NULL); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6974 | if (ret) |
| 6975 | pr_err("%s: failed with err %d\n", __func__, ret); |
| 6976 | |
| 6977 | kfree(buffer); |
| 6978 | out: |
| 6979 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6980 | } |
| 6981 | |
| 6982 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6983 | * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device |
| 6984 | * power mode |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 6985 | * @hba: per adapter instance |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6986 | * @pwr_mode: device power mode to set |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6987 | * |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6988 | * Returns 0 if requested power mode is set successfully |
| 6989 | * Returns non-zero if failed to set the requested power mode |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6990 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6991 | static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba, |
| 6992 | enum ufs_dev_pwr_mode pwr_mode) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6993 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6994 | unsigned char cmd[6] = { START_STOP }; |
| 6995 | struct scsi_sense_hdr sshdr; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6996 | struct scsi_device *sdp; |
| 6997 | unsigned long flags; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6998 | int ret; |
| 6999 | |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 7000 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 7001 | sdp = hba->sdev_ufs_device; |
| 7002 | if (sdp) { |
| 7003 | ret = scsi_device_get(sdp); |
| 7004 | if (!ret && !scsi_device_online(sdp)) { |
| 7005 | ret = -ENODEV; |
| 7006 | scsi_device_put(sdp); |
| 7007 | } |
| 7008 | } else { |
| 7009 | ret = -ENODEV; |
| 7010 | } |
| 7011 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 7012 | |
| 7013 | if (ret) |
| 7014 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7015 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7016 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7017 | * If scsi commands fail, the scsi mid-layer schedules scsi error- |
| 7018 | * handling, which would wait for host to be resumed. Since we know |
| 7019 | * we are functional while we are here, skip host resume in error |
| 7020 | * handling context. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7021 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7022 | hba->host->eh_noresume = 1; |
| 7023 | if (hba->wlun_dev_clr_ua) { |
| 7024 | ret = ufshcd_send_request_sense(hba, sdp); |
| 7025 | if (ret) |
| 7026 | goto out; |
| 7027 | /* Unit attention condition is cleared now */ |
| 7028 | hba->wlun_dev_clr_ua = false; |
| 7029 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7030 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7031 | cmd[4] = pwr_mode << 4; |
| 7032 | |
| 7033 | /* |
| 7034 | * Current function would be generally called from the power management |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 7035 | * 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] | 7036 | * already suspended childs. |
| 7037 | */ |
Christoph Hellwig | fcbfffe | 2017-02-23 16:02:37 +0100 | [diff] [blame] | 7038 | ret = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr, |
| 7039 | START_STOP_TIMEOUT, 0, 0, RQF_PM, NULL); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7040 | if (ret) { |
| 7041 | sdev_printk(KERN_WARNING, sdp, |
Hannes Reinecke | ef61329 | 2014-10-24 14:27:00 +0200 | [diff] [blame] | 7042 | "START_STOP failed for power mode: %d, result %x\n", |
| 7043 | pwr_mode, ret); |
Hannes Reinecke | 2104551 | 2015-01-08 07:43:46 +0100 | [diff] [blame] | 7044 | if (driver_byte(ret) & DRIVER_SENSE) |
| 7045 | scsi_print_sense_hdr(sdp, NULL, &sshdr); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7046 | } |
| 7047 | |
| 7048 | if (!ret) |
| 7049 | hba->curr_dev_pwr_mode = pwr_mode; |
| 7050 | out: |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 7051 | scsi_device_put(sdp); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7052 | hba->host->eh_noresume = 0; |
| 7053 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7054 | } |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7055 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7056 | static int ufshcd_link_state_transition(struct ufs_hba *hba, |
| 7057 | enum uic_link_state req_link_state, |
| 7058 | int check_for_bkops) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 7059 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7060 | int ret = 0; |
| 7061 | |
| 7062 | if (req_link_state == hba->uic_link_state) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 7063 | return 0; |
| 7064 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7065 | if (req_link_state == UIC_LINK_HIBERN8_STATE) { |
| 7066 | ret = ufshcd_uic_hibern8_enter(hba); |
| 7067 | if (!ret) |
| 7068 | ufshcd_set_link_hibern8(hba); |
| 7069 | else |
| 7070 | goto out; |
| 7071 | } |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 7072 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7073 | * If autobkops is enabled, link can't be turned off because |
| 7074 | * turning off the link would also turn off the device. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 7075 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7076 | else if ((req_link_state == UIC_LINK_OFF_STATE) && |
| 7077 | (!check_for_bkops || (check_for_bkops && |
| 7078 | !hba->auto_bkops_enabled))) { |
| 7079 | /* |
Yaniv Gardi | f3099fb | 2016-03-10 17:37:17 +0200 | [diff] [blame] | 7080 | * Let's make sure that link is in low power mode, we are doing |
| 7081 | * this currently by putting the link in Hibern8. Otherway to |
| 7082 | * put the link in low power mode is to send the DME end point |
| 7083 | * to device and then send the DME reset command to local |
| 7084 | * unipro. But putting the link in hibern8 is much faster. |
| 7085 | */ |
| 7086 | ret = ufshcd_uic_hibern8_enter(hba); |
| 7087 | if (ret) |
| 7088 | goto out; |
| 7089 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7090 | * Change controller state to "reset state" which |
| 7091 | * should also put the link in off/reset state |
| 7092 | */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 7093 | ufshcd_hba_stop(hba, true); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7094 | /* |
| 7095 | * TODO: Check if we need any delay to make sure that |
| 7096 | * controller is reset |
| 7097 | */ |
| 7098 | ufshcd_set_link_off(hba); |
| 7099 | } |
| 7100 | |
| 7101 | out: |
| 7102 | return ret; |
| 7103 | } |
| 7104 | |
| 7105 | static void ufshcd_vreg_set_lpm(struct ufs_hba *hba) |
| 7106 | { |
| 7107 | /* |
Yaniv Gardi | b799fdf | 2016-03-10 17:37:18 +0200 | [diff] [blame] | 7108 | * It seems some UFS devices may keep drawing more than sleep current |
| 7109 | * (atleast for 500us) from UFS rails (especially from VCCQ rail). |
| 7110 | * To avoid this situation, add 2ms delay before putting these UFS |
| 7111 | * rails in LPM mode. |
| 7112 | */ |
| 7113 | if (!ufshcd_is_link_active(hba) && |
| 7114 | hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM) |
| 7115 | usleep_range(2000, 2100); |
| 7116 | |
| 7117 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7118 | * If UFS device is either in UFS_Sleep turn off VCC rail to save some |
| 7119 | * power. |
| 7120 | * |
| 7121 | * If UFS device and link is in OFF state, all power supplies (VCC, |
| 7122 | * VCCQ, VCCQ2) can be turned off if power on write protect is not |
| 7123 | * required. If UFS link is inactive (Hibern8 or OFF state) and device |
| 7124 | * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode. |
| 7125 | * |
| 7126 | * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway |
| 7127 | * in low power state which would save some power. |
| 7128 | */ |
| 7129 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) && |
| 7130 | !hba->dev_info.is_lu_power_on_wp) { |
| 7131 | ufshcd_setup_vreg(hba, false); |
| 7132 | } else if (!ufshcd_is_ufs_dev_active(hba)) { |
| 7133 | ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false); |
| 7134 | if (!ufshcd_is_link_active(hba)) { |
| 7135 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq); |
| 7136 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2); |
| 7137 | } |
| 7138 | } |
| 7139 | } |
| 7140 | |
| 7141 | static int ufshcd_vreg_set_hpm(struct ufs_hba *hba) |
| 7142 | { |
| 7143 | int ret = 0; |
| 7144 | |
| 7145 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) && |
| 7146 | !hba->dev_info.is_lu_power_on_wp) { |
| 7147 | ret = ufshcd_setup_vreg(hba, true); |
| 7148 | } else if (!ufshcd_is_ufs_dev_active(hba)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7149 | if (!ret && !ufshcd_is_link_active(hba)) { |
| 7150 | ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq); |
| 7151 | if (ret) |
| 7152 | goto vcc_disable; |
| 7153 | ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2); |
| 7154 | if (ret) |
| 7155 | goto vccq_lpm; |
| 7156 | } |
Subhash Jadavani | 69d72ac | 2016-10-27 17:26:24 -0700 | [diff] [blame] | 7157 | ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7158 | } |
| 7159 | goto out; |
| 7160 | |
| 7161 | vccq_lpm: |
| 7162 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq); |
| 7163 | vcc_disable: |
| 7164 | ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false); |
| 7165 | out: |
| 7166 | return ret; |
| 7167 | } |
| 7168 | |
| 7169 | static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba) |
| 7170 | { |
| 7171 | if (ufshcd_is_link_off(hba)) |
| 7172 | ufshcd_setup_hba_vreg(hba, false); |
| 7173 | } |
| 7174 | |
| 7175 | static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba) |
| 7176 | { |
| 7177 | if (ufshcd_is_link_off(hba)) |
| 7178 | ufshcd_setup_hba_vreg(hba, true); |
| 7179 | } |
| 7180 | |
| 7181 | /** |
| 7182 | * ufshcd_suspend - helper function for suspend operations |
| 7183 | * @hba: per adapter instance |
| 7184 | * @pm_op: desired low power operation type |
| 7185 | * |
| 7186 | * This function will try to put the UFS device and link into low power |
| 7187 | * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl" |
| 7188 | * (System PM level). |
| 7189 | * |
| 7190 | * If this function is called during shutdown, it will make sure that |
| 7191 | * both UFS device and UFS link is powered off. |
| 7192 | * |
| 7193 | * NOTE: UFS device & link must be active before we enter in this function. |
| 7194 | * |
| 7195 | * Returns 0 for success and non-zero for failure |
| 7196 | */ |
| 7197 | static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op) |
| 7198 | { |
| 7199 | int ret = 0; |
| 7200 | enum ufs_pm_level pm_lvl; |
| 7201 | enum ufs_dev_pwr_mode req_dev_pwr_mode; |
| 7202 | enum uic_link_state req_link_state; |
| 7203 | |
| 7204 | hba->pm_op_in_progress = 1; |
| 7205 | if (!ufshcd_is_shutdown_pm(pm_op)) { |
| 7206 | pm_lvl = ufshcd_is_runtime_pm(pm_op) ? |
| 7207 | hba->rpm_lvl : hba->spm_lvl; |
| 7208 | req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl); |
| 7209 | req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl); |
| 7210 | } else { |
| 7211 | req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE; |
| 7212 | req_link_state = UIC_LINK_OFF_STATE; |
| 7213 | } |
| 7214 | |
| 7215 | /* |
| 7216 | * If we can't transition into any of the low power modes |
| 7217 | * just gate the clocks. |
| 7218 | */ |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7219 | ufshcd_hold(hba, false); |
| 7220 | hba->clk_gating.is_suspended = true; |
| 7221 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 7222 | if (hba->clk_scaling.is_allowed) { |
| 7223 | cancel_work_sync(&hba->clk_scaling.suspend_work); |
| 7224 | cancel_work_sync(&hba->clk_scaling.resume_work); |
| 7225 | ufshcd_suspend_clkscaling(hba); |
| 7226 | } |
Subhash Jadavani | d6fcf81 | 2016-10-27 17:26:09 -0700 | [diff] [blame] | 7227 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7228 | if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE && |
| 7229 | req_link_state == UIC_LINK_ACTIVE_STATE) { |
| 7230 | goto disable_clks; |
| 7231 | } |
| 7232 | |
| 7233 | if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) && |
| 7234 | (req_link_state == hba->uic_link_state)) |
Subhash Jadavani | d6fcf81 | 2016-10-27 17:26:09 -0700 | [diff] [blame] | 7235 | goto enable_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7236 | |
| 7237 | /* UFS device & link must be active before we enter in this function */ |
| 7238 | if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) { |
| 7239 | ret = -EINVAL; |
Subhash Jadavani | d6fcf81 | 2016-10-27 17:26:09 -0700 | [diff] [blame] | 7240 | goto enable_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7241 | } |
| 7242 | |
| 7243 | if (ufshcd_is_runtime_pm(pm_op)) { |
Subhash Jadavani | 374a246 | 2014-09-25 15:32:35 +0300 | [diff] [blame] | 7244 | if (ufshcd_can_autobkops_during_suspend(hba)) { |
| 7245 | /* |
| 7246 | * The device is idle with no requests in the queue, |
| 7247 | * allow background operations if bkops status shows |
| 7248 | * that performance might be impacted. |
| 7249 | */ |
| 7250 | ret = ufshcd_urgent_bkops(hba); |
| 7251 | if (ret) |
| 7252 | goto enable_gating; |
| 7253 | } else { |
| 7254 | /* make sure that auto bkops is disabled */ |
| 7255 | ufshcd_disable_auto_bkops(hba); |
| 7256 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7257 | } |
| 7258 | |
| 7259 | if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) && |
| 7260 | ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) || |
| 7261 | !ufshcd_is_runtime_pm(pm_op))) { |
| 7262 | /* ensure that bkops is disabled */ |
| 7263 | ufshcd_disable_auto_bkops(hba); |
| 7264 | ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode); |
| 7265 | if (ret) |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7266 | goto enable_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7267 | } |
| 7268 | |
| 7269 | ret = ufshcd_link_state_transition(hba, req_link_state, 1); |
| 7270 | if (ret) |
| 7271 | goto set_dev_active; |
| 7272 | |
| 7273 | ufshcd_vreg_set_lpm(hba); |
| 7274 | |
| 7275 | disable_clks: |
| 7276 | /* |
| 7277 | * Call vendor specific suspend callback. As these callbacks may access |
| 7278 | * vendor specific host controller register space call them before the |
| 7279 | * host clocks are ON. |
| 7280 | */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7281 | ret = ufshcd_vops_suspend(hba, pm_op); |
| 7282 | if (ret) |
| 7283 | goto set_link_active; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7284 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7285 | if (!ufshcd_is_link_active(hba)) |
| 7286 | ufshcd_setup_clocks(hba, false); |
| 7287 | else |
| 7288 | /* If link is active, device ref_clk can't be switched off */ |
| 7289 | __ufshcd_setup_clocks(hba, false, true); |
| 7290 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7291 | hba->clk_gating.state = CLKS_OFF; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7292 | trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7293 | /* |
| 7294 | * 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] | 7295 | * host controller transaction expected till resume. |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7296 | */ |
| 7297 | ufshcd_disable_irq(hba); |
| 7298 | /* Put the host controller in low power mode if possible */ |
| 7299 | ufshcd_hba_vreg_set_lpm(hba); |
| 7300 | goto out; |
| 7301 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7302 | set_link_active: |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 7303 | if (hba->clk_scaling.is_allowed) |
| 7304 | ufshcd_resume_clkscaling(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7305 | ufshcd_vreg_set_hpm(hba); |
| 7306 | if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba)) |
| 7307 | ufshcd_set_link_active(hba); |
| 7308 | else if (ufshcd_is_link_off(hba)) |
| 7309 | ufshcd_host_reset_and_restore(hba); |
| 7310 | set_dev_active: |
| 7311 | if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE)) |
| 7312 | ufshcd_disable_auto_bkops(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7313 | enable_gating: |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 7314 | if (hba->clk_scaling.is_allowed) |
| 7315 | ufshcd_resume_clkscaling(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7316 | hba->clk_gating.is_suspended = false; |
| 7317 | ufshcd_release(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7318 | out: |
| 7319 | hba->pm_op_in_progress = 0; |
| 7320 | return ret; |
| 7321 | } |
| 7322 | |
| 7323 | /** |
| 7324 | * ufshcd_resume - helper function for resume operations |
| 7325 | * @hba: per adapter instance |
| 7326 | * @pm_op: runtime PM or system PM |
| 7327 | * |
| 7328 | * This function basically brings the UFS device, UniPro link and controller |
| 7329 | * to active state. |
| 7330 | * |
| 7331 | * Returns 0 for success and non-zero for failure |
| 7332 | */ |
| 7333 | static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op) |
| 7334 | { |
| 7335 | int ret; |
| 7336 | enum uic_link_state old_link_state; |
| 7337 | |
| 7338 | hba->pm_op_in_progress = 1; |
| 7339 | old_link_state = hba->uic_link_state; |
| 7340 | |
| 7341 | ufshcd_hba_vreg_set_hpm(hba); |
| 7342 | /* Make sure clocks are enabled before accessing controller */ |
| 7343 | ret = ufshcd_setup_clocks(hba, true); |
| 7344 | if (ret) |
| 7345 | goto out; |
| 7346 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7347 | /* enable the host irq as host controller would be active soon */ |
| 7348 | ret = ufshcd_enable_irq(hba); |
| 7349 | if (ret) |
| 7350 | goto disable_irq_and_vops_clks; |
| 7351 | |
| 7352 | ret = ufshcd_vreg_set_hpm(hba); |
| 7353 | if (ret) |
| 7354 | goto disable_irq_and_vops_clks; |
| 7355 | |
| 7356 | /* |
| 7357 | * Call vendor specific resume callback. As these callbacks may access |
| 7358 | * vendor specific host controller register space call them when the |
| 7359 | * host clocks are ON. |
| 7360 | */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7361 | ret = ufshcd_vops_resume(hba, pm_op); |
| 7362 | if (ret) |
| 7363 | goto disable_vreg; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7364 | |
| 7365 | if (ufshcd_is_link_hibern8(hba)) { |
| 7366 | ret = ufshcd_uic_hibern8_exit(hba); |
| 7367 | if (!ret) |
| 7368 | ufshcd_set_link_active(hba); |
| 7369 | else |
| 7370 | goto vendor_suspend; |
| 7371 | } else if (ufshcd_is_link_off(hba)) { |
| 7372 | ret = ufshcd_host_reset_and_restore(hba); |
| 7373 | /* |
| 7374 | * ufshcd_host_reset_and_restore() should have already |
| 7375 | * set the link state as active |
| 7376 | */ |
| 7377 | if (ret || !ufshcd_is_link_active(hba)) |
| 7378 | goto vendor_suspend; |
| 7379 | } |
| 7380 | |
| 7381 | if (!ufshcd_is_ufs_dev_active(hba)) { |
| 7382 | ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE); |
| 7383 | if (ret) |
| 7384 | goto set_old_link_state; |
| 7385 | } |
| 7386 | |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 7387 | if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) |
| 7388 | ufshcd_enable_auto_bkops(hba); |
| 7389 | else |
| 7390 | /* |
| 7391 | * If BKOPs operations are urgently needed at this moment then |
| 7392 | * keep auto-bkops enabled or else disable it. |
| 7393 | */ |
| 7394 | ufshcd_urgent_bkops(hba); |
| 7395 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7396 | hba->clk_gating.is_suspended = false; |
| 7397 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 7398 | if (hba->clk_scaling.is_allowed) |
| 7399 | ufshcd_resume_clkscaling(hba); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 7400 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7401 | /* Schedule clock gating in case of no access to UFS device yet */ |
| 7402 | ufshcd_release(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7403 | goto out; |
| 7404 | |
| 7405 | set_old_link_state: |
| 7406 | ufshcd_link_state_transition(hba, old_link_state, 0); |
| 7407 | vendor_suspend: |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7408 | ufshcd_vops_suspend(hba, pm_op); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7409 | disable_vreg: |
| 7410 | ufshcd_vreg_set_lpm(hba); |
| 7411 | disable_irq_and_vops_clks: |
| 7412 | ufshcd_disable_irq(hba); |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 7413 | if (hba->clk_scaling.is_allowed) |
| 7414 | ufshcd_suspend_clkscaling(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7415 | ufshcd_setup_clocks(hba, false); |
| 7416 | out: |
| 7417 | hba->pm_op_in_progress = 0; |
| 7418 | return ret; |
| 7419 | } |
| 7420 | |
| 7421 | /** |
| 7422 | * ufshcd_system_suspend - system suspend routine |
| 7423 | * @hba: per adapter instance |
| 7424 | * @pm_op: runtime PM or system PM |
| 7425 | * |
| 7426 | * Check the description of ufshcd_suspend() function for more details. |
| 7427 | * |
| 7428 | * Returns 0 for success and non-zero for failure |
| 7429 | */ |
| 7430 | int ufshcd_system_suspend(struct ufs_hba *hba) |
| 7431 | { |
| 7432 | int ret = 0; |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7433 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7434 | |
| 7435 | if (!hba || !hba->is_powered) |
Dolev Raviv | 233b594 | 2014-10-23 13:25:14 +0300 | [diff] [blame] | 7436 | return 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7437 | |
subhashj@codeaurora.org | 0b25773 | 2016-11-23 16:33:08 -0800 | [diff] [blame] | 7438 | if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) == |
| 7439 | hba->curr_dev_pwr_mode) && |
| 7440 | (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) == |
| 7441 | hba->uic_link_state)) |
| 7442 | goto out; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7443 | |
subhashj@codeaurora.org | 0b25773 | 2016-11-23 16:33:08 -0800 | [diff] [blame] | 7444 | if (pm_runtime_suspended(hba->dev)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7445 | /* |
| 7446 | * UFS device and/or UFS link low power states during runtime |
| 7447 | * suspend seems to be different than what is expected during |
| 7448 | * system suspend. Hence runtime resume the devic & link and |
| 7449 | * let the system suspend low power states to take effect. |
| 7450 | * TODO: If resume takes longer time, we might have optimize |
| 7451 | * it in future by not resuming everything if possible. |
| 7452 | */ |
| 7453 | ret = ufshcd_runtime_resume(hba); |
| 7454 | if (ret) |
| 7455 | goto out; |
| 7456 | } |
| 7457 | |
| 7458 | ret = ufshcd_suspend(hba, UFS_SYSTEM_PM); |
| 7459 | out: |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7460 | trace_ufshcd_system_suspend(dev_name(hba->dev), ret, |
| 7461 | ktime_to_us(ktime_sub(ktime_get(), start)), |
Subhash Jadavani | 73eba2b | 2017-01-10 16:48:25 -0800 | [diff] [blame] | 7462 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
Dolev Raviv | e785060 | 2014-09-25 15:32:36 +0300 | [diff] [blame] | 7463 | if (!ret) |
| 7464 | hba->is_sys_suspended = true; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7465 | return ret; |
| 7466 | } |
| 7467 | EXPORT_SYMBOL(ufshcd_system_suspend); |
| 7468 | |
| 7469 | /** |
| 7470 | * ufshcd_system_resume - system resume routine |
| 7471 | * @hba: per adapter instance |
| 7472 | * |
| 7473 | * Returns 0 for success and non-zero for failure |
| 7474 | */ |
| 7475 | |
| 7476 | int ufshcd_system_resume(struct ufs_hba *hba) |
| 7477 | { |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7478 | int ret = 0; |
| 7479 | ktime_t start = ktime_get(); |
| 7480 | |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 7481 | if (!hba) |
| 7482 | return -EINVAL; |
| 7483 | |
| 7484 | if (!hba->is_powered || pm_runtime_suspended(hba->dev)) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7485 | /* |
| 7486 | * Let the runtime resume take care of resuming |
| 7487 | * if runtime suspended. |
| 7488 | */ |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7489 | goto out; |
| 7490 | else |
| 7491 | ret = ufshcd_resume(hba, UFS_SYSTEM_PM); |
| 7492 | out: |
| 7493 | trace_ufshcd_system_resume(dev_name(hba->dev), ret, |
| 7494 | ktime_to_us(ktime_sub(ktime_get(), start)), |
Subhash Jadavani | 73eba2b | 2017-01-10 16:48:25 -0800 | [diff] [blame] | 7495 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7496 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7497 | } |
| 7498 | EXPORT_SYMBOL(ufshcd_system_resume); |
| 7499 | |
| 7500 | /** |
| 7501 | * ufshcd_runtime_suspend - runtime suspend routine |
| 7502 | * @hba: per adapter instance |
| 7503 | * |
| 7504 | * Check the description of ufshcd_suspend() function for more details. |
| 7505 | * |
| 7506 | * Returns 0 for success and non-zero for failure |
| 7507 | */ |
| 7508 | int ufshcd_runtime_suspend(struct ufs_hba *hba) |
| 7509 | { |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7510 | int ret = 0; |
| 7511 | ktime_t start = ktime_get(); |
| 7512 | |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 7513 | if (!hba) |
| 7514 | return -EINVAL; |
| 7515 | |
| 7516 | if (!hba->is_powered) |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7517 | goto out; |
| 7518 | else |
| 7519 | ret = ufshcd_suspend(hba, UFS_RUNTIME_PM); |
| 7520 | out: |
| 7521 | trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret, |
| 7522 | ktime_to_us(ktime_sub(ktime_get(), start)), |
Subhash Jadavani | 73eba2b | 2017-01-10 16:48:25 -0800 | [diff] [blame] | 7523 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7524 | return ret; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 7525 | } |
| 7526 | EXPORT_SYMBOL(ufshcd_runtime_suspend); |
| 7527 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7528 | /** |
| 7529 | * ufshcd_runtime_resume - runtime resume routine |
| 7530 | * @hba: per adapter instance |
| 7531 | * |
| 7532 | * This function basically brings the UFS device, UniPro link and controller |
| 7533 | * to active state. Following operations are done in this function: |
| 7534 | * |
| 7535 | * 1. Turn on all the controller related clocks |
| 7536 | * 2. Bring the UniPro link out of Hibernate state |
| 7537 | * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device |
| 7538 | * to active state. |
| 7539 | * 4. If auto-bkops is enabled on the device, disable it. |
| 7540 | * |
| 7541 | * So following would be the possible power state after this function return |
| 7542 | * successfully: |
| 7543 | * S1: UFS device in Active state with VCC rail ON |
| 7544 | * UniPro link in Active state |
| 7545 | * All the UFS/UniPro controller clocks are ON |
| 7546 | * |
| 7547 | * Returns 0 for success and non-zero for failure |
| 7548 | */ |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 7549 | int ufshcd_runtime_resume(struct ufs_hba *hba) |
| 7550 | { |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7551 | int ret = 0; |
| 7552 | ktime_t start = ktime_get(); |
| 7553 | |
Yaniv Gardi | e3ce73d | 2016-10-17 17:09:24 -0700 | [diff] [blame] | 7554 | if (!hba) |
| 7555 | return -EINVAL; |
| 7556 | |
| 7557 | if (!hba->is_powered) |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7558 | goto out; |
| 7559 | else |
| 7560 | ret = ufshcd_resume(hba, UFS_RUNTIME_PM); |
| 7561 | out: |
| 7562 | trace_ufshcd_runtime_resume(dev_name(hba->dev), ret, |
| 7563 | ktime_to_us(ktime_sub(ktime_get(), start)), |
Subhash Jadavani | 73eba2b | 2017-01-10 16:48:25 -0800 | [diff] [blame] | 7564 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
subhashj@codeaurora.org | 7ff5ab4 | 2016-12-22 18:39:51 -0800 | [diff] [blame] | 7565 | return ret; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 7566 | } |
| 7567 | EXPORT_SYMBOL(ufshcd_runtime_resume); |
| 7568 | |
| 7569 | int ufshcd_runtime_idle(struct ufs_hba *hba) |
| 7570 | { |
| 7571 | return 0; |
| 7572 | } |
| 7573 | EXPORT_SYMBOL(ufshcd_runtime_idle); |
| 7574 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7575 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7576 | * ufshcd_shutdown - shutdown routine |
| 7577 | * @hba: per adapter instance |
| 7578 | * |
| 7579 | * This function would power off both UFS device and UFS link. |
| 7580 | * |
| 7581 | * Returns 0 always to allow force shutdown even in case of errors. |
| 7582 | */ |
| 7583 | int ufshcd_shutdown(struct ufs_hba *hba) |
| 7584 | { |
| 7585 | int ret = 0; |
| 7586 | |
| 7587 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba)) |
| 7588 | goto out; |
| 7589 | |
| 7590 | if (pm_runtime_suspended(hba->dev)) { |
| 7591 | ret = ufshcd_runtime_resume(hba); |
| 7592 | if (ret) |
| 7593 | goto out; |
| 7594 | } |
| 7595 | |
| 7596 | ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM); |
| 7597 | out: |
| 7598 | if (ret) |
| 7599 | dev_err(hba->dev, "%s failed, err %d\n", __func__, ret); |
| 7600 | /* allow force shutdown even in case of errors */ |
| 7601 | return 0; |
| 7602 | } |
| 7603 | EXPORT_SYMBOL(ufshcd_shutdown); |
| 7604 | |
| 7605 | /** |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7606 | * ufshcd_remove - de-allocate SCSI host and host memory space |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7607 | * data structure memory |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7608 | * @hba - per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7609 | */ |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7610 | void ufshcd_remove(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7611 | { |
Stanislav Nijnikov | cbb6813 | 2018-02-15 14:14:01 +0200 | [diff] [blame] | 7612 | ufs_sysfs_remove_nodes(hba->dev); |
Akinobu Mita | cfdf9c9 | 2013-07-30 00:36:03 +0530 | [diff] [blame] | 7613 | scsi_remove_host(hba->host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7614 | /* disable interrupts */ |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 7615 | ufshcd_disable_intr(hba, hba->intr_mask); |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 7616 | ufshcd_hba_stop(hba, true); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7617 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7618 | ufshcd_exit_clk_gating(hba); |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 7619 | if (ufshcd_is_clkscaling_supported(hba)) |
| 7620 | device_remove_file(hba->dev, &hba->clk_scaling.enable_attr); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7621 | ufshcd_hba_exit(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7622 | } |
| 7623 | EXPORT_SYMBOL_GPL(ufshcd_remove); |
| 7624 | |
| 7625 | /** |
Yaniv Gardi | 47555a5 | 2015-10-28 13:15:49 +0200 | [diff] [blame] | 7626 | * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA) |
| 7627 | * @hba: pointer to Host Bus Adapter (HBA) |
| 7628 | */ |
| 7629 | void ufshcd_dealloc_host(struct ufs_hba *hba) |
| 7630 | { |
| 7631 | scsi_host_put(hba->host); |
| 7632 | } |
| 7633 | EXPORT_SYMBOL_GPL(ufshcd_dealloc_host); |
| 7634 | |
| 7635 | /** |
Akinobu Mita | ca3d7bf | 2014-07-13 21:24:46 +0900 | [diff] [blame] | 7636 | * ufshcd_set_dma_mask - Set dma mask based on the controller |
| 7637 | * addressing capability |
| 7638 | * @hba: per adapter instance |
| 7639 | * |
| 7640 | * Returns 0 for success, non-zero for failure |
| 7641 | */ |
| 7642 | static int ufshcd_set_dma_mask(struct ufs_hba *hba) |
| 7643 | { |
| 7644 | if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) { |
| 7645 | if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64))) |
| 7646 | return 0; |
| 7647 | } |
| 7648 | return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32)); |
| 7649 | } |
| 7650 | |
| 7651 | /** |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7652 | * ufshcd_alloc_host - allocate Host Bus Adapter (HBA) |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7653 | * @dev: pointer to device handle |
| 7654 | * @hba_handle: driver private handle |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7655 | * Returns 0 on success, non-zero value on failure |
| 7656 | */ |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7657 | int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7658 | { |
| 7659 | struct Scsi_Host *host; |
| 7660 | struct ufs_hba *hba; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7661 | int err = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7662 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7663 | if (!dev) { |
| 7664 | dev_err(dev, |
| 7665 | "Invalid memory reference for dev is NULL\n"); |
| 7666 | err = -ENODEV; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7667 | goto out_error; |
| 7668 | } |
| 7669 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7670 | host = scsi_host_alloc(&ufshcd_driver_template, |
| 7671 | sizeof(struct ufs_hba)); |
| 7672 | if (!host) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7673 | dev_err(dev, "scsi_host_alloc failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7674 | err = -ENOMEM; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7675 | goto out_error; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7676 | } |
| 7677 | hba = shost_priv(host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7678 | hba->host = host; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7679 | hba->dev = dev; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7680 | *hba_handle = hba; |
| 7681 | |
Szymon Mielczarek | 566ec9a | 2017-06-05 11:36:54 +0300 | [diff] [blame] | 7682 | INIT_LIST_HEAD(&hba->clk_list_head); |
| 7683 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7684 | out_error: |
| 7685 | return err; |
| 7686 | } |
| 7687 | EXPORT_SYMBOL(ufshcd_alloc_host); |
| 7688 | |
| 7689 | /** |
| 7690 | * ufshcd_init - Driver initialization routine |
| 7691 | * @hba: per-adapter instance |
| 7692 | * @mmio_base: base register address |
| 7693 | * @irq: Interrupt line of device |
| 7694 | * Returns 0 on success, non-zero value on failure |
| 7695 | */ |
| 7696 | int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq) |
| 7697 | { |
| 7698 | int err; |
| 7699 | struct Scsi_Host *host = hba->host; |
| 7700 | struct device *dev = hba->dev; |
| 7701 | |
| 7702 | if (!mmio_base) { |
| 7703 | dev_err(hba->dev, |
| 7704 | "Invalid memory reference for mmio_base is NULL\n"); |
| 7705 | err = -ENODEV; |
| 7706 | goto out_error; |
| 7707 | } |
| 7708 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7709 | hba->mmio_base = mmio_base; |
| 7710 | hba->irq = irq; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7711 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 7712 | /* Set descriptor lengths to specification defaults */ |
| 7713 | ufshcd_def_desc_sizes(hba); |
| 7714 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7715 | err = ufshcd_hba_init(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7716 | if (err) |
| 7717 | goto out_error; |
| 7718 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7719 | /* Read capabilities registers */ |
| 7720 | ufshcd_hba_capabilities(hba); |
| 7721 | |
| 7722 | /* Get UFS version supported by the controller */ |
| 7723 | hba->ufs_version = ufshcd_get_ufs_version(hba); |
| 7724 | |
Yaniv Gardi | c01848c | 2016-12-05 19:25:02 -0800 | [diff] [blame] | 7725 | if ((hba->ufs_version != UFSHCI_VERSION_10) && |
| 7726 | (hba->ufs_version != UFSHCI_VERSION_11) && |
| 7727 | (hba->ufs_version != UFSHCI_VERSION_20) && |
| 7728 | (hba->ufs_version != UFSHCI_VERSION_21)) |
| 7729 | dev_err(hba->dev, "invalid UFS version 0x%x\n", |
| 7730 | hba->ufs_version); |
| 7731 | |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 7732 | /* Get Interrupt bit mask per version */ |
| 7733 | hba->intr_mask = ufshcd_get_intr_mask(hba); |
| 7734 | |
Akinobu Mita | ca3d7bf | 2014-07-13 21:24:46 +0900 | [diff] [blame] | 7735 | err = ufshcd_set_dma_mask(hba); |
| 7736 | if (err) { |
| 7737 | dev_err(hba->dev, "set dma mask failed\n"); |
| 7738 | goto out_disable; |
| 7739 | } |
| 7740 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7741 | /* Allocate memory for host memory space */ |
| 7742 | err = ufshcd_memory_alloc(hba); |
| 7743 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7744 | dev_err(hba->dev, "Memory allocation failed\n"); |
| 7745 | goto out_disable; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7746 | } |
| 7747 | |
| 7748 | /* Configure LRB */ |
| 7749 | ufshcd_host_memory_configure(hba); |
| 7750 | |
| 7751 | host->can_queue = hba->nutrs; |
| 7752 | host->cmd_per_lun = hba->nutrs; |
| 7753 | host->max_id = UFSHCD_MAX_ID; |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 7754 | host->max_lun = UFS_MAX_LUNS; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7755 | host->max_channel = UFSHCD_MAX_CHANNEL; |
| 7756 | host->unique_id = host->host_no; |
| 7757 | host->max_cmd_len = MAX_CDB_SIZE; |
| 7758 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 7759 | hba->max_pwr_info.is_valid = false; |
| 7760 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7761 | /* Initailize wait queue for task management */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 7762 | init_waitqueue_head(&hba->tm_wq); |
| 7763 | init_waitqueue_head(&hba->tm_tag_wq); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7764 | |
| 7765 | /* Initialize work queues */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 7766 | INIT_WORK(&hba->eh_work, ufshcd_err_handler); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 7767 | INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7768 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7769 | /* Initialize UIC command mutex */ |
| 7770 | mutex_init(&hba->uic_cmd_mutex); |
| 7771 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 7772 | /* Initialize mutex for device management commands */ |
| 7773 | mutex_init(&hba->dev_cmd.lock); |
| 7774 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 7775 | init_rwsem(&hba->clk_scaling_lock); |
| 7776 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 7777 | /* Initialize device management tag acquire wait queue */ |
| 7778 | init_waitqueue_head(&hba->dev_cmd.tag_wq); |
| 7779 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7780 | ufshcd_init_clk_gating(hba); |
Yaniv Gardi | 199ef13 | 2016-03-10 17:37:06 +0200 | [diff] [blame] | 7781 | |
| 7782 | /* |
| 7783 | * In order to avoid any spurious interrupt immediately after |
| 7784 | * registering UFS controller interrupt handler, clear any pending UFS |
| 7785 | * interrupt status and disable all the UFS interrupts. |
| 7786 | */ |
| 7787 | ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS), |
| 7788 | REG_INTERRUPT_STATUS); |
| 7789 | ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE); |
| 7790 | /* |
| 7791 | * Make sure that UFS interrupts are disabled and any pending interrupt |
| 7792 | * status is cleared before registering UFS interrupt handler. |
| 7793 | */ |
| 7794 | mb(); |
| 7795 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7796 | /* IRQ registration */ |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 7797 | err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7798 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7799 | dev_err(hba->dev, "request irq failed\n"); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7800 | goto exit_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7801 | } else { |
| 7802 | hba->is_irq_enabled = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7803 | } |
| 7804 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7805 | err = scsi_add_host(host, hba->dev); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7806 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7807 | dev_err(hba->dev, "scsi_add_host failed\n"); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7808 | goto exit_gating; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7809 | } |
| 7810 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7811 | /* Host controller enable */ |
| 7812 | err = ufshcd_hba_enable(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7813 | if (err) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7814 | dev_err(hba->dev, "Host controller enable failed\n"); |
Dolev Raviv | 66cc820 | 2016-12-22 18:39:42 -0800 | [diff] [blame] | 7815 | ufshcd_print_host_regs(hba); |
Gilad Broner | 6ba6558 | 2017-02-03 16:57:28 -0800 | [diff] [blame] | 7816 | ufshcd_print_host_state(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7817 | goto out_remove_scsi_host; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7818 | } |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7819 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 7820 | if (ufshcd_is_clkscaling_supported(hba)) { |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 7821 | char wq_name[sizeof("ufs_clkscaling_00")]; |
| 7822 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 7823 | INIT_WORK(&hba->clk_scaling.suspend_work, |
| 7824 | ufshcd_clk_scaling_suspend_work); |
| 7825 | INIT_WORK(&hba->clk_scaling.resume_work, |
| 7826 | ufshcd_clk_scaling_resume_work); |
| 7827 | |
Tomohiro Kusumi | d985c6ea | 2017-03-28 16:49:29 +0300 | [diff] [blame] | 7828 | snprintf(wq_name, sizeof(wq_name), "ufs_clkscaling_%d", |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 7829 | host->host_no); |
| 7830 | hba->clk_scaling.workq = create_singlethread_workqueue(wq_name); |
| 7831 | |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 7832 | ufshcd_clkscaling_init_sysfs(hba); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 7833 | } |
| 7834 | |
subhashj@codeaurora.org | 0c8f758 | 2016-12-22 18:41:11 -0800 | [diff] [blame] | 7835 | /* |
| 7836 | * Set the default power management level for runtime and system PM. |
| 7837 | * Default power saving mode is to keep UFS link in Hibern8 state |
| 7838 | * and UFS device in sleep state. |
| 7839 | */ |
| 7840 | hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state( |
| 7841 | UFS_SLEEP_PWR_MODE, |
| 7842 | UIC_LINK_HIBERN8_STATE); |
| 7843 | hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state( |
| 7844 | UFS_SLEEP_PWR_MODE, |
| 7845 | UIC_LINK_HIBERN8_STATE); |
| 7846 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 7847 | /* Hold auto suspend until async scan completes */ |
| 7848 | pm_runtime_get_sync(dev); |
| 7849 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7850 | /* |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 7851 | * We are assuming that device wasn't put in sleep/power-down |
| 7852 | * state exclusively during the boot stage before kernel. |
| 7853 | * This assumption helps avoid doing link startup twice during |
| 7854 | * ufshcd_probe_hba(). |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7855 | */ |
subhashj@codeaurora.org | 7caf489 | 2016-11-23 16:32:20 -0800 | [diff] [blame] | 7856 | ufshcd_set_ufs_dev_active(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7857 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7858 | async_schedule(ufshcd_async_scan, hba); |
Stanislav Nijnikov | cbb6813 | 2018-02-15 14:14:01 +0200 | [diff] [blame] | 7859 | ufs_sysfs_add_nodes(hba->dev); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7860 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7861 | return 0; |
| 7862 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7863 | out_remove_scsi_host: |
| 7864 | scsi_remove_host(hba->host); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7865 | exit_gating: |
| 7866 | ufshcd_exit_clk_gating(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7867 | out_disable: |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7868 | hba->is_irq_enabled = false; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7869 | ufshcd_hba_exit(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7870 | out_error: |
| 7871 | return err; |
| 7872 | } |
| 7873 | EXPORT_SYMBOL_GPL(ufshcd_init); |
| 7874 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7875 | MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>"); |
| 7876 | MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>"); |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 7877 | MODULE_DESCRIPTION("Generic UFS host controller driver Core"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7878 | MODULE_LICENSE("GPL"); |
| 7879 | MODULE_VERSION(UFSHCD_DRIVER_VERSION); |