blob: 864cba7da932bfce6f6bc8235df0521ef83356d1 [file] [log] [blame]
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301/*
Vinayak Holikattie0eca632013-02-25 21:44:33 +05302 * Universal Flash Storage Host controller driver Core
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303 *
4 * This code is based on drivers/scsi/ufs/ufshcd.c
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305 * Copyright (C) 2011-2013 Samsung India Software Operations
Stephen Boyd9bc70c32017-03-01 16:58:38 -08006 * Copyright (c) 2013-2017, The Linux Foundation. All rights reserved.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307 *
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308 * Authors:
9 * Santosh Yaraganavi <santosh.sy@samsung.com>
10 * Vinayak Holikatti <h.vinayak@samsung.com>
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053011 *
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 Holikatti3b1d0582013-02-25 21:44:32 +053016 * See the COPYING file in the top-level directory or visit
17 * <http://www.gnu.org/licenses/gpl-2.0.html>
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053018 *
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 Holikatti3b1d0582013-02-25 21:44:32 +053024 * 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 Thumma5c0c28a2014-09-25 15:32:21 +030035 *
36 * The Linux Foundation chooses to take subject only to the GPLv2
37 * license terms, and distributes only under these terms.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053038 */
39
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053040#include <linux/async.h>
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -070041#include <scsi/ufs/ioctl.h>
Sahitya Tummala856b3482014-09-25 15:32:34 +030042#include <linux/devfreq.h>
Yaniv Gardib573d482016-03-10 17:37:09 +020043#include <linux/nls.h>
Yaniv Gardi54b879b2016-03-10 17:37:05 +020044#include <linux/of.h>
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -070045#include <linux/blkdev.h>
Vinayak Holikattie0eca632013-02-25 21:44:33 +053046#include "ufshcd.h"
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -070047#include "ufshci.h"
Yaniv Gardic58ab7a2016-03-10 17:37:10 +020048#include "ufs_quirks.h"
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -070049#include "ufs-debugfs.h"
Subhash Jadavani9c807702017-04-01 00:35:51 -070050#include "ufs-qcom.h"
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -070051
52#define CREATE_TRACE_POINTS
53#include <trace/events/ufs.h>
54
55#ifdef CONFIG_DEBUG_FS
56
57static int ufshcd_tag_req_type(struct request *rq)
58{
59 int rq_type = TS_WRITE;
60
61 if (!rq || !(rq->cmd_type & REQ_TYPE_FS))
62 rq_type = TS_NOT_SUPPORTED;
63 else if (rq->cmd_flags & REQ_PREFLUSH)
64 rq_type = TS_FLUSH;
65 else if (rq_data_dir(rq) == READ)
66 rq_type = (rq->cmd_flags & REQ_URGENT) ?
67 TS_URGENT_READ : TS_READ;
68 else if (rq->cmd_flags & REQ_URGENT)
69 rq_type = TS_URGENT_WRITE;
70
71 return rq_type;
72}
73
74static void ufshcd_update_error_stats(struct ufs_hba *hba, int type)
75{
76 ufsdbg_set_err_state(hba);
77 if (type < UFS_ERR_MAX)
78 hba->ufs_stats.err_stats[type]++;
79}
80
81static void ufshcd_update_tag_stats(struct ufs_hba *hba, int tag)
82{
83 struct request *rq =
84 hba->lrb[tag].cmd ? hba->lrb[tag].cmd->request : NULL;
85 u64 **tag_stats = hba->ufs_stats.tag_stats;
86 int rq_type;
87
88 if (!hba->ufs_stats.enabled)
89 return;
90
91 tag_stats[tag][TS_TAG]++;
92 if (!rq || !(rq->cmd_type & REQ_TYPE_FS))
93 return;
94
95 WARN_ON(hba->ufs_stats.q_depth > hba->nutrs);
96 rq_type = ufshcd_tag_req_type(rq);
97 if (!(rq_type < 0 || rq_type > TS_NUM_STATS))
98 tag_stats[hba->ufs_stats.q_depth++][rq_type]++;
99}
100
101static void ufshcd_update_tag_stats_completion(struct ufs_hba *hba,
102 struct scsi_cmnd *cmd)
103{
104 struct request *rq = cmd ? cmd->request : NULL;
105
106 if (rq && rq->cmd_type & REQ_TYPE_FS)
107 hba->ufs_stats.q_depth--;
108}
109
110static void update_req_stats(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
111{
112 int rq_type;
113 struct request *rq = lrbp->cmd ? lrbp->cmd->request : NULL;
114 s64 delta = ktime_us_delta(lrbp->complete_time_stamp,
115 lrbp->issue_time_stamp);
116
117 /* update general request statistics */
118 if (hba->ufs_stats.req_stats[TS_TAG].count == 0)
119 hba->ufs_stats.req_stats[TS_TAG].min = delta;
120 hba->ufs_stats.req_stats[TS_TAG].count++;
121 hba->ufs_stats.req_stats[TS_TAG].sum += delta;
122 if (delta > hba->ufs_stats.req_stats[TS_TAG].max)
123 hba->ufs_stats.req_stats[TS_TAG].max = delta;
124 if (delta < hba->ufs_stats.req_stats[TS_TAG].min)
125 hba->ufs_stats.req_stats[TS_TAG].min = delta;
126
127 rq_type = ufshcd_tag_req_type(rq);
128 if (rq_type == TS_NOT_SUPPORTED)
129 return;
130
131 /* update request type specific statistics */
132 if (hba->ufs_stats.req_stats[rq_type].count == 0)
133 hba->ufs_stats.req_stats[rq_type].min = delta;
134 hba->ufs_stats.req_stats[rq_type].count++;
135 hba->ufs_stats.req_stats[rq_type].sum += delta;
136 if (delta > hba->ufs_stats.req_stats[rq_type].max)
137 hba->ufs_stats.req_stats[rq_type].max = delta;
138 if (delta < hba->ufs_stats.req_stats[rq_type].min)
139 hba->ufs_stats.req_stats[rq_type].min = delta;
140}
141
142static void
143ufshcd_update_query_stats(struct ufs_hba *hba, enum query_opcode opcode, u8 idn)
144{
145 if (opcode < UPIU_QUERY_OPCODE_MAX && idn < MAX_QUERY_IDN)
146 hba->ufs_stats.query_stats_arr[opcode][idn]++;
147}
148
149#else
150static inline void ufshcd_update_tag_stats(struct ufs_hba *hba, int tag)
151{
152}
153
154static inline void ufshcd_update_tag_stats_completion(struct ufs_hba *hba,
155 struct scsi_cmnd *cmd)
156{
157}
158
159static inline void ufshcd_update_error_stats(struct ufs_hba *hba, int type)
160{
161}
162
163static inline
164void update_req_stats(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
165{
166}
167
168static inline
169void ufshcd_update_query_stats(struct ufs_hba *hba,
170 enum query_opcode opcode, u8 idn)
171{
172}
173#endif
174
175#define UFSHCD_REQ_SENSE_SIZE 18
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530176
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530177#define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
178 UTP_TASK_REQ_COMPL |\
179 UFSHCD_ERROR_MASK)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530180/* UIC command timeout, unit: ms */
181#define UIC_CMD_TIMEOUT 500
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530182
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530183/* NOP OUT retries waiting for NOP IN response */
184#define NOP_OUT_RETRIES 10
185/* Timeout after 30 msecs if NOP OUT hangs without response */
186#define NOP_OUT_TIMEOUT 30 /* msecs */
187
Dolev Raviv68078d52013-07-30 00:35:58 +0530188/* Query request retries */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700189#define QUERY_REQ_RETRIES 3
Dolev Raviv68078d52013-07-30 00:35:58 +0530190/* Query request timeout */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700191#define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */
Dolev Raviv68078d52013-07-30 00:35:58 +0530192
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530193/* Task management command timeout */
194#define TM_CMD_TIMEOUT 100 /* msecs */
195
Yaniv Gardi64238fb2016-02-01 15:02:43 +0200196/* maximum number of retries for a general UIC command */
197#define UFS_UIC_COMMAND_RETRIES 3
198
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300199/* maximum number of link-startup retries */
200#define DME_LINKSTARTUP_RETRIES 3
201
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +0200202/* Maximum retries for Hibern8 enter */
203#define UIC_HIBERN8_ENTER_RETRIES 3
204
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300205/* maximum number of reset retries before giving up */
206#define MAX_HOST_RESET_RETRIES 5
207
Dolev Raviv68078d52013-07-30 00:35:58 +0530208/* Expose the flag value from utp_upiu_query.value */
209#define MASK_QUERY_UPIU_FLAG_LOC 0xFF
210
Seungwon Jeon7d568652013-08-31 21:40:20 +0530211/* Interrupt aggregation default timeout, unit: 40us */
212#define INT_AGGR_DEF_TO 0x02
213
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700214/* default value of auto suspend is 3 seconds */
215#define UFSHCD_AUTO_SUSPEND_DELAY_MS 3000 /* millisecs */
216
217#define UFSHCD_CLK_GATING_DELAY_MS_PWR_SAVE 10
218#define UFSHCD_CLK_GATING_DELAY_MS_PERF 50
219
220/* IOCTL opcode for command - ufs set device read only */
221#define UFS_IOCTL_BLKROSET BLKROSET
222
223#define UFSHCD_DEFAULT_LANES_PER_DIRECTION 2
224
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +0300225#define ufshcd_toggle_vreg(_dev, _vreg, _on) \
226 ({ \
227 int _ret; \
228 if (_on) \
229 _ret = ufshcd_enable_vreg(_dev, _vreg); \
230 else \
231 _ret = ufshcd_disable_vreg(_dev, _vreg); \
232 _ret; \
233 })
234
Subhash Jadavani4386e022016-12-19 13:01:56 -0800235static void ufshcd_hex_dump(struct ufs_hba *hba, const char * const str,
236 const void *buf, size_t len)
237
238{
239 /*
240 * device name is expected to take up ~20 characters and "str" passed
241 * to this function is expected to be of ~10 character so we would need
242 * ~30 characters string to hold the concatenation of these 2 strings.
243 */
244 #define MAX_PREFIX_STR_SIZE 50
245 char prefix_str[MAX_PREFIX_STR_SIZE] = {0};
246
247 /* concatenate the device name and "str" */
248 snprintf(prefix_str, MAX_PREFIX_STR_SIZE, "%s %s: ",
249 dev_name(hba->dev), str);
250 print_hex_dump(KERN_ERR, prefix_str, DUMP_PREFIX_OFFSET,
251 16, 4, buf, len, false);
252}
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700253
Subhash Jadavanida461ce2014-09-25 15:32:25 +0300254static u32 ufs_query_desc_max_size[] = {
255 QUERY_DESC_DEVICE_MAX_SIZE,
256 QUERY_DESC_CONFIGURAION_MAX_SIZE,
257 QUERY_DESC_UNIT_MAX_SIZE,
258 QUERY_DESC_RFU_MAX_SIZE,
259 QUERY_DESC_INTERCONNECT_MAX_SIZE,
260 QUERY_DESC_STRING_MAX_SIZE,
261 QUERY_DESC_RFU_MAX_SIZE,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700262 QUERY_DESC_GEOMETRY_MAZ_SIZE,
Subhash Jadavanida461ce2014-09-25 15:32:25 +0300263 QUERY_DESC_POWER_MAX_SIZE,
264 QUERY_DESC_RFU_MAX_SIZE,
265};
266
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530267enum {
268 UFSHCD_MAX_CHANNEL = 0,
269 UFSHCD_MAX_ID = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530270 UFSHCD_CMD_PER_LUN = 32,
271 UFSHCD_CAN_QUEUE = 32,
272};
273
274/* UFSHCD states */
275enum {
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530276 UFSHCD_STATE_RESET,
277 UFSHCD_STATE_ERROR,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530278 UFSHCD_STATE_OPERATIONAL,
279};
280
281/* UFSHCD error handling flags */
282enum {
283 UFSHCD_EH_IN_PROGRESS = (1 << 0),
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530284};
285
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530286/* UFSHCD UIC layer error flags */
287enum {
288 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +0200289 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */
290 UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */
291 UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */
292 UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */
293 UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530294};
295
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530296/* Interrupt configuration options */
297enum {
298 UFSHCD_INT_DISABLE,
299 UFSHCD_INT_ENABLE,
300 UFSHCD_INT_CLEAR,
301};
302
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700303#define DEFAULT_UFSHCD_DBG_PRINT_EN UFSHCD_DBG_PRINT_ALL
304
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530305#define ufshcd_set_eh_in_progress(h) \
306 (h->eh_flags |= UFSHCD_EH_IN_PROGRESS)
307#define ufshcd_eh_in_progress(h) \
308 (h->eh_flags & UFSHCD_EH_IN_PROGRESS)
309#define ufshcd_clear_eh_in_progress(h) \
310 (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
311
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300312#define ufshcd_set_ufs_dev_active(h) \
313 ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE)
314#define ufshcd_set_ufs_dev_sleep(h) \
315 ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE)
316#define ufshcd_set_ufs_dev_poweroff(h) \
317 ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE)
318#define ufshcd_is_ufs_dev_active(h) \
319 ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE)
320#define ufshcd_is_ufs_dev_sleep(h) \
321 ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE)
322#define ufshcd_is_ufs_dev_poweroff(h) \
323 ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE)
324
325static struct ufs_pm_lvl_states ufs_pm_lvl_states[] = {
326 {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE},
327 {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE},
328 {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE},
329 {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE},
330 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE},
331 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE},
332};
333
334static inline enum ufs_dev_pwr_mode
335ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl)
336{
337 return ufs_pm_lvl_states[lvl].dev_state;
338}
339
340static inline enum uic_link_state
341ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl)
342{
343 return ufs_pm_lvl_states[lvl].link_state;
344}
345
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700346static inline enum ufs_pm_level
347ufs_get_desired_pm_lvl_for_dev_link_state(enum ufs_dev_pwr_mode dev_state,
348 enum uic_link_state link_state)
349{
350 enum ufs_pm_level lvl;
351
352 for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) {
353 if ((ufs_pm_lvl_states[lvl].dev_state == dev_state) &&
354 (ufs_pm_lvl_states[lvl].link_state == link_state))
355 return lvl;
356 }
357
358 /* if no match found, return the level 0 */
359 return UFS_PM_LVL_0;
360}
361
362static inline bool ufshcd_is_valid_pm_lvl(int lvl)
363{
364 if (lvl >= 0 && lvl < ARRAY_SIZE(ufs_pm_lvl_states))
365 return true;
366 else
367 return false;
368}
369
370static irqreturn_t ufshcd_intr(int irq, void *__hba);
Subhash Jadavani9c807702017-04-01 00:35:51 -0700371static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530372static void ufshcd_async_scan(void *data, async_cookie_t cookie);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530373static int ufshcd_reset_and_restore(struct ufs_hba *hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700374static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530375static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300376static void ufshcd_hba_exit(struct ufs_hba *hba);
377static int ufshcd_probe_hba(struct ufs_hba *hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700378static int ufshcd_enable_clocks(struct ufs_hba *hba);
379static int ufshcd_disable_clocks(struct ufs_hba *hba,
380 bool is_gating_context);
381static int ufshcd_disable_clocks_skip_ref_clk(struct ufs_hba *hba,
382 bool is_gating_context);
Yaniv Gardi60f01872016-03-10 17:37:11 +0200383static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused);
Yaniv Gardicad2e032015-03-31 17:37:14 +0300384static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700385static inline void ufshcd_save_tstamp_of_last_dme_cmd(struct ufs_hba *hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300386static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700387static void ufshcd_resume_clkscaling(struct ufs_hba *hba);
388static void ufshcd_suspend_clkscaling(struct ufs_hba *hba);
389static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba);
390static void ufshcd_release_all(struct ufs_hba *hba);
391static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba);
392static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba);
Subhash Jadavani9c807702017-04-01 00:35:51 -0700393static int ufshcd_devfreq_target(struct device *dev,
394 unsigned long *freq, u32 flags);
395static int ufshcd_devfreq_get_dev_status(struct device *dev,
396 struct devfreq_dev_status *stat);
397
398#if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
399static struct devfreq_simple_ondemand_data ufshcd_ondemand_data = {
400 .upthreshold = 35,
401 .downdifferential = 30,
402 .simple_scaling = 1,
403};
404
405static void *gov_data = &ufshcd_ondemand_data;
406#else
407static void *gov_data;
408#endif
409
410static struct devfreq_dev_profile ufs_devfreq_profile = {
411 .polling_ms = 40,
412 .target = ufshcd_devfreq_target,
413 .get_dev_status = ufshcd_devfreq_get_dev_status,
414};
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700415
Yaniv Gardi14497322016-02-01 15:02:39 +0200416static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag)
417{
418 return tag >= 0 && tag < hba->nutrs;
419}
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300420
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700421static inline void ufshcd_enable_irq(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300422{
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300423 if (!hba->is_irq_enabled) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700424 enable_irq(hba->irq);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300425 hba->is_irq_enabled = true;
426 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300427}
428
429static inline void ufshcd_disable_irq(struct ufs_hba *hba)
430{
431 if (hba->is_irq_enabled) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700432 disable_irq(hba->irq);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300433 hba->is_irq_enabled = false;
434 }
435}
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530436
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700437void ufshcd_scsi_unblock_requests(struct ufs_hba *hba)
438{
439 unsigned long flags;
440 bool unblock = false;
441
442 spin_lock_irqsave(hba->host->host_lock, flags);
443 hba->scsi_block_reqs_cnt--;
444 unblock = !hba->scsi_block_reqs_cnt;
445 spin_unlock_irqrestore(hba->host->host_lock, flags);
446 if (unblock)
447 scsi_unblock_requests(hba->host);
448}
449EXPORT_SYMBOL(ufshcd_scsi_unblock_requests);
450
451static inline void __ufshcd_scsi_block_requests(struct ufs_hba *hba)
452{
453 if (!hba->scsi_block_reqs_cnt++)
454 scsi_block_requests(hba->host);
455}
456
457void ufshcd_scsi_block_requests(struct ufs_hba *hba)
458{
459 unsigned long flags;
460
461 spin_lock_irqsave(hba->host->host_lock, flags);
462 __ufshcd_scsi_block_requests(hba);
463 spin_unlock_irqrestore(hba->host->host_lock, flags);
464}
465EXPORT_SYMBOL(ufshcd_scsi_block_requests);
466
Subhash Jadavani9c807702017-04-01 00:35:51 -0700467static int ufshcd_device_reset_ctrl(struct ufs_hba *hba, bool ctrl)
468{
469 int ret = 0;
470
471 if (!hba->pctrl)
472 return 0;
473
474 /* Assert reset if ctrl == true */
475 if (ctrl)
476 ret = pinctrl_select_state(hba->pctrl,
477 pinctrl_lookup_state(hba->pctrl, "dev-reset-assert"));
478 else
479 ret = pinctrl_select_state(hba->pctrl,
480 pinctrl_lookup_state(hba->pctrl, "dev-reset-deassert"));
481
482 if (ret < 0)
483 dev_err(hba->dev, "%s: %s failed with err %d\n",
484 __func__, ctrl ? "Assert" : "Deassert", ret);
485
486 return ret;
487}
488
489static inline int ufshcd_assert_device_reset(struct ufs_hba *hba)
490{
491 return ufshcd_device_reset_ctrl(hba, true);
492}
493
494static inline int ufshcd_deassert_device_reset(struct ufs_hba *hba)
495{
496 return ufshcd_device_reset_ctrl(hba, false);
497}
498
499static int ufshcd_reset_device(struct ufs_hba *hba)
500{
501 int ret;
502
503 /* reset the connected UFS device */
504 ret = ufshcd_assert_device_reset(hba);
505 if (ret)
506 goto out;
507 /*
508 * The reset signal is active low.
509 * The UFS device shall detect more than or equal to 1us of positive
510 * or negative RST_n pulse width.
511 * To be on safe side, keep the reset low for atleast 10us.
512 */
513 usleep_range(10, 15);
514
515 ret = ufshcd_deassert_device_reset(hba);
516 if (ret)
517 goto out;
518 /* same as assert, wait for atleast 10us after deassert */
519 usleep_range(10, 15);
520out:
521 return ret;
522}
523
Yaniv Gardib573d482016-03-10 17:37:09 +0200524/* replace non-printable or non-ASCII characters with spaces */
525static inline void ufshcd_remove_non_printable(char *val)
526{
527 if (!val)
528 return;
529
530 if (*val < 0x20 || *val > 0x7e)
531 *val = ' ';
532}
533
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700534#ifdef CONFIG_TRACEPOINTS
535static void ufshcd_add_command_trace(struct ufs_hba *hba,
536 unsigned int tag, const char *str)
537{
538 sector_t lba = -1;
539 u8 opcode = 0;
540 u32 intr, doorbell;
541 struct ufshcd_lrb *lrbp;
542 int transfer_len = -1;
543
544 lrbp = &hba->lrb[tag];
545
546 if (lrbp->cmd) { /* data phase exists */
547 opcode = (u8)(*lrbp->cmd->cmnd);
548 if ((opcode == READ_10) || (opcode == WRITE_10)) {
549 /*
550 * Currently we only fully trace read(10) and write(10)
551 * commands
552 */
553 if (lrbp->cmd->request && lrbp->cmd->request->bio)
554 lba =
555 lrbp->cmd->request->bio->bi_iter.bi_sector;
556 transfer_len = be32_to_cpu(
557 lrbp->ucd_req_ptr->sc.exp_data_transfer_len);
558 }
559 }
560
561 intr = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
562 doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
563 trace_ufshcd_command(dev_name(hba->dev), str, tag,
564 doorbell, transfer_len, intr, lba, opcode);
565}
566
567static inline void ufshcd_cond_add_cmd_trace(struct ufs_hba *hba,
568 unsigned int tag, const char *str)
569{
570 if (trace_ufshcd_command_enabled())
571 ufshcd_add_command_trace(hba, tag, str);
572}
573#else
574static inline void ufshcd_cond_add_cmd_trace(struct ufs_hba *hba,
575 unsigned int tag, const char *str)
576{
577}
578#endif
579
580static void ufshcd_print_clk_freqs(struct ufs_hba *hba)
581{
582 struct ufs_clk_info *clki;
583 struct list_head *head = &hba->clk_list_head;
584
585 if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_CLK_FREQ_EN))
586 return;
587
588 if (!head || list_empty(head))
589 return;
590
591 list_for_each_entry(clki, head, list) {
592 if (!IS_ERR_OR_NULL(clki->clk) && clki->min_freq &&
593 clki->max_freq)
594 dev_err(hba->dev, "clk: %s, rate: %u\n",
595 clki->name, clki->curr_freq);
596 }
597}
598
599static void ufshcd_print_uic_err_hist(struct ufs_hba *hba,
600 struct ufs_uic_err_reg_hist *err_hist, char *err_name)
601{
602 int i;
603
604 if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_UIC_ERR_HIST_EN))
605 return;
606
607 for (i = 0; i < UIC_ERR_REG_HIST_LENGTH; i++) {
608 int p = (i + err_hist->pos - 1) % UIC_ERR_REG_HIST_LENGTH;
609
610 if (err_hist->reg[p] == 0)
611 continue;
612 dev_err(hba->dev, "%s[%d] = 0x%x at %lld us", err_name, i,
613 err_hist->reg[p], ktime_to_us(err_hist->tstamp[p]));
614 }
615}
616
Subhash Jadavani9c807702017-04-01 00:35:51 -0700617static inline void __ufshcd_print_host_regs(struct ufs_hba *hba, bool no_sleep)
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700618{
619 if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_HOST_REGS_EN))
620 return;
621
622 /*
623 * hex_dump reads its data without the readl macro. This might
624 * cause inconsistency issues on some platform, as the printed
625 * values may be from cache and not the most recent value.
626 * To know whether you are looking at an un-cached version verify
627 * that IORESOURCE_MEM flag is on when xxx_get_resource() is invoked
628 * during platform/pci probe function.
629 */
Subhash Jadavani4386e022016-12-19 13:01:56 -0800630 ufshcd_hex_dump(hba, "host regs", hba->mmio_base,
631 UFSHCI_REG_SPACE_SIZE);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700632 dev_err(hba->dev, "hba->ufs_version = 0x%x, hba->capabilities = 0x%x",
633 hba->ufs_version, hba->capabilities);
634 dev_err(hba->dev,
635 "hba->outstanding_reqs = 0x%x, hba->outstanding_tasks = 0x%x",
636 (u32)hba->outstanding_reqs, (u32)hba->outstanding_tasks);
637 dev_err(hba->dev,
638 "last_hibern8_exit_tstamp at %lld us, hibern8_exit_cnt = %d",
639 ktime_to_us(hba->ufs_stats.last_hibern8_exit_tstamp),
640 hba->ufs_stats.hibern8_exit_cnt);
641
642 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.pa_err, "pa_err");
643 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.dl_err, "dl_err");
644 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.nl_err, "nl_err");
645 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.tl_err, "tl_err");
646 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.dme_err, "dme_err");
647
648 ufshcd_print_clk_freqs(hba);
649
Subhash Jadavani9c807702017-04-01 00:35:51 -0700650 ufshcd_vops_dbg_register_dump(hba, no_sleep);
651}
652
653static void ufshcd_print_host_regs(struct ufs_hba *hba)
654{
655 __ufshcd_print_host_regs(hba, false);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700656}
657
658static
659void ufshcd_print_trs(struct ufs_hba *hba, unsigned long bitmap, bool pr_prdt)
660{
661 struct ufshcd_lrb *lrbp;
662 int prdt_length;
663 int tag;
664
665 if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_TRS_EN))
666 return;
667
668 for_each_set_bit(tag, &bitmap, hba->nutrs) {
669 lrbp = &hba->lrb[tag];
670
671 dev_err(hba->dev, "UPIU[%d] - issue time %lld us",
672 tag, ktime_to_us(lrbp->issue_time_stamp));
673 dev_err(hba->dev,
674 "UPIU[%d] - Transfer Request Descriptor phys@0x%llx",
675 tag, (u64)lrbp->utrd_dma_addr);
Subhash Jadavani4386e022016-12-19 13:01:56 -0800676 ufshcd_hex_dump(hba, "UPIU TRD", lrbp->utr_descriptor_ptr,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700677 sizeof(struct utp_transfer_req_desc));
678 dev_err(hba->dev, "UPIU[%d] - Request UPIU phys@0x%llx", tag,
679 (u64)lrbp->ucd_req_dma_addr);
Subhash Jadavani4386e022016-12-19 13:01:56 -0800680 ufshcd_hex_dump(hba, "UPIU REQ", lrbp->ucd_req_ptr,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700681 sizeof(struct utp_upiu_req));
682 dev_err(hba->dev, "UPIU[%d] - Response UPIU phys@0x%llx", tag,
683 (u64)lrbp->ucd_rsp_dma_addr);
Subhash Jadavani4386e022016-12-19 13:01:56 -0800684 ufshcd_hex_dump(hba, "UPIU RSP", lrbp->ucd_rsp_ptr,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700685 sizeof(struct utp_upiu_rsp));
686 prdt_length =
687 le16_to_cpu(lrbp->utr_descriptor_ptr->prd_table_length);
688 dev_err(hba->dev, "UPIU[%d] - PRDT - %d entries phys@0x%llx",
689 tag, prdt_length, (u64)lrbp->ucd_prdt_dma_addr);
690 if (pr_prdt)
Subhash Jadavani4386e022016-12-19 13:01:56 -0800691 ufshcd_hex_dump(hba, "UPIU PRDT", lrbp->ucd_prdt_ptr,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700692 sizeof(struct ufshcd_sg_entry) * prdt_length);
693 }
694}
695
696static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap)
697{
698 struct utp_task_req_desc *tmrdp;
699 int tag;
700
701 if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_TMRS_EN))
702 return;
703
704 for_each_set_bit(tag, &bitmap, hba->nutmrs) {
705 tmrdp = &hba->utmrdl_base_addr[tag];
706 dev_err(hba->dev, "TM[%d] - Task Management Header", tag);
Subhash Jadavani4386e022016-12-19 13:01:56 -0800707 ufshcd_hex_dump(hba, "TM TRD", &tmrdp->header,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700708 sizeof(struct request_desc_header));
709 dev_err(hba->dev, "TM[%d] - Task Management Request UPIU",
710 tag);
Subhash Jadavani4386e022016-12-19 13:01:56 -0800711 ufshcd_hex_dump(hba, "TM REQ", tmrdp->task_req_upiu,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700712 sizeof(struct utp_upiu_req));
713 dev_err(hba->dev, "TM[%d] - Task Management Response UPIU",
714 tag);
Subhash Jadavani4386e022016-12-19 13:01:56 -0800715 ufshcd_hex_dump(hba, "TM RSP", tmrdp->task_rsp_upiu,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700716 sizeof(struct utp_task_req_desc));
717 }
718}
719
720static void ufshcd_print_host_state(struct ufs_hba *hba)
721{
722 if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_HOST_STATE_EN))
723 return;
724
725 dev_err(hba->dev, "UFS Host state=%d\n", hba->ufshcd_state);
726 dev_err(hba->dev, "lrb in use=0x%lx, outstanding reqs=0x%lx tasks=0x%lx\n",
727 hba->lrb_in_use, hba->outstanding_tasks, hba->outstanding_reqs);
728 dev_err(hba->dev, "saved_err=0x%x, saved_uic_err=0x%x, saved_ce_err=0x%x\n",
729 hba->saved_err, hba->saved_uic_err, hba->saved_ce_err);
730 dev_err(hba->dev, "Device power mode=%d, UIC link state=%d\n",
731 hba->curr_dev_pwr_mode, hba->uic_link_state);
732 dev_err(hba->dev, "PM in progress=%d, sys. suspended=%d\n",
733 hba->pm_op_in_progress, hba->is_sys_suspended);
734 dev_err(hba->dev, "Auto BKOPS=%d, Host self-block=%d\n",
735 hba->auto_bkops_enabled, hba->host->host_self_blocked);
736 dev_err(hba->dev, "Clk gate=%d, hibern8 on idle=%d\n",
737 hba->clk_gating.state, hba->hibern8_on_idle.state);
738 dev_err(hba->dev, "error handling flags=0x%x, req. abort count=%d\n",
739 hba->eh_flags, hba->req_abort_count);
740 dev_err(hba->dev, "Host capabilities=0x%x, caps=0x%x\n",
741 hba->capabilities, hba->caps);
742 dev_err(hba->dev, "quirks=0x%x, dev. quirks=0x%x\n", hba->quirks,
Subhash Jadavani4f0df17b2016-12-16 13:19:27 -0800743 hba->dev_info.quirks);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700744}
745
746/**
747 * ufshcd_print_pwr_info - print power params as saved in hba
748 * power info
749 * @hba: per-adapter instance
750 */
751static void ufshcd_print_pwr_info(struct ufs_hba *hba)
752{
753 char *names[] = {
754 "INVALID MODE",
755 "FAST MODE",
756 "SLOW_MODE",
757 "INVALID MODE",
758 "FASTAUTO_MODE",
759 "SLOWAUTO_MODE",
760 "INVALID MODE",
761 };
762
763 if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_PWR_EN))
764 return;
765
766 dev_err(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n",
767 __func__,
768 hba->pwr_info.gear_rx, hba->pwr_info.gear_tx,
769 hba->pwr_info.lane_rx, hba->pwr_info.lane_tx,
770 names[hba->pwr_info.pwr_rx],
771 names[hba->pwr_info.pwr_tx],
772 hba->pwr_info.hs_rate);
773}
774
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530775/*
776 * ufshcd_wait_for_register - wait for register value to change
777 * @hba - per-adapter interface
778 * @reg - mmio register offset
779 * @mask - mask to apply to read register value
780 * @val - wait condition
781 * @interval_us - polling interval in microsecs
782 * @timeout_ms - timeout in millisecs
Yaniv Gardi596585a2016-03-10 17:37:08 +0200783 * @can_sleep - perform sleep or just spin
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530784 * Returns -ETIMEDOUT on error, zero on success
785 */
Yaniv Gardi596585a2016-03-10 17:37:08 +0200786int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
787 u32 val, unsigned long interval_us,
788 unsigned long timeout_ms, bool can_sleep)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530789{
790 int err = 0;
791 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
792
793 /* ignore bits that we don't intend to wait on */
794 val = val & mask;
795
796 while ((ufshcd_readl(hba, reg) & mask) != val) {
Yaniv Gardi596585a2016-03-10 17:37:08 +0200797 if (can_sleep)
798 usleep_range(interval_us, interval_us + 50);
799 else
800 udelay(interval_us);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530801 if (time_after(jiffies, timeout)) {
802 if ((ufshcd_readl(hba, reg) & mask) != val)
803 err = -ETIMEDOUT;
804 break;
805 }
806 }
807
808 return err;
809}
810
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530811/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530812 * ufshcd_get_intr_mask - Get the interrupt bit mask
813 * @hba - Pointer to adapter instance
814 *
815 * Returns interrupt bit mask per version
816 */
817static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
818{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700819 u32 intr_mask = 0;
820
821 switch (hba->ufs_version) {
822 case UFSHCI_VERSION_10:
823 intr_mask = INTERRUPT_MASK_ALL_VER_10;
824 break;
825 /* allow fall through */
826 case UFSHCI_VERSION_11:
827 case UFSHCI_VERSION_20:
828 intr_mask = INTERRUPT_MASK_ALL_VER_11;
829 break;
830 /* allow fall through */
831 case UFSHCI_VERSION_21:
832 default:
833 intr_mask = INTERRUPT_MASK_ALL_VER_21;
834 }
835
836 if (!ufshcd_is_crypto_supported(hba))
837 intr_mask &= ~CRYPTO_ENGINE_FATAL_ERROR;
838
839 return intr_mask;
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530840}
841
842/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530843 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
844 * @hba - Pointer to adapter instance
845 *
846 * Returns UFSHCI version supported by the controller
847 */
848static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
849{
Yaniv Gardi0263bcd2015-10-28 13:15:48 +0200850 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
851 return ufshcd_vops_get_ufs_hci_version(hba);
Yaniv Gardi9949e702015-05-17 18:55:05 +0300852
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530853 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530854}
855
856/**
857 * ufshcd_is_device_present - Check if any device connected to
858 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300859 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530860 *
Venkatraman S73ec5132012-07-10 19:39:23 +0530861 * Returns 1 if device present, 0 if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530862 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300863static inline int ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530864{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300865 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
866 DEVICE_PRESENT) ? 1 : 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530867}
868
869/**
870 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
871 * @lrb: pointer to local command reference block
872 *
873 * This function is used to get the OCS field from UTRD
874 * Returns the OCS field in the UTRD
875 */
876static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
877{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530878 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530879}
880
881/**
882 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
883 * @task_req_descp: pointer to utp_task_req_desc structure
884 *
885 * This function is used to get the OCS field from UTMRD
886 * Returns the OCS field in the UTMRD
887 */
888static inline int
889ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
890{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530891 return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530892}
893
894/**
895 * ufshcd_get_tm_free_slot - get a free slot for task management request
896 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530897 * @free_slot: pointer to variable with available slot value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530898 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530899 * Get a free tag and lock it until ufshcd_put_tm_slot() is called.
900 * Returns 0 if free slot is not available, else return 1 with tag value
901 * in @free_slot.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530902 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530903static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530904{
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530905 int tag;
906 bool ret = false;
907
908 if (!free_slot)
909 goto out;
910
911 do {
912 tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs);
913 if (tag >= hba->nutmrs)
914 goto out;
915 } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use));
916
917 *free_slot = tag;
918 ret = true;
919out:
920 return ret;
921}
922
923static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot)
924{
925 clear_bit_unlock(slot, &hba->tm_slots_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530926}
927
928/**
929 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
930 * @hba: per adapter instance
931 * @pos: position of the bit to be cleared
932 */
933static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
934{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530935 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530936}
937
938/**
Yaniv Gardia48353f2016-02-01 15:02:40 +0200939 * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field
940 * @hba: per adapter instance
941 * @tag: position of the bit to be cleared
942 */
943static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)
944{
945 __clear_bit(tag, &hba->outstanding_reqs);
946}
947
948/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530949 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
950 * @reg: Register value of host controller status
951 *
952 * Returns integer, 0 on Success and positive value if failed
953 */
954static inline int ufshcd_get_lists_status(u32 reg)
955{
956 /*
957 * The mask 0xFF is for the following HCS register bits
958 * Bit Description
959 * 0 Device Present
960 * 1 UTRLRDY
961 * 2 UTMRLRDY
962 * 3 UCRDY
Yaniv Gardi897efe62016-02-01 15:02:48 +0200963 * 4-7 reserved
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530964 */
Yaniv Gardi897efe62016-02-01 15:02:48 +0200965 return ((reg & 0xFF) >> 1) ^ 0x07;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530966}
967
968/**
969 * ufshcd_get_uic_cmd_result - Get the UIC command result
970 * @hba: Pointer to adapter instance
971 *
972 * This function gets the result of UIC command completion
973 * Returns 0 on success, non zero value on error
974 */
975static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
976{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530977 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530978 MASK_UIC_COMMAND_RESULT;
979}
980
981/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530982 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
983 * @hba: Pointer to adapter instance
984 *
985 * This function gets UIC command argument3
986 * Returns 0 on success, non zero value on error
987 */
988static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
989{
990 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
991}
992
993/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530994 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530995 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530996 */
997static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530998ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530999{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301000 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301001}
1002
1003/**
1004 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
1005 * @ucd_rsp_ptr: pointer to response UPIU
1006 *
1007 * This function gets the response status and scsi_status from response UPIU
1008 * Returns the response result code.
1009 */
1010static inline int
1011ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
1012{
1013 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
1014}
1015
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05301016/*
1017 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
1018 * from response UPIU
1019 * @ucd_rsp_ptr: pointer to response UPIU
1020 *
1021 * Return the data segment length.
1022 */
1023static inline unsigned int
1024ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
1025{
1026 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
1027 MASK_RSP_UPIU_DATA_SEG_LEN;
1028}
1029
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301030/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301031 * ufshcd_is_exception_event - Check if the device raised an exception event
1032 * @ucd_rsp_ptr: pointer to response UPIU
1033 *
1034 * The function checks if the device raised an exception event indicated in
1035 * the Device Information field of response UPIU.
1036 *
1037 * Returns true if exception is raised, false otherwise.
1038 */
1039static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
1040{
1041 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
1042 MASK_RSP_EXCEPTION_EVENT ? true : false;
1043}
1044
1045/**
Seungwon Jeon7d568652013-08-31 21:40:20 +05301046 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301047 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301048 */
1049static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +05301050ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301051{
Seungwon Jeon7d568652013-08-31 21:40:20 +05301052 ufshcd_writel(hba, INT_AGGR_ENABLE |
1053 INT_AGGR_COUNTER_AND_TIMER_RESET,
1054 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
1055}
1056
1057/**
1058 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
1059 * @hba: per adapter instance
1060 * @cnt: Interrupt aggregation counter threshold
1061 * @tmout: Interrupt aggregation timeout value
1062 */
1063static inline void
1064ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
1065{
1066 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
1067 INT_AGGR_COUNTER_THLD_VAL(cnt) |
1068 INT_AGGR_TIMEOUT_VAL(tmout),
1069 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301070}
1071
1072/**
Yaniv Gardib8521902015-05-17 18:54:57 +03001073 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
1074 * @hba: per adapter instance
1075 */
1076static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
1077{
1078 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
1079}
1080
1081/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301082 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
1083 * When run-stop registers are set to 1, it indicates the
1084 * host controller that it can process the requests
1085 * @hba: per adapter instance
1086 */
1087static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
1088{
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301089 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
1090 REG_UTP_TASK_REQ_LIST_RUN_STOP);
1091 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
1092 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301093}
1094
1095/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301096 * ufshcd_hba_start - Start controller initialization sequence
1097 * @hba: per adapter instance
1098 */
1099static inline void ufshcd_hba_start(struct ufs_hba *hba)
1100{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001101 u32 val = CONTROLLER_ENABLE;
1102
1103 if (ufshcd_is_crypto_supported(hba))
1104 val |= CRYPTO_GENERAL_ENABLE;
1105 ufshcd_writel(hba, val, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301106}
1107
1108/**
1109 * ufshcd_is_hba_active - Get controller state
1110 * @hba: per adapter instance
1111 *
1112 * Returns zero if controller is active, 1 otherwise
1113 */
1114static inline int ufshcd_is_hba_active(struct ufs_hba *hba)
1115{
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301116 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301117}
1118
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001119static const char *ufschd_uic_link_state_to_string(
1120 enum uic_link_state state)
1121{
1122 switch (state) {
1123 case UIC_LINK_OFF_STATE: return "OFF";
1124 case UIC_LINK_ACTIVE_STATE: return "ACTIVE";
1125 case UIC_LINK_HIBERN8_STATE: return "HIBERN8";
1126 default: return "UNKNOWN";
1127 }
1128}
1129
1130static const char *ufschd_ufs_dev_pwr_mode_to_string(
1131 enum ufs_dev_pwr_mode state)
1132{
1133 switch (state) {
1134 case UFS_ACTIVE_PWR_MODE: return "ACTIVE";
1135 case UFS_SLEEP_PWR_MODE: return "SLEEP";
1136 case UFS_POWERDOWN_PWR_MODE: return "POWERDOWN";
1137 default: return "UNKNOWN";
1138 }
1139}
1140
Yaniv Gardi37113102016-03-10 17:37:16 +02001141u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba)
1142{
1143 /* HCI version 1.0 and 1.1 supports UniPro 1.41 */
1144 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
1145 (hba->ufs_version == UFSHCI_VERSION_11))
1146 return UFS_UNIPRO_VER_1_41;
1147 else
1148 return UFS_UNIPRO_VER_1_6;
1149}
1150EXPORT_SYMBOL(ufshcd_get_local_unipro_ver);
1151
1152static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba)
1153{
1154 /*
1155 * If both host and device support UniPro ver1.6 or later, PA layer
1156 * parameters tuning happens during link startup itself.
1157 *
1158 * We can manually tune PA layer parameters if either host or device
1159 * doesn't support UniPro ver 1.6 or later. But to keep manual tuning
1160 * logic simple, we will only do manual tuning if local unipro version
1161 * doesn't support ver1.6 or later.
1162 */
1163 if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6)
1164 return true;
1165 else
1166 return false;
1167}
1168
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001169/**
1170 * ufshcd_set_clk_freq - set UFS controller clock frequencies
1171 * @hba: per adapter instance
1172 * @scale_up: If True, set max possible frequency othewise set low frequency
1173 *
1174 * Returns 0 if successful
1175 * Returns < 0 for any other errors
1176 */
1177static int ufshcd_set_clk_freq(struct ufs_hba *hba, bool scale_up)
1178{
1179 int ret = 0;
1180 struct ufs_clk_info *clki;
1181 struct list_head *head = &hba->clk_list_head;
1182
1183 if (!head || list_empty(head))
1184 goto out;
1185
1186 list_for_each_entry(clki, head, list) {
1187 if (!IS_ERR_OR_NULL(clki->clk)) {
1188 if (scale_up && clki->max_freq) {
1189 if (clki->curr_freq == clki->max_freq)
1190 continue;
1191
1192 ret = clk_set_rate(clki->clk, clki->max_freq);
1193 if (ret) {
1194 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
1195 __func__, clki->name,
1196 clki->max_freq, ret);
1197 break;
1198 }
1199 trace_ufshcd_clk_scaling(dev_name(hba->dev),
1200 "scaled up", clki->name,
1201 clki->curr_freq,
1202 clki->max_freq);
1203 clki->curr_freq = clki->max_freq;
1204
1205 } else if (!scale_up && clki->min_freq) {
1206 if (clki->curr_freq == clki->min_freq)
1207 continue;
1208
1209 ret = clk_set_rate(clki->clk, clki->min_freq);
1210 if (ret) {
1211 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
1212 __func__, clki->name,
1213 clki->min_freq, ret);
1214 break;
1215 }
1216 trace_ufshcd_clk_scaling(dev_name(hba->dev),
1217 "scaled down", clki->name,
1218 clki->curr_freq,
1219 clki->min_freq);
1220 clki->curr_freq = clki->min_freq;
1221 }
1222 }
1223 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
1224 clki->name, clk_get_rate(clki->clk));
1225 }
1226
1227out:
1228 return ret;
1229}
1230
1231/**
1232 * ufshcd_scale_clks - scale up or scale down UFS controller clocks
1233 * @hba: per adapter instance
1234 * @scale_up: True if scaling up and false if scaling down
1235 *
1236 * Returns 0 if successful
1237 * Returns < 0 for any other errors
1238 */
1239static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
1240{
1241 int ret = 0;
1242
1243 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
1244 if (ret)
1245 return ret;
1246
1247 ret = ufshcd_set_clk_freq(hba, scale_up);
1248 if (ret)
1249 return ret;
1250
1251 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
1252 if (ret) {
1253 ufshcd_set_clk_freq(hba, !scale_up);
1254 return ret;
1255 }
1256
1257 return ret;
1258}
1259
Subhash Jadavani9c807702017-04-01 00:35:51 -07001260static inline void ufshcd_cancel_gate_work(struct ufs_hba *hba)
1261{
1262 hrtimer_cancel(&hba->clk_gating.gate_hrtimer);
1263 cancel_work_sync(&hba->clk_gating.gate_work);
1264}
1265
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001266static void ufshcd_ungate_work(struct work_struct *work)
1267{
1268 int ret;
1269 unsigned long flags;
1270 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1271 clk_gating.ungate_work);
1272
Subhash Jadavani9c807702017-04-01 00:35:51 -07001273 ufshcd_cancel_gate_work(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001274
1275 spin_lock_irqsave(hba->host->host_lock, flags);
1276 if (hba->clk_gating.state == CLKS_ON) {
1277 spin_unlock_irqrestore(hba->host->host_lock, flags);
1278 goto unblock_reqs;
1279 }
1280
1281 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001282 ufshcd_hba_vreg_set_hpm(hba);
1283 ufshcd_enable_clocks(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001284
1285 /* Exit from hibern8 */
1286 if (ufshcd_can_hibern8_during_gating(hba)) {
1287 /* Prevent gating in this path */
1288 hba->clk_gating.is_suspended = true;
1289 if (ufshcd_is_link_hibern8(hba)) {
1290 ret = ufshcd_uic_hibern8_exit(hba);
1291 if (ret)
1292 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
1293 __func__, ret);
1294 else
1295 ufshcd_set_link_active(hba);
1296 }
1297 hba->clk_gating.is_suspended = false;
1298 }
1299unblock_reqs:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001300 ufshcd_scsi_unblock_requests(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001301}
1302
1303/**
1304 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
1305 * Also, exit from hibern8 mode and set the link as active.
1306 * @hba: per adapter instance
1307 * @async: This indicates whether caller should ungate clocks asynchronously.
1308 */
1309int ufshcd_hold(struct ufs_hba *hba, bool async)
1310{
1311 int rc = 0;
1312 unsigned long flags;
1313
1314 if (!ufshcd_is_clkgating_allowed(hba))
1315 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001316 spin_lock_irqsave(hba->host->host_lock, flags);
1317 hba->clk_gating.active_reqs++;
1318
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001319 if (ufshcd_eh_in_progress(hba)) {
1320 spin_unlock_irqrestore(hba->host->host_lock, flags);
1321 return 0;
1322 }
1323
Sahitya Tummala856b3482014-09-25 15:32:34 +03001324start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001325 switch (hba->clk_gating.state) {
1326 case CLKS_ON:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001327 /*
1328 * Wait for the ungate work to complete if in progress.
1329 * Though the clocks may be in ON state, the link could
1330 * still be in hibner8 state if hibern8 is allowed
1331 * during clock gating.
1332 * Make sure we exit hibern8 state also in addition to
1333 * clocks being ON.
1334 */
1335 if (ufshcd_can_hibern8_during_gating(hba) &&
1336 ufshcd_is_link_hibern8(hba)) {
1337 spin_unlock_irqrestore(hba->host->host_lock, flags);
1338 flush_work(&hba->clk_gating.ungate_work);
1339 spin_lock_irqsave(hba->host->host_lock, flags);
1340 goto start;
1341 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001342 break;
1343 case REQ_CLKS_OFF:
Subhash Jadavani9c807702017-04-01 00:35:51 -07001344 /*
1345 * If the timer was active but the callback was not running
1346 * we have nothing to do, just change state and return.
1347 */
1348 if (hrtimer_try_to_cancel(&hba->clk_gating.gate_hrtimer) == 1) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001349 hba->clk_gating.state = CLKS_ON;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001350 trace_ufshcd_clk_gating(dev_name(hba->dev),
1351 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001352 break;
1353 }
1354 /*
Subhash Jadavani9c807702017-04-01 00:35:51 -07001355 * If we are here, it means gating work is either done or
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001356 * currently running. Hence, fall through to cancel gating
1357 * work and to enable clocks.
1358 */
1359 case CLKS_OFF:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001360 __ufshcd_scsi_block_requests(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001361 hba->clk_gating.state = REQ_CLKS_ON;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001362 trace_ufshcd_clk_gating(dev_name(hba->dev),
1363 hba->clk_gating.state);
Subhash Jadavani9c807702017-04-01 00:35:51 -07001364 queue_work(hba->clk_gating.ungating_workq,
1365 &hba->clk_gating.ungate_work);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001366 /*
1367 * fall through to check if we should wait for this
1368 * work to be done or not.
1369 */
1370 case REQ_CLKS_ON:
1371 if (async) {
1372 rc = -EAGAIN;
1373 hba->clk_gating.active_reqs--;
1374 break;
1375 }
1376
1377 spin_unlock_irqrestore(hba->host->host_lock, flags);
1378 flush_work(&hba->clk_gating.ungate_work);
1379 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +03001380 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001381 goto start;
1382 default:
1383 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
1384 __func__, hba->clk_gating.state);
1385 break;
1386 }
1387 spin_unlock_irqrestore(hba->host->host_lock, flags);
1388out:
1389 return rc;
1390}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001391EXPORT_SYMBOL_GPL(ufshcd_hold);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001392
1393static void ufshcd_gate_work(struct work_struct *work)
1394{
1395 struct ufs_hba *hba = container_of(work, struct ufs_hba,
Subhash Jadavani9c807702017-04-01 00:35:51 -07001396 clk_gating.gate_work);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001397 unsigned long flags;
1398
1399 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani9c807702017-04-01 00:35:51 -07001400 /*
1401 * In case you are here to cancel this work the gating state
1402 * would be marked as REQ_CLKS_ON. In this case save time by
1403 * skipping the gating work and exit after changing the clock
1404 * state to CLKS_ON.
1405 */
1406 if (hba->clk_gating.is_suspended ||
1407 (hba->clk_gating.state == REQ_CLKS_ON)) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001408 hba->clk_gating.state = CLKS_ON;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001409 trace_ufshcd_clk_gating(dev_name(hba->dev),
1410 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001411 goto rel_lock;
1412 }
1413
1414 if (hba->clk_gating.active_reqs
1415 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
1416 || hba->lrb_in_use || hba->outstanding_tasks
1417 || hba->active_uic_cmd || hba->uic_async_done)
1418 goto rel_lock;
1419
1420 spin_unlock_irqrestore(hba->host->host_lock, flags);
1421
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001422 if (ufshcd_is_hibern8_on_idle_allowed(hba) &&
1423 hba->hibern8_on_idle.is_enabled)
1424 /*
1425 * Hibern8 enter work (on Idle) needs clocks to be ON hence
1426 * make sure that it is flushed before turning off the clocks.
1427 */
1428 flush_delayed_work(&hba->hibern8_on_idle.enter_work);
1429
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001430 /* put the link into hibern8 mode before turning off clocks */
1431 if (ufshcd_can_hibern8_during_gating(hba)) {
1432 if (ufshcd_uic_hibern8_enter(hba)) {
1433 hba->clk_gating.state = CLKS_ON;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001434 trace_ufshcd_clk_gating(dev_name(hba->dev),
1435 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001436 goto out;
1437 }
1438 ufshcd_set_link_hibern8(hba);
1439 }
1440
Subhash Jadavani9c807702017-04-01 00:35:51 -07001441 /*
1442 * If auto hibern8 is supported then the link will already
1443 * be in hibern8 state and the ref clock can be gated.
1444 */
1445 if ((ufshcd_is_auto_hibern8_supported(hba) ||
1446 !ufshcd_is_link_active(hba)) && !hba->no_ref_clk_gating)
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001447 ufshcd_disable_clocks(hba, true);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001448 else
1449 /* If link is active, device ref_clk can't be switched off */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001450 ufshcd_disable_clocks_skip_ref_clk(hba, true);
1451
1452 /* Put the host controller in low power mode if possible */
1453 ufshcd_hba_vreg_set_lpm(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001454
1455 /*
1456 * In case you are here to cancel this work the gating state
1457 * would be marked as REQ_CLKS_ON. In this case keep the state
1458 * as REQ_CLKS_ON which would anyway imply that clocks are off
1459 * and a request to turn them on is pending. By doing this way,
1460 * we keep the state machine in tact and this would ultimately
1461 * prevent from doing cancel work multiple times when there are
1462 * new requests arriving before the current cancel work is done.
1463 */
1464 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001465 if (hba->clk_gating.state == REQ_CLKS_OFF) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001466 hba->clk_gating.state = CLKS_OFF;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001467 trace_ufshcd_clk_gating(dev_name(hba->dev),
1468 hba->clk_gating.state);
1469 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001470rel_lock:
1471 spin_unlock_irqrestore(hba->host->host_lock, flags);
1472out:
1473 return;
1474}
1475
1476/* host lock must be held before calling this variant */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001477static void __ufshcd_release(struct ufs_hba *hba, bool no_sched)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001478{
1479 if (!ufshcd_is_clkgating_allowed(hba))
1480 return;
1481
1482 hba->clk_gating.active_reqs--;
1483
1484 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended
1485 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
1486 || hba->lrb_in_use || hba->outstanding_tasks
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001487 || hba->active_uic_cmd || hba->uic_async_done
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001488 || ufshcd_eh_in_progress(hba) || no_sched)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001489 return;
1490
1491 hba->clk_gating.state = REQ_CLKS_OFF;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001492 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state);
1493
Subhash Jadavani9c807702017-04-01 00:35:51 -07001494 hrtimer_start(&hba->clk_gating.gate_hrtimer,
1495 ms_to_ktime(hba->clk_gating.delay_ms),
1496 HRTIMER_MODE_REL);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001497}
1498
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001499void ufshcd_release(struct ufs_hba *hba, bool no_sched)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001500{
1501 unsigned long flags;
1502
1503 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001504 __ufshcd_release(hba, no_sched);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001505 spin_unlock_irqrestore(hba->host->host_lock, flags);
1506}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001507EXPORT_SYMBOL_GPL(ufshcd_release);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001508
1509static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
1510 struct device_attribute *attr, char *buf)
1511{
1512 struct ufs_hba *hba = dev_get_drvdata(dev);
1513
1514 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms);
1515}
1516
1517static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
1518 struct device_attribute *attr, const char *buf, size_t count)
1519{
1520 struct ufs_hba *hba = dev_get_drvdata(dev);
1521 unsigned long flags, value;
1522
1523 if (kstrtoul(buf, 0, &value))
1524 return -EINVAL;
1525
1526 spin_lock_irqsave(hba->host->host_lock, flags);
1527 hba->clk_gating.delay_ms = value;
1528 spin_unlock_irqrestore(hba->host->host_lock, flags);
1529 return count;
1530}
1531
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001532static ssize_t ufshcd_clkgate_delay_pwr_save_show(struct device *dev,
1533 struct device_attribute *attr, char *buf)
1534{
1535 struct ufs_hba *hba = dev_get_drvdata(dev);
1536
1537 return snprintf(buf, PAGE_SIZE, "%lu\n",
1538 hba->clk_gating.delay_ms_pwr_save);
1539}
1540
1541static ssize_t ufshcd_clkgate_delay_pwr_save_store(struct device *dev,
1542 struct device_attribute *attr, const char *buf, size_t count)
1543{
1544 struct ufs_hba *hba = dev_get_drvdata(dev);
1545 unsigned long flags, value;
1546
1547 if (kstrtoul(buf, 0, &value))
1548 return -EINVAL;
1549
1550 spin_lock_irqsave(hba->host->host_lock, flags);
1551
1552 hba->clk_gating.delay_ms_pwr_save = value;
1553 if (ufshcd_is_clkscaling_supported(hba) &&
1554 !hba->clk_scaling.is_scaled_up)
1555 hba->clk_gating.delay_ms = hba->clk_gating.delay_ms_pwr_save;
1556
1557 spin_unlock_irqrestore(hba->host->host_lock, flags);
1558 return count;
1559}
1560
1561static ssize_t ufshcd_clkgate_delay_perf_show(struct device *dev,
1562 struct device_attribute *attr, char *buf)
1563{
1564 struct ufs_hba *hba = dev_get_drvdata(dev);
1565
1566 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms_perf);
1567}
1568
1569static ssize_t ufshcd_clkgate_delay_perf_store(struct device *dev,
1570 struct device_attribute *attr, const char *buf, size_t count)
1571{
1572 struct ufs_hba *hba = dev_get_drvdata(dev);
1573 unsigned long flags, value;
1574
1575 if (kstrtoul(buf, 0, &value))
1576 return -EINVAL;
1577
1578 spin_lock_irqsave(hba->host->host_lock, flags);
1579
1580 hba->clk_gating.delay_ms_perf = value;
1581 if (ufshcd_is_clkscaling_supported(hba) &&
1582 hba->clk_scaling.is_scaled_up)
1583 hba->clk_gating.delay_ms = hba->clk_gating.delay_ms_perf;
1584
1585 spin_unlock_irqrestore(hba->host->host_lock, flags);
1586 return count;
1587}
1588
1589static ssize_t ufshcd_clkgate_enable_show(struct device *dev,
1590 struct device_attribute *attr, char *buf)
1591{
1592 struct ufs_hba *hba = dev_get_drvdata(dev);
1593
1594 return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_gating.is_enabled);
1595}
1596
1597static ssize_t ufshcd_clkgate_enable_store(struct device *dev,
1598 struct device_attribute *attr, const char *buf, size_t count)
1599{
1600 struct ufs_hba *hba = dev_get_drvdata(dev);
1601 unsigned long flags;
1602 u32 value;
1603
1604 if (kstrtou32(buf, 0, &value))
1605 return -EINVAL;
1606
1607 value = !!value;
1608 if (value == hba->clk_gating.is_enabled)
1609 goto out;
1610
1611 if (value) {
1612 ufshcd_release(hba, false);
1613 } else {
1614 spin_lock_irqsave(hba->host->host_lock, flags);
1615 hba->clk_gating.active_reqs++;
1616 spin_unlock_irqrestore(hba->host->host_lock, flags);
1617 }
1618
1619 hba->clk_gating.is_enabled = value;
1620out:
1621 return count;
1622}
1623
Subhash Jadavani9c807702017-04-01 00:35:51 -07001624static enum hrtimer_restart ufshcd_clkgate_hrtimer_handler(
1625 struct hrtimer *timer)
1626{
1627 struct ufs_hba *hba = container_of(timer, struct ufs_hba,
1628 clk_gating.gate_hrtimer);
1629
1630 schedule_work(&hba->clk_gating.gate_work);
1631
1632 return HRTIMER_NORESTART;
1633}
1634
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001635static void ufshcd_init_clk_gating(struct ufs_hba *hba)
1636{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001637 struct ufs_clk_gating *gating = &hba->clk_gating;
Subhash Jadavani9c807702017-04-01 00:35:51 -07001638 char wq_name[sizeof("ufs_clk_ungating_00")];
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001639
1640 hba->clk_gating.state = CLKS_ON;
1641
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001642 if (!ufshcd_is_clkgating_allowed(hba))
1643 return;
1644
Subhash Jadavani9c807702017-04-01 00:35:51 -07001645 /*
1646 * Disable hibern8 during clk gating if
1647 * auto hibern8 is supported
1648 */
1649 if (ufshcd_is_auto_hibern8_supported(hba))
1650 hba->caps &= ~UFSHCD_CAP_HIBERN8_WITH_CLK_GATING;
1651
1652 INIT_WORK(&gating->gate_work, ufshcd_gate_work);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001653 INIT_WORK(&gating->ungate_work, ufshcd_ungate_work);
Subhash Jadavani9c807702017-04-01 00:35:51 -07001654 /*
1655 * Clock gating work must be executed only after auto hibern8
1656 * timeout has expired in the hardware or after aggressive
1657 * hibern8 on idle software timeout. Using jiffy based low
1658 * resolution delayed work is not reliable to guarantee this,
1659 * hence use a high resolution timer to make sure we schedule
1660 * the gate work precisely more than hibern8 timeout.
1661 *
1662 * Always make sure gating->delay_ms > hibern8_on_idle->delay_ms
1663 */
1664 hrtimer_init(&gating->gate_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1665 gating->gate_hrtimer.function = ufshcd_clkgate_hrtimer_handler;
1666
1667 snprintf(wq_name, ARRAY_SIZE(wq_name), "ufs_clk_ungating_%d",
1668 hba->host->host_no);
1669 hba->clk_gating.ungating_workq = create_singlethread_workqueue(wq_name);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001670
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001671 gating->is_enabled = true;
1672
Subhash Jadavani9c807702017-04-01 00:35:51 -07001673 gating->delay_ms_pwr_save = UFSHCD_CLK_GATING_DELAY_MS_PWR_SAVE;
1674 gating->delay_ms_perf = UFSHCD_CLK_GATING_DELAY_MS_PERF;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001675
1676 /* start with performance mode */
1677 gating->delay_ms = gating->delay_ms_perf;
1678
1679 if (!ufshcd_is_clkscaling_supported(hba))
1680 goto scaling_not_supported;
1681
1682 gating->delay_pwr_save_attr.show = ufshcd_clkgate_delay_pwr_save_show;
1683 gating->delay_pwr_save_attr.store = ufshcd_clkgate_delay_pwr_save_store;
1684 sysfs_attr_init(&gating->delay_pwr_save_attr.attr);
1685 gating->delay_pwr_save_attr.attr.name = "clkgate_delay_ms_pwr_save";
1686 gating->delay_pwr_save_attr.attr.mode = S_IRUGO | S_IWUSR;
1687 if (device_create_file(hba->dev, &gating->delay_pwr_save_attr))
1688 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay_ms_pwr_save\n");
1689
1690 gating->delay_perf_attr.show = ufshcd_clkgate_delay_perf_show;
1691 gating->delay_perf_attr.store = ufshcd_clkgate_delay_perf_store;
1692 sysfs_attr_init(&gating->delay_perf_attr.attr);
1693 gating->delay_perf_attr.attr.name = "clkgate_delay_ms_perf";
1694 gating->delay_perf_attr.attr.mode = S_IRUGO | S_IWUSR;
1695 if (device_create_file(hba->dev, &gating->delay_perf_attr))
1696 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay_ms_perf\n");
1697
1698 goto add_clkgate_enable;
1699
1700scaling_not_supported:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001701 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
1702 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
1703 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
1704 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
1705 hba->clk_gating.delay_attr.attr.mode = S_IRUGO | S_IWUSR;
1706 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
1707 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001708
1709add_clkgate_enable:
1710 gating->enable_attr.show = ufshcd_clkgate_enable_show;
1711 gating->enable_attr.store = ufshcd_clkgate_enable_store;
1712 sysfs_attr_init(&gating->enable_attr.attr);
1713 gating->enable_attr.attr.name = "clkgate_enable";
1714 gating->enable_attr.attr.mode = S_IRUGO | S_IWUSR;
1715 if (device_create_file(hba->dev, &gating->enable_attr))
1716 dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001717}
1718
1719static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
1720{
1721 if (!ufshcd_is_clkgating_allowed(hba))
1722 return;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001723 if (ufshcd_is_clkscaling_supported(hba)) {
1724 device_remove_file(hba->dev,
1725 &hba->clk_gating.delay_pwr_save_attr);
1726 device_remove_file(hba->dev, &hba->clk_gating.delay_perf_attr);
1727 } else {
1728 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
1729 }
1730 device_remove_file(hba->dev, &hba->clk_gating.enable_attr);
Subhash Jadavani9c807702017-04-01 00:35:51 -07001731 ufshcd_cancel_gate_work(hba);
Akinobu Mita97cd6802014-11-24 14:24:18 +09001732 cancel_work_sync(&hba->clk_gating.ungate_work);
Subhash Jadavani9c807702017-04-01 00:35:51 -07001733 destroy_workqueue(hba->clk_gating.ungating_workq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001734}
1735
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001736static void ufshcd_set_auto_hibern8_timer(struct ufs_hba *hba, u32 delay)
1737{
1738 ufshcd_rmwl(hba, AUTO_HIBERN8_TIMER_SCALE_MASK |
1739 AUTO_HIBERN8_IDLE_TIMER_MASK,
1740 AUTO_HIBERN8_TIMER_SCALE_1_MS | delay,
1741 REG_AUTO_HIBERN8_IDLE_TIMER);
1742 /* Make sure the timer gets applied before further operations */
1743 mb();
1744}
1745
1746/**
1747 * ufshcd_hibern8_hold - Make sure that link is not in hibern8.
1748 *
1749 * @hba: per adapter instance
1750 * @async: This indicates whether caller wants to exit hibern8 asynchronously.
1751 *
1752 * Exit from hibern8 mode and set the link as active.
1753 *
1754 * Return 0 on success, non-zero on failure.
1755 */
1756static int ufshcd_hibern8_hold(struct ufs_hba *hba, bool async)
1757{
1758 int rc = 0;
1759 unsigned long flags;
1760
1761 if (!ufshcd_is_hibern8_on_idle_allowed(hba))
1762 goto out;
1763
1764 spin_lock_irqsave(hba->host->host_lock, flags);
1765 hba->hibern8_on_idle.active_reqs++;
1766
1767 if (ufshcd_eh_in_progress(hba)) {
1768 spin_unlock_irqrestore(hba->host->host_lock, flags);
1769 return 0;
1770 }
1771
1772start:
1773 switch (hba->hibern8_on_idle.state) {
1774 case HIBERN8_EXITED:
1775 break;
1776 case REQ_HIBERN8_ENTER:
1777 if (cancel_delayed_work(&hba->hibern8_on_idle.enter_work)) {
1778 hba->hibern8_on_idle.state = HIBERN8_EXITED;
1779 trace_ufshcd_hibern8_on_idle(dev_name(hba->dev),
1780 hba->hibern8_on_idle.state);
1781 break;
1782 }
1783 /*
1784 * If we here, it means Hibern8 enter work is either done or
1785 * currently running. Hence, fall through to cancel hibern8
1786 * work and exit hibern8.
1787 */
1788 case HIBERN8_ENTERED:
1789 __ufshcd_scsi_block_requests(hba);
1790 hba->hibern8_on_idle.state = REQ_HIBERN8_EXIT;
1791 trace_ufshcd_hibern8_on_idle(dev_name(hba->dev),
1792 hba->hibern8_on_idle.state);
1793 schedule_work(&hba->hibern8_on_idle.exit_work);
1794 /*
1795 * fall through to check if we should wait for this
1796 * work to be done or not.
1797 */
1798 case REQ_HIBERN8_EXIT:
1799 if (async) {
1800 rc = -EAGAIN;
1801 hba->hibern8_on_idle.active_reqs--;
1802 break;
1803 } else {
1804 spin_unlock_irqrestore(hba->host->host_lock, flags);
1805 flush_work(&hba->hibern8_on_idle.exit_work);
1806 /* Make sure state is HIBERN8_EXITED before returning */
1807 spin_lock_irqsave(hba->host->host_lock, flags);
1808 goto start;
1809 }
1810 default:
1811 dev_err(hba->dev, "%s: H8 is in invalid state %d\n",
1812 __func__, hba->hibern8_on_idle.state);
1813 break;
1814 }
1815 spin_unlock_irqrestore(hba->host->host_lock, flags);
1816out:
1817 return rc;
1818}
1819
1820/* host lock must be held before calling this variant */
1821static void __ufshcd_hibern8_release(struct ufs_hba *hba, bool no_sched)
1822{
1823 unsigned long delay_in_jiffies;
1824
1825 if (!ufshcd_is_hibern8_on_idle_allowed(hba))
1826 return;
1827
1828 hba->hibern8_on_idle.active_reqs--;
1829 BUG_ON(hba->hibern8_on_idle.active_reqs < 0);
1830
1831 if (hba->hibern8_on_idle.active_reqs
1832 || hba->hibern8_on_idle.is_suspended
1833 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
1834 || hba->lrb_in_use || hba->outstanding_tasks
1835 || hba->active_uic_cmd || hba->uic_async_done
1836 || ufshcd_eh_in_progress(hba) || no_sched)
1837 return;
1838
1839 hba->hibern8_on_idle.state = REQ_HIBERN8_ENTER;
1840 trace_ufshcd_hibern8_on_idle(dev_name(hba->dev),
1841 hba->hibern8_on_idle.state);
1842 /*
1843 * Scheduling the delayed work after 1 jiffies will make the work to
1844 * get schedule any time from 0ms to 1000/HZ ms which is not desirable
1845 * for hibern8 enter work as it may impact the performance if it gets
1846 * scheduled almost immediately. Hence make sure that hibern8 enter
1847 * work gets scheduled atleast after 2 jiffies (any time between
1848 * 1000/HZ ms to 2000/HZ ms).
1849 */
1850 delay_in_jiffies = msecs_to_jiffies(hba->hibern8_on_idle.delay_ms);
1851 if (delay_in_jiffies == 1)
1852 delay_in_jiffies++;
1853
1854 schedule_delayed_work(&hba->hibern8_on_idle.enter_work,
1855 delay_in_jiffies);
1856}
1857
1858static void ufshcd_hibern8_release(struct ufs_hba *hba, bool no_sched)
1859{
1860 unsigned long flags;
1861
1862 spin_lock_irqsave(hba->host->host_lock, flags);
1863 __ufshcd_hibern8_release(hba, no_sched);
1864 spin_unlock_irqrestore(hba->host->host_lock, flags);
1865}
1866
1867static void ufshcd_hibern8_enter_work(struct work_struct *work)
1868{
1869 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1870 hibern8_on_idle.enter_work.work);
1871 unsigned long flags;
1872
1873 spin_lock_irqsave(hba->host->host_lock, flags);
1874 if (hba->hibern8_on_idle.is_suspended) {
1875 hba->hibern8_on_idle.state = HIBERN8_EXITED;
1876 trace_ufshcd_hibern8_on_idle(dev_name(hba->dev),
1877 hba->hibern8_on_idle.state);
1878 goto rel_lock;
1879 }
1880
1881 if (hba->hibern8_on_idle.active_reqs
1882 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
1883 || hba->lrb_in_use || hba->outstanding_tasks
1884 || hba->active_uic_cmd || hba->uic_async_done)
1885 goto rel_lock;
1886
1887 spin_unlock_irqrestore(hba->host->host_lock, flags);
1888
1889 if (ufshcd_is_link_active(hba) && ufshcd_uic_hibern8_enter(hba)) {
1890 /* Enter failed */
1891 hba->hibern8_on_idle.state = HIBERN8_EXITED;
1892 trace_ufshcd_hibern8_on_idle(dev_name(hba->dev),
1893 hba->hibern8_on_idle.state);
1894 goto out;
1895 }
1896 ufshcd_set_link_hibern8(hba);
1897
1898 /*
1899 * In case you are here to cancel this work the hibern8_on_idle.state
1900 * would be marked as REQ_HIBERN8_EXIT. In this case keep the state
1901 * as REQ_HIBERN8_EXIT which would anyway imply that we are in hibern8
1902 * and a request to exit from it is pending. By doing this way,
1903 * we keep the state machine in tact and this would ultimately
1904 * prevent from doing cancel work multiple times when there are
1905 * new requests arriving before the current cancel work is done.
1906 */
1907 spin_lock_irqsave(hba->host->host_lock, flags);
1908 if (hba->hibern8_on_idle.state == REQ_HIBERN8_ENTER) {
1909 hba->hibern8_on_idle.state = HIBERN8_ENTERED;
1910 trace_ufshcd_hibern8_on_idle(dev_name(hba->dev),
1911 hba->hibern8_on_idle.state);
1912 }
1913rel_lock:
1914 spin_unlock_irqrestore(hba->host->host_lock, flags);
1915out:
1916 return;
1917}
1918
1919static void ufshcd_hibern8_exit_work(struct work_struct *work)
1920{
1921 int ret;
1922 unsigned long flags;
1923 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1924 hibern8_on_idle.exit_work);
1925
1926 cancel_delayed_work_sync(&hba->hibern8_on_idle.enter_work);
1927
1928 spin_lock_irqsave(hba->host->host_lock, flags);
1929 if ((hba->hibern8_on_idle.state == HIBERN8_EXITED)
1930 || ufshcd_is_link_active(hba)) {
1931 hba->hibern8_on_idle.state = HIBERN8_EXITED;
1932 spin_unlock_irqrestore(hba->host->host_lock, flags);
1933 goto unblock_reqs;
1934 }
1935 spin_unlock_irqrestore(hba->host->host_lock, flags);
1936
1937 /* Exit from hibern8 */
1938 if (ufshcd_is_link_hibern8(hba)) {
1939 ufshcd_hold(hba, false);
1940 ret = ufshcd_uic_hibern8_exit(hba);
1941 ufshcd_release(hba, false);
1942 if (!ret) {
1943 spin_lock_irqsave(hba->host->host_lock, flags);
1944 ufshcd_set_link_active(hba);
1945 hba->hibern8_on_idle.state = HIBERN8_EXITED;
1946 trace_ufshcd_hibern8_on_idle(dev_name(hba->dev),
1947 hba->hibern8_on_idle.state);
1948 spin_unlock_irqrestore(hba->host->host_lock, flags);
1949 }
1950 }
1951unblock_reqs:
1952 ufshcd_scsi_unblock_requests(hba);
1953}
1954
1955static ssize_t ufshcd_hibern8_on_idle_delay_show(struct device *dev,
1956 struct device_attribute *attr, char *buf)
1957{
1958 struct ufs_hba *hba = dev_get_drvdata(dev);
1959
1960 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->hibern8_on_idle.delay_ms);
1961}
1962
1963static ssize_t ufshcd_hibern8_on_idle_delay_store(struct device *dev,
1964 struct device_attribute *attr, const char *buf, size_t count)
1965{
1966 struct ufs_hba *hba = dev_get_drvdata(dev);
1967 unsigned long flags, value;
1968
1969 if (kstrtoul(buf, 0, &value))
1970 return -EINVAL;
1971
1972 spin_lock_irqsave(hba->host->host_lock, flags);
1973 hba->hibern8_on_idle.delay_ms = value;
1974 spin_unlock_irqrestore(hba->host->host_lock, flags);
1975
1976 /* Update auto hibern8 timer value if supported */
1977 if (ufshcd_is_auto_hibern8_supported(hba) &&
1978 hba->hibern8_on_idle.is_enabled)
1979 ufshcd_set_auto_hibern8_timer(hba,
1980 hba->hibern8_on_idle.delay_ms);
1981
1982 return count;
1983}
1984
1985static ssize_t ufshcd_hibern8_on_idle_enable_show(struct device *dev,
1986 struct device_attribute *attr, char *buf)
1987{
1988 struct ufs_hba *hba = dev_get_drvdata(dev);
1989
1990 return snprintf(buf, PAGE_SIZE, "%d\n",
1991 hba->hibern8_on_idle.is_enabled);
1992}
1993
1994static ssize_t ufshcd_hibern8_on_idle_enable_store(struct device *dev,
1995 struct device_attribute *attr, const char *buf, size_t count)
1996{
1997 struct ufs_hba *hba = dev_get_drvdata(dev);
1998 unsigned long flags;
1999 u32 value;
2000
2001 if (kstrtou32(buf, 0, &value))
2002 return -EINVAL;
2003
2004 value = !!value;
2005 if (value == hba->hibern8_on_idle.is_enabled)
2006 goto out;
2007
2008 /* Update auto hibern8 timer value if supported */
2009 if (ufshcd_is_auto_hibern8_supported(hba)) {
2010 ufshcd_set_auto_hibern8_timer(hba,
2011 value ? hba->hibern8_on_idle.delay_ms : value);
2012 goto update;
2013 }
2014
2015 if (value) {
2016 /*
2017 * As clock gating work would wait for the hibern8 enter work
2018 * to finish, clocks would remain on during hibern8 enter work.
2019 */
2020 ufshcd_hold(hba, false);
2021 ufshcd_release_all(hba);
2022 } else {
2023 spin_lock_irqsave(hba->host->host_lock, flags);
2024 hba->hibern8_on_idle.active_reqs++;
2025 spin_unlock_irqrestore(hba->host->host_lock, flags);
2026 }
2027
2028update:
2029 hba->hibern8_on_idle.is_enabled = value;
2030out:
2031 return count;
2032}
2033
2034static void ufshcd_init_hibern8_on_idle(struct ufs_hba *hba)
2035{
2036 /* initialize the state variable here */
2037 hba->hibern8_on_idle.state = HIBERN8_EXITED;
2038
2039 if (!ufshcd_is_hibern8_on_idle_allowed(hba) &&
2040 !ufshcd_is_auto_hibern8_supported(hba))
2041 return;
2042
2043 if (ufshcd_is_auto_hibern8_supported(hba)) {
Subhash Jadavani9c807702017-04-01 00:35:51 -07002044 hba->hibern8_on_idle.delay_ms = 1;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002045 hba->hibern8_on_idle.state = AUTO_HIBERN8;
2046 /*
2047 * Disable SW hibern8 enter on idle in case
2048 * auto hibern8 is supported
2049 */
2050 hba->caps &= ~UFSHCD_CAP_HIBERN8_ENTER_ON_IDLE;
2051 } else {
Subhash Jadavani9c807702017-04-01 00:35:51 -07002052 hba->hibern8_on_idle.delay_ms = 10;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002053 INIT_DELAYED_WORK(&hba->hibern8_on_idle.enter_work,
2054 ufshcd_hibern8_enter_work);
2055 INIT_WORK(&hba->hibern8_on_idle.exit_work,
2056 ufshcd_hibern8_exit_work);
2057 }
2058
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002059 hba->hibern8_on_idle.is_enabled = true;
2060
2061 hba->hibern8_on_idle.delay_attr.show =
2062 ufshcd_hibern8_on_idle_delay_show;
2063 hba->hibern8_on_idle.delay_attr.store =
2064 ufshcd_hibern8_on_idle_delay_store;
2065 sysfs_attr_init(&hba->hibern8_on_idle.delay_attr.attr);
2066 hba->hibern8_on_idle.delay_attr.attr.name = "hibern8_on_idle_delay_ms";
2067 hba->hibern8_on_idle.delay_attr.attr.mode = S_IRUGO | S_IWUSR;
2068 if (device_create_file(hba->dev, &hba->hibern8_on_idle.delay_attr))
2069 dev_err(hba->dev, "Failed to create sysfs for hibern8_on_idle_delay\n");
2070
2071 hba->hibern8_on_idle.enable_attr.show =
2072 ufshcd_hibern8_on_idle_enable_show;
2073 hba->hibern8_on_idle.enable_attr.store =
2074 ufshcd_hibern8_on_idle_enable_store;
2075 sysfs_attr_init(&hba->hibern8_on_idle.enable_attr.attr);
2076 hba->hibern8_on_idle.enable_attr.attr.name = "hibern8_on_idle_enable";
2077 hba->hibern8_on_idle.enable_attr.attr.mode = S_IRUGO | S_IWUSR;
2078 if (device_create_file(hba->dev, &hba->hibern8_on_idle.enable_attr))
2079 dev_err(hba->dev, "Failed to create sysfs for hibern8_on_idle_enable\n");
2080}
2081
2082static void ufshcd_exit_hibern8_on_idle(struct ufs_hba *hba)
2083{
2084 if (!ufshcd_is_hibern8_on_idle_allowed(hba) &&
2085 !ufshcd_is_auto_hibern8_supported(hba))
2086 return;
2087 device_remove_file(hba->dev, &hba->hibern8_on_idle.delay_attr);
2088 device_remove_file(hba->dev, &hba->hibern8_on_idle.enable_attr);
2089}
2090
2091static void ufshcd_hold_all(struct ufs_hba *hba)
2092{
2093 ufshcd_hold(hba, false);
2094 ufshcd_hibern8_hold(hba, false);
2095}
2096
2097static void ufshcd_release_all(struct ufs_hba *hba)
2098{
2099 ufshcd_hibern8_release(hba, false);
2100 ufshcd_release(hba, false);
2101}
2102
Sahitya Tummala856b3482014-09-25 15:32:34 +03002103/* Must be called with host lock acquired */
2104static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
2105{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002106 bool queue_resume_work = false;
2107
2108 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03002109 return;
2110
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002111 if (!hba->clk_scaling.active_reqs++)
2112 queue_resume_work = true;
2113
2114 if (!hba->clk_scaling.is_allowed || hba->pm_op_in_progress)
2115 return;
2116
2117 if (queue_resume_work)
2118 queue_work(hba->clk_scaling.workq,
2119 &hba->clk_scaling.resume_work);
2120
2121 if (!hba->clk_scaling.window_start_t) {
2122 hba->clk_scaling.window_start_t = jiffies;
2123 hba->clk_scaling.tot_busy_t = 0;
2124 hba->clk_scaling.is_busy_started = false;
2125 }
2126
Sahitya Tummala856b3482014-09-25 15:32:34 +03002127 if (!hba->clk_scaling.is_busy_started) {
2128 hba->clk_scaling.busy_start_t = ktime_get();
2129 hba->clk_scaling.is_busy_started = true;
2130 }
2131}
2132
2133static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
2134{
2135 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
2136
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002137 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03002138 return;
2139
2140 if (!hba->outstanding_reqs && scaling->is_busy_started) {
2141 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
2142 scaling->busy_start_t));
2143 scaling->busy_start_t = ktime_set(0, 0);
2144 scaling->is_busy_started = false;
2145 }
2146}
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002147
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302148/**
2149 * ufshcd_send_command - Send SCSI or device management commands
2150 * @hba: per adapter instance
2151 * @task_tag: Task tag of the command
2152 */
2153static inline
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002154int ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302155{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002156 int ret = 0;
2157
2158 hba->lrb[task_tag].issue_time_stamp = ktime_get();
2159 hba->lrb[task_tag].complete_time_stamp = ktime_set(0, 0);
Sahitya Tummala856b3482014-09-25 15:32:34 +03002160 ufshcd_clk_scaling_start_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302161 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302162 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002163 /* Make sure that doorbell is committed immediately */
2164 wmb();
2165 ufshcd_cond_add_cmd_trace(hba, task_tag, "send");
2166 ufshcd_update_tag_stats(hba, task_tag);
2167 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302168}
2169
2170/**
2171 * ufshcd_copy_sense_data - Copy sense data in case of check condition
2172 * @lrb - pointer to local reference block
2173 */
2174static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
2175{
2176 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05302177 if (lrbp->sense_buffer &&
2178 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002179 int len_to_copy;
2180
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302181 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002182 len_to_copy = min_t(int, RESPONSE_UPIU_SENSE_DATA_LENGTH, len);
2183
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302184 memcpy(lrbp->sense_buffer,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302185 lrbp->ucd_rsp_ptr->sr.sense_data,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002186 min_t(int, len_to_copy, UFSHCD_REQ_SENSE_SIZE));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302187 }
2188}
2189
2190/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302191 * ufshcd_copy_query_response() - Copy the Query Response and the data
2192 * descriptor
2193 * @hba: per adapter instance
2194 * @lrb - pointer to local reference block
2195 */
2196static
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002197int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +05302198{
2199 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
2200
Dolev Raviv68078d52013-07-30 00:35:58 +05302201 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05302202
Dolev Raviv68078d52013-07-30 00:35:58 +05302203 /* Get the descriptor */
2204 if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002205 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +05302206 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002207 u16 resp_len;
2208 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +05302209
2210 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002211 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +05302212 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002213 buf_len = be16_to_cpu(
2214 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002215 if (likely(buf_len >= resp_len)) {
2216 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
2217 } else {
2218 dev_warn(hba->dev,
2219 "%s: Response size is bigger than buffer",
2220 __func__);
2221 return -EINVAL;
2222 }
Dolev Raviv68078d52013-07-30 00:35:58 +05302223 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002224
2225 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05302226}
2227
2228/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302229 * ufshcd_hba_capabilities - Read controller capabilities
2230 * @hba: per adapter instance
2231 */
2232static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
2233{
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302234 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302235
2236 /* nutrs and nutmrs are 0 based values */
2237 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
2238 hba->nutmrs =
2239 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
2240}
2241
2242/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302243 * ufshcd_ready_for_uic_cmd - Check if controller is ready
2244 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302245 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302246 * Return true on success, else false
2247 */
2248static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
2249{
2250 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
2251 return true;
2252 else
2253 return false;
2254}
2255
2256/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302257 * ufshcd_get_upmcrs - Get the power mode change request status
2258 * @hba: Pointer to adapter instance
2259 *
2260 * This function gets the UPMCRS field of HCS register
2261 * Returns value of UPMCRS field
2262 */
2263static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
2264{
2265 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
2266}
2267
2268/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302269 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
2270 * @hba: per adapter instance
2271 * @uic_cmd: UIC command
2272 *
2273 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302274 */
2275static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302276ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302277{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302278 WARN_ON(hba->active_uic_cmd);
2279
2280 hba->active_uic_cmd = uic_cmd;
2281
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302282 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302283 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
2284 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
2285 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302286
2287 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302288 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302289 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302290}
2291
2292/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302293 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
2294 * @hba: per adapter instance
2295 * @uic_command: UIC command
2296 *
2297 * Must be called with mutex held.
2298 * Returns 0 only if success.
2299 */
2300static int
2301ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
2302{
2303 int ret;
2304 unsigned long flags;
2305
2306 if (wait_for_completion_timeout(&uic_cmd->done,
2307 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
2308 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
2309 else
2310 ret = -ETIMEDOUT;
2311
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002312 if (ret)
2313 ufsdbg_set_err_state(hba);
2314
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302315 spin_lock_irqsave(hba->host->host_lock, flags);
2316 hba->active_uic_cmd = NULL;
2317 spin_unlock_irqrestore(hba->host->host_lock, flags);
2318
2319 return ret;
2320}
2321
2322/**
2323 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
2324 * @hba: per adapter instance
2325 * @uic_cmd: UIC command
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002326 * @completion: initialize the completion only if this is set to true
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302327 *
2328 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002329 * with mutex held and host_lock locked.
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302330 * Returns 0 only if success.
2331 */
2332static int
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002333__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd,
2334 bool completion)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302335{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302336 if (!ufshcd_ready_for_uic_cmd(hba)) {
2337 dev_err(hba->dev,
2338 "Controller not ready to accept UIC commands\n");
2339 return -EIO;
2340 }
2341
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002342 if (completion)
2343 init_completion(&uic_cmd->done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302344
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302345 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302346
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002347 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302348}
2349
2350/**
2351 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
2352 * @hba: per adapter instance
2353 * @uic_cmd: UIC command
2354 *
2355 * Returns 0 only if success.
2356 */
2357static int
2358ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
2359{
2360 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002361 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302362
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002363 ufshcd_hold_all(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302364 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +03002365 ufshcd_add_delay_before_dme_cmd(hba);
2366
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002367 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002368 ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002369 spin_unlock_irqrestore(hba->host->host_lock, flags);
2370 if (!ret)
2371 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
2372
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002373 ufshcd_save_tstamp_of_last_dme_cmd(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302374 mutex_unlock(&hba->uic_cmd_mutex);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002375 ufshcd_release_all(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302376
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002377 ufsdbg_error_inject_dispatcher(hba,
2378 ERR_INJECT_UIC, 0, &ret);
2379
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302380 return ret;
2381}
2382
2383/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302384 * ufshcd_map_sg - Map scatter-gather list to prdt
2385 * @lrbp - pointer to local reference block
2386 *
2387 * Returns 0 in case of success, non-zero value in case of failure
2388 */
2389static int ufshcd_map_sg(struct ufshcd_lrb *lrbp)
2390{
2391 struct ufshcd_sg_entry *prd_table;
2392 struct scatterlist *sg;
2393 struct scsi_cmnd *cmd;
2394 int sg_segments;
2395 int i;
2396
2397 cmd = lrbp->cmd;
2398 sg_segments = scsi_dma_map(cmd);
2399 if (sg_segments < 0)
2400 return sg_segments;
2401
2402 if (sg_segments) {
2403 lrbp->utr_descriptor_ptr->prd_table_length =
2404 cpu_to_le16((u16) (sg_segments));
2405
2406 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
2407
2408 scsi_for_each_sg(cmd, sg, sg_segments, i) {
2409 prd_table[i].size =
2410 cpu_to_le32(((u32) sg_dma_len(sg))-1);
2411 prd_table[i].base_addr =
2412 cpu_to_le32(lower_32_bits(sg->dma_address));
2413 prd_table[i].upper_addr =
2414 cpu_to_le32(upper_32_bits(sg->dma_address));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002415 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302416 }
2417 } else {
2418 lrbp->utr_descriptor_ptr->prd_table_length = 0;
2419 }
2420
2421 return 0;
2422}
2423
2424/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302425 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302426 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302427 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302428 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302429static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302430{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302431 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2432
2433 if (hba->ufs_version == UFSHCI_VERSION_10) {
2434 u32 rw;
2435 rw = set & INTERRUPT_MASK_RW_VER_10;
2436 set = rw | ((set ^ intrs) & intrs);
2437 } else {
2438 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302439 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302440
2441 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
2442}
2443
2444/**
2445 * ufshcd_disable_intr - disable interrupts
2446 * @hba: per adapter instance
2447 * @intrs: interrupt bits
2448 */
2449static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
2450{
2451 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2452
2453 if (hba->ufs_version == UFSHCI_VERSION_10) {
2454 u32 rw;
2455 rw = (set & INTERRUPT_MASK_RW_VER_10) &
2456 ~(intrs & INTERRUPT_MASK_RW_VER_10);
2457 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
2458
2459 } else {
2460 set &= ~intrs;
2461 }
2462
2463 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302464}
2465
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002466static int ufshcd_prepare_crypto_utrd(struct ufs_hba *hba,
2467 struct ufshcd_lrb *lrbp)
2468{
2469 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
2470 u8 cc_index = 0;
2471 bool enable = false;
2472 u64 dun = 0;
2473 int ret;
2474
2475 /*
2476 * Call vendor specific code to get crypto info for this request:
2477 * enable, crypto config. index, DUN.
2478 * If bypass is set, don't bother setting the other fields.
2479 */
2480 ret = ufshcd_vops_crypto_req_setup(hba, lrbp, &cc_index, &enable, &dun);
2481 if (ret) {
2482 if (ret != -EAGAIN) {
2483 dev_err(hba->dev,
2484 "%s: failed to setup crypto request (%d)\n",
2485 __func__, ret);
2486 }
2487
2488 return ret;
2489 }
2490
2491 if (!enable)
2492 goto out;
2493
2494 req_desc->header.dword_0 |= cc_index | UTRD_CRYPTO_ENABLE;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002495 req_desc->header.dword_1 = (u32)(dun & 0xFFFFFFFF);
2496 req_desc->header.dword_3 = (u32)((dun >> 32) & 0xFFFFFFFF);
2497out:
2498 return 0;
2499}
2500
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302501/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302502 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
2503 * descriptor according to request
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002504 * @hba: per adapter instance
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302505 * @lrbp: pointer to local reference block
2506 * @upiu_flags: flags required in the header
2507 * @cmd_dir: requests data direction
2508 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002509static int ufshcd_prepare_req_desc_hdr(struct ufs_hba *hba,
2510 struct ufshcd_lrb *lrbp, u32 *upiu_flags,
2511 enum dma_data_direction cmd_dir)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302512{
2513 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
2514 u32 data_direction;
2515 u32 dword_0;
2516
2517 if (cmd_dir == DMA_FROM_DEVICE) {
2518 data_direction = UTP_DEVICE_TO_HOST;
2519 *upiu_flags = UPIU_CMD_FLAGS_READ;
2520 } else if (cmd_dir == DMA_TO_DEVICE) {
2521 data_direction = UTP_HOST_TO_DEVICE;
2522 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
2523 } else {
2524 data_direction = UTP_NO_DATA_TRANSFER;
2525 *upiu_flags = UPIU_CMD_FLAGS_NONE;
2526 }
2527
2528 dword_0 = data_direction | (lrbp->command_type
2529 << UPIU_COMMAND_TYPE_OFFSET);
2530 if (lrbp->intr_cmd)
2531 dword_0 |= UTP_REQ_DESC_INT_CMD;
2532
2533 /* Transfer request descriptor header fields */
2534 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002535 /* dword_1 is reserved, hence it is set to 0 */
2536 req_desc->header.dword_1 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302537 /*
2538 * assigning invalid value for command status. Controller
2539 * updates OCS on command completion, with the command
2540 * status
2541 */
2542 req_desc->header.dword_2 =
2543 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002544 /* dword_3 is reserved, hence it is set to 0 */
2545 req_desc->header.dword_3 = 0;
Yaniv Gardi51047262016-02-01 15:02:38 +02002546
2547 req_desc->prd_table_length = 0;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002548
2549 if (ufshcd_is_crypto_supported(hba))
2550 return ufshcd_prepare_crypto_utrd(hba, lrbp);
2551
2552 return 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302553}
2554
2555/**
2556 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
2557 * for scsi commands
2558 * @lrbp - local reference block pointer
2559 * @upiu_flags - flags
2560 */
2561static
2562void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
2563{
2564 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002565 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302566
2567 /* command descriptor fields */
2568 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2569 UPIU_TRANSACTION_COMMAND, upiu_flags,
2570 lrbp->lun, lrbp->task_tag);
2571 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2572 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
2573
2574 /* Total EHS length and Data segment length will be zero */
2575 ucd_req_ptr->header.dword_2 = 0;
2576
2577 ucd_req_ptr->sc.exp_data_transfer_len =
2578 cpu_to_be32(lrbp->cmd->sdb.length);
2579
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002580 cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002581 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002582 if (cdb_len < MAX_CDB_SIZE)
2583 memset(ucd_req_ptr->sc.cdb + cdb_len, 0,
2584 (MAX_CDB_SIZE - cdb_len));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002585 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302586}
2587
Dolev Raviv68078d52013-07-30 00:35:58 +05302588/**
2589 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
2590 * for query requsts
2591 * @hba: UFS hba
2592 * @lrbp: local reference block pointer
2593 * @upiu_flags: flags
2594 */
2595static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
2596 struct ufshcd_lrb *lrbp, u32 upiu_flags)
2597{
2598 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2599 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302600 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05302601 u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
2602
2603 /* Query request header */
2604 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2605 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
2606 lrbp->lun, lrbp->task_tag);
2607 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2608 0, query->request.query_func, 0, 0);
2609
Zang Leigang68612852016-08-25 17:39:19 +08002610 /* Data segment length only need for WRITE_DESC */
2611 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
2612 ucd_req_ptr->header.dword_2 =
2613 UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len);
2614 else
2615 ucd_req_ptr->header.dword_2 = 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05302616
2617 /* Copy the Query Request buffer as is */
2618 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
2619 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05302620
2621 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002622 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
2623 memcpy(descp, query->descriptor, len);
2624
Yaniv Gardi51047262016-02-01 15:02:38 +02002625 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Dolev Raviv68078d52013-07-30 00:35:58 +05302626}
2627
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302628static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
2629{
2630 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2631
2632 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
2633
2634 /* command descriptor fields */
2635 ucd_req_ptr->header.dword_0 =
2636 UPIU_HEADER_DWORD(
2637 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
Yaniv Gardi51047262016-02-01 15:02:38 +02002638 /* clear rest of the fields of basic header */
2639 ucd_req_ptr->header.dword_1 = 0;
2640 ucd_req_ptr->header.dword_2 = 0;
2641
2642 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302643}
2644
2645/**
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002646 * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302647 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302648 * @lrb - pointer to local reference block
2649 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002650static int ufshcd_compose_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302651{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302652 u32 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302653 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302654
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002655 switch (lrbp->command_type) {
2656 case UTP_CMD_TYPE_SCSI:
2657 if (likely(lrbp->cmd)) {
2658 ret = ufshcd_prepare_req_desc_hdr(hba, lrbp,
2659 &upiu_flags, lrbp->cmd->sc_data_direction);
2660 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
2661 } else {
2662 ret = -EINVAL;
2663 }
2664 break;
2665 case UTP_CMD_TYPE_DEV_MANAGE:
2666 ret = ufshcd_prepare_req_desc_hdr(hba, lrbp, &upiu_flags,
2667 DMA_NONE);
2668 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
2669 ufshcd_prepare_utp_query_req_upiu(
2670 hba, lrbp, upiu_flags);
2671 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
2672 ufshcd_prepare_utp_nop_upiu(lrbp);
2673 else
2674 ret = -EINVAL;
2675 break;
2676 case UTP_CMD_TYPE_UFS:
2677 /* For UFS native command implementation */
2678 ret = -ENOTSUPP;
2679 dev_err(hba->dev, "%s: UFS native command are not supported\n",
2680 __func__);
2681 break;
2682 default:
2683 ret = -ENOTSUPP;
2684 dev_err(hba->dev, "%s: unknown command type: 0x%x\n",
2685 __func__, lrbp->command_type);
2686 break;
2687 } /* end of switch */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302688
2689 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302690}
2691
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002692/*
2693 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
2694 * @scsi_lun: scsi LUN id
2695 *
2696 * Returns UPIU LUN id
2697 */
2698static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
2699{
2700 if (scsi_is_wlun(scsi_lun))
2701 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
2702 | UFS_UPIU_WLUN_ID;
2703 else
2704 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
2705}
2706
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302707/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03002708 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
2709 * @scsi_lun: UPIU W-LUN id
2710 *
2711 * Returns SCSI W-LUN id
2712 */
2713static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
2714{
2715 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
2716}
2717
2718/**
Subhash Jadavani9c807702017-04-01 00:35:51 -07002719 * ufshcd_get_write_lock - synchronize between shutdown, scaling &
2720 * arrival of requests
2721 * @hba: ufs host
2722 *
2723 * Lock is predominantly held by shutdown context thus, ensuring
2724 * that no requests from any other context may sneak through.
2725 */
2726static inline void ufshcd_get_write_lock(struct ufs_hba *hba)
2727{
2728 down_write(&hba->lock);
2729}
2730
2731/**
2732 * ufshcd_get_read_lock - synchronize between shutdown, scaling &
2733 * arrival of requests
2734 * @hba: ufs host
2735 *
2736 * Returns 1 if acquired, < 0 on contention
2737 *
2738 * After shutdown's initiated, allow requests only directed to the
2739 * well known device lun. The sync between scaling & issue is maintained
2740 * as is and this restructuring syncs shutdown with these too.
2741 */
2742static int ufshcd_get_read_lock(struct ufs_hba *hba, u64 lun)
2743{
2744 int err = 0;
2745
2746 err = down_read_trylock(&hba->lock);
2747 if (err > 0)
2748 goto out;
2749 /* let requests for well known device lun to go through */
2750 if (ufshcd_scsi_to_upiu_lun(lun) == UFS_UPIU_UFS_DEVICE_WLUN)
2751 return 0;
2752 else if (!ufshcd_is_shutdown_ongoing(hba))
2753 return -EAGAIN;
2754 else
2755 return -EPERM;
2756
2757out:
2758 return err;
2759}
2760
2761/**
2762 * ufshcd_put_read_lock - synchronize between shutdown, scaling &
2763 * arrival of requests
2764 * @hba: ufs host
2765 *
2766 * Returns none
2767 */
2768static inline void ufshcd_put_read_lock(struct ufs_hba *hba)
2769{
2770 up_read(&hba->lock);
2771}
2772
2773/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302774 * ufshcd_queuecommand - main entry point for SCSI requests
2775 * @cmd: command from SCSI Midlayer
2776 * @done: call back function
2777 *
2778 * Returns 0 for success, non-zero in case of failure
2779 */
2780static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
2781{
2782 struct ufshcd_lrb *lrbp;
2783 struct ufs_hba *hba;
2784 unsigned long flags;
2785 int tag;
2786 int err = 0;
Subhash Jadavani9c807702017-04-01 00:35:51 -07002787 bool has_read_lock = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302788
2789 hba = shost_priv(host);
2790
Subhash Jadavani9c807702017-04-01 00:35:51 -07002791 if (!cmd || !cmd->request || !hba)
2792 return -EINVAL;
2793
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302794 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02002795 if (!ufshcd_valid_tag(hba, tag)) {
2796 dev_err(hba->dev,
2797 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
2798 __func__, tag, cmd, cmd->request);
2799 BUG();
2800 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302801
Subhash Jadavani9c807702017-04-01 00:35:51 -07002802 err = ufshcd_get_read_lock(hba, cmd->device->lun);
2803 if (unlikely(err < 0)) {
2804 if (err == -EPERM) {
2805 set_host_byte(cmd, DID_ERROR);
2806 cmd->scsi_done(cmd);
2807 return 0;
2808 }
2809 if (err == -EAGAIN)
2810 return SCSI_MLQUEUE_HOST_BUSY;
2811 } else if (err == 1) {
2812 has_read_lock = true;
2813 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002814
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302815 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani9c807702017-04-01 00:35:51 -07002816
2817 /* if error handling is in progress, return host busy */
2818 if (ufshcd_eh_in_progress(hba)) {
2819 err = SCSI_MLQUEUE_HOST_BUSY;
2820 goto out_unlock;
2821 }
2822
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302823 switch (hba->ufshcd_state) {
2824 case UFSHCD_STATE_OPERATIONAL:
2825 break;
2826 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302827 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302828 goto out_unlock;
2829 case UFSHCD_STATE_ERROR:
2830 set_host_byte(cmd, DID_ERROR);
2831 cmd->scsi_done(cmd);
2832 goto out_unlock;
2833 default:
2834 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
2835 __func__, hba->ufshcd_state);
2836 set_host_byte(cmd, DID_BAD_TARGET);
2837 cmd->scsi_done(cmd);
2838 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302839 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302840 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302841
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002842 hba->req_abort_count = 0;
2843
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302844 /* acquire the tag to make sure device cmds don't use it */
2845 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
2846 /*
2847 * Dev manage command in progress, requeue the command.
2848 * Requeuing the command helps in cases where the request *may*
2849 * find different tag instead of waiting for dev manage command
2850 * completion.
2851 */
2852 err = SCSI_MLQUEUE_HOST_BUSY;
2853 goto out;
2854 }
2855
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002856 err = ufshcd_hold(hba, true);
2857 if (err) {
2858 err = SCSI_MLQUEUE_HOST_BUSY;
2859 clear_bit_unlock(tag, &hba->lrb_in_use);
2860 goto out;
2861 }
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -07002862
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002863 if (ufshcd_is_clkgating_allowed(hba))
2864 WARN_ON(hba->clk_gating.state != CLKS_ON);
2865
2866 err = ufshcd_hibern8_hold(hba, true);
2867 if (err) {
2868 clear_bit_unlock(tag, &hba->lrb_in_use);
2869 err = SCSI_MLQUEUE_HOST_BUSY;
2870 ufshcd_release(hba, true);
2871 goto out;
2872 }
2873 if (ufshcd_is_hibern8_on_idle_allowed(hba))
2874 WARN_ON(hba->hibern8_on_idle.state != HIBERN8_EXITED);
2875
2876 /* Vote PM QoS for the request */
2877 ufshcd_vops_pm_qos_req_start(hba, cmd->request);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002878
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -07002879 /* IO svc time latency histogram */
Subhash Jadavani9c807702017-04-01 00:35:51 -07002880 if (hba->latency_hist_enabled &&
2881 (cmd->request->cmd_type == REQ_TYPE_FS)) {
2882 cmd->request->lat_hist_io_start = ktime_get();
2883 cmd->request->lat_hist_enabled = 1;
2884 } else {
2885 cmd->request->lat_hist_enabled = 0;
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -07002886 }
2887
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302888 WARN_ON(hba->clk_gating.state != CLKS_ON);
2889
2890 lrbp = &hba->lrb[tag];
2891
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302892 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302893 lrbp->cmd = cmd;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002894 lrbp->sense_bufflen = UFSHCD_REQ_SENSE_SIZE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302895 lrbp->sense_buffer = cmd->sense_buffer;
2896 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002897 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03002898 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002899 lrbp->command_type = UTP_CMD_TYPE_SCSI;
2900 lrbp->req_abort_skip = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302901
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002902 /* form UPIU before issuing the command */
2903 err = ufshcd_compose_upiu(hba, lrbp);
2904 if (err) {
2905 if (err != -EAGAIN)
2906 dev_err(hba->dev,
2907 "%s: failed to compose upiu %d\n",
2908 __func__, err);
Stephen Boyd9bc70c32017-03-01 16:58:38 -08002909 lrbp->cmd = NULL;
2910 clear_bit_unlock(tag, &hba->lrb_in_use);
2911 ufshcd_release_all(hba);
2912 ufshcd_vops_pm_qos_req_end(hba, cmd->request, true);
2913 goto out;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002914 }
Joao Pinto300bb132016-05-11 12:21:27 +01002915
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302916 err = ufshcd_map_sg(lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302917 if (err) {
2918 lrbp->cmd = NULL;
2919 clear_bit_unlock(tag, &hba->lrb_in_use);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002920 ufshcd_release_all(hba);
2921 ufshcd_vops_pm_qos_req_end(hba, cmd->request, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302922 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302923 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302924
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002925 err = ufshcd_vops_crypto_engine_cfg_start(hba, tag);
2926 if (err) {
2927 if (err != -EAGAIN)
2928 dev_err(hba->dev,
2929 "%s: failed to configure crypto engine %d\n",
2930 __func__, err);
2931
2932 scsi_dma_unmap(lrbp->cmd);
2933 lrbp->cmd = NULL;
2934 clear_bit_unlock(tag, &hba->lrb_in_use);
2935 ufshcd_release_all(hba);
2936 ufshcd_vops_pm_qos_req_end(hba, cmd->request, true);
2937
2938 goto out;
2939 }
2940
2941 /* Make sure descriptors are ready before ringing the doorbell */
2942 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302943 /* issue command to the controller */
2944 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002945
2946 err = ufshcd_send_command(hba, tag);
2947 if (err) {
2948 spin_unlock_irqrestore(hba->host->host_lock, flags);
2949 scsi_dma_unmap(lrbp->cmd);
2950 lrbp->cmd = NULL;
2951 clear_bit_unlock(tag, &hba->lrb_in_use);
2952 ufshcd_release_all(hba);
2953 ufshcd_vops_pm_qos_req_end(hba, cmd->request, true);
2954 ufshcd_vops_crypto_engine_cfg_end(hba, lrbp, cmd->request);
2955 dev_err(hba->dev, "%s: failed sending command, %d\n",
2956 __func__, err);
2957 err = DID_ERROR;
2958 goto out;
2959 }
2960
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302961out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302962 spin_unlock_irqrestore(hba->host->host_lock, flags);
2963out:
Subhash Jadavani9c807702017-04-01 00:35:51 -07002964 if (has_read_lock)
2965 ufshcd_put_read_lock(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302966 return err;
2967}
2968
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302969static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
2970 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
2971{
2972 lrbp->cmd = NULL;
2973 lrbp->sense_bufflen = 0;
2974 lrbp->sense_buffer = NULL;
2975 lrbp->task_tag = tag;
2976 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002977 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302978 lrbp->intr_cmd = true; /* No interrupt aggregation */
2979 hba->dev_cmd.type = cmd_type;
2980
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002981 return ufshcd_compose_upiu(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302982}
2983
2984static int
2985ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
2986{
2987 int err = 0;
2988 unsigned long flags;
2989 u32 mask = 1 << tag;
2990
2991 /* clear outstanding transaction before retry */
2992 spin_lock_irqsave(hba->host->host_lock, flags);
2993 ufshcd_utrl_clear(hba, tag);
2994 spin_unlock_irqrestore(hba->host->host_lock, flags);
2995
2996 /*
2997 * wait for for h/w to clear corresponding bit in door-bell.
2998 * max. wait is 1 sec.
2999 */
3000 err = ufshcd_wait_for_register(hba,
3001 REG_UTP_TRANSFER_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02003002 mask, ~mask, 1000, 1000, true);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303003
3004 return err;
3005}
3006
Dolev Ravivc6d4a832014-06-29 09:40:18 +03003007static int
3008ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
3009{
3010 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
3011
3012 /* Get the UPIU response */
3013 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
3014 UPIU_RSP_CODE_OFFSET;
3015 return query_res->response;
3016}
3017
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303018/**
3019 * ufshcd_dev_cmd_completion() - handles device management command responses
3020 * @hba: per adapter instance
3021 * @lrbp: pointer to local reference block
3022 */
3023static int
3024ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
3025{
3026 int resp;
3027 int err = 0;
3028
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003029 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303030 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
3031
3032 switch (resp) {
3033 case UPIU_TRANSACTION_NOP_IN:
3034 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
3035 err = -EINVAL;
3036 dev_err(hba->dev, "%s: unexpected response %x\n",
3037 __func__, resp);
3038 }
3039 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05303040 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03003041 err = ufshcd_check_query_response(hba, lrbp);
3042 if (!err)
3043 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05303044 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303045 case UPIU_TRANSACTION_REJECT_UPIU:
3046 /* TODO: handle Reject UPIU Response */
3047 err = -EPERM;
3048 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
3049 __func__);
3050 break;
3051 default:
3052 err = -EINVAL;
3053 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
3054 __func__, resp);
3055 break;
3056 }
3057
3058 return err;
3059}
3060
3061static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
3062 struct ufshcd_lrb *lrbp, int max_timeout)
3063{
3064 int err = 0;
3065 unsigned long time_left;
3066 unsigned long flags;
3067
3068 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
3069 msecs_to_jiffies(max_timeout));
3070
3071 spin_lock_irqsave(hba->host->host_lock, flags);
3072 hba->dev_cmd.complete = NULL;
3073 if (likely(time_left)) {
3074 err = ufshcd_get_tr_ocs(lrbp);
3075 if (!err)
3076 err = ufshcd_dev_cmd_completion(hba, lrbp);
3077 }
3078 spin_unlock_irqrestore(hba->host->host_lock, flags);
3079
3080 if (!time_left) {
3081 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02003082 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
3083 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303084 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02003085 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303086 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02003087 /*
3088 * in case of an error, after clearing the doorbell,
3089 * we also need to clear the outstanding_request
3090 * field in hba
3091 */
3092 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303093 }
3094
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003095 if (err)
3096 ufsdbg_set_err_state(hba);
3097
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303098 return err;
3099}
3100
3101/**
3102 * ufshcd_get_dev_cmd_tag - Get device management command tag
3103 * @hba: per-adapter instance
3104 * @tag: pointer to variable with available slot value
3105 *
3106 * Get a free slot and lock it until device management command
3107 * completes.
3108 *
3109 * Returns false if free slot is unavailable for locking, else
3110 * return true with tag value in @tag.
3111 */
3112static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
3113{
3114 int tag;
3115 bool ret = false;
3116 unsigned long tmp;
3117
3118 if (!tag_out)
3119 goto out;
3120
3121 do {
3122 tmp = ~hba->lrb_in_use;
3123 tag = find_last_bit(&tmp, hba->nutrs);
3124 if (tag >= hba->nutrs)
3125 goto out;
3126 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
3127
3128 *tag_out = tag;
3129 ret = true;
3130out:
3131 return ret;
3132}
3133
3134static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
3135{
3136 clear_bit_unlock(tag, &hba->lrb_in_use);
3137}
3138
3139/**
3140 * ufshcd_exec_dev_cmd - API for sending device management requests
3141 * @hba - UFS hba
3142 * @cmd_type - specifies the type (NOP, Query...)
3143 * @timeout - time in seconds
3144 *
Dolev Raviv68078d52013-07-30 00:35:58 +05303145 * NOTE: Since there is only one available tag for device management commands,
3146 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303147 */
3148static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
3149 enum dev_cmd_type cmd_type, int timeout)
3150{
3151 struct ufshcd_lrb *lrbp;
3152 int err;
3153 int tag;
3154 struct completion wait;
3155 unsigned long flags;
3156
Subhash Jadavani9c807702017-04-01 00:35:51 -07003157 /*
3158 * May get invoked from shutdown and IOCTL contexts.
3159 * In shutdown context, it comes in with lock acquired.
3160 */
3161 if (!ufshcd_is_shutdown_ongoing(hba))
3162 down_read(&hba->lock);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003163
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303164 /*
3165 * Get free slot, sleep if slots are unavailable.
3166 * Even though we use wait_event() which sleeps indefinitely,
3167 * the maximum wait time is bounded by SCSI request timeout.
3168 */
3169 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
3170
3171 init_completion(&wait);
3172 lrbp = &hba->lrb[tag];
3173 WARN_ON(lrbp->cmd);
3174 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
3175 if (unlikely(err))
3176 goto out_put_tag;
3177
3178 hba->dev_cmd.complete = &wait;
3179
Yaniv Gardie3dfdc52016-02-01 15:02:49 +02003180 /* Make sure descriptors are ready before ringing the doorbell */
3181 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303182 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003183 err = ufshcd_send_command(hba, tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303184 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003185 if (err) {
3186 dev_err(hba->dev, "%s: failed sending command, %d\n",
3187 __func__, err);
3188 goto out_put_tag;
3189 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303190 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
3191
3192out_put_tag:
3193 ufshcd_put_dev_cmd_tag(hba, tag);
3194 wake_up(&hba->dev_cmd.tag_wq);
Subhash Jadavani9c807702017-04-01 00:35:51 -07003195 if (!ufshcd_is_shutdown_ongoing(hba))
3196 up_read(&hba->lock);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303197 return err;
3198}
3199
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303200/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003201 * ufshcd_init_query() - init the query response and request parameters
3202 * @hba: per-adapter instance
3203 * @request: address of the request pointer to be initialized
3204 * @response: address of the response pointer to be initialized
3205 * @opcode: operation to perform
3206 * @idn: flag idn to access
3207 * @index: LU number to access
3208 * @selector: query/flag/descriptor further identification
3209 */
3210static inline void ufshcd_init_query(struct ufs_hba *hba,
3211 struct ufs_query_req **request, struct ufs_query_res **response,
3212 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
3213{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003214 int idn_t = (int)idn;
3215
3216 ufsdbg_error_inject_dispatcher(hba,
3217 ERR_INJECT_QUERY, idn_t, (int *)&idn_t);
3218 idn = idn_t;
3219
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003220 *request = &hba->dev_cmd.query.request;
3221 *response = &hba->dev_cmd.query.response;
3222 memset(*request, 0, sizeof(struct ufs_query_req));
3223 memset(*response, 0, sizeof(struct ufs_query_res));
3224 (*request)->upiu_req.opcode = opcode;
3225 (*request)->upiu_req.idn = idn;
3226 (*request)->upiu_req.index = index;
3227 (*request)->upiu_req.selector = selector;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003228
3229 ufshcd_update_query_stats(hba, opcode, idn);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003230}
3231
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003232static int ufshcd_query_flag_retry(struct ufs_hba *hba,
3233 enum query_opcode opcode, enum flag_idn idn, bool *flag_res)
3234{
3235 int ret;
3236 int retries;
3237
3238 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
3239 ret = ufshcd_query_flag(hba, opcode, idn, flag_res);
3240 if (ret)
3241 dev_dbg(hba->dev,
3242 "%s: failed with error %d, retries %d\n",
3243 __func__, ret, retries);
3244 else
3245 break;
3246 }
3247
3248 if (ret)
3249 dev_err(hba->dev,
3250 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
3251 __func__, opcode, idn, ret, retries);
3252 return ret;
3253}
3254
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003255/**
Dolev Raviv68078d52013-07-30 00:35:58 +05303256 * ufshcd_query_flag() - API function for sending flag query requests
3257 * hba: per-adapter instance
3258 * query_opcode: flag query to perform
3259 * idn: flag idn to access
3260 * flag_res: the flag value after the query request completes
3261 *
3262 * Returns 0 for success, non-zero in case of failure
3263 */
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003264int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Dolev Raviv68078d52013-07-30 00:35:58 +05303265 enum flag_idn idn, bool *flag_res)
3266{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003267 struct ufs_query_req *request = NULL;
3268 struct ufs_query_res *response = NULL;
3269 int err, index = 0, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02003270 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05303271
3272 BUG_ON(!hba);
3273
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003274 ufshcd_hold_all(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05303275 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003276 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
3277 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05303278
3279 switch (opcode) {
3280 case UPIU_QUERY_OPCODE_SET_FLAG:
3281 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
3282 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
3283 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
3284 break;
3285 case UPIU_QUERY_OPCODE_READ_FLAG:
3286 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
3287 if (!flag_res) {
3288 /* No dummy reads */
3289 dev_err(hba->dev, "%s: Invalid argument for read request\n",
3290 __func__);
3291 err = -EINVAL;
3292 goto out_unlock;
3293 }
3294 break;
3295 default:
3296 dev_err(hba->dev,
3297 "%s: Expected query flag opcode but got = %d\n",
3298 __func__, opcode);
3299 err = -EINVAL;
3300 goto out_unlock;
3301 }
Dolev Raviv68078d52013-07-30 00:35:58 +05303302
Yaniv Gardie5ad4062016-02-01 15:02:41 +02003303 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05303304
3305 if (err) {
3306 dev_err(hba->dev,
3307 "%s: Sending flag query for idn %d failed, err = %d\n",
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003308 __func__, request->upiu_req.idn, err);
Dolev Raviv68078d52013-07-30 00:35:58 +05303309 goto out_unlock;
3310 }
3311
3312 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05303313 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05303314 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
3315
3316out_unlock:
3317 mutex_unlock(&hba->dev_cmd.lock);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003318 ufshcd_release_all(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05303319 return err;
3320}
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003321EXPORT_SYMBOL(ufshcd_query_flag);
Dolev Raviv68078d52013-07-30 00:35:58 +05303322
3323/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303324 * ufshcd_query_attr - API function for sending attribute requests
3325 * hba: per-adapter instance
3326 * opcode: attribute opcode
3327 * idn: attribute idn to access
3328 * index: index field
3329 * selector: selector field
3330 * attr_val: the attribute value after the query request completes
3331 *
3332 * Returns 0 for success, non-zero in case of failure
3333*/
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003334int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303335 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
3336{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003337 struct ufs_query_req *request = NULL;
3338 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303339 int err;
3340
3341 BUG_ON(!hba);
3342
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003343 ufshcd_hold_all(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303344 if (!attr_val) {
3345 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
3346 __func__, opcode);
3347 err = -EINVAL;
3348 goto out;
3349 }
3350
3351 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003352 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
3353 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303354
3355 switch (opcode) {
3356 case UPIU_QUERY_OPCODE_WRITE_ATTR:
3357 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05303358 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303359 break;
3360 case UPIU_QUERY_OPCODE_READ_ATTR:
3361 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
3362 break;
3363 default:
3364 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
3365 __func__, opcode);
3366 err = -EINVAL;
3367 goto out_unlock;
3368 }
3369
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003370 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303371
3372 if (err) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003373 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
3374 __func__, opcode,
3375 request->upiu_req.idn, index, err);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303376 goto out_unlock;
3377 }
3378
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05303379 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303380
3381out_unlock:
3382 mutex_unlock(&hba->dev_cmd.lock);
3383out:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003384 ufshcd_release_all(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303385 return err;
3386}
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003387EXPORT_SYMBOL(ufshcd_query_attr);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303388
3389/**
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003390 * ufshcd_query_attr_retry() - API function for sending query
3391 * attribute with retries
3392 * @hba: per-adapter instance
3393 * @opcode: attribute opcode
3394 * @idn: attribute idn to access
3395 * @index: index field
3396 * @selector: selector field
3397 * @attr_val: the attribute value after the query request
3398 * completes
3399 *
3400 * Returns 0 for success, non-zero in case of failure
3401*/
3402static int ufshcd_query_attr_retry(struct ufs_hba *hba,
3403 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
3404 u32 *attr_val)
3405{
3406 int ret = 0;
3407 u32 retries;
3408
3409 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
3410 ret = ufshcd_query_attr(hba, opcode, idn, index,
3411 selector, attr_val);
3412 if (ret)
3413 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
3414 __func__, ret, retries);
3415 else
3416 break;
3417 }
3418
3419 if (ret)
3420 dev_err(hba->dev,
3421 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003422 __func__, idn, ret, retries);
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003423 return ret;
3424}
3425
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003426static int __ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003427 enum query_opcode opcode, enum desc_idn idn, u8 index,
3428 u8 selector, u8 *desc_buf, int *buf_len)
3429{
3430 struct ufs_query_req *request = NULL;
3431 struct ufs_query_res *response = NULL;
3432 int err;
3433
3434 BUG_ON(!hba);
3435
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003436 ufshcd_hold_all(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003437 if (!desc_buf) {
3438 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
3439 __func__, opcode);
3440 err = -EINVAL;
3441 goto out;
3442 }
3443
3444 if (*buf_len <= QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
3445 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
3446 __func__, *buf_len);
3447 err = -EINVAL;
3448 goto out;
3449 }
3450
3451 mutex_lock(&hba->dev_cmd.lock);
3452 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
3453 selector);
3454 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03003455 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003456
3457 switch (opcode) {
3458 case UPIU_QUERY_OPCODE_WRITE_DESC:
3459 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
3460 break;
3461 case UPIU_QUERY_OPCODE_READ_DESC:
3462 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
3463 break;
3464 default:
3465 dev_err(hba->dev,
3466 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
3467 __func__, opcode);
3468 err = -EINVAL;
3469 goto out_unlock;
3470 }
3471
3472 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
3473
3474 if (err) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003475 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
3476 __func__, opcode,
3477 request->upiu_req.idn, index, err);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003478 goto out_unlock;
3479 }
3480
3481 hba->dev_cmd.query.descriptor = NULL;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03003482 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003483
3484out_unlock:
3485 mutex_unlock(&hba->dev_cmd.lock);
3486out:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003487 ufshcd_release_all(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003488 return err;
3489}
3490
3491/**
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003492 * ufshcd_query_descriptor - API function for sending descriptor requests
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003493 * hba: per-adapter instance
3494 * opcode: attribute opcode
3495 * idn: attribute idn to access
3496 * index: index field
3497 * selector: selector field
3498 * desc_buf: the buffer that contains the descriptor
3499 * buf_len: length parameter passed to the device
3500 *
3501 * Returns 0 for success, non-zero in case of failure.
3502 * The buf_len parameter will contain, on return, the length parameter
3503 * received on the response.
3504 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003505int ufshcd_query_descriptor(struct ufs_hba *hba,
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003506 enum query_opcode opcode, enum desc_idn idn, u8 index,
3507 u8 selector, u8 *desc_buf, int *buf_len)
3508{
3509 int err;
3510 int retries;
3511
3512 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
3513 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
3514 selector, desc_buf, buf_len);
3515 if (!err || err == -EINVAL)
3516 break;
3517 }
3518
3519 return err;
3520}
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003521EXPORT_SYMBOL(ufshcd_query_descriptor);
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003522
3523/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003524 * ufshcd_read_desc_param - read the specified descriptor parameter
3525 * @hba: Pointer to adapter instance
3526 * @desc_id: descriptor idn value
3527 * @desc_index: descriptor index
3528 * @param_offset: offset of the parameter to read
3529 * @param_read_buf: pointer to buffer where parameter would be read
3530 * @param_size: sizeof(param_read_buf)
3531 *
3532 * Return 0 in case of success, non-zero otherwise
3533 */
3534static int ufshcd_read_desc_param(struct ufs_hba *hba,
3535 enum desc_idn desc_id,
3536 int desc_index,
3537 u32 param_offset,
3538 u8 *param_read_buf,
3539 u32 param_size)
3540{
3541 int ret;
3542 u8 *desc_buf;
3543 u32 buff_len;
3544 bool is_kmalloc = true;
3545
3546 /* safety checks */
3547 if (desc_id >= QUERY_DESC_IDN_MAX)
3548 return -EINVAL;
3549
3550 buff_len = ufs_query_desc_max_size[desc_id];
3551 if ((param_offset + param_size) > buff_len)
3552 return -EINVAL;
3553
3554 if (!param_offset && (param_size == buff_len)) {
3555 /* memory space already available to hold full descriptor */
3556 desc_buf = param_read_buf;
3557 is_kmalloc = false;
3558 } else {
3559 /* allocate memory to hold full descriptor */
3560 desc_buf = kmalloc(buff_len, GFP_KERNEL);
3561 if (!desc_buf)
3562 return -ENOMEM;
3563 }
3564
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003565 ret = ufshcd_query_descriptor(hba, UPIU_QUERY_OPCODE_READ_DESC,
3566 desc_id, desc_index, 0, desc_buf,
3567 &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003568
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003569 if (ret) {
3570 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d",
3571 __func__, desc_id, desc_index, param_offset, ret);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003572
3573 goto out;
3574 }
3575
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003576 /* Sanity check */
3577 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
3578 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header",
3579 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
3580 ret = -EINVAL;
3581 goto out;
3582 }
3583
3584 /*
3585 * While reading variable size descriptors (like string descriptor),
3586 * some UFS devices may report the "LENGTH" (field in "Transaction
3587 * Specific fields" of Query Response UPIU) same as what was requested
3588 * in Query Request UPIU instead of reporting the actual size of the
3589 * variable size descriptor.
3590 * Although it's safe to ignore the "LENGTH" field for variable size
3591 * descriptors as we can always derive the length of the descriptor from
3592 * the descriptor header fields. Hence this change impose the length
3593 * match check only for fixed size descriptors (for which we always
3594 * request the correct size as part of Query Request UPIU).
3595 */
3596 if ((desc_id != QUERY_DESC_IDN_STRING) &&
3597 (buff_len != desc_buf[QUERY_DESC_LENGTH_OFFSET])) {
3598 dev_err(hba->dev, "%s: desc_buf length mismatch: buff_len %d, buff_len(desc_header) %d",
3599 __func__, buff_len, desc_buf[QUERY_DESC_LENGTH_OFFSET]);
3600 ret = -EINVAL;
3601 goto out;
3602 }
3603
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003604 if (is_kmalloc)
3605 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
3606out:
3607 if (is_kmalloc)
3608 kfree(desc_buf);
3609 return ret;
3610}
3611
3612static inline int ufshcd_read_desc(struct ufs_hba *hba,
3613 enum desc_idn desc_id,
3614 int desc_index,
3615 u8 *buf,
3616 u32 size)
3617{
3618 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
3619}
3620
3621static inline int ufshcd_read_power_desc(struct ufs_hba *hba,
3622 u8 *buf,
3623 u32 size)
3624{
3625 return ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size);
3626}
3627
Yaniv Gardib573d482016-03-10 17:37:09 +02003628int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size)
3629{
3630 return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size);
3631}
Yaniv Gardib573d482016-03-10 17:37:09 +02003632
3633/**
3634 * ufshcd_read_string_desc - read string descriptor
3635 * @hba: pointer to adapter instance
3636 * @desc_index: descriptor index
3637 * @buf: pointer to buffer where descriptor would be read
3638 * @size: size of buf
3639 * @ascii: if true convert from unicode to ascii characters
3640 *
3641 * Return 0 in case of success, non-zero otherwise
3642 */
3643int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index, u8 *buf,
3644 u32 size, bool ascii)
3645{
3646 int err = 0;
3647
3648 err = ufshcd_read_desc(hba,
3649 QUERY_DESC_IDN_STRING, desc_index, buf, size);
3650
3651 if (err) {
3652 dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n",
3653 __func__, QUERY_REQ_RETRIES, err);
3654 goto out;
3655 }
3656
3657 if (ascii) {
3658 int desc_len;
3659 int ascii_len;
3660 int i;
3661 char *buff_ascii;
3662
3663 desc_len = buf[0];
3664 /* remove header and divide by 2 to move from UTF16 to UTF8 */
3665 ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1;
3666 if (size < ascii_len + QUERY_DESC_HDR_SIZE) {
3667 dev_err(hba->dev, "%s: buffer allocated size is too small\n",
3668 __func__);
3669 err = -ENOMEM;
3670 goto out;
3671 }
3672
3673 buff_ascii = kmalloc(ascii_len, GFP_KERNEL);
3674 if (!buff_ascii) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003675 dev_err(hba->dev, "%s: Failed allocating %d bytes\n",
3676 __func__, ascii_len);
Yaniv Gardib573d482016-03-10 17:37:09 +02003677 err = -ENOMEM;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003678 goto out_free_buff;
Yaniv Gardib573d482016-03-10 17:37:09 +02003679 }
3680
3681 /*
3682 * the descriptor contains string in UTF16 format
3683 * we need to convert to utf-8 so it can be displayed
3684 */
3685 utf16s_to_utf8s((wchar_t *)&buf[QUERY_DESC_HDR_SIZE],
3686 desc_len - QUERY_DESC_HDR_SIZE,
3687 UTF16_BIG_ENDIAN, buff_ascii, ascii_len);
3688
3689 /* replace non-printable or non-ASCII characters with spaces */
3690 for (i = 0; i < ascii_len; i++)
3691 ufshcd_remove_non_printable(&buff_ascii[i]);
3692
3693 memset(buf + QUERY_DESC_HDR_SIZE, 0,
3694 size - QUERY_DESC_HDR_SIZE);
3695 memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len);
3696 buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003697out_free_buff:
Yaniv Gardib573d482016-03-10 17:37:09 +02003698 kfree(buff_ascii);
3699 }
3700out:
3701 return err;
3702}
Yaniv Gardib573d482016-03-10 17:37:09 +02003703
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003704/**
3705 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
3706 * @hba: Pointer to adapter instance
3707 * @lun: lun id
3708 * @param_offset: offset of the parameter to read
3709 * @param_read_buf: pointer to buffer where parameter would be read
3710 * @param_size: sizeof(param_read_buf)
3711 *
3712 * Return 0 in case of success, non-zero otherwise
3713 */
3714static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
3715 int lun,
3716 enum unit_desc_param param_offset,
3717 u8 *param_read_buf,
3718 u32 param_size)
3719{
3720 /*
3721 * Unit descriptors are only available for general purpose LUs (LUN id
3722 * from 0 to 7) and RPMB Well known LU.
3723 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003724 if (!ufs_is_valid_unit_desc_lun(lun))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003725 return -EOPNOTSUPP;
3726
3727 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
3728 param_offset, param_read_buf, param_size);
3729}
3730
3731/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303732 * ufshcd_memory_alloc - allocate memory for host memory space data structures
3733 * @hba: per adapter instance
3734 *
3735 * 1. Allocate DMA memory for Command Descriptor array
3736 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
3737 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
3738 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
3739 * (UTMRDL)
3740 * 4. Allocate memory for local reference block(lrb).
3741 *
3742 * Returns 0 for success, non-zero in case of failure
3743 */
3744static int ufshcd_memory_alloc(struct ufs_hba *hba)
3745{
3746 size_t utmrdl_size, utrdl_size, ucdl_size;
3747
3748 /* Allocate memory for UTP command descriptors */
3749 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09003750 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
3751 ucdl_size,
3752 &hba->ucdl_dma_addr,
3753 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303754
3755 /*
3756 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
3757 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
3758 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
3759 * be aligned to 128 bytes as well
3760 */
3761 if (!hba->ucdl_base_addr ||
3762 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303763 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303764 "Command Descriptor Memory allocation failed\n");
3765 goto out;
3766 }
3767
3768 /*
3769 * Allocate memory for UTP Transfer descriptors
3770 * UFSHCI requires 1024 byte alignment of UTRD
3771 */
3772 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09003773 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
3774 utrdl_size,
3775 &hba->utrdl_dma_addr,
3776 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303777 if (!hba->utrdl_base_addr ||
3778 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303779 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303780 "Transfer Descriptor Memory allocation failed\n");
3781 goto out;
3782 }
3783
3784 /*
3785 * Allocate memory for UTP Task Management descriptors
3786 * UFSHCI requires 1024 byte alignment of UTMRD
3787 */
3788 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09003789 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
3790 utmrdl_size,
3791 &hba->utmrdl_dma_addr,
3792 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303793 if (!hba->utmrdl_base_addr ||
3794 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303795 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303796 "Task Management Descriptor Memory allocation failed\n");
3797 goto out;
3798 }
3799
3800 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09003801 hba->lrb = devm_kzalloc(hba->dev,
3802 hba->nutrs * sizeof(struct ufshcd_lrb),
3803 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303804 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303805 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303806 goto out;
3807 }
3808 return 0;
3809out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303810 return -ENOMEM;
3811}
3812
3813/**
3814 * ufshcd_host_memory_configure - configure local reference block with
3815 * memory offsets
3816 * @hba: per adapter instance
3817 *
3818 * Configure Host memory space
3819 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
3820 * address.
3821 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
3822 * and PRDT offset.
3823 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
3824 * into local reference block.
3825 */
3826static void ufshcd_host_memory_configure(struct ufs_hba *hba)
3827{
3828 struct utp_transfer_cmd_desc *cmd_descp;
3829 struct utp_transfer_req_desc *utrdlp;
3830 dma_addr_t cmd_desc_dma_addr;
3831 dma_addr_t cmd_desc_element_addr;
3832 u16 response_offset;
3833 u16 prdt_offset;
3834 int cmd_desc_size;
3835 int i;
3836
3837 utrdlp = hba->utrdl_base_addr;
3838 cmd_descp = hba->ucdl_base_addr;
3839
3840 response_offset =
3841 offsetof(struct utp_transfer_cmd_desc, response_upiu);
3842 prdt_offset =
3843 offsetof(struct utp_transfer_cmd_desc, prd_table);
3844
3845 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
3846 cmd_desc_dma_addr = hba->ucdl_dma_addr;
3847
3848 for (i = 0; i < hba->nutrs; i++) {
3849 /* Configure UTRD with command descriptor base address */
3850 cmd_desc_element_addr =
3851 (cmd_desc_dma_addr + (cmd_desc_size * i));
3852 utrdlp[i].command_desc_base_addr_lo =
3853 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
3854 utrdlp[i].command_desc_base_addr_hi =
3855 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
3856
3857 /* Response upiu and prdt offset should be in double words */
3858 utrdlp[i].response_upiu_offset =
3859 cpu_to_le16((response_offset >> 2));
3860 utrdlp[i].prd_table_offset =
3861 cpu_to_le16((prdt_offset >> 2));
3862 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05303863 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303864
3865 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003866 hba->lrb[i].utrd_dma_addr = hba->utrdl_dma_addr +
3867 (i * sizeof(struct utp_transfer_req_desc));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303868 hba->lrb[i].ucd_req_ptr =
3869 (struct utp_upiu_req *)(cmd_descp + i);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003870 hba->lrb[i].ucd_req_dma_addr = cmd_desc_element_addr;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303871 hba->lrb[i].ucd_rsp_ptr =
3872 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003873 hba->lrb[i].ucd_rsp_dma_addr = cmd_desc_element_addr +
3874 response_offset;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303875 hba->lrb[i].ucd_prdt_ptr =
3876 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003877 hba->lrb[i].ucd_prdt_dma_addr = cmd_desc_element_addr +
3878 prdt_offset;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303879 }
3880}
3881
3882/**
3883 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
3884 * @hba: per adapter instance
3885 *
3886 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
3887 * in order to initialize the Unipro link startup procedure.
3888 * Once the Unipro links are up, the device connected to the controller
3889 * is detected.
3890 *
3891 * Returns 0 on success, non-zero value on failure
3892 */
3893static int ufshcd_dme_link_startup(struct ufs_hba *hba)
3894{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303895 struct uic_command uic_cmd = {0};
3896 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303897
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303898 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
3899
3900 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3901 if (ret)
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003902 dev_dbg(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303903 "dme-link-startup: error code %d\n", ret);
3904 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303905}
3906
Yaniv Gardicad2e032015-03-31 17:37:14 +03003907static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
3908{
3909 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
3910 unsigned long min_sleep_time_us;
3911
3912 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
3913 return;
3914
3915 /*
3916 * last_dme_cmd_tstamp will be 0 only for 1st call to
3917 * this function
3918 */
3919 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
3920 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
3921 } else {
3922 unsigned long delta =
3923 (unsigned long) ktime_to_us(
3924 ktime_sub(ktime_get(),
3925 hba->last_dme_cmd_tstamp));
3926
3927 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
3928 min_sleep_time_us =
3929 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
3930 else
3931 return; /* no more delay required */
3932 }
3933
3934 /* allow sleep for extra 50us if needed */
3935 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
3936}
3937
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003938static inline void ufshcd_save_tstamp_of_last_dme_cmd(
3939 struct ufs_hba *hba)
3940{
3941 if (hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS)
3942 hba->last_dme_cmd_tstamp = ktime_get();
3943}
3944
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303945/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303946 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
3947 * @hba: per adapter instance
3948 * @attr_sel: uic command argument1
3949 * @attr_set: attribute set type as uic command argument2
3950 * @mib_val: setting value as uic command argument3
3951 * @peer: indicate whether peer or local
3952 *
3953 * Returns 0 on success, non-zero value on failure
3954 */
3955int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
3956 u8 attr_set, u32 mib_val, u8 peer)
3957{
3958 struct uic_command uic_cmd = {0};
3959 static const char *const action[] = {
3960 "dme-set",
3961 "dme-peer-set"
3962 };
3963 const char *set = action[!!peer];
3964 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003965 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303966
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003967 ufsdbg_error_inject_dispatcher(hba,
3968 ERR_INJECT_DME_ATTR, attr_sel, &attr_sel);
3969
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303970 uic_cmd.command = peer ?
3971 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
3972 uic_cmd.argument1 = attr_sel;
3973 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
3974 uic_cmd.argument3 = mib_val;
3975
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003976 do {
3977 /* for peer attributes we retry upon failure */
3978 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3979 if (ret)
3980 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
3981 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
3982 } while (ret && peer && --retries);
3983
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003984 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003985 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003986 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
3987 UFS_UIC_COMMAND_RETRIES - retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303988
3989 return ret;
3990}
3991EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
3992
3993/**
3994 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
3995 * @hba: per adapter instance
3996 * @attr_sel: uic command argument1
3997 * @mib_val: the value of the attribute as returned by the UIC command
3998 * @peer: indicate whether peer or local
3999 *
4000 * Returns 0 on success, non-zero value on failure
4001 */
4002int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
4003 u32 *mib_val, u8 peer)
4004{
4005 struct uic_command uic_cmd = {0};
4006 static const char *const action[] = {
4007 "dme-get",
4008 "dme-peer-get"
4009 };
4010 const char *get = action[!!peer];
4011 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02004012 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03004013 struct ufs_pa_layer_attr orig_pwr_info;
4014 struct ufs_pa_layer_attr temp_pwr_info;
4015 bool pwr_mode_change = false;
4016
4017 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
4018 orig_pwr_info = hba->pwr_info;
4019 temp_pwr_info = orig_pwr_info;
4020
4021 if (orig_pwr_info.pwr_tx == FAST_MODE ||
4022 orig_pwr_info.pwr_rx == FAST_MODE) {
4023 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
4024 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
4025 pwr_mode_change = true;
4026 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
4027 orig_pwr_info.pwr_rx == SLOW_MODE) {
4028 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
4029 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
4030 pwr_mode_change = true;
4031 }
4032 if (pwr_mode_change) {
4033 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
4034 if (ret)
4035 goto out;
4036 }
4037 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05304038
4039 uic_cmd.command = peer ?
4040 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004041
4042 ufsdbg_error_inject_dispatcher(hba,
4043 ERR_INJECT_DME_ATTR, attr_sel, &attr_sel);
4044
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05304045 uic_cmd.argument1 = attr_sel;
4046
Yaniv Gardi64238fb2016-02-01 15:02:43 +02004047 do {
4048 /* for peer attributes we retry upon failure */
4049 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
4050 if (ret)
4051 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
4052 get, UIC_GET_ATTR_ID(attr_sel), ret);
4053 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05304054
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004055 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02004056 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004057 get, UIC_GET_ATTR_ID(attr_sel),
4058 UFS_UIC_COMMAND_RETRIES - retries);
Yaniv Gardi64238fb2016-02-01 15:02:43 +02004059
4060 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05304061 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03004062
4063 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
4064 && pwr_mode_change)
4065 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05304066out:
4067 return ret;
4068}
4069EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
4070
4071/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004072 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
4073 * state) and waits for it to take effect.
4074 *
4075 * @hba: per adapter instance
4076 * @cmd: UIC command to execute
4077 *
4078 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
4079 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
4080 * and device UniPro link and hence it's final completion would be indicated by
4081 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
4082 * addition to normal UIC command completion Status (UCCS). This function only
4083 * returns after the relevant status bits indicate the completion.
4084 *
4085 * Returns 0 on success, non-zero value on failure
4086 */
4087static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
4088{
4089 struct completion uic_async_done;
4090 unsigned long flags;
4091 u8 status;
4092 int ret;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004093 bool reenable_intr = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004094
4095 mutex_lock(&hba->uic_cmd_mutex);
4096 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03004097 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004098
4099 spin_lock_irqsave(hba->host->host_lock, flags);
4100 hba->uic_async_done = &uic_async_done;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004101 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
4102 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
4103 /*
4104 * Make sure UIC command completion interrupt is disabled before
4105 * issuing UIC command.
4106 */
4107 wmb();
4108 reenable_intr = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004109 }
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004110 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
4111 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004112 if (ret) {
4113 dev_err(hba->dev,
4114 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
4115 cmd->command, cmd->argument3, ret);
4116 goto out;
4117 }
4118
4119 if (!wait_for_completion_timeout(hba->uic_async_done,
4120 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
4121 dev_err(hba->dev,
4122 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
4123 cmd->command, cmd->argument3);
4124 ret = -ETIMEDOUT;
4125 goto out;
4126 }
4127
4128 status = ufshcd_get_upmcrs(hba);
4129 if (status != PWR_LOCAL) {
4130 dev_err(hba->dev,
Kiwoong Kim73615422016-09-08 16:50:02 +09004131 "pwr ctrl cmd 0x%0x failed, host upmcrs:0x%x\n",
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004132 cmd->command, status);
4133 ret = (status != PWR_OK) ? status : -1;
4134 }
4135out:
Subhash Jadavani9c807702017-04-01 00:35:51 -07004136 if (ret) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004137 ufsdbg_set_err_state(hba);
Subhash Jadavani9c807702017-04-01 00:35:51 -07004138 ufshcd_print_host_state(hba);
4139 ufshcd_print_pwr_info(hba);
4140 ufshcd_print_host_regs(hba);
4141 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004142
4143 ufshcd_save_tstamp_of_last_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004144 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004145 hba->active_uic_cmd = NULL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004146 hba->uic_async_done = NULL;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004147 if (reenable_intr)
4148 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004149 spin_unlock_irqrestore(hba->host->host_lock, flags);
4150 mutex_unlock(&hba->uic_cmd_mutex);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004151 return ret;
4152}
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004153
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004154int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba, u64 wait_timeout_us)
4155{
4156 unsigned long flags;
4157 int ret = 0;
4158 u32 tm_doorbell;
4159 u32 tr_doorbell;
4160 bool timeout = false, do_last_check = false;
4161 ktime_t start;
4162
4163 ufshcd_hold_all(hba);
4164 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004165 /*
4166 * Wait for all the outstanding tasks/transfer requests.
4167 * Verify by checking the doorbell registers are clear.
4168 */
4169 start = ktime_get();
4170 do {
Subhash Jadavani9c807702017-04-01 00:35:51 -07004171 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) {
4172 ret = -EBUSY;
4173 goto out;
4174 }
4175
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004176 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
4177 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
4178 if (!tm_doorbell && !tr_doorbell) {
4179 timeout = false;
4180 break;
4181 } else if (do_last_check) {
4182 break;
4183 }
4184
4185 spin_unlock_irqrestore(hba->host->host_lock, flags);
4186 schedule();
4187 if (ktime_to_us(ktime_sub(ktime_get(), start)) >
4188 wait_timeout_us) {
4189 timeout = true;
4190 /*
4191 * We might have scheduled out for long time so make
4192 * sure to check if doorbells are cleared by this time
4193 * or not.
4194 */
4195 do_last_check = true;
4196 }
4197 spin_lock_irqsave(hba->host->host_lock, flags);
4198 } while (tm_doorbell || tr_doorbell);
4199
4200 if (timeout) {
4201 dev_err(hba->dev,
4202 "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n",
4203 __func__, tm_doorbell, tr_doorbell);
4204 ret = -EBUSY;
4205 }
4206out:
4207 spin_unlock_irqrestore(hba->host->host_lock, flags);
4208 ufshcd_release_all(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004209 return ret;
4210}
4211
4212/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304213 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
4214 * using DME_SET primitives.
4215 * @hba: per adapter instance
4216 * @mode: powr mode value
4217 *
4218 * Returns 0 on success, non-zero value on failure
4219 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05304220static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304221{
4222 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004223 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304224
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03004225 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
4226 ret = ufshcd_dme_set(hba,
4227 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
4228 if (ret) {
4229 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
4230 __func__, ret);
4231 goto out;
4232 }
4233 }
4234
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304235 uic_cmd.command = UIC_CMD_DME_SET;
4236 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
4237 uic_cmd.argument3 = mode;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004238 ufshcd_hold_all(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004239 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004240 ufshcd_release_all(hba);
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03004241out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004242 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004243}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304244
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004245static int ufshcd_link_recovery(struct ufs_hba *hba)
4246{
Subhash Jadavani9c807702017-04-01 00:35:51 -07004247 int ret = 0;
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004248 unsigned long flags;
4249
Subhash Jadavani9c807702017-04-01 00:35:51 -07004250 /*
4251 * Check if there is any race with fatal error handling.
4252 * If so, wait for it to complete. Even though fatal error
4253 * handling does reset and restore in some cases, don't assume
4254 * anything out of it. We are just avoiding race here.
4255 */
4256 do {
4257 spin_lock_irqsave(hba->host->host_lock, flags);
4258 if (!(work_pending(&hba->eh_work) ||
4259 hba->ufshcd_state == UFSHCD_STATE_RESET))
4260 break;
4261 spin_unlock_irqrestore(hba->host->host_lock, flags);
4262 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
4263 flush_work(&hba->eh_work);
4264 } while (1);
4265
4266
4267 /*
4268 * we don't know if previous reset had really reset the host controller
4269 * or not. So let's force reset here to be sure.
4270 */
4271 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4272 hba->force_host_reset = true;
4273 schedule_work(&hba->eh_work);
4274
4275 /* wait for the reset work to finish */
4276 do {
4277 if (!(work_pending(&hba->eh_work) ||
4278 hba->ufshcd_state == UFSHCD_STATE_RESET))
4279 break;
4280 spin_unlock_irqrestore(hba->host->host_lock, flags);
4281 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
4282 flush_work(&hba->eh_work);
4283 spin_lock_irqsave(hba->host->host_lock, flags);
4284 } while (1);
4285
4286 if (!((hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) &&
4287 ufshcd_is_link_active(hba)))
4288 ret = -ENOLINK;
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004289 spin_unlock_irqrestore(hba->host->host_lock, flags);
4290
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004291 return ret;
4292}
4293
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004294static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004295{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004296 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004297 struct uic_command uic_cmd = {0};
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004298 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004299
4300 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004301 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004302 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter",
4303 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004304
Subhash Jadavani9c807702017-04-01 00:35:51 -07004305 /*
4306 * Do full reinit if enter failed or if LINERESET was detected during
4307 * Hibern8 operation. After LINERESET, link moves to default PWM-G1
4308 * mode hence full reinit is required to move link to HS speeds.
4309 */
4310 if (ret || hba->full_init_linereset) {
4311 hba->full_init_linereset = false;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004312 ufshcd_update_error_stats(hba, UFS_ERR_HIBERN8_ENTER);
4313 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d",
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004314 __func__, ret);
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004315 /*
4316 * If link recovery fails then return error so that caller
4317 * don't retry the hibern8 enter again.
4318 */
Subhash Jadavani9c807702017-04-01 00:35:51 -07004319 ret = ufshcd_link_recovery(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004320 } else {
4321 dev_dbg(hba->dev, "%s: Hibern8 Enter at %lld us", __func__,
4322 ktime_to_us(ktime_get()));
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004323 }
4324
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004325 return ret;
4326}
4327
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004328int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004329{
4330 int ret = 0, retries;
4331
4332 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
4333 ret = __ufshcd_uic_hibern8_enter(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004334 if (!ret)
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004335 goto out;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004336 /* Unable to recover the link, so no point proceeding */
4337 if (ret == -ENOLINK)
4338 BUG();
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004339 }
4340out:
4341 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004342}
4343
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004344int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004345{
4346 struct uic_command uic_cmd = {0};
4347 int ret;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004348 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004349
4350 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
4351 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004352 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit",
4353 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
4354
Subhash Jadavani9c807702017-04-01 00:35:51 -07004355 /* Do full reinit if exit failed */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304356 if (ret) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004357 ufshcd_update_error_stats(hba, UFS_ERR_HIBERN8_EXIT);
4358 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d",
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004359 __func__, ret);
4360 ret = ufshcd_link_recovery(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004361 /* Unable to recover the link, so no point proceeding */
4362 if (ret)
4363 BUG();
4364 } else {
4365 dev_dbg(hba->dev, "%s: Hibern8 Exit at %lld us", __func__,
4366 ktime_to_us(ktime_get()));
4367 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_get();
4368 hba->ufs_stats.hibern8_exit_cnt++;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304369 }
4370
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304371 return ret;
4372}
4373
Yaniv Gardi50646362014-10-23 13:25:13 +03004374 /**
4375 * ufshcd_init_pwr_info - setting the POR (power on reset)
4376 * values in hba power info
4377 * @hba: per-adapter instance
4378 */
4379static void ufshcd_init_pwr_info(struct ufs_hba *hba)
4380{
4381 hba->pwr_info.gear_rx = UFS_PWM_G1;
4382 hba->pwr_info.gear_tx = UFS_PWM_G1;
4383 hba->pwr_info.lane_rx = 1;
4384 hba->pwr_info.lane_tx = 1;
4385 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
4386 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
4387 hba->pwr_info.hs_rate = 0;
4388}
4389
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304390/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004391 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
4392 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304393 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004394static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304395{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004396 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
4397
4398 if (hba->max_pwr_info.is_valid)
4399 return 0;
4400
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004401 pwr_info->pwr_tx = FAST_MODE;
4402 pwr_info->pwr_rx = FAST_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004403 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304404
4405 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004406 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
4407 &pwr_info->lane_rx);
4408 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4409 &pwr_info->lane_tx);
4410
4411 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
4412 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
4413 __func__,
4414 pwr_info->lane_rx,
4415 pwr_info->lane_tx);
4416 return -EINVAL;
4417 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304418
4419 /*
4420 * First, get the maximum gears of HS speed.
4421 * If a zero value, it means there is no HSGEAR capability.
4422 * Then, get the maximum gears of PWM speed.
4423 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004424 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
4425 if (!pwr_info->gear_rx) {
4426 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
4427 &pwr_info->gear_rx);
4428 if (!pwr_info->gear_rx) {
4429 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
4430 __func__, pwr_info->gear_rx);
4431 return -EINVAL;
Subhash Jadavani5e45e702016-08-09 18:43:10 -07004432 } else {
4433 if (hba->limit_rx_pwm_gear > 0 &&
4434 (hba->limit_rx_pwm_gear < pwr_info->gear_rx))
4435 pwr_info->gear_rx = hba->limit_rx_pwm_gear;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004436 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004437 pwr_info->pwr_rx = SLOW_MODE;
Subhash Jadavani5e45e702016-08-09 18:43:10 -07004438 } else {
4439 if (hba->limit_rx_hs_gear > 0 &&
4440 (hba->limit_rx_hs_gear < pwr_info->gear_rx))
4441 pwr_info->gear_rx = hba->limit_rx_hs_gear;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304442 }
4443
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004444 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
4445 &pwr_info->gear_tx);
4446 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304447 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004448 &pwr_info->gear_tx);
4449 if (!pwr_info->gear_tx) {
4450 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
4451 __func__, pwr_info->gear_tx);
4452 return -EINVAL;
Subhash Jadavani5e45e702016-08-09 18:43:10 -07004453 } else {
4454 if (hba->limit_tx_pwm_gear > 0 &&
4455 (hba->limit_tx_pwm_gear < pwr_info->gear_tx))
4456 pwr_info->gear_tx = hba->limit_tx_pwm_gear;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004457 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004458 pwr_info->pwr_tx = SLOW_MODE;
Subhash Jadavani5e45e702016-08-09 18:43:10 -07004459 } else {
4460 if (hba->limit_tx_hs_gear > 0 &&
4461 (hba->limit_tx_hs_gear < pwr_info->gear_tx))
4462 pwr_info->gear_tx = hba->limit_tx_hs_gear;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004463 }
4464
4465 hba->max_pwr_info.is_valid = true;
4466 return 0;
4467}
4468
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004469int ufshcd_change_power_mode(struct ufs_hba *hba,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004470 struct ufs_pa_layer_attr *pwr_mode)
4471{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004472 int ret = 0;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004473
4474 /* if already configured to the requested pwr_mode */
4475 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
4476 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
4477 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
4478 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
4479 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
4480 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
4481 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
4482 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
4483 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304484 }
4485
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004486 ufsdbg_error_inject_dispatcher(hba, ERR_INJECT_PWR_CHANGE, 0, &ret);
4487 if (ret)
4488 return ret;
4489
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304490 /*
4491 * Configure attributes for power mode change with below.
4492 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
4493 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
4494 * - PA_HSSERIES
4495 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004496 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
4497 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
4498 pwr_mode->lane_rx);
4499 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
4500 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304501 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004502 else
4503 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304504
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004505 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
4506 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
4507 pwr_mode->lane_tx);
4508 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
4509 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304510 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004511 else
4512 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304513
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004514 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
4515 pwr_mode->pwr_tx == FASTAUTO_MODE ||
4516 pwr_mode->pwr_rx == FAST_MODE ||
4517 pwr_mode->pwr_tx == FAST_MODE)
4518 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
4519 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304520
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004521 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0),
4522 DL_FC0ProtectionTimeOutVal_Default);
4523 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1),
4524 DL_TC0ReplayTimeOutVal_Default);
4525 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2),
4526 DL_AFC0ReqTimeOutVal_Default);
4527
4528 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalFC0ProtectionTimeOutVal),
4529 DL_FC0ProtectionTimeOutVal_Default);
4530 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalTC0ReplayTimeOutVal),
4531 DL_TC0ReplayTimeOutVal_Default);
4532 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalAFC0ReqTimeOutVal),
4533 DL_AFC0ReqTimeOutVal_Default);
4534
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004535 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
4536 | pwr_mode->pwr_tx);
4537
4538 if (ret) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004539 ufshcd_update_error_stats(hba, UFS_ERR_POWER_MODE_CHANGE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304540 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004541 "%s: power mode change failed %d\n", __func__, ret);
4542 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004543 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
4544 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004545
4546 memcpy(&hba->pwr_info, pwr_mode,
4547 sizeof(struct ufs_pa_layer_attr));
4548 }
4549
4550 return ret;
4551}
4552
4553/**
4554 * ufshcd_config_pwr_mode - configure a new power mode
4555 * @hba: per-adapter instance
4556 * @desired_pwr_mode: desired power configuration
4557 */
4558static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
4559 struct ufs_pa_layer_attr *desired_pwr_mode)
4560{
4561 struct ufs_pa_layer_attr final_params = { 0 };
4562 int ret;
4563
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004564 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
4565 desired_pwr_mode, &final_params);
4566
4567 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004568 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
4569
4570 ret = ufshcd_change_power_mode(hba, &final_params);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004571 if (!ret)
4572 ufshcd_print_pwr_info(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304573
4574 return ret;
4575}
4576
4577/**
Dolev Raviv68078d52013-07-30 00:35:58 +05304578 * ufshcd_complete_dev_init() - checks device readiness
4579 * hba: per-adapter instance
4580 *
4581 * Set fDeviceInit flag and poll until device toggles it.
4582 */
4583static int ufshcd_complete_dev_init(struct ufs_hba *hba)
4584{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004585 int i;
4586 int err;
Dolev Raviv68078d52013-07-30 00:35:58 +05304587 bool flag_res = 1;
4588
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004589 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
4590 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05304591 if (err) {
4592 dev_err(hba->dev,
4593 "%s setting fDeviceInit flag failed with error %d\n",
4594 __func__, err);
4595 goto out;
4596 }
4597
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004598 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
4599 for (i = 0; i < 1000 && !err && flag_res; i++)
4600 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
4601 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
4602
Dolev Raviv68078d52013-07-30 00:35:58 +05304603 if (err)
4604 dev_err(hba->dev,
4605 "%s reading fDeviceInit flag failed with error %d\n",
4606 __func__, err);
4607 else if (flag_res)
4608 dev_err(hba->dev,
4609 "%s fDeviceInit was not cleared by the device\n",
4610 __func__);
4611
4612out:
4613 return err;
4614}
4615
4616/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304617 * ufshcd_make_hba_operational - Make UFS controller operational
4618 * @hba: per adapter instance
4619 *
4620 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004621 * 1. Enable required interrupts
4622 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02004623 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004624 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304625 *
4626 * Returns 0 on success, non-zero value on failure
4627 */
4628static int ufshcd_make_hba_operational(struct ufs_hba *hba)
4629{
4630 int err = 0;
4631 u32 reg;
4632
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304633 /* Enable required interrupts */
4634 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
4635
4636 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03004637 if (ufshcd_is_intr_aggr_allowed(hba))
4638 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
4639 else
4640 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304641
4642 /* Configure UTRL and UTMRL base address registers */
4643 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
4644 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
4645 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
4646 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
4647 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
4648 REG_UTP_TASK_REQ_LIST_BASE_L);
4649 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
4650 REG_UTP_TASK_REQ_LIST_BASE_H);
4651
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304652 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02004653 * Make sure base address and interrupt setup are updated before
4654 * enabling the run/stop registers below.
4655 */
4656 wmb();
4657
4658 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304659 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304660 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004661 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304662 if (!(ufshcd_get_lists_status(reg))) {
4663 ufshcd_enable_run_stop_reg(hba);
4664 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304665 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304666 "Host controller not ready to process requests");
4667 err = -EIO;
4668 goto out;
4669 }
4670
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304671out:
4672 return err;
4673}
4674
4675/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02004676 * ufshcd_hba_stop - Send controller to reset state
4677 * @hba: per adapter instance
4678 * @can_sleep: perform sleep or just spin
4679 */
4680static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep)
4681{
4682 int err;
4683
4684 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
4685 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
4686 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
4687 10, 1, can_sleep);
4688 if (err)
4689 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
4690}
4691
4692/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304693 * ufshcd_hba_enable - initialize the controller
4694 * @hba: per adapter instance
4695 *
4696 * The controller resets itself and controller firmware initialization
4697 * sequence kicks off. When controller is ready it will set
4698 * the Host Controller Enable bit to 1.
4699 *
4700 * Returns 0 on success, non-zero value on failure
4701 */
4702static int ufshcd_hba_enable(struct ufs_hba *hba)
4703{
4704 int retry;
4705
4706 /*
4707 * msleep of 1 and 5 used in this function might result in msleep(20),
4708 * but it was necessary to send the UFS FPGA to reset mode during
4709 * development and testing of this driver. msleep can be changed to
4710 * mdelay and retry count can be reduced based on the controller.
4711 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02004712 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304713 /* change controller state to "reset state" */
Yaniv Gardi596585a2016-03-10 17:37:08 +02004714 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304715
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004716 /* UniPro link is disabled at this point */
4717 ufshcd_set_link_off(hba);
4718
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004719 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004720
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304721 /* start controller initialization sequence */
4722 ufshcd_hba_start(hba);
4723
4724 /*
4725 * To initialize a UFS host controller HCE bit must be set to 1.
4726 * During initialization the HCE bit value changes from 1->0->1.
4727 * When the host controller completes initialization sequence
4728 * it sets the value of HCE bit to 1. The same HCE bit is read back
4729 * to check if the controller has completed initialization sequence.
4730 * So without this delay the value HCE = 1, set in the previous
4731 * instruction might be read back.
4732 * This delay can be changed based on the controller.
4733 */
4734 msleep(1);
4735
4736 /* wait for the host controller to complete initialization */
4737 retry = 10;
4738 while (ufshcd_is_hba_active(hba)) {
4739 if (retry) {
4740 retry--;
4741 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304742 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304743 "Controller enable failed\n");
4744 return -EIO;
4745 }
4746 msleep(5);
4747 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004748
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004749 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004750 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004751
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004752 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004753
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304754 return 0;
4755}
4756
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004757static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
4758{
4759 int tx_lanes, i, err = 0;
4760
4761 if (!peer)
4762 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4763 &tx_lanes);
4764 else
4765 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4766 &tx_lanes);
4767 for (i = 0; i < tx_lanes; i++) {
4768 if (!peer)
4769 err = ufshcd_dme_set(hba,
4770 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4771 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4772 0);
4773 else
4774 err = ufshcd_dme_peer_set(hba,
4775 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4776 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4777 0);
4778 if (err) {
4779 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
4780 __func__, peer, i, err);
4781 break;
4782 }
4783 }
4784
4785 return err;
4786}
4787
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004788static inline int ufshcd_disable_host_tx_lcc(struct ufs_hba *hba)
4789{
4790 return ufshcd_disable_tx_lcc(hba, false);
4791}
4792
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004793static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
4794{
4795 return ufshcd_disable_tx_lcc(hba, true);
4796}
4797
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304798/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304799 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304800 * @hba: per adapter instance
4801 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304802 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304803 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304804static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304805{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304806 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004807 int retries = DME_LINKSTARTUP_RETRIES;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004808 bool link_startup_again = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304809
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004810 /*
4811 * If UFS device isn't active then we will have to issue link startup
4812 * 2 times to make sure the device state move to active.
4813 */
4814 if (!ufshcd_is_ufs_dev_active(hba))
4815 link_startup_again = true;
4816
4817link_startup:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004818 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004819 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304820
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004821 ret = ufshcd_dme_link_startup(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004822 if (ret)
4823 ufshcd_update_error_stats(hba, UFS_ERR_LINKSTARTUP);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004824
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004825 /* check if device is detected by inter-connect layer */
4826 if (!ret && !ufshcd_is_device_present(hba)) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004827 ufshcd_update_error_stats(hba, UFS_ERR_LINKSTARTUP);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004828 dev_err(hba->dev, "%s: Device not present\n", __func__);
4829 ret = -ENXIO;
4830 goto out;
4831 }
4832
4833 /*
4834 * DME link lost indication is only received when link is up,
4835 * but we can't be sure if the link is up until link startup
4836 * succeeds. So reset the local Uni-Pro and try again.
4837 */
4838 if (ret && ufshcd_hba_enable(hba))
4839 goto out;
4840 } while (ret && retries--);
4841
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304842 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004843 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304844 goto out;
4845
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004846 if (link_startup_again) {
4847 link_startup_again = false;
4848 retries = DME_LINKSTARTUP_RETRIES;
4849 goto link_startup;
4850 }
4851
4852 /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */
4853 ufshcd_init_pwr_info(hba);
4854 ufshcd_print_pwr_info(hba);
4855
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004856 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
4857 ret = ufshcd_disable_device_tx_lcc(hba);
4858 if (ret)
4859 goto out;
4860 }
4861
Subhash Jadavani4f0df17b2016-12-16 13:19:27 -08004862 if (hba->dev_info.quirks & UFS_DEVICE_QUIRK_BROKEN_LCC) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004863 ret = ufshcd_disable_host_tx_lcc(hba);
4864 if (ret)
4865 goto out;
4866 }
4867
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004868 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004869 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
4870 if (ret)
4871 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004872
4873 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304874out:
Subhash Jadavani9c807702017-04-01 00:35:51 -07004875 if (ret) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304876 dev_err(hba->dev, "link startup failed %d\n", ret);
Subhash Jadavani9c807702017-04-01 00:35:51 -07004877 ufshcd_print_host_state(hba);
4878 ufshcd_print_pwr_info(hba);
4879 ufshcd_print_host_regs(hba);
4880 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304881 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304882}
4883
4884/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304885 * ufshcd_verify_dev_init() - Verify device initialization
4886 * @hba: per-adapter instance
4887 *
4888 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
4889 * device Transport Protocol (UTP) layer is ready after a reset.
4890 * If the UTP layer at the device side is not initialized, it may
4891 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
4892 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
4893 */
4894static int ufshcd_verify_dev_init(struct ufs_hba *hba)
4895{
4896 int err = 0;
4897 int retries;
4898
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004899 ufshcd_hold_all(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304900 mutex_lock(&hba->dev_cmd.lock);
4901 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
4902 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
4903 NOP_OUT_TIMEOUT);
4904
4905 if (!err || err == -ETIMEDOUT)
4906 break;
4907
4908 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
4909 }
4910 mutex_unlock(&hba->dev_cmd.lock);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004911 ufshcd_release_all(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304912
4913 if (err)
4914 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
4915 return err;
4916}
4917
4918/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004919 * ufshcd_set_queue_depth - set lun queue depth
4920 * @sdev: pointer to SCSI device
4921 *
4922 * Read bLUQueueDepth value and activate scsi tagged command
4923 * queueing. For WLUN, queue depth is set to 1. For best-effort
4924 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
4925 * value that host can queue.
4926 */
4927static void ufshcd_set_queue_depth(struct scsi_device *sdev)
4928{
4929 int ret = 0;
4930 u8 lun_qdepth;
4931 struct ufs_hba *hba;
4932
4933 hba = shost_priv(sdev->host);
4934
4935 lun_qdepth = hba->nutrs;
4936 ret = ufshcd_read_unit_desc_param(hba,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004937 ufshcd_scsi_to_upiu_lun(sdev->lun),
4938 UNIT_DESC_PARAM_LU_Q_DEPTH,
4939 &lun_qdepth,
4940 sizeof(lun_qdepth));
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004941
4942 /* Some WLUN doesn't support unit descriptor */
4943 if (ret == -EOPNOTSUPP)
4944 lun_qdepth = 1;
4945 else if (!lun_qdepth)
4946 /* eventually, we can figure out the real queue depth */
4947 lun_qdepth = hba->nutrs;
4948 else
4949 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
4950
4951 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
4952 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004953 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004954}
4955
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004956/*
4957 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
4958 * @hba: per-adapter instance
4959 * @lun: UFS device lun id
4960 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
4961 *
4962 * Returns 0 in case of success and b_lu_write_protect status would be returned
4963 * @b_lu_write_protect parameter.
4964 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
4965 * Returns -EINVAL in case of invalid parameters passed to this function.
4966 */
4967static int ufshcd_get_lu_wp(struct ufs_hba *hba,
4968 u8 lun,
4969 u8 *b_lu_write_protect)
4970{
4971 int ret;
4972
4973 if (!b_lu_write_protect)
4974 ret = -EINVAL;
4975 /*
4976 * According to UFS device spec, RPMB LU can't be write
4977 * protected so skip reading bLUWriteProtect parameter for
4978 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
4979 */
4980 else if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
4981 ret = -ENOTSUPP;
4982 else
4983 ret = ufshcd_read_unit_desc_param(hba,
4984 lun,
4985 UNIT_DESC_PARAM_LU_WR_PROTECT,
4986 b_lu_write_protect,
4987 sizeof(*b_lu_write_protect));
4988 return ret;
4989}
4990
4991/**
4992 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
4993 * status
4994 * @hba: per-adapter instance
4995 * @sdev: pointer to SCSI device
4996 *
4997 */
4998static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
4999 struct scsi_device *sdev)
5000{
5001 if (hba->dev_info.f_power_on_wp_en &&
5002 !hba->dev_info.is_lu_power_on_wp) {
5003 u8 b_lu_write_protect;
5004
5005 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
5006 &b_lu_write_protect) &&
5007 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
5008 hba->dev_info.is_lu_power_on_wp = true;
5009 }
5010}
5011
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03005012/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305013 * ufshcd_slave_alloc - handle initial SCSI device configurations
5014 * @sdev: pointer to SCSI device
5015 *
5016 * Returns success
5017 */
5018static int ufshcd_slave_alloc(struct scsi_device *sdev)
5019{
5020 struct ufs_hba *hba;
5021
5022 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305023
5024 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
5025 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305026
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305027 /* allow SCSI layer to restart the device in case of errors */
5028 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03005029
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03005030 /* REPORT SUPPORTED OPERATION CODES is not supported */
5031 sdev->no_report_opcodes = 1;
5032
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005033 /* WRITE_SAME command is not supported*/
5034 sdev->no_write_same = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03005035
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03005036 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03005037
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005038 ufshcd_get_lu_power_on_wp_status(hba, sdev);
5039
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03005040 return 0;
5041}
5042
5043/**
5044 * ufshcd_change_queue_depth - change queue depth
5045 * @sdev: pointer to SCSI device
5046 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03005047 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01005048 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03005049 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01005050static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03005051{
5052 struct ufs_hba *hba = shost_priv(sdev->host);
5053
5054 if (depth > hba->nutrs)
5055 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01005056 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305057}
5058
5059/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09005060 * ufshcd_slave_configure - adjust SCSI device configurations
5061 * @sdev: pointer to SCSI device
5062 */
5063static int ufshcd_slave_configure(struct scsi_device *sdev)
5064{
5065 struct request_queue *q = sdev->request_queue;
Subhash Jadavani5ea586f2016-08-17 19:08:09 -07005066 struct ufs_hba *hba = shost_priv(sdev->host);
Akinobu Mitaeeda4742014-07-01 23:00:32 +09005067
5068 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
5069 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
5070
Subhash Jadavani5ea586f2016-08-17 19:08:09 -07005071 if (hba->scsi_cmd_timeout) {
5072 blk_queue_rq_timeout(q, hba->scsi_cmd_timeout * HZ);
5073 scsi_set_cmd_timeout_override(sdev, hba->scsi_cmd_timeout * HZ);
5074 }
5075
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005076 sdev->autosuspend_delay = UFSHCD_AUTO_SUSPEND_DELAY_MS;
5077 sdev->use_rpm_auto = 1;
5078
Akinobu Mitaeeda4742014-07-01 23:00:32 +09005079 return 0;
5080}
5081
5082/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305083 * ufshcd_slave_destroy - remove SCSI device configurations
5084 * @sdev: pointer to SCSI device
5085 */
5086static void ufshcd_slave_destroy(struct scsi_device *sdev)
5087{
5088 struct ufs_hba *hba;
5089
5090 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03005091 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005092 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
5093 unsigned long flags;
5094
5095 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03005096 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005097 spin_unlock_irqrestore(hba->host->host_lock, flags);
5098 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305099}
5100
5101/**
5102 * ufshcd_task_req_compl - handle task management request completion
5103 * @hba: per adapter instance
5104 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305105 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305106 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305107 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305108 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305109static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305110{
5111 struct utp_task_req_desc *task_req_descp;
5112 struct utp_upiu_task_rsp *task_rsp_upiup;
5113 unsigned long flags;
5114 int ocs_value;
5115 int task_result;
5116
5117 spin_lock_irqsave(hba->host->host_lock, flags);
5118
5119 /* Clear completed tasks from outstanding_tasks */
5120 __clear_bit(index, &hba->outstanding_tasks);
5121
5122 task_req_descp = hba->utmrdl_base_addr;
5123 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
5124
5125 if (ocs_value == OCS_SUCCESS) {
5126 task_rsp_upiup = (struct utp_upiu_task_rsp *)
5127 task_req_descp[index].task_rsp_upiu;
Kiwoong Kim8794ee02016-09-09 08:22:22 +09005128 task_result = be32_to_cpu(task_rsp_upiup->output_param1);
5129 task_result = task_result & MASK_TM_SERVICE_RESP;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305130 if (resp)
5131 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305132 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305133 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
5134 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305135 }
5136 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305137
5138 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305139}
5140
5141/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305142 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
5143 * @lrb: pointer to local reference block of completed command
5144 * @scsi_status: SCSI command status
5145 *
5146 * Returns value base on SCSI command status
5147 */
5148static inline int
5149ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
5150{
5151 int result = 0;
5152
5153 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05305154 case SAM_STAT_CHECK_CONDITION:
5155 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305156 case SAM_STAT_GOOD:
5157 result |= DID_OK << 16 |
5158 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05305159 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305160 break;
5161 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05305162 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305163 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05305164 ufshcd_copy_sense_data(lrbp);
5165 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305166 break;
5167 default:
5168 result |= DID_ERROR << 16;
5169 break;
5170 } /* end of switch */
5171
5172 return result;
5173}
5174
5175/**
5176 * ufshcd_transfer_rsp_status - Get overall status of the response
5177 * @hba: per adapter instance
5178 * @lrb: pointer to local reference block of completed command
5179 *
5180 * Returns result of the command to notify SCSI midlayer
5181 */
5182static inline int
5183ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
5184{
5185 int result = 0;
5186 int scsi_status;
5187 int ocs;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005188 bool print_prdt;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305189
5190 /* overall command status of utrd */
5191 ocs = ufshcd_get_tr_ocs(lrbp);
5192
5193 switch (ocs) {
5194 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305195 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005196 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305197 switch (result) {
5198 case UPIU_TRANSACTION_RESPONSE:
5199 /*
5200 * get the response UPIU result to extract
5201 * the SCSI command status
5202 */
5203 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
5204
5205 /*
5206 * get the result based on SCSI status response
5207 * to notify the SCSI midlayer of the command status
5208 */
5209 scsi_status = result & MASK_SCSI_STATUS;
5210 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305211
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02005212 /*
5213 * Currently we are only supporting BKOPs exception
5214 * events hence we can ignore BKOPs exception event
5215 * during power management callbacks. BKOPs exception
5216 * event is not expected to be raised in runtime suspend
5217 * callback as it allows the urgent bkops.
5218 * During system suspend, we are anyway forcefully
5219 * disabling the bkops and if urgent bkops is needed
5220 * it will be enabled on system resume. Long term
5221 * solution could be to abort the system suspend if
5222 * UFS device needs urgent BKOPs.
5223 */
5224 if (!hba->pm_op_in_progress &&
5225 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305226 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305227 break;
5228 case UPIU_TRANSACTION_REJECT_UPIU:
5229 /* TODO: handle Reject UPIU Response */
5230 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305231 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305232 "Reject UPIU not fully implemented\n");
5233 break;
5234 default:
5235 result = DID_ERROR << 16;
5236 dev_err(hba->dev,
5237 "Unexpected request response code = %x\n",
5238 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305239 break;
5240 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305241 break;
5242 case OCS_ABORTED:
5243 result |= DID_ABORT << 16;
5244 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305245 case OCS_INVALID_COMMAND_STATUS:
5246 result |= DID_REQUEUE << 16;
5247 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305248 case OCS_INVALID_CMD_TABLE_ATTR:
5249 case OCS_INVALID_PRDT_ATTR:
5250 case OCS_MISMATCH_DATA_BUF_SIZE:
5251 case OCS_MISMATCH_RESP_UPIU_SIZE:
5252 case OCS_PEER_COMM_FAILURE:
5253 case OCS_FATAL_ERROR:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005254 case OCS_DEVICE_FATAL_ERROR:
5255 case OCS_INVALID_CRYPTO_CONFIG:
5256 case OCS_GENERAL_CRYPTO_ERROR:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305257 default:
5258 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305259 dev_err(hba->dev,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005260 "OCS error from controller = %x for tag %d\n",
5261 ocs, lrbp->task_tag);
Subhash Jadavani9c807702017-04-01 00:35:51 -07005262 /*
5263 * This is called in interrupt context, hence avoid sleep
5264 * while printing debug registers. Also print only the minimum
5265 * debug registers needed to debug OCS failure.
5266 */
5267 __ufshcd_print_host_regs(hba, true);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005268 ufshcd_print_host_state(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305269 break;
5270 } /* end of switch */
5271
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005272 if ((host_byte(result) != DID_OK) && !hba->silence_err_logs) {
5273 print_prdt = (ocs == OCS_INVALID_PRDT_ATTR ||
5274 ocs == OCS_MISMATCH_DATA_BUF_SIZE);
5275 ufshcd_print_trs(hba, 1 << lrbp->task_tag, print_prdt);
5276 }
5277
5278 if ((host_byte(result) == DID_ERROR) ||
5279 (host_byte(result) == DID_ABORT))
5280 ufsdbg_set_err_state(hba);
5281
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305282 return result;
5283}
5284
5285/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305286 * ufshcd_uic_cmd_compl - handle completion of uic command
5287 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305288 * @intr_status: interrupt status generated by the controller
Subhash Jadavani9c807702017-04-01 00:35:51 -07005289 *
5290 * Returns
5291 * IRQ_HANDLED - If interrupt is valid
5292 * IRQ_NONE - If invalid interrupt
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305293 */
Subhash Jadavani9c807702017-04-01 00:35:51 -07005294static irqreturn_t ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305295{
Subhash Jadavani9c807702017-04-01 00:35:51 -07005296 irqreturn_t retval = IRQ_NONE;
5297
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305298 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305299 hba->active_uic_cmd->argument2 |=
5300 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05305301 hba->active_uic_cmd->argument3 =
5302 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305303 complete(&hba->active_uic_cmd->done);
Subhash Jadavani9c807702017-04-01 00:35:51 -07005304 retval = IRQ_HANDLED;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305305 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305306
Subhash Jadavani9c807702017-04-01 00:35:51 -07005307 if (intr_status & UFSHCD_UIC_PWR_MASK) {
5308 if (hba->uic_async_done) {
5309 complete(hba->uic_async_done);
5310 retval = IRQ_HANDLED;
5311 } else if (ufshcd_is_auto_hibern8_supported(hba)) {
5312 /*
5313 * If uic_async_done flag is not set then this
5314 * is an Auto hibern8 err interrupt.
5315 * Perform a host reset followed by a full
5316 * link recovery.
5317 */
5318 hba->ufshcd_state = UFSHCD_STATE_ERROR;
5319 hba->force_host_reset = true;
5320 dev_err(hba->dev, "%s: Auto Hibern8 %s failed - status: 0x%08x, upmcrs: 0x%08x\n",
5321 __func__, (intr_status & UIC_HIBERNATE_ENTER) ?
5322 "Enter" : "Exit",
5323 intr_status, ufshcd_get_upmcrs(hba));
5324 __ufshcd_print_host_regs(hba, true);
5325 ufshcd_print_host_state(hba);
5326 schedule_work(&hba->eh_work);
5327 retval = IRQ_HANDLED;
5328 }
5329 }
5330 return retval;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305331}
5332
5333/**
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005334 * ufshcd_abort_outstanding_requests - abort all outstanding transfer requests.
5335 * @hba: per adapter instance
5336 * @result: error result to inform scsi layer about
5337 */
5338void ufshcd_abort_outstanding_transfer_requests(struct ufs_hba *hba, int result)
5339{
5340 u8 index;
5341 struct ufshcd_lrb *lrbp;
5342 struct scsi_cmnd *cmd;
5343
5344 if (!hba->outstanding_reqs)
5345 return;
5346
5347 for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) {
5348 lrbp = &hba->lrb[index];
5349 cmd = lrbp->cmd;
5350 if (cmd) {
5351 ufshcd_cond_add_cmd_trace(hba, index, "failed");
5352 ufshcd_update_error_stats(hba,
5353 UFS_ERR_INT_FATAL_ERRORS);
5354 scsi_dma_unmap(cmd);
5355 cmd->result = result;
5356 /* Clear pending transfer requests */
5357 ufshcd_clear_cmd(hba, index);
5358 ufshcd_outstanding_req_clear(hba, index);
5359 clear_bit_unlock(index, &hba->lrb_in_use);
5360 lrbp->complete_time_stamp = ktime_get();
5361 update_req_stats(hba, lrbp);
5362 /* Mark completed command as NULL in LRB */
5363 lrbp->cmd = NULL;
5364 ufshcd_release_all(hba);
5365 if (cmd->request) {
5366 /*
5367 * As we are accessing the "request" structure,
5368 * this must be called before calling
5369 * ->scsi_done() callback.
5370 */
5371 ufshcd_vops_pm_qos_req_end(hba, cmd->request,
5372 true);
5373 ufshcd_vops_crypto_engine_cfg_end(hba,
5374 lrbp, cmd->request);
5375 }
5376 /* Do not touch lrbp after scsi done */
5377 cmd->scsi_done(cmd);
5378 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE) {
5379 if (hba->dev_cmd.complete) {
5380 ufshcd_cond_add_cmd_trace(hba, index,
5381 "dev_failed");
5382 ufshcd_outstanding_req_clear(hba, index);
5383 complete(hba->dev_cmd.complete);
5384 }
5385 }
5386 if (ufshcd_is_clkscaling_supported(hba))
5387 hba->clk_scaling.active_reqs--;
5388 }
5389}
5390
5391/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005392 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305393 * @hba: per adapter instance
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005394 * @completed_reqs: requests to complete
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305395 */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005396static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
5397 unsigned long completed_reqs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305398{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305399 struct ufshcd_lrb *lrbp;
5400 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305401 int result;
5402 int index;
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -07005403 struct request *req;
Dolev Ravive9d501b2014-07-01 12:22:37 +03005404
Dolev Ravive9d501b2014-07-01 12:22:37 +03005405 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
5406 lrbp = &hba->lrb[index];
5407 cmd = lrbp->cmd;
5408 if (cmd) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005409 ufshcd_cond_add_cmd_trace(hba, index, "complete");
5410 ufshcd_update_tag_stats_completion(hba, cmd);
Dolev Ravive9d501b2014-07-01 12:22:37 +03005411 result = ufshcd_transfer_rsp_status(hba, lrbp);
5412 scsi_dma_unmap(cmd);
5413 cmd->result = result;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005414 clear_bit_unlock(index, &hba->lrb_in_use);
5415 lrbp->complete_time_stamp = ktime_get();
5416 update_req_stats(hba, lrbp);
Dolev Ravive9d501b2014-07-01 12:22:37 +03005417 /* Mark completed command as NULL in LRB */
5418 lrbp->cmd = NULL;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005419 __ufshcd_release(hba, false);
5420 __ufshcd_hibern8_release(hba, false);
5421 if (cmd->request) {
5422 /*
5423 * As we are accessing the "request" structure,
5424 * this must be called before calling
5425 * ->scsi_done() callback.
5426 */
5427 ufshcd_vops_pm_qos_req_end(hba, cmd->request,
5428 false);
5429 ufshcd_vops_crypto_engine_cfg_end(hba,
5430 lrbp, cmd->request);
5431 }
5432
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -07005433 req = cmd->request;
5434 if (req) {
5435 /* Update IO svc time latency histogram */
5436 if (req->lat_hist_enabled) {
5437 ktime_t completion;
5438 u_int64_t delta_us;
5439
5440 completion = ktime_get();
5441 delta_us = ktime_us_delta(completion,
5442 req->lat_hist_io_start);
5443 /* rq_data_dir() => true if WRITE */
5444 blk_update_latency_hist(&hba->io_lat_s,
5445 (rq_data_dir(req) == READ),
5446 delta_us);
5447 }
5448 }
Dolev Ravive9d501b2014-07-01 12:22:37 +03005449 /* Do not touch lrbp after scsi done */
5450 cmd->scsi_done(cmd);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005451 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE) {
5452 if (hba->dev_cmd.complete) {
5453 ufshcd_cond_add_cmd_trace(hba, index,
5454 "dev_complete");
Dolev Ravive9d501b2014-07-01 12:22:37 +03005455 complete(hba->dev_cmd.complete);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005456 }
Dolev Ravive9d501b2014-07-01 12:22:37 +03005457 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005458 if (ufshcd_is_clkscaling_supported(hba))
5459 hba->clk_scaling.active_reqs--;
Dolev Ravive9d501b2014-07-01 12:22:37 +03005460 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305461
5462 /* clear corresponding bits of completed commands */
5463 hba->outstanding_reqs ^= completed_reqs;
5464
Sahitya Tummala856b3482014-09-25 15:32:34 +03005465 ufshcd_clk_scaling_update_busy(hba);
5466
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305467 /* we might have free'd some tags above */
5468 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305469}
5470
5471/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005472 * ufshcd_transfer_req_compl - handle SCSI and query command completion
5473 * @hba: per adapter instance
Subhash Jadavani9c807702017-04-01 00:35:51 -07005474 *
5475 * Returns
5476 * IRQ_HANDLED - If interrupt is valid
5477 * IRQ_NONE - If invalid interrupt
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005478 */
Subhash Jadavani9c807702017-04-01 00:35:51 -07005479static irqreturn_t ufshcd_transfer_req_compl(struct ufs_hba *hba)
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005480{
5481 unsigned long completed_reqs;
5482 u32 tr_doorbell;
5483
5484 /* Resetting interrupt aggregation counters first and reading the
5485 * DOOR_BELL afterward allows us to handle all the completed requests.
5486 * In order to prevent other interrupts starvation the DB is read once
5487 * after reset. The down side of this solution is the possibility of
5488 * false interrupt if device completes another request after resetting
5489 * aggregation and before reading the DB.
5490 */
5491 if (ufshcd_is_intr_aggr_allowed(hba))
5492 ufshcd_reset_intr_aggr(hba);
5493
5494 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
5495 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
5496
Subhash Jadavani9c807702017-04-01 00:35:51 -07005497 if (completed_reqs) {
5498 __ufshcd_transfer_req_compl(hba, completed_reqs);
5499 return IRQ_HANDLED;
5500 } else {
5501 return IRQ_NONE;
5502 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005503}
5504
5505/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305506 * ufshcd_disable_ee - disable exception event
5507 * @hba: per-adapter instance
5508 * @mask: exception event to disable
5509 *
5510 * Disables exception event in the device so that the EVENT_ALERT
5511 * bit is not set.
5512 *
5513 * Returns zero on success, non-zero error value on failure.
5514 */
5515static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
5516{
5517 int err = 0;
5518 u32 val;
5519
5520 if (!(hba->ee_ctrl_mask & mask))
5521 goto out;
5522
5523 val = hba->ee_ctrl_mask & ~mask;
5524 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005525 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305526 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
5527 if (!err)
5528 hba->ee_ctrl_mask &= ~mask;
5529out:
5530 return err;
5531}
5532
5533/**
5534 * ufshcd_enable_ee - enable exception event
5535 * @hba: per-adapter instance
5536 * @mask: exception event to enable
5537 *
5538 * Enable corresponding exception event in the device to allow
5539 * device to alert host in critical scenarios.
5540 *
5541 * Returns zero on success, non-zero error value on failure.
5542 */
5543static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
5544{
5545 int err = 0;
5546 u32 val;
5547
5548 if (hba->ee_ctrl_mask & mask)
5549 goto out;
5550
5551 val = hba->ee_ctrl_mask | mask;
5552 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005553 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305554 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
5555 if (!err)
5556 hba->ee_ctrl_mask |= mask;
5557out:
5558 return err;
5559}
5560
5561/**
5562 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
5563 * @hba: per-adapter instance
5564 *
5565 * Allow device to manage background operations on its own. Enabling
5566 * this might lead to inconsistent latencies during normal data transfers
5567 * as the device is allowed to manage its own way of handling background
5568 * operations.
5569 *
5570 * Returns zero on success, non-zero on failure.
5571 */
5572static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
5573{
5574 int err = 0;
5575
5576 if (hba->auto_bkops_enabled)
5577 goto out;
5578
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005579 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305580 QUERY_FLAG_IDN_BKOPS_EN, NULL);
5581 if (err) {
5582 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
5583 __func__, err);
5584 goto out;
5585 }
5586
5587 hba->auto_bkops_enabled = true;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005588 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), 1);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305589
5590 /* No need of URGENT_BKOPS exception from the device */
5591 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
5592 if (err)
5593 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
5594 __func__, err);
5595out:
5596 return err;
5597}
5598
5599/**
5600 * ufshcd_disable_auto_bkops - block device in doing background operations
5601 * @hba: per-adapter instance
5602 *
5603 * Disabling background operations improves command response latency but
5604 * has drawback of device moving into critical state where the device is
5605 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
5606 * host is idle so that BKOPS are managed effectively without any negative
5607 * impacts.
5608 *
5609 * Returns zero on success, non-zero on failure.
5610 */
5611static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
5612{
5613 int err = 0;
5614
5615 if (!hba->auto_bkops_enabled)
5616 goto out;
5617
5618 /*
5619 * If host assisted BKOPs is to be enabled, make sure
5620 * urgent bkops exception is allowed.
5621 */
5622 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
5623 if (err) {
5624 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
5625 __func__, err);
5626 goto out;
5627 }
5628
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005629 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305630 QUERY_FLAG_IDN_BKOPS_EN, NULL);
5631 if (err) {
5632 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
5633 __func__, err);
5634 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
5635 goto out;
5636 }
5637
5638 hba->auto_bkops_enabled = false;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005639 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), 0);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305640out:
5641 return err;
5642}
5643
5644/**
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005645 * ufshcd_force_reset_auto_bkops - force reset auto bkops state
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305646 * @hba: per adapter instance
5647 *
5648 * After a device reset the device may toggle the BKOPS_EN flag
5649 * to default value. The s/w tracking variables should be updated
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005650 * as well. This function would change the auto-bkops state based on
5651 * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305652 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005653static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305654{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005655 if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) {
5656 hba->auto_bkops_enabled = false;
5657 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
5658 ufshcd_enable_auto_bkops(hba);
5659 } else {
5660 hba->auto_bkops_enabled = true;
5661 hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS;
5662 ufshcd_disable_auto_bkops(hba);
5663 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305664}
5665
5666static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
5667{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005668 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305669 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
5670}
5671
5672/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005673 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
5674 * @hba: per-adapter instance
5675 * @status: bkops_status value
5676 *
5677 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
5678 * flag in the device to permit background operations if the device
5679 * bkops_status is greater than or equal to "status" argument passed to
5680 * this function, disable otherwise.
5681 *
5682 * Returns 0 for success, non-zero in case of failure.
5683 *
5684 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
5685 * to know whether auto bkops is enabled or disabled after this function
5686 * returns control to it.
5687 */
5688static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
5689 enum bkops_status status)
5690{
5691 int err;
5692 u32 curr_status = 0;
5693
5694 err = ufshcd_get_bkops_status(hba, &curr_status);
5695 if (err) {
5696 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
5697 __func__, err);
5698 goto out;
5699 } else if (curr_status > BKOPS_STATUS_MAX) {
5700 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
5701 __func__, curr_status);
5702 err = -EINVAL;
5703 goto out;
5704 }
5705
5706 if (curr_status >= status)
5707 err = ufshcd_enable_auto_bkops(hba);
5708 else
5709 err = ufshcd_disable_auto_bkops(hba);
5710out:
5711 return err;
5712}
5713
5714/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305715 * ufshcd_urgent_bkops - handle urgent bkops exception event
5716 * @hba: per-adapter instance
5717 *
5718 * Enable fBackgroundOpsEn flag in the device to permit background
5719 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005720 *
5721 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
5722 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305723 */
5724static int ufshcd_urgent_bkops(struct ufs_hba *hba)
5725{
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005726 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305727}
5728
5729static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
5730{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005731 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305732 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
5733}
5734
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005735static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
5736{
5737 int err;
5738 u32 curr_status = 0;
5739
5740 if (hba->is_urgent_bkops_lvl_checked)
5741 goto enable_auto_bkops;
5742
5743 err = ufshcd_get_bkops_status(hba, &curr_status);
5744 if (err) {
5745 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
5746 __func__, err);
5747 goto out;
5748 }
5749
5750 /*
5751 * We are seeing that some devices are raising the urgent bkops
5752 * exception events even when BKOPS status doesn't indicate performace
5753 * impacted or critical. Handle these device by determining their urgent
5754 * bkops status at runtime.
5755 */
5756 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
5757 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
5758 __func__, curr_status);
5759 /* update the current status as the urgent bkops level */
5760 hba->urgent_bkops_lvl = curr_status;
5761 hba->is_urgent_bkops_lvl_checked = true;
5762 }
5763
5764enable_auto_bkops:
5765 err = ufshcd_enable_auto_bkops(hba);
5766out:
5767 if (err < 0)
5768 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
5769 __func__, err);
5770}
5771
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305772/**
5773 * ufshcd_exception_event_handler - handle exceptions raised by device
5774 * @work: pointer to work data
5775 *
5776 * Read bExceptionEventStatus attribute from the device and handle the
5777 * exception event accordingly.
5778 */
5779static void ufshcd_exception_event_handler(struct work_struct *work)
5780{
5781 struct ufs_hba *hba;
5782 int err;
5783 u32 status = 0;
5784 hba = container_of(work, struct ufs_hba, eeh_work);
5785
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305786 pm_runtime_get_sync(hba->dev);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005787 ufshcd_scsi_block_requests(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305788 err = ufshcd_get_ee_status(hba, &status);
5789 if (err) {
5790 dev_err(hba->dev, "%s: failed to get exception status %d\n",
5791 __func__, err);
5792 goto out;
5793 }
5794
5795 status &= hba->ee_ctrl_mask;
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005796
5797 if (status & MASK_EE_URGENT_BKOPS)
5798 ufshcd_bkops_exception_event_handler(hba);
5799
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305800out:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005801 ufshcd_scsi_unblock_requests(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305802 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305803 return;
5804}
5805
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005806/* Complete requests that have door-bell cleared */
5807static void ufshcd_complete_requests(struct ufs_hba *hba)
5808{
5809 ufshcd_transfer_req_compl(hba);
5810 ufshcd_tmc_handler(hba);
5811}
5812
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305813/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02005814 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
5815 * to recover from the DL NAC errors or not.
5816 * @hba: per-adapter instance
5817 *
5818 * Returns true if error handling is required, false otherwise
5819 */
5820static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
5821{
5822 unsigned long flags;
5823 bool err_handling = true;
5824
5825 spin_lock_irqsave(hba->host->host_lock, flags);
5826 /*
5827 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
5828 * device fatal error and/or DL NAC & REPLAY timeout errors.
5829 */
5830 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
5831 goto out;
5832
5833 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
5834 ((hba->saved_err & UIC_ERROR) &&
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005835 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR))) {
5836 /*
5837 * we have to do error recovery but atleast silence the error
5838 * logs.
5839 */
5840 hba->silence_err_logs = true;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005841 goto out;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005842 }
Yaniv Gardi583fa622016-03-10 17:37:13 +02005843
5844 if ((hba->saved_err & UIC_ERROR) &&
5845 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
5846 int err;
5847 /*
5848 * wait for 50ms to see if we can get any other errors or not.
5849 */
5850 spin_unlock_irqrestore(hba->host->host_lock, flags);
5851 msleep(50);
5852 spin_lock_irqsave(hba->host->host_lock, flags);
5853
5854 /*
5855 * now check if we have got any other severe errors other than
5856 * DL NAC error?
5857 */
5858 if ((hba->saved_err & INT_FATAL_ERRORS) ||
5859 ((hba->saved_err & UIC_ERROR) &&
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005860 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR))) {
5861 if (((hba->saved_err & INT_FATAL_ERRORS) ==
5862 DEVICE_FATAL_ERROR) || (hba->saved_uic_err &
5863 ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR))
5864 hba->silence_err_logs = true;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005865 goto out;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005866 }
Yaniv Gardi583fa622016-03-10 17:37:13 +02005867
5868 /*
5869 * As DL NAC is the only error received so far, send out NOP
5870 * command to confirm if link is still active or not.
5871 * - If we don't get any response then do error recovery.
5872 * - If we get response then clear the DL NAC error bit.
5873 */
5874
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005875 /* silence the error logs from NOP command */
5876 hba->silence_err_logs = true;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005877 spin_unlock_irqrestore(hba->host->host_lock, flags);
5878 err = ufshcd_verify_dev_init(hba);
5879 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005880 hba->silence_err_logs = false;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005881
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005882 if (err) {
5883 hba->silence_err_logs = true;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005884 goto out;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005885 }
Yaniv Gardi583fa622016-03-10 17:37:13 +02005886
5887 /* Link seems to be alive hence ignore the DL NAC errors */
5888 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
5889 hba->saved_err &= ~UIC_ERROR;
5890 /* clear NAC error */
5891 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
5892 if (!hba->saved_uic_err) {
5893 err_handling = false;
5894 goto out;
5895 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005896 /*
5897 * there seems to be some errors other than NAC, so do error
5898 * recovery
5899 */
5900 hba->silence_err_logs = true;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005901 }
5902out:
5903 spin_unlock_irqrestore(hba->host->host_lock, flags);
5904 return err_handling;
5905}
5906
5907/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305908 * ufshcd_err_handler - handle UFS errors that require s/w attention
5909 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305910 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305911static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305912{
5913 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305914 unsigned long flags;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005915 bool err_xfer = false, err_tm = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305916 int err = 0;
5917 int tag;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005918 bool needs_reset = false;
Subhash Jadavani9c807702017-04-01 00:35:51 -07005919 bool clks_enabled = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305920
5921 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305922
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305923 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani9c807702017-04-01 00:35:51 -07005924 ufsdbg_set_err_state(hba);
5925
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005926 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305927 goto out;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305928
Subhash Jadavani9c807702017-04-01 00:35:51 -07005929 /*
5930 * Make sure the clocks are ON before we proceed with err
5931 * handling. For the majority of cases err handler would be
5932 * run with clocks ON. There is a possibility that the err
5933 * handler was scheduled due to auto hibern8 error interrupt,
5934 * in which case the clocks could be gated or be in the
5935 * process of gating when the err handler runs.
5936 */
5937 if (unlikely((hba->clk_gating.state != CLKS_ON) &&
5938 ufshcd_is_auto_hibern8_supported(hba))) {
5939 spin_unlock_irqrestore(hba->host->host_lock, flags);
5940 ufshcd_hold(hba, false);
5941 spin_lock_irqsave(hba->host->host_lock, flags);
5942 clks_enabled = true;
5943 }
5944
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305945 hba->ufshcd_state = UFSHCD_STATE_RESET;
5946 ufshcd_set_eh_in_progress(hba);
5947
5948 /* Complete requests that have door-bell cleared by h/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005949 ufshcd_complete_requests(hba);
Yaniv Gardi583fa622016-03-10 17:37:13 +02005950
Subhash Jadavani4f0df17b2016-12-16 13:19:27 -08005951 if (hba->dev_info.quirks &
5952 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
Yaniv Gardi583fa622016-03-10 17:37:13 +02005953 bool ret;
5954
5955 spin_unlock_irqrestore(hba->host->host_lock, flags);
5956 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
5957 ret = ufshcd_quirk_dl_nac_errors(hba);
5958 spin_lock_irqsave(hba->host->host_lock, flags);
5959 if (!ret)
5960 goto skip_err_handling;
5961 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005962
5963 /*
5964 * Dump controller state before resetting. Transfer requests state
5965 * will be dump as part of the request completion.
5966 */
5967 if (hba->saved_err & (INT_FATAL_ERRORS | UIC_ERROR)) {
5968 dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x",
5969 __func__, hba->saved_err, hba->saved_uic_err);
5970 if (!hba->silence_err_logs) {
Subhash Jadavani9c807702017-04-01 00:35:51 -07005971 /* release lock as print host regs sleeps */
5972 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005973 ufshcd_print_host_regs(hba);
5974 ufshcd_print_host_state(hba);
5975 ufshcd_print_pwr_info(hba);
5976 ufshcd_print_tmrs(hba, hba->outstanding_tasks);
Subhash Jadavani9c807702017-04-01 00:35:51 -07005977 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005978 }
5979 }
5980
Subhash Jadavani9c807702017-04-01 00:35:51 -07005981 if ((hba->saved_err & INT_FATAL_ERRORS)
5982 || hba->saved_ce_err || hba->force_host_reset ||
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005983 ((hba->saved_err & UIC_ERROR) &&
5984 (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR |
5985 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
5986 UFSHCD_UIC_DL_TCx_REPLAY_ERROR))))
5987 needs_reset = true;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305988
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005989 /*
5990 * if host reset is required then skip clearing the pending
5991 * transfers forcefully because they will automatically get
5992 * cleared after link startup.
5993 */
5994 if (needs_reset)
5995 goto skip_pending_xfer_clear;
5996
5997 /* release lock as clear command might sleep */
5998 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305999 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006000 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
6001 if (ufshcd_clear_cmd(hba, tag)) {
6002 err_xfer = true;
6003 goto lock_skip_pending_xfer_clear;
6004 }
6005 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306006
6007 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006008 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
6009 if (ufshcd_clear_tm_cmd(hba, tag)) {
6010 err_tm = true;
6011 goto lock_skip_pending_xfer_clear;
6012 }
6013 }
6014
6015lock_skip_pending_xfer_clear:
6016 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306017
6018 /* Complete the requests that are cleared by s/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006019 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306020
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006021 if (err_xfer || err_tm)
6022 needs_reset = true;
6023
6024skip_pending_xfer_clear:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306025 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006026 if (needs_reset) {
6027 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
6028
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006029 if (hba->saved_err & INT_FATAL_ERRORS)
6030 ufshcd_update_error_stats(hba,
6031 UFS_ERR_INT_FATAL_ERRORS);
6032 if (hba->saved_ce_err)
6033 ufshcd_update_error_stats(hba, UFS_ERR_CRYPTO_ENGINE);
6034
6035 if (hba->saved_err & UIC_ERROR)
6036 ufshcd_update_error_stats(hba,
6037 UFS_ERR_INT_UIC_ERROR);
6038
6039 if (err_xfer || err_tm)
6040 ufshcd_update_error_stats(hba,
6041 UFS_ERR_CLEAR_PEND_XFER_TM);
6042
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006043 /*
6044 * ufshcd_reset_and_restore() does the link reinitialization
6045 * which will need atleast one empty doorbell slot to send the
6046 * device management commands (NOP and query commands).
6047 * If there is no slot empty at this moment then free up last
6048 * slot forcefully.
6049 */
6050 if (hba->outstanding_reqs == max_doorbells)
6051 __ufshcd_transfer_req_compl(hba,
6052 (1UL << (hba->nutrs - 1)));
6053
6054 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306055 err = ufshcd_reset_and_restore(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006056 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306057 if (err) {
6058 dev_err(hba->dev, "%s: reset and restore failed\n",
6059 __func__);
6060 hba->ufshcd_state = UFSHCD_STATE_ERROR;
6061 }
6062 /*
6063 * Inform scsi mid-layer that we did reset and allow to handle
6064 * Unit Attention properly.
6065 */
6066 scsi_report_bus_reset(hba->host, 0);
6067 hba->saved_err = 0;
6068 hba->saved_uic_err = 0;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006069 hba->saved_ce_err = 0;
Subhash Jadavani9c807702017-04-01 00:35:51 -07006070 hba->force_host_reset = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306071 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006072
Yaniv Gardi583fa622016-03-10 17:37:13 +02006073skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006074 if (!needs_reset) {
6075 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
6076 if (hba->saved_err || hba->saved_uic_err)
6077 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
6078 __func__, hba->saved_err, hba->saved_uic_err);
6079 }
6080
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006081 hba->silence_err_logs = false;
Subhash Jadavani9c807702017-04-01 00:35:51 -07006082
6083 if (clks_enabled)
6084 __ufshcd_release(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306085out:
Subhash Jadavani9c807702017-04-01 00:35:51 -07006086 ufshcd_clear_eh_in_progress(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006087 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306088}
6089
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006090static void ufshcd_update_uic_reg_hist(struct ufs_uic_err_reg_hist *reg_hist,
6091 u32 reg)
6092{
6093 reg_hist->reg[reg_hist->pos] = reg;
6094 reg_hist->tstamp[reg_hist->pos] = ktime_get();
6095 reg_hist->pos = (reg_hist->pos + 1) % UIC_ERR_REG_HIST_LENGTH;
6096}
6097
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306098/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306099 * ufshcd_update_uic_error - check and set fatal UIC error flags.
6100 * @hba: per-adapter instance
Subhash Jadavani9c807702017-04-01 00:35:51 -07006101 *
6102 * Returns
6103 * IRQ_HANDLED - If interrupt is valid
6104 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306105 */
Subhash Jadavani9c807702017-04-01 00:35:51 -07006106static irqreturn_t ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306107{
6108 u32 reg;
Subhash Jadavani9c807702017-04-01 00:35:51 -07006109 irqreturn_t retval = IRQ_NONE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306110
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006111 /* PHY layer lane error */
6112 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006113 if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) &&
Subhash Jadavani9c807702017-04-01 00:35:51 -07006114 (reg & UIC_PHY_ADAPTER_LAYER_ERROR_CODE_MASK)) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006115 /*
6116 * To know whether this error is fatal or not, DB timeout
6117 * must be checked but this error is handled separately.
6118 */
6119 dev_dbg(hba->dev, "%s: UIC Lane error reported, reg 0x%x\n",
6120 __func__, reg);
6121 ufshcd_update_uic_reg_hist(&hba->ufs_stats.pa_err, reg);
Subhash Jadavani9c807702017-04-01 00:35:51 -07006122
6123 /*
6124 * Don't ignore LINERESET indication during hibern8
6125 * enter operation.
6126 */
6127 if (reg & UIC_PHY_ADAPTER_LAYER_GENERIC_ERROR) {
6128 struct uic_command *cmd = hba->active_uic_cmd;
6129
6130 if (cmd) {
6131 if (cmd->command == UIC_CMD_DME_HIBER_ENTER) {
6132 dev_err(hba->dev, "%s: LINERESET during hibern8 enter, reg 0x%x\n",
6133 __func__, reg);
6134 hba->full_init_linereset = true;
6135 }
6136 }
6137 }
6138 retval |= IRQ_HANDLED;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006139 }
6140
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306141 /* PA_INIT_ERROR is fatal and needs UIC reset */
6142 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
Subhash Jadavani9c807702017-04-01 00:35:51 -07006143 if ((reg & UIC_DATA_LINK_LAYER_ERROR) &&
6144 (reg & UIC_DATA_LINK_LAYER_ERROR_CODE_MASK)) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006145 ufshcd_update_uic_reg_hist(&hba->ufs_stats.dl_err, reg);
6146
Subhash Jadavani9c807702017-04-01 00:35:51 -07006147 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT) {
6148 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
6149 } else if (hba->dev_info.quirks &
6150 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
6151 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
6152 hba->uic_error |=
6153 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
6154 else if (reg &
6155 UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
6156 hba->uic_error |=
6157 UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
6158 }
6159 retval |= IRQ_HANDLED;
Yaniv Gardi583fa622016-03-10 17:37:13 +02006160 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306161
6162 /* UIC NL/TL/DME errors needs software retry */
6163 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
Subhash Jadavani9c807702017-04-01 00:35:51 -07006164 if ((reg & UIC_NETWORK_LAYER_ERROR) &&
6165 (reg & UIC_NETWORK_LAYER_ERROR_CODE_MASK)) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006166 ufshcd_update_uic_reg_hist(&hba->ufs_stats.nl_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306167 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
Subhash Jadavani9c807702017-04-01 00:35:51 -07006168 retval |= IRQ_HANDLED;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006169 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306170
6171 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
Subhash Jadavani9c807702017-04-01 00:35:51 -07006172 if ((reg & UIC_TRANSPORT_LAYER_ERROR) &&
6173 (reg & UIC_TRANSPORT_LAYER_ERROR_CODE_MASK)) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006174 ufshcd_update_uic_reg_hist(&hba->ufs_stats.tl_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306175 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
Subhash Jadavani9c807702017-04-01 00:35:51 -07006176 retval |= IRQ_HANDLED;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006177 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306178
6179 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
Subhash Jadavani9c807702017-04-01 00:35:51 -07006180 if ((reg & UIC_DME_ERROR) &&
6181 (reg & UIC_DME_ERROR_CODE_MASK)) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006182 ufshcd_update_uic_reg_hist(&hba->ufs_stats.dme_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306183 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
Subhash Jadavani9c807702017-04-01 00:35:51 -07006184 retval |= IRQ_HANDLED;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006185 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306186
6187 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
6188 __func__, hba->uic_error);
Subhash Jadavani9c807702017-04-01 00:35:51 -07006189 return retval;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306190}
6191
6192/**
6193 * ufshcd_check_errors - Check for errors that need s/w attention
6194 * @hba: per-adapter instance
Subhash Jadavani9c807702017-04-01 00:35:51 -07006195 *
6196 * Returns
6197 * IRQ_HANDLED - If interrupt is valid
6198 * IRQ_NONE - If invalid interrupt
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306199 */
Subhash Jadavani9c807702017-04-01 00:35:51 -07006200static irqreturn_t ufshcd_check_errors(struct ufs_hba *hba)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306201{
6202 bool queue_eh_work = false;
Subhash Jadavani9c807702017-04-01 00:35:51 -07006203 irqreturn_t retval = IRQ_NONE;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306204
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006205 if (hba->errors & INT_FATAL_ERRORS || hba->ce_error)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306206 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306207
6208 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306209 hba->uic_error = 0;
Subhash Jadavani9c807702017-04-01 00:35:51 -07006210 retval = ufshcd_update_uic_error(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306211 if (hba->uic_error)
6212 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306213 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306214
6215 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006216 /*
6217 * update the transfer error masks to sticky bits, let's do this
6218 * irrespective of current ufshcd_state.
6219 */
6220 hba->saved_err |= hba->errors;
6221 hba->saved_uic_err |= hba->uic_error;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006222 hba->saved_ce_err |= hba->ce_error;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006223
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306224 /* handle fatal errors only when link is functional */
6225 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
Subhash Jadavani9c807702017-04-01 00:35:51 -07006226 /*
6227 * Set error handling in progress flag early so that we
6228 * don't issue new requests any more.
6229 */
6230 ufshcd_set_eh_in_progress(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306231
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306232 hba->ufshcd_state = UFSHCD_STATE_ERROR;
6233 schedule_work(&hba->eh_work);
6234 }
Subhash Jadavani9c807702017-04-01 00:35:51 -07006235 retval |= IRQ_HANDLED;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306236 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306237 /*
6238 * if (!queue_eh_work) -
6239 * Other errors are either non-fatal where host recovers
6240 * itself without s/w intervention or errors that will be
6241 * handled by the SCSI core layer.
6242 */
Subhash Jadavani9c807702017-04-01 00:35:51 -07006243 return retval;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306244}
6245
6246/**
6247 * ufshcd_tmc_handler - handle task management function completion
6248 * @hba: per adapter instance
Subhash Jadavani9c807702017-04-01 00:35:51 -07006249 *
6250 * Returns
6251 * IRQ_HANDLED - If interrupt is valid
6252 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306253 */
Subhash Jadavani9c807702017-04-01 00:35:51 -07006254static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306255{
6256 u32 tm_doorbell;
6257
Seungwon Jeonb873a2752013-06-26 22:39:26 +05306258 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306259 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Subhash Jadavani9c807702017-04-01 00:35:51 -07006260 if (hba->tm_condition) {
6261 wake_up(&hba->tm_wq);
6262 return IRQ_HANDLED;
6263 } else {
6264 return IRQ_NONE;
6265 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306266}
6267
6268/**
6269 * ufshcd_sl_intr - Interrupt service routine
6270 * @hba: per adapter instance
6271 * @intr_status: contains interrupts generated by the controller
Subhash Jadavani9c807702017-04-01 00:35:51 -07006272 *
6273 * Returns
6274 * IRQ_HANDLED - If interrupt is valid
6275 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306276 */
Subhash Jadavani9c807702017-04-01 00:35:51 -07006277static irqreturn_t ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306278{
Subhash Jadavani9c807702017-04-01 00:35:51 -07006279 irqreturn_t retval = IRQ_NONE;
6280
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006281 ufsdbg_error_inject_dispatcher(hba,
6282 ERR_INJECT_INTR, intr_status, &intr_status);
6283
6284 ufshcd_vops_crypto_engine_get_status(hba, &hba->ce_error);
6285
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306286 hba->errors = UFSHCD_ERROR_MASK & intr_status;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006287 if (hba->errors || hba->ce_error)
Subhash Jadavani9c807702017-04-01 00:35:51 -07006288 retval |= ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306289
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05306290 if (intr_status & UFSHCD_UIC_MASK)
Subhash Jadavani9c807702017-04-01 00:35:51 -07006291 retval |= ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306292
6293 if (intr_status & UTP_TASK_REQ_COMPL)
Subhash Jadavani9c807702017-04-01 00:35:51 -07006294 retval |= ufshcd_tmc_handler(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306295
6296 if (intr_status & UTP_TRANSFER_REQ_COMPL)
Subhash Jadavani9c807702017-04-01 00:35:51 -07006297 retval |= ufshcd_transfer_req_compl(hba);
6298
6299 return retval;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306300}
6301
6302/**
6303 * ufshcd_intr - Main interrupt service routine
6304 * @irq: irq number
6305 * @__hba: pointer to adapter instance
6306 *
Subhash Jadavani9c807702017-04-01 00:35:51 -07006307 * Returns
6308 * IRQ_HANDLED - If interrupt is valid
6309 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306310 */
6311static irqreturn_t ufshcd_intr(int irq, void *__hba)
6312{
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02006313 u32 intr_status, enabled_intr_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306314 irqreturn_t retval = IRQ_NONE;
6315 struct ufs_hba *hba = __hba;
Subhash Jadavani9c807702017-04-01 00:35:51 -07006316 int retries = hba->nutrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306317
6318 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05306319 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306320
Subhash Jadavani9c807702017-04-01 00:35:51 -07006321 /*
6322 * There could be max of hba->nutrs reqs in flight and in worst case
6323 * if the reqs get finished 1 by 1 after the interrupt status is
6324 * read, make sure we handle them by checking the interrupt status
6325 * again in a loop until we process all of the reqs before returning.
6326 */
6327 do {
6328 enabled_intr_status =
6329 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
6330 if (intr_status)
6331 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
6332 if (enabled_intr_status)
6333 retval |= ufshcd_sl_intr(hba, enabled_intr_status);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02006334
Subhash Jadavani9c807702017-04-01 00:35:51 -07006335 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
6336 } while (intr_status && --retries);
6337
6338 if (retval == IRQ_NONE) {
6339 dev_err(hba->dev, "%s: Unhandled interrupt 0x%08x\n",
6340 __func__, intr_status);
6341 ufshcd_hex_dump(hba, "host regs: ", hba->mmio_base,
6342 UFSHCI_REG_SPACE_SIZE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306343 }
Subhash Jadavani9c807702017-04-01 00:35:51 -07006344
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306345 spin_unlock(hba->host->host_lock);
6346 return retval;
6347}
6348
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306349static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
6350{
6351 int err = 0;
6352 u32 mask = 1 << tag;
6353 unsigned long flags;
6354
6355 if (!test_bit(tag, &hba->outstanding_tasks))
6356 goto out;
6357
6358 spin_lock_irqsave(hba->host->host_lock, flags);
6359 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
6360 spin_unlock_irqrestore(hba->host->host_lock, flags);
6361
6362 /* poll for max. 1 sec to clear door bell register by h/w */
6363 err = ufshcd_wait_for_register(hba,
6364 REG_UTP_TASK_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02006365 mask, 0, 1000, 1000, true);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306366out:
6367 return err;
6368}
6369
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306370/**
6371 * ufshcd_issue_tm_cmd - issues task management commands to controller
6372 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306373 * @lun_id: LUN ID to which TM command is sent
6374 * @task_id: task ID to which the TM command is applicable
6375 * @tm_function: task management function opcode
6376 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306377 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306378 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306379 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306380static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
6381 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306382{
6383 struct utp_task_req_desc *task_req_descp;
6384 struct utp_upiu_task_req *task_req_upiup;
6385 struct Scsi_Host *host;
6386 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306387 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306388 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306389 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306390
6391 host = hba->host;
6392
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306393 /*
6394 * Get free slot, sleep if slots are unavailable.
6395 * Even though we use wait_event() which sleeps indefinitely,
6396 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
6397 */
6398 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006399 ufshcd_hold_all(hba);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306400
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306401 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306402 task_req_descp = hba->utmrdl_base_addr;
6403 task_req_descp += free_slot;
6404
6405 /* Configure task request descriptor */
6406 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
6407 task_req_descp->header.dword_2 =
6408 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
6409
6410 /* Configure task request UPIU */
6411 task_req_upiup =
6412 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306413 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306414 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306415 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306416 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306417 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306418 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03006419 /*
6420 * The host shall provide the same value for LUN field in the basic
6421 * header and for Input Parameter.
6422 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306423 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
6424 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306425
6426 /* send command to the controller */
6427 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02006428
6429 /* Make sure descriptors are ready before ringing the task doorbell */
6430 wmb();
6431
Seungwon Jeonb873a2752013-06-26 22:39:26 +05306432 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006433 /* Make sure that doorbell is committed immediately */
6434 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306435
6436 spin_unlock_irqrestore(host->host_lock, flags);
6437
6438 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306439 err = wait_event_timeout(hba->tm_wq,
6440 test_bit(free_slot, &hba->tm_condition),
6441 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306442 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306443 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
6444 __func__, tm_function);
6445 if (ufshcd_clear_tm_cmd(hba, free_slot))
6446 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
6447 __func__, free_slot);
6448 err = -ETIMEDOUT;
6449 } else {
6450 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306451 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306452
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306453 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306454 ufshcd_put_tm_slot(hba, free_slot);
6455 wake_up(&hba->tm_tag_wq);
6456
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006457 ufshcd_release_all(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306458 return err;
6459}
6460
6461/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306462 * ufshcd_eh_device_reset_handler - device reset handler registered to
6463 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306464 * @cmd: SCSI command pointer
6465 *
6466 * Returns SUCCESS/FAILED
6467 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306468static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306469{
6470 struct Scsi_Host *host;
6471 struct ufs_hba *hba;
6472 unsigned int tag;
6473 u32 pos;
6474 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306475 u8 resp = 0xF;
6476 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306477 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306478
6479 host = cmd->device->host;
6480 hba = shost_priv(host);
6481 tag = cmd->request->tag;
6482
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306483 lrbp = &hba->lrb[tag];
6484 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
6485 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306486 if (!err)
6487 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306488 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306489 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306490
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306491 /* clear the commands that were pending for corresponding LUN */
6492 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
6493 if (hba->lrb[pos].lun == lrbp->lun) {
6494 err = ufshcd_clear_cmd(hba, pos);
6495 if (err)
6496 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306497 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306498 }
6499 spin_lock_irqsave(host->host_lock, flags);
6500 ufshcd_transfer_req_compl(hba);
6501 spin_unlock_irqrestore(host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006502
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306503out:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006504 hba->req_abort_count = 0;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306505 if (!err) {
6506 err = SUCCESS;
6507 } else {
6508 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
6509 err = FAILED;
6510 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306511 return err;
6512}
6513
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006514static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap)
6515{
6516 struct ufshcd_lrb *lrbp;
6517 int tag;
6518
6519 for_each_set_bit(tag, &bitmap, hba->nutrs) {
6520 lrbp = &hba->lrb[tag];
6521 lrbp->req_abort_skip = true;
6522 }
6523}
6524
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306525/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306526 * ufshcd_abort - abort a specific command
6527 * @cmd: SCSI command pointer
6528 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306529 * Abort the pending command in device by sending UFS_ABORT_TASK task management
6530 * command, and in host controller by clearing the door-bell register. There can
6531 * be race between controller sending the command to the device while abort is
6532 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
6533 * really issued and then try to abort it.
6534 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306535 * Returns SUCCESS/FAILED
6536 */
6537static int ufshcd_abort(struct scsi_cmnd *cmd)
6538{
6539 struct Scsi_Host *host;
6540 struct ufs_hba *hba;
6541 unsigned long flags;
6542 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306543 int err = 0;
6544 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306545 u8 resp = 0xF;
6546 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03006547 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306548
6549 host = cmd->device->host;
6550 hba = shost_priv(host);
6551 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02006552 if (!ufshcd_valid_tag(hba, tag)) {
6553 dev_err(hba->dev,
6554 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
6555 __func__, tag, cmd, cmd->request);
6556 BUG();
6557 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306558
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006559 lrbp = &hba->lrb[tag];
6560
6561 ufshcd_update_error_stats(hba, UFS_ERR_TASK_ABORT);
6562
6563 /*
6564 * Task abort to the device W-LUN is illegal. When this command
6565 * will fail, due to spec violation, scsi err handling next step
6566 * will be to send LU reset which, again, is a spec violation.
6567 * To avoid these unnecessary/illegal step we skip to the last error
6568 * handling stage: reset and restore.
6569 */
6570 if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN)
6571 return ufshcd_eh_host_reset_handler(cmd);
6572
6573 ufshcd_hold_all(hba);
Dolev Ravive9d501b2014-07-01 12:22:37 +03006574 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02006575 /* If command is already aborted/completed, return SUCCESS */
6576 if (!(test_bit(tag, &hba->outstanding_reqs))) {
6577 dev_err(hba->dev,
6578 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
6579 __func__, tag, hba->outstanding_reqs, reg);
6580 goto out;
6581 }
6582
Dolev Ravive9d501b2014-07-01 12:22:37 +03006583 if (!(reg & (1 << tag))) {
6584 dev_err(hba->dev,
6585 "%s: cmd was completed, but without a notifying intr, tag = %d",
6586 __func__, tag);
6587 }
6588
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006589 /* Print Transfer Request of aborted task */
6590 dev_err(hba->dev, "%s: Device abort task at tag %d", __func__, tag);
6591
6592 /*
6593 * Print detailed info about aborted request.
6594 * As more than one request might get aborted at the same time,
6595 * print full information only for the first aborted request in order
6596 * to reduce repeated printouts. For other aborted requests only print
6597 * basic details.
6598 */
6599 scsi_print_command(cmd);
6600 if (!hba->req_abort_count) {
6601 ufshcd_print_host_regs(hba);
6602 ufshcd_print_host_state(hba);
6603 ufshcd_print_pwr_info(hba);
6604 ufshcd_print_trs(hba, 1 << tag, true);
6605 } else {
6606 ufshcd_print_trs(hba, 1 << tag, false);
6607 }
6608 hba->req_abort_count++;
6609
6610
6611 /* Skip task abort in case previous aborts failed and report failure */
6612 if (lrbp->req_abort_skip) {
6613 err = -EIO;
6614 goto out;
6615 }
6616
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306617 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
6618 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
6619 UFS_QUERY_TASK, &resp);
6620 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
6621 /* cmd pending in the device */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006622 dev_err(hba->dev, "%s: cmd pending in the device. tag = %d",
6623 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306624 break;
6625 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306626 /*
6627 * cmd not pending in the device, check if it is
6628 * in transition.
6629 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006630 dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.",
6631 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306632 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
6633 if (reg & (1 << tag)) {
6634 /* sleep for max. 200us to stabilize */
6635 usleep_range(100, 200);
6636 continue;
6637 }
6638 /* command completed already */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006639 dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.",
6640 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306641 goto out;
6642 } else {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006643 dev_err(hba->dev,
6644 "%s: no response from device. tag = %d, err %d",
6645 __func__, tag, err);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306646 if (!err)
6647 err = resp; /* service response error */
6648 goto out;
6649 }
6650 }
6651
6652 if (!poll_cnt) {
6653 err = -EBUSY;
6654 goto out;
6655 }
6656
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306657 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
6658 UFS_ABORT_TASK, &resp);
6659 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006660 if (!err) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306661 err = resp; /* service response error */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006662 dev_err(hba->dev, "%s: issued. tag = %d, err %d",
6663 __func__, tag, err);
6664 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306665 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306666 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306667
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306668 err = ufshcd_clear_cmd(hba, tag);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006669 if (err) {
6670 dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d",
6671 __func__, tag, err);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306672 goto out;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006673 }
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306674
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306675 scsi_dma_unmap(cmd);
6676
6677 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02006678 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306679 hba->lrb[tag].cmd = NULL;
6680 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306681
6682 clear_bit_unlock(tag, &hba->lrb_in_use);
6683 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006684
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306685out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306686 if (!err) {
6687 err = SUCCESS;
6688 } else {
6689 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006690 ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306691 err = FAILED;
6692 }
6693
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006694 /*
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006695 * This ufshcd_release_all() corresponds to the original scsi cmd that
6696 * got aborted here (as we won't get any IRQ for it).
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006697 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006698 ufshcd_release_all(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306699 return err;
6700}
6701
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306702/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306703 * ufshcd_host_reset_and_restore - reset and restore host controller
6704 * @hba: per-adapter instance
6705 *
6706 * Note that host controller reset may issue DME_RESET to
6707 * local and remote (device) Uni-Pro stack and the attributes
6708 * are reset to default state.
6709 *
6710 * Returns zero on success, non-zero on failure
6711 */
6712static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
6713{
6714 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306715 unsigned long flags;
6716
6717 /* Reset the host controller */
6718 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02006719 ufshcd_hba_stop(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306720 spin_unlock_irqrestore(hba->host->host_lock, flags);
6721
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006722 /* scale up clocks to max frequency before full reinitialization */
6723 ufshcd_set_clk_freq(hba, true);
6724
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306725 err = ufshcd_hba_enable(hba);
6726 if (err)
6727 goto out;
6728
6729 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006730 err = ufshcd_probe_hba(hba);
6731
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006732 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL)) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306733 err = -EIO;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006734 goto out;
6735 }
6736
6737 if (!err) {
6738 err = ufshcd_vops_crypto_engine_reset(hba);
6739 if (err) {
6740 dev_err(hba->dev,
6741 "%s: failed to reset crypto engine %d\n",
6742 __func__, err);
6743 goto out;
6744 }
6745 }
6746
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306747out:
6748 if (err)
6749 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
6750
6751 return err;
6752}
6753
6754/**
6755 * ufshcd_reset_and_restore - reset and re-initialize host/device
6756 * @hba: per-adapter instance
6757 *
6758 * Reset and recover device, host and re-establish link. This
6759 * is helpful to recover the communication in fatal error conditions.
6760 *
6761 * Returns zero on success, non-zero on failure
6762 */
6763static int ufshcd_reset_and_restore(struct ufs_hba *hba)
6764{
6765 int err = 0;
6766 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006767 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306768
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006769 do {
Subhash Jadavani9c807702017-04-01 00:35:51 -07006770 err = ufshcd_vops_full_reset(hba);
6771 if (err)
6772 dev_warn(hba->dev, "%s: full reset returned %d\n",
6773 __func__, err);
6774
6775 err = ufshcd_reset_device(hba);
6776 if (err)
6777 dev_warn(hba->dev, "%s: device reset failed. err %d\n",
6778 __func__, err);
6779
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006780 err = ufshcd_host_reset_and_restore(hba);
6781 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306782
6783 /*
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006784 * There is no point proceeding even after failing
6785 * to recover after multiple retries.
6786 */
6787 if (err)
6788 BUG();
6789 /*
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306790 * After reset the door-bell might be cleared, complete
6791 * outstanding requests in s/w here.
6792 */
6793 spin_lock_irqsave(hba->host->host_lock, flags);
6794 ufshcd_transfer_req_compl(hba);
6795 ufshcd_tmc_handler(hba);
6796 spin_unlock_irqrestore(hba->host->host_lock, flags);
6797
6798 return err;
6799}
6800
6801/**
6802 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
6803 * @cmd - SCSI command pointer
6804 *
6805 * Returns SUCCESS/FAILED
6806 */
6807static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
6808{
Subhash Jadavani9c807702017-04-01 00:35:51 -07006809 int err = SUCCESS;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306810 unsigned long flags;
6811 struct ufs_hba *hba;
6812
6813 hba = shost_priv(cmd->device->host);
6814
6815 /*
6816 * Check if there is any race with fatal error handling.
6817 * If so, wait for it to complete. Even though fatal error
6818 * handling does reset and restore in some cases, don't assume
6819 * anything out of it. We are just avoiding race here.
6820 */
6821 do {
6822 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306823 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306824 hba->ufshcd_state == UFSHCD_STATE_RESET))
6825 break;
6826 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani9c807702017-04-01 00:35:51 -07006827 dev_err(hba->dev, "%s: reset in progress - 1\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306828 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306829 } while (1);
6830
Subhash Jadavani9c807702017-04-01 00:35:51 -07006831 /*
6832 * we don't know if previous reset had really reset the host controller
6833 * or not. So let's force reset here to be sure.
6834 */
6835 hba->ufshcd_state = UFSHCD_STATE_ERROR;
6836 hba->force_host_reset = true;
6837 schedule_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306838
Subhash Jadavani9c807702017-04-01 00:35:51 -07006839 /* wait for the reset work to finish */
6840 do {
6841 if (!(work_pending(&hba->eh_work) ||
6842 hba->ufshcd_state == UFSHCD_STATE_RESET))
6843 break;
6844 spin_unlock_irqrestore(hba->host->host_lock, flags);
6845 dev_err(hba->dev, "%s: reset in progress - 2\n", __func__);
6846 flush_work(&hba->eh_work);
6847 spin_lock_irqsave(hba->host->host_lock, flags);
6848 } while (1);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306849
Subhash Jadavani9c807702017-04-01 00:35:51 -07006850 if (!((hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) &&
6851 ufshcd_is_link_active(hba))) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306852 err = FAILED;
6853 hba->ufshcd_state = UFSHCD_STATE_ERROR;
6854 }
Subhash Jadavani9c807702017-04-01 00:35:51 -07006855
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306856 spin_unlock_irqrestore(hba->host->host_lock, flags);
6857
6858 return err;
6859}
6860
6861/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006862 * ufshcd_get_max_icc_level - calculate the ICC level
6863 * @sup_curr_uA: max. current supported by the regulator
6864 * @start_scan: row at the desc table to start scan from
6865 * @buff: power descriptor buffer
6866 *
6867 * Returns calculated max ICC level for specific regulator
6868 */
6869static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
6870{
6871 int i;
6872 int curr_uA;
6873 u16 data;
6874 u16 unit;
6875
6876 for (i = start_scan; i >= 0; i--) {
6877 data = be16_to_cpu(*((u16 *)(buff + 2*i)));
6878 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
6879 ATTR_ICC_LVL_UNIT_OFFSET;
6880 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
6881 switch (unit) {
6882 case UFSHCD_NANO_AMP:
6883 curr_uA = curr_uA / 1000;
6884 break;
6885 case UFSHCD_MILI_AMP:
6886 curr_uA = curr_uA * 1000;
6887 break;
6888 case UFSHCD_AMP:
6889 curr_uA = curr_uA * 1000 * 1000;
6890 break;
6891 case UFSHCD_MICRO_AMP:
6892 default:
6893 break;
6894 }
6895 if (sup_curr_uA >= curr_uA)
6896 break;
6897 }
6898 if (i < 0) {
6899 i = 0;
6900 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
6901 }
6902
6903 return (u32)i;
6904}
6905
6906/**
Subhash Jadavania8d1ba32016-12-12 18:19:21 -08006907 * ufshcd_find_max_sup_active_icc_level - find the max ICC level
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006908 * In case regulators are not initialized we'll return 0
6909 * @hba: per-adapter instance
6910 * @desc_buf: power descriptor buffer to extract ICC levels from.
6911 * @len: length of desc_buff
6912 *
Subhash Jadavania8d1ba32016-12-12 18:19:21 -08006913 * Returns calculated max ICC level
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006914 */
6915static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
6916 u8 *desc_buf, int len)
6917{
6918 u32 icc_level = 0;
6919
Subhash Jadavania8d1ba32016-12-12 18:19:21 -08006920 /*
6921 * VCCQ rail is optional for removable UFS card and also most of the
6922 * vendors don't use this rail for embedded UFS devices as well. So
6923 * it is normal that VCCQ rail may not be provided for given platform.
6924 */
6925 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq2) {
6926 dev_err(hba->dev, "%s: Regulator capability was not set, bActiveICCLevel=%d\n",
6927 __func__, icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006928 goto out;
6929 }
6930
6931 if (hba->vreg_info.vcc)
6932 icc_level = ufshcd_get_max_icc_level(
6933 hba->vreg_info.vcc->max_uA,
6934 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
6935 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
6936
6937 if (hba->vreg_info.vccq)
6938 icc_level = ufshcd_get_max_icc_level(
6939 hba->vreg_info.vccq->max_uA,
6940 icc_level,
6941 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
6942
6943 if (hba->vreg_info.vccq2)
6944 icc_level = ufshcd_get_max_icc_level(
6945 hba->vreg_info.vccq2->max_uA,
6946 icc_level,
6947 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
6948out:
6949 return icc_level;
6950}
6951
Subhash Jadavani8a93dbd2016-12-12 17:59:44 -08006952static void ufshcd_set_active_icc_lvl(struct ufs_hba *hba)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006953{
6954 int ret;
6955 int buff_len = QUERY_DESC_POWER_MAX_SIZE;
6956 u8 desc_buf[QUERY_DESC_POWER_MAX_SIZE];
Subhash Jadavani35732e52016-12-09 16:09:42 -08006957 u32 icc_level;
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006958
6959 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
6960 if (ret) {
6961 dev_err(hba->dev,
6962 "%s: Failed reading power descriptor.len = %d ret = %d",
6963 __func__, buff_len, ret);
6964 return;
6965 }
6966
Subhash Jadavani35732e52016-12-09 16:09:42 -08006967 icc_level = ufshcd_find_max_sup_active_icc_level(hba, desc_buf,
6968 buff_len);
6969 dev_dbg(hba->dev, "%s: setting icc_level 0x%x", __func__, icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006970
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02006971 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Subhash Jadavani35732e52016-12-09 16:09:42 -08006972 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, &icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006973
6974 if (ret)
6975 dev_err(hba->dev,
6976 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
Subhash Jadavani35732e52016-12-09 16:09:42 -08006977 __func__, icc_level, ret);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006978}
6979
6980/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006981 * ufshcd_scsi_add_wlus - Adds required W-LUs
6982 * @hba: per-adapter instance
6983 *
Subhash Jadavani2df121a2016-12-15 18:27:31 -08006984 * UFS devices can support upto 4 well known logical units:
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006985 * "REPORT_LUNS" (address: 01h)
6986 * "UFS Device" (address: 50h)
6987 * "RPMB" (address: 44h)
6988 * "BOOT" (address: 30h)
Subhash Jadavani2df121a2016-12-15 18:27:31 -08006989 *
6990 * "REPORT_LUNS" & "UFS Device" are mandatory for all device classes (see
6991 * "bDeviceSubClass" parameter of device descriptor) while "BOOT" is supported
6992 * only for bootable devices. "RPMB" is only supported with embedded devices.
6993 *
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006994 * UFS device's power management needs to be controlled by "POWER CONDITION"
6995 * field of SSU (START STOP UNIT) command. But this "power condition" field
6996 * will take effect only when its sent to "UFS device" well known logical unit
6997 * hence we require the scsi_device instance to represent this logical unit in
6998 * order for the UFS host driver to send the SSU command for power management.
6999
7000 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
7001 * Block) LU so user space process can control this LU. User space may also
7002 * want to have access to BOOT LU.
7003
Subhash Jadavani2df121a2016-12-15 18:27:31 -08007004 * This function tries to add scsi device instances for each of all well known
7005 * LUs (except "REPORT LUNS" LU) depending on device class.
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007006 *
7007 * Returns zero on success (all required W-LUs are added successfully),
7008 * non-zero error value on failure (if failed to add any of the required W-LU).
7009 */
7010static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
7011{
7012 int ret = 0;
Channagoud Kadabi075db3b2017-03-16 14:26:17 -07007013 struct scsi_device *sdev_rpmb = NULL;
7014 struct scsi_device *sdev_boot = NULL;
Subhash Jadavani2df121a2016-12-15 18:27:31 -08007015 bool is_bootable_dev = false;
7016 bool is_embedded_dev = false;
7017
7018 if ((hba->dev_info.b_device_sub_class == UFS_DEV_EMBEDDED_BOOTABLE) ||
7019 (hba->dev_info.b_device_sub_class == UFS_DEV_REMOVABLE_BOOTABLE))
7020 is_bootable_dev = true;
7021
7022 if ((hba->dev_info.b_device_sub_class == UFS_DEV_EMBEDDED_BOOTABLE) ||
7023 (hba->dev_info.b_device_sub_class == UFS_DEV_EMBEDDED_NON_BOOTABLE))
7024 is_embedded_dev = true;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007025
7026 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
7027 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
7028 if (IS_ERR(hba->sdev_ufs_device)) {
7029 ret = PTR_ERR(hba->sdev_ufs_device);
Subhash Jadavani2df121a2016-12-15 18:27:31 -08007030 dev_err(hba->dev, "%s: failed adding DEVICE_WLUN. ret %d\n",
7031 __func__, ret);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007032 hba->sdev_ufs_device = NULL;
7033 goto out;
7034 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03007035 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007036
Subhash Jadavani2df121a2016-12-15 18:27:31 -08007037 if (is_bootable_dev) {
7038 sdev_boot = __scsi_add_device(hba->host, 0, 0,
7039 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN),
7040 NULL);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007041
Subhash Jadavani2df121a2016-12-15 18:27:31 -08007042 if (IS_ERR(sdev_boot)) {
7043 dev_err(hba->dev, "%s: failed adding BOOT_WLUN. ret %d\n",
7044 __func__, ret);
7045 ret = PTR_ERR(sdev_boot);
7046 goto remove_sdev_ufs_device;
7047 }
7048 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007049 }
Subhash Jadavani2df121a2016-12-15 18:27:31 -08007050
7051 if (is_embedded_dev) {
7052 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
7053 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN),
7054 NULL);
7055 if (IS_ERR(sdev_rpmb)) {
7056 dev_err(hba->dev, "%s: failed adding RPMB_WLUN. ret %d\n",
7057 __func__, ret);
7058 ret = PTR_ERR(sdev_rpmb);
7059 goto remove_sdev_boot;
7060 }
7061 scsi_device_put(sdev_rpmb);
7062 }
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007063 goto out;
7064
7065remove_sdev_boot:
Subhash Jadavani2df121a2016-12-15 18:27:31 -08007066 if (is_bootable_dev)
7067 scsi_remove_device(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007068remove_sdev_ufs_device:
7069 scsi_remove_device(hba->sdev_ufs_device);
7070out:
7071 return ret;
7072}
7073
7074/**
Yaniv Gardi37113102016-03-10 17:37:16 +02007075 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
7076 * @hba: per-adapter instance
7077 *
7078 * PA_TActivate parameter can be tuned manually if UniPro version is less than
7079 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
7080 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
7081 * the hibern8 exit latency.
7082 *
7083 * Returns zero on success, non-zero error value on failure.
7084 */
7085static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
7086{
7087 int ret = 0;
7088 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
7089
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007090 if (!ufshcd_is_unipro_pa_params_tuning_req(hba))
7091 return 0;
7092
Yaniv Gardi37113102016-03-10 17:37:16 +02007093 ret = ufshcd_dme_peer_get(hba,
7094 UIC_ARG_MIB_SEL(
7095 RX_MIN_ACTIVATETIME_CAPABILITY,
7096 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
7097 &peer_rx_min_activatetime);
7098 if (ret)
7099 goto out;
7100
7101 /* make sure proper unit conversion is applied */
7102 tuned_pa_tactivate =
7103 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
7104 / PA_TACTIVATE_TIME_UNIT_US);
7105 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
7106 tuned_pa_tactivate);
7107
7108out:
7109 return ret;
7110}
7111
7112/**
7113 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
7114 * @hba: per-adapter instance
7115 *
7116 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
7117 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
7118 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
7119 * This optimal value can help reduce the hibern8 exit latency.
7120 *
7121 * Returns zero on success, non-zero error value on failure.
7122 */
7123static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
7124{
7125 int ret = 0;
7126 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
7127 u32 max_hibern8_time, tuned_pa_hibern8time;
7128
7129 ret = ufshcd_dme_get(hba,
7130 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
7131 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
7132 &local_tx_hibern8_time_cap);
7133 if (ret)
7134 goto out;
7135
7136 ret = ufshcd_dme_peer_get(hba,
7137 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
7138 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
7139 &peer_rx_hibern8_time_cap);
7140 if (ret)
7141 goto out;
7142
7143 max_hibern8_time = max(local_tx_hibern8_time_cap,
7144 peer_rx_hibern8_time_cap);
7145 /* make sure proper unit conversion is applied */
7146 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
7147 / PA_HIBERN8_TIME_UNIT_US);
7148 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
7149 tuned_pa_hibern8time);
7150out:
7151 return ret;
7152}
7153
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007154/**
7155 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
7156 * less than device PA_TACTIVATE time.
7157 * @hba: per-adapter instance
7158 *
7159 * Some UFS devices require host PA_TACTIVATE to be lower than device
7160 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
7161 * for such devices.
7162 *
7163 * Returns zero on success, non-zero error value on failure.
7164 */
7165static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
7166{
7167 int ret = 0;
7168 u32 granularity, peer_granularity;
7169 u32 pa_tactivate, peer_pa_tactivate;
7170 u32 pa_tactivate_us, peer_pa_tactivate_us;
7171 u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
7172
7173 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
7174 &granularity);
7175 if (ret)
7176 goto out;
7177
7178 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
7179 &peer_granularity);
7180 if (ret)
7181 goto out;
7182
7183 if ((granularity < PA_GRANULARITY_MIN_VAL) ||
7184 (granularity > PA_GRANULARITY_MAX_VAL)) {
7185 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
7186 __func__, granularity);
7187 return -EINVAL;
7188 }
7189
7190 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
7191 (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
7192 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
7193 __func__, peer_granularity);
7194 return -EINVAL;
7195 }
7196
7197 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
7198 if (ret)
7199 goto out;
7200
7201 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
7202 &peer_pa_tactivate);
7203 if (ret)
7204 goto out;
7205
7206 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
7207 peer_pa_tactivate_us = peer_pa_tactivate *
7208 gran_to_us_table[peer_granularity - 1];
7209
7210 if (pa_tactivate_us > peer_pa_tactivate_us) {
7211 u32 new_peer_pa_tactivate;
7212
7213 new_peer_pa_tactivate = pa_tactivate_us /
7214 gran_to_us_table[peer_granularity - 1];
7215 new_peer_pa_tactivate++;
7216 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
7217 new_peer_pa_tactivate);
7218 }
7219
7220out:
7221 return ret;
7222}
7223
Yaniv Gardi37113102016-03-10 17:37:16 +02007224static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
7225{
7226 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
7227 ufshcd_tune_pa_tactivate(hba);
7228 ufshcd_tune_pa_hibern8time(hba);
7229 }
7230
Subhash Jadavani4f0df17b2016-12-16 13:19:27 -08007231 if (hba->dev_info.quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
Yaniv Gardi37113102016-03-10 17:37:16 +02007232 /* set 1ms timeout for PA_TACTIVATE */
7233 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007234
Subhash Jadavani4f0df17b2016-12-16 13:19:27 -08007235 if (hba->dev_info.quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007236 ufshcd_quirk_tune_host_pa_tactivate(hba);
7237
7238 ufshcd_vops_apply_dev_quirks(hba);
7239}
7240
7241static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba)
7242{
7243 int err_reg_hist_size = sizeof(struct ufs_uic_err_reg_hist);
7244
7245 hba->ufs_stats.hibern8_exit_cnt = 0;
7246 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
7247
7248 memset(&hba->ufs_stats.pa_err, 0, err_reg_hist_size);
7249 memset(&hba->ufs_stats.dl_err, 0, err_reg_hist_size);
7250 memset(&hba->ufs_stats.nl_err, 0, err_reg_hist_size);
7251 memset(&hba->ufs_stats.tl_err, 0, err_reg_hist_size);
7252 memset(&hba->ufs_stats.dme_err, 0, err_reg_hist_size);
7253
7254 hba->req_abort_count = 0;
7255}
7256
7257static void ufshcd_apply_pm_quirks(struct ufs_hba *hba)
7258{
Subhash Jadavani4f0df17b2016-12-16 13:19:27 -08007259 if (hba->dev_info.quirks & UFS_DEVICE_QUIRK_NO_LINK_OFF) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007260 if (ufs_get_pm_lvl_to_link_pwr_state(hba->rpm_lvl) ==
7261 UIC_LINK_OFF_STATE) {
7262 hba->rpm_lvl =
7263 ufs_get_desired_pm_lvl_for_dev_link_state(
7264 UFS_SLEEP_PWR_MODE,
7265 UIC_LINK_HIBERN8_STATE);
7266 dev_info(hba->dev, "UFS_DEVICE_QUIRK_NO_LINK_OFF enabled, changed rpm_lvl to %d\n",
7267 hba->rpm_lvl);
7268 }
7269 if (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) ==
7270 UIC_LINK_OFF_STATE) {
7271 hba->spm_lvl =
7272 ufs_get_desired_pm_lvl_for_dev_link_state(
7273 UFS_SLEEP_PWR_MODE,
7274 UIC_LINK_HIBERN8_STATE);
7275 dev_info(hba->dev, "UFS_DEVICE_QUIRK_NO_LINK_OFF enabled, changed spm_lvl to %d\n",
7276 hba->spm_lvl);
7277 }
7278 }
Yaniv Gardi37113102016-03-10 17:37:16 +02007279}
7280
7281/**
Subhash Jadavani88f99992016-12-13 15:52:21 -08007282 * ufshcd_set_dev_ref_clk - set the device bRefClkFreq
7283 * @hba: per-adapter instance
7284 *
7285 * Read the current value of the bRefClkFreq attribute from device and update it
7286 * if host is supplying different reference clock frequency than one mentioned
7287 * in bRefClkFreq attribute.
7288 *
7289 * Returns zero on success, non-zero error value on failure.
7290 */
7291static int ufshcd_set_dev_ref_clk(struct ufs_hba *hba)
7292{
7293 int err = 0;
7294 int ref_clk = -1;
7295 static const char * const ref_clk_freqs[] = {"19.2 MHz", "26 MHz",
7296 "38.4 MHz", "52 MHz"};
7297
7298 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
7299 QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &ref_clk);
7300
7301 if (err) {
7302 dev_err(hba->dev, "%s: failed reading bRefClkFreq. err = %d\n",
7303 __func__, err);
7304 goto out;
7305 }
7306
7307 if ((ref_clk < 0) || (ref_clk > REF_CLK_FREQ_52_MHZ)) {
7308 dev_err(hba->dev, "%s: invalid ref_clk setting = %d\n",
7309 __func__, ref_clk);
7310 err = -EINVAL;
7311 goto out;
7312 }
7313
7314 if (ref_clk == hba->dev_ref_clk_freq)
7315 goto out; /* nothing to update */
7316
7317 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
7318 QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0,
7319 &hba->dev_ref_clk_freq);
7320
7321 if (err)
7322 dev_err(hba->dev, "%s: bRefClkFreq setting to %s failed\n",
7323 __func__, ref_clk_freqs[hba->dev_ref_clk_freq]);
7324 else
7325 /*
7326 * It is good to print this out here to debug any later failures
7327 * related to gear switch.
7328 */
7329 dev_info(hba->dev, "%s: bRefClkFreq setting to %s succeeded\n",
7330 __func__, ref_clk_freqs[hba->dev_ref_clk_freq]);
7331
7332out:
7333 return err;
7334}
7335
Subhash Jadavani344c16c2016-12-15 17:09:35 -08007336static int ufs_read_device_desc_data(struct ufs_hba *hba)
7337{
7338 int err;
7339 u8 desc_buf[QUERY_DESC_DEVICE_MAX_SIZE];
7340
7341 err = ufshcd_read_device_desc(hba, desc_buf, sizeof(desc_buf));
7342 if (err)
7343 return err;
7344
7345 /*
7346 * getting vendor (manufacturerID) and Bank Index in big endian
7347 * format
7348 */
7349 hba->dev_info.w_manufacturer_id =
7350 desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
7351 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
7352 hba->dev_info.b_device_sub_class =
7353 desc_buf[DEVICE_DESC_PARAM_DEVICE_SUB_CLASS];
7354 hba->dev_info.i_product_name = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
7355
7356 return 0;
7357}
7358
Subhash Jadavani88f99992016-12-13 15:52:21 -08007359/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007360 * ufshcd_probe_hba - probe hba to detect device and initialize
7361 * @hba: per-adapter instance
7362 *
7363 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307364 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007365static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307366{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307367 int ret;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007368 ktime_t start = ktime_get();
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307369
7370 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307371 if (ret)
7372 goto out;
7373
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007374 /* Debug counters initialization */
7375 ufshcd_clear_dbg_ufs_stats(hba);
Yaniv Gardiafdfff52016-03-10 17:37:15 +02007376 /* set the default level for urgent bkops */
7377 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
7378 hba->is_urgent_bkops_lvl_checked = false;
7379
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007380 /* UniPro link is active now */
7381 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05307382
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307383 ret = ufshcd_verify_dev_init(hba);
7384 if (ret)
7385 goto out;
7386
Dolev Raviv68078d52013-07-30 00:35:58 +05307387 ret = ufshcd_complete_dev_init(hba);
7388 if (ret)
7389 goto out;
7390
Subhash Jadavani2df121a2016-12-15 18:27:31 -08007391 /* clear any previous UFS device information */
7392 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
7393
Subhash Jadavani344c16c2016-12-15 17:09:35 -08007394 /* cache important parameters from device descriptor for later use */
7395 ret = ufs_read_device_desc_data(hba);
7396 if (ret)
7397 goto out;
7398
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007399 ufs_advertise_fixup_device(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02007400 ufshcd_tune_unipro_params(hba);
Yaniv Gardi60f01872016-03-10 17:37:11 +02007401
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007402 ufshcd_apply_pm_quirks(hba);
Yaniv Gardi60f01872016-03-10 17:37:11 +02007403 ret = ufshcd_set_vccq_rail_unused(hba,
Subhash Jadavani4f0df17b2016-12-16 13:19:27 -08007404 (hba->dev_info.quirks & UFS_DEVICE_NO_VCCQ) ? true : false);
Yaniv Gardi60f01872016-03-10 17:37:11 +02007405 if (ret)
7406 goto out;
7407
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007408 /* UFS device is also active now */
7409 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307410 ufshcd_force_reset_auto_bkops(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307411
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007412 if (ufshcd_get_max_pwr_mode(hba)) {
7413 dev_err(hba->dev,
7414 "%s: Failed getting max supported power mode\n",
7415 __func__);
7416 } else {
Subhash Jadavani88f99992016-12-13 15:52:21 -08007417 /*
7418 * Set the right value to bRefClkFreq before attempting to
7419 * switch to HS gears.
7420 */
7421 ufshcd_set_dev_ref_clk(hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007422 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007423 if (ret) {
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007424 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
7425 __func__, ret);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007426 goto out;
7427 }
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007428 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007429
Subhash Jadavani8a93dbd2016-12-12 17:59:44 -08007430 /*
7431 * bActiveICCLevel is volatile for UFS device (as per latest v2.1 spec)
7432 * and for removable UFS card as well, hence always set the parameter.
7433 * Note: Error handler may issue the device reset hence resetting
7434 * bActiveICCLevel as well so it is always safe to set this here.
7435 */
7436 ufshcd_set_active_icc_lvl(hba);
7437
Yaniv Gardi53c12d02016-02-01 15:02:45 +02007438 /* set the state as operational after switching to desired gear */
7439 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007440 /*
7441 * If we are in error handling context or in power management callbacks
7442 * context, no need to scan the host
7443 */
7444 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
7445 bool flag;
7446
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02007447 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
7448 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007449 hba->dev_info.f_power_on_wp_en = flag;
7450
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007451 /* Add required well known logical units to scsi mid layer */
7452 if (ufshcd_scsi_add_wlus(hba))
7453 goto out;
7454
Subhash Jadavani9c807702017-04-01 00:35:51 -07007455 /* Initialize devfreq after UFS device is detected */
7456 if (ufshcd_is_clkscaling_supported(hba)) {
7457 memcpy(&hba->clk_scaling.saved_pwr_info.info,
7458 &hba->pwr_info, sizeof(struct ufs_pa_layer_attr));
7459 hba->clk_scaling.saved_pwr_info.is_valid = true;
7460 hba->clk_scaling.is_scaled_up = true;
7461 if (!hba->devfreq) {
7462 hba->devfreq = devfreq_add_device(hba->dev,
7463 &ufs_devfreq_profile,
7464 "simple_ondemand",
7465 gov_data);
7466 if (IS_ERR(hba->devfreq)) {
7467 ret = PTR_ERR(hba->devfreq);
7468 dev_err(hba->dev, "Unable to register with devfreq %d\n",
7469 ret);
7470 goto out;
7471 }
7472 }
7473 hba->clk_scaling.is_allowed = true;
7474 }
7475
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307476 scsi_scan_host(hba->host);
7477 pm_runtime_put_sync(hba->dev);
7478 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007479
Subhash Jadavani9c807702017-04-01 00:35:51 -07007480 /*
7481 * Enable auto hibern8 if supported, after full host and
7482 * device initialization.
7483 */
7484 if (ufshcd_is_auto_hibern8_supported(hba))
7485 ufshcd_set_auto_hibern8_timer(hba,
7486 hba->hibern8_on_idle.delay_ms);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307487out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007488 /*
7489 * If we failed to initialize the device or the device is not
7490 * present, turn off the power/clocks etc.
7491 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007492 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
7493 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007494 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007495 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007496
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007497 trace_ufshcd_init(dev_name(hba->dev), ret,
7498 ktime_to_us(ktime_sub(ktime_get(), start)),
7499 hba->curr_dev_pwr_mode, hba->uic_link_state);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007500 return ret;
7501}
7502
7503/**
7504 * ufshcd_async_scan - asynchronous execution for probing hba
7505 * @data: data pointer to pass to this function
7506 * @cookie: cookie data
7507 */
7508static void ufshcd_async_scan(void *data, async_cookie_t cookie)
7509{
7510 struct ufs_hba *hba = (struct ufs_hba *)data;
7511
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007512 /*
7513 * Don't allow clock gating and hibern8 enter for faster device
7514 * detection.
7515 */
7516 ufshcd_hold_all(hba);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007517 ufshcd_probe_hba(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007518 ufshcd_release_all(hba);
7519}
7520
7521/**
7522 * ufshcd_query_ioctl - perform user read queries
7523 * @hba: per-adapter instance
7524 * @lun: used for lun specific queries
7525 * @buffer: user space buffer for reading and submitting query data and params
7526 * @return: 0 for success negative error code otherwise
7527 *
7528 * Expected/Submitted buffer structure is struct ufs_ioctl_query_data.
7529 * It will read the opcode, idn and buf_length parameters, and, put the
7530 * response in the buffer field while updating the used size in buf_length.
7531 */
7532static int ufshcd_query_ioctl(struct ufs_hba *hba, u8 lun, void __user *buffer)
7533{
7534 struct ufs_ioctl_query_data *ioctl_data;
7535 int err = 0;
7536 int length = 0;
7537 void *data_ptr;
7538 bool flag;
7539 u32 att;
7540 u8 index;
7541 u8 *desc = NULL;
7542
7543 ioctl_data = kzalloc(sizeof(struct ufs_ioctl_query_data), GFP_KERNEL);
7544 if (!ioctl_data) {
7545 dev_err(hba->dev, "%s: Failed allocating %zu bytes\n", __func__,
7546 sizeof(struct ufs_ioctl_query_data));
7547 err = -ENOMEM;
7548 goto out;
7549 }
7550
7551 /* extract params from user buffer */
7552 err = copy_from_user(ioctl_data, buffer,
7553 sizeof(struct ufs_ioctl_query_data));
7554 if (err) {
7555 dev_err(hba->dev,
7556 "%s: Failed copying buffer from user, err %d\n",
7557 __func__, err);
7558 goto out_release_mem;
7559 }
7560
7561 /* verify legal parameters & send query */
7562 switch (ioctl_data->opcode) {
7563 case UPIU_QUERY_OPCODE_READ_DESC:
7564 switch (ioctl_data->idn) {
7565 case QUERY_DESC_IDN_DEVICE:
7566 case QUERY_DESC_IDN_CONFIGURAION:
7567 case QUERY_DESC_IDN_INTERCONNECT:
7568 case QUERY_DESC_IDN_GEOMETRY:
7569 case QUERY_DESC_IDN_POWER:
7570 index = 0;
7571 break;
7572 case QUERY_DESC_IDN_UNIT:
7573 if (!ufs_is_valid_unit_desc_lun(lun)) {
7574 dev_err(hba->dev,
7575 "%s: No unit descriptor for lun 0x%x\n",
7576 __func__, lun);
7577 err = -EINVAL;
7578 goto out_release_mem;
7579 }
7580 index = lun;
7581 break;
7582 default:
7583 goto out_einval;
7584 }
7585 length = min_t(int, QUERY_DESC_MAX_SIZE,
7586 ioctl_data->buf_size);
7587 desc = kzalloc(length, GFP_KERNEL);
7588 if (!desc) {
7589 dev_err(hba->dev, "%s: Failed allocating %d bytes\n",
7590 __func__, length);
7591 err = -ENOMEM;
7592 goto out_release_mem;
7593 }
7594 err = ufshcd_query_descriptor(hba, ioctl_data->opcode,
7595 ioctl_data->idn, index, 0, desc, &length);
7596 break;
7597 case UPIU_QUERY_OPCODE_READ_ATTR:
7598 switch (ioctl_data->idn) {
7599 case QUERY_ATTR_IDN_BOOT_LU_EN:
7600 case QUERY_ATTR_IDN_POWER_MODE:
7601 case QUERY_ATTR_IDN_ACTIVE_ICC_LVL:
7602 case QUERY_ATTR_IDN_OOO_DATA_EN:
7603 case QUERY_ATTR_IDN_BKOPS_STATUS:
7604 case QUERY_ATTR_IDN_PURGE_STATUS:
7605 case QUERY_ATTR_IDN_MAX_DATA_IN:
7606 case QUERY_ATTR_IDN_MAX_DATA_OUT:
7607 case QUERY_ATTR_IDN_REF_CLK_FREQ:
7608 case QUERY_ATTR_IDN_CONF_DESC_LOCK:
7609 case QUERY_ATTR_IDN_MAX_NUM_OF_RTT:
7610 case QUERY_ATTR_IDN_EE_CONTROL:
7611 case QUERY_ATTR_IDN_EE_STATUS:
7612 case QUERY_ATTR_IDN_SECONDS_PASSED:
7613 index = 0;
7614 break;
7615 case QUERY_ATTR_IDN_DYN_CAP_NEEDED:
7616 case QUERY_ATTR_IDN_CORR_PRG_BLK_NUM:
7617 index = lun;
7618 break;
7619 default:
7620 goto out_einval;
7621 }
7622 err = ufshcd_query_attr(hba, ioctl_data->opcode, ioctl_data->idn,
7623 index, 0, &att);
7624 break;
7625
7626 case UPIU_QUERY_OPCODE_WRITE_ATTR:
7627 err = copy_from_user(&att,
7628 buffer + sizeof(struct ufs_ioctl_query_data),
7629 sizeof(u32));
7630 if (err) {
7631 dev_err(hba->dev,
7632 "%s: Failed copying buffer from user, err %d\n",
7633 __func__, err);
7634 goto out_release_mem;
7635 }
7636
7637 switch (ioctl_data->idn) {
7638 case QUERY_ATTR_IDN_BOOT_LU_EN:
7639 index = 0;
7640 if (att > QUERY_ATTR_IDN_BOOT_LU_EN_MAX) {
7641 dev_err(hba->dev,
7642 "%s: Illegal ufs query ioctl data, opcode 0x%x, idn 0x%x, att 0x%x\n",
7643 __func__, ioctl_data->opcode,
7644 (unsigned int)ioctl_data->idn, att);
7645 err = -EINVAL;
7646 goto out_release_mem;
7647 }
7648 break;
7649 default:
7650 goto out_einval;
7651 }
7652 err = ufshcd_query_attr(hba, ioctl_data->opcode,
7653 ioctl_data->idn, index, 0, &att);
7654 break;
7655
7656 case UPIU_QUERY_OPCODE_READ_FLAG:
7657 switch (ioctl_data->idn) {
7658 case QUERY_FLAG_IDN_FDEVICEINIT:
7659 case QUERY_FLAG_IDN_PERMANENT_WPE:
7660 case QUERY_FLAG_IDN_PWR_ON_WPE:
7661 case QUERY_FLAG_IDN_BKOPS_EN:
7662 case QUERY_FLAG_IDN_PURGE_ENABLE:
7663 case QUERY_FLAG_IDN_FPHYRESOURCEREMOVAL:
7664 case QUERY_FLAG_IDN_BUSY_RTC:
7665 break;
7666 default:
7667 goto out_einval;
7668 }
7669 err = ufshcd_query_flag_retry(hba, ioctl_data->opcode,
7670 ioctl_data->idn, &flag);
7671 break;
7672 default:
7673 goto out_einval;
7674 }
7675
7676 if (err) {
7677 dev_err(hba->dev, "%s: Query for idn %d failed\n", __func__,
7678 ioctl_data->idn);
7679 goto out_release_mem;
7680 }
7681
7682 /*
7683 * copy response data
7684 * As we might end up reading less data then what is specified in
7685 * "ioctl_data->buf_size". So we are updating "ioctl_data->
7686 * buf_size" to what exactly we have read.
7687 */
7688 switch (ioctl_data->opcode) {
7689 case UPIU_QUERY_OPCODE_READ_DESC:
7690 ioctl_data->buf_size = min_t(int, ioctl_data->buf_size, length);
7691 data_ptr = desc;
7692 break;
7693 case UPIU_QUERY_OPCODE_READ_ATTR:
7694 ioctl_data->buf_size = sizeof(u32);
7695 data_ptr = &att;
7696 break;
7697 case UPIU_QUERY_OPCODE_READ_FLAG:
7698 ioctl_data->buf_size = 1;
7699 data_ptr = &flag;
7700 break;
7701 case UPIU_QUERY_OPCODE_WRITE_ATTR:
7702 goto out_release_mem;
7703 default:
7704 goto out_einval;
7705 }
7706
7707 /* copy to user */
7708 err = copy_to_user(buffer, ioctl_data,
7709 sizeof(struct ufs_ioctl_query_data));
7710 if (err)
7711 dev_err(hba->dev, "%s: Failed copying back to user.\n",
7712 __func__);
7713 err = copy_to_user(buffer + sizeof(struct ufs_ioctl_query_data),
7714 data_ptr, ioctl_data->buf_size);
7715 if (err)
7716 dev_err(hba->dev, "%s: err %d copying back to user.\n",
7717 __func__, err);
7718 goto out_release_mem;
7719
7720out_einval:
7721 dev_err(hba->dev,
7722 "%s: illegal ufs query ioctl data, opcode 0x%x, idn 0x%x\n",
7723 __func__, ioctl_data->opcode, (unsigned int)ioctl_data->idn);
7724 err = -EINVAL;
7725out_release_mem:
7726 kfree(ioctl_data);
7727 kfree(desc);
7728out:
7729 return err;
7730}
7731
7732/**
7733 * ufshcd_ioctl - ufs ioctl callback registered in scsi_host
7734 * @dev: scsi device required for per LUN queries
7735 * @cmd: command opcode
7736 * @buffer: user space buffer for transferring data
7737 *
7738 * Supported commands:
7739 * UFS_IOCTL_QUERY
7740 */
7741static int ufshcd_ioctl(struct scsi_device *dev, int cmd, void __user *buffer)
7742{
7743 struct ufs_hba *hba = shost_priv(dev->host);
7744 int err = 0;
7745
7746 BUG_ON(!hba);
7747 if (!buffer) {
7748 dev_err(hba->dev, "%s: User buffer is NULL!\n", __func__);
7749 return -EINVAL;
7750 }
7751
7752 switch (cmd) {
7753 case UFS_IOCTL_QUERY:
7754 pm_runtime_get_sync(hba->dev);
7755 err = ufshcd_query_ioctl(hba, ufshcd_scsi_to_upiu_lun(dev->lun),
7756 buffer);
7757 pm_runtime_put_sync(hba->dev);
7758 break;
7759 default:
7760 err = -ENOIOCTLCMD;
7761 dev_dbg(hba->dev, "%s: Unsupported ioctl cmd %d\n", __func__,
7762 cmd);
7763 break;
7764 }
7765
7766 return err;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307767}
7768
Yaniv Gardif550c652016-03-10 17:37:07 +02007769static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd)
7770{
7771 unsigned long flags;
7772 struct Scsi_Host *host;
7773 struct ufs_hba *hba;
7774 int index;
7775 bool found = false;
7776
7777 if (!scmd || !scmd->device || !scmd->device->host)
7778 return BLK_EH_NOT_HANDLED;
7779
7780 host = scmd->device->host;
7781 hba = shost_priv(host);
7782 if (!hba)
7783 return BLK_EH_NOT_HANDLED;
7784
7785 spin_lock_irqsave(host->host_lock, flags);
7786
7787 for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) {
7788 if (hba->lrb[index].cmd == scmd) {
7789 found = true;
7790 break;
7791 }
7792 }
7793
7794 spin_unlock_irqrestore(host->host_lock, flags);
7795
7796 /*
7797 * Bypass SCSI error handling and reset the block layer timer if this
7798 * SCSI command was not actually dispatched to UFS driver, otherwise
7799 * let SCSI layer handle the error as usual.
7800 */
7801 return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER;
7802}
7803
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307804static struct scsi_host_template ufshcd_driver_template = {
7805 .module = THIS_MODULE,
7806 .name = UFSHCD,
7807 .proc_name = UFSHCD,
7808 .queuecommand = ufshcd_queuecommand,
7809 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09007810 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307811 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03007812 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307813 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307814 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
7815 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Yaniv Gardif550c652016-03-10 17:37:07 +02007816 .eh_timed_out = ufshcd_eh_timed_out,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007817 .ioctl = ufshcd_ioctl,
7818#ifdef CONFIG_COMPAT
7819 .compat_ioctl = ufshcd_ioctl,
7820#endif
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307821 .this_id = -1,
7822 .sg_tablesize = SG_ALL,
7823 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
7824 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007825 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01007826 .track_queue_depth = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307827};
7828
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007829static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
7830 int ua)
7831{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08007832 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007833
Bjorn Andersson7b16a072015-02-11 19:35:28 -08007834 if (!vreg)
7835 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007836
Bjorn Andersson7b16a072015-02-11 19:35:28 -08007837 ret = regulator_set_load(vreg->reg, ua);
7838 if (ret < 0) {
7839 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
7840 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007841 }
7842
7843 return ret;
7844}
7845
7846static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
7847 struct ufs_vreg *vreg)
7848{
Yaniv Gardi60f01872016-03-10 17:37:11 +02007849 if (!vreg)
7850 return 0;
7851 else if (vreg->unused)
7852 return 0;
7853 else
7854 return ufshcd_config_vreg_load(hba->dev, vreg,
7855 UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007856}
7857
7858static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
7859 struct ufs_vreg *vreg)
7860{
Yaniv Gardi60f01872016-03-10 17:37:11 +02007861 if (!vreg)
7862 return 0;
7863 else if (vreg->unused)
7864 return 0;
7865 else
7866 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007867}
7868
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007869static int ufshcd_config_vreg(struct device *dev,
7870 struct ufs_vreg *vreg, bool on)
7871{
7872 int ret = 0;
7873 struct regulator *reg = vreg->reg;
7874 const char *name = vreg->name;
7875 int min_uV, uA_load;
7876
7877 BUG_ON(!vreg);
7878
7879 if (regulator_count_voltages(reg) > 0) {
7880 min_uV = on ? vreg->min_uV : 0;
7881 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
7882 if (ret) {
7883 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
7884 __func__, name, ret);
7885 goto out;
7886 }
7887
7888 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007889 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
7890 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007891 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007892 }
7893out:
7894 return ret;
7895}
7896
7897static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
7898{
7899 int ret = 0;
7900
Yaniv Gardi60f01872016-03-10 17:37:11 +02007901 if (!vreg)
7902 goto out;
7903 else if (vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007904 goto out;
7905
7906 ret = ufshcd_config_vreg(dev, vreg, true);
7907 if (!ret)
7908 ret = regulator_enable(vreg->reg);
7909
7910 if (!ret)
7911 vreg->enabled = true;
7912 else
7913 dev_err(dev, "%s: %s enable failed, err=%d\n",
7914 __func__, vreg->name, ret);
7915out:
7916 return ret;
7917}
7918
7919static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
7920{
7921 int ret = 0;
7922
Yaniv Gardi60f01872016-03-10 17:37:11 +02007923 if (!vreg)
7924 goto out;
7925 else if (!vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007926 goto out;
7927
7928 ret = regulator_disable(vreg->reg);
7929
7930 if (!ret) {
7931 /* ignore errors on applying disable config */
7932 ufshcd_config_vreg(dev, vreg, false);
7933 vreg->enabled = false;
7934 } else {
7935 dev_err(dev, "%s: %s disable failed, err=%d\n",
7936 __func__, vreg->name, ret);
7937 }
7938out:
7939 return ret;
7940}
7941
7942static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
7943{
7944 int ret = 0;
7945 struct device *dev = hba->dev;
7946 struct ufs_vreg_info *info = &hba->vreg_info;
7947
7948 if (!info)
7949 goto out;
7950
7951 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
7952 if (ret)
7953 goto out;
7954
7955 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
7956 if (ret)
7957 goto out;
7958
7959 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
7960 if (ret)
7961 goto out;
7962
7963out:
7964 if (ret) {
7965 ufshcd_toggle_vreg(dev, info->vccq2, false);
7966 ufshcd_toggle_vreg(dev, info->vccq, false);
7967 ufshcd_toggle_vreg(dev, info->vcc, false);
7968 }
7969 return ret;
7970}
7971
Raviv Shvili6a771a62014-09-25 15:32:24 +03007972static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
7973{
7974 struct ufs_vreg_info *info = &hba->vreg_info;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007975 int ret = 0;
Raviv Shvili6a771a62014-09-25 15:32:24 +03007976
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007977 if (info->vdd_hba) {
7978 ret = ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
Raviv Shvili6a771a62014-09-25 15:32:24 +03007979
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007980 if (!ret)
7981 ufshcd_vops_update_sec_cfg(hba, on);
7982 }
7983
7984 return ret;
Raviv Shvili6a771a62014-09-25 15:32:24 +03007985}
7986
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007987static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
7988{
7989 int ret = 0;
7990
7991 if (!vreg)
7992 goto out;
7993
7994 vreg->reg = devm_regulator_get(dev, vreg->name);
7995 if (IS_ERR(vreg->reg)) {
7996 ret = PTR_ERR(vreg->reg);
7997 dev_err(dev, "%s: %s get failed, err=%d\n",
7998 __func__, vreg->name, ret);
7999 }
8000out:
8001 return ret;
8002}
8003
8004static int ufshcd_init_vreg(struct ufs_hba *hba)
8005{
8006 int ret = 0;
8007 struct device *dev = hba->dev;
8008 struct ufs_vreg_info *info = &hba->vreg_info;
8009
8010 if (!info)
8011 goto out;
8012
8013 ret = ufshcd_get_vreg(dev, info->vcc);
8014 if (ret)
8015 goto out;
8016
8017 ret = ufshcd_get_vreg(dev, info->vccq);
8018 if (ret)
8019 goto out;
8020
8021 ret = ufshcd_get_vreg(dev, info->vccq2);
8022out:
8023 return ret;
8024}
8025
Raviv Shvili6a771a62014-09-25 15:32:24 +03008026static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
8027{
8028 struct ufs_vreg_info *info = &hba->vreg_info;
8029
8030 if (info)
8031 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
8032
8033 return 0;
8034}
8035
Yaniv Gardi60f01872016-03-10 17:37:11 +02008036static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused)
8037{
8038 int ret = 0;
8039 struct ufs_vreg_info *info = &hba->vreg_info;
8040
8041 if (!info)
8042 goto out;
8043 else if (!info->vccq)
8044 goto out;
8045
8046 if (unused) {
8047 /* shut off the rail here */
8048 ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false);
8049 /*
8050 * Mark this rail as no longer used, so it doesn't get enabled
8051 * later by mistake
8052 */
8053 if (!ret)
8054 info->vccq->unused = true;
8055 } else {
8056 /*
8057 * rail should have been already enabled hence just make sure
8058 * that unused flag is cleared.
8059 */
8060 info->vccq->unused = false;
8061 }
8062out:
8063 return ret;
8064}
8065
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008066static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
8067 bool skip_ref_clk, bool is_gating_context)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008068{
8069 int ret = 0;
8070 struct ufs_clk_info *clki;
8071 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008072 unsigned long flags;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008073 ktime_t start = ktime_get();
8074 bool clk_state_changed = false;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008075
8076 if (!head || list_empty(head))
8077 goto out;
8078
Subhash Jadavani9c807702017-04-01 00:35:51 -07008079 /* call vendor specific bus vote before enabling the clocks */
8080 if (on) {
8081 ret = ufshcd_vops_set_bus_vote(hba, on);
8082 if (ret)
8083 return ret;
8084 }
8085
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008086 /*
8087 * vendor specific setup_clocks ops may depend on clocks managed by
8088 * this standard driver hence call the vendor specific setup_clocks
8089 * before disabling the clocks managed here.
8090 */
8091 if (!on) {
8092 ret = ufshcd_vops_setup_clocks(hba, on, is_gating_context);
8093 if (ret)
8094 return ret;
8095 }
8096
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008097 list_for_each_entry(clki, head, list) {
8098 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008099 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
8100 continue;
8101
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008102 clk_state_changed = on ^ clki->enabled;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008103 if (on && !clki->enabled) {
8104 ret = clk_prepare_enable(clki->clk);
8105 if (ret) {
8106 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
8107 __func__, clki->name, ret);
8108 goto out;
8109 }
8110 } else if (!on && clki->enabled) {
8111 clk_disable_unprepare(clki->clk);
8112 }
8113 clki->enabled = on;
8114 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
8115 clki->name, on ? "en" : "dis");
8116 }
8117 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008118
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008119 /*
8120 * vendor specific setup_clocks ops may depend on clocks managed by
8121 * this standard driver hence call the vendor specific setup_clocks
8122 * after enabling the clocks managed here.
8123 */
Subhash Jadavani9c807702017-04-01 00:35:51 -07008124 if (on) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008125 ret = ufshcd_vops_setup_clocks(hba, on, is_gating_context);
Subhash Jadavani9c807702017-04-01 00:35:51 -07008126 if (ret)
8127 goto out;
8128 }
8129
8130 /*
8131 * call vendor specific bus vote to remove the vote after
8132 * disabling the clocks.
8133 */
8134 if (!on)
8135 ret = ufshcd_vops_set_bus_vote(hba, on);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008136
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008137out:
8138 if (ret) {
Subhash Jadavani9c807702017-04-01 00:35:51 -07008139 if (on)
8140 /* Can't do much if this fails */
8141 (void) ufshcd_vops_set_bus_vote(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008142 list_for_each_entry(clki, head, list) {
8143 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
8144 clk_disable_unprepare(clki->clk);
8145 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008146 } else if (!ret && on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008147 spin_lock_irqsave(hba->host->host_lock, flags);
8148 hba->clk_gating.state = CLKS_ON;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008149 trace_ufshcd_clk_gating(dev_name(hba->dev),
8150 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008151 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008152 /* restore the secure configuration as clocks are enabled */
8153 ufshcd_vops_update_sec_cfg(hba, true);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008154 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008155
8156 if (clk_state_changed)
8157 trace_ufshcd_profile_clk_gating(dev_name(hba->dev),
8158 (on ? "on" : "off"),
8159 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008160 return ret;
8161}
8162
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008163static int ufshcd_enable_clocks(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008164{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008165 return ufshcd_setup_clocks(hba, true, false, false);
8166}
8167
8168static int ufshcd_disable_clocks(struct ufs_hba *hba,
8169 bool is_gating_context)
8170{
8171 return ufshcd_setup_clocks(hba, false, false, is_gating_context);
8172}
8173
8174static int ufshcd_disable_clocks_skip_ref_clk(struct ufs_hba *hba,
8175 bool is_gating_context)
8176{
8177 return ufshcd_setup_clocks(hba, false, true, is_gating_context);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008178}
8179
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008180static int ufshcd_init_clocks(struct ufs_hba *hba)
8181{
8182 int ret = 0;
8183 struct ufs_clk_info *clki;
8184 struct device *dev = hba->dev;
8185 struct list_head *head = &hba->clk_list_head;
8186
8187 if (!head || list_empty(head))
8188 goto out;
8189
8190 list_for_each_entry(clki, head, list) {
8191 if (!clki->name)
8192 continue;
8193
8194 clki->clk = devm_clk_get(dev, clki->name);
8195 if (IS_ERR(clki->clk)) {
8196 ret = PTR_ERR(clki->clk);
8197 dev_err(dev, "%s: %s clk get failed, %d\n",
8198 __func__, clki->name, ret);
8199 goto out;
8200 }
8201
8202 if (clki->max_freq) {
8203 ret = clk_set_rate(clki->clk, clki->max_freq);
8204 if (ret) {
8205 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
8206 __func__, clki->name,
8207 clki->max_freq, ret);
8208 goto out;
8209 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03008210 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008211 }
8212 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
8213 clki->name, clk_get_rate(clki->clk));
8214 }
8215out:
8216 return ret;
8217}
8218
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008219static int ufshcd_variant_hba_init(struct ufs_hba *hba)
8220{
8221 int err = 0;
8222
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008223 if (!hba->var || !hba->var->vops)
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008224 goto out;
8225
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008226 err = ufshcd_vops_init(hba);
8227 if (err)
8228 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008229
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008230 err = ufshcd_vops_setup_regulators(hba, true);
8231 if (err)
8232 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008233
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008234 goto out;
8235
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008236out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008237 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008238out:
8239 if (err)
8240 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008241 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008242 return err;
8243}
8244
8245static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
8246{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008247 if (!hba->var || !hba->var->vops)
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008248 return;
8249
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008250 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008251
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008252 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008253}
8254
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008255static int ufshcd_hba_init(struct ufs_hba *hba)
8256{
8257 int err;
8258
Raviv Shvili6a771a62014-09-25 15:32:24 +03008259 /*
8260 * Handle host controller power separately from the UFS device power
8261 * rails as it will help controlling the UFS host controller power
8262 * collapse easily which is different than UFS device power collapse.
8263 * Also, enable the host controller power before we go ahead with rest
8264 * of the initialization here.
8265 */
8266 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008267 if (err)
8268 goto out;
8269
Raviv Shvili6a771a62014-09-25 15:32:24 +03008270 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008271 if (err)
8272 goto out;
8273
Raviv Shvili6a771a62014-09-25 15:32:24 +03008274 err = ufshcd_init_clocks(hba);
8275 if (err)
8276 goto out_disable_hba_vreg;
8277
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008278 err = ufshcd_enable_clocks(hba);
Raviv Shvili6a771a62014-09-25 15:32:24 +03008279 if (err)
8280 goto out_disable_hba_vreg;
8281
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008282 err = ufshcd_init_vreg(hba);
8283 if (err)
8284 goto out_disable_clks;
8285
8286 err = ufshcd_setup_vreg(hba, true);
8287 if (err)
8288 goto out_disable_clks;
8289
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008290 err = ufshcd_variant_hba_init(hba);
8291 if (err)
8292 goto out_disable_vreg;
8293
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03008294 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008295 goto out;
8296
8297out_disable_vreg:
8298 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008299out_disable_clks:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008300 ufshcd_disable_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03008301out_disable_hba_vreg:
8302 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008303out:
8304 return err;
8305}
8306
8307static void ufshcd_hba_exit(struct ufs_hba *hba)
8308{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03008309 if (hba->is_powered) {
8310 ufshcd_variant_hba_exit(hba);
8311 ufshcd_setup_vreg(hba, false);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008312 if (ufshcd_is_clkscaling_supported(hba)) {
Subhash Jadavani9c807702017-04-01 00:35:51 -07008313 if (hba->devfreq)
8314 ufshcd_suspend_clkscaling(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008315 destroy_workqueue(hba->clk_scaling.workq);
8316 }
8317 ufshcd_disable_clocks(hba, false);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03008318 ufshcd_setup_hba_vreg(hba, false);
8319 hba->is_powered = false;
8320 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008321}
8322
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008323static int
8324ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308325{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008326 unsigned char cmd[6] = {REQUEST_SENSE,
8327 0,
8328 0,
8329 0,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008330 UFSHCD_REQ_SENSE_SIZE,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008331 0};
8332 char *buffer;
8333 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308334
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008335 buffer = kzalloc(UFSHCD_REQ_SENSE_SIZE, GFP_KERNEL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008336 if (!buffer) {
8337 ret = -ENOMEM;
8338 goto out;
8339 }
8340
8341 ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008342 UFSHCD_REQ_SENSE_SIZE, NULL,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008343 msecs_to_jiffies(1000), 3, NULL, REQ_PM);
8344 if (ret)
8345 pr_err("%s: failed with err %d\n", __func__, ret);
8346
8347 kfree(buffer);
8348out:
8349 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308350}
8351
8352/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008353 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
8354 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308355 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008356 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308357 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008358 * Returns 0 if requested power mode is set successfully
8359 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308360 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008361static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
8362 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308363{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008364 unsigned char cmd[6] = { START_STOP };
8365 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03008366 struct scsi_device *sdp;
8367 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008368 int ret;
8369
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03008370 spin_lock_irqsave(hba->host->host_lock, flags);
8371 sdp = hba->sdev_ufs_device;
8372 if (sdp) {
8373 ret = scsi_device_get(sdp);
8374 if (!ret && !scsi_device_online(sdp)) {
8375 ret = -ENODEV;
8376 scsi_device_put(sdp);
8377 }
8378 } else {
8379 ret = -ENODEV;
8380 }
8381 spin_unlock_irqrestore(hba->host->host_lock, flags);
8382
8383 if (ret)
8384 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008385
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308386 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008387 * If scsi commands fail, the scsi mid-layer schedules scsi error-
8388 * handling, which would wait for host to be resumed. Since we know
8389 * we are functional while we are here, skip host resume in error
8390 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308391 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008392 hba->host->eh_noresume = 1;
Subhash Jadavani23df2312016-12-16 12:54:30 -08008393 if (!hba->dev_info.is_ufs_dev_wlun_ua_cleared) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008394 ret = ufshcd_send_request_sense(hba, sdp);
8395 if (ret)
8396 goto out;
8397 /* Unit attention condition is cleared now */
Subhash Jadavani23df2312016-12-16 12:54:30 -08008398 hba->dev_info.is_ufs_dev_wlun_ua_cleared = 1;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008399 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308400
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008401 cmd[4] = pwr_mode << 4;
8402
8403 /*
8404 * Current function would be generally called from the power management
8405 * callbacks hence set the REQ_PM flag so that it doesn't resume the
8406 * already suspended childs.
8407 */
8408 ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
8409 START_STOP_TIMEOUT, 0, NULL, REQ_PM);
8410 if (ret) {
8411 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02008412 "START_STOP failed for power mode: %d, result %x\n",
8413 pwr_mode, ret);
Hannes Reinecke21045512015-01-08 07:43:46 +01008414 if (driver_byte(ret) & DRIVER_SENSE)
8415 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008416 }
8417
8418 if (!ret)
8419 hba->curr_dev_pwr_mode = pwr_mode;
8420out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03008421 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008422 hba->host->eh_noresume = 0;
8423 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308424}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308425
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008426static int ufshcd_link_state_transition(struct ufs_hba *hba,
8427 enum uic_link_state req_link_state,
8428 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308429{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008430 int ret = 0;
8431
8432 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308433 return 0;
8434
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008435 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
8436 ret = ufshcd_uic_hibern8_enter(hba);
8437 if (!ret)
8438 ufshcd_set_link_hibern8(hba);
8439 else
8440 goto out;
8441 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308442 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008443 * If autobkops is enabled, link can't be turned off because
8444 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308445 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008446 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
8447 (!check_for_bkops || (check_for_bkops &&
8448 !hba->auto_bkops_enabled))) {
8449 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02008450 * Let's make sure that link is in low power mode, we are doing
8451 * this currently by putting the link in Hibern8. Otherway to
8452 * put the link in low power mode is to send the DME end point
8453 * to device and then send the DME reset command to local
8454 * unipro. But putting the link in hibern8 is much faster.
8455 */
8456 ret = ufshcd_uic_hibern8_enter(hba);
8457 if (ret)
8458 goto out;
8459 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008460 * Change controller state to "reset state" which
8461 * should also put the link in off/reset state
8462 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02008463 ufshcd_hba_stop(hba, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008464 /*
8465 * TODO: Check if we need any delay to make sure that
8466 * controller is reset
8467 */
8468 ufshcd_set_link_off(hba);
8469 }
8470
8471out:
8472 return ret;
8473}
8474
8475static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
8476{
8477 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02008478 * It seems some UFS devices may keep drawing more than sleep current
8479 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
8480 * To avoid this situation, add 2ms delay before putting these UFS
8481 * rails in LPM mode.
8482 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008483 if (!ufshcd_is_link_active(hba))
Yaniv Gardib799fdf2016-03-10 17:37:18 +02008484 usleep_range(2000, 2100);
8485
8486 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008487 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
8488 * power.
8489 *
8490 * If UFS device and link is in OFF state, all power supplies (VCC,
8491 * VCCQ, VCCQ2) can be turned off if power on write protect is not
8492 * required. If UFS link is inactive (Hibern8 or OFF state) and device
8493 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
8494 *
8495 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
8496 * in low power state which would save some power.
8497 */
8498 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
8499 !hba->dev_info.is_lu_power_on_wp) {
8500 ufshcd_setup_vreg(hba, false);
8501 } else if (!ufshcd_is_ufs_dev_active(hba)) {
8502 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
8503 if (!ufshcd_is_link_active(hba)) {
8504 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
8505 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
8506 }
8507 }
8508}
8509
8510static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
8511{
8512 int ret = 0;
8513
8514 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
8515 !hba->dev_info.is_lu_power_on_wp) {
8516 ret = ufshcd_setup_vreg(hba, true);
8517 } else if (!ufshcd_is_ufs_dev_active(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008518 if (!ret && !ufshcd_is_link_active(hba)) {
8519 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
8520 if (ret)
8521 goto vcc_disable;
8522 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
8523 if (ret)
8524 goto vccq_lpm;
8525 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008526 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008527 }
8528 goto out;
8529
8530vccq_lpm:
8531 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
8532vcc_disable:
8533 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
8534out:
8535 return ret;
8536}
8537
8538static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
8539{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008540 if (ufshcd_is_link_off(hba) ||
8541 (ufshcd_is_link_hibern8(hba)
8542 && ufshcd_is_power_collapse_during_hibern8_allowed(hba)))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008543 ufshcd_setup_hba_vreg(hba, false);
8544}
8545
8546static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
8547{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008548 if (ufshcd_is_link_off(hba) ||
8549 (ufshcd_is_link_hibern8(hba)
8550 && ufshcd_is_power_collapse_during_hibern8_allowed(hba)))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008551 ufshcd_setup_hba_vreg(hba, true);
8552}
8553
8554/**
8555 * ufshcd_suspend - helper function for suspend operations
8556 * @hba: per adapter instance
8557 * @pm_op: desired low power operation type
8558 *
8559 * This function will try to put the UFS device and link into low power
8560 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
8561 * (System PM level).
8562 *
8563 * If this function is called during shutdown, it will make sure that
8564 * both UFS device and UFS link is powered off.
8565 *
8566 * NOTE: UFS device & link must be active before we enter in this function.
8567 *
8568 * Returns 0 for success and non-zero for failure
8569 */
8570static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
8571{
8572 int ret = 0;
8573 enum ufs_pm_level pm_lvl;
8574 enum ufs_dev_pwr_mode req_dev_pwr_mode;
8575 enum uic_link_state req_link_state;
8576
8577 hba->pm_op_in_progress = 1;
8578 if (!ufshcd_is_shutdown_pm(pm_op)) {
8579 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
8580 hba->rpm_lvl : hba->spm_lvl;
8581 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
8582 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
8583 } else {
8584 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
8585 req_link_state = UIC_LINK_OFF_STATE;
8586 }
8587
8588 /*
8589 * If we can't transition into any of the low power modes
8590 * just gate the clocks.
8591 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008592 WARN_ON(hba->hibern8_on_idle.is_enabled &&
8593 hba->hibern8_on_idle.active_reqs);
8594 ufshcd_hold_all(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008595 hba->clk_gating.is_suspended = true;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008596 hba->hibern8_on_idle.is_suspended = true;
8597
8598 if (hba->clk_scaling.is_allowed) {
8599 cancel_work_sync(&hba->clk_scaling.suspend_work);
8600 cancel_work_sync(&hba->clk_scaling.resume_work);
8601 ufshcd_suspend_clkscaling(hba);
8602 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008603
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008604 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
8605 req_link_state == UIC_LINK_ACTIVE_STATE) {
8606 goto disable_clks;
8607 }
8608
8609 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
8610 (req_link_state == hba->uic_link_state))
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008611 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008612
8613 /* UFS device & link must be active before we enter in this function */
8614 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
8615 ret = -EINVAL;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008616 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008617 }
8618
8619 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03008620 if (ufshcd_can_autobkops_during_suspend(hba)) {
8621 /*
8622 * The device is idle with no requests in the queue,
8623 * allow background operations if bkops status shows
8624 * that performance might be impacted.
8625 */
8626 ret = ufshcd_urgent_bkops(hba);
8627 if (ret)
8628 goto enable_gating;
8629 } else {
8630 /* make sure that auto bkops is disabled */
8631 ufshcd_disable_auto_bkops(hba);
8632 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008633 }
8634
8635 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
8636 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
8637 !ufshcd_is_runtime_pm(pm_op))) {
8638 /* ensure that bkops is disabled */
8639 ufshcd_disable_auto_bkops(hba);
8640 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
8641 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008642 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008643 }
8644
8645 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
8646 if (ret)
8647 goto set_dev_active;
8648
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008649 if (ufshcd_is_link_hibern8(hba) &&
8650 ufshcd_is_hibern8_on_idle_allowed(hba))
8651 hba->hibern8_on_idle.state = HIBERN8_ENTERED;
8652
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008653 ufshcd_vreg_set_lpm(hba);
8654
8655disable_clks:
8656 /*
8657 * Call vendor specific suspend callback. As these callbacks may access
8658 * vendor specific host controller register space call them before the
8659 * host clocks are ON.
8660 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008661 ret = ufshcd_vops_suspend(hba, pm_op);
8662 if (ret)
8663 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008664
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008665 if (!ufshcd_is_link_active(hba))
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008666 ret = ufshcd_disable_clocks(hba, false);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008667 else
8668 /* If link is active, device ref_clk can't be switched off */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008669 ret = ufshcd_disable_clocks_skip_ref_clk(hba, false);
8670 if (ret)
8671 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008672
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008673 if (ufshcd_is_clkgating_allowed(hba)) {
8674 hba->clk_gating.state = CLKS_OFF;
8675 trace_ufshcd_clk_gating(dev_name(hba->dev),
8676 hba->clk_gating.state);
8677 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008678 /*
8679 * Disable the host irq as host controller as there won't be any
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008680 * host controller transaction expected till resume.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008681 */
8682 ufshcd_disable_irq(hba);
8683 /* Put the host controller in low power mode if possible */
8684 ufshcd_hba_vreg_set_lpm(hba);
8685 goto out;
8686
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008687set_link_active:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008688 if (hba->clk_scaling.is_allowed)
8689 ufshcd_resume_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008690 ufshcd_vreg_set_hpm(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008691 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008692 ufshcd_set_link_active(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008693 } else if (ufshcd_is_link_off(hba)) {
8694 ufshcd_update_error_stats(hba, UFS_ERR_VOPS_SUSPEND);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008695 ufshcd_host_reset_and_restore(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008696 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008697set_dev_active:
8698 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
8699 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008700enable_gating:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008701 if (hba->clk_scaling.is_allowed)
8702 ufshcd_resume_clkscaling(hba);
8703 hba->hibern8_on_idle.is_suspended = false;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008704 hba->clk_gating.is_suspended = false;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008705 ufshcd_release_all(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008706out:
8707 hba->pm_op_in_progress = 0;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008708
8709 if (ret)
8710 ufshcd_update_error_stats(hba, UFS_ERR_SUSPEND);
8711
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008712 return ret;
8713}
8714
8715/**
8716 * ufshcd_resume - helper function for resume operations
8717 * @hba: per adapter instance
8718 * @pm_op: runtime PM or system PM
8719 *
8720 * This function basically brings the UFS device, UniPro link and controller
8721 * to active state.
8722 *
8723 * Returns 0 for success and non-zero for failure
8724 */
8725static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
8726{
8727 int ret;
8728 enum uic_link_state old_link_state;
8729
8730 hba->pm_op_in_progress = 1;
8731 old_link_state = hba->uic_link_state;
8732
8733 ufshcd_hba_vreg_set_hpm(hba);
8734 /* Make sure clocks are enabled before accessing controller */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008735 ret = ufshcd_enable_clocks(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008736 if (ret)
8737 goto out;
8738
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008739 /* enable the host irq as host controller would be active soon */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008740 ufshcd_enable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008741
8742 ret = ufshcd_vreg_set_hpm(hba);
8743 if (ret)
8744 goto disable_irq_and_vops_clks;
8745
8746 /*
8747 * Call vendor specific resume callback. As these callbacks may access
8748 * vendor specific host controller register space call them when the
8749 * host clocks are ON.
8750 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008751 ret = ufshcd_vops_resume(hba, pm_op);
8752 if (ret)
8753 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008754
8755 if (ufshcd_is_link_hibern8(hba)) {
8756 ret = ufshcd_uic_hibern8_exit(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008757 if (!ret) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008758 ufshcd_set_link_active(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008759 if (ufshcd_is_hibern8_on_idle_allowed(hba))
8760 hba->hibern8_on_idle.state = HIBERN8_EXITED;
8761 } else {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008762 goto vendor_suspend;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008763 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008764 } else if (ufshcd_is_link_off(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008765 /*
Subhash Jadavani9c807702017-04-01 00:35:51 -07008766 * A full initialization of the host and the device is required
8767 * since the link was put to off during suspend.
8768 */
8769 ret = ufshcd_reset_and_restore(hba);
8770 /*
8771 * ufshcd_reset_and_restore() should have already
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008772 * set the link state as active
8773 */
8774 if (ret || !ufshcd_is_link_active(hba))
8775 goto vendor_suspend;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008776 /* mark link state as hibern8 exited */
8777 if (ufshcd_is_hibern8_on_idle_allowed(hba))
8778 hba->hibern8_on_idle.state = HIBERN8_EXITED;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008779 }
8780
8781 if (!ufshcd_is_ufs_dev_active(hba)) {
8782 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
8783 if (ret)
8784 goto set_old_link_state;
8785 }
8786
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008787 if (ufshcd_keep_autobkops_enabled_except_suspend(hba))
8788 ufshcd_enable_auto_bkops(hba);
8789 else
8790 /*
8791 * If BKOPs operations are urgently needed at this moment then
8792 * keep auto-bkops enabled or else disable it.
8793 */
8794 ufshcd_urgent_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008795
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008796 hba->clk_gating.is_suspended = false;
8797 hba->hibern8_on_idle.is_suspended = false;
8798
8799 if (hba->clk_scaling.is_allowed)
8800 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03008801
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008802 /* Schedule clock gating in case of no access to UFS device yet */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008803 ufshcd_release_all(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008804 goto out;
8805
8806set_old_link_state:
8807 ufshcd_link_state_transition(hba, old_link_state, 0);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008808 if (ufshcd_is_link_hibern8(hba) &&
8809 ufshcd_is_hibern8_on_idle_allowed(hba))
8810 hba->hibern8_on_idle.state = HIBERN8_ENTERED;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008811vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008812 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008813disable_vreg:
8814 ufshcd_vreg_set_lpm(hba);
8815disable_irq_and_vops_clks:
8816 ufshcd_disable_irq(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008817 if (hba->clk_scaling.is_allowed)
8818 ufshcd_suspend_clkscaling(hba);
8819 ufshcd_disable_clocks(hba, false);
8820 if (ufshcd_is_clkgating_allowed(hba))
8821 hba->clk_gating.state = CLKS_OFF;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008822out:
8823 hba->pm_op_in_progress = 0;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008824
8825 if (ret)
8826 ufshcd_update_error_stats(hba, UFS_ERR_RESUME);
8827
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008828 return ret;
8829}
8830
8831/**
8832 * ufshcd_system_suspend - system suspend routine
8833 * @hba: per adapter instance
8834 * @pm_op: runtime PM or system PM
8835 *
8836 * Check the description of ufshcd_suspend() function for more details.
8837 *
8838 * Returns 0 for success and non-zero for failure
8839 */
8840int ufshcd_system_suspend(struct ufs_hba *hba)
8841{
8842 int ret = 0;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008843 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008844
8845 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03008846 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008847
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008848 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
8849 hba->curr_dev_pwr_mode) &&
8850 (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) ==
8851 hba->uic_link_state))
8852 goto out;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008853
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008854 if (pm_runtime_suspended(hba->dev)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008855 /*
8856 * UFS device and/or UFS link low power states during runtime
8857 * suspend seems to be different than what is expected during
8858 * system suspend. Hence runtime resume the devic & link and
8859 * let the system suspend low power states to take effect.
8860 * TODO: If resume takes longer time, we might have optimize
8861 * it in future by not resuming everything if possible.
8862 */
8863 ret = ufshcd_runtime_resume(hba);
8864 if (ret)
8865 goto out;
8866 }
8867
8868 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
8869out:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008870 trace_ufshcd_system_suspend(dev_name(hba->dev), ret,
8871 ktime_to_us(ktime_sub(ktime_get(), start)),
8872 hba->curr_dev_pwr_mode, hba->uic_link_state);
Dolev Ravive7850602014-09-25 15:32:36 +03008873 if (!ret)
8874 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008875 return ret;
8876}
8877EXPORT_SYMBOL(ufshcd_system_suspend);
8878
8879/**
8880 * ufshcd_system_resume - system resume routine
8881 * @hba: per adapter instance
8882 *
8883 * Returns 0 for success and non-zero for failure
8884 */
8885
8886int ufshcd_system_resume(struct ufs_hba *hba)
8887{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008888 int ret = 0;
8889 ktime_t start = ktime_get();
8890
8891 if (!hba)
8892 return -EINVAL;
8893
8894 if (!hba->is_powered || pm_runtime_suspended(hba->dev))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008895 /*
8896 * Let the runtime resume take care of resuming
8897 * if runtime suspended.
8898 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008899 goto out;
8900 else
8901 ret = ufshcd_resume(hba, UFS_SYSTEM_PM);
8902out:
8903 trace_ufshcd_system_resume(dev_name(hba->dev), ret,
8904 ktime_to_us(ktime_sub(ktime_get(), start)),
8905 hba->curr_dev_pwr_mode, hba->uic_link_state);
8906 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008907}
8908EXPORT_SYMBOL(ufshcd_system_resume);
8909
8910/**
8911 * ufshcd_runtime_suspend - runtime suspend routine
8912 * @hba: per adapter instance
8913 *
8914 * Check the description of ufshcd_suspend() function for more details.
8915 *
8916 * Returns 0 for success and non-zero for failure
8917 */
8918int ufshcd_runtime_suspend(struct ufs_hba *hba)
8919{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008920 int ret = 0;
8921 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008922
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008923 if (!hba)
8924 return -EINVAL;
8925
8926 if (!hba->is_powered)
8927 goto out;
8928 else
8929 ret = ufshcd_suspend(hba, UFS_RUNTIME_PM);
8930out:
8931 trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret,
8932 ktime_to_us(ktime_sub(ktime_get(), start)),
8933 hba->curr_dev_pwr_mode,
8934 hba->uic_link_state);
8935 return ret;
8936
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308937}
8938EXPORT_SYMBOL(ufshcd_runtime_suspend);
8939
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008940/**
8941 * ufshcd_runtime_resume - runtime resume routine
8942 * @hba: per adapter instance
8943 *
8944 * This function basically brings the UFS device, UniPro link and controller
8945 * to active state. Following operations are done in this function:
8946 *
8947 * 1. Turn on all the controller related clocks
8948 * 2. Bring the UniPro link out of Hibernate state
8949 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
8950 * to active state.
8951 * 4. If auto-bkops is enabled on the device, disable it.
8952 *
8953 * So following would be the possible power state after this function return
8954 * successfully:
8955 * S1: UFS device in Active state with VCC rail ON
8956 * UniPro link in Active state
8957 * All the UFS/UniPro controller clocks are ON
8958 *
8959 * Returns 0 for success and non-zero for failure
8960 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308961int ufshcd_runtime_resume(struct ufs_hba *hba)
8962{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008963 int ret = 0;
8964 ktime_t start = ktime_get();
8965
8966 if (!hba)
8967 return -EINVAL;
8968
8969 if (!hba->is_powered)
8970 goto out;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008971 else
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008972 ret = ufshcd_resume(hba, UFS_RUNTIME_PM);
8973out:
8974 trace_ufshcd_runtime_resume(dev_name(hba->dev), ret,
8975 ktime_to_us(ktime_sub(ktime_get(), start)),
8976 hba->curr_dev_pwr_mode,
8977 hba->uic_link_state);
8978 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308979}
8980EXPORT_SYMBOL(ufshcd_runtime_resume);
8981
8982int ufshcd_runtime_idle(struct ufs_hba *hba)
8983{
8984 return 0;
8985}
8986EXPORT_SYMBOL(ufshcd_runtime_idle);
8987
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008988static inline ssize_t ufshcd_pm_lvl_store(struct device *dev,
8989 struct device_attribute *attr,
8990 const char *buf, size_t count,
8991 bool rpm)
8992{
8993 struct ufs_hba *hba = dev_get_drvdata(dev);
8994 unsigned long flags, value;
8995
8996 if (kstrtoul(buf, 0, &value))
8997 return -EINVAL;
8998
8999 if (value >= UFS_PM_LVL_MAX)
9000 return -EINVAL;
9001
9002 spin_lock_irqsave(hba->host->host_lock, flags);
9003 if (rpm)
9004 hba->rpm_lvl = value;
9005 else
9006 hba->spm_lvl = value;
9007 ufshcd_apply_pm_quirks(hba);
9008 spin_unlock_irqrestore(hba->host->host_lock, flags);
9009 return count;
9010}
9011
9012static ssize_t ufshcd_rpm_lvl_show(struct device *dev,
9013 struct device_attribute *attr, char *buf)
9014{
9015 struct ufs_hba *hba = dev_get_drvdata(dev);
9016 int curr_len;
9017 u8 lvl;
9018
9019 curr_len = snprintf(buf, PAGE_SIZE,
9020 "\nCurrent Runtime PM level [%d] => dev_state [%s] link_state [%s]\n",
9021 hba->rpm_lvl,
9022 ufschd_ufs_dev_pwr_mode_to_string(
9023 ufs_pm_lvl_states[hba->rpm_lvl].dev_state),
9024 ufschd_uic_link_state_to_string(
9025 ufs_pm_lvl_states[hba->rpm_lvl].link_state));
9026
9027 curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len),
9028 "\nAll available Runtime PM levels info:\n");
9029 for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++)
9030 curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len),
9031 "\tRuntime PM level [%d] => dev_state [%s] link_state [%s]\n",
9032 lvl,
9033 ufschd_ufs_dev_pwr_mode_to_string(
9034 ufs_pm_lvl_states[lvl].dev_state),
9035 ufschd_uic_link_state_to_string(
9036 ufs_pm_lvl_states[lvl].link_state));
9037
9038 return curr_len;
9039}
9040
9041static ssize_t ufshcd_rpm_lvl_store(struct device *dev,
9042 struct device_attribute *attr, const char *buf, size_t count)
9043{
9044 return ufshcd_pm_lvl_store(dev, attr, buf, count, true);
9045}
9046
9047static void ufshcd_add_rpm_lvl_sysfs_nodes(struct ufs_hba *hba)
9048{
9049 hba->rpm_lvl_attr.show = ufshcd_rpm_lvl_show;
9050 hba->rpm_lvl_attr.store = ufshcd_rpm_lvl_store;
9051 sysfs_attr_init(&hba->rpm_lvl_attr.attr);
9052 hba->rpm_lvl_attr.attr.name = "rpm_lvl";
9053 hba->rpm_lvl_attr.attr.mode = S_IRUGO | S_IWUSR;
9054 if (device_create_file(hba->dev, &hba->rpm_lvl_attr))
9055 dev_err(hba->dev, "Failed to create sysfs for rpm_lvl\n");
9056}
9057
9058static ssize_t ufshcd_spm_lvl_show(struct device *dev,
9059 struct device_attribute *attr, char *buf)
9060{
9061 struct ufs_hba *hba = dev_get_drvdata(dev);
9062 int curr_len;
9063 u8 lvl;
9064
9065 curr_len = snprintf(buf, PAGE_SIZE,
9066 "\nCurrent System PM level [%d] => dev_state [%s] link_state [%s]\n",
9067 hba->spm_lvl,
9068 ufschd_ufs_dev_pwr_mode_to_string(
9069 ufs_pm_lvl_states[hba->spm_lvl].dev_state),
9070 ufschd_uic_link_state_to_string(
9071 ufs_pm_lvl_states[hba->spm_lvl].link_state));
9072
9073 curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len),
9074 "\nAll available System PM levels info:\n");
9075 for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++)
9076 curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len),
9077 "\tSystem PM level [%d] => dev_state [%s] link_state [%s]\n",
9078 lvl,
9079 ufschd_ufs_dev_pwr_mode_to_string(
9080 ufs_pm_lvl_states[lvl].dev_state),
9081 ufschd_uic_link_state_to_string(
9082 ufs_pm_lvl_states[lvl].link_state));
9083
9084 return curr_len;
9085}
9086
9087static ssize_t ufshcd_spm_lvl_store(struct device *dev,
9088 struct device_attribute *attr, const char *buf, size_t count)
9089{
9090 return ufshcd_pm_lvl_store(dev, attr, buf, count, false);
9091}
9092
9093static void ufshcd_add_spm_lvl_sysfs_nodes(struct ufs_hba *hba)
9094{
9095 hba->spm_lvl_attr.show = ufshcd_spm_lvl_show;
9096 hba->spm_lvl_attr.store = ufshcd_spm_lvl_store;
9097 sysfs_attr_init(&hba->spm_lvl_attr.attr);
9098 hba->spm_lvl_attr.attr.name = "spm_lvl";
9099 hba->spm_lvl_attr.attr.mode = S_IRUGO | S_IWUSR;
9100 if (device_create_file(hba->dev, &hba->spm_lvl_attr))
9101 dev_err(hba->dev, "Failed to create sysfs for spm_lvl\n");
9102}
9103
9104static inline void ufshcd_add_sysfs_nodes(struct ufs_hba *hba)
9105{
9106 ufshcd_add_rpm_lvl_sysfs_nodes(hba);
9107 ufshcd_add_spm_lvl_sysfs_nodes(hba);
9108}
9109
Subhash Jadavani9c807702017-04-01 00:35:51 -07009110static void ufshcd_shutdown_clkscaling(struct ufs_hba *hba)
9111{
9112 bool suspend = false;
9113 unsigned long flags;
9114
9115 spin_lock_irqsave(hba->host->host_lock, flags);
9116 if (hba->clk_scaling.is_allowed) {
9117 hba->clk_scaling.is_allowed = false;
9118 suspend = true;
9119 }
9120 spin_unlock_irqrestore(hba->host->host_lock, flags);
9121
9122 /**
9123 * Scaling may be scheduled before, hence make sure it
9124 * doesn't race with shutdown
9125 */
9126 if (ufshcd_is_clkscaling_supported(hba)) {
9127 device_remove_file(hba->dev, &hba->clk_scaling.enable_attr);
9128 cancel_work_sync(&hba->clk_scaling.suspend_work);
9129 cancel_work_sync(&hba->clk_scaling.resume_work);
9130 if (suspend)
9131 ufshcd_suspend_clkscaling(hba);
9132 }
9133
9134 /* Unregister so that devfreq_monitor can't race with shutdown */
9135 if (hba->devfreq)
9136 devfreq_remove_device(hba->devfreq);
9137}
9138
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309139/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009140 * ufshcd_shutdown - shutdown routine
9141 * @hba: per adapter instance
9142 *
9143 * This function would power off both UFS device and UFS link.
9144 *
9145 * Returns 0 always to allow force shutdown even in case of errors.
9146 */
9147int ufshcd_shutdown(struct ufs_hba *hba)
9148{
Subhash Jadavani9c807702017-04-01 00:35:51 -07009149 int ret = 0;
9150
9151 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
9152 goto out;
9153
9154 pm_runtime_get_sync(hba->dev);
9155 ufshcd_hold_all(hba);
9156 ufshcd_mark_shutdown_ongoing(hba);
9157 ufshcd_shutdown_clkscaling(hba);
9158 /**
9159 * (1) Acquire the lock to stop any more requests
9160 * (2) Wait for all issued requests to complete
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009161 */
Subhash Jadavani9c807702017-04-01 00:35:51 -07009162 ufshcd_get_write_lock(hba);
9163 ufshcd_scsi_block_requests(hba);
9164 ret = ufshcd_wait_for_doorbell_clr(hba, U64_MAX);
9165 if (ret)
9166 dev_err(hba->dev, "%s: waiting for DB clear: failed: %d\n",
9167 __func__, ret);
9168 /* Requests may have errored out above, let it be handled */
9169 flush_work(&hba->eh_work);
9170 /* reqs issued from contexts other than shutdown will fail from now */
9171 ufshcd_scsi_unblock_requests(hba);
9172 ufshcd_release_all(hba);
9173 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
9174out:
9175 if (ret)
9176 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
9177 /* allow force shutdown even in case of errors */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009178 return 0;
9179}
9180EXPORT_SYMBOL(ufshcd_shutdown);
9181
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -07009182/*
9183 * Values permitted 0, 1, 2.
9184 * 0 -> Disable IO latency histograms (default)
9185 * 1 -> Enable IO latency histograms
9186 * 2 -> Zero out IO latency histograms
9187 */
9188static ssize_t
9189latency_hist_store(struct device *dev, struct device_attribute *attr,
9190 const char *buf, size_t count)
9191{
9192 struct ufs_hba *hba = dev_get_drvdata(dev);
9193 long value;
9194
9195 if (kstrtol(buf, 0, &value))
9196 return -EINVAL;
9197 if (value == BLK_IO_LAT_HIST_ZERO)
9198 blk_zero_latency_hist(&hba->io_lat_s);
9199 else if (value == BLK_IO_LAT_HIST_ENABLE ||
9200 value == BLK_IO_LAT_HIST_DISABLE)
9201 hba->latency_hist_enabled = value;
9202 return count;
9203}
9204
9205ssize_t
9206latency_hist_show(struct device *dev, struct device_attribute *attr,
9207 char *buf)
9208{
9209 struct ufs_hba *hba = dev_get_drvdata(dev);
9210
9211 return blk_latency_hist_show(&hba->io_lat_s, buf);
9212}
9213
9214static DEVICE_ATTR(latency_hist, S_IRUGO | S_IWUSR,
9215 latency_hist_show, latency_hist_store);
9216
9217static void
9218ufshcd_init_latency_hist(struct ufs_hba *hba)
9219{
9220 if (device_create_file(hba->dev, &dev_attr_latency_hist))
9221 dev_err(hba->dev, "Failed to create latency_hist sysfs entry\n");
9222}
9223
9224static void
9225ufshcd_exit_latency_hist(struct ufs_hba *hba)
9226{
9227 device_create_file(hba->dev, &dev_attr_latency_hist);
9228}
9229
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009230/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309231 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309232 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309233 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309234 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309235void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309236{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05309237 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309238 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05309239 ufshcd_disable_intr(hba, hba->intr_mask);
Yaniv Gardi596585a2016-03-10 17:37:08 +02009240 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309241
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03009242 ufshcd_exit_clk_gating(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009243 ufshcd_exit_hibern8_on_idle(hba);
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -07009244 ufshcd_exit_latency_hist(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009245 if (ufshcd_is_clkscaling_supported(hba)) {
9246 device_remove_file(hba->dev, &hba->clk_scaling.enable_attr);
Kyle Yan65be4a52016-10-31 15:05:00 -07009247 if (hba->devfreq)
9248 devfreq_remove_device(hba->devfreq);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009249 }
Channagoud Kadabi8810e5f2017-02-17 16:01:05 -08009250
9251 ufshcd_exit_latency_hist(hba);
9252
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03009253 ufshcd_hba_exit(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009254 ufsdbg_remove_debugfs(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309255}
9256EXPORT_SYMBOL_GPL(ufshcd_remove);
9257
9258/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02009259 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
9260 * @hba: pointer to Host Bus Adapter (HBA)
9261 */
9262void ufshcd_dealloc_host(struct ufs_hba *hba)
9263{
9264 scsi_host_put(hba->host);
9265}
9266EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
9267
9268/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09009269 * ufshcd_set_dma_mask - Set dma mask based on the controller
9270 * addressing capability
9271 * @hba: per adapter instance
9272 *
9273 * Returns 0 for success, non-zero for failure
9274 */
9275static int ufshcd_set_dma_mask(struct ufs_hba *hba)
9276{
9277 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
9278 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
9279 return 0;
9280 }
9281 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
9282}
9283
9284/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009285 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309286 * @dev: pointer to device handle
9287 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309288 * Returns 0 on success, non-zero value on failure
9289 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009290int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309291{
9292 struct Scsi_Host *host;
9293 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009294 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309295
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309296 if (!dev) {
9297 dev_err(dev,
9298 "Invalid memory reference for dev is NULL\n");
9299 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309300 goto out_error;
9301 }
9302
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309303 host = scsi_host_alloc(&ufshcd_driver_template,
9304 sizeof(struct ufs_hba));
9305 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309306 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309307 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309308 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309309 }
9310 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309311 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309312 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009313 *hba_handle = hba;
9314
9315out_error:
9316 return err;
9317}
9318EXPORT_SYMBOL(ufshcd_alloc_host);
9319
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009320/**
9321 * ufshcd_is_devfreq_scaling_required - check if scaling is required or not
9322 * @hba: per adapter instance
9323 * @scale_up: True if scaling up and false if scaling down
9324 *
9325 * Returns true if scaling is required, false otherwise.
9326 */
9327static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba,
9328 bool scale_up)
Sahitya Tummala856b3482014-09-25 15:32:34 +03009329{
Sahitya Tummala856b3482014-09-25 15:32:34 +03009330 struct ufs_clk_info *clki;
9331 struct list_head *head = &hba->clk_list_head;
9332
9333 if (!head || list_empty(head))
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009334 return false;
Yaniv Gardif06fcc72015-10-28 13:15:51 +02009335
Sahitya Tummala856b3482014-09-25 15:32:34 +03009336 list_for_each_entry(clki, head, list) {
9337 if (!IS_ERR_OR_NULL(clki->clk)) {
9338 if (scale_up && clki->max_freq) {
9339 if (clki->curr_freq == clki->max_freq)
9340 continue;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009341 return true;
Sahitya Tummala856b3482014-09-25 15:32:34 +03009342 } else if (!scale_up && clki->min_freq) {
9343 if (clki->curr_freq == clki->min_freq)
9344 continue;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009345 return true;
Sahitya Tummala856b3482014-09-25 15:32:34 +03009346 }
9347 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03009348 }
Yaniv Gardif06fcc72015-10-28 13:15:51 +02009349
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009350 return false;
9351}
Yaniv Gardif06fcc72015-10-28 13:15:51 +02009352
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009353/**
9354 * ufshcd_scale_gear - scale up/down UFS gear
9355 * @hba: per adapter instance
9356 * @scale_up: True for scaling up gear and false for scaling down
9357 *
9358 * Returns 0 for success,
9359 * Returns -EBUSY if scaling can't happen at this time
9360 * Returns non-zero for any other errors
9361 */
9362static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up)
9363{
9364 int ret = 0;
9365 struct ufs_pa_layer_attr new_pwr_info;
9366 u32 scale_down_gear = ufshcd_vops_get_scale_down_gear(hba);
9367
9368 BUG_ON(!hba->clk_scaling.saved_pwr_info.is_valid);
9369
9370 if (scale_up) {
9371 memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info.info,
9372 sizeof(struct ufs_pa_layer_attr));
9373 } else {
9374 memcpy(&new_pwr_info, &hba->pwr_info,
9375 sizeof(struct ufs_pa_layer_attr));
9376
9377 if (hba->pwr_info.gear_tx > scale_down_gear
9378 || hba->pwr_info.gear_rx > scale_down_gear) {
9379 /* save the current power mode */
9380 memcpy(&hba->clk_scaling.saved_pwr_info.info,
9381 &hba->pwr_info,
9382 sizeof(struct ufs_pa_layer_attr));
9383
9384 /* scale down gear */
9385 new_pwr_info.gear_tx = scale_down_gear;
9386 new_pwr_info.gear_rx = scale_down_gear;
Subhash Jadavani4f0df17b2016-12-16 13:19:27 -08009387 if (!(hba->dev_info.quirks & UFS_DEVICE_NO_FASTAUTO)) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009388 new_pwr_info.pwr_tx = FASTAUTO_MODE;
9389 new_pwr_info.pwr_rx = FASTAUTO_MODE;
9390 }
9391 }
9392 }
9393
9394 ret = ufshcd_change_power_mode(hba, &new_pwr_info);
9395
9396 if (ret)
9397 dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d), scale_up = %d",
9398 __func__, ret,
9399 hba->pwr_info.gear_tx, hba->pwr_info.gear_rx,
9400 new_pwr_info.gear_tx, new_pwr_info.gear_rx,
9401 scale_up);
9402
Sahitya Tummala856b3482014-09-25 15:32:34 +03009403 return ret;
9404}
9405
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009406static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba)
9407{
9408 #define DOORBELL_CLR_TOUT_US (1000 * 1000) /* 1 sec */
9409 int ret = 0;
9410 /*
9411 * make sure that there are no outstanding requests when
9412 * clock scaling is in progress
9413 */
9414 ufshcd_scsi_block_requests(hba);
Subhash Jadavani9c807702017-04-01 00:35:51 -07009415 down_write(&hba->lock);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009416 if (ufshcd_wait_for_doorbell_clr(hba, DOORBELL_CLR_TOUT_US)) {
9417 ret = -EBUSY;
Subhash Jadavani9c807702017-04-01 00:35:51 -07009418 up_write(&hba->lock);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009419 ufshcd_scsi_unblock_requests(hba);
9420 }
9421
9422 return ret;
9423}
9424
9425static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba)
9426{
Subhash Jadavani9c807702017-04-01 00:35:51 -07009427 up_write(&hba->lock);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009428 ufshcd_scsi_unblock_requests(hba);
9429}
9430
9431/**
9432 * ufshcd_devfreq_scale - scale up/down UFS clocks and gear
9433 * @hba: per adapter instance
9434 * @scale_up: True for scaling up and false for scalin down
9435 *
9436 * Returns 0 for success,
9437 * Returns -EBUSY if scaling can't happen at this time
9438 * Returns non-zero for any other errors
9439 */
9440static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up)
9441{
9442 int ret = 0;
9443
9444 /* let's not get into low power until clock scaling is completed */
9445 ufshcd_hold_all(hba);
9446
9447 ret = ufshcd_clock_scaling_prepare(hba);
9448 if (ret)
9449 goto out;
9450
9451 /* scale down the gear before scaling down clocks */
9452 if (!scale_up) {
9453 ret = ufshcd_scale_gear(hba, false);
9454 if (ret)
9455 goto clk_scaling_unprepare;
9456 }
9457
Subhash Jadavani67c53302017-03-22 17:00:54 -07009458 /*
9459 * If auto hibern8 is supported then put the link in
9460 * hibern8 manually, this is to avoid auto hibern8
9461 * racing during clock frequency scaling sequence.
9462 */
9463 if (ufshcd_is_auto_hibern8_supported(hba)) {
9464 ret = ufshcd_uic_hibern8_enter(hba);
9465 if (ret)
9466 /* link will be bad state so no need to scale_up_gear */
9467 return ret;
9468 }
9469
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009470 ret = ufshcd_scale_clks(hba, scale_up);
9471 if (ret)
9472 goto scale_up_gear;
9473
Subhash Jadavani67c53302017-03-22 17:00:54 -07009474 if (ufshcd_is_auto_hibern8_supported(hba)) {
9475 ret = ufshcd_uic_hibern8_exit(hba);
9476 if (ret)
9477 /* link will be bad state so no need to scale_up_gear */
9478 return ret;
9479 }
9480
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009481 /* scale up the gear after scaling up clocks */
9482 if (scale_up) {
9483 ret = ufshcd_scale_gear(hba, true);
9484 if (ret) {
9485 ufshcd_scale_clks(hba, false);
9486 goto clk_scaling_unprepare;
9487 }
9488 }
9489
9490 if (!ret) {
9491 hba->clk_scaling.is_scaled_up = scale_up;
9492 if (scale_up)
9493 hba->clk_gating.delay_ms =
9494 hba->clk_gating.delay_ms_perf;
9495 else
9496 hba->clk_gating.delay_ms =
9497 hba->clk_gating.delay_ms_pwr_save;
9498 }
9499
9500 goto clk_scaling_unprepare;
9501
9502scale_up_gear:
9503 if (!scale_up)
9504 ufshcd_scale_gear(hba, true);
9505clk_scaling_unprepare:
9506 ufshcd_clock_scaling_unprepare(hba);
9507out:
9508 ufshcd_release_all(hba);
9509 return ret;
9510}
9511
9512static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba)
9513{
9514 unsigned long flags;
9515
9516 devfreq_suspend_device(hba->devfreq);
9517 spin_lock_irqsave(hba->host->host_lock, flags);
9518 hba->clk_scaling.window_start_t = 0;
9519 spin_unlock_irqrestore(hba->host->host_lock, flags);
9520}
9521
9522static void ufshcd_suspend_clkscaling(struct ufs_hba *hba)
9523{
9524 unsigned long flags;
9525 bool suspend = false;
9526
9527 if (!ufshcd_is_clkscaling_supported(hba))
9528 return;
9529
9530 spin_lock_irqsave(hba->host->host_lock, flags);
9531 if (!hba->clk_scaling.is_suspended) {
9532 suspend = true;
9533 hba->clk_scaling.is_suspended = true;
9534 }
9535 spin_unlock_irqrestore(hba->host->host_lock, flags);
9536
9537 if (suspend)
9538 __ufshcd_suspend_clkscaling(hba);
9539}
9540
9541static void ufshcd_resume_clkscaling(struct ufs_hba *hba)
9542{
9543 unsigned long flags;
9544 bool resume = false;
9545
9546 if (!ufshcd_is_clkscaling_supported(hba))
9547 return;
9548
9549 spin_lock_irqsave(hba->host->host_lock, flags);
9550 if (hba->clk_scaling.is_suspended) {
9551 resume = true;
9552 hba->clk_scaling.is_suspended = false;
9553 }
9554 spin_unlock_irqrestore(hba->host->host_lock, flags);
9555
9556 if (resume)
9557 devfreq_resume_device(hba->devfreq);
9558}
9559
9560static ssize_t ufshcd_clkscale_enable_show(struct device *dev,
9561 struct device_attribute *attr, char *buf)
9562{
9563 struct ufs_hba *hba = dev_get_drvdata(dev);
9564
9565 return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_scaling.is_allowed);
9566}
9567
9568static ssize_t ufshcd_clkscale_enable_store(struct device *dev,
9569 struct device_attribute *attr, const char *buf, size_t count)
9570{
9571 struct ufs_hba *hba = dev_get_drvdata(dev);
9572 u32 value;
9573 int err;
9574
9575 if (kstrtou32(buf, 0, &value))
9576 return -EINVAL;
9577
9578 value = !!value;
9579 if (value == hba->clk_scaling.is_allowed)
9580 goto out;
9581
9582 pm_runtime_get_sync(hba->dev);
9583 ufshcd_hold(hba, false);
9584
9585 cancel_work_sync(&hba->clk_scaling.suspend_work);
9586 cancel_work_sync(&hba->clk_scaling.resume_work);
9587
9588 hba->clk_scaling.is_allowed = value;
9589
9590 if (value) {
9591 ufshcd_resume_clkscaling(hba);
9592 } else {
9593 ufshcd_suspend_clkscaling(hba);
9594 err = ufshcd_devfreq_scale(hba, true);
9595 if (err)
9596 dev_err(hba->dev, "%s: failed to scale clocks up %d\n",
9597 __func__, err);
9598 }
9599
9600 ufshcd_release(hba, false);
9601 pm_runtime_put_sync(hba->dev);
9602out:
9603 return count;
9604}
9605
9606static void ufshcd_clk_scaling_suspend_work(struct work_struct *work)
9607{
9608 struct ufs_hba *hba = container_of(work, struct ufs_hba,
9609 clk_scaling.suspend_work);
9610 unsigned long irq_flags;
9611
9612 spin_lock_irqsave(hba->host->host_lock, irq_flags);
9613 if (hba->clk_scaling.active_reqs || hba->clk_scaling.is_suspended) {
9614 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
9615 return;
9616 }
9617 hba->clk_scaling.is_suspended = true;
9618 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
9619
9620 __ufshcd_suspend_clkscaling(hba);
9621}
9622
9623static void ufshcd_clk_scaling_resume_work(struct work_struct *work)
9624{
9625 struct ufs_hba *hba = container_of(work, struct ufs_hba,
9626 clk_scaling.resume_work);
9627 unsigned long irq_flags;
9628
9629 spin_lock_irqsave(hba->host->host_lock, irq_flags);
9630 if (!hba->clk_scaling.is_suspended) {
9631 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
9632 return;
9633 }
9634 hba->clk_scaling.is_suspended = false;
9635 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
9636
9637 devfreq_resume_device(hba->devfreq);
9638}
9639
Sahitya Tummala856b3482014-09-25 15:32:34 +03009640static int ufshcd_devfreq_target(struct device *dev,
9641 unsigned long *freq, u32 flags)
9642{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009643 int ret = 0;
Sahitya Tummala856b3482014-09-25 15:32:34 +03009644 struct ufs_hba *hba = dev_get_drvdata(dev);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009645 unsigned long irq_flags;
9646 ktime_t start;
9647 bool scale_up, sched_clk_scaling_suspend_work = false;
Sahitya Tummala856b3482014-09-25 15:32:34 +03009648
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009649 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03009650 return -EINVAL;
9651
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009652 if ((*freq > 0) && (*freq < UINT_MAX)) {
9653 dev_err(hba->dev, "%s: invalid freq = %lu\n", __func__, *freq);
9654 return -EINVAL;
9655 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03009656
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009657 spin_lock_irqsave(hba->host->host_lock, irq_flags);
9658 if (ufshcd_eh_in_progress(hba)) {
9659 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
9660 return 0;
9661 }
9662
9663 if (!hba->clk_scaling.active_reqs)
9664 sched_clk_scaling_suspend_work = true;
9665
9666 scale_up = (*freq == UINT_MAX) ? true : false;
9667 if (!ufshcd_is_devfreq_scaling_required(hba, scale_up)) {
9668 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
9669 ret = 0;
9670 goto out; /* no state change required */
9671 }
9672 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
9673
9674 start = ktime_get();
9675 ret = ufshcd_devfreq_scale(hba, scale_up);
9676 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
9677 (scale_up ? "up" : "down"),
9678 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
9679
9680out:
9681 if (sched_clk_scaling_suspend_work)
9682 queue_work(hba->clk_scaling.workq,
9683 &hba->clk_scaling.suspend_work);
9684
9685 return ret;
Sahitya Tummala856b3482014-09-25 15:32:34 +03009686}
9687
9688static int ufshcd_devfreq_get_dev_status(struct device *dev,
9689 struct devfreq_dev_status *stat)
9690{
9691 struct ufs_hba *hba = dev_get_drvdata(dev);
9692 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
9693 unsigned long flags;
9694
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009695 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03009696 return -EINVAL;
9697
9698 memset(stat, 0, sizeof(*stat));
9699
9700 spin_lock_irqsave(hba->host->host_lock, flags);
9701 if (!scaling->window_start_t)
9702 goto start_window;
9703
9704 if (scaling->is_busy_started)
9705 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
9706 scaling->busy_start_t));
9707
9708 stat->total_time = jiffies_to_usecs((long)jiffies -
9709 (long)scaling->window_start_t);
9710 stat->busy_time = scaling->tot_busy_t;
9711start_window:
9712 scaling->window_start_t = jiffies;
9713 scaling->tot_busy_t = 0;
9714
9715 if (hba->outstanding_reqs) {
9716 scaling->busy_start_t = ktime_get();
9717 scaling->is_busy_started = true;
9718 } else {
9719 scaling->busy_start_t = ktime_set(0, 0);
9720 scaling->is_busy_started = false;
9721 }
9722 spin_unlock_irqrestore(hba->host->host_lock, flags);
9723 return 0;
9724}
9725
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009726static void ufshcd_clkscaling_init_sysfs(struct ufs_hba *hba)
9727{
9728 hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show;
9729 hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store;
9730 sysfs_attr_init(&hba->clk_scaling.enable_attr.attr);
9731 hba->clk_scaling.enable_attr.attr.name = "clkscale_enable";
9732 hba->clk_scaling.enable_attr.attr.mode = S_IRUGO | S_IWUSR;
9733 if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr))
9734 dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n");
9735}
Sahitya Tummala856b3482014-09-25 15:32:34 +03009736
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009737static void ufshcd_init_lanes_per_dir(struct ufs_hba *hba)
9738{
9739 struct device *dev = hba->dev;
9740 int ret;
9741
9742 ret = of_property_read_u32(dev->of_node, "lanes-per-direction",
9743 &hba->lanes_per_direction);
9744 if (ret) {
9745 dev_dbg(hba->dev,
9746 "%s: failed to read lanes-per-direction, ret=%d\n",
9747 __func__, ret);
9748 hba->lanes_per_direction = UFSHCD_DEFAULT_LANES_PER_DIRECTION;
9749 }
9750}
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009751/**
9752 * ufshcd_init - Driver initialization routine
9753 * @hba: per-adapter instance
9754 * @mmio_base: base register address
9755 * @irq: Interrupt line of device
9756 * Returns 0 on success, non-zero value on failure
9757 */
9758int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
9759{
9760 int err;
9761 struct Scsi_Host *host = hba->host;
9762 struct device *dev = hba->dev;
9763
9764 if (!mmio_base) {
9765 dev_err(hba->dev,
9766 "Invalid memory reference for mmio_base is NULL\n");
9767 err = -ENODEV;
9768 goto out_error;
9769 }
9770
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309771 hba->mmio_base = mmio_base;
9772 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309773
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009774 ufshcd_init_lanes_per_dir(hba);
9775
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03009776 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009777 if (err)
9778 goto out_error;
9779
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309780 /* Read capabilities registers */
9781 ufshcd_hba_capabilities(hba);
9782
9783 /* Get UFS version supported by the controller */
9784 hba->ufs_version = ufshcd_get_ufs_version(hba);
9785
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009786 /* print error message if ufs_version is not valid */
9787 if ((hba->ufs_version != UFSHCI_VERSION_10) &&
9788 (hba->ufs_version != UFSHCI_VERSION_11) &&
9789 (hba->ufs_version != UFSHCI_VERSION_20) &&
9790 (hba->ufs_version != UFSHCI_VERSION_21))
9791 dev_err(hba->dev, "invalid UFS version 0x%x\n",
9792 hba->ufs_version);
9793
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05309794 /* Get Interrupt bit mask per version */
9795 hba->intr_mask = ufshcd_get_intr_mask(hba);
9796
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009797 /* Enable debug prints */
9798 hba->ufshcd_dbg_print = DEFAULT_UFSHCD_DBG_PRINT_EN;
9799
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09009800 err = ufshcd_set_dma_mask(hba);
9801 if (err) {
9802 dev_err(hba->dev, "set dma mask failed\n");
9803 goto out_disable;
9804 }
9805
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309806 /* Allocate memory for host memory space */
9807 err = ufshcd_memory_alloc(hba);
9808 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309809 dev_err(hba->dev, "Memory allocation failed\n");
9810 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309811 }
9812
9813 /* Configure LRB */
9814 ufshcd_host_memory_configure(hba);
9815
9816 host->can_queue = hba->nutrs;
9817 host->cmd_per_lun = hba->nutrs;
9818 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03009819 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309820 host->max_channel = UFSHCD_MAX_CHANNEL;
9821 host->unique_id = host->host_no;
9822 host->max_cmd_len = MAX_CDB_SIZE;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009823 host->set_dbd_for_caching = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309824
Dolev Raviv7eb584d2014-09-25 15:32:31 +03009825 hba->max_pwr_info.is_valid = false;
9826
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309827 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05309828 init_waitqueue_head(&hba->tm_wq);
9829 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309830
9831 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05309832 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05309833 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309834
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309835 /* Initialize UIC command mutex */
9836 mutex_init(&hba->uic_cmd_mutex);
9837
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05309838 /* Initialize mutex for device management commands */
9839 mutex_init(&hba->dev_cmd.lock);
9840
Subhash Jadavani9c807702017-04-01 00:35:51 -07009841 init_rwsem(&hba->lock);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009842
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05309843 /* Initialize device management tag acquire wait queue */
9844 init_waitqueue_head(&hba->dev_cmd.tag_wq);
9845
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03009846 ufshcd_init_clk_gating(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009847 ufshcd_init_hibern8_on_idle(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02009848
9849 /*
9850 * In order to avoid any spurious interrupt immediately after
9851 * registering UFS controller interrupt handler, clear any pending UFS
9852 * interrupt status and disable all the UFS interrupts.
9853 */
9854 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
9855 REG_INTERRUPT_STATUS);
9856 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
9857 /*
9858 * Make sure that UFS interrupts are disabled and any pending interrupt
9859 * status is cleared before registering UFS interrupt handler.
9860 */
9861 mb();
9862
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309863 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09009864 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309865 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309866 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03009867 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009868 } else {
9869 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309870 }
9871
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309872 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309873 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309874 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03009875 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309876 }
9877
Subhash Jadavani9c807702017-04-01 00:35:51 -07009878 /* Reset controller to power on reset (POR) state */
9879 ufshcd_vops_full_reset(hba);
9880
9881 /* reset connected UFS device */
9882 err = ufshcd_reset_device(hba);
9883 if (err)
9884 dev_warn(hba->dev, "%s: device reset failed. err %d\n",
9885 __func__, err);
9886
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309887 /* Host controller enable */
9888 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309889 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309890 dev_err(hba->dev, "Host controller enable failed\n");
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009891 ufshcd_print_host_regs(hba);
9892 ufshcd_print_host_state(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309893 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309894 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309895
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009896 if (ufshcd_is_clkscaling_supported(hba)) {
9897 char wq_name[sizeof("ufs_clkscaling_00")];
9898
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009899 INIT_WORK(&hba->clk_scaling.suspend_work,
9900 ufshcd_clk_scaling_suspend_work);
9901 INIT_WORK(&hba->clk_scaling.resume_work,
9902 ufshcd_clk_scaling_resume_work);
9903
9904 snprintf(wq_name, ARRAY_SIZE(wq_name), "ufs_clkscaling_%d",
9905 host->host_no);
9906 hba->clk_scaling.workq = create_singlethread_workqueue(wq_name);
9907
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009908 ufshcd_clkscaling_init_sysfs(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03009909 }
9910
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009911 /*
9912 * If rpm_lvl and and spm_lvl are not already set to valid levels,
9913 * set the default power management level for UFS runtime and system
9914 * suspend. Default power saving mode selected is keeping UFS link in
9915 * Hibern8 state and UFS device in sleep.
9916 */
9917 if (!ufshcd_is_valid_pm_lvl(hba->rpm_lvl))
9918 hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
9919 UFS_SLEEP_PWR_MODE,
9920 UIC_LINK_HIBERN8_STATE);
9921 if (!ufshcd_is_valid_pm_lvl(hba->spm_lvl))
9922 hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
9923 UFS_SLEEP_PWR_MODE,
9924 UIC_LINK_HIBERN8_STATE);
9925
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05309926 /* Hold auto suspend until async scan completes */
9927 pm_runtime_get_sync(dev);
9928
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -07009929 ufshcd_init_latency_hist(hba);
9930
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009931 /*
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009932 * We are assuming that device wasn't put in sleep/power-down
9933 * state exclusively during the boot stage before kernel.
9934 * This assumption helps avoid doing link startup twice during
9935 * ufshcd_probe_hba().
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009936 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009937 ufshcd_set_ufs_dev_active(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009938
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309939 async_schedule(ufshcd_async_scan, hba);
9940
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009941 ufsdbg_add_debugfs(hba);
9942
9943 ufshcd_add_sysfs_nodes(hba);
9944
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309945 return 0;
9946
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309947out_remove_scsi_host:
9948 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03009949exit_gating:
9950 ufshcd_exit_clk_gating(hba);
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -07009951 ufshcd_exit_latency_hist(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309952out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009953 hba->is_irq_enabled = false;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03009954 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309955out_error:
9956 return err;
9957}
9958EXPORT_SYMBOL_GPL(ufshcd_init);
9959
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309960MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
9961MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05309962MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309963MODULE_LICENSE("GPL");
9964MODULE_VERSION(UFSHCD_DRIVER_VERSION);