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> |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 41 | #include <scsi/ufs/ioctl.h> |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 42 | #include <linux/devfreq.h> |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 43 | #include <linux/nls.h> |
Yaniv Gardi | 54b879b | 2016-03-10 17:37:05 +0200 | [diff] [blame] | 44 | #include <linux/of.h> |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 45 | #include <linux/blkdev.h> |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 46 | #include "ufshcd.h" |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 47 | #include "ufshci.h" |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 48 | #include "ufs_quirks.h" |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 49 | #include "ufs-debugfs.h" |
| 50 | |
| 51 | #define CREATE_TRACE_POINTS |
| 52 | #include <trace/events/ufs.h> |
| 53 | |
| 54 | #ifdef CONFIG_DEBUG_FS |
| 55 | |
| 56 | static int ufshcd_tag_req_type(struct request *rq) |
| 57 | { |
| 58 | int rq_type = TS_WRITE; |
| 59 | |
| 60 | if (!rq || !(rq->cmd_type & REQ_TYPE_FS)) |
| 61 | rq_type = TS_NOT_SUPPORTED; |
| 62 | else if (rq->cmd_flags & REQ_PREFLUSH) |
| 63 | rq_type = TS_FLUSH; |
| 64 | else if (rq_data_dir(rq) == READ) |
| 65 | rq_type = (rq->cmd_flags & REQ_URGENT) ? |
| 66 | TS_URGENT_READ : TS_READ; |
| 67 | else if (rq->cmd_flags & REQ_URGENT) |
| 68 | rq_type = TS_URGENT_WRITE; |
| 69 | |
| 70 | return rq_type; |
| 71 | } |
| 72 | |
| 73 | static void ufshcd_update_error_stats(struct ufs_hba *hba, int type) |
| 74 | { |
| 75 | ufsdbg_set_err_state(hba); |
| 76 | if (type < UFS_ERR_MAX) |
| 77 | hba->ufs_stats.err_stats[type]++; |
| 78 | } |
| 79 | |
| 80 | static void ufshcd_update_tag_stats(struct ufs_hba *hba, int tag) |
| 81 | { |
| 82 | struct request *rq = |
| 83 | hba->lrb[tag].cmd ? hba->lrb[tag].cmd->request : NULL; |
| 84 | u64 **tag_stats = hba->ufs_stats.tag_stats; |
| 85 | int rq_type; |
| 86 | |
| 87 | if (!hba->ufs_stats.enabled) |
| 88 | return; |
| 89 | |
| 90 | tag_stats[tag][TS_TAG]++; |
| 91 | if (!rq || !(rq->cmd_type & REQ_TYPE_FS)) |
| 92 | return; |
| 93 | |
| 94 | WARN_ON(hba->ufs_stats.q_depth > hba->nutrs); |
| 95 | rq_type = ufshcd_tag_req_type(rq); |
| 96 | if (!(rq_type < 0 || rq_type > TS_NUM_STATS)) |
| 97 | tag_stats[hba->ufs_stats.q_depth++][rq_type]++; |
| 98 | } |
| 99 | |
| 100 | static void ufshcd_update_tag_stats_completion(struct ufs_hba *hba, |
| 101 | struct scsi_cmnd *cmd) |
| 102 | { |
| 103 | struct request *rq = cmd ? cmd->request : NULL; |
| 104 | |
| 105 | if (rq && rq->cmd_type & REQ_TYPE_FS) |
| 106 | hba->ufs_stats.q_depth--; |
| 107 | } |
| 108 | |
| 109 | static void update_req_stats(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 110 | { |
| 111 | int rq_type; |
| 112 | struct request *rq = lrbp->cmd ? lrbp->cmd->request : NULL; |
| 113 | s64 delta = ktime_us_delta(lrbp->complete_time_stamp, |
| 114 | lrbp->issue_time_stamp); |
| 115 | |
| 116 | /* update general request statistics */ |
| 117 | if (hba->ufs_stats.req_stats[TS_TAG].count == 0) |
| 118 | hba->ufs_stats.req_stats[TS_TAG].min = delta; |
| 119 | hba->ufs_stats.req_stats[TS_TAG].count++; |
| 120 | hba->ufs_stats.req_stats[TS_TAG].sum += delta; |
| 121 | if (delta > hba->ufs_stats.req_stats[TS_TAG].max) |
| 122 | hba->ufs_stats.req_stats[TS_TAG].max = delta; |
| 123 | if (delta < hba->ufs_stats.req_stats[TS_TAG].min) |
| 124 | hba->ufs_stats.req_stats[TS_TAG].min = delta; |
| 125 | |
| 126 | rq_type = ufshcd_tag_req_type(rq); |
| 127 | if (rq_type == TS_NOT_SUPPORTED) |
| 128 | return; |
| 129 | |
| 130 | /* update request type specific statistics */ |
| 131 | if (hba->ufs_stats.req_stats[rq_type].count == 0) |
| 132 | hba->ufs_stats.req_stats[rq_type].min = delta; |
| 133 | hba->ufs_stats.req_stats[rq_type].count++; |
| 134 | hba->ufs_stats.req_stats[rq_type].sum += delta; |
| 135 | if (delta > hba->ufs_stats.req_stats[rq_type].max) |
| 136 | hba->ufs_stats.req_stats[rq_type].max = delta; |
| 137 | if (delta < hba->ufs_stats.req_stats[rq_type].min) |
| 138 | hba->ufs_stats.req_stats[rq_type].min = delta; |
| 139 | } |
| 140 | |
| 141 | static void |
| 142 | ufshcd_update_query_stats(struct ufs_hba *hba, enum query_opcode opcode, u8 idn) |
| 143 | { |
| 144 | if (opcode < UPIU_QUERY_OPCODE_MAX && idn < MAX_QUERY_IDN) |
| 145 | hba->ufs_stats.query_stats_arr[opcode][idn]++; |
| 146 | } |
| 147 | |
| 148 | #else |
| 149 | static inline void ufshcd_update_tag_stats(struct ufs_hba *hba, int tag) |
| 150 | { |
| 151 | } |
| 152 | |
| 153 | static inline void ufshcd_update_tag_stats_completion(struct ufs_hba *hba, |
| 154 | struct scsi_cmnd *cmd) |
| 155 | { |
| 156 | } |
| 157 | |
| 158 | static inline void ufshcd_update_error_stats(struct ufs_hba *hba, int type) |
| 159 | { |
| 160 | } |
| 161 | |
| 162 | static inline |
| 163 | void update_req_stats(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 164 | { |
| 165 | } |
| 166 | |
| 167 | static inline |
| 168 | void ufshcd_update_query_stats(struct ufs_hba *hba, |
| 169 | enum query_opcode opcode, u8 idn) |
| 170 | { |
| 171 | } |
| 172 | #endif |
| 173 | |
| 174 | #define UFSHCD_REQ_SENSE_SIZE 18 |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 175 | |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 176 | #define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\ |
| 177 | UTP_TASK_REQ_COMPL |\ |
| 178 | UFSHCD_ERROR_MASK) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 179 | /* UIC command timeout, unit: ms */ |
| 180 | #define UIC_CMD_TIMEOUT 500 |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 181 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 182 | /* NOP OUT retries waiting for NOP IN response */ |
| 183 | #define NOP_OUT_RETRIES 10 |
| 184 | /* Timeout after 30 msecs if NOP OUT hangs without response */ |
| 185 | #define NOP_OUT_TIMEOUT 30 /* msecs */ |
| 186 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 187 | /* Query request retries */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 188 | #define QUERY_REQ_RETRIES 3 |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 189 | /* Query request timeout */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 190 | #define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */ |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 191 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 192 | /* Task management command timeout */ |
| 193 | #define TM_CMD_TIMEOUT 100 /* msecs */ |
| 194 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 195 | /* maximum number of retries for a general UIC command */ |
| 196 | #define UFS_UIC_COMMAND_RETRIES 3 |
| 197 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 198 | /* maximum number of link-startup retries */ |
| 199 | #define DME_LINKSTARTUP_RETRIES 3 |
| 200 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 201 | /* Maximum retries for Hibern8 enter */ |
| 202 | #define UIC_HIBERN8_ENTER_RETRIES 3 |
| 203 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 204 | /* maximum number of reset retries before giving up */ |
| 205 | #define MAX_HOST_RESET_RETRIES 5 |
| 206 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 207 | /* Expose the flag value from utp_upiu_query.value */ |
| 208 | #define MASK_QUERY_UPIU_FLAG_LOC 0xFF |
| 209 | |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 210 | /* Interrupt aggregation default timeout, unit: 40us */ |
| 211 | #define INT_AGGR_DEF_TO 0x02 |
| 212 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 213 | /* default value of auto suspend is 3 seconds */ |
| 214 | #define UFSHCD_AUTO_SUSPEND_DELAY_MS 3000 /* millisecs */ |
| 215 | |
| 216 | #define UFSHCD_CLK_GATING_DELAY_MS_PWR_SAVE 10 |
| 217 | #define UFSHCD_CLK_GATING_DELAY_MS_PERF 50 |
| 218 | |
| 219 | /* IOCTL opcode for command - ufs set device read only */ |
| 220 | #define UFS_IOCTL_BLKROSET BLKROSET |
| 221 | |
| 222 | #define UFSHCD_DEFAULT_LANES_PER_DIRECTION 2 |
| 223 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 224 | #define ufshcd_toggle_vreg(_dev, _vreg, _on) \ |
| 225 | ({ \ |
| 226 | int _ret; \ |
| 227 | if (_on) \ |
| 228 | _ret = ufshcd_enable_vreg(_dev, _vreg); \ |
| 229 | else \ |
| 230 | _ret = ufshcd_disable_vreg(_dev, _vreg); \ |
| 231 | _ret; \ |
| 232 | }) |
| 233 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 234 | #define ufshcd_hex_dump(prefix_str, buf, len) \ |
| 235 | print_hex_dump(KERN_ERR, prefix_str, DUMP_PREFIX_OFFSET, 16, 4, buf, len, false) |
| 236 | |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 237 | static u32 ufs_query_desc_max_size[] = { |
| 238 | QUERY_DESC_DEVICE_MAX_SIZE, |
| 239 | QUERY_DESC_CONFIGURAION_MAX_SIZE, |
| 240 | QUERY_DESC_UNIT_MAX_SIZE, |
| 241 | QUERY_DESC_RFU_MAX_SIZE, |
| 242 | QUERY_DESC_INTERCONNECT_MAX_SIZE, |
| 243 | QUERY_DESC_STRING_MAX_SIZE, |
| 244 | QUERY_DESC_RFU_MAX_SIZE, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 245 | QUERY_DESC_GEOMETRY_MAZ_SIZE, |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 246 | QUERY_DESC_POWER_MAX_SIZE, |
| 247 | QUERY_DESC_RFU_MAX_SIZE, |
| 248 | }; |
| 249 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 250 | enum { |
| 251 | UFSHCD_MAX_CHANNEL = 0, |
| 252 | UFSHCD_MAX_ID = 1, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 253 | UFSHCD_CMD_PER_LUN = 32, |
| 254 | UFSHCD_CAN_QUEUE = 32, |
| 255 | }; |
| 256 | |
| 257 | /* UFSHCD states */ |
| 258 | enum { |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 259 | UFSHCD_STATE_RESET, |
| 260 | UFSHCD_STATE_ERROR, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 261 | UFSHCD_STATE_OPERATIONAL, |
| 262 | }; |
| 263 | |
| 264 | /* UFSHCD error handling flags */ |
| 265 | enum { |
| 266 | UFSHCD_EH_IN_PROGRESS = (1 << 0), |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 267 | }; |
| 268 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 269 | /* UFSHCD UIC layer error flags */ |
| 270 | enum { |
| 271 | UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 272 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */ |
| 273 | UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */ |
| 274 | UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */ |
| 275 | UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */ |
| 276 | UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 277 | }; |
| 278 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 279 | /* Interrupt configuration options */ |
| 280 | enum { |
| 281 | UFSHCD_INT_DISABLE, |
| 282 | UFSHCD_INT_ENABLE, |
| 283 | UFSHCD_INT_CLEAR, |
| 284 | }; |
| 285 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 286 | #define DEFAULT_UFSHCD_DBG_PRINT_EN UFSHCD_DBG_PRINT_ALL |
| 287 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 288 | #define ufshcd_set_eh_in_progress(h) \ |
| 289 | (h->eh_flags |= UFSHCD_EH_IN_PROGRESS) |
| 290 | #define ufshcd_eh_in_progress(h) \ |
| 291 | (h->eh_flags & UFSHCD_EH_IN_PROGRESS) |
| 292 | #define ufshcd_clear_eh_in_progress(h) \ |
| 293 | (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS) |
| 294 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 295 | #define ufshcd_set_ufs_dev_active(h) \ |
| 296 | ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE) |
| 297 | #define ufshcd_set_ufs_dev_sleep(h) \ |
| 298 | ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE) |
| 299 | #define ufshcd_set_ufs_dev_poweroff(h) \ |
| 300 | ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE) |
| 301 | #define ufshcd_is_ufs_dev_active(h) \ |
| 302 | ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE) |
| 303 | #define ufshcd_is_ufs_dev_sleep(h) \ |
| 304 | ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE) |
| 305 | #define ufshcd_is_ufs_dev_poweroff(h) \ |
| 306 | ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE) |
| 307 | |
| 308 | static struct ufs_pm_lvl_states ufs_pm_lvl_states[] = { |
| 309 | {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE}, |
| 310 | {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 311 | {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE}, |
| 312 | {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 313 | {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 314 | {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE}, |
| 315 | }; |
| 316 | |
| 317 | static inline enum ufs_dev_pwr_mode |
| 318 | ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl) |
| 319 | { |
| 320 | return ufs_pm_lvl_states[lvl].dev_state; |
| 321 | } |
| 322 | |
| 323 | static inline enum uic_link_state |
| 324 | ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl) |
| 325 | { |
| 326 | return ufs_pm_lvl_states[lvl].link_state; |
| 327 | } |
| 328 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 329 | static inline enum ufs_pm_level |
| 330 | ufs_get_desired_pm_lvl_for_dev_link_state(enum ufs_dev_pwr_mode dev_state, |
| 331 | enum uic_link_state link_state) |
| 332 | { |
| 333 | enum ufs_pm_level lvl; |
| 334 | |
| 335 | for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) { |
| 336 | if ((ufs_pm_lvl_states[lvl].dev_state == dev_state) && |
| 337 | (ufs_pm_lvl_states[lvl].link_state == link_state)) |
| 338 | return lvl; |
| 339 | } |
| 340 | |
| 341 | /* if no match found, return the level 0 */ |
| 342 | return UFS_PM_LVL_0; |
| 343 | } |
| 344 | |
| 345 | static inline bool ufshcd_is_valid_pm_lvl(int lvl) |
| 346 | { |
| 347 | if (lvl >= 0 && lvl < ARRAY_SIZE(ufs_pm_lvl_states)) |
| 348 | return true; |
| 349 | else |
| 350 | return false; |
| 351 | } |
| 352 | |
| 353 | static irqreturn_t ufshcd_intr(int irq, void *__hba); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 354 | static void ufshcd_tmc_handler(struct ufs_hba *hba); |
| 355 | static void ufshcd_async_scan(void *data, async_cookie_t cookie); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 356 | static int ufshcd_reset_and_restore(struct ufs_hba *hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 357 | static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 358 | 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] | 359 | static void ufshcd_hba_exit(struct ufs_hba *hba); |
| 360 | static int ufshcd_probe_hba(struct ufs_hba *hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 361 | static int ufshcd_enable_clocks(struct ufs_hba *hba); |
| 362 | static int ufshcd_disable_clocks(struct ufs_hba *hba, |
| 363 | bool is_gating_context); |
| 364 | static int ufshcd_disable_clocks_skip_ref_clk(struct ufs_hba *hba, |
| 365 | bool is_gating_context); |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 366 | static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 367 | static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 368 | static inline void ufshcd_save_tstamp_of_last_dme_cmd(struct ufs_hba *hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 369 | static int ufshcd_host_reset_and_restore(struct ufs_hba *hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 370 | static void ufshcd_resume_clkscaling(struct ufs_hba *hba); |
| 371 | static void ufshcd_suspend_clkscaling(struct ufs_hba *hba); |
| 372 | static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba); |
| 373 | static void ufshcd_release_all(struct ufs_hba *hba); |
| 374 | static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba); |
| 375 | static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba); |
| 376 | |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 377 | static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag) |
| 378 | { |
| 379 | return tag >= 0 && tag < hba->nutrs; |
| 380 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 381 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 382 | static inline void ufshcd_enable_irq(struct ufs_hba *hba) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 383 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 384 | if (!hba->is_irq_enabled) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 385 | enable_irq(hba->irq); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 386 | hba->is_irq_enabled = true; |
| 387 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | static inline void ufshcd_disable_irq(struct ufs_hba *hba) |
| 391 | { |
| 392 | if (hba->is_irq_enabled) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 393 | disable_irq(hba->irq); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 394 | hba->is_irq_enabled = false; |
| 395 | } |
| 396 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 397 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 398 | void ufshcd_scsi_unblock_requests(struct ufs_hba *hba) |
| 399 | { |
| 400 | unsigned long flags; |
| 401 | bool unblock = false; |
| 402 | |
| 403 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 404 | hba->scsi_block_reqs_cnt--; |
| 405 | unblock = !hba->scsi_block_reqs_cnt; |
| 406 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 407 | if (unblock) |
| 408 | scsi_unblock_requests(hba->host); |
| 409 | } |
| 410 | EXPORT_SYMBOL(ufshcd_scsi_unblock_requests); |
| 411 | |
| 412 | static inline void __ufshcd_scsi_block_requests(struct ufs_hba *hba) |
| 413 | { |
| 414 | if (!hba->scsi_block_reqs_cnt++) |
| 415 | scsi_block_requests(hba->host); |
| 416 | } |
| 417 | |
| 418 | void ufshcd_scsi_block_requests(struct ufs_hba *hba) |
| 419 | { |
| 420 | unsigned long flags; |
| 421 | |
| 422 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 423 | __ufshcd_scsi_block_requests(hba); |
| 424 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 425 | } |
| 426 | EXPORT_SYMBOL(ufshcd_scsi_block_requests); |
| 427 | |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 428 | /* replace non-printable or non-ASCII characters with spaces */ |
| 429 | static inline void ufshcd_remove_non_printable(char *val) |
| 430 | { |
| 431 | if (!val) |
| 432 | return; |
| 433 | |
| 434 | if (*val < 0x20 || *val > 0x7e) |
| 435 | *val = ' '; |
| 436 | } |
| 437 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 438 | #ifdef CONFIG_TRACEPOINTS |
| 439 | static void ufshcd_add_command_trace(struct ufs_hba *hba, |
| 440 | unsigned int tag, const char *str) |
| 441 | { |
| 442 | sector_t lba = -1; |
| 443 | u8 opcode = 0; |
| 444 | u32 intr, doorbell; |
| 445 | struct ufshcd_lrb *lrbp; |
| 446 | int transfer_len = -1; |
| 447 | |
| 448 | lrbp = &hba->lrb[tag]; |
| 449 | |
| 450 | if (lrbp->cmd) { /* data phase exists */ |
| 451 | opcode = (u8)(*lrbp->cmd->cmnd); |
| 452 | if ((opcode == READ_10) || (opcode == WRITE_10)) { |
| 453 | /* |
| 454 | * Currently we only fully trace read(10) and write(10) |
| 455 | * commands |
| 456 | */ |
| 457 | if (lrbp->cmd->request && lrbp->cmd->request->bio) |
| 458 | lba = |
| 459 | lrbp->cmd->request->bio->bi_iter.bi_sector; |
| 460 | transfer_len = be32_to_cpu( |
| 461 | lrbp->ucd_req_ptr->sc.exp_data_transfer_len); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | intr = ufshcd_readl(hba, REG_INTERRUPT_STATUS); |
| 466 | doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 467 | trace_ufshcd_command(dev_name(hba->dev), str, tag, |
| 468 | doorbell, transfer_len, intr, lba, opcode); |
| 469 | } |
| 470 | |
| 471 | static inline void ufshcd_cond_add_cmd_trace(struct ufs_hba *hba, |
| 472 | unsigned int tag, const char *str) |
| 473 | { |
| 474 | if (trace_ufshcd_command_enabled()) |
| 475 | ufshcd_add_command_trace(hba, tag, str); |
| 476 | } |
| 477 | #else |
| 478 | static inline void ufshcd_cond_add_cmd_trace(struct ufs_hba *hba, |
| 479 | unsigned int tag, const char *str) |
| 480 | { |
| 481 | } |
| 482 | #endif |
| 483 | |
| 484 | static void ufshcd_print_clk_freqs(struct ufs_hba *hba) |
| 485 | { |
| 486 | struct ufs_clk_info *clki; |
| 487 | struct list_head *head = &hba->clk_list_head; |
| 488 | |
| 489 | if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_CLK_FREQ_EN)) |
| 490 | return; |
| 491 | |
| 492 | if (!head || list_empty(head)) |
| 493 | return; |
| 494 | |
| 495 | list_for_each_entry(clki, head, list) { |
| 496 | if (!IS_ERR_OR_NULL(clki->clk) && clki->min_freq && |
| 497 | clki->max_freq) |
| 498 | dev_err(hba->dev, "clk: %s, rate: %u\n", |
| 499 | clki->name, clki->curr_freq); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | static void ufshcd_print_uic_err_hist(struct ufs_hba *hba, |
| 504 | struct ufs_uic_err_reg_hist *err_hist, char *err_name) |
| 505 | { |
| 506 | int i; |
| 507 | |
| 508 | if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_UIC_ERR_HIST_EN)) |
| 509 | return; |
| 510 | |
| 511 | for (i = 0; i < UIC_ERR_REG_HIST_LENGTH; i++) { |
| 512 | int p = (i + err_hist->pos - 1) % UIC_ERR_REG_HIST_LENGTH; |
| 513 | |
| 514 | if (err_hist->reg[p] == 0) |
| 515 | continue; |
| 516 | dev_err(hba->dev, "%s[%d] = 0x%x at %lld us", err_name, i, |
| 517 | err_hist->reg[p], ktime_to_us(err_hist->tstamp[p])); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | static void ufshcd_print_host_regs(struct ufs_hba *hba) |
| 522 | { |
| 523 | if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_HOST_REGS_EN)) |
| 524 | return; |
| 525 | |
| 526 | /* |
| 527 | * hex_dump reads its data without the readl macro. This might |
| 528 | * cause inconsistency issues on some platform, as the printed |
| 529 | * values may be from cache and not the most recent value. |
| 530 | * To know whether you are looking at an un-cached version verify |
| 531 | * that IORESOURCE_MEM flag is on when xxx_get_resource() is invoked |
| 532 | * during platform/pci probe function. |
| 533 | */ |
| 534 | ufshcd_hex_dump("host regs: ", hba->mmio_base, UFSHCI_REG_SPACE_SIZE); |
| 535 | dev_err(hba->dev, "hba->ufs_version = 0x%x, hba->capabilities = 0x%x", |
| 536 | hba->ufs_version, hba->capabilities); |
| 537 | dev_err(hba->dev, |
| 538 | "hba->outstanding_reqs = 0x%x, hba->outstanding_tasks = 0x%x", |
| 539 | (u32)hba->outstanding_reqs, (u32)hba->outstanding_tasks); |
| 540 | dev_err(hba->dev, |
| 541 | "last_hibern8_exit_tstamp at %lld us, hibern8_exit_cnt = %d", |
| 542 | ktime_to_us(hba->ufs_stats.last_hibern8_exit_tstamp), |
| 543 | hba->ufs_stats.hibern8_exit_cnt); |
| 544 | |
| 545 | ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.pa_err, "pa_err"); |
| 546 | ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.dl_err, "dl_err"); |
| 547 | ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.nl_err, "nl_err"); |
| 548 | ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.tl_err, "tl_err"); |
| 549 | ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.dme_err, "dme_err"); |
| 550 | |
| 551 | ufshcd_print_clk_freqs(hba); |
| 552 | |
| 553 | ufshcd_vops_dbg_register_dump(hba); |
| 554 | } |
| 555 | |
| 556 | static |
| 557 | void ufshcd_print_trs(struct ufs_hba *hba, unsigned long bitmap, bool pr_prdt) |
| 558 | { |
| 559 | struct ufshcd_lrb *lrbp; |
| 560 | int prdt_length; |
| 561 | int tag; |
| 562 | |
| 563 | if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_TRS_EN)) |
| 564 | return; |
| 565 | |
| 566 | for_each_set_bit(tag, &bitmap, hba->nutrs) { |
| 567 | lrbp = &hba->lrb[tag]; |
| 568 | |
| 569 | dev_err(hba->dev, "UPIU[%d] - issue time %lld us", |
| 570 | tag, ktime_to_us(lrbp->issue_time_stamp)); |
| 571 | dev_err(hba->dev, |
| 572 | "UPIU[%d] - Transfer Request Descriptor phys@0x%llx", |
| 573 | tag, (u64)lrbp->utrd_dma_addr); |
| 574 | ufshcd_hex_dump("UPIU TRD: ", lrbp->utr_descriptor_ptr, |
| 575 | sizeof(struct utp_transfer_req_desc)); |
| 576 | dev_err(hba->dev, "UPIU[%d] - Request UPIU phys@0x%llx", tag, |
| 577 | (u64)lrbp->ucd_req_dma_addr); |
| 578 | ufshcd_hex_dump("UPIU REQ: ", lrbp->ucd_req_ptr, |
| 579 | sizeof(struct utp_upiu_req)); |
| 580 | dev_err(hba->dev, "UPIU[%d] - Response UPIU phys@0x%llx", tag, |
| 581 | (u64)lrbp->ucd_rsp_dma_addr); |
| 582 | ufshcd_hex_dump("UPIU RSP: ", lrbp->ucd_rsp_ptr, |
| 583 | sizeof(struct utp_upiu_rsp)); |
| 584 | prdt_length = |
| 585 | le16_to_cpu(lrbp->utr_descriptor_ptr->prd_table_length); |
| 586 | dev_err(hba->dev, "UPIU[%d] - PRDT - %d entries phys@0x%llx", |
| 587 | tag, prdt_length, (u64)lrbp->ucd_prdt_dma_addr); |
| 588 | if (pr_prdt) |
| 589 | ufshcd_hex_dump("UPIU PRDT: ", lrbp->ucd_prdt_ptr, |
| 590 | sizeof(struct ufshcd_sg_entry) * prdt_length); |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap) |
| 595 | { |
| 596 | struct utp_task_req_desc *tmrdp; |
| 597 | int tag; |
| 598 | |
| 599 | if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_TMRS_EN)) |
| 600 | return; |
| 601 | |
| 602 | for_each_set_bit(tag, &bitmap, hba->nutmrs) { |
| 603 | tmrdp = &hba->utmrdl_base_addr[tag]; |
| 604 | dev_err(hba->dev, "TM[%d] - Task Management Header", tag); |
| 605 | ufshcd_hex_dump("TM TRD: ", &tmrdp->header, |
| 606 | sizeof(struct request_desc_header)); |
| 607 | dev_err(hba->dev, "TM[%d] - Task Management Request UPIU", |
| 608 | tag); |
| 609 | ufshcd_hex_dump("TM REQ: ", tmrdp->task_req_upiu, |
| 610 | sizeof(struct utp_upiu_req)); |
| 611 | dev_err(hba->dev, "TM[%d] - Task Management Response UPIU", |
| 612 | tag); |
| 613 | ufshcd_hex_dump("TM RSP: ", tmrdp->task_rsp_upiu, |
| 614 | sizeof(struct utp_task_req_desc)); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | static void ufshcd_print_host_state(struct ufs_hba *hba) |
| 619 | { |
| 620 | if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_HOST_STATE_EN)) |
| 621 | return; |
| 622 | |
| 623 | dev_err(hba->dev, "UFS Host state=%d\n", hba->ufshcd_state); |
| 624 | dev_err(hba->dev, "lrb in use=0x%lx, outstanding reqs=0x%lx tasks=0x%lx\n", |
| 625 | hba->lrb_in_use, hba->outstanding_tasks, hba->outstanding_reqs); |
| 626 | dev_err(hba->dev, "saved_err=0x%x, saved_uic_err=0x%x, saved_ce_err=0x%x\n", |
| 627 | hba->saved_err, hba->saved_uic_err, hba->saved_ce_err); |
| 628 | dev_err(hba->dev, "Device power mode=%d, UIC link state=%d\n", |
| 629 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
| 630 | dev_err(hba->dev, "PM in progress=%d, sys. suspended=%d\n", |
| 631 | hba->pm_op_in_progress, hba->is_sys_suspended); |
| 632 | dev_err(hba->dev, "Auto BKOPS=%d, Host self-block=%d\n", |
| 633 | hba->auto_bkops_enabled, hba->host->host_self_blocked); |
| 634 | dev_err(hba->dev, "Clk gate=%d, hibern8 on idle=%d\n", |
| 635 | hba->clk_gating.state, hba->hibern8_on_idle.state); |
| 636 | dev_err(hba->dev, "error handling flags=0x%x, req. abort count=%d\n", |
| 637 | hba->eh_flags, hba->req_abort_count); |
| 638 | dev_err(hba->dev, "Host capabilities=0x%x, caps=0x%x\n", |
| 639 | hba->capabilities, hba->caps); |
| 640 | dev_err(hba->dev, "quirks=0x%x, dev. quirks=0x%x\n", hba->quirks, |
| 641 | hba->dev_quirks); |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * ufshcd_print_pwr_info - print power params as saved in hba |
| 646 | * power info |
| 647 | * @hba: per-adapter instance |
| 648 | */ |
| 649 | static void ufshcd_print_pwr_info(struct ufs_hba *hba) |
| 650 | { |
| 651 | char *names[] = { |
| 652 | "INVALID MODE", |
| 653 | "FAST MODE", |
| 654 | "SLOW_MODE", |
| 655 | "INVALID MODE", |
| 656 | "FASTAUTO_MODE", |
| 657 | "SLOWAUTO_MODE", |
| 658 | "INVALID MODE", |
| 659 | }; |
| 660 | |
| 661 | if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_PWR_EN)) |
| 662 | return; |
| 663 | |
| 664 | dev_err(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n", |
| 665 | __func__, |
| 666 | hba->pwr_info.gear_rx, hba->pwr_info.gear_tx, |
| 667 | hba->pwr_info.lane_rx, hba->pwr_info.lane_tx, |
| 668 | names[hba->pwr_info.pwr_rx], |
| 669 | names[hba->pwr_info.pwr_tx], |
| 670 | hba->pwr_info.hs_rate); |
| 671 | } |
| 672 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 673 | /* |
| 674 | * ufshcd_wait_for_register - wait for register value to change |
| 675 | * @hba - per-adapter interface |
| 676 | * @reg - mmio register offset |
| 677 | * @mask - mask to apply to read register value |
| 678 | * @val - wait condition |
| 679 | * @interval_us - polling interval in microsecs |
| 680 | * @timeout_ms - timeout in millisecs |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 681 | * @can_sleep - perform sleep or just spin |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 682 | * Returns -ETIMEDOUT on error, zero on success |
| 683 | */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 684 | int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask, |
| 685 | u32 val, unsigned long interval_us, |
| 686 | unsigned long timeout_ms, bool can_sleep) |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 687 | { |
| 688 | int err = 0; |
| 689 | unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms); |
| 690 | |
| 691 | /* ignore bits that we don't intend to wait on */ |
| 692 | val = val & mask; |
| 693 | |
| 694 | while ((ufshcd_readl(hba, reg) & mask) != val) { |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 695 | if (can_sleep) |
| 696 | usleep_range(interval_us, interval_us + 50); |
| 697 | else |
| 698 | udelay(interval_us); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 699 | if (time_after(jiffies, timeout)) { |
| 700 | if ((ufshcd_readl(hba, reg) & mask) != val) |
| 701 | err = -ETIMEDOUT; |
| 702 | break; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | return err; |
| 707 | } |
| 708 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 709 | /** |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 710 | * ufshcd_get_intr_mask - Get the interrupt bit mask |
| 711 | * @hba - Pointer to adapter instance |
| 712 | * |
| 713 | * Returns interrupt bit mask per version |
| 714 | */ |
| 715 | static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba) |
| 716 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 717 | u32 intr_mask = 0; |
| 718 | |
| 719 | switch (hba->ufs_version) { |
| 720 | case UFSHCI_VERSION_10: |
| 721 | intr_mask = INTERRUPT_MASK_ALL_VER_10; |
| 722 | break; |
| 723 | /* allow fall through */ |
| 724 | case UFSHCI_VERSION_11: |
| 725 | case UFSHCI_VERSION_20: |
| 726 | intr_mask = INTERRUPT_MASK_ALL_VER_11; |
| 727 | break; |
| 728 | /* allow fall through */ |
| 729 | case UFSHCI_VERSION_21: |
| 730 | default: |
| 731 | intr_mask = INTERRUPT_MASK_ALL_VER_21; |
| 732 | } |
| 733 | |
| 734 | if (!ufshcd_is_crypto_supported(hba)) |
| 735 | intr_mask &= ~CRYPTO_ENGINE_FATAL_ERROR; |
| 736 | |
| 737 | return intr_mask; |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 741 | * ufshcd_get_ufs_version - Get the UFS version supported by the HBA |
| 742 | * @hba - Pointer to adapter instance |
| 743 | * |
| 744 | * Returns UFSHCI version supported by the controller |
| 745 | */ |
| 746 | static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba) |
| 747 | { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 748 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION) |
| 749 | return ufshcd_vops_get_ufs_hci_version(hba); |
Yaniv Gardi | 9949e70 | 2015-05-17 18:55:05 +0300 | [diff] [blame] | 750 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 751 | return ufshcd_readl(hba, REG_UFS_VERSION); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | /** |
| 755 | * ufshcd_is_device_present - Check if any device connected to |
| 756 | * the host controller |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 757 | * @hba: pointer to adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 758 | * |
Venkatraman S | 73ec513 | 2012-07-10 19:39:23 +0530 | [diff] [blame] | 759 | * Returns 1 if device present, 0 if no device detected |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 760 | */ |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 761 | static inline int ufshcd_is_device_present(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 762 | { |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 763 | return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & |
| 764 | DEVICE_PRESENT) ? 1 : 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | /** |
| 768 | * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status |
| 769 | * @lrb: pointer to local command reference block |
| 770 | * |
| 771 | * This function is used to get the OCS field from UTRD |
| 772 | * Returns the OCS field in the UTRD |
| 773 | */ |
| 774 | static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp) |
| 775 | { |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 776 | 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] | 777 | } |
| 778 | |
| 779 | /** |
| 780 | * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status |
| 781 | * @task_req_descp: pointer to utp_task_req_desc structure |
| 782 | * |
| 783 | * This function is used to get the OCS field from UTMRD |
| 784 | * Returns the OCS field in the UTMRD |
| 785 | */ |
| 786 | static inline int |
| 787 | ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp) |
| 788 | { |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 789 | return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | /** |
| 793 | * ufshcd_get_tm_free_slot - get a free slot for task management request |
| 794 | * @hba: per adapter instance |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 795 | * @free_slot: pointer to variable with available slot value |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 796 | * |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 797 | * Get a free tag and lock it until ufshcd_put_tm_slot() is called. |
| 798 | * Returns 0 if free slot is not available, else return 1 with tag value |
| 799 | * in @free_slot. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 800 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 801 | 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] | 802 | { |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 803 | int tag; |
| 804 | bool ret = false; |
| 805 | |
| 806 | if (!free_slot) |
| 807 | goto out; |
| 808 | |
| 809 | do { |
| 810 | tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs); |
| 811 | if (tag >= hba->nutmrs) |
| 812 | goto out; |
| 813 | } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use)); |
| 814 | |
| 815 | *free_slot = tag; |
| 816 | ret = true; |
| 817 | out: |
| 818 | return ret; |
| 819 | } |
| 820 | |
| 821 | static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot) |
| 822 | { |
| 823 | clear_bit_unlock(slot, &hba->tm_slots_in_use); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | /** |
| 827 | * ufshcd_utrl_clear - Clear a bit in UTRLCLR register |
| 828 | * @hba: per adapter instance |
| 829 | * @pos: position of the bit to be cleared |
| 830 | */ |
| 831 | static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos) |
| 832 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 833 | ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | /** |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 837 | * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field |
| 838 | * @hba: per adapter instance |
| 839 | * @tag: position of the bit to be cleared |
| 840 | */ |
| 841 | static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag) |
| 842 | { |
| 843 | __clear_bit(tag, &hba->outstanding_reqs); |
| 844 | } |
| 845 | |
| 846 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 847 | * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY |
| 848 | * @reg: Register value of host controller status |
| 849 | * |
| 850 | * Returns integer, 0 on Success and positive value if failed |
| 851 | */ |
| 852 | static inline int ufshcd_get_lists_status(u32 reg) |
| 853 | { |
| 854 | /* |
| 855 | * The mask 0xFF is for the following HCS register bits |
| 856 | * Bit Description |
| 857 | * 0 Device Present |
| 858 | * 1 UTRLRDY |
| 859 | * 2 UTMRLRDY |
| 860 | * 3 UCRDY |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 861 | * 4-7 reserved |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 862 | */ |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 863 | return ((reg & 0xFF) >> 1) ^ 0x07; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | /** |
| 867 | * ufshcd_get_uic_cmd_result - Get the UIC command result |
| 868 | * @hba: Pointer to adapter instance |
| 869 | * |
| 870 | * This function gets the result of UIC command completion |
| 871 | * Returns 0 on success, non zero value on error |
| 872 | */ |
| 873 | static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba) |
| 874 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 875 | return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) & |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 876 | MASK_UIC_COMMAND_RESULT; |
| 877 | } |
| 878 | |
| 879 | /** |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 880 | * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command |
| 881 | * @hba: Pointer to adapter instance |
| 882 | * |
| 883 | * This function gets UIC command argument3 |
| 884 | * Returns 0 on success, non zero value on error |
| 885 | */ |
| 886 | static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba) |
| 887 | { |
| 888 | return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3); |
| 889 | } |
| 890 | |
| 891 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 892 | * ufshcd_get_req_rsp - returns the TR response transaction type |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 893 | * @ucd_rsp_ptr: pointer to response UPIU |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 894 | */ |
| 895 | static inline int |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 896 | ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 897 | { |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 898 | return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | /** |
| 902 | * ufshcd_get_rsp_upiu_result - Get the result from response UPIU |
| 903 | * @ucd_rsp_ptr: pointer to response UPIU |
| 904 | * |
| 905 | * This function gets the response status and scsi_status from response UPIU |
| 906 | * Returns the response result code. |
| 907 | */ |
| 908 | static inline int |
| 909 | ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 910 | { |
| 911 | return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT; |
| 912 | } |
| 913 | |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 914 | /* |
| 915 | * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length |
| 916 | * from response UPIU |
| 917 | * @ucd_rsp_ptr: pointer to response UPIU |
| 918 | * |
| 919 | * Return the data segment length. |
| 920 | */ |
| 921 | static inline unsigned int |
| 922 | ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 923 | { |
| 924 | return be32_to_cpu(ucd_rsp_ptr->header.dword_2) & |
| 925 | MASK_RSP_UPIU_DATA_SEG_LEN; |
| 926 | } |
| 927 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 928 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 929 | * ufshcd_is_exception_event - Check if the device raised an exception event |
| 930 | * @ucd_rsp_ptr: pointer to response UPIU |
| 931 | * |
| 932 | * The function checks if the device raised an exception event indicated in |
| 933 | * the Device Information field of response UPIU. |
| 934 | * |
| 935 | * Returns true if exception is raised, false otherwise. |
| 936 | */ |
| 937 | static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 938 | { |
| 939 | return be32_to_cpu(ucd_rsp_ptr->header.dword_2) & |
| 940 | MASK_RSP_EXCEPTION_EVENT ? true : false; |
| 941 | } |
| 942 | |
| 943 | /** |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 944 | * ufshcd_reset_intr_aggr - Reset interrupt aggregation values. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 945 | * @hba: per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 946 | */ |
| 947 | static inline void |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 948 | ufshcd_reset_intr_aggr(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 949 | { |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 950 | ufshcd_writel(hba, INT_AGGR_ENABLE | |
| 951 | INT_AGGR_COUNTER_AND_TIMER_RESET, |
| 952 | REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
| 953 | } |
| 954 | |
| 955 | /** |
| 956 | * ufshcd_config_intr_aggr - Configure interrupt aggregation values. |
| 957 | * @hba: per adapter instance |
| 958 | * @cnt: Interrupt aggregation counter threshold |
| 959 | * @tmout: Interrupt aggregation timeout value |
| 960 | */ |
| 961 | static inline void |
| 962 | ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout) |
| 963 | { |
| 964 | ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE | |
| 965 | INT_AGGR_COUNTER_THLD_VAL(cnt) | |
| 966 | INT_AGGR_TIMEOUT_VAL(tmout), |
| 967 | REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | /** |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 971 | * ufshcd_disable_intr_aggr - Disables interrupt aggregation. |
| 972 | * @hba: per adapter instance |
| 973 | */ |
| 974 | static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba) |
| 975 | { |
| 976 | ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
| 977 | } |
| 978 | |
| 979 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 980 | * ufshcd_enable_run_stop_reg - Enable run-stop registers, |
| 981 | * When run-stop registers are set to 1, it indicates the |
| 982 | * host controller that it can process the requests |
| 983 | * @hba: per adapter instance |
| 984 | */ |
| 985 | static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba) |
| 986 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 987 | ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT, |
| 988 | REG_UTP_TASK_REQ_LIST_RUN_STOP); |
| 989 | ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT, |
| 990 | REG_UTP_TRANSFER_REQ_LIST_RUN_STOP); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 994 | * ufshcd_hba_start - Start controller initialization sequence |
| 995 | * @hba: per adapter instance |
| 996 | */ |
| 997 | static inline void ufshcd_hba_start(struct ufs_hba *hba) |
| 998 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 999 | u32 val = CONTROLLER_ENABLE; |
| 1000 | |
| 1001 | if (ufshcd_is_crypto_supported(hba)) |
| 1002 | val |= CRYPTO_GENERAL_ENABLE; |
| 1003 | ufshcd_writel(hba, val, REG_CONTROLLER_ENABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | /** |
| 1007 | * ufshcd_is_hba_active - Get controller state |
| 1008 | * @hba: per adapter instance |
| 1009 | * |
| 1010 | * Returns zero if controller is active, 1 otherwise |
| 1011 | */ |
| 1012 | static inline int ufshcd_is_hba_active(struct ufs_hba *hba) |
| 1013 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 1014 | return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1015 | } |
| 1016 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1017 | static const char *ufschd_uic_link_state_to_string( |
| 1018 | enum uic_link_state state) |
| 1019 | { |
| 1020 | switch (state) { |
| 1021 | case UIC_LINK_OFF_STATE: return "OFF"; |
| 1022 | case UIC_LINK_ACTIVE_STATE: return "ACTIVE"; |
| 1023 | case UIC_LINK_HIBERN8_STATE: return "HIBERN8"; |
| 1024 | default: return "UNKNOWN"; |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | static const char *ufschd_ufs_dev_pwr_mode_to_string( |
| 1029 | enum ufs_dev_pwr_mode state) |
| 1030 | { |
| 1031 | switch (state) { |
| 1032 | case UFS_ACTIVE_PWR_MODE: return "ACTIVE"; |
| 1033 | case UFS_SLEEP_PWR_MODE: return "SLEEP"; |
| 1034 | case UFS_POWERDOWN_PWR_MODE: return "POWERDOWN"; |
| 1035 | default: return "UNKNOWN"; |
| 1036 | } |
| 1037 | } |
| 1038 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 1039 | u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba) |
| 1040 | { |
| 1041 | /* HCI version 1.0 and 1.1 supports UniPro 1.41 */ |
| 1042 | if ((hba->ufs_version == UFSHCI_VERSION_10) || |
| 1043 | (hba->ufs_version == UFSHCI_VERSION_11)) |
| 1044 | return UFS_UNIPRO_VER_1_41; |
| 1045 | else |
| 1046 | return UFS_UNIPRO_VER_1_6; |
| 1047 | } |
| 1048 | EXPORT_SYMBOL(ufshcd_get_local_unipro_ver); |
| 1049 | |
| 1050 | static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba) |
| 1051 | { |
| 1052 | /* |
| 1053 | * If both host and device support UniPro ver1.6 or later, PA layer |
| 1054 | * parameters tuning happens during link startup itself. |
| 1055 | * |
| 1056 | * We can manually tune PA layer parameters if either host or device |
| 1057 | * doesn't support UniPro ver 1.6 or later. But to keep manual tuning |
| 1058 | * logic simple, we will only do manual tuning if local unipro version |
| 1059 | * doesn't support ver1.6 or later. |
| 1060 | */ |
| 1061 | if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6) |
| 1062 | return true; |
| 1063 | else |
| 1064 | return false; |
| 1065 | } |
| 1066 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1067 | /** |
| 1068 | * ufshcd_set_clk_freq - set UFS controller clock frequencies |
| 1069 | * @hba: per adapter instance |
| 1070 | * @scale_up: If True, set max possible frequency othewise set low frequency |
| 1071 | * |
| 1072 | * Returns 0 if successful |
| 1073 | * Returns < 0 for any other errors |
| 1074 | */ |
| 1075 | static int ufshcd_set_clk_freq(struct ufs_hba *hba, bool scale_up) |
| 1076 | { |
| 1077 | int ret = 0; |
| 1078 | struct ufs_clk_info *clki; |
| 1079 | struct list_head *head = &hba->clk_list_head; |
| 1080 | |
| 1081 | if (!head || list_empty(head)) |
| 1082 | goto out; |
| 1083 | |
| 1084 | list_for_each_entry(clki, head, list) { |
| 1085 | if (!IS_ERR_OR_NULL(clki->clk)) { |
| 1086 | if (scale_up && clki->max_freq) { |
| 1087 | if (clki->curr_freq == clki->max_freq) |
| 1088 | continue; |
| 1089 | |
| 1090 | ret = clk_set_rate(clki->clk, clki->max_freq); |
| 1091 | if (ret) { |
| 1092 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 1093 | __func__, clki->name, |
| 1094 | clki->max_freq, ret); |
| 1095 | break; |
| 1096 | } |
| 1097 | trace_ufshcd_clk_scaling(dev_name(hba->dev), |
| 1098 | "scaled up", clki->name, |
| 1099 | clki->curr_freq, |
| 1100 | clki->max_freq); |
| 1101 | clki->curr_freq = clki->max_freq; |
| 1102 | |
| 1103 | } else if (!scale_up && clki->min_freq) { |
| 1104 | if (clki->curr_freq == clki->min_freq) |
| 1105 | continue; |
| 1106 | |
| 1107 | ret = clk_set_rate(clki->clk, clki->min_freq); |
| 1108 | if (ret) { |
| 1109 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 1110 | __func__, clki->name, |
| 1111 | clki->min_freq, ret); |
| 1112 | break; |
| 1113 | } |
| 1114 | trace_ufshcd_clk_scaling(dev_name(hba->dev), |
| 1115 | "scaled down", clki->name, |
| 1116 | clki->curr_freq, |
| 1117 | clki->min_freq); |
| 1118 | clki->curr_freq = clki->min_freq; |
| 1119 | } |
| 1120 | } |
| 1121 | dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__, |
| 1122 | clki->name, clk_get_rate(clki->clk)); |
| 1123 | } |
| 1124 | |
| 1125 | out: |
| 1126 | return ret; |
| 1127 | } |
| 1128 | |
| 1129 | /** |
| 1130 | * ufshcd_scale_clks - scale up or scale down UFS controller clocks |
| 1131 | * @hba: per adapter instance |
| 1132 | * @scale_up: True if scaling up and false if scaling down |
| 1133 | * |
| 1134 | * Returns 0 if successful |
| 1135 | * Returns < 0 for any other errors |
| 1136 | */ |
| 1137 | static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up) |
| 1138 | { |
| 1139 | int ret = 0; |
| 1140 | |
| 1141 | ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE); |
| 1142 | if (ret) |
| 1143 | return ret; |
| 1144 | |
| 1145 | ret = ufshcd_set_clk_freq(hba, scale_up); |
| 1146 | if (ret) |
| 1147 | return ret; |
| 1148 | |
| 1149 | ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE); |
| 1150 | if (ret) { |
| 1151 | ufshcd_set_clk_freq(hba, !scale_up); |
| 1152 | return ret; |
| 1153 | } |
| 1154 | |
| 1155 | return ret; |
| 1156 | } |
| 1157 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1158 | static void ufshcd_ungate_work(struct work_struct *work) |
| 1159 | { |
| 1160 | int ret; |
| 1161 | unsigned long flags; |
| 1162 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1163 | clk_gating.ungate_work); |
| 1164 | |
| 1165 | cancel_delayed_work_sync(&hba->clk_gating.gate_work); |
| 1166 | |
| 1167 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1168 | if (hba->clk_gating.state == CLKS_ON) { |
| 1169 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1170 | goto unblock_reqs; |
| 1171 | } |
| 1172 | |
| 1173 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1174 | ufshcd_hba_vreg_set_hpm(hba); |
| 1175 | ufshcd_enable_clocks(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1176 | |
| 1177 | /* Exit from hibern8 */ |
| 1178 | if (ufshcd_can_hibern8_during_gating(hba)) { |
| 1179 | /* Prevent gating in this path */ |
| 1180 | hba->clk_gating.is_suspended = true; |
| 1181 | if (ufshcd_is_link_hibern8(hba)) { |
| 1182 | ret = ufshcd_uic_hibern8_exit(hba); |
| 1183 | if (ret) |
| 1184 | dev_err(hba->dev, "%s: hibern8 exit failed %d\n", |
| 1185 | __func__, ret); |
| 1186 | else |
| 1187 | ufshcd_set_link_active(hba); |
| 1188 | } |
| 1189 | hba->clk_gating.is_suspended = false; |
| 1190 | } |
| 1191 | unblock_reqs: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1192 | ufshcd_scsi_unblock_requests(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | /** |
| 1196 | * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release. |
| 1197 | * Also, exit from hibern8 mode and set the link as active. |
| 1198 | * @hba: per adapter instance |
| 1199 | * @async: This indicates whether caller should ungate clocks asynchronously. |
| 1200 | */ |
| 1201 | int ufshcd_hold(struct ufs_hba *hba, bool async) |
| 1202 | { |
| 1203 | int rc = 0; |
| 1204 | unsigned long flags; |
| 1205 | |
| 1206 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1207 | goto out; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1208 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1209 | hba->clk_gating.active_reqs++; |
| 1210 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 1211 | if (ufshcd_eh_in_progress(hba)) { |
| 1212 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1216 | start: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1217 | switch (hba->clk_gating.state) { |
| 1218 | case CLKS_ON: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1219 | /* |
| 1220 | * Wait for the ungate work to complete if in progress. |
| 1221 | * Though the clocks may be in ON state, the link could |
| 1222 | * still be in hibner8 state if hibern8 is allowed |
| 1223 | * during clock gating. |
| 1224 | * Make sure we exit hibern8 state also in addition to |
| 1225 | * clocks being ON. |
| 1226 | */ |
| 1227 | if (ufshcd_can_hibern8_during_gating(hba) && |
| 1228 | ufshcd_is_link_hibern8(hba)) { |
| 1229 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1230 | flush_work(&hba->clk_gating.ungate_work); |
| 1231 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1232 | goto start; |
| 1233 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1234 | break; |
| 1235 | case REQ_CLKS_OFF: |
| 1236 | if (cancel_delayed_work(&hba->clk_gating.gate_work)) { |
| 1237 | hba->clk_gating.state = CLKS_ON; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1238 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1239 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1240 | break; |
| 1241 | } |
| 1242 | /* |
| 1243 | * If we here, it means gating work is either done or |
| 1244 | * currently running. Hence, fall through to cancel gating |
| 1245 | * work and to enable clocks. |
| 1246 | */ |
| 1247 | case CLKS_OFF: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1248 | __ufshcd_scsi_block_requests(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1249 | hba->clk_gating.state = REQ_CLKS_ON; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1250 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1251 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1252 | schedule_work(&hba->clk_gating.ungate_work); |
| 1253 | /* |
| 1254 | * fall through to check if we should wait for this |
| 1255 | * work to be done or not. |
| 1256 | */ |
| 1257 | case REQ_CLKS_ON: |
| 1258 | if (async) { |
| 1259 | rc = -EAGAIN; |
| 1260 | hba->clk_gating.active_reqs--; |
| 1261 | break; |
| 1262 | } |
| 1263 | |
| 1264 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1265 | flush_work(&hba->clk_gating.ungate_work); |
| 1266 | /* Make sure state is CLKS_ON before returning */ |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1267 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1268 | goto start; |
| 1269 | default: |
| 1270 | dev_err(hba->dev, "%s: clk gating is in invalid state %d\n", |
| 1271 | __func__, hba->clk_gating.state); |
| 1272 | break; |
| 1273 | } |
| 1274 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1275 | out: |
| 1276 | return rc; |
| 1277 | } |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 1278 | EXPORT_SYMBOL_GPL(ufshcd_hold); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1279 | |
| 1280 | static void ufshcd_gate_work(struct work_struct *work) |
| 1281 | { |
| 1282 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1283 | clk_gating.gate_work.work); |
| 1284 | unsigned long flags; |
| 1285 | |
| 1286 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1287 | if (hba->clk_gating.is_suspended) { |
| 1288 | hba->clk_gating.state = CLKS_ON; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1289 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1290 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1291 | goto rel_lock; |
| 1292 | } |
| 1293 | |
| 1294 | if (hba->clk_gating.active_reqs |
| 1295 | || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL |
| 1296 | || hba->lrb_in_use || hba->outstanding_tasks |
| 1297 | || hba->active_uic_cmd || hba->uic_async_done) |
| 1298 | goto rel_lock; |
| 1299 | |
| 1300 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1301 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1302 | if (ufshcd_is_hibern8_on_idle_allowed(hba) && |
| 1303 | hba->hibern8_on_idle.is_enabled) |
| 1304 | /* |
| 1305 | * Hibern8 enter work (on Idle) needs clocks to be ON hence |
| 1306 | * make sure that it is flushed before turning off the clocks. |
| 1307 | */ |
| 1308 | flush_delayed_work(&hba->hibern8_on_idle.enter_work); |
| 1309 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1310 | /* put the link into hibern8 mode before turning off clocks */ |
| 1311 | if (ufshcd_can_hibern8_during_gating(hba)) { |
| 1312 | if (ufshcd_uic_hibern8_enter(hba)) { |
| 1313 | hba->clk_gating.state = CLKS_ON; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1314 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1315 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1316 | goto out; |
| 1317 | } |
| 1318 | ufshcd_set_link_hibern8(hba); |
| 1319 | } |
| 1320 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1321 | if (!ufshcd_is_link_active(hba) && !hba->no_ref_clk_gating) |
| 1322 | ufshcd_disable_clocks(hba, true); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1323 | else |
| 1324 | /* If link is active, device ref_clk can't be switched off */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1325 | ufshcd_disable_clocks_skip_ref_clk(hba, true); |
| 1326 | |
| 1327 | /* Put the host controller in low power mode if possible */ |
| 1328 | ufshcd_hba_vreg_set_lpm(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1329 | |
| 1330 | /* |
| 1331 | * In case you are here to cancel this work the gating state |
| 1332 | * would be marked as REQ_CLKS_ON. In this case keep the state |
| 1333 | * as REQ_CLKS_ON which would anyway imply that clocks are off |
| 1334 | * and a request to turn them on is pending. By doing this way, |
| 1335 | * we keep the state machine in tact and this would ultimately |
| 1336 | * prevent from doing cancel work multiple times when there are |
| 1337 | * new requests arriving before the current cancel work is done. |
| 1338 | */ |
| 1339 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1340 | if (hba->clk_gating.state == REQ_CLKS_OFF) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1341 | hba->clk_gating.state = CLKS_OFF; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1342 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1343 | hba->clk_gating.state); |
| 1344 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1345 | rel_lock: |
| 1346 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1347 | out: |
| 1348 | return; |
| 1349 | } |
| 1350 | |
| 1351 | /* host lock must be held before calling this variant */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1352 | static void __ufshcd_release(struct ufs_hba *hba, bool no_sched) |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1353 | { |
| 1354 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1355 | return; |
| 1356 | |
| 1357 | hba->clk_gating.active_reqs--; |
| 1358 | |
| 1359 | if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended |
| 1360 | || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL |
| 1361 | || hba->lrb_in_use || hba->outstanding_tasks |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 1362 | || hba->active_uic_cmd || hba->uic_async_done |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1363 | || ufshcd_eh_in_progress(hba) || no_sched) |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1364 | return; |
| 1365 | |
| 1366 | hba->clk_gating.state = REQ_CLKS_OFF; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1367 | trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state); |
| 1368 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1369 | schedule_delayed_work(&hba->clk_gating.gate_work, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1370 | msecs_to_jiffies(hba->clk_gating.delay_ms)); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1371 | } |
| 1372 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1373 | void ufshcd_release(struct ufs_hba *hba, bool no_sched) |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1374 | { |
| 1375 | unsigned long flags; |
| 1376 | |
| 1377 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1378 | __ufshcd_release(hba, no_sched); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1379 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1380 | } |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 1381 | EXPORT_SYMBOL_GPL(ufshcd_release); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1382 | |
| 1383 | static ssize_t ufshcd_clkgate_delay_show(struct device *dev, |
| 1384 | struct device_attribute *attr, char *buf) |
| 1385 | { |
| 1386 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1387 | |
| 1388 | return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms); |
| 1389 | } |
| 1390 | |
| 1391 | static ssize_t ufshcd_clkgate_delay_store(struct device *dev, |
| 1392 | struct device_attribute *attr, const char *buf, size_t count) |
| 1393 | { |
| 1394 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1395 | unsigned long flags, value; |
| 1396 | |
| 1397 | if (kstrtoul(buf, 0, &value)) |
| 1398 | return -EINVAL; |
| 1399 | |
| 1400 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1401 | hba->clk_gating.delay_ms = value; |
| 1402 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1403 | return count; |
| 1404 | } |
| 1405 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1406 | static ssize_t ufshcd_clkgate_delay_pwr_save_show(struct device *dev, |
| 1407 | struct device_attribute *attr, char *buf) |
| 1408 | { |
| 1409 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1410 | |
| 1411 | return snprintf(buf, PAGE_SIZE, "%lu\n", |
| 1412 | hba->clk_gating.delay_ms_pwr_save); |
| 1413 | } |
| 1414 | |
| 1415 | static ssize_t ufshcd_clkgate_delay_pwr_save_store(struct device *dev, |
| 1416 | struct device_attribute *attr, const char *buf, size_t count) |
| 1417 | { |
| 1418 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1419 | unsigned long flags, value; |
| 1420 | |
| 1421 | if (kstrtoul(buf, 0, &value)) |
| 1422 | return -EINVAL; |
| 1423 | |
| 1424 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1425 | |
| 1426 | hba->clk_gating.delay_ms_pwr_save = value; |
| 1427 | if (ufshcd_is_clkscaling_supported(hba) && |
| 1428 | !hba->clk_scaling.is_scaled_up) |
| 1429 | hba->clk_gating.delay_ms = hba->clk_gating.delay_ms_pwr_save; |
| 1430 | |
| 1431 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1432 | return count; |
| 1433 | } |
| 1434 | |
| 1435 | static ssize_t ufshcd_clkgate_delay_perf_show(struct device *dev, |
| 1436 | struct device_attribute *attr, char *buf) |
| 1437 | { |
| 1438 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1439 | |
| 1440 | return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms_perf); |
| 1441 | } |
| 1442 | |
| 1443 | static ssize_t ufshcd_clkgate_delay_perf_store(struct device *dev, |
| 1444 | struct device_attribute *attr, const char *buf, size_t count) |
| 1445 | { |
| 1446 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1447 | unsigned long flags, value; |
| 1448 | |
| 1449 | if (kstrtoul(buf, 0, &value)) |
| 1450 | return -EINVAL; |
| 1451 | |
| 1452 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1453 | |
| 1454 | hba->clk_gating.delay_ms_perf = value; |
| 1455 | if (ufshcd_is_clkscaling_supported(hba) && |
| 1456 | hba->clk_scaling.is_scaled_up) |
| 1457 | hba->clk_gating.delay_ms = hba->clk_gating.delay_ms_perf; |
| 1458 | |
| 1459 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1460 | return count; |
| 1461 | } |
| 1462 | |
| 1463 | static ssize_t ufshcd_clkgate_enable_show(struct device *dev, |
| 1464 | struct device_attribute *attr, char *buf) |
| 1465 | { |
| 1466 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1467 | |
| 1468 | return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_gating.is_enabled); |
| 1469 | } |
| 1470 | |
| 1471 | static ssize_t ufshcd_clkgate_enable_store(struct device *dev, |
| 1472 | struct device_attribute *attr, const char *buf, size_t count) |
| 1473 | { |
| 1474 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1475 | unsigned long flags; |
| 1476 | u32 value; |
| 1477 | |
| 1478 | if (kstrtou32(buf, 0, &value)) |
| 1479 | return -EINVAL; |
| 1480 | |
| 1481 | value = !!value; |
| 1482 | if (value == hba->clk_gating.is_enabled) |
| 1483 | goto out; |
| 1484 | |
| 1485 | if (value) { |
| 1486 | ufshcd_release(hba, false); |
| 1487 | } else { |
| 1488 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1489 | hba->clk_gating.active_reqs++; |
| 1490 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1491 | } |
| 1492 | |
| 1493 | hba->clk_gating.is_enabled = value; |
| 1494 | out: |
| 1495 | return count; |
| 1496 | } |
| 1497 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1498 | static void ufshcd_init_clk_gating(struct ufs_hba *hba) |
| 1499 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1500 | struct ufs_clk_gating *gating = &hba->clk_gating; |
| 1501 | |
| 1502 | hba->clk_gating.state = CLKS_ON; |
| 1503 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1504 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1505 | return; |
| 1506 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1507 | INIT_DELAYED_WORK(&gating->gate_work, ufshcd_gate_work); |
| 1508 | INIT_WORK(&gating->ungate_work, ufshcd_ungate_work); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1509 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1510 | gating->is_enabled = true; |
| 1511 | |
| 1512 | /* |
| 1513 | * Scheduling the delayed work after 1 jiffies will make the work to |
| 1514 | * get schedule any time from 0ms to 1000/HZ ms which is not desirable |
| 1515 | * for hibern8 enter work as it may impact the performance if it gets |
| 1516 | * scheduled almost immediately. Hence make sure that hibern8 enter |
| 1517 | * work gets scheduled atleast after 2 jiffies (any time between |
| 1518 | * 1000/HZ ms to 2000/HZ ms). |
| 1519 | */ |
| 1520 | gating->delay_ms_pwr_save = jiffies_to_msecs( |
| 1521 | max_t(unsigned long, |
| 1522 | msecs_to_jiffies(UFSHCD_CLK_GATING_DELAY_MS_PWR_SAVE), |
| 1523 | 2)); |
| 1524 | gating->delay_ms_perf = jiffies_to_msecs( |
| 1525 | max_t(unsigned long, |
| 1526 | msecs_to_jiffies(UFSHCD_CLK_GATING_DELAY_MS_PERF), |
| 1527 | 2)); |
| 1528 | |
| 1529 | /* start with performance mode */ |
| 1530 | gating->delay_ms = gating->delay_ms_perf; |
| 1531 | |
| 1532 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 1533 | goto scaling_not_supported; |
| 1534 | |
| 1535 | gating->delay_pwr_save_attr.show = ufshcd_clkgate_delay_pwr_save_show; |
| 1536 | gating->delay_pwr_save_attr.store = ufshcd_clkgate_delay_pwr_save_store; |
| 1537 | sysfs_attr_init(&gating->delay_pwr_save_attr.attr); |
| 1538 | gating->delay_pwr_save_attr.attr.name = "clkgate_delay_ms_pwr_save"; |
| 1539 | gating->delay_pwr_save_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 1540 | if (device_create_file(hba->dev, &gating->delay_pwr_save_attr)) |
| 1541 | dev_err(hba->dev, "Failed to create sysfs for clkgate_delay_ms_pwr_save\n"); |
| 1542 | |
| 1543 | gating->delay_perf_attr.show = ufshcd_clkgate_delay_perf_show; |
| 1544 | gating->delay_perf_attr.store = ufshcd_clkgate_delay_perf_store; |
| 1545 | sysfs_attr_init(&gating->delay_perf_attr.attr); |
| 1546 | gating->delay_perf_attr.attr.name = "clkgate_delay_ms_perf"; |
| 1547 | gating->delay_perf_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 1548 | if (device_create_file(hba->dev, &gating->delay_perf_attr)) |
| 1549 | dev_err(hba->dev, "Failed to create sysfs for clkgate_delay_ms_perf\n"); |
| 1550 | |
| 1551 | goto add_clkgate_enable; |
| 1552 | |
| 1553 | scaling_not_supported: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1554 | hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show; |
| 1555 | hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store; |
| 1556 | sysfs_attr_init(&hba->clk_gating.delay_attr.attr); |
| 1557 | hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms"; |
| 1558 | hba->clk_gating.delay_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 1559 | if (device_create_file(hba->dev, &hba->clk_gating.delay_attr)) |
| 1560 | dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n"); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1561 | |
| 1562 | add_clkgate_enable: |
| 1563 | gating->enable_attr.show = ufshcd_clkgate_enable_show; |
| 1564 | gating->enable_attr.store = ufshcd_clkgate_enable_store; |
| 1565 | sysfs_attr_init(&gating->enable_attr.attr); |
| 1566 | gating->enable_attr.attr.name = "clkgate_enable"; |
| 1567 | gating->enable_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 1568 | if (device_create_file(hba->dev, &gating->enable_attr)) |
| 1569 | dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n"); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | static void ufshcd_exit_clk_gating(struct ufs_hba *hba) |
| 1573 | { |
| 1574 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1575 | return; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1576 | if (ufshcd_is_clkscaling_supported(hba)) { |
| 1577 | device_remove_file(hba->dev, |
| 1578 | &hba->clk_gating.delay_pwr_save_attr); |
| 1579 | device_remove_file(hba->dev, &hba->clk_gating.delay_perf_attr); |
| 1580 | } else { |
| 1581 | device_remove_file(hba->dev, &hba->clk_gating.delay_attr); |
| 1582 | } |
| 1583 | device_remove_file(hba->dev, &hba->clk_gating.enable_attr); |
Akinobu Mita | 97cd680 | 2014-11-24 14:24:18 +0900 | [diff] [blame] | 1584 | cancel_work_sync(&hba->clk_gating.ungate_work); |
| 1585 | cancel_delayed_work_sync(&hba->clk_gating.gate_work); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1586 | } |
| 1587 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1588 | static void ufshcd_set_auto_hibern8_timer(struct ufs_hba *hba, u32 delay) |
| 1589 | { |
| 1590 | ufshcd_rmwl(hba, AUTO_HIBERN8_TIMER_SCALE_MASK | |
| 1591 | AUTO_HIBERN8_IDLE_TIMER_MASK, |
| 1592 | AUTO_HIBERN8_TIMER_SCALE_1_MS | delay, |
| 1593 | REG_AUTO_HIBERN8_IDLE_TIMER); |
| 1594 | /* Make sure the timer gets applied before further operations */ |
| 1595 | mb(); |
| 1596 | } |
| 1597 | |
| 1598 | /** |
| 1599 | * ufshcd_hibern8_hold - Make sure that link is not in hibern8. |
| 1600 | * |
| 1601 | * @hba: per adapter instance |
| 1602 | * @async: This indicates whether caller wants to exit hibern8 asynchronously. |
| 1603 | * |
| 1604 | * Exit from hibern8 mode and set the link as active. |
| 1605 | * |
| 1606 | * Return 0 on success, non-zero on failure. |
| 1607 | */ |
| 1608 | static int ufshcd_hibern8_hold(struct ufs_hba *hba, bool async) |
| 1609 | { |
| 1610 | int rc = 0; |
| 1611 | unsigned long flags; |
| 1612 | |
| 1613 | if (!ufshcd_is_hibern8_on_idle_allowed(hba)) |
| 1614 | goto out; |
| 1615 | |
| 1616 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1617 | hba->hibern8_on_idle.active_reqs++; |
| 1618 | |
| 1619 | if (ufshcd_eh_in_progress(hba)) { |
| 1620 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1621 | return 0; |
| 1622 | } |
| 1623 | |
| 1624 | start: |
| 1625 | switch (hba->hibern8_on_idle.state) { |
| 1626 | case HIBERN8_EXITED: |
| 1627 | break; |
| 1628 | case REQ_HIBERN8_ENTER: |
| 1629 | if (cancel_delayed_work(&hba->hibern8_on_idle.enter_work)) { |
| 1630 | hba->hibern8_on_idle.state = HIBERN8_EXITED; |
| 1631 | trace_ufshcd_hibern8_on_idle(dev_name(hba->dev), |
| 1632 | hba->hibern8_on_idle.state); |
| 1633 | break; |
| 1634 | } |
| 1635 | /* |
| 1636 | * If we here, it means Hibern8 enter work is either done or |
| 1637 | * currently running. Hence, fall through to cancel hibern8 |
| 1638 | * work and exit hibern8. |
| 1639 | */ |
| 1640 | case HIBERN8_ENTERED: |
| 1641 | __ufshcd_scsi_block_requests(hba); |
| 1642 | hba->hibern8_on_idle.state = REQ_HIBERN8_EXIT; |
| 1643 | trace_ufshcd_hibern8_on_idle(dev_name(hba->dev), |
| 1644 | hba->hibern8_on_idle.state); |
| 1645 | schedule_work(&hba->hibern8_on_idle.exit_work); |
| 1646 | /* |
| 1647 | * fall through to check if we should wait for this |
| 1648 | * work to be done or not. |
| 1649 | */ |
| 1650 | case REQ_HIBERN8_EXIT: |
| 1651 | if (async) { |
| 1652 | rc = -EAGAIN; |
| 1653 | hba->hibern8_on_idle.active_reqs--; |
| 1654 | break; |
| 1655 | } else { |
| 1656 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1657 | flush_work(&hba->hibern8_on_idle.exit_work); |
| 1658 | /* Make sure state is HIBERN8_EXITED before returning */ |
| 1659 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1660 | goto start; |
| 1661 | } |
| 1662 | default: |
| 1663 | dev_err(hba->dev, "%s: H8 is in invalid state %d\n", |
| 1664 | __func__, hba->hibern8_on_idle.state); |
| 1665 | break; |
| 1666 | } |
| 1667 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1668 | out: |
| 1669 | return rc; |
| 1670 | } |
| 1671 | |
| 1672 | /* host lock must be held before calling this variant */ |
| 1673 | static void __ufshcd_hibern8_release(struct ufs_hba *hba, bool no_sched) |
| 1674 | { |
| 1675 | unsigned long delay_in_jiffies; |
| 1676 | |
| 1677 | if (!ufshcd_is_hibern8_on_idle_allowed(hba)) |
| 1678 | return; |
| 1679 | |
| 1680 | hba->hibern8_on_idle.active_reqs--; |
| 1681 | BUG_ON(hba->hibern8_on_idle.active_reqs < 0); |
| 1682 | |
| 1683 | if (hba->hibern8_on_idle.active_reqs |
| 1684 | || hba->hibern8_on_idle.is_suspended |
| 1685 | || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL |
| 1686 | || hba->lrb_in_use || hba->outstanding_tasks |
| 1687 | || hba->active_uic_cmd || hba->uic_async_done |
| 1688 | || ufshcd_eh_in_progress(hba) || no_sched) |
| 1689 | return; |
| 1690 | |
| 1691 | hba->hibern8_on_idle.state = REQ_HIBERN8_ENTER; |
| 1692 | trace_ufshcd_hibern8_on_idle(dev_name(hba->dev), |
| 1693 | hba->hibern8_on_idle.state); |
| 1694 | /* |
| 1695 | * Scheduling the delayed work after 1 jiffies will make the work to |
| 1696 | * get schedule any time from 0ms to 1000/HZ ms which is not desirable |
| 1697 | * for hibern8 enter work as it may impact the performance if it gets |
| 1698 | * scheduled almost immediately. Hence make sure that hibern8 enter |
| 1699 | * work gets scheduled atleast after 2 jiffies (any time between |
| 1700 | * 1000/HZ ms to 2000/HZ ms). |
| 1701 | */ |
| 1702 | delay_in_jiffies = msecs_to_jiffies(hba->hibern8_on_idle.delay_ms); |
| 1703 | if (delay_in_jiffies == 1) |
| 1704 | delay_in_jiffies++; |
| 1705 | |
| 1706 | schedule_delayed_work(&hba->hibern8_on_idle.enter_work, |
| 1707 | delay_in_jiffies); |
| 1708 | } |
| 1709 | |
| 1710 | static void ufshcd_hibern8_release(struct ufs_hba *hba, bool no_sched) |
| 1711 | { |
| 1712 | unsigned long flags; |
| 1713 | |
| 1714 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1715 | __ufshcd_hibern8_release(hba, no_sched); |
| 1716 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1717 | } |
| 1718 | |
| 1719 | static void ufshcd_hibern8_enter_work(struct work_struct *work) |
| 1720 | { |
| 1721 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1722 | hibern8_on_idle.enter_work.work); |
| 1723 | unsigned long flags; |
| 1724 | |
| 1725 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1726 | if (hba->hibern8_on_idle.is_suspended) { |
| 1727 | hba->hibern8_on_idle.state = HIBERN8_EXITED; |
| 1728 | trace_ufshcd_hibern8_on_idle(dev_name(hba->dev), |
| 1729 | hba->hibern8_on_idle.state); |
| 1730 | goto rel_lock; |
| 1731 | } |
| 1732 | |
| 1733 | if (hba->hibern8_on_idle.active_reqs |
| 1734 | || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL |
| 1735 | || hba->lrb_in_use || hba->outstanding_tasks |
| 1736 | || hba->active_uic_cmd || hba->uic_async_done) |
| 1737 | goto rel_lock; |
| 1738 | |
| 1739 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1740 | |
| 1741 | if (ufshcd_is_link_active(hba) && ufshcd_uic_hibern8_enter(hba)) { |
| 1742 | /* Enter failed */ |
| 1743 | hba->hibern8_on_idle.state = HIBERN8_EXITED; |
| 1744 | trace_ufshcd_hibern8_on_idle(dev_name(hba->dev), |
| 1745 | hba->hibern8_on_idle.state); |
| 1746 | goto out; |
| 1747 | } |
| 1748 | ufshcd_set_link_hibern8(hba); |
| 1749 | |
| 1750 | /* |
| 1751 | * In case you are here to cancel this work the hibern8_on_idle.state |
| 1752 | * would be marked as REQ_HIBERN8_EXIT. In this case keep the state |
| 1753 | * as REQ_HIBERN8_EXIT which would anyway imply that we are in hibern8 |
| 1754 | * and a request to exit from it is pending. By doing this way, |
| 1755 | * we keep the state machine in tact and this would ultimately |
| 1756 | * prevent from doing cancel work multiple times when there are |
| 1757 | * new requests arriving before the current cancel work is done. |
| 1758 | */ |
| 1759 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1760 | if (hba->hibern8_on_idle.state == REQ_HIBERN8_ENTER) { |
| 1761 | hba->hibern8_on_idle.state = HIBERN8_ENTERED; |
| 1762 | trace_ufshcd_hibern8_on_idle(dev_name(hba->dev), |
| 1763 | hba->hibern8_on_idle.state); |
| 1764 | } |
| 1765 | rel_lock: |
| 1766 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1767 | out: |
| 1768 | return; |
| 1769 | } |
| 1770 | |
| 1771 | static void ufshcd_hibern8_exit_work(struct work_struct *work) |
| 1772 | { |
| 1773 | int ret; |
| 1774 | unsigned long flags; |
| 1775 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1776 | hibern8_on_idle.exit_work); |
| 1777 | |
| 1778 | cancel_delayed_work_sync(&hba->hibern8_on_idle.enter_work); |
| 1779 | |
| 1780 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1781 | if ((hba->hibern8_on_idle.state == HIBERN8_EXITED) |
| 1782 | || ufshcd_is_link_active(hba)) { |
| 1783 | hba->hibern8_on_idle.state = HIBERN8_EXITED; |
| 1784 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1785 | goto unblock_reqs; |
| 1786 | } |
| 1787 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1788 | |
| 1789 | /* Exit from hibern8 */ |
| 1790 | if (ufshcd_is_link_hibern8(hba)) { |
| 1791 | ufshcd_hold(hba, false); |
| 1792 | ret = ufshcd_uic_hibern8_exit(hba); |
| 1793 | ufshcd_release(hba, false); |
| 1794 | if (!ret) { |
| 1795 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1796 | ufshcd_set_link_active(hba); |
| 1797 | hba->hibern8_on_idle.state = HIBERN8_EXITED; |
| 1798 | trace_ufshcd_hibern8_on_idle(dev_name(hba->dev), |
| 1799 | hba->hibern8_on_idle.state); |
| 1800 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1801 | } |
| 1802 | } |
| 1803 | unblock_reqs: |
| 1804 | ufshcd_scsi_unblock_requests(hba); |
| 1805 | } |
| 1806 | |
| 1807 | static ssize_t ufshcd_hibern8_on_idle_delay_show(struct device *dev, |
| 1808 | struct device_attribute *attr, char *buf) |
| 1809 | { |
| 1810 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1811 | |
| 1812 | return snprintf(buf, PAGE_SIZE, "%lu\n", hba->hibern8_on_idle.delay_ms); |
| 1813 | } |
| 1814 | |
| 1815 | static ssize_t ufshcd_hibern8_on_idle_delay_store(struct device *dev, |
| 1816 | struct device_attribute *attr, const char *buf, size_t count) |
| 1817 | { |
| 1818 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1819 | unsigned long flags, value; |
| 1820 | |
| 1821 | if (kstrtoul(buf, 0, &value)) |
| 1822 | return -EINVAL; |
| 1823 | |
| 1824 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1825 | hba->hibern8_on_idle.delay_ms = value; |
| 1826 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1827 | |
| 1828 | /* Update auto hibern8 timer value if supported */ |
| 1829 | if (ufshcd_is_auto_hibern8_supported(hba) && |
| 1830 | hba->hibern8_on_idle.is_enabled) |
| 1831 | ufshcd_set_auto_hibern8_timer(hba, |
| 1832 | hba->hibern8_on_idle.delay_ms); |
| 1833 | |
| 1834 | return count; |
| 1835 | } |
| 1836 | |
| 1837 | static ssize_t ufshcd_hibern8_on_idle_enable_show(struct device *dev, |
| 1838 | struct device_attribute *attr, char *buf) |
| 1839 | { |
| 1840 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1841 | |
| 1842 | return snprintf(buf, PAGE_SIZE, "%d\n", |
| 1843 | hba->hibern8_on_idle.is_enabled); |
| 1844 | } |
| 1845 | |
| 1846 | static ssize_t ufshcd_hibern8_on_idle_enable_store(struct device *dev, |
| 1847 | struct device_attribute *attr, const char *buf, size_t count) |
| 1848 | { |
| 1849 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1850 | unsigned long flags; |
| 1851 | u32 value; |
| 1852 | |
| 1853 | if (kstrtou32(buf, 0, &value)) |
| 1854 | return -EINVAL; |
| 1855 | |
| 1856 | value = !!value; |
| 1857 | if (value == hba->hibern8_on_idle.is_enabled) |
| 1858 | goto out; |
| 1859 | |
| 1860 | /* Update auto hibern8 timer value if supported */ |
| 1861 | if (ufshcd_is_auto_hibern8_supported(hba)) { |
| 1862 | ufshcd_set_auto_hibern8_timer(hba, |
| 1863 | value ? hba->hibern8_on_idle.delay_ms : value); |
| 1864 | goto update; |
| 1865 | } |
| 1866 | |
| 1867 | if (value) { |
| 1868 | /* |
| 1869 | * As clock gating work would wait for the hibern8 enter work |
| 1870 | * to finish, clocks would remain on during hibern8 enter work. |
| 1871 | */ |
| 1872 | ufshcd_hold(hba, false); |
| 1873 | ufshcd_release_all(hba); |
| 1874 | } else { |
| 1875 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1876 | hba->hibern8_on_idle.active_reqs++; |
| 1877 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1878 | } |
| 1879 | |
| 1880 | update: |
| 1881 | hba->hibern8_on_idle.is_enabled = value; |
| 1882 | out: |
| 1883 | return count; |
| 1884 | } |
| 1885 | |
| 1886 | static void ufshcd_init_hibern8_on_idle(struct ufs_hba *hba) |
| 1887 | { |
| 1888 | /* initialize the state variable here */ |
| 1889 | hba->hibern8_on_idle.state = HIBERN8_EXITED; |
| 1890 | |
| 1891 | if (!ufshcd_is_hibern8_on_idle_allowed(hba) && |
| 1892 | !ufshcd_is_auto_hibern8_supported(hba)) |
| 1893 | return; |
| 1894 | |
| 1895 | if (ufshcd_is_auto_hibern8_supported(hba)) { |
| 1896 | hba->hibern8_on_idle.state = AUTO_HIBERN8; |
| 1897 | /* |
| 1898 | * Disable SW hibern8 enter on idle in case |
| 1899 | * auto hibern8 is supported |
| 1900 | */ |
| 1901 | hba->caps &= ~UFSHCD_CAP_HIBERN8_ENTER_ON_IDLE; |
| 1902 | } else { |
| 1903 | INIT_DELAYED_WORK(&hba->hibern8_on_idle.enter_work, |
| 1904 | ufshcd_hibern8_enter_work); |
| 1905 | INIT_WORK(&hba->hibern8_on_idle.exit_work, |
| 1906 | ufshcd_hibern8_exit_work); |
| 1907 | } |
| 1908 | |
| 1909 | hba->hibern8_on_idle.delay_ms = 10; |
| 1910 | hba->hibern8_on_idle.is_enabled = true; |
| 1911 | |
| 1912 | hba->hibern8_on_idle.delay_attr.show = |
| 1913 | ufshcd_hibern8_on_idle_delay_show; |
| 1914 | hba->hibern8_on_idle.delay_attr.store = |
| 1915 | ufshcd_hibern8_on_idle_delay_store; |
| 1916 | sysfs_attr_init(&hba->hibern8_on_idle.delay_attr.attr); |
| 1917 | hba->hibern8_on_idle.delay_attr.attr.name = "hibern8_on_idle_delay_ms"; |
| 1918 | hba->hibern8_on_idle.delay_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 1919 | if (device_create_file(hba->dev, &hba->hibern8_on_idle.delay_attr)) |
| 1920 | dev_err(hba->dev, "Failed to create sysfs for hibern8_on_idle_delay\n"); |
| 1921 | |
| 1922 | hba->hibern8_on_idle.enable_attr.show = |
| 1923 | ufshcd_hibern8_on_idle_enable_show; |
| 1924 | hba->hibern8_on_idle.enable_attr.store = |
| 1925 | ufshcd_hibern8_on_idle_enable_store; |
| 1926 | sysfs_attr_init(&hba->hibern8_on_idle.enable_attr.attr); |
| 1927 | hba->hibern8_on_idle.enable_attr.attr.name = "hibern8_on_idle_enable"; |
| 1928 | hba->hibern8_on_idle.enable_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 1929 | if (device_create_file(hba->dev, &hba->hibern8_on_idle.enable_attr)) |
| 1930 | dev_err(hba->dev, "Failed to create sysfs for hibern8_on_idle_enable\n"); |
| 1931 | } |
| 1932 | |
| 1933 | static void ufshcd_exit_hibern8_on_idle(struct ufs_hba *hba) |
| 1934 | { |
| 1935 | if (!ufshcd_is_hibern8_on_idle_allowed(hba) && |
| 1936 | !ufshcd_is_auto_hibern8_supported(hba)) |
| 1937 | return; |
| 1938 | device_remove_file(hba->dev, &hba->hibern8_on_idle.delay_attr); |
| 1939 | device_remove_file(hba->dev, &hba->hibern8_on_idle.enable_attr); |
| 1940 | } |
| 1941 | |
| 1942 | static void ufshcd_hold_all(struct ufs_hba *hba) |
| 1943 | { |
| 1944 | ufshcd_hold(hba, false); |
| 1945 | ufshcd_hibern8_hold(hba, false); |
| 1946 | } |
| 1947 | |
| 1948 | static void ufshcd_release_all(struct ufs_hba *hba) |
| 1949 | { |
| 1950 | ufshcd_hibern8_release(hba, false); |
| 1951 | ufshcd_release(hba, false); |
| 1952 | } |
| 1953 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1954 | /* Must be called with host lock acquired */ |
| 1955 | static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba) |
| 1956 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1957 | bool queue_resume_work = false; |
| 1958 | |
| 1959 | if (!ufshcd_is_clkscaling_supported(hba)) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1960 | return; |
| 1961 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1962 | if (!hba->clk_scaling.active_reqs++) |
| 1963 | queue_resume_work = true; |
| 1964 | |
| 1965 | if (!hba->clk_scaling.is_allowed || hba->pm_op_in_progress) |
| 1966 | return; |
| 1967 | |
| 1968 | if (queue_resume_work) |
| 1969 | queue_work(hba->clk_scaling.workq, |
| 1970 | &hba->clk_scaling.resume_work); |
| 1971 | |
| 1972 | if (!hba->clk_scaling.window_start_t) { |
| 1973 | hba->clk_scaling.window_start_t = jiffies; |
| 1974 | hba->clk_scaling.tot_busy_t = 0; |
| 1975 | hba->clk_scaling.is_busy_started = false; |
| 1976 | } |
| 1977 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1978 | if (!hba->clk_scaling.is_busy_started) { |
| 1979 | hba->clk_scaling.busy_start_t = ktime_get(); |
| 1980 | hba->clk_scaling.is_busy_started = true; |
| 1981 | } |
| 1982 | } |
| 1983 | |
| 1984 | static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba) |
| 1985 | { |
| 1986 | struct ufs_clk_scaling *scaling = &hba->clk_scaling; |
| 1987 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1988 | if (!ufshcd_is_clkscaling_supported(hba)) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1989 | return; |
| 1990 | |
| 1991 | if (!hba->outstanding_reqs && scaling->is_busy_started) { |
| 1992 | scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(), |
| 1993 | scaling->busy_start_t)); |
| 1994 | scaling->busy_start_t = ktime_set(0, 0); |
| 1995 | scaling->is_busy_started = false; |
| 1996 | } |
| 1997 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1998 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1999 | /** |
| 2000 | * ufshcd_send_command - Send SCSI or device management commands |
| 2001 | * @hba: per adapter instance |
| 2002 | * @task_tag: Task tag of the command |
| 2003 | */ |
| 2004 | static inline |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2005 | int ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2006 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2007 | int ret = 0; |
| 2008 | |
| 2009 | hba->lrb[task_tag].issue_time_stamp = ktime_get(); |
| 2010 | hba->lrb[task_tag].complete_time_stamp = ktime_set(0, 0); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 2011 | ufshcd_clk_scaling_start_busy(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2012 | __set_bit(task_tag, &hba->outstanding_reqs); |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 2013 | ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2014 | /* Make sure that doorbell is committed immediately */ |
| 2015 | wmb(); |
| 2016 | ufshcd_cond_add_cmd_trace(hba, task_tag, "send"); |
| 2017 | ufshcd_update_tag_stats(hba, task_tag); |
| 2018 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | /** |
| 2022 | * ufshcd_copy_sense_data - Copy sense data in case of check condition |
| 2023 | * @lrb - pointer to local reference block |
| 2024 | */ |
| 2025 | static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp) |
| 2026 | { |
| 2027 | int len; |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 2028 | if (lrbp->sense_buffer && |
| 2029 | ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2030 | int len_to_copy; |
| 2031 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2032 | len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2033 | len_to_copy = min_t(int, RESPONSE_UPIU_SENSE_DATA_LENGTH, len); |
| 2034 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2035 | memcpy(lrbp->sense_buffer, |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2036 | lrbp->ucd_rsp_ptr->sr.sense_data, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2037 | min_t(int, len_to_copy, UFSHCD_REQ_SENSE_SIZE)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2038 | } |
| 2039 | } |
| 2040 | |
| 2041 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2042 | * ufshcd_copy_query_response() - Copy the Query Response and the data |
| 2043 | * descriptor |
| 2044 | * @hba: per adapter instance |
| 2045 | * @lrb - pointer to local reference block |
| 2046 | */ |
| 2047 | static |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2048 | 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] | 2049 | { |
| 2050 | struct ufs_query_res *query_res = &hba->dev_cmd.query.response; |
| 2051 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2052 | 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] | 2053 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2054 | /* Get the descriptor */ |
| 2055 | if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2056 | u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2057 | GENERAL_UPIU_REQUEST_SIZE; |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2058 | u16 resp_len; |
| 2059 | u16 buf_len; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2060 | |
| 2061 | /* data segment length */ |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2062 | resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) & |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2063 | MASK_QUERY_DATA_SEG_LEN; |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 2064 | buf_len = be16_to_cpu( |
| 2065 | hba->dev_cmd.query.request.upiu_req.length); |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2066 | if (likely(buf_len >= resp_len)) { |
| 2067 | memcpy(hba->dev_cmd.query.descriptor, descp, resp_len); |
| 2068 | } else { |
| 2069 | dev_warn(hba->dev, |
| 2070 | "%s: Response size is bigger than buffer", |
| 2071 | __func__); |
| 2072 | return -EINVAL; |
| 2073 | } |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2074 | } |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2075 | |
| 2076 | return 0; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2077 | } |
| 2078 | |
| 2079 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2080 | * ufshcd_hba_capabilities - Read controller capabilities |
| 2081 | * @hba: per adapter instance |
| 2082 | */ |
| 2083 | static inline void ufshcd_hba_capabilities(struct ufs_hba *hba) |
| 2084 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 2085 | hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2086 | |
| 2087 | /* nutrs and nutmrs are 0 based values */ |
| 2088 | hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1; |
| 2089 | hba->nutmrs = |
| 2090 | ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1; |
| 2091 | } |
| 2092 | |
| 2093 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2094 | * ufshcd_ready_for_uic_cmd - Check if controller is ready |
| 2095 | * to accept UIC commands |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2096 | * @hba: per adapter instance |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2097 | * Return true on success, else false |
| 2098 | */ |
| 2099 | static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba) |
| 2100 | { |
| 2101 | if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY) |
| 2102 | return true; |
| 2103 | else |
| 2104 | return false; |
| 2105 | } |
| 2106 | |
| 2107 | /** |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 2108 | * ufshcd_get_upmcrs - Get the power mode change request status |
| 2109 | * @hba: Pointer to adapter instance |
| 2110 | * |
| 2111 | * This function gets the UPMCRS field of HCS register |
| 2112 | * Returns value of UPMCRS field |
| 2113 | */ |
| 2114 | static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba) |
| 2115 | { |
| 2116 | return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7; |
| 2117 | } |
| 2118 | |
| 2119 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2120 | * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers |
| 2121 | * @hba: per adapter instance |
| 2122 | * @uic_cmd: UIC command |
| 2123 | * |
| 2124 | * Mutex must be held. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2125 | */ |
| 2126 | static inline void |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2127 | 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] | 2128 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2129 | WARN_ON(hba->active_uic_cmd); |
| 2130 | |
| 2131 | hba->active_uic_cmd = uic_cmd; |
| 2132 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2133 | /* Write Args */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2134 | ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1); |
| 2135 | ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2); |
| 2136 | ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2137 | |
| 2138 | /* Write UIC Cmd */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2139 | ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK, |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 2140 | REG_UIC_COMMAND); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2141 | } |
| 2142 | |
| 2143 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2144 | * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command |
| 2145 | * @hba: per adapter instance |
| 2146 | * @uic_command: UIC command |
| 2147 | * |
| 2148 | * Must be called with mutex held. |
| 2149 | * Returns 0 only if success. |
| 2150 | */ |
| 2151 | static int |
| 2152 | ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) |
| 2153 | { |
| 2154 | int ret; |
| 2155 | unsigned long flags; |
| 2156 | |
| 2157 | if (wait_for_completion_timeout(&uic_cmd->done, |
| 2158 | msecs_to_jiffies(UIC_CMD_TIMEOUT))) |
| 2159 | ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT; |
| 2160 | else |
| 2161 | ret = -ETIMEDOUT; |
| 2162 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2163 | if (ret) |
| 2164 | ufsdbg_set_err_state(hba); |
| 2165 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2166 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2167 | hba->active_uic_cmd = NULL; |
| 2168 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2169 | |
| 2170 | return ret; |
| 2171 | } |
| 2172 | |
| 2173 | /** |
| 2174 | * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result |
| 2175 | * @hba: per adapter instance |
| 2176 | * @uic_cmd: UIC command |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2177 | * @completion: initialize the completion only if this is set to true |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2178 | * |
| 2179 | * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2180 | * with mutex held and host_lock locked. |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2181 | * Returns 0 only if success. |
| 2182 | */ |
| 2183 | static int |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2184 | __ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd, |
| 2185 | bool completion) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2186 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2187 | if (!ufshcd_ready_for_uic_cmd(hba)) { |
| 2188 | dev_err(hba->dev, |
| 2189 | "Controller not ready to accept UIC commands\n"); |
| 2190 | return -EIO; |
| 2191 | } |
| 2192 | |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2193 | if (completion) |
| 2194 | init_completion(&uic_cmd->done); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2195 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2196 | ufshcd_dispatch_uic_cmd(hba, uic_cmd); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2197 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2198 | return 0; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2199 | } |
| 2200 | |
| 2201 | /** |
| 2202 | * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result |
| 2203 | * @hba: per adapter instance |
| 2204 | * @uic_cmd: UIC command |
| 2205 | * |
| 2206 | * Returns 0 only if success. |
| 2207 | */ |
| 2208 | static int |
| 2209 | ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) |
| 2210 | { |
| 2211 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2212 | unsigned long flags; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2213 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2214 | ufshcd_hold_all(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2215 | mutex_lock(&hba->uic_cmd_mutex); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 2216 | ufshcd_add_delay_before_dme_cmd(hba); |
| 2217 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2218 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2219 | ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2220 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2221 | if (!ret) |
| 2222 | ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd); |
| 2223 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2224 | ufshcd_save_tstamp_of_last_dme_cmd(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2225 | mutex_unlock(&hba->uic_cmd_mutex); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2226 | ufshcd_release_all(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2227 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2228 | ufsdbg_error_inject_dispatcher(hba, |
| 2229 | ERR_INJECT_UIC, 0, &ret); |
| 2230 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2231 | return ret; |
| 2232 | } |
| 2233 | |
| 2234 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2235 | * ufshcd_map_sg - Map scatter-gather list to prdt |
| 2236 | * @lrbp - pointer to local reference block |
| 2237 | * |
| 2238 | * Returns 0 in case of success, non-zero value in case of failure |
| 2239 | */ |
| 2240 | static int ufshcd_map_sg(struct ufshcd_lrb *lrbp) |
| 2241 | { |
| 2242 | struct ufshcd_sg_entry *prd_table; |
| 2243 | struct scatterlist *sg; |
| 2244 | struct scsi_cmnd *cmd; |
| 2245 | int sg_segments; |
| 2246 | int i; |
| 2247 | |
| 2248 | cmd = lrbp->cmd; |
| 2249 | sg_segments = scsi_dma_map(cmd); |
| 2250 | if (sg_segments < 0) |
| 2251 | return sg_segments; |
| 2252 | |
| 2253 | if (sg_segments) { |
| 2254 | lrbp->utr_descriptor_ptr->prd_table_length = |
| 2255 | cpu_to_le16((u16) (sg_segments)); |
| 2256 | |
| 2257 | prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr; |
| 2258 | |
| 2259 | scsi_for_each_sg(cmd, sg, sg_segments, i) { |
| 2260 | prd_table[i].size = |
| 2261 | cpu_to_le32(((u32) sg_dma_len(sg))-1); |
| 2262 | prd_table[i].base_addr = |
| 2263 | cpu_to_le32(lower_32_bits(sg->dma_address)); |
| 2264 | prd_table[i].upper_addr = |
| 2265 | cpu_to_le32(upper_32_bits(sg->dma_address)); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2266 | prd_table[i].reserved = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2267 | } |
| 2268 | } else { |
| 2269 | lrbp->utr_descriptor_ptr->prd_table_length = 0; |
| 2270 | } |
| 2271 | |
| 2272 | return 0; |
| 2273 | } |
| 2274 | |
| 2275 | /** |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2276 | * ufshcd_enable_intr - enable interrupts |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2277 | * @hba: per adapter instance |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2278 | * @intrs: interrupt bits |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2279 | */ |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2280 | static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2281 | { |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2282 | u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
| 2283 | |
| 2284 | if (hba->ufs_version == UFSHCI_VERSION_10) { |
| 2285 | u32 rw; |
| 2286 | rw = set & INTERRUPT_MASK_RW_VER_10; |
| 2287 | set = rw | ((set ^ intrs) & intrs); |
| 2288 | } else { |
| 2289 | set |= intrs; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2290 | } |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2291 | |
| 2292 | ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE); |
| 2293 | } |
| 2294 | |
| 2295 | /** |
| 2296 | * ufshcd_disable_intr - disable interrupts |
| 2297 | * @hba: per adapter instance |
| 2298 | * @intrs: interrupt bits |
| 2299 | */ |
| 2300 | static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs) |
| 2301 | { |
| 2302 | u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
| 2303 | |
| 2304 | if (hba->ufs_version == UFSHCI_VERSION_10) { |
| 2305 | u32 rw; |
| 2306 | rw = (set & INTERRUPT_MASK_RW_VER_10) & |
| 2307 | ~(intrs & INTERRUPT_MASK_RW_VER_10); |
| 2308 | set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10); |
| 2309 | |
| 2310 | } else { |
| 2311 | set &= ~intrs; |
| 2312 | } |
| 2313 | |
| 2314 | ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2315 | } |
| 2316 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2317 | static int ufshcd_prepare_crypto_utrd(struct ufs_hba *hba, |
| 2318 | struct ufshcd_lrb *lrbp) |
| 2319 | { |
| 2320 | struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr; |
| 2321 | u8 cc_index = 0; |
| 2322 | bool enable = false; |
| 2323 | u64 dun = 0; |
| 2324 | int ret; |
| 2325 | |
| 2326 | /* |
| 2327 | * Call vendor specific code to get crypto info for this request: |
| 2328 | * enable, crypto config. index, DUN. |
| 2329 | * If bypass is set, don't bother setting the other fields. |
| 2330 | */ |
| 2331 | ret = ufshcd_vops_crypto_req_setup(hba, lrbp, &cc_index, &enable, &dun); |
| 2332 | if (ret) { |
| 2333 | if (ret != -EAGAIN) { |
| 2334 | dev_err(hba->dev, |
| 2335 | "%s: failed to setup crypto request (%d)\n", |
| 2336 | __func__, ret); |
| 2337 | } |
| 2338 | |
| 2339 | return ret; |
| 2340 | } |
| 2341 | |
| 2342 | if (!enable) |
| 2343 | goto out; |
| 2344 | |
| 2345 | req_desc->header.dword_0 |= cc_index | UTRD_CRYPTO_ENABLE; |
| 2346 | if (lrbp->cmd->request && lrbp->cmd->request->bio) |
| 2347 | dun = lrbp->cmd->request->bio->bi_iter.bi_sector; |
| 2348 | |
| 2349 | req_desc->header.dword_1 = (u32)(dun & 0xFFFFFFFF); |
| 2350 | req_desc->header.dword_3 = (u32)((dun >> 32) & 0xFFFFFFFF); |
| 2351 | out: |
| 2352 | return 0; |
| 2353 | } |
| 2354 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2355 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2356 | * ufshcd_prepare_req_desc_hdr() - Fills the requests header |
| 2357 | * descriptor according to request |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2358 | * @hba: per adapter instance |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2359 | * @lrbp: pointer to local reference block |
| 2360 | * @upiu_flags: flags required in the header |
| 2361 | * @cmd_dir: requests data direction |
| 2362 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2363 | static int ufshcd_prepare_req_desc_hdr(struct ufs_hba *hba, |
| 2364 | struct ufshcd_lrb *lrbp, u32 *upiu_flags, |
| 2365 | enum dma_data_direction cmd_dir) |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2366 | { |
| 2367 | struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr; |
| 2368 | u32 data_direction; |
| 2369 | u32 dword_0; |
| 2370 | |
| 2371 | if (cmd_dir == DMA_FROM_DEVICE) { |
| 2372 | data_direction = UTP_DEVICE_TO_HOST; |
| 2373 | *upiu_flags = UPIU_CMD_FLAGS_READ; |
| 2374 | } else if (cmd_dir == DMA_TO_DEVICE) { |
| 2375 | data_direction = UTP_HOST_TO_DEVICE; |
| 2376 | *upiu_flags = UPIU_CMD_FLAGS_WRITE; |
| 2377 | } else { |
| 2378 | data_direction = UTP_NO_DATA_TRANSFER; |
| 2379 | *upiu_flags = UPIU_CMD_FLAGS_NONE; |
| 2380 | } |
| 2381 | |
| 2382 | dword_0 = data_direction | (lrbp->command_type |
| 2383 | << UPIU_COMMAND_TYPE_OFFSET); |
| 2384 | if (lrbp->intr_cmd) |
| 2385 | dword_0 |= UTP_REQ_DESC_INT_CMD; |
| 2386 | |
| 2387 | /* Transfer request descriptor header fields */ |
| 2388 | req_desc->header.dword_0 = cpu_to_le32(dword_0); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2389 | /* dword_1 is reserved, hence it is set to 0 */ |
| 2390 | req_desc->header.dword_1 = 0; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2391 | /* |
| 2392 | * assigning invalid value for command status. Controller |
| 2393 | * updates OCS on command completion, with the command |
| 2394 | * status |
| 2395 | */ |
| 2396 | req_desc->header.dword_2 = |
| 2397 | cpu_to_le32(OCS_INVALID_COMMAND_STATUS); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2398 | /* dword_3 is reserved, hence it is set to 0 */ |
| 2399 | req_desc->header.dword_3 = 0; |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 2400 | |
| 2401 | req_desc->prd_table_length = 0; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2402 | |
| 2403 | if (ufshcd_is_crypto_supported(hba)) |
| 2404 | return ufshcd_prepare_crypto_utrd(hba, lrbp); |
| 2405 | |
| 2406 | return 0; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2407 | } |
| 2408 | |
| 2409 | /** |
| 2410 | * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc, |
| 2411 | * for scsi commands |
| 2412 | * @lrbp - local reference block pointer |
| 2413 | * @upiu_flags - flags |
| 2414 | */ |
| 2415 | static |
| 2416 | void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags) |
| 2417 | { |
| 2418 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2419 | unsigned short cdb_len; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2420 | |
| 2421 | /* command descriptor fields */ |
| 2422 | ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD( |
| 2423 | UPIU_TRANSACTION_COMMAND, upiu_flags, |
| 2424 | lrbp->lun, lrbp->task_tag); |
| 2425 | ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD( |
| 2426 | UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0); |
| 2427 | |
| 2428 | /* Total EHS length and Data segment length will be zero */ |
| 2429 | ucd_req_ptr->header.dword_2 = 0; |
| 2430 | |
| 2431 | ucd_req_ptr->sc.exp_data_transfer_len = |
| 2432 | cpu_to_be32(lrbp->cmd->sdb.length); |
| 2433 | |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2434 | cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2435 | memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2436 | if (cdb_len < MAX_CDB_SIZE) |
| 2437 | memset(ucd_req_ptr->sc.cdb + cdb_len, 0, |
| 2438 | (MAX_CDB_SIZE - cdb_len)); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2439 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2440 | } |
| 2441 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2442 | /** |
| 2443 | * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc, |
| 2444 | * for query requsts |
| 2445 | * @hba: UFS hba |
| 2446 | * @lrbp: local reference block pointer |
| 2447 | * @upiu_flags: flags |
| 2448 | */ |
| 2449 | static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba, |
| 2450 | struct ufshcd_lrb *lrbp, u32 upiu_flags) |
| 2451 | { |
| 2452 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
| 2453 | struct ufs_query *query = &hba->dev_cmd.query; |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 2454 | u16 len = be16_to_cpu(query->request.upiu_req.length); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2455 | u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE; |
| 2456 | |
| 2457 | /* Query request header */ |
| 2458 | ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD( |
| 2459 | UPIU_TRANSACTION_QUERY_REQ, upiu_flags, |
| 2460 | lrbp->lun, lrbp->task_tag); |
| 2461 | ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD( |
| 2462 | 0, query->request.query_func, 0, 0); |
| 2463 | |
Zang Leigang | 6861285 | 2016-08-25 17:39:19 +0800 | [diff] [blame] | 2464 | /* Data segment length only need for WRITE_DESC */ |
| 2465 | if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) |
| 2466 | ucd_req_ptr->header.dword_2 = |
| 2467 | UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len); |
| 2468 | else |
| 2469 | ucd_req_ptr->header.dword_2 = 0; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2470 | |
| 2471 | /* Copy the Query Request buffer as is */ |
| 2472 | memcpy(&ucd_req_ptr->qr, &query->request.upiu_req, |
| 2473 | QUERY_OSF_SIZE); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2474 | |
| 2475 | /* Copy the Descriptor */ |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2476 | if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) |
| 2477 | memcpy(descp, query->descriptor, len); |
| 2478 | |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 2479 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2480 | } |
| 2481 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2482 | static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp) |
| 2483 | { |
| 2484 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
| 2485 | |
| 2486 | memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req)); |
| 2487 | |
| 2488 | /* command descriptor fields */ |
| 2489 | ucd_req_ptr->header.dword_0 = |
| 2490 | UPIU_HEADER_DWORD( |
| 2491 | UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag); |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 2492 | /* clear rest of the fields of basic header */ |
| 2493 | ucd_req_ptr->header.dword_1 = 0; |
| 2494 | ucd_req_ptr->header.dword_2 = 0; |
| 2495 | |
| 2496 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2497 | } |
| 2498 | |
| 2499 | /** |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2500 | * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU) |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2501 | * @hba - per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2502 | * @lrb - pointer to local reference block |
| 2503 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2504 | static int ufshcd_compose_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2505 | { |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2506 | u32 upiu_flags; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2507 | int ret = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2508 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2509 | switch (lrbp->command_type) { |
| 2510 | case UTP_CMD_TYPE_SCSI: |
| 2511 | if (likely(lrbp->cmd)) { |
| 2512 | ret = ufshcd_prepare_req_desc_hdr(hba, lrbp, |
| 2513 | &upiu_flags, lrbp->cmd->sc_data_direction); |
| 2514 | ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags); |
| 2515 | } else { |
| 2516 | ret = -EINVAL; |
| 2517 | } |
| 2518 | break; |
| 2519 | case UTP_CMD_TYPE_DEV_MANAGE: |
| 2520 | ret = ufshcd_prepare_req_desc_hdr(hba, lrbp, &upiu_flags, |
| 2521 | DMA_NONE); |
| 2522 | if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY) |
| 2523 | ufshcd_prepare_utp_query_req_upiu( |
| 2524 | hba, lrbp, upiu_flags); |
| 2525 | else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP) |
| 2526 | ufshcd_prepare_utp_nop_upiu(lrbp); |
| 2527 | else |
| 2528 | ret = -EINVAL; |
| 2529 | break; |
| 2530 | case UTP_CMD_TYPE_UFS: |
| 2531 | /* For UFS native command implementation */ |
| 2532 | ret = -ENOTSUPP; |
| 2533 | dev_err(hba->dev, "%s: UFS native command are not supported\n", |
| 2534 | __func__); |
| 2535 | break; |
| 2536 | default: |
| 2537 | ret = -ENOTSUPP; |
| 2538 | dev_err(hba->dev, "%s: unknown command type: 0x%x\n", |
| 2539 | __func__, lrbp->command_type); |
| 2540 | break; |
| 2541 | } /* end of switch */ |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2542 | |
| 2543 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2544 | } |
| 2545 | |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 2546 | /* |
| 2547 | * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN |
| 2548 | * @scsi_lun: scsi LUN id |
| 2549 | * |
| 2550 | * Returns UPIU LUN id |
| 2551 | */ |
| 2552 | static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun) |
| 2553 | { |
| 2554 | if (scsi_is_wlun(scsi_lun)) |
| 2555 | return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID) |
| 2556 | | UFS_UPIU_WLUN_ID; |
| 2557 | else |
| 2558 | return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID; |
| 2559 | } |
| 2560 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2561 | /** |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 2562 | * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID |
| 2563 | * @scsi_lun: UPIU W-LUN id |
| 2564 | * |
| 2565 | * Returns SCSI W-LUN id |
| 2566 | */ |
| 2567 | static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id) |
| 2568 | { |
| 2569 | return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE; |
| 2570 | } |
| 2571 | |
| 2572 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2573 | * ufshcd_queuecommand - main entry point for SCSI requests |
| 2574 | * @cmd: command from SCSI Midlayer |
| 2575 | * @done: call back function |
| 2576 | * |
| 2577 | * Returns 0 for success, non-zero in case of failure |
| 2578 | */ |
| 2579 | static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) |
| 2580 | { |
| 2581 | struct ufshcd_lrb *lrbp; |
| 2582 | struct ufs_hba *hba; |
| 2583 | unsigned long flags; |
| 2584 | int tag; |
| 2585 | int err = 0; |
| 2586 | |
| 2587 | hba = shost_priv(host); |
| 2588 | |
| 2589 | tag = cmd->request->tag; |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 2590 | if (!ufshcd_valid_tag(hba, tag)) { |
| 2591 | dev_err(hba->dev, |
| 2592 | "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p", |
| 2593 | __func__, tag, cmd, cmd->request); |
| 2594 | BUG(); |
| 2595 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2596 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2597 | if (!down_read_trylock(&hba->clk_scaling_lock)) |
| 2598 | return SCSI_MLQUEUE_HOST_BUSY; |
| 2599 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2600 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2601 | switch (hba->ufshcd_state) { |
| 2602 | case UFSHCD_STATE_OPERATIONAL: |
| 2603 | break; |
| 2604 | case UFSHCD_STATE_RESET: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2605 | err = SCSI_MLQUEUE_HOST_BUSY; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2606 | goto out_unlock; |
| 2607 | case UFSHCD_STATE_ERROR: |
| 2608 | set_host_byte(cmd, DID_ERROR); |
| 2609 | cmd->scsi_done(cmd); |
| 2610 | goto out_unlock; |
| 2611 | default: |
| 2612 | dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n", |
| 2613 | __func__, hba->ufshcd_state); |
| 2614 | set_host_byte(cmd, DID_BAD_TARGET); |
| 2615 | cmd->scsi_done(cmd); |
| 2616 | goto out_unlock; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2617 | } |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 2618 | |
| 2619 | /* if error handling is in progress, don't issue commands */ |
| 2620 | if (ufshcd_eh_in_progress(hba)) { |
| 2621 | set_host_byte(cmd, DID_ERROR); |
| 2622 | cmd->scsi_done(cmd); |
| 2623 | goto out_unlock; |
| 2624 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2625 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2626 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2627 | hba->req_abort_count = 0; |
| 2628 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2629 | /* acquire the tag to make sure device cmds don't use it */ |
| 2630 | if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) { |
| 2631 | /* |
| 2632 | * Dev manage command in progress, requeue the command. |
| 2633 | * Requeuing the command helps in cases where the request *may* |
| 2634 | * find different tag instead of waiting for dev manage command |
| 2635 | * completion. |
| 2636 | */ |
| 2637 | err = SCSI_MLQUEUE_HOST_BUSY; |
| 2638 | goto out; |
| 2639 | } |
| 2640 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2641 | err = ufshcd_hold(hba, true); |
| 2642 | if (err) { |
| 2643 | err = SCSI_MLQUEUE_HOST_BUSY; |
| 2644 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 2645 | goto out; |
| 2646 | } |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 2647 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2648 | if (ufshcd_is_clkgating_allowed(hba)) |
| 2649 | WARN_ON(hba->clk_gating.state != CLKS_ON); |
| 2650 | |
| 2651 | err = ufshcd_hibern8_hold(hba, true); |
| 2652 | if (err) { |
| 2653 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 2654 | err = SCSI_MLQUEUE_HOST_BUSY; |
| 2655 | ufshcd_release(hba, true); |
| 2656 | goto out; |
| 2657 | } |
| 2658 | if (ufshcd_is_hibern8_on_idle_allowed(hba)) |
| 2659 | WARN_ON(hba->hibern8_on_idle.state != HIBERN8_EXITED); |
| 2660 | |
| 2661 | /* Vote PM QoS for the request */ |
| 2662 | ufshcd_vops_pm_qos_req_start(hba, cmd->request); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2663 | |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 2664 | /* IO svc time latency histogram */ |
| 2665 | if (hba != NULL && cmd->request != NULL) { |
| 2666 | if (hba->latency_hist_enabled && |
| 2667 | (cmd->request->cmd_type == REQ_TYPE_FS)) { |
| 2668 | cmd->request->lat_hist_io_start = ktime_get(); |
| 2669 | cmd->request->lat_hist_enabled = 1; |
| 2670 | } else |
| 2671 | cmd->request->lat_hist_enabled = 0; |
| 2672 | } |
| 2673 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2674 | WARN_ON(hba->clk_gating.state != CLKS_ON); |
| 2675 | |
| 2676 | lrbp = &hba->lrb[tag]; |
| 2677 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2678 | WARN_ON(lrbp->cmd); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2679 | lrbp->cmd = cmd; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2680 | lrbp->sense_bufflen = UFSHCD_REQ_SENSE_SIZE; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2681 | lrbp->sense_buffer = cmd->sense_buffer; |
| 2682 | lrbp->task_tag = tag; |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 2683 | lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun); |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 2684 | lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2685 | lrbp->command_type = UTP_CMD_TYPE_SCSI; |
| 2686 | lrbp->req_abort_skip = false; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2687 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2688 | /* form UPIU before issuing the command */ |
| 2689 | err = ufshcd_compose_upiu(hba, lrbp); |
| 2690 | if (err) { |
| 2691 | if (err != -EAGAIN) |
| 2692 | dev_err(hba->dev, |
| 2693 | "%s: failed to compose upiu %d\n", |
| 2694 | __func__, err); |
| 2695 | |
| 2696 | lrbp->cmd = NULL; |
| 2697 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 2698 | ufshcd_release_all(hba); |
| 2699 | ufshcd_vops_pm_qos_req_end(hba, cmd->request, true); |
| 2700 | goto out; |
| 2701 | } |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2702 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2703 | err = ufshcd_map_sg(lrbp); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2704 | if (err) { |
| 2705 | lrbp->cmd = NULL; |
| 2706 | clear_bit_unlock(tag, &hba->lrb_in_use); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2707 | ufshcd_release_all(hba); |
| 2708 | ufshcd_vops_pm_qos_req_end(hba, cmd->request, true); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2709 | goto out; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2710 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2711 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2712 | err = ufshcd_vops_crypto_engine_cfg_start(hba, tag); |
| 2713 | if (err) { |
| 2714 | if (err != -EAGAIN) |
| 2715 | dev_err(hba->dev, |
| 2716 | "%s: failed to configure crypto engine %d\n", |
| 2717 | __func__, err); |
| 2718 | |
| 2719 | scsi_dma_unmap(lrbp->cmd); |
| 2720 | lrbp->cmd = NULL; |
| 2721 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 2722 | ufshcd_release_all(hba); |
| 2723 | ufshcd_vops_pm_qos_req_end(hba, cmd->request, true); |
| 2724 | |
| 2725 | goto out; |
| 2726 | } |
| 2727 | |
| 2728 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 2729 | wmb(); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2730 | /* issue command to the controller */ |
| 2731 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2732 | |
| 2733 | err = ufshcd_send_command(hba, tag); |
| 2734 | if (err) { |
| 2735 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2736 | scsi_dma_unmap(lrbp->cmd); |
| 2737 | lrbp->cmd = NULL; |
| 2738 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 2739 | ufshcd_release_all(hba); |
| 2740 | ufshcd_vops_pm_qos_req_end(hba, cmd->request, true); |
| 2741 | ufshcd_vops_crypto_engine_cfg_end(hba, lrbp, cmd->request); |
| 2742 | dev_err(hba->dev, "%s: failed sending command, %d\n", |
| 2743 | __func__, err); |
| 2744 | err = DID_ERROR; |
| 2745 | goto out; |
| 2746 | } |
| 2747 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2748 | out_unlock: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2749 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2750 | out: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2751 | up_read(&hba->clk_scaling_lock); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2752 | return err; |
| 2753 | } |
| 2754 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2755 | static int ufshcd_compose_dev_cmd(struct ufs_hba *hba, |
| 2756 | struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag) |
| 2757 | { |
| 2758 | lrbp->cmd = NULL; |
| 2759 | lrbp->sense_bufflen = 0; |
| 2760 | lrbp->sense_buffer = NULL; |
| 2761 | lrbp->task_tag = tag; |
| 2762 | lrbp->lun = 0; /* device management cmd is not specific to any LUN */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2763 | lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2764 | lrbp->intr_cmd = true; /* No interrupt aggregation */ |
| 2765 | hba->dev_cmd.type = cmd_type; |
| 2766 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2767 | return ufshcd_compose_upiu(hba, lrbp); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2768 | } |
| 2769 | |
| 2770 | static int |
| 2771 | ufshcd_clear_cmd(struct ufs_hba *hba, int tag) |
| 2772 | { |
| 2773 | int err = 0; |
| 2774 | unsigned long flags; |
| 2775 | u32 mask = 1 << tag; |
| 2776 | |
| 2777 | /* clear outstanding transaction before retry */ |
| 2778 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2779 | ufshcd_utrl_clear(hba, tag); |
| 2780 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2781 | |
| 2782 | /* |
| 2783 | * wait for for h/w to clear corresponding bit in door-bell. |
| 2784 | * max. wait is 1 sec. |
| 2785 | */ |
| 2786 | err = ufshcd_wait_for_register(hba, |
| 2787 | REG_UTP_TRANSFER_REQ_DOOR_BELL, |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 2788 | mask, ~mask, 1000, 1000, true); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2789 | |
| 2790 | return err; |
| 2791 | } |
| 2792 | |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2793 | static int |
| 2794 | ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 2795 | { |
| 2796 | struct ufs_query_res *query_res = &hba->dev_cmd.query.response; |
| 2797 | |
| 2798 | /* Get the UPIU response */ |
| 2799 | query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >> |
| 2800 | UPIU_RSP_CODE_OFFSET; |
| 2801 | return query_res->response; |
| 2802 | } |
| 2803 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2804 | /** |
| 2805 | * ufshcd_dev_cmd_completion() - handles device management command responses |
| 2806 | * @hba: per adapter instance |
| 2807 | * @lrbp: pointer to local reference block |
| 2808 | */ |
| 2809 | static int |
| 2810 | ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 2811 | { |
| 2812 | int resp; |
| 2813 | int err = 0; |
| 2814 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2815 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2816 | resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr); |
| 2817 | |
| 2818 | switch (resp) { |
| 2819 | case UPIU_TRANSACTION_NOP_IN: |
| 2820 | if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) { |
| 2821 | err = -EINVAL; |
| 2822 | dev_err(hba->dev, "%s: unexpected response %x\n", |
| 2823 | __func__, resp); |
| 2824 | } |
| 2825 | break; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2826 | case UPIU_TRANSACTION_QUERY_RSP: |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2827 | err = ufshcd_check_query_response(hba, lrbp); |
| 2828 | if (!err) |
| 2829 | err = ufshcd_copy_query_response(hba, lrbp); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2830 | break; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2831 | case UPIU_TRANSACTION_REJECT_UPIU: |
| 2832 | /* TODO: handle Reject UPIU Response */ |
| 2833 | err = -EPERM; |
| 2834 | dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n", |
| 2835 | __func__); |
| 2836 | break; |
| 2837 | default: |
| 2838 | err = -EINVAL; |
| 2839 | dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n", |
| 2840 | __func__, resp); |
| 2841 | break; |
| 2842 | } |
| 2843 | |
| 2844 | return err; |
| 2845 | } |
| 2846 | |
| 2847 | static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba, |
| 2848 | struct ufshcd_lrb *lrbp, int max_timeout) |
| 2849 | { |
| 2850 | int err = 0; |
| 2851 | unsigned long time_left; |
| 2852 | unsigned long flags; |
| 2853 | |
| 2854 | time_left = wait_for_completion_timeout(hba->dev_cmd.complete, |
| 2855 | msecs_to_jiffies(max_timeout)); |
| 2856 | |
| 2857 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2858 | hba->dev_cmd.complete = NULL; |
| 2859 | if (likely(time_left)) { |
| 2860 | err = ufshcd_get_tr_ocs(lrbp); |
| 2861 | if (!err) |
| 2862 | err = ufshcd_dev_cmd_completion(hba, lrbp); |
| 2863 | } |
| 2864 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2865 | |
| 2866 | if (!time_left) { |
| 2867 | err = -ETIMEDOUT; |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 2868 | dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n", |
| 2869 | __func__, lrbp->task_tag); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2870 | if (!ufshcd_clear_cmd(hba, lrbp->task_tag)) |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 2871 | /* successfully cleared the command, retry if needed */ |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2872 | err = -EAGAIN; |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 2873 | /* |
| 2874 | * in case of an error, after clearing the doorbell, |
| 2875 | * we also need to clear the outstanding_request |
| 2876 | * field in hba |
| 2877 | */ |
| 2878 | ufshcd_outstanding_req_clear(hba, lrbp->task_tag); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2879 | } |
| 2880 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2881 | if (err) |
| 2882 | ufsdbg_set_err_state(hba); |
| 2883 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2884 | return err; |
| 2885 | } |
| 2886 | |
| 2887 | /** |
| 2888 | * ufshcd_get_dev_cmd_tag - Get device management command tag |
| 2889 | * @hba: per-adapter instance |
| 2890 | * @tag: pointer to variable with available slot value |
| 2891 | * |
| 2892 | * Get a free slot and lock it until device management command |
| 2893 | * completes. |
| 2894 | * |
| 2895 | * Returns false if free slot is unavailable for locking, else |
| 2896 | * return true with tag value in @tag. |
| 2897 | */ |
| 2898 | static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out) |
| 2899 | { |
| 2900 | int tag; |
| 2901 | bool ret = false; |
| 2902 | unsigned long tmp; |
| 2903 | |
| 2904 | if (!tag_out) |
| 2905 | goto out; |
| 2906 | |
| 2907 | do { |
| 2908 | tmp = ~hba->lrb_in_use; |
| 2909 | tag = find_last_bit(&tmp, hba->nutrs); |
| 2910 | if (tag >= hba->nutrs) |
| 2911 | goto out; |
| 2912 | } while (test_and_set_bit_lock(tag, &hba->lrb_in_use)); |
| 2913 | |
| 2914 | *tag_out = tag; |
| 2915 | ret = true; |
| 2916 | out: |
| 2917 | return ret; |
| 2918 | } |
| 2919 | |
| 2920 | static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag) |
| 2921 | { |
| 2922 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 2923 | } |
| 2924 | |
| 2925 | /** |
| 2926 | * ufshcd_exec_dev_cmd - API for sending device management requests |
| 2927 | * @hba - UFS hba |
| 2928 | * @cmd_type - specifies the type (NOP, Query...) |
| 2929 | * @timeout - time in seconds |
| 2930 | * |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2931 | * NOTE: Since there is only one available tag for device management commands, |
| 2932 | * it is expected you hold the hba->dev_cmd.lock mutex. |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2933 | */ |
| 2934 | static int ufshcd_exec_dev_cmd(struct ufs_hba *hba, |
| 2935 | enum dev_cmd_type cmd_type, int timeout) |
| 2936 | { |
| 2937 | struct ufshcd_lrb *lrbp; |
| 2938 | int err; |
| 2939 | int tag; |
| 2940 | struct completion wait; |
| 2941 | unsigned long flags; |
| 2942 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2943 | down_read(&hba->clk_scaling_lock); |
| 2944 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2945 | /* |
| 2946 | * Get free slot, sleep if slots are unavailable. |
| 2947 | * Even though we use wait_event() which sleeps indefinitely, |
| 2948 | * the maximum wait time is bounded by SCSI request timeout. |
| 2949 | */ |
| 2950 | wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag)); |
| 2951 | |
| 2952 | init_completion(&wait); |
| 2953 | lrbp = &hba->lrb[tag]; |
| 2954 | WARN_ON(lrbp->cmd); |
| 2955 | err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag); |
| 2956 | if (unlikely(err)) |
| 2957 | goto out_put_tag; |
| 2958 | |
| 2959 | hba->dev_cmd.complete = &wait; |
| 2960 | |
Yaniv Gardi | e3dfdc5 | 2016-02-01 15:02:49 +0200 | [diff] [blame] | 2961 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 2962 | wmb(); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2963 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2964 | err = ufshcd_send_command(hba, tag); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2965 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2966 | if (err) { |
| 2967 | dev_err(hba->dev, "%s: failed sending command, %d\n", |
| 2968 | __func__, err); |
| 2969 | goto out_put_tag; |
| 2970 | } |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2971 | err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout); |
| 2972 | |
| 2973 | out_put_tag: |
| 2974 | ufshcd_put_dev_cmd_tag(hba, tag); |
| 2975 | wake_up(&hba->dev_cmd.tag_wq); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2976 | up_read(&hba->clk_scaling_lock); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2977 | return err; |
| 2978 | } |
| 2979 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2980 | /** |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2981 | * ufshcd_init_query() - init the query response and request parameters |
| 2982 | * @hba: per-adapter instance |
| 2983 | * @request: address of the request pointer to be initialized |
| 2984 | * @response: address of the response pointer to be initialized |
| 2985 | * @opcode: operation to perform |
| 2986 | * @idn: flag idn to access |
| 2987 | * @index: LU number to access |
| 2988 | * @selector: query/flag/descriptor further identification |
| 2989 | */ |
| 2990 | static inline void ufshcd_init_query(struct ufs_hba *hba, |
| 2991 | struct ufs_query_req **request, struct ufs_query_res **response, |
| 2992 | enum query_opcode opcode, u8 idn, u8 index, u8 selector) |
| 2993 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2994 | int idn_t = (int)idn; |
| 2995 | |
| 2996 | ufsdbg_error_inject_dispatcher(hba, |
| 2997 | ERR_INJECT_QUERY, idn_t, (int *)&idn_t); |
| 2998 | idn = idn_t; |
| 2999 | |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3000 | *request = &hba->dev_cmd.query.request; |
| 3001 | *response = &hba->dev_cmd.query.response; |
| 3002 | memset(*request, 0, sizeof(struct ufs_query_req)); |
| 3003 | memset(*response, 0, sizeof(struct ufs_query_res)); |
| 3004 | (*request)->upiu_req.opcode = opcode; |
| 3005 | (*request)->upiu_req.idn = idn; |
| 3006 | (*request)->upiu_req.index = index; |
| 3007 | (*request)->upiu_req.selector = selector; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3008 | |
| 3009 | ufshcd_update_query_stats(hba, opcode, idn); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3010 | } |
| 3011 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 3012 | static int ufshcd_query_flag_retry(struct ufs_hba *hba, |
| 3013 | enum query_opcode opcode, enum flag_idn idn, bool *flag_res) |
| 3014 | { |
| 3015 | int ret; |
| 3016 | int retries; |
| 3017 | |
| 3018 | for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) { |
| 3019 | ret = ufshcd_query_flag(hba, opcode, idn, flag_res); |
| 3020 | if (ret) |
| 3021 | dev_dbg(hba->dev, |
| 3022 | "%s: failed with error %d, retries %d\n", |
| 3023 | __func__, ret, retries); |
| 3024 | else |
| 3025 | break; |
| 3026 | } |
| 3027 | |
| 3028 | if (ret) |
| 3029 | dev_err(hba->dev, |
| 3030 | "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n", |
| 3031 | __func__, opcode, idn, ret, retries); |
| 3032 | return ret; |
| 3033 | } |
| 3034 | |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3035 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3036 | * ufshcd_query_flag() - API function for sending flag query requests |
| 3037 | * hba: per-adapter instance |
| 3038 | * query_opcode: flag query to perform |
| 3039 | * idn: flag idn to access |
| 3040 | * flag_res: the flag value after the query request completes |
| 3041 | * |
| 3042 | * Returns 0 for success, non-zero in case of failure |
| 3043 | */ |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 3044 | int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode, |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3045 | enum flag_idn idn, bool *flag_res) |
| 3046 | { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3047 | struct ufs_query_req *request = NULL; |
| 3048 | struct ufs_query_res *response = NULL; |
| 3049 | int err, index = 0, selector = 0; |
Yaniv Gardi | e5ad406 | 2016-02-01 15:02:41 +0200 | [diff] [blame] | 3050 | int timeout = QUERY_REQ_TIMEOUT; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3051 | |
| 3052 | BUG_ON(!hba); |
| 3053 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3054 | ufshcd_hold_all(hba); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3055 | mutex_lock(&hba->dev_cmd.lock); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3056 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 3057 | selector); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3058 | |
| 3059 | switch (opcode) { |
| 3060 | case UPIU_QUERY_OPCODE_SET_FLAG: |
| 3061 | case UPIU_QUERY_OPCODE_CLEAR_FLAG: |
| 3062 | case UPIU_QUERY_OPCODE_TOGGLE_FLAG: |
| 3063 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
| 3064 | break; |
| 3065 | case UPIU_QUERY_OPCODE_READ_FLAG: |
| 3066 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 3067 | if (!flag_res) { |
| 3068 | /* No dummy reads */ |
| 3069 | dev_err(hba->dev, "%s: Invalid argument for read request\n", |
| 3070 | __func__); |
| 3071 | err = -EINVAL; |
| 3072 | goto out_unlock; |
| 3073 | } |
| 3074 | break; |
| 3075 | default: |
| 3076 | dev_err(hba->dev, |
| 3077 | "%s: Expected query flag opcode but got = %d\n", |
| 3078 | __func__, opcode); |
| 3079 | err = -EINVAL; |
| 3080 | goto out_unlock; |
| 3081 | } |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3082 | |
Yaniv Gardi | e5ad406 | 2016-02-01 15:02:41 +0200 | [diff] [blame] | 3083 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3084 | |
| 3085 | if (err) { |
| 3086 | dev_err(hba->dev, |
| 3087 | "%s: Sending flag query for idn %d failed, err = %d\n", |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3088 | __func__, request->upiu_req.idn, err); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3089 | goto out_unlock; |
| 3090 | } |
| 3091 | |
| 3092 | if (flag_res) |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 3093 | *flag_res = (be32_to_cpu(response->upiu_res.value) & |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3094 | MASK_QUERY_UPIU_FLAG_LOC) & 0x1; |
| 3095 | |
| 3096 | out_unlock: |
| 3097 | mutex_unlock(&hba->dev_cmd.lock); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3098 | ufshcd_release_all(hba); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3099 | return err; |
| 3100 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3101 | EXPORT_SYMBOL(ufshcd_query_flag); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3102 | |
| 3103 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3104 | * ufshcd_query_attr - API function for sending attribute requests |
| 3105 | * hba: per-adapter instance |
| 3106 | * opcode: attribute opcode |
| 3107 | * idn: attribute idn to access |
| 3108 | * index: index field |
| 3109 | * selector: selector field |
| 3110 | * attr_val: the attribute value after the query request completes |
| 3111 | * |
| 3112 | * Returns 0 for success, non-zero in case of failure |
| 3113 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3114 | 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] | 3115 | enum attr_idn idn, u8 index, u8 selector, u32 *attr_val) |
| 3116 | { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3117 | struct ufs_query_req *request = NULL; |
| 3118 | struct ufs_query_res *response = NULL; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3119 | int err; |
| 3120 | |
| 3121 | BUG_ON(!hba); |
| 3122 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3123 | ufshcd_hold_all(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3124 | if (!attr_val) { |
| 3125 | dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n", |
| 3126 | __func__, opcode); |
| 3127 | err = -EINVAL; |
| 3128 | goto out; |
| 3129 | } |
| 3130 | |
| 3131 | mutex_lock(&hba->dev_cmd.lock); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3132 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 3133 | selector); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3134 | |
| 3135 | switch (opcode) { |
| 3136 | case UPIU_QUERY_OPCODE_WRITE_ATTR: |
| 3137 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 3138 | request->upiu_req.value = cpu_to_be32(*attr_val); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3139 | break; |
| 3140 | case UPIU_QUERY_OPCODE_READ_ATTR: |
| 3141 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 3142 | break; |
| 3143 | default: |
| 3144 | dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n", |
| 3145 | __func__, opcode); |
| 3146 | err = -EINVAL; |
| 3147 | goto out_unlock; |
| 3148 | } |
| 3149 | |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3150 | 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] | 3151 | |
| 3152 | if (err) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3153 | dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n", |
| 3154 | __func__, opcode, |
| 3155 | request->upiu_req.idn, index, err); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3156 | goto out_unlock; |
| 3157 | } |
| 3158 | |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 3159 | *attr_val = be32_to_cpu(response->upiu_res.value); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3160 | |
| 3161 | out_unlock: |
| 3162 | mutex_unlock(&hba->dev_cmd.lock); |
| 3163 | out: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3164 | ufshcd_release_all(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3165 | return err; |
| 3166 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3167 | EXPORT_SYMBOL(ufshcd_query_attr); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3168 | |
| 3169 | /** |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 3170 | * ufshcd_query_attr_retry() - API function for sending query |
| 3171 | * attribute with retries |
| 3172 | * @hba: per-adapter instance |
| 3173 | * @opcode: attribute opcode |
| 3174 | * @idn: attribute idn to access |
| 3175 | * @index: index field |
| 3176 | * @selector: selector field |
| 3177 | * @attr_val: the attribute value after the query request |
| 3178 | * completes |
| 3179 | * |
| 3180 | * Returns 0 for success, non-zero in case of failure |
| 3181 | */ |
| 3182 | static int ufshcd_query_attr_retry(struct ufs_hba *hba, |
| 3183 | enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector, |
| 3184 | u32 *attr_val) |
| 3185 | { |
| 3186 | int ret = 0; |
| 3187 | u32 retries; |
| 3188 | |
| 3189 | for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { |
| 3190 | ret = ufshcd_query_attr(hba, opcode, idn, index, |
| 3191 | selector, attr_val); |
| 3192 | if (ret) |
| 3193 | dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n", |
| 3194 | __func__, ret, retries); |
| 3195 | else |
| 3196 | break; |
| 3197 | } |
| 3198 | |
| 3199 | if (ret) |
| 3200 | dev_err(hba->dev, |
| 3201 | "%s: query attribute, idn %d, failed with error %d after %d retires\n", |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3202 | __func__, idn, ret, retries); |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 3203 | return ret; |
| 3204 | } |
| 3205 | |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3206 | static int __ufshcd_query_descriptor(struct ufs_hba *hba, |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3207 | enum query_opcode opcode, enum desc_idn idn, u8 index, |
| 3208 | u8 selector, u8 *desc_buf, int *buf_len) |
| 3209 | { |
| 3210 | struct ufs_query_req *request = NULL; |
| 3211 | struct ufs_query_res *response = NULL; |
| 3212 | int err; |
| 3213 | |
| 3214 | BUG_ON(!hba); |
| 3215 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3216 | ufshcd_hold_all(hba); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3217 | if (!desc_buf) { |
| 3218 | dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n", |
| 3219 | __func__, opcode); |
| 3220 | err = -EINVAL; |
| 3221 | goto out; |
| 3222 | } |
| 3223 | |
| 3224 | if (*buf_len <= QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) { |
| 3225 | dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n", |
| 3226 | __func__, *buf_len); |
| 3227 | err = -EINVAL; |
| 3228 | goto out; |
| 3229 | } |
| 3230 | |
| 3231 | mutex_lock(&hba->dev_cmd.lock); |
| 3232 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 3233 | selector); |
| 3234 | hba->dev_cmd.query.descriptor = desc_buf; |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 3235 | request->upiu_req.length = cpu_to_be16(*buf_len); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3236 | |
| 3237 | switch (opcode) { |
| 3238 | case UPIU_QUERY_OPCODE_WRITE_DESC: |
| 3239 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
| 3240 | break; |
| 3241 | case UPIU_QUERY_OPCODE_READ_DESC: |
| 3242 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 3243 | break; |
| 3244 | default: |
| 3245 | dev_err(hba->dev, |
| 3246 | "%s: Expected query descriptor opcode but got = 0x%.2x\n", |
| 3247 | __func__, opcode); |
| 3248 | err = -EINVAL; |
| 3249 | goto out_unlock; |
| 3250 | } |
| 3251 | |
| 3252 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT); |
| 3253 | |
| 3254 | if (err) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3255 | dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n", |
| 3256 | __func__, opcode, |
| 3257 | request->upiu_req.idn, index, err); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3258 | goto out_unlock; |
| 3259 | } |
| 3260 | |
| 3261 | hba->dev_cmd.query.descriptor = NULL; |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 3262 | *buf_len = be16_to_cpu(response->upiu_res.length); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3263 | |
| 3264 | out_unlock: |
| 3265 | mutex_unlock(&hba->dev_cmd.lock); |
| 3266 | out: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3267 | ufshcd_release_all(hba); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3268 | return err; |
| 3269 | } |
| 3270 | |
| 3271 | /** |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3272 | * ufshcd_query_descriptor - API function for sending descriptor requests |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3273 | * hba: per-adapter instance |
| 3274 | * opcode: attribute opcode |
| 3275 | * idn: attribute idn to access |
| 3276 | * index: index field |
| 3277 | * selector: selector field |
| 3278 | * desc_buf: the buffer that contains the descriptor |
| 3279 | * buf_len: length parameter passed to the device |
| 3280 | * |
| 3281 | * Returns 0 for success, non-zero in case of failure. |
| 3282 | * The buf_len parameter will contain, on return, the length parameter |
| 3283 | * received on the response. |
| 3284 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3285 | int ufshcd_query_descriptor(struct ufs_hba *hba, |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3286 | enum query_opcode opcode, enum desc_idn idn, u8 index, |
| 3287 | u8 selector, u8 *desc_buf, int *buf_len) |
| 3288 | { |
| 3289 | int err; |
| 3290 | int retries; |
| 3291 | |
| 3292 | for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { |
| 3293 | err = __ufshcd_query_descriptor(hba, opcode, idn, index, |
| 3294 | selector, desc_buf, buf_len); |
| 3295 | if (!err || err == -EINVAL) |
| 3296 | break; |
| 3297 | } |
| 3298 | |
| 3299 | return err; |
| 3300 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3301 | EXPORT_SYMBOL(ufshcd_query_descriptor); |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3302 | |
| 3303 | /** |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3304 | * ufshcd_read_desc_param - read the specified descriptor parameter |
| 3305 | * @hba: Pointer to adapter instance |
| 3306 | * @desc_id: descriptor idn value |
| 3307 | * @desc_index: descriptor index |
| 3308 | * @param_offset: offset of the parameter to read |
| 3309 | * @param_read_buf: pointer to buffer where parameter would be read |
| 3310 | * @param_size: sizeof(param_read_buf) |
| 3311 | * |
| 3312 | * Return 0 in case of success, non-zero otherwise |
| 3313 | */ |
| 3314 | static int ufshcd_read_desc_param(struct ufs_hba *hba, |
| 3315 | enum desc_idn desc_id, |
| 3316 | int desc_index, |
| 3317 | u32 param_offset, |
| 3318 | u8 *param_read_buf, |
| 3319 | u32 param_size) |
| 3320 | { |
| 3321 | int ret; |
| 3322 | u8 *desc_buf; |
| 3323 | u32 buff_len; |
| 3324 | bool is_kmalloc = true; |
| 3325 | |
| 3326 | /* safety checks */ |
| 3327 | if (desc_id >= QUERY_DESC_IDN_MAX) |
| 3328 | return -EINVAL; |
| 3329 | |
| 3330 | buff_len = ufs_query_desc_max_size[desc_id]; |
| 3331 | if ((param_offset + param_size) > buff_len) |
| 3332 | return -EINVAL; |
| 3333 | |
| 3334 | if (!param_offset && (param_size == buff_len)) { |
| 3335 | /* memory space already available to hold full descriptor */ |
| 3336 | desc_buf = param_read_buf; |
| 3337 | is_kmalloc = false; |
| 3338 | } else { |
| 3339 | /* allocate memory to hold full descriptor */ |
| 3340 | desc_buf = kmalloc(buff_len, GFP_KERNEL); |
| 3341 | if (!desc_buf) |
| 3342 | return -ENOMEM; |
| 3343 | } |
| 3344 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3345 | ret = ufshcd_query_descriptor(hba, UPIU_QUERY_OPCODE_READ_DESC, |
| 3346 | desc_id, desc_index, 0, desc_buf, |
| 3347 | &buff_len); |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3348 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3349 | if (ret) { |
| 3350 | dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d", |
| 3351 | __func__, desc_id, desc_index, param_offset, ret); |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3352 | |
| 3353 | goto out; |
| 3354 | } |
| 3355 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3356 | /* Sanity check */ |
| 3357 | if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) { |
| 3358 | dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header", |
| 3359 | __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]); |
| 3360 | ret = -EINVAL; |
| 3361 | goto out; |
| 3362 | } |
| 3363 | |
| 3364 | /* |
| 3365 | * While reading variable size descriptors (like string descriptor), |
| 3366 | * some UFS devices may report the "LENGTH" (field in "Transaction |
| 3367 | * Specific fields" of Query Response UPIU) same as what was requested |
| 3368 | * in Query Request UPIU instead of reporting the actual size of the |
| 3369 | * variable size descriptor. |
| 3370 | * Although it's safe to ignore the "LENGTH" field for variable size |
| 3371 | * descriptors as we can always derive the length of the descriptor from |
| 3372 | * the descriptor header fields. Hence this change impose the length |
| 3373 | * match check only for fixed size descriptors (for which we always |
| 3374 | * request the correct size as part of Query Request UPIU). |
| 3375 | */ |
| 3376 | if ((desc_id != QUERY_DESC_IDN_STRING) && |
| 3377 | (buff_len != desc_buf[QUERY_DESC_LENGTH_OFFSET])) { |
| 3378 | dev_err(hba->dev, "%s: desc_buf length mismatch: buff_len %d, buff_len(desc_header) %d", |
| 3379 | __func__, buff_len, desc_buf[QUERY_DESC_LENGTH_OFFSET]); |
| 3380 | ret = -EINVAL; |
| 3381 | goto out; |
| 3382 | } |
| 3383 | |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3384 | if (is_kmalloc) |
| 3385 | memcpy(param_read_buf, &desc_buf[param_offset], param_size); |
| 3386 | out: |
| 3387 | if (is_kmalloc) |
| 3388 | kfree(desc_buf); |
| 3389 | return ret; |
| 3390 | } |
| 3391 | |
| 3392 | static inline int ufshcd_read_desc(struct ufs_hba *hba, |
| 3393 | enum desc_idn desc_id, |
| 3394 | int desc_index, |
| 3395 | u8 *buf, |
| 3396 | u32 size) |
| 3397 | { |
| 3398 | return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size); |
| 3399 | } |
| 3400 | |
| 3401 | static inline int ufshcd_read_power_desc(struct ufs_hba *hba, |
| 3402 | u8 *buf, |
| 3403 | u32 size) |
| 3404 | { |
| 3405 | return ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size); |
| 3406 | } |
| 3407 | |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3408 | int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size) |
| 3409 | { |
| 3410 | return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size); |
| 3411 | } |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3412 | |
| 3413 | /** |
| 3414 | * ufshcd_read_string_desc - read string descriptor |
| 3415 | * @hba: pointer to adapter instance |
| 3416 | * @desc_index: descriptor index |
| 3417 | * @buf: pointer to buffer where descriptor would be read |
| 3418 | * @size: size of buf |
| 3419 | * @ascii: if true convert from unicode to ascii characters |
| 3420 | * |
| 3421 | * Return 0 in case of success, non-zero otherwise |
| 3422 | */ |
| 3423 | int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index, u8 *buf, |
| 3424 | u32 size, bool ascii) |
| 3425 | { |
| 3426 | int err = 0; |
| 3427 | |
| 3428 | err = ufshcd_read_desc(hba, |
| 3429 | QUERY_DESC_IDN_STRING, desc_index, buf, size); |
| 3430 | |
| 3431 | if (err) { |
| 3432 | dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n", |
| 3433 | __func__, QUERY_REQ_RETRIES, err); |
| 3434 | goto out; |
| 3435 | } |
| 3436 | |
| 3437 | if (ascii) { |
| 3438 | int desc_len; |
| 3439 | int ascii_len; |
| 3440 | int i; |
| 3441 | char *buff_ascii; |
| 3442 | |
| 3443 | desc_len = buf[0]; |
| 3444 | /* remove header and divide by 2 to move from UTF16 to UTF8 */ |
| 3445 | ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1; |
| 3446 | if (size < ascii_len + QUERY_DESC_HDR_SIZE) { |
| 3447 | dev_err(hba->dev, "%s: buffer allocated size is too small\n", |
| 3448 | __func__); |
| 3449 | err = -ENOMEM; |
| 3450 | goto out; |
| 3451 | } |
| 3452 | |
| 3453 | buff_ascii = kmalloc(ascii_len, GFP_KERNEL); |
| 3454 | if (!buff_ascii) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3455 | dev_err(hba->dev, "%s: Failed allocating %d bytes\n", |
| 3456 | __func__, ascii_len); |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3457 | err = -ENOMEM; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3458 | goto out_free_buff; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3459 | } |
| 3460 | |
| 3461 | /* |
| 3462 | * the descriptor contains string in UTF16 format |
| 3463 | * we need to convert to utf-8 so it can be displayed |
| 3464 | */ |
| 3465 | utf16s_to_utf8s((wchar_t *)&buf[QUERY_DESC_HDR_SIZE], |
| 3466 | desc_len - QUERY_DESC_HDR_SIZE, |
| 3467 | UTF16_BIG_ENDIAN, buff_ascii, ascii_len); |
| 3468 | |
| 3469 | /* replace non-printable or non-ASCII characters with spaces */ |
| 3470 | for (i = 0; i < ascii_len; i++) |
| 3471 | ufshcd_remove_non_printable(&buff_ascii[i]); |
| 3472 | |
| 3473 | memset(buf + QUERY_DESC_HDR_SIZE, 0, |
| 3474 | size - QUERY_DESC_HDR_SIZE); |
| 3475 | memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len); |
| 3476 | buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3477 | out_free_buff: |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3478 | kfree(buff_ascii); |
| 3479 | } |
| 3480 | out: |
| 3481 | return err; |
| 3482 | } |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3483 | |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3484 | /** |
| 3485 | * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter |
| 3486 | * @hba: Pointer to adapter instance |
| 3487 | * @lun: lun id |
| 3488 | * @param_offset: offset of the parameter to read |
| 3489 | * @param_read_buf: pointer to buffer where parameter would be read |
| 3490 | * @param_size: sizeof(param_read_buf) |
| 3491 | * |
| 3492 | * Return 0 in case of success, non-zero otherwise |
| 3493 | */ |
| 3494 | static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba, |
| 3495 | int lun, |
| 3496 | enum unit_desc_param param_offset, |
| 3497 | u8 *param_read_buf, |
| 3498 | u32 param_size) |
| 3499 | { |
| 3500 | /* |
| 3501 | * Unit descriptors are only available for general purpose LUs (LUN id |
| 3502 | * from 0 to 7) and RPMB Well known LU. |
| 3503 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3504 | if (!ufs_is_valid_unit_desc_lun(lun)) |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3505 | return -EOPNOTSUPP; |
| 3506 | |
| 3507 | return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun, |
| 3508 | param_offset, param_read_buf, param_size); |
| 3509 | } |
| 3510 | |
| 3511 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3512 | * ufshcd_memory_alloc - allocate memory for host memory space data structures |
| 3513 | * @hba: per adapter instance |
| 3514 | * |
| 3515 | * 1. Allocate DMA memory for Command Descriptor array |
| 3516 | * Each command descriptor consist of Command UPIU, Response UPIU and PRDT |
| 3517 | * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL). |
| 3518 | * 3. Allocate DMA memory for UTP Task Management Request Descriptor List |
| 3519 | * (UTMRDL) |
| 3520 | * 4. Allocate memory for local reference block(lrb). |
| 3521 | * |
| 3522 | * Returns 0 for success, non-zero in case of failure |
| 3523 | */ |
| 3524 | static int ufshcd_memory_alloc(struct ufs_hba *hba) |
| 3525 | { |
| 3526 | size_t utmrdl_size, utrdl_size, ucdl_size; |
| 3527 | |
| 3528 | /* Allocate memory for UTP command descriptors */ |
| 3529 | ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3530 | hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 3531 | ucdl_size, |
| 3532 | &hba->ucdl_dma_addr, |
| 3533 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3534 | |
| 3535 | /* |
| 3536 | * UFSHCI requires UTP command descriptor to be 128 byte aligned. |
| 3537 | * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE |
| 3538 | * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will |
| 3539 | * be aligned to 128 bytes as well |
| 3540 | */ |
| 3541 | if (!hba->ucdl_base_addr || |
| 3542 | WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3543 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3544 | "Command Descriptor Memory allocation failed\n"); |
| 3545 | goto out; |
| 3546 | } |
| 3547 | |
| 3548 | /* |
| 3549 | * Allocate memory for UTP Transfer descriptors |
| 3550 | * UFSHCI requires 1024 byte alignment of UTRD |
| 3551 | */ |
| 3552 | utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3553 | hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 3554 | utrdl_size, |
| 3555 | &hba->utrdl_dma_addr, |
| 3556 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3557 | if (!hba->utrdl_base_addr || |
| 3558 | WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3559 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3560 | "Transfer Descriptor Memory allocation failed\n"); |
| 3561 | goto out; |
| 3562 | } |
| 3563 | |
| 3564 | /* |
| 3565 | * Allocate memory for UTP Task Management descriptors |
| 3566 | * UFSHCI requires 1024 byte alignment of UTMRD |
| 3567 | */ |
| 3568 | utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs; |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3569 | hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 3570 | utmrdl_size, |
| 3571 | &hba->utmrdl_dma_addr, |
| 3572 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3573 | if (!hba->utmrdl_base_addr || |
| 3574 | WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3575 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3576 | "Task Management Descriptor Memory allocation failed\n"); |
| 3577 | goto out; |
| 3578 | } |
| 3579 | |
| 3580 | /* Allocate memory for local reference block */ |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3581 | hba->lrb = devm_kzalloc(hba->dev, |
| 3582 | hba->nutrs * sizeof(struct ufshcd_lrb), |
| 3583 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3584 | if (!hba->lrb) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3585 | dev_err(hba->dev, "LRB Memory allocation failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3586 | goto out; |
| 3587 | } |
| 3588 | return 0; |
| 3589 | out: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3590 | return -ENOMEM; |
| 3591 | } |
| 3592 | |
| 3593 | /** |
| 3594 | * ufshcd_host_memory_configure - configure local reference block with |
| 3595 | * memory offsets |
| 3596 | * @hba: per adapter instance |
| 3597 | * |
| 3598 | * Configure Host memory space |
| 3599 | * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA |
| 3600 | * address. |
| 3601 | * 2. Update each UTRD with Response UPIU offset, Response UPIU length |
| 3602 | * and PRDT offset. |
| 3603 | * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT |
| 3604 | * into local reference block. |
| 3605 | */ |
| 3606 | static void ufshcd_host_memory_configure(struct ufs_hba *hba) |
| 3607 | { |
| 3608 | struct utp_transfer_cmd_desc *cmd_descp; |
| 3609 | struct utp_transfer_req_desc *utrdlp; |
| 3610 | dma_addr_t cmd_desc_dma_addr; |
| 3611 | dma_addr_t cmd_desc_element_addr; |
| 3612 | u16 response_offset; |
| 3613 | u16 prdt_offset; |
| 3614 | int cmd_desc_size; |
| 3615 | int i; |
| 3616 | |
| 3617 | utrdlp = hba->utrdl_base_addr; |
| 3618 | cmd_descp = hba->ucdl_base_addr; |
| 3619 | |
| 3620 | response_offset = |
| 3621 | offsetof(struct utp_transfer_cmd_desc, response_upiu); |
| 3622 | prdt_offset = |
| 3623 | offsetof(struct utp_transfer_cmd_desc, prd_table); |
| 3624 | |
| 3625 | cmd_desc_size = sizeof(struct utp_transfer_cmd_desc); |
| 3626 | cmd_desc_dma_addr = hba->ucdl_dma_addr; |
| 3627 | |
| 3628 | for (i = 0; i < hba->nutrs; i++) { |
| 3629 | /* Configure UTRD with command descriptor base address */ |
| 3630 | cmd_desc_element_addr = |
| 3631 | (cmd_desc_dma_addr + (cmd_desc_size * i)); |
| 3632 | utrdlp[i].command_desc_base_addr_lo = |
| 3633 | cpu_to_le32(lower_32_bits(cmd_desc_element_addr)); |
| 3634 | utrdlp[i].command_desc_base_addr_hi = |
| 3635 | cpu_to_le32(upper_32_bits(cmd_desc_element_addr)); |
| 3636 | |
| 3637 | /* Response upiu and prdt offset should be in double words */ |
| 3638 | utrdlp[i].response_upiu_offset = |
| 3639 | cpu_to_le16((response_offset >> 2)); |
| 3640 | utrdlp[i].prd_table_offset = |
| 3641 | cpu_to_le16((prdt_offset >> 2)); |
| 3642 | utrdlp[i].response_upiu_length = |
Sujit Reddy Thumma | 3ca316c | 2013-06-26 22:39:30 +0530 | [diff] [blame] | 3643 | cpu_to_le16(ALIGNED_UPIU_SIZE >> 2); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3644 | |
| 3645 | hba->lrb[i].utr_descriptor_ptr = (utrdlp + i); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3646 | hba->lrb[i].utrd_dma_addr = hba->utrdl_dma_addr + |
| 3647 | (i * sizeof(struct utp_transfer_req_desc)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3648 | hba->lrb[i].ucd_req_ptr = |
| 3649 | (struct utp_upiu_req *)(cmd_descp + i); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3650 | hba->lrb[i].ucd_req_dma_addr = cmd_desc_element_addr; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3651 | hba->lrb[i].ucd_rsp_ptr = |
| 3652 | (struct utp_upiu_rsp *)cmd_descp[i].response_upiu; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3653 | hba->lrb[i].ucd_rsp_dma_addr = cmd_desc_element_addr + |
| 3654 | response_offset; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3655 | hba->lrb[i].ucd_prdt_ptr = |
| 3656 | (struct ufshcd_sg_entry *)cmd_descp[i].prd_table; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3657 | hba->lrb[i].ucd_prdt_dma_addr = cmd_desc_element_addr + |
| 3658 | prdt_offset; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3659 | } |
| 3660 | } |
| 3661 | |
| 3662 | /** |
| 3663 | * ufshcd_dme_link_startup - Notify Unipro to perform link startup |
| 3664 | * @hba: per adapter instance |
| 3665 | * |
| 3666 | * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer, |
| 3667 | * in order to initialize the Unipro link startup procedure. |
| 3668 | * Once the Unipro links are up, the device connected to the controller |
| 3669 | * is detected. |
| 3670 | * |
| 3671 | * Returns 0 on success, non-zero value on failure |
| 3672 | */ |
| 3673 | static int ufshcd_dme_link_startup(struct ufs_hba *hba) |
| 3674 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3675 | struct uic_command uic_cmd = {0}; |
| 3676 | int ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3677 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3678 | uic_cmd.command = UIC_CMD_DME_LINK_STARTUP; |
| 3679 | |
| 3680 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3681 | if (ret) |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3682 | dev_dbg(hba->dev, |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3683 | "dme-link-startup: error code %d\n", ret); |
| 3684 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3685 | } |
| 3686 | |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 3687 | static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba) |
| 3688 | { |
| 3689 | #define MIN_DELAY_BEFORE_DME_CMDS_US 1000 |
| 3690 | unsigned long min_sleep_time_us; |
| 3691 | |
| 3692 | if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS)) |
| 3693 | return; |
| 3694 | |
| 3695 | /* |
| 3696 | * last_dme_cmd_tstamp will be 0 only for 1st call to |
| 3697 | * this function |
| 3698 | */ |
| 3699 | if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) { |
| 3700 | min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US; |
| 3701 | } else { |
| 3702 | unsigned long delta = |
| 3703 | (unsigned long) ktime_to_us( |
| 3704 | ktime_sub(ktime_get(), |
| 3705 | hba->last_dme_cmd_tstamp)); |
| 3706 | |
| 3707 | if (delta < MIN_DELAY_BEFORE_DME_CMDS_US) |
| 3708 | min_sleep_time_us = |
| 3709 | MIN_DELAY_BEFORE_DME_CMDS_US - delta; |
| 3710 | else |
| 3711 | return; /* no more delay required */ |
| 3712 | } |
| 3713 | |
| 3714 | /* allow sleep for extra 50us if needed */ |
| 3715 | usleep_range(min_sleep_time_us, min_sleep_time_us + 50); |
| 3716 | } |
| 3717 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3718 | static inline void ufshcd_save_tstamp_of_last_dme_cmd( |
| 3719 | struct ufs_hba *hba) |
| 3720 | { |
| 3721 | if (hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS) |
| 3722 | hba->last_dme_cmd_tstamp = ktime_get(); |
| 3723 | } |
| 3724 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3725 | /** |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3726 | * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET |
| 3727 | * @hba: per adapter instance |
| 3728 | * @attr_sel: uic command argument1 |
| 3729 | * @attr_set: attribute set type as uic command argument2 |
| 3730 | * @mib_val: setting value as uic command argument3 |
| 3731 | * @peer: indicate whether peer or local |
| 3732 | * |
| 3733 | * Returns 0 on success, non-zero value on failure |
| 3734 | */ |
| 3735 | int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel, |
| 3736 | u8 attr_set, u32 mib_val, u8 peer) |
| 3737 | { |
| 3738 | struct uic_command uic_cmd = {0}; |
| 3739 | static const char *const action[] = { |
| 3740 | "dme-set", |
| 3741 | "dme-peer-set" |
| 3742 | }; |
| 3743 | const char *set = action[!!peer]; |
| 3744 | int ret; |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3745 | int retries = UFS_UIC_COMMAND_RETRIES; |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3746 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3747 | ufsdbg_error_inject_dispatcher(hba, |
| 3748 | ERR_INJECT_DME_ATTR, attr_sel, &attr_sel); |
| 3749 | |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3750 | uic_cmd.command = peer ? |
| 3751 | UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET; |
| 3752 | uic_cmd.argument1 = attr_sel; |
| 3753 | uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set); |
| 3754 | uic_cmd.argument3 = mib_val; |
| 3755 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3756 | do { |
| 3757 | /* for peer attributes we retry upon failure */ |
| 3758 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3759 | if (ret) |
| 3760 | dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n", |
| 3761 | set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret); |
| 3762 | } while (ret && peer && --retries); |
| 3763 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3764 | if (ret) |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3765 | dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n", |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3766 | set, UIC_GET_ATTR_ID(attr_sel), mib_val, |
| 3767 | UFS_UIC_COMMAND_RETRIES - retries); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3768 | |
| 3769 | return ret; |
| 3770 | } |
| 3771 | EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr); |
| 3772 | |
| 3773 | /** |
| 3774 | * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET |
| 3775 | * @hba: per adapter instance |
| 3776 | * @attr_sel: uic command argument1 |
| 3777 | * @mib_val: the value of the attribute as returned by the UIC command |
| 3778 | * @peer: indicate whether peer or local |
| 3779 | * |
| 3780 | * Returns 0 on success, non-zero value on failure |
| 3781 | */ |
| 3782 | int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel, |
| 3783 | u32 *mib_val, u8 peer) |
| 3784 | { |
| 3785 | struct uic_command uic_cmd = {0}; |
| 3786 | static const char *const action[] = { |
| 3787 | "dme-get", |
| 3788 | "dme-peer-get" |
| 3789 | }; |
| 3790 | const char *get = action[!!peer]; |
| 3791 | int ret; |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3792 | int retries = UFS_UIC_COMMAND_RETRIES; |
Yaniv Gardi | 874237f | 2015-05-17 18:55:03 +0300 | [diff] [blame] | 3793 | struct ufs_pa_layer_attr orig_pwr_info; |
| 3794 | struct ufs_pa_layer_attr temp_pwr_info; |
| 3795 | bool pwr_mode_change = false; |
| 3796 | |
| 3797 | if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) { |
| 3798 | orig_pwr_info = hba->pwr_info; |
| 3799 | temp_pwr_info = orig_pwr_info; |
| 3800 | |
| 3801 | if (orig_pwr_info.pwr_tx == FAST_MODE || |
| 3802 | orig_pwr_info.pwr_rx == FAST_MODE) { |
| 3803 | temp_pwr_info.pwr_tx = FASTAUTO_MODE; |
| 3804 | temp_pwr_info.pwr_rx = FASTAUTO_MODE; |
| 3805 | pwr_mode_change = true; |
| 3806 | } else if (orig_pwr_info.pwr_tx == SLOW_MODE || |
| 3807 | orig_pwr_info.pwr_rx == SLOW_MODE) { |
| 3808 | temp_pwr_info.pwr_tx = SLOWAUTO_MODE; |
| 3809 | temp_pwr_info.pwr_rx = SLOWAUTO_MODE; |
| 3810 | pwr_mode_change = true; |
| 3811 | } |
| 3812 | if (pwr_mode_change) { |
| 3813 | ret = ufshcd_change_power_mode(hba, &temp_pwr_info); |
| 3814 | if (ret) |
| 3815 | goto out; |
| 3816 | } |
| 3817 | } |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3818 | |
| 3819 | uic_cmd.command = peer ? |
| 3820 | UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3821 | |
| 3822 | ufsdbg_error_inject_dispatcher(hba, |
| 3823 | ERR_INJECT_DME_ATTR, attr_sel, &attr_sel); |
| 3824 | |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3825 | uic_cmd.argument1 = attr_sel; |
| 3826 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3827 | do { |
| 3828 | /* for peer attributes we retry upon failure */ |
| 3829 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3830 | if (ret) |
| 3831 | dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n", |
| 3832 | get, UIC_GET_ATTR_ID(attr_sel), ret); |
| 3833 | } while (ret && peer && --retries); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3834 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3835 | if (ret) |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3836 | dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n", |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3837 | get, UIC_GET_ATTR_ID(attr_sel), |
| 3838 | UFS_UIC_COMMAND_RETRIES - retries); |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3839 | |
| 3840 | if (mib_val && !ret) |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3841 | *mib_val = uic_cmd.argument3; |
Yaniv Gardi | 874237f | 2015-05-17 18:55:03 +0300 | [diff] [blame] | 3842 | |
| 3843 | if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE) |
| 3844 | && pwr_mode_change) |
| 3845 | ufshcd_change_power_mode(hba, &orig_pwr_info); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3846 | out: |
| 3847 | return ret; |
| 3848 | } |
| 3849 | EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr); |
| 3850 | |
| 3851 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3852 | * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power |
| 3853 | * state) and waits for it to take effect. |
| 3854 | * |
| 3855 | * @hba: per adapter instance |
| 3856 | * @cmd: UIC command to execute |
| 3857 | * |
| 3858 | * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER & |
| 3859 | * DME_HIBERNATE_EXIT commands take some time to take its effect on both host |
| 3860 | * and device UniPro link and hence it's final completion would be indicated by |
| 3861 | * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in |
| 3862 | * addition to normal UIC command completion Status (UCCS). This function only |
| 3863 | * returns after the relevant status bits indicate the completion. |
| 3864 | * |
| 3865 | * Returns 0 on success, non-zero value on failure |
| 3866 | */ |
| 3867 | static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd) |
| 3868 | { |
| 3869 | struct completion uic_async_done; |
| 3870 | unsigned long flags; |
| 3871 | u8 status; |
| 3872 | int ret; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3873 | bool reenable_intr = false; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3874 | |
| 3875 | mutex_lock(&hba->uic_cmd_mutex); |
| 3876 | init_completion(&uic_async_done); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 3877 | ufshcd_add_delay_before_dme_cmd(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3878 | |
| 3879 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 3880 | hba->uic_async_done = &uic_async_done; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3881 | if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) { |
| 3882 | ufshcd_disable_intr(hba, UIC_COMMAND_COMPL); |
| 3883 | /* |
| 3884 | * Make sure UIC command completion interrupt is disabled before |
| 3885 | * issuing UIC command. |
| 3886 | */ |
| 3887 | wmb(); |
| 3888 | reenable_intr = true; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3889 | } |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3890 | ret = __ufshcd_send_uic_cmd(hba, cmd, false); |
| 3891 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3892 | if (ret) { |
| 3893 | dev_err(hba->dev, |
| 3894 | "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n", |
| 3895 | cmd->command, cmd->argument3, ret); |
| 3896 | goto out; |
| 3897 | } |
| 3898 | |
| 3899 | if (!wait_for_completion_timeout(hba->uic_async_done, |
| 3900 | msecs_to_jiffies(UIC_CMD_TIMEOUT))) { |
| 3901 | dev_err(hba->dev, |
| 3902 | "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n", |
| 3903 | cmd->command, cmd->argument3); |
| 3904 | ret = -ETIMEDOUT; |
| 3905 | goto out; |
| 3906 | } |
| 3907 | |
| 3908 | status = ufshcd_get_upmcrs(hba); |
| 3909 | if (status != PWR_LOCAL) { |
| 3910 | dev_err(hba->dev, |
Kiwoong Kim | 7361542 | 2016-09-08 16:50:02 +0900 | [diff] [blame] | 3911 | "pwr ctrl cmd 0x%0x failed, host upmcrs:0x%x\n", |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3912 | cmd->command, status); |
| 3913 | ret = (status != PWR_OK) ? status : -1; |
| 3914 | } |
| 3915 | out: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3916 | if (ret) |
| 3917 | ufsdbg_set_err_state(hba); |
| 3918 | |
| 3919 | ufshcd_save_tstamp_of_last_dme_cmd(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3920 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3921 | hba->active_uic_cmd = NULL; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3922 | hba->uic_async_done = NULL; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3923 | if (reenable_intr) |
| 3924 | ufshcd_enable_intr(hba, UIC_COMMAND_COMPL); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3925 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3926 | mutex_unlock(&hba->uic_cmd_mutex); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3927 | return ret; |
| 3928 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3929 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3930 | int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba, u64 wait_timeout_us) |
| 3931 | { |
| 3932 | unsigned long flags; |
| 3933 | int ret = 0; |
| 3934 | u32 tm_doorbell; |
| 3935 | u32 tr_doorbell; |
| 3936 | bool timeout = false, do_last_check = false; |
| 3937 | ktime_t start; |
| 3938 | |
| 3939 | ufshcd_hold_all(hba); |
| 3940 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 3941 | if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) { |
| 3942 | ret = -EBUSY; |
| 3943 | goto out; |
| 3944 | } |
| 3945 | |
| 3946 | /* |
| 3947 | * Wait for all the outstanding tasks/transfer requests. |
| 3948 | * Verify by checking the doorbell registers are clear. |
| 3949 | */ |
| 3950 | start = ktime_get(); |
| 3951 | do { |
| 3952 | tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL); |
| 3953 | tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 3954 | if (!tm_doorbell && !tr_doorbell) { |
| 3955 | timeout = false; |
| 3956 | break; |
| 3957 | } else if (do_last_check) { |
| 3958 | break; |
| 3959 | } |
| 3960 | |
| 3961 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3962 | schedule(); |
| 3963 | if (ktime_to_us(ktime_sub(ktime_get(), start)) > |
| 3964 | wait_timeout_us) { |
| 3965 | timeout = true; |
| 3966 | /* |
| 3967 | * We might have scheduled out for long time so make |
| 3968 | * sure to check if doorbells are cleared by this time |
| 3969 | * or not. |
| 3970 | */ |
| 3971 | do_last_check = true; |
| 3972 | } |
| 3973 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 3974 | } while (tm_doorbell || tr_doorbell); |
| 3975 | |
| 3976 | if (timeout) { |
| 3977 | dev_err(hba->dev, |
| 3978 | "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n", |
| 3979 | __func__, tm_doorbell, tr_doorbell); |
| 3980 | ret = -EBUSY; |
| 3981 | } |
| 3982 | out: |
| 3983 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3984 | ufshcd_release_all(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3985 | return ret; |
| 3986 | } |
| 3987 | |
| 3988 | /** |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3989 | * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage |
| 3990 | * using DME_SET primitives. |
| 3991 | * @hba: per adapter instance |
| 3992 | * @mode: powr mode value |
| 3993 | * |
| 3994 | * Returns 0 on success, non-zero value on failure |
| 3995 | */ |
Sujit Reddy Thumma | bdbe5d2 | 2014-05-26 10:59:11 +0530 | [diff] [blame] | 3996 | 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] | 3997 | { |
| 3998 | struct uic_command uic_cmd = {0}; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3999 | int ret; |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4000 | |
Yaniv Gardi | c3a2f9e | 2015-05-17 18:55:01 +0300 | [diff] [blame] | 4001 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) { |
| 4002 | ret = ufshcd_dme_set(hba, |
| 4003 | UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1); |
| 4004 | if (ret) { |
| 4005 | dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n", |
| 4006 | __func__, ret); |
| 4007 | goto out; |
| 4008 | } |
| 4009 | } |
| 4010 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4011 | uic_cmd.command = UIC_CMD_DME_SET; |
| 4012 | uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE); |
| 4013 | uic_cmd.argument3 = mode; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4014 | ufshcd_hold_all(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4015 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4016 | ufshcd_release_all(hba); |
Yaniv Gardi | c3a2f9e | 2015-05-17 18:55:01 +0300 | [diff] [blame] | 4017 | out: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4018 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4019 | } |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4020 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 4021 | static int ufshcd_link_recovery(struct ufs_hba *hba) |
| 4022 | { |
| 4023 | int ret; |
| 4024 | unsigned long flags; |
| 4025 | |
| 4026 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4027 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 4028 | ufshcd_set_eh_in_progress(hba); |
| 4029 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4030 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4031 | ret = ufshcd_vops_full_reset(hba); |
| 4032 | if (ret) |
| 4033 | dev_warn(hba->dev, |
| 4034 | "full reset returned %d, trying to recover the link\n", |
| 4035 | ret); |
| 4036 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 4037 | ret = ufshcd_host_reset_and_restore(hba); |
| 4038 | |
| 4039 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4040 | if (ret) |
| 4041 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 4042 | ufshcd_clear_eh_in_progress(hba); |
| 4043 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4044 | |
| 4045 | if (ret) |
| 4046 | dev_err(hba->dev, "%s: link recovery failed, err %d", |
| 4047 | __func__, ret); |
| 4048 | |
| 4049 | return ret; |
| 4050 | } |
| 4051 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4052 | static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4053 | { |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4054 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4055 | struct uic_command uic_cmd = {0}; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4056 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4057 | |
| 4058 | uic_cmd.command = UIC_CMD_DME_HIBER_ENTER; |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4059 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4060 | trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter", |
| 4061 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4062 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 4063 | if (ret) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4064 | ufshcd_update_error_stats(hba, UFS_ERR_HIBERN8_ENTER); |
| 4065 | dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d", |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4066 | __func__, ret); |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 4067 | /* |
| 4068 | * If link recovery fails then return error so that caller |
| 4069 | * don't retry the hibern8 enter again. |
| 4070 | */ |
| 4071 | if (ufshcd_link_recovery(hba)) |
| 4072 | ret = -ENOLINK; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4073 | } else { |
| 4074 | dev_dbg(hba->dev, "%s: Hibern8 Enter at %lld us", __func__, |
| 4075 | ktime_to_us(ktime_get())); |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 4076 | } |
| 4077 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4078 | return ret; |
| 4079 | } |
| 4080 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4081 | int ufshcd_uic_hibern8_enter(struct ufs_hba *hba) |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4082 | { |
| 4083 | int ret = 0, retries; |
| 4084 | |
| 4085 | for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) { |
| 4086 | ret = __ufshcd_uic_hibern8_enter(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4087 | if (!ret) |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4088 | goto out; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4089 | /* Unable to recover the link, so no point proceeding */ |
| 4090 | if (ret == -ENOLINK) |
| 4091 | BUG(); |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4092 | } |
| 4093 | out: |
| 4094 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4095 | } |
| 4096 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4097 | int ufshcd_uic_hibern8_exit(struct ufs_hba *hba) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4098 | { |
| 4099 | struct uic_command uic_cmd = {0}; |
| 4100 | int ret; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4101 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4102 | |
| 4103 | uic_cmd.command = UIC_CMD_DME_HIBER_EXIT; |
| 4104 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4105 | trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit", |
| 4106 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
| 4107 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4108 | if (ret) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4109 | ufshcd_update_error_stats(hba, UFS_ERR_HIBERN8_EXIT); |
| 4110 | dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d", |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 4111 | __func__, ret); |
| 4112 | ret = ufshcd_link_recovery(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4113 | /* Unable to recover the link, so no point proceeding */ |
| 4114 | if (ret) |
| 4115 | BUG(); |
| 4116 | } else { |
| 4117 | dev_dbg(hba->dev, "%s: Hibern8 Exit at %lld us", __func__, |
| 4118 | ktime_to_us(ktime_get())); |
| 4119 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_get(); |
| 4120 | hba->ufs_stats.hibern8_exit_cnt++; |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4121 | } |
| 4122 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4123 | return ret; |
| 4124 | } |
| 4125 | |
Yaniv Gardi | 5064636 | 2014-10-23 13:25:13 +0300 | [diff] [blame] | 4126 | /** |
| 4127 | * ufshcd_init_pwr_info - setting the POR (power on reset) |
| 4128 | * values in hba power info |
| 4129 | * @hba: per-adapter instance |
| 4130 | */ |
| 4131 | static void ufshcd_init_pwr_info(struct ufs_hba *hba) |
| 4132 | { |
| 4133 | hba->pwr_info.gear_rx = UFS_PWM_G1; |
| 4134 | hba->pwr_info.gear_tx = UFS_PWM_G1; |
| 4135 | hba->pwr_info.lane_rx = 1; |
| 4136 | hba->pwr_info.lane_tx = 1; |
| 4137 | hba->pwr_info.pwr_rx = SLOWAUTO_MODE; |
| 4138 | hba->pwr_info.pwr_tx = SLOWAUTO_MODE; |
| 4139 | hba->pwr_info.hs_rate = 0; |
| 4140 | } |
| 4141 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4142 | /** |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4143 | * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device |
| 4144 | * @hba: per-adapter instance |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4145 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4146 | static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4147 | { |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4148 | struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info; |
| 4149 | |
| 4150 | if (hba->max_pwr_info.is_valid) |
| 4151 | return 0; |
| 4152 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4153 | pwr_info->pwr_tx = FAST_MODE; |
| 4154 | pwr_info->pwr_rx = FAST_MODE; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4155 | pwr_info->hs_rate = PA_HS_MODE_B; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4156 | |
| 4157 | /* Get the connected lane count */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4158 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES), |
| 4159 | &pwr_info->lane_rx); |
| 4160 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 4161 | &pwr_info->lane_tx); |
| 4162 | |
| 4163 | if (!pwr_info->lane_rx || !pwr_info->lane_tx) { |
| 4164 | dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n", |
| 4165 | __func__, |
| 4166 | pwr_info->lane_rx, |
| 4167 | pwr_info->lane_tx); |
| 4168 | return -EINVAL; |
| 4169 | } |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4170 | |
| 4171 | /* |
| 4172 | * First, get the maximum gears of HS speed. |
| 4173 | * If a zero value, it means there is no HSGEAR capability. |
| 4174 | * Then, get the maximum gears of PWM speed. |
| 4175 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4176 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx); |
| 4177 | if (!pwr_info->gear_rx) { |
| 4178 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), |
| 4179 | &pwr_info->gear_rx); |
| 4180 | if (!pwr_info->gear_rx) { |
| 4181 | dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n", |
| 4182 | __func__, pwr_info->gear_rx); |
| 4183 | return -EINVAL; |
Subhash Jadavani | 5e45e70 | 2016-08-09 18:43:10 -0700 | [diff] [blame] | 4184 | } else { |
| 4185 | if (hba->limit_rx_pwm_gear > 0 && |
| 4186 | (hba->limit_rx_pwm_gear < pwr_info->gear_rx)) |
| 4187 | pwr_info->gear_rx = hba->limit_rx_pwm_gear; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4188 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4189 | pwr_info->pwr_rx = SLOW_MODE; |
Subhash Jadavani | 5e45e70 | 2016-08-09 18:43:10 -0700 | [diff] [blame] | 4190 | } else { |
| 4191 | if (hba->limit_rx_hs_gear > 0 && |
| 4192 | (hba->limit_rx_hs_gear < pwr_info->gear_rx)) |
| 4193 | pwr_info->gear_rx = hba->limit_rx_hs_gear; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4194 | } |
| 4195 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4196 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), |
| 4197 | &pwr_info->gear_tx); |
| 4198 | if (!pwr_info->gear_tx) { |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4199 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4200 | &pwr_info->gear_tx); |
| 4201 | if (!pwr_info->gear_tx) { |
| 4202 | dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n", |
| 4203 | __func__, pwr_info->gear_tx); |
| 4204 | return -EINVAL; |
Subhash Jadavani | 5e45e70 | 2016-08-09 18:43:10 -0700 | [diff] [blame] | 4205 | } else { |
| 4206 | if (hba->limit_tx_pwm_gear > 0 && |
| 4207 | (hba->limit_tx_pwm_gear < pwr_info->gear_tx)) |
| 4208 | pwr_info->gear_tx = hba->limit_tx_pwm_gear; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4209 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4210 | pwr_info->pwr_tx = SLOW_MODE; |
Subhash Jadavani | 5e45e70 | 2016-08-09 18:43:10 -0700 | [diff] [blame] | 4211 | } else { |
| 4212 | if (hba->limit_tx_hs_gear > 0 && |
| 4213 | (hba->limit_tx_hs_gear < pwr_info->gear_tx)) |
| 4214 | pwr_info->gear_tx = hba->limit_tx_hs_gear; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4215 | } |
| 4216 | |
| 4217 | hba->max_pwr_info.is_valid = true; |
| 4218 | return 0; |
| 4219 | } |
| 4220 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4221 | int ufshcd_change_power_mode(struct ufs_hba *hba, |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4222 | struct ufs_pa_layer_attr *pwr_mode) |
| 4223 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4224 | int ret = 0; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4225 | |
| 4226 | /* if already configured to the requested pwr_mode */ |
| 4227 | if (pwr_mode->gear_rx == hba->pwr_info.gear_rx && |
| 4228 | pwr_mode->gear_tx == hba->pwr_info.gear_tx && |
| 4229 | pwr_mode->lane_rx == hba->pwr_info.lane_rx && |
| 4230 | pwr_mode->lane_tx == hba->pwr_info.lane_tx && |
| 4231 | pwr_mode->pwr_rx == hba->pwr_info.pwr_rx && |
| 4232 | pwr_mode->pwr_tx == hba->pwr_info.pwr_tx && |
| 4233 | pwr_mode->hs_rate == hba->pwr_info.hs_rate) { |
| 4234 | dev_dbg(hba->dev, "%s: power already configured\n", __func__); |
| 4235 | return 0; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4236 | } |
| 4237 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4238 | ufsdbg_error_inject_dispatcher(hba, ERR_INJECT_PWR_CHANGE, 0, &ret); |
| 4239 | if (ret) |
| 4240 | return ret; |
| 4241 | |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4242 | /* |
| 4243 | * Configure attributes for power mode change with below. |
| 4244 | * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION, |
| 4245 | * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION, |
| 4246 | * - PA_HSSERIES |
| 4247 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4248 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx); |
| 4249 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES), |
| 4250 | pwr_mode->lane_rx); |
| 4251 | if (pwr_mode->pwr_rx == FASTAUTO_MODE || |
| 4252 | pwr_mode->pwr_rx == FAST_MODE) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4253 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4254 | else |
| 4255 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4256 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4257 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx); |
| 4258 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES), |
| 4259 | pwr_mode->lane_tx); |
| 4260 | if (pwr_mode->pwr_tx == FASTAUTO_MODE || |
| 4261 | pwr_mode->pwr_tx == FAST_MODE) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4262 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4263 | else |
| 4264 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4265 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4266 | if (pwr_mode->pwr_rx == FASTAUTO_MODE || |
| 4267 | pwr_mode->pwr_tx == FASTAUTO_MODE || |
| 4268 | pwr_mode->pwr_rx == FAST_MODE || |
| 4269 | pwr_mode->pwr_tx == FAST_MODE) |
| 4270 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES), |
| 4271 | pwr_mode->hs_rate); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4272 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4273 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0), |
| 4274 | DL_FC0ProtectionTimeOutVal_Default); |
| 4275 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1), |
| 4276 | DL_TC0ReplayTimeOutVal_Default); |
| 4277 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2), |
| 4278 | DL_AFC0ReqTimeOutVal_Default); |
| 4279 | |
| 4280 | ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalFC0ProtectionTimeOutVal), |
| 4281 | DL_FC0ProtectionTimeOutVal_Default); |
| 4282 | ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalTC0ReplayTimeOutVal), |
| 4283 | DL_TC0ReplayTimeOutVal_Default); |
| 4284 | ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalAFC0ReqTimeOutVal), |
| 4285 | DL_AFC0ReqTimeOutVal_Default); |
| 4286 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4287 | ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4 |
| 4288 | | pwr_mode->pwr_tx); |
| 4289 | |
| 4290 | if (ret) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4291 | ufshcd_update_error_stats(hba, UFS_ERR_POWER_MODE_CHANGE); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4292 | dev_err(hba->dev, |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4293 | "%s: power mode change failed %d\n", __func__, ret); |
| 4294 | } else { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4295 | ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL, |
| 4296 | pwr_mode); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4297 | |
| 4298 | memcpy(&hba->pwr_info, pwr_mode, |
| 4299 | sizeof(struct ufs_pa_layer_attr)); |
| 4300 | } |
| 4301 | |
| 4302 | return ret; |
| 4303 | } |
| 4304 | |
| 4305 | /** |
| 4306 | * ufshcd_config_pwr_mode - configure a new power mode |
| 4307 | * @hba: per-adapter instance |
| 4308 | * @desired_pwr_mode: desired power configuration |
| 4309 | */ |
| 4310 | static int ufshcd_config_pwr_mode(struct ufs_hba *hba, |
| 4311 | struct ufs_pa_layer_attr *desired_pwr_mode) |
| 4312 | { |
| 4313 | struct ufs_pa_layer_attr final_params = { 0 }; |
| 4314 | int ret; |
| 4315 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4316 | ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE, |
| 4317 | desired_pwr_mode, &final_params); |
| 4318 | |
| 4319 | if (ret) |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4320 | memcpy(&final_params, desired_pwr_mode, sizeof(final_params)); |
| 4321 | |
| 4322 | ret = ufshcd_change_power_mode(hba, &final_params); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4323 | if (!ret) |
| 4324 | ufshcd_print_pwr_info(hba); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4325 | |
| 4326 | return ret; |
| 4327 | } |
| 4328 | |
| 4329 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4330 | * ufshcd_complete_dev_init() - checks device readiness |
| 4331 | * hba: per-adapter instance |
| 4332 | * |
| 4333 | * Set fDeviceInit flag and poll until device toggles it. |
| 4334 | */ |
| 4335 | static int ufshcd_complete_dev_init(struct ufs_hba *hba) |
| 4336 | { |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 4337 | int i; |
| 4338 | int err; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4339 | bool flag_res = 1; |
| 4340 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 4341 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG, |
| 4342 | QUERY_FLAG_IDN_FDEVICEINIT, NULL); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4343 | if (err) { |
| 4344 | dev_err(hba->dev, |
| 4345 | "%s setting fDeviceInit flag failed with error %d\n", |
| 4346 | __func__, err); |
| 4347 | goto out; |
| 4348 | } |
| 4349 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 4350 | /* poll for max. 1000 iterations for fDeviceInit flag to clear */ |
| 4351 | for (i = 0; i < 1000 && !err && flag_res; i++) |
| 4352 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG, |
| 4353 | QUERY_FLAG_IDN_FDEVICEINIT, &flag_res); |
| 4354 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4355 | if (err) |
| 4356 | dev_err(hba->dev, |
| 4357 | "%s reading fDeviceInit flag failed with error %d\n", |
| 4358 | __func__, err); |
| 4359 | else if (flag_res) |
| 4360 | dev_err(hba->dev, |
| 4361 | "%s fDeviceInit was not cleared by the device\n", |
| 4362 | __func__); |
| 4363 | |
| 4364 | out: |
| 4365 | return err; |
| 4366 | } |
| 4367 | |
| 4368 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4369 | * ufshcd_make_hba_operational - Make UFS controller operational |
| 4370 | * @hba: per adapter instance |
| 4371 | * |
| 4372 | * To bring UFS host controller to operational state, |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4373 | * 1. Enable required interrupts |
| 4374 | * 2. Configure interrupt aggregation |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 4375 | * 3. Program UTRL and UTMRL base address |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4376 | * 4. Configure run-stop-registers |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4377 | * |
| 4378 | * Returns 0 on success, non-zero value on failure |
| 4379 | */ |
| 4380 | static int ufshcd_make_hba_operational(struct ufs_hba *hba) |
| 4381 | { |
| 4382 | int err = 0; |
| 4383 | u32 reg; |
| 4384 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4385 | /* Enable required interrupts */ |
| 4386 | ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS); |
| 4387 | |
| 4388 | /* Configure interrupt aggregation */ |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 4389 | if (ufshcd_is_intr_aggr_allowed(hba)) |
| 4390 | ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO); |
| 4391 | else |
| 4392 | ufshcd_disable_intr_aggr(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4393 | |
| 4394 | /* Configure UTRL and UTMRL base address registers */ |
| 4395 | ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr), |
| 4396 | REG_UTP_TRANSFER_REQ_LIST_BASE_L); |
| 4397 | ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr), |
| 4398 | REG_UTP_TRANSFER_REQ_LIST_BASE_H); |
| 4399 | ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr), |
| 4400 | REG_UTP_TASK_REQ_LIST_BASE_L); |
| 4401 | ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr), |
| 4402 | REG_UTP_TASK_REQ_LIST_BASE_H); |
| 4403 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4404 | /* |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 4405 | * Make sure base address and interrupt setup are updated before |
| 4406 | * enabling the run/stop registers below. |
| 4407 | */ |
| 4408 | wmb(); |
| 4409 | |
| 4410 | /* |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4411 | * UCRDY, UTMRLDY and UTRLRDY bits must be 1 |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4412 | */ |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4413 | reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4414 | if (!(ufshcd_get_lists_status(reg))) { |
| 4415 | ufshcd_enable_run_stop_reg(hba); |
| 4416 | } else { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 4417 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4418 | "Host controller not ready to process requests"); |
| 4419 | err = -EIO; |
| 4420 | goto out; |
| 4421 | } |
| 4422 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4423 | out: |
| 4424 | return err; |
| 4425 | } |
| 4426 | |
| 4427 | /** |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4428 | * ufshcd_hba_stop - Send controller to reset state |
| 4429 | * @hba: per adapter instance |
| 4430 | * @can_sleep: perform sleep or just spin |
| 4431 | */ |
| 4432 | static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep) |
| 4433 | { |
| 4434 | int err; |
| 4435 | |
| 4436 | ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE); |
| 4437 | err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE, |
| 4438 | CONTROLLER_ENABLE, CONTROLLER_DISABLE, |
| 4439 | 10, 1, can_sleep); |
| 4440 | if (err) |
| 4441 | dev_err(hba->dev, "%s: Controller disable failed\n", __func__); |
| 4442 | } |
| 4443 | |
| 4444 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4445 | * ufshcd_hba_enable - initialize the controller |
| 4446 | * @hba: per adapter instance |
| 4447 | * |
| 4448 | * The controller resets itself and controller firmware initialization |
| 4449 | * sequence kicks off. When controller is ready it will set |
| 4450 | * the Host Controller Enable bit to 1. |
| 4451 | * |
| 4452 | * Returns 0 on success, non-zero value on failure |
| 4453 | */ |
| 4454 | static int ufshcd_hba_enable(struct ufs_hba *hba) |
| 4455 | { |
| 4456 | int retry; |
| 4457 | |
| 4458 | /* |
| 4459 | * msleep of 1 and 5 used in this function might result in msleep(20), |
| 4460 | * but it was necessary to send the UFS FPGA to reset mode during |
| 4461 | * development and testing of this driver. msleep can be changed to |
| 4462 | * mdelay and retry count can be reduced based on the controller. |
| 4463 | */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4464 | if (!ufshcd_is_hba_active(hba)) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4465 | /* change controller state to "reset state" */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4466 | ufshcd_hba_stop(hba, true); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4467 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4468 | /* UniPro link is disabled at this point */ |
| 4469 | ufshcd_set_link_off(hba); |
| 4470 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4471 | ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4472 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4473 | /* start controller initialization sequence */ |
| 4474 | ufshcd_hba_start(hba); |
| 4475 | |
| 4476 | /* |
| 4477 | * To initialize a UFS host controller HCE bit must be set to 1. |
| 4478 | * During initialization the HCE bit value changes from 1->0->1. |
| 4479 | * When the host controller completes initialization sequence |
| 4480 | * it sets the value of HCE bit to 1. The same HCE bit is read back |
| 4481 | * to check if the controller has completed initialization sequence. |
| 4482 | * So without this delay the value HCE = 1, set in the previous |
| 4483 | * instruction might be read back. |
| 4484 | * This delay can be changed based on the controller. |
| 4485 | */ |
| 4486 | msleep(1); |
| 4487 | |
| 4488 | /* wait for the host controller to complete initialization */ |
| 4489 | retry = 10; |
| 4490 | while (ufshcd_is_hba_active(hba)) { |
| 4491 | if (retry) { |
| 4492 | retry--; |
| 4493 | } else { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 4494 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4495 | "Controller enable failed\n"); |
| 4496 | return -EIO; |
| 4497 | } |
| 4498 | msleep(5); |
| 4499 | } |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4500 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4501 | /* enable UIC related interrupts */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4502 | ufshcd_enable_intr(hba, UFSHCD_UIC_MASK); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4503 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4504 | ufshcd_vops_hce_enable_notify(hba, POST_CHANGE); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4505 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4506 | return 0; |
| 4507 | } |
| 4508 | |
Yaniv Gardi | 7ca38cf | 2015-05-17 18:54:59 +0300 | [diff] [blame] | 4509 | static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer) |
| 4510 | { |
| 4511 | int tx_lanes, i, err = 0; |
| 4512 | |
| 4513 | if (!peer) |
| 4514 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 4515 | &tx_lanes); |
| 4516 | else |
| 4517 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 4518 | &tx_lanes); |
| 4519 | for (i = 0; i < tx_lanes; i++) { |
| 4520 | if (!peer) |
| 4521 | err = ufshcd_dme_set(hba, |
| 4522 | UIC_ARG_MIB_SEL(TX_LCC_ENABLE, |
| 4523 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)), |
| 4524 | 0); |
| 4525 | else |
| 4526 | err = ufshcd_dme_peer_set(hba, |
| 4527 | UIC_ARG_MIB_SEL(TX_LCC_ENABLE, |
| 4528 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)), |
| 4529 | 0); |
| 4530 | if (err) { |
| 4531 | dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d", |
| 4532 | __func__, peer, i, err); |
| 4533 | break; |
| 4534 | } |
| 4535 | } |
| 4536 | |
| 4537 | return err; |
| 4538 | } |
| 4539 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4540 | static inline int ufshcd_disable_host_tx_lcc(struct ufs_hba *hba) |
| 4541 | { |
| 4542 | return ufshcd_disable_tx_lcc(hba, false); |
| 4543 | } |
| 4544 | |
Yaniv Gardi | 7ca38cf | 2015-05-17 18:54:59 +0300 | [diff] [blame] | 4545 | static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba) |
| 4546 | { |
| 4547 | return ufshcd_disable_tx_lcc(hba, true); |
| 4548 | } |
| 4549 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4550 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4551 | * ufshcd_link_startup - Initialize unipro link startup |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4552 | * @hba: per adapter instance |
| 4553 | * |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4554 | * Returns 0 for success, non-zero in case of failure |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4555 | */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4556 | static int ufshcd_link_startup(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4557 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4558 | int ret; |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4559 | int retries = DME_LINKSTARTUP_RETRIES; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4560 | bool link_startup_again = false; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4561 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4562 | /* |
| 4563 | * If UFS device isn't active then we will have to issue link startup |
| 4564 | * 2 times to make sure the device state move to active. |
| 4565 | */ |
| 4566 | if (!ufshcd_is_ufs_dev_active(hba)) |
| 4567 | link_startup_again = true; |
| 4568 | |
| 4569 | link_startup: |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4570 | do { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4571 | ufshcd_vops_link_startup_notify(hba, PRE_CHANGE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4572 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4573 | ret = ufshcd_dme_link_startup(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4574 | if (ret) |
| 4575 | ufshcd_update_error_stats(hba, UFS_ERR_LINKSTARTUP); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4576 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4577 | /* check if device is detected by inter-connect layer */ |
| 4578 | if (!ret && !ufshcd_is_device_present(hba)) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4579 | ufshcd_update_error_stats(hba, UFS_ERR_LINKSTARTUP); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4580 | dev_err(hba->dev, "%s: Device not present\n", __func__); |
| 4581 | ret = -ENXIO; |
| 4582 | goto out; |
| 4583 | } |
| 4584 | |
| 4585 | /* |
| 4586 | * DME link lost indication is only received when link is up, |
| 4587 | * but we can't be sure if the link is up until link startup |
| 4588 | * succeeds. So reset the local Uni-Pro and try again. |
| 4589 | */ |
| 4590 | if (ret && ufshcd_hba_enable(hba)) |
| 4591 | goto out; |
| 4592 | } while (ret && retries--); |
| 4593 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4594 | if (ret) |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4595 | /* failed to get the link up... retire */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4596 | goto out; |
| 4597 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4598 | if (link_startup_again) { |
| 4599 | link_startup_again = false; |
| 4600 | retries = DME_LINKSTARTUP_RETRIES; |
| 4601 | goto link_startup; |
| 4602 | } |
| 4603 | |
| 4604 | /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */ |
| 4605 | ufshcd_init_pwr_info(hba); |
| 4606 | ufshcd_print_pwr_info(hba); |
| 4607 | |
Yaniv Gardi | 7ca38cf | 2015-05-17 18:54:59 +0300 | [diff] [blame] | 4608 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) { |
| 4609 | ret = ufshcd_disable_device_tx_lcc(hba); |
| 4610 | if (ret) |
| 4611 | goto out; |
| 4612 | } |
| 4613 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4614 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_BROKEN_LCC) { |
| 4615 | ret = ufshcd_disable_host_tx_lcc(hba); |
| 4616 | if (ret) |
| 4617 | goto out; |
| 4618 | } |
| 4619 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4620 | /* Include any host controller configuration via UIC commands */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4621 | ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE); |
| 4622 | if (ret) |
| 4623 | goto out; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4624 | |
| 4625 | ret = ufshcd_make_hba_operational(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4626 | out: |
| 4627 | if (ret) |
| 4628 | dev_err(hba->dev, "link startup failed %d\n", ret); |
| 4629 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4630 | } |
| 4631 | |
| 4632 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4633 | * ufshcd_verify_dev_init() - Verify device initialization |
| 4634 | * @hba: per-adapter instance |
| 4635 | * |
| 4636 | * Send NOP OUT UPIU and wait for NOP IN response to check whether the |
| 4637 | * device Transport Protocol (UTP) layer is ready after a reset. |
| 4638 | * If the UTP layer at the device side is not initialized, it may |
| 4639 | * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT |
| 4640 | * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations. |
| 4641 | */ |
| 4642 | static int ufshcd_verify_dev_init(struct ufs_hba *hba) |
| 4643 | { |
| 4644 | int err = 0; |
| 4645 | int retries; |
| 4646 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4647 | ufshcd_hold_all(hba); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4648 | mutex_lock(&hba->dev_cmd.lock); |
| 4649 | for (retries = NOP_OUT_RETRIES; retries > 0; retries--) { |
| 4650 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP, |
| 4651 | NOP_OUT_TIMEOUT); |
| 4652 | |
| 4653 | if (!err || err == -ETIMEDOUT) |
| 4654 | break; |
| 4655 | |
| 4656 | dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err); |
| 4657 | } |
| 4658 | mutex_unlock(&hba->dev_cmd.lock); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4659 | ufshcd_release_all(hba); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4660 | |
| 4661 | if (err) |
| 4662 | dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err); |
| 4663 | return err; |
| 4664 | } |
| 4665 | |
| 4666 | /** |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4667 | * ufshcd_set_queue_depth - set lun queue depth |
| 4668 | * @sdev: pointer to SCSI device |
| 4669 | * |
| 4670 | * Read bLUQueueDepth value and activate scsi tagged command |
| 4671 | * queueing. For WLUN, queue depth is set to 1. For best-effort |
| 4672 | * cases (bLUQueueDepth = 0) the queue depth is set to a maximum |
| 4673 | * value that host can queue. |
| 4674 | */ |
| 4675 | static void ufshcd_set_queue_depth(struct scsi_device *sdev) |
| 4676 | { |
| 4677 | int ret = 0; |
| 4678 | u8 lun_qdepth; |
| 4679 | struct ufs_hba *hba; |
| 4680 | |
| 4681 | hba = shost_priv(sdev->host); |
| 4682 | |
| 4683 | lun_qdepth = hba->nutrs; |
| 4684 | ret = ufshcd_read_unit_desc_param(hba, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4685 | ufshcd_scsi_to_upiu_lun(sdev->lun), |
| 4686 | UNIT_DESC_PARAM_LU_Q_DEPTH, |
| 4687 | &lun_qdepth, |
| 4688 | sizeof(lun_qdepth)); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4689 | |
| 4690 | /* Some WLUN doesn't support unit descriptor */ |
| 4691 | if (ret == -EOPNOTSUPP) |
| 4692 | lun_qdepth = 1; |
| 4693 | else if (!lun_qdepth) |
| 4694 | /* eventually, we can figure out the real queue depth */ |
| 4695 | lun_qdepth = hba->nutrs; |
| 4696 | else |
| 4697 | lun_qdepth = min_t(int, lun_qdepth, hba->nutrs); |
| 4698 | |
| 4699 | dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n", |
| 4700 | __func__, lun_qdepth); |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4701 | scsi_change_queue_depth(sdev, lun_qdepth); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4702 | } |
| 4703 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4704 | /* |
| 4705 | * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR |
| 4706 | * @hba: per-adapter instance |
| 4707 | * @lun: UFS device lun id |
| 4708 | * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info |
| 4709 | * |
| 4710 | * Returns 0 in case of success and b_lu_write_protect status would be returned |
| 4711 | * @b_lu_write_protect parameter. |
| 4712 | * Returns -ENOTSUPP if reading b_lu_write_protect is not supported. |
| 4713 | * Returns -EINVAL in case of invalid parameters passed to this function. |
| 4714 | */ |
| 4715 | static int ufshcd_get_lu_wp(struct ufs_hba *hba, |
| 4716 | u8 lun, |
| 4717 | u8 *b_lu_write_protect) |
| 4718 | { |
| 4719 | int ret; |
| 4720 | |
| 4721 | if (!b_lu_write_protect) |
| 4722 | ret = -EINVAL; |
| 4723 | /* |
| 4724 | * According to UFS device spec, RPMB LU can't be write |
| 4725 | * protected so skip reading bLUWriteProtect parameter for |
| 4726 | * it. For other W-LUs, UNIT DESCRIPTOR is not available. |
| 4727 | */ |
| 4728 | else if (lun >= UFS_UPIU_MAX_GENERAL_LUN) |
| 4729 | ret = -ENOTSUPP; |
| 4730 | else |
| 4731 | ret = ufshcd_read_unit_desc_param(hba, |
| 4732 | lun, |
| 4733 | UNIT_DESC_PARAM_LU_WR_PROTECT, |
| 4734 | b_lu_write_protect, |
| 4735 | sizeof(*b_lu_write_protect)); |
| 4736 | return ret; |
| 4737 | } |
| 4738 | |
| 4739 | /** |
| 4740 | * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect |
| 4741 | * status |
| 4742 | * @hba: per-adapter instance |
| 4743 | * @sdev: pointer to SCSI device |
| 4744 | * |
| 4745 | */ |
| 4746 | static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba, |
| 4747 | struct scsi_device *sdev) |
| 4748 | { |
| 4749 | if (hba->dev_info.f_power_on_wp_en && |
| 4750 | !hba->dev_info.is_lu_power_on_wp) { |
| 4751 | u8 b_lu_write_protect; |
| 4752 | |
| 4753 | if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun), |
| 4754 | &b_lu_write_protect) && |
| 4755 | (b_lu_write_protect == UFS_LU_POWER_ON_WP)) |
| 4756 | hba->dev_info.is_lu_power_on_wp = true; |
| 4757 | } |
| 4758 | } |
| 4759 | |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4760 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4761 | * ufshcd_slave_alloc - handle initial SCSI device configurations |
| 4762 | * @sdev: pointer to SCSI device |
| 4763 | * |
| 4764 | * Returns success |
| 4765 | */ |
| 4766 | static int ufshcd_slave_alloc(struct scsi_device *sdev) |
| 4767 | { |
| 4768 | struct ufs_hba *hba; |
| 4769 | |
| 4770 | hba = shost_priv(sdev->host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4771 | |
| 4772 | /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */ |
| 4773 | sdev->use_10_for_ms = 1; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4774 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4775 | /* allow SCSI layer to restart the device in case of errors */ |
| 4776 | sdev->allow_restart = 1; |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4777 | |
Sujit Reddy Thumma | b2a6c52 | 2014-07-01 12:22:38 +0300 | [diff] [blame] | 4778 | /* REPORT SUPPORTED OPERATION CODES is not supported */ |
| 4779 | sdev->no_report_opcodes = 1; |
| 4780 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4781 | /* WRITE_SAME command is not supported*/ |
| 4782 | sdev->no_write_same = 1; |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4783 | |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4784 | ufshcd_set_queue_depth(sdev); |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4785 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4786 | ufshcd_get_lu_power_on_wp_status(hba, sdev); |
| 4787 | |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4788 | return 0; |
| 4789 | } |
| 4790 | |
| 4791 | /** |
| 4792 | * ufshcd_change_queue_depth - change queue depth |
| 4793 | * @sdev: pointer to SCSI device |
| 4794 | * @depth: required depth to set |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4795 | * |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4796 | * 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] | 4797 | */ |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4798 | 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] | 4799 | { |
| 4800 | struct ufs_hba *hba = shost_priv(sdev->host); |
| 4801 | |
| 4802 | if (depth > hba->nutrs) |
| 4803 | depth = hba->nutrs; |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4804 | return scsi_change_queue_depth(sdev, depth); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4805 | } |
| 4806 | |
| 4807 | /** |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 4808 | * ufshcd_slave_configure - adjust SCSI device configurations |
| 4809 | * @sdev: pointer to SCSI device |
| 4810 | */ |
| 4811 | static int ufshcd_slave_configure(struct scsi_device *sdev) |
| 4812 | { |
| 4813 | struct request_queue *q = sdev->request_queue; |
Subhash Jadavani | 5ea586f | 2016-08-17 19:08:09 -0700 | [diff] [blame] | 4814 | struct ufs_hba *hba = shost_priv(sdev->host); |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 4815 | |
| 4816 | blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1); |
| 4817 | blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX); |
| 4818 | |
Subhash Jadavani | 5ea586f | 2016-08-17 19:08:09 -0700 | [diff] [blame] | 4819 | if (hba->scsi_cmd_timeout) { |
| 4820 | blk_queue_rq_timeout(q, hba->scsi_cmd_timeout * HZ); |
| 4821 | scsi_set_cmd_timeout_override(sdev, hba->scsi_cmd_timeout * HZ); |
| 4822 | } |
| 4823 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4824 | sdev->autosuspend_delay = UFSHCD_AUTO_SUSPEND_DELAY_MS; |
| 4825 | sdev->use_rpm_auto = 1; |
| 4826 | |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 4827 | return 0; |
| 4828 | } |
| 4829 | |
| 4830 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4831 | * ufshcd_slave_destroy - remove SCSI device configurations |
| 4832 | * @sdev: pointer to SCSI device |
| 4833 | */ |
| 4834 | static void ufshcd_slave_destroy(struct scsi_device *sdev) |
| 4835 | { |
| 4836 | struct ufs_hba *hba; |
| 4837 | |
| 4838 | hba = shost_priv(sdev->host); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4839 | /* Drop the reference as it won't be needed anymore */ |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 4840 | if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) { |
| 4841 | unsigned long flags; |
| 4842 | |
| 4843 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4844 | hba->sdev_ufs_device = NULL; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 4845 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4846 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4847 | } |
| 4848 | |
| 4849 | /** |
| 4850 | * ufshcd_task_req_compl - handle task management request completion |
| 4851 | * @hba: per adapter instance |
| 4852 | * @index: index of the completed request |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4853 | * @resp: task management service response |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4854 | * |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4855 | * Returns non-zero value on error, zero on success |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4856 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4857 | 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] | 4858 | { |
| 4859 | struct utp_task_req_desc *task_req_descp; |
| 4860 | struct utp_upiu_task_rsp *task_rsp_upiup; |
| 4861 | unsigned long flags; |
| 4862 | int ocs_value; |
| 4863 | int task_result; |
| 4864 | |
| 4865 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4866 | |
| 4867 | /* Clear completed tasks from outstanding_tasks */ |
| 4868 | __clear_bit(index, &hba->outstanding_tasks); |
| 4869 | |
| 4870 | task_req_descp = hba->utmrdl_base_addr; |
| 4871 | ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]); |
| 4872 | |
| 4873 | if (ocs_value == OCS_SUCCESS) { |
| 4874 | task_rsp_upiup = (struct utp_upiu_task_rsp *) |
| 4875 | task_req_descp[index].task_rsp_upiu; |
Kiwoong Kim | 8794ee0 | 2016-09-09 08:22:22 +0900 | [diff] [blame] | 4876 | task_result = be32_to_cpu(task_rsp_upiup->output_param1); |
| 4877 | task_result = task_result & MASK_TM_SERVICE_RESP; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4878 | if (resp) |
| 4879 | *resp = (u8)task_result; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4880 | } else { |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4881 | dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", |
| 4882 | __func__, ocs_value); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4883 | } |
| 4884 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4885 | |
| 4886 | return ocs_value; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4887 | } |
| 4888 | |
| 4889 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4890 | * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status |
| 4891 | * @lrb: pointer to local reference block of completed command |
| 4892 | * @scsi_status: SCSI command status |
| 4893 | * |
| 4894 | * Returns value base on SCSI command status |
| 4895 | */ |
| 4896 | static inline int |
| 4897 | ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status) |
| 4898 | { |
| 4899 | int result = 0; |
| 4900 | |
| 4901 | switch (scsi_status) { |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 4902 | case SAM_STAT_CHECK_CONDITION: |
| 4903 | ufshcd_copy_sense_data(lrbp); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4904 | case SAM_STAT_GOOD: |
| 4905 | result |= DID_OK << 16 | |
| 4906 | COMMAND_COMPLETE << 8 | |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 4907 | scsi_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4908 | break; |
| 4909 | case SAM_STAT_TASK_SET_FULL: |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 4910 | case SAM_STAT_BUSY: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4911 | case SAM_STAT_TASK_ABORTED: |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 4912 | ufshcd_copy_sense_data(lrbp); |
| 4913 | result |= scsi_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4914 | break; |
| 4915 | default: |
| 4916 | result |= DID_ERROR << 16; |
| 4917 | break; |
| 4918 | } /* end of switch */ |
| 4919 | |
| 4920 | return result; |
| 4921 | } |
| 4922 | |
| 4923 | /** |
| 4924 | * ufshcd_transfer_rsp_status - Get overall status of the response |
| 4925 | * @hba: per adapter instance |
| 4926 | * @lrb: pointer to local reference block of completed command |
| 4927 | * |
| 4928 | * Returns result of the command to notify SCSI midlayer |
| 4929 | */ |
| 4930 | static inline int |
| 4931 | ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 4932 | { |
| 4933 | int result = 0; |
| 4934 | int scsi_status; |
| 4935 | int ocs; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4936 | bool print_prdt; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4937 | |
| 4938 | /* overall command status of utrd */ |
| 4939 | ocs = ufshcd_get_tr_ocs(lrbp); |
| 4940 | |
| 4941 | switch (ocs) { |
| 4942 | case OCS_SUCCESS: |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4943 | result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4944 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4945 | switch (result) { |
| 4946 | case UPIU_TRANSACTION_RESPONSE: |
| 4947 | /* |
| 4948 | * get the response UPIU result to extract |
| 4949 | * the SCSI command status |
| 4950 | */ |
| 4951 | result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr); |
| 4952 | |
| 4953 | /* |
| 4954 | * get the result based on SCSI status response |
| 4955 | * to notify the SCSI midlayer of the command status |
| 4956 | */ |
| 4957 | scsi_status = result & MASK_SCSI_STATUS; |
| 4958 | result = ufshcd_scsi_cmd_status(lrbp, scsi_status); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4959 | |
Yaniv Gardi | f05ac2e | 2016-02-01 15:02:42 +0200 | [diff] [blame] | 4960 | /* |
| 4961 | * Currently we are only supporting BKOPs exception |
| 4962 | * events hence we can ignore BKOPs exception event |
| 4963 | * during power management callbacks. BKOPs exception |
| 4964 | * event is not expected to be raised in runtime suspend |
| 4965 | * callback as it allows the urgent bkops. |
| 4966 | * During system suspend, we are anyway forcefully |
| 4967 | * disabling the bkops and if urgent bkops is needed |
| 4968 | * it will be enabled on system resume. Long term |
| 4969 | * solution could be to abort the system suspend if |
| 4970 | * UFS device needs urgent BKOPs. |
| 4971 | */ |
| 4972 | if (!hba->pm_op_in_progress && |
| 4973 | ufshcd_is_exception_event(lrbp->ucd_rsp_ptr)) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4974 | schedule_work(&hba->eeh_work); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4975 | break; |
| 4976 | case UPIU_TRANSACTION_REJECT_UPIU: |
| 4977 | /* TODO: handle Reject UPIU Response */ |
| 4978 | result = DID_ERROR << 16; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 4979 | dev_err(hba->dev, |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4980 | "Reject UPIU not fully implemented\n"); |
| 4981 | break; |
| 4982 | default: |
| 4983 | result = DID_ERROR << 16; |
| 4984 | dev_err(hba->dev, |
| 4985 | "Unexpected request response code = %x\n", |
| 4986 | result); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4987 | break; |
| 4988 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4989 | break; |
| 4990 | case OCS_ABORTED: |
| 4991 | result |= DID_ABORT << 16; |
| 4992 | break; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4993 | case OCS_INVALID_COMMAND_STATUS: |
| 4994 | result |= DID_REQUEUE << 16; |
| 4995 | break; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4996 | case OCS_INVALID_CMD_TABLE_ATTR: |
| 4997 | case OCS_INVALID_PRDT_ATTR: |
| 4998 | case OCS_MISMATCH_DATA_BUF_SIZE: |
| 4999 | case OCS_MISMATCH_RESP_UPIU_SIZE: |
| 5000 | case OCS_PEER_COMM_FAILURE: |
| 5001 | case OCS_FATAL_ERROR: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5002 | case OCS_DEVICE_FATAL_ERROR: |
| 5003 | case OCS_INVALID_CRYPTO_CONFIG: |
| 5004 | case OCS_GENERAL_CRYPTO_ERROR: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5005 | default: |
| 5006 | result |= DID_ERROR << 16; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 5007 | dev_err(hba->dev, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5008 | "OCS error from controller = %x for tag %d\n", |
| 5009 | ocs, lrbp->task_tag); |
| 5010 | ufshcd_print_host_regs(hba); |
| 5011 | ufshcd_print_host_state(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5012 | break; |
| 5013 | } /* end of switch */ |
| 5014 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5015 | if ((host_byte(result) != DID_OK) && !hba->silence_err_logs) { |
| 5016 | print_prdt = (ocs == OCS_INVALID_PRDT_ATTR || |
| 5017 | ocs == OCS_MISMATCH_DATA_BUF_SIZE); |
| 5018 | ufshcd_print_trs(hba, 1 << lrbp->task_tag, print_prdt); |
| 5019 | } |
| 5020 | |
| 5021 | if ((host_byte(result) == DID_ERROR) || |
| 5022 | (host_byte(result) == DID_ABORT)) |
| 5023 | ufsdbg_set_err_state(hba); |
| 5024 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5025 | return result; |
| 5026 | } |
| 5027 | |
| 5028 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5029 | * ufshcd_uic_cmd_compl - handle completion of uic command |
| 5030 | * @hba: per adapter instance |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 5031 | * @intr_status: interrupt status generated by the controller |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5032 | */ |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 5033 | 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] | 5034 | { |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 5035 | if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5036 | hba->active_uic_cmd->argument2 |= |
| 5037 | ufshcd_get_uic_cmd_result(hba); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 5038 | hba->active_uic_cmd->argument3 = |
| 5039 | ufshcd_get_dme_attr_val(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5040 | complete(&hba->active_uic_cmd->done); |
| 5041 | } |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 5042 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5043 | if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done) |
| 5044 | complete(hba->uic_async_done); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5045 | } |
| 5046 | |
| 5047 | /** |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5048 | * ufshcd_abort_outstanding_requests - abort all outstanding transfer requests. |
| 5049 | * @hba: per adapter instance |
| 5050 | * @result: error result to inform scsi layer about |
| 5051 | */ |
| 5052 | void ufshcd_abort_outstanding_transfer_requests(struct ufs_hba *hba, int result) |
| 5053 | { |
| 5054 | u8 index; |
| 5055 | struct ufshcd_lrb *lrbp; |
| 5056 | struct scsi_cmnd *cmd; |
| 5057 | |
| 5058 | if (!hba->outstanding_reqs) |
| 5059 | return; |
| 5060 | |
| 5061 | for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) { |
| 5062 | lrbp = &hba->lrb[index]; |
| 5063 | cmd = lrbp->cmd; |
| 5064 | if (cmd) { |
| 5065 | ufshcd_cond_add_cmd_trace(hba, index, "failed"); |
| 5066 | ufshcd_update_error_stats(hba, |
| 5067 | UFS_ERR_INT_FATAL_ERRORS); |
| 5068 | scsi_dma_unmap(cmd); |
| 5069 | cmd->result = result; |
| 5070 | /* Clear pending transfer requests */ |
| 5071 | ufshcd_clear_cmd(hba, index); |
| 5072 | ufshcd_outstanding_req_clear(hba, index); |
| 5073 | clear_bit_unlock(index, &hba->lrb_in_use); |
| 5074 | lrbp->complete_time_stamp = ktime_get(); |
| 5075 | update_req_stats(hba, lrbp); |
| 5076 | /* Mark completed command as NULL in LRB */ |
| 5077 | lrbp->cmd = NULL; |
| 5078 | ufshcd_release_all(hba); |
| 5079 | if (cmd->request) { |
| 5080 | /* |
| 5081 | * As we are accessing the "request" structure, |
| 5082 | * this must be called before calling |
| 5083 | * ->scsi_done() callback. |
| 5084 | */ |
| 5085 | ufshcd_vops_pm_qos_req_end(hba, cmd->request, |
| 5086 | true); |
| 5087 | ufshcd_vops_crypto_engine_cfg_end(hba, |
| 5088 | lrbp, cmd->request); |
| 5089 | } |
| 5090 | /* Do not touch lrbp after scsi done */ |
| 5091 | cmd->scsi_done(cmd); |
| 5092 | } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE) { |
| 5093 | if (hba->dev_cmd.complete) { |
| 5094 | ufshcd_cond_add_cmd_trace(hba, index, |
| 5095 | "dev_failed"); |
| 5096 | ufshcd_outstanding_req_clear(hba, index); |
| 5097 | complete(hba->dev_cmd.complete); |
| 5098 | } |
| 5099 | } |
| 5100 | if (ufshcd_is_clkscaling_supported(hba)) |
| 5101 | hba->clk_scaling.active_reqs--; |
| 5102 | } |
| 5103 | } |
| 5104 | |
| 5105 | /** |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5106 | * __ufshcd_transfer_req_compl - handle SCSI and query command completion |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5107 | * @hba: per adapter instance |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5108 | * @completed_reqs: requests to complete |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5109 | */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5110 | static void __ufshcd_transfer_req_compl(struct ufs_hba *hba, |
| 5111 | unsigned long completed_reqs) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5112 | { |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 5113 | struct ufshcd_lrb *lrbp; |
| 5114 | struct scsi_cmnd *cmd; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5115 | int result; |
| 5116 | int index; |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 5117 | struct request *req; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5118 | |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5119 | for_each_set_bit(index, &completed_reqs, hba->nutrs) { |
| 5120 | lrbp = &hba->lrb[index]; |
| 5121 | cmd = lrbp->cmd; |
| 5122 | if (cmd) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5123 | ufshcd_cond_add_cmd_trace(hba, index, "complete"); |
| 5124 | ufshcd_update_tag_stats_completion(hba, cmd); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5125 | result = ufshcd_transfer_rsp_status(hba, lrbp); |
| 5126 | scsi_dma_unmap(cmd); |
| 5127 | cmd->result = result; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5128 | clear_bit_unlock(index, &hba->lrb_in_use); |
| 5129 | lrbp->complete_time_stamp = ktime_get(); |
| 5130 | update_req_stats(hba, lrbp); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5131 | /* Mark completed command as NULL in LRB */ |
| 5132 | lrbp->cmd = NULL; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5133 | __ufshcd_release(hba, false); |
| 5134 | __ufshcd_hibern8_release(hba, false); |
| 5135 | if (cmd->request) { |
| 5136 | /* |
| 5137 | * As we are accessing the "request" structure, |
| 5138 | * this must be called before calling |
| 5139 | * ->scsi_done() callback. |
| 5140 | */ |
| 5141 | ufshcd_vops_pm_qos_req_end(hba, cmd->request, |
| 5142 | false); |
| 5143 | ufshcd_vops_crypto_engine_cfg_end(hba, |
| 5144 | lrbp, cmd->request); |
| 5145 | } |
| 5146 | |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 5147 | req = cmd->request; |
| 5148 | if (req) { |
| 5149 | /* Update IO svc time latency histogram */ |
| 5150 | if (req->lat_hist_enabled) { |
| 5151 | ktime_t completion; |
| 5152 | u_int64_t delta_us; |
| 5153 | |
| 5154 | completion = ktime_get(); |
| 5155 | delta_us = ktime_us_delta(completion, |
| 5156 | req->lat_hist_io_start); |
| 5157 | /* rq_data_dir() => true if WRITE */ |
| 5158 | blk_update_latency_hist(&hba->io_lat_s, |
| 5159 | (rq_data_dir(req) == READ), |
| 5160 | delta_us); |
| 5161 | } |
| 5162 | } |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5163 | /* Do not touch lrbp after scsi done */ |
| 5164 | cmd->scsi_done(cmd); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5165 | } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE) { |
| 5166 | if (hba->dev_cmd.complete) { |
| 5167 | ufshcd_cond_add_cmd_trace(hba, index, |
| 5168 | "dev_complete"); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5169 | complete(hba->dev_cmd.complete); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5170 | } |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5171 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5172 | if (ufshcd_is_clkscaling_supported(hba)) |
| 5173 | hba->clk_scaling.active_reqs--; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5174 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5175 | |
| 5176 | /* clear corresponding bits of completed commands */ |
| 5177 | hba->outstanding_reqs ^= completed_reqs; |
| 5178 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 5179 | ufshcd_clk_scaling_update_busy(hba); |
| 5180 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 5181 | /* we might have free'd some tags above */ |
| 5182 | wake_up(&hba->dev_cmd.tag_wq); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5183 | } |
| 5184 | |
| 5185 | /** |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5186 | * ufshcd_transfer_req_compl - handle SCSI and query command completion |
| 5187 | * @hba: per adapter instance |
| 5188 | */ |
| 5189 | static void ufshcd_transfer_req_compl(struct ufs_hba *hba) |
| 5190 | { |
| 5191 | unsigned long completed_reqs; |
| 5192 | u32 tr_doorbell; |
| 5193 | |
| 5194 | /* Resetting interrupt aggregation counters first and reading the |
| 5195 | * DOOR_BELL afterward allows us to handle all the completed requests. |
| 5196 | * In order to prevent other interrupts starvation the DB is read once |
| 5197 | * after reset. The down side of this solution is the possibility of |
| 5198 | * false interrupt if device completes another request after resetting |
| 5199 | * aggregation and before reading the DB. |
| 5200 | */ |
| 5201 | if (ufshcd_is_intr_aggr_allowed(hba)) |
| 5202 | ufshcd_reset_intr_aggr(hba); |
| 5203 | |
| 5204 | tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 5205 | completed_reqs = tr_doorbell ^ hba->outstanding_reqs; |
| 5206 | |
| 5207 | __ufshcd_transfer_req_compl(hba, completed_reqs); |
| 5208 | } |
| 5209 | |
| 5210 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5211 | * ufshcd_disable_ee - disable exception event |
| 5212 | * @hba: per-adapter instance |
| 5213 | * @mask: exception event to disable |
| 5214 | * |
| 5215 | * Disables exception event in the device so that the EVENT_ALERT |
| 5216 | * bit is not set. |
| 5217 | * |
| 5218 | * Returns zero on success, non-zero error value on failure. |
| 5219 | */ |
| 5220 | static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask) |
| 5221 | { |
| 5222 | int err = 0; |
| 5223 | u32 val; |
| 5224 | |
| 5225 | if (!(hba->ee_ctrl_mask & mask)) |
| 5226 | goto out; |
| 5227 | |
| 5228 | val = hba->ee_ctrl_mask & ~mask; |
| 5229 | val &= 0xFFFF; /* 2 bytes */ |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 5230 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5231 | QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val); |
| 5232 | if (!err) |
| 5233 | hba->ee_ctrl_mask &= ~mask; |
| 5234 | out: |
| 5235 | return err; |
| 5236 | } |
| 5237 | |
| 5238 | /** |
| 5239 | * ufshcd_enable_ee - enable exception event |
| 5240 | * @hba: per-adapter instance |
| 5241 | * @mask: exception event to enable |
| 5242 | * |
| 5243 | * Enable corresponding exception event in the device to allow |
| 5244 | * device to alert host in critical scenarios. |
| 5245 | * |
| 5246 | * Returns zero on success, non-zero error value on failure. |
| 5247 | */ |
| 5248 | static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask) |
| 5249 | { |
| 5250 | int err = 0; |
| 5251 | u32 val; |
| 5252 | |
| 5253 | if (hba->ee_ctrl_mask & mask) |
| 5254 | goto out; |
| 5255 | |
| 5256 | val = hba->ee_ctrl_mask | mask; |
| 5257 | val &= 0xFFFF; /* 2 bytes */ |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 5258 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5259 | QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val); |
| 5260 | if (!err) |
| 5261 | hba->ee_ctrl_mask |= mask; |
| 5262 | out: |
| 5263 | return err; |
| 5264 | } |
| 5265 | |
| 5266 | /** |
| 5267 | * ufshcd_enable_auto_bkops - Allow device managed BKOPS |
| 5268 | * @hba: per-adapter instance |
| 5269 | * |
| 5270 | * Allow device to manage background operations on its own. Enabling |
| 5271 | * this might lead to inconsistent latencies during normal data transfers |
| 5272 | * as the device is allowed to manage its own way of handling background |
| 5273 | * operations. |
| 5274 | * |
| 5275 | * Returns zero on success, non-zero on failure. |
| 5276 | */ |
| 5277 | static int ufshcd_enable_auto_bkops(struct ufs_hba *hba) |
| 5278 | { |
| 5279 | int err = 0; |
| 5280 | |
| 5281 | if (hba->auto_bkops_enabled) |
| 5282 | goto out; |
| 5283 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 5284 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5285 | QUERY_FLAG_IDN_BKOPS_EN, NULL); |
| 5286 | if (err) { |
| 5287 | dev_err(hba->dev, "%s: failed to enable bkops %d\n", |
| 5288 | __func__, err); |
| 5289 | goto out; |
| 5290 | } |
| 5291 | |
| 5292 | hba->auto_bkops_enabled = true; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5293 | trace_ufshcd_auto_bkops_state(dev_name(hba->dev), 1); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5294 | |
| 5295 | /* No need of URGENT_BKOPS exception from the device */ |
| 5296 | err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 5297 | if (err) |
| 5298 | dev_err(hba->dev, "%s: failed to disable exception event %d\n", |
| 5299 | __func__, err); |
| 5300 | out: |
| 5301 | return err; |
| 5302 | } |
| 5303 | |
| 5304 | /** |
| 5305 | * ufshcd_disable_auto_bkops - block device in doing background operations |
| 5306 | * @hba: per-adapter instance |
| 5307 | * |
| 5308 | * Disabling background operations improves command response latency but |
| 5309 | * has drawback of device moving into critical state where the device is |
| 5310 | * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the |
| 5311 | * host is idle so that BKOPS are managed effectively without any negative |
| 5312 | * impacts. |
| 5313 | * |
| 5314 | * Returns zero on success, non-zero on failure. |
| 5315 | */ |
| 5316 | static int ufshcd_disable_auto_bkops(struct ufs_hba *hba) |
| 5317 | { |
| 5318 | int err = 0; |
| 5319 | |
| 5320 | if (!hba->auto_bkops_enabled) |
| 5321 | goto out; |
| 5322 | |
| 5323 | /* |
| 5324 | * If host assisted BKOPs is to be enabled, make sure |
| 5325 | * urgent bkops exception is allowed. |
| 5326 | */ |
| 5327 | err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 5328 | if (err) { |
| 5329 | dev_err(hba->dev, "%s: failed to enable exception event %d\n", |
| 5330 | __func__, err); |
| 5331 | goto out; |
| 5332 | } |
| 5333 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 5334 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5335 | QUERY_FLAG_IDN_BKOPS_EN, NULL); |
| 5336 | if (err) { |
| 5337 | dev_err(hba->dev, "%s: failed to disable bkops %d\n", |
| 5338 | __func__, err); |
| 5339 | ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 5340 | goto out; |
| 5341 | } |
| 5342 | |
| 5343 | hba->auto_bkops_enabled = false; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5344 | trace_ufshcd_auto_bkops_state(dev_name(hba->dev), 0); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5345 | out: |
| 5346 | return err; |
| 5347 | } |
| 5348 | |
| 5349 | /** |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5350 | * ufshcd_force_reset_auto_bkops - force reset auto bkops state |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5351 | * @hba: per adapter instance |
| 5352 | * |
| 5353 | * After a device reset the device may toggle the BKOPS_EN flag |
| 5354 | * to default value. The s/w tracking variables should be updated |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5355 | * as well. This function would change the auto-bkops state based on |
| 5356 | * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5357 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5358 | static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5359 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5360 | if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) { |
| 5361 | hba->auto_bkops_enabled = false; |
| 5362 | hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS; |
| 5363 | ufshcd_enable_auto_bkops(hba); |
| 5364 | } else { |
| 5365 | hba->auto_bkops_enabled = true; |
| 5366 | hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS; |
| 5367 | ufshcd_disable_auto_bkops(hba); |
| 5368 | } |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5369 | } |
| 5370 | |
| 5371 | static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status) |
| 5372 | { |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 5373 | return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5374 | QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status); |
| 5375 | } |
| 5376 | |
| 5377 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5378 | * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status |
| 5379 | * @hba: per-adapter instance |
| 5380 | * @status: bkops_status value |
| 5381 | * |
| 5382 | * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn |
| 5383 | * flag in the device to permit background operations if the device |
| 5384 | * bkops_status is greater than or equal to "status" argument passed to |
| 5385 | * this function, disable otherwise. |
| 5386 | * |
| 5387 | * Returns 0 for success, non-zero in case of failure. |
| 5388 | * |
| 5389 | * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag |
| 5390 | * to know whether auto bkops is enabled or disabled after this function |
| 5391 | * returns control to it. |
| 5392 | */ |
| 5393 | static int ufshcd_bkops_ctrl(struct ufs_hba *hba, |
| 5394 | enum bkops_status status) |
| 5395 | { |
| 5396 | int err; |
| 5397 | u32 curr_status = 0; |
| 5398 | |
| 5399 | err = ufshcd_get_bkops_status(hba, &curr_status); |
| 5400 | if (err) { |
| 5401 | dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", |
| 5402 | __func__, err); |
| 5403 | goto out; |
| 5404 | } else if (curr_status > BKOPS_STATUS_MAX) { |
| 5405 | dev_err(hba->dev, "%s: invalid BKOPS status %d\n", |
| 5406 | __func__, curr_status); |
| 5407 | err = -EINVAL; |
| 5408 | goto out; |
| 5409 | } |
| 5410 | |
| 5411 | if (curr_status >= status) |
| 5412 | err = ufshcd_enable_auto_bkops(hba); |
| 5413 | else |
| 5414 | err = ufshcd_disable_auto_bkops(hba); |
| 5415 | out: |
| 5416 | return err; |
| 5417 | } |
| 5418 | |
| 5419 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5420 | * ufshcd_urgent_bkops - handle urgent bkops exception event |
| 5421 | * @hba: per-adapter instance |
| 5422 | * |
| 5423 | * Enable fBackgroundOpsEn flag in the device to permit background |
| 5424 | * operations. |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5425 | * |
| 5426 | * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled |
| 5427 | * and negative error value for any other failure. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5428 | */ |
| 5429 | static int ufshcd_urgent_bkops(struct ufs_hba *hba) |
| 5430 | { |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 5431 | return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5432 | } |
| 5433 | |
| 5434 | static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status) |
| 5435 | { |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 5436 | return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5437 | QUERY_ATTR_IDN_EE_STATUS, 0, 0, status); |
| 5438 | } |
| 5439 | |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 5440 | static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba) |
| 5441 | { |
| 5442 | int err; |
| 5443 | u32 curr_status = 0; |
| 5444 | |
| 5445 | if (hba->is_urgent_bkops_lvl_checked) |
| 5446 | goto enable_auto_bkops; |
| 5447 | |
| 5448 | err = ufshcd_get_bkops_status(hba, &curr_status); |
| 5449 | if (err) { |
| 5450 | dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", |
| 5451 | __func__, err); |
| 5452 | goto out; |
| 5453 | } |
| 5454 | |
| 5455 | /* |
| 5456 | * We are seeing that some devices are raising the urgent bkops |
| 5457 | * exception events even when BKOPS status doesn't indicate performace |
| 5458 | * impacted or critical. Handle these device by determining their urgent |
| 5459 | * bkops status at runtime. |
| 5460 | */ |
| 5461 | if (curr_status < BKOPS_STATUS_PERF_IMPACT) { |
| 5462 | dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n", |
| 5463 | __func__, curr_status); |
| 5464 | /* update the current status as the urgent bkops level */ |
| 5465 | hba->urgent_bkops_lvl = curr_status; |
| 5466 | hba->is_urgent_bkops_lvl_checked = true; |
| 5467 | } |
| 5468 | |
| 5469 | enable_auto_bkops: |
| 5470 | err = ufshcd_enable_auto_bkops(hba); |
| 5471 | out: |
| 5472 | if (err < 0) |
| 5473 | dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n", |
| 5474 | __func__, err); |
| 5475 | } |
| 5476 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5477 | /** |
| 5478 | * ufshcd_exception_event_handler - handle exceptions raised by device |
| 5479 | * @work: pointer to work data |
| 5480 | * |
| 5481 | * Read bExceptionEventStatus attribute from the device and handle the |
| 5482 | * exception event accordingly. |
| 5483 | */ |
| 5484 | static void ufshcd_exception_event_handler(struct work_struct *work) |
| 5485 | { |
| 5486 | struct ufs_hba *hba; |
| 5487 | int err; |
| 5488 | u32 status = 0; |
| 5489 | hba = container_of(work, struct ufs_hba, eeh_work); |
| 5490 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 5491 | pm_runtime_get_sync(hba->dev); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5492 | ufshcd_scsi_block_requests(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5493 | err = ufshcd_get_ee_status(hba, &status); |
| 5494 | if (err) { |
| 5495 | dev_err(hba->dev, "%s: failed to get exception status %d\n", |
| 5496 | __func__, err); |
| 5497 | goto out; |
| 5498 | } |
| 5499 | |
| 5500 | status &= hba->ee_ctrl_mask; |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 5501 | |
| 5502 | if (status & MASK_EE_URGENT_BKOPS) |
| 5503 | ufshcd_bkops_exception_event_handler(hba); |
| 5504 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5505 | out: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5506 | ufshcd_scsi_unblock_requests(hba); |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 5507 | pm_runtime_put_sync(hba->dev); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5508 | return; |
| 5509 | } |
| 5510 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5511 | /* Complete requests that have door-bell cleared */ |
| 5512 | static void ufshcd_complete_requests(struct ufs_hba *hba) |
| 5513 | { |
| 5514 | ufshcd_transfer_req_compl(hba); |
| 5515 | ufshcd_tmc_handler(hba); |
| 5516 | } |
| 5517 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5518 | /** |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5519 | * ufshcd_quirk_dl_nac_errors - This function checks if error handling is |
| 5520 | * to recover from the DL NAC errors or not. |
| 5521 | * @hba: per-adapter instance |
| 5522 | * |
| 5523 | * Returns true if error handling is required, false otherwise |
| 5524 | */ |
| 5525 | static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba) |
| 5526 | { |
| 5527 | unsigned long flags; |
| 5528 | bool err_handling = true; |
| 5529 | |
| 5530 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5531 | /* |
| 5532 | * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the |
| 5533 | * device fatal error and/or DL NAC & REPLAY timeout errors. |
| 5534 | */ |
| 5535 | if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR)) |
| 5536 | goto out; |
| 5537 | |
| 5538 | if ((hba->saved_err & DEVICE_FATAL_ERROR) || |
| 5539 | ((hba->saved_err & UIC_ERROR) && |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5540 | (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR))) { |
| 5541 | /* |
| 5542 | * we have to do error recovery but atleast silence the error |
| 5543 | * logs. |
| 5544 | */ |
| 5545 | hba->silence_err_logs = true; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5546 | goto out; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5547 | } |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5548 | |
| 5549 | if ((hba->saved_err & UIC_ERROR) && |
| 5550 | (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) { |
| 5551 | int err; |
| 5552 | /* |
| 5553 | * wait for 50ms to see if we can get any other errors or not. |
| 5554 | */ |
| 5555 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5556 | msleep(50); |
| 5557 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5558 | |
| 5559 | /* |
| 5560 | * now check if we have got any other severe errors other than |
| 5561 | * DL NAC error? |
| 5562 | */ |
| 5563 | if ((hba->saved_err & INT_FATAL_ERRORS) || |
| 5564 | ((hba->saved_err & UIC_ERROR) && |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5565 | (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR))) { |
| 5566 | if (((hba->saved_err & INT_FATAL_ERRORS) == |
| 5567 | DEVICE_FATAL_ERROR) || (hba->saved_uic_err & |
| 5568 | ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) |
| 5569 | hba->silence_err_logs = true; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5570 | goto out; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5571 | } |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5572 | |
| 5573 | /* |
| 5574 | * As DL NAC is the only error received so far, send out NOP |
| 5575 | * command to confirm if link is still active or not. |
| 5576 | * - If we don't get any response then do error recovery. |
| 5577 | * - If we get response then clear the DL NAC error bit. |
| 5578 | */ |
| 5579 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5580 | /* silence the error logs from NOP command */ |
| 5581 | hba->silence_err_logs = true; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5582 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5583 | err = ufshcd_verify_dev_init(hba); |
| 5584 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5585 | hba->silence_err_logs = false; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5586 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5587 | if (err) { |
| 5588 | hba->silence_err_logs = true; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5589 | goto out; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5590 | } |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5591 | |
| 5592 | /* Link seems to be alive hence ignore the DL NAC errors */ |
| 5593 | if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR) |
| 5594 | hba->saved_err &= ~UIC_ERROR; |
| 5595 | /* clear NAC error */ |
| 5596 | hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR; |
| 5597 | if (!hba->saved_uic_err) { |
| 5598 | err_handling = false; |
| 5599 | goto out; |
| 5600 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5601 | /* |
| 5602 | * there seems to be some errors other than NAC, so do error |
| 5603 | * recovery |
| 5604 | */ |
| 5605 | hba->silence_err_logs = true; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5606 | } |
| 5607 | out: |
| 5608 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5609 | return err_handling; |
| 5610 | } |
| 5611 | |
| 5612 | /** |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5613 | * ufshcd_err_handler - handle UFS errors that require s/w attention |
| 5614 | * @work: pointer to work structure |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5615 | */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5616 | static void ufshcd_err_handler(struct work_struct *work) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5617 | { |
| 5618 | struct ufs_hba *hba; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5619 | unsigned long flags; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5620 | bool err_xfer = false, err_tm = false; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5621 | int err = 0; |
| 5622 | int tag; |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5623 | bool needs_reset = false; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5624 | |
| 5625 | hba = container_of(work, struct ufs_hba, eh_work); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5626 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5627 | ufsdbg_set_err_state(hba); |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 5628 | pm_runtime_get_sync(hba->dev); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5629 | ufshcd_hold_all(hba); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5630 | |
| 5631 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5632 | if (hba->ufshcd_state == UFSHCD_STATE_RESET) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5633 | goto out; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5634 | |
| 5635 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 5636 | ufshcd_set_eh_in_progress(hba); |
| 5637 | |
| 5638 | /* Complete requests that have door-bell cleared by h/w */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5639 | ufshcd_complete_requests(hba); |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5640 | |
| 5641 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) { |
| 5642 | bool ret; |
| 5643 | |
| 5644 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5645 | /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */ |
| 5646 | ret = ufshcd_quirk_dl_nac_errors(hba); |
| 5647 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5648 | if (!ret) |
| 5649 | goto skip_err_handling; |
| 5650 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5651 | |
| 5652 | /* |
| 5653 | * Dump controller state before resetting. Transfer requests state |
| 5654 | * will be dump as part of the request completion. |
| 5655 | */ |
| 5656 | if (hba->saved_err & (INT_FATAL_ERRORS | UIC_ERROR)) { |
| 5657 | dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x", |
| 5658 | __func__, hba->saved_err, hba->saved_uic_err); |
| 5659 | if (!hba->silence_err_logs) { |
| 5660 | ufshcd_print_host_regs(hba); |
| 5661 | ufshcd_print_host_state(hba); |
| 5662 | ufshcd_print_pwr_info(hba); |
| 5663 | ufshcd_print_tmrs(hba, hba->outstanding_tasks); |
| 5664 | } |
| 5665 | } |
| 5666 | |
| 5667 | if ((hba->saved_err & INT_FATAL_ERRORS) || hba->saved_ce_err || |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5668 | ((hba->saved_err & UIC_ERROR) && |
| 5669 | (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR | |
| 5670 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR | |
| 5671 | UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))) |
| 5672 | needs_reset = true; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5673 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5674 | /* |
| 5675 | * if host reset is required then skip clearing the pending |
| 5676 | * transfers forcefully because they will automatically get |
| 5677 | * cleared after link startup. |
| 5678 | */ |
| 5679 | if (needs_reset) |
| 5680 | goto skip_pending_xfer_clear; |
| 5681 | |
| 5682 | /* release lock as clear command might sleep */ |
| 5683 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5684 | /* Clear pending transfer requests */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5685 | for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) { |
| 5686 | if (ufshcd_clear_cmd(hba, tag)) { |
| 5687 | err_xfer = true; |
| 5688 | goto lock_skip_pending_xfer_clear; |
| 5689 | } |
| 5690 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5691 | |
| 5692 | /* Clear pending task management requests */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5693 | for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) { |
| 5694 | if (ufshcd_clear_tm_cmd(hba, tag)) { |
| 5695 | err_tm = true; |
| 5696 | goto lock_skip_pending_xfer_clear; |
| 5697 | } |
| 5698 | } |
| 5699 | |
| 5700 | lock_skip_pending_xfer_clear: |
| 5701 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5702 | |
| 5703 | /* Complete the requests that are cleared by s/w */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5704 | ufshcd_complete_requests(hba); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5705 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5706 | if (err_xfer || err_tm) |
| 5707 | needs_reset = true; |
| 5708 | |
| 5709 | skip_pending_xfer_clear: |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5710 | /* Fatal errors need reset */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5711 | if (needs_reset) { |
| 5712 | unsigned long max_doorbells = (1UL << hba->nutrs) - 1; |
| 5713 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5714 | if (hba->saved_err & INT_FATAL_ERRORS) |
| 5715 | ufshcd_update_error_stats(hba, |
| 5716 | UFS_ERR_INT_FATAL_ERRORS); |
| 5717 | if (hba->saved_ce_err) |
| 5718 | ufshcd_update_error_stats(hba, UFS_ERR_CRYPTO_ENGINE); |
| 5719 | |
| 5720 | if (hba->saved_err & UIC_ERROR) |
| 5721 | ufshcd_update_error_stats(hba, |
| 5722 | UFS_ERR_INT_UIC_ERROR); |
| 5723 | |
| 5724 | if (err_xfer || err_tm) |
| 5725 | ufshcd_update_error_stats(hba, |
| 5726 | UFS_ERR_CLEAR_PEND_XFER_TM); |
| 5727 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5728 | /* |
| 5729 | * ufshcd_reset_and_restore() does the link reinitialization |
| 5730 | * which will need atleast one empty doorbell slot to send the |
| 5731 | * device management commands (NOP and query commands). |
| 5732 | * If there is no slot empty at this moment then free up last |
| 5733 | * slot forcefully. |
| 5734 | */ |
| 5735 | if (hba->outstanding_reqs == max_doorbells) |
| 5736 | __ufshcd_transfer_req_compl(hba, |
| 5737 | (1UL << (hba->nutrs - 1))); |
| 5738 | |
| 5739 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5740 | err = ufshcd_reset_and_restore(hba); |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5741 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5742 | if (err) { |
| 5743 | dev_err(hba->dev, "%s: reset and restore failed\n", |
| 5744 | __func__); |
| 5745 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 5746 | } |
| 5747 | /* |
| 5748 | * Inform scsi mid-layer that we did reset and allow to handle |
| 5749 | * Unit Attention properly. |
| 5750 | */ |
| 5751 | scsi_report_bus_reset(hba->host, 0); |
| 5752 | hba->saved_err = 0; |
| 5753 | hba->saved_uic_err = 0; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5754 | hba->saved_ce_err = 0; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5755 | } |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5756 | |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5757 | skip_err_handling: |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5758 | if (!needs_reset) { |
| 5759 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
| 5760 | if (hba->saved_err || hba->saved_uic_err) |
| 5761 | dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x", |
| 5762 | __func__, hba->saved_err, hba->saved_uic_err); |
| 5763 | } |
| 5764 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5765 | hba->silence_err_logs = false; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5766 | ufshcd_clear_eh_in_progress(hba); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5767 | out: |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5768 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5769 | ufshcd_scsi_unblock_requests(hba); |
| 5770 | ufshcd_release_all(hba); |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 5771 | pm_runtime_put_sync(hba->dev); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5772 | } |
| 5773 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5774 | static void ufshcd_update_uic_reg_hist(struct ufs_uic_err_reg_hist *reg_hist, |
| 5775 | u32 reg) |
| 5776 | { |
| 5777 | reg_hist->reg[reg_hist->pos] = reg; |
| 5778 | reg_hist->tstamp[reg_hist->pos] = ktime_get(); |
| 5779 | reg_hist->pos = (reg_hist->pos + 1) % UIC_ERR_REG_HIST_LENGTH; |
| 5780 | } |
| 5781 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5782 | /** |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5783 | * ufshcd_update_uic_error - check and set fatal UIC error flags. |
| 5784 | * @hba: per-adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5785 | */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5786 | static void ufshcd_update_uic_error(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5787 | { |
| 5788 | u32 reg; |
| 5789 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5790 | /* PHY layer lane error */ |
| 5791 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER); |
| 5792 | /* Ignore LINERESET indication, as this is not an error */ |
| 5793 | if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) && |
| 5794 | (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK)) { |
| 5795 | /* |
| 5796 | * To know whether this error is fatal or not, DB timeout |
| 5797 | * must be checked but this error is handled separately. |
| 5798 | */ |
| 5799 | dev_dbg(hba->dev, "%s: UIC Lane error reported, reg 0x%x\n", |
| 5800 | __func__, reg); |
| 5801 | ufshcd_update_uic_reg_hist(&hba->ufs_stats.pa_err, reg); |
| 5802 | } |
| 5803 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5804 | /* PA_INIT_ERROR is fatal and needs UIC reset */ |
| 5805 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5806 | if (reg) |
| 5807 | ufshcd_update_uic_reg_hist(&hba->ufs_stats.dl_err, reg); |
| 5808 | |
| 5809 | if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT) { |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5810 | hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5811 | } else if (hba->dev_quirks & |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5812 | UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) { |
| 5813 | if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED) |
| 5814 | hba->uic_error |= |
| 5815 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR; |
| 5816 | else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT) |
| 5817 | hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR; |
| 5818 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5819 | |
| 5820 | /* UIC NL/TL/DME errors needs software retry */ |
| 5821 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5822 | if (reg) { |
| 5823 | ufshcd_update_uic_reg_hist(&hba->ufs_stats.nl_err, reg); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5824 | hba->uic_error |= UFSHCD_UIC_NL_ERROR; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5825 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5826 | |
| 5827 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5828 | if (reg) { |
| 5829 | ufshcd_update_uic_reg_hist(&hba->ufs_stats.tl_err, reg); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5830 | hba->uic_error |= UFSHCD_UIC_TL_ERROR; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5831 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5832 | |
| 5833 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5834 | if (reg) { |
| 5835 | ufshcd_update_uic_reg_hist(&hba->ufs_stats.dme_err, reg); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5836 | hba->uic_error |= UFSHCD_UIC_DME_ERROR; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5837 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5838 | |
| 5839 | dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n", |
| 5840 | __func__, hba->uic_error); |
| 5841 | } |
| 5842 | |
| 5843 | /** |
| 5844 | * ufshcd_check_errors - Check for errors that need s/w attention |
| 5845 | * @hba: per-adapter instance |
| 5846 | */ |
| 5847 | static void ufshcd_check_errors(struct ufs_hba *hba) |
| 5848 | { |
| 5849 | bool queue_eh_work = false; |
| 5850 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5851 | if (hba->errors & INT_FATAL_ERRORS || hba->ce_error) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5852 | queue_eh_work = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5853 | |
| 5854 | if (hba->errors & UIC_ERROR) { |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5855 | hba->uic_error = 0; |
| 5856 | ufshcd_update_uic_error(hba); |
| 5857 | if (hba->uic_error) |
| 5858 | queue_eh_work = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5859 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5860 | |
| 5861 | if (queue_eh_work) { |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5862 | /* |
| 5863 | * update the transfer error masks to sticky bits, let's do this |
| 5864 | * irrespective of current ufshcd_state. |
| 5865 | */ |
| 5866 | hba->saved_err |= hba->errors; |
| 5867 | hba->saved_uic_err |= hba->uic_error; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5868 | hba->saved_ce_err |= hba->ce_error; |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5869 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5870 | /* handle fatal errors only when link is functional */ |
| 5871 | if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) { |
| 5872 | /* block commands from scsi mid-layer */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5873 | __ufshcd_scsi_block_requests(hba); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5874 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5875 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 5876 | schedule_work(&hba->eh_work); |
| 5877 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5878 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5879 | /* |
| 5880 | * if (!queue_eh_work) - |
| 5881 | * Other errors are either non-fatal where host recovers |
| 5882 | * itself without s/w intervention or errors that will be |
| 5883 | * handled by the SCSI core layer. |
| 5884 | */ |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5885 | } |
| 5886 | |
| 5887 | /** |
| 5888 | * ufshcd_tmc_handler - handle task management function completion |
| 5889 | * @hba: per adapter instance |
| 5890 | */ |
| 5891 | static void ufshcd_tmc_handler(struct ufs_hba *hba) |
| 5892 | { |
| 5893 | u32 tm_doorbell; |
| 5894 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 5895 | tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5896 | hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5897 | wake_up(&hba->tm_wq); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5898 | } |
| 5899 | |
| 5900 | /** |
| 5901 | * ufshcd_sl_intr - Interrupt service routine |
| 5902 | * @hba: per adapter instance |
| 5903 | * @intr_status: contains interrupts generated by the controller |
| 5904 | */ |
| 5905 | static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status) |
| 5906 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5907 | ufsdbg_error_inject_dispatcher(hba, |
| 5908 | ERR_INJECT_INTR, intr_status, &intr_status); |
| 5909 | |
| 5910 | ufshcd_vops_crypto_engine_get_status(hba, &hba->ce_error); |
| 5911 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5912 | hba->errors = UFSHCD_ERROR_MASK & intr_status; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5913 | if (hba->errors || hba->ce_error) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5914 | ufshcd_check_errors(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5915 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 5916 | if (intr_status & UFSHCD_UIC_MASK) |
| 5917 | ufshcd_uic_cmd_compl(hba, intr_status); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5918 | |
| 5919 | if (intr_status & UTP_TASK_REQ_COMPL) |
| 5920 | ufshcd_tmc_handler(hba); |
| 5921 | |
| 5922 | if (intr_status & UTP_TRANSFER_REQ_COMPL) |
| 5923 | ufshcd_transfer_req_compl(hba); |
| 5924 | } |
| 5925 | |
| 5926 | /** |
| 5927 | * ufshcd_intr - Main interrupt service routine |
| 5928 | * @irq: irq number |
| 5929 | * @__hba: pointer to adapter instance |
| 5930 | * |
| 5931 | * Returns IRQ_HANDLED - If interrupt is valid |
| 5932 | * IRQ_NONE - If invalid interrupt |
| 5933 | */ |
| 5934 | static irqreturn_t ufshcd_intr(int irq, void *__hba) |
| 5935 | { |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 5936 | u32 intr_status, enabled_intr_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5937 | irqreturn_t retval = IRQ_NONE; |
| 5938 | struct ufs_hba *hba = __hba; |
| 5939 | |
| 5940 | spin_lock(hba->host->host_lock); |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 5941 | intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 5942 | enabled_intr_status = |
| 5943 | intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5944 | |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 5945 | if (intr_status) |
Seungwon Jeon | 261ea45 | 2013-06-26 22:39:28 +0530 | [diff] [blame] | 5946 | ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 5947 | |
| 5948 | if (enabled_intr_status) { |
| 5949 | ufshcd_sl_intr(hba, enabled_intr_status); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5950 | retval = IRQ_HANDLED; |
| 5951 | } |
| 5952 | spin_unlock(hba->host->host_lock); |
| 5953 | return retval; |
| 5954 | } |
| 5955 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5956 | static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag) |
| 5957 | { |
| 5958 | int err = 0; |
| 5959 | u32 mask = 1 << tag; |
| 5960 | unsigned long flags; |
| 5961 | |
| 5962 | if (!test_bit(tag, &hba->outstanding_tasks)) |
| 5963 | goto out; |
| 5964 | |
| 5965 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5966 | ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR); |
| 5967 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5968 | |
| 5969 | /* poll for max. 1 sec to clear door bell register by h/w */ |
| 5970 | err = ufshcd_wait_for_register(hba, |
| 5971 | REG_UTP_TASK_REQ_DOOR_BELL, |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 5972 | mask, 0, 1000, 1000, true); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5973 | out: |
| 5974 | return err; |
| 5975 | } |
| 5976 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5977 | /** |
| 5978 | * ufshcd_issue_tm_cmd - issues task management commands to controller |
| 5979 | * @hba: per adapter instance |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5980 | * @lun_id: LUN ID to which TM command is sent |
| 5981 | * @task_id: task ID to which the TM command is applicable |
| 5982 | * @tm_function: task management function opcode |
| 5983 | * @tm_response: task management service response return value |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5984 | * |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5985 | * Returns non-zero value on error, zero on success. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5986 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5987 | static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id, |
| 5988 | u8 tm_function, u8 *tm_response) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5989 | { |
| 5990 | struct utp_task_req_desc *task_req_descp; |
| 5991 | struct utp_upiu_task_req *task_req_upiup; |
| 5992 | struct Scsi_Host *host; |
| 5993 | unsigned long flags; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5994 | int free_slot; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5995 | int err; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5996 | int task_tag; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5997 | |
| 5998 | host = hba->host; |
| 5999 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6000 | /* |
| 6001 | * Get free slot, sleep if slots are unavailable. |
| 6002 | * Even though we use wait_event() which sleeps indefinitely, |
| 6003 | * the maximum wait time is bounded by %TM_CMD_TIMEOUT. |
| 6004 | */ |
| 6005 | wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot)); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6006 | ufshcd_hold_all(hba); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6007 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6008 | spin_lock_irqsave(host->host_lock, flags); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6009 | task_req_descp = hba->utmrdl_base_addr; |
| 6010 | task_req_descp += free_slot; |
| 6011 | |
| 6012 | /* Configure task request descriptor */ |
| 6013 | task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD); |
| 6014 | task_req_descp->header.dword_2 = |
| 6015 | cpu_to_le32(OCS_INVALID_COMMAND_STATUS); |
| 6016 | |
| 6017 | /* Configure task request UPIU */ |
| 6018 | task_req_upiup = |
| 6019 | (struct utp_upiu_task_req *) task_req_descp->task_req_upiu; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6020 | task_tag = hba->nutrs + free_slot; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6021 | task_req_upiup->header.dword_0 = |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 6022 | UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0, |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6023 | lun_id, task_tag); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6024 | task_req_upiup->header.dword_1 = |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 6025 | UPIU_HEADER_DWORD(0, tm_function, 0, 0); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 6026 | /* |
| 6027 | * The host shall provide the same value for LUN field in the basic |
| 6028 | * header and for Input Parameter. |
| 6029 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6030 | task_req_upiup->input_param1 = cpu_to_be32(lun_id); |
| 6031 | task_req_upiup->input_param2 = cpu_to_be32(task_id); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6032 | |
| 6033 | /* send command to the controller */ |
| 6034 | __set_bit(free_slot, &hba->outstanding_tasks); |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 6035 | |
| 6036 | /* Make sure descriptors are ready before ringing the task doorbell */ |
| 6037 | wmb(); |
| 6038 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 6039 | ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6040 | /* Make sure that doorbell is committed immediately */ |
| 6041 | wmb(); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6042 | |
| 6043 | spin_unlock_irqrestore(host->host_lock, flags); |
| 6044 | |
| 6045 | /* wait until the task management command is completed */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6046 | err = wait_event_timeout(hba->tm_wq, |
| 6047 | test_bit(free_slot, &hba->tm_condition), |
| 6048 | msecs_to_jiffies(TM_CMD_TIMEOUT)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6049 | if (!err) { |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6050 | dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n", |
| 6051 | __func__, tm_function); |
| 6052 | if (ufshcd_clear_tm_cmd(hba, free_slot)) |
| 6053 | dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n", |
| 6054 | __func__, free_slot); |
| 6055 | err = -ETIMEDOUT; |
| 6056 | } else { |
| 6057 | err = ufshcd_task_req_compl(hba, free_slot, tm_response); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6058 | } |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6059 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6060 | clear_bit(free_slot, &hba->tm_condition); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6061 | ufshcd_put_tm_slot(hba, free_slot); |
| 6062 | wake_up(&hba->tm_tag_wq); |
| 6063 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6064 | ufshcd_release_all(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6065 | return err; |
| 6066 | } |
| 6067 | |
| 6068 | /** |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6069 | * ufshcd_eh_device_reset_handler - device reset handler registered to |
| 6070 | * scsi layer. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6071 | * @cmd: SCSI command pointer |
| 6072 | * |
| 6073 | * Returns SUCCESS/FAILED |
| 6074 | */ |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6075 | static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6076 | { |
| 6077 | struct Scsi_Host *host; |
| 6078 | struct ufs_hba *hba; |
| 6079 | unsigned int tag; |
| 6080 | u32 pos; |
| 6081 | int err; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6082 | u8 resp = 0xF; |
| 6083 | struct ufshcd_lrb *lrbp; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6084 | unsigned long flags; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6085 | |
| 6086 | host = cmd->device->host; |
| 6087 | hba = shost_priv(host); |
| 6088 | tag = cmd->request->tag; |
| 6089 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6090 | lrbp = &hba->lrb[tag]; |
| 6091 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp); |
| 6092 | if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6093 | if (!err) |
| 6094 | err = resp; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6095 | goto out; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6096 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6097 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6098 | /* clear the commands that were pending for corresponding LUN */ |
| 6099 | for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) { |
| 6100 | if (hba->lrb[pos].lun == lrbp->lun) { |
| 6101 | err = ufshcd_clear_cmd(hba, pos); |
| 6102 | if (err) |
| 6103 | break; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6104 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6105 | } |
| 6106 | spin_lock_irqsave(host->host_lock, flags); |
| 6107 | ufshcd_transfer_req_compl(hba); |
| 6108 | spin_unlock_irqrestore(host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6109 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6110 | out: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6111 | hba->req_abort_count = 0; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6112 | if (!err) { |
| 6113 | err = SUCCESS; |
| 6114 | } else { |
| 6115 | dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); |
| 6116 | err = FAILED; |
| 6117 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6118 | return err; |
| 6119 | } |
| 6120 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6121 | static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap) |
| 6122 | { |
| 6123 | struct ufshcd_lrb *lrbp; |
| 6124 | int tag; |
| 6125 | |
| 6126 | for_each_set_bit(tag, &bitmap, hba->nutrs) { |
| 6127 | lrbp = &hba->lrb[tag]; |
| 6128 | lrbp->req_abort_skip = true; |
| 6129 | } |
| 6130 | } |
| 6131 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6132 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6133 | * ufshcd_abort - abort a specific command |
| 6134 | * @cmd: SCSI command pointer |
| 6135 | * |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6136 | * Abort the pending command in device by sending UFS_ABORT_TASK task management |
| 6137 | * command, and in host controller by clearing the door-bell register. There can |
| 6138 | * be race between controller sending the command to the device while abort is |
| 6139 | * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is |
| 6140 | * really issued and then try to abort it. |
| 6141 | * |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6142 | * Returns SUCCESS/FAILED |
| 6143 | */ |
| 6144 | static int ufshcd_abort(struct scsi_cmnd *cmd) |
| 6145 | { |
| 6146 | struct Scsi_Host *host; |
| 6147 | struct ufs_hba *hba; |
| 6148 | unsigned long flags; |
| 6149 | unsigned int tag; |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6150 | int err = 0; |
| 6151 | int poll_cnt; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6152 | u8 resp = 0xF; |
| 6153 | struct ufshcd_lrb *lrbp; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 6154 | u32 reg; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6155 | |
| 6156 | host = cmd->device->host; |
| 6157 | hba = shost_priv(host); |
| 6158 | tag = cmd->request->tag; |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 6159 | if (!ufshcd_valid_tag(hba, tag)) { |
| 6160 | dev_err(hba->dev, |
| 6161 | "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p", |
| 6162 | __func__, tag, cmd, cmd->request); |
| 6163 | BUG(); |
| 6164 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6165 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6166 | lrbp = &hba->lrb[tag]; |
| 6167 | |
| 6168 | ufshcd_update_error_stats(hba, UFS_ERR_TASK_ABORT); |
| 6169 | |
| 6170 | /* |
| 6171 | * Task abort to the device W-LUN is illegal. When this command |
| 6172 | * will fail, due to spec violation, scsi err handling next step |
| 6173 | * will be to send LU reset which, again, is a spec violation. |
| 6174 | * To avoid these unnecessary/illegal step we skip to the last error |
| 6175 | * handling stage: reset and restore. |
| 6176 | */ |
| 6177 | if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN) |
| 6178 | return ufshcd_eh_host_reset_handler(cmd); |
| 6179 | |
| 6180 | ufshcd_hold_all(hba); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 6181 | reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 6182 | /* If command is already aborted/completed, return SUCCESS */ |
| 6183 | if (!(test_bit(tag, &hba->outstanding_reqs))) { |
| 6184 | dev_err(hba->dev, |
| 6185 | "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n", |
| 6186 | __func__, tag, hba->outstanding_reqs, reg); |
| 6187 | goto out; |
| 6188 | } |
| 6189 | |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 6190 | if (!(reg & (1 << tag))) { |
| 6191 | dev_err(hba->dev, |
| 6192 | "%s: cmd was completed, but without a notifying intr, tag = %d", |
| 6193 | __func__, tag); |
| 6194 | } |
| 6195 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6196 | /* Print Transfer Request of aborted task */ |
| 6197 | dev_err(hba->dev, "%s: Device abort task at tag %d", __func__, tag); |
| 6198 | |
| 6199 | /* |
| 6200 | * Print detailed info about aborted request. |
| 6201 | * As more than one request might get aborted at the same time, |
| 6202 | * print full information only for the first aborted request in order |
| 6203 | * to reduce repeated printouts. For other aborted requests only print |
| 6204 | * basic details. |
| 6205 | */ |
| 6206 | scsi_print_command(cmd); |
| 6207 | if (!hba->req_abort_count) { |
| 6208 | ufshcd_print_host_regs(hba); |
| 6209 | ufshcd_print_host_state(hba); |
| 6210 | ufshcd_print_pwr_info(hba); |
| 6211 | ufshcd_print_trs(hba, 1 << tag, true); |
| 6212 | } else { |
| 6213 | ufshcd_print_trs(hba, 1 << tag, false); |
| 6214 | } |
| 6215 | hba->req_abort_count++; |
| 6216 | |
| 6217 | |
| 6218 | /* Skip task abort in case previous aborts failed and report failure */ |
| 6219 | if (lrbp->req_abort_skip) { |
| 6220 | err = -EIO; |
| 6221 | goto out; |
| 6222 | } |
| 6223 | |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6224 | for (poll_cnt = 100; poll_cnt; poll_cnt--) { |
| 6225 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, |
| 6226 | UFS_QUERY_TASK, &resp); |
| 6227 | if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) { |
| 6228 | /* cmd pending in the device */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6229 | dev_err(hba->dev, "%s: cmd pending in the device. tag = %d", |
| 6230 | __func__, tag); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6231 | break; |
| 6232 | } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6233 | /* |
| 6234 | * cmd not pending in the device, check if it is |
| 6235 | * in transition. |
| 6236 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6237 | dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.", |
| 6238 | __func__, tag); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6239 | reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 6240 | if (reg & (1 << tag)) { |
| 6241 | /* sleep for max. 200us to stabilize */ |
| 6242 | usleep_range(100, 200); |
| 6243 | continue; |
| 6244 | } |
| 6245 | /* command completed already */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6246 | dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.", |
| 6247 | __func__, tag); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6248 | goto out; |
| 6249 | } else { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6250 | dev_err(hba->dev, |
| 6251 | "%s: no response from device. tag = %d, err %d", |
| 6252 | __func__, tag, err); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6253 | if (!err) |
| 6254 | err = resp; /* service response error */ |
| 6255 | goto out; |
| 6256 | } |
| 6257 | } |
| 6258 | |
| 6259 | if (!poll_cnt) { |
| 6260 | err = -EBUSY; |
| 6261 | goto out; |
| 6262 | } |
| 6263 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6264 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, |
| 6265 | UFS_ABORT_TASK, &resp); |
| 6266 | if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6267 | if (!err) { |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6268 | err = resp; /* service response error */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6269 | dev_err(hba->dev, "%s: issued. tag = %d, err %d", |
| 6270 | __func__, tag, err); |
| 6271 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6272 | goto out; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6273 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6274 | |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6275 | err = ufshcd_clear_cmd(hba, tag); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6276 | if (err) { |
| 6277 | dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d", |
| 6278 | __func__, tag, err); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6279 | goto out; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6280 | } |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6281 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6282 | scsi_dma_unmap(cmd); |
| 6283 | |
| 6284 | spin_lock_irqsave(host->host_lock, flags); |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 6285 | ufshcd_outstanding_req_clear(hba, tag); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6286 | hba->lrb[tag].cmd = NULL; |
| 6287 | spin_unlock_irqrestore(host->host_lock, flags); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 6288 | |
| 6289 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 6290 | wake_up(&hba->dev_cmd.tag_wq); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6291 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6292 | out: |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6293 | if (!err) { |
| 6294 | err = SUCCESS; |
| 6295 | } else { |
| 6296 | dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6297 | ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6298 | err = FAILED; |
| 6299 | } |
| 6300 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6301 | /* |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6302 | * This ufshcd_release_all() corresponds to the original scsi cmd that |
| 6303 | * got aborted here (as we won't get any IRQ for it). |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6304 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6305 | ufshcd_release_all(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6306 | return err; |
| 6307 | } |
| 6308 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6309 | /** |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6310 | * ufshcd_host_reset_and_restore - reset and restore host controller |
| 6311 | * @hba: per-adapter instance |
| 6312 | * |
| 6313 | * Note that host controller reset may issue DME_RESET to |
| 6314 | * local and remote (device) Uni-Pro stack and the attributes |
| 6315 | * are reset to default state. |
| 6316 | * |
| 6317 | * Returns zero on success, non-zero on failure |
| 6318 | */ |
| 6319 | static int ufshcd_host_reset_and_restore(struct ufs_hba *hba) |
| 6320 | { |
| 6321 | int err; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6322 | unsigned long flags; |
| 6323 | |
| 6324 | /* Reset the host controller */ |
| 6325 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 6326 | ufshcd_hba_stop(hba, false); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6327 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6328 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6329 | /* scale up clocks to max frequency before full reinitialization */ |
| 6330 | ufshcd_set_clk_freq(hba, true); |
| 6331 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6332 | err = ufshcd_hba_enable(hba); |
| 6333 | if (err) |
| 6334 | goto out; |
| 6335 | |
| 6336 | /* Establish the link again and restore the device */ |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6337 | err = ufshcd_probe_hba(hba); |
| 6338 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6339 | if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL)) { |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6340 | err = -EIO; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6341 | goto out; |
| 6342 | } |
| 6343 | |
| 6344 | if (!err) { |
| 6345 | err = ufshcd_vops_crypto_engine_reset(hba); |
| 6346 | if (err) { |
| 6347 | dev_err(hba->dev, |
| 6348 | "%s: failed to reset crypto engine %d\n", |
| 6349 | __func__, err); |
| 6350 | goto out; |
| 6351 | } |
| 6352 | } |
| 6353 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6354 | out: |
| 6355 | if (err) |
| 6356 | dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err); |
| 6357 | |
| 6358 | return err; |
| 6359 | } |
| 6360 | |
| 6361 | /** |
| 6362 | * ufshcd_reset_and_restore - reset and re-initialize host/device |
| 6363 | * @hba: per-adapter instance |
| 6364 | * |
| 6365 | * Reset and recover device, host and re-establish link. This |
| 6366 | * is helpful to recover the communication in fatal error conditions. |
| 6367 | * |
| 6368 | * Returns zero on success, non-zero on failure |
| 6369 | */ |
| 6370 | static int ufshcd_reset_and_restore(struct ufs_hba *hba) |
| 6371 | { |
| 6372 | int err = 0; |
| 6373 | unsigned long flags; |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6374 | int retries = MAX_HOST_RESET_RETRIES; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6375 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6376 | do { |
| 6377 | err = ufshcd_host_reset_and_restore(hba); |
| 6378 | } while (err && --retries); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6379 | |
| 6380 | /* |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6381 | * There is no point proceeding even after failing |
| 6382 | * to recover after multiple retries. |
| 6383 | */ |
| 6384 | if (err) |
| 6385 | BUG(); |
| 6386 | /* |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6387 | * After reset the door-bell might be cleared, complete |
| 6388 | * outstanding requests in s/w here. |
| 6389 | */ |
| 6390 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 6391 | ufshcd_transfer_req_compl(hba); |
| 6392 | ufshcd_tmc_handler(hba); |
| 6393 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6394 | |
| 6395 | return err; |
| 6396 | } |
| 6397 | |
| 6398 | /** |
| 6399 | * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer |
| 6400 | * @cmd - SCSI command pointer |
| 6401 | * |
| 6402 | * Returns SUCCESS/FAILED |
| 6403 | */ |
| 6404 | static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd) |
| 6405 | { |
| 6406 | int err; |
| 6407 | unsigned long flags; |
| 6408 | struct ufs_hba *hba; |
| 6409 | |
| 6410 | hba = shost_priv(cmd->device->host); |
| 6411 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6412 | ufshcd_hold_all(hba); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6413 | /* |
| 6414 | * Check if there is any race with fatal error handling. |
| 6415 | * If so, wait for it to complete. Even though fatal error |
| 6416 | * handling does reset and restore in some cases, don't assume |
| 6417 | * anything out of it. We are just avoiding race here. |
| 6418 | */ |
| 6419 | do { |
| 6420 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6421 | if (!(work_pending(&hba->eh_work) || |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6422 | hba->ufshcd_state == UFSHCD_STATE_RESET)) |
| 6423 | break; |
| 6424 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6425 | dev_dbg(hba->dev, "%s: reset in progress\n", __func__); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6426 | flush_work(&hba->eh_work); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6427 | } while (1); |
| 6428 | |
| 6429 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 6430 | ufshcd_set_eh_in_progress(hba); |
| 6431 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6432 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6433 | ufshcd_update_error_stats(hba, UFS_ERR_EH); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6434 | err = ufshcd_reset_and_restore(hba); |
| 6435 | |
| 6436 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 6437 | if (!err) { |
| 6438 | err = SUCCESS; |
| 6439 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
| 6440 | } else { |
| 6441 | err = FAILED; |
| 6442 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 6443 | } |
| 6444 | ufshcd_clear_eh_in_progress(hba); |
| 6445 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6446 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6447 | ufshcd_release_all(hba); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6448 | return err; |
| 6449 | } |
| 6450 | |
| 6451 | /** |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6452 | * ufshcd_get_max_icc_level - calculate the ICC level |
| 6453 | * @sup_curr_uA: max. current supported by the regulator |
| 6454 | * @start_scan: row at the desc table to start scan from |
| 6455 | * @buff: power descriptor buffer |
| 6456 | * |
| 6457 | * Returns calculated max ICC level for specific regulator |
| 6458 | */ |
| 6459 | static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff) |
| 6460 | { |
| 6461 | int i; |
| 6462 | int curr_uA; |
| 6463 | u16 data; |
| 6464 | u16 unit; |
| 6465 | |
| 6466 | for (i = start_scan; i >= 0; i--) { |
| 6467 | data = be16_to_cpu(*((u16 *)(buff + 2*i))); |
| 6468 | unit = (data & ATTR_ICC_LVL_UNIT_MASK) >> |
| 6469 | ATTR_ICC_LVL_UNIT_OFFSET; |
| 6470 | curr_uA = data & ATTR_ICC_LVL_VALUE_MASK; |
| 6471 | switch (unit) { |
| 6472 | case UFSHCD_NANO_AMP: |
| 6473 | curr_uA = curr_uA / 1000; |
| 6474 | break; |
| 6475 | case UFSHCD_MILI_AMP: |
| 6476 | curr_uA = curr_uA * 1000; |
| 6477 | break; |
| 6478 | case UFSHCD_AMP: |
| 6479 | curr_uA = curr_uA * 1000 * 1000; |
| 6480 | break; |
| 6481 | case UFSHCD_MICRO_AMP: |
| 6482 | default: |
| 6483 | break; |
| 6484 | } |
| 6485 | if (sup_curr_uA >= curr_uA) |
| 6486 | break; |
| 6487 | } |
| 6488 | if (i < 0) { |
| 6489 | i = 0; |
| 6490 | pr_err("%s: Couldn't find valid icc_level = %d", __func__, i); |
| 6491 | } |
| 6492 | |
| 6493 | return (u32)i; |
| 6494 | } |
| 6495 | |
| 6496 | /** |
Subhash Jadavani | a8d1ba3 | 2016-12-12 18:19:21 -0800 | [diff] [blame] | 6497 | * ufshcd_find_max_sup_active_icc_level - find the max ICC level |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6498 | * In case regulators are not initialized we'll return 0 |
| 6499 | * @hba: per-adapter instance |
| 6500 | * @desc_buf: power descriptor buffer to extract ICC levels from. |
| 6501 | * @len: length of desc_buff |
| 6502 | * |
Subhash Jadavani | a8d1ba3 | 2016-12-12 18:19:21 -0800 | [diff] [blame] | 6503 | * Returns calculated max ICC level |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6504 | */ |
| 6505 | static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba, |
| 6506 | u8 *desc_buf, int len) |
| 6507 | { |
| 6508 | u32 icc_level = 0; |
| 6509 | |
Subhash Jadavani | a8d1ba3 | 2016-12-12 18:19:21 -0800 | [diff] [blame] | 6510 | /* |
| 6511 | * VCCQ rail is optional for removable UFS card and also most of the |
| 6512 | * vendors don't use this rail for embedded UFS devices as well. So |
| 6513 | * it is normal that VCCQ rail may not be provided for given platform. |
| 6514 | */ |
| 6515 | if (!hba->vreg_info.vcc || !hba->vreg_info.vccq2) { |
| 6516 | dev_err(hba->dev, "%s: Regulator capability was not set, bActiveICCLevel=%d\n", |
| 6517 | __func__, icc_level); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6518 | goto out; |
| 6519 | } |
| 6520 | |
| 6521 | if (hba->vreg_info.vcc) |
| 6522 | icc_level = ufshcd_get_max_icc_level( |
| 6523 | hba->vreg_info.vcc->max_uA, |
| 6524 | POWER_DESC_MAX_ACTV_ICC_LVLS - 1, |
| 6525 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]); |
| 6526 | |
| 6527 | if (hba->vreg_info.vccq) |
| 6528 | icc_level = ufshcd_get_max_icc_level( |
| 6529 | hba->vreg_info.vccq->max_uA, |
| 6530 | icc_level, |
| 6531 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]); |
| 6532 | |
| 6533 | if (hba->vreg_info.vccq2) |
| 6534 | icc_level = ufshcd_get_max_icc_level( |
| 6535 | hba->vreg_info.vccq2->max_uA, |
| 6536 | icc_level, |
| 6537 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]); |
| 6538 | out: |
| 6539 | return icc_level; |
| 6540 | } |
| 6541 | |
Subhash Jadavani | 8a93dbd | 2016-12-12 17:59:44 -0800 | [diff] [blame] | 6542 | static void ufshcd_set_active_icc_lvl(struct ufs_hba *hba) |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6543 | { |
| 6544 | int ret; |
| 6545 | int buff_len = QUERY_DESC_POWER_MAX_SIZE; |
| 6546 | u8 desc_buf[QUERY_DESC_POWER_MAX_SIZE]; |
Subhash Jadavani | 35732e5 | 2016-12-09 16:09:42 -0800 | [diff] [blame] | 6547 | u32 icc_level; |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6548 | |
| 6549 | ret = ufshcd_read_power_desc(hba, desc_buf, buff_len); |
| 6550 | if (ret) { |
| 6551 | dev_err(hba->dev, |
| 6552 | "%s: Failed reading power descriptor.len = %d ret = %d", |
| 6553 | __func__, buff_len, ret); |
| 6554 | return; |
| 6555 | } |
| 6556 | |
Subhash Jadavani | 35732e5 | 2016-12-09 16:09:42 -0800 | [diff] [blame] | 6557 | icc_level = ufshcd_find_max_sup_active_icc_level(hba, desc_buf, |
| 6558 | buff_len); |
| 6559 | dev_dbg(hba->dev, "%s: setting icc_level 0x%x", __func__, icc_level); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6560 | |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 6561 | ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
Subhash Jadavani | 35732e5 | 2016-12-09 16:09:42 -0800 | [diff] [blame] | 6562 | QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, &icc_level); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6563 | |
| 6564 | if (ret) |
| 6565 | dev_err(hba->dev, |
| 6566 | "%s: Failed configuring bActiveICCLevel = %d ret = %d", |
Subhash Jadavani | 35732e5 | 2016-12-09 16:09:42 -0800 | [diff] [blame] | 6567 | __func__, icc_level, ret); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6568 | } |
| 6569 | |
| 6570 | /** |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6571 | * ufshcd_scsi_add_wlus - Adds required W-LUs |
| 6572 | * @hba: per-adapter instance |
| 6573 | * |
| 6574 | * UFS device specification requires the UFS devices to support 4 well known |
| 6575 | * logical units: |
| 6576 | * "REPORT_LUNS" (address: 01h) |
| 6577 | * "UFS Device" (address: 50h) |
| 6578 | * "RPMB" (address: 44h) |
| 6579 | * "BOOT" (address: 30h) |
| 6580 | * UFS device's power management needs to be controlled by "POWER CONDITION" |
| 6581 | * field of SSU (START STOP UNIT) command. But this "power condition" field |
| 6582 | * will take effect only when its sent to "UFS device" well known logical unit |
| 6583 | * hence we require the scsi_device instance to represent this logical unit in |
| 6584 | * order for the UFS host driver to send the SSU command for power management. |
| 6585 | |
| 6586 | * We also require the scsi_device instance for "RPMB" (Replay Protected Memory |
| 6587 | * Block) LU so user space process can control this LU. User space may also |
| 6588 | * want to have access to BOOT LU. |
| 6589 | |
| 6590 | * This function adds scsi device instances for each of all well known LUs |
| 6591 | * (except "REPORT LUNS" LU). |
| 6592 | * |
| 6593 | * Returns zero on success (all required W-LUs are added successfully), |
| 6594 | * non-zero error value on failure (if failed to add any of the required W-LU). |
| 6595 | */ |
| 6596 | static int ufshcd_scsi_add_wlus(struct ufs_hba *hba) |
| 6597 | { |
| 6598 | int ret = 0; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6599 | struct scsi_device *sdev_rpmb; |
| 6600 | struct scsi_device *sdev_boot; |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6601 | |
| 6602 | hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0, |
| 6603 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL); |
| 6604 | if (IS_ERR(hba->sdev_ufs_device)) { |
| 6605 | ret = PTR_ERR(hba->sdev_ufs_device); |
| 6606 | hba->sdev_ufs_device = NULL; |
| 6607 | goto out; |
| 6608 | } |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6609 | scsi_device_put(hba->sdev_ufs_device); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6610 | |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6611 | sdev_boot = __scsi_add_device(hba->host, 0, 0, |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6612 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL); |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6613 | if (IS_ERR(sdev_boot)) { |
| 6614 | ret = PTR_ERR(sdev_boot); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6615 | goto remove_sdev_ufs_device; |
| 6616 | } |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6617 | scsi_device_put(sdev_boot); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6618 | |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6619 | sdev_rpmb = __scsi_add_device(hba->host, 0, 0, |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6620 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL); |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6621 | if (IS_ERR(sdev_rpmb)) { |
| 6622 | ret = PTR_ERR(sdev_rpmb); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6623 | goto remove_sdev_boot; |
| 6624 | } |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6625 | scsi_device_put(sdev_rpmb); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6626 | goto out; |
| 6627 | |
| 6628 | remove_sdev_boot: |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6629 | scsi_remove_device(sdev_boot); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6630 | remove_sdev_ufs_device: |
| 6631 | scsi_remove_device(hba->sdev_ufs_device); |
| 6632 | out: |
| 6633 | return ret; |
| 6634 | } |
| 6635 | |
| 6636 | /** |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 6637 | * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro |
| 6638 | * @hba: per-adapter instance |
| 6639 | * |
| 6640 | * PA_TActivate parameter can be tuned manually if UniPro version is less than |
| 6641 | * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's |
| 6642 | * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce |
| 6643 | * the hibern8 exit latency. |
| 6644 | * |
| 6645 | * Returns zero on success, non-zero error value on failure. |
| 6646 | */ |
| 6647 | static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba) |
| 6648 | { |
| 6649 | int ret = 0; |
| 6650 | u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate; |
| 6651 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6652 | if (!ufshcd_is_unipro_pa_params_tuning_req(hba)) |
| 6653 | return 0; |
| 6654 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 6655 | ret = ufshcd_dme_peer_get(hba, |
| 6656 | UIC_ARG_MIB_SEL( |
| 6657 | RX_MIN_ACTIVATETIME_CAPABILITY, |
| 6658 | UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), |
| 6659 | &peer_rx_min_activatetime); |
| 6660 | if (ret) |
| 6661 | goto out; |
| 6662 | |
| 6663 | /* make sure proper unit conversion is applied */ |
| 6664 | tuned_pa_tactivate = |
| 6665 | ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US) |
| 6666 | / PA_TACTIVATE_TIME_UNIT_US); |
| 6667 | ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 6668 | tuned_pa_tactivate); |
| 6669 | |
| 6670 | out: |
| 6671 | return ret; |
| 6672 | } |
| 6673 | |
| 6674 | /** |
| 6675 | * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro |
| 6676 | * @hba: per-adapter instance |
| 6677 | * |
| 6678 | * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than |
| 6679 | * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's |
| 6680 | * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY. |
| 6681 | * This optimal value can help reduce the hibern8 exit latency. |
| 6682 | * |
| 6683 | * Returns zero on success, non-zero error value on failure. |
| 6684 | */ |
| 6685 | static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba) |
| 6686 | { |
| 6687 | int ret = 0; |
| 6688 | u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0; |
| 6689 | u32 max_hibern8_time, tuned_pa_hibern8time; |
| 6690 | |
| 6691 | ret = ufshcd_dme_get(hba, |
| 6692 | UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY, |
| 6693 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), |
| 6694 | &local_tx_hibern8_time_cap); |
| 6695 | if (ret) |
| 6696 | goto out; |
| 6697 | |
| 6698 | ret = ufshcd_dme_peer_get(hba, |
| 6699 | UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY, |
| 6700 | UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), |
| 6701 | &peer_rx_hibern8_time_cap); |
| 6702 | if (ret) |
| 6703 | goto out; |
| 6704 | |
| 6705 | max_hibern8_time = max(local_tx_hibern8_time_cap, |
| 6706 | peer_rx_hibern8_time_cap); |
| 6707 | /* make sure proper unit conversion is applied */ |
| 6708 | tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US) |
| 6709 | / PA_HIBERN8_TIME_UNIT_US); |
| 6710 | ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME), |
| 6711 | tuned_pa_hibern8time); |
| 6712 | out: |
| 6713 | return ret; |
| 6714 | } |
| 6715 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6716 | /** |
| 6717 | * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is |
| 6718 | * less than device PA_TACTIVATE time. |
| 6719 | * @hba: per-adapter instance |
| 6720 | * |
| 6721 | * Some UFS devices require host PA_TACTIVATE to be lower than device |
| 6722 | * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk |
| 6723 | * for such devices. |
| 6724 | * |
| 6725 | * Returns zero on success, non-zero error value on failure. |
| 6726 | */ |
| 6727 | static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba) |
| 6728 | { |
| 6729 | int ret = 0; |
| 6730 | u32 granularity, peer_granularity; |
| 6731 | u32 pa_tactivate, peer_pa_tactivate; |
| 6732 | u32 pa_tactivate_us, peer_pa_tactivate_us; |
| 6733 | u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100}; |
| 6734 | |
| 6735 | ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY), |
| 6736 | &granularity); |
| 6737 | if (ret) |
| 6738 | goto out; |
| 6739 | |
| 6740 | ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY), |
| 6741 | &peer_granularity); |
| 6742 | if (ret) |
| 6743 | goto out; |
| 6744 | |
| 6745 | if ((granularity < PA_GRANULARITY_MIN_VAL) || |
| 6746 | (granularity > PA_GRANULARITY_MAX_VAL)) { |
| 6747 | dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d", |
| 6748 | __func__, granularity); |
| 6749 | return -EINVAL; |
| 6750 | } |
| 6751 | |
| 6752 | if ((peer_granularity < PA_GRANULARITY_MIN_VAL) || |
| 6753 | (peer_granularity > PA_GRANULARITY_MAX_VAL)) { |
| 6754 | dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d", |
| 6755 | __func__, peer_granularity); |
| 6756 | return -EINVAL; |
| 6757 | } |
| 6758 | |
| 6759 | ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate); |
| 6760 | if (ret) |
| 6761 | goto out; |
| 6762 | |
| 6763 | ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 6764 | &peer_pa_tactivate); |
| 6765 | if (ret) |
| 6766 | goto out; |
| 6767 | |
| 6768 | pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1]; |
| 6769 | peer_pa_tactivate_us = peer_pa_tactivate * |
| 6770 | gran_to_us_table[peer_granularity - 1]; |
| 6771 | |
| 6772 | if (pa_tactivate_us > peer_pa_tactivate_us) { |
| 6773 | u32 new_peer_pa_tactivate; |
| 6774 | |
| 6775 | new_peer_pa_tactivate = pa_tactivate_us / |
| 6776 | gran_to_us_table[peer_granularity - 1]; |
| 6777 | new_peer_pa_tactivate++; |
| 6778 | ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 6779 | new_peer_pa_tactivate); |
| 6780 | } |
| 6781 | |
| 6782 | out: |
| 6783 | return ret; |
| 6784 | } |
| 6785 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 6786 | static void ufshcd_tune_unipro_params(struct ufs_hba *hba) |
| 6787 | { |
| 6788 | if (ufshcd_is_unipro_pa_params_tuning_req(hba)) { |
| 6789 | ufshcd_tune_pa_tactivate(hba); |
| 6790 | ufshcd_tune_pa_hibern8time(hba); |
| 6791 | } |
| 6792 | |
| 6793 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE) |
| 6794 | /* set 1ms timeout for PA_TACTIVATE */ |
| 6795 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6796 | |
| 6797 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE) |
| 6798 | ufshcd_quirk_tune_host_pa_tactivate(hba); |
| 6799 | |
| 6800 | ufshcd_vops_apply_dev_quirks(hba); |
| 6801 | } |
| 6802 | |
| 6803 | static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba) |
| 6804 | { |
| 6805 | int err_reg_hist_size = sizeof(struct ufs_uic_err_reg_hist); |
| 6806 | |
| 6807 | hba->ufs_stats.hibern8_exit_cnt = 0; |
| 6808 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); |
| 6809 | |
| 6810 | memset(&hba->ufs_stats.pa_err, 0, err_reg_hist_size); |
| 6811 | memset(&hba->ufs_stats.dl_err, 0, err_reg_hist_size); |
| 6812 | memset(&hba->ufs_stats.nl_err, 0, err_reg_hist_size); |
| 6813 | memset(&hba->ufs_stats.tl_err, 0, err_reg_hist_size); |
| 6814 | memset(&hba->ufs_stats.dme_err, 0, err_reg_hist_size); |
| 6815 | |
| 6816 | hba->req_abort_count = 0; |
| 6817 | } |
| 6818 | |
| 6819 | static void ufshcd_apply_pm_quirks(struct ufs_hba *hba) |
| 6820 | { |
| 6821 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_NO_LINK_OFF) { |
| 6822 | if (ufs_get_pm_lvl_to_link_pwr_state(hba->rpm_lvl) == |
| 6823 | UIC_LINK_OFF_STATE) { |
| 6824 | hba->rpm_lvl = |
| 6825 | ufs_get_desired_pm_lvl_for_dev_link_state( |
| 6826 | UFS_SLEEP_PWR_MODE, |
| 6827 | UIC_LINK_HIBERN8_STATE); |
| 6828 | dev_info(hba->dev, "UFS_DEVICE_QUIRK_NO_LINK_OFF enabled, changed rpm_lvl to %d\n", |
| 6829 | hba->rpm_lvl); |
| 6830 | } |
| 6831 | if (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) == |
| 6832 | UIC_LINK_OFF_STATE) { |
| 6833 | hba->spm_lvl = |
| 6834 | ufs_get_desired_pm_lvl_for_dev_link_state( |
| 6835 | UFS_SLEEP_PWR_MODE, |
| 6836 | UIC_LINK_HIBERN8_STATE); |
| 6837 | dev_info(hba->dev, "UFS_DEVICE_QUIRK_NO_LINK_OFF enabled, changed spm_lvl to %d\n", |
| 6838 | hba->spm_lvl); |
| 6839 | } |
| 6840 | } |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 6841 | } |
| 6842 | |
| 6843 | /** |
Subhash Jadavani | 88f9999 | 2016-12-13 15:52:21 -0800 | [diff] [blame^] | 6844 | * ufshcd_set_dev_ref_clk - set the device bRefClkFreq |
| 6845 | * @hba: per-adapter instance |
| 6846 | * |
| 6847 | * Read the current value of the bRefClkFreq attribute from device and update it |
| 6848 | * if host is supplying different reference clock frequency than one mentioned |
| 6849 | * in bRefClkFreq attribute. |
| 6850 | * |
| 6851 | * Returns zero on success, non-zero error value on failure. |
| 6852 | */ |
| 6853 | static int ufshcd_set_dev_ref_clk(struct ufs_hba *hba) |
| 6854 | { |
| 6855 | int err = 0; |
| 6856 | int ref_clk = -1; |
| 6857 | static const char * const ref_clk_freqs[] = {"19.2 MHz", "26 MHz", |
| 6858 | "38.4 MHz", "52 MHz"}; |
| 6859 | |
| 6860 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
| 6861 | QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &ref_clk); |
| 6862 | |
| 6863 | if (err) { |
| 6864 | dev_err(hba->dev, "%s: failed reading bRefClkFreq. err = %d\n", |
| 6865 | __func__, err); |
| 6866 | goto out; |
| 6867 | } |
| 6868 | |
| 6869 | if ((ref_clk < 0) || (ref_clk > REF_CLK_FREQ_52_MHZ)) { |
| 6870 | dev_err(hba->dev, "%s: invalid ref_clk setting = %d\n", |
| 6871 | __func__, ref_clk); |
| 6872 | err = -EINVAL; |
| 6873 | goto out; |
| 6874 | } |
| 6875 | |
| 6876 | if (ref_clk == hba->dev_ref_clk_freq) |
| 6877 | goto out; /* nothing to update */ |
| 6878 | |
| 6879 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
| 6880 | QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, |
| 6881 | &hba->dev_ref_clk_freq); |
| 6882 | |
| 6883 | if (err) |
| 6884 | dev_err(hba->dev, "%s: bRefClkFreq setting to %s failed\n", |
| 6885 | __func__, ref_clk_freqs[hba->dev_ref_clk_freq]); |
| 6886 | else |
| 6887 | /* |
| 6888 | * It is good to print this out here to debug any later failures |
| 6889 | * related to gear switch. |
| 6890 | */ |
| 6891 | dev_info(hba->dev, "%s: bRefClkFreq setting to %s succeeded\n", |
| 6892 | __func__, ref_clk_freqs[hba->dev_ref_clk_freq]); |
| 6893 | |
| 6894 | out: |
| 6895 | return err; |
| 6896 | } |
| 6897 | |
| 6898 | /** |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6899 | * ufshcd_probe_hba - probe hba to detect device and initialize |
| 6900 | * @hba: per-adapter instance |
| 6901 | * |
| 6902 | * Execute link-startup and verify device initialization |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6903 | */ |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6904 | static int ufshcd_probe_hba(struct ufs_hba *hba) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6905 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6906 | int ret; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6907 | ktime_t start = ktime_get(); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6908 | |
| 6909 | ret = ufshcd_link_startup(hba); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 6910 | if (ret) |
| 6911 | goto out; |
| 6912 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6913 | /* Enable auto hibern8 if supported */ |
| 6914 | if (ufshcd_is_auto_hibern8_supported(hba)) |
| 6915 | ufshcd_set_auto_hibern8_timer(hba, |
| 6916 | hba->hibern8_on_idle.delay_ms); |
Yaniv Gardi | 5064636 | 2014-10-23 13:25:13 +0300 | [diff] [blame] | 6917 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6918 | /* Debug counters initialization */ |
| 6919 | ufshcd_clear_dbg_ufs_stats(hba); |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 6920 | /* set the default level for urgent bkops */ |
| 6921 | hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT; |
| 6922 | hba->is_urgent_bkops_lvl_checked = false; |
| 6923 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6924 | /* UniPro link is active now */ |
| 6925 | ufshcd_set_link_active(hba); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 6926 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 6927 | ret = ufshcd_verify_dev_init(hba); |
| 6928 | if (ret) |
| 6929 | goto out; |
| 6930 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 6931 | ret = ufshcd_complete_dev_init(hba); |
| 6932 | if (ret) |
| 6933 | goto out; |
| 6934 | |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6935 | ufs_advertise_fixup_device(hba); |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 6936 | ufshcd_tune_unipro_params(hba); |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 6937 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6938 | ufshcd_apply_pm_quirks(hba); |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 6939 | ret = ufshcd_set_vccq_rail_unused(hba, |
| 6940 | (hba->dev_quirks & UFS_DEVICE_NO_VCCQ) ? true : false); |
| 6941 | if (ret) |
| 6942 | goto out; |
| 6943 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6944 | /* UFS device is also active now */ |
| 6945 | ufshcd_set_ufs_dev_active(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 6946 | ufshcd_force_reset_auto_bkops(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6947 | hba->wlun_dev_clr_ua = true; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6948 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 6949 | if (ufshcd_get_max_pwr_mode(hba)) { |
| 6950 | dev_err(hba->dev, |
| 6951 | "%s: Failed getting max supported power mode\n", |
| 6952 | __func__); |
| 6953 | } else { |
Subhash Jadavani | 88f9999 | 2016-12-13 15:52:21 -0800 | [diff] [blame^] | 6954 | /* |
| 6955 | * Set the right value to bRefClkFreq before attempting to |
| 6956 | * switch to HS gears. |
| 6957 | */ |
| 6958 | ufshcd_set_dev_ref_clk(hba); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 6959 | ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6960 | if (ret) { |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 6961 | dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n", |
| 6962 | __func__, ret); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6963 | goto out; |
| 6964 | } |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 6965 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6966 | |
Subhash Jadavani | 8a93dbd | 2016-12-12 17:59:44 -0800 | [diff] [blame] | 6967 | /* |
| 6968 | * bActiveICCLevel is volatile for UFS device (as per latest v2.1 spec) |
| 6969 | * and for removable UFS card as well, hence always set the parameter. |
| 6970 | * Note: Error handler may issue the device reset hence resetting |
| 6971 | * bActiveICCLevel as well so it is always safe to set this here. |
| 6972 | */ |
| 6973 | ufshcd_set_active_icc_lvl(hba); |
| 6974 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 6975 | /* set the state as operational after switching to desired gear */ |
| 6976 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6977 | /* |
| 6978 | * If we are in error handling context or in power management callbacks |
| 6979 | * context, no need to scan the host |
| 6980 | */ |
| 6981 | if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) { |
| 6982 | bool flag; |
| 6983 | |
| 6984 | /* clear any previous UFS device information */ |
| 6985 | memset(&hba->dev_info, 0, sizeof(hba->dev_info)); |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 6986 | if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG, |
| 6987 | QUERY_FLAG_IDN_PWR_ON_WPE, &flag)) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6988 | hba->dev_info.f_power_on_wp_en = flag; |
| 6989 | |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6990 | /* Add required well known logical units to scsi mid layer */ |
| 6991 | if (ufshcd_scsi_add_wlus(hba)) |
| 6992 | goto out; |
| 6993 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6994 | scsi_scan_host(hba->host); |
| 6995 | pm_runtime_put_sync(hba->dev); |
| 6996 | } |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6997 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6998 | /* Resume devfreq after UFS device is detected */ |
| 6999 | if (ufshcd_is_clkscaling_supported(hba)) { |
| 7000 | memcpy(&hba->clk_scaling.saved_pwr_info.info, &hba->pwr_info, |
| 7001 | sizeof(struct ufs_pa_layer_attr)); |
| 7002 | hba->clk_scaling.saved_pwr_info.is_valid = true; |
| 7003 | hba->clk_scaling.is_scaled_up = true; |
| 7004 | ufshcd_resume_clkscaling(hba); |
| 7005 | hba->clk_scaling.is_allowed = true; |
| 7006 | } |
| 7007 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 7008 | out: |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7009 | /* |
| 7010 | * If we failed to initialize the device or the device is not |
| 7011 | * present, turn off the power/clocks etc. |
| 7012 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7013 | if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) { |
| 7014 | pm_runtime_put_sync(hba->dev); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7015 | ufshcd_hba_exit(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7016 | } |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7017 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7018 | trace_ufshcd_init(dev_name(hba->dev), ret, |
| 7019 | ktime_to_us(ktime_sub(ktime_get(), start)), |
| 7020 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7021 | return ret; |
| 7022 | } |
| 7023 | |
| 7024 | /** |
| 7025 | * ufshcd_async_scan - asynchronous execution for probing hba |
| 7026 | * @data: data pointer to pass to this function |
| 7027 | * @cookie: cookie data |
| 7028 | */ |
| 7029 | static void ufshcd_async_scan(void *data, async_cookie_t cookie) |
| 7030 | { |
| 7031 | struct ufs_hba *hba = (struct ufs_hba *)data; |
| 7032 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7033 | /* |
| 7034 | * Don't allow clock gating and hibern8 enter for faster device |
| 7035 | * detection. |
| 7036 | */ |
| 7037 | ufshcd_hold_all(hba); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7038 | ufshcd_probe_hba(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7039 | ufshcd_release_all(hba); |
| 7040 | } |
| 7041 | |
| 7042 | /** |
| 7043 | * ufshcd_query_ioctl - perform user read queries |
| 7044 | * @hba: per-adapter instance |
| 7045 | * @lun: used for lun specific queries |
| 7046 | * @buffer: user space buffer for reading and submitting query data and params |
| 7047 | * @return: 0 for success negative error code otherwise |
| 7048 | * |
| 7049 | * Expected/Submitted buffer structure is struct ufs_ioctl_query_data. |
| 7050 | * It will read the opcode, idn and buf_length parameters, and, put the |
| 7051 | * response in the buffer field while updating the used size in buf_length. |
| 7052 | */ |
| 7053 | static int ufshcd_query_ioctl(struct ufs_hba *hba, u8 lun, void __user *buffer) |
| 7054 | { |
| 7055 | struct ufs_ioctl_query_data *ioctl_data; |
| 7056 | int err = 0; |
| 7057 | int length = 0; |
| 7058 | void *data_ptr; |
| 7059 | bool flag; |
| 7060 | u32 att; |
| 7061 | u8 index; |
| 7062 | u8 *desc = NULL; |
| 7063 | |
| 7064 | ioctl_data = kzalloc(sizeof(struct ufs_ioctl_query_data), GFP_KERNEL); |
| 7065 | if (!ioctl_data) { |
| 7066 | dev_err(hba->dev, "%s: Failed allocating %zu bytes\n", __func__, |
| 7067 | sizeof(struct ufs_ioctl_query_data)); |
| 7068 | err = -ENOMEM; |
| 7069 | goto out; |
| 7070 | } |
| 7071 | |
| 7072 | /* extract params from user buffer */ |
| 7073 | err = copy_from_user(ioctl_data, buffer, |
| 7074 | sizeof(struct ufs_ioctl_query_data)); |
| 7075 | if (err) { |
| 7076 | dev_err(hba->dev, |
| 7077 | "%s: Failed copying buffer from user, err %d\n", |
| 7078 | __func__, err); |
| 7079 | goto out_release_mem; |
| 7080 | } |
| 7081 | |
| 7082 | /* verify legal parameters & send query */ |
| 7083 | switch (ioctl_data->opcode) { |
| 7084 | case UPIU_QUERY_OPCODE_READ_DESC: |
| 7085 | switch (ioctl_data->idn) { |
| 7086 | case QUERY_DESC_IDN_DEVICE: |
| 7087 | case QUERY_DESC_IDN_CONFIGURAION: |
| 7088 | case QUERY_DESC_IDN_INTERCONNECT: |
| 7089 | case QUERY_DESC_IDN_GEOMETRY: |
| 7090 | case QUERY_DESC_IDN_POWER: |
| 7091 | index = 0; |
| 7092 | break; |
| 7093 | case QUERY_DESC_IDN_UNIT: |
| 7094 | if (!ufs_is_valid_unit_desc_lun(lun)) { |
| 7095 | dev_err(hba->dev, |
| 7096 | "%s: No unit descriptor for lun 0x%x\n", |
| 7097 | __func__, lun); |
| 7098 | err = -EINVAL; |
| 7099 | goto out_release_mem; |
| 7100 | } |
| 7101 | index = lun; |
| 7102 | break; |
| 7103 | default: |
| 7104 | goto out_einval; |
| 7105 | } |
| 7106 | length = min_t(int, QUERY_DESC_MAX_SIZE, |
| 7107 | ioctl_data->buf_size); |
| 7108 | desc = kzalloc(length, GFP_KERNEL); |
| 7109 | if (!desc) { |
| 7110 | dev_err(hba->dev, "%s: Failed allocating %d bytes\n", |
| 7111 | __func__, length); |
| 7112 | err = -ENOMEM; |
| 7113 | goto out_release_mem; |
| 7114 | } |
| 7115 | err = ufshcd_query_descriptor(hba, ioctl_data->opcode, |
| 7116 | ioctl_data->idn, index, 0, desc, &length); |
| 7117 | break; |
| 7118 | case UPIU_QUERY_OPCODE_READ_ATTR: |
| 7119 | switch (ioctl_data->idn) { |
| 7120 | case QUERY_ATTR_IDN_BOOT_LU_EN: |
| 7121 | case QUERY_ATTR_IDN_POWER_MODE: |
| 7122 | case QUERY_ATTR_IDN_ACTIVE_ICC_LVL: |
| 7123 | case QUERY_ATTR_IDN_OOO_DATA_EN: |
| 7124 | case QUERY_ATTR_IDN_BKOPS_STATUS: |
| 7125 | case QUERY_ATTR_IDN_PURGE_STATUS: |
| 7126 | case QUERY_ATTR_IDN_MAX_DATA_IN: |
| 7127 | case QUERY_ATTR_IDN_MAX_DATA_OUT: |
| 7128 | case QUERY_ATTR_IDN_REF_CLK_FREQ: |
| 7129 | case QUERY_ATTR_IDN_CONF_DESC_LOCK: |
| 7130 | case QUERY_ATTR_IDN_MAX_NUM_OF_RTT: |
| 7131 | case QUERY_ATTR_IDN_EE_CONTROL: |
| 7132 | case QUERY_ATTR_IDN_EE_STATUS: |
| 7133 | case QUERY_ATTR_IDN_SECONDS_PASSED: |
| 7134 | index = 0; |
| 7135 | break; |
| 7136 | case QUERY_ATTR_IDN_DYN_CAP_NEEDED: |
| 7137 | case QUERY_ATTR_IDN_CORR_PRG_BLK_NUM: |
| 7138 | index = lun; |
| 7139 | break; |
| 7140 | default: |
| 7141 | goto out_einval; |
| 7142 | } |
| 7143 | err = ufshcd_query_attr(hba, ioctl_data->opcode, ioctl_data->idn, |
| 7144 | index, 0, &att); |
| 7145 | break; |
| 7146 | |
| 7147 | case UPIU_QUERY_OPCODE_WRITE_ATTR: |
| 7148 | err = copy_from_user(&att, |
| 7149 | buffer + sizeof(struct ufs_ioctl_query_data), |
| 7150 | sizeof(u32)); |
| 7151 | if (err) { |
| 7152 | dev_err(hba->dev, |
| 7153 | "%s: Failed copying buffer from user, err %d\n", |
| 7154 | __func__, err); |
| 7155 | goto out_release_mem; |
| 7156 | } |
| 7157 | |
| 7158 | switch (ioctl_data->idn) { |
| 7159 | case QUERY_ATTR_IDN_BOOT_LU_EN: |
| 7160 | index = 0; |
| 7161 | if (att > QUERY_ATTR_IDN_BOOT_LU_EN_MAX) { |
| 7162 | dev_err(hba->dev, |
| 7163 | "%s: Illegal ufs query ioctl data, opcode 0x%x, idn 0x%x, att 0x%x\n", |
| 7164 | __func__, ioctl_data->opcode, |
| 7165 | (unsigned int)ioctl_data->idn, att); |
| 7166 | err = -EINVAL; |
| 7167 | goto out_release_mem; |
| 7168 | } |
| 7169 | break; |
| 7170 | default: |
| 7171 | goto out_einval; |
| 7172 | } |
| 7173 | err = ufshcd_query_attr(hba, ioctl_data->opcode, |
| 7174 | ioctl_data->idn, index, 0, &att); |
| 7175 | break; |
| 7176 | |
| 7177 | case UPIU_QUERY_OPCODE_READ_FLAG: |
| 7178 | switch (ioctl_data->idn) { |
| 7179 | case QUERY_FLAG_IDN_FDEVICEINIT: |
| 7180 | case QUERY_FLAG_IDN_PERMANENT_WPE: |
| 7181 | case QUERY_FLAG_IDN_PWR_ON_WPE: |
| 7182 | case QUERY_FLAG_IDN_BKOPS_EN: |
| 7183 | case QUERY_FLAG_IDN_PURGE_ENABLE: |
| 7184 | case QUERY_FLAG_IDN_FPHYRESOURCEREMOVAL: |
| 7185 | case QUERY_FLAG_IDN_BUSY_RTC: |
| 7186 | break; |
| 7187 | default: |
| 7188 | goto out_einval; |
| 7189 | } |
| 7190 | err = ufshcd_query_flag_retry(hba, ioctl_data->opcode, |
| 7191 | ioctl_data->idn, &flag); |
| 7192 | break; |
| 7193 | default: |
| 7194 | goto out_einval; |
| 7195 | } |
| 7196 | |
| 7197 | if (err) { |
| 7198 | dev_err(hba->dev, "%s: Query for idn %d failed\n", __func__, |
| 7199 | ioctl_data->idn); |
| 7200 | goto out_release_mem; |
| 7201 | } |
| 7202 | |
| 7203 | /* |
| 7204 | * copy response data |
| 7205 | * As we might end up reading less data then what is specified in |
| 7206 | * "ioctl_data->buf_size". So we are updating "ioctl_data-> |
| 7207 | * buf_size" to what exactly we have read. |
| 7208 | */ |
| 7209 | switch (ioctl_data->opcode) { |
| 7210 | case UPIU_QUERY_OPCODE_READ_DESC: |
| 7211 | ioctl_data->buf_size = min_t(int, ioctl_data->buf_size, length); |
| 7212 | data_ptr = desc; |
| 7213 | break; |
| 7214 | case UPIU_QUERY_OPCODE_READ_ATTR: |
| 7215 | ioctl_data->buf_size = sizeof(u32); |
| 7216 | data_ptr = &att; |
| 7217 | break; |
| 7218 | case UPIU_QUERY_OPCODE_READ_FLAG: |
| 7219 | ioctl_data->buf_size = 1; |
| 7220 | data_ptr = &flag; |
| 7221 | break; |
| 7222 | case UPIU_QUERY_OPCODE_WRITE_ATTR: |
| 7223 | goto out_release_mem; |
| 7224 | default: |
| 7225 | goto out_einval; |
| 7226 | } |
| 7227 | |
| 7228 | /* copy to user */ |
| 7229 | err = copy_to_user(buffer, ioctl_data, |
| 7230 | sizeof(struct ufs_ioctl_query_data)); |
| 7231 | if (err) |
| 7232 | dev_err(hba->dev, "%s: Failed copying back to user.\n", |
| 7233 | __func__); |
| 7234 | err = copy_to_user(buffer + sizeof(struct ufs_ioctl_query_data), |
| 7235 | data_ptr, ioctl_data->buf_size); |
| 7236 | if (err) |
| 7237 | dev_err(hba->dev, "%s: err %d copying back to user.\n", |
| 7238 | __func__, err); |
| 7239 | goto out_release_mem; |
| 7240 | |
| 7241 | out_einval: |
| 7242 | dev_err(hba->dev, |
| 7243 | "%s: illegal ufs query ioctl data, opcode 0x%x, idn 0x%x\n", |
| 7244 | __func__, ioctl_data->opcode, (unsigned int)ioctl_data->idn); |
| 7245 | err = -EINVAL; |
| 7246 | out_release_mem: |
| 7247 | kfree(ioctl_data); |
| 7248 | kfree(desc); |
| 7249 | out: |
| 7250 | return err; |
| 7251 | } |
| 7252 | |
| 7253 | /** |
| 7254 | * ufshcd_ioctl - ufs ioctl callback registered in scsi_host |
| 7255 | * @dev: scsi device required for per LUN queries |
| 7256 | * @cmd: command opcode |
| 7257 | * @buffer: user space buffer for transferring data |
| 7258 | * |
| 7259 | * Supported commands: |
| 7260 | * UFS_IOCTL_QUERY |
| 7261 | */ |
| 7262 | static int ufshcd_ioctl(struct scsi_device *dev, int cmd, void __user *buffer) |
| 7263 | { |
| 7264 | struct ufs_hba *hba = shost_priv(dev->host); |
| 7265 | int err = 0; |
| 7266 | |
| 7267 | BUG_ON(!hba); |
| 7268 | if (!buffer) { |
| 7269 | dev_err(hba->dev, "%s: User buffer is NULL!\n", __func__); |
| 7270 | return -EINVAL; |
| 7271 | } |
| 7272 | |
| 7273 | switch (cmd) { |
| 7274 | case UFS_IOCTL_QUERY: |
| 7275 | pm_runtime_get_sync(hba->dev); |
| 7276 | err = ufshcd_query_ioctl(hba, ufshcd_scsi_to_upiu_lun(dev->lun), |
| 7277 | buffer); |
| 7278 | pm_runtime_put_sync(hba->dev); |
| 7279 | break; |
| 7280 | default: |
| 7281 | err = -ENOIOCTLCMD; |
| 7282 | dev_dbg(hba->dev, "%s: Unsupported ioctl cmd %d\n", __func__, |
| 7283 | cmd); |
| 7284 | break; |
| 7285 | } |
| 7286 | |
| 7287 | return err; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7288 | } |
| 7289 | |
Yaniv Gardi | f550c65 | 2016-03-10 17:37:07 +0200 | [diff] [blame] | 7290 | static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd) |
| 7291 | { |
| 7292 | unsigned long flags; |
| 7293 | struct Scsi_Host *host; |
| 7294 | struct ufs_hba *hba; |
| 7295 | int index; |
| 7296 | bool found = false; |
| 7297 | |
| 7298 | if (!scmd || !scmd->device || !scmd->device->host) |
| 7299 | return BLK_EH_NOT_HANDLED; |
| 7300 | |
| 7301 | host = scmd->device->host; |
| 7302 | hba = shost_priv(host); |
| 7303 | if (!hba) |
| 7304 | return BLK_EH_NOT_HANDLED; |
| 7305 | |
| 7306 | spin_lock_irqsave(host->host_lock, flags); |
| 7307 | |
| 7308 | for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) { |
| 7309 | if (hba->lrb[index].cmd == scmd) { |
| 7310 | found = true; |
| 7311 | break; |
| 7312 | } |
| 7313 | } |
| 7314 | |
| 7315 | spin_unlock_irqrestore(host->host_lock, flags); |
| 7316 | |
| 7317 | /* |
| 7318 | * Bypass SCSI error handling and reset the block layer timer if this |
| 7319 | * SCSI command was not actually dispatched to UFS driver, otherwise |
| 7320 | * let SCSI layer handle the error as usual. |
| 7321 | */ |
| 7322 | return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER; |
| 7323 | } |
| 7324 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7325 | static struct scsi_host_template ufshcd_driver_template = { |
| 7326 | .module = THIS_MODULE, |
| 7327 | .name = UFSHCD, |
| 7328 | .proc_name = UFSHCD, |
| 7329 | .queuecommand = ufshcd_queuecommand, |
| 7330 | .slave_alloc = ufshcd_slave_alloc, |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 7331 | .slave_configure = ufshcd_slave_configure, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7332 | .slave_destroy = ufshcd_slave_destroy, |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 7333 | .change_queue_depth = ufshcd_change_queue_depth, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7334 | .eh_abort_handler = ufshcd_abort, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 7335 | .eh_device_reset_handler = ufshcd_eh_device_reset_handler, |
| 7336 | .eh_host_reset_handler = ufshcd_eh_host_reset_handler, |
Yaniv Gardi | f550c65 | 2016-03-10 17:37:07 +0200 | [diff] [blame] | 7337 | .eh_timed_out = ufshcd_eh_timed_out, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7338 | .ioctl = ufshcd_ioctl, |
| 7339 | #ifdef CONFIG_COMPAT |
| 7340 | .compat_ioctl = ufshcd_ioctl, |
| 7341 | #endif |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7342 | .this_id = -1, |
| 7343 | .sg_tablesize = SG_ALL, |
| 7344 | .cmd_per_lun = UFSHCD_CMD_PER_LUN, |
| 7345 | .can_queue = UFSHCD_CAN_QUEUE, |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7346 | .max_host_blocked = 1, |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 7347 | .track_queue_depth = 1, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7348 | }; |
| 7349 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7350 | static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg, |
| 7351 | int ua) |
| 7352 | { |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 7353 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7354 | |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 7355 | if (!vreg) |
| 7356 | return 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7357 | |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 7358 | ret = regulator_set_load(vreg->reg, ua); |
| 7359 | if (ret < 0) { |
| 7360 | dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n", |
| 7361 | __func__, vreg->name, ua, ret); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7362 | } |
| 7363 | |
| 7364 | return ret; |
| 7365 | } |
| 7366 | |
| 7367 | static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba, |
| 7368 | struct ufs_vreg *vreg) |
| 7369 | { |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 7370 | if (!vreg) |
| 7371 | return 0; |
| 7372 | else if (vreg->unused) |
| 7373 | return 0; |
| 7374 | else |
| 7375 | return ufshcd_config_vreg_load(hba->dev, vreg, |
| 7376 | UFS_VREG_LPM_LOAD_UA); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7377 | } |
| 7378 | |
| 7379 | static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba, |
| 7380 | struct ufs_vreg *vreg) |
| 7381 | { |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 7382 | if (!vreg) |
| 7383 | return 0; |
| 7384 | else if (vreg->unused) |
| 7385 | return 0; |
| 7386 | else |
| 7387 | return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7388 | } |
| 7389 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7390 | static int ufshcd_config_vreg(struct device *dev, |
| 7391 | struct ufs_vreg *vreg, bool on) |
| 7392 | { |
| 7393 | int ret = 0; |
| 7394 | struct regulator *reg = vreg->reg; |
| 7395 | const char *name = vreg->name; |
| 7396 | int min_uV, uA_load; |
| 7397 | |
| 7398 | BUG_ON(!vreg); |
| 7399 | |
| 7400 | if (regulator_count_voltages(reg) > 0) { |
| 7401 | min_uV = on ? vreg->min_uV : 0; |
| 7402 | ret = regulator_set_voltage(reg, min_uV, vreg->max_uV); |
| 7403 | if (ret) { |
| 7404 | dev_err(dev, "%s: %s set voltage failed, err=%d\n", |
| 7405 | __func__, name, ret); |
| 7406 | goto out; |
| 7407 | } |
| 7408 | |
| 7409 | uA_load = on ? vreg->max_uA : 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7410 | ret = ufshcd_config_vreg_load(dev, vreg, uA_load); |
| 7411 | if (ret) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7412 | goto out; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7413 | } |
| 7414 | out: |
| 7415 | return ret; |
| 7416 | } |
| 7417 | |
| 7418 | static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 7419 | { |
| 7420 | int ret = 0; |
| 7421 | |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 7422 | if (!vreg) |
| 7423 | goto out; |
| 7424 | else if (vreg->enabled || vreg->unused) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7425 | goto out; |
| 7426 | |
| 7427 | ret = ufshcd_config_vreg(dev, vreg, true); |
| 7428 | if (!ret) |
| 7429 | ret = regulator_enable(vreg->reg); |
| 7430 | |
| 7431 | if (!ret) |
| 7432 | vreg->enabled = true; |
| 7433 | else |
| 7434 | dev_err(dev, "%s: %s enable failed, err=%d\n", |
| 7435 | __func__, vreg->name, ret); |
| 7436 | out: |
| 7437 | return ret; |
| 7438 | } |
| 7439 | |
| 7440 | static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 7441 | { |
| 7442 | int ret = 0; |
| 7443 | |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 7444 | if (!vreg) |
| 7445 | goto out; |
| 7446 | else if (!vreg->enabled || vreg->unused) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7447 | goto out; |
| 7448 | |
| 7449 | ret = regulator_disable(vreg->reg); |
| 7450 | |
| 7451 | if (!ret) { |
| 7452 | /* ignore errors on applying disable config */ |
| 7453 | ufshcd_config_vreg(dev, vreg, false); |
| 7454 | vreg->enabled = false; |
| 7455 | } else { |
| 7456 | dev_err(dev, "%s: %s disable failed, err=%d\n", |
| 7457 | __func__, vreg->name, ret); |
| 7458 | } |
| 7459 | out: |
| 7460 | return ret; |
| 7461 | } |
| 7462 | |
| 7463 | static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on) |
| 7464 | { |
| 7465 | int ret = 0; |
| 7466 | struct device *dev = hba->dev; |
| 7467 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 7468 | |
| 7469 | if (!info) |
| 7470 | goto out; |
| 7471 | |
| 7472 | ret = ufshcd_toggle_vreg(dev, info->vcc, on); |
| 7473 | if (ret) |
| 7474 | goto out; |
| 7475 | |
| 7476 | ret = ufshcd_toggle_vreg(dev, info->vccq, on); |
| 7477 | if (ret) |
| 7478 | goto out; |
| 7479 | |
| 7480 | ret = ufshcd_toggle_vreg(dev, info->vccq2, on); |
| 7481 | if (ret) |
| 7482 | goto out; |
| 7483 | |
| 7484 | out: |
| 7485 | if (ret) { |
| 7486 | ufshcd_toggle_vreg(dev, info->vccq2, false); |
| 7487 | ufshcd_toggle_vreg(dev, info->vccq, false); |
| 7488 | ufshcd_toggle_vreg(dev, info->vcc, false); |
| 7489 | } |
| 7490 | return ret; |
| 7491 | } |
| 7492 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7493 | static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on) |
| 7494 | { |
| 7495 | struct ufs_vreg_info *info = &hba->vreg_info; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7496 | int ret = 0; |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7497 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7498 | if (info->vdd_hba) { |
| 7499 | ret = ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on); |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7500 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7501 | if (!ret) |
| 7502 | ufshcd_vops_update_sec_cfg(hba, on); |
| 7503 | } |
| 7504 | |
| 7505 | return ret; |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7506 | } |
| 7507 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7508 | static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 7509 | { |
| 7510 | int ret = 0; |
| 7511 | |
| 7512 | if (!vreg) |
| 7513 | goto out; |
| 7514 | |
| 7515 | vreg->reg = devm_regulator_get(dev, vreg->name); |
| 7516 | if (IS_ERR(vreg->reg)) { |
| 7517 | ret = PTR_ERR(vreg->reg); |
| 7518 | dev_err(dev, "%s: %s get failed, err=%d\n", |
| 7519 | __func__, vreg->name, ret); |
| 7520 | } |
| 7521 | out: |
| 7522 | return ret; |
| 7523 | } |
| 7524 | |
| 7525 | static int ufshcd_init_vreg(struct ufs_hba *hba) |
| 7526 | { |
| 7527 | int ret = 0; |
| 7528 | struct device *dev = hba->dev; |
| 7529 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 7530 | |
| 7531 | if (!info) |
| 7532 | goto out; |
| 7533 | |
| 7534 | ret = ufshcd_get_vreg(dev, info->vcc); |
| 7535 | if (ret) |
| 7536 | goto out; |
| 7537 | |
| 7538 | ret = ufshcd_get_vreg(dev, info->vccq); |
| 7539 | if (ret) |
| 7540 | goto out; |
| 7541 | |
| 7542 | ret = ufshcd_get_vreg(dev, info->vccq2); |
| 7543 | out: |
| 7544 | return ret; |
| 7545 | } |
| 7546 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7547 | static int ufshcd_init_hba_vreg(struct ufs_hba *hba) |
| 7548 | { |
| 7549 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 7550 | |
| 7551 | if (info) |
| 7552 | return ufshcd_get_vreg(hba->dev, info->vdd_hba); |
| 7553 | |
| 7554 | return 0; |
| 7555 | } |
| 7556 | |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 7557 | static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused) |
| 7558 | { |
| 7559 | int ret = 0; |
| 7560 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 7561 | |
| 7562 | if (!info) |
| 7563 | goto out; |
| 7564 | else if (!info->vccq) |
| 7565 | goto out; |
| 7566 | |
| 7567 | if (unused) { |
| 7568 | /* shut off the rail here */ |
| 7569 | ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false); |
| 7570 | /* |
| 7571 | * Mark this rail as no longer used, so it doesn't get enabled |
| 7572 | * later by mistake |
| 7573 | */ |
| 7574 | if (!ret) |
| 7575 | info->vccq->unused = true; |
| 7576 | } else { |
| 7577 | /* |
| 7578 | * rail should have been already enabled hence just make sure |
| 7579 | * that unused flag is cleared. |
| 7580 | */ |
| 7581 | info->vccq->unused = false; |
| 7582 | } |
| 7583 | out: |
| 7584 | return ret; |
| 7585 | } |
| 7586 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7587 | static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on, |
| 7588 | bool skip_ref_clk, bool is_gating_context) |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7589 | { |
| 7590 | int ret = 0; |
| 7591 | struct ufs_clk_info *clki; |
| 7592 | struct list_head *head = &hba->clk_list_head; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7593 | unsigned long flags; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7594 | ktime_t start = ktime_get(); |
| 7595 | bool clk_state_changed = false; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7596 | |
| 7597 | if (!head || list_empty(head)) |
| 7598 | goto out; |
| 7599 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7600 | /* |
| 7601 | * vendor specific setup_clocks ops may depend on clocks managed by |
| 7602 | * this standard driver hence call the vendor specific setup_clocks |
| 7603 | * before disabling the clocks managed here. |
| 7604 | */ |
| 7605 | if (!on) { |
| 7606 | ret = ufshcd_vops_setup_clocks(hba, on, is_gating_context); |
| 7607 | if (ret) |
| 7608 | return ret; |
| 7609 | } |
| 7610 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7611 | list_for_each_entry(clki, head, list) { |
| 7612 | if (!IS_ERR_OR_NULL(clki->clk)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7613 | if (skip_ref_clk && !strcmp(clki->name, "ref_clk")) |
| 7614 | continue; |
| 7615 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7616 | clk_state_changed = on ^ clki->enabled; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7617 | if (on && !clki->enabled) { |
| 7618 | ret = clk_prepare_enable(clki->clk); |
| 7619 | if (ret) { |
| 7620 | dev_err(hba->dev, "%s: %s prepare enable failed, %d\n", |
| 7621 | __func__, clki->name, ret); |
| 7622 | goto out; |
| 7623 | } |
| 7624 | } else if (!on && clki->enabled) { |
| 7625 | clk_disable_unprepare(clki->clk); |
| 7626 | } |
| 7627 | clki->enabled = on; |
| 7628 | dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__, |
| 7629 | clki->name, on ? "en" : "dis"); |
| 7630 | } |
| 7631 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7632 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7633 | /* |
| 7634 | * vendor specific setup_clocks ops may depend on clocks managed by |
| 7635 | * this standard driver hence call the vendor specific setup_clocks |
| 7636 | * after enabling the clocks managed here. |
| 7637 | */ |
| 7638 | if (on) |
| 7639 | ret = ufshcd_vops_setup_clocks(hba, on, is_gating_context); |
| 7640 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7641 | out: |
| 7642 | if (ret) { |
| 7643 | list_for_each_entry(clki, head, list) { |
| 7644 | if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled) |
| 7645 | clk_disable_unprepare(clki->clk); |
| 7646 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7647 | } else if (!ret && on) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7648 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 7649 | hba->clk_gating.state = CLKS_ON; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7650 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 7651 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7652 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7653 | /* restore the secure configuration as clocks are enabled */ |
| 7654 | ufshcd_vops_update_sec_cfg(hba, true); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7655 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7656 | |
| 7657 | if (clk_state_changed) |
| 7658 | trace_ufshcd_profile_clk_gating(dev_name(hba->dev), |
| 7659 | (on ? "on" : "off"), |
| 7660 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7661 | return ret; |
| 7662 | } |
| 7663 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7664 | static int ufshcd_enable_clocks(struct ufs_hba *hba) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7665 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7666 | return ufshcd_setup_clocks(hba, true, false, false); |
| 7667 | } |
| 7668 | |
| 7669 | static int ufshcd_disable_clocks(struct ufs_hba *hba, |
| 7670 | bool is_gating_context) |
| 7671 | { |
| 7672 | return ufshcd_setup_clocks(hba, false, false, is_gating_context); |
| 7673 | } |
| 7674 | |
| 7675 | static int ufshcd_disable_clocks_skip_ref_clk(struct ufs_hba *hba, |
| 7676 | bool is_gating_context) |
| 7677 | { |
| 7678 | return ufshcd_setup_clocks(hba, false, true, is_gating_context); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7679 | } |
| 7680 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7681 | static int ufshcd_init_clocks(struct ufs_hba *hba) |
| 7682 | { |
| 7683 | int ret = 0; |
| 7684 | struct ufs_clk_info *clki; |
| 7685 | struct device *dev = hba->dev; |
| 7686 | struct list_head *head = &hba->clk_list_head; |
| 7687 | |
| 7688 | if (!head || list_empty(head)) |
| 7689 | goto out; |
| 7690 | |
| 7691 | list_for_each_entry(clki, head, list) { |
| 7692 | if (!clki->name) |
| 7693 | continue; |
| 7694 | |
| 7695 | clki->clk = devm_clk_get(dev, clki->name); |
| 7696 | if (IS_ERR(clki->clk)) { |
| 7697 | ret = PTR_ERR(clki->clk); |
| 7698 | dev_err(dev, "%s: %s clk get failed, %d\n", |
| 7699 | __func__, clki->name, ret); |
| 7700 | goto out; |
| 7701 | } |
| 7702 | |
| 7703 | if (clki->max_freq) { |
| 7704 | ret = clk_set_rate(clki->clk, clki->max_freq); |
| 7705 | if (ret) { |
| 7706 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 7707 | __func__, clki->name, |
| 7708 | clki->max_freq, ret); |
| 7709 | goto out; |
| 7710 | } |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 7711 | clki->curr_freq = clki->max_freq; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7712 | } |
| 7713 | dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__, |
| 7714 | clki->name, clk_get_rate(clki->clk)); |
| 7715 | } |
| 7716 | out: |
| 7717 | return ret; |
| 7718 | } |
| 7719 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7720 | static int ufshcd_variant_hba_init(struct ufs_hba *hba) |
| 7721 | { |
| 7722 | int err = 0; |
| 7723 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7724 | if (!hba->var || !hba->var->vops) |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7725 | goto out; |
| 7726 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7727 | err = ufshcd_vops_init(hba); |
| 7728 | if (err) |
| 7729 | goto out; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7730 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7731 | err = ufshcd_vops_setup_regulators(hba, true); |
| 7732 | if (err) |
| 7733 | goto out_exit; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7734 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7735 | goto out; |
| 7736 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7737 | out_exit: |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7738 | ufshcd_vops_exit(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7739 | out: |
| 7740 | if (err) |
| 7741 | dev_err(hba->dev, "%s: variant %s init failed err %d\n", |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7742 | __func__, ufshcd_get_var_name(hba), err); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7743 | return err; |
| 7744 | } |
| 7745 | |
| 7746 | static void ufshcd_variant_hba_exit(struct ufs_hba *hba) |
| 7747 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7748 | if (!hba->var || !hba->var->vops) |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7749 | return; |
| 7750 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7751 | ufshcd_vops_setup_regulators(hba, false); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7752 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7753 | ufshcd_vops_exit(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7754 | } |
| 7755 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7756 | static int ufshcd_hba_init(struct ufs_hba *hba) |
| 7757 | { |
| 7758 | int err; |
| 7759 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7760 | /* |
| 7761 | * Handle host controller power separately from the UFS device power |
| 7762 | * rails as it will help controlling the UFS host controller power |
| 7763 | * collapse easily which is different than UFS device power collapse. |
| 7764 | * Also, enable the host controller power before we go ahead with rest |
| 7765 | * of the initialization here. |
| 7766 | */ |
| 7767 | err = ufshcd_init_hba_vreg(hba); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7768 | if (err) |
| 7769 | goto out; |
| 7770 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7771 | err = ufshcd_setup_hba_vreg(hba, true); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7772 | if (err) |
| 7773 | goto out; |
| 7774 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7775 | err = ufshcd_init_clocks(hba); |
| 7776 | if (err) |
| 7777 | goto out_disable_hba_vreg; |
| 7778 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7779 | err = ufshcd_enable_clocks(hba); |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7780 | if (err) |
| 7781 | goto out_disable_hba_vreg; |
| 7782 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7783 | err = ufshcd_init_vreg(hba); |
| 7784 | if (err) |
| 7785 | goto out_disable_clks; |
| 7786 | |
| 7787 | err = ufshcd_setup_vreg(hba, true); |
| 7788 | if (err) |
| 7789 | goto out_disable_clks; |
| 7790 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7791 | err = ufshcd_variant_hba_init(hba); |
| 7792 | if (err) |
| 7793 | goto out_disable_vreg; |
| 7794 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7795 | hba->is_powered = true; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7796 | goto out; |
| 7797 | |
| 7798 | out_disable_vreg: |
| 7799 | ufshcd_setup_vreg(hba, false); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7800 | out_disable_clks: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7801 | ufshcd_disable_clocks(hba, false); |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7802 | out_disable_hba_vreg: |
| 7803 | ufshcd_setup_hba_vreg(hba, false); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7804 | out: |
| 7805 | return err; |
| 7806 | } |
| 7807 | |
| 7808 | static void ufshcd_hba_exit(struct ufs_hba *hba) |
| 7809 | { |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7810 | if (hba->is_powered) { |
| 7811 | ufshcd_variant_hba_exit(hba); |
| 7812 | ufshcd_setup_vreg(hba, false); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7813 | if (ufshcd_is_clkscaling_supported(hba)) { |
| 7814 | ufshcd_suspend_clkscaling(hba); |
| 7815 | destroy_workqueue(hba->clk_scaling.workq); |
| 7816 | } |
| 7817 | ufshcd_disable_clocks(hba, false); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7818 | ufshcd_setup_hba_vreg(hba, false); |
| 7819 | hba->is_powered = false; |
| 7820 | } |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7821 | } |
| 7822 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7823 | static int |
| 7824 | ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7825 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7826 | unsigned char cmd[6] = {REQUEST_SENSE, |
| 7827 | 0, |
| 7828 | 0, |
| 7829 | 0, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7830 | UFSHCD_REQ_SENSE_SIZE, |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7831 | 0}; |
| 7832 | char *buffer; |
| 7833 | int ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7834 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7835 | buffer = kzalloc(UFSHCD_REQ_SENSE_SIZE, GFP_KERNEL); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7836 | if (!buffer) { |
| 7837 | ret = -ENOMEM; |
| 7838 | goto out; |
| 7839 | } |
| 7840 | |
| 7841 | ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7842 | UFSHCD_REQ_SENSE_SIZE, NULL, |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7843 | msecs_to_jiffies(1000), 3, NULL, REQ_PM); |
| 7844 | if (ret) |
| 7845 | pr_err("%s: failed with err %d\n", __func__, ret); |
| 7846 | |
| 7847 | kfree(buffer); |
| 7848 | out: |
| 7849 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7850 | } |
| 7851 | |
| 7852 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7853 | * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device |
| 7854 | * power mode |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7855 | * @hba: per adapter instance |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7856 | * @pwr_mode: device power mode to set |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7857 | * |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7858 | * Returns 0 if requested power mode is set successfully |
| 7859 | * Returns non-zero if failed to set the requested power mode |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7860 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7861 | static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba, |
| 7862 | enum ufs_dev_pwr_mode pwr_mode) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7863 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7864 | unsigned char cmd[6] = { START_STOP }; |
| 7865 | struct scsi_sense_hdr sshdr; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 7866 | struct scsi_device *sdp; |
| 7867 | unsigned long flags; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7868 | int ret; |
| 7869 | |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 7870 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 7871 | sdp = hba->sdev_ufs_device; |
| 7872 | if (sdp) { |
| 7873 | ret = scsi_device_get(sdp); |
| 7874 | if (!ret && !scsi_device_online(sdp)) { |
| 7875 | ret = -ENODEV; |
| 7876 | scsi_device_put(sdp); |
| 7877 | } |
| 7878 | } else { |
| 7879 | ret = -ENODEV; |
| 7880 | } |
| 7881 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 7882 | |
| 7883 | if (ret) |
| 7884 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7885 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7886 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7887 | * If scsi commands fail, the scsi mid-layer schedules scsi error- |
| 7888 | * handling, which would wait for host to be resumed. Since we know |
| 7889 | * we are functional while we are here, skip host resume in error |
| 7890 | * handling context. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7891 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7892 | hba->host->eh_noresume = 1; |
| 7893 | if (hba->wlun_dev_clr_ua) { |
| 7894 | ret = ufshcd_send_request_sense(hba, sdp); |
| 7895 | if (ret) |
| 7896 | goto out; |
| 7897 | /* Unit attention condition is cleared now */ |
| 7898 | hba->wlun_dev_clr_ua = false; |
| 7899 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7900 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7901 | cmd[4] = pwr_mode << 4; |
| 7902 | |
| 7903 | /* |
| 7904 | * Current function would be generally called from the power management |
| 7905 | * callbacks hence set the REQ_PM flag so that it doesn't resume the |
| 7906 | * already suspended childs. |
| 7907 | */ |
| 7908 | ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr, |
| 7909 | START_STOP_TIMEOUT, 0, NULL, REQ_PM); |
| 7910 | if (ret) { |
| 7911 | sdev_printk(KERN_WARNING, sdp, |
Hannes Reinecke | ef61329 | 2014-10-24 14:27:00 +0200 | [diff] [blame] | 7912 | "START_STOP failed for power mode: %d, result %x\n", |
| 7913 | pwr_mode, ret); |
Hannes Reinecke | 2104551 | 2015-01-08 07:43:46 +0100 | [diff] [blame] | 7914 | if (driver_byte(ret) & DRIVER_SENSE) |
| 7915 | scsi_print_sense_hdr(sdp, NULL, &sshdr); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7916 | } |
| 7917 | |
| 7918 | if (!ret) |
| 7919 | hba->curr_dev_pwr_mode = pwr_mode; |
| 7920 | out: |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 7921 | scsi_device_put(sdp); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7922 | hba->host->eh_noresume = 0; |
| 7923 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7924 | } |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7925 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7926 | static int ufshcd_link_state_transition(struct ufs_hba *hba, |
| 7927 | enum uic_link_state req_link_state, |
| 7928 | int check_for_bkops) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 7929 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7930 | int ret = 0; |
| 7931 | |
| 7932 | if (req_link_state == hba->uic_link_state) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 7933 | return 0; |
| 7934 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7935 | if (req_link_state == UIC_LINK_HIBERN8_STATE) { |
| 7936 | ret = ufshcd_uic_hibern8_enter(hba); |
| 7937 | if (!ret) |
| 7938 | ufshcd_set_link_hibern8(hba); |
| 7939 | else |
| 7940 | goto out; |
| 7941 | } |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 7942 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7943 | * If autobkops is enabled, link can't be turned off because |
| 7944 | * turning off the link would also turn off the device. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 7945 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7946 | else if ((req_link_state == UIC_LINK_OFF_STATE) && |
| 7947 | (!check_for_bkops || (check_for_bkops && |
| 7948 | !hba->auto_bkops_enabled))) { |
| 7949 | /* |
Yaniv Gardi | f3099fb | 2016-03-10 17:37:17 +0200 | [diff] [blame] | 7950 | * Let's make sure that link is in low power mode, we are doing |
| 7951 | * this currently by putting the link in Hibern8. Otherway to |
| 7952 | * put the link in low power mode is to send the DME end point |
| 7953 | * to device and then send the DME reset command to local |
| 7954 | * unipro. But putting the link in hibern8 is much faster. |
| 7955 | */ |
| 7956 | ret = ufshcd_uic_hibern8_enter(hba); |
| 7957 | if (ret) |
| 7958 | goto out; |
| 7959 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7960 | * Change controller state to "reset state" which |
| 7961 | * should also put the link in off/reset state |
| 7962 | */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 7963 | ufshcd_hba_stop(hba, true); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7964 | /* |
| 7965 | * TODO: Check if we need any delay to make sure that |
| 7966 | * controller is reset |
| 7967 | */ |
| 7968 | ufshcd_set_link_off(hba); |
| 7969 | } |
| 7970 | |
| 7971 | out: |
| 7972 | return ret; |
| 7973 | } |
| 7974 | |
| 7975 | static void ufshcd_vreg_set_lpm(struct ufs_hba *hba) |
| 7976 | { |
| 7977 | /* |
Yaniv Gardi | b799fdf | 2016-03-10 17:37:18 +0200 | [diff] [blame] | 7978 | * It seems some UFS devices may keep drawing more than sleep current |
| 7979 | * (atleast for 500us) from UFS rails (especially from VCCQ rail). |
| 7980 | * To avoid this situation, add 2ms delay before putting these UFS |
| 7981 | * rails in LPM mode. |
| 7982 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7983 | if (!ufshcd_is_link_active(hba)) |
Yaniv Gardi | b799fdf | 2016-03-10 17:37:18 +0200 | [diff] [blame] | 7984 | usleep_range(2000, 2100); |
| 7985 | |
| 7986 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7987 | * If UFS device is either in UFS_Sleep turn off VCC rail to save some |
| 7988 | * power. |
| 7989 | * |
| 7990 | * If UFS device and link is in OFF state, all power supplies (VCC, |
| 7991 | * VCCQ, VCCQ2) can be turned off if power on write protect is not |
| 7992 | * required. If UFS link is inactive (Hibern8 or OFF state) and device |
| 7993 | * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode. |
| 7994 | * |
| 7995 | * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway |
| 7996 | * in low power state which would save some power. |
| 7997 | */ |
| 7998 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) && |
| 7999 | !hba->dev_info.is_lu_power_on_wp) { |
| 8000 | ufshcd_setup_vreg(hba, false); |
| 8001 | } else if (!ufshcd_is_ufs_dev_active(hba)) { |
| 8002 | ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false); |
| 8003 | if (!ufshcd_is_link_active(hba)) { |
| 8004 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq); |
| 8005 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2); |
| 8006 | } |
| 8007 | } |
| 8008 | } |
| 8009 | |
| 8010 | static int ufshcd_vreg_set_hpm(struct ufs_hba *hba) |
| 8011 | { |
| 8012 | int ret = 0; |
| 8013 | |
| 8014 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) && |
| 8015 | !hba->dev_info.is_lu_power_on_wp) { |
| 8016 | ret = ufshcd_setup_vreg(hba, true); |
| 8017 | } else if (!ufshcd_is_ufs_dev_active(hba)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8018 | if (!ret && !ufshcd_is_link_active(hba)) { |
| 8019 | ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq); |
| 8020 | if (ret) |
| 8021 | goto vcc_disable; |
| 8022 | ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2); |
| 8023 | if (ret) |
| 8024 | goto vccq_lpm; |
| 8025 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8026 | ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8027 | } |
| 8028 | goto out; |
| 8029 | |
| 8030 | vccq_lpm: |
| 8031 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq); |
| 8032 | vcc_disable: |
| 8033 | ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false); |
| 8034 | out: |
| 8035 | return ret; |
| 8036 | } |
| 8037 | |
| 8038 | static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba) |
| 8039 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8040 | if (ufshcd_is_link_off(hba) || |
| 8041 | (ufshcd_is_link_hibern8(hba) |
| 8042 | && ufshcd_is_power_collapse_during_hibern8_allowed(hba))) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8043 | ufshcd_setup_hba_vreg(hba, false); |
| 8044 | } |
| 8045 | |
| 8046 | static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba) |
| 8047 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8048 | if (ufshcd_is_link_off(hba) || |
| 8049 | (ufshcd_is_link_hibern8(hba) |
| 8050 | && ufshcd_is_power_collapse_during_hibern8_allowed(hba))) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8051 | ufshcd_setup_hba_vreg(hba, true); |
| 8052 | } |
| 8053 | |
| 8054 | /** |
| 8055 | * ufshcd_suspend - helper function for suspend operations |
| 8056 | * @hba: per adapter instance |
| 8057 | * @pm_op: desired low power operation type |
| 8058 | * |
| 8059 | * This function will try to put the UFS device and link into low power |
| 8060 | * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl" |
| 8061 | * (System PM level). |
| 8062 | * |
| 8063 | * If this function is called during shutdown, it will make sure that |
| 8064 | * both UFS device and UFS link is powered off. |
| 8065 | * |
| 8066 | * NOTE: UFS device & link must be active before we enter in this function. |
| 8067 | * |
| 8068 | * Returns 0 for success and non-zero for failure |
| 8069 | */ |
| 8070 | static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op) |
| 8071 | { |
| 8072 | int ret = 0; |
| 8073 | enum ufs_pm_level pm_lvl; |
| 8074 | enum ufs_dev_pwr_mode req_dev_pwr_mode; |
| 8075 | enum uic_link_state req_link_state; |
| 8076 | |
| 8077 | hba->pm_op_in_progress = 1; |
| 8078 | if (!ufshcd_is_shutdown_pm(pm_op)) { |
| 8079 | pm_lvl = ufshcd_is_runtime_pm(pm_op) ? |
| 8080 | hba->rpm_lvl : hba->spm_lvl; |
| 8081 | req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl); |
| 8082 | req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl); |
| 8083 | } else { |
| 8084 | req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE; |
| 8085 | req_link_state = UIC_LINK_OFF_STATE; |
| 8086 | } |
| 8087 | |
| 8088 | /* |
| 8089 | * If we can't transition into any of the low power modes |
| 8090 | * just gate the clocks. |
| 8091 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8092 | WARN_ON(hba->hibern8_on_idle.is_enabled && |
| 8093 | hba->hibern8_on_idle.active_reqs); |
| 8094 | ufshcd_hold_all(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8095 | hba->clk_gating.is_suspended = true; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8096 | hba->hibern8_on_idle.is_suspended = true; |
| 8097 | |
| 8098 | if (hba->clk_scaling.is_allowed) { |
| 8099 | cancel_work_sync(&hba->clk_scaling.suspend_work); |
| 8100 | cancel_work_sync(&hba->clk_scaling.resume_work); |
| 8101 | ufshcd_suspend_clkscaling(hba); |
| 8102 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8103 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8104 | if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE && |
| 8105 | req_link_state == UIC_LINK_ACTIVE_STATE) { |
| 8106 | goto disable_clks; |
| 8107 | } |
| 8108 | |
| 8109 | if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) && |
| 8110 | (req_link_state == hba->uic_link_state)) |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8111 | goto enable_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8112 | |
| 8113 | /* UFS device & link must be active before we enter in this function */ |
| 8114 | if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) { |
| 8115 | ret = -EINVAL; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8116 | goto enable_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8117 | } |
| 8118 | |
| 8119 | if (ufshcd_is_runtime_pm(pm_op)) { |
Subhash Jadavani | 374a246 | 2014-09-25 15:32:35 +0300 | [diff] [blame] | 8120 | if (ufshcd_can_autobkops_during_suspend(hba)) { |
| 8121 | /* |
| 8122 | * The device is idle with no requests in the queue, |
| 8123 | * allow background operations if bkops status shows |
| 8124 | * that performance might be impacted. |
| 8125 | */ |
| 8126 | ret = ufshcd_urgent_bkops(hba); |
| 8127 | if (ret) |
| 8128 | goto enable_gating; |
| 8129 | } else { |
| 8130 | /* make sure that auto bkops is disabled */ |
| 8131 | ufshcd_disable_auto_bkops(hba); |
| 8132 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8133 | } |
| 8134 | |
| 8135 | if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) && |
| 8136 | ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) || |
| 8137 | !ufshcd_is_runtime_pm(pm_op))) { |
| 8138 | /* ensure that bkops is disabled */ |
| 8139 | ufshcd_disable_auto_bkops(hba); |
| 8140 | ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode); |
| 8141 | if (ret) |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8142 | goto enable_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8143 | } |
| 8144 | |
| 8145 | ret = ufshcd_link_state_transition(hba, req_link_state, 1); |
| 8146 | if (ret) |
| 8147 | goto set_dev_active; |
| 8148 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8149 | if (ufshcd_is_link_hibern8(hba) && |
| 8150 | ufshcd_is_hibern8_on_idle_allowed(hba)) |
| 8151 | hba->hibern8_on_idle.state = HIBERN8_ENTERED; |
| 8152 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8153 | ufshcd_vreg_set_lpm(hba); |
| 8154 | |
| 8155 | disable_clks: |
| 8156 | /* |
| 8157 | * Call vendor specific suspend callback. As these callbacks may access |
| 8158 | * vendor specific host controller register space call them before the |
| 8159 | * host clocks are ON. |
| 8160 | */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 8161 | ret = ufshcd_vops_suspend(hba, pm_op); |
| 8162 | if (ret) |
| 8163 | goto set_link_active; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8164 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8165 | if (!ufshcd_is_link_active(hba)) |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8166 | ret = ufshcd_disable_clocks(hba, false); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8167 | else |
| 8168 | /* If link is active, device ref_clk can't be switched off */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8169 | ret = ufshcd_disable_clocks_skip_ref_clk(hba, false); |
| 8170 | if (ret) |
| 8171 | goto set_link_active; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8172 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8173 | if (ufshcd_is_clkgating_allowed(hba)) { |
| 8174 | hba->clk_gating.state = CLKS_OFF; |
| 8175 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 8176 | hba->clk_gating.state); |
| 8177 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8178 | /* |
| 8179 | * 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] | 8180 | * host controller transaction expected till resume. |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8181 | */ |
| 8182 | ufshcd_disable_irq(hba); |
| 8183 | /* Put the host controller in low power mode if possible */ |
| 8184 | ufshcd_hba_vreg_set_lpm(hba); |
| 8185 | goto out; |
| 8186 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8187 | set_link_active: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8188 | if (hba->clk_scaling.is_allowed) |
| 8189 | ufshcd_resume_clkscaling(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8190 | ufshcd_vreg_set_hpm(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8191 | if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8192 | ufshcd_set_link_active(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8193 | } else if (ufshcd_is_link_off(hba)) { |
| 8194 | ufshcd_update_error_stats(hba, UFS_ERR_VOPS_SUSPEND); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8195 | ufshcd_host_reset_and_restore(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8196 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8197 | set_dev_active: |
| 8198 | if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE)) |
| 8199 | ufshcd_disable_auto_bkops(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8200 | enable_gating: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8201 | if (hba->clk_scaling.is_allowed) |
| 8202 | ufshcd_resume_clkscaling(hba); |
| 8203 | hba->hibern8_on_idle.is_suspended = false; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8204 | hba->clk_gating.is_suspended = false; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8205 | ufshcd_release_all(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8206 | out: |
| 8207 | hba->pm_op_in_progress = 0; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8208 | |
| 8209 | if (ret) |
| 8210 | ufshcd_update_error_stats(hba, UFS_ERR_SUSPEND); |
| 8211 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8212 | return ret; |
| 8213 | } |
| 8214 | |
| 8215 | /** |
| 8216 | * ufshcd_resume - helper function for resume operations |
| 8217 | * @hba: per adapter instance |
| 8218 | * @pm_op: runtime PM or system PM |
| 8219 | * |
| 8220 | * This function basically brings the UFS device, UniPro link and controller |
| 8221 | * to active state. |
| 8222 | * |
| 8223 | * Returns 0 for success and non-zero for failure |
| 8224 | */ |
| 8225 | static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op) |
| 8226 | { |
| 8227 | int ret; |
| 8228 | enum uic_link_state old_link_state; |
| 8229 | |
| 8230 | hba->pm_op_in_progress = 1; |
| 8231 | old_link_state = hba->uic_link_state; |
| 8232 | |
| 8233 | ufshcd_hba_vreg_set_hpm(hba); |
| 8234 | /* Make sure clocks are enabled before accessing controller */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8235 | ret = ufshcd_enable_clocks(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8236 | if (ret) |
| 8237 | goto out; |
| 8238 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8239 | /* enable the host irq as host controller would be active soon */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8240 | ufshcd_enable_irq(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8241 | |
| 8242 | ret = ufshcd_vreg_set_hpm(hba); |
| 8243 | if (ret) |
| 8244 | goto disable_irq_and_vops_clks; |
| 8245 | |
| 8246 | /* |
| 8247 | * Call vendor specific resume callback. As these callbacks may access |
| 8248 | * vendor specific host controller register space call them when the |
| 8249 | * host clocks are ON. |
| 8250 | */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 8251 | ret = ufshcd_vops_resume(hba, pm_op); |
| 8252 | if (ret) |
| 8253 | goto disable_vreg; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8254 | |
| 8255 | if (ufshcd_is_link_hibern8(hba)) { |
| 8256 | ret = ufshcd_uic_hibern8_exit(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8257 | if (!ret) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8258 | ufshcd_set_link_active(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8259 | if (ufshcd_is_hibern8_on_idle_allowed(hba)) |
| 8260 | hba->hibern8_on_idle.state = HIBERN8_EXITED; |
| 8261 | } else { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8262 | goto vendor_suspend; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8263 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8264 | } else if (ufshcd_is_link_off(hba)) { |
| 8265 | ret = ufshcd_host_reset_and_restore(hba); |
| 8266 | /* |
| 8267 | * ufshcd_host_reset_and_restore() should have already |
| 8268 | * set the link state as active |
| 8269 | */ |
| 8270 | if (ret || !ufshcd_is_link_active(hba)) |
| 8271 | goto vendor_suspend; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8272 | /* mark link state as hibern8 exited */ |
| 8273 | if (ufshcd_is_hibern8_on_idle_allowed(hba)) |
| 8274 | hba->hibern8_on_idle.state = HIBERN8_EXITED; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8275 | } |
| 8276 | |
| 8277 | if (!ufshcd_is_ufs_dev_active(hba)) { |
| 8278 | ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE); |
| 8279 | if (ret) |
| 8280 | goto set_old_link_state; |
| 8281 | } |
| 8282 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8283 | if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) |
| 8284 | ufshcd_enable_auto_bkops(hba); |
| 8285 | else |
| 8286 | /* |
| 8287 | * If BKOPs operations are urgently needed at this moment then |
| 8288 | * keep auto-bkops enabled or else disable it. |
| 8289 | */ |
| 8290 | ufshcd_urgent_bkops(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8291 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8292 | hba->clk_gating.is_suspended = false; |
| 8293 | hba->hibern8_on_idle.is_suspended = false; |
| 8294 | |
| 8295 | if (hba->clk_scaling.is_allowed) |
| 8296 | ufshcd_resume_clkscaling(hba); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 8297 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8298 | /* Schedule clock gating in case of no access to UFS device yet */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8299 | ufshcd_release_all(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8300 | goto out; |
| 8301 | |
| 8302 | set_old_link_state: |
| 8303 | ufshcd_link_state_transition(hba, old_link_state, 0); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8304 | if (ufshcd_is_link_hibern8(hba) && |
| 8305 | ufshcd_is_hibern8_on_idle_allowed(hba)) |
| 8306 | hba->hibern8_on_idle.state = HIBERN8_ENTERED; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8307 | vendor_suspend: |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 8308 | ufshcd_vops_suspend(hba, pm_op); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8309 | disable_vreg: |
| 8310 | ufshcd_vreg_set_lpm(hba); |
| 8311 | disable_irq_and_vops_clks: |
| 8312 | ufshcd_disable_irq(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8313 | if (hba->clk_scaling.is_allowed) |
| 8314 | ufshcd_suspend_clkscaling(hba); |
| 8315 | ufshcd_disable_clocks(hba, false); |
| 8316 | if (ufshcd_is_clkgating_allowed(hba)) |
| 8317 | hba->clk_gating.state = CLKS_OFF; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8318 | out: |
| 8319 | hba->pm_op_in_progress = 0; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8320 | |
| 8321 | if (ret) |
| 8322 | ufshcd_update_error_stats(hba, UFS_ERR_RESUME); |
| 8323 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8324 | return ret; |
| 8325 | } |
| 8326 | |
| 8327 | /** |
| 8328 | * ufshcd_system_suspend - system suspend routine |
| 8329 | * @hba: per adapter instance |
| 8330 | * @pm_op: runtime PM or system PM |
| 8331 | * |
| 8332 | * Check the description of ufshcd_suspend() function for more details. |
| 8333 | * |
| 8334 | * Returns 0 for success and non-zero for failure |
| 8335 | */ |
| 8336 | int ufshcd_system_suspend(struct ufs_hba *hba) |
| 8337 | { |
| 8338 | int ret = 0; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8339 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8340 | |
| 8341 | if (!hba || !hba->is_powered) |
Dolev Raviv | 233b594 | 2014-10-23 13:25:14 +0300 | [diff] [blame] | 8342 | return 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8343 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8344 | if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) == |
| 8345 | hba->curr_dev_pwr_mode) && |
| 8346 | (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) == |
| 8347 | hba->uic_link_state)) |
| 8348 | goto out; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8349 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8350 | if (pm_runtime_suspended(hba->dev)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8351 | /* |
| 8352 | * UFS device and/or UFS link low power states during runtime |
| 8353 | * suspend seems to be different than what is expected during |
| 8354 | * system suspend. Hence runtime resume the devic & link and |
| 8355 | * let the system suspend low power states to take effect. |
| 8356 | * TODO: If resume takes longer time, we might have optimize |
| 8357 | * it in future by not resuming everything if possible. |
| 8358 | */ |
| 8359 | ret = ufshcd_runtime_resume(hba); |
| 8360 | if (ret) |
| 8361 | goto out; |
| 8362 | } |
| 8363 | |
| 8364 | ret = ufshcd_suspend(hba, UFS_SYSTEM_PM); |
| 8365 | out: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8366 | trace_ufshcd_system_suspend(dev_name(hba->dev), ret, |
| 8367 | ktime_to_us(ktime_sub(ktime_get(), start)), |
| 8368 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
Dolev Raviv | e785060 | 2014-09-25 15:32:36 +0300 | [diff] [blame] | 8369 | if (!ret) |
| 8370 | hba->is_sys_suspended = true; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8371 | return ret; |
| 8372 | } |
| 8373 | EXPORT_SYMBOL(ufshcd_system_suspend); |
| 8374 | |
| 8375 | /** |
| 8376 | * ufshcd_system_resume - system resume routine |
| 8377 | * @hba: per adapter instance |
| 8378 | * |
| 8379 | * Returns 0 for success and non-zero for failure |
| 8380 | */ |
| 8381 | |
| 8382 | int ufshcd_system_resume(struct ufs_hba *hba) |
| 8383 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8384 | int ret = 0; |
| 8385 | ktime_t start = ktime_get(); |
| 8386 | |
| 8387 | if (!hba) |
| 8388 | return -EINVAL; |
| 8389 | |
| 8390 | if (!hba->is_powered || pm_runtime_suspended(hba->dev)) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8391 | /* |
| 8392 | * Let the runtime resume take care of resuming |
| 8393 | * if runtime suspended. |
| 8394 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8395 | goto out; |
| 8396 | else |
| 8397 | ret = ufshcd_resume(hba, UFS_SYSTEM_PM); |
| 8398 | out: |
| 8399 | trace_ufshcd_system_resume(dev_name(hba->dev), ret, |
| 8400 | ktime_to_us(ktime_sub(ktime_get(), start)), |
| 8401 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
| 8402 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8403 | } |
| 8404 | EXPORT_SYMBOL(ufshcd_system_resume); |
| 8405 | |
| 8406 | /** |
| 8407 | * ufshcd_runtime_suspend - runtime suspend routine |
| 8408 | * @hba: per adapter instance |
| 8409 | * |
| 8410 | * Check the description of ufshcd_suspend() function for more details. |
| 8411 | * |
| 8412 | * Returns 0 for success and non-zero for failure |
| 8413 | */ |
| 8414 | int ufshcd_runtime_suspend(struct ufs_hba *hba) |
| 8415 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8416 | int ret = 0; |
| 8417 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8418 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8419 | if (!hba) |
| 8420 | return -EINVAL; |
| 8421 | |
| 8422 | if (!hba->is_powered) |
| 8423 | goto out; |
| 8424 | else |
| 8425 | ret = ufshcd_suspend(hba, UFS_RUNTIME_PM); |
| 8426 | out: |
| 8427 | trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret, |
| 8428 | ktime_to_us(ktime_sub(ktime_get(), start)), |
| 8429 | hba->curr_dev_pwr_mode, |
| 8430 | hba->uic_link_state); |
| 8431 | return ret; |
| 8432 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8433 | } |
| 8434 | EXPORT_SYMBOL(ufshcd_runtime_suspend); |
| 8435 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8436 | /** |
| 8437 | * ufshcd_runtime_resume - runtime resume routine |
| 8438 | * @hba: per adapter instance |
| 8439 | * |
| 8440 | * This function basically brings the UFS device, UniPro link and controller |
| 8441 | * to active state. Following operations are done in this function: |
| 8442 | * |
| 8443 | * 1. Turn on all the controller related clocks |
| 8444 | * 2. Bring the UniPro link out of Hibernate state |
| 8445 | * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device |
| 8446 | * to active state. |
| 8447 | * 4. If auto-bkops is enabled on the device, disable it. |
| 8448 | * |
| 8449 | * So following would be the possible power state after this function return |
| 8450 | * successfully: |
| 8451 | * S1: UFS device in Active state with VCC rail ON |
| 8452 | * UniPro link in Active state |
| 8453 | * All the UFS/UniPro controller clocks are ON |
| 8454 | * |
| 8455 | * Returns 0 for success and non-zero for failure |
| 8456 | */ |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8457 | int ufshcd_runtime_resume(struct ufs_hba *hba) |
| 8458 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8459 | int ret = 0; |
| 8460 | ktime_t start = ktime_get(); |
| 8461 | |
| 8462 | if (!hba) |
| 8463 | return -EINVAL; |
| 8464 | |
| 8465 | if (!hba->is_powered) |
| 8466 | goto out; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8467 | else |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8468 | ret = ufshcd_resume(hba, UFS_RUNTIME_PM); |
| 8469 | out: |
| 8470 | trace_ufshcd_runtime_resume(dev_name(hba->dev), ret, |
| 8471 | ktime_to_us(ktime_sub(ktime_get(), start)), |
| 8472 | hba->curr_dev_pwr_mode, |
| 8473 | hba->uic_link_state); |
| 8474 | return ret; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8475 | } |
| 8476 | EXPORT_SYMBOL(ufshcd_runtime_resume); |
| 8477 | |
| 8478 | int ufshcd_runtime_idle(struct ufs_hba *hba) |
| 8479 | { |
| 8480 | return 0; |
| 8481 | } |
| 8482 | EXPORT_SYMBOL(ufshcd_runtime_idle); |
| 8483 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8484 | static inline ssize_t ufshcd_pm_lvl_store(struct device *dev, |
| 8485 | struct device_attribute *attr, |
| 8486 | const char *buf, size_t count, |
| 8487 | bool rpm) |
| 8488 | { |
| 8489 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 8490 | unsigned long flags, value; |
| 8491 | |
| 8492 | if (kstrtoul(buf, 0, &value)) |
| 8493 | return -EINVAL; |
| 8494 | |
| 8495 | if (value >= UFS_PM_LVL_MAX) |
| 8496 | return -EINVAL; |
| 8497 | |
| 8498 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 8499 | if (rpm) |
| 8500 | hba->rpm_lvl = value; |
| 8501 | else |
| 8502 | hba->spm_lvl = value; |
| 8503 | ufshcd_apply_pm_quirks(hba); |
| 8504 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 8505 | return count; |
| 8506 | } |
| 8507 | |
| 8508 | static ssize_t ufshcd_rpm_lvl_show(struct device *dev, |
| 8509 | struct device_attribute *attr, char *buf) |
| 8510 | { |
| 8511 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 8512 | int curr_len; |
| 8513 | u8 lvl; |
| 8514 | |
| 8515 | curr_len = snprintf(buf, PAGE_SIZE, |
| 8516 | "\nCurrent Runtime PM level [%d] => dev_state [%s] link_state [%s]\n", |
| 8517 | hba->rpm_lvl, |
| 8518 | ufschd_ufs_dev_pwr_mode_to_string( |
| 8519 | ufs_pm_lvl_states[hba->rpm_lvl].dev_state), |
| 8520 | ufschd_uic_link_state_to_string( |
| 8521 | ufs_pm_lvl_states[hba->rpm_lvl].link_state)); |
| 8522 | |
| 8523 | curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len), |
| 8524 | "\nAll available Runtime PM levels info:\n"); |
| 8525 | for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) |
| 8526 | curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len), |
| 8527 | "\tRuntime PM level [%d] => dev_state [%s] link_state [%s]\n", |
| 8528 | lvl, |
| 8529 | ufschd_ufs_dev_pwr_mode_to_string( |
| 8530 | ufs_pm_lvl_states[lvl].dev_state), |
| 8531 | ufschd_uic_link_state_to_string( |
| 8532 | ufs_pm_lvl_states[lvl].link_state)); |
| 8533 | |
| 8534 | return curr_len; |
| 8535 | } |
| 8536 | |
| 8537 | static ssize_t ufshcd_rpm_lvl_store(struct device *dev, |
| 8538 | struct device_attribute *attr, const char *buf, size_t count) |
| 8539 | { |
| 8540 | return ufshcd_pm_lvl_store(dev, attr, buf, count, true); |
| 8541 | } |
| 8542 | |
| 8543 | static void ufshcd_add_rpm_lvl_sysfs_nodes(struct ufs_hba *hba) |
| 8544 | { |
| 8545 | hba->rpm_lvl_attr.show = ufshcd_rpm_lvl_show; |
| 8546 | hba->rpm_lvl_attr.store = ufshcd_rpm_lvl_store; |
| 8547 | sysfs_attr_init(&hba->rpm_lvl_attr.attr); |
| 8548 | hba->rpm_lvl_attr.attr.name = "rpm_lvl"; |
| 8549 | hba->rpm_lvl_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 8550 | if (device_create_file(hba->dev, &hba->rpm_lvl_attr)) |
| 8551 | dev_err(hba->dev, "Failed to create sysfs for rpm_lvl\n"); |
| 8552 | } |
| 8553 | |
| 8554 | static ssize_t ufshcd_spm_lvl_show(struct device *dev, |
| 8555 | struct device_attribute *attr, char *buf) |
| 8556 | { |
| 8557 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 8558 | int curr_len; |
| 8559 | u8 lvl; |
| 8560 | |
| 8561 | curr_len = snprintf(buf, PAGE_SIZE, |
| 8562 | "\nCurrent System PM level [%d] => dev_state [%s] link_state [%s]\n", |
| 8563 | hba->spm_lvl, |
| 8564 | ufschd_ufs_dev_pwr_mode_to_string( |
| 8565 | ufs_pm_lvl_states[hba->spm_lvl].dev_state), |
| 8566 | ufschd_uic_link_state_to_string( |
| 8567 | ufs_pm_lvl_states[hba->spm_lvl].link_state)); |
| 8568 | |
| 8569 | curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len), |
| 8570 | "\nAll available System PM levels info:\n"); |
| 8571 | for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) |
| 8572 | curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len), |
| 8573 | "\tSystem PM level [%d] => dev_state [%s] link_state [%s]\n", |
| 8574 | lvl, |
| 8575 | ufschd_ufs_dev_pwr_mode_to_string( |
| 8576 | ufs_pm_lvl_states[lvl].dev_state), |
| 8577 | ufschd_uic_link_state_to_string( |
| 8578 | ufs_pm_lvl_states[lvl].link_state)); |
| 8579 | |
| 8580 | return curr_len; |
| 8581 | } |
| 8582 | |
| 8583 | static ssize_t ufshcd_spm_lvl_store(struct device *dev, |
| 8584 | struct device_attribute *attr, const char *buf, size_t count) |
| 8585 | { |
| 8586 | return ufshcd_pm_lvl_store(dev, attr, buf, count, false); |
| 8587 | } |
| 8588 | |
| 8589 | static void ufshcd_add_spm_lvl_sysfs_nodes(struct ufs_hba *hba) |
| 8590 | { |
| 8591 | hba->spm_lvl_attr.show = ufshcd_spm_lvl_show; |
| 8592 | hba->spm_lvl_attr.store = ufshcd_spm_lvl_store; |
| 8593 | sysfs_attr_init(&hba->spm_lvl_attr.attr); |
| 8594 | hba->spm_lvl_attr.attr.name = "spm_lvl"; |
| 8595 | hba->spm_lvl_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 8596 | if (device_create_file(hba->dev, &hba->spm_lvl_attr)) |
| 8597 | dev_err(hba->dev, "Failed to create sysfs for spm_lvl\n"); |
| 8598 | } |
| 8599 | |
| 8600 | static inline void ufshcd_add_sysfs_nodes(struct ufs_hba *hba) |
| 8601 | { |
| 8602 | ufshcd_add_rpm_lvl_sysfs_nodes(hba); |
| 8603 | ufshcd_add_spm_lvl_sysfs_nodes(hba); |
| 8604 | } |
| 8605 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8606 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8607 | * ufshcd_shutdown - shutdown routine |
| 8608 | * @hba: per adapter instance |
| 8609 | * |
| 8610 | * This function would power off both UFS device and UFS link. |
| 8611 | * |
| 8612 | * Returns 0 always to allow force shutdown even in case of errors. |
| 8613 | */ |
| 8614 | int ufshcd_shutdown(struct ufs_hba *hba) |
| 8615 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8616 | /* |
| 8617 | * TODO: This function should send the power down notification to |
| 8618 | * UFS device and then power off the UFS link. But we need to be sure |
| 8619 | * that there will not be any new UFS requests issued after this. |
| 8620 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8621 | return 0; |
| 8622 | } |
| 8623 | EXPORT_SYMBOL(ufshcd_shutdown); |
| 8624 | |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 8625 | /* |
| 8626 | * Values permitted 0, 1, 2. |
| 8627 | * 0 -> Disable IO latency histograms (default) |
| 8628 | * 1 -> Enable IO latency histograms |
| 8629 | * 2 -> Zero out IO latency histograms |
| 8630 | */ |
| 8631 | static ssize_t |
| 8632 | latency_hist_store(struct device *dev, struct device_attribute *attr, |
| 8633 | const char *buf, size_t count) |
| 8634 | { |
| 8635 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 8636 | long value; |
| 8637 | |
| 8638 | if (kstrtol(buf, 0, &value)) |
| 8639 | return -EINVAL; |
| 8640 | if (value == BLK_IO_LAT_HIST_ZERO) |
| 8641 | blk_zero_latency_hist(&hba->io_lat_s); |
| 8642 | else if (value == BLK_IO_LAT_HIST_ENABLE || |
| 8643 | value == BLK_IO_LAT_HIST_DISABLE) |
| 8644 | hba->latency_hist_enabled = value; |
| 8645 | return count; |
| 8646 | } |
| 8647 | |
| 8648 | ssize_t |
| 8649 | latency_hist_show(struct device *dev, struct device_attribute *attr, |
| 8650 | char *buf) |
| 8651 | { |
| 8652 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 8653 | |
| 8654 | return blk_latency_hist_show(&hba->io_lat_s, buf); |
| 8655 | } |
| 8656 | |
| 8657 | static DEVICE_ATTR(latency_hist, S_IRUGO | S_IWUSR, |
| 8658 | latency_hist_show, latency_hist_store); |
| 8659 | |
| 8660 | static void |
| 8661 | ufshcd_init_latency_hist(struct ufs_hba *hba) |
| 8662 | { |
| 8663 | if (device_create_file(hba->dev, &dev_attr_latency_hist)) |
| 8664 | dev_err(hba->dev, "Failed to create latency_hist sysfs entry\n"); |
| 8665 | } |
| 8666 | |
| 8667 | static void |
| 8668 | ufshcd_exit_latency_hist(struct ufs_hba *hba) |
| 8669 | { |
| 8670 | device_create_file(hba->dev, &dev_attr_latency_hist); |
| 8671 | } |
| 8672 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8673 | /** |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8674 | * ufshcd_remove - de-allocate SCSI host and host memory space |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8675 | * data structure memory |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8676 | * @hba - per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8677 | */ |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8678 | void ufshcd_remove(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8679 | { |
Akinobu Mita | cfdf9c9 | 2013-07-30 00:36:03 +0530 | [diff] [blame] | 8680 | scsi_remove_host(hba->host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8681 | /* disable interrupts */ |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 8682 | ufshcd_disable_intr(hba, hba->intr_mask); |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 8683 | ufshcd_hba_stop(hba, true); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8684 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8685 | ufshcd_exit_clk_gating(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8686 | ufshcd_exit_hibern8_on_idle(hba); |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 8687 | ufshcd_exit_latency_hist(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8688 | if (ufshcd_is_clkscaling_supported(hba)) { |
| 8689 | device_remove_file(hba->dev, &hba->clk_scaling.enable_attr); |
Kyle Yan | 65be4a5 | 2016-10-31 15:05:00 -0700 | [diff] [blame] | 8690 | if (hba->devfreq) |
| 8691 | devfreq_remove_device(hba->devfreq); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8692 | } |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8693 | ufshcd_hba_exit(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8694 | ufsdbg_remove_debugfs(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8695 | } |
| 8696 | EXPORT_SYMBOL_GPL(ufshcd_remove); |
| 8697 | |
| 8698 | /** |
Yaniv Gardi | 47555a5 | 2015-10-28 13:15:49 +0200 | [diff] [blame] | 8699 | * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA) |
| 8700 | * @hba: pointer to Host Bus Adapter (HBA) |
| 8701 | */ |
| 8702 | void ufshcd_dealloc_host(struct ufs_hba *hba) |
| 8703 | { |
| 8704 | scsi_host_put(hba->host); |
| 8705 | } |
| 8706 | EXPORT_SYMBOL_GPL(ufshcd_dealloc_host); |
| 8707 | |
| 8708 | /** |
Akinobu Mita | ca3d7bf | 2014-07-13 21:24:46 +0900 | [diff] [blame] | 8709 | * ufshcd_set_dma_mask - Set dma mask based on the controller |
| 8710 | * addressing capability |
| 8711 | * @hba: per adapter instance |
| 8712 | * |
| 8713 | * Returns 0 for success, non-zero for failure |
| 8714 | */ |
| 8715 | static int ufshcd_set_dma_mask(struct ufs_hba *hba) |
| 8716 | { |
| 8717 | if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) { |
| 8718 | if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64))) |
| 8719 | return 0; |
| 8720 | } |
| 8721 | return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32)); |
| 8722 | } |
| 8723 | |
| 8724 | /** |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8725 | * ufshcd_alloc_host - allocate Host Bus Adapter (HBA) |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8726 | * @dev: pointer to device handle |
| 8727 | * @hba_handle: driver private handle |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8728 | * Returns 0 on success, non-zero value on failure |
| 8729 | */ |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8730 | int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8731 | { |
| 8732 | struct Scsi_Host *host; |
| 8733 | struct ufs_hba *hba; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8734 | int err = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8735 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8736 | if (!dev) { |
| 8737 | dev_err(dev, |
| 8738 | "Invalid memory reference for dev is NULL\n"); |
| 8739 | err = -ENODEV; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8740 | goto out_error; |
| 8741 | } |
| 8742 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8743 | host = scsi_host_alloc(&ufshcd_driver_template, |
| 8744 | sizeof(struct ufs_hba)); |
| 8745 | if (!host) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8746 | dev_err(dev, "scsi_host_alloc failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8747 | err = -ENOMEM; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8748 | goto out_error; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8749 | } |
| 8750 | hba = shost_priv(host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8751 | hba->host = host; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8752 | hba->dev = dev; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8753 | *hba_handle = hba; |
| 8754 | |
| 8755 | out_error: |
| 8756 | return err; |
| 8757 | } |
| 8758 | EXPORT_SYMBOL(ufshcd_alloc_host); |
| 8759 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8760 | /** |
| 8761 | * ufshcd_is_devfreq_scaling_required - check if scaling is required or not |
| 8762 | * @hba: per adapter instance |
| 8763 | * @scale_up: True if scaling up and false if scaling down |
| 8764 | * |
| 8765 | * Returns true if scaling is required, false otherwise. |
| 8766 | */ |
| 8767 | static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba, |
| 8768 | bool scale_up) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 8769 | { |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 8770 | struct ufs_clk_info *clki; |
| 8771 | struct list_head *head = &hba->clk_list_head; |
| 8772 | |
| 8773 | if (!head || list_empty(head)) |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8774 | return false; |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 8775 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 8776 | list_for_each_entry(clki, head, list) { |
| 8777 | if (!IS_ERR_OR_NULL(clki->clk)) { |
| 8778 | if (scale_up && clki->max_freq) { |
| 8779 | if (clki->curr_freq == clki->max_freq) |
| 8780 | continue; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8781 | return true; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 8782 | } else if (!scale_up && clki->min_freq) { |
| 8783 | if (clki->curr_freq == clki->min_freq) |
| 8784 | continue; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8785 | return true; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 8786 | } |
| 8787 | } |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 8788 | } |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 8789 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8790 | return false; |
| 8791 | } |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 8792 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8793 | /** |
| 8794 | * ufshcd_scale_gear - scale up/down UFS gear |
| 8795 | * @hba: per adapter instance |
| 8796 | * @scale_up: True for scaling up gear and false for scaling down |
| 8797 | * |
| 8798 | * Returns 0 for success, |
| 8799 | * Returns -EBUSY if scaling can't happen at this time |
| 8800 | * Returns non-zero for any other errors |
| 8801 | */ |
| 8802 | static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up) |
| 8803 | { |
| 8804 | int ret = 0; |
| 8805 | struct ufs_pa_layer_attr new_pwr_info; |
| 8806 | u32 scale_down_gear = ufshcd_vops_get_scale_down_gear(hba); |
| 8807 | |
| 8808 | BUG_ON(!hba->clk_scaling.saved_pwr_info.is_valid); |
| 8809 | |
| 8810 | if (scale_up) { |
| 8811 | memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info.info, |
| 8812 | sizeof(struct ufs_pa_layer_attr)); |
| 8813 | } else { |
| 8814 | memcpy(&new_pwr_info, &hba->pwr_info, |
| 8815 | sizeof(struct ufs_pa_layer_attr)); |
| 8816 | |
| 8817 | if (hba->pwr_info.gear_tx > scale_down_gear |
| 8818 | || hba->pwr_info.gear_rx > scale_down_gear) { |
| 8819 | /* save the current power mode */ |
| 8820 | memcpy(&hba->clk_scaling.saved_pwr_info.info, |
| 8821 | &hba->pwr_info, |
| 8822 | sizeof(struct ufs_pa_layer_attr)); |
| 8823 | |
| 8824 | /* scale down gear */ |
| 8825 | new_pwr_info.gear_tx = scale_down_gear; |
| 8826 | new_pwr_info.gear_rx = scale_down_gear; |
| 8827 | if (!(hba->dev_quirks & UFS_DEVICE_NO_FASTAUTO)) { |
| 8828 | new_pwr_info.pwr_tx = FASTAUTO_MODE; |
| 8829 | new_pwr_info.pwr_rx = FASTAUTO_MODE; |
| 8830 | } |
| 8831 | } |
| 8832 | } |
| 8833 | |
| 8834 | ret = ufshcd_change_power_mode(hba, &new_pwr_info); |
| 8835 | |
| 8836 | if (ret) |
| 8837 | dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d), scale_up = %d", |
| 8838 | __func__, ret, |
| 8839 | hba->pwr_info.gear_tx, hba->pwr_info.gear_rx, |
| 8840 | new_pwr_info.gear_tx, new_pwr_info.gear_rx, |
| 8841 | scale_up); |
| 8842 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 8843 | return ret; |
| 8844 | } |
| 8845 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8846 | static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba) |
| 8847 | { |
| 8848 | #define DOORBELL_CLR_TOUT_US (1000 * 1000) /* 1 sec */ |
| 8849 | int ret = 0; |
| 8850 | /* |
| 8851 | * make sure that there are no outstanding requests when |
| 8852 | * clock scaling is in progress |
| 8853 | */ |
| 8854 | ufshcd_scsi_block_requests(hba); |
| 8855 | down_write(&hba->clk_scaling_lock); |
| 8856 | if (ufshcd_wait_for_doorbell_clr(hba, DOORBELL_CLR_TOUT_US)) { |
| 8857 | ret = -EBUSY; |
| 8858 | up_write(&hba->clk_scaling_lock); |
| 8859 | ufshcd_scsi_unblock_requests(hba); |
| 8860 | } |
| 8861 | |
| 8862 | return ret; |
| 8863 | } |
| 8864 | |
| 8865 | static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba) |
| 8866 | { |
| 8867 | up_write(&hba->clk_scaling_lock); |
| 8868 | ufshcd_scsi_unblock_requests(hba); |
| 8869 | } |
| 8870 | |
| 8871 | /** |
| 8872 | * ufshcd_devfreq_scale - scale up/down UFS clocks and gear |
| 8873 | * @hba: per adapter instance |
| 8874 | * @scale_up: True for scaling up and false for scalin down |
| 8875 | * |
| 8876 | * Returns 0 for success, |
| 8877 | * Returns -EBUSY if scaling can't happen at this time |
| 8878 | * Returns non-zero for any other errors |
| 8879 | */ |
| 8880 | static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up) |
| 8881 | { |
| 8882 | int ret = 0; |
| 8883 | |
| 8884 | /* let's not get into low power until clock scaling is completed */ |
| 8885 | ufshcd_hold_all(hba); |
| 8886 | |
| 8887 | ret = ufshcd_clock_scaling_prepare(hba); |
| 8888 | if (ret) |
| 8889 | goto out; |
| 8890 | |
| 8891 | /* scale down the gear before scaling down clocks */ |
| 8892 | if (!scale_up) { |
| 8893 | ret = ufshcd_scale_gear(hba, false); |
| 8894 | if (ret) |
| 8895 | goto clk_scaling_unprepare; |
| 8896 | } |
| 8897 | |
| 8898 | ret = ufshcd_scale_clks(hba, scale_up); |
| 8899 | if (ret) |
| 8900 | goto scale_up_gear; |
| 8901 | |
| 8902 | /* scale up the gear after scaling up clocks */ |
| 8903 | if (scale_up) { |
| 8904 | ret = ufshcd_scale_gear(hba, true); |
| 8905 | if (ret) { |
| 8906 | ufshcd_scale_clks(hba, false); |
| 8907 | goto clk_scaling_unprepare; |
| 8908 | } |
| 8909 | } |
| 8910 | |
| 8911 | if (!ret) { |
| 8912 | hba->clk_scaling.is_scaled_up = scale_up; |
| 8913 | if (scale_up) |
| 8914 | hba->clk_gating.delay_ms = |
| 8915 | hba->clk_gating.delay_ms_perf; |
| 8916 | else |
| 8917 | hba->clk_gating.delay_ms = |
| 8918 | hba->clk_gating.delay_ms_pwr_save; |
| 8919 | } |
| 8920 | |
| 8921 | goto clk_scaling_unprepare; |
| 8922 | |
| 8923 | scale_up_gear: |
| 8924 | if (!scale_up) |
| 8925 | ufshcd_scale_gear(hba, true); |
| 8926 | clk_scaling_unprepare: |
| 8927 | ufshcd_clock_scaling_unprepare(hba); |
| 8928 | out: |
| 8929 | ufshcd_release_all(hba); |
| 8930 | return ret; |
| 8931 | } |
| 8932 | |
| 8933 | static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba) |
| 8934 | { |
| 8935 | unsigned long flags; |
| 8936 | |
| 8937 | devfreq_suspend_device(hba->devfreq); |
| 8938 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 8939 | hba->clk_scaling.window_start_t = 0; |
| 8940 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 8941 | } |
| 8942 | |
| 8943 | static void ufshcd_suspend_clkscaling(struct ufs_hba *hba) |
| 8944 | { |
| 8945 | unsigned long flags; |
| 8946 | bool suspend = false; |
| 8947 | |
| 8948 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 8949 | return; |
| 8950 | |
| 8951 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 8952 | if (!hba->clk_scaling.is_suspended) { |
| 8953 | suspend = true; |
| 8954 | hba->clk_scaling.is_suspended = true; |
| 8955 | } |
| 8956 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 8957 | |
| 8958 | if (suspend) |
| 8959 | __ufshcd_suspend_clkscaling(hba); |
| 8960 | } |
| 8961 | |
| 8962 | static void ufshcd_resume_clkscaling(struct ufs_hba *hba) |
| 8963 | { |
| 8964 | unsigned long flags; |
| 8965 | bool resume = false; |
| 8966 | |
| 8967 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 8968 | return; |
| 8969 | |
| 8970 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 8971 | if (hba->clk_scaling.is_suspended) { |
| 8972 | resume = true; |
| 8973 | hba->clk_scaling.is_suspended = false; |
| 8974 | } |
| 8975 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 8976 | |
| 8977 | if (resume) |
| 8978 | devfreq_resume_device(hba->devfreq); |
| 8979 | } |
| 8980 | |
| 8981 | static ssize_t ufshcd_clkscale_enable_show(struct device *dev, |
| 8982 | struct device_attribute *attr, char *buf) |
| 8983 | { |
| 8984 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 8985 | |
| 8986 | return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_scaling.is_allowed); |
| 8987 | } |
| 8988 | |
| 8989 | static ssize_t ufshcd_clkscale_enable_store(struct device *dev, |
| 8990 | struct device_attribute *attr, const char *buf, size_t count) |
| 8991 | { |
| 8992 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 8993 | u32 value; |
| 8994 | int err; |
| 8995 | |
| 8996 | if (kstrtou32(buf, 0, &value)) |
| 8997 | return -EINVAL; |
| 8998 | |
| 8999 | value = !!value; |
| 9000 | if (value == hba->clk_scaling.is_allowed) |
| 9001 | goto out; |
| 9002 | |
| 9003 | pm_runtime_get_sync(hba->dev); |
| 9004 | ufshcd_hold(hba, false); |
| 9005 | |
| 9006 | cancel_work_sync(&hba->clk_scaling.suspend_work); |
| 9007 | cancel_work_sync(&hba->clk_scaling.resume_work); |
| 9008 | |
| 9009 | hba->clk_scaling.is_allowed = value; |
| 9010 | |
| 9011 | if (value) { |
| 9012 | ufshcd_resume_clkscaling(hba); |
| 9013 | } else { |
| 9014 | ufshcd_suspend_clkscaling(hba); |
| 9015 | err = ufshcd_devfreq_scale(hba, true); |
| 9016 | if (err) |
| 9017 | dev_err(hba->dev, "%s: failed to scale clocks up %d\n", |
| 9018 | __func__, err); |
| 9019 | } |
| 9020 | |
| 9021 | ufshcd_release(hba, false); |
| 9022 | pm_runtime_put_sync(hba->dev); |
| 9023 | out: |
| 9024 | return count; |
| 9025 | } |
| 9026 | |
| 9027 | static void ufshcd_clk_scaling_suspend_work(struct work_struct *work) |
| 9028 | { |
| 9029 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 9030 | clk_scaling.suspend_work); |
| 9031 | unsigned long irq_flags; |
| 9032 | |
| 9033 | spin_lock_irqsave(hba->host->host_lock, irq_flags); |
| 9034 | if (hba->clk_scaling.active_reqs || hba->clk_scaling.is_suspended) { |
| 9035 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 9036 | return; |
| 9037 | } |
| 9038 | hba->clk_scaling.is_suspended = true; |
| 9039 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 9040 | |
| 9041 | __ufshcd_suspend_clkscaling(hba); |
| 9042 | } |
| 9043 | |
| 9044 | static void ufshcd_clk_scaling_resume_work(struct work_struct *work) |
| 9045 | { |
| 9046 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 9047 | clk_scaling.resume_work); |
| 9048 | unsigned long irq_flags; |
| 9049 | |
| 9050 | spin_lock_irqsave(hba->host->host_lock, irq_flags); |
| 9051 | if (!hba->clk_scaling.is_suspended) { |
| 9052 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 9053 | return; |
| 9054 | } |
| 9055 | hba->clk_scaling.is_suspended = false; |
| 9056 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 9057 | |
| 9058 | devfreq_resume_device(hba->devfreq); |
| 9059 | } |
| 9060 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9061 | static int ufshcd_devfreq_target(struct device *dev, |
| 9062 | unsigned long *freq, u32 flags) |
| 9063 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9064 | int ret = 0; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9065 | struct ufs_hba *hba = dev_get_drvdata(dev); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9066 | unsigned long irq_flags; |
| 9067 | ktime_t start; |
| 9068 | bool scale_up, sched_clk_scaling_suspend_work = false; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9069 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9070 | if (!ufshcd_is_clkscaling_supported(hba)) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9071 | return -EINVAL; |
| 9072 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9073 | if ((*freq > 0) && (*freq < UINT_MAX)) { |
| 9074 | dev_err(hba->dev, "%s: invalid freq = %lu\n", __func__, *freq); |
| 9075 | return -EINVAL; |
| 9076 | } |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9077 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9078 | spin_lock_irqsave(hba->host->host_lock, irq_flags); |
| 9079 | if (ufshcd_eh_in_progress(hba)) { |
| 9080 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 9081 | return 0; |
| 9082 | } |
| 9083 | |
| 9084 | if (!hba->clk_scaling.active_reqs) |
| 9085 | sched_clk_scaling_suspend_work = true; |
| 9086 | |
| 9087 | scale_up = (*freq == UINT_MAX) ? true : false; |
| 9088 | if (!ufshcd_is_devfreq_scaling_required(hba, scale_up)) { |
| 9089 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 9090 | ret = 0; |
| 9091 | goto out; /* no state change required */ |
| 9092 | } |
| 9093 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 9094 | |
| 9095 | start = ktime_get(); |
| 9096 | ret = ufshcd_devfreq_scale(hba, scale_up); |
| 9097 | trace_ufshcd_profile_clk_scaling(dev_name(hba->dev), |
| 9098 | (scale_up ? "up" : "down"), |
| 9099 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
| 9100 | |
| 9101 | out: |
| 9102 | if (sched_clk_scaling_suspend_work) |
| 9103 | queue_work(hba->clk_scaling.workq, |
| 9104 | &hba->clk_scaling.suspend_work); |
| 9105 | |
| 9106 | return ret; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9107 | } |
| 9108 | |
| 9109 | static int ufshcd_devfreq_get_dev_status(struct device *dev, |
| 9110 | struct devfreq_dev_status *stat) |
| 9111 | { |
| 9112 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 9113 | struct ufs_clk_scaling *scaling = &hba->clk_scaling; |
| 9114 | unsigned long flags; |
| 9115 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9116 | if (!ufshcd_is_clkscaling_supported(hba)) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9117 | return -EINVAL; |
| 9118 | |
| 9119 | memset(stat, 0, sizeof(*stat)); |
| 9120 | |
| 9121 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 9122 | if (!scaling->window_start_t) |
| 9123 | goto start_window; |
| 9124 | |
| 9125 | if (scaling->is_busy_started) |
| 9126 | scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(), |
| 9127 | scaling->busy_start_t)); |
| 9128 | |
| 9129 | stat->total_time = jiffies_to_usecs((long)jiffies - |
| 9130 | (long)scaling->window_start_t); |
| 9131 | stat->busy_time = scaling->tot_busy_t; |
| 9132 | start_window: |
| 9133 | scaling->window_start_t = jiffies; |
| 9134 | scaling->tot_busy_t = 0; |
| 9135 | |
| 9136 | if (hba->outstanding_reqs) { |
| 9137 | scaling->busy_start_t = ktime_get(); |
| 9138 | scaling->is_busy_started = true; |
| 9139 | } else { |
| 9140 | scaling->busy_start_t = ktime_set(0, 0); |
| 9141 | scaling->is_busy_started = false; |
| 9142 | } |
| 9143 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 9144 | return 0; |
| 9145 | } |
| 9146 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9147 | #if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND) |
| 9148 | static struct devfreq_simple_ondemand_data ufshcd_ondemand_data = { |
| 9149 | .upthreshold = 35, |
| 9150 | .downdifferential = 30, |
| 9151 | .simple_scaling = 1, |
| 9152 | }; |
| 9153 | |
| 9154 | static void *gov_data = &ufshcd_ondemand_data; |
| 9155 | #else |
| 9156 | static void *gov_data; |
| 9157 | #endif |
| 9158 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9159 | static struct devfreq_dev_profile ufs_devfreq_profile = { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9160 | .polling_ms = 40, |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9161 | .target = ufshcd_devfreq_target, |
| 9162 | .get_dev_status = ufshcd_devfreq_get_dev_status, |
| 9163 | }; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9164 | static void ufshcd_clkscaling_init_sysfs(struct ufs_hba *hba) |
| 9165 | { |
| 9166 | hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show; |
| 9167 | hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store; |
| 9168 | sysfs_attr_init(&hba->clk_scaling.enable_attr.attr); |
| 9169 | hba->clk_scaling.enable_attr.attr.name = "clkscale_enable"; |
| 9170 | hba->clk_scaling.enable_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 9171 | if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr)) |
| 9172 | dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n"); |
| 9173 | } |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9174 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9175 | static void ufshcd_init_lanes_per_dir(struct ufs_hba *hba) |
| 9176 | { |
| 9177 | struct device *dev = hba->dev; |
| 9178 | int ret; |
| 9179 | |
| 9180 | ret = of_property_read_u32(dev->of_node, "lanes-per-direction", |
| 9181 | &hba->lanes_per_direction); |
| 9182 | if (ret) { |
| 9183 | dev_dbg(hba->dev, |
| 9184 | "%s: failed to read lanes-per-direction, ret=%d\n", |
| 9185 | __func__, ret); |
| 9186 | hba->lanes_per_direction = UFSHCD_DEFAULT_LANES_PER_DIRECTION; |
| 9187 | } |
| 9188 | } |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 9189 | /** |
| 9190 | * ufshcd_init - Driver initialization routine |
| 9191 | * @hba: per-adapter instance |
| 9192 | * @mmio_base: base register address |
| 9193 | * @irq: Interrupt line of device |
| 9194 | * Returns 0 on success, non-zero value on failure |
| 9195 | */ |
| 9196 | int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq) |
| 9197 | { |
| 9198 | int err; |
| 9199 | struct Scsi_Host *host = hba->host; |
| 9200 | struct device *dev = hba->dev; |
| 9201 | |
| 9202 | if (!mmio_base) { |
| 9203 | dev_err(hba->dev, |
| 9204 | "Invalid memory reference for mmio_base is NULL\n"); |
| 9205 | err = -ENODEV; |
| 9206 | goto out_error; |
| 9207 | } |
| 9208 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9209 | hba->mmio_base = mmio_base; |
| 9210 | hba->irq = irq; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9211 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9212 | ufshcd_init_lanes_per_dir(hba); |
| 9213 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 9214 | err = ufshcd_hba_init(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 9215 | if (err) |
| 9216 | goto out_error; |
| 9217 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9218 | /* Read capabilities registers */ |
| 9219 | ufshcd_hba_capabilities(hba); |
| 9220 | |
| 9221 | /* Get UFS version supported by the controller */ |
| 9222 | hba->ufs_version = ufshcd_get_ufs_version(hba); |
| 9223 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9224 | /* print error message if ufs_version is not valid */ |
| 9225 | if ((hba->ufs_version != UFSHCI_VERSION_10) && |
| 9226 | (hba->ufs_version != UFSHCI_VERSION_11) && |
| 9227 | (hba->ufs_version != UFSHCI_VERSION_20) && |
| 9228 | (hba->ufs_version != UFSHCI_VERSION_21)) |
| 9229 | dev_err(hba->dev, "invalid UFS version 0x%x\n", |
| 9230 | hba->ufs_version); |
| 9231 | |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 9232 | /* Get Interrupt bit mask per version */ |
| 9233 | hba->intr_mask = ufshcd_get_intr_mask(hba); |
| 9234 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9235 | /* Enable debug prints */ |
| 9236 | hba->ufshcd_dbg_print = DEFAULT_UFSHCD_DBG_PRINT_EN; |
| 9237 | |
Akinobu Mita | ca3d7bf | 2014-07-13 21:24:46 +0900 | [diff] [blame] | 9238 | err = ufshcd_set_dma_mask(hba); |
| 9239 | if (err) { |
| 9240 | dev_err(hba->dev, "set dma mask failed\n"); |
| 9241 | goto out_disable; |
| 9242 | } |
| 9243 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9244 | /* Allocate memory for host memory space */ |
| 9245 | err = ufshcd_memory_alloc(hba); |
| 9246 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9247 | dev_err(hba->dev, "Memory allocation failed\n"); |
| 9248 | goto out_disable; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9249 | } |
| 9250 | |
| 9251 | /* Configure LRB */ |
| 9252 | ufshcd_host_memory_configure(hba); |
| 9253 | |
| 9254 | host->can_queue = hba->nutrs; |
| 9255 | host->cmd_per_lun = hba->nutrs; |
| 9256 | host->max_id = UFSHCD_MAX_ID; |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 9257 | host->max_lun = UFS_MAX_LUNS; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9258 | host->max_channel = UFSHCD_MAX_CHANNEL; |
| 9259 | host->unique_id = host->host_no; |
| 9260 | host->max_cmd_len = MAX_CDB_SIZE; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9261 | host->set_dbd_for_caching = 1; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9262 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 9263 | hba->max_pwr_info.is_valid = false; |
| 9264 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9265 | /* Initailize wait queue for task management */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 9266 | init_waitqueue_head(&hba->tm_wq); |
| 9267 | init_waitqueue_head(&hba->tm_tag_wq); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9268 | |
| 9269 | /* Initialize work queues */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 9270 | INIT_WORK(&hba->eh_work, ufshcd_err_handler); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 9271 | INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9272 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 9273 | /* Initialize UIC command mutex */ |
| 9274 | mutex_init(&hba->uic_cmd_mutex); |
| 9275 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 9276 | /* Initialize mutex for device management commands */ |
| 9277 | mutex_init(&hba->dev_cmd.lock); |
| 9278 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9279 | init_rwsem(&hba->clk_scaling_lock); |
| 9280 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 9281 | /* Initialize device management tag acquire wait queue */ |
| 9282 | init_waitqueue_head(&hba->dev_cmd.tag_wq); |
| 9283 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 9284 | ufshcd_init_clk_gating(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9285 | ufshcd_init_hibern8_on_idle(hba); |
Yaniv Gardi | 199ef13 | 2016-03-10 17:37:06 +0200 | [diff] [blame] | 9286 | |
| 9287 | /* |
| 9288 | * In order to avoid any spurious interrupt immediately after |
| 9289 | * registering UFS controller interrupt handler, clear any pending UFS |
| 9290 | * interrupt status and disable all the UFS interrupts. |
| 9291 | */ |
| 9292 | ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS), |
| 9293 | REG_INTERRUPT_STATUS); |
| 9294 | ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE); |
| 9295 | /* |
| 9296 | * Make sure that UFS interrupts are disabled and any pending interrupt |
| 9297 | * status is cleared before registering UFS interrupt handler. |
| 9298 | */ |
| 9299 | mb(); |
| 9300 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9301 | /* IRQ registration */ |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 9302 | err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9303 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9304 | dev_err(hba->dev, "request irq failed\n"); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 9305 | goto exit_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9306 | } else { |
| 9307 | hba->is_irq_enabled = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9308 | } |
| 9309 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9310 | err = scsi_add_host(host, hba->dev); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9311 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9312 | dev_err(hba->dev, "scsi_add_host failed\n"); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 9313 | goto exit_gating; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9314 | } |
| 9315 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 9316 | /* Host controller enable */ |
| 9317 | err = ufshcd_hba_enable(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9318 | if (err) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 9319 | dev_err(hba->dev, "Host controller enable failed\n"); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9320 | ufshcd_print_host_regs(hba); |
| 9321 | ufshcd_print_host_state(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9322 | goto out_remove_scsi_host; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9323 | } |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 9324 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9325 | if (ufshcd_is_clkscaling_supported(hba)) { |
| 9326 | char wq_name[sizeof("ufs_clkscaling_00")]; |
| 9327 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9328 | hba->devfreq = devfreq_add_device(dev, &ufs_devfreq_profile, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9329 | "simple_ondemand", gov_data); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9330 | if (IS_ERR(hba->devfreq)) { |
| 9331 | dev_err(hba->dev, "Unable to register with devfreq %ld\n", |
| 9332 | PTR_ERR(hba->devfreq)); |
Wei Yongjun | 73811c9 | 2016-09-28 14:49:42 +0000 | [diff] [blame] | 9333 | err = PTR_ERR(hba->devfreq); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9334 | goto out_remove_scsi_host; |
| 9335 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9336 | hba->clk_scaling.is_suspended = false; |
| 9337 | |
| 9338 | INIT_WORK(&hba->clk_scaling.suspend_work, |
| 9339 | ufshcd_clk_scaling_suspend_work); |
| 9340 | INIT_WORK(&hba->clk_scaling.resume_work, |
| 9341 | ufshcd_clk_scaling_resume_work); |
| 9342 | |
| 9343 | snprintf(wq_name, ARRAY_SIZE(wq_name), "ufs_clkscaling_%d", |
| 9344 | host->host_no); |
| 9345 | hba->clk_scaling.workq = create_singlethread_workqueue(wq_name); |
| 9346 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9347 | /* Suspend devfreq until the UFS device is detected */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9348 | ufshcd_suspend_clkscaling(hba); |
| 9349 | ufshcd_clkscaling_init_sysfs(hba); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9350 | } |
| 9351 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9352 | /* |
| 9353 | * If rpm_lvl and and spm_lvl are not already set to valid levels, |
| 9354 | * set the default power management level for UFS runtime and system |
| 9355 | * suspend. Default power saving mode selected is keeping UFS link in |
| 9356 | * Hibern8 state and UFS device in sleep. |
| 9357 | */ |
| 9358 | if (!ufshcd_is_valid_pm_lvl(hba->rpm_lvl)) |
| 9359 | hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state( |
| 9360 | UFS_SLEEP_PWR_MODE, |
| 9361 | UIC_LINK_HIBERN8_STATE); |
| 9362 | if (!ufshcd_is_valid_pm_lvl(hba->spm_lvl)) |
| 9363 | hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state( |
| 9364 | UFS_SLEEP_PWR_MODE, |
| 9365 | UIC_LINK_HIBERN8_STATE); |
| 9366 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 9367 | /* Hold auto suspend until async scan completes */ |
| 9368 | pm_runtime_get_sync(dev); |
| 9369 | |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 9370 | ufshcd_init_latency_hist(hba); |
| 9371 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9372 | /* |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9373 | * We are assuming that device wasn't put in sleep/power-down |
| 9374 | * state exclusively during the boot stage before kernel. |
| 9375 | * This assumption helps avoid doing link startup twice during |
| 9376 | * ufshcd_probe_hba(). |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9377 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9378 | ufshcd_set_ufs_dev_active(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9379 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 9380 | async_schedule(ufshcd_async_scan, hba); |
| 9381 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9382 | ufsdbg_add_debugfs(hba); |
| 9383 | |
| 9384 | ufshcd_add_sysfs_nodes(hba); |
| 9385 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9386 | return 0; |
| 9387 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9388 | out_remove_scsi_host: |
| 9389 | scsi_remove_host(hba->host); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 9390 | exit_gating: |
| 9391 | ufshcd_exit_clk_gating(hba); |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 9392 | ufshcd_exit_latency_hist(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9393 | out_disable: |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9394 | hba->is_irq_enabled = false; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 9395 | ufshcd_hba_exit(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9396 | out_error: |
| 9397 | return err; |
| 9398 | } |
| 9399 | EXPORT_SYMBOL_GPL(ufshcd_init); |
| 9400 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9401 | MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>"); |
| 9402 | MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>"); |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 9403 | MODULE_DESCRIPTION("Generic UFS host controller driver Core"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9404 | MODULE_LICENSE("GPL"); |
| 9405 | MODULE_VERSION(UFSHCD_DRIVER_VERSION); |