Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1 | /* |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 2 | * Universal Flash Storage Host controller driver Core |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3 | * |
| 4 | * This code is based on drivers/scsi/ufs/ufshcd.c |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 5 | * Copyright (C) 2011-2013 Samsung India Software Operations |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 6 | * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7 | * |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8 | * Authors: |
| 9 | * Santosh Yaraganavi <santosh.sy@samsung.com> |
| 10 | * Vinayak Holikatti <h.vinayak@samsung.com> |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 16 | * See the COPYING file in the top-level directory or visit |
| 17 | * <http://www.gnu.org/licenses/gpl-2.0.html> |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 24 | * This program is provided "AS IS" and "WITH ALL FAULTS" and |
| 25 | * without warranty of any kind. You are solely responsible for |
| 26 | * determining the appropriateness of using and distributing |
| 27 | * the program and assume all risks associated with your exercise |
| 28 | * of rights with respect to the program, including but not limited |
| 29 | * to infringement of third party rights, the risks and costs of |
| 30 | * program errors, damage to or loss of data, programs or equipment, |
| 31 | * and unavailability or interruption of operations. Under no |
| 32 | * circumstances will the contributor of this Program be liable for |
| 33 | * any damages of any kind arising from your use or distribution of |
| 34 | * this program. |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 35 | * |
| 36 | * The Linux Foundation chooses to take subject only to the GPLv2 |
| 37 | * license terms, and distributes only under these terms. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 38 | */ |
| 39 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 40 | #include <linux/async.h> |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 41 | #include <scsi/ufs/ioctl.h> |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 42 | #include <linux/devfreq.h> |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 43 | #include <linux/nls.h> |
Yaniv Gardi | 54b879b | 2016-03-10 17:37:05 +0200 | [diff] [blame] | 44 | #include <linux/of.h> |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 45 | #include <linux/blkdev.h> |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 46 | #include "ufshcd.h" |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 47 | #include "ufshci.h" |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 48 | #include "ufs_quirks.h" |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 49 | #include "ufs-debugfs.h" |
| 50 | |
| 51 | #define CREATE_TRACE_POINTS |
| 52 | #include <trace/events/ufs.h> |
| 53 | |
| 54 | #ifdef CONFIG_DEBUG_FS |
| 55 | |
| 56 | static int ufshcd_tag_req_type(struct request *rq) |
| 57 | { |
| 58 | int rq_type = TS_WRITE; |
| 59 | |
| 60 | if (!rq || !(rq->cmd_type & REQ_TYPE_FS)) |
| 61 | rq_type = TS_NOT_SUPPORTED; |
| 62 | else if (rq->cmd_flags & REQ_PREFLUSH) |
| 63 | rq_type = TS_FLUSH; |
| 64 | else if (rq_data_dir(rq) == READ) |
| 65 | rq_type = (rq->cmd_flags & REQ_URGENT) ? |
| 66 | TS_URGENT_READ : TS_READ; |
| 67 | else if (rq->cmd_flags & REQ_URGENT) |
| 68 | rq_type = TS_URGENT_WRITE; |
| 69 | |
| 70 | return rq_type; |
| 71 | } |
| 72 | |
| 73 | static void ufshcd_update_error_stats(struct ufs_hba *hba, int type) |
| 74 | { |
| 75 | ufsdbg_set_err_state(hba); |
| 76 | if (type < UFS_ERR_MAX) |
| 77 | hba->ufs_stats.err_stats[type]++; |
| 78 | } |
| 79 | |
| 80 | static void ufshcd_update_tag_stats(struct ufs_hba *hba, int tag) |
| 81 | { |
| 82 | struct request *rq = |
| 83 | hba->lrb[tag].cmd ? hba->lrb[tag].cmd->request : NULL; |
| 84 | u64 **tag_stats = hba->ufs_stats.tag_stats; |
| 85 | int rq_type; |
| 86 | |
| 87 | if (!hba->ufs_stats.enabled) |
| 88 | return; |
| 89 | |
| 90 | tag_stats[tag][TS_TAG]++; |
| 91 | if (!rq || !(rq->cmd_type & REQ_TYPE_FS)) |
| 92 | return; |
| 93 | |
| 94 | WARN_ON(hba->ufs_stats.q_depth > hba->nutrs); |
| 95 | rq_type = ufshcd_tag_req_type(rq); |
| 96 | if (!(rq_type < 0 || rq_type > TS_NUM_STATS)) |
| 97 | tag_stats[hba->ufs_stats.q_depth++][rq_type]++; |
| 98 | } |
| 99 | |
| 100 | static void ufshcd_update_tag_stats_completion(struct ufs_hba *hba, |
| 101 | struct scsi_cmnd *cmd) |
| 102 | { |
| 103 | struct request *rq = cmd ? cmd->request : NULL; |
| 104 | |
| 105 | if (rq && rq->cmd_type & REQ_TYPE_FS) |
| 106 | hba->ufs_stats.q_depth--; |
| 107 | } |
| 108 | |
| 109 | static void update_req_stats(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 110 | { |
| 111 | int rq_type; |
| 112 | struct request *rq = lrbp->cmd ? lrbp->cmd->request : NULL; |
| 113 | s64 delta = ktime_us_delta(lrbp->complete_time_stamp, |
| 114 | lrbp->issue_time_stamp); |
| 115 | |
| 116 | /* update general request statistics */ |
| 117 | if (hba->ufs_stats.req_stats[TS_TAG].count == 0) |
| 118 | hba->ufs_stats.req_stats[TS_TAG].min = delta; |
| 119 | hba->ufs_stats.req_stats[TS_TAG].count++; |
| 120 | hba->ufs_stats.req_stats[TS_TAG].sum += delta; |
| 121 | if (delta > hba->ufs_stats.req_stats[TS_TAG].max) |
| 122 | hba->ufs_stats.req_stats[TS_TAG].max = delta; |
| 123 | if (delta < hba->ufs_stats.req_stats[TS_TAG].min) |
| 124 | hba->ufs_stats.req_stats[TS_TAG].min = delta; |
| 125 | |
| 126 | rq_type = ufshcd_tag_req_type(rq); |
| 127 | if (rq_type == TS_NOT_SUPPORTED) |
| 128 | return; |
| 129 | |
| 130 | /* update request type specific statistics */ |
| 131 | if (hba->ufs_stats.req_stats[rq_type].count == 0) |
| 132 | hba->ufs_stats.req_stats[rq_type].min = delta; |
| 133 | hba->ufs_stats.req_stats[rq_type].count++; |
| 134 | hba->ufs_stats.req_stats[rq_type].sum += delta; |
| 135 | if (delta > hba->ufs_stats.req_stats[rq_type].max) |
| 136 | hba->ufs_stats.req_stats[rq_type].max = delta; |
| 137 | if (delta < hba->ufs_stats.req_stats[rq_type].min) |
| 138 | hba->ufs_stats.req_stats[rq_type].min = delta; |
| 139 | } |
| 140 | |
| 141 | static void |
| 142 | ufshcd_update_query_stats(struct ufs_hba *hba, enum query_opcode opcode, u8 idn) |
| 143 | { |
| 144 | if (opcode < UPIU_QUERY_OPCODE_MAX && idn < MAX_QUERY_IDN) |
| 145 | hba->ufs_stats.query_stats_arr[opcode][idn]++; |
| 146 | } |
| 147 | |
| 148 | #else |
| 149 | static inline void ufshcd_update_tag_stats(struct ufs_hba *hba, int tag) |
| 150 | { |
| 151 | } |
| 152 | |
| 153 | static inline void ufshcd_update_tag_stats_completion(struct ufs_hba *hba, |
| 154 | struct scsi_cmnd *cmd) |
| 155 | { |
| 156 | } |
| 157 | |
| 158 | static inline void ufshcd_update_error_stats(struct ufs_hba *hba, int type) |
| 159 | { |
| 160 | } |
| 161 | |
| 162 | static inline |
| 163 | void update_req_stats(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 164 | { |
| 165 | } |
| 166 | |
| 167 | static inline |
| 168 | void ufshcd_update_query_stats(struct ufs_hba *hba, |
| 169 | enum query_opcode opcode, u8 idn) |
| 170 | { |
| 171 | } |
| 172 | #endif |
| 173 | |
| 174 | #define UFSHCD_REQ_SENSE_SIZE 18 |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 175 | |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 176 | #define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\ |
| 177 | UTP_TASK_REQ_COMPL |\ |
| 178 | UFSHCD_ERROR_MASK) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 179 | /* UIC command timeout, unit: ms */ |
| 180 | #define UIC_CMD_TIMEOUT 500 |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 181 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 182 | /* NOP OUT retries waiting for NOP IN response */ |
| 183 | #define NOP_OUT_RETRIES 10 |
| 184 | /* Timeout after 30 msecs if NOP OUT hangs without response */ |
| 185 | #define NOP_OUT_TIMEOUT 30 /* msecs */ |
| 186 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 187 | /* Query request retries */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 188 | #define QUERY_REQ_RETRIES 3 |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 189 | /* Query request timeout */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 190 | #define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */ |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 191 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 192 | /* Task management command timeout */ |
| 193 | #define TM_CMD_TIMEOUT 100 /* msecs */ |
| 194 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 195 | /* maximum number of retries for a general UIC command */ |
| 196 | #define UFS_UIC_COMMAND_RETRIES 3 |
| 197 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 198 | /* maximum number of link-startup retries */ |
| 199 | #define DME_LINKSTARTUP_RETRIES 3 |
| 200 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 201 | /* Maximum retries for Hibern8 enter */ |
| 202 | #define UIC_HIBERN8_ENTER_RETRIES 3 |
| 203 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 204 | /* maximum number of reset retries before giving up */ |
| 205 | #define MAX_HOST_RESET_RETRIES 5 |
| 206 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 207 | /* Expose the flag value from utp_upiu_query.value */ |
| 208 | #define MASK_QUERY_UPIU_FLAG_LOC 0xFF |
| 209 | |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 210 | /* Interrupt aggregation default timeout, unit: 40us */ |
| 211 | #define INT_AGGR_DEF_TO 0x02 |
| 212 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 213 | /* default value of auto suspend is 3 seconds */ |
| 214 | #define UFSHCD_AUTO_SUSPEND_DELAY_MS 3000 /* millisecs */ |
| 215 | |
| 216 | #define UFSHCD_CLK_GATING_DELAY_MS_PWR_SAVE 10 |
| 217 | #define UFSHCD_CLK_GATING_DELAY_MS_PERF 50 |
| 218 | |
| 219 | /* IOCTL opcode for command - ufs set device read only */ |
| 220 | #define UFS_IOCTL_BLKROSET BLKROSET |
| 221 | |
| 222 | #define UFSHCD_DEFAULT_LANES_PER_DIRECTION 2 |
| 223 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 224 | #define ufshcd_toggle_vreg(_dev, _vreg, _on) \ |
| 225 | ({ \ |
| 226 | int _ret; \ |
| 227 | if (_on) \ |
| 228 | _ret = ufshcd_enable_vreg(_dev, _vreg); \ |
| 229 | else \ |
| 230 | _ret = ufshcd_disable_vreg(_dev, _vreg); \ |
| 231 | _ret; \ |
| 232 | }) |
| 233 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 234 | #define ufshcd_hex_dump(prefix_str, buf, len) \ |
| 235 | print_hex_dump(KERN_ERR, prefix_str, DUMP_PREFIX_OFFSET, 16, 4, buf, len, false) |
| 236 | |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 237 | static u32 ufs_query_desc_max_size[] = { |
| 238 | QUERY_DESC_DEVICE_MAX_SIZE, |
| 239 | QUERY_DESC_CONFIGURAION_MAX_SIZE, |
| 240 | QUERY_DESC_UNIT_MAX_SIZE, |
| 241 | QUERY_DESC_RFU_MAX_SIZE, |
| 242 | QUERY_DESC_INTERCONNECT_MAX_SIZE, |
| 243 | QUERY_DESC_STRING_MAX_SIZE, |
| 244 | QUERY_DESC_RFU_MAX_SIZE, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 245 | QUERY_DESC_GEOMETRY_MAZ_SIZE, |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 246 | QUERY_DESC_POWER_MAX_SIZE, |
| 247 | QUERY_DESC_RFU_MAX_SIZE, |
| 248 | }; |
| 249 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 250 | enum { |
| 251 | UFSHCD_MAX_CHANNEL = 0, |
| 252 | UFSHCD_MAX_ID = 1, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 253 | UFSHCD_CMD_PER_LUN = 32, |
| 254 | UFSHCD_CAN_QUEUE = 32, |
| 255 | }; |
| 256 | |
| 257 | /* UFSHCD states */ |
| 258 | enum { |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 259 | UFSHCD_STATE_RESET, |
| 260 | UFSHCD_STATE_ERROR, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 261 | UFSHCD_STATE_OPERATIONAL, |
| 262 | }; |
| 263 | |
| 264 | /* UFSHCD error handling flags */ |
| 265 | enum { |
| 266 | UFSHCD_EH_IN_PROGRESS = (1 << 0), |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 267 | }; |
| 268 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 269 | /* UFSHCD UIC layer error flags */ |
| 270 | enum { |
| 271 | UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 272 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */ |
| 273 | UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */ |
| 274 | UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */ |
| 275 | UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */ |
| 276 | UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 277 | }; |
| 278 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 279 | /* Interrupt configuration options */ |
| 280 | enum { |
| 281 | UFSHCD_INT_DISABLE, |
| 282 | UFSHCD_INT_ENABLE, |
| 283 | UFSHCD_INT_CLEAR, |
| 284 | }; |
| 285 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 286 | #define DEFAULT_UFSHCD_DBG_PRINT_EN UFSHCD_DBG_PRINT_ALL |
| 287 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 288 | #define ufshcd_set_eh_in_progress(h) \ |
| 289 | (h->eh_flags |= UFSHCD_EH_IN_PROGRESS) |
| 290 | #define ufshcd_eh_in_progress(h) \ |
| 291 | (h->eh_flags & UFSHCD_EH_IN_PROGRESS) |
| 292 | #define ufshcd_clear_eh_in_progress(h) \ |
| 293 | (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS) |
| 294 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 295 | #define ufshcd_set_ufs_dev_active(h) \ |
| 296 | ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE) |
| 297 | #define ufshcd_set_ufs_dev_sleep(h) \ |
| 298 | ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE) |
| 299 | #define ufshcd_set_ufs_dev_poweroff(h) \ |
| 300 | ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE) |
| 301 | #define ufshcd_is_ufs_dev_active(h) \ |
| 302 | ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE) |
| 303 | #define ufshcd_is_ufs_dev_sleep(h) \ |
| 304 | ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE) |
| 305 | #define ufshcd_is_ufs_dev_poweroff(h) \ |
| 306 | ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE) |
| 307 | |
| 308 | static struct ufs_pm_lvl_states ufs_pm_lvl_states[] = { |
| 309 | {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE}, |
| 310 | {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 311 | {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE}, |
| 312 | {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 313 | {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE}, |
| 314 | {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE}, |
| 315 | }; |
| 316 | |
| 317 | static inline enum ufs_dev_pwr_mode |
| 318 | ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl) |
| 319 | { |
| 320 | return ufs_pm_lvl_states[lvl].dev_state; |
| 321 | } |
| 322 | |
| 323 | static inline enum uic_link_state |
| 324 | ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl) |
| 325 | { |
| 326 | return ufs_pm_lvl_states[lvl].link_state; |
| 327 | } |
| 328 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 329 | static inline enum ufs_pm_level |
| 330 | ufs_get_desired_pm_lvl_for_dev_link_state(enum ufs_dev_pwr_mode dev_state, |
| 331 | enum uic_link_state link_state) |
| 332 | { |
| 333 | enum ufs_pm_level lvl; |
| 334 | |
| 335 | for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) { |
| 336 | if ((ufs_pm_lvl_states[lvl].dev_state == dev_state) && |
| 337 | (ufs_pm_lvl_states[lvl].link_state == link_state)) |
| 338 | return lvl; |
| 339 | } |
| 340 | |
| 341 | /* if no match found, return the level 0 */ |
| 342 | return UFS_PM_LVL_0; |
| 343 | } |
| 344 | |
| 345 | static inline bool ufshcd_is_valid_pm_lvl(int lvl) |
| 346 | { |
| 347 | if (lvl >= 0 && lvl < ARRAY_SIZE(ufs_pm_lvl_states)) |
| 348 | return true; |
| 349 | else |
| 350 | return false; |
| 351 | } |
| 352 | |
| 353 | static irqreturn_t ufshcd_intr(int irq, void *__hba); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 354 | static void ufshcd_tmc_handler(struct ufs_hba *hba); |
| 355 | static void ufshcd_async_scan(void *data, async_cookie_t cookie); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 356 | static int ufshcd_reset_and_restore(struct ufs_hba *hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 357 | static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 358 | static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 359 | static void ufshcd_hba_exit(struct ufs_hba *hba); |
| 360 | static int ufshcd_probe_hba(struct ufs_hba *hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 361 | static int ufshcd_enable_clocks(struct ufs_hba *hba); |
| 362 | static int ufshcd_disable_clocks(struct ufs_hba *hba, |
| 363 | bool is_gating_context); |
| 364 | static int ufshcd_disable_clocks_skip_ref_clk(struct ufs_hba *hba, |
| 365 | bool is_gating_context); |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 366 | static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 367 | static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 368 | static inline void ufshcd_save_tstamp_of_last_dme_cmd(struct ufs_hba *hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 369 | static int ufshcd_host_reset_and_restore(struct ufs_hba *hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 370 | static void ufshcd_resume_clkscaling(struct ufs_hba *hba); |
| 371 | static void ufshcd_suspend_clkscaling(struct ufs_hba *hba); |
| 372 | static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba); |
| 373 | static void ufshcd_release_all(struct ufs_hba *hba); |
| 374 | static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba); |
| 375 | static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba); |
| 376 | |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 377 | static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag) |
| 378 | { |
| 379 | return tag >= 0 && tag < hba->nutrs; |
| 380 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 381 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 382 | static inline void ufshcd_enable_irq(struct ufs_hba *hba) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 383 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 384 | if (!hba->is_irq_enabled) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 385 | enable_irq(hba->irq); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 386 | hba->is_irq_enabled = true; |
| 387 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | static inline void ufshcd_disable_irq(struct ufs_hba *hba) |
| 391 | { |
| 392 | if (hba->is_irq_enabled) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 393 | disable_irq(hba->irq); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 394 | hba->is_irq_enabled = false; |
| 395 | } |
| 396 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 397 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 398 | void ufshcd_scsi_unblock_requests(struct ufs_hba *hba) |
| 399 | { |
| 400 | unsigned long flags; |
| 401 | bool unblock = false; |
| 402 | |
| 403 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 404 | hba->scsi_block_reqs_cnt--; |
| 405 | unblock = !hba->scsi_block_reqs_cnt; |
| 406 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 407 | if (unblock) |
| 408 | scsi_unblock_requests(hba->host); |
| 409 | } |
| 410 | EXPORT_SYMBOL(ufshcd_scsi_unblock_requests); |
| 411 | |
| 412 | static inline void __ufshcd_scsi_block_requests(struct ufs_hba *hba) |
| 413 | { |
| 414 | if (!hba->scsi_block_reqs_cnt++) |
| 415 | scsi_block_requests(hba->host); |
| 416 | } |
| 417 | |
| 418 | void ufshcd_scsi_block_requests(struct ufs_hba *hba) |
| 419 | { |
| 420 | unsigned long flags; |
| 421 | |
| 422 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 423 | __ufshcd_scsi_block_requests(hba); |
| 424 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 425 | } |
| 426 | EXPORT_SYMBOL(ufshcd_scsi_block_requests); |
| 427 | |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 428 | /* replace non-printable or non-ASCII characters with spaces */ |
| 429 | static inline void ufshcd_remove_non_printable(char *val) |
| 430 | { |
| 431 | if (!val) |
| 432 | return; |
| 433 | |
| 434 | if (*val < 0x20 || *val > 0x7e) |
| 435 | *val = ' '; |
| 436 | } |
| 437 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 438 | #ifdef CONFIG_TRACEPOINTS |
| 439 | static void ufshcd_add_command_trace(struct ufs_hba *hba, |
| 440 | unsigned int tag, const char *str) |
| 441 | { |
| 442 | sector_t lba = -1; |
| 443 | u8 opcode = 0; |
| 444 | u32 intr, doorbell; |
| 445 | struct ufshcd_lrb *lrbp; |
| 446 | int transfer_len = -1; |
| 447 | |
| 448 | lrbp = &hba->lrb[tag]; |
| 449 | |
| 450 | if (lrbp->cmd) { /* data phase exists */ |
| 451 | opcode = (u8)(*lrbp->cmd->cmnd); |
| 452 | if ((opcode == READ_10) || (opcode == WRITE_10)) { |
| 453 | /* |
| 454 | * Currently we only fully trace read(10) and write(10) |
| 455 | * commands |
| 456 | */ |
| 457 | if (lrbp->cmd->request && lrbp->cmd->request->bio) |
| 458 | lba = |
| 459 | lrbp->cmd->request->bio->bi_iter.bi_sector; |
| 460 | transfer_len = be32_to_cpu( |
| 461 | lrbp->ucd_req_ptr->sc.exp_data_transfer_len); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | intr = ufshcd_readl(hba, REG_INTERRUPT_STATUS); |
| 466 | doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 467 | trace_ufshcd_command(dev_name(hba->dev), str, tag, |
| 468 | doorbell, transfer_len, intr, lba, opcode); |
| 469 | } |
| 470 | |
| 471 | static inline void ufshcd_cond_add_cmd_trace(struct ufs_hba *hba, |
| 472 | unsigned int tag, const char *str) |
| 473 | { |
| 474 | if (trace_ufshcd_command_enabled()) |
| 475 | ufshcd_add_command_trace(hba, tag, str); |
| 476 | } |
| 477 | #else |
| 478 | static inline void ufshcd_cond_add_cmd_trace(struct ufs_hba *hba, |
| 479 | unsigned int tag, const char *str) |
| 480 | { |
| 481 | } |
| 482 | #endif |
| 483 | |
| 484 | static void ufshcd_print_clk_freqs(struct ufs_hba *hba) |
| 485 | { |
| 486 | struct ufs_clk_info *clki; |
| 487 | struct list_head *head = &hba->clk_list_head; |
| 488 | |
| 489 | if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_CLK_FREQ_EN)) |
| 490 | return; |
| 491 | |
| 492 | if (!head || list_empty(head)) |
| 493 | return; |
| 494 | |
| 495 | list_for_each_entry(clki, head, list) { |
| 496 | if (!IS_ERR_OR_NULL(clki->clk) && clki->min_freq && |
| 497 | clki->max_freq) |
| 498 | dev_err(hba->dev, "clk: %s, rate: %u\n", |
| 499 | clki->name, clki->curr_freq); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | static void ufshcd_print_uic_err_hist(struct ufs_hba *hba, |
| 504 | struct ufs_uic_err_reg_hist *err_hist, char *err_name) |
| 505 | { |
| 506 | int i; |
| 507 | |
| 508 | if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_UIC_ERR_HIST_EN)) |
| 509 | return; |
| 510 | |
| 511 | for (i = 0; i < UIC_ERR_REG_HIST_LENGTH; i++) { |
| 512 | int p = (i + err_hist->pos - 1) % UIC_ERR_REG_HIST_LENGTH; |
| 513 | |
| 514 | if (err_hist->reg[p] == 0) |
| 515 | continue; |
| 516 | dev_err(hba->dev, "%s[%d] = 0x%x at %lld us", err_name, i, |
| 517 | err_hist->reg[p], ktime_to_us(err_hist->tstamp[p])); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | static void ufshcd_print_host_regs(struct ufs_hba *hba) |
| 522 | { |
| 523 | if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_HOST_REGS_EN)) |
| 524 | return; |
| 525 | |
| 526 | /* |
| 527 | * hex_dump reads its data without the readl macro. This might |
| 528 | * cause inconsistency issues on some platform, as the printed |
| 529 | * values may be from cache and not the most recent value. |
| 530 | * To know whether you are looking at an un-cached version verify |
| 531 | * that IORESOURCE_MEM flag is on when xxx_get_resource() is invoked |
| 532 | * during platform/pci probe function. |
| 533 | */ |
| 534 | ufshcd_hex_dump("host regs: ", hba->mmio_base, UFSHCI_REG_SPACE_SIZE); |
| 535 | dev_err(hba->dev, "hba->ufs_version = 0x%x, hba->capabilities = 0x%x", |
| 536 | hba->ufs_version, hba->capabilities); |
| 537 | dev_err(hba->dev, |
| 538 | "hba->outstanding_reqs = 0x%x, hba->outstanding_tasks = 0x%x", |
| 539 | (u32)hba->outstanding_reqs, (u32)hba->outstanding_tasks); |
| 540 | dev_err(hba->dev, |
| 541 | "last_hibern8_exit_tstamp at %lld us, hibern8_exit_cnt = %d", |
| 542 | ktime_to_us(hba->ufs_stats.last_hibern8_exit_tstamp), |
| 543 | hba->ufs_stats.hibern8_exit_cnt); |
| 544 | |
| 545 | ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.pa_err, "pa_err"); |
| 546 | ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.dl_err, "dl_err"); |
| 547 | ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.nl_err, "nl_err"); |
| 548 | ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.tl_err, "tl_err"); |
| 549 | ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.dme_err, "dme_err"); |
| 550 | |
| 551 | ufshcd_print_clk_freqs(hba); |
| 552 | |
| 553 | ufshcd_vops_dbg_register_dump(hba); |
| 554 | } |
| 555 | |
| 556 | static |
| 557 | void ufshcd_print_trs(struct ufs_hba *hba, unsigned long bitmap, bool pr_prdt) |
| 558 | { |
| 559 | struct ufshcd_lrb *lrbp; |
| 560 | int prdt_length; |
| 561 | int tag; |
| 562 | |
| 563 | if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_TRS_EN)) |
| 564 | return; |
| 565 | |
| 566 | for_each_set_bit(tag, &bitmap, hba->nutrs) { |
| 567 | lrbp = &hba->lrb[tag]; |
| 568 | |
| 569 | dev_err(hba->dev, "UPIU[%d] - issue time %lld us", |
| 570 | tag, ktime_to_us(lrbp->issue_time_stamp)); |
| 571 | dev_err(hba->dev, |
| 572 | "UPIU[%d] - Transfer Request Descriptor phys@0x%llx", |
| 573 | tag, (u64)lrbp->utrd_dma_addr); |
| 574 | ufshcd_hex_dump("UPIU TRD: ", lrbp->utr_descriptor_ptr, |
| 575 | sizeof(struct utp_transfer_req_desc)); |
| 576 | dev_err(hba->dev, "UPIU[%d] - Request UPIU phys@0x%llx", tag, |
| 577 | (u64)lrbp->ucd_req_dma_addr); |
| 578 | ufshcd_hex_dump("UPIU REQ: ", lrbp->ucd_req_ptr, |
| 579 | sizeof(struct utp_upiu_req)); |
| 580 | dev_err(hba->dev, "UPIU[%d] - Response UPIU phys@0x%llx", tag, |
| 581 | (u64)lrbp->ucd_rsp_dma_addr); |
| 582 | ufshcd_hex_dump("UPIU RSP: ", lrbp->ucd_rsp_ptr, |
| 583 | sizeof(struct utp_upiu_rsp)); |
| 584 | prdt_length = |
| 585 | le16_to_cpu(lrbp->utr_descriptor_ptr->prd_table_length); |
| 586 | dev_err(hba->dev, "UPIU[%d] - PRDT - %d entries phys@0x%llx", |
| 587 | tag, prdt_length, (u64)lrbp->ucd_prdt_dma_addr); |
| 588 | if (pr_prdt) |
| 589 | ufshcd_hex_dump("UPIU PRDT: ", lrbp->ucd_prdt_ptr, |
| 590 | sizeof(struct ufshcd_sg_entry) * prdt_length); |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap) |
| 595 | { |
| 596 | struct utp_task_req_desc *tmrdp; |
| 597 | int tag; |
| 598 | |
| 599 | if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_TMRS_EN)) |
| 600 | return; |
| 601 | |
| 602 | for_each_set_bit(tag, &bitmap, hba->nutmrs) { |
| 603 | tmrdp = &hba->utmrdl_base_addr[tag]; |
| 604 | dev_err(hba->dev, "TM[%d] - Task Management Header", tag); |
| 605 | ufshcd_hex_dump("TM TRD: ", &tmrdp->header, |
| 606 | sizeof(struct request_desc_header)); |
| 607 | dev_err(hba->dev, "TM[%d] - Task Management Request UPIU", |
| 608 | tag); |
| 609 | ufshcd_hex_dump("TM REQ: ", tmrdp->task_req_upiu, |
| 610 | sizeof(struct utp_upiu_req)); |
| 611 | dev_err(hba->dev, "TM[%d] - Task Management Response UPIU", |
| 612 | tag); |
| 613 | ufshcd_hex_dump("TM RSP: ", tmrdp->task_rsp_upiu, |
| 614 | sizeof(struct utp_task_req_desc)); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | static void ufshcd_print_host_state(struct ufs_hba *hba) |
| 619 | { |
| 620 | if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_HOST_STATE_EN)) |
| 621 | return; |
| 622 | |
| 623 | dev_err(hba->dev, "UFS Host state=%d\n", hba->ufshcd_state); |
| 624 | dev_err(hba->dev, "lrb in use=0x%lx, outstanding reqs=0x%lx tasks=0x%lx\n", |
| 625 | hba->lrb_in_use, hba->outstanding_tasks, hba->outstanding_reqs); |
| 626 | dev_err(hba->dev, "saved_err=0x%x, saved_uic_err=0x%x, saved_ce_err=0x%x\n", |
| 627 | hba->saved_err, hba->saved_uic_err, hba->saved_ce_err); |
| 628 | dev_err(hba->dev, "Device power mode=%d, UIC link state=%d\n", |
| 629 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
| 630 | dev_err(hba->dev, "PM in progress=%d, sys. suspended=%d\n", |
| 631 | hba->pm_op_in_progress, hba->is_sys_suspended); |
| 632 | dev_err(hba->dev, "Auto BKOPS=%d, Host self-block=%d\n", |
| 633 | hba->auto_bkops_enabled, hba->host->host_self_blocked); |
| 634 | dev_err(hba->dev, "Clk gate=%d, hibern8 on idle=%d\n", |
| 635 | hba->clk_gating.state, hba->hibern8_on_idle.state); |
| 636 | dev_err(hba->dev, "error handling flags=0x%x, req. abort count=%d\n", |
| 637 | hba->eh_flags, hba->req_abort_count); |
| 638 | dev_err(hba->dev, "Host capabilities=0x%x, caps=0x%x\n", |
| 639 | hba->capabilities, hba->caps); |
| 640 | dev_err(hba->dev, "quirks=0x%x, dev. quirks=0x%x\n", hba->quirks, |
| 641 | hba->dev_quirks); |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * ufshcd_print_pwr_info - print power params as saved in hba |
| 646 | * power info |
| 647 | * @hba: per-adapter instance |
| 648 | */ |
| 649 | static void ufshcd_print_pwr_info(struct ufs_hba *hba) |
| 650 | { |
| 651 | char *names[] = { |
| 652 | "INVALID MODE", |
| 653 | "FAST MODE", |
| 654 | "SLOW_MODE", |
| 655 | "INVALID MODE", |
| 656 | "FASTAUTO_MODE", |
| 657 | "SLOWAUTO_MODE", |
| 658 | "INVALID MODE", |
| 659 | }; |
| 660 | |
| 661 | if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_PWR_EN)) |
| 662 | return; |
| 663 | |
| 664 | dev_err(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n", |
| 665 | __func__, |
| 666 | hba->pwr_info.gear_rx, hba->pwr_info.gear_tx, |
| 667 | hba->pwr_info.lane_rx, hba->pwr_info.lane_tx, |
| 668 | names[hba->pwr_info.pwr_rx], |
| 669 | names[hba->pwr_info.pwr_tx], |
| 670 | hba->pwr_info.hs_rate); |
| 671 | } |
| 672 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 673 | /* |
| 674 | * ufshcd_wait_for_register - wait for register value to change |
| 675 | * @hba - per-adapter interface |
| 676 | * @reg - mmio register offset |
| 677 | * @mask - mask to apply to read register value |
| 678 | * @val - wait condition |
| 679 | * @interval_us - polling interval in microsecs |
| 680 | * @timeout_ms - timeout in millisecs |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 681 | * @can_sleep - perform sleep or just spin |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 682 | * Returns -ETIMEDOUT on error, zero on success |
| 683 | */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 684 | int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask, |
| 685 | u32 val, unsigned long interval_us, |
| 686 | unsigned long timeout_ms, bool can_sleep) |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 687 | { |
| 688 | int err = 0; |
| 689 | unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms); |
| 690 | |
| 691 | /* ignore bits that we don't intend to wait on */ |
| 692 | val = val & mask; |
| 693 | |
| 694 | while ((ufshcd_readl(hba, reg) & mask) != val) { |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 695 | if (can_sleep) |
| 696 | usleep_range(interval_us, interval_us + 50); |
| 697 | else |
| 698 | udelay(interval_us); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 699 | if (time_after(jiffies, timeout)) { |
| 700 | if ((ufshcd_readl(hba, reg) & mask) != val) |
| 701 | err = -ETIMEDOUT; |
| 702 | break; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | return err; |
| 707 | } |
| 708 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 709 | /** |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 710 | * ufshcd_get_intr_mask - Get the interrupt bit mask |
| 711 | * @hba - Pointer to adapter instance |
| 712 | * |
| 713 | * Returns interrupt bit mask per version |
| 714 | */ |
| 715 | static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba) |
| 716 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 717 | u32 intr_mask = 0; |
| 718 | |
| 719 | switch (hba->ufs_version) { |
| 720 | case UFSHCI_VERSION_10: |
| 721 | intr_mask = INTERRUPT_MASK_ALL_VER_10; |
| 722 | break; |
| 723 | /* allow fall through */ |
| 724 | case UFSHCI_VERSION_11: |
| 725 | case UFSHCI_VERSION_20: |
| 726 | intr_mask = INTERRUPT_MASK_ALL_VER_11; |
| 727 | break; |
| 728 | /* allow fall through */ |
| 729 | case UFSHCI_VERSION_21: |
| 730 | default: |
| 731 | intr_mask = INTERRUPT_MASK_ALL_VER_21; |
| 732 | } |
| 733 | |
| 734 | if (!ufshcd_is_crypto_supported(hba)) |
| 735 | intr_mask &= ~CRYPTO_ENGINE_FATAL_ERROR; |
| 736 | |
| 737 | return intr_mask; |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 741 | * ufshcd_get_ufs_version - Get the UFS version supported by the HBA |
| 742 | * @hba - Pointer to adapter instance |
| 743 | * |
| 744 | * Returns UFSHCI version supported by the controller |
| 745 | */ |
| 746 | static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba) |
| 747 | { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 748 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION) |
| 749 | return ufshcd_vops_get_ufs_hci_version(hba); |
Yaniv Gardi | 9949e70 | 2015-05-17 18:55:05 +0300 | [diff] [blame] | 750 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 751 | return ufshcd_readl(hba, REG_UFS_VERSION); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | /** |
| 755 | * ufshcd_is_device_present - Check if any device connected to |
| 756 | * the host controller |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 757 | * @hba: pointer to adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 758 | * |
Venkatraman S | 73ec513 | 2012-07-10 19:39:23 +0530 | [diff] [blame] | 759 | * Returns 1 if device present, 0 if no device detected |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 760 | */ |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 761 | static inline int ufshcd_is_device_present(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 762 | { |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 763 | return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & |
| 764 | DEVICE_PRESENT) ? 1 : 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | /** |
| 768 | * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status |
| 769 | * @lrb: pointer to local command reference block |
| 770 | * |
| 771 | * This function is used to get the OCS field from UTRD |
| 772 | * Returns the OCS field in the UTRD |
| 773 | */ |
| 774 | static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp) |
| 775 | { |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 776 | return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | /** |
| 780 | * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status |
| 781 | * @task_req_descp: pointer to utp_task_req_desc structure |
| 782 | * |
| 783 | * This function is used to get the OCS field from UTMRD |
| 784 | * Returns the OCS field in the UTMRD |
| 785 | */ |
| 786 | static inline int |
| 787 | ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp) |
| 788 | { |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 789 | return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | /** |
| 793 | * ufshcd_get_tm_free_slot - get a free slot for task management request |
| 794 | * @hba: per adapter instance |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 795 | * @free_slot: pointer to variable with available slot value |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 796 | * |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 797 | * Get a free tag and lock it until ufshcd_put_tm_slot() is called. |
| 798 | * Returns 0 if free slot is not available, else return 1 with tag value |
| 799 | * in @free_slot. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 800 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 801 | static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 802 | { |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 803 | int tag; |
| 804 | bool ret = false; |
| 805 | |
| 806 | if (!free_slot) |
| 807 | goto out; |
| 808 | |
| 809 | do { |
| 810 | tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs); |
| 811 | if (tag >= hba->nutmrs) |
| 812 | goto out; |
| 813 | } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use)); |
| 814 | |
| 815 | *free_slot = tag; |
| 816 | ret = true; |
| 817 | out: |
| 818 | return ret; |
| 819 | } |
| 820 | |
| 821 | static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot) |
| 822 | { |
| 823 | clear_bit_unlock(slot, &hba->tm_slots_in_use); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | /** |
| 827 | * ufshcd_utrl_clear - Clear a bit in UTRLCLR register |
| 828 | * @hba: per adapter instance |
| 829 | * @pos: position of the bit to be cleared |
| 830 | */ |
| 831 | static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos) |
| 832 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 833 | ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | /** |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 837 | * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field |
| 838 | * @hba: per adapter instance |
| 839 | * @tag: position of the bit to be cleared |
| 840 | */ |
| 841 | static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag) |
| 842 | { |
| 843 | __clear_bit(tag, &hba->outstanding_reqs); |
| 844 | } |
| 845 | |
| 846 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 847 | * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY |
| 848 | * @reg: Register value of host controller status |
| 849 | * |
| 850 | * Returns integer, 0 on Success and positive value if failed |
| 851 | */ |
| 852 | static inline int ufshcd_get_lists_status(u32 reg) |
| 853 | { |
| 854 | /* |
| 855 | * The mask 0xFF is for the following HCS register bits |
| 856 | * Bit Description |
| 857 | * 0 Device Present |
| 858 | * 1 UTRLRDY |
| 859 | * 2 UTMRLRDY |
| 860 | * 3 UCRDY |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 861 | * 4-7 reserved |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 862 | */ |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 863 | return ((reg & 0xFF) >> 1) ^ 0x07; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | /** |
| 867 | * ufshcd_get_uic_cmd_result - Get the UIC command result |
| 868 | * @hba: Pointer to adapter instance |
| 869 | * |
| 870 | * This function gets the result of UIC command completion |
| 871 | * Returns 0 on success, non zero value on error |
| 872 | */ |
| 873 | static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba) |
| 874 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 875 | return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) & |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 876 | MASK_UIC_COMMAND_RESULT; |
| 877 | } |
| 878 | |
| 879 | /** |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 880 | * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command |
| 881 | * @hba: Pointer to adapter instance |
| 882 | * |
| 883 | * This function gets UIC command argument3 |
| 884 | * Returns 0 on success, non zero value on error |
| 885 | */ |
| 886 | static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba) |
| 887 | { |
| 888 | return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3); |
| 889 | } |
| 890 | |
| 891 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 892 | * ufshcd_get_req_rsp - returns the TR response transaction type |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 893 | * @ucd_rsp_ptr: pointer to response UPIU |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 894 | */ |
| 895 | static inline int |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 896 | ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 897 | { |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 898 | return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | /** |
| 902 | * ufshcd_get_rsp_upiu_result - Get the result from response UPIU |
| 903 | * @ucd_rsp_ptr: pointer to response UPIU |
| 904 | * |
| 905 | * This function gets the response status and scsi_status from response UPIU |
| 906 | * Returns the response result code. |
| 907 | */ |
| 908 | static inline int |
| 909 | ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 910 | { |
| 911 | return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT; |
| 912 | } |
| 913 | |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 914 | /* |
| 915 | * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length |
| 916 | * from response UPIU |
| 917 | * @ucd_rsp_ptr: pointer to response UPIU |
| 918 | * |
| 919 | * Return the data segment length. |
| 920 | */ |
| 921 | static inline unsigned int |
| 922 | ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 923 | { |
| 924 | return be32_to_cpu(ucd_rsp_ptr->header.dword_2) & |
| 925 | MASK_RSP_UPIU_DATA_SEG_LEN; |
| 926 | } |
| 927 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 928 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 929 | * ufshcd_is_exception_event - Check if the device raised an exception event |
| 930 | * @ucd_rsp_ptr: pointer to response UPIU |
| 931 | * |
| 932 | * The function checks if the device raised an exception event indicated in |
| 933 | * the Device Information field of response UPIU. |
| 934 | * |
| 935 | * Returns true if exception is raised, false otherwise. |
| 936 | */ |
| 937 | static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 938 | { |
| 939 | return be32_to_cpu(ucd_rsp_ptr->header.dword_2) & |
| 940 | MASK_RSP_EXCEPTION_EVENT ? true : false; |
| 941 | } |
| 942 | |
| 943 | /** |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 944 | * ufshcd_reset_intr_aggr - Reset interrupt aggregation values. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 945 | * @hba: per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 946 | */ |
| 947 | static inline void |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 948 | ufshcd_reset_intr_aggr(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 949 | { |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 950 | ufshcd_writel(hba, INT_AGGR_ENABLE | |
| 951 | INT_AGGR_COUNTER_AND_TIMER_RESET, |
| 952 | REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
| 953 | } |
| 954 | |
| 955 | /** |
| 956 | * ufshcd_config_intr_aggr - Configure interrupt aggregation values. |
| 957 | * @hba: per adapter instance |
| 958 | * @cnt: Interrupt aggregation counter threshold |
| 959 | * @tmout: Interrupt aggregation timeout value |
| 960 | */ |
| 961 | static inline void |
| 962 | ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout) |
| 963 | { |
| 964 | ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE | |
| 965 | INT_AGGR_COUNTER_THLD_VAL(cnt) | |
| 966 | INT_AGGR_TIMEOUT_VAL(tmout), |
| 967 | REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | /** |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 971 | * ufshcd_disable_intr_aggr - Disables interrupt aggregation. |
| 972 | * @hba: per adapter instance |
| 973 | */ |
| 974 | static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba) |
| 975 | { |
| 976 | ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); |
| 977 | } |
| 978 | |
| 979 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 980 | * ufshcd_enable_run_stop_reg - Enable run-stop registers, |
| 981 | * When run-stop registers are set to 1, it indicates the |
| 982 | * host controller that it can process the requests |
| 983 | * @hba: per adapter instance |
| 984 | */ |
| 985 | static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba) |
| 986 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 987 | ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT, |
| 988 | REG_UTP_TASK_REQ_LIST_RUN_STOP); |
| 989 | ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT, |
| 990 | REG_UTP_TRANSFER_REQ_LIST_RUN_STOP); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 994 | * ufshcd_hba_start - Start controller initialization sequence |
| 995 | * @hba: per adapter instance |
| 996 | */ |
| 997 | static inline void ufshcd_hba_start(struct ufs_hba *hba) |
| 998 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 999 | u32 val = CONTROLLER_ENABLE; |
| 1000 | |
| 1001 | if (ufshcd_is_crypto_supported(hba)) |
| 1002 | val |= CRYPTO_GENERAL_ENABLE; |
| 1003 | ufshcd_writel(hba, val, REG_CONTROLLER_ENABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | /** |
| 1007 | * ufshcd_is_hba_active - Get controller state |
| 1008 | * @hba: per adapter instance |
| 1009 | * |
| 1010 | * Returns zero if controller is active, 1 otherwise |
| 1011 | */ |
| 1012 | static inline int ufshcd_is_hba_active(struct ufs_hba *hba) |
| 1013 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 1014 | return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1015 | } |
| 1016 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1017 | static const char *ufschd_uic_link_state_to_string( |
| 1018 | enum uic_link_state state) |
| 1019 | { |
| 1020 | switch (state) { |
| 1021 | case UIC_LINK_OFF_STATE: return "OFF"; |
| 1022 | case UIC_LINK_ACTIVE_STATE: return "ACTIVE"; |
| 1023 | case UIC_LINK_HIBERN8_STATE: return "HIBERN8"; |
| 1024 | default: return "UNKNOWN"; |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | static const char *ufschd_ufs_dev_pwr_mode_to_string( |
| 1029 | enum ufs_dev_pwr_mode state) |
| 1030 | { |
| 1031 | switch (state) { |
| 1032 | case UFS_ACTIVE_PWR_MODE: return "ACTIVE"; |
| 1033 | case UFS_SLEEP_PWR_MODE: return "SLEEP"; |
| 1034 | case UFS_POWERDOWN_PWR_MODE: return "POWERDOWN"; |
| 1035 | default: return "UNKNOWN"; |
| 1036 | } |
| 1037 | } |
| 1038 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 1039 | u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba) |
| 1040 | { |
| 1041 | /* HCI version 1.0 and 1.1 supports UniPro 1.41 */ |
| 1042 | if ((hba->ufs_version == UFSHCI_VERSION_10) || |
| 1043 | (hba->ufs_version == UFSHCI_VERSION_11)) |
| 1044 | return UFS_UNIPRO_VER_1_41; |
| 1045 | else |
| 1046 | return UFS_UNIPRO_VER_1_6; |
| 1047 | } |
| 1048 | EXPORT_SYMBOL(ufshcd_get_local_unipro_ver); |
| 1049 | |
| 1050 | static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba) |
| 1051 | { |
| 1052 | /* |
| 1053 | * If both host and device support UniPro ver1.6 or later, PA layer |
| 1054 | * parameters tuning happens during link startup itself. |
| 1055 | * |
| 1056 | * We can manually tune PA layer parameters if either host or device |
| 1057 | * doesn't support UniPro ver 1.6 or later. But to keep manual tuning |
| 1058 | * logic simple, we will only do manual tuning if local unipro version |
| 1059 | * doesn't support ver1.6 or later. |
| 1060 | */ |
| 1061 | if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6) |
| 1062 | return true; |
| 1063 | else |
| 1064 | return false; |
| 1065 | } |
| 1066 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1067 | /** |
| 1068 | * ufshcd_set_clk_freq - set UFS controller clock frequencies |
| 1069 | * @hba: per adapter instance |
| 1070 | * @scale_up: If True, set max possible frequency othewise set low frequency |
| 1071 | * |
| 1072 | * Returns 0 if successful |
| 1073 | * Returns < 0 for any other errors |
| 1074 | */ |
| 1075 | static int ufshcd_set_clk_freq(struct ufs_hba *hba, bool scale_up) |
| 1076 | { |
| 1077 | int ret = 0; |
| 1078 | struct ufs_clk_info *clki; |
| 1079 | struct list_head *head = &hba->clk_list_head; |
| 1080 | |
| 1081 | if (!head || list_empty(head)) |
| 1082 | goto out; |
| 1083 | |
| 1084 | list_for_each_entry(clki, head, list) { |
| 1085 | if (!IS_ERR_OR_NULL(clki->clk)) { |
| 1086 | if (scale_up && clki->max_freq) { |
| 1087 | if (clki->curr_freq == clki->max_freq) |
| 1088 | continue; |
| 1089 | |
| 1090 | ret = clk_set_rate(clki->clk, clki->max_freq); |
| 1091 | if (ret) { |
| 1092 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 1093 | __func__, clki->name, |
| 1094 | clki->max_freq, ret); |
| 1095 | break; |
| 1096 | } |
| 1097 | trace_ufshcd_clk_scaling(dev_name(hba->dev), |
| 1098 | "scaled up", clki->name, |
| 1099 | clki->curr_freq, |
| 1100 | clki->max_freq); |
| 1101 | clki->curr_freq = clki->max_freq; |
| 1102 | |
| 1103 | } else if (!scale_up && clki->min_freq) { |
| 1104 | if (clki->curr_freq == clki->min_freq) |
| 1105 | continue; |
| 1106 | |
| 1107 | ret = clk_set_rate(clki->clk, clki->min_freq); |
| 1108 | if (ret) { |
| 1109 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 1110 | __func__, clki->name, |
| 1111 | clki->min_freq, ret); |
| 1112 | break; |
| 1113 | } |
| 1114 | trace_ufshcd_clk_scaling(dev_name(hba->dev), |
| 1115 | "scaled down", clki->name, |
| 1116 | clki->curr_freq, |
| 1117 | clki->min_freq); |
| 1118 | clki->curr_freq = clki->min_freq; |
| 1119 | } |
| 1120 | } |
| 1121 | dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__, |
| 1122 | clki->name, clk_get_rate(clki->clk)); |
| 1123 | } |
| 1124 | |
| 1125 | out: |
| 1126 | return ret; |
| 1127 | } |
| 1128 | |
| 1129 | /** |
| 1130 | * ufshcd_scale_clks - scale up or scale down UFS controller clocks |
| 1131 | * @hba: per adapter instance |
| 1132 | * @scale_up: True if scaling up and false if scaling down |
| 1133 | * |
| 1134 | * Returns 0 if successful |
| 1135 | * Returns < 0 for any other errors |
| 1136 | */ |
| 1137 | static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up) |
| 1138 | { |
| 1139 | int ret = 0; |
| 1140 | |
| 1141 | ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE); |
| 1142 | if (ret) |
| 1143 | return ret; |
| 1144 | |
| 1145 | ret = ufshcd_set_clk_freq(hba, scale_up); |
| 1146 | if (ret) |
| 1147 | return ret; |
| 1148 | |
| 1149 | ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE); |
| 1150 | if (ret) { |
| 1151 | ufshcd_set_clk_freq(hba, !scale_up); |
| 1152 | return ret; |
| 1153 | } |
| 1154 | |
| 1155 | return ret; |
| 1156 | } |
| 1157 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1158 | static void ufshcd_ungate_work(struct work_struct *work) |
| 1159 | { |
| 1160 | int ret; |
| 1161 | unsigned long flags; |
| 1162 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1163 | clk_gating.ungate_work); |
| 1164 | |
| 1165 | cancel_delayed_work_sync(&hba->clk_gating.gate_work); |
| 1166 | |
| 1167 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1168 | if (hba->clk_gating.state == CLKS_ON) { |
| 1169 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1170 | goto unblock_reqs; |
| 1171 | } |
| 1172 | |
| 1173 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1174 | ufshcd_hba_vreg_set_hpm(hba); |
| 1175 | ufshcd_enable_clocks(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1176 | |
| 1177 | /* Exit from hibern8 */ |
| 1178 | if (ufshcd_can_hibern8_during_gating(hba)) { |
| 1179 | /* Prevent gating in this path */ |
| 1180 | hba->clk_gating.is_suspended = true; |
| 1181 | if (ufshcd_is_link_hibern8(hba)) { |
| 1182 | ret = ufshcd_uic_hibern8_exit(hba); |
| 1183 | if (ret) |
| 1184 | dev_err(hba->dev, "%s: hibern8 exit failed %d\n", |
| 1185 | __func__, ret); |
| 1186 | else |
| 1187 | ufshcd_set_link_active(hba); |
| 1188 | } |
| 1189 | hba->clk_gating.is_suspended = false; |
| 1190 | } |
| 1191 | unblock_reqs: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1192 | ufshcd_scsi_unblock_requests(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | /** |
| 1196 | * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release. |
| 1197 | * Also, exit from hibern8 mode and set the link as active. |
| 1198 | * @hba: per adapter instance |
| 1199 | * @async: This indicates whether caller should ungate clocks asynchronously. |
| 1200 | */ |
| 1201 | int ufshcd_hold(struct ufs_hba *hba, bool async) |
| 1202 | { |
| 1203 | int rc = 0; |
| 1204 | unsigned long flags; |
| 1205 | |
| 1206 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1207 | goto out; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1208 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1209 | hba->clk_gating.active_reqs++; |
| 1210 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 1211 | if (ufshcd_eh_in_progress(hba)) { |
| 1212 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1216 | start: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1217 | switch (hba->clk_gating.state) { |
| 1218 | case CLKS_ON: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1219 | /* |
| 1220 | * Wait for the ungate work to complete if in progress. |
| 1221 | * Though the clocks may be in ON state, the link could |
| 1222 | * still be in hibner8 state if hibern8 is allowed |
| 1223 | * during clock gating. |
| 1224 | * Make sure we exit hibern8 state also in addition to |
| 1225 | * clocks being ON. |
| 1226 | */ |
| 1227 | if (ufshcd_can_hibern8_during_gating(hba) && |
| 1228 | ufshcd_is_link_hibern8(hba)) { |
| 1229 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1230 | flush_work(&hba->clk_gating.ungate_work); |
| 1231 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1232 | goto start; |
| 1233 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1234 | break; |
| 1235 | case REQ_CLKS_OFF: |
| 1236 | if (cancel_delayed_work(&hba->clk_gating.gate_work)) { |
| 1237 | hba->clk_gating.state = CLKS_ON; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1238 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1239 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1240 | break; |
| 1241 | } |
| 1242 | /* |
| 1243 | * If we here, it means gating work is either done or |
| 1244 | * currently running. Hence, fall through to cancel gating |
| 1245 | * work and to enable clocks. |
| 1246 | */ |
| 1247 | case CLKS_OFF: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1248 | __ufshcd_scsi_block_requests(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1249 | hba->clk_gating.state = REQ_CLKS_ON; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1250 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1251 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1252 | schedule_work(&hba->clk_gating.ungate_work); |
| 1253 | /* |
| 1254 | * fall through to check if we should wait for this |
| 1255 | * work to be done or not. |
| 1256 | */ |
| 1257 | case REQ_CLKS_ON: |
| 1258 | if (async) { |
| 1259 | rc = -EAGAIN; |
| 1260 | hba->clk_gating.active_reqs--; |
| 1261 | break; |
| 1262 | } |
| 1263 | |
| 1264 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1265 | flush_work(&hba->clk_gating.ungate_work); |
| 1266 | /* Make sure state is CLKS_ON before returning */ |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1267 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1268 | goto start; |
| 1269 | default: |
| 1270 | dev_err(hba->dev, "%s: clk gating is in invalid state %d\n", |
| 1271 | __func__, hba->clk_gating.state); |
| 1272 | break; |
| 1273 | } |
| 1274 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1275 | out: |
| 1276 | return rc; |
| 1277 | } |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 1278 | EXPORT_SYMBOL_GPL(ufshcd_hold); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1279 | |
| 1280 | static void ufshcd_gate_work(struct work_struct *work) |
| 1281 | { |
| 1282 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1283 | clk_gating.gate_work.work); |
| 1284 | unsigned long flags; |
| 1285 | |
| 1286 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1287 | if (hba->clk_gating.is_suspended) { |
| 1288 | hba->clk_gating.state = CLKS_ON; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1289 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1290 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1291 | goto rel_lock; |
| 1292 | } |
| 1293 | |
| 1294 | if (hba->clk_gating.active_reqs |
| 1295 | || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL |
| 1296 | || hba->lrb_in_use || hba->outstanding_tasks |
| 1297 | || hba->active_uic_cmd || hba->uic_async_done) |
| 1298 | goto rel_lock; |
| 1299 | |
| 1300 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1301 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1302 | if (ufshcd_is_hibern8_on_idle_allowed(hba) && |
| 1303 | hba->hibern8_on_idle.is_enabled) |
| 1304 | /* |
| 1305 | * Hibern8 enter work (on Idle) needs clocks to be ON hence |
| 1306 | * make sure that it is flushed before turning off the clocks. |
| 1307 | */ |
| 1308 | flush_delayed_work(&hba->hibern8_on_idle.enter_work); |
| 1309 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1310 | /* put the link into hibern8 mode before turning off clocks */ |
| 1311 | if (ufshcd_can_hibern8_during_gating(hba)) { |
| 1312 | if (ufshcd_uic_hibern8_enter(hba)) { |
| 1313 | hba->clk_gating.state = CLKS_ON; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1314 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1315 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1316 | goto out; |
| 1317 | } |
| 1318 | ufshcd_set_link_hibern8(hba); |
| 1319 | } |
| 1320 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1321 | if (!ufshcd_is_link_active(hba) && !hba->no_ref_clk_gating) |
| 1322 | ufshcd_disable_clocks(hba, true); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1323 | else |
| 1324 | /* If link is active, device ref_clk can't be switched off */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1325 | ufshcd_disable_clocks_skip_ref_clk(hba, true); |
| 1326 | |
| 1327 | /* Put the host controller in low power mode if possible */ |
| 1328 | ufshcd_hba_vreg_set_lpm(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1329 | |
| 1330 | /* |
| 1331 | * In case you are here to cancel this work the gating state |
| 1332 | * would be marked as REQ_CLKS_ON. In this case keep the state |
| 1333 | * as REQ_CLKS_ON which would anyway imply that clocks are off |
| 1334 | * and a request to turn them on is pending. By doing this way, |
| 1335 | * we keep the state machine in tact and this would ultimately |
| 1336 | * prevent from doing cancel work multiple times when there are |
| 1337 | * new requests arriving before the current cancel work is done. |
| 1338 | */ |
| 1339 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1340 | if (hba->clk_gating.state == REQ_CLKS_OFF) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1341 | hba->clk_gating.state = CLKS_OFF; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1342 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 1343 | hba->clk_gating.state); |
| 1344 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1345 | rel_lock: |
| 1346 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1347 | out: |
| 1348 | return; |
| 1349 | } |
| 1350 | |
| 1351 | /* host lock must be held before calling this variant */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1352 | static void __ufshcd_release(struct ufs_hba *hba, bool no_sched) |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1353 | { |
| 1354 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1355 | return; |
| 1356 | |
| 1357 | hba->clk_gating.active_reqs--; |
| 1358 | |
| 1359 | if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended |
| 1360 | || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL |
| 1361 | || hba->lrb_in_use || hba->outstanding_tasks |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 1362 | || hba->active_uic_cmd || hba->uic_async_done |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1363 | || ufshcd_eh_in_progress(hba) || no_sched) |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1364 | return; |
| 1365 | |
| 1366 | hba->clk_gating.state = REQ_CLKS_OFF; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1367 | trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state); |
| 1368 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1369 | schedule_delayed_work(&hba->clk_gating.gate_work, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1370 | msecs_to_jiffies(hba->clk_gating.delay_ms)); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1371 | } |
| 1372 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1373 | void ufshcd_release(struct ufs_hba *hba, bool no_sched) |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1374 | { |
| 1375 | unsigned long flags; |
| 1376 | |
| 1377 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1378 | __ufshcd_release(hba, no_sched); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1379 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1380 | } |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 1381 | EXPORT_SYMBOL_GPL(ufshcd_release); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1382 | |
| 1383 | static ssize_t ufshcd_clkgate_delay_show(struct device *dev, |
| 1384 | struct device_attribute *attr, char *buf) |
| 1385 | { |
| 1386 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1387 | |
| 1388 | return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms); |
| 1389 | } |
| 1390 | |
| 1391 | static ssize_t ufshcd_clkgate_delay_store(struct device *dev, |
| 1392 | struct device_attribute *attr, const char *buf, size_t count) |
| 1393 | { |
| 1394 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1395 | unsigned long flags, value; |
| 1396 | |
| 1397 | if (kstrtoul(buf, 0, &value)) |
| 1398 | return -EINVAL; |
| 1399 | |
| 1400 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1401 | hba->clk_gating.delay_ms = value; |
| 1402 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1403 | return count; |
| 1404 | } |
| 1405 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1406 | static ssize_t ufshcd_clkgate_delay_pwr_save_show(struct device *dev, |
| 1407 | struct device_attribute *attr, char *buf) |
| 1408 | { |
| 1409 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1410 | |
| 1411 | return snprintf(buf, PAGE_SIZE, "%lu\n", |
| 1412 | hba->clk_gating.delay_ms_pwr_save); |
| 1413 | } |
| 1414 | |
| 1415 | static ssize_t ufshcd_clkgate_delay_pwr_save_store(struct device *dev, |
| 1416 | struct device_attribute *attr, const char *buf, size_t count) |
| 1417 | { |
| 1418 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1419 | unsigned long flags, value; |
| 1420 | |
| 1421 | if (kstrtoul(buf, 0, &value)) |
| 1422 | return -EINVAL; |
| 1423 | |
| 1424 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1425 | |
| 1426 | hba->clk_gating.delay_ms_pwr_save = value; |
| 1427 | if (ufshcd_is_clkscaling_supported(hba) && |
| 1428 | !hba->clk_scaling.is_scaled_up) |
| 1429 | hba->clk_gating.delay_ms = hba->clk_gating.delay_ms_pwr_save; |
| 1430 | |
| 1431 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1432 | return count; |
| 1433 | } |
| 1434 | |
| 1435 | static ssize_t ufshcd_clkgate_delay_perf_show(struct device *dev, |
| 1436 | struct device_attribute *attr, char *buf) |
| 1437 | { |
| 1438 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1439 | |
| 1440 | return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms_perf); |
| 1441 | } |
| 1442 | |
| 1443 | static ssize_t ufshcd_clkgate_delay_perf_store(struct device *dev, |
| 1444 | struct device_attribute *attr, const char *buf, size_t count) |
| 1445 | { |
| 1446 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1447 | unsigned long flags, value; |
| 1448 | |
| 1449 | if (kstrtoul(buf, 0, &value)) |
| 1450 | return -EINVAL; |
| 1451 | |
| 1452 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1453 | |
| 1454 | hba->clk_gating.delay_ms_perf = value; |
| 1455 | if (ufshcd_is_clkscaling_supported(hba) && |
| 1456 | hba->clk_scaling.is_scaled_up) |
| 1457 | hba->clk_gating.delay_ms = hba->clk_gating.delay_ms_perf; |
| 1458 | |
| 1459 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1460 | return count; |
| 1461 | } |
| 1462 | |
| 1463 | static ssize_t ufshcd_clkgate_enable_show(struct device *dev, |
| 1464 | struct device_attribute *attr, char *buf) |
| 1465 | { |
| 1466 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1467 | |
| 1468 | return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_gating.is_enabled); |
| 1469 | } |
| 1470 | |
| 1471 | static ssize_t ufshcd_clkgate_enable_store(struct device *dev, |
| 1472 | struct device_attribute *attr, const char *buf, size_t count) |
| 1473 | { |
| 1474 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1475 | unsigned long flags; |
| 1476 | u32 value; |
| 1477 | |
| 1478 | if (kstrtou32(buf, 0, &value)) |
| 1479 | return -EINVAL; |
| 1480 | |
| 1481 | value = !!value; |
| 1482 | if (value == hba->clk_gating.is_enabled) |
| 1483 | goto out; |
| 1484 | |
| 1485 | if (value) { |
| 1486 | ufshcd_release(hba, false); |
| 1487 | } else { |
| 1488 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1489 | hba->clk_gating.active_reqs++; |
| 1490 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1491 | } |
| 1492 | |
| 1493 | hba->clk_gating.is_enabled = value; |
| 1494 | out: |
| 1495 | return count; |
| 1496 | } |
| 1497 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1498 | static void ufshcd_init_clk_gating(struct ufs_hba *hba) |
| 1499 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1500 | struct ufs_clk_gating *gating = &hba->clk_gating; |
| 1501 | |
| 1502 | hba->clk_gating.state = CLKS_ON; |
| 1503 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1504 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1505 | return; |
| 1506 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1507 | INIT_DELAYED_WORK(&gating->gate_work, ufshcd_gate_work); |
| 1508 | INIT_WORK(&gating->ungate_work, ufshcd_ungate_work); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1509 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1510 | gating->is_enabled = true; |
| 1511 | |
| 1512 | /* |
| 1513 | * Scheduling the delayed work after 1 jiffies will make the work to |
| 1514 | * get schedule any time from 0ms to 1000/HZ ms which is not desirable |
| 1515 | * for hibern8 enter work as it may impact the performance if it gets |
| 1516 | * scheduled almost immediately. Hence make sure that hibern8 enter |
| 1517 | * work gets scheduled atleast after 2 jiffies (any time between |
| 1518 | * 1000/HZ ms to 2000/HZ ms). |
| 1519 | */ |
| 1520 | gating->delay_ms_pwr_save = jiffies_to_msecs( |
| 1521 | max_t(unsigned long, |
| 1522 | msecs_to_jiffies(UFSHCD_CLK_GATING_DELAY_MS_PWR_SAVE), |
| 1523 | 2)); |
| 1524 | gating->delay_ms_perf = jiffies_to_msecs( |
| 1525 | max_t(unsigned long, |
| 1526 | msecs_to_jiffies(UFSHCD_CLK_GATING_DELAY_MS_PERF), |
| 1527 | 2)); |
| 1528 | |
| 1529 | /* start with performance mode */ |
| 1530 | gating->delay_ms = gating->delay_ms_perf; |
| 1531 | |
| 1532 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 1533 | goto scaling_not_supported; |
| 1534 | |
| 1535 | gating->delay_pwr_save_attr.show = ufshcd_clkgate_delay_pwr_save_show; |
| 1536 | gating->delay_pwr_save_attr.store = ufshcd_clkgate_delay_pwr_save_store; |
| 1537 | sysfs_attr_init(&gating->delay_pwr_save_attr.attr); |
| 1538 | gating->delay_pwr_save_attr.attr.name = "clkgate_delay_ms_pwr_save"; |
| 1539 | gating->delay_pwr_save_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 1540 | if (device_create_file(hba->dev, &gating->delay_pwr_save_attr)) |
| 1541 | dev_err(hba->dev, "Failed to create sysfs for clkgate_delay_ms_pwr_save\n"); |
| 1542 | |
| 1543 | gating->delay_perf_attr.show = ufshcd_clkgate_delay_perf_show; |
| 1544 | gating->delay_perf_attr.store = ufshcd_clkgate_delay_perf_store; |
| 1545 | sysfs_attr_init(&gating->delay_perf_attr.attr); |
| 1546 | gating->delay_perf_attr.attr.name = "clkgate_delay_ms_perf"; |
| 1547 | gating->delay_perf_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 1548 | if (device_create_file(hba->dev, &gating->delay_perf_attr)) |
| 1549 | dev_err(hba->dev, "Failed to create sysfs for clkgate_delay_ms_perf\n"); |
| 1550 | |
| 1551 | goto add_clkgate_enable; |
| 1552 | |
| 1553 | scaling_not_supported: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1554 | hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show; |
| 1555 | hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store; |
| 1556 | sysfs_attr_init(&hba->clk_gating.delay_attr.attr); |
| 1557 | hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms"; |
| 1558 | hba->clk_gating.delay_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 1559 | if (device_create_file(hba->dev, &hba->clk_gating.delay_attr)) |
| 1560 | dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n"); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1561 | |
| 1562 | add_clkgate_enable: |
| 1563 | gating->enable_attr.show = ufshcd_clkgate_enable_show; |
| 1564 | gating->enable_attr.store = ufshcd_clkgate_enable_store; |
| 1565 | sysfs_attr_init(&gating->enable_attr.attr); |
| 1566 | gating->enable_attr.attr.name = "clkgate_enable"; |
| 1567 | gating->enable_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 1568 | if (device_create_file(hba->dev, &gating->enable_attr)) |
| 1569 | dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n"); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | static void ufshcd_exit_clk_gating(struct ufs_hba *hba) |
| 1573 | { |
| 1574 | if (!ufshcd_is_clkgating_allowed(hba)) |
| 1575 | return; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1576 | if (ufshcd_is_clkscaling_supported(hba)) { |
| 1577 | device_remove_file(hba->dev, |
| 1578 | &hba->clk_gating.delay_pwr_save_attr); |
| 1579 | device_remove_file(hba->dev, &hba->clk_gating.delay_perf_attr); |
| 1580 | } else { |
| 1581 | device_remove_file(hba->dev, &hba->clk_gating.delay_attr); |
| 1582 | } |
| 1583 | device_remove_file(hba->dev, &hba->clk_gating.enable_attr); |
Akinobu Mita | 97cd680 | 2014-11-24 14:24:18 +0900 | [diff] [blame] | 1584 | cancel_work_sync(&hba->clk_gating.ungate_work); |
| 1585 | cancel_delayed_work_sync(&hba->clk_gating.gate_work); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1586 | } |
| 1587 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1588 | static void ufshcd_set_auto_hibern8_timer(struct ufs_hba *hba, u32 delay) |
| 1589 | { |
| 1590 | ufshcd_rmwl(hba, AUTO_HIBERN8_TIMER_SCALE_MASK | |
| 1591 | AUTO_HIBERN8_IDLE_TIMER_MASK, |
| 1592 | AUTO_HIBERN8_TIMER_SCALE_1_MS | delay, |
| 1593 | REG_AUTO_HIBERN8_IDLE_TIMER); |
| 1594 | /* Make sure the timer gets applied before further operations */ |
| 1595 | mb(); |
| 1596 | } |
| 1597 | |
| 1598 | /** |
| 1599 | * ufshcd_hibern8_hold - Make sure that link is not in hibern8. |
| 1600 | * |
| 1601 | * @hba: per adapter instance |
| 1602 | * @async: This indicates whether caller wants to exit hibern8 asynchronously. |
| 1603 | * |
| 1604 | * Exit from hibern8 mode and set the link as active. |
| 1605 | * |
| 1606 | * Return 0 on success, non-zero on failure. |
| 1607 | */ |
| 1608 | static int ufshcd_hibern8_hold(struct ufs_hba *hba, bool async) |
| 1609 | { |
| 1610 | int rc = 0; |
| 1611 | unsigned long flags; |
| 1612 | |
| 1613 | if (!ufshcd_is_hibern8_on_idle_allowed(hba)) |
| 1614 | goto out; |
| 1615 | |
| 1616 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1617 | hba->hibern8_on_idle.active_reqs++; |
| 1618 | |
| 1619 | if (ufshcd_eh_in_progress(hba)) { |
| 1620 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1621 | return 0; |
| 1622 | } |
| 1623 | |
| 1624 | start: |
| 1625 | switch (hba->hibern8_on_idle.state) { |
| 1626 | case HIBERN8_EXITED: |
| 1627 | break; |
| 1628 | case REQ_HIBERN8_ENTER: |
| 1629 | if (cancel_delayed_work(&hba->hibern8_on_idle.enter_work)) { |
| 1630 | hba->hibern8_on_idle.state = HIBERN8_EXITED; |
| 1631 | trace_ufshcd_hibern8_on_idle(dev_name(hba->dev), |
| 1632 | hba->hibern8_on_idle.state); |
| 1633 | break; |
| 1634 | } |
| 1635 | /* |
| 1636 | * If we here, it means Hibern8 enter work is either done or |
| 1637 | * currently running. Hence, fall through to cancel hibern8 |
| 1638 | * work and exit hibern8. |
| 1639 | */ |
| 1640 | case HIBERN8_ENTERED: |
| 1641 | __ufshcd_scsi_block_requests(hba); |
| 1642 | hba->hibern8_on_idle.state = REQ_HIBERN8_EXIT; |
| 1643 | trace_ufshcd_hibern8_on_idle(dev_name(hba->dev), |
| 1644 | hba->hibern8_on_idle.state); |
| 1645 | schedule_work(&hba->hibern8_on_idle.exit_work); |
| 1646 | /* |
| 1647 | * fall through to check if we should wait for this |
| 1648 | * work to be done or not. |
| 1649 | */ |
| 1650 | case REQ_HIBERN8_EXIT: |
| 1651 | if (async) { |
| 1652 | rc = -EAGAIN; |
| 1653 | hba->hibern8_on_idle.active_reqs--; |
| 1654 | break; |
| 1655 | } else { |
| 1656 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1657 | flush_work(&hba->hibern8_on_idle.exit_work); |
| 1658 | /* Make sure state is HIBERN8_EXITED before returning */ |
| 1659 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1660 | goto start; |
| 1661 | } |
| 1662 | default: |
| 1663 | dev_err(hba->dev, "%s: H8 is in invalid state %d\n", |
| 1664 | __func__, hba->hibern8_on_idle.state); |
| 1665 | break; |
| 1666 | } |
| 1667 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1668 | out: |
| 1669 | return rc; |
| 1670 | } |
| 1671 | |
| 1672 | /* host lock must be held before calling this variant */ |
| 1673 | static void __ufshcd_hibern8_release(struct ufs_hba *hba, bool no_sched) |
| 1674 | { |
| 1675 | unsigned long delay_in_jiffies; |
| 1676 | |
| 1677 | if (!ufshcd_is_hibern8_on_idle_allowed(hba)) |
| 1678 | return; |
| 1679 | |
| 1680 | hba->hibern8_on_idle.active_reqs--; |
| 1681 | BUG_ON(hba->hibern8_on_idle.active_reqs < 0); |
| 1682 | |
| 1683 | if (hba->hibern8_on_idle.active_reqs |
| 1684 | || hba->hibern8_on_idle.is_suspended |
| 1685 | || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL |
| 1686 | || hba->lrb_in_use || hba->outstanding_tasks |
| 1687 | || hba->active_uic_cmd || hba->uic_async_done |
| 1688 | || ufshcd_eh_in_progress(hba) || no_sched) |
| 1689 | return; |
| 1690 | |
| 1691 | hba->hibern8_on_idle.state = REQ_HIBERN8_ENTER; |
| 1692 | trace_ufshcd_hibern8_on_idle(dev_name(hba->dev), |
| 1693 | hba->hibern8_on_idle.state); |
| 1694 | /* |
| 1695 | * Scheduling the delayed work after 1 jiffies will make the work to |
| 1696 | * get schedule any time from 0ms to 1000/HZ ms which is not desirable |
| 1697 | * for hibern8 enter work as it may impact the performance if it gets |
| 1698 | * scheduled almost immediately. Hence make sure that hibern8 enter |
| 1699 | * work gets scheduled atleast after 2 jiffies (any time between |
| 1700 | * 1000/HZ ms to 2000/HZ ms). |
| 1701 | */ |
| 1702 | delay_in_jiffies = msecs_to_jiffies(hba->hibern8_on_idle.delay_ms); |
| 1703 | if (delay_in_jiffies == 1) |
| 1704 | delay_in_jiffies++; |
| 1705 | |
| 1706 | schedule_delayed_work(&hba->hibern8_on_idle.enter_work, |
| 1707 | delay_in_jiffies); |
| 1708 | } |
| 1709 | |
| 1710 | static void ufshcd_hibern8_release(struct ufs_hba *hba, bool no_sched) |
| 1711 | { |
| 1712 | unsigned long flags; |
| 1713 | |
| 1714 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1715 | __ufshcd_hibern8_release(hba, no_sched); |
| 1716 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1717 | } |
| 1718 | |
| 1719 | static void ufshcd_hibern8_enter_work(struct work_struct *work) |
| 1720 | { |
| 1721 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1722 | hibern8_on_idle.enter_work.work); |
| 1723 | unsigned long flags; |
| 1724 | |
| 1725 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1726 | if (hba->hibern8_on_idle.is_suspended) { |
| 1727 | hba->hibern8_on_idle.state = HIBERN8_EXITED; |
| 1728 | trace_ufshcd_hibern8_on_idle(dev_name(hba->dev), |
| 1729 | hba->hibern8_on_idle.state); |
| 1730 | goto rel_lock; |
| 1731 | } |
| 1732 | |
| 1733 | if (hba->hibern8_on_idle.active_reqs |
| 1734 | || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL |
| 1735 | || hba->lrb_in_use || hba->outstanding_tasks |
| 1736 | || hba->active_uic_cmd || hba->uic_async_done) |
| 1737 | goto rel_lock; |
| 1738 | |
| 1739 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1740 | |
| 1741 | if (ufshcd_is_link_active(hba) && ufshcd_uic_hibern8_enter(hba)) { |
| 1742 | /* Enter failed */ |
| 1743 | hba->hibern8_on_idle.state = HIBERN8_EXITED; |
| 1744 | trace_ufshcd_hibern8_on_idle(dev_name(hba->dev), |
| 1745 | hba->hibern8_on_idle.state); |
| 1746 | goto out; |
| 1747 | } |
| 1748 | ufshcd_set_link_hibern8(hba); |
| 1749 | |
| 1750 | /* |
| 1751 | * In case you are here to cancel this work the hibern8_on_idle.state |
| 1752 | * would be marked as REQ_HIBERN8_EXIT. In this case keep the state |
| 1753 | * as REQ_HIBERN8_EXIT which would anyway imply that we are in hibern8 |
| 1754 | * and a request to exit from it is pending. By doing this way, |
| 1755 | * we keep the state machine in tact and this would ultimately |
| 1756 | * prevent from doing cancel work multiple times when there are |
| 1757 | * new requests arriving before the current cancel work is done. |
| 1758 | */ |
| 1759 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1760 | if (hba->hibern8_on_idle.state == REQ_HIBERN8_ENTER) { |
| 1761 | hba->hibern8_on_idle.state = HIBERN8_ENTERED; |
| 1762 | trace_ufshcd_hibern8_on_idle(dev_name(hba->dev), |
| 1763 | hba->hibern8_on_idle.state); |
| 1764 | } |
| 1765 | rel_lock: |
| 1766 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1767 | out: |
| 1768 | return; |
| 1769 | } |
| 1770 | |
| 1771 | static void ufshcd_hibern8_exit_work(struct work_struct *work) |
| 1772 | { |
| 1773 | int ret; |
| 1774 | unsigned long flags; |
| 1775 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 1776 | hibern8_on_idle.exit_work); |
| 1777 | |
| 1778 | cancel_delayed_work_sync(&hba->hibern8_on_idle.enter_work); |
| 1779 | |
| 1780 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1781 | if ((hba->hibern8_on_idle.state == HIBERN8_EXITED) |
| 1782 | || ufshcd_is_link_active(hba)) { |
| 1783 | hba->hibern8_on_idle.state = HIBERN8_EXITED; |
| 1784 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1785 | goto unblock_reqs; |
| 1786 | } |
| 1787 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1788 | |
| 1789 | /* Exit from hibern8 */ |
| 1790 | if (ufshcd_is_link_hibern8(hba)) { |
| 1791 | ufshcd_hold(hba, false); |
| 1792 | ret = ufshcd_uic_hibern8_exit(hba); |
| 1793 | ufshcd_release(hba, false); |
| 1794 | if (!ret) { |
| 1795 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1796 | ufshcd_set_link_active(hba); |
| 1797 | hba->hibern8_on_idle.state = HIBERN8_EXITED; |
| 1798 | trace_ufshcd_hibern8_on_idle(dev_name(hba->dev), |
| 1799 | hba->hibern8_on_idle.state); |
| 1800 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1801 | } |
| 1802 | } |
| 1803 | unblock_reqs: |
| 1804 | ufshcd_scsi_unblock_requests(hba); |
| 1805 | } |
| 1806 | |
| 1807 | static ssize_t ufshcd_hibern8_on_idle_delay_show(struct device *dev, |
| 1808 | struct device_attribute *attr, char *buf) |
| 1809 | { |
| 1810 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1811 | |
| 1812 | return snprintf(buf, PAGE_SIZE, "%lu\n", hba->hibern8_on_idle.delay_ms); |
| 1813 | } |
| 1814 | |
| 1815 | static ssize_t ufshcd_hibern8_on_idle_delay_store(struct device *dev, |
| 1816 | struct device_attribute *attr, const char *buf, size_t count) |
| 1817 | { |
| 1818 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1819 | unsigned long flags, value; |
| 1820 | |
| 1821 | if (kstrtoul(buf, 0, &value)) |
| 1822 | return -EINVAL; |
| 1823 | |
| 1824 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1825 | hba->hibern8_on_idle.delay_ms = value; |
| 1826 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1827 | |
| 1828 | /* Update auto hibern8 timer value if supported */ |
| 1829 | if (ufshcd_is_auto_hibern8_supported(hba) && |
| 1830 | hba->hibern8_on_idle.is_enabled) |
| 1831 | ufshcd_set_auto_hibern8_timer(hba, |
| 1832 | hba->hibern8_on_idle.delay_ms); |
| 1833 | |
| 1834 | return count; |
| 1835 | } |
| 1836 | |
| 1837 | static ssize_t ufshcd_hibern8_on_idle_enable_show(struct device *dev, |
| 1838 | struct device_attribute *attr, char *buf) |
| 1839 | { |
| 1840 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1841 | |
| 1842 | return snprintf(buf, PAGE_SIZE, "%d\n", |
| 1843 | hba->hibern8_on_idle.is_enabled); |
| 1844 | } |
| 1845 | |
| 1846 | static ssize_t ufshcd_hibern8_on_idle_enable_store(struct device *dev, |
| 1847 | struct device_attribute *attr, const char *buf, size_t count) |
| 1848 | { |
| 1849 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 1850 | unsigned long flags; |
| 1851 | u32 value; |
| 1852 | |
| 1853 | if (kstrtou32(buf, 0, &value)) |
| 1854 | return -EINVAL; |
| 1855 | |
| 1856 | value = !!value; |
| 1857 | if (value == hba->hibern8_on_idle.is_enabled) |
| 1858 | goto out; |
| 1859 | |
| 1860 | /* Update auto hibern8 timer value if supported */ |
| 1861 | if (ufshcd_is_auto_hibern8_supported(hba)) { |
| 1862 | ufshcd_set_auto_hibern8_timer(hba, |
| 1863 | value ? hba->hibern8_on_idle.delay_ms : value); |
| 1864 | goto update; |
| 1865 | } |
| 1866 | |
| 1867 | if (value) { |
| 1868 | /* |
| 1869 | * As clock gating work would wait for the hibern8 enter work |
| 1870 | * to finish, clocks would remain on during hibern8 enter work. |
| 1871 | */ |
| 1872 | ufshcd_hold(hba, false); |
| 1873 | ufshcd_release_all(hba); |
| 1874 | } else { |
| 1875 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1876 | hba->hibern8_on_idle.active_reqs++; |
| 1877 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1878 | } |
| 1879 | |
| 1880 | update: |
| 1881 | hba->hibern8_on_idle.is_enabled = value; |
| 1882 | out: |
| 1883 | return count; |
| 1884 | } |
| 1885 | |
| 1886 | static void ufshcd_init_hibern8_on_idle(struct ufs_hba *hba) |
| 1887 | { |
| 1888 | /* initialize the state variable here */ |
| 1889 | hba->hibern8_on_idle.state = HIBERN8_EXITED; |
| 1890 | |
| 1891 | if (!ufshcd_is_hibern8_on_idle_allowed(hba) && |
| 1892 | !ufshcd_is_auto_hibern8_supported(hba)) |
| 1893 | return; |
| 1894 | |
| 1895 | if (ufshcd_is_auto_hibern8_supported(hba)) { |
| 1896 | hba->hibern8_on_idle.state = AUTO_HIBERN8; |
| 1897 | /* |
| 1898 | * Disable SW hibern8 enter on idle in case |
| 1899 | * auto hibern8 is supported |
| 1900 | */ |
| 1901 | hba->caps &= ~UFSHCD_CAP_HIBERN8_ENTER_ON_IDLE; |
| 1902 | } else { |
| 1903 | INIT_DELAYED_WORK(&hba->hibern8_on_idle.enter_work, |
| 1904 | ufshcd_hibern8_enter_work); |
| 1905 | INIT_WORK(&hba->hibern8_on_idle.exit_work, |
| 1906 | ufshcd_hibern8_exit_work); |
| 1907 | } |
| 1908 | |
| 1909 | hba->hibern8_on_idle.delay_ms = 10; |
| 1910 | hba->hibern8_on_idle.is_enabled = true; |
| 1911 | |
| 1912 | hba->hibern8_on_idle.delay_attr.show = |
| 1913 | ufshcd_hibern8_on_idle_delay_show; |
| 1914 | hba->hibern8_on_idle.delay_attr.store = |
| 1915 | ufshcd_hibern8_on_idle_delay_store; |
| 1916 | sysfs_attr_init(&hba->hibern8_on_idle.delay_attr.attr); |
| 1917 | hba->hibern8_on_idle.delay_attr.attr.name = "hibern8_on_idle_delay_ms"; |
| 1918 | hba->hibern8_on_idle.delay_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 1919 | if (device_create_file(hba->dev, &hba->hibern8_on_idle.delay_attr)) |
| 1920 | dev_err(hba->dev, "Failed to create sysfs for hibern8_on_idle_delay\n"); |
| 1921 | |
| 1922 | hba->hibern8_on_idle.enable_attr.show = |
| 1923 | ufshcd_hibern8_on_idle_enable_show; |
| 1924 | hba->hibern8_on_idle.enable_attr.store = |
| 1925 | ufshcd_hibern8_on_idle_enable_store; |
| 1926 | sysfs_attr_init(&hba->hibern8_on_idle.enable_attr.attr); |
| 1927 | hba->hibern8_on_idle.enable_attr.attr.name = "hibern8_on_idle_enable"; |
| 1928 | hba->hibern8_on_idle.enable_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 1929 | if (device_create_file(hba->dev, &hba->hibern8_on_idle.enable_attr)) |
| 1930 | dev_err(hba->dev, "Failed to create sysfs for hibern8_on_idle_enable\n"); |
| 1931 | } |
| 1932 | |
| 1933 | static void ufshcd_exit_hibern8_on_idle(struct ufs_hba *hba) |
| 1934 | { |
| 1935 | if (!ufshcd_is_hibern8_on_idle_allowed(hba) && |
| 1936 | !ufshcd_is_auto_hibern8_supported(hba)) |
| 1937 | return; |
| 1938 | device_remove_file(hba->dev, &hba->hibern8_on_idle.delay_attr); |
| 1939 | device_remove_file(hba->dev, &hba->hibern8_on_idle.enable_attr); |
| 1940 | } |
| 1941 | |
| 1942 | static void ufshcd_hold_all(struct ufs_hba *hba) |
| 1943 | { |
| 1944 | ufshcd_hold(hba, false); |
| 1945 | ufshcd_hibern8_hold(hba, false); |
| 1946 | } |
| 1947 | |
| 1948 | static void ufshcd_release_all(struct ufs_hba *hba) |
| 1949 | { |
| 1950 | ufshcd_hibern8_release(hba, false); |
| 1951 | ufshcd_release(hba, false); |
| 1952 | } |
| 1953 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1954 | /* Must be called with host lock acquired */ |
| 1955 | static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba) |
| 1956 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1957 | bool queue_resume_work = false; |
| 1958 | |
| 1959 | if (!ufshcd_is_clkscaling_supported(hba)) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1960 | return; |
| 1961 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1962 | if (!hba->clk_scaling.active_reqs++) |
| 1963 | queue_resume_work = true; |
| 1964 | |
| 1965 | if (!hba->clk_scaling.is_allowed || hba->pm_op_in_progress) |
| 1966 | return; |
| 1967 | |
| 1968 | if (queue_resume_work) |
| 1969 | queue_work(hba->clk_scaling.workq, |
| 1970 | &hba->clk_scaling.resume_work); |
| 1971 | |
| 1972 | if (!hba->clk_scaling.window_start_t) { |
| 1973 | hba->clk_scaling.window_start_t = jiffies; |
| 1974 | hba->clk_scaling.tot_busy_t = 0; |
| 1975 | hba->clk_scaling.is_busy_started = false; |
| 1976 | } |
| 1977 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1978 | if (!hba->clk_scaling.is_busy_started) { |
| 1979 | hba->clk_scaling.busy_start_t = ktime_get(); |
| 1980 | hba->clk_scaling.is_busy_started = true; |
| 1981 | } |
| 1982 | } |
| 1983 | |
| 1984 | static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba) |
| 1985 | { |
| 1986 | struct ufs_clk_scaling *scaling = &hba->clk_scaling; |
| 1987 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1988 | if (!ufshcd_is_clkscaling_supported(hba)) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 1989 | return; |
| 1990 | |
| 1991 | if (!hba->outstanding_reqs && scaling->is_busy_started) { |
| 1992 | scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(), |
| 1993 | scaling->busy_start_t)); |
| 1994 | scaling->busy_start_t = ktime_set(0, 0); |
| 1995 | scaling->is_busy_started = false; |
| 1996 | } |
| 1997 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 1998 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1999 | /** |
| 2000 | * ufshcd_send_command - Send SCSI or device management commands |
| 2001 | * @hba: per adapter instance |
| 2002 | * @task_tag: Task tag of the command |
| 2003 | */ |
| 2004 | static inline |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2005 | int ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2006 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2007 | int ret = 0; |
| 2008 | |
| 2009 | hba->lrb[task_tag].issue_time_stamp = ktime_get(); |
| 2010 | hba->lrb[task_tag].complete_time_stamp = ktime_set(0, 0); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 2011 | ufshcd_clk_scaling_start_busy(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2012 | __set_bit(task_tag, &hba->outstanding_reqs); |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 2013 | ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2014 | /* Make sure that doorbell is committed immediately */ |
| 2015 | wmb(); |
| 2016 | ufshcd_cond_add_cmd_trace(hba, task_tag, "send"); |
| 2017 | ufshcd_update_tag_stats(hba, task_tag); |
| 2018 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | /** |
| 2022 | * ufshcd_copy_sense_data - Copy sense data in case of check condition |
| 2023 | * @lrb - pointer to local reference block |
| 2024 | */ |
| 2025 | static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp) |
| 2026 | { |
| 2027 | int len; |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 2028 | if (lrbp->sense_buffer && |
| 2029 | ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2030 | int len_to_copy; |
| 2031 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2032 | len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2033 | len_to_copy = min_t(int, RESPONSE_UPIU_SENSE_DATA_LENGTH, len); |
| 2034 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2035 | memcpy(lrbp->sense_buffer, |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2036 | lrbp->ucd_rsp_ptr->sr.sense_data, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2037 | min_t(int, len_to_copy, UFSHCD_REQ_SENSE_SIZE)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2038 | } |
| 2039 | } |
| 2040 | |
| 2041 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2042 | * ufshcd_copy_query_response() - Copy the Query Response and the data |
| 2043 | * descriptor |
| 2044 | * @hba: per adapter instance |
| 2045 | * @lrb - pointer to local reference block |
| 2046 | */ |
| 2047 | static |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2048 | int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2049 | { |
| 2050 | struct ufs_query_res *query_res = &hba->dev_cmd.query.response; |
| 2051 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2052 | memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2053 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2054 | /* Get the descriptor */ |
| 2055 | if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2056 | u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2057 | GENERAL_UPIU_REQUEST_SIZE; |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2058 | u16 resp_len; |
| 2059 | u16 buf_len; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2060 | |
| 2061 | /* data segment length */ |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2062 | resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) & |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2063 | MASK_QUERY_DATA_SEG_LEN; |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 2064 | buf_len = be16_to_cpu( |
| 2065 | hba->dev_cmd.query.request.upiu_req.length); |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2066 | if (likely(buf_len >= resp_len)) { |
| 2067 | memcpy(hba->dev_cmd.query.descriptor, descp, resp_len); |
| 2068 | } else { |
| 2069 | dev_warn(hba->dev, |
| 2070 | "%s: Response size is bigger than buffer", |
| 2071 | __func__); |
| 2072 | return -EINVAL; |
| 2073 | } |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2074 | } |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2075 | |
| 2076 | return 0; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2077 | } |
| 2078 | |
| 2079 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2080 | * ufshcd_hba_capabilities - Read controller capabilities |
| 2081 | * @hba: per adapter instance |
| 2082 | */ |
| 2083 | static inline void ufshcd_hba_capabilities(struct ufs_hba *hba) |
| 2084 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 2085 | hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2086 | |
| 2087 | /* nutrs and nutmrs are 0 based values */ |
| 2088 | hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1; |
| 2089 | hba->nutmrs = |
| 2090 | ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1; |
| 2091 | } |
| 2092 | |
| 2093 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2094 | * ufshcd_ready_for_uic_cmd - Check if controller is ready |
| 2095 | * to accept UIC commands |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2096 | * @hba: per adapter instance |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2097 | * Return true on success, else false |
| 2098 | */ |
| 2099 | static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba) |
| 2100 | { |
| 2101 | if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY) |
| 2102 | return true; |
| 2103 | else |
| 2104 | return false; |
| 2105 | } |
| 2106 | |
| 2107 | /** |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 2108 | * ufshcd_get_upmcrs - Get the power mode change request status |
| 2109 | * @hba: Pointer to adapter instance |
| 2110 | * |
| 2111 | * This function gets the UPMCRS field of HCS register |
| 2112 | * Returns value of UPMCRS field |
| 2113 | */ |
| 2114 | static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba) |
| 2115 | { |
| 2116 | return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7; |
| 2117 | } |
| 2118 | |
| 2119 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2120 | * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers |
| 2121 | * @hba: per adapter instance |
| 2122 | * @uic_cmd: UIC command |
| 2123 | * |
| 2124 | * Mutex must be held. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2125 | */ |
| 2126 | static inline void |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2127 | ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2128 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2129 | WARN_ON(hba->active_uic_cmd); |
| 2130 | |
| 2131 | hba->active_uic_cmd = uic_cmd; |
| 2132 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2133 | /* Write Args */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2134 | ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1); |
| 2135 | ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2); |
| 2136 | ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2137 | |
| 2138 | /* Write UIC Cmd */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2139 | ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK, |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 2140 | REG_UIC_COMMAND); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2141 | } |
| 2142 | |
| 2143 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2144 | * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command |
| 2145 | * @hba: per adapter instance |
| 2146 | * @uic_command: UIC command |
| 2147 | * |
| 2148 | * Must be called with mutex held. |
| 2149 | * Returns 0 only if success. |
| 2150 | */ |
| 2151 | static int |
| 2152 | ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) |
| 2153 | { |
| 2154 | int ret; |
| 2155 | unsigned long flags; |
| 2156 | |
| 2157 | if (wait_for_completion_timeout(&uic_cmd->done, |
| 2158 | msecs_to_jiffies(UIC_CMD_TIMEOUT))) |
| 2159 | ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT; |
| 2160 | else |
| 2161 | ret = -ETIMEDOUT; |
| 2162 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2163 | if (ret) |
| 2164 | ufsdbg_set_err_state(hba); |
| 2165 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2166 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2167 | hba->active_uic_cmd = NULL; |
| 2168 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2169 | |
| 2170 | return ret; |
| 2171 | } |
| 2172 | |
| 2173 | /** |
| 2174 | * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result |
| 2175 | * @hba: per adapter instance |
| 2176 | * @uic_cmd: UIC command |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2177 | * @completion: initialize the completion only if this is set to true |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2178 | * |
| 2179 | * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2180 | * with mutex held and host_lock locked. |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2181 | * Returns 0 only if success. |
| 2182 | */ |
| 2183 | static int |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2184 | __ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd, |
| 2185 | bool completion) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2186 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2187 | if (!ufshcd_ready_for_uic_cmd(hba)) { |
| 2188 | dev_err(hba->dev, |
| 2189 | "Controller not ready to accept UIC commands\n"); |
| 2190 | return -EIO; |
| 2191 | } |
| 2192 | |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2193 | if (completion) |
| 2194 | init_completion(&uic_cmd->done); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2195 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2196 | ufshcd_dispatch_uic_cmd(hba, uic_cmd); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2197 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2198 | return 0; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2199 | } |
| 2200 | |
| 2201 | /** |
| 2202 | * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result |
| 2203 | * @hba: per adapter instance |
| 2204 | * @uic_cmd: UIC command |
| 2205 | * |
| 2206 | * Returns 0 only if success. |
| 2207 | */ |
| 2208 | static int |
| 2209 | ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) |
| 2210 | { |
| 2211 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2212 | unsigned long flags; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2213 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2214 | ufshcd_hold_all(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2215 | mutex_lock(&hba->uic_cmd_mutex); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 2216 | ufshcd_add_delay_before_dme_cmd(hba); |
| 2217 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2218 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 2219 | ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 2220 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2221 | if (!ret) |
| 2222 | ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd); |
| 2223 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2224 | ufshcd_save_tstamp_of_last_dme_cmd(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2225 | mutex_unlock(&hba->uic_cmd_mutex); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2226 | ufshcd_release_all(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2227 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2228 | ufsdbg_error_inject_dispatcher(hba, |
| 2229 | ERR_INJECT_UIC, 0, &ret); |
| 2230 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2231 | return ret; |
| 2232 | } |
| 2233 | |
| 2234 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2235 | * ufshcd_map_sg - Map scatter-gather list to prdt |
| 2236 | * @lrbp - pointer to local reference block |
| 2237 | * |
| 2238 | * Returns 0 in case of success, non-zero value in case of failure |
| 2239 | */ |
| 2240 | static int ufshcd_map_sg(struct ufshcd_lrb *lrbp) |
| 2241 | { |
| 2242 | struct ufshcd_sg_entry *prd_table; |
| 2243 | struct scatterlist *sg; |
| 2244 | struct scsi_cmnd *cmd; |
| 2245 | int sg_segments; |
| 2246 | int i; |
| 2247 | |
| 2248 | cmd = lrbp->cmd; |
| 2249 | sg_segments = scsi_dma_map(cmd); |
| 2250 | if (sg_segments < 0) |
| 2251 | return sg_segments; |
| 2252 | |
| 2253 | if (sg_segments) { |
| 2254 | lrbp->utr_descriptor_ptr->prd_table_length = |
| 2255 | cpu_to_le16((u16) (sg_segments)); |
| 2256 | |
| 2257 | prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr; |
| 2258 | |
| 2259 | scsi_for_each_sg(cmd, sg, sg_segments, i) { |
| 2260 | prd_table[i].size = |
| 2261 | cpu_to_le32(((u32) sg_dma_len(sg))-1); |
| 2262 | prd_table[i].base_addr = |
| 2263 | cpu_to_le32(lower_32_bits(sg->dma_address)); |
| 2264 | prd_table[i].upper_addr = |
| 2265 | cpu_to_le32(upper_32_bits(sg->dma_address)); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2266 | prd_table[i].reserved = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2267 | } |
| 2268 | } else { |
| 2269 | lrbp->utr_descriptor_ptr->prd_table_length = 0; |
| 2270 | } |
| 2271 | |
| 2272 | return 0; |
| 2273 | } |
| 2274 | |
| 2275 | /** |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2276 | * ufshcd_enable_intr - enable interrupts |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2277 | * @hba: per adapter instance |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2278 | * @intrs: interrupt bits |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2279 | */ |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2280 | static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2281 | { |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2282 | u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
| 2283 | |
| 2284 | if (hba->ufs_version == UFSHCI_VERSION_10) { |
| 2285 | u32 rw; |
| 2286 | rw = set & INTERRUPT_MASK_RW_VER_10; |
| 2287 | set = rw | ((set ^ intrs) & intrs); |
| 2288 | } else { |
| 2289 | set |= intrs; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2290 | } |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 2291 | |
| 2292 | ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE); |
| 2293 | } |
| 2294 | |
| 2295 | /** |
| 2296 | * ufshcd_disable_intr - disable interrupts |
| 2297 | * @hba: per adapter instance |
| 2298 | * @intrs: interrupt bits |
| 2299 | */ |
| 2300 | static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs) |
| 2301 | { |
| 2302 | u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
| 2303 | |
| 2304 | if (hba->ufs_version == UFSHCI_VERSION_10) { |
| 2305 | u32 rw; |
| 2306 | rw = (set & INTERRUPT_MASK_RW_VER_10) & |
| 2307 | ~(intrs & INTERRUPT_MASK_RW_VER_10); |
| 2308 | set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10); |
| 2309 | |
| 2310 | } else { |
| 2311 | set &= ~intrs; |
| 2312 | } |
| 2313 | |
| 2314 | ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2315 | } |
| 2316 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2317 | static int ufshcd_prepare_crypto_utrd(struct ufs_hba *hba, |
| 2318 | struct ufshcd_lrb *lrbp) |
| 2319 | { |
| 2320 | struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr; |
| 2321 | u8 cc_index = 0; |
| 2322 | bool enable = false; |
| 2323 | u64 dun = 0; |
| 2324 | int ret; |
| 2325 | |
| 2326 | /* |
| 2327 | * Call vendor specific code to get crypto info for this request: |
| 2328 | * enable, crypto config. index, DUN. |
| 2329 | * If bypass is set, don't bother setting the other fields. |
| 2330 | */ |
| 2331 | ret = ufshcd_vops_crypto_req_setup(hba, lrbp, &cc_index, &enable, &dun); |
| 2332 | if (ret) { |
| 2333 | if (ret != -EAGAIN) { |
| 2334 | dev_err(hba->dev, |
| 2335 | "%s: failed to setup crypto request (%d)\n", |
| 2336 | __func__, ret); |
| 2337 | } |
| 2338 | |
| 2339 | return ret; |
| 2340 | } |
| 2341 | |
| 2342 | if (!enable) |
| 2343 | goto out; |
| 2344 | |
| 2345 | req_desc->header.dword_0 |= cc_index | UTRD_CRYPTO_ENABLE; |
| 2346 | if (lrbp->cmd->request && lrbp->cmd->request->bio) |
| 2347 | dun = lrbp->cmd->request->bio->bi_iter.bi_sector; |
| 2348 | |
| 2349 | req_desc->header.dword_1 = (u32)(dun & 0xFFFFFFFF); |
| 2350 | req_desc->header.dword_3 = (u32)((dun >> 32) & 0xFFFFFFFF); |
| 2351 | out: |
| 2352 | return 0; |
| 2353 | } |
| 2354 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2355 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2356 | * ufshcd_prepare_req_desc_hdr() - Fills the requests header |
| 2357 | * descriptor according to request |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2358 | * @hba: per adapter instance |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2359 | * @lrbp: pointer to local reference block |
| 2360 | * @upiu_flags: flags required in the header |
| 2361 | * @cmd_dir: requests data direction |
| 2362 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2363 | static int ufshcd_prepare_req_desc_hdr(struct ufs_hba *hba, |
| 2364 | struct ufshcd_lrb *lrbp, u32 *upiu_flags, |
| 2365 | enum dma_data_direction cmd_dir) |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2366 | { |
| 2367 | struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr; |
| 2368 | u32 data_direction; |
| 2369 | u32 dword_0; |
| 2370 | |
| 2371 | if (cmd_dir == DMA_FROM_DEVICE) { |
| 2372 | data_direction = UTP_DEVICE_TO_HOST; |
| 2373 | *upiu_flags = UPIU_CMD_FLAGS_READ; |
| 2374 | } else if (cmd_dir == DMA_TO_DEVICE) { |
| 2375 | data_direction = UTP_HOST_TO_DEVICE; |
| 2376 | *upiu_flags = UPIU_CMD_FLAGS_WRITE; |
| 2377 | } else { |
| 2378 | data_direction = UTP_NO_DATA_TRANSFER; |
| 2379 | *upiu_flags = UPIU_CMD_FLAGS_NONE; |
| 2380 | } |
| 2381 | |
| 2382 | dword_0 = data_direction | (lrbp->command_type |
| 2383 | << UPIU_COMMAND_TYPE_OFFSET); |
| 2384 | if (lrbp->intr_cmd) |
| 2385 | dword_0 |= UTP_REQ_DESC_INT_CMD; |
| 2386 | |
| 2387 | /* Transfer request descriptor header fields */ |
| 2388 | req_desc->header.dword_0 = cpu_to_le32(dword_0); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2389 | /* dword_1 is reserved, hence it is set to 0 */ |
| 2390 | req_desc->header.dword_1 = 0; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2391 | /* |
| 2392 | * assigning invalid value for command status. Controller |
| 2393 | * updates OCS on command completion, with the command |
| 2394 | * status |
| 2395 | */ |
| 2396 | req_desc->header.dword_2 = |
| 2397 | cpu_to_le32(OCS_INVALID_COMMAND_STATUS); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2398 | /* dword_3 is reserved, hence it is set to 0 */ |
| 2399 | req_desc->header.dword_3 = 0; |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 2400 | |
| 2401 | req_desc->prd_table_length = 0; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2402 | |
| 2403 | if (ufshcd_is_crypto_supported(hba)) |
| 2404 | return ufshcd_prepare_crypto_utrd(hba, lrbp); |
| 2405 | |
| 2406 | return 0; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2407 | } |
| 2408 | |
| 2409 | /** |
| 2410 | * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc, |
| 2411 | * for scsi commands |
| 2412 | * @lrbp - local reference block pointer |
| 2413 | * @upiu_flags - flags |
| 2414 | */ |
| 2415 | static |
| 2416 | void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags) |
| 2417 | { |
| 2418 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2419 | unsigned short cdb_len; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2420 | |
| 2421 | /* command descriptor fields */ |
| 2422 | ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD( |
| 2423 | UPIU_TRANSACTION_COMMAND, upiu_flags, |
| 2424 | lrbp->lun, lrbp->task_tag); |
| 2425 | ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD( |
| 2426 | UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0); |
| 2427 | |
| 2428 | /* Total EHS length and Data segment length will be zero */ |
| 2429 | ucd_req_ptr->header.dword_2 = 0; |
| 2430 | |
| 2431 | ucd_req_ptr->sc.exp_data_transfer_len = |
| 2432 | cpu_to_be32(lrbp->cmd->sdb.length); |
| 2433 | |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2434 | cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2435 | memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2436 | if (cdb_len < MAX_CDB_SIZE) |
| 2437 | memset(ucd_req_ptr->sc.cdb + cdb_len, 0, |
| 2438 | (MAX_CDB_SIZE - cdb_len)); |
Yaniv Gardi | 52ac95f | 2016-02-01 15:02:37 +0200 | [diff] [blame] | 2439 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2440 | } |
| 2441 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2442 | /** |
| 2443 | * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc, |
| 2444 | * for query requsts |
| 2445 | * @hba: UFS hba |
| 2446 | * @lrbp: local reference block pointer |
| 2447 | * @upiu_flags: flags |
| 2448 | */ |
| 2449 | static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba, |
| 2450 | struct ufshcd_lrb *lrbp, u32 upiu_flags) |
| 2451 | { |
| 2452 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
| 2453 | struct ufs_query *query = &hba->dev_cmd.query; |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 2454 | u16 len = be16_to_cpu(query->request.upiu_req.length); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2455 | u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE; |
| 2456 | |
| 2457 | /* Query request header */ |
| 2458 | ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD( |
| 2459 | UPIU_TRANSACTION_QUERY_REQ, upiu_flags, |
| 2460 | lrbp->lun, lrbp->task_tag); |
| 2461 | ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD( |
| 2462 | 0, query->request.query_func, 0, 0); |
| 2463 | |
Zang Leigang | 6861285 | 2016-08-25 17:39:19 +0800 | [diff] [blame] | 2464 | /* Data segment length only need for WRITE_DESC */ |
| 2465 | if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) |
| 2466 | ucd_req_ptr->header.dword_2 = |
| 2467 | UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len); |
| 2468 | else |
| 2469 | ucd_req_ptr->header.dword_2 = 0; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2470 | |
| 2471 | /* Copy the Query Request buffer as is */ |
| 2472 | memcpy(&ucd_req_ptr->qr, &query->request.upiu_req, |
| 2473 | QUERY_OSF_SIZE); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2474 | |
| 2475 | /* Copy the Descriptor */ |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2476 | if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) |
| 2477 | memcpy(descp, query->descriptor, len); |
| 2478 | |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 2479 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2480 | } |
| 2481 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2482 | static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp) |
| 2483 | { |
| 2484 | struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; |
| 2485 | |
| 2486 | memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req)); |
| 2487 | |
| 2488 | /* command descriptor fields */ |
| 2489 | ucd_req_ptr->header.dword_0 = |
| 2490 | UPIU_HEADER_DWORD( |
| 2491 | UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag); |
Yaniv Gardi | 5104726 | 2016-02-01 15:02:38 +0200 | [diff] [blame] | 2492 | /* clear rest of the fields of basic header */ |
| 2493 | ucd_req_ptr->header.dword_1 = 0; |
| 2494 | ucd_req_ptr->header.dword_2 = 0; |
| 2495 | |
| 2496 | memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2497 | } |
| 2498 | |
| 2499 | /** |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2500 | * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU) |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2501 | * @hba - per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2502 | * @lrb - pointer to local reference block |
| 2503 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2504 | static int ufshcd_compose_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2505 | { |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2506 | u32 upiu_flags; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2507 | int ret = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2508 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2509 | switch (lrbp->command_type) { |
| 2510 | case UTP_CMD_TYPE_SCSI: |
| 2511 | if (likely(lrbp->cmd)) { |
| 2512 | ret = ufshcd_prepare_req_desc_hdr(hba, lrbp, |
| 2513 | &upiu_flags, lrbp->cmd->sc_data_direction); |
| 2514 | ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags); |
| 2515 | } else { |
| 2516 | ret = -EINVAL; |
| 2517 | } |
| 2518 | break; |
| 2519 | case UTP_CMD_TYPE_DEV_MANAGE: |
| 2520 | ret = ufshcd_prepare_req_desc_hdr(hba, lrbp, &upiu_flags, |
| 2521 | DMA_NONE); |
| 2522 | if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY) |
| 2523 | ufshcd_prepare_utp_query_req_upiu( |
| 2524 | hba, lrbp, upiu_flags); |
| 2525 | else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP) |
| 2526 | ufshcd_prepare_utp_nop_upiu(lrbp); |
| 2527 | else |
| 2528 | ret = -EINVAL; |
| 2529 | break; |
| 2530 | case UTP_CMD_TYPE_UFS: |
| 2531 | /* For UFS native command implementation */ |
| 2532 | ret = -ENOTSUPP; |
| 2533 | dev_err(hba->dev, "%s: UFS native command are not supported\n", |
| 2534 | __func__); |
| 2535 | break; |
| 2536 | default: |
| 2537 | ret = -ENOTSUPP; |
| 2538 | dev_err(hba->dev, "%s: unknown command type: 0x%x\n", |
| 2539 | __func__, lrbp->command_type); |
| 2540 | break; |
| 2541 | } /* end of switch */ |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2542 | |
| 2543 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2544 | } |
| 2545 | |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 2546 | /* |
| 2547 | * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN |
| 2548 | * @scsi_lun: scsi LUN id |
| 2549 | * |
| 2550 | * Returns UPIU LUN id |
| 2551 | */ |
| 2552 | static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun) |
| 2553 | { |
| 2554 | if (scsi_is_wlun(scsi_lun)) |
| 2555 | return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID) |
| 2556 | | UFS_UPIU_WLUN_ID; |
| 2557 | else |
| 2558 | return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID; |
| 2559 | } |
| 2560 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2561 | /** |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 2562 | * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID |
| 2563 | * @scsi_lun: UPIU W-LUN id |
| 2564 | * |
| 2565 | * Returns SCSI W-LUN id |
| 2566 | */ |
| 2567 | static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id) |
| 2568 | { |
| 2569 | return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE; |
| 2570 | } |
| 2571 | |
| 2572 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2573 | * ufshcd_queuecommand - main entry point for SCSI requests |
| 2574 | * @cmd: command from SCSI Midlayer |
| 2575 | * @done: call back function |
| 2576 | * |
| 2577 | * Returns 0 for success, non-zero in case of failure |
| 2578 | */ |
| 2579 | static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) |
| 2580 | { |
| 2581 | struct ufshcd_lrb *lrbp; |
| 2582 | struct ufs_hba *hba; |
| 2583 | unsigned long flags; |
| 2584 | int tag; |
| 2585 | int err = 0; |
| 2586 | |
| 2587 | hba = shost_priv(host); |
| 2588 | |
| 2589 | tag = cmd->request->tag; |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 2590 | if (!ufshcd_valid_tag(hba, tag)) { |
| 2591 | dev_err(hba->dev, |
| 2592 | "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p", |
| 2593 | __func__, tag, cmd, cmd->request); |
| 2594 | BUG(); |
| 2595 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2596 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2597 | if (!down_read_trylock(&hba->clk_scaling_lock)) |
| 2598 | return SCSI_MLQUEUE_HOST_BUSY; |
| 2599 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2600 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2601 | switch (hba->ufshcd_state) { |
| 2602 | case UFSHCD_STATE_OPERATIONAL: |
| 2603 | break; |
| 2604 | case UFSHCD_STATE_RESET: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2605 | err = SCSI_MLQUEUE_HOST_BUSY; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2606 | goto out_unlock; |
| 2607 | case UFSHCD_STATE_ERROR: |
| 2608 | set_host_byte(cmd, DID_ERROR); |
| 2609 | cmd->scsi_done(cmd); |
| 2610 | goto out_unlock; |
| 2611 | default: |
| 2612 | dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n", |
| 2613 | __func__, hba->ufshcd_state); |
| 2614 | set_host_byte(cmd, DID_BAD_TARGET); |
| 2615 | cmd->scsi_done(cmd); |
| 2616 | goto out_unlock; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2617 | } |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 2618 | |
| 2619 | /* if error handling is in progress, don't issue commands */ |
| 2620 | if (ufshcd_eh_in_progress(hba)) { |
| 2621 | set_host_byte(cmd, DID_ERROR); |
| 2622 | cmd->scsi_done(cmd); |
| 2623 | goto out_unlock; |
| 2624 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2625 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2626 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2627 | hba->req_abort_count = 0; |
| 2628 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2629 | /* acquire the tag to make sure device cmds don't use it */ |
| 2630 | if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) { |
| 2631 | /* |
| 2632 | * Dev manage command in progress, requeue the command. |
| 2633 | * Requeuing the command helps in cases where the request *may* |
| 2634 | * find different tag instead of waiting for dev manage command |
| 2635 | * completion. |
| 2636 | */ |
| 2637 | err = SCSI_MLQUEUE_HOST_BUSY; |
| 2638 | goto out; |
| 2639 | } |
| 2640 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2641 | err = ufshcd_hold(hba, true); |
| 2642 | if (err) { |
| 2643 | err = SCSI_MLQUEUE_HOST_BUSY; |
| 2644 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 2645 | goto out; |
| 2646 | } |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 2647 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2648 | if (ufshcd_is_clkgating_allowed(hba)) |
| 2649 | WARN_ON(hba->clk_gating.state != CLKS_ON); |
| 2650 | |
| 2651 | err = ufshcd_hibern8_hold(hba, true); |
| 2652 | if (err) { |
| 2653 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 2654 | err = SCSI_MLQUEUE_HOST_BUSY; |
| 2655 | ufshcd_release(hba, true); |
| 2656 | goto out; |
| 2657 | } |
| 2658 | if (ufshcd_is_hibern8_on_idle_allowed(hba)) |
| 2659 | WARN_ON(hba->hibern8_on_idle.state != HIBERN8_EXITED); |
| 2660 | |
| 2661 | /* Vote PM QoS for the request */ |
| 2662 | ufshcd_vops_pm_qos_req_start(hba, cmd->request); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 2663 | |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 2664 | /* IO svc time latency histogram */ |
| 2665 | if (hba != NULL && cmd->request != NULL) { |
| 2666 | if (hba->latency_hist_enabled && |
| 2667 | (cmd->request->cmd_type == REQ_TYPE_FS)) { |
| 2668 | cmd->request->lat_hist_io_start = ktime_get(); |
| 2669 | cmd->request->lat_hist_enabled = 1; |
| 2670 | } else |
| 2671 | cmd->request->lat_hist_enabled = 0; |
| 2672 | } |
| 2673 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2674 | WARN_ON(hba->clk_gating.state != CLKS_ON); |
| 2675 | |
| 2676 | lrbp = &hba->lrb[tag]; |
| 2677 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2678 | WARN_ON(lrbp->cmd); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2679 | lrbp->cmd = cmd; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2680 | lrbp->sense_bufflen = UFSHCD_REQ_SENSE_SIZE; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2681 | lrbp->sense_buffer = cmd->sense_buffer; |
| 2682 | lrbp->task_tag = tag; |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 2683 | lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun); |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 2684 | lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2685 | lrbp->command_type = UTP_CMD_TYPE_SCSI; |
| 2686 | lrbp->req_abort_skip = false; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2687 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2688 | /* form UPIU before issuing the command */ |
| 2689 | err = ufshcd_compose_upiu(hba, lrbp); |
| 2690 | if (err) { |
| 2691 | if (err != -EAGAIN) |
| 2692 | dev_err(hba->dev, |
| 2693 | "%s: failed to compose upiu %d\n", |
| 2694 | __func__, err); |
| 2695 | |
| 2696 | lrbp->cmd = NULL; |
| 2697 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 2698 | ufshcd_release_all(hba); |
| 2699 | ufshcd_vops_pm_qos_req_end(hba, cmd->request, true); |
| 2700 | goto out; |
| 2701 | } |
Joao Pinto | 300bb13 | 2016-05-11 12:21:27 +0100 | [diff] [blame] | 2702 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2703 | err = ufshcd_map_sg(lrbp); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2704 | if (err) { |
| 2705 | lrbp->cmd = NULL; |
| 2706 | clear_bit_unlock(tag, &hba->lrb_in_use); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2707 | ufshcd_release_all(hba); |
| 2708 | ufshcd_vops_pm_qos_req_end(hba, cmd->request, true); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2709 | goto out; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2710 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2711 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2712 | err = ufshcd_vops_crypto_engine_cfg_start(hba, tag); |
| 2713 | if (err) { |
| 2714 | if (err != -EAGAIN) |
| 2715 | dev_err(hba->dev, |
| 2716 | "%s: failed to configure crypto engine %d\n", |
| 2717 | __func__, err); |
| 2718 | |
| 2719 | scsi_dma_unmap(lrbp->cmd); |
| 2720 | lrbp->cmd = NULL; |
| 2721 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 2722 | ufshcd_release_all(hba); |
| 2723 | ufshcd_vops_pm_qos_req_end(hba, cmd->request, true); |
| 2724 | |
| 2725 | goto out; |
| 2726 | } |
| 2727 | |
| 2728 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 2729 | wmb(); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2730 | /* issue command to the controller */ |
| 2731 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2732 | |
| 2733 | err = ufshcd_send_command(hba, tag); |
| 2734 | if (err) { |
| 2735 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2736 | scsi_dma_unmap(lrbp->cmd); |
| 2737 | lrbp->cmd = NULL; |
| 2738 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 2739 | ufshcd_release_all(hba); |
| 2740 | ufshcd_vops_pm_qos_req_end(hba, cmd->request, true); |
| 2741 | ufshcd_vops_crypto_engine_cfg_end(hba, lrbp, cmd->request); |
| 2742 | dev_err(hba->dev, "%s: failed sending command, %d\n", |
| 2743 | __func__, err); |
| 2744 | err = DID_ERROR; |
| 2745 | goto out; |
| 2746 | } |
| 2747 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2748 | out_unlock: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2749 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2750 | out: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2751 | up_read(&hba->clk_scaling_lock); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2752 | return err; |
| 2753 | } |
| 2754 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2755 | static int ufshcd_compose_dev_cmd(struct ufs_hba *hba, |
| 2756 | struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag) |
| 2757 | { |
| 2758 | lrbp->cmd = NULL; |
| 2759 | lrbp->sense_bufflen = 0; |
| 2760 | lrbp->sense_buffer = NULL; |
| 2761 | lrbp->task_tag = tag; |
| 2762 | lrbp->lun = 0; /* device management cmd is not specific to any LUN */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2763 | lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2764 | lrbp->intr_cmd = true; /* No interrupt aggregation */ |
| 2765 | hba->dev_cmd.type = cmd_type; |
| 2766 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2767 | return ufshcd_compose_upiu(hba, lrbp); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2768 | } |
| 2769 | |
| 2770 | static int |
| 2771 | ufshcd_clear_cmd(struct ufs_hba *hba, int tag) |
| 2772 | { |
| 2773 | int err = 0; |
| 2774 | unsigned long flags; |
| 2775 | u32 mask = 1 << tag; |
| 2776 | |
| 2777 | /* clear outstanding transaction before retry */ |
| 2778 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2779 | ufshcd_utrl_clear(hba, tag); |
| 2780 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2781 | |
| 2782 | /* |
| 2783 | * wait for for h/w to clear corresponding bit in door-bell. |
| 2784 | * max. wait is 1 sec. |
| 2785 | */ |
| 2786 | err = ufshcd_wait_for_register(hba, |
| 2787 | REG_UTP_TRANSFER_REQ_DOOR_BELL, |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 2788 | mask, ~mask, 1000, 1000, true); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2789 | |
| 2790 | return err; |
| 2791 | } |
| 2792 | |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2793 | static int |
| 2794 | ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 2795 | { |
| 2796 | struct ufs_query_res *query_res = &hba->dev_cmd.query.response; |
| 2797 | |
| 2798 | /* Get the UPIU response */ |
| 2799 | query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >> |
| 2800 | UPIU_RSP_CODE_OFFSET; |
| 2801 | return query_res->response; |
| 2802 | } |
| 2803 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2804 | /** |
| 2805 | * ufshcd_dev_cmd_completion() - handles device management command responses |
| 2806 | * @hba: per adapter instance |
| 2807 | * @lrbp: pointer to local reference block |
| 2808 | */ |
| 2809 | static int |
| 2810 | ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 2811 | { |
| 2812 | int resp; |
| 2813 | int err = 0; |
| 2814 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2815 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2816 | resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr); |
| 2817 | |
| 2818 | switch (resp) { |
| 2819 | case UPIU_TRANSACTION_NOP_IN: |
| 2820 | if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) { |
| 2821 | err = -EINVAL; |
| 2822 | dev_err(hba->dev, "%s: unexpected response %x\n", |
| 2823 | __func__, resp); |
| 2824 | } |
| 2825 | break; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2826 | case UPIU_TRANSACTION_QUERY_RSP: |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 2827 | err = ufshcd_check_query_response(hba, lrbp); |
| 2828 | if (!err) |
| 2829 | err = ufshcd_copy_query_response(hba, lrbp); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2830 | break; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2831 | case UPIU_TRANSACTION_REJECT_UPIU: |
| 2832 | /* TODO: handle Reject UPIU Response */ |
| 2833 | err = -EPERM; |
| 2834 | dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n", |
| 2835 | __func__); |
| 2836 | break; |
| 2837 | default: |
| 2838 | err = -EINVAL; |
| 2839 | dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n", |
| 2840 | __func__, resp); |
| 2841 | break; |
| 2842 | } |
| 2843 | |
| 2844 | return err; |
| 2845 | } |
| 2846 | |
| 2847 | static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba, |
| 2848 | struct ufshcd_lrb *lrbp, int max_timeout) |
| 2849 | { |
| 2850 | int err = 0; |
| 2851 | unsigned long time_left; |
| 2852 | unsigned long flags; |
| 2853 | |
| 2854 | time_left = wait_for_completion_timeout(hba->dev_cmd.complete, |
| 2855 | msecs_to_jiffies(max_timeout)); |
| 2856 | |
| 2857 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2858 | hba->dev_cmd.complete = NULL; |
| 2859 | if (likely(time_left)) { |
| 2860 | err = ufshcd_get_tr_ocs(lrbp); |
| 2861 | if (!err) |
| 2862 | err = ufshcd_dev_cmd_completion(hba, lrbp); |
| 2863 | } |
| 2864 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2865 | |
| 2866 | if (!time_left) { |
| 2867 | err = -ETIMEDOUT; |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 2868 | dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n", |
| 2869 | __func__, lrbp->task_tag); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2870 | if (!ufshcd_clear_cmd(hba, lrbp->task_tag)) |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 2871 | /* successfully cleared the command, retry if needed */ |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2872 | err = -EAGAIN; |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 2873 | /* |
| 2874 | * in case of an error, after clearing the doorbell, |
| 2875 | * we also need to clear the outstanding_request |
| 2876 | * field in hba |
| 2877 | */ |
| 2878 | ufshcd_outstanding_req_clear(hba, lrbp->task_tag); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2879 | } |
| 2880 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2881 | if (err) |
| 2882 | ufsdbg_set_err_state(hba); |
| 2883 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2884 | return err; |
| 2885 | } |
| 2886 | |
| 2887 | /** |
| 2888 | * ufshcd_get_dev_cmd_tag - Get device management command tag |
| 2889 | * @hba: per-adapter instance |
| 2890 | * @tag: pointer to variable with available slot value |
| 2891 | * |
| 2892 | * Get a free slot and lock it until device management command |
| 2893 | * completes. |
| 2894 | * |
| 2895 | * Returns false if free slot is unavailable for locking, else |
| 2896 | * return true with tag value in @tag. |
| 2897 | */ |
| 2898 | static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out) |
| 2899 | { |
| 2900 | int tag; |
| 2901 | bool ret = false; |
| 2902 | unsigned long tmp; |
| 2903 | |
| 2904 | if (!tag_out) |
| 2905 | goto out; |
| 2906 | |
| 2907 | do { |
| 2908 | tmp = ~hba->lrb_in_use; |
| 2909 | tag = find_last_bit(&tmp, hba->nutrs); |
| 2910 | if (tag >= hba->nutrs) |
| 2911 | goto out; |
| 2912 | } while (test_and_set_bit_lock(tag, &hba->lrb_in_use)); |
| 2913 | |
| 2914 | *tag_out = tag; |
| 2915 | ret = true; |
| 2916 | out: |
| 2917 | return ret; |
| 2918 | } |
| 2919 | |
| 2920 | static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag) |
| 2921 | { |
| 2922 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 2923 | } |
| 2924 | |
| 2925 | /** |
| 2926 | * ufshcd_exec_dev_cmd - API for sending device management requests |
| 2927 | * @hba - UFS hba |
| 2928 | * @cmd_type - specifies the type (NOP, Query...) |
| 2929 | * @timeout - time in seconds |
| 2930 | * |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 2931 | * NOTE: Since there is only one available tag for device management commands, |
| 2932 | * it is expected you hold the hba->dev_cmd.lock mutex. |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2933 | */ |
| 2934 | static int ufshcd_exec_dev_cmd(struct ufs_hba *hba, |
| 2935 | enum dev_cmd_type cmd_type, int timeout) |
| 2936 | { |
| 2937 | struct ufshcd_lrb *lrbp; |
| 2938 | int err; |
| 2939 | int tag; |
| 2940 | struct completion wait; |
| 2941 | unsigned long flags; |
| 2942 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2943 | down_read(&hba->clk_scaling_lock); |
| 2944 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2945 | /* |
| 2946 | * Get free slot, sleep if slots are unavailable. |
| 2947 | * Even though we use wait_event() which sleeps indefinitely, |
| 2948 | * the maximum wait time is bounded by SCSI request timeout. |
| 2949 | */ |
| 2950 | wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag)); |
| 2951 | |
| 2952 | init_completion(&wait); |
| 2953 | lrbp = &hba->lrb[tag]; |
| 2954 | WARN_ON(lrbp->cmd); |
| 2955 | err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag); |
| 2956 | if (unlikely(err)) |
| 2957 | goto out_put_tag; |
| 2958 | |
| 2959 | hba->dev_cmd.complete = &wait; |
| 2960 | |
Yaniv Gardi | e3dfdc5 | 2016-02-01 15:02:49 +0200 | [diff] [blame] | 2961 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 2962 | wmb(); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2963 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2964 | err = ufshcd_send_command(hba, tag); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2965 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2966 | if (err) { |
| 2967 | dev_err(hba->dev, "%s: failed sending command, %d\n", |
| 2968 | __func__, err); |
| 2969 | goto out_put_tag; |
| 2970 | } |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2971 | err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout); |
| 2972 | |
| 2973 | out_put_tag: |
| 2974 | ufshcd_put_dev_cmd_tag(hba, tag); |
| 2975 | wake_up(&hba->dev_cmd.tag_wq); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2976 | up_read(&hba->clk_scaling_lock); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2977 | return err; |
| 2978 | } |
| 2979 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2980 | /** |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 2981 | * ufshcd_init_query() - init the query response and request parameters |
| 2982 | * @hba: per-adapter instance |
| 2983 | * @request: address of the request pointer to be initialized |
| 2984 | * @response: address of the response pointer to be initialized |
| 2985 | * @opcode: operation to perform |
| 2986 | * @idn: flag idn to access |
| 2987 | * @index: LU number to access |
| 2988 | * @selector: query/flag/descriptor further identification |
| 2989 | */ |
| 2990 | static inline void ufshcd_init_query(struct ufs_hba *hba, |
| 2991 | struct ufs_query_req **request, struct ufs_query_res **response, |
| 2992 | enum query_opcode opcode, u8 idn, u8 index, u8 selector) |
| 2993 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 2994 | int idn_t = (int)idn; |
| 2995 | |
| 2996 | ufsdbg_error_inject_dispatcher(hba, |
| 2997 | ERR_INJECT_QUERY, idn_t, (int *)&idn_t); |
| 2998 | idn = idn_t; |
| 2999 | |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3000 | *request = &hba->dev_cmd.query.request; |
| 3001 | *response = &hba->dev_cmd.query.response; |
| 3002 | memset(*request, 0, sizeof(struct ufs_query_req)); |
| 3003 | memset(*response, 0, sizeof(struct ufs_query_res)); |
| 3004 | (*request)->upiu_req.opcode = opcode; |
| 3005 | (*request)->upiu_req.idn = idn; |
| 3006 | (*request)->upiu_req.index = index; |
| 3007 | (*request)->upiu_req.selector = selector; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3008 | |
| 3009 | ufshcd_update_query_stats(hba, opcode, idn); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3010 | } |
| 3011 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 3012 | static int ufshcd_query_flag_retry(struct ufs_hba *hba, |
| 3013 | enum query_opcode opcode, enum flag_idn idn, bool *flag_res) |
| 3014 | { |
| 3015 | int ret; |
| 3016 | int retries; |
| 3017 | |
| 3018 | for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) { |
| 3019 | ret = ufshcd_query_flag(hba, opcode, idn, flag_res); |
| 3020 | if (ret) |
| 3021 | dev_dbg(hba->dev, |
| 3022 | "%s: failed with error %d, retries %d\n", |
| 3023 | __func__, ret, retries); |
| 3024 | else |
| 3025 | break; |
| 3026 | } |
| 3027 | |
| 3028 | if (ret) |
| 3029 | dev_err(hba->dev, |
| 3030 | "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n", |
| 3031 | __func__, opcode, idn, ret, retries); |
| 3032 | return ret; |
| 3033 | } |
| 3034 | |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3035 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3036 | * ufshcd_query_flag() - API function for sending flag query requests |
| 3037 | * hba: per-adapter instance |
| 3038 | * query_opcode: flag query to perform |
| 3039 | * idn: flag idn to access |
| 3040 | * flag_res: the flag value after the query request completes |
| 3041 | * |
| 3042 | * Returns 0 for success, non-zero in case of failure |
| 3043 | */ |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 3044 | int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode, |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3045 | enum flag_idn idn, bool *flag_res) |
| 3046 | { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3047 | struct ufs_query_req *request = NULL; |
| 3048 | struct ufs_query_res *response = NULL; |
| 3049 | int err, index = 0, selector = 0; |
Yaniv Gardi | e5ad406 | 2016-02-01 15:02:41 +0200 | [diff] [blame] | 3050 | int timeout = QUERY_REQ_TIMEOUT; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3051 | |
| 3052 | BUG_ON(!hba); |
| 3053 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3054 | ufshcd_hold_all(hba); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3055 | mutex_lock(&hba->dev_cmd.lock); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3056 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 3057 | selector); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3058 | |
| 3059 | switch (opcode) { |
| 3060 | case UPIU_QUERY_OPCODE_SET_FLAG: |
| 3061 | case UPIU_QUERY_OPCODE_CLEAR_FLAG: |
| 3062 | case UPIU_QUERY_OPCODE_TOGGLE_FLAG: |
| 3063 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
| 3064 | break; |
| 3065 | case UPIU_QUERY_OPCODE_READ_FLAG: |
| 3066 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 3067 | if (!flag_res) { |
| 3068 | /* No dummy reads */ |
| 3069 | dev_err(hba->dev, "%s: Invalid argument for read request\n", |
| 3070 | __func__); |
| 3071 | err = -EINVAL; |
| 3072 | goto out_unlock; |
| 3073 | } |
| 3074 | break; |
| 3075 | default: |
| 3076 | dev_err(hba->dev, |
| 3077 | "%s: Expected query flag opcode but got = %d\n", |
| 3078 | __func__, opcode); |
| 3079 | err = -EINVAL; |
| 3080 | goto out_unlock; |
| 3081 | } |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3082 | |
Yaniv Gardi | e5ad406 | 2016-02-01 15:02:41 +0200 | [diff] [blame] | 3083 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3084 | |
| 3085 | if (err) { |
| 3086 | dev_err(hba->dev, |
| 3087 | "%s: Sending flag query for idn %d failed, err = %d\n", |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3088 | __func__, request->upiu_req.idn, err); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3089 | goto out_unlock; |
| 3090 | } |
| 3091 | |
| 3092 | if (flag_res) |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 3093 | *flag_res = (be32_to_cpu(response->upiu_res.value) & |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3094 | MASK_QUERY_UPIU_FLAG_LOC) & 0x1; |
| 3095 | |
| 3096 | out_unlock: |
| 3097 | mutex_unlock(&hba->dev_cmd.lock); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3098 | ufshcd_release_all(hba); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3099 | return err; |
| 3100 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3101 | EXPORT_SYMBOL(ufshcd_query_flag); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3102 | |
| 3103 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3104 | * ufshcd_query_attr - API function for sending attribute requests |
| 3105 | * hba: per-adapter instance |
| 3106 | * opcode: attribute opcode |
| 3107 | * idn: attribute idn to access |
| 3108 | * index: index field |
| 3109 | * selector: selector field |
| 3110 | * attr_val: the attribute value after the query request completes |
| 3111 | * |
| 3112 | * Returns 0 for success, non-zero in case of failure |
| 3113 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3114 | int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3115 | enum attr_idn idn, u8 index, u8 selector, u32 *attr_val) |
| 3116 | { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3117 | struct ufs_query_req *request = NULL; |
| 3118 | struct ufs_query_res *response = NULL; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3119 | int err; |
| 3120 | |
| 3121 | BUG_ON(!hba); |
| 3122 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3123 | ufshcd_hold_all(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3124 | if (!attr_val) { |
| 3125 | dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n", |
| 3126 | __func__, opcode); |
| 3127 | err = -EINVAL; |
| 3128 | goto out; |
| 3129 | } |
| 3130 | |
| 3131 | mutex_lock(&hba->dev_cmd.lock); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3132 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 3133 | selector); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3134 | |
| 3135 | switch (opcode) { |
| 3136 | case UPIU_QUERY_OPCODE_WRITE_ATTR: |
| 3137 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 3138 | request->upiu_req.value = cpu_to_be32(*attr_val); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3139 | break; |
| 3140 | case UPIU_QUERY_OPCODE_READ_ATTR: |
| 3141 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 3142 | break; |
| 3143 | default: |
| 3144 | dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n", |
| 3145 | __func__, opcode); |
| 3146 | err = -EINVAL; |
| 3147 | goto out_unlock; |
| 3148 | } |
| 3149 | |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3150 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3151 | |
| 3152 | if (err) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3153 | dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n", |
| 3154 | __func__, opcode, |
| 3155 | request->upiu_req.idn, index, err); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3156 | goto out_unlock; |
| 3157 | } |
| 3158 | |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 3159 | *attr_val = be32_to_cpu(response->upiu_res.value); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3160 | |
| 3161 | out_unlock: |
| 3162 | mutex_unlock(&hba->dev_cmd.lock); |
| 3163 | out: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3164 | ufshcd_release_all(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3165 | return err; |
| 3166 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3167 | EXPORT_SYMBOL(ufshcd_query_attr); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3168 | |
| 3169 | /** |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 3170 | * ufshcd_query_attr_retry() - API function for sending query |
| 3171 | * attribute with retries |
| 3172 | * @hba: per-adapter instance |
| 3173 | * @opcode: attribute opcode |
| 3174 | * @idn: attribute idn to access |
| 3175 | * @index: index field |
| 3176 | * @selector: selector field |
| 3177 | * @attr_val: the attribute value after the query request |
| 3178 | * completes |
| 3179 | * |
| 3180 | * Returns 0 for success, non-zero in case of failure |
| 3181 | */ |
| 3182 | static int ufshcd_query_attr_retry(struct ufs_hba *hba, |
| 3183 | enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector, |
| 3184 | u32 *attr_val) |
| 3185 | { |
| 3186 | int ret = 0; |
| 3187 | u32 retries; |
| 3188 | |
| 3189 | for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { |
| 3190 | ret = ufshcd_query_attr(hba, opcode, idn, index, |
| 3191 | selector, attr_val); |
| 3192 | if (ret) |
| 3193 | dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n", |
| 3194 | __func__, ret, retries); |
| 3195 | else |
| 3196 | break; |
| 3197 | } |
| 3198 | |
| 3199 | if (ret) |
| 3200 | dev_err(hba->dev, |
| 3201 | "%s: query attribute, idn %d, failed with error %d after %d retires\n", |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3202 | __func__, idn, ret, retries); |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 3203 | return ret; |
| 3204 | } |
| 3205 | |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3206 | static int __ufshcd_query_descriptor(struct ufs_hba *hba, |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3207 | enum query_opcode opcode, enum desc_idn idn, u8 index, |
| 3208 | u8 selector, u8 *desc_buf, int *buf_len) |
| 3209 | { |
| 3210 | struct ufs_query_req *request = NULL; |
| 3211 | struct ufs_query_res *response = NULL; |
| 3212 | int err; |
| 3213 | |
| 3214 | BUG_ON(!hba); |
| 3215 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3216 | ufshcd_hold_all(hba); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3217 | if (!desc_buf) { |
| 3218 | dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n", |
| 3219 | __func__, opcode); |
| 3220 | err = -EINVAL; |
| 3221 | goto out; |
| 3222 | } |
| 3223 | |
| 3224 | if (*buf_len <= QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) { |
| 3225 | dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n", |
| 3226 | __func__, *buf_len); |
| 3227 | err = -EINVAL; |
| 3228 | goto out; |
| 3229 | } |
| 3230 | |
| 3231 | mutex_lock(&hba->dev_cmd.lock); |
| 3232 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 3233 | selector); |
| 3234 | hba->dev_cmd.query.descriptor = desc_buf; |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 3235 | request->upiu_req.length = cpu_to_be16(*buf_len); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3236 | |
| 3237 | switch (opcode) { |
| 3238 | case UPIU_QUERY_OPCODE_WRITE_DESC: |
| 3239 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
| 3240 | break; |
| 3241 | case UPIU_QUERY_OPCODE_READ_DESC: |
| 3242 | request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; |
| 3243 | break; |
| 3244 | default: |
| 3245 | dev_err(hba->dev, |
| 3246 | "%s: Expected query descriptor opcode but got = 0x%.2x\n", |
| 3247 | __func__, opcode); |
| 3248 | err = -EINVAL; |
| 3249 | goto out_unlock; |
| 3250 | } |
| 3251 | |
| 3252 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT); |
| 3253 | |
| 3254 | if (err) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3255 | dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n", |
| 3256 | __func__, opcode, |
| 3257 | request->upiu_req.idn, index, err); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3258 | goto out_unlock; |
| 3259 | } |
| 3260 | |
| 3261 | hba->dev_cmd.query.descriptor = NULL; |
Sujit Reddy Thumma | ea2aab2 | 2014-07-23 09:31:12 +0300 | [diff] [blame] | 3262 | *buf_len = be16_to_cpu(response->upiu_res.length); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3263 | |
| 3264 | out_unlock: |
| 3265 | mutex_unlock(&hba->dev_cmd.lock); |
| 3266 | out: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3267 | ufshcd_release_all(hba); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 3268 | return err; |
| 3269 | } |
| 3270 | |
| 3271 | /** |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3272 | * ufshcd_query_descriptor - API function for sending descriptor requests |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3273 | * hba: per-adapter instance |
| 3274 | * opcode: attribute opcode |
| 3275 | * idn: attribute idn to access |
| 3276 | * index: index field |
| 3277 | * selector: selector field |
| 3278 | * desc_buf: the buffer that contains the descriptor |
| 3279 | * buf_len: length parameter passed to the device |
| 3280 | * |
| 3281 | * Returns 0 for success, non-zero in case of failure. |
| 3282 | * The buf_len parameter will contain, on return, the length parameter |
| 3283 | * received on the response. |
| 3284 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3285 | int ufshcd_query_descriptor(struct ufs_hba *hba, |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3286 | enum query_opcode opcode, enum desc_idn idn, u8 index, |
| 3287 | u8 selector, u8 *desc_buf, int *buf_len) |
| 3288 | { |
| 3289 | int err; |
| 3290 | int retries; |
| 3291 | |
| 3292 | for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { |
| 3293 | err = __ufshcd_query_descriptor(hba, opcode, idn, index, |
| 3294 | selector, desc_buf, buf_len); |
| 3295 | if (!err || err == -EINVAL) |
| 3296 | break; |
| 3297 | } |
| 3298 | |
| 3299 | return err; |
| 3300 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3301 | EXPORT_SYMBOL(ufshcd_query_descriptor); |
Yaniv Gardi | a70e91b | 2016-03-10 17:37:14 +0200 | [diff] [blame] | 3302 | |
| 3303 | /** |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3304 | * ufshcd_read_desc_param - read the specified descriptor parameter |
| 3305 | * @hba: Pointer to adapter instance |
| 3306 | * @desc_id: descriptor idn value |
| 3307 | * @desc_index: descriptor index |
| 3308 | * @param_offset: offset of the parameter to read |
| 3309 | * @param_read_buf: pointer to buffer where parameter would be read |
| 3310 | * @param_size: sizeof(param_read_buf) |
| 3311 | * |
| 3312 | * Return 0 in case of success, non-zero otherwise |
| 3313 | */ |
| 3314 | static int ufshcd_read_desc_param(struct ufs_hba *hba, |
| 3315 | enum desc_idn desc_id, |
| 3316 | int desc_index, |
| 3317 | u32 param_offset, |
| 3318 | u8 *param_read_buf, |
| 3319 | u32 param_size) |
| 3320 | { |
| 3321 | int ret; |
| 3322 | u8 *desc_buf; |
| 3323 | u32 buff_len; |
| 3324 | bool is_kmalloc = true; |
| 3325 | |
| 3326 | /* safety checks */ |
| 3327 | if (desc_id >= QUERY_DESC_IDN_MAX) |
| 3328 | return -EINVAL; |
| 3329 | |
| 3330 | buff_len = ufs_query_desc_max_size[desc_id]; |
| 3331 | if ((param_offset + param_size) > buff_len) |
| 3332 | return -EINVAL; |
| 3333 | |
| 3334 | if (!param_offset && (param_size == buff_len)) { |
| 3335 | /* memory space already available to hold full descriptor */ |
| 3336 | desc_buf = param_read_buf; |
| 3337 | is_kmalloc = false; |
| 3338 | } else { |
| 3339 | /* allocate memory to hold full descriptor */ |
| 3340 | desc_buf = kmalloc(buff_len, GFP_KERNEL); |
| 3341 | if (!desc_buf) |
| 3342 | return -ENOMEM; |
| 3343 | } |
| 3344 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3345 | ret = ufshcd_query_descriptor(hba, UPIU_QUERY_OPCODE_READ_DESC, |
| 3346 | desc_id, desc_index, 0, desc_buf, |
| 3347 | &buff_len); |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3348 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3349 | if (ret) { |
| 3350 | dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d", |
| 3351 | __func__, desc_id, desc_index, param_offset, ret); |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3352 | |
| 3353 | goto out; |
| 3354 | } |
| 3355 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3356 | /* Sanity check */ |
| 3357 | if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) { |
| 3358 | dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header", |
| 3359 | __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]); |
| 3360 | ret = -EINVAL; |
| 3361 | goto out; |
| 3362 | } |
| 3363 | |
| 3364 | /* |
| 3365 | * While reading variable size descriptors (like string descriptor), |
| 3366 | * some UFS devices may report the "LENGTH" (field in "Transaction |
| 3367 | * Specific fields" of Query Response UPIU) same as what was requested |
| 3368 | * in Query Request UPIU instead of reporting the actual size of the |
| 3369 | * variable size descriptor. |
| 3370 | * Although it's safe to ignore the "LENGTH" field for variable size |
| 3371 | * descriptors as we can always derive the length of the descriptor from |
| 3372 | * the descriptor header fields. Hence this change impose the length |
| 3373 | * match check only for fixed size descriptors (for which we always |
| 3374 | * request the correct size as part of Query Request UPIU). |
| 3375 | */ |
| 3376 | if ((desc_id != QUERY_DESC_IDN_STRING) && |
| 3377 | (buff_len != desc_buf[QUERY_DESC_LENGTH_OFFSET])) { |
| 3378 | dev_err(hba->dev, "%s: desc_buf length mismatch: buff_len %d, buff_len(desc_header) %d", |
| 3379 | __func__, buff_len, desc_buf[QUERY_DESC_LENGTH_OFFSET]); |
| 3380 | ret = -EINVAL; |
| 3381 | goto out; |
| 3382 | } |
| 3383 | |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3384 | if (is_kmalloc) |
| 3385 | memcpy(param_read_buf, &desc_buf[param_offset], param_size); |
| 3386 | out: |
| 3387 | if (is_kmalloc) |
| 3388 | kfree(desc_buf); |
| 3389 | return ret; |
| 3390 | } |
| 3391 | |
| 3392 | static inline int ufshcd_read_desc(struct ufs_hba *hba, |
| 3393 | enum desc_idn desc_id, |
| 3394 | int desc_index, |
| 3395 | u8 *buf, |
| 3396 | u32 size) |
| 3397 | { |
| 3398 | return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size); |
| 3399 | } |
| 3400 | |
| 3401 | static inline int ufshcd_read_power_desc(struct ufs_hba *hba, |
| 3402 | u8 *buf, |
| 3403 | u32 size) |
| 3404 | { |
| 3405 | return ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size); |
| 3406 | } |
| 3407 | |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3408 | int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size) |
| 3409 | { |
| 3410 | return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size); |
| 3411 | } |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3412 | |
| 3413 | /** |
| 3414 | * ufshcd_read_string_desc - read string descriptor |
| 3415 | * @hba: pointer to adapter instance |
| 3416 | * @desc_index: descriptor index |
| 3417 | * @buf: pointer to buffer where descriptor would be read |
| 3418 | * @size: size of buf |
| 3419 | * @ascii: if true convert from unicode to ascii characters |
| 3420 | * |
| 3421 | * Return 0 in case of success, non-zero otherwise |
| 3422 | */ |
| 3423 | int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index, u8 *buf, |
| 3424 | u32 size, bool ascii) |
| 3425 | { |
| 3426 | int err = 0; |
| 3427 | |
| 3428 | err = ufshcd_read_desc(hba, |
| 3429 | QUERY_DESC_IDN_STRING, desc_index, buf, size); |
| 3430 | |
| 3431 | if (err) { |
| 3432 | dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n", |
| 3433 | __func__, QUERY_REQ_RETRIES, err); |
| 3434 | goto out; |
| 3435 | } |
| 3436 | |
| 3437 | if (ascii) { |
| 3438 | int desc_len; |
| 3439 | int ascii_len; |
| 3440 | int i; |
| 3441 | char *buff_ascii; |
| 3442 | |
| 3443 | desc_len = buf[0]; |
| 3444 | /* remove header and divide by 2 to move from UTF16 to UTF8 */ |
| 3445 | ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1; |
| 3446 | if (size < ascii_len + QUERY_DESC_HDR_SIZE) { |
| 3447 | dev_err(hba->dev, "%s: buffer allocated size is too small\n", |
| 3448 | __func__); |
| 3449 | err = -ENOMEM; |
| 3450 | goto out; |
| 3451 | } |
| 3452 | |
| 3453 | buff_ascii = kmalloc(ascii_len, GFP_KERNEL); |
| 3454 | if (!buff_ascii) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3455 | dev_err(hba->dev, "%s: Failed allocating %d bytes\n", |
| 3456 | __func__, ascii_len); |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3457 | err = -ENOMEM; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3458 | goto out_free_buff; |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3459 | } |
| 3460 | |
| 3461 | /* |
| 3462 | * the descriptor contains string in UTF16 format |
| 3463 | * we need to convert to utf-8 so it can be displayed |
| 3464 | */ |
| 3465 | utf16s_to_utf8s((wchar_t *)&buf[QUERY_DESC_HDR_SIZE], |
| 3466 | desc_len - QUERY_DESC_HDR_SIZE, |
| 3467 | UTF16_BIG_ENDIAN, buff_ascii, ascii_len); |
| 3468 | |
| 3469 | /* replace non-printable or non-ASCII characters with spaces */ |
| 3470 | for (i = 0; i < ascii_len; i++) |
| 3471 | ufshcd_remove_non_printable(&buff_ascii[i]); |
| 3472 | |
| 3473 | memset(buf + QUERY_DESC_HDR_SIZE, 0, |
| 3474 | size - QUERY_DESC_HDR_SIZE); |
| 3475 | memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len); |
| 3476 | buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3477 | out_free_buff: |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3478 | kfree(buff_ascii); |
| 3479 | } |
| 3480 | out: |
| 3481 | return err; |
| 3482 | } |
Yaniv Gardi | b573d48 | 2016-03-10 17:37:09 +0200 | [diff] [blame] | 3483 | |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3484 | /** |
| 3485 | * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter |
| 3486 | * @hba: Pointer to adapter instance |
| 3487 | * @lun: lun id |
| 3488 | * @param_offset: offset of the parameter to read |
| 3489 | * @param_read_buf: pointer to buffer where parameter would be read |
| 3490 | * @param_size: sizeof(param_read_buf) |
| 3491 | * |
| 3492 | * Return 0 in case of success, non-zero otherwise |
| 3493 | */ |
| 3494 | static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba, |
| 3495 | int lun, |
| 3496 | enum unit_desc_param param_offset, |
| 3497 | u8 *param_read_buf, |
| 3498 | u32 param_size) |
| 3499 | { |
| 3500 | /* |
| 3501 | * Unit descriptors are only available for general purpose LUs (LUN id |
| 3502 | * from 0 to 7) and RPMB Well known LU. |
| 3503 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3504 | if (!ufs_is_valid_unit_desc_lun(lun)) |
Subhash Jadavani | da461ce | 2014-09-25 15:32:25 +0300 | [diff] [blame] | 3505 | return -EOPNOTSUPP; |
| 3506 | |
| 3507 | return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun, |
| 3508 | param_offset, param_read_buf, param_size); |
| 3509 | } |
| 3510 | |
| 3511 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3512 | * ufshcd_memory_alloc - allocate memory for host memory space data structures |
| 3513 | * @hba: per adapter instance |
| 3514 | * |
| 3515 | * 1. Allocate DMA memory for Command Descriptor array |
| 3516 | * Each command descriptor consist of Command UPIU, Response UPIU and PRDT |
| 3517 | * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL). |
| 3518 | * 3. Allocate DMA memory for UTP Task Management Request Descriptor List |
| 3519 | * (UTMRDL) |
| 3520 | * 4. Allocate memory for local reference block(lrb). |
| 3521 | * |
| 3522 | * Returns 0 for success, non-zero in case of failure |
| 3523 | */ |
| 3524 | static int ufshcd_memory_alloc(struct ufs_hba *hba) |
| 3525 | { |
| 3526 | size_t utmrdl_size, utrdl_size, ucdl_size; |
| 3527 | |
| 3528 | /* Allocate memory for UTP command descriptors */ |
| 3529 | ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3530 | hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 3531 | ucdl_size, |
| 3532 | &hba->ucdl_dma_addr, |
| 3533 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3534 | |
| 3535 | /* |
| 3536 | * UFSHCI requires UTP command descriptor to be 128 byte aligned. |
| 3537 | * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE |
| 3538 | * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will |
| 3539 | * be aligned to 128 bytes as well |
| 3540 | */ |
| 3541 | if (!hba->ucdl_base_addr || |
| 3542 | WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3543 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3544 | "Command Descriptor Memory allocation failed\n"); |
| 3545 | goto out; |
| 3546 | } |
| 3547 | |
| 3548 | /* |
| 3549 | * Allocate memory for UTP Transfer descriptors |
| 3550 | * UFSHCI requires 1024 byte alignment of UTRD |
| 3551 | */ |
| 3552 | utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3553 | hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 3554 | utrdl_size, |
| 3555 | &hba->utrdl_dma_addr, |
| 3556 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3557 | if (!hba->utrdl_base_addr || |
| 3558 | WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3559 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3560 | "Transfer Descriptor Memory allocation failed\n"); |
| 3561 | goto out; |
| 3562 | } |
| 3563 | |
| 3564 | /* |
| 3565 | * Allocate memory for UTP Task Management descriptors |
| 3566 | * UFSHCI requires 1024 byte alignment of UTMRD |
| 3567 | */ |
| 3568 | utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs; |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3569 | hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 3570 | utmrdl_size, |
| 3571 | &hba->utmrdl_dma_addr, |
| 3572 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3573 | if (!hba->utmrdl_base_addr || |
| 3574 | WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3575 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3576 | "Task Management Descriptor Memory allocation failed\n"); |
| 3577 | goto out; |
| 3578 | } |
| 3579 | |
| 3580 | /* Allocate memory for local reference block */ |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3581 | hba->lrb = devm_kzalloc(hba->dev, |
| 3582 | hba->nutrs * sizeof(struct ufshcd_lrb), |
| 3583 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3584 | if (!hba->lrb) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3585 | dev_err(hba->dev, "LRB Memory allocation failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3586 | goto out; |
| 3587 | } |
| 3588 | return 0; |
| 3589 | out: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3590 | return -ENOMEM; |
| 3591 | } |
| 3592 | |
| 3593 | /** |
| 3594 | * ufshcd_host_memory_configure - configure local reference block with |
| 3595 | * memory offsets |
| 3596 | * @hba: per adapter instance |
| 3597 | * |
| 3598 | * Configure Host memory space |
| 3599 | * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA |
| 3600 | * address. |
| 3601 | * 2. Update each UTRD with Response UPIU offset, Response UPIU length |
| 3602 | * and PRDT offset. |
| 3603 | * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT |
| 3604 | * into local reference block. |
| 3605 | */ |
| 3606 | static void ufshcd_host_memory_configure(struct ufs_hba *hba) |
| 3607 | { |
| 3608 | struct utp_transfer_cmd_desc *cmd_descp; |
| 3609 | struct utp_transfer_req_desc *utrdlp; |
| 3610 | dma_addr_t cmd_desc_dma_addr; |
| 3611 | dma_addr_t cmd_desc_element_addr; |
| 3612 | u16 response_offset; |
| 3613 | u16 prdt_offset; |
| 3614 | int cmd_desc_size; |
| 3615 | int i; |
| 3616 | |
| 3617 | utrdlp = hba->utrdl_base_addr; |
| 3618 | cmd_descp = hba->ucdl_base_addr; |
| 3619 | |
| 3620 | response_offset = |
| 3621 | offsetof(struct utp_transfer_cmd_desc, response_upiu); |
| 3622 | prdt_offset = |
| 3623 | offsetof(struct utp_transfer_cmd_desc, prd_table); |
| 3624 | |
| 3625 | cmd_desc_size = sizeof(struct utp_transfer_cmd_desc); |
| 3626 | cmd_desc_dma_addr = hba->ucdl_dma_addr; |
| 3627 | |
| 3628 | for (i = 0; i < hba->nutrs; i++) { |
| 3629 | /* Configure UTRD with command descriptor base address */ |
| 3630 | cmd_desc_element_addr = |
| 3631 | (cmd_desc_dma_addr + (cmd_desc_size * i)); |
| 3632 | utrdlp[i].command_desc_base_addr_lo = |
| 3633 | cpu_to_le32(lower_32_bits(cmd_desc_element_addr)); |
| 3634 | utrdlp[i].command_desc_base_addr_hi = |
| 3635 | cpu_to_le32(upper_32_bits(cmd_desc_element_addr)); |
| 3636 | |
| 3637 | /* Response upiu and prdt offset should be in double words */ |
| 3638 | utrdlp[i].response_upiu_offset = |
| 3639 | cpu_to_le16((response_offset >> 2)); |
| 3640 | utrdlp[i].prd_table_offset = |
| 3641 | cpu_to_le16((prdt_offset >> 2)); |
| 3642 | utrdlp[i].response_upiu_length = |
Sujit Reddy Thumma | 3ca316c | 2013-06-26 22:39:30 +0530 | [diff] [blame] | 3643 | cpu_to_le16(ALIGNED_UPIU_SIZE >> 2); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3644 | |
| 3645 | hba->lrb[i].utr_descriptor_ptr = (utrdlp + i); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3646 | hba->lrb[i].utrd_dma_addr = hba->utrdl_dma_addr + |
| 3647 | (i * sizeof(struct utp_transfer_req_desc)); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3648 | hba->lrb[i].ucd_req_ptr = |
| 3649 | (struct utp_upiu_req *)(cmd_descp + i); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3650 | hba->lrb[i].ucd_req_dma_addr = cmd_desc_element_addr; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3651 | hba->lrb[i].ucd_rsp_ptr = |
| 3652 | (struct utp_upiu_rsp *)cmd_descp[i].response_upiu; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3653 | hba->lrb[i].ucd_rsp_dma_addr = cmd_desc_element_addr + |
| 3654 | response_offset; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3655 | hba->lrb[i].ucd_prdt_ptr = |
| 3656 | (struct ufshcd_sg_entry *)cmd_descp[i].prd_table; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3657 | hba->lrb[i].ucd_prdt_dma_addr = cmd_desc_element_addr + |
| 3658 | prdt_offset; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3659 | } |
| 3660 | } |
| 3661 | |
| 3662 | /** |
| 3663 | * ufshcd_dme_link_startup - Notify Unipro to perform link startup |
| 3664 | * @hba: per adapter instance |
| 3665 | * |
| 3666 | * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer, |
| 3667 | * in order to initialize the Unipro link startup procedure. |
| 3668 | * Once the Unipro links are up, the device connected to the controller |
| 3669 | * is detected. |
| 3670 | * |
| 3671 | * Returns 0 on success, non-zero value on failure |
| 3672 | */ |
| 3673 | static int ufshcd_dme_link_startup(struct ufs_hba *hba) |
| 3674 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3675 | struct uic_command uic_cmd = {0}; |
| 3676 | int ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3677 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3678 | uic_cmd.command = UIC_CMD_DME_LINK_STARTUP; |
| 3679 | |
| 3680 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3681 | if (ret) |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3682 | dev_dbg(hba->dev, |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3683 | "dme-link-startup: error code %d\n", ret); |
| 3684 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3685 | } |
| 3686 | |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 3687 | static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba) |
| 3688 | { |
| 3689 | #define MIN_DELAY_BEFORE_DME_CMDS_US 1000 |
| 3690 | unsigned long min_sleep_time_us; |
| 3691 | |
| 3692 | if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS)) |
| 3693 | return; |
| 3694 | |
| 3695 | /* |
| 3696 | * last_dme_cmd_tstamp will be 0 only for 1st call to |
| 3697 | * this function |
| 3698 | */ |
| 3699 | if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) { |
| 3700 | min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US; |
| 3701 | } else { |
| 3702 | unsigned long delta = |
| 3703 | (unsigned long) ktime_to_us( |
| 3704 | ktime_sub(ktime_get(), |
| 3705 | hba->last_dme_cmd_tstamp)); |
| 3706 | |
| 3707 | if (delta < MIN_DELAY_BEFORE_DME_CMDS_US) |
| 3708 | min_sleep_time_us = |
| 3709 | MIN_DELAY_BEFORE_DME_CMDS_US - delta; |
| 3710 | else |
| 3711 | return; /* no more delay required */ |
| 3712 | } |
| 3713 | |
| 3714 | /* allow sleep for extra 50us if needed */ |
| 3715 | usleep_range(min_sleep_time_us, min_sleep_time_us + 50); |
| 3716 | } |
| 3717 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3718 | static inline void ufshcd_save_tstamp_of_last_dme_cmd( |
| 3719 | struct ufs_hba *hba) |
| 3720 | { |
| 3721 | if (hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS) |
| 3722 | hba->last_dme_cmd_tstamp = ktime_get(); |
| 3723 | } |
| 3724 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3725 | /** |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3726 | * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET |
| 3727 | * @hba: per adapter instance |
| 3728 | * @attr_sel: uic command argument1 |
| 3729 | * @attr_set: attribute set type as uic command argument2 |
| 3730 | * @mib_val: setting value as uic command argument3 |
| 3731 | * @peer: indicate whether peer or local |
| 3732 | * |
| 3733 | * Returns 0 on success, non-zero value on failure |
| 3734 | */ |
| 3735 | int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel, |
| 3736 | u8 attr_set, u32 mib_val, u8 peer) |
| 3737 | { |
| 3738 | struct uic_command uic_cmd = {0}; |
| 3739 | static const char *const action[] = { |
| 3740 | "dme-set", |
| 3741 | "dme-peer-set" |
| 3742 | }; |
| 3743 | const char *set = action[!!peer]; |
| 3744 | int ret; |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3745 | int retries = UFS_UIC_COMMAND_RETRIES; |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3746 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3747 | ufsdbg_error_inject_dispatcher(hba, |
| 3748 | ERR_INJECT_DME_ATTR, attr_sel, &attr_sel); |
| 3749 | |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3750 | uic_cmd.command = peer ? |
| 3751 | UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET; |
| 3752 | uic_cmd.argument1 = attr_sel; |
| 3753 | uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set); |
| 3754 | uic_cmd.argument3 = mib_val; |
| 3755 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3756 | do { |
| 3757 | /* for peer attributes we retry upon failure */ |
| 3758 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3759 | if (ret) |
| 3760 | dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n", |
| 3761 | set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret); |
| 3762 | } while (ret && peer && --retries); |
| 3763 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3764 | if (ret) |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3765 | dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n", |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3766 | set, UIC_GET_ATTR_ID(attr_sel), mib_val, |
| 3767 | UFS_UIC_COMMAND_RETRIES - retries); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3768 | |
| 3769 | return ret; |
| 3770 | } |
| 3771 | EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr); |
| 3772 | |
| 3773 | /** |
| 3774 | * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET |
| 3775 | * @hba: per adapter instance |
| 3776 | * @attr_sel: uic command argument1 |
| 3777 | * @mib_val: the value of the attribute as returned by the UIC command |
| 3778 | * @peer: indicate whether peer or local |
| 3779 | * |
| 3780 | * Returns 0 on success, non-zero value on failure |
| 3781 | */ |
| 3782 | int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel, |
| 3783 | u32 *mib_val, u8 peer) |
| 3784 | { |
| 3785 | struct uic_command uic_cmd = {0}; |
| 3786 | static const char *const action[] = { |
| 3787 | "dme-get", |
| 3788 | "dme-peer-get" |
| 3789 | }; |
| 3790 | const char *get = action[!!peer]; |
| 3791 | int ret; |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3792 | int retries = UFS_UIC_COMMAND_RETRIES; |
Yaniv Gardi | 874237f | 2015-05-17 18:55:03 +0300 | [diff] [blame] | 3793 | struct ufs_pa_layer_attr orig_pwr_info; |
| 3794 | struct ufs_pa_layer_attr temp_pwr_info; |
| 3795 | bool pwr_mode_change = false; |
| 3796 | |
| 3797 | if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) { |
| 3798 | orig_pwr_info = hba->pwr_info; |
| 3799 | temp_pwr_info = orig_pwr_info; |
| 3800 | |
| 3801 | if (orig_pwr_info.pwr_tx == FAST_MODE || |
| 3802 | orig_pwr_info.pwr_rx == FAST_MODE) { |
| 3803 | temp_pwr_info.pwr_tx = FASTAUTO_MODE; |
| 3804 | temp_pwr_info.pwr_rx = FASTAUTO_MODE; |
| 3805 | pwr_mode_change = true; |
| 3806 | } else if (orig_pwr_info.pwr_tx == SLOW_MODE || |
| 3807 | orig_pwr_info.pwr_rx == SLOW_MODE) { |
| 3808 | temp_pwr_info.pwr_tx = SLOWAUTO_MODE; |
| 3809 | temp_pwr_info.pwr_rx = SLOWAUTO_MODE; |
| 3810 | pwr_mode_change = true; |
| 3811 | } |
| 3812 | if (pwr_mode_change) { |
| 3813 | ret = ufshcd_change_power_mode(hba, &temp_pwr_info); |
| 3814 | if (ret) |
| 3815 | goto out; |
| 3816 | } |
| 3817 | } |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3818 | |
| 3819 | uic_cmd.command = peer ? |
| 3820 | UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3821 | |
| 3822 | ufsdbg_error_inject_dispatcher(hba, |
| 3823 | ERR_INJECT_DME_ATTR, attr_sel, &attr_sel); |
| 3824 | |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3825 | uic_cmd.argument1 = attr_sel; |
| 3826 | |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3827 | do { |
| 3828 | /* for peer attributes we retry upon failure */ |
| 3829 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 3830 | if (ret) |
| 3831 | dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n", |
| 3832 | get, UIC_GET_ATTR_ID(attr_sel), ret); |
| 3833 | } while (ret && peer && --retries); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3834 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3835 | if (ret) |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3836 | dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n", |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3837 | get, UIC_GET_ATTR_ID(attr_sel), |
| 3838 | UFS_UIC_COMMAND_RETRIES - retries); |
Yaniv Gardi | 64238fb | 2016-02-01 15:02:43 +0200 | [diff] [blame] | 3839 | |
| 3840 | if (mib_val && !ret) |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3841 | *mib_val = uic_cmd.argument3; |
Yaniv Gardi | 874237f | 2015-05-17 18:55:03 +0300 | [diff] [blame] | 3842 | |
| 3843 | if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE) |
| 3844 | && pwr_mode_change) |
| 3845 | ufshcd_change_power_mode(hba, &orig_pwr_info); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 3846 | out: |
| 3847 | return ret; |
| 3848 | } |
| 3849 | EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr); |
| 3850 | |
| 3851 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3852 | * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power |
| 3853 | * state) and waits for it to take effect. |
| 3854 | * |
| 3855 | * @hba: per adapter instance |
| 3856 | * @cmd: UIC command to execute |
| 3857 | * |
| 3858 | * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER & |
| 3859 | * DME_HIBERNATE_EXIT commands take some time to take its effect on both host |
| 3860 | * and device UniPro link and hence it's final completion would be indicated by |
| 3861 | * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in |
| 3862 | * addition to normal UIC command completion Status (UCCS). This function only |
| 3863 | * returns after the relevant status bits indicate the completion. |
| 3864 | * |
| 3865 | * Returns 0 on success, non-zero value on failure |
| 3866 | */ |
| 3867 | static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd) |
| 3868 | { |
| 3869 | struct completion uic_async_done; |
| 3870 | unsigned long flags; |
| 3871 | u8 status; |
| 3872 | int ret; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3873 | bool reenable_intr = false; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3874 | |
| 3875 | mutex_lock(&hba->uic_cmd_mutex); |
| 3876 | init_completion(&uic_async_done); |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 3877 | ufshcd_add_delay_before_dme_cmd(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3878 | |
| 3879 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 3880 | hba->uic_async_done = &uic_async_done; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3881 | if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) { |
| 3882 | ufshcd_disable_intr(hba, UIC_COMMAND_COMPL); |
| 3883 | /* |
| 3884 | * Make sure UIC command completion interrupt is disabled before |
| 3885 | * issuing UIC command. |
| 3886 | */ |
| 3887 | wmb(); |
| 3888 | reenable_intr = true; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3889 | } |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3890 | ret = __ufshcd_send_uic_cmd(hba, cmd, false); |
| 3891 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3892 | if (ret) { |
| 3893 | dev_err(hba->dev, |
| 3894 | "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n", |
| 3895 | cmd->command, cmd->argument3, ret); |
| 3896 | goto out; |
| 3897 | } |
| 3898 | |
| 3899 | if (!wait_for_completion_timeout(hba->uic_async_done, |
| 3900 | msecs_to_jiffies(UIC_CMD_TIMEOUT))) { |
| 3901 | dev_err(hba->dev, |
| 3902 | "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n", |
| 3903 | cmd->command, cmd->argument3); |
| 3904 | ret = -ETIMEDOUT; |
| 3905 | goto out; |
| 3906 | } |
| 3907 | |
| 3908 | status = ufshcd_get_upmcrs(hba); |
| 3909 | if (status != PWR_LOCAL) { |
| 3910 | dev_err(hba->dev, |
Kiwoong Kim | 7361542 | 2016-09-08 16:50:02 +0900 | [diff] [blame] | 3911 | "pwr ctrl cmd 0x%0x failed, host upmcrs:0x%x\n", |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3912 | cmd->command, status); |
| 3913 | ret = (status != PWR_OK) ? status : -1; |
| 3914 | } |
| 3915 | out: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3916 | if (ret) |
| 3917 | ufsdbg_set_err_state(hba); |
| 3918 | |
| 3919 | ufshcd_save_tstamp_of_last_dme_cmd(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3920 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3921 | hba->active_uic_cmd = NULL; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3922 | hba->uic_async_done = NULL; |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 3923 | if (reenable_intr) |
| 3924 | ufshcd_enable_intr(hba, UIC_COMMAND_COMPL); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3925 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3926 | mutex_unlock(&hba->uic_cmd_mutex); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3927 | return ret; |
| 3928 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3929 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 3930 | int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba, u64 wait_timeout_us) |
| 3931 | { |
| 3932 | unsigned long flags; |
| 3933 | int ret = 0; |
| 3934 | u32 tm_doorbell; |
| 3935 | u32 tr_doorbell; |
| 3936 | bool timeout = false, do_last_check = false; |
| 3937 | ktime_t start; |
| 3938 | |
| 3939 | ufshcd_hold_all(hba); |
| 3940 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 3941 | if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) { |
| 3942 | ret = -EBUSY; |
| 3943 | goto out; |
| 3944 | } |
| 3945 | |
| 3946 | /* |
| 3947 | * Wait for all the outstanding tasks/transfer requests. |
| 3948 | * Verify by checking the doorbell registers are clear. |
| 3949 | */ |
| 3950 | start = ktime_get(); |
| 3951 | do { |
| 3952 | tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL); |
| 3953 | tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 3954 | if (!tm_doorbell && !tr_doorbell) { |
| 3955 | timeout = false; |
| 3956 | break; |
| 3957 | } else if (do_last_check) { |
| 3958 | break; |
| 3959 | } |
| 3960 | |
| 3961 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3962 | schedule(); |
| 3963 | if (ktime_to_us(ktime_sub(ktime_get(), start)) > |
| 3964 | wait_timeout_us) { |
| 3965 | timeout = true; |
| 3966 | /* |
| 3967 | * We might have scheduled out for long time so make |
| 3968 | * sure to check if doorbells are cleared by this time |
| 3969 | * or not. |
| 3970 | */ |
| 3971 | do_last_check = true; |
| 3972 | } |
| 3973 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 3974 | } while (tm_doorbell || tr_doorbell); |
| 3975 | |
| 3976 | if (timeout) { |
| 3977 | dev_err(hba->dev, |
| 3978 | "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n", |
| 3979 | __func__, tm_doorbell, tr_doorbell); |
| 3980 | ret = -EBUSY; |
| 3981 | } |
| 3982 | out: |
| 3983 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3984 | ufshcd_release_all(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 3985 | return ret; |
| 3986 | } |
| 3987 | |
| 3988 | /** |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3989 | * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage |
| 3990 | * using DME_SET primitives. |
| 3991 | * @hba: per adapter instance |
| 3992 | * @mode: powr mode value |
| 3993 | * |
| 3994 | * Returns 0 on success, non-zero value on failure |
| 3995 | */ |
Sujit Reddy Thumma | bdbe5d2 | 2014-05-26 10:59:11 +0530 | [diff] [blame] | 3996 | static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode) |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 3997 | { |
| 3998 | struct uic_command uic_cmd = {0}; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 3999 | int ret; |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4000 | |
Yaniv Gardi | c3a2f9e | 2015-05-17 18:55:01 +0300 | [diff] [blame] | 4001 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) { |
| 4002 | ret = ufshcd_dme_set(hba, |
| 4003 | UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1); |
| 4004 | if (ret) { |
| 4005 | dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n", |
| 4006 | __func__, ret); |
| 4007 | goto out; |
| 4008 | } |
| 4009 | } |
| 4010 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4011 | uic_cmd.command = UIC_CMD_DME_SET; |
| 4012 | uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE); |
| 4013 | uic_cmd.argument3 = mode; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4014 | ufshcd_hold_all(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4015 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4016 | ufshcd_release_all(hba); |
Yaniv Gardi | c3a2f9e | 2015-05-17 18:55:01 +0300 | [diff] [blame] | 4017 | out: |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 4018 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4019 | } |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4020 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 4021 | static int ufshcd_link_recovery(struct ufs_hba *hba) |
| 4022 | { |
| 4023 | int ret; |
| 4024 | unsigned long flags; |
| 4025 | |
| 4026 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4027 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 4028 | ufshcd_set_eh_in_progress(hba); |
| 4029 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4030 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4031 | ret = ufshcd_vops_full_reset(hba); |
| 4032 | if (ret) |
| 4033 | dev_warn(hba->dev, |
| 4034 | "full reset returned %d, trying to recover the link\n", |
| 4035 | ret); |
| 4036 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 4037 | ret = ufshcd_host_reset_and_restore(hba); |
| 4038 | |
| 4039 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4040 | if (ret) |
| 4041 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 4042 | ufshcd_clear_eh_in_progress(hba); |
| 4043 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4044 | |
| 4045 | if (ret) |
| 4046 | dev_err(hba->dev, "%s: link recovery failed, err %d", |
| 4047 | __func__, ret); |
| 4048 | |
| 4049 | return ret; |
| 4050 | } |
| 4051 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4052 | static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4053 | { |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4054 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4055 | struct uic_command uic_cmd = {0}; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4056 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4057 | |
| 4058 | uic_cmd.command = UIC_CMD_DME_HIBER_ENTER; |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4059 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4060 | trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter", |
| 4061 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4062 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 4063 | if (ret) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4064 | ufshcd_update_error_stats(hba, UFS_ERR_HIBERN8_ENTER); |
| 4065 | dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d", |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4066 | __func__, ret); |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 4067 | /* |
| 4068 | * If link recovery fails then return error so that caller |
| 4069 | * don't retry the hibern8 enter again. |
| 4070 | */ |
| 4071 | if (ufshcd_link_recovery(hba)) |
| 4072 | ret = -ENOLINK; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4073 | } else { |
| 4074 | dev_dbg(hba->dev, "%s: Hibern8 Enter at %lld us", __func__, |
| 4075 | ktime_to_us(ktime_get())); |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 4076 | } |
| 4077 | |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4078 | return ret; |
| 4079 | } |
| 4080 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4081 | int ufshcd_uic_hibern8_enter(struct ufs_hba *hba) |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4082 | { |
| 4083 | int ret = 0, retries; |
| 4084 | |
| 4085 | for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) { |
| 4086 | ret = __ufshcd_uic_hibern8_enter(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4087 | if (!ret) |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4088 | goto out; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4089 | /* Unable to recover the link, so no point proceeding */ |
| 4090 | if (ret == -ENOLINK) |
| 4091 | BUG(); |
Yaniv Gardi | 87d0b4a | 2016-02-01 15:02:44 +0200 | [diff] [blame] | 4092 | } |
| 4093 | out: |
| 4094 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4095 | } |
| 4096 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4097 | int ufshcd_uic_hibern8_exit(struct ufs_hba *hba) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4098 | { |
| 4099 | struct uic_command uic_cmd = {0}; |
| 4100 | int ret; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4101 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4102 | |
| 4103 | uic_cmd.command = UIC_CMD_DME_HIBER_EXIT; |
| 4104 | ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4105 | trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit", |
| 4106 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
| 4107 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4108 | if (ret) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4109 | ufshcd_update_error_stats(hba, UFS_ERR_HIBERN8_EXIT); |
| 4110 | dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d", |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 4111 | __func__, ret); |
| 4112 | ret = ufshcd_link_recovery(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4113 | /* Unable to recover the link, so no point proceeding */ |
| 4114 | if (ret) |
| 4115 | BUG(); |
| 4116 | } else { |
| 4117 | dev_dbg(hba->dev, "%s: Hibern8 Exit at %lld us", __func__, |
| 4118 | ktime_to_us(ktime_get())); |
| 4119 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_get(); |
| 4120 | hba->ufs_stats.hibern8_exit_cnt++; |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4121 | } |
| 4122 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4123 | return ret; |
| 4124 | } |
| 4125 | |
Yaniv Gardi | 5064636 | 2014-10-23 13:25:13 +0300 | [diff] [blame] | 4126 | /** |
| 4127 | * ufshcd_init_pwr_info - setting the POR (power on reset) |
| 4128 | * values in hba power info |
| 4129 | * @hba: per-adapter instance |
| 4130 | */ |
| 4131 | static void ufshcd_init_pwr_info(struct ufs_hba *hba) |
| 4132 | { |
| 4133 | hba->pwr_info.gear_rx = UFS_PWM_G1; |
| 4134 | hba->pwr_info.gear_tx = UFS_PWM_G1; |
| 4135 | hba->pwr_info.lane_rx = 1; |
| 4136 | hba->pwr_info.lane_tx = 1; |
| 4137 | hba->pwr_info.pwr_rx = SLOWAUTO_MODE; |
| 4138 | hba->pwr_info.pwr_tx = SLOWAUTO_MODE; |
| 4139 | hba->pwr_info.hs_rate = 0; |
| 4140 | } |
| 4141 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 4142 | /** |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4143 | * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device |
| 4144 | * @hba: per-adapter instance |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4145 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4146 | static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4147 | { |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4148 | struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info; |
| 4149 | |
| 4150 | if (hba->max_pwr_info.is_valid) |
| 4151 | return 0; |
| 4152 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4153 | pwr_info->pwr_tx = FAST_MODE; |
| 4154 | pwr_info->pwr_rx = FAST_MODE; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4155 | pwr_info->hs_rate = PA_HS_MODE_B; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4156 | |
| 4157 | /* Get the connected lane count */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4158 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES), |
| 4159 | &pwr_info->lane_rx); |
| 4160 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 4161 | &pwr_info->lane_tx); |
| 4162 | |
| 4163 | if (!pwr_info->lane_rx || !pwr_info->lane_tx) { |
| 4164 | dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n", |
| 4165 | __func__, |
| 4166 | pwr_info->lane_rx, |
| 4167 | pwr_info->lane_tx); |
| 4168 | return -EINVAL; |
| 4169 | } |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4170 | |
| 4171 | /* |
| 4172 | * First, get the maximum gears of HS speed. |
| 4173 | * If a zero value, it means there is no HSGEAR capability. |
| 4174 | * Then, get the maximum gears of PWM speed. |
| 4175 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4176 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx); |
| 4177 | if (!pwr_info->gear_rx) { |
| 4178 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), |
| 4179 | &pwr_info->gear_rx); |
| 4180 | if (!pwr_info->gear_rx) { |
| 4181 | dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n", |
| 4182 | __func__, pwr_info->gear_rx); |
| 4183 | return -EINVAL; |
Subhash Jadavani | 5e45e70 | 2016-08-09 18:43:10 -0700 | [diff] [blame] | 4184 | } else { |
| 4185 | if (hba->limit_rx_pwm_gear > 0 && |
| 4186 | (hba->limit_rx_pwm_gear < pwr_info->gear_rx)) |
| 4187 | pwr_info->gear_rx = hba->limit_rx_pwm_gear; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4188 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4189 | pwr_info->pwr_rx = SLOW_MODE; |
Subhash Jadavani | 5e45e70 | 2016-08-09 18:43:10 -0700 | [diff] [blame] | 4190 | } else { |
| 4191 | if (hba->limit_rx_hs_gear > 0 && |
| 4192 | (hba->limit_rx_hs_gear < pwr_info->gear_rx)) |
| 4193 | pwr_info->gear_rx = hba->limit_rx_hs_gear; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4194 | } |
| 4195 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4196 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), |
| 4197 | &pwr_info->gear_tx); |
| 4198 | if (!pwr_info->gear_tx) { |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4199 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4200 | &pwr_info->gear_tx); |
| 4201 | if (!pwr_info->gear_tx) { |
| 4202 | dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n", |
| 4203 | __func__, pwr_info->gear_tx); |
| 4204 | return -EINVAL; |
Subhash Jadavani | 5e45e70 | 2016-08-09 18:43:10 -0700 | [diff] [blame] | 4205 | } else { |
| 4206 | if (hba->limit_tx_pwm_gear > 0 && |
| 4207 | (hba->limit_tx_pwm_gear < pwr_info->gear_tx)) |
| 4208 | pwr_info->gear_tx = hba->limit_tx_pwm_gear; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4209 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4210 | pwr_info->pwr_tx = SLOW_MODE; |
Subhash Jadavani | 5e45e70 | 2016-08-09 18:43:10 -0700 | [diff] [blame] | 4211 | } else { |
| 4212 | if (hba->limit_tx_hs_gear > 0 && |
| 4213 | (hba->limit_tx_hs_gear < pwr_info->gear_tx)) |
| 4214 | pwr_info->gear_tx = hba->limit_tx_hs_gear; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4215 | } |
| 4216 | |
| 4217 | hba->max_pwr_info.is_valid = true; |
| 4218 | return 0; |
| 4219 | } |
| 4220 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4221 | int ufshcd_change_power_mode(struct ufs_hba *hba, |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4222 | struct ufs_pa_layer_attr *pwr_mode) |
| 4223 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4224 | int ret = 0; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4225 | |
| 4226 | /* if already configured to the requested pwr_mode */ |
| 4227 | if (pwr_mode->gear_rx == hba->pwr_info.gear_rx && |
| 4228 | pwr_mode->gear_tx == hba->pwr_info.gear_tx && |
| 4229 | pwr_mode->lane_rx == hba->pwr_info.lane_rx && |
| 4230 | pwr_mode->lane_tx == hba->pwr_info.lane_tx && |
| 4231 | pwr_mode->pwr_rx == hba->pwr_info.pwr_rx && |
| 4232 | pwr_mode->pwr_tx == hba->pwr_info.pwr_tx && |
| 4233 | pwr_mode->hs_rate == hba->pwr_info.hs_rate) { |
| 4234 | dev_dbg(hba->dev, "%s: power already configured\n", __func__); |
| 4235 | return 0; |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4236 | } |
| 4237 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4238 | ufsdbg_error_inject_dispatcher(hba, ERR_INJECT_PWR_CHANGE, 0, &ret); |
| 4239 | if (ret) |
| 4240 | return ret; |
| 4241 | |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4242 | /* |
| 4243 | * Configure attributes for power mode change with below. |
| 4244 | * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION, |
| 4245 | * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION, |
| 4246 | * - PA_HSSERIES |
| 4247 | */ |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4248 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx); |
| 4249 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES), |
| 4250 | pwr_mode->lane_rx); |
| 4251 | if (pwr_mode->pwr_rx == FASTAUTO_MODE || |
| 4252 | pwr_mode->pwr_rx == FAST_MODE) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4253 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4254 | else |
| 4255 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4256 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4257 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx); |
| 4258 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES), |
| 4259 | pwr_mode->lane_tx); |
| 4260 | if (pwr_mode->pwr_tx == FASTAUTO_MODE || |
| 4261 | pwr_mode->pwr_tx == FAST_MODE) |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4262 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4263 | else |
| 4264 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4265 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4266 | if (pwr_mode->pwr_rx == FASTAUTO_MODE || |
| 4267 | pwr_mode->pwr_tx == FASTAUTO_MODE || |
| 4268 | pwr_mode->pwr_rx == FAST_MODE || |
| 4269 | pwr_mode->pwr_tx == FAST_MODE) |
| 4270 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES), |
| 4271 | pwr_mode->hs_rate); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4272 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4273 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0), |
| 4274 | DL_FC0ProtectionTimeOutVal_Default); |
| 4275 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1), |
| 4276 | DL_TC0ReplayTimeOutVal_Default); |
| 4277 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2), |
| 4278 | DL_AFC0ReqTimeOutVal_Default); |
| 4279 | |
| 4280 | ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalFC0ProtectionTimeOutVal), |
| 4281 | DL_FC0ProtectionTimeOutVal_Default); |
| 4282 | ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalTC0ReplayTimeOutVal), |
| 4283 | DL_TC0ReplayTimeOutVal_Default); |
| 4284 | ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalAFC0ReqTimeOutVal), |
| 4285 | DL_AFC0ReqTimeOutVal_Default); |
| 4286 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4287 | ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4 |
| 4288 | | pwr_mode->pwr_tx); |
| 4289 | |
| 4290 | if (ret) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4291 | ufshcd_update_error_stats(hba, UFS_ERR_POWER_MODE_CHANGE); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4292 | dev_err(hba->dev, |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4293 | "%s: power mode change failed %d\n", __func__, ret); |
| 4294 | } else { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4295 | ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL, |
| 4296 | pwr_mode); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4297 | |
| 4298 | memcpy(&hba->pwr_info, pwr_mode, |
| 4299 | sizeof(struct ufs_pa_layer_attr)); |
| 4300 | } |
| 4301 | |
| 4302 | return ret; |
| 4303 | } |
| 4304 | |
| 4305 | /** |
| 4306 | * ufshcd_config_pwr_mode - configure a new power mode |
| 4307 | * @hba: per-adapter instance |
| 4308 | * @desired_pwr_mode: desired power configuration |
| 4309 | */ |
| 4310 | static int ufshcd_config_pwr_mode(struct ufs_hba *hba, |
| 4311 | struct ufs_pa_layer_attr *desired_pwr_mode) |
| 4312 | { |
| 4313 | struct ufs_pa_layer_attr final_params = { 0 }; |
| 4314 | int ret; |
| 4315 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4316 | ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE, |
| 4317 | desired_pwr_mode, &final_params); |
| 4318 | |
| 4319 | if (ret) |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 4320 | memcpy(&final_params, desired_pwr_mode, sizeof(final_params)); |
| 4321 | |
| 4322 | ret = ufshcd_change_power_mode(hba, &final_params); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4323 | if (!ret) |
| 4324 | ufshcd_print_pwr_info(hba); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 4325 | |
| 4326 | return ret; |
| 4327 | } |
| 4328 | |
| 4329 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4330 | * ufshcd_complete_dev_init() - checks device readiness |
| 4331 | * hba: per-adapter instance |
| 4332 | * |
| 4333 | * Set fDeviceInit flag and poll until device toggles it. |
| 4334 | */ |
| 4335 | static int ufshcd_complete_dev_init(struct ufs_hba *hba) |
| 4336 | { |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 4337 | int i; |
| 4338 | int err; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4339 | bool flag_res = 1; |
| 4340 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 4341 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG, |
| 4342 | QUERY_FLAG_IDN_FDEVICEINIT, NULL); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4343 | if (err) { |
| 4344 | dev_err(hba->dev, |
| 4345 | "%s setting fDeviceInit flag failed with error %d\n", |
| 4346 | __func__, err); |
| 4347 | goto out; |
| 4348 | } |
| 4349 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 4350 | /* poll for max. 1000 iterations for fDeviceInit flag to clear */ |
| 4351 | for (i = 0; i < 1000 && !err && flag_res; i++) |
| 4352 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG, |
| 4353 | QUERY_FLAG_IDN_FDEVICEINIT, &flag_res); |
| 4354 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 4355 | if (err) |
| 4356 | dev_err(hba->dev, |
| 4357 | "%s reading fDeviceInit flag failed with error %d\n", |
| 4358 | __func__, err); |
| 4359 | else if (flag_res) |
| 4360 | dev_err(hba->dev, |
| 4361 | "%s fDeviceInit was not cleared by the device\n", |
| 4362 | __func__); |
| 4363 | |
| 4364 | out: |
| 4365 | return err; |
| 4366 | } |
| 4367 | |
| 4368 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4369 | * ufshcd_make_hba_operational - Make UFS controller operational |
| 4370 | * @hba: per adapter instance |
| 4371 | * |
| 4372 | * To bring UFS host controller to operational state, |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4373 | * 1. Enable required interrupts |
| 4374 | * 2. Configure interrupt aggregation |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 4375 | * 3. Program UTRL and UTMRL base address |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4376 | * 4. Configure run-stop-registers |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4377 | * |
| 4378 | * Returns 0 on success, non-zero value on failure |
| 4379 | */ |
| 4380 | static int ufshcd_make_hba_operational(struct ufs_hba *hba) |
| 4381 | { |
| 4382 | int err = 0; |
| 4383 | u32 reg; |
| 4384 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4385 | /* Enable required interrupts */ |
| 4386 | ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS); |
| 4387 | |
| 4388 | /* Configure interrupt aggregation */ |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 4389 | if (ufshcd_is_intr_aggr_allowed(hba)) |
| 4390 | ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO); |
| 4391 | else |
| 4392 | ufshcd_disable_intr_aggr(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4393 | |
| 4394 | /* Configure UTRL and UTMRL base address registers */ |
| 4395 | ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr), |
| 4396 | REG_UTP_TRANSFER_REQ_LIST_BASE_L); |
| 4397 | ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr), |
| 4398 | REG_UTP_TRANSFER_REQ_LIST_BASE_H); |
| 4399 | ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr), |
| 4400 | REG_UTP_TASK_REQ_LIST_BASE_L); |
| 4401 | ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr), |
| 4402 | REG_UTP_TASK_REQ_LIST_BASE_H); |
| 4403 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4404 | /* |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 4405 | * Make sure base address and interrupt setup are updated before |
| 4406 | * enabling the run/stop registers below. |
| 4407 | */ |
| 4408 | wmb(); |
| 4409 | |
| 4410 | /* |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4411 | * UCRDY, UTMRLDY and UTRLRDY bits must be 1 |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4412 | */ |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4413 | reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4414 | if (!(ufshcd_get_lists_status(reg))) { |
| 4415 | ufshcd_enable_run_stop_reg(hba); |
| 4416 | } else { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 4417 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4418 | "Host controller not ready to process requests"); |
| 4419 | err = -EIO; |
| 4420 | goto out; |
| 4421 | } |
| 4422 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4423 | out: |
| 4424 | return err; |
| 4425 | } |
| 4426 | |
| 4427 | /** |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4428 | * ufshcd_hba_stop - Send controller to reset state |
| 4429 | * @hba: per adapter instance |
| 4430 | * @can_sleep: perform sleep or just spin |
| 4431 | */ |
| 4432 | static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep) |
| 4433 | { |
| 4434 | int err; |
| 4435 | |
| 4436 | ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE); |
| 4437 | err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE, |
| 4438 | CONTROLLER_ENABLE, CONTROLLER_DISABLE, |
| 4439 | 10, 1, can_sleep); |
| 4440 | if (err) |
| 4441 | dev_err(hba->dev, "%s: Controller disable failed\n", __func__); |
| 4442 | } |
| 4443 | |
| 4444 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4445 | * ufshcd_hba_enable - initialize the controller |
| 4446 | * @hba: per adapter instance |
| 4447 | * |
| 4448 | * The controller resets itself and controller firmware initialization |
| 4449 | * sequence kicks off. When controller is ready it will set |
| 4450 | * the Host Controller Enable bit to 1. |
| 4451 | * |
| 4452 | * Returns 0 on success, non-zero value on failure |
| 4453 | */ |
| 4454 | static int ufshcd_hba_enable(struct ufs_hba *hba) |
| 4455 | { |
| 4456 | int retry; |
| 4457 | |
| 4458 | /* |
| 4459 | * msleep of 1 and 5 used in this function might result in msleep(20), |
| 4460 | * but it was necessary to send the UFS FPGA to reset mode during |
| 4461 | * development and testing of this driver. msleep can be changed to |
| 4462 | * mdelay and retry count can be reduced based on the controller. |
| 4463 | */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4464 | if (!ufshcd_is_hba_active(hba)) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4465 | /* change controller state to "reset state" */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 4466 | ufshcd_hba_stop(hba, true); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4467 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4468 | /* UniPro link is disabled at this point */ |
| 4469 | ufshcd_set_link_off(hba); |
| 4470 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4471 | ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4472 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4473 | /* start controller initialization sequence */ |
| 4474 | ufshcd_hba_start(hba); |
| 4475 | |
| 4476 | /* |
| 4477 | * To initialize a UFS host controller HCE bit must be set to 1. |
| 4478 | * During initialization the HCE bit value changes from 1->0->1. |
| 4479 | * When the host controller completes initialization sequence |
| 4480 | * it sets the value of HCE bit to 1. The same HCE bit is read back |
| 4481 | * to check if the controller has completed initialization sequence. |
| 4482 | * So without this delay the value HCE = 1, set in the previous |
| 4483 | * instruction might be read back. |
| 4484 | * This delay can be changed based on the controller. |
| 4485 | */ |
| 4486 | msleep(1); |
| 4487 | |
| 4488 | /* wait for the host controller to complete initialization */ |
| 4489 | retry = 10; |
| 4490 | while (ufshcd_is_hba_active(hba)) { |
| 4491 | if (retry) { |
| 4492 | retry--; |
| 4493 | } else { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 4494 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4495 | "Controller enable failed\n"); |
| 4496 | return -EIO; |
| 4497 | } |
| 4498 | msleep(5); |
| 4499 | } |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4500 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4501 | /* enable UIC related interrupts */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4502 | ufshcd_enable_intr(hba, UFSHCD_UIC_MASK); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4503 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4504 | ufshcd_vops_hce_enable_notify(hba, POST_CHANGE); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4505 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4506 | return 0; |
| 4507 | } |
| 4508 | |
Yaniv Gardi | 7ca38cf | 2015-05-17 18:54:59 +0300 | [diff] [blame] | 4509 | static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer) |
| 4510 | { |
| 4511 | int tx_lanes, i, err = 0; |
| 4512 | |
| 4513 | if (!peer) |
| 4514 | ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 4515 | &tx_lanes); |
| 4516 | else |
| 4517 | ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), |
| 4518 | &tx_lanes); |
| 4519 | for (i = 0; i < tx_lanes; i++) { |
| 4520 | if (!peer) |
| 4521 | err = ufshcd_dme_set(hba, |
| 4522 | UIC_ARG_MIB_SEL(TX_LCC_ENABLE, |
| 4523 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)), |
| 4524 | 0); |
| 4525 | else |
| 4526 | err = ufshcd_dme_peer_set(hba, |
| 4527 | UIC_ARG_MIB_SEL(TX_LCC_ENABLE, |
| 4528 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)), |
| 4529 | 0); |
| 4530 | if (err) { |
| 4531 | dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d", |
| 4532 | __func__, peer, i, err); |
| 4533 | break; |
| 4534 | } |
| 4535 | } |
| 4536 | |
| 4537 | return err; |
| 4538 | } |
| 4539 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4540 | static inline int ufshcd_disable_host_tx_lcc(struct ufs_hba *hba) |
| 4541 | { |
| 4542 | return ufshcd_disable_tx_lcc(hba, false); |
| 4543 | } |
| 4544 | |
Yaniv Gardi | 7ca38cf | 2015-05-17 18:54:59 +0300 | [diff] [blame] | 4545 | static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba) |
| 4546 | { |
| 4547 | return ufshcd_disable_tx_lcc(hba, true); |
| 4548 | } |
| 4549 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4550 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4551 | * ufshcd_link_startup - Initialize unipro link startup |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4552 | * @hba: per adapter instance |
| 4553 | * |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4554 | * Returns 0 for success, non-zero in case of failure |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4555 | */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4556 | static int ufshcd_link_startup(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4557 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4558 | int ret; |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4559 | int retries = DME_LINKSTARTUP_RETRIES; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4560 | bool link_startup_again = false; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4561 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4562 | /* |
| 4563 | * If UFS device isn't active then we will have to issue link startup |
| 4564 | * 2 times to make sure the device state move to active. |
| 4565 | */ |
| 4566 | if (!ufshcd_is_ufs_dev_active(hba)) |
| 4567 | link_startup_again = true; |
| 4568 | |
| 4569 | link_startup: |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4570 | do { |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4571 | ufshcd_vops_link_startup_notify(hba, PRE_CHANGE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4572 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4573 | ret = ufshcd_dme_link_startup(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4574 | if (ret) |
| 4575 | ufshcd_update_error_stats(hba, UFS_ERR_LINKSTARTUP); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4576 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4577 | /* check if device is detected by inter-connect layer */ |
| 4578 | if (!ret && !ufshcd_is_device_present(hba)) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4579 | ufshcd_update_error_stats(hba, UFS_ERR_LINKSTARTUP); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4580 | dev_err(hba->dev, "%s: Device not present\n", __func__); |
| 4581 | ret = -ENXIO; |
| 4582 | goto out; |
| 4583 | } |
| 4584 | |
| 4585 | /* |
| 4586 | * DME link lost indication is only received when link is up, |
| 4587 | * but we can't be sure if the link is up until link startup |
| 4588 | * succeeds. So reset the local Uni-Pro and try again. |
| 4589 | */ |
| 4590 | if (ret && ufshcd_hba_enable(hba)) |
| 4591 | goto out; |
| 4592 | } while (ret && retries--); |
| 4593 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4594 | if (ret) |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 4595 | /* failed to get the link up... retire */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4596 | goto out; |
| 4597 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4598 | if (link_startup_again) { |
| 4599 | link_startup_again = false; |
| 4600 | retries = DME_LINKSTARTUP_RETRIES; |
| 4601 | goto link_startup; |
| 4602 | } |
| 4603 | |
| 4604 | /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */ |
| 4605 | ufshcd_init_pwr_info(hba); |
| 4606 | ufshcd_print_pwr_info(hba); |
| 4607 | |
Yaniv Gardi | 7ca38cf | 2015-05-17 18:54:59 +0300 | [diff] [blame] | 4608 | if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) { |
| 4609 | ret = ufshcd_disable_device_tx_lcc(hba); |
| 4610 | if (ret) |
| 4611 | goto out; |
| 4612 | } |
| 4613 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4614 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_BROKEN_LCC) { |
| 4615 | ret = ufshcd_disable_host_tx_lcc(hba); |
| 4616 | if (ret) |
| 4617 | goto out; |
| 4618 | } |
| 4619 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4620 | /* Include any host controller configuration via UIC commands */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 4621 | ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE); |
| 4622 | if (ret) |
| 4623 | goto out; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 4624 | |
| 4625 | ret = ufshcd_make_hba_operational(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 4626 | out: |
| 4627 | if (ret) |
| 4628 | dev_err(hba->dev, "link startup failed %d\n", ret); |
| 4629 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4630 | } |
| 4631 | |
| 4632 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4633 | * ufshcd_verify_dev_init() - Verify device initialization |
| 4634 | * @hba: per-adapter instance |
| 4635 | * |
| 4636 | * Send NOP OUT UPIU and wait for NOP IN response to check whether the |
| 4637 | * device Transport Protocol (UTP) layer is ready after a reset. |
| 4638 | * If the UTP layer at the device side is not initialized, it may |
| 4639 | * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT |
| 4640 | * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations. |
| 4641 | */ |
| 4642 | static int ufshcd_verify_dev_init(struct ufs_hba *hba) |
| 4643 | { |
| 4644 | int err = 0; |
| 4645 | int retries; |
| 4646 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4647 | ufshcd_hold_all(hba); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4648 | mutex_lock(&hba->dev_cmd.lock); |
| 4649 | for (retries = NOP_OUT_RETRIES; retries > 0; retries--) { |
| 4650 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP, |
| 4651 | NOP_OUT_TIMEOUT); |
| 4652 | |
| 4653 | if (!err || err == -ETIMEDOUT) |
| 4654 | break; |
| 4655 | |
| 4656 | dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err); |
| 4657 | } |
| 4658 | mutex_unlock(&hba->dev_cmd.lock); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4659 | ufshcd_release_all(hba); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4660 | |
| 4661 | if (err) |
| 4662 | dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err); |
| 4663 | return err; |
| 4664 | } |
| 4665 | |
| 4666 | /** |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4667 | * ufshcd_set_queue_depth - set lun queue depth |
| 4668 | * @sdev: pointer to SCSI device |
| 4669 | * |
| 4670 | * Read bLUQueueDepth value and activate scsi tagged command |
| 4671 | * queueing. For WLUN, queue depth is set to 1. For best-effort |
| 4672 | * cases (bLUQueueDepth = 0) the queue depth is set to a maximum |
| 4673 | * value that host can queue. |
| 4674 | */ |
| 4675 | static void ufshcd_set_queue_depth(struct scsi_device *sdev) |
| 4676 | { |
| 4677 | int ret = 0; |
| 4678 | u8 lun_qdepth; |
| 4679 | struct ufs_hba *hba; |
| 4680 | |
| 4681 | hba = shost_priv(sdev->host); |
| 4682 | |
| 4683 | lun_qdepth = hba->nutrs; |
| 4684 | ret = ufshcd_read_unit_desc_param(hba, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4685 | ufshcd_scsi_to_upiu_lun(sdev->lun), |
| 4686 | UNIT_DESC_PARAM_LU_Q_DEPTH, |
| 4687 | &lun_qdepth, |
| 4688 | sizeof(lun_qdepth)); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4689 | |
| 4690 | /* Some WLUN doesn't support unit descriptor */ |
| 4691 | if (ret == -EOPNOTSUPP) |
| 4692 | lun_qdepth = 1; |
| 4693 | else if (!lun_qdepth) |
| 4694 | /* eventually, we can figure out the real queue depth */ |
| 4695 | lun_qdepth = hba->nutrs; |
| 4696 | else |
| 4697 | lun_qdepth = min_t(int, lun_qdepth, hba->nutrs); |
| 4698 | |
| 4699 | dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n", |
| 4700 | __func__, lun_qdepth); |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4701 | scsi_change_queue_depth(sdev, lun_qdepth); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4702 | } |
| 4703 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4704 | /* |
| 4705 | * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR |
| 4706 | * @hba: per-adapter instance |
| 4707 | * @lun: UFS device lun id |
| 4708 | * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info |
| 4709 | * |
| 4710 | * Returns 0 in case of success and b_lu_write_protect status would be returned |
| 4711 | * @b_lu_write_protect parameter. |
| 4712 | * Returns -ENOTSUPP if reading b_lu_write_protect is not supported. |
| 4713 | * Returns -EINVAL in case of invalid parameters passed to this function. |
| 4714 | */ |
| 4715 | static int ufshcd_get_lu_wp(struct ufs_hba *hba, |
| 4716 | u8 lun, |
| 4717 | u8 *b_lu_write_protect) |
| 4718 | { |
| 4719 | int ret; |
| 4720 | |
| 4721 | if (!b_lu_write_protect) |
| 4722 | ret = -EINVAL; |
| 4723 | /* |
| 4724 | * According to UFS device spec, RPMB LU can't be write |
| 4725 | * protected so skip reading bLUWriteProtect parameter for |
| 4726 | * it. For other W-LUs, UNIT DESCRIPTOR is not available. |
| 4727 | */ |
| 4728 | else if (lun >= UFS_UPIU_MAX_GENERAL_LUN) |
| 4729 | ret = -ENOTSUPP; |
| 4730 | else |
| 4731 | ret = ufshcd_read_unit_desc_param(hba, |
| 4732 | lun, |
| 4733 | UNIT_DESC_PARAM_LU_WR_PROTECT, |
| 4734 | b_lu_write_protect, |
| 4735 | sizeof(*b_lu_write_protect)); |
| 4736 | return ret; |
| 4737 | } |
| 4738 | |
| 4739 | /** |
| 4740 | * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect |
| 4741 | * status |
| 4742 | * @hba: per-adapter instance |
| 4743 | * @sdev: pointer to SCSI device |
| 4744 | * |
| 4745 | */ |
| 4746 | static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba, |
| 4747 | struct scsi_device *sdev) |
| 4748 | { |
| 4749 | if (hba->dev_info.f_power_on_wp_en && |
| 4750 | !hba->dev_info.is_lu_power_on_wp) { |
| 4751 | u8 b_lu_write_protect; |
| 4752 | |
| 4753 | if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun), |
| 4754 | &b_lu_write_protect) && |
| 4755 | (b_lu_write_protect == UFS_LU_POWER_ON_WP)) |
| 4756 | hba->dev_info.is_lu_power_on_wp = true; |
| 4757 | } |
| 4758 | } |
| 4759 | |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4760 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4761 | * ufshcd_slave_alloc - handle initial SCSI device configurations |
| 4762 | * @sdev: pointer to SCSI device |
| 4763 | * |
| 4764 | * Returns success |
| 4765 | */ |
| 4766 | static int ufshcd_slave_alloc(struct scsi_device *sdev) |
| 4767 | { |
| 4768 | struct ufs_hba *hba; |
| 4769 | |
| 4770 | hba = shost_priv(sdev->host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4771 | |
| 4772 | /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */ |
| 4773 | sdev->use_10_for_ms = 1; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4774 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4775 | /* allow SCSI layer to restart the device in case of errors */ |
| 4776 | sdev->allow_restart = 1; |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4777 | |
Sujit Reddy Thumma | b2a6c52 | 2014-07-01 12:22:38 +0300 | [diff] [blame] | 4778 | /* REPORT SUPPORTED OPERATION CODES is not supported */ |
| 4779 | sdev->no_report_opcodes = 1; |
| 4780 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4781 | /* WRITE_SAME command is not supported*/ |
| 4782 | sdev->no_write_same = 1; |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4783 | |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4784 | ufshcd_set_queue_depth(sdev); |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4785 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 4786 | ufshcd_get_lu_power_on_wp_status(hba, sdev); |
| 4787 | |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4788 | return 0; |
| 4789 | } |
| 4790 | |
| 4791 | /** |
| 4792 | * ufshcd_change_queue_depth - change queue depth |
| 4793 | * @sdev: pointer to SCSI device |
| 4794 | * @depth: required depth to set |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4795 | * |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4796 | * Change queue depth and make sure the max. limits are not crossed. |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4797 | */ |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4798 | static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth) |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 4799 | { |
| 4800 | struct ufs_hba *hba = shost_priv(sdev->host); |
| 4801 | |
| 4802 | if (depth > hba->nutrs) |
| 4803 | depth = hba->nutrs; |
Christoph Hellwig | db5ed4d | 2014-11-13 15:08:42 +0100 | [diff] [blame] | 4804 | return scsi_change_queue_depth(sdev, depth); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4805 | } |
| 4806 | |
| 4807 | /** |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 4808 | * ufshcd_slave_configure - adjust SCSI device configurations |
| 4809 | * @sdev: pointer to SCSI device |
| 4810 | */ |
| 4811 | static int ufshcd_slave_configure(struct scsi_device *sdev) |
| 4812 | { |
| 4813 | struct request_queue *q = sdev->request_queue; |
Subhash Jadavani | 5ea586f | 2016-08-17 19:08:09 -0700 | [diff] [blame] | 4814 | struct ufs_hba *hba = shost_priv(sdev->host); |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 4815 | |
| 4816 | blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1); |
| 4817 | blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX); |
| 4818 | |
Subhash Jadavani | 5ea586f | 2016-08-17 19:08:09 -0700 | [diff] [blame] | 4819 | if (hba->scsi_cmd_timeout) { |
| 4820 | blk_queue_rq_timeout(q, hba->scsi_cmd_timeout * HZ); |
| 4821 | scsi_set_cmd_timeout_override(sdev, hba->scsi_cmd_timeout * HZ); |
| 4822 | } |
| 4823 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4824 | sdev->autosuspend_delay = UFSHCD_AUTO_SUSPEND_DELAY_MS; |
| 4825 | sdev->use_rpm_auto = 1; |
| 4826 | |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 4827 | return 0; |
| 4828 | } |
| 4829 | |
| 4830 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4831 | * ufshcd_slave_destroy - remove SCSI device configurations |
| 4832 | * @sdev: pointer to SCSI device |
| 4833 | */ |
| 4834 | static void ufshcd_slave_destroy(struct scsi_device *sdev) |
| 4835 | { |
| 4836 | struct ufs_hba *hba; |
| 4837 | |
| 4838 | hba = shost_priv(sdev->host); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4839 | /* Drop the reference as it won't be needed anymore */ |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 4840 | if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) { |
| 4841 | unsigned long flags; |
| 4842 | |
| 4843 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 4844 | hba->sdev_ufs_device = NULL; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 4845 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 4846 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4847 | } |
| 4848 | |
| 4849 | /** |
| 4850 | * ufshcd_task_req_compl - handle task management request completion |
| 4851 | * @hba: per adapter instance |
| 4852 | * @index: index of the completed request |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4853 | * @resp: task management service response |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4854 | * |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4855 | * Returns non-zero value on error, zero on success |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4856 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4857 | static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4858 | { |
| 4859 | struct utp_task_req_desc *task_req_descp; |
| 4860 | struct utp_upiu_task_rsp *task_rsp_upiup; |
| 4861 | unsigned long flags; |
| 4862 | int ocs_value; |
| 4863 | int task_result; |
| 4864 | |
| 4865 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 4866 | |
| 4867 | /* Clear completed tasks from outstanding_tasks */ |
| 4868 | __clear_bit(index, &hba->outstanding_tasks); |
| 4869 | |
| 4870 | task_req_descp = hba->utmrdl_base_addr; |
| 4871 | ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]); |
| 4872 | |
| 4873 | if (ocs_value == OCS_SUCCESS) { |
| 4874 | task_rsp_upiup = (struct utp_upiu_task_rsp *) |
| 4875 | task_req_descp[index].task_rsp_upiu; |
Kiwoong Kim | 8794ee0 | 2016-09-09 08:22:22 +0900 | [diff] [blame] | 4876 | task_result = be32_to_cpu(task_rsp_upiup->output_param1); |
| 4877 | task_result = task_result & MASK_TM_SERVICE_RESP; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4878 | if (resp) |
| 4879 | *resp = (u8)task_result; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4880 | } else { |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4881 | dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", |
| 4882 | __func__, ocs_value); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4883 | } |
| 4884 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 4885 | |
| 4886 | return ocs_value; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4887 | } |
| 4888 | |
| 4889 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4890 | * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status |
| 4891 | * @lrb: pointer to local reference block of completed command |
| 4892 | * @scsi_status: SCSI command status |
| 4893 | * |
| 4894 | * Returns value base on SCSI command status |
| 4895 | */ |
| 4896 | static inline int |
| 4897 | ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status) |
| 4898 | { |
| 4899 | int result = 0; |
| 4900 | |
| 4901 | switch (scsi_status) { |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 4902 | case SAM_STAT_CHECK_CONDITION: |
| 4903 | ufshcd_copy_sense_data(lrbp); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4904 | case SAM_STAT_GOOD: |
| 4905 | result |= DID_OK << 16 | |
| 4906 | COMMAND_COMPLETE << 8 | |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 4907 | scsi_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4908 | break; |
| 4909 | case SAM_STAT_TASK_SET_FULL: |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 4910 | case SAM_STAT_BUSY: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4911 | case SAM_STAT_TASK_ABORTED: |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 4912 | ufshcd_copy_sense_data(lrbp); |
| 4913 | result |= scsi_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4914 | break; |
| 4915 | default: |
| 4916 | result |= DID_ERROR << 16; |
| 4917 | break; |
| 4918 | } /* end of switch */ |
| 4919 | |
| 4920 | return result; |
| 4921 | } |
| 4922 | |
| 4923 | /** |
| 4924 | * ufshcd_transfer_rsp_status - Get overall status of the response |
| 4925 | * @hba: per adapter instance |
| 4926 | * @lrb: pointer to local reference block of completed command |
| 4927 | * |
| 4928 | * Returns result of the command to notify SCSI midlayer |
| 4929 | */ |
| 4930 | static inline int |
| 4931 | ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 4932 | { |
| 4933 | int result = 0; |
| 4934 | int scsi_status; |
| 4935 | int ocs; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4936 | bool print_prdt; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4937 | |
| 4938 | /* overall command status of utrd */ |
| 4939 | ocs = ufshcd_get_tr_ocs(lrbp); |
| 4940 | |
| 4941 | switch (ocs) { |
| 4942 | case OCS_SUCCESS: |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4943 | result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 4944 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4945 | switch (result) { |
| 4946 | case UPIU_TRANSACTION_RESPONSE: |
| 4947 | /* |
| 4948 | * get the response UPIU result to extract |
| 4949 | * the SCSI command status |
| 4950 | */ |
| 4951 | result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr); |
| 4952 | |
| 4953 | /* |
| 4954 | * get the result based on SCSI status response |
| 4955 | * to notify the SCSI midlayer of the command status |
| 4956 | */ |
| 4957 | scsi_status = result & MASK_SCSI_STATUS; |
| 4958 | result = ufshcd_scsi_cmd_status(lrbp, scsi_status); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4959 | |
Yaniv Gardi | f05ac2e | 2016-02-01 15:02:42 +0200 | [diff] [blame] | 4960 | /* |
| 4961 | * Currently we are only supporting BKOPs exception |
| 4962 | * events hence we can ignore BKOPs exception event |
| 4963 | * during power management callbacks. BKOPs exception |
| 4964 | * event is not expected to be raised in runtime suspend |
| 4965 | * callback as it allows the urgent bkops. |
| 4966 | * During system suspend, we are anyway forcefully |
| 4967 | * disabling the bkops and if urgent bkops is needed |
| 4968 | * it will be enabled on system resume. Long term |
| 4969 | * solution could be to abort the system suspend if |
| 4970 | * UFS device needs urgent BKOPs. |
| 4971 | */ |
| 4972 | if (!hba->pm_op_in_progress && |
| 4973 | ufshcd_is_exception_event(lrbp->ucd_rsp_ptr)) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 4974 | schedule_work(&hba->eeh_work); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4975 | break; |
| 4976 | case UPIU_TRANSACTION_REJECT_UPIU: |
| 4977 | /* TODO: handle Reject UPIU Response */ |
| 4978 | result = DID_ERROR << 16; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 4979 | dev_err(hba->dev, |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 4980 | "Reject UPIU not fully implemented\n"); |
| 4981 | break; |
| 4982 | default: |
| 4983 | result = DID_ERROR << 16; |
| 4984 | dev_err(hba->dev, |
| 4985 | "Unexpected request response code = %x\n", |
| 4986 | result); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4987 | break; |
| 4988 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4989 | break; |
| 4990 | case OCS_ABORTED: |
| 4991 | result |= DID_ABORT << 16; |
| 4992 | break; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 4993 | case OCS_INVALID_COMMAND_STATUS: |
| 4994 | result |= DID_REQUEUE << 16; |
| 4995 | break; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 4996 | case OCS_INVALID_CMD_TABLE_ATTR: |
| 4997 | case OCS_INVALID_PRDT_ATTR: |
| 4998 | case OCS_MISMATCH_DATA_BUF_SIZE: |
| 4999 | case OCS_MISMATCH_RESP_UPIU_SIZE: |
| 5000 | case OCS_PEER_COMM_FAILURE: |
| 5001 | case OCS_FATAL_ERROR: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5002 | case OCS_DEVICE_FATAL_ERROR: |
| 5003 | case OCS_INVALID_CRYPTO_CONFIG: |
| 5004 | case OCS_GENERAL_CRYPTO_ERROR: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5005 | default: |
| 5006 | result |= DID_ERROR << 16; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 5007 | dev_err(hba->dev, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5008 | "OCS error from controller = %x for tag %d\n", |
| 5009 | ocs, lrbp->task_tag); |
| 5010 | ufshcd_print_host_regs(hba); |
| 5011 | ufshcd_print_host_state(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5012 | break; |
| 5013 | } /* end of switch */ |
| 5014 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5015 | if ((host_byte(result) != DID_OK) && !hba->silence_err_logs) { |
| 5016 | print_prdt = (ocs == OCS_INVALID_PRDT_ATTR || |
| 5017 | ocs == OCS_MISMATCH_DATA_BUF_SIZE); |
| 5018 | ufshcd_print_trs(hba, 1 << lrbp->task_tag, print_prdt); |
| 5019 | } |
| 5020 | |
| 5021 | if ((host_byte(result) == DID_ERROR) || |
| 5022 | (host_byte(result) == DID_ABORT)) |
| 5023 | ufsdbg_set_err_state(hba); |
| 5024 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5025 | return result; |
| 5026 | } |
| 5027 | |
| 5028 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5029 | * ufshcd_uic_cmd_compl - handle completion of uic command |
| 5030 | * @hba: per adapter instance |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 5031 | * @intr_status: interrupt status generated by the controller |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5032 | */ |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 5033 | static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5034 | { |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 5035 | if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5036 | hba->active_uic_cmd->argument2 |= |
| 5037 | ufshcd_get_uic_cmd_result(hba); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 5038 | hba->active_uic_cmd->argument3 = |
| 5039 | ufshcd_get_dme_attr_val(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5040 | complete(&hba->active_uic_cmd->done); |
| 5041 | } |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 5042 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5043 | if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done) |
| 5044 | complete(hba->uic_async_done); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 5045 | } |
| 5046 | |
| 5047 | /** |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5048 | * ufshcd_abort_outstanding_requests - abort all outstanding transfer requests. |
| 5049 | * @hba: per adapter instance |
| 5050 | * @result: error result to inform scsi layer about |
| 5051 | */ |
| 5052 | void ufshcd_abort_outstanding_transfer_requests(struct ufs_hba *hba, int result) |
| 5053 | { |
| 5054 | u8 index; |
| 5055 | struct ufshcd_lrb *lrbp; |
| 5056 | struct scsi_cmnd *cmd; |
| 5057 | |
| 5058 | if (!hba->outstanding_reqs) |
| 5059 | return; |
| 5060 | |
| 5061 | for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) { |
| 5062 | lrbp = &hba->lrb[index]; |
| 5063 | cmd = lrbp->cmd; |
| 5064 | if (cmd) { |
| 5065 | ufshcd_cond_add_cmd_trace(hba, index, "failed"); |
| 5066 | ufshcd_update_error_stats(hba, |
| 5067 | UFS_ERR_INT_FATAL_ERRORS); |
| 5068 | scsi_dma_unmap(cmd); |
| 5069 | cmd->result = result; |
| 5070 | /* Clear pending transfer requests */ |
| 5071 | ufshcd_clear_cmd(hba, index); |
| 5072 | ufshcd_outstanding_req_clear(hba, index); |
| 5073 | clear_bit_unlock(index, &hba->lrb_in_use); |
| 5074 | lrbp->complete_time_stamp = ktime_get(); |
| 5075 | update_req_stats(hba, lrbp); |
| 5076 | /* Mark completed command as NULL in LRB */ |
| 5077 | lrbp->cmd = NULL; |
| 5078 | ufshcd_release_all(hba); |
| 5079 | if (cmd->request) { |
| 5080 | /* |
| 5081 | * As we are accessing the "request" structure, |
| 5082 | * this must be called before calling |
| 5083 | * ->scsi_done() callback. |
| 5084 | */ |
| 5085 | ufshcd_vops_pm_qos_req_end(hba, cmd->request, |
| 5086 | true); |
| 5087 | ufshcd_vops_crypto_engine_cfg_end(hba, |
| 5088 | lrbp, cmd->request); |
| 5089 | } |
| 5090 | /* Do not touch lrbp after scsi done */ |
| 5091 | cmd->scsi_done(cmd); |
| 5092 | } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE) { |
| 5093 | if (hba->dev_cmd.complete) { |
| 5094 | ufshcd_cond_add_cmd_trace(hba, index, |
| 5095 | "dev_failed"); |
| 5096 | ufshcd_outstanding_req_clear(hba, index); |
| 5097 | complete(hba->dev_cmd.complete); |
| 5098 | } |
| 5099 | } |
| 5100 | if (ufshcd_is_clkscaling_supported(hba)) |
| 5101 | hba->clk_scaling.active_reqs--; |
| 5102 | } |
| 5103 | } |
| 5104 | |
| 5105 | /** |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5106 | * __ufshcd_transfer_req_compl - handle SCSI and query command completion |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5107 | * @hba: per adapter instance |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5108 | * @completed_reqs: requests to complete |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5109 | */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5110 | static void __ufshcd_transfer_req_compl(struct ufs_hba *hba, |
| 5111 | unsigned long completed_reqs) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5112 | { |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 5113 | struct ufshcd_lrb *lrbp; |
| 5114 | struct scsi_cmnd *cmd; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5115 | int result; |
| 5116 | int index; |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 5117 | struct request *req; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5118 | |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5119 | for_each_set_bit(index, &completed_reqs, hba->nutrs) { |
| 5120 | lrbp = &hba->lrb[index]; |
| 5121 | cmd = lrbp->cmd; |
| 5122 | if (cmd) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5123 | ufshcd_cond_add_cmd_trace(hba, index, "complete"); |
| 5124 | ufshcd_update_tag_stats_completion(hba, cmd); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5125 | result = ufshcd_transfer_rsp_status(hba, lrbp); |
| 5126 | scsi_dma_unmap(cmd); |
| 5127 | cmd->result = result; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5128 | clear_bit_unlock(index, &hba->lrb_in_use); |
| 5129 | lrbp->complete_time_stamp = ktime_get(); |
| 5130 | update_req_stats(hba, lrbp); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5131 | /* Mark completed command as NULL in LRB */ |
| 5132 | lrbp->cmd = NULL; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5133 | __ufshcd_release(hba, false); |
| 5134 | __ufshcd_hibern8_release(hba, false); |
| 5135 | if (cmd->request) { |
| 5136 | /* |
| 5137 | * As we are accessing the "request" structure, |
| 5138 | * this must be called before calling |
| 5139 | * ->scsi_done() callback. |
| 5140 | */ |
| 5141 | ufshcd_vops_pm_qos_req_end(hba, cmd->request, |
| 5142 | false); |
| 5143 | ufshcd_vops_crypto_engine_cfg_end(hba, |
| 5144 | lrbp, cmd->request); |
| 5145 | } |
| 5146 | |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 5147 | req = cmd->request; |
| 5148 | if (req) { |
| 5149 | /* Update IO svc time latency histogram */ |
| 5150 | if (req->lat_hist_enabled) { |
| 5151 | ktime_t completion; |
| 5152 | u_int64_t delta_us; |
| 5153 | |
| 5154 | completion = ktime_get(); |
| 5155 | delta_us = ktime_us_delta(completion, |
| 5156 | req->lat_hist_io_start); |
| 5157 | /* rq_data_dir() => true if WRITE */ |
| 5158 | blk_update_latency_hist(&hba->io_lat_s, |
| 5159 | (rq_data_dir(req) == READ), |
| 5160 | delta_us); |
| 5161 | } |
| 5162 | } |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5163 | /* Do not touch lrbp after scsi done */ |
| 5164 | cmd->scsi_done(cmd); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5165 | } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE) { |
| 5166 | if (hba->dev_cmd.complete) { |
| 5167 | ufshcd_cond_add_cmd_trace(hba, index, |
| 5168 | "dev_complete"); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5169 | complete(hba->dev_cmd.complete); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5170 | } |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5171 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5172 | if (ufshcd_is_clkscaling_supported(hba)) |
| 5173 | hba->clk_scaling.active_reqs--; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 5174 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5175 | |
| 5176 | /* clear corresponding bits of completed commands */ |
| 5177 | hba->outstanding_reqs ^= completed_reqs; |
| 5178 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 5179 | ufshcd_clk_scaling_update_busy(hba); |
| 5180 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 5181 | /* we might have free'd some tags above */ |
| 5182 | wake_up(&hba->dev_cmd.tag_wq); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5183 | } |
| 5184 | |
| 5185 | /** |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5186 | * ufshcd_transfer_req_compl - handle SCSI and query command completion |
| 5187 | * @hba: per adapter instance |
| 5188 | */ |
| 5189 | static void ufshcd_transfer_req_compl(struct ufs_hba *hba) |
| 5190 | { |
| 5191 | unsigned long completed_reqs; |
| 5192 | u32 tr_doorbell; |
| 5193 | |
| 5194 | /* Resetting interrupt aggregation counters first and reading the |
| 5195 | * DOOR_BELL afterward allows us to handle all the completed requests. |
| 5196 | * In order to prevent other interrupts starvation the DB is read once |
| 5197 | * after reset. The down side of this solution is the possibility of |
| 5198 | * false interrupt if device completes another request after resetting |
| 5199 | * aggregation and before reading the DB. |
| 5200 | */ |
| 5201 | if (ufshcd_is_intr_aggr_allowed(hba)) |
| 5202 | ufshcd_reset_intr_aggr(hba); |
| 5203 | |
| 5204 | tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 5205 | completed_reqs = tr_doorbell ^ hba->outstanding_reqs; |
| 5206 | |
| 5207 | __ufshcd_transfer_req_compl(hba, completed_reqs); |
| 5208 | } |
| 5209 | |
| 5210 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5211 | * ufshcd_disable_ee - disable exception event |
| 5212 | * @hba: per-adapter instance |
| 5213 | * @mask: exception event to disable |
| 5214 | * |
| 5215 | * Disables exception event in the device so that the EVENT_ALERT |
| 5216 | * bit is not set. |
| 5217 | * |
| 5218 | * Returns zero on success, non-zero error value on failure. |
| 5219 | */ |
| 5220 | static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask) |
| 5221 | { |
| 5222 | int err = 0; |
| 5223 | u32 val; |
| 5224 | |
| 5225 | if (!(hba->ee_ctrl_mask & mask)) |
| 5226 | goto out; |
| 5227 | |
| 5228 | val = hba->ee_ctrl_mask & ~mask; |
| 5229 | val &= 0xFFFF; /* 2 bytes */ |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 5230 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5231 | QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val); |
| 5232 | if (!err) |
| 5233 | hba->ee_ctrl_mask &= ~mask; |
| 5234 | out: |
| 5235 | return err; |
| 5236 | } |
| 5237 | |
| 5238 | /** |
| 5239 | * ufshcd_enable_ee - enable exception event |
| 5240 | * @hba: per-adapter instance |
| 5241 | * @mask: exception event to enable |
| 5242 | * |
| 5243 | * Enable corresponding exception event in the device to allow |
| 5244 | * device to alert host in critical scenarios. |
| 5245 | * |
| 5246 | * Returns zero on success, non-zero error value on failure. |
| 5247 | */ |
| 5248 | static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask) |
| 5249 | { |
| 5250 | int err = 0; |
| 5251 | u32 val; |
| 5252 | |
| 5253 | if (hba->ee_ctrl_mask & mask) |
| 5254 | goto out; |
| 5255 | |
| 5256 | val = hba->ee_ctrl_mask | mask; |
| 5257 | val &= 0xFFFF; /* 2 bytes */ |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 5258 | err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5259 | QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val); |
| 5260 | if (!err) |
| 5261 | hba->ee_ctrl_mask |= mask; |
| 5262 | out: |
| 5263 | return err; |
| 5264 | } |
| 5265 | |
| 5266 | /** |
| 5267 | * ufshcd_enable_auto_bkops - Allow device managed BKOPS |
| 5268 | * @hba: per-adapter instance |
| 5269 | * |
| 5270 | * Allow device to manage background operations on its own. Enabling |
| 5271 | * this might lead to inconsistent latencies during normal data transfers |
| 5272 | * as the device is allowed to manage its own way of handling background |
| 5273 | * operations. |
| 5274 | * |
| 5275 | * Returns zero on success, non-zero on failure. |
| 5276 | */ |
| 5277 | static int ufshcd_enable_auto_bkops(struct ufs_hba *hba) |
| 5278 | { |
| 5279 | int err = 0; |
| 5280 | |
| 5281 | if (hba->auto_bkops_enabled) |
| 5282 | goto out; |
| 5283 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 5284 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5285 | QUERY_FLAG_IDN_BKOPS_EN, NULL); |
| 5286 | if (err) { |
| 5287 | dev_err(hba->dev, "%s: failed to enable bkops %d\n", |
| 5288 | __func__, err); |
| 5289 | goto out; |
| 5290 | } |
| 5291 | |
| 5292 | hba->auto_bkops_enabled = true; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5293 | trace_ufshcd_auto_bkops_state(dev_name(hba->dev), 1); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5294 | |
| 5295 | /* No need of URGENT_BKOPS exception from the device */ |
| 5296 | err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 5297 | if (err) |
| 5298 | dev_err(hba->dev, "%s: failed to disable exception event %d\n", |
| 5299 | __func__, err); |
| 5300 | out: |
| 5301 | return err; |
| 5302 | } |
| 5303 | |
| 5304 | /** |
| 5305 | * ufshcd_disable_auto_bkops - block device in doing background operations |
| 5306 | * @hba: per-adapter instance |
| 5307 | * |
| 5308 | * Disabling background operations improves command response latency but |
| 5309 | * has drawback of device moving into critical state where the device is |
| 5310 | * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the |
| 5311 | * host is idle so that BKOPS are managed effectively without any negative |
| 5312 | * impacts. |
| 5313 | * |
| 5314 | * Returns zero on success, non-zero on failure. |
| 5315 | */ |
| 5316 | static int ufshcd_disable_auto_bkops(struct ufs_hba *hba) |
| 5317 | { |
| 5318 | int err = 0; |
| 5319 | |
| 5320 | if (!hba->auto_bkops_enabled) |
| 5321 | goto out; |
| 5322 | |
| 5323 | /* |
| 5324 | * If host assisted BKOPs is to be enabled, make sure |
| 5325 | * urgent bkops exception is allowed. |
| 5326 | */ |
| 5327 | err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 5328 | if (err) { |
| 5329 | dev_err(hba->dev, "%s: failed to enable exception event %d\n", |
| 5330 | __func__, err); |
| 5331 | goto out; |
| 5332 | } |
| 5333 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 5334 | err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5335 | QUERY_FLAG_IDN_BKOPS_EN, NULL); |
| 5336 | if (err) { |
| 5337 | dev_err(hba->dev, "%s: failed to disable bkops %d\n", |
| 5338 | __func__, err); |
| 5339 | ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 5340 | goto out; |
| 5341 | } |
| 5342 | |
| 5343 | hba->auto_bkops_enabled = false; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5344 | trace_ufshcd_auto_bkops_state(dev_name(hba->dev), 0); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5345 | out: |
| 5346 | return err; |
| 5347 | } |
| 5348 | |
| 5349 | /** |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5350 | * ufshcd_force_reset_auto_bkops - force reset auto bkops state |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5351 | * @hba: per adapter instance |
| 5352 | * |
| 5353 | * After a device reset the device may toggle the BKOPS_EN flag |
| 5354 | * to default value. The s/w tracking variables should be updated |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5355 | * as well. This function would change the auto-bkops state based on |
| 5356 | * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5357 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5358 | static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5359 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5360 | if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) { |
| 5361 | hba->auto_bkops_enabled = false; |
| 5362 | hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS; |
| 5363 | ufshcd_enable_auto_bkops(hba); |
| 5364 | } else { |
| 5365 | hba->auto_bkops_enabled = true; |
| 5366 | hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS; |
| 5367 | ufshcd_disable_auto_bkops(hba); |
| 5368 | } |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5369 | } |
| 5370 | |
| 5371 | static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status) |
| 5372 | { |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 5373 | return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5374 | QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status); |
| 5375 | } |
| 5376 | |
| 5377 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5378 | * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status |
| 5379 | * @hba: per-adapter instance |
| 5380 | * @status: bkops_status value |
| 5381 | * |
| 5382 | * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn |
| 5383 | * flag in the device to permit background operations if the device |
| 5384 | * bkops_status is greater than or equal to "status" argument passed to |
| 5385 | * this function, disable otherwise. |
| 5386 | * |
| 5387 | * Returns 0 for success, non-zero in case of failure. |
| 5388 | * |
| 5389 | * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag |
| 5390 | * to know whether auto bkops is enabled or disabled after this function |
| 5391 | * returns control to it. |
| 5392 | */ |
| 5393 | static int ufshcd_bkops_ctrl(struct ufs_hba *hba, |
| 5394 | enum bkops_status status) |
| 5395 | { |
| 5396 | int err; |
| 5397 | u32 curr_status = 0; |
| 5398 | |
| 5399 | err = ufshcd_get_bkops_status(hba, &curr_status); |
| 5400 | if (err) { |
| 5401 | dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", |
| 5402 | __func__, err); |
| 5403 | goto out; |
| 5404 | } else if (curr_status > BKOPS_STATUS_MAX) { |
| 5405 | dev_err(hba->dev, "%s: invalid BKOPS status %d\n", |
| 5406 | __func__, curr_status); |
| 5407 | err = -EINVAL; |
| 5408 | goto out; |
| 5409 | } |
| 5410 | |
| 5411 | if (curr_status >= status) |
| 5412 | err = ufshcd_enable_auto_bkops(hba); |
| 5413 | else |
| 5414 | err = ufshcd_disable_auto_bkops(hba); |
| 5415 | out: |
| 5416 | return err; |
| 5417 | } |
| 5418 | |
| 5419 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5420 | * ufshcd_urgent_bkops - handle urgent bkops exception event |
| 5421 | * @hba: per-adapter instance |
| 5422 | * |
| 5423 | * Enable fBackgroundOpsEn flag in the device to permit background |
| 5424 | * operations. |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 5425 | * |
| 5426 | * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled |
| 5427 | * and negative error value for any other failure. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5428 | */ |
| 5429 | static int ufshcd_urgent_bkops(struct ufs_hba *hba) |
| 5430 | { |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 5431 | return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5432 | } |
| 5433 | |
| 5434 | static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status) |
| 5435 | { |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 5436 | return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5437 | QUERY_ATTR_IDN_EE_STATUS, 0, 0, status); |
| 5438 | } |
| 5439 | |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 5440 | static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba) |
| 5441 | { |
| 5442 | int err; |
| 5443 | u32 curr_status = 0; |
| 5444 | |
| 5445 | if (hba->is_urgent_bkops_lvl_checked) |
| 5446 | goto enable_auto_bkops; |
| 5447 | |
| 5448 | err = ufshcd_get_bkops_status(hba, &curr_status); |
| 5449 | if (err) { |
| 5450 | dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", |
| 5451 | __func__, err); |
| 5452 | goto out; |
| 5453 | } |
| 5454 | |
| 5455 | /* |
| 5456 | * We are seeing that some devices are raising the urgent bkops |
| 5457 | * exception events even when BKOPS status doesn't indicate performace |
| 5458 | * impacted or critical. Handle these device by determining their urgent |
| 5459 | * bkops status at runtime. |
| 5460 | */ |
| 5461 | if (curr_status < BKOPS_STATUS_PERF_IMPACT) { |
| 5462 | dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n", |
| 5463 | __func__, curr_status); |
| 5464 | /* update the current status as the urgent bkops level */ |
| 5465 | hba->urgent_bkops_lvl = curr_status; |
| 5466 | hba->is_urgent_bkops_lvl_checked = true; |
| 5467 | } |
| 5468 | |
| 5469 | enable_auto_bkops: |
| 5470 | err = ufshcd_enable_auto_bkops(hba); |
| 5471 | out: |
| 5472 | if (err < 0) |
| 5473 | dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n", |
| 5474 | __func__, err); |
| 5475 | } |
| 5476 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5477 | /** |
| 5478 | * ufshcd_exception_event_handler - handle exceptions raised by device |
| 5479 | * @work: pointer to work data |
| 5480 | * |
| 5481 | * Read bExceptionEventStatus attribute from the device and handle the |
| 5482 | * exception event accordingly. |
| 5483 | */ |
| 5484 | static void ufshcd_exception_event_handler(struct work_struct *work) |
| 5485 | { |
| 5486 | struct ufs_hba *hba; |
| 5487 | int err; |
| 5488 | u32 status = 0; |
| 5489 | hba = container_of(work, struct ufs_hba, eeh_work); |
| 5490 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 5491 | pm_runtime_get_sync(hba->dev); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5492 | ufshcd_scsi_block_requests(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5493 | err = ufshcd_get_ee_status(hba, &status); |
| 5494 | if (err) { |
| 5495 | dev_err(hba->dev, "%s: failed to get exception status %d\n", |
| 5496 | __func__, err); |
| 5497 | goto out; |
| 5498 | } |
| 5499 | |
| 5500 | status &= hba->ee_ctrl_mask; |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 5501 | |
| 5502 | if (status & MASK_EE_URGENT_BKOPS) |
| 5503 | ufshcd_bkops_exception_event_handler(hba); |
| 5504 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5505 | out: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5506 | ufshcd_scsi_unblock_requests(hba); |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 5507 | pm_runtime_put_sync(hba->dev); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5508 | return; |
| 5509 | } |
| 5510 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5511 | /* Complete requests that have door-bell cleared */ |
| 5512 | static void ufshcd_complete_requests(struct ufs_hba *hba) |
| 5513 | { |
| 5514 | ufshcd_transfer_req_compl(hba); |
| 5515 | ufshcd_tmc_handler(hba); |
| 5516 | } |
| 5517 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 5518 | /** |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5519 | * ufshcd_quirk_dl_nac_errors - This function checks if error handling is |
| 5520 | * to recover from the DL NAC errors or not. |
| 5521 | * @hba: per-adapter instance |
| 5522 | * |
| 5523 | * Returns true if error handling is required, false otherwise |
| 5524 | */ |
| 5525 | static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba) |
| 5526 | { |
| 5527 | unsigned long flags; |
| 5528 | bool err_handling = true; |
| 5529 | |
| 5530 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5531 | /* |
| 5532 | * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the |
| 5533 | * device fatal error and/or DL NAC & REPLAY timeout errors. |
| 5534 | */ |
| 5535 | if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR)) |
| 5536 | goto out; |
| 5537 | |
| 5538 | if ((hba->saved_err & DEVICE_FATAL_ERROR) || |
| 5539 | ((hba->saved_err & UIC_ERROR) && |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5540 | (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR))) { |
| 5541 | /* |
| 5542 | * we have to do error recovery but atleast silence the error |
| 5543 | * logs. |
| 5544 | */ |
| 5545 | hba->silence_err_logs = true; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5546 | goto out; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5547 | } |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5548 | |
| 5549 | if ((hba->saved_err & UIC_ERROR) && |
| 5550 | (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) { |
| 5551 | int err; |
| 5552 | /* |
| 5553 | * wait for 50ms to see if we can get any other errors or not. |
| 5554 | */ |
| 5555 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5556 | msleep(50); |
| 5557 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5558 | |
| 5559 | /* |
| 5560 | * now check if we have got any other severe errors other than |
| 5561 | * DL NAC error? |
| 5562 | */ |
| 5563 | if ((hba->saved_err & INT_FATAL_ERRORS) || |
| 5564 | ((hba->saved_err & UIC_ERROR) && |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5565 | (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR))) { |
| 5566 | if (((hba->saved_err & INT_FATAL_ERRORS) == |
| 5567 | DEVICE_FATAL_ERROR) || (hba->saved_uic_err & |
| 5568 | ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) |
| 5569 | hba->silence_err_logs = true; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5570 | goto out; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5571 | } |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5572 | |
| 5573 | /* |
| 5574 | * As DL NAC is the only error received so far, send out NOP |
| 5575 | * command to confirm if link is still active or not. |
| 5576 | * - If we don't get any response then do error recovery. |
| 5577 | * - If we get response then clear the DL NAC error bit. |
| 5578 | */ |
| 5579 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5580 | /* silence the error logs from NOP command */ |
| 5581 | hba->silence_err_logs = true; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5582 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5583 | err = ufshcd_verify_dev_init(hba); |
| 5584 | spin_lock_irqsave(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5585 | hba->silence_err_logs = false; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5586 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5587 | if (err) { |
| 5588 | hba->silence_err_logs = true; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5589 | goto out; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5590 | } |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5591 | |
| 5592 | /* Link seems to be alive hence ignore the DL NAC errors */ |
| 5593 | if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR) |
| 5594 | hba->saved_err &= ~UIC_ERROR; |
| 5595 | /* clear NAC error */ |
| 5596 | hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR; |
| 5597 | if (!hba->saved_uic_err) { |
| 5598 | err_handling = false; |
| 5599 | goto out; |
| 5600 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5601 | /* |
| 5602 | * there seems to be some errors other than NAC, so do error |
| 5603 | * recovery |
| 5604 | */ |
| 5605 | hba->silence_err_logs = true; |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5606 | } |
| 5607 | out: |
| 5608 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5609 | return err_handling; |
| 5610 | } |
| 5611 | |
| 5612 | /** |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5613 | * ufshcd_err_handler - handle UFS errors that require s/w attention |
| 5614 | * @work: pointer to work structure |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5615 | */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5616 | static void ufshcd_err_handler(struct work_struct *work) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5617 | { |
| 5618 | struct ufs_hba *hba; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5619 | unsigned long flags; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5620 | bool err_xfer = false, err_tm = false; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5621 | int err = 0; |
| 5622 | int tag; |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5623 | bool needs_reset = false; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5624 | |
| 5625 | hba = container_of(work, struct ufs_hba, eh_work); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5626 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5627 | ufsdbg_set_err_state(hba); |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 5628 | pm_runtime_get_sync(hba->dev); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5629 | ufshcd_hold_all(hba); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5630 | |
| 5631 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5632 | if (hba->ufshcd_state == UFSHCD_STATE_RESET) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5633 | goto out; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5634 | |
| 5635 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 5636 | ufshcd_set_eh_in_progress(hba); |
| 5637 | |
| 5638 | /* Complete requests that have door-bell cleared by h/w */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5639 | ufshcd_complete_requests(hba); |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5640 | |
| 5641 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) { |
| 5642 | bool ret; |
| 5643 | |
| 5644 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5645 | /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */ |
| 5646 | ret = ufshcd_quirk_dl_nac_errors(hba); |
| 5647 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5648 | if (!ret) |
| 5649 | goto skip_err_handling; |
| 5650 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5651 | |
| 5652 | /* |
| 5653 | * Dump controller state before resetting. Transfer requests state |
| 5654 | * will be dump as part of the request completion. |
| 5655 | */ |
| 5656 | if (hba->saved_err & (INT_FATAL_ERRORS | UIC_ERROR)) { |
| 5657 | dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x", |
| 5658 | __func__, hba->saved_err, hba->saved_uic_err); |
| 5659 | if (!hba->silence_err_logs) { |
| 5660 | ufshcd_print_host_regs(hba); |
| 5661 | ufshcd_print_host_state(hba); |
| 5662 | ufshcd_print_pwr_info(hba); |
| 5663 | ufshcd_print_tmrs(hba, hba->outstanding_tasks); |
| 5664 | } |
| 5665 | } |
| 5666 | |
| 5667 | if ((hba->saved_err & INT_FATAL_ERRORS) || hba->saved_ce_err || |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5668 | ((hba->saved_err & UIC_ERROR) && |
| 5669 | (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR | |
| 5670 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR | |
| 5671 | UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))) |
| 5672 | needs_reset = true; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5673 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5674 | /* |
| 5675 | * if host reset is required then skip clearing the pending |
| 5676 | * transfers forcefully because they will automatically get |
| 5677 | * cleared after link startup. |
| 5678 | */ |
| 5679 | if (needs_reset) |
| 5680 | goto skip_pending_xfer_clear; |
| 5681 | |
| 5682 | /* release lock as clear command might sleep */ |
| 5683 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5684 | /* Clear pending transfer requests */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5685 | for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) { |
| 5686 | if (ufshcd_clear_cmd(hba, tag)) { |
| 5687 | err_xfer = true; |
| 5688 | goto lock_skip_pending_xfer_clear; |
| 5689 | } |
| 5690 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5691 | |
| 5692 | /* Clear pending task management requests */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5693 | for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) { |
| 5694 | if (ufshcd_clear_tm_cmd(hba, tag)) { |
| 5695 | err_tm = true; |
| 5696 | goto lock_skip_pending_xfer_clear; |
| 5697 | } |
| 5698 | } |
| 5699 | |
| 5700 | lock_skip_pending_xfer_clear: |
| 5701 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5702 | |
| 5703 | /* Complete the requests that are cleared by s/w */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5704 | ufshcd_complete_requests(hba); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5705 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5706 | if (err_xfer || err_tm) |
| 5707 | needs_reset = true; |
| 5708 | |
| 5709 | skip_pending_xfer_clear: |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5710 | /* Fatal errors need reset */ |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5711 | if (needs_reset) { |
| 5712 | unsigned long max_doorbells = (1UL << hba->nutrs) - 1; |
| 5713 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5714 | if (hba->saved_err & INT_FATAL_ERRORS) |
| 5715 | ufshcd_update_error_stats(hba, |
| 5716 | UFS_ERR_INT_FATAL_ERRORS); |
| 5717 | if (hba->saved_ce_err) |
| 5718 | ufshcd_update_error_stats(hba, UFS_ERR_CRYPTO_ENGINE); |
| 5719 | |
| 5720 | if (hba->saved_err & UIC_ERROR) |
| 5721 | ufshcd_update_error_stats(hba, |
| 5722 | UFS_ERR_INT_UIC_ERROR); |
| 5723 | |
| 5724 | if (err_xfer || err_tm) |
| 5725 | ufshcd_update_error_stats(hba, |
| 5726 | UFS_ERR_CLEAR_PEND_XFER_TM); |
| 5727 | |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5728 | /* |
| 5729 | * ufshcd_reset_and_restore() does the link reinitialization |
| 5730 | * which will need atleast one empty doorbell slot to send the |
| 5731 | * device management commands (NOP and query commands). |
| 5732 | * If there is no slot empty at this moment then free up last |
| 5733 | * slot forcefully. |
| 5734 | */ |
| 5735 | if (hba->outstanding_reqs == max_doorbells) |
| 5736 | __ufshcd_transfer_req_compl(hba, |
| 5737 | (1UL << (hba->nutrs - 1))); |
| 5738 | |
| 5739 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5740 | err = ufshcd_reset_and_restore(hba); |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5741 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5742 | if (err) { |
| 5743 | dev_err(hba->dev, "%s: reset and restore failed\n", |
| 5744 | __func__); |
| 5745 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 5746 | } |
| 5747 | /* |
| 5748 | * Inform scsi mid-layer that we did reset and allow to handle |
| 5749 | * Unit Attention properly. |
| 5750 | */ |
| 5751 | scsi_report_bus_reset(hba->host, 0); |
| 5752 | hba->saved_err = 0; |
| 5753 | hba->saved_uic_err = 0; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5754 | hba->saved_ce_err = 0; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5755 | } |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5756 | |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5757 | skip_err_handling: |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5758 | if (!needs_reset) { |
| 5759 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
| 5760 | if (hba->saved_err || hba->saved_uic_err) |
| 5761 | dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x", |
| 5762 | __func__, hba->saved_err, hba->saved_uic_err); |
| 5763 | } |
| 5764 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5765 | hba->silence_err_logs = false; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5766 | ufshcd_clear_eh_in_progress(hba); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5767 | out: |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5768 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5769 | ufshcd_scsi_unblock_requests(hba); |
| 5770 | ufshcd_release_all(hba); |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 5771 | pm_runtime_put_sync(hba->dev); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5772 | } |
| 5773 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5774 | static void ufshcd_update_uic_reg_hist(struct ufs_uic_err_reg_hist *reg_hist, |
| 5775 | u32 reg) |
| 5776 | { |
| 5777 | reg_hist->reg[reg_hist->pos] = reg; |
| 5778 | reg_hist->tstamp[reg_hist->pos] = ktime_get(); |
| 5779 | reg_hist->pos = (reg_hist->pos + 1) % UIC_ERR_REG_HIST_LENGTH; |
| 5780 | } |
| 5781 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5782 | /** |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5783 | * ufshcd_update_uic_error - check and set fatal UIC error flags. |
| 5784 | * @hba: per-adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5785 | */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5786 | static void ufshcd_update_uic_error(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5787 | { |
| 5788 | u32 reg; |
| 5789 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5790 | /* PHY layer lane error */ |
| 5791 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER); |
| 5792 | /* Ignore LINERESET indication, as this is not an error */ |
| 5793 | if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) && |
| 5794 | (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK)) { |
| 5795 | /* |
| 5796 | * To know whether this error is fatal or not, DB timeout |
| 5797 | * must be checked but this error is handled separately. |
| 5798 | */ |
| 5799 | dev_dbg(hba->dev, "%s: UIC Lane error reported, reg 0x%x\n", |
| 5800 | __func__, reg); |
| 5801 | ufshcd_update_uic_reg_hist(&hba->ufs_stats.pa_err, reg); |
| 5802 | } |
| 5803 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5804 | /* PA_INIT_ERROR is fatal and needs UIC reset */ |
| 5805 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5806 | if (reg) |
| 5807 | ufshcd_update_uic_reg_hist(&hba->ufs_stats.dl_err, reg); |
| 5808 | |
| 5809 | if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT) { |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5810 | hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5811 | } else if (hba->dev_quirks & |
Yaniv Gardi | 583fa62 | 2016-03-10 17:37:13 +0200 | [diff] [blame] | 5812 | UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) { |
| 5813 | if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED) |
| 5814 | hba->uic_error |= |
| 5815 | UFSHCD_UIC_DL_NAC_RECEIVED_ERROR; |
| 5816 | else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT) |
| 5817 | hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR; |
| 5818 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5819 | |
| 5820 | /* UIC NL/TL/DME errors needs software retry */ |
| 5821 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5822 | if (reg) { |
| 5823 | ufshcd_update_uic_reg_hist(&hba->ufs_stats.nl_err, reg); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5824 | hba->uic_error |= UFSHCD_UIC_NL_ERROR; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5825 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5826 | |
| 5827 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5828 | if (reg) { |
| 5829 | ufshcd_update_uic_reg_hist(&hba->ufs_stats.tl_err, reg); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5830 | hba->uic_error |= UFSHCD_UIC_TL_ERROR; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5831 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5832 | |
| 5833 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5834 | if (reg) { |
| 5835 | ufshcd_update_uic_reg_hist(&hba->ufs_stats.dme_err, reg); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5836 | hba->uic_error |= UFSHCD_UIC_DME_ERROR; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5837 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5838 | |
| 5839 | dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n", |
| 5840 | __func__, hba->uic_error); |
| 5841 | } |
| 5842 | |
| 5843 | /** |
| 5844 | * ufshcd_check_errors - Check for errors that need s/w attention |
| 5845 | * @hba: per-adapter instance |
| 5846 | */ |
| 5847 | static void ufshcd_check_errors(struct ufs_hba *hba) |
| 5848 | { |
| 5849 | bool queue_eh_work = false; |
| 5850 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5851 | if (hba->errors & INT_FATAL_ERRORS || hba->ce_error) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5852 | queue_eh_work = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5853 | |
| 5854 | if (hba->errors & UIC_ERROR) { |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5855 | hba->uic_error = 0; |
| 5856 | ufshcd_update_uic_error(hba); |
| 5857 | if (hba->uic_error) |
| 5858 | queue_eh_work = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5859 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5860 | |
| 5861 | if (queue_eh_work) { |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5862 | /* |
| 5863 | * update the transfer error masks to sticky bits, let's do this |
| 5864 | * irrespective of current ufshcd_state. |
| 5865 | */ |
| 5866 | hba->saved_err |= hba->errors; |
| 5867 | hba->saved_uic_err |= hba->uic_error; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5868 | hba->saved_ce_err |= hba->ce_error; |
Yaniv Gardi | 9a47ec7 | 2016-03-10 17:37:12 +0200 | [diff] [blame] | 5869 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5870 | /* handle fatal errors only when link is functional */ |
| 5871 | if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) { |
| 5872 | /* block commands from scsi mid-layer */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5873 | __ufshcd_scsi_block_requests(hba); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5874 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5875 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 5876 | schedule_work(&hba->eh_work); |
| 5877 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 5878 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5879 | /* |
| 5880 | * if (!queue_eh_work) - |
| 5881 | * Other errors are either non-fatal where host recovers |
| 5882 | * itself without s/w intervention or errors that will be |
| 5883 | * handled by the SCSI core layer. |
| 5884 | */ |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5885 | } |
| 5886 | |
| 5887 | /** |
| 5888 | * ufshcd_tmc_handler - handle task management function completion |
| 5889 | * @hba: per adapter instance |
| 5890 | */ |
| 5891 | static void ufshcd_tmc_handler(struct ufs_hba *hba) |
| 5892 | { |
| 5893 | u32 tm_doorbell; |
| 5894 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 5895 | tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5896 | hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5897 | wake_up(&hba->tm_wq); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5898 | } |
| 5899 | |
| 5900 | /** |
| 5901 | * ufshcd_sl_intr - Interrupt service routine |
| 5902 | * @hba: per adapter instance |
| 5903 | * @intr_status: contains interrupts generated by the controller |
| 5904 | */ |
| 5905 | static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status) |
| 5906 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5907 | ufsdbg_error_inject_dispatcher(hba, |
| 5908 | ERR_INJECT_INTR, intr_status, &intr_status); |
| 5909 | |
| 5910 | ufshcd_vops_crypto_engine_get_status(hba, &hba->ce_error); |
| 5911 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5912 | hba->errors = UFSHCD_ERROR_MASK & intr_status; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 5913 | if (hba->errors || hba->ce_error) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 5914 | ufshcd_check_errors(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5915 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 5916 | if (intr_status & UFSHCD_UIC_MASK) |
| 5917 | ufshcd_uic_cmd_compl(hba, intr_status); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5918 | |
| 5919 | if (intr_status & UTP_TASK_REQ_COMPL) |
| 5920 | ufshcd_tmc_handler(hba); |
| 5921 | |
| 5922 | if (intr_status & UTP_TRANSFER_REQ_COMPL) |
| 5923 | ufshcd_transfer_req_compl(hba); |
| 5924 | } |
| 5925 | |
| 5926 | /** |
| 5927 | * ufshcd_intr - Main interrupt service routine |
| 5928 | * @irq: irq number |
| 5929 | * @__hba: pointer to adapter instance |
| 5930 | * |
| 5931 | * Returns IRQ_HANDLED - If interrupt is valid |
| 5932 | * IRQ_NONE - If invalid interrupt |
| 5933 | */ |
| 5934 | static irqreturn_t ufshcd_intr(int irq, void *__hba) |
| 5935 | { |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 5936 | u32 intr_status, enabled_intr_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5937 | irqreturn_t retval = IRQ_NONE; |
| 5938 | struct ufs_hba *hba = __hba; |
| 5939 | |
| 5940 | spin_lock(hba->host->host_lock); |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 5941 | intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 5942 | enabled_intr_status = |
| 5943 | intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5944 | |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 5945 | if (intr_status) |
Seungwon Jeon | 261ea45 | 2013-06-26 22:39:28 +0530 | [diff] [blame] | 5946 | ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS); |
Yaniv Gardi | d75f7fe | 2016-02-01 15:02:47 +0200 | [diff] [blame] | 5947 | |
| 5948 | if (enabled_intr_status) { |
| 5949 | ufshcd_sl_intr(hba, enabled_intr_status); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5950 | retval = IRQ_HANDLED; |
| 5951 | } |
| 5952 | spin_unlock(hba->host->host_lock); |
| 5953 | return retval; |
| 5954 | } |
| 5955 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5956 | static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag) |
| 5957 | { |
| 5958 | int err = 0; |
| 5959 | u32 mask = 1 << tag; |
| 5960 | unsigned long flags; |
| 5961 | |
| 5962 | if (!test_bit(tag, &hba->outstanding_tasks)) |
| 5963 | goto out; |
| 5964 | |
| 5965 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 5966 | ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR); |
| 5967 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 5968 | |
| 5969 | /* poll for max. 1 sec to clear door bell register by h/w */ |
| 5970 | err = ufshcd_wait_for_register(hba, |
| 5971 | REG_UTP_TASK_REQ_DOOR_BELL, |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 5972 | mask, 0, 1000, 1000, true); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5973 | out: |
| 5974 | return err; |
| 5975 | } |
| 5976 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5977 | /** |
| 5978 | * ufshcd_issue_tm_cmd - issues task management commands to controller |
| 5979 | * @hba: per adapter instance |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5980 | * @lun_id: LUN ID to which TM command is sent |
| 5981 | * @task_id: task ID to which the TM command is applicable |
| 5982 | * @tm_function: task management function opcode |
| 5983 | * @tm_response: task management service response return value |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5984 | * |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5985 | * Returns non-zero value on error, zero on success. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5986 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5987 | static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id, |
| 5988 | u8 tm_function, u8 *tm_response) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5989 | { |
| 5990 | struct utp_task_req_desc *task_req_descp; |
| 5991 | struct utp_upiu_task_req *task_req_upiup; |
| 5992 | struct Scsi_Host *host; |
| 5993 | unsigned long flags; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5994 | int free_slot; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5995 | int err; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 5996 | int task_tag; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 5997 | |
| 5998 | host = hba->host; |
| 5999 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6000 | /* |
| 6001 | * Get free slot, sleep if slots are unavailable. |
| 6002 | * Even though we use wait_event() which sleeps indefinitely, |
| 6003 | * the maximum wait time is bounded by %TM_CMD_TIMEOUT. |
| 6004 | */ |
| 6005 | wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot)); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6006 | ufshcd_hold_all(hba); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6007 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6008 | spin_lock_irqsave(host->host_lock, flags); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6009 | task_req_descp = hba->utmrdl_base_addr; |
| 6010 | task_req_descp += free_slot; |
| 6011 | |
| 6012 | /* Configure task request descriptor */ |
| 6013 | task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD); |
| 6014 | task_req_descp->header.dword_2 = |
| 6015 | cpu_to_le32(OCS_INVALID_COMMAND_STATUS); |
| 6016 | |
| 6017 | /* Configure task request UPIU */ |
| 6018 | task_req_upiup = |
| 6019 | (struct utp_upiu_task_req *) task_req_descp->task_req_upiu; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6020 | task_tag = hba->nutrs + free_slot; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6021 | task_req_upiup->header.dword_0 = |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 6022 | UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0, |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6023 | lun_id, task_tag); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6024 | task_req_upiup->header.dword_1 = |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 6025 | UPIU_HEADER_DWORD(0, tm_function, 0, 0); |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 6026 | /* |
| 6027 | * The host shall provide the same value for LUN field in the basic |
| 6028 | * header and for Input Parameter. |
| 6029 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6030 | task_req_upiup->input_param1 = cpu_to_be32(lun_id); |
| 6031 | task_req_upiup->input_param2 = cpu_to_be32(task_id); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6032 | |
| 6033 | /* send command to the controller */ |
| 6034 | __set_bit(free_slot, &hba->outstanding_tasks); |
Yaniv Gardi | 897efe6 | 2016-02-01 15:02:48 +0200 | [diff] [blame] | 6035 | |
| 6036 | /* Make sure descriptors are ready before ringing the task doorbell */ |
| 6037 | wmb(); |
| 6038 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 6039 | ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6040 | /* Make sure that doorbell is committed immediately */ |
| 6041 | wmb(); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6042 | |
| 6043 | spin_unlock_irqrestore(host->host_lock, flags); |
| 6044 | |
| 6045 | /* wait until the task management command is completed */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6046 | err = wait_event_timeout(hba->tm_wq, |
| 6047 | test_bit(free_slot, &hba->tm_condition), |
| 6048 | msecs_to_jiffies(TM_CMD_TIMEOUT)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6049 | if (!err) { |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6050 | dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n", |
| 6051 | __func__, tm_function); |
| 6052 | if (ufshcd_clear_tm_cmd(hba, free_slot)) |
| 6053 | dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n", |
| 6054 | __func__, free_slot); |
| 6055 | err = -ETIMEDOUT; |
| 6056 | } else { |
| 6057 | err = ufshcd_task_req_compl(hba, free_slot, tm_response); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6058 | } |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6059 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6060 | clear_bit(free_slot, &hba->tm_condition); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6061 | ufshcd_put_tm_slot(hba, free_slot); |
| 6062 | wake_up(&hba->tm_tag_wq); |
| 6063 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6064 | ufshcd_release_all(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6065 | return err; |
| 6066 | } |
| 6067 | |
| 6068 | /** |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6069 | * ufshcd_eh_device_reset_handler - device reset handler registered to |
| 6070 | * scsi layer. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6071 | * @cmd: SCSI command pointer |
| 6072 | * |
| 6073 | * Returns SUCCESS/FAILED |
| 6074 | */ |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6075 | static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6076 | { |
| 6077 | struct Scsi_Host *host; |
| 6078 | struct ufs_hba *hba; |
| 6079 | unsigned int tag; |
| 6080 | u32 pos; |
| 6081 | int err; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6082 | u8 resp = 0xF; |
| 6083 | struct ufshcd_lrb *lrbp; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6084 | unsigned long flags; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6085 | |
| 6086 | host = cmd->device->host; |
| 6087 | hba = shost_priv(host); |
| 6088 | tag = cmd->request->tag; |
| 6089 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6090 | lrbp = &hba->lrb[tag]; |
| 6091 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp); |
| 6092 | if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6093 | if (!err) |
| 6094 | err = resp; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6095 | goto out; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6096 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6097 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6098 | /* clear the commands that were pending for corresponding LUN */ |
| 6099 | for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) { |
| 6100 | if (hba->lrb[pos].lun == lrbp->lun) { |
| 6101 | err = ufshcd_clear_cmd(hba, pos); |
| 6102 | if (err) |
| 6103 | break; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6104 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6105 | } |
| 6106 | spin_lock_irqsave(host->host_lock, flags); |
| 6107 | ufshcd_transfer_req_compl(hba); |
| 6108 | spin_unlock_irqrestore(host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6109 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6110 | out: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6111 | hba->req_abort_count = 0; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6112 | if (!err) { |
| 6113 | err = SUCCESS; |
| 6114 | } else { |
| 6115 | dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); |
| 6116 | err = FAILED; |
| 6117 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6118 | return err; |
| 6119 | } |
| 6120 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6121 | static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap) |
| 6122 | { |
| 6123 | struct ufshcd_lrb *lrbp; |
| 6124 | int tag; |
| 6125 | |
| 6126 | for_each_set_bit(tag, &bitmap, hba->nutrs) { |
| 6127 | lrbp = &hba->lrb[tag]; |
| 6128 | lrbp->req_abort_skip = true; |
| 6129 | } |
| 6130 | } |
| 6131 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6132 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6133 | * ufshcd_abort - abort a specific command |
| 6134 | * @cmd: SCSI command pointer |
| 6135 | * |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6136 | * Abort the pending command in device by sending UFS_ABORT_TASK task management |
| 6137 | * command, and in host controller by clearing the door-bell register. There can |
| 6138 | * be race between controller sending the command to the device while abort is |
| 6139 | * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is |
| 6140 | * really issued and then try to abort it. |
| 6141 | * |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6142 | * Returns SUCCESS/FAILED |
| 6143 | */ |
| 6144 | static int ufshcd_abort(struct scsi_cmnd *cmd) |
| 6145 | { |
| 6146 | struct Scsi_Host *host; |
| 6147 | struct ufs_hba *hba; |
| 6148 | unsigned long flags; |
| 6149 | unsigned int tag; |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6150 | int err = 0; |
| 6151 | int poll_cnt; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6152 | u8 resp = 0xF; |
| 6153 | struct ufshcd_lrb *lrbp; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 6154 | u32 reg; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6155 | |
| 6156 | host = cmd->device->host; |
| 6157 | hba = shost_priv(host); |
| 6158 | tag = cmd->request->tag; |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 6159 | if (!ufshcd_valid_tag(hba, tag)) { |
| 6160 | dev_err(hba->dev, |
| 6161 | "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p", |
| 6162 | __func__, tag, cmd, cmd->request); |
| 6163 | BUG(); |
| 6164 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6165 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6166 | lrbp = &hba->lrb[tag]; |
| 6167 | |
| 6168 | ufshcd_update_error_stats(hba, UFS_ERR_TASK_ABORT); |
| 6169 | |
| 6170 | /* |
| 6171 | * Task abort to the device W-LUN is illegal. When this command |
| 6172 | * will fail, due to spec violation, scsi err handling next step |
| 6173 | * will be to send LU reset which, again, is a spec violation. |
| 6174 | * To avoid these unnecessary/illegal step we skip to the last error |
| 6175 | * handling stage: reset and restore. |
| 6176 | */ |
| 6177 | if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN) |
| 6178 | return ufshcd_eh_host_reset_handler(cmd); |
| 6179 | |
| 6180 | ufshcd_hold_all(hba); |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 6181 | reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
Yaniv Gardi | 1449732 | 2016-02-01 15:02:39 +0200 | [diff] [blame] | 6182 | /* If command is already aborted/completed, return SUCCESS */ |
| 6183 | if (!(test_bit(tag, &hba->outstanding_reqs))) { |
| 6184 | dev_err(hba->dev, |
| 6185 | "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n", |
| 6186 | __func__, tag, hba->outstanding_reqs, reg); |
| 6187 | goto out; |
| 6188 | } |
| 6189 | |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 6190 | if (!(reg & (1 << tag))) { |
| 6191 | dev_err(hba->dev, |
| 6192 | "%s: cmd was completed, but without a notifying intr, tag = %d", |
| 6193 | __func__, tag); |
| 6194 | } |
| 6195 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6196 | /* Print Transfer Request of aborted task */ |
| 6197 | dev_err(hba->dev, "%s: Device abort task at tag %d", __func__, tag); |
| 6198 | |
| 6199 | /* |
| 6200 | * Print detailed info about aborted request. |
| 6201 | * As more than one request might get aborted at the same time, |
| 6202 | * print full information only for the first aborted request in order |
| 6203 | * to reduce repeated printouts. For other aborted requests only print |
| 6204 | * basic details. |
| 6205 | */ |
| 6206 | scsi_print_command(cmd); |
| 6207 | if (!hba->req_abort_count) { |
| 6208 | ufshcd_print_host_regs(hba); |
| 6209 | ufshcd_print_host_state(hba); |
| 6210 | ufshcd_print_pwr_info(hba); |
| 6211 | ufshcd_print_trs(hba, 1 << tag, true); |
| 6212 | } else { |
| 6213 | ufshcd_print_trs(hba, 1 << tag, false); |
| 6214 | } |
| 6215 | hba->req_abort_count++; |
| 6216 | |
| 6217 | |
| 6218 | /* Skip task abort in case previous aborts failed and report failure */ |
| 6219 | if (lrbp->req_abort_skip) { |
| 6220 | err = -EIO; |
| 6221 | goto out; |
| 6222 | } |
| 6223 | |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6224 | for (poll_cnt = 100; poll_cnt; poll_cnt--) { |
| 6225 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, |
| 6226 | UFS_QUERY_TASK, &resp); |
| 6227 | if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) { |
| 6228 | /* cmd pending in the device */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6229 | dev_err(hba->dev, "%s: cmd pending in the device. tag = %d", |
| 6230 | __func__, tag); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6231 | break; |
| 6232 | } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6233 | /* |
| 6234 | * cmd not pending in the device, check if it is |
| 6235 | * in transition. |
| 6236 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6237 | dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.", |
| 6238 | __func__, tag); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6239 | reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 6240 | if (reg & (1 << tag)) { |
| 6241 | /* sleep for max. 200us to stabilize */ |
| 6242 | usleep_range(100, 200); |
| 6243 | continue; |
| 6244 | } |
| 6245 | /* command completed already */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6246 | dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.", |
| 6247 | __func__, tag); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6248 | goto out; |
| 6249 | } else { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6250 | dev_err(hba->dev, |
| 6251 | "%s: no response from device. tag = %d, err %d", |
| 6252 | __func__, tag, err); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6253 | if (!err) |
| 6254 | err = resp; /* service response error */ |
| 6255 | goto out; |
| 6256 | } |
| 6257 | } |
| 6258 | |
| 6259 | if (!poll_cnt) { |
| 6260 | err = -EBUSY; |
| 6261 | goto out; |
| 6262 | } |
| 6263 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6264 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, |
| 6265 | UFS_ABORT_TASK, &resp); |
| 6266 | if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6267 | if (!err) { |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6268 | err = resp; /* service response error */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6269 | dev_err(hba->dev, "%s: issued. tag = %d, err %d", |
| 6270 | __func__, tag, err); |
| 6271 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6272 | goto out; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 6273 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6274 | |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6275 | err = ufshcd_clear_cmd(hba, tag); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6276 | if (err) { |
| 6277 | dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d", |
| 6278 | __func__, tag, err); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6279 | goto out; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6280 | } |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6281 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6282 | scsi_dma_unmap(cmd); |
| 6283 | |
| 6284 | spin_lock_irqsave(host->host_lock, flags); |
Yaniv Gardi | a48353f | 2016-02-01 15:02:40 +0200 | [diff] [blame] | 6285 | ufshcd_outstanding_req_clear(hba, tag); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6286 | hba->lrb[tag].cmd = NULL; |
| 6287 | spin_unlock_irqrestore(host->host_lock, flags); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 6288 | |
| 6289 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 6290 | wake_up(&hba->dev_cmd.tag_wq); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6291 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6292 | out: |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6293 | if (!err) { |
| 6294 | err = SUCCESS; |
| 6295 | } else { |
| 6296 | dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6297 | ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs); |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 6298 | err = FAILED; |
| 6299 | } |
| 6300 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6301 | /* |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6302 | * This ufshcd_release_all() corresponds to the original scsi cmd that |
| 6303 | * got aborted here (as we won't get any IRQ for it). |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 6304 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6305 | ufshcd_release_all(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6306 | return err; |
| 6307 | } |
| 6308 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6309 | /** |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6310 | * ufshcd_host_reset_and_restore - reset and restore host controller |
| 6311 | * @hba: per-adapter instance |
| 6312 | * |
| 6313 | * Note that host controller reset may issue DME_RESET to |
| 6314 | * local and remote (device) Uni-Pro stack and the attributes |
| 6315 | * are reset to default state. |
| 6316 | * |
| 6317 | * Returns zero on success, non-zero on failure |
| 6318 | */ |
| 6319 | static int ufshcd_host_reset_and_restore(struct ufs_hba *hba) |
| 6320 | { |
| 6321 | int err; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6322 | unsigned long flags; |
| 6323 | |
| 6324 | /* Reset the host controller */ |
| 6325 | spin_lock_irqsave(hba->host->host_lock, flags); |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 6326 | ufshcd_hba_stop(hba, false); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6327 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6328 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6329 | /* scale up clocks to max frequency before full reinitialization */ |
| 6330 | ufshcd_set_clk_freq(hba, true); |
| 6331 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6332 | err = ufshcd_hba_enable(hba); |
| 6333 | if (err) |
| 6334 | goto out; |
| 6335 | |
| 6336 | /* Establish the link again and restore the device */ |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6337 | err = ufshcd_probe_hba(hba); |
| 6338 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6339 | if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL)) { |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6340 | err = -EIO; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6341 | goto out; |
| 6342 | } |
| 6343 | |
| 6344 | if (!err) { |
| 6345 | err = ufshcd_vops_crypto_engine_reset(hba); |
| 6346 | if (err) { |
| 6347 | dev_err(hba->dev, |
| 6348 | "%s: failed to reset crypto engine %d\n", |
| 6349 | __func__, err); |
| 6350 | goto out; |
| 6351 | } |
| 6352 | } |
| 6353 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6354 | out: |
| 6355 | if (err) |
| 6356 | dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err); |
| 6357 | |
| 6358 | return err; |
| 6359 | } |
| 6360 | |
| 6361 | /** |
| 6362 | * ufshcd_reset_and_restore - reset and re-initialize host/device |
| 6363 | * @hba: per-adapter instance |
| 6364 | * |
| 6365 | * Reset and recover device, host and re-establish link. This |
| 6366 | * is helpful to recover the communication in fatal error conditions. |
| 6367 | * |
| 6368 | * Returns zero on success, non-zero on failure |
| 6369 | */ |
| 6370 | static int ufshcd_reset_and_restore(struct ufs_hba *hba) |
| 6371 | { |
| 6372 | int err = 0; |
| 6373 | unsigned long flags; |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6374 | int retries = MAX_HOST_RESET_RETRIES; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6375 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6376 | do { |
| 6377 | err = ufshcd_host_reset_and_restore(hba); |
| 6378 | } while (err && --retries); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6379 | |
| 6380 | /* |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6381 | * There is no point proceeding even after failing |
| 6382 | * to recover after multiple retries. |
| 6383 | */ |
| 6384 | if (err) |
| 6385 | BUG(); |
| 6386 | /* |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6387 | * After reset the door-bell might be cleared, complete |
| 6388 | * outstanding requests in s/w here. |
| 6389 | */ |
| 6390 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 6391 | ufshcd_transfer_req_compl(hba); |
| 6392 | ufshcd_tmc_handler(hba); |
| 6393 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6394 | |
| 6395 | return err; |
| 6396 | } |
| 6397 | |
| 6398 | /** |
| 6399 | * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer |
| 6400 | * @cmd - SCSI command pointer |
| 6401 | * |
| 6402 | * Returns SUCCESS/FAILED |
| 6403 | */ |
| 6404 | static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd) |
| 6405 | { |
| 6406 | int err; |
| 6407 | unsigned long flags; |
| 6408 | struct ufs_hba *hba; |
| 6409 | |
| 6410 | hba = shost_priv(cmd->device->host); |
| 6411 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6412 | ufshcd_hold_all(hba); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6413 | /* |
| 6414 | * Check if there is any race with fatal error handling. |
| 6415 | * If so, wait for it to complete. Even though fatal error |
| 6416 | * handling does reset and restore in some cases, don't assume |
| 6417 | * anything out of it. We are just avoiding race here. |
| 6418 | */ |
| 6419 | do { |
| 6420 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6421 | if (!(work_pending(&hba->eh_work) || |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6422 | hba->ufshcd_state == UFSHCD_STATE_RESET)) |
| 6423 | break; |
| 6424 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6425 | dev_dbg(hba->dev, "%s: reset in progress\n", __func__); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 6426 | flush_work(&hba->eh_work); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6427 | } while (1); |
| 6428 | |
| 6429 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 6430 | ufshcd_set_eh_in_progress(hba); |
| 6431 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6432 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6433 | ufshcd_update_error_stats(hba, UFS_ERR_EH); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6434 | err = ufshcd_reset_and_restore(hba); |
| 6435 | |
| 6436 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 6437 | if (!err) { |
| 6438 | err = SUCCESS; |
| 6439 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
| 6440 | } else { |
| 6441 | err = FAILED; |
| 6442 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 6443 | } |
| 6444 | ufshcd_clear_eh_in_progress(hba); |
| 6445 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 6446 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6447 | ufshcd_release_all(hba); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6448 | return err; |
| 6449 | } |
| 6450 | |
| 6451 | /** |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6452 | * ufshcd_get_max_icc_level - calculate the ICC level |
| 6453 | * @sup_curr_uA: max. current supported by the regulator |
| 6454 | * @start_scan: row at the desc table to start scan from |
| 6455 | * @buff: power descriptor buffer |
| 6456 | * |
| 6457 | * Returns calculated max ICC level for specific regulator |
| 6458 | */ |
| 6459 | static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff) |
| 6460 | { |
| 6461 | int i; |
| 6462 | int curr_uA; |
| 6463 | u16 data; |
| 6464 | u16 unit; |
| 6465 | |
| 6466 | for (i = start_scan; i >= 0; i--) { |
| 6467 | data = be16_to_cpu(*((u16 *)(buff + 2*i))); |
| 6468 | unit = (data & ATTR_ICC_LVL_UNIT_MASK) >> |
| 6469 | ATTR_ICC_LVL_UNIT_OFFSET; |
| 6470 | curr_uA = data & ATTR_ICC_LVL_VALUE_MASK; |
| 6471 | switch (unit) { |
| 6472 | case UFSHCD_NANO_AMP: |
| 6473 | curr_uA = curr_uA / 1000; |
| 6474 | break; |
| 6475 | case UFSHCD_MILI_AMP: |
| 6476 | curr_uA = curr_uA * 1000; |
| 6477 | break; |
| 6478 | case UFSHCD_AMP: |
| 6479 | curr_uA = curr_uA * 1000 * 1000; |
| 6480 | break; |
| 6481 | case UFSHCD_MICRO_AMP: |
| 6482 | default: |
| 6483 | break; |
| 6484 | } |
| 6485 | if (sup_curr_uA >= curr_uA) |
| 6486 | break; |
| 6487 | } |
| 6488 | if (i < 0) { |
| 6489 | i = 0; |
| 6490 | pr_err("%s: Couldn't find valid icc_level = %d", __func__, i); |
| 6491 | } |
| 6492 | |
| 6493 | return (u32)i; |
| 6494 | } |
| 6495 | |
| 6496 | /** |
| 6497 | * ufshcd_calc_icc_level - calculate the max ICC level |
| 6498 | * In case regulators are not initialized we'll return 0 |
| 6499 | * @hba: per-adapter instance |
| 6500 | * @desc_buf: power descriptor buffer to extract ICC levels from. |
| 6501 | * @len: length of desc_buff |
| 6502 | * |
| 6503 | * Returns calculated ICC level |
| 6504 | */ |
| 6505 | static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba, |
| 6506 | u8 *desc_buf, int len) |
| 6507 | { |
| 6508 | u32 icc_level = 0; |
| 6509 | |
| 6510 | if (!hba->vreg_info.vcc || !hba->vreg_info.vccq || |
| 6511 | !hba->vreg_info.vccq2) { |
| 6512 | dev_err(hba->dev, |
| 6513 | "%s: Regulator capability was not set, actvIccLevel=%d", |
| 6514 | __func__, icc_level); |
| 6515 | goto out; |
| 6516 | } |
| 6517 | |
| 6518 | if (hba->vreg_info.vcc) |
| 6519 | icc_level = ufshcd_get_max_icc_level( |
| 6520 | hba->vreg_info.vcc->max_uA, |
| 6521 | POWER_DESC_MAX_ACTV_ICC_LVLS - 1, |
| 6522 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]); |
| 6523 | |
| 6524 | if (hba->vreg_info.vccq) |
| 6525 | icc_level = ufshcd_get_max_icc_level( |
| 6526 | hba->vreg_info.vccq->max_uA, |
| 6527 | icc_level, |
| 6528 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]); |
| 6529 | |
| 6530 | if (hba->vreg_info.vccq2) |
| 6531 | icc_level = ufshcd_get_max_icc_level( |
| 6532 | hba->vreg_info.vccq2->max_uA, |
| 6533 | icc_level, |
| 6534 | &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]); |
| 6535 | out: |
| 6536 | return icc_level; |
| 6537 | } |
| 6538 | |
| 6539 | static void ufshcd_init_icc_levels(struct ufs_hba *hba) |
| 6540 | { |
| 6541 | int ret; |
| 6542 | int buff_len = QUERY_DESC_POWER_MAX_SIZE; |
| 6543 | u8 desc_buf[QUERY_DESC_POWER_MAX_SIZE]; |
Subhash Jadavani | 35732e5 | 2016-12-09 16:09:42 -0800 | [diff] [blame^] | 6544 | u32 icc_level; |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6545 | |
| 6546 | ret = ufshcd_read_power_desc(hba, desc_buf, buff_len); |
| 6547 | if (ret) { |
| 6548 | dev_err(hba->dev, |
| 6549 | "%s: Failed reading power descriptor.len = %d ret = %d", |
| 6550 | __func__, buff_len, ret); |
| 6551 | return; |
| 6552 | } |
| 6553 | |
Subhash Jadavani | 35732e5 | 2016-12-09 16:09:42 -0800 | [diff] [blame^] | 6554 | icc_level = ufshcd_find_max_sup_active_icc_level(hba, desc_buf, |
| 6555 | buff_len); |
| 6556 | 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] | 6557 | |
Yaniv Gardi | 5e86ae4 | 2016-02-01 15:02:50 +0200 | [diff] [blame] | 6558 | ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
Subhash Jadavani | 35732e5 | 2016-12-09 16:09:42 -0800 | [diff] [blame^] | 6559 | QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, &icc_level); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6560 | |
| 6561 | if (ret) |
| 6562 | dev_err(hba->dev, |
| 6563 | "%s: Failed configuring bActiveICCLevel = %d ret = %d", |
Subhash Jadavani | 35732e5 | 2016-12-09 16:09:42 -0800 | [diff] [blame^] | 6564 | __func__, icc_level, ret); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6565 | } |
| 6566 | |
| 6567 | /** |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6568 | * ufshcd_scsi_add_wlus - Adds required W-LUs |
| 6569 | * @hba: per-adapter instance |
| 6570 | * |
| 6571 | * UFS device specification requires the UFS devices to support 4 well known |
| 6572 | * logical units: |
| 6573 | * "REPORT_LUNS" (address: 01h) |
| 6574 | * "UFS Device" (address: 50h) |
| 6575 | * "RPMB" (address: 44h) |
| 6576 | * "BOOT" (address: 30h) |
| 6577 | * UFS device's power management needs to be controlled by "POWER CONDITION" |
| 6578 | * field of SSU (START STOP UNIT) command. But this "power condition" field |
| 6579 | * will take effect only when its sent to "UFS device" well known logical unit |
| 6580 | * hence we require the scsi_device instance to represent this logical unit in |
| 6581 | * order for the UFS host driver to send the SSU command for power management. |
| 6582 | |
| 6583 | * We also require the scsi_device instance for "RPMB" (Replay Protected Memory |
| 6584 | * Block) LU so user space process can control this LU. User space may also |
| 6585 | * want to have access to BOOT LU. |
| 6586 | |
| 6587 | * This function adds scsi device instances for each of all well known LUs |
| 6588 | * (except "REPORT LUNS" LU). |
| 6589 | * |
| 6590 | * Returns zero on success (all required W-LUs are added successfully), |
| 6591 | * non-zero error value on failure (if failed to add any of the required W-LU). |
| 6592 | */ |
| 6593 | static int ufshcd_scsi_add_wlus(struct ufs_hba *hba) |
| 6594 | { |
| 6595 | int ret = 0; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6596 | struct scsi_device *sdev_rpmb; |
| 6597 | struct scsi_device *sdev_boot; |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6598 | |
| 6599 | hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0, |
| 6600 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL); |
| 6601 | if (IS_ERR(hba->sdev_ufs_device)) { |
| 6602 | ret = PTR_ERR(hba->sdev_ufs_device); |
| 6603 | hba->sdev_ufs_device = NULL; |
| 6604 | goto out; |
| 6605 | } |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6606 | scsi_device_put(hba->sdev_ufs_device); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6607 | |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6608 | sdev_boot = __scsi_add_device(hba->host, 0, 0, |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6609 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL); |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6610 | if (IS_ERR(sdev_boot)) { |
| 6611 | ret = PTR_ERR(sdev_boot); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6612 | goto remove_sdev_ufs_device; |
| 6613 | } |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6614 | scsi_device_put(sdev_boot); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6615 | |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6616 | sdev_rpmb = __scsi_add_device(hba->host, 0, 0, |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6617 | ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL); |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6618 | if (IS_ERR(sdev_rpmb)) { |
| 6619 | ret = PTR_ERR(sdev_rpmb); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6620 | goto remove_sdev_boot; |
| 6621 | } |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6622 | scsi_device_put(sdev_rpmb); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6623 | goto out; |
| 6624 | |
| 6625 | remove_sdev_boot: |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 6626 | scsi_remove_device(sdev_boot); |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6627 | remove_sdev_ufs_device: |
| 6628 | scsi_remove_device(hba->sdev_ufs_device); |
| 6629 | out: |
| 6630 | return ret; |
| 6631 | } |
| 6632 | |
| 6633 | /** |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 6634 | * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro |
| 6635 | * @hba: per-adapter instance |
| 6636 | * |
| 6637 | * PA_TActivate parameter can be tuned manually if UniPro version is less than |
| 6638 | * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's |
| 6639 | * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce |
| 6640 | * the hibern8 exit latency. |
| 6641 | * |
| 6642 | * Returns zero on success, non-zero error value on failure. |
| 6643 | */ |
| 6644 | static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba) |
| 6645 | { |
| 6646 | int ret = 0; |
| 6647 | u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate; |
| 6648 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6649 | if (!ufshcd_is_unipro_pa_params_tuning_req(hba)) |
| 6650 | return 0; |
| 6651 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 6652 | ret = ufshcd_dme_peer_get(hba, |
| 6653 | UIC_ARG_MIB_SEL( |
| 6654 | RX_MIN_ACTIVATETIME_CAPABILITY, |
| 6655 | UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), |
| 6656 | &peer_rx_min_activatetime); |
| 6657 | if (ret) |
| 6658 | goto out; |
| 6659 | |
| 6660 | /* make sure proper unit conversion is applied */ |
| 6661 | tuned_pa_tactivate = |
| 6662 | ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US) |
| 6663 | / PA_TACTIVATE_TIME_UNIT_US); |
| 6664 | ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 6665 | tuned_pa_tactivate); |
| 6666 | |
| 6667 | out: |
| 6668 | return ret; |
| 6669 | } |
| 6670 | |
| 6671 | /** |
| 6672 | * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro |
| 6673 | * @hba: per-adapter instance |
| 6674 | * |
| 6675 | * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than |
| 6676 | * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's |
| 6677 | * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY. |
| 6678 | * This optimal value can help reduce the hibern8 exit latency. |
| 6679 | * |
| 6680 | * Returns zero on success, non-zero error value on failure. |
| 6681 | */ |
| 6682 | static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba) |
| 6683 | { |
| 6684 | int ret = 0; |
| 6685 | u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0; |
| 6686 | u32 max_hibern8_time, tuned_pa_hibern8time; |
| 6687 | |
| 6688 | ret = ufshcd_dme_get(hba, |
| 6689 | UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY, |
| 6690 | UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)), |
| 6691 | &local_tx_hibern8_time_cap); |
| 6692 | if (ret) |
| 6693 | goto out; |
| 6694 | |
| 6695 | ret = ufshcd_dme_peer_get(hba, |
| 6696 | UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY, |
| 6697 | UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)), |
| 6698 | &peer_rx_hibern8_time_cap); |
| 6699 | if (ret) |
| 6700 | goto out; |
| 6701 | |
| 6702 | max_hibern8_time = max(local_tx_hibern8_time_cap, |
| 6703 | peer_rx_hibern8_time_cap); |
| 6704 | /* make sure proper unit conversion is applied */ |
| 6705 | tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US) |
| 6706 | / PA_HIBERN8_TIME_UNIT_US); |
| 6707 | ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME), |
| 6708 | tuned_pa_hibern8time); |
| 6709 | out: |
| 6710 | return ret; |
| 6711 | } |
| 6712 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6713 | /** |
| 6714 | * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is |
| 6715 | * less than device PA_TACTIVATE time. |
| 6716 | * @hba: per-adapter instance |
| 6717 | * |
| 6718 | * Some UFS devices require host PA_TACTIVATE to be lower than device |
| 6719 | * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk |
| 6720 | * for such devices. |
| 6721 | * |
| 6722 | * Returns zero on success, non-zero error value on failure. |
| 6723 | */ |
| 6724 | static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba) |
| 6725 | { |
| 6726 | int ret = 0; |
| 6727 | u32 granularity, peer_granularity; |
| 6728 | u32 pa_tactivate, peer_pa_tactivate; |
| 6729 | u32 pa_tactivate_us, peer_pa_tactivate_us; |
| 6730 | u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100}; |
| 6731 | |
| 6732 | ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY), |
| 6733 | &granularity); |
| 6734 | if (ret) |
| 6735 | goto out; |
| 6736 | |
| 6737 | ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY), |
| 6738 | &peer_granularity); |
| 6739 | if (ret) |
| 6740 | goto out; |
| 6741 | |
| 6742 | if ((granularity < PA_GRANULARITY_MIN_VAL) || |
| 6743 | (granularity > PA_GRANULARITY_MAX_VAL)) { |
| 6744 | dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d", |
| 6745 | __func__, granularity); |
| 6746 | return -EINVAL; |
| 6747 | } |
| 6748 | |
| 6749 | if ((peer_granularity < PA_GRANULARITY_MIN_VAL) || |
| 6750 | (peer_granularity > PA_GRANULARITY_MAX_VAL)) { |
| 6751 | dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d", |
| 6752 | __func__, peer_granularity); |
| 6753 | return -EINVAL; |
| 6754 | } |
| 6755 | |
| 6756 | ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate); |
| 6757 | if (ret) |
| 6758 | goto out; |
| 6759 | |
| 6760 | ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 6761 | &peer_pa_tactivate); |
| 6762 | if (ret) |
| 6763 | goto out; |
| 6764 | |
| 6765 | pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1]; |
| 6766 | peer_pa_tactivate_us = peer_pa_tactivate * |
| 6767 | gran_to_us_table[peer_granularity - 1]; |
| 6768 | |
| 6769 | if (pa_tactivate_us > peer_pa_tactivate_us) { |
| 6770 | u32 new_peer_pa_tactivate; |
| 6771 | |
| 6772 | new_peer_pa_tactivate = pa_tactivate_us / |
| 6773 | gran_to_us_table[peer_granularity - 1]; |
| 6774 | new_peer_pa_tactivate++; |
| 6775 | ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE), |
| 6776 | new_peer_pa_tactivate); |
| 6777 | } |
| 6778 | |
| 6779 | out: |
| 6780 | return ret; |
| 6781 | } |
| 6782 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 6783 | static void ufshcd_tune_unipro_params(struct ufs_hba *hba) |
| 6784 | { |
| 6785 | if (ufshcd_is_unipro_pa_params_tuning_req(hba)) { |
| 6786 | ufshcd_tune_pa_tactivate(hba); |
| 6787 | ufshcd_tune_pa_hibern8time(hba); |
| 6788 | } |
| 6789 | |
| 6790 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE) |
| 6791 | /* set 1ms timeout for PA_TACTIVATE */ |
| 6792 | ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6793 | |
| 6794 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE) |
| 6795 | ufshcd_quirk_tune_host_pa_tactivate(hba); |
| 6796 | |
| 6797 | ufshcd_vops_apply_dev_quirks(hba); |
| 6798 | } |
| 6799 | |
| 6800 | static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba) |
| 6801 | { |
| 6802 | int err_reg_hist_size = sizeof(struct ufs_uic_err_reg_hist); |
| 6803 | |
| 6804 | hba->ufs_stats.hibern8_exit_cnt = 0; |
| 6805 | hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); |
| 6806 | |
| 6807 | memset(&hba->ufs_stats.pa_err, 0, err_reg_hist_size); |
| 6808 | memset(&hba->ufs_stats.dl_err, 0, err_reg_hist_size); |
| 6809 | memset(&hba->ufs_stats.nl_err, 0, err_reg_hist_size); |
| 6810 | memset(&hba->ufs_stats.tl_err, 0, err_reg_hist_size); |
| 6811 | memset(&hba->ufs_stats.dme_err, 0, err_reg_hist_size); |
| 6812 | |
| 6813 | hba->req_abort_count = 0; |
| 6814 | } |
| 6815 | |
| 6816 | static void ufshcd_apply_pm_quirks(struct ufs_hba *hba) |
| 6817 | { |
| 6818 | if (hba->dev_quirks & UFS_DEVICE_QUIRK_NO_LINK_OFF) { |
| 6819 | if (ufs_get_pm_lvl_to_link_pwr_state(hba->rpm_lvl) == |
| 6820 | UIC_LINK_OFF_STATE) { |
| 6821 | hba->rpm_lvl = |
| 6822 | ufs_get_desired_pm_lvl_for_dev_link_state( |
| 6823 | UFS_SLEEP_PWR_MODE, |
| 6824 | UIC_LINK_HIBERN8_STATE); |
| 6825 | dev_info(hba->dev, "UFS_DEVICE_QUIRK_NO_LINK_OFF enabled, changed rpm_lvl to %d\n", |
| 6826 | hba->rpm_lvl); |
| 6827 | } |
| 6828 | if (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) == |
| 6829 | UIC_LINK_OFF_STATE) { |
| 6830 | hba->spm_lvl = |
| 6831 | ufs_get_desired_pm_lvl_for_dev_link_state( |
| 6832 | UFS_SLEEP_PWR_MODE, |
| 6833 | UIC_LINK_HIBERN8_STATE); |
| 6834 | dev_info(hba->dev, "UFS_DEVICE_QUIRK_NO_LINK_OFF enabled, changed spm_lvl to %d\n", |
| 6835 | hba->spm_lvl); |
| 6836 | } |
| 6837 | } |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 6838 | } |
| 6839 | |
| 6840 | /** |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6841 | * ufshcd_probe_hba - probe hba to detect device and initialize |
| 6842 | * @hba: per-adapter instance |
| 6843 | * |
| 6844 | * Execute link-startup and verify device initialization |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6845 | */ |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6846 | static int ufshcd_probe_hba(struct ufs_hba *hba) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6847 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6848 | int ret; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6849 | ktime_t start = ktime_get(); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 6850 | |
| 6851 | ret = ufshcd_link_startup(hba); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 6852 | if (ret) |
| 6853 | goto out; |
| 6854 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6855 | /* Enable auto hibern8 if supported */ |
| 6856 | if (ufshcd_is_auto_hibern8_supported(hba)) |
| 6857 | ufshcd_set_auto_hibern8_timer(hba, |
| 6858 | hba->hibern8_on_idle.delay_ms); |
Yaniv Gardi | 5064636 | 2014-10-23 13:25:13 +0300 | [diff] [blame] | 6859 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6860 | /* Debug counters initialization */ |
| 6861 | ufshcd_clear_dbg_ufs_stats(hba); |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 6862 | /* set the default level for urgent bkops */ |
| 6863 | hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT; |
| 6864 | hba->is_urgent_bkops_lvl_checked = false; |
| 6865 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6866 | /* UniPro link is active now */ |
| 6867 | ufshcd_set_link_active(hba); |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 6868 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 6869 | ret = ufshcd_verify_dev_init(hba); |
| 6870 | if (ret) |
| 6871 | goto out; |
| 6872 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 6873 | ret = ufshcd_complete_dev_init(hba); |
| 6874 | if (ret) |
| 6875 | goto out; |
| 6876 | |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 6877 | ufs_advertise_fixup_device(hba); |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 6878 | ufshcd_tune_unipro_params(hba); |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 6879 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6880 | ufshcd_apply_pm_quirks(hba); |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 6881 | ret = ufshcd_set_vccq_rail_unused(hba, |
| 6882 | (hba->dev_quirks & UFS_DEVICE_NO_VCCQ) ? true : false); |
| 6883 | if (ret) |
| 6884 | goto out; |
| 6885 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6886 | /* UFS device is also active now */ |
| 6887 | ufshcd_set_ufs_dev_active(hba); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 6888 | ufshcd_force_reset_auto_bkops(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6889 | hba->wlun_dev_clr_ua = true; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6890 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 6891 | if (ufshcd_get_max_pwr_mode(hba)) { |
| 6892 | dev_err(hba->dev, |
| 6893 | "%s: Failed getting max supported power mode\n", |
| 6894 | __func__); |
| 6895 | } else { |
| 6896 | ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6897 | if (ret) { |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 6898 | dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n", |
| 6899 | __func__, ret); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6900 | goto out; |
| 6901 | } |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 6902 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6903 | |
Yaniv Gardi | 53c12d0 | 2016-02-01 15:02:45 +0200 | [diff] [blame] | 6904 | /* set the state as operational after switching to desired gear */ |
| 6905 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6906 | /* |
| 6907 | * If we are in error handling context or in power management callbacks |
| 6908 | * context, no need to scan the host |
| 6909 | */ |
| 6910 | if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) { |
| 6911 | bool flag; |
| 6912 | |
| 6913 | /* clear any previous UFS device information */ |
| 6914 | memset(&hba->dev_info, 0, sizeof(hba->dev_info)); |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 6915 | if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG, |
| 6916 | QUERY_FLAG_IDN_PWR_ON_WPE, &flag)) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6917 | hba->dev_info.f_power_on_wp_en = flag; |
| 6918 | |
Subhash Jadavani | 35732e5 | 2016-12-09 16:09:42 -0800 | [diff] [blame^] | 6919 | ufshcd_init_icc_levels(hba); |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6920 | |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 6921 | /* Add required well known logical units to scsi mid layer */ |
| 6922 | if (ufshcd_scsi_add_wlus(hba)) |
| 6923 | goto out; |
| 6924 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 6925 | scsi_scan_host(hba->host); |
| 6926 | pm_runtime_put_sync(hba->dev); |
| 6927 | } |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 6928 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6929 | /* Resume devfreq after UFS device is detected */ |
| 6930 | if (ufshcd_is_clkscaling_supported(hba)) { |
| 6931 | memcpy(&hba->clk_scaling.saved_pwr_info.info, &hba->pwr_info, |
| 6932 | sizeof(struct ufs_pa_layer_attr)); |
| 6933 | hba->clk_scaling.saved_pwr_info.is_valid = true; |
| 6934 | hba->clk_scaling.is_scaled_up = true; |
| 6935 | ufshcd_resume_clkscaling(hba); |
| 6936 | hba->clk_scaling.is_allowed = true; |
| 6937 | } |
| 6938 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 6939 | out: |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6940 | /* |
| 6941 | * If we failed to initialize the device or the device is not |
| 6942 | * present, turn off the power/clocks etc. |
| 6943 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6944 | if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) { |
| 6945 | pm_runtime_put_sync(hba->dev); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6946 | ufshcd_hba_exit(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 6947 | } |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6948 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6949 | trace_ufshcd_init(dev_name(hba->dev), ret, |
| 6950 | ktime_to_us(ktime_sub(ktime_get(), start)), |
| 6951 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6952 | return ret; |
| 6953 | } |
| 6954 | |
| 6955 | /** |
| 6956 | * ufshcd_async_scan - asynchronous execution for probing hba |
| 6957 | * @data: data pointer to pass to this function |
| 6958 | * @cookie: cookie data |
| 6959 | */ |
| 6960 | static void ufshcd_async_scan(void *data, async_cookie_t cookie) |
| 6961 | { |
| 6962 | struct ufs_hba *hba = (struct ufs_hba *)data; |
| 6963 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6964 | /* |
| 6965 | * Don't allow clock gating and hibern8 enter for faster device |
| 6966 | * detection. |
| 6967 | */ |
| 6968 | ufshcd_hold_all(hba); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 6969 | ufshcd_probe_hba(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 6970 | ufshcd_release_all(hba); |
| 6971 | } |
| 6972 | |
| 6973 | /** |
| 6974 | * ufshcd_query_ioctl - perform user read queries |
| 6975 | * @hba: per-adapter instance |
| 6976 | * @lun: used for lun specific queries |
| 6977 | * @buffer: user space buffer for reading and submitting query data and params |
| 6978 | * @return: 0 for success negative error code otherwise |
| 6979 | * |
| 6980 | * Expected/Submitted buffer structure is struct ufs_ioctl_query_data. |
| 6981 | * It will read the opcode, idn and buf_length parameters, and, put the |
| 6982 | * response in the buffer field while updating the used size in buf_length. |
| 6983 | */ |
| 6984 | static int ufshcd_query_ioctl(struct ufs_hba *hba, u8 lun, void __user *buffer) |
| 6985 | { |
| 6986 | struct ufs_ioctl_query_data *ioctl_data; |
| 6987 | int err = 0; |
| 6988 | int length = 0; |
| 6989 | void *data_ptr; |
| 6990 | bool flag; |
| 6991 | u32 att; |
| 6992 | u8 index; |
| 6993 | u8 *desc = NULL; |
| 6994 | |
| 6995 | ioctl_data = kzalloc(sizeof(struct ufs_ioctl_query_data), GFP_KERNEL); |
| 6996 | if (!ioctl_data) { |
| 6997 | dev_err(hba->dev, "%s: Failed allocating %zu bytes\n", __func__, |
| 6998 | sizeof(struct ufs_ioctl_query_data)); |
| 6999 | err = -ENOMEM; |
| 7000 | goto out; |
| 7001 | } |
| 7002 | |
| 7003 | /* extract params from user buffer */ |
| 7004 | err = copy_from_user(ioctl_data, buffer, |
| 7005 | sizeof(struct ufs_ioctl_query_data)); |
| 7006 | if (err) { |
| 7007 | dev_err(hba->dev, |
| 7008 | "%s: Failed copying buffer from user, err %d\n", |
| 7009 | __func__, err); |
| 7010 | goto out_release_mem; |
| 7011 | } |
| 7012 | |
| 7013 | /* verify legal parameters & send query */ |
| 7014 | switch (ioctl_data->opcode) { |
| 7015 | case UPIU_QUERY_OPCODE_READ_DESC: |
| 7016 | switch (ioctl_data->idn) { |
| 7017 | case QUERY_DESC_IDN_DEVICE: |
| 7018 | case QUERY_DESC_IDN_CONFIGURAION: |
| 7019 | case QUERY_DESC_IDN_INTERCONNECT: |
| 7020 | case QUERY_DESC_IDN_GEOMETRY: |
| 7021 | case QUERY_DESC_IDN_POWER: |
| 7022 | index = 0; |
| 7023 | break; |
| 7024 | case QUERY_DESC_IDN_UNIT: |
| 7025 | if (!ufs_is_valid_unit_desc_lun(lun)) { |
| 7026 | dev_err(hba->dev, |
| 7027 | "%s: No unit descriptor for lun 0x%x\n", |
| 7028 | __func__, lun); |
| 7029 | err = -EINVAL; |
| 7030 | goto out_release_mem; |
| 7031 | } |
| 7032 | index = lun; |
| 7033 | break; |
| 7034 | default: |
| 7035 | goto out_einval; |
| 7036 | } |
| 7037 | length = min_t(int, QUERY_DESC_MAX_SIZE, |
| 7038 | ioctl_data->buf_size); |
| 7039 | desc = kzalloc(length, GFP_KERNEL); |
| 7040 | if (!desc) { |
| 7041 | dev_err(hba->dev, "%s: Failed allocating %d bytes\n", |
| 7042 | __func__, length); |
| 7043 | err = -ENOMEM; |
| 7044 | goto out_release_mem; |
| 7045 | } |
| 7046 | err = ufshcd_query_descriptor(hba, ioctl_data->opcode, |
| 7047 | ioctl_data->idn, index, 0, desc, &length); |
| 7048 | break; |
| 7049 | case UPIU_QUERY_OPCODE_READ_ATTR: |
| 7050 | switch (ioctl_data->idn) { |
| 7051 | case QUERY_ATTR_IDN_BOOT_LU_EN: |
| 7052 | case QUERY_ATTR_IDN_POWER_MODE: |
| 7053 | case QUERY_ATTR_IDN_ACTIVE_ICC_LVL: |
| 7054 | case QUERY_ATTR_IDN_OOO_DATA_EN: |
| 7055 | case QUERY_ATTR_IDN_BKOPS_STATUS: |
| 7056 | case QUERY_ATTR_IDN_PURGE_STATUS: |
| 7057 | case QUERY_ATTR_IDN_MAX_DATA_IN: |
| 7058 | case QUERY_ATTR_IDN_MAX_DATA_OUT: |
| 7059 | case QUERY_ATTR_IDN_REF_CLK_FREQ: |
| 7060 | case QUERY_ATTR_IDN_CONF_DESC_LOCK: |
| 7061 | case QUERY_ATTR_IDN_MAX_NUM_OF_RTT: |
| 7062 | case QUERY_ATTR_IDN_EE_CONTROL: |
| 7063 | case QUERY_ATTR_IDN_EE_STATUS: |
| 7064 | case QUERY_ATTR_IDN_SECONDS_PASSED: |
| 7065 | index = 0; |
| 7066 | break; |
| 7067 | case QUERY_ATTR_IDN_DYN_CAP_NEEDED: |
| 7068 | case QUERY_ATTR_IDN_CORR_PRG_BLK_NUM: |
| 7069 | index = lun; |
| 7070 | break; |
| 7071 | default: |
| 7072 | goto out_einval; |
| 7073 | } |
| 7074 | err = ufshcd_query_attr(hba, ioctl_data->opcode, ioctl_data->idn, |
| 7075 | index, 0, &att); |
| 7076 | break; |
| 7077 | |
| 7078 | case UPIU_QUERY_OPCODE_WRITE_ATTR: |
| 7079 | err = copy_from_user(&att, |
| 7080 | buffer + sizeof(struct ufs_ioctl_query_data), |
| 7081 | sizeof(u32)); |
| 7082 | if (err) { |
| 7083 | dev_err(hba->dev, |
| 7084 | "%s: Failed copying buffer from user, err %d\n", |
| 7085 | __func__, err); |
| 7086 | goto out_release_mem; |
| 7087 | } |
| 7088 | |
| 7089 | switch (ioctl_data->idn) { |
| 7090 | case QUERY_ATTR_IDN_BOOT_LU_EN: |
| 7091 | index = 0; |
| 7092 | if (att > QUERY_ATTR_IDN_BOOT_LU_EN_MAX) { |
| 7093 | dev_err(hba->dev, |
| 7094 | "%s: Illegal ufs query ioctl data, opcode 0x%x, idn 0x%x, att 0x%x\n", |
| 7095 | __func__, ioctl_data->opcode, |
| 7096 | (unsigned int)ioctl_data->idn, att); |
| 7097 | err = -EINVAL; |
| 7098 | goto out_release_mem; |
| 7099 | } |
| 7100 | break; |
| 7101 | default: |
| 7102 | goto out_einval; |
| 7103 | } |
| 7104 | err = ufshcd_query_attr(hba, ioctl_data->opcode, |
| 7105 | ioctl_data->idn, index, 0, &att); |
| 7106 | break; |
| 7107 | |
| 7108 | case UPIU_QUERY_OPCODE_READ_FLAG: |
| 7109 | switch (ioctl_data->idn) { |
| 7110 | case QUERY_FLAG_IDN_FDEVICEINIT: |
| 7111 | case QUERY_FLAG_IDN_PERMANENT_WPE: |
| 7112 | case QUERY_FLAG_IDN_PWR_ON_WPE: |
| 7113 | case QUERY_FLAG_IDN_BKOPS_EN: |
| 7114 | case QUERY_FLAG_IDN_PURGE_ENABLE: |
| 7115 | case QUERY_FLAG_IDN_FPHYRESOURCEREMOVAL: |
| 7116 | case QUERY_FLAG_IDN_BUSY_RTC: |
| 7117 | break; |
| 7118 | default: |
| 7119 | goto out_einval; |
| 7120 | } |
| 7121 | err = ufshcd_query_flag_retry(hba, ioctl_data->opcode, |
| 7122 | ioctl_data->idn, &flag); |
| 7123 | break; |
| 7124 | default: |
| 7125 | goto out_einval; |
| 7126 | } |
| 7127 | |
| 7128 | if (err) { |
| 7129 | dev_err(hba->dev, "%s: Query for idn %d failed\n", __func__, |
| 7130 | ioctl_data->idn); |
| 7131 | goto out_release_mem; |
| 7132 | } |
| 7133 | |
| 7134 | /* |
| 7135 | * copy response data |
| 7136 | * As we might end up reading less data then what is specified in |
| 7137 | * "ioctl_data->buf_size". So we are updating "ioctl_data-> |
| 7138 | * buf_size" to what exactly we have read. |
| 7139 | */ |
| 7140 | switch (ioctl_data->opcode) { |
| 7141 | case UPIU_QUERY_OPCODE_READ_DESC: |
| 7142 | ioctl_data->buf_size = min_t(int, ioctl_data->buf_size, length); |
| 7143 | data_ptr = desc; |
| 7144 | break; |
| 7145 | case UPIU_QUERY_OPCODE_READ_ATTR: |
| 7146 | ioctl_data->buf_size = sizeof(u32); |
| 7147 | data_ptr = &att; |
| 7148 | break; |
| 7149 | case UPIU_QUERY_OPCODE_READ_FLAG: |
| 7150 | ioctl_data->buf_size = 1; |
| 7151 | data_ptr = &flag; |
| 7152 | break; |
| 7153 | case UPIU_QUERY_OPCODE_WRITE_ATTR: |
| 7154 | goto out_release_mem; |
| 7155 | default: |
| 7156 | goto out_einval; |
| 7157 | } |
| 7158 | |
| 7159 | /* copy to user */ |
| 7160 | err = copy_to_user(buffer, ioctl_data, |
| 7161 | sizeof(struct ufs_ioctl_query_data)); |
| 7162 | if (err) |
| 7163 | dev_err(hba->dev, "%s: Failed copying back to user.\n", |
| 7164 | __func__); |
| 7165 | err = copy_to_user(buffer + sizeof(struct ufs_ioctl_query_data), |
| 7166 | data_ptr, ioctl_data->buf_size); |
| 7167 | if (err) |
| 7168 | dev_err(hba->dev, "%s: err %d copying back to user.\n", |
| 7169 | __func__, err); |
| 7170 | goto out_release_mem; |
| 7171 | |
| 7172 | out_einval: |
| 7173 | dev_err(hba->dev, |
| 7174 | "%s: illegal ufs query ioctl data, opcode 0x%x, idn 0x%x\n", |
| 7175 | __func__, ioctl_data->opcode, (unsigned int)ioctl_data->idn); |
| 7176 | err = -EINVAL; |
| 7177 | out_release_mem: |
| 7178 | kfree(ioctl_data); |
| 7179 | kfree(desc); |
| 7180 | out: |
| 7181 | return err; |
| 7182 | } |
| 7183 | |
| 7184 | /** |
| 7185 | * ufshcd_ioctl - ufs ioctl callback registered in scsi_host |
| 7186 | * @dev: scsi device required for per LUN queries |
| 7187 | * @cmd: command opcode |
| 7188 | * @buffer: user space buffer for transferring data |
| 7189 | * |
| 7190 | * Supported commands: |
| 7191 | * UFS_IOCTL_QUERY |
| 7192 | */ |
| 7193 | static int ufshcd_ioctl(struct scsi_device *dev, int cmd, void __user *buffer) |
| 7194 | { |
| 7195 | struct ufs_hba *hba = shost_priv(dev->host); |
| 7196 | int err = 0; |
| 7197 | |
| 7198 | BUG_ON(!hba); |
| 7199 | if (!buffer) { |
| 7200 | dev_err(hba->dev, "%s: User buffer is NULL!\n", __func__); |
| 7201 | return -EINVAL; |
| 7202 | } |
| 7203 | |
| 7204 | switch (cmd) { |
| 7205 | case UFS_IOCTL_QUERY: |
| 7206 | pm_runtime_get_sync(hba->dev); |
| 7207 | err = ufshcd_query_ioctl(hba, ufshcd_scsi_to_upiu_lun(dev->lun), |
| 7208 | buffer); |
| 7209 | pm_runtime_put_sync(hba->dev); |
| 7210 | break; |
| 7211 | default: |
| 7212 | err = -ENOIOCTLCMD; |
| 7213 | dev_dbg(hba->dev, "%s: Unsupported ioctl cmd %d\n", __func__, |
| 7214 | cmd); |
| 7215 | break; |
| 7216 | } |
| 7217 | |
| 7218 | return err; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 7219 | } |
| 7220 | |
Yaniv Gardi | f550c65 | 2016-03-10 17:37:07 +0200 | [diff] [blame] | 7221 | static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd) |
| 7222 | { |
| 7223 | unsigned long flags; |
| 7224 | struct Scsi_Host *host; |
| 7225 | struct ufs_hba *hba; |
| 7226 | int index; |
| 7227 | bool found = false; |
| 7228 | |
| 7229 | if (!scmd || !scmd->device || !scmd->device->host) |
| 7230 | return BLK_EH_NOT_HANDLED; |
| 7231 | |
| 7232 | host = scmd->device->host; |
| 7233 | hba = shost_priv(host); |
| 7234 | if (!hba) |
| 7235 | return BLK_EH_NOT_HANDLED; |
| 7236 | |
| 7237 | spin_lock_irqsave(host->host_lock, flags); |
| 7238 | |
| 7239 | for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) { |
| 7240 | if (hba->lrb[index].cmd == scmd) { |
| 7241 | found = true; |
| 7242 | break; |
| 7243 | } |
| 7244 | } |
| 7245 | |
| 7246 | spin_unlock_irqrestore(host->host_lock, flags); |
| 7247 | |
| 7248 | /* |
| 7249 | * Bypass SCSI error handling and reset the block layer timer if this |
| 7250 | * SCSI command was not actually dispatched to UFS driver, otherwise |
| 7251 | * let SCSI layer handle the error as usual. |
| 7252 | */ |
| 7253 | return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER; |
| 7254 | } |
| 7255 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7256 | static struct scsi_host_template ufshcd_driver_template = { |
| 7257 | .module = THIS_MODULE, |
| 7258 | .name = UFSHCD, |
| 7259 | .proc_name = UFSHCD, |
| 7260 | .queuecommand = ufshcd_queuecommand, |
| 7261 | .slave_alloc = ufshcd_slave_alloc, |
Akinobu Mita | eeda474 | 2014-07-01 23:00:32 +0900 | [diff] [blame] | 7262 | .slave_configure = ufshcd_slave_configure, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7263 | .slave_destroy = ufshcd_slave_destroy, |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 7264 | .change_queue_depth = ufshcd_change_queue_depth, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7265 | .eh_abort_handler = ufshcd_abort, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 7266 | .eh_device_reset_handler = ufshcd_eh_device_reset_handler, |
| 7267 | .eh_host_reset_handler = ufshcd_eh_host_reset_handler, |
Yaniv Gardi | f550c65 | 2016-03-10 17:37:07 +0200 | [diff] [blame] | 7268 | .eh_timed_out = ufshcd_eh_timed_out, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7269 | .ioctl = ufshcd_ioctl, |
| 7270 | #ifdef CONFIG_COMPAT |
| 7271 | .compat_ioctl = ufshcd_ioctl, |
| 7272 | #endif |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7273 | .this_id = -1, |
| 7274 | .sg_tablesize = SG_ALL, |
| 7275 | .cmd_per_lun = UFSHCD_CMD_PER_LUN, |
| 7276 | .can_queue = UFSHCD_CAN_QUEUE, |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7277 | .max_host_blocked = 1, |
Christoph Hellwig | c40ecc1 | 2014-11-13 14:25:11 +0100 | [diff] [blame] | 7278 | .track_queue_depth = 1, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7279 | }; |
| 7280 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7281 | static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg, |
| 7282 | int ua) |
| 7283 | { |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 7284 | int ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7285 | |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 7286 | if (!vreg) |
| 7287 | return 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7288 | |
Bjorn Andersson | 7b16a07 | 2015-02-11 19:35:28 -0800 | [diff] [blame] | 7289 | ret = regulator_set_load(vreg->reg, ua); |
| 7290 | if (ret < 0) { |
| 7291 | dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n", |
| 7292 | __func__, vreg->name, ua, ret); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7293 | } |
| 7294 | |
| 7295 | return ret; |
| 7296 | } |
| 7297 | |
| 7298 | static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba, |
| 7299 | struct ufs_vreg *vreg) |
| 7300 | { |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 7301 | if (!vreg) |
| 7302 | return 0; |
| 7303 | else if (vreg->unused) |
| 7304 | return 0; |
| 7305 | else |
| 7306 | return ufshcd_config_vreg_load(hba->dev, vreg, |
| 7307 | UFS_VREG_LPM_LOAD_UA); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7308 | } |
| 7309 | |
| 7310 | static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba, |
| 7311 | struct ufs_vreg *vreg) |
| 7312 | { |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 7313 | if (!vreg) |
| 7314 | return 0; |
| 7315 | else if (vreg->unused) |
| 7316 | return 0; |
| 7317 | else |
| 7318 | return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7319 | } |
| 7320 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7321 | static int ufshcd_config_vreg(struct device *dev, |
| 7322 | struct ufs_vreg *vreg, bool on) |
| 7323 | { |
| 7324 | int ret = 0; |
| 7325 | struct regulator *reg = vreg->reg; |
| 7326 | const char *name = vreg->name; |
| 7327 | int min_uV, uA_load; |
| 7328 | |
| 7329 | BUG_ON(!vreg); |
| 7330 | |
| 7331 | if (regulator_count_voltages(reg) > 0) { |
| 7332 | min_uV = on ? vreg->min_uV : 0; |
| 7333 | ret = regulator_set_voltage(reg, min_uV, vreg->max_uV); |
| 7334 | if (ret) { |
| 7335 | dev_err(dev, "%s: %s set voltage failed, err=%d\n", |
| 7336 | __func__, name, ret); |
| 7337 | goto out; |
| 7338 | } |
| 7339 | |
| 7340 | uA_load = on ? vreg->max_uA : 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7341 | ret = ufshcd_config_vreg_load(dev, vreg, uA_load); |
| 7342 | if (ret) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7343 | goto out; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7344 | } |
| 7345 | out: |
| 7346 | return ret; |
| 7347 | } |
| 7348 | |
| 7349 | static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 7350 | { |
| 7351 | int ret = 0; |
| 7352 | |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 7353 | if (!vreg) |
| 7354 | goto out; |
| 7355 | else if (vreg->enabled || vreg->unused) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7356 | goto out; |
| 7357 | |
| 7358 | ret = ufshcd_config_vreg(dev, vreg, true); |
| 7359 | if (!ret) |
| 7360 | ret = regulator_enable(vreg->reg); |
| 7361 | |
| 7362 | if (!ret) |
| 7363 | vreg->enabled = true; |
| 7364 | else |
| 7365 | dev_err(dev, "%s: %s enable failed, err=%d\n", |
| 7366 | __func__, vreg->name, ret); |
| 7367 | out: |
| 7368 | return ret; |
| 7369 | } |
| 7370 | |
| 7371 | static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 7372 | { |
| 7373 | int ret = 0; |
| 7374 | |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 7375 | if (!vreg) |
| 7376 | goto out; |
| 7377 | else if (!vreg->enabled || vreg->unused) |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7378 | goto out; |
| 7379 | |
| 7380 | ret = regulator_disable(vreg->reg); |
| 7381 | |
| 7382 | if (!ret) { |
| 7383 | /* ignore errors on applying disable config */ |
| 7384 | ufshcd_config_vreg(dev, vreg, false); |
| 7385 | vreg->enabled = false; |
| 7386 | } else { |
| 7387 | dev_err(dev, "%s: %s disable failed, err=%d\n", |
| 7388 | __func__, vreg->name, ret); |
| 7389 | } |
| 7390 | out: |
| 7391 | return ret; |
| 7392 | } |
| 7393 | |
| 7394 | static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on) |
| 7395 | { |
| 7396 | int ret = 0; |
| 7397 | struct device *dev = hba->dev; |
| 7398 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 7399 | |
| 7400 | if (!info) |
| 7401 | goto out; |
| 7402 | |
| 7403 | ret = ufshcd_toggle_vreg(dev, info->vcc, on); |
| 7404 | if (ret) |
| 7405 | goto out; |
| 7406 | |
| 7407 | ret = ufshcd_toggle_vreg(dev, info->vccq, on); |
| 7408 | if (ret) |
| 7409 | goto out; |
| 7410 | |
| 7411 | ret = ufshcd_toggle_vreg(dev, info->vccq2, on); |
| 7412 | if (ret) |
| 7413 | goto out; |
| 7414 | |
| 7415 | out: |
| 7416 | if (ret) { |
| 7417 | ufshcd_toggle_vreg(dev, info->vccq2, false); |
| 7418 | ufshcd_toggle_vreg(dev, info->vccq, false); |
| 7419 | ufshcd_toggle_vreg(dev, info->vcc, false); |
| 7420 | } |
| 7421 | return ret; |
| 7422 | } |
| 7423 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7424 | static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on) |
| 7425 | { |
| 7426 | struct ufs_vreg_info *info = &hba->vreg_info; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7427 | int ret = 0; |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7428 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7429 | if (info->vdd_hba) { |
| 7430 | ret = ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on); |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7431 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7432 | if (!ret) |
| 7433 | ufshcd_vops_update_sec_cfg(hba, on); |
| 7434 | } |
| 7435 | |
| 7436 | return ret; |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7437 | } |
| 7438 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7439 | static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg) |
| 7440 | { |
| 7441 | int ret = 0; |
| 7442 | |
| 7443 | if (!vreg) |
| 7444 | goto out; |
| 7445 | |
| 7446 | vreg->reg = devm_regulator_get(dev, vreg->name); |
| 7447 | if (IS_ERR(vreg->reg)) { |
| 7448 | ret = PTR_ERR(vreg->reg); |
| 7449 | dev_err(dev, "%s: %s get failed, err=%d\n", |
| 7450 | __func__, vreg->name, ret); |
| 7451 | } |
| 7452 | out: |
| 7453 | return ret; |
| 7454 | } |
| 7455 | |
| 7456 | static int ufshcd_init_vreg(struct ufs_hba *hba) |
| 7457 | { |
| 7458 | int ret = 0; |
| 7459 | struct device *dev = hba->dev; |
| 7460 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 7461 | |
| 7462 | if (!info) |
| 7463 | goto out; |
| 7464 | |
| 7465 | ret = ufshcd_get_vreg(dev, info->vcc); |
| 7466 | if (ret) |
| 7467 | goto out; |
| 7468 | |
| 7469 | ret = ufshcd_get_vreg(dev, info->vccq); |
| 7470 | if (ret) |
| 7471 | goto out; |
| 7472 | |
| 7473 | ret = ufshcd_get_vreg(dev, info->vccq2); |
| 7474 | out: |
| 7475 | return ret; |
| 7476 | } |
| 7477 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7478 | static int ufshcd_init_hba_vreg(struct ufs_hba *hba) |
| 7479 | { |
| 7480 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 7481 | |
| 7482 | if (info) |
| 7483 | return ufshcd_get_vreg(hba->dev, info->vdd_hba); |
| 7484 | |
| 7485 | return 0; |
| 7486 | } |
| 7487 | |
Yaniv Gardi | 60f0187 | 2016-03-10 17:37:11 +0200 | [diff] [blame] | 7488 | static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused) |
| 7489 | { |
| 7490 | int ret = 0; |
| 7491 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 7492 | |
| 7493 | if (!info) |
| 7494 | goto out; |
| 7495 | else if (!info->vccq) |
| 7496 | goto out; |
| 7497 | |
| 7498 | if (unused) { |
| 7499 | /* shut off the rail here */ |
| 7500 | ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false); |
| 7501 | /* |
| 7502 | * Mark this rail as no longer used, so it doesn't get enabled |
| 7503 | * later by mistake |
| 7504 | */ |
| 7505 | if (!ret) |
| 7506 | info->vccq->unused = true; |
| 7507 | } else { |
| 7508 | /* |
| 7509 | * rail should have been already enabled hence just make sure |
| 7510 | * that unused flag is cleared. |
| 7511 | */ |
| 7512 | info->vccq->unused = false; |
| 7513 | } |
| 7514 | out: |
| 7515 | return ret; |
| 7516 | } |
| 7517 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7518 | static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on, |
| 7519 | bool skip_ref_clk, bool is_gating_context) |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7520 | { |
| 7521 | int ret = 0; |
| 7522 | struct ufs_clk_info *clki; |
| 7523 | struct list_head *head = &hba->clk_list_head; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7524 | unsigned long flags; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7525 | ktime_t start = ktime_get(); |
| 7526 | bool clk_state_changed = false; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7527 | |
| 7528 | if (!head || list_empty(head)) |
| 7529 | goto out; |
| 7530 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7531 | /* |
| 7532 | * vendor specific setup_clocks ops may depend on clocks managed by |
| 7533 | * this standard driver hence call the vendor specific setup_clocks |
| 7534 | * before disabling the clocks managed here. |
| 7535 | */ |
| 7536 | if (!on) { |
| 7537 | ret = ufshcd_vops_setup_clocks(hba, on, is_gating_context); |
| 7538 | if (ret) |
| 7539 | return ret; |
| 7540 | } |
| 7541 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7542 | list_for_each_entry(clki, head, list) { |
| 7543 | if (!IS_ERR_OR_NULL(clki->clk)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7544 | if (skip_ref_clk && !strcmp(clki->name, "ref_clk")) |
| 7545 | continue; |
| 7546 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7547 | clk_state_changed = on ^ clki->enabled; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7548 | if (on && !clki->enabled) { |
| 7549 | ret = clk_prepare_enable(clki->clk); |
| 7550 | if (ret) { |
| 7551 | dev_err(hba->dev, "%s: %s prepare enable failed, %d\n", |
| 7552 | __func__, clki->name, ret); |
| 7553 | goto out; |
| 7554 | } |
| 7555 | } else if (!on && clki->enabled) { |
| 7556 | clk_disable_unprepare(clki->clk); |
| 7557 | } |
| 7558 | clki->enabled = on; |
| 7559 | dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__, |
| 7560 | clki->name, on ? "en" : "dis"); |
| 7561 | } |
| 7562 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7563 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7564 | /* |
| 7565 | * vendor specific setup_clocks ops may depend on clocks managed by |
| 7566 | * this standard driver hence call the vendor specific setup_clocks |
| 7567 | * after enabling the clocks managed here. |
| 7568 | */ |
| 7569 | if (on) |
| 7570 | ret = ufshcd_vops_setup_clocks(hba, on, is_gating_context); |
| 7571 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7572 | out: |
| 7573 | if (ret) { |
| 7574 | list_for_each_entry(clki, head, list) { |
| 7575 | if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled) |
| 7576 | clk_disable_unprepare(clki->clk); |
| 7577 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7578 | } else if (!ret && on) { |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7579 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 7580 | hba->clk_gating.state = CLKS_ON; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7581 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 7582 | hba->clk_gating.state); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 7583 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7584 | /* restore the secure configuration as clocks are enabled */ |
| 7585 | ufshcd_vops_update_sec_cfg(hba, true); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7586 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7587 | |
| 7588 | if (clk_state_changed) |
| 7589 | trace_ufshcd_profile_clk_gating(dev_name(hba->dev), |
| 7590 | (on ? "on" : "off"), |
| 7591 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7592 | return ret; |
| 7593 | } |
| 7594 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7595 | static int ufshcd_enable_clocks(struct ufs_hba *hba) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7596 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7597 | return ufshcd_setup_clocks(hba, true, false, false); |
| 7598 | } |
| 7599 | |
| 7600 | static int ufshcd_disable_clocks(struct ufs_hba *hba, |
| 7601 | bool is_gating_context) |
| 7602 | { |
| 7603 | return ufshcd_setup_clocks(hba, false, false, is_gating_context); |
| 7604 | } |
| 7605 | |
| 7606 | static int ufshcd_disable_clocks_skip_ref_clk(struct ufs_hba *hba, |
| 7607 | bool is_gating_context) |
| 7608 | { |
| 7609 | return ufshcd_setup_clocks(hba, false, true, is_gating_context); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7610 | } |
| 7611 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7612 | static int ufshcd_init_clocks(struct ufs_hba *hba) |
| 7613 | { |
| 7614 | int ret = 0; |
| 7615 | struct ufs_clk_info *clki; |
| 7616 | struct device *dev = hba->dev; |
| 7617 | struct list_head *head = &hba->clk_list_head; |
| 7618 | |
| 7619 | if (!head || list_empty(head)) |
| 7620 | goto out; |
| 7621 | |
| 7622 | list_for_each_entry(clki, head, list) { |
| 7623 | if (!clki->name) |
| 7624 | continue; |
| 7625 | |
| 7626 | clki->clk = devm_clk_get(dev, clki->name); |
| 7627 | if (IS_ERR(clki->clk)) { |
| 7628 | ret = PTR_ERR(clki->clk); |
| 7629 | dev_err(dev, "%s: %s clk get failed, %d\n", |
| 7630 | __func__, clki->name, ret); |
| 7631 | goto out; |
| 7632 | } |
| 7633 | |
| 7634 | if (clki->max_freq) { |
| 7635 | ret = clk_set_rate(clki->clk, clki->max_freq); |
| 7636 | if (ret) { |
| 7637 | dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", |
| 7638 | __func__, clki->name, |
| 7639 | clki->max_freq, ret); |
| 7640 | goto out; |
| 7641 | } |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 7642 | clki->curr_freq = clki->max_freq; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7643 | } |
| 7644 | dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__, |
| 7645 | clki->name, clk_get_rate(clki->clk)); |
| 7646 | } |
| 7647 | out: |
| 7648 | return ret; |
| 7649 | } |
| 7650 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7651 | static int ufshcd_variant_hba_init(struct ufs_hba *hba) |
| 7652 | { |
| 7653 | int err = 0; |
| 7654 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7655 | if (!hba->var || !hba->var->vops) |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7656 | goto out; |
| 7657 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7658 | err = ufshcd_vops_init(hba); |
| 7659 | if (err) |
| 7660 | goto out; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7661 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7662 | err = ufshcd_vops_setup_regulators(hba, true); |
| 7663 | if (err) |
| 7664 | goto out_exit; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7665 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7666 | goto out; |
| 7667 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7668 | out_exit: |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7669 | ufshcd_vops_exit(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7670 | out: |
| 7671 | if (err) |
| 7672 | dev_err(hba->dev, "%s: variant %s init failed err %d\n", |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7673 | __func__, ufshcd_get_var_name(hba), err); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7674 | return err; |
| 7675 | } |
| 7676 | |
| 7677 | static void ufshcd_variant_hba_exit(struct ufs_hba *hba) |
| 7678 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7679 | if (!hba->var || !hba->var->vops) |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7680 | return; |
| 7681 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7682 | ufshcd_vops_setup_regulators(hba, false); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7683 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 7684 | ufshcd_vops_exit(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 7685 | } |
| 7686 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7687 | static int ufshcd_hba_init(struct ufs_hba *hba) |
| 7688 | { |
| 7689 | int err; |
| 7690 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7691 | /* |
| 7692 | * Handle host controller power separately from the UFS device power |
| 7693 | * rails as it will help controlling the UFS host controller power |
| 7694 | * collapse easily which is different than UFS device power collapse. |
| 7695 | * Also, enable the host controller power before we go ahead with rest |
| 7696 | * of the initialization here. |
| 7697 | */ |
| 7698 | err = ufshcd_init_hba_vreg(hba); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7699 | if (err) |
| 7700 | goto out; |
| 7701 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7702 | err = ufshcd_setup_hba_vreg(hba, true); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7703 | if (err) |
| 7704 | goto out; |
| 7705 | |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7706 | err = ufshcd_init_clocks(hba); |
| 7707 | if (err) |
| 7708 | goto out_disable_hba_vreg; |
| 7709 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7710 | err = ufshcd_enable_clocks(hba); |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7711 | if (err) |
| 7712 | goto out_disable_hba_vreg; |
| 7713 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7714 | err = ufshcd_init_vreg(hba); |
| 7715 | if (err) |
| 7716 | goto out_disable_clks; |
| 7717 | |
| 7718 | err = ufshcd_setup_vreg(hba, true); |
| 7719 | if (err) |
| 7720 | goto out_disable_clks; |
| 7721 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7722 | err = ufshcd_variant_hba_init(hba); |
| 7723 | if (err) |
| 7724 | goto out_disable_vreg; |
| 7725 | |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7726 | hba->is_powered = true; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7727 | goto out; |
| 7728 | |
| 7729 | out_disable_vreg: |
| 7730 | ufshcd_setup_vreg(hba, false); |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 7731 | out_disable_clks: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7732 | ufshcd_disable_clocks(hba, false); |
Raviv Shvili | 6a771a6 | 2014-09-25 15:32:24 +0300 | [diff] [blame] | 7733 | out_disable_hba_vreg: |
| 7734 | ufshcd_setup_hba_vreg(hba, false); |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7735 | out: |
| 7736 | return err; |
| 7737 | } |
| 7738 | |
| 7739 | static void ufshcd_hba_exit(struct ufs_hba *hba) |
| 7740 | { |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7741 | if (hba->is_powered) { |
| 7742 | ufshcd_variant_hba_exit(hba); |
| 7743 | ufshcd_setup_vreg(hba, false); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7744 | if (ufshcd_is_clkscaling_supported(hba)) { |
| 7745 | ufshcd_suspend_clkscaling(hba); |
| 7746 | destroy_workqueue(hba->clk_scaling.workq); |
| 7747 | } |
| 7748 | ufshcd_disable_clocks(hba, false); |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 7749 | ufshcd_setup_hba_vreg(hba, false); |
| 7750 | hba->is_powered = false; |
| 7751 | } |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 7752 | } |
| 7753 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7754 | static int |
| 7755 | ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7756 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7757 | unsigned char cmd[6] = {REQUEST_SENSE, |
| 7758 | 0, |
| 7759 | 0, |
| 7760 | 0, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7761 | UFSHCD_REQ_SENSE_SIZE, |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7762 | 0}; |
| 7763 | char *buffer; |
| 7764 | int ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7765 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7766 | buffer = kzalloc(UFSHCD_REQ_SENSE_SIZE, GFP_KERNEL); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7767 | if (!buffer) { |
| 7768 | ret = -ENOMEM; |
| 7769 | goto out; |
| 7770 | } |
| 7771 | |
| 7772 | ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7773 | UFSHCD_REQ_SENSE_SIZE, NULL, |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7774 | msecs_to_jiffies(1000), 3, NULL, REQ_PM); |
| 7775 | if (ret) |
| 7776 | pr_err("%s: failed with err %d\n", __func__, ret); |
| 7777 | |
| 7778 | kfree(buffer); |
| 7779 | out: |
| 7780 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7781 | } |
| 7782 | |
| 7783 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7784 | * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device |
| 7785 | * power mode |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7786 | * @hba: per adapter instance |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7787 | * @pwr_mode: device power mode to set |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7788 | * |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7789 | * Returns 0 if requested power mode is set successfully |
| 7790 | * Returns non-zero if failed to set the requested power mode |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7791 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7792 | static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba, |
| 7793 | enum ufs_dev_pwr_mode pwr_mode) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7794 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7795 | unsigned char cmd[6] = { START_STOP }; |
| 7796 | struct scsi_sense_hdr sshdr; |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 7797 | struct scsi_device *sdp; |
| 7798 | unsigned long flags; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7799 | int ret; |
| 7800 | |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 7801 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 7802 | sdp = hba->sdev_ufs_device; |
| 7803 | if (sdp) { |
| 7804 | ret = scsi_device_get(sdp); |
| 7805 | if (!ret && !scsi_device_online(sdp)) { |
| 7806 | ret = -ENODEV; |
| 7807 | scsi_device_put(sdp); |
| 7808 | } |
| 7809 | } else { |
| 7810 | ret = -ENODEV; |
| 7811 | } |
| 7812 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 7813 | |
| 7814 | if (ret) |
| 7815 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7816 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7817 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7818 | * If scsi commands fail, the scsi mid-layer schedules scsi error- |
| 7819 | * handling, which would wait for host to be resumed. Since we know |
| 7820 | * we are functional while we are here, skip host resume in error |
| 7821 | * handling context. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7822 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7823 | hba->host->eh_noresume = 1; |
| 7824 | if (hba->wlun_dev_clr_ua) { |
| 7825 | ret = ufshcd_send_request_sense(hba, sdp); |
| 7826 | if (ret) |
| 7827 | goto out; |
| 7828 | /* Unit attention condition is cleared now */ |
| 7829 | hba->wlun_dev_clr_ua = false; |
| 7830 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7831 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7832 | cmd[4] = pwr_mode << 4; |
| 7833 | |
| 7834 | /* |
| 7835 | * Current function would be generally called from the power management |
| 7836 | * callbacks hence set the REQ_PM flag so that it doesn't resume the |
| 7837 | * already suspended childs. |
| 7838 | */ |
| 7839 | ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr, |
| 7840 | START_STOP_TIMEOUT, 0, NULL, REQ_PM); |
| 7841 | if (ret) { |
| 7842 | sdev_printk(KERN_WARNING, sdp, |
Hannes Reinecke | ef61329 | 2014-10-24 14:27:00 +0200 | [diff] [blame] | 7843 | "START_STOP failed for power mode: %d, result %x\n", |
| 7844 | pwr_mode, ret); |
Hannes Reinecke | 2104551 | 2015-01-08 07:43:46 +0100 | [diff] [blame] | 7845 | if (driver_byte(ret) & DRIVER_SENSE) |
| 7846 | scsi_print_sense_hdr(sdp, NULL, &sshdr); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7847 | } |
| 7848 | |
| 7849 | if (!ret) |
| 7850 | hba->curr_dev_pwr_mode = pwr_mode; |
| 7851 | out: |
Akinobu Mita | 7c48bfd | 2014-10-23 13:25:12 +0300 | [diff] [blame] | 7852 | scsi_device_put(sdp); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7853 | hba->host->eh_noresume = 0; |
| 7854 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 7855 | } |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7856 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7857 | static int ufshcd_link_state_transition(struct ufs_hba *hba, |
| 7858 | enum uic_link_state req_link_state, |
| 7859 | int check_for_bkops) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 7860 | { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7861 | int ret = 0; |
| 7862 | |
| 7863 | if (req_link_state == hba->uic_link_state) |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 7864 | return 0; |
| 7865 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7866 | if (req_link_state == UIC_LINK_HIBERN8_STATE) { |
| 7867 | ret = ufshcd_uic_hibern8_enter(hba); |
| 7868 | if (!ret) |
| 7869 | ufshcd_set_link_hibern8(hba); |
| 7870 | else |
| 7871 | goto out; |
| 7872 | } |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 7873 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7874 | * If autobkops is enabled, link can't be turned off because |
| 7875 | * turning off the link would also turn off the device. |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 7876 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7877 | else if ((req_link_state == UIC_LINK_OFF_STATE) && |
| 7878 | (!check_for_bkops || (check_for_bkops && |
| 7879 | !hba->auto_bkops_enabled))) { |
| 7880 | /* |
Yaniv Gardi | f3099fb | 2016-03-10 17:37:17 +0200 | [diff] [blame] | 7881 | * Let's make sure that link is in low power mode, we are doing |
| 7882 | * this currently by putting the link in Hibern8. Otherway to |
| 7883 | * put the link in low power mode is to send the DME end point |
| 7884 | * to device and then send the DME reset command to local |
| 7885 | * unipro. But putting the link in hibern8 is much faster. |
| 7886 | */ |
| 7887 | ret = ufshcd_uic_hibern8_enter(hba); |
| 7888 | if (ret) |
| 7889 | goto out; |
| 7890 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7891 | * Change controller state to "reset state" which |
| 7892 | * should also put the link in off/reset state |
| 7893 | */ |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 7894 | ufshcd_hba_stop(hba, true); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7895 | /* |
| 7896 | * TODO: Check if we need any delay to make sure that |
| 7897 | * controller is reset |
| 7898 | */ |
| 7899 | ufshcd_set_link_off(hba); |
| 7900 | } |
| 7901 | |
| 7902 | out: |
| 7903 | return ret; |
| 7904 | } |
| 7905 | |
| 7906 | static void ufshcd_vreg_set_lpm(struct ufs_hba *hba) |
| 7907 | { |
| 7908 | /* |
Yaniv Gardi | b799fdf | 2016-03-10 17:37:18 +0200 | [diff] [blame] | 7909 | * It seems some UFS devices may keep drawing more than sleep current |
| 7910 | * (atleast for 500us) from UFS rails (especially from VCCQ rail). |
| 7911 | * To avoid this situation, add 2ms delay before putting these UFS |
| 7912 | * rails in LPM mode. |
| 7913 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7914 | if (!ufshcd_is_link_active(hba)) |
Yaniv Gardi | b799fdf | 2016-03-10 17:37:18 +0200 | [diff] [blame] | 7915 | usleep_range(2000, 2100); |
| 7916 | |
| 7917 | /* |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7918 | * If UFS device is either in UFS_Sleep turn off VCC rail to save some |
| 7919 | * power. |
| 7920 | * |
| 7921 | * If UFS device and link is in OFF state, all power supplies (VCC, |
| 7922 | * VCCQ, VCCQ2) can be turned off if power on write protect is not |
| 7923 | * required. If UFS link is inactive (Hibern8 or OFF state) and device |
| 7924 | * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode. |
| 7925 | * |
| 7926 | * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway |
| 7927 | * in low power state which would save some power. |
| 7928 | */ |
| 7929 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) && |
| 7930 | !hba->dev_info.is_lu_power_on_wp) { |
| 7931 | ufshcd_setup_vreg(hba, false); |
| 7932 | } else if (!ufshcd_is_ufs_dev_active(hba)) { |
| 7933 | ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false); |
| 7934 | if (!ufshcd_is_link_active(hba)) { |
| 7935 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq); |
| 7936 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2); |
| 7937 | } |
| 7938 | } |
| 7939 | } |
| 7940 | |
| 7941 | static int ufshcd_vreg_set_hpm(struct ufs_hba *hba) |
| 7942 | { |
| 7943 | int ret = 0; |
| 7944 | |
| 7945 | if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) && |
| 7946 | !hba->dev_info.is_lu_power_on_wp) { |
| 7947 | ret = ufshcd_setup_vreg(hba, true); |
| 7948 | } else if (!ufshcd_is_ufs_dev_active(hba)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7949 | if (!ret && !ufshcd_is_link_active(hba)) { |
| 7950 | ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq); |
| 7951 | if (ret) |
| 7952 | goto vcc_disable; |
| 7953 | ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2); |
| 7954 | if (ret) |
| 7955 | goto vccq_lpm; |
| 7956 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7957 | ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7958 | } |
| 7959 | goto out; |
| 7960 | |
| 7961 | vccq_lpm: |
| 7962 | ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq); |
| 7963 | vcc_disable: |
| 7964 | ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false); |
| 7965 | out: |
| 7966 | return ret; |
| 7967 | } |
| 7968 | |
| 7969 | static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba) |
| 7970 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7971 | if (ufshcd_is_link_off(hba) || |
| 7972 | (ufshcd_is_link_hibern8(hba) |
| 7973 | && ufshcd_is_power_collapse_during_hibern8_allowed(hba))) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7974 | ufshcd_setup_hba_vreg(hba, false); |
| 7975 | } |
| 7976 | |
| 7977 | static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba) |
| 7978 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 7979 | if (ufshcd_is_link_off(hba) || |
| 7980 | (ufshcd_is_link_hibern8(hba) |
| 7981 | && ufshcd_is_power_collapse_during_hibern8_allowed(hba))) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 7982 | ufshcd_setup_hba_vreg(hba, true); |
| 7983 | } |
| 7984 | |
| 7985 | /** |
| 7986 | * ufshcd_suspend - helper function for suspend operations |
| 7987 | * @hba: per adapter instance |
| 7988 | * @pm_op: desired low power operation type |
| 7989 | * |
| 7990 | * This function will try to put the UFS device and link into low power |
| 7991 | * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl" |
| 7992 | * (System PM level). |
| 7993 | * |
| 7994 | * If this function is called during shutdown, it will make sure that |
| 7995 | * both UFS device and UFS link is powered off. |
| 7996 | * |
| 7997 | * NOTE: UFS device & link must be active before we enter in this function. |
| 7998 | * |
| 7999 | * Returns 0 for success and non-zero for failure |
| 8000 | */ |
| 8001 | static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op) |
| 8002 | { |
| 8003 | int ret = 0; |
| 8004 | enum ufs_pm_level pm_lvl; |
| 8005 | enum ufs_dev_pwr_mode req_dev_pwr_mode; |
| 8006 | enum uic_link_state req_link_state; |
| 8007 | |
| 8008 | hba->pm_op_in_progress = 1; |
| 8009 | if (!ufshcd_is_shutdown_pm(pm_op)) { |
| 8010 | pm_lvl = ufshcd_is_runtime_pm(pm_op) ? |
| 8011 | hba->rpm_lvl : hba->spm_lvl; |
| 8012 | req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl); |
| 8013 | req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl); |
| 8014 | } else { |
| 8015 | req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE; |
| 8016 | req_link_state = UIC_LINK_OFF_STATE; |
| 8017 | } |
| 8018 | |
| 8019 | /* |
| 8020 | * If we can't transition into any of the low power modes |
| 8021 | * just gate the clocks. |
| 8022 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8023 | WARN_ON(hba->hibern8_on_idle.is_enabled && |
| 8024 | hba->hibern8_on_idle.active_reqs); |
| 8025 | ufshcd_hold_all(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8026 | hba->clk_gating.is_suspended = true; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8027 | hba->hibern8_on_idle.is_suspended = true; |
| 8028 | |
| 8029 | if (hba->clk_scaling.is_allowed) { |
| 8030 | cancel_work_sync(&hba->clk_scaling.suspend_work); |
| 8031 | cancel_work_sync(&hba->clk_scaling.resume_work); |
| 8032 | ufshcd_suspend_clkscaling(hba); |
| 8033 | } |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8034 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8035 | if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE && |
| 8036 | req_link_state == UIC_LINK_ACTIVE_STATE) { |
| 8037 | goto disable_clks; |
| 8038 | } |
| 8039 | |
| 8040 | if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) && |
| 8041 | (req_link_state == hba->uic_link_state)) |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8042 | goto enable_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8043 | |
| 8044 | /* UFS device & link must be active before we enter in this function */ |
| 8045 | if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) { |
| 8046 | ret = -EINVAL; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8047 | goto enable_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8048 | } |
| 8049 | |
| 8050 | if (ufshcd_is_runtime_pm(pm_op)) { |
Subhash Jadavani | 374a246 | 2014-09-25 15:32:35 +0300 | [diff] [blame] | 8051 | if (ufshcd_can_autobkops_during_suspend(hba)) { |
| 8052 | /* |
| 8053 | * The device is idle with no requests in the queue, |
| 8054 | * allow background operations if bkops status shows |
| 8055 | * that performance might be impacted. |
| 8056 | */ |
| 8057 | ret = ufshcd_urgent_bkops(hba); |
| 8058 | if (ret) |
| 8059 | goto enable_gating; |
| 8060 | } else { |
| 8061 | /* make sure that auto bkops is disabled */ |
| 8062 | ufshcd_disable_auto_bkops(hba); |
| 8063 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8064 | } |
| 8065 | |
| 8066 | if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) && |
| 8067 | ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) || |
| 8068 | !ufshcd_is_runtime_pm(pm_op))) { |
| 8069 | /* ensure that bkops is disabled */ |
| 8070 | ufshcd_disable_auto_bkops(hba); |
| 8071 | ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode); |
| 8072 | if (ret) |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8073 | goto enable_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8074 | } |
| 8075 | |
| 8076 | ret = ufshcd_link_state_transition(hba, req_link_state, 1); |
| 8077 | if (ret) |
| 8078 | goto set_dev_active; |
| 8079 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8080 | if (ufshcd_is_link_hibern8(hba) && |
| 8081 | ufshcd_is_hibern8_on_idle_allowed(hba)) |
| 8082 | hba->hibern8_on_idle.state = HIBERN8_ENTERED; |
| 8083 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8084 | ufshcd_vreg_set_lpm(hba); |
| 8085 | |
| 8086 | disable_clks: |
| 8087 | /* |
| 8088 | * Call vendor specific suspend callback. As these callbacks may access |
| 8089 | * vendor specific host controller register space call them before the |
| 8090 | * host clocks are ON. |
| 8091 | */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 8092 | ret = ufshcd_vops_suspend(hba, pm_op); |
| 8093 | if (ret) |
| 8094 | goto set_link_active; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8095 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8096 | if (!ufshcd_is_link_active(hba)) |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8097 | ret = ufshcd_disable_clocks(hba, false); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8098 | else |
| 8099 | /* If link is active, device ref_clk can't be switched off */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8100 | ret = ufshcd_disable_clocks_skip_ref_clk(hba, false); |
| 8101 | if (ret) |
| 8102 | goto set_link_active; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8103 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8104 | if (ufshcd_is_clkgating_allowed(hba)) { |
| 8105 | hba->clk_gating.state = CLKS_OFF; |
| 8106 | trace_ufshcd_clk_gating(dev_name(hba->dev), |
| 8107 | hba->clk_gating.state); |
| 8108 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8109 | /* |
| 8110 | * 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] | 8111 | * host controller transaction expected till resume. |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8112 | */ |
| 8113 | ufshcd_disable_irq(hba); |
| 8114 | /* Put the host controller in low power mode if possible */ |
| 8115 | ufshcd_hba_vreg_set_lpm(hba); |
| 8116 | goto out; |
| 8117 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8118 | set_link_active: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8119 | if (hba->clk_scaling.is_allowed) |
| 8120 | ufshcd_resume_clkscaling(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8121 | ufshcd_vreg_set_hpm(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8122 | if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8123 | ufshcd_set_link_active(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8124 | } else if (ufshcd_is_link_off(hba)) { |
| 8125 | ufshcd_update_error_stats(hba, UFS_ERR_VOPS_SUSPEND); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8126 | ufshcd_host_reset_and_restore(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8127 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8128 | set_dev_active: |
| 8129 | if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE)) |
| 8130 | ufshcd_disable_auto_bkops(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8131 | enable_gating: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8132 | if (hba->clk_scaling.is_allowed) |
| 8133 | ufshcd_resume_clkscaling(hba); |
| 8134 | hba->hibern8_on_idle.is_suspended = false; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8135 | hba->clk_gating.is_suspended = false; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8136 | ufshcd_release_all(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8137 | out: |
| 8138 | hba->pm_op_in_progress = 0; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8139 | |
| 8140 | if (ret) |
| 8141 | ufshcd_update_error_stats(hba, UFS_ERR_SUSPEND); |
| 8142 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8143 | return ret; |
| 8144 | } |
| 8145 | |
| 8146 | /** |
| 8147 | * ufshcd_resume - helper function for resume operations |
| 8148 | * @hba: per adapter instance |
| 8149 | * @pm_op: runtime PM or system PM |
| 8150 | * |
| 8151 | * This function basically brings the UFS device, UniPro link and controller |
| 8152 | * to active state. |
| 8153 | * |
| 8154 | * Returns 0 for success and non-zero for failure |
| 8155 | */ |
| 8156 | static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op) |
| 8157 | { |
| 8158 | int ret; |
| 8159 | enum uic_link_state old_link_state; |
| 8160 | |
| 8161 | hba->pm_op_in_progress = 1; |
| 8162 | old_link_state = hba->uic_link_state; |
| 8163 | |
| 8164 | ufshcd_hba_vreg_set_hpm(hba); |
| 8165 | /* Make sure clocks are enabled before accessing controller */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8166 | ret = ufshcd_enable_clocks(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8167 | if (ret) |
| 8168 | goto out; |
| 8169 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8170 | /* enable the host irq as host controller would be active soon */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8171 | ufshcd_enable_irq(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8172 | |
| 8173 | ret = ufshcd_vreg_set_hpm(hba); |
| 8174 | if (ret) |
| 8175 | goto disable_irq_and_vops_clks; |
| 8176 | |
| 8177 | /* |
| 8178 | * Call vendor specific resume callback. As these callbacks may access |
| 8179 | * vendor specific host controller register space call them when the |
| 8180 | * host clocks are ON. |
| 8181 | */ |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 8182 | ret = ufshcd_vops_resume(hba, pm_op); |
| 8183 | if (ret) |
| 8184 | goto disable_vreg; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8185 | |
| 8186 | if (ufshcd_is_link_hibern8(hba)) { |
| 8187 | ret = ufshcd_uic_hibern8_exit(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8188 | if (!ret) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8189 | ufshcd_set_link_active(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8190 | if (ufshcd_is_hibern8_on_idle_allowed(hba)) |
| 8191 | hba->hibern8_on_idle.state = HIBERN8_EXITED; |
| 8192 | } else { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8193 | goto vendor_suspend; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8194 | } |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8195 | } else if (ufshcd_is_link_off(hba)) { |
| 8196 | ret = ufshcd_host_reset_and_restore(hba); |
| 8197 | /* |
| 8198 | * ufshcd_host_reset_and_restore() should have already |
| 8199 | * set the link state as active |
| 8200 | */ |
| 8201 | if (ret || !ufshcd_is_link_active(hba)) |
| 8202 | goto vendor_suspend; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8203 | /* mark link state as hibern8 exited */ |
| 8204 | if (ufshcd_is_hibern8_on_idle_allowed(hba)) |
| 8205 | hba->hibern8_on_idle.state = HIBERN8_EXITED; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8206 | } |
| 8207 | |
| 8208 | if (!ufshcd_is_ufs_dev_active(hba)) { |
| 8209 | ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE); |
| 8210 | if (ret) |
| 8211 | goto set_old_link_state; |
| 8212 | } |
| 8213 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8214 | if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) |
| 8215 | ufshcd_enable_auto_bkops(hba); |
| 8216 | else |
| 8217 | /* |
| 8218 | * If BKOPs operations are urgently needed at this moment then |
| 8219 | * keep auto-bkops enabled or else disable it. |
| 8220 | */ |
| 8221 | ufshcd_urgent_bkops(hba); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8222 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8223 | hba->clk_gating.is_suspended = false; |
| 8224 | hba->hibern8_on_idle.is_suspended = false; |
| 8225 | |
| 8226 | if (hba->clk_scaling.is_allowed) |
| 8227 | ufshcd_resume_clkscaling(hba); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 8228 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8229 | /* Schedule clock gating in case of no access to UFS device yet */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8230 | ufshcd_release_all(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8231 | goto out; |
| 8232 | |
| 8233 | set_old_link_state: |
| 8234 | ufshcd_link_state_transition(hba, old_link_state, 0); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8235 | if (ufshcd_is_link_hibern8(hba) && |
| 8236 | ufshcd_is_hibern8_on_idle_allowed(hba)) |
| 8237 | hba->hibern8_on_idle.state = HIBERN8_ENTERED; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8238 | vendor_suspend: |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 8239 | ufshcd_vops_suspend(hba, pm_op); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8240 | disable_vreg: |
| 8241 | ufshcd_vreg_set_lpm(hba); |
| 8242 | disable_irq_and_vops_clks: |
| 8243 | ufshcd_disable_irq(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8244 | if (hba->clk_scaling.is_allowed) |
| 8245 | ufshcd_suspend_clkscaling(hba); |
| 8246 | ufshcd_disable_clocks(hba, false); |
| 8247 | if (ufshcd_is_clkgating_allowed(hba)) |
| 8248 | hba->clk_gating.state = CLKS_OFF; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8249 | out: |
| 8250 | hba->pm_op_in_progress = 0; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8251 | |
| 8252 | if (ret) |
| 8253 | ufshcd_update_error_stats(hba, UFS_ERR_RESUME); |
| 8254 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8255 | return ret; |
| 8256 | } |
| 8257 | |
| 8258 | /** |
| 8259 | * ufshcd_system_suspend - system suspend routine |
| 8260 | * @hba: per adapter instance |
| 8261 | * @pm_op: runtime PM or system PM |
| 8262 | * |
| 8263 | * Check the description of ufshcd_suspend() function for more details. |
| 8264 | * |
| 8265 | * Returns 0 for success and non-zero for failure |
| 8266 | */ |
| 8267 | int ufshcd_system_suspend(struct ufs_hba *hba) |
| 8268 | { |
| 8269 | int ret = 0; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8270 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8271 | |
| 8272 | if (!hba || !hba->is_powered) |
Dolev Raviv | 233b594 | 2014-10-23 13:25:14 +0300 | [diff] [blame] | 8273 | return 0; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8274 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8275 | if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) == |
| 8276 | hba->curr_dev_pwr_mode) && |
| 8277 | (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) == |
| 8278 | hba->uic_link_state)) |
| 8279 | goto out; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8280 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8281 | if (pm_runtime_suspended(hba->dev)) { |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8282 | /* |
| 8283 | * UFS device and/or UFS link low power states during runtime |
| 8284 | * suspend seems to be different than what is expected during |
| 8285 | * system suspend. Hence runtime resume the devic & link and |
| 8286 | * let the system suspend low power states to take effect. |
| 8287 | * TODO: If resume takes longer time, we might have optimize |
| 8288 | * it in future by not resuming everything if possible. |
| 8289 | */ |
| 8290 | ret = ufshcd_runtime_resume(hba); |
| 8291 | if (ret) |
| 8292 | goto out; |
| 8293 | } |
| 8294 | |
| 8295 | ret = ufshcd_suspend(hba, UFS_SYSTEM_PM); |
| 8296 | out: |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8297 | trace_ufshcd_system_suspend(dev_name(hba->dev), ret, |
| 8298 | ktime_to_us(ktime_sub(ktime_get(), start)), |
| 8299 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
Dolev Raviv | e785060 | 2014-09-25 15:32:36 +0300 | [diff] [blame] | 8300 | if (!ret) |
| 8301 | hba->is_sys_suspended = true; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8302 | return ret; |
| 8303 | } |
| 8304 | EXPORT_SYMBOL(ufshcd_system_suspend); |
| 8305 | |
| 8306 | /** |
| 8307 | * ufshcd_system_resume - system resume routine |
| 8308 | * @hba: per adapter instance |
| 8309 | * |
| 8310 | * Returns 0 for success and non-zero for failure |
| 8311 | */ |
| 8312 | |
| 8313 | int ufshcd_system_resume(struct ufs_hba *hba) |
| 8314 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8315 | int ret = 0; |
| 8316 | ktime_t start = ktime_get(); |
| 8317 | |
| 8318 | if (!hba) |
| 8319 | return -EINVAL; |
| 8320 | |
| 8321 | if (!hba->is_powered || pm_runtime_suspended(hba->dev)) |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8322 | /* |
| 8323 | * Let the runtime resume take care of resuming |
| 8324 | * if runtime suspended. |
| 8325 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8326 | goto out; |
| 8327 | else |
| 8328 | ret = ufshcd_resume(hba, UFS_SYSTEM_PM); |
| 8329 | out: |
| 8330 | trace_ufshcd_system_resume(dev_name(hba->dev), ret, |
| 8331 | ktime_to_us(ktime_sub(ktime_get(), start)), |
| 8332 | hba->curr_dev_pwr_mode, hba->uic_link_state); |
| 8333 | return ret; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8334 | } |
| 8335 | EXPORT_SYMBOL(ufshcd_system_resume); |
| 8336 | |
| 8337 | /** |
| 8338 | * ufshcd_runtime_suspend - runtime suspend routine |
| 8339 | * @hba: per adapter instance |
| 8340 | * |
| 8341 | * Check the description of ufshcd_suspend() function for more details. |
| 8342 | * |
| 8343 | * Returns 0 for success and non-zero for failure |
| 8344 | */ |
| 8345 | int ufshcd_runtime_suspend(struct ufs_hba *hba) |
| 8346 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8347 | int ret = 0; |
| 8348 | ktime_t start = ktime_get(); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8349 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8350 | if (!hba) |
| 8351 | return -EINVAL; |
| 8352 | |
| 8353 | if (!hba->is_powered) |
| 8354 | goto out; |
| 8355 | else |
| 8356 | ret = ufshcd_suspend(hba, UFS_RUNTIME_PM); |
| 8357 | out: |
| 8358 | trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret, |
| 8359 | ktime_to_us(ktime_sub(ktime_get(), start)), |
| 8360 | hba->curr_dev_pwr_mode, |
| 8361 | hba->uic_link_state); |
| 8362 | return ret; |
| 8363 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8364 | } |
| 8365 | EXPORT_SYMBOL(ufshcd_runtime_suspend); |
| 8366 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8367 | /** |
| 8368 | * ufshcd_runtime_resume - runtime resume routine |
| 8369 | * @hba: per adapter instance |
| 8370 | * |
| 8371 | * This function basically brings the UFS device, UniPro link and controller |
| 8372 | * to active state. Following operations are done in this function: |
| 8373 | * |
| 8374 | * 1. Turn on all the controller related clocks |
| 8375 | * 2. Bring the UniPro link out of Hibernate state |
| 8376 | * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device |
| 8377 | * to active state. |
| 8378 | * 4. If auto-bkops is enabled on the device, disable it. |
| 8379 | * |
| 8380 | * So following would be the possible power state after this function return |
| 8381 | * successfully: |
| 8382 | * S1: UFS device in Active state with VCC rail ON |
| 8383 | * UniPro link in Active state |
| 8384 | * All the UFS/UniPro controller clocks are ON |
| 8385 | * |
| 8386 | * Returns 0 for success and non-zero for failure |
| 8387 | */ |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8388 | int ufshcd_runtime_resume(struct ufs_hba *hba) |
| 8389 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8390 | int ret = 0; |
| 8391 | ktime_t start = ktime_get(); |
| 8392 | |
| 8393 | if (!hba) |
| 8394 | return -EINVAL; |
| 8395 | |
| 8396 | if (!hba->is_powered) |
| 8397 | goto out; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8398 | else |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8399 | ret = ufshcd_resume(hba, UFS_RUNTIME_PM); |
| 8400 | out: |
| 8401 | trace_ufshcd_runtime_resume(dev_name(hba->dev), ret, |
| 8402 | ktime_to_us(ktime_sub(ktime_get(), start)), |
| 8403 | hba->curr_dev_pwr_mode, |
| 8404 | hba->uic_link_state); |
| 8405 | return ret; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 8406 | } |
| 8407 | EXPORT_SYMBOL(ufshcd_runtime_resume); |
| 8408 | |
| 8409 | int ufshcd_runtime_idle(struct ufs_hba *hba) |
| 8410 | { |
| 8411 | return 0; |
| 8412 | } |
| 8413 | EXPORT_SYMBOL(ufshcd_runtime_idle); |
| 8414 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8415 | static inline ssize_t ufshcd_pm_lvl_store(struct device *dev, |
| 8416 | struct device_attribute *attr, |
| 8417 | const char *buf, size_t count, |
| 8418 | bool rpm) |
| 8419 | { |
| 8420 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 8421 | unsigned long flags, value; |
| 8422 | |
| 8423 | if (kstrtoul(buf, 0, &value)) |
| 8424 | return -EINVAL; |
| 8425 | |
| 8426 | if (value >= UFS_PM_LVL_MAX) |
| 8427 | return -EINVAL; |
| 8428 | |
| 8429 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 8430 | if (rpm) |
| 8431 | hba->rpm_lvl = value; |
| 8432 | else |
| 8433 | hba->spm_lvl = value; |
| 8434 | ufshcd_apply_pm_quirks(hba); |
| 8435 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 8436 | return count; |
| 8437 | } |
| 8438 | |
| 8439 | static ssize_t ufshcd_rpm_lvl_show(struct device *dev, |
| 8440 | struct device_attribute *attr, char *buf) |
| 8441 | { |
| 8442 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 8443 | int curr_len; |
| 8444 | u8 lvl; |
| 8445 | |
| 8446 | curr_len = snprintf(buf, PAGE_SIZE, |
| 8447 | "\nCurrent Runtime PM level [%d] => dev_state [%s] link_state [%s]\n", |
| 8448 | hba->rpm_lvl, |
| 8449 | ufschd_ufs_dev_pwr_mode_to_string( |
| 8450 | ufs_pm_lvl_states[hba->rpm_lvl].dev_state), |
| 8451 | ufschd_uic_link_state_to_string( |
| 8452 | ufs_pm_lvl_states[hba->rpm_lvl].link_state)); |
| 8453 | |
| 8454 | curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len), |
| 8455 | "\nAll available Runtime PM levels info:\n"); |
| 8456 | for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) |
| 8457 | curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len), |
| 8458 | "\tRuntime PM level [%d] => dev_state [%s] link_state [%s]\n", |
| 8459 | lvl, |
| 8460 | ufschd_ufs_dev_pwr_mode_to_string( |
| 8461 | ufs_pm_lvl_states[lvl].dev_state), |
| 8462 | ufschd_uic_link_state_to_string( |
| 8463 | ufs_pm_lvl_states[lvl].link_state)); |
| 8464 | |
| 8465 | return curr_len; |
| 8466 | } |
| 8467 | |
| 8468 | static ssize_t ufshcd_rpm_lvl_store(struct device *dev, |
| 8469 | struct device_attribute *attr, const char *buf, size_t count) |
| 8470 | { |
| 8471 | return ufshcd_pm_lvl_store(dev, attr, buf, count, true); |
| 8472 | } |
| 8473 | |
| 8474 | static void ufshcd_add_rpm_lvl_sysfs_nodes(struct ufs_hba *hba) |
| 8475 | { |
| 8476 | hba->rpm_lvl_attr.show = ufshcd_rpm_lvl_show; |
| 8477 | hba->rpm_lvl_attr.store = ufshcd_rpm_lvl_store; |
| 8478 | sysfs_attr_init(&hba->rpm_lvl_attr.attr); |
| 8479 | hba->rpm_lvl_attr.attr.name = "rpm_lvl"; |
| 8480 | hba->rpm_lvl_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 8481 | if (device_create_file(hba->dev, &hba->rpm_lvl_attr)) |
| 8482 | dev_err(hba->dev, "Failed to create sysfs for rpm_lvl\n"); |
| 8483 | } |
| 8484 | |
| 8485 | static ssize_t ufshcd_spm_lvl_show(struct device *dev, |
| 8486 | struct device_attribute *attr, char *buf) |
| 8487 | { |
| 8488 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 8489 | int curr_len; |
| 8490 | u8 lvl; |
| 8491 | |
| 8492 | curr_len = snprintf(buf, PAGE_SIZE, |
| 8493 | "\nCurrent System PM level [%d] => dev_state [%s] link_state [%s]\n", |
| 8494 | hba->spm_lvl, |
| 8495 | ufschd_ufs_dev_pwr_mode_to_string( |
| 8496 | ufs_pm_lvl_states[hba->spm_lvl].dev_state), |
| 8497 | ufschd_uic_link_state_to_string( |
| 8498 | ufs_pm_lvl_states[hba->spm_lvl].link_state)); |
| 8499 | |
| 8500 | curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len), |
| 8501 | "\nAll available System PM levels info:\n"); |
| 8502 | for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) |
| 8503 | curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len), |
| 8504 | "\tSystem PM level [%d] => dev_state [%s] link_state [%s]\n", |
| 8505 | lvl, |
| 8506 | ufschd_ufs_dev_pwr_mode_to_string( |
| 8507 | ufs_pm_lvl_states[lvl].dev_state), |
| 8508 | ufschd_uic_link_state_to_string( |
| 8509 | ufs_pm_lvl_states[lvl].link_state)); |
| 8510 | |
| 8511 | return curr_len; |
| 8512 | } |
| 8513 | |
| 8514 | static ssize_t ufshcd_spm_lvl_store(struct device *dev, |
| 8515 | struct device_attribute *attr, const char *buf, size_t count) |
| 8516 | { |
| 8517 | return ufshcd_pm_lvl_store(dev, attr, buf, count, false); |
| 8518 | } |
| 8519 | |
| 8520 | static void ufshcd_add_spm_lvl_sysfs_nodes(struct ufs_hba *hba) |
| 8521 | { |
| 8522 | hba->spm_lvl_attr.show = ufshcd_spm_lvl_show; |
| 8523 | hba->spm_lvl_attr.store = ufshcd_spm_lvl_store; |
| 8524 | sysfs_attr_init(&hba->spm_lvl_attr.attr); |
| 8525 | hba->spm_lvl_attr.attr.name = "spm_lvl"; |
| 8526 | hba->spm_lvl_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 8527 | if (device_create_file(hba->dev, &hba->spm_lvl_attr)) |
| 8528 | dev_err(hba->dev, "Failed to create sysfs for spm_lvl\n"); |
| 8529 | } |
| 8530 | |
| 8531 | static inline void ufshcd_add_sysfs_nodes(struct ufs_hba *hba) |
| 8532 | { |
| 8533 | ufshcd_add_rpm_lvl_sysfs_nodes(hba); |
| 8534 | ufshcd_add_spm_lvl_sysfs_nodes(hba); |
| 8535 | } |
| 8536 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8537 | /** |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8538 | * ufshcd_shutdown - shutdown routine |
| 8539 | * @hba: per adapter instance |
| 8540 | * |
| 8541 | * This function would power off both UFS device and UFS link. |
| 8542 | * |
| 8543 | * Returns 0 always to allow force shutdown even in case of errors. |
| 8544 | */ |
| 8545 | int ufshcd_shutdown(struct ufs_hba *hba) |
| 8546 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8547 | /* |
| 8548 | * TODO: This function should send the power down notification to |
| 8549 | * UFS device and then power off the UFS link. But we need to be sure |
| 8550 | * that there will not be any new UFS requests issued after this. |
| 8551 | */ |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8552 | return 0; |
| 8553 | } |
| 8554 | EXPORT_SYMBOL(ufshcd_shutdown); |
| 8555 | |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 8556 | /* |
| 8557 | * Values permitted 0, 1, 2. |
| 8558 | * 0 -> Disable IO latency histograms (default) |
| 8559 | * 1 -> Enable IO latency histograms |
| 8560 | * 2 -> Zero out IO latency histograms |
| 8561 | */ |
| 8562 | static ssize_t |
| 8563 | latency_hist_store(struct device *dev, struct device_attribute *attr, |
| 8564 | const char *buf, size_t count) |
| 8565 | { |
| 8566 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 8567 | long value; |
| 8568 | |
| 8569 | if (kstrtol(buf, 0, &value)) |
| 8570 | return -EINVAL; |
| 8571 | if (value == BLK_IO_LAT_HIST_ZERO) |
| 8572 | blk_zero_latency_hist(&hba->io_lat_s); |
| 8573 | else if (value == BLK_IO_LAT_HIST_ENABLE || |
| 8574 | value == BLK_IO_LAT_HIST_DISABLE) |
| 8575 | hba->latency_hist_enabled = value; |
| 8576 | return count; |
| 8577 | } |
| 8578 | |
| 8579 | ssize_t |
| 8580 | latency_hist_show(struct device *dev, struct device_attribute *attr, |
| 8581 | char *buf) |
| 8582 | { |
| 8583 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 8584 | |
| 8585 | return blk_latency_hist_show(&hba->io_lat_s, buf); |
| 8586 | } |
| 8587 | |
| 8588 | static DEVICE_ATTR(latency_hist, S_IRUGO | S_IWUSR, |
| 8589 | latency_hist_show, latency_hist_store); |
| 8590 | |
| 8591 | static void |
| 8592 | ufshcd_init_latency_hist(struct ufs_hba *hba) |
| 8593 | { |
| 8594 | if (device_create_file(hba->dev, &dev_attr_latency_hist)) |
| 8595 | dev_err(hba->dev, "Failed to create latency_hist sysfs entry\n"); |
| 8596 | } |
| 8597 | |
| 8598 | static void |
| 8599 | ufshcd_exit_latency_hist(struct ufs_hba *hba) |
| 8600 | { |
| 8601 | device_create_file(hba->dev, &dev_attr_latency_hist); |
| 8602 | } |
| 8603 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 8604 | /** |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8605 | * ufshcd_remove - de-allocate SCSI host and host memory space |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8606 | * data structure memory |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8607 | * @hba - per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8608 | */ |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8609 | void ufshcd_remove(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8610 | { |
Akinobu Mita | cfdf9c9 | 2013-07-30 00:36:03 +0530 | [diff] [blame] | 8611 | scsi_remove_host(hba->host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8612 | /* disable interrupts */ |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 8613 | ufshcd_disable_intr(hba, hba->intr_mask); |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 8614 | ufshcd_hba_stop(hba, true); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8615 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 8616 | ufshcd_exit_clk_gating(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8617 | ufshcd_exit_hibern8_on_idle(hba); |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 8618 | ufshcd_exit_latency_hist(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8619 | if (ufshcd_is_clkscaling_supported(hba)) { |
| 8620 | device_remove_file(hba->dev, &hba->clk_scaling.enable_attr); |
Kyle Yan | 65be4a5 | 2016-10-31 15:05:00 -0700 | [diff] [blame] | 8621 | if (hba->devfreq) |
| 8622 | devfreq_remove_device(hba->devfreq); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8623 | } |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 8624 | ufshcd_hba_exit(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8625 | ufsdbg_remove_debugfs(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8626 | } |
| 8627 | EXPORT_SYMBOL_GPL(ufshcd_remove); |
| 8628 | |
| 8629 | /** |
Yaniv Gardi | 47555a5 | 2015-10-28 13:15:49 +0200 | [diff] [blame] | 8630 | * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA) |
| 8631 | * @hba: pointer to Host Bus Adapter (HBA) |
| 8632 | */ |
| 8633 | void ufshcd_dealloc_host(struct ufs_hba *hba) |
| 8634 | { |
| 8635 | scsi_host_put(hba->host); |
| 8636 | } |
| 8637 | EXPORT_SYMBOL_GPL(ufshcd_dealloc_host); |
| 8638 | |
| 8639 | /** |
Akinobu Mita | ca3d7bf | 2014-07-13 21:24:46 +0900 | [diff] [blame] | 8640 | * ufshcd_set_dma_mask - Set dma mask based on the controller |
| 8641 | * addressing capability |
| 8642 | * @hba: per adapter instance |
| 8643 | * |
| 8644 | * Returns 0 for success, non-zero for failure |
| 8645 | */ |
| 8646 | static int ufshcd_set_dma_mask(struct ufs_hba *hba) |
| 8647 | { |
| 8648 | if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) { |
| 8649 | if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64))) |
| 8650 | return 0; |
| 8651 | } |
| 8652 | return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32)); |
| 8653 | } |
| 8654 | |
| 8655 | /** |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8656 | * ufshcd_alloc_host - allocate Host Bus Adapter (HBA) |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8657 | * @dev: pointer to device handle |
| 8658 | * @hba_handle: driver private handle |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8659 | * Returns 0 on success, non-zero value on failure |
| 8660 | */ |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8661 | int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8662 | { |
| 8663 | struct Scsi_Host *host; |
| 8664 | struct ufs_hba *hba; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8665 | int err = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8666 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8667 | if (!dev) { |
| 8668 | dev_err(dev, |
| 8669 | "Invalid memory reference for dev is NULL\n"); |
| 8670 | err = -ENODEV; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8671 | goto out_error; |
| 8672 | } |
| 8673 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8674 | host = scsi_host_alloc(&ufshcd_driver_template, |
| 8675 | sizeof(struct ufs_hba)); |
| 8676 | if (!host) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8677 | dev_err(dev, "scsi_host_alloc failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8678 | err = -ENOMEM; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8679 | goto out_error; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8680 | } |
| 8681 | hba = shost_priv(host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 8682 | hba->host = host; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 8683 | hba->dev = dev; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 8684 | *hba_handle = hba; |
| 8685 | |
| 8686 | out_error: |
| 8687 | return err; |
| 8688 | } |
| 8689 | EXPORT_SYMBOL(ufshcd_alloc_host); |
| 8690 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8691 | /** |
| 8692 | * ufshcd_is_devfreq_scaling_required - check if scaling is required or not |
| 8693 | * @hba: per adapter instance |
| 8694 | * @scale_up: True if scaling up and false if scaling down |
| 8695 | * |
| 8696 | * Returns true if scaling is required, false otherwise. |
| 8697 | */ |
| 8698 | static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba, |
| 8699 | bool scale_up) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 8700 | { |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 8701 | struct ufs_clk_info *clki; |
| 8702 | struct list_head *head = &hba->clk_list_head; |
| 8703 | |
| 8704 | if (!head || list_empty(head)) |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8705 | return false; |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 8706 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 8707 | list_for_each_entry(clki, head, list) { |
| 8708 | if (!IS_ERR_OR_NULL(clki->clk)) { |
| 8709 | if (scale_up && clki->max_freq) { |
| 8710 | if (clki->curr_freq == clki->max_freq) |
| 8711 | continue; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8712 | return true; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 8713 | } else if (!scale_up && clki->min_freq) { |
| 8714 | if (clki->curr_freq == clki->min_freq) |
| 8715 | continue; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8716 | return true; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 8717 | } |
| 8718 | } |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 8719 | } |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 8720 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8721 | return false; |
| 8722 | } |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 8723 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8724 | /** |
| 8725 | * ufshcd_scale_gear - scale up/down UFS gear |
| 8726 | * @hba: per adapter instance |
| 8727 | * @scale_up: True for scaling up gear and false for scaling down |
| 8728 | * |
| 8729 | * Returns 0 for success, |
| 8730 | * Returns -EBUSY if scaling can't happen at this time |
| 8731 | * Returns non-zero for any other errors |
| 8732 | */ |
| 8733 | static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up) |
| 8734 | { |
| 8735 | int ret = 0; |
| 8736 | struct ufs_pa_layer_attr new_pwr_info; |
| 8737 | u32 scale_down_gear = ufshcd_vops_get_scale_down_gear(hba); |
| 8738 | |
| 8739 | BUG_ON(!hba->clk_scaling.saved_pwr_info.is_valid); |
| 8740 | |
| 8741 | if (scale_up) { |
| 8742 | memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info.info, |
| 8743 | sizeof(struct ufs_pa_layer_attr)); |
| 8744 | } else { |
| 8745 | memcpy(&new_pwr_info, &hba->pwr_info, |
| 8746 | sizeof(struct ufs_pa_layer_attr)); |
| 8747 | |
| 8748 | if (hba->pwr_info.gear_tx > scale_down_gear |
| 8749 | || hba->pwr_info.gear_rx > scale_down_gear) { |
| 8750 | /* save the current power mode */ |
| 8751 | memcpy(&hba->clk_scaling.saved_pwr_info.info, |
| 8752 | &hba->pwr_info, |
| 8753 | sizeof(struct ufs_pa_layer_attr)); |
| 8754 | |
| 8755 | /* scale down gear */ |
| 8756 | new_pwr_info.gear_tx = scale_down_gear; |
| 8757 | new_pwr_info.gear_rx = scale_down_gear; |
| 8758 | if (!(hba->dev_quirks & UFS_DEVICE_NO_FASTAUTO)) { |
| 8759 | new_pwr_info.pwr_tx = FASTAUTO_MODE; |
| 8760 | new_pwr_info.pwr_rx = FASTAUTO_MODE; |
| 8761 | } |
| 8762 | } |
| 8763 | } |
| 8764 | |
| 8765 | ret = ufshcd_change_power_mode(hba, &new_pwr_info); |
| 8766 | |
| 8767 | if (ret) |
| 8768 | dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d), scale_up = %d", |
| 8769 | __func__, ret, |
| 8770 | hba->pwr_info.gear_tx, hba->pwr_info.gear_rx, |
| 8771 | new_pwr_info.gear_tx, new_pwr_info.gear_rx, |
| 8772 | scale_up); |
| 8773 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 8774 | return ret; |
| 8775 | } |
| 8776 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8777 | static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba) |
| 8778 | { |
| 8779 | #define DOORBELL_CLR_TOUT_US (1000 * 1000) /* 1 sec */ |
| 8780 | int ret = 0; |
| 8781 | /* |
| 8782 | * make sure that there are no outstanding requests when |
| 8783 | * clock scaling is in progress |
| 8784 | */ |
| 8785 | ufshcd_scsi_block_requests(hba); |
| 8786 | down_write(&hba->clk_scaling_lock); |
| 8787 | if (ufshcd_wait_for_doorbell_clr(hba, DOORBELL_CLR_TOUT_US)) { |
| 8788 | ret = -EBUSY; |
| 8789 | up_write(&hba->clk_scaling_lock); |
| 8790 | ufshcd_scsi_unblock_requests(hba); |
| 8791 | } |
| 8792 | |
| 8793 | return ret; |
| 8794 | } |
| 8795 | |
| 8796 | static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba) |
| 8797 | { |
| 8798 | up_write(&hba->clk_scaling_lock); |
| 8799 | ufshcd_scsi_unblock_requests(hba); |
| 8800 | } |
| 8801 | |
| 8802 | /** |
| 8803 | * ufshcd_devfreq_scale - scale up/down UFS clocks and gear |
| 8804 | * @hba: per adapter instance |
| 8805 | * @scale_up: True for scaling up and false for scalin down |
| 8806 | * |
| 8807 | * Returns 0 for success, |
| 8808 | * Returns -EBUSY if scaling can't happen at this time |
| 8809 | * Returns non-zero for any other errors |
| 8810 | */ |
| 8811 | static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up) |
| 8812 | { |
| 8813 | int ret = 0; |
| 8814 | |
| 8815 | /* let's not get into low power until clock scaling is completed */ |
| 8816 | ufshcd_hold_all(hba); |
| 8817 | |
| 8818 | ret = ufshcd_clock_scaling_prepare(hba); |
| 8819 | if (ret) |
| 8820 | goto out; |
| 8821 | |
| 8822 | /* scale down the gear before scaling down clocks */ |
| 8823 | if (!scale_up) { |
| 8824 | ret = ufshcd_scale_gear(hba, false); |
| 8825 | if (ret) |
| 8826 | goto clk_scaling_unprepare; |
| 8827 | } |
| 8828 | |
| 8829 | ret = ufshcd_scale_clks(hba, scale_up); |
| 8830 | if (ret) |
| 8831 | goto scale_up_gear; |
| 8832 | |
| 8833 | /* scale up the gear after scaling up clocks */ |
| 8834 | if (scale_up) { |
| 8835 | ret = ufshcd_scale_gear(hba, true); |
| 8836 | if (ret) { |
| 8837 | ufshcd_scale_clks(hba, false); |
| 8838 | goto clk_scaling_unprepare; |
| 8839 | } |
| 8840 | } |
| 8841 | |
| 8842 | if (!ret) { |
| 8843 | hba->clk_scaling.is_scaled_up = scale_up; |
| 8844 | if (scale_up) |
| 8845 | hba->clk_gating.delay_ms = |
| 8846 | hba->clk_gating.delay_ms_perf; |
| 8847 | else |
| 8848 | hba->clk_gating.delay_ms = |
| 8849 | hba->clk_gating.delay_ms_pwr_save; |
| 8850 | } |
| 8851 | |
| 8852 | goto clk_scaling_unprepare; |
| 8853 | |
| 8854 | scale_up_gear: |
| 8855 | if (!scale_up) |
| 8856 | ufshcd_scale_gear(hba, true); |
| 8857 | clk_scaling_unprepare: |
| 8858 | ufshcd_clock_scaling_unprepare(hba); |
| 8859 | out: |
| 8860 | ufshcd_release_all(hba); |
| 8861 | return ret; |
| 8862 | } |
| 8863 | |
| 8864 | static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba) |
| 8865 | { |
| 8866 | unsigned long flags; |
| 8867 | |
| 8868 | devfreq_suspend_device(hba->devfreq); |
| 8869 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 8870 | hba->clk_scaling.window_start_t = 0; |
| 8871 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 8872 | } |
| 8873 | |
| 8874 | static void ufshcd_suspend_clkscaling(struct ufs_hba *hba) |
| 8875 | { |
| 8876 | unsigned long flags; |
| 8877 | bool suspend = false; |
| 8878 | |
| 8879 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 8880 | return; |
| 8881 | |
| 8882 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 8883 | if (!hba->clk_scaling.is_suspended) { |
| 8884 | suspend = true; |
| 8885 | hba->clk_scaling.is_suspended = true; |
| 8886 | } |
| 8887 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 8888 | |
| 8889 | if (suspend) |
| 8890 | __ufshcd_suspend_clkscaling(hba); |
| 8891 | } |
| 8892 | |
| 8893 | static void ufshcd_resume_clkscaling(struct ufs_hba *hba) |
| 8894 | { |
| 8895 | unsigned long flags; |
| 8896 | bool resume = false; |
| 8897 | |
| 8898 | if (!ufshcd_is_clkscaling_supported(hba)) |
| 8899 | return; |
| 8900 | |
| 8901 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 8902 | if (hba->clk_scaling.is_suspended) { |
| 8903 | resume = true; |
| 8904 | hba->clk_scaling.is_suspended = false; |
| 8905 | } |
| 8906 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 8907 | |
| 8908 | if (resume) |
| 8909 | devfreq_resume_device(hba->devfreq); |
| 8910 | } |
| 8911 | |
| 8912 | static ssize_t ufshcd_clkscale_enable_show(struct device *dev, |
| 8913 | struct device_attribute *attr, char *buf) |
| 8914 | { |
| 8915 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 8916 | |
| 8917 | return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_scaling.is_allowed); |
| 8918 | } |
| 8919 | |
| 8920 | static ssize_t ufshcd_clkscale_enable_store(struct device *dev, |
| 8921 | struct device_attribute *attr, const char *buf, size_t count) |
| 8922 | { |
| 8923 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 8924 | u32 value; |
| 8925 | int err; |
| 8926 | |
| 8927 | if (kstrtou32(buf, 0, &value)) |
| 8928 | return -EINVAL; |
| 8929 | |
| 8930 | value = !!value; |
| 8931 | if (value == hba->clk_scaling.is_allowed) |
| 8932 | goto out; |
| 8933 | |
| 8934 | pm_runtime_get_sync(hba->dev); |
| 8935 | ufshcd_hold(hba, false); |
| 8936 | |
| 8937 | cancel_work_sync(&hba->clk_scaling.suspend_work); |
| 8938 | cancel_work_sync(&hba->clk_scaling.resume_work); |
| 8939 | |
| 8940 | hba->clk_scaling.is_allowed = value; |
| 8941 | |
| 8942 | if (value) { |
| 8943 | ufshcd_resume_clkscaling(hba); |
| 8944 | } else { |
| 8945 | ufshcd_suspend_clkscaling(hba); |
| 8946 | err = ufshcd_devfreq_scale(hba, true); |
| 8947 | if (err) |
| 8948 | dev_err(hba->dev, "%s: failed to scale clocks up %d\n", |
| 8949 | __func__, err); |
| 8950 | } |
| 8951 | |
| 8952 | ufshcd_release(hba, false); |
| 8953 | pm_runtime_put_sync(hba->dev); |
| 8954 | out: |
| 8955 | return count; |
| 8956 | } |
| 8957 | |
| 8958 | static void ufshcd_clk_scaling_suspend_work(struct work_struct *work) |
| 8959 | { |
| 8960 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 8961 | clk_scaling.suspend_work); |
| 8962 | unsigned long irq_flags; |
| 8963 | |
| 8964 | spin_lock_irqsave(hba->host->host_lock, irq_flags); |
| 8965 | if (hba->clk_scaling.active_reqs || hba->clk_scaling.is_suspended) { |
| 8966 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 8967 | return; |
| 8968 | } |
| 8969 | hba->clk_scaling.is_suspended = true; |
| 8970 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 8971 | |
| 8972 | __ufshcd_suspend_clkscaling(hba); |
| 8973 | } |
| 8974 | |
| 8975 | static void ufshcd_clk_scaling_resume_work(struct work_struct *work) |
| 8976 | { |
| 8977 | struct ufs_hba *hba = container_of(work, struct ufs_hba, |
| 8978 | clk_scaling.resume_work); |
| 8979 | unsigned long irq_flags; |
| 8980 | |
| 8981 | spin_lock_irqsave(hba->host->host_lock, irq_flags); |
| 8982 | if (!hba->clk_scaling.is_suspended) { |
| 8983 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 8984 | return; |
| 8985 | } |
| 8986 | hba->clk_scaling.is_suspended = false; |
| 8987 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 8988 | |
| 8989 | devfreq_resume_device(hba->devfreq); |
| 8990 | } |
| 8991 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 8992 | static int ufshcd_devfreq_target(struct device *dev, |
| 8993 | unsigned long *freq, u32 flags) |
| 8994 | { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8995 | int ret = 0; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 8996 | struct ufs_hba *hba = dev_get_drvdata(dev); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 8997 | unsigned long irq_flags; |
| 8998 | ktime_t start; |
| 8999 | bool scale_up, sched_clk_scaling_suspend_work = false; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9000 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9001 | if (!ufshcd_is_clkscaling_supported(hba)) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9002 | return -EINVAL; |
| 9003 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9004 | if ((*freq > 0) && (*freq < UINT_MAX)) { |
| 9005 | dev_err(hba->dev, "%s: invalid freq = %lu\n", __func__, *freq); |
| 9006 | return -EINVAL; |
| 9007 | } |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9008 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9009 | spin_lock_irqsave(hba->host->host_lock, irq_flags); |
| 9010 | if (ufshcd_eh_in_progress(hba)) { |
| 9011 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 9012 | return 0; |
| 9013 | } |
| 9014 | |
| 9015 | if (!hba->clk_scaling.active_reqs) |
| 9016 | sched_clk_scaling_suspend_work = true; |
| 9017 | |
| 9018 | scale_up = (*freq == UINT_MAX) ? true : false; |
| 9019 | if (!ufshcd_is_devfreq_scaling_required(hba, scale_up)) { |
| 9020 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 9021 | ret = 0; |
| 9022 | goto out; /* no state change required */ |
| 9023 | } |
| 9024 | spin_unlock_irqrestore(hba->host->host_lock, irq_flags); |
| 9025 | |
| 9026 | start = ktime_get(); |
| 9027 | ret = ufshcd_devfreq_scale(hba, scale_up); |
| 9028 | trace_ufshcd_profile_clk_scaling(dev_name(hba->dev), |
| 9029 | (scale_up ? "up" : "down"), |
| 9030 | ktime_to_us(ktime_sub(ktime_get(), start)), ret); |
| 9031 | |
| 9032 | out: |
| 9033 | if (sched_clk_scaling_suspend_work) |
| 9034 | queue_work(hba->clk_scaling.workq, |
| 9035 | &hba->clk_scaling.suspend_work); |
| 9036 | |
| 9037 | return ret; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9038 | } |
| 9039 | |
| 9040 | static int ufshcd_devfreq_get_dev_status(struct device *dev, |
| 9041 | struct devfreq_dev_status *stat) |
| 9042 | { |
| 9043 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 9044 | struct ufs_clk_scaling *scaling = &hba->clk_scaling; |
| 9045 | unsigned long flags; |
| 9046 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9047 | if (!ufshcd_is_clkscaling_supported(hba)) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9048 | return -EINVAL; |
| 9049 | |
| 9050 | memset(stat, 0, sizeof(*stat)); |
| 9051 | |
| 9052 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 9053 | if (!scaling->window_start_t) |
| 9054 | goto start_window; |
| 9055 | |
| 9056 | if (scaling->is_busy_started) |
| 9057 | scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(), |
| 9058 | scaling->busy_start_t)); |
| 9059 | |
| 9060 | stat->total_time = jiffies_to_usecs((long)jiffies - |
| 9061 | (long)scaling->window_start_t); |
| 9062 | stat->busy_time = scaling->tot_busy_t; |
| 9063 | start_window: |
| 9064 | scaling->window_start_t = jiffies; |
| 9065 | scaling->tot_busy_t = 0; |
| 9066 | |
| 9067 | if (hba->outstanding_reqs) { |
| 9068 | scaling->busy_start_t = ktime_get(); |
| 9069 | scaling->is_busy_started = true; |
| 9070 | } else { |
| 9071 | scaling->busy_start_t = ktime_set(0, 0); |
| 9072 | scaling->is_busy_started = false; |
| 9073 | } |
| 9074 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 9075 | return 0; |
| 9076 | } |
| 9077 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9078 | #if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND) |
| 9079 | static struct devfreq_simple_ondemand_data ufshcd_ondemand_data = { |
| 9080 | .upthreshold = 35, |
| 9081 | .downdifferential = 30, |
| 9082 | .simple_scaling = 1, |
| 9083 | }; |
| 9084 | |
| 9085 | static void *gov_data = &ufshcd_ondemand_data; |
| 9086 | #else |
| 9087 | static void *gov_data; |
| 9088 | #endif |
| 9089 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9090 | static struct devfreq_dev_profile ufs_devfreq_profile = { |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9091 | .polling_ms = 40, |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9092 | .target = ufshcd_devfreq_target, |
| 9093 | .get_dev_status = ufshcd_devfreq_get_dev_status, |
| 9094 | }; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9095 | static void ufshcd_clkscaling_init_sysfs(struct ufs_hba *hba) |
| 9096 | { |
| 9097 | hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show; |
| 9098 | hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store; |
| 9099 | sysfs_attr_init(&hba->clk_scaling.enable_attr.attr); |
| 9100 | hba->clk_scaling.enable_attr.attr.name = "clkscale_enable"; |
| 9101 | hba->clk_scaling.enable_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 9102 | if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr)) |
| 9103 | dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n"); |
| 9104 | } |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9105 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9106 | static void ufshcd_init_lanes_per_dir(struct ufs_hba *hba) |
| 9107 | { |
| 9108 | struct device *dev = hba->dev; |
| 9109 | int ret; |
| 9110 | |
| 9111 | ret = of_property_read_u32(dev->of_node, "lanes-per-direction", |
| 9112 | &hba->lanes_per_direction); |
| 9113 | if (ret) { |
| 9114 | dev_dbg(hba->dev, |
| 9115 | "%s: failed to read lanes-per-direction, ret=%d\n", |
| 9116 | __func__, ret); |
| 9117 | hba->lanes_per_direction = UFSHCD_DEFAULT_LANES_PER_DIRECTION; |
| 9118 | } |
| 9119 | } |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 9120 | /** |
| 9121 | * ufshcd_init - Driver initialization routine |
| 9122 | * @hba: per-adapter instance |
| 9123 | * @mmio_base: base register address |
| 9124 | * @irq: Interrupt line of device |
| 9125 | * Returns 0 on success, non-zero value on failure |
| 9126 | */ |
| 9127 | int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq) |
| 9128 | { |
| 9129 | int err; |
| 9130 | struct Scsi_Host *host = hba->host; |
| 9131 | struct device *dev = hba->dev; |
| 9132 | |
| 9133 | if (!mmio_base) { |
| 9134 | dev_err(hba->dev, |
| 9135 | "Invalid memory reference for mmio_base is NULL\n"); |
| 9136 | err = -ENODEV; |
| 9137 | goto out_error; |
| 9138 | } |
| 9139 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9140 | hba->mmio_base = mmio_base; |
| 9141 | hba->irq = irq; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9142 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9143 | ufshcd_init_lanes_per_dir(hba); |
| 9144 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 9145 | err = ufshcd_hba_init(hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 9146 | if (err) |
| 9147 | goto out_error; |
| 9148 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9149 | /* Read capabilities registers */ |
| 9150 | ufshcd_hba_capabilities(hba); |
| 9151 | |
| 9152 | /* Get UFS version supported by the controller */ |
| 9153 | hba->ufs_version = ufshcd_get_ufs_version(hba); |
| 9154 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9155 | /* print error message if ufs_version is not valid */ |
| 9156 | if ((hba->ufs_version != UFSHCI_VERSION_10) && |
| 9157 | (hba->ufs_version != UFSHCI_VERSION_11) && |
| 9158 | (hba->ufs_version != UFSHCI_VERSION_20) && |
| 9159 | (hba->ufs_version != UFSHCI_VERSION_21)) |
| 9160 | dev_err(hba->dev, "invalid UFS version 0x%x\n", |
| 9161 | hba->ufs_version); |
| 9162 | |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 9163 | /* Get Interrupt bit mask per version */ |
| 9164 | hba->intr_mask = ufshcd_get_intr_mask(hba); |
| 9165 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9166 | /* Enable debug prints */ |
| 9167 | hba->ufshcd_dbg_print = DEFAULT_UFSHCD_DBG_PRINT_EN; |
| 9168 | |
Akinobu Mita | ca3d7bf | 2014-07-13 21:24:46 +0900 | [diff] [blame] | 9169 | err = ufshcd_set_dma_mask(hba); |
| 9170 | if (err) { |
| 9171 | dev_err(hba->dev, "set dma mask failed\n"); |
| 9172 | goto out_disable; |
| 9173 | } |
| 9174 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9175 | /* Allocate memory for host memory space */ |
| 9176 | err = ufshcd_memory_alloc(hba); |
| 9177 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9178 | dev_err(hba->dev, "Memory allocation failed\n"); |
| 9179 | goto out_disable; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9180 | } |
| 9181 | |
| 9182 | /* Configure LRB */ |
| 9183 | ufshcd_host_memory_configure(hba); |
| 9184 | |
| 9185 | host->can_queue = hba->nutrs; |
| 9186 | host->cmd_per_lun = hba->nutrs; |
| 9187 | host->max_id = UFSHCD_MAX_ID; |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 9188 | host->max_lun = UFS_MAX_LUNS; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9189 | host->max_channel = UFSHCD_MAX_CHANNEL; |
| 9190 | host->unique_id = host->host_no; |
| 9191 | host->max_cmd_len = MAX_CDB_SIZE; |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9192 | host->set_dbd_for_caching = 1; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9193 | |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 9194 | hba->max_pwr_info.is_valid = false; |
| 9195 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9196 | /* Initailize wait queue for task management */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 9197 | init_waitqueue_head(&hba->tm_wq); |
| 9198 | init_waitqueue_head(&hba->tm_tag_wq); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9199 | |
| 9200 | /* Initialize work queues */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 9201 | INIT_WORK(&hba->eh_work, ufshcd_err_handler); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 9202 | INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9203 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 9204 | /* Initialize UIC command mutex */ |
| 9205 | mutex_init(&hba->uic_cmd_mutex); |
| 9206 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 9207 | /* Initialize mutex for device management commands */ |
| 9208 | mutex_init(&hba->dev_cmd.lock); |
| 9209 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9210 | init_rwsem(&hba->clk_scaling_lock); |
| 9211 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 9212 | /* Initialize device management tag acquire wait queue */ |
| 9213 | init_waitqueue_head(&hba->dev_cmd.tag_wq); |
| 9214 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 9215 | ufshcd_init_clk_gating(hba); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9216 | ufshcd_init_hibern8_on_idle(hba); |
Yaniv Gardi | 199ef13 | 2016-03-10 17:37:06 +0200 | [diff] [blame] | 9217 | |
| 9218 | /* |
| 9219 | * In order to avoid any spurious interrupt immediately after |
| 9220 | * registering UFS controller interrupt handler, clear any pending UFS |
| 9221 | * interrupt status and disable all the UFS interrupts. |
| 9222 | */ |
| 9223 | ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS), |
| 9224 | REG_INTERRUPT_STATUS); |
| 9225 | ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE); |
| 9226 | /* |
| 9227 | * Make sure that UFS interrupts are disabled and any pending interrupt |
| 9228 | * status is cleared before registering UFS interrupt handler. |
| 9229 | */ |
| 9230 | mb(); |
| 9231 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9232 | /* IRQ registration */ |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 9233 | err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9234 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9235 | dev_err(hba->dev, "request irq failed\n"); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 9236 | goto exit_gating; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9237 | } else { |
| 9238 | hba->is_irq_enabled = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9239 | } |
| 9240 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9241 | err = scsi_add_host(host, hba->dev); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9242 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9243 | dev_err(hba->dev, "scsi_add_host failed\n"); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 9244 | goto exit_gating; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9245 | } |
| 9246 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 9247 | /* Host controller enable */ |
| 9248 | err = ufshcd_hba_enable(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9249 | if (err) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 9250 | dev_err(hba->dev, "Host controller enable failed\n"); |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9251 | ufshcd_print_host_regs(hba); |
| 9252 | ufshcd_print_host_state(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9253 | goto out_remove_scsi_host; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9254 | } |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 9255 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9256 | if (ufshcd_is_clkscaling_supported(hba)) { |
| 9257 | char wq_name[sizeof("ufs_clkscaling_00")]; |
| 9258 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9259 | hba->devfreq = devfreq_add_device(dev, &ufs_devfreq_profile, |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9260 | "simple_ondemand", gov_data); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9261 | if (IS_ERR(hba->devfreq)) { |
| 9262 | dev_err(hba->dev, "Unable to register with devfreq %ld\n", |
| 9263 | PTR_ERR(hba->devfreq)); |
Wei Yongjun | 73811c9 | 2016-09-28 14:49:42 +0000 | [diff] [blame] | 9264 | err = PTR_ERR(hba->devfreq); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9265 | goto out_remove_scsi_host; |
| 9266 | } |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9267 | hba->clk_scaling.is_suspended = false; |
| 9268 | |
| 9269 | INIT_WORK(&hba->clk_scaling.suspend_work, |
| 9270 | ufshcd_clk_scaling_suspend_work); |
| 9271 | INIT_WORK(&hba->clk_scaling.resume_work, |
| 9272 | ufshcd_clk_scaling_resume_work); |
| 9273 | |
| 9274 | snprintf(wq_name, ARRAY_SIZE(wq_name), "ufs_clkscaling_%d", |
| 9275 | host->host_no); |
| 9276 | hba->clk_scaling.workq = create_singlethread_workqueue(wq_name); |
| 9277 | |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9278 | /* Suspend devfreq until the UFS device is detected */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9279 | ufshcd_suspend_clkscaling(hba); |
| 9280 | ufshcd_clkscaling_init_sysfs(hba); |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 9281 | } |
| 9282 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9283 | /* |
| 9284 | * If rpm_lvl and and spm_lvl are not already set to valid levels, |
| 9285 | * set the default power management level for UFS runtime and system |
| 9286 | * suspend. Default power saving mode selected is keeping UFS link in |
| 9287 | * Hibern8 state and UFS device in sleep. |
| 9288 | */ |
| 9289 | if (!ufshcd_is_valid_pm_lvl(hba->rpm_lvl)) |
| 9290 | hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state( |
| 9291 | UFS_SLEEP_PWR_MODE, |
| 9292 | UIC_LINK_HIBERN8_STATE); |
| 9293 | if (!ufshcd_is_valid_pm_lvl(hba->spm_lvl)) |
| 9294 | hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state( |
| 9295 | UFS_SLEEP_PWR_MODE, |
| 9296 | UIC_LINK_HIBERN8_STATE); |
| 9297 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 9298 | /* Hold auto suspend until async scan completes */ |
| 9299 | pm_runtime_get_sync(dev); |
| 9300 | |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 9301 | ufshcd_init_latency_hist(hba); |
| 9302 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9303 | /* |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9304 | * We are assuming that device wasn't put in sleep/power-down |
| 9305 | * state exclusively during the boot stage before kernel. |
| 9306 | * This assumption helps avoid doing link startup twice during |
| 9307 | * ufshcd_probe_hba(). |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9308 | */ |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9309 | ufshcd_set_ufs_dev_active(hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9310 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 9311 | async_schedule(ufshcd_async_scan, hba); |
| 9312 | |
Subhash Jadavani | cce6fbc | 2016-08-11 11:35:26 -0700 | [diff] [blame] | 9313 | ufsdbg_add_debugfs(hba); |
| 9314 | |
| 9315 | ufshcd_add_sysfs_nodes(hba); |
| 9316 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9317 | return 0; |
| 9318 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9319 | out_remove_scsi_host: |
| 9320 | scsi_remove_host(hba->host); |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 9321 | exit_gating: |
| 9322 | ufshcd_exit_clk_gating(hba); |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 9323 | ufshcd_exit_latency_hist(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9324 | out_disable: |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 9325 | hba->is_irq_enabled = false; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 9326 | ufshcd_hba_exit(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9327 | out_error: |
| 9328 | return err; |
| 9329 | } |
| 9330 | EXPORT_SYMBOL_GPL(ufshcd_init); |
| 9331 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 9332 | MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>"); |
| 9333 | MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>"); |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 9334 | MODULE_DESCRIPTION("Generic UFS host controller driver Core"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 9335 | MODULE_LICENSE("GPL"); |
| 9336 | MODULE_VERSION(UFSHCD_DRIVER_VERSION); |