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 |
Stephen Boyd | 9bc70c3 | 2017-03-01 16:58:38 -0800 | [diff] [blame] | 6 | * Copyright (c) 2013-2017, 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" |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 50 | #include "ufs-qcom.h" |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 51 | |
| 52 | #define CREATE_TRACE_POINTS |
| 53 | #include <trace/events/ufs.h> |
| 54 | |
| 55 | #ifdef CONFIG_DEBUG_FS |
| 56 | |
| 57 | static int ufshcd_tag_req_type(struct request *rq) |
| 58 | { |
| 59 | int rq_type = TS_WRITE; |
| 60 | |
| 61 | if (!rq || !(rq->cmd_type & REQ_TYPE_FS)) |
| 62 | rq_type = TS_NOT_SUPPORTED; |
| 63 | else if (rq->cmd_flags & REQ_PREFLUSH) |
| 64 | rq_type = TS_FLUSH; |
| 65 | else if (rq_data_dir(rq) == READ) |
| 66 | rq_type = (rq->cmd_flags & REQ_URGENT) ? |
| 67 | TS_URGENT_READ : TS_READ; |
| 68 | else if (rq->cmd_flags & REQ_URGENT) |
| 69 | rq_type = TS_URGENT_WRITE; |
| 70 | |
| 71 | return rq_type; |
| 72 | } |
| 73 | |
| 74 | static void ufshcd_update_error_stats(struct ufs_hba *hba, int type) |
| 75 | { |
| 76 | ufsdbg_set_err_state(hba); |
| 77 | if (type < UFS_ERR_MAX) |
| 78 | hba->ufs_stats.err_stats[type]++; |
| 79 | } |
| 80 | |
| 81 | static void ufshcd_update_tag_stats(struct ufs_hba *hba, int tag) |
| 82 | { |
| 83 | struct request *rq = |
| 84 | hba->lrb[tag].cmd ? hba->lrb[tag].cmd->request : NULL; |
| 85 | u64 **tag_stats = hba->ufs_stats.tag_stats; |
| 86 | int rq_type; |
| 87 | |
| 88 | if (!hba->ufs_stats.enabled) |
| 89 | return; |
| 90 | |
| 91 | tag_stats[tag][TS_TAG]++; |
| 92 | if (!rq || !(rq->cmd_type & REQ_TYPE_FS)) |
| 93 | return; |
| 94 | |
| 95 | WARN_ON(hba->ufs_stats.q_depth > hba->nutrs); |
| 96 | rq_type = ufshcd_tag_req_type(rq); |
| 97 | if (!(rq_type < 0 || rq_type > TS_NUM_STATS)) |
| 98 | tag_stats[hba->ufs_stats.q_depth++][rq_type]++; |
| 99 | } |
| 100 | |
| 101 | static void ufshcd_update_tag_stats_completion(struct ufs_hba *hba, |
| 102 | struct scsi_cmnd *cmd) |
| 103 | { |
| 104 | struct request *rq = cmd ? cmd->request : NULL; |
| 105 | |
| 106 | if (rq && rq->cmd_type & REQ_TYPE_FS) |
| 107 | hba->ufs_stats.q_depth--; |
| 108 | } |
| 109 | |
| 110 | static void update_req_stats(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 111 | { |
| 112 | int rq_type; |
| 113 | struct request *rq = lrbp->cmd ? lrbp->cmd->request : NULL; |
| 114 | s64 delta = ktime_us_delta(lrbp->complete_time_stamp, |
| 115 | lrbp->issue_time_stamp); |
| 116 | |
| 117 | /* update general request statistics */ |
| 118 | if (hba->ufs_stats.req_stats[TS_TAG].count == 0) |
| 119 | hba->ufs_stats.req_stats[TS_TAG].min = delta; |
| 120 | hba->ufs_stats.req_stats[TS_TAG].count++; |
| 121 | hba->ufs_stats.req_stats[TS_TAG].sum += delta; |
| 122 | if (delta > hba->ufs_stats.req_stats[TS_TAG].max) |
| 123 | hba->ufs_stats.req_stats[TS_TAG].max = delta; |
| 124 | if (delta < hba->ufs_stats.req_stats[TS_TAG].min) |
| 125 | hba->ufs_stats.req_stats[TS_TAG].min = delta; |
| 126 | |
| 127 | rq_type = ufshcd_tag_req_type(rq); |
| 128 | if (rq_type == TS_NOT_SUPPORTED) |
| 129 | return; |
| 130 | |
| 131 | /* update request type specific statistics */ |
| 132 | if (hba->ufs_stats.req_stats[rq_type].count == 0) |
| 133 | hba->ufs_stats.req_stats[rq_type].min = delta; |
| 134 | hba->ufs_stats.req_stats[rq_type].count++; |
| 135 | hba->ufs_stats.req_stats[rq_type].sum += delta; |
| 136 | if (delta > hba->ufs_stats.req_stats[rq_type].max) |
| 137 | hba->ufs_stats.req_stats[rq_type].max = delta; |
| 138 | if (delta < hba->ufs_stats.req_stats[rq_type].min) |
| 139 | hba->ufs_stats.req_stats[rq_type].min = delta; |
| 140 | } |
| 141 | |
| 142 | static void |
| 143 | ufshcd_update_query_stats(struct ufs_hba *hba, enum query_opcode opcode, u8 idn) |
| 144 | { |
| 145 | if (opcode < UPIU_QUERY_OPCODE_MAX && idn < MAX_QUERY_IDN) |
| 146 | hba->ufs_stats.query_stats_arr[opcode][idn]++; |
| 147 | } |
| 148 | |
| 149 | #else |
| 150 | static inline void ufshcd_update_tag_stats(struct ufs_hba *hba, int tag) |
| 151 | { |
| 152 | } |
| 153 | |
| 154 | static inline void ufshcd_update_tag_stats_completion(struct ufs_hba *hba, |
| 155 | struct scsi_cmnd *cmd) |
| 156 | { |
| 157 | } |
| 158 | |
| 159 | static inline void ufshcd_update_error_stats(struct ufs_hba *hba, int type) |
| 160 | { |
| 161 | } |
| 162 | |
| 163 | static inline |
| 164 | void update_req_stats(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 165 | { |
| 166 | } |
| 167 | |
| 168 | static inline |
| 169 | void ufshcd_update_query_stats(struct ufs_hba *hba, |
| 170 | enum query_opcode opcode, u8 idn) |
| 171 | { |
| 172 | } |
| 173 | #endif |
| 174 | |
| 175 | #define UFSHCD_REQ_SENSE_SIZE 18 |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 176 | |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 177 | #define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\ |
| 178 | UTP_TASK_REQ_COMPL |\ |
| 179 | UFSHCD_ERROR_MASK) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 180 | /* UIC command timeout, unit: ms */ |
| 181 | #define UIC_CMD_TIMEOUT 500 |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 182 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 183 | /* NOP OUT retries waiting for NOP IN response */ |
| 184 | #define NOP_OUT_RETRIES 10 |
| 185 | /* Timeout after 30 msecs if NOP OUT hangs without response */ |
| 186 | #define NOP_OUT_TIMEOUT 30 /* msecs */ |
| 187 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 188 | /* Query request retries */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 189 | #define QUERY_REQ_RETRIES 3 |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 190 | /* Query request timeout */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 191 | #define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */ |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 192 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 193 | /* Task management command timeout */ |
| 194 | #define TM_CMD_TIMEOUT 100 /* msecs */ |
| 195 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 196 | /* maximum number of retries for a general UIC command */ |
| 197 | #define UFS_UIC_COMMAND_RETRIES 3 |
| 198 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 199 | /* maximum number of link-startup retries */ |
| 200 | #define DME_LINKSTARTUP_RETRIES 3 |
| 201 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 202 | /* Maximum retries for Hibern8 enter */ |
| 203 | #define UIC_HIBERN8_ENTER_RETRIES 3 |
| 204 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 205 | /* maximum number of reset retries before giving up */ |
| 206 | #define MAX_HOST_RESET_RETRIES 5 |
| 207 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 208 | /* Expose the flag value from utp_upiu_query.value */ |
| 209 | #define MASK_QUERY_UPIU_FLAG_LOC 0xFF |
| 210 | |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 211 | /* Interrupt aggregation default timeout, unit: 40us */ |
| 212 | #define INT_AGGR_DEF_TO 0x02 |
| 213 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 214 | /* default value of auto suspend is 3 seconds */ |
| 215 | #define UFSHCD_AUTO_SUSPEND_DELAY_MS 3000 /* millisecs */ |
| 216 | |
| 217 | #define UFSHCD_CLK_GATING_DELAY_MS_PWR_SAVE 10 |
| 218 | #define UFSHCD_CLK_GATING_DELAY_MS_PERF 50 |
| 219 | |
| 220 | /* IOCTL opcode for command - ufs set device read only */ |
| 221 | #define UFS_IOCTL_BLKROSET BLKROSET |
| 222 | |
| 223 | #define UFSHCD_DEFAULT_LANES_PER_DIRECTION 2 |
| 224 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 225 | #define ufshcd_toggle_vreg(_dev, _vreg, _on) \ |
| 226 | ({ \ |
| 227 | int _ret; \ |
| 228 | if (_on) \ |
| 229 | _ret = ufshcd_enable_vreg(_dev, _vreg); \ |
| 230 | else \ |
| 231 | _ret = ufshcd_disable_vreg(_dev, _vreg); \ |
| 232 | _ret; \ |
| 233 | }) |
| 234 | |
Subhash Jadavani | 4386e02 | 2016-12-19 13:01:56 -0800 | [diff] [blame] | 235 | static void ufshcd_hex_dump(struct ufs_hba *hba, const char * const str, |
| 236 | const void *buf, size_t len) |
| 237 | |
| 238 | { |
| 239 | /* |
| 240 | * device name is expected to take up ~20 characters and "str" passed |
| 241 | * to this function is expected to be of ~10 character so we would need |
| 242 | * ~30 characters string to hold the concatenation of these 2 strings. |
| 243 | */ |
| 244 | #define MAX_PREFIX_STR_SIZE 50 |
| 245 | char prefix_str[MAX_PREFIX_STR_SIZE] = {0}; |
| 246 | |
| 247 | /* concatenate the device name and "str" */ |
| 248 | snprintf(prefix_str, MAX_PREFIX_STR_SIZE, "%s %s: ", |
| 249 | dev_name(hba->dev), str); |
| 250 | print_hex_dump(KERN_ERR, prefix_str, DUMP_PREFIX_OFFSET, |
| 251 | 16, 4, buf, len, false); |
| 252 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 253 | |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 254 | static u32 ufs_query_desc_max_size[] = { |
| 255 | QUERY_DESC_DEVICE_MAX_SIZE, |
| 256 | QUERY_DESC_CONFIGURAION_MAX_SIZE, |
| 257 | QUERY_DESC_UNIT_MAX_SIZE, |
| 258 | QUERY_DESC_RFU_MAX_SIZE, |
| 259 | QUERY_DESC_INTERCONNECT_MAX_SIZE, |
| 260 | QUERY_DESC_STRING_MAX_SIZE, |
| 261 | QUERY_DESC_RFU_MAX_SIZE, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 262 | QUERY_DESC_GEOMETRY_MAZ_SIZE, |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 263 | QUERY_DESC_POWER_MAX_SIZE, |
| 264 | QUERY_DESC_RFU_MAX_SIZE, |
| 265 | }; |
| 266 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 267 | enum { |
| 268 | UFSHCD_MAX_CHANNEL = 0, |
| 269 | UFSHCD_MAX_ID = 1, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 270 | UFSHCD_CMD_PER_LUN = 32, |
| 271 | UFSHCD_CAN_QUEUE = 32, |
| 272 | }; |
| 273 | |
| 274 | /* UFSHCD states */ |
| 275 | enum { |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 276 | UFSHCD_STATE_RESET, |
| 277 | UFSHCD_STATE_ERROR, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 278 | UFSHCD_STATE_OPERATIONAL, |
| 279 | }; |
| 280 | |
| 281 | /* UFSHCD error handling flags */ |
| 282 | enum { |
| 283 | UFSHCD_EH_IN_PROGRESS = (1 << 0), |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 284 | }; |
| 285 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 286 | /* UFSHCD UIC layer error flags */ |
| 287 | enum { |
| 288 | UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 289 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */ |
| 290 | UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */ |
| 291 | UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */ |
| 292 | UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */ |
| 293 | UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 294 | }; |
| 295 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 296 | /* Interrupt configuration options */ |
| 297 | enum { |
| 298 | UFSHCD_INT_DISABLE, |
| 299 | UFSHCD_INT_ENABLE, |
| 300 | UFSHCD_INT_CLEAR, |
| 301 | }; |
| 302 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 303 | #define DEFAULT_UFSHCD_DBG_PRINT_EN UFSHCD_DBG_PRINT_ALL |
| 304 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 305 | #define ufshcd_set_eh_in_progress(h) \ |
| 306 | (h->eh_flags |= UFSHCD_EH_IN_PROGRESS) |
| 307 | #define ufshcd_eh_in_progress(h) \ |
| 308 | (h->eh_flags & UFSHCD_EH_IN_PROGRESS) |
| 309 | #define ufshcd_clear_eh_in_progress(h) \ |
| 310 | (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS) |
| 311 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 312 | #define ufshcd_set_ufs_dev_active(h) \ |
| 313 | ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE) |
| 314 | #define ufshcd_set_ufs_dev_sleep(h) \ |
| 315 | ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE) |
| 316 | #define ufshcd_set_ufs_dev_poweroff(h) \ |
| 317 | ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE) |
| 318 | #define ufshcd_is_ufs_dev_active(h) \ |
| 319 | ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE) |
| 320 | #define ufshcd_is_ufs_dev_sleep(h) \ |
| 321 | ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE) |
| 322 | #define ufshcd_is_ufs_dev_poweroff(h) \ |
| 323 | ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE) |
| 324 | |
| 325 | static struct ufs_pm_lvl_states ufs_pm_lvl_states[] = { |
| 326 | {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE}, |
| 327 | {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 328 | {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE}, |
| 329 | {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 330 | {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 331 | {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE}, |
| 332 | }; |
| 333 | |
| 334 | static inline enum ufs_dev_pwr_mode |
| 335 | ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl) |
| 336 | { |
| 337 | return ufs_pm_lvl_states[lvl].dev_state; |
| 338 | } |
| 339 | |
| 340 | static inline enum uic_link_state |
| 341 | ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl) |
| 342 | { |
| 343 | return ufs_pm_lvl_states[lvl].link_state; |
| 344 | } |
| 345 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 346 | static inline enum ufs_pm_level |
| 347 | ufs_get_desired_pm_lvl_for_dev_link_state(enum ufs_dev_pwr_mode dev_state, |
| 348 | enum uic_link_state link_state) |
| 349 | { |
| 350 | enum ufs_pm_level lvl; |
| 351 | |
| 352 | for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) { |
| 353 | if ((ufs_pm_lvl_states[lvl].dev_state == dev_state) && |
| 354 | (ufs_pm_lvl_states[lvl].link_state == link_state)) |
| 355 | return lvl; |
| 356 | } |
| 357 | |
| 358 | /* if no match found, return the level 0 */ |
| 359 | return UFS_PM_LVL_0; |
| 360 | } |
| 361 | |
| 362 | static inline bool ufshcd_is_valid_pm_lvl(int lvl) |
| 363 | { |
| 364 | if (lvl >= 0 && lvl < ARRAY_SIZE(ufs_pm_lvl_states)) |
| 365 | return true; |
| 366 | else |
| 367 | return false; |
| 368 | } |
| 369 | |
| 370 | static irqreturn_t ufshcd_intr(int irq, void *__hba); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 371 | static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 372 | static void ufshcd_async_scan(void *data, async_cookie_t cookie); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 373 | static int ufshcd_reset_and_restore(struct ufs_hba *hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 374 | static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 375 | 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] | 376 | static void ufshcd_hba_exit(struct ufs_hba *hba); |
| 377 | static int ufshcd_probe_hba(struct ufs_hba *hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 378 | static int ufshcd_enable_clocks(struct ufs_hba *hba); |
| 379 | static int ufshcd_disable_clocks(struct ufs_hba *hba, |
| 380 | bool is_gating_context); |
| 381 | static int ufshcd_disable_clocks_skip_ref_clk(struct ufs_hba *hba, |
| 382 | bool is_gating_context); |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 383 | 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] | 384 | 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] | 385 | 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] | 386 | static int ufshcd_host_reset_and_restore(struct ufs_hba *hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 387 | static void ufshcd_resume_clkscaling(struct ufs_hba *hba); |
| 388 | static void ufshcd_suspend_clkscaling(struct ufs_hba *hba); |
| 389 | static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba); |
| 390 | static void ufshcd_release_all(struct ufs_hba *hba); |
| 391 | static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba); |
| 392 | static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 393 | static int ufshcd_devfreq_target(struct device *dev, |
| 394 | unsigned long *freq, u32 flags); |
| 395 | static int ufshcd_devfreq_get_dev_status(struct device *dev, |
| 396 | struct devfreq_dev_status *stat); |
| 397 | |
| 398 | #if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND) |
| 399 | static struct devfreq_simple_ondemand_data ufshcd_ondemand_data = { |
| 400 | .upthreshold = 35, |
| 401 | .downdifferential = 30, |
| 402 | .simple_scaling = 1, |
| 403 | }; |
| 404 | |
| 405 | static void *gov_data = &ufshcd_ondemand_data; |
| 406 | #else |
| 407 | static void *gov_data; |
| 408 | #endif |
| 409 | |
| 410 | static struct devfreq_dev_profile ufs_devfreq_profile = { |
| 411 | .polling_ms = 40, |
| 412 | .target = ufshcd_devfreq_target, |
| 413 | .get_dev_status = ufshcd_devfreq_get_dev_status, |
| 414 | }; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 415 | |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 416 | static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag) |
| 417 | { |
| 418 | return tag >= 0 && tag < hba->nutrs; |
| 419 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 420 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 421 | static inline void ufshcd_enable_irq(struct ufs_hba *hba) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 422 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 423 | if (!hba->is_irq_enabled) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 424 | enable_irq(hba->irq); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 425 | hba->is_irq_enabled = true; |
| 426 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | static inline void ufshcd_disable_irq(struct ufs_hba *hba) |
| 430 | { |
| 431 | if (hba->is_irq_enabled) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 432 | disable_irq(hba->irq); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 433 | hba->is_irq_enabled = false; |
| 434 | } |
| 435 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 436 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 437 | void ufshcd_scsi_unblock_requests(struct ufs_hba *hba) |
| 438 | { |
| 439 | unsigned long flags; |
| 440 | bool unblock = false; |
| 441 | |
| 442 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 443 | hba->scsi_block_reqs_cnt--; |
| 444 | unblock = !hba->scsi_block_reqs_cnt; |
| 445 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 446 | if (unblock) |
| 447 | scsi_unblock_requests(hba->host); |
| 448 | } |
| 449 | EXPORT_SYMBOL(ufshcd_scsi_unblock_requests); |
| 450 | |
| 451 | static inline void __ufshcd_scsi_block_requests(struct ufs_hba *hba) |
| 452 | { |
| 453 | if (!hba->scsi_block_reqs_cnt++) |
| 454 | scsi_block_requests(hba->host); |
| 455 | } |
| 456 | |
| 457 | void ufshcd_scsi_block_requests(struct ufs_hba *hba) |
| 458 | { |
| 459 | unsigned long flags; |
| 460 | |
| 461 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 462 | __ufshcd_scsi_block_requests(hba); |
| 463 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 464 | } |
| 465 | EXPORT_SYMBOL(ufshcd_scsi_block_requests); |
| 466 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 467 | static int ufshcd_device_reset_ctrl(struct ufs_hba *hba, bool ctrl) |
| 468 | { |
| 469 | int ret = 0; |
| 470 | |
| 471 | if (!hba->pctrl) |
| 472 | return 0; |
| 473 | |
| 474 | /* Assert reset if ctrl == true */ |
| 475 | if (ctrl) |
| 476 | ret = pinctrl_select_state(hba->pctrl, |
| 477 | pinctrl_lookup_state(hba->pctrl, "dev-reset-assert")); |
| 478 | else |
| 479 | ret = pinctrl_select_state(hba->pctrl, |
| 480 | pinctrl_lookup_state(hba->pctrl, "dev-reset-deassert")); |
| 481 | |
| 482 | if (ret < 0) |
| 483 | dev_err(hba->dev, "%s: %s failed with err %d\n", |
| 484 | __func__, ctrl ? "Assert" : "Deassert", ret); |
| 485 | |
| 486 | return ret; |
| 487 | } |
| 488 | |
| 489 | static inline int ufshcd_assert_device_reset(struct ufs_hba *hba) |
| 490 | { |
| 491 | return ufshcd_device_reset_ctrl(hba, true); |
| 492 | } |
| 493 | |
| 494 | static inline int ufshcd_deassert_device_reset(struct ufs_hba *hba) |
| 495 | { |
| 496 | return ufshcd_device_reset_ctrl(hba, false); |
| 497 | } |
| 498 | |
| 499 | static int ufshcd_reset_device(struct ufs_hba *hba) |
| 500 | { |
| 501 | int ret; |
| 502 | |
| 503 | /* reset the connected UFS device */ |
| 504 | ret = ufshcd_assert_device_reset(hba); |
| 505 | if (ret) |
| 506 | goto out; |
| 507 | /* |
| 508 | * The reset signal is active low. |
| 509 | * The UFS device shall detect more than or equal to 1us of positive |
| 510 | * or negative RST_n pulse width. |
| 511 | * To be on safe side, keep the reset low for atleast 10us. |
| 512 | */ |
| 513 | usleep_range(10, 15); |
| 514 | |
| 515 | ret = ufshcd_deassert_device_reset(hba); |
| 516 | if (ret) |
| 517 | goto out; |
| 518 | /* same as assert, wait for atleast 10us after deassert */ |
| 519 | usleep_range(10, 15); |
| 520 | out: |
| 521 | return ret; |
| 522 | } |
| 523 | |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 524 | /* replace non-printable or non-ASCII characters with spaces */ |
| 525 | static inline void ufshcd_remove_non_printable(char *val) |
| 526 | { |
| 527 | if (!val) |
| 528 | return; |
| 529 | |
| 530 | if (*val < 0x20 || *val > 0x7e) |
| 531 | *val = ' '; |
| 532 | } |
| 533 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 534 | #ifdef CONFIG_TRACEPOINTS |
| 535 | static void ufshcd_add_command_trace(struct ufs_hba *hba, |
| 536 | unsigned int tag, const char *str) |
| 537 | { |
| 538 | sector_t lba = -1; |
| 539 | u8 opcode = 0; |
| 540 | u32 intr, doorbell; |
| 541 | struct ufshcd_lrb *lrbp; |
| 542 | int transfer_len = -1; |
| 543 | |
| 544 | lrbp = &hba->lrb[tag]; |
| 545 | |
| 546 | if (lrbp->cmd) { /* data phase exists */ |
| 547 | opcode = (u8)(*lrbp->cmd->cmnd); |
| 548 | if ((opcode == READ_10) || (opcode == WRITE_10)) { |
| 549 | /* |
| 550 | * Currently we only fully trace read(10) and write(10) |
| 551 | * commands |
| 552 | */ |
| 553 | if (lrbp->cmd->request && lrbp->cmd->request->bio) |
| 554 | lba = |
| 555 | lrbp->cmd->request->bio->bi_iter.bi_sector; |
| 556 | transfer_len = be32_to_cpu( |
| 557 | lrbp->ucd_req_ptr->sc.exp_data_transfer_len); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | intr = ufshcd_readl(hba, REG_INTERRUPT_STATUS); |
| 562 | doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 563 | trace_ufshcd_command(dev_name(hba->dev), str, tag, |
| 564 | doorbell, transfer_len, intr, lba, opcode); |
| 565 | } |
| 566 | |
| 567 | static inline void ufshcd_cond_add_cmd_trace(struct ufs_hba *hba, |
| 568 | unsigned int tag, const char *str) |
| 569 | { |
| 570 | if (trace_ufshcd_command_enabled()) |
| 571 | ufshcd_add_command_trace(hba, tag, str); |
| 572 | } |
| 573 | #else |
| 574 | static inline void ufshcd_cond_add_cmd_trace(struct ufs_hba *hba, |
| 575 | unsigned int tag, const char *str) |
| 576 | { |
| 577 | } |
| 578 | #endif |
| 579 | |
| 580 | static void ufshcd_print_clk_freqs(struct ufs_hba *hba) |
| 581 | { |
| 582 | struct ufs_clk_info *clki; |
| 583 | struct list_head *head = &hba->clk_list_head; |
| 584 | |
| 585 | if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_CLK_FREQ_EN)) |
| 586 | return; |
| 587 | |
| 588 | if (!head || list_empty(head)) |
| 589 | return; |
| 590 | |
| 591 | list_for_each_entry(clki, head, list) { |
| 592 | if (!IS_ERR_OR_NULL(clki->clk) && clki->min_freq && |
| 593 | clki->max_freq) |
| 594 | dev_err(hba->dev, "clk: %s, rate: %u\n", |
| 595 | clki->name, clki->curr_freq); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | static void ufshcd_print_uic_err_hist(struct ufs_hba *hba, |
| 600 | struct ufs_uic_err_reg_hist *err_hist, char *err_name) |
| 601 | { |
| 602 | int i; |
| 603 | |
| 604 | if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_UIC_ERR_HIST_EN)) |
| 605 | return; |
| 606 | |
| 607 | for (i = 0; i < UIC_ERR_REG_HIST_LENGTH; i++) { |
| 608 | int p = (i + err_hist->pos - 1) % UIC_ERR_REG_HIST_LENGTH; |
| 609 | |
| 610 | if (err_hist->reg[p] == 0) |
| 611 | continue; |
| 612 | dev_err(hba->dev, "%s[%d] = 0x%x at %lld us", err_name, i, |
| 613 | err_hist->reg[p], ktime_to_us(err_hist->tstamp[p])); |
| 614 | } |
| 615 | } |
| 616 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 617 | static inline void __ufshcd_print_host_regs(struct ufs_hba *hba, bool no_sleep) |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 618 | { |
| 619 | if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_HOST_REGS_EN)) |
| 620 | return; |
| 621 | |
| 622 | /* |
| 623 | * hex_dump reads its data without the readl macro. This might |
| 624 | * cause inconsistency issues on some platform, as the printed |
| 625 | * values may be from cache and not the most recent value. |
| 626 | * To know whether you are looking at an un-cached version verify |
| 627 | * that IORESOURCE_MEM flag is on when xxx_get_resource() is invoked |
| 628 | * during platform/pci probe function. |
| 629 | */ |
Subhash Jadavani | 4386e02 | 2016-12-19 13:01:56 -0800 | [diff] [blame] | 630 | ufshcd_hex_dump(hba, "host regs", hba->mmio_base, |
| 631 | UFSHCI_REG_SPACE_SIZE); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 632 | dev_err(hba->dev, "hba->ufs_version = 0x%x, hba->capabilities = 0x%x", |
| 633 | hba->ufs_version, hba->capabilities); |
| 634 | dev_err(hba->dev, |
| 635 | "hba->outstanding_reqs = 0x%x, hba->outstanding_tasks = 0x%x", |
| 636 | (u32)hba->outstanding_reqs, (u32)hba->outstanding_tasks); |
| 637 | dev_err(hba->dev, |
| 638 | "last_hibern8_exit_tstamp at %lld us, hibern8_exit_cnt = %d", |
| 639 | ktime_to_us(hba->ufs_stats.last_hibern8_exit_tstamp), |
| 640 | hba->ufs_stats.hibern8_exit_cnt); |
| 641 | |
| 642 | ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.pa_err, "pa_err"); |
| 643 | ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.dl_err, "dl_err"); |
| 644 | ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.nl_err, "nl_err"); |
| 645 | ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.tl_err, "tl_err"); |
| 646 | ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.dme_err, "dme_err"); |
| 647 | |
| 648 | ufshcd_print_clk_freqs(hba); |
| 649 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 650 | ufshcd_vops_dbg_register_dump(hba, no_sleep); |
| 651 | } |
| 652 | |
| 653 | static void ufshcd_print_host_regs(struct ufs_hba *hba) |
| 654 | { |
| 655 | __ufshcd_print_host_regs(hba, false); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | static |
| 659 | void ufshcd_print_trs(struct ufs_hba *hba, unsigned long bitmap, bool pr_prdt) |
| 660 | { |
| 661 | struct ufshcd_lrb *lrbp; |
| 662 | int prdt_length; |
| 663 | int tag; |
| 664 | |
| 665 | if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_TRS_EN)) |
| 666 | return; |
| 667 | |
| 668 | for_each_set_bit(tag, &bitmap, hba->nutrs) { |
| 669 | lrbp = &hba->lrb[tag]; |
| 670 | |
| 671 | dev_err(hba->dev, "UPIU[%d] - issue time %lld us", |
| 672 | tag, ktime_to_us(lrbp->issue_time_stamp)); |
| 673 | dev_err(hba->dev, |
| 674 | "UPIU[%d] - Transfer Request Descriptor phys@0x%llx", |
| 675 | tag, (u64)lrbp->utrd_dma_addr); |
Subhash Jadavani | 4386e02 | 2016-12-19 13:01:56 -0800 | [diff] [blame] | 676 | ufshcd_hex_dump(hba, "UPIU TRD", lrbp->utr_descriptor_ptr, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 677 | sizeof(struct utp_transfer_req_desc)); |
| 678 | dev_err(hba->dev, "UPIU[%d] - Request UPIU phys@0x%llx", tag, |
| 679 | (u64)lrbp->ucd_req_dma_addr); |
Subhash Jadavani | 4386e02 | 2016-12-19 13:01:56 -0800 | [diff] [blame] | 680 | ufshcd_hex_dump(hba, "UPIU REQ", lrbp->ucd_req_ptr, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 681 | sizeof(struct utp_upiu_req)); |
| 682 | dev_err(hba->dev, "UPIU[%d] - Response UPIU phys@0x%llx", tag, |
| 683 | (u64)lrbp->ucd_rsp_dma_addr); |
Subhash Jadavani | 4386e02 | 2016-12-19 13:01:56 -0800 | [diff] [blame] | 684 | ufshcd_hex_dump(hba, "UPIU RSP", lrbp->ucd_rsp_ptr, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 685 | sizeof(struct utp_upiu_rsp)); |
| 686 | prdt_length = |
| 687 | le16_to_cpu(lrbp->utr_descriptor_ptr->prd_table_length); |
| 688 | dev_err(hba->dev, "UPIU[%d] - PRDT - %d entries phys@0x%llx", |
| 689 | tag, prdt_length, (u64)lrbp->ucd_prdt_dma_addr); |
| 690 | if (pr_prdt) |
Subhash Jadavani | 4386e02 | 2016-12-19 13:01:56 -0800 | [diff] [blame] | 691 | ufshcd_hex_dump(hba, "UPIU PRDT", lrbp->ucd_prdt_ptr, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 692 | sizeof(struct ufshcd_sg_entry) * prdt_length); |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap) |
| 697 | { |
| 698 | struct utp_task_req_desc *tmrdp; |
| 699 | int tag; |
| 700 | |
| 701 | if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_TMRS_EN)) |
| 702 | return; |
| 703 | |
| 704 | for_each_set_bit(tag, &bitmap, hba->nutmrs) { |
| 705 | tmrdp = &hba->utmrdl_base_addr[tag]; |
| 706 | dev_err(hba->dev, "TM[%d] - Task Management Header", tag); |
Subhash Jadavani | 4386e02 | 2016-12-19 13:01:56 -0800 | [diff] [blame] | 707 | ufshcd_hex_dump(hba, "TM TRD", &tmrdp->header, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 708 | sizeof(struct request_desc_header)); |
| 709 | dev_err(hba->dev, "TM[%d] - Task Management Request UPIU", |
| 710 | tag); |
Subhash Jadavani | 4386e02 | 2016-12-19 13:01:56 -0800 | [diff] [blame] | 711 | ufshcd_hex_dump(hba, "TM REQ", tmrdp->task_req_upiu, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 712 | sizeof(struct utp_upiu_req)); |
| 713 | dev_err(hba->dev, "TM[%d] - Task Management Response UPIU", |
| 714 | tag); |
Subhash Jadavani | 4386e02 | 2016-12-19 13:01:56 -0800 | [diff] [blame] | 715 | ufshcd_hex_dump(hba, "TM RSP", tmrdp->task_rsp_upiu, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 716 | sizeof(struct utp_task_req_desc)); |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | static void ufshcd_print_host_state(struct ufs_hba *hba) |
| 721 | { |
| 722 | if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_HOST_STATE_EN)) |
| 723 | return; |
| 724 | |
| 725 | dev_err(hba->dev, "UFS Host state=%d\n", hba->ufshcd_state); |
| 726 | dev_err(hba->dev, "lrb in use=0x%lx, outstanding reqs=0x%lx tasks=0x%lx\n", |
| 727 | hba->lrb_in_use, hba->outstanding_tasks, hba->outstanding_reqs); |
| 728 | dev_err(hba->dev, "saved_err=0x%x, saved_uic_err=0x%x, saved_ce_err=0x%x\n", |
| 729 | hba->saved_err, hba->saved_uic_err, hba->saved_ce_err); |
| 730 | dev_err(hba->dev, "Device power mode=%d, UIC link state=%d\n", |
| 731 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
| 732 | dev_err(hba->dev, "PM in progress=%d, sys. suspended=%d\n", |
| 733 | hba->pm_op_in_progress, hba->is_sys_suspended); |
| 734 | dev_err(hba->dev, "Auto BKOPS=%d, Host self-block=%d\n", |
| 735 | hba->auto_bkops_enabled, hba->host->host_self_blocked); |
| 736 | dev_err(hba->dev, "Clk gate=%d, hibern8 on idle=%d\n", |
| 737 | hba->clk_gating.state, hba->hibern8_on_idle.state); |
| 738 | dev_err(hba->dev, "error handling flags=0x%x, req. abort count=%d\n", |
| 739 | hba->eh_flags, hba->req_abort_count); |
| 740 | dev_err(hba->dev, "Host capabilities=0x%x, caps=0x%x\n", |
| 741 | hba->capabilities, hba->caps); |
| 742 | dev_err(hba->dev, "quirks=0x%x, dev. quirks=0x%x\n", hba->quirks, |
Subhash Jadavani | 4f0df17b | 2016-12-16 13:19:27 -0800 | [diff] [blame] | 743 | hba->dev_info.quirks); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | /** |
| 747 | * ufshcd_print_pwr_info - print power params as saved in hba |
| 748 | * power info |
| 749 | * @hba: per-adapter instance |
| 750 | */ |
| 751 | static void ufshcd_print_pwr_info(struct ufs_hba *hba) |
| 752 | { |
| 753 | char *names[] = { |
| 754 | "INVALID MODE", |
| 755 | "FAST MODE", |
| 756 | "SLOW_MODE", |
| 757 | "INVALID MODE", |
| 758 | "FASTAUTO_MODE", |
| 759 | "SLOWAUTO_MODE", |
| 760 | "INVALID MODE", |
| 761 | }; |
| 762 | |
| 763 | if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_PWR_EN)) |
| 764 | return; |
| 765 | |
| 766 | dev_err(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n", |
| 767 | __func__, |
| 768 | hba->pwr_info.gear_rx, hba->pwr_info.gear_tx, |
| 769 | hba->pwr_info.lane_rx, hba->pwr_info.lane_tx, |
| 770 | names[hba->pwr_info.pwr_rx], |
| 771 | names[hba->pwr_info.pwr_tx], |
| 772 | hba->pwr_info.hs_rate); |
| 773 | } |
| 774 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 775 | /* |
| 776 | * ufshcd_wait_for_register - wait for register value to change |
| 777 | * @hba - per-adapter interface |
| 778 | * @reg - mmio register offset |
| 779 | * @mask - mask to apply to read register value |
| 780 | * @val - wait condition |
| 781 | * @interval_us - polling interval in microsecs |
| 782 | * @timeout_ms - timeout in millisecs |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 783 | * @can_sleep - perform sleep or just spin |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 784 | * Returns -ETIMEDOUT on error, zero on success |
| 785 | */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 786 | int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask, |
| 787 | u32 val, unsigned long interval_us, |
| 788 | unsigned long timeout_ms, bool can_sleep) |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 789 | { |
| 790 | int err = 0; |
| 791 | unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms); |
| 792 | |
| 793 | /* ignore bits that we don't intend to wait on */ |
| 794 | val = val & mask; |
| 795 | |
| 796 | while ((ufshcd_readl(hba, reg) & mask) != val) { |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 797 | if (can_sleep) |
| 798 | usleep_range(interval_us, interval_us + 50); |
| 799 | else |
| 800 | udelay(interval_us); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 801 | if (time_after(jiffies, timeout)) { |
| 802 | if ((ufshcd_readl(hba, reg) & mask) != val) |
| 803 | err = -ETIMEDOUT; |
| 804 | break; |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | return err; |
| 809 | } |
| 810 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 811 | /** |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 812 | * ufshcd_get_intr_mask - Get the interrupt bit mask |
| 813 | * @hba - Pointer to adapter instance |
| 814 | * |
| 815 | * Returns interrupt bit mask per version |
| 816 | */ |
| 817 | static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba) |
| 818 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 819 | u32 intr_mask = 0; |
| 820 | |
| 821 | switch (hba->ufs_version) { |
| 822 | case UFSHCI_VERSION_10: |
| 823 | intr_mask = INTERRUPT_MASK_ALL_VER_10; |
| 824 | break; |
| 825 | /* allow fall through */ |
| 826 | case UFSHCI_VERSION_11: |
| 827 | case UFSHCI_VERSION_20: |
| 828 | intr_mask = INTERRUPT_MASK_ALL_VER_11; |
| 829 | break; |
| 830 | /* allow fall through */ |
| 831 | case UFSHCI_VERSION_21: |
| 832 | default: |
| 833 | intr_mask = INTERRUPT_MASK_ALL_VER_21; |
| 834 | } |
| 835 | |
| 836 | if (!ufshcd_is_crypto_supported(hba)) |
| 837 | intr_mask &= ~CRYPTO_ENGINE_FATAL_ERROR; |
| 838 | |
| 839 | return intr_mask; |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 843 | * ufshcd_get_ufs_version - Get the UFS version supported by the HBA |
| 844 | * @hba - Pointer to adapter instance |
| 845 | * |
| 846 | * Returns UFSHCI version supported by the controller |
| 847 | */ |
| 848 | static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba) |
| 849 | { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 850 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION) |
| 851 | return ufshcd_vops_get_ufs_hci_version(hba); |
Yaniv Gardi | 9949e70 | 2015-05-17 18:55:05 +0300 | [diff] [blame] | 852 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 853 | return ufshcd_readl(hba, REG_UFS_VERSION); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | /** |
| 857 | * ufshcd_is_device_present - Check if any device connected to |
| 858 | * the host controller |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 859 | * @hba: pointer to adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 860 | * |
Venkatraman S | 73ec513 | 2012-07-10 19:39:23 +0530 | [diff] [blame] | 861 | * Returns 1 if device present, 0 if no device detected |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 862 | */ |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 863 | static inline int ufshcd_is_device_present(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 864 | { |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 865 | return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & |
| 866 | DEVICE_PRESENT) ? 1 : 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | /** |
| 870 | * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status |
| 871 | * @lrb: pointer to local command reference block |
| 872 | * |
| 873 | * This function is used to get the OCS field from UTRD |
| 874 | * Returns the OCS field in the UTRD |
| 875 | */ |
| 876 | static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp) |
| 877 | { |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 878 | 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] | 879 | } |
| 880 | |
| 881 | /** |
| 882 | * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status |
| 883 | * @task_req_descp: pointer to utp_task_req_desc structure |
| 884 | * |
| 885 | * This function is used to get the OCS field from UTMRD |
| 886 | * Returns the OCS field in the UTMRD |
| 887 | */ |
| 888 | static inline int |
| 889 | ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp) |
| 890 | { |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 891 | return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | /** |
| 895 | * ufshcd_get_tm_free_slot - get a free slot for task management request |
| 896 | * @hba: per adapter instance |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 897 | * @free_slot: pointer to variable with available slot value |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 898 | * |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 899 | * Get a free tag and lock it until ufshcd_put_tm_slot() is called. |
| 900 | * Returns 0 if free slot is not available, else return 1 with tag value |
| 901 | * in @free_slot. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 902 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 903 | 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] | 904 | { |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 905 | int tag; |
| 906 | bool ret = false; |
| 907 | |
| 908 | if (!free_slot) |
| 909 | goto out; |
| 910 | |
| 911 | do { |
| 912 | tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs); |
| 913 | if (tag >= hba->nutmrs) |
| 914 | goto out; |
| 915 | } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use)); |
| 916 | |
| 917 | *free_slot = tag; |
| 918 | ret = true; |
| 919 | out: |
| 920 | return ret; |
| 921 | } |
| 922 | |
| 923 | static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot) |
| 924 | { |
| 925 | clear_bit_unlock(slot, &hba->tm_slots_in_use); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | /** |
| 929 | * ufshcd_utrl_clear - Clear a bit in UTRLCLR register |
| 930 | * @hba: per adapter instance |
| 931 | * @pos: position of the bit to be cleared |
| 932 | */ |
| 933 | static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos) |
| 934 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 935 | ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | /** |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 939 | * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field |
| 940 | * @hba: per adapter instance |
| 941 | * @tag: position of the bit to be cleared |
| 942 | */ |
| 943 | static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag) |
| 944 | { |
| 945 | __clear_bit(tag, &hba->outstanding_reqs); |
| 946 | } |
| 947 | |
| 948 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 949 | * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY |
| 950 | * @reg: Register value of host controller status |
| 951 | * |
| 952 | * Returns integer, 0 on Success and positive value if failed |
| 953 | */ |
| 954 | static inline int ufshcd_get_lists_status(u32 reg) |
| 955 | { |
| 956 | /* |
| 957 | * The mask 0xFF is for the following HCS register bits |
| 958 | * Bit Description |
| 959 | * 0 Device Present |
| 960 | * 1 UTRLRDY |
| 961 | * 2 UTMRLRDY |
| 962 | * 3 UCRDY |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 963 | * 4-7 reserved |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 964 | */ |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 965 | return ((reg & 0xFF) >> 1) ^ 0x07; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | /** |
| 969 | * ufshcd_get_uic_cmd_result - Get the UIC command result |
| 970 | * @hba: Pointer to adapter instance |
| 971 | * |
| 972 | * This function gets the result of UIC command completion |
| 973 | * Returns 0 on success, non zero value on error |
| 974 | */ |
| 975 | static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba) |
| 976 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 977 | return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) & |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 978 | MASK_UIC_COMMAND_RESULT; |
| 979 | } |
| 980 | |
| 981 | /** |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 982 | * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command |
| 983 | * @hba: Pointer to adapter instance |
| 984 | * |
| 985 | * This function gets UIC command argument3 |
| 986 | * Returns 0 on success, non zero value on error |
| 987 | */ |
| 988 | static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba) |
| 989 | { |
| 990 | return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3); |
| 991 | } |
| 992 | |
| 993 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 994 | * ufshcd_get_req_rsp - returns the TR response transaction type |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 995 | * @ucd_rsp_ptr: pointer to response UPIU |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 996 | */ |
| 997 | static inline int |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 998 | ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 999 | { |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1000 | return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | /** |
| 1004 | * ufshcd_get_rsp_upiu_result - Get the result from response UPIU |
| 1005 | * @ucd_rsp_ptr: pointer to response UPIU |
| 1006 | * |
| 1007 | * This function gets the response status and scsi_status from response UPIU |
| 1008 | * Returns the response result code. |
| 1009 | */ |
| 1010 | static inline int |
| 1011 | ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 1012 | { |
| 1013 | return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT; |
| 1014 | } |
| 1015 | |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 1016 | /* |
| 1017 | * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length |
| 1018 | * from response UPIU |
| 1019 | * @ucd_rsp_ptr: pointer to response UPIU |
| 1020 | * |
| 1021 | * Return the data segment length. |
| 1022 | */ |
| 1023 | static inline unsigned int |
| 1024 | ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 1025 | { |
| 1026 | return be32_to_cpu(ucd_rsp_ptr->header.dword_2) & |
| 1027 | MASK_RSP_UPIU_DATA_SEG_LEN; |
| 1028 | } |
| 1029 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1030 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 1031 | * ufshcd_is_exception_event - Check if the device raised an exception event |
| 1032 | * @ucd_rsp_ptr: pointer to response UPIU |
| 1033 | * |
| 1034 | * The function checks if the device raised an exception event indicated in |
| 1035 | * the Device Information field of response UPIU. |
| 1036 | * |
| 1037 | * Returns true if exception is raised, false otherwise. |
| 1038 | */ |
| 1039 | static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 1040 | { |
| 1041 | return be32_to_cpu(ucd_rsp_ptr->header.dword_2) & |
| 1042 | MASK_RSP_EXCEPTION_EVENT ? true : false; |
| 1043 | } |
| 1044 | |
| 1045 | /** |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 1046 | * ufshcd_reset_intr_aggr - Reset interrupt aggregation values. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1047 | * @hba: per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1048 | */ |
| 1049 | static inline void |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 1050 | ufshcd_reset_intr_aggr(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1051 | { |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 1052 | ufshcd_writel(hba, INT_AGGR_ENABLE | |
| 1053 | INT_AGGR_COUNTER_AND_TIMER_RESET, |
| 1054 | REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
| 1055 | } |
| 1056 | |
| 1057 | /** |
| 1058 | * ufshcd_config_intr_aggr - Configure interrupt aggregation values. |
| 1059 | * @hba: per adapter instance |
| 1060 | * @cnt: Interrupt aggregation counter threshold |
| 1061 | * @tmout: Interrupt aggregation timeout value |
| 1062 | */ |
| 1063 | static inline void |
| 1064 | ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout) |
| 1065 | { |
| 1066 | ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE | |
| 1067 | INT_AGGR_COUNTER_THLD_VAL(cnt) | |
| 1068 | INT_AGGR_TIMEOUT_VAL(tmout), |
| 1069 | REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | /** |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 1073 | * ufshcd_disable_intr_aggr - Disables interrupt aggregation. |
| 1074 | * @hba: per adapter instance |
| 1075 | */ |
| 1076 | static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba) |
| 1077 | { |
| 1078 | ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
| 1079 | } |
| 1080 | |
| 1081 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1082 | * ufshcd_enable_run_stop_reg - Enable run-stop registers, |
| 1083 | * When run-stop registers are set to 1, it indicates the |
| 1084 | * host controller that it can process the requests |
| 1085 | * @hba: per adapter instance |
| 1086 | */ |
| 1087 | static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba) |
| 1088 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 1089 | ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT, |
| 1090 | REG_UTP_TASK_REQ_LIST_RUN_STOP); |
| 1091 | ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT, |
| 1092 | REG_UTP_TRANSFER_REQ_LIST_RUN_STOP); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1096 | * ufshcd_hba_start - Start controller initialization sequence |
| 1097 | * @hba: per adapter instance |
| 1098 | */ |
| 1099 | static inline void ufshcd_hba_start(struct ufs_hba *hba) |
| 1100 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1101 | u32 val = CONTROLLER_ENABLE; |
| 1102 | |
| 1103 | if (ufshcd_is_crypto_supported(hba)) |
| 1104 | val |= CRYPTO_GENERAL_ENABLE; |
| 1105 | ufshcd_writel(hba, val, REG_CONTROLLER_ENABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | /** |
| 1109 | * ufshcd_is_hba_active - Get controller state |
| 1110 | * @hba: per adapter instance |
| 1111 | * |
| 1112 | * Returns zero if controller is active, 1 otherwise |
| 1113 | */ |
| 1114 | static inline int ufshcd_is_hba_active(struct ufs_hba *hba) |
| 1115 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 1116 | return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1117 | } |
| 1118 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1119 | static const char *ufschd_uic_link_state_to_string( |
| 1120 | enum uic_link_state state) |
| 1121 | { |
| 1122 | switch (state) { |
| 1123 | case UIC_LINK_OFF_STATE: return "OFF"; |
| 1124 | case UIC_LINK_ACTIVE_STATE: return "ACTIVE"; |
| 1125 | case UIC_LINK_HIBERN8_STATE: return "HIBERN8"; |
| 1126 | default: return "UNKNOWN"; |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | static const char *ufschd_ufs_dev_pwr_mode_to_string( |
| 1131 | enum ufs_dev_pwr_mode state) |
| 1132 | { |
| 1133 | switch (state) { |
| 1134 | case UFS_ACTIVE_PWR_MODE: return "ACTIVE"; |
| 1135 | case UFS_SLEEP_PWR_MODE: return "SLEEP"; |
| 1136 | case UFS_POWERDOWN_PWR_MODE: return "POWERDOWN"; |
| 1137 | default: return "UNKNOWN"; |
| 1138 | } |
| 1139 | } |
| 1140 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 1141 | u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba) |
| 1142 | { |
| 1143 | /* HCI version 1.0 and 1.1 supports UniPro 1.41 */ |
| 1144 | if ((hba->ufs_version == UFSHCI_VERSION_10) || |
| 1145 | (hba->ufs_version == UFSHCI_VERSION_11)) |
| 1146 | return UFS_UNIPRO_VER_1_41; |
| 1147 | else |
| 1148 | return UFS_UNIPRO_VER_1_6; |
| 1149 | } |
| 1150 | EXPORT_SYMBOL(ufshcd_get_local_unipro_ver); |
| 1151 | |
| 1152 | static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba) |
| 1153 | { |
| 1154 | /* |
| 1155 | * If both host and device support UniPro ver1.6 or later, PA layer |
| 1156 | * parameters tuning happens during link startup itself. |
| 1157 | * |
| 1158 | * We can manually tune PA layer parameters if either host or device |
| 1159 | * doesn't support UniPro ver 1.6 or later. But to keep manual tuning |
| 1160 | * logic simple, we will only do manual tuning if local unipro version |
| 1161 | * doesn't support ver1.6 or later. |
| 1162 | */ |
| 1163 | if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6) |
| 1164 | return true; |
| 1165 | else |
| 1166 | return false; |
| 1167 | } |
| 1168 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1169 | /** |
| 1170 | * ufshcd_set_clk_freq - set UFS controller clock frequencies |
| 1171 | * @hba: per adapter instance |
| 1172 | * @scale_up: If True, set max possible frequency othewise set low frequency |
| 1173 | * |
| 1174 | * Returns 0 if successful |
| 1175 | * Returns < 0 for any other errors |
| 1176 | */ |
| 1177 | static int ufshcd_set_clk_freq(struct ufs_hba *hba, bool scale_up) |
| 1178 | { |
| 1179 | int ret = 0; |
| 1180 | struct ufs_clk_info *clki; |
| 1181 | struct list_head *head = &hba->clk_list_head; |
| 1182 | |
| 1183 | if (!head || list_empty(head)) |
| 1184 | goto out; |
| 1185 | |
| 1186 | list_for_each_entry(clki, head, list) { |
| 1187 | if (!IS_ERR_OR_NULL(clki->clk)) { |
| 1188 | if (scale_up && clki->max_freq) { |
| 1189 | if (clki->curr_freq == clki->max_freq) |
| 1190 | continue; |
| 1191 | |
| 1192 | ret = clk_set_rate(clki->clk, clki->max_freq); |
| 1193 | if (ret) { |
| 1194 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 1195 | __func__, clki->name, |
| 1196 | clki->max_freq, ret); |
| 1197 | break; |
| 1198 | } |
| 1199 | trace_ufshcd_clk_scaling(dev_name(hba->dev), |
| 1200 | "scaled up", clki->name, |
| 1201 | clki->curr_freq, |
| 1202 | clki->max_freq); |
| 1203 | clki->curr_freq = clki->max_freq; |
| 1204 | |
| 1205 | } else if (!scale_up && clki->min_freq) { |
| 1206 | if (clki->curr_freq == clki->min_freq) |
| 1207 | continue; |
| 1208 | |
| 1209 | ret = clk_set_rate(clki->clk, clki->min_freq); |
| 1210 | if (ret) { |
| 1211 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 1212 | __func__, clki->name, |
| 1213 | clki->min_freq, ret); |
| 1214 | break; |
| 1215 | } |
| 1216 | trace_ufshcd_clk_scaling(dev_name(hba->dev), |
| 1217 | "scaled down", clki->name, |
| 1218 | clki->curr_freq, |
| 1219 | clki->min_freq); |
| 1220 | clki->curr_freq = clki->min_freq; |
| 1221 | } |
| 1222 | } |
| 1223 | dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__, |
| 1224 | clki->name, clk_get_rate(clki->clk)); |
| 1225 | } |
| 1226 | |
| 1227 | out: |
| 1228 | return ret; |
| 1229 | } |
| 1230 | |
| 1231 | /** |
| 1232 | * ufshcd_scale_clks - scale up or scale down UFS controller clocks |
| 1233 | * @hba: per adapter instance |
| 1234 | * @scale_up: True if scaling up and false if scaling down |
| 1235 | * |
| 1236 | * Returns 0 if successful |
| 1237 | * Returns < 0 for any other errors |
| 1238 | */ |
| 1239 | static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up) |
| 1240 | { |
| 1241 | int ret = 0; |
| 1242 | |
| 1243 | ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE); |
| 1244 | if (ret) |
| 1245 | return ret; |
| 1246 | |
| 1247 | ret = ufshcd_set_clk_freq(hba, scale_up); |
| 1248 | if (ret) |
| 1249 | return ret; |
| 1250 | |
| 1251 | ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE); |
| 1252 | if (ret) { |
| 1253 | ufshcd_set_clk_freq(hba, !scale_up); |
| 1254 | return ret; |
| 1255 | } |
| 1256 | |
| 1257 | return ret; |
| 1258 | } |
| 1259 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 1260 | static inline void ufshcd_cancel_gate_work(struct ufs_hba *hba) |
| 1261 | { |
| 1262 | hrtimer_cancel(&hba->clk_gating.gate_hrtimer); |
| 1263 | cancel_work_sync(&hba->clk_gating.gate_work); |
| 1264 | } |
| 1265 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1266 | static void ufshcd_ungate_work(struct work_struct *work) |
| 1267 | { |
| 1268 | int ret; |
| 1269 | unsigned long flags; |
| 1270 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1271 | clk_gating.ungate_work); |
| 1272 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 1273 | ufshcd_cancel_gate_work(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1274 | |
| 1275 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1276 | if (hba->clk_gating.state == CLKS_ON) { |
| 1277 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1278 | goto unblock_reqs; |
| 1279 | } |
| 1280 | |
| 1281 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1282 | ufshcd_hba_vreg_set_hpm(hba); |
| 1283 | ufshcd_enable_clocks(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1284 | |
| 1285 | /* Exit from hibern8 */ |
| 1286 | if (ufshcd_can_hibern8_during_gating(hba)) { |
| 1287 | /* Prevent gating in this path */ |
| 1288 | hba->clk_gating.is_suspended = true; |
| 1289 | if (ufshcd_is_link_hibern8(hba)) { |
| 1290 | ret = ufshcd_uic_hibern8_exit(hba); |
| 1291 | if (ret) |
| 1292 | dev_err(hba->dev, "%s: hibern8 exit failed %d\n", |
| 1293 | __func__, ret); |
| 1294 | else |
| 1295 | ufshcd_set_link_active(hba); |
| 1296 | } |
| 1297 | hba->clk_gating.is_suspended = false; |
| 1298 | } |
| 1299 | unblock_reqs: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1300 | ufshcd_scsi_unblock_requests(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | /** |
| 1304 | * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release. |
| 1305 | * Also, exit from hibern8 mode and set the link as active. |
| 1306 | * @hba: per adapter instance |
| 1307 | * @async: This indicates whether caller should ungate clocks asynchronously. |
| 1308 | */ |
| 1309 | int ufshcd_hold(struct ufs_hba *hba, bool async) |
| 1310 | { |
| 1311 | int rc = 0; |
| 1312 | unsigned long flags; |
| 1313 | |
| 1314 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1315 | goto out; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1316 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1317 | hba->clk_gating.active_reqs++; |
| 1318 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 1319 | if (ufshcd_eh_in_progress(hba)) { |
| 1320 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1321 | return 0; |
| 1322 | } |
| 1323 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1324 | start: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1325 | switch (hba->clk_gating.state) { |
| 1326 | case CLKS_ON: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1327 | /* |
| 1328 | * Wait for the ungate work to complete if in progress. |
| 1329 | * Though the clocks may be in ON state, the link could |
| 1330 | * still be in hibner8 state if hibern8 is allowed |
| 1331 | * during clock gating. |
| 1332 | * Make sure we exit hibern8 state also in addition to |
| 1333 | * clocks being ON. |
| 1334 | */ |
| 1335 | if (ufshcd_can_hibern8_during_gating(hba) && |
| 1336 | ufshcd_is_link_hibern8(hba)) { |
| 1337 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1338 | flush_work(&hba->clk_gating.ungate_work); |
| 1339 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1340 | goto start; |
| 1341 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1342 | break; |
| 1343 | case REQ_CLKS_OFF: |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 1344 | /* |
| 1345 | * If the timer was active but the callback was not running |
| 1346 | * we have nothing to do, just change state and return. |
| 1347 | */ |
| 1348 | if (hrtimer_try_to_cancel(&hba->clk_gating.gate_hrtimer) == 1) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1349 | hba->clk_gating.state = CLKS_ON; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1350 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1351 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1352 | break; |
| 1353 | } |
| 1354 | /* |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 1355 | * If we are here, it means gating work is either done or |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1356 | * currently running. Hence, fall through to cancel gating |
| 1357 | * work and to enable clocks. |
| 1358 | */ |
| 1359 | case CLKS_OFF: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1360 | __ufshcd_scsi_block_requests(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1361 | hba->clk_gating.state = REQ_CLKS_ON; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1362 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1363 | hba->clk_gating.state); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 1364 | queue_work(hba->clk_gating.ungating_workq, |
| 1365 | &hba->clk_gating.ungate_work); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1366 | /* |
| 1367 | * fall through to check if we should wait for this |
| 1368 | * work to be done or not. |
| 1369 | */ |
| 1370 | case REQ_CLKS_ON: |
| 1371 | if (async) { |
| 1372 | rc = -EAGAIN; |
| 1373 | hba->clk_gating.active_reqs--; |
| 1374 | break; |
| 1375 | } |
| 1376 | |
| 1377 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1378 | flush_work(&hba->clk_gating.ungate_work); |
| 1379 | /* Make sure state is CLKS_ON before returning */ |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1380 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1381 | goto start; |
| 1382 | default: |
| 1383 | dev_err(hba->dev, "%s: clk gating is in invalid state %d\n", |
| 1384 | __func__, hba->clk_gating.state); |
| 1385 | break; |
| 1386 | } |
| 1387 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1388 | out: |
| 1389 | return rc; |
| 1390 | } |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 1391 | EXPORT_SYMBOL_GPL(ufshcd_hold); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1392 | |
| 1393 | static void ufshcd_gate_work(struct work_struct *work) |
| 1394 | { |
| 1395 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 1396 | clk_gating.gate_work); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1397 | unsigned long flags; |
| 1398 | |
| 1399 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 1400 | /* |
| 1401 | * In case you are here to cancel this work the gating state |
| 1402 | * would be marked as REQ_CLKS_ON. In this case save time by |
| 1403 | * skipping the gating work and exit after changing the clock |
| 1404 | * state to CLKS_ON. |
| 1405 | */ |
| 1406 | if (hba->clk_gating.is_suspended || |
| 1407 | (hba->clk_gating.state == REQ_CLKS_ON)) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1408 | hba->clk_gating.state = CLKS_ON; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1409 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1410 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1411 | goto rel_lock; |
| 1412 | } |
| 1413 | |
| 1414 | if (hba->clk_gating.active_reqs |
| 1415 | || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL |
| 1416 | || hba->lrb_in_use || hba->outstanding_tasks |
| 1417 | || hba->active_uic_cmd || hba->uic_async_done) |
| 1418 | goto rel_lock; |
| 1419 | |
| 1420 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1421 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1422 | if (ufshcd_is_hibern8_on_idle_allowed(hba) && |
| 1423 | hba->hibern8_on_idle.is_enabled) |
| 1424 | /* |
| 1425 | * Hibern8 enter work (on Idle) needs clocks to be ON hence |
| 1426 | * make sure that it is flushed before turning off the clocks. |
| 1427 | */ |
| 1428 | flush_delayed_work(&hba->hibern8_on_idle.enter_work); |
| 1429 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1430 | /* put the link into hibern8 mode before turning off clocks */ |
| 1431 | if (ufshcd_can_hibern8_during_gating(hba)) { |
| 1432 | if (ufshcd_uic_hibern8_enter(hba)) { |
| 1433 | hba->clk_gating.state = CLKS_ON; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1434 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1435 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1436 | goto out; |
| 1437 | } |
| 1438 | ufshcd_set_link_hibern8(hba); |
| 1439 | } |
| 1440 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 1441 | /* |
| 1442 | * If auto hibern8 is supported then the link will already |
| 1443 | * be in hibern8 state and the ref clock can be gated. |
| 1444 | */ |
| 1445 | if ((ufshcd_is_auto_hibern8_supported(hba) || |
| 1446 | !ufshcd_is_link_active(hba)) && !hba->no_ref_clk_gating) |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1447 | ufshcd_disable_clocks(hba, true); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1448 | else |
| 1449 | /* If link is active, device ref_clk can't be switched off */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1450 | ufshcd_disable_clocks_skip_ref_clk(hba, true); |
| 1451 | |
| 1452 | /* Put the host controller in low power mode if possible */ |
| 1453 | ufshcd_hba_vreg_set_lpm(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1454 | |
| 1455 | /* |
| 1456 | * In case you are here to cancel this work the gating state |
| 1457 | * would be marked as REQ_CLKS_ON. In this case keep the state |
| 1458 | * as REQ_CLKS_ON which would anyway imply that clocks are off |
| 1459 | * and a request to turn them on is pending. By doing this way, |
| 1460 | * we keep the state machine in tact and this would ultimately |
| 1461 | * prevent from doing cancel work multiple times when there are |
| 1462 | * new requests arriving before the current cancel work is done. |
| 1463 | */ |
| 1464 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1465 | if (hba->clk_gating.state == REQ_CLKS_OFF) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1466 | hba->clk_gating.state = CLKS_OFF; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1467 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1468 | hba->clk_gating.state); |
| 1469 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1470 | rel_lock: |
| 1471 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1472 | out: |
| 1473 | return; |
| 1474 | } |
| 1475 | |
| 1476 | /* host lock must be held before calling this variant */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1477 | static void __ufshcd_release(struct ufs_hba *hba, bool no_sched) |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1478 | { |
| 1479 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1480 | return; |
| 1481 | |
| 1482 | hba->clk_gating.active_reqs--; |
| 1483 | |
| 1484 | if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended |
| 1485 | || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL |
| 1486 | || hba->lrb_in_use || hba->outstanding_tasks |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 1487 | || hba->active_uic_cmd || hba->uic_async_done |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1488 | || ufshcd_eh_in_progress(hba) || no_sched) |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1489 | return; |
| 1490 | |
| 1491 | hba->clk_gating.state = REQ_CLKS_OFF; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1492 | trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state); |
| 1493 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 1494 | hrtimer_start(&hba->clk_gating.gate_hrtimer, |
| 1495 | ms_to_ktime(hba->clk_gating.delay_ms), |
| 1496 | HRTIMER_MODE_REL); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1497 | } |
| 1498 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1499 | void ufshcd_release(struct ufs_hba *hba, bool no_sched) |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1500 | { |
| 1501 | unsigned long flags; |
| 1502 | |
| 1503 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1504 | __ufshcd_release(hba, no_sched); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1505 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1506 | } |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 1507 | EXPORT_SYMBOL_GPL(ufshcd_release); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1508 | |
| 1509 | static ssize_t ufshcd_clkgate_delay_show(struct device *dev, |
| 1510 | struct device_attribute *attr, char *buf) |
| 1511 | { |
| 1512 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1513 | |
| 1514 | return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms); |
| 1515 | } |
| 1516 | |
| 1517 | static ssize_t ufshcd_clkgate_delay_store(struct device *dev, |
| 1518 | struct device_attribute *attr, const char *buf, size_t count) |
| 1519 | { |
| 1520 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1521 | unsigned long flags, value; |
| 1522 | |
| 1523 | if (kstrtoul(buf, 0, &value)) |
| 1524 | return -EINVAL; |
| 1525 | |
| 1526 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1527 | hba->clk_gating.delay_ms = value; |
| 1528 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1529 | return count; |
| 1530 | } |
| 1531 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1532 | static ssize_t ufshcd_clkgate_delay_pwr_save_show(struct device *dev, |
| 1533 | struct device_attribute *attr, char *buf) |
| 1534 | { |
| 1535 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1536 | |
| 1537 | return snprintf(buf, PAGE_SIZE, "%lu\n", |
| 1538 | hba->clk_gating.delay_ms_pwr_save); |
| 1539 | } |
| 1540 | |
| 1541 | static ssize_t ufshcd_clkgate_delay_pwr_save_store(struct device *dev, |
| 1542 | struct device_attribute *attr, const char *buf, size_t count) |
| 1543 | { |
| 1544 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1545 | unsigned long flags, value; |
| 1546 | |
| 1547 | if (kstrtoul(buf, 0, &value)) |
| 1548 | return -EINVAL; |
| 1549 | |
| 1550 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1551 | |
| 1552 | hba->clk_gating.delay_ms_pwr_save = value; |
| 1553 | if (ufshcd_is_clkscaling_supported(hba) && |
| 1554 | !hba->clk_scaling.is_scaled_up) |
| 1555 | hba->clk_gating.delay_ms = hba->clk_gating.delay_ms_pwr_save; |
| 1556 | |
| 1557 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1558 | return count; |
| 1559 | } |
| 1560 | |
| 1561 | static ssize_t ufshcd_clkgate_delay_perf_show(struct device *dev, |
| 1562 | struct device_attribute *attr, char *buf) |
| 1563 | { |
| 1564 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1565 | |
| 1566 | return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms_perf); |
| 1567 | } |
| 1568 | |
| 1569 | static ssize_t ufshcd_clkgate_delay_perf_store(struct device *dev, |
| 1570 | struct device_attribute *attr, const char *buf, size_t count) |
| 1571 | { |
| 1572 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1573 | unsigned long flags, value; |
| 1574 | |
| 1575 | if (kstrtoul(buf, 0, &value)) |
| 1576 | return -EINVAL; |
| 1577 | |
| 1578 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1579 | |
| 1580 | hba->clk_gating.delay_ms_perf = value; |
| 1581 | if (ufshcd_is_clkscaling_supported(hba) && |
| 1582 | hba->clk_scaling.is_scaled_up) |
| 1583 | hba->clk_gating.delay_ms = hba->clk_gating.delay_ms_perf; |
| 1584 | |
| 1585 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1586 | return count; |
| 1587 | } |
| 1588 | |
| 1589 | static ssize_t ufshcd_clkgate_enable_show(struct device *dev, |
| 1590 | struct device_attribute *attr, char *buf) |
| 1591 | { |
| 1592 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1593 | |
| 1594 | return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_gating.is_enabled); |
| 1595 | } |
| 1596 | |
| 1597 | static ssize_t ufshcd_clkgate_enable_store(struct device *dev, |
| 1598 | struct device_attribute *attr, const char *buf, size_t count) |
| 1599 | { |
| 1600 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1601 | unsigned long flags; |
| 1602 | u32 value; |
| 1603 | |
| 1604 | if (kstrtou32(buf, 0, &value)) |
| 1605 | return -EINVAL; |
| 1606 | |
| 1607 | value = !!value; |
| 1608 | if (value == hba->clk_gating.is_enabled) |
| 1609 | goto out; |
| 1610 | |
| 1611 | if (value) { |
| 1612 | ufshcd_release(hba, false); |
| 1613 | } else { |
| 1614 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1615 | hba->clk_gating.active_reqs++; |
| 1616 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1617 | } |
| 1618 | |
| 1619 | hba->clk_gating.is_enabled = value; |
| 1620 | out: |
| 1621 | return count; |
| 1622 | } |
| 1623 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 1624 | static enum hrtimer_restart ufshcd_clkgate_hrtimer_handler( |
| 1625 | struct hrtimer *timer) |
| 1626 | { |
| 1627 | struct ufs_hba *hba = container_of(timer, struct ufs_hba, |
| 1628 | clk_gating.gate_hrtimer); |
| 1629 | |
| 1630 | schedule_work(&hba->clk_gating.gate_work); |
| 1631 | |
| 1632 | return HRTIMER_NORESTART; |
| 1633 | } |
| 1634 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1635 | static void ufshcd_init_clk_gating(struct ufs_hba *hba) |
| 1636 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1637 | struct ufs_clk_gating *gating = &hba->clk_gating; |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 1638 | char wq_name[sizeof("ufs_clk_ungating_00")]; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1639 | |
| 1640 | hba->clk_gating.state = CLKS_ON; |
| 1641 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1642 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1643 | return; |
| 1644 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 1645 | /* |
| 1646 | * Disable hibern8 during clk gating if |
| 1647 | * auto hibern8 is supported |
| 1648 | */ |
| 1649 | if (ufshcd_is_auto_hibern8_supported(hba)) |
| 1650 | hba->caps &= ~UFSHCD_CAP_HIBERN8_WITH_CLK_GATING; |
| 1651 | |
| 1652 | INIT_WORK(&gating->gate_work, ufshcd_gate_work); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1653 | INIT_WORK(&gating->ungate_work, ufshcd_ungate_work); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 1654 | /* |
| 1655 | * Clock gating work must be executed only after auto hibern8 |
| 1656 | * timeout has expired in the hardware or after aggressive |
| 1657 | * hibern8 on idle software timeout. Using jiffy based low |
| 1658 | * resolution delayed work is not reliable to guarantee this, |
| 1659 | * hence use a high resolution timer to make sure we schedule |
| 1660 | * the gate work precisely more than hibern8 timeout. |
| 1661 | * |
| 1662 | * Always make sure gating->delay_ms > hibern8_on_idle->delay_ms |
| 1663 | */ |
| 1664 | hrtimer_init(&gating->gate_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 1665 | gating->gate_hrtimer.function = ufshcd_clkgate_hrtimer_handler; |
| 1666 | |
| 1667 | snprintf(wq_name, ARRAY_SIZE(wq_name), "ufs_clk_ungating_%d", |
| 1668 | hba->host->host_no); |
| 1669 | hba->clk_gating.ungating_workq = create_singlethread_workqueue(wq_name); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1670 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1671 | gating->is_enabled = true; |
| 1672 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 1673 | gating->delay_ms_pwr_save = UFSHCD_CLK_GATING_DELAY_MS_PWR_SAVE; |
| 1674 | gating->delay_ms_perf = UFSHCD_CLK_GATING_DELAY_MS_PERF; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1675 | |
| 1676 | /* start with performance mode */ |
| 1677 | gating->delay_ms = gating->delay_ms_perf; |
| 1678 | |
| 1679 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 1680 | goto scaling_not_supported; |
| 1681 | |
| 1682 | gating->delay_pwr_save_attr.show = ufshcd_clkgate_delay_pwr_save_show; |
| 1683 | gating->delay_pwr_save_attr.store = ufshcd_clkgate_delay_pwr_save_store; |
| 1684 | sysfs_attr_init(&gating->delay_pwr_save_attr.attr); |
| 1685 | gating->delay_pwr_save_attr.attr.name = "clkgate_delay_ms_pwr_save"; |
| 1686 | gating->delay_pwr_save_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 1687 | if (device_create_file(hba->dev, &gating->delay_pwr_save_attr)) |
| 1688 | dev_err(hba->dev, "Failed to create sysfs for clkgate_delay_ms_pwr_save\n"); |
| 1689 | |
| 1690 | gating->delay_perf_attr.show = ufshcd_clkgate_delay_perf_show; |
| 1691 | gating->delay_perf_attr.store = ufshcd_clkgate_delay_perf_store; |
| 1692 | sysfs_attr_init(&gating->delay_perf_attr.attr); |
| 1693 | gating->delay_perf_attr.attr.name = "clkgate_delay_ms_perf"; |
| 1694 | gating->delay_perf_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 1695 | if (device_create_file(hba->dev, &gating->delay_perf_attr)) |
| 1696 | dev_err(hba->dev, "Failed to create sysfs for clkgate_delay_ms_perf\n"); |
| 1697 | |
| 1698 | goto add_clkgate_enable; |
| 1699 | |
| 1700 | scaling_not_supported: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1701 | hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show; |
| 1702 | hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store; |
| 1703 | sysfs_attr_init(&hba->clk_gating.delay_attr.attr); |
| 1704 | hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms"; |
| 1705 | hba->clk_gating.delay_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 1706 | if (device_create_file(hba->dev, &hba->clk_gating.delay_attr)) |
| 1707 | dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n"); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1708 | |
| 1709 | add_clkgate_enable: |
| 1710 | gating->enable_attr.show = ufshcd_clkgate_enable_show; |
| 1711 | gating->enable_attr.store = ufshcd_clkgate_enable_store; |
| 1712 | sysfs_attr_init(&gating->enable_attr.attr); |
| 1713 | gating->enable_attr.attr.name = "clkgate_enable"; |
| 1714 | gating->enable_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 1715 | if (device_create_file(hba->dev, &gating->enable_attr)) |
| 1716 | dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n"); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | static void ufshcd_exit_clk_gating(struct ufs_hba *hba) |
| 1720 | { |
| 1721 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1722 | return; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1723 | if (ufshcd_is_clkscaling_supported(hba)) { |
| 1724 | device_remove_file(hba->dev, |
| 1725 | &hba->clk_gating.delay_pwr_save_attr); |
| 1726 | device_remove_file(hba->dev, &hba->clk_gating.delay_perf_attr); |
| 1727 | } else { |
| 1728 | device_remove_file(hba->dev, &hba->clk_gating.delay_attr); |
| 1729 | } |
| 1730 | device_remove_file(hba->dev, &hba->clk_gating.enable_attr); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 1731 | ufshcd_cancel_gate_work(hba); |
Akinobu Mita | 97cd680 | 2014-11-24 14:24:18 +0900 | [diff] [blame] | 1732 | cancel_work_sync(&hba->clk_gating.ungate_work); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 1733 | destroy_workqueue(hba->clk_gating.ungating_workq); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1734 | } |
| 1735 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1736 | static void ufshcd_set_auto_hibern8_timer(struct ufs_hba *hba, u32 delay) |
| 1737 | { |
| 1738 | ufshcd_rmwl(hba, AUTO_HIBERN8_TIMER_SCALE_MASK | |
| 1739 | AUTO_HIBERN8_IDLE_TIMER_MASK, |
| 1740 | AUTO_HIBERN8_TIMER_SCALE_1_MS | delay, |
| 1741 | REG_AUTO_HIBERN8_IDLE_TIMER); |
| 1742 | /* Make sure the timer gets applied before further operations */ |
| 1743 | mb(); |
| 1744 | } |
| 1745 | |
| 1746 | /** |
| 1747 | * ufshcd_hibern8_hold - Make sure that link is not in hibern8. |
| 1748 | * |
| 1749 | * @hba: per adapter instance |
| 1750 | * @async: This indicates whether caller wants to exit hibern8 asynchronously. |
| 1751 | * |
| 1752 | * Exit from hibern8 mode and set the link as active. |
| 1753 | * |
| 1754 | * Return 0 on success, non-zero on failure. |
| 1755 | */ |
| 1756 | static int ufshcd_hibern8_hold(struct ufs_hba *hba, bool async) |
| 1757 | { |
| 1758 | int rc = 0; |
| 1759 | unsigned long flags; |
| 1760 | |
| 1761 | if (!ufshcd_is_hibern8_on_idle_allowed(hba)) |
| 1762 | goto out; |
| 1763 | |
| 1764 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1765 | hba->hibern8_on_idle.active_reqs++; |
| 1766 | |
| 1767 | if (ufshcd_eh_in_progress(hba)) { |
| 1768 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1769 | return 0; |
| 1770 | } |
| 1771 | |
| 1772 | start: |
| 1773 | switch (hba->hibern8_on_idle.state) { |
| 1774 | case HIBERN8_EXITED: |
| 1775 | break; |
| 1776 | case REQ_HIBERN8_ENTER: |
| 1777 | if (cancel_delayed_work(&hba->hibern8_on_idle.enter_work)) { |
| 1778 | hba->hibern8_on_idle.state = HIBERN8_EXITED; |
| 1779 | trace_ufshcd_hibern8_on_idle(dev_name(hba->dev), |
| 1780 | hba->hibern8_on_idle.state); |
| 1781 | break; |
| 1782 | } |
| 1783 | /* |
| 1784 | * If we here, it means Hibern8 enter work is either done or |
| 1785 | * currently running. Hence, fall through to cancel hibern8 |
| 1786 | * work and exit hibern8. |
| 1787 | */ |
| 1788 | case HIBERN8_ENTERED: |
| 1789 | __ufshcd_scsi_block_requests(hba); |
| 1790 | hba->hibern8_on_idle.state = REQ_HIBERN8_EXIT; |
| 1791 | trace_ufshcd_hibern8_on_idle(dev_name(hba->dev), |
| 1792 | hba->hibern8_on_idle.state); |
| 1793 | schedule_work(&hba->hibern8_on_idle.exit_work); |
| 1794 | /* |
| 1795 | * fall through to check if we should wait for this |
| 1796 | * work to be done or not. |
| 1797 | */ |
| 1798 | case REQ_HIBERN8_EXIT: |
| 1799 | if (async) { |
| 1800 | rc = -EAGAIN; |
| 1801 | hba->hibern8_on_idle.active_reqs--; |
| 1802 | break; |
| 1803 | } else { |
| 1804 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1805 | flush_work(&hba->hibern8_on_idle.exit_work); |
| 1806 | /* Make sure state is HIBERN8_EXITED before returning */ |
| 1807 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1808 | goto start; |
| 1809 | } |
| 1810 | default: |
| 1811 | dev_err(hba->dev, "%s: H8 is in invalid state %d\n", |
| 1812 | __func__, hba->hibern8_on_idle.state); |
| 1813 | break; |
| 1814 | } |
| 1815 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1816 | out: |
| 1817 | return rc; |
| 1818 | } |
| 1819 | |
| 1820 | /* host lock must be held before calling this variant */ |
| 1821 | static void __ufshcd_hibern8_release(struct ufs_hba *hba, bool no_sched) |
| 1822 | { |
| 1823 | unsigned long delay_in_jiffies; |
| 1824 | |
| 1825 | if (!ufshcd_is_hibern8_on_idle_allowed(hba)) |
| 1826 | return; |
| 1827 | |
| 1828 | hba->hibern8_on_idle.active_reqs--; |
| 1829 | BUG_ON(hba->hibern8_on_idle.active_reqs < 0); |
| 1830 | |
| 1831 | if (hba->hibern8_on_idle.active_reqs |
| 1832 | || hba->hibern8_on_idle.is_suspended |
| 1833 | || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL |
| 1834 | || hba->lrb_in_use || hba->outstanding_tasks |
| 1835 | || hba->active_uic_cmd || hba->uic_async_done |
| 1836 | || ufshcd_eh_in_progress(hba) || no_sched) |
| 1837 | return; |
| 1838 | |
| 1839 | hba->hibern8_on_idle.state = REQ_HIBERN8_ENTER; |
| 1840 | trace_ufshcd_hibern8_on_idle(dev_name(hba->dev), |
| 1841 | hba->hibern8_on_idle.state); |
| 1842 | /* |
| 1843 | * Scheduling the delayed work after 1 jiffies will make the work to |
| 1844 | * get schedule any time from 0ms to 1000/HZ ms which is not desirable |
| 1845 | * for hibern8 enter work as it may impact the performance if it gets |
| 1846 | * scheduled almost immediately. Hence make sure that hibern8 enter |
| 1847 | * work gets scheduled atleast after 2 jiffies (any time between |
| 1848 | * 1000/HZ ms to 2000/HZ ms). |
| 1849 | */ |
| 1850 | delay_in_jiffies = msecs_to_jiffies(hba->hibern8_on_idle.delay_ms); |
| 1851 | if (delay_in_jiffies == 1) |
| 1852 | delay_in_jiffies++; |
| 1853 | |
| 1854 | schedule_delayed_work(&hba->hibern8_on_idle.enter_work, |
| 1855 | delay_in_jiffies); |
| 1856 | } |
| 1857 | |
| 1858 | static void ufshcd_hibern8_release(struct ufs_hba *hba, bool no_sched) |
| 1859 | { |
| 1860 | unsigned long flags; |
| 1861 | |
| 1862 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1863 | __ufshcd_hibern8_release(hba, no_sched); |
| 1864 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1865 | } |
| 1866 | |
| 1867 | static void ufshcd_hibern8_enter_work(struct work_struct *work) |
| 1868 | { |
| 1869 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1870 | hibern8_on_idle.enter_work.work); |
| 1871 | unsigned long flags; |
| 1872 | |
| 1873 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1874 | if (hba->hibern8_on_idle.is_suspended) { |
| 1875 | hba->hibern8_on_idle.state = HIBERN8_EXITED; |
| 1876 | trace_ufshcd_hibern8_on_idle(dev_name(hba->dev), |
| 1877 | hba->hibern8_on_idle.state); |
| 1878 | goto rel_lock; |
| 1879 | } |
| 1880 | |
| 1881 | if (hba->hibern8_on_idle.active_reqs |
| 1882 | || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL |
| 1883 | || hba->lrb_in_use || hba->outstanding_tasks |
| 1884 | || hba->active_uic_cmd || hba->uic_async_done) |
| 1885 | goto rel_lock; |
| 1886 | |
| 1887 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1888 | |
| 1889 | if (ufshcd_is_link_active(hba) && ufshcd_uic_hibern8_enter(hba)) { |
| 1890 | /* Enter failed */ |
| 1891 | hba->hibern8_on_idle.state = HIBERN8_EXITED; |
| 1892 | trace_ufshcd_hibern8_on_idle(dev_name(hba->dev), |
| 1893 | hba->hibern8_on_idle.state); |
| 1894 | goto out; |
| 1895 | } |
| 1896 | ufshcd_set_link_hibern8(hba); |
| 1897 | |
| 1898 | /* |
| 1899 | * In case you are here to cancel this work the hibern8_on_idle.state |
| 1900 | * would be marked as REQ_HIBERN8_EXIT. In this case keep the state |
| 1901 | * as REQ_HIBERN8_EXIT which would anyway imply that we are in hibern8 |
| 1902 | * and a request to exit from it is pending. By doing this way, |
| 1903 | * we keep the state machine in tact and this would ultimately |
| 1904 | * prevent from doing cancel work multiple times when there are |
| 1905 | * new requests arriving before the current cancel work is done. |
| 1906 | */ |
| 1907 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1908 | if (hba->hibern8_on_idle.state == REQ_HIBERN8_ENTER) { |
| 1909 | hba->hibern8_on_idle.state = HIBERN8_ENTERED; |
| 1910 | trace_ufshcd_hibern8_on_idle(dev_name(hba->dev), |
| 1911 | hba->hibern8_on_idle.state); |
| 1912 | } |
| 1913 | rel_lock: |
| 1914 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1915 | out: |
| 1916 | return; |
| 1917 | } |
| 1918 | |
| 1919 | static void ufshcd_hibern8_exit_work(struct work_struct *work) |
| 1920 | { |
| 1921 | int ret; |
| 1922 | unsigned long flags; |
| 1923 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1924 | hibern8_on_idle.exit_work); |
| 1925 | |
| 1926 | cancel_delayed_work_sync(&hba->hibern8_on_idle.enter_work); |
| 1927 | |
| 1928 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1929 | if ((hba->hibern8_on_idle.state == HIBERN8_EXITED) |
| 1930 | || ufshcd_is_link_active(hba)) { |
| 1931 | hba->hibern8_on_idle.state = HIBERN8_EXITED; |
| 1932 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1933 | goto unblock_reqs; |
| 1934 | } |
| 1935 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1936 | |
| 1937 | /* Exit from hibern8 */ |
| 1938 | if (ufshcd_is_link_hibern8(hba)) { |
| 1939 | ufshcd_hold(hba, false); |
| 1940 | ret = ufshcd_uic_hibern8_exit(hba); |
| 1941 | ufshcd_release(hba, false); |
| 1942 | if (!ret) { |
| 1943 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1944 | ufshcd_set_link_active(hba); |
| 1945 | hba->hibern8_on_idle.state = HIBERN8_EXITED; |
| 1946 | trace_ufshcd_hibern8_on_idle(dev_name(hba->dev), |
| 1947 | hba->hibern8_on_idle.state); |
| 1948 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1949 | } |
| 1950 | } |
| 1951 | unblock_reqs: |
| 1952 | ufshcd_scsi_unblock_requests(hba); |
| 1953 | } |
| 1954 | |
| 1955 | static ssize_t ufshcd_hibern8_on_idle_delay_show(struct device *dev, |
| 1956 | struct device_attribute *attr, char *buf) |
| 1957 | { |
| 1958 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1959 | |
| 1960 | return snprintf(buf, PAGE_SIZE, "%lu\n", hba->hibern8_on_idle.delay_ms); |
| 1961 | } |
| 1962 | |
| 1963 | static ssize_t ufshcd_hibern8_on_idle_delay_store(struct device *dev, |
| 1964 | struct device_attribute *attr, const char *buf, size_t count) |
| 1965 | { |
| 1966 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1967 | unsigned long flags, value; |
| 1968 | |
| 1969 | if (kstrtoul(buf, 0, &value)) |
| 1970 | return -EINVAL; |
| 1971 | |
| 1972 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1973 | hba->hibern8_on_idle.delay_ms = value; |
| 1974 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1975 | |
| 1976 | /* Update auto hibern8 timer value if supported */ |
| 1977 | if (ufshcd_is_auto_hibern8_supported(hba) && |
| 1978 | hba->hibern8_on_idle.is_enabled) |
| 1979 | ufshcd_set_auto_hibern8_timer(hba, |
| 1980 | hba->hibern8_on_idle.delay_ms); |
| 1981 | |
| 1982 | return count; |
| 1983 | } |
| 1984 | |
| 1985 | static ssize_t ufshcd_hibern8_on_idle_enable_show(struct device *dev, |
| 1986 | struct device_attribute *attr, char *buf) |
| 1987 | { |
| 1988 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1989 | |
| 1990 | return snprintf(buf, PAGE_SIZE, "%d\n", |
| 1991 | hba->hibern8_on_idle.is_enabled); |
| 1992 | } |
| 1993 | |
| 1994 | static ssize_t ufshcd_hibern8_on_idle_enable_store(struct device *dev, |
| 1995 | struct device_attribute *attr, const char *buf, size_t count) |
| 1996 | { |
| 1997 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1998 | unsigned long flags; |
| 1999 | u32 value; |
| 2000 | |
| 2001 | if (kstrtou32(buf, 0, &value)) |
| 2002 | return -EINVAL; |
| 2003 | |
| 2004 | value = !!value; |
| 2005 | if (value == hba->hibern8_on_idle.is_enabled) |
| 2006 | goto out; |
| 2007 | |
| 2008 | /* Update auto hibern8 timer value if supported */ |
| 2009 | if (ufshcd_is_auto_hibern8_supported(hba)) { |
| 2010 | ufshcd_set_auto_hibern8_timer(hba, |
| 2011 | value ? hba->hibern8_on_idle.delay_ms : value); |
| 2012 | goto update; |
| 2013 | } |
| 2014 | |
| 2015 | if (value) { |
| 2016 | /* |
| 2017 | * As clock gating work would wait for the hibern8 enter work |
| 2018 | * to finish, clocks would remain on during hibern8 enter work. |
| 2019 | */ |
| 2020 | ufshcd_hold(hba, false); |
| 2021 | ufshcd_release_all(hba); |
| 2022 | } else { |
| 2023 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2024 | hba->hibern8_on_idle.active_reqs++; |
| 2025 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2026 | } |
| 2027 | |
| 2028 | update: |
| 2029 | hba->hibern8_on_idle.is_enabled = value; |
| 2030 | out: |
| 2031 | return count; |
| 2032 | } |
| 2033 | |
| 2034 | static void ufshcd_init_hibern8_on_idle(struct ufs_hba *hba) |
| 2035 | { |
| 2036 | /* initialize the state variable here */ |
| 2037 | hba->hibern8_on_idle.state = HIBERN8_EXITED; |
| 2038 | |
| 2039 | if (!ufshcd_is_hibern8_on_idle_allowed(hba) && |
| 2040 | !ufshcd_is_auto_hibern8_supported(hba)) |
| 2041 | return; |
| 2042 | |
| 2043 | if (ufshcd_is_auto_hibern8_supported(hba)) { |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 2044 | hba->hibern8_on_idle.delay_ms = 1; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2045 | hba->hibern8_on_idle.state = AUTO_HIBERN8; |
| 2046 | /* |
| 2047 | * Disable SW hibern8 enter on idle in case |
| 2048 | * auto hibern8 is supported |
| 2049 | */ |
| 2050 | hba->caps &= ~UFSHCD_CAP_HIBERN8_ENTER_ON_IDLE; |
| 2051 | } else { |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 2052 | hba->hibern8_on_idle.delay_ms = 10; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2053 | INIT_DELAYED_WORK(&hba->hibern8_on_idle.enter_work, |
| 2054 | ufshcd_hibern8_enter_work); |
| 2055 | INIT_WORK(&hba->hibern8_on_idle.exit_work, |
| 2056 | ufshcd_hibern8_exit_work); |
| 2057 | } |
| 2058 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2059 | hba->hibern8_on_idle.is_enabled = true; |
| 2060 | |
| 2061 | hba->hibern8_on_idle.delay_attr.show = |
| 2062 | ufshcd_hibern8_on_idle_delay_show; |
| 2063 | hba->hibern8_on_idle.delay_attr.store = |
| 2064 | ufshcd_hibern8_on_idle_delay_store; |
| 2065 | sysfs_attr_init(&hba->hibern8_on_idle.delay_attr.attr); |
| 2066 | hba->hibern8_on_idle.delay_attr.attr.name = "hibern8_on_idle_delay_ms"; |
| 2067 | hba->hibern8_on_idle.delay_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 2068 | if (device_create_file(hba->dev, &hba->hibern8_on_idle.delay_attr)) |
| 2069 | dev_err(hba->dev, "Failed to create sysfs for hibern8_on_idle_delay\n"); |
| 2070 | |
| 2071 | hba->hibern8_on_idle.enable_attr.show = |
| 2072 | ufshcd_hibern8_on_idle_enable_show; |
| 2073 | hba->hibern8_on_idle.enable_attr.store = |
| 2074 | ufshcd_hibern8_on_idle_enable_store; |
| 2075 | sysfs_attr_init(&hba->hibern8_on_idle.enable_attr.attr); |
| 2076 | hba->hibern8_on_idle.enable_attr.attr.name = "hibern8_on_idle_enable"; |
| 2077 | hba->hibern8_on_idle.enable_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 2078 | if (device_create_file(hba->dev, &hba->hibern8_on_idle.enable_attr)) |
| 2079 | dev_err(hba->dev, "Failed to create sysfs for hibern8_on_idle_enable\n"); |
| 2080 | } |
| 2081 | |
| 2082 | static void ufshcd_exit_hibern8_on_idle(struct ufs_hba *hba) |
| 2083 | { |
| 2084 | if (!ufshcd_is_hibern8_on_idle_allowed(hba) && |
| 2085 | !ufshcd_is_auto_hibern8_supported(hba)) |
| 2086 | return; |
| 2087 | device_remove_file(hba->dev, &hba->hibern8_on_idle.delay_attr); |
| 2088 | device_remove_file(hba->dev, &hba->hibern8_on_idle.enable_attr); |
| 2089 | } |
| 2090 | |
| 2091 | static void ufshcd_hold_all(struct ufs_hba *hba) |
| 2092 | { |
| 2093 | ufshcd_hold(hba, false); |
| 2094 | ufshcd_hibern8_hold(hba, false); |
| 2095 | } |
| 2096 | |
| 2097 | static void ufshcd_release_all(struct ufs_hba *hba) |
| 2098 | { |
| 2099 | ufshcd_hibern8_release(hba, false); |
| 2100 | ufshcd_release(hba, false); |
| 2101 | } |
| 2102 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 2103 | /* Must be called with host lock acquired */ |
| 2104 | static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba) |
| 2105 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2106 | bool queue_resume_work = false; |
| 2107 | |
| 2108 | if (!ufshcd_is_clkscaling_supported(hba)) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 2109 | return; |
| 2110 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2111 | if (!hba->clk_scaling.active_reqs++) |
| 2112 | queue_resume_work = true; |
| 2113 | |
| 2114 | if (!hba->clk_scaling.is_allowed || hba->pm_op_in_progress) |
| 2115 | return; |
| 2116 | |
| 2117 | if (queue_resume_work) |
| 2118 | queue_work(hba->clk_scaling.workq, |
| 2119 | &hba->clk_scaling.resume_work); |
| 2120 | |
| 2121 | if (!hba->clk_scaling.window_start_t) { |
| 2122 | hba->clk_scaling.window_start_t = jiffies; |
| 2123 | hba->clk_scaling.tot_busy_t = 0; |
| 2124 | hba->clk_scaling.is_busy_started = false; |
| 2125 | } |
| 2126 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 2127 | if (!hba->clk_scaling.is_busy_started) { |
| 2128 | hba->clk_scaling.busy_start_t = ktime_get(); |
| 2129 | hba->clk_scaling.is_busy_started = true; |
| 2130 | } |
| 2131 | } |
| 2132 | |
| 2133 | static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba) |
| 2134 | { |
| 2135 | struct ufs_clk_scaling *scaling = &hba->clk_scaling; |
| 2136 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2137 | if (!ufshcd_is_clkscaling_supported(hba)) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 2138 | return; |
| 2139 | |
| 2140 | if (!hba->outstanding_reqs && scaling->is_busy_started) { |
| 2141 | scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(), |
| 2142 | scaling->busy_start_t)); |
| 2143 | scaling->busy_start_t = ktime_set(0, 0); |
| 2144 | scaling->is_busy_started = false; |
| 2145 | } |
| 2146 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2147 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2148 | /** |
| 2149 | * ufshcd_send_command - Send SCSI or device management commands |
| 2150 | * @hba: per adapter instance |
| 2151 | * @task_tag: Task tag of the command |
| 2152 | */ |
| 2153 | static inline |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2154 | int ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2155 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2156 | int ret = 0; |
| 2157 | |
| 2158 | hba->lrb[task_tag].issue_time_stamp = ktime_get(); |
| 2159 | hba->lrb[task_tag].complete_time_stamp = ktime_set(0, 0); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 2160 | ufshcd_clk_scaling_start_busy(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2161 | __set_bit(task_tag, &hba->outstanding_reqs); |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 2162 | ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2163 | /* Make sure that doorbell is committed immediately */ |
| 2164 | wmb(); |
| 2165 | ufshcd_cond_add_cmd_trace(hba, task_tag, "send"); |
| 2166 | ufshcd_update_tag_stats(hba, task_tag); |
| 2167 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2168 | } |
| 2169 | |
| 2170 | /** |
| 2171 | * ufshcd_copy_sense_data - Copy sense data in case of check condition |
| 2172 | * @lrb - pointer to local reference block |
| 2173 | */ |
| 2174 | static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp) |
| 2175 | { |
| 2176 | int len; |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 2177 | if (lrbp->sense_buffer && |
| 2178 | ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2179 | int len_to_copy; |
| 2180 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2181 | len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2182 | len_to_copy = min_t(int, RESPONSE_UPIU_SENSE_DATA_LENGTH, len); |
| 2183 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2184 | memcpy(lrbp->sense_buffer, |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2185 | lrbp->ucd_rsp_ptr->sr.sense_data, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2186 | min_t(int, len_to_copy, UFSHCD_REQ_SENSE_SIZE)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2187 | } |
| 2188 | } |
| 2189 | |
| 2190 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2191 | * ufshcd_copy_query_response() - Copy the Query Response and the data |
| 2192 | * descriptor |
| 2193 | * @hba: per adapter instance |
| 2194 | * @lrb - pointer to local reference block |
| 2195 | */ |
| 2196 | static |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2197 | 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] | 2198 | { |
| 2199 | struct ufs_query_res *query_res = &hba->dev_cmd.query.response; |
| 2200 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2201 | 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] | 2202 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2203 | /* Get the descriptor */ |
| 2204 | if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2205 | u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2206 | GENERAL_UPIU_REQUEST_SIZE; |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2207 | u16 resp_len; |
| 2208 | u16 buf_len; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2209 | |
| 2210 | /* data segment length */ |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2211 | resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) & |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2212 | MASK_QUERY_DATA_SEG_LEN; |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 2213 | buf_len = be16_to_cpu( |
| 2214 | hba->dev_cmd.query.request.upiu_req.length); |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2215 | if (likely(buf_len >= resp_len)) { |
| 2216 | memcpy(hba->dev_cmd.query.descriptor, descp, resp_len); |
| 2217 | } else { |
| 2218 | dev_warn(hba->dev, |
| 2219 | "%s: Response size is bigger than buffer", |
| 2220 | __func__); |
| 2221 | return -EINVAL; |
| 2222 | } |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2223 | } |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2224 | |
| 2225 | return 0; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2226 | } |
| 2227 | |
| 2228 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2229 | * ufshcd_hba_capabilities - Read controller capabilities |
| 2230 | * @hba: per adapter instance |
| 2231 | */ |
| 2232 | static inline void ufshcd_hba_capabilities(struct ufs_hba *hba) |
| 2233 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 2234 | hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2235 | |
| 2236 | /* nutrs and nutmrs are 0 based values */ |
| 2237 | hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1; |
| 2238 | hba->nutmrs = |
| 2239 | ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1; |
| 2240 | } |
| 2241 | |
| 2242 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2243 | * ufshcd_ready_for_uic_cmd - Check if controller is ready |
| 2244 | * to accept UIC commands |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2245 | * @hba: per adapter instance |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2246 | * Return true on success, else false |
| 2247 | */ |
| 2248 | static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba) |
| 2249 | { |
| 2250 | if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY) |
| 2251 | return true; |
| 2252 | else |
| 2253 | return false; |
| 2254 | } |
| 2255 | |
| 2256 | /** |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 2257 | * ufshcd_get_upmcrs - Get the power mode change request status |
| 2258 | * @hba: Pointer to adapter instance |
| 2259 | * |
| 2260 | * This function gets the UPMCRS field of HCS register |
| 2261 | * Returns value of UPMCRS field |
| 2262 | */ |
| 2263 | static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba) |
| 2264 | { |
| 2265 | return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7; |
| 2266 | } |
| 2267 | |
| 2268 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2269 | * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers |
| 2270 | * @hba: per adapter instance |
| 2271 | * @uic_cmd: UIC command |
| 2272 | * |
| 2273 | * Mutex must be held. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2274 | */ |
| 2275 | static inline void |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2276 | 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] | 2277 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2278 | WARN_ON(hba->active_uic_cmd); |
| 2279 | |
| 2280 | hba->active_uic_cmd = uic_cmd; |
| 2281 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2282 | /* Write Args */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2283 | ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1); |
| 2284 | ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2); |
| 2285 | ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2286 | |
| 2287 | /* Write UIC Cmd */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2288 | ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK, |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 2289 | REG_UIC_COMMAND); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2290 | } |
| 2291 | |
| 2292 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2293 | * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command |
| 2294 | * @hba: per adapter instance |
| 2295 | * @uic_command: UIC command |
| 2296 | * |
| 2297 | * Must be called with mutex held. |
| 2298 | * Returns 0 only if success. |
| 2299 | */ |
| 2300 | static int |
| 2301 | ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) |
| 2302 | { |
| 2303 | int ret; |
| 2304 | unsigned long flags; |
| 2305 | |
| 2306 | if (wait_for_completion_timeout(&uic_cmd->done, |
| 2307 | msecs_to_jiffies(UIC_CMD_TIMEOUT))) |
| 2308 | ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT; |
| 2309 | else |
| 2310 | ret = -ETIMEDOUT; |
| 2311 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2312 | if (ret) |
| 2313 | ufsdbg_set_err_state(hba); |
| 2314 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2315 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2316 | hba->active_uic_cmd = NULL; |
| 2317 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2318 | |
| 2319 | return ret; |
| 2320 | } |
| 2321 | |
| 2322 | /** |
| 2323 | * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result |
| 2324 | * @hba: per adapter instance |
| 2325 | * @uic_cmd: UIC command |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2326 | * @completion: initialize the completion only if this is set to true |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2327 | * |
| 2328 | * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2329 | * with mutex held and host_lock locked. |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2330 | * Returns 0 only if success. |
| 2331 | */ |
| 2332 | static int |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2333 | __ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd, |
| 2334 | bool completion) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2335 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2336 | if (!ufshcd_ready_for_uic_cmd(hba)) { |
| 2337 | dev_err(hba->dev, |
| 2338 | "Controller not ready to accept UIC commands\n"); |
| 2339 | return -EIO; |
| 2340 | } |
| 2341 | |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2342 | if (completion) |
| 2343 | init_completion(&uic_cmd->done); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2344 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2345 | ufshcd_dispatch_uic_cmd(hba, uic_cmd); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2346 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2347 | return 0; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | /** |
| 2351 | * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result |
| 2352 | * @hba: per adapter instance |
| 2353 | * @uic_cmd: UIC command |
| 2354 | * |
| 2355 | * Returns 0 only if success. |
| 2356 | */ |
| 2357 | static int |
| 2358 | ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) |
| 2359 | { |
| 2360 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2361 | unsigned long flags; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2362 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2363 | ufshcd_hold_all(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2364 | mutex_lock(&hba->uic_cmd_mutex); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 2365 | ufshcd_add_delay_before_dme_cmd(hba); |
| 2366 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2367 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2368 | ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2369 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2370 | if (!ret) |
| 2371 | ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd); |
| 2372 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2373 | ufshcd_save_tstamp_of_last_dme_cmd(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2374 | mutex_unlock(&hba->uic_cmd_mutex); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2375 | ufshcd_release_all(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2376 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2377 | ufsdbg_error_inject_dispatcher(hba, |
| 2378 | ERR_INJECT_UIC, 0, &ret); |
| 2379 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2380 | return ret; |
| 2381 | } |
| 2382 | |
| 2383 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2384 | * ufshcd_map_sg - Map scatter-gather list to prdt |
| 2385 | * @lrbp - pointer to local reference block |
| 2386 | * |
| 2387 | * Returns 0 in case of success, non-zero value in case of failure |
| 2388 | */ |
| 2389 | static int ufshcd_map_sg(struct ufshcd_lrb *lrbp) |
| 2390 | { |
| 2391 | struct ufshcd_sg_entry *prd_table; |
| 2392 | struct scatterlist *sg; |
| 2393 | struct scsi_cmnd *cmd; |
| 2394 | int sg_segments; |
| 2395 | int i; |
| 2396 | |
| 2397 | cmd = lrbp->cmd; |
| 2398 | sg_segments = scsi_dma_map(cmd); |
| 2399 | if (sg_segments < 0) |
| 2400 | return sg_segments; |
| 2401 | |
| 2402 | if (sg_segments) { |
| 2403 | lrbp->utr_descriptor_ptr->prd_table_length = |
| 2404 | cpu_to_le16((u16) (sg_segments)); |
| 2405 | |
| 2406 | prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr; |
| 2407 | |
| 2408 | scsi_for_each_sg(cmd, sg, sg_segments, i) { |
| 2409 | prd_table[i].size = |
| 2410 | cpu_to_le32(((u32) sg_dma_len(sg))-1); |
| 2411 | prd_table[i].base_addr = |
| 2412 | cpu_to_le32(lower_32_bits(sg->dma_address)); |
| 2413 | prd_table[i].upper_addr = |
| 2414 | cpu_to_le32(upper_32_bits(sg->dma_address)); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2415 | prd_table[i].reserved = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2416 | } |
| 2417 | } else { |
| 2418 | lrbp->utr_descriptor_ptr->prd_table_length = 0; |
| 2419 | } |
| 2420 | |
| 2421 | return 0; |
| 2422 | } |
| 2423 | |
| 2424 | /** |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2425 | * ufshcd_enable_intr - enable interrupts |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2426 | * @hba: per adapter instance |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2427 | * @intrs: interrupt bits |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2428 | */ |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2429 | static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2430 | { |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2431 | u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
| 2432 | |
| 2433 | if (hba->ufs_version == UFSHCI_VERSION_10) { |
| 2434 | u32 rw; |
| 2435 | rw = set & INTERRUPT_MASK_RW_VER_10; |
| 2436 | set = rw | ((set ^ intrs) & intrs); |
| 2437 | } else { |
| 2438 | set |= intrs; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2439 | } |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2440 | |
| 2441 | ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE); |
| 2442 | } |
| 2443 | |
| 2444 | /** |
| 2445 | * ufshcd_disable_intr - disable interrupts |
| 2446 | * @hba: per adapter instance |
| 2447 | * @intrs: interrupt bits |
| 2448 | */ |
| 2449 | static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs) |
| 2450 | { |
| 2451 | u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
| 2452 | |
| 2453 | if (hba->ufs_version == UFSHCI_VERSION_10) { |
| 2454 | u32 rw; |
| 2455 | rw = (set & INTERRUPT_MASK_RW_VER_10) & |
| 2456 | ~(intrs & INTERRUPT_MASK_RW_VER_10); |
| 2457 | set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10); |
| 2458 | |
| 2459 | } else { |
| 2460 | set &= ~intrs; |
| 2461 | } |
| 2462 | |
| 2463 | ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2464 | } |
| 2465 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2466 | static int ufshcd_prepare_crypto_utrd(struct ufs_hba *hba, |
| 2467 | struct ufshcd_lrb *lrbp) |
| 2468 | { |
| 2469 | struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr; |
| 2470 | u8 cc_index = 0; |
| 2471 | bool enable = false; |
| 2472 | u64 dun = 0; |
| 2473 | int ret; |
| 2474 | |
| 2475 | /* |
| 2476 | * Call vendor specific code to get crypto info for this request: |
| 2477 | * enable, crypto config. index, DUN. |
| 2478 | * If bypass is set, don't bother setting the other fields. |
| 2479 | */ |
| 2480 | ret = ufshcd_vops_crypto_req_setup(hba, lrbp, &cc_index, &enable, &dun); |
| 2481 | if (ret) { |
| 2482 | if (ret != -EAGAIN) { |
| 2483 | dev_err(hba->dev, |
| 2484 | "%s: failed to setup crypto request (%d)\n", |
| 2485 | __func__, ret); |
| 2486 | } |
| 2487 | |
| 2488 | return ret; |
| 2489 | } |
| 2490 | |
| 2491 | if (!enable) |
| 2492 | goto out; |
| 2493 | |
| 2494 | req_desc->header.dword_0 |= cc_index | UTRD_CRYPTO_ENABLE; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2495 | req_desc->header.dword_1 = (u32)(dun & 0xFFFFFFFF); |
| 2496 | req_desc->header.dword_3 = (u32)((dun >> 32) & 0xFFFFFFFF); |
| 2497 | out: |
| 2498 | return 0; |
| 2499 | } |
| 2500 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2501 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2502 | * ufshcd_prepare_req_desc_hdr() - Fills the requests header |
| 2503 | * descriptor according to request |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2504 | * @hba: per adapter instance |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2505 | * @lrbp: pointer to local reference block |
| 2506 | * @upiu_flags: flags required in the header |
| 2507 | * @cmd_dir: requests data direction |
| 2508 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2509 | static int ufshcd_prepare_req_desc_hdr(struct ufs_hba *hba, |
| 2510 | struct ufshcd_lrb *lrbp, u32 *upiu_flags, |
| 2511 | enum dma_data_direction cmd_dir) |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2512 | { |
| 2513 | struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr; |
| 2514 | u32 data_direction; |
| 2515 | u32 dword_0; |
| 2516 | |
| 2517 | if (cmd_dir == DMA_FROM_DEVICE) { |
| 2518 | data_direction = UTP_DEVICE_TO_HOST; |
| 2519 | *upiu_flags = UPIU_CMD_FLAGS_READ; |
| 2520 | } else if (cmd_dir == DMA_TO_DEVICE) { |
| 2521 | data_direction = UTP_HOST_TO_DEVICE; |
| 2522 | *upiu_flags = UPIU_CMD_FLAGS_WRITE; |
| 2523 | } else { |
| 2524 | data_direction = UTP_NO_DATA_TRANSFER; |
| 2525 | *upiu_flags = UPIU_CMD_FLAGS_NONE; |
| 2526 | } |
| 2527 | |
| 2528 | dword_0 = data_direction | (lrbp->command_type |
| 2529 | << UPIU_COMMAND_TYPE_OFFSET); |
| 2530 | if (lrbp->intr_cmd) |
| 2531 | dword_0 |= UTP_REQ_DESC_INT_CMD; |
| 2532 | |
| 2533 | /* Transfer request descriptor header fields */ |
| 2534 | req_desc->header.dword_0 = cpu_to_le32(dword_0); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2535 | /* dword_1 is reserved, hence it is set to 0 */ |
| 2536 | req_desc->header.dword_1 = 0; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2537 | /* |
| 2538 | * assigning invalid value for command status. Controller |
| 2539 | * updates OCS on command completion, with the command |
| 2540 | * status |
| 2541 | */ |
| 2542 | req_desc->header.dword_2 = |
| 2543 | cpu_to_le32(OCS_INVALID_COMMAND_STATUS); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2544 | /* dword_3 is reserved, hence it is set to 0 */ |
| 2545 | req_desc->header.dword_3 = 0; |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 2546 | |
| 2547 | req_desc->prd_table_length = 0; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2548 | |
| 2549 | if (ufshcd_is_crypto_supported(hba)) |
| 2550 | return ufshcd_prepare_crypto_utrd(hba, lrbp); |
| 2551 | |
| 2552 | return 0; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2553 | } |
| 2554 | |
| 2555 | /** |
| 2556 | * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc, |
| 2557 | * for scsi commands |
| 2558 | * @lrbp - local reference block pointer |
| 2559 | * @upiu_flags - flags |
| 2560 | */ |
| 2561 | static |
| 2562 | void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags) |
| 2563 | { |
| 2564 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2565 | unsigned short cdb_len; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2566 | |
| 2567 | /* command descriptor fields */ |
| 2568 | ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD( |
| 2569 | UPIU_TRANSACTION_COMMAND, upiu_flags, |
| 2570 | lrbp->lun, lrbp->task_tag); |
| 2571 | ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD( |
| 2572 | UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0); |
| 2573 | |
| 2574 | /* Total EHS length and Data segment length will be zero */ |
| 2575 | ucd_req_ptr->header.dword_2 = 0; |
| 2576 | |
| 2577 | ucd_req_ptr->sc.exp_data_transfer_len = |
| 2578 | cpu_to_be32(lrbp->cmd->sdb.length); |
| 2579 | |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2580 | 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] | 2581 | memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2582 | if (cdb_len < MAX_CDB_SIZE) |
| 2583 | memset(ucd_req_ptr->sc.cdb + cdb_len, 0, |
| 2584 | (MAX_CDB_SIZE - cdb_len)); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2585 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2586 | } |
| 2587 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2588 | /** |
| 2589 | * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc, |
| 2590 | * for query requsts |
| 2591 | * @hba: UFS hba |
| 2592 | * @lrbp: local reference block pointer |
| 2593 | * @upiu_flags: flags |
| 2594 | */ |
| 2595 | static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba, |
| 2596 | struct ufshcd_lrb *lrbp, u32 upiu_flags) |
| 2597 | { |
| 2598 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
| 2599 | struct ufs_query *query = &hba->dev_cmd.query; |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 2600 | u16 len = be16_to_cpu(query->request.upiu_req.length); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2601 | u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE; |
| 2602 | |
| 2603 | /* Query request header */ |
| 2604 | ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD( |
| 2605 | UPIU_TRANSACTION_QUERY_REQ, upiu_flags, |
| 2606 | lrbp->lun, lrbp->task_tag); |
| 2607 | ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD( |
| 2608 | 0, query->request.query_func, 0, 0); |
| 2609 | |
Zang Leigang | 6861285 | 2016-08-25 17:39:19 +0800 | [diff] [blame] | 2610 | /* Data segment length only need for WRITE_DESC */ |
| 2611 | if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) |
| 2612 | ucd_req_ptr->header.dword_2 = |
| 2613 | UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len); |
| 2614 | else |
| 2615 | ucd_req_ptr->header.dword_2 = 0; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2616 | |
| 2617 | /* Copy the Query Request buffer as is */ |
| 2618 | memcpy(&ucd_req_ptr->qr, &query->request.upiu_req, |
| 2619 | QUERY_OSF_SIZE); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2620 | |
| 2621 | /* Copy the Descriptor */ |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2622 | if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) |
| 2623 | memcpy(descp, query->descriptor, len); |
| 2624 | |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 2625 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2626 | } |
| 2627 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2628 | static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp) |
| 2629 | { |
| 2630 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
| 2631 | |
| 2632 | memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req)); |
| 2633 | |
| 2634 | /* command descriptor fields */ |
| 2635 | ucd_req_ptr->header.dword_0 = |
| 2636 | UPIU_HEADER_DWORD( |
| 2637 | UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag); |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 2638 | /* clear rest of the fields of basic header */ |
| 2639 | ucd_req_ptr->header.dword_1 = 0; |
| 2640 | ucd_req_ptr->header.dword_2 = 0; |
| 2641 | |
| 2642 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2643 | } |
| 2644 | |
| 2645 | /** |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2646 | * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU) |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2647 | * @hba - per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2648 | * @lrb - pointer to local reference block |
| 2649 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2650 | 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] | 2651 | { |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2652 | u32 upiu_flags; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2653 | int ret = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2654 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2655 | switch (lrbp->command_type) { |
| 2656 | case UTP_CMD_TYPE_SCSI: |
| 2657 | if (likely(lrbp->cmd)) { |
| 2658 | ret = ufshcd_prepare_req_desc_hdr(hba, lrbp, |
| 2659 | &upiu_flags, lrbp->cmd->sc_data_direction); |
| 2660 | ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags); |
| 2661 | } else { |
| 2662 | ret = -EINVAL; |
| 2663 | } |
| 2664 | break; |
| 2665 | case UTP_CMD_TYPE_DEV_MANAGE: |
| 2666 | ret = ufshcd_prepare_req_desc_hdr(hba, lrbp, &upiu_flags, |
| 2667 | DMA_NONE); |
| 2668 | if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY) |
| 2669 | ufshcd_prepare_utp_query_req_upiu( |
| 2670 | hba, lrbp, upiu_flags); |
| 2671 | else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP) |
| 2672 | ufshcd_prepare_utp_nop_upiu(lrbp); |
| 2673 | else |
| 2674 | ret = -EINVAL; |
| 2675 | break; |
| 2676 | case UTP_CMD_TYPE_UFS: |
| 2677 | /* For UFS native command implementation */ |
| 2678 | ret = -ENOTSUPP; |
| 2679 | dev_err(hba->dev, "%s: UFS native command are not supported\n", |
| 2680 | __func__); |
| 2681 | break; |
| 2682 | default: |
| 2683 | ret = -ENOTSUPP; |
| 2684 | dev_err(hba->dev, "%s: unknown command type: 0x%x\n", |
| 2685 | __func__, lrbp->command_type); |
| 2686 | break; |
| 2687 | } /* end of switch */ |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2688 | |
| 2689 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2690 | } |
| 2691 | |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 2692 | /* |
| 2693 | * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN |
| 2694 | * @scsi_lun: scsi LUN id |
| 2695 | * |
| 2696 | * Returns UPIU LUN id |
| 2697 | */ |
| 2698 | static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun) |
| 2699 | { |
| 2700 | if (scsi_is_wlun(scsi_lun)) |
| 2701 | return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID) |
| 2702 | | UFS_UPIU_WLUN_ID; |
| 2703 | else |
| 2704 | return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID; |
| 2705 | } |
| 2706 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2707 | /** |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 2708 | * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID |
| 2709 | * @scsi_lun: UPIU W-LUN id |
| 2710 | * |
| 2711 | * Returns SCSI W-LUN id |
| 2712 | */ |
| 2713 | static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id) |
| 2714 | { |
| 2715 | return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE; |
| 2716 | } |
| 2717 | |
| 2718 | /** |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 2719 | * ufshcd_get_write_lock - synchronize between shutdown, scaling & |
| 2720 | * arrival of requests |
| 2721 | * @hba: ufs host |
| 2722 | * |
| 2723 | * Lock is predominantly held by shutdown context thus, ensuring |
| 2724 | * that no requests from any other context may sneak through. |
| 2725 | */ |
| 2726 | static inline void ufshcd_get_write_lock(struct ufs_hba *hba) |
| 2727 | { |
| 2728 | down_write(&hba->lock); |
| 2729 | } |
| 2730 | |
| 2731 | /** |
| 2732 | * ufshcd_get_read_lock - synchronize between shutdown, scaling & |
| 2733 | * arrival of requests |
| 2734 | * @hba: ufs host |
| 2735 | * |
| 2736 | * Returns 1 if acquired, < 0 on contention |
| 2737 | * |
| 2738 | * After shutdown's initiated, allow requests only directed to the |
| 2739 | * well known device lun. The sync between scaling & issue is maintained |
| 2740 | * as is and this restructuring syncs shutdown with these too. |
| 2741 | */ |
| 2742 | static int ufshcd_get_read_lock(struct ufs_hba *hba, u64 lun) |
| 2743 | { |
| 2744 | int err = 0; |
| 2745 | |
| 2746 | err = down_read_trylock(&hba->lock); |
| 2747 | if (err > 0) |
| 2748 | goto out; |
| 2749 | /* let requests for well known device lun to go through */ |
| 2750 | if (ufshcd_scsi_to_upiu_lun(lun) == UFS_UPIU_UFS_DEVICE_WLUN) |
| 2751 | return 0; |
| 2752 | else if (!ufshcd_is_shutdown_ongoing(hba)) |
| 2753 | return -EAGAIN; |
| 2754 | else |
| 2755 | return -EPERM; |
| 2756 | |
| 2757 | out: |
| 2758 | return err; |
| 2759 | } |
| 2760 | |
| 2761 | /** |
| 2762 | * ufshcd_put_read_lock - synchronize between shutdown, scaling & |
| 2763 | * arrival of requests |
| 2764 | * @hba: ufs host |
| 2765 | * |
| 2766 | * Returns none |
| 2767 | */ |
| 2768 | static inline void ufshcd_put_read_lock(struct ufs_hba *hba) |
| 2769 | { |
| 2770 | up_read(&hba->lock); |
| 2771 | } |
| 2772 | |
| 2773 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2774 | * ufshcd_queuecommand - main entry point for SCSI requests |
| 2775 | * @cmd: command from SCSI Midlayer |
| 2776 | * @done: call back function |
| 2777 | * |
| 2778 | * Returns 0 for success, non-zero in case of failure |
| 2779 | */ |
| 2780 | static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) |
| 2781 | { |
| 2782 | struct ufshcd_lrb *lrbp; |
| 2783 | struct ufs_hba *hba; |
| 2784 | unsigned long flags; |
| 2785 | int tag; |
| 2786 | int err = 0; |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 2787 | bool has_read_lock = false; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2788 | |
| 2789 | hba = shost_priv(host); |
| 2790 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 2791 | if (!cmd || !cmd->request || !hba) |
| 2792 | return -EINVAL; |
| 2793 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2794 | tag = cmd->request->tag; |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 2795 | if (!ufshcd_valid_tag(hba, tag)) { |
| 2796 | dev_err(hba->dev, |
| 2797 | "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p", |
| 2798 | __func__, tag, cmd, cmd->request); |
| 2799 | BUG(); |
| 2800 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2801 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 2802 | err = ufshcd_get_read_lock(hba, cmd->device->lun); |
| 2803 | if (unlikely(err < 0)) { |
| 2804 | if (err == -EPERM) { |
| 2805 | set_host_byte(cmd, DID_ERROR); |
| 2806 | cmd->scsi_done(cmd); |
| 2807 | return 0; |
| 2808 | } |
| 2809 | if (err == -EAGAIN) |
| 2810 | return SCSI_MLQUEUE_HOST_BUSY; |
| 2811 | } else if (err == 1) { |
| 2812 | has_read_lock = true; |
| 2813 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2814 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2815 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 2816 | |
| 2817 | /* if error handling is in progress, return host busy */ |
| 2818 | if (ufshcd_eh_in_progress(hba)) { |
| 2819 | err = SCSI_MLQUEUE_HOST_BUSY; |
| 2820 | goto out_unlock; |
| 2821 | } |
| 2822 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2823 | switch (hba->ufshcd_state) { |
| 2824 | case UFSHCD_STATE_OPERATIONAL: |
| 2825 | break; |
| 2826 | case UFSHCD_STATE_RESET: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2827 | err = SCSI_MLQUEUE_HOST_BUSY; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2828 | goto out_unlock; |
| 2829 | case UFSHCD_STATE_ERROR: |
| 2830 | set_host_byte(cmd, DID_ERROR); |
| 2831 | cmd->scsi_done(cmd); |
| 2832 | goto out_unlock; |
| 2833 | default: |
| 2834 | dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n", |
| 2835 | __func__, hba->ufshcd_state); |
| 2836 | set_host_byte(cmd, DID_BAD_TARGET); |
| 2837 | cmd->scsi_done(cmd); |
| 2838 | goto out_unlock; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2839 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2840 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2841 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2842 | hba->req_abort_count = 0; |
| 2843 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2844 | /* acquire the tag to make sure device cmds don't use it */ |
| 2845 | if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) { |
| 2846 | /* |
| 2847 | * Dev manage command in progress, requeue the command. |
| 2848 | * Requeuing the command helps in cases where the request *may* |
| 2849 | * find different tag instead of waiting for dev manage command |
| 2850 | * completion. |
| 2851 | */ |
| 2852 | err = SCSI_MLQUEUE_HOST_BUSY; |
| 2853 | goto out; |
| 2854 | } |
| 2855 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2856 | err = ufshcd_hold(hba, true); |
| 2857 | if (err) { |
| 2858 | err = SCSI_MLQUEUE_HOST_BUSY; |
| 2859 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 2860 | goto out; |
| 2861 | } |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 2862 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2863 | if (ufshcd_is_clkgating_allowed(hba)) |
| 2864 | WARN_ON(hba->clk_gating.state != CLKS_ON); |
| 2865 | |
| 2866 | err = ufshcd_hibern8_hold(hba, true); |
| 2867 | if (err) { |
| 2868 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 2869 | err = SCSI_MLQUEUE_HOST_BUSY; |
| 2870 | ufshcd_release(hba, true); |
| 2871 | goto out; |
| 2872 | } |
| 2873 | if (ufshcd_is_hibern8_on_idle_allowed(hba)) |
| 2874 | WARN_ON(hba->hibern8_on_idle.state != HIBERN8_EXITED); |
| 2875 | |
| 2876 | /* Vote PM QoS for the request */ |
| 2877 | ufshcd_vops_pm_qos_req_start(hba, cmd->request); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2878 | |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 2879 | /* IO svc time latency histogram */ |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 2880 | if (hba->latency_hist_enabled && |
| 2881 | (cmd->request->cmd_type == REQ_TYPE_FS)) { |
| 2882 | cmd->request->lat_hist_io_start = ktime_get(); |
| 2883 | cmd->request->lat_hist_enabled = 1; |
| 2884 | } else { |
| 2885 | cmd->request->lat_hist_enabled = 0; |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 2886 | } |
| 2887 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2888 | WARN_ON(hba->clk_gating.state != CLKS_ON); |
| 2889 | |
| 2890 | lrbp = &hba->lrb[tag]; |
| 2891 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2892 | WARN_ON(lrbp->cmd); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2893 | lrbp->cmd = cmd; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2894 | lrbp->sense_bufflen = UFSHCD_REQ_SENSE_SIZE; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2895 | lrbp->sense_buffer = cmd->sense_buffer; |
| 2896 | lrbp->task_tag = tag; |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 2897 | lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun); |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 2898 | lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2899 | lrbp->command_type = UTP_CMD_TYPE_SCSI; |
| 2900 | lrbp->req_abort_skip = false; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2901 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2902 | /* form UPIU before issuing the command */ |
| 2903 | err = ufshcd_compose_upiu(hba, lrbp); |
| 2904 | if (err) { |
| 2905 | if (err != -EAGAIN) |
| 2906 | dev_err(hba->dev, |
| 2907 | "%s: failed to compose upiu %d\n", |
| 2908 | __func__, err); |
Stephen Boyd | 9bc70c3 | 2017-03-01 16:58:38 -0800 | [diff] [blame] | 2909 | lrbp->cmd = NULL; |
| 2910 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 2911 | ufshcd_release_all(hba); |
| 2912 | ufshcd_vops_pm_qos_req_end(hba, cmd->request, true); |
| 2913 | goto out; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2914 | } |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2915 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2916 | err = ufshcd_map_sg(lrbp); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2917 | if (err) { |
| 2918 | lrbp->cmd = NULL; |
| 2919 | clear_bit_unlock(tag, &hba->lrb_in_use); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2920 | ufshcd_release_all(hba); |
| 2921 | ufshcd_vops_pm_qos_req_end(hba, cmd->request, true); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2922 | goto out; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2923 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2924 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2925 | err = ufshcd_vops_crypto_engine_cfg_start(hba, tag); |
| 2926 | if (err) { |
| 2927 | if (err != -EAGAIN) |
| 2928 | dev_err(hba->dev, |
| 2929 | "%s: failed to configure crypto engine %d\n", |
| 2930 | __func__, err); |
| 2931 | |
| 2932 | scsi_dma_unmap(lrbp->cmd); |
| 2933 | lrbp->cmd = NULL; |
| 2934 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 2935 | ufshcd_release_all(hba); |
| 2936 | ufshcd_vops_pm_qos_req_end(hba, cmd->request, true); |
| 2937 | |
| 2938 | goto out; |
| 2939 | } |
| 2940 | |
| 2941 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 2942 | wmb(); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2943 | /* issue command to the controller */ |
| 2944 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2945 | |
| 2946 | err = ufshcd_send_command(hba, tag); |
| 2947 | if (err) { |
| 2948 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2949 | scsi_dma_unmap(lrbp->cmd); |
| 2950 | lrbp->cmd = NULL; |
| 2951 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 2952 | ufshcd_release_all(hba); |
| 2953 | ufshcd_vops_pm_qos_req_end(hba, cmd->request, true); |
| 2954 | ufshcd_vops_crypto_engine_cfg_end(hba, lrbp, cmd->request); |
| 2955 | dev_err(hba->dev, "%s: failed sending command, %d\n", |
| 2956 | __func__, err); |
| 2957 | err = DID_ERROR; |
| 2958 | goto out; |
| 2959 | } |
| 2960 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2961 | out_unlock: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2962 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2963 | out: |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 2964 | if (has_read_lock) |
| 2965 | ufshcd_put_read_lock(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2966 | return err; |
| 2967 | } |
| 2968 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2969 | static int ufshcd_compose_dev_cmd(struct ufs_hba *hba, |
| 2970 | struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag) |
| 2971 | { |
| 2972 | lrbp->cmd = NULL; |
| 2973 | lrbp->sense_bufflen = 0; |
| 2974 | lrbp->sense_buffer = NULL; |
| 2975 | lrbp->task_tag = tag; |
| 2976 | lrbp->lun = 0; /* device management cmd is not specific to any LUN */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2977 | lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2978 | lrbp->intr_cmd = true; /* No interrupt aggregation */ |
| 2979 | hba->dev_cmd.type = cmd_type; |
| 2980 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2981 | return ufshcd_compose_upiu(hba, lrbp); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2982 | } |
| 2983 | |
| 2984 | static int |
| 2985 | ufshcd_clear_cmd(struct ufs_hba *hba, int tag) |
| 2986 | { |
| 2987 | int err = 0; |
| 2988 | unsigned long flags; |
| 2989 | u32 mask = 1 << tag; |
| 2990 | |
| 2991 | /* clear outstanding transaction before retry */ |
| 2992 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2993 | ufshcd_utrl_clear(hba, tag); |
| 2994 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2995 | |
| 2996 | /* |
| 2997 | * wait for for h/w to clear corresponding bit in door-bell. |
| 2998 | * max. wait is 1 sec. |
| 2999 | */ |
| 3000 | err = ufshcd_wait_for_register(hba, |
| 3001 | REG_UTP_TRANSFER_REQ_DOOR_BELL, |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 3002 | mask, ~mask, 1000, 1000, true); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3003 | |
| 3004 | return err; |
| 3005 | } |
| 3006 | |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 3007 | static int |
| 3008 | ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 3009 | { |
| 3010 | struct ufs_query_res *query_res = &hba->dev_cmd.query.response; |
| 3011 | |
| 3012 | /* Get the UPIU response */ |
| 3013 | query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >> |
| 3014 | UPIU_RSP_CODE_OFFSET; |
| 3015 | return query_res->response; |
| 3016 | } |
| 3017 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3018 | /** |
| 3019 | * ufshcd_dev_cmd_completion() - handles device management command responses |
| 3020 | * @hba: per adapter instance |
| 3021 | * @lrbp: pointer to local reference block |
| 3022 | */ |
| 3023 | static int |
| 3024 | ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 3025 | { |
| 3026 | int resp; |
| 3027 | int err = 0; |
| 3028 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3029 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3030 | resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr); |
| 3031 | |
| 3032 | switch (resp) { |
| 3033 | case UPIU_TRANSACTION_NOP_IN: |
| 3034 | if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) { |
| 3035 | err = -EINVAL; |
| 3036 | dev_err(hba->dev, "%s: unexpected response %x\n", |
| 3037 | __func__, resp); |
| 3038 | } |
| 3039 | break; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3040 | case UPIU_TRANSACTION_QUERY_RSP: |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 3041 | err = ufshcd_check_query_response(hba, lrbp); |
| 3042 | if (!err) |
| 3043 | err = ufshcd_copy_query_response(hba, lrbp); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3044 | break; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3045 | case UPIU_TRANSACTION_REJECT_UPIU: |
| 3046 | /* TODO: handle Reject UPIU Response */ |
| 3047 | err = -EPERM; |
| 3048 | dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n", |
| 3049 | __func__); |
| 3050 | break; |
| 3051 | default: |
| 3052 | err = -EINVAL; |
| 3053 | dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n", |
| 3054 | __func__, resp); |
| 3055 | break; |
| 3056 | } |
| 3057 | |
| 3058 | return err; |
| 3059 | } |
| 3060 | |
| 3061 | static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba, |
| 3062 | struct ufshcd_lrb *lrbp, int max_timeout) |
| 3063 | { |
| 3064 | int err = 0; |
| 3065 | unsigned long time_left; |
| 3066 | unsigned long flags; |
| 3067 | |
| 3068 | time_left = wait_for_completion_timeout(hba->dev_cmd.complete, |
| 3069 | msecs_to_jiffies(max_timeout)); |
| 3070 | |
| 3071 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 3072 | hba->dev_cmd.complete = NULL; |
| 3073 | if (likely(time_left)) { |
| 3074 | err = ufshcd_get_tr_ocs(lrbp); |
| 3075 | if (!err) |
| 3076 | err = ufshcd_dev_cmd_completion(hba, lrbp); |
| 3077 | } |
| 3078 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3079 | |
| 3080 | if (!time_left) { |
| 3081 | err = -ETIMEDOUT; |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 3082 | dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n", |
| 3083 | __func__, lrbp->task_tag); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3084 | if (!ufshcd_clear_cmd(hba, lrbp->task_tag)) |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 3085 | /* successfully cleared the command, retry if needed */ |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3086 | err = -EAGAIN; |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 3087 | /* |
| 3088 | * in case of an error, after clearing the doorbell, |
| 3089 | * we also need to clear the outstanding_request |
| 3090 | * field in hba |
| 3091 | */ |
| 3092 | ufshcd_outstanding_req_clear(hba, lrbp->task_tag); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3093 | } |
| 3094 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3095 | if (err) |
| 3096 | ufsdbg_set_err_state(hba); |
| 3097 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3098 | return err; |
| 3099 | } |
| 3100 | |
| 3101 | /** |
| 3102 | * ufshcd_get_dev_cmd_tag - Get device management command tag |
| 3103 | * @hba: per-adapter instance |
| 3104 | * @tag: pointer to variable with available slot value |
| 3105 | * |
| 3106 | * Get a free slot and lock it until device management command |
| 3107 | * completes. |
| 3108 | * |
| 3109 | * Returns false if free slot is unavailable for locking, else |
| 3110 | * return true with tag value in @tag. |
| 3111 | */ |
| 3112 | static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out) |
| 3113 | { |
| 3114 | int tag; |
| 3115 | bool ret = false; |
| 3116 | unsigned long tmp; |
| 3117 | |
| 3118 | if (!tag_out) |
| 3119 | goto out; |
| 3120 | |
| 3121 | do { |
| 3122 | tmp = ~hba->lrb_in_use; |
| 3123 | tag = find_last_bit(&tmp, hba->nutrs); |
| 3124 | if (tag >= hba->nutrs) |
| 3125 | goto out; |
| 3126 | } while (test_and_set_bit_lock(tag, &hba->lrb_in_use)); |
| 3127 | |
| 3128 | *tag_out = tag; |
| 3129 | ret = true; |
| 3130 | out: |
| 3131 | return ret; |
| 3132 | } |
| 3133 | |
| 3134 | static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag) |
| 3135 | { |
| 3136 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 3137 | } |
| 3138 | |
| 3139 | /** |
| 3140 | * ufshcd_exec_dev_cmd - API for sending device management requests |
| 3141 | * @hba - UFS hba |
| 3142 | * @cmd_type - specifies the type (NOP, Query...) |
| 3143 | * @timeout - time in seconds |
| 3144 | * |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3145 | * NOTE: Since there is only one available tag for device management commands, |
| 3146 | * it is expected you hold the hba->dev_cmd.lock mutex. |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3147 | */ |
| 3148 | static int ufshcd_exec_dev_cmd(struct ufs_hba *hba, |
| 3149 | enum dev_cmd_type cmd_type, int timeout) |
| 3150 | { |
| 3151 | struct ufshcd_lrb *lrbp; |
| 3152 | int err; |
| 3153 | int tag; |
| 3154 | struct completion wait; |
| 3155 | unsigned long flags; |
| 3156 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 3157 | /* |
| 3158 | * May get invoked from shutdown and IOCTL contexts. |
| 3159 | * In shutdown context, it comes in with lock acquired. |
| 3160 | */ |
| 3161 | if (!ufshcd_is_shutdown_ongoing(hba)) |
| 3162 | down_read(&hba->lock); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3163 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3164 | /* |
| 3165 | * Get free slot, sleep if slots are unavailable. |
| 3166 | * Even though we use wait_event() which sleeps indefinitely, |
| 3167 | * the maximum wait time is bounded by SCSI request timeout. |
| 3168 | */ |
| 3169 | wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag)); |
| 3170 | |
| 3171 | init_completion(&wait); |
| 3172 | lrbp = &hba->lrb[tag]; |
| 3173 | WARN_ON(lrbp->cmd); |
| 3174 | err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag); |
| 3175 | if (unlikely(err)) |
| 3176 | goto out_put_tag; |
| 3177 | |
| 3178 | hba->dev_cmd.complete = &wait; |
| 3179 | |
Yaniv Gardi | e3dfdc5 | 2016-02-01 15:02:49 +0200 | [diff] [blame] | 3180 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 3181 | wmb(); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3182 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3183 | err = ufshcd_send_command(hba, tag); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3184 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3185 | if (err) { |
| 3186 | dev_err(hba->dev, "%s: failed sending command, %d\n", |
| 3187 | __func__, err); |
| 3188 | goto out_put_tag; |
| 3189 | } |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3190 | err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout); |
| 3191 | |
| 3192 | out_put_tag: |
| 3193 | ufshcd_put_dev_cmd_tag(hba, tag); |
| 3194 | wake_up(&hba->dev_cmd.tag_wq); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 3195 | if (!ufshcd_is_shutdown_ongoing(hba)) |
| 3196 | up_read(&hba->lock); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3197 | return err; |
| 3198 | } |
| 3199 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3200 | /** |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3201 | * ufshcd_init_query() - init the query response and request parameters |
| 3202 | * @hba: per-adapter instance |
| 3203 | * @request: address of the request pointer to be initialized |
| 3204 | * @response: address of the response pointer to be initialized |
| 3205 | * @opcode: operation to perform |
| 3206 | * @idn: flag idn to access |
| 3207 | * @index: LU number to access |
| 3208 | * @selector: query/flag/descriptor further identification |
| 3209 | */ |
| 3210 | static inline void ufshcd_init_query(struct ufs_hba *hba, |
| 3211 | struct ufs_query_req **request, struct ufs_query_res **response, |
| 3212 | enum query_opcode opcode, u8 idn, u8 index, u8 selector) |
| 3213 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3214 | int idn_t = (int)idn; |
| 3215 | |
| 3216 | ufsdbg_error_inject_dispatcher(hba, |
| 3217 | ERR_INJECT_QUERY, idn_t, (int *)&idn_t); |
| 3218 | idn = idn_t; |
| 3219 | |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3220 | *request = &hba->dev_cmd.query.request; |
| 3221 | *response = &hba->dev_cmd.query.response; |
| 3222 | memset(*request, 0, sizeof(struct ufs_query_req)); |
| 3223 | memset(*response, 0, sizeof(struct ufs_query_res)); |
| 3224 | (*request)->upiu_req.opcode = opcode; |
| 3225 | (*request)->upiu_req.idn = idn; |
| 3226 | (*request)->upiu_req.index = index; |
| 3227 | (*request)->upiu_req.selector = selector; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3228 | |
| 3229 | ufshcd_update_query_stats(hba, opcode, idn); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3230 | } |
| 3231 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 3232 | static int ufshcd_query_flag_retry(struct ufs_hba *hba, |
| 3233 | enum query_opcode opcode, enum flag_idn idn, bool *flag_res) |
| 3234 | { |
| 3235 | int ret; |
| 3236 | int retries; |
| 3237 | |
| 3238 | for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) { |
| 3239 | ret = ufshcd_query_flag(hba, opcode, idn, flag_res); |
| 3240 | if (ret) |
| 3241 | dev_dbg(hba->dev, |
| 3242 | "%s: failed with error %d, retries %d\n", |
| 3243 | __func__, ret, retries); |
| 3244 | else |
| 3245 | break; |
| 3246 | } |
| 3247 | |
| 3248 | if (ret) |
| 3249 | dev_err(hba->dev, |
| 3250 | "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n", |
| 3251 | __func__, opcode, idn, ret, retries); |
| 3252 | return ret; |
| 3253 | } |
| 3254 | |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3255 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3256 | * ufshcd_query_flag() - API function for sending flag query requests |
| 3257 | * hba: per-adapter instance |
| 3258 | * query_opcode: flag query to perform |
| 3259 | * idn: flag idn to access |
| 3260 | * flag_res: the flag value after the query request completes |
| 3261 | * |
| 3262 | * Returns 0 for success, non-zero in case of failure |
| 3263 | */ |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 3264 | int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode, |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3265 | enum flag_idn idn, bool *flag_res) |
| 3266 | { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3267 | struct ufs_query_req *request = NULL; |
| 3268 | struct ufs_query_res *response = NULL; |
| 3269 | int err, index = 0, selector = 0; |
Yaniv Gardi | e5ad406 | 2016-02-01 15:02:41 +0200 | [diff] [blame] | 3270 | int timeout = QUERY_REQ_TIMEOUT; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3271 | |
| 3272 | BUG_ON(!hba); |
| 3273 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3274 | ufshcd_hold_all(hba); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3275 | mutex_lock(&hba->dev_cmd.lock); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3276 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 3277 | selector); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3278 | |
| 3279 | switch (opcode) { |
| 3280 | case UPIU_QUERY_OPCODE_SET_FLAG: |
| 3281 | case UPIU_QUERY_OPCODE_CLEAR_FLAG: |
| 3282 | case UPIU_QUERY_OPCODE_TOGGLE_FLAG: |
| 3283 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
| 3284 | break; |
| 3285 | case UPIU_QUERY_OPCODE_READ_FLAG: |
| 3286 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 3287 | if (!flag_res) { |
| 3288 | /* No dummy reads */ |
| 3289 | dev_err(hba->dev, "%s: Invalid argument for read request\n", |
| 3290 | __func__); |
| 3291 | err = -EINVAL; |
| 3292 | goto out_unlock; |
| 3293 | } |
| 3294 | break; |
| 3295 | default: |
| 3296 | dev_err(hba->dev, |
| 3297 | "%s: Expected query flag opcode but got = %d\n", |
| 3298 | __func__, opcode); |
| 3299 | err = -EINVAL; |
| 3300 | goto out_unlock; |
| 3301 | } |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3302 | |
Yaniv Gardi | e5ad406 | 2016-02-01 15:02:41 +0200 | [diff] [blame] | 3303 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3304 | |
| 3305 | if (err) { |
| 3306 | dev_err(hba->dev, |
| 3307 | "%s: Sending flag query for idn %d failed, err = %d\n", |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3308 | __func__, request->upiu_req.idn, err); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3309 | goto out_unlock; |
| 3310 | } |
| 3311 | |
| 3312 | if (flag_res) |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 3313 | *flag_res = (be32_to_cpu(response->upiu_res.value) & |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3314 | MASK_QUERY_UPIU_FLAG_LOC) & 0x1; |
| 3315 | |
| 3316 | out_unlock: |
| 3317 | mutex_unlock(&hba->dev_cmd.lock); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3318 | ufshcd_release_all(hba); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3319 | return err; |
| 3320 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3321 | EXPORT_SYMBOL(ufshcd_query_flag); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3322 | |
| 3323 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3324 | * ufshcd_query_attr - API function for sending attribute requests |
| 3325 | * hba: per-adapter instance |
| 3326 | * opcode: attribute opcode |
| 3327 | * idn: attribute idn to access |
| 3328 | * index: index field |
| 3329 | * selector: selector field |
| 3330 | * attr_val: the attribute value after the query request completes |
| 3331 | * |
| 3332 | * Returns 0 for success, non-zero in case of failure |
| 3333 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3334 | 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] | 3335 | enum attr_idn idn, u8 index, u8 selector, u32 *attr_val) |
| 3336 | { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3337 | struct ufs_query_req *request = NULL; |
| 3338 | struct ufs_query_res *response = NULL; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3339 | int err; |
| 3340 | |
| 3341 | BUG_ON(!hba); |
| 3342 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3343 | ufshcd_hold_all(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3344 | if (!attr_val) { |
| 3345 | dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n", |
| 3346 | __func__, opcode); |
| 3347 | err = -EINVAL; |
| 3348 | goto out; |
| 3349 | } |
| 3350 | |
| 3351 | mutex_lock(&hba->dev_cmd.lock); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3352 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 3353 | selector); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3354 | |
| 3355 | switch (opcode) { |
| 3356 | case UPIU_QUERY_OPCODE_WRITE_ATTR: |
| 3357 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 3358 | request->upiu_req.value = cpu_to_be32(*attr_val); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3359 | break; |
| 3360 | case UPIU_QUERY_OPCODE_READ_ATTR: |
| 3361 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 3362 | break; |
| 3363 | default: |
| 3364 | dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n", |
| 3365 | __func__, opcode); |
| 3366 | err = -EINVAL; |
| 3367 | goto out_unlock; |
| 3368 | } |
| 3369 | |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3370 | 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] | 3371 | |
| 3372 | if (err) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3373 | dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n", |
| 3374 | __func__, opcode, |
| 3375 | request->upiu_req.idn, index, err); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3376 | goto out_unlock; |
| 3377 | } |
| 3378 | |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 3379 | *attr_val = be32_to_cpu(response->upiu_res.value); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3380 | |
| 3381 | out_unlock: |
| 3382 | mutex_unlock(&hba->dev_cmd.lock); |
| 3383 | out: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3384 | ufshcd_release_all(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3385 | return err; |
| 3386 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3387 | EXPORT_SYMBOL(ufshcd_query_attr); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3388 | |
| 3389 | /** |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 3390 | * ufshcd_query_attr_retry() - API function for sending query |
| 3391 | * attribute with retries |
| 3392 | * @hba: per-adapter instance |
| 3393 | * @opcode: attribute opcode |
| 3394 | * @idn: attribute idn to access |
| 3395 | * @index: index field |
| 3396 | * @selector: selector field |
| 3397 | * @attr_val: the attribute value after the query request |
| 3398 | * completes |
| 3399 | * |
| 3400 | * Returns 0 for success, non-zero in case of failure |
| 3401 | */ |
| 3402 | static int ufshcd_query_attr_retry(struct ufs_hba *hba, |
| 3403 | enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector, |
| 3404 | u32 *attr_val) |
| 3405 | { |
| 3406 | int ret = 0; |
| 3407 | u32 retries; |
| 3408 | |
| 3409 | for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { |
| 3410 | ret = ufshcd_query_attr(hba, opcode, idn, index, |
| 3411 | selector, attr_val); |
| 3412 | if (ret) |
| 3413 | dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n", |
| 3414 | __func__, ret, retries); |
| 3415 | else |
| 3416 | break; |
| 3417 | } |
| 3418 | |
| 3419 | if (ret) |
| 3420 | dev_err(hba->dev, |
| 3421 | "%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] | 3422 | __func__, idn, ret, retries); |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 3423 | return ret; |
| 3424 | } |
| 3425 | |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3426 | static int __ufshcd_query_descriptor(struct ufs_hba *hba, |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3427 | enum query_opcode opcode, enum desc_idn idn, u8 index, |
| 3428 | u8 selector, u8 *desc_buf, int *buf_len) |
| 3429 | { |
| 3430 | struct ufs_query_req *request = NULL; |
| 3431 | struct ufs_query_res *response = NULL; |
| 3432 | int err; |
| 3433 | |
| 3434 | BUG_ON(!hba); |
| 3435 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3436 | ufshcd_hold_all(hba); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3437 | if (!desc_buf) { |
| 3438 | dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n", |
| 3439 | __func__, opcode); |
| 3440 | err = -EINVAL; |
| 3441 | goto out; |
| 3442 | } |
| 3443 | |
| 3444 | if (*buf_len <= QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) { |
| 3445 | dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n", |
| 3446 | __func__, *buf_len); |
| 3447 | err = -EINVAL; |
| 3448 | goto out; |
| 3449 | } |
| 3450 | |
| 3451 | mutex_lock(&hba->dev_cmd.lock); |
| 3452 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 3453 | selector); |
| 3454 | hba->dev_cmd.query.descriptor = desc_buf; |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 3455 | request->upiu_req.length = cpu_to_be16(*buf_len); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3456 | |
| 3457 | switch (opcode) { |
| 3458 | case UPIU_QUERY_OPCODE_WRITE_DESC: |
| 3459 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
| 3460 | break; |
| 3461 | case UPIU_QUERY_OPCODE_READ_DESC: |
| 3462 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 3463 | break; |
| 3464 | default: |
| 3465 | dev_err(hba->dev, |
| 3466 | "%s: Expected query descriptor opcode but got = 0x%.2x\n", |
| 3467 | __func__, opcode); |
| 3468 | err = -EINVAL; |
| 3469 | goto out_unlock; |
| 3470 | } |
| 3471 | |
| 3472 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT); |
| 3473 | |
| 3474 | if (err) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3475 | dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n", |
| 3476 | __func__, opcode, |
| 3477 | request->upiu_req.idn, index, err); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3478 | goto out_unlock; |
| 3479 | } |
| 3480 | |
| 3481 | hba->dev_cmd.query.descriptor = NULL; |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 3482 | *buf_len = be16_to_cpu(response->upiu_res.length); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3483 | |
| 3484 | out_unlock: |
| 3485 | mutex_unlock(&hba->dev_cmd.lock); |
| 3486 | out: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3487 | ufshcd_release_all(hba); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3488 | return err; |
| 3489 | } |
| 3490 | |
| 3491 | /** |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3492 | * ufshcd_query_descriptor - API function for sending descriptor requests |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3493 | * hba: per-adapter instance |
| 3494 | * opcode: attribute opcode |
| 3495 | * idn: attribute idn to access |
| 3496 | * index: index field |
| 3497 | * selector: selector field |
| 3498 | * desc_buf: the buffer that contains the descriptor |
| 3499 | * buf_len: length parameter passed to the device |
| 3500 | * |
| 3501 | * Returns 0 for success, non-zero in case of failure. |
| 3502 | * The buf_len parameter will contain, on return, the length parameter |
| 3503 | * received on the response. |
| 3504 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3505 | int ufshcd_query_descriptor(struct ufs_hba *hba, |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3506 | enum query_opcode opcode, enum desc_idn idn, u8 index, |
| 3507 | u8 selector, u8 *desc_buf, int *buf_len) |
| 3508 | { |
| 3509 | int err; |
| 3510 | int retries; |
| 3511 | |
| 3512 | for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { |
| 3513 | err = __ufshcd_query_descriptor(hba, opcode, idn, index, |
| 3514 | selector, desc_buf, buf_len); |
| 3515 | if (!err || err == -EINVAL) |
| 3516 | break; |
| 3517 | } |
| 3518 | |
| 3519 | return err; |
| 3520 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3521 | EXPORT_SYMBOL(ufshcd_query_descriptor); |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3522 | |
| 3523 | /** |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3524 | * ufshcd_read_desc_param - read the specified descriptor parameter |
| 3525 | * @hba: Pointer to adapter instance |
| 3526 | * @desc_id: descriptor idn value |
| 3527 | * @desc_index: descriptor index |
| 3528 | * @param_offset: offset of the parameter to read |
| 3529 | * @param_read_buf: pointer to buffer where parameter would be read |
| 3530 | * @param_size: sizeof(param_read_buf) |
| 3531 | * |
| 3532 | * Return 0 in case of success, non-zero otherwise |
| 3533 | */ |
| 3534 | static int ufshcd_read_desc_param(struct ufs_hba *hba, |
| 3535 | enum desc_idn desc_id, |
| 3536 | int desc_index, |
| 3537 | u32 param_offset, |
| 3538 | u8 *param_read_buf, |
| 3539 | u32 param_size) |
| 3540 | { |
| 3541 | int ret; |
| 3542 | u8 *desc_buf; |
| 3543 | u32 buff_len; |
| 3544 | bool is_kmalloc = true; |
| 3545 | |
| 3546 | /* safety checks */ |
| 3547 | if (desc_id >= QUERY_DESC_IDN_MAX) |
| 3548 | return -EINVAL; |
| 3549 | |
| 3550 | buff_len = ufs_query_desc_max_size[desc_id]; |
| 3551 | if ((param_offset + param_size) > buff_len) |
| 3552 | return -EINVAL; |
| 3553 | |
| 3554 | if (!param_offset && (param_size == buff_len)) { |
| 3555 | /* memory space already available to hold full descriptor */ |
| 3556 | desc_buf = param_read_buf; |
| 3557 | is_kmalloc = false; |
| 3558 | } else { |
| 3559 | /* allocate memory to hold full descriptor */ |
| 3560 | desc_buf = kmalloc(buff_len, GFP_KERNEL); |
| 3561 | if (!desc_buf) |
| 3562 | return -ENOMEM; |
| 3563 | } |
| 3564 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3565 | ret = ufshcd_query_descriptor(hba, UPIU_QUERY_OPCODE_READ_DESC, |
| 3566 | desc_id, desc_index, 0, desc_buf, |
| 3567 | &buff_len); |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3568 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3569 | if (ret) { |
| 3570 | dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d", |
| 3571 | __func__, desc_id, desc_index, param_offset, ret); |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3572 | |
| 3573 | goto out; |
| 3574 | } |
| 3575 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3576 | /* Sanity check */ |
| 3577 | if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) { |
| 3578 | dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header", |
| 3579 | __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]); |
| 3580 | ret = -EINVAL; |
| 3581 | goto out; |
| 3582 | } |
| 3583 | |
| 3584 | /* |
| 3585 | * While reading variable size descriptors (like string descriptor), |
| 3586 | * some UFS devices may report the "LENGTH" (field in "Transaction |
| 3587 | * Specific fields" of Query Response UPIU) same as what was requested |
| 3588 | * in Query Request UPIU instead of reporting the actual size of the |
| 3589 | * variable size descriptor. |
| 3590 | * Although it's safe to ignore the "LENGTH" field for variable size |
| 3591 | * descriptors as we can always derive the length of the descriptor from |
| 3592 | * the descriptor header fields. Hence this change impose the length |
| 3593 | * match check only for fixed size descriptors (for which we always |
| 3594 | * request the correct size as part of Query Request UPIU). |
| 3595 | */ |
| 3596 | if ((desc_id != QUERY_DESC_IDN_STRING) && |
| 3597 | (buff_len != desc_buf[QUERY_DESC_LENGTH_OFFSET])) { |
| 3598 | dev_err(hba->dev, "%s: desc_buf length mismatch: buff_len %d, buff_len(desc_header) %d", |
| 3599 | __func__, buff_len, desc_buf[QUERY_DESC_LENGTH_OFFSET]); |
| 3600 | ret = -EINVAL; |
| 3601 | goto out; |
| 3602 | } |
| 3603 | |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3604 | if (is_kmalloc) |
| 3605 | memcpy(param_read_buf, &desc_buf[param_offset], param_size); |
| 3606 | out: |
| 3607 | if (is_kmalloc) |
| 3608 | kfree(desc_buf); |
| 3609 | return ret; |
| 3610 | } |
| 3611 | |
| 3612 | static inline int ufshcd_read_desc(struct ufs_hba *hba, |
| 3613 | enum desc_idn desc_id, |
| 3614 | int desc_index, |
| 3615 | u8 *buf, |
| 3616 | u32 size) |
| 3617 | { |
| 3618 | return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size); |
| 3619 | } |
| 3620 | |
| 3621 | static inline int ufshcd_read_power_desc(struct ufs_hba *hba, |
| 3622 | u8 *buf, |
| 3623 | u32 size) |
| 3624 | { |
| 3625 | return ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size); |
| 3626 | } |
| 3627 | |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3628 | int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size) |
| 3629 | { |
| 3630 | return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size); |
| 3631 | } |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3632 | |
| 3633 | /** |
| 3634 | * ufshcd_read_string_desc - read string descriptor |
| 3635 | * @hba: pointer to adapter instance |
| 3636 | * @desc_index: descriptor index |
| 3637 | * @buf: pointer to buffer where descriptor would be read |
| 3638 | * @size: size of buf |
| 3639 | * @ascii: if true convert from unicode to ascii characters |
| 3640 | * |
| 3641 | * Return 0 in case of success, non-zero otherwise |
| 3642 | */ |
| 3643 | int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index, u8 *buf, |
| 3644 | u32 size, bool ascii) |
| 3645 | { |
| 3646 | int err = 0; |
| 3647 | |
| 3648 | err = ufshcd_read_desc(hba, |
| 3649 | QUERY_DESC_IDN_STRING, desc_index, buf, size); |
| 3650 | |
| 3651 | if (err) { |
| 3652 | dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n", |
| 3653 | __func__, QUERY_REQ_RETRIES, err); |
| 3654 | goto out; |
| 3655 | } |
| 3656 | |
| 3657 | if (ascii) { |
| 3658 | int desc_len; |
| 3659 | int ascii_len; |
| 3660 | int i; |
| 3661 | char *buff_ascii; |
| 3662 | |
| 3663 | desc_len = buf[0]; |
| 3664 | /* remove header and divide by 2 to move from UTF16 to UTF8 */ |
| 3665 | ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1; |
| 3666 | if (size < ascii_len + QUERY_DESC_HDR_SIZE) { |
| 3667 | dev_err(hba->dev, "%s: buffer allocated size is too small\n", |
| 3668 | __func__); |
| 3669 | err = -ENOMEM; |
| 3670 | goto out; |
| 3671 | } |
| 3672 | |
| 3673 | buff_ascii = kmalloc(ascii_len, GFP_KERNEL); |
| 3674 | if (!buff_ascii) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3675 | dev_err(hba->dev, "%s: Failed allocating %d bytes\n", |
| 3676 | __func__, ascii_len); |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3677 | err = -ENOMEM; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3678 | goto out_free_buff; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3679 | } |
| 3680 | |
| 3681 | /* |
| 3682 | * the descriptor contains string in UTF16 format |
| 3683 | * we need to convert to utf-8 so it can be displayed |
| 3684 | */ |
| 3685 | utf16s_to_utf8s((wchar_t *)&buf[QUERY_DESC_HDR_SIZE], |
| 3686 | desc_len - QUERY_DESC_HDR_SIZE, |
| 3687 | UTF16_BIG_ENDIAN, buff_ascii, ascii_len); |
| 3688 | |
| 3689 | /* replace non-printable or non-ASCII characters with spaces */ |
| 3690 | for (i = 0; i < ascii_len; i++) |
| 3691 | ufshcd_remove_non_printable(&buff_ascii[i]); |
| 3692 | |
| 3693 | memset(buf + QUERY_DESC_HDR_SIZE, 0, |
| 3694 | size - QUERY_DESC_HDR_SIZE); |
| 3695 | memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len); |
| 3696 | buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3697 | out_free_buff: |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3698 | kfree(buff_ascii); |
| 3699 | } |
| 3700 | out: |
| 3701 | return err; |
| 3702 | } |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3703 | |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3704 | /** |
| 3705 | * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter |
| 3706 | * @hba: Pointer to adapter instance |
| 3707 | * @lun: lun id |
| 3708 | * @param_offset: offset of the parameter to read |
| 3709 | * @param_read_buf: pointer to buffer where parameter would be read |
| 3710 | * @param_size: sizeof(param_read_buf) |
| 3711 | * |
| 3712 | * Return 0 in case of success, non-zero otherwise |
| 3713 | */ |
| 3714 | static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba, |
| 3715 | int lun, |
| 3716 | enum unit_desc_param param_offset, |
| 3717 | u8 *param_read_buf, |
| 3718 | u32 param_size) |
| 3719 | { |
| 3720 | /* |
| 3721 | * Unit descriptors are only available for general purpose LUs (LUN id |
| 3722 | * from 0 to 7) and RPMB Well known LU. |
| 3723 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3724 | if (!ufs_is_valid_unit_desc_lun(lun)) |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3725 | return -EOPNOTSUPP; |
| 3726 | |
| 3727 | return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun, |
| 3728 | param_offset, param_read_buf, param_size); |
| 3729 | } |
| 3730 | |
| 3731 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3732 | * ufshcd_memory_alloc - allocate memory for host memory space data structures |
| 3733 | * @hba: per adapter instance |
| 3734 | * |
| 3735 | * 1. Allocate DMA memory for Command Descriptor array |
| 3736 | * Each command descriptor consist of Command UPIU, Response UPIU and PRDT |
| 3737 | * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL). |
| 3738 | * 3. Allocate DMA memory for UTP Task Management Request Descriptor List |
| 3739 | * (UTMRDL) |
| 3740 | * 4. Allocate memory for local reference block(lrb). |
| 3741 | * |
| 3742 | * Returns 0 for success, non-zero in case of failure |
| 3743 | */ |
| 3744 | static int ufshcd_memory_alloc(struct ufs_hba *hba) |
| 3745 | { |
| 3746 | size_t utmrdl_size, utrdl_size, ucdl_size; |
| 3747 | |
| 3748 | /* Allocate memory for UTP command descriptors */ |
| 3749 | ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3750 | hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 3751 | ucdl_size, |
| 3752 | &hba->ucdl_dma_addr, |
| 3753 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3754 | |
| 3755 | /* |
| 3756 | * UFSHCI requires UTP command descriptor to be 128 byte aligned. |
| 3757 | * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE |
| 3758 | * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will |
| 3759 | * be aligned to 128 bytes as well |
| 3760 | */ |
| 3761 | if (!hba->ucdl_base_addr || |
| 3762 | WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3763 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3764 | "Command Descriptor Memory allocation failed\n"); |
| 3765 | goto out; |
| 3766 | } |
| 3767 | |
| 3768 | /* |
| 3769 | * Allocate memory for UTP Transfer descriptors |
| 3770 | * UFSHCI requires 1024 byte alignment of UTRD |
| 3771 | */ |
| 3772 | utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3773 | hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 3774 | utrdl_size, |
| 3775 | &hba->utrdl_dma_addr, |
| 3776 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3777 | if (!hba->utrdl_base_addr || |
| 3778 | WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3779 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3780 | "Transfer Descriptor Memory allocation failed\n"); |
| 3781 | goto out; |
| 3782 | } |
| 3783 | |
| 3784 | /* |
| 3785 | * Allocate memory for UTP Task Management descriptors |
| 3786 | * UFSHCI requires 1024 byte alignment of UTMRD |
| 3787 | */ |
| 3788 | utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs; |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3789 | hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 3790 | utmrdl_size, |
| 3791 | &hba->utmrdl_dma_addr, |
| 3792 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3793 | if (!hba->utmrdl_base_addr || |
| 3794 | WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3795 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3796 | "Task Management Descriptor Memory allocation failed\n"); |
| 3797 | goto out; |
| 3798 | } |
| 3799 | |
| 3800 | /* Allocate memory for local reference block */ |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3801 | hba->lrb = devm_kzalloc(hba->dev, |
| 3802 | hba->nutrs * sizeof(struct ufshcd_lrb), |
| 3803 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3804 | if (!hba->lrb) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3805 | dev_err(hba->dev, "LRB Memory allocation failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3806 | goto out; |
| 3807 | } |
| 3808 | return 0; |
| 3809 | out: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3810 | return -ENOMEM; |
| 3811 | } |
| 3812 | |
| 3813 | /** |
| 3814 | * ufshcd_host_memory_configure - configure local reference block with |
| 3815 | * memory offsets |
| 3816 | * @hba: per adapter instance |
| 3817 | * |
| 3818 | * Configure Host memory space |
| 3819 | * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA |
| 3820 | * address. |
| 3821 | * 2. Update each UTRD with Response UPIU offset, Response UPIU length |
| 3822 | * and PRDT offset. |
| 3823 | * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT |
| 3824 | * into local reference block. |
| 3825 | */ |
| 3826 | static void ufshcd_host_memory_configure(struct ufs_hba *hba) |
| 3827 | { |
| 3828 | struct utp_transfer_cmd_desc *cmd_descp; |
| 3829 | struct utp_transfer_req_desc *utrdlp; |
| 3830 | dma_addr_t cmd_desc_dma_addr; |
| 3831 | dma_addr_t cmd_desc_element_addr; |
| 3832 | u16 response_offset; |
| 3833 | u16 prdt_offset; |
| 3834 | int cmd_desc_size; |
| 3835 | int i; |
| 3836 | |
| 3837 | utrdlp = hba->utrdl_base_addr; |
| 3838 | cmd_descp = hba->ucdl_base_addr; |
| 3839 | |
| 3840 | response_offset = |
| 3841 | offsetof(struct utp_transfer_cmd_desc, response_upiu); |
| 3842 | prdt_offset = |
| 3843 | offsetof(struct utp_transfer_cmd_desc, prd_table); |
| 3844 | |
| 3845 | cmd_desc_size = sizeof(struct utp_transfer_cmd_desc); |
| 3846 | cmd_desc_dma_addr = hba->ucdl_dma_addr; |
| 3847 | |
| 3848 | for (i = 0; i < hba->nutrs; i++) { |
| 3849 | /* Configure UTRD with command descriptor base address */ |
| 3850 | cmd_desc_element_addr = |
| 3851 | (cmd_desc_dma_addr + (cmd_desc_size * i)); |
| 3852 | utrdlp[i].command_desc_base_addr_lo = |
| 3853 | cpu_to_le32(lower_32_bits(cmd_desc_element_addr)); |
| 3854 | utrdlp[i].command_desc_base_addr_hi = |
| 3855 | cpu_to_le32(upper_32_bits(cmd_desc_element_addr)); |
| 3856 | |
| 3857 | /* Response upiu and prdt offset should be in double words */ |
| 3858 | utrdlp[i].response_upiu_offset = |
| 3859 | cpu_to_le16((response_offset >> 2)); |
| 3860 | utrdlp[i].prd_table_offset = |
| 3861 | cpu_to_le16((prdt_offset >> 2)); |
| 3862 | utrdlp[i].response_upiu_length = |
Sujit Reddy Thumma | 3ca316c | 2013-06-26 22:39:30 +0530 | [diff] [blame] | 3863 | cpu_to_le16(ALIGNED_UPIU_SIZE >> 2); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3864 | |
| 3865 | hba->lrb[i].utr_descriptor_ptr = (utrdlp + i); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3866 | hba->lrb[i].utrd_dma_addr = hba->utrdl_dma_addr + |
| 3867 | (i * sizeof(struct utp_transfer_req_desc)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3868 | hba->lrb[i].ucd_req_ptr = |
| 3869 | (struct utp_upiu_req *)(cmd_descp + i); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3870 | hba->lrb[i].ucd_req_dma_addr = cmd_desc_element_addr; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3871 | hba->lrb[i].ucd_rsp_ptr = |
| 3872 | (struct utp_upiu_rsp *)cmd_descp[i].response_upiu; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3873 | hba->lrb[i].ucd_rsp_dma_addr = cmd_desc_element_addr + |
| 3874 | response_offset; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3875 | hba->lrb[i].ucd_prdt_ptr = |
| 3876 | (struct ufshcd_sg_entry *)cmd_descp[i].prd_table; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3877 | hba->lrb[i].ucd_prdt_dma_addr = cmd_desc_element_addr + |
| 3878 | prdt_offset; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3879 | } |
| 3880 | } |
| 3881 | |
| 3882 | /** |
| 3883 | * ufshcd_dme_link_startup - Notify Unipro to perform link startup |
| 3884 | * @hba: per adapter instance |
| 3885 | * |
| 3886 | * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer, |
| 3887 | * in order to initialize the Unipro link startup procedure. |
| 3888 | * Once the Unipro links are up, the device connected to the controller |
| 3889 | * is detected. |
| 3890 | * |
| 3891 | * Returns 0 on success, non-zero value on failure |
| 3892 | */ |
| 3893 | static int ufshcd_dme_link_startup(struct ufs_hba *hba) |
| 3894 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3895 | struct uic_command uic_cmd = {0}; |
| 3896 | int ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3897 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3898 | uic_cmd.command = UIC_CMD_DME_LINK_STARTUP; |
| 3899 | |
| 3900 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3901 | if (ret) |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3902 | dev_dbg(hba->dev, |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3903 | "dme-link-startup: error code %d\n", ret); |
| 3904 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3905 | } |
| 3906 | |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 3907 | static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba) |
| 3908 | { |
| 3909 | #define MIN_DELAY_BEFORE_DME_CMDS_US 1000 |
| 3910 | unsigned long min_sleep_time_us; |
| 3911 | |
| 3912 | if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS)) |
| 3913 | return; |
| 3914 | |
| 3915 | /* |
| 3916 | * last_dme_cmd_tstamp will be 0 only for 1st call to |
| 3917 | * this function |
| 3918 | */ |
| 3919 | if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) { |
| 3920 | min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US; |
| 3921 | } else { |
| 3922 | unsigned long delta = |
| 3923 | (unsigned long) ktime_to_us( |
| 3924 | ktime_sub(ktime_get(), |
| 3925 | hba->last_dme_cmd_tstamp)); |
| 3926 | |
| 3927 | if (delta < MIN_DELAY_BEFORE_DME_CMDS_US) |
| 3928 | min_sleep_time_us = |
| 3929 | MIN_DELAY_BEFORE_DME_CMDS_US - delta; |
| 3930 | else |
| 3931 | return; /* no more delay required */ |
| 3932 | } |
| 3933 | |
| 3934 | /* allow sleep for extra 50us if needed */ |
| 3935 | usleep_range(min_sleep_time_us, min_sleep_time_us + 50); |
| 3936 | } |
| 3937 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3938 | static inline void ufshcd_save_tstamp_of_last_dme_cmd( |
| 3939 | struct ufs_hba *hba) |
| 3940 | { |
| 3941 | if (hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS) |
| 3942 | hba->last_dme_cmd_tstamp = ktime_get(); |
| 3943 | } |
| 3944 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3945 | /** |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3946 | * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET |
| 3947 | * @hba: per adapter instance |
| 3948 | * @attr_sel: uic command argument1 |
| 3949 | * @attr_set: attribute set type as uic command argument2 |
| 3950 | * @mib_val: setting value as uic command argument3 |
| 3951 | * @peer: indicate whether peer or local |
| 3952 | * |
| 3953 | * Returns 0 on success, non-zero value on failure |
| 3954 | */ |
| 3955 | int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel, |
| 3956 | u8 attr_set, u32 mib_val, u8 peer) |
| 3957 | { |
| 3958 | struct uic_command uic_cmd = {0}; |
| 3959 | static const char *const action[] = { |
| 3960 | "dme-set", |
| 3961 | "dme-peer-set" |
| 3962 | }; |
| 3963 | const char *set = action[!!peer]; |
| 3964 | int ret; |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3965 | int retries = UFS_UIC_COMMAND_RETRIES; |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3966 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3967 | ufsdbg_error_inject_dispatcher(hba, |
| 3968 | ERR_INJECT_DME_ATTR, attr_sel, &attr_sel); |
| 3969 | |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3970 | uic_cmd.command = peer ? |
| 3971 | UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET; |
| 3972 | uic_cmd.argument1 = attr_sel; |
| 3973 | uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set); |
| 3974 | uic_cmd.argument3 = mib_val; |
| 3975 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3976 | do { |
| 3977 | /* for peer attributes we retry upon failure */ |
| 3978 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3979 | if (ret) |
| 3980 | dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n", |
| 3981 | set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret); |
| 3982 | } while (ret && peer && --retries); |
| 3983 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3984 | if (ret) |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3985 | 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] | 3986 | set, UIC_GET_ATTR_ID(attr_sel), mib_val, |
| 3987 | UFS_UIC_COMMAND_RETRIES - retries); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3988 | |
| 3989 | return ret; |
| 3990 | } |
| 3991 | EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr); |
| 3992 | |
| 3993 | /** |
| 3994 | * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET |
| 3995 | * @hba: per adapter instance |
| 3996 | * @attr_sel: uic command argument1 |
| 3997 | * @mib_val: the value of the attribute as returned by the UIC command |
| 3998 | * @peer: indicate whether peer or local |
| 3999 | * |
| 4000 | * Returns 0 on success, non-zero value on failure |
| 4001 | */ |
| 4002 | int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel, |
| 4003 | u32 *mib_val, u8 peer) |
| 4004 | { |
| 4005 | struct uic_command uic_cmd = {0}; |
| 4006 | static const char *const action[] = { |
| 4007 | "dme-get", |
| 4008 | "dme-peer-get" |
| 4009 | }; |
| 4010 | const char *get = action[!!peer]; |
| 4011 | int ret; |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 4012 | int retries = UFS_UIC_COMMAND_RETRIES; |
Yaniv Gardi | 874237f | 2015-05-17 18:55:03 +0300 | [diff] [blame] | 4013 | struct ufs_pa_layer_attr orig_pwr_info; |
| 4014 | struct ufs_pa_layer_attr temp_pwr_info; |
| 4015 | bool pwr_mode_change = false; |
| 4016 | |
| 4017 | if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) { |
| 4018 | orig_pwr_info = hba->pwr_info; |
| 4019 | temp_pwr_info = orig_pwr_info; |
| 4020 | |
| 4021 | if (orig_pwr_info.pwr_tx == FAST_MODE || |
| 4022 | orig_pwr_info.pwr_rx == FAST_MODE) { |
| 4023 | temp_pwr_info.pwr_tx = FASTAUTO_MODE; |
| 4024 | temp_pwr_info.pwr_rx = FASTAUTO_MODE; |
| 4025 | pwr_mode_change = true; |
| 4026 | } else if (orig_pwr_info.pwr_tx == SLOW_MODE || |
| 4027 | orig_pwr_info.pwr_rx == SLOW_MODE) { |
| 4028 | temp_pwr_info.pwr_tx = SLOWAUTO_MODE; |
| 4029 | temp_pwr_info.pwr_rx = SLOWAUTO_MODE; |
| 4030 | pwr_mode_change = true; |
| 4031 | } |
| 4032 | if (pwr_mode_change) { |
| 4033 | ret = ufshcd_change_power_mode(hba, &temp_pwr_info); |
| 4034 | if (ret) |
| 4035 | goto out; |
| 4036 | } |
| 4037 | } |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 4038 | |
| 4039 | uic_cmd.command = peer ? |
| 4040 | UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4041 | |
| 4042 | ufsdbg_error_inject_dispatcher(hba, |
| 4043 | ERR_INJECT_DME_ATTR, attr_sel, &attr_sel); |
| 4044 | |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 4045 | uic_cmd.argument1 = attr_sel; |
| 4046 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 4047 | do { |
| 4048 | /* for peer attributes we retry upon failure */ |
| 4049 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 4050 | if (ret) |
| 4051 | dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n", |
| 4052 | get, UIC_GET_ATTR_ID(attr_sel), ret); |
| 4053 | } while (ret && peer && --retries); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 4054 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4055 | if (ret) |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 4056 | 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] | 4057 | get, UIC_GET_ATTR_ID(attr_sel), |
| 4058 | UFS_UIC_COMMAND_RETRIES - retries); |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 4059 | |
| 4060 | if (mib_val && !ret) |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 4061 | *mib_val = uic_cmd.argument3; |
Yaniv Gardi | 874237f | 2015-05-17 18:55:03 +0300 | [diff] [blame] | 4062 | |
| 4063 | if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE) |
| 4064 | && pwr_mode_change) |
| 4065 | ufshcd_change_power_mode(hba, &orig_pwr_info); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 4066 | out: |
| 4067 | return ret; |
| 4068 | } |
| 4069 | EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr); |
| 4070 | |
| 4071 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4072 | * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power |
| 4073 | * state) and waits for it to take effect. |
| 4074 | * |
| 4075 | * @hba: per adapter instance |
| 4076 | * @cmd: UIC command to execute |
| 4077 | * |
| 4078 | * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER & |
| 4079 | * DME_HIBERNATE_EXIT commands take some time to take its effect on both host |
| 4080 | * and device UniPro link and hence it's final completion would be indicated by |
| 4081 | * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in |
| 4082 | * addition to normal UIC command completion Status (UCCS). This function only |
| 4083 | * returns after the relevant status bits indicate the completion. |
| 4084 | * |
| 4085 | * Returns 0 on success, non-zero value on failure |
| 4086 | */ |
| 4087 | static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd) |
| 4088 | { |
| 4089 | struct completion uic_async_done; |
| 4090 | unsigned long flags; |
| 4091 | u8 status; |
| 4092 | int ret; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 4093 | bool reenable_intr = false; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4094 | |
| 4095 | mutex_lock(&hba->uic_cmd_mutex); |
| 4096 | init_completion(&uic_async_done); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 4097 | ufshcd_add_delay_before_dme_cmd(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4098 | |
| 4099 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4100 | hba->uic_async_done = &uic_async_done; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 4101 | if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) { |
| 4102 | ufshcd_disable_intr(hba, UIC_COMMAND_COMPL); |
| 4103 | /* |
| 4104 | * Make sure UIC command completion interrupt is disabled before |
| 4105 | * issuing UIC command. |
| 4106 | */ |
| 4107 | wmb(); |
| 4108 | reenable_intr = true; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4109 | } |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 4110 | ret = __ufshcd_send_uic_cmd(hba, cmd, false); |
| 4111 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4112 | if (ret) { |
| 4113 | dev_err(hba->dev, |
| 4114 | "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n", |
| 4115 | cmd->command, cmd->argument3, ret); |
| 4116 | goto out; |
| 4117 | } |
| 4118 | |
| 4119 | if (!wait_for_completion_timeout(hba->uic_async_done, |
| 4120 | msecs_to_jiffies(UIC_CMD_TIMEOUT))) { |
| 4121 | dev_err(hba->dev, |
| 4122 | "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n", |
| 4123 | cmd->command, cmd->argument3); |
| 4124 | ret = -ETIMEDOUT; |
| 4125 | goto out; |
| 4126 | } |
| 4127 | |
| 4128 | status = ufshcd_get_upmcrs(hba); |
| 4129 | if (status != PWR_LOCAL) { |
| 4130 | dev_err(hba->dev, |
Kiwoong Kim | 7361542 | 2016-09-08 16:50:02 +0900 | [diff] [blame] | 4131 | "pwr ctrl cmd 0x%0x failed, host upmcrs:0x%x\n", |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4132 | cmd->command, status); |
| 4133 | ret = (status != PWR_OK) ? status : -1; |
| 4134 | } |
| 4135 | out: |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 4136 | if (ret) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4137 | ufsdbg_set_err_state(hba); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 4138 | ufshcd_print_host_state(hba); |
| 4139 | ufshcd_print_pwr_info(hba); |
| 4140 | ufshcd_print_host_regs(hba); |
| 4141 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4142 | |
| 4143 | ufshcd_save_tstamp_of_last_dme_cmd(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4144 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 4145 | hba->active_uic_cmd = NULL; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4146 | hba->uic_async_done = NULL; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 4147 | if (reenable_intr) |
| 4148 | ufshcd_enable_intr(hba, UIC_COMMAND_COMPL); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4149 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4150 | mutex_unlock(&hba->uic_cmd_mutex); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4151 | return ret; |
| 4152 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4153 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4154 | int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba, u64 wait_timeout_us) |
| 4155 | { |
| 4156 | unsigned long flags; |
| 4157 | int ret = 0; |
| 4158 | u32 tm_doorbell; |
| 4159 | u32 tr_doorbell; |
| 4160 | bool timeout = false, do_last_check = false; |
| 4161 | ktime_t start; |
| 4162 | |
| 4163 | ufshcd_hold_all(hba); |
| 4164 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4165 | /* |
| 4166 | * Wait for all the outstanding tasks/transfer requests. |
| 4167 | * Verify by checking the doorbell registers are clear. |
| 4168 | */ |
| 4169 | start = ktime_get(); |
| 4170 | do { |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 4171 | if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) { |
| 4172 | ret = -EBUSY; |
| 4173 | goto out; |
| 4174 | } |
| 4175 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4176 | tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL); |
| 4177 | tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 4178 | if (!tm_doorbell && !tr_doorbell) { |
| 4179 | timeout = false; |
| 4180 | break; |
| 4181 | } else if (do_last_check) { |
| 4182 | break; |
| 4183 | } |
| 4184 | |
| 4185 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4186 | schedule(); |
| 4187 | if (ktime_to_us(ktime_sub(ktime_get(), start)) > |
| 4188 | wait_timeout_us) { |
| 4189 | timeout = true; |
| 4190 | /* |
| 4191 | * We might have scheduled out for long time so make |
| 4192 | * sure to check if doorbells are cleared by this time |
| 4193 | * or not. |
| 4194 | */ |
| 4195 | do_last_check = true; |
| 4196 | } |
| 4197 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4198 | } while (tm_doorbell || tr_doorbell); |
| 4199 | |
| 4200 | if (timeout) { |
| 4201 | dev_err(hba->dev, |
| 4202 | "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n", |
| 4203 | __func__, tm_doorbell, tr_doorbell); |
| 4204 | ret = -EBUSY; |
| 4205 | } |
| 4206 | out: |
| 4207 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4208 | ufshcd_release_all(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4209 | return ret; |
| 4210 | } |
| 4211 | |
| 4212 | /** |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4213 | * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage |
| 4214 | * using DME_SET primitives. |
| 4215 | * @hba: per adapter instance |
| 4216 | * @mode: powr mode value |
| 4217 | * |
| 4218 | * Returns 0 on success, non-zero value on failure |
| 4219 | */ |
Sujit Reddy Thumma | bdbe5d2 | 2014-05-26 10:59:11 +0530 | [diff] [blame] | 4220 | 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] | 4221 | { |
| 4222 | struct uic_command uic_cmd = {0}; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4223 | int ret; |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4224 | |
Yaniv Gardi | c3a2f9e | 2015-05-17 18:55:01 +0300 | [diff] [blame] | 4225 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) { |
| 4226 | ret = ufshcd_dme_set(hba, |
| 4227 | UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1); |
| 4228 | if (ret) { |
| 4229 | dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n", |
| 4230 | __func__, ret); |
| 4231 | goto out; |
| 4232 | } |
| 4233 | } |
| 4234 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4235 | uic_cmd.command = UIC_CMD_DME_SET; |
| 4236 | uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE); |
| 4237 | uic_cmd.argument3 = mode; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4238 | ufshcd_hold_all(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4239 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4240 | ufshcd_release_all(hba); |
Yaniv Gardi | c3a2f9e | 2015-05-17 18:55:01 +0300 | [diff] [blame] | 4241 | out: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4242 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4243 | } |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4244 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 4245 | static int ufshcd_link_recovery(struct ufs_hba *hba) |
| 4246 | { |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 4247 | int ret = 0; |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 4248 | unsigned long flags; |
| 4249 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 4250 | /* |
| 4251 | * Check if there is any race with fatal error handling. |
| 4252 | * If so, wait for it to complete. Even though fatal error |
| 4253 | * handling does reset and restore in some cases, don't assume |
| 4254 | * anything out of it. We are just avoiding race here. |
| 4255 | */ |
| 4256 | do { |
| 4257 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4258 | if (!(work_pending(&hba->eh_work) || |
| 4259 | hba->ufshcd_state == UFSHCD_STATE_RESET)) |
| 4260 | break; |
| 4261 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4262 | dev_dbg(hba->dev, "%s: reset in progress\n", __func__); |
| 4263 | flush_work(&hba->eh_work); |
| 4264 | } while (1); |
| 4265 | |
| 4266 | |
| 4267 | /* |
| 4268 | * we don't know if previous reset had really reset the host controller |
| 4269 | * or not. So let's force reset here to be sure. |
| 4270 | */ |
| 4271 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 4272 | hba->force_host_reset = true; |
| 4273 | schedule_work(&hba->eh_work); |
| 4274 | |
| 4275 | /* wait for the reset work to finish */ |
| 4276 | do { |
| 4277 | if (!(work_pending(&hba->eh_work) || |
| 4278 | hba->ufshcd_state == UFSHCD_STATE_RESET)) |
| 4279 | break; |
| 4280 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4281 | dev_dbg(hba->dev, "%s: reset in progress\n", __func__); |
| 4282 | flush_work(&hba->eh_work); |
| 4283 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4284 | } while (1); |
| 4285 | |
| 4286 | if (!((hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) && |
| 4287 | ufshcd_is_link_active(hba))) |
| 4288 | ret = -ENOLINK; |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 4289 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4290 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 4291 | return ret; |
| 4292 | } |
| 4293 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4294 | static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4295 | { |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4296 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4297 | struct uic_command uic_cmd = {0}; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4298 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4299 | |
| 4300 | uic_cmd.command = UIC_CMD_DME_HIBER_ENTER; |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4301 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4302 | trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter", |
| 4303 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4304 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 4305 | /* |
| 4306 | * Do full reinit if enter failed or if LINERESET was detected during |
| 4307 | * Hibern8 operation. After LINERESET, link moves to default PWM-G1 |
| 4308 | * mode hence full reinit is required to move link to HS speeds. |
| 4309 | */ |
| 4310 | if (ret || hba->full_init_linereset) { |
| 4311 | hba->full_init_linereset = false; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4312 | ufshcd_update_error_stats(hba, UFS_ERR_HIBERN8_ENTER); |
| 4313 | dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d", |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4314 | __func__, ret); |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 4315 | /* |
| 4316 | * If link recovery fails then return error so that caller |
| 4317 | * don't retry the hibern8 enter again. |
| 4318 | */ |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 4319 | ret = ufshcd_link_recovery(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4320 | } else { |
| 4321 | dev_dbg(hba->dev, "%s: Hibern8 Enter at %lld us", __func__, |
| 4322 | ktime_to_us(ktime_get())); |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 4323 | } |
| 4324 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4325 | return ret; |
| 4326 | } |
| 4327 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4328 | int ufshcd_uic_hibern8_enter(struct ufs_hba *hba) |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4329 | { |
| 4330 | int ret = 0, retries; |
| 4331 | |
| 4332 | for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) { |
| 4333 | ret = __ufshcd_uic_hibern8_enter(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4334 | if (!ret) |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4335 | goto out; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4336 | /* Unable to recover the link, so no point proceeding */ |
| 4337 | if (ret == -ENOLINK) |
| 4338 | BUG(); |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4339 | } |
| 4340 | out: |
| 4341 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4342 | } |
| 4343 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4344 | int ufshcd_uic_hibern8_exit(struct ufs_hba *hba) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4345 | { |
| 4346 | struct uic_command uic_cmd = {0}; |
| 4347 | int ret; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4348 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4349 | |
| 4350 | uic_cmd.command = UIC_CMD_DME_HIBER_EXIT; |
| 4351 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4352 | trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit", |
| 4353 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
| 4354 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 4355 | /* Do full reinit if exit failed */ |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4356 | if (ret) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4357 | ufshcd_update_error_stats(hba, UFS_ERR_HIBERN8_EXIT); |
| 4358 | dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d", |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 4359 | __func__, ret); |
| 4360 | ret = ufshcd_link_recovery(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4361 | /* Unable to recover the link, so no point proceeding */ |
| 4362 | if (ret) |
| 4363 | BUG(); |
| 4364 | } else { |
| 4365 | dev_dbg(hba->dev, "%s: Hibern8 Exit at %lld us", __func__, |
| 4366 | ktime_to_us(ktime_get())); |
| 4367 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_get(); |
| 4368 | hba->ufs_stats.hibern8_exit_cnt++; |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4369 | } |
| 4370 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4371 | return ret; |
| 4372 | } |
| 4373 | |
Yaniv Gardi | 5064636 | 2014-10-23 13:25:13 +0300 | [diff] [blame] | 4374 | /** |
| 4375 | * ufshcd_init_pwr_info - setting the POR (power on reset) |
| 4376 | * values in hba power info |
| 4377 | * @hba: per-adapter instance |
| 4378 | */ |
| 4379 | static void ufshcd_init_pwr_info(struct ufs_hba *hba) |
| 4380 | { |
| 4381 | hba->pwr_info.gear_rx = UFS_PWM_G1; |
| 4382 | hba->pwr_info.gear_tx = UFS_PWM_G1; |
| 4383 | hba->pwr_info.lane_rx = 1; |
| 4384 | hba->pwr_info.lane_tx = 1; |
| 4385 | hba->pwr_info.pwr_rx = SLOWAUTO_MODE; |
| 4386 | hba->pwr_info.pwr_tx = SLOWAUTO_MODE; |
| 4387 | hba->pwr_info.hs_rate = 0; |
| 4388 | } |
| 4389 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4390 | /** |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4391 | * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device |
| 4392 | * @hba: per-adapter instance |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4393 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4394 | static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4395 | { |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4396 | struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info; |
| 4397 | |
| 4398 | if (hba->max_pwr_info.is_valid) |
| 4399 | return 0; |
| 4400 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4401 | pwr_info->pwr_tx = FAST_MODE; |
| 4402 | pwr_info->pwr_rx = FAST_MODE; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4403 | pwr_info->hs_rate = PA_HS_MODE_B; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4404 | |
| 4405 | /* Get the connected lane count */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4406 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES), |
| 4407 | &pwr_info->lane_rx); |
| 4408 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 4409 | &pwr_info->lane_tx); |
| 4410 | |
| 4411 | if (!pwr_info->lane_rx || !pwr_info->lane_tx) { |
| 4412 | dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n", |
| 4413 | __func__, |
| 4414 | pwr_info->lane_rx, |
| 4415 | pwr_info->lane_tx); |
| 4416 | return -EINVAL; |
| 4417 | } |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4418 | |
| 4419 | /* |
| 4420 | * First, get the maximum gears of HS speed. |
| 4421 | * If a zero value, it means there is no HSGEAR capability. |
| 4422 | * Then, get the maximum gears of PWM speed. |
| 4423 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4424 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx); |
| 4425 | if (!pwr_info->gear_rx) { |
| 4426 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), |
| 4427 | &pwr_info->gear_rx); |
| 4428 | if (!pwr_info->gear_rx) { |
| 4429 | dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n", |
| 4430 | __func__, pwr_info->gear_rx); |
| 4431 | return -EINVAL; |
Subhash Jadavani | 5e45e70 | 2016-08-09 18:43:10 -0700 | [diff] [blame] | 4432 | } else { |
| 4433 | if (hba->limit_rx_pwm_gear > 0 && |
| 4434 | (hba->limit_rx_pwm_gear < pwr_info->gear_rx)) |
| 4435 | pwr_info->gear_rx = hba->limit_rx_pwm_gear; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4436 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4437 | pwr_info->pwr_rx = SLOW_MODE; |
Subhash Jadavani | 5e45e70 | 2016-08-09 18:43:10 -0700 | [diff] [blame] | 4438 | } else { |
| 4439 | if (hba->limit_rx_hs_gear > 0 && |
| 4440 | (hba->limit_rx_hs_gear < pwr_info->gear_rx)) |
| 4441 | pwr_info->gear_rx = hba->limit_rx_hs_gear; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4442 | } |
| 4443 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4444 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), |
| 4445 | &pwr_info->gear_tx); |
| 4446 | if (!pwr_info->gear_tx) { |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4447 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4448 | &pwr_info->gear_tx); |
| 4449 | if (!pwr_info->gear_tx) { |
| 4450 | dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n", |
| 4451 | __func__, pwr_info->gear_tx); |
| 4452 | return -EINVAL; |
Subhash Jadavani | 5e45e70 | 2016-08-09 18:43:10 -0700 | [diff] [blame] | 4453 | } else { |
| 4454 | if (hba->limit_tx_pwm_gear > 0 && |
| 4455 | (hba->limit_tx_pwm_gear < pwr_info->gear_tx)) |
| 4456 | pwr_info->gear_tx = hba->limit_tx_pwm_gear; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4457 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4458 | pwr_info->pwr_tx = SLOW_MODE; |
Subhash Jadavani | 5e45e70 | 2016-08-09 18:43:10 -0700 | [diff] [blame] | 4459 | } else { |
| 4460 | if (hba->limit_tx_hs_gear > 0 && |
| 4461 | (hba->limit_tx_hs_gear < pwr_info->gear_tx)) |
| 4462 | pwr_info->gear_tx = hba->limit_tx_hs_gear; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4463 | } |
| 4464 | |
| 4465 | hba->max_pwr_info.is_valid = true; |
| 4466 | return 0; |
| 4467 | } |
| 4468 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4469 | int ufshcd_change_power_mode(struct ufs_hba *hba, |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4470 | struct ufs_pa_layer_attr *pwr_mode) |
| 4471 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4472 | int ret = 0; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4473 | |
| 4474 | /* if already configured to the requested pwr_mode */ |
| 4475 | if (pwr_mode->gear_rx == hba->pwr_info.gear_rx && |
| 4476 | pwr_mode->gear_tx == hba->pwr_info.gear_tx && |
| 4477 | pwr_mode->lane_rx == hba->pwr_info.lane_rx && |
| 4478 | pwr_mode->lane_tx == hba->pwr_info.lane_tx && |
| 4479 | pwr_mode->pwr_rx == hba->pwr_info.pwr_rx && |
| 4480 | pwr_mode->pwr_tx == hba->pwr_info.pwr_tx && |
| 4481 | pwr_mode->hs_rate == hba->pwr_info.hs_rate) { |
| 4482 | dev_dbg(hba->dev, "%s: power already configured\n", __func__); |
| 4483 | return 0; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4484 | } |
| 4485 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4486 | ufsdbg_error_inject_dispatcher(hba, ERR_INJECT_PWR_CHANGE, 0, &ret); |
| 4487 | if (ret) |
| 4488 | return ret; |
| 4489 | |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4490 | /* |
| 4491 | * Configure attributes for power mode change with below. |
| 4492 | * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION, |
| 4493 | * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION, |
| 4494 | * - PA_HSSERIES |
| 4495 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4496 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx); |
| 4497 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES), |
| 4498 | pwr_mode->lane_rx); |
| 4499 | if (pwr_mode->pwr_rx == FASTAUTO_MODE || |
| 4500 | pwr_mode->pwr_rx == FAST_MODE) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4501 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4502 | else |
| 4503 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4504 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4505 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx); |
| 4506 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES), |
| 4507 | pwr_mode->lane_tx); |
| 4508 | if (pwr_mode->pwr_tx == FASTAUTO_MODE || |
| 4509 | pwr_mode->pwr_tx == FAST_MODE) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4510 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4511 | else |
| 4512 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4513 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4514 | if (pwr_mode->pwr_rx == FASTAUTO_MODE || |
| 4515 | pwr_mode->pwr_tx == FASTAUTO_MODE || |
| 4516 | pwr_mode->pwr_rx == FAST_MODE || |
| 4517 | pwr_mode->pwr_tx == FAST_MODE) |
| 4518 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES), |
| 4519 | pwr_mode->hs_rate); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4520 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4521 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0), |
| 4522 | DL_FC0ProtectionTimeOutVal_Default); |
| 4523 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1), |
| 4524 | DL_TC0ReplayTimeOutVal_Default); |
| 4525 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2), |
| 4526 | DL_AFC0ReqTimeOutVal_Default); |
| 4527 | |
| 4528 | ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalFC0ProtectionTimeOutVal), |
| 4529 | DL_FC0ProtectionTimeOutVal_Default); |
| 4530 | ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalTC0ReplayTimeOutVal), |
| 4531 | DL_TC0ReplayTimeOutVal_Default); |
| 4532 | ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalAFC0ReqTimeOutVal), |
| 4533 | DL_AFC0ReqTimeOutVal_Default); |
| 4534 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4535 | ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4 |
| 4536 | | pwr_mode->pwr_tx); |
| 4537 | |
| 4538 | if (ret) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4539 | ufshcd_update_error_stats(hba, UFS_ERR_POWER_MODE_CHANGE); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4540 | dev_err(hba->dev, |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4541 | "%s: power mode change failed %d\n", __func__, ret); |
| 4542 | } else { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4543 | ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL, |
| 4544 | pwr_mode); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4545 | |
| 4546 | memcpy(&hba->pwr_info, pwr_mode, |
| 4547 | sizeof(struct ufs_pa_layer_attr)); |
| 4548 | } |
| 4549 | |
| 4550 | return ret; |
| 4551 | } |
| 4552 | |
| 4553 | /** |
| 4554 | * ufshcd_config_pwr_mode - configure a new power mode |
| 4555 | * @hba: per-adapter instance |
| 4556 | * @desired_pwr_mode: desired power configuration |
| 4557 | */ |
| 4558 | static int ufshcd_config_pwr_mode(struct ufs_hba *hba, |
| 4559 | struct ufs_pa_layer_attr *desired_pwr_mode) |
| 4560 | { |
| 4561 | struct ufs_pa_layer_attr final_params = { 0 }; |
| 4562 | int ret; |
| 4563 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4564 | ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE, |
| 4565 | desired_pwr_mode, &final_params); |
| 4566 | |
| 4567 | if (ret) |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4568 | memcpy(&final_params, desired_pwr_mode, sizeof(final_params)); |
| 4569 | |
| 4570 | ret = ufshcd_change_power_mode(hba, &final_params); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4571 | if (!ret) |
| 4572 | ufshcd_print_pwr_info(hba); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4573 | |
| 4574 | return ret; |
| 4575 | } |
| 4576 | |
| 4577 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4578 | * ufshcd_complete_dev_init() - checks device readiness |
| 4579 | * hba: per-adapter instance |
| 4580 | * |
| 4581 | * Set fDeviceInit flag and poll until device toggles it. |
| 4582 | */ |
| 4583 | static int ufshcd_complete_dev_init(struct ufs_hba *hba) |
| 4584 | { |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 4585 | int i; |
| 4586 | int err; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4587 | bool flag_res = 1; |
| 4588 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 4589 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG, |
| 4590 | QUERY_FLAG_IDN_FDEVICEINIT, NULL); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4591 | if (err) { |
| 4592 | dev_err(hba->dev, |
| 4593 | "%s setting fDeviceInit flag failed with error %d\n", |
| 4594 | __func__, err); |
| 4595 | goto out; |
| 4596 | } |
| 4597 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 4598 | /* poll for max. 1000 iterations for fDeviceInit flag to clear */ |
| 4599 | for (i = 0; i < 1000 && !err && flag_res; i++) |
| 4600 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG, |
| 4601 | QUERY_FLAG_IDN_FDEVICEINIT, &flag_res); |
| 4602 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4603 | if (err) |
| 4604 | dev_err(hba->dev, |
| 4605 | "%s reading fDeviceInit flag failed with error %d\n", |
| 4606 | __func__, err); |
| 4607 | else if (flag_res) |
| 4608 | dev_err(hba->dev, |
| 4609 | "%s fDeviceInit was not cleared by the device\n", |
| 4610 | __func__); |
| 4611 | |
| 4612 | out: |
| 4613 | return err; |
| 4614 | } |
| 4615 | |
| 4616 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4617 | * ufshcd_make_hba_operational - Make UFS controller operational |
| 4618 | * @hba: per adapter instance |
| 4619 | * |
| 4620 | * To bring UFS host controller to operational state, |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4621 | * 1. Enable required interrupts |
| 4622 | * 2. Configure interrupt aggregation |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 4623 | * 3. Program UTRL and UTMRL base address |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4624 | * 4. Configure run-stop-registers |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4625 | * |
| 4626 | * Returns 0 on success, non-zero value on failure |
| 4627 | */ |
| 4628 | static int ufshcd_make_hba_operational(struct ufs_hba *hba) |
| 4629 | { |
| 4630 | int err = 0; |
| 4631 | u32 reg; |
| 4632 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4633 | /* Enable required interrupts */ |
| 4634 | ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS); |
| 4635 | |
| 4636 | /* Configure interrupt aggregation */ |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 4637 | if (ufshcd_is_intr_aggr_allowed(hba)) |
| 4638 | ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO); |
| 4639 | else |
| 4640 | ufshcd_disable_intr_aggr(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4641 | |
| 4642 | /* Configure UTRL and UTMRL base address registers */ |
| 4643 | ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr), |
| 4644 | REG_UTP_TRANSFER_REQ_LIST_BASE_L); |
| 4645 | ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr), |
| 4646 | REG_UTP_TRANSFER_REQ_LIST_BASE_H); |
| 4647 | ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr), |
| 4648 | REG_UTP_TASK_REQ_LIST_BASE_L); |
| 4649 | ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr), |
| 4650 | REG_UTP_TASK_REQ_LIST_BASE_H); |
| 4651 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4652 | /* |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 4653 | * Make sure base address and interrupt setup are updated before |
| 4654 | * enabling the run/stop registers below. |
| 4655 | */ |
| 4656 | wmb(); |
| 4657 | |
| 4658 | /* |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4659 | * UCRDY, UTMRLDY and UTRLRDY bits must be 1 |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4660 | */ |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4661 | reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4662 | if (!(ufshcd_get_lists_status(reg))) { |
| 4663 | ufshcd_enable_run_stop_reg(hba); |
| 4664 | } else { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 4665 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4666 | "Host controller not ready to process requests"); |
| 4667 | err = -EIO; |
| 4668 | goto out; |
| 4669 | } |
| 4670 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4671 | out: |
| 4672 | return err; |
| 4673 | } |
| 4674 | |
| 4675 | /** |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4676 | * ufshcd_hba_stop - Send controller to reset state |
| 4677 | * @hba: per adapter instance |
| 4678 | * @can_sleep: perform sleep or just spin |
| 4679 | */ |
| 4680 | static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep) |
| 4681 | { |
| 4682 | int err; |
| 4683 | |
| 4684 | ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE); |
| 4685 | err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE, |
| 4686 | CONTROLLER_ENABLE, CONTROLLER_DISABLE, |
| 4687 | 10, 1, can_sleep); |
| 4688 | if (err) |
| 4689 | dev_err(hba->dev, "%s: Controller disable failed\n", __func__); |
| 4690 | } |
| 4691 | |
| 4692 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4693 | * ufshcd_hba_enable - initialize the controller |
| 4694 | * @hba: per adapter instance |
| 4695 | * |
| 4696 | * The controller resets itself and controller firmware initialization |
| 4697 | * sequence kicks off. When controller is ready it will set |
| 4698 | * the Host Controller Enable bit to 1. |
| 4699 | * |
| 4700 | * Returns 0 on success, non-zero value on failure |
| 4701 | */ |
| 4702 | static int ufshcd_hba_enable(struct ufs_hba *hba) |
| 4703 | { |
| 4704 | int retry; |
| 4705 | |
| 4706 | /* |
| 4707 | * msleep of 1 and 5 used in this function might result in msleep(20), |
| 4708 | * but it was necessary to send the UFS FPGA to reset mode during |
| 4709 | * development and testing of this driver. msleep can be changed to |
| 4710 | * mdelay and retry count can be reduced based on the controller. |
| 4711 | */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4712 | if (!ufshcd_is_hba_active(hba)) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4713 | /* change controller state to "reset state" */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4714 | ufshcd_hba_stop(hba, true); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4715 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4716 | /* UniPro link is disabled at this point */ |
| 4717 | ufshcd_set_link_off(hba); |
| 4718 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4719 | ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4720 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4721 | /* start controller initialization sequence */ |
| 4722 | ufshcd_hba_start(hba); |
| 4723 | |
| 4724 | /* |
| 4725 | * To initialize a UFS host controller HCE bit must be set to 1. |
| 4726 | * During initialization the HCE bit value changes from 1->0->1. |
| 4727 | * When the host controller completes initialization sequence |
| 4728 | * it sets the value of HCE bit to 1. The same HCE bit is read back |
| 4729 | * to check if the controller has completed initialization sequence. |
| 4730 | * So without this delay the value HCE = 1, set in the previous |
| 4731 | * instruction might be read back. |
| 4732 | * This delay can be changed based on the controller. |
| 4733 | */ |
| 4734 | msleep(1); |
| 4735 | |
| 4736 | /* wait for the host controller to complete initialization */ |
| 4737 | retry = 10; |
| 4738 | while (ufshcd_is_hba_active(hba)) { |
| 4739 | if (retry) { |
| 4740 | retry--; |
| 4741 | } else { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 4742 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4743 | "Controller enable failed\n"); |
| 4744 | return -EIO; |
| 4745 | } |
| 4746 | msleep(5); |
| 4747 | } |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4748 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4749 | /* enable UIC related interrupts */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4750 | ufshcd_enable_intr(hba, UFSHCD_UIC_MASK); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4751 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4752 | ufshcd_vops_hce_enable_notify(hba, POST_CHANGE); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4753 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4754 | return 0; |
| 4755 | } |
| 4756 | |
Yaniv Gardi | 7ca38cf | 2015-05-17 18:54:59 +0300 | [diff] [blame] | 4757 | static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer) |
| 4758 | { |
| 4759 | int tx_lanes, i, err = 0; |
| 4760 | |
| 4761 | if (!peer) |
| 4762 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 4763 | &tx_lanes); |
| 4764 | else |
| 4765 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 4766 | &tx_lanes); |
| 4767 | for (i = 0; i < tx_lanes; i++) { |
| 4768 | if (!peer) |
| 4769 | err = ufshcd_dme_set(hba, |
| 4770 | UIC_ARG_MIB_SEL(TX_LCC_ENABLE, |
| 4771 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)), |
| 4772 | 0); |
| 4773 | else |
| 4774 | err = ufshcd_dme_peer_set(hba, |
| 4775 | UIC_ARG_MIB_SEL(TX_LCC_ENABLE, |
| 4776 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)), |
| 4777 | 0); |
| 4778 | if (err) { |
| 4779 | dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d", |
| 4780 | __func__, peer, i, err); |
| 4781 | break; |
| 4782 | } |
| 4783 | } |
| 4784 | |
| 4785 | return err; |
| 4786 | } |
| 4787 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4788 | static inline int ufshcd_disable_host_tx_lcc(struct ufs_hba *hba) |
| 4789 | { |
| 4790 | return ufshcd_disable_tx_lcc(hba, false); |
| 4791 | } |
| 4792 | |
Yaniv Gardi | 7ca38cf | 2015-05-17 18:54:59 +0300 | [diff] [blame] | 4793 | static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba) |
| 4794 | { |
| 4795 | return ufshcd_disable_tx_lcc(hba, true); |
| 4796 | } |
| 4797 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4798 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4799 | * ufshcd_link_startup - Initialize unipro link startup |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4800 | * @hba: per adapter instance |
| 4801 | * |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4802 | * Returns 0 for success, non-zero in case of failure |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4803 | */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4804 | static int ufshcd_link_startup(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4805 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4806 | int ret; |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4807 | int retries = DME_LINKSTARTUP_RETRIES; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4808 | bool link_startup_again = false; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4809 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4810 | /* |
| 4811 | * If UFS device isn't active then we will have to issue link startup |
| 4812 | * 2 times to make sure the device state move to active. |
| 4813 | */ |
| 4814 | if (!ufshcd_is_ufs_dev_active(hba)) |
| 4815 | link_startup_again = true; |
| 4816 | |
| 4817 | link_startup: |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4818 | do { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4819 | ufshcd_vops_link_startup_notify(hba, PRE_CHANGE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4820 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4821 | ret = ufshcd_dme_link_startup(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4822 | if (ret) |
| 4823 | ufshcd_update_error_stats(hba, UFS_ERR_LINKSTARTUP); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4824 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4825 | /* check if device is detected by inter-connect layer */ |
| 4826 | if (!ret && !ufshcd_is_device_present(hba)) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4827 | ufshcd_update_error_stats(hba, UFS_ERR_LINKSTARTUP); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4828 | dev_err(hba->dev, "%s: Device not present\n", __func__); |
| 4829 | ret = -ENXIO; |
| 4830 | goto out; |
| 4831 | } |
| 4832 | |
| 4833 | /* |
| 4834 | * DME link lost indication is only received when link is up, |
| 4835 | * but we can't be sure if the link is up until link startup |
| 4836 | * succeeds. So reset the local Uni-Pro and try again. |
| 4837 | */ |
| 4838 | if (ret && ufshcd_hba_enable(hba)) |
| 4839 | goto out; |
| 4840 | } while (ret && retries--); |
| 4841 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4842 | if (ret) |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4843 | /* failed to get the link up... retire */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4844 | goto out; |
| 4845 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4846 | if (link_startup_again) { |
| 4847 | link_startup_again = false; |
| 4848 | retries = DME_LINKSTARTUP_RETRIES; |
| 4849 | goto link_startup; |
| 4850 | } |
| 4851 | |
| 4852 | /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */ |
| 4853 | ufshcd_init_pwr_info(hba); |
| 4854 | ufshcd_print_pwr_info(hba); |
| 4855 | |
Yaniv Gardi | 7ca38cf | 2015-05-17 18:54:59 +0300 | [diff] [blame] | 4856 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) { |
| 4857 | ret = ufshcd_disable_device_tx_lcc(hba); |
| 4858 | if (ret) |
| 4859 | goto out; |
| 4860 | } |
| 4861 | |
Subhash Jadavani | 4f0df17b | 2016-12-16 13:19:27 -0800 | [diff] [blame] | 4862 | if (hba->dev_info.quirks & UFS_DEVICE_QUIRK_BROKEN_LCC) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4863 | ret = ufshcd_disable_host_tx_lcc(hba); |
| 4864 | if (ret) |
| 4865 | goto out; |
| 4866 | } |
| 4867 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4868 | /* Include any host controller configuration via UIC commands */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4869 | ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE); |
| 4870 | if (ret) |
| 4871 | goto out; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4872 | |
| 4873 | ret = ufshcd_make_hba_operational(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4874 | out: |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 4875 | if (ret) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4876 | dev_err(hba->dev, "link startup failed %d\n", ret); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 4877 | ufshcd_print_host_state(hba); |
| 4878 | ufshcd_print_pwr_info(hba); |
| 4879 | ufshcd_print_host_regs(hba); |
| 4880 | } |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4881 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4882 | } |
| 4883 | |
| 4884 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4885 | * ufshcd_verify_dev_init() - Verify device initialization |
| 4886 | * @hba: per-adapter instance |
| 4887 | * |
| 4888 | * Send NOP OUT UPIU and wait for NOP IN response to check whether the |
| 4889 | * device Transport Protocol (UTP) layer is ready after a reset. |
| 4890 | * If the UTP layer at the device side is not initialized, it may |
| 4891 | * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT |
| 4892 | * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations. |
| 4893 | */ |
| 4894 | static int ufshcd_verify_dev_init(struct ufs_hba *hba) |
| 4895 | { |
| 4896 | int err = 0; |
| 4897 | int retries; |
| 4898 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4899 | ufshcd_hold_all(hba); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4900 | mutex_lock(&hba->dev_cmd.lock); |
| 4901 | for (retries = NOP_OUT_RETRIES; retries > 0; retries--) { |
| 4902 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP, |
| 4903 | NOP_OUT_TIMEOUT); |
| 4904 | |
| 4905 | if (!err || err == -ETIMEDOUT) |
| 4906 | break; |
| 4907 | |
| 4908 | dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err); |
| 4909 | } |
| 4910 | mutex_unlock(&hba->dev_cmd.lock); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4911 | ufshcd_release_all(hba); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4912 | |
| 4913 | if (err) |
| 4914 | dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err); |
| 4915 | return err; |
| 4916 | } |
| 4917 | |
| 4918 | /** |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4919 | * ufshcd_set_queue_depth - set lun queue depth |
| 4920 | * @sdev: pointer to SCSI device |
| 4921 | * |
| 4922 | * Read bLUQueueDepth value and activate scsi tagged command |
| 4923 | * queueing. For WLUN, queue depth is set to 1. For best-effort |
| 4924 | * cases (bLUQueueDepth = 0) the queue depth is set to a maximum |
| 4925 | * value that host can queue. |
| 4926 | */ |
| 4927 | static void ufshcd_set_queue_depth(struct scsi_device *sdev) |
| 4928 | { |
| 4929 | int ret = 0; |
| 4930 | u8 lun_qdepth; |
| 4931 | struct ufs_hba *hba; |
| 4932 | |
| 4933 | hba = shost_priv(sdev->host); |
| 4934 | |
| 4935 | lun_qdepth = hba->nutrs; |
| 4936 | ret = ufshcd_read_unit_desc_param(hba, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4937 | ufshcd_scsi_to_upiu_lun(sdev->lun), |
| 4938 | UNIT_DESC_PARAM_LU_Q_DEPTH, |
| 4939 | &lun_qdepth, |
| 4940 | sizeof(lun_qdepth)); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4941 | |
| 4942 | /* Some WLUN doesn't support unit descriptor */ |
| 4943 | if (ret == -EOPNOTSUPP) |
| 4944 | lun_qdepth = 1; |
| 4945 | else if (!lun_qdepth) |
| 4946 | /* eventually, we can figure out the real queue depth */ |
| 4947 | lun_qdepth = hba->nutrs; |
| 4948 | else |
| 4949 | lun_qdepth = min_t(int, lun_qdepth, hba->nutrs); |
| 4950 | |
| 4951 | dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n", |
| 4952 | __func__, lun_qdepth); |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4953 | scsi_change_queue_depth(sdev, lun_qdepth); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4954 | } |
| 4955 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4956 | /* |
| 4957 | * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR |
| 4958 | * @hba: per-adapter instance |
| 4959 | * @lun: UFS device lun id |
| 4960 | * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info |
| 4961 | * |
| 4962 | * Returns 0 in case of success and b_lu_write_protect status would be returned |
| 4963 | * @b_lu_write_protect parameter. |
| 4964 | * Returns -ENOTSUPP if reading b_lu_write_protect is not supported. |
| 4965 | * Returns -EINVAL in case of invalid parameters passed to this function. |
| 4966 | */ |
| 4967 | static int ufshcd_get_lu_wp(struct ufs_hba *hba, |
| 4968 | u8 lun, |
| 4969 | u8 *b_lu_write_protect) |
| 4970 | { |
| 4971 | int ret; |
| 4972 | |
| 4973 | if (!b_lu_write_protect) |
| 4974 | ret = -EINVAL; |
| 4975 | /* |
| 4976 | * According to UFS device spec, RPMB LU can't be write |
| 4977 | * protected so skip reading bLUWriteProtect parameter for |
| 4978 | * it. For other W-LUs, UNIT DESCRIPTOR is not available. |
| 4979 | */ |
| 4980 | else if (lun >= UFS_UPIU_MAX_GENERAL_LUN) |
| 4981 | ret = -ENOTSUPP; |
| 4982 | else |
| 4983 | ret = ufshcd_read_unit_desc_param(hba, |
| 4984 | lun, |
| 4985 | UNIT_DESC_PARAM_LU_WR_PROTECT, |
| 4986 | b_lu_write_protect, |
| 4987 | sizeof(*b_lu_write_protect)); |
| 4988 | return ret; |
| 4989 | } |
| 4990 | |
| 4991 | /** |
| 4992 | * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect |
| 4993 | * status |
| 4994 | * @hba: per-adapter instance |
| 4995 | * @sdev: pointer to SCSI device |
| 4996 | * |
| 4997 | */ |
| 4998 | static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba, |
| 4999 | struct scsi_device *sdev) |
| 5000 | { |
| 5001 | if (hba->dev_info.f_power_on_wp_en && |
| 5002 | !hba->dev_info.is_lu_power_on_wp) { |
| 5003 | u8 b_lu_write_protect; |
| 5004 | |
| 5005 | if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun), |
| 5006 | &b_lu_write_protect) && |
| 5007 | (b_lu_write_protect == UFS_LU_POWER_ON_WP)) |
| 5008 | hba->dev_info.is_lu_power_on_wp = true; |
| 5009 | } |
| 5010 | } |
| 5011 | |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 5012 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5013 | * ufshcd_slave_alloc - handle initial SCSI device configurations |
| 5014 | * @sdev: pointer to SCSI device |
| 5015 | * |
| 5016 | * Returns success |
| 5017 | */ |
| 5018 | static int ufshcd_slave_alloc(struct scsi_device *sdev) |
| 5019 | { |
| 5020 | struct ufs_hba *hba; |
| 5021 | |
| 5022 | hba = shost_priv(sdev->host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5023 | |
| 5024 | /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */ |
| 5025 | sdev->use_10_for_ms = 1; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5026 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5027 | /* allow SCSI layer to restart the device in case of errors */ |
| 5028 | sdev->allow_restart = 1; |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 5029 | |
Sujit Reddy Thumma | b2a6c52 | 2014-07-01 12:22:38 +0300 | [diff] [blame] | 5030 | /* REPORT SUPPORTED OPERATION CODES is not supported */ |
| 5031 | sdev->no_report_opcodes = 1; |
| 5032 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5033 | /* WRITE_SAME command is not supported*/ |
| 5034 | sdev->no_write_same = 1; |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 5035 | |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 5036 | ufshcd_set_queue_depth(sdev); |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 5037 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5038 | ufshcd_get_lu_power_on_wp_status(hba, sdev); |
| 5039 | |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 5040 | return 0; |
| 5041 | } |
| 5042 | |
| 5043 | /** |
| 5044 | * ufshcd_change_queue_depth - change queue depth |
| 5045 | * @sdev: pointer to SCSI device |
| 5046 | * @depth: required depth to set |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 5047 | * |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 5048 | * 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] | 5049 | */ |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 5050 | 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] | 5051 | { |
| 5052 | struct ufs_hba *hba = shost_priv(sdev->host); |
| 5053 | |
| 5054 | if (depth > hba->nutrs) |
| 5055 | depth = hba->nutrs; |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 5056 | return scsi_change_queue_depth(sdev, depth); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5057 | } |
| 5058 | |
| 5059 | /** |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 5060 | * ufshcd_slave_configure - adjust SCSI device configurations |
| 5061 | * @sdev: pointer to SCSI device |
| 5062 | */ |
| 5063 | static int ufshcd_slave_configure(struct scsi_device *sdev) |
| 5064 | { |
| 5065 | struct request_queue *q = sdev->request_queue; |
Subhash Jadavani | 5ea586f | 2016-08-17 19:08:09 -0700 | [diff] [blame] | 5066 | struct ufs_hba *hba = shost_priv(sdev->host); |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 5067 | |
| 5068 | blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1); |
| 5069 | blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX); |
| 5070 | |
Subhash Jadavani | 5ea586f | 2016-08-17 19:08:09 -0700 | [diff] [blame] | 5071 | if (hba->scsi_cmd_timeout) { |
| 5072 | blk_queue_rq_timeout(q, hba->scsi_cmd_timeout * HZ); |
| 5073 | scsi_set_cmd_timeout_override(sdev, hba->scsi_cmd_timeout * HZ); |
| 5074 | } |
| 5075 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5076 | sdev->autosuspend_delay = UFSHCD_AUTO_SUSPEND_DELAY_MS; |
| 5077 | sdev->use_rpm_auto = 1; |
| 5078 | |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 5079 | return 0; |
| 5080 | } |
| 5081 | |
| 5082 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5083 | * ufshcd_slave_destroy - remove SCSI device configurations |
| 5084 | * @sdev: pointer to SCSI device |
| 5085 | */ |
| 5086 | static void ufshcd_slave_destroy(struct scsi_device *sdev) |
| 5087 | { |
| 5088 | struct ufs_hba *hba; |
| 5089 | |
| 5090 | hba = shost_priv(sdev->host); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 5091 | /* Drop the reference as it won't be needed anymore */ |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 5092 | if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) { |
| 5093 | unsigned long flags; |
| 5094 | |
| 5095 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 5096 | hba->sdev_ufs_device = NULL; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 5097 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5098 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5099 | } |
| 5100 | |
| 5101 | /** |
| 5102 | * ufshcd_task_req_compl - handle task management request completion |
| 5103 | * @hba: per adapter instance |
| 5104 | * @index: index of the completed request |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5105 | * @resp: task management service response |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5106 | * |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5107 | * Returns non-zero value on error, zero on success |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5108 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5109 | 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] | 5110 | { |
| 5111 | struct utp_task_req_desc *task_req_descp; |
| 5112 | struct utp_upiu_task_rsp *task_rsp_upiup; |
| 5113 | unsigned long flags; |
| 5114 | int ocs_value; |
| 5115 | int task_result; |
| 5116 | |
| 5117 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5118 | |
| 5119 | /* Clear completed tasks from outstanding_tasks */ |
| 5120 | __clear_bit(index, &hba->outstanding_tasks); |
| 5121 | |
| 5122 | task_req_descp = hba->utmrdl_base_addr; |
| 5123 | ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]); |
| 5124 | |
| 5125 | if (ocs_value == OCS_SUCCESS) { |
| 5126 | task_rsp_upiup = (struct utp_upiu_task_rsp *) |
| 5127 | task_req_descp[index].task_rsp_upiu; |
Kiwoong Kim | 8794ee0 | 2016-09-09 08:22:22 +0900 | [diff] [blame] | 5128 | task_result = be32_to_cpu(task_rsp_upiup->output_param1); |
| 5129 | task_result = task_result & MASK_TM_SERVICE_RESP; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5130 | if (resp) |
| 5131 | *resp = (u8)task_result; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5132 | } else { |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5133 | dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", |
| 5134 | __func__, ocs_value); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5135 | } |
| 5136 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5137 | |
| 5138 | return ocs_value; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5139 | } |
| 5140 | |
| 5141 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5142 | * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status |
| 5143 | * @lrb: pointer to local reference block of completed command |
| 5144 | * @scsi_status: SCSI command status |
| 5145 | * |
| 5146 | * Returns value base on SCSI command status |
| 5147 | */ |
| 5148 | static inline int |
| 5149 | ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status) |
| 5150 | { |
| 5151 | int result = 0; |
| 5152 | |
| 5153 | switch (scsi_status) { |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 5154 | case SAM_STAT_CHECK_CONDITION: |
| 5155 | ufshcd_copy_sense_data(lrbp); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5156 | case SAM_STAT_GOOD: |
| 5157 | result |= DID_OK << 16 | |
| 5158 | COMMAND_COMPLETE << 8 | |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 5159 | scsi_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5160 | break; |
| 5161 | case SAM_STAT_TASK_SET_FULL: |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 5162 | case SAM_STAT_BUSY: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5163 | case SAM_STAT_TASK_ABORTED: |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 5164 | ufshcd_copy_sense_data(lrbp); |
| 5165 | result |= scsi_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5166 | break; |
| 5167 | default: |
| 5168 | result |= DID_ERROR << 16; |
| 5169 | break; |
| 5170 | } /* end of switch */ |
| 5171 | |
| 5172 | return result; |
| 5173 | } |
| 5174 | |
| 5175 | /** |
| 5176 | * ufshcd_transfer_rsp_status - Get overall status of the response |
| 5177 | * @hba: per adapter instance |
| 5178 | * @lrb: pointer to local reference block of completed command |
| 5179 | * |
| 5180 | * Returns result of the command to notify SCSI midlayer |
| 5181 | */ |
| 5182 | static inline int |
| 5183 | ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 5184 | { |
| 5185 | int result = 0; |
| 5186 | int scsi_status; |
| 5187 | int ocs; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5188 | bool print_prdt; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5189 | |
| 5190 | /* overall command status of utrd */ |
| 5191 | ocs = ufshcd_get_tr_ocs(lrbp); |
| 5192 | |
| 5193 | switch (ocs) { |
| 5194 | case OCS_SUCCESS: |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 5195 | result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5196 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 5197 | switch (result) { |
| 5198 | case UPIU_TRANSACTION_RESPONSE: |
| 5199 | /* |
| 5200 | * get the response UPIU result to extract |
| 5201 | * the SCSI command status |
| 5202 | */ |
| 5203 | result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr); |
| 5204 | |
| 5205 | /* |
| 5206 | * get the result based on SCSI status response |
| 5207 | * to notify the SCSI midlayer of the command status |
| 5208 | */ |
| 5209 | scsi_status = result & MASK_SCSI_STATUS; |
| 5210 | result = ufshcd_scsi_cmd_status(lrbp, scsi_status); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5211 | |
Yaniv Gardi | f05ac2e | 2016-02-01 15:02:42 +0200 | [diff] [blame] | 5212 | /* |
| 5213 | * Currently we are only supporting BKOPs exception |
| 5214 | * events hence we can ignore BKOPs exception event |
| 5215 | * during power management callbacks. BKOPs exception |
| 5216 | * event is not expected to be raised in runtime suspend |
| 5217 | * callback as it allows the urgent bkops. |
| 5218 | * During system suspend, we are anyway forcefully |
| 5219 | * disabling the bkops and if urgent bkops is needed |
| 5220 | * it will be enabled on system resume. Long term |
| 5221 | * solution could be to abort the system suspend if |
| 5222 | * UFS device needs urgent BKOPs. |
| 5223 | */ |
| 5224 | if (!hba->pm_op_in_progress && |
| 5225 | ufshcd_is_exception_event(lrbp->ucd_rsp_ptr)) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5226 | schedule_work(&hba->eeh_work); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 5227 | break; |
| 5228 | case UPIU_TRANSACTION_REJECT_UPIU: |
| 5229 | /* TODO: handle Reject UPIU Response */ |
| 5230 | result = DID_ERROR << 16; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 5231 | dev_err(hba->dev, |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 5232 | "Reject UPIU not fully implemented\n"); |
| 5233 | break; |
| 5234 | default: |
| 5235 | result = DID_ERROR << 16; |
| 5236 | dev_err(hba->dev, |
| 5237 | "Unexpected request response code = %x\n", |
| 5238 | result); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5239 | break; |
| 5240 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5241 | break; |
| 5242 | case OCS_ABORTED: |
| 5243 | result |= DID_ABORT << 16; |
| 5244 | break; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5245 | case OCS_INVALID_COMMAND_STATUS: |
| 5246 | result |= DID_REQUEUE << 16; |
| 5247 | break; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5248 | case OCS_INVALID_CMD_TABLE_ATTR: |
| 5249 | case OCS_INVALID_PRDT_ATTR: |
| 5250 | case OCS_MISMATCH_DATA_BUF_SIZE: |
| 5251 | case OCS_MISMATCH_RESP_UPIU_SIZE: |
| 5252 | case OCS_PEER_COMM_FAILURE: |
| 5253 | case OCS_FATAL_ERROR: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5254 | case OCS_DEVICE_FATAL_ERROR: |
| 5255 | case OCS_INVALID_CRYPTO_CONFIG: |
| 5256 | case OCS_GENERAL_CRYPTO_ERROR: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5257 | default: |
| 5258 | result |= DID_ERROR << 16; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 5259 | dev_err(hba->dev, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5260 | "OCS error from controller = %x for tag %d\n", |
| 5261 | ocs, lrbp->task_tag); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 5262 | /* |
| 5263 | * This is called in interrupt context, hence avoid sleep |
| 5264 | * while printing debug registers. Also print only the minimum |
| 5265 | * debug registers needed to debug OCS failure. |
| 5266 | */ |
| 5267 | __ufshcd_print_host_regs(hba, true); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5268 | ufshcd_print_host_state(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5269 | break; |
| 5270 | } /* end of switch */ |
| 5271 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5272 | if ((host_byte(result) != DID_OK) && !hba->silence_err_logs) { |
| 5273 | print_prdt = (ocs == OCS_INVALID_PRDT_ATTR || |
| 5274 | ocs == OCS_MISMATCH_DATA_BUF_SIZE); |
| 5275 | ufshcd_print_trs(hba, 1 << lrbp->task_tag, print_prdt); |
| 5276 | } |
| 5277 | |
| 5278 | if ((host_byte(result) == DID_ERROR) || |
| 5279 | (host_byte(result) == DID_ABORT)) |
| 5280 | ufsdbg_set_err_state(hba); |
| 5281 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5282 | return result; |
| 5283 | } |
| 5284 | |
| 5285 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5286 | * ufshcd_uic_cmd_compl - handle completion of uic command |
| 5287 | * @hba: per adapter instance |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 5288 | * @intr_status: interrupt status generated by the controller |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 5289 | * |
| 5290 | * Returns |
| 5291 | * IRQ_HANDLED - If interrupt is valid |
| 5292 | * IRQ_NONE - If invalid interrupt |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5293 | */ |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 5294 | static irqreturn_t ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5295 | { |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 5296 | irqreturn_t retval = IRQ_NONE; |
| 5297 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 5298 | if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5299 | hba->active_uic_cmd->argument2 |= |
| 5300 | ufshcd_get_uic_cmd_result(hba); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 5301 | hba->active_uic_cmd->argument3 = |
| 5302 | ufshcd_get_dme_attr_val(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5303 | complete(&hba->active_uic_cmd->done); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 5304 | retval = IRQ_HANDLED; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5305 | } |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 5306 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 5307 | if (intr_status & UFSHCD_UIC_PWR_MASK) { |
| 5308 | if (hba->uic_async_done) { |
| 5309 | complete(hba->uic_async_done); |
| 5310 | retval = IRQ_HANDLED; |
| 5311 | } else if (ufshcd_is_auto_hibern8_supported(hba)) { |
| 5312 | /* |
| 5313 | * If uic_async_done flag is not set then this |
| 5314 | * is an Auto hibern8 err interrupt. |
| 5315 | * Perform a host reset followed by a full |
| 5316 | * link recovery. |
| 5317 | */ |
| 5318 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 5319 | hba->force_host_reset = true; |
| 5320 | dev_err(hba->dev, "%s: Auto Hibern8 %s failed - status: 0x%08x, upmcrs: 0x%08x\n", |
| 5321 | __func__, (intr_status & UIC_HIBERNATE_ENTER) ? |
| 5322 | "Enter" : "Exit", |
| 5323 | intr_status, ufshcd_get_upmcrs(hba)); |
| 5324 | __ufshcd_print_host_regs(hba, true); |
| 5325 | ufshcd_print_host_state(hba); |
| 5326 | schedule_work(&hba->eh_work); |
| 5327 | retval = IRQ_HANDLED; |
| 5328 | } |
| 5329 | } |
| 5330 | return retval; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5331 | } |
| 5332 | |
| 5333 | /** |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5334 | * ufshcd_abort_outstanding_requests - abort all outstanding transfer requests. |
| 5335 | * @hba: per adapter instance |
| 5336 | * @result: error result to inform scsi layer about |
| 5337 | */ |
| 5338 | void ufshcd_abort_outstanding_transfer_requests(struct ufs_hba *hba, int result) |
| 5339 | { |
| 5340 | u8 index; |
| 5341 | struct ufshcd_lrb *lrbp; |
| 5342 | struct scsi_cmnd *cmd; |
| 5343 | |
| 5344 | if (!hba->outstanding_reqs) |
| 5345 | return; |
| 5346 | |
| 5347 | for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) { |
| 5348 | lrbp = &hba->lrb[index]; |
| 5349 | cmd = lrbp->cmd; |
| 5350 | if (cmd) { |
| 5351 | ufshcd_cond_add_cmd_trace(hba, index, "failed"); |
| 5352 | ufshcd_update_error_stats(hba, |
| 5353 | UFS_ERR_INT_FATAL_ERRORS); |
| 5354 | scsi_dma_unmap(cmd); |
| 5355 | cmd->result = result; |
| 5356 | /* Clear pending transfer requests */ |
| 5357 | ufshcd_clear_cmd(hba, index); |
| 5358 | ufshcd_outstanding_req_clear(hba, index); |
| 5359 | clear_bit_unlock(index, &hba->lrb_in_use); |
| 5360 | lrbp->complete_time_stamp = ktime_get(); |
| 5361 | update_req_stats(hba, lrbp); |
| 5362 | /* Mark completed command as NULL in LRB */ |
| 5363 | lrbp->cmd = NULL; |
| 5364 | ufshcd_release_all(hba); |
| 5365 | if (cmd->request) { |
| 5366 | /* |
| 5367 | * As we are accessing the "request" structure, |
| 5368 | * this must be called before calling |
| 5369 | * ->scsi_done() callback. |
| 5370 | */ |
| 5371 | ufshcd_vops_pm_qos_req_end(hba, cmd->request, |
| 5372 | true); |
| 5373 | ufshcd_vops_crypto_engine_cfg_end(hba, |
| 5374 | lrbp, cmd->request); |
| 5375 | } |
| 5376 | /* Do not touch lrbp after scsi done */ |
| 5377 | cmd->scsi_done(cmd); |
| 5378 | } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE) { |
| 5379 | if (hba->dev_cmd.complete) { |
| 5380 | ufshcd_cond_add_cmd_trace(hba, index, |
| 5381 | "dev_failed"); |
| 5382 | ufshcd_outstanding_req_clear(hba, index); |
| 5383 | complete(hba->dev_cmd.complete); |
| 5384 | } |
| 5385 | } |
| 5386 | if (ufshcd_is_clkscaling_supported(hba)) |
| 5387 | hba->clk_scaling.active_reqs--; |
| 5388 | } |
| 5389 | } |
| 5390 | |
| 5391 | /** |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5392 | * __ufshcd_transfer_req_compl - handle SCSI and query command completion |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5393 | * @hba: per adapter instance |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5394 | * @completed_reqs: requests to complete |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5395 | */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5396 | static void __ufshcd_transfer_req_compl(struct ufs_hba *hba, |
| 5397 | unsigned long completed_reqs) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5398 | { |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 5399 | struct ufshcd_lrb *lrbp; |
| 5400 | struct scsi_cmnd *cmd; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5401 | int result; |
| 5402 | int index; |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 5403 | struct request *req; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5404 | |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5405 | for_each_set_bit(index, &completed_reqs, hba->nutrs) { |
| 5406 | lrbp = &hba->lrb[index]; |
| 5407 | cmd = lrbp->cmd; |
| 5408 | if (cmd) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5409 | ufshcd_cond_add_cmd_trace(hba, index, "complete"); |
| 5410 | ufshcd_update_tag_stats_completion(hba, cmd); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5411 | result = ufshcd_transfer_rsp_status(hba, lrbp); |
| 5412 | scsi_dma_unmap(cmd); |
| 5413 | cmd->result = result; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5414 | clear_bit_unlock(index, &hba->lrb_in_use); |
| 5415 | lrbp->complete_time_stamp = ktime_get(); |
| 5416 | update_req_stats(hba, lrbp); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5417 | /* Mark completed command as NULL in LRB */ |
| 5418 | lrbp->cmd = NULL; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5419 | __ufshcd_release(hba, false); |
| 5420 | __ufshcd_hibern8_release(hba, false); |
| 5421 | if (cmd->request) { |
| 5422 | /* |
| 5423 | * As we are accessing the "request" structure, |
| 5424 | * this must be called before calling |
| 5425 | * ->scsi_done() callback. |
| 5426 | */ |
| 5427 | ufshcd_vops_pm_qos_req_end(hba, cmd->request, |
| 5428 | false); |
| 5429 | ufshcd_vops_crypto_engine_cfg_end(hba, |
| 5430 | lrbp, cmd->request); |
| 5431 | } |
| 5432 | |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 5433 | req = cmd->request; |
| 5434 | if (req) { |
| 5435 | /* Update IO svc time latency histogram */ |
| 5436 | if (req->lat_hist_enabled) { |
| 5437 | ktime_t completion; |
| 5438 | u_int64_t delta_us; |
| 5439 | |
| 5440 | completion = ktime_get(); |
| 5441 | delta_us = ktime_us_delta(completion, |
| 5442 | req->lat_hist_io_start); |
| 5443 | /* rq_data_dir() => true if WRITE */ |
| 5444 | blk_update_latency_hist(&hba->io_lat_s, |
| 5445 | (rq_data_dir(req) == READ), |
| 5446 | delta_us); |
| 5447 | } |
| 5448 | } |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5449 | /* Do not touch lrbp after scsi done */ |
| 5450 | cmd->scsi_done(cmd); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5451 | } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE) { |
| 5452 | if (hba->dev_cmd.complete) { |
| 5453 | ufshcd_cond_add_cmd_trace(hba, index, |
| 5454 | "dev_complete"); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5455 | complete(hba->dev_cmd.complete); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5456 | } |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5457 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5458 | if (ufshcd_is_clkscaling_supported(hba)) |
| 5459 | hba->clk_scaling.active_reqs--; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5460 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5461 | |
| 5462 | /* clear corresponding bits of completed commands */ |
| 5463 | hba->outstanding_reqs ^= completed_reqs; |
| 5464 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 5465 | ufshcd_clk_scaling_update_busy(hba); |
| 5466 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 5467 | /* we might have free'd some tags above */ |
| 5468 | wake_up(&hba->dev_cmd.tag_wq); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5469 | } |
| 5470 | |
| 5471 | /** |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5472 | * ufshcd_transfer_req_compl - handle SCSI and query command completion |
| 5473 | * @hba: per adapter instance |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 5474 | * |
| 5475 | * Returns |
| 5476 | * IRQ_HANDLED - If interrupt is valid |
| 5477 | * IRQ_NONE - If invalid interrupt |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5478 | */ |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 5479 | static irqreturn_t ufshcd_transfer_req_compl(struct ufs_hba *hba) |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5480 | { |
| 5481 | unsigned long completed_reqs; |
| 5482 | u32 tr_doorbell; |
| 5483 | |
| 5484 | /* Resetting interrupt aggregation counters first and reading the |
| 5485 | * DOOR_BELL afterward allows us to handle all the completed requests. |
| 5486 | * In order to prevent other interrupts starvation the DB is read once |
| 5487 | * after reset. The down side of this solution is the possibility of |
| 5488 | * false interrupt if device completes another request after resetting |
| 5489 | * aggregation and before reading the DB. |
| 5490 | */ |
| 5491 | if (ufshcd_is_intr_aggr_allowed(hba)) |
| 5492 | ufshcd_reset_intr_aggr(hba); |
| 5493 | |
| 5494 | tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 5495 | completed_reqs = tr_doorbell ^ hba->outstanding_reqs; |
| 5496 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 5497 | if (completed_reqs) { |
| 5498 | __ufshcd_transfer_req_compl(hba, completed_reqs); |
| 5499 | return IRQ_HANDLED; |
| 5500 | } else { |
| 5501 | return IRQ_NONE; |
| 5502 | } |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5503 | } |
| 5504 | |
| 5505 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5506 | * ufshcd_disable_ee - disable exception event |
| 5507 | * @hba: per-adapter instance |
| 5508 | * @mask: exception event to disable |
| 5509 | * |
| 5510 | * Disables exception event in the device so that the EVENT_ALERT |
| 5511 | * bit is not set. |
| 5512 | * |
| 5513 | * Returns zero on success, non-zero error value on failure. |
| 5514 | */ |
| 5515 | static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask) |
| 5516 | { |
| 5517 | int err = 0; |
| 5518 | u32 val; |
| 5519 | |
| 5520 | if (!(hba->ee_ctrl_mask & mask)) |
| 5521 | goto out; |
| 5522 | |
| 5523 | val = hba->ee_ctrl_mask & ~mask; |
| 5524 | val &= 0xFFFF; /* 2 bytes */ |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 5525 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5526 | QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val); |
| 5527 | if (!err) |
| 5528 | hba->ee_ctrl_mask &= ~mask; |
| 5529 | out: |
| 5530 | return err; |
| 5531 | } |
| 5532 | |
| 5533 | /** |
| 5534 | * ufshcd_enable_ee - enable exception event |
| 5535 | * @hba: per-adapter instance |
| 5536 | * @mask: exception event to enable |
| 5537 | * |
| 5538 | * Enable corresponding exception event in the device to allow |
| 5539 | * device to alert host in critical scenarios. |
| 5540 | * |
| 5541 | * Returns zero on success, non-zero error value on failure. |
| 5542 | */ |
| 5543 | static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask) |
| 5544 | { |
| 5545 | int err = 0; |
| 5546 | u32 val; |
| 5547 | |
| 5548 | if (hba->ee_ctrl_mask & mask) |
| 5549 | goto out; |
| 5550 | |
| 5551 | val = hba->ee_ctrl_mask | mask; |
| 5552 | val &= 0xFFFF; /* 2 bytes */ |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 5553 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5554 | QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val); |
| 5555 | if (!err) |
| 5556 | hba->ee_ctrl_mask |= mask; |
| 5557 | out: |
| 5558 | return err; |
| 5559 | } |
| 5560 | |
| 5561 | /** |
| 5562 | * ufshcd_enable_auto_bkops - Allow device managed BKOPS |
| 5563 | * @hba: per-adapter instance |
| 5564 | * |
| 5565 | * Allow device to manage background operations on its own. Enabling |
| 5566 | * this might lead to inconsistent latencies during normal data transfers |
| 5567 | * as the device is allowed to manage its own way of handling background |
| 5568 | * operations. |
| 5569 | * |
| 5570 | * Returns zero on success, non-zero on failure. |
| 5571 | */ |
| 5572 | static int ufshcd_enable_auto_bkops(struct ufs_hba *hba) |
| 5573 | { |
| 5574 | int err = 0; |
| 5575 | |
| 5576 | if (hba->auto_bkops_enabled) |
| 5577 | goto out; |
| 5578 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 5579 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5580 | QUERY_FLAG_IDN_BKOPS_EN, NULL); |
| 5581 | if (err) { |
| 5582 | dev_err(hba->dev, "%s: failed to enable bkops %d\n", |
| 5583 | __func__, err); |
| 5584 | goto out; |
| 5585 | } |
| 5586 | |
| 5587 | hba->auto_bkops_enabled = true; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5588 | trace_ufshcd_auto_bkops_state(dev_name(hba->dev), 1); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5589 | |
| 5590 | /* No need of URGENT_BKOPS exception from the device */ |
| 5591 | err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 5592 | if (err) |
| 5593 | dev_err(hba->dev, "%s: failed to disable exception event %d\n", |
| 5594 | __func__, err); |
| 5595 | out: |
| 5596 | return err; |
| 5597 | } |
| 5598 | |
| 5599 | /** |
| 5600 | * ufshcd_disable_auto_bkops - block device in doing background operations |
| 5601 | * @hba: per-adapter instance |
| 5602 | * |
| 5603 | * Disabling background operations improves command response latency but |
| 5604 | * has drawback of device moving into critical state where the device is |
| 5605 | * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the |
| 5606 | * host is idle so that BKOPS are managed effectively without any negative |
| 5607 | * impacts. |
| 5608 | * |
| 5609 | * Returns zero on success, non-zero on failure. |
| 5610 | */ |
| 5611 | static int ufshcd_disable_auto_bkops(struct ufs_hba *hba) |
| 5612 | { |
| 5613 | int err = 0; |
| 5614 | |
| 5615 | if (!hba->auto_bkops_enabled) |
| 5616 | goto out; |
| 5617 | |
| 5618 | /* |
| 5619 | * If host assisted BKOPs is to be enabled, make sure |
| 5620 | * urgent bkops exception is allowed. |
| 5621 | */ |
| 5622 | err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 5623 | if (err) { |
| 5624 | dev_err(hba->dev, "%s: failed to enable exception event %d\n", |
| 5625 | __func__, err); |
| 5626 | goto out; |
| 5627 | } |
| 5628 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 5629 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5630 | QUERY_FLAG_IDN_BKOPS_EN, NULL); |
| 5631 | if (err) { |
| 5632 | dev_err(hba->dev, "%s: failed to disable bkops %d\n", |
| 5633 | __func__, err); |
| 5634 | ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 5635 | goto out; |
| 5636 | } |
| 5637 | |
| 5638 | hba->auto_bkops_enabled = false; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5639 | trace_ufshcd_auto_bkops_state(dev_name(hba->dev), 0); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5640 | out: |
| 5641 | return err; |
| 5642 | } |
| 5643 | |
| 5644 | /** |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5645 | * ufshcd_force_reset_auto_bkops - force reset auto bkops state |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5646 | * @hba: per adapter instance |
| 5647 | * |
| 5648 | * After a device reset the device may toggle the BKOPS_EN flag |
| 5649 | * to default value. The s/w tracking variables should be updated |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5650 | * as well. This function would change the auto-bkops state based on |
| 5651 | * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5652 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5653 | static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5654 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5655 | if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) { |
| 5656 | hba->auto_bkops_enabled = false; |
| 5657 | hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS; |
| 5658 | ufshcd_enable_auto_bkops(hba); |
| 5659 | } else { |
| 5660 | hba->auto_bkops_enabled = true; |
| 5661 | hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS; |
| 5662 | ufshcd_disable_auto_bkops(hba); |
| 5663 | } |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5664 | } |
| 5665 | |
| 5666 | static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status) |
| 5667 | { |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 5668 | return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5669 | QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status); |
| 5670 | } |
| 5671 | |
| 5672 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5673 | * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status |
| 5674 | * @hba: per-adapter instance |
| 5675 | * @status: bkops_status value |
| 5676 | * |
| 5677 | * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn |
| 5678 | * flag in the device to permit background operations if the device |
| 5679 | * bkops_status is greater than or equal to "status" argument passed to |
| 5680 | * this function, disable otherwise. |
| 5681 | * |
| 5682 | * Returns 0 for success, non-zero in case of failure. |
| 5683 | * |
| 5684 | * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag |
| 5685 | * to know whether auto bkops is enabled or disabled after this function |
| 5686 | * returns control to it. |
| 5687 | */ |
| 5688 | static int ufshcd_bkops_ctrl(struct ufs_hba *hba, |
| 5689 | enum bkops_status status) |
| 5690 | { |
| 5691 | int err; |
| 5692 | u32 curr_status = 0; |
| 5693 | |
| 5694 | err = ufshcd_get_bkops_status(hba, &curr_status); |
| 5695 | if (err) { |
| 5696 | dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", |
| 5697 | __func__, err); |
| 5698 | goto out; |
| 5699 | } else if (curr_status > BKOPS_STATUS_MAX) { |
| 5700 | dev_err(hba->dev, "%s: invalid BKOPS status %d\n", |
| 5701 | __func__, curr_status); |
| 5702 | err = -EINVAL; |
| 5703 | goto out; |
| 5704 | } |
| 5705 | |
| 5706 | if (curr_status >= status) |
| 5707 | err = ufshcd_enable_auto_bkops(hba); |
| 5708 | else |
| 5709 | err = ufshcd_disable_auto_bkops(hba); |
| 5710 | out: |
| 5711 | return err; |
| 5712 | } |
| 5713 | |
| 5714 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5715 | * ufshcd_urgent_bkops - handle urgent bkops exception event |
| 5716 | * @hba: per-adapter instance |
| 5717 | * |
| 5718 | * Enable fBackgroundOpsEn flag in the device to permit background |
| 5719 | * operations. |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5720 | * |
| 5721 | * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled |
| 5722 | * and negative error value for any other failure. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5723 | */ |
| 5724 | static int ufshcd_urgent_bkops(struct ufs_hba *hba) |
| 5725 | { |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 5726 | return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5727 | } |
| 5728 | |
| 5729 | static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status) |
| 5730 | { |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 5731 | return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5732 | QUERY_ATTR_IDN_EE_STATUS, 0, 0, status); |
| 5733 | } |
| 5734 | |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 5735 | static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba) |
| 5736 | { |
| 5737 | int err; |
| 5738 | u32 curr_status = 0; |
| 5739 | |
| 5740 | if (hba->is_urgent_bkops_lvl_checked) |
| 5741 | goto enable_auto_bkops; |
| 5742 | |
| 5743 | err = ufshcd_get_bkops_status(hba, &curr_status); |
| 5744 | if (err) { |
| 5745 | dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", |
| 5746 | __func__, err); |
| 5747 | goto out; |
| 5748 | } |
| 5749 | |
| 5750 | /* |
| 5751 | * We are seeing that some devices are raising the urgent bkops |
| 5752 | * exception events even when BKOPS status doesn't indicate performace |
| 5753 | * impacted or critical. Handle these device by determining their urgent |
| 5754 | * bkops status at runtime. |
| 5755 | */ |
| 5756 | if (curr_status < BKOPS_STATUS_PERF_IMPACT) { |
| 5757 | dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n", |
| 5758 | __func__, curr_status); |
| 5759 | /* update the current status as the urgent bkops level */ |
| 5760 | hba->urgent_bkops_lvl = curr_status; |
| 5761 | hba->is_urgent_bkops_lvl_checked = true; |
| 5762 | } |
| 5763 | |
| 5764 | enable_auto_bkops: |
| 5765 | err = ufshcd_enable_auto_bkops(hba); |
| 5766 | out: |
| 5767 | if (err < 0) |
| 5768 | dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n", |
| 5769 | __func__, err); |
| 5770 | } |
| 5771 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5772 | /** |
| 5773 | * ufshcd_exception_event_handler - handle exceptions raised by device |
| 5774 | * @work: pointer to work data |
| 5775 | * |
| 5776 | * Read bExceptionEventStatus attribute from the device and handle the |
| 5777 | * exception event accordingly. |
| 5778 | */ |
| 5779 | static void ufshcd_exception_event_handler(struct work_struct *work) |
| 5780 | { |
| 5781 | struct ufs_hba *hba; |
| 5782 | int err; |
| 5783 | u32 status = 0; |
| 5784 | hba = container_of(work, struct ufs_hba, eeh_work); |
| 5785 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 5786 | pm_runtime_get_sync(hba->dev); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5787 | ufshcd_scsi_block_requests(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5788 | err = ufshcd_get_ee_status(hba, &status); |
| 5789 | if (err) { |
| 5790 | dev_err(hba->dev, "%s: failed to get exception status %d\n", |
| 5791 | __func__, err); |
| 5792 | goto out; |
| 5793 | } |
| 5794 | |
| 5795 | status &= hba->ee_ctrl_mask; |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 5796 | |
| 5797 | if (status & MASK_EE_URGENT_BKOPS) |
| 5798 | ufshcd_bkops_exception_event_handler(hba); |
| 5799 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5800 | out: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5801 | ufshcd_scsi_unblock_requests(hba); |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 5802 | pm_runtime_put_sync(hba->dev); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5803 | return; |
| 5804 | } |
| 5805 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5806 | /* Complete requests that have door-bell cleared */ |
| 5807 | static void ufshcd_complete_requests(struct ufs_hba *hba) |
| 5808 | { |
| 5809 | ufshcd_transfer_req_compl(hba); |
| 5810 | ufshcd_tmc_handler(hba); |
| 5811 | } |
| 5812 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5813 | /** |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5814 | * ufshcd_quirk_dl_nac_errors - This function checks if error handling is |
| 5815 | * to recover from the DL NAC errors or not. |
| 5816 | * @hba: per-adapter instance |
| 5817 | * |
| 5818 | * Returns true if error handling is required, false otherwise |
| 5819 | */ |
| 5820 | static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba) |
| 5821 | { |
| 5822 | unsigned long flags; |
| 5823 | bool err_handling = true; |
| 5824 | |
| 5825 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5826 | /* |
| 5827 | * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the |
| 5828 | * device fatal error and/or DL NAC & REPLAY timeout errors. |
| 5829 | */ |
| 5830 | if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR)) |
| 5831 | goto out; |
| 5832 | |
| 5833 | if ((hba->saved_err & DEVICE_FATAL_ERROR) || |
| 5834 | ((hba->saved_err & UIC_ERROR) && |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5835 | (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR))) { |
| 5836 | /* |
| 5837 | * we have to do error recovery but atleast silence the error |
| 5838 | * logs. |
| 5839 | */ |
| 5840 | hba->silence_err_logs = true; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5841 | goto out; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5842 | } |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5843 | |
| 5844 | if ((hba->saved_err & UIC_ERROR) && |
| 5845 | (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) { |
| 5846 | int err; |
| 5847 | /* |
| 5848 | * wait for 50ms to see if we can get any other errors or not. |
| 5849 | */ |
| 5850 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5851 | msleep(50); |
| 5852 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5853 | |
| 5854 | /* |
| 5855 | * now check if we have got any other severe errors other than |
| 5856 | * DL NAC error? |
| 5857 | */ |
| 5858 | if ((hba->saved_err & INT_FATAL_ERRORS) || |
| 5859 | ((hba->saved_err & UIC_ERROR) && |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5860 | (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR))) { |
| 5861 | if (((hba->saved_err & INT_FATAL_ERRORS) == |
| 5862 | DEVICE_FATAL_ERROR) || (hba->saved_uic_err & |
| 5863 | ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) |
| 5864 | hba->silence_err_logs = true; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5865 | goto out; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5866 | } |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5867 | |
| 5868 | /* |
| 5869 | * As DL NAC is the only error received so far, send out NOP |
| 5870 | * command to confirm if link is still active or not. |
| 5871 | * - If we don't get any response then do error recovery. |
| 5872 | * - If we get response then clear the DL NAC error bit. |
| 5873 | */ |
| 5874 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5875 | /* silence the error logs from NOP command */ |
| 5876 | hba->silence_err_logs = true; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5877 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5878 | err = ufshcd_verify_dev_init(hba); |
| 5879 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5880 | hba->silence_err_logs = false; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5881 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5882 | if (err) { |
| 5883 | hba->silence_err_logs = true; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5884 | goto out; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5885 | } |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5886 | |
| 5887 | /* Link seems to be alive hence ignore the DL NAC errors */ |
| 5888 | if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR) |
| 5889 | hba->saved_err &= ~UIC_ERROR; |
| 5890 | /* clear NAC error */ |
| 5891 | hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR; |
| 5892 | if (!hba->saved_uic_err) { |
| 5893 | err_handling = false; |
| 5894 | goto out; |
| 5895 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5896 | /* |
| 5897 | * there seems to be some errors other than NAC, so do error |
| 5898 | * recovery |
| 5899 | */ |
| 5900 | hba->silence_err_logs = true; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5901 | } |
| 5902 | out: |
| 5903 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5904 | return err_handling; |
| 5905 | } |
| 5906 | |
| 5907 | /** |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5908 | * ufshcd_err_handler - handle UFS errors that require s/w attention |
| 5909 | * @work: pointer to work structure |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5910 | */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5911 | static void ufshcd_err_handler(struct work_struct *work) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5912 | { |
| 5913 | struct ufs_hba *hba; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5914 | unsigned long flags; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5915 | bool err_xfer = false, err_tm = false; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5916 | int err = 0; |
| 5917 | int tag; |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5918 | bool needs_reset = false; |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 5919 | bool clks_enabled = false; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5920 | |
| 5921 | hba = container_of(work, struct ufs_hba, eh_work); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5922 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5923 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 5924 | ufsdbg_set_err_state(hba); |
| 5925 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5926 | if (hba->ufshcd_state == UFSHCD_STATE_RESET) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5927 | goto out; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5928 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 5929 | /* |
| 5930 | * Make sure the clocks are ON before we proceed with err |
| 5931 | * handling. For the majority of cases err handler would be |
| 5932 | * run with clocks ON. There is a possibility that the err |
| 5933 | * handler was scheduled due to auto hibern8 error interrupt, |
| 5934 | * in which case the clocks could be gated or be in the |
| 5935 | * process of gating when the err handler runs. |
| 5936 | */ |
| 5937 | if (unlikely((hba->clk_gating.state != CLKS_ON) && |
| 5938 | ufshcd_is_auto_hibern8_supported(hba))) { |
| 5939 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5940 | ufshcd_hold(hba, false); |
| 5941 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5942 | clks_enabled = true; |
| 5943 | } |
| 5944 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5945 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 5946 | ufshcd_set_eh_in_progress(hba); |
| 5947 | |
| 5948 | /* Complete requests that have door-bell cleared by h/w */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5949 | ufshcd_complete_requests(hba); |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5950 | |
Subhash Jadavani | 4f0df17b | 2016-12-16 13:19:27 -0800 | [diff] [blame] | 5951 | if (hba->dev_info.quirks & |
| 5952 | UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) { |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5953 | bool ret; |
| 5954 | |
| 5955 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5956 | /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */ |
| 5957 | ret = ufshcd_quirk_dl_nac_errors(hba); |
| 5958 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5959 | if (!ret) |
| 5960 | goto skip_err_handling; |
| 5961 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5962 | |
| 5963 | /* |
| 5964 | * Dump controller state before resetting. Transfer requests state |
| 5965 | * will be dump as part of the request completion. |
| 5966 | */ |
| 5967 | if (hba->saved_err & (INT_FATAL_ERRORS | UIC_ERROR)) { |
| 5968 | dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x", |
| 5969 | __func__, hba->saved_err, hba->saved_uic_err); |
| 5970 | if (!hba->silence_err_logs) { |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 5971 | /* release lock as print host regs sleeps */ |
| 5972 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5973 | ufshcd_print_host_regs(hba); |
| 5974 | ufshcd_print_host_state(hba); |
| 5975 | ufshcd_print_pwr_info(hba); |
| 5976 | ufshcd_print_tmrs(hba, hba->outstanding_tasks); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 5977 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5978 | } |
| 5979 | } |
| 5980 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 5981 | if ((hba->saved_err & INT_FATAL_ERRORS) |
| 5982 | || hba->saved_ce_err || hba->force_host_reset || |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5983 | ((hba->saved_err & UIC_ERROR) && |
| 5984 | (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR | |
| 5985 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR | |
| 5986 | UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))) |
| 5987 | needs_reset = true; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5988 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5989 | /* |
| 5990 | * if host reset is required then skip clearing the pending |
| 5991 | * transfers forcefully because they will automatically get |
| 5992 | * cleared after link startup. |
| 5993 | */ |
| 5994 | if (needs_reset) |
| 5995 | goto skip_pending_xfer_clear; |
| 5996 | |
| 5997 | /* release lock as clear command might sleep */ |
| 5998 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5999 | /* Clear pending transfer requests */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 6000 | for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) { |
| 6001 | if (ufshcd_clear_cmd(hba, tag)) { |
| 6002 | err_xfer = true; |
| 6003 | goto lock_skip_pending_xfer_clear; |
| 6004 | } |
| 6005 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6006 | |
| 6007 | /* Clear pending task management requests */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 6008 | for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) { |
| 6009 | if (ufshcd_clear_tm_cmd(hba, tag)) { |
| 6010 | err_tm = true; |
| 6011 | goto lock_skip_pending_xfer_clear; |
| 6012 | } |
| 6013 | } |
| 6014 | |
| 6015 | lock_skip_pending_xfer_clear: |
| 6016 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6017 | |
| 6018 | /* Complete the requests that are cleared by s/w */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 6019 | ufshcd_complete_requests(hba); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6020 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 6021 | if (err_xfer || err_tm) |
| 6022 | needs_reset = true; |
| 6023 | |
| 6024 | skip_pending_xfer_clear: |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6025 | /* Fatal errors need reset */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 6026 | if (needs_reset) { |
| 6027 | unsigned long max_doorbells = (1UL << hba->nutrs) - 1; |
| 6028 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6029 | if (hba->saved_err & INT_FATAL_ERRORS) |
| 6030 | ufshcd_update_error_stats(hba, |
| 6031 | UFS_ERR_INT_FATAL_ERRORS); |
| 6032 | if (hba->saved_ce_err) |
| 6033 | ufshcd_update_error_stats(hba, UFS_ERR_CRYPTO_ENGINE); |
| 6034 | |
| 6035 | if (hba->saved_err & UIC_ERROR) |
| 6036 | ufshcd_update_error_stats(hba, |
| 6037 | UFS_ERR_INT_UIC_ERROR); |
| 6038 | |
| 6039 | if (err_xfer || err_tm) |
| 6040 | ufshcd_update_error_stats(hba, |
| 6041 | UFS_ERR_CLEAR_PEND_XFER_TM); |
| 6042 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 6043 | /* |
| 6044 | * ufshcd_reset_and_restore() does the link reinitialization |
| 6045 | * which will need atleast one empty doorbell slot to send the |
| 6046 | * device management commands (NOP and query commands). |
| 6047 | * If there is no slot empty at this moment then free up last |
| 6048 | * slot forcefully. |
| 6049 | */ |
| 6050 | if (hba->outstanding_reqs == max_doorbells) |
| 6051 | __ufshcd_transfer_req_compl(hba, |
| 6052 | (1UL << (hba->nutrs - 1))); |
| 6053 | |
| 6054 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6055 | err = ufshcd_reset_and_restore(hba); |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 6056 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6057 | if (err) { |
| 6058 | dev_err(hba->dev, "%s: reset and restore failed\n", |
| 6059 | __func__); |
| 6060 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 6061 | } |
| 6062 | /* |
| 6063 | * Inform scsi mid-layer that we did reset and allow to handle |
| 6064 | * Unit Attention properly. |
| 6065 | */ |
| 6066 | scsi_report_bus_reset(hba->host, 0); |
| 6067 | hba->saved_err = 0; |
| 6068 | hba->saved_uic_err = 0; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6069 | hba->saved_ce_err = 0; |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6070 | hba->force_host_reset = false; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6071 | } |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 6072 | |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 6073 | skip_err_handling: |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 6074 | if (!needs_reset) { |
| 6075 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
| 6076 | if (hba->saved_err || hba->saved_uic_err) |
| 6077 | dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x", |
| 6078 | __func__, hba->saved_err, hba->saved_uic_err); |
| 6079 | } |
| 6080 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6081 | hba->silence_err_logs = false; |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6082 | |
| 6083 | if (clks_enabled) |
| 6084 | __ufshcd_release(hba, false); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6085 | out: |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6086 | ufshcd_clear_eh_in_progress(hba); |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 6087 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6088 | } |
| 6089 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6090 | static void ufshcd_update_uic_reg_hist(struct ufs_uic_err_reg_hist *reg_hist, |
| 6091 | u32 reg) |
| 6092 | { |
| 6093 | reg_hist->reg[reg_hist->pos] = reg; |
| 6094 | reg_hist->tstamp[reg_hist->pos] = ktime_get(); |
| 6095 | reg_hist->pos = (reg_hist->pos + 1) % UIC_ERR_REG_HIST_LENGTH; |
| 6096 | } |
| 6097 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6098 | /** |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6099 | * ufshcd_update_uic_error - check and set fatal UIC error flags. |
| 6100 | * @hba: per-adapter instance |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6101 | * |
| 6102 | * Returns |
| 6103 | * IRQ_HANDLED - If interrupt is valid |
| 6104 | * IRQ_NONE - If invalid interrupt |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6105 | */ |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6106 | static irqreturn_t ufshcd_update_uic_error(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6107 | { |
| 6108 | u32 reg; |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6109 | irqreturn_t retval = IRQ_NONE; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6110 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6111 | /* PHY layer lane error */ |
| 6112 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6113 | if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) && |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6114 | (reg & UIC_PHY_ADAPTER_LAYER_ERROR_CODE_MASK)) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6115 | /* |
| 6116 | * To know whether this error is fatal or not, DB timeout |
| 6117 | * must be checked but this error is handled separately. |
| 6118 | */ |
| 6119 | dev_dbg(hba->dev, "%s: UIC Lane error reported, reg 0x%x\n", |
| 6120 | __func__, reg); |
| 6121 | ufshcd_update_uic_reg_hist(&hba->ufs_stats.pa_err, reg); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6122 | |
| 6123 | /* |
| 6124 | * Don't ignore LINERESET indication during hibern8 |
| 6125 | * enter operation. |
| 6126 | */ |
| 6127 | if (reg & UIC_PHY_ADAPTER_LAYER_GENERIC_ERROR) { |
| 6128 | struct uic_command *cmd = hba->active_uic_cmd; |
| 6129 | |
| 6130 | if (cmd) { |
| 6131 | if (cmd->command == UIC_CMD_DME_HIBER_ENTER) { |
| 6132 | dev_err(hba->dev, "%s: LINERESET during hibern8 enter, reg 0x%x\n", |
| 6133 | __func__, reg); |
| 6134 | hba->full_init_linereset = true; |
| 6135 | } |
| 6136 | } |
| 6137 | } |
| 6138 | retval |= IRQ_HANDLED; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6139 | } |
| 6140 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6141 | /* PA_INIT_ERROR is fatal and needs UIC reset */ |
| 6142 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6143 | if ((reg & UIC_DATA_LINK_LAYER_ERROR) && |
| 6144 | (reg & UIC_DATA_LINK_LAYER_ERROR_CODE_MASK)) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6145 | ufshcd_update_uic_reg_hist(&hba->ufs_stats.dl_err, reg); |
| 6146 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6147 | if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT) { |
| 6148 | hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR; |
| 6149 | } else if (hba->dev_info.quirks & |
| 6150 | UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) { |
| 6151 | if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED) |
| 6152 | hba->uic_error |= |
| 6153 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR; |
| 6154 | else if (reg & |
| 6155 | UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT) |
| 6156 | hba->uic_error |= |
| 6157 | UFSHCD_UIC_DL_TCx_REPLAY_ERROR; |
| 6158 | } |
| 6159 | retval |= IRQ_HANDLED; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 6160 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6161 | |
| 6162 | /* UIC NL/TL/DME errors needs software retry */ |
| 6163 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6164 | if ((reg & UIC_NETWORK_LAYER_ERROR) && |
| 6165 | (reg & UIC_NETWORK_LAYER_ERROR_CODE_MASK)) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6166 | ufshcd_update_uic_reg_hist(&hba->ufs_stats.nl_err, reg); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6167 | hba->uic_error |= UFSHCD_UIC_NL_ERROR; |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6168 | retval |= IRQ_HANDLED; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6169 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6170 | |
| 6171 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6172 | if ((reg & UIC_TRANSPORT_LAYER_ERROR) && |
| 6173 | (reg & UIC_TRANSPORT_LAYER_ERROR_CODE_MASK)) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6174 | ufshcd_update_uic_reg_hist(&hba->ufs_stats.tl_err, reg); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6175 | hba->uic_error |= UFSHCD_UIC_TL_ERROR; |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6176 | retval |= IRQ_HANDLED; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6177 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6178 | |
| 6179 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6180 | if ((reg & UIC_DME_ERROR) && |
| 6181 | (reg & UIC_DME_ERROR_CODE_MASK)) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6182 | ufshcd_update_uic_reg_hist(&hba->ufs_stats.dme_err, reg); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6183 | hba->uic_error |= UFSHCD_UIC_DME_ERROR; |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6184 | retval |= IRQ_HANDLED; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6185 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6186 | |
| 6187 | dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n", |
| 6188 | __func__, hba->uic_error); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6189 | return retval; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6190 | } |
| 6191 | |
| 6192 | /** |
| 6193 | * ufshcd_check_errors - Check for errors that need s/w attention |
| 6194 | * @hba: per-adapter instance |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6195 | * |
| 6196 | * Returns |
| 6197 | * IRQ_HANDLED - If interrupt is valid |
| 6198 | * IRQ_NONE - If invalid interrupt |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6199 | */ |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6200 | static irqreturn_t ufshcd_check_errors(struct ufs_hba *hba) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6201 | { |
| 6202 | bool queue_eh_work = false; |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6203 | irqreturn_t retval = IRQ_NONE; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6204 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6205 | if (hba->errors & INT_FATAL_ERRORS || hba->ce_error) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6206 | queue_eh_work = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6207 | |
| 6208 | if (hba->errors & UIC_ERROR) { |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6209 | hba->uic_error = 0; |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6210 | retval = ufshcd_update_uic_error(hba); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6211 | if (hba->uic_error) |
| 6212 | queue_eh_work = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6213 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6214 | |
| 6215 | if (queue_eh_work) { |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 6216 | /* |
| 6217 | * update the transfer error masks to sticky bits, let's do this |
| 6218 | * irrespective of current ufshcd_state. |
| 6219 | */ |
| 6220 | hba->saved_err |= hba->errors; |
| 6221 | hba->saved_uic_err |= hba->uic_error; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6222 | hba->saved_ce_err |= hba->ce_error; |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 6223 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6224 | /* handle fatal errors only when link is functional */ |
| 6225 | if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) { |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6226 | /* |
| 6227 | * Set error handling in progress flag early so that we |
| 6228 | * don't issue new requests any more. |
| 6229 | */ |
| 6230 | ufshcd_set_eh_in_progress(hba); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6231 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6232 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 6233 | schedule_work(&hba->eh_work); |
| 6234 | } |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6235 | retval |= IRQ_HANDLED; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6236 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6237 | /* |
| 6238 | * if (!queue_eh_work) - |
| 6239 | * Other errors are either non-fatal where host recovers |
| 6240 | * itself without s/w intervention or errors that will be |
| 6241 | * handled by the SCSI core layer. |
| 6242 | */ |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6243 | return retval; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6244 | } |
| 6245 | |
| 6246 | /** |
| 6247 | * ufshcd_tmc_handler - handle task management function completion |
| 6248 | * @hba: per adapter instance |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6249 | * |
| 6250 | * Returns |
| 6251 | * IRQ_HANDLED - If interrupt is valid |
| 6252 | * IRQ_NONE - If invalid interrupt |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6253 | */ |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6254 | static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6255 | { |
| 6256 | u32 tm_doorbell; |
| 6257 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 6258 | tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6259 | hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks; |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6260 | if (hba->tm_condition) { |
| 6261 | wake_up(&hba->tm_wq); |
| 6262 | return IRQ_HANDLED; |
| 6263 | } else { |
| 6264 | return IRQ_NONE; |
| 6265 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6266 | } |
| 6267 | |
| 6268 | /** |
| 6269 | * ufshcd_sl_intr - Interrupt service routine |
| 6270 | * @hba: per adapter instance |
| 6271 | * @intr_status: contains interrupts generated by the controller |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6272 | * |
| 6273 | * Returns |
| 6274 | * IRQ_HANDLED - If interrupt is valid |
| 6275 | * IRQ_NONE - If invalid interrupt |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6276 | */ |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6277 | static irqreturn_t ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6278 | { |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6279 | irqreturn_t retval = IRQ_NONE; |
| 6280 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6281 | ufsdbg_error_inject_dispatcher(hba, |
| 6282 | ERR_INJECT_INTR, intr_status, &intr_status); |
| 6283 | |
| 6284 | ufshcd_vops_crypto_engine_get_status(hba, &hba->ce_error); |
| 6285 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6286 | hba->errors = UFSHCD_ERROR_MASK & intr_status; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6287 | if (hba->errors || hba->ce_error) |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6288 | retval |= ufshcd_check_errors(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6289 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 6290 | if (intr_status & UFSHCD_UIC_MASK) |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6291 | retval |= ufshcd_uic_cmd_compl(hba, intr_status); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6292 | |
| 6293 | if (intr_status & UTP_TASK_REQ_COMPL) |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6294 | retval |= ufshcd_tmc_handler(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6295 | |
| 6296 | if (intr_status & UTP_TRANSFER_REQ_COMPL) |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6297 | retval |= ufshcd_transfer_req_compl(hba); |
| 6298 | |
| 6299 | return retval; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6300 | } |
| 6301 | |
| 6302 | /** |
| 6303 | * ufshcd_intr - Main interrupt service routine |
| 6304 | * @irq: irq number |
| 6305 | * @__hba: pointer to adapter instance |
| 6306 | * |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6307 | * Returns |
| 6308 | * IRQ_HANDLED - If interrupt is valid |
| 6309 | * IRQ_NONE - If invalid interrupt |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6310 | */ |
| 6311 | static irqreturn_t ufshcd_intr(int irq, void *__hba) |
| 6312 | { |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 6313 | u32 intr_status, enabled_intr_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6314 | irqreturn_t retval = IRQ_NONE; |
| 6315 | struct ufs_hba *hba = __hba; |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6316 | int retries = hba->nutrs; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6317 | |
| 6318 | spin_lock(hba->host->host_lock); |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 6319 | intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6320 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6321 | /* |
| 6322 | * There could be max of hba->nutrs reqs in flight and in worst case |
| 6323 | * if the reqs get finished 1 by 1 after the interrupt status is |
| 6324 | * read, make sure we handle them by checking the interrupt status |
| 6325 | * again in a loop until we process all of the reqs before returning. |
| 6326 | */ |
| 6327 | do { |
| 6328 | enabled_intr_status = |
| 6329 | intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
| 6330 | if (intr_status) |
| 6331 | ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS); |
| 6332 | if (enabled_intr_status) |
| 6333 | retval |= ufshcd_sl_intr(hba, enabled_intr_status); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 6334 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6335 | intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS); |
| 6336 | } while (intr_status && --retries); |
| 6337 | |
| 6338 | if (retval == IRQ_NONE) { |
| 6339 | dev_err(hba->dev, "%s: Unhandled interrupt 0x%08x\n", |
| 6340 | __func__, intr_status); |
| 6341 | ufshcd_hex_dump(hba, "host regs: ", hba->mmio_base, |
| 6342 | UFSHCI_REG_SPACE_SIZE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6343 | } |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6344 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6345 | spin_unlock(hba->host->host_lock); |
| 6346 | return retval; |
| 6347 | } |
| 6348 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6349 | static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag) |
| 6350 | { |
| 6351 | int err = 0; |
| 6352 | u32 mask = 1 << tag; |
| 6353 | unsigned long flags; |
| 6354 | |
| 6355 | if (!test_bit(tag, &hba->outstanding_tasks)) |
| 6356 | goto out; |
| 6357 | |
| 6358 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 6359 | ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR); |
| 6360 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6361 | |
| 6362 | /* poll for max. 1 sec to clear door bell register by h/w */ |
| 6363 | err = ufshcd_wait_for_register(hba, |
| 6364 | REG_UTP_TASK_REQ_DOOR_BELL, |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 6365 | mask, 0, 1000, 1000, true); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6366 | out: |
| 6367 | return err; |
| 6368 | } |
| 6369 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6370 | /** |
| 6371 | * ufshcd_issue_tm_cmd - issues task management commands to controller |
| 6372 | * @hba: per adapter instance |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6373 | * @lun_id: LUN ID to which TM command is sent |
| 6374 | * @task_id: task ID to which the TM command is applicable |
| 6375 | * @tm_function: task management function opcode |
| 6376 | * @tm_response: task management service response return value |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6377 | * |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6378 | * Returns non-zero value on error, zero on success. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6379 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6380 | static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id, |
| 6381 | u8 tm_function, u8 *tm_response) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6382 | { |
| 6383 | struct utp_task_req_desc *task_req_descp; |
| 6384 | struct utp_upiu_task_req *task_req_upiup; |
| 6385 | struct Scsi_Host *host; |
| 6386 | unsigned long flags; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6387 | int free_slot; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6388 | int err; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6389 | int task_tag; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6390 | |
| 6391 | host = hba->host; |
| 6392 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6393 | /* |
| 6394 | * Get free slot, sleep if slots are unavailable. |
| 6395 | * Even though we use wait_event() which sleeps indefinitely, |
| 6396 | * the maximum wait time is bounded by %TM_CMD_TIMEOUT. |
| 6397 | */ |
| 6398 | 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] | 6399 | ufshcd_hold_all(hba); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6400 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6401 | spin_lock_irqsave(host->host_lock, flags); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6402 | task_req_descp = hba->utmrdl_base_addr; |
| 6403 | task_req_descp += free_slot; |
| 6404 | |
| 6405 | /* Configure task request descriptor */ |
| 6406 | task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD); |
| 6407 | task_req_descp->header.dword_2 = |
| 6408 | cpu_to_le32(OCS_INVALID_COMMAND_STATUS); |
| 6409 | |
| 6410 | /* Configure task request UPIU */ |
| 6411 | task_req_upiup = |
| 6412 | (struct utp_upiu_task_req *) task_req_descp->task_req_upiu; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6413 | task_tag = hba->nutrs + free_slot; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6414 | task_req_upiup->header.dword_0 = |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 6415 | UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0, |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6416 | lun_id, task_tag); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6417 | task_req_upiup->header.dword_1 = |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 6418 | UPIU_HEADER_DWORD(0, tm_function, 0, 0); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 6419 | /* |
| 6420 | * The host shall provide the same value for LUN field in the basic |
| 6421 | * header and for Input Parameter. |
| 6422 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6423 | task_req_upiup->input_param1 = cpu_to_be32(lun_id); |
| 6424 | task_req_upiup->input_param2 = cpu_to_be32(task_id); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6425 | |
| 6426 | /* send command to the controller */ |
| 6427 | __set_bit(free_slot, &hba->outstanding_tasks); |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 6428 | |
| 6429 | /* Make sure descriptors are ready before ringing the task doorbell */ |
| 6430 | wmb(); |
| 6431 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 6432 | ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6433 | /* Make sure that doorbell is committed immediately */ |
| 6434 | wmb(); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6435 | |
| 6436 | spin_unlock_irqrestore(host->host_lock, flags); |
| 6437 | |
| 6438 | /* wait until the task management command is completed */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6439 | err = wait_event_timeout(hba->tm_wq, |
| 6440 | test_bit(free_slot, &hba->tm_condition), |
| 6441 | msecs_to_jiffies(TM_CMD_TIMEOUT)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6442 | if (!err) { |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6443 | dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n", |
| 6444 | __func__, tm_function); |
| 6445 | if (ufshcd_clear_tm_cmd(hba, free_slot)) |
| 6446 | dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n", |
| 6447 | __func__, free_slot); |
| 6448 | err = -ETIMEDOUT; |
| 6449 | } else { |
| 6450 | err = ufshcd_task_req_compl(hba, free_slot, tm_response); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6451 | } |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6452 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6453 | clear_bit(free_slot, &hba->tm_condition); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6454 | ufshcd_put_tm_slot(hba, free_slot); |
| 6455 | wake_up(&hba->tm_tag_wq); |
| 6456 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6457 | ufshcd_release_all(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6458 | return err; |
| 6459 | } |
| 6460 | |
| 6461 | /** |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6462 | * ufshcd_eh_device_reset_handler - device reset handler registered to |
| 6463 | * scsi layer. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6464 | * @cmd: SCSI command pointer |
| 6465 | * |
| 6466 | * Returns SUCCESS/FAILED |
| 6467 | */ |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6468 | static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6469 | { |
| 6470 | struct Scsi_Host *host; |
| 6471 | struct ufs_hba *hba; |
| 6472 | unsigned int tag; |
| 6473 | u32 pos; |
| 6474 | int err; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6475 | u8 resp = 0xF; |
| 6476 | struct ufshcd_lrb *lrbp; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6477 | unsigned long flags; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6478 | |
| 6479 | host = cmd->device->host; |
| 6480 | hba = shost_priv(host); |
| 6481 | tag = cmd->request->tag; |
| 6482 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6483 | lrbp = &hba->lrb[tag]; |
| 6484 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp); |
| 6485 | if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6486 | if (!err) |
| 6487 | err = resp; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6488 | goto out; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6489 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6490 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6491 | /* clear the commands that were pending for corresponding LUN */ |
| 6492 | for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) { |
| 6493 | if (hba->lrb[pos].lun == lrbp->lun) { |
| 6494 | err = ufshcd_clear_cmd(hba, pos); |
| 6495 | if (err) |
| 6496 | break; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6497 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6498 | } |
| 6499 | spin_lock_irqsave(host->host_lock, flags); |
| 6500 | ufshcd_transfer_req_compl(hba); |
| 6501 | spin_unlock_irqrestore(host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6502 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6503 | out: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6504 | hba->req_abort_count = 0; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6505 | if (!err) { |
| 6506 | err = SUCCESS; |
| 6507 | } else { |
| 6508 | dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); |
| 6509 | err = FAILED; |
| 6510 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6511 | return err; |
| 6512 | } |
| 6513 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6514 | static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap) |
| 6515 | { |
| 6516 | struct ufshcd_lrb *lrbp; |
| 6517 | int tag; |
| 6518 | |
| 6519 | for_each_set_bit(tag, &bitmap, hba->nutrs) { |
| 6520 | lrbp = &hba->lrb[tag]; |
| 6521 | lrbp->req_abort_skip = true; |
| 6522 | } |
| 6523 | } |
| 6524 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6525 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6526 | * ufshcd_abort - abort a specific command |
| 6527 | * @cmd: SCSI command pointer |
| 6528 | * |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6529 | * Abort the pending command in device by sending UFS_ABORT_TASK task management |
| 6530 | * command, and in host controller by clearing the door-bell register. There can |
| 6531 | * be race between controller sending the command to the device while abort is |
| 6532 | * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is |
| 6533 | * really issued and then try to abort it. |
| 6534 | * |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6535 | * Returns SUCCESS/FAILED |
| 6536 | */ |
| 6537 | static int ufshcd_abort(struct scsi_cmnd *cmd) |
| 6538 | { |
| 6539 | struct Scsi_Host *host; |
| 6540 | struct ufs_hba *hba; |
| 6541 | unsigned long flags; |
| 6542 | unsigned int tag; |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6543 | int err = 0; |
| 6544 | int poll_cnt; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6545 | u8 resp = 0xF; |
| 6546 | struct ufshcd_lrb *lrbp; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 6547 | u32 reg; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6548 | |
| 6549 | host = cmd->device->host; |
| 6550 | hba = shost_priv(host); |
| 6551 | tag = cmd->request->tag; |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 6552 | if (!ufshcd_valid_tag(hba, tag)) { |
| 6553 | dev_err(hba->dev, |
| 6554 | "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p", |
| 6555 | __func__, tag, cmd, cmd->request); |
| 6556 | BUG(); |
| 6557 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6558 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6559 | lrbp = &hba->lrb[tag]; |
| 6560 | |
| 6561 | ufshcd_update_error_stats(hba, UFS_ERR_TASK_ABORT); |
| 6562 | |
| 6563 | /* |
| 6564 | * Task abort to the device W-LUN is illegal. When this command |
| 6565 | * will fail, due to spec violation, scsi err handling next step |
| 6566 | * will be to send LU reset which, again, is a spec violation. |
| 6567 | * To avoid these unnecessary/illegal step we skip to the last error |
| 6568 | * handling stage: reset and restore. |
| 6569 | */ |
| 6570 | if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN) |
| 6571 | return ufshcd_eh_host_reset_handler(cmd); |
| 6572 | |
| 6573 | ufshcd_hold_all(hba); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 6574 | reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 6575 | /* If command is already aborted/completed, return SUCCESS */ |
| 6576 | if (!(test_bit(tag, &hba->outstanding_reqs))) { |
| 6577 | dev_err(hba->dev, |
| 6578 | "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n", |
| 6579 | __func__, tag, hba->outstanding_reqs, reg); |
| 6580 | goto out; |
| 6581 | } |
| 6582 | |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 6583 | if (!(reg & (1 << tag))) { |
| 6584 | dev_err(hba->dev, |
| 6585 | "%s: cmd was completed, but without a notifying intr, tag = %d", |
| 6586 | __func__, tag); |
| 6587 | } |
| 6588 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6589 | /* Print Transfer Request of aborted task */ |
| 6590 | dev_err(hba->dev, "%s: Device abort task at tag %d", __func__, tag); |
| 6591 | |
| 6592 | /* |
| 6593 | * Print detailed info about aborted request. |
| 6594 | * As more than one request might get aborted at the same time, |
| 6595 | * print full information only for the first aborted request in order |
| 6596 | * to reduce repeated printouts. For other aborted requests only print |
| 6597 | * basic details. |
| 6598 | */ |
| 6599 | scsi_print_command(cmd); |
| 6600 | if (!hba->req_abort_count) { |
| 6601 | ufshcd_print_host_regs(hba); |
| 6602 | ufshcd_print_host_state(hba); |
| 6603 | ufshcd_print_pwr_info(hba); |
| 6604 | ufshcd_print_trs(hba, 1 << tag, true); |
| 6605 | } else { |
| 6606 | ufshcd_print_trs(hba, 1 << tag, false); |
| 6607 | } |
| 6608 | hba->req_abort_count++; |
| 6609 | |
| 6610 | |
| 6611 | /* Skip task abort in case previous aborts failed and report failure */ |
| 6612 | if (lrbp->req_abort_skip) { |
| 6613 | err = -EIO; |
| 6614 | goto out; |
| 6615 | } |
| 6616 | |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6617 | for (poll_cnt = 100; poll_cnt; poll_cnt--) { |
| 6618 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, |
| 6619 | UFS_QUERY_TASK, &resp); |
| 6620 | if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) { |
| 6621 | /* cmd pending in the device */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6622 | dev_err(hba->dev, "%s: cmd pending in the device. tag = %d", |
| 6623 | __func__, tag); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6624 | break; |
| 6625 | } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6626 | /* |
| 6627 | * cmd not pending in the device, check if it is |
| 6628 | * in transition. |
| 6629 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6630 | dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.", |
| 6631 | __func__, tag); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6632 | reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 6633 | if (reg & (1 << tag)) { |
| 6634 | /* sleep for max. 200us to stabilize */ |
| 6635 | usleep_range(100, 200); |
| 6636 | continue; |
| 6637 | } |
| 6638 | /* command completed already */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6639 | dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.", |
| 6640 | __func__, tag); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6641 | goto out; |
| 6642 | } else { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6643 | dev_err(hba->dev, |
| 6644 | "%s: no response from device. tag = %d, err %d", |
| 6645 | __func__, tag, err); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6646 | if (!err) |
| 6647 | err = resp; /* service response error */ |
| 6648 | goto out; |
| 6649 | } |
| 6650 | } |
| 6651 | |
| 6652 | if (!poll_cnt) { |
| 6653 | err = -EBUSY; |
| 6654 | goto out; |
| 6655 | } |
| 6656 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6657 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, |
| 6658 | UFS_ABORT_TASK, &resp); |
| 6659 | if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6660 | if (!err) { |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6661 | err = resp; /* service response error */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6662 | dev_err(hba->dev, "%s: issued. tag = %d, err %d", |
| 6663 | __func__, tag, err); |
| 6664 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6665 | goto out; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6666 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6667 | |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6668 | err = ufshcd_clear_cmd(hba, tag); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6669 | if (err) { |
| 6670 | dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d", |
| 6671 | __func__, tag, err); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6672 | goto out; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6673 | } |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6674 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6675 | scsi_dma_unmap(cmd); |
| 6676 | |
| 6677 | spin_lock_irqsave(host->host_lock, flags); |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 6678 | ufshcd_outstanding_req_clear(hba, tag); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6679 | hba->lrb[tag].cmd = NULL; |
| 6680 | spin_unlock_irqrestore(host->host_lock, flags); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 6681 | |
| 6682 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 6683 | wake_up(&hba->dev_cmd.tag_wq); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6684 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6685 | out: |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6686 | if (!err) { |
| 6687 | err = SUCCESS; |
| 6688 | } else { |
| 6689 | dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6690 | ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6691 | err = FAILED; |
| 6692 | } |
| 6693 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6694 | /* |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6695 | * This ufshcd_release_all() corresponds to the original scsi cmd that |
| 6696 | * got aborted here (as we won't get any IRQ for it). |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6697 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6698 | ufshcd_release_all(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6699 | return err; |
| 6700 | } |
| 6701 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6702 | /** |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6703 | * ufshcd_host_reset_and_restore - reset and restore host controller |
| 6704 | * @hba: per-adapter instance |
| 6705 | * |
| 6706 | * Note that host controller reset may issue DME_RESET to |
| 6707 | * local and remote (device) Uni-Pro stack and the attributes |
| 6708 | * are reset to default state. |
| 6709 | * |
| 6710 | * Returns zero on success, non-zero on failure |
| 6711 | */ |
| 6712 | static int ufshcd_host_reset_and_restore(struct ufs_hba *hba) |
| 6713 | { |
| 6714 | int err; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6715 | unsigned long flags; |
| 6716 | |
| 6717 | /* Reset the host controller */ |
| 6718 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 6719 | ufshcd_hba_stop(hba, false); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6720 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6721 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6722 | /* scale up clocks to max frequency before full reinitialization */ |
| 6723 | ufshcd_set_clk_freq(hba, true); |
| 6724 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6725 | err = ufshcd_hba_enable(hba); |
| 6726 | if (err) |
| 6727 | goto out; |
| 6728 | |
| 6729 | /* Establish the link again and restore the device */ |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6730 | err = ufshcd_probe_hba(hba); |
| 6731 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6732 | if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL)) { |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6733 | err = -EIO; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6734 | goto out; |
| 6735 | } |
| 6736 | |
| 6737 | if (!err) { |
| 6738 | err = ufshcd_vops_crypto_engine_reset(hba); |
| 6739 | if (err) { |
| 6740 | dev_err(hba->dev, |
| 6741 | "%s: failed to reset crypto engine %d\n", |
| 6742 | __func__, err); |
| 6743 | goto out; |
| 6744 | } |
| 6745 | } |
| 6746 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6747 | out: |
| 6748 | if (err) |
| 6749 | dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err); |
| 6750 | |
| 6751 | return err; |
| 6752 | } |
| 6753 | |
| 6754 | /** |
| 6755 | * ufshcd_reset_and_restore - reset and re-initialize host/device |
| 6756 | * @hba: per-adapter instance |
| 6757 | * |
| 6758 | * Reset and recover device, host and re-establish link. This |
| 6759 | * is helpful to recover the communication in fatal error conditions. |
| 6760 | * |
| 6761 | * Returns zero on success, non-zero on failure |
| 6762 | */ |
| 6763 | static int ufshcd_reset_and_restore(struct ufs_hba *hba) |
| 6764 | { |
| 6765 | int err = 0; |
| 6766 | unsigned long flags; |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6767 | int retries = MAX_HOST_RESET_RETRIES; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6768 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6769 | do { |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6770 | err = ufshcd_vops_full_reset(hba); |
| 6771 | if (err) |
| 6772 | dev_warn(hba->dev, "%s: full reset returned %d\n", |
| 6773 | __func__, err); |
| 6774 | |
| 6775 | err = ufshcd_reset_device(hba); |
| 6776 | if (err) |
| 6777 | dev_warn(hba->dev, "%s: device reset failed. err %d\n", |
| 6778 | __func__, err); |
| 6779 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6780 | err = ufshcd_host_reset_and_restore(hba); |
| 6781 | } while (err && --retries); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6782 | |
| 6783 | /* |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6784 | * There is no point proceeding even after failing |
| 6785 | * to recover after multiple retries. |
| 6786 | */ |
| 6787 | if (err) |
| 6788 | BUG(); |
| 6789 | /* |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6790 | * After reset the door-bell might be cleared, complete |
| 6791 | * outstanding requests in s/w here. |
| 6792 | */ |
| 6793 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 6794 | ufshcd_transfer_req_compl(hba); |
| 6795 | ufshcd_tmc_handler(hba); |
| 6796 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6797 | |
| 6798 | return err; |
| 6799 | } |
| 6800 | |
| 6801 | /** |
| 6802 | * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer |
| 6803 | * @cmd - SCSI command pointer |
| 6804 | * |
| 6805 | * Returns SUCCESS/FAILED |
| 6806 | */ |
| 6807 | static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd) |
| 6808 | { |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6809 | int err = SUCCESS; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6810 | unsigned long flags; |
| 6811 | struct ufs_hba *hba; |
| 6812 | |
| 6813 | hba = shost_priv(cmd->device->host); |
| 6814 | |
| 6815 | /* |
| 6816 | * Check if there is any race with fatal error handling. |
| 6817 | * If so, wait for it to complete. Even though fatal error |
| 6818 | * handling does reset and restore in some cases, don't assume |
| 6819 | * anything out of it. We are just avoiding race here. |
| 6820 | */ |
| 6821 | do { |
| 6822 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6823 | if (!(work_pending(&hba->eh_work) || |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6824 | hba->ufshcd_state == UFSHCD_STATE_RESET)) |
| 6825 | break; |
| 6826 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6827 | dev_err(hba->dev, "%s: reset in progress - 1\n", __func__); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6828 | flush_work(&hba->eh_work); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6829 | } while (1); |
| 6830 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6831 | /* |
| 6832 | * we don't know if previous reset had really reset the host controller |
| 6833 | * or not. So let's force reset here to be sure. |
| 6834 | */ |
| 6835 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 6836 | hba->force_host_reset = true; |
| 6837 | schedule_work(&hba->eh_work); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6838 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6839 | /* wait for the reset work to finish */ |
| 6840 | do { |
| 6841 | if (!(work_pending(&hba->eh_work) || |
| 6842 | hba->ufshcd_state == UFSHCD_STATE_RESET)) |
| 6843 | break; |
| 6844 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6845 | dev_err(hba->dev, "%s: reset in progress - 2\n", __func__); |
| 6846 | flush_work(&hba->eh_work); |
| 6847 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 6848 | } while (1); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6849 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6850 | if (!((hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) && |
| 6851 | ufshcd_is_link_active(hba))) { |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6852 | err = FAILED; |
| 6853 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 6854 | } |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 6855 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6856 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6857 | |
| 6858 | return err; |
| 6859 | } |
| 6860 | |
| 6861 | /** |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6862 | * ufshcd_get_max_icc_level - calculate the ICC level |
| 6863 | * @sup_curr_uA: max. current supported by the regulator |
| 6864 | * @start_scan: row at the desc table to start scan from |
| 6865 | * @buff: power descriptor buffer |
| 6866 | * |
| 6867 | * Returns calculated max ICC level for specific regulator |
| 6868 | */ |
| 6869 | static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff) |
| 6870 | { |
| 6871 | int i; |
| 6872 | int curr_uA; |
| 6873 | u16 data; |
| 6874 | u16 unit; |
| 6875 | |
| 6876 | for (i = start_scan; i >= 0; i--) { |
| 6877 | data = be16_to_cpu(*((u16 *)(buff + 2*i))); |
| 6878 | unit = (data & ATTR_ICC_LVL_UNIT_MASK) >> |
| 6879 | ATTR_ICC_LVL_UNIT_OFFSET; |
| 6880 | curr_uA = data & ATTR_ICC_LVL_VALUE_MASK; |
| 6881 | switch (unit) { |
| 6882 | case UFSHCD_NANO_AMP: |
| 6883 | curr_uA = curr_uA / 1000; |
| 6884 | break; |
| 6885 | case UFSHCD_MILI_AMP: |
| 6886 | curr_uA = curr_uA * 1000; |
| 6887 | break; |
| 6888 | case UFSHCD_AMP: |
| 6889 | curr_uA = curr_uA * 1000 * 1000; |
| 6890 | break; |
| 6891 | case UFSHCD_MICRO_AMP: |
| 6892 | default: |
| 6893 | break; |
| 6894 | } |
| 6895 | if (sup_curr_uA >= curr_uA) |
| 6896 | break; |
| 6897 | } |
| 6898 | if (i < 0) { |
| 6899 | i = 0; |
| 6900 | pr_err("%s: Couldn't find valid icc_level = %d", __func__, i); |
| 6901 | } |
| 6902 | |
| 6903 | return (u32)i; |
| 6904 | } |
| 6905 | |
| 6906 | /** |
Subhash Jadavani | a8d1ba3 | 2016-12-12 18:19:21 -0800 | [diff] [blame] | 6907 | * ufshcd_find_max_sup_active_icc_level - find the max ICC level |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6908 | * In case regulators are not initialized we'll return 0 |
| 6909 | * @hba: per-adapter instance |
| 6910 | * @desc_buf: power descriptor buffer to extract ICC levels from. |
| 6911 | * @len: length of desc_buff |
| 6912 | * |
Subhash Jadavani | a8d1ba3 | 2016-12-12 18:19:21 -0800 | [diff] [blame] | 6913 | * Returns calculated max ICC level |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6914 | */ |
| 6915 | static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba, |
| 6916 | u8 *desc_buf, int len) |
| 6917 | { |
| 6918 | u32 icc_level = 0; |
| 6919 | |
Subhash Jadavani | a8d1ba3 | 2016-12-12 18:19:21 -0800 | [diff] [blame] | 6920 | /* |
| 6921 | * VCCQ rail is optional for removable UFS card and also most of the |
| 6922 | * vendors don't use this rail for embedded UFS devices as well. So |
| 6923 | * it is normal that VCCQ rail may not be provided for given platform. |
| 6924 | */ |
| 6925 | if (!hba->vreg_info.vcc || !hba->vreg_info.vccq2) { |
| 6926 | dev_err(hba->dev, "%s: Regulator capability was not set, bActiveICCLevel=%d\n", |
| 6927 | __func__, icc_level); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6928 | goto out; |
| 6929 | } |
| 6930 | |
| 6931 | if (hba->vreg_info.vcc) |
| 6932 | icc_level = ufshcd_get_max_icc_level( |
| 6933 | hba->vreg_info.vcc->max_uA, |
| 6934 | POWER_DESC_MAX_ACTV_ICC_LVLS - 1, |
| 6935 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]); |
| 6936 | |
| 6937 | if (hba->vreg_info.vccq) |
| 6938 | icc_level = ufshcd_get_max_icc_level( |
| 6939 | hba->vreg_info.vccq->max_uA, |
| 6940 | icc_level, |
| 6941 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]); |
| 6942 | |
| 6943 | if (hba->vreg_info.vccq2) |
| 6944 | icc_level = ufshcd_get_max_icc_level( |
| 6945 | hba->vreg_info.vccq2->max_uA, |
| 6946 | icc_level, |
| 6947 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]); |
| 6948 | out: |
| 6949 | return icc_level; |
| 6950 | } |
| 6951 | |
Subhash Jadavani | 8a93dbd | 2016-12-12 17:59:44 -0800 | [diff] [blame] | 6952 | static void ufshcd_set_active_icc_lvl(struct ufs_hba *hba) |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6953 | { |
| 6954 | int ret; |
| 6955 | int buff_len = QUERY_DESC_POWER_MAX_SIZE; |
| 6956 | u8 desc_buf[QUERY_DESC_POWER_MAX_SIZE]; |
Subhash Jadavani | 35732e5 | 2016-12-09 16:09:42 -0800 | [diff] [blame] | 6957 | u32 icc_level; |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6958 | |
| 6959 | ret = ufshcd_read_power_desc(hba, desc_buf, buff_len); |
| 6960 | if (ret) { |
| 6961 | dev_err(hba->dev, |
| 6962 | "%s: Failed reading power descriptor.len = %d ret = %d", |
| 6963 | __func__, buff_len, ret); |
| 6964 | return; |
| 6965 | } |
| 6966 | |
Subhash Jadavani | 35732e5 | 2016-12-09 16:09:42 -0800 | [diff] [blame] | 6967 | icc_level = ufshcd_find_max_sup_active_icc_level(hba, desc_buf, |
| 6968 | buff_len); |
| 6969 | 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] | 6970 | |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 6971 | ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
Subhash Jadavani | 35732e5 | 2016-12-09 16:09:42 -0800 | [diff] [blame] | 6972 | QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, &icc_level); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6973 | |
| 6974 | if (ret) |
| 6975 | dev_err(hba->dev, |
| 6976 | "%s: Failed configuring bActiveICCLevel = %d ret = %d", |
Subhash Jadavani | 35732e5 | 2016-12-09 16:09:42 -0800 | [diff] [blame] | 6977 | __func__, icc_level, ret); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6978 | } |
| 6979 | |
| 6980 | /** |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6981 | * ufshcd_scsi_add_wlus - Adds required W-LUs |
| 6982 | * @hba: per-adapter instance |
| 6983 | * |
Subhash Jadavani | 2df121a | 2016-12-15 18:27:31 -0800 | [diff] [blame] | 6984 | * UFS devices can support upto 4 well known logical units: |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6985 | * "REPORT_LUNS" (address: 01h) |
| 6986 | * "UFS Device" (address: 50h) |
| 6987 | * "RPMB" (address: 44h) |
| 6988 | * "BOOT" (address: 30h) |
Subhash Jadavani | 2df121a | 2016-12-15 18:27:31 -0800 | [diff] [blame] | 6989 | * |
| 6990 | * "REPORT_LUNS" & "UFS Device" are mandatory for all device classes (see |
| 6991 | * "bDeviceSubClass" parameter of device descriptor) while "BOOT" is supported |
| 6992 | * only for bootable devices. "RPMB" is only supported with embedded devices. |
| 6993 | * |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6994 | * UFS device's power management needs to be controlled by "POWER CONDITION" |
| 6995 | * field of SSU (START STOP UNIT) command. But this "power condition" field |
| 6996 | * will take effect only when its sent to "UFS device" well known logical unit |
| 6997 | * hence we require the scsi_device instance to represent this logical unit in |
| 6998 | * order for the UFS host driver to send the SSU command for power management. |
| 6999 | |
| 7000 | * We also require the scsi_device instance for "RPMB" (Replay Protected Memory |
| 7001 | * Block) LU so user space process can control this LU. User space may also |
| 7002 | * want to have access to BOOT LU. |
| 7003 | |
Subhash Jadavani | 2df121a | 2016-12-15 18:27:31 -0800 | [diff] [blame] | 7004 | * This function tries to add scsi device instances for each of all well known |
| 7005 | * LUs (except "REPORT LUNS" LU) depending on device class. |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 7006 | * |
| 7007 | * Returns zero on success (all required W-LUs are added successfully), |
| 7008 | * non-zero error value on failure (if failed to add any of the required W-LU). |
| 7009 | */ |
| 7010 | static int ufshcd_scsi_add_wlus(struct ufs_hba *hba) |
| 7011 | { |
| 7012 | int ret = 0; |
Channagoud Kadabi | 075db3b | 2017-03-16 14:26:17 -0700 | [diff] [blame] | 7013 | struct scsi_device *sdev_rpmb = NULL; |
| 7014 | struct scsi_device *sdev_boot = NULL; |
Subhash Jadavani | 2df121a | 2016-12-15 18:27:31 -0800 | [diff] [blame] | 7015 | bool is_bootable_dev = false; |
| 7016 | bool is_embedded_dev = false; |
| 7017 | |
| 7018 | if ((hba->dev_info.b_device_sub_class == UFS_DEV_EMBEDDED_BOOTABLE) || |
| 7019 | (hba->dev_info.b_device_sub_class == UFS_DEV_REMOVABLE_BOOTABLE)) |
| 7020 | is_bootable_dev = true; |
| 7021 | |
| 7022 | if ((hba->dev_info.b_device_sub_class == UFS_DEV_EMBEDDED_BOOTABLE) || |
| 7023 | (hba->dev_info.b_device_sub_class == UFS_DEV_EMBEDDED_NON_BOOTABLE)) |
| 7024 | is_embedded_dev = true; |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 7025 | |
| 7026 | hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0, |
| 7027 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL); |
| 7028 | if (IS_ERR(hba->sdev_ufs_device)) { |
| 7029 | ret = PTR_ERR(hba->sdev_ufs_device); |
Subhash Jadavani | 2df121a | 2016-12-15 18:27:31 -0800 | [diff] [blame] | 7030 | dev_err(hba->dev, "%s: failed adding DEVICE_WLUN. ret %d\n", |
| 7031 | __func__, ret); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 7032 | hba->sdev_ufs_device = NULL; |
| 7033 | goto out; |
| 7034 | } |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 7035 | scsi_device_put(hba->sdev_ufs_device); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 7036 | |
Subhash Jadavani | 2df121a | 2016-12-15 18:27:31 -0800 | [diff] [blame] | 7037 | if (is_bootable_dev) { |
| 7038 | sdev_boot = __scsi_add_device(hba->host, 0, 0, |
| 7039 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), |
| 7040 | NULL); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 7041 | |
Subhash Jadavani | 2df121a | 2016-12-15 18:27:31 -0800 | [diff] [blame] | 7042 | if (IS_ERR(sdev_boot)) { |
| 7043 | dev_err(hba->dev, "%s: failed adding BOOT_WLUN. ret %d\n", |
| 7044 | __func__, ret); |
| 7045 | ret = PTR_ERR(sdev_boot); |
| 7046 | goto remove_sdev_ufs_device; |
| 7047 | } |
| 7048 | scsi_device_put(sdev_boot); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 7049 | } |
Subhash Jadavani | 2df121a | 2016-12-15 18:27:31 -0800 | [diff] [blame] | 7050 | |
| 7051 | if (is_embedded_dev) { |
| 7052 | sdev_rpmb = __scsi_add_device(hba->host, 0, 0, |
| 7053 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), |
| 7054 | NULL); |
| 7055 | if (IS_ERR(sdev_rpmb)) { |
| 7056 | dev_err(hba->dev, "%s: failed adding RPMB_WLUN. ret %d\n", |
| 7057 | __func__, ret); |
| 7058 | ret = PTR_ERR(sdev_rpmb); |
| 7059 | goto remove_sdev_boot; |
| 7060 | } |
| 7061 | scsi_device_put(sdev_rpmb); |
| 7062 | } |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 7063 | goto out; |
| 7064 | |
| 7065 | remove_sdev_boot: |
Subhash Jadavani | 2df121a | 2016-12-15 18:27:31 -0800 | [diff] [blame] | 7066 | if (is_bootable_dev) |
| 7067 | scsi_remove_device(sdev_boot); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 7068 | remove_sdev_ufs_device: |
| 7069 | scsi_remove_device(hba->sdev_ufs_device); |
| 7070 | out: |
| 7071 | return ret; |
| 7072 | } |
| 7073 | |
| 7074 | /** |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 7075 | * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro |
| 7076 | * @hba: per-adapter instance |
| 7077 | * |
| 7078 | * PA_TActivate parameter can be tuned manually if UniPro version is less than |
| 7079 | * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's |
| 7080 | * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce |
| 7081 | * the hibern8 exit latency. |
| 7082 | * |
| 7083 | * Returns zero on success, non-zero error value on failure. |
| 7084 | */ |
| 7085 | static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba) |
| 7086 | { |
| 7087 | int ret = 0; |
| 7088 | u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate; |
| 7089 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7090 | if (!ufshcd_is_unipro_pa_params_tuning_req(hba)) |
| 7091 | return 0; |
| 7092 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 7093 | ret = ufshcd_dme_peer_get(hba, |
| 7094 | UIC_ARG_MIB_SEL( |
| 7095 | RX_MIN_ACTIVATETIME_CAPABILITY, |
| 7096 | UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), |
| 7097 | &peer_rx_min_activatetime); |
| 7098 | if (ret) |
| 7099 | goto out; |
| 7100 | |
| 7101 | /* make sure proper unit conversion is applied */ |
| 7102 | tuned_pa_tactivate = |
| 7103 | ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US) |
| 7104 | / PA_TACTIVATE_TIME_UNIT_US); |
| 7105 | ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 7106 | tuned_pa_tactivate); |
| 7107 | |
| 7108 | out: |
| 7109 | return ret; |
| 7110 | } |
| 7111 | |
| 7112 | /** |
| 7113 | * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro |
| 7114 | * @hba: per-adapter instance |
| 7115 | * |
| 7116 | * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than |
| 7117 | * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's |
| 7118 | * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY. |
| 7119 | * This optimal value can help reduce the hibern8 exit latency. |
| 7120 | * |
| 7121 | * Returns zero on success, non-zero error value on failure. |
| 7122 | */ |
| 7123 | static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba) |
| 7124 | { |
| 7125 | int ret = 0; |
| 7126 | u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0; |
| 7127 | u32 max_hibern8_time, tuned_pa_hibern8time; |
| 7128 | |
| 7129 | ret = ufshcd_dme_get(hba, |
| 7130 | UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY, |
| 7131 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), |
| 7132 | &local_tx_hibern8_time_cap); |
| 7133 | if (ret) |
| 7134 | goto out; |
| 7135 | |
| 7136 | ret = ufshcd_dme_peer_get(hba, |
| 7137 | UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY, |
| 7138 | UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), |
| 7139 | &peer_rx_hibern8_time_cap); |
| 7140 | if (ret) |
| 7141 | goto out; |
| 7142 | |
| 7143 | max_hibern8_time = max(local_tx_hibern8_time_cap, |
| 7144 | peer_rx_hibern8_time_cap); |
| 7145 | /* make sure proper unit conversion is applied */ |
| 7146 | tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US) |
| 7147 | / PA_HIBERN8_TIME_UNIT_US); |
| 7148 | ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME), |
| 7149 | tuned_pa_hibern8time); |
| 7150 | out: |
| 7151 | return ret; |
| 7152 | } |
| 7153 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7154 | /** |
| 7155 | * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is |
| 7156 | * less than device PA_TACTIVATE time. |
| 7157 | * @hba: per-adapter instance |
| 7158 | * |
| 7159 | * Some UFS devices require host PA_TACTIVATE to be lower than device |
| 7160 | * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk |
| 7161 | * for such devices. |
| 7162 | * |
| 7163 | * Returns zero on success, non-zero error value on failure. |
| 7164 | */ |
| 7165 | static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba) |
| 7166 | { |
| 7167 | int ret = 0; |
| 7168 | u32 granularity, peer_granularity; |
| 7169 | u32 pa_tactivate, peer_pa_tactivate; |
| 7170 | u32 pa_tactivate_us, peer_pa_tactivate_us; |
| 7171 | u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100}; |
| 7172 | |
| 7173 | ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY), |
| 7174 | &granularity); |
| 7175 | if (ret) |
| 7176 | goto out; |
| 7177 | |
| 7178 | ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY), |
| 7179 | &peer_granularity); |
| 7180 | if (ret) |
| 7181 | goto out; |
| 7182 | |
| 7183 | if ((granularity < PA_GRANULARITY_MIN_VAL) || |
| 7184 | (granularity > PA_GRANULARITY_MAX_VAL)) { |
| 7185 | dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d", |
| 7186 | __func__, granularity); |
| 7187 | return -EINVAL; |
| 7188 | } |
| 7189 | |
| 7190 | if ((peer_granularity < PA_GRANULARITY_MIN_VAL) || |
| 7191 | (peer_granularity > PA_GRANULARITY_MAX_VAL)) { |
| 7192 | dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d", |
| 7193 | __func__, peer_granularity); |
| 7194 | return -EINVAL; |
| 7195 | } |
| 7196 | |
| 7197 | ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate); |
| 7198 | if (ret) |
| 7199 | goto out; |
| 7200 | |
| 7201 | ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 7202 | &peer_pa_tactivate); |
| 7203 | if (ret) |
| 7204 | goto out; |
| 7205 | |
| 7206 | pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1]; |
| 7207 | peer_pa_tactivate_us = peer_pa_tactivate * |
| 7208 | gran_to_us_table[peer_granularity - 1]; |
| 7209 | |
| 7210 | if (pa_tactivate_us > peer_pa_tactivate_us) { |
| 7211 | u32 new_peer_pa_tactivate; |
| 7212 | |
| 7213 | new_peer_pa_tactivate = pa_tactivate_us / |
| 7214 | gran_to_us_table[peer_granularity - 1]; |
| 7215 | new_peer_pa_tactivate++; |
| 7216 | ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 7217 | new_peer_pa_tactivate); |
| 7218 | } |
| 7219 | |
| 7220 | out: |
| 7221 | return ret; |
| 7222 | } |
| 7223 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 7224 | static void ufshcd_tune_unipro_params(struct ufs_hba *hba) |
| 7225 | { |
| 7226 | if (ufshcd_is_unipro_pa_params_tuning_req(hba)) { |
| 7227 | ufshcd_tune_pa_tactivate(hba); |
| 7228 | ufshcd_tune_pa_hibern8time(hba); |
| 7229 | } |
| 7230 | |
Subhash Jadavani | 4f0df17b | 2016-12-16 13:19:27 -0800 | [diff] [blame] | 7231 | if (hba->dev_info.quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE) |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 7232 | /* set 1ms timeout for PA_TACTIVATE */ |
| 7233 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7234 | |
Subhash Jadavani | 4f0df17b | 2016-12-16 13:19:27 -0800 | [diff] [blame] | 7235 | if (hba->dev_info.quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE) |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7236 | ufshcd_quirk_tune_host_pa_tactivate(hba); |
| 7237 | |
| 7238 | ufshcd_vops_apply_dev_quirks(hba); |
| 7239 | } |
| 7240 | |
| 7241 | static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba) |
| 7242 | { |
| 7243 | int err_reg_hist_size = sizeof(struct ufs_uic_err_reg_hist); |
| 7244 | |
| 7245 | hba->ufs_stats.hibern8_exit_cnt = 0; |
| 7246 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); |
| 7247 | |
| 7248 | memset(&hba->ufs_stats.pa_err, 0, err_reg_hist_size); |
| 7249 | memset(&hba->ufs_stats.dl_err, 0, err_reg_hist_size); |
| 7250 | memset(&hba->ufs_stats.nl_err, 0, err_reg_hist_size); |
| 7251 | memset(&hba->ufs_stats.tl_err, 0, err_reg_hist_size); |
| 7252 | memset(&hba->ufs_stats.dme_err, 0, err_reg_hist_size); |
| 7253 | |
| 7254 | hba->req_abort_count = 0; |
| 7255 | } |
| 7256 | |
| 7257 | static void ufshcd_apply_pm_quirks(struct ufs_hba *hba) |
| 7258 | { |
Subhash Jadavani | 4f0df17b | 2016-12-16 13:19:27 -0800 | [diff] [blame] | 7259 | if (hba->dev_info.quirks & UFS_DEVICE_QUIRK_NO_LINK_OFF) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7260 | if (ufs_get_pm_lvl_to_link_pwr_state(hba->rpm_lvl) == |
| 7261 | UIC_LINK_OFF_STATE) { |
| 7262 | hba->rpm_lvl = |
| 7263 | ufs_get_desired_pm_lvl_for_dev_link_state( |
| 7264 | UFS_SLEEP_PWR_MODE, |
| 7265 | UIC_LINK_HIBERN8_STATE); |
| 7266 | dev_info(hba->dev, "UFS_DEVICE_QUIRK_NO_LINK_OFF enabled, changed rpm_lvl to %d\n", |
| 7267 | hba->rpm_lvl); |
| 7268 | } |
| 7269 | if (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) == |
| 7270 | UIC_LINK_OFF_STATE) { |
| 7271 | hba->spm_lvl = |
| 7272 | ufs_get_desired_pm_lvl_for_dev_link_state( |
| 7273 | UFS_SLEEP_PWR_MODE, |
| 7274 | UIC_LINK_HIBERN8_STATE); |
| 7275 | dev_info(hba->dev, "UFS_DEVICE_QUIRK_NO_LINK_OFF enabled, changed spm_lvl to %d\n", |
| 7276 | hba->spm_lvl); |
| 7277 | } |
| 7278 | } |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 7279 | } |
| 7280 | |
| 7281 | /** |
Subhash Jadavani | 88f9999 | 2016-12-13 15:52:21 -0800 | [diff] [blame] | 7282 | * ufshcd_set_dev_ref_clk - set the device bRefClkFreq |
| 7283 | * @hba: per-adapter instance |
| 7284 | * |
| 7285 | * Read the current value of the bRefClkFreq attribute from device and update it |
| 7286 | * if host is supplying different reference clock frequency than one mentioned |
| 7287 | * in bRefClkFreq attribute. |
| 7288 | * |
| 7289 | * Returns zero on success, non-zero error value on failure. |
| 7290 | */ |
| 7291 | static int ufshcd_set_dev_ref_clk(struct ufs_hba *hba) |
| 7292 | { |
| 7293 | int err = 0; |
| 7294 | int ref_clk = -1; |
| 7295 | static const char * const ref_clk_freqs[] = {"19.2 MHz", "26 MHz", |
| 7296 | "38.4 MHz", "52 MHz"}; |
| 7297 | |
| 7298 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
| 7299 | QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &ref_clk); |
| 7300 | |
| 7301 | if (err) { |
| 7302 | dev_err(hba->dev, "%s: failed reading bRefClkFreq. err = %d\n", |
| 7303 | __func__, err); |
| 7304 | goto out; |
| 7305 | } |
| 7306 | |
| 7307 | if ((ref_clk < 0) || (ref_clk > REF_CLK_FREQ_52_MHZ)) { |
| 7308 | dev_err(hba->dev, "%s: invalid ref_clk setting = %d\n", |
| 7309 | __func__, ref_clk); |
| 7310 | err = -EINVAL; |
| 7311 | goto out; |
| 7312 | } |
| 7313 | |
| 7314 | if (ref_clk == hba->dev_ref_clk_freq) |
| 7315 | goto out; /* nothing to update */ |
| 7316 | |
| 7317 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
| 7318 | QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, |
| 7319 | &hba->dev_ref_clk_freq); |
| 7320 | |
| 7321 | if (err) |
| 7322 | dev_err(hba->dev, "%s: bRefClkFreq setting to %s failed\n", |
| 7323 | __func__, ref_clk_freqs[hba->dev_ref_clk_freq]); |
| 7324 | else |
| 7325 | /* |
| 7326 | * It is good to print this out here to debug any later failures |
| 7327 | * related to gear switch. |
| 7328 | */ |
| 7329 | dev_info(hba->dev, "%s: bRefClkFreq setting to %s succeeded\n", |
| 7330 | __func__, ref_clk_freqs[hba->dev_ref_clk_freq]); |
| 7331 | |
| 7332 | out: |
| 7333 | return err; |
| 7334 | } |
| 7335 | |
Subhash Jadavani | 344c16c | 2016-12-15 17:09:35 -0800 | [diff] [blame] | 7336 | static int ufs_read_device_desc_data(struct ufs_hba *hba) |
| 7337 | { |
| 7338 | int err; |
| 7339 | u8 desc_buf[QUERY_DESC_DEVICE_MAX_SIZE]; |
| 7340 | |
| 7341 | err = ufshcd_read_device_desc(hba, desc_buf, sizeof(desc_buf)); |
| 7342 | if (err) |
| 7343 | return err; |
| 7344 | |
| 7345 | /* |
| 7346 | * getting vendor (manufacturerID) and Bank Index in big endian |
| 7347 | * format |
| 7348 | */ |
| 7349 | hba->dev_info.w_manufacturer_id = |
| 7350 | desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 | |
| 7351 | desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1]; |
| 7352 | hba->dev_info.b_device_sub_class = |
| 7353 | desc_buf[DEVICE_DESC_PARAM_DEVICE_SUB_CLASS]; |
| 7354 | hba->dev_info.i_product_name = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME]; |
| 7355 | |
| 7356 | return 0; |
| 7357 | } |
| 7358 | |
Subhash Jadavani | 88f9999 | 2016-12-13 15:52:21 -0800 | [diff] [blame] | 7359 | /** |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7360 | * ufshcd_probe_hba - probe hba to detect device and initialize |
| 7361 | * @hba: per-adapter instance |
| 7362 | * |
| 7363 | * Execute link-startup and verify device initialization |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7364 | */ |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7365 | static int ufshcd_probe_hba(struct ufs_hba *hba) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7366 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7367 | int ret; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7368 | ktime_t start = ktime_get(); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7369 | |
| 7370 | ret = ufshcd_link_startup(hba); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 7371 | if (ret) |
| 7372 | goto out; |
| 7373 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7374 | /* Debug counters initialization */ |
| 7375 | ufshcd_clear_dbg_ufs_stats(hba); |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 7376 | /* set the default level for urgent bkops */ |
| 7377 | hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT; |
| 7378 | hba->is_urgent_bkops_lvl_checked = false; |
| 7379 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7380 | /* UniPro link is active now */ |
| 7381 | ufshcd_set_link_active(hba); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 7382 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 7383 | ret = ufshcd_verify_dev_init(hba); |
| 7384 | if (ret) |
| 7385 | goto out; |
| 7386 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 7387 | ret = ufshcd_complete_dev_init(hba); |
| 7388 | if (ret) |
| 7389 | goto out; |
| 7390 | |
Subhash Jadavani | 2df121a | 2016-12-15 18:27:31 -0800 | [diff] [blame] | 7391 | /* clear any previous UFS device information */ |
| 7392 | memset(&hba->dev_info, 0, sizeof(hba->dev_info)); |
| 7393 | |
Subhash Jadavani | 344c16c | 2016-12-15 17:09:35 -0800 | [diff] [blame] | 7394 | /* cache important parameters from device descriptor for later use */ |
| 7395 | ret = ufs_read_device_desc_data(hba); |
| 7396 | if (ret) |
| 7397 | goto out; |
| 7398 | |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 7399 | ufs_advertise_fixup_device(hba); |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 7400 | ufshcd_tune_unipro_params(hba); |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 7401 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7402 | ufshcd_apply_pm_quirks(hba); |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 7403 | ret = ufshcd_set_vccq_rail_unused(hba, |
Subhash Jadavani | 4f0df17b | 2016-12-16 13:19:27 -0800 | [diff] [blame] | 7404 | (hba->dev_info.quirks & UFS_DEVICE_NO_VCCQ) ? true : false); |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 7405 | if (ret) |
| 7406 | goto out; |
| 7407 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7408 | /* UFS device is also active now */ |
| 7409 | ufshcd_set_ufs_dev_active(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 7410 | ufshcd_force_reset_auto_bkops(hba); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 7411 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 7412 | if (ufshcd_get_max_pwr_mode(hba)) { |
| 7413 | dev_err(hba->dev, |
| 7414 | "%s: Failed getting max supported power mode\n", |
| 7415 | __func__); |
| 7416 | } else { |
Subhash Jadavani | 88f9999 | 2016-12-13 15:52:21 -0800 | [diff] [blame] | 7417 | /* |
| 7418 | * Set the right value to bRefClkFreq before attempting to |
| 7419 | * switch to HS gears. |
| 7420 | */ |
| 7421 | ufshcd_set_dev_ref_clk(hba); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 7422 | ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7423 | if (ret) { |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 7424 | dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n", |
| 7425 | __func__, ret); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7426 | goto out; |
| 7427 | } |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 7428 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7429 | |
Subhash Jadavani | 8a93dbd | 2016-12-12 17:59:44 -0800 | [diff] [blame] | 7430 | /* |
| 7431 | * bActiveICCLevel is volatile for UFS device (as per latest v2.1 spec) |
| 7432 | * and for removable UFS card as well, hence always set the parameter. |
| 7433 | * Note: Error handler may issue the device reset hence resetting |
| 7434 | * bActiveICCLevel as well so it is always safe to set this here. |
| 7435 | */ |
| 7436 | ufshcd_set_active_icc_lvl(hba); |
| 7437 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 7438 | /* set the state as operational after switching to desired gear */ |
| 7439 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7440 | /* |
| 7441 | * If we are in error handling context or in power management callbacks |
| 7442 | * context, no need to scan the host |
| 7443 | */ |
| 7444 | if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) { |
| 7445 | bool flag; |
| 7446 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 7447 | if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG, |
| 7448 | QUERY_FLAG_IDN_PWR_ON_WPE, &flag)) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7449 | hba->dev_info.f_power_on_wp_en = flag; |
| 7450 | |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 7451 | /* Add required well known logical units to scsi mid layer */ |
| 7452 | if (ufshcd_scsi_add_wlus(hba)) |
| 7453 | goto out; |
| 7454 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 7455 | /* Initialize devfreq after UFS device is detected */ |
| 7456 | if (ufshcd_is_clkscaling_supported(hba)) { |
| 7457 | memcpy(&hba->clk_scaling.saved_pwr_info.info, |
| 7458 | &hba->pwr_info, sizeof(struct ufs_pa_layer_attr)); |
| 7459 | hba->clk_scaling.saved_pwr_info.is_valid = true; |
| 7460 | hba->clk_scaling.is_scaled_up = true; |
| 7461 | if (!hba->devfreq) { |
| 7462 | hba->devfreq = devfreq_add_device(hba->dev, |
| 7463 | &ufs_devfreq_profile, |
| 7464 | "simple_ondemand", |
| 7465 | gov_data); |
| 7466 | if (IS_ERR(hba->devfreq)) { |
| 7467 | ret = PTR_ERR(hba->devfreq); |
| 7468 | dev_err(hba->dev, "Unable to register with devfreq %d\n", |
| 7469 | ret); |
| 7470 | goto out; |
| 7471 | } |
| 7472 | } |
| 7473 | hba->clk_scaling.is_allowed = true; |
| 7474 | } |
| 7475 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 7476 | scsi_scan_host(hba->host); |
| 7477 | pm_runtime_put_sync(hba->dev); |
| 7478 | } |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 7479 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 7480 | /* |
| 7481 | * Enable auto hibern8 if supported, after full host and |
| 7482 | * device initialization. |
| 7483 | */ |
| 7484 | if (ufshcd_is_auto_hibern8_supported(hba)) |
| 7485 | ufshcd_set_auto_hibern8_timer(hba, |
| 7486 | hba->hibern8_on_idle.delay_ms); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 7487 | out: |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7488 | /* |
| 7489 | * If we failed to initialize the device or the device is not |
| 7490 | * present, turn off the power/clocks etc. |
| 7491 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7492 | if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) { |
| 7493 | pm_runtime_put_sync(hba->dev); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7494 | ufshcd_hba_exit(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7495 | } |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7496 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7497 | trace_ufshcd_init(dev_name(hba->dev), ret, |
| 7498 | ktime_to_us(ktime_sub(ktime_get(), start)), |
| 7499 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7500 | return ret; |
| 7501 | } |
| 7502 | |
| 7503 | /** |
| 7504 | * ufshcd_async_scan - asynchronous execution for probing hba |
| 7505 | * @data: data pointer to pass to this function |
| 7506 | * @cookie: cookie data |
| 7507 | */ |
| 7508 | static void ufshcd_async_scan(void *data, async_cookie_t cookie) |
| 7509 | { |
| 7510 | struct ufs_hba *hba = (struct ufs_hba *)data; |
| 7511 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7512 | /* |
| 7513 | * Don't allow clock gating and hibern8 enter for faster device |
| 7514 | * detection. |
| 7515 | */ |
| 7516 | ufshcd_hold_all(hba); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7517 | ufshcd_probe_hba(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7518 | ufshcd_release_all(hba); |
| 7519 | } |
| 7520 | |
| 7521 | /** |
| 7522 | * ufshcd_query_ioctl - perform user read queries |
| 7523 | * @hba: per-adapter instance |
| 7524 | * @lun: used for lun specific queries |
| 7525 | * @buffer: user space buffer for reading and submitting query data and params |
| 7526 | * @return: 0 for success negative error code otherwise |
| 7527 | * |
| 7528 | * Expected/Submitted buffer structure is struct ufs_ioctl_query_data. |
| 7529 | * It will read the opcode, idn and buf_length parameters, and, put the |
| 7530 | * response in the buffer field while updating the used size in buf_length. |
| 7531 | */ |
| 7532 | static int ufshcd_query_ioctl(struct ufs_hba *hba, u8 lun, void __user *buffer) |
| 7533 | { |
| 7534 | struct ufs_ioctl_query_data *ioctl_data; |
| 7535 | int err = 0; |
| 7536 | int length = 0; |
| 7537 | void *data_ptr; |
| 7538 | bool flag; |
| 7539 | u32 att; |
| 7540 | u8 index; |
| 7541 | u8 *desc = NULL; |
| 7542 | |
| 7543 | ioctl_data = kzalloc(sizeof(struct ufs_ioctl_query_data), GFP_KERNEL); |
| 7544 | if (!ioctl_data) { |
| 7545 | dev_err(hba->dev, "%s: Failed allocating %zu bytes\n", __func__, |
| 7546 | sizeof(struct ufs_ioctl_query_data)); |
| 7547 | err = -ENOMEM; |
| 7548 | goto out; |
| 7549 | } |
| 7550 | |
| 7551 | /* extract params from user buffer */ |
| 7552 | err = copy_from_user(ioctl_data, buffer, |
| 7553 | sizeof(struct ufs_ioctl_query_data)); |
| 7554 | if (err) { |
| 7555 | dev_err(hba->dev, |
| 7556 | "%s: Failed copying buffer from user, err %d\n", |
| 7557 | __func__, err); |
| 7558 | goto out_release_mem; |
| 7559 | } |
| 7560 | |
| 7561 | /* verify legal parameters & send query */ |
| 7562 | switch (ioctl_data->opcode) { |
| 7563 | case UPIU_QUERY_OPCODE_READ_DESC: |
| 7564 | switch (ioctl_data->idn) { |
| 7565 | case QUERY_DESC_IDN_DEVICE: |
| 7566 | case QUERY_DESC_IDN_CONFIGURAION: |
| 7567 | case QUERY_DESC_IDN_INTERCONNECT: |
| 7568 | case QUERY_DESC_IDN_GEOMETRY: |
| 7569 | case QUERY_DESC_IDN_POWER: |
| 7570 | index = 0; |
| 7571 | break; |
| 7572 | case QUERY_DESC_IDN_UNIT: |
| 7573 | if (!ufs_is_valid_unit_desc_lun(lun)) { |
| 7574 | dev_err(hba->dev, |
| 7575 | "%s: No unit descriptor for lun 0x%x\n", |
| 7576 | __func__, lun); |
| 7577 | err = -EINVAL; |
| 7578 | goto out_release_mem; |
| 7579 | } |
| 7580 | index = lun; |
| 7581 | break; |
| 7582 | default: |
| 7583 | goto out_einval; |
| 7584 | } |
| 7585 | length = min_t(int, QUERY_DESC_MAX_SIZE, |
| 7586 | ioctl_data->buf_size); |
| 7587 | desc = kzalloc(length, GFP_KERNEL); |
| 7588 | if (!desc) { |
| 7589 | dev_err(hba->dev, "%s: Failed allocating %d bytes\n", |
| 7590 | __func__, length); |
| 7591 | err = -ENOMEM; |
| 7592 | goto out_release_mem; |
| 7593 | } |
| 7594 | err = ufshcd_query_descriptor(hba, ioctl_data->opcode, |
| 7595 | ioctl_data->idn, index, 0, desc, &length); |
| 7596 | break; |
| 7597 | case UPIU_QUERY_OPCODE_READ_ATTR: |
| 7598 | switch (ioctl_data->idn) { |
| 7599 | case QUERY_ATTR_IDN_BOOT_LU_EN: |
| 7600 | case QUERY_ATTR_IDN_POWER_MODE: |
| 7601 | case QUERY_ATTR_IDN_ACTIVE_ICC_LVL: |
| 7602 | case QUERY_ATTR_IDN_OOO_DATA_EN: |
| 7603 | case QUERY_ATTR_IDN_BKOPS_STATUS: |
| 7604 | case QUERY_ATTR_IDN_PURGE_STATUS: |
| 7605 | case QUERY_ATTR_IDN_MAX_DATA_IN: |
| 7606 | case QUERY_ATTR_IDN_MAX_DATA_OUT: |
| 7607 | case QUERY_ATTR_IDN_REF_CLK_FREQ: |
| 7608 | case QUERY_ATTR_IDN_CONF_DESC_LOCK: |
| 7609 | case QUERY_ATTR_IDN_MAX_NUM_OF_RTT: |
| 7610 | case QUERY_ATTR_IDN_EE_CONTROL: |
| 7611 | case QUERY_ATTR_IDN_EE_STATUS: |
| 7612 | case QUERY_ATTR_IDN_SECONDS_PASSED: |
| 7613 | index = 0; |
| 7614 | break; |
| 7615 | case QUERY_ATTR_IDN_DYN_CAP_NEEDED: |
| 7616 | case QUERY_ATTR_IDN_CORR_PRG_BLK_NUM: |
| 7617 | index = lun; |
| 7618 | break; |
| 7619 | default: |
| 7620 | goto out_einval; |
| 7621 | } |
| 7622 | err = ufshcd_query_attr(hba, ioctl_data->opcode, ioctl_data->idn, |
| 7623 | index, 0, &att); |
| 7624 | break; |
| 7625 | |
| 7626 | case UPIU_QUERY_OPCODE_WRITE_ATTR: |
| 7627 | err = copy_from_user(&att, |
| 7628 | buffer + sizeof(struct ufs_ioctl_query_data), |
| 7629 | sizeof(u32)); |
| 7630 | if (err) { |
| 7631 | dev_err(hba->dev, |
| 7632 | "%s: Failed copying buffer from user, err %d\n", |
| 7633 | __func__, err); |
| 7634 | goto out_release_mem; |
| 7635 | } |
| 7636 | |
| 7637 | switch (ioctl_data->idn) { |
| 7638 | case QUERY_ATTR_IDN_BOOT_LU_EN: |
| 7639 | index = 0; |
| 7640 | if (att > QUERY_ATTR_IDN_BOOT_LU_EN_MAX) { |
| 7641 | dev_err(hba->dev, |
| 7642 | "%s: Illegal ufs query ioctl data, opcode 0x%x, idn 0x%x, att 0x%x\n", |
| 7643 | __func__, ioctl_data->opcode, |
| 7644 | (unsigned int)ioctl_data->idn, att); |
| 7645 | err = -EINVAL; |
| 7646 | goto out_release_mem; |
| 7647 | } |
| 7648 | break; |
| 7649 | default: |
| 7650 | goto out_einval; |
| 7651 | } |
| 7652 | err = ufshcd_query_attr(hba, ioctl_data->opcode, |
| 7653 | ioctl_data->idn, index, 0, &att); |
| 7654 | break; |
| 7655 | |
| 7656 | case UPIU_QUERY_OPCODE_READ_FLAG: |
| 7657 | switch (ioctl_data->idn) { |
| 7658 | case QUERY_FLAG_IDN_FDEVICEINIT: |
| 7659 | case QUERY_FLAG_IDN_PERMANENT_WPE: |
| 7660 | case QUERY_FLAG_IDN_PWR_ON_WPE: |
| 7661 | case QUERY_FLAG_IDN_BKOPS_EN: |
| 7662 | case QUERY_FLAG_IDN_PURGE_ENABLE: |
| 7663 | case QUERY_FLAG_IDN_FPHYRESOURCEREMOVAL: |
| 7664 | case QUERY_FLAG_IDN_BUSY_RTC: |
| 7665 | break; |
| 7666 | default: |
| 7667 | goto out_einval; |
| 7668 | } |
| 7669 | err = ufshcd_query_flag_retry(hba, ioctl_data->opcode, |
| 7670 | ioctl_data->idn, &flag); |
| 7671 | break; |
| 7672 | default: |
| 7673 | goto out_einval; |
| 7674 | } |
| 7675 | |
| 7676 | if (err) { |
| 7677 | dev_err(hba->dev, "%s: Query for idn %d failed\n", __func__, |
| 7678 | ioctl_data->idn); |
| 7679 | goto out_release_mem; |
| 7680 | } |
| 7681 | |
| 7682 | /* |
| 7683 | * copy response data |
| 7684 | * As we might end up reading less data then what is specified in |
| 7685 | * "ioctl_data->buf_size". So we are updating "ioctl_data-> |
| 7686 | * buf_size" to what exactly we have read. |
| 7687 | */ |
| 7688 | switch (ioctl_data->opcode) { |
| 7689 | case UPIU_QUERY_OPCODE_READ_DESC: |
| 7690 | ioctl_data->buf_size = min_t(int, ioctl_data->buf_size, length); |
| 7691 | data_ptr = desc; |
| 7692 | break; |
| 7693 | case UPIU_QUERY_OPCODE_READ_ATTR: |
| 7694 | ioctl_data->buf_size = sizeof(u32); |
| 7695 | data_ptr = &att; |
| 7696 | break; |
| 7697 | case UPIU_QUERY_OPCODE_READ_FLAG: |
| 7698 | ioctl_data->buf_size = 1; |
| 7699 | data_ptr = &flag; |
| 7700 | break; |
| 7701 | case UPIU_QUERY_OPCODE_WRITE_ATTR: |
| 7702 | goto out_release_mem; |
| 7703 | default: |
| 7704 | goto out_einval; |
| 7705 | } |
| 7706 | |
| 7707 | /* copy to user */ |
| 7708 | err = copy_to_user(buffer, ioctl_data, |
| 7709 | sizeof(struct ufs_ioctl_query_data)); |
| 7710 | if (err) |
| 7711 | dev_err(hba->dev, "%s: Failed copying back to user.\n", |
| 7712 | __func__); |
| 7713 | err = copy_to_user(buffer + sizeof(struct ufs_ioctl_query_data), |
| 7714 | data_ptr, ioctl_data->buf_size); |
| 7715 | if (err) |
| 7716 | dev_err(hba->dev, "%s: err %d copying back to user.\n", |
| 7717 | __func__, err); |
| 7718 | goto out_release_mem; |
| 7719 | |
| 7720 | out_einval: |
| 7721 | dev_err(hba->dev, |
| 7722 | "%s: illegal ufs query ioctl data, opcode 0x%x, idn 0x%x\n", |
| 7723 | __func__, ioctl_data->opcode, (unsigned int)ioctl_data->idn); |
| 7724 | err = -EINVAL; |
| 7725 | out_release_mem: |
| 7726 | kfree(ioctl_data); |
| 7727 | kfree(desc); |
| 7728 | out: |
| 7729 | return err; |
| 7730 | } |
| 7731 | |
| 7732 | /** |
| 7733 | * ufshcd_ioctl - ufs ioctl callback registered in scsi_host |
| 7734 | * @dev: scsi device required for per LUN queries |
| 7735 | * @cmd: command opcode |
| 7736 | * @buffer: user space buffer for transferring data |
| 7737 | * |
| 7738 | * Supported commands: |
| 7739 | * UFS_IOCTL_QUERY |
| 7740 | */ |
| 7741 | static int ufshcd_ioctl(struct scsi_device *dev, int cmd, void __user *buffer) |
| 7742 | { |
| 7743 | struct ufs_hba *hba = shost_priv(dev->host); |
| 7744 | int err = 0; |
| 7745 | |
| 7746 | BUG_ON(!hba); |
| 7747 | if (!buffer) { |
| 7748 | dev_err(hba->dev, "%s: User buffer is NULL!\n", __func__); |
| 7749 | return -EINVAL; |
| 7750 | } |
| 7751 | |
| 7752 | switch (cmd) { |
| 7753 | case UFS_IOCTL_QUERY: |
| 7754 | pm_runtime_get_sync(hba->dev); |
| 7755 | err = ufshcd_query_ioctl(hba, ufshcd_scsi_to_upiu_lun(dev->lun), |
| 7756 | buffer); |
| 7757 | pm_runtime_put_sync(hba->dev); |
| 7758 | break; |
| 7759 | default: |
| 7760 | err = -ENOIOCTLCMD; |
| 7761 | dev_dbg(hba->dev, "%s: Unsupported ioctl cmd %d\n", __func__, |
| 7762 | cmd); |
| 7763 | break; |
| 7764 | } |
| 7765 | |
| 7766 | return err; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7767 | } |
| 7768 | |
Yaniv Gardi | f550c65 | 2016-03-10 17:37:07 +0200 | [diff] [blame] | 7769 | static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd) |
| 7770 | { |
| 7771 | unsigned long flags; |
| 7772 | struct Scsi_Host *host; |
| 7773 | struct ufs_hba *hba; |
| 7774 | int index; |
| 7775 | bool found = false; |
| 7776 | |
| 7777 | if (!scmd || !scmd->device || !scmd->device->host) |
| 7778 | return BLK_EH_NOT_HANDLED; |
| 7779 | |
| 7780 | host = scmd->device->host; |
| 7781 | hba = shost_priv(host); |
| 7782 | if (!hba) |
| 7783 | return BLK_EH_NOT_HANDLED; |
| 7784 | |
| 7785 | spin_lock_irqsave(host->host_lock, flags); |
| 7786 | |
| 7787 | for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) { |
| 7788 | if (hba->lrb[index].cmd == scmd) { |
| 7789 | found = true; |
| 7790 | break; |
| 7791 | } |
| 7792 | } |
| 7793 | |
| 7794 | spin_unlock_irqrestore(host->host_lock, flags); |
| 7795 | |
| 7796 | /* |
| 7797 | * Bypass SCSI error handling and reset the block layer timer if this |
| 7798 | * SCSI command was not actually dispatched to UFS driver, otherwise |
| 7799 | * let SCSI layer handle the error as usual. |
| 7800 | */ |
| 7801 | return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER; |
| 7802 | } |
| 7803 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7804 | static struct scsi_host_template ufshcd_driver_template = { |
| 7805 | .module = THIS_MODULE, |
| 7806 | .name = UFSHCD, |
| 7807 | .proc_name = UFSHCD, |
| 7808 | .queuecommand = ufshcd_queuecommand, |
| 7809 | .slave_alloc = ufshcd_slave_alloc, |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 7810 | .slave_configure = ufshcd_slave_configure, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7811 | .slave_destroy = ufshcd_slave_destroy, |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 7812 | .change_queue_depth = ufshcd_change_queue_depth, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7813 | .eh_abort_handler = ufshcd_abort, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 7814 | .eh_device_reset_handler = ufshcd_eh_device_reset_handler, |
| 7815 | .eh_host_reset_handler = ufshcd_eh_host_reset_handler, |
Yaniv Gardi | f550c65 | 2016-03-10 17:37:07 +0200 | [diff] [blame] | 7816 | .eh_timed_out = ufshcd_eh_timed_out, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7817 | .ioctl = ufshcd_ioctl, |
| 7818 | #ifdef CONFIG_COMPAT |
| 7819 | .compat_ioctl = ufshcd_ioctl, |
| 7820 | #endif |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7821 | .this_id = -1, |
| 7822 | .sg_tablesize = SG_ALL, |
| 7823 | .cmd_per_lun = UFSHCD_CMD_PER_LUN, |
| 7824 | .can_queue = UFSHCD_CAN_QUEUE, |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7825 | .max_host_blocked = 1, |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 7826 | .track_queue_depth = 1, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7827 | }; |
| 7828 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7829 | static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg, |
| 7830 | int ua) |
| 7831 | { |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 7832 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7833 | |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 7834 | if (!vreg) |
| 7835 | return 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7836 | |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 7837 | ret = regulator_set_load(vreg->reg, ua); |
| 7838 | if (ret < 0) { |
| 7839 | dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n", |
| 7840 | __func__, vreg->name, ua, ret); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7841 | } |
| 7842 | |
| 7843 | return ret; |
| 7844 | } |
| 7845 | |
| 7846 | static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba, |
| 7847 | struct ufs_vreg *vreg) |
| 7848 | { |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 7849 | if (!vreg) |
| 7850 | return 0; |
| 7851 | else if (vreg->unused) |
| 7852 | return 0; |
| 7853 | else |
| 7854 | return ufshcd_config_vreg_load(hba->dev, vreg, |
| 7855 | UFS_VREG_LPM_LOAD_UA); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7856 | } |
| 7857 | |
| 7858 | static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba, |
| 7859 | struct ufs_vreg *vreg) |
| 7860 | { |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 7861 | if (!vreg) |
| 7862 | return 0; |
| 7863 | else if (vreg->unused) |
| 7864 | return 0; |
| 7865 | else |
| 7866 | return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7867 | } |
| 7868 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7869 | static int ufshcd_config_vreg(struct device *dev, |
| 7870 | struct ufs_vreg *vreg, bool on) |
| 7871 | { |
| 7872 | int ret = 0; |
| 7873 | struct regulator *reg = vreg->reg; |
| 7874 | const char *name = vreg->name; |
| 7875 | int min_uV, uA_load; |
| 7876 | |
| 7877 | BUG_ON(!vreg); |
| 7878 | |
| 7879 | if (regulator_count_voltages(reg) > 0) { |
| 7880 | min_uV = on ? vreg->min_uV : 0; |
| 7881 | ret = regulator_set_voltage(reg, min_uV, vreg->max_uV); |
| 7882 | if (ret) { |
| 7883 | dev_err(dev, "%s: %s set voltage failed, err=%d\n", |
| 7884 | __func__, name, ret); |
| 7885 | goto out; |
| 7886 | } |
| 7887 | |
| 7888 | uA_load = on ? vreg->max_uA : 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7889 | ret = ufshcd_config_vreg_load(dev, vreg, uA_load); |
| 7890 | if (ret) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7891 | goto out; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7892 | } |
| 7893 | out: |
| 7894 | return ret; |
| 7895 | } |
| 7896 | |
| 7897 | static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 7898 | { |
| 7899 | int ret = 0; |
| 7900 | |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 7901 | if (!vreg) |
| 7902 | goto out; |
| 7903 | else if (vreg->enabled || vreg->unused) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7904 | goto out; |
| 7905 | |
| 7906 | ret = ufshcd_config_vreg(dev, vreg, true); |
| 7907 | if (!ret) |
| 7908 | ret = regulator_enable(vreg->reg); |
| 7909 | |
| 7910 | if (!ret) |
| 7911 | vreg->enabled = true; |
| 7912 | else |
| 7913 | dev_err(dev, "%s: %s enable failed, err=%d\n", |
| 7914 | __func__, vreg->name, ret); |
| 7915 | out: |
| 7916 | return ret; |
| 7917 | } |
| 7918 | |
| 7919 | static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 7920 | { |
| 7921 | int ret = 0; |
| 7922 | |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 7923 | if (!vreg) |
| 7924 | goto out; |
| 7925 | else if (!vreg->enabled || vreg->unused) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7926 | goto out; |
| 7927 | |
| 7928 | ret = regulator_disable(vreg->reg); |
| 7929 | |
| 7930 | if (!ret) { |
| 7931 | /* ignore errors on applying disable config */ |
| 7932 | ufshcd_config_vreg(dev, vreg, false); |
| 7933 | vreg->enabled = false; |
| 7934 | } else { |
| 7935 | dev_err(dev, "%s: %s disable failed, err=%d\n", |
| 7936 | __func__, vreg->name, ret); |
| 7937 | } |
| 7938 | out: |
| 7939 | return ret; |
| 7940 | } |
| 7941 | |
| 7942 | static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on) |
| 7943 | { |
| 7944 | int ret = 0; |
| 7945 | struct device *dev = hba->dev; |
| 7946 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 7947 | |
| 7948 | if (!info) |
| 7949 | goto out; |
| 7950 | |
| 7951 | ret = ufshcd_toggle_vreg(dev, info->vcc, on); |
| 7952 | if (ret) |
| 7953 | goto out; |
| 7954 | |
| 7955 | ret = ufshcd_toggle_vreg(dev, info->vccq, on); |
| 7956 | if (ret) |
| 7957 | goto out; |
| 7958 | |
| 7959 | ret = ufshcd_toggle_vreg(dev, info->vccq2, on); |
| 7960 | if (ret) |
| 7961 | goto out; |
| 7962 | |
| 7963 | out: |
| 7964 | if (ret) { |
| 7965 | ufshcd_toggle_vreg(dev, info->vccq2, false); |
| 7966 | ufshcd_toggle_vreg(dev, info->vccq, false); |
| 7967 | ufshcd_toggle_vreg(dev, info->vcc, false); |
| 7968 | } |
| 7969 | return ret; |
| 7970 | } |
| 7971 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7972 | static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on) |
| 7973 | { |
| 7974 | struct ufs_vreg_info *info = &hba->vreg_info; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7975 | int ret = 0; |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7976 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7977 | if (info->vdd_hba) { |
| 7978 | ret = ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on); |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7979 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7980 | if (!ret) |
| 7981 | ufshcd_vops_update_sec_cfg(hba, on); |
| 7982 | } |
| 7983 | |
| 7984 | return ret; |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7985 | } |
| 7986 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7987 | static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 7988 | { |
| 7989 | int ret = 0; |
| 7990 | |
| 7991 | if (!vreg) |
| 7992 | goto out; |
| 7993 | |
| 7994 | vreg->reg = devm_regulator_get(dev, vreg->name); |
| 7995 | if (IS_ERR(vreg->reg)) { |
| 7996 | ret = PTR_ERR(vreg->reg); |
| 7997 | dev_err(dev, "%s: %s get failed, err=%d\n", |
| 7998 | __func__, vreg->name, ret); |
| 7999 | } |
| 8000 | out: |
| 8001 | return ret; |
| 8002 | } |
| 8003 | |
| 8004 | static int ufshcd_init_vreg(struct ufs_hba *hba) |
| 8005 | { |
| 8006 | int ret = 0; |
| 8007 | struct device *dev = hba->dev; |
| 8008 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 8009 | |
| 8010 | if (!info) |
| 8011 | goto out; |
| 8012 | |
| 8013 | ret = ufshcd_get_vreg(dev, info->vcc); |
| 8014 | if (ret) |
| 8015 | goto out; |
| 8016 | |
| 8017 | ret = ufshcd_get_vreg(dev, info->vccq); |
| 8018 | if (ret) |
| 8019 | goto out; |
| 8020 | |
| 8021 | ret = ufshcd_get_vreg(dev, info->vccq2); |
| 8022 | out: |
| 8023 | return ret; |
| 8024 | } |
| 8025 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 8026 | static int ufshcd_init_hba_vreg(struct ufs_hba *hba) |
| 8027 | { |
| 8028 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 8029 | |
| 8030 | if (info) |
| 8031 | return ufshcd_get_vreg(hba->dev, info->vdd_hba); |
| 8032 | |
| 8033 | return 0; |
| 8034 | } |
| 8035 | |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 8036 | static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused) |
| 8037 | { |
| 8038 | int ret = 0; |
| 8039 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 8040 | |
| 8041 | if (!info) |
| 8042 | goto out; |
| 8043 | else if (!info->vccq) |
| 8044 | goto out; |
| 8045 | |
| 8046 | if (unused) { |
| 8047 | /* shut off the rail here */ |
| 8048 | ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false); |
| 8049 | /* |
| 8050 | * Mark this rail as no longer used, so it doesn't get enabled |
| 8051 | * later by mistake |
| 8052 | */ |
| 8053 | if (!ret) |
| 8054 | info->vccq->unused = true; |
| 8055 | } else { |
| 8056 | /* |
| 8057 | * rail should have been already enabled hence just make sure |
| 8058 | * that unused flag is cleared. |
| 8059 | */ |
| 8060 | info->vccq->unused = false; |
| 8061 | } |
| 8062 | out: |
| 8063 | return ret; |
| 8064 | } |
| 8065 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8066 | static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on, |
| 8067 | bool skip_ref_clk, bool is_gating_context) |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8068 | { |
| 8069 | int ret = 0; |
| 8070 | struct ufs_clk_info *clki; |
| 8071 | struct list_head *head = &hba->clk_list_head; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8072 | unsigned long flags; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8073 | ktime_t start = ktime_get(); |
| 8074 | bool clk_state_changed = false; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8075 | |
| 8076 | if (!head || list_empty(head)) |
| 8077 | goto out; |
| 8078 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 8079 | /* call vendor specific bus vote before enabling the clocks */ |
| 8080 | if (on) { |
| 8081 | ret = ufshcd_vops_set_bus_vote(hba, on); |
| 8082 | if (ret) |
| 8083 | return ret; |
| 8084 | } |
| 8085 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8086 | /* |
| 8087 | * vendor specific setup_clocks ops may depend on clocks managed by |
| 8088 | * this standard driver hence call the vendor specific setup_clocks |
| 8089 | * before disabling the clocks managed here. |
| 8090 | */ |
| 8091 | if (!on) { |
| 8092 | ret = ufshcd_vops_setup_clocks(hba, on, is_gating_context); |
| 8093 | if (ret) |
| 8094 | return ret; |
| 8095 | } |
| 8096 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8097 | list_for_each_entry(clki, head, list) { |
| 8098 | if (!IS_ERR_OR_NULL(clki->clk)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8099 | if (skip_ref_clk && !strcmp(clki->name, "ref_clk")) |
| 8100 | continue; |
| 8101 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8102 | clk_state_changed = on ^ clki->enabled; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8103 | if (on && !clki->enabled) { |
| 8104 | ret = clk_prepare_enable(clki->clk); |
| 8105 | if (ret) { |
| 8106 | dev_err(hba->dev, "%s: %s prepare enable failed, %d\n", |
| 8107 | __func__, clki->name, ret); |
| 8108 | goto out; |
| 8109 | } |
| 8110 | } else if (!on && clki->enabled) { |
| 8111 | clk_disable_unprepare(clki->clk); |
| 8112 | } |
| 8113 | clki->enabled = on; |
| 8114 | dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__, |
| 8115 | clki->name, on ? "en" : "dis"); |
| 8116 | } |
| 8117 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8118 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8119 | /* |
| 8120 | * vendor specific setup_clocks ops may depend on clocks managed by |
| 8121 | * this standard driver hence call the vendor specific setup_clocks |
| 8122 | * after enabling the clocks managed here. |
| 8123 | */ |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 8124 | if (on) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8125 | ret = ufshcd_vops_setup_clocks(hba, on, is_gating_context); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 8126 | if (ret) |
| 8127 | goto out; |
| 8128 | } |
| 8129 | |
| 8130 | /* |
| 8131 | * call vendor specific bus vote to remove the vote after |
| 8132 | * disabling the clocks. |
| 8133 | */ |
| 8134 | if (!on) |
| 8135 | ret = ufshcd_vops_set_bus_vote(hba, on); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8136 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8137 | out: |
| 8138 | if (ret) { |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 8139 | if (on) |
| 8140 | /* Can't do much if this fails */ |
| 8141 | (void) ufshcd_vops_set_bus_vote(hba, false); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8142 | list_for_each_entry(clki, head, list) { |
| 8143 | if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled) |
| 8144 | clk_disable_unprepare(clki->clk); |
| 8145 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8146 | } else if (!ret && on) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8147 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 8148 | hba->clk_gating.state = CLKS_ON; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8149 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 8150 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8151 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8152 | /* restore the secure configuration as clocks are enabled */ |
| 8153 | ufshcd_vops_update_sec_cfg(hba, true); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8154 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8155 | |
| 8156 | if (clk_state_changed) |
| 8157 | trace_ufshcd_profile_clk_gating(dev_name(hba->dev), |
| 8158 | (on ? "on" : "off"), |
| 8159 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8160 | return ret; |
| 8161 | } |
| 8162 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8163 | static int ufshcd_enable_clocks(struct ufs_hba *hba) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8164 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8165 | return ufshcd_setup_clocks(hba, true, false, false); |
| 8166 | } |
| 8167 | |
| 8168 | static int ufshcd_disable_clocks(struct ufs_hba *hba, |
| 8169 | bool is_gating_context) |
| 8170 | { |
| 8171 | return ufshcd_setup_clocks(hba, false, false, is_gating_context); |
| 8172 | } |
| 8173 | |
| 8174 | static int ufshcd_disable_clocks_skip_ref_clk(struct ufs_hba *hba, |
| 8175 | bool is_gating_context) |
| 8176 | { |
| 8177 | return ufshcd_setup_clocks(hba, false, true, is_gating_context); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8178 | } |
| 8179 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8180 | static int ufshcd_init_clocks(struct ufs_hba *hba) |
| 8181 | { |
| 8182 | int ret = 0; |
| 8183 | struct ufs_clk_info *clki; |
| 8184 | struct device *dev = hba->dev; |
| 8185 | struct list_head *head = &hba->clk_list_head; |
| 8186 | |
| 8187 | if (!head || list_empty(head)) |
| 8188 | goto out; |
| 8189 | |
| 8190 | list_for_each_entry(clki, head, list) { |
| 8191 | if (!clki->name) |
| 8192 | continue; |
| 8193 | |
| 8194 | clki->clk = devm_clk_get(dev, clki->name); |
| 8195 | if (IS_ERR(clki->clk)) { |
| 8196 | ret = PTR_ERR(clki->clk); |
| 8197 | dev_err(dev, "%s: %s clk get failed, %d\n", |
| 8198 | __func__, clki->name, ret); |
| 8199 | goto out; |
| 8200 | } |
| 8201 | |
| 8202 | if (clki->max_freq) { |
| 8203 | ret = clk_set_rate(clki->clk, clki->max_freq); |
| 8204 | if (ret) { |
| 8205 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 8206 | __func__, clki->name, |
| 8207 | clki->max_freq, ret); |
| 8208 | goto out; |
| 8209 | } |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 8210 | clki->curr_freq = clki->max_freq; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8211 | } |
| 8212 | dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__, |
| 8213 | clki->name, clk_get_rate(clki->clk)); |
| 8214 | } |
| 8215 | out: |
| 8216 | return ret; |
| 8217 | } |
| 8218 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8219 | static int ufshcd_variant_hba_init(struct ufs_hba *hba) |
| 8220 | { |
| 8221 | int err = 0; |
| 8222 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8223 | if (!hba->var || !hba->var->vops) |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8224 | goto out; |
| 8225 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 8226 | err = ufshcd_vops_init(hba); |
| 8227 | if (err) |
| 8228 | goto out; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8229 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 8230 | err = ufshcd_vops_setup_regulators(hba, true); |
| 8231 | if (err) |
| 8232 | goto out_exit; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8233 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8234 | goto out; |
| 8235 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8236 | out_exit: |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 8237 | ufshcd_vops_exit(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8238 | out: |
| 8239 | if (err) |
| 8240 | dev_err(hba->dev, "%s: variant %s init failed err %d\n", |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 8241 | __func__, ufshcd_get_var_name(hba), err); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8242 | return err; |
| 8243 | } |
| 8244 | |
| 8245 | static void ufshcd_variant_hba_exit(struct ufs_hba *hba) |
| 8246 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8247 | if (!hba->var || !hba->var->vops) |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8248 | return; |
| 8249 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 8250 | ufshcd_vops_setup_regulators(hba, false); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8251 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 8252 | ufshcd_vops_exit(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8253 | } |
| 8254 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8255 | static int ufshcd_hba_init(struct ufs_hba *hba) |
| 8256 | { |
| 8257 | int err; |
| 8258 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 8259 | /* |
| 8260 | * Handle host controller power separately from the UFS device power |
| 8261 | * rails as it will help controlling the UFS host controller power |
| 8262 | * collapse easily which is different than UFS device power collapse. |
| 8263 | * Also, enable the host controller power before we go ahead with rest |
| 8264 | * of the initialization here. |
| 8265 | */ |
| 8266 | err = ufshcd_init_hba_vreg(hba); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8267 | if (err) |
| 8268 | goto out; |
| 8269 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 8270 | err = ufshcd_setup_hba_vreg(hba, true); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8271 | if (err) |
| 8272 | goto out; |
| 8273 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 8274 | err = ufshcd_init_clocks(hba); |
| 8275 | if (err) |
| 8276 | goto out_disable_hba_vreg; |
| 8277 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8278 | err = ufshcd_enable_clocks(hba); |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 8279 | if (err) |
| 8280 | goto out_disable_hba_vreg; |
| 8281 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8282 | err = ufshcd_init_vreg(hba); |
| 8283 | if (err) |
| 8284 | goto out_disable_clks; |
| 8285 | |
| 8286 | err = ufshcd_setup_vreg(hba, true); |
| 8287 | if (err) |
| 8288 | goto out_disable_clks; |
| 8289 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8290 | err = ufshcd_variant_hba_init(hba); |
| 8291 | if (err) |
| 8292 | goto out_disable_vreg; |
| 8293 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 8294 | hba->is_powered = true; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8295 | goto out; |
| 8296 | |
| 8297 | out_disable_vreg: |
| 8298 | ufshcd_setup_vreg(hba, false); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 8299 | out_disable_clks: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8300 | ufshcd_disable_clocks(hba, false); |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 8301 | out_disable_hba_vreg: |
| 8302 | ufshcd_setup_hba_vreg(hba, false); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8303 | out: |
| 8304 | return err; |
| 8305 | } |
| 8306 | |
| 8307 | static void ufshcd_hba_exit(struct ufs_hba *hba) |
| 8308 | { |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 8309 | if (hba->is_powered) { |
| 8310 | ufshcd_variant_hba_exit(hba); |
| 8311 | ufshcd_setup_vreg(hba, false); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8312 | if (ufshcd_is_clkscaling_supported(hba)) { |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 8313 | if (hba->devfreq) |
| 8314 | ufshcd_suspend_clkscaling(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8315 | destroy_workqueue(hba->clk_scaling.workq); |
| 8316 | } |
| 8317 | ufshcd_disable_clocks(hba, false); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 8318 | ufshcd_setup_hba_vreg(hba, false); |
| 8319 | hba->is_powered = false; |
| 8320 | } |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8321 | } |
| 8322 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8323 | static int |
| 8324 | ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8325 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8326 | unsigned char cmd[6] = {REQUEST_SENSE, |
| 8327 | 0, |
| 8328 | 0, |
| 8329 | 0, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8330 | UFSHCD_REQ_SENSE_SIZE, |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8331 | 0}; |
| 8332 | char *buffer; |
| 8333 | int ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8334 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8335 | buffer = kzalloc(UFSHCD_REQ_SENSE_SIZE, GFP_KERNEL); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8336 | if (!buffer) { |
| 8337 | ret = -ENOMEM; |
| 8338 | goto out; |
| 8339 | } |
| 8340 | |
| 8341 | ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8342 | UFSHCD_REQ_SENSE_SIZE, NULL, |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8343 | msecs_to_jiffies(1000), 3, NULL, REQ_PM); |
| 8344 | if (ret) |
| 8345 | pr_err("%s: failed with err %d\n", __func__, ret); |
| 8346 | |
| 8347 | kfree(buffer); |
| 8348 | out: |
| 8349 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8350 | } |
| 8351 | |
| 8352 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8353 | * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device |
| 8354 | * power mode |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8355 | * @hba: per adapter instance |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8356 | * @pwr_mode: device power mode to set |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8357 | * |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8358 | * Returns 0 if requested power mode is set successfully |
| 8359 | * Returns non-zero if failed to set the requested power mode |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8360 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8361 | static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba, |
| 8362 | enum ufs_dev_pwr_mode pwr_mode) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8363 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8364 | unsigned char cmd[6] = { START_STOP }; |
| 8365 | struct scsi_sense_hdr sshdr; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 8366 | struct scsi_device *sdp; |
| 8367 | unsigned long flags; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8368 | int ret; |
| 8369 | |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 8370 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 8371 | sdp = hba->sdev_ufs_device; |
| 8372 | if (sdp) { |
| 8373 | ret = scsi_device_get(sdp); |
| 8374 | if (!ret && !scsi_device_online(sdp)) { |
| 8375 | ret = -ENODEV; |
| 8376 | scsi_device_put(sdp); |
| 8377 | } |
| 8378 | } else { |
| 8379 | ret = -ENODEV; |
| 8380 | } |
| 8381 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 8382 | |
| 8383 | if (ret) |
| 8384 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8385 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8386 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8387 | * If scsi commands fail, the scsi mid-layer schedules scsi error- |
| 8388 | * handling, which would wait for host to be resumed. Since we know |
| 8389 | * we are functional while we are here, skip host resume in error |
| 8390 | * handling context. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8391 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8392 | hba->host->eh_noresume = 1; |
Subhash Jadavani | 23df231 | 2016-12-16 12:54:30 -0800 | [diff] [blame] | 8393 | if (!hba->dev_info.is_ufs_dev_wlun_ua_cleared) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8394 | ret = ufshcd_send_request_sense(hba, sdp); |
| 8395 | if (ret) |
| 8396 | goto out; |
| 8397 | /* Unit attention condition is cleared now */ |
Subhash Jadavani | 23df231 | 2016-12-16 12:54:30 -0800 | [diff] [blame] | 8398 | hba->dev_info.is_ufs_dev_wlun_ua_cleared = 1; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8399 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8400 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8401 | cmd[4] = pwr_mode << 4; |
| 8402 | |
| 8403 | /* |
| 8404 | * Current function would be generally called from the power management |
| 8405 | * callbacks hence set the REQ_PM flag so that it doesn't resume the |
| 8406 | * already suspended childs. |
| 8407 | */ |
| 8408 | ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr, |
| 8409 | START_STOP_TIMEOUT, 0, NULL, REQ_PM); |
| 8410 | if (ret) { |
| 8411 | sdev_printk(KERN_WARNING, sdp, |
Hannes Reinecke | ef61329 | 2014-10-24 14:27:00 +0200 | [diff] [blame] | 8412 | "START_STOP failed for power mode: %d, result %x\n", |
| 8413 | pwr_mode, ret); |
Hannes Reinecke | 2104551 | 2015-01-08 07:43:46 +0100 | [diff] [blame] | 8414 | if (driver_byte(ret) & DRIVER_SENSE) |
| 8415 | scsi_print_sense_hdr(sdp, NULL, &sshdr); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8416 | } |
| 8417 | |
| 8418 | if (!ret) |
| 8419 | hba->curr_dev_pwr_mode = pwr_mode; |
| 8420 | out: |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 8421 | scsi_device_put(sdp); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8422 | hba->host->eh_noresume = 0; |
| 8423 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8424 | } |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8425 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8426 | static int ufshcd_link_state_transition(struct ufs_hba *hba, |
| 8427 | enum uic_link_state req_link_state, |
| 8428 | int check_for_bkops) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8429 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8430 | int ret = 0; |
| 8431 | |
| 8432 | if (req_link_state == hba->uic_link_state) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8433 | return 0; |
| 8434 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8435 | if (req_link_state == UIC_LINK_HIBERN8_STATE) { |
| 8436 | ret = ufshcd_uic_hibern8_enter(hba); |
| 8437 | if (!ret) |
| 8438 | ufshcd_set_link_hibern8(hba); |
| 8439 | else |
| 8440 | goto out; |
| 8441 | } |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8442 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8443 | * If autobkops is enabled, link can't be turned off because |
| 8444 | * turning off the link would also turn off the device. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8445 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8446 | else if ((req_link_state == UIC_LINK_OFF_STATE) && |
| 8447 | (!check_for_bkops || (check_for_bkops && |
| 8448 | !hba->auto_bkops_enabled))) { |
| 8449 | /* |
Yaniv Gardi | f3099fb | 2016-03-10 17:37:17 +0200 | [diff] [blame] | 8450 | * Let's make sure that link is in low power mode, we are doing |
| 8451 | * this currently by putting the link in Hibern8. Otherway to |
| 8452 | * put the link in low power mode is to send the DME end point |
| 8453 | * to device and then send the DME reset command to local |
| 8454 | * unipro. But putting the link in hibern8 is much faster. |
| 8455 | */ |
| 8456 | ret = ufshcd_uic_hibern8_enter(hba); |
| 8457 | if (ret) |
| 8458 | goto out; |
| 8459 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8460 | * Change controller state to "reset state" which |
| 8461 | * should also put the link in off/reset state |
| 8462 | */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 8463 | ufshcd_hba_stop(hba, true); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8464 | /* |
| 8465 | * TODO: Check if we need any delay to make sure that |
| 8466 | * controller is reset |
| 8467 | */ |
| 8468 | ufshcd_set_link_off(hba); |
| 8469 | } |
| 8470 | |
| 8471 | out: |
| 8472 | return ret; |
| 8473 | } |
| 8474 | |
| 8475 | static void ufshcd_vreg_set_lpm(struct ufs_hba *hba) |
| 8476 | { |
| 8477 | /* |
Yaniv Gardi | b799fdf | 2016-03-10 17:37:18 +0200 | [diff] [blame] | 8478 | * It seems some UFS devices may keep drawing more than sleep current |
| 8479 | * (atleast for 500us) from UFS rails (especially from VCCQ rail). |
| 8480 | * To avoid this situation, add 2ms delay before putting these UFS |
| 8481 | * rails in LPM mode. |
| 8482 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8483 | if (!ufshcd_is_link_active(hba)) |
Yaniv Gardi | b799fdf | 2016-03-10 17:37:18 +0200 | [diff] [blame] | 8484 | usleep_range(2000, 2100); |
| 8485 | |
| 8486 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8487 | * If UFS device is either in UFS_Sleep turn off VCC rail to save some |
| 8488 | * power. |
| 8489 | * |
| 8490 | * If UFS device and link is in OFF state, all power supplies (VCC, |
| 8491 | * VCCQ, VCCQ2) can be turned off if power on write protect is not |
| 8492 | * required. If UFS link is inactive (Hibern8 or OFF state) and device |
| 8493 | * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode. |
| 8494 | * |
| 8495 | * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway |
| 8496 | * in low power state which would save some power. |
| 8497 | */ |
| 8498 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) && |
| 8499 | !hba->dev_info.is_lu_power_on_wp) { |
| 8500 | ufshcd_setup_vreg(hba, false); |
| 8501 | } else if (!ufshcd_is_ufs_dev_active(hba)) { |
| 8502 | ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false); |
| 8503 | if (!ufshcd_is_link_active(hba)) { |
| 8504 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq); |
| 8505 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2); |
| 8506 | } |
| 8507 | } |
| 8508 | } |
| 8509 | |
| 8510 | static int ufshcd_vreg_set_hpm(struct ufs_hba *hba) |
| 8511 | { |
| 8512 | int ret = 0; |
| 8513 | |
| 8514 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) && |
| 8515 | !hba->dev_info.is_lu_power_on_wp) { |
| 8516 | ret = ufshcd_setup_vreg(hba, true); |
| 8517 | } else if (!ufshcd_is_ufs_dev_active(hba)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8518 | if (!ret && !ufshcd_is_link_active(hba)) { |
| 8519 | ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq); |
| 8520 | if (ret) |
| 8521 | goto vcc_disable; |
| 8522 | ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2); |
| 8523 | if (ret) |
| 8524 | goto vccq_lpm; |
| 8525 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8526 | ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8527 | } |
| 8528 | goto out; |
| 8529 | |
| 8530 | vccq_lpm: |
| 8531 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq); |
| 8532 | vcc_disable: |
| 8533 | ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false); |
| 8534 | out: |
| 8535 | return ret; |
| 8536 | } |
| 8537 | |
| 8538 | static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba) |
| 8539 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8540 | if (ufshcd_is_link_off(hba) || |
| 8541 | (ufshcd_is_link_hibern8(hba) |
| 8542 | && ufshcd_is_power_collapse_during_hibern8_allowed(hba))) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8543 | ufshcd_setup_hba_vreg(hba, false); |
| 8544 | } |
| 8545 | |
| 8546 | static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba) |
| 8547 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8548 | if (ufshcd_is_link_off(hba) || |
| 8549 | (ufshcd_is_link_hibern8(hba) |
| 8550 | && ufshcd_is_power_collapse_during_hibern8_allowed(hba))) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8551 | ufshcd_setup_hba_vreg(hba, true); |
| 8552 | } |
| 8553 | |
| 8554 | /** |
| 8555 | * ufshcd_suspend - helper function for suspend operations |
| 8556 | * @hba: per adapter instance |
| 8557 | * @pm_op: desired low power operation type |
| 8558 | * |
| 8559 | * This function will try to put the UFS device and link into low power |
| 8560 | * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl" |
| 8561 | * (System PM level). |
| 8562 | * |
| 8563 | * If this function is called during shutdown, it will make sure that |
| 8564 | * both UFS device and UFS link is powered off. |
| 8565 | * |
| 8566 | * NOTE: UFS device & link must be active before we enter in this function. |
| 8567 | * |
| 8568 | * Returns 0 for success and non-zero for failure |
| 8569 | */ |
| 8570 | static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op) |
| 8571 | { |
| 8572 | int ret = 0; |
| 8573 | enum ufs_pm_level pm_lvl; |
| 8574 | enum ufs_dev_pwr_mode req_dev_pwr_mode; |
| 8575 | enum uic_link_state req_link_state; |
| 8576 | |
| 8577 | hba->pm_op_in_progress = 1; |
| 8578 | if (!ufshcd_is_shutdown_pm(pm_op)) { |
| 8579 | pm_lvl = ufshcd_is_runtime_pm(pm_op) ? |
| 8580 | hba->rpm_lvl : hba->spm_lvl; |
| 8581 | req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl); |
| 8582 | req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl); |
| 8583 | } else { |
| 8584 | req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE; |
| 8585 | req_link_state = UIC_LINK_OFF_STATE; |
| 8586 | } |
| 8587 | |
| 8588 | /* |
| 8589 | * If we can't transition into any of the low power modes |
| 8590 | * just gate the clocks. |
| 8591 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8592 | WARN_ON(hba->hibern8_on_idle.is_enabled && |
| 8593 | hba->hibern8_on_idle.active_reqs); |
| 8594 | ufshcd_hold_all(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8595 | hba->clk_gating.is_suspended = true; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8596 | hba->hibern8_on_idle.is_suspended = true; |
| 8597 | |
| 8598 | if (hba->clk_scaling.is_allowed) { |
| 8599 | cancel_work_sync(&hba->clk_scaling.suspend_work); |
| 8600 | cancel_work_sync(&hba->clk_scaling.resume_work); |
| 8601 | ufshcd_suspend_clkscaling(hba); |
| 8602 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8603 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8604 | if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE && |
| 8605 | req_link_state == UIC_LINK_ACTIVE_STATE) { |
| 8606 | goto disable_clks; |
| 8607 | } |
| 8608 | |
| 8609 | if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) && |
| 8610 | (req_link_state == hba->uic_link_state)) |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8611 | goto enable_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8612 | |
| 8613 | /* UFS device & link must be active before we enter in this function */ |
| 8614 | if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) { |
| 8615 | ret = -EINVAL; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8616 | goto enable_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8617 | } |
| 8618 | |
| 8619 | if (ufshcd_is_runtime_pm(pm_op)) { |
Subhash Jadavani | 374a246 | 2014-09-25 15:32:35 +0300 | [diff] [blame] | 8620 | if (ufshcd_can_autobkops_during_suspend(hba)) { |
| 8621 | /* |
| 8622 | * The device is idle with no requests in the queue, |
| 8623 | * allow background operations if bkops status shows |
| 8624 | * that performance might be impacted. |
| 8625 | */ |
| 8626 | ret = ufshcd_urgent_bkops(hba); |
| 8627 | if (ret) |
| 8628 | goto enable_gating; |
| 8629 | } else { |
| 8630 | /* make sure that auto bkops is disabled */ |
| 8631 | ufshcd_disable_auto_bkops(hba); |
| 8632 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8633 | } |
| 8634 | |
| 8635 | if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) && |
| 8636 | ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) || |
| 8637 | !ufshcd_is_runtime_pm(pm_op))) { |
| 8638 | /* ensure that bkops is disabled */ |
| 8639 | ufshcd_disable_auto_bkops(hba); |
| 8640 | ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode); |
| 8641 | if (ret) |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8642 | goto enable_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8643 | } |
| 8644 | |
| 8645 | ret = ufshcd_link_state_transition(hba, req_link_state, 1); |
| 8646 | if (ret) |
| 8647 | goto set_dev_active; |
| 8648 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8649 | if (ufshcd_is_link_hibern8(hba) && |
| 8650 | ufshcd_is_hibern8_on_idle_allowed(hba)) |
| 8651 | hba->hibern8_on_idle.state = HIBERN8_ENTERED; |
| 8652 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8653 | ufshcd_vreg_set_lpm(hba); |
| 8654 | |
| 8655 | disable_clks: |
| 8656 | /* |
| 8657 | * Call vendor specific suspend callback. As these callbacks may access |
| 8658 | * vendor specific host controller register space call them before the |
| 8659 | * host clocks are ON. |
| 8660 | */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 8661 | ret = ufshcd_vops_suspend(hba, pm_op); |
| 8662 | if (ret) |
| 8663 | goto set_link_active; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8664 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8665 | if (!ufshcd_is_link_active(hba)) |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8666 | ret = ufshcd_disable_clocks(hba, false); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8667 | else |
| 8668 | /* If link is active, device ref_clk can't be switched off */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8669 | ret = ufshcd_disable_clocks_skip_ref_clk(hba, false); |
| 8670 | if (ret) |
| 8671 | goto set_link_active; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8672 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8673 | if (ufshcd_is_clkgating_allowed(hba)) { |
| 8674 | hba->clk_gating.state = CLKS_OFF; |
| 8675 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 8676 | hba->clk_gating.state); |
| 8677 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8678 | /* |
| 8679 | * 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] | 8680 | * host controller transaction expected till resume. |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8681 | */ |
| 8682 | ufshcd_disable_irq(hba); |
| 8683 | /* Put the host controller in low power mode if possible */ |
| 8684 | ufshcd_hba_vreg_set_lpm(hba); |
| 8685 | goto out; |
| 8686 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8687 | set_link_active: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8688 | if (hba->clk_scaling.is_allowed) |
| 8689 | ufshcd_resume_clkscaling(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8690 | ufshcd_vreg_set_hpm(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8691 | if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8692 | ufshcd_set_link_active(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8693 | } else if (ufshcd_is_link_off(hba)) { |
| 8694 | ufshcd_update_error_stats(hba, UFS_ERR_VOPS_SUSPEND); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8695 | ufshcd_host_reset_and_restore(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8696 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8697 | set_dev_active: |
| 8698 | if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE)) |
| 8699 | ufshcd_disable_auto_bkops(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8700 | enable_gating: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8701 | if (hba->clk_scaling.is_allowed) |
| 8702 | ufshcd_resume_clkscaling(hba); |
| 8703 | hba->hibern8_on_idle.is_suspended = false; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8704 | hba->clk_gating.is_suspended = false; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8705 | ufshcd_release_all(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8706 | out: |
| 8707 | hba->pm_op_in_progress = 0; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8708 | |
| 8709 | if (ret) |
| 8710 | ufshcd_update_error_stats(hba, UFS_ERR_SUSPEND); |
| 8711 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8712 | return ret; |
| 8713 | } |
| 8714 | |
| 8715 | /** |
| 8716 | * ufshcd_resume - helper function for resume operations |
| 8717 | * @hba: per adapter instance |
| 8718 | * @pm_op: runtime PM or system PM |
| 8719 | * |
| 8720 | * This function basically brings the UFS device, UniPro link and controller |
| 8721 | * to active state. |
| 8722 | * |
| 8723 | * Returns 0 for success and non-zero for failure |
| 8724 | */ |
| 8725 | static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op) |
| 8726 | { |
| 8727 | int ret; |
| 8728 | enum uic_link_state old_link_state; |
| 8729 | |
| 8730 | hba->pm_op_in_progress = 1; |
| 8731 | old_link_state = hba->uic_link_state; |
| 8732 | |
| 8733 | ufshcd_hba_vreg_set_hpm(hba); |
| 8734 | /* Make sure clocks are enabled before accessing controller */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8735 | ret = ufshcd_enable_clocks(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8736 | if (ret) |
| 8737 | goto out; |
| 8738 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8739 | /* enable the host irq as host controller would be active soon */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8740 | ufshcd_enable_irq(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8741 | |
| 8742 | ret = ufshcd_vreg_set_hpm(hba); |
| 8743 | if (ret) |
| 8744 | goto disable_irq_and_vops_clks; |
| 8745 | |
| 8746 | /* |
| 8747 | * Call vendor specific resume callback. As these callbacks may access |
| 8748 | * vendor specific host controller register space call them when the |
| 8749 | * host clocks are ON. |
| 8750 | */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 8751 | ret = ufshcd_vops_resume(hba, pm_op); |
| 8752 | if (ret) |
| 8753 | goto disable_vreg; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8754 | |
| 8755 | if (ufshcd_is_link_hibern8(hba)) { |
| 8756 | ret = ufshcd_uic_hibern8_exit(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8757 | if (!ret) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8758 | ufshcd_set_link_active(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8759 | if (ufshcd_is_hibern8_on_idle_allowed(hba)) |
| 8760 | hba->hibern8_on_idle.state = HIBERN8_EXITED; |
| 8761 | } else { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8762 | goto vendor_suspend; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8763 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8764 | } else if (ufshcd_is_link_off(hba)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8765 | /* |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 8766 | * A full initialization of the host and the device is required |
| 8767 | * since the link was put to off during suspend. |
| 8768 | */ |
| 8769 | ret = ufshcd_reset_and_restore(hba); |
| 8770 | /* |
| 8771 | * ufshcd_reset_and_restore() should have already |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8772 | * set the link state as active |
| 8773 | */ |
| 8774 | if (ret || !ufshcd_is_link_active(hba)) |
| 8775 | goto vendor_suspend; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8776 | /* mark link state as hibern8 exited */ |
| 8777 | if (ufshcd_is_hibern8_on_idle_allowed(hba)) |
| 8778 | hba->hibern8_on_idle.state = HIBERN8_EXITED; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8779 | } |
| 8780 | |
| 8781 | if (!ufshcd_is_ufs_dev_active(hba)) { |
| 8782 | ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE); |
| 8783 | if (ret) |
| 8784 | goto set_old_link_state; |
| 8785 | } |
| 8786 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8787 | if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) |
| 8788 | ufshcd_enable_auto_bkops(hba); |
| 8789 | else |
| 8790 | /* |
| 8791 | * If BKOPs operations are urgently needed at this moment then |
| 8792 | * keep auto-bkops enabled or else disable it. |
| 8793 | */ |
| 8794 | ufshcd_urgent_bkops(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8795 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8796 | hba->clk_gating.is_suspended = false; |
| 8797 | hba->hibern8_on_idle.is_suspended = false; |
| 8798 | |
| 8799 | if (hba->clk_scaling.is_allowed) |
| 8800 | ufshcd_resume_clkscaling(hba); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 8801 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8802 | /* Schedule clock gating in case of no access to UFS device yet */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8803 | ufshcd_release_all(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8804 | goto out; |
| 8805 | |
| 8806 | set_old_link_state: |
| 8807 | ufshcd_link_state_transition(hba, old_link_state, 0); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8808 | if (ufshcd_is_link_hibern8(hba) && |
| 8809 | ufshcd_is_hibern8_on_idle_allowed(hba)) |
| 8810 | hba->hibern8_on_idle.state = HIBERN8_ENTERED; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8811 | vendor_suspend: |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 8812 | ufshcd_vops_suspend(hba, pm_op); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8813 | disable_vreg: |
| 8814 | ufshcd_vreg_set_lpm(hba); |
| 8815 | disable_irq_and_vops_clks: |
| 8816 | ufshcd_disable_irq(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8817 | if (hba->clk_scaling.is_allowed) |
| 8818 | ufshcd_suspend_clkscaling(hba); |
| 8819 | ufshcd_disable_clocks(hba, false); |
| 8820 | if (ufshcd_is_clkgating_allowed(hba)) |
| 8821 | hba->clk_gating.state = CLKS_OFF; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8822 | out: |
| 8823 | hba->pm_op_in_progress = 0; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8824 | |
| 8825 | if (ret) |
| 8826 | ufshcd_update_error_stats(hba, UFS_ERR_RESUME); |
| 8827 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8828 | return ret; |
| 8829 | } |
| 8830 | |
| 8831 | /** |
| 8832 | * ufshcd_system_suspend - system suspend routine |
| 8833 | * @hba: per adapter instance |
| 8834 | * @pm_op: runtime PM or system PM |
| 8835 | * |
| 8836 | * Check the description of ufshcd_suspend() function for more details. |
| 8837 | * |
| 8838 | * Returns 0 for success and non-zero for failure |
| 8839 | */ |
| 8840 | int ufshcd_system_suspend(struct ufs_hba *hba) |
| 8841 | { |
| 8842 | int ret = 0; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8843 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8844 | |
| 8845 | if (!hba || !hba->is_powered) |
Dolev Raviv | 233b594 | 2014-10-23 13:25:14 +0300 | [diff] [blame] | 8846 | return 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8847 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8848 | if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) == |
| 8849 | hba->curr_dev_pwr_mode) && |
| 8850 | (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) == |
| 8851 | hba->uic_link_state)) |
| 8852 | goto out; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8853 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8854 | if (pm_runtime_suspended(hba->dev)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8855 | /* |
| 8856 | * UFS device and/or UFS link low power states during runtime |
| 8857 | * suspend seems to be different than what is expected during |
| 8858 | * system suspend. Hence runtime resume the devic & link and |
| 8859 | * let the system suspend low power states to take effect. |
| 8860 | * TODO: If resume takes longer time, we might have optimize |
| 8861 | * it in future by not resuming everything if possible. |
| 8862 | */ |
| 8863 | ret = ufshcd_runtime_resume(hba); |
| 8864 | if (ret) |
| 8865 | goto out; |
| 8866 | } |
| 8867 | |
| 8868 | ret = ufshcd_suspend(hba, UFS_SYSTEM_PM); |
| 8869 | out: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8870 | trace_ufshcd_system_suspend(dev_name(hba->dev), ret, |
| 8871 | ktime_to_us(ktime_sub(ktime_get(), start)), |
| 8872 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
Dolev Raviv | e785060 | 2014-09-25 15:32:36 +0300 | [diff] [blame] | 8873 | if (!ret) |
| 8874 | hba->is_sys_suspended = true; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8875 | return ret; |
| 8876 | } |
| 8877 | EXPORT_SYMBOL(ufshcd_system_suspend); |
| 8878 | |
| 8879 | /** |
| 8880 | * ufshcd_system_resume - system resume routine |
| 8881 | * @hba: per adapter instance |
| 8882 | * |
| 8883 | * Returns 0 for success and non-zero for failure |
| 8884 | */ |
| 8885 | |
| 8886 | int ufshcd_system_resume(struct ufs_hba *hba) |
| 8887 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8888 | int ret = 0; |
| 8889 | ktime_t start = ktime_get(); |
| 8890 | |
| 8891 | if (!hba) |
| 8892 | return -EINVAL; |
| 8893 | |
| 8894 | if (!hba->is_powered || pm_runtime_suspended(hba->dev)) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8895 | /* |
| 8896 | * Let the runtime resume take care of resuming |
| 8897 | * if runtime suspended. |
| 8898 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8899 | goto out; |
| 8900 | else |
| 8901 | ret = ufshcd_resume(hba, UFS_SYSTEM_PM); |
| 8902 | out: |
| 8903 | trace_ufshcd_system_resume(dev_name(hba->dev), ret, |
| 8904 | ktime_to_us(ktime_sub(ktime_get(), start)), |
| 8905 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
| 8906 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8907 | } |
| 8908 | EXPORT_SYMBOL(ufshcd_system_resume); |
| 8909 | |
| 8910 | /** |
| 8911 | * ufshcd_runtime_suspend - runtime suspend routine |
| 8912 | * @hba: per adapter instance |
| 8913 | * |
| 8914 | * Check the description of ufshcd_suspend() function for more details. |
| 8915 | * |
| 8916 | * Returns 0 for success and non-zero for failure |
| 8917 | */ |
| 8918 | int ufshcd_runtime_suspend(struct ufs_hba *hba) |
| 8919 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8920 | int ret = 0; |
| 8921 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8922 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8923 | if (!hba) |
| 8924 | return -EINVAL; |
| 8925 | |
| 8926 | if (!hba->is_powered) |
| 8927 | goto out; |
| 8928 | else |
| 8929 | ret = ufshcd_suspend(hba, UFS_RUNTIME_PM); |
| 8930 | out: |
| 8931 | trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret, |
| 8932 | ktime_to_us(ktime_sub(ktime_get(), start)), |
| 8933 | hba->curr_dev_pwr_mode, |
| 8934 | hba->uic_link_state); |
| 8935 | return ret; |
| 8936 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8937 | } |
| 8938 | EXPORT_SYMBOL(ufshcd_runtime_suspend); |
| 8939 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8940 | /** |
| 8941 | * ufshcd_runtime_resume - runtime resume routine |
| 8942 | * @hba: per adapter instance |
| 8943 | * |
| 8944 | * This function basically brings the UFS device, UniPro link and controller |
| 8945 | * to active state. Following operations are done in this function: |
| 8946 | * |
| 8947 | * 1. Turn on all the controller related clocks |
| 8948 | * 2. Bring the UniPro link out of Hibernate state |
| 8949 | * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device |
| 8950 | * to active state. |
| 8951 | * 4. If auto-bkops is enabled on the device, disable it. |
| 8952 | * |
| 8953 | * So following would be the possible power state after this function return |
| 8954 | * successfully: |
| 8955 | * S1: UFS device in Active state with VCC rail ON |
| 8956 | * UniPro link in Active state |
| 8957 | * All the UFS/UniPro controller clocks are ON |
| 8958 | * |
| 8959 | * Returns 0 for success and non-zero for failure |
| 8960 | */ |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8961 | int ufshcd_runtime_resume(struct ufs_hba *hba) |
| 8962 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8963 | int ret = 0; |
| 8964 | ktime_t start = ktime_get(); |
| 8965 | |
| 8966 | if (!hba) |
| 8967 | return -EINVAL; |
| 8968 | |
| 8969 | if (!hba->is_powered) |
| 8970 | goto out; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8971 | else |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8972 | ret = ufshcd_resume(hba, UFS_RUNTIME_PM); |
| 8973 | out: |
| 8974 | trace_ufshcd_runtime_resume(dev_name(hba->dev), ret, |
| 8975 | ktime_to_us(ktime_sub(ktime_get(), start)), |
| 8976 | hba->curr_dev_pwr_mode, |
| 8977 | hba->uic_link_state); |
| 8978 | return ret; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8979 | } |
| 8980 | EXPORT_SYMBOL(ufshcd_runtime_resume); |
| 8981 | |
| 8982 | int ufshcd_runtime_idle(struct ufs_hba *hba) |
| 8983 | { |
| 8984 | return 0; |
| 8985 | } |
| 8986 | EXPORT_SYMBOL(ufshcd_runtime_idle); |
| 8987 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8988 | static inline ssize_t ufshcd_pm_lvl_store(struct device *dev, |
| 8989 | struct device_attribute *attr, |
| 8990 | const char *buf, size_t count, |
| 8991 | bool rpm) |
| 8992 | { |
| 8993 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 8994 | unsigned long flags, value; |
| 8995 | |
| 8996 | if (kstrtoul(buf, 0, &value)) |
| 8997 | return -EINVAL; |
| 8998 | |
| 8999 | if (value >= UFS_PM_LVL_MAX) |
| 9000 | return -EINVAL; |
| 9001 | |
| 9002 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 9003 | if (rpm) |
| 9004 | hba->rpm_lvl = value; |
| 9005 | else |
| 9006 | hba->spm_lvl = value; |
| 9007 | ufshcd_apply_pm_quirks(hba); |
| 9008 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 9009 | return count; |
| 9010 | } |
| 9011 | |
| 9012 | static ssize_t ufshcd_rpm_lvl_show(struct device *dev, |
| 9013 | struct device_attribute *attr, char *buf) |
| 9014 | { |
| 9015 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 9016 | int curr_len; |
| 9017 | u8 lvl; |
| 9018 | |
| 9019 | curr_len = snprintf(buf, PAGE_SIZE, |
| 9020 | "\nCurrent Runtime PM level [%d] => dev_state [%s] link_state [%s]\n", |
| 9021 | hba->rpm_lvl, |
| 9022 | ufschd_ufs_dev_pwr_mode_to_string( |
| 9023 | ufs_pm_lvl_states[hba->rpm_lvl].dev_state), |
| 9024 | ufschd_uic_link_state_to_string( |
| 9025 | ufs_pm_lvl_states[hba->rpm_lvl].link_state)); |
| 9026 | |
| 9027 | curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len), |
| 9028 | "\nAll available Runtime PM levels info:\n"); |
| 9029 | for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) |
| 9030 | curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len), |
| 9031 | "\tRuntime PM level [%d] => dev_state [%s] link_state [%s]\n", |
| 9032 | lvl, |
| 9033 | ufschd_ufs_dev_pwr_mode_to_string( |
| 9034 | ufs_pm_lvl_states[lvl].dev_state), |
| 9035 | ufschd_uic_link_state_to_string( |
| 9036 | ufs_pm_lvl_states[lvl].link_state)); |
| 9037 | |
| 9038 | return curr_len; |
| 9039 | } |
| 9040 | |
| 9041 | static ssize_t ufshcd_rpm_lvl_store(struct device *dev, |
| 9042 | struct device_attribute *attr, const char *buf, size_t count) |
| 9043 | { |
| 9044 | return ufshcd_pm_lvl_store(dev, attr, buf, count, true); |
| 9045 | } |
| 9046 | |
| 9047 | static void ufshcd_add_rpm_lvl_sysfs_nodes(struct ufs_hba *hba) |
| 9048 | { |
| 9049 | hba->rpm_lvl_attr.show = ufshcd_rpm_lvl_show; |
| 9050 | hba->rpm_lvl_attr.store = ufshcd_rpm_lvl_store; |
| 9051 | sysfs_attr_init(&hba->rpm_lvl_attr.attr); |
| 9052 | hba->rpm_lvl_attr.attr.name = "rpm_lvl"; |
| 9053 | hba->rpm_lvl_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 9054 | if (device_create_file(hba->dev, &hba->rpm_lvl_attr)) |
| 9055 | dev_err(hba->dev, "Failed to create sysfs for rpm_lvl\n"); |
| 9056 | } |
| 9057 | |
| 9058 | static ssize_t ufshcd_spm_lvl_show(struct device *dev, |
| 9059 | struct device_attribute *attr, char *buf) |
| 9060 | { |
| 9061 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 9062 | int curr_len; |
| 9063 | u8 lvl; |
| 9064 | |
| 9065 | curr_len = snprintf(buf, PAGE_SIZE, |
| 9066 | "\nCurrent System PM level [%d] => dev_state [%s] link_state [%s]\n", |
| 9067 | hba->spm_lvl, |
| 9068 | ufschd_ufs_dev_pwr_mode_to_string( |
| 9069 | ufs_pm_lvl_states[hba->spm_lvl].dev_state), |
| 9070 | ufschd_uic_link_state_to_string( |
| 9071 | ufs_pm_lvl_states[hba->spm_lvl].link_state)); |
| 9072 | |
| 9073 | curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len), |
| 9074 | "\nAll available System PM levels info:\n"); |
| 9075 | for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) |
| 9076 | curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len), |
| 9077 | "\tSystem PM level [%d] => dev_state [%s] link_state [%s]\n", |
| 9078 | lvl, |
| 9079 | ufschd_ufs_dev_pwr_mode_to_string( |
| 9080 | ufs_pm_lvl_states[lvl].dev_state), |
| 9081 | ufschd_uic_link_state_to_string( |
| 9082 | ufs_pm_lvl_states[lvl].link_state)); |
| 9083 | |
| 9084 | return curr_len; |
| 9085 | } |
| 9086 | |
| 9087 | static ssize_t ufshcd_spm_lvl_store(struct device *dev, |
| 9088 | struct device_attribute *attr, const char *buf, size_t count) |
| 9089 | { |
| 9090 | return ufshcd_pm_lvl_store(dev, attr, buf, count, false); |
| 9091 | } |
| 9092 | |
| 9093 | static void ufshcd_add_spm_lvl_sysfs_nodes(struct ufs_hba *hba) |
| 9094 | { |
| 9095 | hba->spm_lvl_attr.show = ufshcd_spm_lvl_show; |
| 9096 | hba->spm_lvl_attr.store = ufshcd_spm_lvl_store; |
| 9097 | sysfs_attr_init(&hba->spm_lvl_attr.attr); |
| 9098 | hba->spm_lvl_attr.attr.name = "spm_lvl"; |
| 9099 | hba->spm_lvl_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 9100 | if (device_create_file(hba->dev, &hba->spm_lvl_attr)) |
| 9101 | dev_err(hba->dev, "Failed to create sysfs for spm_lvl\n"); |
| 9102 | } |
| 9103 | |
| 9104 | static inline void ufshcd_add_sysfs_nodes(struct ufs_hba *hba) |
| 9105 | { |
| 9106 | ufshcd_add_rpm_lvl_sysfs_nodes(hba); |
| 9107 | ufshcd_add_spm_lvl_sysfs_nodes(hba); |
| 9108 | } |
| 9109 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 9110 | static void ufshcd_shutdown_clkscaling(struct ufs_hba *hba) |
| 9111 | { |
| 9112 | bool suspend = false; |
| 9113 | unsigned long flags; |
| 9114 | |
| 9115 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 9116 | if (hba->clk_scaling.is_allowed) { |
| 9117 | hba->clk_scaling.is_allowed = false; |
| 9118 | suspend = true; |
| 9119 | } |
| 9120 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 9121 | |
| 9122 | /** |
| 9123 | * Scaling may be scheduled before, hence make sure it |
| 9124 | * doesn't race with shutdown |
| 9125 | */ |
| 9126 | if (ufshcd_is_clkscaling_supported(hba)) { |
| 9127 | device_remove_file(hba->dev, &hba->clk_scaling.enable_attr); |
| 9128 | cancel_work_sync(&hba->clk_scaling.suspend_work); |
| 9129 | cancel_work_sync(&hba->clk_scaling.resume_work); |
| 9130 | if (suspend) |
| 9131 | ufshcd_suspend_clkscaling(hba); |
| 9132 | } |
| 9133 | |
| 9134 | /* Unregister so that devfreq_monitor can't race with shutdown */ |
| 9135 | if (hba->devfreq) |
| 9136 | devfreq_remove_device(hba->devfreq); |
| 9137 | } |
| 9138 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9139 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9140 | * ufshcd_shutdown - shutdown routine |
| 9141 | * @hba: per adapter instance |
| 9142 | * |
| 9143 | * This function would power off both UFS device and UFS link. |
| 9144 | * |
| 9145 | * Returns 0 always to allow force shutdown even in case of errors. |
| 9146 | */ |
| 9147 | int ufshcd_shutdown(struct ufs_hba *hba) |
| 9148 | { |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 9149 | int ret = 0; |
| 9150 | |
| 9151 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba)) |
| 9152 | goto out; |
| 9153 | |
| 9154 | pm_runtime_get_sync(hba->dev); |
| 9155 | ufshcd_hold_all(hba); |
| 9156 | ufshcd_mark_shutdown_ongoing(hba); |
| 9157 | ufshcd_shutdown_clkscaling(hba); |
| 9158 | /** |
| 9159 | * (1) Acquire the lock to stop any more requests |
| 9160 | * (2) Wait for all issued requests to complete |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9161 | */ |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 9162 | ufshcd_get_write_lock(hba); |
| 9163 | ufshcd_scsi_block_requests(hba); |
| 9164 | ret = ufshcd_wait_for_doorbell_clr(hba, U64_MAX); |
| 9165 | if (ret) |
| 9166 | dev_err(hba->dev, "%s: waiting for DB clear: failed: %d\n", |
| 9167 | __func__, ret); |
| 9168 | /* Requests may have errored out above, let it be handled */ |
| 9169 | flush_work(&hba->eh_work); |
| 9170 | /* reqs issued from contexts other than shutdown will fail from now */ |
| 9171 | ufshcd_scsi_unblock_requests(hba); |
| 9172 | ufshcd_release_all(hba); |
| 9173 | ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM); |
| 9174 | out: |
| 9175 | if (ret) |
| 9176 | dev_err(hba->dev, "%s failed, err %d\n", __func__, ret); |
| 9177 | /* allow force shutdown even in case of errors */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9178 | return 0; |
| 9179 | } |
| 9180 | EXPORT_SYMBOL(ufshcd_shutdown); |
| 9181 | |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 9182 | /* |
| 9183 | * Values permitted 0, 1, 2. |
| 9184 | * 0 -> Disable IO latency histograms (default) |
| 9185 | * 1 -> Enable IO latency histograms |
| 9186 | * 2 -> Zero out IO latency histograms |
| 9187 | */ |
| 9188 | static ssize_t |
| 9189 | latency_hist_store(struct device *dev, struct device_attribute *attr, |
| 9190 | const char *buf, size_t count) |
| 9191 | { |
| 9192 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 9193 | long value; |
| 9194 | |
| 9195 | if (kstrtol(buf, 0, &value)) |
| 9196 | return -EINVAL; |
| 9197 | if (value == BLK_IO_LAT_HIST_ZERO) |
| 9198 | blk_zero_latency_hist(&hba->io_lat_s); |
| 9199 | else if (value == BLK_IO_LAT_HIST_ENABLE || |
| 9200 | value == BLK_IO_LAT_HIST_DISABLE) |
| 9201 | hba->latency_hist_enabled = value; |
| 9202 | return count; |
| 9203 | } |
| 9204 | |
| 9205 | ssize_t |
| 9206 | latency_hist_show(struct device *dev, struct device_attribute *attr, |
| 9207 | char *buf) |
| 9208 | { |
| 9209 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 9210 | |
| 9211 | return blk_latency_hist_show(&hba->io_lat_s, buf); |
| 9212 | } |
| 9213 | |
| 9214 | static DEVICE_ATTR(latency_hist, S_IRUGO | S_IWUSR, |
| 9215 | latency_hist_show, latency_hist_store); |
| 9216 | |
| 9217 | static void |
| 9218 | ufshcd_init_latency_hist(struct ufs_hba *hba) |
| 9219 | { |
| 9220 | if (device_create_file(hba->dev, &dev_attr_latency_hist)) |
| 9221 | dev_err(hba->dev, "Failed to create latency_hist sysfs entry\n"); |
| 9222 | } |
| 9223 | |
| 9224 | static void |
| 9225 | ufshcd_exit_latency_hist(struct ufs_hba *hba) |
| 9226 | { |
| 9227 | device_create_file(hba->dev, &dev_attr_latency_hist); |
| 9228 | } |
| 9229 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9230 | /** |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9231 | * ufshcd_remove - de-allocate SCSI host and host memory space |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9232 | * data structure memory |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9233 | * @hba - per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9234 | */ |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9235 | void ufshcd_remove(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9236 | { |
Akinobu Mita | cfdf9c9 | 2013-07-30 00:36:03 +0530 | [diff] [blame] | 9237 | scsi_remove_host(hba->host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9238 | /* disable interrupts */ |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 9239 | ufshcd_disable_intr(hba, hba->intr_mask); |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 9240 | ufshcd_hba_stop(hba, true); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9241 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 9242 | ufshcd_exit_clk_gating(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9243 | ufshcd_exit_hibern8_on_idle(hba); |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 9244 | ufshcd_exit_latency_hist(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9245 | if (ufshcd_is_clkscaling_supported(hba)) { |
| 9246 | device_remove_file(hba->dev, &hba->clk_scaling.enable_attr); |
Kyle Yan | 65be4a5 | 2016-10-31 15:05:00 -0700 | [diff] [blame] | 9247 | if (hba->devfreq) |
| 9248 | devfreq_remove_device(hba->devfreq); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9249 | } |
Channagoud Kadabi | 8810e5f | 2017-02-17 16:01:05 -0800 | [diff] [blame] | 9250 | |
| 9251 | ufshcd_exit_latency_hist(hba); |
| 9252 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 9253 | ufshcd_hba_exit(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9254 | ufsdbg_remove_debugfs(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9255 | } |
| 9256 | EXPORT_SYMBOL_GPL(ufshcd_remove); |
| 9257 | |
| 9258 | /** |
Yaniv Gardi | 47555a5 | 2015-10-28 13:15:49 +0200 | [diff] [blame] | 9259 | * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA) |
| 9260 | * @hba: pointer to Host Bus Adapter (HBA) |
| 9261 | */ |
| 9262 | void ufshcd_dealloc_host(struct ufs_hba *hba) |
| 9263 | { |
| 9264 | scsi_host_put(hba->host); |
| 9265 | } |
| 9266 | EXPORT_SYMBOL_GPL(ufshcd_dealloc_host); |
| 9267 | |
| 9268 | /** |
Akinobu Mita | ca3d7bf | 2014-07-13 21:24:46 +0900 | [diff] [blame] | 9269 | * ufshcd_set_dma_mask - Set dma mask based on the controller |
| 9270 | * addressing capability |
| 9271 | * @hba: per adapter instance |
| 9272 | * |
| 9273 | * Returns 0 for success, non-zero for failure |
| 9274 | */ |
| 9275 | static int ufshcd_set_dma_mask(struct ufs_hba *hba) |
| 9276 | { |
| 9277 | if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) { |
| 9278 | if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64))) |
| 9279 | return 0; |
| 9280 | } |
| 9281 | return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32)); |
| 9282 | } |
| 9283 | |
| 9284 | /** |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 9285 | * ufshcd_alloc_host - allocate Host Bus Adapter (HBA) |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9286 | * @dev: pointer to device handle |
| 9287 | * @hba_handle: driver private handle |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9288 | * Returns 0 on success, non-zero value on failure |
| 9289 | */ |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 9290 | int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9291 | { |
| 9292 | struct Scsi_Host *host; |
| 9293 | struct ufs_hba *hba; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 9294 | int err = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9295 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9296 | if (!dev) { |
| 9297 | dev_err(dev, |
| 9298 | "Invalid memory reference for dev is NULL\n"); |
| 9299 | err = -ENODEV; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9300 | goto out_error; |
| 9301 | } |
| 9302 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9303 | host = scsi_host_alloc(&ufshcd_driver_template, |
| 9304 | sizeof(struct ufs_hba)); |
| 9305 | if (!host) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9306 | dev_err(dev, "scsi_host_alloc failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9307 | err = -ENOMEM; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9308 | goto out_error; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9309 | } |
| 9310 | hba = shost_priv(host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9311 | hba->host = host; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9312 | hba->dev = dev; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 9313 | *hba_handle = hba; |
| 9314 | |
| 9315 | out_error: |
| 9316 | return err; |
| 9317 | } |
| 9318 | EXPORT_SYMBOL(ufshcd_alloc_host); |
| 9319 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9320 | /** |
| 9321 | * ufshcd_is_devfreq_scaling_required - check if scaling is required or not |
| 9322 | * @hba: per adapter instance |
| 9323 | * @scale_up: True if scaling up and false if scaling down |
| 9324 | * |
| 9325 | * Returns true if scaling is required, false otherwise. |
| 9326 | */ |
| 9327 | static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba, |
| 9328 | bool scale_up) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9329 | { |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9330 | struct ufs_clk_info *clki; |
| 9331 | struct list_head *head = &hba->clk_list_head; |
| 9332 | |
| 9333 | if (!head || list_empty(head)) |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9334 | return false; |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 9335 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9336 | list_for_each_entry(clki, head, list) { |
| 9337 | if (!IS_ERR_OR_NULL(clki->clk)) { |
| 9338 | if (scale_up && clki->max_freq) { |
| 9339 | if (clki->curr_freq == clki->max_freq) |
| 9340 | continue; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9341 | return true; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9342 | } else if (!scale_up && clki->min_freq) { |
| 9343 | if (clki->curr_freq == clki->min_freq) |
| 9344 | continue; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9345 | return true; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9346 | } |
| 9347 | } |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9348 | } |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 9349 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9350 | return false; |
| 9351 | } |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 9352 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9353 | /** |
| 9354 | * ufshcd_scale_gear - scale up/down UFS gear |
| 9355 | * @hba: per adapter instance |
| 9356 | * @scale_up: True for scaling up gear and false for scaling down |
| 9357 | * |
| 9358 | * Returns 0 for success, |
| 9359 | * Returns -EBUSY if scaling can't happen at this time |
| 9360 | * Returns non-zero for any other errors |
| 9361 | */ |
| 9362 | static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up) |
| 9363 | { |
| 9364 | int ret = 0; |
| 9365 | struct ufs_pa_layer_attr new_pwr_info; |
| 9366 | u32 scale_down_gear = ufshcd_vops_get_scale_down_gear(hba); |
| 9367 | |
| 9368 | BUG_ON(!hba->clk_scaling.saved_pwr_info.is_valid); |
| 9369 | |
| 9370 | if (scale_up) { |
| 9371 | memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info.info, |
| 9372 | sizeof(struct ufs_pa_layer_attr)); |
| 9373 | } else { |
| 9374 | memcpy(&new_pwr_info, &hba->pwr_info, |
| 9375 | sizeof(struct ufs_pa_layer_attr)); |
| 9376 | |
| 9377 | if (hba->pwr_info.gear_tx > scale_down_gear |
| 9378 | || hba->pwr_info.gear_rx > scale_down_gear) { |
| 9379 | /* save the current power mode */ |
| 9380 | memcpy(&hba->clk_scaling.saved_pwr_info.info, |
| 9381 | &hba->pwr_info, |
| 9382 | sizeof(struct ufs_pa_layer_attr)); |
| 9383 | |
| 9384 | /* scale down gear */ |
| 9385 | new_pwr_info.gear_tx = scale_down_gear; |
| 9386 | new_pwr_info.gear_rx = scale_down_gear; |
Subhash Jadavani | 4f0df17b | 2016-12-16 13:19:27 -0800 | [diff] [blame] | 9387 | if (!(hba->dev_info.quirks & UFS_DEVICE_NO_FASTAUTO)) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9388 | new_pwr_info.pwr_tx = FASTAUTO_MODE; |
| 9389 | new_pwr_info.pwr_rx = FASTAUTO_MODE; |
| 9390 | } |
| 9391 | } |
| 9392 | } |
| 9393 | |
| 9394 | ret = ufshcd_change_power_mode(hba, &new_pwr_info); |
| 9395 | |
| 9396 | if (ret) |
| 9397 | dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d), scale_up = %d", |
| 9398 | __func__, ret, |
| 9399 | hba->pwr_info.gear_tx, hba->pwr_info.gear_rx, |
| 9400 | new_pwr_info.gear_tx, new_pwr_info.gear_rx, |
| 9401 | scale_up); |
| 9402 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9403 | return ret; |
| 9404 | } |
| 9405 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9406 | static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba) |
| 9407 | { |
| 9408 | #define DOORBELL_CLR_TOUT_US (1000 * 1000) /* 1 sec */ |
| 9409 | int ret = 0; |
| 9410 | /* |
| 9411 | * make sure that there are no outstanding requests when |
| 9412 | * clock scaling is in progress |
| 9413 | */ |
| 9414 | ufshcd_scsi_block_requests(hba); |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 9415 | down_write(&hba->lock); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9416 | if (ufshcd_wait_for_doorbell_clr(hba, DOORBELL_CLR_TOUT_US)) { |
| 9417 | ret = -EBUSY; |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 9418 | up_write(&hba->lock); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9419 | ufshcd_scsi_unblock_requests(hba); |
| 9420 | } |
| 9421 | |
| 9422 | return ret; |
| 9423 | } |
| 9424 | |
| 9425 | static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba) |
| 9426 | { |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 9427 | up_write(&hba->lock); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9428 | ufshcd_scsi_unblock_requests(hba); |
| 9429 | } |
| 9430 | |
| 9431 | /** |
| 9432 | * ufshcd_devfreq_scale - scale up/down UFS clocks and gear |
| 9433 | * @hba: per adapter instance |
| 9434 | * @scale_up: True for scaling up and false for scalin down |
| 9435 | * |
| 9436 | * Returns 0 for success, |
| 9437 | * Returns -EBUSY if scaling can't happen at this time |
| 9438 | * Returns non-zero for any other errors |
| 9439 | */ |
| 9440 | static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up) |
| 9441 | { |
| 9442 | int ret = 0; |
| 9443 | |
| 9444 | /* let's not get into low power until clock scaling is completed */ |
| 9445 | ufshcd_hold_all(hba); |
| 9446 | |
| 9447 | ret = ufshcd_clock_scaling_prepare(hba); |
| 9448 | if (ret) |
| 9449 | goto out; |
| 9450 | |
| 9451 | /* scale down the gear before scaling down clocks */ |
| 9452 | if (!scale_up) { |
| 9453 | ret = ufshcd_scale_gear(hba, false); |
| 9454 | if (ret) |
| 9455 | goto clk_scaling_unprepare; |
| 9456 | } |
| 9457 | |
Subhash Jadavani | 67c5330 | 2017-03-22 17:00:54 -0700 | [diff] [blame^] | 9458 | /* |
| 9459 | * If auto hibern8 is supported then put the link in |
| 9460 | * hibern8 manually, this is to avoid auto hibern8 |
| 9461 | * racing during clock frequency scaling sequence. |
| 9462 | */ |
| 9463 | if (ufshcd_is_auto_hibern8_supported(hba)) { |
| 9464 | ret = ufshcd_uic_hibern8_enter(hba); |
| 9465 | if (ret) |
| 9466 | /* link will be bad state so no need to scale_up_gear */ |
| 9467 | return ret; |
| 9468 | } |
| 9469 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9470 | ret = ufshcd_scale_clks(hba, scale_up); |
| 9471 | if (ret) |
| 9472 | goto scale_up_gear; |
| 9473 | |
Subhash Jadavani | 67c5330 | 2017-03-22 17:00:54 -0700 | [diff] [blame^] | 9474 | if (ufshcd_is_auto_hibern8_supported(hba)) { |
| 9475 | ret = ufshcd_uic_hibern8_exit(hba); |
| 9476 | if (ret) |
| 9477 | /* link will be bad state so no need to scale_up_gear */ |
| 9478 | return ret; |
| 9479 | } |
| 9480 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9481 | /* scale up the gear after scaling up clocks */ |
| 9482 | if (scale_up) { |
| 9483 | ret = ufshcd_scale_gear(hba, true); |
| 9484 | if (ret) { |
| 9485 | ufshcd_scale_clks(hba, false); |
| 9486 | goto clk_scaling_unprepare; |
| 9487 | } |
| 9488 | } |
| 9489 | |
| 9490 | if (!ret) { |
| 9491 | hba->clk_scaling.is_scaled_up = scale_up; |
| 9492 | if (scale_up) |
| 9493 | hba->clk_gating.delay_ms = |
| 9494 | hba->clk_gating.delay_ms_perf; |
| 9495 | else |
| 9496 | hba->clk_gating.delay_ms = |
| 9497 | hba->clk_gating.delay_ms_pwr_save; |
| 9498 | } |
| 9499 | |
| 9500 | goto clk_scaling_unprepare; |
| 9501 | |
| 9502 | scale_up_gear: |
| 9503 | if (!scale_up) |
| 9504 | ufshcd_scale_gear(hba, true); |
| 9505 | clk_scaling_unprepare: |
| 9506 | ufshcd_clock_scaling_unprepare(hba); |
| 9507 | out: |
| 9508 | ufshcd_release_all(hba); |
| 9509 | return ret; |
| 9510 | } |
| 9511 | |
| 9512 | static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba) |
| 9513 | { |
| 9514 | unsigned long flags; |
| 9515 | |
| 9516 | devfreq_suspend_device(hba->devfreq); |
| 9517 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 9518 | hba->clk_scaling.window_start_t = 0; |
| 9519 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 9520 | } |
| 9521 | |
| 9522 | static void ufshcd_suspend_clkscaling(struct ufs_hba *hba) |
| 9523 | { |
| 9524 | unsigned long flags; |
| 9525 | bool suspend = false; |
| 9526 | |
| 9527 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 9528 | return; |
| 9529 | |
| 9530 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 9531 | if (!hba->clk_scaling.is_suspended) { |
| 9532 | suspend = true; |
| 9533 | hba->clk_scaling.is_suspended = true; |
| 9534 | } |
| 9535 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 9536 | |
| 9537 | if (suspend) |
| 9538 | __ufshcd_suspend_clkscaling(hba); |
| 9539 | } |
| 9540 | |
| 9541 | static void ufshcd_resume_clkscaling(struct ufs_hba *hba) |
| 9542 | { |
| 9543 | unsigned long flags; |
| 9544 | bool resume = false; |
| 9545 | |
| 9546 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 9547 | return; |
| 9548 | |
| 9549 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 9550 | if (hba->clk_scaling.is_suspended) { |
| 9551 | resume = true; |
| 9552 | hba->clk_scaling.is_suspended = false; |
| 9553 | } |
| 9554 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 9555 | |
| 9556 | if (resume) |
| 9557 | devfreq_resume_device(hba->devfreq); |
| 9558 | } |
| 9559 | |
| 9560 | static ssize_t ufshcd_clkscale_enable_show(struct device *dev, |
| 9561 | struct device_attribute *attr, char *buf) |
| 9562 | { |
| 9563 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 9564 | |
| 9565 | return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_scaling.is_allowed); |
| 9566 | } |
| 9567 | |
| 9568 | static ssize_t ufshcd_clkscale_enable_store(struct device *dev, |
| 9569 | struct device_attribute *attr, const char *buf, size_t count) |
| 9570 | { |
| 9571 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 9572 | u32 value; |
| 9573 | int err; |
| 9574 | |
| 9575 | if (kstrtou32(buf, 0, &value)) |
| 9576 | return -EINVAL; |
| 9577 | |
| 9578 | value = !!value; |
| 9579 | if (value == hba->clk_scaling.is_allowed) |
| 9580 | goto out; |
| 9581 | |
| 9582 | pm_runtime_get_sync(hba->dev); |
| 9583 | ufshcd_hold(hba, false); |
| 9584 | |
| 9585 | cancel_work_sync(&hba->clk_scaling.suspend_work); |
| 9586 | cancel_work_sync(&hba->clk_scaling.resume_work); |
| 9587 | |
| 9588 | hba->clk_scaling.is_allowed = value; |
| 9589 | |
| 9590 | if (value) { |
| 9591 | ufshcd_resume_clkscaling(hba); |
| 9592 | } else { |
| 9593 | ufshcd_suspend_clkscaling(hba); |
| 9594 | err = ufshcd_devfreq_scale(hba, true); |
| 9595 | if (err) |
| 9596 | dev_err(hba->dev, "%s: failed to scale clocks up %d\n", |
| 9597 | __func__, err); |
| 9598 | } |
| 9599 | |
| 9600 | ufshcd_release(hba, false); |
| 9601 | pm_runtime_put_sync(hba->dev); |
| 9602 | out: |
| 9603 | return count; |
| 9604 | } |
| 9605 | |
| 9606 | static void ufshcd_clk_scaling_suspend_work(struct work_struct *work) |
| 9607 | { |
| 9608 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 9609 | clk_scaling.suspend_work); |
| 9610 | unsigned long irq_flags; |
| 9611 | |
| 9612 | spin_lock_irqsave(hba->host->host_lock, irq_flags); |
| 9613 | if (hba->clk_scaling.active_reqs || hba->clk_scaling.is_suspended) { |
| 9614 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 9615 | return; |
| 9616 | } |
| 9617 | hba->clk_scaling.is_suspended = true; |
| 9618 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 9619 | |
| 9620 | __ufshcd_suspend_clkscaling(hba); |
| 9621 | } |
| 9622 | |
| 9623 | static void ufshcd_clk_scaling_resume_work(struct work_struct *work) |
| 9624 | { |
| 9625 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 9626 | clk_scaling.resume_work); |
| 9627 | unsigned long irq_flags; |
| 9628 | |
| 9629 | spin_lock_irqsave(hba->host->host_lock, irq_flags); |
| 9630 | if (!hba->clk_scaling.is_suspended) { |
| 9631 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 9632 | return; |
| 9633 | } |
| 9634 | hba->clk_scaling.is_suspended = false; |
| 9635 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 9636 | |
| 9637 | devfreq_resume_device(hba->devfreq); |
| 9638 | } |
| 9639 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9640 | static int ufshcd_devfreq_target(struct device *dev, |
| 9641 | unsigned long *freq, u32 flags) |
| 9642 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9643 | int ret = 0; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9644 | struct ufs_hba *hba = dev_get_drvdata(dev); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9645 | unsigned long irq_flags; |
| 9646 | ktime_t start; |
| 9647 | bool scale_up, sched_clk_scaling_suspend_work = false; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9648 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9649 | if (!ufshcd_is_clkscaling_supported(hba)) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9650 | return -EINVAL; |
| 9651 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9652 | if ((*freq > 0) && (*freq < UINT_MAX)) { |
| 9653 | dev_err(hba->dev, "%s: invalid freq = %lu\n", __func__, *freq); |
| 9654 | return -EINVAL; |
| 9655 | } |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9656 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9657 | spin_lock_irqsave(hba->host->host_lock, irq_flags); |
| 9658 | if (ufshcd_eh_in_progress(hba)) { |
| 9659 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 9660 | return 0; |
| 9661 | } |
| 9662 | |
| 9663 | if (!hba->clk_scaling.active_reqs) |
| 9664 | sched_clk_scaling_suspend_work = true; |
| 9665 | |
| 9666 | scale_up = (*freq == UINT_MAX) ? true : false; |
| 9667 | if (!ufshcd_is_devfreq_scaling_required(hba, scale_up)) { |
| 9668 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 9669 | ret = 0; |
| 9670 | goto out; /* no state change required */ |
| 9671 | } |
| 9672 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 9673 | |
| 9674 | start = ktime_get(); |
| 9675 | ret = ufshcd_devfreq_scale(hba, scale_up); |
| 9676 | trace_ufshcd_profile_clk_scaling(dev_name(hba->dev), |
| 9677 | (scale_up ? "up" : "down"), |
| 9678 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
| 9679 | |
| 9680 | out: |
| 9681 | if (sched_clk_scaling_suspend_work) |
| 9682 | queue_work(hba->clk_scaling.workq, |
| 9683 | &hba->clk_scaling.suspend_work); |
| 9684 | |
| 9685 | return ret; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9686 | } |
| 9687 | |
| 9688 | static int ufshcd_devfreq_get_dev_status(struct device *dev, |
| 9689 | struct devfreq_dev_status *stat) |
| 9690 | { |
| 9691 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 9692 | struct ufs_clk_scaling *scaling = &hba->clk_scaling; |
| 9693 | unsigned long flags; |
| 9694 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9695 | if (!ufshcd_is_clkscaling_supported(hba)) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9696 | return -EINVAL; |
| 9697 | |
| 9698 | memset(stat, 0, sizeof(*stat)); |
| 9699 | |
| 9700 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 9701 | if (!scaling->window_start_t) |
| 9702 | goto start_window; |
| 9703 | |
| 9704 | if (scaling->is_busy_started) |
| 9705 | scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(), |
| 9706 | scaling->busy_start_t)); |
| 9707 | |
| 9708 | stat->total_time = jiffies_to_usecs((long)jiffies - |
| 9709 | (long)scaling->window_start_t); |
| 9710 | stat->busy_time = scaling->tot_busy_t; |
| 9711 | start_window: |
| 9712 | scaling->window_start_t = jiffies; |
| 9713 | scaling->tot_busy_t = 0; |
| 9714 | |
| 9715 | if (hba->outstanding_reqs) { |
| 9716 | scaling->busy_start_t = ktime_get(); |
| 9717 | scaling->is_busy_started = true; |
| 9718 | } else { |
| 9719 | scaling->busy_start_t = ktime_set(0, 0); |
| 9720 | scaling->is_busy_started = false; |
| 9721 | } |
| 9722 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 9723 | return 0; |
| 9724 | } |
| 9725 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9726 | static void ufshcd_clkscaling_init_sysfs(struct ufs_hba *hba) |
| 9727 | { |
| 9728 | hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show; |
| 9729 | hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store; |
| 9730 | sysfs_attr_init(&hba->clk_scaling.enable_attr.attr); |
| 9731 | hba->clk_scaling.enable_attr.attr.name = "clkscale_enable"; |
| 9732 | hba->clk_scaling.enable_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 9733 | if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr)) |
| 9734 | dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n"); |
| 9735 | } |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9736 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9737 | static void ufshcd_init_lanes_per_dir(struct ufs_hba *hba) |
| 9738 | { |
| 9739 | struct device *dev = hba->dev; |
| 9740 | int ret; |
| 9741 | |
| 9742 | ret = of_property_read_u32(dev->of_node, "lanes-per-direction", |
| 9743 | &hba->lanes_per_direction); |
| 9744 | if (ret) { |
| 9745 | dev_dbg(hba->dev, |
| 9746 | "%s: failed to read lanes-per-direction, ret=%d\n", |
| 9747 | __func__, ret); |
| 9748 | hba->lanes_per_direction = UFSHCD_DEFAULT_LANES_PER_DIRECTION; |
| 9749 | } |
| 9750 | } |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 9751 | /** |
| 9752 | * ufshcd_init - Driver initialization routine |
| 9753 | * @hba: per-adapter instance |
| 9754 | * @mmio_base: base register address |
| 9755 | * @irq: Interrupt line of device |
| 9756 | * Returns 0 on success, non-zero value on failure |
| 9757 | */ |
| 9758 | int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq) |
| 9759 | { |
| 9760 | int err; |
| 9761 | struct Scsi_Host *host = hba->host; |
| 9762 | struct device *dev = hba->dev; |
| 9763 | |
| 9764 | if (!mmio_base) { |
| 9765 | dev_err(hba->dev, |
| 9766 | "Invalid memory reference for mmio_base is NULL\n"); |
| 9767 | err = -ENODEV; |
| 9768 | goto out_error; |
| 9769 | } |
| 9770 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9771 | hba->mmio_base = mmio_base; |
| 9772 | hba->irq = irq; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9773 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9774 | ufshcd_init_lanes_per_dir(hba); |
| 9775 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 9776 | err = ufshcd_hba_init(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 9777 | if (err) |
| 9778 | goto out_error; |
| 9779 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9780 | /* Read capabilities registers */ |
| 9781 | ufshcd_hba_capabilities(hba); |
| 9782 | |
| 9783 | /* Get UFS version supported by the controller */ |
| 9784 | hba->ufs_version = ufshcd_get_ufs_version(hba); |
| 9785 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9786 | /* print error message if ufs_version is not valid */ |
| 9787 | if ((hba->ufs_version != UFSHCI_VERSION_10) && |
| 9788 | (hba->ufs_version != UFSHCI_VERSION_11) && |
| 9789 | (hba->ufs_version != UFSHCI_VERSION_20) && |
| 9790 | (hba->ufs_version != UFSHCI_VERSION_21)) |
| 9791 | dev_err(hba->dev, "invalid UFS version 0x%x\n", |
| 9792 | hba->ufs_version); |
| 9793 | |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 9794 | /* Get Interrupt bit mask per version */ |
| 9795 | hba->intr_mask = ufshcd_get_intr_mask(hba); |
| 9796 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9797 | /* Enable debug prints */ |
| 9798 | hba->ufshcd_dbg_print = DEFAULT_UFSHCD_DBG_PRINT_EN; |
| 9799 | |
Akinobu Mita | ca3d7bf | 2014-07-13 21:24:46 +0900 | [diff] [blame] | 9800 | err = ufshcd_set_dma_mask(hba); |
| 9801 | if (err) { |
| 9802 | dev_err(hba->dev, "set dma mask failed\n"); |
| 9803 | goto out_disable; |
| 9804 | } |
| 9805 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9806 | /* Allocate memory for host memory space */ |
| 9807 | err = ufshcd_memory_alloc(hba); |
| 9808 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9809 | dev_err(hba->dev, "Memory allocation failed\n"); |
| 9810 | goto out_disable; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9811 | } |
| 9812 | |
| 9813 | /* Configure LRB */ |
| 9814 | ufshcd_host_memory_configure(hba); |
| 9815 | |
| 9816 | host->can_queue = hba->nutrs; |
| 9817 | host->cmd_per_lun = hba->nutrs; |
| 9818 | host->max_id = UFSHCD_MAX_ID; |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 9819 | host->max_lun = UFS_MAX_LUNS; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9820 | host->max_channel = UFSHCD_MAX_CHANNEL; |
| 9821 | host->unique_id = host->host_no; |
| 9822 | host->max_cmd_len = MAX_CDB_SIZE; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9823 | host->set_dbd_for_caching = 1; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9824 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 9825 | hba->max_pwr_info.is_valid = false; |
| 9826 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9827 | /* Initailize wait queue for task management */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 9828 | init_waitqueue_head(&hba->tm_wq); |
| 9829 | init_waitqueue_head(&hba->tm_tag_wq); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9830 | |
| 9831 | /* Initialize work queues */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 9832 | INIT_WORK(&hba->eh_work, ufshcd_err_handler); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 9833 | INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9834 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 9835 | /* Initialize UIC command mutex */ |
| 9836 | mutex_init(&hba->uic_cmd_mutex); |
| 9837 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 9838 | /* Initialize mutex for device management commands */ |
| 9839 | mutex_init(&hba->dev_cmd.lock); |
| 9840 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 9841 | init_rwsem(&hba->lock); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9842 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 9843 | /* Initialize device management tag acquire wait queue */ |
| 9844 | init_waitqueue_head(&hba->dev_cmd.tag_wq); |
| 9845 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 9846 | ufshcd_init_clk_gating(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9847 | ufshcd_init_hibern8_on_idle(hba); |
Yaniv Gardi | 199ef13 | 2016-03-10 17:37:06 +0200 | [diff] [blame] | 9848 | |
| 9849 | /* |
| 9850 | * In order to avoid any spurious interrupt immediately after |
| 9851 | * registering UFS controller interrupt handler, clear any pending UFS |
| 9852 | * interrupt status and disable all the UFS interrupts. |
| 9853 | */ |
| 9854 | ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS), |
| 9855 | REG_INTERRUPT_STATUS); |
| 9856 | ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE); |
| 9857 | /* |
| 9858 | * Make sure that UFS interrupts are disabled and any pending interrupt |
| 9859 | * status is cleared before registering UFS interrupt handler. |
| 9860 | */ |
| 9861 | mb(); |
| 9862 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9863 | /* IRQ registration */ |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 9864 | err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9865 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9866 | dev_err(hba->dev, "request irq failed\n"); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 9867 | goto exit_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9868 | } else { |
| 9869 | hba->is_irq_enabled = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9870 | } |
| 9871 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9872 | err = scsi_add_host(host, hba->dev); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9873 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9874 | dev_err(hba->dev, "scsi_add_host failed\n"); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 9875 | goto exit_gating; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9876 | } |
| 9877 | |
Subhash Jadavani | 9c80770 | 2017-04-01 00:35:51 -0700 | [diff] [blame] | 9878 | /* Reset controller to power on reset (POR) state */ |
| 9879 | ufshcd_vops_full_reset(hba); |
| 9880 | |
| 9881 | /* reset connected UFS device */ |
| 9882 | err = ufshcd_reset_device(hba); |
| 9883 | if (err) |
| 9884 | dev_warn(hba->dev, "%s: device reset failed. err %d\n", |
| 9885 | __func__, err); |
| 9886 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 9887 | /* Host controller enable */ |
| 9888 | err = ufshcd_hba_enable(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9889 | if (err) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 9890 | dev_err(hba->dev, "Host controller enable failed\n"); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9891 | ufshcd_print_host_regs(hba); |
| 9892 | ufshcd_print_host_state(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9893 | goto out_remove_scsi_host; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9894 | } |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 9895 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9896 | if (ufshcd_is_clkscaling_supported(hba)) { |
| 9897 | char wq_name[sizeof("ufs_clkscaling_00")]; |
| 9898 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9899 | INIT_WORK(&hba->clk_scaling.suspend_work, |
| 9900 | ufshcd_clk_scaling_suspend_work); |
| 9901 | INIT_WORK(&hba->clk_scaling.resume_work, |
| 9902 | ufshcd_clk_scaling_resume_work); |
| 9903 | |
| 9904 | snprintf(wq_name, ARRAY_SIZE(wq_name), "ufs_clkscaling_%d", |
| 9905 | host->host_no); |
| 9906 | hba->clk_scaling.workq = create_singlethread_workqueue(wq_name); |
| 9907 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9908 | ufshcd_clkscaling_init_sysfs(hba); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9909 | } |
| 9910 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9911 | /* |
| 9912 | * If rpm_lvl and and spm_lvl are not already set to valid levels, |
| 9913 | * set the default power management level for UFS runtime and system |
| 9914 | * suspend. Default power saving mode selected is keeping UFS link in |
| 9915 | * Hibern8 state and UFS device in sleep. |
| 9916 | */ |
| 9917 | if (!ufshcd_is_valid_pm_lvl(hba->rpm_lvl)) |
| 9918 | hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state( |
| 9919 | UFS_SLEEP_PWR_MODE, |
| 9920 | UIC_LINK_HIBERN8_STATE); |
| 9921 | if (!ufshcd_is_valid_pm_lvl(hba->spm_lvl)) |
| 9922 | hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state( |
| 9923 | UFS_SLEEP_PWR_MODE, |
| 9924 | UIC_LINK_HIBERN8_STATE); |
| 9925 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 9926 | /* Hold auto suspend until async scan completes */ |
| 9927 | pm_runtime_get_sync(dev); |
| 9928 | |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 9929 | ufshcd_init_latency_hist(hba); |
| 9930 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9931 | /* |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9932 | * We are assuming that device wasn't put in sleep/power-down |
| 9933 | * state exclusively during the boot stage before kernel. |
| 9934 | * This assumption helps avoid doing link startup twice during |
| 9935 | * ufshcd_probe_hba(). |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9936 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9937 | ufshcd_set_ufs_dev_active(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9938 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 9939 | async_schedule(ufshcd_async_scan, hba); |
| 9940 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9941 | ufsdbg_add_debugfs(hba); |
| 9942 | |
| 9943 | ufshcd_add_sysfs_nodes(hba); |
| 9944 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9945 | return 0; |
| 9946 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9947 | out_remove_scsi_host: |
| 9948 | scsi_remove_host(hba->host); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 9949 | exit_gating: |
| 9950 | ufshcd_exit_clk_gating(hba); |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 9951 | ufshcd_exit_latency_hist(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9952 | out_disable: |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9953 | hba->is_irq_enabled = false; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 9954 | ufshcd_hba_exit(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9955 | out_error: |
| 9956 | return err; |
| 9957 | } |
| 9958 | EXPORT_SYMBOL_GPL(ufshcd_init); |
| 9959 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9960 | MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>"); |
| 9961 | MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>"); |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 9962 | MODULE_DESCRIPTION("Generic UFS host controller driver Core"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9963 | MODULE_LICENSE("GPL"); |
| 9964 | MODULE_VERSION(UFSHCD_DRIVER_VERSION); |